sa_query.c 28 KB

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