sa_query.c 65 KB

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