sa_query.c 28 KB

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