verbs.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546
  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. * Validate that the rdma_ah_attr is valid for the device before passing it
  292. * off to the driver.
  293. */
  294. static int rdma_check_ah_attr(struct ib_device *device,
  295. struct rdma_ah_attr *ah_attr)
  296. {
  297. if (!rdma_is_port_valid(device, ah_attr->port_num))
  298. return -EINVAL;
  299. if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE &&
  300. !(ah_attr->ah_flags & IB_AH_GRH))
  301. return -EINVAL;
  302. if (ah_attr->grh.sgid_attr) {
  303. /*
  304. * Make sure the passed sgid_attr is consistent with the
  305. * parameters
  306. */
  307. if (ah_attr->grh.sgid_attr->index != ah_attr->grh.sgid_index ||
  308. ah_attr->grh.sgid_attr->port_num != ah_attr->port_num)
  309. return -EINVAL;
  310. }
  311. return 0;
  312. }
  313. /*
  314. * If the ah requires a GRH then ensure that sgid_attr pointer is filled in.
  315. * On success the caller is responsible to call rdma_unfill_sgid_attr().
  316. */
  317. static int rdma_fill_sgid_attr(struct ib_device *device,
  318. struct rdma_ah_attr *ah_attr,
  319. const struct ib_gid_attr **old_sgid_attr)
  320. {
  321. const struct ib_gid_attr *sgid_attr;
  322. struct ib_global_route *grh;
  323. int ret;
  324. *old_sgid_attr = ah_attr->grh.sgid_attr;
  325. ret = rdma_check_ah_attr(device, ah_attr);
  326. if (ret)
  327. return ret;
  328. if (!(ah_attr->ah_flags & IB_AH_GRH))
  329. return 0;
  330. grh = rdma_ah_retrieve_grh(ah_attr);
  331. if (grh->sgid_attr)
  332. return 0;
  333. sgid_attr =
  334. rdma_get_gid_attr(device, ah_attr->port_num, grh->sgid_index);
  335. if (IS_ERR(sgid_attr))
  336. return PTR_ERR(sgid_attr);
  337. /* Move ownerhip of the kref into the ah_attr */
  338. grh->sgid_attr = sgid_attr;
  339. return 0;
  340. }
  341. static void rdma_unfill_sgid_attr(struct rdma_ah_attr *ah_attr,
  342. const struct ib_gid_attr *old_sgid_attr)
  343. {
  344. /*
  345. * Fill didn't change anything, the caller retains ownership of
  346. * whatever it passed
  347. */
  348. if (ah_attr->grh.sgid_attr == old_sgid_attr)
  349. return;
  350. /*
  351. * Otherwise, we need to undo what rdma_fill_sgid_attr so the caller
  352. * doesn't see any change in the rdma_ah_attr. If we get here
  353. * old_sgid_attr is NULL.
  354. */
  355. rdma_destroy_ah_attr(ah_attr);
  356. }
  357. static struct ib_ah *_rdma_create_ah(struct ib_pd *pd,
  358. struct rdma_ah_attr *ah_attr,
  359. struct ib_udata *udata)
  360. {
  361. struct ib_ah *ah;
  362. ah = pd->device->create_ah(pd, ah_attr, udata);
  363. if (!IS_ERR(ah)) {
  364. ah->device = pd->device;
  365. ah->pd = pd;
  366. ah->uobject = NULL;
  367. ah->type = ah_attr->type;
  368. atomic_inc(&pd->usecnt);
  369. }
  370. return ah;
  371. }
  372. /**
  373. * rdma_create_ah - Creates an address handle for the
  374. * given address vector.
  375. * @pd: The protection domain associated with the address handle.
  376. * @ah_attr: The attributes of the address vector.
  377. *
  378. * It returns 0 on success and returns appropriate error code on error.
  379. * The address handle is used to reference a local or global destination
  380. * in all UD QP post sends.
  381. */
  382. struct ib_ah *rdma_create_ah(struct ib_pd *pd, struct rdma_ah_attr *ah_attr)
  383. {
  384. const struct ib_gid_attr *old_sgid_attr;
  385. struct ib_ah *ah;
  386. int ret;
  387. ret = rdma_fill_sgid_attr(pd->device, ah_attr, &old_sgid_attr);
  388. if (ret)
  389. return ERR_PTR(ret);
  390. ah = _rdma_create_ah(pd, ah_attr, NULL);
  391. rdma_unfill_sgid_attr(ah_attr, old_sgid_attr);
  392. return ah;
  393. }
  394. EXPORT_SYMBOL(rdma_create_ah);
  395. /**
  396. * rdma_create_user_ah - Creates an address handle for the
  397. * given address vector.
  398. * It resolves destination mac address for ah attribute of RoCE type.
  399. * @pd: The protection domain associated with the address handle.
  400. * @ah_attr: The attributes of the address vector.
  401. * @udata: pointer to user's input output buffer information need by
  402. * provider driver.
  403. *
  404. * It returns 0 on success and returns appropriate error code on error.
  405. * The address handle is used to reference a local or global destination
  406. * in all UD QP post sends.
  407. */
  408. struct ib_ah *rdma_create_user_ah(struct ib_pd *pd,
  409. struct rdma_ah_attr *ah_attr,
  410. struct ib_udata *udata)
  411. {
  412. const struct ib_gid_attr *old_sgid_attr;
  413. struct ib_ah *ah;
  414. int err;
  415. err = rdma_fill_sgid_attr(pd->device, ah_attr, &old_sgid_attr);
  416. if (err)
  417. return ERR_PTR(err);
  418. if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) {
  419. err = ib_resolve_eth_dmac(pd->device, ah_attr);
  420. if (err) {
  421. ah = ERR_PTR(err);
  422. goto out;
  423. }
  424. }
  425. ah = _rdma_create_ah(pd, ah_attr, udata);
  426. out:
  427. rdma_unfill_sgid_attr(ah_attr, old_sgid_attr);
  428. return ah;
  429. }
  430. EXPORT_SYMBOL(rdma_create_user_ah);
  431. int ib_get_rdma_header_version(const union rdma_network_hdr *hdr)
  432. {
  433. const struct iphdr *ip4h = (struct iphdr *)&hdr->roce4grh;
  434. struct iphdr ip4h_checked;
  435. const struct ipv6hdr *ip6h = (struct ipv6hdr *)&hdr->ibgrh;
  436. /* If it's IPv6, the version must be 6, otherwise, the first
  437. * 20 bytes (before the IPv4 header) are garbled.
  438. */
  439. if (ip6h->version != 6)
  440. return (ip4h->version == 4) ? 4 : 0;
  441. /* version may be 6 or 4 because the first 20 bytes could be garbled */
  442. /* RoCE v2 requires no options, thus header length
  443. * must be 5 words
  444. */
  445. if (ip4h->ihl != 5)
  446. return 6;
  447. /* Verify checksum.
  448. * We can't write on scattered buffers so we need to copy to
  449. * temp buffer.
  450. */
  451. memcpy(&ip4h_checked, ip4h, sizeof(ip4h_checked));
  452. ip4h_checked.check = 0;
  453. ip4h_checked.check = ip_fast_csum((u8 *)&ip4h_checked, 5);
  454. /* if IPv4 header checksum is OK, believe it */
  455. if (ip4h->check == ip4h_checked.check)
  456. return 4;
  457. return 6;
  458. }
  459. EXPORT_SYMBOL(ib_get_rdma_header_version);
  460. static enum rdma_network_type ib_get_net_type_by_grh(struct ib_device *device,
  461. u8 port_num,
  462. const struct ib_grh *grh)
  463. {
  464. int grh_version;
  465. if (rdma_protocol_ib(device, port_num))
  466. return RDMA_NETWORK_IB;
  467. grh_version = ib_get_rdma_header_version((union rdma_network_hdr *)grh);
  468. if (grh_version == 4)
  469. return RDMA_NETWORK_IPV4;
  470. if (grh->next_hdr == IPPROTO_UDP)
  471. return RDMA_NETWORK_IPV6;
  472. return RDMA_NETWORK_ROCE_V1;
  473. }
  474. struct find_gid_index_context {
  475. u16 vlan_id;
  476. enum ib_gid_type gid_type;
  477. };
  478. static bool find_gid_index(const union ib_gid *gid,
  479. const struct ib_gid_attr *gid_attr,
  480. void *context)
  481. {
  482. struct find_gid_index_context *ctx = context;
  483. if (ctx->gid_type != gid_attr->gid_type)
  484. return false;
  485. if ((!!(ctx->vlan_id != 0xffff) == !is_vlan_dev(gid_attr->ndev)) ||
  486. (is_vlan_dev(gid_attr->ndev) &&
  487. vlan_dev_vlan_id(gid_attr->ndev) != ctx->vlan_id))
  488. return false;
  489. return true;
  490. }
  491. static int get_sgid_index_from_eth(struct ib_device *device, u8 port_num,
  492. u16 vlan_id, const union ib_gid *sgid,
  493. enum ib_gid_type gid_type,
  494. u16 *gid_index)
  495. {
  496. struct find_gid_index_context context = {.vlan_id = vlan_id,
  497. .gid_type = gid_type};
  498. return ib_find_gid_by_filter(device, sgid, port_num, find_gid_index,
  499. &context, gid_index);
  500. }
  501. int ib_get_gids_from_rdma_hdr(const union rdma_network_hdr *hdr,
  502. enum rdma_network_type net_type,
  503. union ib_gid *sgid, union ib_gid *dgid)
  504. {
  505. struct sockaddr_in src_in;
  506. struct sockaddr_in dst_in;
  507. __be32 src_saddr, dst_saddr;
  508. if (!sgid || !dgid)
  509. return -EINVAL;
  510. if (net_type == RDMA_NETWORK_IPV4) {
  511. memcpy(&src_in.sin_addr.s_addr,
  512. &hdr->roce4grh.saddr, 4);
  513. memcpy(&dst_in.sin_addr.s_addr,
  514. &hdr->roce4grh.daddr, 4);
  515. src_saddr = src_in.sin_addr.s_addr;
  516. dst_saddr = dst_in.sin_addr.s_addr;
  517. ipv6_addr_set_v4mapped(src_saddr,
  518. (struct in6_addr *)sgid);
  519. ipv6_addr_set_v4mapped(dst_saddr,
  520. (struct in6_addr *)dgid);
  521. return 0;
  522. } else if (net_type == RDMA_NETWORK_IPV6 ||
  523. net_type == RDMA_NETWORK_IB) {
  524. *dgid = hdr->ibgrh.dgid;
  525. *sgid = hdr->ibgrh.sgid;
  526. return 0;
  527. } else {
  528. return -EINVAL;
  529. }
  530. }
  531. EXPORT_SYMBOL(ib_get_gids_from_rdma_hdr);
  532. /* Resolve destination mac address and hop limit for unicast destination
  533. * GID entry, considering the source GID entry as well.
  534. * ah_attribute must have have valid port_num, sgid_index.
  535. */
  536. static int ib_resolve_unicast_gid_dmac(struct ib_device *device,
  537. struct rdma_ah_attr *ah_attr)
  538. {
  539. struct ib_gid_attr sgid_attr;
  540. struct ib_global_route *grh;
  541. int hop_limit = 0xff;
  542. union ib_gid sgid;
  543. int ret;
  544. grh = rdma_ah_retrieve_grh(ah_attr);
  545. ret = ib_get_cached_gid(device, rdma_ah_get_port_num(ah_attr),
  546. grh->sgid_index, &sgid, &sgid_attr);
  547. if (ret || !sgid_attr.ndev) {
  548. if (!ret)
  549. ret = -ENXIO;
  550. return ret;
  551. }
  552. /* If destination is link local and source GID is RoCEv1,
  553. * IP stack is not used.
  554. */
  555. if (rdma_link_local_addr((struct in6_addr *)grh->dgid.raw) &&
  556. sgid_attr.gid_type == IB_GID_TYPE_ROCE) {
  557. rdma_get_ll_mac((struct in6_addr *)grh->dgid.raw,
  558. ah_attr->roce.dmac);
  559. goto done;
  560. }
  561. ret = rdma_addr_find_l2_eth_by_grh(&sgid, &grh->dgid,
  562. ah_attr->roce.dmac,
  563. sgid_attr.ndev, &hop_limit);
  564. done:
  565. dev_put(sgid_attr.ndev);
  566. grh->hop_limit = hop_limit;
  567. return ret;
  568. }
  569. /*
  570. * This function initializes address handle attributes from the incoming packet.
  571. * Incoming packet has dgid of the receiver node on which this code is
  572. * getting executed and, sgid contains the GID of the sender.
  573. *
  574. * When resolving mac address of destination, the arrived dgid is used
  575. * as sgid and, sgid is used as dgid because sgid contains destinations
  576. * GID whom to respond to.
  577. *
  578. */
  579. int ib_init_ah_attr_from_wc(struct ib_device *device, u8 port_num,
  580. const struct ib_wc *wc, const struct ib_grh *grh,
  581. struct rdma_ah_attr *ah_attr)
  582. {
  583. u32 flow_class;
  584. u16 gid_index;
  585. int ret;
  586. enum rdma_network_type net_type = RDMA_NETWORK_IB;
  587. enum ib_gid_type gid_type = IB_GID_TYPE_IB;
  588. int hoplimit = 0xff;
  589. union ib_gid dgid;
  590. union ib_gid sgid;
  591. might_sleep();
  592. memset(ah_attr, 0, sizeof *ah_attr);
  593. ah_attr->type = rdma_ah_find_type(device, port_num);
  594. if (rdma_cap_eth_ah(device, port_num)) {
  595. if (wc->wc_flags & IB_WC_WITH_NETWORK_HDR_TYPE)
  596. net_type = wc->network_hdr_type;
  597. else
  598. net_type = ib_get_net_type_by_grh(device, port_num, grh);
  599. gid_type = ib_network_to_gid_type(net_type);
  600. }
  601. ret = ib_get_gids_from_rdma_hdr((union rdma_network_hdr *)grh, net_type,
  602. &sgid, &dgid);
  603. if (ret)
  604. return ret;
  605. rdma_ah_set_sl(ah_attr, wc->sl);
  606. rdma_ah_set_port_num(ah_attr, port_num);
  607. if (rdma_protocol_roce(device, port_num)) {
  608. u16 vlan_id = wc->wc_flags & IB_WC_WITH_VLAN ?
  609. wc->vlan_id : 0xffff;
  610. if (!(wc->wc_flags & IB_WC_GRH))
  611. return -EPROTOTYPE;
  612. ret = get_sgid_index_from_eth(device, port_num,
  613. vlan_id, &dgid,
  614. gid_type, &gid_index);
  615. if (ret)
  616. return ret;
  617. flow_class = be32_to_cpu(grh->version_tclass_flow);
  618. rdma_ah_set_grh(ah_attr, &sgid,
  619. flow_class & 0xFFFFF,
  620. (u8)gid_index, hoplimit,
  621. (flow_class >> 20) & 0xFF);
  622. return ib_resolve_unicast_gid_dmac(device, ah_attr);
  623. } else {
  624. rdma_ah_set_dlid(ah_attr, wc->slid);
  625. rdma_ah_set_path_bits(ah_attr, wc->dlid_path_bits);
  626. if (wc->wc_flags & IB_WC_GRH) {
  627. if (dgid.global.interface_id != cpu_to_be64(IB_SA_WELL_KNOWN_GUID)) {
  628. ret = ib_find_cached_gid_by_port(device, &dgid,
  629. IB_GID_TYPE_IB,
  630. port_num, NULL,
  631. &gid_index);
  632. if (ret)
  633. return ret;
  634. } else {
  635. gid_index = 0;
  636. }
  637. flow_class = be32_to_cpu(grh->version_tclass_flow);
  638. rdma_ah_set_grh(ah_attr, &sgid,
  639. flow_class & 0xFFFFF,
  640. (u8)gid_index, hoplimit,
  641. (flow_class >> 20) & 0xFF);
  642. }
  643. return 0;
  644. }
  645. }
  646. EXPORT_SYMBOL(ib_init_ah_attr_from_wc);
  647. /**
  648. * rdma_move_grh_sgid_attr - Sets the sgid attribute of GRH, taking ownership
  649. * of the reference
  650. *
  651. * @attr: Pointer to AH attribute structure
  652. * @dgid: Destination GID
  653. * @flow_label: Flow label
  654. * @hop_limit: Hop limit
  655. * @traffic_class: traffic class
  656. * @sgid_attr: Pointer to SGID attribute
  657. *
  658. * This takes ownership of the sgid_attr reference. The caller must ensure
  659. * rdma_destroy_ah_attr() is called before destroying the rdma_ah_attr after
  660. * calling this function.
  661. */
  662. void rdma_move_grh_sgid_attr(struct rdma_ah_attr *attr, union ib_gid *dgid,
  663. u32 flow_label, u8 hop_limit, u8 traffic_class,
  664. const struct ib_gid_attr *sgid_attr)
  665. {
  666. rdma_ah_set_grh(attr, dgid, flow_label, sgid_attr->index, hop_limit,
  667. traffic_class);
  668. attr->grh.sgid_attr = sgid_attr;
  669. }
  670. EXPORT_SYMBOL(rdma_move_grh_sgid_attr);
  671. /**
  672. * rdma_destroy_ah_attr - Release reference to SGID attribute of
  673. * ah attribute.
  674. * @ah_attr: Pointer to ah attribute
  675. *
  676. * Release reference to the SGID attribute of the ah attribute if it is
  677. * non NULL. It is safe to call this multiple times, and safe to call it on
  678. * a zero initialized ah_attr.
  679. */
  680. void rdma_destroy_ah_attr(struct rdma_ah_attr *ah_attr)
  681. {
  682. if (ah_attr->grh.sgid_attr) {
  683. rdma_put_gid_attr(ah_attr->grh.sgid_attr);
  684. ah_attr->grh.sgid_attr = NULL;
  685. }
  686. }
  687. EXPORT_SYMBOL(rdma_destroy_ah_attr);
  688. struct ib_ah *ib_create_ah_from_wc(struct ib_pd *pd, const struct ib_wc *wc,
  689. const struct ib_grh *grh, u8 port_num)
  690. {
  691. struct rdma_ah_attr ah_attr;
  692. int ret;
  693. ret = ib_init_ah_attr_from_wc(pd->device, port_num, wc, grh, &ah_attr);
  694. if (ret)
  695. return ERR_PTR(ret);
  696. return rdma_create_ah(pd, &ah_attr);
  697. }
  698. EXPORT_SYMBOL(ib_create_ah_from_wc);
  699. int rdma_modify_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr)
  700. {
  701. const struct ib_gid_attr *old_sgid_attr;
  702. int ret;
  703. if (ah->type != ah_attr->type)
  704. return -EINVAL;
  705. ret = rdma_fill_sgid_attr(ah->device, ah_attr, &old_sgid_attr);
  706. if (ret)
  707. return ret;
  708. ret = ah->device->modify_ah ?
  709. ah->device->modify_ah(ah, ah_attr) :
  710. -EOPNOTSUPP;
  711. rdma_unfill_sgid_attr(ah_attr, old_sgid_attr);
  712. return ret;
  713. }
  714. EXPORT_SYMBOL(rdma_modify_ah);
  715. int rdma_query_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr)
  716. {
  717. ah_attr->grh.sgid_attr = NULL;
  718. return ah->device->query_ah ?
  719. ah->device->query_ah(ah, ah_attr) :
  720. -EOPNOTSUPP;
  721. }
  722. EXPORT_SYMBOL(rdma_query_ah);
  723. int rdma_destroy_ah(struct ib_ah *ah)
  724. {
  725. struct ib_pd *pd;
  726. int ret;
  727. pd = ah->pd;
  728. ret = ah->device->destroy_ah(ah);
  729. if (!ret)
  730. atomic_dec(&pd->usecnt);
  731. return ret;
  732. }
  733. EXPORT_SYMBOL(rdma_destroy_ah);
  734. /* Shared receive queues */
  735. struct ib_srq *ib_create_srq(struct ib_pd *pd,
  736. struct ib_srq_init_attr *srq_init_attr)
  737. {
  738. struct ib_srq *srq;
  739. if (!pd->device->create_srq)
  740. return ERR_PTR(-EOPNOTSUPP);
  741. srq = pd->device->create_srq(pd, srq_init_attr, NULL);
  742. if (!IS_ERR(srq)) {
  743. srq->device = pd->device;
  744. srq->pd = pd;
  745. srq->uobject = NULL;
  746. srq->event_handler = srq_init_attr->event_handler;
  747. srq->srq_context = srq_init_attr->srq_context;
  748. srq->srq_type = srq_init_attr->srq_type;
  749. if (ib_srq_has_cq(srq->srq_type)) {
  750. srq->ext.cq = srq_init_attr->ext.cq;
  751. atomic_inc(&srq->ext.cq->usecnt);
  752. }
  753. if (srq->srq_type == IB_SRQT_XRC) {
  754. srq->ext.xrc.xrcd = srq_init_attr->ext.xrc.xrcd;
  755. atomic_inc(&srq->ext.xrc.xrcd->usecnt);
  756. }
  757. atomic_inc(&pd->usecnt);
  758. atomic_set(&srq->usecnt, 0);
  759. }
  760. return srq;
  761. }
  762. EXPORT_SYMBOL(ib_create_srq);
  763. int ib_modify_srq(struct ib_srq *srq,
  764. struct ib_srq_attr *srq_attr,
  765. enum ib_srq_attr_mask srq_attr_mask)
  766. {
  767. return srq->device->modify_srq ?
  768. srq->device->modify_srq(srq, srq_attr, srq_attr_mask, NULL) :
  769. -EOPNOTSUPP;
  770. }
  771. EXPORT_SYMBOL(ib_modify_srq);
  772. int ib_query_srq(struct ib_srq *srq,
  773. struct ib_srq_attr *srq_attr)
  774. {
  775. return srq->device->query_srq ?
  776. srq->device->query_srq(srq, srq_attr) : -EOPNOTSUPP;
  777. }
  778. EXPORT_SYMBOL(ib_query_srq);
  779. int ib_destroy_srq(struct ib_srq *srq)
  780. {
  781. struct ib_pd *pd;
  782. enum ib_srq_type srq_type;
  783. struct ib_xrcd *uninitialized_var(xrcd);
  784. struct ib_cq *uninitialized_var(cq);
  785. int ret;
  786. if (atomic_read(&srq->usecnt))
  787. return -EBUSY;
  788. pd = srq->pd;
  789. srq_type = srq->srq_type;
  790. if (ib_srq_has_cq(srq_type))
  791. cq = srq->ext.cq;
  792. if (srq_type == IB_SRQT_XRC)
  793. xrcd = srq->ext.xrc.xrcd;
  794. ret = srq->device->destroy_srq(srq);
  795. if (!ret) {
  796. atomic_dec(&pd->usecnt);
  797. if (srq_type == IB_SRQT_XRC)
  798. atomic_dec(&xrcd->usecnt);
  799. if (ib_srq_has_cq(srq_type))
  800. atomic_dec(&cq->usecnt);
  801. }
  802. return ret;
  803. }
  804. EXPORT_SYMBOL(ib_destroy_srq);
  805. /* Queue pairs */
  806. static void __ib_shared_qp_event_handler(struct ib_event *event, void *context)
  807. {
  808. struct ib_qp *qp = context;
  809. unsigned long flags;
  810. spin_lock_irqsave(&qp->device->event_handler_lock, flags);
  811. list_for_each_entry(event->element.qp, &qp->open_list, open_list)
  812. if (event->element.qp->event_handler)
  813. event->element.qp->event_handler(event, event->element.qp->qp_context);
  814. spin_unlock_irqrestore(&qp->device->event_handler_lock, flags);
  815. }
  816. static void __ib_insert_xrcd_qp(struct ib_xrcd *xrcd, struct ib_qp *qp)
  817. {
  818. mutex_lock(&xrcd->tgt_qp_mutex);
  819. list_add(&qp->xrcd_list, &xrcd->tgt_qp_list);
  820. mutex_unlock(&xrcd->tgt_qp_mutex);
  821. }
  822. static struct ib_qp *__ib_open_qp(struct ib_qp *real_qp,
  823. void (*event_handler)(struct ib_event *, void *),
  824. void *qp_context)
  825. {
  826. struct ib_qp *qp;
  827. unsigned long flags;
  828. int err;
  829. qp = kzalloc(sizeof *qp, GFP_KERNEL);
  830. if (!qp)
  831. return ERR_PTR(-ENOMEM);
  832. qp->real_qp = real_qp;
  833. err = ib_open_shared_qp_security(qp, real_qp->device);
  834. if (err) {
  835. kfree(qp);
  836. return ERR_PTR(err);
  837. }
  838. qp->real_qp = real_qp;
  839. atomic_inc(&real_qp->usecnt);
  840. qp->device = real_qp->device;
  841. qp->event_handler = event_handler;
  842. qp->qp_context = qp_context;
  843. qp->qp_num = real_qp->qp_num;
  844. qp->qp_type = real_qp->qp_type;
  845. spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
  846. list_add(&qp->open_list, &real_qp->open_list);
  847. spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
  848. return qp;
  849. }
  850. struct ib_qp *ib_open_qp(struct ib_xrcd *xrcd,
  851. struct ib_qp_open_attr *qp_open_attr)
  852. {
  853. struct ib_qp *qp, *real_qp;
  854. if (qp_open_attr->qp_type != IB_QPT_XRC_TGT)
  855. return ERR_PTR(-EINVAL);
  856. qp = ERR_PTR(-EINVAL);
  857. mutex_lock(&xrcd->tgt_qp_mutex);
  858. list_for_each_entry(real_qp, &xrcd->tgt_qp_list, xrcd_list) {
  859. if (real_qp->qp_num == qp_open_attr->qp_num) {
  860. qp = __ib_open_qp(real_qp, qp_open_attr->event_handler,
  861. qp_open_attr->qp_context);
  862. break;
  863. }
  864. }
  865. mutex_unlock(&xrcd->tgt_qp_mutex);
  866. return qp;
  867. }
  868. EXPORT_SYMBOL(ib_open_qp);
  869. static struct ib_qp *ib_create_xrc_qp(struct ib_qp *qp,
  870. struct ib_qp_init_attr *qp_init_attr)
  871. {
  872. struct ib_qp *real_qp = qp;
  873. qp->event_handler = __ib_shared_qp_event_handler;
  874. qp->qp_context = qp;
  875. qp->pd = NULL;
  876. qp->send_cq = qp->recv_cq = NULL;
  877. qp->srq = NULL;
  878. qp->xrcd = qp_init_attr->xrcd;
  879. atomic_inc(&qp_init_attr->xrcd->usecnt);
  880. INIT_LIST_HEAD(&qp->open_list);
  881. qp = __ib_open_qp(real_qp, qp_init_attr->event_handler,
  882. qp_init_attr->qp_context);
  883. if (!IS_ERR(qp))
  884. __ib_insert_xrcd_qp(qp_init_attr->xrcd, real_qp);
  885. else
  886. real_qp->device->destroy_qp(real_qp);
  887. return qp;
  888. }
  889. struct ib_qp *ib_create_qp(struct ib_pd *pd,
  890. struct ib_qp_init_attr *qp_init_attr)
  891. {
  892. struct ib_device *device = pd ? pd->device : qp_init_attr->xrcd->device;
  893. struct ib_qp *qp;
  894. int ret;
  895. if (qp_init_attr->rwq_ind_tbl &&
  896. (qp_init_attr->recv_cq ||
  897. qp_init_attr->srq || qp_init_attr->cap.max_recv_wr ||
  898. qp_init_attr->cap.max_recv_sge))
  899. return ERR_PTR(-EINVAL);
  900. /*
  901. * If the callers is using the RDMA API calculate the resources
  902. * needed for the RDMA READ/WRITE operations.
  903. *
  904. * Note that these callers need to pass in a port number.
  905. */
  906. if (qp_init_attr->cap.max_rdma_ctxs)
  907. rdma_rw_init_qp(device, qp_init_attr);
  908. qp = _ib_create_qp(device, pd, qp_init_attr, NULL, NULL);
  909. if (IS_ERR(qp))
  910. return qp;
  911. ret = ib_create_qp_security(qp, device);
  912. if (ret) {
  913. ib_destroy_qp(qp);
  914. return ERR_PTR(ret);
  915. }
  916. qp->real_qp = qp;
  917. qp->qp_type = qp_init_attr->qp_type;
  918. qp->rwq_ind_tbl = qp_init_attr->rwq_ind_tbl;
  919. atomic_set(&qp->usecnt, 0);
  920. qp->mrs_used = 0;
  921. spin_lock_init(&qp->mr_lock);
  922. INIT_LIST_HEAD(&qp->rdma_mrs);
  923. INIT_LIST_HEAD(&qp->sig_mrs);
  924. qp->port = 0;
  925. if (qp_init_attr->qp_type == IB_QPT_XRC_TGT)
  926. return ib_create_xrc_qp(qp, qp_init_attr);
  927. qp->event_handler = qp_init_attr->event_handler;
  928. qp->qp_context = qp_init_attr->qp_context;
  929. if (qp_init_attr->qp_type == IB_QPT_XRC_INI) {
  930. qp->recv_cq = NULL;
  931. qp->srq = NULL;
  932. } else {
  933. qp->recv_cq = qp_init_attr->recv_cq;
  934. if (qp_init_attr->recv_cq)
  935. atomic_inc(&qp_init_attr->recv_cq->usecnt);
  936. qp->srq = qp_init_attr->srq;
  937. if (qp->srq)
  938. atomic_inc(&qp_init_attr->srq->usecnt);
  939. }
  940. qp->send_cq = qp_init_attr->send_cq;
  941. qp->xrcd = NULL;
  942. atomic_inc(&pd->usecnt);
  943. if (qp_init_attr->send_cq)
  944. atomic_inc(&qp_init_attr->send_cq->usecnt);
  945. if (qp_init_attr->rwq_ind_tbl)
  946. atomic_inc(&qp->rwq_ind_tbl->usecnt);
  947. if (qp_init_attr->cap.max_rdma_ctxs) {
  948. ret = rdma_rw_init_mrs(qp, qp_init_attr);
  949. if (ret) {
  950. pr_err("failed to init MR pool ret= %d\n", ret);
  951. ib_destroy_qp(qp);
  952. return ERR_PTR(ret);
  953. }
  954. }
  955. /*
  956. * Note: all hw drivers guarantee that max_send_sge is lower than
  957. * the device RDMA WRITE SGE limit but not all hw drivers ensure that
  958. * max_send_sge <= max_sge_rd.
  959. */
  960. qp->max_write_sge = qp_init_attr->cap.max_send_sge;
  961. qp->max_read_sge = min_t(u32, qp_init_attr->cap.max_send_sge,
  962. device->attrs.max_sge_rd);
  963. return qp;
  964. }
  965. EXPORT_SYMBOL(ib_create_qp);
  966. static const struct {
  967. int valid;
  968. enum ib_qp_attr_mask req_param[IB_QPT_MAX];
  969. enum ib_qp_attr_mask opt_param[IB_QPT_MAX];
  970. } qp_state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = {
  971. [IB_QPS_RESET] = {
  972. [IB_QPS_RESET] = { .valid = 1 },
  973. [IB_QPS_INIT] = {
  974. .valid = 1,
  975. .req_param = {
  976. [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
  977. IB_QP_PORT |
  978. IB_QP_QKEY),
  979. [IB_QPT_RAW_PACKET] = IB_QP_PORT,
  980. [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
  981. IB_QP_PORT |
  982. IB_QP_ACCESS_FLAGS),
  983. [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
  984. IB_QP_PORT |
  985. IB_QP_ACCESS_FLAGS),
  986. [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX |
  987. IB_QP_PORT |
  988. IB_QP_ACCESS_FLAGS),
  989. [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX |
  990. IB_QP_PORT |
  991. IB_QP_ACCESS_FLAGS),
  992. [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
  993. IB_QP_QKEY),
  994. [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
  995. IB_QP_QKEY),
  996. }
  997. },
  998. },
  999. [IB_QPS_INIT] = {
  1000. [IB_QPS_RESET] = { .valid = 1 },
  1001. [IB_QPS_ERR] = { .valid = 1 },
  1002. [IB_QPS_INIT] = {
  1003. .valid = 1,
  1004. .opt_param = {
  1005. [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
  1006. IB_QP_PORT |
  1007. IB_QP_QKEY),
  1008. [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
  1009. IB_QP_PORT |
  1010. IB_QP_ACCESS_FLAGS),
  1011. [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
  1012. IB_QP_PORT |
  1013. IB_QP_ACCESS_FLAGS),
  1014. [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX |
  1015. IB_QP_PORT |
  1016. IB_QP_ACCESS_FLAGS),
  1017. [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX |
  1018. IB_QP_PORT |
  1019. IB_QP_ACCESS_FLAGS),
  1020. [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
  1021. IB_QP_QKEY),
  1022. [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
  1023. IB_QP_QKEY),
  1024. }
  1025. },
  1026. [IB_QPS_RTR] = {
  1027. .valid = 1,
  1028. .req_param = {
  1029. [IB_QPT_UC] = (IB_QP_AV |
  1030. IB_QP_PATH_MTU |
  1031. IB_QP_DEST_QPN |
  1032. IB_QP_RQ_PSN),
  1033. [IB_QPT_RC] = (IB_QP_AV |
  1034. IB_QP_PATH_MTU |
  1035. IB_QP_DEST_QPN |
  1036. IB_QP_RQ_PSN |
  1037. IB_QP_MAX_DEST_RD_ATOMIC |
  1038. IB_QP_MIN_RNR_TIMER),
  1039. [IB_QPT_XRC_INI] = (IB_QP_AV |
  1040. IB_QP_PATH_MTU |
  1041. IB_QP_DEST_QPN |
  1042. IB_QP_RQ_PSN),
  1043. [IB_QPT_XRC_TGT] = (IB_QP_AV |
  1044. IB_QP_PATH_MTU |
  1045. IB_QP_DEST_QPN |
  1046. IB_QP_RQ_PSN |
  1047. IB_QP_MAX_DEST_RD_ATOMIC |
  1048. IB_QP_MIN_RNR_TIMER),
  1049. },
  1050. .opt_param = {
  1051. [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
  1052. IB_QP_QKEY),
  1053. [IB_QPT_UC] = (IB_QP_ALT_PATH |
  1054. IB_QP_ACCESS_FLAGS |
  1055. IB_QP_PKEY_INDEX),
  1056. [IB_QPT_RC] = (IB_QP_ALT_PATH |
  1057. IB_QP_ACCESS_FLAGS |
  1058. IB_QP_PKEY_INDEX),
  1059. [IB_QPT_XRC_INI] = (IB_QP_ALT_PATH |
  1060. IB_QP_ACCESS_FLAGS |
  1061. IB_QP_PKEY_INDEX),
  1062. [IB_QPT_XRC_TGT] = (IB_QP_ALT_PATH |
  1063. IB_QP_ACCESS_FLAGS |
  1064. IB_QP_PKEY_INDEX),
  1065. [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
  1066. IB_QP_QKEY),
  1067. [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
  1068. IB_QP_QKEY),
  1069. },
  1070. },
  1071. },
  1072. [IB_QPS_RTR] = {
  1073. [IB_QPS_RESET] = { .valid = 1 },
  1074. [IB_QPS_ERR] = { .valid = 1 },
  1075. [IB_QPS_RTS] = {
  1076. .valid = 1,
  1077. .req_param = {
  1078. [IB_QPT_UD] = IB_QP_SQ_PSN,
  1079. [IB_QPT_UC] = IB_QP_SQ_PSN,
  1080. [IB_QPT_RC] = (IB_QP_TIMEOUT |
  1081. IB_QP_RETRY_CNT |
  1082. IB_QP_RNR_RETRY |
  1083. IB_QP_SQ_PSN |
  1084. IB_QP_MAX_QP_RD_ATOMIC),
  1085. [IB_QPT_XRC_INI] = (IB_QP_TIMEOUT |
  1086. IB_QP_RETRY_CNT |
  1087. IB_QP_RNR_RETRY |
  1088. IB_QP_SQ_PSN |
  1089. IB_QP_MAX_QP_RD_ATOMIC),
  1090. [IB_QPT_XRC_TGT] = (IB_QP_TIMEOUT |
  1091. IB_QP_SQ_PSN),
  1092. [IB_QPT_SMI] = IB_QP_SQ_PSN,
  1093. [IB_QPT_GSI] = IB_QP_SQ_PSN,
  1094. },
  1095. .opt_param = {
  1096. [IB_QPT_UD] = (IB_QP_CUR_STATE |
  1097. IB_QP_QKEY),
  1098. [IB_QPT_UC] = (IB_QP_CUR_STATE |
  1099. IB_QP_ALT_PATH |
  1100. IB_QP_ACCESS_FLAGS |
  1101. IB_QP_PATH_MIG_STATE),
  1102. [IB_QPT_RC] = (IB_QP_CUR_STATE |
  1103. IB_QP_ALT_PATH |
  1104. IB_QP_ACCESS_FLAGS |
  1105. IB_QP_MIN_RNR_TIMER |
  1106. IB_QP_PATH_MIG_STATE),
  1107. [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
  1108. IB_QP_ALT_PATH |
  1109. IB_QP_ACCESS_FLAGS |
  1110. IB_QP_PATH_MIG_STATE),
  1111. [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
  1112. IB_QP_ALT_PATH |
  1113. IB_QP_ACCESS_FLAGS |
  1114. IB_QP_MIN_RNR_TIMER |
  1115. IB_QP_PATH_MIG_STATE),
  1116. [IB_QPT_SMI] = (IB_QP_CUR_STATE |
  1117. IB_QP_QKEY),
  1118. [IB_QPT_GSI] = (IB_QP_CUR_STATE |
  1119. IB_QP_QKEY),
  1120. [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
  1121. }
  1122. }
  1123. },
  1124. [IB_QPS_RTS] = {
  1125. [IB_QPS_RESET] = { .valid = 1 },
  1126. [IB_QPS_ERR] = { .valid = 1 },
  1127. [IB_QPS_RTS] = {
  1128. .valid = 1,
  1129. .opt_param = {
  1130. [IB_QPT_UD] = (IB_QP_CUR_STATE |
  1131. IB_QP_QKEY),
  1132. [IB_QPT_UC] = (IB_QP_CUR_STATE |
  1133. IB_QP_ACCESS_FLAGS |
  1134. IB_QP_ALT_PATH |
  1135. IB_QP_PATH_MIG_STATE),
  1136. [IB_QPT_RC] = (IB_QP_CUR_STATE |
  1137. IB_QP_ACCESS_FLAGS |
  1138. IB_QP_ALT_PATH |
  1139. IB_QP_PATH_MIG_STATE |
  1140. IB_QP_MIN_RNR_TIMER),
  1141. [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
  1142. IB_QP_ACCESS_FLAGS |
  1143. IB_QP_ALT_PATH |
  1144. IB_QP_PATH_MIG_STATE),
  1145. [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
  1146. IB_QP_ACCESS_FLAGS |
  1147. IB_QP_ALT_PATH |
  1148. IB_QP_PATH_MIG_STATE |
  1149. IB_QP_MIN_RNR_TIMER),
  1150. [IB_QPT_SMI] = (IB_QP_CUR_STATE |
  1151. IB_QP_QKEY),
  1152. [IB_QPT_GSI] = (IB_QP_CUR_STATE |
  1153. IB_QP_QKEY),
  1154. [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT,
  1155. }
  1156. },
  1157. [IB_QPS_SQD] = {
  1158. .valid = 1,
  1159. .opt_param = {
  1160. [IB_QPT_UD] = IB_QP_EN_SQD_ASYNC_NOTIFY,
  1161. [IB_QPT_UC] = IB_QP_EN_SQD_ASYNC_NOTIFY,
  1162. [IB_QPT_RC] = IB_QP_EN_SQD_ASYNC_NOTIFY,
  1163. [IB_QPT_XRC_INI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
  1164. [IB_QPT_XRC_TGT] = IB_QP_EN_SQD_ASYNC_NOTIFY, /* ??? */
  1165. [IB_QPT_SMI] = IB_QP_EN_SQD_ASYNC_NOTIFY,
  1166. [IB_QPT_GSI] = IB_QP_EN_SQD_ASYNC_NOTIFY
  1167. }
  1168. },
  1169. },
  1170. [IB_QPS_SQD] = {
  1171. [IB_QPS_RESET] = { .valid = 1 },
  1172. [IB_QPS_ERR] = { .valid = 1 },
  1173. [IB_QPS_RTS] = {
  1174. .valid = 1,
  1175. .opt_param = {
  1176. [IB_QPT_UD] = (IB_QP_CUR_STATE |
  1177. IB_QP_QKEY),
  1178. [IB_QPT_UC] = (IB_QP_CUR_STATE |
  1179. IB_QP_ALT_PATH |
  1180. IB_QP_ACCESS_FLAGS |
  1181. IB_QP_PATH_MIG_STATE),
  1182. [IB_QPT_RC] = (IB_QP_CUR_STATE |
  1183. IB_QP_ALT_PATH |
  1184. IB_QP_ACCESS_FLAGS |
  1185. IB_QP_MIN_RNR_TIMER |
  1186. IB_QP_PATH_MIG_STATE),
  1187. [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE |
  1188. IB_QP_ALT_PATH |
  1189. IB_QP_ACCESS_FLAGS |
  1190. IB_QP_PATH_MIG_STATE),
  1191. [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE |
  1192. IB_QP_ALT_PATH |
  1193. IB_QP_ACCESS_FLAGS |
  1194. IB_QP_MIN_RNR_TIMER |
  1195. IB_QP_PATH_MIG_STATE),
  1196. [IB_QPT_SMI] = (IB_QP_CUR_STATE |
  1197. IB_QP_QKEY),
  1198. [IB_QPT_GSI] = (IB_QP_CUR_STATE |
  1199. IB_QP_QKEY),
  1200. }
  1201. },
  1202. [IB_QPS_SQD] = {
  1203. .valid = 1,
  1204. .opt_param = {
  1205. [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
  1206. IB_QP_QKEY),
  1207. [IB_QPT_UC] = (IB_QP_AV |
  1208. IB_QP_ALT_PATH |
  1209. IB_QP_ACCESS_FLAGS |
  1210. IB_QP_PKEY_INDEX |
  1211. IB_QP_PATH_MIG_STATE),
  1212. [IB_QPT_RC] = (IB_QP_PORT |
  1213. IB_QP_AV |
  1214. IB_QP_TIMEOUT |
  1215. IB_QP_RETRY_CNT |
  1216. IB_QP_RNR_RETRY |
  1217. IB_QP_MAX_QP_RD_ATOMIC |
  1218. IB_QP_MAX_DEST_RD_ATOMIC |
  1219. IB_QP_ALT_PATH |
  1220. IB_QP_ACCESS_FLAGS |
  1221. IB_QP_PKEY_INDEX |
  1222. IB_QP_MIN_RNR_TIMER |
  1223. IB_QP_PATH_MIG_STATE),
  1224. [IB_QPT_XRC_INI] = (IB_QP_PORT |
  1225. IB_QP_AV |
  1226. IB_QP_TIMEOUT |
  1227. IB_QP_RETRY_CNT |
  1228. IB_QP_RNR_RETRY |
  1229. IB_QP_MAX_QP_RD_ATOMIC |
  1230. IB_QP_ALT_PATH |
  1231. IB_QP_ACCESS_FLAGS |
  1232. IB_QP_PKEY_INDEX |
  1233. IB_QP_PATH_MIG_STATE),
  1234. [IB_QPT_XRC_TGT] = (IB_QP_PORT |
  1235. IB_QP_AV |
  1236. IB_QP_TIMEOUT |
  1237. IB_QP_MAX_DEST_RD_ATOMIC |
  1238. IB_QP_ALT_PATH |
  1239. IB_QP_ACCESS_FLAGS |
  1240. IB_QP_PKEY_INDEX |
  1241. IB_QP_MIN_RNR_TIMER |
  1242. IB_QP_PATH_MIG_STATE),
  1243. [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
  1244. IB_QP_QKEY),
  1245. [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
  1246. IB_QP_QKEY),
  1247. }
  1248. }
  1249. },
  1250. [IB_QPS_SQE] = {
  1251. [IB_QPS_RESET] = { .valid = 1 },
  1252. [IB_QPS_ERR] = { .valid = 1 },
  1253. [IB_QPS_RTS] = {
  1254. .valid = 1,
  1255. .opt_param = {
  1256. [IB_QPT_UD] = (IB_QP_CUR_STATE |
  1257. IB_QP_QKEY),
  1258. [IB_QPT_UC] = (IB_QP_CUR_STATE |
  1259. IB_QP_ACCESS_FLAGS),
  1260. [IB_QPT_SMI] = (IB_QP_CUR_STATE |
  1261. IB_QP_QKEY),
  1262. [IB_QPT_GSI] = (IB_QP_CUR_STATE |
  1263. IB_QP_QKEY),
  1264. }
  1265. }
  1266. },
  1267. [IB_QPS_ERR] = {
  1268. [IB_QPS_RESET] = { .valid = 1 },
  1269. [IB_QPS_ERR] = { .valid = 1 }
  1270. }
  1271. };
  1272. bool ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
  1273. enum ib_qp_type type, enum ib_qp_attr_mask mask,
  1274. enum rdma_link_layer ll)
  1275. {
  1276. enum ib_qp_attr_mask req_param, opt_param;
  1277. if (mask & IB_QP_CUR_STATE &&
  1278. cur_state != IB_QPS_RTR && cur_state != IB_QPS_RTS &&
  1279. cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE)
  1280. return false;
  1281. if (!qp_state_table[cur_state][next_state].valid)
  1282. return false;
  1283. req_param = qp_state_table[cur_state][next_state].req_param[type];
  1284. opt_param = qp_state_table[cur_state][next_state].opt_param[type];
  1285. if ((mask & req_param) != req_param)
  1286. return false;
  1287. if (mask & ~(req_param | opt_param | IB_QP_STATE))
  1288. return false;
  1289. return true;
  1290. }
  1291. EXPORT_SYMBOL(ib_modify_qp_is_ok);
  1292. /**
  1293. * ib_resolve_eth_dmac - Resolve destination mac address
  1294. * @device: Device to consider
  1295. * @ah_attr: address handle attribute which describes the
  1296. * source and destination parameters
  1297. * ib_resolve_eth_dmac() resolves destination mac address and L3 hop limit It
  1298. * returns 0 on success or appropriate error code. It initializes the
  1299. * necessary ah_attr fields when call is successful.
  1300. */
  1301. static int ib_resolve_eth_dmac(struct ib_device *device,
  1302. struct rdma_ah_attr *ah_attr)
  1303. {
  1304. int ret = 0;
  1305. if (rdma_is_multicast_addr((struct in6_addr *)ah_attr->grh.dgid.raw)) {
  1306. if (ipv6_addr_v4mapped((struct in6_addr *)ah_attr->grh.dgid.raw)) {
  1307. __be32 addr = 0;
  1308. memcpy(&addr, ah_attr->grh.dgid.raw + 12, 4);
  1309. ip_eth_mc_map(addr, (char *)ah_attr->roce.dmac);
  1310. } else {
  1311. ipv6_eth_mc_map((struct in6_addr *)ah_attr->grh.dgid.raw,
  1312. (char *)ah_attr->roce.dmac);
  1313. }
  1314. } else {
  1315. ret = ib_resolve_unicast_gid_dmac(device, ah_attr);
  1316. }
  1317. return ret;
  1318. }
  1319. static bool is_qp_type_connected(const struct ib_qp *qp)
  1320. {
  1321. return (qp->qp_type == IB_QPT_UC ||
  1322. qp->qp_type == IB_QPT_RC ||
  1323. qp->qp_type == IB_QPT_XRC_INI ||
  1324. qp->qp_type == IB_QPT_XRC_TGT);
  1325. }
  1326. /**
  1327. * IB core internal function to perform QP attributes modification.
  1328. */
  1329. static int _ib_modify_qp(struct ib_qp *qp, struct ib_qp_attr *attr,
  1330. int attr_mask, struct ib_udata *udata)
  1331. {
  1332. u8 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
  1333. const struct ib_gid_attr *old_sgid_attr_av;
  1334. const struct ib_gid_attr *old_sgid_attr_alt_av;
  1335. int ret;
  1336. /*
  1337. * Today the core code can only handle alternate paths and APM for IB
  1338. * ban them in roce mode.
  1339. */
  1340. if (attr_mask & IB_QP_ALT_PATH &&
  1341. !rdma_protocol_ib(qp->device, attr->alt_ah_attr.port_num))
  1342. return -EINVAL;
  1343. if (attr_mask & IB_QP_AV) {
  1344. ret = rdma_fill_sgid_attr(qp->device, &attr->ah_attr,
  1345. &old_sgid_attr_av);
  1346. if (ret)
  1347. return ret;
  1348. }
  1349. if (attr_mask & IB_QP_ALT_PATH) {
  1350. ret = rdma_fill_sgid_attr(qp->device, &attr->alt_ah_attr,
  1351. &old_sgid_attr_alt_av);
  1352. if (ret)
  1353. goto out_av;
  1354. }
  1355. /*
  1356. * If the user provided the qp_attr then we have to resolve it. Kernel
  1357. * users have to provide already resolved rdma_ah_attr's
  1358. */
  1359. if (udata && (attr_mask & IB_QP_AV) &&
  1360. attr->ah_attr.type == RDMA_AH_ATTR_TYPE_ROCE &&
  1361. is_qp_type_connected(qp)) {
  1362. ret = ib_resolve_eth_dmac(qp->device, &attr->ah_attr);
  1363. if (ret)
  1364. goto out;
  1365. }
  1366. if (rdma_ib_or_roce(qp->device, port)) {
  1367. if (attr_mask & IB_QP_RQ_PSN && attr->rq_psn & ~0xffffff) {
  1368. pr_warn("%s: %s rq_psn overflow, masking to 24 bits\n",
  1369. __func__, qp->device->name);
  1370. attr->rq_psn &= 0xffffff;
  1371. }
  1372. if (attr_mask & IB_QP_SQ_PSN && attr->sq_psn & ~0xffffff) {
  1373. pr_warn("%s: %s sq_psn overflow, masking to 24 bits\n",
  1374. __func__, qp->device->name);
  1375. attr->sq_psn &= 0xffffff;
  1376. }
  1377. }
  1378. ret = ib_security_modify_qp(qp, attr, attr_mask, udata);
  1379. if (!ret && (attr_mask & IB_QP_PORT))
  1380. qp->port = attr->port_num;
  1381. out:
  1382. if (attr_mask & IB_QP_ALT_PATH)
  1383. rdma_unfill_sgid_attr(&attr->alt_ah_attr, old_sgid_attr_alt_av);
  1384. out_av:
  1385. if (attr_mask & IB_QP_AV)
  1386. rdma_unfill_sgid_attr(&attr->ah_attr, old_sgid_attr_av);
  1387. return ret;
  1388. }
  1389. /**
  1390. * ib_modify_qp_with_udata - Modifies the attributes for the specified QP.
  1391. * @ib_qp: The QP to modify.
  1392. * @attr: On input, specifies the QP attributes to modify. On output,
  1393. * the current values of selected QP attributes are returned.
  1394. * @attr_mask: A bit-mask used to specify which attributes of the QP
  1395. * are being modified.
  1396. * @udata: pointer to user's input output buffer information
  1397. * are being modified.
  1398. * It returns 0 on success and returns appropriate error code on error.
  1399. */
  1400. int ib_modify_qp_with_udata(struct ib_qp *ib_qp, struct ib_qp_attr *attr,
  1401. int attr_mask, struct ib_udata *udata)
  1402. {
  1403. return _ib_modify_qp(ib_qp->real_qp, attr, attr_mask, udata);
  1404. }
  1405. EXPORT_SYMBOL(ib_modify_qp_with_udata);
  1406. int ib_get_eth_speed(struct ib_device *dev, u8 port_num, u8 *speed, u8 *width)
  1407. {
  1408. int rc;
  1409. u32 netdev_speed;
  1410. struct net_device *netdev;
  1411. struct ethtool_link_ksettings lksettings;
  1412. if (rdma_port_get_link_layer(dev, port_num) != IB_LINK_LAYER_ETHERNET)
  1413. return -EINVAL;
  1414. if (!dev->get_netdev)
  1415. return -EOPNOTSUPP;
  1416. netdev = dev->get_netdev(dev, port_num);
  1417. if (!netdev)
  1418. return -ENODEV;
  1419. rtnl_lock();
  1420. rc = __ethtool_get_link_ksettings(netdev, &lksettings);
  1421. rtnl_unlock();
  1422. dev_put(netdev);
  1423. if (!rc) {
  1424. netdev_speed = lksettings.base.speed;
  1425. } else {
  1426. netdev_speed = SPEED_1000;
  1427. pr_warn("%s speed is unknown, defaulting to %d\n", netdev->name,
  1428. netdev_speed);
  1429. }
  1430. if (netdev_speed <= SPEED_1000) {
  1431. *width = IB_WIDTH_1X;
  1432. *speed = IB_SPEED_SDR;
  1433. } else if (netdev_speed <= SPEED_10000) {
  1434. *width = IB_WIDTH_1X;
  1435. *speed = IB_SPEED_FDR10;
  1436. } else if (netdev_speed <= SPEED_20000) {
  1437. *width = IB_WIDTH_4X;
  1438. *speed = IB_SPEED_DDR;
  1439. } else if (netdev_speed <= SPEED_25000) {
  1440. *width = IB_WIDTH_1X;
  1441. *speed = IB_SPEED_EDR;
  1442. } else if (netdev_speed <= SPEED_40000) {
  1443. *width = IB_WIDTH_4X;
  1444. *speed = IB_SPEED_FDR10;
  1445. } else {
  1446. *width = IB_WIDTH_4X;
  1447. *speed = IB_SPEED_EDR;
  1448. }
  1449. return 0;
  1450. }
  1451. EXPORT_SYMBOL(ib_get_eth_speed);
  1452. int ib_modify_qp(struct ib_qp *qp,
  1453. struct ib_qp_attr *qp_attr,
  1454. int qp_attr_mask)
  1455. {
  1456. return _ib_modify_qp(qp->real_qp, qp_attr, qp_attr_mask, NULL);
  1457. }
  1458. EXPORT_SYMBOL(ib_modify_qp);
  1459. int ib_query_qp(struct ib_qp *qp,
  1460. struct ib_qp_attr *qp_attr,
  1461. int qp_attr_mask,
  1462. struct ib_qp_init_attr *qp_init_attr)
  1463. {
  1464. qp_attr->ah_attr.grh.sgid_attr = NULL;
  1465. qp_attr->alt_ah_attr.grh.sgid_attr = NULL;
  1466. return qp->device->query_qp ?
  1467. qp->device->query_qp(qp->real_qp, qp_attr, qp_attr_mask, qp_init_attr) :
  1468. -EOPNOTSUPP;
  1469. }
  1470. EXPORT_SYMBOL(ib_query_qp);
  1471. int ib_close_qp(struct ib_qp *qp)
  1472. {
  1473. struct ib_qp *real_qp;
  1474. unsigned long flags;
  1475. real_qp = qp->real_qp;
  1476. if (real_qp == qp)
  1477. return -EINVAL;
  1478. spin_lock_irqsave(&real_qp->device->event_handler_lock, flags);
  1479. list_del(&qp->open_list);
  1480. spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags);
  1481. atomic_dec(&real_qp->usecnt);
  1482. if (qp->qp_sec)
  1483. ib_close_shared_qp_security(qp->qp_sec);
  1484. kfree(qp);
  1485. return 0;
  1486. }
  1487. EXPORT_SYMBOL(ib_close_qp);
  1488. static int __ib_destroy_shared_qp(struct ib_qp *qp)
  1489. {
  1490. struct ib_xrcd *xrcd;
  1491. struct ib_qp *real_qp;
  1492. int ret;
  1493. real_qp = qp->real_qp;
  1494. xrcd = real_qp->xrcd;
  1495. mutex_lock(&xrcd->tgt_qp_mutex);
  1496. ib_close_qp(qp);
  1497. if (atomic_read(&real_qp->usecnt) == 0)
  1498. list_del(&real_qp->xrcd_list);
  1499. else
  1500. real_qp = NULL;
  1501. mutex_unlock(&xrcd->tgt_qp_mutex);
  1502. if (real_qp) {
  1503. ret = ib_destroy_qp(real_qp);
  1504. if (!ret)
  1505. atomic_dec(&xrcd->usecnt);
  1506. else
  1507. __ib_insert_xrcd_qp(xrcd, real_qp);
  1508. }
  1509. return 0;
  1510. }
  1511. int ib_destroy_qp(struct ib_qp *qp)
  1512. {
  1513. struct ib_pd *pd;
  1514. struct ib_cq *scq, *rcq;
  1515. struct ib_srq *srq;
  1516. struct ib_rwq_ind_table *ind_tbl;
  1517. struct ib_qp_security *sec;
  1518. int ret;
  1519. WARN_ON_ONCE(qp->mrs_used > 0);
  1520. if (atomic_read(&qp->usecnt))
  1521. return -EBUSY;
  1522. if (qp->real_qp != qp)
  1523. return __ib_destroy_shared_qp(qp);
  1524. pd = qp->pd;
  1525. scq = qp->send_cq;
  1526. rcq = qp->recv_cq;
  1527. srq = qp->srq;
  1528. ind_tbl = qp->rwq_ind_tbl;
  1529. sec = qp->qp_sec;
  1530. if (sec)
  1531. ib_destroy_qp_security_begin(sec);
  1532. if (!qp->uobject)
  1533. rdma_rw_cleanup_mrs(qp);
  1534. rdma_restrack_del(&qp->res);
  1535. ret = qp->device->destroy_qp(qp);
  1536. if (!ret) {
  1537. if (pd)
  1538. atomic_dec(&pd->usecnt);
  1539. if (scq)
  1540. atomic_dec(&scq->usecnt);
  1541. if (rcq)
  1542. atomic_dec(&rcq->usecnt);
  1543. if (srq)
  1544. atomic_dec(&srq->usecnt);
  1545. if (ind_tbl)
  1546. atomic_dec(&ind_tbl->usecnt);
  1547. if (sec)
  1548. ib_destroy_qp_security_end(sec);
  1549. } else {
  1550. if (sec)
  1551. ib_destroy_qp_security_abort(sec);
  1552. }
  1553. return ret;
  1554. }
  1555. EXPORT_SYMBOL(ib_destroy_qp);
  1556. /* Completion queues */
  1557. struct ib_cq *ib_create_cq(struct ib_device *device,
  1558. ib_comp_handler comp_handler,
  1559. void (*event_handler)(struct ib_event *, void *),
  1560. void *cq_context,
  1561. const struct ib_cq_init_attr *cq_attr)
  1562. {
  1563. struct ib_cq *cq;
  1564. cq = device->create_cq(device, cq_attr, NULL, NULL);
  1565. if (!IS_ERR(cq)) {
  1566. cq->device = device;
  1567. cq->uobject = NULL;
  1568. cq->comp_handler = comp_handler;
  1569. cq->event_handler = event_handler;
  1570. cq->cq_context = cq_context;
  1571. atomic_set(&cq->usecnt, 0);
  1572. cq->res.type = RDMA_RESTRACK_CQ;
  1573. rdma_restrack_add(&cq->res);
  1574. }
  1575. return cq;
  1576. }
  1577. EXPORT_SYMBOL(ib_create_cq);
  1578. int rdma_set_cq_moderation(struct ib_cq *cq, u16 cq_count, u16 cq_period)
  1579. {
  1580. return cq->device->modify_cq ?
  1581. cq->device->modify_cq(cq, cq_count, cq_period) : -EOPNOTSUPP;
  1582. }
  1583. EXPORT_SYMBOL(rdma_set_cq_moderation);
  1584. int ib_destroy_cq(struct ib_cq *cq)
  1585. {
  1586. if (atomic_read(&cq->usecnt))
  1587. return -EBUSY;
  1588. rdma_restrack_del(&cq->res);
  1589. return cq->device->destroy_cq(cq);
  1590. }
  1591. EXPORT_SYMBOL(ib_destroy_cq);
  1592. int ib_resize_cq(struct ib_cq *cq, int cqe)
  1593. {
  1594. return cq->device->resize_cq ?
  1595. cq->device->resize_cq(cq, cqe, NULL) : -EOPNOTSUPP;
  1596. }
  1597. EXPORT_SYMBOL(ib_resize_cq);
  1598. /* Memory regions */
  1599. int ib_dereg_mr(struct ib_mr *mr)
  1600. {
  1601. struct ib_pd *pd = mr->pd;
  1602. struct ib_dm *dm = mr->dm;
  1603. int ret;
  1604. rdma_restrack_del(&mr->res);
  1605. ret = mr->device->dereg_mr(mr);
  1606. if (!ret) {
  1607. atomic_dec(&pd->usecnt);
  1608. if (dm)
  1609. atomic_dec(&dm->usecnt);
  1610. }
  1611. return ret;
  1612. }
  1613. EXPORT_SYMBOL(ib_dereg_mr);
  1614. /**
  1615. * ib_alloc_mr() - Allocates a memory region
  1616. * @pd: protection domain associated with the region
  1617. * @mr_type: memory region type
  1618. * @max_num_sg: maximum sg entries available for registration.
  1619. *
  1620. * Notes:
  1621. * Memory registeration page/sg lists must not exceed max_num_sg.
  1622. * For mr_type IB_MR_TYPE_MEM_REG, the total length cannot exceed
  1623. * max_num_sg * used_page_size.
  1624. *
  1625. */
  1626. struct ib_mr *ib_alloc_mr(struct ib_pd *pd,
  1627. enum ib_mr_type mr_type,
  1628. u32 max_num_sg)
  1629. {
  1630. struct ib_mr *mr;
  1631. if (!pd->device->alloc_mr)
  1632. return ERR_PTR(-EOPNOTSUPP);
  1633. mr = pd->device->alloc_mr(pd, mr_type, max_num_sg);
  1634. if (!IS_ERR(mr)) {
  1635. mr->device = pd->device;
  1636. mr->pd = pd;
  1637. mr->dm = NULL;
  1638. mr->uobject = NULL;
  1639. atomic_inc(&pd->usecnt);
  1640. mr->need_inval = false;
  1641. mr->res.type = RDMA_RESTRACK_MR;
  1642. rdma_restrack_add(&mr->res);
  1643. }
  1644. return mr;
  1645. }
  1646. EXPORT_SYMBOL(ib_alloc_mr);
  1647. /* "Fast" memory regions */
  1648. struct ib_fmr *ib_alloc_fmr(struct ib_pd *pd,
  1649. int mr_access_flags,
  1650. struct ib_fmr_attr *fmr_attr)
  1651. {
  1652. struct ib_fmr *fmr;
  1653. if (!pd->device->alloc_fmr)
  1654. return ERR_PTR(-EOPNOTSUPP);
  1655. fmr = pd->device->alloc_fmr(pd, mr_access_flags, fmr_attr);
  1656. if (!IS_ERR(fmr)) {
  1657. fmr->device = pd->device;
  1658. fmr->pd = pd;
  1659. atomic_inc(&pd->usecnt);
  1660. }
  1661. return fmr;
  1662. }
  1663. EXPORT_SYMBOL(ib_alloc_fmr);
  1664. int ib_unmap_fmr(struct list_head *fmr_list)
  1665. {
  1666. struct ib_fmr *fmr;
  1667. if (list_empty(fmr_list))
  1668. return 0;
  1669. fmr = list_entry(fmr_list->next, struct ib_fmr, list);
  1670. return fmr->device->unmap_fmr(fmr_list);
  1671. }
  1672. EXPORT_SYMBOL(ib_unmap_fmr);
  1673. int ib_dealloc_fmr(struct ib_fmr *fmr)
  1674. {
  1675. struct ib_pd *pd;
  1676. int ret;
  1677. pd = fmr->pd;
  1678. ret = fmr->device->dealloc_fmr(fmr);
  1679. if (!ret)
  1680. atomic_dec(&pd->usecnt);
  1681. return ret;
  1682. }
  1683. EXPORT_SYMBOL(ib_dealloc_fmr);
  1684. /* Multicast groups */
  1685. static bool is_valid_mcast_lid(struct ib_qp *qp, u16 lid)
  1686. {
  1687. struct ib_qp_init_attr init_attr = {};
  1688. struct ib_qp_attr attr = {};
  1689. int num_eth_ports = 0;
  1690. int port;
  1691. /* If QP state >= init, it is assigned to a port and we can check this
  1692. * port only.
  1693. */
  1694. if (!ib_query_qp(qp, &attr, IB_QP_STATE | IB_QP_PORT, &init_attr)) {
  1695. if (attr.qp_state >= IB_QPS_INIT) {
  1696. if (rdma_port_get_link_layer(qp->device, attr.port_num) !=
  1697. IB_LINK_LAYER_INFINIBAND)
  1698. return true;
  1699. goto lid_check;
  1700. }
  1701. }
  1702. /* Can't get a quick answer, iterate over all ports */
  1703. for (port = 0; port < qp->device->phys_port_cnt; port++)
  1704. if (rdma_port_get_link_layer(qp->device, port) !=
  1705. IB_LINK_LAYER_INFINIBAND)
  1706. num_eth_ports++;
  1707. /* If we have at lease one Ethernet port, RoCE annex declares that
  1708. * multicast LID should be ignored. We can't tell at this step if the
  1709. * QP belongs to an IB or Ethernet port.
  1710. */
  1711. if (num_eth_ports)
  1712. return true;
  1713. /* If all the ports are IB, we can check according to IB spec. */
  1714. lid_check:
  1715. return !(lid < be16_to_cpu(IB_MULTICAST_LID_BASE) ||
  1716. lid == be16_to_cpu(IB_LID_PERMISSIVE));
  1717. }
  1718. int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
  1719. {
  1720. int ret;
  1721. if (!qp->device->attach_mcast)
  1722. return -EOPNOTSUPP;
  1723. if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
  1724. qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
  1725. return -EINVAL;
  1726. ret = qp->device->attach_mcast(qp, gid, lid);
  1727. if (!ret)
  1728. atomic_inc(&qp->usecnt);
  1729. return ret;
  1730. }
  1731. EXPORT_SYMBOL(ib_attach_mcast);
  1732. int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid)
  1733. {
  1734. int ret;
  1735. if (!qp->device->detach_mcast)
  1736. return -EOPNOTSUPP;
  1737. if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) ||
  1738. qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid))
  1739. return -EINVAL;
  1740. ret = qp->device->detach_mcast(qp, gid, lid);
  1741. if (!ret)
  1742. atomic_dec(&qp->usecnt);
  1743. return ret;
  1744. }
  1745. EXPORT_SYMBOL(ib_detach_mcast);
  1746. struct ib_xrcd *__ib_alloc_xrcd(struct ib_device *device, const char *caller)
  1747. {
  1748. struct ib_xrcd *xrcd;
  1749. if (!device->alloc_xrcd)
  1750. return ERR_PTR(-EOPNOTSUPP);
  1751. xrcd = device->alloc_xrcd(device, NULL, NULL);
  1752. if (!IS_ERR(xrcd)) {
  1753. xrcd->device = device;
  1754. xrcd->inode = NULL;
  1755. atomic_set(&xrcd->usecnt, 0);
  1756. mutex_init(&xrcd->tgt_qp_mutex);
  1757. INIT_LIST_HEAD(&xrcd->tgt_qp_list);
  1758. }
  1759. return xrcd;
  1760. }
  1761. EXPORT_SYMBOL(__ib_alloc_xrcd);
  1762. int ib_dealloc_xrcd(struct ib_xrcd *xrcd)
  1763. {
  1764. struct ib_qp *qp;
  1765. int ret;
  1766. if (atomic_read(&xrcd->usecnt))
  1767. return -EBUSY;
  1768. while (!list_empty(&xrcd->tgt_qp_list)) {
  1769. qp = list_entry(xrcd->tgt_qp_list.next, struct ib_qp, xrcd_list);
  1770. ret = ib_destroy_qp(qp);
  1771. if (ret)
  1772. return ret;
  1773. }
  1774. return xrcd->device->dealloc_xrcd(xrcd);
  1775. }
  1776. EXPORT_SYMBOL(ib_dealloc_xrcd);
  1777. /**
  1778. * ib_create_wq - Creates a WQ associated with the specified protection
  1779. * domain.
  1780. * @pd: The protection domain associated with the WQ.
  1781. * @wq_attr: A list of initial attributes required to create the
  1782. * WQ. If WQ creation succeeds, then the attributes are updated to
  1783. * the actual capabilities of the created WQ.
  1784. *
  1785. * wq_attr->max_wr and wq_attr->max_sge determine
  1786. * the requested size of the WQ, and set to the actual values allocated
  1787. * on return.
  1788. * If ib_create_wq() succeeds, then max_wr and max_sge will always be
  1789. * at least as large as the requested values.
  1790. */
  1791. struct ib_wq *ib_create_wq(struct ib_pd *pd,
  1792. struct ib_wq_init_attr *wq_attr)
  1793. {
  1794. struct ib_wq *wq;
  1795. if (!pd->device->create_wq)
  1796. return ERR_PTR(-EOPNOTSUPP);
  1797. wq = pd->device->create_wq(pd, wq_attr, NULL);
  1798. if (!IS_ERR(wq)) {
  1799. wq->event_handler = wq_attr->event_handler;
  1800. wq->wq_context = wq_attr->wq_context;
  1801. wq->wq_type = wq_attr->wq_type;
  1802. wq->cq = wq_attr->cq;
  1803. wq->device = pd->device;
  1804. wq->pd = pd;
  1805. wq->uobject = NULL;
  1806. atomic_inc(&pd->usecnt);
  1807. atomic_inc(&wq_attr->cq->usecnt);
  1808. atomic_set(&wq->usecnt, 0);
  1809. }
  1810. return wq;
  1811. }
  1812. EXPORT_SYMBOL(ib_create_wq);
  1813. /**
  1814. * ib_destroy_wq - Destroys the specified WQ.
  1815. * @wq: The WQ to destroy.
  1816. */
  1817. int ib_destroy_wq(struct ib_wq *wq)
  1818. {
  1819. int err;
  1820. struct ib_cq *cq = wq->cq;
  1821. struct ib_pd *pd = wq->pd;
  1822. if (atomic_read(&wq->usecnt))
  1823. return -EBUSY;
  1824. err = wq->device->destroy_wq(wq);
  1825. if (!err) {
  1826. atomic_dec(&pd->usecnt);
  1827. atomic_dec(&cq->usecnt);
  1828. }
  1829. return err;
  1830. }
  1831. EXPORT_SYMBOL(ib_destroy_wq);
  1832. /**
  1833. * ib_modify_wq - Modifies the specified WQ.
  1834. * @wq: The WQ to modify.
  1835. * @wq_attr: On input, specifies the WQ attributes to modify.
  1836. * @wq_attr_mask: A bit-mask used to specify which attributes of the WQ
  1837. * are being modified.
  1838. * On output, the current values of selected WQ attributes are returned.
  1839. */
  1840. int ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
  1841. u32 wq_attr_mask)
  1842. {
  1843. int err;
  1844. if (!wq->device->modify_wq)
  1845. return -EOPNOTSUPP;
  1846. err = wq->device->modify_wq(wq, wq_attr, wq_attr_mask, NULL);
  1847. return err;
  1848. }
  1849. EXPORT_SYMBOL(ib_modify_wq);
  1850. /*
  1851. * ib_create_rwq_ind_table - Creates a RQ Indirection Table.
  1852. * @device: The device on which to create the rwq indirection table.
  1853. * @ib_rwq_ind_table_init_attr: A list of initial attributes required to
  1854. * create the Indirection Table.
  1855. *
  1856. * Note: The life time of ib_rwq_ind_table_init_attr->ind_tbl is not less
  1857. * than the created ib_rwq_ind_table object and the caller is responsible
  1858. * for its memory allocation/free.
  1859. */
  1860. struct ib_rwq_ind_table *ib_create_rwq_ind_table(struct ib_device *device,
  1861. struct ib_rwq_ind_table_init_attr *init_attr)
  1862. {
  1863. struct ib_rwq_ind_table *rwq_ind_table;
  1864. int i;
  1865. u32 table_size;
  1866. if (!device->create_rwq_ind_table)
  1867. return ERR_PTR(-EOPNOTSUPP);
  1868. table_size = (1 << init_attr->log_ind_tbl_size);
  1869. rwq_ind_table = device->create_rwq_ind_table(device,
  1870. init_attr, NULL);
  1871. if (IS_ERR(rwq_ind_table))
  1872. return rwq_ind_table;
  1873. rwq_ind_table->ind_tbl = init_attr->ind_tbl;
  1874. rwq_ind_table->log_ind_tbl_size = init_attr->log_ind_tbl_size;
  1875. rwq_ind_table->device = device;
  1876. rwq_ind_table->uobject = NULL;
  1877. atomic_set(&rwq_ind_table->usecnt, 0);
  1878. for (i = 0; i < table_size; i++)
  1879. atomic_inc(&rwq_ind_table->ind_tbl[i]->usecnt);
  1880. return rwq_ind_table;
  1881. }
  1882. EXPORT_SYMBOL(ib_create_rwq_ind_table);
  1883. /*
  1884. * ib_destroy_rwq_ind_table - Destroys the specified Indirection Table.
  1885. * @wq_ind_table: The Indirection Table to destroy.
  1886. */
  1887. int ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *rwq_ind_table)
  1888. {
  1889. int err, i;
  1890. u32 table_size = (1 << rwq_ind_table->log_ind_tbl_size);
  1891. struct ib_wq **ind_tbl = rwq_ind_table->ind_tbl;
  1892. if (atomic_read(&rwq_ind_table->usecnt))
  1893. return -EBUSY;
  1894. err = rwq_ind_table->device->destroy_rwq_ind_table(rwq_ind_table);
  1895. if (!err) {
  1896. for (i = 0; i < table_size; i++)
  1897. atomic_dec(&ind_tbl[i]->usecnt);
  1898. }
  1899. return err;
  1900. }
  1901. EXPORT_SYMBOL(ib_destroy_rwq_ind_table);
  1902. struct ib_flow *ib_create_flow(struct ib_qp *qp,
  1903. struct ib_flow_attr *flow_attr,
  1904. int domain)
  1905. {
  1906. struct ib_flow *flow_id;
  1907. if (!qp->device->create_flow)
  1908. return ERR_PTR(-EOPNOTSUPP);
  1909. flow_id = qp->device->create_flow(qp, flow_attr, domain, NULL);
  1910. if (!IS_ERR(flow_id)) {
  1911. atomic_inc(&qp->usecnt);
  1912. flow_id->qp = qp;
  1913. }
  1914. return flow_id;
  1915. }
  1916. EXPORT_SYMBOL(ib_create_flow);
  1917. int ib_destroy_flow(struct ib_flow *flow_id)
  1918. {
  1919. int err;
  1920. struct ib_qp *qp = flow_id->qp;
  1921. err = qp->device->destroy_flow(flow_id);
  1922. if (!err)
  1923. atomic_dec(&qp->usecnt);
  1924. return err;
  1925. }
  1926. EXPORT_SYMBOL(ib_destroy_flow);
  1927. int ib_check_mr_status(struct ib_mr *mr, u32 check_mask,
  1928. struct ib_mr_status *mr_status)
  1929. {
  1930. return mr->device->check_mr_status ?
  1931. mr->device->check_mr_status(mr, check_mask, mr_status) : -EOPNOTSUPP;
  1932. }
  1933. EXPORT_SYMBOL(ib_check_mr_status);
  1934. int ib_set_vf_link_state(struct ib_device *device, int vf, u8 port,
  1935. int state)
  1936. {
  1937. if (!device->set_vf_link_state)
  1938. return -EOPNOTSUPP;
  1939. return device->set_vf_link_state(device, vf, port, state);
  1940. }
  1941. EXPORT_SYMBOL(ib_set_vf_link_state);
  1942. int ib_get_vf_config(struct ib_device *device, int vf, u8 port,
  1943. struct ifla_vf_info *info)
  1944. {
  1945. if (!device->get_vf_config)
  1946. return -EOPNOTSUPP;
  1947. return device->get_vf_config(device, vf, port, info);
  1948. }
  1949. EXPORT_SYMBOL(ib_get_vf_config);
  1950. int ib_get_vf_stats(struct ib_device *device, int vf, u8 port,
  1951. struct ifla_vf_stats *stats)
  1952. {
  1953. if (!device->get_vf_stats)
  1954. return -EOPNOTSUPP;
  1955. return device->get_vf_stats(device, vf, port, stats);
  1956. }
  1957. EXPORT_SYMBOL(ib_get_vf_stats);
  1958. int ib_set_vf_guid(struct ib_device *device, int vf, u8 port, u64 guid,
  1959. int type)
  1960. {
  1961. if (!device->set_vf_guid)
  1962. return -EOPNOTSUPP;
  1963. return device->set_vf_guid(device, vf, port, guid, type);
  1964. }
  1965. EXPORT_SYMBOL(ib_set_vf_guid);
  1966. /**
  1967. * ib_map_mr_sg() - Map the largest prefix of a dma mapped SG list
  1968. * and set it the memory region.
  1969. * @mr: memory region
  1970. * @sg: dma mapped scatterlist
  1971. * @sg_nents: number of entries in sg
  1972. * @sg_offset: offset in bytes into sg
  1973. * @page_size: page vector desired page size
  1974. *
  1975. * Constraints:
  1976. * - The first sg element is allowed to have an offset.
  1977. * - Each sg element must either be aligned to page_size or virtually
  1978. * contiguous to the previous element. In case an sg element has a
  1979. * non-contiguous offset, the mapping prefix will not include it.
  1980. * - The last sg element is allowed to have length less than page_size.
  1981. * - If sg_nents total byte length exceeds the mr max_num_sge * page_size
  1982. * then only max_num_sg entries will be mapped.
  1983. * - If the MR was allocated with type IB_MR_TYPE_SG_GAPS, none of these
  1984. * constraints holds and the page_size argument is ignored.
  1985. *
  1986. * Returns the number of sg elements that were mapped to the memory region.
  1987. *
  1988. * After this completes successfully, the memory region
  1989. * is ready for registration.
  1990. */
  1991. int ib_map_mr_sg(struct ib_mr *mr, struct scatterlist *sg, int sg_nents,
  1992. unsigned int *sg_offset, unsigned int page_size)
  1993. {
  1994. if (unlikely(!mr->device->map_mr_sg))
  1995. return -EOPNOTSUPP;
  1996. mr->page_size = page_size;
  1997. return mr->device->map_mr_sg(mr, sg, sg_nents, sg_offset);
  1998. }
  1999. EXPORT_SYMBOL(ib_map_mr_sg);
  2000. /**
  2001. * ib_sg_to_pages() - Convert the largest prefix of a sg list
  2002. * to a page vector
  2003. * @mr: memory region
  2004. * @sgl: dma mapped scatterlist
  2005. * @sg_nents: number of entries in sg
  2006. * @sg_offset_p: IN: start offset in bytes into sg
  2007. * OUT: offset in bytes for element n of the sg of the first
  2008. * byte that has not been processed where n is the return
  2009. * value of this function.
  2010. * @set_page: driver page assignment function pointer
  2011. *
  2012. * Core service helper for drivers to convert the largest
  2013. * prefix of given sg list to a page vector. The sg list
  2014. * prefix converted is the prefix that meet the requirements
  2015. * of ib_map_mr_sg.
  2016. *
  2017. * Returns the number of sg elements that were assigned to
  2018. * a page vector.
  2019. */
  2020. int ib_sg_to_pages(struct ib_mr *mr, struct scatterlist *sgl, int sg_nents,
  2021. unsigned int *sg_offset_p, int (*set_page)(struct ib_mr *, u64))
  2022. {
  2023. struct scatterlist *sg;
  2024. u64 last_end_dma_addr = 0;
  2025. unsigned int sg_offset = sg_offset_p ? *sg_offset_p : 0;
  2026. unsigned int last_page_off = 0;
  2027. u64 page_mask = ~((u64)mr->page_size - 1);
  2028. int i, ret;
  2029. if (unlikely(sg_nents <= 0 || sg_offset > sg_dma_len(&sgl[0])))
  2030. return -EINVAL;
  2031. mr->iova = sg_dma_address(&sgl[0]) + sg_offset;
  2032. mr->length = 0;
  2033. for_each_sg(sgl, sg, sg_nents, i) {
  2034. u64 dma_addr = sg_dma_address(sg) + sg_offset;
  2035. u64 prev_addr = dma_addr;
  2036. unsigned int dma_len = sg_dma_len(sg) - sg_offset;
  2037. u64 end_dma_addr = dma_addr + dma_len;
  2038. u64 page_addr = dma_addr & page_mask;
  2039. /*
  2040. * For the second and later elements, check whether either the
  2041. * end of element i-1 or the start of element i is not aligned
  2042. * on a page boundary.
  2043. */
  2044. if (i && (last_page_off != 0 || page_addr != dma_addr)) {
  2045. /* Stop mapping if there is a gap. */
  2046. if (last_end_dma_addr != dma_addr)
  2047. break;
  2048. /*
  2049. * Coalesce this element with the last. If it is small
  2050. * enough just update mr->length. Otherwise start
  2051. * mapping from the next page.
  2052. */
  2053. goto next_page;
  2054. }
  2055. do {
  2056. ret = set_page(mr, page_addr);
  2057. if (unlikely(ret < 0)) {
  2058. sg_offset = prev_addr - sg_dma_address(sg);
  2059. mr->length += prev_addr - dma_addr;
  2060. if (sg_offset_p)
  2061. *sg_offset_p = sg_offset;
  2062. return i || sg_offset ? i : ret;
  2063. }
  2064. prev_addr = page_addr;
  2065. next_page:
  2066. page_addr += mr->page_size;
  2067. } while (page_addr < end_dma_addr);
  2068. mr->length += dma_len;
  2069. last_end_dma_addr = end_dma_addr;
  2070. last_page_off = end_dma_addr & ~page_mask;
  2071. sg_offset = 0;
  2072. }
  2073. if (sg_offset_p)
  2074. *sg_offset_p = 0;
  2075. return i;
  2076. }
  2077. EXPORT_SYMBOL(ib_sg_to_pages);
  2078. struct ib_drain_cqe {
  2079. struct ib_cqe cqe;
  2080. struct completion done;
  2081. };
  2082. static void ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
  2083. {
  2084. struct ib_drain_cqe *cqe = container_of(wc->wr_cqe, struct ib_drain_cqe,
  2085. cqe);
  2086. complete(&cqe->done);
  2087. }
  2088. /*
  2089. * Post a WR and block until its completion is reaped for the SQ.
  2090. */
  2091. static void __ib_drain_sq(struct ib_qp *qp)
  2092. {
  2093. struct ib_cq *cq = qp->send_cq;
  2094. struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
  2095. struct ib_drain_cqe sdrain;
  2096. struct ib_send_wr *bad_swr;
  2097. struct ib_rdma_wr swr = {
  2098. .wr = {
  2099. .next = NULL,
  2100. { .wr_cqe = &sdrain.cqe, },
  2101. .opcode = IB_WR_RDMA_WRITE,
  2102. },
  2103. };
  2104. int ret;
  2105. ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
  2106. if (ret) {
  2107. WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
  2108. return;
  2109. }
  2110. sdrain.cqe.done = ib_drain_qp_done;
  2111. init_completion(&sdrain.done);
  2112. ret = ib_post_send(qp, &swr.wr, &bad_swr);
  2113. if (ret) {
  2114. WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
  2115. return;
  2116. }
  2117. if (cq->poll_ctx == IB_POLL_DIRECT)
  2118. while (wait_for_completion_timeout(&sdrain.done, HZ / 10) <= 0)
  2119. ib_process_cq_direct(cq, -1);
  2120. else
  2121. wait_for_completion(&sdrain.done);
  2122. }
  2123. /*
  2124. * Post a WR and block until its completion is reaped for the RQ.
  2125. */
  2126. static void __ib_drain_rq(struct ib_qp *qp)
  2127. {
  2128. struct ib_cq *cq = qp->recv_cq;
  2129. struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
  2130. struct ib_drain_cqe rdrain;
  2131. struct ib_recv_wr rwr = {}, *bad_rwr;
  2132. int ret;
  2133. ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
  2134. if (ret) {
  2135. WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
  2136. return;
  2137. }
  2138. rwr.wr_cqe = &rdrain.cqe;
  2139. rdrain.cqe.done = ib_drain_qp_done;
  2140. init_completion(&rdrain.done);
  2141. ret = ib_post_recv(qp, &rwr, &bad_rwr);
  2142. if (ret) {
  2143. WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
  2144. return;
  2145. }
  2146. if (cq->poll_ctx == IB_POLL_DIRECT)
  2147. while (wait_for_completion_timeout(&rdrain.done, HZ / 10) <= 0)
  2148. ib_process_cq_direct(cq, -1);
  2149. else
  2150. wait_for_completion(&rdrain.done);
  2151. }
  2152. /**
  2153. * ib_drain_sq() - Block until all SQ CQEs have been consumed by the
  2154. * application.
  2155. * @qp: queue pair to drain
  2156. *
  2157. * If the device has a provider-specific drain function, then
  2158. * call that. Otherwise call the generic drain function
  2159. * __ib_drain_sq().
  2160. *
  2161. * The caller must:
  2162. *
  2163. * ensure there is room in the CQ and SQ for the drain work request and
  2164. * completion.
  2165. *
  2166. * allocate the CQ using ib_alloc_cq().
  2167. *
  2168. * ensure that there are no other contexts that are posting WRs concurrently.
  2169. * Otherwise the drain is not guaranteed.
  2170. */
  2171. void ib_drain_sq(struct ib_qp *qp)
  2172. {
  2173. if (qp->device->drain_sq)
  2174. qp->device->drain_sq(qp);
  2175. else
  2176. __ib_drain_sq(qp);
  2177. }
  2178. EXPORT_SYMBOL(ib_drain_sq);
  2179. /**
  2180. * ib_drain_rq() - Block until all RQ CQEs have been consumed by the
  2181. * application.
  2182. * @qp: queue pair to drain
  2183. *
  2184. * If the device has a provider-specific drain function, then
  2185. * call that. Otherwise call the generic drain function
  2186. * __ib_drain_rq().
  2187. *
  2188. * The caller must:
  2189. *
  2190. * ensure there is room in the CQ and RQ for the drain work request and
  2191. * completion.
  2192. *
  2193. * allocate the CQ using ib_alloc_cq().
  2194. *
  2195. * ensure that there are no other contexts that are posting WRs concurrently.
  2196. * Otherwise the drain is not guaranteed.
  2197. */
  2198. void ib_drain_rq(struct ib_qp *qp)
  2199. {
  2200. if (qp->device->drain_rq)
  2201. qp->device->drain_rq(qp);
  2202. else
  2203. __ib_drain_rq(qp);
  2204. }
  2205. EXPORT_SYMBOL(ib_drain_rq);
  2206. /**
  2207. * ib_drain_qp() - Block until all CQEs have been consumed by the
  2208. * application on both the RQ and SQ.
  2209. * @qp: queue pair to drain
  2210. *
  2211. * The caller must:
  2212. *
  2213. * ensure there is room in the CQ(s), SQ, and RQ for drain work requests
  2214. * and completions.
  2215. *
  2216. * allocate the CQs using ib_alloc_cq().
  2217. *
  2218. * ensure that there are no other contexts that are posting WRs concurrently.
  2219. * Otherwise the drain is not guaranteed.
  2220. */
  2221. void ib_drain_qp(struct ib_qp *qp)
  2222. {
  2223. ib_drain_sq(qp);
  2224. if (!qp->srq)
  2225. ib_drain_rq(qp);
  2226. }
  2227. EXPORT_SYMBOL(ib_drain_qp);