sa_query.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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. .retries = 0
  427. }
  428. }
  429. };
  430. retry:
  431. if (!idr_pre_get(&query_idr, GFP_ATOMIC))
  432. return -ENOMEM;
  433. spin_lock_irqsave(&idr_lock, flags);
  434. ret = idr_get_new(&query_idr, query, &query->id);
  435. spin_unlock_irqrestore(&idr_lock, flags);
  436. if (ret == -EAGAIN)
  437. goto retry;
  438. if (ret)
  439. return ret;
  440. wr.wr_id = query->id;
  441. spin_lock_irqsave(&port->ah_lock, flags);
  442. kref_get(&port->sm_ah->ref);
  443. query->sm_ah = port->sm_ah;
  444. wr.wr.ud.ah = port->sm_ah->ah;
  445. spin_unlock_irqrestore(&port->ah_lock, flags);
  446. gather_list.addr = dma_map_single(port->agent->device->dma_device,
  447. query->mad,
  448. sizeof (struct ib_sa_mad),
  449. DMA_TO_DEVICE);
  450. gather_list.length = sizeof (struct ib_sa_mad);
  451. gather_list.lkey = port->agent->mr->lkey;
  452. pci_unmap_addr_set(query, mapping, gather_list.addr);
  453. ret = ib_post_send_mad(port->agent, &wr, &bad_wr);
  454. if (ret) {
  455. dma_unmap_single(port->agent->device->dma_device,
  456. pci_unmap_addr(query, mapping),
  457. sizeof (struct ib_sa_mad),
  458. DMA_TO_DEVICE);
  459. kref_put(&query->sm_ah->ref, free_sm_ah);
  460. spin_lock_irqsave(&idr_lock, flags);
  461. idr_remove(&query_idr, query->id);
  462. spin_unlock_irqrestore(&idr_lock, flags);
  463. }
  464. /*
  465. * It's not safe to dereference query any more, because the
  466. * send may already have completed and freed the query in
  467. * another context. So use wr.wr_id, which has a copy of the
  468. * query's id.
  469. */
  470. return ret ? ret : wr.wr_id;
  471. }
  472. static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
  473. int status,
  474. struct ib_sa_mad *mad)
  475. {
  476. struct ib_sa_path_query *query =
  477. container_of(sa_query, struct ib_sa_path_query, sa_query);
  478. if (mad) {
  479. struct ib_sa_path_rec rec;
  480. ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table),
  481. mad->data, &rec);
  482. query->callback(status, &rec, query->context);
  483. } else
  484. query->callback(status, NULL, query->context);
  485. }
  486. static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
  487. {
  488. kfree(sa_query->mad);
  489. kfree(container_of(sa_query, struct ib_sa_path_query, sa_query));
  490. }
  491. /**
  492. * ib_sa_path_rec_get - Start a Path get query
  493. * @device:device to send query on
  494. * @port_num: port number to send query on
  495. * @rec:Path Record to send in query
  496. * @comp_mask:component mask to send in query
  497. * @timeout_ms:time to wait for response
  498. * @gfp_mask:GFP mask to use for internal allocations
  499. * @callback:function called when query completes, times out or is
  500. * canceled
  501. * @context:opaque user context passed to callback
  502. * @sa_query:query context, used to cancel query
  503. *
  504. * Send a Path Record Get query to the SA to look up a path. The
  505. * callback function will be called when the query completes (or
  506. * fails); status is 0 for a successful response, -EINTR if the query
  507. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  508. * occurred sending the query. The resp parameter of the callback is
  509. * only valid if status is 0.
  510. *
  511. * If the return value of ib_sa_path_rec_get() is negative, it is an
  512. * error code. Otherwise it is a query ID that can be used to cancel
  513. * the query.
  514. */
  515. int ib_sa_path_rec_get(struct ib_device *device, u8 port_num,
  516. struct ib_sa_path_rec *rec,
  517. ib_sa_comp_mask comp_mask,
  518. int timeout_ms, int gfp_mask,
  519. void (*callback)(int status,
  520. struct ib_sa_path_rec *resp,
  521. void *context),
  522. void *context,
  523. struct ib_sa_query **sa_query)
  524. {
  525. struct ib_sa_path_query *query;
  526. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  527. struct ib_sa_port *port = &sa_dev->port[port_num - sa_dev->start_port];
  528. struct ib_mad_agent *agent = port->agent;
  529. int ret;
  530. query = kmalloc(sizeof *query, gfp_mask);
  531. if (!query)
  532. return -ENOMEM;
  533. query->sa_query.mad = kmalloc(sizeof *query->sa_query.mad, gfp_mask);
  534. if (!query->sa_query.mad) {
  535. kfree(query);
  536. return -ENOMEM;
  537. }
  538. query->callback = callback;
  539. query->context = context;
  540. init_mad(query->sa_query.mad, agent);
  541. query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL;
  542. query->sa_query.release = ib_sa_path_rec_release;
  543. query->sa_query.port = port;
  544. query->sa_query.mad->mad_hdr.method = IB_MGMT_METHOD_GET;
  545. query->sa_query.mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_PATH_REC);
  546. query->sa_query.mad->sa_hdr.comp_mask = comp_mask;
  547. ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table),
  548. rec, query->sa_query.mad->data);
  549. *sa_query = &query->sa_query;
  550. ret = send_mad(&query->sa_query, timeout_ms);
  551. if (ret < 0) {
  552. *sa_query = NULL;
  553. kfree(query->sa_query.mad);
  554. kfree(query);
  555. }
  556. return ret;
  557. }
  558. EXPORT_SYMBOL(ib_sa_path_rec_get);
  559. static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
  560. int status,
  561. struct ib_sa_mad *mad)
  562. {
  563. struct ib_sa_mcmember_query *query =
  564. container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
  565. if (mad) {
  566. struct ib_sa_mcmember_rec rec;
  567. ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  568. mad->data, &rec);
  569. query->callback(status, &rec, query->context);
  570. } else
  571. query->callback(status, NULL, query->context);
  572. }
  573. static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
  574. {
  575. kfree(sa_query->mad);
  576. kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
  577. }
  578. int ib_sa_mcmember_rec_query(struct ib_device *device, u8 port_num,
  579. u8 method,
  580. struct ib_sa_mcmember_rec *rec,
  581. ib_sa_comp_mask comp_mask,
  582. int timeout_ms, int gfp_mask,
  583. void (*callback)(int status,
  584. struct ib_sa_mcmember_rec *resp,
  585. void *context),
  586. void *context,
  587. struct ib_sa_query **sa_query)
  588. {
  589. struct ib_sa_mcmember_query *query;
  590. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  591. struct ib_sa_port *port = &sa_dev->port[port_num - sa_dev->start_port];
  592. struct ib_mad_agent *agent = port->agent;
  593. int ret;
  594. query = kmalloc(sizeof *query, gfp_mask);
  595. if (!query)
  596. return -ENOMEM;
  597. query->sa_query.mad = kmalloc(sizeof *query->sa_query.mad, gfp_mask);
  598. if (!query->sa_query.mad) {
  599. kfree(query);
  600. return -ENOMEM;
  601. }
  602. query->callback = callback;
  603. query->context = context;
  604. init_mad(query->sa_query.mad, agent);
  605. query->sa_query.callback = callback ? ib_sa_mcmember_rec_callback : NULL;
  606. query->sa_query.release = ib_sa_mcmember_rec_release;
  607. query->sa_query.port = port;
  608. query->sa_query.mad->mad_hdr.method = method;
  609. query->sa_query.mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
  610. query->sa_query.mad->sa_hdr.comp_mask = comp_mask;
  611. ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  612. rec, query->sa_query.mad->data);
  613. *sa_query = &query->sa_query;
  614. ret = send_mad(&query->sa_query, timeout_ms);
  615. if (ret < 0) {
  616. *sa_query = NULL;
  617. kfree(query->sa_query.mad);
  618. kfree(query);
  619. }
  620. return ret;
  621. }
  622. EXPORT_SYMBOL(ib_sa_mcmember_rec_query);
  623. static void send_handler(struct ib_mad_agent *agent,
  624. struct ib_mad_send_wc *mad_send_wc)
  625. {
  626. struct ib_sa_query *query;
  627. unsigned long flags;
  628. spin_lock_irqsave(&idr_lock, flags);
  629. query = idr_find(&query_idr, mad_send_wc->wr_id);
  630. spin_unlock_irqrestore(&idr_lock, flags);
  631. if (!query)
  632. return;
  633. if (query->callback)
  634. switch (mad_send_wc->status) {
  635. case IB_WC_SUCCESS:
  636. /* No callback -- already got recv */
  637. break;
  638. case IB_WC_RESP_TIMEOUT_ERR:
  639. query->callback(query, -ETIMEDOUT, NULL);
  640. break;
  641. case IB_WC_WR_FLUSH_ERR:
  642. query->callback(query, -EINTR, NULL);
  643. break;
  644. default:
  645. query->callback(query, -EIO, NULL);
  646. break;
  647. }
  648. dma_unmap_single(agent->device->dma_device,
  649. pci_unmap_addr(query, mapping),
  650. sizeof (struct ib_sa_mad),
  651. DMA_TO_DEVICE);
  652. kref_put(&query->sm_ah->ref, free_sm_ah);
  653. query->release(query);
  654. spin_lock_irqsave(&idr_lock, flags);
  655. idr_remove(&query_idr, mad_send_wc->wr_id);
  656. spin_unlock_irqrestore(&idr_lock, flags);
  657. }
  658. static void recv_handler(struct ib_mad_agent *mad_agent,
  659. struct ib_mad_recv_wc *mad_recv_wc)
  660. {
  661. struct ib_sa_query *query;
  662. unsigned long flags;
  663. spin_lock_irqsave(&idr_lock, flags);
  664. query = idr_find(&query_idr, mad_recv_wc->wc->wr_id);
  665. spin_unlock_irqrestore(&idr_lock, flags);
  666. if (query && query->callback) {
  667. if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
  668. query->callback(query,
  669. mad_recv_wc->recv_buf.mad->mad_hdr.status ?
  670. -EINVAL : 0,
  671. (struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
  672. else
  673. query->callback(query, -EIO, NULL);
  674. }
  675. ib_free_recv_mad(mad_recv_wc);
  676. }
  677. static void ib_sa_add_one(struct ib_device *device)
  678. {
  679. struct ib_sa_device *sa_dev;
  680. int s, e, i;
  681. if (device->node_type == IB_NODE_SWITCH)
  682. s = e = 0;
  683. else {
  684. s = 1;
  685. e = device->phys_port_cnt;
  686. }
  687. sa_dev = kmalloc(sizeof *sa_dev +
  688. (e - s + 1) * sizeof (struct ib_sa_port),
  689. GFP_KERNEL);
  690. if (!sa_dev)
  691. return;
  692. sa_dev->start_port = s;
  693. sa_dev->end_port = e;
  694. for (i = 0; i <= e - s; ++i) {
  695. sa_dev->port[i].sm_ah = NULL;
  696. sa_dev->port[i].port_num = i + s;
  697. spin_lock_init(&sa_dev->port[i].ah_lock);
  698. sa_dev->port[i].agent =
  699. ib_register_mad_agent(device, i + s, IB_QPT_GSI,
  700. NULL, 0, send_handler,
  701. recv_handler, sa_dev);
  702. if (IS_ERR(sa_dev->port[i].agent))
  703. goto err;
  704. INIT_WORK(&sa_dev->port[i].update_task,
  705. update_sm_ah, &sa_dev->port[i]);
  706. }
  707. ib_set_client_data(device, &sa_client, sa_dev);
  708. /*
  709. * We register our event handler after everything is set up,
  710. * and then update our cached info after the event handler is
  711. * registered to avoid any problems if a port changes state
  712. * during our initialization.
  713. */
  714. INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
  715. if (ib_register_event_handler(&sa_dev->event_handler))
  716. goto err;
  717. for (i = 0; i <= e - s; ++i)
  718. update_sm_ah(&sa_dev->port[i]);
  719. return;
  720. err:
  721. while (--i >= 0)
  722. ib_unregister_mad_agent(sa_dev->port[i].agent);
  723. kfree(sa_dev);
  724. return;
  725. }
  726. static void ib_sa_remove_one(struct ib_device *device)
  727. {
  728. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  729. int i;
  730. if (!sa_dev)
  731. return;
  732. ib_unregister_event_handler(&sa_dev->event_handler);
  733. for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
  734. ib_unregister_mad_agent(sa_dev->port[i].agent);
  735. kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
  736. }
  737. kfree(sa_dev);
  738. }
  739. static int __init ib_sa_init(void)
  740. {
  741. int ret;
  742. spin_lock_init(&idr_lock);
  743. spin_lock_init(&tid_lock);
  744. get_random_bytes(&tid, sizeof tid);
  745. ret = ib_register_client(&sa_client);
  746. if (ret)
  747. printk(KERN_ERR "Couldn't register ib_sa client\n");
  748. return ret;
  749. }
  750. static void __exit ib_sa_cleanup(void)
  751. {
  752. ib_unregister_client(&sa_client);
  753. }
  754. module_init(ib_sa_init);
  755. module_exit(ib_sa_cleanup);