sa_query.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. * $Id: sa_query.c 1389 2004-12-27 22:56:47Z roland $
  33. */
  34. #include <linux/module.h>
  35. #include <linux/init.h>
  36. #include <linux/err.h>
  37. #include <linux/random.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/slab.h>
  40. #include <linux/pci.h>
  41. #include <linux/dma-mapping.h>
  42. #include <linux/kref.h>
  43. #include <linux/idr.h>
  44. #include <ib_pack.h>
  45. #include <ib_sa.h>
  46. MODULE_AUTHOR("Roland Dreier");
  47. MODULE_DESCRIPTION("InfiniBand subnet administration query support");
  48. MODULE_LICENSE("Dual BSD/GPL");
  49. /*
  50. * These two structures must be packed because they have 64-bit fields
  51. * that are only 32-bit aligned. 64-bit architectures will lay them
  52. * out wrong otherwise. (And unfortunately they are sent on the wire
  53. * so we can't change the layout)
  54. */
  55. struct ib_sa_hdr {
  56. u64 sm_key;
  57. u16 attr_offset;
  58. u16 reserved;
  59. ib_sa_comp_mask comp_mask;
  60. } __attribute__ ((packed));
  61. struct ib_sa_mad {
  62. struct ib_mad_hdr mad_hdr;
  63. struct ib_rmpp_hdr rmpp_hdr;
  64. struct ib_sa_hdr sa_hdr;
  65. u8 data[200];
  66. } __attribute__ ((packed));
  67. struct ib_sa_sm_ah {
  68. struct ib_ah *ah;
  69. struct kref ref;
  70. };
  71. struct ib_sa_port {
  72. struct ib_mad_agent *agent;
  73. struct ib_sa_sm_ah *sm_ah;
  74. struct work_struct update_task;
  75. spinlock_t ah_lock;
  76. u8 port_num;
  77. };
  78. struct ib_sa_device {
  79. int start_port, end_port;
  80. struct ib_event_handler event_handler;
  81. struct ib_sa_port port[0];
  82. };
  83. struct ib_sa_query {
  84. void (*callback)(struct ib_sa_query *, int, struct ib_sa_mad *);
  85. void (*release)(struct ib_sa_query *);
  86. struct ib_sa_port *port;
  87. struct ib_sa_mad *mad;
  88. struct ib_sa_sm_ah *sm_ah;
  89. DECLARE_PCI_UNMAP_ADDR(mapping)
  90. int id;
  91. };
  92. struct ib_sa_path_query {
  93. void (*callback)(int, struct ib_sa_path_rec *, void *);
  94. void *context;
  95. struct ib_sa_query sa_query;
  96. };
  97. struct ib_sa_mcmember_query {
  98. void (*callback)(int, struct ib_sa_mcmember_rec *, void *);
  99. void *context;
  100. struct ib_sa_query sa_query;
  101. };
  102. static void ib_sa_add_one(struct ib_device *device);
  103. static void ib_sa_remove_one(struct ib_device *device);
  104. static struct ib_client sa_client = {
  105. .name = "sa",
  106. .add = ib_sa_add_one,
  107. .remove = ib_sa_remove_one
  108. };
  109. static spinlock_t idr_lock;
  110. static DEFINE_IDR(query_idr);
  111. static spinlock_t tid_lock;
  112. static u32 tid;
  113. enum {
  114. IB_SA_ATTR_CLASS_PORTINFO = 0x01,
  115. IB_SA_ATTR_NOTICE = 0x02,
  116. IB_SA_ATTR_INFORM_INFO = 0x03,
  117. IB_SA_ATTR_NODE_REC = 0x11,
  118. IB_SA_ATTR_PORT_INFO_REC = 0x12,
  119. IB_SA_ATTR_SL2VL_REC = 0x13,
  120. IB_SA_ATTR_SWITCH_REC = 0x14,
  121. IB_SA_ATTR_LINEAR_FDB_REC = 0x15,
  122. IB_SA_ATTR_RANDOM_FDB_REC = 0x16,
  123. IB_SA_ATTR_MCAST_FDB_REC = 0x17,
  124. IB_SA_ATTR_SM_INFO_REC = 0x18,
  125. IB_SA_ATTR_LINK_REC = 0x20,
  126. IB_SA_ATTR_GUID_INFO_REC = 0x30,
  127. IB_SA_ATTR_SERVICE_REC = 0x31,
  128. IB_SA_ATTR_PARTITION_REC = 0x33,
  129. IB_SA_ATTR_RANGE_REC = 0x34,
  130. IB_SA_ATTR_PATH_REC = 0x35,
  131. IB_SA_ATTR_VL_ARB_REC = 0x36,
  132. IB_SA_ATTR_MC_GROUP_REC = 0x37,
  133. IB_SA_ATTR_MC_MEMBER_REC = 0x38,
  134. IB_SA_ATTR_TRACE_REC = 0x39,
  135. IB_SA_ATTR_MULTI_PATH_REC = 0x3a,
  136. IB_SA_ATTR_SERVICE_ASSOC_REC = 0x3b
  137. };
  138. #define PATH_REC_FIELD(field) \
  139. .struct_offset_bytes = offsetof(struct ib_sa_path_rec, field), \
  140. .struct_size_bytes = sizeof ((struct ib_sa_path_rec *) 0)->field, \
  141. .field_name = "sa_path_rec:" #field
  142. static const struct ib_field path_rec_table[] = {
  143. { RESERVED,
  144. .offset_words = 0,
  145. .offset_bits = 0,
  146. .size_bits = 32 },
  147. { RESERVED,
  148. .offset_words = 1,
  149. .offset_bits = 0,
  150. .size_bits = 32 },
  151. { PATH_REC_FIELD(dgid),
  152. .offset_words = 2,
  153. .offset_bits = 0,
  154. .size_bits = 128 },
  155. { PATH_REC_FIELD(sgid),
  156. .offset_words = 6,
  157. .offset_bits = 0,
  158. .size_bits = 128 },
  159. { PATH_REC_FIELD(dlid),
  160. .offset_words = 10,
  161. .offset_bits = 0,
  162. .size_bits = 16 },
  163. { PATH_REC_FIELD(slid),
  164. .offset_words = 10,
  165. .offset_bits = 16,
  166. .size_bits = 16 },
  167. { PATH_REC_FIELD(raw_traffic),
  168. .offset_words = 11,
  169. .offset_bits = 0,
  170. .size_bits = 1 },
  171. { RESERVED,
  172. .offset_words = 11,
  173. .offset_bits = 1,
  174. .size_bits = 3 },
  175. { PATH_REC_FIELD(flow_label),
  176. .offset_words = 11,
  177. .offset_bits = 4,
  178. .size_bits = 20 },
  179. { PATH_REC_FIELD(hop_limit),
  180. .offset_words = 11,
  181. .offset_bits = 24,
  182. .size_bits = 8 },
  183. { PATH_REC_FIELD(traffic_class),
  184. .offset_words = 12,
  185. .offset_bits = 0,
  186. .size_bits = 8 },
  187. { PATH_REC_FIELD(reversible),
  188. .offset_words = 12,
  189. .offset_bits = 8,
  190. .size_bits = 1 },
  191. { PATH_REC_FIELD(numb_path),
  192. .offset_words = 12,
  193. .offset_bits = 9,
  194. .size_bits = 7 },
  195. { PATH_REC_FIELD(pkey),
  196. .offset_words = 12,
  197. .offset_bits = 16,
  198. .size_bits = 16 },
  199. { RESERVED,
  200. .offset_words = 13,
  201. .offset_bits = 0,
  202. .size_bits = 12 },
  203. { PATH_REC_FIELD(sl),
  204. .offset_words = 13,
  205. .offset_bits = 12,
  206. .size_bits = 4 },
  207. { PATH_REC_FIELD(mtu_selector),
  208. .offset_words = 13,
  209. .offset_bits = 16,
  210. .size_bits = 2 },
  211. { PATH_REC_FIELD(mtu),
  212. .offset_words = 13,
  213. .offset_bits = 18,
  214. .size_bits = 6 },
  215. { PATH_REC_FIELD(rate_selector),
  216. .offset_words = 13,
  217. .offset_bits = 24,
  218. .size_bits = 2 },
  219. { PATH_REC_FIELD(rate),
  220. .offset_words = 13,
  221. .offset_bits = 26,
  222. .size_bits = 6 },
  223. { PATH_REC_FIELD(packet_life_time_selector),
  224. .offset_words = 14,
  225. .offset_bits = 0,
  226. .size_bits = 2 },
  227. { PATH_REC_FIELD(packet_life_time),
  228. .offset_words = 14,
  229. .offset_bits = 2,
  230. .size_bits = 6 },
  231. { PATH_REC_FIELD(preference),
  232. .offset_words = 14,
  233. .offset_bits = 8,
  234. .size_bits = 8 },
  235. { RESERVED,
  236. .offset_words = 14,
  237. .offset_bits = 16,
  238. .size_bits = 48 },
  239. };
  240. #define MCMEMBER_REC_FIELD(field) \
  241. .struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field), \
  242. .struct_size_bytes = sizeof ((struct ib_sa_mcmember_rec *) 0)->field, \
  243. .field_name = "sa_mcmember_rec:" #field
  244. static const struct ib_field mcmember_rec_table[] = {
  245. { MCMEMBER_REC_FIELD(mgid),
  246. .offset_words = 0,
  247. .offset_bits = 0,
  248. .size_bits = 128 },
  249. { MCMEMBER_REC_FIELD(port_gid),
  250. .offset_words = 4,
  251. .offset_bits = 0,
  252. .size_bits = 128 },
  253. { MCMEMBER_REC_FIELD(qkey),
  254. .offset_words = 8,
  255. .offset_bits = 0,
  256. .size_bits = 32 },
  257. { MCMEMBER_REC_FIELD(mlid),
  258. .offset_words = 9,
  259. .offset_bits = 0,
  260. .size_bits = 16 },
  261. { MCMEMBER_REC_FIELD(mtu_selector),
  262. .offset_words = 9,
  263. .offset_bits = 16,
  264. .size_bits = 2 },
  265. { MCMEMBER_REC_FIELD(mtu),
  266. .offset_words = 9,
  267. .offset_bits = 18,
  268. .size_bits = 6 },
  269. { MCMEMBER_REC_FIELD(traffic_class),
  270. .offset_words = 9,
  271. .offset_bits = 24,
  272. .size_bits = 8 },
  273. { MCMEMBER_REC_FIELD(pkey),
  274. .offset_words = 10,
  275. .offset_bits = 0,
  276. .size_bits = 16 },
  277. { MCMEMBER_REC_FIELD(rate_selector),
  278. .offset_words = 10,
  279. .offset_bits = 16,
  280. .size_bits = 2 },
  281. { MCMEMBER_REC_FIELD(rate),
  282. .offset_words = 10,
  283. .offset_bits = 18,
  284. .size_bits = 6 },
  285. { MCMEMBER_REC_FIELD(packet_life_time_selector),
  286. .offset_words = 10,
  287. .offset_bits = 24,
  288. .size_bits = 2 },
  289. { MCMEMBER_REC_FIELD(packet_life_time),
  290. .offset_words = 10,
  291. .offset_bits = 26,
  292. .size_bits = 6 },
  293. { MCMEMBER_REC_FIELD(sl),
  294. .offset_words = 11,
  295. .offset_bits = 0,
  296. .size_bits = 4 },
  297. { MCMEMBER_REC_FIELD(flow_label),
  298. .offset_words = 11,
  299. .offset_bits = 4,
  300. .size_bits = 20 },
  301. { MCMEMBER_REC_FIELD(hop_limit),
  302. .offset_words = 11,
  303. .offset_bits = 24,
  304. .size_bits = 8 },
  305. { MCMEMBER_REC_FIELD(scope),
  306. .offset_words = 12,
  307. .offset_bits = 0,
  308. .size_bits = 4 },
  309. { MCMEMBER_REC_FIELD(join_state),
  310. .offset_words = 12,
  311. .offset_bits = 4,
  312. .size_bits = 4 },
  313. { MCMEMBER_REC_FIELD(proxy_join),
  314. .offset_words = 12,
  315. .offset_bits = 8,
  316. .size_bits = 1 },
  317. { RESERVED,
  318. .offset_words = 12,
  319. .offset_bits = 9,
  320. .size_bits = 23 },
  321. };
  322. static void free_sm_ah(struct kref *kref)
  323. {
  324. struct ib_sa_sm_ah *sm_ah = container_of(kref, struct ib_sa_sm_ah, ref);
  325. ib_destroy_ah(sm_ah->ah);
  326. kfree(sm_ah);
  327. }
  328. static void update_sm_ah(void *port_ptr)
  329. {
  330. struct ib_sa_port *port = port_ptr;
  331. struct ib_sa_sm_ah *new_ah, *old_ah;
  332. struct ib_port_attr port_attr;
  333. struct ib_ah_attr ah_attr;
  334. if (ib_query_port(port->agent->device, port->port_num, &port_attr)) {
  335. printk(KERN_WARNING "Couldn't query port\n");
  336. return;
  337. }
  338. new_ah = kmalloc(sizeof *new_ah, GFP_KERNEL);
  339. if (!new_ah) {
  340. printk(KERN_WARNING "Couldn't allocate new SM AH\n");
  341. return;
  342. }
  343. kref_init(&new_ah->ref);
  344. memset(&ah_attr, 0, sizeof ah_attr);
  345. ah_attr.dlid = port_attr.sm_lid;
  346. ah_attr.sl = port_attr.sm_sl;
  347. ah_attr.port_num = port->port_num;
  348. new_ah->ah = ib_create_ah(port->agent->qp->pd, &ah_attr);
  349. if (IS_ERR(new_ah->ah)) {
  350. printk(KERN_WARNING "Couldn't create new SM AH\n");
  351. kfree(new_ah);
  352. return;
  353. }
  354. spin_lock_irq(&port->ah_lock);
  355. old_ah = port->sm_ah;
  356. port->sm_ah = new_ah;
  357. spin_unlock_irq(&port->ah_lock);
  358. if (old_ah)
  359. kref_put(&old_ah->ref, free_sm_ah);
  360. }
  361. static void ib_sa_event(struct ib_event_handler *handler, struct ib_event *event)
  362. {
  363. if (event->event == IB_EVENT_PORT_ERR ||
  364. event->event == IB_EVENT_PORT_ACTIVE ||
  365. event->event == IB_EVENT_LID_CHANGE ||
  366. event->event == IB_EVENT_PKEY_CHANGE ||
  367. event->event == IB_EVENT_SM_CHANGE) {
  368. struct ib_sa_device *sa_dev =
  369. ib_get_client_data(event->device, &sa_client);
  370. schedule_work(&sa_dev->port[event->element.port_num -
  371. sa_dev->start_port].update_task);
  372. }
  373. }
  374. /**
  375. * ib_sa_cancel_query - try to cancel an SA query
  376. * @id:ID of query to cancel
  377. * @query:query pointer to cancel
  378. *
  379. * Try to cancel an SA query. If the id and query don't match up or
  380. * the query has already completed, nothing is done. Otherwise the
  381. * query is canceled and will complete with a status of -EINTR.
  382. */
  383. void ib_sa_cancel_query(int id, struct ib_sa_query *query)
  384. {
  385. unsigned long flags;
  386. struct ib_mad_agent *agent;
  387. spin_lock_irqsave(&idr_lock, flags);
  388. if (idr_find(&query_idr, id) != query) {
  389. spin_unlock_irqrestore(&idr_lock, flags);
  390. return;
  391. }
  392. agent = query->port->agent;
  393. spin_unlock_irqrestore(&idr_lock, flags);
  394. ib_cancel_mad(agent, id);
  395. }
  396. EXPORT_SYMBOL(ib_sa_cancel_query);
  397. static void init_mad(struct ib_sa_mad *mad, struct ib_mad_agent *agent)
  398. {
  399. unsigned long flags;
  400. memset(mad, 0, sizeof *mad);
  401. mad->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
  402. mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  403. mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
  404. spin_lock_irqsave(&tid_lock, flags);
  405. mad->mad_hdr.tid =
  406. cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
  407. spin_unlock_irqrestore(&tid_lock, flags);
  408. }
  409. static int send_mad(struct ib_sa_query *query, int timeout_ms)
  410. {
  411. struct ib_sa_port *port = query->port;
  412. unsigned long flags;
  413. int ret;
  414. struct ib_sge gather_list;
  415. struct ib_send_wr *bad_wr, wr = {
  416. .opcode = IB_WR_SEND,
  417. .sg_list = &gather_list,
  418. .num_sge = 1,
  419. .send_flags = IB_SEND_SIGNALED,
  420. .wr = {
  421. .ud = {
  422. .mad_hdr = &query->mad->mad_hdr,
  423. .remote_qpn = 1,
  424. .remote_qkey = IB_QP1_QKEY,
  425. .timeout_ms = timeout_ms
  426. }
  427. }
  428. };
  429. retry:
  430. if (!idr_pre_get(&query_idr, GFP_ATOMIC))
  431. return -ENOMEM;
  432. spin_lock_irqsave(&idr_lock, flags);
  433. ret = idr_get_new(&query_idr, query, &query->id);
  434. spin_unlock_irqrestore(&idr_lock, flags);
  435. if (ret == -EAGAIN)
  436. goto retry;
  437. if (ret)
  438. return ret;
  439. wr.wr_id = query->id;
  440. spin_lock_irqsave(&port->ah_lock, flags);
  441. kref_get(&port->sm_ah->ref);
  442. query->sm_ah = port->sm_ah;
  443. wr.wr.ud.ah = port->sm_ah->ah;
  444. spin_unlock_irqrestore(&port->ah_lock, flags);
  445. gather_list.addr = dma_map_single(port->agent->device->dma_device,
  446. query->mad,
  447. sizeof (struct ib_sa_mad),
  448. DMA_TO_DEVICE);
  449. gather_list.length = sizeof (struct ib_sa_mad);
  450. gather_list.lkey = port->agent->mr->lkey;
  451. pci_unmap_addr_set(query, mapping, gather_list.addr);
  452. ret = ib_post_send_mad(port->agent, &wr, &bad_wr);
  453. if (ret) {
  454. dma_unmap_single(port->agent->device->dma_device,
  455. pci_unmap_addr(query, mapping),
  456. sizeof (struct ib_sa_mad),
  457. DMA_TO_DEVICE);
  458. kref_put(&query->sm_ah->ref, free_sm_ah);
  459. spin_lock_irqsave(&idr_lock, flags);
  460. idr_remove(&query_idr, query->id);
  461. spin_unlock_irqrestore(&idr_lock, flags);
  462. }
  463. /*
  464. * It's not safe to dereference query any more, because the
  465. * send may already have completed and freed the query in
  466. * another context. So use wr.wr_id, which has a copy of the
  467. * query's id.
  468. */
  469. return ret ? ret : wr.wr_id;
  470. }
  471. static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
  472. int status,
  473. struct ib_sa_mad *mad)
  474. {
  475. struct ib_sa_path_query *query =
  476. container_of(sa_query, struct ib_sa_path_query, sa_query);
  477. if (mad) {
  478. struct ib_sa_path_rec rec;
  479. ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table),
  480. mad->data, &rec);
  481. query->callback(status, &rec, query->context);
  482. } else
  483. query->callback(status, NULL, query->context);
  484. }
  485. static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
  486. {
  487. kfree(sa_query->mad);
  488. kfree(container_of(sa_query, struct ib_sa_path_query, sa_query));
  489. }
  490. /**
  491. * ib_sa_path_rec_get - Start a Path get query
  492. * @device:device to send query on
  493. * @port_num: port number to send query on
  494. * @rec:Path Record to send in query
  495. * @comp_mask:component mask to send in query
  496. * @timeout_ms:time to wait for response
  497. * @gfp_mask:GFP mask to use for internal allocations
  498. * @callback:function called when query completes, times out or is
  499. * canceled
  500. * @context:opaque user context passed to callback
  501. * @sa_query:query context, used to cancel query
  502. *
  503. * Send a Path Record Get query to the SA to look up a path. The
  504. * callback function will be called when the query completes (or
  505. * fails); status is 0 for a successful response, -EINTR if the query
  506. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  507. * occurred sending the query. The resp parameter of the callback is
  508. * only valid if status is 0.
  509. *
  510. * If the return value of ib_sa_path_rec_get() is negative, it is an
  511. * error code. Otherwise it is a query ID that can be used to cancel
  512. * the query.
  513. */
  514. int ib_sa_path_rec_get(struct ib_device *device, u8 port_num,
  515. struct ib_sa_path_rec *rec,
  516. ib_sa_comp_mask comp_mask,
  517. int timeout_ms, int gfp_mask,
  518. void (*callback)(int status,
  519. struct ib_sa_path_rec *resp,
  520. void *context),
  521. void *context,
  522. struct ib_sa_query **sa_query)
  523. {
  524. struct ib_sa_path_query *query;
  525. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  526. struct ib_sa_port *port = &sa_dev->port[port_num - sa_dev->start_port];
  527. struct ib_mad_agent *agent = port->agent;
  528. int ret;
  529. query = kmalloc(sizeof *query, gfp_mask);
  530. if (!query)
  531. return -ENOMEM;
  532. query->sa_query.mad = kmalloc(sizeof *query->sa_query.mad, gfp_mask);
  533. if (!query->sa_query.mad) {
  534. kfree(query);
  535. return -ENOMEM;
  536. }
  537. query->callback = callback;
  538. query->context = context;
  539. init_mad(query->sa_query.mad, agent);
  540. query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL;
  541. query->sa_query.release = ib_sa_path_rec_release;
  542. query->sa_query.port = port;
  543. query->sa_query.mad->mad_hdr.method = IB_MGMT_METHOD_GET;
  544. query->sa_query.mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_PATH_REC);
  545. query->sa_query.mad->sa_hdr.comp_mask = comp_mask;
  546. ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table),
  547. rec, query->sa_query.mad->data);
  548. *sa_query = &query->sa_query;
  549. ret = send_mad(&query->sa_query, timeout_ms);
  550. if (ret < 0) {
  551. *sa_query = NULL;
  552. kfree(query->sa_query.mad);
  553. kfree(query);
  554. }
  555. return ret;
  556. }
  557. EXPORT_SYMBOL(ib_sa_path_rec_get);
  558. static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
  559. int status,
  560. struct ib_sa_mad *mad)
  561. {
  562. struct ib_sa_mcmember_query *query =
  563. container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
  564. if (mad) {
  565. struct ib_sa_mcmember_rec rec;
  566. ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  567. mad->data, &rec);
  568. query->callback(status, &rec, query->context);
  569. } else
  570. query->callback(status, NULL, query->context);
  571. }
  572. static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
  573. {
  574. kfree(sa_query->mad);
  575. kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
  576. }
  577. int ib_sa_mcmember_rec_query(struct ib_device *device, u8 port_num,
  578. u8 method,
  579. struct ib_sa_mcmember_rec *rec,
  580. ib_sa_comp_mask comp_mask,
  581. int timeout_ms, int gfp_mask,
  582. void (*callback)(int status,
  583. struct ib_sa_mcmember_rec *resp,
  584. void *context),
  585. void *context,
  586. struct ib_sa_query **sa_query)
  587. {
  588. struct ib_sa_mcmember_query *query;
  589. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  590. struct ib_sa_port *port = &sa_dev->port[port_num - sa_dev->start_port];
  591. struct ib_mad_agent *agent = port->agent;
  592. int ret;
  593. query = kmalloc(sizeof *query, gfp_mask);
  594. if (!query)
  595. return -ENOMEM;
  596. query->sa_query.mad = kmalloc(sizeof *query->sa_query.mad, gfp_mask);
  597. if (!query->sa_query.mad) {
  598. kfree(query);
  599. return -ENOMEM;
  600. }
  601. query->callback = callback;
  602. query->context = context;
  603. init_mad(query->sa_query.mad, agent);
  604. query->sa_query.callback = callback ? ib_sa_mcmember_rec_callback : NULL;
  605. query->sa_query.release = ib_sa_mcmember_rec_release;
  606. query->sa_query.port = port;
  607. query->sa_query.mad->mad_hdr.method = method;
  608. query->sa_query.mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
  609. query->sa_query.mad->sa_hdr.comp_mask = comp_mask;
  610. ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  611. rec, query->sa_query.mad->data);
  612. *sa_query = &query->sa_query;
  613. ret = send_mad(&query->sa_query, timeout_ms);
  614. if (ret < 0) {
  615. *sa_query = NULL;
  616. kfree(query->sa_query.mad);
  617. kfree(query);
  618. }
  619. return ret;
  620. }
  621. EXPORT_SYMBOL(ib_sa_mcmember_rec_query);
  622. static void send_handler(struct ib_mad_agent *agent,
  623. struct ib_mad_send_wc *mad_send_wc)
  624. {
  625. struct ib_sa_query *query;
  626. unsigned long flags;
  627. spin_lock_irqsave(&idr_lock, flags);
  628. query = idr_find(&query_idr, mad_send_wc->wr_id);
  629. spin_unlock_irqrestore(&idr_lock, flags);
  630. if (!query)
  631. return;
  632. if (query->callback)
  633. switch (mad_send_wc->status) {
  634. case IB_WC_SUCCESS:
  635. /* No callback -- already got recv */
  636. break;
  637. case IB_WC_RESP_TIMEOUT_ERR:
  638. query->callback(query, -ETIMEDOUT, NULL);
  639. break;
  640. case IB_WC_WR_FLUSH_ERR:
  641. query->callback(query, -EINTR, NULL);
  642. break;
  643. default:
  644. query->callback(query, -EIO, NULL);
  645. break;
  646. }
  647. dma_unmap_single(agent->device->dma_device,
  648. pci_unmap_addr(query, mapping),
  649. sizeof (struct ib_sa_mad),
  650. DMA_TO_DEVICE);
  651. kref_put(&query->sm_ah->ref, free_sm_ah);
  652. query->release(query);
  653. spin_lock_irqsave(&idr_lock, flags);
  654. idr_remove(&query_idr, mad_send_wc->wr_id);
  655. spin_unlock_irqrestore(&idr_lock, flags);
  656. }
  657. static void recv_handler(struct ib_mad_agent *mad_agent,
  658. struct ib_mad_recv_wc *mad_recv_wc)
  659. {
  660. struct ib_sa_query *query;
  661. unsigned long flags;
  662. spin_lock_irqsave(&idr_lock, flags);
  663. query = idr_find(&query_idr, mad_recv_wc->wc->wr_id);
  664. spin_unlock_irqrestore(&idr_lock, flags);
  665. if (query && query->callback) {
  666. if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
  667. query->callback(query,
  668. mad_recv_wc->recv_buf.mad->mad_hdr.status ?
  669. -EINVAL : 0,
  670. (struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
  671. else
  672. query->callback(query, -EIO, NULL);
  673. }
  674. ib_free_recv_mad(mad_recv_wc);
  675. }
  676. static void ib_sa_add_one(struct ib_device *device)
  677. {
  678. struct ib_sa_device *sa_dev;
  679. int s, e, i;
  680. if (device->node_type == IB_NODE_SWITCH)
  681. s = e = 0;
  682. else {
  683. s = 1;
  684. e = device->phys_port_cnt;
  685. }
  686. sa_dev = kmalloc(sizeof *sa_dev +
  687. (e - s + 1) * sizeof (struct ib_sa_port),
  688. GFP_KERNEL);
  689. if (!sa_dev)
  690. return;
  691. sa_dev->start_port = s;
  692. sa_dev->end_port = e;
  693. for (i = 0; i <= e - s; ++i) {
  694. sa_dev->port[i].sm_ah = NULL;
  695. sa_dev->port[i].port_num = i + s;
  696. spin_lock_init(&sa_dev->port[i].ah_lock);
  697. sa_dev->port[i].agent =
  698. ib_register_mad_agent(device, i + s, IB_QPT_GSI,
  699. NULL, 0, send_handler,
  700. recv_handler, sa_dev);
  701. if (IS_ERR(sa_dev->port[i].agent))
  702. goto err;
  703. INIT_WORK(&sa_dev->port[i].update_task,
  704. update_sm_ah, &sa_dev->port[i]);
  705. }
  706. ib_set_client_data(device, &sa_client, sa_dev);
  707. /*
  708. * We register our event handler after everything is set up,
  709. * and then update our cached info after the event handler is
  710. * registered to avoid any problems if a port changes state
  711. * during our initialization.
  712. */
  713. INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
  714. if (ib_register_event_handler(&sa_dev->event_handler))
  715. goto err;
  716. for (i = 0; i <= e - s; ++i)
  717. update_sm_ah(&sa_dev->port[i]);
  718. return;
  719. err:
  720. while (--i >= 0)
  721. ib_unregister_mad_agent(sa_dev->port[i].agent);
  722. kfree(sa_dev);
  723. return;
  724. }
  725. static void ib_sa_remove_one(struct ib_device *device)
  726. {
  727. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  728. int i;
  729. if (!sa_dev)
  730. return;
  731. ib_unregister_event_handler(&sa_dev->event_handler);
  732. for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
  733. ib_unregister_mad_agent(sa_dev->port[i].agent);
  734. kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
  735. }
  736. kfree(sa_dev);
  737. }
  738. static int __init ib_sa_init(void)
  739. {
  740. int ret;
  741. spin_lock_init(&idr_lock);
  742. spin_lock_init(&tid_lock);
  743. get_random_bytes(&tid, sizeof tid);
  744. ret = ib_register_client(&sa_client);
  745. if (ret)
  746. printk(KERN_ERR "Couldn't register ib_sa client\n");
  747. return ret;
  748. }
  749. static void __exit ib_sa_cleanup(void)
  750. {
  751. ib_unregister_client(&sa_client);
  752. }
  753. module_init(ib_sa_init);
  754. module_exit(ib_sa_cleanup);