sa_query.c 59 KB

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