Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Hal Rosenstock | de493d4 | 2007-04-02 11:24:07 -0400 | [diff] [blame] | 2 | * Copyright (c) 2004-2007 Voltaire, Inc. All rights reserved. |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 3 | * Copyright (c) 2005 Intel Corporation. All rights reserved. |
| 4 | * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved. |
Hal Rosenstock | b76aabc | 2009-09-07 08:28:48 -0700 | [diff] [blame] | 5 | * Copyright (c) 2009 HNR Consulting. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/dma-mapping.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 37 | #include <linux/slab.h> |
Jack Morgenstein | 9874e74 | 2006-06-17 20:37:34 -0700 | [diff] [blame] | 38 | #include <rdma/ib_cache.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | #include "mad_priv.h" |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 41 | #include "mad_rmpp.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Roland Dreier | 1693395 | 2010-05-23 21:39:31 -0700 | [diff] [blame] | 50 | static int mad_sendq_size = IB_MAD_QP_SEND_SIZE; |
| 51 | static int mad_recvq_size = IB_MAD_QP_RECV_SIZE; |
Hal Rosenstock | b76aabc | 2009-09-07 08:28:48 -0700 | [diff] [blame] | 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 | |
Roland Dreier | e54f818 | 2006-11-29 15:33:07 -0800 | [diff] [blame] | 58 | static struct kmem_cache *ib_mad_cache; |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | static struct list_head ib_mad_port_list; |
| 61 | static u32 ib_mad_client_id = 0; |
| 62 | |
| 63 | /* Port list lock */ |
Roland Dreier | 6276e08 | 2009-09-05 20:24:23 -0700 | [diff] [blame] | 64 | static DEFINE_SPINLOCK(ib_mad_port_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Hal Rosenstock | 4a0754f | 2005-07-27 11:45:24 -0700 | [diff] [blame] | 72 | struct ib_mad *mad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 76 | static void timeout_sends(struct work_struct *work); |
| 77 | static void local_completions(struct work_struct *work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Sean Hefty | 2527e68 | 2006-07-20 11:25:50 +0300 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 216 | if (rmpp_version && rmpp_version != IB_MGMT_RMPP_VERSION) |
| 217 | goto error1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Hal Rosenstock | 618a3c0 | 2006-03-28 16:40:04 -0800 | [diff] [blame] | 247 | /* Make sure class supplied is consistent with RMPP */ |
Hal Rosenstock | 64cb9c6 | 2006-04-12 21:29:10 -0400 | [diff] [blame] | 248 | if (!ib_is_mad_class_rmpp(mad_reg_req->mgmt_class)) { |
Hal Rosenstock | 618a3c0 | 2006-03-28 16:40:04 -0800 | [diff] [blame] | 249 | if (rmpp_version) |
| 250 | goto error1; |
| 251 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Roland Dreier | de6eb66 | 2005-11-02 07:23:14 -0800 | [diff] [blame] | 280 | mad_agent_priv = kzalloc(sizeof *mad_agent_priv, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | if (!mad_agent_priv) { |
| 282 | ret = ERR_PTR(-ENOMEM); |
| 283 | goto error1; |
| 284 | } |
Hal Rosenstock | b82cab6 | 2005-07-27 11:45:22 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
| 293 | if (mad_reg_req) { |
Julia Lawall | 9893e74 | 2010-05-15 23:22:38 +0200 | [diff] [blame] | 294 | reg_req = kmemdup(mad_reg_req, sizeof *reg_req, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | if (!reg_req) { |
| 296 | ret = ERR_PTR(-ENOMEM); |
Hal Rosenstock | b82cab6 | 2005-07-27 11:45:22 -0700 | [diff] [blame] | 297 | goto error3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /* Now, fill in the various structures */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | mad_agent_priv->qp_info = &port_priv->qp_info[qpn]; |
| 303 | mad_agent_priv->reg_req = reg_req; |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 304 | mad_agent_priv->agent.rmpp_version = rmpp_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Ralph Campbell | d9620a4 | 2009-02-27 14:44:32 -0800 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)) |
Hal Rosenstock | b82cab6 | 2005-07-27 11:45:22 -0700 | [diff] [blame] | 339 | goto error4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)) |
Hal Rosenstock | b82cab6 | 2005-07-27 11:45:22 -0700 | [diff] [blame] | 355 | goto error4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv); |
| 359 | } |
| 360 | if (ret2) { |
| 361 | ret = ERR_PTR(ret2); |
Hal Rosenstock | b82cab6 | 2005-07-27 11:45:22 -0700 | [diff] [blame] | 362 | goto error4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | return &mad_agent_priv->agent; |
| 371 | |
Hal Rosenstock | b82cab6 | 2005-07-27 11:45:22 -0700 | [diff] [blame] | 372 | error4: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | spin_unlock_irqrestore(&port_priv->reg_lock, flags); |
| 374 | kfree(reg_req); |
Hal Rosenstock | b82cab6 | 2005-07-27 11:45:22 -0700 | [diff] [blame] | 375 | error3: |
Hal Rosenstock | b82cab6 | 2005-07-27 11:45:22 -0700 | [diff] [blame] | 376 | ib_dereg_mr(mad_agent_priv->agent.mr); |
Adrian Bunk | 2012a11 | 2005-11-27 00:37:36 +0100 | [diff] [blame] | 377 | error2: |
| 378 | kfree(mad_agent_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. */ |
Roland Dreier | 5280517 | 2008-10-14 14:05:36 -0700 | [diff] [blame] | 415 | new_snoop_table = krealloc(qp_info->snoop_table, |
| 416 | sizeof mad_snoop_priv * |
| 417 | (qp_info->snoop_table_size + 1), |
| 418 | GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | if (!new_snoop_table) { |
| 420 | i = -ENOMEM; |
| 421 | goto out; |
| 422 | } |
Roland Dreier | 5280517 | 2008-10-14 14:05:36 -0700 | [diff] [blame] | 423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Roland Dreier | de6eb66 | 2005-11-02 07:23:14 -0800 | [diff] [blame] | 464 | mad_snoop_priv = kzalloc(sizeof *mad_snoop_priv, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | if (!mad_snoop_priv) { |
| 466 | ret = ERR_PTR(-ENOMEM); |
| 467 | goto error1; |
| 468 | } |
| 469 | |
| 470 | /* Now, fill in the various structures */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Sean Hefty | 1b52fa98 | 2006-05-12 14:57:52 -0700 | [diff] [blame] | 479 | init_completion(&mad_snoop_priv->comp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Sean Hefty | 1b52fa98 | 2006-05-12 14:57:52 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | port_priv = mad_agent_priv->qp_info->port_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | cancel_delayed_work(&mad_agent_priv->timed_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Hal Rosenstock | b82cab6 | 2005-07-27 11:45:22 -0700 | [diff] [blame] | 530 | flush_workqueue(port_priv->wq); |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 531 | ib_cancel_rmpp_recvs(mad_agent_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | |
Sean Hefty | 1b52fa98 | 2006-05-12 14:57:52 -0700 | [diff] [blame] | 533 | deref_mad_agent(mad_agent_priv); |
| 534 | wait_for_completion(&mad_agent_priv->comp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
Jesper Juhl | 6044ec8 | 2005-11-07 01:01:32 -0800 | [diff] [blame] | 536 | kfree(mad_agent_priv->reg_req); |
Hal Rosenstock | b82cab6 | 2005-07-27 11:45:22 -0700 | [diff] [blame] | 537 | ib_dereg_mr(mad_agent_priv->agent.mr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Sean Hefty | 1b52fa98 | 2006-05-12 14:57:52 -0700 | [diff] [blame] | 552 | deref_snoop_agent(mad_snoop_priv); |
| 553 | wait_for_completion(&mad_snoop_priv->comp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 596 | struct ib_mad_send_buf *send_buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 614 | send_buf, mad_send_wc); |
Sean Hefty | 1b52fa98 | 2006-05-12 14:57:52 -0700 | [diff] [blame] | 615 | deref_snoop_agent(mad_snoop_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Sean Hefty | 1b52fa98 | 2006-05-12 14:57:52 -0700 | [diff] [blame] | 640 | deref_snoop_agent(mad_snoop_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | spin_lock_irqsave(&qp_info->snoop_lock, flags); |
| 642 | } |
| 643 | spin_unlock_irqrestore(&qp_info->snoop_lock, flags); |
| 644 | } |
| 645 | |
Michael S. Tsirkin | 062dbb6 | 2006-12-31 21:09:42 +0200 | [diff] [blame] | 646 | static void build_smp_wc(struct ib_qp *qp, |
| 647 | u64 wr_id, u16 slid, u16 pkey_index, u8 port_num, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Michael S. Tsirkin | 062dbb6 | 2006-12-31 21:09:42 +0200 | [diff] [blame] | 657 | wc->qp = qp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 670 | struct ib_mad_send_wr_private *mad_send_wr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | { |
Hal Rosenstock | de493d4 | 2007-04-02 11:24:07 -0400 | [diff] [blame] | 672 | int ret = 0; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 673 | struct ib_smp *smp = mad_send_wr->send_buf.mad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Hal Rosenstock | 1bae4db | 2007-05-14 17:21:52 -0400 | [diff] [blame] | 680 | u8 port_num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | struct ib_wc mad_wc; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 682 | struct ib_send_wr *send_wr = &mad_send_wr->send_wr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | |
Hal Rosenstock | 1bae4db | 2007-05-14 17:21:52 -0400 | [diff] [blame] | 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 | |
Ralph Campbell | 8cf3f04 | 2006-02-03 14:28:48 -0800 | [diff] [blame] | 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 && |
Hal Rosenstock | de493d4 | 2007-04-02 11:24:07 -0400 | [diff] [blame] | 698 | smi_handle_dr_smp_send(smp, device->node_type, port_num) == |
| 699 | IB_SMI_DISCARD) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | ret = -EINVAL; |
| 701 | printk(KERN_ERR PFX "Invalid directed route\n"); |
| 702 | goto out; |
| 703 | } |
Hal Rosenstock | de493d4 | 2007-04-02 11:24:07 -0400 | [diff] [blame] | 704 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | /* Check to post send on QP or process locally */ |
Steve Welch | 727792d | 2007-10-23 15:06:10 -0700 | [diff] [blame] | 706 | if (smi_check_local_smp(smp, device) == IB_SMI_DISCARD && |
| 707 | smi_check_local_returning_smp(smp, device) == IB_SMI_DISCARD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Michael S. Tsirkin | 062dbb6 | 2006-12-31 21:09:42 +0200 | [diff] [blame] | 726 | build_smp_wc(mad_agent_priv->agent.qp, |
| 727 | send_wr->wr_id, be16_to_cpu(smp->dr_slid), |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 728 | send_wr->wr.ud.pkey_index, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: |
Sean Hefty | 2527e68 | 2006-07-20 11:25:50 +0300 | [diff] [blame] | 738 | if (ib_response_mad(&mad_priv->mad.mad) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Ralph Campbell | 4780c19 | 2009-03-03 14:22:17 -0800 | [diff] [blame] | 752 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | case IB_MAD_RESULT_SUCCESS: |
| 754 | /* Treat like an incoming receive MAD */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | port_priv = ib_get_mad_port(mad_agent_priv->agent.device, |
| 756 | mad_agent_priv->agent.port_num); |
| 757 | if (port_priv) { |
Steve Welch | 727792d | 2007-10-23 15:06:10 -0700 | [diff] [blame] | 758 | memcpy(&mad_priv->mad.mad, smp, sizeof(struct ib_mad)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | recv_mad_agent = find_mad_agent(port_priv, |
Hal Rosenstock | 4a0754f | 2005-07-27 11:45:24 -0700 | [diff] [blame] | 760 | &mad_priv->mad.mad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | } |
| 762 | if (!port_priv || !recv_mad_agent) { |
Ralph Campbell | 4780c19 | 2009-03-03 14:22:17 -0800 | [diff] [blame] | 763 | /* |
| 764 | * No receiving agent so drop packet and |
| 765 | * generate send completion. |
| 766 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | kmem_cache_free(ib_mad_cache, mad_priv); |
Ralph Campbell | 4780c19 | 2009-03-03 14:22:17 -0800 | [diff] [blame] | 768 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 780 | local->mad_send_wr = mad_send_wr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Hal Rosenstock | b82cab6 | 2005-07-27 11:45:22 -0700 | [diff] [blame] | 788 | &mad_agent_priv->local_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | ret = 1; |
| 790 | out: |
| 791 | return ret; |
| 792 | } |
| 793 | |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 794 | static int get_pad_size(int hdr_len, int data_len) |
Hal Rosenstock | 824c8ae | 2005-07-27 11:45:23 -0700 | [diff] [blame] | 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; |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 801 | return pad == seg_size ? 0 : pad; |
Hal Rosenstock | 824c8ae | 2005-07-27 11:45:23 -0700 | [diff] [blame] | 802 | } else |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 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; |
Hal Rosenstock | 824c8ae | 2005-07-27 11:45:23 -0700 | [diff] [blame] | 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, |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 859 | int rmpp_active, |
Hal Rosenstock | 824c8ae | 2005-07-27 11:45:23 -0700 | [diff] [blame] | 860 | int hdr_len, int data_len, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 861 | gfp_t gfp_mask) |
Hal Rosenstock | 824c8ae | 2005-07-27 11:45:23 -0700 | [diff] [blame] | 862 | { |
| 863 | struct ib_mad_agent_private *mad_agent_priv; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 864 | struct ib_mad_send_wr_private *mad_send_wr; |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 865 | int pad, message_size, ret, size; |
Hal Rosenstock | 824c8ae | 2005-07-27 11:45:23 -0700 | [diff] [blame] | 866 | void *buf; |
| 867 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 868 | mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private, |
| 869 | agent); |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 870 | pad = get_pad_size(hdr_len, data_len); |
| 871 | message_size = hdr_len + data_len + pad; |
Hal Rosenstock | 824c8ae | 2005-07-27 11:45:23 -0700 | [diff] [blame] | 872 | |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 873 | if ((!mad_agent->rmpp_version && |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 874 | (rmpp_active || message_size > sizeof(struct ib_mad))) || |
| 875 | (!rmpp_active && message_size > sizeof(struct ib_mad))) |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 876 | return ERR_PTR(-EINVAL); |
| 877 | |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 878 | size = rmpp_active ? hdr_len : sizeof(struct ib_mad); |
| 879 | buf = kzalloc(sizeof *mad_send_wr + size, gfp_mask); |
Hal Rosenstock | 824c8ae | 2005-07-27 11:45:23 -0700 | [diff] [blame] | 880 | if (!buf) |
| 881 | return ERR_PTR(-ENOMEM); |
| 882 | |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 883 | mad_send_wr = buf + size; |
| 884 | INIT_LIST_HEAD(&mad_send_wr->rmpp_list); |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 885 | mad_send_wr->send_buf.mad = buf; |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 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; |
Hal Rosenstock | 824c8ae | 2005-07-27 11:45:23 -0700 | [diff] [blame] | 889 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 890 | mad_send_wr->mad_agent_priv = mad_agent_priv; |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 891 | mad_send_wr->sg_list[0].length = hdr_len; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 892 | mad_send_wr->sg_list[0].lkey = mad_agent->mr->lkey; |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 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; |
Hal Rosenstock | 824c8ae | 2005-07-27 11:45:23 -0700 | [diff] [blame] | 895 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 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; |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 898 | mad_send_wr->send_wr.num_sge = 2; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 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; |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 904 | |
| 905 | if (rmpp_active) { |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 906 | ret = alloc_send_rmpp_list(mad_send_wr, gfp_mask); |
| 907 | if (ret) { |
| 908 | kfree(buf); |
| 909 | return ERR_PTR(ret); |
| 910 | } |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 911 | } |
| 912 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 913 | mad_send_wr->send_buf.mad_agent = mad_agent; |
Hal Rosenstock | 824c8ae | 2005-07-27 11:45:23 -0700 | [diff] [blame] | 914 | atomic_inc(&mad_agent_priv->refcount); |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 915 | return &mad_send_wr->send_buf; |
Hal Rosenstock | 824c8ae | 2005-07-27 11:45:23 -0700 | [diff] [blame] | 916 | } |
| 917 | EXPORT_SYMBOL(ib_create_send_mad); |
| 918 | |
Hal Rosenstock | 618a3c0 | 2006-03-28 16:40:04 -0800 | [diff] [blame] | 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 | |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 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 | |
Hal Rosenstock | 824c8ae | 2005-07-27 11:45:23 -0700 | [diff] [blame] | 980 | void ib_free_send_mad(struct ib_mad_send_buf *send_buf) |
| 981 | { |
| 982 | struct ib_mad_agent_private *mad_agent_priv; |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 983 | struct ib_mad_send_wr_private *mad_send_wr; |
Hal Rosenstock | 824c8ae | 2005-07-27 11:45:23 -0700 | [diff] [blame] | 984 | |
| 985 | mad_agent_priv = container_of(send_buf->mad_agent, |
| 986 | struct ib_mad_agent_private, agent); |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 987 | mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private, |
| 988 | send_buf); |
Hal Rosenstock | 824c8ae | 2005-07-27 11:45:23 -0700 | [diff] [blame] | 989 | |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 990 | free_send_rmpp_list(mad_send_wr); |
| 991 | kfree(send_buf->mad); |
Sean Hefty | 1b52fa98 | 2006-05-12 14:57:52 -0700 | [diff] [blame] | 992 | deref_mad_agent(mad_agent_priv); |
Hal Rosenstock | 824c8ae | 2005-07-27 11:45:23 -0700 | [diff] [blame] | 993 | } |
| 994 | EXPORT_SYMBOL(ib_free_send_mad); |
| 995 | |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 996 | int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | { |
| 998 | struct ib_mad_qp_info *qp_info; |
Hal Rosenstock | cabe3cb | 2005-07-27 11:45:33 -0700 | [diff] [blame] | 999 | struct list_head *list; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1000 | struct ib_send_wr *bad_send_wr; |
| 1001 | struct ib_mad_agent *mad_agent; |
| 1002 | struct ib_sge *sge; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | unsigned long flags; |
| 1004 | int ret; |
| 1005 | |
Hal Rosenstock | f8197a4 | 2005-07-27 11:45:24 -0700 | [diff] [blame] | 1006 | /* Set WR ID to find mad_send_wr upon completion */ |
Hal Rosenstock | d760ce8 | 2005-07-27 11:45:25 -0700 | [diff] [blame] | 1007 | qp_info = mad_send_wr->mad_agent_priv->qp_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1011 | mad_agent = mad_send_wr->send_buf.mad_agent; |
| 1012 | sge = mad_send_wr->sg_list; |
Ralph Campbell | 1527106 | 2006-12-12 14:28:30 -0800 | [diff] [blame] | 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; |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 1018 | |
Ralph Campbell | 1527106 | 2006-12-12 14:28:30 -0800 | [diff] [blame] | 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; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1024 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | spin_lock_irqsave(&qp_info->send_queue.lock, flags); |
Hal Rosenstock | cabe3cb | 2005-07-27 11:45:33 -0700 | [diff] [blame] | 1026 | if (qp_info->send_queue.count < qp_info->send_queue.max_active) { |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1027 | ret = ib_post_send(mad_agent->qp, &mad_send_wr->send_wr, |
| 1028 | &bad_send_wr); |
Hal Rosenstock | cabe3cb | 2005-07-27 11:45:33 -0700 | [diff] [blame] | 1029 | list = &qp_info->send_queue.list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | ret = 0; |
Hal Rosenstock | cabe3cb | 2005-07-27 11:45:33 -0700 | [diff] [blame] | 1032 | list = &qp_info->overflow_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | } |
Hal Rosenstock | cabe3cb | 2005-07-27 11:45:33 -0700 | [diff] [blame] | 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); |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 1040 | if (ret) { |
Ralph Campbell | 1527106 | 2006-12-12 14:28:30 -0800 | [diff] [blame] | 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); |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 1047 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1055 | int ib_post_send_mad(struct ib_mad_send_buf *send_buf, |
| 1056 | struct ib_mad_send_buf **bad_send_buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | struct ib_mad_agent_private *mad_agent_priv; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | |
| 1064 | /* Walk list of send WRs and post each on send list */ |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1065 | for (; send_buf; send_buf = next_send_buf) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
Hal Rosenstock | 618a3c0 | 2006-03-28 16:40:04 -0800 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1091 | next_send_buf = send_buf->next; |
| 1092 | mad_send_wr->send_wr.wr.ud.ah = send_buf->ah; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | if (ret < 0) /* error */ |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1099 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | else if (ret == 1) /* locally consumed */ |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1101 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | } |
| 1103 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1104 | mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | /* Timeout will be updated after send completes */ |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1106 | mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms); |
Sean Hefty | 4fc8cd4 | 2007-11-27 00:11:04 -0800 | [diff] [blame] | 1107 | mad_send_wr->max_retries = send_buf->retries; |
| 1108 | mad_send_wr->retries_left = send_buf->retries; |
| 1109 | send_buf->retries = 0; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1110 | /* Reference for work request to QP + response */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 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) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1133 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | } |
| 1136 | return 0; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1137 | error: |
| 1138 | if (bad_send_buf) |
| 1139 | *bad_send_buf = send_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 1150 | struct ib_mad_recv_buf *mad_recv_buf, *temp_recv_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | struct ib_mad_private_header *mad_priv_hdr; |
| 1152 | struct ib_mad_private *priv; |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 1153 | struct list_head free_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 1155 | INIT_LIST_HEAD(&free_list); |
| 1156 | list_splice_init(&mad_recv_wc->rmpp_list, &free_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 1167 | kmem_cache_free(ib_mad_cache, priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | } |
| 1170 | EXPORT_SYMBOL(ib_free_recv_mad); |
| 1171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Akinobu Mita | 19b629f | 2010-03-05 13:41:38 -0800 | [diff] [blame] | 1195 | for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Roland Dreier | de6eb66 | 2005-11-02 07:23:14 -0800 | [diff] [blame] | 1207 | *method = kzalloc(sizeof **method, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | if (!*method) { |
| 1209 | printk(KERN_ERR PFX "No memory for " |
| 1210 | "ib_mad_mgmt_method_table\n"); |
| 1211 | return -ENOMEM; |
| 1212 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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++) |
Roland Dreier | 3cd9656 | 2006-09-22 15:22:46 -0700 | [diff] [blame] | 1259 | /* Is there matching OUI for this vendor class ? */ |
| 1260 | if (!memcmp(vendor_class->oui[i], oui, 3)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Roland Dreier | de6eb66 | 2005-11-02 07:23:14 -0800 | [diff] [blame] | 1303 | *class = kzalloc(sizeof **class, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Roland Dreier | de6eb66 | 2005-11-02 07:23:14 -0800 | [diff] [blame] | 1310 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Akinobu Mita | 19b629f | 2010-03-05 13:41:38 -0800 | [diff] [blame] | 1329 | for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | (*method)->agent[i] = agent_priv; |
Akinobu Mita | 19b629f | 2010-03-05 13:41:38 -0800 | [diff] [blame] | 1331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Roland Dreier | de6eb66 | 2005-11-02 07:23:14 -0800 | [diff] [blame] | 1370 | vendor = kzalloc(sizeof *vendor, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | if (!vendor) { |
| 1372 | printk(KERN_ERR PFX "No memory for " |
| 1373 | "ib_mad_mgmt_vendor_class_table\n"); |
| 1374 | goto error1; |
| 1375 | } |
Roland Dreier | de6eb66 | 2005-11-02 07:23:14 -0800 | [diff] [blame] | 1376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | *vendor_table = vendor; |
| 1378 | } |
| 1379 | if (!(*vendor_table)->vendor_class[vclass]) { |
| 1380 | /* Allocate table for this management vendor class */ |
Roland Dreier | de6eb66 | 2005-11-02 07:23:14 -0800 | [diff] [blame] | 1381 | vendor_class = kzalloc(sizeof *vendor_class, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | if (!vendor_class) { |
| 1383 | printk(KERN_ERR PFX "No memory for " |
| 1384 | "ib_mad_mgmt_vendor_class\n"); |
| 1385 | goto error2; |
| 1386 | } |
Roland Dreier | de6eb66 | 2005-11-02 07:23:14 -0800 | [diff] [blame] | 1387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Akinobu Mita | 19b629f | 2010-03-05 13:41:38 -0800 | [diff] [blame] | 1424 | for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | (*method)->agent[i] = agent_priv; |
Akinobu Mita | 19b629f | 2010-03-05 13:41:38 -0800 | [diff] [blame] | 1426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | static struct ib_mad_agent_private * |
| 1551 | find_mad_agent(struct ib_mad_port_private *port_priv, |
Hal Rosenstock | 4a0754f | 2005-07-27 11:45:24 -0700 | [diff] [blame] | 1552 | struct ib_mad *mad) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | { |
| 1554 | struct ib_mad_agent_private *mad_agent = NULL; |
| 1555 | unsigned long flags; |
| 1556 | |
| 1557 | spin_lock_irqsave(&port_priv->reg_lock, flags); |
Sean Hefty | 2527e68 | 2006-07-20 11:25:50 +0300 | [diff] [blame] | 1558 | if (ib_response_mad(mad)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1567 | list_for_each_entry(entry, &port_priv->agent_list, agent_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 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 | |
Jack Morgenstein | fa9656b | 2006-03-28 16:39:07 -0800 | [diff] [blame] | 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 | |
Jack Morgenstein | 9874e74 | 2006-06-17 20:37:34 -0700 | [diff] [blame] | 1680 | static inline int rcv_has_same_gid(struct ib_mad_agent_private *mad_agent_priv, |
| 1681 | struct ib_mad_send_wr_private *wr, |
Jack Morgenstein | fa9656b | 2006-03-28 16:39:07 -0800 | [diff] [blame] | 1682 | struct ib_mad_recv_wc *rwc ) |
| 1683 | { |
| 1684 | struct ib_ah_attr attr; |
| 1685 | u8 send_resp, rcv_resp; |
Jack Morgenstein | 9874e74 | 2006-06-17 20:37:34 -0700 | [diff] [blame] | 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; |
Jack Morgenstein | fa9656b | 2006-03-28 16:39:07 -0800 | [diff] [blame] | 1690 | |
Michael Brooks | 7097228 | 2008-09-20 20:06:16 -0700 | [diff] [blame] | 1691 | send_resp = ib_response_mad((struct ib_mad *)wr->send_buf.mad); |
| 1692 | rcv_resp = ib_response_mad(rwc->recv_buf.mad); |
Jack Morgenstein | fa9656b | 2006-03-28 16:39:07 -0800 | [diff] [blame] | 1693 | |
Jack Morgenstein | fa9656b | 2006-03-28 16:39:07 -0800 | [diff] [blame] | 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 | |
Jack Morgenstein | 9874e74 | 2006-06-17 20:37:34 -0700 | [diff] [blame] | 1702 | if (!!(attr.ah_flags & IB_AH_GRH) != |
| 1703 | !!(rwc->wc->wc_flags & IB_WC_GRH)) |
Jack Morgenstein | fa9656b | 2006-03-28 16:39:07 -0800 | [diff] [blame] | 1704 | /* one has GID, other does not. Assume different */ |
| 1705 | return 0; |
Jack Morgenstein | 9874e74 | 2006-06-17 20:37:34 -0700 | [diff] [blame] | 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); |
Jack Morgenstein | fa9656b | 2006-03-28 16:39:07 -0800 | [diff] [blame] | 1729 | } |
Jack Morgenstein | 9874e74 | 2006-06-17 20:37:34 -0700 | [diff] [blame] | 1730 | |
| 1731 | static inline int is_direct(u8 class) |
| 1732 | { |
| 1733 | return (class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE); |
| 1734 | } |
| 1735 | |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 1736 | struct ib_mad_send_wr_private* |
Jack Morgenstein | fa9656b | 2006-03-28 16:39:07 -0800 | [diff] [blame] | 1737 | ib_find_send_mad(struct ib_mad_agent_private *mad_agent_priv, |
Jack Morgenstein | 9874e74 | 2006-06-17 20:37:34 -0700 | [diff] [blame] | 1738 | struct ib_mad_recv_wc *wc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | { |
Jack Morgenstein | 9874e74 | 2006-06-17 20:37:34 -0700 | [diff] [blame] | 1740 | struct ib_mad_send_wr_private *wr; |
Jack Morgenstein | fa9656b | 2006-03-28 16:39:07 -0800 | [diff] [blame] | 1741 | struct ib_mad *mad; |
| 1742 | |
Jack Morgenstein | 9874e74 | 2006-06-17 20:37:34 -0700 | [diff] [blame] | 1743 | mad = (struct ib_mad *)wc->recv_buf.mad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | |
Jack Morgenstein | 9874e74 | 2006-06-17 20:37:34 -0700 | [diff] [blame] | 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))) |
Roland Dreier | 3979869 | 2006-11-13 09:38:07 -0800 | [diff] [blame] | 1754 | return (wr->status == IB_WC_SUCCESS) ? wr : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | /* |
| 1758 | * It's possible to receive the response before we've |
| 1759 | * been notified that the send has completed |
| 1760 | */ |
Jack Morgenstein | 9874e74 | 2006-06-17 20:37:34 -0700 | [diff] [blame] | 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))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | /* Verify request has not been canceled */ |
Jack Morgenstein | 9874e74 | 2006-06-17 20:37:34 -0700 | [diff] [blame] | 1773 | return (wr->status == IB_WC_SUCCESS) ? wr : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | } |
| 1775 | return NULL; |
| 1776 | } |
| 1777 | |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 1778 | void ib_mark_mad_done(struct ib_mad_send_wr_private *mad_send_wr) |
Hal Rosenstock | 6a0c435 | 2005-07-27 11:45:26 -0700 | [diff] [blame] | 1779 | { |
| 1780 | mad_send_wr->timeout = 0; |
Akinobu Mita | 179e091 | 2006-06-26 00:24:41 -0700 | [diff] [blame] | 1781 | if (mad_send_wr->refcount == 1) |
| 1782 | list_move_tail(&mad_send_wr->agent_list, |
Hal Rosenstock | 6a0c435 | 2005-07-27 11:45:26 -0700 | [diff] [blame] | 1783 | &mad_send_wr->mad_agent_priv->done_list); |
Hal Rosenstock | 6a0c435 | 2005-07-27 11:45:26 -0700 | [diff] [blame] | 1784 | } |
| 1785 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv, |
Hal Rosenstock | 4a0754f | 2005-07-27 11:45:24 -0700 | [diff] [blame] | 1787 | struct ib_mad_recv_wc *mad_recv_wc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 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) { |
Sean Hefty | 1b52fa98 | 2006-05-12 14:57:52 -0700 | [diff] [blame] | 1799 | deref_mad_agent(mad_agent_priv); |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 1800 | return; |
| 1801 | } |
| 1802 | } |
| 1803 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | /* Complete corresponding request */ |
Sean Hefty | 2527e68 | 2006-07-20 11:25:50 +0300 | [diff] [blame] | 1805 | if (ib_response_mad(mad_recv_wc->recv_buf.mad)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | spin_lock_irqsave(&mad_agent_priv->lock, flags); |
Jack Morgenstein | fa9656b | 2006-03-28 16:39:07 -0800 | [diff] [blame] | 1807 | mad_send_wr = ib_find_send_mad(mad_agent_priv, mad_recv_wc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | if (!mad_send_wr) { |
| 1809 | spin_unlock_irqrestore(&mad_agent_priv->lock, flags); |
Hal Rosenstock | 4a0754f | 2005-07-27 11:45:24 -0700 | [diff] [blame] | 1810 | ib_free_recv_mad(mad_recv_wc); |
Sean Hefty | 1b52fa98 | 2006-05-12 14:57:52 -0700 | [diff] [blame] | 1811 | deref_mad_agent(mad_agent_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | return; |
| 1813 | } |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 1814 | ib_mark_mad_done(mad_send_wr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | spin_unlock_irqrestore(&mad_agent_priv->lock, flags); |
| 1816 | |
| 1817 | /* Defined behavior is to complete response before request */ |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1818 | mad_recv_wc->wc->wr_id = (unsigned long) &mad_send_wr->send_buf; |
Hal Rosenstock | 4a0754f | 2005-07-27 11:45:24 -0700 | [diff] [blame] | 1819 | mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent, |
| 1820 | mad_recv_wc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | atomic_dec(&mad_agent_priv->refcount); |
| 1822 | |
| 1823 | mad_send_wc.status = IB_WC_SUCCESS; |
| 1824 | mad_send_wc.vendor_err = 0; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1825 | mad_send_wc.send_buf = &mad_send_wr->send_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc); |
| 1827 | } else { |
Hal Rosenstock | 4a0754f | 2005-07-27 11:45:24 -0700 | [diff] [blame] | 1828 | mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent, |
| 1829 | mad_recv_wc); |
Sean Hefty | 1b52fa98 | 2006-05-12 14:57:52 -0700 | [diff] [blame] | 1830 | deref_mad_agent(mad_agent_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Hal Rosenstock | 445d680 | 2007-08-03 10:45:17 -0700 | [diff] [blame] | 1839 | struct ib_mad_private *recv, *response = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | struct ib_mad_list_head *mad_list; |
| 1841 | struct ib_mad_agent_private *mad_agent; |
Hal Rosenstock | 1bae4db | 2007-05-14 17:21:52 -0400 | [diff] [blame] | 1842 | int port_num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Ralph Campbell | 1527106 | 2006-12-12 14:28:30 -0800 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | |
| 1857 | /* Setup MAD receive work completion from "normal" work completion */ |
Sean Hefty | 24239af | 2005-04-16 15:26:08 -0700 | [diff] [blame] | 1858 | recv->header.wc = *wc; |
| 1859 | recv->header.recv_wc.wc = &recv->header.wc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Hal Rosenstock | 445d680 | 2007-08-03 10:45:17 -0700 | [diff] [blame] | 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 | |
Hal Rosenstock | 1bae4db | 2007-05-14 17:21:52 -0400 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | if (recv->mad.mad.mad_hdr.mgmt_class == |
| 1884 | IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) { |
Hal Rosenstock | 1bae4db | 2007-05-14 17:21:52 -0400 | [diff] [blame] | 1885 | enum smi_forward_action retsmi; |
| 1886 | |
Hal Rosenstock | de493d4 | 2007-04-02 11:24:07 -0400 | [diff] [blame] | 1887 | if (smi_handle_dr_smp_recv(&recv->mad.smp, |
| 1888 | port_priv->device->node_type, |
Hal Rosenstock | 1bae4db | 2007-05-14 17:21:52 -0400 | [diff] [blame] | 1889 | port_num, |
Hal Rosenstock | de493d4 | 2007-04-02 11:24:07 -0400 | [diff] [blame] | 1890 | port_priv->device->phys_port_cnt) == |
| 1891 | IB_SMI_DISCARD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | goto out; |
Hal Rosenstock | de493d4 | 2007-04-02 11:24:07 -0400 | [diff] [blame] | 1893 | |
Hal Rosenstock | 1bae4db | 2007-05-14 17:21:52 -0400 | [diff] [blame] | 1894 | retsmi = smi_check_forward_dr_smp(&recv->mad.smp); |
| 1895 | if (retsmi == IB_SMI_LOCAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | goto local; |
Hal Rosenstock | de493d4 | 2007-04-02 11:24:07 -0400 | [diff] [blame] | 1897 | |
Hal Rosenstock | 1bae4db | 2007-05-14 17:21:52 -0400 | [diff] [blame] | 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; |
Hal Rosenstock | de493d4 | 2007-04-02 11:24:07 -0400 | [diff] [blame] | 1903 | |
Hal Rosenstock | 1bae4db | 2007-05-14 17:21:52 -0400 | [diff] [blame] | 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 | |
Hal Rosenstock | 86dfbec | 2007-08-03 10:45:17 -0700 | [diff] [blame] | 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); |
Hal Rosenstock | 1bae4db | 2007-05-14 17:21:52 -0400 | [diff] [blame] | 1918 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | goto out; |
Hal Rosenstock | 1bae4db | 2007-05-14 17:21:52 -0400 | [diff] [blame] | 1920 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1937 | agent_send_response(&response->mad.mad, |
| 1938 | &recv->grh, wc, |
| 1939 | port_priv->device, |
Hal Rosenstock | 1bae4db | 2007-05-14 17:21:52 -0400 | [diff] [blame] | 1940 | port_num, |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 1941 | qp_info->qp->qp_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | goto out; |
| 1943 | } |
| 1944 | } |
| 1945 | } |
| 1946 | |
Hal Rosenstock | 4a0754f | 2005-07-27 11:45:24 -0700 | [diff] [blame] | 1947 | mad_agent = find_mad_agent(port_priv, &recv->mad.mad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | if (mad_agent) { |
Hal Rosenstock | 4a0754f | 2005-07-27 11:45:24 -0700 | [diff] [blame] | 1949 | ib_mad_complete_recv(mad_agent, &recv->header.recv_wc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)) { |
Roland Dreier | 6b2eef8 | 2009-09-07 08:27:50 -0700 | [diff] [blame] | 1973 | __cancel_delayed_work(&mad_agent_priv->timed_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Roland Dreier | 6b2eef8 | 2009-09-07 08:27:50 -0700 | [diff] [blame] | 1982 | __cancel_delayed_work(&mad_agent_priv->timed_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Hal Rosenstock | d760ce8 | 2005-07-27 11:45:25 -0700 | [diff] [blame] | 1993 | static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | { |
Hal Rosenstock | d760ce8 | 2005-07-27 11:45:25 -0700 | [diff] [blame] | 1995 | struct ib_mad_agent_private *mad_agent_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | struct ib_mad_send_wr_private *temp_mad_send_wr; |
| 1997 | struct list_head *list_item; |
| 1998 | unsigned long delay; |
| 1999 | |
Hal Rosenstock | d760ce8 | 2005-07-27 11:45:25 -0700 | [diff] [blame] | 2000 | mad_agent_priv = mad_send_wr->mad_agent_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | list_del(&mad_send_wr->agent_list); |
| 2002 | |
| 2003 | delay = mad_send_wr->timeout; |
| 2004 | mad_send_wr->timeout += jiffies; |
| 2005 | |
Hal Rosenstock | 29bb33d | 2005-07-27 11:45:32 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | } |
Hal Rosenstock | 29bb33d | 2005-07-27 11:45:32 -0700 | [diff] [blame] | 2016 | else |
| 2017 | list_item = &mad_agent_priv->wait_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Roland Dreier | 6b2eef8 | 2009-09-07 08:27:50 -0700 | [diff] [blame] | 2022 | __cancel_delayed_work(&mad_agent_priv->timed_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | queue_delayed_work(mad_agent_priv->qp_info->port_priv->wq, |
| 2024 | &mad_agent_priv->timed_work, delay); |
| 2025 | } |
| 2026 | } |
| 2027 | |
Hal Rosenstock | 03b61ad | 2005-07-27 11:45:32 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | /* |
| 2036 | * Process a send work completion |
| 2037 | */ |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | { |
| 2041 | struct ib_mad_agent_private *mad_agent_priv; |
| 2042 | unsigned long flags; |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 2043 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | |
Hal Rosenstock | d760ce8 | 2005-07-27 11:45:25 -0700 | [diff] [blame] | 2045 | mad_agent_priv = mad_send_wr->mad_agent_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | spin_lock_irqsave(&mad_agent_priv->lock, flags); |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Hal Rosenstock | d760ce8 | 2005-07-27 11:45:25 -0700 | [diff] [blame] | 2063 | wait_for_response(mad_send_wr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | } |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 2065 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2075 | if (ret == IB_RMPP_RESULT_INTERNAL) |
| 2076 | ib_rmpp_send_handler(mad_send_wc); |
| 2077 | else |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 2078 | mad_agent_priv->agent.send_handler(&mad_agent_priv->agent, |
| 2079 | mad_send_wc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | |
| 2081 | /* Release reference on agent taken when sending */ |
Sean Hefty | 1b52fa98 | 2006-05-12 14:57:52 -0700 | [diff] [blame] | 2082 | deref_mad_agent(mad_agent_priv); |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 2083 | return; |
| 2084 | done: |
| 2085 | spin_unlock_irqrestore(&mad_agent_priv->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2096 | struct ib_mad_send_wc mad_send_wc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: |
Ralph Campbell | 1527106 | 2006-12-12 14:28:30 -0800 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Akinobu Mita | 179e091 | 2006-06-26 00:24:41 -0700 | [diff] [blame] | 2124 | list_move_tail(&mad_list->list, &send_queue->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | } |
| 2126 | spin_unlock_irqrestore(&send_queue->lock, flags); |
| 2127 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | if (atomic_read(&qp_info->snoop_count)) |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2132 | snoop_send(qp_info, &mad_send_wr->send_buf, &mad_send_wc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | IB_MAD_SNOOP_SEND_COMPLETIONS); |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2134 | ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | |
| 2136 | if (queued_send_wr) { |
| 2137 | ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr, |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2138 | &bad_send_wr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 2224 | static void ib_mad_completion_handler(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2225 | { |
| 2226 | struct ib_mad_port_private *port_priv; |
| 2227 | struct ib_wc wc; |
| 2228 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 2229 | port_priv = container_of(work, struct ib_mad_port_private, work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Roland Dreier | 3cd9656 | 2006-09-22 15:22:46 -0700 | [diff] [blame] | 2263 | mad_send_wr->status = IB_WC_WR_FLUSH_ERR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2278 | mad_send_wc.send_buf = &mad_send_wr->send_buf; |
| 2279 | list_del(&mad_send_wr->agent_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | mad_agent_priv->agent.send_handler(&mad_agent_priv->agent, |
| 2281 | &mad_send_wc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | atomic_dec(&mad_agent_priv->refcount); |
| 2283 | } |
| 2284 | } |
| 2285 | |
| 2286 | static struct ib_mad_send_wr_private* |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2287 | find_send_wr(struct ib_mad_agent_private *mad_agent_priv, |
| 2288 | struct ib_mad_send_buf *send_buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2294 | if (&mad_send_wr->send_buf == send_buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | return mad_send_wr; |
| 2296 | } |
| 2297 | |
| 2298 | list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list, |
| 2299 | agent_list) { |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2300 | if (is_data_mad(mad_agent_priv, mad_send_wr->send_buf.mad) && |
| 2301 | &mad_send_wr->send_buf == send_buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | return mad_send_wr; |
| 2303 | } |
| 2304 | return NULL; |
| 2305 | } |
| 2306 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2307 | int ib_modify_mad(struct ib_mad_agent *mad_agent, |
| 2308 | struct ib_mad_send_buf *send_buf, u32 timeout_ms) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2309 | { |
| 2310 | struct ib_mad_agent_private *mad_agent_priv; |
| 2311 | struct ib_mad_send_wr_private *mad_send_wr; |
| 2312 | unsigned long flags; |
Hal Rosenstock | cabe3cb | 2005-07-27 11:45:33 -0700 | [diff] [blame] | 2313 | int active; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2318 | mad_send_wr = find_send_wr(mad_agent_priv, send_buf); |
Hal Rosenstock | 03b61ad | 2005-07-27 11:45:32 -0700 | [diff] [blame] | 2319 | if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | spin_unlock_irqrestore(&mad_agent_priv->lock, flags); |
Hal Rosenstock | 03b61ad | 2005-07-27 11:45:32 -0700 | [diff] [blame] | 2321 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | } |
| 2323 | |
Hal Rosenstock | cabe3cb | 2005-07-27 11:45:33 -0700 | [diff] [blame] | 2324 | active = (!mad_send_wr->timeout || mad_send_wr->refcount > 1); |
Hal Rosenstock | 03b61ad | 2005-07-27 11:45:32 -0700 | [diff] [blame] | 2325 | if (!timeout_ms) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2326 | mad_send_wr->status = IB_WC_WR_FLUSH_ERR; |
Hal Rosenstock | 03b61ad | 2005-07-27 11:45:32 -0700 | [diff] [blame] | 2327 | mad_send_wr->refcount -= (mad_send_wr->timeout > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | } |
| 2329 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2330 | mad_send_wr->send_buf.timeout_ms = timeout_ms; |
Hal Rosenstock | cabe3cb | 2005-07-27 11:45:33 -0700 | [diff] [blame] | 2331 | if (active) |
Hal Rosenstock | 03b61ad | 2005-07-27 11:45:32 -0700 | [diff] [blame] | 2332 | mad_send_wr->timeout = msecs_to_jiffies(timeout_ms); |
| 2333 | else |
| 2334 | ib_reset_mad_timeout(mad_send_wr, timeout_ms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | |
Hal Rosenstock | 03b61ad | 2005-07-27 11:45:32 -0700 | [diff] [blame] | 2336 | spin_unlock_irqrestore(&mad_agent_priv->lock, flags); |
| 2337 | return 0; |
| 2338 | } |
| 2339 | EXPORT_SYMBOL(ib_modify_mad); |
| 2340 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2341 | void ib_cancel_mad(struct ib_mad_agent *mad_agent, |
| 2342 | struct ib_mad_send_buf *send_buf) |
Hal Rosenstock | 03b61ad | 2005-07-27 11:45:32 -0700 | [diff] [blame] | 2343 | { |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2344 | ib_modify_mad(mad_agent, send_buf, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | } |
| 2346 | EXPORT_SYMBOL(ib_cancel_mad); |
| 2347 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 2348 | static void local_completions(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Ralph Campbell | 1d9bc6d6 | 2009-02-27 10:34:30 -0800 | [diff] [blame] | 2354 | int free_mad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2355 | struct ib_wc wc; |
| 2356 | struct ib_mad_send_wc mad_send_wc; |
| 2357 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 2358 | mad_agent_priv = |
| 2359 | container_of(work, struct ib_mad_agent_private, local_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Michael S. Tsirkin | 37289ef | 2006-03-30 15:52:54 +0200 | [diff] [blame] | 2366 | list_del(&local->completion_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | spin_unlock_irqrestore(&mad_agent_priv->lock, flags); |
Ralph Campbell | 1d9bc6d6 | 2009-02-27 10:34:30 -0800 | [diff] [blame] | 2368 | free_mad = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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"); |
Ralph Campbell | 1d9bc6d6 | 2009-02-27 10:34:30 -0800 | [diff] [blame] | 2373 | free_mad = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | goto local_send_completion; |
| 2375 | } |
| 2376 | |
| 2377 | /* |
| 2378 | * Defined behavior is to complete response |
| 2379 | * before request |
| 2380 | */ |
Michael S. Tsirkin | 062dbb6 | 2006-12-31 21:09:42 +0200 | [diff] [blame] | 2381 | build_smp_wc(recv_mad_agent->agent.qp, |
| 2382 | (unsigned long) local->mad_send_wr, |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 2383 | be16_to_cpu(IB_LID_PERMISSIVE), |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2384 | 0, recv_mad_agent->agent.port_num, &wc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2385 | |
| 2386 | local->mad_priv->header.recv_wc.wc = &wc; |
| 2387 | local->mad_priv->header.recv_wc.mad_len = |
| 2388 | sizeof(struct ib_mad); |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2411 | mad_send_wc.send_buf = &local->mad_send_wr->send_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2412 | if (atomic_read(&mad_agent_priv->qp_info->snoop_count)) |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2413 | snoop_send(mad_agent_priv->qp_info, |
| 2414 | &local->mad_send_wr->send_buf, |
| 2415 | &mad_send_wc, IB_MAD_SNOOP_SEND_COMPLETIONS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 | atomic_dec(&mad_agent_priv->refcount); |
Ralph Campbell | 1d9bc6d6 | 2009-02-27 10:34:30 -0800 | [diff] [blame] | 2421 | if (free_mad) |
Hal Rosenstock | 2c153b9 | 2005-07-27 11:45:31 -0700 | [diff] [blame] | 2422 | kmem_cache_free(ib_mad_cache, local->mad_priv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2423 | kfree(local); |
| 2424 | } |
| 2425 | spin_unlock_irqrestore(&mad_agent_priv->lock, flags); |
| 2426 | } |
| 2427 | |
Hal Rosenstock | f75b7a5 | 2005-07-27 11:45:29 -0700 | [diff] [blame] | 2428 | static int retry_send(struct ib_mad_send_wr_private *mad_send_wr) |
| 2429 | { |
| 2430 | int ret; |
| 2431 | |
Sean Hefty | 4fc8cd4 | 2007-11-27 00:11:04 -0800 | [diff] [blame] | 2432 | if (!mad_send_wr->retries_left) |
Hal Rosenstock | f75b7a5 | 2005-07-27 11:45:29 -0700 | [diff] [blame] | 2433 | return -ETIMEDOUT; |
| 2434 | |
Sean Hefty | 4fc8cd4 | 2007-11-27 00:11:04 -0800 | [diff] [blame] | 2435 | mad_send_wr->retries_left--; |
| 2436 | mad_send_wr->send_buf.retries++; |
| 2437 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2438 | mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms); |
Hal Rosenstock | f75b7a5 | 2005-07-27 11:45:29 -0700 | [diff] [blame] | 2439 | |
Hal Rosenstock | fa619a7 | 2005-07-27 11:45:37 -0700 | [diff] [blame] | 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); |
Hal Rosenstock | f75b7a5 | 2005-07-27 11:45:29 -0700 | [diff] [blame] | 2455 | |
| 2456 | if (!ret) { |
| 2457 | mad_send_wr->refcount++; |
Hal Rosenstock | f75b7a5 | 2005-07-27 11:45:29 -0700 | [diff] [blame] | 2458 | list_add_tail(&mad_send_wr->agent_list, |
| 2459 | &mad_send_wr->mad_agent_priv->send_list); |
| 2460 | } |
| 2461 | return ret; |
| 2462 | } |
| 2463 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 2464 | static void timeout_sends(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 2471 | mad_agent_priv = container_of(work, struct ib_mad_agent_private, |
| 2472 | timed_work.work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Hal Rosenstock | dbf9227 | 2005-07-27 11:45:30 -0700 | [diff] [blame] | 2491 | list_del(&mad_send_wr->agent_list); |
Hal Rosenstock | 29bb33d | 2005-07-27 11:45:32 -0700 | [diff] [blame] | 2492 | if (mad_send_wr->status == IB_WC_SUCCESS && |
| 2493 | !retry_send(mad_send_wr)) |
Hal Rosenstock | f75b7a5 | 2005-07-27 11:45:29 -0700 | [diff] [blame] | 2494 | continue; |
| 2495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2496 | spin_unlock_irqrestore(&mad_agent_priv->lock, flags); |
| 2497 | |
Hal Rosenstock | 03b61ad | 2005-07-27 11:45:32 -0700 | [diff] [blame] | 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; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 2502 | mad_send_wc.send_buf = &mad_send_wr->send_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | mad_agent_priv->agent.send_handler(&mad_agent_priv->agent, |
| 2504 | &mad_send_wc); |
| 2505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Hal Rosenstock | 5dd2ce1 | 2005-08-15 14:16:36 -0700 | [diff] [blame] | 2512 | static void ib_mad_thread_completion_handler(struct ib_cq *cq, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | { |
| 2514 | struct ib_mad_port_private *port_priv = cq->cq_context; |
Michael S. Tsirkin | dc05980 | 2006-03-20 10:08:25 -0800 | [diff] [blame] | 2515 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | |
Michael S. Tsirkin | dc05980 | 2006-03-20 10:08:25 -0800 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Ralph Campbell | 1527106 | 2006-12-12 14:28:30 -0800 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Ralph Campbell | 1527106 | 2006-12-12 14:28:30 -0800 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Eli Cohen | fac70d5 | 2010-09-27 17:51:11 -0700 | [diff] [blame] | 2601 | if (!qp_info->qp) |
| 2602 | return; |
| 2603 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Ralph Campbell | 1527106 | 2006-12-12 14:28:30 -0800 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Roland Dreier | 3cd9656 | 2006-09-22 15:22:46 -0700 | [diff] [blame] | 2638 | if (!attr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Eli Cohen | fac70d5 | 2010-09-27 17:51:11 -0700 | [diff] [blame] | 2645 | if (!qp) |
| 2646 | continue; |
| 2647 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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++) { |
Eli Cohen | fac70d5 | 2010-09-27 17:51:11 -0700 | [diff] [blame] | 2689 | if (!port_priv->qp_info[i].qp) |
| 2690 | continue; |
| 2691 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Hal Rosenstock | b76aabc | 2009-09-07 08:28:48 -0700 | [diff] [blame] | 2744 | qp_init_attr.cap.max_send_wr = mad_sendq_size; |
| 2745 | qp_init_attr.cap.max_recv_wr = mad_recvq_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Hal Rosenstock | b76aabc | 2009-09-07 08:28:48 -0700 | [diff] [blame] | 2760 | qp_info->send_queue.max_active = mad_sendq_size; |
| 2761 | qp_info->recv_queue.max_active = mad_recvq_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Eli Cohen | fac70d5 | 2010-09-27 17:51:11 -0700 | [diff] [blame] | 2770 | if (!qp_info->qp) |
| 2771 | return; |
| 2772 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2773 | ib_destroy_qp(qp_info->qp); |
Jesper Juhl | 6044ec8 | 2005-11-07 01:01:32 -0800 | [diff] [blame] | 2774 | kfree(qp_info->snoop_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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"]; |
Eli Cohen | fac70d5 | 2010-09-27 17:51:11 -0700 | [diff] [blame] | 2788 | int has_smi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | /* Create new device info */ |
Roland Dreier | de6eb66 | 2005-11-02 07:23:14 -0800 | [diff] [blame] | 2791 | port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | if (!port_priv) { |
| 2793 | printk(KERN_ERR PFX "No memory for ib_mad_port_private\n"); |
| 2794 | return -ENOMEM; |
| 2795 | } |
Roland Dreier | de6eb66 | 2005-11-02 07:23:14 -0800 | [diff] [blame] | 2796 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Eli Cohen | fac70d5 | 2010-09-27 17:51:11 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2809 | port_priv->cq = ib_create_cq(port_priv->device, |
Hal Rosenstock | 5dd2ce1 | 2005-08-15 14:16:36 -0700 | [diff] [blame] | 2810 | ib_mad_thread_completion_handler, |
Michael S. Tsirkin | f4fd0b2 | 2007-05-03 13:48:47 +0300 | [diff] [blame] | 2811 | NULL, port_priv, cq_size, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Eli Cohen | fac70d5 | 2010-09-27 17:51:11 -0700 | [diff] [blame] | 2832 | if (has_smi) { |
| 2833 | ret = create_mad_qp(&port_priv->qp_info[0], IB_QPT_SMI); |
| 2834 | if (ret) |
| 2835 | goto error6; |
| 2836 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 2847 | INIT_WORK(&port_priv->work, ib_mad_completion_handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2848 | |
Michael S. Tsirkin | dc05980 | 2006-03-20 10:08:25 -0800 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2859 | return 0; |
| 2860 | |
| 2861 | error9: |
Michael S. Tsirkin | dc05980 | 2006-03-20 10:08:25 -0800 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Michael S. Tsirkin | dc05980 | 2006-03-20 10:08:25 -0800 | [diff] [blame] | 2902 | list_del_init(&port_priv->port_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2903 | spin_unlock_irqrestore(&ib_mad_port_list_lock, flags); |
| 2904 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Roland Dreier | 4ab6fb7 | 2005-10-06 13:28:16 -0700 | [diff] [blame] | 2922 | int start, end, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 2924 | if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB) |
| 2925 | return; |
| 2926 | |
| 2927 | if (device->node_type == RDMA_NODE_IB_SWITCH) { |
Roland Dreier | 4ab6fb7 | 2005-10-06 13:28:16 -0700 | [diff] [blame] | 2928 | start = 0; |
| 2929 | end = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2930 | } else { |
Roland Dreier | 4ab6fb7 | 2005-10-06 13:28:16 -0700 | [diff] [blame] | 2931 | start = 1; |
| 2932 | end = device->phys_port_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2933 | } |
Roland Dreier | 4ab6fb7 | 2005-10-06 13:28:16 -0700 | [diff] [blame] | 2934 | |
| 2935 | for (i = start; i <= end; i++) { |
| 2936 | if (ib_mad_port_open(device, i)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2937 | printk(KERN_ERR PFX "Couldn't open %s port %d\n", |
Roland Dreier | 4ab6fb7 | 2005-10-06 13:28:16 -0700 | [diff] [blame] | 2938 | device->name, i); |
| 2939 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2940 | } |
Roland Dreier | 4ab6fb7 | 2005-10-06 13:28:16 -0700 | [diff] [blame] | 2941 | if (ib_agent_port_open(device, i)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2942 | printk(KERN_ERR PFX "Couldn't open %s port %d " |
| 2943 | "for agents\n", |
Roland Dreier | 4ab6fb7 | 2005-10-06 13:28:16 -0700 | [diff] [blame] | 2944 | device->name, i); |
| 2945 | goto error_agent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2946 | } |
| 2947 | } |
Hal Rosenstock | f68bcc2 | 2005-07-27 11:45:27 -0700 | [diff] [blame] | 2948 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2949 | |
Roland Dreier | 4ab6fb7 | 2005-10-06 13:28:16 -0700 | [diff] [blame] | 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)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2960 | printk(KERN_ERR PFX "Couldn't close %s port %d " |
| 2961 | "for agents\n", |
Roland Dreier | 4ab6fb7 | 2005-10-06 13:28:16 -0700 | [diff] [blame] | 2962 | device->name, i); |
| 2963 | if (ib_mad_port_close(device, i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2964 | printk(KERN_ERR PFX "Couldn't close %s port %d\n", |
Roland Dreier | 4ab6fb7 | 2005-10-06 13:28:16 -0700 | [diff] [blame] | 2965 | device->name, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2966 | i--; |
| 2967 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2968 | } |
| 2969 | |
| 2970 | static void ib_mad_remove_device(struct ib_device *device) |
| 2971 | { |
Hal Rosenstock | f68bcc2 | 2005-07-27 11:45:27 -0700 | [diff] [blame] | 2972 | int i, num_ports, cur_port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2973 | |
Steve Wise | 070e140 | 2010-03-04 18:18:18 +0000 | [diff] [blame] | 2974 | if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB) |
| 2975 | return; |
| 2976 | |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 2977 | if (device->node_type == RDMA_NODE_IB_SWITCH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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++) { |
Hal Rosenstock | f68bcc2 | 2005-07-27 11:45:27 -0700 | [diff] [blame] | 2985 | if (ib_agent_port_close(device, cur_port)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2986 | printk(KERN_ERR PFX "Couldn't close %s port %d " |
| 2987 | "for agents\n", |
| 2988 | device->name, cur_port); |
Hal Rosenstock | f68bcc2 | 2005-07-27 11:45:27 -0700 | [diff] [blame] | 2989 | if (ib_mad_port_close(device, cur_port)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2990 | printk(KERN_ERR PFX "Couldn't close %s port %d\n", |
| 2991 | device->name, cur_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Hal Rosenstock | b76aabc | 2009-09-07 08:28:48 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3011 | ib_mad_cache = kmem_cache_create("ib_mad", |
| 3012 | sizeof(struct ib_mad_private), |
| 3013 | 0, |
| 3014 | SLAB_HWCACHE_ALIGN, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Alexey Dobriyan | 1a1d92c | 2006-09-27 01:49:40 -0700 | [diff] [blame] | 3041 | kmem_cache_destroy(ib_mad_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | module_init(ib_mad_init_module); |
| 3045 | module_exit(ib_mad_cleanup_module); |