sa_query.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467
  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/xarray.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 <rdma/opa_addr.h>
  53. #include "sa.h"
  54. #include "core_priv.h"
  55. #define IB_SA_LOCAL_SVC_TIMEOUT_MIN 100
  56. #define IB_SA_LOCAL_SVC_TIMEOUT_DEFAULT 2000
  57. #define IB_SA_LOCAL_SVC_TIMEOUT_MAX 200000
  58. #define IB_SA_CPI_MAX_RETRY_CNT 3
  59. #define IB_SA_CPI_RETRY_WAIT 1000 /*msecs */
  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. enum rdma_class_port_info_type {
  68. RDMA_CLASS_PORT_INFO_IB,
  69. RDMA_CLASS_PORT_INFO_OPA
  70. };
  71. struct rdma_class_port_info {
  72. enum rdma_class_port_info_type type;
  73. union {
  74. struct ib_class_port_info ib;
  75. struct opa_class_port_info opa;
  76. };
  77. };
  78. struct ib_sa_classport_cache {
  79. bool valid;
  80. int retry_cnt;
  81. struct rdma_class_port_info data;
  82. };
  83. struct ib_sa_port {
  84. struct ib_mad_agent *agent;
  85. struct ib_sa_sm_ah *sm_ah;
  86. struct work_struct update_task;
  87. struct ib_sa_classport_cache classport_info;
  88. struct delayed_work ib_cpi_work;
  89. spinlock_t classport_lock; /* protects class port info set */
  90. spinlock_t ah_lock;
  91. u8 port_num;
  92. };
  93. struct ib_sa_device {
  94. int start_port, end_port;
  95. struct ib_event_handler event_handler;
  96. struct ib_sa_port port[];
  97. };
  98. struct ib_sa_query {
  99. void (*callback)(struct ib_sa_query *, int, struct ib_sa_mad *);
  100. void (*release)(struct ib_sa_query *);
  101. struct ib_sa_client *client;
  102. struct ib_sa_port *port;
  103. struct ib_mad_send_buf *mad_buf;
  104. struct ib_sa_sm_ah *sm_ah;
  105. int id;
  106. u32 flags;
  107. struct list_head list; /* Local svc request list */
  108. u32 seq; /* Local svc request sequence number */
  109. unsigned long timeout; /* Local svc timeout */
  110. u8 path_use; /* How will the pathrecord be used */
  111. };
  112. #define IB_SA_ENABLE_LOCAL_SERVICE 0x00000001
  113. #define IB_SA_CANCEL 0x00000002
  114. #define IB_SA_QUERY_OPA 0x00000004
  115. struct ib_sa_service_query {
  116. void (*callback)(int, struct ib_sa_service_rec *, void *);
  117. void *context;
  118. struct ib_sa_query sa_query;
  119. };
  120. struct ib_sa_path_query {
  121. void (*callback)(int, struct sa_path_rec *, void *);
  122. void *context;
  123. struct ib_sa_query sa_query;
  124. struct sa_path_rec *conv_pr;
  125. };
  126. struct ib_sa_guidinfo_query {
  127. void (*callback)(int, struct ib_sa_guidinfo_rec *, void *);
  128. void *context;
  129. struct ib_sa_query sa_query;
  130. };
  131. struct ib_sa_classport_info_query {
  132. void (*callback)(void *);
  133. void *context;
  134. struct ib_sa_query sa_query;
  135. };
  136. struct ib_sa_mcmember_query {
  137. void (*callback)(int, struct ib_sa_mcmember_rec *, void *);
  138. void *context;
  139. struct ib_sa_query sa_query;
  140. };
  141. static LIST_HEAD(ib_nl_request_list);
  142. static DEFINE_SPINLOCK(ib_nl_request_lock);
  143. static atomic_t ib_nl_sa_request_seq;
  144. static struct workqueue_struct *ib_nl_wq;
  145. static struct delayed_work ib_nl_timed_work;
  146. static const struct nla_policy ib_nl_policy[LS_NLA_TYPE_MAX] = {
  147. [LS_NLA_TYPE_PATH_RECORD] = {.type = NLA_BINARY,
  148. .len = sizeof(struct ib_path_rec_data)},
  149. [LS_NLA_TYPE_TIMEOUT] = {.type = NLA_U32},
  150. [LS_NLA_TYPE_SERVICE_ID] = {.type = NLA_U64},
  151. [LS_NLA_TYPE_DGID] = {.type = NLA_BINARY,
  152. .len = sizeof(struct rdma_nla_ls_gid)},
  153. [LS_NLA_TYPE_SGID] = {.type = NLA_BINARY,
  154. .len = sizeof(struct rdma_nla_ls_gid)},
  155. [LS_NLA_TYPE_TCLASS] = {.type = NLA_U8},
  156. [LS_NLA_TYPE_PKEY] = {.type = NLA_U16},
  157. [LS_NLA_TYPE_QOS_CLASS] = {.type = NLA_U16},
  158. };
  159. static int ib_sa_add_one(struct ib_device *device);
  160. static void ib_sa_remove_one(struct ib_device *device, void *client_data);
  161. static struct ib_client sa_client = {
  162. .name = "sa",
  163. .add = ib_sa_add_one,
  164. .remove = ib_sa_remove_one
  165. };
  166. static DEFINE_XARRAY_FLAGS(queries, XA_FLAGS_ALLOC | XA_FLAGS_LOCK_IRQ);
  167. static DEFINE_SPINLOCK(tid_lock);
  168. static u32 tid;
  169. #define PATH_REC_FIELD(field) \
  170. .struct_offset_bytes = offsetof(struct sa_path_rec, field), \
  171. .struct_size_bytes = sizeof_field(struct sa_path_rec, field), \
  172. .field_name = "sa_path_rec:" #field
  173. static const struct ib_field path_rec_table[] = {
  174. { PATH_REC_FIELD(service_id),
  175. .offset_words = 0,
  176. .offset_bits = 0,
  177. .size_bits = 64 },
  178. { PATH_REC_FIELD(dgid),
  179. .offset_words = 2,
  180. .offset_bits = 0,
  181. .size_bits = 128 },
  182. { PATH_REC_FIELD(sgid),
  183. .offset_words = 6,
  184. .offset_bits = 0,
  185. .size_bits = 128 },
  186. { PATH_REC_FIELD(ib.dlid),
  187. .offset_words = 10,
  188. .offset_bits = 0,
  189. .size_bits = 16 },
  190. { PATH_REC_FIELD(ib.slid),
  191. .offset_words = 10,
  192. .offset_bits = 16,
  193. .size_bits = 16 },
  194. { PATH_REC_FIELD(ib.raw_traffic),
  195. .offset_words = 11,
  196. .offset_bits = 0,
  197. .size_bits = 1 },
  198. { RESERVED,
  199. .offset_words = 11,
  200. .offset_bits = 1,
  201. .size_bits = 3 },
  202. { PATH_REC_FIELD(flow_label),
  203. .offset_words = 11,
  204. .offset_bits = 4,
  205. .size_bits = 20 },
  206. { PATH_REC_FIELD(hop_limit),
  207. .offset_words = 11,
  208. .offset_bits = 24,
  209. .size_bits = 8 },
  210. { PATH_REC_FIELD(traffic_class),
  211. .offset_words = 12,
  212. .offset_bits = 0,
  213. .size_bits = 8 },
  214. { PATH_REC_FIELD(reversible),
  215. .offset_words = 12,
  216. .offset_bits = 8,
  217. .size_bits = 1 },
  218. { PATH_REC_FIELD(numb_path),
  219. .offset_words = 12,
  220. .offset_bits = 9,
  221. .size_bits = 7 },
  222. { PATH_REC_FIELD(pkey),
  223. .offset_words = 12,
  224. .offset_bits = 16,
  225. .size_bits = 16 },
  226. { PATH_REC_FIELD(qos_class),
  227. .offset_words = 13,
  228. .offset_bits = 0,
  229. .size_bits = 12 },
  230. { PATH_REC_FIELD(sl),
  231. .offset_words = 13,
  232. .offset_bits = 12,
  233. .size_bits = 4 },
  234. { PATH_REC_FIELD(mtu_selector),
  235. .offset_words = 13,
  236. .offset_bits = 16,
  237. .size_bits = 2 },
  238. { PATH_REC_FIELD(mtu),
  239. .offset_words = 13,
  240. .offset_bits = 18,
  241. .size_bits = 6 },
  242. { PATH_REC_FIELD(rate_selector),
  243. .offset_words = 13,
  244. .offset_bits = 24,
  245. .size_bits = 2 },
  246. { PATH_REC_FIELD(rate),
  247. .offset_words = 13,
  248. .offset_bits = 26,
  249. .size_bits = 6 },
  250. { PATH_REC_FIELD(packet_life_time_selector),
  251. .offset_words = 14,
  252. .offset_bits = 0,
  253. .size_bits = 2 },
  254. { PATH_REC_FIELD(packet_life_time),
  255. .offset_words = 14,
  256. .offset_bits = 2,
  257. .size_bits = 6 },
  258. { PATH_REC_FIELD(preference),
  259. .offset_words = 14,
  260. .offset_bits = 8,
  261. .size_bits = 8 },
  262. { RESERVED,
  263. .offset_words = 14,
  264. .offset_bits = 16,
  265. .size_bits = 48 },
  266. };
  267. #define OPA_PATH_REC_FIELD(field) \
  268. .struct_offset_bytes = \
  269. offsetof(struct sa_path_rec, field), \
  270. .struct_size_bytes = \
  271. sizeof_field(struct sa_path_rec, field), \
  272. .field_name = "sa_path_rec:" #field
  273. static const struct ib_field opa_path_rec_table[] = {
  274. { OPA_PATH_REC_FIELD(service_id),
  275. .offset_words = 0,
  276. .offset_bits = 0,
  277. .size_bits = 64 },
  278. { OPA_PATH_REC_FIELD(dgid),
  279. .offset_words = 2,
  280. .offset_bits = 0,
  281. .size_bits = 128 },
  282. { OPA_PATH_REC_FIELD(sgid),
  283. .offset_words = 6,
  284. .offset_bits = 0,
  285. .size_bits = 128 },
  286. { OPA_PATH_REC_FIELD(opa.dlid),
  287. .offset_words = 10,
  288. .offset_bits = 0,
  289. .size_bits = 32 },
  290. { OPA_PATH_REC_FIELD(opa.slid),
  291. .offset_words = 11,
  292. .offset_bits = 0,
  293. .size_bits = 32 },
  294. { OPA_PATH_REC_FIELD(opa.raw_traffic),
  295. .offset_words = 12,
  296. .offset_bits = 0,
  297. .size_bits = 1 },
  298. { RESERVED,
  299. .offset_words = 12,
  300. .offset_bits = 1,
  301. .size_bits = 3 },
  302. { OPA_PATH_REC_FIELD(flow_label),
  303. .offset_words = 12,
  304. .offset_bits = 4,
  305. .size_bits = 20 },
  306. { OPA_PATH_REC_FIELD(hop_limit),
  307. .offset_words = 12,
  308. .offset_bits = 24,
  309. .size_bits = 8 },
  310. { OPA_PATH_REC_FIELD(traffic_class),
  311. .offset_words = 13,
  312. .offset_bits = 0,
  313. .size_bits = 8 },
  314. { OPA_PATH_REC_FIELD(reversible),
  315. .offset_words = 13,
  316. .offset_bits = 8,
  317. .size_bits = 1 },
  318. { OPA_PATH_REC_FIELD(numb_path),
  319. .offset_words = 13,
  320. .offset_bits = 9,
  321. .size_bits = 7 },
  322. { OPA_PATH_REC_FIELD(pkey),
  323. .offset_words = 13,
  324. .offset_bits = 16,
  325. .size_bits = 16 },
  326. { OPA_PATH_REC_FIELD(opa.l2_8B),
  327. .offset_words = 14,
  328. .offset_bits = 0,
  329. .size_bits = 1 },
  330. { OPA_PATH_REC_FIELD(opa.l2_10B),
  331. .offset_words = 14,
  332. .offset_bits = 1,
  333. .size_bits = 1 },
  334. { OPA_PATH_REC_FIELD(opa.l2_9B),
  335. .offset_words = 14,
  336. .offset_bits = 2,
  337. .size_bits = 1 },
  338. { OPA_PATH_REC_FIELD(opa.l2_16B),
  339. .offset_words = 14,
  340. .offset_bits = 3,
  341. .size_bits = 1 },
  342. { RESERVED,
  343. .offset_words = 14,
  344. .offset_bits = 4,
  345. .size_bits = 2 },
  346. { OPA_PATH_REC_FIELD(opa.qos_type),
  347. .offset_words = 14,
  348. .offset_bits = 6,
  349. .size_bits = 2 },
  350. { OPA_PATH_REC_FIELD(opa.qos_priority),
  351. .offset_words = 14,
  352. .offset_bits = 8,
  353. .size_bits = 8 },
  354. { RESERVED,
  355. .offset_words = 14,
  356. .offset_bits = 16,
  357. .size_bits = 3 },
  358. { OPA_PATH_REC_FIELD(sl),
  359. .offset_words = 14,
  360. .offset_bits = 19,
  361. .size_bits = 5 },
  362. { RESERVED,
  363. .offset_words = 14,
  364. .offset_bits = 24,
  365. .size_bits = 8 },
  366. { OPA_PATH_REC_FIELD(mtu_selector),
  367. .offset_words = 15,
  368. .offset_bits = 0,
  369. .size_bits = 2 },
  370. { OPA_PATH_REC_FIELD(mtu),
  371. .offset_words = 15,
  372. .offset_bits = 2,
  373. .size_bits = 6 },
  374. { OPA_PATH_REC_FIELD(rate_selector),
  375. .offset_words = 15,
  376. .offset_bits = 8,
  377. .size_bits = 2 },
  378. { OPA_PATH_REC_FIELD(rate),
  379. .offset_words = 15,
  380. .offset_bits = 10,
  381. .size_bits = 6 },
  382. { OPA_PATH_REC_FIELD(packet_life_time_selector),
  383. .offset_words = 15,
  384. .offset_bits = 16,
  385. .size_bits = 2 },
  386. { OPA_PATH_REC_FIELD(packet_life_time),
  387. .offset_words = 15,
  388. .offset_bits = 18,
  389. .size_bits = 6 },
  390. { OPA_PATH_REC_FIELD(preference),
  391. .offset_words = 15,
  392. .offset_bits = 24,
  393. .size_bits = 8 },
  394. };
  395. #define MCMEMBER_REC_FIELD(field) \
  396. .struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field), \
  397. .struct_size_bytes = sizeof_field(struct ib_sa_mcmember_rec, field), \
  398. .field_name = "sa_mcmember_rec:" #field
  399. static const struct ib_field mcmember_rec_table[] = {
  400. { MCMEMBER_REC_FIELD(mgid),
  401. .offset_words = 0,
  402. .offset_bits = 0,
  403. .size_bits = 128 },
  404. { MCMEMBER_REC_FIELD(port_gid),
  405. .offset_words = 4,
  406. .offset_bits = 0,
  407. .size_bits = 128 },
  408. { MCMEMBER_REC_FIELD(qkey),
  409. .offset_words = 8,
  410. .offset_bits = 0,
  411. .size_bits = 32 },
  412. { MCMEMBER_REC_FIELD(mlid),
  413. .offset_words = 9,
  414. .offset_bits = 0,
  415. .size_bits = 16 },
  416. { MCMEMBER_REC_FIELD(mtu_selector),
  417. .offset_words = 9,
  418. .offset_bits = 16,
  419. .size_bits = 2 },
  420. { MCMEMBER_REC_FIELD(mtu),
  421. .offset_words = 9,
  422. .offset_bits = 18,
  423. .size_bits = 6 },
  424. { MCMEMBER_REC_FIELD(traffic_class),
  425. .offset_words = 9,
  426. .offset_bits = 24,
  427. .size_bits = 8 },
  428. { MCMEMBER_REC_FIELD(pkey),
  429. .offset_words = 10,
  430. .offset_bits = 0,
  431. .size_bits = 16 },
  432. { MCMEMBER_REC_FIELD(rate_selector),
  433. .offset_words = 10,
  434. .offset_bits = 16,
  435. .size_bits = 2 },
  436. { MCMEMBER_REC_FIELD(rate),
  437. .offset_words = 10,
  438. .offset_bits = 18,
  439. .size_bits = 6 },
  440. { MCMEMBER_REC_FIELD(packet_life_time_selector),
  441. .offset_words = 10,
  442. .offset_bits = 24,
  443. .size_bits = 2 },
  444. { MCMEMBER_REC_FIELD(packet_life_time),
  445. .offset_words = 10,
  446. .offset_bits = 26,
  447. .size_bits = 6 },
  448. { MCMEMBER_REC_FIELD(sl),
  449. .offset_words = 11,
  450. .offset_bits = 0,
  451. .size_bits = 4 },
  452. { MCMEMBER_REC_FIELD(flow_label),
  453. .offset_words = 11,
  454. .offset_bits = 4,
  455. .size_bits = 20 },
  456. { MCMEMBER_REC_FIELD(hop_limit),
  457. .offset_words = 11,
  458. .offset_bits = 24,
  459. .size_bits = 8 },
  460. { MCMEMBER_REC_FIELD(scope),
  461. .offset_words = 12,
  462. .offset_bits = 0,
  463. .size_bits = 4 },
  464. { MCMEMBER_REC_FIELD(join_state),
  465. .offset_words = 12,
  466. .offset_bits = 4,
  467. .size_bits = 4 },
  468. { MCMEMBER_REC_FIELD(proxy_join),
  469. .offset_words = 12,
  470. .offset_bits = 8,
  471. .size_bits = 1 },
  472. { RESERVED,
  473. .offset_words = 12,
  474. .offset_bits = 9,
  475. .size_bits = 23 },
  476. };
  477. #define SERVICE_REC_FIELD(field) \
  478. .struct_offset_bytes = offsetof(struct ib_sa_service_rec, field), \
  479. .struct_size_bytes = sizeof_field(struct ib_sa_service_rec, field), \
  480. .field_name = "sa_service_rec:" #field
  481. static const struct ib_field service_rec_table[] = {
  482. { SERVICE_REC_FIELD(id),
  483. .offset_words = 0,
  484. .offset_bits = 0,
  485. .size_bits = 64 },
  486. { SERVICE_REC_FIELD(gid),
  487. .offset_words = 2,
  488. .offset_bits = 0,
  489. .size_bits = 128 },
  490. { SERVICE_REC_FIELD(pkey),
  491. .offset_words = 6,
  492. .offset_bits = 0,
  493. .size_bits = 16 },
  494. { SERVICE_REC_FIELD(lease),
  495. .offset_words = 7,
  496. .offset_bits = 0,
  497. .size_bits = 32 },
  498. { SERVICE_REC_FIELD(key),
  499. .offset_words = 8,
  500. .offset_bits = 0,
  501. .size_bits = 128 },
  502. { SERVICE_REC_FIELD(name),
  503. .offset_words = 12,
  504. .offset_bits = 0,
  505. .size_bits = 64*8 },
  506. { SERVICE_REC_FIELD(data8),
  507. .offset_words = 28,
  508. .offset_bits = 0,
  509. .size_bits = 16*8 },
  510. { SERVICE_REC_FIELD(data16),
  511. .offset_words = 32,
  512. .offset_bits = 0,
  513. .size_bits = 8*16 },
  514. { SERVICE_REC_FIELD(data32),
  515. .offset_words = 36,
  516. .offset_bits = 0,
  517. .size_bits = 4*32 },
  518. { SERVICE_REC_FIELD(data64),
  519. .offset_words = 40,
  520. .offset_bits = 0,
  521. .size_bits = 2*64 },
  522. };
  523. #define CLASSPORTINFO_REC_FIELD(field) \
  524. .struct_offset_bytes = offsetof(struct ib_class_port_info, field), \
  525. .struct_size_bytes = sizeof_field(struct ib_class_port_info, field), \
  526. .field_name = "ib_class_port_info:" #field
  527. static const struct ib_field ib_classport_info_rec_table[] = {
  528. { CLASSPORTINFO_REC_FIELD(base_version),
  529. .offset_words = 0,
  530. .offset_bits = 0,
  531. .size_bits = 8 },
  532. { CLASSPORTINFO_REC_FIELD(class_version),
  533. .offset_words = 0,
  534. .offset_bits = 8,
  535. .size_bits = 8 },
  536. { CLASSPORTINFO_REC_FIELD(capability_mask),
  537. .offset_words = 0,
  538. .offset_bits = 16,
  539. .size_bits = 16 },
  540. { CLASSPORTINFO_REC_FIELD(cap_mask2_resp_time),
  541. .offset_words = 1,
  542. .offset_bits = 0,
  543. .size_bits = 32 },
  544. { CLASSPORTINFO_REC_FIELD(redirect_gid),
  545. .offset_words = 2,
  546. .offset_bits = 0,
  547. .size_bits = 128 },
  548. { CLASSPORTINFO_REC_FIELD(redirect_tcslfl),
  549. .offset_words = 6,
  550. .offset_bits = 0,
  551. .size_bits = 32 },
  552. { CLASSPORTINFO_REC_FIELD(redirect_lid),
  553. .offset_words = 7,
  554. .offset_bits = 0,
  555. .size_bits = 16 },
  556. { CLASSPORTINFO_REC_FIELD(redirect_pkey),
  557. .offset_words = 7,
  558. .offset_bits = 16,
  559. .size_bits = 16 },
  560. { CLASSPORTINFO_REC_FIELD(redirect_qp),
  561. .offset_words = 8,
  562. .offset_bits = 0,
  563. .size_bits = 32 },
  564. { CLASSPORTINFO_REC_FIELD(redirect_qkey),
  565. .offset_words = 9,
  566. .offset_bits = 0,
  567. .size_bits = 32 },
  568. { CLASSPORTINFO_REC_FIELD(trap_gid),
  569. .offset_words = 10,
  570. .offset_bits = 0,
  571. .size_bits = 128 },
  572. { CLASSPORTINFO_REC_FIELD(trap_tcslfl),
  573. .offset_words = 14,
  574. .offset_bits = 0,
  575. .size_bits = 32 },
  576. { CLASSPORTINFO_REC_FIELD(trap_lid),
  577. .offset_words = 15,
  578. .offset_bits = 0,
  579. .size_bits = 16 },
  580. { CLASSPORTINFO_REC_FIELD(trap_pkey),
  581. .offset_words = 15,
  582. .offset_bits = 16,
  583. .size_bits = 16 },
  584. { CLASSPORTINFO_REC_FIELD(trap_hlqp),
  585. .offset_words = 16,
  586. .offset_bits = 0,
  587. .size_bits = 32 },
  588. { CLASSPORTINFO_REC_FIELD(trap_qkey),
  589. .offset_words = 17,
  590. .offset_bits = 0,
  591. .size_bits = 32 },
  592. };
  593. #define OPA_CLASSPORTINFO_REC_FIELD(field) \
  594. .struct_offset_bytes =\
  595. offsetof(struct opa_class_port_info, field), \
  596. .struct_size_bytes = \
  597. sizeof_field(struct opa_class_port_info, field), \
  598. .field_name = "opa_class_port_info:" #field
  599. static const struct ib_field opa_classport_info_rec_table[] = {
  600. { OPA_CLASSPORTINFO_REC_FIELD(base_version),
  601. .offset_words = 0,
  602. .offset_bits = 0,
  603. .size_bits = 8 },
  604. { OPA_CLASSPORTINFO_REC_FIELD(class_version),
  605. .offset_words = 0,
  606. .offset_bits = 8,
  607. .size_bits = 8 },
  608. { OPA_CLASSPORTINFO_REC_FIELD(cap_mask),
  609. .offset_words = 0,
  610. .offset_bits = 16,
  611. .size_bits = 16 },
  612. { OPA_CLASSPORTINFO_REC_FIELD(cap_mask2_resp_time),
  613. .offset_words = 1,
  614. .offset_bits = 0,
  615. .size_bits = 32 },
  616. { OPA_CLASSPORTINFO_REC_FIELD(redirect_gid),
  617. .offset_words = 2,
  618. .offset_bits = 0,
  619. .size_bits = 128 },
  620. { OPA_CLASSPORTINFO_REC_FIELD(redirect_tc_fl),
  621. .offset_words = 6,
  622. .offset_bits = 0,
  623. .size_bits = 32 },
  624. { OPA_CLASSPORTINFO_REC_FIELD(redirect_lid),
  625. .offset_words = 7,
  626. .offset_bits = 0,
  627. .size_bits = 32 },
  628. { OPA_CLASSPORTINFO_REC_FIELD(redirect_sl_qp),
  629. .offset_words = 8,
  630. .offset_bits = 0,
  631. .size_bits = 32 },
  632. { OPA_CLASSPORTINFO_REC_FIELD(redirect_qkey),
  633. .offset_words = 9,
  634. .offset_bits = 0,
  635. .size_bits = 32 },
  636. { OPA_CLASSPORTINFO_REC_FIELD(trap_gid),
  637. .offset_words = 10,
  638. .offset_bits = 0,
  639. .size_bits = 128 },
  640. { OPA_CLASSPORTINFO_REC_FIELD(trap_tc_fl),
  641. .offset_words = 14,
  642. .offset_bits = 0,
  643. .size_bits = 32 },
  644. { OPA_CLASSPORTINFO_REC_FIELD(trap_lid),
  645. .offset_words = 15,
  646. .offset_bits = 0,
  647. .size_bits = 32 },
  648. { OPA_CLASSPORTINFO_REC_FIELD(trap_hl_qp),
  649. .offset_words = 16,
  650. .offset_bits = 0,
  651. .size_bits = 32 },
  652. { OPA_CLASSPORTINFO_REC_FIELD(trap_qkey),
  653. .offset_words = 17,
  654. .offset_bits = 0,
  655. .size_bits = 32 },
  656. { OPA_CLASSPORTINFO_REC_FIELD(trap_pkey),
  657. .offset_words = 18,
  658. .offset_bits = 0,
  659. .size_bits = 16 },
  660. { OPA_CLASSPORTINFO_REC_FIELD(redirect_pkey),
  661. .offset_words = 18,
  662. .offset_bits = 16,
  663. .size_bits = 16 },
  664. { OPA_CLASSPORTINFO_REC_FIELD(trap_sl_rsvd),
  665. .offset_words = 19,
  666. .offset_bits = 0,
  667. .size_bits = 8 },
  668. { RESERVED,
  669. .offset_words = 19,
  670. .offset_bits = 8,
  671. .size_bits = 24 },
  672. };
  673. #define GUIDINFO_REC_FIELD(field) \
  674. .struct_offset_bytes = offsetof(struct ib_sa_guidinfo_rec, field), \
  675. .struct_size_bytes = sizeof_field(struct ib_sa_guidinfo_rec, field), \
  676. .field_name = "sa_guidinfo_rec:" #field
  677. static const struct ib_field guidinfo_rec_table[] = {
  678. { GUIDINFO_REC_FIELD(lid),
  679. .offset_words = 0,
  680. .offset_bits = 0,
  681. .size_bits = 16 },
  682. { GUIDINFO_REC_FIELD(block_num),
  683. .offset_words = 0,
  684. .offset_bits = 16,
  685. .size_bits = 8 },
  686. { GUIDINFO_REC_FIELD(res1),
  687. .offset_words = 0,
  688. .offset_bits = 24,
  689. .size_bits = 8 },
  690. { GUIDINFO_REC_FIELD(res2),
  691. .offset_words = 1,
  692. .offset_bits = 0,
  693. .size_bits = 32 },
  694. { GUIDINFO_REC_FIELD(guid_info_list),
  695. .offset_words = 2,
  696. .offset_bits = 0,
  697. .size_bits = 512 },
  698. };
  699. static inline void ib_sa_disable_local_svc(struct ib_sa_query *query)
  700. {
  701. query->flags &= ~IB_SA_ENABLE_LOCAL_SERVICE;
  702. }
  703. static inline int ib_sa_query_cancelled(struct ib_sa_query *query)
  704. {
  705. return (query->flags & IB_SA_CANCEL);
  706. }
  707. static void ib_nl_set_path_rec_attrs(struct sk_buff *skb,
  708. struct ib_sa_query *query)
  709. {
  710. struct sa_path_rec *sa_rec = query->mad_buf->context[1];
  711. struct ib_sa_mad *mad = query->mad_buf->mad;
  712. ib_sa_comp_mask comp_mask = mad->sa_hdr.comp_mask;
  713. u16 val16;
  714. u64 val64;
  715. struct rdma_ls_resolve_header *header;
  716. query->mad_buf->context[1] = NULL;
  717. /* Construct the family header first */
  718. header = skb_put(skb, NLMSG_ALIGN(sizeof(*header)));
  719. strscpy_pad(header->device_name,
  720. dev_name(&query->port->agent->device->dev),
  721. LS_DEVICE_NAME_MAX);
  722. header->port_num = query->port->port_num;
  723. if ((comp_mask & IB_SA_PATH_REC_REVERSIBLE) &&
  724. sa_rec->reversible != 0)
  725. query->path_use = LS_RESOLVE_PATH_USE_GMP;
  726. else
  727. query->path_use = LS_RESOLVE_PATH_USE_UNIDIRECTIONAL;
  728. header->path_use = query->path_use;
  729. /* Now build the attributes */
  730. if (comp_mask & IB_SA_PATH_REC_SERVICE_ID) {
  731. val64 = be64_to_cpu(sa_rec->service_id);
  732. nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_SERVICE_ID,
  733. sizeof(val64), &val64);
  734. }
  735. if (comp_mask & IB_SA_PATH_REC_DGID)
  736. nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_DGID,
  737. sizeof(sa_rec->dgid), &sa_rec->dgid);
  738. if (comp_mask & IB_SA_PATH_REC_SGID)
  739. nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_SGID,
  740. sizeof(sa_rec->sgid), &sa_rec->sgid);
  741. if (comp_mask & IB_SA_PATH_REC_TRAFFIC_CLASS)
  742. nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_TCLASS,
  743. sizeof(sa_rec->traffic_class), &sa_rec->traffic_class);
  744. if (comp_mask & IB_SA_PATH_REC_PKEY) {
  745. val16 = be16_to_cpu(sa_rec->pkey);
  746. nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_PKEY,
  747. sizeof(val16), &val16);
  748. }
  749. if (comp_mask & IB_SA_PATH_REC_QOS_CLASS) {
  750. val16 = be16_to_cpu(sa_rec->qos_class);
  751. nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_QOS_CLASS,
  752. sizeof(val16), &val16);
  753. }
  754. }
  755. static int ib_nl_get_path_rec_attrs_len(ib_sa_comp_mask comp_mask)
  756. {
  757. int len = 0;
  758. if (comp_mask & IB_SA_PATH_REC_SERVICE_ID)
  759. len += nla_total_size(sizeof(u64));
  760. if (comp_mask & IB_SA_PATH_REC_DGID)
  761. len += nla_total_size(sizeof(struct rdma_nla_ls_gid));
  762. if (comp_mask & IB_SA_PATH_REC_SGID)
  763. len += nla_total_size(sizeof(struct rdma_nla_ls_gid));
  764. if (comp_mask & IB_SA_PATH_REC_TRAFFIC_CLASS)
  765. len += nla_total_size(sizeof(u8));
  766. if (comp_mask & IB_SA_PATH_REC_PKEY)
  767. len += nla_total_size(sizeof(u16));
  768. if (comp_mask & IB_SA_PATH_REC_QOS_CLASS)
  769. len += nla_total_size(sizeof(u16));
  770. /*
  771. * Make sure that at least some of the required comp_mask bits are
  772. * set.
  773. */
  774. if (WARN_ON(len == 0))
  775. return len;
  776. /* Add the family header */
  777. len += NLMSG_ALIGN(sizeof(struct rdma_ls_resolve_header));
  778. return len;
  779. }
  780. static int ib_nl_make_request(struct ib_sa_query *query, gfp_t gfp_mask)
  781. {
  782. struct sk_buff *skb = NULL;
  783. struct nlmsghdr *nlh;
  784. void *data;
  785. struct ib_sa_mad *mad;
  786. int len;
  787. unsigned long flags;
  788. unsigned long delay;
  789. gfp_t gfp_flag;
  790. int ret;
  791. INIT_LIST_HEAD(&query->list);
  792. query->seq = (u32)atomic_inc_return(&ib_nl_sa_request_seq);
  793. mad = query->mad_buf->mad;
  794. len = ib_nl_get_path_rec_attrs_len(mad->sa_hdr.comp_mask);
  795. if (len <= 0)
  796. return -EMSGSIZE;
  797. skb = nlmsg_new(len, gfp_mask);
  798. if (!skb)
  799. return -ENOMEM;
  800. /* Put nlmsg header only for now */
  801. data = ibnl_put_msg(skb, &nlh, query->seq, 0, RDMA_NL_LS,
  802. RDMA_NL_LS_OP_RESOLVE, NLM_F_REQUEST);
  803. if (!data) {
  804. nlmsg_free(skb);
  805. return -EMSGSIZE;
  806. }
  807. /* Add attributes */
  808. ib_nl_set_path_rec_attrs(skb, query);
  809. /* Repair the nlmsg header length */
  810. nlmsg_end(skb, nlh);
  811. gfp_flag = ((gfp_mask & GFP_ATOMIC) == GFP_ATOMIC) ? GFP_ATOMIC :
  812. GFP_NOWAIT;
  813. spin_lock_irqsave(&ib_nl_request_lock, flags);
  814. ret = rdma_nl_multicast(&init_net, skb, RDMA_NL_GROUP_LS, gfp_flag);
  815. if (ret)
  816. goto out;
  817. /* Put the request on the list.*/
  818. delay = msecs_to_jiffies(sa_local_svc_timeout_ms);
  819. query->timeout = delay + jiffies;
  820. list_add_tail(&query->list, &ib_nl_request_list);
  821. /* Start the timeout if this is the only request */
  822. if (ib_nl_request_list.next == &query->list)
  823. queue_delayed_work(ib_nl_wq, &ib_nl_timed_work, delay);
  824. out:
  825. spin_unlock_irqrestore(&ib_nl_request_lock, flags);
  826. return ret;
  827. }
  828. static int ib_nl_cancel_request(struct ib_sa_query *query)
  829. {
  830. unsigned long flags;
  831. struct ib_sa_query *wait_query;
  832. int found = 0;
  833. spin_lock_irqsave(&ib_nl_request_lock, flags);
  834. list_for_each_entry(wait_query, &ib_nl_request_list, list) {
  835. /* Let the timeout to take care of the callback */
  836. if (query == wait_query) {
  837. query->flags |= IB_SA_CANCEL;
  838. query->timeout = jiffies;
  839. list_move(&query->list, &ib_nl_request_list);
  840. found = 1;
  841. mod_delayed_work(ib_nl_wq, &ib_nl_timed_work, 1);
  842. break;
  843. }
  844. }
  845. spin_unlock_irqrestore(&ib_nl_request_lock, flags);
  846. return found;
  847. }
  848. static void send_handler(struct ib_mad_agent *agent,
  849. struct ib_mad_send_wc *mad_send_wc);
  850. static void ib_nl_process_good_resolve_rsp(struct ib_sa_query *query,
  851. const struct nlmsghdr *nlh)
  852. {
  853. struct ib_mad_send_wc mad_send_wc;
  854. struct ib_sa_mad *mad = NULL;
  855. const struct nlattr *head, *curr;
  856. struct ib_path_rec_data *rec;
  857. int len, rem;
  858. u32 mask = 0;
  859. int status = -EIO;
  860. if (query->callback) {
  861. head = (const struct nlattr *) nlmsg_data(nlh);
  862. len = nlmsg_len(nlh);
  863. switch (query->path_use) {
  864. case LS_RESOLVE_PATH_USE_UNIDIRECTIONAL:
  865. mask = IB_PATH_PRIMARY | IB_PATH_OUTBOUND;
  866. break;
  867. case LS_RESOLVE_PATH_USE_ALL:
  868. case LS_RESOLVE_PATH_USE_GMP:
  869. default:
  870. mask = IB_PATH_PRIMARY | IB_PATH_GMP |
  871. IB_PATH_BIDIRECTIONAL;
  872. break;
  873. }
  874. nla_for_each_attr(curr, head, len, rem) {
  875. if (curr->nla_type == LS_NLA_TYPE_PATH_RECORD) {
  876. rec = nla_data(curr);
  877. /*
  878. * Get the first one. In the future, we may
  879. * need to get up to 6 pathrecords.
  880. */
  881. if ((rec->flags & mask) == mask) {
  882. mad = query->mad_buf->mad;
  883. mad->mad_hdr.method |=
  884. IB_MGMT_METHOD_RESP;
  885. memcpy(mad->data, rec->path_rec,
  886. sizeof(rec->path_rec));
  887. status = 0;
  888. break;
  889. }
  890. }
  891. }
  892. query->callback(query, status, mad);
  893. }
  894. mad_send_wc.send_buf = query->mad_buf;
  895. mad_send_wc.status = IB_WC_SUCCESS;
  896. send_handler(query->mad_buf->mad_agent, &mad_send_wc);
  897. }
  898. static void ib_nl_request_timeout(struct work_struct *work)
  899. {
  900. unsigned long flags;
  901. struct ib_sa_query *query;
  902. unsigned long delay;
  903. struct ib_mad_send_wc mad_send_wc;
  904. int ret;
  905. spin_lock_irqsave(&ib_nl_request_lock, flags);
  906. while (!list_empty(&ib_nl_request_list)) {
  907. query = list_entry(ib_nl_request_list.next,
  908. struct ib_sa_query, list);
  909. if (time_after(query->timeout, jiffies)) {
  910. delay = query->timeout - jiffies;
  911. if ((long)delay <= 0)
  912. delay = 1;
  913. queue_delayed_work(ib_nl_wq, &ib_nl_timed_work, delay);
  914. break;
  915. }
  916. list_del(&query->list);
  917. ib_sa_disable_local_svc(query);
  918. /* Hold the lock to protect against query cancellation */
  919. if (ib_sa_query_cancelled(query))
  920. ret = -1;
  921. else
  922. ret = ib_post_send_mad(query->mad_buf, NULL);
  923. if (ret) {
  924. mad_send_wc.send_buf = query->mad_buf;
  925. mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
  926. spin_unlock_irqrestore(&ib_nl_request_lock, flags);
  927. send_handler(query->port->agent, &mad_send_wc);
  928. spin_lock_irqsave(&ib_nl_request_lock, flags);
  929. }
  930. }
  931. spin_unlock_irqrestore(&ib_nl_request_lock, flags);
  932. }
  933. int ib_nl_handle_set_timeout(struct sk_buff *skb,
  934. struct nlmsghdr *nlh,
  935. struct netlink_ext_ack *extack)
  936. {
  937. int timeout, delta, abs_delta;
  938. const struct nlattr *attr;
  939. unsigned long flags;
  940. struct ib_sa_query *query;
  941. long delay = 0;
  942. struct nlattr *tb[LS_NLA_TYPE_MAX];
  943. int ret;
  944. if (!(nlh->nlmsg_flags & NLM_F_REQUEST) ||
  945. !(NETLINK_CB(skb).sk))
  946. return -EPERM;
  947. ret = nla_parse_deprecated(tb, LS_NLA_TYPE_MAX - 1, nlmsg_data(nlh),
  948. nlmsg_len(nlh), ib_nl_policy, NULL);
  949. attr = (const struct nlattr *)tb[LS_NLA_TYPE_TIMEOUT];
  950. if (ret || !attr)
  951. goto settimeout_out;
  952. timeout = *(int *) nla_data(attr);
  953. if (timeout < IB_SA_LOCAL_SVC_TIMEOUT_MIN)
  954. timeout = IB_SA_LOCAL_SVC_TIMEOUT_MIN;
  955. if (timeout > IB_SA_LOCAL_SVC_TIMEOUT_MAX)
  956. timeout = IB_SA_LOCAL_SVC_TIMEOUT_MAX;
  957. delta = timeout - sa_local_svc_timeout_ms;
  958. if (delta < 0)
  959. abs_delta = -delta;
  960. else
  961. abs_delta = delta;
  962. if (delta != 0) {
  963. spin_lock_irqsave(&ib_nl_request_lock, flags);
  964. sa_local_svc_timeout_ms = timeout;
  965. list_for_each_entry(query, &ib_nl_request_list, list) {
  966. if (delta < 0 && abs_delta > query->timeout)
  967. query->timeout = 0;
  968. else
  969. query->timeout += delta;
  970. /* Get the new delay from the first entry */
  971. if (!delay) {
  972. delay = query->timeout - jiffies;
  973. if (delay <= 0)
  974. delay = 1;
  975. }
  976. }
  977. if (delay)
  978. mod_delayed_work(ib_nl_wq, &ib_nl_timed_work,
  979. (unsigned long)delay);
  980. spin_unlock_irqrestore(&ib_nl_request_lock, flags);
  981. }
  982. settimeout_out:
  983. return 0;
  984. }
  985. static inline int ib_nl_is_good_resolve_resp(const struct nlmsghdr *nlh)
  986. {
  987. struct nlattr *tb[LS_NLA_TYPE_MAX];
  988. int ret;
  989. if (nlh->nlmsg_flags & RDMA_NL_LS_F_ERR)
  990. return 0;
  991. ret = nla_parse_deprecated(tb, LS_NLA_TYPE_MAX - 1, nlmsg_data(nlh),
  992. nlmsg_len(nlh), ib_nl_policy, NULL);
  993. if (ret)
  994. return 0;
  995. return 1;
  996. }
  997. int ib_nl_handle_resolve_resp(struct sk_buff *skb,
  998. struct nlmsghdr *nlh,
  999. struct netlink_ext_ack *extack)
  1000. {
  1001. unsigned long flags;
  1002. struct ib_sa_query *query;
  1003. struct ib_mad_send_buf *send_buf;
  1004. struct ib_mad_send_wc mad_send_wc;
  1005. int found = 0;
  1006. int ret;
  1007. if ((nlh->nlmsg_flags & NLM_F_REQUEST) ||
  1008. !(NETLINK_CB(skb).sk))
  1009. return -EPERM;
  1010. spin_lock_irqsave(&ib_nl_request_lock, flags);
  1011. list_for_each_entry(query, &ib_nl_request_list, list) {
  1012. /*
  1013. * If the query is cancelled, let the timeout routine
  1014. * take care of it.
  1015. */
  1016. if (nlh->nlmsg_seq == query->seq) {
  1017. found = !ib_sa_query_cancelled(query);
  1018. if (found)
  1019. list_del(&query->list);
  1020. break;
  1021. }
  1022. }
  1023. if (!found) {
  1024. spin_unlock_irqrestore(&ib_nl_request_lock, flags);
  1025. goto resp_out;
  1026. }
  1027. send_buf = query->mad_buf;
  1028. if (!ib_nl_is_good_resolve_resp(nlh)) {
  1029. /* if the result is a failure, send out the packet via IB */
  1030. ib_sa_disable_local_svc(query);
  1031. ret = ib_post_send_mad(query->mad_buf, NULL);
  1032. spin_unlock_irqrestore(&ib_nl_request_lock, flags);
  1033. if (ret) {
  1034. mad_send_wc.send_buf = send_buf;
  1035. mad_send_wc.status = IB_WC_GENERAL_ERR;
  1036. send_handler(query->port->agent, &mad_send_wc);
  1037. }
  1038. } else {
  1039. spin_unlock_irqrestore(&ib_nl_request_lock, flags);
  1040. ib_nl_process_good_resolve_rsp(query, nlh);
  1041. }
  1042. resp_out:
  1043. return 0;
  1044. }
  1045. static void free_sm_ah(struct kref *kref)
  1046. {
  1047. struct ib_sa_sm_ah *sm_ah = container_of(kref, struct ib_sa_sm_ah, ref);
  1048. rdma_destroy_ah(sm_ah->ah, 0);
  1049. kfree(sm_ah);
  1050. }
  1051. void ib_sa_register_client(struct ib_sa_client *client)
  1052. {
  1053. atomic_set(&client->users, 1);
  1054. init_completion(&client->comp);
  1055. }
  1056. EXPORT_SYMBOL(ib_sa_register_client);
  1057. void ib_sa_unregister_client(struct ib_sa_client *client)
  1058. {
  1059. ib_sa_client_put(client);
  1060. wait_for_completion(&client->comp);
  1061. }
  1062. EXPORT_SYMBOL(ib_sa_unregister_client);
  1063. /**
  1064. * ib_sa_cancel_query - try to cancel an SA query
  1065. * @id:ID of query to cancel
  1066. * @query:query pointer to cancel
  1067. *
  1068. * Try to cancel an SA query. If the id and query don't match up or
  1069. * the query has already completed, nothing is done. Otherwise the
  1070. * query is canceled and will complete with a status of -EINTR.
  1071. */
  1072. void ib_sa_cancel_query(int id, struct ib_sa_query *query)
  1073. {
  1074. unsigned long flags;
  1075. struct ib_mad_agent *agent;
  1076. struct ib_mad_send_buf *mad_buf;
  1077. xa_lock_irqsave(&queries, flags);
  1078. if (xa_load(&queries, id) != query) {
  1079. xa_unlock_irqrestore(&queries, flags);
  1080. return;
  1081. }
  1082. agent = query->port->agent;
  1083. mad_buf = query->mad_buf;
  1084. xa_unlock_irqrestore(&queries, flags);
  1085. /*
  1086. * If the query is still on the netlink request list, schedule
  1087. * it to be cancelled by the timeout routine. Otherwise, it has been
  1088. * sent to the MAD layer and has to be cancelled from there.
  1089. */
  1090. if (!ib_nl_cancel_request(query))
  1091. ib_cancel_mad(agent, mad_buf);
  1092. }
  1093. EXPORT_SYMBOL(ib_sa_cancel_query);
  1094. static u8 get_src_path_mask(struct ib_device *device, u8 port_num)
  1095. {
  1096. struct ib_sa_device *sa_dev;
  1097. struct ib_sa_port *port;
  1098. unsigned long flags;
  1099. u8 src_path_mask;
  1100. sa_dev = ib_get_client_data(device, &sa_client);
  1101. if (!sa_dev)
  1102. return 0x7f;
  1103. port = &sa_dev->port[port_num - sa_dev->start_port];
  1104. spin_lock_irqsave(&port->ah_lock, flags);
  1105. src_path_mask = port->sm_ah ? port->sm_ah->src_path_mask : 0x7f;
  1106. spin_unlock_irqrestore(&port->ah_lock, flags);
  1107. return src_path_mask;
  1108. }
  1109. static int init_ah_attr_grh_fields(struct ib_device *device, u8 port_num,
  1110. struct sa_path_rec *rec,
  1111. struct rdma_ah_attr *ah_attr,
  1112. const struct ib_gid_attr *gid_attr)
  1113. {
  1114. enum ib_gid_type type = sa_conv_pathrec_to_gid_type(rec);
  1115. if (!gid_attr) {
  1116. gid_attr = rdma_find_gid_by_port(device, &rec->sgid, type,
  1117. port_num, NULL);
  1118. if (IS_ERR(gid_attr))
  1119. return PTR_ERR(gid_attr);
  1120. } else
  1121. rdma_hold_gid_attr(gid_attr);
  1122. rdma_move_grh_sgid_attr(ah_attr, &rec->dgid,
  1123. be32_to_cpu(rec->flow_label),
  1124. rec->hop_limit, rec->traffic_class,
  1125. gid_attr);
  1126. return 0;
  1127. }
  1128. /**
  1129. * ib_init_ah_attr_from_path - Initialize address handle attributes based on
  1130. * an SA path record.
  1131. * @device: Device associated ah attributes initialization.
  1132. * @port_num: Port on the specified device.
  1133. * @rec: path record entry to use for ah attributes initialization.
  1134. * @ah_attr: address handle attributes to initialization from path record.
  1135. * @gid_attr: SGID attribute to consider during initialization.
  1136. *
  1137. * When ib_init_ah_attr_from_path() returns success,
  1138. * (a) for IB link layer it optionally contains a reference to SGID attribute
  1139. * when GRH is present for IB link layer.
  1140. * (b) for RoCE link layer it contains a reference to SGID attribute.
  1141. * User must invoke rdma_destroy_ah_attr() to release reference to SGID
  1142. * attributes which are initialized using ib_init_ah_attr_from_path().
  1143. */
  1144. int ib_init_ah_attr_from_path(struct ib_device *device, u8 port_num,
  1145. struct sa_path_rec *rec,
  1146. struct rdma_ah_attr *ah_attr,
  1147. const struct ib_gid_attr *gid_attr)
  1148. {
  1149. int ret = 0;
  1150. memset(ah_attr, 0, sizeof(*ah_attr));
  1151. ah_attr->type = rdma_ah_find_type(device, port_num);
  1152. rdma_ah_set_sl(ah_attr, rec->sl);
  1153. rdma_ah_set_port_num(ah_attr, port_num);
  1154. rdma_ah_set_static_rate(ah_attr, rec->rate);
  1155. if (sa_path_is_roce(rec)) {
  1156. ret = roce_resolve_route_from_path(rec, gid_attr);
  1157. if (ret)
  1158. return ret;
  1159. memcpy(ah_attr->roce.dmac, sa_path_get_dmac(rec), ETH_ALEN);
  1160. } else {
  1161. rdma_ah_set_dlid(ah_attr, be32_to_cpu(sa_path_get_dlid(rec)));
  1162. if (sa_path_is_opa(rec) &&
  1163. rdma_ah_get_dlid(ah_attr) == be16_to_cpu(IB_LID_PERMISSIVE))
  1164. rdma_ah_set_make_grd(ah_attr, true);
  1165. rdma_ah_set_path_bits(ah_attr,
  1166. be32_to_cpu(sa_path_get_slid(rec)) &
  1167. get_src_path_mask(device, port_num));
  1168. }
  1169. if (rec->hop_limit > 0 || sa_path_is_roce(rec))
  1170. ret = init_ah_attr_grh_fields(device, port_num,
  1171. rec, ah_attr, gid_attr);
  1172. return ret;
  1173. }
  1174. EXPORT_SYMBOL(ib_init_ah_attr_from_path);
  1175. static int alloc_mad(struct ib_sa_query *query, gfp_t gfp_mask)
  1176. {
  1177. struct rdma_ah_attr ah_attr;
  1178. unsigned long flags;
  1179. spin_lock_irqsave(&query->port->ah_lock, flags);
  1180. if (!query->port->sm_ah) {
  1181. spin_unlock_irqrestore(&query->port->ah_lock, flags);
  1182. return -EAGAIN;
  1183. }
  1184. kref_get(&query->port->sm_ah->ref);
  1185. query->sm_ah = query->port->sm_ah;
  1186. spin_unlock_irqrestore(&query->port->ah_lock, flags);
  1187. /*
  1188. * Always check if sm_ah has valid dlid assigned,
  1189. * before querying for class port info
  1190. */
  1191. if ((rdma_query_ah(query->sm_ah->ah, &ah_attr) < 0) ||
  1192. !rdma_is_valid_unicast_lid(&ah_attr)) {
  1193. kref_put(&query->sm_ah->ref, free_sm_ah);
  1194. return -EAGAIN;
  1195. }
  1196. query->mad_buf = ib_create_send_mad(query->port->agent, 1,
  1197. query->sm_ah->pkey_index,
  1198. 0, IB_MGMT_SA_HDR, IB_MGMT_SA_DATA,
  1199. gfp_mask,
  1200. ((query->flags & IB_SA_QUERY_OPA) ?
  1201. OPA_MGMT_BASE_VERSION :
  1202. IB_MGMT_BASE_VERSION));
  1203. if (IS_ERR(query->mad_buf)) {
  1204. kref_put(&query->sm_ah->ref, free_sm_ah);
  1205. return -ENOMEM;
  1206. }
  1207. query->mad_buf->ah = query->sm_ah->ah;
  1208. return 0;
  1209. }
  1210. static void free_mad(struct ib_sa_query *query)
  1211. {
  1212. ib_free_send_mad(query->mad_buf);
  1213. kref_put(&query->sm_ah->ref, free_sm_ah);
  1214. }
  1215. static void init_mad(struct ib_sa_query *query, struct ib_mad_agent *agent)
  1216. {
  1217. struct ib_sa_mad *mad = query->mad_buf->mad;
  1218. unsigned long flags;
  1219. memset(mad, 0, sizeof *mad);
  1220. if (query->flags & IB_SA_QUERY_OPA) {
  1221. mad->mad_hdr.base_version = OPA_MGMT_BASE_VERSION;
  1222. mad->mad_hdr.class_version = OPA_SA_CLASS_VERSION;
  1223. } else {
  1224. mad->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
  1225. mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
  1226. }
  1227. mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  1228. spin_lock_irqsave(&tid_lock, flags);
  1229. mad->mad_hdr.tid =
  1230. cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
  1231. spin_unlock_irqrestore(&tid_lock, flags);
  1232. }
  1233. static int send_mad(struct ib_sa_query *query, unsigned long timeout_ms,
  1234. gfp_t gfp_mask)
  1235. {
  1236. unsigned long flags;
  1237. int ret, id;
  1238. xa_lock_irqsave(&queries, flags);
  1239. ret = __xa_alloc(&queries, &id, query, xa_limit_32b, gfp_mask);
  1240. xa_unlock_irqrestore(&queries, flags);
  1241. if (ret < 0)
  1242. return ret;
  1243. query->mad_buf->timeout_ms = timeout_ms;
  1244. query->mad_buf->context[0] = query;
  1245. query->id = id;
  1246. if ((query->flags & IB_SA_ENABLE_LOCAL_SERVICE) &&
  1247. (!(query->flags & IB_SA_QUERY_OPA))) {
  1248. if (rdma_nl_chk_listeners(RDMA_NL_GROUP_LS)) {
  1249. if (!ib_nl_make_request(query, gfp_mask))
  1250. return id;
  1251. }
  1252. ib_sa_disable_local_svc(query);
  1253. }
  1254. ret = ib_post_send_mad(query->mad_buf, NULL);
  1255. if (ret) {
  1256. xa_lock_irqsave(&queries, flags);
  1257. __xa_erase(&queries, id);
  1258. xa_unlock_irqrestore(&queries, flags);
  1259. }
  1260. /*
  1261. * It's not safe to dereference query any more, because the
  1262. * send may already have completed and freed the query in
  1263. * another context.
  1264. */
  1265. return ret ? ret : id;
  1266. }
  1267. void ib_sa_unpack_path(void *attribute, struct sa_path_rec *rec)
  1268. {
  1269. ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table), attribute, rec);
  1270. }
  1271. EXPORT_SYMBOL(ib_sa_unpack_path);
  1272. void ib_sa_pack_path(struct sa_path_rec *rec, void *attribute)
  1273. {
  1274. ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, attribute);
  1275. }
  1276. EXPORT_SYMBOL(ib_sa_pack_path);
  1277. static bool ib_sa_opa_pathrecord_support(struct ib_sa_client *client,
  1278. struct ib_sa_device *sa_dev,
  1279. u8 port_num)
  1280. {
  1281. struct ib_sa_port *port;
  1282. unsigned long flags;
  1283. bool ret = false;
  1284. port = &sa_dev->port[port_num - sa_dev->start_port];
  1285. spin_lock_irqsave(&port->classport_lock, flags);
  1286. if (!port->classport_info.valid)
  1287. goto ret;
  1288. if (port->classport_info.data.type == RDMA_CLASS_PORT_INFO_OPA)
  1289. ret = opa_get_cpi_capmask2(&port->classport_info.data.opa) &
  1290. OPA_CLASS_PORT_INFO_PR_SUPPORT;
  1291. ret:
  1292. spin_unlock_irqrestore(&port->classport_lock, flags);
  1293. return ret;
  1294. }
  1295. enum opa_pr_supported {
  1296. PR_NOT_SUPPORTED,
  1297. PR_OPA_SUPPORTED,
  1298. PR_IB_SUPPORTED
  1299. };
  1300. /**
  1301. * Check if current PR query can be an OPA query.
  1302. * Retuns PR_NOT_SUPPORTED if a path record query is not
  1303. * possible, PR_OPA_SUPPORTED if an OPA path record query
  1304. * is possible and PR_IB_SUPPORTED if an IB path record
  1305. * query is possible.
  1306. */
  1307. static int opa_pr_query_possible(struct ib_sa_client *client,
  1308. struct ib_sa_device *sa_dev,
  1309. struct ib_device *device, u8 port_num,
  1310. struct sa_path_rec *rec)
  1311. {
  1312. struct ib_port_attr port_attr;
  1313. if (ib_query_port(device, port_num, &port_attr))
  1314. return PR_NOT_SUPPORTED;
  1315. if (ib_sa_opa_pathrecord_support(client, sa_dev, port_num))
  1316. return PR_OPA_SUPPORTED;
  1317. if (port_attr.lid >= be16_to_cpu(IB_MULTICAST_LID_BASE))
  1318. return PR_NOT_SUPPORTED;
  1319. else
  1320. return PR_IB_SUPPORTED;
  1321. }
  1322. static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
  1323. int status,
  1324. struct ib_sa_mad *mad)
  1325. {
  1326. struct ib_sa_path_query *query =
  1327. container_of(sa_query, struct ib_sa_path_query, sa_query);
  1328. if (mad) {
  1329. struct sa_path_rec rec;
  1330. if (sa_query->flags & IB_SA_QUERY_OPA) {
  1331. ib_unpack(opa_path_rec_table,
  1332. ARRAY_SIZE(opa_path_rec_table),
  1333. mad->data, &rec);
  1334. rec.rec_type = SA_PATH_REC_TYPE_OPA;
  1335. query->callback(status, &rec, query->context);
  1336. } else {
  1337. ib_unpack(path_rec_table,
  1338. ARRAY_SIZE(path_rec_table),
  1339. mad->data, &rec);
  1340. rec.rec_type = SA_PATH_REC_TYPE_IB;
  1341. sa_path_set_dmac_zero(&rec);
  1342. if (query->conv_pr) {
  1343. struct sa_path_rec opa;
  1344. memset(&opa, 0, sizeof(struct sa_path_rec));
  1345. sa_convert_path_ib_to_opa(&opa, &rec);
  1346. query->callback(status, &opa, query->context);
  1347. } else {
  1348. query->callback(status, &rec, query->context);
  1349. }
  1350. }
  1351. } else
  1352. query->callback(status, NULL, query->context);
  1353. }
  1354. static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
  1355. {
  1356. struct ib_sa_path_query *query =
  1357. container_of(sa_query, struct ib_sa_path_query, sa_query);
  1358. kfree(query->conv_pr);
  1359. kfree(query);
  1360. }
  1361. /**
  1362. * ib_sa_path_rec_get - Start a Path get query
  1363. * @client:SA client
  1364. * @device:device to send query on
  1365. * @port_num: port number to send query on
  1366. * @rec:Path Record to send in query
  1367. * @comp_mask:component mask to send in query
  1368. * @timeout_ms:time to wait for response
  1369. * @gfp_mask:GFP mask to use for internal allocations
  1370. * @callback:function called when query completes, times out or is
  1371. * canceled
  1372. * @context:opaque user context passed to callback
  1373. * @sa_query:query context, used to cancel query
  1374. *
  1375. * Send a Path Record Get query to the SA to look up a path. The
  1376. * callback function will be called when the query completes (or
  1377. * fails); status is 0 for a successful response, -EINTR if the query
  1378. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  1379. * occurred sending the query. The resp parameter of the callback is
  1380. * only valid if status is 0.
  1381. *
  1382. * If the return value of ib_sa_path_rec_get() is negative, it is an
  1383. * error code. Otherwise it is a query ID that can be used to cancel
  1384. * the query.
  1385. */
  1386. int ib_sa_path_rec_get(struct ib_sa_client *client,
  1387. struct ib_device *device, u8 port_num,
  1388. struct sa_path_rec *rec,
  1389. ib_sa_comp_mask comp_mask,
  1390. unsigned long timeout_ms, gfp_t gfp_mask,
  1391. void (*callback)(int status,
  1392. struct sa_path_rec *resp,
  1393. void *context),
  1394. void *context,
  1395. struct ib_sa_query **sa_query)
  1396. {
  1397. struct ib_sa_path_query *query;
  1398. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  1399. struct ib_sa_port *port;
  1400. struct ib_mad_agent *agent;
  1401. struct ib_sa_mad *mad;
  1402. enum opa_pr_supported status;
  1403. int ret;
  1404. if (!sa_dev)
  1405. return -ENODEV;
  1406. if ((rec->rec_type != SA_PATH_REC_TYPE_IB) &&
  1407. (rec->rec_type != SA_PATH_REC_TYPE_OPA))
  1408. return -EINVAL;
  1409. port = &sa_dev->port[port_num - sa_dev->start_port];
  1410. agent = port->agent;
  1411. query = kzalloc(sizeof(*query), gfp_mask);
  1412. if (!query)
  1413. return -ENOMEM;
  1414. query->sa_query.port = port;
  1415. if (rec->rec_type == SA_PATH_REC_TYPE_OPA) {
  1416. status = opa_pr_query_possible(client, sa_dev, device, port_num,
  1417. rec);
  1418. if (status == PR_NOT_SUPPORTED) {
  1419. ret = -EINVAL;
  1420. goto err1;
  1421. } else if (status == PR_OPA_SUPPORTED) {
  1422. query->sa_query.flags |= IB_SA_QUERY_OPA;
  1423. } else {
  1424. query->conv_pr =
  1425. kmalloc(sizeof(*query->conv_pr), gfp_mask);
  1426. if (!query->conv_pr) {
  1427. ret = -ENOMEM;
  1428. goto err1;
  1429. }
  1430. }
  1431. }
  1432. ret = alloc_mad(&query->sa_query, gfp_mask);
  1433. if (ret)
  1434. goto err2;
  1435. ib_sa_client_get(client);
  1436. query->sa_query.client = client;
  1437. query->callback = callback;
  1438. query->context = context;
  1439. mad = query->sa_query.mad_buf->mad;
  1440. init_mad(&query->sa_query, agent);
  1441. query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL;
  1442. query->sa_query.release = ib_sa_path_rec_release;
  1443. mad->mad_hdr.method = IB_MGMT_METHOD_GET;
  1444. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_PATH_REC);
  1445. mad->sa_hdr.comp_mask = comp_mask;
  1446. if (query->sa_query.flags & IB_SA_QUERY_OPA) {
  1447. ib_pack(opa_path_rec_table, ARRAY_SIZE(opa_path_rec_table),
  1448. rec, mad->data);
  1449. } else if (query->conv_pr) {
  1450. sa_convert_path_opa_to_ib(query->conv_pr, rec);
  1451. ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table),
  1452. query->conv_pr, mad->data);
  1453. } else {
  1454. ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table),
  1455. rec, mad->data);
  1456. }
  1457. *sa_query = &query->sa_query;
  1458. query->sa_query.flags |= IB_SA_ENABLE_LOCAL_SERVICE;
  1459. query->sa_query.mad_buf->context[1] = (query->conv_pr) ?
  1460. query->conv_pr : rec;
  1461. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  1462. if (ret < 0)
  1463. goto err3;
  1464. return ret;
  1465. err3:
  1466. *sa_query = NULL;
  1467. ib_sa_client_put(query->sa_query.client);
  1468. free_mad(&query->sa_query);
  1469. err2:
  1470. kfree(query->conv_pr);
  1471. err1:
  1472. kfree(query);
  1473. return ret;
  1474. }
  1475. EXPORT_SYMBOL(ib_sa_path_rec_get);
  1476. static void ib_sa_service_rec_callback(struct ib_sa_query *sa_query,
  1477. int status,
  1478. struct ib_sa_mad *mad)
  1479. {
  1480. struct ib_sa_service_query *query =
  1481. container_of(sa_query, struct ib_sa_service_query, sa_query);
  1482. if (mad) {
  1483. struct ib_sa_service_rec rec;
  1484. ib_unpack(service_rec_table, ARRAY_SIZE(service_rec_table),
  1485. mad->data, &rec);
  1486. query->callback(status, &rec, query->context);
  1487. } else
  1488. query->callback(status, NULL, query->context);
  1489. }
  1490. static void ib_sa_service_rec_release(struct ib_sa_query *sa_query)
  1491. {
  1492. kfree(container_of(sa_query, struct ib_sa_service_query, sa_query));
  1493. }
  1494. /**
  1495. * ib_sa_service_rec_query - Start Service Record operation
  1496. * @client:SA client
  1497. * @device:device to send request on
  1498. * @port_num: port number to send request on
  1499. * @method:SA method - should be get, set, or delete
  1500. * @rec:Service Record to send in request
  1501. * @comp_mask:component mask to send in request
  1502. * @timeout_ms:time to wait for response
  1503. * @gfp_mask:GFP mask to use for internal allocations
  1504. * @callback:function called when request completes, times out or is
  1505. * canceled
  1506. * @context:opaque user context passed to callback
  1507. * @sa_query:request context, used to cancel request
  1508. *
  1509. * Send a Service Record set/get/delete to the SA to register,
  1510. * unregister or query a service record.
  1511. * The callback function will be called when the request completes (or
  1512. * fails); status is 0 for a successful response, -EINTR if the query
  1513. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  1514. * occurred sending the query. The resp parameter of the callback is
  1515. * only valid if status is 0.
  1516. *
  1517. * If the return value of ib_sa_service_rec_query() is negative, it is an
  1518. * error code. Otherwise it is a request ID that can be used to cancel
  1519. * the query.
  1520. */
  1521. int ib_sa_service_rec_query(struct ib_sa_client *client,
  1522. struct ib_device *device, u8 port_num, u8 method,
  1523. struct ib_sa_service_rec *rec,
  1524. ib_sa_comp_mask comp_mask,
  1525. unsigned long timeout_ms, gfp_t gfp_mask,
  1526. void (*callback)(int status,
  1527. struct ib_sa_service_rec *resp,
  1528. void *context),
  1529. void *context,
  1530. struct ib_sa_query **sa_query)
  1531. {
  1532. struct ib_sa_service_query *query;
  1533. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  1534. struct ib_sa_port *port;
  1535. struct ib_mad_agent *agent;
  1536. struct ib_sa_mad *mad;
  1537. int ret;
  1538. if (!sa_dev)
  1539. return -ENODEV;
  1540. port = &sa_dev->port[port_num - sa_dev->start_port];
  1541. agent = port->agent;
  1542. if (method != IB_MGMT_METHOD_GET &&
  1543. method != IB_MGMT_METHOD_SET &&
  1544. method != IB_SA_METHOD_DELETE)
  1545. return -EINVAL;
  1546. query = kzalloc(sizeof(*query), gfp_mask);
  1547. if (!query)
  1548. return -ENOMEM;
  1549. query->sa_query.port = port;
  1550. ret = alloc_mad(&query->sa_query, gfp_mask);
  1551. if (ret)
  1552. goto err1;
  1553. ib_sa_client_get(client);
  1554. query->sa_query.client = client;
  1555. query->callback = callback;
  1556. query->context = context;
  1557. mad = query->sa_query.mad_buf->mad;
  1558. init_mad(&query->sa_query, agent);
  1559. query->sa_query.callback = callback ? ib_sa_service_rec_callback : NULL;
  1560. query->sa_query.release = ib_sa_service_rec_release;
  1561. mad->mad_hdr.method = method;
  1562. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_SERVICE_REC);
  1563. mad->sa_hdr.comp_mask = comp_mask;
  1564. ib_pack(service_rec_table, ARRAY_SIZE(service_rec_table),
  1565. rec, mad->data);
  1566. *sa_query = &query->sa_query;
  1567. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  1568. if (ret < 0)
  1569. goto err2;
  1570. return ret;
  1571. err2:
  1572. *sa_query = NULL;
  1573. ib_sa_client_put(query->sa_query.client);
  1574. free_mad(&query->sa_query);
  1575. err1:
  1576. kfree(query);
  1577. return ret;
  1578. }
  1579. EXPORT_SYMBOL(ib_sa_service_rec_query);
  1580. static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
  1581. int status,
  1582. struct ib_sa_mad *mad)
  1583. {
  1584. struct ib_sa_mcmember_query *query =
  1585. container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
  1586. if (mad) {
  1587. struct ib_sa_mcmember_rec rec;
  1588. ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  1589. mad->data, &rec);
  1590. query->callback(status, &rec, query->context);
  1591. } else
  1592. query->callback(status, NULL, query->context);
  1593. }
  1594. static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
  1595. {
  1596. kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
  1597. }
  1598. int ib_sa_mcmember_rec_query(struct ib_sa_client *client,
  1599. struct ib_device *device, u8 port_num,
  1600. u8 method,
  1601. struct ib_sa_mcmember_rec *rec,
  1602. ib_sa_comp_mask comp_mask,
  1603. unsigned long timeout_ms, gfp_t gfp_mask,
  1604. void (*callback)(int status,
  1605. struct ib_sa_mcmember_rec *resp,
  1606. void *context),
  1607. void *context,
  1608. struct ib_sa_query **sa_query)
  1609. {
  1610. struct ib_sa_mcmember_query *query;
  1611. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  1612. struct ib_sa_port *port;
  1613. struct ib_mad_agent *agent;
  1614. struct ib_sa_mad *mad;
  1615. int ret;
  1616. if (!sa_dev)
  1617. return -ENODEV;
  1618. port = &sa_dev->port[port_num - sa_dev->start_port];
  1619. agent = port->agent;
  1620. query = kzalloc(sizeof(*query), gfp_mask);
  1621. if (!query)
  1622. return -ENOMEM;
  1623. query->sa_query.port = port;
  1624. ret = alloc_mad(&query->sa_query, gfp_mask);
  1625. if (ret)
  1626. goto err1;
  1627. ib_sa_client_get(client);
  1628. query->sa_query.client = client;
  1629. query->callback = callback;
  1630. query->context = context;
  1631. mad = query->sa_query.mad_buf->mad;
  1632. init_mad(&query->sa_query, agent);
  1633. query->sa_query.callback = callback ? ib_sa_mcmember_rec_callback : NULL;
  1634. query->sa_query.release = ib_sa_mcmember_rec_release;
  1635. mad->mad_hdr.method = method;
  1636. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
  1637. mad->sa_hdr.comp_mask = comp_mask;
  1638. ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  1639. rec, mad->data);
  1640. *sa_query = &query->sa_query;
  1641. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  1642. if (ret < 0)
  1643. goto err2;
  1644. return ret;
  1645. err2:
  1646. *sa_query = NULL;
  1647. ib_sa_client_put(query->sa_query.client);
  1648. free_mad(&query->sa_query);
  1649. err1:
  1650. kfree(query);
  1651. return ret;
  1652. }
  1653. /* Support GuidInfoRecord */
  1654. static void ib_sa_guidinfo_rec_callback(struct ib_sa_query *sa_query,
  1655. int status,
  1656. struct ib_sa_mad *mad)
  1657. {
  1658. struct ib_sa_guidinfo_query *query =
  1659. container_of(sa_query, struct ib_sa_guidinfo_query, sa_query);
  1660. if (mad) {
  1661. struct ib_sa_guidinfo_rec rec;
  1662. ib_unpack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table),
  1663. mad->data, &rec);
  1664. query->callback(status, &rec, query->context);
  1665. } else
  1666. query->callback(status, NULL, query->context);
  1667. }
  1668. static void ib_sa_guidinfo_rec_release(struct ib_sa_query *sa_query)
  1669. {
  1670. kfree(container_of(sa_query, struct ib_sa_guidinfo_query, sa_query));
  1671. }
  1672. int ib_sa_guid_info_rec_query(struct ib_sa_client *client,
  1673. struct ib_device *device, u8 port_num,
  1674. struct ib_sa_guidinfo_rec *rec,
  1675. ib_sa_comp_mask comp_mask, u8 method,
  1676. unsigned long timeout_ms, gfp_t gfp_mask,
  1677. void (*callback)(int status,
  1678. struct ib_sa_guidinfo_rec *resp,
  1679. void *context),
  1680. void *context,
  1681. struct ib_sa_query **sa_query)
  1682. {
  1683. struct ib_sa_guidinfo_query *query;
  1684. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  1685. struct ib_sa_port *port;
  1686. struct ib_mad_agent *agent;
  1687. struct ib_sa_mad *mad;
  1688. int ret;
  1689. if (!sa_dev)
  1690. return -ENODEV;
  1691. if (method != IB_MGMT_METHOD_GET &&
  1692. method != IB_MGMT_METHOD_SET &&
  1693. method != IB_SA_METHOD_DELETE) {
  1694. return -EINVAL;
  1695. }
  1696. port = &sa_dev->port[port_num - sa_dev->start_port];
  1697. agent = port->agent;
  1698. query = kzalloc(sizeof(*query), gfp_mask);
  1699. if (!query)
  1700. return -ENOMEM;
  1701. query->sa_query.port = port;
  1702. ret = alloc_mad(&query->sa_query, gfp_mask);
  1703. if (ret)
  1704. goto err1;
  1705. ib_sa_client_get(client);
  1706. query->sa_query.client = client;
  1707. query->callback = callback;
  1708. query->context = context;
  1709. mad = query->sa_query.mad_buf->mad;
  1710. init_mad(&query->sa_query, agent);
  1711. query->sa_query.callback = callback ? ib_sa_guidinfo_rec_callback : NULL;
  1712. query->sa_query.release = ib_sa_guidinfo_rec_release;
  1713. mad->mad_hdr.method = method;
  1714. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_GUID_INFO_REC);
  1715. mad->sa_hdr.comp_mask = comp_mask;
  1716. ib_pack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table), rec,
  1717. mad->data);
  1718. *sa_query = &query->sa_query;
  1719. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  1720. if (ret < 0)
  1721. goto err2;
  1722. return ret;
  1723. err2:
  1724. *sa_query = NULL;
  1725. ib_sa_client_put(query->sa_query.client);
  1726. free_mad(&query->sa_query);
  1727. err1:
  1728. kfree(query);
  1729. return ret;
  1730. }
  1731. EXPORT_SYMBOL(ib_sa_guid_info_rec_query);
  1732. bool ib_sa_sendonly_fullmem_support(struct ib_sa_client *client,
  1733. struct ib_device *device,
  1734. u8 port_num)
  1735. {
  1736. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  1737. struct ib_sa_port *port;
  1738. bool ret = false;
  1739. unsigned long flags;
  1740. if (!sa_dev)
  1741. return ret;
  1742. port = &sa_dev->port[port_num - sa_dev->start_port];
  1743. spin_lock_irqsave(&port->classport_lock, flags);
  1744. if ((port->classport_info.valid) &&
  1745. (port->classport_info.data.type == RDMA_CLASS_PORT_INFO_IB))
  1746. ret = ib_get_cpi_capmask2(&port->classport_info.data.ib)
  1747. & IB_SA_CAP_MASK2_SENDONLY_FULL_MEM_SUPPORT;
  1748. spin_unlock_irqrestore(&port->classport_lock, flags);
  1749. return ret;
  1750. }
  1751. EXPORT_SYMBOL(ib_sa_sendonly_fullmem_support);
  1752. struct ib_classport_info_context {
  1753. struct completion done;
  1754. struct ib_sa_query *sa_query;
  1755. };
  1756. static void ib_classportinfo_cb(void *context)
  1757. {
  1758. struct ib_classport_info_context *cb_ctx = context;
  1759. complete(&cb_ctx->done);
  1760. }
  1761. static void ib_sa_classport_info_rec_callback(struct ib_sa_query *sa_query,
  1762. int status,
  1763. struct ib_sa_mad *mad)
  1764. {
  1765. unsigned long flags;
  1766. struct ib_sa_classport_info_query *query =
  1767. container_of(sa_query, struct ib_sa_classport_info_query, sa_query);
  1768. struct ib_sa_classport_cache *info = &sa_query->port->classport_info;
  1769. if (mad) {
  1770. if (sa_query->flags & IB_SA_QUERY_OPA) {
  1771. struct opa_class_port_info rec;
  1772. ib_unpack(opa_classport_info_rec_table,
  1773. ARRAY_SIZE(opa_classport_info_rec_table),
  1774. mad->data, &rec);
  1775. spin_lock_irqsave(&sa_query->port->classport_lock,
  1776. flags);
  1777. if (!status && !info->valid) {
  1778. memcpy(&info->data.opa, &rec,
  1779. sizeof(info->data.opa));
  1780. info->valid = true;
  1781. info->data.type = RDMA_CLASS_PORT_INFO_OPA;
  1782. }
  1783. spin_unlock_irqrestore(&sa_query->port->classport_lock,
  1784. flags);
  1785. } else {
  1786. struct ib_class_port_info rec;
  1787. ib_unpack(ib_classport_info_rec_table,
  1788. ARRAY_SIZE(ib_classport_info_rec_table),
  1789. mad->data, &rec);
  1790. spin_lock_irqsave(&sa_query->port->classport_lock,
  1791. flags);
  1792. if (!status && !info->valid) {
  1793. memcpy(&info->data.ib, &rec,
  1794. sizeof(info->data.ib));
  1795. info->valid = true;
  1796. info->data.type = RDMA_CLASS_PORT_INFO_IB;
  1797. }
  1798. spin_unlock_irqrestore(&sa_query->port->classport_lock,
  1799. flags);
  1800. }
  1801. }
  1802. query->callback(query->context);
  1803. }
  1804. static void ib_sa_classport_info_rec_release(struct ib_sa_query *sa_query)
  1805. {
  1806. kfree(container_of(sa_query, struct ib_sa_classport_info_query,
  1807. sa_query));
  1808. }
  1809. static int ib_sa_classport_info_rec_query(struct ib_sa_port *port,
  1810. unsigned long timeout_ms,
  1811. void (*callback)(void *context),
  1812. void *context,
  1813. struct ib_sa_query **sa_query)
  1814. {
  1815. struct ib_mad_agent *agent;
  1816. struct ib_sa_classport_info_query *query;
  1817. struct ib_sa_mad *mad;
  1818. gfp_t gfp_mask = GFP_KERNEL;
  1819. int ret;
  1820. agent = port->agent;
  1821. query = kzalloc(sizeof(*query), gfp_mask);
  1822. if (!query)
  1823. return -ENOMEM;
  1824. query->sa_query.port = port;
  1825. query->sa_query.flags |= rdma_cap_opa_ah(port->agent->device,
  1826. port->port_num) ?
  1827. IB_SA_QUERY_OPA : 0;
  1828. ret = alloc_mad(&query->sa_query, gfp_mask);
  1829. if (ret)
  1830. goto err_free;
  1831. query->callback = callback;
  1832. query->context = context;
  1833. mad = query->sa_query.mad_buf->mad;
  1834. init_mad(&query->sa_query, agent);
  1835. query->sa_query.callback = ib_sa_classport_info_rec_callback;
  1836. query->sa_query.release = ib_sa_classport_info_rec_release;
  1837. mad->mad_hdr.method = IB_MGMT_METHOD_GET;
  1838. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_CLASS_PORTINFO);
  1839. mad->sa_hdr.comp_mask = 0;
  1840. *sa_query = &query->sa_query;
  1841. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  1842. if (ret < 0)
  1843. goto err_free_mad;
  1844. return ret;
  1845. err_free_mad:
  1846. *sa_query = NULL;
  1847. free_mad(&query->sa_query);
  1848. err_free:
  1849. kfree(query);
  1850. return ret;
  1851. }
  1852. static void update_ib_cpi(struct work_struct *work)
  1853. {
  1854. struct ib_sa_port *port =
  1855. container_of(work, struct ib_sa_port, ib_cpi_work.work);
  1856. struct ib_classport_info_context *cb_context;
  1857. unsigned long flags;
  1858. int ret;
  1859. /* If the classport info is valid, nothing
  1860. * to do here.
  1861. */
  1862. spin_lock_irqsave(&port->classport_lock, flags);
  1863. if (port->classport_info.valid) {
  1864. spin_unlock_irqrestore(&port->classport_lock, flags);
  1865. return;
  1866. }
  1867. spin_unlock_irqrestore(&port->classport_lock, flags);
  1868. cb_context = kmalloc(sizeof(*cb_context), GFP_KERNEL);
  1869. if (!cb_context)
  1870. goto err_nomem;
  1871. init_completion(&cb_context->done);
  1872. ret = ib_sa_classport_info_rec_query(port, 3000,
  1873. ib_classportinfo_cb, cb_context,
  1874. &cb_context->sa_query);
  1875. if (ret < 0)
  1876. goto free_cb_err;
  1877. wait_for_completion(&cb_context->done);
  1878. free_cb_err:
  1879. kfree(cb_context);
  1880. spin_lock_irqsave(&port->classport_lock, flags);
  1881. /* If the classport info is still not valid, the query should have
  1882. * failed for some reason. Retry issuing the query
  1883. */
  1884. if (!port->classport_info.valid) {
  1885. port->classport_info.retry_cnt++;
  1886. if (port->classport_info.retry_cnt <=
  1887. IB_SA_CPI_MAX_RETRY_CNT) {
  1888. unsigned long delay =
  1889. msecs_to_jiffies(IB_SA_CPI_RETRY_WAIT);
  1890. queue_delayed_work(ib_wq, &port->ib_cpi_work, delay);
  1891. }
  1892. }
  1893. spin_unlock_irqrestore(&port->classport_lock, flags);
  1894. err_nomem:
  1895. return;
  1896. }
  1897. static void send_handler(struct ib_mad_agent *agent,
  1898. struct ib_mad_send_wc *mad_send_wc)
  1899. {
  1900. struct ib_sa_query *query = mad_send_wc->send_buf->context[0];
  1901. unsigned long flags;
  1902. if (query->callback)
  1903. switch (mad_send_wc->status) {
  1904. case IB_WC_SUCCESS:
  1905. /* No callback -- already got recv */
  1906. break;
  1907. case IB_WC_RESP_TIMEOUT_ERR:
  1908. query->callback(query, -ETIMEDOUT, NULL);
  1909. break;
  1910. case IB_WC_WR_FLUSH_ERR:
  1911. query->callback(query, -EINTR, NULL);
  1912. break;
  1913. default:
  1914. query->callback(query, -EIO, NULL);
  1915. break;
  1916. }
  1917. xa_lock_irqsave(&queries, flags);
  1918. __xa_erase(&queries, query->id);
  1919. xa_unlock_irqrestore(&queries, flags);
  1920. free_mad(query);
  1921. if (query->client)
  1922. ib_sa_client_put(query->client);
  1923. query->release(query);
  1924. }
  1925. static void recv_handler(struct ib_mad_agent *mad_agent,
  1926. struct ib_mad_send_buf *send_buf,
  1927. struct ib_mad_recv_wc *mad_recv_wc)
  1928. {
  1929. struct ib_sa_query *query;
  1930. if (!send_buf)
  1931. return;
  1932. query = send_buf->context[0];
  1933. if (query->callback) {
  1934. if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
  1935. query->callback(query,
  1936. mad_recv_wc->recv_buf.mad->mad_hdr.status ?
  1937. -EINVAL : 0,
  1938. (struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
  1939. else
  1940. query->callback(query, -EIO, NULL);
  1941. }
  1942. ib_free_recv_mad(mad_recv_wc);
  1943. }
  1944. static void update_sm_ah(struct work_struct *work)
  1945. {
  1946. struct ib_sa_port *port =
  1947. container_of(work, struct ib_sa_port, update_task);
  1948. struct ib_sa_sm_ah *new_ah;
  1949. struct ib_port_attr port_attr;
  1950. struct rdma_ah_attr ah_attr;
  1951. bool grh_required;
  1952. if (ib_query_port(port->agent->device, port->port_num, &port_attr)) {
  1953. pr_warn("Couldn't query port\n");
  1954. return;
  1955. }
  1956. new_ah = kmalloc(sizeof(*new_ah), GFP_KERNEL);
  1957. if (!new_ah)
  1958. return;
  1959. kref_init(&new_ah->ref);
  1960. new_ah->src_path_mask = (1 << port_attr.lmc) - 1;
  1961. new_ah->pkey_index = 0;
  1962. if (ib_find_pkey(port->agent->device, port->port_num,
  1963. IB_DEFAULT_PKEY_FULL, &new_ah->pkey_index))
  1964. pr_err("Couldn't find index for default PKey\n");
  1965. memset(&ah_attr, 0, sizeof(ah_attr));
  1966. ah_attr.type = rdma_ah_find_type(port->agent->device,
  1967. port->port_num);
  1968. rdma_ah_set_dlid(&ah_attr, port_attr.sm_lid);
  1969. rdma_ah_set_sl(&ah_attr, port_attr.sm_sl);
  1970. rdma_ah_set_port_num(&ah_attr, port->port_num);
  1971. grh_required = rdma_is_grh_required(port->agent->device,
  1972. port->port_num);
  1973. /*
  1974. * The OPA sm_lid of 0xFFFF needs special handling so that it can be
  1975. * differentiated from a permissive LID of 0xFFFF. We set the
  1976. * grh_required flag here so the SA can program the DGID in the
  1977. * address handle appropriately
  1978. */
  1979. if (ah_attr.type == RDMA_AH_ATTR_TYPE_OPA &&
  1980. (grh_required ||
  1981. port_attr.sm_lid == be16_to_cpu(IB_LID_PERMISSIVE)))
  1982. rdma_ah_set_make_grd(&ah_attr, true);
  1983. if (ah_attr.type == RDMA_AH_ATTR_TYPE_IB && grh_required) {
  1984. rdma_ah_set_ah_flags(&ah_attr, IB_AH_GRH);
  1985. rdma_ah_set_subnet_prefix(&ah_attr,
  1986. cpu_to_be64(port_attr.subnet_prefix));
  1987. rdma_ah_set_interface_id(&ah_attr,
  1988. cpu_to_be64(IB_SA_WELL_KNOWN_GUID));
  1989. }
  1990. new_ah->ah = rdma_create_ah(port->agent->qp->pd, &ah_attr,
  1991. RDMA_CREATE_AH_SLEEPABLE);
  1992. if (IS_ERR(new_ah->ah)) {
  1993. pr_warn("Couldn't create new SM AH\n");
  1994. kfree(new_ah);
  1995. return;
  1996. }
  1997. spin_lock_irq(&port->ah_lock);
  1998. if (port->sm_ah)
  1999. kref_put(&port->sm_ah->ref, free_sm_ah);
  2000. port->sm_ah = new_ah;
  2001. spin_unlock_irq(&port->ah_lock);
  2002. }
  2003. static void ib_sa_event(struct ib_event_handler *handler,
  2004. struct ib_event *event)
  2005. {
  2006. if (event->event == IB_EVENT_PORT_ERR ||
  2007. event->event == IB_EVENT_PORT_ACTIVE ||
  2008. event->event == IB_EVENT_LID_CHANGE ||
  2009. event->event == IB_EVENT_PKEY_CHANGE ||
  2010. event->event == IB_EVENT_SM_CHANGE ||
  2011. event->event == IB_EVENT_CLIENT_REREGISTER) {
  2012. unsigned long flags;
  2013. struct ib_sa_device *sa_dev =
  2014. container_of(handler, typeof(*sa_dev), event_handler);
  2015. u8 port_num = event->element.port_num - sa_dev->start_port;
  2016. struct ib_sa_port *port = &sa_dev->port[port_num];
  2017. if (!rdma_cap_ib_sa(handler->device, port->port_num))
  2018. return;
  2019. spin_lock_irqsave(&port->ah_lock, flags);
  2020. if (port->sm_ah)
  2021. kref_put(&port->sm_ah->ref, free_sm_ah);
  2022. port->sm_ah = NULL;
  2023. spin_unlock_irqrestore(&port->ah_lock, flags);
  2024. if (event->event == IB_EVENT_SM_CHANGE ||
  2025. event->event == IB_EVENT_CLIENT_REREGISTER ||
  2026. event->event == IB_EVENT_LID_CHANGE ||
  2027. event->event == IB_EVENT_PORT_ACTIVE) {
  2028. unsigned long delay =
  2029. msecs_to_jiffies(IB_SA_CPI_RETRY_WAIT);
  2030. spin_lock_irqsave(&port->classport_lock, flags);
  2031. port->classport_info.valid = false;
  2032. port->classport_info.retry_cnt = 0;
  2033. spin_unlock_irqrestore(&port->classport_lock, flags);
  2034. queue_delayed_work(ib_wq,
  2035. &port->ib_cpi_work, delay);
  2036. }
  2037. queue_work(ib_wq, &sa_dev->port[port_num].update_task);
  2038. }
  2039. }
  2040. static int ib_sa_add_one(struct ib_device *device)
  2041. {
  2042. struct ib_sa_device *sa_dev;
  2043. int s, e, i;
  2044. int count = 0;
  2045. int ret;
  2046. s = rdma_start_port(device);
  2047. e = rdma_end_port(device);
  2048. sa_dev = kzalloc(struct_size(sa_dev, port, e - s + 1), GFP_KERNEL);
  2049. if (!sa_dev)
  2050. return -ENOMEM;
  2051. sa_dev->start_port = s;
  2052. sa_dev->end_port = e;
  2053. for (i = 0; i <= e - s; ++i) {
  2054. spin_lock_init(&sa_dev->port[i].ah_lock);
  2055. if (!rdma_cap_ib_sa(device, i + 1))
  2056. continue;
  2057. sa_dev->port[i].sm_ah = NULL;
  2058. sa_dev->port[i].port_num = i + s;
  2059. spin_lock_init(&sa_dev->port[i].classport_lock);
  2060. sa_dev->port[i].classport_info.valid = false;
  2061. sa_dev->port[i].agent =
  2062. ib_register_mad_agent(device, i + s, IB_QPT_GSI,
  2063. NULL, 0, send_handler,
  2064. recv_handler, sa_dev, 0);
  2065. if (IS_ERR(sa_dev->port[i].agent)) {
  2066. ret = PTR_ERR(sa_dev->port[i].agent);
  2067. goto err;
  2068. }
  2069. INIT_WORK(&sa_dev->port[i].update_task, update_sm_ah);
  2070. INIT_DELAYED_WORK(&sa_dev->port[i].ib_cpi_work,
  2071. update_ib_cpi);
  2072. count++;
  2073. }
  2074. if (!count) {
  2075. ret = -EOPNOTSUPP;
  2076. goto free;
  2077. }
  2078. ib_set_client_data(device, &sa_client, sa_dev);
  2079. /*
  2080. * We register our event handler after everything is set up,
  2081. * and then update our cached info after the event handler is
  2082. * registered to avoid any problems if a port changes state
  2083. * during our initialization.
  2084. */
  2085. INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
  2086. ib_register_event_handler(&sa_dev->event_handler);
  2087. for (i = 0; i <= e - s; ++i) {
  2088. if (rdma_cap_ib_sa(device, i + 1))
  2089. update_sm_ah(&sa_dev->port[i].update_task);
  2090. }
  2091. return 0;
  2092. err:
  2093. while (--i >= 0) {
  2094. if (rdma_cap_ib_sa(device, i + 1))
  2095. ib_unregister_mad_agent(sa_dev->port[i].agent);
  2096. }
  2097. free:
  2098. kfree(sa_dev);
  2099. return ret;
  2100. }
  2101. static void ib_sa_remove_one(struct ib_device *device, void *client_data)
  2102. {
  2103. struct ib_sa_device *sa_dev = client_data;
  2104. int i;
  2105. ib_unregister_event_handler(&sa_dev->event_handler);
  2106. flush_workqueue(ib_wq);
  2107. for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
  2108. if (rdma_cap_ib_sa(device, i + 1)) {
  2109. cancel_delayed_work_sync(&sa_dev->port[i].ib_cpi_work);
  2110. ib_unregister_mad_agent(sa_dev->port[i].agent);
  2111. if (sa_dev->port[i].sm_ah)
  2112. kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
  2113. }
  2114. }
  2115. kfree(sa_dev);
  2116. }
  2117. int ib_sa_init(void)
  2118. {
  2119. int ret;
  2120. get_random_bytes(&tid, sizeof tid);
  2121. atomic_set(&ib_nl_sa_request_seq, 0);
  2122. ret = ib_register_client(&sa_client);
  2123. if (ret) {
  2124. pr_err("Couldn't register ib_sa client\n");
  2125. goto err1;
  2126. }
  2127. ret = mcast_init();
  2128. if (ret) {
  2129. pr_err("Couldn't initialize multicast handling\n");
  2130. goto err2;
  2131. }
  2132. ib_nl_wq = alloc_ordered_workqueue("ib_nl_sa_wq", WQ_MEM_RECLAIM);
  2133. if (!ib_nl_wq) {
  2134. ret = -ENOMEM;
  2135. goto err3;
  2136. }
  2137. INIT_DELAYED_WORK(&ib_nl_timed_work, ib_nl_request_timeout);
  2138. return 0;
  2139. err3:
  2140. mcast_cleanup();
  2141. err2:
  2142. ib_unregister_client(&sa_client);
  2143. err1:
  2144. return ret;
  2145. }
  2146. void ib_sa_cleanup(void)
  2147. {
  2148. cancel_delayed_work(&ib_nl_timed_work);
  2149. flush_workqueue(ib_nl_wq);
  2150. destroy_workqueue(ib_nl_wq);
  2151. mcast_cleanup();
  2152. ib_unregister_client(&sa_client);
  2153. WARN_ON(!xa_empty(&queries));
  2154. }