sa_query.c 29 KB

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