sa_query.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854
  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 (!(nlh->nlmsg_flags & NLM_F_REQUEST) ||
  655. !(NETLINK_CB(skb).sk) ||
  656. !netlink_capable(skb, CAP_NET_ADMIN))
  657. return -EPERM;
  658. ret = nla_parse(tb, LS_NLA_TYPE_MAX - 1, nlmsg_data(nlh),
  659. nlmsg_len(nlh), ib_nl_policy);
  660. attr = (const struct nlattr *)tb[LS_NLA_TYPE_TIMEOUT];
  661. if (ret || !attr)
  662. goto settimeout_out;
  663. timeout = *(int *) nla_data(attr);
  664. if (timeout < IB_SA_LOCAL_SVC_TIMEOUT_MIN)
  665. timeout = IB_SA_LOCAL_SVC_TIMEOUT_MIN;
  666. if (timeout > IB_SA_LOCAL_SVC_TIMEOUT_MAX)
  667. timeout = IB_SA_LOCAL_SVC_TIMEOUT_MAX;
  668. delta = timeout - sa_local_svc_timeout_ms;
  669. if (delta < 0)
  670. abs_delta = -delta;
  671. else
  672. abs_delta = delta;
  673. if (delta != 0) {
  674. spin_lock_irqsave(&ib_nl_request_lock, flags);
  675. sa_local_svc_timeout_ms = timeout;
  676. list_for_each_entry(query, &ib_nl_request_list, list) {
  677. if (delta < 0 && abs_delta > query->timeout)
  678. query->timeout = 0;
  679. else
  680. query->timeout += delta;
  681. /* Get the new delay from the first entry */
  682. if (!delay) {
  683. delay = query->timeout - jiffies;
  684. if (delay <= 0)
  685. delay = 1;
  686. }
  687. }
  688. if (delay)
  689. mod_delayed_work(ib_nl_wq, &ib_nl_timed_work,
  690. (unsigned long)delay);
  691. spin_unlock_irqrestore(&ib_nl_request_lock, flags);
  692. }
  693. settimeout_out:
  694. return skb->len;
  695. }
  696. static inline int ib_nl_is_good_resolve_resp(const struct nlmsghdr *nlh)
  697. {
  698. struct nlattr *tb[LS_NLA_TYPE_MAX];
  699. int ret;
  700. if (nlh->nlmsg_flags & RDMA_NL_LS_F_ERR)
  701. return 0;
  702. ret = nla_parse(tb, LS_NLA_TYPE_MAX - 1, nlmsg_data(nlh),
  703. nlmsg_len(nlh), ib_nl_policy);
  704. if (ret)
  705. return 0;
  706. return 1;
  707. }
  708. static int ib_nl_handle_resolve_resp(struct sk_buff *skb,
  709. struct netlink_callback *cb)
  710. {
  711. const struct nlmsghdr *nlh = (struct nlmsghdr *)cb->nlh;
  712. unsigned long flags;
  713. struct ib_sa_query *query;
  714. struct ib_mad_send_buf *send_buf;
  715. struct ib_mad_send_wc mad_send_wc;
  716. int found = 0;
  717. int ret;
  718. if ((nlh->nlmsg_flags & NLM_F_REQUEST) ||
  719. !(NETLINK_CB(skb).sk) ||
  720. !netlink_capable(skb, CAP_NET_ADMIN))
  721. return -EPERM;
  722. spin_lock_irqsave(&ib_nl_request_lock, flags);
  723. list_for_each_entry(query, &ib_nl_request_list, list) {
  724. /*
  725. * If the query is cancelled, let the timeout routine
  726. * take care of it.
  727. */
  728. if (nlh->nlmsg_seq == query->seq) {
  729. found = !ib_sa_query_cancelled(query);
  730. if (found)
  731. list_del(&query->list);
  732. break;
  733. }
  734. }
  735. if (!found) {
  736. spin_unlock_irqrestore(&ib_nl_request_lock, flags);
  737. goto resp_out;
  738. }
  739. send_buf = query->mad_buf;
  740. if (!ib_nl_is_good_resolve_resp(nlh)) {
  741. /* if the result is a failure, send out the packet via IB */
  742. ib_sa_disable_local_svc(query);
  743. ret = ib_post_send_mad(query->mad_buf, NULL);
  744. spin_unlock_irqrestore(&ib_nl_request_lock, flags);
  745. if (ret) {
  746. mad_send_wc.send_buf = send_buf;
  747. mad_send_wc.status = IB_WC_GENERAL_ERR;
  748. send_handler(query->port->agent, &mad_send_wc);
  749. }
  750. } else {
  751. spin_unlock_irqrestore(&ib_nl_request_lock, flags);
  752. ib_nl_process_good_resolve_rsp(query, nlh);
  753. }
  754. resp_out:
  755. return skb->len;
  756. }
  757. static struct ibnl_client_cbs ib_sa_cb_table[] = {
  758. [RDMA_NL_LS_OP_RESOLVE] = {
  759. .dump = ib_nl_handle_resolve_resp,
  760. .module = THIS_MODULE },
  761. [RDMA_NL_LS_OP_SET_TIMEOUT] = {
  762. .dump = ib_nl_handle_set_timeout,
  763. .module = THIS_MODULE },
  764. };
  765. static void free_sm_ah(struct kref *kref)
  766. {
  767. struct ib_sa_sm_ah *sm_ah = container_of(kref, struct ib_sa_sm_ah, ref);
  768. ib_destroy_ah(sm_ah->ah);
  769. kfree(sm_ah);
  770. }
  771. static void update_sm_ah(struct work_struct *work)
  772. {
  773. struct ib_sa_port *port =
  774. container_of(work, struct ib_sa_port, update_task);
  775. struct ib_sa_sm_ah *new_ah;
  776. struct ib_port_attr port_attr;
  777. struct ib_ah_attr ah_attr;
  778. if (ib_query_port(port->agent->device, port->port_num, &port_attr)) {
  779. pr_warn("Couldn't query port\n");
  780. return;
  781. }
  782. new_ah = kmalloc(sizeof *new_ah, GFP_KERNEL);
  783. if (!new_ah) {
  784. return;
  785. }
  786. kref_init(&new_ah->ref);
  787. new_ah->src_path_mask = (1 << port_attr.lmc) - 1;
  788. new_ah->pkey_index = 0;
  789. if (ib_find_pkey(port->agent->device, port->port_num,
  790. IB_DEFAULT_PKEY_FULL, &new_ah->pkey_index))
  791. pr_err("Couldn't find index for default PKey\n");
  792. memset(&ah_attr, 0, sizeof ah_attr);
  793. ah_attr.dlid = port_attr.sm_lid;
  794. ah_attr.sl = port_attr.sm_sl;
  795. ah_attr.port_num = port->port_num;
  796. if (port_attr.grh_required) {
  797. ah_attr.ah_flags = IB_AH_GRH;
  798. ah_attr.grh.dgid.global.subnet_prefix = cpu_to_be64(port_attr.subnet_prefix);
  799. ah_attr.grh.dgid.global.interface_id = cpu_to_be64(IB_SA_WELL_KNOWN_GUID);
  800. }
  801. new_ah->ah = ib_create_ah(port->agent->qp->pd, &ah_attr);
  802. if (IS_ERR(new_ah->ah)) {
  803. pr_warn("Couldn't create new SM AH\n");
  804. kfree(new_ah);
  805. return;
  806. }
  807. spin_lock_irq(&port->ah_lock);
  808. if (port->sm_ah)
  809. kref_put(&port->sm_ah->ref, free_sm_ah);
  810. port->sm_ah = new_ah;
  811. spin_unlock_irq(&port->ah_lock);
  812. }
  813. static void ib_sa_event(struct ib_event_handler *handler, struct ib_event *event)
  814. {
  815. if (event->event == IB_EVENT_PORT_ERR ||
  816. event->event == IB_EVENT_PORT_ACTIVE ||
  817. event->event == IB_EVENT_LID_CHANGE ||
  818. event->event == IB_EVENT_PKEY_CHANGE ||
  819. event->event == IB_EVENT_SM_CHANGE ||
  820. event->event == IB_EVENT_CLIENT_REREGISTER) {
  821. unsigned long flags;
  822. struct ib_sa_device *sa_dev =
  823. container_of(handler, typeof(*sa_dev), event_handler);
  824. struct ib_sa_port *port =
  825. &sa_dev->port[event->element.port_num - sa_dev->start_port];
  826. if (!rdma_cap_ib_sa(handler->device, port->port_num))
  827. return;
  828. spin_lock_irqsave(&port->ah_lock, flags);
  829. if (port->sm_ah)
  830. kref_put(&port->sm_ah->ref, free_sm_ah);
  831. port->sm_ah = NULL;
  832. spin_unlock_irqrestore(&port->ah_lock, flags);
  833. queue_work(ib_wq, &sa_dev->port[event->element.port_num -
  834. sa_dev->start_port].update_task);
  835. }
  836. }
  837. void ib_sa_register_client(struct ib_sa_client *client)
  838. {
  839. atomic_set(&client->users, 1);
  840. init_completion(&client->comp);
  841. }
  842. EXPORT_SYMBOL(ib_sa_register_client);
  843. void ib_sa_unregister_client(struct ib_sa_client *client)
  844. {
  845. ib_sa_client_put(client);
  846. wait_for_completion(&client->comp);
  847. }
  848. EXPORT_SYMBOL(ib_sa_unregister_client);
  849. /**
  850. * ib_sa_cancel_query - try to cancel an SA query
  851. * @id:ID of query to cancel
  852. * @query:query pointer to cancel
  853. *
  854. * Try to cancel an SA query. If the id and query don't match up or
  855. * the query has already completed, nothing is done. Otherwise the
  856. * query is canceled and will complete with a status of -EINTR.
  857. */
  858. void ib_sa_cancel_query(int id, struct ib_sa_query *query)
  859. {
  860. unsigned long flags;
  861. struct ib_mad_agent *agent;
  862. struct ib_mad_send_buf *mad_buf;
  863. spin_lock_irqsave(&idr_lock, flags);
  864. if (idr_find(&query_idr, id) != query) {
  865. spin_unlock_irqrestore(&idr_lock, flags);
  866. return;
  867. }
  868. agent = query->port->agent;
  869. mad_buf = query->mad_buf;
  870. spin_unlock_irqrestore(&idr_lock, flags);
  871. /*
  872. * If the query is still on the netlink request list, schedule
  873. * it to be cancelled by the timeout routine. Otherwise, it has been
  874. * sent to the MAD layer and has to be cancelled from there.
  875. */
  876. if (!ib_nl_cancel_request(query))
  877. ib_cancel_mad(agent, mad_buf);
  878. }
  879. EXPORT_SYMBOL(ib_sa_cancel_query);
  880. static u8 get_src_path_mask(struct ib_device *device, u8 port_num)
  881. {
  882. struct ib_sa_device *sa_dev;
  883. struct ib_sa_port *port;
  884. unsigned long flags;
  885. u8 src_path_mask;
  886. sa_dev = ib_get_client_data(device, &sa_client);
  887. if (!sa_dev)
  888. return 0x7f;
  889. port = &sa_dev->port[port_num - sa_dev->start_port];
  890. spin_lock_irqsave(&port->ah_lock, flags);
  891. src_path_mask = port->sm_ah ? port->sm_ah->src_path_mask : 0x7f;
  892. spin_unlock_irqrestore(&port->ah_lock, flags);
  893. return src_path_mask;
  894. }
  895. int ib_init_ah_from_path(struct ib_device *device, u8 port_num,
  896. struct ib_sa_path_rec *rec, struct ib_ah_attr *ah_attr)
  897. {
  898. int ret;
  899. u16 gid_index;
  900. int use_roce;
  901. struct net_device *ndev = NULL;
  902. memset(ah_attr, 0, sizeof *ah_attr);
  903. ah_attr->dlid = be16_to_cpu(rec->dlid);
  904. ah_attr->sl = rec->sl;
  905. ah_attr->src_path_bits = be16_to_cpu(rec->slid) &
  906. get_src_path_mask(device, port_num);
  907. ah_attr->port_num = port_num;
  908. ah_attr->static_rate = rec->rate;
  909. use_roce = rdma_cap_eth_ah(device, port_num);
  910. if (use_roce) {
  911. struct net_device *idev;
  912. struct net_device *resolved_dev;
  913. struct rdma_dev_addr dev_addr = {.bound_dev_if = rec->ifindex,
  914. .net = rec->net ? rec->net :
  915. &init_net};
  916. union {
  917. struct sockaddr _sockaddr;
  918. struct sockaddr_in _sockaddr_in;
  919. struct sockaddr_in6 _sockaddr_in6;
  920. } sgid_addr, dgid_addr;
  921. if (!device->get_netdev)
  922. return -EOPNOTSUPP;
  923. rdma_gid2ip(&sgid_addr._sockaddr, &rec->sgid);
  924. rdma_gid2ip(&dgid_addr._sockaddr, &rec->dgid);
  925. /* validate the route */
  926. ret = rdma_resolve_ip_route(&sgid_addr._sockaddr,
  927. &dgid_addr._sockaddr, &dev_addr);
  928. if (ret)
  929. return ret;
  930. if ((dev_addr.network == RDMA_NETWORK_IPV4 ||
  931. dev_addr.network == RDMA_NETWORK_IPV6) &&
  932. rec->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
  933. return -EINVAL;
  934. idev = device->get_netdev(device, port_num);
  935. if (!idev)
  936. return -ENODEV;
  937. resolved_dev = dev_get_by_index(dev_addr.net,
  938. dev_addr.bound_dev_if);
  939. if (resolved_dev->flags & IFF_LOOPBACK) {
  940. dev_put(resolved_dev);
  941. resolved_dev = idev;
  942. dev_hold(resolved_dev);
  943. }
  944. ndev = ib_get_ndev_from_path(rec);
  945. rcu_read_lock();
  946. if ((ndev && ndev != resolved_dev) ||
  947. (resolved_dev != idev &&
  948. !rdma_is_upper_dev_rcu(idev, resolved_dev)))
  949. ret = -EHOSTUNREACH;
  950. rcu_read_unlock();
  951. dev_put(idev);
  952. dev_put(resolved_dev);
  953. if (ret) {
  954. if (ndev)
  955. dev_put(ndev);
  956. return ret;
  957. }
  958. }
  959. if (rec->hop_limit > 0 || use_roce) {
  960. ah_attr->ah_flags = IB_AH_GRH;
  961. ah_attr->grh.dgid = rec->dgid;
  962. ret = ib_find_cached_gid_by_port(device, &rec->sgid,
  963. rec->gid_type, port_num, ndev,
  964. &gid_index);
  965. if (ret) {
  966. if (ndev)
  967. dev_put(ndev);
  968. return ret;
  969. }
  970. ah_attr->grh.sgid_index = gid_index;
  971. ah_attr->grh.flow_label = be32_to_cpu(rec->flow_label);
  972. ah_attr->grh.hop_limit = rec->hop_limit;
  973. ah_attr->grh.traffic_class = rec->traffic_class;
  974. if (ndev)
  975. dev_put(ndev);
  976. }
  977. if (use_roce)
  978. memcpy(ah_attr->dmac, rec->dmac, ETH_ALEN);
  979. return 0;
  980. }
  981. EXPORT_SYMBOL(ib_init_ah_from_path);
  982. static int alloc_mad(struct ib_sa_query *query, gfp_t gfp_mask)
  983. {
  984. unsigned long flags;
  985. spin_lock_irqsave(&query->port->ah_lock, flags);
  986. if (!query->port->sm_ah) {
  987. spin_unlock_irqrestore(&query->port->ah_lock, flags);
  988. return -EAGAIN;
  989. }
  990. kref_get(&query->port->sm_ah->ref);
  991. query->sm_ah = query->port->sm_ah;
  992. spin_unlock_irqrestore(&query->port->ah_lock, flags);
  993. query->mad_buf = ib_create_send_mad(query->port->agent, 1,
  994. query->sm_ah->pkey_index,
  995. 0, IB_MGMT_SA_HDR, IB_MGMT_SA_DATA,
  996. gfp_mask,
  997. IB_MGMT_BASE_VERSION);
  998. if (IS_ERR(query->mad_buf)) {
  999. kref_put(&query->sm_ah->ref, free_sm_ah);
  1000. return -ENOMEM;
  1001. }
  1002. query->mad_buf->ah = query->sm_ah->ah;
  1003. return 0;
  1004. }
  1005. static void free_mad(struct ib_sa_query *query)
  1006. {
  1007. ib_free_send_mad(query->mad_buf);
  1008. kref_put(&query->sm_ah->ref, free_sm_ah);
  1009. }
  1010. static void init_mad(struct ib_sa_mad *mad, struct ib_mad_agent *agent)
  1011. {
  1012. unsigned long flags;
  1013. memset(mad, 0, sizeof *mad);
  1014. mad->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
  1015. mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  1016. mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
  1017. spin_lock_irqsave(&tid_lock, flags);
  1018. mad->mad_hdr.tid =
  1019. cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
  1020. spin_unlock_irqrestore(&tid_lock, flags);
  1021. }
  1022. static int send_mad(struct ib_sa_query *query, int timeout_ms, gfp_t gfp_mask)
  1023. {
  1024. bool preload = gfpflags_allow_blocking(gfp_mask);
  1025. unsigned long flags;
  1026. int ret, id;
  1027. if (preload)
  1028. idr_preload(gfp_mask);
  1029. spin_lock_irqsave(&idr_lock, flags);
  1030. id = idr_alloc(&query_idr, query, 0, 0, GFP_NOWAIT);
  1031. spin_unlock_irqrestore(&idr_lock, flags);
  1032. if (preload)
  1033. idr_preload_end();
  1034. if (id < 0)
  1035. return id;
  1036. query->mad_buf->timeout_ms = timeout_ms;
  1037. query->mad_buf->context[0] = query;
  1038. query->id = id;
  1039. if (query->flags & IB_SA_ENABLE_LOCAL_SERVICE) {
  1040. if (!ibnl_chk_listeners(RDMA_NL_GROUP_LS)) {
  1041. if (!ib_nl_make_request(query, gfp_mask))
  1042. return id;
  1043. }
  1044. ib_sa_disable_local_svc(query);
  1045. }
  1046. ret = ib_post_send_mad(query->mad_buf, NULL);
  1047. if (ret) {
  1048. spin_lock_irqsave(&idr_lock, flags);
  1049. idr_remove(&query_idr, id);
  1050. spin_unlock_irqrestore(&idr_lock, flags);
  1051. }
  1052. /*
  1053. * It's not safe to dereference query any more, because the
  1054. * send may already have completed and freed the query in
  1055. * another context.
  1056. */
  1057. return ret ? ret : id;
  1058. }
  1059. void ib_sa_unpack_path(void *attribute, struct ib_sa_path_rec *rec)
  1060. {
  1061. ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table), attribute, rec);
  1062. }
  1063. EXPORT_SYMBOL(ib_sa_unpack_path);
  1064. void ib_sa_pack_path(struct ib_sa_path_rec *rec, void *attribute)
  1065. {
  1066. ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, attribute);
  1067. }
  1068. EXPORT_SYMBOL(ib_sa_pack_path);
  1069. static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
  1070. int status,
  1071. struct ib_sa_mad *mad)
  1072. {
  1073. struct ib_sa_path_query *query =
  1074. container_of(sa_query, struct ib_sa_path_query, sa_query);
  1075. if (mad) {
  1076. struct ib_sa_path_rec rec;
  1077. ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table),
  1078. mad->data, &rec);
  1079. rec.net = NULL;
  1080. rec.ifindex = 0;
  1081. rec.gid_type = IB_GID_TYPE_IB;
  1082. eth_zero_addr(rec.dmac);
  1083. query->callback(status, &rec, query->context);
  1084. } else
  1085. query->callback(status, NULL, query->context);
  1086. }
  1087. static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
  1088. {
  1089. kfree(container_of(sa_query, struct ib_sa_path_query, sa_query));
  1090. }
  1091. /**
  1092. * ib_sa_path_rec_get - Start a Path get query
  1093. * @client:SA client
  1094. * @device:device to send query on
  1095. * @port_num: port number to send query on
  1096. * @rec:Path Record to send in query
  1097. * @comp_mask:component mask to send in query
  1098. * @timeout_ms:time to wait for response
  1099. * @gfp_mask:GFP mask to use for internal allocations
  1100. * @callback:function called when query completes, times out or is
  1101. * canceled
  1102. * @context:opaque user context passed to callback
  1103. * @sa_query:query context, used to cancel query
  1104. *
  1105. * Send a Path Record Get query to the SA to look up a path. The
  1106. * callback function will be called when the query completes (or
  1107. * fails); status is 0 for a successful response, -EINTR if the query
  1108. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  1109. * occurred sending the query. The resp parameter of the callback is
  1110. * only valid if status is 0.
  1111. *
  1112. * If the return value of ib_sa_path_rec_get() is negative, it is an
  1113. * error code. Otherwise it is a query ID that can be used to cancel
  1114. * the query.
  1115. */
  1116. int ib_sa_path_rec_get(struct ib_sa_client *client,
  1117. struct ib_device *device, u8 port_num,
  1118. struct ib_sa_path_rec *rec,
  1119. ib_sa_comp_mask comp_mask,
  1120. int timeout_ms, gfp_t gfp_mask,
  1121. void (*callback)(int status,
  1122. struct ib_sa_path_rec *resp,
  1123. void *context),
  1124. void *context,
  1125. struct ib_sa_query **sa_query)
  1126. {
  1127. struct ib_sa_path_query *query;
  1128. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  1129. struct ib_sa_port *port;
  1130. struct ib_mad_agent *agent;
  1131. struct ib_sa_mad *mad;
  1132. int ret;
  1133. if (!sa_dev)
  1134. return -ENODEV;
  1135. port = &sa_dev->port[port_num - sa_dev->start_port];
  1136. agent = port->agent;
  1137. query = kzalloc(sizeof(*query), gfp_mask);
  1138. if (!query)
  1139. return -ENOMEM;
  1140. query->sa_query.port = port;
  1141. ret = alloc_mad(&query->sa_query, gfp_mask);
  1142. if (ret)
  1143. goto err1;
  1144. ib_sa_client_get(client);
  1145. query->sa_query.client = client;
  1146. query->callback = callback;
  1147. query->context = context;
  1148. mad = query->sa_query.mad_buf->mad;
  1149. init_mad(mad, agent);
  1150. query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL;
  1151. query->sa_query.release = ib_sa_path_rec_release;
  1152. mad->mad_hdr.method = IB_MGMT_METHOD_GET;
  1153. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_PATH_REC);
  1154. mad->sa_hdr.comp_mask = comp_mask;
  1155. ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, mad->data);
  1156. *sa_query = &query->sa_query;
  1157. query->sa_query.flags |= IB_SA_ENABLE_LOCAL_SERVICE;
  1158. query->sa_query.mad_buf->context[1] = rec;
  1159. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  1160. if (ret < 0)
  1161. goto err2;
  1162. return ret;
  1163. err2:
  1164. *sa_query = NULL;
  1165. ib_sa_client_put(query->sa_query.client);
  1166. free_mad(&query->sa_query);
  1167. err1:
  1168. kfree(query);
  1169. return ret;
  1170. }
  1171. EXPORT_SYMBOL(ib_sa_path_rec_get);
  1172. static void ib_sa_service_rec_callback(struct ib_sa_query *sa_query,
  1173. int status,
  1174. struct ib_sa_mad *mad)
  1175. {
  1176. struct ib_sa_service_query *query =
  1177. container_of(sa_query, struct ib_sa_service_query, sa_query);
  1178. if (mad) {
  1179. struct ib_sa_service_rec rec;
  1180. ib_unpack(service_rec_table, ARRAY_SIZE(service_rec_table),
  1181. mad->data, &rec);
  1182. query->callback(status, &rec, query->context);
  1183. } else
  1184. query->callback(status, NULL, query->context);
  1185. }
  1186. static void ib_sa_service_rec_release(struct ib_sa_query *sa_query)
  1187. {
  1188. kfree(container_of(sa_query, struct ib_sa_service_query, sa_query));
  1189. }
  1190. /**
  1191. * ib_sa_service_rec_query - Start Service Record operation
  1192. * @client:SA client
  1193. * @device:device to send request on
  1194. * @port_num: port number to send request on
  1195. * @method:SA method - should be get, set, or delete
  1196. * @rec:Service Record to send in request
  1197. * @comp_mask:component mask to send in request
  1198. * @timeout_ms:time to wait for response
  1199. * @gfp_mask:GFP mask to use for internal allocations
  1200. * @callback:function called when request completes, times out or is
  1201. * canceled
  1202. * @context:opaque user context passed to callback
  1203. * @sa_query:request context, used to cancel request
  1204. *
  1205. * Send a Service Record set/get/delete to the SA to register,
  1206. * unregister or query a service record.
  1207. * The callback function will be called when the request completes (or
  1208. * fails); status is 0 for a successful response, -EINTR if the query
  1209. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  1210. * occurred sending the query. The resp parameter of the callback is
  1211. * only valid if status is 0.
  1212. *
  1213. * If the return value of ib_sa_service_rec_query() is negative, it is an
  1214. * error code. Otherwise it is a request ID that can be used to cancel
  1215. * the query.
  1216. */
  1217. int ib_sa_service_rec_query(struct ib_sa_client *client,
  1218. struct ib_device *device, u8 port_num, u8 method,
  1219. struct ib_sa_service_rec *rec,
  1220. ib_sa_comp_mask comp_mask,
  1221. int timeout_ms, gfp_t gfp_mask,
  1222. void (*callback)(int status,
  1223. struct ib_sa_service_rec *resp,
  1224. void *context),
  1225. void *context,
  1226. struct ib_sa_query **sa_query)
  1227. {
  1228. struct ib_sa_service_query *query;
  1229. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  1230. struct ib_sa_port *port;
  1231. struct ib_mad_agent *agent;
  1232. struct ib_sa_mad *mad;
  1233. int ret;
  1234. if (!sa_dev)
  1235. return -ENODEV;
  1236. port = &sa_dev->port[port_num - sa_dev->start_port];
  1237. agent = port->agent;
  1238. if (method != IB_MGMT_METHOD_GET &&
  1239. method != IB_MGMT_METHOD_SET &&
  1240. method != IB_SA_METHOD_DELETE)
  1241. return -EINVAL;
  1242. query = kzalloc(sizeof(*query), gfp_mask);
  1243. if (!query)
  1244. return -ENOMEM;
  1245. query->sa_query.port = port;
  1246. ret = alloc_mad(&query->sa_query, gfp_mask);
  1247. if (ret)
  1248. goto err1;
  1249. ib_sa_client_get(client);
  1250. query->sa_query.client = client;
  1251. query->callback = callback;
  1252. query->context = context;
  1253. mad = query->sa_query.mad_buf->mad;
  1254. init_mad(mad, agent);
  1255. query->sa_query.callback = callback ? ib_sa_service_rec_callback : NULL;
  1256. query->sa_query.release = ib_sa_service_rec_release;
  1257. mad->mad_hdr.method = method;
  1258. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_SERVICE_REC);
  1259. mad->sa_hdr.comp_mask = comp_mask;
  1260. ib_pack(service_rec_table, ARRAY_SIZE(service_rec_table),
  1261. rec, mad->data);
  1262. *sa_query = &query->sa_query;
  1263. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  1264. if (ret < 0)
  1265. goto err2;
  1266. return ret;
  1267. err2:
  1268. *sa_query = NULL;
  1269. ib_sa_client_put(query->sa_query.client);
  1270. free_mad(&query->sa_query);
  1271. err1:
  1272. kfree(query);
  1273. return ret;
  1274. }
  1275. EXPORT_SYMBOL(ib_sa_service_rec_query);
  1276. static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
  1277. int status,
  1278. struct ib_sa_mad *mad)
  1279. {
  1280. struct ib_sa_mcmember_query *query =
  1281. container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
  1282. if (mad) {
  1283. struct ib_sa_mcmember_rec rec;
  1284. ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  1285. mad->data, &rec);
  1286. query->callback(status, &rec, query->context);
  1287. } else
  1288. query->callback(status, NULL, query->context);
  1289. }
  1290. static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
  1291. {
  1292. kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
  1293. }
  1294. int ib_sa_mcmember_rec_query(struct ib_sa_client *client,
  1295. struct ib_device *device, u8 port_num,
  1296. u8 method,
  1297. struct ib_sa_mcmember_rec *rec,
  1298. ib_sa_comp_mask comp_mask,
  1299. int timeout_ms, gfp_t gfp_mask,
  1300. void (*callback)(int status,
  1301. struct ib_sa_mcmember_rec *resp,
  1302. void *context),
  1303. void *context,
  1304. struct ib_sa_query **sa_query)
  1305. {
  1306. struct ib_sa_mcmember_query *query;
  1307. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  1308. struct ib_sa_port *port;
  1309. struct ib_mad_agent *agent;
  1310. struct ib_sa_mad *mad;
  1311. int ret;
  1312. if (!sa_dev)
  1313. return -ENODEV;
  1314. port = &sa_dev->port[port_num - sa_dev->start_port];
  1315. agent = port->agent;
  1316. query = kzalloc(sizeof(*query), gfp_mask);
  1317. if (!query)
  1318. return -ENOMEM;
  1319. query->sa_query.port = port;
  1320. ret = alloc_mad(&query->sa_query, gfp_mask);
  1321. if (ret)
  1322. goto err1;
  1323. ib_sa_client_get(client);
  1324. query->sa_query.client = client;
  1325. query->callback = callback;
  1326. query->context = context;
  1327. mad = query->sa_query.mad_buf->mad;
  1328. init_mad(mad, agent);
  1329. query->sa_query.callback = callback ? ib_sa_mcmember_rec_callback : NULL;
  1330. query->sa_query.release = ib_sa_mcmember_rec_release;
  1331. mad->mad_hdr.method = method;
  1332. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
  1333. mad->sa_hdr.comp_mask = comp_mask;
  1334. ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  1335. rec, mad->data);
  1336. *sa_query = &query->sa_query;
  1337. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  1338. if (ret < 0)
  1339. goto err2;
  1340. return ret;
  1341. err2:
  1342. *sa_query = NULL;
  1343. ib_sa_client_put(query->sa_query.client);
  1344. free_mad(&query->sa_query);
  1345. err1:
  1346. kfree(query);
  1347. return ret;
  1348. }
  1349. /* Support GuidInfoRecord */
  1350. static void ib_sa_guidinfo_rec_callback(struct ib_sa_query *sa_query,
  1351. int status,
  1352. struct ib_sa_mad *mad)
  1353. {
  1354. struct ib_sa_guidinfo_query *query =
  1355. container_of(sa_query, struct ib_sa_guidinfo_query, sa_query);
  1356. if (mad) {
  1357. struct ib_sa_guidinfo_rec rec;
  1358. ib_unpack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table),
  1359. mad->data, &rec);
  1360. query->callback(status, &rec, query->context);
  1361. } else
  1362. query->callback(status, NULL, query->context);
  1363. }
  1364. static void ib_sa_guidinfo_rec_release(struct ib_sa_query *sa_query)
  1365. {
  1366. kfree(container_of(sa_query, struct ib_sa_guidinfo_query, sa_query));
  1367. }
  1368. int ib_sa_guid_info_rec_query(struct ib_sa_client *client,
  1369. struct ib_device *device, u8 port_num,
  1370. struct ib_sa_guidinfo_rec *rec,
  1371. ib_sa_comp_mask comp_mask, u8 method,
  1372. int timeout_ms, gfp_t gfp_mask,
  1373. void (*callback)(int status,
  1374. struct ib_sa_guidinfo_rec *resp,
  1375. void *context),
  1376. void *context,
  1377. struct ib_sa_query **sa_query)
  1378. {
  1379. struct ib_sa_guidinfo_query *query;
  1380. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  1381. struct ib_sa_port *port;
  1382. struct ib_mad_agent *agent;
  1383. struct ib_sa_mad *mad;
  1384. int ret;
  1385. if (!sa_dev)
  1386. return -ENODEV;
  1387. if (method != IB_MGMT_METHOD_GET &&
  1388. method != IB_MGMT_METHOD_SET &&
  1389. method != IB_SA_METHOD_DELETE) {
  1390. return -EINVAL;
  1391. }
  1392. port = &sa_dev->port[port_num - sa_dev->start_port];
  1393. agent = port->agent;
  1394. query = kzalloc(sizeof(*query), gfp_mask);
  1395. if (!query)
  1396. return -ENOMEM;
  1397. query->sa_query.port = port;
  1398. ret = alloc_mad(&query->sa_query, gfp_mask);
  1399. if (ret)
  1400. goto err1;
  1401. ib_sa_client_get(client);
  1402. query->sa_query.client = client;
  1403. query->callback = callback;
  1404. query->context = context;
  1405. mad = query->sa_query.mad_buf->mad;
  1406. init_mad(mad, agent);
  1407. query->sa_query.callback = callback ? ib_sa_guidinfo_rec_callback : NULL;
  1408. query->sa_query.release = ib_sa_guidinfo_rec_release;
  1409. mad->mad_hdr.method = method;
  1410. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_GUID_INFO_REC);
  1411. mad->sa_hdr.comp_mask = comp_mask;
  1412. ib_pack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table), rec,
  1413. mad->data);
  1414. *sa_query = &query->sa_query;
  1415. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  1416. if (ret < 0)
  1417. goto err2;
  1418. return ret;
  1419. err2:
  1420. *sa_query = NULL;
  1421. ib_sa_client_put(query->sa_query.client);
  1422. free_mad(&query->sa_query);
  1423. err1:
  1424. kfree(query);
  1425. return ret;
  1426. }
  1427. EXPORT_SYMBOL(ib_sa_guid_info_rec_query);
  1428. static void send_handler(struct ib_mad_agent *agent,
  1429. struct ib_mad_send_wc *mad_send_wc)
  1430. {
  1431. struct ib_sa_query *query = mad_send_wc->send_buf->context[0];
  1432. unsigned long flags;
  1433. if (query->callback)
  1434. switch (mad_send_wc->status) {
  1435. case IB_WC_SUCCESS:
  1436. /* No callback -- already got recv */
  1437. break;
  1438. case IB_WC_RESP_TIMEOUT_ERR:
  1439. query->callback(query, -ETIMEDOUT, NULL);
  1440. break;
  1441. case IB_WC_WR_FLUSH_ERR:
  1442. query->callback(query, -EINTR, NULL);
  1443. break;
  1444. default:
  1445. query->callback(query, -EIO, NULL);
  1446. break;
  1447. }
  1448. spin_lock_irqsave(&idr_lock, flags);
  1449. idr_remove(&query_idr, query->id);
  1450. spin_unlock_irqrestore(&idr_lock, flags);
  1451. free_mad(query);
  1452. ib_sa_client_put(query->client);
  1453. query->release(query);
  1454. }
  1455. static void recv_handler(struct ib_mad_agent *mad_agent,
  1456. struct ib_mad_send_buf *send_buf,
  1457. struct ib_mad_recv_wc *mad_recv_wc)
  1458. {
  1459. struct ib_sa_query *query;
  1460. if (!send_buf)
  1461. return;
  1462. query = send_buf->context[0];
  1463. if (query->callback) {
  1464. if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
  1465. query->callback(query,
  1466. mad_recv_wc->recv_buf.mad->mad_hdr.status ?
  1467. -EINVAL : 0,
  1468. (struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
  1469. else
  1470. query->callback(query, -EIO, NULL);
  1471. }
  1472. ib_free_recv_mad(mad_recv_wc);
  1473. }
  1474. static void ib_sa_add_one(struct ib_device *device)
  1475. {
  1476. struct ib_sa_device *sa_dev;
  1477. int s, e, i;
  1478. int count = 0;
  1479. s = rdma_start_port(device);
  1480. e = rdma_end_port(device);
  1481. sa_dev = kzalloc(sizeof *sa_dev +
  1482. (e - s + 1) * sizeof (struct ib_sa_port),
  1483. GFP_KERNEL);
  1484. if (!sa_dev)
  1485. return;
  1486. sa_dev->start_port = s;
  1487. sa_dev->end_port = e;
  1488. for (i = 0; i <= e - s; ++i) {
  1489. spin_lock_init(&sa_dev->port[i].ah_lock);
  1490. if (!rdma_cap_ib_sa(device, i + 1))
  1491. continue;
  1492. sa_dev->port[i].sm_ah = NULL;
  1493. sa_dev->port[i].port_num = i + s;
  1494. sa_dev->port[i].agent =
  1495. ib_register_mad_agent(device, i + s, IB_QPT_GSI,
  1496. NULL, 0, send_handler,
  1497. recv_handler, sa_dev, 0);
  1498. if (IS_ERR(sa_dev->port[i].agent))
  1499. goto err;
  1500. INIT_WORK(&sa_dev->port[i].update_task, update_sm_ah);
  1501. count++;
  1502. }
  1503. if (!count)
  1504. goto free;
  1505. ib_set_client_data(device, &sa_client, sa_dev);
  1506. /*
  1507. * We register our event handler after everything is set up,
  1508. * and then update our cached info after the event handler is
  1509. * registered to avoid any problems if a port changes state
  1510. * during our initialization.
  1511. */
  1512. INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
  1513. if (ib_register_event_handler(&sa_dev->event_handler))
  1514. goto err;
  1515. for (i = 0; i <= e - s; ++i) {
  1516. if (rdma_cap_ib_sa(device, i + 1))
  1517. update_sm_ah(&sa_dev->port[i].update_task);
  1518. }
  1519. return;
  1520. err:
  1521. while (--i >= 0) {
  1522. if (rdma_cap_ib_sa(device, i + 1))
  1523. ib_unregister_mad_agent(sa_dev->port[i].agent);
  1524. }
  1525. free:
  1526. kfree(sa_dev);
  1527. return;
  1528. }
  1529. static void ib_sa_remove_one(struct ib_device *device, void *client_data)
  1530. {
  1531. struct ib_sa_device *sa_dev = client_data;
  1532. int i;
  1533. if (!sa_dev)
  1534. return;
  1535. ib_unregister_event_handler(&sa_dev->event_handler);
  1536. flush_workqueue(ib_wq);
  1537. for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
  1538. if (rdma_cap_ib_sa(device, i + 1)) {
  1539. ib_unregister_mad_agent(sa_dev->port[i].agent);
  1540. if (sa_dev->port[i].sm_ah)
  1541. kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
  1542. }
  1543. }
  1544. kfree(sa_dev);
  1545. }
  1546. static int __init ib_sa_init(void)
  1547. {
  1548. int ret;
  1549. get_random_bytes(&tid, sizeof tid);
  1550. atomic_set(&ib_nl_sa_request_seq, 0);
  1551. ret = ib_register_client(&sa_client);
  1552. if (ret) {
  1553. pr_err("Couldn't register ib_sa client\n");
  1554. goto err1;
  1555. }
  1556. ret = mcast_init();
  1557. if (ret) {
  1558. pr_err("Couldn't initialize multicast handling\n");
  1559. goto err2;
  1560. }
  1561. ib_nl_wq = create_singlethread_workqueue("ib_nl_sa_wq");
  1562. if (!ib_nl_wq) {
  1563. ret = -ENOMEM;
  1564. goto err3;
  1565. }
  1566. if (ibnl_add_client(RDMA_NL_LS, RDMA_NL_LS_NUM_OPS,
  1567. ib_sa_cb_table)) {
  1568. pr_err("Failed to add netlink callback\n");
  1569. ret = -EINVAL;
  1570. goto err4;
  1571. }
  1572. INIT_DELAYED_WORK(&ib_nl_timed_work, ib_nl_request_timeout);
  1573. return 0;
  1574. err4:
  1575. destroy_workqueue(ib_nl_wq);
  1576. err3:
  1577. mcast_cleanup();
  1578. err2:
  1579. ib_unregister_client(&sa_client);
  1580. err1:
  1581. return ret;
  1582. }
  1583. static void __exit ib_sa_cleanup(void)
  1584. {
  1585. ibnl_remove_client(RDMA_NL_LS);
  1586. cancel_delayed_work(&ib_nl_timed_work);
  1587. flush_workqueue(ib_nl_wq);
  1588. destroy_workqueue(ib_nl_wq);
  1589. mcast_cleanup();
  1590. ib_unregister_client(&sa_client);
  1591. idr_destroy(&query_idr);
  1592. }
  1593. module_init(ib_sa_init);
  1594. module_exit(ib_sa_cleanup);