sa_query.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  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 <rdma/rdma_netlink.h>
  48. #include <net/netlink.h>
  49. #include <uapi/rdma/ib_user_sa.h>
  50. #include <rdma/ib_marshall.h>
  51. #include <rdma/ib_addr.h>
  52. #include "sa.h"
  53. #include "core_priv.h"
  54. MODULE_AUTHOR("Roland Dreier");
  55. MODULE_DESCRIPTION("InfiniBand subnet administration query support");
  56. MODULE_LICENSE("Dual BSD/GPL");
  57. #define IB_SA_LOCAL_SVC_TIMEOUT_MIN 100
  58. #define IB_SA_LOCAL_SVC_TIMEOUT_DEFAULT 2000
  59. #define IB_SA_LOCAL_SVC_TIMEOUT_MAX 200000
  60. static int sa_local_svc_timeout_ms = IB_SA_LOCAL_SVC_TIMEOUT_DEFAULT;
  61. struct ib_sa_sm_ah {
  62. struct ib_ah *ah;
  63. struct kref ref;
  64. u16 pkey_index;
  65. u8 src_path_mask;
  66. };
  67. struct ib_sa_port {
  68. struct ib_mad_agent *agent;
  69. struct ib_sa_sm_ah *sm_ah;
  70. struct work_struct update_task;
  71. spinlock_t ah_lock;
  72. u8 port_num;
  73. };
  74. struct ib_sa_device {
  75. int start_port, end_port;
  76. struct ib_event_handler event_handler;
  77. struct ib_sa_port port[0];
  78. };
  79. struct ib_sa_query {
  80. void (*callback)(struct ib_sa_query *, int, struct ib_sa_mad *);
  81. void (*release)(struct ib_sa_query *);
  82. struct ib_sa_client *client;
  83. struct ib_sa_port *port;
  84. struct ib_mad_send_buf *mad_buf;
  85. struct ib_sa_sm_ah *sm_ah;
  86. int id;
  87. u32 flags;
  88. struct list_head list; /* Local svc request list */
  89. u32 seq; /* Local svc request sequence number */
  90. unsigned long timeout; /* Local svc timeout */
  91. u8 path_use; /* How will the pathrecord be used */
  92. };
  93. #define IB_SA_ENABLE_LOCAL_SERVICE 0x00000001
  94. #define IB_SA_CANCEL 0x00000002
  95. struct ib_sa_service_query {
  96. void (*callback)(int, struct ib_sa_service_rec *, void *);
  97. void *context;
  98. struct ib_sa_query sa_query;
  99. };
  100. struct ib_sa_path_query {
  101. void (*callback)(int, struct ib_sa_path_rec *, void *);
  102. void *context;
  103. struct ib_sa_query sa_query;
  104. };
  105. struct ib_sa_guidinfo_query {
  106. void (*callback)(int, struct ib_sa_guidinfo_rec *, void *);
  107. void *context;
  108. struct ib_sa_query sa_query;
  109. };
  110. struct ib_sa_mcmember_query {
  111. void (*callback)(int, struct ib_sa_mcmember_rec *, void *);
  112. void *context;
  113. struct ib_sa_query sa_query;
  114. };
  115. static LIST_HEAD(ib_nl_request_list);
  116. static DEFINE_SPINLOCK(ib_nl_request_lock);
  117. static atomic_t ib_nl_sa_request_seq;
  118. static struct workqueue_struct *ib_nl_wq;
  119. static struct delayed_work ib_nl_timed_work;
  120. static const struct nla_policy ib_nl_policy[LS_NLA_TYPE_MAX] = {
  121. [LS_NLA_TYPE_PATH_RECORD] = {.type = NLA_BINARY,
  122. .len = sizeof(struct ib_path_rec_data)},
  123. [LS_NLA_TYPE_TIMEOUT] = {.type = NLA_U32},
  124. [LS_NLA_TYPE_SERVICE_ID] = {.type = NLA_U64},
  125. [LS_NLA_TYPE_DGID] = {.type = NLA_BINARY,
  126. .len = sizeof(struct rdma_nla_ls_gid)},
  127. [LS_NLA_TYPE_SGID] = {.type = NLA_BINARY,
  128. .len = sizeof(struct rdma_nla_ls_gid)},
  129. [LS_NLA_TYPE_TCLASS] = {.type = NLA_U8},
  130. [LS_NLA_TYPE_PKEY] = {.type = NLA_U16},
  131. [LS_NLA_TYPE_QOS_CLASS] = {.type = NLA_U16},
  132. };
  133. static void ib_sa_add_one(struct ib_device *device);
  134. static void ib_sa_remove_one(struct ib_device *device, void *client_data);
  135. static struct ib_client sa_client = {
  136. .name = "sa",
  137. .add = ib_sa_add_one,
  138. .remove = ib_sa_remove_one
  139. };
  140. static DEFINE_SPINLOCK(idr_lock);
  141. static DEFINE_IDR(query_idr);
  142. static DEFINE_SPINLOCK(tid_lock);
  143. static u32 tid;
  144. #define PATH_REC_FIELD(field) \
  145. .struct_offset_bytes = offsetof(struct ib_sa_path_rec, field), \
  146. .struct_size_bytes = sizeof ((struct ib_sa_path_rec *) 0)->field, \
  147. .field_name = "sa_path_rec:" #field
  148. static const struct ib_field path_rec_table[] = {
  149. { PATH_REC_FIELD(service_id),
  150. .offset_words = 0,
  151. .offset_bits = 0,
  152. .size_bits = 64 },
  153. { PATH_REC_FIELD(dgid),
  154. .offset_words = 2,
  155. .offset_bits = 0,
  156. .size_bits = 128 },
  157. { PATH_REC_FIELD(sgid),
  158. .offset_words = 6,
  159. .offset_bits = 0,
  160. .size_bits = 128 },
  161. { PATH_REC_FIELD(dlid),
  162. .offset_words = 10,
  163. .offset_bits = 0,
  164. .size_bits = 16 },
  165. { PATH_REC_FIELD(slid),
  166. .offset_words = 10,
  167. .offset_bits = 16,
  168. .size_bits = 16 },
  169. { PATH_REC_FIELD(raw_traffic),
  170. .offset_words = 11,
  171. .offset_bits = 0,
  172. .size_bits = 1 },
  173. { RESERVED,
  174. .offset_words = 11,
  175. .offset_bits = 1,
  176. .size_bits = 3 },
  177. { PATH_REC_FIELD(flow_label),
  178. .offset_words = 11,
  179. .offset_bits = 4,
  180. .size_bits = 20 },
  181. { PATH_REC_FIELD(hop_limit),
  182. .offset_words = 11,
  183. .offset_bits = 24,
  184. .size_bits = 8 },
  185. { PATH_REC_FIELD(traffic_class),
  186. .offset_words = 12,
  187. .offset_bits = 0,
  188. .size_bits = 8 },
  189. { PATH_REC_FIELD(reversible),
  190. .offset_words = 12,
  191. .offset_bits = 8,
  192. .size_bits = 1 },
  193. { PATH_REC_FIELD(numb_path),
  194. .offset_words = 12,
  195. .offset_bits = 9,
  196. .size_bits = 7 },
  197. { PATH_REC_FIELD(pkey),
  198. .offset_words = 12,
  199. .offset_bits = 16,
  200. .size_bits = 16 },
  201. { PATH_REC_FIELD(qos_class),
  202. .offset_words = 13,
  203. .offset_bits = 0,
  204. .size_bits = 12 },
  205. { PATH_REC_FIELD(sl),
  206. .offset_words = 13,
  207. .offset_bits = 12,
  208. .size_bits = 4 },
  209. { PATH_REC_FIELD(mtu_selector),
  210. .offset_words = 13,
  211. .offset_bits = 16,
  212. .size_bits = 2 },
  213. { PATH_REC_FIELD(mtu),
  214. .offset_words = 13,
  215. .offset_bits = 18,
  216. .size_bits = 6 },
  217. { PATH_REC_FIELD(rate_selector),
  218. .offset_words = 13,
  219. .offset_bits = 24,
  220. .size_bits = 2 },
  221. { PATH_REC_FIELD(rate),
  222. .offset_words = 13,
  223. .offset_bits = 26,
  224. .size_bits = 6 },
  225. { PATH_REC_FIELD(packet_life_time_selector),
  226. .offset_words = 14,
  227. .offset_bits = 0,
  228. .size_bits = 2 },
  229. { PATH_REC_FIELD(packet_life_time),
  230. .offset_words = 14,
  231. .offset_bits = 2,
  232. .size_bits = 6 },
  233. { PATH_REC_FIELD(preference),
  234. .offset_words = 14,
  235. .offset_bits = 8,
  236. .size_bits = 8 },
  237. { RESERVED,
  238. .offset_words = 14,
  239. .offset_bits = 16,
  240. .size_bits = 48 },
  241. };
  242. #define MCMEMBER_REC_FIELD(field) \
  243. .struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field), \
  244. .struct_size_bytes = sizeof ((struct ib_sa_mcmember_rec *) 0)->field, \
  245. .field_name = "sa_mcmember_rec:" #field
  246. static const struct ib_field mcmember_rec_table[] = {
  247. { MCMEMBER_REC_FIELD(mgid),
  248. .offset_words = 0,
  249. .offset_bits = 0,
  250. .size_bits = 128 },
  251. { MCMEMBER_REC_FIELD(port_gid),
  252. .offset_words = 4,
  253. .offset_bits = 0,
  254. .size_bits = 128 },
  255. { MCMEMBER_REC_FIELD(qkey),
  256. .offset_words = 8,
  257. .offset_bits = 0,
  258. .size_bits = 32 },
  259. { MCMEMBER_REC_FIELD(mlid),
  260. .offset_words = 9,
  261. .offset_bits = 0,
  262. .size_bits = 16 },
  263. { MCMEMBER_REC_FIELD(mtu_selector),
  264. .offset_words = 9,
  265. .offset_bits = 16,
  266. .size_bits = 2 },
  267. { MCMEMBER_REC_FIELD(mtu),
  268. .offset_words = 9,
  269. .offset_bits = 18,
  270. .size_bits = 6 },
  271. { MCMEMBER_REC_FIELD(traffic_class),
  272. .offset_words = 9,
  273. .offset_bits = 24,
  274. .size_bits = 8 },
  275. { MCMEMBER_REC_FIELD(pkey),
  276. .offset_words = 10,
  277. .offset_bits = 0,
  278. .size_bits = 16 },
  279. { MCMEMBER_REC_FIELD(rate_selector),
  280. .offset_words = 10,
  281. .offset_bits = 16,
  282. .size_bits = 2 },
  283. { MCMEMBER_REC_FIELD(rate),
  284. .offset_words = 10,
  285. .offset_bits = 18,
  286. .size_bits = 6 },
  287. { MCMEMBER_REC_FIELD(packet_life_time_selector),
  288. .offset_words = 10,
  289. .offset_bits = 24,
  290. .size_bits = 2 },
  291. { MCMEMBER_REC_FIELD(packet_life_time),
  292. .offset_words = 10,
  293. .offset_bits = 26,
  294. .size_bits = 6 },
  295. { MCMEMBER_REC_FIELD(sl),
  296. .offset_words = 11,
  297. .offset_bits = 0,
  298. .size_bits = 4 },
  299. { MCMEMBER_REC_FIELD(flow_label),
  300. .offset_words = 11,
  301. .offset_bits = 4,
  302. .size_bits = 20 },
  303. { MCMEMBER_REC_FIELD(hop_limit),
  304. .offset_words = 11,
  305. .offset_bits = 24,
  306. .size_bits = 8 },
  307. { MCMEMBER_REC_FIELD(scope),
  308. .offset_words = 12,
  309. .offset_bits = 0,
  310. .size_bits = 4 },
  311. { MCMEMBER_REC_FIELD(join_state),
  312. .offset_words = 12,
  313. .offset_bits = 4,
  314. .size_bits = 4 },
  315. { MCMEMBER_REC_FIELD(proxy_join),
  316. .offset_words = 12,
  317. .offset_bits = 8,
  318. .size_bits = 1 },
  319. { RESERVED,
  320. .offset_words = 12,
  321. .offset_bits = 9,
  322. .size_bits = 23 },
  323. };
  324. #define SERVICE_REC_FIELD(field) \
  325. .struct_offset_bytes = offsetof(struct ib_sa_service_rec, field), \
  326. .struct_size_bytes = sizeof ((struct ib_sa_service_rec *) 0)->field, \
  327. .field_name = "sa_service_rec:" #field
  328. static const struct ib_field service_rec_table[] = {
  329. { SERVICE_REC_FIELD(id),
  330. .offset_words = 0,
  331. .offset_bits = 0,
  332. .size_bits = 64 },
  333. { SERVICE_REC_FIELD(gid),
  334. .offset_words = 2,
  335. .offset_bits = 0,
  336. .size_bits = 128 },
  337. { SERVICE_REC_FIELD(pkey),
  338. .offset_words = 6,
  339. .offset_bits = 0,
  340. .size_bits = 16 },
  341. { SERVICE_REC_FIELD(lease),
  342. .offset_words = 7,
  343. .offset_bits = 0,
  344. .size_bits = 32 },
  345. { SERVICE_REC_FIELD(key),
  346. .offset_words = 8,
  347. .offset_bits = 0,
  348. .size_bits = 128 },
  349. { SERVICE_REC_FIELD(name),
  350. .offset_words = 12,
  351. .offset_bits = 0,
  352. .size_bits = 64*8 },
  353. { SERVICE_REC_FIELD(data8),
  354. .offset_words = 28,
  355. .offset_bits = 0,
  356. .size_bits = 16*8 },
  357. { SERVICE_REC_FIELD(data16),
  358. .offset_words = 32,
  359. .offset_bits = 0,
  360. .size_bits = 8*16 },
  361. { SERVICE_REC_FIELD(data32),
  362. .offset_words = 36,
  363. .offset_bits = 0,
  364. .size_bits = 4*32 },
  365. { SERVICE_REC_FIELD(data64),
  366. .offset_words = 40,
  367. .offset_bits = 0,
  368. .size_bits = 2*64 },
  369. };
  370. #define GUIDINFO_REC_FIELD(field) \
  371. .struct_offset_bytes = offsetof(struct ib_sa_guidinfo_rec, field), \
  372. .struct_size_bytes = sizeof((struct ib_sa_guidinfo_rec *) 0)->field, \
  373. .field_name = "sa_guidinfo_rec:" #field
  374. static const struct ib_field guidinfo_rec_table[] = {
  375. { GUIDINFO_REC_FIELD(lid),
  376. .offset_words = 0,
  377. .offset_bits = 0,
  378. .size_bits = 16 },
  379. { GUIDINFO_REC_FIELD(block_num),
  380. .offset_words = 0,
  381. .offset_bits = 16,
  382. .size_bits = 8 },
  383. { GUIDINFO_REC_FIELD(res1),
  384. .offset_words = 0,
  385. .offset_bits = 24,
  386. .size_bits = 8 },
  387. { GUIDINFO_REC_FIELD(res2),
  388. .offset_words = 1,
  389. .offset_bits = 0,
  390. .size_bits = 32 },
  391. { GUIDINFO_REC_FIELD(guid_info_list),
  392. .offset_words = 2,
  393. .offset_bits = 0,
  394. .size_bits = 512 },
  395. };
  396. static inline void ib_sa_disable_local_svc(struct ib_sa_query *query)
  397. {
  398. query->flags &= ~IB_SA_ENABLE_LOCAL_SERVICE;
  399. }
  400. static inline int ib_sa_query_cancelled(struct ib_sa_query *query)
  401. {
  402. return (query->flags & IB_SA_CANCEL);
  403. }
  404. static void ib_nl_set_path_rec_attrs(struct sk_buff *skb,
  405. struct ib_sa_query *query)
  406. {
  407. struct ib_sa_path_rec *sa_rec = query->mad_buf->context[1];
  408. struct ib_sa_mad *mad = query->mad_buf->mad;
  409. ib_sa_comp_mask comp_mask = mad->sa_hdr.comp_mask;
  410. u16 val16;
  411. u64 val64;
  412. struct rdma_ls_resolve_header *header;
  413. query->mad_buf->context[1] = NULL;
  414. /* Construct the family header first */
  415. header = (struct rdma_ls_resolve_header *)
  416. skb_put(skb, NLMSG_ALIGN(sizeof(*header)));
  417. memcpy(header->device_name, query->port->agent->device->name,
  418. LS_DEVICE_NAME_MAX);
  419. header->port_num = query->port->port_num;
  420. if ((comp_mask & IB_SA_PATH_REC_REVERSIBLE) &&
  421. sa_rec->reversible != 0)
  422. query->path_use = LS_RESOLVE_PATH_USE_GMP;
  423. else
  424. query->path_use = LS_RESOLVE_PATH_USE_UNIDIRECTIONAL;
  425. header->path_use = query->path_use;
  426. /* Now build the attributes */
  427. if (comp_mask & IB_SA_PATH_REC_SERVICE_ID) {
  428. val64 = be64_to_cpu(sa_rec->service_id);
  429. nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_SERVICE_ID,
  430. sizeof(val64), &val64);
  431. }
  432. if (comp_mask & IB_SA_PATH_REC_DGID)
  433. nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_DGID,
  434. sizeof(sa_rec->dgid), &sa_rec->dgid);
  435. if (comp_mask & IB_SA_PATH_REC_SGID)
  436. nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_SGID,
  437. sizeof(sa_rec->sgid), &sa_rec->sgid);
  438. if (comp_mask & IB_SA_PATH_REC_TRAFFIC_CLASS)
  439. nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_TCLASS,
  440. sizeof(sa_rec->traffic_class), &sa_rec->traffic_class);
  441. if (comp_mask & IB_SA_PATH_REC_PKEY) {
  442. val16 = be16_to_cpu(sa_rec->pkey);
  443. nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_PKEY,
  444. sizeof(val16), &val16);
  445. }
  446. if (comp_mask & IB_SA_PATH_REC_QOS_CLASS) {
  447. val16 = be16_to_cpu(sa_rec->qos_class);
  448. nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_QOS_CLASS,
  449. sizeof(val16), &val16);
  450. }
  451. }
  452. static int ib_nl_get_path_rec_attrs_len(ib_sa_comp_mask comp_mask)
  453. {
  454. int len = 0;
  455. if (comp_mask & IB_SA_PATH_REC_SERVICE_ID)
  456. len += nla_total_size(sizeof(u64));
  457. if (comp_mask & IB_SA_PATH_REC_DGID)
  458. len += nla_total_size(sizeof(struct rdma_nla_ls_gid));
  459. if (comp_mask & IB_SA_PATH_REC_SGID)
  460. len += nla_total_size(sizeof(struct rdma_nla_ls_gid));
  461. if (comp_mask & IB_SA_PATH_REC_TRAFFIC_CLASS)
  462. len += nla_total_size(sizeof(u8));
  463. if (comp_mask & IB_SA_PATH_REC_PKEY)
  464. len += nla_total_size(sizeof(u16));
  465. if (comp_mask & IB_SA_PATH_REC_QOS_CLASS)
  466. len += nla_total_size(sizeof(u16));
  467. /*
  468. * Make sure that at least some of the required comp_mask bits are
  469. * set.
  470. */
  471. if (WARN_ON(len == 0))
  472. return len;
  473. /* Add the family header */
  474. len += NLMSG_ALIGN(sizeof(struct rdma_ls_resolve_header));
  475. return len;
  476. }
  477. static int ib_nl_send_msg(struct ib_sa_query *query, gfp_t gfp_mask)
  478. {
  479. struct sk_buff *skb = NULL;
  480. struct nlmsghdr *nlh;
  481. void *data;
  482. int ret = 0;
  483. struct ib_sa_mad *mad;
  484. int len;
  485. mad = query->mad_buf->mad;
  486. len = ib_nl_get_path_rec_attrs_len(mad->sa_hdr.comp_mask);
  487. if (len <= 0)
  488. return -EMSGSIZE;
  489. skb = nlmsg_new(len, gfp_mask);
  490. if (!skb)
  491. return -ENOMEM;
  492. /* Put nlmsg header only for now */
  493. data = ibnl_put_msg(skb, &nlh, query->seq, 0, RDMA_NL_LS,
  494. RDMA_NL_LS_OP_RESOLVE, NLM_F_REQUEST);
  495. if (!data) {
  496. kfree_skb(skb);
  497. return -EMSGSIZE;
  498. }
  499. /* Add attributes */
  500. ib_nl_set_path_rec_attrs(skb, query);
  501. /* Repair the nlmsg header length */
  502. nlmsg_end(skb, nlh);
  503. ret = ibnl_multicast(skb, nlh, RDMA_NL_GROUP_LS, gfp_mask);
  504. if (!ret)
  505. ret = len;
  506. else
  507. ret = 0;
  508. return ret;
  509. }
  510. static int ib_nl_make_request(struct ib_sa_query *query, gfp_t gfp_mask)
  511. {
  512. unsigned long flags;
  513. unsigned long delay;
  514. int ret;
  515. INIT_LIST_HEAD(&query->list);
  516. query->seq = (u32)atomic_inc_return(&ib_nl_sa_request_seq);
  517. /* Put the request on the list first.*/
  518. spin_lock_irqsave(&ib_nl_request_lock, flags);
  519. delay = msecs_to_jiffies(sa_local_svc_timeout_ms);
  520. query->timeout = delay + jiffies;
  521. list_add_tail(&query->list, &ib_nl_request_list);
  522. /* Start the timeout if this is the only request */
  523. if (ib_nl_request_list.next == &query->list)
  524. queue_delayed_work(ib_nl_wq, &ib_nl_timed_work, delay);
  525. spin_unlock_irqrestore(&ib_nl_request_lock, flags);
  526. ret = ib_nl_send_msg(query, gfp_mask);
  527. if (ret <= 0) {
  528. ret = -EIO;
  529. /* Remove the request */
  530. spin_lock_irqsave(&ib_nl_request_lock, flags);
  531. list_del(&query->list);
  532. spin_unlock_irqrestore(&ib_nl_request_lock, flags);
  533. } else {
  534. ret = 0;
  535. }
  536. return ret;
  537. }
  538. static int ib_nl_cancel_request(struct ib_sa_query *query)
  539. {
  540. unsigned long flags;
  541. struct ib_sa_query *wait_query;
  542. int found = 0;
  543. spin_lock_irqsave(&ib_nl_request_lock, flags);
  544. list_for_each_entry(wait_query, &ib_nl_request_list, list) {
  545. /* Let the timeout to take care of the callback */
  546. if (query == wait_query) {
  547. query->flags |= IB_SA_CANCEL;
  548. query->timeout = jiffies;
  549. list_move(&query->list, &ib_nl_request_list);
  550. found = 1;
  551. mod_delayed_work(ib_nl_wq, &ib_nl_timed_work, 1);
  552. break;
  553. }
  554. }
  555. spin_unlock_irqrestore(&ib_nl_request_lock, flags);
  556. return found;
  557. }
  558. static void send_handler(struct ib_mad_agent *agent,
  559. struct ib_mad_send_wc *mad_send_wc);
  560. static void ib_nl_process_good_resolve_rsp(struct ib_sa_query *query,
  561. const struct nlmsghdr *nlh)
  562. {
  563. struct ib_mad_send_wc mad_send_wc;
  564. struct ib_sa_mad *mad = NULL;
  565. const struct nlattr *head, *curr;
  566. struct ib_path_rec_data *rec;
  567. int len, rem;
  568. u32 mask = 0;
  569. int status = -EIO;
  570. if (query->callback) {
  571. head = (const struct nlattr *) nlmsg_data(nlh);
  572. len = nlmsg_len(nlh);
  573. switch (query->path_use) {
  574. case LS_RESOLVE_PATH_USE_UNIDIRECTIONAL:
  575. mask = IB_PATH_PRIMARY | IB_PATH_OUTBOUND;
  576. break;
  577. case LS_RESOLVE_PATH_USE_ALL:
  578. case LS_RESOLVE_PATH_USE_GMP:
  579. default:
  580. mask = IB_PATH_PRIMARY | IB_PATH_GMP |
  581. IB_PATH_BIDIRECTIONAL;
  582. break;
  583. }
  584. nla_for_each_attr(curr, head, len, rem) {
  585. if (curr->nla_type == LS_NLA_TYPE_PATH_RECORD) {
  586. rec = nla_data(curr);
  587. /*
  588. * Get the first one. In the future, we may
  589. * need to get up to 6 pathrecords.
  590. */
  591. if ((rec->flags & mask) == mask) {
  592. mad = query->mad_buf->mad;
  593. mad->mad_hdr.method |=
  594. IB_MGMT_METHOD_RESP;
  595. memcpy(mad->data, rec->path_rec,
  596. sizeof(rec->path_rec));
  597. status = 0;
  598. break;
  599. }
  600. }
  601. }
  602. query->callback(query, status, mad);
  603. }
  604. mad_send_wc.send_buf = query->mad_buf;
  605. mad_send_wc.status = IB_WC_SUCCESS;
  606. send_handler(query->mad_buf->mad_agent, &mad_send_wc);
  607. }
  608. static void ib_nl_request_timeout(struct work_struct *work)
  609. {
  610. unsigned long flags;
  611. struct ib_sa_query *query;
  612. unsigned long delay;
  613. struct ib_mad_send_wc mad_send_wc;
  614. int ret;
  615. spin_lock_irqsave(&ib_nl_request_lock, flags);
  616. while (!list_empty(&ib_nl_request_list)) {
  617. query = list_entry(ib_nl_request_list.next,
  618. struct ib_sa_query, list);
  619. if (time_after(query->timeout, jiffies)) {
  620. delay = query->timeout - jiffies;
  621. if ((long)delay <= 0)
  622. delay = 1;
  623. queue_delayed_work(ib_nl_wq, &ib_nl_timed_work, delay);
  624. break;
  625. }
  626. list_del(&query->list);
  627. ib_sa_disable_local_svc(query);
  628. /* Hold the lock to protect against query cancellation */
  629. if (ib_sa_query_cancelled(query))
  630. ret = -1;
  631. else
  632. ret = ib_post_send_mad(query->mad_buf, NULL);
  633. if (ret) {
  634. mad_send_wc.send_buf = query->mad_buf;
  635. mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
  636. spin_unlock_irqrestore(&ib_nl_request_lock, flags);
  637. send_handler(query->port->agent, &mad_send_wc);
  638. spin_lock_irqsave(&ib_nl_request_lock, flags);
  639. }
  640. }
  641. spin_unlock_irqrestore(&ib_nl_request_lock, flags);
  642. }
  643. static int ib_nl_handle_set_timeout(struct sk_buff *skb,
  644. struct netlink_callback *cb)
  645. {
  646. const struct nlmsghdr *nlh = (struct nlmsghdr *)cb->nlh;
  647. int timeout, delta, abs_delta;
  648. const struct nlattr *attr;
  649. unsigned long flags;
  650. struct ib_sa_query *query;
  651. long delay = 0;
  652. struct nlattr *tb[LS_NLA_TYPE_MAX];
  653. int ret;
  654. if (!netlink_capable(skb, CAP_NET_ADMIN))
  655. return -EPERM;
  656. ret = nla_parse(tb, LS_NLA_TYPE_MAX - 1, nlmsg_data(nlh),
  657. nlmsg_len(nlh), ib_nl_policy);
  658. attr = (const struct nlattr *)tb[LS_NLA_TYPE_TIMEOUT];
  659. if (ret || !attr)
  660. goto settimeout_out;
  661. timeout = *(int *) nla_data(attr);
  662. if (timeout < IB_SA_LOCAL_SVC_TIMEOUT_MIN)
  663. timeout = IB_SA_LOCAL_SVC_TIMEOUT_MIN;
  664. if (timeout > IB_SA_LOCAL_SVC_TIMEOUT_MAX)
  665. timeout = IB_SA_LOCAL_SVC_TIMEOUT_MAX;
  666. delta = timeout - sa_local_svc_timeout_ms;
  667. if (delta < 0)
  668. abs_delta = -delta;
  669. else
  670. abs_delta = delta;
  671. if (delta != 0) {
  672. spin_lock_irqsave(&ib_nl_request_lock, flags);
  673. sa_local_svc_timeout_ms = timeout;
  674. list_for_each_entry(query, &ib_nl_request_list, list) {
  675. if (delta < 0 && abs_delta > query->timeout)
  676. query->timeout = 0;
  677. else
  678. query->timeout += delta;
  679. /* Get the new delay from the first entry */
  680. if (!delay) {
  681. delay = query->timeout - jiffies;
  682. if (delay <= 0)
  683. delay = 1;
  684. }
  685. }
  686. if (delay)
  687. mod_delayed_work(ib_nl_wq, &ib_nl_timed_work,
  688. (unsigned long)delay);
  689. spin_unlock_irqrestore(&ib_nl_request_lock, flags);
  690. }
  691. settimeout_out:
  692. return skb->len;
  693. }
  694. static inline int ib_nl_is_good_resolve_resp(const struct nlmsghdr *nlh)
  695. {
  696. struct nlattr *tb[LS_NLA_TYPE_MAX];
  697. int ret;
  698. if (nlh->nlmsg_flags & RDMA_NL_LS_F_ERR)
  699. return 0;
  700. ret = nla_parse(tb, LS_NLA_TYPE_MAX - 1, nlmsg_data(nlh),
  701. nlmsg_len(nlh), ib_nl_policy);
  702. if (ret)
  703. return 0;
  704. return 1;
  705. }
  706. static int ib_nl_handle_resolve_resp(struct sk_buff *skb,
  707. struct netlink_callback *cb)
  708. {
  709. const struct nlmsghdr *nlh = (struct nlmsghdr *)cb->nlh;
  710. unsigned long flags;
  711. struct ib_sa_query *query;
  712. struct ib_mad_send_buf *send_buf;
  713. struct ib_mad_send_wc mad_send_wc;
  714. int found = 0;
  715. int ret;
  716. if (!netlink_capable(skb, CAP_NET_ADMIN))
  717. return -EPERM;
  718. spin_lock_irqsave(&ib_nl_request_lock, flags);
  719. list_for_each_entry(query, &ib_nl_request_list, list) {
  720. /*
  721. * If the query is cancelled, let the timeout routine
  722. * take care of it.
  723. */
  724. if (nlh->nlmsg_seq == query->seq) {
  725. found = !ib_sa_query_cancelled(query);
  726. if (found)
  727. list_del(&query->list);
  728. break;
  729. }
  730. }
  731. if (!found) {
  732. spin_unlock_irqrestore(&ib_nl_request_lock, flags);
  733. goto resp_out;
  734. }
  735. send_buf = query->mad_buf;
  736. if (!ib_nl_is_good_resolve_resp(nlh)) {
  737. /* if the result is a failure, send out the packet via IB */
  738. ib_sa_disable_local_svc(query);
  739. ret = ib_post_send_mad(query->mad_buf, NULL);
  740. spin_unlock_irqrestore(&ib_nl_request_lock, flags);
  741. if (ret) {
  742. mad_send_wc.send_buf = send_buf;
  743. mad_send_wc.status = IB_WC_GENERAL_ERR;
  744. send_handler(query->port->agent, &mad_send_wc);
  745. }
  746. } else {
  747. spin_unlock_irqrestore(&ib_nl_request_lock, flags);
  748. ib_nl_process_good_resolve_rsp(query, nlh);
  749. }
  750. resp_out:
  751. return skb->len;
  752. }
  753. static struct ibnl_client_cbs ib_sa_cb_table[] = {
  754. [RDMA_NL_LS_OP_RESOLVE] = {
  755. .dump = ib_nl_handle_resolve_resp,
  756. .module = THIS_MODULE },
  757. [RDMA_NL_LS_OP_SET_TIMEOUT] = {
  758. .dump = ib_nl_handle_set_timeout,
  759. .module = THIS_MODULE },
  760. };
  761. static void free_sm_ah(struct kref *kref)
  762. {
  763. struct ib_sa_sm_ah *sm_ah = container_of(kref, struct ib_sa_sm_ah, ref);
  764. ib_destroy_ah(sm_ah->ah);
  765. kfree(sm_ah);
  766. }
  767. static void update_sm_ah(struct work_struct *work)
  768. {
  769. struct ib_sa_port *port =
  770. container_of(work, struct ib_sa_port, update_task);
  771. struct ib_sa_sm_ah *new_ah;
  772. struct ib_port_attr port_attr;
  773. struct ib_ah_attr ah_attr;
  774. if (ib_query_port(port->agent->device, port->port_num, &port_attr)) {
  775. printk(KERN_WARNING "Couldn't query port\n");
  776. return;
  777. }
  778. new_ah = kmalloc(sizeof *new_ah, GFP_KERNEL);
  779. if (!new_ah) {
  780. printk(KERN_WARNING "Couldn't allocate new SM AH\n");
  781. return;
  782. }
  783. kref_init(&new_ah->ref);
  784. new_ah->src_path_mask = (1 << port_attr.lmc) - 1;
  785. new_ah->pkey_index = 0;
  786. if (ib_find_pkey(port->agent->device, port->port_num,
  787. IB_DEFAULT_PKEY_FULL, &new_ah->pkey_index))
  788. printk(KERN_ERR "Couldn't find index for default PKey\n");
  789. memset(&ah_attr, 0, sizeof ah_attr);
  790. ah_attr.dlid = port_attr.sm_lid;
  791. ah_attr.sl = port_attr.sm_sl;
  792. ah_attr.port_num = port->port_num;
  793. new_ah->ah = ib_create_ah(port->agent->qp->pd, &ah_attr);
  794. if (IS_ERR(new_ah->ah)) {
  795. printk(KERN_WARNING "Couldn't create new SM AH\n");
  796. kfree(new_ah);
  797. return;
  798. }
  799. spin_lock_irq(&port->ah_lock);
  800. if (port->sm_ah)
  801. kref_put(&port->sm_ah->ref, free_sm_ah);
  802. port->sm_ah = new_ah;
  803. spin_unlock_irq(&port->ah_lock);
  804. }
  805. static void ib_sa_event(struct ib_event_handler *handler, struct ib_event *event)
  806. {
  807. if (event->event == IB_EVENT_PORT_ERR ||
  808. event->event == IB_EVENT_PORT_ACTIVE ||
  809. event->event == IB_EVENT_LID_CHANGE ||
  810. event->event == IB_EVENT_PKEY_CHANGE ||
  811. event->event == IB_EVENT_SM_CHANGE ||
  812. event->event == IB_EVENT_CLIENT_REREGISTER) {
  813. unsigned long flags;
  814. struct ib_sa_device *sa_dev =
  815. container_of(handler, typeof(*sa_dev), event_handler);
  816. struct ib_sa_port *port =
  817. &sa_dev->port[event->element.port_num - sa_dev->start_port];
  818. if (!rdma_cap_ib_sa(handler->device, port->port_num))
  819. return;
  820. spin_lock_irqsave(&port->ah_lock, flags);
  821. if (port->sm_ah)
  822. kref_put(&port->sm_ah->ref, free_sm_ah);
  823. port->sm_ah = NULL;
  824. spin_unlock_irqrestore(&port->ah_lock, flags);
  825. queue_work(ib_wq, &sa_dev->port[event->element.port_num -
  826. sa_dev->start_port].update_task);
  827. }
  828. }
  829. void ib_sa_register_client(struct ib_sa_client *client)
  830. {
  831. atomic_set(&client->users, 1);
  832. init_completion(&client->comp);
  833. }
  834. EXPORT_SYMBOL(ib_sa_register_client);
  835. void ib_sa_unregister_client(struct ib_sa_client *client)
  836. {
  837. ib_sa_client_put(client);
  838. wait_for_completion(&client->comp);
  839. }
  840. EXPORT_SYMBOL(ib_sa_unregister_client);
  841. /**
  842. * ib_sa_cancel_query - try to cancel an SA query
  843. * @id:ID of query to cancel
  844. * @query:query pointer to cancel
  845. *
  846. * Try to cancel an SA query. If the id and query don't match up or
  847. * the query has already completed, nothing is done. Otherwise the
  848. * query is canceled and will complete with a status of -EINTR.
  849. */
  850. void ib_sa_cancel_query(int id, struct ib_sa_query *query)
  851. {
  852. unsigned long flags;
  853. struct ib_mad_agent *agent;
  854. struct ib_mad_send_buf *mad_buf;
  855. spin_lock_irqsave(&idr_lock, flags);
  856. if (idr_find(&query_idr, id) != query) {
  857. spin_unlock_irqrestore(&idr_lock, flags);
  858. return;
  859. }
  860. agent = query->port->agent;
  861. mad_buf = query->mad_buf;
  862. spin_unlock_irqrestore(&idr_lock, flags);
  863. /*
  864. * If the query is still on the netlink request list, schedule
  865. * it to be cancelled by the timeout routine. Otherwise, it has been
  866. * sent to the MAD layer and has to be cancelled from there.
  867. */
  868. if (!ib_nl_cancel_request(query))
  869. ib_cancel_mad(agent, mad_buf);
  870. }
  871. EXPORT_SYMBOL(ib_sa_cancel_query);
  872. static u8 get_src_path_mask(struct ib_device *device, u8 port_num)
  873. {
  874. struct ib_sa_device *sa_dev;
  875. struct ib_sa_port *port;
  876. unsigned long flags;
  877. u8 src_path_mask;
  878. sa_dev = ib_get_client_data(device, &sa_client);
  879. if (!sa_dev)
  880. return 0x7f;
  881. port = &sa_dev->port[port_num - sa_dev->start_port];
  882. spin_lock_irqsave(&port->ah_lock, flags);
  883. src_path_mask = port->sm_ah ? port->sm_ah->src_path_mask : 0x7f;
  884. spin_unlock_irqrestore(&port->ah_lock, flags);
  885. return src_path_mask;
  886. }
  887. int ib_init_ah_from_path(struct ib_device *device, u8 port_num,
  888. struct ib_sa_path_rec *rec, struct ib_ah_attr *ah_attr)
  889. {
  890. int ret;
  891. u16 gid_index;
  892. int use_roce;
  893. struct net_device *ndev = NULL;
  894. memset(ah_attr, 0, sizeof *ah_attr);
  895. ah_attr->dlid = be16_to_cpu(rec->dlid);
  896. ah_attr->sl = rec->sl;
  897. ah_attr->src_path_bits = be16_to_cpu(rec->slid) &
  898. get_src_path_mask(device, port_num);
  899. ah_attr->port_num = port_num;
  900. ah_attr->static_rate = rec->rate;
  901. use_roce = rdma_cap_eth_ah(device, port_num);
  902. if (use_roce) {
  903. struct net_device *idev;
  904. struct net_device *resolved_dev;
  905. struct rdma_dev_addr dev_addr = {.bound_dev_if = rec->ifindex,
  906. .net = rec->net ? rec->net :
  907. &init_net};
  908. union {
  909. struct sockaddr _sockaddr;
  910. struct sockaddr_in _sockaddr_in;
  911. struct sockaddr_in6 _sockaddr_in6;
  912. } sgid_addr, dgid_addr;
  913. if (!device->get_netdev)
  914. return -EOPNOTSUPP;
  915. rdma_gid2ip(&sgid_addr._sockaddr, &rec->sgid);
  916. rdma_gid2ip(&dgid_addr._sockaddr, &rec->dgid);
  917. /* validate the route */
  918. ret = rdma_resolve_ip_route(&sgid_addr._sockaddr,
  919. &dgid_addr._sockaddr, &dev_addr);
  920. if (ret)
  921. return ret;
  922. if ((dev_addr.network == RDMA_NETWORK_IPV4 ||
  923. dev_addr.network == RDMA_NETWORK_IPV6) &&
  924. rec->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
  925. return -EINVAL;
  926. idev = device->get_netdev(device, port_num);
  927. if (!idev)
  928. return -ENODEV;
  929. resolved_dev = dev_get_by_index(dev_addr.net,
  930. dev_addr.bound_dev_if);
  931. if (resolved_dev->flags & IFF_LOOPBACK) {
  932. dev_put(resolved_dev);
  933. resolved_dev = idev;
  934. dev_hold(resolved_dev);
  935. }
  936. ndev = ib_get_ndev_from_path(rec);
  937. rcu_read_lock();
  938. if ((ndev && ndev != resolved_dev) ||
  939. (resolved_dev != idev &&
  940. !rdma_is_upper_dev_rcu(idev, resolved_dev)))
  941. ret = -EHOSTUNREACH;
  942. rcu_read_unlock();
  943. dev_put(idev);
  944. dev_put(resolved_dev);
  945. if (ret) {
  946. if (ndev)
  947. dev_put(ndev);
  948. return ret;
  949. }
  950. }
  951. if (rec->hop_limit > 1 || use_roce) {
  952. ah_attr->ah_flags = IB_AH_GRH;
  953. ah_attr->grh.dgid = rec->dgid;
  954. ret = ib_find_cached_gid_by_port(device, &rec->sgid,
  955. rec->gid_type, port_num, ndev,
  956. &gid_index);
  957. if (ret) {
  958. if (ndev)
  959. dev_put(ndev);
  960. return ret;
  961. }
  962. ah_attr->grh.sgid_index = gid_index;
  963. ah_attr->grh.flow_label = be32_to_cpu(rec->flow_label);
  964. ah_attr->grh.hop_limit = rec->hop_limit;
  965. ah_attr->grh.traffic_class = rec->traffic_class;
  966. if (ndev)
  967. dev_put(ndev);
  968. }
  969. if (use_roce)
  970. memcpy(ah_attr->dmac, rec->dmac, ETH_ALEN);
  971. return 0;
  972. }
  973. EXPORT_SYMBOL(ib_init_ah_from_path);
  974. static int alloc_mad(struct ib_sa_query *query, gfp_t gfp_mask)
  975. {
  976. unsigned long flags;
  977. spin_lock_irqsave(&query->port->ah_lock, flags);
  978. if (!query->port->sm_ah) {
  979. spin_unlock_irqrestore(&query->port->ah_lock, flags);
  980. return -EAGAIN;
  981. }
  982. kref_get(&query->port->sm_ah->ref);
  983. query->sm_ah = query->port->sm_ah;
  984. spin_unlock_irqrestore(&query->port->ah_lock, flags);
  985. query->mad_buf = ib_create_send_mad(query->port->agent, 1,
  986. query->sm_ah->pkey_index,
  987. 0, IB_MGMT_SA_HDR, IB_MGMT_SA_DATA,
  988. gfp_mask,
  989. IB_MGMT_BASE_VERSION);
  990. if (IS_ERR(query->mad_buf)) {
  991. kref_put(&query->sm_ah->ref, free_sm_ah);
  992. return -ENOMEM;
  993. }
  994. query->mad_buf->ah = query->sm_ah->ah;
  995. return 0;
  996. }
  997. static void free_mad(struct ib_sa_query *query)
  998. {
  999. ib_free_send_mad(query->mad_buf);
  1000. kref_put(&query->sm_ah->ref, free_sm_ah);
  1001. }
  1002. static void init_mad(struct ib_sa_mad *mad, struct ib_mad_agent *agent)
  1003. {
  1004. unsigned long flags;
  1005. memset(mad, 0, sizeof *mad);
  1006. mad->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
  1007. mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  1008. mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
  1009. spin_lock_irqsave(&tid_lock, flags);
  1010. mad->mad_hdr.tid =
  1011. cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
  1012. spin_unlock_irqrestore(&tid_lock, flags);
  1013. }
  1014. static int send_mad(struct ib_sa_query *query, int timeout_ms, gfp_t gfp_mask)
  1015. {
  1016. bool preload = gfpflags_allow_blocking(gfp_mask);
  1017. unsigned long flags;
  1018. int ret, id;
  1019. if (preload)
  1020. idr_preload(gfp_mask);
  1021. spin_lock_irqsave(&idr_lock, flags);
  1022. id = idr_alloc(&query_idr, query, 0, 0, GFP_NOWAIT);
  1023. spin_unlock_irqrestore(&idr_lock, flags);
  1024. if (preload)
  1025. idr_preload_end();
  1026. if (id < 0)
  1027. return id;
  1028. query->mad_buf->timeout_ms = timeout_ms;
  1029. query->mad_buf->context[0] = query;
  1030. query->id = id;
  1031. if (query->flags & IB_SA_ENABLE_LOCAL_SERVICE) {
  1032. if (!ibnl_chk_listeners(RDMA_NL_GROUP_LS)) {
  1033. if (!ib_nl_make_request(query, gfp_mask))
  1034. return id;
  1035. }
  1036. ib_sa_disable_local_svc(query);
  1037. }
  1038. ret = ib_post_send_mad(query->mad_buf, NULL);
  1039. if (ret) {
  1040. spin_lock_irqsave(&idr_lock, flags);
  1041. idr_remove(&query_idr, id);
  1042. spin_unlock_irqrestore(&idr_lock, flags);
  1043. }
  1044. /*
  1045. * It's not safe to dereference query any more, because the
  1046. * send may already have completed and freed the query in
  1047. * another context.
  1048. */
  1049. return ret ? ret : id;
  1050. }
  1051. void ib_sa_unpack_path(void *attribute, struct ib_sa_path_rec *rec)
  1052. {
  1053. ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table), attribute, rec);
  1054. }
  1055. EXPORT_SYMBOL(ib_sa_unpack_path);
  1056. void ib_sa_pack_path(struct ib_sa_path_rec *rec, void *attribute)
  1057. {
  1058. ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, attribute);
  1059. }
  1060. EXPORT_SYMBOL(ib_sa_pack_path);
  1061. static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
  1062. int status,
  1063. struct ib_sa_mad *mad)
  1064. {
  1065. struct ib_sa_path_query *query =
  1066. container_of(sa_query, struct ib_sa_path_query, sa_query);
  1067. if (mad) {
  1068. struct ib_sa_path_rec rec;
  1069. ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table),
  1070. mad->data, &rec);
  1071. rec.net = NULL;
  1072. rec.ifindex = 0;
  1073. rec.gid_type = IB_GID_TYPE_IB;
  1074. memset(rec.dmac, 0, ETH_ALEN);
  1075. query->callback(status, &rec, query->context);
  1076. } else
  1077. query->callback(status, NULL, query->context);
  1078. }
  1079. static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
  1080. {
  1081. kfree(container_of(sa_query, struct ib_sa_path_query, sa_query));
  1082. }
  1083. /**
  1084. * ib_sa_path_rec_get - Start a Path get query
  1085. * @client:SA client
  1086. * @device:device to send query on
  1087. * @port_num: port number to send query on
  1088. * @rec:Path Record to send in query
  1089. * @comp_mask:component mask to send in query
  1090. * @timeout_ms:time to wait for response
  1091. * @gfp_mask:GFP mask to use for internal allocations
  1092. * @callback:function called when query completes, times out or is
  1093. * canceled
  1094. * @context:opaque user context passed to callback
  1095. * @sa_query:query context, used to cancel query
  1096. *
  1097. * Send a Path Record Get query to the SA to look up a path. The
  1098. * callback function will be called when the query completes (or
  1099. * fails); status is 0 for a successful response, -EINTR if the query
  1100. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  1101. * occurred sending the query. The resp parameter of the callback is
  1102. * only valid if status is 0.
  1103. *
  1104. * If the return value of ib_sa_path_rec_get() is negative, it is an
  1105. * error code. Otherwise it is a query ID that can be used to cancel
  1106. * the query.
  1107. */
  1108. int ib_sa_path_rec_get(struct ib_sa_client *client,
  1109. struct ib_device *device, u8 port_num,
  1110. struct ib_sa_path_rec *rec,
  1111. ib_sa_comp_mask comp_mask,
  1112. int timeout_ms, gfp_t gfp_mask,
  1113. void (*callback)(int status,
  1114. struct ib_sa_path_rec *resp,
  1115. void *context),
  1116. void *context,
  1117. struct ib_sa_query **sa_query)
  1118. {
  1119. struct ib_sa_path_query *query;
  1120. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  1121. struct ib_sa_port *port;
  1122. struct ib_mad_agent *agent;
  1123. struct ib_sa_mad *mad;
  1124. int ret;
  1125. if (!sa_dev)
  1126. return -ENODEV;
  1127. port = &sa_dev->port[port_num - sa_dev->start_port];
  1128. agent = port->agent;
  1129. query = kzalloc(sizeof(*query), gfp_mask);
  1130. if (!query)
  1131. return -ENOMEM;
  1132. query->sa_query.port = port;
  1133. ret = alloc_mad(&query->sa_query, gfp_mask);
  1134. if (ret)
  1135. goto err1;
  1136. ib_sa_client_get(client);
  1137. query->sa_query.client = client;
  1138. query->callback = callback;
  1139. query->context = context;
  1140. mad = query->sa_query.mad_buf->mad;
  1141. init_mad(mad, agent);
  1142. query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL;
  1143. query->sa_query.release = ib_sa_path_rec_release;
  1144. mad->mad_hdr.method = IB_MGMT_METHOD_GET;
  1145. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_PATH_REC);
  1146. mad->sa_hdr.comp_mask = comp_mask;
  1147. ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, mad->data);
  1148. *sa_query = &query->sa_query;
  1149. query->sa_query.flags |= IB_SA_ENABLE_LOCAL_SERVICE;
  1150. query->sa_query.mad_buf->context[1] = rec;
  1151. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  1152. if (ret < 0)
  1153. goto err2;
  1154. return ret;
  1155. err2:
  1156. *sa_query = NULL;
  1157. ib_sa_client_put(query->sa_query.client);
  1158. free_mad(&query->sa_query);
  1159. err1:
  1160. kfree(query);
  1161. return ret;
  1162. }
  1163. EXPORT_SYMBOL(ib_sa_path_rec_get);
  1164. static void ib_sa_service_rec_callback(struct ib_sa_query *sa_query,
  1165. int status,
  1166. struct ib_sa_mad *mad)
  1167. {
  1168. struct ib_sa_service_query *query =
  1169. container_of(sa_query, struct ib_sa_service_query, sa_query);
  1170. if (mad) {
  1171. struct ib_sa_service_rec rec;
  1172. ib_unpack(service_rec_table, ARRAY_SIZE(service_rec_table),
  1173. mad->data, &rec);
  1174. query->callback(status, &rec, query->context);
  1175. } else
  1176. query->callback(status, NULL, query->context);
  1177. }
  1178. static void ib_sa_service_rec_release(struct ib_sa_query *sa_query)
  1179. {
  1180. kfree(container_of(sa_query, struct ib_sa_service_query, sa_query));
  1181. }
  1182. /**
  1183. * ib_sa_service_rec_query - Start Service Record operation
  1184. * @client:SA client
  1185. * @device:device to send request on
  1186. * @port_num: port number to send request on
  1187. * @method:SA method - should be get, set, or delete
  1188. * @rec:Service Record to send in request
  1189. * @comp_mask:component mask to send in request
  1190. * @timeout_ms:time to wait for response
  1191. * @gfp_mask:GFP mask to use for internal allocations
  1192. * @callback:function called when request completes, times out or is
  1193. * canceled
  1194. * @context:opaque user context passed to callback
  1195. * @sa_query:request context, used to cancel request
  1196. *
  1197. * Send a Service Record set/get/delete to the SA to register,
  1198. * unregister or query a service record.
  1199. * The callback function will be called when the request completes (or
  1200. * fails); status is 0 for a successful response, -EINTR if the query
  1201. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  1202. * occurred sending the query. The resp parameter of the callback is
  1203. * only valid if status is 0.
  1204. *
  1205. * If the return value of ib_sa_service_rec_query() is negative, it is an
  1206. * error code. Otherwise it is a request ID that can be used to cancel
  1207. * the query.
  1208. */
  1209. int ib_sa_service_rec_query(struct ib_sa_client *client,
  1210. struct ib_device *device, u8 port_num, u8 method,
  1211. struct ib_sa_service_rec *rec,
  1212. ib_sa_comp_mask comp_mask,
  1213. int timeout_ms, gfp_t gfp_mask,
  1214. void (*callback)(int status,
  1215. struct ib_sa_service_rec *resp,
  1216. void *context),
  1217. void *context,
  1218. struct ib_sa_query **sa_query)
  1219. {
  1220. struct ib_sa_service_query *query;
  1221. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  1222. struct ib_sa_port *port;
  1223. struct ib_mad_agent *agent;
  1224. struct ib_sa_mad *mad;
  1225. int ret;
  1226. if (!sa_dev)
  1227. return -ENODEV;
  1228. port = &sa_dev->port[port_num - sa_dev->start_port];
  1229. agent = port->agent;
  1230. if (method != IB_MGMT_METHOD_GET &&
  1231. method != IB_MGMT_METHOD_SET &&
  1232. method != IB_SA_METHOD_DELETE)
  1233. return -EINVAL;
  1234. query = kzalloc(sizeof(*query), gfp_mask);
  1235. if (!query)
  1236. return -ENOMEM;
  1237. query->sa_query.port = port;
  1238. ret = alloc_mad(&query->sa_query, gfp_mask);
  1239. if (ret)
  1240. goto err1;
  1241. ib_sa_client_get(client);
  1242. query->sa_query.client = client;
  1243. query->callback = callback;
  1244. query->context = context;
  1245. mad = query->sa_query.mad_buf->mad;
  1246. init_mad(mad, agent);
  1247. query->sa_query.callback = callback ? ib_sa_service_rec_callback : NULL;
  1248. query->sa_query.release = ib_sa_service_rec_release;
  1249. mad->mad_hdr.method = method;
  1250. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_SERVICE_REC);
  1251. mad->sa_hdr.comp_mask = comp_mask;
  1252. ib_pack(service_rec_table, ARRAY_SIZE(service_rec_table),
  1253. rec, mad->data);
  1254. *sa_query = &query->sa_query;
  1255. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  1256. if (ret < 0)
  1257. goto err2;
  1258. return ret;
  1259. err2:
  1260. *sa_query = NULL;
  1261. ib_sa_client_put(query->sa_query.client);
  1262. free_mad(&query->sa_query);
  1263. err1:
  1264. kfree(query);
  1265. return ret;
  1266. }
  1267. EXPORT_SYMBOL(ib_sa_service_rec_query);
  1268. static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
  1269. int status,
  1270. struct ib_sa_mad *mad)
  1271. {
  1272. struct ib_sa_mcmember_query *query =
  1273. container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
  1274. if (mad) {
  1275. struct ib_sa_mcmember_rec rec;
  1276. ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  1277. mad->data, &rec);
  1278. query->callback(status, &rec, query->context);
  1279. } else
  1280. query->callback(status, NULL, query->context);
  1281. }
  1282. static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
  1283. {
  1284. kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
  1285. }
  1286. int ib_sa_mcmember_rec_query(struct ib_sa_client *client,
  1287. struct ib_device *device, u8 port_num,
  1288. u8 method,
  1289. struct ib_sa_mcmember_rec *rec,
  1290. ib_sa_comp_mask comp_mask,
  1291. int timeout_ms, gfp_t gfp_mask,
  1292. void (*callback)(int status,
  1293. struct ib_sa_mcmember_rec *resp,
  1294. void *context),
  1295. void *context,
  1296. struct ib_sa_query **sa_query)
  1297. {
  1298. struct ib_sa_mcmember_query *query;
  1299. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  1300. struct ib_sa_port *port;
  1301. struct ib_mad_agent *agent;
  1302. struct ib_sa_mad *mad;
  1303. int ret;
  1304. if (!sa_dev)
  1305. return -ENODEV;
  1306. port = &sa_dev->port[port_num - sa_dev->start_port];
  1307. agent = port->agent;
  1308. query = kzalloc(sizeof(*query), gfp_mask);
  1309. if (!query)
  1310. return -ENOMEM;
  1311. query->sa_query.port = port;
  1312. ret = alloc_mad(&query->sa_query, gfp_mask);
  1313. if (ret)
  1314. goto err1;
  1315. ib_sa_client_get(client);
  1316. query->sa_query.client = client;
  1317. query->callback = callback;
  1318. query->context = context;
  1319. mad = query->sa_query.mad_buf->mad;
  1320. init_mad(mad, agent);
  1321. query->sa_query.callback = callback ? ib_sa_mcmember_rec_callback : NULL;
  1322. query->sa_query.release = ib_sa_mcmember_rec_release;
  1323. mad->mad_hdr.method = method;
  1324. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
  1325. mad->sa_hdr.comp_mask = comp_mask;
  1326. ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  1327. rec, mad->data);
  1328. *sa_query = &query->sa_query;
  1329. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  1330. if (ret < 0)
  1331. goto err2;
  1332. return ret;
  1333. err2:
  1334. *sa_query = NULL;
  1335. ib_sa_client_put(query->sa_query.client);
  1336. free_mad(&query->sa_query);
  1337. err1:
  1338. kfree(query);
  1339. return ret;
  1340. }
  1341. /* Support GuidInfoRecord */
  1342. static void ib_sa_guidinfo_rec_callback(struct ib_sa_query *sa_query,
  1343. int status,
  1344. struct ib_sa_mad *mad)
  1345. {
  1346. struct ib_sa_guidinfo_query *query =
  1347. container_of(sa_query, struct ib_sa_guidinfo_query, sa_query);
  1348. if (mad) {
  1349. struct ib_sa_guidinfo_rec rec;
  1350. ib_unpack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table),
  1351. mad->data, &rec);
  1352. query->callback(status, &rec, query->context);
  1353. } else
  1354. query->callback(status, NULL, query->context);
  1355. }
  1356. static void ib_sa_guidinfo_rec_release(struct ib_sa_query *sa_query)
  1357. {
  1358. kfree(container_of(sa_query, struct ib_sa_guidinfo_query, sa_query));
  1359. }
  1360. int ib_sa_guid_info_rec_query(struct ib_sa_client *client,
  1361. struct ib_device *device, u8 port_num,
  1362. struct ib_sa_guidinfo_rec *rec,
  1363. ib_sa_comp_mask comp_mask, u8 method,
  1364. int timeout_ms, gfp_t gfp_mask,
  1365. void (*callback)(int status,
  1366. struct ib_sa_guidinfo_rec *resp,
  1367. void *context),
  1368. void *context,
  1369. struct ib_sa_query **sa_query)
  1370. {
  1371. struct ib_sa_guidinfo_query *query;
  1372. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  1373. struct ib_sa_port *port;
  1374. struct ib_mad_agent *agent;
  1375. struct ib_sa_mad *mad;
  1376. int ret;
  1377. if (!sa_dev)
  1378. return -ENODEV;
  1379. if (method != IB_MGMT_METHOD_GET &&
  1380. method != IB_MGMT_METHOD_SET &&
  1381. method != IB_SA_METHOD_DELETE) {
  1382. return -EINVAL;
  1383. }
  1384. port = &sa_dev->port[port_num - sa_dev->start_port];
  1385. agent = port->agent;
  1386. query = kzalloc(sizeof(*query), gfp_mask);
  1387. if (!query)
  1388. return -ENOMEM;
  1389. query->sa_query.port = port;
  1390. ret = alloc_mad(&query->sa_query, gfp_mask);
  1391. if (ret)
  1392. goto err1;
  1393. ib_sa_client_get(client);
  1394. query->sa_query.client = client;
  1395. query->callback = callback;
  1396. query->context = context;
  1397. mad = query->sa_query.mad_buf->mad;
  1398. init_mad(mad, agent);
  1399. query->sa_query.callback = callback ? ib_sa_guidinfo_rec_callback : NULL;
  1400. query->sa_query.release = ib_sa_guidinfo_rec_release;
  1401. mad->mad_hdr.method = method;
  1402. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_GUID_INFO_REC);
  1403. mad->sa_hdr.comp_mask = comp_mask;
  1404. ib_pack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table), rec,
  1405. mad->data);
  1406. *sa_query = &query->sa_query;
  1407. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  1408. if (ret < 0)
  1409. goto err2;
  1410. return ret;
  1411. err2:
  1412. *sa_query = NULL;
  1413. ib_sa_client_put(query->sa_query.client);
  1414. free_mad(&query->sa_query);
  1415. err1:
  1416. kfree(query);
  1417. return ret;
  1418. }
  1419. EXPORT_SYMBOL(ib_sa_guid_info_rec_query);
  1420. static void send_handler(struct ib_mad_agent *agent,
  1421. struct ib_mad_send_wc *mad_send_wc)
  1422. {
  1423. struct ib_sa_query *query = mad_send_wc->send_buf->context[0];
  1424. unsigned long flags;
  1425. if (query->callback)
  1426. switch (mad_send_wc->status) {
  1427. case IB_WC_SUCCESS:
  1428. /* No callback -- already got recv */
  1429. break;
  1430. case IB_WC_RESP_TIMEOUT_ERR:
  1431. query->callback(query, -ETIMEDOUT, NULL);
  1432. break;
  1433. case IB_WC_WR_FLUSH_ERR:
  1434. query->callback(query, -EINTR, NULL);
  1435. break;
  1436. default:
  1437. query->callback(query, -EIO, NULL);
  1438. break;
  1439. }
  1440. spin_lock_irqsave(&idr_lock, flags);
  1441. idr_remove(&query_idr, query->id);
  1442. spin_unlock_irqrestore(&idr_lock, flags);
  1443. free_mad(query);
  1444. ib_sa_client_put(query->client);
  1445. query->release(query);
  1446. }
  1447. static void recv_handler(struct ib_mad_agent *mad_agent,
  1448. struct ib_mad_recv_wc *mad_recv_wc)
  1449. {
  1450. struct ib_sa_query *query;
  1451. struct ib_mad_send_buf *mad_buf;
  1452. mad_buf = (void *) (unsigned long) mad_recv_wc->wc->wr_id;
  1453. query = mad_buf->context[0];
  1454. if (query->callback) {
  1455. if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
  1456. query->callback(query,
  1457. mad_recv_wc->recv_buf.mad->mad_hdr.status ?
  1458. -EINVAL : 0,
  1459. (struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
  1460. else
  1461. query->callback(query, -EIO, NULL);
  1462. }
  1463. ib_free_recv_mad(mad_recv_wc);
  1464. }
  1465. static void ib_sa_add_one(struct ib_device *device)
  1466. {
  1467. struct ib_sa_device *sa_dev;
  1468. int s, e, i;
  1469. int count = 0;
  1470. s = rdma_start_port(device);
  1471. e = rdma_end_port(device);
  1472. sa_dev = kzalloc(sizeof *sa_dev +
  1473. (e - s + 1) * sizeof (struct ib_sa_port),
  1474. GFP_KERNEL);
  1475. if (!sa_dev)
  1476. return;
  1477. sa_dev->start_port = s;
  1478. sa_dev->end_port = e;
  1479. for (i = 0; i <= e - s; ++i) {
  1480. spin_lock_init(&sa_dev->port[i].ah_lock);
  1481. if (!rdma_cap_ib_sa(device, i + 1))
  1482. continue;
  1483. sa_dev->port[i].sm_ah = NULL;
  1484. sa_dev->port[i].port_num = i + s;
  1485. sa_dev->port[i].agent =
  1486. ib_register_mad_agent(device, i + s, IB_QPT_GSI,
  1487. NULL, 0, send_handler,
  1488. recv_handler, sa_dev, 0);
  1489. if (IS_ERR(sa_dev->port[i].agent))
  1490. goto err;
  1491. INIT_WORK(&sa_dev->port[i].update_task, update_sm_ah);
  1492. count++;
  1493. }
  1494. if (!count)
  1495. goto free;
  1496. ib_set_client_data(device, &sa_client, sa_dev);
  1497. /*
  1498. * We register our event handler after everything is set up,
  1499. * and then update our cached info after the event handler is
  1500. * registered to avoid any problems if a port changes state
  1501. * during our initialization.
  1502. */
  1503. INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
  1504. if (ib_register_event_handler(&sa_dev->event_handler))
  1505. goto err;
  1506. for (i = 0; i <= e - s; ++i) {
  1507. if (rdma_cap_ib_sa(device, i + 1))
  1508. update_sm_ah(&sa_dev->port[i].update_task);
  1509. }
  1510. return;
  1511. err:
  1512. while (--i >= 0) {
  1513. if (rdma_cap_ib_sa(device, i + 1))
  1514. ib_unregister_mad_agent(sa_dev->port[i].agent);
  1515. }
  1516. free:
  1517. kfree(sa_dev);
  1518. return;
  1519. }
  1520. static void ib_sa_remove_one(struct ib_device *device, void *client_data)
  1521. {
  1522. struct ib_sa_device *sa_dev = client_data;
  1523. int i;
  1524. if (!sa_dev)
  1525. return;
  1526. ib_unregister_event_handler(&sa_dev->event_handler);
  1527. flush_workqueue(ib_wq);
  1528. for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
  1529. if (rdma_cap_ib_sa(device, i + 1)) {
  1530. ib_unregister_mad_agent(sa_dev->port[i].agent);
  1531. if (sa_dev->port[i].sm_ah)
  1532. kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
  1533. }
  1534. }
  1535. kfree(sa_dev);
  1536. }
  1537. static int __init ib_sa_init(void)
  1538. {
  1539. int ret;
  1540. get_random_bytes(&tid, sizeof tid);
  1541. atomic_set(&ib_nl_sa_request_seq, 0);
  1542. ret = ib_register_client(&sa_client);
  1543. if (ret) {
  1544. printk(KERN_ERR "Couldn't register ib_sa client\n");
  1545. goto err1;
  1546. }
  1547. ret = mcast_init();
  1548. if (ret) {
  1549. printk(KERN_ERR "Couldn't initialize multicast handling\n");
  1550. goto err2;
  1551. }
  1552. ib_nl_wq = create_singlethread_workqueue("ib_nl_sa_wq");
  1553. if (!ib_nl_wq) {
  1554. ret = -ENOMEM;
  1555. goto err3;
  1556. }
  1557. if (ibnl_add_client(RDMA_NL_LS, RDMA_NL_LS_NUM_OPS,
  1558. ib_sa_cb_table)) {
  1559. pr_err("Failed to add netlink callback\n");
  1560. ret = -EINVAL;
  1561. goto err4;
  1562. }
  1563. INIT_DELAYED_WORK(&ib_nl_timed_work, ib_nl_request_timeout);
  1564. return 0;
  1565. err4:
  1566. destroy_workqueue(ib_nl_wq);
  1567. err3:
  1568. mcast_cleanup();
  1569. err2:
  1570. ib_unregister_client(&sa_client);
  1571. err1:
  1572. return ret;
  1573. }
  1574. static void __exit ib_sa_cleanup(void)
  1575. {
  1576. ibnl_remove_client(RDMA_NL_LS);
  1577. cancel_delayed_work(&ib_nl_timed_work);
  1578. flush_workqueue(ib_nl_wq);
  1579. destroy_workqueue(ib_nl_wq);
  1580. mcast_cleanup();
  1581. ib_unregister_client(&sa_client);
  1582. idr_destroy(&query_idr);
  1583. }
  1584. module_init(ib_sa_init);
  1585. module_exit(ib_sa_cleanup);