sa_query.c 53 KB

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