Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2004 Topspin Communications. All rights reserved. |
Roland Dreier | 2a1d9b7 | 2005-08-10 23:03:10 -0700 | [diff] [blame] | 3 | * Copyright (c) 2005 Mellanox Technologies. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * This software is available to you under a choice of one of two |
| 6 | * licenses. You may choose to be licensed under the terms of the GNU |
| 7 | * General Public License (GPL) Version 2, available from the file |
| 8 | * COPYING in the main directory of this source tree, or the |
| 9 | * OpenIB.org BSD license below: |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or |
| 12 | * without modification, are permitted provided that the following |
| 13 | * conditions are met: |
| 14 | * |
| 15 | * - Redistributions of source code must retain the above |
| 16 | * copyright notice, this list of conditions and the following |
| 17 | * disclaimer. |
| 18 | * |
| 19 | * - Redistributions in binary form must reproduce the above |
| 20 | * copyright notice, this list of conditions and the following |
| 21 | * disclaimer in the documentation and/or other materials |
| 22 | * provided with the distribution. |
| 23 | * |
| 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 25 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 26 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 27 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 28 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 29 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 30 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 31 | * SOFTWARE. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | */ |
| 33 | |
| 34 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/errno.h> |
| 36 | |
| 37 | #include "mthca_dev.h" |
| 38 | #include "mthca_cmd.h" |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 39 | #include "mthca_memfree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 41 | struct mthca_mtt { |
| 42 | struct mthca_buddy *buddy; |
| 43 | int order; |
| 44 | u32 first_seg; |
| 45 | }; |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | /* |
| 48 | * Must be packed because mtt_seg is 64 bits but only aligned to 32 bits. |
| 49 | */ |
| 50 | struct mthca_mpt_entry { |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 51 | __be32 flags; |
| 52 | __be32 page_size; |
| 53 | __be32 key; |
| 54 | __be32 pd; |
| 55 | __be64 start; |
| 56 | __be64 length; |
| 57 | __be32 lkey; |
| 58 | __be32 window_count; |
| 59 | __be32 window_count_limit; |
| 60 | __be64 mtt_seg; |
| 61 | __be32 mtt_sz; /* Arbel only */ |
| 62 | u32 reserved[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } __attribute__((packed)); |
| 64 | |
| 65 | #define MTHCA_MPT_FLAG_SW_OWNS (0xfUL << 28) |
| 66 | #define MTHCA_MPT_FLAG_MIO (1 << 17) |
| 67 | #define MTHCA_MPT_FLAG_BIND_ENABLE (1 << 15) |
| 68 | #define MTHCA_MPT_FLAG_PHYSICAL (1 << 9) |
| 69 | #define MTHCA_MPT_FLAG_REGION (1 << 8) |
| 70 | |
| 71 | #define MTHCA_MTT_FLAG_PRESENT 1 |
| 72 | |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 73 | #define MTHCA_MPT_STATUS_SW 0xF0 |
| 74 | #define MTHCA_MPT_STATUS_HW 0x00 |
| 75 | |
Eli Cohen | 651eaac | 2006-03-02 12:40:46 -0800 | [diff] [blame] | 76 | #define SINAI_FMR_KEY_INC 0x1000000 |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | /* |
| 79 | * Buddy allocator for MTT segments (currently not very efficient |
| 80 | * since it doesn't keep a free list and just searches linearly |
| 81 | * through the bitmaps) |
| 82 | */ |
| 83 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 84 | static u32 mthca_buddy_alloc(struct mthca_buddy *buddy, int order) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
| 86 | int o; |
| 87 | int m; |
| 88 | u32 seg; |
| 89 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 90 | spin_lock(&buddy->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Roland Dreier | e8bb4be | 2008-07-22 14:20:05 -0700 | [diff] [blame] | 92 | for (o = order; o <= buddy->max_order; ++o) |
| 93 | if (buddy->num_free[o]) { |
| 94 | m = 1 << (buddy->max_order - o); |
| 95 | seg = find_first_bit(buddy->bits[o], m); |
| 96 | if (seg < m) |
| 97 | goto found; |
| 98 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 100 | spin_unlock(&buddy->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | return -1; |
| 102 | |
| 103 | found: |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 104 | clear_bit(seg, buddy->bits[o]); |
Roland Dreier | e8bb4be | 2008-07-22 14:20:05 -0700 | [diff] [blame] | 105 | --buddy->num_free[o]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | while (o > order) { |
| 108 | --o; |
| 109 | seg <<= 1; |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 110 | set_bit(seg ^ 1, buddy->bits[o]); |
Roland Dreier | e8bb4be | 2008-07-22 14:20:05 -0700 | [diff] [blame] | 111 | ++buddy->num_free[o]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 114 | spin_unlock(&buddy->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | seg <<= order; |
| 117 | |
| 118 | return seg; |
| 119 | } |
| 120 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 121 | static void mthca_buddy_free(struct mthca_buddy *buddy, u32 seg, int order) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
| 123 | seg >>= order; |
| 124 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 125 | spin_lock(&buddy->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 127 | while (test_bit(seg ^ 1, buddy->bits[order])) { |
| 128 | clear_bit(seg ^ 1, buddy->bits[order]); |
Roland Dreier | e8bb4be | 2008-07-22 14:20:05 -0700 | [diff] [blame] | 129 | --buddy->num_free[order]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | seg >>= 1; |
| 131 | ++order; |
| 132 | } |
| 133 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 134 | set_bit(seg, buddy->bits[order]); |
Roland Dreier | e8bb4be | 2008-07-22 14:20:05 -0700 | [diff] [blame] | 135 | ++buddy->num_free[order]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 137 | spin_unlock(&buddy->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Roland Dreier | f4f3d0f | 2006-11-29 15:33:06 -0800 | [diff] [blame] | 140 | static int mthca_buddy_init(struct mthca_buddy *buddy, int max_order) |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 141 | { |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 142 | int i, s; |
| 143 | |
| 144 | buddy->max_order = max_order; |
| 145 | spin_lock_init(&buddy->lock); |
| 146 | |
Roland Dreier | de6eb66 | 2005-11-02 07:23:14 -0800 | [diff] [blame] | 147 | buddy->bits = kzalloc((buddy->max_order + 1) * sizeof (long *), |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 148 | GFP_KERNEL); |
Roland Dreier | e422131 | 2011-11-03 17:48:25 -0700 | [diff] [blame] | 149 | buddy->num_free = kcalloc((buddy->max_order + 1), sizeof *buddy->num_free, |
Roland Dreier | e8bb4be | 2008-07-22 14:20:05 -0700 | [diff] [blame] | 150 | GFP_KERNEL); |
| 151 | if (!buddy->bits || !buddy->num_free) |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 152 | goto err_out; |
| 153 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 154 | for (i = 0; i <= buddy->max_order; ++i) { |
| 155 | s = BITS_TO_LONGS(1 << (buddy->max_order - i)); |
| 156 | buddy->bits[i] = kmalloc(s * sizeof (long), GFP_KERNEL); |
| 157 | if (!buddy->bits[i]) |
| 158 | goto err_out_free; |
| 159 | bitmap_zero(buddy->bits[i], |
| 160 | 1 << (buddy->max_order - i)); |
| 161 | } |
| 162 | |
| 163 | set_bit(0, buddy->bits[buddy->max_order]); |
Roland Dreier | e8bb4be | 2008-07-22 14:20:05 -0700 | [diff] [blame] | 164 | buddy->num_free[buddy->max_order] = 1; |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 165 | |
| 166 | return 0; |
| 167 | |
| 168 | err_out_free: |
| 169 | for (i = 0; i <= buddy->max_order; ++i) |
| 170 | kfree(buddy->bits[i]); |
| 171 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 172 | err_out: |
Roland Dreier | e8bb4be | 2008-07-22 14:20:05 -0700 | [diff] [blame] | 173 | kfree(buddy->bits); |
| 174 | kfree(buddy->num_free); |
| 175 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 176 | return -ENOMEM; |
| 177 | } |
| 178 | |
Roland Dreier | e1f7868 | 2006-03-29 09:36:46 -0800 | [diff] [blame] | 179 | static void mthca_buddy_cleanup(struct mthca_buddy *buddy) |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 180 | { |
| 181 | int i; |
| 182 | |
| 183 | for (i = 0; i <= buddy->max_order; ++i) |
| 184 | kfree(buddy->bits[i]); |
| 185 | |
| 186 | kfree(buddy->bits); |
Roland Dreier | e8bb4be | 2008-07-22 14:20:05 -0700 | [diff] [blame] | 187 | kfree(buddy->num_free); |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 190 | static u32 mthca_alloc_mtt_range(struct mthca_dev *dev, int order, |
| 191 | struct mthca_buddy *buddy) |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 192 | { |
| 193 | u32 seg = mthca_buddy_alloc(buddy, order); |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 194 | |
| 195 | if (seg == -1) |
| 196 | return -1; |
| 197 | |
Roland Dreier | d10ddbf | 2005-04-16 15:26:32 -0700 | [diff] [blame] | 198 | if (mthca_is_memfree(dev)) |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 199 | if (mthca_table_get_range(dev, dev->mr_table.mtt_table, seg, |
| 200 | seg + (1 << order) - 1)) { |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 201 | mthca_buddy_free(buddy, seg, order); |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 202 | seg = -1; |
| 203 | } |
| 204 | |
| 205 | return seg; |
| 206 | } |
| 207 | |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 208 | static struct mthca_mtt *__mthca_alloc_mtt(struct mthca_dev *dev, int size, |
| 209 | struct mthca_buddy *buddy) |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 210 | { |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 211 | struct mthca_mtt *mtt; |
| 212 | int i; |
| 213 | |
| 214 | if (size <= 0) |
| 215 | return ERR_PTR(-EINVAL); |
| 216 | |
| 217 | mtt = kmalloc(sizeof *mtt, GFP_KERNEL); |
| 218 | if (!mtt) |
| 219 | return ERR_PTR(-ENOMEM); |
| 220 | |
| 221 | mtt->buddy = buddy; |
| 222 | mtt->order = 0; |
Eli Cohen | c1f67a8 | 2009-05-27 14:36:16 -0700 | [diff] [blame] | 223 | for (i = dev->limits.mtt_seg_size / 8; i < size; i <<= 1) |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 224 | ++mtt->order; |
| 225 | |
| 226 | mtt->first_seg = mthca_alloc_mtt_range(dev, mtt->order, buddy); |
| 227 | if (mtt->first_seg == -1) { |
| 228 | kfree(mtt); |
| 229 | return ERR_PTR(-ENOMEM); |
| 230 | } |
| 231 | |
| 232 | return mtt; |
| 233 | } |
| 234 | |
| 235 | struct mthca_mtt *mthca_alloc_mtt(struct mthca_dev *dev, int size) |
| 236 | { |
| 237 | return __mthca_alloc_mtt(dev, size, &dev->mr_table.mtt_buddy); |
| 238 | } |
| 239 | |
| 240 | void mthca_free_mtt(struct mthca_dev *dev, struct mthca_mtt *mtt) |
| 241 | { |
| 242 | if (!mtt) |
| 243 | return; |
| 244 | |
| 245 | mthca_buddy_free(mtt->buddy, mtt->first_seg, mtt->order); |
| 246 | |
| 247 | mthca_table_put_range(dev, dev->mr_table.mtt_table, |
| 248 | mtt->first_seg, |
| 249 | mtt->first_seg + (1 << mtt->order) - 1); |
| 250 | |
| 251 | kfree(mtt); |
| 252 | } |
| 253 | |
Michael S. Tsirkin | b2875d4 | 2007-02-10 23:14:25 +0200 | [diff] [blame] | 254 | static int __mthca_write_mtt(struct mthca_dev *dev, struct mthca_mtt *mtt, |
| 255 | int start_index, u64 *buffer_list, int list_len) |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 256 | { |
Roland Dreier | ed87845 | 2005-06-27 14:36:45 -0700 | [diff] [blame] | 257 | struct mthca_mailbox *mailbox; |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 258 | __be64 *mtt_entry; |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 259 | int err = 0; |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 260 | int i; |
| 261 | |
Roland Dreier | ed87845 | 2005-06-27 14:36:45 -0700 | [diff] [blame] | 262 | mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL); |
| 263 | if (IS_ERR(mailbox)) |
| 264 | return PTR_ERR(mailbox); |
| 265 | mtt_entry = mailbox->buf; |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 266 | |
| 267 | while (list_len > 0) { |
| 268 | mtt_entry[0] = cpu_to_be64(dev->mr_table.mtt_base + |
Eli Cohen | c1f67a8 | 2009-05-27 14:36:16 -0700 | [diff] [blame] | 269 | mtt->first_seg * dev->limits.mtt_seg_size + |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 270 | start_index * 8); |
| 271 | mtt_entry[1] = 0; |
Roland Dreier | ed87845 | 2005-06-27 14:36:45 -0700 | [diff] [blame] | 272 | for (i = 0; i < list_len && i < MTHCA_MAILBOX_SIZE / 8 - 2; ++i) |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 273 | mtt_entry[i + 2] = cpu_to_be64(buffer_list[i] | |
| 274 | MTHCA_MTT_FLAG_PRESENT); |
| 275 | |
| 276 | /* |
| 277 | * If we have an odd number of entries to write, add |
| 278 | * one more dummy entry for firmware efficiency. |
| 279 | */ |
| 280 | if (i & 1) |
| 281 | mtt_entry[i + 2] = 0; |
| 282 | |
Goldwyn Rodrigues | cdb73db | 2011-07-07 17:20:40 +0000 | [diff] [blame] | 283 | err = mthca_WRITE_MTT(dev, mailbox, (i + 1) & ~1); |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 284 | if (err) { |
| 285 | mthca_warn(dev, "WRITE_MTT failed (%d)\n", err); |
| 286 | goto out; |
| 287 | } |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 288 | |
| 289 | list_len -= i; |
| 290 | start_index += i; |
| 291 | buffer_list += i; |
| 292 | } |
| 293 | |
| 294 | out: |
Roland Dreier | ed87845 | 2005-06-27 14:36:45 -0700 | [diff] [blame] | 295 | mthca_free_mailbox(dev, mailbox); |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 296 | return err; |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Michael S. Tsirkin | b2875d4 | 2007-02-10 23:14:25 +0200 | [diff] [blame] | 299 | int mthca_write_mtt_size(struct mthca_dev *dev) |
| 300 | { |
Roland Dreier | 532c3b5 | 2007-04-24 16:31:04 -0700 | [diff] [blame] | 301 | if (dev->mr_table.fmr_mtt_buddy != &dev->mr_table.mtt_buddy || |
| 302 | !(dev->mthca_flags & MTHCA_FLAG_FMR)) |
Michael S. Tsirkin | b2875d4 | 2007-02-10 23:14:25 +0200 | [diff] [blame] | 303 | /* |
| 304 | * Be friendly to WRITE_MTT command |
| 305 | * and leave two empty slots for the |
| 306 | * index and reserved fields of the |
| 307 | * mailbox. |
| 308 | */ |
| 309 | return PAGE_SIZE / sizeof (u64) - 2; |
| 310 | |
| 311 | /* For Arbel, all MTTs must fit in the same page. */ |
| 312 | return mthca_is_memfree(dev) ? (PAGE_SIZE / sizeof (u64)) : 0x7ffffff; |
| 313 | } |
| 314 | |
Adrian Bunk | c9add6e | 2007-02-20 01:02:13 +0100 | [diff] [blame] | 315 | static void mthca_tavor_write_mtt_seg(struct mthca_dev *dev, |
| 316 | struct mthca_mtt *mtt, int start_index, |
| 317 | u64 *buffer_list, int list_len) |
Michael S. Tsirkin | b2875d4 | 2007-02-10 23:14:25 +0200 | [diff] [blame] | 318 | { |
| 319 | u64 __iomem *mtts; |
| 320 | int i; |
| 321 | |
Eli Cohen | c1f67a8 | 2009-05-27 14:36:16 -0700 | [diff] [blame] | 322 | mtts = dev->mr_table.tavor_fmr.mtt_base + mtt->first_seg * dev->limits.mtt_seg_size + |
Michael S. Tsirkin | b2875d4 | 2007-02-10 23:14:25 +0200 | [diff] [blame] | 323 | start_index * sizeof (u64); |
| 324 | for (i = 0; i < list_len; ++i) |
| 325 | mthca_write64_raw(cpu_to_be64(buffer_list[i] | MTHCA_MTT_FLAG_PRESENT), |
| 326 | mtts + i); |
| 327 | } |
| 328 | |
Adrian Bunk | c9add6e | 2007-02-20 01:02:13 +0100 | [diff] [blame] | 329 | static void mthca_arbel_write_mtt_seg(struct mthca_dev *dev, |
| 330 | struct mthca_mtt *mtt, int start_index, |
| 331 | u64 *buffer_list, int list_len) |
Michael S. Tsirkin | b2875d4 | 2007-02-10 23:14:25 +0200 | [diff] [blame] | 332 | { |
| 333 | __be64 *mtts; |
| 334 | dma_addr_t dma_handle; |
| 335 | int i; |
| 336 | int s = start_index * sizeof (u64); |
| 337 | |
| 338 | /* For Arbel, all MTTs must fit in the same page. */ |
| 339 | BUG_ON(s / PAGE_SIZE != (s + list_len * sizeof(u64) - 1) / PAGE_SIZE); |
| 340 | /* Require full segments */ |
Eli Cohen | c1f67a8 | 2009-05-27 14:36:16 -0700 | [diff] [blame] | 341 | BUG_ON(s % dev->limits.mtt_seg_size); |
Michael S. Tsirkin | b2875d4 | 2007-02-10 23:14:25 +0200 | [diff] [blame] | 342 | |
| 343 | mtts = mthca_table_find(dev->mr_table.mtt_table, mtt->first_seg + |
Eli Cohen | c1f67a8 | 2009-05-27 14:36:16 -0700 | [diff] [blame] | 344 | s / dev->limits.mtt_seg_size, &dma_handle); |
Michael S. Tsirkin | b2875d4 | 2007-02-10 23:14:25 +0200 | [diff] [blame] | 345 | |
| 346 | BUG_ON(!mtts); |
| 347 | |
Roland Dreier | 99987be | 2009-06-22 23:04:13 -0700 | [diff] [blame] | 348 | dma_sync_single_for_cpu(&dev->pdev->dev, dma_handle, |
| 349 | list_len * sizeof (u64), DMA_TO_DEVICE); |
| 350 | |
Michael S. Tsirkin | b2875d4 | 2007-02-10 23:14:25 +0200 | [diff] [blame] | 351 | for (i = 0; i < list_len; ++i) |
| 352 | mtts[i] = cpu_to_be64(buffer_list[i] | MTHCA_MTT_FLAG_PRESENT); |
| 353 | |
Roland Dreier | 99987be | 2009-06-22 23:04:13 -0700 | [diff] [blame] | 354 | dma_sync_single_for_device(&dev->pdev->dev, dma_handle, |
| 355 | list_len * sizeof (u64), DMA_TO_DEVICE); |
Michael S. Tsirkin | b2875d4 | 2007-02-10 23:14:25 +0200 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | int mthca_write_mtt(struct mthca_dev *dev, struct mthca_mtt *mtt, |
| 359 | int start_index, u64 *buffer_list, int list_len) |
| 360 | { |
| 361 | int size = mthca_write_mtt_size(dev); |
| 362 | int chunk; |
| 363 | |
Roland Dreier | 532c3b5 | 2007-04-24 16:31:04 -0700 | [diff] [blame] | 364 | if (dev->mr_table.fmr_mtt_buddy != &dev->mr_table.mtt_buddy || |
| 365 | !(dev->mthca_flags & MTHCA_FLAG_FMR)) |
Michael S. Tsirkin | b2875d4 | 2007-02-10 23:14:25 +0200 | [diff] [blame] | 366 | return __mthca_write_mtt(dev, mtt, start_index, buffer_list, list_len); |
| 367 | |
| 368 | while (list_len > 0) { |
| 369 | chunk = min(size, list_len); |
| 370 | if (mthca_is_memfree(dev)) |
| 371 | mthca_arbel_write_mtt_seg(dev, mtt, start_index, |
| 372 | buffer_list, chunk); |
| 373 | else |
| 374 | mthca_tavor_write_mtt_seg(dev, mtt, start_index, |
| 375 | buffer_list, chunk); |
| 376 | |
| 377 | list_len -= chunk; |
| 378 | start_index += chunk; |
| 379 | buffer_list += chunk; |
| 380 | } |
| 381 | |
| 382 | return 0; |
| 383 | } |
| 384 | |
Michael S. Tsirkin | d0a9d25 | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 385 | static inline u32 tavor_hw_index_to_key(u32 ind) |
| 386 | { |
| 387 | return ind; |
| 388 | } |
| 389 | |
| 390 | static inline u32 tavor_key_to_hw_index(u32 key) |
| 391 | { |
| 392 | return key; |
| 393 | } |
| 394 | |
| 395 | static inline u32 arbel_hw_index_to_key(u32 ind) |
| 396 | { |
| 397 | return (ind >> 24) | (ind << 8); |
| 398 | } |
| 399 | |
| 400 | static inline u32 arbel_key_to_hw_index(u32 key) |
| 401 | { |
| 402 | return (key << 24) | (key >> 8); |
| 403 | } |
| 404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | static inline u32 hw_index_to_key(struct mthca_dev *dev, u32 ind) |
| 406 | { |
Roland Dreier | d10ddbf | 2005-04-16 15:26:32 -0700 | [diff] [blame] | 407 | if (mthca_is_memfree(dev)) |
Michael S. Tsirkin | d0a9d25 | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 408 | return arbel_hw_index_to_key(ind); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | else |
Michael S. Tsirkin | d0a9d25 | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 410 | return tavor_hw_index_to_key(ind); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | static inline u32 key_to_hw_index(struct mthca_dev *dev, u32 key) |
| 414 | { |
Roland Dreier | d10ddbf | 2005-04-16 15:26:32 -0700 | [diff] [blame] | 415 | if (mthca_is_memfree(dev)) |
Michael S. Tsirkin | d0a9d25 | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 416 | return arbel_key_to_hw_index(key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | else |
Michael S. Tsirkin | d0a9d25 | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 418 | return tavor_key_to_hw_index(key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Eli Cohen | 651eaac | 2006-03-02 12:40:46 -0800 | [diff] [blame] | 421 | static inline u32 adjust_key(struct mthca_dev *dev, u32 key) |
| 422 | { |
| 423 | if (dev->mthca_flags & MTHCA_FLAG_SINAI_OPT) |
| 424 | return ((key << 20) & 0x800000) | (key & 0x7fffff); |
| 425 | else |
| 426 | return key; |
| 427 | } |
| 428 | |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 429 | int mthca_mr_alloc(struct mthca_dev *dev, u32 pd, int buffer_size_shift, |
| 430 | u64 iova, u64 total_size, u32 access, struct mthca_mr *mr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | { |
Roland Dreier | ed87845 | 2005-06-27 14:36:45 -0700 | [diff] [blame] | 432 | struct mthca_mailbox *mailbox; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | struct mthca_mpt_entry *mpt_entry; |
| 434 | u32 key; |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 435 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 438 | WARN_ON(buffer_size_shift >= 32); |
| 439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | key = mthca_alloc(&dev->mr_table.mpt_alloc); |
| 441 | if (key == -1) |
| 442 | return -ENOMEM; |
Eli Cohen | 651eaac | 2006-03-02 12:40:46 -0800 | [diff] [blame] | 443 | key = adjust_key(dev, key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | mr->ibmr.rkey = mr->ibmr.lkey = hw_index_to_key(dev, key); |
| 445 | |
Roland Dreier | d10ddbf | 2005-04-16 15:26:32 -0700 | [diff] [blame] | 446 | if (mthca_is_memfree(dev)) { |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 447 | err = mthca_table_get(dev, dev->mr_table.mpt_table, key); |
| 448 | if (err) |
| 449 | goto err_out_mpt_free; |
| 450 | } |
| 451 | |
Roland Dreier | ed87845 | 2005-06-27 14:36:45 -0700 | [diff] [blame] | 452 | mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL); |
| 453 | if (IS_ERR(mailbox)) { |
| 454 | err = PTR_ERR(mailbox); |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 455 | goto err_out_table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | } |
Roland Dreier | ed87845 | 2005-06-27 14:36:45 -0700 | [diff] [blame] | 457 | mpt_entry = mailbox->buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
| 459 | mpt_entry->flags = cpu_to_be32(MTHCA_MPT_FLAG_SW_OWNS | |
| 460 | MTHCA_MPT_FLAG_MIO | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | MTHCA_MPT_FLAG_REGION | |
| 462 | access); |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 463 | if (!mr->mtt) |
| 464 | mpt_entry->flags |= cpu_to_be32(MTHCA_MPT_FLAG_PHYSICAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
| 466 | mpt_entry->page_size = cpu_to_be32(buffer_size_shift - 12); |
| 467 | mpt_entry->key = cpu_to_be32(key); |
| 468 | mpt_entry->pd = cpu_to_be32(pd); |
| 469 | mpt_entry->start = cpu_to_be64(iova); |
| 470 | mpt_entry->length = cpu_to_be64(total_size); |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | memset(&mpt_entry->lkey, 0, |
| 473 | sizeof *mpt_entry - offsetof(struct mthca_mpt_entry, lkey)); |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 474 | |
| 475 | if (mr->mtt) |
| 476 | mpt_entry->mtt_seg = |
| 477 | cpu_to_be64(dev->mr_table.mtt_base + |
Eli Cohen | c1f67a8 | 2009-05-27 14:36:16 -0700 | [diff] [blame] | 478 | mr->mtt->first_seg * dev->limits.mtt_seg_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
| 480 | if (0) { |
| 481 | mthca_dbg(dev, "Dumping MPT entry %08x:\n", mr->ibmr.lkey); |
| 482 | for (i = 0; i < sizeof (struct mthca_mpt_entry) / 4; ++i) { |
| 483 | if (i % 4 == 0) |
| 484 | printk("[%02x] ", i * 4); |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 485 | printk(" %08x", be32_to_cpu(((__be32 *) mpt_entry)[i])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | if ((i + 1) % 4 == 0) |
| 487 | printk("\n"); |
| 488 | } |
| 489 | } |
| 490 | |
Roland Dreier | ed87845 | 2005-06-27 14:36:45 -0700 | [diff] [blame] | 491 | err = mthca_SW2HW_MPT(dev, mailbox, |
Goldwyn Rodrigues | cdb73db | 2011-07-07 17:20:40 +0000 | [diff] [blame] | 492 | key & (dev->limits.num_mpts - 1)); |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 493 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | mthca_warn(dev, "SW2HW_MPT failed (%d)\n", err); |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 495 | goto err_out_mailbox; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Roland Dreier | ed87845 | 2005-06-27 14:36:45 -0700 | [diff] [blame] | 498 | mthca_free_mailbox(dev, mailbox); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | return err; |
| 500 | |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 501 | err_out_mailbox: |
Roland Dreier | ed87845 | 2005-06-27 14:36:45 -0700 | [diff] [blame] | 502 | mthca_free_mailbox(dev, mailbox); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 504 | err_out_table: |
Roland Dreier | a03a5a6 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 505 | mthca_table_put(dev, dev->mr_table.mpt_table, key); |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 506 | |
| 507 | err_out_mpt_free: |
Michael S. Tsirkin | 55645e9 | 2005-04-16 15:26:19 -0700 | [diff] [blame] | 508 | mthca_free(&dev->mr_table.mpt_alloc, key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | return err; |
| 510 | } |
| 511 | |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 512 | int mthca_mr_alloc_notrans(struct mthca_dev *dev, u32 pd, |
| 513 | u32 access, struct mthca_mr *mr) |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 514 | { |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 515 | mr->mtt = NULL; |
| 516 | return mthca_mr_alloc(dev, pd, 12, 0, ~0ULL, access, mr); |
| 517 | } |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 518 | |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 519 | int mthca_mr_alloc_phys(struct mthca_dev *dev, u32 pd, |
| 520 | u64 *buffer_list, int buffer_size_shift, |
| 521 | int list_len, u64 iova, u64 total_size, |
| 522 | u32 access, struct mthca_mr *mr) |
| 523 | { |
| 524 | int err; |
| 525 | |
| 526 | mr->mtt = mthca_alloc_mtt(dev, list_len); |
| 527 | if (IS_ERR(mr->mtt)) |
| 528 | return PTR_ERR(mr->mtt); |
| 529 | |
| 530 | err = mthca_write_mtt(dev, mr->mtt, 0, buffer_list, list_len); |
| 531 | if (err) { |
| 532 | mthca_free_mtt(dev, mr->mtt); |
| 533 | return err; |
| 534 | } |
| 535 | |
| 536 | err = mthca_mr_alloc(dev, pd, buffer_size_shift, iova, |
| 537 | total_size, access, mr); |
| 538 | if (err) |
| 539 | mthca_free_mtt(dev, mr->mtt); |
| 540 | |
| 541 | return err; |
| 542 | } |
| 543 | |
| 544 | /* Free mr or fmr */ |
| 545 | static void mthca_free_region(struct mthca_dev *dev, u32 lkey) |
| 546 | { |
Roland Dreier | a03a5a6 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 547 | mthca_table_put(dev, dev->mr_table.mpt_table, |
Guy German | 7f9f2db | 2005-08-15 07:38:50 -0700 | [diff] [blame] | 548 | key_to_hw_index(dev, lkey)); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 549 | |
| 550 | mthca_free(&dev->mr_table.mpt_alloc, key_to_hw_index(dev, lkey)); |
| 551 | } |
| 552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | void mthca_free_mr(struct mthca_dev *dev, struct mthca_mr *mr) |
| 554 | { |
| 555 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | err = mthca_HW2SW_MPT(dev, NULL, |
| 558 | key_to_hw_index(dev, mr->ibmr.lkey) & |
Goldwyn Rodrigues | cdb73db | 2011-07-07 17:20:40 +0000 | [diff] [blame] | 559 | (dev->limits.num_mpts - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | if (err) |
| 561 | mthca_warn(dev, "HW2SW_MPT failed (%d)\n", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 563 | mthca_free_region(dev, mr->ibmr.lkey); |
| 564 | mthca_free_mtt(dev, mr->mtt); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 565 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 567 | int mthca_fmr_alloc(struct mthca_dev *dev, u32 pd, |
| 568 | u32 access, struct mthca_fmr *mr) |
| 569 | { |
| 570 | struct mthca_mpt_entry *mpt_entry; |
Roland Dreier | ed87845 | 2005-06-27 14:36:45 -0700 | [diff] [blame] | 571 | struct mthca_mailbox *mailbox; |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 572 | u64 mtt_seg; |
| 573 | u32 key, idx; |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 574 | int list_len = mr->attr.max_pages; |
| 575 | int err = -ENOMEM; |
| 576 | int i; |
| 577 | |
Or Gerlitz | d36f34a | 2006-02-02 10:43:45 -0800 | [diff] [blame] | 578 | if (mr->attr.page_shift < 12 || mr->attr.page_shift >= 32) |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 579 | return -EINVAL; |
| 580 | |
| 581 | /* For Arbel, all MTTs must fit in the same page. */ |
Roland Dreier | d10ddbf | 2005-04-16 15:26:32 -0700 | [diff] [blame] | 582 | if (mthca_is_memfree(dev) && |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 583 | mr->attr.max_pages * sizeof *mr->mem.arbel.mtts > PAGE_SIZE) |
| 584 | return -EINVAL; |
| 585 | |
| 586 | mr->maps = 0; |
| 587 | |
| 588 | key = mthca_alloc(&dev->mr_table.mpt_alloc); |
| 589 | if (key == -1) |
| 590 | return -ENOMEM; |
Eli Cohen | 651eaac | 2006-03-02 12:40:46 -0800 | [diff] [blame] | 591 | key = adjust_key(dev, key); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 592 | |
| 593 | idx = key & (dev->limits.num_mpts - 1); |
| 594 | mr->ibmr.rkey = mr->ibmr.lkey = hw_index_to_key(dev, key); |
| 595 | |
Roland Dreier | d10ddbf | 2005-04-16 15:26:32 -0700 | [diff] [blame] | 596 | if (mthca_is_memfree(dev)) { |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 597 | err = mthca_table_get(dev, dev->mr_table.mpt_table, key); |
| 598 | if (err) |
| 599 | goto err_out_mpt_free; |
| 600 | |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 601 | mr->mem.arbel.mpt = mthca_table_find(dev->mr_table.mpt_table, key, NULL); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 602 | BUG_ON(!mr->mem.arbel.mpt); |
| 603 | } else |
| 604 | mr->mem.tavor.mpt = dev->mr_table.tavor_fmr.mpt_base + |
Roland Dreier | 2fa5e2e | 2006-02-01 13:38:24 -0800 | [diff] [blame] | 605 | sizeof *(mr->mem.tavor.mpt) * idx; |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 606 | |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 607 | mr->mtt = __mthca_alloc_mtt(dev, list_len, dev->mr_table.fmr_mtt_buddy); |
Olaf Kirch | 2c78853 | 2008-02-04 20:20:44 -0800 | [diff] [blame] | 608 | if (IS_ERR(mr->mtt)) { |
| 609 | err = PTR_ERR(mr->mtt); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 610 | goto err_out_table; |
Olaf Kirch | 2c78853 | 2008-02-04 20:20:44 -0800 | [diff] [blame] | 611 | } |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 612 | |
Eli Cohen | c1f67a8 | 2009-05-27 14:36:16 -0700 | [diff] [blame] | 613 | mtt_seg = mr->mtt->first_seg * dev->limits.mtt_seg_size; |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 614 | |
Roland Dreier | d10ddbf | 2005-04-16 15:26:32 -0700 | [diff] [blame] | 615 | if (mthca_is_memfree(dev)) { |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 616 | mr->mem.arbel.mtts = mthca_table_find(dev->mr_table.mtt_table, |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 617 | mr->mtt->first_seg, |
| 618 | &mr->mem.arbel.dma_handle); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 619 | BUG_ON(!mr->mem.arbel.mtts); |
| 620 | } else |
| 621 | mr->mem.tavor.mtts = dev->mr_table.tavor_fmr.mtt_base + mtt_seg; |
| 622 | |
Roland Dreier | ed87845 | 2005-06-27 14:36:45 -0700 | [diff] [blame] | 623 | mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL); |
Olaf Kirch | 2c78853 | 2008-02-04 20:20:44 -0800 | [diff] [blame] | 624 | if (IS_ERR(mailbox)) { |
| 625 | err = PTR_ERR(mailbox); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 626 | goto err_out_free_mtt; |
Olaf Kirch | 2c78853 | 2008-02-04 20:20:44 -0800 | [diff] [blame] | 627 | } |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 628 | |
Roland Dreier | ed87845 | 2005-06-27 14:36:45 -0700 | [diff] [blame] | 629 | mpt_entry = mailbox->buf; |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 630 | |
| 631 | mpt_entry->flags = cpu_to_be32(MTHCA_MPT_FLAG_SW_OWNS | |
| 632 | MTHCA_MPT_FLAG_MIO | |
| 633 | MTHCA_MPT_FLAG_REGION | |
| 634 | access); |
| 635 | |
Or Gerlitz | d36f34a | 2006-02-02 10:43:45 -0800 | [diff] [blame] | 636 | mpt_entry->page_size = cpu_to_be32(mr->attr.page_shift - 12); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 637 | mpt_entry->key = cpu_to_be32(key); |
| 638 | mpt_entry->pd = cpu_to_be32(pd); |
| 639 | memset(&mpt_entry->start, 0, |
| 640 | sizeof *mpt_entry - offsetof(struct mthca_mpt_entry, start)); |
| 641 | mpt_entry->mtt_seg = cpu_to_be64(dev->mr_table.mtt_base + mtt_seg); |
| 642 | |
| 643 | if (0) { |
| 644 | mthca_dbg(dev, "Dumping MPT entry %08x:\n", mr->ibmr.lkey); |
| 645 | for (i = 0; i < sizeof (struct mthca_mpt_entry) / 4; ++i) { |
| 646 | if (i % 4 == 0) |
| 647 | printk("[%02x] ", i * 4); |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 648 | printk(" %08x", be32_to_cpu(((__be32 *) mpt_entry)[i])); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 649 | if ((i + 1) % 4 == 0) |
| 650 | printk("\n"); |
| 651 | } |
| 652 | } |
| 653 | |
Roland Dreier | ed87845 | 2005-06-27 14:36:45 -0700 | [diff] [blame] | 654 | err = mthca_SW2HW_MPT(dev, mailbox, |
Goldwyn Rodrigues | cdb73db | 2011-07-07 17:20:40 +0000 | [diff] [blame] | 655 | key & (dev->limits.num_mpts - 1)); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 656 | if (err) { |
| 657 | mthca_warn(dev, "SW2HW_MPT failed (%d)\n", err); |
| 658 | goto err_out_mailbox_free; |
| 659 | } |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 660 | |
Roland Dreier | ed87845 | 2005-06-27 14:36:45 -0700 | [diff] [blame] | 661 | mthca_free_mailbox(dev, mailbox); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 662 | return 0; |
| 663 | |
| 664 | err_out_mailbox_free: |
Roland Dreier | ed87845 | 2005-06-27 14:36:45 -0700 | [diff] [blame] | 665 | mthca_free_mailbox(dev, mailbox); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 666 | |
| 667 | err_out_free_mtt: |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 668 | mthca_free_mtt(dev, mr->mtt); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 669 | |
| 670 | err_out_table: |
Roland Dreier | a03a5a6 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 671 | mthca_table_put(dev, dev->mr_table.mpt_table, key); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 672 | |
| 673 | err_out_mpt_free: |
Roland Dreier | b7f9c11 | 2008-02-19 10:42:50 -0800 | [diff] [blame] | 674 | mthca_free(&dev->mr_table.mpt_alloc, key); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 675 | return err; |
| 676 | } |
| 677 | |
| 678 | int mthca_free_fmr(struct mthca_dev *dev, struct mthca_fmr *fmr) |
| 679 | { |
| 680 | if (fmr->maps) |
| 681 | return -EBUSY; |
| 682 | |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 683 | mthca_free_region(dev, fmr->ibmr.lkey); |
| 684 | mthca_free_mtt(dev, fmr->mtt); |
| 685 | |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | static inline int mthca_check_fmr(struct mthca_fmr *fmr, u64 *page_list, |
| 690 | int list_len, u64 iova) |
| 691 | { |
| 692 | int i, page_mask; |
| 693 | |
| 694 | if (list_len > fmr->attr.max_pages) |
| 695 | return -EINVAL; |
| 696 | |
Or Gerlitz | d36f34a | 2006-02-02 10:43:45 -0800 | [diff] [blame] | 697 | page_mask = (1 << fmr->attr.page_shift) - 1; |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 698 | |
| 699 | /* We are getting page lists, so va must be page aligned. */ |
| 700 | if (iova & page_mask) |
| 701 | return -EINVAL; |
| 702 | |
| 703 | /* Trust the user not to pass misaligned data in page_list */ |
| 704 | if (0) |
| 705 | for (i = 0; i < list_len; ++i) { |
| 706 | if (page_list[i] & ~page_mask) |
| 707 | return -EINVAL; |
| 708 | } |
| 709 | |
| 710 | if (fmr->maps >= fmr->attr.max_maps) |
| 711 | return -EINVAL; |
| 712 | |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | |
| 717 | int mthca_tavor_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list, |
| 718 | int list_len, u64 iova) |
| 719 | { |
| 720 | struct mthca_fmr *fmr = to_mfmr(ibfmr); |
| 721 | struct mthca_dev *dev = to_mdev(ibfmr->device); |
| 722 | struct mthca_mpt_entry mpt_entry; |
| 723 | u32 key; |
| 724 | int i, err; |
| 725 | |
| 726 | err = mthca_check_fmr(fmr, page_list, list_len, iova); |
| 727 | if (err) |
| 728 | return err; |
| 729 | |
| 730 | ++fmr->maps; |
| 731 | |
| 732 | key = tavor_key_to_hw_index(fmr->ibmr.lkey); |
| 733 | key += dev->limits.num_mpts; |
| 734 | fmr->ibmr.lkey = fmr->ibmr.rkey = tavor_hw_index_to_key(key); |
| 735 | |
| 736 | writeb(MTHCA_MPT_STATUS_SW, fmr->mem.tavor.mpt); |
| 737 | |
| 738 | for (i = 0; i < list_len; ++i) { |
| 739 | __be64 mtt_entry = cpu_to_be64(page_list[i] | |
| 740 | MTHCA_MTT_FLAG_PRESENT); |
| 741 | mthca_write64_raw(mtt_entry, fmr->mem.tavor.mtts + i); |
| 742 | } |
| 743 | |
| 744 | mpt_entry.lkey = cpu_to_be32(key); |
Or Gerlitz | d36f34a | 2006-02-02 10:43:45 -0800 | [diff] [blame] | 745 | mpt_entry.length = cpu_to_be64(list_len * (1ull << fmr->attr.page_shift)); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 746 | mpt_entry.start = cpu_to_be64(iova); |
| 747 | |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 748 | __raw_writel((__force u32) mpt_entry.lkey, &fmr->mem.tavor.mpt->key); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 749 | memcpy_toio(&fmr->mem.tavor.mpt->start, &mpt_entry.start, |
| 750 | offsetof(struct mthca_mpt_entry, window_count) - |
| 751 | offsetof(struct mthca_mpt_entry, start)); |
| 752 | |
| 753 | writeb(MTHCA_MPT_STATUS_HW, fmr->mem.tavor.mpt); |
| 754 | |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | int mthca_arbel_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list, |
| 759 | int list_len, u64 iova) |
| 760 | { |
| 761 | struct mthca_fmr *fmr = to_mfmr(ibfmr); |
| 762 | struct mthca_dev *dev = to_mdev(ibfmr->device); |
| 763 | u32 key; |
| 764 | int i, err; |
| 765 | |
| 766 | err = mthca_check_fmr(fmr, page_list, list_len, iova); |
| 767 | if (err) |
| 768 | return err; |
| 769 | |
| 770 | ++fmr->maps; |
| 771 | |
| 772 | key = arbel_key_to_hw_index(fmr->ibmr.lkey); |
Eli Cohen | 651eaac | 2006-03-02 12:40:46 -0800 | [diff] [blame] | 773 | if (dev->mthca_flags & MTHCA_FLAG_SINAI_OPT) |
| 774 | key += SINAI_FMR_KEY_INC; |
| 775 | else |
| 776 | key += dev->limits.num_mpts; |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 777 | fmr->ibmr.lkey = fmr->ibmr.rkey = arbel_hw_index_to_key(key); |
| 778 | |
| 779 | *(u8 *) fmr->mem.arbel.mpt = MTHCA_MPT_STATUS_SW; |
| 780 | |
| 781 | wmb(); |
| 782 | |
Roland Dreier | 99987be | 2009-06-22 23:04:13 -0700 | [diff] [blame] | 783 | dma_sync_single_for_cpu(&dev->pdev->dev, fmr->mem.arbel.dma_handle, |
| 784 | list_len * sizeof(u64), DMA_TO_DEVICE); |
| 785 | |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 786 | for (i = 0; i < list_len; ++i) |
| 787 | fmr->mem.arbel.mtts[i] = cpu_to_be64(page_list[i] | |
| 788 | MTHCA_MTT_FLAG_PRESENT); |
| 789 | |
Roland Dreier | 99987be | 2009-06-22 23:04:13 -0700 | [diff] [blame] | 790 | dma_sync_single_for_device(&dev->pdev->dev, fmr->mem.arbel.dma_handle, |
| 791 | list_len * sizeof(u64), DMA_TO_DEVICE); |
Michael S. Tsirkin | 391e4de | 2007-02-10 23:15:08 +0200 | [diff] [blame] | 792 | |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 793 | fmr->mem.arbel.mpt->key = cpu_to_be32(key); |
| 794 | fmr->mem.arbel.mpt->lkey = cpu_to_be32(key); |
Or Gerlitz | d36f34a | 2006-02-02 10:43:45 -0800 | [diff] [blame] | 795 | fmr->mem.arbel.mpt->length = cpu_to_be64(list_len * (1ull << fmr->attr.page_shift)); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 796 | fmr->mem.arbel.mpt->start = cpu_to_be64(iova); |
| 797 | |
| 798 | wmb(); |
| 799 | |
| 800 | *(u8 *) fmr->mem.arbel.mpt = MTHCA_MPT_STATUS_HW; |
| 801 | |
| 802 | wmb(); |
| 803 | |
| 804 | return 0; |
| 805 | } |
| 806 | |
| 807 | void mthca_tavor_fmr_unmap(struct mthca_dev *dev, struct mthca_fmr *fmr) |
| 808 | { |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 809 | if (!fmr->maps) |
| 810 | return; |
| 811 | |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 812 | fmr->maps = 0; |
| 813 | |
| 814 | writeb(MTHCA_MPT_STATUS_SW, fmr->mem.tavor.mpt); |
| 815 | } |
| 816 | |
| 817 | void mthca_arbel_fmr_unmap(struct mthca_dev *dev, struct mthca_fmr *fmr) |
| 818 | { |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 819 | if (!fmr->maps) |
| 820 | return; |
| 821 | |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 822 | fmr->maps = 0; |
| 823 | |
| 824 | *(u8 *) fmr->mem.arbel.mpt = MTHCA_MPT_STATUS_SW; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Roland Dreier | f4f3d0f | 2006-11-29 15:33:06 -0800 | [diff] [blame] | 827 | int mthca_init_mr_table(struct mthca_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | { |
John L. Burr | eb4a7cb | 2011-01-11 20:39:46 -0800 | [diff] [blame] | 829 | phys_addr_t addr; |
Michael S. Tsirkin | c20e20a | 2007-02-10 23:13:12 +0200 | [diff] [blame] | 830 | int mpts, mtts, err, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | |
| 832 | err = mthca_alloc_init(&dev->mr_table.mpt_alloc, |
| 833 | dev->limits.num_mpts, |
| 834 | ~0, dev->limits.reserved_mrws); |
| 835 | if (err) |
| 836 | return err; |
| 837 | |
Roland Dreier | d10ddbf | 2005-04-16 15:26:32 -0700 | [diff] [blame] | 838 | if (!mthca_is_memfree(dev) && |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 839 | (dev->mthca_flags & MTHCA_FLAG_DDR_HIDDEN)) |
| 840 | dev->limits.fmr_reserved_mtts = 0; |
| 841 | else |
| 842 | dev->mthca_flags |= MTHCA_FLAG_FMR; |
| 843 | |
Eli Cohen | 651eaac | 2006-03-02 12:40:46 -0800 | [diff] [blame] | 844 | if (dev->mthca_flags & MTHCA_FLAG_SINAI_OPT) |
| 845 | mthca_dbg(dev, "Memory key throughput optimization activated.\n"); |
| 846 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 847 | err = mthca_buddy_init(&dev->mr_table.mtt_buddy, |
| 848 | fls(dev->limits.num_mtt_segs - 1)); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 849 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 850 | if (err) |
| 851 | goto err_mtt_buddy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 853 | dev->mr_table.tavor_fmr.mpt_base = NULL; |
| 854 | dev->mr_table.tavor_fmr.mtt_base = NULL; |
| 855 | |
| 856 | if (dev->limits.fmr_reserved_mtts) { |
| 857 | i = fls(dev->limits.fmr_reserved_mtts - 1); |
| 858 | |
| 859 | if (i >= 31) { |
| 860 | mthca_warn(dev, "Unable to reserve 2^31 FMR MTTs.\n"); |
| 861 | err = -EINVAL; |
| 862 | goto err_fmr_mpt; |
| 863 | } |
Michael S. Tsirkin | c20e20a | 2007-02-10 23:13:12 +0200 | [diff] [blame] | 864 | mpts = mtts = 1 << i; |
| 865 | } else { |
Michael S. Tsirkin | 0264d88 | 2007-03-25 11:17:43 +0200 | [diff] [blame] | 866 | mtts = dev->limits.num_mtt_segs; |
| 867 | mpts = dev->limits.num_mpts; |
Michael S. Tsirkin | c20e20a | 2007-02-10 23:13:12 +0200 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | if (!mthca_is_memfree(dev) && |
| 871 | (dev->mthca_flags & MTHCA_FLAG_FMR)) { |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 872 | |
Michael S. Tsirkin | ce477ae | 2006-05-10 17:58:41 +0300 | [diff] [blame] | 873 | addr = pci_resource_start(dev->pdev, 4) + |
| 874 | ((pci_resource_len(dev->pdev, 4) - 1) & |
| 875 | dev->mr_table.mpt_base); |
| 876 | |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 877 | dev->mr_table.tavor_fmr.mpt_base = |
Michael S. Tsirkin | c20e20a | 2007-02-10 23:13:12 +0200 | [diff] [blame] | 878 | ioremap(addr, mpts * sizeof(struct mthca_mpt_entry)); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 879 | |
| 880 | if (!dev->mr_table.tavor_fmr.mpt_base) { |
| 881 | mthca_warn(dev, "MPT ioremap for FMR failed.\n"); |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 882 | err = -ENOMEM; |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 883 | goto err_fmr_mpt; |
| 884 | } |
| 885 | |
Michael S. Tsirkin | ce477ae | 2006-05-10 17:58:41 +0300 | [diff] [blame] | 886 | addr = pci_resource_start(dev->pdev, 4) + |
| 887 | ((pci_resource_len(dev->pdev, 4) - 1) & |
| 888 | dev->mr_table.mtt_base); |
| 889 | |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 890 | dev->mr_table.tavor_fmr.mtt_base = |
Eli Cohen | c1f67a8 | 2009-05-27 14:36:16 -0700 | [diff] [blame] | 891 | ioremap(addr, mtts * dev->limits.mtt_seg_size); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 892 | if (!dev->mr_table.tavor_fmr.mtt_base) { |
| 893 | mthca_warn(dev, "MTT ioremap for FMR failed.\n"); |
| 894 | err = -ENOMEM; |
| 895 | goto err_fmr_mtt; |
| 896 | } |
Michael S. Tsirkin | c20e20a | 2007-02-10 23:13:12 +0200 | [diff] [blame] | 897 | } |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 898 | |
Michael S. Tsirkin | c20e20a | 2007-02-10 23:13:12 +0200 | [diff] [blame] | 899 | if (dev->limits.fmr_reserved_mtts) { |
| 900 | err = mthca_buddy_init(&dev->mr_table.tavor_fmr.mtt_buddy, fls(mtts - 1)); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 901 | if (err) |
| 902 | goto err_fmr_mtt_buddy; |
| 903 | |
| 904 | /* Prevent regular MRs from using FMR keys */ |
Michael S. Tsirkin | c20e20a | 2007-02-10 23:13:12 +0200 | [diff] [blame] | 905 | err = mthca_buddy_alloc(&dev->mr_table.mtt_buddy, fls(mtts - 1)); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 906 | if (err) |
| 907 | goto err_reserve_fmr; |
| 908 | |
| 909 | dev->mr_table.fmr_mtt_buddy = |
Roland Dreier | 2fa5e2e | 2006-02-01 13:38:24 -0800 | [diff] [blame] | 910 | &dev->mr_table.tavor_fmr.mtt_buddy; |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 911 | } else |
| 912 | dev->mr_table.fmr_mtt_buddy = &dev->mr_table.mtt_buddy; |
| 913 | |
| 914 | /* FMR table is always the first, take reserved MTTs out of there */ |
| 915 | if (dev->limits.reserved_mtts) { |
| 916 | i = fls(dev->limits.reserved_mtts - 1); |
| 917 | |
Roland Dreier | d56d6f9 | 2005-06-27 14:36:43 -0700 | [diff] [blame] | 918 | if (mthca_alloc_mtt_range(dev, i, |
| 919 | dev->mr_table.fmr_mtt_buddy) == -1) { |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 920 | mthca_warn(dev, "MTT table of order %d is too small.\n", |
| 921 | dev->mr_table.fmr_mtt_buddy->max_order); |
| 922 | err = -ENOMEM; |
| 923 | goto err_reserve_mtts; |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 924 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | return 0; |
| 928 | |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 929 | err_reserve_mtts: |
| 930 | err_reserve_fmr: |
| 931 | if (dev->limits.fmr_reserved_mtts) |
| 932 | mthca_buddy_cleanup(&dev->mr_table.tavor_fmr.mtt_buddy); |
| 933 | |
| 934 | err_fmr_mtt_buddy: |
| 935 | if (dev->mr_table.tavor_fmr.mtt_base) |
| 936 | iounmap(dev->mr_table.tavor_fmr.mtt_base); |
| 937 | |
| 938 | err_fmr_mtt: |
| 939 | if (dev->mr_table.tavor_fmr.mpt_base) |
| 940 | iounmap(dev->mr_table.tavor_fmr.mpt_base); |
| 941 | |
| 942 | err_fmr_mpt: |
| 943 | mthca_buddy_cleanup(&dev->mr_table.mtt_buddy); |
| 944 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 945 | err_mtt_buddy: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | mthca_alloc_cleanup(&dev->mr_table.mpt_alloc); |
| 947 | |
| 948 | return err; |
| 949 | } |
| 950 | |
Roland Dreier | e1f7868 | 2006-03-29 09:36:46 -0800 | [diff] [blame] | 951 | void mthca_cleanup_mr_table(struct mthca_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | /* XXX check if any MRs are still allocated? */ |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 954 | if (dev->limits.fmr_reserved_mtts) |
| 955 | mthca_buddy_cleanup(&dev->mr_table.tavor_fmr.mtt_buddy); |
| 956 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 957 | mthca_buddy_cleanup(&dev->mr_table.mtt_buddy); |
Michael S. Tsirkin | e0f5fdc | 2005-04-16 15:26:30 -0700 | [diff] [blame] | 958 | |
| 959 | if (dev->mr_table.tavor_fmr.mtt_base) |
| 960 | iounmap(dev->mr_table.tavor_fmr.mtt_base); |
| 961 | if (dev->mr_table.tavor_fmr.mpt_base) |
| 962 | iounmap(dev->mr_table.tavor_fmr.mpt_base); |
| 963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | mthca_alloc_cleanup(&dev->mr_table.mpt_alloc); |
| 965 | } |