sa_query.c 26 KB

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