sa_query.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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_mr *mr;
  74. struct ib_sa_sm_ah *sm_ah;
  75. struct work_struct update_task;
  76. spinlock_t ah_lock;
  77. u8 port_num;
  78. };
  79. struct ib_sa_device {
  80. int start_port, end_port;
  81. struct ib_event_handler event_handler;
  82. struct ib_sa_port port[0];
  83. };
  84. struct ib_sa_query {
  85. void (*callback)(struct ib_sa_query *, int, struct ib_sa_mad *);
  86. void (*release)(struct ib_sa_query *);
  87. struct ib_sa_port *port;
  88. struct ib_sa_mad *mad;
  89. struct ib_sa_sm_ah *sm_ah;
  90. DECLARE_PCI_UNMAP_ADDR(mapping)
  91. int id;
  92. };
  93. struct ib_sa_path_query {
  94. void (*callback)(int, struct ib_sa_path_rec *, void *);
  95. void *context;
  96. struct ib_sa_query sa_query;
  97. };
  98. struct ib_sa_mcmember_query {
  99. void (*callback)(int, struct ib_sa_mcmember_rec *, void *);
  100. void *context;
  101. struct ib_sa_query sa_query;
  102. };
  103. static void ib_sa_add_one(struct ib_device *device);
  104. static void ib_sa_remove_one(struct ib_device *device);
  105. static struct ib_client sa_client = {
  106. .name = "sa",
  107. .add = ib_sa_add_one,
  108. .remove = ib_sa_remove_one
  109. };
  110. static spinlock_t idr_lock;
  111. static DEFINE_IDR(query_idr);
  112. static spinlock_t tid_lock;
  113. static u32 tid;
  114. enum {
  115. IB_SA_ATTR_CLASS_PORTINFO = 0x01,
  116. IB_SA_ATTR_NOTICE = 0x02,
  117. IB_SA_ATTR_INFORM_INFO = 0x03,
  118. IB_SA_ATTR_NODE_REC = 0x11,
  119. IB_SA_ATTR_PORT_INFO_REC = 0x12,
  120. IB_SA_ATTR_SL2VL_REC = 0x13,
  121. IB_SA_ATTR_SWITCH_REC = 0x14,
  122. IB_SA_ATTR_LINEAR_FDB_REC = 0x15,
  123. IB_SA_ATTR_RANDOM_FDB_REC = 0x16,
  124. IB_SA_ATTR_MCAST_FDB_REC = 0x17,
  125. IB_SA_ATTR_SM_INFO_REC = 0x18,
  126. IB_SA_ATTR_LINK_REC = 0x20,
  127. IB_SA_ATTR_GUID_INFO_REC = 0x30,
  128. IB_SA_ATTR_SERVICE_REC = 0x31,
  129. IB_SA_ATTR_PARTITION_REC = 0x33,
  130. IB_SA_ATTR_RANGE_REC = 0x34,
  131. IB_SA_ATTR_PATH_REC = 0x35,
  132. IB_SA_ATTR_VL_ARB_REC = 0x36,
  133. IB_SA_ATTR_MC_GROUP_REC = 0x37,
  134. IB_SA_ATTR_MC_MEMBER_REC = 0x38,
  135. IB_SA_ATTR_TRACE_REC = 0x39,
  136. IB_SA_ATTR_MULTI_PATH_REC = 0x3a,
  137. IB_SA_ATTR_SERVICE_ASSOC_REC = 0x3b
  138. };
  139. #define PATH_REC_FIELD(field) \
  140. .struct_offset_bytes = offsetof(struct ib_sa_path_rec, field), \
  141. .struct_size_bytes = sizeof ((struct ib_sa_path_rec *) 0)->field, \
  142. .field_name = "sa_path_rec:" #field
  143. static const struct ib_field path_rec_table[] = {
  144. { RESERVED,
  145. .offset_words = 0,
  146. .offset_bits = 0,
  147. .size_bits = 32 },
  148. { RESERVED,
  149. .offset_words = 1,
  150. .offset_bits = 0,
  151. .size_bits = 32 },
  152. { PATH_REC_FIELD(dgid),
  153. .offset_words = 2,
  154. .offset_bits = 0,
  155. .size_bits = 128 },
  156. { PATH_REC_FIELD(sgid),
  157. .offset_words = 6,
  158. .offset_bits = 0,
  159. .size_bits = 128 },
  160. { PATH_REC_FIELD(dlid),
  161. .offset_words = 10,
  162. .offset_bits = 0,
  163. .size_bits = 16 },
  164. { PATH_REC_FIELD(slid),
  165. .offset_words = 10,
  166. .offset_bits = 16,
  167. .size_bits = 16 },
  168. { PATH_REC_FIELD(raw_traffic),
  169. .offset_words = 11,
  170. .offset_bits = 0,
  171. .size_bits = 1 },
  172. { RESERVED,
  173. .offset_words = 11,
  174. .offset_bits = 1,
  175. .size_bits = 3 },
  176. { PATH_REC_FIELD(flow_label),
  177. .offset_words = 11,
  178. .offset_bits = 4,
  179. .size_bits = 20 },
  180. { PATH_REC_FIELD(hop_limit),
  181. .offset_words = 11,
  182. .offset_bits = 24,
  183. .size_bits = 8 },
  184. { PATH_REC_FIELD(traffic_class),
  185. .offset_words = 12,
  186. .offset_bits = 0,
  187. .size_bits = 8 },
  188. { PATH_REC_FIELD(reversible),
  189. .offset_words = 12,
  190. .offset_bits = 8,
  191. .size_bits = 1 },
  192. { PATH_REC_FIELD(numb_path),
  193. .offset_words = 12,
  194. .offset_bits = 9,
  195. .size_bits = 7 },
  196. { PATH_REC_FIELD(pkey),
  197. .offset_words = 12,
  198. .offset_bits = 16,
  199. .size_bits = 16 },
  200. { RESERVED,
  201. .offset_words = 13,
  202. .offset_bits = 0,
  203. .size_bits = 12 },
  204. { PATH_REC_FIELD(sl),
  205. .offset_words = 13,
  206. .offset_bits = 12,
  207. .size_bits = 4 },
  208. { PATH_REC_FIELD(mtu_selector),
  209. .offset_words = 13,
  210. .offset_bits = 16,
  211. .size_bits = 2 },
  212. { PATH_REC_FIELD(mtu),
  213. .offset_words = 13,
  214. .offset_bits = 18,
  215. .size_bits = 6 },
  216. { PATH_REC_FIELD(rate_selector),
  217. .offset_words = 13,
  218. .offset_bits = 24,
  219. .size_bits = 2 },
  220. { PATH_REC_FIELD(rate),
  221. .offset_words = 13,
  222. .offset_bits = 26,
  223. .size_bits = 6 },
  224. { PATH_REC_FIELD(packet_life_time_selector),
  225. .offset_words = 14,
  226. .offset_bits = 0,
  227. .size_bits = 2 },
  228. { PATH_REC_FIELD(packet_life_time),
  229. .offset_words = 14,
  230. .offset_bits = 2,
  231. .size_bits = 6 },
  232. { PATH_REC_FIELD(preference),
  233. .offset_words = 14,
  234. .offset_bits = 8,
  235. .size_bits = 8 },
  236. { RESERVED,
  237. .offset_words = 14,
  238. .offset_bits = 16,
  239. .size_bits = 48 },
  240. };
  241. #define MCMEMBER_REC_FIELD(field) \
  242. .struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field), \
  243. .struct_size_bytes = sizeof ((struct ib_sa_mcmember_rec *) 0)->field, \
  244. .field_name = "sa_mcmember_rec:" #field
  245. static const struct ib_field mcmember_rec_table[] = {
  246. { MCMEMBER_REC_FIELD(mgid),
  247. .offset_words = 0,
  248. .offset_bits = 0,
  249. .size_bits = 128 },
  250. { MCMEMBER_REC_FIELD(port_gid),
  251. .offset_words = 4,
  252. .offset_bits = 0,
  253. .size_bits = 128 },
  254. { MCMEMBER_REC_FIELD(qkey),
  255. .offset_words = 8,
  256. .offset_bits = 0,
  257. .size_bits = 32 },
  258. { MCMEMBER_REC_FIELD(mlid),
  259. .offset_words = 9,
  260. .offset_bits = 0,
  261. .size_bits = 16 },
  262. { MCMEMBER_REC_FIELD(mtu_selector),
  263. .offset_words = 9,
  264. .offset_bits = 16,
  265. .size_bits = 2 },
  266. { MCMEMBER_REC_FIELD(mtu),
  267. .offset_words = 9,
  268. .offset_bits = 18,
  269. .size_bits = 6 },
  270. { MCMEMBER_REC_FIELD(traffic_class),
  271. .offset_words = 9,
  272. .offset_bits = 24,
  273. .size_bits = 8 },
  274. { MCMEMBER_REC_FIELD(pkey),
  275. .offset_words = 10,
  276. .offset_bits = 0,
  277. .size_bits = 16 },
  278. { MCMEMBER_REC_FIELD(rate_selector),
  279. .offset_words = 10,
  280. .offset_bits = 16,
  281. .size_bits = 2 },
  282. { MCMEMBER_REC_FIELD(rate),
  283. .offset_words = 10,
  284. .offset_bits = 18,
  285. .size_bits = 6 },
  286. { MCMEMBER_REC_FIELD(packet_life_time_selector),
  287. .offset_words = 10,
  288. .offset_bits = 24,
  289. .size_bits = 2 },
  290. { MCMEMBER_REC_FIELD(packet_life_time),
  291. .offset_words = 10,
  292. .offset_bits = 26,
  293. .size_bits = 6 },
  294. { MCMEMBER_REC_FIELD(sl),
  295. .offset_words = 11,
  296. .offset_bits = 0,
  297. .size_bits = 4 },
  298. { MCMEMBER_REC_FIELD(flow_label),
  299. .offset_words = 11,
  300. .offset_bits = 4,
  301. .size_bits = 20 },
  302. { MCMEMBER_REC_FIELD(hop_limit),
  303. .offset_words = 11,
  304. .offset_bits = 24,
  305. .size_bits = 8 },
  306. { MCMEMBER_REC_FIELD(scope),
  307. .offset_words = 12,
  308. .offset_bits = 0,
  309. .size_bits = 4 },
  310. { MCMEMBER_REC_FIELD(join_state),
  311. .offset_words = 12,
  312. .offset_bits = 4,
  313. .size_bits = 4 },
  314. { MCMEMBER_REC_FIELD(proxy_join),
  315. .offset_words = 12,
  316. .offset_bits = 8,
  317. .size_bits = 1 },
  318. { RESERVED,
  319. .offset_words = 12,
  320. .offset_bits = 9,
  321. .size_bits = 23 },
  322. };
  323. static void free_sm_ah(struct kref *kref)
  324. {
  325. struct ib_sa_sm_ah *sm_ah = container_of(kref, struct ib_sa_sm_ah, ref);
  326. ib_destroy_ah(sm_ah->ah);
  327. kfree(sm_ah);
  328. }
  329. static void update_sm_ah(void *port_ptr)
  330. {
  331. struct ib_sa_port *port = port_ptr;
  332. struct ib_sa_sm_ah *new_ah, *old_ah;
  333. struct ib_port_attr port_attr;
  334. struct ib_ah_attr ah_attr;
  335. if (ib_query_port(port->agent->device, port->port_num, &port_attr)) {
  336. printk(KERN_WARNING "Couldn't query port\n");
  337. return;
  338. }
  339. new_ah = kmalloc(sizeof *new_ah, GFP_KERNEL);
  340. if (!new_ah) {
  341. printk(KERN_WARNING "Couldn't allocate new SM AH\n");
  342. return;
  343. }
  344. kref_init(&new_ah->ref);
  345. memset(&ah_attr, 0, sizeof ah_attr);
  346. ah_attr.dlid = port_attr.sm_lid;
  347. ah_attr.sl = port_attr.sm_sl;
  348. ah_attr.port_num = port->port_num;
  349. new_ah->ah = ib_create_ah(port->agent->qp->pd, &ah_attr);
  350. if (IS_ERR(new_ah->ah)) {
  351. printk(KERN_WARNING "Couldn't create new SM AH\n");
  352. kfree(new_ah);
  353. return;
  354. }
  355. spin_lock_irq(&port->ah_lock);
  356. old_ah = port->sm_ah;
  357. port->sm_ah = new_ah;
  358. spin_unlock_irq(&port->ah_lock);
  359. if (old_ah)
  360. kref_put(&old_ah->ref, free_sm_ah);
  361. }
  362. static void ib_sa_event(struct ib_event_handler *handler, struct ib_event *event)
  363. {
  364. if (event->event == IB_EVENT_PORT_ERR ||
  365. event->event == IB_EVENT_PORT_ACTIVE ||
  366. event->event == IB_EVENT_LID_CHANGE ||
  367. event->event == IB_EVENT_PKEY_CHANGE ||
  368. event->event == IB_EVENT_SM_CHANGE) {
  369. struct ib_sa_device *sa_dev =
  370. ib_get_client_data(event->device, &sa_client);
  371. schedule_work(&sa_dev->port[event->element.port_num -
  372. sa_dev->start_port].update_task);
  373. }
  374. }
  375. /**
  376. * ib_sa_cancel_query - try to cancel an SA query
  377. * @id:ID of query to cancel
  378. * @query:query pointer to cancel
  379. *
  380. * Try to cancel an SA query. If the id and query don't match up or
  381. * the query has already completed, nothing is done. Otherwise the
  382. * query is canceled and will complete with a status of -EINTR.
  383. */
  384. void ib_sa_cancel_query(int id, struct ib_sa_query *query)
  385. {
  386. unsigned long flags;
  387. struct ib_mad_agent *agent;
  388. spin_lock_irqsave(&idr_lock, flags);
  389. if (idr_find(&query_idr, id) != query) {
  390. spin_unlock_irqrestore(&idr_lock, flags);
  391. return;
  392. }
  393. agent = query->port->agent;
  394. spin_unlock_irqrestore(&idr_lock, flags);
  395. ib_cancel_mad(agent, id);
  396. }
  397. EXPORT_SYMBOL(ib_sa_cancel_query);
  398. static void init_mad(struct ib_sa_mad *mad, struct ib_mad_agent *agent)
  399. {
  400. unsigned long flags;
  401. memset(mad, 0, sizeof *mad);
  402. mad->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
  403. mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  404. mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
  405. spin_lock_irqsave(&tid_lock, flags);
  406. mad->mad_hdr.tid =
  407. cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
  408. spin_unlock_irqrestore(&tid_lock, flags);
  409. }
  410. static int send_mad(struct ib_sa_query *query, int timeout_ms)
  411. {
  412. struct ib_sa_port *port = query->port;
  413. unsigned long flags;
  414. int ret;
  415. struct ib_sge gather_list;
  416. struct ib_send_wr *bad_wr, wr = {
  417. .opcode = IB_WR_SEND,
  418. .sg_list = &gather_list,
  419. .num_sge = 1,
  420. .send_flags = IB_SEND_SIGNALED,
  421. .wr = {
  422. .ud = {
  423. .mad_hdr = &query->mad->mad_hdr,
  424. .remote_qpn = 1,
  425. .remote_qkey = IB_QP1_QKEY,
  426. .timeout_ms = timeout_ms
  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->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].mr = NULL;
  696. sa_dev->port[i].sm_ah = NULL;
  697. sa_dev->port[i].port_num = i + s;
  698. spin_lock_init(&sa_dev->port[i].ah_lock);
  699. sa_dev->port[i].agent =
  700. ib_register_mad_agent(device, i + s, IB_QPT_GSI,
  701. NULL, 0, send_handler,
  702. recv_handler, sa_dev);
  703. if (IS_ERR(sa_dev->port[i].agent))
  704. goto err;
  705. sa_dev->port[i].mr = ib_get_dma_mr(sa_dev->port[i].agent->qp->pd,
  706. IB_ACCESS_LOCAL_WRITE);
  707. if (IS_ERR(sa_dev->port[i].mr)) {
  708. ib_unregister_mad_agent(sa_dev->port[i].agent);
  709. goto err;
  710. }
  711. INIT_WORK(&sa_dev->port[i].update_task,
  712. update_sm_ah, &sa_dev->port[i]);
  713. }
  714. ib_set_client_data(device, &sa_client, sa_dev);
  715. /*
  716. * We register our event handler after everything is set up,
  717. * and then update our cached info after the event handler is
  718. * registered to avoid any problems if a port changes state
  719. * during our initialization.
  720. */
  721. INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
  722. if (ib_register_event_handler(&sa_dev->event_handler))
  723. goto err;
  724. for (i = 0; i <= e - s; ++i)
  725. update_sm_ah(&sa_dev->port[i]);
  726. return;
  727. err:
  728. while (--i >= 0) {
  729. ib_dereg_mr(sa_dev->port[i].mr);
  730. ib_unregister_mad_agent(sa_dev->port[i].agent);
  731. }
  732. kfree(sa_dev);
  733. return;
  734. }
  735. static void ib_sa_remove_one(struct ib_device *device)
  736. {
  737. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  738. int i;
  739. if (!sa_dev)
  740. return;
  741. ib_unregister_event_handler(&sa_dev->event_handler);
  742. for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
  743. ib_unregister_mad_agent(sa_dev->port[i].agent);
  744. kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
  745. }
  746. kfree(sa_dev);
  747. }
  748. static int __init ib_sa_init(void)
  749. {
  750. int ret;
  751. spin_lock_init(&idr_lock);
  752. spin_lock_init(&tid_lock);
  753. get_random_bytes(&tid, sizeof tid);
  754. ret = ib_register_client(&sa_client);
  755. if (ret)
  756. printk(KERN_ERR "Couldn't register ib_sa client\n");
  757. return ret;
  758. }
  759. static void __exit ib_sa_cleanup(void)
  760. {
  761. ib_unregister_client(&sa_client);
  762. }
  763. module_init(ib_sa_init);
  764. module_exit(ib_sa_cleanup);