1 /* 2 * Copyright (c) 2004-2007 Voltaire, Inc. All rights reserved. 3 * Copyright (c) 2005 Intel Corporation. All rights reserved. 4 * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved. 5 * Copyright (c) 2009 HNR Consulting. All rights reserved. 6 * 7 * This software is available to you under a choice of one of two 8 * licenses. You may choose to be licensed under the terms of the GNU 9 * General Public License (GPL) Version 2, available from the file 10 * COPYING in the main directory of this source tree, or the 11 * OpenIB.org BSD license below: 12 * 13 * Redistribution and use in source and binary forms, with or 14 * without modification, are permitted provided that the following 15 * conditions are met: 16 * 17 * - Redistributions of source code must retain the above 18 * copyright notice, this list of conditions and the following 19 * disclaimer. 20 * 21 * - Redistributions in binary form must reproduce the above 22 * copyright notice, this list of conditions and the following 23 * disclaimer in the documentation and/or other materials 24 * provided with the distribution. 25 * 26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 33 * SOFTWARE. 34 * 35 */ 36 #include <linux/dma-mapping.h> 37 #include <linux/slab.h> 38 #include <rdma/ib_cache.h> 39 40 #include "mad_priv.h" 41 #include "mad_rmpp.h" 42 #include "smi.h" 43 #include "agent.h" 44 45 MODULE_LICENSE("Dual BSD/GPL"); 46 MODULE_DESCRIPTION("kernel IB MAD API"); 47 MODULE_AUTHOR("Hal Rosenstock"); 48 MODULE_AUTHOR("Sean Hefty"); 49 50 static int mad_sendq_size = IB_MAD_QP_SEND_SIZE; 51 static int mad_recvq_size = IB_MAD_QP_RECV_SIZE; 52 53 module_param_named(send_queue_size, mad_sendq_size, int, 0444); 54 MODULE_PARM_DESC(send_queue_size, "Size of send queue in number of work requests"); 55 module_param_named(recv_queue_size, mad_recvq_size, int, 0444); 56 MODULE_PARM_DESC(recv_queue_size, "Size of receive queue in number of work requests"); 57 58 static struct kmem_cache *ib_mad_cache; 59 60 static struct list_head ib_mad_port_list; 61 static u32 ib_mad_client_id = 0; 62 63 /* Port list lock */ 64 static DEFINE_SPINLOCK(ib_mad_port_list_lock); 65 66 /* Forward declarations */ 67 static int method_in_use(struct ib_mad_mgmt_method_table **method, 68 struct ib_mad_reg_req *mad_reg_req); 69 static void remove_mad_reg_req(struct ib_mad_agent_private *priv); 70 static struct ib_mad_agent_private *find_mad_agent( 71 struct ib_mad_port_private *port_priv, 72 struct ib_mad *mad); 73 static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info, 74 struct ib_mad_private *mad); 75 static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv); 76 static void timeout_sends(struct work_struct *work); 77 static void local_completions(struct work_struct *work); 78 static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req, 79 struct ib_mad_agent_private *agent_priv, 80 u8 mgmt_class); 81 static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req, 82 struct ib_mad_agent_private *agent_priv); 83 84 /* 85 * Returns a ib_mad_port_private structure or NULL for a device/port 86 * Assumes ib_mad_port_list_lock is being held 87 */ 88 static inline struct ib_mad_port_private * 89 __ib_get_mad_port(struct ib_device *device, int port_num) 90 { 91 struct ib_mad_port_private *entry; 92 93 list_for_each_entry(entry, &ib_mad_port_list, port_list) { 94 if (entry->device == device && entry->port_num == port_num) 95 return entry; 96 } 97 return NULL; 98 } 99 100 /* 101 * Wrapper function to return a ib_mad_port_private structure or NULL 102 * for a device/port 103 */ 104 static inline struct ib_mad_port_private * 105 ib_get_mad_port(struct ib_device *device, int port_num) 106 { 107 struct ib_mad_port_private *entry; 108 unsigned long flags; 109 110 spin_lock_irqsave(&ib_mad_port_list_lock, flags); 111 entry = __ib_get_mad_port(device, port_num); 112 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags); 113 114 return entry; 115 } 116 117 static inline u8 convert_mgmt_class(u8 mgmt_class) 118 { 119 /* Alias IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE to 0 */ 120 return mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ? 121 0 : mgmt_class; 122 } 123 124 static int get_spl_qp_index(enum ib_qp_type qp_type) 125 { 126 switch (qp_type) 127 { 128 case IB_QPT_SMI: 129 return 0; 130 case IB_QPT_GSI: 131 return 1; 132 default: 133 return -1; 134 } 135 } 136 137 static int vendor_class_index(u8 mgmt_class) 138 { 139 return mgmt_class - IB_MGMT_CLASS_VENDOR_RANGE2_START; 140 } 141 142 static int is_vendor_class(u8 mgmt_class) 143 { 144 if ((mgmt_class < IB_MGMT_CLASS_VENDOR_RANGE2_START) || 145 (mgmt_class > IB_MGMT_CLASS_VENDOR_RANGE2_END)) 146 return 0; 147 return 1; 148 } 149 150 static int is_vendor_oui(char *oui) 151 { 152 if (oui[0] || oui[1] || oui[2]) 153 return 1; 154 return 0; 155 } 156 157 static int is_vendor_method_in_use( 158 struct ib_mad_mgmt_vendor_class *vendor_class, 159 struct ib_mad_reg_req *mad_reg_req) 160 { 161 struct ib_mad_mgmt_method_table *method; 162 int i; 163 164 for (i = 0; i < MAX_MGMT_OUI; i++) { 165 if (!memcmp(vendor_class->oui[i], mad_reg_req->oui, 3)) { 166 method = vendor_class->method_table[i]; 167 if (method) { 168 if (method_in_use(&method, mad_reg_req)) 169 return 1; 170 else 171 break; 172 } 173 } 174 } 175 return 0; 176 } 177 178 int ib_response_mad(struct ib_mad *mad) 179 { 180 return ((mad->mad_hdr.method & IB_MGMT_METHOD_RESP) || 181 (mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS) || 182 ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_BM) && 183 (mad->mad_hdr.attr_mod & IB_BM_ATTR_MOD_RESP))); 184 } 185 EXPORT_SYMBOL(ib_response_mad); 186 187 /* 188 * ib_register_mad_agent - Register to send/receive MADs 189 */ 190 struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device, 191 u8 port_num, 192 enum ib_qp_type qp_type, 193 struct ib_mad_reg_req *mad_reg_req, 194 u8 rmpp_version, 195 ib_mad_send_handler send_handler, 196 ib_mad_recv_handler recv_handler, 197 void *context) 198 { 199 struct ib_mad_port_private *port_priv; 200 struct ib_mad_agent *ret = ERR_PTR(-EINVAL); 201 struct ib_mad_agent_private *mad_agent_priv; 202 struct ib_mad_reg_req *reg_req = NULL; 203 struct ib_mad_mgmt_class_table *class; 204 struct ib_mad_mgmt_vendor_class_table *vendor; 205 struct ib_mad_mgmt_vendor_class *vendor_class; 206 struct ib_mad_mgmt_method_table *method; 207 int ret2, qpn; 208 unsigned long flags; 209 u8 mgmt_class, vclass; 210 211 /* Validate parameters */ 212 qpn = get_spl_qp_index(qp_type); 213 if (qpn == -1) 214 goto error1; 215 216 if (rmpp_version && rmpp_version != IB_MGMT_RMPP_VERSION) 217 goto error1; 218 219 /* Validate MAD registration request if supplied */ 220 if (mad_reg_req) { 221 if (mad_reg_req->mgmt_class_version >= MAX_MGMT_VERSION) 222 goto error1; 223 if (!recv_handler) 224 goto error1; 225 if (mad_reg_req->mgmt_class >= MAX_MGMT_CLASS) { 226 /* 227 * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE is the only 228 * one in this range currently allowed 229 */ 230 if (mad_reg_req->mgmt_class != 231 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) 232 goto error1; 233 } else if (mad_reg_req->mgmt_class == 0) { 234 /* 235 * Class 0 is reserved in IBA and is used for 236 * aliasing of IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE 237 */ 238 goto error1; 239 } else if (is_vendor_class(mad_reg_req->mgmt_class)) { 240 /* 241 * If class is in "new" vendor range, 242 * ensure supplied OUI is not zero 243 */ 244 if (!is_vendor_oui(mad_reg_req->oui)) 245 goto error1; 246 } 247 /* Make sure class supplied is consistent with RMPP */ 248 if (!ib_is_mad_class_rmpp(mad_reg_req->mgmt_class)) { 249 if (rmpp_version) 250 goto error1; 251 } 252 /* Make sure class supplied is consistent with QP type */ 253 if (qp_type == IB_QPT_SMI) { 254 if ((mad_reg_req->mgmt_class != 255 IB_MGMT_CLASS_SUBN_LID_ROUTED) && 256 (mad_reg_req->mgmt_class != 257 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) 258 goto error1; 259 } else { 260 if ((mad_reg_req->mgmt_class == 261 IB_MGMT_CLASS_SUBN_LID_ROUTED) || 262 (mad_reg_req->mgmt_class == 263 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) 264 goto error1; 265 } 266 } else { 267 /* No registration request supplied */ 268 if (!send_handler) 269 goto error1; 270 } 271 272 /* Validate device and port */ 273 port_priv = ib_get_mad_port(device, port_num); 274 if (!port_priv) { 275 ret = ERR_PTR(-ENODEV); 276 goto error1; 277 } 278 279 /* Allocate structures */ 280 mad_agent_priv = kzalloc(sizeof *mad_agent_priv, GFP_KERNEL); 281 if (!mad_agent_priv) { 282 ret = ERR_PTR(-ENOMEM); 283 goto error1; 284 } 285 286 mad_agent_priv->agent.mr = ib_get_dma_mr(port_priv->qp_info[qpn].qp->pd, 287 IB_ACCESS_LOCAL_WRITE); 288 if (IS_ERR(mad_agent_priv->agent.mr)) { 289 ret = ERR_PTR(-ENOMEM); 290 goto error2; 291 } 292 293 if (mad_reg_req) { 294 reg_req = kmemdup(mad_reg_req, sizeof *reg_req, GFP_KERNEL); 295 if (!reg_req) { 296 ret = ERR_PTR(-ENOMEM); 297 goto error3; 298 } 299 } 300 301 /* Now, fill in the various structures */ 302 mad_agent_priv->qp_info = &port_priv->qp_info[qpn]; 303 mad_agent_priv->reg_req = reg_req; 304 mad_agent_priv->agent.rmpp_version = rmpp_version; 305 mad_agent_priv->agent.device = device; 306 mad_agent_priv->agent.recv_handler = recv_handler; 307 mad_agent_priv->agent.send_handler = send_handler; 308 mad_agent_priv->agent.context = context; 309 mad_agent_priv->agent.qp = port_priv->qp_info[qpn].qp; 310 mad_agent_priv->agent.port_num = port_num; 311 spin_lock_init(&mad_agent_priv->lock); 312 INIT_LIST_HEAD(&mad_agent_priv->send_list); 313 INIT_LIST_HEAD(&mad_agent_priv->wait_list); 314 INIT_LIST_HEAD(&mad_agent_priv->done_list); 315 INIT_LIST_HEAD(&mad_agent_priv->rmpp_list); 316 INIT_DELAYED_WORK(&mad_agent_priv->timed_work, timeout_sends); 317 INIT_LIST_HEAD(&mad_agent_priv->local_list); 318 INIT_WORK(&mad_agent_priv->local_work, local_completions); 319 atomic_set(&mad_agent_priv->refcount, 1); 320 init_completion(&mad_agent_priv->comp); 321 322 spin_lock_irqsave(&port_priv->reg_lock, flags); 323 mad_agent_priv->agent.hi_tid = ++ib_mad_client_id; 324 325 /* 326 * Make sure MAD registration (if supplied) 327 * is non overlapping with any existing ones 328 */ 329 if (mad_reg_req) { 330 mgmt_class = convert_mgmt_class(mad_reg_req->mgmt_class); 331 if (!is_vendor_class(mgmt_class)) { 332 class = port_priv->version[mad_reg_req-> 333 mgmt_class_version].class; 334 if (class) { 335 method = class->method_table[mgmt_class]; 336 if (method) { 337 if (method_in_use(&method, 338 mad_reg_req)) 339 goto error4; 340 } 341 } 342 ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv, 343 mgmt_class); 344 } else { 345 /* "New" vendor class range */ 346 vendor = port_priv->version[mad_reg_req-> 347 mgmt_class_version].vendor; 348 if (vendor) { 349 vclass = vendor_class_index(mgmt_class); 350 vendor_class = vendor->vendor_class[vclass]; 351 if (vendor_class) { 352 if (is_vendor_method_in_use( 353 vendor_class, 354 mad_reg_req)) 355 goto error4; 356 } 357 } 358 ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv); 359 } 360 if (ret2) { 361 ret = ERR_PTR(ret2); 362 goto error4; 363 } 364 } 365 366 /* Add mad agent into port's agent list */ 367 list_add_tail(&mad_agent_priv->agent_list, &port_priv->agent_list); 368 spin_unlock_irqrestore(&port_priv->reg_lock, flags); 369 370 return &mad_agent_priv->agent; 371 372 error4: 373 spin_unlock_irqrestore(&port_priv->reg_lock, flags); 374 kfree(reg_req); 375 error3: 376 ib_dereg_mr(mad_agent_priv->agent.mr); 377 error2: 378 kfree(mad_agent_priv); 379 error1: 380 return ret; 381 } 382 EXPORT_SYMBOL(ib_register_mad_agent); 383 384 static inline int is_snooping_sends(int mad_snoop_flags) 385 { 386 return (mad_snoop_flags & 387 (/*IB_MAD_SNOOP_POSTED_SENDS | 388 IB_MAD_SNOOP_RMPP_SENDS |*/ 389 IB_MAD_SNOOP_SEND_COMPLETIONS /*| 390 IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/)); 391 } 392 393 static inline int is_snooping_recvs(int mad_snoop_flags) 394 { 395 return (mad_snoop_flags & 396 (IB_MAD_SNOOP_RECVS /*| 397 IB_MAD_SNOOP_RMPP_RECVS*/)); 398 } 399 400 static int register_snoop_agent(struct ib_mad_qp_info *qp_info, 401 struct ib_mad_snoop_private *mad_snoop_priv) 402 { 403 struct ib_mad_snoop_private **new_snoop_table; 404 unsigned long flags; 405 int i; 406 407 spin_lock_irqsave(&qp_info->snoop_lock, flags); 408 /* Check for empty slot in array. */ 409 for (i = 0; i < qp_info->snoop_table_size; i++) 410 if (!qp_info->snoop_table[i]) 411 break; 412 413 if (i == qp_info->snoop_table_size) { 414 /* Grow table. */ 415 new_snoop_table = krealloc(qp_info->snoop_table, 416 sizeof mad_snoop_priv * 417 (qp_info->snoop_table_size + 1), 418 GFP_ATOMIC); 419 if (!new_snoop_table) { 420 i = -ENOMEM; 421 goto out; 422 } 423 424 qp_info->snoop_table = new_snoop_table; 425 qp_info->snoop_table_size++; 426 } 427 qp_info->snoop_table[i] = mad_snoop_priv; 428 atomic_inc(&qp_info->snoop_count); 429 out: 430 spin_unlock_irqrestore(&qp_info->snoop_lock, flags); 431 return i; 432 } 433 434 struct ib_mad_agent *ib_register_mad_snoop(struct ib_device *device, 435 u8 port_num, 436 enum ib_qp_type qp_type, 437 int mad_snoop_flags, 438 ib_mad_snoop_handler snoop_handler, 439 ib_mad_recv_handler recv_handler, 440 void *context) 441 { 442 struct ib_mad_port_private *port_priv; 443 struct ib_mad_agent *ret; 444 struct ib_mad_snoop_private *mad_snoop_priv; 445 int qpn; 446 447 /* Validate parameters */ 448 if ((is_snooping_sends(mad_snoop_flags) && !snoop_handler) || 449 (is_snooping_recvs(mad_snoop_flags) && !recv_handler)) { 450 ret = ERR_PTR(-EINVAL); 451 goto error1; 452 } 453 qpn = get_spl_qp_index(qp_type); 454 if (qpn == -1) { 455 ret = ERR_PTR(-EINVAL); 456 goto error1; 457 } 458 port_priv = ib_get_mad_port(device, port_num); 459 if (!port_priv) { 460 ret = ERR_PTR(-ENODEV); 461 goto error1; 462 } 463 /* Allocate structures */ 464 mad_snoop_priv = kzalloc(sizeof *mad_snoop_priv, GFP_KERNEL); 465 if (!mad_snoop_priv) { 466 ret = ERR_PTR(-ENOMEM); 467 goto error1; 468 } 469 470 /* Now, fill in the various structures */ 471 mad_snoop_priv->qp_info = &port_priv->qp_info[qpn]; 472 mad_snoop_priv->agent.device = device; 473 mad_snoop_priv->agent.recv_handler = recv_handler; 474 mad_snoop_priv->agent.snoop_handler = snoop_handler; 475 mad_snoop_priv->agent.context = context; 476 mad_snoop_priv->agent.qp = port_priv->qp_info[qpn].qp; 477 mad_snoop_priv->agent.port_num = port_num; 478 mad_snoop_priv->mad_snoop_flags = mad_snoop_flags; 479 init_completion(&mad_snoop_priv->comp); 480 mad_snoop_priv->snoop_index = register_snoop_agent( 481 &port_priv->qp_info[qpn], 482 mad_snoop_priv); 483 if (mad_snoop_priv->snoop_index < 0) { 484 ret = ERR_PTR(mad_snoop_priv->snoop_index); 485 goto error2; 486 } 487 488 atomic_set(&mad_snoop_priv->refcount, 1); 489 return &mad_snoop_priv->agent; 490 491 error2: 492 kfree(mad_snoop_priv); 493 error1: 494 return ret; 495 } 496 EXPORT_SYMBOL(ib_register_mad_snoop); 497 498 static inline void deref_mad_agent(struct ib_mad_agent_private *mad_agent_priv) 499 { 500 if (atomic_dec_and_test(&mad_agent_priv->refcount)) 501 complete(&mad_agent_priv->comp); 502 } 503 504 static inline void deref_snoop_agent(struct ib_mad_snoop_private *mad_snoop_priv) 505 { 506 if (atomic_dec_and_test(&mad_snoop_priv->refcount)) 507 complete(&mad_snoop_priv->comp); 508 } 509 510 static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv) 511 { 512 struct ib_mad_port_private *port_priv; 513 unsigned long flags; 514 515 /* Note that we could still be handling received MADs */ 516 517 /* 518 * Canceling all sends results in dropping received response 519 * MADs, preventing us from queuing additional work 520 */ 521 cancel_mads(mad_agent_priv); 522 port_priv = mad_agent_priv->qp_info->port_priv; 523 cancel_delayed_work(&mad_agent_priv->timed_work); 524 525 spin_lock_irqsave(&port_priv->reg_lock, flags); 526 remove_mad_reg_req(mad_agent_priv); 527 list_del(&mad_agent_priv->agent_list); 528 spin_unlock_irqrestore(&port_priv->reg_lock, flags); 529 530 flush_workqueue(port_priv->wq); 531 ib_cancel_rmpp_recvs(mad_agent_priv); 532 533 deref_mad_agent(mad_agent_priv); 534 wait_for_completion(&mad_agent_priv->comp); 535 536 kfree(mad_agent_priv->reg_req); 537 ib_dereg_mr(mad_agent_priv->agent.mr); 538 kfree(mad_agent_priv); 539 } 540 541 static void unregister_mad_snoop(struct ib_mad_snoop_private *mad_snoop_priv) 542 { 543 struct ib_mad_qp_info *qp_info; 544 unsigned long flags; 545 546 qp_info = mad_snoop_priv->qp_info; 547 spin_lock_irqsave(&qp_info->snoop_lock, flags); 548 qp_info->snoop_table[mad_snoop_priv->snoop_index] = NULL; 549 atomic_dec(&qp_info->snoop_count); 550 spin_unlock_irqrestore(&qp_info->snoop_lock, flags); 551 552 deref_snoop_agent(mad_snoop_priv); 553 wait_for_completion(&mad_snoop_priv->comp); 554 555 kfree(mad_snoop_priv); 556 } 557 558 /* 559 * ib_unregister_mad_agent - Unregisters a client from using MAD services 560 */ 561 int ib_unregister_mad_agent(struct ib_mad_agent *mad_agent) 562 { 563 struct ib_mad_agent_private *mad_agent_priv; 564 struct ib_mad_snoop_private *mad_snoop_priv; 565 566 /* If the TID is zero, the agent can only snoop. */ 567 if (mad_agent->hi_tid) { 568 mad_agent_priv = container_of(mad_agent, 569 struct ib_mad_agent_private, 570 agent); 571 unregister_mad_agent(mad_agent_priv); 572 } else { 573 mad_snoop_priv = container_of(mad_agent, 574 struct ib_mad_snoop_private, 575 agent); 576 unregister_mad_snoop(mad_snoop_priv); 577 } 578 return 0; 579 } 580 EXPORT_SYMBOL(ib_unregister_mad_agent); 581 582 static void dequeue_mad(struct ib_mad_list_head *mad_list) 583 { 584 struct ib_mad_queue *mad_queue; 585 unsigned long flags; 586 587 BUG_ON(!mad_list->mad_queue); 588 mad_queue = mad_list->mad_queue; 589 spin_lock_irqsave(&mad_queue->lock, flags); 590 list_del(&mad_list->list); 591 mad_queue->count--; 592 spin_unlock_irqrestore(&mad_queue->lock, flags); 593 } 594 595 static void snoop_send(struct ib_mad_qp_info *qp_info, 596 struct ib_mad_send_buf *send_buf, 597 struct ib_mad_send_wc *mad_send_wc, 598 int mad_snoop_flags) 599 { 600 struct ib_mad_snoop_private *mad_snoop_priv; 601 unsigned long flags; 602 int i; 603 604 spin_lock_irqsave(&qp_info->snoop_lock, flags); 605 for (i = 0; i < qp_info->snoop_table_size; i++) { 606 mad_snoop_priv = qp_info->snoop_table[i]; 607 if (!mad_snoop_priv || 608 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags)) 609 continue; 610 611 atomic_inc(&mad_snoop_priv->refcount); 612 spin_unlock_irqrestore(&qp_info->snoop_lock, flags); 613 mad_snoop_priv->agent.snoop_handler(&mad_snoop_priv->agent, 614 send_buf, mad_send_wc); 615 deref_snoop_agent(mad_snoop_priv); 616 spin_lock_irqsave(&qp_info->snoop_lock, flags); 617 } 618 spin_unlock_irqrestore(&qp_info->snoop_lock, flags); 619 } 620 621 static void snoop_recv(struct ib_mad_qp_info *qp_info, 622 struct ib_mad_recv_wc *mad_recv_wc, 623 int mad_snoop_flags) 624 { 625 struct ib_mad_snoop_private *mad_snoop_priv; 626 unsigned long flags; 627 int i; 628 629 spin_lock_irqsave(&qp_info->snoop_lock, flags); 630 for (i = 0; i < qp_info->snoop_table_size; i++) { 631 mad_snoop_priv = qp_info->snoop_table[i]; 632 if (!mad_snoop_priv || 633 !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags)) 634 continue; 635 636 atomic_inc(&mad_snoop_priv->refcount); 637 spin_unlock_irqrestore(&qp_info->snoop_lock, flags); 638 mad_snoop_priv->agent.recv_handler(&mad_snoop_priv->agent, 639 mad_recv_wc); 640 deref_snoop_agent(mad_snoop_priv); 641 spin_lock_irqsave(&qp_info->snoop_lock, flags); 642 } 643 spin_unlock_irqrestore(&qp_info->snoop_lock, flags); 644 } 645 646 static void build_smp_wc(struct ib_qp *qp, 647 u64 wr_id, u16 slid, u16 pkey_index, u8 port_num, 648 struct ib_wc *wc) 649 { 650 memset(wc, 0, sizeof *wc); 651 wc->wr_id = wr_id; 652 wc->status = IB_WC_SUCCESS; 653 wc->opcode = IB_WC_RECV; 654 wc->pkey_index = pkey_index; 655 wc->byte_len = sizeof(struct ib_mad) + sizeof(struct ib_grh); 656 wc->src_qp = IB_QP0; 657 wc->qp = qp; 658 wc->slid = slid; 659 wc->sl = 0; 660 wc->dlid_path_bits = 0; 661 wc->port_num = port_num; 662 } 663 664 /* 665 * Return 0 if SMP is to be sent 666 * Return 1 if SMP was consumed locally (whether or not solicited) 667 * Return < 0 if error 668 */ 669 static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv, 670 struct ib_mad_send_wr_private *mad_send_wr) 671 { 672 int ret = 0; 673 struct ib_smp *smp = mad_send_wr->send_buf.mad; 674 unsigned long flags; 675 struct ib_mad_local_private *local; 676 struct ib_mad_private *mad_priv; 677 struct ib_mad_port_private *port_priv; 678 struct ib_mad_agent_private *recv_mad_agent = NULL; 679 struct ib_device *device = mad_agent_priv->agent.device; 680 u8 port_num; 681 struct ib_wc mad_wc; 682 struct ib_send_wr *send_wr = &mad_send_wr->send_wr; 683 684 if (device->node_type == RDMA_NODE_IB_SWITCH && 685 smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) 686 port_num = send_wr->wr.ud.port_num; 687 else 688 port_num = mad_agent_priv->agent.port_num; 689 690 /* 691 * Directed route handling starts if the initial LID routed part of 692 * a request or the ending LID routed part of a response is empty. 693 * If we are at the start of the LID routed part, don't update the 694 * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec. 695 */ 696 if ((ib_get_smp_direction(smp) ? smp->dr_dlid : smp->dr_slid) == 697 IB_LID_PERMISSIVE && 698 smi_handle_dr_smp_send(smp, device->node_type, port_num) == 699 IB_SMI_DISCARD) { 700 ret = -EINVAL; 701 printk(KERN_ERR PFX "Invalid directed route\n"); 702 goto out; 703 } 704 705 /* Check to post send on QP or process locally */ 706 if (smi_check_local_smp(smp, device) == IB_SMI_DISCARD && 707 smi_check_local_returning_smp(smp, device) == IB_SMI_DISCARD) 708 goto out; 709 710 local = kmalloc(sizeof *local, GFP_ATOMIC); 711 if (!local) { 712 ret = -ENOMEM; 713 printk(KERN_ERR PFX "No memory for ib_mad_local_private\n"); 714 goto out; 715 } 716 local->mad_priv = NULL; 717 local->recv_mad_agent = NULL; 718 mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_ATOMIC); 719 if (!mad_priv) { 720 ret = -ENOMEM; 721 printk(KERN_ERR PFX "No memory for local response MAD\n"); 722 kfree(local); 723 goto out; 724 } 725 726 build_smp_wc(mad_agent_priv->agent.qp, 727 send_wr->wr_id, be16_to_cpu(smp->dr_slid), 728 send_wr->wr.ud.pkey_index, 729 send_wr->wr.ud.port_num, &mad_wc); 730 731 /* No GRH for DR SMP */ 732 ret = device->process_mad(device, 0, port_num, &mad_wc, NULL, 733 (struct ib_mad *)smp, 734 (struct ib_mad *)&mad_priv->mad); 735 switch (ret) 736 { 737 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY: 738 if (ib_response_mad(&mad_priv->mad.mad) && 739 mad_agent_priv->agent.recv_handler) { 740 local->mad_priv = mad_priv; 741 local->recv_mad_agent = mad_agent_priv; 742 /* 743 * Reference MAD agent until receive 744 * side of local completion handled 745 */ 746 atomic_inc(&mad_agent_priv->refcount); 747 } else 748 kmem_cache_free(ib_mad_cache, mad_priv); 749 break; 750 case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED: 751 kmem_cache_free(ib_mad_cache, mad_priv); 752 break; 753 case IB_MAD_RESULT_SUCCESS: 754 /* Treat like an incoming receive MAD */ 755 port_priv = ib_get_mad_port(mad_agent_priv->agent.device, 756 mad_agent_priv->agent.port_num); 757 if (port_priv) { 758 memcpy(&mad_priv->mad.mad, smp, sizeof(struct ib_mad)); 759 recv_mad_agent = find_mad_agent(port_priv, 760 &mad_priv->mad.mad); 761 } 762 if (!port_priv || !recv_mad_agent) { 763 /* 764 * No receiving agent so drop packet and 765 * generate send completion. 766 */ 767 kmem_cache_free(ib_mad_cache, mad_priv); 768 break; 769 } 770 local->mad_priv = mad_priv; 771 local->recv_mad_agent = recv_mad_agent; 772 break; 773 default: 774 kmem_cache_free(ib_mad_cache, mad_priv); 775 kfree(local); 776 ret = -EINVAL; 777 goto out; 778 } 779 780 local->mad_send_wr = mad_send_wr; 781 /* Reference MAD agent until send side of local completion handled */ 782 atomic_inc(&mad_agent_priv->refcount); 783 /* Queue local completion to local list */ 784 spin_lock_irqsave(&mad_agent_priv->lock, flags); 785 list_add_tail(&local->completion_list, &mad_agent_priv->local_list); 786 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 787 queue_work(mad_agent_priv->qp_info->port_priv->wq, 788 &mad_agent_priv->local_work); 789 ret = 1; 790 out: 791 return ret; 792 } 793 794 static int get_pad_size(int hdr_len, int data_len) 795 { 796 int seg_size, pad; 797 798 seg_size = sizeof(struct ib_mad) - hdr_len; 799 if (data_len && seg_size) { 800 pad = seg_size - data_len % seg_size; 801 return pad == seg_size ? 0 : pad; 802 } else 803 return seg_size; 804 } 805 806 static void free_send_rmpp_list(struct ib_mad_send_wr_private *mad_send_wr) 807 { 808 struct ib_rmpp_segment *s, *t; 809 810 list_for_each_entry_safe(s, t, &mad_send_wr->rmpp_list, list) { 811 list_del(&s->list); 812 kfree(s); 813 } 814 } 815 816 static int alloc_send_rmpp_list(struct ib_mad_send_wr_private *send_wr, 817 gfp_t gfp_mask) 818 { 819 struct ib_mad_send_buf *send_buf = &send_wr->send_buf; 820 struct ib_rmpp_mad *rmpp_mad = send_buf->mad; 821 struct ib_rmpp_segment *seg = NULL; 822 int left, seg_size, pad; 823 824 send_buf->seg_size = sizeof (struct ib_mad) - send_buf->hdr_len; 825 seg_size = send_buf->seg_size; 826 pad = send_wr->pad; 827 828 /* Allocate data segments. */ 829 for (left = send_buf->data_len + pad; left > 0; left -= seg_size) { 830 seg = kmalloc(sizeof (*seg) + seg_size, gfp_mask); 831 if (!seg) { 832 printk(KERN_ERR "alloc_send_rmpp_segs: RMPP mem " 833 "alloc failed for len %zd, gfp %#x\n", 834 sizeof (*seg) + seg_size, gfp_mask); 835 free_send_rmpp_list(send_wr); 836 return -ENOMEM; 837 } 838 seg->num = ++send_buf->seg_count; 839 list_add_tail(&seg->list, &send_wr->rmpp_list); 840 } 841 842 /* Zero any padding */ 843 if (pad) 844 memset(seg->data + seg_size - pad, 0, pad); 845 846 rmpp_mad->rmpp_hdr.rmpp_version = send_wr->mad_agent_priv-> 847 agent.rmpp_version; 848 rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_DATA; 849 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE); 850 851 send_wr->cur_seg = container_of(send_wr->rmpp_list.next, 852 struct ib_rmpp_segment, list); 853 send_wr->last_ack_seg = send_wr->cur_seg; 854 return 0; 855 } 856 857 struct ib_mad_send_buf * ib_create_send_mad(struct ib_mad_agent *mad_agent, 858 u32 remote_qpn, u16 pkey_index, 859 int rmpp_active, 860 int hdr_len, int data_len, 861 gfp_t gfp_mask) 862 { 863 struct ib_mad_agent_private *mad_agent_priv; 864 struct ib_mad_send_wr_private *mad_send_wr; 865 int pad, message_size, ret, size; 866 void *buf; 867 868 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private, 869 agent); 870 pad = get_pad_size(hdr_len, data_len); 871 message_size = hdr_len + data_len + pad; 872 873 if ((!mad_agent->rmpp_version && 874 (rmpp_active || message_size > sizeof(struct ib_mad))) || 875 (!rmpp_active && message_size > sizeof(struct ib_mad))) 876 return ERR_PTR(-EINVAL); 877 878 size = rmpp_active ? hdr_len : sizeof(struct ib_mad); 879 buf = kzalloc(sizeof *mad_send_wr + size, gfp_mask); 880 if (!buf) 881 return ERR_PTR(-ENOMEM); 882 883 mad_send_wr = buf + size; 884 INIT_LIST_HEAD(&mad_send_wr->rmpp_list); 885 mad_send_wr->send_buf.mad = buf; 886 mad_send_wr->send_buf.hdr_len = hdr_len; 887 mad_send_wr->send_buf.data_len = data_len; 888 mad_send_wr->pad = pad; 889 890 mad_send_wr->mad_agent_priv = mad_agent_priv; 891 mad_send_wr->sg_list[0].length = hdr_len; 892 mad_send_wr->sg_list[0].lkey = mad_agent->mr->lkey; 893 mad_send_wr->sg_list[1].length = sizeof(struct ib_mad) - hdr_len; 894 mad_send_wr->sg_list[1].lkey = mad_agent->mr->lkey; 895 896 mad_send_wr->send_wr.wr_id = (unsigned long) mad_send_wr; 897 mad_send_wr->send_wr.sg_list = mad_send_wr->sg_list; 898 mad_send_wr->send_wr.num_sge = 2; 899 mad_send_wr->send_wr.opcode = IB_WR_SEND; 900 mad_send_wr->send_wr.send_flags = IB_SEND_SIGNALED; 901 mad_send_wr->send_wr.wr.ud.remote_qpn = remote_qpn; 902 mad_send_wr->send_wr.wr.ud.remote_qkey = IB_QP_SET_QKEY; 903 mad_send_wr->send_wr.wr.ud.pkey_index = pkey_index; 904 905 if (rmpp_active) { 906 ret = alloc_send_rmpp_list(mad_send_wr, gfp_mask); 907 if (ret) { 908 kfree(buf); 909 return ERR_PTR(ret); 910 } 911 } 912 913 mad_send_wr->send_buf.mad_agent = mad_agent; 914 atomic_inc(&mad_agent_priv->refcount); 915 return &mad_send_wr->send_buf; 916 } 917 EXPORT_SYMBOL(ib_create_send_mad); 918 919 int ib_get_mad_data_offset(u8 mgmt_class) 920 { 921 if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM) 922 return IB_MGMT_SA_HDR; 923 else if ((mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) || 924 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) || 925 (mgmt_class == IB_MGMT_CLASS_BIS)) 926 return IB_MGMT_DEVICE_HDR; 927 else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) && 928 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)) 929 return IB_MGMT_VENDOR_HDR; 930 else 931 return IB_MGMT_MAD_HDR; 932 } 933 EXPORT_SYMBOL(ib_get_mad_data_offset); 934 935 int ib_is_mad_class_rmpp(u8 mgmt_class) 936 { 937 if ((mgmt_class == IB_MGMT_CLASS_SUBN_ADM) || 938 (mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) || 939 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) || 940 (mgmt_class == IB_MGMT_CLASS_BIS) || 941 ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) && 942 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))) 943 return 1; 944 return 0; 945 } 946 EXPORT_SYMBOL(ib_is_mad_class_rmpp); 947 948 void *ib_get_rmpp_segment(struct ib_mad_send_buf *send_buf, int seg_num) 949 { 950 struct ib_mad_send_wr_private *mad_send_wr; 951 struct list_head *list; 952 953 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private, 954 send_buf); 955 list = &mad_send_wr->cur_seg->list; 956 957 if (mad_send_wr->cur_seg->num < seg_num) { 958 list_for_each_entry(mad_send_wr->cur_seg, list, list) 959 if (mad_send_wr->cur_seg->num == seg_num) 960 break; 961 } else if (mad_send_wr->cur_seg->num > seg_num) { 962 list_for_each_entry_reverse(mad_send_wr->cur_seg, list, list) 963 if (mad_send_wr->cur_seg->num == seg_num) 964 break; 965 } 966 return mad_send_wr->cur_seg->data; 967 } 968 EXPORT_SYMBOL(ib_get_rmpp_segment); 969 970 static inline void *ib_get_payload(struct ib_mad_send_wr_private *mad_send_wr) 971 { 972 if (mad_send_wr->send_buf.seg_count) 973 return ib_get_rmpp_segment(&mad_send_wr->send_buf, 974 mad_send_wr->seg_num); 975 else 976 return mad_send_wr->send_buf.mad + 977 mad_send_wr->send_buf.hdr_len; 978 } 979 980 void ib_free_send_mad(struct ib_mad_send_buf *send_buf) 981 { 982 struct ib_mad_agent_private *mad_agent_priv; 983 struct ib_mad_send_wr_private *mad_send_wr; 984 985 mad_agent_priv = container_of(send_buf->mad_agent, 986 struct ib_mad_agent_private, agent); 987 mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private, 988 send_buf); 989 990 free_send_rmpp_list(mad_send_wr); 991 kfree(send_buf->mad); 992 deref_mad_agent(mad_agent_priv); 993 } 994 EXPORT_SYMBOL(ib_free_send_mad); 995 996 int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr) 997 { 998 struct ib_mad_qp_info *qp_info; 999 struct list_head *list; 1000 struct ib_send_wr *bad_send_wr; 1001 struct ib_mad_agent *mad_agent; 1002 struct ib_sge *sge; 1003 unsigned long flags; 1004 int ret; 1005 1006 /* Set WR ID to find mad_send_wr upon completion */ 1007 qp_info = mad_send_wr->mad_agent_priv->qp_info; 1008 mad_send_wr->send_wr.wr_id = (unsigned long)&mad_send_wr->mad_list; 1009 mad_send_wr->mad_list.mad_queue = &qp_info->send_queue; 1010 1011 mad_agent = mad_send_wr->send_buf.mad_agent; 1012 sge = mad_send_wr->sg_list; 1013 sge[0].addr = ib_dma_map_single(mad_agent->device, 1014 mad_send_wr->send_buf.mad, 1015 sge[0].length, 1016 DMA_TO_DEVICE); 1017 mad_send_wr->header_mapping = sge[0].addr; 1018 1019 sge[1].addr = ib_dma_map_single(mad_agent->device, 1020 ib_get_payload(mad_send_wr), 1021 sge[1].length, 1022 DMA_TO_DEVICE); 1023 mad_send_wr->payload_mapping = sge[1].addr; 1024 1025 spin_lock_irqsave(&qp_info->send_queue.lock, flags); 1026 if (qp_info->send_queue.count < qp_info->send_queue.max_active) { 1027 ret = ib_post_send(mad_agent->qp, &mad_send_wr->send_wr, 1028 &bad_send_wr); 1029 list = &qp_info->send_queue.list; 1030 } else { 1031 ret = 0; 1032 list = &qp_info->overflow_list; 1033 } 1034 1035 if (!ret) { 1036 qp_info->send_queue.count++; 1037 list_add_tail(&mad_send_wr->mad_list.list, list); 1038 } 1039 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags); 1040 if (ret) { 1041 ib_dma_unmap_single(mad_agent->device, 1042 mad_send_wr->header_mapping, 1043 sge[0].length, DMA_TO_DEVICE); 1044 ib_dma_unmap_single(mad_agent->device, 1045 mad_send_wr->payload_mapping, 1046 sge[1].length, DMA_TO_DEVICE); 1047 } 1048 return ret; 1049 } 1050 1051 /* 1052 * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated 1053 * with the registered client 1054 */ 1055 int ib_post_send_mad(struct ib_mad_send_buf *send_buf, 1056 struct ib_mad_send_buf **bad_send_buf) 1057 { 1058 struct ib_mad_agent_private *mad_agent_priv; 1059 struct ib_mad_send_buf *next_send_buf; 1060 struct ib_mad_send_wr_private *mad_send_wr; 1061 unsigned long flags; 1062 int ret = -EINVAL; 1063 1064 /* Walk list of send WRs and post each on send list */ 1065 for (; send_buf; send_buf = next_send_buf) { 1066 1067 mad_send_wr = container_of(send_buf, 1068 struct ib_mad_send_wr_private, 1069 send_buf); 1070 mad_agent_priv = mad_send_wr->mad_agent_priv; 1071 1072 if (!send_buf->mad_agent->send_handler || 1073 (send_buf->timeout_ms && 1074 !send_buf->mad_agent->recv_handler)) { 1075 ret = -EINVAL; 1076 goto error; 1077 } 1078 1079 if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr *) send_buf->mad)->mgmt_class)) { 1080 if (mad_agent_priv->agent.rmpp_version) { 1081 ret = -EINVAL; 1082 goto error; 1083 } 1084 } 1085 1086 /* 1087 * Save pointer to next work request to post in case the 1088 * current one completes, and the user modifies the work 1089 * request associated with the completion 1090 */ 1091 next_send_buf = send_buf->next; 1092 mad_send_wr->send_wr.wr.ud.ah = send_buf->ah; 1093 1094 if (((struct ib_mad_hdr *) send_buf->mad)->mgmt_class == 1095 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) { 1096 ret = handle_outgoing_dr_smp(mad_agent_priv, 1097 mad_send_wr); 1098 if (ret < 0) /* error */ 1099 goto error; 1100 else if (ret == 1) /* locally consumed */ 1101 continue; 1102 } 1103 1104 mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid; 1105 /* Timeout will be updated after send completes */ 1106 mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms); 1107 mad_send_wr->max_retries = send_buf->retries; 1108 mad_send_wr->retries_left = send_buf->retries; 1109 send_buf->retries = 0; 1110 /* Reference for work request to QP + response */ 1111 mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0); 1112 mad_send_wr->status = IB_WC_SUCCESS; 1113 1114 /* Reference MAD agent until send completes */ 1115 atomic_inc(&mad_agent_priv->refcount); 1116 spin_lock_irqsave(&mad_agent_priv->lock, flags); 1117 list_add_tail(&mad_send_wr->agent_list, 1118 &mad_agent_priv->send_list); 1119 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 1120 1121 if (mad_agent_priv->agent.rmpp_version) { 1122 ret = ib_send_rmpp_mad(mad_send_wr); 1123 if (ret >= 0 && ret != IB_RMPP_RESULT_CONSUMED) 1124 ret = ib_send_mad(mad_send_wr); 1125 } else 1126 ret = ib_send_mad(mad_send_wr); 1127 if (ret < 0) { 1128 /* Fail send request */ 1129 spin_lock_irqsave(&mad_agent_priv->lock, flags); 1130 list_del(&mad_send_wr->agent_list); 1131 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 1132 atomic_dec(&mad_agent_priv->refcount); 1133 goto error; 1134 } 1135 } 1136 return 0; 1137 error: 1138 if (bad_send_buf) 1139 *bad_send_buf = send_buf; 1140 return ret; 1141 } 1142 EXPORT_SYMBOL(ib_post_send_mad); 1143 1144 /* 1145 * ib_free_recv_mad - Returns data buffers used to receive 1146 * a MAD to the access layer 1147 */ 1148 void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc) 1149 { 1150 struct ib_mad_recv_buf *mad_recv_buf, *temp_recv_buf; 1151 struct ib_mad_private_header *mad_priv_hdr; 1152 struct ib_mad_private *priv; 1153 struct list_head free_list; 1154 1155 INIT_LIST_HEAD(&free_list); 1156 list_splice_init(&mad_recv_wc->rmpp_list, &free_list); 1157 1158 list_for_each_entry_safe(mad_recv_buf, temp_recv_buf, 1159 &free_list, list) { 1160 mad_recv_wc = container_of(mad_recv_buf, struct ib_mad_recv_wc, 1161 recv_buf); 1162 mad_priv_hdr = container_of(mad_recv_wc, 1163 struct ib_mad_private_header, 1164 recv_wc); 1165 priv = container_of(mad_priv_hdr, struct ib_mad_private, 1166 header); 1167 kmem_cache_free(ib_mad_cache, priv); 1168 } 1169 } 1170 EXPORT_SYMBOL(ib_free_recv_mad); 1171 1172 struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp, 1173 u8 rmpp_version, 1174 ib_mad_send_handler send_handler, 1175 ib_mad_recv_handler recv_handler, 1176 void *context) 1177 { 1178 return ERR_PTR(-EINVAL); /* XXX: for now */ 1179 } 1180 EXPORT_SYMBOL(ib_redirect_mad_qp); 1181 1182 int ib_process_mad_wc(struct ib_mad_agent *mad_agent, 1183 struct ib_wc *wc) 1184 { 1185 printk(KERN_ERR PFX "ib_process_mad_wc() not implemented yet\n"); 1186 return 0; 1187 } 1188 EXPORT_SYMBOL(ib_process_mad_wc); 1189 1190 static int method_in_use(struct ib_mad_mgmt_method_table **method, 1191 struct ib_mad_reg_req *mad_reg_req) 1192 { 1193 int i; 1194 1195 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS) { 1196 if ((*method)->agent[i]) { 1197 printk(KERN_ERR PFX "Method %d already in use\n", i); 1198 return -EINVAL; 1199 } 1200 } 1201 return 0; 1202 } 1203 1204 static int allocate_method_table(struct ib_mad_mgmt_method_table **method) 1205 { 1206 /* Allocate management method table */ 1207 *method = kzalloc(sizeof **method, GFP_ATOMIC); 1208 if (!*method) { 1209 printk(KERN_ERR PFX "No memory for " 1210 "ib_mad_mgmt_method_table\n"); 1211 return -ENOMEM; 1212 } 1213 1214 return 0; 1215 } 1216 1217 /* 1218 * Check to see if there are any methods still in use 1219 */ 1220 static int check_method_table(struct ib_mad_mgmt_method_table *method) 1221 { 1222 int i; 1223 1224 for (i = 0; i < IB_MGMT_MAX_METHODS; i++) 1225 if (method->agent[i]) 1226 return 1; 1227 return 0; 1228 } 1229 1230 /* 1231 * Check to see if there are any method tables for this class still in use 1232 */ 1233 static int check_class_table(struct ib_mad_mgmt_class_table *class) 1234 { 1235 int i; 1236 1237 for (i = 0; i < MAX_MGMT_CLASS; i++) 1238 if (class->method_table[i]) 1239 return 1; 1240 return 0; 1241 } 1242 1243 static int check_vendor_class(struct ib_mad_mgmt_vendor_class *vendor_class) 1244 { 1245 int i; 1246 1247 for (i = 0; i < MAX_MGMT_OUI; i++) 1248 if (vendor_class->method_table[i]) 1249 return 1; 1250 return 0; 1251 } 1252 1253 static int find_vendor_oui(struct ib_mad_mgmt_vendor_class *vendor_class, 1254 char *oui) 1255 { 1256 int i; 1257 1258 for (i = 0; i < MAX_MGMT_OUI; i++) 1259 /* Is there matching OUI for this vendor class ? */ 1260 if (!memcmp(vendor_class->oui[i], oui, 3)) 1261 return i; 1262 1263 return -1; 1264 } 1265 1266 static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table *vendor) 1267 { 1268 int i; 1269 1270 for (i = 0; i < MAX_MGMT_VENDOR_RANGE2; i++) 1271 if (vendor->vendor_class[i]) 1272 return 1; 1273 1274 return 0; 1275 } 1276 1277 static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table *method, 1278 struct ib_mad_agent_private *agent) 1279 { 1280 int i; 1281 1282 /* Remove any methods for this mad agent */ 1283 for (i = 0; i < IB_MGMT_MAX_METHODS; i++) { 1284 if (method->agent[i] == agent) { 1285 method->agent[i] = NULL; 1286 } 1287 } 1288 } 1289 1290 static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req, 1291 struct ib_mad_agent_private *agent_priv, 1292 u8 mgmt_class) 1293 { 1294 struct ib_mad_port_private *port_priv; 1295 struct ib_mad_mgmt_class_table **class; 1296 struct ib_mad_mgmt_method_table **method; 1297 int i, ret; 1298 1299 port_priv = agent_priv->qp_info->port_priv; 1300 class = &port_priv->version[mad_reg_req->mgmt_class_version].class; 1301 if (!*class) { 1302 /* Allocate management class table for "new" class version */ 1303 *class = kzalloc(sizeof **class, GFP_ATOMIC); 1304 if (!*class) { 1305 printk(KERN_ERR PFX "No memory for " 1306 "ib_mad_mgmt_class_table\n"); 1307 ret = -ENOMEM; 1308 goto error1; 1309 } 1310 1311 /* Allocate method table for this management class */ 1312 method = &(*class)->method_table[mgmt_class]; 1313 if ((ret = allocate_method_table(method))) 1314 goto error2; 1315 } else { 1316 method = &(*class)->method_table[mgmt_class]; 1317 if (!*method) { 1318 /* Allocate method table for this management class */ 1319 if ((ret = allocate_method_table(method))) 1320 goto error1; 1321 } 1322 } 1323 1324 /* Now, make sure methods are not already in use */ 1325 if (method_in_use(method, mad_reg_req)) 1326 goto error3; 1327 1328 /* Finally, add in methods being registered */ 1329 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS) 1330 (*method)->agent[i] = agent_priv; 1331 1332 return 0; 1333 1334 error3: 1335 /* Remove any methods for this mad agent */ 1336 remove_methods_mad_agent(*method, agent_priv); 1337 /* Now, check to see if there are any methods in use */ 1338 if (!check_method_table(*method)) { 1339 /* If not, release management method table */ 1340 kfree(*method); 1341 *method = NULL; 1342 } 1343 ret = -EINVAL; 1344 goto error1; 1345 error2: 1346 kfree(*class); 1347 *class = NULL; 1348 error1: 1349 return ret; 1350 } 1351 1352 static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req, 1353 struct ib_mad_agent_private *agent_priv) 1354 { 1355 struct ib_mad_port_private *port_priv; 1356 struct ib_mad_mgmt_vendor_class_table **vendor_table; 1357 struct ib_mad_mgmt_vendor_class_table *vendor = NULL; 1358 struct ib_mad_mgmt_vendor_class *vendor_class = NULL; 1359 struct ib_mad_mgmt_method_table **method; 1360 int i, ret = -ENOMEM; 1361 u8 vclass; 1362 1363 /* "New" vendor (with OUI) class */ 1364 vclass = vendor_class_index(mad_reg_req->mgmt_class); 1365 port_priv = agent_priv->qp_info->port_priv; 1366 vendor_table = &port_priv->version[ 1367 mad_reg_req->mgmt_class_version].vendor; 1368 if (!*vendor_table) { 1369 /* Allocate mgmt vendor class table for "new" class version */ 1370 vendor = kzalloc(sizeof *vendor, GFP_ATOMIC); 1371 if (!vendor) { 1372 printk(KERN_ERR PFX "No memory for " 1373 "ib_mad_mgmt_vendor_class_table\n"); 1374 goto error1; 1375 } 1376 1377 *vendor_table = vendor; 1378 } 1379 if (!(*vendor_table)->vendor_class[vclass]) { 1380 /* Allocate table for this management vendor class */ 1381 vendor_class = kzalloc(sizeof *vendor_class, GFP_ATOMIC); 1382 if (!vendor_class) { 1383 printk(KERN_ERR PFX "No memory for " 1384 "ib_mad_mgmt_vendor_class\n"); 1385 goto error2; 1386 } 1387 1388 (*vendor_table)->vendor_class[vclass] = vendor_class; 1389 } 1390 for (i = 0; i < MAX_MGMT_OUI; i++) { 1391 /* Is there matching OUI for this vendor class ? */ 1392 if (!memcmp((*vendor_table)->vendor_class[vclass]->oui[i], 1393 mad_reg_req->oui, 3)) { 1394 method = &(*vendor_table)->vendor_class[ 1395 vclass]->method_table[i]; 1396 BUG_ON(!*method); 1397 goto check_in_use; 1398 } 1399 } 1400 for (i = 0; i < MAX_MGMT_OUI; i++) { 1401 /* OUI slot available ? */ 1402 if (!is_vendor_oui((*vendor_table)->vendor_class[ 1403 vclass]->oui[i])) { 1404 method = &(*vendor_table)->vendor_class[ 1405 vclass]->method_table[i]; 1406 BUG_ON(*method); 1407 /* Allocate method table for this OUI */ 1408 if ((ret = allocate_method_table(method))) 1409 goto error3; 1410 memcpy((*vendor_table)->vendor_class[vclass]->oui[i], 1411 mad_reg_req->oui, 3); 1412 goto check_in_use; 1413 } 1414 } 1415 printk(KERN_ERR PFX "All OUI slots in use\n"); 1416 goto error3; 1417 1418 check_in_use: 1419 /* Now, make sure methods are not already in use */ 1420 if (method_in_use(method, mad_reg_req)) 1421 goto error4; 1422 1423 /* Finally, add in methods being registered */ 1424 for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS) 1425 (*method)->agent[i] = agent_priv; 1426 1427 return 0; 1428 1429 error4: 1430 /* Remove any methods for this mad agent */ 1431 remove_methods_mad_agent(*method, agent_priv); 1432 /* Now, check to see if there are any methods in use */ 1433 if (!check_method_table(*method)) { 1434 /* If not, release management method table */ 1435 kfree(*method); 1436 *method = NULL; 1437 } 1438 ret = -EINVAL; 1439 error3: 1440 if (vendor_class) { 1441 (*vendor_table)->vendor_class[vclass] = NULL; 1442 kfree(vendor_class); 1443 } 1444 error2: 1445 if (vendor) { 1446 *vendor_table = NULL; 1447 kfree(vendor); 1448 } 1449 error1: 1450 return ret; 1451 } 1452 1453 static void remove_mad_reg_req(struct ib_mad_agent_private *agent_priv) 1454 { 1455 struct ib_mad_port_private *port_priv; 1456 struct ib_mad_mgmt_class_table *class; 1457 struct ib_mad_mgmt_method_table *method; 1458 struct ib_mad_mgmt_vendor_class_table *vendor; 1459 struct ib_mad_mgmt_vendor_class *vendor_class; 1460 int index; 1461 u8 mgmt_class; 1462 1463 /* 1464 * Was MAD registration request supplied 1465 * with original registration ? 1466 */ 1467 if (!agent_priv->reg_req) { 1468 goto out; 1469 } 1470 1471 port_priv = agent_priv->qp_info->port_priv; 1472 mgmt_class = convert_mgmt_class(agent_priv->reg_req->mgmt_class); 1473 class = port_priv->version[ 1474 agent_priv->reg_req->mgmt_class_version].class; 1475 if (!class) 1476 goto vendor_check; 1477 1478 method = class->method_table[mgmt_class]; 1479 if (method) { 1480 /* Remove any methods for this mad agent */ 1481 remove_methods_mad_agent(method, agent_priv); 1482 /* Now, check to see if there are any methods still in use */ 1483 if (!check_method_table(method)) { 1484 /* If not, release management method table */ 1485 kfree(method); 1486 class->method_table[mgmt_class] = NULL; 1487 /* Any management classes left ? */ 1488 if (!check_class_table(class)) { 1489 /* If not, release management class table */ 1490 kfree(class); 1491 port_priv->version[ 1492 agent_priv->reg_req-> 1493 mgmt_class_version].class = NULL; 1494 } 1495 } 1496 } 1497 1498 vendor_check: 1499 if (!is_vendor_class(mgmt_class)) 1500 goto out; 1501 1502 /* normalize mgmt_class to vendor range 2 */ 1503 mgmt_class = vendor_class_index(agent_priv->reg_req->mgmt_class); 1504 vendor = port_priv->version[ 1505 agent_priv->reg_req->mgmt_class_version].vendor; 1506 1507 if (!vendor) 1508 goto out; 1509 1510 vendor_class = vendor->vendor_class[mgmt_class]; 1511 if (vendor_class) { 1512 index = find_vendor_oui(vendor_class, agent_priv->reg_req->oui); 1513 if (index < 0) 1514 goto out; 1515 method = vendor_class->method_table[index]; 1516 if (method) { 1517 /* Remove any methods for this mad agent */ 1518 remove_methods_mad_agent(method, agent_priv); 1519 /* 1520 * Now, check to see if there are 1521 * any methods still in use 1522 */ 1523 if (!check_method_table(method)) { 1524 /* If not, release management method table */ 1525 kfree(method); 1526 vendor_class->method_table[index] = NULL; 1527 memset(vendor_class->oui[index], 0, 3); 1528 /* Any OUIs left ? */ 1529 if (!check_vendor_class(vendor_class)) { 1530 /* If not, release vendor class table */ 1531 kfree(vendor_class); 1532 vendor->vendor_class[mgmt_class] = NULL; 1533 /* Any other vendor classes left ? */ 1534 if (!check_vendor_table(vendor)) { 1535 kfree(vendor); 1536 port_priv->version[ 1537 agent_priv->reg_req-> 1538 mgmt_class_version]. 1539 vendor = NULL; 1540 } 1541 } 1542 } 1543 } 1544 } 1545 1546 out: 1547 return; 1548 } 1549 1550 static struct ib_mad_agent_private * 1551 find_mad_agent(struct ib_mad_port_private *port_priv, 1552 struct ib_mad *mad) 1553 { 1554 struct ib_mad_agent_private *mad_agent = NULL; 1555 unsigned long flags; 1556 1557 spin_lock_irqsave(&port_priv->reg_lock, flags); 1558 if (ib_response_mad(mad)) { 1559 u32 hi_tid; 1560 struct ib_mad_agent_private *entry; 1561 1562 /* 1563 * Routing is based on high 32 bits of transaction ID 1564 * of MAD. 1565 */ 1566 hi_tid = be64_to_cpu(mad->mad_hdr.tid) >> 32; 1567 list_for_each_entry(entry, &port_priv->agent_list, agent_list) { 1568 if (entry->agent.hi_tid == hi_tid) { 1569 mad_agent = entry; 1570 break; 1571 } 1572 } 1573 } else { 1574 struct ib_mad_mgmt_class_table *class; 1575 struct ib_mad_mgmt_method_table *method; 1576 struct ib_mad_mgmt_vendor_class_table *vendor; 1577 struct ib_mad_mgmt_vendor_class *vendor_class; 1578 struct ib_vendor_mad *vendor_mad; 1579 int index; 1580 1581 /* 1582 * Routing is based on version, class, and method 1583 * For "newer" vendor MADs, also based on OUI 1584 */ 1585 if (mad->mad_hdr.class_version >= MAX_MGMT_VERSION) 1586 goto out; 1587 if (!is_vendor_class(mad->mad_hdr.mgmt_class)) { 1588 class = port_priv->version[ 1589 mad->mad_hdr.class_version].class; 1590 if (!class) 1591 goto out; 1592 method = class->method_table[convert_mgmt_class( 1593 mad->mad_hdr.mgmt_class)]; 1594 if (method) 1595 mad_agent = method->agent[mad->mad_hdr.method & 1596 ~IB_MGMT_METHOD_RESP]; 1597 } else { 1598 vendor = port_priv->version[ 1599 mad->mad_hdr.class_version].vendor; 1600 if (!vendor) 1601 goto out; 1602 vendor_class = vendor->vendor_class[vendor_class_index( 1603 mad->mad_hdr.mgmt_class)]; 1604 if (!vendor_class) 1605 goto out; 1606 /* Find matching OUI */ 1607 vendor_mad = (struct ib_vendor_mad *)mad; 1608 index = find_vendor_oui(vendor_class, vendor_mad->oui); 1609 if (index == -1) 1610 goto out; 1611 method = vendor_class->method_table[index]; 1612 if (method) { 1613 mad_agent = method->agent[mad->mad_hdr.method & 1614 ~IB_MGMT_METHOD_RESP]; 1615 } 1616 } 1617 } 1618 1619 if (mad_agent) { 1620 if (mad_agent->agent.recv_handler) 1621 atomic_inc(&mad_agent->refcount); 1622 else { 1623 printk(KERN_NOTICE PFX "No receive handler for client " 1624 "%p on port %d\n", 1625 &mad_agent->agent, port_priv->port_num); 1626 mad_agent = NULL; 1627 } 1628 } 1629 out: 1630 spin_unlock_irqrestore(&port_priv->reg_lock, flags); 1631 1632 return mad_agent; 1633 } 1634 1635 static int validate_mad(struct ib_mad *mad, u32 qp_num) 1636 { 1637 int valid = 0; 1638 1639 /* Make sure MAD base version is understood */ 1640 if (mad->mad_hdr.base_version != IB_MGMT_BASE_VERSION) { 1641 printk(KERN_ERR PFX "MAD received with unsupported base " 1642 "version %d\n", mad->mad_hdr.base_version); 1643 goto out; 1644 } 1645 1646 /* Filter SMI packets sent to other than QP0 */ 1647 if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED) || 1648 (mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) { 1649 if (qp_num == 0) 1650 valid = 1; 1651 } else { 1652 /* Filter GSI packets sent to QP0 */ 1653 if (qp_num != 0) 1654 valid = 1; 1655 } 1656 1657 out: 1658 return valid; 1659 } 1660 1661 static int is_data_mad(struct ib_mad_agent_private *mad_agent_priv, 1662 struct ib_mad_hdr *mad_hdr) 1663 { 1664 struct ib_rmpp_mad *rmpp_mad; 1665 1666 rmpp_mad = (struct ib_rmpp_mad *)mad_hdr; 1667 return !mad_agent_priv->agent.rmpp_version || 1668 !(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & 1669 IB_MGMT_RMPP_FLAG_ACTIVE) || 1670 (rmpp_mad->rmpp_hdr.rmpp_type == IB_MGMT_RMPP_TYPE_DATA); 1671 } 1672 1673 static inline int rcv_has_same_class(struct ib_mad_send_wr_private *wr, 1674 struct ib_mad_recv_wc *rwc) 1675 { 1676 return ((struct ib_mad *)(wr->send_buf.mad))->mad_hdr.mgmt_class == 1677 rwc->recv_buf.mad->mad_hdr.mgmt_class; 1678 } 1679 1680 static inline int rcv_has_same_gid(struct ib_mad_agent_private *mad_agent_priv, 1681 struct ib_mad_send_wr_private *wr, 1682 struct ib_mad_recv_wc *rwc ) 1683 { 1684 struct ib_ah_attr attr; 1685 u8 send_resp, rcv_resp; 1686 union ib_gid sgid; 1687 struct ib_device *device = mad_agent_priv->agent.device; 1688 u8 port_num = mad_agent_priv->agent.port_num; 1689 u8 lmc; 1690 1691 send_resp = ib_response_mad((struct ib_mad *)wr->send_buf.mad); 1692 rcv_resp = ib_response_mad(rwc->recv_buf.mad); 1693 1694 if (send_resp == rcv_resp) 1695 /* both requests, or both responses. GIDs different */ 1696 return 0; 1697 1698 if (ib_query_ah(wr->send_buf.ah, &attr)) 1699 /* Assume not equal, to avoid false positives. */ 1700 return 0; 1701 1702 if (!!(attr.ah_flags & IB_AH_GRH) != 1703 !!(rwc->wc->wc_flags & IB_WC_GRH)) 1704 /* one has GID, other does not. Assume different */ 1705 return 0; 1706 1707 if (!send_resp && rcv_resp) { 1708 /* is request/response. */ 1709 if (!(attr.ah_flags & IB_AH_GRH)) { 1710 if (ib_get_cached_lmc(device, port_num, &lmc)) 1711 return 0; 1712 return (!lmc || !((attr.src_path_bits ^ 1713 rwc->wc->dlid_path_bits) & 1714 ((1 << lmc) - 1))); 1715 } else { 1716 if (ib_get_cached_gid(device, port_num, 1717 attr.grh.sgid_index, &sgid)) 1718 return 0; 1719 return !memcmp(sgid.raw, rwc->recv_buf.grh->dgid.raw, 1720 16); 1721 } 1722 } 1723 1724 if (!(attr.ah_flags & IB_AH_GRH)) 1725 return attr.dlid == rwc->wc->slid; 1726 else 1727 return !memcmp(attr.grh.dgid.raw, rwc->recv_buf.grh->sgid.raw, 1728 16); 1729 } 1730 1731 static inline int is_direct(u8 class) 1732 { 1733 return (class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE); 1734 } 1735 1736 struct ib_mad_send_wr_private* 1737 ib_find_send_mad(struct ib_mad_agent_private *mad_agent_priv, 1738 struct ib_mad_recv_wc *wc) 1739 { 1740 struct ib_mad_send_wr_private *wr; 1741 struct ib_mad *mad; 1742 1743 mad = (struct ib_mad *)wc->recv_buf.mad; 1744 1745 list_for_each_entry(wr, &mad_agent_priv->wait_list, agent_list) { 1746 if ((wr->tid == mad->mad_hdr.tid) && 1747 rcv_has_same_class(wr, wc) && 1748 /* 1749 * Don't check GID for direct routed MADs. 1750 * These might have permissive LIDs. 1751 */ 1752 (is_direct(wc->recv_buf.mad->mad_hdr.mgmt_class) || 1753 rcv_has_same_gid(mad_agent_priv, wr, wc))) 1754 return (wr->status == IB_WC_SUCCESS) ? wr : NULL; 1755 } 1756 1757 /* 1758 * It's possible to receive the response before we've 1759 * been notified that the send has completed 1760 */ 1761 list_for_each_entry(wr, &mad_agent_priv->send_list, agent_list) { 1762 if (is_data_mad(mad_agent_priv, wr->send_buf.mad) && 1763 wr->tid == mad->mad_hdr.tid && 1764 wr->timeout && 1765 rcv_has_same_class(wr, wc) && 1766 /* 1767 * Don't check GID for direct routed MADs. 1768 * These might have permissive LIDs. 1769 */ 1770 (is_direct(wc->recv_buf.mad->mad_hdr.mgmt_class) || 1771 rcv_has_same_gid(mad_agent_priv, wr, wc))) 1772 /* Verify request has not been canceled */ 1773 return (wr->status == IB_WC_SUCCESS) ? wr : NULL; 1774 } 1775 return NULL; 1776 } 1777 1778 void ib_mark_mad_done(struct ib_mad_send_wr_private *mad_send_wr) 1779 { 1780 mad_send_wr->timeout = 0; 1781 if (mad_send_wr->refcount == 1) 1782 list_move_tail(&mad_send_wr->agent_list, 1783 &mad_send_wr->mad_agent_priv->done_list); 1784 } 1785 1786 static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv, 1787 struct ib_mad_recv_wc *mad_recv_wc) 1788 { 1789 struct ib_mad_send_wr_private *mad_send_wr; 1790 struct ib_mad_send_wc mad_send_wc; 1791 unsigned long flags; 1792 1793 INIT_LIST_HEAD(&mad_recv_wc->rmpp_list); 1794 list_add(&mad_recv_wc->recv_buf.list, &mad_recv_wc->rmpp_list); 1795 if (mad_agent_priv->agent.rmpp_version) { 1796 mad_recv_wc = ib_process_rmpp_recv_wc(mad_agent_priv, 1797 mad_recv_wc); 1798 if (!mad_recv_wc) { 1799 deref_mad_agent(mad_agent_priv); 1800 return; 1801 } 1802 } 1803 1804 /* Complete corresponding request */ 1805 if (ib_response_mad(mad_recv_wc->recv_buf.mad)) { 1806 spin_lock_irqsave(&mad_agent_priv->lock, flags); 1807 mad_send_wr = ib_find_send_mad(mad_agent_priv, mad_recv_wc); 1808 if (!mad_send_wr) { 1809 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 1810 ib_free_recv_mad(mad_recv_wc); 1811 deref_mad_agent(mad_agent_priv); 1812 return; 1813 } 1814 ib_mark_mad_done(mad_send_wr); 1815 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 1816 1817 /* Defined behavior is to complete response before request */ 1818 mad_recv_wc->wc->wr_id = (unsigned long) &mad_send_wr->send_buf; 1819 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent, 1820 mad_recv_wc); 1821 atomic_dec(&mad_agent_priv->refcount); 1822 1823 mad_send_wc.status = IB_WC_SUCCESS; 1824 mad_send_wc.vendor_err = 0; 1825 mad_send_wc.send_buf = &mad_send_wr->send_buf; 1826 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc); 1827 } else { 1828 mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent, 1829 mad_recv_wc); 1830 deref_mad_agent(mad_agent_priv); 1831 } 1832 } 1833 1834 static void ib_mad_recv_done_handler(struct ib_mad_port_private *port_priv, 1835 struct ib_wc *wc) 1836 { 1837 struct ib_mad_qp_info *qp_info; 1838 struct ib_mad_private_header *mad_priv_hdr; 1839 struct ib_mad_private *recv, *response = NULL; 1840 struct ib_mad_list_head *mad_list; 1841 struct ib_mad_agent_private *mad_agent; 1842 int port_num; 1843 1844 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id; 1845 qp_info = mad_list->mad_queue->qp_info; 1846 dequeue_mad(mad_list); 1847 1848 mad_priv_hdr = container_of(mad_list, struct ib_mad_private_header, 1849 mad_list); 1850 recv = container_of(mad_priv_hdr, struct ib_mad_private, header); 1851 ib_dma_unmap_single(port_priv->device, 1852 recv->header.mapping, 1853 sizeof(struct ib_mad_private) - 1854 sizeof(struct ib_mad_private_header), 1855 DMA_FROM_DEVICE); 1856 1857 /* Setup MAD receive work completion from "normal" work completion */ 1858 recv->header.wc = *wc; 1859 recv->header.recv_wc.wc = &recv->header.wc; 1860 recv->header.recv_wc.mad_len = sizeof(struct ib_mad); 1861 recv->header.recv_wc.recv_buf.mad = &recv->mad.mad; 1862 recv->header.recv_wc.recv_buf.grh = &recv->grh; 1863 1864 if (atomic_read(&qp_info->snoop_count)) 1865 snoop_recv(qp_info, &recv->header.recv_wc, IB_MAD_SNOOP_RECVS); 1866 1867 /* Validate MAD */ 1868 if (!validate_mad(&recv->mad.mad, qp_info->qp->qp_num)) 1869 goto out; 1870 1871 response = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL); 1872 if (!response) { 1873 printk(KERN_ERR PFX "ib_mad_recv_done_handler no memory " 1874 "for response buffer\n"); 1875 goto out; 1876 } 1877 1878 if (port_priv->device->node_type == RDMA_NODE_IB_SWITCH) 1879 port_num = wc->port_num; 1880 else 1881 port_num = port_priv->port_num; 1882 1883 if (recv->mad.mad.mad_hdr.mgmt_class == 1884 IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) { 1885 enum smi_forward_action retsmi; 1886 1887 if (smi_handle_dr_smp_recv(&recv->mad.smp, 1888 port_priv->device->node_type, 1889 port_num, 1890 port_priv->device->phys_port_cnt) == 1891 IB_SMI_DISCARD) 1892 goto out; 1893 1894 retsmi = smi_check_forward_dr_smp(&recv->mad.smp); 1895 if (retsmi == IB_SMI_LOCAL) 1896 goto local; 1897 1898 if (retsmi == IB_SMI_SEND) { /* don't forward */ 1899 if (smi_handle_dr_smp_send(&recv->mad.smp, 1900 port_priv->device->node_type, 1901 port_num) == IB_SMI_DISCARD) 1902 goto out; 1903 1904 if (smi_check_local_smp(&recv->mad.smp, port_priv->device) == IB_SMI_DISCARD) 1905 goto out; 1906 } else if (port_priv->device->node_type == RDMA_NODE_IB_SWITCH) { 1907 /* forward case for switches */ 1908 memcpy(response, recv, sizeof(*response)); 1909 response->header.recv_wc.wc = &response->header.wc; 1910 response->header.recv_wc.recv_buf.mad = &response->mad.mad; 1911 response->header.recv_wc.recv_buf.grh = &response->grh; 1912 1913 agent_send_response(&response->mad.mad, 1914 &response->grh, wc, 1915 port_priv->device, 1916 smi_get_fwd_port(&recv->mad.smp), 1917 qp_info->qp->qp_num); 1918 1919 goto out; 1920 } 1921 } 1922 1923 local: 1924 /* Give driver "right of first refusal" on incoming MAD */ 1925 if (port_priv->device->process_mad) { 1926 int ret; 1927 1928 ret = port_priv->device->process_mad(port_priv->device, 0, 1929 port_priv->port_num, 1930 wc, &recv->grh, 1931 &recv->mad.mad, 1932 &response->mad.mad); 1933 if (ret & IB_MAD_RESULT_SUCCESS) { 1934 if (ret & IB_MAD_RESULT_CONSUMED) 1935 goto out; 1936 if (ret & IB_MAD_RESULT_REPLY) { 1937 agent_send_response(&response->mad.mad, 1938 &recv->grh, wc, 1939 port_priv->device, 1940 port_num, 1941 qp_info->qp->qp_num); 1942 goto out; 1943 } 1944 } 1945 } 1946 1947 mad_agent = find_mad_agent(port_priv, &recv->mad.mad); 1948 if (mad_agent) { 1949 ib_mad_complete_recv(mad_agent, &recv->header.recv_wc); 1950 /* 1951 * recv is freed up in error cases in ib_mad_complete_recv 1952 * or via recv_handler in ib_mad_complete_recv() 1953 */ 1954 recv = NULL; 1955 } 1956 1957 out: 1958 /* Post another receive request for this QP */ 1959 if (response) { 1960 ib_mad_post_receive_mads(qp_info, response); 1961 if (recv) 1962 kmem_cache_free(ib_mad_cache, recv); 1963 } else 1964 ib_mad_post_receive_mads(qp_info, recv); 1965 } 1966 1967 static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv) 1968 { 1969 struct ib_mad_send_wr_private *mad_send_wr; 1970 unsigned long delay; 1971 1972 if (list_empty(&mad_agent_priv->wait_list)) { 1973 __cancel_delayed_work(&mad_agent_priv->timed_work); 1974 } else { 1975 mad_send_wr = list_entry(mad_agent_priv->wait_list.next, 1976 struct ib_mad_send_wr_private, 1977 agent_list); 1978 1979 if (time_after(mad_agent_priv->timeout, 1980 mad_send_wr->timeout)) { 1981 mad_agent_priv->timeout = mad_send_wr->timeout; 1982 __cancel_delayed_work(&mad_agent_priv->timed_work); 1983 delay = mad_send_wr->timeout - jiffies; 1984 if ((long)delay <= 0) 1985 delay = 1; 1986 queue_delayed_work(mad_agent_priv->qp_info-> 1987 port_priv->wq, 1988 &mad_agent_priv->timed_work, delay); 1989 } 1990 } 1991 } 1992 1993 static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr) 1994 { 1995 struct ib_mad_agent_private *mad_agent_priv; 1996 struct ib_mad_send_wr_private *temp_mad_send_wr; 1997 struct list_head *list_item; 1998 unsigned long delay; 1999 2000 mad_agent_priv = mad_send_wr->mad_agent_priv; 2001 list_del(&mad_send_wr->agent_list); 2002 2003 delay = mad_send_wr->timeout; 2004 mad_send_wr->timeout += jiffies; 2005 2006 if (delay) { 2007 list_for_each_prev(list_item, &mad_agent_priv->wait_list) { 2008 temp_mad_send_wr = list_entry(list_item, 2009 struct ib_mad_send_wr_private, 2010 agent_list); 2011 if (time_after(mad_send_wr->timeout, 2012 temp_mad_send_wr->timeout)) 2013 break; 2014 } 2015 } 2016 else 2017 list_item = &mad_agent_priv->wait_list; 2018 list_add(&mad_send_wr->agent_list, list_item); 2019 2020 /* Reschedule a work item if we have a shorter timeout */ 2021 if (mad_agent_priv->wait_list.next == &mad_send_wr->agent_list) { 2022 __cancel_delayed_work(&mad_agent_priv->timed_work); 2023 queue_delayed_work(mad_agent_priv->qp_info->port_priv->wq, 2024 &mad_agent_priv->timed_work, delay); 2025 } 2026 } 2027 2028 void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr, 2029 int timeout_ms) 2030 { 2031 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms); 2032 wait_for_response(mad_send_wr); 2033 } 2034 2035 /* 2036 * Process a send work completion 2037 */ 2038 void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr, 2039 struct ib_mad_send_wc *mad_send_wc) 2040 { 2041 struct ib_mad_agent_private *mad_agent_priv; 2042 unsigned long flags; 2043 int ret; 2044 2045 mad_agent_priv = mad_send_wr->mad_agent_priv; 2046 spin_lock_irqsave(&mad_agent_priv->lock, flags); 2047 if (mad_agent_priv->agent.rmpp_version) { 2048 ret = ib_process_rmpp_send_wc(mad_send_wr, mad_send_wc); 2049 if (ret == IB_RMPP_RESULT_CONSUMED) 2050 goto done; 2051 } else 2052 ret = IB_RMPP_RESULT_UNHANDLED; 2053 2054 if (mad_send_wc->status != IB_WC_SUCCESS && 2055 mad_send_wr->status == IB_WC_SUCCESS) { 2056 mad_send_wr->status = mad_send_wc->status; 2057 mad_send_wr->refcount -= (mad_send_wr->timeout > 0); 2058 } 2059 2060 if (--mad_send_wr->refcount > 0) { 2061 if (mad_send_wr->refcount == 1 && mad_send_wr->timeout && 2062 mad_send_wr->status == IB_WC_SUCCESS) { 2063 wait_for_response(mad_send_wr); 2064 } 2065 goto done; 2066 } 2067 2068 /* Remove send from MAD agent and notify client of completion */ 2069 list_del(&mad_send_wr->agent_list); 2070 adjust_timeout(mad_agent_priv); 2071 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 2072 2073 if (mad_send_wr->status != IB_WC_SUCCESS ) 2074 mad_send_wc->status = mad_send_wr->status; 2075 if (ret == IB_RMPP_RESULT_INTERNAL) 2076 ib_rmpp_send_handler(mad_send_wc); 2077 else 2078 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent, 2079 mad_send_wc); 2080 2081 /* Release reference on agent taken when sending */ 2082 deref_mad_agent(mad_agent_priv); 2083 return; 2084 done: 2085 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 2086 } 2087 2088 static void ib_mad_send_done_handler(struct ib_mad_port_private *port_priv, 2089 struct ib_wc *wc) 2090 { 2091 struct ib_mad_send_wr_private *mad_send_wr, *queued_send_wr; 2092 struct ib_mad_list_head *mad_list; 2093 struct ib_mad_qp_info *qp_info; 2094 struct ib_mad_queue *send_queue; 2095 struct ib_send_wr *bad_send_wr; 2096 struct ib_mad_send_wc mad_send_wc; 2097 unsigned long flags; 2098 int ret; 2099 2100 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id; 2101 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private, 2102 mad_list); 2103 send_queue = mad_list->mad_queue; 2104 qp_info = send_queue->qp_info; 2105 2106 retry: 2107 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device, 2108 mad_send_wr->header_mapping, 2109 mad_send_wr->sg_list[0].length, DMA_TO_DEVICE); 2110 ib_dma_unmap_single(mad_send_wr->send_buf.mad_agent->device, 2111 mad_send_wr->payload_mapping, 2112 mad_send_wr->sg_list[1].length, DMA_TO_DEVICE); 2113 queued_send_wr = NULL; 2114 spin_lock_irqsave(&send_queue->lock, flags); 2115 list_del(&mad_list->list); 2116 2117 /* Move queued send to the send queue */ 2118 if (send_queue->count-- > send_queue->max_active) { 2119 mad_list = container_of(qp_info->overflow_list.next, 2120 struct ib_mad_list_head, list); 2121 queued_send_wr = container_of(mad_list, 2122 struct ib_mad_send_wr_private, 2123 mad_list); 2124 list_move_tail(&mad_list->list, &send_queue->list); 2125 } 2126 spin_unlock_irqrestore(&send_queue->lock, flags); 2127 2128 mad_send_wc.send_buf = &mad_send_wr->send_buf; 2129 mad_send_wc.status = wc->status; 2130 mad_send_wc.vendor_err = wc->vendor_err; 2131 if (atomic_read(&qp_info->snoop_count)) 2132 snoop_send(qp_info, &mad_send_wr->send_buf, &mad_send_wc, 2133 IB_MAD_SNOOP_SEND_COMPLETIONS); 2134 ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc); 2135 2136 if (queued_send_wr) { 2137 ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr, 2138 &bad_send_wr); 2139 if (ret) { 2140 printk(KERN_ERR PFX "ib_post_send failed: %d\n", ret); 2141 mad_send_wr = queued_send_wr; 2142 wc->status = IB_WC_LOC_QP_OP_ERR; 2143 goto retry; 2144 } 2145 } 2146 } 2147 2148 static void mark_sends_for_retry(struct ib_mad_qp_info *qp_info) 2149 { 2150 struct ib_mad_send_wr_private *mad_send_wr; 2151 struct ib_mad_list_head *mad_list; 2152 unsigned long flags; 2153 2154 spin_lock_irqsave(&qp_info->send_queue.lock, flags); 2155 list_for_each_entry(mad_list, &qp_info->send_queue.list, list) { 2156 mad_send_wr = container_of(mad_list, 2157 struct ib_mad_send_wr_private, 2158 mad_list); 2159 mad_send_wr->retry = 1; 2160 } 2161 spin_unlock_irqrestore(&qp_info->send_queue.lock, flags); 2162 } 2163 2164 static void mad_error_handler(struct ib_mad_port_private *port_priv, 2165 struct ib_wc *wc) 2166 { 2167 struct ib_mad_list_head *mad_list; 2168 struct ib_mad_qp_info *qp_info; 2169 struct ib_mad_send_wr_private *mad_send_wr; 2170 int ret; 2171 2172 /* Determine if failure was a send or receive */ 2173 mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id; 2174 qp_info = mad_list->mad_queue->qp_info; 2175 if (mad_list->mad_queue == &qp_info->recv_queue) 2176 /* 2177 * Receive errors indicate that the QP has entered the error 2178 * state - error handling/shutdown code will cleanup 2179 */ 2180 return; 2181 2182 /* 2183 * Send errors will transition the QP to SQE - move 2184 * QP to RTS and repost flushed work requests 2185 */ 2186 mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private, 2187 mad_list); 2188 if (wc->status == IB_WC_WR_FLUSH_ERR) { 2189 if (mad_send_wr->retry) { 2190 /* Repost send */ 2191 struct ib_send_wr *bad_send_wr; 2192 2193 mad_send_wr->retry = 0; 2194 ret = ib_post_send(qp_info->qp, &mad_send_wr->send_wr, 2195 &bad_send_wr); 2196 if (ret) 2197 ib_mad_send_done_handler(port_priv, wc); 2198 } else 2199 ib_mad_send_done_handler(port_priv, wc); 2200 } else { 2201 struct ib_qp_attr *attr; 2202 2203 /* Transition QP to RTS and fail offending send */ 2204 attr = kmalloc(sizeof *attr, GFP_KERNEL); 2205 if (attr) { 2206 attr->qp_state = IB_QPS_RTS; 2207 attr->cur_qp_state = IB_QPS_SQE; 2208 ret = ib_modify_qp(qp_info->qp, attr, 2209 IB_QP_STATE | IB_QP_CUR_STATE); 2210 kfree(attr); 2211 if (ret) 2212 printk(KERN_ERR PFX "mad_error_handler - " 2213 "ib_modify_qp to RTS : %d\n", ret); 2214 else 2215 mark_sends_for_retry(qp_info); 2216 } 2217 ib_mad_send_done_handler(port_priv, wc); 2218 } 2219 } 2220 2221 /* 2222 * IB MAD completion callback 2223 */ 2224 static void ib_mad_completion_handler(struct work_struct *work) 2225 { 2226 struct ib_mad_port_private *port_priv; 2227 struct ib_wc wc; 2228 2229 port_priv = container_of(work, struct ib_mad_port_private, work); 2230 ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP); 2231 2232 while (ib_poll_cq(port_priv->cq, 1, &wc) == 1) { 2233 if (wc.status == IB_WC_SUCCESS) { 2234 switch (wc.opcode) { 2235 case IB_WC_SEND: 2236 ib_mad_send_done_handler(port_priv, &wc); 2237 break; 2238 case IB_WC_RECV: 2239 ib_mad_recv_done_handler(port_priv, &wc); 2240 break; 2241 default: 2242 BUG_ON(1); 2243 break; 2244 } 2245 } else 2246 mad_error_handler(port_priv, &wc); 2247 } 2248 } 2249 2250 static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv) 2251 { 2252 unsigned long flags; 2253 struct ib_mad_send_wr_private *mad_send_wr, *temp_mad_send_wr; 2254 struct ib_mad_send_wc mad_send_wc; 2255 struct list_head cancel_list; 2256 2257 INIT_LIST_HEAD(&cancel_list); 2258 2259 spin_lock_irqsave(&mad_agent_priv->lock, flags); 2260 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr, 2261 &mad_agent_priv->send_list, agent_list) { 2262 if (mad_send_wr->status == IB_WC_SUCCESS) { 2263 mad_send_wr->status = IB_WC_WR_FLUSH_ERR; 2264 mad_send_wr->refcount -= (mad_send_wr->timeout > 0); 2265 } 2266 } 2267 2268 /* Empty wait list to prevent receives from finding a request */ 2269 list_splice_init(&mad_agent_priv->wait_list, &cancel_list); 2270 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 2271 2272 /* Report all cancelled requests */ 2273 mad_send_wc.status = IB_WC_WR_FLUSH_ERR; 2274 mad_send_wc.vendor_err = 0; 2275 2276 list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr, 2277 &cancel_list, agent_list) { 2278 mad_send_wc.send_buf = &mad_send_wr->send_buf; 2279 list_del(&mad_send_wr->agent_list); 2280 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent, 2281 &mad_send_wc); 2282 atomic_dec(&mad_agent_priv->refcount); 2283 } 2284 } 2285 2286 static struct ib_mad_send_wr_private* 2287 find_send_wr(struct ib_mad_agent_private *mad_agent_priv, 2288 struct ib_mad_send_buf *send_buf) 2289 { 2290 struct ib_mad_send_wr_private *mad_send_wr; 2291 2292 list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list, 2293 agent_list) { 2294 if (&mad_send_wr->send_buf == send_buf) 2295 return mad_send_wr; 2296 } 2297 2298 list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list, 2299 agent_list) { 2300 if (is_data_mad(mad_agent_priv, mad_send_wr->send_buf.mad) && 2301 &mad_send_wr->send_buf == send_buf) 2302 return mad_send_wr; 2303 } 2304 return NULL; 2305 } 2306 2307 int ib_modify_mad(struct ib_mad_agent *mad_agent, 2308 struct ib_mad_send_buf *send_buf, u32 timeout_ms) 2309 { 2310 struct ib_mad_agent_private *mad_agent_priv; 2311 struct ib_mad_send_wr_private *mad_send_wr; 2312 unsigned long flags; 2313 int active; 2314 2315 mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private, 2316 agent); 2317 spin_lock_irqsave(&mad_agent_priv->lock, flags); 2318 mad_send_wr = find_send_wr(mad_agent_priv, send_buf); 2319 if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) { 2320 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 2321 return -EINVAL; 2322 } 2323 2324 active = (!mad_send_wr->timeout || mad_send_wr->refcount > 1); 2325 if (!timeout_ms) { 2326 mad_send_wr->status = IB_WC_WR_FLUSH_ERR; 2327 mad_send_wr->refcount -= (mad_send_wr->timeout > 0); 2328 } 2329 2330 mad_send_wr->send_buf.timeout_ms = timeout_ms; 2331 if (active) 2332 mad_send_wr->timeout = msecs_to_jiffies(timeout_ms); 2333 else 2334 ib_reset_mad_timeout(mad_send_wr, timeout_ms); 2335 2336 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 2337 return 0; 2338 } 2339 EXPORT_SYMBOL(ib_modify_mad); 2340 2341 void ib_cancel_mad(struct ib_mad_agent *mad_agent, 2342 struct ib_mad_send_buf *send_buf) 2343 { 2344 ib_modify_mad(mad_agent, send_buf, 0); 2345 } 2346 EXPORT_SYMBOL(ib_cancel_mad); 2347 2348 static void local_completions(struct work_struct *work) 2349 { 2350 struct ib_mad_agent_private *mad_agent_priv; 2351 struct ib_mad_local_private *local; 2352 struct ib_mad_agent_private *recv_mad_agent; 2353 unsigned long flags; 2354 int free_mad; 2355 struct ib_wc wc; 2356 struct ib_mad_send_wc mad_send_wc; 2357 2358 mad_agent_priv = 2359 container_of(work, struct ib_mad_agent_private, local_work); 2360 2361 spin_lock_irqsave(&mad_agent_priv->lock, flags); 2362 while (!list_empty(&mad_agent_priv->local_list)) { 2363 local = list_entry(mad_agent_priv->local_list.next, 2364 struct ib_mad_local_private, 2365 completion_list); 2366 list_del(&local->completion_list); 2367 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 2368 free_mad = 0; 2369 if (local->mad_priv) { 2370 recv_mad_agent = local->recv_mad_agent; 2371 if (!recv_mad_agent) { 2372 printk(KERN_ERR PFX "No receive MAD agent for local completion\n"); 2373 free_mad = 1; 2374 goto local_send_completion; 2375 } 2376 2377 /* 2378 * Defined behavior is to complete response 2379 * before request 2380 */ 2381 build_smp_wc(recv_mad_agent->agent.qp, 2382 (unsigned long) local->mad_send_wr, 2383 be16_to_cpu(IB_LID_PERMISSIVE), 2384 0, recv_mad_agent->agent.port_num, &wc); 2385 2386 local->mad_priv->header.recv_wc.wc = &wc; 2387 local->mad_priv->header.recv_wc.mad_len = 2388 sizeof(struct ib_mad); 2389 INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.rmpp_list); 2390 list_add(&local->mad_priv->header.recv_wc.recv_buf.list, 2391 &local->mad_priv->header.recv_wc.rmpp_list); 2392 local->mad_priv->header.recv_wc.recv_buf.grh = NULL; 2393 local->mad_priv->header.recv_wc.recv_buf.mad = 2394 &local->mad_priv->mad.mad; 2395 if (atomic_read(&recv_mad_agent->qp_info->snoop_count)) 2396 snoop_recv(recv_mad_agent->qp_info, 2397 &local->mad_priv->header.recv_wc, 2398 IB_MAD_SNOOP_RECVS); 2399 recv_mad_agent->agent.recv_handler( 2400 &recv_mad_agent->agent, 2401 &local->mad_priv->header.recv_wc); 2402 spin_lock_irqsave(&recv_mad_agent->lock, flags); 2403 atomic_dec(&recv_mad_agent->refcount); 2404 spin_unlock_irqrestore(&recv_mad_agent->lock, flags); 2405 } 2406 2407 local_send_completion: 2408 /* Complete send */ 2409 mad_send_wc.status = IB_WC_SUCCESS; 2410 mad_send_wc.vendor_err = 0; 2411 mad_send_wc.send_buf = &local->mad_send_wr->send_buf; 2412 if (atomic_read(&mad_agent_priv->qp_info->snoop_count)) 2413 snoop_send(mad_agent_priv->qp_info, 2414 &local->mad_send_wr->send_buf, 2415 &mad_send_wc, IB_MAD_SNOOP_SEND_COMPLETIONS); 2416 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent, 2417 &mad_send_wc); 2418 2419 spin_lock_irqsave(&mad_agent_priv->lock, flags); 2420 atomic_dec(&mad_agent_priv->refcount); 2421 if (free_mad) 2422 kmem_cache_free(ib_mad_cache, local->mad_priv); 2423 kfree(local); 2424 } 2425 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 2426 } 2427 2428 static int retry_send(struct ib_mad_send_wr_private *mad_send_wr) 2429 { 2430 int ret; 2431 2432 if (!mad_send_wr->retries_left) 2433 return -ETIMEDOUT; 2434 2435 mad_send_wr->retries_left--; 2436 mad_send_wr->send_buf.retries++; 2437 2438 mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms); 2439 2440 if (mad_send_wr->mad_agent_priv->agent.rmpp_version) { 2441 ret = ib_retry_rmpp(mad_send_wr); 2442 switch (ret) { 2443 case IB_RMPP_RESULT_UNHANDLED: 2444 ret = ib_send_mad(mad_send_wr); 2445 break; 2446 case IB_RMPP_RESULT_CONSUMED: 2447 ret = 0; 2448 break; 2449 default: 2450 ret = -ECOMM; 2451 break; 2452 } 2453 } else 2454 ret = ib_send_mad(mad_send_wr); 2455 2456 if (!ret) { 2457 mad_send_wr->refcount++; 2458 list_add_tail(&mad_send_wr->agent_list, 2459 &mad_send_wr->mad_agent_priv->send_list); 2460 } 2461 return ret; 2462 } 2463 2464 static void timeout_sends(struct work_struct *work) 2465 { 2466 struct ib_mad_agent_private *mad_agent_priv; 2467 struct ib_mad_send_wr_private *mad_send_wr; 2468 struct ib_mad_send_wc mad_send_wc; 2469 unsigned long flags, delay; 2470 2471 mad_agent_priv = container_of(work, struct ib_mad_agent_private, 2472 timed_work.work); 2473 mad_send_wc.vendor_err = 0; 2474 2475 spin_lock_irqsave(&mad_agent_priv->lock, flags); 2476 while (!list_empty(&mad_agent_priv->wait_list)) { 2477 mad_send_wr = list_entry(mad_agent_priv->wait_list.next, 2478 struct ib_mad_send_wr_private, 2479 agent_list); 2480 2481 if (time_after(mad_send_wr->timeout, jiffies)) { 2482 delay = mad_send_wr->timeout - jiffies; 2483 if ((long)delay <= 0) 2484 delay = 1; 2485 queue_delayed_work(mad_agent_priv->qp_info-> 2486 port_priv->wq, 2487 &mad_agent_priv->timed_work, delay); 2488 break; 2489 } 2490 2491 list_del(&mad_send_wr->agent_list); 2492 if (mad_send_wr->status == IB_WC_SUCCESS && 2493 !retry_send(mad_send_wr)) 2494 continue; 2495 2496 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 2497 2498 if (mad_send_wr->status == IB_WC_SUCCESS) 2499 mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR; 2500 else 2501 mad_send_wc.status = mad_send_wr->status; 2502 mad_send_wc.send_buf = &mad_send_wr->send_buf; 2503 mad_agent_priv->agent.send_handler(&mad_agent_priv->agent, 2504 &mad_send_wc); 2505 2506 atomic_dec(&mad_agent_priv->refcount); 2507 spin_lock_irqsave(&mad_agent_priv->lock, flags); 2508 } 2509 spin_unlock_irqrestore(&mad_agent_priv->lock, flags); 2510 } 2511 2512 static void ib_mad_thread_completion_handler(struct ib_cq *cq, void *arg) 2513 { 2514 struct ib_mad_port_private *port_priv = cq->cq_context; 2515 unsigned long flags; 2516 2517 spin_lock_irqsave(&ib_mad_port_list_lock, flags); 2518 if (!list_empty(&port_priv->port_list)) 2519 queue_work(port_priv->wq, &port_priv->work); 2520 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags); 2521 } 2522 2523 /* 2524 * Allocate receive MADs and post receive WRs for them 2525 */ 2526 static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info, 2527 struct ib_mad_private *mad) 2528 { 2529 unsigned long flags; 2530 int post, ret; 2531 struct ib_mad_private *mad_priv; 2532 struct ib_sge sg_list; 2533 struct ib_recv_wr recv_wr, *bad_recv_wr; 2534 struct ib_mad_queue *recv_queue = &qp_info->recv_queue; 2535 2536 /* Initialize common scatter list fields */ 2537 sg_list.length = sizeof *mad_priv - sizeof mad_priv->header; 2538 sg_list.lkey = (*qp_info->port_priv->mr).lkey; 2539 2540 /* Initialize common receive WR fields */ 2541 recv_wr.next = NULL; 2542 recv_wr.sg_list = &sg_list; 2543 recv_wr.num_sge = 1; 2544 2545 do { 2546 /* Allocate and map receive buffer */ 2547 if (mad) { 2548 mad_priv = mad; 2549 mad = NULL; 2550 } else { 2551 mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL); 2552 if (!mad_priv) { 2553 printk(KERN_ERR PFX "No memory for receive buffer\n"); 2554 ret = -ENOMEM; 2555 break; 2556 } 2557 } 2558 sg_list.addr = ib_dma_map_single(qp_info->port_priv->device, 2559 &mad_priv->grh, 2560 sizeof *mad_priv - 2561 sizeof mad_priv->header, 2562 DMA_FROM_DEVICE); 2563 mad_priv->header.mapping = sg_list.addr; 2564 recv_wr.wr_id = (unsigned long)&mad_priv->header.mad_list; 2565 mad_priv->header.mad_list.mad_queue = recv_queue; 2566 2567 /* Post receive WR */ 2568 spin_lock_irqsave(&recv_queue->lock, flags); 2569 post = (++recv_queue->count < recv_queue->max_active); 2570 list_add_tail(&mad_priv->header.mad_list.list, &recv_queue->list); 2571 spin_unlock_irqrestore(&recv_queue->lock, flags); 2572 ret = ib_post_recv(qp_info->qp, &recv_wr, &bad_recv_wr); 2573 if (ret) { 2574 spin_lock_irqsave(&recv_queue->lock, flags); 2575 list_del(&mad_priv->header.mad_list.list); 2576 recv_queue->count--; 2577 spin_unlock_irqrestore(&recv_queue->lock, flags); 2578 ib_dma_unmap_single(qp_info->port_priv->device, 2579 mad_priv->header.mapping, 2580 sizeof *mad_priv - 2581 sizeof mad_priv->header, 2582 DMA_FROM_DEVICE); 2583 kmem_cache_free(ib_mad_cache, mad_priv); 2584 printk(KERN_ERR PFX "ib_post_recv failed: %d\n", ret); 2585 break; 2586 } 2587 } while (post); 2588 2589 return ret; 2590 } 2591 2592 /* 2593 * Return all the posted receive MADs 2594 */ 2595 static void cleanup_recv_queue(struct ib_mad_qp_info *qp_info) 2596 { 2597 struct ib_mad_private_header *mad_priv_hdr; 2598 struct ib_mad_private *recv; 2599 struct ib_mad_list_head *mad_list; 2600 2601 if (!qp_info->qp) 2602 return; 2603 2604 while (!list_empty(&qp_info->recv_queue.list)) { 2605 2606 mad_list = list_entry(qp_info->recv_queue.list.next, 2607 struct ib_mad_list_head, list); 2608 mad_priv_hdr = container_of(mad_list, 2609 struct ib_mad_private_header, 2610 mad_list); 2611 recv = container_of(mad_priv_hdr, struct ib_mad_private, 2612 header); 2613 2614 /* Remove from posted receive MAD list */ 2615 list_del(&mad_list->list); 2616 2617 ib_dma_unmap_single(qp_info->port_priv->device, 2618 recv->header.mapping, 2619 sizeof(struct ib_mad_private) - 2620 sizeof(struct ib_mad_private_header), 2621 DMA_FROM_DEVICE); 2622 kmem_cache_free(ib_mad_cache, recv); 2623 } 2624 2625 qp_info->recv_queue.count = 0; 2626 } 2627 2628 /* 2629 * Start the port 2630 */ 2631 static int ib_mad_port_start(struct ib_mad_port_private *port_priv) 2632 { 2633 int ret, i; 2634 struct ib_qp_attr *attr; 2635 struct ib_qp *qp; 2636 2637 attr = kmalloc(sizeof *attr, GFP_KERNEL); 2638 if (!attr) { 2639 printk(KERN_ERR PFX "Couldn't kmalloc ib_qp_attr\n"); 2640 return -ENOMEM; 2641 } 2642 2643 for (i = 0; i < IB_MAD_QPS_CORE; i++) { 2644 qp = port_priv->qp_info[i].qp; 2645 if (!qp) 2646 continue; 2647 2648 /* 2649 * PKey index for QP1 is irrelevant but 2650 * one is needed for the Reset to Init transition 2651 */ 2652 attr->qp_state = IB_QPS_INIT; 2653 attr->pkey_index = 0; 2654 attr->qkey = (qp->qp_num == 0) ? 0 : IB_QP1_QKEY; 2655 ret = ib_modify_qp(qp, attr, IB_QP_STATE | 2656 IB_QP_PKEY_INDEX | IB_QP_QKEY); 2657 if (ret) { 2658 printk(KERN_ERR PFX "Couldn't change QP%d state to " 2659 "INIT: %d\n", i, ret); 2660 goto out; 2661 } 2662 2663 attr->qp_state = IB_QPS_RTR; 2664 ret = ib_modify_qp(qp, attr, IB_QP_STATE); 2665 if (ret) { 2666 printk(KERN_ERR PFX "Couldn't change QP%d state to " 2667 "RTR: %d\n", i, ret); 2668 goto out; 2669 } 2670 2671 attr->qp_state = IB_QPS_RTS; 2672 attr->sq_psn = IB_MAD_SEND_Q_PSN; 2673 ret = ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_SQ_PSN); 2674 if (ret) { 2675 printk(KERN_ERR PFX "Couldn't change QP%d state to " 2676 "RTS: %d\n", i, ret); 2677 goto out; 2678 } 2679 } 2680 2681 ret = ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP); 2682 if (ret) { 2683 printk(KERN_ERR PFX "Failed to request completion " 2684 "notification: %d\n", ret); 2685 goto out; 2686 } 2687 2688 for (i = 0; i < IB_MAD_QPS_CORE; i++) { 2689 if (!port_priv->qp_info[i].qp) 2690 continue; 2691 2692 ret = ib_mad_post_receive_mads(&port_priv->qp_info[i], NULL); 2693 if (ret) { 2694 printk(KERN_ERR PFX "Couldn't post receive WRs\n"); 2695 goto out; 2696 } 2697 } 2698 out: 2699 kfree(attr); 2700 return ret; 2701 } 2702 2703 static void qp_event_handler(struct ib_event *event, void *qp_context) 2704 { 2705 struct ib_mad_qp_info *qp_info = qp_context; 2706 2707 /* It's worse than that! He's dead, Jim! */ 2708 printk(KERN_ERR PFX "Fatal error (%d) on MAD QP (%d)\n", 2709 event->event, qp_info->qp->qp_num); 2710 } 2711 2712 static void init_mad_queue(struct ib_mad_qp_info *qp_info, 2713 struct ib_mad_queue *mad_queue) 2714 { 2715 mad_queue->qp_info = qp_info; 2716 mad_queue->count = 0; 2717 spin_lock_init(&mad_queue->lock); 2718 INIT_LIST_HEAD(&mad_queue->list); 2719 } 2720 2721 static void init_mad_qp(struct ib_mad_port_private *port_priv, 2722 struct ib_mad_qp_info *qp_info) 2723 { 2724 qp_info->port_priv = port_priv; 2725 init_mad_queue(qp_info, &qp_info->send_queue); 2726 init_mad_queue(qp_info, &qp_info->recv_queue); 2727 INIT_LIST_HEAD(&qp_info->overflow_list); 2728 spin_lock_init(&qp_info->snoop_lock); 2729 qp_info->snoop_table = NULL; 2730 qp_info->snoop_table_size = 0; 2731 atomic_set(&qp_info->snoop_count, 0); 2732 } 2733 2734 static int create_mad_qp(struct ib_mad_qp_info *qp_info, 2735 enum ib_qp_type qp_type) 2736 { 2737 struct ib_qp_init_attr qp_init_attr; 2738 int ret; 2739 2740 memset(&qp_init_attr, 0, sizeof qp_init_attr); 2741 qp_init_attr.send_cq = qp_info->port_priv->cq; 2742 qp_init_attr.recv_cq = qp_info->port_priv->cq; 2743 qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR; 2744 qp_init_attr.cap.max_send_wr = mad_sendq_size; 2745 qp_init_attr.cap.max_recv_wr = mad_recvq_size; 2746 qp_init_attr.cap.max_send_sge = IB_MAD_SEND_REQ_MAX_SG; 2747 qp_init_attr.cap.max_recv_sge = IB_MAD_RECV_REQ_MAX_SG; 2748 qp_init_attr.qp_type = qp_type; 2749 qp_init_attr.port_num = qp_info->port_priv->port_num; 2750 qp_init_attr.qp_context = qp_info; 2751 qp_init_attr.event_handler = qp_event_handler; 2752 qp_info->qp = ib_create_qp(qp_info->port_priv->pd, &qp_init_attr); 2753 if (IS_ERR(qp_info->qp)) { 2754 printk(KERN_ERR PFX "Couldn't create ib_mad QP%d\n", 2755 get_spl_qp_index(qp_type)); 2756 ret = PTR_ERR(qp_info->qp); 2757 goto error; 2758 } 2759 /* Use minimum queue sizes unless the CQ is resized */ 2760 qp_info->send_queue.max_active = mad_sendq_size; 2761 qp_info->recv_queue.max_active = mad_recvq_size; 2762 return 0; 2763 2764 error: 2765 return ret; 2766 } 2767 2768 static void destroy_mad_qp(struct ib_mad_qp_info *qp_info) 2769 { 2770 if (!qp_info->qp) 2771 return; 2772 2773 ib_destroy_qp(qp_info->qp); 2774 kfree(qp_info->snoop_table); 2775 } 2776 2777 /* 2778 * Open the port 2779 * Create the QP, PD, MR, and CQ if needed 2780 */ 2781 static int ib_mad_port_open(struct ib_device *device, 2782 int port_num) 2783 { 2784 int ret, cq_size; 2785 struct ib_mad_port_private *port_priv; 2786 unsigned long flags; 2787 char name[sizeof "ib_mad123"]; 2788 int has_smi; 2789 2790 /* Create new device info */ 2791 port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL); 2792 if (!port_priv) { 2793 printk(KERN_ERR PFX "No memory for ib_mad_port_private\n"); 2794 return -ENOMEM; 2795 } 2796 2797 port_priv->device = device; 2798 port_priv->port_num = port_num; 2799 spin_lock_init(&port_priv->reg_lock); 2800 INIT_LIST_HEAD(&port_priv->agent_list); 2801 init_mad_qp(port_priv, &port_priv->qp_info[0]); 2802 init_mad_qp(port_priv, &port_priv->qp_info[1]); 2803 2804 cq_size = mad_sendq_size + mad_recvq_size; 2805 has_smi = rdma_port_get_link_layer(device, port_num) == IB_LINK_LAYER_INFINIBAND; 2806 if (has_smi) 2807 cq_size *= 2; 2808 2809 port_priv->cq = ib_create_cq(port_priv->device, 2810 ib_mad_thread_completion_handler, 2811 NULL, port_priv, cq_size, 0); 2812 if (IS_ERR(port_priv->cq)) { 2813 printk(KERN_ERR PFX "Couldn't create ib_mad CQ\n"); 2814 ret = PTR_ERR(port_priv->cq); 2815 goto error3; 2816 } 2817 2818 port_priv->pd = ib_alloc_pd(device); 2819 if (IS_ERR(port_priv->pd)) { 2820 printk(KERN_ERR PFX "Couldn't create ib_mad PD\n"); 2821 ret = PTR_ERR(port_priv->pd); 2822 goto error4; 2823 } 2824 2825 port_priv->mr = ib_get_dma_mr(port_priv->pd, IB_ACCESS_LOCAL_WRITE); 2826 if (IS_ERR(port_priv->mr)) { 2827 printk(KERN_ERR PFX "Couldn't get ib_mad DMA MR\n"); 2828 ret = PTR_ERR(port_priv->mr); 2829 goto error5; 2830 } 2831 2832 if (has_smi) { 2833 ret = create_mad_qp(&port_priv->qp_info[0], IB_QPT_SMI); 2834 if (ret) 2835 goto error6; 2836 } 2837 ret = create_mad_qp(&port_priv->qp_info[1], IB_QPT_GSI); 2838 if (ret) 2839 goto error7; 2840 2841 snprintf(name, sizeof name, "ib_mad%d", port_num); 2842 port_priv->wq = create_singlethread_workqueue(name); 2843 if (!port_priv->wq) { 2844 ret = -ENOMEM; 2845 goto error8; 2846 } 2847 INIT_WORK(&port_priv->work, ib_mad_completion_handler); 2848 2849 spin_lock_irqsave(&ib_mad_port_list_lock, flags); 2850 list_add_tail(&port_priv->port_list, &ib_mad_port_list); 2851 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags); 2852 2853 ret = ib_mad_port_start(port_priv); 2854 if (ret) { 2855 printk(KERN_ERR PFX "Couldn't start port\n"); 2856 goto error9; 2857 } 2858 2859 return 0; 2860 2861 error9: 2862 spin_lock_irqsave(&ib_mad_port_list_lock, flags); 2863 list_del_init(&port_priv->port_list); 2864 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags); 2865 2866 destroy_workqueue(port_priv->wq); 2867 error8: 2868 destroy_mad_qp(&port_priv->qp_info[1]); 2869 error7: 2870 destroy_mad_qp(&port_priv->qp_info[0]); 2871 error6: 2872 ib_dereg_mr(port_priv->mr); 2873 error5: 2874 ib_dealloc_pd(port_priv->pd); 2875 error4: 2876 ib_destroy_cq(port_priv->cq); 2877 cleanup_recv_queue(&port_priv->qp_info[1]); 2878 cleanup_recv_queue(&port_priv->qp_info[0]); 2879 error3: 2880 kfree(port_priv); 2881 2882 return ret; 2883 } 2884 2885 /* 2886 * Close the port 2887 * If there are no classes using the port, free the port 2888 * resources (CQ, MR, PD, QP) and remove the port's info structure 2889 */ 2890 static int ib_mad_port_close(struct ib_device *device, int port_num) 2891 { 2892 struct ib_mad_port_private *port_priv; 2893 unsigned long flags; 2894 2895 spin_lock_irqsave(&ib_mad_port_list_lock, flags); 2896 port_priv = __ib_get_mad_port(device, port_num); 2897 if (port_priv == NULL) { 2898 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags); 2899 printk(KERN_ERR PFX "Port %d not found\n", port_num); 2900 return -ENODEV; 2901 } 2902 list_del_init(&port_priv->port_list); 2903 spin_unlock_irqrestore(&ib_mad_port_list_lock, flags); 2904 2905 destroy_workqueue(port_priv->wq); 2906 destroy_mad_qp(&port_priv->qp_info[1]); 2907 destroy_mad_qp(&port_priv->qp_info[0]); 2908 ib_dereg_mr(port_priv->mr); 2909 ib_dealloc_pd(port_priv->pd); 2910 ib_destroy_cq(port_priv->cq); 2911 cleanup_recv_queue(&port_priv->qp_info[1]); 2912 cleanup_recv_queue(&port_priv->qp_info[0]); 2913 /* XXX: Handle deallocation of MAD registration tables */ 2914 2915 kfree(port_priv); 2916 2917 return 0; 2918 } 2919 2920 static void ib_mad_init_device(struct ib_device *device) 2921 { 2922 int start, end, i; 2923 2924 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB) 2925 return; 2926 2927 if (device->node_type == RDMA_NODE_IB_SWITCH) { 2928 start = 0; 2929 end = 0; 2930 } else { 2931 start = 1; 2932 end = device->phys_port_cnt; 2933 } 2934 2935 for (i = start; i <= end; i++) { 2936 if (ib_mad_port_open(device, i)) { 2937 printk(KERN_ERR PFX "Couldn't open %s port %d\n", 2938 device->name, i); 2939 goto error; 2940 } 2941 if (ib_agent_port_open(device, i)) { 2942 printk(KERN_ERR PFX "Couldn't open %s port %d " 2943 "for agents\n", 2944 device->name, i); 2945 goto error_agent; 2946 } 2947 } 2948 return; 2949 2950 error_agent: 2951 if (ib_mad_port_close(device, i)) 2952 printk(KERN_ERR PFX "Couldn't close %s port %d\n", 2953 device->name, i); 2954 2955 error: 2956 i--; 2957 2958 while (i >= start) { 2959 if (ib_agent_port_close(device, i)) 2960 printk(KERN_ERR PFX "Couldn't close %s port %d " 2961 "for agents\n", 2962 device->name, i); 2963 if (ib_mad_port_close(device, i)) 2964 printk(KERN_ERR PFX "Couldn't close %s port %d\n", 2965 device->name, i); 2966 i--; 2967 } 2968 } 2969 2970 static void ib_mad_remove_device(struct ib_device *device) 2971 { 2972 int i, num_ports, cur_port; 2973 2974 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB) 2975 return; 2976 2977 if (device->node_type == RDMA_NODE_IB_SWITCH) { 2978 num_ports = 1; 2979 cur_port = 0; 2980 } else { 2981 num_ports = device->phys_port_cnt; 2982 cur_port = 1; 2983 } 2984 for (i = 0; i < num_ports; i++, cur_port++) { 2985 if (ib_agent_port_close(device, cur_port)) 2986 printk(KERN_ERR PFX "Couldn't close %s port %d " 2987 "for agents\n", 2988 device->name, cur_port); 2989 if (ib_mad_port_close(device, cur_port)) 2990 printk(KERN_ERR PFX "Couldn't close %s port %d\n", 2991 device->name, cur_port); 2992 } 2993 } 2994 2995 static struct ib_client mad_client = { 2996 .name = "mad", 2997 .add = ib_mad_init_device, 2998 .remove = ib_mad_remove_device 2999 }; 3000 3001 static int __init ib_mad_init_module(void) 3002 { 3003 int ret; 3004 3005 mad_recvq_size = min(mad_recvq_size, IB_MAD_QP_MAX_SIZE); 3006 mad_recvq_size = max(mad_recvq_size, IB_MAD_QP_MIN_SIZE); 3007 3008 mad_sendq_size = min(mad_sendq_size, IB_MAD_QP_MAX_SIZE); 3009 mad_sendq_size = max(mad_sendq_size, IB_MAD_QP_MIN_SIZE); 3010 3011 ib_mad_cache = kmem_cache_create("ib_mad", 3012 sizeof(struct ib_mad_private), 3013 0, 3014 SLAB_HWCACHE_ALIGN, 3015 NULL); 3016 if (!ib_mad_cache) { 3017 printk(KERN_ERR PFX "Couldn't create ib_mad cache\n"); 3018 ret = -ENOMEM; 3019 goto error1; 3020 } 3021 3022 INIT_LIST_HEAD(&ib_mad_port_list); 3023 3024 if (ib_register_client(&mad_client)) { 3025 printk(KERN_ERR PFX "Couldn't register ib_mad client\n"); 3026 ret = -EINVAL; 3027 goto error2; 3028 } 3029 3030 return 0; 3031 3032 error2: 3033 kmem_cache_destroy(ib_mad_cache); 3034 error1: 3035 return ret; 3036 } 3037 3038 static void __exit ib_mad_cleanup_module(void) 3039 { 3040 ib_unregister_client(&mad_client); 3041 kmem_cache_destroy(ib_mad_cache); 3042 } 3043 3044 module_init(ib_mad_init_module); 3045 module_exit(ib_mad_cleanup_module); 3046