sa_query.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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_sa_mad *mad;
  71. struct ib_sa_sm_ah *sm_ah;
  72. DECLARE_PCI_UNMAP_ADDR(mapping)
  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. 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. spin_unlock_irqrestore(&idr_lock, flags);
  403. ib_cancel_mad(agent, id);
  404. }
  405. EXPORT_SYMBOL(ib_sa_cancel_query);
  406. static void init_mad(struct ib_sa_mad *mad, struct ib_mad_agent *agent)
  407. {
  408. unsigned long flags;
  409. memset(mad, 0, sizeof *mad);
  410. mad->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
  411. mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  412. mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
  413. spin_lock_irqsave(&tid_lock, flags);
  414. mad->mad_hdr.tid =
  415. cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
  416. spin_unlock_irqrestore(&tid_lock, flags);
  417. }
  418. static int send_mad(struct ib_sa_query *query, int timeout_ms)
  419. {
  420. struct ib_sa_port *port = query->port;
  421. unsigned long flags;
  422. int ret;
  423. struct ib_sge gather_list;
  424. struct ib_send_wr *bad_wr, wr = {
  425. .opcode = IB_WR_SEND,
  426. .sg_list = &gather_list,
  427. .num_sge = 1,
  428. .send_flags = IB_SEND_SIGNALED,
  429. .wr = {
  430. .ud = {
  431. .mad_hdr = &query->mad->mad_hdr,
  432. .remote_qpn = 1,
  433. .remote_qkey = IB_QP1_QKEY,
  434. .timeout_ms = timeout_ms,
  435. }
  436. }
  437. };
  438. retry:
  439. if (!idr_pre_get(&query_idr, GFP_ATOMIC))
  440. return -ENOMEM;
  441. spin_lock_irqsave(&idr_lock, flags);
  442. ret = idr_get_new(&query_idr, query, &query->id);
  443. spin_unlock_irqrestore(&idr_lock, flags);
  444. if (ret == -EAGAIN)
  445. goto retry;
  446. if (ret)
  447. return ret;
  448. wr.wr_id = query->id;
  449. spin_lock_irqsave(&port->ah_lock, flags);
  450. kref_get(&port->sm_ah->ref);
  451. query->sm_ah = port->sm_ah;
  452. wr.wr.ud.ah = port->sm_ah->ah;
  453. spin_unlock_irqrestore(&port->ah_lock, flags);
  454. gather_list.addr = dma_map_single(port->agent->device->dma_device,
  455. query->mad,
  456. sizeof (struct ib_sa_mad),
  457. DMA_TO_DEVICE);
  458. gather_list.length = sizeof (struct ib_sa_mad);
  459. gather_list.lkey = port->agent->mr->lkey;
  460. pci_unmap_addr_set(query, mapping, gather_list.addr);
  461. ret = ib_post_send_mad(port->agent, &wr, &bad_wr);
  462. if (ret) {
  463. dma_unmap_single(port->agent->device->dma_device,
  464. pci_unmap_addr(query, mapping),
  465. sizeof (struct ib_sa_mad),
  466. DMA_TO_DEVICE);
  467. kref_put(&query->sm_ah->ref, free_sm_ah);
  468. spin_lock_irqsave(&idr_lock, flags);
  469. idr_remove(&query_idr, query->id);
  470. spin_unlock_irqrestore(&idr_lock, flags);
  471. }
  472. /*
  473. * It's not safe to dereference query any more, because the
  474. * send may already have completed and freed the query in
  475. * another context. So use wr.wr_id, which has a copy of the
  476. * query's id.
  477. */
  478. return ret ? ret : wr.wr_id;
  479. }
  480. static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
  481. int status,
  482. struct ib_sa_mad *mad)
  483. {
  484. struct ib_sa_path_query *query =
  485. container_of(sa_query, struct ib_sa_path_query, sa_query);
  486. if (mad) {
  487. struct ib_sa_path_rec rec;
  488. ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table),
  489. mad->data, &rec);
  490. query->callback(status, &rec, query->context);
  491. } else
  492. query->callback(status, NULL, query->context);
  493. }
  494. static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
  495. {
  496. kfree(sa_query->mad);
  497. kfree(container_of(sa_query, struct ib_sa_path_query, sa_query));
  498. }
  499. /**
  500. * ib_sa_path_rec_get - Start a Path get query
  501. * @device:device to send query on
  502. * @port_num: port number to send query on
  503. * @rec:Path Record to send in query
  504. * @comp_mask:component mask to send in query
  505. * @timeout_ms:time to wait for response
  506. * @gfp_mask:GFP mask to use for internal allocations
  507. * @callback:function called when query completes, times out or is
  508. * canceled
  509. * @context:opaque user context passed to callback
  510. * @sa_query:query context, used to cancel query
  511. *
  512. * Send a Path Record Get query to the SA to look up a path. The
  513. * callback function will be called when the query completes (or
  514. * fails); status is 0 for a successful response, -EINTR if the query
  515. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  516. * occurred sending the query. The resp parameter of the callback is
  517. * only valid if status is 0.
  518. *
  519. * If the return value of ib_sa_path_rec_get() is negative, it is an
  520. * error code. Otherwise it is a query ID that can be used to cancel
  521. * the query.
  522. */
  523. int ib_sa_path_rec_get(struct ib_device *device, u8 port_num,
  524. struct ib_sa_path_rec *rec,
  525. ib_sa_comp_mask comp_mask,
  526. int timeout_ms, unsigned int __nocast gfp_mask,
  527. void (*callback)(int status,
  528. struct ib_sa_path_rec *resp,
  529. void *context),
  530. void *context,
  531. struct ib_sa_query **sa_query)
  532. {
  533. struct ib_sa_path_query *query;
  534. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  535. struct ib_sa_port *port = &sa_dev->port[port_num - sa_dev->start_port];
  536. struct ib_mad_agent *agent = port->agent;
  537. int ret;
  538. query = kmalloc(sizeof *query, gfp_mask);
  539. if (!query)
  540. return -ENOMEM;
  541. query->sa_query.mad = kmalloc(sizeof *query->sa_query.mad, gfp_mask);
  542. if (!query->sa_query.mad) {
  543. kfree(query);
  544. return -ENOMEM;
  545. }
  546. query->callback = callback;
  547. query->context = context;
  548. init_mad(query->sa_query.mad, agent);
  549. query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL;
  550. query->sa_query.release = ib_sa_path_rec_release;
  551. query->sa_query.port = port;
  552. query->sa_query.mad->mad_hdr.method = IB_MGMT_METHOD_GET;
  553. query->sa_query.mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_PATH_REC);
  554. query->sa_query.mad->sa_hdr.comp_mask = comp_mask;
  555. ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table),
  556. rec, query->sa_query.mad->data);
  557. *sa_query = &query->sa_query;
  558. ret = send_mad(&query->sa_query, timeout_ms);
  559. if (ret < 0) {
  560. *sa_query = NULL;
  561. kfree(query->sa_query.mad);
  562. kfree(query);
  563. }
  564. return ret;
  565. }
  566. EXPORT_SYMBOL(ib_sa_path_rec_get);
  567. static void ib_sa_service_rec_callback(struct ib_sa_query *sa_query,
  568. int status,
  569. struct ib_sa_mad *mad)
  570. {
  571. struct ib_sa_service_query *query =
  572. container_of(sa_query, struct ib_sa_service_query, sa_query);
  573. if (mad) {
  574. struct ib_sa_service_rec rec;
  575. ib_unpack(service_rec_table, ARRAY_SIZE(service_rec_table),
  576. mad->data, &rec);
  577. query->callback(status, &rec, query->context);
  578. } else
  579. query->callback(status, NULL, query->context);
  580. }
  581. static void ib_sa_service_rec_release(struct ib_sa_query *sa_query)
  582. {
  583. kfree(sa_query->mad);
  584. kfree(container_of(sa_query, struct ib_sa_service_query, sa_query));
  585. }
  586. /**
  587. * ib_sa_service_rec_query - Start Service Record operation
  588. * @device:device to send request on
  589. * @port_num: port number to send request on
  590. * @method:SA method - should be get, set, or delete
  591. * @rec:Service Record to send in request
  592. * @comp_mask:component mask to send in request
  593. * @timeout_ms:time to wait for response
  594. * @gfp_mask:GFP mask to use for internal allocations
  595. * @callback:function called when request completes, times out or is
  596. * canceled
  597. * @context:opaque user context passed to callback
  598. * @sa_query:request context, used to cancel request
  599. *
  600. * Send a Service Record set/get/delete to the SA to register,
  601. * unregister or query a service record.
  602. * The callback function will be called when the request completes (or
  603. * fails); status is 0 for a successful response, -EINTR if the query
  604. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  605. * occurred sending the query. The resp parameter of the callback is
  606. * only valid if status is 0.
  607. *
  608. * If the return value of ib_sa_service_rec_query() is negative, it is an
  609. * error code. Otherwise it is a request ID that can be used to cancel
  610. * the query.
  611. */
  612. int ib_sa_service_rec_query(struct ib_device *device, u8 port_num, u8 method,
  613. struct ib_sa_service_rec *rec,
  614. ib_sa_comp_mask comp_mask,
  615. int timeout_ms, unsigned int __nocast gfp_mask,
  616. void (*callback)(int status,
  617. struct ib_sa_service_rec *resp,
  618. void *context),
  619. void *context,
  620. struct ib_sa_query **sa_query)
  621. {
  622. struct ib_sa_service_query *query;
  623. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  624. struct ib_sa_port *port = &sa_dev->port[port_num - sa_dev->start_port];
  625. struct ib_mad_agent *agent = port->agent;
  626. int ret;
  627. if (method != IB_MGMT_METHOD_GET &&
  628. method != IB_MGMT_METHOD_SET &&
  629. method != IB_SA_METHOD_DELETE)
  630. return -EINVAL;
  631. query = kmalloc(sizeof *query, gfp_mask);
  632. if (!query)
  633. return -ENOMEM;
  634. query->sa_query.mad = kmalloc(sizeof *query->sa_query.mad, gfp_mask);
  635. if (!query->sa_query.mad) {
  636. kfree(query);
  637. return -ENOMEM;
  638. }
  639. query->callback = callback;
  640. query->context = context;
  641. init_mad(query->sa_query.mad, agent);
  642. query->sa_query.callback = callback ? ib_sa_service_rec_callback : NULL;
  643. query->sa_query.release = ib_sa_service_rec_release;
  644. query->sa_query.port = port;
  645. query->sa_query.mad->mad_hdr.method = method;
  646. query->sa_query.mad->mad_hdr.attr_id =
  647. cpu_to_be16(IB_SA_ATTR_SERVICE_REC);
  648. query->sa_query.mad->sa_hdr.comp_mask = comp_mask;
  649. ib_pack(service_rec_table, ARRAY_SIZE(service_rec_table),
  650. rec, query->sa_query.mad->data);
  651. *sa_query = &query->sa_query;
  652. ret = send_mad(&query->sa_query, timeout_ms);
  653. if (ret < 0) {
  654. *sa_query = NULL;
  655. kfree(query->sa_query.mad);
  656. kfree(query);
  657. }
  658. return ret;
  659. }
  660. EXPORT_SYMBOL(ib_sa_service_rec_query);
  661. static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
  662. int status,
  663. struct ib_sa_mad *mad)
  664. {
  665. struct ib_sa_mcmember_query *query =
  666. container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
  667. if (mad) {
  668. struct ib_sa_mcmember_rec rec;
  669. ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  670. mad->data, &rec);
  671. query->callback(status, &rec, query->context);
  672. } else
  673. query->callback(status, NULL, query->context);
  674. }
  675. static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
  676. {
  677. kfree(sa_query->mad);
  678. kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
  679. }
  680. int ib_sa_mcmember_rec_query(struct ib_device *device, u8 port_num,
  681. u8 method,
  682. struct ib_sa_mcmember_rec *rec,
  683. ib_sa_comp_mask comp_mask,
  684. int timeout_ms, unsigned int __nocast gfp_mask,
  685. void (*callback)(int status,
  686. struct ib_sa_mcmember_rec *resp,
  687. void *context),
  688. void *context,
  689. struct ib_sa_query **sa_query)
  690. {
  691. struct ib_sa_mcmember_query *query;
  692. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  693. struct ib_sa_port *port = &sa_dev->port[port_num - sa_dev->start_port];
  694. struct ib_mad_agent *agent = port->agent;
  695. int ret;
  696. query = kmalloc(sizeof *query, gfp_mask);
  697. if (!query)
  698. return -ENOMEM;
  699. query->sa_query.mad = kmalloc(sizeof *query->sa_query.mad, gfp_mask);
  700. if (!query->sa_query.mad) {
  701. kfree(query);
  702. return -ENOMEM;
  703. }
  704. query->callback = callback;
  705. query->context = context;
  706. init_mad(query->sa_query.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. query->sa_query.mad->mad_hdr.method = method;
  711. query->sa_query.mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
  712. query->sa_query.mad->sa_hdr.comp_mask = comp_mask;
  713. ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  714. rec, query->sa_query.mad->data);
  715. *sa_query = &query->sa_query;
  716. ret = send_mad(&query->sa_query, timeout_ms);
  717. if (ret < 0) {
  718. *sa_query = NULL;
  719. kfree(query->sa_query.mad);
  720. kfree(query);
  721. }
  722. return ret;
  723. }
  724. EXPORT_SYMBOL(ib_sa_mcmember_rec_query);
  725. static void send_handler(struct ib_mad_agent *agent,
  726. struct ib_mad_send_wc *mad_send_wc)
  727. {
  728. struct ib_sa_query *query;
  729. unsigned long flags;
  730. spin_lock_irqsave(&idr_lock, flags);
  731. query = idr_find(&query_idr, mad_send_wc->wr_id);
  732. spin_unlock_irqrestore(&idr_lock, flags);
  733. if (!query)
  734. return;
  735. if (query->callback)
  736. switch (mad_send_wc->status) {
  737. case IB_WC_SUCCESS:
  738. /* No callback -- already got recv */
  739. break;
  740. case IB_WC_RESP_TIMEOUT_ERR:
  741. query->callback(query, -ETIMEDOUT, NULL);
  742. break;
  743. case IB_WC_WR_FLUSH_ERR:
  744. query->callback(query, -EINTR, NULL);
  745. break;
  746. default:
  747. query->callback(query, -EIO, NULL);
  748. break;
  749. }
  750. dma_unmap_single(agent->device->dma_device,
  751. pci_unmap_addr(query, mapping),
  752. sizeof (struct ib_sa_mad),
  753. DMA_TO_DEVICE);
  754. kref_put(&query->sm_ah->ref, free_sm_ah);
  755. query->release(query);
  756. spin_lock_irqsave(&idr_lock, flags);
  757. idr_remove(&query_idr, mad_send_wc->wr_id);
  758. spin_unlock_irqrestore(&idr_lock, flags);
  759. }
  760. static void recv_handler(struct ib_mad_agent *mad_agent,
  761. struct ib_mad_recv_wc *mad_recv_wc)
  762. {
  763. struct ib_sa_query *query;
  764. unsigned long flags;
  765. spin_lock_irqsave(&idr_lock, flags);
  766. query = idr_find(&query_idr, mad_recv_wc->wc->wr_id);
  767. spin_unlock_irqrestore(&idr_lock, flags);
  768. if (query && query->callback) {
  769. if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
  770. query->callback(query,
  771. mad_recv_wc->recv_buf.mad->mad_hdr.status ?
  772. -EINVAL : 0,
  773. (struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
  774. else
  775. query->callback(query, -EIO, NULL);
  776. }
  777. ib_free_recv_mad(mad_recv_wc);
  778. }
  779. static void ib_sa_add_one(struct ib_device *device)
  780. {
  781. struct ib_sa_device *sa_dev;
  782. int s, e, i;
  783. if (device->node_type == IB_NODE_SWITCH)
  784. s = e = 0;
  785. else {
  786. s = 1;
  787. e = device->phys_port_cnt;
  788. }
  789. sa_dev = kmalloc(sizeof *sa_dev +
  790. (e - s + 1) * sizeof (struct ib_sa_port),
  791. GFP_KERNEL);
  792. if (!sa_dev)
  793. return;
  794. sa_dev->start_port = s;
  795. sa_dev->end_port = e;
  796. for (i = 0; i <= e - s; ++i) {
  797. sa_dev->port[i].sm_ah = NULL;
  798. sa_dev->port[i].port_num = i + s;
  799. spin_lock_init(&sa_dev->port[i].ah_lock);
  800. sa_dev->port[i].agent =
  801. ib_register_mad_agent(device, i + s, IB_QPT_GSI,
  802. NULL, 0, send_handler,
  803. recv_handler, sa_dev);
  804. if (IS_ERR(sa_dev->port[i].agent))
  805. goto err;
  806. INIT_WORK(&sa_dev->port[i].update_task,
  807. update_sm_ah, &sa_dev->port[i]);
  808. }
  809. ib_set_client_data(device, &sa_client, sa_dev);
  810. /*
  811. * We register our event handler after everything is set up,
  812. * and then update our cached info after the event handler is
  813. * registered to avoid any problems if a port changes state
  814. * during our initialization.
  815. */
  816. INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
  817. if (ib_register_event_handler(&sa_dev->event_handler))
  818. goto err;
  819. for (i = 0; i <= e - s; ++i)
  820. update_sm_ah(&sa_dev->port[i]);
  821. return;
  822. err:
  823. while (--i >= 0)
  824. ib_unregister_mad_agent(sa_dev->port[i].agent);
  825. kfree(sa_dev);
  826. return;
  827. }
  828. static void ib_sa_remove_one(struct ib_device *device)
  829. {
  830. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  831. int i;
  832. if (!sa_dev)
  833. return;
  834. ib_unregister_event_handler(&sa_dev->event_handler);
  835. for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
  836. ib_unregister_mad_agent(sa_dev->port[i].agent);
  837. kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
  838. }
  839. kfree(sa_dev);
  840. }
  841. static int __init ib_sa_init(void)
  842. {
  843. int ret;
  844. spin_lock_init(&idr_lock);
  845. spin_lock_init(&tid_lock);
  846. get_random_bytes(&tid, sizeof tid);
  847. ret = ib_register_client(&sa_client);
  848. if (ret)
  849. printk(KERN_ERR "Couldn't register ib_sa client\n");
  850. return ret;
  851. }
  852. static void __exit ib_sa_cleanup(void)
  853. {
  854. ib_unregister_client(&sa_client);
  855. }
  856. module_init(ib_sa_init);
  857. module_exit(ib_sa_cleanup);