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