agent.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright (c) 2004, 2005 Mellanox Technologies Ltd. All rights reserved.
  3. * Copyright (c) 2004, 2005 Infinicon Corporation. All rights reserved.
  4. * Copyright (c) 2004, 2005 Intel Corporation. All rights reserved.
  5. * Copyright (c) 2004, 2005 Topspin Corporation. All rights reserved.
  6. * Copyright (c) 2004, 2005 Voltaire Corporation. All rights reserved.
  7. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  8. *
  9. * This software is available to you under a choice of one of two
  10. * licenses. You may choose to be licensed under the terms of the GNU
  11. * General Public License (GPL) Version 2, available from the file
  12. * COPYING in the main directory of this source tree, or the
  13. * OpenIB.org BSD license below:
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer.
  22. *
  23. * - Redistributions in binary form must reproduce the above
  24. * copyright notice, this list of conditions and the following
  25. * disclaimer in the documentation and/or other materials
  26. * provided with the distribution.
  27. *
  28. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  29. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  31. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  32. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  33. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  34. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  35. * SOFTWARE.
  36. *
  37. * $Id: agent.c 1389 2004-12-27 22:56:47Z roland $
  38. */
  39. #include <linux/dma-mapping.h>
  40. #include <asm/bug.h>
  41. #include <rdma/ib_smi.h>
  42. #include "smi.h"
  43. #include "agent_priv.h"
  44. #include "mad_priv.h"
  45. #include "agent.h"
  46. spinlock_t ib_agent_port_list_lock;
  47. static LIST_HEAD(ib_agent_port_list);
  48. /*
  49. * Caller must hold ib_agent_port_list_lock
  50. */
  51. static inline struct ib_agent_port_private *
  52. __ib_get_agent_port(struct ib_device *device, int port_num,
  53. struct ib_mad_agent *mad_agent)
  54. {
  55. struct ib_agent_port_private *entry;
  56. BUG_ON(!(!!device ^ !!mad_agent)); /* Exactly one MUST be (!NULL) */
  57. if (device) {
  58. list_for_each_entry(entry, &ib_agent_port_list, port_list) {
  59. if (entry->smp_agent->device == device &&
  60. entry->port_num == port_num)
  61. return entry;
  62. }
  63. } else {
  64. list_for_each_entry(entry, &ib_agent_port_list, port_list) {
  65. if ((entry->smp_agent == mad_agent) ||
  66. (entry->perf_mgmt_agent == mad_agent))
  67. return entry;
  68. }
  69. }
  70. return NULL;
  71. }
  72. static inline struct ib_agent_port_private *
  73. ib_get_agent_port(struct ib_device *device, int port_num,
  74. struct ib_mad_agent *mad_agent)
  75. {
  76. struct ib_agent_port_private *entry;
  77. unsigned long flags;
  78. spin_lock_irqsave(&ib_agent_port_list_lock, flags);
  79. entry = __ib_get_agent_port(device, port_num, mad_agent);
  80. spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
  81. return entry;
  82. }
  83. int smi_check_local_dr_smp(struct ib_smp *smp,
  84. struct ib_device *device,
  85. int port_num)
  86. {
  87. struct ib_agent_port_private *port_priv;
  88. if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
  89. return 1;
  90. port_priv = ib_get_agent_port(device, port_num, NULL);
  91. if (!port_priv) {
  92. printk(KERN_DEBUG SPFX "smi_check_local_dr_smp %s port %d "
  93. "not open\n",
  94. device->name, port_num);
  95. return 1;
  96. }
  97. return smi_check_local_smp(port_priv->smp_agent, smp);
  98. }
  99. static int agent_mad_send(struct ib_mad_agent *mad_agent,
  100. struct ib_agent_port_private *port_priv,
  101. struct ib_mad_private *mad_priv,
  102. struct ib_grh *grh,
  103. struct ib_wc *wc)
  104. {
  105. struct ib_agent_send_wr *agent_send_wr;
  106. struct ib_sge gather_list;
  107. struct ib_send_wr send_wr;
  108. struct ib_send_wr *bad_send_wr;
  109. struct ib_ah_attr ah_attr;
  110. unsigned long flags;
  111. int ret = 1;
  112. agent_send_wr = kmalloc(sizeof(*agent_send_wr), GFP_KERNEL);
  113. if (!agent_send_wr)
  114. goto out;
  115. agent_send_wr->mad = mad_priv;
  116. gather_list.addr = dma_map_single(mad_agent->device->dma_device,
  117. &mad_priv->mad,
  118. sizeof(mad_priv->mad),
  119. DMA_TO_DEVICE);
  120. gather_list.length = sizeof(mad_priv->mad);
  121. gather_list.lkey = mad_agent->mr->lkey;
  122. send_wr.next = NULL;
  123. send_wr.opcode = IB_WR_SEND;
  124. send_wr.sg_list = &gather_list;
  125. send_wr.num_sge = 1;
  126. send_wr.wr.ud.remote_qpn = wc->src_qp; /* DQPN */
  127. send_wr.wr.ud.timeout_ms = 0;
  128. send_wr.send_flags = IB_SEND_SIGNALED | IB_SEND_SOLICITED;
  129. ah_attr.dlid = wc->slid;
  130. ah_attr.port_num = mad_agent->port_num;
  131. ah_attr.src_path_bits = wc->dlid_path_bits;
  132. ah_attr.sl = wc->sl;
  133. ah_attr.static_rate = 0;
  134. ah_attr.ah_flags = 0; /* No GRH */
  135. if (mad_priv->mad.mad.mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT) {
  136. if (wc->wc_flags & IB_WC_GRH) {
  137. ah_attr.ah_flags = IB_AH_GRH;
  138. /* Should sgid be looked up ? */
  139. ah_attr.grh.sgid_index = 0;
  140. ah_attr.grh.hop_limit = grh->hop_limit;
  141. ah_attr.grh.flow_label = be32_to_cpu(
  142. grh->version_tclass_flow) & 0xfffff;
  143. ah_attr.grh.traffic_class = (be32_to_cpu(
  144. grh->version_tclass_flow) >> 20) & 0xff;
  145. memcpy(ah_attr.grh.dgid.raw,
  146. grh->sgid.raw,
  147. sizeof(ah_attr.grh.dgid));
  148. }
  149. }
  150. agent_send_wr->ah = ib_create_ah(mad_agent->qp->pd, &ah_attr);
  151. if (IS_ERR(agent_send_wr->ah)) {
  152. printk(KERN_ERR SPFX "No memory for address handle\n");
  153. kfree(agent_send_wr);
  154. goto out;
  155. }
  156. send_wr.wr.ud.ah = agent_send_wr->ah;
  157. if (mad_priv->mad.mad.mad_hdr.mgmt_class == IB_MGMT_CLASS_PERF_MGMT) {
  158. send_wr.wr.ud.pkey_index = wc->pkey_index;
  159. send_wr.wr.ud.remote_qkey = IB_QP1_QKEY;
  160. } else { /* for SMPs */
  161. send_wr.wr.ud.pkey_index = 0;
  162. send_wr.wr.ud.remote_qkey = 0;
  163. }
  164. send_wr.wr.ud.mad_hdr = &mad_priv->mad.mad.mad_hdr;
  165. send_wr.wr_id = (unsigned long)agent_send_wr;
  166. pci_unmap_addr_set(agent_send_wr, mapping, gather_list.addr);
  167. /* Send */
  168. spin_lock_irqsave(&port_priv->send_list_lock, flags);
  169. if (ib_post_send_mad(mad_agent, &send_wr, &bad_send_wr)) {
  170. spin_unlock_irqrestore(&port_priv->send_list_lock, flags);
  171. dma_unmap_single(mad_agent->device->dma_device,
  172. pci_unmap_addr(agent_send_wr, mapping),
  173. sizeof(mad_priv->mad),
  174. DMA_TO_DEVICE);
  175. ib_destroy_ah(agent_send_wr->ah);
  176. kfree(agent_send_wr);
  177. } else {
  178. list_add_tail(&agent_send_wr->send_list,
  179. &port_priv->send_posted_list);
  180. spin_unlock_irqrestore(&port_priv->send_list_lock, flags);
  181. ret = 0;
  182. }
  183. out:
  184. return ret;
  185. }
  186. int agent_send(struct ib_mad_private *mad,
  187. struct ib_grh *grh,
  188. struct ib_wc *wc,
  189. struct ib_device *device,
  190. int port_num)
  191. {
  192. struct ib_agent_port_private *port_priv;
  193. struct ib_mad_agent *mad_agent;
  194. port_priv = ib_get_agent_port(device, port_num, NULL);
  195. if (!port_priv) {
  196. printk(KERN_DEBUG SPFX "agent_send %s port %d not open\n",
  197. device->name, port_num);
  198. return 1;
  199. }
  200. /* Get mad agent based on mgmt_class in MAD */
  201. switch (mad->mad.mad.mad_hdr.mgmt_class) {
  202. case IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE:
  203. case IB_MGMT_CLASS_SUBN_LID_ROUTED:
  204. mad_agent = port_priv->smp_agent;
  205. break;
  206. case IB_MGMT_CLASS_PERF_MGMT:
  207. mad_agent = port_priv->perf_mgmt_agent;
  208. break;
  209. default:
  210. return 1;
  211. }
  212. return agent_mad_send(mad_agent, port_priv, mad, grh, wc);
  213. }
  214. static void agent_send_handler(struct ib_mad_agent *mad_agent,
  215. struct ib_mad_send_wc *mad_send_wc)
  216. {
  217. struct ib_agent_port_private *port_priv;
  218. struct ib_agent_send_wr *agent_send_wr;
  219. unsigned long flags;
  220. /* Find matching MAD agent */
  221. port_priv = ib_get_agent_port(NULL, 0, mad_agent);
  222. if (!port_priv) {
  223. printk(KERN_ERR SPFX "agent_send_handler: no matching MAD "
  224. "agent %p\n", mad_agent);
  225. return;
  226. }
  227. agent_send_wr = (struct ib_agent_send_wr *)(unsigned long)mad_send_wc->wr_id;
  228. spin_lock_irqsave(&port_priv->send_list_lock, flags);
  229. /* Remove completed send from posted send MAD list */
  230. list_del(&agent_send_wr->send_list);
  231. spin_unlock_irqrestore(&port_priv->send_list_lock, flags);
  232. dma_unmap_single(mad_agent->device->dma_device,
  233. pci_unmap_addr(agent_send_wr, mapping),
  234. sizeof(agent_send_wr->mad->mad),
  235. DMA_TO_DEVICE);
  236. ib_destroy_ah(agent_send_wr->ah);
  237. /* Release allocated memory */
  238. kmem_cache_free(ib_mad_cache, agent_send_wr->mad);
  239. kfree(agent_send_wr);
  240. }
  241. int ib_agent_port_open(struct ib_device *device, int port_num)
  242. {
  243. int ret;
  244. struct ib_agent_port_private *port_priv;
  245. unsigned long flags;
  246. /* First, check if port already open for SMI */
  247. port_priv = ib_get_agent_port(device, port_num, NULL);
  248. if (port_priv) {
  249. printk(KERN_DEBUG SPFX "%s port %d already open\n",
  250. device->name, port_num);
  251. return 0;
  252. }
  253. /* Create new device info */
  254. port_priv = kmalloc(sizeof *port_priv, GFP_KERNEL);
  255. if (!port_priv) {
  256. printk(KERN_ERR SPFX "No memory for ib_agent_port_private\n");
  257. ret = -ENOMEM;
  258. goto error1;
  259. }
  260. memset(port_priv, 0, sizeof *port_priv);
  261. port_priv->port_num = port_num;
  262. spin_lock_init(&port_priv->send_list_lock);
  263. INIT_LIST_HEAD(&port_priv->send_posted_list);
  264. /* Obtain send only MAD agent for SM class (SMI QP) */
  265. port_priv->smp_agent = ib_register_mad_agent(device, port_num,
  266. IB_QPT_SMI,
  267. NULL, 0,
  268. &agent_send_handler,
  269. NULL, NULL);
  270. if (IS_ERR(port_priv->smp_agent)) {
  271. ret = PTR_ERR(port_priv->smp_agent);
  272. goto error2;
  273. }
  274. /* Obtain send only MAD agent for PerfMgmt class (GSI QP) */
  275. port_priv->perf_mgmt_agent = ib_register_mad_agent(device, port_num,
  276. IB_QPT_GSI,
  277. NULL, 0,
  278. &agent_send_handler,
  279. NULL, NULL);
  280. if (IS_ERR(port_priv->perf_mgmt_agent)) {
  281. ret = PTR_ERR(port_priv->perf_mgmt_agent);
  282. goto error3;
  283. }
  284. spin_lock_irqsave(&ib_agent_port_list_lock, flags);
  285. list_add_tail(&port_priv->port_list, &ib_agent_port_list);
  286. spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
  287. return 0;
  288. error3:
  289. ib_unregister_mad_agent(port_priv->smp_agent);
  290. error2:
  291. kfree(port_priv);
  292. error1:
  293. return ret;
  294. }
  295. int ib_agent_port_close(struct ib_device *device, int port_num)
  296. {
  297. struct ib_agent_port_private *port_priv;
  298. unsigned long flags;
  299. spin_lock_irqsave(&ib_agent_port_list_lock, flags);
  300. port_priv = __ib_get_agent_port(device, port_num, NULL);
  301. if (port_priv == NULL) {
  302. spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
  303. printk(KERN_ERR SPFX "Port %d not found\n", port_num);
  304. return -ENODEV;
  305. }
  306. list_del(&port_priv->port_list);
  307. spin_unlock_irqrestore(&ib_agent_port_list_lock, flags);
  308. ib_unregister_mad_agent(port_priv->perf_mgmt_agent);
  309. ib_unregister_mad_agent(port_priv->smp_agent);
  310. kfree(port_priv);
  311. return 0;
  312. }