sa_query.c 29 KB

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