sa_query.c 28 KB

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