uverbs_cmd.c 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124
  1. /*
  2. * Copyright (c) 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005, 2006, 2007 Cisco Systems. All rights reserved.
  4. * Copyright (c) 2005 PathScale, Inc. All rights reserved.
  5. * Copyright (c) 2006 Mellanox Technologies. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * OpenIB.org BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. */
  35. #include <linux/file.h>
  36. #include <linux/fs.h>
  37. #include <linux/slab.h>
  38. #include <linux/sched.h>
  39. #include <linux/uaccess.h>
  40. #include <rdma/uverbs_types.h>
  41. #include <rdma/uverbs_std_types.h>
  42. #include "rdma_core.h"
  43. #include "uverbs.h"
  44. #include "core_priv.h"
  45. static struct ib_uverbs_completion_event_file *
  46. ib_uverbs_lookup_comp_file(int fd, struct ib_uverbs_file *ufile)
  47. {
  48. struct ib_uobject *uobj = uobj_get_read(UVERBS_OBJECT_COMP_CHANNEL,
  49. fd, ufile);
  50. if (IS_ERR(uobj))
  51. return (void *)uobj;
  52. uverbs_uobject_get(uobj);
  53. uobj_put_read(uobj);
  54. return container_of(uobj, struct ib_uverbs_completion_event_file,
  55. uobj);
  56. }
  57. ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
  58. struct ib_device *ib_dev,
  59. const char __user *buf,
  60. int in_len, int out_len)
  61. {
  62. struct ib_uverbs_get_context cmd;
  63. struct ib_uverbs_get_context_resp resp;
  64. struct ib_udata udata;
  65. struct ib_ucontext *ucontext;
  66. struct file *filp;
  67. struct ib_rdmacg_object cg_obj;
  68. int ret;
  69. if (out_len < sizeof resp)
  70. return -ENOSPC;
  71. if (copy_from_user(&cmd, buf, sizeof cmd))
  72. return -EFAULT;
  73. mutex_lock(&file->mutex);
  74. if (file->ucontext) {
  75. ret = -EINVAL;
  76. goto err;
  77. }
  78. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  79. u64_to_user_ptr(cmd.response) + sizeof(resp),
  80. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  81. out_len - sizeof(resp));
  82. ret = ib_rdmacg_try_charge(&cg_obj, ib_dev, RDMACG_RESOURCE_HCA_HANDLE);
  83. if (ret)
  84. goto err;
  85. ucontext = ib_dev->alloc_ucontext(ib_dev, &udata);
  86. if (IS_ERR(ucontext)) {
  87. ret = PTR_ERR(ucontext);
  88. goto err_alloc;
  89. }
  90. ucontext->device = ib_dev;
  91. ucontext->cg_obj = cg_obj;
  92. /* ufile is required when some objects are released */
  93. ucontext->ufile = file;
  94. rcu_read_lock();
  95. ucontext->tgid = get_task_pid(current->group_leader, PIDTYPE_PID);
  96. rcu_read_unlock();
  97. ucontext->closing = 0;
  98. ucontext->cleanup_retryable = false;
  99. #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
  100. ucontext->umem_tree = RB_ROOT_CACHED;
  101. init_rwsem(&ucontext->umem_rwsem);
  102. ucontext->odp_mrs_count = 0;
  103. INIT_LIST_HEAD(&ucontext->no_private_counters);
  104. if (!(ib_dev->attrs.device_cap_flags & IB_DEVICE_ON_DEMAND_PAGING))
  105. ucontext->invalidate_range = NULL;
  106. #endif
  107. resp.num_comp_vectors = file->device->num_comp_vectors;
  108. ret = get_unused_fd_flags(O_CLOEXEC);
  109. if (ret < 0)
  110. goto err_free;
  111. resp.async_fd = ret;
  112. filp = ib_uverbs_alloc_async_event_file(file, ib_dev);
  113. if (IS_ERR(filp)) {
  114. ret = PTR_ERR(filp);
  115. goto err_fd;
  116. }
  117. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
  118. ret = -EFAULT;
  119. goto err_file;
  120. }
  121. file->ucontext = ucontext;
  122. fd_install(resp.async_fd, filp);
  123. mutex_unlock(&file->mutex);
  124. return in_len;
  125. err_file:
  126. ib_uverbs_free_async_event_file(file);
  127. fput(filp);
  128. err_fd:
  129. put_unused_fd(resp.async_fd);
  130. err_free:
  131. put_pid(ucontext->tgid);
  132. ib_dev->dealloc_ucontext(ucontext);
  133. err_alloc:
  134. ib_rdmacg_uncharge(&cg_obj, ib_dev, RDMACG_RESOURCE_HCA_HANDLE);
  135. err:
  136. mutex_unlock(&file->mutex);
  137. return ret;
  138. }
  139. static void copy_query_dev_fields(struct ib_uverbs_file *file,
  140. struct ib_device *ib_dev,
  141. struct ib_uverbs_query_device_resp *resp,
  142. struct ib_device_attr *attr)
  143. {
  144. resp->fw_ver = attr->fw_ver;
  145. resp->node_guid = ib_dev->node_guid;
  146. resp->sys_image_guid = attr->sys_image_guid;
  147. resp->max_mr_size = attr->max_mr_size;
  148. resp->page_size_cap = attr->page_size_cap;
  149. resp->vendor_id = attr->vendor_id;
  150. resp->vendor_part_id = attr->vendor_part_id;
  151. resp->hw_ver = attr->hw_ver;
  152. resp->max_qp = attr->max_qp;
  153. resp->max_qp_wr = attr->max_qp_wr;
  154. resp->device_cap_flags = lower_32_bits(attr->device_cap_flags);
  155. resp->max_sge = min(attr->max_send_sge, attr->max_recv_sge);
  156. resp->max_sge_rd = attr->max_sge_rd;
  157. resp->max_cq = attr->max_cq;
  158. resp->max_cqe = attr->max_cqe;
  159. resp->max_mr = attr->max_mr;
  160. resp->max_pd = attr->max_pd;
  161. resp->max_qp_rd_atom = attr->max_qp_rd_atom;
  162. resp->max_ee_rd_atom = attr->max_ee_rd_atom;
  163. resp->max_res_rd_atom = attr->max_res_rd_atom;
  164. resp->max_qp_init_rd_atom = attr->max_qp_init_rd_atom;
  165. resp->max_ee_init_rd_atom = attr->max_ee_init_rd_atom;
  166. resp->atomic_cap = attr->atomic_cap;
  167. resp->max_ee = attr->max_ee;
  168. resp->max_rdd = attr->max_rdd;
  169. resp->max_mw = attr->max_mw;
  170. resp->max_raw_ipv6_qp = attr->max_raw_ipv6_qp;
  171. resp->max_raw_ethy_qp = attr->max_raw_ethy_qp;
  172. resp->max_mcast_grp = attr->max_mcast_grp;
  173. resp->max_mcast_qp_attach = attr->max_mcast_qp_attach;
  174. resp->max_total_mcast_qp_attach = attr->max_total_mcast_qp_attach;
  175. resp->max_ah = attr->max_ah;
  176. resp->max_fmr = attr->max_fmr;
  177. resp->max_map_per_fmr = attr->max_map_per_fmr;
  178. resp->max_srq = attr->max_srq;
  179. resp->max_srq_wr = attr->max_srq_wr;
  180. resp->max_srq_sge = attr->max_srq_sge;
  181. resp->max_pkeys = attr->max_pkeys;
  182. resp->local_ca_ack_delay = attr->local_ca_ack_delay;
  183. resp->phys_port_cnt = ib_dev->phys_port_cnt;
  184. }
  185. ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
  186. struct ib_device *ib_dev,
  187. const char __user *buf,
  188. int in_len, int out_len)
  189. {
  190. struct ib_uverbs_query_device cmd;
  191. struct ib_uverbs_query_device_resp resp;
  192. if (out_len < sizeof resp)
  193. return -ENOSPC;
  194. if (copy_from_user(&cmd, buf, sizeof cmd))
  195. return -EFAULT;
  196. memset(&resp, 0, sizeof resp);
  197. copy_query_dev_fields(file, ib_dev, &resp, &ib_dev->attrs);
  198. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
  199. return -EFAULT;
  200. return in_len;
  201. }
  202. /*
  203. * ib_uverbs_query_port_resp.port_cap_flags started out as just a copy of the
  204. * PortInfo CapabilityMask, but was extended with unique bits.
  205. */
  206. static u32 make_port_cap_flags(const struct ib_port_attr *attr)
  207. {
  208. u32 res;
  209. /* All IBA CapabilityMask bits are passed through here, except bit 26,
  210. * which is overridden with IP_BASED_GIDS. This is due to a historical
  211. * mistake in the implementation of IP_BASED_GIDS. Otherwise all other
  212. * bits match the IBA definition across all kernel versions.
  213. */
  214. res = attr->port_cap_flags & ~(u32)IB_UVERBS_PCF_IP_BASED_GIDS;
  215. if (attr->ip_gids)
  216. res |= IB_UVERBS_PCF_IP_BASED_GIDS;
  217. return res;
  218. }
  219. ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
  220. struct ib_device *ib_dev,
  221. const char __user *buf,
  222. int in_len, int out_len)
  223. {
  224. struct ib_uverbs_query_port cmd;
  225. struct ib_uverbs_query_port_resp resp;
  226. struct ib_port_attr attr;
  227. int ret;
  228. if (out_len < sizeof resp)
  229. return -ENOSPC;
  230. if (copy_from_user(&cmd, buf, sizeof cmd))
  231. return -EFAULT;
  232. ret = ib_query_port(ib_dev, cmd.port_num, &attr);
  233. if (ret)
  234. return ret;
  235. memset(&resp, 0, sizeof resp);
  236. resp.state = attr.state;
  237. resp.max_mtu = attr.max_mtu;
  238. resp.active_mtu = attr.active_mtu;
  239. resp.gid_tbl_len = attr.gid_tbl_len;
  240. resp.port_cap_flags = make_port_cap_flags(&attr);
  241. resp.max_msg_sz = attr.max_msg_sz;
  242. resp.bad_pkey_cntr = attr.bad_pkey_cntr;
  243. resp.qkey_viol_cntr = attr.qkey_viol_cntr;
  244. resp.pkey_tbl_len = attr.pkey_tbl_len;
  245. if (rdma_is_grh_required(ib_dev, cmd.port_num))
  246. resp.flags |= IB_UVERBS_QPF_GRH_REQUIRED;
  247. if (rdma_cap_opa_ah(ib_dev, cmd.port_num)) {
  248. resp.lid = OPA_TO_IB_UCAST_LID(attr.lid);
  249. resp.sm_lid = OPA_TO_IB_UCAST_LID(attr.sm_lid);
  250. } else {
  251. resp.lid = ib_lid_cpu16(attr.lid);
  252. resp.sm_lid = ib_lid_cpu16(attr.sm_lid);
  253. }
  254. resp.lmc = attr.lmc;
  255. resp.max_vl_num = attr.max_vl_num;
  256. resp.sm_sl = attr.sm_sl;
  257. resp.subnet_timeout = attr.subnet_timeout;
  258. resp.init_type_reply = attr.init_type_reply;
  259. resp.active_width = attr.active_width;
  260. resp.active_speed = attr.active_speed;
  261. resp.phys_state = attr.phys_state;
  262. resp.link_layer = rdma_port_get_link_layer(ib_dev,
  263. cmd.port_num);
  264. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
  265. return -EFAULT;
  266. return in_len;
  267. }
  268. ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
  269. struct ib_device *ib_dev,
  270. const char __user *buf,
  271. int in_len, int out_len)
  272. {
  273. struct ib_uverbs_alloc_pd cmd;
  274. struct ib_uverbs_alloc_pd_resp resp;
  275. struct ib_udata udata;
  276. struct ib_uobject *uobj;
  277. struct ib_pd *pd;
  278. int ret;
  279. if (out_len < sizeof resp)
  280. return -ENOSPC;
  281. if (copy_from_user(&cmd, buf, sizeof cmd))
  282. return -EFAULT;
  283. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  284. u64_to_user_ptr(cmd.response) + sizeof(resp),
  285. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  286. out_len - sizeof(resp));
  287. uobj = uobj_alloc(UVERBS_OBJECT_PD, file);
  288. if (IS_ERR(uobj))
  289. return PTR_ERR(uobj);
  290. pd = ib_dev->alloc_pd(ib_dev, file->ucontext, &udata);
  291. if (IS_ERR(pd)) {
  292. ret = PTR_ERR(pd);
  293. goto err;
  294. }
  295. pd->device = ib_dev;
  296. pd->uobject = uobj;
  297. pd->__internal_mr = NULL;
  298. atomic_set(&pd->usecnt, 0);
  299. uobj->object = pd;
  300. memset(&resp, 0, sizeof resp);
  301. resp.pd_handle = uobj->id;
  302. pd->res.type = RDMA_RESTRACK_PD;
  303. rdma_restrack_add(&pd->res);
  304. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
  305. ret = -EFAULT;
  306. goto err_copy;
  307. }
  308. uobj_alloc_commit(uobj);
  309. return in_len;
  310. err_copy:
  311. ib_dealloc_pd(pd);
  312. err:
  313. uobj_alloc_abort(uobj);
  314. return ret;
  315. }
  316. ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
  317. struct ib_device *ib_dev,
  318. const char __user *buf,
  319. int in_len, int out_len)
  320. {
  321. struct ib_uverbs_dealloc_pd cmd;
  322. if (copy_from_user(&cmd, buf, sizeof cmd))
  323. return -EFAULT;
  324. return uobj_perform_destroy(UVERBS_OBJECT_PD, cmd.pd_handle, file,
  325. in_len);
  326. }
  327. struct xrcd_table_entry {
  328. struct rb_node node;
  329. struct ib_xrcd *xrcd;
  330. struct inode *inode;
  331. };
  332. static int xrcd_table_insert(struct ib_uverbs_device *dev,
  333. struct inode *inode,
  334. struct ib_xrcd *xrcd)
  335. {
  336. struct xrcd_table_entry *entry, *scan;
  337. struct rb_node **p = &dev->xrcd_tree.rb_node;
  338. struct rb_node *parent = NULL;
  339. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  340. if (!entry)
  341. return -ENOMEM;
  342. entry->xrcd = xrcd;
  343. entry->inode = inode;
  344. while (*p) {
  345. parent = *p;
  346. scan = rb_entry(parent, struct xrcd_table_entry, node);
  347. if (inode < scan->inode) {
  348. p = &(*p)->rb_left;
  349. } else if (inode > scan->inode) {
  350. p = &(*p)->rb_right;
  351. } else {
  352. kfree(entry);
  353. return -EEXIST;
  354. }
  355. }
  356. rb_link_node(&entry->node, parent, p);
  357. rb_insert_color(&entry->node, &dev->xrcd_tree);
  358. igrab(inode);
  359. return 0;
  360. }
  361. static struct xrcd_table_entry *xrcd_table_search(struct ib_uverbs_device *dev,
  362. struct inode *inode)
  363. {
  364. struct xrcd_table_entry *entry;
  365. struct rb_node *p = dev->xrcd_tree.rb_node;
  366. while (p) {
  367. entry = rb_entry(p, struct xrcd_table_entry, node);
  368. if (inode < entry->inode)
  369. p = p->rb_left;
  370. else if (inode > entry->inode)
  371. p = p->rb_right;
  372. else
  373. return entry;
  374. }
  375. return NULL;
  376. }
  377. static struct ib_xrcd *find_xrcd(struct ib_uverbs_device *dev, struct inode *inode)
  378. {
  379. struct xrcd_table_entry *entry;
  380. entry = xrcd_table_search(dev, inode);
  381. if (!entry)
  382. return NULL;
  383. return entry->xrcd;
  384. }
  385. static void xrcd_table_delete(struct ib_uverbs_device *dev,
  386. struct inode *inode)
  387. {
  388. struct xrcd_table_entry *entry;
  389. entry = xrcd_table_search(dev, inode);
  390. if (entry) {
  391. iput(inode);
  392. rb_erase(&entry->node, &dev->xrcd_tree);
  393. kfree(entry);
  394. }
  395. }
  396. ssize_t ib_uverbs_open_xrcd(struct ib_uverbs_file *file,
  397. struct ib_device *ib_dev,
  398. const char __user *buf, int in_len,
  399. int out_len)
  400. {
  401. struct ib_uverbs_open_xrcd cmd;
  402. struct ib_uverbs_open_xrcd_resp resp;
  403. struct ib_udata udata;
  404. struct ib_uxrcd_object *obj;
  405. struct ib_xrcd *xrcd = NULL;
  406. struct fd f = {NULL, 0};
  407. struct inode *inode = NULL;
  408. int ret = 0;
  409. int new_xrcd = 0;
  410. if (out_len < sizeof resp)
  411. return -ENOSPC;
  412. if (copy_from_user(&cmd, buf, sizeof cmd))
  413. return -EFAULT;
  414. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  415. u64_to_user_ptr(cmd.response) + sizeof(resp),
  416. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  417. out_len - sizeof(resp));
  418. mutex_lock(&file->device->xrcd_tree_mutex);
  419. if (cmd.fd != -1) {
  420. /* search for file descriptor */
  421. f = fdget(cmd.fd);
  422. if (!f.file) {
  423. ret = -EBADF;
  424. goto err_tree_mutex_unlock;
  425. }
  426. inode = file_inode(f.file);
  427. xrcd = find_xrcd(file->device, inode);
  428. if (!xrcd && !(cmd.oflags & O_CREAT)) {
  429. /* no file descriptor. Need CREATE flag */
  430. ret = -EAGAIN;
  431. goto err_tree_mutex_unlock;
  432. }
  433. if (xrcd && cmd.oflags & O_EXCL) {
  434. ret = -EINVAL;
  435. goto err_tree_mutex_unlock;
  436. }
  437. }
  438. obj = (struct ib_uxrcd_object *)uobj_alloc(UVERBS_OBJECT_XRCD, file);
  439. if (IS_ERR(obj)) {
  440. ret = PTR_ERR(obj);
  441. goto err_tree_mutex_unlock;
  442. }
  443. if (!xrcd) {
  444. xrcd = ib_dev->alloc_xrcd(ib_dev, file->ucontext, &udata);
  445. if (IS_ERR(xrcd)) {
  446. ret = PTR_ERR(xrcd);
  447. goto err;
  448. }
  449. xrcd->inode = inode;
  450. xrcd->device = ib_dev;
  451. atomic_set(&xrcd->usecnt, 0);
  452. mutex_init(&xrcd->tgt_qp_mutex);
  453. INIT_LIST_HEAD(&xrcd->tgt_qp_list);
  454. new_xrcd = 1;
  455. }
  456. atomic_set(&obj->refcnt, 0);
  457. obj->uobject.object = xrcd;
  458. memset(&resp, 0, sizeof resp);
  459. resp.xrcd_handle = obj->uobject.id;
  460. if (inode) {
  461. if (new_xrcd) {
  462. /* create new inode/xrcd table entry */
  463. ret = xrcd_table_insert(file->device, inode, xrcd);
  464. if (ret)
  465. goto err_dealloc_xrcd;
  466. }
  467. atomic_inc(&xrcd->usecnt);
  468. }
  469. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
  470. ret = -EFAULT;
  471. goto err_copy;
  472. }
  473. if (f.file)
  474. fdput(f);
  475. mutex_unlock(&file->device->xrcd_tree_mutex);
  476. uobj_alloc_commit(&obj->uobject);
  477. return in_len;
  478. err_copy:
  479. if (inode) {
  480. if (new_xrcd)
  481. xrcd_table_delete(file->device, inode);
  482. atomic_dec(&xrcd->usecnt);
  483. }
  484. err_dealloc_xrcd:
  485. ib_dealloc_xrcd(xrcd);
  486. err:
  487. uobj_alloc_abort(&obj->uobject);
  488. err_tree_mutex_unlock:
  489. if (f.file)
  490. fdput(f);
  491. mutex_unlock(&file->device->xrcd_tree_mutex);
  492. return ret;
  493. }
  494. ssize_t ib_uverbs_close_xrcd(struct ib_uverbs_file *file,
  495. struct ib_device *ib_dev,
  496. const char __user *buf, int in_len,
  497. int out_len)
  498. {
  499. struct ib_uverbs_close_xrcd cmd;
  500. if (copy_from_user(&cmd, buf, sizeof cmd))
  501. return -EFAULT;
  502. return uobj_perform_destroy(UVERBS_OBJECT_XRCD, cmd.xrcd_handle, file,
  503. in_len);
  504. }
  505. int ib_uverbs_dealloc_xrcd(struct ib_uobject *uobject,
  506. struct ib_xrcd *xrcd,
  507. enum rdma_remove_reason why)
  508. {
  509. struct inode *inode;
  510. int ret;
  511. struct ib_uverbs_device *dev = uobject->context->ufile->device;
  512. inode = xrcd->inode;
  513. if (inode && !atomic_dec_and_test(&xrcd->usecnt))
  514. return 0;
  515. ret = ib_dealloc_xrcd(xrcd);
  516. if (ib_is_destroy_retryable(ret, why, uobject)) {
  517. atomic_inc(&xrcd->usecnt);
  518. return ret;
  519. }
  520. if (inode)
  521. xrcd_table_delete(dev, inode);
  522. return ret;
  523. }
  524. ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
  525. struct ib_device *ib_dev,
  526. const char __user *buf, int in_len,
  527. int out_len)
  528. {
  529. struct ib_uverbs_reg_mr cmd;
  530. struct ib_uverbs_reg_mr_resp resp;
  531. struct ib_udata udata;
  532. struct ib_uobject *uobj;
  533. struct ib_pd *pd;
  534. struct ib_mr *mr;
  535. int ret;
  536. if (out_len < sizeof resp)
  537. return -ENOSPC;
  538. if (copy_from_user(&cmd, buf, sizeof cmd))
  539. return -EFAULT;
  540. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  541. u64_to_user_ptr(cmd.response) + sizeof(resp),
  542. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  543. out_len - sizeof(resp));
  544. if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
  545. return -EINVAL;
  546. ret = ib_check_mr_access(cmd.access_flags);
  547. if (ret)
  548. return ret;
  549. uobj = uobj_alloc(UVERBS_OBJECT_MR, file);
  550. if (IS_ERR(uobj))
  551. return PTR_ERR(uobj);
  552. pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, file);
  553. if (!pd) {
  554. ret = -EINVAL;
  555. goto err_free;
  556. }
  557. if (cmd.access_flags & IB_ACCESS_ON_DEMAND) {
  558. if (!(pd->device->attrs.device_cap_flags &
  559. IB_DEVICE_ON_DEMAND_PAGING)) {
  560. pr_debug("ODP support not available\n");
  561. ret = -EINVAL;
  562. goto err_put;
  563. }
  564. }
  565. mr = pd->device->reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
  566. cmd.access_flags, &udata);
  567. if (IS_ERR(mr)) {
  568. ret = PTR_ERR(mr);
  569. goto err_put;
  570. }
  571. mr->device = pd->device;
  572. mr->pd = pd;
  573. mr->dm = NULL;
  574. mr->uobject = uobj;
  575. atomic_inc(&pd->usecnt);
  576. mr->res.type = RDMA_RESTRACK_MR;
  577. rdma_restrack_add(&mr->res);
  578. uobj->object = mr;
  579. memset(&resp, 0, sizeof resp);
  580. resp.lkey = mr->lkey;
  581. resp.rkey = mr->rkey;
  582. resp.mr_handle = uobj->id;
  583. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
  584. ret = -EFAULT;
  585. goto err_copy;
  586. }
  587. uobj_put_obj_read(pd);
  588. uobj_alloc_commit(uobj);
  589. return in_len;
  590. err_copy:
  591. ib_dereg_mr(mr);
  592. err_put:
  593. uobj_put_obj_read(pd);
  594. err_free:
  595. uobj_alloc_abort(uobj);
  596. return ret;
  597. }
  598. ssize_t ib_uverbs_rereg_mr(struct ib_uverbs_file *file,
  599. struct ib_device *ib_dev,
  600. const char __user *buf, int in_len,
  601. int out_len)
  602. {
  603. struct ib_uverbs_rereg_mr cmd;
  604. struct ib_uverbs_rereg_mr_resp resp;
  605. struct ib_udata udata;
  606. struct ib_pd *pd = NULL;
  607. struct ib_mr *mr;
  608. struct ib_pd *old_pd;
  609. int ret;
  610. struct ib_uobject *uobj;
  611. if (out_len < sizeof(resp))
  612. return -ENOSPC;
  613. if (copy_from_user(&cmd, buf, sizeof(cmd)))
  614. return -EFAULT;
  615. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  616. u64_to_user_ptr(cmd.response) + sizeof(resp),
  617. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  618. out_len - sizeof(resp));
  619. if (cmd.flags & ~IB_MR_REREG_SUPPORTED || !cmd.flags)
  620. return -EINVAL;
  621. if ((cmd.flags & IB_MR_REREG_TRANS) &&
  622. (!cmd.start || !cmd.hca_va || 0 >= cmd.length ||
  623. (cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK)))
  624. return -EINVAL;
  625. uobj = uobj_get_write(UVERBS_OBJECT_MR, cmd.mr_handle, file);
  626. if (IS_ERR(uobj))
  627. return PTR_ERR(uobj);
  628. mr = uobj->object;
  629. if (mr->dm) {
  630. ret = -EINVAL;
  631. goto put_uobjs;
  632. }
  633. if (cmd.flags & IB_MR_REREG_ACCESS) {
  634. ret = ib_check_mr_access(cmd.access_flags);
  635. if (ret)
  636. goto put_uobjs;
  637. }
  638. if (cmd.flags & IB_MR_REREG_PD) {
  639. pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle,
  640. file);
  641. if (!pd) {
  642. ret = -EINVAL;
  643. goto put_uobjs;
  644. }
  645. }
  646. old_pd = mr->pd;
  647. ret = mr->device->rereg_user_mr(mr, cmd.flags, cmd.start,
  648. cmd.length, cmd.hca_va,
  649. cmd.access_flags, pd, &udata);
  650. if (!ret) {
  651. if (cmd.flags & IB_MR_REREG_PD) {
  652. atomic_inc(&pd->usecnt);
  653. mr->pd = pd;
  654. atomic_dec(&old_pd->usecnt);
  655. }
  656. } else {
  657. goto put_uobj_pd;
  658. }
  659. memset(&resp, 0, sizeof(resp));
  660. resp.lkey = mr->lkey;
  661. resp.rkey = mr->rkey;
  662. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof(resp)))
  663. ret = -EFAULT;
  664. else
  665. ret = in_len;
  666. put_uobj_pd:
  667. if (cmd.flags & IB_MR_REREG_PD)
  668. uobj_put_obj_read(pd);
  669. put_uobjs:
  670. uobj_put_write(uobj);
  671. return ret;
  672. }
  673. ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
  674. struct ib_device *ib_dev,
  675. const char __user *buf, int in_len,
  676. int out_len)
  677. {
  678. struct ib_uverbs_dereg_mr cmd;
  679. if (copy_from_user(&cmd, buf, sizeof cmd))
  680. return -EFAULT;
  681. return uobj_perform_destroy(UVERBS_OBJECT_MR, cmd.mr_handle, file,
  682. in_len);
  683. }
  684. ssize_t ib_uverbs_alloc_mw(struct ib_uverbs_file *file,
  685. struct ib_device *ib_dev,
  686. const char __user *buf, int in_len,
  687. int out_len)
  688. {
  689. struct ib_uverbs_alloc_mw cmd;
  690. struct ib_uverbs_alloc_mw_resp resp;
  691. struct ib_uobject *uobj;
  692. struct ib_pd *pd;
  693. struct ib_mw *mw;
  694. struct ib_udata udata;
  695. int ret;
  696. if (out_len < sizeof(resp))
  697. return -ENOSPC;
  698. if (copy_from_user(&cmd, buf, sizeof(cmd)))
  699. return -EFAULT;
  700. uobj = uobj_alloc(UVERBS_OBJECT_MW, file);
  701. if (IS_ERR(uobj))
  702. return PTR_ERR(uobj);
  703. pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, file);
  704. if (!pd) {
  705. ret = -EINVAL;
  706. goto err_free;
  707. }
  708. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  709. u64_to_user_ptr(cmd.response) + sizeof(resp),
  710. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  711. out_len - sizeof(resp));
  712. mw = pd->device->alloc_mw(pd, cmd.mw_type, &udata);
  713. if (IS_ERR(mw)) {
  714. ret = PTR_ERR(mw);
  715. goto err_put;
  716. }
  717. mw->device = pd->device;
  718. mw->pd = pd;
  719. mw->uobject = uobj;
  720. atomic_inc(&pd->usecnt);
  721. uobj->object = mw;
  722. memset(&resp, 0, sizeof(resp));
  723. resp.rkey = mw->rkey;
  724. resp.mw_handle = uobj->id;
  725. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof(resp))) {
  726. ret = -EFAULT;
  727. goto err_copy;
  728. }
  729. uobj_put_obj_read(pd);
  730. uobj_alloc_commit(uobj);
  731. return in_len;
  732. err_copy:
  733. uverbs_dealloc_mw(mw);
  734. err_put:
  735. uobj_put_obj_read(pd);
  736. err_free:
  737. uobj_alloc_abort(uobj);
  738. return ret;
  739. }
  740. ssize_t ib_uverbs_dealloc_mw(struct ib_uverbs_file *file,
  741. struct ib_device *ib_dev,
  742. const char __user *buf, int in_len,
  743. int out_len)
  744. {
  745. struct ib_uverbs_dealloc_mw cmd;
  746. if (copy_from_user(&cmd, buf, sizeof(cmd)))
  747. return -EFAULT;
  748. return uobj_perform_destroy(UVERBS_OBJECT_MW, cmd.mw_handle, file,
  749. in_len);
  750. }
  751. ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
  752. struct ib_device *ib_dev,
  753. const char __user *buf, int in_len,
  754. int out_len)
  755. {
  756. struct ib_uverbs_create_comp_channel cmd;
  757. struct ib_uverbs_create_comp_channel_resp resp;
  758. struct ib_uobject *uobj;
  759. struct ib_uverbs_completion_event_file *ev_file;
  760. if (out_len < sizeof resp)
  761. return -ENOSPC;
  762. if (copy_from_user(&cmd, buf, sizeof cmd))
  763. return -EFAULT;
  764. uobj = uobj_alloc(UVERBS_OBJECT_COMP_CHANNEL, file);
  765. if (IS_ERR(uobj))
  766. return PTR_ERR(uobj);
  767. resp.fd = uobj->id;
  768. ev_file = container_of(uobj, struct ib_uverbs_completion_event_file,
  769. uobj);
  770. ib_uverbs_init_event_queue(&ev_file->ev_queue);
  771. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
  772. uobj_alloc_abort(uobj);
  773. return -EFAULT;
  774. }
  775. uobj_alloc_commit(uobj);
  776. return in_len;
  777. }
  778. static struct ib_ucq_object *create_cq(struct ib_uverbs_file *file,
  779. struct ib_device *ib_dev,
  780. struct ib_udata *ucore,
  781. struct ib_udata *uhw,
  782. struct ib_uverbs_ex_create_cq *cmd,
  783. size_t cmd_sz,
  784. int (*cb)(struct ib_uverbs_file *file,
  785. struct ib_ucq_object *obj,
  786. struct ib_uverbs_ex_create_cq_resp *resp,
  787. struct ib_udata *udata,
  788. void *context),
  789. void *context)
  790. {
  791. struct ib_ucq_object *obj;
  792. struct ib_uverbs_completion_event_file *ev_file = NULL;
  793. struct ib_cq *cq;
  794. int ret;
  795. struct ib_uverbs_ex_create_cq_resp resp;
  796. struct ib_cq_init_attr attr = {};
  797. if (!ib_dev->create_cq)
  798. return ERR_PTR(-EOPNOTSUPP);
  799. if (cmd->comp_vector >= file->device->num_comp_vectors)
  800. return ERR_PTR(-EINVAL);
  801. obj = (struct ib_ucq_object *)uobj_alloc(UVERBS_OBJECT_CQ, file);
  802. if (IS_ERR(obj))
  803. return obj;
  804. if (cmd->comp_channel >= 0) {
  805. ev_file = ib_uverbs_lookup_comp_file(cmd->comp_channel, file);
  806. if (IS_ERR(ev_file)) {
  807. ret = PTR_ERR(ev_file);
  808. goto err;
  809. }
  810. }
  811. obj->uobject.user_handle = cmd->user_handle;
  812. obj->comp_events_reported = 0;
  813. obj->async_events_reported = 0;
  814. INIT_LIST_HEAD(&obj->comp_list);
  815. INIT_LIST_HEAD(&obj->async_list);
  816. attr.cqe = cmd->cqe;
  817. attr.comp_vector = cmd->comp_vector;
  818. if (cmd_sz > offsetof(typeof(*cmd), flags) + sizeof(cmd->flags))
  819. attr.flags = cmd->flags;
  820. cq = ib_dev->create_cq(ib_dev, &attr, file->ucontext, uhw);
  821. if (IS_ERR(cq)) {
  822. ret = PTR_ERR(cq);
  823. goto err_file;
  824. }
  825. cq->device = ib_dev;
  826. cq->uobject = &obj->uobject;
  827. cq->comp_handler = ib_uverbs_comp_handler;
  828. cq->event_handler = ib_uverbs_cq_event_handler;
  829. cq->cq_context = ev_file ? &ev_file->ev_queue : NULL;
  830. atomic_set(&cq->usecnt, 0);
  831. obj->uobject.object = cq;
  832. memset(&resp, 0, sizeof resp);
  833. resp.base.cq_handle = obj->uobject.id;
  834. resp.base.cqe = cq->cqe;
  835. resp.response_length = offsetof(typeof(resp), response_length) +
  836. sizeof(resp.response_length);
  837. cq->res.type = RDMA_RESTRACK_CQ;
  838. rdma_restrack_add(&cq->res);
  839. ret = cb(file, obj, &resp, ucore, context);
  840. if (ret)
  841. goto err_cb;
  842. uobj_alloc_commit(&obj->uobject);
  843. return obj;
  844. err_cb:
  845. ib_destroy_cq(cq);
  846. err_file:
  847. if (ev_file)
  848. ib_uverbs_release_ucq(file, ev_file, obj);
  849. err:
  850. uobj_alloc_abort(&obj->uobject);
  851. return ERR_PTR(ret);
  852. }
  853. static int ib_uverbs_create_cq_cb(struct ib_uverbs_file *file,
  854. struct ib_ucq_object *obj,
  855. struct ib_uverbs_ex_create_cq_resp *resp,
  856. struct ib_udata *ucore, void *context)
  857. {
  858. if (ib_copy_to_udata(ucore, &resp->base, sizeof(resp->base)))
  859. return -EFAULT;
  860. return 0;
  861. }
  862. ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
  863. struct ib_device *ib_dev,
  864. const char __user *buf, int in_len,
  865. int out_len)
  866. {
  867. struct ib_uverbs_create_cq cmd;
  868. struct ib_uverbs_ex_create_cq cmd_ex;
  869. struct ib_uverbs_create_cq_resp resp;
  870. struct ib_udata ucore;
  871. struct ib_udata uhw;
  872. struct ib_ucq_object *obj;
  873. if (out_len < sizeof(resp))
  874. return -ENOSPC;
  875. if (copy_from_user(&cmd, buf, sizeof(cmd)))
  876. return -EFAULT;
  877. ib_uverbs_init_udata(&ucore, buf, u64_to_user_ptr(cmd.response),
  878. sizeof(cmd), sizeof(resp));
  879. ib_uverbs_init_udata(&uhw, buf + sizeof(cmd),
  880. u64_to_user_ptr(cmd.response) + sizeof(resp),
  881. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  882. out_len - sizeof(resp));
  883. memset(&cmd_ex, 0, sizeof(cmd_ex));
  884. cmd_ex.user_handle = cmd.user_handle;
  885. cmd_ex.cqe = cmd.cqe;
  886. cmd_ex.comp_vector = cmd.comp_vector;
  887. cmd_ex.comp_channel = cmd.comp_channel;
  888. obj = create_cq(file, ib_dev, &ucore, &uhw, &cmd_ex,
  889. offsetof(typeof(cmd_ex), comp_channel) +
  890. sizeof(cmd.comp_channel), ib_uverbs_create_cq_cb,
  891. NULL);
  892. if (IS_ERR(obj))
  893. return PTR_ERR(obj);
  894. return in_len;
  895. }
  896. static int ib_uverbs_ex_create_cq_cb(struct ib_uverbs_file *file,
  897. struct ib_ucq_object *obj,
  898. struct ib_uverbs_ex_create_cq_resp *resp,
  899. struct ib_udata *ucore, void *context)
  900. {
  901. if (ib_copy_to_udata(ucore, resp, resp->response_length))
  902. return -EFAULT;
  903. return 0;
  904. }
  905. int ib_uverbs_ex_create_cq(struct ib_uverbs_file *file,
  906. struct ib_device *ib_dev,
  907. struct ib_udata *ucore,
  908. struct ib_udata *uhw)
  909. {
  910. struct ib_uverbs_ex_create_cq_resp resp;
  911. struct ib_uverbs_ex_create_cq cmd;
  912. struct ib_ucq_object *obj;
  913. int err;
  914. if (ucore->inlen < sizeof(cmd))
  915. return -EINVAL;
  916. err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
  917. if (err)
  918. return err;
  919. if (cmd.comp_mask)
  920. return -EINVAL;
  921. if (cmd.reserved)
  922. return -EINVAL;
  923. if (ucore->outlen < (offsetof(typeof(resp), response_length) +
  924. sizeof(resp.response_length)))
  925. return -ENOSPC;
  926. obj = create_cq(file, ib_dev, ucore, uhw, &cmd,
  927. min(ucore->inlen, sizeof(cmd)),
  928. ib_uverbs_ex_create_cq_cb, NULL);
  929. return PTR_ERR_OR_ZERO(obj);
  930. }
  931. ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
  932. struct ib_device *ib_dev,
  933. const char __user *buf, int in_len,
  934. int out_len)
  935. {
  936. struct ib_uverbs_resize_cq cmd;
  937. struct ib_uverbs_resize_cq_resp resp = {};
  938. struct ib_udata udata;
  939. struct ib_cq *cq;
  940. int ret = -EINVAL;
  941. if (copy_from_user(&cmd, buf, sizeof cmd))
  942. return -EFAULT;
  943. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  944. u64_to_user_ptr(cmd.response) + sizeof(resp),
  945. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  946. out_len - sizeof(resp));
  947. cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, file);
  948. if (!cq)
  949. return -EINVAL;
  950. ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
  951. if (ret)
  952. goto out;
  953. resp.cqe = cq->cqe;
  954. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp.cqe))
  955. ret = -EFAULT;
  956. out:
  957. uobj_put_obj_read(cq);
  958. return ret ? ret : in_len;
  959. }
  960. static int copy_wc_to_user(struct ib_device *ib_dev, void __user *dest,
  961. struct ib_wc *wc)
  962. {
  963. struct ib_uverbs_wc tmp;
  964. tmp.wr_id = wc->wr_id;
  965. tmp.status = wc->status;
  966. tmp.opcode = wc->opcode;
  967. tmp.vendor_err = wc->vendor_err;
  968. tmp.byte_len = wc->byte_len;
  969. tmp.ex.imm_data = wc->ex.imm_data;
  970. tmp.qp_num = wc->qp->qp_num;
  971. tmp.src_qp = wc->src_qp;
  972. tmp.wc_flags = wc->wc_flags;
  973. tmp.pkey_index = wc->pkey_index;
  974. if (rdma_cap_opa_ah(ib_dev, wc->port_num))
  975. tmp.slid = OPA_TO_IB_UCAST_LID(wc->slid);
  976. else
  977. tmp.slid = ib_lid_cpu16(wc->slid);
  978. tmp.sl = wc->sl;
  979. tmp.dlid_path_bits = wc->dlid_path_bits;
  980. tmp.port_num = wc->port_num;
  981. tmp.reserved = 0;
  982. if (copy_to_user(dest, &tmp, sizeof tmp))
  983. return -EFAULT;
  984. return 0;
  985. }
  986. ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
  987. struct ib_device *ib_dev,
  988. const char __user *buf, int in_len,
  989. int out_len)
  990. {
  991. struct ib_uverbs_poll_cq cmd;
  992. struct ib_uverbs_poll_cq_resp resp;
  993. u8 __user *header_ptr;
  994. u8 __user *data_ptr;
  995. struct ib_cq *cq;
  996. struct ib_wc wc;
  997. int ret;
  998. if (copy_from_user(&cmd, buf, sizeof cmd))
  999. return -EFAULT;
  1000. cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, file);
  1001. if (!cq)
  1002. return -EINVAL;
  1003. /* we copy a struct ib_uverbs_poll_cq_resp to user space */
  1004. header_ptr = u64_to_user_ptr(cmd.response);
  1005. data_ptr = header_ptr + sizeof resp;
  1006. memset(&resp, 0, sizeof resp);
  1007. while (resp.count < cmd.ne) {
  1008. ret = ib_poll_cq(cq, 1, &wc);
  1009. if (ret < 0)
  1010. goto out_put;
  1011. if (!ret)
  1012. break;
  1013. ret = copy_wc_to_user(ib_dev, data_ptr, &wc);
  1014. if (ret)
  1015. goto out_put;
  1016. data_ptr += sizeof(struct ib_uverbs_wc);
  1017. ++resp.count;
  1018. }
  1019. if (copy_to_user(header_ptr, &resp, sizeof resp)) {
  1020. ret = -EFAULT;
  1021. goto out_put;
  1022. }
  1023. ret = in_len;
  1024. out_put:
  1025. uobj_put_obj_read(cq);
  1026. return ret;
  1027. }
  1028. ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
  1029. struct ib_device *ib_dev,
  1030. const char __user *buf, int in_len,
  1031. int out_len)
  1032. {
  1033. struct ib_uverbs_req_notify_cq cmd;
  1034. struct ib_cq *cq;
  1035. if (copy_from_user(&cmd, buf, sizeof cmd))
  1036. return -EFAULT;
  1037. cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, file);
  1038. if (!cq)
  1039. return -EINVAL;
  1040. ib_req_notify_cq(cq, cmd.solicited_only ?
  1041. IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
  1042. uobj_put_obj_read(cq);
  1043. return in_len;
  1044. }
  1045. ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
  1046. struct ib_device *ib_dev,
  1047. const char __user *buf, int in_len,
  1048. int out_len)
  1049. {
  1050. struct ib_uverbs_destroy_cq cmd;
  1051. struct ib_uverbs_destroy_cq_resp resp;
  1052. struct ib_uobject *uobj;
  1053. struct ib_cq *cq;
  1054. struct ib_ucq_object *obj;
  1055. int ret = -EINVAL;
  1056. if (copy_from_user(&cmd, buf, sizeof cmd))
  1057. return -EFAULT;
  1058. uobj = uobj_get_write(UVERBS_OBJECT_CQ, cmd.cq_handle, file);
  1059. if (IS_ERR(uobj))
  1060. return PTR_ERR(uobj);
  1061. /*
  1062. * Make sure we don't free the memory in remove_commit as we still
  1063. * needs the uobject memory to create the response.
  1064. */
  1065. uverbs_uobject_get(uobj);
  1066. cq = uobj->object;
  1067. obj = container_of(cq->uobject, struct ib_ucq_object, uobject);
  1068. memset(&resp, 0, sizeof(resp));
  1069. ret = uobj_remove_commit(uobj);
  1070. if (ret) {
  1071. uverbs_uobject_put(uobj);
  1072. return ret;
  1073. }
  1074. resp.comp_events_reported = obj->comp_events_reported;
  1075. resp.async_events_reported = obj->async_events_reported;
  1076. uverbs_uobject_put(uobj);
  1077. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
  1078. return -EFAULT;
  1079. return in_len;
  1080. }
  1081. static int create_qp(struct ib_uverbs_file *file,
  1082. struct ib_udata *ucore,
  1083. struct ib_udata *uhw,
  1084. struct ib_uverbs_ex_create_qp *cmd,
  1085. size_t cmd_sz,
  1086. int (*cb)(struct ib_uverbs_file *file,
  1087. struct ib_uverbs_ex_create_qp_resp *resp,
  1088. struct ib_udata *udata),
  1089. void *context)
  1090. {
  1091. struct ib_uqp_object *obj;
  1092. struct ib_device *device;
  1093. struct ib_pd *pd = NULL;
  1094. struct ib_xrcd *xrcd = NULL;
  1095. struct ib_uobject *xrcd_uobj = ERR_PTR(-ENOENT);
  1096. struct ib_cq *scq = NULL, *rcq = NULL;
  1097. struct ib_srq *srq = NULL;
  1098. struct ib_qp *qp;
  1099. char *buf;
  1100. struct ib_qp_init_attr attr = {};
  1101. struct ib_uverbs_ex_create_qp_resp resp;
  1102. int ret;
  1103. struct ib_rwq_ind_table *ind_tbl = NULL;
  1104. bool has_sq = true;
  1105. if (cmd->qp_type == IB_QPT_RAW_PACKET && !capable(CAP_NET_RAW))
  1106. return -EPERM;
  1107. obj = (struct ib_uqp_object *)uobj_alloc(UVERBS_OBJECT_QP, file);
  1108. if (IS_ERR(obj))
  1109. return PTR_ERR(obj);
  1110. obj->uxrcd = NULL;
  1111. obj->uevent.uobject.user_handle = cmd->user_handle;
  1112. mutex_init(&obj->mcast_lock);
  1113. if (cmd_sz >= offsetof(typeof(*cmd), rwq_ind_tbl_handle) +
  1114. sizeof(cmd->rwq_ind_tbl_handle) &&
  1115. (cmd->comp_mask & IB_UVERBS_CREATE_QP_MASK_IND_TABLE)) {
  1116. ind_tbl = uobj_get_obj_read(rwq_ind_table,
  1117. UVERBS_OBJECT_RWQ_IND_TBL,
  1118. cmd->rwq_ind_tbl_handle, file);
  1119. if (!ind_tbl) {
  1120. ret = -EINVAL;
  1121. goto err_put;
  1122. }
  1123. attr.rwq_ind_tbl = ind_tbl;
  1124. }
  1125. if (cmd_sz > sizeof(*cmd) &&
  1126. !ib_is_udata_cleared(ucore, sizeof(*cmd),
  1127. cmd_sz - sizeof(*cmd))) {
  1128. ret = -EOPNOTSUPP;
  1129. goto err_put;
  1130. }
  1131. if (ind_tbl && (cmd->max_recv_wr || cmd->max_recv_sge || cmd->is_srq)) {
  1132. ret = -EINVAL;
  1133. goto err_put;
  1134. }
  1135. if (ind_tbl && !cmd->max_send_wr)
  1136. has_sq = false;
  1137. if (cmd->qp_type == IB_QPT_XRC_TGT) {
  1138. xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd->pd_handle,
  1139. file);
  1140. if (IS_ERR(xrcd_uobj)) {
  1141. ret = -EINVAL;
  1142. goto err_put;
  1143. }
  1144. xrcd = (struct ib_xrcd *)xrcd_uobj->object;
  1145. if (!xrcd) {
  1146. ret = -EINVAL;
  1147. goto err_put;
  1148. }
  1149. device = xrcd->device;
  1150. } else {
  1151. if (cmd->qp_type == IB_QPT_XRC_INI) {
  1152. cmd->max_recv_wr = 0;
  1153. cmd->max_recv_sge = 0;
  1154. } else {
  1155. if (cmd->is_srq) {
  1156. srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ,
  1157. cmd->srq_handle, file);
  1158. if (!srq || srq->srq_type == IB_SRQT_XRC) {
  1159. ret = -EINVAL;
  1160. goto err_put;
  1161. }
  1162. }
  1163. if (!ind_tbl) {
  1164. if (cmd->recv_cq_handle != cmd->send_cq_handle) {
  1165. rcq = uobj_get_obj_read(
  1166. cq, UVERBS_OBJECT_CQ,
  1167. cmd->recv_cq_handle, file);
  1168. if (!rcq) {
  1169. ret = -EINVAL;
  1170. goto err_put;
  1171. }
  1172. }
  1173. }
  1174. }
  1175. if (has_sq)
  1176. scq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ,
  1177. cmd->send_cq_handle, file);
  1178. if (!ind_tbl)
  1179. rcq = rcq ?: scq;
  1180. pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd->pd_handle,
  1181. file);
  1182. if (!pd || (!scq && has_sq)) {
  1183. ret = -EINVAL;
  1184. goto err_put;
  1185. }
  1186. device = pd->device;
  1187. }
  1188. attr.event_handler = ib_uverbs_qp_event_handler;
  1189. attr.qp_context = file;
  1190. attr.send_cq = scq;
  1191. attr.recv_cq = rcq;
  1192. attr.srq = srq;
  1193. attr.xrcd = xrcd;
  1194. attr.sq_sig_type = cmd->sq_sig_all ? IB_SIGNAL_ALL_WR :
  1195. IB_SIGNAL_REQ_WR;
  1196. attr.qp_type = cmd->qp_type;
  1197. attr.create_flags = 0;
  1198. attr.cap.max_send_wr = cmd->max_send_wr;
  1199. attr.cap.max_recv_wr = cmd->max_recv_wr;
  1200. attr.cap.max_send_sge = cmd->max_send_sge;
  1201. attr.cap.max_recv_sge = cmd->max_recv_sge;
  1202. attr.cap.max_inline_data = cmd->max_inline_data;
  1203. obj->uevent.events_reported = 0;
  1204. INIT_LIST_HEAD(&obj->uevent.event_list);
  1205. INIT_LIST_HEAD(&obj->mcast_list);
  1206. if (cmd_sz >= offsetof(typeof(*cmd), create_flags) +
  1207. sizeof(cmd->create_flags))
  1208. attr.create_flags = cmd->create_flags;
  1209. if (attr.create_flags & ~(IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
  1210. IB_QP_CREATE_CROSS_CHANNEL |
  1211. IB_QP_CREATE_MANAGED_SEND |
  1212. IB_QP_CREATE_MANAGED_RECV |
  1213. IB_QP_CREATE_SCATTER_FCS |
  1214. IB_QP_CREATE_CVLAN_STRIPPING |
  1215. IB_QP_CREATE_SOURCE_QPN |
  1216. IB_QP_CREATE_PCI_WRITE_END_PADDING)) {
  1217. ret = -EINVAL;
  1218. goto err_put;
  1219. }
  1220. if (attr.create_flags & IB_QP_CREATE_SOURCE_QPN) {
  1221. if (!capable(CAP_NET_RAW)) {
  1222. ret = -EPERM;
  1223. goto err_put;
  1224. }
  1225. attr.source_qpn = cmd->source_qpn;
  1226. }
  1227. buf = (void *)cmd + sizeof(*cmd);
  1228. if (cmd_sz > sizeof(*cmd))
  1229. if (!(buf[0] == 0 && !memcmp(buf, buf + 1,
  1230. cmd_sz - sizeof(*cmd) - 1))) {
  1231. ret = -EINVAL;
  1232. goto err_put;
  1233. }
  1234. if (cmd->qp_type == IB_QPT_XRC_TGT)
  1235. qp = ib_create_qp(pd, &attr);
  1236. else
  1237. qp = _ib_create_qp(device, pd, &attr, uhw,
  1238. &obj->uevent.uobject);
  1239. if (IS_ERR(qp)) {
  1240. ret = PTR_ERR(qp);
  1241. goto err_put;
  1242. }
  1243. if (cmd->qp_type != IB_QPT_XRC_TGT) {
  1244. ret = ib_create_qp_security(qp, device);
  1245. if (ret)
  1246. goto err_cb;
  1247. qp->real_qp = qp;
  1248. qp->pd = pd;
  1249. qp->send_cq = attr.send_cq;
  1250. qp->recv_cq = attr.recv_cq;
  1251. qp->srq = attr.srq;
  1252. qp->rwq_ind_tbl = ind_tbl;
  1253. qp->event_handler = attr.event_handler;
  1254. qp->qp_context = attr.qp_context;
  1255. qp->qp_type = attr.qp_type;
  1256. atomic_set(&qp->usecnt, 0);
  1257. atomic_inc(&pd->usecnt);
  1258. qp->port = 0;
  1259. if (attr.send_cq)
  1260. atomic_inc(&attr.send_cq->usecnt);
  1261. if (attr.recv_cq)
  1262. atomic_inc(&attr.recv_cq->usecnt);
  1263. if (attr.srq)
  1264. atomic_inc(&attr.srq->usecnt);
  1265. if (ind_tbl)
  1266. atomic_inc(&ind_tbl->usecnt);
  1267. } else {
  1268. /* It is done in _ib_create_qp for other QP types */
  1269. qp->uobject = &obj->uevent.uobject;
  1270. }
  1271. obj->uevent.uobject.object = qp;
  1272. memset(&resp, 0, sizeof resp);
  1273. resp.base.qpn = qp->qp_num;
  1274. resp.base.qp_handle = obj->uevent.uobject.id;
  1275. resp.base.max_recv_sge = attr.cap.max_recv_sge;
  1276. resp.base.max_send_sge = attr.cap.max_send_sge;
  1277. resp.base.max_recv_wr = attr.cap.max_recv_wr;
  1278. resp.base.max_send_wr = attr.cap.max_send_wr;
  1279. resp.base.max_inline_data = attr.cap.max_inline_data;
  1280. resp.response_length = offsetof(typeof(resp), response_length) +
  1281. sizeof(resp.response_length);
  1282. ret = cb(file, &resp, ucore);
  1283. if (ret)
  1284. goto err_cb;
  1285. if (xrcd) {
  1286. obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object,
  1287. uobject);
  1288. atomic_inc(&obj->uxrcd->refcnt);
  1289. uobj_put_read(xrcd_uobj);
  1290. }
  1291. if (pd)
  1292. uobj_put_obj_read(pd);
  1293. if (scq)
  1294. uobj_put_obj_read(scq);
  1295. if (rcq && rcq != scq)
  1296. uobj_put_obj_read(rcq);
  1297. if (srq)
  1298. uobj_put_obj_read(srq);
  1299. if (ind_tbl)
  1300. uobj_put_obj_read(ind_tbl);
  1301. uobj_alloc_commit(&obj->uevent.uobject);
  1302. return 0;
  1303. err_cb:
  1304. ib_destroy_qp(qp);
  1305. err_put:
  1306. if (!IS_ERR(xrcd_uobj))
  1307. uobj_put_read(xrcd_uobj);
  1308. if (pd)
  1309. uobj_put_obj_read(pd);
  1310. if (scq)
  1311. uobj_put_obj_read(scq);
  1312. if (rcq && rcq != scq)
  1313. uobj_put_obj_read(rcq);
  1314. if (srq)
  1315. uobj_put_obj_read(srq);
  1316. if (ind_tbl)
  1317. uobj_put_obj_read(ind_tbl);
  1318. uobj_alloc_abort(&obj->uevent.uobject);
  1319. return ret;
  1320. }
  1321. static int ib_uverbs_create_qp_cb(struct ib_uverbs_file *file,
  1322. struct ib_uverbs_ex_create_qp_resp *resp,
  1323. struct ib_udata *ucore)
  1324. {
  1325. if (ib_copy_to_udata(ucore, &resp->base, sizeof(resp->base)))
  1326. return -EFAULT;
  1327. return 0;
  1328. }
  1329. ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
  1330. struct ib_device *ib_dev,
  1331. const char __user *buf, int in_len,
  1332. int out_len)
  1333. {
  1334. struct ib_uverbs_create_qp cmd;
  1335. struct ib_uverbs_ex_create_qp cmd_ex;
  1336. struct ib_udata ucore;
  1337. struct ib_udata uhw;
  1338. ssize_t resp_size = sizeof(struct ib_uverbs_create_qp_resp);
  1339. int err;
  1340. if (out_len < resp_size)
  1341. return -ENOSPC;
  1342. if (copy_from_user(&cmd, buf, sizeof(cmd)))
  1343. return -EFAULT;
  1344. ib_uverbs_init_udata(&ucore, buf, u64_to_user_ptr(cmd.response),
  1345. sizeof(cmd), resp_size);
  1346. ib_uverbs_init_udata(&uhw, buf + sizeof(cmd),
  1347. u64_to_user_ptr(cmd.response) + resp_size,
  1348. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  1349. out_len - resp_size);
  1350. memset(&cmd_ex, 0, sizeof(cmd_ex));
  1351. cmd_ex.user_handle = cmd.user_handle;
  1352. cmd_ex.pd_handle = cmd.pd_handle;
  1353. cmd_ex.send_cq_handle = cmd.send_cq_handle;
  1354. cmd_ex.recv_cq_handle = cmd.recv_cq_handle;
  1355. cmd_ex.srq_handle = cmd.srq_handle;
  1356. cmd_ex.max_send_wr = cmd.max_send_wr;
  1357. cmd_ex.max_recv_wr = cmd.max_recv_wr;
  1358. cmd_ex.max_send_sge = cmd.max_send_sge;
  1359. cmd_ex.max_recv_sge = cmd.max_recv_sge;
  1360. cmd_ex.max_inline_data = cmd.max_inline_data;
  1361. cmd_ex.sq_sig_all = cmd.sq_sig_all;
  1362. cmd_ex.qp_type = cmd.qp_type;
  1363. cmd_ex.is_srq = cmd.is_srq;
  1364. err = create_qp(file, &ucore, &uhw, &cmd_ex,
  1365. offsetof(typeof(cmd_ex), is_srq) +
  1366. sizeof(cmd.is_srq), ib_uverbs_create_qp_cb,
  1367. NULL);
  1368. if (err)
  1369. return err;
  1370. return in_len;
  1371. }
  1372. static int ib_uverbs_ex_create_qp_cb(struct ib_uverbs_file *file,
  1373. struct ib_uverbs_ex_create_qp_resp *resp,
  1374. struct ib_udata *ucore)
  1375. {
  1376. if (ib_copy_to_udata(ucore, resp, resp->response_length))
  1377. return -EFAULT;
  1378. return 0;
  1379. }
  1380. int ib_uverbs_ex_create_qp(struct ib_uverbs_file *file,
  1381. struct ib_device *ib_dev,
  1382. struct ib_udata *ucore,
  1383. struct ib_udata *uhw)
  1384. {
  1385. struct ib_uverbs_ex_create_qp_resp resp;
  1386. struct ib_uverbs_ex_create_qp cmd = {0};
  1387. int err;
  1388. if (ucore->inlen < (offsetof(typeof(cmd), comp_mask) +
  1389. sizeof(cmd.comp_mask)))
  1390. return -EINVAL;
  1391. err = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
  1392. if (err)
  1393. return err;
  1394. if (cmd.comp_mask & ~IB_UVERBS_CREATE_QP_SUP_COMP_MASK)
  1395. return -EINVAL;
  1396. if (cmd.reserved)
  1397. return -EINVAL;
  1398. if (ucore->outlen < (offsetof(typeof(resp), response_length) +
  1399. sizeof(resp.response_length)))
  1400. return -ENOSPC;
  1401. err = create_qp(file, ucore, uhw, &cmd,
  1402. min(ucore->inlen, sizeof(cmd)),
  1403. ib_uverbs_ex_create_qp_cb, NULL);
  1404. if (err)
  1405. return err;
  1406. return 0;
  1407. }
  1408. ssize_t ib_uverbs_open_qp(struct ib_uverbs_file *file,
  1409. struct ib_device *ib_dev,
  1410. const char __user *buf, int in_len, int out_len)
  1411. {
  1412. struct ib_uverbs_open_qp cmd;
  1413. struct ib_uverbs_create_qp_resp resp;
  1414. struct ib_udata udata;
  1415. struct ib_uqp_object *obj;
  1416. struct ib_xrcd *xrcd;
  1417. struct ib_uobject *uninitialized_var(xrcd_uobj);
  1418. struct ib_qp *qp;
  1419. struct ib_qp_open_attr attr;
  1420. int ret;
  1421. if (out_len < sizeof resp)
  1422. return -ENOSPC;
  1423. if (copy_from_user(&cmd, buf, sizeof cmd))
  1424. return -EFAULT;
  1425. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  1426. u64_to_user_ptr(cmd.response) + sizeof(resp),
  1427. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  1428. out_len - sizeof(resp));
  1429. obj = (struct ib_uqp_object *)uobj_alloc(UVERBS_OBJECT_QP, file);
  1430. if (IS_ERR(obj))
  1431. return PTR_ERR(obj);
  1432. xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd.pd_handle, file);
  1433. if (IS_ERR(xrcd_uobj)) {
  1434. ret = -EINVAL;
  1435. goto err_put;
  1436. }
  1437. xrcd = (struct ib_xrcd *)xrcd_uobj->object;
  1438. if (!xrcd) {
  1439. ret = -EINVAL;
  1440. goto err_xrcd;
  1441. }
  1442. attr.event_handler = ib_uverbs_qp_event_handler;
  1443. attr.qp_context = file;
  1444. attr.qp_num = cmd.qpn;
  1445. attr.qp_type = cmd.qp_type;
  1446. obj->uevent.events_reported = 0;
  1447. INIT_LIST_HEAD(&obj->uevent.event_list);
  1448. INIT_LIST_HEAD(&obj->mcast_list);
  1449. qp = ib_open_qp(xrcd, &attr);
  1450. if (IS_ERR(qp)) {
  1451. ret = PTR_ERR(qp);
  1452. goto err_xrcd;
  1453. }
  1454. obj->uevent.uobject.object = qp;
  1455. obj->uevent.uobject.user_handle = cmd.user_handle;
  1456. memset(&resp, 0, sizeof resp);
  1457. resp.qpn = qp->qp_num;
  1458. resp.qp_handle = obj->uevent.uobject.id;
  1459. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
  1460. ret = -EFAULT;
  1461. goto err_destroy;
  1462. }
  1463. obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
  1464. atomic_inc(&obj->uxrcd->refcnt);
  1465. qp->uobject = &obj->uevent.uobject;
  1466. uobj_put_read(xrcd_uobj);
  1467. uobj_alloc_commit(&obj->uevent.uobject);
  1468. return in_len;
  1469. err_destroy:
  1470. ib_destroy_qp(qp);
  1471. err_xrcd:
  1472. uobj_put_read(xrcd_uobj);
  1473. err_put:
  1474. uobj_alloc_abort(&obj->uevent.uobject);
  1475. return ret;
  1476. }
  1477. static void copy_ah_attr_to_uverbs(struct ib_uverbs_qp_dest *uverb_attr,
  1478. struct rdma_ah_attr *rdma_attr)
  1479. {
  1480. const struct ib_global_route *grh;
  1481. uverb_attr->dlid = rdma_ah_get_dlid(rdma_attr);
  1482. uverb_attr->sl = rdma_ah_get_sl(rdma_attr);
  1483. uverb_attr->src_path_bits = rdma_ah_get_path_bits(rdma_attr);
  1484. uverb_attr->static_rate = rdma_ah_get_static_rate(rdma_attr);
  1485. uverb_attr->is_global = !!(rdma_ah_get_ah_flags(rdma_attr) &
  1486. IB_AH_GRH);
  1487. if (uverb_attr->is_global) {
  1488. grh = rdma_ah_read_grh(rdma_attr);
  1489. memcpy(uverb_attr->dgid, grh->dgid.raw, 16);
  1490. uverb_attr->flow_label = grh->flow_label;
  1491. uverb_attr->sgid_index = grh->sgid_index;
  1492. uverb_attr->hop_limit = grh->hop_limit;
  1493. uverb_attr->traffic_class = grh->traffic_class;
  1494. }
  1495. uverb_attr->port_num = rdma_ah_get_port_num(rdma_attr);
  1496. }
  1497. ssize_t ib_uverbs_query_qp(struct ib_uverbs_file *file,
  1498. struct ib_device *ib_dev,
  1499. const char __user *buf, int in_len,
  1500. int out_len)
  1501. {
  1502. struct ib_uverbs_query_qp cmd;
  1503. struct ib_uverbs_query_qp_resp resp;
  1504. struct ib_qp *qp;
  1505. struct ib_qp_attr *attr;
  1506. struct ib_qp_init_attr *init_attr;
  1507. int ret;
  1508. if (copy_from_user(&cmd, buf, sizeof cmd))
  1509. return -EFAULT;
  1510. attr = kmalloc(sizeof *attr, GFP_KERNEL);
  1511. init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
  1512. if (!attr || !init_attr) {
  1513. ret = -ENOMEM;
  1514. goto out;
  1515. }
  1516. qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, file);
  1517. if (!qp) {
  1518. ret = -EINVAL;
  1519. goto out;
  1520. }
  1521. ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
  1522. uobj_put_obj_read(qp);
  1523. if (ret)
  1524. goto out;
  1525. memset(&resp, 0, sizeof resp);
  1526. resp.qp_state = attr->qp_state;
  1527. resp.cur_qp_state = attr->cur_qp_state;
  1528. resp.path_mtu = attr->path_mtu;
  1529. resp.path_mig_state = attr->path_mig_state;
  1530. resp.qkey = attr->qkey;
  1531. resp.rq_psn = attr->rq_psn;
  1532. resp.sq_psn = attr->sq_psn;
  1533. resp.dest_qp_num = attr->dest_qp_num;
  1534. resp.qp_access_flags = attr->qp_access_flags;
  1535. resp.pkey_index = attr->pkey_index;
  1536. resp.alt_pkey_index = attr->alt_pkey_index;
  1537. resp.sq_draining = attr->sq_draining;
  1538. resp.max_rd_atomic = attr->max_rd_atomic;
  1539. resp.max_dest_rd_atomic = attr->max_dest_rd_atomic;
  1540. resp.min_rnr_timer = attr->min_rnr_timer;
  1541. resp.port_num = attr->port_num;
  1542. resp.timeout = attr->timeout;
  1543. resp.retry_cnt = attr->retry_cnt;
  1544. resp.rnr_retry = attr->rnr_retry;
  1545. resp.alt_port_num = attr->alt_port_num;
  1546. resp.alt_timeout = attr->alt_timeout;
  1547. copy_ah_attr_to_uverbs(&resp.dest, &attr->ah_attr);
  1548. copy_ah_attr_to_uverbs(&resp.alt_dest, &attr->alt_ah_attr);
  1549. resp.max_send_wr = init_attr->cap.max_send_wr;
  1550. resp.max_recv_wr = init_attr->cap.max_recv_wr;
  1551. resp.max_send_sge = init_attr->cap.max_send_sge;
  1552. resp.max_recv_sge = init_attr->cap.max_recv_sge;
  1553. resp.max_inline_data = init_attr->cap.max_inline_data;
  1554. resp.sq_sig_all = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
  1555. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
  1556. ret = -EFAULT;
  1557. out:
  1558. kfree(attr);
  1559. kfree(init_attr);
  1560. return ret ? ret : in_len;
  1561. }
  1562. /* Remove ignored fields set in the attribute mask */
  1563. static int modify_qp_mask(enum ib_qp_type qp_type, int mask)
  1564. {
  1565. switch (qp_type) {
  1566. case IB_QPT_XRC_INI:
  1567. return mask & ~(IB_QP_MAX_DEST_RD_ATOMIC | IB_QP_MIN_RNR_TIMER);
  1568. case IB_QPT_XRC_TGT:
  1569. return mask & ~(IB_QP_MAX_QP_RD_ATOMIC | IB_QP_RETRY_CNT |
  1570. IB_QP_RNR_RETRY);
  1571. default:
  1572. return mask;
  1573. }
  1574. }
  1575. static void copy_ah_attr_from_uverbs(struct ib_device *dev,
  1576. struct rdma_ah_attr *rdma_attr,
  1577. struct ib_uverbs_qp_dest *uverb_attr)
  1578. {
  1579. rdma_attr->type = rdma_ah_find_type(dev, uverb_attr->port_num);
  1580. if (uverb_attr->is_global) {
  1581. rdma_ah_set_grh(rdma_attr, NULL,
  1582. uverb_attr->flow_label,
  1583. uverb_attr->sgid_index,
  1584. uverb_attr->hop_limit,
  1585. uverb_attr->traffic_class);
  1586. rdma_ah_set_dgid_raw(rdma_attr, uverb_attr->dgid);
  1587. } else {
  1588. rdma_ah_set_ah_flags(rdma_attr, 0);
  1589. }
  1590. rdma_ah_set_dlid(rdma_attr, uverb_attr->dlid);
  1591. rdma_ah_set_sl(rdma_attr, uverb_attr->sl);
  1592. rdma_ah_set_path_bits(rdma_attr, uverb_attr->src_path_bits);
  1593. rdma_ah_set_static_rate(rdma_attr, uverb_attr->static_rate);
  1594. rdma_ah_set_port_num(rdma_attr, uverb_attr->port_num);
  1595. rdma_ah_set_make_grd(rdma_attr, false);
  1596. }
  1597. static int modify_qp(struct ib_uverbs_file *file,
  1598. struct ib_uverbs_ex_modify_qp *cmd, struct ib_udata *udata)
  1599. {
  1600. struct ib_qp_attr *attr;
  1601. struct ib_qp *qp;
  1602. int ret;
  1603. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  1604. if (!attr)
  1605. return -ENOMEM;
  1606. qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd->base.qp_handle, file);
  1607. if (!qp) {
  1608. ret = -EINVAL;
  1609. goto out;
  1610. }
  1611. if ((cmd->base.attr_mask & IB_QP_PORT) &&
  1612. !rdma_is_port_valid(qp->device, cmd->base.port_num)) {
  1613. ret = -EINVAL;
  1614. goto release_qp;
  1615. }
  1616. if ((cmd->base.attr_mask & IB_QP_AV) &&
  1617. !rdma_is_port_valid(qp->device, cmd->base.dest.port_num)) {
  1618. ret = -EINVAL;
  1619. goto release_qp;
  1620. }
  1621. if ((cmd->base.attr_mask & IB_QP_ALT_PATH) &&
  1622. (!rdma_is_port_valid(qp->device, cmd->base.alt_port_num) ||
  1623. !rdma_is_port_valid(qp->device, cmd->base.alt_dest.port_num))) {
  1624. ret = -EINVAL;
  1625. goto release_qp;
  1626. }
  1627. if ((cmd->base.attr_mask & IB_QP_CUR_STATE &&
  1628. cmd->base.cur_qp_state > IB_QPS_ERR) ||
  1629. cmd->base.qp_state > IB_QPS_ERR) {
  1630. ret = -EINVAL;
  1631. goto release_qp;
  1632. }
  1633. attr->qp_state = cmd->base.qp_state;
  1634. attr->cur_qp_state = cmd->base.cur_qp_state;
  1635. attr->path_mtu = cmd->base.path_mtu;
  1636. attr->path_mig_state = cmd->base.path_mig_state;
  1637. attr->qkey = cmd->base.qkey;
  1638. attr->rq_psn = cmd->base.rq_psn;
  1639. attr->sq_psn = cmd->base.sq_psn;
  1640. attr->dest_qp_num = cmd->base.dest_qp_num;
  1641. attr->qp_access_flags = cmd->base.qp_access_flags;
  1642. attr->pkey_index = cmd->base.pkey_index;
  1643. attr->alt_pkey_index = cmd->base.alt_pkey_index;
  1644. attr->en_sqd_async_notify = cmd->base.en_sqd_async_notify;
  1645. attr->max_rd_atomic = cmd->base.max_rd_atomic;
  1646. attr->max_dest_rd_atomic = cmd->base.max_dest_rd_atomic;
  1647. attr->min_rnr_timer = cmd->base.min_rnr_timer;
  1648. attr->port_num = cmd->base.port_num;
  1649. attr->timeout = cmd->base.timeout;
  1650. attr->retry_cnt = cmd->base.retry_cnt;
  1651. attr->rnr_retry = cmd->base.rnr_retry;
  1652. attr->alt_port_num = cmd->base.alt_port_num;
  1653. attr->alt_timeout = cmd->base.alt_timeout;
  1654. attr->rate_limit = cmd->rate_limit;
  1655. if (cmd->base.attr_mask & IB_QP_AV)
  1656. copy_ah_attr_from_uverbs(qp->device, &attr->ah_attr,
  1657. &cmd->base.dest);
  1658. if (cmd->base.attr_mask & IB_QP_ALT_PATH)
  1659. copy_ah_attr_from_uverbs(qp->device, &attr->alt_ah_attr,
  1660. &cmd->base.alt_dest);
  1661. ret = ib_modify_qp_with_udata(qp, attr,
  1662. modify_qp_mask(qp->qp_type,
  1663. cmd->base.attr_mask),
  1664. udata);
  1665. release_qp:
  1666. uobj_put_obj_read(qp);
  1667. out:
  1668. kfree(attr);
  1669. return ret;
  1670. }
  1671. ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
  1672. struct ib_device *ib_dev,
  1673. const char __user *buf, int in_len,
  1674. int out_len)
  1675. {
  1676. struct ib_uverbs_ex_modify_qp cmd = {};
  1677. struct ib_udata udata;
  1678. int ret;
  1679. if (copy_from_user(&cmd.base, buf, sizeof(cmd.base)))
  1680. return -EFAULT;
  1681. if (cmd.base.attr_mask &
  1682. ~((IB_USER_LEGACY_LAST_QP_ATTR_MASK << 1) - 1))
  1683. return -EOPNOTSUPP;
  1684. ib_uverbs_init_udata(&udata, buf + sizeof(cmd.base), NULL,
  1685. in_len - sizeof(cmd.base) - sizeof(struct ib_uverbs_cmd_hdr),
  1686. out_len);
  1687. ret = modify_qp(file, &cmd, &udata);
  1688. if (ret)
  1689. return ret;
  1690. return in_len;
  1691. }
  1692. int ib_uverbs_ex_modify_qp(struct ib_uverbs_file *file,
  1693. struct ib_device *ib_dev,
  1694. struct ib_udata *ucore,
  1695. struct ib_udata *uhw)
  1696. {
  1697. struct ib_uverbs_ex_modify_qp cmd = {};
  1698. int ret;
  1699. /*
  1700. * Last bit is reserved for extending the attr_mask by
  1701. * using another field.
  1702. */
  1703. BUILD_BUG_ON(IB_USER_LAST_QP_ATTR_MASK == (1 << 31));
  1704. if (ucore->inlen < sizeof(cmd.base))
  1705. return -EINVAL;
  1706. ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
  1707. if (ret)
  1708. return ret;
  1709. if (cmd.base.attr_mask &
  1710. ~((IB_USER_LAST_QP_ATTR_MASK << 1) - 1))
  1711. return -EOPNOTSUPP;
  1712. if (ucore->inlen > sizeof(cmd)) {
  1713. if (!ib_is_udata_cleared(ucore, sizeof(cmd),
  1714. ucore->inlen - sizeof(cmd)))
  1715. return -EOPNOTSUPP;
  1716. }
  1717. ret = modify_qp(file, &cmd, uhw);
  1718. return ret;
  1719. }
  1720. ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
  1721. struct ib_device *ib_dev,
  1722. const char __user *buf, int in_len,
  1723. int out_len)
  1724. {
  1725. struct ib_uverbs_destroy_qp cmd;
  1726. struct ib_uverbs_destroy_qp_resp resp;
  1727. struct ib_uobject *uobj;
  1728. struct ib_uqp_object *obj;
  1729. int ret = -EINVAL;
  1730. if (copy_from_user(&cmd, buf, sizeof cmd))
  1731. return -EFAULT;
  1732. memset(&resp, 0, sizeof resp);
  1733. uobj = uobj_get_write(UVERBS_OBJECT_QP, cmd.qp_handle, file);
  1734. if (IS_ERR(uobj))
  1735. return PTR_ERR(uobj);
  1736. obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
  1737. /*
  1738. * Make sure we don't free the memory in remove_commit as we still
  1739. * needs the uobject memory to create the response.
  1740. */
  1741. uverbs_uobject_get(uobj);
  1742. ret = uobj_remove_commit(uobj);
  1743. if (ret) {
  1744. uverbs_uobject_put(uobj);
  1745. return ret;
  1746. }
  1747. resp.events_reported = obj->uevent.events_reported;
  1748. uverbs_uobject_put(uobj);
  1749. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
  1750. return -EFAULT;
  1751. return in_len;
  1752. }
  1753. static void *alloc_wr(size_t wr_size, __u32 num_sge)
  1754. {
  1755. if (num_sge >= (U32_MAX - ALIGN(wr_size, sizeof (struct ib_sge))) /
  1756. sizeof (struct ib_sge))
  1757. return NULL;
  1758. return kmalloc(ALIGN(wr_size, sizeof (struct ib_sge)) +
  1759. num_sge * sizeof (struct ib_sge), GFP_KERNEL);
  1760. }
  1761. ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
  1762. struct ib_device *ib_dev,
  1763. const char __user *buf, int in_len,
  1764. int out_len)
  1765. {
  1766. struct ib_uverbs_post_send cmd;
  1767. struct ib_uverbs_post_send_resp resp;
  1768. struct ib_uverbs_send_wr *user_wr;
  1769. struct ib_send_wr *wr = NULL, *last, *next, *bad_wr;
  1770. struct ib_qp *qp;
  1771. int i, sg_ind;
  1772. int is_ud;
  1773. ssize_t ret = -EINVAL;
  1774. size_t next_size;
  1775. if (copy_from_user(&cmd, buf, sizeof cmd))
  1776. return -EFAULT;
  1777. if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
  1778. cmd.sge_count * sizeof (struct ib_uverbs_sge))
  1779. return -EINVAL;
  1780. if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
  1781. return -EINVAL;
  1782. user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
  1783. if (!user_wr)
  1784. return -ENOMEM;
  1785. qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, file);
  1786. if (!qp)
  1787. goto out;
  1788. is_ud = qp->qp_type == IB_QPT_UD;
  1789. sg_ind = 0;
  1790. last = NULL;
  1791. for (i = 0; i < cmd.wr_count; ++i) {
  1792. if (copy_from_user(user_wr,
  1793. buf + sizeof cmd + i * cmd.wqe_size,
  1794. cmd.wqe_size)) {
  1795. ret = -EFAULT;
  1796. goto out_put;
  1797. }
  1798. if (user_wr->num_sge + sg_ind > cmd.sge_count) {
  1799. ret = -EINVAL;
  1800. goto out_put;
  1801. }
  1802. if (is_ud) {
  1803. struct ib_ud_wr *ud;
  1804. if (user_wr->opcode != IB_WR_SEND &&
  1805. user_wr->opcode != IB_WR_SEND_WITH_IMM) {
  1806. ret = -EINVAL;
  1807. goto out_put;
  1808. }
  1809. next_size = sizeof(*ud);
  1810. ud = alloc_wr(next_size, user_wr->num_sge);
  1811. if (!ud) {
  1812. ret = -ENOMEM;
  1813. goto out_put;
  1814. }
  1815. ud->ah = uobj_get_obj_read(ah, UVERBS_OBJECT_AH,
  1816. user_wr->wr.ud.ah, file);
  1817. if (!ud->ah) {
  1818. kfree(ud);
  1819. ret = -EINVAL;
  1820. goto out_put;
  1821. }
  1822. ud->remote_qpn = user_wr->wr.ud.remote_qpn;
  1823. ud->remote_qkey = user_wr->wr.ud.remote_qkey;
  1824. next = &ud->wr;
  1825. } else if (user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
  1826. user_wr->opcode == IB_WR_RDMA_WRITE ||
  1827. user_wr->opcode == IB_WR_RDMA_READ) {
  1828. struct ib_rdma_wr *rdma;
  1829. next_size = sizeof(*rdma);
  1830. rdma = alloc_wr(next_size, user_wr->num_sge);
  1831. if (!rdma) {
  1832. ret = -ENOMEM;
  1833. goto out_put;
  1834. }
  1835. rdma->remote_addr = user_wr->wr.rdma.remote_addr;
  1836. rdma->rkey = user_wr->wr.rdma.rkey;
  1837. next = &rdma->wr;
  1838. } else if (user_wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
  1839. user_wr->opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
  1840. struct ib_atomic_wr *atomic;
  1841. next_size = sizeof(*atomic);
  1842. atomic = alloc_wr(next_size, user_wr->num_sge);
  1843. if (!atomic) {
  1844. ret = -ENOMEM;
  1845. goto out_put;
  1846. }
  1847. atomic->remote_addr = user_wr->wr.atomic.remote_addr;
  1848. atomic->compare_add = user_wr->wr.atomic.compare_add;
  1849. atomic->swap = user_wr->wr.atomic.swap;
  1850. atomic->rkey = user_wr->wr.atomic.rkey;
  1851. next = &atomic->wr;
  1852. } else if (user_wr->opcode == IB_WR_SEND ||
  1853. user_wr->opcode == IB_WR_SEND_WITH_IMM ||
  1854. user_wr->opcode == IB_WR_SEND_WITH_INV) {
  1855. next_size = sizeof(*next);
  1856. next = alloc_wr(next_size, user_wr->num_sge);
  1857. if (!next) {
  1858. ret = -ENOMEM;
  1859. goto out_put;
  1860. }
  1861. } else {
  1862. ret = -EINVAL;
  1863. goto out_put;
  1864. }
  1865. if (user_wr->opcode == IB_WR_SEND_WITH_IMM ||
  1866. user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM) {
  1867. next->ex.imm_data =
  1868. (__be32 __force) user_wr->ex.imm_data;
  1869. } else if (user_wr->opcode == IB_WR_SEND_WITH_INV) {
  1870. next->ex.invalidate_rkey = user_wr->ex.invalidate_rkey;
  1871. }
  1872. if (!last)
  1873. wr = next;
  1874. else
  1875. last->next = next;
  1876. last = next;
  1877. next->next = NULL;
  1878. next->wr_id = user_wr->wr_id;
  1879. next->num_sge = user_wr->num_sge;
  1880. next->opcode = user_wr->opcode;
  1881. next->send_flags = user_wr->send_flags;
  1882. if (next->num_sge) {
  1883. next->sg_list = (void *) next +
  1884. ALIGN(next_size, sizeof(struct ib_sge));
  1885. if (copy_from_user(next->sg_list,
  1886. buf + sizeof cmd +
  1887. cmd.wr_count * cmd.wqe_size +
  1888. sg_ind * sizeof (struct ib_sge),
  1889. next->num_sge * sizeof (struct ib_sge))) {
  1890. ret = -EFAULT;
  1891. goto out_put;
  1892. }
  1893. sg_ind += next->num_sge;
  1894. } else
  1895. next->sg_list = NULL;
  1896. }
  1897. resp.bad_wr = 0;
  1898. ret = qp->device->post_send(qp->real_qp, wr, &bad_wr);
  1899. if (ret)
  1900. for (next = wr; next; next = next->next) {
  1901. ++resp.bad_wr;
  1902. if (next == bad_wr)
  1903. break;
  1904. }
  1905. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
  1906. ret = -EFAULT;
  1907. out_put:
  1908. uobj_put_obj_read(qp);
  1909. while (wr) {
  1910. if (is_ud && ud_wr(wr)->ah)
  1911. uobj_put_obj_read(ud_wr(wr)->ah);
  1912. next = wr->next;
  1913. kfree(wr);
  1914. wr = next;
  1915. }
  1916. out:
  1917. kfree(user_wr);
  1918. return ret ? ret : in_len;
  1919. }
  1920. static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
  1921. int in_len,
  1922. u32 wr_count,
  1923. u32 sge_count,
  1924. u32 wqe_size)
  1925. {
  1926. struct ib_uverbs_recv_wr *user_wr;
  1927. struct ib_recv_wr *wr = NULL, *last, *next;
  1928. int sg_ind;
  1929. int i;
  1930. int ret;
  1931. if (in_len < wqe_size * wr_count +
  1932. sge_count * sizeof (struct ib_uverbs_sge))
  1933. return ERR_PTR(-EINVAL);
  1934. if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
  1935. return ERR_PTR(-EINVAL);
  1936. user_wr = kmalloc(wqe_size, GFP_KERNEL);
  1937. if (!user_wr)
  1938. return ERR_PTR(-ENOMEM);
  1939. sg_ind = 0;
  1940. last = NULL;
  1941. for (i = 0; i < wr_count; ++i) {
  1942. if (copy_from_user(user_wr, buf + i * wqe_size,
  1943. wqe_size)) {
  1944. ret = -EFAULT;
  1945. goto err;
  1946. }
  1947. if (user_wr->num_sge + sg_ind > sge_count) {
  1948. ret = -EINVAL;
  1949. goto err;
  1950. }
  1951. if (user_wr->num_sge >=
  1952. (U32_MAX - ALIGN(sizeof *next, sizeof (struct ib_sge))) /
  1953. sizeof (struct ib_sge)) {
  1954. ret = -EINVAL;
  1955. goto err;
  1956. }
  1957. next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
  1958. user_wr->num_sge * sizeof (struct ib_sge),
  1959. GFP_KERNEL);
  1960. if (!next) {
  1961. ret = -ENOMEM;
  1962. goto err;
  1963. }
  1964. if (!last)
  1965. wr = next;
  1966. else
  1967. last->next = next;
  1968. last = next;
  1969. next->next = NULL;
  1970. next->wr_id = user_wr->wr_id;
  1971. next->num_sge = user_wr->num_sge;
  1972. if (next->num_sge) {
  1973. next->sg_list = (void *) next +
  1974. ALIGN(sizeof *next, sizeof (struct ib_sge));
  1975. if (copy_from_user(next->sg_list,
  1976. buf + wr_count * wqe_size +
  1977. sg_ind * sizeof (struct ib_sge),
  1978. next->num_sge * sizeof (struct ib_sge))) {
  1979. ret = -EFAULT;
  1980. goto err;
  1981. }
  1982. sg_ind += next->num_sge;
  1983. } else
  1984. next->sg_list = NULL;
  1985. }
  1986. kfree(user_wr);
  1987. return wr;
  1988. err:
  1989. kfree(user_wr);
  1990. while (wr) {
  1991. next = wr->next;
  1992. kfree(wr);
  1993. wr = next;
  1994. }
  1995. return ERR_PTR(ret);
  1996. }
  1997. ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
  1998. struct ib_device *ib_dev,
  1999. const char __user *buf, int in_len,
  2000. int out_len)
  2001. {
  2002. struct ib_uverbs_post_recv cmd;
  2003. struct ib_uverbs_post_recv_resp resp;
  2004. struct ib_recv_wr *wr, *next, *bad_wr;
  2005. struct ib_qp *qp;
  2006. ssize_t ret = -EINVAL;
  2007. if (copy_from_user(&cmd, buf, sizeof cmd))
  2008. return -EFAULT;
  2009. wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
  2010. in_len - sizeof cmd, cmd.wr_count,
  2011. cmd.sge_count, cmd.wqe_size);
  2012. if (IS_ERR(wr))
  2013. return PTR_ERR(wr);
  2014. qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, file);
  2015. if (!qp)
  2016. goto out;
  2017. resp.bad_wr = 0;
  2018. ret = qp->device->post_recv(qp->real_qp, wr, &bad_wr);
  2019. uobj_put_obj_read(qp);
  2020. if (ret) {
  2021. for (next = wr; next; next = next->next) {
  2022. ++resp.bad_wr;
  2023. if (next == bad_wr)
  2024. break;
  2025. }
  2026. }
  2027. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
  2028. ret = -EFAULT;
  2029. out:
  2030. while (wr) {
  2031. next = wr->next;
  2032. kfree(wr);
  2033. wr = next;
  2034. }
  2035. return ret ? ret : in_len;
  2036. }
  2037. ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
  2038. struct ib_device *ib_dev,
  2039. const char __user *buf, int in_len,
  2040. int out_len)
  2041. {
  2042. struct ib_uverbs_post_srq_recv cmd;
  2043. struct ib_uverbs_post_srq_recv_resp resp;
  2044. struct ib_recv_wr *wr, *next, *bad_wr;
  2045. struct ib_srq *srq;
  2046. ssize_t ret = -EINVAL;
  2047. if (copy_from_user(&cmd, buf, sizeof cmd))
  2048. return -EFAULT;
  2049. wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
  2050. in_len - sizeof cmd, cmd.wr_count,
  2051. cmd.sge_count, cmd.wqe_size);
  2052. if (IS_ERR(wr))
  2053. return PTR_ERR(wr);
  2054. srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, file);
  2055. if (!srq)
  2056. goto out;
  2057. resp.bad_wr = 0;
  2058. ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
  2059. uobj_put_obj_read(srq);
  2060. if (ret)
  2061. for (next = wr; next; next = next->next) {
  2062. ++resp.bad_wr;
  2063. if (next == bad_wr)
  2064. break;
  2065. }
  2066. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
  2067. ret = -EFAULT;
  2068. out:
  2069. while (wr) {
  2070. next = wr->next;
  2071. kfree(wr);
  2072. wr = next;
  2073. }
  2074. return ret ? ret : in_len;
  2075. }
  2076. ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
  2077. struct ib_device *ib_dev,
  2078. const char __user *buf, int in_len,
  2079. int out_len)
  2080. {
  2081. struct ib_uverbs_create_ah cmd;
  2082. struct ib_uverbs_create_ah_resp resp;
  2083. struct ib_uobject *uobj;
  2084. struct ib_pd *pd;
  2085. struct ib_ah *ah;
  2086. struct rdma_ah_attr attr = {};
  2087. int ret;
  2088. struct ib_udata udata;
  2089. if (out_len < sizeof resp)
  2090. return -ENOSPC;
  2091. if (copy_from_user(&cmd, buf, sizeof cmd))
  2092. return -EFAULT;
  2093. if (!rdma_is_port_valid(ib_dev, cmd.attr.port_num))
  2094. return -EINVAL;
  2095. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  2096. u64_to_user_ptr(cmd.response) + sizeof(resp),
  2097. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  2098. out_len - sizeof(resp));
  2099. uobj = uobj_alloc(UVERBS_OBJECT_AH, file);
  2100. if (IS_ERR(uobj))
  2101. return PTR_ERR(uobj);
  2102. pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, file);
  2103. if (!pd) {
  2104. ret = -EINVAL;
  2105. goto err;
  2106. }
  2107. attr.type = rdma_ah_find_type(ib_dev, cmd.attr.port_num);
  2108. rdma_ah_set_make_grd(&attr, false);
  2109. rdma_ah_set_dlid(&attr, cmd.attr.dlid);
  2110. rdma_ah_set_sl(&attr, cmd.attr.sl);
  2111. rdma_ah_set_path_bits(&attr, cmd.attr.src_path_bits);
  2112. rdma_ah_set_static_rate(&attr, cmd.attr.static_rate);
  2113. rdma_ah_set_port_num(&attr, cmd.attr.port_num);
  2114. if (cmd.attr.is_global) {
  2115. rdma_ah_set_grh(&attr, NULL, cmd.attr.grh.flow_label,
  2116. cmd.attr.grh.sgid_index,
  2117. cmd.attr.grh.hop_limit,
  2118. cmd.attr.grh.traffic_class);
  2119. rdma_ah_set_dgid_raw(&attr, cmd.attr.grh.dgid);
  2120. } else {
  2121. rdma_ah_set_ah_flags(&attr, 0);
  2122. }
  2123. ah = rdma_create_user_ah(pd, &attr, &udata);
  2124. if (IS_ERR(ah)) {
  2125. ret = PTR_ERR(ah);
  2126. goto err_put;
  2127. }
  2128. ah->uobject = uobj;
  2129. uobj->user_handle = cmd.user_handle;
  2130. uobj->object = ah;
  2131. resp.ah_handle = uobj->id;
  2132. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
  2133. ret = -EFAULT;
  2134. goto err_copy;
  2135. }
  2136. uobj_put_obj_read(pd);
  2137. uobj_alloc_commit(uobj);
  2138. return in_len;
  2139. err_copy:
  2140. rdma_destroy_ah(ah);
  2141. err_put:
  2142. uobj_put_obj_read(pd);
  2143. err:
  2144. uobj_alloc_abort(uobj);
  2145. return ret;
  2146. }
  2147. ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
  2148. struct ib_device *ib_dev,
  2149. const char __user *buf, int in_len, int out_len)
  2150. {
  2151. struct ib_uverbs_destroy_ah cmd;
  2152. if (copy_from_user(&cmd, buf, sizeof cmd))
  2153. return -EFAULT;
  2154. return uobj_perform_destroy(UVERBS_OBJECT_AH, cmd.ah_handle, file,
  2155. in_len);
  2156. }
  2157. ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
  2158. struct ib_device *ib_dev,
  2159. const char __user *buf, int in_len,
  2160. int out_len)
  2161. {
  2162. struct ib_uverbs_attach_mcast cmd;
  2163. struct ib_qp *qp;
  2164. struct ib_uqp_object *obj;
  2165. struct ib_uverbs_mcast_entry *mcast;
  2166. int ret;
  2167. if (copy_from_user(&cmd, buf, sizeof cmd))
  2168. return -EFAULT;
  2169. qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, file);
  2170. if (!qp)
  2171. return -EINVAL;
  2172. obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
  2173. mutex_lock(&obj->mcast_lock);
  2174. list_for_each_entry(mcast, &obj->mcast_list, list)
  2175. if (cmd.mlid == mcast->lid &&
  2176. !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
  2177. ret = 0;
  2178. goto out_put;
  2179. }
  2180. mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
  2181. if (!mcast) {
  2182. ret = -ENOMEM;
  2183. goto out_put;
  2184. }
  2185. mcast->lid = cmd.mlid;
  2186. memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
  2187. ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
  2188. if (!ret)
  2189. list_add_tail(&mcast->list, &obj->mcast_list);
  2190. else
  2191. kfree(mcast);
  2192. out_put:
  2193. mutex_unlock(&obj->mcast_lock);
  2194. uobj_put_obj_read(qp);
  2195. return ret ? ret : in_len;
  2196. }
  2197. ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
  2198. struct ib_device *ib_dev,
  2199. const char __user *buf, int in_len,
  2200. int out_len)
  2201. {
  2202. struct ib_uverbs_detach_mcast cmd;
  2203. struct ib_uqp_object *obj;
  2204. struct ib_qp *qp;
  2205. struct ib_uverbs_mcast_entry *mcast;
  2206. int ret = -EINVAL;
  2207. bool found = false;
  2208. if (copy_from_user(&cmd, buf, sizeof cmd))
  2209. return -EFAULT;
  2210. qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, file);
  2211. if (!qp)
  2212. return -EINVAL;
  2213. obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
  2214. mutex_lock(&obj->mcast_lock);
  2215. list_for_each_entry(mcast, &obj->mcast_list, list)
  2216. if (cmd.mlid == mcast->lid &&
  2217. !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
  2218. list_del(&mcast->list);
  2219. kfree(mcast);
  2220. found = true;
  2221. break;
  2222. }
  2223. if (!found) {
  2224. ret = -EINVAL;
  2225. goto out_put;
  2226. }
  2227. ret = ib_detach_mcast(qp, (union ib_gid *)cmd.gid, cmd.mlid);
  2228. out_put:
  2229. mutex_unlock(&obj->mcast_lock);
  2230. uobj_put_obj_read(qp);
  2231. return ret ? ret : in_len;
  2232. }
  2233. struct ib_uflow_resources {
  2234. size_t max;
  2235. size_t num;
  2236. size_t collection_num;
  2237. size_t counters_num;
  2238. struct ib_counters **counters;
  2239. struct ib_flow_action **collection;
  2240. };
  2241. static struct ib_uflow_resources *flow_resources_alloc(size_t num_specs)
  2242. {
  2243. struct ib_uflow_resources *resources;
  2244. resources = kzalloc(sizeof(*resources), GFP_KERNEL);
  2245. if (!resources)
  2246. return NULL;
  2247. if (!num_specs)
  2248. goto out;
  2249. resources->counters =
  2250. kcalloc(num_specs, sizeof(*resources->counters), GFP_KERNEL);
  2251. resources->collection =
  2252. kcalloc(num_specs, sizeof(*resources->collection), GFP_KERNEL);
  2253. if (!resources->counters || !resources->collection)
  2254. goto err;
  2255. out:
  2256. resources->max = num_specs;
  2257. return resources;
  2258. err:
  2259. kfree(resources->counters);
  2260. kfree(resources);
  2261. return NULL;
  2262. }
  2263. void ib_uverbs_flow_resources_free(struct ib_uflow_resources *uflow_res)
  2264. {
  2265. unsigned int i;
  2266. if (!uflow_res)
  2267. return;
  2268. for (i = 0; i < uflow_res->collection_num; i++)
  2269. atomic_dec(&uflow_res->collection[i]->usecnt);
  2270. for (i = 0; i < uflow_res->counters_num; i++)
  2271. atomic_dec(&uflow_res->counters[i]->usecnt);
  2272. kfree(uflow_res->collection);
  2273. kfree(uflow_res->counters);
  2274. kfree(uflow_res);
  2275. }
  2276. static void flow_resources_add(struct ib_uflow_resources *uflow_res,
  2277. enum ib_flow_spec_type type,
  2278. void *ibobj)
  2279. {
  2280. WARN_ON(uflow_res->num >= uflow_res->max);
  2281. switch (type) {
  2282. case IB_FLOW_SPEC_ACTION_HANDLE:
  2283. atomic_inc(&((struct ib_flow_action *)ibobj)->usecnt);
  2284. uflow_res->collection[uflow_res->collection_num++] =
  2285. (struct ib_flow_action *)ibobj;
  2286. break;
  2287. case IB_FLOW_SPEC_ACTION_COUNT:
  2288. atomic_inc(&((struct ib_counters *)ibobj)->usecnt);
  2289. uflow_res->counters[uflow_res->counters_num++] =
  2290. (struct ib_counters *)ibobj;
  2291. break;
  2292. default:
  2293. WARN_ON(1);
  2294. }
  2295. uflow_res->num++;
  2296. }
  2297. static int kern_spec_to_ib_spec_action(struct ib_uverbs_file *ufile,
  2298. struct ib_uverbs_flow_spec *kern_spec,
  2299. union ib_flow_spec *ib_spec,
  2300. struct ib_uflow_resources *uflow_res)
  2301. {
  2302. ib_spec->type = kern_spec->type;
  2303. switch (ib_spec->type) {
  2304. case IB_FLOW_SPEC_ACTION_TAG:
  2305. if (kern_spec->flow_tag.size !=
  2306. sizeof(struct ib_uverbs_flow_spec_action_tag))
  2307. return -EINVAL;
  2308. ib_spec->flow_tag.size = sizeof(struct ib_flow_spec_action_tag);
  2309. ib_spec->flow_tag.tag_id = kern_spec->flow_tag.tag_id;
  2310. break;
  2311. case IB_FLOW_SPEC_ACTION_DROP:
  2312. if (kern_spec->drop.size !=
  2313. sizeof(struct ib_uverbs_flow_spec_action_drop))
  2314. return -EINVAL;
  2315. ib_spec->drop.size = sizeof(struct ib_flow_spec_action_drop);
  2316. break;
  2317. case IB_FLOW_SPEC_ACTION_HANDLE:
  2318. if (kern_spec->action.size !=
  2319. sizeof(struct ib_uverbs_flow_spec_action_handle))
  2320. return -EOPNOTSUPP;
  2321. ib_spec->action.act = uobj_get_obj_read(flow_action,
  2322. UVERBS_OBJECT_FLOW_ACTION,
  2323. kern_spec->action.handle,
  2324. ufile);
  2325. if (!ib_spec->action.act)
  2326. return -EINVAL;
  2327. ib_spec->action.size =
  2328. sizeof(struct ib_flow_spec_action_handle);
  2329. flow_resources_add(uflow_res,
  2330. IB_FLOW_SPEC_ACTION_HANDLE,
  2331. ib_spec->action.act);
  2332. uobj_put_obj_read(ib_spec->action.act);
  2333. break;
  2334. case IB_FLOW_SPEC_ACTION_COUNT:
  2335. if (kern_spec->flow_count.size !=
  2336. sizeof(struct ib_uverbs_flow_spec_action_count))
  2337. return -EINVAL;
  2338. ib_spec->flow_count.counters =
  2339. uobj_get_obj_read(counters,
  2340. UVERBS_OBJECT_COUNTERS,
  2341. kern_spec->flow_count.handle,
  2342. ufile);
  2343. if (!ib_spec->flow_count.counters)
  2344. return -EINVAL;
  2345. ib_spec->flow_count.size =
  2346. sizeof(struct ib_flow_spec_action_count);
  2347. flow_resources_add(uflow_res,
  2348. IB_FLOW_SPEC_ACTION_COUNT,
  2349. ib_spec->flow_count.counters);
  2350. uobj_put_obj_read(ib_spec->flow_count.counters);
  2351. break;
  2352. default:
  2353. return -EINVAL;
  2354. }
  2355. return 0;
  2356. }
  2357. static size_t kern_spec_filter_sz(const struct ib_uverbs_flow_spec_hdr *spec)
  2358. {
  2359. /* Returns user space filter size, includes padding */
  2360. return (spec->size - sizeof(struct ib_uverbs_flow_spec_hdr)) / 2;
  2361. }
  2362. static ssize_t spec_filter_size(const void *kern_spec_filter, u16 kern_filter_size,
  2363. u16 ib_real_filter_sz)
  2364. {
  2365. /*
  2366. * User space filter structures must be 64 bit aligned, otherwise this
  2367. * may pass, but we won't handle additional new attributes.
  2368. */
  2369. if (kern_filter_size > ib_real_filter_sz) {
  2370. if (memchr_inv(kern_spec_filter +
  2371. ib_real_filter_sz, 0,
  2372. kern_filter_size - ib_real_filter_sz))
  2373. return -EINVAL;
  2374. return ib_real_filter_sz;
  2375. }
  2376. return kern_filter_size;
  2377. }
  2378. int ib_uverbs_kern_spec_to_ib_spec_filter(enum ib_flow_spec_type type,
  2379. const void *kern_spec_mask,
  2380. const void *kern_spec_val,
  2381. size_t kern_filter_sz,
  2382. union ib_flow_spec *ib_spec)
  2383. {
  2384. ssize_t actual_filter_sz;
  2385. ssize_t ib_filter_sz;
  2386. /* User flow spec size must be aligned to 4 bytes */
  2387. if (kern_filter_sz != ALIGN(kern_filter_sz, 4))
  2388. return -EINVAL;
  2389. ib_spec->type = type;
  2390. if (ib_spec->type == (IB_FLOW_SPEC_INNER | IB_FLOW_SPEC_VXLAN_TUNNEL))
  2391. return -EINVAL;
  2392. switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
  2393. case IB_FLOW_SPEC_ETH:
  2394. ib_filter_sz = offsetof(struct ib_flow_eth_filter, real_sz);
  2395. actual_filter_sz = spec_filter_size(kern_spec_mask,
  2396. kern_filter_sz,
  2397. ib_filter_sz);
  2398. if (actual_filter_sz <= 0)
  2399. return -EINVAL;
  2400. ib_spec->size = sizeof(struct ib_flow_spec_eth);
  2401. memcpy(&ib_spec->eth.val, kern_spec_val, actual_filter_sz);
  2402. memcpy(&ib_spec->eth.mask, kern_spec_mask, actual_filter_sz);
  2403. break;
  2404. case IB_FLOW_SPEC_IPV4:
  2405. ib_filter_sz = offsetof(struct ib_flow_ipv4_filter, real_sz);
  2406. actual_filter_sz = spec_filter_size(kern_spec_mask,
  2407. kern_filter_sz,
  2408. ib_filter_sz);
  2409. if (actual_filter_sz <= 0)
  2410. return -EINVAL;
  2411. ib_spec->size = sizeof(struct ib_flow_spec_ipv4);
  2412. memcpy(&ib_spec->ipv4.val, kern_spec_val, actual_filter_sz);
  2413. memcpy(&ib_spec->ipv4.mask, kern_spec_mask, actual_filter_sz);
  2414. break;
  2415. case IB_FLOW_SPEC_IPV6:
  2416. ib_filter_sz = offsetof(struct ib_flow_ipv6_filter, real_sz);
  2417. actual_filter_sz = spec_filter_size(kern_spec_mask,
  2418. kern_filter_sz,
  2419. ib_filter_sz);
  2420. if (actual_filter_sz <= 0)
  2421. return -EINVAL;
  2422. ib_spec->size = sizeof(struct ib_flow_spec_ipv6);
  2423. memcpy(&ib_spec->ipv6.val, kern_spec_val, actual_filter_sz);
  2424. memcpy(&ib_spec->ipv6.mask, kern_spec_mask, actual_filter_sz);
  2425. if ((ntohl(ib_spec->ipv6.mask.flow_label)) >= BIT(20) ||
  2426. (ntohl(ib_spec->ipv6.val.flow_label)) >= BIT(20))
  2427. return -EINVAL;
  2428. break;
  2429. case IB_FLOW_SPEC_TCP:
  2430. case IB_FLOW_SPEC_UDP:
  2431. ib_filter_sz = offsetof(struct ib_flow_tcp_udp_filter, real_sz);
  2432. actual_filter_sz = spec_filter_size(kern_spec_mask,
  2433. kern_filter_sz,
  2434. ib_filter_sz);
  2435. if (actual_filter_sz <= 0)
  2436. return -EINVAL;
  2437. ib_spec->size = sizeof(struct ib_flow_spec_tcp_udp);
  2438. memcpy(&ib_spec->tcp_udp.val, kern_spec_val, actual_filter_sz);
  2439. memcpy(&ib_spec->tcp_udp.mask, kern_spec_mask, actual_filter_sz);
  2440. break;
  2441. case IB_FLOW_SPEC_VXLAN_TUNNEL:
  2442. ib_filter_sz = offsetof(struct ib_flow_tunnel_filter, real_sz);
  2443. actual_filter_sz = spec_filter_size(kern_spec_mask,
  2444. kern_filter_sz,
  2445. ib_filter_sz);
  2446. if (actual_filter_sz <= 0)
  2447. return -EINVAL;
  2448. ib_spec->tunnel.size = sizeof(struct ib_flow_spec_tunnel);
  2449. memcpy(&ib_spec->tunnel.val, kern_spec_val, actual_filter_sz);
  2450. memcpy(&ib_spec->tunnel.mask, kern_spec_mask, actual_filter_sz);
  2451. if ((ntohl(ib_spec->tunnel.mask.tunnel_id)) >= BIT(24) ||
  2452. (ntohl(ib_spec->tunnel.val.tunnel_id)) >= BIT(24))
  2453. return -EINVAL;
  2454. break;
  2455. case IB_FLOW_SPEC_ESP:
  2456. ib_filter_sz = offsetof(struct ib_flow_esp_filter, real_sz);
  2457. actual_filter_sz = spec_filter_size(kern_spec_mask,
  2458. kern_filter_sz,
  2459. ib_filter_sz);
  2460. if (actual_filter_sz <= 0)
  2461. return -EINVAL;
  2462. ib_spec->esp.size = sizeof(struct ib_flow_spec_esp);
  2463. memcpy(&ib_spec->esp.val, kern_spec_val, actual_filter_sz);
  2464. memcpy(&ib_spec->esp.mask, kern_spec_mask, actual_filter_sz);
  2465. break;
  2466. case IB_FLOW_SPEC_GRE:
  2467. ib_filter_sz = offsetof(struct ib_flow_gre_filter, real_sz);
  2468. actual_filter_sz = spec_filter_size(kern_spec_mask,
  2469. kern_filter_sz,
  2470. ib_filter_sz);
  2471. if (actual_filter_sz <= 0)
  2472. return -EINVAL;
  2473. ib_spec->gre.size = sizeof(struct ib_flow_spec_gre);
  2474. memcpy(&ib_spec->gre.val, kern_spec_val, actual_filter_sz);
  2475. memcpy(&ib_spec->gre.mask, kern_spec_mask, actual_filter_sz);
  2476. break;
  2477. case IB_FLOW_SPEC_MPLS:
  2478. ib_filter_sz = offsetof(struct ib_flow_mpls_filter, real_sz);
  2479. actual_filter_sz = spec_filter_size(kern_spec_mask,
  2480. kern_filter_sz,
  2481. ib_filter_sz);
  2482. if (actual_filter_sz <= 0)
  2483. return -EINVAL;
  2484. ib_spec->mpls.size = sizeof(struct ib_flow_spec_mpls);
  2485. memcpy(&ib_spec->mpls.val, kern_spec_val, actual_filter_sz);
  2486. memcpy(&ib_spec->mpls.mask, kern_spec_mask, actual_filter_sz);
  2487. break;
  2488. default:
  2489. return -EINVAL;
  2490. }
  2491. return 0;
  2492. }
  2493. static int kern_spec_to_ib_spec_filter(struct ib_uverbs_flow_spec *kern_spec,
  2494. union ib_flow_spec *ib_spec)
  2495. {
  2496. ssize_t kern_filter_sz;
  2497. void *kern_spec_mask;
  2498. void *kern_spec_val;
  2499. kern_filter_sz = kern_spec_filter_sz(&kern_spec->hdr);
  2500. kern_spec_val = (void *)kern_spec +
  2501. sizeof(struct ib_uverbs_flow_spec_hdr);
  2502. kern_spec_mask = kern_spec_val + kern_filter_sz;
  2503. return ib_uverbs_kern_spec_to_ib_spec_filter(kern_spec->type,
  2504. kern_spec_mask,
  2505. kern_spec_val,
  2506. kern_filter_sz, ib_spec);
  2507. }
  2508. static int kern_spec_to_ib_spec(struct ib_uverbs_file *ufile,
  2509. struct ib_uverbs_flow_spec *kern_spec,
  2510. union ib_flow_spec *ib_spec,
  2511. struct ib_uflow_resources *uflow_res)
  2512. {
  2513. if (kern_spec->reserved)
  2514. return -EINVAL;
  2515. if (kern_spec->type >= IB_FLOW_SPEC_ACTION_TAG)
  2516. return kern_spec_to_ib_spec_action(ufile, kern_spec, ib_spec,
  2517. uflow_res);
  2518. else
  2519. return kern_spec_to_ib_spec_filter(kern_spec, ib_spec);
  2520. }
  2521. int ib_uverbs_ex_create_wq(struct ib_uverbs_file *file,
  2522. struct ib_device *ib_dev,
  2523. struct ib_udata *ucore,
  2524. struct ib_udata *uhw)
  2525. {
  2526. struct ib_uverbs_ex_create_wq cmd = {};
  2527. struct ib_uverbs_ex_create_wq_resp resp = {};
  2528. struct ib_uwq_object *obj;
  2529. int err = 0;
  2530. struct ib_cq *cq;
  2531. struct ib_pd *pd;
  2532. struct ib_wq *wq;
  2533. struct ib_wq_init_attr wq_init_attr = {};
  2534. size_t required_cmd_sz;
  2535. size_t required_resp_len;
  2536. required_cmd_sz = offsetof(typeof(cmd), max_sge) + sizeof(cmd.max_sge);
  2537. required_resp_len = offsetof(typeof(resp), wqn) + sizeof(resp.wqn);
  2538. if (ucore->inlen < required_cmd_sz)
  2539. return -EINVAL;
  2540. if (ucore->outlen < required_resp_len)
  2541. return -ENOSPC;
  2542. if (ucore->inlen > sizeof(cmd) &&
  2543. !ib_is_udata_cleared(ucore, sizeof(cmd),
  2544. ucore->inlen - sizeof(cmd)))
  2545. return -EOPNOTSUPP;
  2546. err = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
  2547. if (err)
  2548. return err;
  2549. if (cmd.comp_mask)
  2550. return -EOPNOTSUPP;
  2551. obj = (struct ib_uwq_object *)uobj_alloc(UVERBS_OBJECT_WQ, file);
  2552. if (IS_ERR(obj))
  2553. return PTR_ERR(obj);
  2554. pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, file);
  2555. if (!pd) {
  2556. err = -EINVAL;
  2557. goto err_uobj;
  2558. }
  2559. cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, file);
  2560. if (!cq) {
  2561. err = -EINVAL;
  2562. goto err_put_pd;
  2563. }
  2564. wq_init_attr.cq = cq;
  2565. wq_init_attr.max_sge = cmd.max_sge;
  2566. wq_init_attr.max_wr = cmd.max_wr;
  2567. wq_init_attr.wq_context = file;
  2568. wq_init_attr.wq_type = cmd.wq_type;
  2569. wq_init_attr.event_handler = ib_uverbs_wq_event_handler;
  2570. if (ucore->inlen >= (offsetof(typeof(cmd), create_flags) +
  2571. sizeof(cmd.create_flags)))
  2572. wq_init_attr.create_flags = cmd.create_flags;
  2573. obj->uevent.events_reported = 0;
  2574. INIT_LIST_HEAD(&obj->uevent.event_list);
  2575. if (!pd->device->create_wq) {
  2576. err = -EOPNOTSUPP;
  2577. goto err_put_cq;
  2578. }
  2579. wq = pd->device->create_wq(pd, &wq_init_attr, uhw);
  2580. if (IS_ERR(wq)) {
  2581. err = PTR_ERR(wq);
  2582. goto err_put_cq;
  2583. }
  2584. wq->uobject = &obj->uevent.uobject;
  2585. obj->uevent.uobject.object = wq;
  2586. wq->wq_type = wq_init_attr.wq_type;
  2587. wq->cq = cq;
  2588. wq->pd = pd;
  2589. wq->device = pd->device;
  2590. wq->wq_context = wq_init_attr.wq_context;
  2591. atomic_set(&wq->usecnt, 0);
  2592. atomic_inc(&pd->usecnt);
  2593. atomic_inc(&cq->usecnt);
  2594. wq->uobject = &obj->uevent.uobject;
  2595. obj->uevent.uobject.object = wq;
  2596. memset(&resp, 0, sizeof(resp));
  2597. resp.wq_handle = obj->uevent.uobject.id;
  2598. resp.max_sge = wq_init_attr.max_sge;
  2599. resp.max_wr = wq_init_attr.max_wr;
  2600. resp.wqn = wq->wq_num;
  2601. resp.response_length = required_resp_len;
  2602. err = ib_copy_to_udata(ucore,
  2603. &resp, resp.response_length);
  2604. if (err)
  2605. goto err_copy;
  2606. uobj_put_obj_read(pd);
  2607. uobj_put_obj_read(cq);
  2608. uobj_alloc_commit(&obj->uevent.uobject);
  2609. return 0;
  2610. err_copy:
  2611. ib_destroy_wq(wq);
  2612. err_put_cq:
  2613. uobj_put_obj_read(cq);
  2614. err_put_pd:
  2615. uobj_put_obj_read(pd);
  2616. err_uobj:
  2617. uobj_alloc_abort(&obj->uevent.uobject);
  2618. return err;
  2619. }
  2620. int ib_uverbs_ex_destroy_wq(struct ib_uverbs_file *file,
  2621. struct ib_device *ib_dev,
  2622. struct ib_udata *ucore,
  2623. struct ib_udata *uhw)
  2624. {
  2625. struct ib_uverbs_ex_destroy_wq cmd = {};
  2626. struct ib_uverbs_ex_destroy_wq_resp resp = {};
  2627. struct ib_uobject *uobj;
  2628. struct ib_uwq_object *obj;
  2629. size_t required_cmd_sz;
  2630. size_t required_resp_len;
  2631. int ret;
  2632. required_cmd_sz = offsetof(typeof(cmd), wq_handle) + sizeof(cmd.wq_handle);
  2633. required_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
  2634. if (ucore->inlen < required_cmd_sz)
  2635. return -EINVAL;
  2636. if (ucore->outlen < required_resp_len)
  2637. return -ENOSPC;
  2638. if (ucore->inlen > sizeof(cmd) &&
  2639. !ib_is_udata_cleared(ucore, sizeof(cmd),
  2640. ucore->inlen - sizeof(cmd)))
  2641. return -EOPNOTSUPP;
  2642. ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
  2643. if (ret)
  2644. return ret;
  2645. if (cmd.comp_mask)
  2646. return -EOPNOTSUPP;
  2647. resp.response_length = required_resp_len;
  2648. uobj = uobj_get_write(UVERBS_OBJECT_WQ, cmd.wq_handle, file);
  2649. if (IS_ERR(uobj))
  2650. return PTR_ERR(uobj);
  2651. obj = container_of(uobj, struct ib_uwq_object, uevent.uobject);
  2652. /*
  2653. * Make sure we don't free the memory in remove_commit as we still
  2654. * needs the uobject memory to create the response.
  2655. */
  2656. uverbs_uobject_get(uobj);
  2657. ret = uobj_remove_commit(uobj);
  2658. resp.events_reported = obj->uevent.events_reported;
  2659. uverbs_uobject_put(uobj);
  2660. if (ret)
  2661. return ret;
  2662. return ib_copy_to_udata(ucore, &resp, resp.response_length);
  2663. }
  2664. int ib_uverbs_ex_modify_wq(struct ib_uverbs_file *file,
  2665. struct ib_device *ib_dev,
  2666. struct ib_udata *ucore,
  2667. struct ib_udata *uhw)
  2668. {
  2669. struct ib_uverbs_ex_modify_wq cmd = {};
  2670. struct ib_wq *wq;
  2671. struct ib_wq_attr wq_attr = {};
  2672. size_t required_cmd_sz;
  2673. int ret;
  2674. required_cmd_sz = offsetof(typeof(cmd), curr_wq_state) + sizeof(cmd.curr_wq_state);
  2675. if (ucore->inlen < required_cmd_sz)
  2676. return -EINVAL;
  2677. if (ucore->inlen > sizeof(cmd) &&
  2678. !ib_is_udata_cleared(ucore, sizeof(cmd),
  2679. ucore->inlen - sizeof(cmd)))
  2680. return -EOPNOTSUPP;
  2681. ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
  2682. if (ret)
  2683. return ret;
  2684. if (!cmd.attr_mask)
  2685. return -EINVAL;
  2686. if (cmd.attr_mask > (IB_WQ_STATE | IB_WQ_CUR_STATE | IB_WQ_FLAGS))
  2687. return -EINVAL;
  2688. wq = uobj_get_obj_read(wq, UVERBS_OBJECT_WQ, cmd.wq_handle, file);
  2689. if (!wq)
  2690. return -EINVAL;
  2691. wq_attr.curr_wq_state = cmd.curr_wq_state;
  2692. wq_attr.wq_state = cmd.wq_state;
  2693. if (cmd.attr_mask & IB_WQ_FLAGS) {
  2694. wq_attr.flags = cmd.flags;
  2695. wq_attr.flags_mask = cmd.flags_mask;
  2696. }
  2697. if (!wq->device->modify_wq) {
  2698. ret = -EOPNOTSUPP;
  2699. goto out;
  2700. }
  2701. ret = wq->device->modify_wq(wq, &wq_attr, cmd.attr_mask, uhw);
  2702. out:
  2703. uobj_put_obj_read(wq);
  2704. return ret;
  2705. }
  2706. int ib_uverbs_ex_create_rwq_ind_table(struct ib_uverbs_file *file,
  2707. struct ib_device *ib_dev,
  2708. struct ib_udata *ucore,
  2709. struct ib_udata *uhw)
  2710. {
  2711. struct ib_uverbs_ex_create_rwq_ind_table cmd = {};
  2712. struct ib_uverbs_ex_create_rwq_ind_table_resp resp = {};
  2713. struct ib_uobject *uobj;
  2714. int err = 0;
  2715. struct ib_rwq_ind_table_init_attr init_attr = {};
  2716. struct ib_rwq_ind_table *rwq_ind_tbl;
  2717. struct ib_wq **wqs = NULL;
  2718. u32 *wqs_handles = NULL;
  2719. struct ib_wq *wq = NULL;
  2720. int i, j, num_read_wqs;
  2721. u32 num_wq_handles;
  2722. u32 expected_in_size;
  2723. size_t required_cmd_sz_header;
  2724. size_t required_resp_len;
  2725. required_cmd_sz_header = offsetof(typeof(cmd), log_ind_tbl_size) + sizeof(cmd.log_ind_tbl_size);
  2726. required_resp_len = offsetof(typeof(resp), ind_tbl_num) + sizeof(resp.ind_tbl_num);
  2727. if (ucore->inlen < required_cmd_sz_header)
  2728. return -EINVAL;
  2729. if (ucore->outlen < required_resp_len)
  2730. return -ENOSPC;
  2731. err = ib_copy_from_udata(&cmd, ucore, required_cmd_sz_header);
  2732. if (err)
  2733. return err;
  2734. ucore->inbuf += required_cmd_sz_header;
  2735. ucore->inlen -= required_cmd_sz_header;
  2736. if (cmd.comp_mask)
  2737. return -EOPNOTSUPP;
  2738. if (cmd.log_ind_tbl_size > IB_USER_VERBS_MAX_LOG_IND_TBL_SIZE)
  2739. return -EINVAL;
  2740. num_wq_handles = 1 << cmd.log_ind_tbl_size;
  2741. expected_in_size = num_wq_handles * sizeof(__u32);
  2742. if (num_wq_handles == 1)
  2743. /* input size for wq handles is u64 aligned */
  2744. expected_in_size += sizeof(__u32);
  2745. if (ucore->inlen < expected_in_size)
  2746. return -EINVAL;
  2747. if (ucore->inlen > expected_in_size &&
  2748. !ib_is_udata_cleared(ucore, expected_in_size,
  2749. ucore->inlen - expected_in_size))
  2750. return -EOPNOTSUPP;
  2751. wqs_handles = kcalloc(num_wq_handles, sizeof(*wqs_handles),
  2752. GFP_KERNEL);
  2753. if (!wqs_handles)
  2754. return -ENOMEM;
  2755. err = ib_copy_from_udata(wqs_handles, ucore,
  2756. num_wq_handles * sizeof(__u32));
  2757. if (err)
  2758. goto err_free;
  2759. wqs = kcalloc(num_wq_handles, sizeof(*wqs), GFP_KERNEL);
  2760. if (!wqs) {
  2761. err = -ENOMEM;
  2762. goto err_free;
  2763. }
  2764. for (num_read_wqs = 0; num_read_wqs < num_wq_handles;
  2765. num_read_wqs++) {
  2766. wq = uobj_get_obj_read(wq, UVERBS_OBJECT_WQ,
  2767. wqs_handles[num_read_wqs], file);
  2768. if (!wq) {
  2769. err = -EINVAL;
  2770. goto put_wqs;
  2771. }
  2772. wqs[num_read_wqs] = wq;
  2773. }
  2774. uobj = uobj_alloc(UVERBS_OBJECT_RWQ_IND_TBL, file);
  2775. if (IS_ERR(uobj)) {
  2776. err = PTR_ERR(uobj);
  2777. goto put_wqs;
  2778. }
  2779. init_attr.log_ind_tbl_size = cmd.log_ind_tbl_size;
  2780. init_attr.ind_tbl = wqs;
  2781. if (!ib_dev->create_rwq_ind_table) {
  2782. err = -EOPNOTSUPP;
  2783. goto err_uobj;
  2784. }
  2785. rwq_ind_tbl = ib_dev->create_rwq_ind_table(ib_dev, &init_attr, uhw);
  2786. if (IS_ERR(rwq_ind_tbl)) {
  2787. err = PTR_ERR(rwq_ind_tbl);
  2788. goto err_uobj;
  2789. }
  2790. rwq_ind_tbl->ind_tbl = wqs;
  2791. rwq_ind_tbl->log_ind_tbl_size = init_attr.log_ind_tbl_size;
  2792. rwq_ind_tbl->uobject = uobj;
  2793. uobj->object = rwq_ind_tbl;
  2794. rwq_ind_tbl->device = ib_dev;
  2795. atomic_set(&rwq_ind_tbl->usecnt, 0);
  2796. for (i = 0; i < num_wq_handles; i++)
  2797. atomic_inc(&wqs[i]->usecnt);
  2798. resp.ind_tbl_handle = uobj->id;
  2799. resp.ind_tbl_num = rwq_ind_tbl->ind_tbl_num;
  2800. resp.response_length = required_resp_len;
  2801. err = ib_copy_to_udata(ucore,
  2802. &resp, resp.response_length);
  2803. if (err)
  2804. goto err_copy;
  2805. kfree(wqs_handles);
  2806. for (j = 0; j < num_read_wqs; j++)
  2807. uobj_put_obj_read(wqs[j]);
  2808. uobj_alloc_commit(uobj);
  2809. return 0;
  2810. err_copy:
  2811. ib_destroy_rwq_ind_table(rwq_ind_tbl);
  2812. err_uobj:
  2813. uobj_alloc_abort(uobj);
  2814. put_wqs:
  2815. for (j = 0; j < num_read_wqs; j++)
  2816. uobj_put_obj_read(wqs[j]);
  2817. err_free:
  2818. kfree(wqs_handles);
  2819. kfree(wqs);
  2820. return err;
  2821. }
  2822. int ib_uverbs_ex_destroy_rwq_ind_table(struct ib_uverbs_file *file,
  2823. struct ib_device *ib_dev,
  2824. struct ib_udata *ucore,
  2825. struct ib_udata *uhw)
  2826. {
  2827. struct ib_uverbs_ex_destroy_rwq_ind_table cmd = {};
  2828. int ret;
  2829. size_t required_cmd_sz;
  2830. required_cmd_sz = offsetof(typeof(cmd), ind_tbl_handle) + sizeof(cmd.ind_tbl_handle);
  2831. if (ucore->inlen < required_cmd_sz)
  2832. return -EINVAL;
  2833. if (ucore->inlen > sizeof(cmd) &&
  2834. !ib_is_udata_cleared(ucore, sizeof(cmd),
  2835. ucore->inlen - sizeof(cmd)))
  2836. return -EOPNOTSUPP;
  2837. ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
  2838. if (ret)
  2839. return ret;
  2840. if (cmd.comp_mask)
  2841. return -EOPNOTSUPP;
  2842. return uobj_perform_destroy(UVERBS_OBJECT_RWQ_IND_TBL,
  2843. cmd.ind_tbl_handle, file, 0);
  2844. }
  2845. int ib_uverbs_ex_create_flow(struct ib_uverbs_file *file,
  2846. struct ib_device *ib_dev,
  2847. struct ib_udata *ucore,
  2848. struct ib_udata *uhw)
  2849. {
  2850. struct ib_uverbs_create_flow cmd;
  2851. struct ib_uverbs_create_flow_resp resp;
  2852. struct ib_uobject *uobj;
  2853. struct ib_uflow_object *uflow;
  2854. struct ib_flow *flow_id;
  2855. struct ib_uverbs_flow_attr *kern_flow_attr;
  2856. struct ib_flow_attr *flow_attr;
  2857. struct ib_qp *qp;
  2858. struct ib_uflow_resources *uflow_res;
  2859. int err = 0;
  2860. void *kern_spec;
  2861. void *ib_spec;
  2862. int i;
  2863. if (ucore->inlen < sizeof(cmd))
  2864. return -EINVAL;
  2865. if (ucore->outlen < sizeof(resp))
  2866. return -ENOSPC;
  2867. err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
  2868. if (err)
  2869. return err;
  2870. ucore->inbuf += sizeof(cmd);
  2871. ucore->inlen -= sizeof(cmd);
  2872. if (cmd.comp_mask)
  2873. return -EINVAL;
  2874. if (!capable(CAP_NET_RAW))
  2875. return -EPERM;
  2876. if (cmd.flow_attr.flags >= IB_FLOW_ATTR_FLAGS_RESERVED)
  2877. return -EINVAL;
  2878. if ((cmd.flow_attr.flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) &&
  2879. ((cmd.flow_attr.type == IB_FLOW_ATTR_ALL_DEFAULT) ||
  2880. (cmd.flow_attr.type == IB_FLOW_ATTR_MC_DEFAULT)))
  2881. return -EINVAL;
  2882. if (cmd.flow_attr.num_of_specs > IB_FLOW_SPEC_SUPPORT_LAYERS)
  2883. return -EINVAL;
  2884. if (cmd.flow_attr.size > ucore->inlen ||
  2885. cmd.flow_attr.size >
  2886. (cmd.flow_attr.num_of_specs * sizeof(struct ib_uverbs_flow_spec)))
  2887. return -EINVAL;
  2888. if (cmd.flow_attr.reserved[0] ||
  2889. cmd.flow_attr.reserved[1])
  2890. return -EINVAL;
  2891. if (cmd.flow_attr.num_of_specs) {
  2892. kern_flow_attr = kmalloc(sizeof(*kern_flow_attr) + cmd.flow_attr.size,
  2893. GFP_KERNEL);
  2894. if (!kern_flow_attr)
  2895. return -ENOMEM;
  2896. memcpy(kern_flow_attr, &cmd.flow_attr, sizeof(*kern_flow_attr));
  2897. err = ib_copy_from_udata(kern_flow_attr + 1, ucore,
  2898. cmd.flow_attr.size);
  2899. if (err)
  2900. goto err_free_attr;
  2901. } else {
  2902. kern_flow_attr = &cmd.flow_attr;
  2903. }
  2904. uobj = uobj_alloc(UVERBS_OBJECT_FLOW, file);
  2905. if (IS_ERR(uobj)) {
  2906. err = PTR_ERR(uobj);
  2907. goto err_free_attr;
  2908. }
  2909. qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, file);
  2910. if (!qp) {
  2911. err = -EINVAL;
  2912. goto err_uobj;
  2913. }
  2914. if (!qp->device->create_flow) {
  2915. err = -EOPNOTSUPP;
  2916. goto err_put;
  2917. }
  2918. flow_attr = kzalloc(struct_size(flow_attr, flows,
  2919. cmd.flow_attr.num_of_specs), GFP_KERNEL);
  2920. if (!flow_attr) {
  2921. err = -ENOMEM;
  2922. goto err_put;
  2923. }
  2924. uflow_res = flow_resources_alloc(cmd.flow_attr.num_of_specs);
  2925. if (!uflow_res) {
  2926. err = -ENOMEM;
  2927. goto err_free_flow_attr;
  2928. }
  2929. flow_attr->type = kern_flow_attr->type;
  2930. flow_attr->priority = kern_flow_attr->priority;
  2931. flow_attr->num_of_specs = kern_flow_attr->num_of_specs;
  2932. flow_attr->port = kern_flow_attr->port;
  2933. flow_attr->flags = kern_flow_attr->flags;
  2934. flow_attr->size = sizeof(*flow_attr);
  2935. kern_spec = kern_flow_attr + 1;
  2936. ib_spec = flow_attr + 1;
  2937. for (i = 0; i < flow_attr->num_of_specs &&
  2938. cmd.flow_attr.size > offsetof(struct ib_uverbs_flow_spec, reserved) &&
  2939. cmd.flow_attr.size >=
  2940. ((struct ib_uverbs_flow_spec *)kern_spec)->size; i++) {
  2941. err = kern_spec_to_ib_spec(file, kern_spec, ib_spec, uflow_res);
  2942. if (err)
  2943. goto err_free;
  2944. flow_attr->size +=
  2945. ((union ib_flow_spec *) ib_spec)->size;
  2946. cmd.flow_attr.size -= ((struct ib_uverbs_flow_spec *)kern_spec)->size;
  2947. kern_spec += ((struct ib_uverbs_flow_spec *) kern_spec)->size;
  2948. ib_spec += ((union ib_flow_spec *) ib_spec)->size;
  2949. }
  2950. if (cmd.flow_attr.size || (i != flow_attr->num_of_specs)) {
  2951. pr_warn("create flow failed, flow %d: %d bytes left from uverb cmd\n",
  2952. i, cmd.flow_attr.size);
  2953. err = -EINVAL;
  2954. goto err_free;
  2955. }
  2956. flow_id = qp->device->create_flow(qp, flow_attr,
  2957. IB_FLOW_DOMAIN_USER, uhw);
  2958. if (IS_ERR(flow_id)) {
  2959. err = PTR_ERR(flow_id);
  2960. goto err_free;
  2961. }
  2962. atomic_inc(&qp->usecnt);
  2963. flow_id->qp = qp;
  2964. flow_id->device = qp->device;
  2965. flow_id->uobject = uobj;
  2966. uobj->object = flow_id;
  2967. uflow = container_of(uobj, typeof(*uflow), uobject);
  2968. uflow->resources = uflow_res;
  2969. memset(&resp, 0, sizeof(resp));
  2970. resp.flow_handle = uobj->id;
  2971. err = ib_copy_to_udata(ucore,
  2972. &resp, sizeof(resp));
  2973. if (err)
  2974. goto err_copy;
  2975. uobj_put_obj_read(qp);
  2976. uobj_alloc_commit(uobj);
  2977. kfree(flow_attr);
  2978. if (cmd.flow_attr.num_of_specs)
  2979. kfree(kern_flow_attr);
  2980. return 0;
  2981. err_copy:
  2982. if (!qp->device->destroy_flow(flow_id))
  2983. atomic_dec(&qp->usecnt);
  2984. err_free:
  2985. ib_uverbs_flow_resources_free(uflow_res);
  2986. err_free_flow_attr:
  2987. kfree(flow_attr);
  2988. err_put:
  2989. uobj_put_obj_read(qp);
  2990. err_uobj:
  2991. uobj_alloc_abort(uobj);
  2992. err_free_attr:
  2993. if (cmd.flow_attr.num_of_specs)
  2994. kfree(kern_flow_attr);
  2995. return err;
  2996. }
  2997. int ib_uverbs_ex_destroy_flow(struct ib_uverbs_file *file,
  2998. struct ib_device *ib_dev,
  2999. struct ib_udata *ucore,
  3000. struct ib_udata *uhw)
  3001. {
  3002. struct ib_uverbs_destroy_flow cmd;
  3003. int ret;
  3004. if (ucore->inlen < sizeof(cmd))
  3005. return -EINVAL;
  3006. ret = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
  3007. if (ret)
  3008. return ret;
  3009. if (cmd.comp_mask)
  3010. return -EINVAL;
  3011. return uobj_perform_destroy(UVERBS_OBJECT_FLOW, cmd.flow_handle, file,
  3012. 0);
  3013. }
  3014. static int __uverbs_create_xsrq(struct ib_uverbs_file *file,
  3015. struct ib_device *ib_dev,
  3016. struct ib_uverbs_create_xsrq *cmd,
  3017. struct ib_udata *udata)
  3018. {
  3019. struct ib_uverbs_create_srq_resp resp;
  3020. struct ib_usrq_object *obj;
  3021. struct ib_pd *pd;
  3022. struct ib_srq *srq;
  3023. struct ib_uobject *uninitialized_var(xrcd_uobj);
  3024. struct ib_srq_init_attr attr;
  3025. int ret;
  3026. obj = (struct ib_usrq_object *)uobj_alloc(UVERBS_OBJECT_SRQ, file);
  3027. if (IS_ERR(obj))
  3028. return PTR_ERR(obj);
  3029. if (cmd->srq_type == IB_SRQT_TM)
  3030. attr.ext.tag_matching.max_num_tags = cmd->max_num_tags;
  3031. if (cmd->srq_type == IB_SRQT_XRC) {
  3032. xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd->xrcd_handle,
  3033. file);
  3034. if (IS_ERR(xrcd_uobj)) {
  3035. ret = -EINVAL;
  3036. goto err;
  3037. }
  3038. attr.ext.xrc.xrcd = (struct ib_xrcd *)xrcd_uobj->object;
  3039. if (!attr.ext.xrc.xrcd) {
  3040. ret = -EINVAL;
  3041. goto err_put_xrcd;
  3042. }
  3043. obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
  3044. atomic_inc(&obj->uxrcd->refcnt);
  3045. }
  3046. if (ib_srq_has_cq(cmd->srq_type)) {
  3047. attr.ext.cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ,
  3048. cmd->cq_handle, file);
  3049. if (!attr.ext.cq) {
  3050. ret = -EINVAL;
  3051. goto err_put_xrcd;
  3052. }
  3053. }
  3054. pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd->pd_handle, file);
  3055. if (!pd) {
  3056. ret = -EINVAL;
  3057. goto err_put_cq;
  3058. }
  3059. attr.event_handler = ib_uverbs_srq_event_handler;
  3060. attr.srq_context = file;
  3061. attr.srq_type = cmd->srq_type;
  3062. attr.attr.max_wr = cmd->max_wr;
  3063. attr.attr.max_sge = cmd->max_sge;
  3064. attr.attr.srq_limit = cmd->srq_limit;
  3065. obj->uevent.events_reported = 0;
  3066. INIT_LIST_HEAD(&obj->uevent.event_list);
  3067. srq = pd->device->create_srq(pd, &attr, udata);
  3068. if (IS_ERR(srq)) {
  3069. ret = PTR_ERR(srq);
  3070. goto err_put;
  3071. }
  3072. srq->device = pd->device;
  3073. srq->pd = pd;
  3074. srq->srq_type = cmd->srq_type;
  3075. srq->uobject = &obj->uevent.uobject;
  3076. srq->event_handler = attr.event_handler;
  3077. srq->srq_context = attr.srq_context;
  3078. if (ib_srq_has_cq(cmd->srq_type)) {
  3079. srq->ext.cq = attr.ext.cq;
  3080. atomic_inc(&attr.ext.cq->usecnt);
  3081. }
  3082. if (cmd->srq_type == IB_SRQT_XRC) {
  3083. srq->ext.xrc.xrcd = attr.ext.xrc.xrcd;
  3084. atomic_inc(&attr.ext.xrc.xrcd->usecnt);
  3085. }
  3086. atomic_inc(&pd->usecnt);
  3087. atomic_set(&srq->usecnt, 0);
  3088. obj->uevent.uobject.object = srq;
  3089. obj->uevent.uobject.user_handle = cmd->user_handle;
  3090. memset(&resp, 0, sizeof resp);
  3091. resp.srq_handle = obj->uevent.uobject.id;
  3092. resp.max_wr = attr.attr.max_wr;
  3093. resp.max_sge = attr.attr.max_sge;
  3094. if (cmd->srq_type == IB_SRQT_XRC)
  3095. resp.srqn = srq->ext.xrc.srq_num;
  3096. if (copy_to_user(u64_to_user_ptr(cmd->response),
  3097. &resp, sizeof resp)) {
  3098. ret = -EFAULT;
  3099. goto err_copy;
  3100. }
  3101. if (cmd->srq_type == IB_SRQT_XRC)
  3102. uobj_put_read(xrcd_uobj);
  3103. if (ib_srq_has_cq(cmd->srq_type))
  3104. uobj_put_obj_read(attr.ext.cq);
  3105. uobj_put_obj_read(pd);
  3106. uobj_alloc_commit(&obj->uevent.uobject);
  3107. return 0;
  3108. err_copy:
  3109. ib_destroy_srq(srq);
  3110. err_put:
  3111. uobj_put_obj_read(pd);
  3112. err_put_cq:
  3113. if (ib_srq_has_cq(cmd->srq_type))
  3114. uobj_put_obj_read(attr.ext.cq);
  3115. err_put_xrcd:
  3116. if (cmd->srq_type == IB_SRQT_XRC) {
  3117. atomic_dec(&obj->uxrcd->refcnt);
  3118. uobj_put_read(xrcd_uobj);
  3119. }
  3120. err:
  3121. uobj_alloc_abort(&obj->uevent.uobject);
  3122. return ret;
  3123. }
  3124. ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
  3125. struct ib_device *ib_dev,
  3126. const char __user *buf, int in_len,
  3127. int out_len)
  3128. {
  3129. struct ib_uverbs_create_srq cmd;
  3130. struct ib_uverbs_create_xsrq xcmd;
  3131. struct ib_uverbs_create_srq_resp resp;
  3132. struct ib_udata udata;
  3133. int ret;
  3134. if (out_len < sizeof resp)
  3135. return -ENOSPC;
  3136. if (copy_from_user(&cmd, buf, sizeof cmd))
  3137. return -EFAULT;
  3138. memset(&xcmd, 0, sizeof(xcmd));
  3139. xcmd.response = cmd.response;
  3140. xcmd.user_handle = cmd.user_handle;
  3141. xcmd.srq_type = IB_SRQT_BASIC;
  3142. xcmd.pd_handle = cmd.pd_handle;
  3143. xcmd.max_wr = cmd.max_wr;
  3144. xcmd.max_sge = cmd.max_sge;
  3145. xcmd.srq_limit = cmd.srq_limit;
  3146. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  3147. u64_to_user_ptr(cmd.response) + sizeof(resp),
  3148. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  3149. out_len - sizeof(resp));
  3150. ret = __uverbs_create_xsrq(file, ib_dev, &xcmd, &udata);
  3151. if (ret)
  3152. return ret;
  3153. return in_len;
  3154. }
  3155. ssize_t ib_uverbs_create_xsrq(struct ib_uverbs_file *file,
  3156. struct ib_device *ib_dev,
  3157. const char __user *buf, int in_len, int out_len)
  3158. {
  3159. struct ib_uverbs_create_xsrq cmd;
  3160. struct ib_uverbs_create_srq_resp resp;
  3161. struct ib_udata udata;
  3162. int ret;
  3163. if (out_len < sizeof resp)
  3164. return -ENOSPC;
  3165. if (copy_from_user(&cmd, buf, sizeof cmd))
  3166. return -EFAULT;
  3167. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  3168. u64_to_user_ptr(cmd.response) + sizeof(resp),
  3169. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  3170. out_len - sizeof(resp));
  3171. ret = __uverbs_create_xsrq(file, ib_dev, &cmd, &udata);
  3172. if (ret)
  3173. return ret;
  3174. return in_len;
  3175. }
  3176. ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
  3177. struct ib_device *ib_dev,
  3178. const char __user *buf, int in_len,
  3179. int out_len)
  3180. {
  3181. struct ib_uverbs_modify_srq cmd;
  3182. struct ib_udata udata;
  3183. struct ib_srq *srq;
  3184. struct ib_srq_attr attr;
  3185. int ret;
  3186. if (copy_from_user(&cmd, buf, sizeof cmd))
  3187. return -EFAULT;
  3188. ib_uverbs_init_udata(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
  3189. out_len);
  3190. srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, file);
  3191. if (!srq)
  3192. return -EINVAL;
  3193. attr.max_wr = cmd.max_wr;
  3194. attr.srq_limit = cmd.srq_limit;
  3195. ret = srq->device->modify_srq(srq, &attr, cmd.attr_mask, &udata);
  3196. uobj_put_obj_read(srq);
  3197. return ret ? ret : in_len;
  3198. }
  3199. ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
  3200. struct ib_device *ib_dev,
  3201. const char __user *buf,
  3202. int in_len, int out_len)
  3203. {
  3204. struct ib_uverbs_query_srq cmd;
  3205. struct ib_uverbs_query_srq_resp resp;
  3206. struct ib_srq_attr attr;
  3207. struct ib_srq *srq;
  3208. int ret;
  3209. if (out_len < sizeof resp)
  3210. return -ENOSPC;
  3211. if (copy_from_user(&cmd, buf, sizeof cmd))
  3212. return -EFAULT;
  3213. srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, file);
  3214. if (!srq)
  3215. return -EINVAL;
  3216. ret = ib_query_srq(srq, &attr);
  3217. uobj_put_obj_read(srq);
  3218. if (ret)
  3219. return ret;
  3220. memset(&resp, 0, sizeof resp);
  3221. resp.max_wr = attr.max_wr;
  3222. resp.max_sge = attr.max_sge;
  3223. resp.srq_limit = attr.srq_limit;
  3224. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
  3225. return -EFAULT;
  3226. return in_len;
  3227. }
  3228. ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
  3229. struct ib_device *ib_dev,
  3230. const char __user *buf, int in_len,
  3231. int out_len)
  3232. {
  3233. struct ib_uverbs_destroy_srq cmd;
  3234. struct ib_uverbs_destroy_srq_resp resp;
  3235. struct ib_uobject *uobj;
  3236. struct ib_uevent_object *obj;
  3237. int ret = -EINVAL;
  3238. if (copy_from_user(&cmd, buf, sizeof cmd))
  3239. return -EFAULT;
  3240. uobj = uobj_get_write(UVERBS_OBJECT_SRQ, cmd.srq_handle, file);
  3241. if (IS_ERR(uobj))
  3242. return PTR_ERR(uobj);
  3243. obj = container_of(uobj, struct ib_uevent_object, uobject);
  3244. /*
  3245. * Make sure we don't free the memory in remove_commit as we still
  3246. * needs the uobject memory to create the response.
  3247. */
  3248. uverbs_uobject_get(uobj);
  3249. memset(&resp, 0, sizeof(resp));
  3250. ret = uobj_remove_commit(uobj);
  3251. if (ret) {
  3252. uverbs_uobject_put(uobj);
  3253. return ret;
  3254. }
  3255. resp.events_reported = obj->events_reported;
  3256. uverbs_uobject_put(uobj);
  3257. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof(resp)))
  3258. return -EFAULT;
  3259. return in_len;
  3260. }
  3261. int ib_uverbs_ex_query_device(struct ib_uverbs_file *file,
  3262. struct ib_device *ib_dev,
  3263. struct ib_udata *ucore,
  3264. struct ib_udata *uhw)
  3265. {
  3266. struct ib_uverbs_ex_query_device_resp resp = { {0} };
  3267. struct ib_uverbs_ex_query_device cmd;
  3268. struct ib_device_attr attr = {0};
  3269. int err;
  3270. if (!ib_dev->query_device)
  3271. return -EOPNOTSUPP;
  3272. if (ucore->inlen < sizeof(cmd))
  3273. return -EINVAL;
  3274. err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
  3275. if (err)
  3276. return err;
  3277. if (cmd.comp_mask)
  3278. return -EINVAL;
  3279. if (cmd.reserved)
  3280. return -EINVAL;
  3281. resp.response_length = offsetof(typeof(resp), odp_caps);
  3282. if (ucore->outlen < resp.response_length)
  3283. return -ENOSPC;
  3284. err = ib_dev->query_device(ib_dev, &attr, uhw);
  3285. if (err)
  3286. return err;
  3287. copy_query_dev_fields(file, ib_dev, &resp.base, &attr);
  3288. if (ucore->outlen < resp.response_length + sizeof(resp.odp_caps))
  3289. goto end;
  3290. #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
  3291. resp.odp_caps.general_caps = attr.odp_caps.general_caps;
  3292. resp.odp_caps.per_transport_caps.rc_odp_caps =
  3293. attr.odp_caps.per_transport_caps.rc_odp_caps;
  3294. resp.odp_caps.per_transport_caps.uc_odp_caps =
  3295. attr.odp_caps.per_transport_caps.uc_odp_caps;
  3296. resp.odp_caps.per_transport_caps.ud_odp_caps =
  3297. attr.odp_caps.per_transport_caps.ud_odp_caps;
  3298. #endif
  3299. resp.response_length += sizeof(resp.odp_caps);
  3300. if (ucore->outlen < resp.response_length + sizeof(resp.timestamp_mask))
  3301. goto end;
  3302. resp.timestamp_mask = attr.timestamp_mask;
  3303. resp.response_length += sizeof(resp.timestamp_mask);
  3304. if (ucore->outlen < resp.response_length + sizeof(resp.hca_core_clock))
  3305. goto end;
  3306. resp.hca_core_clock = attr.hca_core_clock;
  3307. resp.response_length += sizeof(resp.hca_core_clock);
  3308. if (ucore->outlen < resp.response_length + sizeof(resp.device_cap_flags_ex))
  3309. goto end;
  3310. resp.device_cap_flags_ex = attr.device_cap_flags;
  3311. resp.response_length += sizeof(resp.device_cap_flags_ex);
  3312. if (ucore->outlen < resp.response_length + sizeof(resp.rss_caps))
  3313. goto end;
  3314. resp.rss_caps.supported_qpts = attr.rss_caps.supported_qpts;
  3315. resp.rss_caps.max_rwq_indirection_tables =
  3316. attr.rss_caps.max_rwq_indirection_tables;
  3317. resp.rss_caps.max_rwq_indirection_table_size =
  3318. attr.rss_caps.max_rwq_indirection_table_size;
  3319. resp.response_length += sizeof(resp.rss_caps);
  3320. if (ucore->outlen < resp.response_length + sizeof(resp.max_wq_type_rq))
  3321. goto end;
  3322. resp.max_wq_type_rq = attr.max_wq_type_rq;
  3323. resp.response_length += sizeof(resp.max_wq_type_rq);
  3324. if (ucore->outlen < resp.response_length + sizeof(resp.raw_packet_caps))
  3325. goto end;
  3326. resp.raw_packet_caps = attr.raw_packet_caps;
  3327. resp.response_length += sizeof(resp.raw_packet_caps);
  3328. if (ucore->outlen < resp.response_length + sizeof(resp.tm_caps))
  3329. goto end;
  3330. resp.tm_caps.max_rndv_hdr_size = attr.tm_caps.max_rndv_hdr_size;
  3331. resp.tm_caps.max_num_tags = attr.tm_caps.max_num_tags;
  3332. resp.tm_caps.max_ops = attr.tm_caps.max_ops;
  3333. resp.tm_caps.max_sge = attr.tm_caps.max_sge;
  3334. resp.tm_caps.flags = attr.tm_caps.flags;
  3335. resp.response_length += sizeof(resp.tm_caps);
  3336. if (ucore->outlen < resp.response_length + sizeof(resp.cq_moderation_caps))
  3337. goto end;
  3338. resp.cq_moderation_caps.max_cq_moderation_count =
  3339. attr.cq_caps.max_cq_moderation_count;
  3340. resp.cq_moderation_caps.max_cq_moderation_period =
  3341. attr.cq_caps.max_cq_moderation_period;
  3342. resp.response_length += sizeof(resp.cq_moderation_caps);
  3343. if (ucore->outlen < resp.response_length + sizeof(resp.max_dm_size))
  3344. goto end;
  3345. resp.max_dm_size = attr.max_dm_size;
  3346. resp.response_length += sizeof(resp.max_dm_size);
  3347. end:
  3348. err = ib_copy_to_udata(ucore, &resp, resp.response_length);
  3349. return err;
  3350. }
  3351. int ib_uverbs_ex_modify_cq(struct ib_uverbs_file *file,
  3352. struct ib_device *ib_dev,
  3353. struct ib_udata *ucore,
  3354. struct ib_udata *uhw)
  3355. {
  3356. struct ib_uverbs_ex_modify_cq cmd = {};
  3357. struct ib_cq *cq;
  3358. size_t required_cmd_sz;
  3359. int ret;
  3360. required_cmd_sz = offsetof(typeof(cmd), reserved) +
  3361. sizeof(cmd.reserved);
  3362. if (ucore->inlen < required_cmd_sz)
  3363. return -EINVAL;
  3364. /* sanity checks */
  3365. if (ucore->inlen > sizeof(cmd) &&
  3366. !ib_is_udata_cleared(ucore, sizeof(cmd),
  3367. ucore->inlen - sizeof(cmd)))
  3368. return -EOPNOTSUPP;
  3369. ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
  3370. if (ret)
  3371. return ret;
  3372. if (!cmd.attr_mask || cmd.reserved)
  3373. return -EINVAL;
  3374. if (cmd.attr_mask > IB_CQ_MODERATE)
  3375. return -EOPNOTSUPP;
  3376. cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, file);
  3377. if (!cq)
  3378. return -EINVAL;
  3379. ret = rdma_set_cq_moderation(cq, cmd.attr.cq_count, cmd.attr.cq_period);
  3380. uobj_put_obj_read(cq);
  3381. return ret;
  3382. }