sa_query.c 26 KB

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