Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2004 Topspin Communications. All rights reserved. |
| 3 | * |
| 4 | * This software is available to you under a choice of one of two |
| 5 | * licenses. You may choose to be licensed under the terms of the GNU |
| 6 | * General Public License (GPL) Version 2, available from the file |
| 7 | * COPYING in the main directory of this source tree, or the |
| 8 | * OpenIB.org BSD license below: |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or |
| 11 | * without modification, are permitted provided that the following |
| 12 | * conditions are met: |
| 13 | * |
| 14 | * - Redistributions of source code must retain the above |
| 15 | * copyright notice, this list of conditions and the following |
| 16 | * disclaimer. |
| 17 | * |
| 18 | * - Redistributions in binary form must reproduce the above |
| 19 | * copyright notice, this list of conditions and the following |
| 20 | * disclaimer in the documentation and/or other materials |
| 21 | * provided with the distribution. |
| 22 | * |
| 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 25 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 27 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 28 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 29 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 30 | * SOFTWARE. |
| 31 | * |
| 32 | * $Id: mthca_mr.c 1349 2004-12-16 21:09:43Z roland $ |
| 33 | */ |
| 34 | |
| 35 | #include <linux/slab.h> |
| 36 | #include <linux/init.h> |
| 37 | #include <linux/errno.h> |
| 38 | |
| 39 | #include "mthca_dev.h" |
| 40 | #include "mthca_cmd.h" |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 41 | #include "mthca_memfree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * Must be packed because mtt_seg is 64 bits but only aligned to 32 bits. |
| 45 | */ |
| 46 | struct mthca_mpt_entry { |
| 47 | u32 flags; |
| 48 | u32 page_size; |
| 49 | u32 key; |
| 50 | u32 pd; |
| 51 | u64 start; |
| 52 | u64 length; |
| 53 | u32 lkey; |
| 54 | u32 window_count; |
| 55 | u32 window_count_limit; |
| 56 | u64 mtt_seg; |
| 57 | u32 mtt_sz; /* Arbel only */ |
| 58 | u32 reserved[2]; |
| 59 | } __attribute__((packed)); |
| 60 | |
| 61 | #define MTHCA_MPT_FLAG_SW_OWNS (0xfUL << 28) |
| 62 | #define MTHCA_MPT_FLAG_MIO (1 << 17) |
| 63 | #define MTHCA_MPT_FLAG_BIND_ENABLE (1 << 15) |
| 64 | #define MTHCA_MPT_FLAG_PHYSICAL (1 << 9) |
| 65 | #define MTHCA_MPT_FLAG_REGION (1 << 8) |
| 66 | |
| 67 | #define MTHCA_MTT_FLAG_PRESENT 1 |
| 68 | |
| 69 | /* |
| 70 | * Buddy allocator for MTT segments (currently not very efficient |
| 71 | * since it doesn't keep a free list and just searches linearly |
| 72 | * through the bitmaps) |
| 73 | */ |
| 74 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 75 | static u32 mthca_buddy_alloc(struct mthca_buddy *buddy, int order) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
| 77 | int o; |
| 78 | int m; |
| 79 | u32 seg; |
| 80 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 81 | spin_lock(&buddy->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 83 | for (o = order; o <= buddy->max_order; ++o) { |
| 84 | m = 1 << (buddy->max_order - o); |
| 85 | seg = find_first_bit(buddy->bits[o], m); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | if (seg < m) |
| 87 | goto found; |
| 88 | } |
| 89 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 90 | spin_unlock(&buddy->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | return -1; |
| 92 | |
| 93 | found: |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 94 | clear_bit(seg, buddy->bits[o]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | while (o > order) { |
| 97 | --o; |
| 98 | seg <<= 1; |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 99 | set_bit(seg ^ 1, buddy->bits[o]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 102 | spin_unlock(&buddy->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
| 104 | seg <<= order; |
| 105 | |
| 106 | return seg; |
| 107 | } |
| 108 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 109 | 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] | 110 | { |
| 111 | seg >>= order; |
| 112 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 113 | spin_lock(&buddy->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 115 | while (test_bit(seg ^ 1, buddy->bits[order])) { |
| 116 | clear_bit(seg ^ 1, buddy->bits[order]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | seg >>= 1; |
| 118 | ++order; |
| 119 | } |
| 120 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 121 | set_bit(seg, buddy->bits[order]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 123 | spin_unlock(&buddy->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 126 | static int __devinit mthca_buddy_init(struct mthca_buddy *buddy, int max_order) |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 127 | { |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 128 | int i, s; |
| 129 | |
| 130 | buddy->max_order = max_order; |
| 131 | spin_lock_init(&buddy->lock); |
| 132 | |
| 133 | buddy->bits = kmalloc((buddy->max_order + 1) * sizeof (long *), |
| 134 | GFP_KERNEL); |
| 135 | if (!buddy->bits) |
| 136 | goto err_out; |
| 137 | |
| 138 | memset(buddy->bits, 0, (buddy->max_order + 1) * sizeof (long *)); |
| 139 | |
| 140 | for (i = 0; i <= buddy->max_order; ++i) { |
| 141 | s = BITS_TO_LONGS(1 << (buddy->max_order - i)); |
| 142 | buddy->bits[i] = kmalloc(s * sizeof (long), GFP_KERNEL); |
| 143 | if (!buddy->bits[i]) |
| 144 | goto err_out_free; |
| 145 | bitmap_zero(buddy->bits[i], |
| 146 | 1 << (buddy->max_order - i)); |
| 147 | } |
| 148 | |
| 149 | set_bit(0, buddy->bits[buddy->max_order]); |
| 150 | |
| 151 | return 0; |
| 152 | |
| 153 | err_out_free: |
| 154 | for (i = 0; i <= buddy->max_order; ++i) |
| 155 | kfree(buddy->bits[i]); |
| 156 | |
| 157 | kfree(buddy->bits); |
| 158 | |
| 159 | err_out: |
| 160 | return -ENOMEM; |
| 161 | } |
| 162 | |
| 163 | static void __devexit mthca_buddy_cleanup(struct mthca_buddy *buddy) |
| 164 | { |
| 165 | int i; |
| 166 | |
| 167 | for (i = 0; i <= buddy->max_order; ++i) |
| 168 | kfree(buddy->bits[i]); |
| 169 | |
| 170 | kfree(buddy->bits); |
| 171 | } |
| 172 | |
| 173 | static u32 mthca_alloc_mtt(struct mthca_dev *dev, int order, |
| 174 | struct mthca_buddy *buddy) |
| 175 | { |
| 176 | u32 seg = mthca_buddy_alloc(buddy, order); |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 177 | |
| 178 | if (seg == -1) |
| 179 | return -1; |
| 180 | |
| 181 | if (dev->hca_type == ARBEL_NATIVE) |
| 182 | if (mthca_table_get_range(dev, dev->mr_table.mtt_table, seg, |
| 183 | seg + (1 << order) - 1)) { |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 184 | mthca_buddy_free(buddy, seg, order); |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 185 | seg = -1; |
| 186 | } |
| 187 | |
| 188 | return seg; |
| 189 | } |
| 190 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 191 | static void mthca_free_mtt(struct mthca_dev *dev, u32 seg, int order, |
| 192 | struct mthca_buddy* buddy) |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 193 | { |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 194 | mthca_buddy_free(buddy, seg, order); |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 195 | |
| 196 | if (dev->hca_type == ARBEL_NATIVE) |
| 197 | mthca_table_put_range(dev, dev->mr_table.mtt_table, seg, |
| 198 | seg + (1 << order) - 1); |
| 199 | } |
| 200 | |
Michael S. Tsirkin | d0a9d25 | 2005-04-16 15:26:30 -0700 | [diff] [blame^] | 201 | static inline u32 tavor_hw_index_to_key(u32 ind) |
| 202 | { |
| 203 | return ind; |
| 204 | } |
| 205 | |
| 206 | static inline u32 tavor_key_to_hw_index(u32 key) |
| 207 | { |
| 208 | return key; |
| 209 | } |
| 210 | |
| 211 | static inline u32 arbel_hw_index_to_key(u32 ind) |
| 212 | { |
| 213 | return (ind >> 24) | (ind << 8); |
| 214 | } |
| 215 | |
| 216 | static inline u32 arbel_key_to_hw_index(u32 key) |
| 217 | { |
| 218 | return (key << 24) | (key >> 8); |
| 219 | } |
| 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | static inline u32 hw_index_to_key(struct mthca_dev *dev, u32 ind) |
| 222 | { |
| 223 | if (dev->hca_type == ARBEL_NATIVE) |
Michael S. Tsirkin | d0a9d25 | 2005-04-16 15:26:30 -0700 | [diff] [blame^] | 224 | return arbel_hw_index_to_key(ind); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | else |
Michael S. Tsirkin | d0a9d25 | 2005-04-16 15:26:30 -0700 | [diff] [blame^] | 226 | return tavor_hw_index_to_key(ind); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static inline u32 key_to_hw_index(struct mthca_dev *dev, u32 key) |
| 230 | { |
| 231 | if (dev->hca_type == ARBEL_NATIVE) |
Michael S. Tsirkin | d0a9d25 | 2005-04-16 15:26:30 -0700 | [diff] [blame^] | 232 | return arbel_key_to_hw_index(key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | else |
Michael S. Tsirkin | d0a9d25 | 2005-04-16 15:26:30 -0700 | [diff] [blame^] | 234 | return tavor_key_to_hw_index(key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | int mthca_mr_alloc_notrans(struct mthca_dev *dev, u32 pd, |
| 238 | u32 access, struct mthca_mr *mr) |
| 239 | { |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 240 | void *mailbox = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | struct mthca_mpt_entry *mpt_entry; |
| 242 | u32 key; |
| 243 | int err; |
| 244 | u8 status; |
| 245 | |
| 246 | might_sleep(); |
| 247 | |
| 248 | mr->order = -1; |
| 249 | key = mthca_alloc(&dev->mr_table.mpt_alloc); |
| 250 | if (key == -1) |
| 251 | return -ENOMEM; |
| 252 | mr->ibmr.rkey = mr->ibmr.lkey = hw_index_to_key(dev, key); |
| 253 | |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 254 | if (dev->hca_type == ARBEL_NATIVE) { |
| 255 | err = mthca_table_get(dev, dev->mr_table.mpt_table, key); |
| 256 | if (err) |
| 257 | goto err_out_mpt_free; |
| 258 | } |
| 259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | mailbox = kmalloc(sizeof *mpt_entry + MTHCA_CMD_MAILBOX_EXTRA, |
| 261 | GFP_KERNEL); |
| 262 | if (!mailbox) { |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 263 | err = -ENOMEM; |
| 264 | goto err_out_table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |
| 266 | mpt_entry = MAILBOX_ALIGN(mailbox); |
| 267 | |
| 268 | mpt_entry->flags = cpu_to_be32(MTHCA_MPT_FLAG_SW_OWNS | |
| 269 | MTHCA_MPT_FLAG_MIO | |
| 270 | MTHCA_MPT_FLAG_PHYSICAL | |
| 271 | MTHCA_MPT_FLAG_REGION | |
| 272 | access); |
| 273 | mpt_entry->page_size = 0; |
| 274 | mpt_entry->key = cpu_to_be32(key); |
| 275 | mpt_entry->pd = cpu_to_be32(pd); |
| 276 | mpt_entry->start = 0; |
| 277 | mpt_entry->length = ~0ULL; |
| 278 | |
| 279 | memset(&mpt_entry->lkey, 0, |
| 280 | sizeof *mpt_entry - offsetof(struct mthca_mpt_entry, lkey)); |
| 281 | |
| 282 | err = mthca_SW2HW_MPT(dev, mpt_entry, |
| 283 | key & (dev->limits.num_mpts - 1), |
| 284 | &status); |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 285 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | mthca_warn(dev, "SW2HW_MPT failed (%d)\n", err); |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 287 | goto err_out_table; |
| 288 | } else if (status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | mthca_warn(dev, "SW2HW_MPT returned status 0x%02x\n", |
| 290 | status); |
| 291 | err = -EINVAL; |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 292 | goto err_out_table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | kfree(mailbox); |
| 296 | return err; |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 297 | |
| 298 | err_out_table: |
| 299 | if (dev->hca_type == ARBEL_NATIVE) |
| 300 | mthca_table_put(dev, dev->mr_table.mpt_table, key); |
| 301 | |
| 302 | err_out_mpt_free: |
Michael S. Tsirkin | 55645e9 | 2005-04-16 15:26:19 -0700 | [diff] [blame] | 303 | mthca_free(&dev->mr_table.mpt_alloc, key); |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 304 | kfree(mailbox); |
| 305 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | int mthca_mr_alloc_phys(struct mthca_dev *dev, u32 pd, |
| 309 | u64 *buffer_list, int buffer_size_shift, |
| 310 | int list_len, u64 iova, u64 total_size, |
| 311 | u32 access, struct mthca_mr *mr) |
| 312 | { |
| 313 | void *mailbox; |
| 314 | u64 *mtt_entry; |
| 315 | struct mthca_mpt_entry *mpt_entry; |
| 316 | u32 key; |
| 317 | int err = -ENOMEM; |
| 318 | u8 status; |
| 319 | int i; |
| 320 | |
| 321 | might_sleep(); |
| 322 | WARN_ON(buffer_size_shift >= 32); |
| 323 | |
| 324 | key = mthca_alloc(&dev->mr_table.mpt_alloc); |
| 325 | if (key == -1) |
| 326 | return -ENOMEM; |
| 327 | mr->ibmr.rkey = mr->ibmr.lkey = hw_index_to_key(dev, key); |
| 328 | |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 329 | if (dev->hca_type == ARBEL_NATIVE) { |
| 330 | err = mthca_table_get(dev, dev->mr_table.mpt_table, key); |
| 331 | if (err) |
| 332 | goto err_out_mpt_free; |
| 333 | } |
| 334 | |
Roland Dreier | 44ea668 | 2005-04-16 15:26:24 -0700 | [diff] [blame] | 335 | for (i = MTHCA_MTT_SEG_SIZE / 8, mr->order = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | i < list_len; |
| 337 | i <<= 1, ++mr->order) |
| 338 | ; /* nothing */ |
| 339 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 340 | mr->first_seg = mthca_alloc_mtt(dev, mr->order, |
| 341 | &dev->mr_table.mtt_buddy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | if (mr->first_seg == -1) |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 343 | goto err_out_table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
| 345 | /* |
| 346 | * If list_len is odd, we add one more dummy entry for |
| 347 | * firmware efficiency. |
| 348 | */ |
| 349 | mailbox = kmalloc(max(sizeof *mpt_entry, |
| 350 | (size_t) 8 * (list_len + (list_len & 1) + 2)) + |
| 351 | MTHCA_CMD_MAILBOX_EXTRA, |
| 352 | GFP_KERNEL); |
| 353 | if (!mailbox) |
| 354 | goto err_out_free_mtt; |
| 355 | |
| 356 | mtt_entry = MAILBOX_ALIGN(mailbox); |
| 357 | |
| 358 | mtt_entry[0] = cpu_to_be64(dev->mr_table.mtt_base + |
Roland Dreier | 44ea668 | 2005-04-16 15:26:24 -0700 | [diff] [blame] | 359 | mr->first_seg * MTHCA_MTT_SEG_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | mtt_entry[1] = 0; |
| 361 | for (i = 0; i < list_len; ++i) |
| 362 | mtt_entry[i + 2] = cpu_to_be64(buffer_list[i] | |
| 363 | MTHCA_MTT_FLAG_PRESENT); |
| 364 | if (list_len & 1) { |
| 365 | mtt_entry[i + 2] = 0; |
| 366 | ++list_len; |
| 367 | } |
| 368 | |
| 369 | if (0) { |
| 370 | mthca_dbg(dev, "Dumping MPT entry\n"); |
| 371 | for (i = 0; i < list_len + 2; ++i) |
| 372 | printk(KERN_ERR "[%2d] %016llx\n", |
| 373 | i, (unsigned long long) be64_to_cpu(mtt_entry[i])); |
| 374 | } |
| 375 | |
| 376 | err = mthca_WRITE_MTT(dev, mtt_entry, list_len, &status); |
| 377 | if (err) { |
| 378 | mthca_warn(dev, "WRITE_MTT failed (%d)\n", err); |
| 379 | goto err_out_mailbox_free; |
| 380 | } |
| 381 | if (status) { |
| 382 | mthca_warn(dev, "WRITE_MTT returned status 0x%02x\n", |
| 383 | status); |
| 384 | err = -EINVAL; |
| 385 | goto err_out_mailbox_free; |
| 386 | } |
| 387 | |
| 388 | mpt_entry = MAILBOX_ALIGN(mailbox); |
| 389 | |
| 390 | mpt_entry->flags = cpu_to_be32(MTHCA_MPT_FLAG_SW_OWNS | |
| 391 | MTHCA_MPT_FLAG_MIO | |
| 392 | MTHCA_MPT_FLAG_REGION | |
| 393 | access); |
| 394 | |
| 395 | mpt_entry->page_size = cpu_to_be32(buffer_size_shift - 12); |
| 396 | mpt_entry->key = cpu_to_be32(key); |
| 397 | mpt_entry->pd = cpu_to_be32(pd); |
| 398 | mpt_entry->start = cpu_to_be64(iova); |
| 399 | mpt_entry->length = cpu_to_be64(total_size); |
| 400 | memset(&mpt_entry->lkey, 0, |
| 401 | sizeof *mpt_entry - offsetof(struct mthca_mpt_entry, lkey)); |
| 402 | mpt_entry->mtt_seg = cpu_to_be64(dev->mr_table.mtt_base + |
Roland Dreier | 44ea668 | 2005-04-16 15:26:24 -0700 | [diff] [blame] | 403 | mr->first_seg * MTHCA_MTT_SEG_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
| 405 | if (0) { |
| 406 | mthca_dbg(dev, "Dumping MPT entry %08x:\n", mr->ibmr.lkey); |
| 407 | for (i = 0; i < sizeof (struct mthca_mpt_entry) / 4; ++i) { |
| 408 | if (i % 4 == 0) |
| 409 | printk("[%02x] ", i * 4); |
| 410 | printk(" %08x", be32_to_cpu(((u32 *) mpt_entry)[i])); |
| 411 | if ((i + 1) % 4 == 0) |
| 412 | printk("\n"); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | err = mthca_SW2HW_MPT(dev, mpt_entry, |
| 417 | key & (dev->limits.num_mpts - 1), |
| 418 | &status); |
| 419 | if (err) |
| 420 | mthca_warn(dev, "SW2HW_MPT failed (%d)\n", err); |
| 421 | else if (status) { |
| 422 | mthca_warn(dev, "SW2HW_MPT returned status 0x%02x\n", |
| 423 | status); |
| 424 | err = -EINVAL; |
| 425 | } |
| 426 | |
| 427 | kfree(mailbox); |
| 428 | return err; |
| 429 | |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 430 | err_out_mailbox_free: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | kfree(mailbox); |
| 432 | |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 433 | err_out_free_mtt: |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 434 | mthca_free_mtt(dev, mr->first_seg, mr->order, &dev->mr_table.mtt_buddy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 436 | err_out_table: |
| 437 | if (dev->hca_type == ARBEL_NATIVE) |
| 438 | mthca_table_put(dev, dev->mr_table.mpt_table, key); |
| 439 | |
| 440 | err_out_mpt_free: |
Michael S. Tsirkin | 55645e9 | 2005-04-16 15:26:19 -0700 | [diff] [blame] | 441 | mthca_free(&dev->mr_table.mpt_alloc, key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | return err; |
| 443 | } |
| 444 | |
| 445 | void mthca_free_mr(struct mthca_dev *dev, struct mthca_mr *mr) |
| 446 | { |
| 447 | int err; |
| 448 | u8 status; |
| 449 | |
| 450 | might_sleep(); |
| 451 | |
| 452 | err = mthca_HW2SW_MPT(dev, NULL, |
| 453 | key_to_hw_index(dev, mr->ibmr.lkey) & |
| 454 | (dev->limits.num_mpts - 1), |
| 455 | &status); |
| 456 | if (err) |
| 457 | mthca_warn(dev, "HW2SW_MPT failed (%d)\n", err); |
| 458 | else if (status) |
| 459 | mthca_warn(dev, "HW2SW_MPT returned status 0x%02x\n", |
| 460 | status); |
| 461 | |
| 462 | if (mr->order >= 0) |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 463 | mthca_free_mtt(dev, mr->first_seg, mr->order, &dev->mr_table.mtt_buddy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
Roland Dreier | 86562a1 | 2005-04-16 15:26:13 -0700 | [diff] [blame] | 465 | if (dev->hca_type == ARBEL_NATIVE) |
| 466 | mthca_table_put(dev, dev->mr_table.mpt_table, |
| 467 | key_to_hw_index(dev, mr->ibmr.lkey)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | mthca_free(&dev->mr_table.mpt_alloc, key_to_hw_index(dev, mr->ibmr.lkey)); |
| 469 | } |
| 470 | |
| 471 | int __devinit mthca_init_mr_table(struct mthca_dev *dev) |
| 472 | { |
| 473 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
| 475 | err = mthca_alloc_init(&dev->mr_table.mpt_alloc, |
| 476 | dev->limits.num_mpts, |
| 477 | ~0, dev->limits.reserved_mrws); |
| 478 | if (err) |
| 479 | return err; |
| 480 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 481 | err = mthca_buddy_init(&dev->mr_table.mtt_buddy, |
| 482 | fls(dev->limits.num_mtt_segs - 1)); |
| 483 | if (err) |
| 484 | goto err_mtt_buddy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 486 | if (dev->limits.reserved_mtts) { |
| 487 | if (mthca_alloc_mtt(dev, fls(dev->limits.reserved_mtts - 1), |
| 488 | &dev->mr_table.mtt_buddy) == -1) { |
| 489 | mthca_warn(dev, "MTT table of order %d is too small.\n", |
| 490 | dev->mr_table.mtt_buddy.max_order); |
| 491 | err = -ENOMEM; |
| 492 | goto err_mtt_buddy; |
| 493 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | return 0; |
| 497 | |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 498 | err_mtt_buddy: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | mthca_alloc_cleanup(&dev->mr_table.mpt_alloc); |
| 500 | |
| 501 | return err; |
| 502 | } |
| 503 | |
| 504 | void __devexit mthca_cleanup_mr_table(struct mthca_dev *dev) |
| 505 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | /* XXX check if any MRs are still allocated? */ |
Michael S. Tsirkin | 9095e20 | 2005-04-16 15:26:26 -0700 | [diff] [blame] | 507 | mthca_buddy_cleanup(&dev->mr_table.mtt_buddy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | mthca_alloc_cleanup(&dev->mr_table.mpt_alloc); |
| 509 | } |