sa_query.c 29 KB

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