ocrdma_verbs.c 66 KB

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