sa_query.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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. if (port->sm_ah)
  369. kref_put(&port->sm_ah->ref, free_sm_ah);
  370. port->sm_ah = new_ah;
  371. spin_unlock_irq(&port->ah_lock);
  372. }
  373. static void ib_sa_event(struct ib_event_handler *handler, struct ib_event *event)
  374. {
  375. if (event->event == IB_EVENT_PORT_ERR ||
  376. event->event == IB_EVENT_PORT_ACTIVE ||
  377. event->event == IB_EVENT_LID_CHANGE ||
  378. event->event == IB_EVENT_PKEY_CHANGE ||
  379. event->event == IB_EVENT_SM_CHANGE ||
  380. event->event == IB_EVENT_CLIENT_REREGISTER) {
  381. unsigned long flags;
  382. struct ib_sa_device *sa_dev =
  383. container_of(handler, typeof(*sa_dev), event_handler);
  384. struct ib_sa_port *port =
  385. &sa_dev->port[event->element.port_num - sa_dev->start_port];
  386. spin_lock_irqsave(&port->ah_lock, flags);
  387. if (port->sm_ah)
  388. kref_put(&port->sm_ah->ref, free_sm_ah);
  389. port->sm_ah = NULL;
  390. spin_unlock_irqrestore(&port->ah_lock, flags);
  391. schedule_work(&sa_dev->port[event->element.port_num -
  392. sa_dev->start_port].update_task);
  393. }
  394. }
  395. void ib_sa_register_client(struct ib_sa_client *client)
  396. {
  397. atomic_set(&client->users, 1);
  398. init_completion(&client->comp);
  399. }
  400. EXPORT_SYMBOL(ib_sa_register_client);
  401. void ib_sa_unregister_client(struct ib_sa_client *client)
  402. {
  403. ib_sa_client_put(client);
  404. wait_for_completion(&client->comp);
  405. }
  406. EXPORT_SYMBOL(ib_sa_unregister_client);
  407. /**
  408. * ib_sa_cancel_query - try to cancel an SA query
  409. * @id:ID of query to cancel
  410. * @query:query pointer to cancel
  411. *
  412. * Try to cancel an SA query. If the id and query don't match up or
  413. * the query has already completed, nothing is done. Otherwise the
  414. * query is canceled and will complete with a status of -EINTR.
  415. */
  416. void ib_sa_cancel_query(int id, struct ib_sa_query *query)
  417. {
  418. unsigned long flags;
  419. struct ib_mad_agent *agent;
  420. struct ib_mad_send_buf *mad_buf;
  421. spin_lock_irqsave(&idr_lock, flags);
  422. if (idr_find(&query_idr, id) != query) {
  423. spin_unlock_irqrestore(&idr_lock, flags);
  424. return;
  425. }
  426. agent = query->port->agent;
  427. mad_buf = query->mad_buf;
  428. spin_unlock_irqrestore(&idr_lock, flags);
  429. ib_cancel_mad(agent, mad_buf);
  430. }
  431. EXPORT_SYMBOL(ib_sa_cancel_query);
  432. static u8 get_src_path_mask(struct ib_device *device, u8 port_num)
  433. {
  434. struct ib_sa_device *sa_dev;
  435. struct ib_sa_port *port;
  436. unsigned long flags;
  437. u8 src_path_mask;
  438. sa_dev = ib_get_client_data(device, &sa_client);
  439. if (!sa_dev)
  440. return 0x7f;
  441. port = &sa_dev->port[port_num - sa_dev->start_port];
  442. spin_lock_irqsave(&port->ah_lock, flags);
  443. src_path_mask = port->sm_ah ? port->sm_ah->src_path_mask : 0x7f;
  444. spin_unlock_irqrestore(&port->ah_lock, flags);
  445. return src_path_mask;
  446. }
  447. int ib_init_ah_from_path(struct ib_device *device, u8 port_num,
  448. struct ib_sa_path_rec *rec, struct ib_ah_attr *ah_attr)
  449. {
  450. int ret;
  451. u16 gid_index;
  452. memset(ah_attr, 0, sizeof *ah_attr);
  453. ah_attr->dlid = be16_to_cpu(rec->dlid);
  454. ah_attr->sl = rec->sl;
  455. ah_attr->src_path_bits = be16_to_cpu(rec->slid) &
  456. get_src_path_mask(device, port_num);
  457. ah_attr->port_num = port_num;
  458. ah_attr->static_rate = rec->rate;
  459. if (rec->hop_limit > 1) {
  460. ah_attr->ah_flags = IB_AH_GRH;
  461. ah_attr->grh.dgid = rec->dgid;
  462. ret = ib_find_cached_gid(device, &rec->sgid, &port_num,
  463. &gid_index);
  464. if (ret)
  465. return ret;
  466. ah_attr->grh.sgid_index = gid_index;
  467. ah_attr->grh.flow_label = be32_to_cpu(rec->flow_label);
  468. ah_attr->grh.hop_limit = rec->hop_limit;
  469. ah_attr->grh.traffic_class = rec->traffic_class;
  470. }
  471. return 0;
  472. }
  473. EXPORT_SYMBOL(ib_init_ah_from_path);
  474. static int alloc_mad(struct ib_sa_query *query, gfp_t gfp_mask)
  475. {
  476. unsigned long flags;
  477. spin_lock_irqsave(&query->port->ah_lock, flags);
  478. if (!query->port->sm_ah) {
  479. spin_unlock_irqrestore(&query->port->ah_lock, flags);
  480. return -EAGAIN;
  481. }
  482. kref_get(&query->port->sm_ah->ref);
  483. query->sm_ah = query->port->sm_ah;
  484. spin_unlock_irqrestore(&query->port->ah_lock, flags);
  485. query->mad_buf = ib_create_send_mad(query->port->agent, 1,
  486. query->sm_ah->pkey_index,
  487. 0, IB_MGMT_SA_HDR, IB_MGMT_SA_DATA,
  488. gfp_mask);
  489. if (IS_ERR(query->mad_buf)) {
  490. kref_put(&query->sm_ah->ref, free_sm_ah);
  491. return -ENOMEM;
  492. }
  493. query->mad_buf->ah = query->sm_ah->ah;
  494. return 0;
  495. }
  496. static void free_mad(struct ib_sa_query *query)
  497. {
  498. ib_free_send_mad(query->mad_buf);
  499. kref_put(&query->sm_ah->ref, free_sm_ah);
  500. }
  501. static void init_mad(struct ib_sa_mad *mad, struct ib_mad_agent *agent)
  502. {
  503. unsigned long flags;
  504. memset(mad, 0, sizeof *mad);
  505. mad->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
  506. mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  507. mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
  508. spin_lock_irqsave(&tid_lock, flags);
  509. mad->mad_hdr.tid =
  510. cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
  511. spin_unlock_irqrestore(&tid_lock, flags);
  512. }
  513. static int send_mad(struct ib_sa_query *query, int timeout_ms, gfp_t gfp_mask)
  514. {
  515. unsigned long flags;
  516. int ret, id;
  517. retry:
  518. if (!idr_pre_get(&query_idr, gfp_mask))
  519. return -ENOMEM;
  520. spin_lock_irqsave(&idr_lock, flags);
  521. ret = idr_get_new(&query_idr, query, &id);
  522. spin_unlock_irqrestore(&idr_lock, flags);
  523. if (ret == -EAGAIN)
  524. goto retry;
  525. if (ret)
  526. return ret;
  527. query->mad_buf->timeout_ms = timeout_ms;
  528. query->mad_buf->context[0] = query;
  529. query->id = id;
  530. ret = ib_post_send_mad(query->mad_buf, NULL);
  531. if (ret) {
  532. spin_lock_irqsave(&idr_lock, flags);
  533. idr_remove(&query_idr, id);
  534. spin_unlock_irqrestore(&idr_lock, flags);
  535. }
  536. /*
  537. * It's not safe to dereference query any more, because the
  538. * send may already have completed and freed the query in
  539. * another context.
  540. */
  541. return ret ? ret : id;
  542. }
  543. static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
  544. int status,
  545. struct ib_sa_mad *mad)
  546. {
  547. struct ib_sa_path_query *query =
  548. container_of(sa_query, struct ib_sa_path_query, sa_query);
  549. if (mad) {
  550. struct ib_sa_path_rec rec;
  551. ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table),
  552. mad->data, &rec);
  553. query->callback(status, &rec, query->context);
  554. } else
  555. query->callback(status, NULL, query->context);
  556. }
  557. static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
  558. {
  559. kfree(container_of(sa_query, struct ib_sa_path_query, sa_query));
  560. }
  561. /**
  562. * ib_sa_path_rec_get - Start a Path get query
  563. * @client:SA client
  564. * @device:device to send query on
  565. * @port_num: port number to send query on
  566. * @rec:Path Record to send in query
  567. * @comp_mask:component mask to send in query
  568. * @timeout_ms:time to wait for response
  569. * @gfp_mask:GFP mask to use for internal allocations
  570. * @callback:function called when query completes, times out or is
  571. * canceled
  572. * @context:opaque user context passed to callback
  573. * @sa_query:query context, used to cancel query
  574. *
  575. * Send a Path Record Get query to the SA to look up a path. The
  576. * callback function will be called when the query completes (or
  577. * fails); status is 0 for a successful response, -EINTR if the query
  578. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  579. * occurred sending the query. The resp parameter of the callback is
  580. * only valid if status is 0.
  581. *
  582. * If the return value of ib_sa_path_rec_get() is negative, it is an
  583. * error code. Otherwise it is a query ID that can be used to cancel
  584. * the query.
  585. */
  586. int ib_sa_path_rec_get(struct ib_sa_client *client,
  587. struct ib_device *device, u8 port_num,
  588. struct ib_sa_path_rec *rec,
  589. ib_sa_comp_mask comp_mask,
  590. int timeout_ms, gfp_t gfp_mask,
  591. void (*callback)(int status,
  592. struct ib_sa_path_rec *resp,
  593. void *context),
  594. void *context,
  595. struct ib_sa_query **sa_query)
  596. {
  597. struct ib_sa_path_query *query;
  598. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  599. struct ib_sa_port *port;
  600. struct ib_mad_agent *agent;
  601. struct ib_sa_mad *mad;
  602. int ret;
  603. if (!sa_dev)
  604. return -ENODEV;
  605. port = &sa_dev->port[port_num - sa_dev->start_port];
  606. agent = port->agent;
  607. query = kmalloc(sizeof *query, gfp_mask);
  608. if (!query)
  609. return -ENOMEM;
  610. query->sa_query.port = port;
  611. ret = alloc_mad(&query->sa_query, gfp_mask);
  612. if (ret)
  613. goto err1;
  614. ib_sa_client_get(client);
  615. query->sa_query.client = client;
  616. query->callback = callback;
  617. query->context = context;
  618. mad = query->sa_query.mad_buf->mad;
  619. init_mad(mad, agent);
  620. query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL;
  621. query->sa_query.release = ib_sa_path_rec_release;
  622. mad->mad_hdr.method = IB_MGMT_METHOD_GET;
  623. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_PATH_REC);
  624. mad->sa_hdr.comp_mask = comp_mask;
  625. ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, mad->data);
  626. *sa_query = &query->sa_query;
  627. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  628. if (ret < 0)
  629. goto err2;
  630. return ret;
  631. err2:
  632. *sa_query = NULL;
  633. ib_sa_client_put(query->sa_query.client);
  634. free_mad(&query->sa_query);
  635. err1:
  636. kfree(query);
  637. return ret;
  638. }
  639. EXPORT_SYMBOL(ib_sa_path_rec_get);
  640. static void ib_sa_service_rec_callback(struct ib_sa_query *sa_query,
  641. int status,
  642. struct ib_sa_mad *mad)
  643. {
  644. struct ib_sa_service_query *query =
  645. container_of(sa_query, struct ib_sa_service_query, sa_query);
  646. if (mad) {
  647. struct ib_sa_service_rec rec;
  648. ib_unpack(service_rec_table, ARRAY_SIZE(service_rec_table),
  649. mad->data, &rec);
  650. query->callback(status, &rec, query->context);
  651. } else
  652. query->callback(status, NULL, query->context);
  653. }
  654. static void ib_sa_service_rec_release(struct ib_sa_query *sa_query)
  655. {
  656. kfree(container_of(sa_query, struct ib_sa_service_query, sa_query));
  657. }
  658. /**
  659. * ib_sa_service_rec_query - Start Service Record operation
  660. * @client:SA client
  661. * @device:device to send request on
  662. * @port_num: port number to send request on
  663. * @method:SA method - should be get, set, or delete
  664. * @rec:Service Record to send in request
  665. * @comp_mask:component mask to send in request
  666. * @timeout_ms:time to wait for response
  667. * @gfp_mask:GFP mask to use for internal allocations
  668. * @callback:function called when request completes, times out or is
  669. * canceled
  670. * @context:opaque user context passed to callback
  671. * @sa_query:request context, used to cancel request
  672. *
  673. * Send a Service Record set/get/delete to the SA to register,
  674. * unregister or query a service record.
  675. * The callback function will be called when the request completes (or
  676. * fails); status is 0 for a successful response, -EINTR if the query
  677. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  678. * occurred sending the query. The resp parameter of the callback is
  679. * only valid if status is 0.
  680. *
  681. * If the return value of ib_sa_service_rec_query() is negative, it is an
  682. * error code. Otherwise it is a request ID that can be used to cancel
  683. * the query.
  684. */
  685. int ib_sa_service_rec_query(struct ib_sa_client *client,
  686. struct ib_device *device, u8 port_num, u8 method,
  687. struct ib_sa_service_rec *rec,
  688. ib_sa_comp_mask comp_mask,
  689. int timeout_ms, gfp_t gfp_mask,
  690. void (*callback)(int status,
  691. struct ib_sa_service_rec *resp,
  692. void *context),
  693. void *context,
  694. struct ib_sa_query **sa_query)
  695. {
  696. struct ib_sa_service_query *query;
  697. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  698. struct ib_sa_port *port;
  699. struct ib_mad_agent *agent;
  700. struct ib_sa_mad *mad;
  701. int ret;
  702. if (!sa_dev)
  703. return -ENODEV;
  704. port = &sa_dev->port[port_num - sa_dev->start_port];
  705. agent = port->agent;
  706. if (method != IB_MGMT_METHOD_GET &&
  707. method != IB_MGMT_METHOD_SET &&
  708. method != IB_SA_METHOD_DELETE)
  709. return -EINVAL;
  710. query = kmalloc(sizeof *query, gfp_mask);
  711. if (!query)
  712. return -ENOMEM;
  713. query->sa_query.port = port;
  714. ret = alloc_mad(&query->sa_query, gfp_mask);
  715. if (ret)
  716. goto err1;
  717. ib_sa_client_get(client);
  718. query->sa_query.client = client;
  719. query->callback = callback;
  720. query->context = context;
  721. mad = query->sa_query.mad_buf->mad;
  722. init_mad(mad, agent);
  723. query->sa_query.callback = callback ? ib_sa_service_rec_callback : NULL;
  724. query->sa_query.release = ib_sa_service_rec_release;
  725. mad->mad_hdr.method = method;
  726. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_SERVICE_REC);
  727. mad->sa_hdr.comp_mask = comp_mask;
  728. ib_pack(service_rec_table, ARRAY_SIZE(service_rec_table),
  729. rec, mad->data);
  730. *sa_query = &query->sa_query;
  731. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  732. if (ret < 0)
  733. goto err2;
  734. return ret;
  735. err2:
  736. *sa_query = NULL;
  737. ib_sa_client_put(query->sa_query.client);
  738. free_mad(&query->sa_query);
  739. err1:
  740. kfree(query);
  741. return ret;
  742. }
  743. EXPORT_SYMBOL(ib_sa_service_rec_query);
  744. static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
  745. int status,
  746. struct ib_sa_mad *mad)
  747. {
  748. struct ib_sa_mcmember_query *query =
  749. container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
  750. if (mad) {
  751. struct ib_sa_mcmember_rec rec;
  752. ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  753. mad->data, &rec);
  754. query->callback(status, &rec, query->context);
  755. } else
  756. query->callback(status, NULL, query->context);
  757. }
  758. static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
  759. {
  760. kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
  761. }
  762. int ib_sa_mcmember_rec_query(struct ib_sa_client *client,
  763. struct ib_device *device, u8 port_num,
  764. u8 method,
  765. struct ib_sa_mcmember_rec *rec,
  766. ib_sa_comp_mask comp_mask,
  767. int timeout_ms, gfp_t gfp_mask,
  768. void (*callback)(int status,
  769. struct ib_sa_mcmember_rec *resp,
  770. void *context),
  771. void *context,
  772. struct ib_sa_query **sa_query)
  773. {
  774. struct ib_sa_mcmember_query *query;
  775. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  776. struct ib_sa_port *port;
  777. struct ib_mad_agent *agent;
  778. struct ib_sa_mad *mad;
  779. int ret;
  780. if (!sa_dev)
  781. return -ENODEV;
  782. port = &sa_dev->port[port_num - sa_dev->start_port];
  783. agent = port->agent;
  784. query = kmalloc(sizeof *query, gfp_mask);
  785. if (!query)
  786. return -ENOMEM;
  787. query->sa_query.port = port;
  788. ret = alloc_mad(&query->sa_query, gfp_mask);
  789. if (ret)
  790. goto err1;
  791. ib_sa_client_get(client);
  792. query->sa_query.client = client;
  793. query->callback = callback;
  794. query->context = context;
  795. mad = query->sa_query.mad_buf->mad;
  796. init_mad(mad, agent);
  797. query->sa_query.callback = callback ? ib_sa_mcmember_rec_callback : NULL;
  798. query->sa_query.release = ib_sa_mcmember_rec_release;
  799. mad->mad_hdr.method = method;
  800. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
  801. mad->sa_hdr.comp_mask = comp_mask;
  802. ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  803. rec, mad->data);
  804. *sa_query = &query->sa_query;
  805. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  806. if (ret < 0)
  807. goto err2;
  808. return ret;
  809. err2:
  810. *sa_query = NULL;
  811. ib_sa_client_put(query->sa_query.client);
  812. free_mad(&query->sa_query);
  813. err1:
  814. kfree(query);
  815. return ret;
  816. }
  817. static void send_handler(struct ib_mad_agent *agent,
  818. struct ib_mad_send_wc *mad_send_wc)
  819. {
  820. struct ib_sa_query *query = mad_send_wc->send_buf->context[0];
  821. unsigned long flags;
  822. if (query->callback)
  823. switch (mad_send_wc->status) {
  824. case IB_WC_SUCCESS:
  825. /* No callback -- already got recv */
  826. break;
  827. case IB_WC_RESP_TIMEOUT_ERR:
  828. query->callback(query, -ETIMEDOUT, NULL);
  829. break;
  830. case IB_WC_WR_FLUSH_ERR:
  831. query->callback(query, -EINTR, NULL);
  832. break;
  833. default:
  834. query->callback(query, -EIO, NULL);
  835. break;
  836. }
  837. spin_lock_irqsave(&idr_lock, flags);
  838. idr_remove(&query_idr, query->id);
  839. spin_unlock_irqrestore(&idr_lock, flags);
  840. free_mad(query);
  841. ib_sa_client_put(query->client);
  842. query->release(query);
  843. }
  844. static void recv_handler(struct ib_mad_agent *mad_agent,
  845. struct ib_mad_recv_wc *mad_recv_wc)
  846. {
  847. struct ib_sa_query *query;
  848. struct ib_mad_send_buf *mad_buf;
  849. mad_buf = (void *) (unsigned long) mad_recv_wc->wc->wr_id;
  850. query = mad_buf->context[0];
  851. if (query->callback) {
  852. if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
  853. query->callback(query,
  854. mad_recv_wc->recv_buf.mad->mad_hdr.status ?
  855. -EINVAL : 0,
  856. (struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
  857. else
  858. query->callback(query, -EIO, NULL);
  859. }
  860. ib_free_recv_mad(mad_recv_wc);
  861. }
  862. static void ib_sa_add_one(struct ib_device *device)
  863. {
  864. struct ib_sa_device *sa_dev;
  865. int s, e, i;
  866. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  867. return;
  868. if (device->node_type == RDMA_NODE_IB_SWITCH)
  869. s = e = 0;
  870. else {
  871. s = 1;
  872. e = device->phys_port_cnt;
  873. }
  874. sa_dev = kmalloc(sizeof *sa_dev +
  875. (e - s + 1) * sizeof (struct ib_sa_port),
  876. GFP_KERNEL);
  877. if (!sa_dev)
  878. return;
  879. sa_dev->start_port = s;
  880. sa_dev->end_port = e;
  881. for (i = 0; i <= e - s; ++i) {
  882. sa_dev->port[i].sm_ah = NULL;
  883. sa_dev->port[i].port_num = i + s;
  884. spin_lock_init(&sa_dev->port[i].ah_lock);
  885. sa_dev->port[i].agent =
  886. ib_register_mad_agent(device, i + s, IB_QPT_GSI,
  887. NULL, 0, send_handler,
  888. recv_handler, sa_dev);
  889. if (IS_ERR(sa_dev->port[i].agent))
  890. goto err;
  891. INIT_WORK(&sa_dev->port[i].update_task, update_sm_ah);
  892. }
  893. ib_set_client_data(device, &sa_client, sa_dev);
  894. /*
  895. * We register our event handler after everything is set up,
  896. * and then update our cached info after the event handler is
  897. * registered to avoid any problems if a port changes state
  898. * during our initialization.
  899. */
  900. INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
  901. if (ib_register_event_handler(&sa_dev->event_handler))
  902. goto err;
  903. for (i = 0; i <= e - s; ++i)
  904. update_sm_ah(&sa_dev->port[i].update_task);
  905. return;
  906. err:
  907. while (--i >= 0)
  908. ib_unregister_mad_agent(sa_dev->port[i].agent);
  909. kfree(sa_dev);
  910. return;
  911. }
  912. static void ib_sa_remove_one(struct ib_device *device)
  913. {
  914. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  915. int i;
  916. if (!sa_dev)
  917. return;
  918. ib_unregister_event_handler(&sa_dev->event_handler);
  919. flush_scheduled_work();
  920. for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
  921. ib_unregister_mad_agent(sa_dev->port[i].agent);
  922. if (sa_dev->port[i].sm_ah)
  923. kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
  924. }
  925. kfree(sa_dev);
  926. }
  927. static int __init ib_sa_init(void)
  928. {
  929. int ret;
  930. spin_lock_init(&idr_lock);
  931. spin_lock_init(&tid_lock);
  932. get_random_bytes(&tid, sizeof tid);
  933. ret = ib_register_client(&sa_client);
  934. if (ret) {
  935. printk(KERN_ERR "Couldn't register ib_sa client\n");
  936. goto err1;
  937. }
  938. ret = mcast_init();
  939. if (ret) {
  940. printk(KERN_ERR "Couldn't initialize multicast handling\n");
  941. goto err2;
  942. }
  943. return 0;
  944. err2:
  945. ib_unregister_client(&sa_client);
  946. err1:
  947. return ret;
  948. }
  949. static void __exit ib_sa_cleanup(void)
  950. {
  951. mcast_cleanup();
  952. ib_unregister_client(&sa_client);
  953. idr_destroy(&query_idr);
  954. }
  955. module_init(ib_sa_init);
  956. module_exit(ib_sa_cleanup);