sa_query.c 29 KB

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