sa_query.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528
  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 0;
  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 0;
  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 roce_resolve_route_from_path(struct sa_path_rec *rec,
  1123. const struct ib_gid_attr *attr)
  1124. {
  1125. struct rdma_dev_addr dev_addr = {};
  1126. union {
  1127. struct sockaddr_in _sockaddr_in;
  1128. struct sockaddr_in6 _sockaddr_in6;
  1129. } sgid_addr, dgid_addr;
  1130. int ret;
  1131. if (rec->roce.route_resolved)
  1132. return 0;
  1133. if (!attr || !attr->ndev)
  1134. return -EINVAL;
  1135. dev_addr.bound_dev_if = attr->ndev->ifindex;
  1136. /* TODO: Use net from the ib_gid_attr once it is added to it,
  1137. * until than, limit itself to init_net.
  1138. */
  1139. dev_addr.net = &init_net;
  1140. rdma_gid2ip((struct sockaddr *)&sgid_addr, &rec->sgid);
  1141. rdma_gid2ip((struct sockaddr *)&dgid_addr, &rec->dgid);
  1142. /* validate the route */
  1143. ret = rdma_resolve_ip_route((struct sockaddr *)&sgid_addr,
  1144. (struct sockaddr *)&dgid_addr, &dev_addr);
  1145. if (ret)
  1146. return ret;
  1147. if ((dev_addr.network == RDMA_NETWORK_IPV4 ||
  1148. dev_addr.network == RDMA_NETWORK_IPV6) &&
  1149. rec->rec_type != SA_PATH_REC_TYPE_ROCE_V2)
  1150. return -EINVAL;
  1151. rec->roce.route_resolved = true;
  1152. return 0;
  1153. }
  1154. static int init_ah_attr_grh_fields(struct ib_device *device, u8 port_num,
  1155. struct sa_path_rec *rec,
  1156. struct rdma_ah_attr *ah_attr,
  1157. const struct ib_gid_attr *gid_attr)
  1158. {
  1159. enum ib_gid_type type = sa_conv_pathrec_to_gid_type(rec);
  1160. if (!gid_attr) {
  1161. gid_attr = rdma_find_gid_by_port(device, &rec->sgid, type,
  1162. port_num, NULL);
  1163. if (IS_ERR(gid_attr))
  1164. return PTR_ERR(gid_attr);
  1165. } else
  1166. rdma_hold_gid_attr(gid_attr);
  1167. rdma_move_grh_sgid_attr(ah_attr, &rec->dgid,
  1168. be32_to_cpu(rec->flow_label),
  1169. rec->hop_limit, rec->traffic_class,
  1170. gid_attr);
  1171. return 0;
  1172. }
  1173. /**
  1174. * ib_init_ah_attr_from_path - Initialize address handle attributes based on
  1175. * an SA path record.
  1176. * @device: Device associated ah attributes initialization.
  1177. * @port_num: Port on the specified device.
  1178. * @rec: path record entry to use for ah attributes initialization.
  1179. * @ah_attr: address handle attributes to initialization from path record.
  1180. * @sgid_attr: SGID attribute to consider during initialization.
  1181. *
  1182. * When ib_init_ah_attr_from_path() returns success,
  1183. * (a) for IB link layer it optionally contains a reference to SGID attribute
  1184. * when GRH is present for IB link layer.
  1185. * (b) for RoCE link layer it contains a reference to SGID attribute.
  1186. * User must invoke rdma_destroy_ah_attr() to release reference to SGID
  1187. * attributes which are initialized using ib_init_ah_attr_from_path().
  1188. */
  1189. int ib_init_ah_attr_from_path(struct ib_device *device, u8 port_num,
  1190. struct sa_path_rec *rec,
  1191. struct rdma_ah_attr *ah_attr,
  1192. const struct ib_gid_attr *gid_attr)
  1193. {
  1194. int ret = 0;
  1195. memset(ah_attr, 0, sizeof(*ah_attr));
  1196. ah_attr->type = rdma_ah_find_type(device, port_num);
  1197. rdma_ah_set_sl(ah_attr, rec->sl);
  1198. rdma_ah_set_port_num(ah_attr, port_num);
  1199. rdma_ah_set_static_rate(ah_attr, rec->rate);
  1200. if (sa_path_is_roce(rec)) {
  1201. ret = roce_resolve_route_from_path(rec, gid_attr);
  1202. if (ret)
  1203. return ret;
  1204. memcpy(ah_attr->roce.dmac, sa_path_get_dmac(rec), ETH_ALEN);
  1205. } else {
  1206. rdma_ah_set_dlid(ah_attr, be32_to_cpu(sa_path_get_dlid(rec)));
  1207. if (sa_path_is_opa(rec) &&
  1208. rdma_ah_get_dlid(ah_attr) == be16_to_cpu(IB_LID_PERMISSIVE))
  1209. rdma_ah_set_make_grd(ah_attr, true);
  1210. rdma_ah_set_path_bits(ah_attr,
  1211. be32_to_cpu(sa_path_get_slid(rec)) &
  1212. get_src_path_mask(device, port_num));
  1213. }
  1214. if (rec->hop_limit > 0 || sa_path_is_roce(rec))
  1215. ret = init_ah_attr_grh_fields(device, port_num,
  1216. rec, ah_attr, gid_attr);
  1217. return ret;
  1218. }
  1219. EXPORT_SYMBOL(ib_init_ah_attr_from_path);
  1220. static int alloc_mad(struct ib_sa_query *query, gfp_t gfp_mask)
  1221. {
  1222. struct rdma_ah_attr ah_attr;
  1223. unsigned long flags;
  1224. spin_lock_irqsave(&query->port->ah_lock, flags);
  1225. if (!query->port->sm_ah) {
  1226. spin_unlock_irqrestore(&query->port->ah_lock, flags);
  1227. return -EAGAIN;
  1228. }
  1229. kref_get(&query->port->sm_ah->ref);
  1230. query->sm_ah = query->port->sm_ah;
  1231. spin_unlock_irqrestore(&query->port->ah_lock, flags);
  1232. /*
  1233. * Always check if sm_ah has valid dlid assigned,
  1234. * before querying for class port info
  1235. */
  1236. if ((rdma_query_ah(query->sm_ah->ah, &ah_attr) < 0) ||
  1237. !rdma_is_valid_unicast_lid(&ah_attr)) {
  1238. kref_put(&query->sm_ah->ref, free_sm_ah);
  1239. return -EAGAIN;
  1240. }
  1241. query->mad_buf = ib_create_send_mad(query->port->agent, 1,
  1242. query->sm_ah->pkey_index,
  1243. 0, IB_MGMT_SA_HDR, IB_MGMT_SA_DATA,
  1244. gfp_mask,
  1245. ((query->flags & IB_SA_QUERY_OPA) ?
  1246. OPA_MGMT_BASE_VERSION :
  1247. IB_MGMT_BASE_VERSION));
  1248. if (IS_ERR(query->mad_buf)) {
  1249. kref_put(&query->sm_ah->ref, free_sm_ah);
  1250. return -ENOMEM;
  1251. }
  1252. query->mad_buf->ah = query->sm_ah->ah;
  1253. return 0;
  1254. }
  1255. static void free_mad(struct ib_sa_query *query)
  1256. {
  1257. ib_free_send_mad(query->mad_buf);
  1258. kref_put(&query->sm_ah->ref, free_sm_ah);
  1259. }
  1260. static void init_mad(struct ib_sa_query *query, struct ib_mad_agent *agent)
  1261. {
  1262. struct ib_sa_mad *mad = query->mad_buf->mad;
  1263. unsigned long flags;
  1264. memset(mad, 0, sizeof *mad);
  1265. if (query->flags & IB_SA_QUERY_OPA) {
  1266. mad->mad_hdr.base_version = OPA_MGMT_BASE_VERSION;
  1267. mad->mad_hdr.class_version = OPA_SA_CLASS_VERSION;
  1268. } else {
  1269. mad->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
  1270. mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
  1271. }
  1272. mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  1273. spin_lock_irqsave(&tid_lock, flags);
  1274. mad->mad_hdr.tid =
  1275. cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
  1276. spin_unlock_irqrestore(&tid_lock, flags);
  1277. }
  1278. static int send_mad(struct ib_sa_query *query, int timeout_ms, gfp_t gfp_mask)
  1279. {
  1280. bool preload = gfpflags_allow_blocking(gfp_mask);
  1281. unsigned long flags;
  1282. int ret, id;
  1283. if (preload)
  1284. idr_preload(gfp_mask);
  1285. spin_lock_irqsave(&idr_lock, flags);
  1286. id = idr_alloc(&query_idr, query, 0, 0, GFP_NOWAIT);
  1287. spin_unlock_irqrestore(&idr_lock, flags);
  1288. if (preload)
  1289. idr_preload_end();
  1290. if (id < 0)
  1291. return id;
  1292. query->mad_buf->timeout_ms = timeout_ms;
  1293. query->mad_buf->context[0] = query;
  1294. query->id = id;
  1295. if ((query->flags & IB_SA_ENABLE_LOCAL_SERVICE) &&
  1296. (!(query->flags & IB_SA_QUERY_OPA))) {
  1297. if (!rdma_nl_chk_listeners(RDMA_NL_GROUP_LS)) {
  1298. if (!ib_nl_make_request(query, gfp_mask))
  1299. return id;
  1300. }
  1301. ib_sa_disable_local_svc(query);
  1302. }
  1303. ret = ib_post_send_mad(query->mad_buf, NULL);
  1304. if (ret) {
  1305. spin_lock_irqsave(&idr_lock, flags);
  1306. idr_remove(&query_idr, id);
  1307. spin_unlock_irqrestore(&idr_lock, flags);
  1308. }
  1309. /*
  1310. * It's not safe to dereference query any more, because the
  1311. * send may already have completed and freed the query in
  1312. * another context.
  1313. */
  1314. return ret ? ret : id;
  1315. }
  1316. void ib_sa_unpack_path(void *attribute, struct sa_path_rec *rec)
  1317. {
  1318. ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table), attribute, rec);
  1319. }
  1320. EXPORT_SYMBOL(ib_sa_unpack_path);
  1321. void ib_sa_pack_path(struct sa_path_rec *rec, void *attribute)
  1322. {
  1323. ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, attribute);
  1324. }
  1325. EXPORT_SYMBOL(ib_sa_pack_path);
  1326. static bool ib_sa_opa_pathrecord_support(struct ib_sa_client *client,
  1327. struct ib_device *device,
  1328. u8 port_num)
  1329. {
  1330. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  1331. struct ib_sa_port *port;
  1332. unsigned long flags;
  1333. bool ret = false;
  1334. if (!sa_dev)
  1335. return ret;
  1336. port = &sa_dev->port[port_num - sa_dev->start_port];
  1337. spin_lock_irqsave(&port->classport_lock, flags);
  1338. if (!port->classport_info.valid)
  1339. goto ret;
  1340. if (port->classport_info.data.type == RDMA_CLASS_PORT_INFO_OPA)
  1341. ret = opa_get_cpi_capmask2(&port->classport_info.data.opa) &
  1342. OPA_CLASS_PORT_INFO_PR_SUPPORT;
  1343. ret:
  1344. spin_unlock_irqrestore(&port->classport_lock, flags);
  1345. return ret;
  1346. }
  1347. enum opa_pr_supported {
  1348. PR_NOT_SUPPORTED,
  1349. PR_OPA_SUPPORTED,
  1350. PR_IB_SUPPORTED
  1351. };
  1352. /**
  1353. * Check if current PR query can be an OPA query.
  1354. * Retuns PR_NOT_SUPPORTED if a path record query is not
  1355. * possible, PR_OPA_SUPPORTED if an OPA path record query
  1356. * is possible and PR_IB_SUPPORTED if an IB path record
  1357. * query is possible.
  1358. */
  1359. static int opa_pr_query_possible(struct ib_sa_client *client,
  1360. struct ib_device *device,
  1361. u8 port_num,
  1362. struct sa_path_rec *rec)
  1363. {
  1364. struct ib_port_attr port_attr;
  1365. if (ib_query_port(device, port_num, &port_attr))
  1366. return PR_NOT_SUPPORTED;
  1367. if (ib_sa_opa_pathrecord_support(client, device, port_num))
  1368. return PR_OPA_SUPPORTED;
  1369. if (port_attr.lid >= be16_to_cpu(IB_MULTICAST_LID_BASE))
  1370. return PR_NOT_SUPPORTED;
  1371. else
  1372. return PR_IB_SUPPORTED;
  1373. }
  1374. static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
  1375. int status,
  1376. struct ib_sa_mad *mad)
  1377. {
  1378. struct ib_sa_path_query *query =
  1379. container_of(sa_query, struct ib_sa_path_query, sa_query);
  1380. if (mad) {
  1381. struct sa_path_rec rec;
  1382. if (sa_query->flags & IB_SA_QUERY_OPA) {
  1383. ib_unpack(opa_path_rec_table,
  1384. ARRAY_SIZE(opa_path_rec_table),
  1385. mad->data, &rec);
  1386. rec.rec_type = SA_PATH_REC_TYPE_OPA;
  1387. query->callback(status, &rec, query->context);
  1388. } else {
  1389. ib_unpack(path_rec_table,
  1390. ARRAY_SIZE(path_rec_table),
  1391. mad->data, &rec);
  1392. rec.rec_type = SA_PATH_REC_TYPE_IB;
  1393. sa_path_set_dmac_zero(&rec);
  1394. if (query->conv_pr) {
  1395. struct sa_path_rec opa;
  1396. memset(&opa, 0, sizeof(struct sa_path_rec));
  1397. sa_convert_path_ib_to_opa(&opa, &rec);
  1398. query->callback(status, &opa, query->context);
  1399. } else {
  1400. query->callback(status, &rec, query->context);
  1401. }
  1402. }
  1403. } else
  1404. query->callback(status, NULL, query->context);
  1405. }
  1406. static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
  1407. {
  1408. struct ib_sa_path_query *query =
  1409. container_of(sa_query, struct ib_sa_path_query, sa_query);
  1410. kfree(query->conv_pr);
  1411. kfree(query);
  1412. }
  1413. /**
  1414. * ib_sa_path_rec_get - Start a Path get query
  1415. * @client:SA client
  1416. * @device:device to send query on
  1417. * @port_num: port number to send query on
  1418. * @rec:Path Record to send in query
  1419. * @comp_mask:component mask to send in query
  1420. * @timeout_ms:time to wait for response
  1421. * @gfp_mask:GFP mask to use for internal allocations
  1422. * @callback:function called when query completes, times out or is
  1423. * canceled
  1424. * @context:opaque user context passed to callback
  1425. * @sa_query:query context, used to cancel query
  1426. *
  1427. * Send a Path Record Get query to the SA to look up a path. The
  1428. * callback function will be called when the query completes (or
  1429. * fails); status is 0 for a successful response, -EINTR if the query
  1430. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  1431. * occurred sending the query. The resp parameter of the callback is
  1432. * only valid if status is 0.
  1433. *
  1434. * If the return value of ib_sa_path_rec_get() is negative, it is an
  1435. * error code. Otherwise it is a query ID that can be used to cancel
  1436. * the query.
  1437. */
  1438. int ib_sa_path_rec_get(struct ib_sa_client *client,
  1439. struct ib_device *device, u8 port_num,
  1440. struct sa_path_rec *rec,
  1441. ib_sa_comp_mask comp_mask,
  1442. int timeout_ms, gfp_t gfp_mask,
  1443. void (*callback)(int status,
  1444. struct sa_path_rec *resp,
  1445. void *context),
  1446. void *context,
  1447. struct ib_sa_query **sa_query)
  1448. {
  1449. struct ib_sa_path_query *query;
  1450. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  1451. struct ib_sa_port *port;
  1452. struct ib_mad_agent *agent;
  1453. struct ib_sa_mad *mad;
  1454. enum opa_pr_supported status;
  1455. int ret;
  1456. if (!sa_dev)
  1457. return -ENODEV;
  1458. if ((rec->rec_type != SA_PATH_REC_TYPE_IB) &&
  1459. (rec->rec_type != SA_PATH_REC_TYPE_OPA))
  1460. return -EINVAL;
  1461. port = &sa_dev->port[port_num - sa_dev->start_port];
  1462. agent = port->agent;
  1463. query = kzalloc(sizeof(*query), gfp_mask);
  1464. if (!query)
  1465. return -ENOMEM;
  1466. query->sa_query.port = port;
  1467. if (rec->rec_type == SA_PATH_REC_TYPE_OPA) {
  1468. status = opa_pr_query_possible(client, device, port_num, rec);
  1469. if (status == PR_NOT_SUPPORTED) {
  1470. ret = -EINVAL;
  1471. goto err1;
  1472. } else if (status == PR_OPA_SUPPORTED) {
  1473. query->sa_query.flags |= IB_SA_QUERY_OPA;
  1474. } else {
  1475. query->conv_pr =
  1476. kmalloc(sizeof(*query->conv_pr), gfp_mask);
  1477. if (!query->conv_pr) {
  1478. ret = -ENOMEM;
  1479. goto err1;
  1480. }
  1481. }
  1482. }
  1483. ret = alloc_mad(&query->sa_query, gfp_mask);
  1484. if (ret)
  1485. goto err2;
  1486. ib_sa_client_get(client);
  1487. query->sa_query.client = client;
  1488. query->callback = callback;
  1489. query->context = context;
  1490. mad = query->sa_query.mad_buf->mad;
  1491. init_mad(&query->sa_query, agent);
  1492. query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL;
  1493. query->sa_query.release = ib_sa_path_rec_release;
  1494. mad->mad_hdr.method = IB_MGMT_METHOD_GET;
  1495. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_PATH_REC);
  1496. mad->sa_hdr.comp_mask = comp_mask;
  1497. if (query->sa_query.flags & IB_SA_QUERY_OPA) {
  1498. ib_pack(opa_path_rec_table, ARRAY_SIZE(opa_path_rec_table),
  1499. rec, mad->data);
  1500. } else if (query->conv_pr) {
  1501. sa_convert_path_opa_to_ib(query->conv_pr, rec);
  1502. ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table),
  1503. query->conv_pr, mad->data);
  1504. } else {
  1505. ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table),
  1506. rec, mad->data);
  1507. }
  1508. *sa_query = &query->sa_query;
  1509. query->sa_query.flags |= IB_SA_ENABLE_LOCAL_SERVICE;
  1510. query->sa_query.mad_buf->context[1] = (query->conv_pr) ?
  1511. query->conv_pr : rec;
  1512. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  1513. if (ret < 0)
  1514. goto err3;
  1515. return ret;
  1516. err3:
  1517. *sa_query = NULL;
  1518. ib_sa_client_put(query->sa_query.client);
  1519. free_mad(&query->sa_query);
  1520. err2:
  1521. kfree(query->conv_pr);
  1522. err1:
  1523. kfree(query);
  1524. return ret;
  1525. }
  1526. EXPORT_SYMBOL(ib_sa_path_rec_get);
  1527. static void ib_sa_service_rec_callback(struct ib_sa_query *sa_query,
  1528. int status,
  1529. struct ib_sa_mad *mad)
  1530. {
  1531. struct ib_sa_service_query *query =
  1532. container_of(sa_query, struct ib_sa_service_query, sa_query);
  1533. if (mad) {
  1534. struct ib_sa_service_rec rec;
  1535. ib_unpack(service_rec_table, ARRAY_SIZE(service_rec_table),
  1536. mad->data, &rec);
  1537. query->callback(status, &rec, query->context);
  1538. } else
  1539. query->callback(status, NULL, query->context);
  1540. }
  1541. static void ib_sa_service_rec_release(struct ib_sa_query *sa_query)
  1542. {
  1543. kfree(container_of(sa_query, struct ib_sa_service_query, sa_query));
  1544. }
  1545. /**
  1546. * ib_sa_service_rec_query - Start Service Record operation
  1547. * @client:SA client
  1548. * @device:device to send request on
  1549. * @port_num: port number to send request on
  1550. * @method:SA method - should be get, set, or delete
  1551. * @rec:Service Record to send in request
  1552. * @comp_mask:component mask to send in request
  1553. * @timeout_ms:time to wait for response
  1554. * @gfp_mask:GFP mask to use for internal allocations
  1555. * @callback:function called when request completes, times out or is
  1556. * canceled
  1557. * @context:opaque user context passed to callback
  1558. * @sa_query:request context, used to cancel request
  1559. *
  1560. * Send a Service Record set/get/delete to the SA to register,
  1561. * unregister or query a service record.
  1562. * The callback function will be called when the request completes (or
  1563. * fails); status is 0 for a successful response, -EINTR if the query
  1564. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  1565. * occurred sending the query. The resp parameter of the callback is
  1566. * only valid if status is 0.
  1567. *
  1568. * If the return value of ib_sa_service_rec_query() is negative, it is an
  1569. * error code. Otherwise it is a request ID that can be used to cancel
  1570. * the query.
  1571. */
  1572. int ib_sa_service_rec_query(struct ib_sa_client *client,
  1573. struct ib_device *device, u8 port_num, u8 method,
  1574. struct ib_sa_service_rec *rec,
  1575. ib_sa_comp_mask comp_mask,
  1576. int timeout_ms, gfp_t gfp_mask,
  1577. void (*callback)(int status,
  1578. struct ib_sa_service_rec *resp,
  1579. void *context),
  1580. void *context,
  1581. struct ib_sa_query **sa_query)
  1582. {
  1583. struct ib_sa_service_query *query;
  1584. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  1585. struct ib_sa_port *port;
  1586. struct ib_mad_agent *agent;
  1587. struct ib_sa_mad *mad;
  1588. int ret;
  1589. if (!sa_dev)
  1590. return -ENODEV;
  1591. port = &sa_dev->port[port_num - sa_dev->start_port];
  1592. agent = port->agent;
  1593. if (method != IB_MGMT_METHOD_GET &&
  1594. method != IB_MGMT_METHOD_SET &&
  1595. method != IB_SA_METHOD_DELETE)
  1596. return -EINVAL;
  1597. query = kzalloc(sizeof(*query), gfp_mask);
  1598. if (!query)
  1599. return -ENOMEM;
  1600. query->sa_query.port = port;
  1601. ret = alloc_mad(&query->sa_query, gfp_mask);
  1602. if (ret)
  1603. goto err1;
  1604. ib_sa_client_get(client);
  1605. query->sa_query.client = client;
  1606. query->callback = callback;
  1607. query->context = context;
  1608. mad = query->sa_query.mad_buf->mad;
  1609. init_mad(&query->sa_query, agent);
  1610. query->sa_query.callback = callback ? ib_sa_service_rec_callback : NULL;
  1611. query->sa_query.release = ib_sa_service_rec_release;
  1612. mad->mad_hdr.method = method;
  1613. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_SERVICE_REC);
  1614. mad->sa_hdr.comp_mask = comp_mask;
  1615. ib_pack(service_rec_table, ARRAY_SIZE(service_rec_table),
  1616. rec, mad->data);
  1617. *sa_query = &query->sa_query;
  1618. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  1619. if (ret < 0)
  1620. goto err2;
  1621. return ret;
  1622. err2:
  1623. *sa_query = NULL;
  1624. ib_sa_client_put(query->sa_query.client);
  1625. free_mad(&query->sa_query);
  1626. err1:
  1627. kfree(query);
  1628. return ret;
  1629. }
  1630. EXPORT_SYMBOL(ib_sa_service_rec_query);
  1631. static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
  1632. int status,
  1633. struct ib_sa_mad *mad)
  1634. {
  1635. struct ib_sa_mcmember_query *query =
  1636. container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
  1637. if (mad) {
  1638. struct ib_sa_mcmember_rec rec;
  1639. ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  1640. mad->data, &rec);
  1641. query->callback(status, &rec, query->context);
  1642. } else
  1643. query->callback(status, NULL, query->context);
  1644. }
  1645. static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
  1646. {
  1647. kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
  1648. }
  1649. int ib_sa_mcmember_rec_query(struct ib_sa_client *client,
  1650. struct ib_device *device, u8 port_num,
  1651. u8 method,
  1652. struct ib_sa_mcmember_rec *rec,
  1653. ib_sa_comp_mask comp_mask,
  1654. int timeout_ms, gfp_t gfp_mask,
  1655. void (*callback)(int status,
  1656. struct ib_sa_mcmember_rec *resp,
  1657. void *context),
  1658. void *context,
  1659. struct ib_sa_query **sa_query)
  1660. {
  1661. struct ib_sa_mcmember_query *query;
  1662. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  1663. struct ib_sa_port *port;
  1664. struct ib_mad_agent *agent;
  1665. struct ib_sa_mad *mad;
  1666. int ret;
  1667. if (!sa_dev)
  1668. return -ENODEV;
  1669. port = &sa_dev->port[port_num - sa_dev->start_port];
  1670. agent = port->agent;
  1671. query = kzalloc(sizeof(*query), gfp_mask);
  1672. if (!query)
  1673. return -ENOMEM;
  1674. query->sa_query.port = port;
  1675. ret = alloc_mad(&query->sa_query, gfp_mask);
  1676. if (ret)
  1677. goto err1;
  1678. ib_sa_client_get(client);
  1679. query->sa_query.client = client;
  1680. query->callback = callback;
  1681. query->context = context;
  1682. mad = query->sa_query.mad_buf->mad;
  1683. init_mad(&query->sa_query, agent);
  1684. query->sa_query.callback = callback ? ib_sa_mcmember_rec_callback : NULL;
  1685. query->sa_query.release = ib_sa_mcmember_rec_release;
  1686. mad->mad_hdr.method = method;
  1687. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
  1688. mad->sa_hdr.comp_mask = comp_mask;
  1689. ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  1690. rec, mad->data);
  1691. *sa_query = &query->sa_query;
  1692. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  1693. if (ret < 0)
  1694. goto err2;
  1695. return ret;
  1696. err2:
  1697. *sa_query = NULL;
  1698. ib_sa_client_put(query->sa_query.client);
  1699. free_mad(&query->sa_query);
  1700. err1:
  1701. kfree(query);
  1702. return ret;
  1703. }
  1704. /* Support GuidInfoRecord */
  1705. static void ib_sa_guidinfo_rec_callback(struct ib_sa_query *sa_query,
  1706. int status,
  1707. struct ib_sa_mad *mad)
  1708. {
  1709. struct ib_sa_guidinfo_query *query =
  1710. container_of(sa_query, struct ib_sa_guidinfo_query, sa_query);
  1711. if (mad) {
  1712. struct ib_sa_guidinfo_rec rec;
  1713. ib_unpack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table),
  1714. mad->data, &rec);
  1715. query->callback(status, &rec, query->context);
  1716. } else
  1717. query->callback(status, NULL, query->context);
  1718. }
  1719. static void ib_sa_guidinfo_rec_release(struct ib_sa_query *sa_query)
  1720. {
  1721. kfree(container_of(sa_query, struct ib_sa_guidinfo_query, sa_query));
  1722. }
  1723. int ib_sa_guid_info_rec_query(struct ib_sa_client *client,
  1724. struct ib_device *device, u8 port_num,
  1725. struct ib_sa_guidinfo_rec *rec,
  1726. ib_sa_comp_mask comp_mask, u8 method,
  1727. int timeout_ms, gfp_t gfp_mask,
  1728. void (*callback)(int status,
  1729. struct ib_sa_guidinfo_rec *resp,
  1730. void *context),
  1731. void *context,
  1732. struct ib_sa_query **sa_query)
  1733. {
  1734. struct ib_sa_guidinfo_query *query;
  1735. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  1736. struct ib_sa_port *port;
  1737. struct ib_mad_agent *agent;
  1738. struct ib_sa_mad *mad;
  1739. int ret;
  1740. if (!sa_dev)
  1741. return -ENODEV;
  1742. if (method != IB_MGMT_METHOD_GET &&
  1743. method != IB_MGMT_METHOD_SET &&
  1744. method != IB_SA_METHOD_DELETE) {
  1745. return -EINVAL;
  1746. }
  1747. port = &sa_dev->port[port_num - sa_dev->start_port];
  1748. agent = port->agent;
  1749. query = kzalloc(sizeof(*query), gfp_mask);
  1750. if (!query)
  1751. return -ENOMEM;
  1752. query->sa_query.port = port;
  1753. ret = alloc_mad(&query->sa_query, gfp_mask);
  1754. if (ret)
  1755. goto err1;
  1756. ib_sa_client_get(client);
  1757. query->sa_query.client = client;
  1758. query->callback = callback;
  1759. query->context = context;
  1760. mad = query->sa_query.mad_buf->mad;
  1761. init_mad(&query->sa_query, agent);
  1762. query->sa_query.callback = callback ? ib_sa_guidinfo_rec_callback : NULL;
  1763. query->sa_query.release = ib_sa_guidinfo_rec_release;
  1764. mad->mad_hdr.method = method;
  1765. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_GUID_INFO_REC);
  1766. mad->sa_hdr.comp_mask = comp_mask;
  1767. ib_pack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table), rec,
  1768. mad->data);
  1769. *sa_query = &query->sa_query;
  1770. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  1771. if (ret < 0)
  1772. goto err2;
  1773. return ret;
  1774. err2:
  1775. *sa_query = NULL;
  1776. ib_sa_client_put(query->sa_query.client);
  1777. free_mad(&query->sa_query);
  1778. err1:
  1779. kfree(query);
  1780. return ret;
  1781. }
  1782. EXPORT_SYMBOL(ib_sa_guid_info_rec_query);
  1783. bool ib_sa_sendonly_fullmem_support(struct ib_sa_client *client,
  1784. struct ib_device *device,
  1785. u8 port_num)
  1786. {
  1787. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  1788. struct ib_sa_port *port;
  1789. bool ret = false;
  1790. unsigned long flags;
  1791. if (!sa_dev)
  1792. return ret;
  1793. port = &sa_dev->port[port_num - sa_dev->start_port];
  1794. spin_lock_irqsave(&port->classport_lock, flags);
  1795. if ((port->classport_info.valid) &&
  1796. (port->classport_info.data.type == RDMA_CLASS_PORT_INFO_IB))
  1797. ret = ib_get_cpi_capmask2(&port->classport_info.data.ib)
  1798. & IB_SA_CAP_MASK2_SENDONLY_FULL_MEM_SUPPORT;
  1799. spin_unlock_irqrestore(&port->classport_lock, flags);
  1800. return ret;
  1801. }
  1802. EXPORT_SYMBOL(ib_sa_sendonly_fullmem_support);
  1803. struct ib_classport_info_context {
  1804. struct completion done;
  1805. struct ib_sa_query *sa_query;
  1806. };
  1807. static void ib_classportinfo_cb(void *context)
  1808. {
  1809. struct ib_classport_info_context *cb_ctx = context;
  1810. complete(&cb_ctx->done);
  1811. }
  1812. static void ib_sa_classport_info_rec_callback(struct ib_sa_query *sa_query,
  1813. int status,
  1814. struct ib_sa_mad *mad)
  1815. {
  1816. unsigned long flags;
  1817. struct ib_sa_classport_info_query *query =
  1818. container_of(sa_query, struct ib_sa_classport_info_query, sa_query);
  1819. struct ib_sa_classport_cache *info = &sa_query->port->classport_info;
  1820. if (mad) {
  1821. if (sa_query->flags & IB_SA_QUERY_OPA) {
  1822. struct opa_class_port_info rec;
  1823. ib_unpack(opa_classport_info_rec_table,
  1824. ARRAY_SIZE(opa_classport_info_rec_table),
  1825. mad->data, &rec);
  1826. spin_lock_irqsave(&sa_query->port->classport_lock,
  1827. flags);
  1828. if (!status && !info->valid) {
  1829. memcpy(&info->data.opa, &rec,
  1830. sizeof(info->data.opa));
  1831. info->valid = true;
  1832. info->data.type = RDMA_CLASS_PORT_INFO_OPA;
  1833. }
  1834. spin_unlock_irqrestore(&sa_query->port->classport_lock,
  1835. flags);
  1836. } else {
  1837. struct ib_class_port_info rec;
  1838. ib_unpack(ib_classport_info_rec_table,
  1839. ARRAY_SIZE(ib_classport_info_rec_table),
  1840. mad->data, &rec);
  1841. spin_lock_irqsave(&sa_query->port->classport_lock,
  1842. flags);
  1843. if (!status && !info->valid) {
  1844. memcpy(&info->data.ib, &rec,
  1845. sizeof(info->data.ib));
  1846. info->valid = true;
  1847. info->data.type = RDMA_CLASS_PORT_INFO_IB;
  1848. }
  1849. spin_unlock_irqrestore(&sa_query->port->classport_lock,
  1850. flags);
  1851. }
  1852. }
  1853. query->callback(query->context);
  1854. }
  1855. static void ib_sa_classport_info_rec_release(struct ib_sa_query *sa_query)
  1856. {
  1857. kfree(container_of(sa_query, struct ib_sa_classport_info_query,
  1858. sa_query));
  1859. }
  1860. static int ib_sa_classport_info_rec_query(struct ib_sa_port *port,
  1861. int timeout_ms,
  1862. void (*callback)(void *context),
  1863. void *context,
  1864. struct ib_sa_query **sa_query)
  1865. {
  1866. struct ib_mad_agent *agent;
  1867. struct ib_sa_classport_info_query *query;
  1868. struct ib_sa_mad *mad;
  1869. gfp_t gfp_mask = GFP_KERNEL;
  1870. int ret;
  1871. agent = port->agent;
  1872. query = kzalloc(sizeof(*query), gfp_mask);
  1873. if (!query)
  1874. return -ENOMEM;
  1875. query->sa_query.port = port;
  1876. query->sa_query.flags |= rdma_cap_opa_ah(port->agent->device,
  1877. port->port_num) ?
  1878. IB_SA_QUERY_OPA : 0;
  1879. ret = alloc_mad(&query->sa_query, gfp_mask);
  1880. if (ret)
  1881. goto err_free;
  1882. query->callback = callback;
  1883. query->context = context;
  1884. mad = query->sa_query.mad_buf->mad;
  1885. init_mad(&query->sa_query, agent);
  1886. query->sa_query.callback = ib_sa_classport_info_rec_callback;
  1887. query->sa_query.release = ib_sa_classport_info_rec_release;
  1888. mad->mad_hdr.method = IB_MGMT_METHOD_GET;
  1889. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_CLASS_PORTINFO);
  1890. mad->sa_hdr.comp_mask = 0;
  1891. *sa_query = &query->sa_query;
  1892. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  1893. if (ret < 0)
  1894. goto err_free_mad;
  1895. return ret;
  1896. err_free_mad:
  1897. *sa_query = NULL;
  1898. free_mad(&query->sa_query);
  1899. err_free:
  1900. kfree(query);
  1901. return ret;
  1902. }
  1903. static void update_ib_cpi(struct work_struct *work)
  1904. {
  1905. struct ib_sa_port *port =
  1906. container_of(work, struct ib_sa_port, ib_cpi_work.work);
  1907. struct ib_classport_info_context *cb_context;
  1908. unsigned long flags;
  1909. int ret;
  1910. /* If the classport info is valid, nothing
  1911. * to do here.
  1912. */
  1913. spin_lock_irqsave(&port->classport_lock, flags);
  1914. if (port->classport_info.valid) {
  1915. spin_unlock_irqrestore(&port->classport_lock, flags);
  1916. return;
  1917. }
  1918. spin_unlock_irqrestore(&port->classport_lock, flags);
  1919. cb_context = kmalloc(sizeof(*cb_context), GFP_KERNEL);
  1920. if (!cb_context)
  1921. goto err_nomem;
  1922. init_completion(&cb_context->done);
  1923. ret = ib_sa_classport_info_rec_query(port, 3000,
  1924. ib_classportinfo_cb, cb_context,
  1925. &cb_context->sa_query);
  1926. if (ret < 0)
  1927. goto free_cb_err;
  1928. wait_for_completion(&cb_context->done);
  1929. free_cb_err:
  1930. kfree(cb_context);
  1931. spin_lock_irqsave(&port->classport_lock, flags);
  1932. /* If the classport info is still not valid, the query should have
  1933. * failed for some reason. Retry issuing the query
  1934. */
  1935. if (!port->classport_info.valid) {
  1936. port->classport_info.retry_cnt++;
  1937. if (port->classport_info.retry_cnt <=
  1938. IB_SA_CPI_MAX_RETRY_CNT) {
  1939. unsigned long delay =
  1940. msecs_to_jiffies(IB_SA_CPI_RETRY_WAIT);
  1941. queue_delayed_work(ib_wq, &port->ib_cpi_work, delay);
  1942. }
  1943. }
  1944. spin_unlock_irqrestore(&port->classport_lock, flags);
  1945. err_nomem:
  1946. return;
  1947. }
  1948. static void send_handler(struct ib_mad_agent *agent,
  1949. struct ib_mad_send_wc *mad_send_wc)
  1950. {
  1951. struct ib_sa_query *query = mad_send_wc->send_buf->context[0];
  1952. unsigned long flags;
  1953. if (query->callback)
  1954. switch (mad_send_wc->status) {
  1955. case IB_WC_SUCCESS:
  1956. /* No callback -- already got recv */
  1957. break;
  1958. case IB_WC_RESP_TIMEOUT_ERR:
  1959. query->callback(query, -ETIMEDOUT, NULL);
  1960. break;
  1961. case IB_WC_WR_FLUSH_ERR:
  1962. query->callback(query, -EINTR, NULL);
  1963. break;
  1964. default:
  1965. query->callback(query, -EIO, NULL);
  1966. break;
  1967. }
  1968. spin_lock_irqsave(&idr_lock, flags);
  1969. idr_remove(&query_idr, query->id);
  1970. spin_unlock_irqrestore(&idr_lock, flags);
  1971. free_mad(query);
  1972. if (query->client)
  1973. ib_sa_client_put(query->client);
  1974. query->release(query);
  1975. }
  1976. static void recv_handler(struct ib_mad_agent *mad_agent,
  1977. struct ib_mad_send_buf *send_buf,
  1978. struct ib_mad_recv_wc *mad_recv_wc)
  1979. {
  1980. struct ib_sa_query *query;
  1981. if (!send_buf)
  1982. return;
  1983. query = send_buf->context[0];
  1984. if (query->callback) {
  1985. if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
  1986. query->callback(query,
  1987. mad_recv_wc->recv_buf.mad->mad_hdr.status ?
  1988. -EINVAL : 0,
  1989. (struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
  1990. else
  1991. query->callback(query, -EIO, NULL);
  1992. }
  1993. ib_free_recv_mad(mad_recv_wc);
  1994. }
  1995. static void update_sm_ah(struct work_struct *work)
  1996. {
  1997. struct ib_sa_port *port =
  1998. container_of(work, struct ib_sa_port, update_task);
  1999. struct ib_sa_sm_ah *new_ah;
  2000. struct ib_port_attr port_attr;
  2001. struct rdma_ah_attr ah_attr;
  2002. bool grh_required;
  2003. if (ib_query_port(port->agent->device, port->port_num, &port_attr)) {
  2004. pr_warn("Couldn't query port\n");
  2005. return;
  2006. }
  2007. new_ah = kmalloc(sizeof(*new_ah), GFP_KERNEL);
  2008. if (!new_ah)
  2009. return;
  2010. kref_init(&new_ah->ref);
  2011. new_ah->src_path_mask = (1 << port_attr.lmc) - 1;
  2012. new_ah->pkey_index = 0;
  2013. if (ib_find_pkey(port->agent->device, port->port_num,
  2014. IB_DEFAULT_PKEY_FULL, &new_ah->pkey_index))
  2015. pr_err("Couldn't find index for default PKey\n");
  2016. memset(&ah_attr, 0, sizeof(ah_attr));
  2017. ah_attr.type = rdma_ah_find_type(port->agent->device,
  2018. port->port_num);
  2019. rdma_ah_set_dlid(&ah_attr, port_attr.sm_lid);
  2020. rdma_ah_set_sl(&ah_attr, port_attr.sm_sl);
  2021. rdma_ah_set_port_num(&ah_attr, port->port_num);
  2022. grh_required = rdma_is_grh_required(port->agent->device,
  2023. port->port_num);
  2024. /*
  2025. * The OPA sm_lid of 0xFFFF needs special handling so that it can be
  2026. * differentiated from a permissive LID of 0xFFFF. We set the
  2027. * grh_required flag here so the SA can program the DGID in the
  2028. * address handle appropriately
  2029. */
  2030. if (ah_attr.type == RDMA_AH_ATTR_TYPE_OPA &&
  2031. (grh_required ||
  2032. port_attr.sm_lid == be16_to_cpu(IB_LID_PERMISSIVE)))
  2033. rdma_ah_set_make_grd(&ah_attr, true);
  2034. if (ah_attr.type == RDMA_AH_ATTR_TYPE_IB && grh_required) {
  2035. rdma_ah_set_ah_flags(&ah_attr, IB_AH_GRH);
  2036. rdma_ah_set_subnet_prefix(&ah_attr,
  2037. cpu_to_be64(port_attr.subnet_prefix));
  2038. rdma_ah_set_interface_id(&ah_attr,
  2039. cpu_to_be64(IB_SA_WELL_KNOWN_GUID));
  2040. }
  2041. new_ah->ah = rdma_create_ah(port->agent->qp->pd, &ah_attr);
  2042. if (IS_ERR(new_ah->ah)) {
  2043. pr_warn("Couldn't create new SM AH\n");
  2044. kfree(new_ah);
  2045. return;
  2046. }
  2047. spin_lock_irq(&port->ah_lock);
  2048. if (port->sm_ah)
  2049. kref_put(&port->sm_ah->ref, free_sm_ah);
  2050. port->sm_ah = new_ah;
  2051. spin_unlock_irq(&port->ah_lock);
  2052. }
  2053. static void ib_sa_event(struct ib_event_handler *handler,
  2054. struct ib_event *event)
  2055. {
  2056. if (event->event == IB_EVENT_PORT_ERR ||
  2057. event->event == IB_EVENT_PORT_ACTIVE ||
  2058. event->event == IB_EVENT_LID_CHANGE ||
  2059. event->event == IB_EVENT_PKEY_CHANGE ||
  2060. event->event == IB_EVENT_SM_CHANGE ||
  2061. event->event == IB_EVENT_CLIENT_REREGISTER) {
  2062. unsigned long flags;
  2063. struct ib_sa_device *sa_dev =
  2064. container_of(handler, typeof(*sa_dev), event_handler);
  2065. u8 port_num = event->element.port_num - sa_dev->start_port;
  2066. struct ib_sa_port *port = &sa_dev->port[port_num];
  2067. if (!rdma_cap_ib_sa(handler->device, port->port_num))
  2068. return;
  2069. spin_lock_irqsave(&port->ah_lock, flags);
  2070. if (port->sm_ah)
  2071. kref_put(&port->sm_ah->ref, free_sm_ah);
  2072. port->sm_ah = NULL;
  2073. spin_unlock_irqrestore(&port->ah_lock, flags);
  2074. if (event->event == IB_EVENT_SM_CHANGE ||
  2075. event->event == IB_EVENT_CLIENT_REREGISTER ||
  2076. event->event == IB_EVENT_LID_CHANGE ||
  2077. event->event == IB_EVENT_PORT_ACTIVE) {
  2078. unsigned long delay =
  2079. msecs_to_jiffies(IB_SA_CPI_RETRY_WAIT);
  2080. spin_lock_irqsave(&port->classport_lock, flags);
  2081. port->classport_info.valid = false;
  2082. port->classport_info.retry_cnt = 0;
  2083. spin_unlock_irqrestore(&port->classport_lock, flags);
  2084. queue_delayed_work(ib_wq,
  2085. &port->ib_cpi_work, delay);
  2086. }
  2087. queue_work(ib_wq, &sa_dev->port[port_num].update_task);
  2088. }
  2089. }
  2090. static void ib_sa_add_one(struct ib_device *device)
  2091. {
  2092. struct ib_sa_device *sa_dev;
  2093. int s, e, i;
  2094. int count = 0;
  2095. s = rdma_start_port(device);
  2096. e = rdma_end_port(device);
  2097. sa_dev = kzalloc(sizeof *sa_dev +
  2098. (e - s + 1) * sizeof (struct ib_sa_port),
  2099. GFP_KERNEL);
  2100. if (!sa_dev)
  2101. return;
  2102. sa_dev->start_port = s;
  2103. sa_dev->end_port = e;
  2104. for (i = 0; i <= e - s; ++i) {
  2105. spin_lock_init(&sa_dev->port[i].ah_lock);
  2106. if (!rdma_cap_ib_sa(device, i + 1))
  2107. continue;
  2108. sa_dev->port[i].sm_ah = NULL;
  2109. sa_dev->port[i].port_num = i + s;
  2110. spin_lock_init(&sa_dev->port[i].classport_lock);
  2111. sa_dev->port[i].classport_info.valid = false;
  2112. sa_dev->port[i].agent =
  2113. ib_register_mad_agent(device, i + s, IB_QPT_GSI,
  2114. NULL, 0, send_handler,
  2115. recv_handler, sa_dev, 0);
  2116. if (IS_ERR(sa_dev->port[i].agent))
  2117. goto err;
  2118. INIT_WORK(&sa_dev->port[i].update_task, update_sm_ah);
  2119. INIT_DELAYED_WORK(&sa_dev->port[i].ib_cpi_work,
  2120. update_ib_cpi);
  2121. count++;
  2122. }
  2123. if (!count)
  2124. goto free;
  2125. ib_set_client_data(device, &sa_client, sa_dev);
  2126. /*
  2127. * We register our event handler after everything is set up,
  2128. * and then update our cached info after the event handler is
  2129. * registered to avoid any problems if a port changes state
  2130. * during our initialization.
  2131. */
  2132. INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
  2133. ib_register_event_handler(&sa_dev->event_handler);
  2134. for (i = 0; i <= e - s; ++i) {
  2135. if (rdma_cap_ib_sa(device, i + 1))
  2136. update_sm_ah(&sa_dev->port[i].update_task);
  2137. }
  2138. return;
  2139. err:
  2140. while (--i >= 0) {
  2141. if (rdma_cap_ib_sa(device, i + 1))
  2142. ib_unregister_mad_agent(sa_dev->port[i].agent);
  2143. }
  2144. free:
  2145. kfree(sa_dev);
  2146. return;
  2147. }
  2148. static void ib_sa_remove_one(struct ib_device *device, void *client_data)
  2149. {
  2150. struct ib_sa_device *sa_dev = client_data;
  2151. int i;
  2152. if (!sa_dev)
  2153. return;
  2154. ib_unregister_event_handler(&sa_dev->event_handler);
  2155. flush_workqueue(ib_wq);
  2156. for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
  2157. if (rdma_cap_ib_sa(device, i + 1)) {
  2158. cancel_delayed_work_sync(&sa_dev->port[i].ib_cpi_work);
  2159. ib_unregister_mad_agent(sa_dev->port[i].agent);
  2160. if (sa_dev->port[i].sm_ah)
  2161. kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
  2162. }
  2163. }
  2164. kfree(sa_dev);
  2165. }
  2166. int ib_sa_init(void)
  2167. {
  2168. int ret;
  2169. get_random_bytes(&tid, sizeof tid);
  2170. atomic_set(&ib_nl_sa_request_seq, 0);
  2171. ret = ib_register_client(&sa_client);
  2172. if (ret) {
  2173. pr_err("Couldn't register ib_sa client\n");
  2174. goto err1;
  2175. }
  2176. ret = mcast_init();
  2177. if (ret) {
  2178. pr_err("Couldn't initialize multicast handling\n");
  2179. goto err2;
  2180. }
  2181. ib_nl_wq = alloc_ordered_workqueue("ib_nl_sa_wq", WQ_MEM_RECLAIM);
  2182. if (!ib_nl_wq) {
  2183. ret = -ENOMEM;
  2184. goto err3;
  2185. }
  2186. INIT_DELAYED_WORK(&ib_nl_timed_work, ib_nl_request_timeout);
  2187. return 0;
  2188. err3:
  2189. mcast_cleanup();
  2190. err2:
  2191. ib_unregister_client(&sa_client);
  2192. err1:
  2193. return ret;
  2194. }
  2195. void ib_sa_cleanup(void)
  2196. {
  2197. cancel_delayed_work(&ib_nl_timed_work);
  2198. flush_workqueue(ib_nl_wq);
  2199. destroy_workqueue(ib_nl_wq);
  2200. mcast_cleanup();
  2201. ib_unregister_client(&sa_client);
  2202. idr_destroy(&query_idr);
  2203. }