sa_query.c 66 KB

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