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(void *port_ptr)
  341. {
  342. struct ib_sa_port *port = port_ptr;
  343. struct ib_sa_sm_ah *new_ah, *old_ah;
  344. struct ib_port_attr port_attr;
  345. struct ib_ah_attr ah_attr;
  346. if (ib_query_port(port->agent->device, port->port_num, &port_attr)) {
  347. printk(KERN_WARNING "Couldn't query port\n");
  348. return;
  349. }
  350. new_ah = kmalloc(sizeof *new_ah, GFP_KERNEL);
  351. if (!new_ah) {
  352. printk(KERN_WARNING "Couldn't allocate new SM AH\n");
  353. return;
  354. }
  355. kref_init(&new_ah->ref);
  356. memset(&ah_attr, 0, sizeof ah_attr);
  357. ah_attr.dlid = port_attr.sm_lid;
  358. ah_attr.sl = port_attr.sm_sl;
  359. ah_attr.port_num = port->port_num;
  360. new_ah->ah = ib_create_ah(port->agent->qp->pd, &ah_attr);
  361. if (IS_ERR(new_ah->ah)) {
  362. printk(KERN_WARNING "Couldn't create new SM AH\n");
  363. kfree(new_ah);
  364. return;
  365. }
  366. spin_lock_irq(&port->ah_lock);
  367. old_ah = port->sm_ah;
  368. port->sm_ah = new_ah;
  369. spin_unlock_irq(&port->ah_lock);
  370. if (old_ah)
  371. kref_put(&old_ah->ref, free_sm_ah);
  372. }
  373. static void ib_sa_event(struct ib_event_handler *handler, struct ib_event *event)
  374. {
  375. if (event->event == IB_EVENT_PORT_ERR ||
  376. event->event == IB_EVENT_PORT_ACTIVE ||
  377. event->event == IB_EVENT_LID_CHANGE ||
  378. event->event == IB_EVENT_PKEY_CHANGE ||
  379. event->event == IB_EVENT_SM_CHANGE ||
  380. event->event == IB_EVENT_CLIENT_REREGISTER) {
  381. struct ib_sa_device *sa_dev;
  382. sa_dev = container_of(handler, typeof(*sa_dev), event_handler);
  383. schedule_work(&sa_dev->port[event->element.port_num -
  384. sa_dev->start_port].update_task);
  385. }
  386. }
  387. void ib_sa_register_client(struct ib_sa_client *client)
  388. {
  389. atomic_set(&client->users, 1);
  390. init_completion(&client->comp);
  391. }
  392. EXPORT_SYMBOL(ib_sa_register_client);
  393. static inline void ib_sa_client_get(struct ib_sa_client *client)
  394. {
  395. atomic_inc(&client->users);
  396. }
  397. static inline void ib_sa_client_put(struct ib_sa_client *client)
  398. {
  399. if (atomic_dec_and_test(&client->users))
  400. complete(&client->comp);
  401. }
  402. void ib_sa_unregister_client(struct ib_sa_client *client)
  403. {
  404. ib_sa_client_put(client);
  405. wait_for_completion(&client->comp);
  406. }
  407. EXPORT_SYMBOL(ib_sa_unregister_client);
  408. /**
  409. * ib_sa_cancel_query - try to cancel an SA query
  410. * @id:ID of query to cancel
  411. * @query:query pointer to cancel
  412. *
  413. * Try to cancel an SA query. If the id and query don't match up or
  414. * the query has already completed, nothing is done. Otherwise the
  415. * query is canceled and will complete with a status of -EINTR.
  416. */
  417. void ib_sa_cancel_query(int id, struct ib_sa_query *query)
  418. {
  419. unsigned long flags;
  420. struct ib_mad_agent *agent;
  421. struct ib_mad_send_buf *mad_buf;
  422. spin_lock_irqsave(&idr_lock, flags);
  423. if (idr_find(&query_idr, id) != query) {
  424. spin_unlock_irqrestore(&idr_lock, flags);
  425. return;
  426. }
  427. agent = query->port->agent;
  428. mad_buf = query->mad_buf;
  429. spin_unlock_irqrestore(&idr_lock, flags);
  430. ib_cancel_mad(agent, mad_buf);
  431. }
  432. EXPORT_SYMBOL(ib_sa_cancel_query);
  433. int ib_init_ah_from_path(struct ib_device *device, u8 port_num,
  434. struct ib_sa_path_rec *rec, struct ib_ah_attr *ah_attr)
  435. {
  436. int ret;
  437. u16 gid_index;
  438. memset(ah_attr, 0, sizeof *ah_attr);
  439. ah_attr->dlid = be16_to_cpu(rec->dlid);
  440. ah_attr->sl = rec->sl;
  441. ah_attr->src_path_bits = be16_to_cpu(rec->slid) & 0x7f;
  442. ah_attr->port_num = port_num;
  443. if (rec->hop_limit > 1) {
  444. ah_attr->ah_flags = IB_AH_GRH;
  445. ah_attr->grh.dgid = rec->dgid;
  446. ret = ib_find_cached_gid(device, &rec->sgid, &port_num,
  447. &gid_index);
  448. if (ret)
  449. return ret;
  450. ah_attr->grh.sgid_index = gid_index;
  451. ah_attr->grh.flow_label = be32_to_cpu(rec->flow_label);
  452. ah_attr->grh.hop_limit = rec->hop_limit;
  453. ah_attr->grh.traffic_class = rec->traffic_class;
  454. }
  455. return 0;
  456. }
  457. EXPORT_SYMBOL(ib_init_ah_from_path);
  458. static void init_mad(struct ib_sa_mad *mad, struct ib_mad_agent *agent)
  459. {
  460. unsigned long flags;
  461. memset(mad, 0, sizeof *mad);
  462. mad->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
  463. mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  464. mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
  465. spin_lock_irqsave(&tid_lock, flags);
  466. mad->mad_hdr.tid =
  467. cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
  468. spin_unlock_irqrestore(&tid_lock, flags);
  469. }
  470. static int send_mad(struct ib_sa_query *query, int timeout_ms, gfp_t gfp_mask)
  471. {
  472. unsigned long flags;
  473. int ret, id;
  474. retry:
  475. if (!idr_pre_get(&query_idr, gfp_mask))
  476. return -ENOMEM;
  477. spin_lock_irqsave(&idr_lock, flags);
  478. ret = idr_get_new(&query_idr, query, &id);
  479. spin_unlock_irqrestore(&idr_lock, flags);
  480. if (ret == -EAGAIN)
  481. goto retry;
  482. if (ret)
  483. return ret;
  484. query->mad_buf->timeout_ms = timeout_ms;
  485. query->mad_buf->context[0] = query;
  486. query->id = id;
  487. spin_lock_irqsave(&query->port->ah_lock, flags);
  488. kref_get(&query->port->sm_ah->ref);
  489. query->sm_ah = query->port->sm_ah;
  490. spin_unlock_irqrestore(&query->port->ah_lock, flags);
  491. query->mad_buf->ah = query->sm_ah->ah;
  492. ret = ib_post_send_mad(query->mad_buf, NULL);
  493. if (ret) {
  494. spin_lock_irqsave(&idr_lock, flags);
  495. idr_remove(&query_idr, id);
  496. spin_unlock_irqrestore(&idr_lock, flags);
  497. kref_put(&query->sm_ah->ref, free_sm_ah);
  498. }
  499. /*
  500. * It's not safe to dereference query any more, because the
  501. * send may already have completed and freed the query in
  502. * another context.
  503. */
  504. return ret ? ret : id;
  505. }
  506. static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
  507. int status,
  508. struct ib_sa_mad *mad)
  509. {
  510. struct ib_sa_path_query *query =
  511. container_of(sa_query, struct ib_sa_path_query, sa_query);
  512. if (mad) {
  513. struct ib_sa_path_rec rec;
  514. ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table),
  515. mad->data, &rec);
  516. query->callback(status, &rec, query->context);
  517. } else
  518. query->callback(status, NULL, query->context);
  519. }
  520. static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
  521. {
  522. kfree(container_of(sa_query, struct ib_sa_path_query, sa_query));
  523. }
  524. /**
  525. * ib_sa_path_rec_get - Start a Path get query
  526. * @client:SA client
  527. * @device:device to send query on
  528. * @port_num: port number to send query on
  529. * @rec:Path Record to send in query
  530. * @comp_mask:component mask to send in query
  531. * @timeout_ms:time to wait for response
  532. * @gfp_mask:GFP mask to use for internal allocations
  533. * @callback:function called when query completes, times out or is
  534. * canceled
  535. * @context:opaque user context passed to callback
  536. * @sa_query:query context, used to cancel query
  537. *
  538. * Send a Path Record Get query to the SA to look up a path. The
  539. * callback function will be called when the query completes (or
  540. * fails); status is 0 for a successful response, -EINTR if the query
  541. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  542. * occurred sending the query. The resp parameter of the callback is
  543. * only valid if status is 0.
  544. *
  545. * If the return value of ib_sa_path_rec_get() is negative, it is an
  546. * error code. Otherwise it is a query ID that can be used to cancel
  547. * the query.
  548. */
  549. int ib_sa_path_rec_get(struct ib_sa_client *client,
  550. struct ib_device *device, u8 port_num,
  551. struct ib_sa_path_rec *rec,
  552. ib_sa_comp_mask comp_mask,
  553. int timeout_ms, gfp_t gfp_mask,
  554. void (*callback)(int status,
  555. struct ib_sa_path_rec *resp,
  556. void *context),
  557. void *context,
  558. struct ib_sa_query **sa_query)
  559. {
  560. struct ib_sa_path_query *query;
  561. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  562. struct ib_sa_port *port;
  563. struct ib_mad_agent *agent;
  564. struct ib_sa_mad *mad;
  565. int ret;
  566. if (!sa_dev)
  567. return -ENODEV;
  568. port = &sa_dev->port[port_num - sa_dev->start_port];
  569. agent = port->agent;
  570. query = kmalloc(sizeof *query, gfp_mask);
  571. if (!query)
  572. return -ENOMEM;
  573. query->sa_query.mad_buf = ib_create_send_mad(agent, 1, 0,
  574. 0, IB_MGMT_SA_HDR,
  575. IB_MGMT_SA_DATA, gfp_mask);
  576. if (!query->sa_query.mad_buf) {
  577. ret = -ENOMEM;
  578. goto err1;
  579. }
  580. ib_sa_client_get(client);
  581. query->sa_query.client = client;
  582. query->callback = callback;
  583. query->context = context;
  584. mad = query->sa_query.mad_buf->mad;
  585. init_mad(mad, agent);
  586. query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL;
  587. query->sa_query.release = ib_sa_path_rec_release;
  588. query->sa_query.port = port;
  589. mad->mad_hdr.method = IB_MGMT_METHOD_GET;
  590. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_PATH_REC);
  591. mad->sa_hdr.comp_mask = comp_mask;
  592. ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, mad->data);
  593. *sa_query = &query->sa_query;
  594. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  595. if (ret < 0)
  596. goto err2;
  597. return ret;
  598. err2:
  599. *sa_query = NULL;
  600. ib_sa_client_put(query->sa_query.client);
  601. ib_free_send_mad(query->sa_query.mad_buf);
  602. err1:
  603. kfree(query);
  604. return ret;
  605. }
  606. EXPORT_SYMBOL(ib_sa_path_rec_get);
  607. static void ib_sa_service_rec_callback(struct ib_sa_query *sa_query,
  608. int status,
  609. struct ib_sa_mad *mad)
  610. {
  611. struct ib_sa_service_query *query =
  612. container_of(sa_query, struct ib_sa_service_query, sa_query);
  613. if (mad) {
  614. struct ib_sa_service_rec rec;
  615. ib_unpack(service_rec_table, ARRAY_SIZE(service_rec_table),
  616. mad->data, &rec);
  617. query->callback(status, &rec, query->context);
  618. } else
  619. query->callback(status, NULL, query->context);
  620. }
  621. static void ib_sa_service_rec_release(struct ib_sa_query *sa_query)
  622. {
  623. kfree(container_of(sa_query, struct ib_sa_service_query, sa_query));
  624. }
  625. /**
  626. * ib_sa_service_rec_query - Start Service Record operation
  627. * @client:SA client
  628. * @device:device to send request on
  629. * @port_num: port number to send request on
  630. * @method:SA method - should be get, set, or delete
  631. * @rec:Service Record to send in request
  632. * @comp_mask:component mask to send in request
  633. * @timeout_ms:time to wait for response
  634. * @gfp_mask:GFP mask to use for internal allocations
  635. * @callback:function called when request completes, times out or is
  636. * canceled
  637. * @context:opaque user context passed to callback
  638. * @sa_query:request context, used to cancel request
  639. *
  640. * Send a Service Record set/get/delete to the SA to register,
  641. * unregister or query a service record.
  642. * The callback function will be called when the request completes (or
  643. * fails); status is 0 for a successful response, -EINTR if the query
  644. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  645. * occurred sending the query. The resp parameter of the callback is
  646. * only valid if status is 0.
  647. *
  648. * If the return value of ib_sa_service_rec_query() is negative, it is an
  649. * error code. Otherwise it is a request ID that can be used to cancel
  650. * the query.
  651. */
  652. int ib_sa_service_rec_query(struct ib_sa_client *client,
  653. struct ib_device *device, u8 port_num, u8 method,
  654. struct ib_sa_service_rec *rec,
  655. ib_sa_comp_mask comp_mask,
  656. int timeout_ms, gfp_t gfp_mask,
  657. void (*callback)(int status,
  658. struct ib_sa_service_rec *resp,
  659. void *context),
  660. void *context,
  661. struct ib_sa_query **sa_query)
  662. {
  663. struct ib_sa_service_query *query;
  664. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  665. struct ib_sa_port *port;
  666. struct ib_mad_agent *agent;
  667. struct ib_sa_mad *mad;
  668. int ret;
  669. if (!sa_dev)
  670. return -ENODEV;
  671. port = &sa_dev->port[port_num - sa_dev->start_port];
  672. agent = port->agent;
  673. if (method != IB_MGMT_METHOD_GET &&
  674. method != IB_MGMT_METHOD_SET &&
  675. method != IB_SA_METHOD_DELETE)
  676. return -EINVAL;
  677. query = kmalloc(sizeof *query, gfp_mask);
  678. if (!query)
  679. return -ENOMEM;
  680. query->sa_query.mad_buf = ib_create_send_mad(agent, 1, 0,
  681. 0, IB_MGMT_SA_HDR,
  682. IB_MGMT_SA_DATA, gfp_mask);
  683. if (!query->sa_query.mad_buf) {
  684. ret = -ENOMEM;
  685. goto err1;
  686. }
  687. ib_sa_client_get(client);
  688. query->sa_query.client = client;
  689. query->callback = callback;
  690. query->context = context;
  691. mad = query->sa_query.mad_buf->mad;
  692. init_mad(mad, agent);
  693. query->sa_query.callback = callback ? ib_sa_service_rec_callback : NULL;
  694. query->sa_query.release = ib_sa_service_rec_release;
  695. query->sa_query.port = port;
  696. mad->mad_hdr.method = method;
  697. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_SERVICE_REC);
  698. mad->sa_hdr.comp_mask = comp_mask;
  699. ib_pack(service_rec_table, ARRAY_SIZE(service_rec_table),
  700. rec, mad->data);
  701. *sa_query = &query->sa_query;
  702. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  703. if (ret < 0)
  704. goto err2;
  705. return ret;
  706. err2:
  707. *sa_query = NULL;
  708. ib_sa_client_put(query->sa_query.client);
  709. ib_free_send_mad(query->sa_query.mad_buf);
  710. err1:
  711. kfree(query);
  712. return ret;
  713. }
  714. EXPORT_SYMBOL(ib_sa_service_rec_query);
  715. static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
  716. int status,
  717. struct ib_sa_mad *mad)
  718. {
  719. struct ib_sa_mcmember_query *query =
  720. container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
  721. if (mad) {
  722. struct ib_sa_mcmember_rec rec;
  723. ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  724. mad->data, &rec);
  725. query->callback(status, &rec, query->context);
  726. } else
  727. query->callback(status, NULL, query->context);
  728. }
  729. static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
  730. {
  731. kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
  732. }
  733. int ib_sa_mcmember_rec_query(struct ib_sa_client *client,
  734. struct ib_device *device, u8 port_num,
  735. u8 method,
  736. struct ib_sa_mcmember_rec *rec,
  737. ib_sa_comp_mask comp_mask,
  738. int timeout_ms, gfp_t gfp_mask,
  739. void (*callback)(int status,
  740. struct ib_sa_mcmember_rec *resp,
  741. void *context),
  742. void *context,
  743. struct ib_sa_query **sa_query)
  744. {
  745. struct ib_sa_mcmember_query *query;
  746. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  747. struct ib_sa_port *port;
  748. struct ib_mad_agent *agent;
  749. struct ib_sa_mad *mad;
  750. int ret;
  751. if (!sa_dev)
  752. return -ENODEV;
  753. port = &sa_dev->port[port_num - sa_dev->start_port];
  754. agent = port->agent;
  755. query = kmalloc(sizeof *query, gfp_mask);
  756. if (!query)
  757. return -ENOMEM;
  758. query->sa_query.mad_buf = ib_create_send_mad(agent, 1, 0,
  759. 0, IB_MGMT_SA_HDR,
  760. IB_MGMT_SA_DATA, gfp_mask);
  761. if (!query->sa_query.mad_buf) {
  762. ret = -ENOMEM;
  763. goto err1;
  764. }
  765. ib_sa_client_get(client);
  766. query->sa_query.client = client;
  767. query->callback = callback;
  768. query->context = context;
  769. mad = query->sa_query.mad_buf->mad;
  770. init_mad(mad, agent);
  771. query->sa_query.callback = callback ? ib_sa_mcmember_rec_callback : NULL;
  772. query->sa_query.release = ib_sa_mcmember_rec_release;
  773. query->sa_query.port = port;
  774. mad->mad_hdr.method = method;
  775. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
  776. mad->sa_hdr.comp_mask = comp_mask;
  777. ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  778. rec, mad->data);
  779. *sa_query = &query->sa_query;
  780. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  781. if (ret < 0)
  782. goto err2;
  783. return ret;
  784. err2:
  785. *sa_query = NULL;
  786. ib_sa_client_put(query->sa_query.client);
  787. ib_free_send_mad(query->sa_query.mad_buf);
  788. err1:
  789. kfree(query);
  790. return ret;
  791. }
  792. EXPORT_SYMBOL(ib_sa_mcmember_rec_query);
  793. static void send_handler(struct ib_mad_agent *agent,
  794. struct ib_mad_send_wc *mad_send_wc)
  795. {
  796. struct ib_sa_query *query = mad_send_wc->send_buf->context[0];
  797. unsigned long flags;
  798. if (query->callback)
  799. switch (mad_send_wc->status) {
  800. case IB_WC_SUCCESS:
  801. /* No callback -- already got recv */
  802. break;
  803. case IB_WC_RESP_TIMEOUT_ERR:
  804. query->callback(query, -ETIMEDOUT, NULL);
  805. break;
  806. case IB_WC_WR_FLUSH_ERR:
  807. query->callback(query, -EINTR, NULL);
  808. break;
  809. default:
  810. query->callback(query, -EIO, NULL);
  811. break;
  812. }
  813. spin_lock_irqsave(&idr_lock, flags);
  814. idr_remove(&query_idr, query->id);
  815. spin_unlock_irqrestore(&idr_lock, flags);
  816. ib_free_send_mad(mad_send_wc->send_buf);
  817. kref_put(&query->sm_ah->ref, free_sm_ah);
  818. ib_sa_client_put(query->client);
  819. query->release(query);
  820. }
  821. static void recv_handler(struct ib_mad_agent *mad_agent,
  822. struct ib_mad_recv_wc *mad_recv_wc)
  823. {
  824. struct ib_sa_query *query;
  825. struct ib_mad_send_buf *mad_buf;
  826. mad_buf = (void *) (unsigned long) mad_recv_wc->wc->wr_id;
  827. query = mad_buf->context[0];
  828. if (query->callback) {
  829. if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
  830. query->callback(query,
  831. mad_recv_wc->recv_buf.mad->mad_hdr.status ?
  832. -EINVAL : 0,
  833. (struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
  834. else
  835. query->callback(query, -EIO, NULL);
  836. }
  837. ib_free_recv_mad(mad_recv_wc);
  838. }
  839. static void ib_sa_add_one(struct ib_device *device)
  840. {
  841. struct ib_sa_device *sa_dev;
  842. int s, e, i;
  843. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  844. return;
  845. if (device->node_type == RDMA_NODE_IB_SWITCH)
  846. s = e = 0;
  847. else {
  848. s = 1;
  849. e = device->phys_port_cnt;
  850. }
  851. sa_dev = kmalloc(sizeof *sa_dev +
  852. (e - s + 1) * sizeof (struct ib_sa_port),
  853. GFP_KERNEL);
  854. if (!sa_dev)
  855. return;
  856. sa_dev->start_port = s;
  857. sa_dev->end_port = e;
  858. for (i = 0; i <= e - s; ++i) {
  859. sa_dev->port[i].sm_ah = NULL;
  860. sa_dev->port[i].port_num = i + s;
  861. spin_lock_init(&sa_dev->port[i].ah_lock);
  862. sa_dev->port[i].agent =
  863. ib_register_mad_agent(device, i + s, IB_QPT_GSI,
  864. NULL, 0, send_handler,
  865. recv_handler, sa_dev);
  866. if (IS_ERR(sa_dev->port[i].agent))
  867. goto err;
  868. INIT_WORK(&sa_dev->port[i].update_task,
  869. update_sm_ah, &sa_dev->port[i]);
  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]);
  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);