sa_query.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Voltaire, Inc.  All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. *
  33. * $Id: sa_query.c 2811 2005-07-06 18:11:43Z halr $
  34. */
  35. #include <linux/module.h>
  36. #include <linux/init.h>
  37. #include <linux/err.h>
  38. #include <linux/random.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/slab.h>
  41. #include <linux/pci.h>
  42. #include <linux/dma-mapping.h>
  43. #include <linux/kref.h>
  44. #include <linux/idr.h>
  45. #include <rdma/ib_pack.h>
  46. #include <rdma/ib_sa.h>
  47. MODULE_AUTHOR("Roland Dreier");
  48. MODULE_DESCRIPTION("InfiniBand subnet administration query support");
  49. MODULE_LICENSE("Dual BSD/GPL");
  50. struct ib_sa_sm_ah {
  51. struct ib_ah *ah;
  52. struct kref ref;
  53. };
  54. struct ib_sa_port {
  55. struct ib_mad_agent *agent;
  56. struct ib_sa_sm_ah *sm_ah;
  57. struct work_struct update_task;
  58. spinlock_t ah_lock;
  59. u8 port_num;
  60. };
  61. struct ib_sa_device {
  62. int start_port, end_port;
  63. struct ib_event_handler event_handler;
  64. struct ib_sa_port port[0];
  65. };
  66. struct ib_sa_query {
  67. void (*callback)(struct ib_sa_query *, int, struct ib_sa_mad *);
  68. void (*release)(struct ib_sa_query *);
  69. struct ib_sa_port *port;
  70. struct ib_mad_send_buf *mad_buf;
  71. struct ib_sa_sm_ah *sm_ah;
  72. int id;
  73. };
  74. struct ib_sa_service_query {
  75. void (*callback)(int, struct ib_sa_service_rec *, void *);
  76. void *context;
  77. struct ib_sa_query sa_query;
  78. };
  79. struct ib_sa_path_query {
  80. void (*callback)(int, struct ib_sa_path_rec *, void *);
  81. void *context;
  82. struct ib_sa_query sa_query;
  83. };
  84. struct ib_sa_mcmember_query {
  85. void (*callback)(int, struct ib_sa_mcmember_rec *, void *);
  86. void *context;
  87. struct ib_sa_query sa_query;
  88. };
  89. static void ib_sa_add_one(struct ib_device *device);
  90. static void ib_sa_remove_one(struct ib_device *device);
  91. static struct ib_client sa_client = {
  92. .name = "sa",
  93. .add = ib_sa_add_one,
  94. .remove = ib_sa_remove_one
  95. };
  96. static spinlock_t idr_lock;
  97. static DEFINE_IDR(query_idr);
  98. static spinlock_t tid_lock;
  99. static u32 tid;
  100. #define PATH_REC_FIELD(field) \
  101. .struct_offset_bytes = offsetof(struct ib_sa_path_rec, field), \
  102. .struct_size_bytes = sizeof ((struct ib_sa_path_rec *) 0)->field, \
  103. .field_name = "sa_path_rec:" #field
  104. static const struct ib_field path_rec_table[] = {
  105. { RESERVED,
  106. .offset_words = 0,
  107. .offset_bits = 0,
  108. .size_bits = 32 },
  109. { RESERVED,
  110. .offset_words = 1,
  111. .offset_bits = 0,
  112. .size_bits = 32 },
  113. { PATH_REC_FIELD(dgid),
  114. .offset_words = 2,
  115. .offset_bits = 0,
  116. .size_bits = 128 },
  117. { PATH_REC_FIELD(sgid),
  118. .offset_words = 6,
  119. .offset_bits = 0,
  120. .size_bits = 128 },
  121. { PATH_REC_FIELD(dlid),
  122. .offset_words = 10,
  123. .offset_bits = 0,
  124. .size_bits = 16 },
  125. { PATH_REC_FIELD(slid),
  126. .offset_words = 10,
  127. .offset_bits = 16,
  128. .size_bits = 16 },
  129. { PATH_REC_FIELD(raw_traffic),
  130. .offset_words = 11,
  131. .offset_bits = 0,
  132. .size_bits = 1 },
  133. { RESERVED,
  134. .offset_words = 11,
  135. .offset_bits = 1,
  136. .size_bits = 3 },
  137. { PATH_REC_FIELD(flow_label),
  138. .offset_words = 11,
  139. .offset_bits = 4,
  140. .size_bits = 20 },
  141. { PATH_REC_FIELD(hop_limit),
  142. .offset_words = 11,
  143. .offset_bits = 24,
  144. .size_bits = 8 },
  145. { PATH_REC_FIELD(traffic_class),
  146. .offset_words = 12,
  147. .offset_bits = 0,
  148. .size_bits = 8 },
  149. { PATH_REC_FIELD(reversible),
  150. .offset_words = 12,
  151. .offset_bits = 8,
  152. .size_bits = 1 },
  153. { PATH_REC_FIELD(numb_path),
  154. .offset_words = 12,
  155. .offset_bits = 9,
  156. .size_bits = 7 },
  157. { PATH_REC_FIELD(pkey),
  158. .offset_words = 12,
  159. .offset_bits = 16,
  160. .size_bits = 16 },
  161. { RESERVED,
  162. .offset_words = 13,
  163. .offset_bits = 0,
  164. .size_bits = 12 },
  165. { PATH_REC_FIELD(sl),
  166. .offset_words = 13,
  167. .offset_bits = 12,
  168. .size_bits = 4 },
  169. { PATH_REC_FIELD(mtu_selector),
  170. .offset_words = 13,
  171. .offset_bits = 16,
  172. .size_bits = 2 },
  173. { PATH_REC_FIELD(mtu),
  174. .offset_words = 13,
  175. .offset_bits = 18,
  176. .size_bits = 6 },
  177. { PATH_REC_FIELD(rate_selector),
  178. .offset_words = 13,
  179. .offset_bits = 24,
  180. .size_bits = 2 },
  181. { PATH_REC_FIELD(rate),
  182. .offset_words = 13,
  183. .offset_bits = 26,
  184. .size_bits = 6 },
  185. { PATH_REC_FIELD(packet_life_time_selector),
  186. .offset_words = 14,
  187. .offset_bits = 0,
  188. .size_bits = 2 },
  189. { PATH_REC_FIELD(packet_life_time),
  190. .offset_words = 14,
  191. .offset_bits = 2,
  192. .size_bits = 6 },
  193. { PATH_REC_FIELD(preference),
  194. .offset_words = 14,
  195. .offset_bits = 8,
  196. .size_bits = 8 },
  197. { RESERVED,
  198. .offset_words = 14,
  199. .offset_bits = 16,
  200. .size_bits = 48 },
  201. };
  202. #define MCMEMBER_REC_FIELD(field) \
  203. .struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field), \
  204. .struct_size_bytes = sizeof ((struct ib_sa_mcmember_rec *) 0)->field, \
  205. .field_name = "sa_mcmember_rec:" #field
  206. static const struct ib_field mcmember_rec_table[] = {
  207. { MCMEMBER_REC_FIELD(mgid),
  208. .offset_words = 0,
  209. .offset_bits = 0,
  210. .size_bits = 128 },
  211. { MCMEMBER_REC_FIELD(port_gid),
  212. .offset_words = 4,
  213. .offset_bits = 0,
  214. .size_bits = 128 },
  215. { MCMEMBER_REC_FIELD(qkey),
  216. .offset_words = 8,
  217. .offset_bits = 0,
  218. .size_bits = 32 },
  219. { MCMEMBER_REC_FIELD(mlid),
  220. .offset_words = 9,
  221. .offset_bits = 0,
  222. .size_bits = 16 },
  223. { MCMEMBER_REC_FIELD(mtu_selector),
  224. .offset_words = 9,
  225. .offset_bits = 16,
  226. .size_bits = 2 },
  227. { MCMEMBER_REC_FIELD(mtu),
  228. .offset_words = 9,
  229. .offset_bits = 18,
  230. .size_bits = 6 },
  231. { MCMEMBER_REC_FIELD(traffic_class),
  232. .offset_words = 9,
  233. .offset_bits = 24,
  234. .size_bits = 8 },
  235. { MCMEMBER_REC_FIELD(pkey),
  236. .offset_words = 10,
  237. .offset_bits = 0,
  238. .size_bits = 16 },
  239. { MCMEMBER_REC_FIELD(rate_selector),
  240. .offset_words = 10,
  241. .offset_bits = 16,
  242. .size_bits = 2 },
  243. { MCMEMBER_REC_FIELD(rate),
  244. .offset_words = 10,
  245. .offset_bits = 18,
  246. .size_bits = 6 },
  247. { MCMEMBER_REC_FIELD(packet_life_time_selector),
  248. .offset_words = 10,
  249. .offset_bits = 24,
  250. .size_bits = 2 },
  251. { MCMEMBER_REC_FIELD(packet_life_time),
  252. .offset_words = 10,
  253. .offset_bits = 26,
  254. .size_bits = 6 },
  255. { MCMEMBER_REC_FIELD(sl),
  256. .offset_words = 11,
  257. .offset_bits = 0,
  258. .size_bits = 4 },
  259. { MCMEMBER_REC_FIELD(flow_label),
  260. .offset_words = 11,
  261. .offset_bits = 4,
  262. .size_bits = 20 },
  263. { MCMEMBER_REC_FIELD(hop_limit),
  264. .offset_words = 11,
  265. .offset_bits = 24,
  266. .size_bits = 8 },
  267. { MCMEMBER_REC_FIELD(scope),
  268. .offset_words = 12,
  269. .offset_bits = 0,
  270. .size_bits = 4 },
  271. { MCMEMBER_REC_FIELD(join_state),
  272. .offset_words = 12,
  273. .offset_bits = 4,
  274. .size_bits = 4 },
  275. { MCMEMBER_REC_FIELD(proxy_join),
  276. .offset_words = 12,
  277. .offset_bits = 8,
  278. .size_bits = 1 },
  279. { RESERVED,
  280. .offset_words = 12,
  281. .offset_bits = 9,
  282. .size_bits = 23 },
  283. };
  284. #define SERVICE_REC_FIELD(field) \
  285. .struct_offset_bytes = offsetof(struct ib_sa_service_rec, field), \
  286. .struct_size_bytes = sizeof ((struct ib_sa_service_rec *) 0)->field, \
  287. .field_name = "sa_service_rec:" #field
  288. static const struct ib_field service_rec_table[] = {
  289. { SERVICE_REC_FIELD(id),
  290. .offset_words = 0,
  291. .offset_bits = 0,
  292. .size_bits = 64 },
  293. { SERVICE_REC_FIELD(gid),
  294. .offset_words = 2,
  295. .offset_bits = 0,
  296. .size_bits = 128 },
  297. { SERVICE_REC_FIELD(pkey),
  298. .offset_words = 6,
  299. .offset_bits = 0,
  300. .size_bits = 16 },
  301. { SERVICE_REC_FIELD(lease),
  302. .offset_words = 7,
  303. .offset_bits = 0,
  304. .size_bits = 32 },
  305. { SERVICE_REC_FIELD(key),
  306. .offset_words = 8,
  307. .offset_bits = 0,
  308. .size_bits = 128 },
  309. { SERVICE_REC_FIELD(name),
  310. .offset_words = 12,
  311. .offset_bits = 0,
  312. .size_bits = 64*8 },
  313. { SERVICE_REC_FIELD(data8),
  314. .offset_words = 28,
  315. .offset_bits = 0,
  316. .size_bits = 16*8 },
  317. { SERVICE_REC_FIELD(data16),
  318. .offset_words = 32,
  319. .offset_bits = 0,
  320. .size_bits = 8*16 },
  321. { SERVICE_REC_FIELD(data32),
  322. .offset_words = 36,
  323. .offset_bits = 0,
  324. .size_bits = 4*32 },
  325. { SERVICE_REC_FIELD(data64),
  326. .offset_words = 40,
  327. .offset_bits = 0,
  328. .size_bits = 2*64 },
  329. };
  330. static void free_sm_ah(struct kref *kref)
  331. {
  332. struct ib_sa_sm_ah *sm_ah = container_of(kref, struct ib_sa_sm_ah, ref);
  333. ib_destroy_ah(sm_ah->ah);
  334. kfree(sm_ah);
  335. }
  336. static void update_sm_ah(void *port_ptr)
  337. {
  338. struct ib_sa_port *port = port_ptr;
  339. struct ib_sa_sm_ah *new_ah, *old_ah;
  340. struct ib_port_attr port_attr;
  341. struct ib_ah_attr ah_attr;
  342. if (ib_query_port(port->agent->device, port->port_num, &port_attr)) {
  343. printk(KERN_WARNING "Couldn't query port\n");
  344. return;
  345. }
  346. new_ah = kmalloc(sizeof *new_ah, GFP_KERNEL);
  347. if (!new_ah) {
  348. printk(KERN_WARNING "Couldn't allocate new SM AH\n");
  349. return;
  350. }
  351. kref_init(&new_ah->ref);
  352. memset(&ah_attr, 0, sizeof ah_attr);
  353. ah_attr.dlid = port_attr.sm_lid;
  354. ah_attr.sl = port_attr.sm_sl;
  355. ah_attr.port_num = port->port_num;
  356. new_ah->ah = ib_create_ah(port->agent->qp->pd, &ah_attr);
  357. if (IS_ERR(new_ah->ah)) {
  358. printk(KERN_WARNING "Couldn't create new SM AH\n");
  359. kfree(new_ah);
  360. return;
  361. }
  362. spin_lock_irq(&port->ah_lock);
  363. old_ah = port->sm_ah;
  364. port->sm_ah = new_ah;
  365. spin_unlock_irq(&port->ah_lock);
  366. if (old_ah)
  367. kref_put(&old_ah->ref, free_sm_ah);
  368. }
  369. static void ib_sa_event(struct ib_event_handler *handler, struct ib_event *event)
  370. {
  371. if (event->event == IB_EVENT_PORT_ERR ||
  372. event->event == IB_EVENT_PORT_ACTIVE ||
  373. event->event == IB_EVENT_LID_CHANGE ||
  374. event->event == IB_EVENT_PKEY_CHANGE ||
  375. event->event == IB_EVENT_SM_CHANGE) {
  376. struct ib_sa_device *sa_dev;
  377. sa_dev = container_of(handler, typeof(*sa_dev), event_handler);
  378. schedule_work(&sa_dev->port[event->element.port_num -
  379. sa_dev->start_port].update_task);
  380. }
  381. }
  382. /**
  383. * ib_sa_cancel_query - try to cancel an SA query
  384. * @id:ID of query to cancel
  385. * @query:query pointer to cancel
  386. *
  387. * Try to cancel an SA query. If the id and query don't match up or
  388. * the query has already completed, nothing is done. Otherwise the
  389. * query is canceled and will complete with a status of -EINTR.
  390. */
  391. void ib_sa_cancel_query(int id, struct ib_sa_query *query)
  392. {
  393. unsigned long flags;
  394. struct ib_mad_agent *agent;
  395. struct ib_mad_send_buf *mad_buf;
  396. spin_lock_irqsave(&idr_lock, flags);
  397. if (idr_find(&query_idr, id) != query) {
  398. spin_unlock_irqrestore(&idr_lock, flags);
  399. return;
  400. }
  401. agent = query->port->agent;
  402. mad_buf = query->mad_buf;
  403. spin_unlock_irqrestore(&idr_lock, flags);
  404. ib_cancel_mad(agent, mad_buf);
  405. }
  406. EXPORT_SYMBOL(ib_sa_cancel_query);
  407. static void init_mad(struct ib_sa_mad *mad, struct ib_mad_agent *agent)
  408. {
  409. unsigned long flags;
  410. memset(mad, 0, sizeof *mad);
  411. mad->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
  412. mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  413. mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
  414. spin_lock_irqsave(&tid_lock, flags);
  415. mad->mad_hdr.tid =
  416. cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
  417. spin_unlock_irqrestore(&tid_lock, flags);
  418. }
  419. static int send_mad(struct ib_sa_query *query, int timeout_ms)
  420. {
  421. unsigned long flags;
  422. int ret, id;
  423. retry:
  424. if (!idr_pre_get(&query_idr, GFP_ATOMIC))
  425. return -ENOMEM;
  426. spin_lock_irqsave(&idr_lock, flags);
  427. ret = idr_get_new(&query_idr, query, &id);
  428. spin_unlock_irqrestore(&idr_lock, flags);
  429. if (ret == -EAGAIN)
  430. goto retry;
  431. if (ret)
  432. return ret;
  433. query->mad_buf->timeout_ms = timeout_ms;
  434. query->mad_buf->context[0] = query;
  435. query->id = id;
  436. spin_lock_irqsave(&query->port->ah_lock, flags);
  437. kref_get(&query->port->sm_ah->ref);
  438. query->sm_ah = query->port->sm_ah;
  439. spin_unlock_irqrestore(&query->port->ah_lock, flags);
  440. query->mad_buf->ah = query->sm_ah->ah;
  441. ret = ib_post_send_mad(query->mad_buf, NULL);
  442. if (ret) {
  443. spin_lock_irqsave(&idr_lock, flags);
  444. idr_remove(&query_idr, id);
  445. spin_unlock_irqrestore(&idr_lock, flags);
  446. kref_put(&query->sm_ah->ref, free_sm_ah);
  447. }
  448. /*
  449. * It's not safe to dereference query any more, because the
  450. * send may already have completed and freed the query in
  451. * another context.
  452. */
  453. return ret ? ret : id;
  454. }
  455. static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
  456. int status,
  457. struct ib_sa_mad *mad)
  458. {
  459. struct ib_sa_path_query *query =
  460. container_of(sa_query, struct ib_sa_path_query, sa_query);
  461. if (mad) {
  462. struct ib_sa_path_rec rec;
  463. ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table),
  464. mad->data, &rec);
  465. query->callback(status, &rec, query->context);
  466. } else
  467. query->callback(status, NULL, query->context);
  468. }
  469. static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
  470. {
  471. kfree(container_of(sa_query, struct ib_sa_path_query, sa_query));
  472. }
  473. /**
  474. * ib_sa_path_rec_get - Start a Path get query
  475. * @device:device to send query on
  476. * @port_num: port number to send query on
  477. * @rec:Path Record to send in query
  478. * @comp_mask:component mask to send in query
  479. * @timeout_ms:time to wait for response
  480. * @gfp_mask:GFP mask to use for internal allocations
  481. * @callback:function called when query completes, times out or is
  482. * canceled
  483. * @context:opaque user context passed to callback
  484. * @sa_query:query context, used to cancel query
  485. *
  486. * Send a Path Record Get query to the SA to look up a path. The
  487. * callback function will be called when the query completes (or
  488. * fails); status is 0 for a successful response, -EINTR if the query
  489. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  490. * occurred sending the query. The resp parameter of the callback is
  491. * only valid if status is 0.
  492. *
  493. * If the return value of ib_sa_path_rec_get() is negative, it is an
  494. * error code. Otherwise it is a query ID that can be used to cancel
  495. * the query.
  496. */
  497. int ib_sa_path_rec_get(struct ib_device *device, u8 port_num,
  498. struct ib_sa_path_rec *rec,
  499. ib_sa_comp_mask comp_mask,
  500. int timeout_ms, gfp_t gfp_mask,
  501. void (*callback)(int status,
  502. struct ib_sa_path_rec *resp,
  503. void *context),
  504. void *context,
  505. struct ib_sa_query **sa_query)
  506. {
  507. struct ib_sa_path_query *query;
  508. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  509. struct ib_sa_port *port;
  510. struct ib_mad_agent *agent;
  511. struct ib_sa_mad *mad;
  512. int ret;
  513. if (!sa_dev)
  514. return -ENODEV;
  515. port = &sa_dev->port[port_num - sa_dev->start_port];
  516. agent = port->agent;
  517. query = kmalloc(sizeof *query, gfp_mask);
  518. if (!query)
  519. return -ENOMEM;
  520. query->sa_query.mad_buf = ib_create_send_mad(agent, 1, 0,
  521. 0, IB_MGMT_SA_HDR,
  522. IB_MGMT_SA_DATA, gfp_mask);
  523. if (!query->sa_query.mad_buf) {
  524. ret = -ENOMEM;
  525. goto err1;
  526. }
  527. query->callback = callback;
  528. query->context = context;
  529. mad = query->sa_query.mad_buf->mad;
  530. init_mad(mad, agent);
  531. query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL;
  532. query->sa_query.release = ib_sa_path_rec_release;
  533. query->sa_query.port = port;
  534. mad->mad_hdr.method = IB_MGMT_METHOD_GET;
  535. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_PATH_REC);
  536. mad->sa_hdr.comp_mask = comp_mask;
  537. ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, mad->data);
  538. *sa_query = &query->sa_query;
  539. ret = send_mad(&query->sa_query, timeout_ms);
  540. if (ret < 0)
  541. goto err2;
  542. return ret;
  543. err2:
  544. *sa_query = NULL;
  545. ib_free_send_mad(query->sa_query.mad_buf);
  546. err1:
  547. kfree(query);
  548. return ret;
  549. }
  550. EXPORT_SYMBOL(ib_sa_path_rec_get);
  551. static void ib_sa_service_rec_callback(struct ib_sa_query *sa_query,
  552. int status,
  553. struct ib_sa_mad *mad)
  554. {
  555. struct ib_sa_service_query *query =
  556. container_of(sa_query, struct ib_sa_service_query, sa_query);
  557. if (mad) {
  558. struct ib_sa_service_rec rec;
  559. ib_unpack(service_rec_table, ARRAY_SIZE(service_rec_table),
  560. mad->data, &rec);
  561. query->callback(status, &rec, query->context);
  562. } else
  563. query->callback(status, NULL, query->context);
  564. }
  565. static void ib_sa_service_rec_release(struct ib_sa_query *sa_query)
  566. {
  567. kfree(container_of(sa_query, struct ib_sa_service_query, sa_query));
  568. }
  569. /**
  570. * ib_sa_service_rec_query - Start Service Record operation
  571. * @device:device to send request on
  572. * @port_num: port number to send request on
  573. * @method:SA method - should be get, set, or delete
  574. * @rec:Service Record to send in request
  575. * @comp_mask:component mask to send in request
  576. * @timeout_ms:time to wait for response
  577. * @gfp_mask:GFP mask to use for internal allocations
  578. * @callback:function called when request completes, times out or is
  579. * canceled
  580. * @context:opaque user context passed to callback
  581. * @sa_query:request context, used to cancel request
  582. *
  583. * Send a Service Record set/get/delete to the SA to register,
  584. * unregister or query a service record.
  585. * The callback function will be called when the request completes (or
  586. * fails); status is 0 for a successful response, -EINTR if the query
  587. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  588. * occurred sending the query. The resp parameter of the callback is
  589. * only valid if status is 0.
  590. *
  591. * If the return value of ib_sa_service_rec_query() is negative, it is an
  592. * error code. Otherwise it is a request ID that can be used to cancel
  593. * the query.
  594. */
  595. int ib_sa_service_rec_query(struct ib_device *device, u8 port_num, u8 method,
  596. struct ib_sa_service_rec *rec,
  597. ib_sa_comp_mask comp_mask,
  598. int timeout_ms, gfp_t gfp_mask,
  599. void (*callback)(int status,
  600. struct ib_sa_service_rec *resp,
  601. void *context),
  602. void *context,
  603. struct ib_sa_query **sa_query)
  604. {
  605. struct ib_sa_service_query *query;
  606. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  607. struct ib_sa_port *port;
  608. struct ib_mad_agent *agent;
  609. struct ib_sa_mad *mad;
  610. int ret;
  611. if (!sa_dev)
  612. return -ENODEV;
  613. port = &sa_dev->port[port_num - sa_dev->start_port];
  614. agent = port->agent;
  615. if (method != IB_MGMT_METHOD_GET &&
  616. method != IB_MGMT_METHOD_SET &&
  617. method != IB_SA_METHOD_DELETE)
  618. return -EINVAL;
  619. query = kmalloc(sizeof *query, gfp_mask);
  620. if (!query)
  621. return -ENOMEM;
  622. query->sa_query.mad_buf = ib_create_send_mad(agent, 1, 0,
  623. 0, IB_MGMT_SA_HDR,
  624. IB_MGMT_SA_DATA, gfp_mask);
  625. if (!query->sa_query.mad_buf) {
  626. ret = -ENOMEM;
  627. goto err1;
  628. }
  629. query->callback = callback;
  630. query->context = context;
  631. mad = query->sa_query.mad_buf->mad;
  632. init_mad(mad, agent);
  633. query->sa_query.callback = callback ? ib_sa_service_rec_callback : NULL;
  634. query->sa_query.release = ib_sa_service_rec_release;
  635. query->sa_query.port = port;
  636. mad->mad_hdr.method = method;
  637. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_SERVICE_REC);
  638. mad->sa_hdr.comp_mask = comp_mask;
  639. ib_pack(service_rec_table, ARRAY_SIZE(service_rec_table),
  640. rec, mad->data);
  641. *sa_query = &query->sa_query;
  642. ret = send_mad(&query->sa_query, timeout_ms);
  643. if (ret < 0)
  644. goto err2;
  645. return ret;
  646. err2:
  647. *sa_query = NULL;
  648. ib_free_send_mad(query->sa_query.mad_buf);
  649. err1:
  650. kfree(query);
  651. return ret;
  652. }
  653. EXPORT_SYMBOL(ib_sa_service_rec_query);
  654. static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
  655. int status,
  656. struct ib_sa_mad *mad)
  657. {
  658. struct ib_sa_mcmember_query *query =
  659. container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
  660. if (mad) {
  661. struct ib_sa_mcmember_rec rec;
  662. ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  663. mad->data, &rec);
  664. query->callback(status, &rec, query->context);
  665. } else
  666. query->callback(status, NULL, query->context);
  667. }
  668. static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
  669. {
  670. kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
  671. }
  672. int ib_sa_mcmember_rec_query(struct ib_device *device, u8 port_num,
  673. u8 method,
  674. struct ib_sa_mcmember_rec *rec,
  675. ib_sa_comp_mask comp_mask,
  676. int timeout_ms, gfp_t gfp_mask,
  677. void (*callback)(int status,
  678. struct ib_sa_mcmember_rec *resp,
  679. void *context),
  680. void *context,
  681. struct ib_sa_query **sa_query)
  682. {
  683. struct ib_sa_mcmember_query *query;
  684. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  685. struct ib_sa_port *port;
  686. struct ib_mad_agent *agent;
  687. struct ib_sa_mad *mad;
  688. int ret;
  689. if (!sa_dev)
  690. return -ENODEV;
  691. port = &sa_dev->port[port_num - sa_dev->start_port];
  692. agent = port->agent;
  693. query = kmalloc(sizeof *query, gfp_mask);
  694. if (!query)
  695. return -ENOMEM;
  696. query->sa_query.mad_buf = ib_create_send_mad(agent, 1, 0,
  697. 0, IB_MGMT_SA_HDR,
  698. IB_MGMT_SA_DATA, gfp_mask);
  699. if (!query->sa_query.mad_buf) {
  700. ret = -ENOMEM;
  701. goto err1;
  702. }
  703. query->callback = callback;
  704. query->context = context;
  705. mad = query->sa_query.mad_buf->mad;
  706. init_mad(mad, agent);
  707. query->sa_query.callback = callback ? ib_sa_mcmember_rec_callback : NULL;
  708. query->sa_query.release = ib_sa_mcmember_rec_release;
  709. query->sa_query.port = port;
  710. mad->mad_hdr.method = method;
  711. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
  712. mad->sa_hdr.comp_mask = comp_mask;
  713. ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  714. rec, mad->data);
  715. *sa_query = &query->sa_query;
  716. ret = send_mad(&query->sa_query, timeout_ms);
  717. if (ret < 0)
  718. goto err2;
  719. return ret;
  720. err2:
  721. *sa_query = NULL;
  722. ib_free_send_mad(query->sa_query.mad_buf);
  723. err1:
  724. kfree(query);
  725. return ret;
  726. }
  727. EXPORT_SYMBOL(ib_sa_mcmember_rec_query);
  728. static void send_handler(struct ib_mad_agent *agent,
  729. struct ib_mad_send_wc *mad_send_wc)
  730. {
  731. struct ib_sa_query *query = mad_send_wc->send_buf->context[0];
  732. unsigned long flags;
  733. if (query->callback)
  734. switch (mad_send_wc->status) {
  735. case IB_WC_SUCCESS:
  736. /* No callback -- already got recv */
  737. break;
  738. case IB_WC_RESP_TIMEOUT_ERR:
  739. query->callback(query, -ETIMEDOUT, NULL);
  740. break;
  741. case IB_WC_WR_FLUSH_ERR:
  742. query->callback(query, -EINTR, NULL);
  743. break;
  744. default:
  745. query->callback(query, -EIO, NULL);
  746. break;
  747. }
  748. spin_lock_irqsave(&idr_lock, flags);
  749. idr_remove(&query_idr, query->id);
  750. spin_unlock_irqrestore(&idr_lock, flags);
  751. ib_free_send_mad(mad_send_wc->send_buf);
  752. kref_put(&query->sm_ah->ref, free_sm_ah);
  753. query->release(query);
  754. }
  755. static void recv_handler(struct ib_mad_agent *mad_agent,
  756. struct ib_mad_recv_wc *mad_recv_wc)
  757. {
  758. struct ib_sa_query *query;
  759. struct ib_mad_send_buf *mad_buf;
  760. mad_buf = (void *) (unsigned long) mad_recv_wc->wc->wr_id;
  761. query = mad_buf->context[0];
  762. if (query->callback) {
  763. if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
  764. query->callback(query,
  765. mad_recv_wc->recv_buf.mad->mad_hdr.status ?
  766. -EINVAL : 0,
  767. (struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
  768. else
  769. query->callback(query, -EIO, NULL);
  770. }
  771. ib_free_recv_mad(mad_recv_wc);
  772. }
  773. static void ib_sa_add_one(struct ib_device *device)
  774. {
  775. struct ib_sa_device *sa_dev;
  776. int s, e, i;
  777. if (device->node_type == IB_NODE_SWITCH)
  778. s = e = 0;
  779. else {
  780. s = 1;
  781. e = device->phys_port_cnt;
  782. }
  783. sa_dev = kmalloc(sizeof *sa_dev +
  784. (e - s + 1) * sizeof (struct ib_sa_port),
  785. GFP_KERNEL);
  786. if (!sa_dev)
  787. return;
  788. sa_dev->start_port = s;
  789. sa_dev->end_port = e;
  790. for (i = 0; i <= e - s; ++i) {
  791. sa_dev->port[i].sm_ah = NULL;
  792. sa_dev->port[i].port_num = i + s;
  793. spin_lock_init(&sa_dev->port[i].ah_lock);
  794. sa_dev->port[i].agent =
  795. ib_register_mad_agent(device, i + s, IB_QPT_GSI,
  796. NULL, 0, send_handler,
  797. recv_handler, sa_dev);
  798. if (IS_ERR(sa_dev->port[i].agent))
  799. goto err;
  800. INIT_WORK(&sa_dev->port[i].update_task,
  801. update_sm_ah, &sa_dev->port[i]);
  802. }
  803. ib_set_client_data(device, &sa_client, sa_dev);
  804. /*
  805. * We register our event handler after everything is set up,
  806. * and then update our cached info after the event handler is
  807. * registered to avoid any problems if a port changes state
  808. * during our initialization.
  809. */
  810. INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
  811. if (ib_register_event_handler(&sa_dev->event_handler))
  812. goto err;
  813. for (i = 0; i <= e - s; ++i)
  814. update_sm_ah(&sa_dev->port[i]);
  815. return;
  816. err:
  817. while (--i >= 0)
  818. ib_unregister_mad_agent(sa_dev->port[i].agent);
  819. kfree(sa_dev);
  820. return;
  821. }
  822. static void ib_sa_remove_one(struct ib_device *device)
  823. {
  824. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  825. int i;
  826. if (!sa_dev)
  827. return;
  828. ib_unregister_event_handler(&sa_dev->event_handler);
  829. for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
  830. ib_unregister_mad_agent(sa_dev->port[i].agent);
  831. kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
  832. }
  833. kfree(sa_dev);
  834. }
  835. static int __init ib_sa_init(void)
  836. {
  837. int ret;
  838. spin_lock_init(&idr_lock);
  839. spin_lock_init(&tid_lock);
  840. get_random_bytes(&tid, sizeof tid);
  841. ret = ib_register_client(&sa_client);
  842. if (ret)
  843. printk(KERN_ERR "Couldn't register ib_sa client\n");
  844. return ret;
  845. }
  846. static void __exit ib_sa_cleanup(void)
  847. {
  848. ib_unregister_client(&sa_client);
  849. idr_destroy(&query_idr);
  850. }
  851. module_init(ib_sa_init);
  852. module_exit(ib_sa_cleanup);