verbs.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630
  1. /*
  2. * Copyright (c) 2004 Mellanox Technologies Ltd. All rights reserved.
  3. * Copyright (c) 2004 Infinicon Corporation. All rights reserved.
  4. * Copyright (c) 2004 Intel Corporation. All rights reserved.
  5. * Copyright (c) 2004 Topspin Corporation. All rights reserved.
  6. * Copyright (c) 2004 Voltaire Corporation. All rights reserved.
  7. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  8. * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.
  9. *
  10. * This software is available to you under a choice of one of two
  11. * licenses. You may choose to be licensed under the terms of the GNU
  12. * General Public License (GPL) Version 2, available from the file
  13. * COPYING in the main directory of this source tree, or the
  14. * OpenIB.org BSD license below:
  15. *
  16. * Redistribution and use in source and binary forms, with or
  17. * without modification, are permitted provided that the following
  18. * conditions are met:
  19. *
  20. * - Redistributions of source code must retain the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer.
  23. *
  24. * - Redistributions in binary form must reproduce the above
  25. * copyright notice, this list of conditions and the following
  26. * disclaimer in the documentation and/or other materials
  27. * provided with the distribution.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  30. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  32. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  33. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  34. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  35. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  36. * SOFTWARE.
  37. */
  38. #include <linux/errno.h>
  39. #include <linux/err.h>
  40. #include <linux/export.h>
  41. #include <linux/string.h>
  42. #include <linux/slab.h>
  43. #include <linux/in.h>
  44. #include <linux/in6.h>
  45. #include <net/addrconf.h>
  46. #include <linux/security.h>
  47. #include <rdma/ib_verbs.h>
  48. #include <rdma/ib_cache.h>
  49. #include <rdma/ib_addr.h>
  50. #include <rdma/rw.h>
  51. #include "core_priv.h"
  52. static int ib_resolve_eth_dmac(struct ib_device *device,
  53. struct rdma_ah_attr *ah_attr);
  54. static const char * const ib_events[] = {
  55. [IB_EVENT_CQ_ERR] = "CQ error",
  56. [IB_EVENT_QP_FATAL] = "QP fatal error",
  57. [IB_EVENT_QP_REQ_ERR] = "QP request error",
  58. [IB_EVENT_QP_ACCESS_ERR] = "QP access error",
  59. [IB_EVENT_COMM_EST] = "communication established",
  60. [IB_EVENT_SQ_DRAINED] = "send queue drained",
  61. [IB_EVENT_PATH_MIG] = "path migration successful",
  62. [IB_EVENT_PATH_MIG_ERR] = "path migration error",
  63. [IB_EVENT_DEVICE_FATAL] = "device fatal error",
  64. [IB_EVENT_PORT_ACTIVE] = "port active",
  65. [IB_EVENT_PORT_ERR] = "port error",
  66. [IB_EVENT_LID_CHANGE] = "LID change",
  67. [IB_EVENT_PKEY_CHANGE] = "P_key change",
  68. [IB_EVENT_SM_CHANGE] = "SM change",
  69. [IB_EVENT_SRQ_ERR] = "SRQ error",
  70. [IB_EVENT_SRQ_LIMIT_REACHED] = "SRQ limit reached",
  71. [IB_EVENT_QP_LAST_WQE_REACHED] = "last WQE reached",
  72. [IB_EVENT_CLIENT_REREGISTER] = "client reregister",
  73. [IB_EVENT_GID_CHANGE] = "GID changed",
  74. };
  75. const char *__attribute_const__ ib_event_msg(enum ib_event_type event)
  76. {
  77. size_t index = event;
  78. return (index < ARRAY_SIZE(ib_events) && ib_events[index]) ?
  79. ib_events[index] : "unrecognized event";
  80. }
  81. EXPORT_SYMBOL(ib_event_msg);
  82. static const char * const wc_statuses[] = {
  83. [IB_WC_SUCCESS] = "success",
  84. [IB_WC_LOC_LEN_ERR] = "local length error",
  85. [IB_WC_LOC_QP_OP_ERR] = "local QP operation error",
  86. [IB_WC_LOC_EEC_OP_ERR] = "local EE context operation error",
  87. [IB_WC_LOC_PROT_ERR] = "local protection error",
  88. [IB_WC_WR_FLUSH_ERR] = "WR flushed",
  89. [IB_WC_MW_BIND_ERR] = "memory management operation error",
  90. [IB_WC_BAD_RESP_ERR] = "bad response error",
  91. [IB_WC_LOC_ACCESS_ERR] = "local access error",
  92. [IB_WC_REM_INV_REQ_ERR] = "invalid request error",
  93. [IB_WC_REM_ACCESS_ERR] = "remote access error",
  94. [IB_WC_REM_OP_ERR] = "remote operation error",
  95. [IB_WC_RETRY_EXC_ERR] = "transport retry counter exceeded",
  96. [IB_WC_RNR_RETRY_EXC_ERR] = "RNR retry counter exceeded",
  97. [IB_WC_LOC_RDD_VIOL_ERR] = "local RDD violation error",
  98. [IB_WC_REM_INV_RD_REQ_ERR] = "remote invalid RD request",
  99. [IB_WC_REM_ABORT_ERR] = "operation aborted",
  100. [IB_WC_INV_EECN_ERR] = "invalid EE context number",
  101. [IB_WC_INV_EEC_STATE_ERR] = "invalid EE context state",
  102. [IB_WC_FATAL_ERR] = "fatal error",
  103. [IB_WC_RESP_TIMEOUT_ERR] = "response timeout error",
  104. [IB_WC_GENERAL_ERR] = "general error",
  105. };
  106. const char *__attribute_const__ ib_wc_status_msg(enum ib_wc_status status)
  107. {
  108. size_t index = status;
  109. return (index < ARRAY_SIZE(wc_statuses) && wc_statuses[index]) ?
  110. wc_statuses[index] : "unrecognized status";
  111. }
  112. EXPORT_SYMBOL(ib_wc_status_msg);
  113. __attribute_const__ int ib_rate_to_mult(enum ib_rate rate)
  114. {
  115. switch (rate) {
  116. case IB_RATE_2_5_GBPS: return 1;
  117. case IB_RATE_5_GBPS: return 2;
  118. case IB_RATE_10_GBPS: return 4;
  119. case IB_RATE_20_GBPS: return 8;
  120. case IB_RATE_30_GBPS: return 12;
  121. case IB_RATE_40_GBPS: return 16;
  122. case IB_RATE_60_GBPS: return 24;
  123. case IB_RATE_80_GBPS: return 32;
  124. case IB_RATE_120_GBPS: return 48;
  125. case IB_RATE_14_GBPS: return 6;
  126. case IB_RATE_56_GBPS: return 22;
  127. case IB_RATE_112_GBPS: return 45;
  128. case IB_RATE_168_GBPS: return 67;
  129. case IB_RATE_25_GBPS: return 10;
  130. case IB_RATE_100_GBPS: return 40;
  131. case IB_RATE_200_GBPS: return 80;
  132. case IB_RATE_300_GBPS: return 120;
  133. default: return -1;
  134. }
  135. }
  136. EXPORT_SYMBOL(ib_rate_to_mult);
  137. __attribute_const__ enum ib_rate mult_to_ib_rate(int mult)
  138. {
  139. switch (mult) {
  140. case 1: return IB_RATE_2_5_GBPS;
  141. case 2: return IB_RATE_5_GBPS;
  142. case 4: return IB_RATE_10_GBPS;
  143. case 8: return IB_RATE_20_GBPS;
  144. case 12: return IB_RATE_30_GBPS;
  145. case 16: return IB_RATE_40_GBPS;
  146. case 24: return IB_RATE_60_GBPS;
  147. case 32: return IB_RATE_80_GBPS;
  148. case 48: return IB_RATE_120_GBPS;
  149. case 6: return IB_RATE_14_GBPS;
  150. case 22: return IB_RATE_56_GBPS;
  151. case 45: return IB_RATE_112_GBPS;
  152. case 67: return IB_RATE_168_GBPS;
  153. case 10: return IB_RATE_25_GBPS;
  154. case 40: return IB_RATE_100_GBPS;
  155. case 80: return IB_RATE_200_GBPS;
  156. case 120: return IB_RATE_300_GBPS;
  157. default: return IB_RATE_PORT_CURRENT;
  158. }
  159. }
  160. EXPORT_SYMBOL(mult_to_ib_rate);
  161. __attribute_const__ int ib_rate_to_mbps(enum ib_rate rate)
  162. {
  163. switch (rate) {
  164. case IB_RATE_2_5_GBPS: return 2500;
  165. case IB_RATE_5_GBPS: return 5000;
  166. case IB_RATE_10_GBPS: return 10000;
  167. case IB_RATE_20_GBPS: return 20000;
  168. case IB_RATE_30_GBPS: return 30000;
  169. case IB_RATE_40_GBPS: return 40000;
  170. case IB_RATE_60_GBPS: return 60000;
  171. case IB_RATE_80_GBPS: return 80000;
  172. case IB_RATE_120_GBPS: return 120000;
  173. case IB_RATE_14_GBPS: return 14062;
  174. case IB_RATE_56_GBPS: return 56250;
  175. case IB_RATE_112_GBPS: return 112500;
  176. case IB_RATE_168_GBPS: return 168750;
  177. case IB_RATE_25_GBPS: return 25781;
  178. case IB_RATE_100_GBPS: return 103125;
  179. case IB_RATE_200_GBPS: return 206250;
  180. case IB_RATE_300_GBPS: return 309375;
  181. default: return -1;
  182. }
  183. }
  184. EXPORT_SYMBOL(ib_rate_to_mbps);
  185. __attribute_const__ enum rdma_transport_type
  186. rdma_node_get_transport(enum rdma_node_type node_type)
  187. {
  188. if (node_type == RDMA_NODE_USNIC)
  189. return RDMA_TRANSPORT_USNIC;
  190. if (node_type == RDMA_NODE_USNIC_UDP)
  191. return RDMA_TRANSPORT_USNIC_UDP;
  192. if (node_type == RDMA_NODE_RNIC)
  193. return RDMA_TRANSPORT_IWARP;
  194. return RDMA_TRANSPORT_IB;
  195. }
  196. EXPORT_SYMBOL(rdma_node_get_transport);
  197. enum rdma_link_layer rdma_port_get_link_layer(struct ib_device *device, u8 port_num)
  198. {
  199. enum rdma_transport_type lt;
  200. if (device->get_link_layer)
  201. return device->get_link_layer(device, port_num);
  202. lt = rdma_node_get_transport(device->node_type);
  203. if (lt == RDMA_TRANSPORT_IB)
  204. return IB_LINK_LAYER_INFINIBAND;
  205. return IB_LINK_LAYER_ETHERNET;
  206. }
  207. EXPORT_SYMBOL(rdma_port_get_link_layer);
  208. /* Protection domains */
  209. /**
  210. * ib_alloc_pd - Allocates an unused protection domain.
  211. * @device: The device on which to allocate the protection domain.
  212. *
  213. * A protection domain object provides an association between QPs, shared
  214. * receive queues, address handles, memory regions, and memory windows.
  215. *
  216. * Every PD has a local_dma_lkey which can be used as the lkey value for local
  217. * memory operations.
  218. */
  219. struct ib_pd *__ib_alloc_pd(struct ib_device *device, unsigned int flags,
  220. const char *caller)
  221. {
  222. struct ib_pd *pd;
  223. int mr_access_flags = 0;
  224. pd = device->alloc_pd(device, NULL, NULL);
  225. if (IS_ERR(pd))
  226. return pd;
  227. pd->device = device;
  228. pd->uobject = NULL;
  229. pd->__internal_mr = NULL;
  230. atomic_set(&pd->usecnt, 0);
  231. pd->flags = flags;
  232. if (device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)
  233. pd->local_dma_lkey = device->local_dma_lkey;
  234. else
  235. mr_access_flags |= IB_ACCESS_LOCAL_WRITE;
  236. if (flags & IB_PD_UNSAFE_GLOBAL_RKEY) {
  237. pr_warn("%s: enabling unsafe global rkey\n", caller);
  238. mr_access_flags |= IB_ACCESS_REMOTE_READ | IB_ACCESS_REMOTE_WRITE;
  239. }
  240. pd->res.type = RDMA_RESTRACK_PD;
  241. pd->res.kern_name = caller;
  242. rdma_restrack_add(&pd->res);
  243. if (mr_access_flags) {
  244. struct ib_mr *mr;
  245. mr = pd->device->get_dma_mr(pd, mr_access_flags);
  246. if (IS_ERR(mr)) {
  247. ib_dealloc_pd(pd);
  248. return ERR_CAST(mr);
  249. }
  250. mr->device = pd->device;
  251. mr->pd = pd;
  252. mr->uobject = NULL;
  253. mr->need_inval = false;
  254. pd->__internal_mr = mr;
  255. if (!(device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY))
  256. pd->local_dma_lkey = pd->__internal_mr->lkey;
  257. if (flags & IB_PD_UNSAFE_GLOBAL_RKEY)
  258. pd->unsafe_global_rkey = pd->__internal_mr->rkey;
  259. }
  260. return pd;
  261. }
  262. EXPORT_SYMBOL(__ib_alloc_pd);
  263. /**
  264. * ib_dealloc_pd - Deallocates a protection domain.
  265. * @pd: The protection domain to deallocate.
  266. *
  267. * It is an error to call this function while any resources in the pd still
  268. * exist. The caller is responsible to synchronously destroy them and
  269. * guarantee no new allocations will happen.
  270. */
  271. void ib_dealloc_pd(struct ib_pd *pd)
  272. {
  273. int ret;
  274. if (pd->__internal_mr) {
  275. ret = pd->device->dereg_mr(pd->__internal_mr);
  276. WARN_ON(ret);
  277. pd->__internal_mr = NULL;
  278. }
  279. /* uverbs manipulates usecnt with proper locking, while the kabi
  280. requires the caller to guarantee we can't race here. */
  281. WARN_ON(atomic_read(&pd->usecnt));
  282. rdma_restrack_del(&pd->res);
  283. /* Making delalloc_pd a void return is a WIP, no driver should return
  284. an error here. */
  285. ret = pd->device->dealloc_pd(pd);
  286. WARN_ONCE(ret, "Infiniband HW driver failed dealloc_pd");
  287. }
  288. EXPORT_SYMBOL(ib_dealloc_pd);
  289. /* Address handles */
  290. /**
  291. * rdma_copy_ah_attr - Copy rdma ah attribute from source to destination.
  292. * @dest: Pointer to destination ah_attr. Contents of the destination
  293. * pointer is assumed to be invalid and attribute are overwritten.
  294. * @src: Pointer to source ah_attr.
  295. */
  296. void rdma_copy_ah_attr(struct rdma_ah_attr *dest,
  297. const struct rdma_ah_attr *src)
  298. {
  299. *dest = *src;
  300. if (dest->grh.sgid_attr)
  301. rdma_hold_gid_attr(dest->grh.sgid_attr);
  302. }
  303. EXPORT_SYMBOL(rdma_copy_ah_attr);
  304. /**
  305. * rdma_replace_ah_attr - Replace valid ah_attr with new new one.
  306. * @old: Pointer to existing ah_attr which needs to be replaced.
  307. * old is assumed to be valid or zero'd
  308. * @new: Pointer to the new ah_attr.
  309. *
  310. * rdma_replace_ah_attr() first releases any reference in the old ah_attr if
  311. * old the ah_attr is valid; after that it copies the new attribute and holds
  312. * the reference to the replaced ah_attr.
  313. */
  314. void rdma_replace_ah_attr(struct rdma_ah_attr *old,
  315. const struct rdma_ah_attr *new)
  316. {
  317. rdma_destroy_ah_attr(old);
  318. *old = *new;
  319. if (old->grh.sgid_attr)
  320. rdma_hold_gid_attr(old->grh.sgid_attr);
  321. }
  322. EXPORT_SYMBOL(rdma_replace_ah_attr);
  323. /**
  324. * rdma_move_ah_attr - Move ah_attr pointed by source to destination.
  325. * @dest: Pointer to destination ah_attr to copy to.
  326. * dest is assumed to be valid or zero'd
  327. * @src: Pointer to the new ah_attr.
  328. *
  329. * rdma_move_ah_attr() first releases any reference in the destination ah_attr
  330. * if it is valid. This also transfers ownership of internal references from
  331. * src to dest, making src invalid in the process. No new reference of the src
  332. * ah_attr is taken.
  333. */
  334. void rdma_move_ah_attr(struct rdma_ah_attr *dest, struct rdma_ah_attr *src)
  335. {
  336. rdma_destroy_ah_attr(dest);
  337. *dest = *src;
  338. src->grh.sgid_attr = NULL;
  339. }
  340. EXPORT_SYMBOL(rdma_move_ah_attr);
  341. /*
  342. * Validate that the rdma_ah_attr is valid for the device before passing it
  343. * off to the driver.
  344. */
  345. static int rdma_check_ah_attr(struct ib_device *device,
  346. struct rdma_ah_attr *ah_attr)
  347. {
  348. if (!rdma_is_port_valid(device, ah_attr->port_num))
  349. return -EINVAL;
  350. if ((rdma_is_grh_required(device, ah_attr->port_num) ||
  351. ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) &&
  352. !(ah_attr->ah_flags & IB_AH_GRH))
  353. return -EINVAL;
  354. if (ah_attr->grh.sgid_attr) {
  355. /*
  356. * Make sure the passed sgid_attr is consistent with the
  357. * parameters
  358. */
  359. if (ah_attr->grh.sgid_attr->index != ah_attr->grh.sgid_index ||
  360. ah_attr->grh.sgid_attr->port_num != ah_attr->port_num)
  361. return -EINVAL;
  362. }
  363. return 0;
  364. }
  365. /*
  366. * If the ah requires a GRH then ensure that sgid_attr pointer is filled in.
  367. * On success the caller is responsible to call rdma_unfill_sgid_attr().
  368. */
  369. static int rdma_fill_sgid_attr(struct ib_device *device,
  370. struct rdma_ah_attr *ah_attr,
  371. const struct ib_gid_attr **old_sgid_attr)
  372. {
  373. const struct ib_gid_attr *sgid_attr;
  374. struct ib_global_route *grh;
  375. int ret;
  376. *old_sgid_attr = ah_attr->grh.sgid_attr;
  377. ret = rdma_check_ah_attr(device, ah_attr);
  378. if (ret)
  379. return ret;
  380. if (!(ah_attr->ah_flags & IB_AH_GRH))
  381. return 0;
  382. grh = rdma_ah_retrieve_grh(ah_attr);
  383. if (grh->sgid_attr)
  384. return 0;
  385. sgid_attr =
  386. rdma_get_gid_attr(device, ah_attr->port_num, grh->sgid_index);
  387. if (IS_ERR(sgid_attr))
  388. return PTR_ERR(sgid_attr);
  389. /* Move ownerhip of the kref into the ah_attr */
  390. grh->sgid_attr = sgid_attr;
  391. return 0;
  392. }
  393. static void rdma_unfill_sgid_attr(struct rdma_ah_attr *ah_attr,
  394. const struct ib_gid_attr *old_sgid_attr)
  395. {
  396. /*
  397. * Fill didn't change anything, the caller retains ownership of
  398. * whatever it passed
  399. */
  400. if (ah_attr->grh.sgid_attr == old_sgid_attr)
  401. return;
  402. /*
  403. * Otherwise, we need to undo what rdma_fill_sgid_attr so the caller
  404. * doesn't see any change in the rdma_ah_attr. If we get here
  405. * old_sgid_attr is NULL.
  406. */
  407. rdma_destroy_ah_attr(ah_attr);
  408. }
  409. static const struct ib_gid_attr *
  410. rdma_update_sgid_attr(struct rdma_ah_attr *ah_attr,
  411. const struct ib_gid_attr *old_attr)
  412. {
  413. if (old_attr)
  414. rdma_put_gid_attr(old_attr);
  415. if (ah_attr->ah_flags & IB_AH_GRH) {
  416. rdma_hold_gid_attr(ah_attr->grh.sgid_attr);
  417. return ah_attr->grh.sgid_attr;
  418. }
  419. return NULL;
  420. }
  421. static struct ib_ah *_rdma_create_ah(struct ib_pd *pd,
  422. struct rdma_ah_attr *ah_attr,
  423. struct ib_udata *udata)
  424. {
  425. struct ib_ah *ah;
  426. if (!pd->device->create_ah)
  427. return ERR_PTR(-EOPNOTSUPP);
  428. ah = pd->device->create_ah(pd, ah_attr, udata);
  429. if (!IS_ERR(ah)) {
  430. ah->device = pd->device;
  431. ah->pd = pd;
  432. ah->uobject = NULL;
  433. ah->type = ah_attr->type;
  434. ah->sgid_attr = rdma_update_sgid_attr(ah_attr, NULL);
  435. atomic_inc(&pd->usecnt);
  436. }
  437. return ah;
  438. }
  439. /**
  440. * rdma_create_ah - Creates an address handle for the
  441. * given address vector.
  442. * @pd: The protection domain associated with the address handle.
  443. * @ah_attr: The attributes of the address vector.
  444. *
  445. * It returns 0 on success and returns appropriate error code on error.
  446. * The address handle is used to reference a local or global destination
  447. * in all UD QP post sends.
  448. */
  449. struct ib_ah *rdma_create_ah(struct ib_pd *pd, struct rdma_ah_attr *ah_attr)
  450. {
  451. const struct ib_gid_attr *old_sgid_attr;
  452. struct ib_ah *ah;
  453. int ret;
  454. ret = rdma_fill_sgid_attr(pd->device, ah_attr, &old_sgid_attr);
  455. if (ret)
  456. return ERR_PTR(ret);
  457. ah = _rdma_create_ah(pd, ah_attr, NULL);
  458. rdma_unfill_sgid_attr(ah_attr, old_sgid_attr);
  459. return ah;
  460. }
  461. EXPORT_SYMBOL(rdma_create_ah);
  462. /**
  463. * rdma_create_user_ah - Creates an address handle for the
  464. * given address vector.
  465. * It resolves destination mac address for ah attribute of RoCE type.
  466. * @pd: The protection domain associated with the address handle.
  467. * @ah_attr: The attributes of the address vector.
  468. * @udata: pointer to user's input output buffer information need by
  469. * provider driver.
  470. *
  471. * It returns 0 on success and returns appropriate error code on error.
  472. * The address handle is used to reference a local or global destination
  473. * in all UD QP post sends.
  474. */
  475. struct ib_ah *rdma_create_user_ah(struct ib_pd *pd,
  476. struct rdma_ah_attr *ah_attr,
  477. struct ib_udata *udata)
  478. {
  479. const struct ib_gid_attr *old_sgid_attr;
  480. struct ib_ah *ah;
  481. int err;
  482. err = rdma_fill_sgid_attr(pd->device, ah_attr, &old_sgid_attr);
  483. if (err)
  484. return ERR_PTR(err);
  485. if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) {
  486. err = ib_resolve_eth_dmac(pd->device, ah_attr);
  487. if (err) {
  488. ah = ERR_PTR(err);
  489. goto out;
  490. }
  491. }
  492. ah = _rdma_create_ah(pd, ah_attr, udata);
  493. out:
  494. rdma_unfill_sgid_attr(ah_attr, old_sgid_attr);
  495. return ah;
  496. }
  497. EXPORT_SYMBOL(rdma_create_user_ah);
  498. int ib_get_rdma_header_version(const union rdma_network_hdr *hdr)
  499. {
  500. const struct iphdr *ip4h = (struct iphdr *)&hdr->roce4grh;
  501. struct iphdr ip4h_checked;
  502. const struct ipv6hdr *ip6h = (struct ipv6hdr *)&hdr->ibgrh;
  503. /* If it's IPv6, the version must be 6, otherwise, the first
  504. * 20 bytes (before the IPv4 header) are garbled.
  505. */
  506. if (ip6h->version != 6)
  507. return (ip4h->version == 4) ? 4 : 0;
  508. /* version may be 6 or 4 because the first 20 bytes could be garbled */
  509. /* RoCE v2 requires no options, thus header length
  510. * must be 5 words
  511. */
  512. if (ip4h->ihl != 5)
  513. return 6;
  514. /* Verify checksum.
  515. * We can't write on scattered buffers so we need to copy to
  516. * temp buffer.
  517. */
  518. memcpy(&ip4h_checked, ip4h, sizeof(ip4h_checked));
  519. ip4h_checked.check = 0;
  520. ip4h_checked.check = ip_fast_csum((u8 *)&ip4h_checked, 5);
  521. /* if IPv4 header checksum is OK, believe it */
  522. if (ip4h->check == ip4h_checked.check)
  523. return 4;
  524. return 6;
  525. }
  526. EXPORT_SYMBOL(ib_get_rdma_header_version);
  527. static enum rdma_network_type ib_get_net_type_by_grh(struct ib_device *device,
  528. u8 port_num,
  529. const struct ib_grh *grh)
  530. {
  531. int grh_version;
  532. if (rdma_protocol_ib(device, port_num))
  533. return RDMA_NETWORK_IB;
  534. grh_version = ib_get_rdma_header_version((union rdma_network_hdr *)grh);
  535. if (grh_version == 4)
  536. return RDMA_NETWORK_IPV4;
  537. if (grh->next_hdr == IPPROTO_UDP)
  538. return RDMA_NETWORK_IPV6;
  539. return RDMA_NETWORK_ROCE_V1;
  540. }
  541. struct find_gid_index_context {
  542. u16 vlan_id;
  543. enum ib_gid_type gid_type;
  544. };
  545. static bool find_gid_index(const union ib_gid *gid,
  546. const struct ib_gid_attr *gid_attr,
  547. void *context)
  548. {
  549. struct find_gid_index_context *ctx = context;
  550. if (ctx->gid_type != gid_attr->gid_type)
  551. return false;
  552. if ((!!(ctx->vlan_id != 0xffff) == !is_vlan_dev(gid_attr->ndev)) ||
  553. (is_vlan_dev(gid_attr->ndev) &&
  554. vlan_dev_vlan_id(gid_attr->ndev) != ctx->vlan_id))
  555. return false;
  556. return true;
  557. }
  558. static const struct ib_gid_attr *
  559. get_sgid_attr_from_eth(struct ib_device *device, u8 port_num,
  560. u16 vlan_id, const union ib_gid *sgid,
  561. enum ib_gid_type gid_type)
  562. {
  563. struct find_gid_index_context context = {.vlan_id = vlan_id,
  564. .gid_type = gid_type};
  565. return rdma_find_gid_by_filter(device, sgid, port_num, find_gid_index,
  566. &context);
  567. }
  568. int ib_get_gids_from_rdma_hdr(const union rdma_network_hdr *hdr,
  569. enum rdma_network_type net_type,
  570. union ib_gid *sgid, union ib_gid *dgid)
  571. {
  572. struct sockaddr_in src_in;
  573. struct sockaddr_in dst_in;
  574. __be32 src_saddr, dst_saddr;
  575. if (!sgid || !dgid)
  576. return -EINVAL;
  577. if (net_type == RDMA_NETWORK_IPV4) {
  578. memcpy(&src_in.sin_addr.s_addr,
  579. &hdr->roce4grh.saddr, 4);
  580. memcpy(&dst_in.sin_addr.s_addr,
  581. &hdr->roce4grh.daddr, 4);
  582. src_saddr = src_in.sin_addr.s_addr;
  583. dst_saddr = dst_in.sin_addr.s_addr;
  584. ipv6_addr_set_v4mapped(src_saddr,
  585. (struct in6_addr *)sgid);
  586. ipv6_addr_set_v4mapped(dst_saddr,
  587. (struct in6_addr *)dgid);
  588. return 0;
  589. } else if (net_type == RDMA_NETWORK_IPV6 ||
  590. net_type == RDMA_NETWORK_IB) {
  591. *dgid = hdr->ibgrh.dgid;
  592. *sgid = hdr->ibgrh.sgid;
  593. return 0;
  594. } else {
  595. return -EINVAL;
  596. }
  597. }
  598. EXPORT_SYMBOL(ib_get_gids_from_rdma_hdr);
  599. /* Resolve destination mac address and hop limit for unicast destination
  600. * GID entry, considering the source GID entry as well.
  601. * ah_attribute must have have valid port_num, sgid_index.
  602. */
  603. static int ib_resolve_unicast_gid_dmac(struct ib_device *device,
  604. struct rdma_ah_attr *ah_attr)
  605. {
  606. struct ib_global_route *grh = rdma_ah_retrieve_grh(ah_attr);
  607. const struct ib_gid_attr *sgid_attr = grh->sgid_attr;
  608. int hop_limit = 0xff;
  609. int ret = 0;
  610. /* If destination is link local and source GID is RoCEv1,
  611. * IP stack is not used.
  612. */
  613. if (rdma_link_local_addr((struct in6_addr *)grh->dgid.raw) &&
  614. sgid_attr->gid_type == IB_GID_TYPE_ROCE) {
  615. rdma_get_ll_mac((struct in6_addr *)grh->dgid.raw,
  616. ah_attr->roce.dmac);
  617. return ret;
  618. }
  619. ret = rdma_addr_find_l2_eth_by_grh(&sgid_attr->gid, &grh->dgid,
  620. ah_attr->roce.dmac,
  621. sgid_attr->ndev, &hop_limit);
  622. grh->hop_limit = hop_limit;
  623. return ret;
  624. }
  625. /*
  626. * This function initializes address handle attributes from the incoming packet.
  627. * Incoming packet has dgid of the receiver node on which this code is
  628. * getting executed and, sgid contains the GID of the sender.
  629. *
  630. * When resolving mac address of destination, the arrived dgid is used
  631. * as sgid and, sgid is used as dgid because sgid contains destinations
  632. * GID whom to respond to.
  633. *
  634. * On success the caller is responsible to call rdma_destroy_ah_attr on the
  635. * attr.
  636. */
  637. int ib_init_ah_attr_from_wc(struct ib_device *device, u8 port_num,
  638. const struct ib_wc *wc, const struct ib_grh *grh,
  639. struct rdma_ah_attr *ah_attr)
  640. {
  641. u32 flow_class;
  642. int ret;
  643. enum rdma_network_type net_type = RDMA_NETWORK_IB;
  644. enum ib_gid_type gid_type = IB_GID_TYPE_IB;
  645. const struct ib_gid_attr *sgid_attr;
  646. int hoplimit = 0xff;
  647. union ib_gid dgid;
  648. union ib_gid sgid;
  649. might_sleep();
  650. memset(ah_attr, 0, sizeof *ah_attr);
  651. ah_attr->type = rdma_ah_find_type(device, port_num);
  652. if (rdma_cap_eth_ah(device, port_num)) {
  653. if (wc->wc_flags & IB_WC_WITH_NETWORK_HDR_TYPE)
  654. net_type = wc->network_hdr_type;
  655. else
  656. net_type = ib_get_net_type_by_grh(device, port_num, grh);
  657. gid_type = ib_network_to_gid_type(net_type);
  658. }
  659. ret = ib_get_gids_from_rdma_hdr((union rdma_network_hdr *)grh, net_type,
  660. &sgid, &dgid);
  661. if (ret)
  662. return ret;
  663. rdma_ah_set_sl(ah_attr, wc->sl);
  664. rdma_ah_set_port_num(ah_attr, port_num);
  665. if (rdma_protocol_roce(device, port_num)) {
  666. u16 vlan_id = wc->wc_flags & IB_WC_WITH_VLAN ?
  667. wc->vlan_id : 0xffff;
  668. if (!(wc->wc_flags & IB_WC_GRH))
  669. return -EPROTOTYPE;
  670. sgid_attr = get_sgid_attr_from_eth(device, port_num,
  671. vlan_id, &dgid,
  672. gid_type);
  673. if (IS_ERR(sgid_attr))
  674. return PTR_ERR(sgid_attr);
  675. flow_class = be32_to_cpu(grh->version_tclass_flow);
  676. rdma_move_grh_sgid_attr(ah_attr,
  677. &sgid,
  678. flow_class & 0xFFFFF,
  679. hoplimit,
  680. (flow_class >> 20) & 0xFF,
  681. sgid_attr);
  682. ret = ib_resolve_unicast_gid_dmac(device, ah_attr);
  683. if (ret)
  684. rdma_destroy_ah_attr(ah_attr);
  685. return ret;
  686. } else {
  687. rdma_ah_set_dlid(ah_attr, wc->slid);
  688. rdma_ah_set_path_bits(ah_attr, wc->dlid_path_bits);
  689. if ((wc->wc_flags & IB_WC_GRH) == 0)
  690. return 0;
  691. if (dgid.global.interface_id !=
  692. cpu_to_be64(IB_SA_WELL_KNOWN_GUID)) {
  693. sgid_attr = rdma_find_gid_by_port(
  694. device, &dgid, IB_GID_TYPE_IB, port_num, NULL);
  695. } else
  696. sgid_attr = rdma_get_gid_attr(device, port_num, 0);
  697. if (IS_ERR(sgid_attr))
  698. return PTR_ERR(sgid_attr);
  699. flow_class = be32_to_cpu(grh->version_tclass_flow);
  700. rdma_move_grh_sgid_attr(ah_attr,
  701. &sgid,
  702. flow_class & 0xFFFFF,
  703. hoplimit,
  704. (flow_class >> 20) & 0xFF,
  705. sgid_attr);
  706. return 0;
  707. }
  708. }
  709. EXPORT_SYMBOL(ib_init_ah_attr_from_wc);
  710. /**
  711. * rdma_move_grh_sgid_attr - Sets the sgid attribute of GRH, taking ownership
  712. * of the reference
  713. *
  714. * @attr: Pointer to AH attribute structure
  715. * @dgid: Destination GID
  716. * @flow_label: Flow label
  717. * @hop_limit: Hop limit
  718. * @traffic_class: traffic class
  719. * @sgid_attr: Pointer to SGID attribute
  720. *
  721. * This takes ownership of the sgid_attr reference. The caller must ensure
  722. * rdma_destroy_ah_attr() is called before destroying the rdma_ah_attr after
  723. * calling this function.
  724. */
  725. void rdma_move_grh_sgid_attr(struct rdma_ah_attr *attr, union ib_gid *dgid,
  726. u32 flow_label, u8 hop_limit, u8 traffic_class,
  727. const struct ib_gid_attr *sgid_attr)
  728. {
  729. rdma_ah_set_grh(attr, dgid, flow_label, sgid_attr->index, hop_limit,
  730. traffic_class);
  731. attr->grh.sgid_attr = sgid_attr;
  732. }
  733. EXPORT_SYMBOL(rdma_move_grh_sgid_attr);
  734. /**
  735. * rdma_destroy_ah_attr - Release reference to SGID attribute of
  736. * ah attribute.
  737. * @ah_attr: Pointer to ah attribute
  738. *
  739. * Release reference to the SGID attribute of the ah attribute if it is
  740. * non NULL. It is safe to call this multiple times, and safe to call it on
  741. * a zero initialized ah_attr.
  742. */
  743. void rdma_destroy_ah_attr(struct rdma_ah_attr *ah_attr)
  744. {
  745. if (ah_attr->grh.sgid_attr) {
  746. rdma_put_gid_attr(ah_attr->grh.sgid_attr);
  747. ah_attr->grh.sgid_attr = NULL;
  748. }
  749. }
  750. EXPORT_SYMBOL(rdma_destroy_ah_attr);
  751. struct ib_ah *ib_create_ah_from_wc(struct ib_pd *pd, const struct ib_wc *wc,
  752. const struct ib_grh *grh, u8 port_num)
  753. {
  754. struct rdma_ah_attr ah_attr;
  755. struct ib_ah *ah;
  756. int ret;
  757. ret = ib_init_ah_attr_from_wc(pd->device, port_num, wc, grh, &ah_attr);
  758. if (ret)
  759. return ERR_PTR(ret);
  760. ah = rdma_create_ah(pd, &ah_attr);
  761. rdma_destroy_ah_attr(&ah_attr);
  762. return ah;
  763. }
  764. EXPORT_SYMBOL(ib_create_ah_from_wc);
  765. int rdma_modify_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr)
  766. {
  767. const struct ib_gid_attr *old_sgid_attr;
  768. int ret;
  769. if (ah->type != ah_attr->type)
  770. return -EINVAL;
  771. ret = rdma_fill_sgid_attr(ah->device, ah_attr, &old_sgid_attr);
  772. if (ret)
  773. return ret;
  774. ret = ah->device->modify_ah ?
  775. ah->device->modify_ah(ah, ah_attr) :
  776. -EOPNOTSUPP;
  777. ah->sgid_attr = rdma_update_sgid_attr(ah_attr, ah->sgid_attr);
  778. rdma_unfill_sgid_attr(ah_attr, old_sgid_attr);
  779. return ret;
  780. }
  781. EXPORT_SYMBOL(rdma_modify_ah);
  782. int rdma_query_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr)
  783. {
  784. ah_attr->grh.sgid_attr = NULL;
  785. return ah->device->query_ah ?
  786. ah->device->query_ah(ah, ah_attr) :
  787. -EOPNOTSUPP;
  788. }
  789. EXPORT_SYMBOL(rdma_query_ah);
  790. int rdma_destroy_ah(struct ib_ah *ah)
  791. {
  792. const struct ib_gid_attr *sgid_attr = ah->sgid_attr;
  793. struct ib_pd *pd;
  794. int ret;
  795. pd = ah->pd;
  796. ret = ah->device->destroy_ah(ah);
  797. if (!ret) {
  798. atomic_dec(&pd->usecnt);
  799. if (sgid_attr)
  800. rdma_put_gid_attr(sgid_attr);
  801. }
  802. return ret;
  803. }
  804. EXPORT_SYMBOL(rdma_destroy_ah);
  805. /* Shared receive queues */
  806. struct ib_srq *ib_create_srq(struct ib_pd *pd,
  807. struct ib_srq_init_attr *srq_init_attr)
  808. {
  809. struct ib_srq *srq;
  810. if (!pd->device->create_srq)
  811. return ERR_PTR(-EOPNOTSUPP);
  812. srq = pd->device->create_srq(pd, srq_init_attr, NULL);
  813. if (!IS_ERR(srq)) {
  814. srq->device = pd->device;
  815. srq->pd = pd;
  816. srq->uobject = NULL;
  817. srq->event_handler = srq_init_attr->event_handler;
  818. srq->srq_context = srq_init_attr->srq_context;
  819. srq->srq_type = srq_init_attr->srq_type;
  820. if (ib_srq_has_cq(srq->srq_type)) {
  821. srq->ext.cq = srq_init_attr->ext.cq;
  822. atomic_inc(&srq->ext.cq->usecnt);
  823. }
  824. if (srq->srq_type == IB_SRQT_XRC) {
  825. srq->ext.xrc.xrcd = srq_init_attr->ext.xrc.xrcd;
  826. atomic_inc(&srq->ext.xrc.xrcd->usecnt);
  827. }
  828. atomic_inc(&pd->usecnt);
  829. atomic_set(&srq->usecnt, 0);
  830. }
  831. return srq;
  832. }
  833. EXPORT_SYMBOL(ib_create_srq);
  834. int ib_modify_srq(struct ib_srq *srq,
  835. struct ib_srq_attr *srq_attr,
  836. enum ib_srq_attr_mask srq_attr_mask)
  837. {
  838. return srq->device->modify_srq ?
  839. srq->device->modify_srq(srq, srq_attr, srq_attr_mask, NULL) :
  840. -EOPNOTSUPP;
  841. }
  842. EXPORT_SYMBOL(ib_modify_srq);
  843. int ib_query_srq(struct ib_srq *srq,
  844. struct ib_srq_attr *srq_attr)
  845. {
  846. return srq->device->query_srq ?
  847. srq->device->query_srq(srq, srq_attr) : -EOPNOTSUPP;
  848. }
  849. EXPORT_SYMBOL(ib_query_srq);
  850. int ib_destroy_srq(struct ib_srq *srq)
  851. {
  852. struct ib_pd *pd;
  853. enum ib_srq_type srq_type;
  854. struct ib_xrcd *uninitialized_var(xrcd);
  855. struct ib_cq *uninitialized_var(cq);
  856. int ret;
  857. if (atomic_read(&srq->usecnt))
  858. return -EBUSY;
  859. pd = srq->pd;
  860. srq_type = srq->srq_type;
  861. if (ib_srq_has_cq(srq_type))
  862. cq = srq->ext.cq;
  863. if (srq_type == IB_SRQT_XRC)
  864. xrcd = srq->ext.xrc.xrcd;
  865. ret = srq->device->destroy_srq(srq);
  866. if (!ret) {
  867. atomic_dec(&pd->usecnt);
  868. if (srq_type == IB_SRQT_XRC)
  869. atomic_dec(&xrcd->usecnt);
  870. if (ib_srq_has_cq(srq_type))
  871. atomic_dec(&cq->usecnt);
  872. }
  873. return ret;
  874. }
  875. EXPORT_SYMBOL(ib_destroy_srq);
  876. /* Queue pairs */
  877. static void __ib_shared_qp_event_handler(struct ib_event *event, void *context)
  878. {
  879. struct ib_qp *qp = context;
  880. unsigned long flags;
  881. spin_lock_irqsave(&qp->device->event_handler_lock, flags);
  882. list_for_each_entry(event->element.qp, &qp->open_list, open_list)
  883. if (event->element.qp->event_handler)
  884. event->element.qp->event_handler(event, event->element.qp->qp_context);
  885. spin_unlock_irqrestore(&qp->device->event_handler_lock, flags);
  886. }
  887. static void __ib_insert_xrcd_qp(struct ib_xrcd *xrcd, struct ib_qp *qp)
  888. {
  889. mutex_lock(&xrcd->tgt_qp_mutex);
  890. list_add(&qp->xrcd_list, &xrcd->tgt_qp_list);
  891. mutex_unlock(&xrcd->tgt_qp_mutex);
  892. }
  893. static struct ib_qp *__ib_open_qp(struct ib_qp *real_qp,
  894. void (*event_handler)(struct ib_event *, void *),
  895. void *qp_context)
  896. {
  897. struct ib_qp *qp;
  898. unsigned long flags;
  899. int err;
  900. qp = kzalloc(sizeof *qp, GFP_KERNEL);
  901. if (!qp)
  902. return ERR_PTR(-ENOMEM);
  903. qp->real_qp = real_qp;
  904. err = ib_open_shared_qp_security(qp, real_qp->device);
  905. if (err) {
  906. kfree(qp);
  907. return ERR_PTR(err);
  908. }
  909. qp->real_qp = real_qp;
  910. atomic_inc(&real_qp->usecnt);
  911. qp->device = real_qp->device;
  912. qp->event_handler = event_handler;
  913. qp->qp_context = qp_context;
  914. qp->qp_num = real_qp->qp_num;
  915. qp->qp_type = real_qp->qp_type;
  916. spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
  917. list_add(&qp->open_list, &real_qp->open_list);
  918. spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
  919. return qp;
  920. }
  921. struct ib_qp *ib_open_qp(struct ib_xrcd *xrcd,
  922. struct ib_qp_open_attr *qp_open_attr)
  923. {
  924. struct ib_qp *qp, *real_qp;
  925. if (qp_open_attr->qp_type != IB_QPT_XRC_TGT)
  926. return ERR_PTR(-EINVAL);
  927. qp = ERR_PTR(-EINVAL);
  928. mutex_lock(&xrcd->tgt_qp_mutex);
  929. list_for_each_entry(real_qp, &xrcd->tgt_qp_list, xrcd_list) {
  930. if (real_qp->qp_num == qp_open_attr->qp_num) {
  931. qp = __ib_open_qp(real_qp, qp_open_attr->event_handler,
  932. qp_open_attr->qp_context);
  933. break;
  934. }
  935. }
  936. mutex_unlock(&xrcd->tgt_qp_mutex);
  937. return qp;
  938. }
  939. EXPORT_SYMBOL(ib_open_qp);
  940. static struct ib_qp *create_xrc_qp(struct ib_qp *qp,
  941. struct ib_qp_init_attr *qp_init_attr)
  942. {
  943. struct ib_qp *real_qp = qp;
  944. qp->event_handler = __ib_shared_qp_event_handler;
  945. qp->qp_context = qp;
  946. qp->pd = NULL;
  947. qp->send_cq = qp->recv_cq = NULL;
  948. qp->srq = NULL;
  949. qp->xrcd = qp_init_attr->xrcd;
  950. atomic_inc(&qp_init_attr->xrcd->usecnt);
  951. INIT_LIST_HEAD(&qp->open_list);
  952. qp = __ib_open_qp(real_qp, qp_init_attr->event_handler,
  953. qp_init_attr->qp_context);
  954. if (IS_ERR(qp))
  955. return qp;
  956. __ib_insert_xrcd_qp(qp_init_attr->xrcd, real_qp);
  957. return qp;
  958. }
  959. struct ib_qp *ib_create_qp(struct ib_pd *pd,
  960. struct ib_qp_init_attr *qp_init_attr)
  961. {
  962. struct ib_device *device = pd ? pd->device : qp_init_attr->xrcd->device;
  963. struct ib_qp *qp;
  964. int ret;
  965. if (qp_init_attr->rwq_ind_tbl &&
  966. (qp_init_attr->recv_cq ||
  967. qp_init_attr->srq || qp_init_attr->cap.max_recv_wr ||
  968. qp_init_attr->cap.max_recv_sge))
  969. return ERR_PTR(-EINVAL);
  970. /*
  971. * If the callers is using the RDMA API calculate the resources
  972. * needed for the RDMA READ/WRITE operations.
  973. *
  974. * Note that these callers need to pass in a port number.
  975. */
  976. if (qp_init_attr->cap.max_rdma_ctxs)
  977. rdma_rw_init_qp(device, qp_init_attr);
  978. qp = _ib_create_qp(device, pd, qp_init_attr, NULL, NULL);
  979. if (IS_ERR(qp))
  980. return qp;
  981. ret = ib_create_qp_security(qp, device);
  982. if (ret)
  983. goto err;
  984. qp->real_qp = qp;
  985. qp->qp_type = qp_init_attr->qp_type;
  986. qp->rwq_ind_tbl = qp_init_attr->rwq_ind_tbl;
  987. atomic_set(&qp->usecnt, 0);
  988. qp->mrs_used = 0;
  989. spin_lock_init(&qp->mr_lock);
  990. INIT_LIST_HEAD(&qp->rdma_mrs);
  991. INIT_LIST_HEAD(&qp->sig_mrs);
  992. qp->port = 0;
  993. if (qp_init_attr->qp_type == IB_QPT_XRC_TGT) {
  994. struct ib_qp *xrc_qp = create_xrc_qp(qp, qp_init_attr);
  995. if (IS_ERR(xrc_qp)) {
  996. ret = PTR_ERR(xrc_qp);
  997. goto err;
  998. }
  999. return xrc_qp;
  1000. }
  1001. qp->event_handler = qp_init_attr->event_handler;
  1002. qp->qp_context = qp_init_attr->qp_context;
  1003. if (qp_init_attr->qp_type == IB_QPT_XRC_INI) {
  1004. qp->recv_cq = NULL;
  1005. qp->srq = NULL;
  1006. } else {
  1007. qp->recv_cq = qp_init_attr->recv_cq;
  1008. if (qp_init_attr->recv_cq)
  1009. atomic_inc(&qp_init_attr->recv_cq->usecnt);
  1010. qp->srq = qp_init_attr->srq;
  1011. if (qp->srq)
  1012. atomic_inc(&qp_init_attr->srq->usecnt);
  1013. }
  1014. qp->send_cq = qp_init_attr->send_cq;
  1015. qp->xrcd = NULL;
  1016. atomic_inc(&pd->usecnt);
  1017. if (qp_init_attr->send_cq)
  1018. atomic_inc(&qp_init_attr->send_cq->usecnt);
  1019. if (qp_init_attr->rwq_ind_tbl)
  1020. atomic_inc(&qp->rwq_ind_tbl->usecnt);
  1021. if (qp_init_attr->cap.max_rdma_ctxs) {
  1022. ret = rdma_rw_init_mrs(qp, qp_init_attr);
  1023. if (ret)
  1024. goto err;
  1025. }
  1026. /*
  1027. * Note: all hw drivers guarantee that max_send_sge is lower than
  1028. * the device RDMA WRITE SGE limit but not all hw drivers ensure that
  1029. * max_send_sge <= max_sge_rd.
  1030. */
  1031. qp->max_write_sge = qp_init_attr->cap.max_send_sge;
  1032. qp->max_read_sge = min_t(u32, qp_init_attr->cap.max_send_sge,
  1033. device->attrs.max_sge_rd);
  1034. return qp;
  1035. err:
  1036. ib_destroy_qp(qp);
  1037. return ERR_PTR(ret);
  1038. }
  1039. EXPORT_SYMBOL(ib_create_qp);
  1040. static const struct {
  1041. int valid;
  1042. enum ib_qp_attr_mask req_param[IB_QPT_MAX];
  1043. enum ib_qp_attr_mask opt_param[IB_QPT_MAX];
  1044. } qp_state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
  1045. [IB_QPS_RESET] = {
  1046. [IB_QPS_RESET] = { .valid = 1 },
  1047. [IB_QPS_INIT] = {
  1048. .valid = 1,
  1049. .req_param = {
  1050. [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
  1051. IB_QP_PORT |
  1052. IB_QP_QKEY),
  1053. [IB_QPT_RAW_PACKET] = IB_QP_PORT,
  1054. [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
  1055. IB_QP_PORT |
  1056. IB_QP_ACCESS_FLAGS),
  1057. [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
  1058. IB_QP_PORT |
  1059. IB_QP_ACCESS_FLAGS),
  1060. [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX |
  1061. IB_QP_PORT |
  1062. IB_QP_ACCESS_FLAGS),
  1063. [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX |
  1064. IB_QP_PORT |
  1065. IB_QP_ACCESS_FLAGS),
  1066. [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
  1067. IB_QP_QKEY),
  1068. [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
  1069. IB_QP_QKEY),
  1070. }
  1071. },
  1072. },
  1073. [IB_QPS_INIT] = {
  1074. [IB_QPS_RESET] = { .valid = 1 },
  1075. [IB_QPS_ERR] = { .valid = 1 },
  1076. [IB_QPS_INIT] = {
  1077. .valid = 1,
  1078. .opt_param = {
  1079. [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
  1080. IB_QP_PORT |
  1081. IB_QP_QKEY),
  1082. [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
  1083. IB_QP_PORT |
  1084. IB_QP_ACCESS_FLAGS),
  1085. [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
  1086. IB_QP_PORT |
  1087. IB_QP_ACCESS_FLAGS),
  1088. [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX |
  1089. IB_QP_PORT |
  1090. IB_QP_ACCESS_FLAGS),
  1091. [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX |
  1092. IB_QP_PORT |
  1093. IB_QP_ACCESS_FLAGS),
  1094. [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
  1095. IB_QP_QKEY),
  1096. [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
  1097. IB_QP_QKEY),
  1098. }
  1099. },
  1100. [IB_QPS_RTR] = {
  1101. .valid = 1,
  1102. .req_param = {
  1103. [IB_QPT_UC] = (IB_QP_AV |
  1104. IB_QP_PATH_MTU |
  1105. IB_QP_DEST_QPN |
  1106. IB_QP_RQ_PSN),
  1107. [IB_QPT_RC] = (IB_QP_AV |
  1108. IB_QP_PATH_MTU |
  1109. IB_QP_DEST_QPN |
  1110. IB_QP_RQ_PSN |
  1111. IB_QP_MAX_DEST_RD_ATOMIC |
  1112. IB_QP_MIN_RNR_TIMER),
  1113. [IB_QPT_XRC_INI] = (IB_QP_AV |
  1114. IB_QP_PATH_MTU |
  1115. IB_QP_DEST_QPN |
  1116. IB_QP_RQ_PSN),
  1117. [IB_QPT_XRC_TGT] = (IB_QP_AV |
  1118. IB_QP_PATH_MTU |
  1119. IB_QP_DEST_QPN |
  1120. IB_QP_RQ_PSN |
  1121. IB_QP_MAX_DEST_RD_ATOMIC |
  1122. IB_QP_MIN_RNR_TIMER),
  1123. },
  1124. .opt_param = {
  1125. [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
  1126. IB_QP_QKEY),
  1127. [IB_QPT_UC] = (IB_QP_ALT_PATH |
  1128. IB_QP_ACCESS_FLAGS |
  1129. IB_QP_PKEY_INDEX),
  1130. [IB_QPT_RC] = (IB_QP_ALT_PATH |
  1131. IB_QP_ACCESS_FLAGS |
  1132. IB_QP_PKEY_INDEX),
  1133. [IB_QPT_XRC_INI] = (IB_QP_ALT_PATH |
  1134. IB_QP_ACCESS_FLAGS |
  1135. IB_QP_PKEY_INDEX),
  1136. [IB_QPT_XRC_TGT] = (IB_QP_ALT_PATH |
  1137. IB_QP_ACCESS_FLAGS |
  1138. IB_QP_PKEY_INDEX),
  1139. [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
  1140. IB_QP_QKEY),
  1141. [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
  1142. IB_QP_QKEY),
  1143. },
  1144. },
  1145. },
  1146. [IB_QPS_RTR] = {
  1147. [IB_QPS_RESET] = { .valid = 1 },
  1148. [IB_QPS_ERR] = { .valid = 1 },
  1149. [IB_QPS_RTS] = {
  1150. .valid = 1,
  1151. .req_param = {
  1152. [IB_QPT_UD] = IB_QP_SQ_PSN,
  1153. [IB_QPT_UC] = IB_QP_SQ_PSN,
  1154. [IB_QPT_RC] = (IB_QP_TIMEOUT |
  1155. IB_QP_RETRY_CNT |
  1156. IB_QP_RNR_RETRY |
  1157. IB_QP_SQ_PSN |
  1158. IB_QP_MAX_QP_RD_ATOMIC),
  1159. [IB_QPT_XRC_INI] = (IB_QP_TIMEOUT |
  1160. IB_QP_RETRY_CNT |
  1161. IB_QP_RNR_RETRY |
  1162. IB_QP_SQ_PSN |
  1163. IB_QP_MAX_QP_RD_ATOMIC),
  1164. [IB_QPT_XRC_TGT] = (IB_QP_TIMEOUT |
  1165. IB_QP_SQ_PSN),
  1166. [IB_QPT_SMI] = IB_QP_SQ_PSN,
  1167. [IB_QPT_GSI] = IB_QP_SQ_PSN,
  1168. },
  1169. .opt_param = {
  1170. [IB_QPT_UD] = (IB_QP_CUR_STATE |
  1171. IB_QP_QKEY),
  1172. [IB_QPT_UC] = (IB_QP_CUR_STATE |
  1173. IB_QP_ALT_PATH |
  1174. IB_QP_ACCESS_FLAGS |
  1175. IB_QP_PATH_MIG_STATE),
  1176. [IB_QPT_RC] = (IB_QP_CUR_STATE |
  1177. IB_QP_ALT_PATH |
  1178. IB_QP_ACCESS_FLAGS |
  1179. IB_QP_MIN_RNR_TIMER |
  1180. IB_QP_PATH_MIG_STATE),
  1181. [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
  1182. IB_QP_ALT_PATH |
  1183. IB_QP_ACCESS_FLAGS |
  1184. IB_QP_PATH_MIG_STATE),
  1185. [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
  1186. IB_QP_ALT_PATH |
  1187. IB_QP_ACCESS_FLAGS |
  1188. IB_QP_MIN_RNR_TIMER |
  1189. IB_QP_PATH_MIG_STATE),
  1190. [IB_QPT_SMI] = (IB_QP_CUR_STATE |
  1191. IB_QP_QKEY),
  1192. [IB_QPT_GSI] = (IB_QP_CUR_STATE |
  1193. IB_QP_QKEY),
  1194. [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
  1195. }
  1196. }
  1197. },
  1198. [IB_QPS_RTS] = {
  1199. [IB_QPS_RESET] = { .valid = 1 },
  1200. [IB_QPS_ERR] = { .valid = 1 },
  1201. [IB_QPS_RTS] = {
  1202. .valid = 1,
  1203. .opt_param = {
  1204. [IB_QPT_UD] = (IB_QP_CUR_STATE |
  1205. IB_QP_QKEY),
  1206. [IB_QPT_UC] = (IB_QP_CUR_STATE |
  1207. IB_QP_ACCESS_FLAGS |
  1208. IB_QP_ALT_PATH |
  1209. IB_QP_PATH_MIG_STATE),
  1210. [IB_QPT_RC] = (IB_QP_CUR_STATE |
  1211. IB_QP_ACCESS_FLAGS |
  1212. IB_QP_ALT_PATH |
  1213. IB_QP_PATH_MIG_STATE |
  1214. IB_QP_MIN_RNR_TIMER),
  1215. [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
  1216. IB_QP_ACCESS_FLAGS |
  1217. IB_QP_ALT_PATH |
  1218. IB_QP_PATH_MIG_STATE),
  1219. [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
  1220. IB_QP_ACCESS_FLAGS |
  1221. IB_QP_ALT_PATH |
  1222. IB_QP_PATH_MIG_STATE |
  1223. IB_QP_MIN_RNR_TIMER),
  1224. [IB_QPT_SMI] = (IB_QP_CUR_STATE |
  1225. IB_QP_QKEY),
  1226. [IB_QPT_GSI] = (IB_QP_CUR_STATE |
  1227. IB_QP_QKEY),
  1228. [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
  1229. }
  1230. },
  1231. [IB_QPS_SQD] = {
  1232. .valid = 1,
  1233. .opt_param = {
  1234. [IB_QPT_UD] = IB_QP_EN_SQD_ASYNC_NOTIFY,
  1235. [IB_QPT_UC] = IB_QP_EN_SQD_ASYNC_NOTIFY,
  1236. [IB_QPT_RC] = IB_QP_EN_SQD_ASYNC_NOTIFY,
  1237. [IB_QPT_XRC_INI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
  1238. [IB_QPT_XRC_TGT] = IB_QP_EN_SQD_ASYNC_NOTIFY, /* ??? */
  1239. [IB_QPT_SMI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
  1240. [IB_QPT_GSI] = IB_QP_EN_SQD_ASYNC_NOTIFY
  1241. }
  1242. },
  1243. },
  1244. [IB_QPS_SQD] = {
  1245. [IB_QPS_RESET] = { .valid = 1 },
  1246. [IB_QPS_ERR] = { .valid = 1 },
  1247. [IB_QPS_RTS] = {
  1248. .valid = 1,
  1249. .opt_param = {
  1250. [IB_QPT_UD] = (IB_QP_CUR_STATE |
  1251. IB_QP_QKEY),
  1252. [IB_QPT_UC] = (IB_QP_CUR_STATE |
  1253. IB_QP_ALT_PATH |
  1254. IB_QP_ACCESS_FLAGS |
  1255. IB_QP_PATH_MIG_STATE),
  1256. [IB_QPT_RC] = (IB_QP_CUR_STATE |
  1257. IB_QP_ALT_PATH |
  1258. IB_QP_ACCESS_FLAGS |
  1259. IB_QP_MIN_RNR_TIMER |
  1260. IB_QP_PATH_MIG_STATE),
  1261. [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
  1262. IB_QP_ALT_PATH |
  1263. IB_QP_ACCESS_FLAGS |
  1264. IB_QP_PATH_MIG_STATE),
  1265. [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
  1266. IB_QP_ALT_PATH |
  1267. IB_QP_ACCESS_FLAGS |
  1268. IB_QP_MIN_RNR_TIMER |
  1269. IB_QP_PATH_MIG_STATE),
  1270. [IB_QPT_SMI] = (IB_QP_CUR_STATE |
  1271. IB_QP_QKEY),
  1272. [IB_QPT_GSI] = (IB_QP_CUR_STATE |
  1273. IB_QP_QKEY),
  1274. }
  1275. },
  1276. [IB_QPS_SQD] = {
  1277. .valid = 1,
  1278. .opt_param = {
  1279. [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
  1280. IB_QP_QKEY),
  1281. [IB_QPT_UC] = (IB_QP_AV |
  1282. IB_QP_ALT_PATH |
  1283. IB_QP_ACCESS_FLAGS |
  1284. IB_QP_PKEY_INDEX |
  1285. IB_QP_PATH_MIG_STATE),
  1286. [IB_QPT_RC] = (IB_QP_PORT |
  1287. IB_QP_AV |
  1288. IB_QP_TIMEOUT |
  1289. IB_QP_RETRY_CNT |
  1290. IB_QP_RNR_RETRY |
  1291. IB_QP_MAX_QP_RD_ATOMIC |
  1292. IB_QP_MAX_DEST_RD_ATOMIC |
  1293. IB_QP_ALT_PATH |
  1294. IB_QP_ACCESS_FLAGS |
  1295. IB_QP_PKEY_INDEX |
  1296. IB_QP_MIN_RNR_TIMER |
  1297. IB_QP_PATH_MIG_STATE),
  1298. [IB_QPT_XRC_INI] = (IB_QP_PORT |
  1299. IB_QP_AV |
  1300. IB_QP_TIMEOUT |
  1301. IB_QP_RETRY_CNT |
  1302. IB_QP_RNR_RETRY |
  1303. IB_QP_MAX_QP_RD_ATOMIC |
  1304. IB_QP_ALT_PATH |
  1305. IB_QP_ACCESS_FLAGS |
  1306. IB_QP_PKEY_INDEX |
  1307. IB_QP_PATH_MIG_STATE),
  1308. [IB_QPT_XRC_TGT] = (IB_QP_PORT |
  1309. IB_QP_AV |
  1310. IB_QP_TIMEOUT |
  1311. IB_QP_MAX_DEST_RD_ATOMIC |
  1312. IB_QP_ALT_PATH |
  1313. IB_QP_ACCESS_FLAGS |
  1314. IB_QP_PKEY_INDEX |
  1315. IB_QP_MIN_RNR_TIMER |
  1316. IB_QP_PATH_MIG_STATE),
  1317. [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
  1318. IB_QP_QKEY),
  1319. [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
  1320. IB_QP_QKEY),
  1321. }
  1322. }
  1323. },
  1324. [IB_QPS_SQE] = {
  1325. [IB_QPS_RESET] = { .valid = 1 },
  1326. [IB_QPS_ERR] = { .valid = 1 },
  1327. [IB_QPS_RTS] = {
  1328. .valid = 1,
  1329. .opt_param = {
  1330. [IB_QPT_UD] = (IB_QP_CUR_STATE |
  1331. IB_QP_QKEY),
  1332. [IB_QPT_UC] = (IB_QP_CUR_STATE |
  1333. IB_QP_ACCESS_FLAGS),
  1334. [IB_QPT_SMI] = (IB_QP_CUR_STATE |
  1335. IB_QP_QKEY),
  1336. [IB_QPT_GSI] = (IB_QP_CUR_STATE |
  1337. IB_QP_QKEY),
  1338. }
  1339. }
  1340. },
  1341. [IB_QPS_ERR] = {
  1342. [IB_QPS_RESET] = { .valid = 1 },
  1343. [IB_QPS_ERR] = { .valid = 1 }
  1344. }
  1345. };
  1346. bool ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
  1347. enum ib_qp_type type, enum ib_qp_attr_mask mask,
  1348. enum rdma_link_layer ll)
  1349. {
  1350. enum ib_qp_attr_mask req_param, opt_param;
  1351. if (mask & IB_QP_CUR_STATE &&
  1352. cur_state != IB_QPS_RTR && cur_state != IB_QPS_RTS &&
  1353. cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE)
  1354. return false;
  1355. if (!qp_state_table[cur_state][next_state].valid)
  1356. return false;
  1357. req_param = qp_state_table[cur_state][next_state].req_param[type];
  1358. opt_param = qp_state_table[cur_state][next_state].opt_param[type];
  1359. if ((mask & req_param) != req_param)
  1360. return false;
  1361. if (mask & ~(req_param | opt_param | IB_QP_STATE))
  1362. return false;
  1363. return true;
  1364. }
  1365. EXPORT_SYMBOL(ib_modify_qp_is_ok);
  1366. /**
  1367. * ib_resolve_eth_dmac - Resolve destination mac address
  1368. * @device: Device to consider
  1369. * @ah_attr: address handle attribute which describes the
  1370. * source and destination parameters
  1371. * ib_resolve_eth_dmac() resolves destination mac address and L3 hop limit It
  1372. * returns 0 on success or appropriate error code. It initializes the
  1373. * necessary ah_attr fields when call is successful.
  1374. */
  1375. static int ib_resolve_eth_dmac(struct ib_device *device,
  1376. struct rdma_ah_attr *ah_attr)
  1377. {
  1378. int ret = 0;
  1379. if (rdma_is_multicast_addr((struct in6_addr *)ah_attr->grh.dgid.raw)) {
  1380. if (ipv6_addr_v4mapped((struct in6_addr *)ah_attr->grh.dgid.raw)) {
  1381. __be32 addr = 0;
  1382. memcpy(&addr, ah_attr->grh.dgid.raw + 12, 4);
  1383. ip_eth_mc_map(addr, (char *)ah_attr->roce.dmac);
  1384. } else {
  1385. ipv6_eth_mc_map((struct in6_addr *)ah_attr->grh.dgid.raw,
  1386. (char *)ah_attr->roce.dmac);
  1387. }
  1388. } else {
  1389. ret = ib_resolve_unicast_gid_dmac(device, ah_attr);
  1390. }
  1391. return ret;
  1392. }
  1393. static bool is_qp_type_connected(const struct ib_qp *qp)
  1394. {
  1395. return (qp->qp_type == IB_QPT_UC ||
  1396. qp->qp_type == IB_QPT_RC ||
  1397. qp->qp_type == IB_QPT_XRC_INI ||
  1398. qp->qp_type == IB_QPT_XRC_TGT);
  1399. }
  1400. /**
  1401. * IB core internal function to perform QP attributes modification.
  1402. */
  1403. static int _ib_modify_qp(struct ib_qp *qp, struct ib_qp_attr *attr,
  1404. int attr_mask, struct ib_udata *udata)
  1405. {
  1406. u8 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
  1407. const struct ib_gid_attr *old_sgid_attr_av;
  1408. const struct ib_gid_attr *old_sgid_attr_alt_av;
  1409. int ret;
  1410. if (attr_mask & IB_QP_AV) {
  1411. ret = rdma_fill_sgid_attr(qp->device, &attr->ah_attr,
  1412. &old_sgid_attr_av);
  1413. if (ret)
  1414. return ret;
  1415. }
  1416. if (attr_mask & IB_QP_ALT_PATH) {
  1417. /*
  1418. * FIXME: This does not track the migration state, so if the
  1419. * user loads a new alternate path after the HW has migrated
  1420. * from primary->alternate we will keep the wrong
  1421. * references. This is OK for IB because the reference
  1422. * counting does not serve any functional purpose.
  1423. */
  1424. ret = rdma_fill_sgid_attr(qp->device, &attr->alt_ah_attr,
  1425. &old_sgid_attr_alt_av);
  1426. if (ret)
  1427. goto out_av;
  1428. /*
  1429. * Today the core code can only handle alternate paths and APM
  1430. * for IB. Ban them in roce mode.
  1431. */
  1432. if (!(rdma_protocol_ib(qp->device,
  1433. attr->alt_ah_attr.port_num) &&
  1434. rdma_protocol_ib(qp->device, port))) {
  1435. ret = -EINVAL;
  1436. goto out;
  1437. }
  1438. }
  1439. /*
  1440. * If the user provided the qp_attr then we have to resolve it. Kernel
  1441. * users have to provide already resolved rdma_ah_attr's
  1442. */
  1443. if (udata && (attr_mask & IB_QP_AV) &&
  1444. attr->ah_attr.type == RDMA_AH_ATTR_TYPE_ROCE &&
  1445. is_qp_type_connected(qp)) {
  1446. ret = ib_resolve_eth_dmac(qp->device, &attr->ah_attr);
  1447. if (ret)
  1448. goto out;
  1449. }
  1450. if (rdma_ib_or_roce(qp->device, port)) {
  1451. if (attr_mask & IB_QP_RQ_PSN && attr->rq_psn & ~0xffffff) {
  1452. pr_warn("%s: %s rq_psn overflow, masking to 24 bits\n",
  1453. __func__, qp->device->name);
  1454. attr->rq_psn &= 0xffffff;
  1455. }
  1456. if (attr_mask & IB_QP_SQ_PSN && attr->sq_psn & ~0xffffff) {
  1457. pr_warn("%s: %s sq_psn overflow, masking to 24 bits\n",
  1458. __func__, qp->device->name);
  1459. attr->sq_psn &= 0xffffff;
  1460. }
  1461. }
  1462. ret = ib_security_modify_qp(qp, attr, attr_mask, udata);
  1463. if (ret)
  1464. goto out;
  1465. if (attr_mask & IB_QP_PORT)
  1466. qp->port = attr->port_num;
  1467. if (attr_mask & IB_QP_AV)
  1468. qp->av_sgid_attr =
  1469. rdma_update_sgid_attr(&attr->ah_attr, qp->av_sgid_attr);
  1470. if (attr_mask & IB_QP_ALT_PATH)
  1471. qp->alt_path_sgid_attr = rdma_update_sgid_attr(
  1472. &attr->alt_ah_attr, qp->alt_path_sgid_attr);
  1473. out:
  1474. if (attr_mask & IB_QP_ALT_PATH)
  1475. rdma_unfill_sgid_attr(&attr->alt_ah_attr, old_sgid_attr_alt_av);
  1476. out_av:
  1477. if (attr_mask & IB_QP_AV)
  1478. rdma_unfill_sgid_attr(&attr->ah_attr, old_sgid_attr_av);
  1479. return ret;
  1480. }
  1481. /**
  1482. * ib_modify_qp_with_udata - Modifies the attributes for the specified QP.
  1483. * @ib_qp: The QP to modify.
  1484. * @attr: On input, specifies the QP attributes to modify. On output,
  1485. * the current values of selected QP attributes are returned.
  1486. * @attr_mask: A bit-mask used to specify which attributes of the QP
  1487. * are being modified.
  1488. * @udata: pointer to user's input output buffer information
  1489. * are being modified.
  1490. * It returns 0 on success and returns appropriate error code on error.
  1491. */
  1492. int ib_modify_qp_with_udata(struct ib_qp *ib_qp, struct ib_qp_attr *attr,
  1493. int attr_mask, struct ib_udata *udata)
  1494. {
  1495. return _ib_modify_qp(ib_qp->real_qp, attr, attr_mask, udata);
  1496. }
  1497. EXPORT_SYMBOL(ib_modify_qp_with_udata);
  1498. int ib_get_eth_speed(struct ib_device *dev, u8 port_num, u8 *speed, u8 *width)
  1499. {
  1500. int rc;
  1501. u32 netdev_speed;
  1502. struct net_device *netdev;
  1503. struct ethtool_link_ksettings lksettings;
  1504. if (rdma_port_get_link_layer(dev, port_num) != IB_LINK_LAYER_ETHERNET)
  1505. return -EINVAL;
  1506. if (!dev->get_netdev)
  1507. return -EOPNOTSUPP;
  1508. netdev = dev->get_netdev(dev, port_num);
  1509. if (!netdev)
  1510. return -ENODEV;
  1511. rtnl_lock();
  1512. rc = __ethtool_get_link_ksettings(netdev, &lksettings);
  1513. rtnl_unlock();
  1514. dev_put(netdev);
  1515. if (!rc && lksettings.base.speed != (u32)SPEED_UNKNOWN) {
  1516. netdev_speed = lksettings.base.speed;
  1517. } else {
  1518. netdev_speed = SPEED_1000;
  1519. pr_warn("%s speed is unknown, defaulting to %d\n", netdev->name,
  1520. netdev_speed);
  1521. }
  1522. if (netdev_speed <= SPEED_1000) {
  1523. *width = IB_WIDTH_1X;
  1524. *speed = IB_SPEED_SDR;
  1525. } else if (netdev_speed <= SPEED_10000) {
  1526. *width = IB_WIDTH_1X;
  1527. *speed = IB_SPEED_FDR10;
  1528. } else if (netdev_speed <= SPEED_20000) {
  1529. *width = IB_WIDTH_4X;
  1530. *speed = IB_SPEED_DDR;
  1531. } else if (netdev_speed <= SPEED_25000) {
  1532. *width = IB_WIDTH_1X;
  1533. *speed = IB_SPEED_EDR;
  1534. } else if (netdev_speed <= SPEED_40000) {
  1535. *width = IB_WIDTH_4X;
  1536. *speed = IB_SPEED_FDR10;
  1537. } else {
  1538. *width = IB_WIDTH_4X;
  1539. *speed = IB_SPEED_EDR;
  1540. }
  1541. return 0;
  1542. }
  1543. EXPORT_SYMBOL(ib_get_eth_speed);
  1544. int ib_modify_qp(struct ib_qp *qp,
  1545. struct ib_qp_attr *qp_attr,
  1546. int qp_attr_mask)
  1547. {
  1548. return _ib_modify_qp(qp->real_qp, qp_attr, qp_attr_mask, NULL);
  1549. }
  1550. EXPORT_SYMBOL(ib_modify_qp);
  1551. int ib_query_qp(struct ib_qp *qp,
  1552. struct ib_qp_attr *qp_attr,
  1553. int qp_attr_mask,
  1554. struct ib_qp_init_attr *qp_init_attr)
  1555. {
  1556. qp_attr->ah_attr.grh.sgid_attr = NULL;
  1557. qp_attr->alt_ah_attr.grh.sgid_attr = NULL;
  1558. return qp->device->query_qp ?
  1559. qp->device->query_qp(qp->real_qp, qp_attr, qp_attr_mask, qp_init_attr) :
  1560. -EOPNOTSUPP;
  1561. }
  1562. EXPORT_SYMBOL(ib_query_qp);
  1563. int ib_close_qp(struct ib_qp *qp)
  1564. {
  1565. struct ib_qp *real_qp;
  1566. unsigned long flags;
  1567. real_qp = qp->real_qp;
  1568. if (real_qp == qp)
  1569. return -EINVAL;
  1570. spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
  1571. list_del(&qp->open_list);
  1572. spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
  1573. atomic_dec(&real_qp->usecnt);
  1574. if (qp->qp_sec)
  1575. ib_close_shared_qp_security(qp->qp_sec);
  1576. kfree(qp);
  1577. return 0;
  1578. }
  1579. EXPORT_SYMBOL(ib_close_qp);
  1580. static int __ib_destroy_shared_qp(struct ib_qp *qp)
  1581. {
  1582. struct ib_xrcd *xrcd;
  1583. struct ib_qp *real_qp;
  1584. int ret;
  1585. real_qp = qp->real_qp;
  1586. xrcd = real_qp->xrcd;
  1587. mutex_lock(&xrcd->tgt_qp_mutex);
  1588. ib_close_qp(qp);
  1589. if (atomic_read(&real_qp->usecnt) == 0)
  1590. list_del(&real_qp->xrcd_list);
  1591. else
  1592. real_qp = NULL;
  1593. mutex_unlock(&xrcd->tgt_qp_mutex);
  1594. if (real_qp) {
  1595. ret = ib_destroy_qp(real_qp);
  1596. if (!ret)
  1597. atomic_dec(&xrcd->usecnt);
  1598. else
  1599. __ib_insert_xrcd_qp(xrcd, real_qp);
  1600. }
  1601. return 0;
  1602. }
  1603. int ib_destroy_qp(struct ib_qp *qp)
  1604. {
  1605. const struct ib_gid_attr *alt_path_sgid_attr = qp->alt_path_sgid_attr;
  1606. const struct ib_gid_attr *av_sgid_attr = qp->av_sgid_attr;
  1607. struct ib_pd *pd;
  1608. struct ib_cq *scq, *rcq;
  1609. struct ib_srq *srq;
  1610. struct ib_rwq_ind_table *ind_tbl;
  1611. struct ib_qp_security *sec;
  1612. int ret;
  1613. WARN_ON_ONCE(qp->mrs_used > 0);
  1614. if (atomic_read(&qp->usecnt))
  1615. return -EBUSY;
  1616. if (qp->real_qp != qp)
  1617. return __ib_destroy_shared_qp(qp);
  1618. pd = qp->pd;
  1619. scq = qp->send_cq;
  1620. rcq = qp->recv_cq;
  1621. srq = qp->srq;
  1622. ind_tbl = qp->rwq_ind_tbl;
  1623. sec = qp->qp_sec;
  1624. if (sec)
  1625. ib_destroy_qp_security_begin(sec);
  1626. if (!qp->uobject)
  1627. rdma_rw_cleanup_mrs(qp);
  1628. rdma_restrack_del(&qp->res);
  1629. ret = qp->device->destroy_qp(qp);
  1630. if (!ret) {
  1631. if (alt_path_sgid_attr)
  1632. rdma_put_gid_attr(alt_path_sgid_attr);
  1633. if (av_sgid_attr)
  1634. rdma_put_gid_attr(av_sgid_attr);
  1635. if (pd)
  1636. atomic_dec(&pd->usecnt);
  1637. if (scq)
  1638. atomic_dec(&scq->usecnt);
  1639. if (rcq)
  1640. atomic_dec(&rcq->usecnt);
  1641. if (srq)
  1642. atomic_dec(&srq->usecnt);
  1643. if (ind_tbl)
  1644. atomic_dec(&ind_tbl->usecnt);
  1645. if (sec)
  1646. ib_destroy_qp_security_end(sec);
  1647. } else {
  1648. if (sec)
  1649. ib_destroy_qp_security_abort(sec);
  1650. }
  1651. return ret;
  1652. }
  1653. EXPORT_SYMBOL(ib_destroy_qp);
  1654. /* Completion queues */
  1655. struct ib_cq *__ib_create_cq(struct ib_device *device,
  1656. ib_comp_handler comp_handler,
  1657. void (*event_handler)(struct ib_event *, void *),
  1658. void *cq_context,
  1659. const struct ib_cq_init_attr *cq_attr,
  1660. const char *caller)
  1661. {
  1662. struct ib_cq *cq;
  1663. cq = device->create_cq(device, cq_attr, NULL, NULL);
  1664. if (!IS_ERR(cq)) {
  1665. cq->device = device;
  1666. cq->uobject = NULL;
  1667. cq->comp_handler = comp_handler;
  1668. cq->event_handler = event_handler;
  1669. cq->cq_context = cq_context;
  1670. atomic_set(&cq->usecnt, 0);
  1671. cq->res.type = RDMA_RESTRACK_CQ;
  1672. cq->res.kern_name = caller;
  1673. rdma_restrack_add(&cq->res);
  1674. }
  1675. return cq;
  1676. }
  1677. EXPORT_SYMBOL(__ib_create_cq);
  1678. int rdma_set_cq_moderation(struct ib_cq *cq, u16 cq_count, u16 cq_period)
  1679. {
  1680. return cq->device->modify_cq ?
  1681. cq->device->modify_cq(cq, cq_count, cq_period) : -EOPNOTSUPP;
  1682. }
  1683. EXPORT_SYMBOL(rdma_set_cq_moderation);
  1684. int ib_destroy_cq(struct ib_cq *cq)
  1685. {
  1686. if (atomic_read(&cq->usecnt))
  1687. return -EBUSY;
  1688. rdma_restrack_del(&cq->res);
  1689. return cq->device->destroy_cq(cq);
  1690. }
  1691. EXPORT_SYMBOL(ib_destroy_cq);
  1692. int ib_resize_cq(struct ib_cq *cq, int cqe)
  1693. {
  1694. return cq->device->resize_cq ?
  1695. cq->device->resize_cq(cq, cqe, NULL) : -EOPNOTSUPP;
  1696. }
  1697. EXPORT_SYMBOL(ib_resize_cq);
  1698. /* Memory regions */
  1699. int ib_dereg_mr(struct ib_mr *mr)
  1700. {
  1701. struct ib_pd *pd = mr->pd;
  1702. struct ib_dm *dm = mr->dm;
  1703. int ret;
  1704. rdma_restrack_del(&mr->res);
  1705. ret = mr->device->dereg_mr(mr);
  1706. if (!ret) {
  1707. atomic_dec(&pd->usecnt);
  1708. if (dm)
  1709. atomic_dec(&dm->usecnt);
  1710. }
  1711. return ret;
  1712. }
  1713. EXPORT_SYMBOL(ib_dereg_mr);
  1714. /**
  1715. * ib_alloc_mr() - Allocates a memory region
  1716. * @pd: protection domain associated with the region
  1717. * @mr_type: memory region type
  1718. * @max_num_sg: maximum sg entries available for registration.
  1719. *
  1720. * Notes:
  1721. * Memory registeration page/sg lists must not exceed max_num_sg.
  1722. * For mr_type IB_MR_TYPE_MEM_REG, the total length cannot exceed
  1723. * max_num_sg * used_page_size.
  1724. *
  1725. */
  1726. struct ib_mr *ib_alloc_mr(struct ib_pd *pd,
  1727. enum ib_mr_type mr_type,
  1728. u32 max_num_sg)
  1729. {
  1730. struct ib_mr *mr;
  1731. if (!pd->device->alloc_mr)
  1732. return ERR_PTR(-EOPNOTSUPP);
  1733. mr = pd->device->alloc_mr(pd, mr_type, max_num_sg);
  1734. if (!IS_ERR(mr)) {
  1735. mr->device = pd->device;
  1736. mr->pd = pd;
  1737. mr->dm = NULL;
  1738. mr->uobject = NULL;
  1739. atomic_inc(&pd->usecnt);
  1740. mr->need_inval = false;
  1741. mr->res.type = RDMA_RESTRACK_MR;
  1742. rdma_restrack_add(&mr->res);
  1743. }
  1744. return mr;
  1745. }
  1746. EXPORT_SYMBOL(ib_alloc_mr);
  1747. /* "Fast" memory regions */
  1748. struct ib_fmr *ib_alloc_fmr(struct ib_pd *pd,
  1749. int mr_access_flags,
  1750. struct ib_fmr_attr *fmr_attr)
  1751. {
  1752. struct ib_fmr *fmr;
  1753. if (!pd->device->alloc_fmr)
  1754. return ERR_PTR(-EOPNOTSUPP);
  1755. fmr = pd->device->alloc_fmr(pd, mr_access_flags, fmr_attr);
  1756. if (!IS_ERR(fmr)) {
  1757. fmr->device = pd->device;
  1758. fmr->pd = pd;
  1759. atomic_inc(&pd->usecnt);
  1760. }
  1761. return fmr;
  1762. }
  1763. EXPORT_SYMBOL(ib_alloc_fmr);
  1764. int ib_unmap_fmr(struct list_head *fmr_list)
  1765. {
  1766. struct ib_fmr *fmr;
  1767. if (list_empty(fmr_list))
  1768. return 0;
  1769. fmr = list_entry(fmr_list->next, struct ib_fmr, list);
  1770. return fmr->device->unmap_fmr(fmr_list);
  1771. }
  1772. EXPORT_SYMBOL(ib_unmap_fmr);
  1773. int ib_dealloc_fmr(struct ib_fmr *fmr)
  1774. {
  1775. struct ib_pd *pd;
  1776. int ret;
  1777. pd = fmr->pd;
  1778. ret = fmr->device->dealloc_fmr(fmr);
  1779. if (!ret)
  1780. atomic_dec(&pd->usecnt);
  1781. return ret;
  1782. }
  1783. EXPORT_SYMBOL(ib_dealloc_fmr);
  1784. /* Multicast groups */
  1785. static bool is_valid_mcast_lid(struct ib_qp *qp, u16 lid)
  1786. {
  1787. struct ib_qp_init_attr init_attr = {};
  1788. struct ib_qp_attr attr = {};
  1789. int num_eth_ports = 0;
  1790. int port;
  1791. /* If QP state >= init, it is assigned to a port and we can check this
  1792. * port only.
  1793. */
  1794. if (!ib_query_qp(qp, &attr, IB_QP_STATE | IB_QP_PORT, &init_attr)) {
  1795. if (attr.qp_state >= IB_QPS_INIT) {
  1796. if (rdma_port_get_link_layer(qp->device, attr.port_num) !=
  1797. IB_LINK_LAYER_INFINIBAND)
  1798. return true;
  1799. goto lid_check;
  1800. }
  1801. }
  1802. /* Can't get a quick answer, iterate over all ports */
  1803. for (port = 0; port < qp->device->phys_port_cnt; port++)
  1804. if (rdma_port_get_link_layer(qp->device, port) !=
  1805. IB_LINK_LAYER_INFINIBAND)
  1806. num_eth_ports++;
  1807. /* If we have at lease one Ethernet port, RoCE annex declares that
  1808. * multicast LID should be ignored. We can't tell at this step if the
  1809. * QP belongs to an IB or Ethernet port.
  1810. */
  1811. if (num_eth_ports)
  1812. return true;
  1813. /* If all the ports are IB, we can check according to IB spec. */
  1814. lid_check:
  1815. return !(lid < be16_to_cpu(IB_MULTICAST_LID_BASE) ||
  1816. lid == be16_to_cpu(IB_LID_PERMISSIVE));
  1817. }
  1818. int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
  1819. {
  1820. int ret;
  1821. if (!qp->device->attach_mcast)
  1822. return -EOPNOTSUPP;
  1823. if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
  1824. qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
  1825. return -EINVAL;
  1826. ret = qp->device->attach_mcast(qp, gid, lid);
  1827. if (!ret)
  1828. atomic_inc(&qp->usecnt);
  1829. return ret;
  1830. }
  1831. EXPORT_SYMBOL(ib_attach_mcast);
  1832. int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
  1833. {
  1834. int ret;
  1835. if (!qp->device->detach_mcast)
  1836. return -EOPNOTSUPP;
  1837. if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
  1838. qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
  1839. return -EINVAL;
  1840. ret = qp->device->detach_mcast(qp, gid, lid);
  1841. if (!ret)
  1842. atomic_dec(&qp->usecnt);
  1843. return ret;
  1844. }
  1845. EXPORT_SYMBOL(ib_detach_mcast);
  1846. struct ib_xrcd *__ib_alloc_xrcd(struct ib_device *device, const char *caller)
  1847. {
  1848. struct ib_xrcd *xrcd;
  1849. if (!device->alloc_xrcd)
  1850. return ERR_PTR(-EOPNOTSUPP);
  1851. xrcd = device->alloc_xrcd(device, NULL, NULL);
  1852. if (!IS_ERR(xrcd)) {
  1853. xrcd->device = device;
  1854. xrcd->inode = NULL;
  1855. atomic_set(&xrcd->usecnt, 0);
  1856. mutex_init(&xrcd->tgt_qp_mutex);
  1857. INIT_LIST_HEAD(&xrcd->tgt_qp_list);
  1858. }
  1859. return xrcd;
  1860. }
  1861. EXPORT_SYMBOL(__ib_alloc_xrcd);
  1862. int ib_dealloc_xrcd(struct ib_xrcd *xrcd)
  1863. {
  1864. struct ib_qp *qp;
  1865. int ret;
  1866. if (atomic_read(&xrcd->usecnt))
  1867. return -EBUSY;
  1868. while (!list_empty(&xrcd->tgt_qp_list)) {
  1869. qp = list_entry(xrcd->tgt_qp_list.next, struct ib_qp, xrcd_list);
  1870. ret = ib_destroy_qp(qp);
  1871. if (ret)
  1872. return ret;
  1873. }
  1874. return xrcd->device->dealloc_xrcd(xrcd);
  1875. }
  1876. EXPORT_SYMBOL(ib_dealloc_xrcd);
  1877. /**
  1878. * ib_create_wq - Creates a WQ associated with the specified protection
  1879. * domain.
  1880. * @pd: The protection domain associated with the WQ.
  1881. * @wq_attr: A list of initial attributes required to create the
  1882. * WQ. If WQ creation succeeds, then the attributes are updated to
  1883. * the actual capabilities of the created WQ.
  1884. *
  1885. * wq_attr->max_wr and wq_attr->max_sge determine
  1886. * the requested size of the WQ, and set to the actual values allocated
  1887. * on return.
  1888. * If ib_create_wq() succeeds, then max_wr and max_sge will always be
  1889. * at least as large as the requested values.
  1890. */
  1891. struct ib_wq *ib_create_wq(struct ib_pd *pd,
  1892. struct ib_wq_init_attr *wq_attr)
  1893. {
  1894. struct ib_wq *wq;
  1895. if (!pd->device->create_wq)
  1896. return ERR_PTR(-EOPNOTSUPP);
  1897. wq = pd->device->create_wq(pd, wq_attr, NULL);
  1898. if (!IS_ERR(wq)) {
  1899. wq->event_handler = wq_attr->event_handler;
  1900. wq->wq_context = wq_attr->wq_context;
  1901. wq->wq_type = wq_attr->wq_type;
  1902. wq->cq = wq_attr->cq;
  1903. wq->device = pd->device;
  1904. wq->pd = pd;
  1905. wq->uobject = NULL;
  1906. atomic_inc(&pd->usecnt);
  1907. atomic_inc(&wq_attr->cq->usecnt);
  1908. atomic_set(&wq->usecnt, 0);
  1909. }
  1910. return wq;
  1911. }
  1912. EXPORT_SYMBOL(ib_create_wq);
  1913. /**
  1914. * ib_destroy_wq - Destroys the specified WQ.
  1915. * @wq: The WQ to destroy.
  1916. */
  1917. int ib_destroy_wq(struct ib_wq *wq)
  1918. {
  1919. int err;
  1920. struct ib_cq *cq = wq->cq;
  1921. struct ib_pd *pd = wq->pd;
  1922. if (atomic_read(&wq->usecnt))
  1923. return -EBUSY;
  1924. err = wq->device->destroy_wq(wq);
  1925. if (!err) {
  1926. atomic_dec(&pd->usecnt);
  1927. atomic_dec(&cq->usecnt);
  1928. }
  1929. return err;
  1930. }
  1931. EXPORT_SYMBOL(ib_destroy_wq);
  1932. /**
  1933. * ib_modify_wq - Modifies the specified WQ.
  1934. * @wq: The WQ to modify.
  1935. * @wq_attr: On input, specifies the WQ attributes to modify.
  1936. * @wq_attr_mask: A bit-mask used to specify which attributes of the WQ
  1937. * are being modified.
  1938. * On output, the current values of selected WQ attributes are returned.
  1939. */
  1940. int ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
  1941. u32 wq_attr_mask)
  1942. {
  1943. int err;
  1944. if (!wq->device->modify_wq)
  1945. return -EOPNOTSUPP;
  1946. err = wq->device->modify_wq(wq, wq_attr, wq_attr_mask, NULL);
  1947. return err;
  1948. }
  1949. EXPORT_SYMBOL(ib_modify_wq);
  1950. /*
  1951. * ib_create_rwq_ind_table - Creates a RQ Indirection Table.
  1952. * @device: The device on which to create the rwq indirection table.
  1953. * @ib_rwq_ind_table_init_attr: A list of initial attributes required to
  1954. * create the Indirection Table.
  1955. *
  1956. * Note: The life time of ib_rwq_ind_table_init_attr->ind_tbl is not less
  1957. * than the created ib_rwq_ind_table object and the caller is responsible
  1958. * for its memory allocation/free.
  1959. */
  1960. struct ib_rwq_ind_table *ib_create_rwq_ind_table(struct ib_device *device,
  1961. struct ib_rwq_ind_table_init_attr *init_attr)
  1962. {
  1963. struct ib_rwq_ind_table *rwq_ind_table;
  1964. int i;
  1965. u32 table_size;
  1966. if (!device->create_rwq_ind_table)
  1967. return ERR_PTR(-EOPNOTSUPP);
  1968. table_size = (1 << init_attr->log_ind_tbl_size);
  1969. rwq_ind_table = device->create_rwq_ind_table(device,
  1970. init_attr, NULL);
  1971. if (IS_ERR(rwq_ind_table))
  1972. return rwq_ind_table;
  1973. rwq_ind_table->ind_tbl = init_attr->ind_tbl;
  1974. rwq_ind_table->log_ind_tbl_size = init_attr->log_ind_tbl_size;
  1975. rwq_ind_table->device = device;
  1976. rwq_ind_table->uobject = NULL;
  1977. atomic_set(&rwq_ind_table->usecnt, 0);
  1978. for (i = 0; i < table_size; i++)
  1979. atomic_inc(&rwq_ind_table->ind_tbl[i]->usecnt);
  1980. return rwq_ind_table;
  1981. }
  1982. EXPORT_SYMBOL(ib_create_rwq_ind_table);
  1983. /*
  1984. * ib_destroy_rwq_ind_table - Destroys the specified Indirection Table.
  1985. * @wq_ind_table: The Indirection Table to destroy.
  1986. */
  1987. int ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *rwq_ind_table)
  1988. {
  1989. int err, i;
  1990. u32 table_size = (1 << rwq_ind_table->log_ind_tbl_size);
  1991. struct ib_wq **ind_tbl = rwq_ind_table->ind_tbl;
  1992. if (atomic_read(&rwq_ind_table->usecnt))
  1993. return -EBUSY;
  1994. err = rwq_ind_table->device->destroy_rwq_ind_table(rwq_ind_table);
  1995. if (!err) {
  1996. for (i = 0; i < table_size; i++)
  1997. atomic_dec(&ind_tbl[i]->usecnt);
  1998. }
  1999. return err;
  2000. }
  2001. EXPORT_SYMBOL(ib_destroy_rwq_ind_table);
  2002. int ib_check_mr_status(struct ib_mr *mr, u32 check_mask,
  2003. struct ib_mr_status *mr_status)
  2004. {
  2005. return mr->device->check_mr_status ?
  2006. mr->device->check_mr_status(mr, check_mask, mr_status) : -EOPNOTSUPP;
  2007. }
  2008. EXPORT_SYMBOL(ib_check_mr_status);
  2009. int ib_set_vf_link_state(struct ib_device *device, int vf, u8 port,
  2010. int state)
  2011. {
  2012. if (!device->set_vf_link_state)
  2013. return -EOPNOTSUPP;
  2014. return device->set_vf_link_state(device, vf, port, state);
  2015. }
  2016. EXPORT_SYMBOL(ib_set_vf_link_state);
  2017. int ib_get_vf_config(struct ib_device *device, int vf, u8 port,
  2018. struct ifla_vf_info *info)
  2019. {
  2020. if (!device->get_vf_config)
  2021. return -EOPNOTSUPP;
  2022. return device->get_vf_config(device, vf, port, info);
  2023. }
  2024. EXPORT_SYMBOL(ib_get_vf_config);
  2025. int ib_get_vf_stats(struct ib_device *device, int vf, u8 port,
  2026. struct ifla_vf_stats *stats)
  2027. {
  2028. if (!device->get_vf_stats)
  2029. return -EOPNOTSUPP;
  2030. return device->get_vf_stats(device, vf, port, stats);
  2031. }
  2032. EXPORT_SYMBOL(ib_get_vf_stats);
  2033. int ib_set_vf_guid(struct ib_device *device, int vf, u8 port, u64 guid,
  2034. int type)
  2035. {
  2036. if (!device->set_vf_guid)
  2037. return -EOPNOTSUPP;
  2038. return device->set_vf_guid(device, vf, port, guid, type);
  2039. }
  2040. EXPORT_SYMBOL(ib_set_vf_guid);
  2041. /**
  2042. * ib_map_mr_sg() - Map the largest prefix of a dma mapped SG list
  2043. * and set it the memory region.
  2044. * @mr: memory region
  2045. * @sg: dma mapped scatterlist
  2046. * @sg_nents: number of entries in sg
  2047. * @sg_offset: offset in bytes into sg
  2048. * @page_size: page vector desired page size
  2049. *
  2050. * Constraints:
  2051. * - The first sg element is allowed to have an offset.
  2052. * - Each sg element must either be aligned to page_size or virtually
  2053. * contiguous to the previous element. In case an sg element has a
  2054. * non-contiguous offset, the mapping prefix will not include it.
  2055. * - The last sg element is allowed to have length less than page_size.
  2056. * - If sg_nents total byte length exceeds the mr max_num_sge * page_size
  2057. * then only max_num_sg entries will be mapped.
  2058. * - If the MR was allocated with type IB_MR_TYPE_SG_GAPS, none of these
  2059. * constraints holds and the page_size argument is ignored.
  2060. *
  2061. * Returns the number of sg elements that were mapped to the memory region.
  2062. *
  2063. * After this completes successfully, the memory region
  2064. * is ready for registration.
  2065. */
  2066. int ib_map_mr_sg(struct ib_mr *mr, struct scatterlist *sg, int sg_nents,
  2067. unsigned int *sg_offset, unsigned int page_size)
  2068. {
  2069. if (unlikely(!mr->device->map_mr_sg))
  2070. return -EOPNOTSUPP;
  2071. mr->page_size = page_size;
  2072. return mr->device->map_mr_sg(mr, sg, sg_nents, sg_offset);
  2073. }
  2074. EXPORT_SYMBOL(ib_map_mr_sg);
  2075. /**
  2076. * ib_sg_to_pages() - Convert the largest prefix of a sg list
  2077. * to a page vector
  2078. * @mr: memory region
  2079. * @sgl: dma mapped scatterlist
  2080. * @sg_nents: number of entries in sg
  2081. * @sg_offset_p: IN: start offset in bytes into sg
  2082. * OUT: offset in bytes for element n of the sg of the first
  2083. * byte that has not been processed where n is the return
  2084. * value of this function.
  2085. * @set_page: driver page assignment function pointer
  2086. *
  2087. * Core service helper for drivers to convert the largest
  2088. * prefix of given sg list to a page vector. The sg list
  2089. * prefix converted is the prefix that meet the requirements
  2090. * of ib_map_mr_sg.
  2091. *
  2092. * Returns the number of sg elements that were assigned to
  2093. * a page vector.
  2094. */
  2095. int ib_sg_to_pages(struct ib_mr *mr, struct scatterlist *sgl, int sg_nents,
  2096. unsigned int *sg_offset_p, int (*set_page)(struct ib_mr *, u64))
  2097. {
  2098. struct scatterlist *sg;
  2099. u64 last_end_dma_addr = 0;
  2100. unsigned int sg_offset = sg_offset_p ? *sg_offset_p : 0;
  2101. unsigned int last_page_off = 0;
  2102. u64 page_mask = ~((u64)mr->page_size - 1);
  2103. int i, ret;
  2104. if (unlikely(sg_nents <= 0 || sg_offset > sg_dma_len(&sgl[0])))
  2105. return -EINVAL;
  2106. mr->iova = sg_dma_address(&sgl[0]) + sg_offset;
  2107. mr->length = 0;
  2108. for_each_sg(sgl, sg, sg_nents, i) {
  2109. u64 dma_addr = sg_dma_address(sg) + sg_offset;
  2110. u64 prev_addr = dma_addr;
  2111. unsigned int dma_len = sg_dma_len(sg) - sg_offset;
  2112. u64 end_dma_addr = dma_addr + dma_len;
  2113. u64 page_addr = dma_addr & page_mask;
  2114. /*
  2115. * For the second and later elements, check whether either the
  2116. * end of element i-1 or the start of element i is not aligned
  2117. * on a page boundary.
  2118. */
  2119. if (i && (last_page_off != 0 || page_addr != dma_addr)) {
  2120. /* Stop mapping if there is a gap. */
  2121. if (last_end_dma_addr != dma_addr)
  2122. break;
  2123. /*
  2124. * Coalesce this element with the last. If it is small
  2125. * enough just update mr->length. Otherwise start
  2126. * mapping from the next page.
  2127. */
  2128. goto next_page;
  2129. }
  2130. do {
  2131. ret = set_page(mr, page_addr);
  2132. if (unlikely(ret < 0)) {
  2133. sg_offset = prev_addr - sg_dma_address(sg);
  2134. mr->length += prev_addr - dma_addr;
  2135. if (sg_offset_p)
  2136. *sg_offset_p = sg_offset;
  2137. return i || sg_offset ? i : ret;
  2138. }
  2139. prev_addr = page_addr;
  2140. next_page:
  2141. page_addr += mr->page_size;
  2142. } while (page_addr < end_dma_addr);
  2143. mr->length += dma_len;
  2144. last_end_dma_addr = end_dma_addr;
  2145. last_page_off = end_dma_addr & ~page_mask;
  2146. sg_offset = 0;
  2147. }
  2148. if (sg_offset_p)
  2149. *sg_offset_p = 0;
  2150. return i;
  2151. }
  2152. EXPORT_SYMBOL(ib_sg_to_pages);
  2153. struct ib_drain_cqe {
  2154. struct ib_cqe cqe;
  2155. struct completion done;
  2156. };
  2157. static void ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
  2158. {
  2159. struct ib_drain_cqe *cqe = container_of(wc->wr_cqe, struct ib_drain_cqe,
  2160. cqe);
  2161. complete(&cqe->done);
  2162. }
  2163. /*
  2164. * Post a WR and block until its completion is reaped for the SQ.
  2165. */
  2166. static void __ib_drain_sq(struct ib_qp *qp)
  2167. {
  2168. struct ib_cq *cq = qp->send_cq;
  2169. struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
  2170. struct ib_drain_cqe sdrain;
  2171. struct ib_rdma_wr swr = {
  2172. .wr = {
  2173. .next = NULL,
  2174. { .wr_cqe = &sdrain.cqe, },
  2175. .opcode = IB_WR_RDMA_WRITE,
  2176. },
  2177. };
  2178. int ret;
  2179. ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
  2180. if (ret) {
  2181. WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
  2182. return;
  2183. }
  2184. sdrain.cqe.done = ib_drain_qp_done;
  2185. init_completion(&sdrain.done);
  2186. ret = ib_post_send(qp, &swr.wr, NULL);
  2187. if (ret) {
  2188. WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
  2189. return;
  2190. }
  2191. if (cq->poll_ctx == IB_POLL_DIRECT)
  2192. while (wait_for_completion_timeout(&sdrain.done, HZ / 10) <= 0)
  2193. ib_process_cq_direct(cq, -1);
  2194. else
  2195. wait_for_completion(&sdrain.done);
  2196. }
  2197. /*
  2198. * Post a WR and block until its completion is reaped for the RQ.
  2199. */
  2200. static void __ib_drain_rq(struct ib_qp *qp)
  2201. {
  2202. struct ib_cq *cq = qp->recv_cq;
  2203. struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
  2204. struct ib_drain_cqe rdrain;
  2205. struct ib_recv_wr rwr = {};
  2206. int ret;
  2207. ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
  2208. if (ret) {
  2209. WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
  2210. return;
  2211. }
  2212. rwr.wr_cqe = &rdrain.cqe;
  2213. rdrain.cqe.done = ib_drain_qp_done;
  2214. init_completion(&rdrain.done);
  2215. ret = ib_post_recv(qp, &rwr, NULL);
  2216. if (ret) {
  2217. WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
  2218. return;
  2219. }
  2220. if (cq->poll_ctx == IB_POLL_DIRECT)
  2221. while (wait_for_completion_timeout(&rdrain.done, HZ / 10) <= 0)
  2222. ib_process_cq_direct(cq, -1);
  2223. else
  2224. wait_for_completion(&rdrain.done);
  2225. }
  2226. /**
  2227. * ib_drain_sq() - Block until all SQ CQEs have been consumed by the
  2228. * application.
  2229. * @qp: queue pair to drain
  2230. *
  2231. * If the device has a provider-specific drain function, then
  2232. * call that. Otherwise call the generic drain function
  2233. * __ib_drain_sq().
  2234. *
  2235. * The caller must:
  2236. *
  2237. * ensure there is room in the CQ and SQ for the drain work request and
  2238. * completion.
  2239. *
  2240. * allocate the CQ using ib_alloc_cq().
  2241. *
  2242. * ensure that there are no other contexts that are posting WRs concurrently.
  2243. * Otherwise the drain is not guaranteed.
  2244. */
  2245. void ib_drain_sq(struct ib_qp *qp)
  2246. {
  2247. if (qp->device->drain_sq)
  2248. qp->device->drain_sq(qp);
  2249. else
  2250. __ib_drain_sq(qp);
  2251. }
  2252. EXPORT_SYMBOL(ib_drain_sq);
  2253. /**
  2254. * ib_drain_rq() - Block until all RQ CQEs have been consumed by the
  2255. * application.
  2256. * @qp: queue pair to drain
  2257. *
  2258. * If the device has a provider-specific drain function, then
  2259. * call that. Otherwise call the generic drain function
  2260. * __ib_drain_rq().
  2261. *
  2262. * The caller must:
  2263. *
  2264. * ensure there is room in the CQ and RQ for the drain work request and
  2265. * completion.
  2266. *
  2267. * allocate the CQ using ib_alloc_cq().
  2268. *
  2269. * ensure that there are no other contexts that are posting WRs concurrently.
  2270. * Otherwise the drain is not guaranteed.
  2271. */
  2272. void ib_drain_rq(struct ib_qp *qp)
  2273. {
  2274. if (qp->device->drain_rq)
  2275. qp->device->drain_rq(qp);
  2276. else
  2277. __ib_drain_rq(qp);
  2278. }
  2279. EXPORT_SYMBOL(ib_drain_rq);
  2280. /**
  2281. * ib_drain_qp() - Block until all CQEs have been consumed by the
  2282. * application on both the RQ and SQ.
  2283. * @qp: queue pair to drain
  2284. *
  2285. * The caller must:
  2286. *
  2287. * ensure there is room in the CQ(s), SQ, and RQ for drain work requests
  2288. * and completions.
  2289. *
  2290. * allocate the CQs using ib_alloc_cq().
  2291. *
  2292. * ensure that there are no other contexts that are posting WRs concurrently.
  2293. * Otherwise the drain is not guaranteed.
  2294. */
  2295. void ib_drain_qp(struct ib_qp *qp)
  2296. {
  2297. ib_drain_sq(qp);
  2298. if (!qp->srq)
  2299. ib_drain_rq(qp);
  2300. }
  2301. EXPORT_SYMBOL(ib_drain_qp);