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