sa_query.c 29 KB

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