Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2005 Ammasso, Inc. All rights reserved. |
| 3 | * Copyright (c) 2005 Open Grid Computing, Inc. All rights reserved. |
| 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 | */ |
| 34 | |
| 35 | #include <linux/module.h> |
| 36 | #include <linux/moduleparam.h> |
| 37 | #include <linux/pci.h> |
| 38 | #include <linux/netdevice.h> |
| 39 | #include <linux/etherdevice.h> |
| 40 | #include <linux/inetdevice.h> |
| 41 | #include <linux/delay.h> |
| 42 | #include <linux/ethtool.h> |
| 43 | #include <linux/mii.h> |
| 44 | #include <linux/if_vlan.h> |
| 45 | #include <linux/crc32.h> |
| 46 | #include <linux/in.h> |
| 47 | #include <linux/ip.h> |
| 48 | #include <linux/tcp.h> |
| 49 | #include <linux/init.h> |
| 50 | #include <linux/dma-mapping.h> |
| 51 | #include <linux/if_arp.h> |
Al Viro | d7b2004 | 2006-09-23 16:44:16 +0100 | [diff] [blame] | 52 | #include <linux/vmalloc.h> |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 53 | |
| 54 | #include <asm/io.h> |
| 55 | #include <asm/irq.h> |
| 56 | #include <asm/byteorder.h> |
| 57 | |
| 58 | #include <rdma/ib_smi.h> |
| 59 | #include <rdma/ib_user_verbs.h> |
| 60 | #include "c2.h" |
| 61 | #include "c2_provider.h" |
| 62 | #include "c2_user.h" |
| 63 | |
| 64 | static int c2_query_device(struct ib_device *ibdev, |
| 65 | struct ib_device_attr *props) |
| 66 | { |
| 67 | struct c2_dev *c2dev = to_c2dev(ibdev); |
| 68 | |
| 69 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 70 | |
| 71 | *props = c2dev->props; |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int c2_query_port(struct ib_device *ibdev, |
| 76 | u8 port, struct ib_port_attr *props) |
| 77 | { |
| 78 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 79 | |
| 80 | props->max_mtu = IB_MTU_4096; |
| 81 | props->lid = 0; |
| 82 | props->lmc = 0; |
| 83 | props->sm_lid = 0; |
| 84 | props->sm_sl = 0; |
| 85 | props->state = IB_PORT_ACTIVE; |
| 86 | props->phys_state = 0; |
| 87 | props->port_cap_flags = |
| 88 | IB_PORT_CM_SUP | |
| 89 | IB_PORT_REINIT_SUP | |
| 90 | IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP; |
| 91 | props->gid_tbl_len = 1; |
| 92 | props->pkey_tbl_len = 1; |
| 93 | props->qkey_viol_cntr = 0; |
| 94 | props->active_width = 1; |
| 95 | props->active_speed = 1; |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static int c2_modify_port(struct ib_device *ibdev, |
| 101 | u8 port, int port_modify_mask, |
| 102 | struct ib_port_modify *props) |
| 103 | { |
| 104 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static int c2_query_pkey(struct ib_device *ibdev, |
| 109 | u8 port, u16 index, u16 * pkey) |
| 110 | { |
| 111 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 112 | *pkey = 0; |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static int c2_query_gid(struct ib_device *ibdev, u8 port, |
| 117 | int index, union ib_gid *gid) |
| 118 | { |
| 119 | struct c2_dev *c2dev = to_c2dev(ibdev); |
| 120 | |
| 121 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 122 | memset(&(gid->raw[0]), 0, sizeof(gid->raw)); |
| 123 | memcpy(&(gid->raw[0]), c2dev->pseudo_netdev->dev_addr, 6); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | /* Allocate the user context data structure. This keeps track |
| 129 | * of all objects associated with a particular user-mode client. |
| 130 | */ |
| 131 | static struct ib_ucontext *c2_alloc_ucontext(struct ib_device *ibdev, |
| 132 | struct ib_udata *udata) |
| 133 | { |
| 134 | struct c2_ucontext *context; |
| 135 | |
| 136 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 137 | context = kmalloc(sizeof(*context), GFP_KERNEL); |
| 138 | if (!context) |
| 139 | return ERR_PTR(-ENOMEM); |
| 140 | |
| 141 | return &context->ibucontext; |
| 142 | } |
| 143 | |
| 144 | static int c2_dealloc_ucontext(struct ib_ucontext *context) |
| 145 | { |
| 146 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 147 | kfree(context); |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static int c2_mmap_uar(struct ib_ucontext *context, struct vm_area_struct *vma) |
| 152 | { |
| 153 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 154 | return -ENOSYS; |
| 155 | } |
| 156 | |
| 157 | static struct ib_pd *c2_alloc_pd(struct ib_device *ibdev, |
| 158 | struct ib_ucontext *context, |
| 159 | struct ib_udata *udata) |
| 160 | { |
| 161 | struct c2_pd *pd; |
| 162 | int err; |
| 163 | |
| 164 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 165 | |
| 166 | pd = kmalloc(sizeof(*pd), GFP_KERNEL); |
| 167 | if (!pd) |
| 168 | return ERR_PTR(-ENOMEM); |
| 169 | |
| 170 | err = c2_pd_alloc(to_c2dev(ibdev), !context, pd); |
| 171 | if (err) { |
| 172 | kfree(pd); |
| 173 | return ERR_PTR(err); |
| 174 | } |
| 175 | |
| 176 | if (context) { |
| 177 | if (ib_copy_to_udata(udata, &pd->pd_id, sizeof(__u32))) { |
| 178 | c2_pd_free(to_c2dev(ibdev), pd); |
| 179 | kfree(pd); |
| 180 | return ERR_PTR(-EFAULT); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return &pd->ibpd; |
| 185 | } |
| 186 | |
| 187 | static int c2_dealloc_pd(struct ib_pd *pd) |
| 188 | { |
| 189 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 190 | c2_pd_free(to_c2dev(pd->device), to_c2pd(pd)); |
| 191 | kfree(pd); |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static struct ib_ah *c2_ah_create(struct ib_pd *pd, struct ib_ah_attr *ah_attr) |
| 197 | { |
| 198 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 199 | return ERR_PTR(-ENOSYS); |
| 200 | } |
| 201 | |
| 202 | static int c2_ah_destroy(struct ib_ah *ah) |
| 203 | { |
| 204 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 205 | return -ENOSYS; |
| 206 | } |
| 207 | |
| 208 | static void c2_add_ref(struct ib_qp *ibqp) |
| 209 | { |
| 210 | struct c2_qp *qp; |
| 211 | BUG_ON(!ibqp); |
| 212 | qp = to_c2qp(ibqp); |
| 213 | atomic_inc(&qp->refcount); |
| 214 | } |
| 215 | |
| 216 | static void c2_rem_ref(struct ib_qp *ibqp) |
| 217 | { |
| 218 | struct c2_qp *qp; |
| 219 | BUG_ON(!ibqp); |
| 220 | qp = to_c2qp(ibqp); |
| 221 | if (atomic_dec_and_test(&qp->refcount)) |
| 222 | wake_up(&qp->wait); |
| 223 | } |
| 224 | |
| 225 | struct ib_qp *c2_get_qp(struct ib_device *device, int qpn) |
| 226 | { |
| 227 | struct c2_dev* c2dev = to_c2dev(device); |
| 228 | struct c2_qp *qp; |
| 229 | |
| 230 | qp = c2_find_qpn(c2dev, qpn); |
| 231 | pr_debug("%s Returning QP=%p for QPN=%d, device=%p, refcount=%d\n", |
| 232 | __FUNCTION__, qp, qpn, device, |
| 233 | (qp?atomic_read(&qp->refcount):0)); |
| 234 | |
| 235 | return (qp?&qp->ibqp:NULL); |
| 236 | } |
| 237 | |
| 238 | static struct ib_qp *c2_create_qp(struct ib_pd *pd, |
| 239 | struct ib_qp_init_attr *init_attr, |
| 240 | struct ib_udata *udata) |
| 241 | { |
| 242 | struct c2_qp *qp; |
| 243 | int err; |
| 244 | |
| 245 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 246 | |
| 247 | switch (init_attr->qp_type) { |
| 248 | case IB_QPT_RC: |
| 249 | qp = kzalloc(sizeof(*qp), GFP_KERNEL); |
| 250 | if (!qp) { |
| 251 | pr_debug("%s: Unable to allocate QP\n", __FUNCTION__); |
| 252 | return ERR_PTR(-ENOMEM); |
| 253 | } |
| 254 | spin_lock_init(&qp->lock); |
| 255 | if (pd->uobject) { |
| 256 | /* userspace specific */ |
| 257 | } |
| 258 | |
| 259 | err = c2_alloc_qp(to_c2dev(pd->device), |
| 260 | to_c2pd(pd), init_attr, qp); |
| 261 | |
| 262 | if (err && pd->uobject) { |
| 263 | /* userspace specific */ |
| 264 | } |
| 265 | |
| 266 | break; |
| 267 | default: |
| 268 | pr_debug("%s: Invalid QP type: %d\n", __FUNCTION__, |
| 269 | init_attr->qp_type); |
| 270 | return ERR_PTR(-EINVAL); |
| 271 | break; |
| 272 | } |
| 273 | |
| 274 | if (err) { |
| 275 | kfree(qp); |
| 276 | return ERR_PTR(err); |
| 277 | } |
| 278 | |
| 279 | return &qp->ibqp; |
| 280 | } |
| 281 | |
| 282 | static int c2_destroy_qp(struct ib_qp *ib_qp) |
| 283 | { |
| 284 | struct c2_qp *qp = to_c2qp(ib_qp); |
| 285 | |
| 286 | pr_debug("%s:%u qp=%p,qp->state=%d\n", |
| 287 | __FUNCTION__, __LINE__,ib_qp,qp->state); |
| 288 | c2_free_qp(to_c2dev(ib_qp->device), qp); |
| 289 | kfree(qp); |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | static struct ib_cq *c2_create_cq(struct ib_device *ibdev, int entries, |
| 294 | struct ib_ucontext *context, |
| 295 | struct ib_udata *udata) |
| 296 | { |
| 297 | struct c2_cq *cq; |
| 298 | int err; |
| 299 | |
| 300 | cq = kmalloc(sizeof(*cq), GFP_KERNEL); |
| 301 | if (!cq) { |
| 302 | pr_debug("%s: Unable to allocate CQ\n", __FUNCTION__); |
| 303 | return ERR_PTR(-ENOMEM); |
| 304 | } |
| 305 | |
| 306 | err = c2_init_cq(to_c2dev(ibdev), entries, NULL, cq); |
| 307 | if (err) { |
| 308 | pr_debug("%s: error initializing CQ\n", __FUNCTION__); |
| 309 | kfree(cq); |
| 310 | return ERR_PTR(err); |
| 311 | } |
| 312 | |
| 313 | return &cq->ibcq; |
| 314 | } |
| 315 | |
| 316 | static int c2_destroy_cq(struct ib_cq *ib_cq) |
| 317 | { |
| 318 | struct c2_cq *cq = to_c2cq(ib_cq); |
| 319 | |
| 320 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 321 | |
| 322 | c2_free_cq(to_c2dev(ib_cq->device), cq); |
| 323 | kfree(cq); |
| 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | static inline u32 c2_convert_access(int acc) |
| 329 | { |
| 330 | return (acc & IB_ACCESS_REMOTE_WRITE ? C2_ACF_REMOTE_WRITE : 0) | |
| 331 | (acc & IB_ACCESS_REMOTE_READ ? C2_ACF_REMOTE_READ : 0) | |
| 332 | (acc & IB_ACCESS_LOCAL_WRITE ? C2_ACF_LOCAL_WRITE : 0) | |
| 333 | C2_ACF_LOCAL_READ | C2_ACF_WINDOW_BIND; |
| 334 | } |
| 335 | |
| 336 | static struct ib_mr *c2_reg_phys_mr(struct ib_pd *ib_pd, |
| 337 | struct ib_phys_buf *buffer_list, |
| 338 | int num_phys_buf, int acc, u64 * iova_start) |
| 339 | { |
| 340 | struct c2_mr *mr; |
| 341 | u64 *page_list; |
| 342 | u32 total_len; |
| 343 | int err, i, j, k, page_shift, pbl_depth; |
| 344 | |
| 345 | pbl_depth = 0; |
| 346 | total_len = 0; |
| 347 | |
| 348 | page_shift = PAGE_SHIFT; |
| 349 | /* |
| 350 | * If there is only 1 buffer we assume this could |
| 351 | * be a map of all phy mem...use a 32k page_shift. |
| 352 | */ |
| 353 | if (num_phys_buf == 1) |
| 354 | page_shift += 3; |
| 355 | |
| 356 | for (i = 0; i < num_phys_buf; i++) { |
| 357 | |
| 358 | if (buffer_list[i].addr & ~PAGE_MASK) { |
| 359 | pr_debug("Unaligned Memory Buffer: 0x%x\n", |
| 360 | (unsigned int) buffer_list[i].addr); |
| 361 | return ERR_PTR(-EINVAL); |
| 362 | } |
| 363 | |
| 364 | if (!buffer_list[i].size) { |
| 365 | pr_debug("Invalid Buffer Size\n"); |
| 366 | return ERR_PTR(-EINVAL); |
| 367 | } |
| 368 | |
| 369 | total_len += buffer_list[i].size; |
| 370 | pbl_depth += ALIGN(buffer_list[i].size, |
| 371 | (1 << page_shift)) >> page_shift; |
| 372 | } |
| 373 | |
| 374 | page_list = vmalloc(sizeof(u64) * pbl_depth); |
| 375 | if (!page_list) { |
| 376 | pr_debug("couldn't vmalloc page_list of size %zd\n", |
| 377 | (sizeof(u64) * pbl_depth)); |
| 378 | return ERR_PTR(-ENOMEM); |
| 379 | } |
| 380 | |
| 381 | for (i = 0, j = 0; i < num_phys_buf; i++) { |
| 382 | |
| 383 | int naddrs; |
| 384 | |
| 385 | naddrs = ALIGN(buffer_list[i].size, |
| 386 | (1 << page_shift)) >> page_shift; |
| 387 | for (k = 0; k < naddrs; k++) |
| 388 | page_list[j++] = (buffer_list[i].addr + |
| 389 | (k << page_shift)); |
| 390 | } |
| 391 | |
| 392 | mr = kmalloc(sizeof(*mr), GFP_KERNEL); |
| 393 | if (!mr) |
| 394 | return ERR_PTR(-ENOMEM); |
| 395 | |
| 396 | mr->pd = to_c2pd(ib_pd); |
| 397 | pr_debug("%s - page shift %d, pbl_depth %d, total_len %u, " |
| 398 | "*iova_start %llx, first pa %llx, last pa %llx\n", |
| 399 | __FUNCTION__, page_shift, pbl_depth, total_len, |
Roland Dreier | 6edf602 | 2006-09-27 14:42:56 -0700 | [diff] [blame^] | 400 | (unsigned long long) *iova_start, |
| 401 | (unsigned long long) page_list[0], |
| 402 | (unsigned long long) page_list[pbl_depth-1]); |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 403 | err = c2_nsmr_register_phys_kern(to_c2dev(ib_pd->device), page_list, |
| 404 | (1 << page_shift), pbl_depth, |
| 405 | total_len, 0, iova_start, |
| 406 | c2_convert_access(acc), mr); |
| 407 | vfree(page_list); |
| 408 | if (err) { |
| 409 | kfree(mr); |
| 410 | return ERR_PTR(err); |
| 411 | } |
| 412 | |
| 413 | return &mr->ibmr; |
| 414 | } |
| 415 | |
| 416 | static struct ib_mr *c2_get_dma_mr(struct ib_pd *pd, int acc) |
| 417 | { |
| 418 | struct ib_phys_buf bl; |
| 419 | u64 kva = 0; |
| 420 | |
| 421 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 422 | |
| 423 | /* AMSO1100 limit */ |
| 424 | bl.size = 0xffffffff; |
| 425 | bl.addr = 0; |
| 426 | return c2_reg_phys_mr(pd, &bl, 1, acc, &kva); |
| 427 | } |
| 428 | |
| 429 | static struct ib_mr *c2_reg_user_mr(struct ib_pd *pd, struct ib_umem *region, |
| 430 | int acc, struct ib_udata *udata) |
| 431 | { |
| 432 | u64 *pages; |
| 433 | u64 kva = 0; |
| 434 | int shift, n, len; |
| 435 | int i, j, k; |
| 436 | int err = 0; |
| 437 | struct ib_umem_chunk *chunk; |
| 438 | struct c2_pd *c2pd = to_c2pd(pd); |
| 439 | struct c2_mr *c2mr; |
| 440 | |
| 441 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 442 | shift = ffs(region->page_size) - 1; |
| 443 | |
| 444 | c2mr = kmalloc(sizeof(*c2mr), GFP_KERNEL); |
| 445 | if (!c2mr) |
| 446 | return ERR_PTR(-ENOMEM); |
| 447 | c2mr->pd = c2pd; |
| 448 | |
| 449 | n = 0; |
| 450 | list_for_each_entry(chunk, ®ion->chunk_list, list) |
| 451 | n += chunk->nents; |
| 452 | |
| 453 | pages = kmalloc(n * sizeof(u64), GFP_KERNEL); |
| 454 | if (!pages) { |
| 455 | err = -ENOMEM; |
| 456 | goto err; |
| 457 | } |
| 458 | |
| 459 | i = 0; |
| 460 | list_for_each_entry(chunk, ®ion->chunk_list, list) { |
| 461 | for (j = 0; j < chunk->nmap; ++j) { |
| 462 | len = sg_dma_len(&chunk->page_list[j]) >> shift; |
| 463 | for (k = 0; k < len; ++k) { |
| 464 | pages[i++] = |
| 465 | sg_dma_address(&chunk->page_list[j]) + |
| 466 | (region->page_size * k); |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | kva = (u64)region->virt_base; |
| 472 | err = c2_nsmr_register_phys_kern(to_c2dev(pd->device), |
| 473 | pages, |
| 474 | region->page_size, |
| 475 | i, |
| 476 | region->length, |
| 477 | region->offset, |
| 478 | &kva, |
| 479 | c2_convert_access(acc), |
| 480 | c2mr); |
| 481 | kfree(pages); |
| 482 | if (err) { |
| 483 | kfree(c2mr); |
| 484 | return ERR_PTR(err); |
| 485 | } |
| 486 | return &c2mr->ibmr; |
| 487 | |
| 488 | err: |
| 489 | kfree(c2mr); |
| 490 | return ERR_PTR(err); |
| 491 | } |
| 492 | |
| 493 | static int c2_dereg_mr(struct ib_mr *ib_mr) |
| 494 | { |
| 495 | struct c2_mr *mr = to_c2mr(ib_mr); |
| 496 | int err; |
| 497 | |
| 498 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 499 | |
| 500 | err = c2_stag_dealloc(to_c2dev(ib_mr->device), ib_mr->lkey); |
| 501 | if (err) |
| 502 | pr_debug("c2_stag_dealloc failed: %d\n", err); |
| 503 | else |
| 504 | kfree(mr); |
| 505 | |
| 506 | return err; |
| 507 | } |
| 508 | |
| 509 | static ssize_t show_rev(struct class_device *cdev, char *buf) |
| 510 | { |
| 511 | struct c2_dev *dev = container_of(cdev, struct c2_dev, ibdev.class_dev); |
| 512 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 513 | return sprintf(buf, "%x\n", dev->props.hw_ver); |
| 514 | } |
| 515 | |
| 516 | static ssize_t show_fw_ver(struct class_device *cdev, char *buf) |
| 517 | { |
| 518 | struct c2_dev *dev = container_of(cdev, struct c2_dev, ibdev.class_dev); |
| 519 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 520 | return sprintf(buf, "%x.%x.%x\n", |
| 521 | (int) (dev->props.fw_ver >> 32), |
| 522 | (int) (dev->props.fw_ver >> 16) & 0xffff, |
| 523 | (int) (dev->props.fw_ver & 0xffff)); |
| 524 | } |
| 525 | |
| 526 | static ssize_t show_hca(struct class_device *cdev, char *buf) |
| 527 | { |
| 528 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 529 | return sprintf(buf, "AMSO1100\n"); |
| 530 | } |
| 531 | |
| 532 | static ssize_t show_board(struct class_device *cdev, char *buf) |
| 533 | { |
| 534 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 535 | return sprintf(buf, "%.*s\n", 32, "AMSO1100 Board ID"); |
| 536 | } |
| 537 | |
| 538 | static CLASS_DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL); |
| 539 | static CLASS_DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL); |
| 540 | static CLASS_DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL); |
| 541 | static CLASS_DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL); |
| 542 | |
| 543 | static struct class_device_attribute *c2_class_attributes[] = { |
| 544 | &class_device_attr_hw_rev, |
| 545 | &class_device_attr_fw_ver, |
| 546 | &class_device_attr_hca_type, |
| 547 | &class_device_attr_board_id |
| 548 | }; |
| 549 | |
| 550 | static int c2_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, |
| 551 | int attr_mask, struct ib_udata *udata) |
| 552 | { |
| 553 | int err; |
| 554 | |
| 555 | err = |
| 556 | c2_qp_modify(to_c2dev(ibqp->device), to_c2qp(ibqp), attr, |
| 557 | attr_mask); |
| 558 | |
| 559 | return err; |
| 560 | } |
| 561 | |
| 562 | static int c2_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) |
| 563 | { |
| 564 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 565 | return -ENOSYS; |
| 566 | } |
| 567 | |
| 568 | static int c2_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid) |
| 569 | { |
| 570 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 571 | return -ENOSYS; |
| 572 | } |
| 573 | |
| 574 | static int c2_process_mad(struct ib_device *ibdev, |
| 575 | int mad_flags, |
| 576 | u8 port_num, |
| 577 | struct ib_wc *in_wc, |
| 578 | struct ib_grh *in_grh, |
| 579 | struct ib_mad *in_mad, struct ib_mad *out_mad) |
| 580 | { |
| 581 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 582 | return -ENOSYS; |
| 583 | } |
| 584 | |
| 585 | static int c2_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param) |
| 586 | { |
| 587 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 588 | |
| 589 | /* Request a connection */ |
| 590 | return c2_llp_connect(cm_id, iw_param); |
| 591 | } |
| 592 | |
| 593 | static int c2_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param) |
| 594 | { |
| 595 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 596 | |
| 597 | /* Accept the new connection */ |
| 598 | return c2_llp_accept(cm_id, iw_param); |
| 599 | } |
| 600 | |
| 601 | static int c2_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len) |
| 602 | { |
| 603 | int err; |
| 604 | |
| 605 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 606 | |
| 607 | err = c2_llp_reject(cm_id, pdata, pdata_len); |
| 608 | return err; |
| 609 | } |
| 610 | |
| 611 | static int c2_service_create(struct iw_cm_id *cm_id, int backlog) |
| 612 | { |
| 613 | int err; |
| 614 | |
| 615 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 616 | err = c2_llp_service_create(cm_id, backlog); |
| 617 | pr_debug("%s:%u err=%d\n", |
| 618 | __FUNCTION__, __LINE__, |
| 619 | err); |
| 620 | return err; |
| 621 | } |
| 622 | |
| 623 | static int c2_service_destroy(struct iw_cm_id *cm_id) |
| 624 | { |
| 625 | int err; |
| 626 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 627 | |
| 628 | err = c2_llp_service_destroy(cm_id); |
| 629 | |
| 630 | return err; |
| 631 | } |
| 632 | |
| 633 | static int c2_pseudo_up(struct net_device *netdev) |
| 634 | { |
| 635 | struct in_device *ind; |
| 636 | struct c2_dev *c2dev = netdev->priv; |
| 637 | |
| 638 | ind = in_dev_get(netdev); |
| 639 | if (!ind) |
| 640 | return 0; |
| 641 | |
| 642 | pr_debug("adding...\n"); |
| 643 | for_ifa(ind) { |
| 644 | #ifdef DEBUG |
| 645 | u8 *ip = (u8 *) & ifa->ifa_address; |
| 646 | |
| 647 | pr_debug("%s: %d.%d.%d.%d\n", |
| 648 | ifa->ifa_label, ip[0], ip[1], ip[2], ip[3]); |
| 649 | #endif |
| 650 | c2_add_addr(c2dev, ifa->ifa_address, ifa->ifa_mask); |
| 651 | } |
| 652 | endfor_ifa(ind); |
| 653 | in_dev_put(ind); |
| 654 | |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | static int c2_pseudo_down(struct net_device *netdev) |
| 659 | { |
| 660 | struct in_device *ind; |
| 661 | struct c2_dev *c2dev = netdev->priv; |
| 662 | |
| 663 | ind = in_dev_get(netdev); |
| 664 | if (!ind) |
| 665 | return 0; |
| 666 | |
| 667 | pr_debug("deleting...\n"); |
| 668 | for_ifa(ind) { |
| 669 | #ifdef DEBUG |
| 670 | u8 *ip = (u8 *) & ifa->ifa_address; |
| 671 | |
| 672 | pr_debug("%s: %d.%d.%d.%d\n", |
| 673 | ifa->ifa_label, ip[0], ip[1], ip[2], ip[3]); |
| 674 | #endif |
| 675 | c2_del_addr(c2dev, ifa->ifa_address, ifa->ifa_mask); |
| 676 | } |
| 677 | endfor_ifa(ind); |
| 678 | in_dev_put(ind); |
| 679 | |
| 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | static int c2_pseudo_xmit_frame(struct sk_buff *skb, struct net_device *netdev) |
| 684 | { |
| 685 | kfree_skb(skb); |
| 686 | return NETDEV_TX_OK; |
| 687 | } |
| 688 | |
| 689 | static int c2_pseudo_change_mtu(struct net_device *netdev, int new_mtu) |
| 690 | { |
| 691 | int ret = 0; |
| 692 | |
| 693 | if (new_mtu < ETH_ZLEN || new_mtu > ETH_JUMBO_MTU) |
| 694 | return -EINVAL; |
| 695 | |
| 696 | netdev->mtu = new_mtu; |
| 697 | |
| 698 | /* TODO: Tell rnic about new rmda interface mtu */ |
| 699 | return ret; |
| 700 | } |
| 701 | |
| 702 | static void setup(struct net_device *netdev) |
| 703 | { |
| 704 | SET_MODULE_OWNER(netdev); |
| 705 | netdev->open = c2_pseudo_up; |
| 706 | netdev->stop = c2_pseudo_down; |
| 707 | netdev->hard_start_xmit = c2_pseudo_xmit_frame; |
| 708 | netdev->get_stats = NULL; |
| 709 | netdev->tx_timeout = NULL; |
| 710 | netdev->set_mac_address = NULL; |
| 711 | netdev->change_mtu = c2_pseudo_change_mtu; |
| 712 | netdev->watchdog_timeo = 0; |
| 713 | netdev->type = ARPHRD_ETHER; |
| 714 | netdev->mtu = 1500; |
| 715 | netdev->hard_header_len = ETH_HLEN; |
| 716 | netdev->addr_len = ETH_ALEN; |
| 717 | netdev->tx_queue_len = 0; |
| 718 | netdev->flags |= IFF_NOARP; |
| 719 | return; |
| 720 | } |
| 721 | |
| 722 | static struct net_device *c2_pseudo_netdev_init(struct c2_dev *c2dev) |
| 723 | { |
| 724 | char name[IFNAMSIZ]; |
| 725 | struct net_device *netdev; |
| 726 | |
| 727 | /* change ethxxx to iwxxx */ |
| 728 | strcpy(name, "iw"); |
| 729 | strcat(name, &c2dev->netdev->name[3]); |
| 730 | netdev = alloc_netdev(sizeof(*netdev), name, setup); |
| 731 | if (!netdev) { |
| 732 | printk(KERN_ERR PFX "%s - etherdev alloc failed", |
| 733 | __FUNCTION__); |
| 734 | return NULL; |
| 735 | } |
| 736 | |
| 737 | netdev->priv = c2dev; |
| 738 | |
| 739 | SET_NETDEV_DEV(netdev, &c2dev->pcidev->dev); |
| 740 | |
| 741 | memcpy_fromio(netdev->dev_addr, c2dev->kva + C2_REGS_RDMA_ENADDR, 6); |
| 742 | |
| 743 | /* Print out the MAC address */ |
| 744 | pr_debug("%s: MAC %02X:%02X:%02X:%02X:%02X:%02X\n", |
| 745 | netdev->name, |
| 746 | netdev->dev_addr[0], netdev->dev_addr[1], netdev->dev_addr[2], |
| 747 | netdev->dev_addr[3], netdev->dev_addr[4], netdev->dev_addr[5]); |
| 748 | |
| 749 | #if 0 |
| 750 | /* Disable network packets */ |
| 751 | netif_stop_queue(netdev); |
| 752 | #endif |
| 753 | return netdev; |
| 754 | } |
| 755 | |
| 756 | int c2_register_device(struct c2_dev *dev) |
| 757 | { |
| 758 | int ret; |
| 759 | int i; |
| 760 | |
| 761 | /* Register pseudo network device */ |
| 762 | dev->pseudo_netdev = c2_pseudo_netdev_init(dev); |
| 763 | if (dev->pseudo_netdev) { |
| 764 | ret = register_netdev(dev->pseudo_netdev); |
| 765 | if (ret) { |
| 766 | printk(KERN_ERR PFX |
| 767 | "Unable to register netdev, ret = %d\n", ret); |
| 768 | free_netdev(dev->pseudo_netdev); |
| 769 | return ret; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 774 | strlcpy(dev->ibdev.name, "amso%d", IB_DEVICE_NAME_MAX); |
| 775 | dev->ibdev.owner = THIS_MODULE; |
| 776 | dev->ibdev.uverbs_cmd_mask = |
| 777 | (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) | |
| 778 | (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) | |
| 779 | (1ull << IB_USER_VERBS_CMD_QUERY_PORT) | |
| 780 | (1ull << IB_USER_VERBS_CMD_ALLOC_PD) | |
| 781 | (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) | |
| 782 | (1ull << IB_USER_VERBS_CMD_REG_MR) | |
| 783 | (1ull << IB_USER_VERBS_CMD_DEREG_MR) | |
| 784 | (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) | |
| 785 | (1ull << IB_USER_VERBS_CMD_CREATE_CQ) | |
| 786 | (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) | |
| 787 | (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) | |
| 788 | (1ull << IB_USER_VERBS_CMD_CREATE_QP) | |
| 789 | (1ull << IB_USER_VERBS_CMD_MODIFY_QP) | |
| 790 | (1ull << IB_USER_VERBS_CMD_POLL_CQ) | |
| 791 | (1ull << IB_USER_VERBS_CMD_DESTROY_QP) | |
| 792 | (1ull << IB_USER_VERBS_CMD_POST_SEND) | |
| 793 | (1ull << IB_USER_VERBS_CMD_POST_RECV); |
| 794 | |
| 795 | dev->ibdev.node_type = RDMA_NODE_RNIC; |
| 796 | memset(&dev->ibdev.node_guid, 0, sizeof(dev->ibdev.node_guid)); |
| 797 | memcpy(&dev->ibdev.node_guid, dev->pseudo_netdev->dev_addr, 6); |
| 798 | dev->ibdev.phys_port_cnt = 1; |
| 799 | dev->ibdev.dma_device = &dev->pcidev->dev; |
| 800 | dev->ibdev.class_dev.dev = &dev->pcidev->dev; |
| 801 | dev->ibdev.query_device = c2_query_device; |
| 802 | dev->ibdev.query_port = c2_query_port; |
| 803 | dev->ibdev.modify_port = c2_modify_port; |
| 804 | dev->ibdev.query_pkey = c2_query_pkey; |
| 805 | dev->ibdev.query_gid = c2_query_gid; |
| 806 | dev->ibdev.alloc_ucontext = c2_alloc_ucontext; |
| 807 | dev->ibdev.dealloc_ucontext = c2_dealloc_ucontext; |
| 808 | dev->ibdev.mmap = c2_mmap_uar; |
| 809 | dev->ibdev.alloc_pd = c2_alloc_pd; |
| 810 | dev->ibdev.dealloc_pd = c2_dealloc_pd; |
| 811 | dev->ibdev.create_ah = c2_ah_create; |
| 812 | dev->ibdev.destroy_ah = c2_ah_destroy; |
| 813 | dev->ibdev.create_qp = c2_create_qp; |
| 814 | dev->ibdev.modify_qp = c2_modify_qp; |
| 815 | dev->ibdev.destroy_qp = c2_destroy_qp; |
| 816 | dev->ibdev.create_cq = c2_create_cq; |
| 817 | dev->ibdev.destroy_cq = c2_destroy_cq; |
| 818 | dev->ibdev.poll_cq = c2_poll_cq; |
| 819 | dev->ibdev.get_dma_mr = c2_get_dma_mr; |
| 820 | dev->ibdev.reg_phys_mr = c2_reg_phys_mr; |
| 821 | dev->ibdev.reg_user_mr = c2_reg_user_mr; |
| 822 | dev->ibdev.dereg_mr = c2_dereg_mr; |
| 823 | |
| 824 | dev->ibdev.alloc_fmr = NULL; |
| 825 | dev->ibdev.unmap_fmr = NULL; |
| 826 | dev->ibdev.dealloc_fmr = NULL; |
| 827 | dev->ibdev.map_phys_fmr = NULL; |
| 828 | |
| 829 | dev->ibdev.attach_mcast = c2_multicast_attach; |
| 830 | dev->ibdev.detach_mcast = c2_multicast_detach; |
| 831 | dev->ibdev.process_mad = c2_process_mad; |
| 832 | |
| 833 | dev->ibdev.req_notify_cq = c2_arm_cq; |
| 834 | dev->ibdev.post_send = c2_post_send; |
| 835 | dev->ibdev.post_recv = c2_post_receive; |
| 836 | |
| 837 | dev->ibdev.iwcm = kmalloc(sizeof(*dev->ibdev.iwcm), GFP_KERNEL); |
| 838 | dev->ibdev.iwcm->add_ref = c2_add_ref; |
| 839 | dev->ibdev.iwcm->rem_ref = c2_rem_ref; |
| 840 | dev->ibdev.iwcm->get_qp = c2_get_qp; |
| 841 | dev->ibdev.iwcm->connect = c2_connect; |
| 842 | dev->ibdev.iwcm->accept = c2_accept; |
| 843 | dev->ibdev.iwcm->reject = c2_reject; |
| 844 | dev->ibdev.iwcm->create_listen = c2_service_create; |
| 845 | dev->ibdev.iwcm->destroy_listen = c2_service_destroy; |
| 846 | |
| 847 | ret = ib_register_device(&dev->ibdev); |
| 848 | if (ret) |
| 849 | return ret; |
| 850 | |
| 851 | for (i = 0; i < ARRAY_SIZE(c2_class_attributes); ++i) { |
| 852 | ret = class_device_create_file(&dev->ibdev.class_dev, |
| 853 | c2_class_attributes[i]); |
| 854 | if (ret) { |
| 855 | unregister_netdev(dev->pseudo_netdev); |
| 856 | free_netdev(dev->pseudo_netdev); |
| 857 | ib_unregister_device(&dev->ibdev); |
| 858 | return ret; |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | void c2_unregister_device(struct c2_dev *dev) |
| 867 | { |
| 868 | pr_debug("%s:%u\n", __FUNCTION__, __LINE__); |
| 869 | unregister_netdev(dev->pseudo_netdev); |
| 870 | free_netdev(dev->pseudo_netdev); |
| 871 | ib_unregister_device(&dev->ibdev); |
| 872 | } |