PageRenderTime 1495ms CodeModel.GetById 40ms RepoModel.GetById 1ms app.codeStats 1ms

/drivers/infiniband/core/mad.c

https://bitbucket.org/cresqo/cm7-p500-kernel
C | 3026 lines | 2389 code | 356 blank | 281 comment | 392 complexity | e99f6b5a2fd60ee78e8c3c34540713aa MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * Copyright (c) 2004-2007 Voltaire, Inc. All rights reserved.
  3. * Copyright (c) 2005 Intel Corporation. All rights reserved.
  4. * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved.
  5. * Copyright (c) 2009 HNR Consulting. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * OpenIB.org BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. *
  35. */
  36. #include <linux/dma-mapping.h>
  37. #include <linux/slab.h>
  38. #include <rdma/ib_cache.h>
  39. #include "mad_priv.h"
  40. #include "mad_rmpp.h"
  41. #include "smi.h"
  42. #include "agent.h"
  43. MODULE_LICENSE("Dual BSD/GPL");
  44. MODULE_DESCRIPTION("kernel IB MAD API");
  45. MODULE_AUTHOR("Hal Rosenstock");
  46. MODULE_AUTHOR("Sean Hefty");
  47. static int mad_sendq_size = IB_MAD_QP_SEND_SIZE;
  48. static int mad_recvq_size = IB_MAD_QP_RECV_SIZE;
  49. module_param_named(send_queue_size, mad_sendq_size, int, 0444);
  50. MODULE_PARM_DESC(send_queue_size, "Size of send queue in number of work requests");
  51. module_param_named(recv_queue_size, mad_recvq_size, int, 0444);
  52. MODULE_PARM_DESC(recv_queue_size, "Size of receive queue in number of work requests");
  53. static struct kmem_cache *ib_mad_cache;
  54. static struct list_head ib_mad_port_list;
  55. static u32 ib_mad_client_id = 0;
  56. /* Port list lock */
  57. static DEFINE_SPINLOCK(ib_mad_port_list_lock);
  58. /* Forward declarations */
  59. static int method_in_use(struct ib_mad_mgmt_method_table **method,
  60. struct ib_mad_reg_req *mad_reg_req);
  61. static void remove_mad_reg_req(struct ib_mad_agent_private *priv);
  62. static struct ib_mad_agent_private *find_mad_agent(
  63. struct ib_mad_port_private *port_priv,
  64. struct ib_mad *mad);
  65. static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
  66. struct ib_mad_private *mad);
  67. static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv);
  68. static void timeout_sends(struct work_struct *work);
  69. static void local_completions(struct work_struct *work);
  70. static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
  71. struct ib_mad_agent_private *agent_priv,
  72. u8 mgmt_class);
  73. static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
  74. struct ib_mad_agent_private *agent_priv);
  75. /*
  76. * Returns a ib_mad_port_private structure or NULL for a device/port
  77. * Assumes ib_mad_port_list_lock is being held
  78. */
  79. static inline struct ib_mad_port_private *
  80. __ib_get_mad_port(struct ib_device *device, int port_num)
  81. {
  82. struct ib_mad_port_private *entry;
  83. list_for_each_entry(entry, &ib_mad_port_list, port_list) {
  84. if (entry->device == device && entry->port_num == port_num)
  85. return entry;
  86. }
  87. return NULL;
  88. }
  89. /*
  90. * Wrapper function to return a ib_mad_port_private structure or NULL
  91. * for a device/port
  92. */
  93. static inline struct ib_mad_port_private *
  94. ib_get_mad_port(struct ib_device *device, int port_num)
  95. {
  96. struct ib_mad_port_private *entry;
  97. unsigned long flags;
  98. spin_lock_irqsave(&ib_mad_port_list_lock, flags);
  99. entry = __ib_get_mad_port(device, port_num);
  100. spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
  101. return entry;
  102. }
  103. static inline u8 convert_mgmt_class(u8 mgmt_class)
  104. {
  105. /* Alias IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE to 0 */
  106. return mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ?
  107. 0 : mgmt_class;
  108. }
  109. static int get_spl_qp_index(enum ib_qp_type qp_type)
  110. {
  111. switch (qp_type)
  112. {
  113. case IB_QPT_SMI:
  114. return 0;
  115. case IB_QPT_GSI:
  116. return 1;
  117. default:
  118. return -1;
  119. }
  120. }
  121. static int vendor_class_index(u8 mgmt_class)
  122. {
  123. return mgmt_class - IB_MGMT_CLASS_VENDOR_RANGE2_START;
  124. }
  125. static int is_vendor_class(u8 mgmt_class)
  126. {
  127. if ((mgmt_class < IB_MGMT_CLASS_VENDOR_RANGE2_START) ||
  128. (mgmt_class > IB_MGMT_CLASS_VENDOR_RANGE2_END))
  129. return 0;
  130. return 1;
  131. }
  132. static int is_vendor_oui(char *oui)
  133. {
  134. if (oui[0] || oui[1] || oui[2])
  135. return 1;
  136. return 0;
  137. }
  138. static int is_vendor_method_in_use(
  139. struct ib_mad_mgmt_vendor_class *vendor_class,
  140. struct ib_mad_reg_req *mad_reg_req)
  141. {
  142. struct ib_mad_mgmt_method_table *method;
  143. int i;
  144. for (i = 0; i < MAX_MGMT_OUI; i++) {
  145. if (!memcmp(vendor_class->oui[i], mad_reg_req->oui, 3)) {
  146. method = vendor_class->method_table[i];
  147. if (method) {
  148. if (method_in_use(&method, mad_reg_req))
  149. return 1;
  150. else
  151. break;
  152. }
  153. }
  154. }
  155. return 0;
  156. }
  157. int ib_response_mad(struct ib_mad *mad)
  158. {
  159. return ((mad->mad_hdr.method & IB_MGMT_METHOD_RESP) ||
  160. (mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS) ||
  161. ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_BM) &&
  162. (mad->mad_hdr.attr_mod & IB_BM_ATTR_MOD_RESP)));
  163. }
  164. EXPORT_SYMBOL(ib_response_mad);
  165. /*
  166. * ib_register_mad_agent - Register to send/receive MADs
  167. */
  168. struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
  169. u8 port_num,
  170. enum ib_qp_type qp_type,
  171. struct ib_mad_reg_req *mad_reg_req,
  172. u8 rmpp_version,
  173. ib_mad_send_handler send_handler,
  174. ib_mad_recv_handler recv_handler,
  175. void *context)
  176. {
  177. struct ib_mad_port_private *port_priv;
  178. struct ib_mad_agent *ret = ERR_PTR(-EINVAL);
  179. struct ib_mad_agent_private *mad_agent_priv;
  180. struct ib_mad_reg_req *reg_req = NULL;
  181. struct ib_mad_mgmt_class_table *class;
  182. struct ib_mad_mgmt_vendor_class_table *vendor;
  183. struct ib_mad_mgmt_vendor_class *vendor_class;
  184. struct ib_mad_mgmt_method_table *method;
  185. int ret2, qpn;
  186. unsigned long flags;
  187. u8 mgmt_class, vclass;
  188. /* Validate parameters */
  189. qpn = get_spl_qp_index(qp_type);
  190. if (qpn == -1)
  191. goto error1;
  192. if (rmpp_version && rmpp_version != IB_MGMT_RMPP_VERSION)
  193. goto error1;
  194. /* Validate MAD registration request if supplied */
  195. if (mad_reg_req) {
  196. if (mad_reg_req->mgmt_class_version >= MAX_MGMT_VERSION)
  197. goto error1;
  198. if (!recv_handler)
  199. goto error1;
  200. if (mad_reg_req->mgmt_class >= MAX_MGMT_CLASS) {
  201. /*
  202. * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE is the only
  203. * one in this range currently allowed
  204. */
  205. if (mad_reg_req->mgmt_class !=
  206. IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
  207. goto error1;
  208. } else if (mad_reg_req->mgmt_class == 0) {
  209. /*
  210. * Class 0 is reserved in IBA and is used for
  211. * aliasing of IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
  212. */
  213. goto error1;
  214. } else if (is_vendor_class(mad_reg_req->mgmt_class)) {
  215. /*
  216. * If class is in "new" vendor range,
  217. * ensure supplied OUI is not zero
  218. */
  219. if (!is_vendor_oui(mad_reg_req->oui))
  220. goto error1;
  221. }
  222. /* Make sure class supplied is consistent with RMPP */
  223. if (!ib_is_mad_class_rmpp(mad_reg_req->mgmt_class)) {
  224. if (rmpp_version)
  225. goto error1;
  226. }
  227. /* Make sure class supplied is consistent with QP type */
  228. if (qp_type == IB_QPT_SMI) {
  229. if ((mad_reg_req->mgmt_class !=
  230. IB_MGMT_CLASS_SUBN_LID_ROUTED) &&
  231. (mad_reg_req->mgmt_class !=
  232. IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
  233. goto error1;
  234. } else {
  235. if ((mad_reg_req->mgmt_class ==
  236. IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
  237. (mad_reg_req->mgmt_class ==
  238. IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
  239. goto error1;
  240. }
  241. } else {
  242. /* No registration request supplied */
  243. if (!send_handler)
  244. goto error1;
  245. }
  246. /* Validate device and port */
  247. port_priv = ib_get_mad_port(device, port_num);
  248. if (!port_priv) {
  249. ret = ERR_PTR(-ENODEV);
  250. goto error1;
  251. }
  252. /* Allocate structures */
  253. mad_agent_priv = kzalloc(sizeof *mad_agent_priv, GFP_KERNEL);
  254. if (!mad_agent_priv) {
  255. ret = ERR_PTR(-ENOMEM);
  256. goto error1;
  257. }
  258. mad_agent_priv->agent.mr = ib_get_dma_mr(port_priv->qp_info[qpn].qp->pd,
  259. IB_ACCESS_LOCAL_WRITE);
  260. if (IS_ERR(mad_agent_priv->agent.mr)) {
  261. ret = ERR_PTR(-ENOMEM);
  262. goto error2;
  263. }
  264. if (mad_reg_req) {
  265. reg_req = kmemdup(mad_reg_req, sizeof *reg_req, GFP_KERNEL);
  266. if (!reg_req) {
  267. ret = ERR_PTR(-ENOMEM);
  268. goto error3;
  269. }
  270. }
  271. /* Now, fill in the various structures */
  272. mad_agent_priv->qp_info = &port_priv->qp_info[qpn];
  273. mad_agent_priv->reg_req = reg_req;
  274. mad_agent_priv->agent.rmpp_version = rmpp_version;
  275. mad_agent_priv->agent.device = device;
  276. mad_agent_priv->agent.recv_handler = recv_handler;
  277. mad_agent_priv->agent.send_handler = send_handler;
  278. mad_agent_priv->agent.context = context;
  279. mad_agent_priv->agent.qp = port_priv->qp_info[qpn].qp;
  280. mad_agent_priv->agent.port_num = port_num;
  281. spin_lock_init(&mad_agent_priv->lock);
  282. INIT_LIST_HEAD(&mad_agent_priv->send_list);
  283. INIT_LIST_HEAD(&mad_agent_priv->wait_list);
  284. INIT_LIST_HEAD(&mad_agent_priv->done_list);
  285. INIT_LIST_HEAD(&mad_agent_priv->rmpp_list);
  286. INIT_DELAYED_WORK(&mad_agent_priv->timed_work, timeout_sends);
  287. INIT_LIST_HEAD(&mad_agent_priv->local_list);
  288. INIT_WORK(&mad_agent_priv->local_work, local_completions);
  289. atomic_set(&mad_agent_priv->refcount, 1);
  290. init_completion(&mad_agent_priv->comp);
  291. spin_lock_irqsave(&port_priv->reg_lock, flags);
  292. mad_agent_priv->agent.hi_tid = ++ib_mad_client_id;
  293. /*
  294. * Make sure MAD registration (if supplied)
  295. * is non overlapping with any existing ones
  296. */
  297. if (mad_reg_req) {
  298. mgmt_class = convert_mgmt_class(mad_reg_req->mgmt_class);
  299. if (!is_vendor_class(mgmt_class)) {
  300. class = port_priv->version[mad_reg_req->
  301. mgmt_class_version].class;
  302. if (class) {
  303. method = class->method_table[mgmt_class];
  304. if (method) {
  305. if (method_in_use(&method,
  306. mad_reg_req))
  307. goto error4;
  308. }
  309. }
  310. ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv,
  311. mgmt_class);
  312. } else {
  313. /* "New" vendor class range */
  314. vendor = port_priv->version[mad_reg_req->
  315. mgmt_class_version].vendor;
  316. if (vendor) {
  317. vclass = vendor_class_index(mgmt_class);
  318. vendor_class = vendor->vendor_class[vclass];
  319. if (vendor_class) {
  320. if (is_vendor_method_in_use(
  321. vendor_class,
  322. mad_reg_req))
  323. goto error4;
  324. }
  325. }
  326. ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv);
  327. }
  328. if (ret2) {
  329. ret = ERR_PTR(ret2);
  330. goto error4;
  331. }
  332. }
  333. /* Add mad agent into port's agent list */
  334. list_add_tail(&mad_agent_priv->agent_list, &port_priv->agent_list);
  335. spin_unlock_irqrestore(&port_priv->reg_lock, flags);
  336. return &mad_agent_priv->agent;
  337. error4:
  338. spin_unlock_irqrestore(&port_priv->reg_lock, flags);
  339. kfree(reg_req);
  340. error3:
  341. ib_dereg_mr(mad_agent_priv->agent.mr);
  342. error2:
  343. kfree(mad_agent_priv);
  344. error1:
  345. return ret;
  346. }
  347. EXPORT_SYMBOL(ib_register_mad_agent);
  348. static inline int is_snooping_sends(int mad_snoop_flags)
  349. {
  350. return (mad_snoop_flags &
  351. (/*IB_MAD_SNOOP_POSTED_SENDS |
  352. IB_MAD_SNOOP_RMPP_SENDS |*/
  353. IB_MAD_SNOOP_SEND_COMPLETIONS /*|
  354. IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/));
  355. }
  356. static inline int is_snooping_recvs(int mad_snoop_flags)
  357. {
  358. return (mad_snoop_flags &
  359. (IB_MAD_SNOOP_RECVS /*|
  360. IB_MAD_SNOOP_RMPP_RECVS*/));
  361. }
  362. static int register_snoop_agent(struct ib_mad_qp_info *qp_info,
  363. struct ib_mad_snoop_private *mad_snoop_priv)
  364. {
  365. struct ib_mad_snoop_private **new_snoop_table;
  366. unsigned long flags;
  367. int i;
  368. spin_lock_irqsave(&qp_info->snoop_lock, flags);
  369. /* Check for empty slot in array. */
  370. for (i = 0; i < qp_info->snoop_table_size; i++)
  371. if (!qp_info->snoop_table[i])
  372. break;
  373. if (i == qp_info->snoop_table_size) {
  374. /* Grow table. */
  375. new_snoop_table = krealloc(qp_info->snoop_table,
  376. sizeof mad_snoop_priv *
  377. (qp_info->snoop_table_size + 1),
  378. GFP_ATOMIC);
  379. if (!new_snoop_table) {
  380. i = -ENOMEM;
  381. goto out;
  382. }
  383. qp_info->snoop_table = new_snoop_table;
  384. qp_info->snoop_table_size++;
  385. }
  386. qp_info->snoop_table[i] = mad_snoop_priv;
  387. atomic_inc(&qp_info->snoop_count);
  388. out:
  389. spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
  390. return i;
  391. }
  392. struct ib_mad_agent *ib_register_mad_snoop(struct ib_device *device,
  393. u8 port_num,
  394. enum ib_qp_type qp_type,
  395. int mad_snoop_flags,
  396. ib_mad_snoop_handler snoop_handler,
  397. ib_mad_recv_handler recv_handler,
  398. void *context)
  399. {
  400. struct ib_mad_port_private *port_priv;
  401. struct ib_mad_agent *ret;
  402. struct ib_mad_snoop_private *mad_snoop_priv;
  403. int qpn;
  404. /* Validate parameters */
  405. if ((is_snooping_sends(mad_snoop_flags) && !snoop_handler) ||
  406. (is_snooping_recvs(mad_snoop_flags) && !recv_handler)) {
  407. ret = ERR_PTR(-EINVAL);
  408. goto error1;
  409. }
  410. qpn = get_spl_qp_index(qp_type);
  411. if (qpn == -1) {
  412. ret = ERR_PTR(-EINVAL);
  413. goto error1;
  414. }
  415. port_priv = ib_get_mad_port(device, port_num);
  416. if (!port_priv) {
  417. ret = ERR_PTR(-ENODEV);
  418. goto error1;
  419. }
  420. /* Allocate structures */
  421. mad_snoop_priv = kzalloc(sizeof *mad_snoop_priv, GFP_KERNEL);
  422. if (!mad_snoop_priv) {
  423. ret = ERR_PTR(-ENOMEM);
  424. goto error1;
  425. }
  426. /* Now, fill in the various structures */
  427. mad_snoop_priv->qp_info = &port_priv->qp_info[qpn];
  428. mad_snoop_priv->agent.device = device;
  429. mad_snoop_priv->agent.recv_handler = recv_handler;
  430. mad_snoop_priv->agent.snoop_handler = snoop_handler;
  431. mad_snoop_priv->agent.context = context;
  432. mad_snoop_priv->agent.qp = port_priv->qp_info[qpn].qp;
  433. mad_snoop_priv->agent.port_num = port_num;
  434. mad_snoop_priv->mad_snoop_flags = mad_snoop_flags;
  435. init_completion(&mad_snoop_priv->comp);
  436. mad_snoop_priv->snoop_index = register_snoop_agent(
  437. &port_priv->qp_info[qpn],
  438. mad_snoop_priv);
  439. if (mad_snoop_priv->snoop_index < 0) {
  440. ret = ERR_PTR(mad_snoop_priv->snoop_index);
  441. goto error2;
  442. }
  443. atomic_set(&mad_snoop_priv->refcount, 1);
  444. return &mad_snoop_priv->agent;
  445. error2:
  446. kfree(mad_snoop_priv);
  447. error1:
  448. return ret;
  449. }
  450. EXPORT_SYMBOL(ib_register_mad_snoop);
  451. static inline void deref_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
  452. {
  453. if (atomic_dec_and_test(&mad_agent_priv->refcount))
  454. complete(&mad_agent_priv->comp);
  455. }
  456. static inline void deref_snoop_agent(struct ib_mad_snoop_private *mad_snoop_priv)
  457. {
  458. if (atomic_dec_and_test(&mad_snoop_priv->refcount))
  459. complete(&mad_snoop_priv->comp);
  460. }
  461. static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
  462. {
  463. struct ib_mad_port_private *port_priv;
  464. unsigned long flags;
  465. /* Note that we could still be handling received MADs */
  466. /*
  467. * Canceling all sends results in dropping received response
  468. * MADs, preventing us from queuing additional work
  469. */
  470. cancel_mads(mad_agent_priv);
  471. port_priv = mad_agent_priv->qp_info->port_priv;
  472. cancel_delayed_work(&mad_agent_priv->timed_work);
  473. spin_lock_irqsave(&port_priv->reg_lock, flags);
  474. remove_mad_reg_req(mad_agent_priv);
  475. list_del(&mad_agent_priv->agent_list);
  476. spin_unlock_irqrestore(&port_priv->reg_lock, flags);
  477. flush_workqueue(port_priv->wq);
  478. ib_cancel_rmpp_recvs(mad_agent_priv);
  479. deref_mad_agent(mad_agent_priv);
  480. wait_for_completion(&mad_agent_priv->comp);
  481. kfree(mad_agent_priv->reg_req);
  482. ib_dereg_mr(mad_agent_priv->agent.mr);
  483. kfree(mad_agent_priv);
  484. }
  485. static void unregister_mad_snoop(struct ib_mad_snoop_private *mad_snoop_priv)
  486. {
  487. struct ib_mad_qp_info *qp_info;
  488. unsigned long flags;
  489. qp_info = mad_snoop_priv->qp_info;
  490. spin_lock_irqsave(&qp_info->snoop_lock, flags);
  491. qp_info->snoop_table[mad_snoop_priv->snoop_index] = NULL;
  492. atomic_dec(&qp_info->snoop_count);
  493. spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
  494. deref_snoop_agent(mad_snoop_priv);
  495. wait_for_completion(&mad_snoop_priv->comp);
  496. kfree(mad_snoop_priv);
  497. }
  498. /*
  499. * ib_unregister_mad_agent - Unregisters a client from using MAD services
  500. */
  501. int ib_unregister_mad_agent(struct ib_mad_agent *mad_agent)
  502. {
  503. struct ib_mad_agent_private *mad_agent_priv;
  504. struct ib_mad_snoop_private *mad_snoop_priv;
  505. /* If the TID is zero, the agent can only snoop. */
  506. if (mad_agent->hi_tid) {
  507. mad_agent_priv = container_of(mad_agent,
  508. struct ib_mad_agent_private,
  509. agent);
  510. unregister_mad_agent(mad_agent_priv);
  511. } else {
  512. mad_snoop_priv = container_of(mad_agent,
  513. struct ib_mad_snoop_private,
  514. agent);
  515. unregister_mad_snoop(mad_snoop_priv);
  516. }
  517. return 0;
  518. }
  519. EXPORT_SYMBOL(ib_unregister_mad_agent);
  520. static void dequeue_mad(struct ib_mad_list_head *mad_list)
  521. {
  522. struct ib_mad_queue *mad_queue;
  523. unsigned long flags;
  524. BUG_ON(!mad_list->mad_queue);
  525. mad_queue = mad_list->mad_queue;
  526. spin_lock_irqsave(&mad_queue->lock, flags);
  527. list_del(&mad_list->list);
  528. mad_queue->count--;
  529. spin_unlock_irqrestore(&mad_queue->lock, flags);
  530. }
  531. static void snoop_send(struct ib_mad_qp_info *qp_info,
  532. struct ib_mad_send_buf *send_buf,
  533. struct ib_mad_send_wc *mad_send_wc,
  534. int mad_snoop_flags)
  535. {
  536. struct ib_mad_snoop_private *mad_snoop_priv;
  537. unsigned long flags;
  538. int i;
  539. spin_lock_irqsave(&qp_info->snoop_lock, flags);
  540. for (i = 0; i < qp_info->snoop_table_size; i++) {
  541. mad_snoop_priv = qp_info->snoop_table[i];
  542. if (!mad_snoop_priv ||
  543. !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
  544. continue;
  545. atomic_inc(&mad_snoop_priv->refcount);
  546. spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
  547. mad_snoop_priv->agent.snoop_handler(&mad_snoop_priv->agent,
  548. send_buf, mad_send_wc);
  549. deref_snoop_agent(mad_snoop_priv);
  550. spin_lock_irqsave(&qp_info->snoop_lock, flags);
  551. }
  552. spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
  553. }
  554. static void snoop_recv(struct ib_mad_qp_info *qp_info,
  555. struct ib_mad_recv_wc *mad_recv_wc,
  556. int mad_snoop_flags)
  557. {
  558. struct ib_mad_snoop_private *mad_snoop_priv;
  559. unsigned long flags;
  560. int i;
  561. spin_lock_irqsave(&qp_info->snoop_lock, flags);
  562. for (i = 0; i < qp_info->snoop_table_size; i++) {
  563. mad_snoop_priv = qp_info->snoop_table[i];
  564. if (!mad_snoop_priv ||
  565. !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
  566. continue;
  567. atomic_inc(&mad_snoop_priv->refcount);
  568. spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
  569. mad_snoop_priv->agent.recv_handler(&mad_snoop_priv->agent,
  570. mad_recv_wc);
  571. deref_snoop_agent(mad_snoop_priv);
  572. spin_lock_irqsave(&qp_info->snoop_lock, flags);
  573. }
  574. spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
  575. }
  576. static void build_smp_wc(struct ib_qp *qp,
  577. u64 wr_id, u16 slid, u16 pkey_index, u8 port_num,
  578. struct ib_wc *wc)
  579. {
  580. memset(wc, 0, sizeof *wc);
  581. wc->wr_id = wr_id;
  582. wc->status = IB_WC_SUCCESS;
  583. wc->opcode = IB_WC_RECV;
  584. wc->pkey_index = pkey_index;
  585. wc->byte_len = sizeof(struct ib_mad) + sizeof(struct ib_grh);
  586. wc->src_qp = IB_QP0;
  587. wc->qp = qp;
  588. wc->slid = slid;
  589. wc->sl = 0;
  590. wc->dlid_path_bits = 0;
  591. wc->port_num = port_num;
  592. }
  593. /*
  594. * Return 0 if SMP is to be sent
  595. * Return 1 if SMP was consumed locally (whether or not solicited)
  596. * Return < 0 if error
  597. */
  598. static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
  599. struct ib_mad_send_wr_private *mad_send_wr)
  600. {
  601. int ret = 0;
  602. struct ib_smp *smp = mad_send_wr->send_buf.mad;
  603. unsigned long flags;
  604. struct ib_mad_local_private *local;
  605. struct ib_mad_private *mad_priv;
  606. struct ib_mad_port_private *port_priv;
  607. struct ib_mad_agent_private *recv_mad_agent = NULL;
  608. struct ib_device *device = mad_agent_priv->agent.device;
  609. u8 port_num;
  610. struct ib_wc mad_wc;
  611. struct ib_send_wr *send_wr = &mad_send_wr->send_wr;
  612. if (device->node_type == RDMA_NODE_IB_SWITCH &&
  613. smp->mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
  614. port_num = send_wr->wr.ud.port_num;
  615. else
  616. port_num = mad_agent_priv->agent.port_num;
  617. /*
  618. * Directed route handling starts if the initial LID routed part of
  619. * a request or the ending LID routed part of a response is empty.
  620. * If we are at the start of the LID routed part, don't update the
  621. * hop_ptr or hop_cnt. See section 14.2.2, Vol 1 IB spec.
  622. */
  623. if ((ib_get_smp_direction(smp) ? smp->dr_dlid : smp->dr_slid) ==
  624. IB_LID_PERMISSIVE &&
  625. smi_handle_dr_smp_send(smp, device->node_type, port_num) ==
  626. IB_SMI_DISCARD) {
  627. ret = -EINVAL;
  628. printk(KERN_ERR PFX "Invalid directed route\n");
  629. goto out;
  630. }
  631. /* Check to post send on QP or process locally */
  632. if (smi_check_local_smp(smp, device) == IB_SMI_DISCARD &&
  633. smi_check_local_returning_smp(smp, device) == IB_SMI_DISCARD)
  634. goto out;
  635. local = kmalloc(sizeof *local, GFP_ATOMIC);
  636. if (!local) {
  637. ret = -ENOMEM;
  638. printk(KERN_ERR PFX "No memory for ib_mad_local_private\n");
  639. goto out;
  640. }
  641. local->mad_priv = NULL;
  642. local->recv_mad_agent = NULL;
  643. mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_ATOMIC);
  644. if (!mad_priv) {
  645. ret = -ENOMEM;
  646. printk(KERN_ERR PFX "No memory for local response MAD\n");
  647. kfree(local);
  648. goto out;
  649. }
  650. build_smp_wc(mad_agent_priv->agent.qp,
  651. send_wr->wr_id, be16_to_cpu(smp->dr_slid),
  652. send_wr->wr.ud.pkey_index,
  653. send_wr->wr.ud.port_num, &mad_wc);
  654. /* No GRH for DR SMP */
  655. ret = device->process_mad(device, 0, port_num, &mad_wc, NULL,
  656. (struct ib_mad *)smp,
  657. (struct ib_mad *)&mad_priv->mad);
  658. switch (ret)
  659. {
  660. case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY:
  661. if (ib_response_mad(&mad_priv->mad.mad) &&
  662. mad_agent_priv->agent.recv_handler) {
  663. local->mad_priv = mad_priv;
  664. local->recv_mad_agent = mad_agent_priv;
  665. /*
  666. * Reference MAD agent until receive
  667. * side of local completion handled
  668. */
  669. atomic_inc(&mad_agent_priv->refcount);
  670. } else
  671. kmem_cache_free(ib_mad_cache, mad_priv);
  672. break;
  673. case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED:
  674. kmem_cache_free(ib_mad_cache, mad_priv);
  675. break;
  676. case IB_MAD_RESULT_SUCCESS:
  677. /* Treat like an incoming receive MAD */
  678. port_priv = ib_get_mad_port(mad_agent_priv->agent.device,
  679. mad_agent_priv->agent.port_num);
  680. if (port_priv) {
  681. memcpy(&mad_priv->mad.mad, smp, sizeof(struct ib_mad));
  682. recv_mad_agent = find_mad_agent(port_priv,
  683. &mad_priv->mad.mad);
  684. }
  685. if (!port_priv || !recv_mad_agent) {
  686. /*
  687. * No receiving agent so drop packet and
  688. * generate send completion.
  689. */
  690. kmem_cache_free(ib_mad_cache, mad_priv);
  691. break;
  692. }
  693. local->mad_priv = mad_priv;
  694. local->recv_mad_agent = recv_mad_agent;
  695. break;
  696. default:
  697. kmem_cache_free(ib_mad_cache, mad_priv);
  698. kfree(local);
  699. ret = -EINVAL;
  700. goto out;
  701. }
  702. local->mad_send_wr = mad_send_wr;
  703. /* Reference MAD agent until send side of local completion handled */
  704. atomic_inc(&mad_agent_priv->refcount);
  705. /* Queue local completion to local list */
  706. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  707. list_add_tail(&local->completion_list, &mad_agent_priv->local_list);
  708. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  709. queue_work(mad_agent_priv->qp_info->port_priv->wq,
  710. &mad_agent_priv->local_work);
  711. ret = 1;
  712. out:
  713. return ret;
  714. }
  715. static int get_pad_size(int hdr_len, int data_len)
  716. {
  717. int seg_size, pad;
  718. seg_size = sizeof(struct ib_mad) - hdr_len;
  719. if (data_len && seg_size) {
  720. pad = seg_size - data_len % seg_size;
  721. return pad == seg_size ? 0 : pad;
  722. } else
  723. return seg_size;
  724. }
  725. static void free_send_rmpp_list(struct ib_mad_send_wr_private *mad_send_wr)
  726. {
  727. struct ib_rmpp_segment *s, *t;
  728. list_for_each_entry_safe(s, t, &mad_send_wr->rmpp_list, list) {
  729. list_del(&s->list);
  730. kfree(s);
  731. }
  732. }
  733. static int alloc_send_rmpp_list(struct ib_mad_send_wr_private *send_wr,
  734. gfp_t gfp_mask)
  735. {
  736. struct ib_mad_send_buf *send_buf = &send_wr->send_buf;
  737. struct ib_rmpp_mad *rmpp_mad = send_buf->mad;
  738. struct ib_rmpp_segment *seg = NULL;
  739. int left, seg_size, pad;
  740. send_buf->seg_size = sizeof (struct ib_mad) - send_buf->hdr_len;
  741. seg_size = send_buf->seg_size;
  742. pad = send_wr->pad;
  743. /* Allocate data segments. */
  744. for (left = send_buf->data_len + pad; left > 0; left -= seg_size) {
  745. seg = kmalloc(sizeof (*seg) + seg_size, gfp_mask);
  746. if (!seg) {
  747. printk(KERN_ERR "alloc_send_rmpp_segs: RMPP mem "
  748. "alloc failed for len %zd, gfp %#x\n",
  749. sizeof (*seg) + seg_size, gfp_mask);
  750. free_send_rmpp_list(send_wr);
  751. return -ENOMEM;
  752. }
  753. seg->num = ++send_buf->seg_count;
  754. list_add_tail(&seg->list, &send_wr->rmpp_list);
  755. }
  756. /* Zero any padding */
  757. if (pad)
  758. memset(seg->data + seg_size - pad, 0, pad);
  759. rmpp_mad->rmpp_hdr.rmpp_version = send_wr->mad_agent_priv->
  760. agent.rmpp_version;
  761. rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_DATA;
  762. ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
  763. send_wr->cur_seg = container_of(send_wr->rmpp_list.next,
  764. struct ib_rmpp_segment, list);
  765. send_wr->last_ack_seg = send_wr->cur_seg;
  766. return 0;
  767. }
  768. struct ib_mad_send_buf * ib_create_send_mad(struct ib_mad_agent *mad_agent,
  769. u32 remote_qpn, u16 pkey_index,
  770. int rmpp_active,
  771. int hdr_len, int data_len,
  772. gfp_t gfp_mask)
  773. {
  774. struct ib_mad_agent_private *mad_agent_priv;
  775. struct ib_mad_send_wr_private *mad_send_wr;
  776. int pad, message_size, ret, size;
  777. void *buf;
  778. mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
  779. agent);
  780. pad = get_pad_size(hdr_len, data_len);
  781. message_size = hdr_len + data_len + pad;
  782. if ((!mad_agent->rmpp_version &&
  783. (rmpp_active || message_size > sizeof(struct ib_mad))) ||
  784. (!rmpp_active && message_size > sizeof(struct ib_mad)))
  785. return ERR_PTR(-EINVAL);
  786. size = rmpp_active ? hdr_len : sizeof(struct ib_mad);
  787. buf = kzalloc(sizeof *mad_send_wr + size, gfp_mask);
  788. if (!buf)
  789. return ERR_PTR(-ENOMEM);
  790. mad_send_wr = buf + size;
  791. INIT_LIST_HEAD(&mad_send_wr->rmpp_list);
  792. mad_send_wr->send_buf.mad = buf;
  793. mad_send_wr->send_buf.hdr_len = hdr_len;
  794. mad_send_wr->send_buf.data_len = data_len;
  795. mad_send_wr->pad = pad;
  796. mad_send_wr->mad_agent_priv = mad_agent_priv;
  797. mad_send_wr->sg_list[0].length = hdr_len;
  798. mad_send_wr->sg_list[0].lkey = mad_agent->mr->lkey;
  799. mad_send_wr->sg_list[1].length = sizeof(struct ib_mad) - hdr_len;
  800. mad_send_wr->sg_list[1].lkey = mad_agent->mr->lkey;
  801. mad_send_wr->send_wr.wr_id = (unsigned long) mad_send_wr;
  802. mad_send_wr->send_wr.sg_list = mad_send_wr->sg_list;
  803. mad_send_wr->send_wr.num_sge = 2;
  804. mad_send_wr->send_wr.opcode = IB_WR_SEND;
  805. mad_send_wr->send_wr.send_flags = IB_SEND_SIGNALED;
  806. mad_send_wr->send_wr.wr.ud.remote_qpn = remote_qpn;
  807. mad_send_wr->send_wr.wr.ud.remote_qkey = IB_QP_SET_QKEY;
  808. mad_send_wr->send_wr.wr.ud.pkey_index = pkey_index;
  809. if (rmpp_active) {
  810. ret = alloc_send_rmpp_list(mad_send_wr, gfp_mask);
  811. if (ret) {
  812. kfree(buf);
  813. return ERR_PTR(ret);
  814. }
  815. }
  816. mad_send_wr->send_buf.mad_agent = mad_agent;
  817. atomic_inc(&mad_agent_priv->refcount);
  818. return &mad_send_wr->send_buf;
  819. }
  820. EXPORT_SYMBOL(ib_create_send_mad);
  821. int ib_get_mad_data_offset(u8 mgmt_class)
  822. {
  823. if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
  824. return IB_MGMT_SA_HDR;
  825. else if ((mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
  826. (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
  827. (mgmt_class == IB_MGMT_CLASS_BIS))
  828. return IB_MGMT_DEVICE_HDR;
  829. else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
  830. (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
  831. return IB_MGMT_VENDOR_HDR;
  832. else
  833. return IB_MGMT_MAD_HDR;
  834. }
  835. EXPORT_SYMBOL(ib_get_mad_data_offset);
  836. int ib_is_mad_class_rmpp(u8 mgmt_class)
  837. {
  838. if ((mgmt_class == IB_MGMT_CLASS_SUBN_ADM) ||
  839. (mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
  840. (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
  841. (mgmt_class == IB_MGMT_CLASS_BIS) ||
  842. ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
  843. (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)))
  844. return 1;
  845. return 0;
  846. }
  847. EXPORT_SYMBOL(ib_is_mad_class_rmpp);
  848. void *ib_get_rmpp_segment(struct ib_mad_send_buf *send_buf, int seg_num)
  849. {
  850. struct ib_mad_send_wr_private *mad_send_wr;
  851. struct list_head *list;
  852. mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
  853. send_buf);
  854. list = &mad_send_wr->cur_seg->list;
  855. if (mad_send_wr->cur_seg->num < seg_num) {
  856. list_for_each_entry(mad_send_wr->cur_seg, list, list)
  857. if (mad_send_wr->cur_seg->num == seg_num)
  858. break;
  859. } else if (mad_send_wr->cur_seg->num > seg_num) {
  860. list_for_each_entry_reverse(mad_send_wr->cur_seg, list, list)
  861. if (mad_send_wr->cur_seg->num == seg_num)
  862. break;
  863. }
  864. return mad_send_wr->cur_seg->data;
  865. }
  866. EXPORT_SYMBOL(ib_get_rmpp_segment);
  867. static inline void *ib_get_payload(struct ib_mad_send_wr_private *mad_send_wr)
  868. {
  869. if (mad_send_wr->send_buf.seg_count)
  870. return ib_get_rmpp_segment(&mad_send_wr->send_buf,
  871. mad_send_wr->seg_num);
  872. else
  873. return mad_send_wr->send_buf.mad +
  874. mad_send_wr->send_buf.hdr_len;
  875. }
  876. void ib_free_send_mad(struct ib_mad_send_buf *send_buf)
  877. {
  878. struct ib_mad_agent_private *mad_agent_priv;
  879. struct ib_mad_send_wr_private *mad_send_wr;
  880. mad_agent_priv = container_of(send_buf->mad_agent,
  881. struct ib_mad_agent_private, agent);
  882. mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
  883. send_buf);
  884. free_send_rmpp_list(mad_send_wr);
  885. kfree(send_buf->mad);
  886. deref_mad_agent(mad_agent_priv);
  887. }
  888. EXPORT_SYMBOL(ib_free_send_mad);
  889. int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr)
  890. {
  891. struct ib_mad_qp_info *qp_info;
  892. struct list_head *list;
  893. struct ib_send_wr *bad_send_wr;
  894. struct ib_mad_agent *mad_agent;
  895. struct ib_sge *sge;
  896. unsigned long flags;
  897. int ret;
  898. /* Set WR ID to find mad_send_wr upon completion */
  899. qp_info = mad_send_wr->mad_agent_priv->qp_info;
  900. mad_send_wr->send_wr.wr_id = (unsigned long)&mad_send_wr->mad_list;
  901. mad_send_wr->mad_list.mad_queue = &qp_info->send_queue;
  902. mad_agent = mad_send_wr->send_buf.mad_agent;
  903. sge = mad_send_wr->sg_list;
  904. sge[0].addr = ib_dma_map_single(mad_agent->device,
  905. mad_send_wr->send_buf.mad,
  906. sge[0].length,
  907. DMA_TO_DEVICE);
  908. mad_send_wr->header_mapping = sge[0].addr;
  909. sge[1].addr = ib_dma_map_single(mad_agent->device,
  910. ib_get_payload(mad_send_wr),
  911. sge[1].length,
  912. DMA_TO_DEVICE);
  913. mad_send_wr->payload_mapping = sge[1].addr;
  914. spin_lock_irqsave(&qp_info->send_queue.lock, flags);
  915. if (qp_info->send_queue.count < qp_info->send_queue.max_active) {
  916. ret = ib_post_send(mad_agent->qp, &mad_send_wr->send_wr,
  917. &bad_send_wr);
  918. list = &qp_info->send_queue.list;
  919. } else {
  920. ret = 0;
  921. list = &qp_info->overflow_list;
  922. }
  923. if (!ret) {
  924. qp_info->send_queue.count++;
  925. list_add_tail(&mad_send_wr->mad_list.list, list);
  926. }
  927. spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
  928. if (ret) {
  929. ib_dma_unmap_single(mad_agent->device,
  930. mad_send_wr->header_mapping,
  931. sge[0].length, DMA_TO_DEVICE);
  932. ib_dma_unmap_single(mad_agent->device,
  933. mad_send_wr->payload_mapping,
  934. sge[1].length, DMA_TO_DEVICE);
  935. }
  936. return ret;
  937. }
  938. /*
  939. * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
  940. * with the registered client
  941. */
  942. int ib_post_send_mad(struct ib_mad_send_buf *send_buf,
  943. struct ib_mad_send_buf **bad_send_buf)
  944. {
  945. struct ib_mad_agent_private *mad_agent_priv;
  946. struct ib_mad_send_buf *next_send_buf;
  947. struct ib_mad_send_wr_private *mad_send_wr;
  948. unsigned long flags;
  949. int ret = -EINVAL;
  950. /* Walk list of send WRs and post each on send list */
  951. for (; send_buf; send_buf = next_send_buf) {
  952. mad_send_wr = container_of(send_buf,
  953. struct ib_mad_send_wr_private,
  954. send_buf);
  955. mad_agent_priv = mad_send_wr->mad_agent_priv;
  956. if (!send_buf->mad_agent->send_handler ||
  957. (send_buf->timeout_ms &&
  958. !send_buf->mad_agent->recv_handler)) {
  959. ret = -EINVAL;
  960. goto error;
  961. }
  962. if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr *) send_buf->mad)->mgmt_class)) {
  963. if (mad_agent_priv->agent.rmpp_version) {
  964. ret = -EINVAL;
  965. goto error;
  966. }
  967. }
  968. /*
  969. * Save pointer to next work request to post in case the
  970. * current one completes, and the user modifies the work
  971. * request associated with the completion
  972. */
  973. next_send_buf = send_buf->next;
  974. mad_send_wr->send_wr.wr.ud.ah = send_buf->ah;
  975. if (((struct ib_mad_hdr *) send_buf->mad)->mgmt_class ==
  976. IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
  977. ret = handle_outgoing_dr_smp(mad_agent_priv,
  978. mad_send_wr);
  979. if (ret < 0) /* error */
  980. goto error;
  981. else if (ret == 1) /* locally consumed */
  982. continue;
  983. }
  984. mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid;
  985. /* Timeout will be updated after send completes */
  986. mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms);
  987. mad_send_wr->max_retries = send_buf->retries;
  988. mad_send_wr->retries_left = send_buf->retries;
  989. send_buf->retries = 0;
  990. /* Reference for work request to QP + response */
  991. mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0);
  992. mad_send_wr->status = IB_WC_SUCCESS;
  993. /* Reference MAD agent until send completes */
  994. atomic_inc(&mad_agent_priv->refcount);
  995. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  996. list_add_tail(&mad_send_wr->agent_list,
  997. &mad_agent_priv->send_list);
  998. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  999. if (mad_agent_priv->agent.rmpp_version) {
  1000. ret = ib_send_rmpp_mad(mad_send_wr);
  1001. if (ret >= 0 && ret != IB_RMPP_RESULT_CONSUMED)
  1002. ret = ib_send_mad(mad_send_wr);
  1003. } else
  1004. ret = ib_send_mad(mad_send_wr);
  1005. if (ret < 0) {
  1006. /* Fail send request */
  1007. spin_lock_irqsave(&mad_agent_priv->lock, flags);
  1008. list_del(&mad_send_wr->agent_list);
  1009. spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
  1010. atomic_dec(&mad_agent_priv->refcount);
  1011. goto error;
  1012. }
  1013. }
  1014. return 0;
  1015. error:
  1016. if (bad_send_buf)
  1017. *bad_send_buf = send_buf;
  1018. return ret;
  1019. }
  1020. EXPORT_SYMBOL(ib_post_send_mad);
  1021. /*
  1022. * ib_free_recv_mad - Returns data buffers used to receive
  1023. * a MAD to the access layer
  1024. */
  1025. void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc)
  1026. {
  1027. struct ib_mad_recv_buf *mad_recv_buf, *temp_recv_buf;
  1028. struct ib_mad_private_header *mad_priv_hdr;
  1029. struct ib_mad_private *priv;
  1030. struct list_head free_list;
  1031. INIT_LIST_HEAD(&free_list);
  1032. list_splice_init(&mad_recv_wc->rmpp_list, &free_list);
  1033. list_for_each_entry_safe(mad_recv_buf, temp_recv_buf,
  1034. &free_list, list) {
  1035. mad_recv_wc = container_of(mad_recv_buf, struct ib_mad_recv_wc,
  1036. recv_buf);
  1037. mad_priv_hdr = container_of(mad_recv_wc,
  1038. struct ib_mad_private_header,
  1039. recv_wc);
  1040. priv = container_of(mad_priv_hdr, struct ib_mad_private,
  1041. header);
  1042. kmem_cache_free(ib_mad_cache, priv);
  1043. }
  1044. }
  1045. EXPORT_SYMBOL(ib_free_recv_mad);
  1046. struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp,
  1047. u8 rmpp_version,
  1048. ib_mad_send_handler send_handler,
  1049. ib_mad_recv_handler recv_handler,
  1050. void *context)
  1051. {
  1052. return ERR_PTR(-EINVAL); /* XXX: for now */
  1053. }
  1054. EXPORT_SYMBOL(ib_redirect_mad_qp);
  1055. int ib_process_mad_wc(struct ib_mad_agent *mad_agent,
  1056. struct ib_wc *wc)
  1057. {
  1058. printk(KERN_ERR PFX "ib_process_mad_wc() not implemented yet\n");
  1059. return 0;
  1060. }
  1061. EXPORT_SYMBOL(ib_process_mad_wc);
  1062. static int method_in_use(struct ib_mad_mgmt_method_table **method,
  1063. struct ib_mad_reg_req *mad_reg_req)
  1064. {
  1065. int i;
  1066. for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS) {
  1067. if ((*method)->agent[i]) {
  1068. printk(KERN_ERR PFX "Method %d already in use\n", i);
  1069. return -EINVAL;
  1070. }
  1071. }
  1072. return 0;
  1073. }
  1074. static int allocate_method_table(struct ib_mad_mgmt_method_table **method)
  1075. {
  1076. /* Allocate management method table */
  1077. *method = kzalloc(sizeof **method, GFP_ATOMIC);
  1078. if (!*method) {
  1079. printk(KERN_ERR PFX "No memory for "
  1080. "ib_mad_mgmt_method_table\n");
  1081. return -ENOMEM;
  1082. }
  1083. return 0;
  1084. }
  1085. /*
  1086. * Check to see if there are any methods still in use
  1087. */
  1088. static int check_method_table(struct ib_mad_mgmt_method_table *method)
  1089. {
  1090. int i;
  1091. for (i = 0; i < IB_MGMT_MAX_METHODS; i++)
  1092. if (method->agent[i])
  1093. return 1;
  1094. return 0;
  1095. }
  1096. /*
  1097. * Check to see if there are any method tables for this class still in use
  1098. */
  1099. static int check_class_table(struct ib_mad_mgmt_class_table *class)
  1100. {
  1101. int i;
  1102. for (i = 0; i < MAX_MGMT_CLASS; i++)
  1103. if (class->method_table[i])
  1104. return 1;
  1105. return 0;
  1106. }
  1107. static int check_vendor_class(struct ib_mad_mgmt_vendor_class *vendor_class)
  1108. {
  1109. int i;
  1110. for (i = 0; i < MAX_MGMT_OUI; i++)
  1111. if (vendor_class->method_table[i])
  1112. return 1;
  1113. return 0;
  1114. }
  1115. static int find_vendor_oui(struct ib_mad_mgmt_vendor_class *vendor_class,
  1116. char *oui)
  1117. {
  1118. int i;
  1119. for (i = 0; i < MAX_MGMT_OUI; i++)
  1120. /* Is there matching OUI for this vendor class ? */
  1121. if (!memcmp(vendor_class->oui[i], oui, 3))
  1122. return i;
  1123. return -1;
  1124. }
  1125. static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table *vendor)
  1126. {
  1127. int i;
  1128. for (i = 0; i < MAX_MGMT_VENDOR_RANGE2; i++)
  1129. if (vendor->vendor_class[i])
  1130. return 1;
  1131. return 0;
  1132. }
  1133. static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table *method,
  1134. struct ib_mad_agent_private *agent)
  1135. {
  1136. int i;
  1137. /* Remove any methods for this mad agent */
  1138. for (i = 0; i < IB_MGMT_MAX_METHODS; i++) {
  1139. if (method->agent[i] == agent) {
  1140. method->agent[i] = NULL;
  1141. }
  1142. }
  1143. }
  1144. static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
  1145. struct ib_mad_agent_private *agent_priv,
  1146. u8 mgmt_class)
  1147. {
  1148. struct ib_mad_port_private *port_priv;
  1149. struct ib_mad_mgmt_class_table **class;
  1150. struct ib_mad_mgmt_method_table **method;
  1151. int i, ret;
  1152. port_priv = agent_priv->qp_info->port_priv;
  1153. class = &port_priv->version[mad_reg_req->mgmt_class_version].class;
  1154. if (!*class) {
  1155. /* Allocate management class table for "new" class version */
  1156. *class = kzalloc(sizeof **class, GFP_ATOMIC);
  1157. if (!*class) {
  1158. printk(KERN_ERR PFX "No memory for "
  1159. "ib_mad_mgmt_class_table\n");
  1160. ret = -ENOMEM;
  1161. goto error1;
  1162. }
  1163. /* Allocate method table for this management class */
  1164. method = &(*class)->method_table[mgmt_class];
  1165. if ((ret = allocate_method_table(method)))
  1166. goto error2;
  1167. } else {
  1168. method = &(*class)->method_table[mgmt_class];
  1169. if (!*method) {
  1170. /* Allocate method table for this management class */
  1171. if ((ret = allocate_method_table(method)))
  1172. goto error1;
  1173. }
  1174. }
  1175. /* Now, make sure methods are not already in use */
  1176. if (method_in_use(method, mad_reg_req))
  1177. goto error3;
  1178. /* Finally, add in methods being registered */
  1179. for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS)
  1180. (*method)->agent[i] = agent_priv;
  1181. return 0;
  1182. error3:
  1183. /* Remove any methods for this mad agent */
  1184. remove_methods_mad_agent(*method, agent_priv);
  1185. /* Now, check to see if there are any methods in use */
  1186. if (!check_method_table(*method)) {
  1187. /* If not, release management method table */
  1188. kfree(*method);
  1189. *method = NULL;
  1190. }
  1191. ret = -EINVAL;
  1192. goto error1;
  1193. error2:
  1194. kfree(*class);
  1195. *class = NULL;
  1196. error1:
  1197. return ret;
  1198. }
  1199. static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
  1200. struct ib_mad_agent_private *agent_priv)
  1201. {
  1202. struct ib_mad_port_private *port_priv;
  1203. struct ib_mad_mgmt_vendor_class_table **vendor_table;
  1204. struct ib_mad_mgmt_vendor_class_table *vendor = NULL;
  1205. struct ib_mad_mgmt_vendor_class *vendor_class = NULL;
  1206. struct ib_mad_mgmt_method_table **method;
  1207. int i, ret = -ENOMEM;
  1208. u8 vclass;
  1209. /* "New" vendor (with OUI) class */
  1210. vclass = vendor_class_index(mad_reg_req->mgmt_class);
  1211. port_priv = agent_priv->qp_info->port_priv;
  1212. vendor_table = &port_priv->version[
  1213. mad_reg_req->mgmt_class_version].vendor;
  1214. if (!*vendor_table) {
  1215. /* Allocate mgmt vendor class table for "new" class version */
  1216. vendor = kzalloc(sizeof *vendor, GFP_ATOMIC);
  1217. if (!vendor) {
  1218. printk(KERN_ERR PFX "No memory for "
  1219. "ib_mad_mgmt_vendor_class_table\n");
  1220. goto error1;
  1221. }
  1222. *vendor_table = vendor;
  1223. }
  1224. if (!(*vendor_table)->vendor_class[vclass]) {
  1225. /* Allocate table for this management vendor class */
  1226. vendor_class = kzalloc(sizeof *vendor_class, GFP_ATOMIC);
  1227. if (!vendor_class) {
  1228. printk(KERN_ERR PFX "No memory for "
  1229. "ib_mad_mgmt_vendor_class\n");
  1230. goto error2;
  1231. }
  1232. (*vendor_table)->vendor_class[vclass] = vendor_class;
  1233. }
  1234. for (i = 0; i < MAX_MGMT_OUI; i++) {
  1235. /* Is there matching OUI for this vendor class ? */
  1236. if (!memcmp((*vendor_table)->vendor_class[vclass]->oui[i],
  1237. mad_reg_req->oui, 3)) {
  1238. method = &(*vendor_table)->vendor_class[
  1239. vclass]->method_table[i];
  1240. BUG_ON(!*method);
  1241. goto check_in_use;
  1242. }
  1243. }
  1244. for (i = 0; i < MAX_MGMT_OUI; i++) {
  1245. /* OUI slot available ? */
  1246. if (!is_vendor_oui((*vendor_table)->vendor_class[
  1247. vclass]->oui[i])) {
  1248. method = &(*vendor_table)->vendor_class[
  1249. vclass]->method_table[i];
  1250. BUG_ON(*method);
  1251. /* Allocate method table for this OUI */
  1252. if ((ret = allocate_method_table(method)))
  1253. goto error3;
  1254. memcpy((*vendor_table)->vendor_class[vclass]->oui[i],
  1255. mad_reg_req->oui, 3);
  1256. goto check_in_use;
  1257. }
  1258. }
  1259. printk(KERN_ERR PFX "All OUI slots in use\n");
  1260. goto error3;
  1261. check_in_use:
  1262. /* Now, make sure methods are not already in use */
  1263. if (method_in_use(method, mad_reg_req))
  1264. goto error4;
  1265. /* Finally, add in methods being registered */
  1266. for_each_set_bit(i, mad_reg_req->method_mask, IB_MGMT_MAX_METHODS)
  1267. (*method)->agent[i] = agent_priv;
  1268. return 0;
  1269. error4:
  1270. /* Remove any methods for this mad agent */
  1271. remove_methods_mad_agent(*method, agent_priv);
  1272. /* Now, check to see if there are any methods in use */
  1273. if (!check_method_table(*method)) {
  1274. /* If not, release management method table */
  1275. kfree(*method);
  1276. *method = NULL;
  1277. }
  1278. ret = -EINVAL;
  1279. error3:
  1280. if (vendor_class) {
  1281. (*vendor_table)->vendor_class[vclass] = NULL;
  1282. kfree(vendor_class);
  1283. }
  1284. error2:
  1285. if (vendor) {
  1286. *vendor_table = NULL;
  1287. kfree(vendor);
  1288. }
  1289. error1:
  1290. return ret;
  1291. }
  1292. static void remove_mad_reg_req(struct ib_mad_agent_private *agent_priv)
  1293. {
  1294. struct ib_mad_port_private *port_priv;
  1295. struct ib_mad_mgmt_class_table *class;
  1296. struct ib_mad_mgmt_method_table *method;
  1297. struct ib_mad_mgmt_vendor_class_table *vendor;
  1298. struct ib_mad_mgmt_vendor_class *vendor_class;
  1299. int index;
  1300. u8 mgmt_class;
  1301. /*
  1302. * Was MAD registration request supplied
  1303. * with original registration ?
  1304. */
  1305. if (!agent_priv->reg_req) {
  1306. goto out;
  1307. }
  1308. port_priv = agent_priv->qp_info->port_priv;
  1309. mgmt_class = convert_mgmt_class(agent_priv->reg_req->mgmt_class);
  1310. class = port_priv->version[
  1311. agent_priv->reg_req->mgmt_class_version].class;
  1312. if (!class)
  1313. goto vendor_check;
  1314. method = class->method_table[mgmt_class];
  1315. if (method) {
  1316. /* Remove any methods for this mad agent */
  1317. remove_methods_mad_agent(method, agent_priv);
  1318. /* Now, check to see if there are any methods still in use */
  1319. if (!check_method_table(method)) {
  1320. /* If not, release management method table */
  1321. kfree(method);
  1322. class->method_table[mgmt_class] = NULL;
  1323. /* Any management classes left ? */
  1324. if (!check_class_table(class)) {
  1325. /* If not, release management class table */
  1326. kfree(class);
  1327. port_priv->version[
  1328. agent_priv->reg_req->
  1329. mgmt_class_version].class = NULL;
  1330. }
  1331. }
  1332. }
  1333. vendor_check:
  1334. if (!is_vendor_class(mgmt_class))
  1335. goto out;
  1336. /* normalize mgmt_class to vendor range 2 */
  1337. mgmt_class = vendor_class_index(agent_priv->reg_req->mgmt_class);
  1338. vendor = port_priv->version[
  1339. agent_priv->reg_req->mgmt_class_version].vendor;
  1340. if (!vendor)
  1341. goto out;
  1342. vendor_class = vendor->vendor_class[mgmt_class];
  1343. if (vendor_class) {
  1344. index = find_vendor_oui(vendor_class, agent_priv->reg_req->oui);
  1345. if (index < 0)
  1346. goto out;
  1347. method = vendor_class->method_table[index];
  1348. if (method) {
  1349. /* Remove any methods for this mad agent */
  1350. remove_methods_mad_agent(method, agent_priv);
  1351. /*
  1352. * Now, check to see if there are
  1353. * any methods still in use
  1354. */
  1355. if (!check_method_table(method)) {
  1356. /* If not, release management method table */
  1357. kfree(method);
  1358. vendor_class->method_table[index] = NULL;
  1359. memset(vendor_class->oui[index], 0, 3);
  1360. /* Any OUIs left ? */
  1361. if (!check_vendor_class(vendor_class)) {
  1362. /* If not, release vendor class table */
  1363. kfree(vendor_class);
  1364. vendor->vendor_class[mgmt_class] = NULL;
  1365. /* Any other vendor classes left ? */
  1366. if (!check_vendor_table(vendor)) {
  1367. kfree(vendor);
  1368. port_priv->version[
  1369. agent_priv->reg_req->
  1370. mgmt_class_version].
  1371. vendor = NULL;
  1372. }
  1373. }
  1374. }
  1375. }
  1376. }
  1377. out:
  1378. return;
  1379. }
  1380. static struct ib_mad_agent_private *
  1381. find_mad_agent(struct ib_mad_port_private *port_priv,
  1382. struct ib_mad *mad)
  1383. {
  1384. struct ib_mad_agent_private *mad_agent = NULL;
  1385. unsigned long flags;
  1386. spin_lock_irqsave(&port_priv->reg_lock, flags);
  1387. if (ib_response_mad(mad)) {
  1388. u32 hi_tid;
  1389. struct ib_mad_agent_private *entry;
  1390. /*
  1391. * Routing is based on high 32 bits of transaction ID
  1392. * of MAD.
  1393. */
  1394. hi_tid = be64_to_cpu(mad->mad_hdr.tid) >> 32;
  1395. list_for_each_entry(entry, &port_priv->agent_list, agent_list) {
  1396. if (entry->agent.hi_tid == hi_tid) {
  1397. mad_agent = entry;
  1398. break;
  1399. }
  1400. }
  1401. } else {
  1402. struct ib_mad_mgmt_class_table *class;
  1403. struct ib_mad_mgmt_method_table *method;
  1404. struct ib_mad_mgmt_vendor_class_table *vendor;
  1405. struct ib_mad_mgmt_vendor_class *vendor_class;
  1406. struct ib_vendor_mad *vendor_mad;
  1407. int index;
  1408. /*
  1409. * Routing is based on version, class, and method
  1410. * For "newer" vendor MADs, also based on OUI
  1411. */
  1412. if (mad->mad_hdr.class_version >= MAX_MGMT_VERSION)
  1413. goto out;
  1414. if (!is_vendor_class(mad->mad_hdr.mgmt_class)) {
  1415. class = port_priv->version[
  1416. mad->mad_hdr.class_version].class;
  1417. if (!class)
  1418. goto out;
  1419. method = class->method_table[convert_mgmt_class(
  1420. mad->mad_hdr.mgmt_class)];
  1421. if (method)
  1422. mad_agent = method->agent[mad->mad_hdr.method &
  1423. ~IB_MGMT_METHOD_RESP];
  1424. } else {
  1425. vendor = port_priv->version[
  1426. mad->mad_hdr.class_version].vendor;
  1427. if (!vendor)
  1428. goto out;
  1429. vendor_class = vendor->vendor_class[vendor_class_index(
  1430. mad->mad_hdr.mgmt_class)];
  1431. if (!vendor_class)
  1432. goto out;
  1433. /* Find matching OUI */
  1434. vendor_mad = (struct ib_vendor_mad *)mad;
  1435. index = find_vendor_oui(vendor_class, vendor_mad->oui);
  1436. if (index == -1)
  1437. goto out;
  1438. method = vendor_class->method_table[index];
  1439. if (method) {
  1440. mad_agent = method->agent[mad->mad_hdr.method &
  1441. ~IB_MGMT_METHOD_RESP];
  1442. }
  1443. }
  1444. }
  1445. if (mad_agent) {
  1446. if (mad_agent->agent.recv_handler)
  1447. atomic_inc(&mad_agent->refcount);
  1448. else {
  1449. printk(KERN_NOTICE PFX "No receive handler for client "
  1450. "%p on port %d\n",
  1451. &mad_agent->agent, port_priv->port_num);
  1452. mad_agent = NULL;
  1453. }
  1454. }
  1455. out:
  1456. spin_unlock_irqrestore(&port_priv->reg_lock, flags);
  1457. return mad_agent;
  1458. }
  1459. static int validate_mad(struct ib_mad *mad, u32 qp_num)
  1460. {
  1461. int valid = 0;
  1462. /* Make sure MAD base version is understood */
  1463. if (mad->mad_hdr.base_version != IB_MGMT_BASE_VERSION) {
  1464. printk(KERN_ERR PFX "MAD received with unsupported base "
  1465. "version %d\n", mad->mad_hdr.base_version);
  1466. goto out;
  1467. }
  1468. /* Filter SMI packets sent to other than QP0 */
  1469. if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
  1470. (mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
  1471. if (qp_num == 0)
  1472. valid = 1;
  1473. } else {
  1474. /* Filter GSI packets sent to QP0 */
  1475. if (qp_num != 0)
  1476. valid = 1;
  1477. }
  1478. out:
  1479. return valid;
  1480. }
  1481. static int is_data_mad(struct ib_mad_agent_private *mad_agent_priv,
  1482. struct ib_mad_hdr *mad_hdr)
  1483. {
  1484. struct ib_rmpp_mad *rmpp_mad;
  1485. rmpp_mad = (struct ib_rmpp_mad *)mad_hdr;
  1486. return !mad_agent_priv->agent.rmpp_version ||
  1487. !(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
  1488. IB_MGMT_RMPP_FLAG_ACTIVE) ||
  1489. (rmpp_mad->rmpp_hdr.rmpp_type == IB_MGMT_RMPP_TYPE_DATA);
  1490. }
  1491. static inline int rcv_has_same_class(struct ib_mad_send_wr_private *wr,
  1492. struct ib_mad_recv_wc *rwc)
  1493. {
  1494. return ((struct ib_mad *)(wr->send_buf.mad))->mad_hdr.mgmt_class ==
  1495. rwc->recv_buf.mad->mad_hdr.mgmt_class;
  1496. }
  1497. static inline int rcv_has_same_gid(struct ib_mad_agent_private *mad_agent_priv,
  1498. struct ib_mad_send_wr_private *wr,
  1499. struct ib_mad_recv_wc *rwc )
  1500. {
  1501. struct ib_ah_attr attr;
  1502. u8 send_resp, rcv_resp;
  1503. union ib_gid sgid;
  1504. struct ib_device *device = mad_agent_priv->agent.device;
  1505. u8 port_num = mad_agent_priv->agent.port_num;
  1506. u8 lmc;
  1507. send_resp = ib_response_mad((struct ib_mad *)wr->send_buf.mad);
  1508. rcv_resp = ib_response_mad(rwc->recv_buf.mad);
  1509. if (send_resp == rcv_resp)
  1510. /* both requests, or both responses. GIDs different */
  1511. return 0;
  1512. if (ib_query_ah(wr->send_buf.ah, &attr))
  1513. /* Assume not equal, to avoid false positives. */
  1514. return 0;
  1515. if (!!(attr.ah_flags & IB_AH_GRH) !=
  1516. !!(rwc->wc->wc_flags & IB_WC_GRH))
  1517. /* one has GID, other does not. Assume different */
  1518. return 0;
  1519. if (!send_resp && rcv_resp) {
  1520. /* is request/response. */
  1521. if (!(attr.ah_flags & IB_AH_GRH)) {
  1522. if (ib_get_cached_lmc(device, port_num, &lmc))
  1523. return 0;
  1524. return (!lmc || !((attr.src_path_bits ^
  1525. rwc->wc->dlid_path_bits) &
  1526. ((1 << lmc) - 1)));
  1527. } else {
  1528. if (ib_get_cached_gid(device, port_num,
  1529. attr.grh.sgid_index, &sgid))
  1530. return 0;
  1531. return !memcmp(sgid.raw, rwc->recv_buf.grh->dgid.raw,
  1532. 16);
  1533. }
  1534. }
  1535. if (!(attr.ah_flags & IB_AH_GRH))
  1536. return attr.dlid == rwc->wc->slid;
  1537. else
  1538. return !memcmp(attr.grh.dgid.raw, rwc->recv_buf.grh->sgid.raw,
  1539. 16);
  1540. }
  1541. static inline int is_direct(u8 class)
  1542. {
  1543. return (class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE);
  1544. }
  1545. struct ib_mad_send_wr_private*
  1546. ib_find_send_mad(struct ib_mad_agent_private *mad_agent_priv,
  1547. struct ib_mad_recv_wc *wc)
  1548. {
  1549. struct ib_mad_send_wr_private *wr;
  1550. struct ib_mad *mad;
  1551. mad = (struct ib_mad *)wc->recv_buf.mad;
  1552. list_for_each_entry(wr, &mad_agent_priv->wait_list, agent_list) {
  1553. if ((wr->tid == mad->mad_hdr.tid) &&
  1554. rcv_has_same_class(wr, wc) &&
  1555. /*
  1556. * Don't check GID for direct routed MADs.
  1557. * These might have permissive LIDs.
  1558. */
  1559. (is_direct(wc->recv_buf.mad->mad_hdr.mgmt_class) ||
  1560. rcv_has_same_gid(mad_agent_priv, wr, wc)))
  1561. return (wr->status == IB_WC_SUCCESS) ? wr : NULL;
  1562. }
  1563. /*
  1564. * It's possible to receive the response before we've
  1565. * been notified that the send has completed
  1566. */
  1567. list_for_each

Large files files are truncated, but you can click here to view the full file