sa_query.c 22 KB

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