ocrdma_verbs.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030
  1. /*******************************************************************
  2. * This file is part of the Emulex RoCE Device Driver for *
  3. * RoCE (RDMA over Converged Ethernet) adapters. *
  4. * Copyright (C) 2008-2012 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * *
  8. * This program is free software; you can redistribute it and/or *
  9. * modify it under the terms of version 2 of the GNU General *
  10. * Public License as published by the Free Software Foundation. *
  11. * This program is distributed in the hope that it will be useful. *
  12. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  13. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  14. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  15. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  16. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  17. * more details, a copy of which can be found in the file COPYING *
  18. * included with this package. *
  19. *
  20. * Contact Information:
  21. * linux-drivers@emulex.com
  22. *
  23. * Emulex
  24. * 3333 Susan Street
  25. * Costa Mesa, CA 92626
  26. *******************************************************************/
  27. #include <linux/dma-mapping.h>
  28. #include <rdma/ib_verbs.h>
  29. #include <rdma/ib_user_verbs.h>
  30. #include <rdma/iw_cm.h>
  31. #include <rdma/ib_umem.h>
  32. #include <rdma/ib_addr.h>
  33. #include "ocrdma.h"
  34. #include "ocrdma_hw.h"
  35. #include "ocrdma_verbs.h"
  36. #include "ocrdma_abi.h"
  37. int ocrdma_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
  38. {
  39. if (index > 1)
  40. return -EINVAL;
  41. *pkey = 0xffff;
  42. return 0;
  43. }
  44. int ocrdma_query_gid(struct ib_device *ibdev, u8 port,
  45. int index, union ib_gid *sgid)
  46. {
  47. struct ocrdma_dev *dev;
  48. dev = get_ocrdma_dev(ibdev);
  49. memset(sgid, 0, sizeof(*sgid));
  50. if (index >= OCRDMA_MAX_SGID)
  51. return -EINVAL;
  52. memcpy(sgid, &dev->sgid_tbl[index], sizeof(*sgid));
  53. return 0;
  54. }
  55. int ocrdma_query_device(struct ib_device *ibdev, struct ib_device_attr *attr)
  56. {
  57. struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
  58. memset(attr, 0, sizeof *attr);
  59. memcpy(&attr->fw_ver, &dev->attr.fw_ver[0],
  60. min(sizeof(dev->attr.fw_ver), sizeof(attr->fw_ver)));
  61. ocrdma_get_guid(dev, (u8 *)&attr->sys_image_guid);
  62. attr->max_mr_size = ~0ull;
  63. attr->page_size_cap = 0xffff000;
  64. attr->vendor_id = dev->nic_info.pdev->vendor;
  65. attr->vendor_part_id = dev->nic_info.pdev->device;
  66. attr->hw_ver = 0;
  67. attr->max_qp = dev->attr.max_qp;
  68. attr->max_ah = OCRDMA_MAX_AH;
  69. attr->max_qp_wr = dev->attr.max_wqe;
  70. attr->device_cap_flags = IB_DEVICE_CURR_QP_STATE_MOD |
  71. IB_DEVICE_RC_RNR_NAK_GEN |
  72. IB_DEVICE_SHUTDOWN_PORT |
  73. IB_DEVICE_SYS_IMAGE_GUID |
  74. IB_DEVICE_LOCAL_DMA_LKEY |
  75. IB_DEVICE_MEM_MGT_EXTENSIONS;
  76. attr->max_sge = min(dev->attr.max_send_sge, dev->attr.max_srq_sge);
  77. attr->max_sge_rd = 0;
  78. attr->max_cq = dev->attr.max_cq;
  79. attr->max_cqe = dev->attr.max_cqe;
  80. attr->max_mr = dev->attr.max_mr;
  81. attr->max_mw = 0;
  82. attr->max_pd = dev->attr.max_pd;
  83. attr->atomic_cap = 0;
  84. attr->max_fmr = 0;
  85. attr->max_map_per_fmr = 0;
  86. attr->max_qp_rd_atom =
  87. min(dev->attr.max_ord_per_qp, dev->attr.max_ird_per_qp);
  88. attr->max_qp_init_rd_atom = dev->attr.max_ord_per_qp;
  89. attr->max_srq = dev->attr.max_srq;
  90. attr->max_srq_sge = dev->attr.max_srq_sge;
  91. attr->max_srq_wr = dev->attr.max_rqe;
  92. attr->local_ca_ack_delay = dev->attr.local_ca_ack_delay;
  93. attr->max_fast_reg_page_list_len = 0;
  94. attr->max_pkeys = 1;
  95. return 0;
  96. }
  97. static inline void get_link_speed_and_width(struct ocrdma_dev *dev,
  98. u8 *ib_speed, u8 *ib_width)
  99. {
  100. int status;
  101. u8 speed;
  102. status = ocrdma_mbx_get_link_speed(dev, &speed);
  103. if (status)
  104. speed = OCRDMA_PHYS_LINK_SPEED_ZERO;
  105. switch (speed) {
  106. case OCRDMA_PHYS_LINK_SPEED_1GBPS:
  107. *ib_speed = IB_SPEED_SDR;
  108. *ib_width = IB_WIDTH_1X;
  109. break;
  110. case OCRDMA_PHYS_LINK_SPEED_10GBPS:
  111. *ib_speed = IB_SPEED_QDR;
  112. *ib_width = IB_WIDTH_1X;
  113. break;
  114. case OCRDMA_PHYS_LINK_SPEED_20GBPS:
  115. *ib_speed = IB_SPEED_DDR;
  116. *ib_width = IB_WIDTH_4X;
  117. break;
  118. case OCRDMA_PHYS_LINK_SPEED_40GBPS:
  119. *ib_speed = IB_SPEED_QDR;
  120. *ib_width = IB_WIDTH_4X;
  121. break;
  122. default:
  123. /* Unsupported */
  124. *ib_speed = IB_SPEED_SDR;
  125. *ib_width = IB_WIDTH_1X;
  126. }
  127. }
  128. int ocrdma_query_port(struct ib_device *ibdev,
  129. u8 port, struct ib_port_attr *props)
  130. {
  131. enum ib_port_state port_state;
  132. struct ocrdma_dev *dev;
  133. struct net_device *netdev;
  134. dev = get_ocrdma_dev(ibdev);
  135. if (port > 1) {
  136. pr_err("%s(%d) invalid_port=0x%x\n", __func__,
  137. dev->id, port);
  138. return -EINVAL;
  139. }
  140. netdev = dev->nic_info.netdev;
  141. if (netif_running(netdev) && netif_oper_up(netdev)) {
  142. port_state = IB_PORT_ACTIVE;
  143. props->phys_state = 5;
  144. } else {
  145. port_state = IB_PORT_DOWN;
  146. props->phys_state = 3;
  147. }
  148. props->max_mtu = IB_MTU_4096;
  149. props->active_mtu = iboe_get_mtu(netdev->mtu);
  150. props->lid = 0;
  151. props->lmc = 0;
  152. props->sm_lid = 0;
  153. props->sm_sl = 0;
  154. props->state = port_state;
  155. props->port_cap_flags =
  156. IB_PORT_CM_SUP |
  157. IB_PORT_REINIT_SUP |
  158. IB_PORT_DEVICE_MGMT_SUP | IB_PORT_VENDOR_CLASS_SUP;
  159. props->gid_tbl_len = OCRDMA_MAX_SGID;
  160. props->pkey_tbl_len = 1;
  161. props->bad_pkey_cntr = 0;
  162. props->qkey_viol_cntr = 0;
  163. get_link_speed_and_width(dev, &props->active_speed,
  164. &props->active_width);
  165. props->max_msg_sz = 0x80000000;
  166. props->max_vl_num = 4;
  167. return 0;
  168. }
  169. int ocrdma_modify_port(struct ib_device *ibdev, u8 port, int mask,
  170. struct ib_port_modify *props)
  171. {
  172. struct ocrdma_dev *dev;
  173. dev = get_ocrdma_dev(ibdev);
  174. if (port > 1) {
  175. pr_err("%s(%d) invalid_port=0x%x\n", __func__, dev->id, port);
  176. return -EINVAL;
  177. }
  178. return 0;
  179. }
  180. static int ocrdma_add_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
  181. unsigned long len)
  182. {
  183. struct ocrdma_mm *mm;
  184. mm = kzalloc(sizeof(*mm), GFP_KERNEL);
  185. if (mm == NULL)
  186. return -ENOMEM;
  187. mm->key.phy_addr = phy_addr;
  188. mm->key.len = len;
  189. INIT_LIST_HEAD(&mm->entry);
  190. mutex_lock(&uctx->mm_list_lock);
  191. list_add_tail(&mm->entry, &uctx->mm_head);
  192. mutex_unlock(&uctx->mm_list_lock);
  193. return 0;
  194. }
  195. static void ocrdma_del_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
  196. unsigned long len)
  197. {
  198. struct ocrdma_mm *mm, *tmp;
  199. mutex_lock(&uctx->mm_list_lock);
  200. list_for_each_entry_safe(mm, tmp, &uctx->mm_head, entry) {
  201. if (len != mm->key.len && phy_addr != mm->key.phy_addr)
  202. continue;
  203. list_del(&mm->entry);
  204. kfree(mm);
  205. break;
  206. }
  207. mutex_unlock(&uctx->mm_list_lock);
  208. }
  209. static bool ocrdma_search_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
  210. unsigned long len)
  211. {
  212. bool found = false;
  213. struct ocrdma_mm *mm;
  214. mutex_lock(&uctx->mm_list_lock);
  215. list_for_each_entry(mm, &uctx->mm_head, entry) {
  216. if (len != mm->key.len && phy_addr != mm->key.phy_addr)
  217. continue;
  218. found = true;
  219. break;
  220. }
  221. mutex_unlock(&uctx->mm_list_lock);
  222. return found;
  223. }
  224. static struct ocrdma_pd *_ocrdma_alloc_pd(struct ocrdma_dev *dev,
  225. struct ocrdma_ucontext *uctx,
  226. struct ib_udata *udata)
  227. {
  228. struct ocrdma_pd *pd = NULL;
  229. int status = 0;
  230. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  231. if (!pd)
  232. return ERR_PTR(-ENOMEM);
  233. if (udata && uctx) {
  234. pd->dpp_enabled =
  235. dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY;
  236. pd->num_dpp_qp =
  237. pd->dpp_enabled ? OCRDMA_PD_MAX_DPP_ENABLED_QP : 0;
  238. }
  239. retry:
  240. status = ocrdma_mbx_alloc_pd(dev, pd);
  241. if (status) {
  242. if (pd->dpp_enabled) {
  243. pd->dpp_enabled = false;
  244. pd->num_dpp_qp = 0;
  245. goto retry;
  246. } else {
  247. kfree(pd);
  248. return ERR_PTR(status);
  249. }
  250. }
  251. return pd;
  252. }
  253. static inline int is_ucontext_pd(struct ocrdma_ucontext *uctx,
  254. struct ocrdma_pd *pd)
  255. {
  256. return (uctx->cntxt_pd == pd ? true : false);
  257. }
  258. static int _ocrdma_dealloc_pd(struct ocrdma_dev *dev,
  259. struct ocrdma_pd *pd)
  260. {
  261. int status = 0;
  262. status = ocrdma_mbx_dealloc_pd(dev, pd);
  263. kfree(pd);
  264. return status;
  265. }
  266. static int ocrdma_alloc_ucontext_pd(struct ocrdma_dev *dev,
  267. struct ocrdma_ucontext *uctx,
  268. struct ib_udata *udata)
  269. {
  270. int status = 0;
  271. uctx->cntxt_pd = _ocrdma_alloc_pd(dev, uctx, udata);
  272. if (IS_ERR(uctx->cntxt_pd)) {
  273. status = PTR_ERR(uctx->cntxt_pd);
  274. uctx->cntxt_pd = NULL;
  275. goto err;
  276. }
  277. uctx->cntxt_pd->uctx = uctx;
  278. uctx->cntxt_pd->ibpd.device = &dev->ibdev;
  279. err:
  280. return status;
  281. }
  282. static int ocrdma_dealloc_ucontext_pd(struct ocrdma_ucontext *uctx)
  283. {
  284. int status = 0;
  285. struct ocrdma_pd *pd = uctx->cntxt_pd;
  286. struct ocrdma_dev *dev = get_ocrdma_dev(pd->ibpd.device);
  287. BUG_ON(uctx->pd_in_use);
  288. uctx->cntxt_pd = NULL;
  289. status = _ocrdma_dealloc_pd(dev, pd);
  290. return status;
  291. }
  292. static struct ocrdma_pd *ocrdma_get_ucontext_pd(struct ocrdma_ucontext *uctx)
  293. {
  294. struct ocrdma_pd *pd = NULL;
  295. mutex_lock(&uctx->mm_list_lock);
  296. if (!uctx->pd_in_use) {
  297. uctx->pd_in_use = true;
  298. pd = uctx->cntxt_pd;
  299. }
  300. mutex_unlock(&uctx->mm_list_lock);
  301. return pd;
  302. }
  303. static void ocrdma_release_ucontext_pd(struct ocrdma_ucontext *uctx)
  304. {
  305. mutex_lock(&uctx->mm_list_lock);
  306. uctx->pd_in_use = false;
  307. mutex_unlock(&uctx->mm_list_lock);
  308. }
  309. struct ib_ucontext *ocrdma_alloc_ucontext(struct ib_device *ibdev,
  310. struct ib_udata *udata)
  311. {
  312. int status;
  313. struct ocrdma_ucontext *ctx;
  314. struct ocrdma_alloc_ucontext_resp resp;
  315. struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
  316. struct pci_dev *pdev = dev->nic_info.pdev;
  317. u32 map_len = roundup(sizeof(u32) * 2048, PAGE_SIZE);
  318. if (!udata)
  319. return ERR_PTR(-EFAULT);
  320. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  321. if (!ctx)
  322. return ERR_PTR(-ENOMEM);
  323. INIT_LIST_HEAD(&ctx->mm_head);
  324. mutex_init(&ctx->mm_list_lock);
  325. ctx->ah_tbl.va = dma_alloc_coherent(&pdev->dev, map_len,
  326. &ctx->ah_tbl.pa, GFP_KERNEL);
  327. if (!ctx->ah_tbl.va) {
  328. kfree(ctx);
  329. return ERR_PTR(-ENOMEM);
  330. }
  331. memset(ctx->ah_tbl.va, 0, map_len);
  332. ctx->ah_tbl.len = map_len;
  333. memset(&resp, 0, sizeof(resp));
  334. resp.ah_tbl_len = ctx->ah_tbl.len;
  335. resp.ah_tbl_page = ctx->ah_tbl.pa;
  336. status = ocrdma_add_mmap(ctx, resp.ah_tbl_page, resp.ah_tbl_len);
  337. if (status)
  338. goto map_err;
  339. status = ocrdma_alloc_ucontext_pd(dev, ctx, udata);
  340. if (status)
  341. goto pd_err;
  342. resp.dev_id = dev->id;
  343. resp.max_inline_data = dev->attr.max_inline_data;
  344. resp.wqe_size = dev->attr.wqe_size;
  345. resp.rqe_size = dev->attr.rqe_size;
  346. resp.dpp_wqe_size = dev->attr.wqe_size;
  347. memcpy(resp.fw_ver, dev->attr.fw_ver, sizeof(resp.fw_ver));
  348. status = ib_copy_to_udata(udata, &resp, sizeof(resp));
  349. if (status)
  350. goto cpy_err;
  351. return &ctx->ibucontext;
  352. cpy_err:
  353. pd_err:
  354. ocrdma_del_mmap(ctx, ctx->ah_tbl.pa, ctx->ah_tbl.len);
  355. map_err:
  356. dma_free_coherent(&pdev->dev, ctx->ah_tbl.len, ctx->ah_tbl.va,
  357. ctx->ah_tbl.pa);
  358. kfree(ctx);
  359. return ERR_PTR(status);
  360. }
  361. int ocrdma_dealloc_ucontext(struct ib_ucontext *ibctx)
  362. {
  363. int status = 0;
  364. struct ocrdma_mm *mm, *tmp;
  365. struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ibctx);
  366. struct ocrdma_dev *dev = get_ocrdma_dev(ibctx->device);
  367. struct pci_dev *pdev = dev->nic_info.pdev;
  368. status = ocrdma_dealloc_ucontext_pd(uctx);
  369. ocrdma_del_mmap(uctx, uctx->ah_tbl.pa, uctx->ah_tbl.len);
  370. dma_free_coherent(&pdev->dev, uctx->ah_tbl.len, uctx->ah_tbl.va,
  371. uctx->ah_tbl.pa);
  372. list_for_each_entry_safe(mm, tmp, &uctx->mm_head, entry) {
  373. list_del(&mm->entry);
  374. kfree(mm);
  375. }
  376. kfree(uctx);
  377. return status;
  378. }
  379. int ocrdma_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
  380. {
  381. struct ocrdma_ucontext *ucontext = get_ocrdma_ucontext(context);
  382. struct ocrdma_dev *dev = get_ocrdma_dev(context->device);
  383. unsigned long vm_page = vma->vm_pgoff << PAGE_SHIFT;
  384. u64 unmapped_db = (u64) dev->nic_info.unmapped_db;
  385. unsigned long len = (vma->vm_end - vma->vm_start);
  386. int status = 0;
  387. bool found;
  388. if (vma->vm_start & (PAGE_SIZE - 1))
  389. return -EINVAL;
  390. found = ocrdma_search_mmap(ucontext, vma->vm_pgoff << PAGE_SHIFT, len);
  391. if (!found)
  392. return -EINVAL;
  393. if ((vm_page >= unmapped_db) && (vm_page <= (unmapped_db +
  394. dev->nic_info.db_total_size)) &&
  395. (len <= dev->nic_info.db_page_size)) {
  396. if (vma->vm_flags & VM_READ)
  397. return -EPERM;
  398. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  399. status = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  400. len, vma->vm_page_prot);
  401. } else if (dev->nic_info.dpp_unmapped_len &&
  402. (vm_page >= (u64) dev->nic_info.dpp_unmapped_addr) &&
  403. (vm_page <= (u64) (dev->nic_info.dpp_unmapped_addr +
  404. dev->nic_info.dpp_unmapped_len)) &&
  405. (len <= dev->nic_info.dpp_unmapped_len)) {
  406. if (vma->vm_flags & VM_READ)
  407. return -EPERM;
  408. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  409. status = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  410. len, vma->vm_page_prot);
  411. } else {
  412. status = remap_pfn_range(vma, vma->vm_start,
  413. vma->vm_pgoff, len, vma->vm_page_prot);
  414. }
  415. return status;
  416. }
  417. static int ocrdma_copy_pd_uresp(struct ocrdma_dev *dev, struct ocrdma_pd *pd,
  418. struct ib_ucontext *ib_ctx,
  419. struct ib_udata *udata)
  420. {
  421. int status;
  422. u64 db_page_addr;
  423. u64 dpp_page_addr = 0;
  424. u32 db_page_size;
  425. struct ocrdma_alloc_pd_uresp rsp;
  426. struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ib_ctx);
  427. memset(&rsp, 0, sizeof(rsp));
  428. rsp.id = pd->id;
  429. rsp.dpp_enabled = pd->dpp_enabled;
  430. db_page_addr = ocrdma_get_db_addr(dev, pd->id);
  431. db_page_size = dev->nic_info.db_page_size;
  432. status = ocrdma_add_mmap(uctx, db_page_addr, db_page_size);
  433. if (status)
  434. return status;
  435. if (pd->dpp_enabled) {
  436. dpp_page_addr = dev->nic_info.dpp_unmapped_addr +
  437. (pd->id * PAGE_SIZE);
  438. status = ocrdma_add_mmap(uctx, dpp_page_addr,
  439. PAGE_SIZE);
  440. if (status)
  441. goto dpp_map_err;
  442. rsp.dpp_page_addr_hi = upper_32_bits(dpp_page_addr);
  443. rsp.dpp_page_addr_lo = dpp_page_addr;
  444. }
  445. status = ib_copy_to_udata(udata, &rsp, sizeof(rsp));
  446. if (status)
  447. goto ucopy_err;
  448. pd->uctx = uctx;
  449. return 0;
  450. ucopy_err:
  451. if (pd->dpp_enabled)
  452. ocrdma_del_mmap(pd->uctx, dpp_page_addr, PAGE_SIZE);
  453. dpp_map_err:
  454. ocrdma_del_mmap(pd->uctx, db_page_addr, db_page_size);
  455. return status;
  456. }
  457. struct ib_pd *ocrdma_alloc_pd(struct ib_device *ibdev,
  458. struct ib_ucontext *context,
  459. struct ib_udata *udata)
  460. {
  461. struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
  462. struct ocrdma_pd *pd;
  463. struct ocrdma_ucontext *uctx = NULL;
  464. int status;
  465. u8 is_uctx_pd = false;
  466. if (udata && context) {
  467. uctx = get_ocrdma_ucontext(context);
  468. pd = ocrdma_get_ucontext_pd(uctx);
  469. if (pd) {
  470. is_uctx_pd = true;
  471. goto pd_mapping;
  472. }
  473. }
  474. pd = _ocrdma_alloc_pd(dev, uctx, udata);
  475. if (IS_ERR(pd)) {
  476. status = PTR_ERR(pd);
  477. goto exit;
  478. }
  479. pd_mapping:
  480. if (udata && context) {
  481. status = ocrdma_copy_pd_uresp(dev, pd, context, udata);
  482. if (status)
  483. goto err;
  484. }
  485. return &pd->ibpd;
  486. err:
  487. if (is_uctx_pd) {
  488. ocrdma_release_ucontext_pd(uctx);
  489. } else {
  490. status = ocrdma_mbx_dealloc_pd(dev, pd);
  491. kfree(pd);
  492. }
  493. exit:
  494. return ERR_PTR(status);
  495. }
  496. int ocrdma_dealloc_pd(struct ib_pd *ibpd)
  497. {
  498. struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
  499. struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
  500. struct ocrdma_ucontext *uctx = NULL;
  501. int status = 0;
  502. u64 usr_db;
  503. uctx = pd->uctx;
  504. if (uctx) {
  505. u64 dpp_db = dev->nic_info.dpp_unmapped_addr +
  506. (pd->id * PAGE_SIZE);
  507. if (pd->dpp_enabled)
  508. ocrdma_del_mmap(pd->uctx, dpp_db, PAGE_SIZE);
  509. usr_db = ocrdma_get_db_addr(dev, pd->id);
  510. ocrdma_del_mmap(pd->uctx, usr_db, dev->nic_info.db_page_size);
  511. if (is_ucontext_pd(uctx, pd)) {
  512. ocrdma_release_ucontext_pd(uctx);
  513. return status;
  514. }
  515. }
  516. status = _ocrdma_dealloc_pd(dev, pd);
  517. return status;
  518. }
  519. static int ocrdma_alloc_lkey(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
  520. u32 pdid, int acc, u32 num_pbls, u32 addr_check)
  521. {
  522. int status;
  523. mr->hwmr.fr_mr = 0;
  524. mr->hwmr.local_rd = 1;
  525. mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
  526. mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
  527. mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
  528. mr->hwmr.mw_bind = (acc & IB_ACCESS_MW_BIND) ? 1 : 0;
  529. mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
  530. mr->hwmr.num_pbls = num_pbls;
  531. status = ocrdma_mbx_alloc_lkey(dev, &mr->hwmr, pdid, addr_check);
  532. if (status)
  533. return status;
  534. mr->ibmr.lkey = mr->hwmr.lkey;
  535. if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
  536. mr->ibmr.rkey = mr->hwmr.lkey;
  537. return 0;
  538. }
  539. struct ib_mr *ocrdma_get_dma_mr(struct ib_pd *ibpd, int acc)
  540. {
  541. int status;
  542. struct ocrdma_mr *mr;
  543. struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
  544. struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
  545. if (acc & IB_ACCESS_REMOTE_WRITE && !(acc & IB_ACCESS_LOCAL_WRITE)) {
  546. pr_err("%s err, invalid access rights\n", __func__);
  547. return ERR_PTR(-EINVAL);
  548. }
  549. mr = kzalloc(sizeof(*mr), GFP_KERNEL);
  550. if (!mr)
  551. return ERR_PTR(-ENOMEM);
  552. status = ocrdma_alloc_lkey(dev, mr, pd->id, acc, 0,
  553. OCRDMA_ADDR_CHECK_DISABLE);
  554. if (status) {
  555. kfree(mr);
  556. return ERR_PTR(status);
  557. }
  558. return &mr->ibmr;
  559. }
  560. static void ocrdma_free_mr_pbl_tbl(struct ocrdma_dev *dev,
  561. struct ocrdma_hw_mr *mr)
  562. {
  563. struct pci_dev *pdev = dev->nic_info.pdev;
  564. int i = 0;
  565. if (mr->pbl_table) {
  566. for (i = 0; i < mr->num_pbls; i++) {
  567. if (!mr->pbl_table[i].va)
  568. continue;
  569. dma_free_coherent(&pdev->dev, mr->pbl_size,
  570. mr->pbl_table[i].va,
  571. mr->pbl_table[i].pa);
  572. }
  573. kfree(mr->pbl_table);
  574. mr->pbl_table = NULL;
  575. }
  576. }
  577. static int ocrdma_get_pbl_info(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
  578. u32 num_pbes)
  579. {
  580. u32 num_pbls = 0;
  581. u32 idx = 0;
  582. int status = 0;
  583. u32 pbl_size;
  584. do {
  585. pbl_size = OCRDMA_MIN_HPAGE_SIZE * (1 << idx);
  586. if (pbl_size > MAX_OCRDMA_PBL_SIZE) {
  587. status = -EFAULT;
  588. break;
  589. }
  590. num_pbls = roundup(num_pbes, (pbl_size / sizeof(u64)));
  591. num_pbls = num_pbls / (pbl_size / sizeof(u64));
  592. idx++;
  593. } while (num_pbls >= dev->attr.max_num_mr_pbl);
  594. mr->hwmr.num_pbes = num_pbes;
  595. mr->hwmr.num_pbls = num_pbls;
  596. mr->hwmr.pbl_size = pbl_size;
  597. return status;
  598. }
  599. static int ocrdma_build_pbl_tbl(struct ocrdma_dev *dev, struct ocrdma_hw_mr *mr)
  600. {
  601. int status = 0;
  602. int i;
  603. u32 dma_len = mr->pbl_size;
  604. struct pci_dev *pdev = dev->nic_info.pdev;
  605. void *va;
  606. dma_addr_t pa;
  607. mr->pbl_table = kzalloc(sizeof(struct ocrdma_pbl) *
  608. mr->num_pbls, GFP_KERNEL);
  609. if (!mr->pbl_table)
  610. return -ENOMEM;
  611. for (i = 0; i < mr->num_pbls; i++) {
  612. va = dma_alloc_coherent(&pdev->dev, dma_len, &pa, GFP_KERNEL);
  613. if (!va) {
  614. ocrdma_free_mr_pbl_tbl(dev, mr);
  615. status = -ENOMEM;
  616. break;
  617. }
  618. memset(va, 0, dma_len);
  619. mr->pbl_table[i].va = va;
  620. mr->pbl_table[i].pa = pa;
  621. }
  622. return status;
  623. }
  624. static void build_user_pbes(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
  625. u32 num_pbes)
  626. {
  627. struct ocrdma_pbe *pbe;
  628. struct ib_umem_chunk *chunk;
  629. struct ocrdma_pbl *pbl_tbl = mr->hwmr.pbl_table;
  630. struct ib_umem *umem = mr->umem;
  631. int i, shift, pg_cnt, pages, pbe_cnt, total_num_pbes = 0;
  632. if (!mr->hwmr.num_pbes)
  633. return;
  634. pbe = (struct ocrdma_pbe *)pbl_tbl->va;
  635. pbe_cnt = 0;
  636. shift = ilog2(umem->page_size);
  637. list_for_each_entry(chunk, &umem->chunk_list, list) {
  638. /* get all the dma regions from the chunk. */
  639. for (i = 0; i < chunk->nmap; i++) {
  640. pages = sg_dma_len(&chunk->page_list[i]) >> shift;
  641. for (pg_cnt = 0; pg_cnt < pages; pg_cnt++) {
  642. /* store the page address in pbe */
  643. pbe->pa_lo =
  644. cpu_to_le32(sg_dma_address
  645. (&chunk->page_list[i]) +
  646. (umem->page_size * pg_cnt));
  647. pbe->pa_hi =
  648. cpu_to_le32(upper_32_bits
  649. ((sg_dma_address
  650. (&chunk->page_list[i]) +
  651. umem->page_size * pg_cnt)));
  652. pbe_cnt += 1;
  653. total_num_pbes += 1;
  654. pbe++;
  655. /* if done building pbes, issue the mbx cmd. */
  656. if (total_num_pbes == num_pbes)
  657. return;
  658. /* if the given pbl is full storing the pbes,
  659. * move to next pbl.
  660. */
  661. if (pbe_cnt ==
  662. (mr->hwmr.pbl_size / sizeof(u64))) {
  663. pbl_tbl++;
  664. pbe = (struct ocrdma_pbe *)pbl_tbl->va;
  665. pbe_cnt = 0;
  666. }
  667. }
  668. }
  669. }
  670. }
  671. struct ib_mr *ocrdma_reg_user_mr(struct ib_pd *ibpd, u64 start, u64 len,
  672. u64 usr_addr, int acc, struct ib_udata *udata)
  673. {
  674. int status = -ENOMEM;
  675. struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
  676. struct ocrdma_mr *mr;
  677. struct ocrdma_pd *pd;
  678. u32 num_pbes;
  679. pd = get_ocrdma_pd(ibpd);
  680. if (acc & IB_ACCESS_REMOTE_WRITE && !(acc & IB_ACCESS_LOCAL_WRITE))
  681. return ERR_PTR(-EINVAL);
  682. mr = kzalloc(sizeof(*mr), GFP_KERNEL);
  683. if (!mr)
  684. return ERR_PTR(status);
  685. mr->umem = ib_umem_get(ibpd->uobject->context, start, len, acc, 0);
  686. if (IS_ERR(mr->umem)) {
  687. status = -EFAULT;
  688. goto umem_err;
  689. }
  690. num_pbes = ib_umem_page_count(mr->umem);
  691. status = ocrdma_get_pbl_info(dev, mr, num_pbes);
  692. if (status)
  693. goto umem_err;
  694. mr->hwmr.pbe_size = mr->umem->page_size;
  695. mr->hwmr.fbo = mr->umem->offset;
  696. mr->hwmr.va = usr_addr;
  697. mr->hwmr.len = len;
  698. mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
  699. mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
  700. mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
  701. mr->hwmr.local_rd = 1;
  702. mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
  703. status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
  704. if (status)
  705. goto umem_err;
  706. build_user_pbes(dev, mr, num_pbes);
  707. status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, acc);
  708. if (status)
  709. goto mbx_err;
  710. mr->ibmr.lkey = mr->hwmr.lkey;
  711. if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
  712. mr->ibmr.rkey = mr->hwmr.lkey;
  713. return &mr->ibmr;
  714. mbx_err:
  715. ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
  716. umem_err:
  717. kfree(mr);
  718. return ERR_PTR(status);
  719. }
  720. int ocrdma_dereg_mr(struct ib_mr *ib_mr)
  721. {
  722. struct ocrdma_mr *mr = get_ocrdma_mr(ib_mr);
  723. struct ocrdma_dev *dev = get_ocrdma_dev(ib_mr->device);
  724. int status;
  725. status = ocrdma_mbx_dealloc_lkey(dev, mr->hwmr.fr_mr, mr->hwmr.lkey);
  726. if (mr->hwmr.fr_mr == 0)
  727. ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
  728. /* it could be user registered memory. */
  729. if (mr->umem)
  730. ib_umem_release(mr->umem);
  731. kfree(mr);
  732. return status;
  733. }
  734. static int ocrdma_copy_cq_uresp(struct ocrdma_dev *dev, struct ocrdma_cq *cq,
  735. struct ib_udata *udata,
  736. struct ib_ucontext *ib_ctx)
  737. {
  738. int status;
  739. struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ib_ctx);
  740. struct ocrdma_create_cq_uresp uresp;
  741. memset(&uresp, 0, sizeof(uresp));
  742. uresp.cq_id = cq->id;
  743. uresp.page_size = PAGE_ALIGN(cq->len);
  744. uresp.num_pages = 1;
  745. uresp.max_hw_cqe = cq->max_hw_cqe;
  746. uresp.page_addr[0] = cq->pa;
  747. uresp.db_page_addr = ocrdma_get_db_addr(dev, uctx->cntxt_pd->id);
  748. uresp.db_page_size = dev->nic_info.db_page_size;
  749. uresp.phase_change = cq->phase_change ? 1 : 0;
  750. status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
  751. if (status) {
  752. pr_err("%s(%d) copy error cqid=0x%x.\n",
  753. __func__, dev->id, cq->id);
  754. goto err;
  755. }
  756. status = ocrdma_add_mmap(uctx, uresp.db_page_addr, uresp.db_page_size);
  757. if (status)
  758. goto err;
  759. status = ocrdma_add_mmap(uctx, uresp.page_addr[0], uresp.page_size);
  760. if (status) {
  761. ocrdma_del_mmap(uctx, uresp.db_page_addr, uresp.db_page_size);
  762. goto err;
  763. }
  764. cq->ucontext = uctx;
  765. err:
  766. return status;
  767. }
  768. struct ib_cq *ocrdma_create_cq(struct ib_device *ibdev, int entries, int vector,
  769. struct ib_ucontext *ib_ctx,
  770. struct ib_udata *udata)
  771. {
  772. struct ocrdma_cq *cq;
  773. struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
  774. struct ocrdma_ucontext *uctx = NULL;
  775. u16 pd_id = 0;
  776. int status;
  777. struct ocrdma_create_cq_ureq ureq;
  778. if (udata) {
  779. if (ib_copy_from_udata(&ureq, udata, sizeof(ureq)))
  780. return ERR_PTR(-EFAULT);
  781. } else
  782. ureq.dpp_cq = 0;
  783. cq = kzalloc(sizeof(*cq), GFP_KERNEL);
  784. if (!cq)
  785. return ERR_PTR(-ENOMEM);
  786. spin_lock_init(&cq->cq_lock);
  787. spin_lock_init(&cq->comp_handler_lock);
  788. INIT_LIST_HEAD(&cq->sq_head);
  789. INIT_LIST_HEAD(&cq->rq_head);
  790. if (ib_ctx) {
  791. uctx = get_ocrdma_ucontext(ib_ctx);
  792. pd_id = uctx->cntxt_pd->id;
  793. }
  794. status = ocrdma_mbx_create_cq(dev, cq, entries, ureq.dpp_cq, pd_id);
  795. if (status) {
  796. kfree(cq);
  797. return ERR_PTR(status);
  798. }
  799. if (ib_ctx) {
  800. status = ocrdma_copy_cq_uresp(dev, cq, udata, ib_ctx);
  801. if (status)
  802. goto ctx_err;
  803. }
  804. cq->phase = OCRDMA_CQE_VALID;
  805. cq->arm_needed = true;
  806. dev->cq_tbl[cq->id] = cq;
  807. return &cq->ibcq;
  808. ctx_err:
  809. ocrdma_mbx_destroy_cq(dev, cq);
  810. kfree(cq);
  811. return ERR_PTR(status);
  812. }
  813. int ocrdma_resize_cq(struct ib_cq *ibcq, int new_cnt,
  814. struct ib_udata *udata)
  815. {
  816. int status = 0;
  817. struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
  818. if (new_cnt < 1 || new_cnt > cq->max_hw_cqe) {
  819. status = -EINVAL;
  820. return status;
  821. }
  822. ibcq->cqe = new_cnt;
  823. return status;
  824. }
  825. int ocrdma_destroy_cq(struct ib_cq *ibcq)
  826. {
  827. int status;
  828. struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
  829. struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
  830. int pdid = 0;
  831. status = ocrdma_mbx_destroy_cq(dev, cq);
  832. if (cq->ucontext) {
  833. pdid = cq->ucontext->cntxt_pd->id;
  834. ocrdma_del_mmap(cq->ucontext, (u64) cq->pa,
  835. PAGE_ALIGN(cq->len));
  836. ocrdma_del_mmap(cq->ucontext,
  837. ocrdma_get_db_addr(dev, pdid),
  838. dev->nic_info.db_page_size);
  839. }
  840. dev->cq_tbl[cq->id] = NULL;
  841. kfree(cq);
  842. return status;
  843. }
  844. static int ocrdma_add_qpn_map(struct ocrdma_dev *dev, struct ocrdma_qp *qp)
  845. {
  846. int status = -EINVAL;
  847. if (qp->id < OCRDMA_MAX_QP && dev->qp_tbl[qp->id] == NULL) {
  848. dev->qp_tbl[qp->id] = qp;
  849. status = 0;
  850. }
  851. return status;
  852. }
  853. static void ocrdma_del_qpn_map(struct ocrdma_dev *dev, struct ocrdma_qp *qp)
  854. {
  855. dev->qp_tbl[qp->id] = NULL;
  856. }
  857. static int ocrdma_check_qp_params(struct ib_pd *ibpd, struct ocrdma_dev *dev,
  858. struct ib_qp_init_attr *attrs)
  859. {
  860. if ((attrs->qp_type != IB_QPT_GSI) &&
  861. (attrs->qp_type != IB_QPT_RC) &&
  862. (attrs->qp_type != IB_QPT_UC) &&
  863. (attrs->qp_type != IB_QPT_UD)) {
  864. pr_err("%s(%d) unsupported qp type=0x%x requested\n",
  865. __func__, dev->id, attrs->qp_type);
  866. return -EINVAL;
  867. }
  868. /* Skip the check for QP1 to support CM size of 128 */
  869. if ((attrs->qp_type != IB_QPT_GSI) &&
  870. (attrs->cap.max_send_wr > dev->attr.max_wqe)) {
  871. pr_err("%s(%d) unsupported send_wr=0x%x requested\n",
  872. __func__, dev->id, attrs->cap.max_send_wr);
  873. pr_err("%s(%d) supported send_wr=0x%x\n",
  874. __func__, dev->id, dev->attr.max_wqe);
  875. return -EINVAL;
  876. }
  877. if (!attrs->srq && (attrs->cap.max_recv_wr > dev->attr.max_rqe)) {
  878. pr_err("%s(%d) unsupported recv_wr=0x%x requested\n",
  879. __func__, dev->id, attrs->cap.max_recv_wr);
  880. pr_err("%s(%d) supported recv_wr=0x%x\n",
  881. __func__, dev->id, dev->attr.max_rqe);
  882. return -EINVAL;
  883. }
  884. if (attrs->cap.max_inline_data > dev->attr.max_inline_data) {
  885. pr_err("%s(%d) unsupported inline data size=0x%x requested\n",
  886. __func__, dev->id, attrs->cap.max_inline_data);
  887. pr_err("%s(%d) supported inline data size=0x%x\n",
  888. __func__, dev->id, dev->attr.max_inline_data);
  889. return -EINVAL;
  890. }
  891. if (attrs->cap.max_send_sge > dev->attr.max_send_sge) {
  892. pr_err("%s(%d) unsupported send_sge=0x%x requested\n",
  893. __func__, dev->id, attrs->cap.max_send_sge);
  894. pr_err("%s(%d) supported send_sge=0x%x\n",
  895. __func__, dev->id, dev->attr.max_send_sge);
  896. return -EINVAL;
  897. }
  898. if (attrs->cap.max_recv_sge > dev->attr.max_recv_sge) {
  899. pr_err("%s(%d) unsupported recv_sge=0x%x requested\n",
  900. __func__, dev->id, attrs->cap.max_recv_sge);
  901. pr_err("%s(%d) supported recv_sge=0x%x\n",
  902. __func__, dev->id, dev->attr.max_recv_sge);
  903. return -EINVAL;
  904. }
  905. /* unprivileged user space cannot create special QP */
  906. if (ibpd->uobject && attrs->qp_type == IB_QPT_GSI) {
  907. pr_err
  908. ("%s(%d) Userspace can't create special QPs of type=0x%x\n",
  909. __func__, dev->id, attrs->qp_type);
  910. return -EINVAL;
  911. }
  912. /* allow creating only one GSI type of QP */
  913. if (attrs->qp_type == IB_QPT_GSI && dev->gsi_qp_created) {
  914. pr_err("%s(%d) GSI special QPs already created.\n",
  915. __func__, dev->id);
  916. return -EINVAL;
  917. }
  918. /* verify consumer QPs are not trying to use GSI QP's CQ */
  919. if ((attrs->qp_type != IB_QPT_GSI) && (dev->gsi_qp_created)) {
  920. if ((dev->gsi_sqcq == get_ocrdma_cq(attrs->send_cq)) ||
  921. (dev->gsi_rqcq == get_ocrdma_cq(attrs->recv_cq))) {
  922. pr_err("%s(%d) Consumer QP cannot use GSI CQs.\n",
  923. __func__, dev->id);
  924. return -EINVAL;
  925. }
  926. }
  927. return 0;
  928. }
  929. static int ocrdma_copy_qp_uresp(struct ocrdma_qp *qp,
  930. struct ib_udata *udata, int dpp_offset,
  931. int dpp_credit_lmt, int srq)
  932. {
  933. int status = 0;
  934. u64 usr_db;
  935. struct ocrdma_create_qp_uresp uresp;
  936. struct ocrdma_dev *dev = qp->dev;
  937. struct ocrdma_pd *pd = qp->pd;
  938. memset(&uresp, 0, sizeof(uresp));
  939. usr_db = dev->nic_info.unmapped_db +
  940. (pd->id * dev->nic_info.db_page_size);
  941. uresp.qp_id = qp->id;
  942. uresp.sq_dbid = qp->sq.dbid;
  943. uresp.num_sq_pages = 1;
  944. uresp.sq_page_size = PAGE_ALIGN(qp->sq.len);
  945. uresp.sq_page_addr[0] = qp->sq.pa;
  946. uresp.num_wqe_allocated = qp->sq.max_cnt;
  947. if (!srq) {
  948. uresp.rq_dbid = qp->rq.dbid;
  949. uresp.num_rq_pages = 1;
  950. uresp.rq_page_size = PAGE_ALIGN(qp->rq.len);
  951. uresp.rq_page_addr[0] = qp->rq.pa;
  952. uresp.num_rqe_allocated = qp->rq.max_cnt;
  953. }
  954. uresp.db_page_addr = usr_db;
  955. uresp.db_page_size = dev->nic_info.db_page_size;
  956. if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) {
  957. uresp.db_sq_offset = OCRDMA_DB_GEN2_SQ_OFFSET;
  958. uresp.db_rq_offset = OCRDMA_DB_GEN2_RQ_OFFSET;
  959. uresp.db_shift = 24;
  960. } else {
  961. uresp.db_sq_offset = OCRDMA_DB_SQ_OFFSET;
  962. uresp.db_rq_offset = OCRDMA_DB_RQ_OFFSET;
  963. uresp.db_shift = 16;
  964. }
  965. if (qp->dpp_enabled) {
  966. uresp.dpp_credit = dpp_credit_lmt;
  967. uresp.dpp_offset = dpp_offset;
  968. }
  969. status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
  970. if (status) {
  971. pr_err("%s(%d) user copy error.\n", __func__, dev->id);
  972. goto err;
  973. }
  974. status = ocrdma_add_mmap(pd->uctx, uresp.sq_page_addr[0],
  975. uresp.sq_page_size);
  976. if (status)
  977. goto err;
  978. if (!srq) {
  979. status = ocrdma_add_mmap(pd->uctx, uresp.rq_page_addr[0],
  980. uresp.rq_page_size);
  981. if (status)
  982. goto rq_map_err;
  983. }
  984. return status;
  985. rq_map_err:
  986. ocrdma_del_mmap(pd->uctx, uresp.sq_page_addr[0], uresp.sq_page_size);
  987. err:
  988. return status;
  989. }
  990. static void ocrdma_set_qp_db(struct ocrdma_dev *dev, struct ocrdma_qp *qp,
  991. struct ocrdma_pd *pd)
  992. {
  993. if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) {
  994. qp->sq_db = dev->nic_info.db +
  995. (pd->id * dev->nic_info.db_page_size) +
  996. OCRDMA_DB_GEN2_SQ_OFFSET;
  997. qp->rq_db = dev->nic_info.db +
  998. (pd->id * dev->nic_info.db_page_size) +
  999. OCRDMA_DB_GEN2_RQ_OFFSET;
  1000. } else {
  1001. qp->sq_db = dev->nic_info.db +
  1002. (pd->id * dev->nic_info.db_page_size) +
  1003. OCRDMA_DB_SQ_OFFSET;
  1004. qp->rq_db = dev->nic_info.db +
  1005. (pd->id * dev->nic_info.db_page_size) +
  1006. OCRDMA_DB_RQ_OFFSET;
  1007. }
  1008. }
  1009. static int ocrdma_alloc_wr_id_tbl(struct ocrdma_qp *qp)
  1010. {
  1011. qp->wqe_wr_id_tbl =
  1012. kzalloc(sizeof(*(qp->wqe_wr_id_tbl)) * qp->sq.max_cnt,
  1013. GFP_KERNEL);
  1014. if (qp->wqe_wr_id_tbl == NULL)
  1015. return -ENOMEM;
  1016. qp->rqe_wr_id_tbl =
  1017. kzalloc(sizeof(u64) * qp->rq.max_cnt, GFP_KERNEL);
  1018. if (qp->rqe_wr_id_tbl == NULL)
  1019. return -ENOMEM;
  1020. return 0;
  1021. }
  1022. static void ocrdma_set_qp_init_params(struct ocrdma_qp *qp,
  1023. struct ocrdma_pd *pd,
  1024. struct ib_qp_init_attr *attrs)
  1025. {
  1026. qp->pd = pd;
  1027. spin_lock_init(&qp->q_lock);
  1028. INIT_LIST_HEAD(&qp->sq_entry);
  1029. INIT_LIST_HEAD(&qp->rq_entry);
  1030. qp->qp_type = attrs->qp_type;
  1031. qp->cap_flags = OCRDMA_QP_INB_RD | OCRDMA_QP_INB_WR;
  1032. qp->max_inline_data = attrs->cap.max_inline_data;
  1033. qp->sq.max_sges = attrs->cap.max_send_sge;
  1034. qp->rq.max_sges = attrs->cap.max_recv_sge;
  1035. qp->state = OCRDMA_QPS_RST;
  1036. qp->signaled = (attrs->sq_sig_type == IB_SIGNAL_ALL_WR) ? true : false;
  1037. }
  1038. static void ocrdma_store_gsi_qp_cq(struct ocrdma_dev *dev,
  1039. struct ib_qp_init_attr *attrs)
  1040. {
  1041. if (attrs->qp_type == IB_QPT_GSI) {
  1042. dev->gsi_qp_created = 1;
  1043. dev->gsi_sqcq = get_ocrdma_cq(attrs->send_cq);
  1044. dev->gsi_rqcq = get_ocrdma_cq(attrs->recv_cq);
  1045. }
  1046. }
  1047. struct ib_qp *ocrdma_create_qp(struct ib_pd *ibpd,
  1048. struct ib_qp_init_attr *attrs,
  1049. struct ib_udata *udata)
  1050. {
  1051. int status;
  1052. struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
  1053. struct ocrdma_qp *qp;
  1054. struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
  1055. struct ocrdma_create_qp_ureq ureq;
  1056. u16 dpp_credit_lmt, dpp_offset;
  1057. status = ocrdma_check_qp_params(ibpd, dev, attrs);
  1058. if (status)
  1059. goto gen_err;
  1060. memset(&ureq, 0, sizeof(ureq));
  1061. if (udata) {
  1062. if (ib_copy_from_udata(&ureq, udata, sizeof(ureq)))
  1063. return ERR_PTR(-EFAULT);
  1064. }
  1065. qp = kzalloc(sizeof(*qp), GFP_KERNEL);
  1066. if (!qp) {
  1067. status = -ENOMEM;
  1068. goto gen_err;
  1069. }
  1070. qp->dev = dev;
  1071. ocrdma_set_qp_init_params(qp, pd, attrs);
  1072. if (udata == NULL)
  1073. qp->cap_flags |= (OCRDMA_QP_MW_BIND | OCRDMA_QP_LKEY0 |
  1074. OCRDMA_QP_FAST_REG);
  1075. mutex_lock(&dev->dev_lock);
  1076. status = ocrdma_mbx_create_qp(qp, attrs, ureq.enable_dpp_cq,
  1077. ureq.dpp_cq_id,
  1078. &dpp_offset, &dpp_credit_lmt);
  1079. if (status)
  1080. goto mbx_err;
  1081. /* user space QP's wr_id table are managed in library */
  1082. if (udata == NULL) {
  1083. status = ocrdma_alloc_wr_id_tbl(qp);
  1084. if (status)
  1085. goto map_err;
  1086. }
  1087. status = ocrdma_add_qpn_map(dev, qp);
  1088. if (status)
  1089. goto map_err;
  1090. ocrdma_set_qp_db(dev, qp, pd);
  1091. if (udata) {
  1092. status = ocrdma_copy_qp_uresp(qp, udata, dpp_offset,
  1093. dpp_credit_lmt,
  1094. (attrs->srq != NULL));
  1095. if (status)
  1096. goto cpy_err;
  1097. }
  1098. ocrdma_store_gsi_qp_cq(dev, attrs);
  1099. qp->ibqp.qp_num = qp->id;
  1100. mutex_unlock(&dev->dev_lock);
  1101. return &qp->ibqp;
  1102. cpy_err:
  1103. ocrdma_del_qpn_map(dev, qp);
  1104. map_err:
  1105. ocrdma_mbx_destroy_qp(dev, qp);
  1106. mbx_err:
  1107. mutex_unlock(&dev->dev_lock);
  1108. kfree(qp->wqe_wr_id_tbl);
  1109. kfree(qp->rqe_wr_id_tbl);
  1110. kfree(qp);
  1111. pr_err("%s(%d) error=%d\n", __func__, dev->id, status);
  1112. gen_err:
  1113. return ERR_PTR(status);
  1114. }
  1115. static void ocrdma_flush_rq_db(struct ocrdma_qp *qp)
  1116. {
  1117. if (qp->db_cache) {
  1118. u32 val = qp->rq.dbid | (qp->db_cache <<
  1119. ocrdma_get_num_posted_shift(qp));
  1120. iowrite32(val, qp->rq_db);
  1121. qp->db_cache = 0;
  1122. }
  1123. }
  1124. int _ocrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1125. int attr_mask)
  1126. {
  1127. int status = 0;
  1128. struct ocrdma_qp *qp;
  1129. struct ocrdma_dev *dev;
  1130. enum ib_qp_state old_qps;
  1131. qp = get_ocrdma_qp(ibqp);
  1132. dev = qp->dev;
  1133. if (attr_mask & IB_QP_STATE)
  1134. status = ocrdma_qp_state_change(qp, attr->qp_state, &old_qps);
  1135. /* if new and previous states are same hw doesn't need to
  1136. * know about it.
  1137. */
  1138. if (status < 0)
  1139. return status;
  1140. status = ocrdma_mbx_modify_qp(dev, qp, attr, attr_mask, old_qps);
  1141. if (!status && attr_mask & IB_QP_STATE && attr->qp_state == IB_QPS_RTR)
  1142. ocrdma_flush_rq_db(qp);
  1143. return status;
  1144. }
  1145. int ocrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1146. int attr_mask, struct ib_udata *udata)
  1147. {
  1148. unsigned long flags;
  1149. int status = -EINVAL;
  1150. struct ocrdma_qp *qp;
  1151. struct ocrdma_dev *dev;
  1152. enum ib_qp_state old_qps, new_qps;
  1153. qp = get_ocrdma_qp(ibqp);
  1154. dev = qp->dev;
  1155. /* syncronize with multiple context trying to change, retrive qps */
  1156. mutex_lock(&dev->dev_lock);
  1157. /* syncronize with wqe, rqe posting and cqe processing contexts */
  1158. spin_lock_irqsave(&qp->q_lock, flags);
  1159. old_qps = get_ibqp_state(qp->state);
  1160. if (attr_mask & IB_QP_STATE)
  1161. new_qps = attr->qp_state;
  1162. else
  1163. new_qps = old_qps;
  1164. spin_unlock_irqrestore(&qp->q_lock, flags);
  1165. if (!ib_modify_qp_is_ok(old_qps, new_qps, ibqp->qp_type, attr_mask)) {
  1166. pr_err("%s(%d) invalid attribute mask=0x%x specified for\n"
  1167. "qpn=0x%x of type=0x%x old_qps=0x%x, new_qps=0x%x\n",
  1168. __func__, dev->id, attr_mask, qp->id, ibqp->qp_type,
  1169. old_qps, new_qps);
  1170. goto param_err;
  1171. }
  1172. status = _ocrdma_modify_qp(ibqp, attr, attr_mask);
  1173. if (status > 0)
  1174. status = 0;
  1175. param_err:
  1176. mutex_unlock(&dev->dev_lock);
  1177. return status;
  1178. }
  1179. static enum ib_mtu ocrdma_mtu_int_to_enum(u16 mtu)
  1180. {
  1181. switch (mtu) {
  1182. case 256:
  1183. return IB_MTU_256;
  1184. case 512:
  1185. return IB_MTU_512;
  1186. case 1024:
  1187. return IB_MTU_1024;
  1188. case 2048:
  1189. return IB_MTU_2048;
  1190. case 4096:
  1191. return IB_MTU_4096;
  1192. default:
  1193. return IB_MTU_1024;
  1194. }
  1195. }
  1196. static int ocrdma_to_ib_qp_acc_flags(int qp_cap_flags)
  1197. {
  1198. int ib_qp_acc_flags = 0;
  1199. if (qp_cap_flags & OCRDMA_QP_INB_WR)
  1200. ib_qp_acc_flags |= IB_ACCESS_REMOTE_WRITE;
  1201. if (qp_cap_flags & OCRDMA_QP_INB_RD)
  1202. ib_qp_acc_flags |= IB_ACCESS_LOCAL_WRITE;
  1203. return ib_qp_acc_flags;
  1204. }
  1205. int ocrdma_query_qp(struct ib_qp *ibqp,
  1206. struct ib_qp_attr *qp_attr,
  1207. int attr_mask, struct ib_qp_init_attr *qp_init_attr)
  1208. {
  1209. int status;
  1210. u32 qp_state;
  1211. struct ocrdma_qp_params params;
  1212. struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
  1213. struct ocrdma_dev *dev = qp->dev;
  1214. memset(&params, 0, sizeof(params));
  1215. mutex_lock(&dev->dev_lock);
  1216. status = ocrdma_mbx_query_qp(dev, qp, &params);
  1217. mutex_unlock(&dev->dev_lock);
  1218. if (status)
  1219. goto mbx_err;
  1220. qp_attr->qp_state = get_ibqp_state(IB_QPS_INIT);
  1221. qp_attr->cur_qp_state = get_ibqp_state(IB_QPS_INIT);
  1222. qp_attr->path_mtu =
  1223. ocrdma_mtu_int_to_enum(params.path_mtu_pkey_indx &
  1224. OCRDMA_QP_PARAMS_PATH_MTU_MASK) >>
  1225. OCRDMA_QP_PARAMS_PATH_MTU_SHIFT;
  1226. qp_attr->path_mig_state = IB_MIG_MIGRATED;
  1227. qp_attr->rq_psn = params.hop_lmt_rq_psn & OCRDMA_QP_PARAMS_RQ_PSN_MASK;
  1228. qp_attr->sq_psn = params.tclass_sq_psn & OCRDMA_QP_PARAMS_SQ_PSN_MASK;
  1229. qp_attr->dest_qp_num =
  1230. params.ack_to_rnr_rtc_dest_qpn & OCRDMA_QP_PARAMS_DEST_QPN_MASK;
  1231. qp_attr->qp_access_flags = ocrdma_to_ib_qp_acc_flags(qp->cap_flags);
  1232. qp_attr->cap.max_send_wr = qp->sq.max_cnt - 1;
  1233. qp_attr->cap.max_recv_wr = qp->rq.max_cnt - 1;
  1234. qp_attr->cap.max_send_sge = qp->sq.max_sges;
  1235. qp_attr->cap.max_recv_sge = qp->rq.max_sges;
  1236. qp_attr->cap.max_inline_data = qp->max_inline_data;
  1237. qp_init_attr->cap = qp_attr->cap;
  1238. memcpy(&qp_attr->ah_attr.grh.dgid, &params.dgid[0],
  1239. sizeof(params.dgid));
  1240. qp_attr->ah_attr.grh.flow_label = params.rnt_rc_sl_fl &
  1241. OCRDMA_QP_PARAMS_FLOW_LABEL_MASK;
  1242. qp_attr->ah_attr.grh.sgid_index = qp->sgid_idx;
  1243. qp_attr->ah_attr.grh.hop_limit = (params.hop_lmt_rq_psn &
  1244. OCRDMA_QP_PARAMS_HOP_LMT_MASK) >>
  1245. OCRDMA_QP_PARAMS_HOP_LMT_SHIFT;
  1246. qp_attr->ah_attr.grh.traffic_class = (params.tclass_sq_psn &
  1247. OCRDMA_QP_PARAMS_SQ_PSN_MASK) >>
  1248. OCRDMA_QP_PARAMS_TCLASS_SHIFT;
  1249. qp_attr->ah_attr.ah_flags = IB_AH_GRH;
  1250. qp_attr->ah_attr.port_num = 1;
  1251. qp_attr->ah_attr.sl = (params.rnt_rc_sl_fl &
  1252. OCRDMA_QP_PARAMS_SL_MASK) >>
  1253. OCRDMA_QP_PARAMS_SL_SHIFT;
  1254. qp_attr->timeout = (params.ack_to_rnr_rtc_dest_qpn &
  1255. OCRDMA_QP_PARAMS_ACK_TIMEOUT_MASK) >>
  1256. OCRDMA_QP_PARAMS_ACK_TIMEOUT_SHIFT;
  1257. qp_attr->rnr_retry = (params.ack_to_rnr_rtc_dest_qpn &
  1258. OCRDMA_QP_PARAMS_RNR_RETRY_CNT_MASK) >>
  1259. OCRDMA_QP_PARAMS_RNR_RETRY_CNT_SHIFT;
  1260. qp_attr->retry_cnt =
  1261. (params.rnt_rc_sl_fl & OCRDMA_QP_PARAMS_RETRY_CNT_MASK) >>
  1262. OCRDMA_QP_PARAMS_RETRY_CNT_SHIFT;
  1263. qp_attr->min_rnr_timer = 0;
  1264. qp_attr->pkey_index = 0;
  1265. qp_attr->port_num = 1;
  1266. qp_attr->ah_attr.src_path_bits = 0;
  1267. qp_attr->ah_attr.static_rate = 0;
  1268. qp_attr->alt_pkey_index = 0;
  1269. qp_attr->alt_port_num = 0;
  1270. qp_attr->alt_timeout = 0;
  1271. memset(&qp_attr->alt_ah_attr, 0, sizeof(qp_attr->alt_ah_attr));
  1272. qp_state = (params.max_sge_recv_flags & OCRDMA_QP_PARAMS_STATE_MASK) >>
  1273. OCRDMA_QP_PARAMS_STATE_SHIFT;
  1274. qp_attr->sq_draining = (qp_state == OCRDMA_QPS_SQ_DRAINING) ? 1 : 0;
  1275. qp_attr->max_dest_rd_atomic =
  1276. params.max_ord_ird >> OCRDMA_QP_PARAMS_MAX_ORD_SHIFT;
  1277. qp_attr->max_rd_atomic =
  1278. params.max_ord_ird & OCRDMA_QP_PARAMS_MAX_IRD_MASK;
  1279. qp_attr->en_sqd_async_notify = (params.max_sge_recv_flags &
  1280. OCRDMA_QP_PARAMS_FLAGS_SQD_ASYNC) ? 1 : 0;
  1281. mbx_err:
  1282. return status;
  1283. }
  1284. static void ocrdma_srq_toggle_bit(struct ocrdma_srq *srq, int idx)
  1285. {
  1286. int i = idx / 32;
  1287. unsigned int mask = (1 << (idx % 32));
  1288. if (srq->idx_bit_fields[i] & mask)
  1289. srq->idx_bit_fields[i] &= ~mask;
  1290. else
  1291. srq->idx_bit_fields[i] |= mask;
  1292. }
  1293. static int ocrdma_hwq_free_cnt(struct ocrdma_qp_hwq_info *q)
  1294. {
  1295. return ((q->max_wqe_idx - q->head) + q->tail) % q->max_cnt;
  1296. }
  1297. static int is_hw_sq_empty(struct ocrdma_qp *qp)
  1298. {
  1299. return (qp->sq.tail == qp->sq.head);
  1300. }
  1301. static int is_hw_rq_empty(struct ocrdma_qp *qp)
  1302. {
  1303. return (qp->rq.tail == qp->rq.head);
  1304. }
  1305. static void *ocrdma_hwq_head(struct ocrdma_qp_hwq_info *q)
  1306. {
  1307. return q->va + (q->head * q->entry_size);
  1308. }
  1309. static void *ocrdma_hwq_head_from_idx(struct ocrdma_qp_hwq_info *q,
  1310. u32 idx)
  1311. {
  1312. return q->va + (idx * q->entry_size);
  1313. }
  1314. static void ocrdma_hwq_inc_head(struct ocrdma_qp_hwq_info *q)
  1315. {
  1316. q->head = (q->head + 1) & q->max_wqe_idx;
  1317. }
  1318. static void ocrdma_hwq_inc_tail(struct ocrdma_qp_hwq_info *q)
  1319. {
  1320. q->tail = (q->tail + 1) & q->max_wqe_idx;
  1321. }
  1322. /* discard the cqe for a given QP */
  1323. static void ocrdma_discard_cqes(struct ocrdma_qp *qp, struct ocrdma_cq *cq)
  1324. {
  1325. unsigned long cq_flags;
  1326. unsigned long flags;
  1327. int discard_cnt = 0;
  1328. u32 cur_getp, stop_getp;
  1329. struct ocrdma_cqe *cqe;
  1330. u32 qpn = 0;
  1331. spin_lock_irqsave(&cq->cq_lock, cq_flags);
  1332. /* traverse through the CQEs in the hw CQ,
  1333. * find the matching CQE for a given qp,
  1334. * mark the matching one discarded by clearing qpn.
  1335. * ring the doorbell in the poll_cq() as
  1336. * we don't complete out of order cqe.
  1337. */
  1338. cur_getp = cq->getp;
  1339. /* find upto when do we reap the cq. */
  1340. stop_getp = cur_getp;
  1341. do {
  1342. if (is_hw_sq_empty(qp) && (!qp->srq && is_hw_rq_empty(qp)))
  1343. break;
  1344. cqe = cq->va + cur_getp;
  1345. /* if (a) done reaping whole hw cq, or
  1346. * (b) qp_xq becomes empty.
  1347. * then exit
  1348. */
  1349. qpn = cqe->cmn.qpn & OCRDMA_CQE_QPN_MASK;
  1350. /* if previously discarded cqe found, skip that too. */
  1351. /* check for matching qp */
  1352. if (qpn == 0 || qpn != qp->id)
  1353. goto skip_cqe;
  1354. /* mark cqe discarded so that it is not picked up later
  1355. * in the poll_cq().
  1356. */
  1357. discard_cnt += 1;
  1358. cqe->cmn.qpn = 0;
  1359. if (is_cqe_for_sq(cqe)) {
  1360. ocrdma_hwq_inc_tail(&qp->sq);
  1361. } else {
  1362. if (qp->srq) {
  1363. spin_lock_irqsave(&qp->srq->q_lock, flags);
  1364. ocrdma_hwq_inc_tail(&qp->srq->rq);
  1365. ocrdma_srq_toggle_bit(qp->srq, cur_getp);
  1366. spin_unlock_irqrestore(&qp->srq->q_lock, flags);
  1367. } else {
  1368. ocrdma_hwq_inc_tail(&qp->rq);
  1369. }
  1370. }
  1371. skip_cqe:
  1372. cur_getp = (cur_getp + 1) % cq->max_hw_cqe;
  1373. } while (cur_getp != stop_getp);
  1374. spin_unlock_irqrestore(&cq->cq_lock, cq_flags);
  1375. }
  1376. void ocrdma_del_flush_qp(struct ocrdma_qp *qp)
  1377. {
  1378. int found = false;
  1379. unsigned long flags;
  1380. struct ocrdma_dev *dev = qp->dev;
  1381. /* sync with any active CQ poll */
  1382. spin_lock_irqsave(&dev->flush_q_lock, flags);
  1383. found = ocrdma_is_qp_in_sq_flushlist(qp->sq_cq, qp);
  1384. if (found)
  1385. list_del(&qp->sq_entry);
  1386. if (!qp->srq) {
  1387. found = ocrdma_is_qp_in_rq_flushlist(qp->rq_cq, qp);
  1388. if (found)
  1389. list_del(&qp->rq_entry);
  1390. }
  1391. spin_unlock_irqrestore(&dev->flush_q_lock, flags);
  1392. }
  1393. int ocrdma_destroy_qp(struct ib_qp *ibqp)
  1394. {
  1395. int status;
  1396. struct ocrdma_pd *pd;
  1397. struct ocrdma_qp *qp;
  1398. struct ocrdma_dev *dev;
  1399. struct ib_qp_attr attrs;
  1400. int attr_mask = IB_QP_STATE;
  1401. unsigned long flags;
  1402. qp = get_ocrdma_qp(ibqp);
  1403. dev = qp->dev;
  1404. attrs.qp_state = IB_QPS_ERR;
  1405. pd = qp->pd;
  1406. /* change the QP state to ERROR */
  1407. _ocrdma_modify_qp(ibqp, &attrs, attr_mask);
  1408. /* ensure that CQEs for newly created QP (whose id may be same with
  1409. * one which just getting destroyed are same), dont get
  1410. * discarded until the old CQEs are discarded.
  1411. */
  1412. mutex_lock(&dev->dev_lock);
  1413. status = ocrdma_mbx_destroy_qp(dev, qp);
  1414. /*
  1415. * acquire CQ lock while destroy is in progress, in order to
  1416. * protect against proessing in-flight CQEs for this QP.
  1417. */
  1418. spin_lock_irqsave(&qp->sq_cq->cq_lock, flags);
  1419. if (qp->rq_cq && (qp->rq_cq != qp->sq_cq))
  1420. spin_lock(&qp->rq_cq->cq_lock);
  1421. ocrdma_del_qpn_map(dev, qp);
  1422. if (qp->rq_cq && (qp->rq_cq != qp->sq_cq))
  1423. spin_unlock(&qp->rq_cq->cq_lock);
  1424. spin_unlock_irqrestore(&qp->sq_cq->cq_lock, flags);
  1425. if (!pd->uctx) {
  1426. ocrdma_discard_cqes(qp, qp->sq_cq);
  1427. ocrdma_discard_cqes(qp, qp->rq_cq);
  1428. }
  1429. mutex_unlock(&dev->dev_lock);
  1430. if (pd->uctx) {
  1431. ocrdma_del_mmap(pd->uctx, (u64) qp->sq.pa,
  1432. PAGE_ALIGN(qp->sq.len));
  1433. if (!qp->srq)
  1434. ocrdma_del_mmap(pd->uctx, (u64) qp->rq.pa,
  1435. PAGE_ALIGN(qp->rq.len));
  1436. }
  1437. ocrdma_del_flush_qp(qp);
  1438. kfree(qp->wqe_wr_id_tbl);
  1439. kfree(qp->rqe_wr_id_tbl);
  1440. kfree(qp);
  1441. return status;
  1442. }
  1443. static int ocrdma_copy_srq_uresp(struct ocrdma_dev *dev, struct ocrdma_srq *srq,
  1444. struct ib_udata *udata)
  1445. {
  1446. int status;
  1447. struct ocrdma_create_srq_uresp uresp;
  1448. memset(&uresp, 0, sizeof(uresp));
  1449. uresp.rq_dbid = srq->rq.dbid;
  1450. uresp.num_rq_pages = 1;
  1451. uresp.rq_page_addr[0] = srq->rq.pa;
  1452. uresp.rq_page_size = srq->rq.len;
  1453. uresp.db_page_addr = dev->nic_info.unmapped_db +
  1454. (srq->pd->id * dev->nic_info.db_page_size);
  1455. uresp.db_page_size = dev->nic_info.db_page_size;
  1456. uresp.num_rqe_allocated = srq->rq.max_cnt;
  1457. if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) {
  1458. uresp.db_rq_offset = OCRDMA_DB_GEN2_RQ_OFFSET;
  1459. uresp.db_shift = 24;
  1460. } else {
  1461. uresp.db_rq_offset = OCRDMA_DB_RQ_OFFSET;
  1462. uresp.db_shift = 16;
  1463. }
  1464. status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
  1465. if (status)
  1466. return status;
  1467. status = ocrdma_add_mmap(srq->pd->uctx, uresp.rq_page_addr[0],
  1468. uresp.rq_page_size);
  1469. if (status)
  1470. return status;
  1471. return status;
  1472. }
  1473. struct ib_srq *ocrdma_create_srq(struct ib_pd *ibpd,
  1474. struct ib_srq_init_attr *init_attr,
  1475. struct ib_udata *udata)
  1476. {
  1477. int status = -ENOMEM;
  1478. struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
  1479. struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
  1480. struct ocrdma_srq *srq;
  1481. if (init_attr->attr.max_sge > dev->attr.max_recv_sge)
  1482. return ERR_PTR(-EINVAL);
  1483. if (init_attr->attr.max_wr > dev->attr.max_rqe)
  1484. return ERR_PTR(-EINVAL);
  1485. srq = kzalloc(sizeof(*srq), GFP_KERNEL);
  1486. if (!srq)
  1487. return ERR_PTR(status);
  1488. spin_lock_init(&srq->q_lock);
  1489. srq->pd = pd;
  1490. srq->db = dev->nic_info.db + (pd->id * dev->nic_info.db_page_size);
  1491. status = ocrdma_mbx_create_srq(dev, srq, init_attr, pd);
  1492. if (status)
  1493. goto err;
  1494. if (udata == NULL) {
  1495. srq->rqe_wr_id_tbl = kzalloc(sizeof(u64) * srq->rq.max_cnt,
  1496. GFP_KERNEL);
  1497. if (srq->rqe_wr_id_tbl == NULL)
  1498. goto arm_err;
  1499. srq->bit_fields_len = (srq->rq.max_cnt / 32) +
  1500. (srq->rq.max_cnt % 32 ? 1 : 0);
  1501. srq->idx_bit_fields =
  1502. kmalloc(srq->bit_fields_len * sizeof(u32), GFP_KERNEL);
  1503. if (srq->idx_bit_fields == NULL)
  1504. goto arm_err;
  1505. memset(srq->idx_bit_fields, 0xff,
  1506. srq->bit_fields_len * sizeof(u32));
  1507. }
  1508. if (init_attr->attr.srq_limit) {
  1509. status = ocrdma_mbx_modify_srq(srq, &init_attr->attr);
  1510. if (status)
  1511. goto arm_err;
  1512. }
  1513. if (udata) {
  1514. status = ocrdma_copy_srq_uresp(dev, srq, udata);
  1515. if (status)
  1516. goto arm_err;
  1517. }
  1518. return &srq->ibsrq;
  1519. arm_err:
  1520. ocrdma_mbx_destroy_srq(dev, srq);
  1521. err:
  1522. kfree(srq->rqe_wr_id_tbl);
  1523. kfree(srq->idx_bit_fields);
  1524. kfree(srq);
  1525. return ERR_PTR(status);
  1526. }
  1527. int ocrdma_modify_srq(struct ib_srq *ibsrq,
  1528. struct ib_srq_attr *srq_attr,
  1529. enum ib_srq_attr_mask srq_attr_mask,
  1530. struct ib_udata *udata)
  1531. {
  1532. int status = 0;
  1533. struct ocrdma_srq *srq;
  1534. srq = get_ocrdma_srq(ibsrq);
  1535. if (srq_attr_mask & IB_SRQ_MAX_WR)
  1536. status = -EINVAL;
  1537. else
  1538. status = ocrdma_mbx_modify_srq(srq, srq_attr);
  1539. return status;
  1540. }
  1541. int ocrdma_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
  1542. {
  1543. int status;
  1544. struct ocrdma_srq *srq;
  1545. srq = get_ocrdma_srq(ibsrq);
  1546. status = ocrdma_mbx_query_srq(srq, srq_attr);
  1547. return status;
  1548. }
  1549. int ocrdma_destroy_srq(struct ib_srq *ibsrq)
  1550. {
  1551. int status;
  1552. struct ocrdma_srq *srq;
  1553. struct ocrdma_dev *dev = get_ocrdma_dev(ibsrq->device);
  1554. srq = get_ocrdma_srq(ibsrq);
  1555. status = ocrdma_mbx_destroy_srq(dev, srq);
  1556. if (srq->pd->uctx)
  1557. ocrdma_del_mmap(srq->pd->uctx, (u64) srq->rq.pa,
  1558. PAGE_ALIGN(srq->rq.len));
  1559. kfree(srq->idx_bit_fields);
  1560. kfree(srq->rqe_wr_id_tbl);
  1561. kfree(srq);
  1562. return status;
  1563. }
  1564. /* unprivileged verbs and their support functions. */
  1565. static void ocrdma_build_ud_hdr(struct ocrdma_qp *qp,
  1566. struct ocrdma_hdr_wqe *hdr,
  1567. struct ib_send_wr *wr)
  1568. {
  1569. struct ocrdma_ewqe_ud_hdr *ud_hdr =
  1570. (struct ocrdma_ewqe_ud_hdr *)(hdr + 1);
  1571. struct ocrdma_ah *ah = get_ocrdma_ah(wr->wr.ud.ah);
  1572. ud_hdr->rsvd_dest_qpn = wr->wr.ud.remote_qpn;
  1573. if (qp->qp_type == IB_QPT_GSI)
  1574. ud_hdr->qkey = qp->qkey;
  1575. else
  1576. ud_hdr->qkey = wr->wr.ud.remote_qkey;
  1577. ud_hdr->rsvd_ahid = ah->id;
  1578. }
  1579. static void ocrdma_build_sges(struct ocrdma_hdr_wqe *hdr,
  1580. struct ocrdma_sge *sge, int num_sge,
  1581. struct ib_sge *sg_list)
  1582. {
  1583. int i;
  1584. for (i = 0; i < num_sge; i++) {
  1585. sge[i].lrkey = sg_list[i].lkey;
  1586. sge[i].addr_lo = sg_list[i].addr;
  1587. sge[i].addr_hi = upper_32_bits(sg_list[i].addr);
  1588. sge[i].len = sg_list[i].length;
  1589. hdr->total_len += sg_list[i].length;
  1590. }
  1591. if (num_sge == 0)
  1592. memset(sge, 0, sizeof(*sge));
  1593. }
  1594. static inline uint32_t ocrdma_sglist_len(struct ib_sge *sg_list, int num_sge)
  1595. {
  1596. uint32_t total_len = 0, i;
  1597. for (i = 0; i < num_sge; i++)
  1598. total_len += sg_list[i].length;
  1599. return total_len;
  1600. }
  1601. static int ocrdma_build_inline_sges(struct ocrdma_qp *qp,
  1602. struct ocrdma_hdr_wqe *hdr,
  1603. struct ocrdma_sge *sge,
  1604. struct ib_send_wr *wr, u32 wqe_size)
  1605. {
  1606. int i;
  1607. char *dpp_addr;
  1608. if (wr->send_flags & IB_SEND_INLINE && qp->qp_type != IB_QPT_UD) {
  1609. hdr->total_len = ocrdma_sglist_len(wr->sg_list, wr->num_sge);
  1610. if (unlikely(hdr->total_len > qp->max_inline_data)) {
  1611. pr_err("%s() supported_len=0x%x,\n"
  1612. " unspported len req=0x%x\n", __func__,
  1613. qp->max_inline_data, hdr->total_len);
  1614. return -EINVAL;
  1615. }
  1616. dpp_addr = (char *)sge;
  1617. for (i = 0; i < wr->num_sge; i++) {
  1618. memcpy(dpp_addr,
  1619. (void *)(unsigned long)wr->sg_list[i].addr,
  1620. wr->sg_list[i].length);
  1621. dpp_addr += wr->sg_list[i].length;
  1622. }
  1623. wqe_size += roundup(hdr->total_len, OCRDMA_WQE_ALIGN_BYTES);
  1624. if (0 == hdr->total_len)
  1625. wqe_size += sizeof(struct ocrdma_sge);
  1626. hdr->cw |= (OCRDMA_TYPE_INLINE << OCRDMA_WQE_TYPE_SHIFT);
  1627. } else {
  1628. ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list);
  1629. if (wr->num_sge)
  1630. wqe_size += (wr->num_sge * sizeof(struct ocrdma_sge));
  1631. else
  1632. wqe_size += sizeof(struct ocrdma_sge);
  1633. hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
  1634. }
  1635. hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
  1636. return 0;
  1637. }
  1638. static int ocrdma_build_send(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
  1639. struct ib_send_wr *wr)
  1640. {
  1641. int status;
  1642. struct ocrdma_sge *sge;
  1643. u32 wqe_size = sizeof(*hdr);
  1644. if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
  1645. ocrdma_build_ud_hdr(qp, hdr, wr);
  1646. sge = (struct ocrdma_sge *)(hdr + 2);
  1647. wqe_size += sizeof(struct ocrdma_ewqe_ud_hdr);
  1648. } else {
  1649. sge = (struct ocrdma_sge *)(hdr + 1);
  1650. }
  1651. status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size);
  1652. return status;
  1653. }
  1654. static int ocrdma_build_write(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
  1655. struct ib_send_wr *wr)
  1656. {
  1657. int status;
  1658. struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1);
  1659. struct ocrdma_sge *sge = ext_rw + 1;
  1660. u32 wqe_size = sizeof(*hdr) + sizeof(*ext_rw);
  1661. status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size);
  1662. if (status)
  1663. return status;
  1664. ext_rw->addr_lo = wr->wr.rdma.remote_addr;
  1665. ext_rw->addr_hi = upper_32_bits(wr->wr.rdma.remote_addr);
  1666. ext_rw->lrkey = wr->wr.rdma.rkey;
  1667. ext_rw->len = hdr->total_len;
  1668. return 0;
  1669. }
  1670. static void ocrdma_build_read(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
  1671. struct ib_send_wr *wr)
  1672. {
  1673. struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1);
  1674. struct ocrdma_sge *sge = ext_rw + 1;
  1675. u32 wqe_size = ((wr->num_sge + 1) * sizeof(struct ocrdma_sge)) +
  1676. sizeof(struct ocrdma_hdr_wqe);
  1677. ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list);
  1678. hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
  1679. hdr->cw |= (OCRDMA_READ << OCRDMA_WQE_OPCODE_SHIFT);
  1680. hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
  1681. ext_rw->addr_lo = wr->wr.rdma.remote_addr;
  1682. ext_rw->addr_hi = upper_32_bits(wr->wr.rdma.remote_addr);
  1683. ext_rw->lrkey = wr->wr.rdma.rkey;
  1684. ext_rw->len = hdr->total_len;
  1685. }
  1686. static void build_frmr_pbes(struct ib_send_wr *wr, struct ocrdma_pbl *pbl_tbl,
  1687. struct ocrdma_hw_mr *hwmr)
  1688. {
  1689. int i;
  1690. u64 buf_addr = 0;
  1691. int num_pbes;
  1692. struct ocrdma_pbe *pbe;
  1693. pbe = (struct ocrdma_pbe *)pbl_tbl->va;
  1694. num_pbes = 0;
  1695. /* go through the OS phy regions & fill hw pbe entries into pbls. */
  1696. for (i = 0; i < wr->wr.fast_reg.page_list_len; i++) {
  1697. /* number of pbes can be more for one OS buf, when
  1698. * buffers are of different sizes.
  1699. * split the ib_buf to one or more pbes.
  1700. */
  1701. buf_addr = wr->wr.fast_reg.page_list->page_list[i];
  1702. pbe->pa_lo = cpu_to_le32((u32) (buf_addr & PAGE_MASK));
  1703. pbe->pa_hi = cpu_to_le32((u32) upper_32_bits(buf_addr));
  1704. num_pbes += 1;
  1705. pbe++;
  1706. /* if the pbl is full storing the pbes,
  1707. * move to next pbl.
  1708. */
  1709. if (num_pbes == (hwmr->pbl_size/sizeof(u64))) {
  1710. pbl_tbl++;
  1711. pbe = (struct ocrdma_pbe *)pbl_tbl->va;
  1712. }
  1713. }
  1714. return;
  1715. }
  1716. static int get_encoded_page_size(int pg_sz)
  1717. {
  1718. /* Max size is 256M 4096 << 16 */
  1719. int i = 0;
  1720. for (; i < 17; i++)
  1721. if (pg_sz == (4096 << i))
  1722. break;
  1723. return i;
  1724. }
  1725. static int ocrdma_build_fr(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
  1726. struct ib_send_wr *wr)
  1727. {
  1728. u64 fbo;
  1729. struct ocrdma_ewqe_fr *fast_reg = (struct ocrdma_ewqe_fr *)(hdr + 1);
  1730. struct ocrdma_mr *mr;
  1731. u32 wqe_size = sizeof(*fast_reg) + sizeof(*hdr);
  1732. wqe_size = roundup(wqe_size, OCRDMA_WQE_ALIGN_BYTES);
  1733. if (wr->wr.fast_reg.page_list_len > qp->dev->attr.max_pages_per_frmr)
  1734. return -EINVAL;
  1735. hdr->cw |= (OCRDMA_FR_MR << OCRDMA_WQE_OPCODE_SHIFT);
  1736. hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
  1737. if (wr->wr.fast_reg.page_list_len == 0)
  1738. BUG();
  1739. if (wr->wr.fast_reg.access_flags & IB_ACCESS_LOCAL_WRITE)
  1740. hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_LOCAL_WR;
  1741. if (wr->wr.fast_reg.access_flags & IB_ACCESS_REMOTE_WRITE)
  1742. hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_REMOTE_WR;
  1743. if (wr->wr.fast_reg.access_flags & IB_ACCESS_REMOTE_READ)
  1744. hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_REMOTE_RD;
  1745. hdr->lkey = wr->wr.fast_reg.rkey;
  1746. hdr->total_len = wr->wr.fast_reg.length;
  1747. fbo = wr->wr.fast_reg.iova_start -
  1748. (wr->wr.fast_reg.page_list->page_list[0] & PAGE_MASK);
  1749. fast_reg->va_hi = upper_32_bits(wr->wr.fast_reg.iova_start);
  1750. fast_reg->va_lo = (u32) (wr->wr.fast_reg.iova_start & 0xffffffff);
  1751. fast_reg->fbo_hi = upper_32_bits(fbo);
  1752. fast_reg->fbo_lo = (u32) fbo & 0xffffffff;
  1753. fast_reg->num_sges = wr->wr.fast_reg.page_list_len;
  1754. fast_reg->size_sge =
  1755. get_encoded_page_size(1 << wr->wr.fast_reg.page_shift);
  1756. mr = (struct ocrdma_mr *) (unsigned long) qp->dev->stag_arr[(hdr->lkey >> 8) &
  1757. (OCRDMA_MAX_STAG - 1)];
  1758. build_frmr_pbes(wr, mr->hwmr.pbl_table, &mr->hwmr);
  1759. return 0;
  1760. }
  1761. static void ocrdma_ring_sq_db(struct ocrdma_qp *qp)
  1762. {
  1763. u32 val = qp->sq.dbid | (1 << 16);
  1764. iowrite32(val, qp->sq_db);
  1765. }
  1766. int ocrdma_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  1767. struct ib_send_wr **bad_wr)
  1768. {
  1769. int status = 0;
  1770. struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
  1771. struct ocrdma_hdr_wqe *hdr;
  1772. unsigned long flags;
  1773. spin_lock_irqsave(&qp->q_lock, flags);
  1774. if (qp->state != OCRDMA_QPS_RTS && qp->state != OCRDMA_QPS_SQD) {
  1775. spin_unlock_irqrestore(&qp->q_lock, flags);
  1776. *bad_wr = wr;
  1777. return -EINVAL;
  1778. }
  1779. while (wr) {
  1780. if (ocrdma_hwq_free_cnt(&qp->sq) == 0 ||
  1781. wr->num_sge > qp->sq.max_sges) {
  1782. *bad_wr = wr;
  1783. status = -ENOMEM;
  1784. break;
  1785. }
  1786. hdr = ocrdma_hwq_head(&qp->sq);
  1787. hdr->cw = 0;
  1788. if (wr->send_flags & IB_SEND_SIGNALED || qp->signaled)
  1789. hdr->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT);
  1790. if (wr->send_flags & IB_SEND_FENCE)
  1791. hdr->cw |=
  1792. (OCRDMA_FLAG_FENCE_L << OCRDMA_WQE_FLAGS_SHIFT);
  1793. if (wr->send_flags & IB_SEND_SOLICITED)
  1794. hdr->cw |=
  1795. (OCRDMA_FLAG_SOLICIT << OCRDMA_WQE_FLAGS_SHIFT);
  1796. hdr->total_len = 0;
  1797. switch (wr->opcode) {
  1798. case IB_WR_SEND_WITH_IMM:
  1799. hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT);
  1800. hdr->immdt = ntohl(wr->ex.imm_data);
  1801. case IB_WR_SEND:
  1802. hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT);
  1803. ocrdma_build_send(qp, hdr, wr);
  1804. break;
  1805. case IB_WR_SEND_WITH_INV:
  1806. hdr->cw |= (OCRDMA_FLAG_INV << OCRDMA_WQE_FLAGS_SHIFT);
  1807. hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT);
  1808. hdr->lkey = wr->ex.invalidate_rkey;
  1809. status = ocrdma_build_send(qp, hdr, wr);
  1810. break;
  1811. case IB_WR_RDMA_WRITE_WITH_IMM:
  1812. hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT);
  1813. hdr->immdt = ntohl(wr->ex.imm_data);
  1814. case IB_WR_RDMA_WRITE:
  1815. hdr->cw |= (OCRDMA_WRITE << OCRDMA_WQE_OPCODE_SHIFT);
  1816. status = ocrdma_build_write(qp, hdr, wr);
  1817. break;
  1818. case IB_WR_RDMA_READ_WITH_INV:
  1819. hdr->cw |= (OCRDMA_FLAG_INV << OCRDMA_WQE_FLAGS_SHIFT);
  1820. case IB_WR_RDMA_READ:
  1821. ocrdma_build_read(qp, hdr, wr);
  1822. break;
  1823. case IB_WR_LOCAL_INV:
  1824. hdr->cw |=
  1825. (OCRDMA_LKEY_INV << OCRDMA_WQE_OPCODE_SHIFT);
  1826. hdr->cw |= ((sizeof(struct ocrdma_hdr_wqe) +
  1827. sizeof(struct ocrdma_sge)) /
  1828. OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT;
  1829. hdr->lkey = wr->ex.invalidate_rkey;
  1830. break;
  1831. case IB_WR_FAST_REG_MR:
  1832. status = ocrdma_build_fr(qp, hdr, wr);
  1833. break;
  1834. default:
  1835. status = -EINVAL;
  1836. break;
  1837. }
  1838. if (status) {
  1839. *bad_wr = wr;
  1840. break;
  1841. }
  1842. if (wr->send_flags & IB_SEND_SIGNALED || qp->signaled)
  1843. qp->wqe_wr_id_tbl[qp->sq.head].signaled = 1;
  1844. else
  1845. qp->wqe_wr_id_tbl[qp->sq.head].signaled = 0;
  1846. qp->wqe_wr_id_tbl[qp->sq.head].wrid = wr->wr_id;
  1847. ocrdma_cpu_to_le32(hdr, ((hdr->cw >> OCRDMA_WQE_SIZE_SHIFT) &
  1848. OCRDMA_WQE_SIZE_MASK) * OCRDMA_WQE_STRIDE);
  1849. /* make sure wqe is written before adapter can access it */
  1850. wmb();
  1851. /* inform hw to start processing it */
  1852. ocrdma_ring_sq_db(qp);
  1853. /* update pointer, counter for next wr */
  1854. ocrdma_hwq_inc_head(&qp->sq);
  1855. wr = wr->next;
  1856. }
  1857. spin_unlock_irqrestore(&qp->q_lock, flags);
  1858. return status;
  1859. }
  1860. static void ocrdma_ring_rq_db(struct ocrdma_qp *qp)
  1861. {
  1862. u32 val = qp->rq.dbid | (1 << ocrdma_get_num_posted_shift(qp));
  1863. if (qp->state != OCRDMA_QPS_INIT)
  1864. iowrite32(val, qp->rq_db);
  1865. else
  1866. qp->db_cache++;
  1867. }
  1868. static void ocrdma_build_rqe(struct ocrdma_hdr_wqe *rqe, struct ib_recv_wr *wr,
  1869. u16 tag)
  1870. {
  1871. u32 wqe_size = 0;
  1872. struct ocrdma_sge *sge;
  1873. if (wr->num_sge)
  1874. wqe_size = (wr->num_sge * sizeof(*sge)) + sizeof(*rqe);
  1875. else
  1876. wqe_size = sizeof(*sge) + sizeof(*rqe);
  1877. rqe->cw = ((wqe_size / OCRDMA_WQE_STRIDE) <<
  1878. OCRDMA_WQE_SIZE_SHIFT);
  1879. rqe->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT);
  1880. rqe->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
  1881. rqe->total_len = 0;
  1882. rqe->rsvd_tag = tag;
  1883. sge = (struct ocrdma_sge *)(rqe + 1);
  1884. ocrdma_build_sges(rqe, sge, wr->num_sge, wr->sg_list);
  1885. ocrdma_cpu_to_le32(rqe, wqe_size);
  1886. }
  1887. int ocrdma_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1888. struct ib_recv_wr **bad_wr)
  1889. {
  1890. int status = 0;
  1891. unsigned long flags;
  1892. struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
  1893. struct ocrdma_hdr_wqe *rqe;
  1894. spin_lock_irqsave(&qp->q_lock, flags);
  1895. if (qp->state == OCRDMA_QPS_RST || qp->state == OCRDMA_QPS_ERR) {
  1896. spin_unlock_irqrestore(&qp->q_lock, flags);
  1897. *bad_wr = wr;
  1898. return -EINVAL;
  1899. }
  1900. while (wr) {
  1901. if (ocrdma_hwq_free_cnt(&qp->rq) == 0 ||
  1902. wr->num_sge > qp->rq.max_sges) {
  1903. *bad_wr = wr;
  1904. status = -ENOMEM;
  1905. break;
  1906. }
  1907. rqe = ocrdma_hwq_head(&qp->rq);
  1908. ocrdma_build_rqe(rqe, wr, 0);
  1909. qp->rqe_wr_id_tbl[qp->rq.head] = wr->wr_id;
  1910. /* make sure rqe is written before adapter can access it */
  1911. wmb();
  1912. /* inform hw to start processing it */
  1913. ocrdma_ring_rq_db(qp);
  1914. /* update pointer, counter for next wr */
  1915. ocrdma_hwq_inc_head(&qp->rq);
  1916. wr = wr->next;
  1917. }
  1918. spin_unlock_irqrestore(&qp->q_lock, flags);
  1919. return status;
  1920. }
  1921. /* cqe for srq's rqe can potentially arrive out of order.
  1922. * index gives the entry in the shadow table where to store
  1923. * the wr_id. tag/index is returned in cqe to reference back
  1924. * for a given rqe.
  1925. */
  1926. static int ocrdma_srq_get_idx(struct ocrdma_srq *srq)
  1927. {
  1928. int row = 0;
  1929. int indx = 0;
  1930. for (row = 0; row < srq->bit_fields_len; row++) {
  1931. if (srq->idx_bit_fields[row]) {
  1932. indx = ffs(srq->idx_bit_fields[row]);
  1933. indx = (row * 32) + (indx - 1);
  1934. if (indx >= srq->rq.max_cnt)
  1935. BUG();
  1936. ocrdma_srq_toggle_bit(srq, indx);
  1937. break;
  1938. }
  1939. }
  1940. if (row == srq->bit_fields_len)
  1941. BUG();
  1942. return indx;
  1943. }
  1944. static void ocrdma_ring_srq_db(struct ocrdma_srq *srq)
  1945. {
  1946. u32 val = srq->rq.dbid | (1 << 16);
  1947. iowrite32(val, srq->db + OCRDMA_DB_GEN2_SRQ_OFFSET);
  1948. }
  1949. int ocrdma_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  1950. struct ib_recv_wr **bad_wr)
  1951. {
  1952. int status = 0;
  1953. unsigned long flags;
  1954. struct ocrdma_srq *srq;
  1955. struct ocrdma_hdr_wqe *rqe;
  1956. u16 tag;
  1957. srq = get_ocrdma_srq(ibsrq);
  1958. spin_lock_irqsave(&srq->q_lock, flags);
  1959. while (wr) {
  1960. if (ocrdma_hwq_free_cnt(&srq->rq) == 0 ||
  1961. wr->num_sge > srq->rq.max_sges) {
  1962. status = -ENOMEM;
  1963. *bad_wr = wr;
  1964. break;
  1965. }
  1966. tag = ocrdma_srq_get_idx(srq);
  1967. rqe = ocrdma_hwq_head(&srq->rq);
  1968. ocrdma_build_rqe(rqe, wr, tag);
  1969. srq->rqe_wr_id_tbl[tag] = wr->wr_id;
  1970. /* make sure rqe is written before adapter can perform DMA */
  1971. wmb();
  1972. /* inform hw to start processing it */
  1973. ocrdma_ring_srq_db(srq);
  1974. /* update pointer, counter for next wr */
  1975. ocrdma_hwq_inc_head(&srq->rq);
  1976. wr = wr->next;
  1977. }
  1978. spin_unlock_irqrestore(&srq->q_lock, flags);
  1979. return status;
  1980. }
  1981. static enum ib_wc_status ocrdma_to_ibwc_err(u16 status)
  1982. {
  1983. enum ib_wc_status ibwc_status;
  1984. switch (status) {
  1985. case OCRDMA_CQE_GENERAL_ERR:
  1986. ibwc_status = IB_WC_GENERAL_ERR;
  1987. break;
  1988. case OCRDMA_CQE_LOC_LEN_ERR:
  1989. ibwc_status = IB_WC_LOC_LEN_ERR;
  1990. break;
  1991. case OCRDMA_CQE_LOC_QP_OP_ERR:
  1992. ibwc_status = IB_WC_LOC_QP_OP_ERR;
  1993. break;
  1994. case OCRDMA_CQE_LOC_EEC_OP_ERR:
  1995. ibwc_status = IB_WC_LOC_EEC_OP_ERR;
  1996. break;
  1997. case OCRDMA_CQE_LOC_PROT_ERR:
  1998. ibwc_status = IB_WC_LOC_PROT_ERR;
  1999. break;
  2000. case OCRDMA_CQE_WR_FLUSH_ERR:
  2001. ibwc_status = IB_WC_WR_FLUSH_ERR;
  2002. break;
  2003. case OCRDMA_CQE_MW_BIND_ERR:
  2004. ibwc_status = IB_WC_MW_BIND_ERR;
  2005. break;
  2006. case OCRDMA_CQE_BAD_RESP_ERR:
  2007. ibwc_status = IB_WC_BAD_RESP_ERR;
  2008. break;
  2009. case OCRDMA_CQE_LOC_ACCESS_ERR:
  2010. ibwc_status = IB_WC_LOC_ACCESS_ERR;
  2011. break;
  2012. case OCRDMA_CQE_REM_INV_REQ_ERR:
  2013. ibwc_status = IB_WC_REM_INV_REQ_ERR;
  2014. break;
  2015. case OCRDMA_CQE_REM_ACCESS_ERR:
  2016. ibwc_status = IB_WC_REM_ACCESS_ERR;
  2017. break;
  2018. case OCRDMA_CQE_REM_OP_ERR:
  2019. ibwc_status = IB_WC_REM_OP_ERR;
  2020. break;
  2021. case OCRDMA_CQE_RETRY_EXC_ERR:
  2022. ibwc_status = IB_WC_RETRY_EXC_ERR;
  2023. break;
  2024. case OCRDMA_CQE_RNR_RETRY_EXC_ERR:
  2025. ibwc_status = IB_WC_RNR_RETRY_EXC_ERR;
  2026. break;
  2027. case OCRDMA_CQE_LOC_RDD_VIOL_ERR:
  2028. ibwc_status = IB_WC_LOC_RDD_VIOL_ERR;
  2029. break;
  2030. case OCRDMA_CQE_REM_INV_RD_REQ_ERR:
  2031. ibwc_status = IB_WC_REM_INV_RD_REQ_ERR;
  2032. break;
  2033. case OCRDMA_CQE_REM_ABORT_ERR:
  2034. ibwc_status = IB_WC_REM_ABORT_ERR;
  2035. break;
  2036. case OCRDMA_CQE_INV_EECN_ERR:
  2037. ibwc_status = IB_WC_INV_EECN_ERR;
  2038. break;
  2039. case OCRDMA_CQE_INV_EEC_STATE_ERR:
  2040. ibwc_status = IB_WC_INV_EEC_STATE_ERR;
  2041. break;
  2042. case OCRDMA_CQE_FATAL_ERR:
  2043. ibwc_status = IB_WC_FATAL_ERR;
  2044. break;
  2045. case OCRDMA_CQE_RESP_TIMEOUT_ERR:
  2046. ibwc_status = IB_WC_RESP_TIMEOUT_ERR;
  2047. break;
  2048. default:
  2049. ibwc_status = IB_WC_GENERAL_ERR;
  2050. break;
  2051. }
  2052. return ibwc_status;
  2053. }
  2054. static void ocrdma_update_wc(struct ocrdma_qp *qp, struct ib_wc *ibwc,
  2055. u32 wqe_idx)
  2056. {
  2057. struct ocrdma_hdr_wqe *hdr;
  2058. struct ocrdma_sge *rw;
  2059. int opcode;
  2060. hdr = ocrdma_hwq_head_from_idx(&qp->sq, wqe_idx);
  2061. ibwc->wr_id = qp->wqe_wr_id_tbl[wqe_idx].wrid;
  2062. /* Undo the hdr->cw swap */
  2063. opcode = le32_to_cpu(hdr->cw) & OCRDMA_WQE_OPCODE_MASK;
  2064. switch (opcode) {
  2065. case OCRDMA_WRITE:
  2066. ibwc->opcode = IB_WC_RDMA_WRITE;
  2067. break;
  2068. case OCRDMA_READ:
  2069. rw = (struct ocrdma_sge *)(hdr + 1);
  2070. ibwc->opcode = IB_WC_RDMA_READ;
  2071. ibwc->byte_len = rw->len;
  2072. break;
  2073. case OCRDMA_SEND:
  2074. ibwc->opcode = IB_WC_SEND;
  2075. break;
  2076. case OCRDMA_FR_MR:
  2077. ibwc->opcode = IB_WC_FAST_REG_MR;
  2078. break;
  2079. case OCRDMA_LKEY_INV:
  2080. ibwc->opcode = IB_WC_LOCAL_INV;
  2081. break;
  2082. default:
  2083. ibwc->status = IB_WC_GENERAL_ERR;
  2084. pr_err("%s() invalid opcode received = 0x%x\n",
  2085. __func__, hdr->cw & OCRDMA_WQE_OPCODE_MASK);
  2086. break;
  2087. }
  2088. }
  2089. static void ocrdma_set_cqe_status_flushed(struct ocrdma_qp *qp,
  2090. struct ocrdma_cqe *cqe)
  2091. {
  2092. if (is_cqe_for_sq(cqe)) {
  2093. cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
  2094. cqe->flags_status_srcqpn) &
  2095. ~OCRDMA_CQE_STATUS_MASK);
  2096. cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
  2097. cqe->flags_status_srcqpn) |
  2098. (OCRDMA_CQE_WR_FLUSH_ERR <<
  2099. OCRDMA_CQE_STATUS_SHIFT));
  2100. } else {
  2101. if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
  2102. cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
  2103. cqe->flags_status_srcqpn) &
  2104. ~OCRDMA_CQE_UD_STATUS_MASK);
  2105. cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
  2106. cqe->flags_status_srcqpn) |
  2107. (OCRDMA_CQE_WR_FLUSH_ERR <<
  2108. OCRDMA_CQE_UD_STATUS_SHIFT));
  2109. } else {
  2110. cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
  2111. cqe->flags_status_srcqpn) &
  2112. ~OCRDMA_CQE_STATUS_MASK);
  2113. cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
  2114. cqe->flags_status_srcqpn) |
  2115. (OCRDMA_CQE_WR_FLUSH_ERR <<
  2116. OCRDMA_CQE_STATUS_SHIFT));
  2117. }
  2118. }
  2119. }
  2120. static bool ocrdma_update_err_cqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
  2121. struct ocrdma_qp *qp, int status)
  2122. {
  2123. bool expand = false;
  2124. ibwc->byte_len = 0;
  2125. ibwc->qp = &qp->ibqp;
  2126. ibwc->status = ocrdma_to_ibwc_err(status);
  2127. ocrdma_flush_qp(qp);
  2128. ocrdma_qp_state_change(qp, IB_QPS_ERR, NULL);
  2129. /* if wqe/rqe pending for which cqe needs to be returned,
  2130. * trigger inflating it.
  2131. */
  2132. if (!is_hw_rq_empty(qp) || !is_hw_sq_empty(qp)) {
  2133. expand = true;
  2134. ocrdma_set_cqe_status_flushed(qp, cqe);
  2135. }
  2136. return expand;
  2137. }
  2138. static int ocrdma_update_err_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
  2139. struct ocrdma_qp *qp, int status)
  2140. {
  2141. ibwc->opcode = IB_WC_RECV;
  2142. ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
  2143. ocrdma_hwq_inc_tail(&qp->rq);
  2144. return ocrdma_update_err_cqe(ibwc, cqe, qp, status);
  2145. }
  2146. static int ocrdma_update_err_scqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
  2147. struct ocrdma_qp *qp, int status)
  2148. {
  2149. ocrdma_update_wc(qp, ibwc, qp->sq.tail);
  2150. ocrdma_hwq_inc_tail(&qp->sq);
  2151. return ocrdma_update_err_cqe(ibwc, cqe, qp, status);
  2152. }
  2153. static bool ocrdma_poll_err_scqe(struct ocrdma_qp *qp,
  2154. struct ocrdma_cqe *cqe, struct ib_wc *ibwc,
  2155. bool *polled, bool *stop)
  2156. {
  2157. bool expand;
  2158. int status = (le32_to_cpu(cqe->flags_status_srcqpn) &
  2159. OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
  2160. /* when hw sq is empty, but rq is not empty, so we continue
  2161. * to keep the cqe in order to get the cq event again.
  2162. */
  2163. if (is_hw_sq_empty(qp) && !is_hw_rq_empty(qp)) {
  2164. /* when cq for rq and sq is same, it is safe to return
  2165. * flush cqe for RQEs.
  2166. */
  2167. if (!qp->srq && (qp->sq_cq == qp->rq_cq)) {
  2168. *polled = true;
  2169. status = OCRDMA_CQE_WR_FLUSH_ERR;
  2170. expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status);
  2171. } else {
  2172. /* stop processing further cqe as this cqe is used for
  2173. * triggering cq event on buddy cq of RQ.
  2174. * When QP is destroyed, this cqe will be removed
  2175. * from the cq's hardware q.
  2176. */
  2177. *polled = false;
  2178. *stop = true;
  2179. expand = false;
  2180. }
  2181. } else {
  2182. *polled = true;
  2183. expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status);
  2184. }
  2185. return expand;
  2186. }
  2187. static bool ocrdma_poll_success_scqe(struct ocrdma_qp *qp,
  2188. struct ocrdma_cqe *cqe,
  2189. struct ib_wc *ibwc, bool *polled)
  2190. {
  2191. bool expand = false;
  2192. int tail = qp->sq.tail;
  2193. u32 wqe_idx;
  2194. if (!qp->wqe_wr_id_tbl[tail].signaled) {
  2195. *polled = false; /* WC cannot be consumed yet */
  2196. } else {
  2197. ibwc->status = IB_WC_SUCCESS;
  2198. ibwc->wc_flags = 0;
  2199. ibwc->qp = &qp->ibqp;
  2200. ocrdma_update_wc(qp, ibwc, tail);
  2201. *polled = true;
  2202. }
  2203. wqe_idx = (le32_to_cpu(cqe->wq.wqeidx) &
  2204. OCRDMA_CQE_WQEIDX_MASK) & qp->sq.max_wqe_idx;
  2205. if (tail != wqe_idx)
  2206. expand = true; /* Coalesced CQE can't be consumed yet */
  2207. ocrdma_hwq_inc_tail(&qp->sq);
  2208. return expand;
  2209. }
  2210. static bool ocrdma_poll_scqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
  2211. struct ib_wc *ibwc, bool *polled, bool *stop)
  2212. {
  2213. int status;
  2214. bool expand;
  2215. status = (le32_to_cpu(cqe->flags_status_srcqpn) &
  2216. OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
  2217. if (status == OCRDMA_CQE_SUCCESS)
  2218. expand = ocrdma_poll_success_scqe(qp, cqe, ibwc, polled);
  2219. else
  2220. expand = ocrdma_poll_err_scqe(qp, cqe, ibwc, polled, stop);
  2221. return expand;
  2222. }
  2223. static int ocrdma_update_ud_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe)
  2224. {
  2225. int status;
  2226. status = (le32_to_cpu(cqe->flags_status_srcqpn) &
  2227. OCRDMA_CQE_UD_STATUS_MASK) >> OCRDMA_CQE_UD_STATUS_SHIFT;
  2228. ibwc->src_qp = le32_to_cpu(cqe->flags_status_srcqpn) &
  2229. OCRDMA_CQE_SRCQP_MASK;
  2230. ibwc->pkey_index = le32_to_cpu(cqe->ud.rxlen_pkey) &
  2231. OCRDMA_CQE_PKEY_MASK;
  2232. ibwc->wc_flags = IB_WC_GRH;
  2233. ibwc->byte_len = (le32_to_cpu(cqe->ud.rxlen_pkey) >>
  2234. OCRDMA_CQE_UD_XFER_LEN_SHIFT);
  2235. return status;
  2236. }
  2237. static void ocrdma_update_free_srq_cqe(struct ib_wc *ibwc,
  2238. struct ocrdma_cqe *cqe,
  2239. struct ocrdma_qp *qp)
  2240. {
  2241. unsigned long flags;
  2242. struct ocrdma_srq *srq;
  2243. u32 wqe_idx;
  2244. srq = get_ocrdma_srq(qp->ibqp.srq);
  2245. wqe_idx = (le32_to_cpu(cqe->rq.buftag_qpn) >>
  2246. OCRDMA_CQE_BUFTAG_SHIFT) & srq->rq.max_wqe_idx;
  2247. ibwc->wr_id = srq->rqe_wr_id_tbl[wqe_idx];
  2248. spin_lock_irqsave(&srq->q_lock, flags);
  2249. ocrdma_srq_toggle_bit(srq, wqe_idx);
  2250. spin_unlock_irqrestore(&srq->q_lock, flags);
  2251. ocrdma_hwq_inc_tail(&srq->rq);
  2252. }
  2253. static bool ocrdma_poll_err_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
  2254. struct ib_wc *ibwc, bool *polled, bool *stop,
  2255. int status)
  2256. {
  2257. bool expand;
  2258. /* when hw_rq is empty, but wq is not empty, so continue
  2259. * to keep the cqe to get the cq event again.
  2260. */
  2261. if (is_hw_rq_empty(qp) && !is_hw_sq_empty(qp)) {
  2262. if (!qp->srq && (qp->sq_cq == qp->rq_cq)) {
  2263. *polled = true;
  2264. status = OCRDMA_CQE_WR_FLUSH_ERR;
  2265. expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status);
  2266. } else {
  2267. *polled = false;
  2268. *stop = true;
  2269. expand = false;
  2270. }
  2271. } else {
  2272. *polled = true;
  2273. expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status);
  2274. }
  2275. return expand;
  2276. }
  2277. static void ocrdma_poll_success_rcqe(struct ocrdma_qp *qp,
  2278. struct ocrdma_cqe *cqe, struct ib_wc *ibwc)
  2279. {
  2280. ibwc->opcode = IB_WC_RECV;
  2281. ibwc->qp = &qp->ibqp;
  2282. ibwc->status = IB_WC_SUCCESS;
  2283. if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI)
  2284. ocrdma_update_ud_rcqe(ibwc, cqe);
  2285. else
  2286. ibwc->byte_len = le32_to_cpu(cqe->rq.rxlen);
  2287. if (is_cqe_imm(cqe)) {
  2288. ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt));
  2289. ibwc->wc_flags |= IB_WC_WITH_IMM;
  2290. } else if (is_cqe_wr_imm(cqe)) {
  2291. ibwc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
  2292. ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt));
  2293. ibwc->wc_flags |= IB_WC_WITH_IMM;
  2294. } else if (is_cqe_invalidated(cqe)) {
  2295. ibwc->ex.invalidate_rkey = le32_to_cpu(cqe->rq.lkey_immdt);
  2296. ibwc->wc_flags |= IB_WC_WITH_INVALIDATE;
  2297. }
  2298. if (qp->ibqp.srq) {
  2299. ocrdma_update_free_srq_cqe(ibwc, cqe, qp);
  2300. } else {
  2301. ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
  2302. ocrdma_hwq_inc_tail(&qp->rq);
  2303. }
  2304. }
  2305. static bool ocrdma_poll_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
  2306. struct ib_wc *ibwc, bool *polled, bool *stop)
  2307. {
  2308. int status;
  2309. bool expand = false;
  2310. ibwc->wc_flags = 0;
  2311. if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
  2312. status = (le32_to_cpu(cqe->flags_status_srcqpn) &
  2313. OCRDMA_CQE_UD_STATUS_MASK) >>
  2314. OCRDMA_CQE_UD_STATUS_SHIFT;
  2315. } else {
  2316. status = (le32_to_cpu(cqe->flags_status_srcqpn) &
  2317. OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
  2318. }
  2319. if (status == OCRDMA_CQE_SUCCESS) {
  2320. *polled = true;
  2321. ocrdma_poll_success_rcqe(qp, cqe, ibwc);
  2322. } else {
  2323. expand = ocrdma_poll_err_rcqe(qp, cqe, ibwc, polled, stop,
  2324. status);
  2325. }
  2326. return expand;
  2327. }
  2328. static void ocrdma_change_cq_phase(struct ocrdma_cq *cq, struct ocrdma_cqe *cqe,
  2329. u16 cur_getp)
  2330. {
  2331. if (cq->phase_change) {
  2332. if (cur_getp == 0)
  2333. cq->phase = (~cq->phase & OCRDMA_CQE_VALID);
  2334. } else {
  2335. /* clear valid bit */
  2336. cqe->flags_status_srcqpn = 0;
  2337. }
  2338. }
  2339. static int ocrdma_poll_hwcq(struct ocrdma_cq *cq, int num_entries,
  2340. struct ib_wc *ibwc)
  2341. {
  2342. u16 qpn = 0;
  2343. int i = 0;
  2344. bool expand = false;
  2345. int polled_hw_cqes = 0;
  2346. struct ocrdma_qp *qp = NULL;
  2347. struct ocrdma_dev *dev = get_ocrdma_dev(cq->ibcq.device);
  2348. struct ocrdma_cqe *cqe;
  2349. u16 cur_getp; bool polled = false; bool stop = false;
  2350. cur_getp = cq->getp;
  2351. while (num_entries) {
  2352. cqe = cq->va + cur_getp;
  2353. /* check whether valid cqe or not */
  2354. if (!is_cqe_valid(cq, cqe))
  2355. break;
  2356. qpn = (le32_to_cpu(cqe->cmn.qpn) & OCRDMA_CQE_QPN_MASK);
  2357. /* ignore discarded cqe */
  2358. if (qpn == 0)
  2359. goto skip_cqe;
  2360. qp = dev->qp_tbl[qpn];
  2361. BUG_ON(qp == NULL);
  2362. if (is_cqe_for_sq(cqe)) {
  2363. expand = ocrdma_poll_scqe(qp, cqe, ibwc, &polled,
  2364. &stop);
  2365. } else {
  2366. expand = ocrdma_poll_rcqe(qp, cqe, ibwc, &polled,
  2367. &stop);
  2368. }
  2369. if (expand)
  2370. goto expand_cqe;
  2371. if (stop)
  2372. goto stop_cqe;
  2373. /* clear qpn to avoid duplicate processing by discard_cqe() */
  2374. cqe->cmn.qpn = 0;
  2375. skip_cqe:
  2376. polled_hw_cqes += 1;
  2377. cur_getp = (cur_getp + 1) % cq->max_hw_cqe;
  2378. ocrdma_change_cq_phase(cq, cqe, cur_getp);
  2379. expand_cqe:
  2380. if (polled) {
  2381. num_entries -= 1;
  2382. i += 1;
  2383. ibwc = ibwc + 1;
  2384. polled = false;
  2385. }
  2386. }
  2387. stop_cqe:
  2388. cq->getp = cur_getp;
  2389. if (polled_hw_cqes || expand || stop) {
  2390. ocrdma_ring_cq_db(dev, cq->id, cq->armed, cq->solicited,
  2391. polled_hw_cqes);
  2392. }
  2393. return i;
  2394. }
  2395. /* insert error cqe if the QP's SQ or RQ's CQ matches the CQ under poll. */
  2396. static int ocrdma_add_err_cqe(struct ocrdma_cq *cq, int num_entries,
  2397. struct ocrdma_qp *qp, struct ib_wc *ibwc)
  2398. {
  2399. int err_cqes = 0;
  2400. while (num_entries) {
  2401. if (is_hw_sq_empty(qp) && is_hw_rq_empty(qp))
  2402. break;
  2403. if (!is_hw_sq_empty(qp) && qp->sq_cq == cq) {
  2404. ocrdma_update_wc(qp, ibwc, qp->sq.tail);
  2405. ocrdma_hwq_inc_tail(&qp->sq);
  2406. } else if (!is_hw_rq_empty(qp) && qp->rq_cq == cq) {
  2407. ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
  2408. ocrdma_hwq_inc_tail(&qp->rq);
  2409. } else {
  2410. return err_cqes;
  2411. }
  2412. ibwc->byte_len = 0;
  2413. ibwc->status = IB_WC_WR_FLUSH_ERR;
  2414. ibwc = ibwc + 1;
  2415. err_cqes += 1;
  2416. num_entries -= 1;
  2417. }
  2418. return err_cqes;
  2419. }
  2420. int ocrdma_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
  2421. {
  2422. int cqes_to_poll = num_entries;
  2423. struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
  2424. struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
  2425. int num_os_cqe = 0, err_cqes = 0;
  2426. struct ocrdma_qp *qp;
  2427. unsigned long flags;
  2428. /* poll cqes from adapter CQ */
  2429. spin_lock_irqsave(&cq->cq_lock, flags);
  2430. num_os_cqe = ocrdma_poll_hwcq(cq, cqes_to_poll, wc);
  2431. spin_unlock_irqrestore(&cq->cq_lock, flags);
  2432. cqes_to_poll -= num_os_cqe;
  2433. if (cqes_to_poll) {
  2434. wc = wc + num_os_cqe;
  2435. /* adapter returns single error cqe when qp moves to
  2436. * error state. So insert error cqes with wc_status as
  2437. * FLUSHED for pending WQEs and RQEs of QP's SQ and RQ
  2438. * respectively which uses this CQ.
  2439. */
  2440. spin_lock_irqsave(&dev->flush_q_lock, flags);
  2441. list_for_each_entry(qp, &cq->sq_head, sq_entry) {
  2442. if (cqes_to_poll == 0)
  2443. break;
  2444. err_cqes = ocrdma_add_err_cqe(cq, cqes_to_poll, qp, wc);
  2445. cqes_to_poll -= err_cqes;
  2446. num_os_cqe += err_cqes;
  2447. wc = wc + err_cqes;
  2448. }
  2449. spin_unlock_irqrestore(&dev->flush_q_lock, flags);
  2450. }
  2451. return num_os_cqe;
  2452. }
  2453. int ocrdma_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags cq_flags)
  2454. {
  2455. struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
  2456. struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
  2457. u16 cq_id;
  2458. u16 cur_getp;
  2459. struct ocrdma_cqe *cqe;
  2460. unsigned long flags;
  2461. cq_id = cq->id;
  2462. spin_lock_irqsave(&cq->cq_lock, flags);
  2463. if (cq_flags & IB_CQ_NEXT_COMP || cq_flags & IB_CQ_SOLICITED)
  2464. cq->armed = true;
  2465. if (cq_flags & IB_CQ_SOLICITED)
  2466. cq->solicited = true;
  2467. cur_getp = cq->getp;
  2468. cqe = cq->va + cur_getp;
  2469. /* check whether any valid cqe exist or not, if not then safe to
  2470. * arm. If cqe is not yet consumed, then let it get consumed and then
  2471. * we arm it to avoid false interrupts.
  2472. */
  2473. if (!is_cqe_valid(cq, cqe) || cq->arm_needed) {
  2474. cq->arm_needed = false;
  2475. ocrdma_ring_cq_db(dev, cq_id, cq->armed, cq->solicited, 0);
  2476. }
  2477. spin_unlock_irqrestore(&cq->cq_lock, flags);
  2478. return 0;
  2479. }
  2480. struct ib_mr *ocrdma_alloc_frmr(struct ib_pd *ibpd, int max_page_list_len)
  2481. {
  2482. int status;
  2483. struct ocrdma_mr *mr;
  2484. struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
  2485. struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
  2486. if (max_page_list_len > dev->attr.max_pages_per_frmr)
  2487. return ERR_PTR(-EINVAL);
  2488. mr = kzalloc(sizeof(*mr), GFP_KERNEL);
  2489. if (!mr)
  2490. return ERR_PTR(-ENOMEM);
  2491. status = ocrdma_get_pbl_info(dev, mr, max_page_list_len);
  2492. if (status)
  2493. goto pbl_err;
  2494. mr->hwmr.fr_mr = 1;
  2495. mr->hwmr.remote_rd = 0;
  2496. mr->hwmr.remote_wr = 0;
  2497. mr->hwmr.local_rd = 0;
  2498. mr->hwmr.local_wr = 0;
  2499. mr->hwmr.mw_bind = 0;
  2500. status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
  2501. if (status)
  2502. goto pbl_err;
  2503. status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, 0);
  2504. if (status)
  2505. goto mbx_err;
  2506. mr->ibmr.rkey = mr->hwmr.lkey;
  2507. mr->ibmr.lkey = mr->hwmr.lkey;
  2508. dev->stag_arr[(mr->hwmr.lkey >> 8) & (OCRDMA_MAX_STAG - 1)] = mr;
  2509. return &mr->ibmr;
  2510. mbx_err:
  2511. ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
  2512. pbl_err:
  2513. kfree(mr);
  2514. return ERR_PTR(-ENOMEM);
  2515. }
  2516. struct ib_fast_reg_page_list *ocrdma_alloc_frmr_page_list(struct ib_device
  2517. *ibdev,
  2518. int page_list_len)
  2519. {
  2520. struct ib_fast_reg_page_list *frmr_list;
  2521. int size;
  2522. size = sizeof(*frmr_list) + (page_list_len * sizeof(u64));
  2523. frmr_list = kzalloc(size, GFP_KERNEL);
  2524. if (!frmr_list)
  2525. return ERR_PTR(-ENOMEM);
  2526. frmr_list->page_list = (u64 *)(frmr_list + 1);
  2527. return frmr_list;
  2528. }
  2529. void ocrdma_free_frmr_page_list(struct ib_fast_reg_page_list *page_list)
  2530. {
  2531. kfree(page_list);
  2532. }
  2533. #define MAX_KERNEL_PBE_SIZE 65536
  2534. static inline int count_kernel_pbes(struct ib_phys_buf *buf_list,
  2535. int buf_cnt, u32 *pbe_size)
  2536. {
  2537. u64 total_size = 0;
  2538. u64 buf_size = 0;
  2539. int i;
  2540. *pbe_size = roundup(buf_list[0].size, PAGE_SIZE);
  2541. *pbe_size = roundup_pow_of_two(*pbe_size);
  2542. /* find the smallest PBE size that we can have */
  2543. for (i = 0; i < buf_cnt; i++) {
  2544. /* first addr may not be page aligned, so ignore checking */
  2545. if ((i != 0) && ((buf_list[i].addr & ~PAGE_MASK) ||
  2546. (buf_list[i].size & ~PAGE_MASK))) {
  2547. return 0;
  2548. }
  2549. /* if configured PBE size is greater then the chosen one,
  2550. * reduce the PBE size.
  2551. */
  2552. buf_size = roundup(buf_list[i].size, PAGE_SIZE);
  2553. /* pbe_size has to be even multiple of 4K 1,2,4,8...*/
  2554. buf_size = roundup_pow_of_two(buf_size);
  2555. if (*pbe_size > buf_size)
  2556. *pbe_size = buf_size;
  2557. total_size += buf_size;
  2558. }
  2559. *pbe_size = *pbe_size > MAX_KERNEL_PBE_SIZE ?
  2560. (MAX_KERNEL_PBE_SIZE) : (*pbe_size);
  2561. /* num_pbes = total_size / (*pbe_size); this is implemented below. */
  2562. return total_size >> ilog2(*pbe_size);
  2563. }
  2564. static void build_kernel_pbes(struct ib_phys_buf *buf_list, int ib_buf_cnt,
  2565. u32 pbe_size, struct ocrdma_pbl *pbl_tbl,
  2566. struct ocrdma_hw_mr *hwmr)
  2567. {
  2568. int i;
  2569. int idx;
  2570. int pbes_per_buf = 0;
  2571. u64 buf_addr = 0;
  2572. int num_pbes;
  2573. struct ocrdma_pbe *pbe;
  2574. int total_num_pbes = 0;
  2575. if (!hwmr->num_pbes)
  2576. return;
  2577. pbe = (struct ocrdma_pbe *)pbl_tbl->va;
  2578. num_pbes = 0;
  2579. /* go through the OS phy regions & fill hw pbe entries into pbls. */
  2580. for (i = 0; i < ib_buf_cnt; i++) {
  2581. buf_addr = buf_list[i].addr;
  2582. pbes_per_buf =
  2583. roundup_pow_of_two(roundup(buf_list[i].size, PAGE_SIZE)) /
  2584. pbe_size;
  2585. hwmr->len += buf_list[i].size;
  2586. /* number of pbes can be more for one OS buf, when
  2587. * buffers are of different sizes.
  2588. * split the ib_buf to one or more pbes.
  2589. */
  2590. for (idx = 0; idx < pbes_per_buf; idx++) {
  2591. /* we program always page aligned addresses,
  2592. * first unaligned address is taken care by fbo.
  2593. */
  2594. if (i == 0) {
  2595. /* for non zero fbo, assign the
  2596. * start of the page.
  2597. */
  2598. pbe->pa_lo =
  2599. cpu_to_le32((u32) (buf_addr & PAGE_MASK));
  2600. pbe->pa_hi =
  2601. cpu_to_le32((u32) upper_32_bits(buf_addr));
  2602. } else {
  2603. pbe->pa_lo =
  2604. cpu_to_le32((u32) (buf_addr & 0xffffffff));
  2605. pbe->pa_hi =
  2606. cpu_to_le32((u32) upper_32_bits(buf_addr));
  2607. }
  2608. buf_addr += pbe_size;
  2609. num_pbes += 1;
  2610. total_num_pbes += 1;
  2611. pbe++;
  2612. if (total_num_pbes == hwmr->num_pbes)
  2613. goto mr_tbl_done;
  2614. /* if the pbl is full storing the pbes,
  2615. * move to next pbl.
  2616. */
  2617. if (num_pbes == (hwmr->pbl_size/sizeof(u64))) {
  2618. pbl_tbl++;
  2619. pbe = (struct ocrdma_pbe *)pbl_tbl->va;
  2620. num_pbes = 0;
  2621. }
  2622. }
  2623. }
  2624. mr_tbl_done:
  2625. return;
  2626. }
  2627. struct ib_mr *ocrdma_reg_kernel_mr(struct ib_pd *ibpd,
  2628. struct ib_phys_buf *buf_list,
  2629. int buf_cnt, int acc, u64 *iova_start)
  2630. {
  2631. int status = -ENOMEM;
  2632. struct ocrdma_mr *mr;
  2633. struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
  2634. struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
  2635. u32 num_pbes;
  2636. u32 pbe_size = 0;
  2637. if ((acc & IB_ACCESS_REMOTE_WRITE) && !(acc & IB_ACCESS_LOCAL_WRITE))
  2638. return ERR_PTR(-EINVAL);
  2639. mr = kzalloc(sizeof(*mr), GFP_KERNEL);
  2640. if (!mr)
  2641. return ERR_PTR(status);
  2642. num_pbes = count_kernel_pbes(buf_list, buf_cnt, &pbe_size);
  2643. if (num_pbes == 0) {
  2644. status = -EINVAL;
  2645. goto pbl_err;
  2646. }
  2647. status = ocrdma_get_pbl_info(dev, mr, num_pbes);
  2648. if (status)
  2649. goto pbl_err;
  2650. mr->hwmr.pbe_size = pbe_size;
  2651. mr->hwmr.fbo = *iova_start - (buf_list[0].addr & PAGE_MASK);
  2652. mr->hwmr.va = *iova_start;
  2653. mr->hwmr.local_rd = 1;
  2654. mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
  2655. mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
  2656. mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
  2657. mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
  2658. mr->hwmr.mw_bind = (acc & IB_ACCESS_MW_BIND) ? 1 : 0;
  2659. status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
  2660. if (status)
  2661. goto pbl_err;
  2662. build_kernel_pbes(buf_list, buf_cnt, pbe_size, mr->hwmr.pbl_table,
  2663. &mr->hwmr);
  2664. status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, acc);
  2665. if (status)
  2666. goto mbx_err;
  2667. mr->ibmr.lkey = mr->hwmr.lkey;
  2668. if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
  2669. mr->ibmr.rkey = mr->hwmr.lkey;
  2670. return &mr->ibmr;
  2671. mbx_err:
  2672. ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
  2673. pbl_err:
  2674. kfree(mr);
  2675. return ERR_PTR(status);
  2676. }