sa_query.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  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_port_get_link_layer(handler->device, port->port_num) != IB_LINK_LAYER_INFINIBAND)
  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_port_get_link_layer(device, port_num) == IB_LINK_LAYER_ETHERNET;
  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. if (IS_ERR(query->mad_buf)) {
  532. kref_put(&query->sm_ah->ref, free_sm_ah);
  533. return -ENOMEM;
  534. }
  535. query->mad_buf->ah = query->sm_ah->ah;
  536. return 0;
  537. }
  538. static void free_mad(struct ib_sa_query *query)
  539. {
  540. ib_free_send_mad(query->mad_buf);
  541. kref_put(&query->sm_ah->ref, free_sm_ah);
  542. }
  543. static void init_mad(struct ib_sa_mad *mad, struct ib_mad_agent *agent)
  544. {
  545. unsigned long flags;
  546. memset(mad, 0, sizeof *mad);
  547. mad->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
  548. mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  549. mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
  550. spin_lock_irqsave(&tid_lock, flags);
  551. mad->mad_hdr.tid =
  552. cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
  553. spin_unlock_irqrestore(&tid_lock, flags);
  554. }
  555. static int send_mad(struct ib_sa_query *query, int timeout_ms, gfp_t gfp_mask)
  556. {
  557. bool preload = !!(gfp_mask & __GFP_WAIT);
  558. unsigned long flags;
  559. int ret, id;
  560. if (preload)
  561. idr_preload(gfp_mask);
  562. spin_lock_irqsave(&idr_lock, flags);
  563. id = idr_alloc(&query_idr, query, 0, 0, GFP_NOWAIT);
  564. spin_unlock_irqrestore(&idr_lock, flags);
  565. if (preload)
  566. idr_preload_end();
  567. if (id < 0)
  568. return id;
  569. query->mad_buf->timeout_ms = timeout_ms;
  570. query->mad_buf->context[0] = query;
  571. query->id = id;
  572. ret = ib_post_send_mad(query->mad_buf, NULL);
  573. if (ret) {
  574. spin_lock_irqsave(&idr_lock, flags);
  575. idr_remove(&query_idr, id);
  576. spin_unlock_irqrestore(&idr_lock, flags);
  577. }
  578. /*
  579. * It's not safe to dereference query any more, because the
  580. * send may already have completed and freed the query in
  581. * another context.
  582. */
  583. return ret ? ret : id;
  584. }
  585. void ib_sa_unpack_path(void *attribute, struct ib_sa_path_rec *rec)
  586. {
  587. ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table), attribute, rec);
  588. }
  589. EXPORT_SYMBOL(ib_sa_unpack_path);
  590. void ib_sa_pack_path(struct ib_sa_path_rec *rec, void *attribute)
  591. {
  592. ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, attribute);
  593. }
  594. EXPORT_SYMBOL(ib_sa_pack_path);
  595. static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
  596. int status,
  597. struct ib_sa_mad *mad)
  598. {
  599. struct ib_sa_path_query *query =
  600. container_of(sa_query, struct ib_sa_path_query, sa_query);
  601. if (mad) {
  602. struct ib_sa_path_rec rec;
  603. ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table),
  604. mad->data, &rec);
  605. rec.vlan_id = 0xffff;
  606. memset(rec.dmac, 0, ETH_ALEN);
  607. memset(rec.smac, 0, ETH_ALEN);
  608. query->callback(status, &rec, query->context);
  609. } else
  610. query->callback(status, NULL, query->context);
  611. }
  612. static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
  613. {
  614. kfree(container_of(sa_query, struct ib_sa_path_query, sa_query));
  615. }
  616. /**
  617. * ib_sa_path_rec_get - Start a Path get query
  618. * @client:SA client
  619. * @device:device to send query on
  620. * @port_num: port number to send query on
  621. * @rec:Path Record to send in query
  622. * @comp_mask:component mask to send in query
  623. * @timeout_ms:time to wait for response
  624. * @gfp_mask:GFP mask to use for internal allocations
  625. * @callback:function called when query completes, times out or is
  626. * canceled
  627. * @context:opaque user context passed to callback
  628. * @sa_query:query context, used to cancel query
  629. *
  630. * Send a Path Record Get query to the SA to look up a path. The
  631. * callback function will be called when the query completes (or
  632. * fails); status is 0 for a successful response, -EINTR if the query
  633. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  634. * occurred sending the query. The resp parameter of the callback is
  635. * only valid if status is 0.
  636. *
  637. * If the return value of ib_sa_path_rec_get() is negative, it is an
  638. * error code. Otherwise it is a query ID that can be used to cancel
  639. * the query.
  640. */
  641. int ib_sa_path_rec_get(struct ib_sa_client *client,
  642. struct ib_device *device, u8 port_num,
  643. struct ib_sa_path_rec *rec,
  644. ib_sa_comp_mask comp_mask,
  645. int timeout_ms, gfp_t gfp_mask,
  646. void (*callback)(int status,
  647. struct ib_sa_path_rec *resp,
  648. void *context),
  649. void *context,
  650. struct ib_sa_query **sa_query)
  651. {
  652. struct ib_sa_path_query *query;
  653. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  654. struct ib_sa_port *port;
  655. struct ib_mad_agent *agent;
  656. struct ib_sa_mad *mad;
  657. int ret;
  658. if (!sa_dev)
  659. return -ENODEV;
  660. port = &sa_dev->port[port_num - sa_dev->start_port];
  661. agent = port->agent;
  662. query = kmalloc(sizeof *query, gfp_mask);
  663. if (!query)
  664. return -ENOMEM;
  665. query->sa_query.port = port;
  666. ret = alloc_mad(&query->sa_query, gfp_mask);
  667. if (ret)
  668. goto err1;
  669. ib_sa_client_get(client);
  670. query->sa_query.client = client;
  671. query->callback = callback;
  672. query->context = context;
  673. mad = query->sa_query.mad_buf->mad;
  674. init_mad(mad, agent);
  675. query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL;
  676. query->sa_query.release = ib_sa_path_rec_release;
  677. mad->mad_hdr.method = IB_MGMT_METHOD_GET;
  678. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_PATH_REC);
  679. mad->sa_hdr.comp_mask = comp_mask;
  680. ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, mad->data);
  681. *sa_query = &query->sa_query;
  682. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  683. if (ret < 0)
  684. goto err2;
  685. return ret;
  686. err2:
  687. *sa_query = NULL;
  688. ib_sa_client_put(query->sa_query.client);
  689. free_mad(&query->sa_query);
  690. err1:
  691. kfree(query);
  692. return ret;
  693. }
  694. EXPORT_SYMBOL(ib_sa_path_rec_get);
  695. static void ib_sa_service_rec_callback(struct ib_sa_query *sa_query,
  696. int status,
  697. struct ib_sa_mad *mad)
  698. {
  699. struct ib_sa_service_query *query =
  700. container_of(sa_query, struct ib_sa_service_query, sa_query);
  701. if (mad) {
  702. struct ib_sa_service_rec rec;
  703. ib_unpack(service_rec_table, ARRAY_SIZE(service_rec_table),
  704. mad->data, &rec);
  705. query->callback(status, &rec, query->context);
  706. } else
  707. query->callback(status, NULL, query->context);
  708. }
  709. static void ib_sa_service_rec_release(struct ib_sa_query *sa_query)
  710. {
  711. kfree(container_of(sa_query, struct ib_sa_service_query, sa_query));
  712. }
  713. /**
  714. * ib_sa_service_rec_query - Start Service Record operation
  715. * @client:SA client
  716. * @device:device to send request on
  717. * @port_num: port number to send request on
  718. * @method:SA method - should be get, set, or delete
  719. * @rec:Service Record to send in request
  720. * @comp_mask:component mask to send in request
  721. * @timeout_ms:time to wait for response
  722. * @gfp_mask:GFP mask to use for internal allocations
  723. * @callback:function called when request completes, times out or is
  724. * canceled
  725. * @context:opaque user context passed to callback
  726. * @sa_query:request context, used to cancel request
  727. *
  728. * Send a Service Record set/get/delete to the SA to register,
  729. * unregister or query a service record.
  730. * The callback function will be called when the request completes (or
  731. * fails); status is 0 for a successful response, -EINTR if the query
  732. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  733. * occurred sending the query. The resp parameter of the callback is
  734. * only valid if status is 0.
  735. *
  736. * If the return value of ib_sa_service_rec_query() is negative, it is an
  737. * error code. Otherwise it is a request ID that can be used to cancel
  738. * the query.
  739. */
  740. int ib_sa_service_rec_query(struct ib_sa_client *client,
  741. struct ib_device *device, u8 port_num, u8 method,
  742. struct ib_sa_service_rec *rec,
  743. ib_sa_comp_mask comp_mask,
  744. int timeout_ms, gfp_t gfp_mask,
  745. void (*callback)(int status,
  746. struct ib_sa_service_rec *resp,
  747. void *context),
  748. void *context,
  749. struct ib_sa_query **sa_query)
  750. {
  751. struct ib_sa_service_query *query;
  752. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  753. struct ib_sa_port *port;
  754. struct ib_mad_agent *agent;
  755. struct ib_sa_mad *mad;
  756. int ret;
  757. if (!sa_dev)
  758. return -ENODEV;
  759. port = &sa_dev->port[port_num - sa_dev->start_port];
  760. agent = port->agent;
  761. if (method != IB_MGMT_METHOD_GET &&
  762. method != IB_MGMT_METHOD_SET &&
  763. method != IB_SA_METHOD_DELETE)
  764. return -EINVAL;
  765. query = kmalloc(sizeof *query, gfp_mask);
  766. if (!query)
  767. return -ENOMEM;
  768. query->sa_query.port = port;
  769. ret = alloc_mad(&query->sa_query, gfp_mask);
  770. if (ret)
  771. goto err1;
  772. ib_sa_client_get(client);
  773. query->sa_query.client = client;
  774. query->callback = callback;
  775. query->context = context;
  776. mad = query->sa_query.mad_buf->mad;
  777. init_mad(mad, agent);
  778. query->sa_query.callback = callback ? ib_sa_service_rec_callback : NULL;
  779. query->sa_query.release = ib_sa_service_rec_release;
  780. mad->mad_hdr.method = method;
  781. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_SERVICE_REC);
  782. mad->sa_hdr.comp_mask = comp_mask;
  783. ib_pack(service_rec_table, ARRAY_SIZE(service_rec_table),
  784. rec, mad->data);
  785. *sa_query = &query->sa_query;
  786. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  787. if (ret < 0)
  788. goto err2;
  789. return ret;
  790. err2:
  791. *sa_query = NULL;
  792. ib_sa_client_put(query->sa_query.client);
  793. free_mad(&query->sa_query);
  794. err1:
  795. kfree(query);
  796. return ret;
  797. }
  798. EXPORT_SYMBOL(ib_sa_service_rec_query);
  799. static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
  800. int status,
  801. struct ib_sa_mad *mad)
  802. {
  803. struct ib_sa_mcmember_query *query =
  804. container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
  805. if (mad) {
  806. struct ib_sa_mcmember_rec rec;
  807. ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  808. mad->data, &rec);
  809. query->callback(status, &rec, query->context);
  810. } else
  811. query->callback(status, NULL, query->context);
  812. }
  813. static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
  814. {
  815. kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
  816. }
  817. int ib_sa_mcmember_rec_query(struct ib_sa_client *client,
  818. struct ib_device *device, u8 port_num,
  819. u8 method,
  820. struct ib_sa_mcmember_rec *rec,
  821. ib_sa_comp_mask comp_mask,
  822. int timeout_ms, gfp_t gfp_mask,
  823. void (*callback)(int status,
  824. struct ib_sa_mcmember_rec *resp,
  825. void *context),
  826. void *context,
  827. struct ib_sa_query **sa_query)
  828. {
  829. struct ib_sa_mcmember_query *query;
  830. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  831. struct ib_sa_port *port;
  832. struct ib_mad_agent *agent;
  833. struct ib_sa_mad *mad;
  834. int ret;
  835. if (!sa_dev)
  836. return -ENODEV;
  837. port = &sa_dev->port[port_num - sa_dev->start_port];
  838. agent = port->agent;
  839. query = kmalloc(sizeof *query, gfp_mask);
  840. if (!query)
  841. return -ENOMEM;
  842. query->sa_query.port = port;
  843. ret = alloc_mad(&query->sa_query, gfp_mask);
  844. if (ret)
  845. goto err1;
  846. ib_sa_client_get(client);
  847. query->sa_query.client = client;
  848. query->callback = callback;
  849. query->context = context;
  850. mad = query->sa_query.mad_buf->mad;
  851. init_mad(mad, agent);
  852. query->sa_query.callback = callback ? ib_sa_mcmember_rec_callback : NULL;
  853. query->sa_query.release = ib_sa_mcmember_rec_release;
  854. mad->mad_hdr.method = method;
  855. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
  856. mad->sa_hdr.comp_mask = comp_mask;
  857. ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  858. rec, mad->data);
  859. *sa_query = &query->sa_query;
  860. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  861. if (ret < 0)
  862. goto err2;
  863. return ret;
  864. err2:
  865. *sa_query = NULL;
  866. ib_sa_client_put(query->sa_query.client);
  867. free_mad(&query->sa_query);
  868. err1:
  869. kfree(query);
  870. return ret;
  871. }
  872. /* Support GuidInfoRecord */
  873. static void ib_sa_guidinfo_rec_callback(struct ib_sa_query *sa_query,
  874. int status,
  875. struct ib_sa_mad *mad)
  876. {
  877. struct ib_sa_guidinfo_query *query =
  878. container_of(sa_query, struct ib_sa_guidinfo_query, sa_query);
  879. if (mad) {
  880. struct ib_sa_guidinfo_rec rec;
  881. ib_unpack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table),
  882. mad->data, &rec);
  883. query->callback(status, &rec, query->context);
  884. } else
  885. query->callback(status, NULL, query->context);
  886. }
  887. static void ib_sa_guidinfo_rec_release(struct ib_sa_query *sa_query)
  888. {
  889. kfree(container_of(sa_query, struct ib_sa_guidinfo_query, sa_query));
  890. }
  891. int ib_sa_guid_info_rec_query(struct ib_sa_client *client,
  892. struct ib_device *device, u8 port_num,
  893. struct ib_sa_guidinfo_rec *rec,
  894. ib_sa_comp_mask comp_mask, u8 method,
  895. int timeout_ms, gfp_t gfp_mask,
  896. void (*callback)(int status,
  897. struct ib_sa_guidinfo_rec *resp,
  898. void *context),
  899. void *context,
  900. struct ib_sa_query **sa_query)
  901. {
  902. struct ib_sa_guidinfo_query *query;
  903. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  904. struct ib_sa_port *port;
  905. struct ib_mad_agent *agent;
  906. struct ib_sa_mad *mad;
  907. int ret;
  908. if (!sa_dev)
  909. return -ENODEV;
  910. if (method != IB_MGMT_METHOD_GET &&
  911. method != IB_MGMT_METHOD_SET &&
  912. method != IB_SA_METHOD_DELETE) {
  913. return -EINVAL;
  914. }
  915. port = &sa_dev->port[port_num - sa_dev->start_port];
  916. agent = port->agent;
  917. query = kmalloc(sizeof *query, gfp_mask);
  918. if (!query)
  919. return -ENOMEM;
  920. query->sa_query.port = port;
  921. ret = alloc_mad(&query->sa_query, gfp_mask);
  922. if (ret)
  923. goto err1;
  924. ib_sa_client_get(client);
  925. query->sa_query.client = client;
  926. query->callback = callback;
  927. query->context = context;
  928. mad = query->sa_query.mad_buf->mad;
  929. init_mad(mad, agent);
  930. query->sa_query.callback = callback ? ib_sa_guidinfo_rec_callback : NULL;
  931. query->sa_query.release = ib_sa_guidinfo_rec_release;
  932. mad->mad_hdr.method = method;
  933. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_GUID_INFO_REC);
  934. mad->sa_hdr.comp_mask = comp_mask;
  935. ib_pack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table), rec,
  936. mad->data);
  937. *sa_query = &query->sa_query;
  938. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  939. if (ret < 0)
  940. goto err2;
  941. return ret;
  942. err2:
  943. *sa_query = NULL;
  944. ib_sa_client_put(query->sa_query.client);
  945. free_mad(&query->sa_query);
  946. err1:
  947. kfree(query);
  948. return ret;
  949. }
  950. EXPORT_SYMBOL(ib_sa_guid_info_rec_query);
  951. static void send_handler(struct ib_mad_agent *agent,
  952. struct ib_mad_send_wc *mad_send_wc)
  953. {
  954. struct ib_sa_query *query = mad_send_wc->send_buf->context[0];
  955. unsigned long flags;
  956. if (query->callback)
  957. switch (mad_send_wc->status) {
  958. case IB_WC_SUCCESS:
  959. /* No callback -- already got recv */
  960. break;
  961. case IB_WC_RESP_TIMEOUT_ERR:
  962. query->callback(query, -ETIMEDOUT, NULL);
  963. break;
  964. case IB_WC_WR_FLUSH_ERR:
  965. query->callback(query, -EINTR, NULL);
  966. break;
  967. default:
  968. query->callback(query, -EIO, NULL);
  969. break;
  970. }
  971. spin_lock_irqsave(&idr_lock, flags);
  972. idr_remove(&query_idr, query->id);
  973. spin_unlock_irqrestore(&idr_lock, flags);
  974. free_mad(query);
  975. ib_sa_client_put(query->client);
  976. query->release(query);
  977. }
  978. static void recv_handler(struct ib_mad_agent *mad_agent,
  979. struct ib_mad_recv_wc *mad_recv_wc)
  980. {
  981. struct ib_sa_query *query;
  982. struct ib_mad_send_buf *mad_buf;
  983. mad_buf = (void *) (unsigned long) mad_recv_wc->wc->wr_id;
  984. query = mad_buf->context[0];
  985. if (query->callback) {
  986. if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
  987. query->callback(query,
  988. mad_recv_wc->recv_buf.mad->mad_hdr.status ?
  989. -EINVAL : 0,
  990. (struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
  991. else
  992. query->callback(query, -EIO, NULL);
  993. }
  994. ib_free_recv_mad(mad_recv_wc);
  995. }
  996. static void ib_sa_add_one(struct ib_device *device)
  997. {
  998. struct ib_sa_device *sa_dev;
  999. int s, e, i;
  1000. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  1001. return;
  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_port_get_link_layer(device, i + 1) != IB_LINK_LAYER_INFINIBAND)
  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);
  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. }
  1029. ib_set_client_data(device, &sa_client, sa_dev);
  1030. /*
  1031. * We register our event handler after everything is set up,
  1032. * and then update our cached info after the event handler is
  1033. * registered to avoid any problems if a port changes state
  1034. * during our initialization.
  1035. */
  1036. INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
  1037. if (ib_register_event_handler(&sa_dev->event_handler))
  1038. goto err;
  1039. for (i = 0; i <= e - s; ++i)
  1040. if (rdma_port_get_link_layer(device, i + 1) == IB_LINK_LAYER_INFINIBAND)
  1041. update_sm_ah(&sa_dev->port[i].update_task);
  1042. return;
  1043. err:
  1044. while (--i >= 0)
  1045. if (rdma_port_get_link_layer(device, i + 1) == IB_LINK_LAYER_INFINIBAND)
  1046. ib_unregister_mad_agent(sa_dev->port[i].agent);
  1047. kfree(sa_dev);
  1048. return;
  1049. }
  1050. static void ib_sa_remove_one(struct ib_device *device)
  1051. {
  1052. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  1053. int i;
  1054. if (!sa_dev)
  1055. return;
  1056. ib_unregister_event_handler(&sa_dev->event_handler);
  1057. flush_workqueue(ib_wq);
  1058. for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
  1059. if (rdma_port_get_link_layer(device, i + 1) == IB_LINK_LAYER_INFINIBAND) {
  1060. ib_unregister_mad_agent(sa_dev->port[i].agent);
  1061. if (sa_dev->port[i].sm_ah)
  1062. kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
  1063. }
  1064. }
  1065. kfree(sa_dev);
  1066. }
  1067. static int __init ib_sa_init(void)
  1068. {
  1069. int ret;
  1070. get_random_bytes(&tid, sizeof tid);
  1071. ret = ib_register_client(&sa_client);
  1072. if (ret) {
  1073. printk(KERN_ERR "Couldn't register ib_sa client\n");
  1074. goto err1;
  1075. }
  1076. ret = mcast_init();
  1077. if (ret) {
  1078. printk(KERN_ERR "Couldn't initialize multicast handling\n");
  1079. goto err2;
  1080. }
  1081. return 0;
  1082. err2:
  1083. ib_unregister_client(&sa_client);
  1084. err1:
  1085. return ret;
  1086. }
  1087. static void __exit ib_sa_cleanup(void)
  1088. {
  1089. mcast_cleanup();
  1090. ib_unregister_client(&sa_client);
  1091. idr_destroy(&query_idr);
  1092. }
  1093. module_init(ib_sa_init);
  1094. module_exit(ib_sa_cleanup);