sa_query.c 28 KB

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