sa_query.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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 <uapi/linux/if_ether.h>
  45. #include <rdma/ib_pack.h>
  46. #include <rdma/ib_cache.h>
  47. #include "sa.h"
  48. MODULE_AUTHOR("Roland Dreier");
  49. MODULE_DESCRIPTION("InfiniBand subnet administration query support");
  50. MODULE_LICENSE("Dual BSD/GPL");
  51. struct ib_sa_sm_ah {
  52. struct ib_ah *ah;
  53. struct kref ref;
  54. u16 pkey_index;
  55. u8 src_path_mask;
  56. };
  57. struct ib_sa_port {
  58. struct ib_mad_agent *agent;
  59. struct ib_sa_sm_ah *sm_ah;
  60. struct work_struct update_task;
  61. spinlock_t ah_lock;
  62. u8 port_num;
  63. };
  64. struct ib_sa_device {
  65. int start_port, end_port;
  66. struct ib_event_handler event_handler;
  67. struct ib_sa_port port[0];
  68. };
  69. struct ib_sa_query {
  70. void (*callback)(struct ib_sa_query *, int, struct ib_sa_mad *);
  71. void (*release)(struct ib_sa_query *);
  72. struct ib_sa_client *client;
  73. struct ib_sa_port *port;
  74. struct ib_mad_send_buf *mad_buf;
  75. struct ib_sa_sm_ah *sm_ah;
  76. int id;
  77. };
  78. struct ib_sa_service_query {
  79. void (*callback)(int, struct ib_sa_service_rec *, void *);
  80. void *context;
  81. struct ib_sa_query sa_query;
  82. };
  83. struct ib_sa_path_query {
  84. void (*callback)(int, struct ib_sa_path_rec *, void *);
  85. void *context;
  86. struct ib_sa_query sa_query;
  87. };
  88. struct ib_sa_guidinfo_query {
  89. void (*callback)(int, struct ib_sa_guidinfo_rec *, void *);
  90. void *context;
  91. struct ib_sa_query sa_query;
  92. };
  93. struct ib_sa_mcmember_query {
  94. void (*callback)(int, struct ib_sa_mcmember_rec *, void *);
  95. void *context;
  96. struct ib_sa_query sa_query;
  97. };
  98. static void ib_sa_add_one(struct ib_device *device);
  99. static void ib_sa_remove_one(struct ib_device *device);
  100. static struct ib_client sa_client = {
  101. .name = "sa",
  102. .add = ib_sa_add_one,
  103. .remove = ib_sa_remove_one
  104. };
  105. static DEFINE_SPINLOCK(idr_lock);
  106. static DEFINE_IDR(query_idr);
  107. static DEFINE_SPINLOCK(tid_lock);
  108. static u32 tid;
  109. #define PATH_REC_FIELD(field) \
  110. .struct_offset_bytes = offsetof(struct ib_sa_path_rec, field), \
  111. .struct_size_bytes = sizeof ((struct ib_sa_path_rec *) 0)->field, \
  112. .field_name = "sa_path_rec:" #field
  113. static const struct ib_field path_rec_table[] = {
  114. { PATH_REC_FIELD(service_id),
  115. .offset_words = 0,
  116. .offset_bits = 0,
  117. .size_bits = 64 },
  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. { PATH_REC_FIELD(qos_class),
  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. #define GUIDINFO_REC_FIELD(field) \
  336. .struct_offset_bytes = offsetof(struct ib_sa_guidinfo_rec, field), \
  337. .struct_size_bytes = sizeof((struct ib_sa_guidinfo_rec *) 0)->field, \
  338. .field_name = "sa_guidinfo_rec:" #field
  339. static const struct ib_field guidinfo_rec_table[] = {
  340. { GUIDINFO_REC_FIELD(lid),
  341. .offset_words = 0,
  342. .offset_bits = 0,
  343. .size_bits = 16 },
  344. { GUIDINFO_REC_FIELD(block_num),
  345. .offset_words = 0,
  346. .offset_bits = 16,
  347. .size_bits = 8 },
  348. { GUIDINFO_REC_FIELD(res1),
  349. .offset_words = 0,
  350. .offset_bits = 24,
  351. .size_bits = 8 },
  352. { GUIDINFO_REC_FIELD(res2),
  353. .offset_words = 1,
  354. .offset_bits = 0,
  355. .size_bits = 32 },
  356. { GUIDINFO_REC_FIELD(guid_info_list),
  357. .offset_words = 2,
  358. .offset_bits = 0,
  359. .size_bits = 512 },
  360. };
  361. static void free_sm_ah(struct kref *kref)
  362. {
  363. struct ib_sa_sm_ah *sm_ah = container_of(kref, struct ib_sa_sm_ah, ref);
  364. ib_destroy_ah(sm_ah->ah);
  365. kfree(sm_ah);
  366. }
  367. static void update_sm_ah(struct work_struct *work)
  368. {
  369. struct ib_sa_port *port =
  370. container_of(work, struct ib_sa_port, update_task);
  371. struct ib_sa_sm_ah *new_ah;
  372. struct ib_port_attr port_attr;
  373. struct ib_ah_attr ah_attr;
  374. if (ib_query_port(port->agent->device, port->port_num, &port_attr)) {
  375. printk(KERN_WARNING "Couldn't query port\n");
  376. return;
  377. }
  378. new_ah = kmalloc(sizeof *new_ah, GFP_KERNEL);
  379. if (!new_ah) {
  380. printk(KERN_WARNING "Couldn't allocate new SM AH\n");
  381. return;
  382. }
  383. kref_init(&new_ah->ref);
  384. new_ah->src_path_mask = (1 << port_attr.lmc) - 1;
  385. new_ah->pkey_index = 0;
  386. if (ib_find_pkey(port->agent->device, port->port_num,
  387. IB_DEFAULT_PKEY_FULL, &new_ah->pkey_index))
  388. printk(KERN_ERR "Couldn't find index for default PKey\n");
  389. memset(&ah_attr, 0, sizeof ah_attr);
  390. ah_attr.dlid = port_attr.sm_lid;
  391. ah_attr.sl = port_attr.sm_sl;
  392. ah_attr.port_num = port->port_num;
  393. new_ah->ah = ib_create_ah(port->agent->qp->pd, &ah_attr);
  394. if (IS_ERR(new_ah->ah)) {
  395. printk(KERN_WARNING "Couldn't create new SM AH\n");
  396. kfree(new_ah);
  397. return;
  398. }
  399. spin_lock_irq(&port->ah_lock);
  400. if (port->sm_ah)
  401. kref_put(&port->sm_ah->ref, free_sm_ah);
  402. port->sm_ah = new_ah;
  403. spin_unlock_irq(&port->ah_lock);
  404. }
  405. static void ib_sa_event(struct ib_event_handler *handler, struct ib_event *event)
  406. {
  407. if (event->event == IB_EVENT_PORT_ERR ||
  408. event->event == IB_EVENT_PORT_ACTIVE ||
  409. event->event == IB_EVENT_LID_CHANGE ||
  410. event->event == IB_EVENT_PKEY_CHANGE ||
  411. event->event == IB_EVENT_SM_CHANGE ||
  412. event->event == IB_EVENT_CLIENT_REREGISTER) {
  413. unsigned long flags;
  414. struct ib_sa_device *sa_dev =
  415. container_of(handler, typeof(*sa_dev), event_handler);
  416. struct ib_sa_port *port =
  417. &sa_dev->port[event->element.port_num - sa_dev->start_port];
  418. if (!rdma_cap_ib_sa(handler->device, port->port_num))
  419. return;
  420. spin_lock_irqsave(&port->ah_lock, flags);
  421. if (port->sm_ah)
  422. kref_put(&port->sm_ah->ref, free_sm_ah);
  423. port->sm_ah = NULL;
  424. spin_unlock_irqrestore(&port->ah_lock, flags);
  425. queue_work(ib_wq, &sa_dev->port[event->element.port_num -
  426. sa_dev->start_port].update_task);
  427. }
  428. }
  429. void ib_sa_register_client(struct ib_sa_client *client)
  430. {
  431. atomic_set(&client->users, 1);
  432. init_completion(&client->comp);
  433. }
  434. EXPORT_SYMBOL(ib_sa_register_client);
  435. void ib_sa_unregister_client(struct ib_sa_client *client)
  436. {
  437. ib_sa_client_put(client);
  438. wait_for_completion(&client->comp);
  439. }
  440. EXPORT_SYMBOL(ib_sa_unregister_client);
  441. /**
  442. * ib_sa_cancel_query - try to cancel an SA query
  443. * @id:ID of query to cancel
  444. * @query:query pointer to cancel
  445. *
  446. * Try to cancel an SA query. If the id and query don't match up or
  447. * the query has already completed, nothing is done. Otherwise the
  448. * query is canceled and will complete with a status of -EINTR.
  449. */
  450. void ib_sa_cancel_query(int id, struct ib_sa_query *query)
  451. {
  452. unsigned long flags;
  453. struct ib_mad_agent *agent;
  454. struct ib_mad_send_buf *mad_buf;
  455. spin_lock_irqsave(&idr_lock, flags);
  456. if (idr_find(&query_idr, id) != query) {
  457. spin_unlock_irqrestore(&idr_lock, flags);
  458. return;
  459. }
  460. agent = query->port->agent;
  461. mad_buf = query->mad_buf;
  462. spin_unlock_irqrestore(&idr_lock, flags);
  463. ib_cancel_mad(agent, mad_buf);
  464. }
  465. EXPORT_SYMBOL(ib_sa_cancel_query);
  466. static u8 get_src_path_mask(struct ib_device *device, u8 port_num)
  467. {
  468. struct ib_sa_device *sa_dev;
  469. struct ib_sa_port *port;
  470. unsigned long flags;
  471. u8 src_path_mask;
  472. sa_dev = ib_get_client_data(device, &sa_client);
  473. if (!sa_dev)
  474. return 0x7f;
  475. port = &sa_dev->port[port_num - sa_dev->start_port];
  476. spin_lock_irqsave(&port->ah_lock, flags);
  477. src_path_mask = port->sm_ah ? port->sm_ah->src_path_mask : 0x7f;
  478. spin_unlock_irqrestore(&port->ah_lock, flags);
  479. return src_path_mask;
  480. }
  481. int ib_init_ah_from_path(struct ib_device *device, u8 port_num,
  482. struct ib_sa_path_rec *rec, struct ib_ah_attr *ah_attr)
  483. {
  484. int ret;
  485. u16 gid_index;
  486. int force_grh;
  487. memset(ah_attr, 0, sizeof *ah_attr);
  488. ah_attr->dlid = be16_to_cpu(rec->dlid);
  489. ah_attr->sl = rec->sl;
  490. ah_attr->src_path_bits = be16_to_cpu(rec->slid) &
  491. get_src_path_mask(device, port_num);
  492. ah_attr->port_num = port_num;
  493. ah_attr->static_rate = rec->rate;
  494. force_grh = rdma_cap_eth_ah(device, port_num);
  495. if (rec->hop_limit > 1 || force_grh) {
  496. ah_attr->ah_flags = IB_AH_GRH;
  497. ah_attr->grh.dgid = rec->dgid;
  498. ret = ib_find_cached_gid(device, &rec->sgid, &port_num,
  499. &gid_index);
  500. if (ret)
  501. return ret;
  502. ah_attr->grh.sgid_index = gid_index;
  503. ah_attr->grh.flow_label = be32_to_cpu(rec->flow_label);
  504. ah_attr->grh.hop_limit = rec->hop_limit;
  505. ah_attr->grh.traffic_class = rec->traffic_class;
  506. }
  507. if (force_grh) {
  508. memcpy(ah_attr->dmac, rec->dmac, ETH_ALEN);
  509. ah_attr->vlan_id = rec->vlan_id;
  510. } else {
  511. ah_attr->vlan_id = 0xffff;
  512. }
  513. return 0;
  514. }
  515. EXPORT_SYMBOL(ib_init_ah_from_path);
  516. static int alloc_mad(struct ib_sa_query *query, gfp_t gfp_mask)
  517. {
  518. unsigned long flags;
  519. spin_lock_irqsave(&query->port->ah_lock, flags);
  520. if (!query->port->sm_ah) {
  521. spin_unlock_irqrestore(&query->port->ah_lock, flags);
  522. return -EAGAIN;
  523. }
  524. kref_get(&query->port->sm_ah->ref);
  525. query->sm_ah = query->port->sm_ah;
  526. spin_unlock_irqrestore(&query->port->ah_lock, flags);
  527. query->mad_buf = ib_create_send_mad(query->port->agent, 1,
  528. query->sm_ah->pkey_index,
  529. 0, IB_MGMT_SA_HDR, IB_MGMT_SA_DATA,
  530. gfp_mask,
  531. IB_MGMT_BASE_VERSION);
  532. if (IS_ERR(query->mad_buf)) {
  533. kref_put(&query->sm_ah->ref, free_sm_ah);
  534. return -ENOMEM;
  535. }
  536. query->mad_buf->ah = query->sm_ah->ah;
  537. return 0;
  538. }
  539. static void free_mad(struct ib_sa_query *query)
  540. {
  541. ib_free_send_mad(query->mad_buf);
  542. kref_put(&query->sm_ah->ref, free_sm_ah);
  543. }
  544. static void init_mad(struct ib_sa_mad *mad, struct ib_mad_agent *agent)
  545. {
  546. unsigned long flags;
  547. memset(mad, 0, sizeof *mad);
  548. mad->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
  549. mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  550. mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
  551. spin_lock_irqsave(&tid_lock, flags);
  552. mad->mad_hdr.tid =
  553. cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
  554. spin_unlock_irqrestore(&tid_lock, flags);
  555. }
  556. static int send_mad(struct ib_sa_query *query, int timeout_ms, gfp_t gfp_mask)
  557. {
  558. bool preload = !!(gfp_mask & __GFP_WAIT);
  559. unsigned long flags;
  560. int ret, id;
  561. if (preload)
  562. idr_preload(gfp_mask);
  563. spin_lock_irqsave(&idr_lock, flags);
  564. id = idr_alloc(&query_idr, query, 0, 0, GFP_NOWAIT);
  565. spin_unlock_irqrestore(&idr_lock, flags);
  566. if (preload)
  567. idr_preload_end();
  568. if (id < 0)
  569. return id;
  570. query->mad_buf->timeout_ms = timeout_ms;
  571. query->mad_buf->context[0] = query;
  572. query->id = id;
  573. ret = ib_post_send_mad(query->mad_buf, NULL);
  574. if (ret) {
  575. spin_lock_irqsave(&idr_lock, flags);
  576. idr_remove(&query_idr, id);
  577. spin_unlock_irqrestore(&idr_lock, flags);
  578. }
  579. /*
  580. * It's not safe to dereference query any more, because the
  581. * send may already have completed and freed the query in
  582. * another context.
  583. */
  584. return ret ? ret : id;
  585. }
  586. void ib_sa_unpack_path(void *attribute, struct ib_sa_path_rec *rec)
  587. {
  588. ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table), attribute, rec);
  589. }
  590. EXPORT_SYMBOL(ib_sa_unpack_path);
  591. void ib_sa_pack_path(struct ib_sa_path_rec *rec, void *attribute)
  592. {
  593. ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, attribute);
  594. }
  595. EXPORT_SYMBOL(ib_sa_pack_path);
  596. static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
  597. int status,
  598. struct ib_sa_mad *mad)
  599. {
  600. struct ib_sa_path_query *query =
  601. container_of(sa_query, struct ib_sa_path_query, sa_query);
  602. if (mad) {
  603. struct ib_sa_path_rec rec;
  604. ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table),
  605. mad->data, &rec);
  606. rec.vlan_id = 0xffff;
  607. memset(rec.dmac, 0, ETH_ALEN);
  608. memset(rec.smac, 0, ETH_ALEN);
  609. query->callback(status, &rec, query->context);
  610. } else
  611. query->callback(status, NULL, query->context);
  612. }
  613. static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
  614. {
  615. kfree(container_of(sa_query, struct ib_sa_path_query, sa_query));
  616. }
  617. /**
  618. * ib_sa_path_rec_get - Start a Path get query
  619. * @client:SA client
  620. * @device:device to send query on
  621. * @port_num: port number to send query on
  622. * @rec:Path Record to send in query
  623. * @comp_mask:component mask to send in query
  624. * @timeout_ms:time to wait for response
  625. * @gfp_mask:GFP mask to use for internal allocations
  626. * @callback:function called when query completes, times out or is
  627. * canceled
  628. * @context:opaque user context passed to callback
  629. * @sa_query:query context, used to cancel query
  630. *
  631. * Send a Path Record Get query to the SA to look up a path. The
  632. * callback function will be called when the query completes (or
  633. * fails); status is 0 for a successful response, -EINTR if the query
  634. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  635. * occurred sending the query. The resp parameter of the callback is
  636. * only valid if status is 0.
  637. *
  638. * If the return value of ib_sa_path_rec_get() is negative, it is an
  639. * error code. Otherwise it is a query ID that can be used to cancel
  640. * the query.
  641. */
  642. int ib_sa_path_rec_get(struct ib_sa_client *client,
  643. struct ib_device *device, u8 port_num,
  644. struct ib_sa_path_rec *rec,
  645. ib_sa_comp_mask comp_mask,
  646. int timeout_ms, gfp_t gfp_mask,
  647. void (*callback)(int status,
  648. struct ib_sa_path_rec *resp,
  649. void *context),
  650. void *context,
  651. struct ib_sa_query **sa_query)
  652. {
  653. struct ib_sa_path_query *query;
  654. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  655. struct ib_sa_port *port;
  656. struct ib_mad_agent *agent;
  657. struct ib_sa_mad *mad;
  658. int ret;
  659. if (!sa_dev)
  660. return -ENODEV;
  661. port = &sa_dev->port[port_num - sa_dev->start_port];
  662. agent = port->agent;
  663. query = kmalloc(sizeof *query, gfp_mask);
  664. if (!query)
  665. return -ENOMEM;
  666. query->sa_query.port = port;
  667. ret = alloc_mad(&query->sa_query, gfp_mask);
  668. if (ret)
  669. goto err1;
  670. ib_sa_client_get(client);
  671. query->sa_query.client = client;
  672. query->callback = callback;
  673. query->context = context;
  674. mad = query->sa_query.mad_buf->mad;
  675. init_mad(mad, agent);
  676. query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL;
  677. query->sa_query.release = ib_sa_path_rec_release;
  678. mad->mad_hdr.method = IB_MGMT_METHOD_GET;
  679. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_PATH_REC);
  680. mad->sa_hdr.comp_mask = comp_mask;
  681. ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, mad->data);
  682. *sa_query = &query->sa_query;
  683. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  684. if (ret < 0)
  685. goto err2;
  686. return ret;
  687. err2:
  688. *sa_query = NULL;
  689. ib_sa_client_put(query->sa_query.client);
  690. free_mad(&query->sa_query);
  691. err1:
  692. kfree(query);
  693. return ret;
  694. }
  695. EXPORT_SYMBOL(ib_sa_path_rec_get);
  696. static void ib_sa_service_rec_callback(struct ib_sa_query *sa_query,
  697. int status,
  698. struct ib_sa_mad *mad)
  699. {
  700. struct ib_sa_service_query *query =
  701. container_of(sa_query, struct ib_sa_service_query, sa_query);
  702. if (mad) {
  703. struct ib_sa_service_rec rec;
  704. ib_unpack(service_rec_table, ARRAY_SIZE(service_rec_table),
  705. mad->data, &rec);
  706. query->callback(status, &rec, query->context);
  707. } else
  708. query->callback(status, NULL, query->context);
  709. }
  710. static void ib_sa_service_rec_release(struct ib_sa_query *sa_query)
  711. {
  712. kfree(container_of(sa_query, struct ib_sa_service_query, sa_query));
  713. }
  714. /**
  715. * ib_sa_service_rec_query - Start Service Record operation
  716. * @client:SA client
  717. * @device:device to send request on
  718. * @port_num: port number to send request on
  719. * @method:SA method - should be get, set, or delete
  720. * @rec:Service Record to send in request
  721. * @comp_mask:component mask to send in request
  722. * @timeout_ms:time to wait for response
  723. * @gfp_mask:GFP mask to use for internal allocations
  724. * @callback:function called when request completes, times out or is
  725. * canceled
  726. * @context:opaque user context passed to callback
  727. * @sa_query:request context, used to cancel request
  728. *
  729. * Send a Service Record set/get/delete to the SA to register,
  730. * unregister or query a service record.
  731. * The callback function will be called when the request completes (or
  732. * fails); status is 0 for a successful response, -EINTR if the query
  733. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  734. * occurred sending the query. The resp parameter of the callback is
  735. * only valid if status is 0.
  736. *
  737. * If the return value of ib_sa_service_rec_query() is negative, it is an
  738. * error code. Otherwise it is a request ID that can be used to cancel
  739. * the query.
  740. */
  741. int ib_sa_service_rec_query(struct ib_sa_client *client,
  742. struct ib_device *device, u8 port_num, u8 method,
  743. struct ib_sa_service_rec *rec,
  744. ib_sa_comp_mask comp_mask,
  745. int timeout_ms, gfp_t gfp_mask,
  746. void (*callback)(int status,
  747. struct ib_sa_service_rec *resp,
  748. void *context),
  749. void *context,
  750. struct ib_sa_query **sa_query)
  751. {
  752. struct ib_sa_service_query *query;
  753. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  754. struct ib_sa_port *port;
  755. struct ib_mad_agent *agent;
  756. struct ib_sa_mad *mad;
  757. int ret;
  758. if (!sa_dev)
  759. return -ENODEV;
  760. port = &sa_dev->port[port_num - sa_dev->start_port];
  761. agent = port->agent;
  762. if (method != IB_MGMT_METHOD_GET &&
  763. method != IB_MGMT_METHOD_SET &&
  764. method != IB_SA_METHOD_DELETE)
  765. return -EINVAL;
  766. query = kmalloc(sizeof *query, gfp_mask);
  767. if (!query)
  768. return -ENOMEM;
  769. query->sa_query.port = port;
  770. ret = alloc_mad(&query->sa_query, gfp_mask);
  771. if (ret)
  772. goto err1;
  773. ib_sa_client_get(client);
  774. query->sa_query.client = client;
  775. query->callback = callback;
  776. query->context = context;
  777. mad = query->sa_query.mad_buf->mad;
  778. init_mad(mad, agent);
  779. query->sa_query.callback = callback ? ib_sa_service_rec_callback : NULL;
  780. query->sa_query.release = ib_sa_service_rec_release;
  781. mad->mad_hdr.method = method;
  782. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_SERVICE_REC);
  783. mad->sa_hdr.comp_mask = comp_mask;
  784. ib_pack(service_rec_table, ARRAY_SIZE(service_rec_table),
  785. rec, mad->data);
  786. *sa_query = &query->sa_query;
  787. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  788. if (ret < 0)
  789. goto err2;
  790. return ret;
  791. err2:
  792. *sa_query = NULL;
  793. ib_sa_client_put(query->sa_query.client);
  794. free_mad(&query->sa_query);
  795. err1:
  796. kfree(query);
  797. return ret;
  798. }
  799. EXPORT_SYMBOL(ib_sa_service_rec_query);
  800. static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
  801. int status,
  802. struct ib_sa_mad *mad)
  803. {
  804. struct ib_sa_mcmember_query *query =
  805. container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
  806. if (mad) {
  807. struct ib_sa_mcmember_rec rec;
  808. ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  809. mad->data, &rec);
  810. query->callback(status, &rec, query->context);
  811. } else
  812. query->callback(status, NULL, query->context);
  813. }
  814. static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
  815. {
  816. kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
  817. }
  818. int ib_sa_mcmember_rec_query(struct ib_sa_client *client,
  819. struct ib_device *device, u8 port_num,
  820. u8 method,
  821. struct ib_sa_mcmember_rec *rec,
  822. ib_sa_comp_mask comp_mask,
  823. int timeout_ms, gfp_t gfp_mask,
  824. void (*callback)(int status,
  825. struct ib_sa_mcmember_rec *resp,
  826. void *context),
  827. void *context,
  828. struct ib_sa_query **sa_query)
  829. {
  830. struct ib_sa_mcmember_query *query;
  831. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  832. struct ib_sa_port *port;
  833. struct ib_mad_agent *agent;
  834. struct ib_sa_mad *mad;
  835. int ret;
  836. if (!sa_dev)
  837. return -ENODEV;
  838. port = &sa_dev->port[port_num - sa_dev->start_port];
  839. agent = port->agent;
  840. query = kmalloc(sizeof *query, gfp_mask);
  841. if (!query)
  842. return -ENOMEM;
  843. query->sa_query.port = port;
  844. ret = alloc_mad(&query->sa_query, gfp_mask);
  845. if (ret)
  846. goto err1;
  847. ib_sa_client_get(client);
  848. query->sa_query.client = client;
  849. query->callback = callback;
  850. query->context = context;
  851. mad = query->sa_query.mad_buf->mad;
  852. init_mad(mad, agent);
  853. query->sa_query.callback = callback ? ib_sa_mcmember_rec_callback : NULL;
  854. query->sa_query.release = ib_sa_mcmember_rec_release;
  855. mad->mad_hdr.method = method;
  856. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
  857. mad->sa_hdr.comp_mask = comp_mask;
  858. ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  859. rec, mad->data);
  860. *sa_query = &query->sa_query;
  861. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  862. if (ret < 0)
  863. goto err2;
  864. return ret;
  865. err2:
  866. *sa_query = NULL;
  867. ib_sa_client_put(query->sa_query.client);
  868. free_mad(&query->sa_query);
  869. err1:
  870. kfree(query);
  871. return ret;
  872. }
  873. /* Support GuidInfoRecord */
  874. static void ib_sa_guidinfo_rec_callback(struct ib_sa_query *sa_query,
  875. int status,
  876. struct ib_sa_mad *mad)
  877. {
  878. struct ib_sa_guidinfo_query *query =
  879. container_of(sa_query, struct ib_sa_guidinfo_query, sa_query);
  880. if (mad) {
  881. struct ib_sa_guidinfo_rec rec;
  882. ib_unpack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table),
  883. mad->data, &rec);
  884. query->callback(status, &rec, query->context);
  885. } else
  886. query->callback(status, NULL, query->context);
  887. }
  888. static void ib_sa_guidinfo_rec_release(struct ib_sa_query *sa_query)
  889. {
  890. kfree(container_of(sa_query, struct ib_sa_guidinfo_query, sa_query));
  891. }
  892. int ib_sa_guid_info_rec_query(struct ib_sa_client *client,
  893. struct ib_device *device, u8 port_num,
  894. struct ib_sa_guidinfo_rec *rec,
  895. ib_sa_comp_mask comp_mask, u8 method,
  896. int timeout_ms, gfp_t gfp_mask,
  897. void (*callback)(int status,
  898. struct ib_sa_guidinfo_rec *resp,
  899. void *context),
  900. void *context,
  901. struct ib_sa_query **sa_query)
  902. {
  903. struct ib_sa_guidinfo_query *query;
  904. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  905. struct ib_sa_port *port;
  906. struct ib_mad_agent *agent;
  907. struct ib_sa_mad *mad;
  908. int ret;
  909. if (!sa_dev)
  910. return -ENODEV;
  911. if (method != IB_MGMT_METHOD_GET &&
  912. method != IB_MGMT_METHOD_SET &&
  913. method != IB_SA_METHOD_DELETE) {
  914. return -EINVAL;
  915. }
  916. port = &sa_dev->port[port_num - sa_dev->start_port];
  917. agent = port->agent;
  918. query = kmalloc(sizeof *query, gfp_mask);
  919. if (!query)
  920. return -ENOMEM;
  921. query->sa_query.port = port;
  922. ret = alloc_mad(&query->sa_query, gfp_mask);
  923. if (ret)
  924. goto err1;
  925. ib_sa_client_get(client);
  926. query->sa_query.client = client;
  927. query->callback = callback;
  928. query->context = context;
  929. mad = query->sa_query.mad_buf->mad;
  930. init_mad(mad, agent);
  931. query->sa_query.callback = callback ? ib_sa_guidinfo_rec_callback : NULL;
  932. query->sa_query.release = ib_sa_guidinfo_rec_release;
  933. mad->mad_hdr.method = method;
  934. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_GUID_INFO_REC);
  935. mad->sa_hdr.comp_mask = comp_mask;
  936. ib_pack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table), rec,
  937. mad->data);
  938. *sa_query = &query->sa_query;
  939. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  940. if (ret < 0)
  941. goto err2;
  942. return ret;
  943. err2:
  944. *sa_query = NULL;
  945. ib_sa_client_put(query->sa_query.client);
  946. free_mad(&query->sa_query);
  947. err1:
  948. kfree(query);
  949. return ret;
  950. }
  951. EXPORT_SYMBOL(ib_sa_guid_info_rec_query);
  952. static void send_handler(struct ib_mad_agent *agent,
  953. struct ib_mad_send_wc *mad_send_wc)
  954. {
  955. struct ib_sa_query *query = mad_send_wc->send_buf->context[0];
  956. unsigned long flags;
  957. if (query->callback)
  958. switch (mad_send_wc->status) {
  959. case IB_WC_SUCCESS:
  960. /* No callback -- already got recv */
  961. break;
  962. case IB_WC_RESP_TIMEOUT_ERR:
  963. query->callback(query, -ETIMEDOUT, NULL);
  964. break;
  965. case IB_WC_WR_FLUSH_ERR:
  966. query->callback(query, -EINTR, NULL);
  967. break;
  968. default:
  969. query->callback(query, -EIO, NULL);
  970. break;
  971. }
  972. spin_lock_irqsave(&idr_lock, flags);
  973. idr_remove(&query_idr, query->id);
  974. spin_unlock_irqrestore(&idr_lock, flags);
  975. free_mad(query);
  976. ib_sa_client_put(query->client);
  977. query->release(query);
  978. }
  979. static void recv_handler(struct ib_mad_agent *mad_agent,
  980. struct ib_mad_recv_wc *mad_recv_wc)
  981. {
  982. struct ib_sa_query *query;
  983. struct ib_mad_send_buf *mad_buf;
  984. mad_buf = (void *) (unsigned long) mad_recv_wc->wc->wr_id;
  985. query = mad_buf->context[0];
  986. if (query->callback) {
  987. if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
  988. query->callback(query,
  989. mad_recv_wc->recv_buf.mad->mad_hdr.status ?
  990. -EINVAL : 0,
  991. (struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
  992. else
  993. query->callback(query, -EIO, NULL);
  994. }
  995. ib_free_recv_mad(mad_recv_wc);
  996. }
  997. static void ib_sa_add_one(struct ib_device *device)
  998. {
  999. struct ib_sa_device *sa_dev;
  1000. int s, e, i;
  1001. int count = 0;
  1002. if (device->node_type == RDMA_NODE_IB_SWITCH)
  1003. s = e = 0;
  1004. else {
  1005. s = 1;
  1006. e = device->phys_port_cnt;
  1007. }
  1008. sa_dev = kzalloc(sizeof *sa_dev +
  1009. (e - s + 1) * sizeof (struct ib_sa_port),
  1010. GFP_KERNEL);
  1011. if (!sa_dev)
  1012. return;
  1013. sa_dev->start_port = s;
  1014. sa_dev->end_port = e;
  1015. for (i = 0; i <= e - s; ++i) {
  1016. spin_lock_init(&sa_dev->port[i].ah_lock);
  1017. if (!rdma_cap_ib_sa(device, i + 1))
  1018. continue;
  1019. sa_dev->port[i].sm_ah = NULL;
  1020. sa_dev->port[i].port_num = i + s;
  1021. sa_dev->port[i].agent =
  1022. ib_register_mad_agent(device, i + s, IB_QPT_GSI,
  1023. NULL, 0, send_handler,
  1024. recv_handler, sa_dev, 0);
  1025. if (IS_ERR(sa_dev->port[i].agent))
  1026. goto err;
  1027. INIT_WORK(&sa_dev->port[i].update_task, update_sm_ah);
  1028. count++;
  1029. }
  1030. if (!count)
  1031. goto free;
  1032. ib_set_client_data(device, &sa_client, sa_dev);
  1033. /*
  1034. * We register our event handler after everything is set up,
  1035. * and then update our cached info after the event handler is
  1036. * registered to avoid any problems if a port changes state
  1037. * during our initialization.
  1038. */
  1039. INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
  1040. if (ib_register_event_handler(&sa_dev->event_handler))
  1041. goto err;
  1042. for (i = 0; i <= e - s; ++i) {
  1043. if (rdma_cap_ib_sa(device, i + 1))
  1044. update_sm_ah(&sa_dev->port[i].update_task);
  1045. }
  1046. return;
  1047. err:
  1048. while (--i >= 0) {
  1049. if (rdma_cap_ib_sa(device, i + 1))
  1050. ib_unregister_mad_agent(sa_dev->port[i].agent);
  1051. }
  1052. free:
  1053. kfree(sa_dev);
  1054. return;
  1055. }
  1056. static void ib_sa_remove_one(struct ib_device *device)
  1057. {
  1058. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  1059. int i;
  1060. if (!sa_dev)
  1061. return;
  1062. ib_unregister_event_handler(&sa_dev->event_handler);
  1063. flush_workqueue(ib_wq);
  1064. for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
  1065. if (rdma_cap_ib_sa(device, i + 1)) {
  1066. ib_unregister_mad_agent(sa_dev->port[i].agent);
  1067. if (sa_dev->port[i].sm_ah)
  1068. kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
  1069. }
  1070. }
  1071. kfree(sa_dev);
  1072. }
  1073. static int __init ib_sa_init(void)
  1074. {
  1075. int ret;
  1076. get_random_bytes(&tid, sizeof tid);
  1077. ret = ib_register_client(&sa_client);
  1078. if (ret) {
  1079. printk(KERN_ERR "Couldn't register ib_sa client\n");
  1080. goto err1;
  1081. }
  1082. ret = mcast_init();
  1083. if (ret) {
  1084. printk(KERN_ERR "Couldn't initialize multicast handling\n");
  1085. goto err2;
  1086. }
  1087. return 0;
  1088. err2:
  1089. ib_unregister_client(&sa_client);
  1090. err1:
  1091. return ret;
  1092. }
  1093. static void __exit ib_sa_cleanup(void)
  1094. {
  1095. mcast_cleanup();
  1096. ib_unregister_client(&sa_client);
  1097. idr_destroy(&query_idr);
  1098. }
  1099. module_init(ib_sa_init);
  1100. module_exit(ib_sa_cleanup);