sa_query.c 26 KB

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