sa_query.c 26 KB

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