sa_query.c 28 KB

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