ocrdma_verbs.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031
  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 | IB_PORT_IP_BASED_GIDS;
  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. IB_LINK_LAYER_ETHERNET)) {
  1167. pr_err("%s(%d) invalid attribute mask=0x%x specified for\n"
  1168. "qpn=0x%x of type=0x%x old_qps=0x%x, new_qps=0x%x\n",
  1169. __func__, dev->id, attr_mask, qp->id, ibqp->qp_type,
  1170. old_qps, new_qps);
  1171. goto param_err;
  1172. }
  1173. status = _ocrdma_modify_qp(ibqp, attr, attr_mask);
  1174. if (status > 0)
  1175. status = 0;
  1176. param_err:
  1177. mutex_unlock(&dev->dev_lock);
  1178. return status;
  1179. }
  1180. static enum ib_mtu ocrdma_mtu_int_to_enum(u16 mtu)
  1181. {
  1182. switch (mtu) {
  1183. case 256:
  1184. return IB_MTU_256;
  1185. case 512:
  1186. return IB_MTU_512;
  1187. case 1024:
  1188. return IB_MTU_1024;
  1189. case 2048:
  1190. return IB_MTU_2048;
  1191. case 4096:
  1192. return IB_MTU_4096;
  1193. default:
  1194. return IB_MTU_1024;
  1195. }
  1196. }
  1197. static int ocrdma_to_ib_qp_acc_flags(int qp_cap_flags)
  1198. {
  1199. int ib_qp_acc_flags = 0;
  1200. if (qp_cap_flags & OCRDMA_QP_INB_WR)
  1201. ib_qp_acc_flags |= IB_ACCESS_REMOTE_WRITE;
  1202. if (qp_cap_flags & OCRDMA_QP_INB_RD)
  1203. ib_qp_acc_flags |= IB_ACCESS_LOCAL_WRITE;
  1204. return ib_qp_acc_flags;
  1205. }
  1206. int ocrdma_query_qp(struct ib_qp *ibqp,
  1207. struct ib_qp_attr *qp_attr,
  1208. int attr_mask, struct ib_qp_init_attr *qp_init_attr)
  1209. {
  1210. int status;
  1211. u32 qp_state;
  1212. struct ocrdma_qp_params params;
  1213. struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
  1214. struct ocrdma_dev *dev = qp->dev;
  1215. memset(&params, 0, sizeof(params));
  1216. mutex_lock(&dev->dev_lock);
  1217. status = ocrdma_mbx_query_qp(dev, qp, &params);
  1218. mutex_unlock(&dev->dev_lock);
  1219. if (status)
  1220. goto mbx_err;
  1221. qp_attr->qp_state = get_ibqp_state(IB_QPS_INIT);
  1222. qp_attr->cur_qp_state = get_ibqp_state(IB_QPS_INIT);
  1223. qp_attr->path_mtu =
  1224. ocrdma_mtu_int_to_enum(params.path_mtu_pkey_indx &
  1225. OCRDMA_QP_PARAMS_PATH_MTU_MASK) >>
  1226. OCRDMA_QP_PARAMS_PATH_MTU_SHIFT;
  1227. qp_attr->path_mig_state = IB_MIG_MIGRATED;
  1228. qp_attr->rq_psn = params.hop_lmt_rq_psn & OCRDMA_QP_PARAMS_RQ_PSN_MASK;
  1229. qp_attr->sq_psn = params.tclass_sq_psn & OCRDMA_QP_PARAMS_SQ_PSN_MASK;
  1230. qp_attr->dest_qp_num =
  1231. params.ack_to_rnr_rtc_dest_qpn & OCRDMA_QP_PARAMS_DEST_QPN_MASK;
  1232. qp_attr->qp_access_flags = ocrdma_to_ib_qp_acc_flags(qp->cap_flags);
  1233. qp_attr->cap.max_send_wr = qp->sq.max_cnt - 1;
  1234. qp_attr->cap.max_recv_wr = qp->rq.max_cnt - 1;
  1235. qp_attr->cap.max_send_sge = qp->sq.max_sges;
  1236. qp_attr->cap.max_recv_sge = qp->rq.max_sges;
  1237. qp_attr->cap.max_inline_data = qp->max_inline_data;
  1238. qp_init_attr->cap = qp_attr->cap;
  1239. memcpy(&qp_attr->ah_attr.grh.dgid, &params.dgid[0],
  1240. sizeof(params.dgid));
  1241. qp_attr->ah_attr.grh.flow_label = params.rnt_rc_sl_fl &
  1242. OCRDMA_QP_PARAMS_FLOW_LABEL_MASK;
  1243. qp_attr->ah_attr.grh.sgid_index = qp->sgid_idx;
  1244. qp_attr->ah_attr.grh.hop_limit = (params.hop_lmt_rq_psn &
  1245. OCRDMA_QP_PARAMS_HOP_LMT_MASK) >>
  1246. OCRDMA_QP_PARAMS_HOP_LMT_SHIFT;
  1247. qp_attr->ah_attr.grh.traffic_class = (params.tclass_sq_psn &
  1248. OCRDMA_QP_PARAMS_TCLASS_MASK) >>
  1249. OCRDMA_QP_PARAMS_TCLASS_SHIFT;
  1250. qp_attr->ah_attr.ah_flags = IB_AH_GRH;
  1251. qp_attr->ah_attr.port_num = 1;
  1252. qp_attr->ah_attr.sl = (params.rnt_rc_sl_fl &
  1253. OCRDMA_QP_PARAMS_SL_MASK) >>
  1254. OCRDMA_QP_PARAMS_SL_SHIFT;
  1255. qp_attr->timeout = (params.ack_to_rnr_rtc_dest_qpn &
  1256. OCRDMA_QP_PARAMS_ACK_TIMEOUT_MASK) >>
  1257. OCRDMA_QP_PARAMS_ACK_TIMEOUT_SHIFT;
  1258. qp_attr->rnr_retry = (params.ack_to_rnr_rtc_dest_qpn &
  1259. OCRDMA_QP_PARAMS_RNR_RETRY_CNT_MASK) >>
  1260. OCRDMA_QP_PARAMS_RNR_RETRY_CNT_SHIFT;
  1261. qp_attr->retry_cnt =
  1262. (params.rnt_rc_sl_fl & OCRDMA_QP_PARAMS_RETRY_CNT_MASK) >>
  1263. OCRDMA_QP_PARAMS_RETRY_CNT_SHIFT;
  1264. qp_attr->min_rnr_timer = 0;
  1265. qp_attr->pkey_index = 0;
  1266. qp_attr->port_num = 1;
  1267. qp_attr->ah_attr.src_path_bits = 0;
  1268. qp_attr->ah_attr.static_rate = 0;
  1269. qp_attr->alt_pkey_index = 0;
  1270. qp_attr->alt_port_num = 0;
  1271. qp_attr->alt_timeout = 0;
  1272. memset(&qp_attr->alt_ah_attr, 0, sizeof(qp_attr->alt_ah_attr));
  1273. qp_state = (params.max_sge_recv_flags & OCRDMA_QP_PARAMS_STATE_MASK) >>
  1274. OCRDMA_QP_PARAMS_STATE_SHIFT;
  1275. qp_attr->sq_draining = (qp_state == OCRDMA_QPS_SQ_DRAINING) ? 1 : 0;
  1276. qp_attr->max_dest_rd_atomic =
  1277. params.max_ord_ird >> OCRDMA_QP_PARAMS_MAX_ORD_SHIFT;
  1278. qp_attr->max_rd_atomic =
  1279. params.max_ord_ird & OCRDMA_QP_PARAMS_MAX_IRD_MASK;
  1280. qp_attr->en_sqd_async_notify = (params.max_sge_recv_flags &
  1281. OCRDMA_QP_PARAMS_FLAGS_SQD_ASYNC) ? 1 : 0;
  1282. mbx_err:
  1283. return status;
  1284. }
  1285. static void ocrdma_srq_toggle_bit(struct ocrdma_srq *srq, int idx)
  1286. {
  1287. int i = idx / 32;
  1288. unsigned int mask = (1 << (idx % 32));
  1289. if (srq->idx_bit_fields[i] & mask)
  1290. srq->idx_bit_fields[i] &= ~mask;
  1291. else
  1292. srq->idx_bit_fields[i] |= mask;
  1293. }
  1294. static int ocrdma_hwq_free_cnt(struct ocrdma_qp_hwq_info *q)
  1295. {
  1296. return ((q->max_wqe_idx - q->head) + q->tail) % q->max_cnt;
  1297. }
  1298. static int is_hw_sq_empty(struct ocrdma_qp *qp)
  1299. {
  1300. return (qp->sq.tail == qp->sq.head);
  1301. }
  1302. static int is_hw_rq_empty(struct ocrdma_qp *qp)
  1303. {
  1304. return (qp->rq.tail == qp->rq.head);
  1305. }
  1306. static void *ocrdma_hwq_head(struct ocrdma_qp_hwq_info *q)
  1307. {
  1308. return q->va + (q->head * q->entry_size);
  1309. }
  1310. static void *ocrdma_hwq_head_from_idx(struct ocrdma_qp_hwq_info *q,
  1311. u32 idx)
  1312. {
  1313. return q->va + (idx * q->entry_size);
  1314. }
  1315. static void ocrdma_hwq_inc_head(struct ocrdma_qp_hwq_info *q)
  1316. {
  1317. q->head = (q->head + 1) & q->max_wqe_idx;
  1318. }
  1319. static void ocrdma_hwq_inc_tail(struct ocrdma_qp_hwq_info *q)
  1320. {
  1321. q->tail = (q->tail + 1) & q->max_wqe_idx;
  1322. }
  1323. /* discard the cqe for a given QP */
  1324. static void ocrdma_discard_cqes(struct ocrdma_qp *qp, struct ocrdma_cq *cq)
  1325. {
  1326. unsigned long cq_flags;
  1327. unsigned long flags;
  1328. int discard_cnt = 0;
  1329. u32 cur_getp, stop_getp;
  1330. struct ocrdma_cqe *cqe;
  1331. u32 qpn = 0;
  1332. spin_lock_irqsave(&cq->cq_lock, cq_flags);
  1333. /* traverse through the CQEs in the hw CQ,
  1334. * find the matching CQE for a given qp,
  1335. * mark the matching one discarded by clearing qpn.
  1336. * ring the doorbell in the poll_cq() as
  1337. * we don't complete out of order cqe.
  1338. */
  1339. cur_getp = cq->getp;
  1340. /* find upto when do we reap the cq. */
  1341. stop_getp = cur_getp;
  1342. do {
  1343. if (is_hw_sq_empty(qp) && (!qp->srq && is_hw_rq_empty(qp)))
  1344. break;
  1345. cqe = cq->va + cur_getp;
  1346. /* if (a) done reaping whole hw cq, or
  1347. * (b) qp_xq becomes empty.
  1348. * then exit
  1349. */
  1350. qpn = cqe->cmn.qpn & OCRDMA_CQE_QPN_MASK;
  1351. /* if previously discarded cqe found, skip that too. */
  1352. /* check for matching qp */
  1353. if (qpn == 0 || qpn != qp->id)
  1354. goto skip_cqe;
  1355. /* mark cqe discarded so that it is not picked up later
  1356. * in the poll_cq().
  1357. */
  1358. discard_cnt += 1;
  1359. cqe->cmn.qpn = 0;
  1360. if (is_cqe_for_sq(cqe)) {
  1361. ocrdma_hwq_inc_tail(&qp->sq);
  1362. } else {
  1363. if (qp->srq) {
  1364. spin_lock_irqsave(&qp->srq->q_lock, flags);
  1365. ocrdma_hwq_inc_tail(&qp->srq->rq);
  1366. ocrdma_srq_toggle_bit(qp->srq, cur_getp);
  1367. spin_unlock_irqrestore(&qp->srq->q_lock, flags);
  1368. } else {
  1369. ocrdma_hwq_inc_tail(&qp->rq);
  1370. }
  1371. }
  1372. skip_cqe:
  1373. cur_getp = (cur_getp + 1) % cq->max_hw_cqe;
  1374. } while (cur_getp != stop_getp);
  1375. spin_unlock_irqrestore(&cq->cq_lock, cq_flags);
  1376. }
  1377. void ocrdma_del_flush_qp(struct ocrdma_qp *qp)
  1378. {
  1379. int found = false;
  1380. unsigned long flags;
  1381. struct ocrdma_dev *dev = qp->dev;
  1382. /* sync with any active CQ poll */
  1383. spin_lock_irqsave(&dev->flush_q_lock, flags);
  1384. found = ocrdma_is_qp_in_sq_flushlist(qp->sq_cq, qp);
  1385. if (found)
  1386. list_del(&qp->sq_entry);
  1387. if (!qp->srq) {
  1388. found = ocrdma_is_qp_in_rq_flushlist(qp->rq_cq, qp);
  1389. if (found)
  1390. list_del(&qp->rq_entry);
  1391. }
  1392. spin_unlock_irqrestore(&dev->flush_q_lock, flags);
  1393. }
  1394. int ocrdma_destroy_qp(struct ib_qp *ibqp)
  1395. {
  1396. int status;
  1397. struct ocrdma_pd *pd;
  1398. struct ocrdma_qp *qp;
  1399. struct ocrdma_dev *dev;
  1400. struct ib_qp_attr attrs;
  1401. int attr_mask = IB_QP_STATE;
  1402. unsigned long flags;
  1403. qp = get_ocrdma_qp(ibqp);
  1404. dev = qp->dev;
  1405. attrs.qp_state = IB_QPS_ERR;
  1406. pd = qp->pd;
  1407. /* change the QP state to ERROR */
  1408. _ocrdma_modify_qp(ibqp, &attrs, attr_mask);
  1409. /* ensure that CQEs for newly created QP (whose id may be same with
  1410. * one which just getting destroyed are same), dont get
  1411. * discarded until the old CQEs are discarded.
  1412. */
  1413. mutex_lock(&dev->dev_lock);
  1414. status = ocrdma_mbx_destroy_qp(dev, qp);
  1415. /*
  1416. * acquire CQ lock while destroy is in progress, in order to
  1417. * protect against proessing in-flight CQEs for this QP.
  1418. */
  1419. spin_lock_irqsave(&qp->sq_cq->cq_lock, flags);
  1420. if (qp->rq_cq && (qp->rq_cq != qp->sq_cq))
  1421. spin_lock(&qp->rq_cq->cq_lock);
  1422. ocrdma_del_qpn_map(dev, qp);
  1423. if (qp->rq_cq && (qp->rq_cq != qp->sq_cq))
  1424. spin_unlock(&qp->rq_cq->cq_lock);
  1425. spin_unlock_irqrestore(&qp->sq_cq->cq_lock, flags);
  1426. if (!pd->uctx) {
  1427. ocrdma_discard_cqes(qp, qp->sq_cq);
  1428. ocrdma_discard_cqes(qp, qp->rq_cq);
  1429. }
  1430. mutex_unlock(&dev->dev_lock);
  1431. if (pd->uctx) {
  1432. ocrdma_del_mmap(pd->uctx, (u64) qp->sq.pa,
  1433. PAGE_ALIGN(qp->sq.len));
  1434. if (!qp->srq)
  1435. ocrdma_del_mmap(pd->uctx, (u64) qp->rq.pa,
  1436. PAGE_ALIGN(qp->rq.len));
  1437. }
  1438. ocrdma_del_flush_qp(qp);
  1439. kfree(qp->wqe_wr_id_tbl);
  1440. kfree(qp->rqe_wr_id_tbl);
  1441. kfree(qp);
  1442. return status;
  1443. }
  1444. static int ocrdma_copy_srq_uresp(struct ocrdma_dev *dev, struct ocrdma_srq *srq,
  1445. struct ib_udata *udata)
  1446. {
  1447. int status;
  1448. struct ocrdma_create_srq_uresp uresp;
  1449. memset(&uresp, 0, sizeof(uresp));
  1450. uresp.rq_dbid = srq->rq.dbid;
  1451. uresp.num_rq_pages = 1;
  1452. uresp.rq_page_addr[0] = srq->rq.pa;
  1453. uresp.rq_page_size = srq->rq.len;
  1454. uresp.db_page_addr = dev->nic_info.unmapped_db +
  1455. (srq->pd->id * dev->nic_info.db_page_size);
  1456. uresp.db_page_size = dev->nic_info.db_page_size;
  1457. uresp.num_rqe_allocated = srq->rq.max_cnt;
  1458. if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) {
  1459. uresp.db_rq_offset = OCRDMA_DB_GEN2_RQ_OFFSET;
  1460. uresp.db_shift = 24;
  1461. } else {
  1462. uresp.db_rq_offset = OCRDMA_DB_RQ_OFFSET;
  1463. uresp.db_shift = 16;
  1464. }
  1465. status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
  1466. if (status)
  1467. return status;
  1468. status = ocrdma_add_mmap(srq->pd->uctx, uresp.rq_page_addr[0],
  1469. uresp.rq_page_size);
  1470. if (status)
  1471. return status;
  1472. return status;
  1473. }
  1474. struct ib_srq *ocrdma_create_srq(struct ib_pd *ibpd,
  1475. struct ib_srq_init_attr *init_attr,
  1476. struct ib_udata *udata)
  1477. {
  1478. int status = -ENOMEM;
  1479. struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
  1480. struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
  1481. struct ocrdma_srq *srq;
  1482. if (init_attr->attr.max_sge > dev->attr.max_recv_sge)
  1483. return ERR_PTR(-EINVAL);
  1484. if (init_attr->attr.max_wr > dev->attr.max_rqe)
  1485. return ERR_PTR(-EINVAL);
  1486. srq = kzalloc(sizeof(*srq), GFP_KERNEL);
  1487. if (!srq)
  1488. return ERR_PTR(status);
  1489. spin_lock_init(&srq->q_lock);
  1490. srq->pd = pd;
  1491. srq->db = dev->nic_info.db + (pd->id * dev->nic_info.db_page_size);
  1492. status = ocrdma_mbx_create_srq(dev, srq, init_attr, pd);
  1493. if (status)
  1494. goto err;
  1495. if (udata == NULL) {
  1496. srq->rqe_wr_id_tbl = kzalloc(sizeof(u64) * srq->rq.max_cnt,
  1497. GFP_KERNEL);
  1498. if (srq->rqe_wr_id_tbl == NULL)
  1499. goto arm_err;
  1500. srq->bit_fields_len = (srq->rq.max_cnt / 32) +
  1501. (srq->rq.max_cnt % 32 ? 1 : 0);
  1502. srq->idx_bit_fields =
  1503. kmalloc(srq->bit_fields_len * sizeof(u32), GFP_KERNEL);
  1504. if (srq->idx_bit_fields == NULL)
  1505. goto arm_err;
  1506. memset(srq->idx_bit_fields, 0xff,
  1507. srq->bit_fields_len * sizeof(u32));
  1508. }
  1509. if (init_attr->attr.srq_limit) {
  1510. status = ocrdma_mbx_modify_srq(srq, &init_attr->attr);
  1511. if (status)
  1512. goto arm_err;
  1513. }
  1514. if (udata) {
  1515. status = ocrdma_copy_srq_uresp(dev, srq, udata);
  1516. if (status)
  1517. goto arm_err;
  1518. }
  1519. return &srq->ibsrq;
  1520. arm_err:
  1521. ocrdma_mbx_destroy_srq(dev, srq);
  1522. err:
  1523. kfree(srq->rqe_wr_id_tbl);
  1524. kfree(srq->idx_bit_fields);
  1525. kfree(srq);
  1526. return ERR_PTR(status);
  1527. }
  1528. int ocrdma_modify_srq(struct ib_srq *ibsrq,
  1529. struct ib_srq_attr *srq_attr,
  1530. enum ib_srq_attr_mask srq_attr_mask,
  1531. struct ib_udata *udata)
  1532. {
  1533. int status = 0;
  1534. struct ocrdma_srq *srq;
  1535. srq = get_ocrdma_srq(ibsrq);
  1536. if (srq_attr_mask & IB_SRQ_MAX_WR)
  1537. status = -EINVAL;
  1538. else
  1539. status = ocrdma_mbx_modify_srq(srq, srq_attr);
  1540. return status;
  1541. }
  1542. int ocrdma_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
  1543. {
  1544. int status;
  1545. struct ocrdma_srq *srq;
  1546. srq = get_ocrdma_srq(ibsrq);
  1547. status = ocrdma_mbx_query_srq(srq, srq_attr);
  1548. return status;
  1549. }
  1550. int ocrdma_destroy_srq(struct ib_srq *ibsrq)
  1551. {
  1552. int status;
  1553. struct ocrdma_srq *srq;
  1554. struct ocrdma_dev *dev = get_ocrdma_dev(ibsrq->device);
  1555. srq = get_ocrdma_srq(ibsrq);
  1556. status = ocrdma_mbx_destroy_srq(dev, srq);
  1557. if (srq->pd->uctx)
  1558. ocrdma_del_mmap(srq->pd->uctx, (u64) srq->rq.pa,
  1559. PAGE_ALIGN(srq->rq.len));
  1560. kfree(srq->idx_bit_fields);
  1561. kfree(srq->rqe_wr_id_tbl);
  1562. kfree(srq);
  1563. return status;
  1564. }
  1565. /* unprivileged verbs and their support functions. */
  1566. static void ocrdma_build_ud_hdr(struct ocrdma_qp *qp,
  1567. struct ocrdma_hdr_wqe *hdr,
  1568. struct ib_send_wr *wr)
  1569. {
  1570. struct ocrdma_ewqe_ud_hdr *ud_hdr =
  1571. (struct ocrdma_ewqe_ud_hdr *)(hdr + 1);
  1572. struct ocrdma_ah *ah = get_ocrdma_ah(wr->wr.ud.ah);
  1573. ud_hdr->rsvd_dest_qpn = wr->wr.ud.remote_qpn;
  1574. if (qp->qp_type == IB_QPT_GSI)
  1575. ud_hdr->qkey = qp->qkey;
  1576. else
  1577. ud_hdr->qkey = wr->wr.ud.remote_qkey;
  1578. ud_hdr->rsvd_ahid = ah->id;
  1579. }
  1580. static void ocrdma_build_sges(struct ocrdma_hdr_wqe *hdr,
  1581. struct ocrdma_sge *sge, int num_sge,
  1582. struct ib_sge *sg_list)
  1583. {
  1584. int i;
  1585. for (i = 0; i < num_sge; i++) {
  1586. sge[i].lrkey = sg_list[i].lkey;
  1587. sge[i].addr_lo = sg_list[i].addr;
  1588. sge[i].addr_hi = upper_32_bits(sg_list[i].addr);
  1589. sge[i].len = sg_list[i].length;
  1590. hdr->total_len += sg_list[i].length;
  1591. }
  1592. if (num_sge == 0)
  1593. memset(sge, 0, sizeof(*sge));
  1594. }
  1595. static inline uint32_t ocrdma_sglist_len(struct ib_sge *sg_list, int num_sge)
  1596. {
  1597. uint32_t total_len = 0, i;
  1598. for (i = 0; i < num_sge; i++)
  1599. total_len += sg_list[i].length;
  1600. return total_len;
  1601. }
  1602. static int ocrdma_build_inline_sges(struct ocrdma_qp *qp,
  1603. struct ocrdma_hdr_wqe *hdr,
  1604. struct ocrdma_sge *sge,
  1605. struct ib_send_wr *wr, u32 wqe_size)
  1606. {
  1607. int i;
  1608. char *dpp_addr;
  1609. if (wr->send_flags & IB_SEND_INLINE && qp->qp_type != IB_QPT_UD) {
  1610. hdr->total_len = ocrdma_sglist_len(wr->sg_list, wr->num_sge);
  1611. if (unlikely(hdr->total_len > qp->max_inline_data)) {
  1612. pr_err("%s() supported_len=0x%x,\n"
  1613. " unspported len req=0x%x\n", __func__,
  1614. qp->max_inline_data, hdr->total_len);
  1615. return -EINVAL;
  1616. }
  1617. dpp_addr = (char *)sge;
  1618. for (i = 0; i < wr->num_sge; i++) {
  1619. memcpy(dpp_addr,
  1620. (void *)(unsigned long)wr->sg_list[i].addr,
  1621. wr->sg_list[i].length);
  1622. dpp_addr += wr->sg_list[i].length;
  1623. }
  1624. wqe_size += roundup(hdr->total_len, OCRDMA_WQE_ALIGN_BYTES);
  1625. if (0 == hdr->total_len)
  1626. wqe_size += sizeof(struct ocrdma_sge);
  1627. hdr->cw |= (OCRDMA_TYPE_INLINE << OCRDMA_WQE_TYPE_SHIFT);
  1628. } else {
  1629. ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list);
  1630. if (wr->num_sge)
  1631. wqe_size += (wr->num_sge * sizeof(struct ocrdma_sge));
  1632. else
  1633. wqe_size += sizeof(struct ocrdma_sge);
  1634. hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
  1635. }
  1636. hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
  1637. return 0;
  1638. }
  1639. static int ocrdma_build_send(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
  1640. struct ib_send_wr *wr)
  1641. {
  1642. int status;
  1643. struct ocrdma_sge *sge;
  1644. u32 wqe_size = sizeof(*hdr);
  1645. if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
  1646. ocrdma_build_ud_hdr(qp, hdr, wr);
  1647. sge = (struct ocrdma_sge *)(hdr + 2);
  1648. wqe_size += sizeof(struct ocrdma_ewqe_ud_hdr);
  1649. } else {
  1650. sge = (struct ocrdma_sge *)(hdr + 1);
  1651. }
  1652. status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size);
  1653. return status;
  1654. }
  1655. static int ocrdma_build_write(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
  1656. struct ib_send_wr *wr)
  1657. {
  1658. int status;
  1659. struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1);
  1660. struct ocrdma_sge *sge = ext_rw + 1;
  1661. u32 wqe_size = sizeof(*hdr) + sizeof(*ext_rw);
  1662. status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size);
  1663. if (status)
  1664. return status;
  1665. ext_rw->addr_lo = wr->wr.rdma.remote_addr;
  1666. ext_rw->addr_hi = upper_32_bits(wr->wr.rdma.remote_addr);
  1667. ext_rw->lrkey = wr->wr.rdma.rkey;
  1668. ext_rw->len = hdr->total_len;
  1669. return 0;
  1670. }
  1671. static void ocrdma_build_read(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
  1672. struct ib_send_wr *wr)
  1673. {
  1674. struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1);
  1675. struct ocrdma_sge *sge = ext_rw + 1;
  1676. u32 wqe_size = ((wr->num_sge + 1) * sizeof(struct ocrdma_sge)) +
  1677. sizeof(struct ocrdma_hdr_wqe);
  1678. ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list);
  1679. hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
  1680. hdr->cw |= (OCRDMA_READ << OCRDMA_WQE_OPCODE_SHIFT);
  1681. hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
  1682. ext_rw->addr_lo = wr->wr.rdma.remote_addr;
  1683. ext_rw->addr_hi = upper_32_bits(wr->wr.rdma.remote_addr);
  1684. ext_rw->lrkey = wr->wr.rdma.rkey;
  1685. ext_rw->len = hdr->total_len;
  1686. }
  1687. static void build_frmr_pbes(struct ib_send_wr *wr, struct ocrdma_pbl *pbl_tbl,
  1688. struct ocrdma_hw_mr *hwmr)
  1689. {
  1690. int i;
  1691. u64 buf_addr = 0;
  1692. int num_pbes;
  1693. struct ocrdma_pbe *pbe;
  1694. pbe = (struct ocrdma_pbe *)pbl_tbl->va;
  1695. num_pbes = 0;
  1696. /* go through the OS phy regions & fill hw pbe entries into pbls. */
  1697. for (i = 0; i < wr->wr.fast_reg.page_list_len; i++) {
  1698. /* number of pbes can be more for one OS buf, when
  1699. * buffers are of different sizes.
  1700. * split the ib_buf to one or more pbes.
  1701. */
  1702. buf_addr = wr->wr.fast_reg.page_list->page_list[i];
  1703. pbe->pa_lo = cpu_to_le32((u32) (buf_addr & PAGE_MASK));
  1704. pbe->pa_hi = cpu_to_le32((u32) upper_32_bits(buf_addr));
  1705. num_pbes += 1;
  1706. pbe++;
  1707. /* if the pbl is full storing the pbes,
  1708. * move to next pbl.
  1709. */
  1710. if (num_pbes == (hwmr->pbl_size/sizeof(u64))) {
  1711. pbl_tbl++;
  1712. pbe = (struct ocrdma_pbe *)pbl_tbl->va;
  1713. }
  1714. }
  1715. return;
  1716. }
  1717. static int get_encoded_page_size(int pg_sz)
  1718. {
  1719. /* Max size is 256M 4096 << 16 */
  1720. int i = 0;
  1721. for (; i < 17; i++)
  1722. if (pg_sz == (4096 << i))
  1723. break;
  1724. return i;
  1725. }
  1726. static int ocrdma_build_fr(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
  1727. struct ib_send_wr *wr)
  1728. {
  1729. u64 fbo;
  1730. struct ocrdma_ewqe_fr *fast_reg = (struct ocrdma_ewqe_fr *)(hdr + 1);
  1731. struct ocrdma_mr *mr;
  1732. u32 wqe_size = sizeof(*fast_reg) + sizeof(*hdr);
  1733. wqe_size = roundup(wqe_size, OCRDMA_WQE_ALIGN_BYTES);
  1734. if (wr->wr.fast_reg.page_list_len > qp->dev->attr.max_pages_per_frmr)
  1735. return -EINVAL;
  1736. hdr->cw |= (OCRDMA_FR_MR << OCRDMA_WQE_OPCODE_SHIFT);
  1737. hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
  1738. if (wr->wr.fast_reg.page_list_len == 0)
  1739. BUG();
  1740. if (wr->wr.fast_reg.access_flags & IB_ACCESS_LOCAL_WRITE)
  1741. hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_LOCAL_WR;
  1742. if (wr->wr.fast_reg.access_flags & IB_ACCESS_REMOTE_WRITE)
  1743. hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_REMOTE_WR;
  1744. if (wr->wr.fast_reg.access_flags & IB_ACCESS_REMOTE_READ)
  1745. hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_REMOTE_RD;
  1746. hdr->lkey = wr->wr.fast_reg.rkey;
  1747. hdr->total_len = wr->wr.fast_reg.length;
  1748. fbo = wr->wr.fast_reg.iova_start -
  1749. (wr->wr.fast_reg.page_list->page_list[0] & PAGE_MASK);
  1750. fast_reg->va_hi = upper_32_bits(wr->wr.fast_reg.iova_start);
  1751. fast_reg->va_lo = (u32) (wr->wr.fast_reg.iova_start & 0xffffffff);
  1752. fast_reg->fbo_hi = upper_32_bits(fbo);
  1753. fast_reg->fbo_lo = (u32) fbo & 0xffffffff;
  1754. fast_reg->num_sges = wr->wr.fast_reg.page_list_len;
  1755. fast_reg->size_sge =
  1756. get_encoded_page_size(1 << wr->wr.fast_reg.page_shift);
  1757. mr = (struct ocrdma_mr *) (unsigned long) qp->dev->stag_arr[(hdr->lkey >> 8) &
  1758. (OCRDMA_MAX_STAG - 1)];
  1759. build_frmr_pbes(wr, mr->hwmr.pbl_table, &mr->hwmr);
  1760. return 0;
  1761. }
  1762. static void ocrdma_ring_sq_db(struct ocrdma_qp *qp)
  1763. {
  1764. u32 val = qp->sq.dbid | (1 << 16);
  1765. iowrite32(val, qp->sq_db);
  1766. }
  1767. int ocrdma_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  1768. struct ib_send_wr **bad_wr)
  1769. {
  1770. int status = 0;
  1771. struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
  1772. struct ocrdma_hdr_wqe *hdr;
  1773. unsigned long flags;
  1774. spin_lock_irqsave(&qp->q_lock, flags);
  1775. if (qp->state != OCRDMA_QPS_RTS && qp->state != OCRDMA_QPS_SQD) {
  1776. spin_unlock_irqrestore(&qp->q_lock, flags);
  1777. *bad_wr = wr;
  1778. return -EINVAL;
  1779. }
  1780. while (wr) {
  1781. if (ocrdma_hwq_free_cnt(&qp->sq) == 0 ||
  1782. wr->num_sge > qp->sq.max_sges) {
  1783. *bad_wr = wr;
  1784. status = -ENOMEM;
  1785. break;
  1786. }
  1787. hdr = ocrdma_hwq_head(&qp->sq);
  1788. hdr->cw = 0;
  1789. if (wr->send_flags & IB_SEND_SIGNALED || qp->signaled)
  1790. hdr->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT);
  1791. if (wr->send_flags & IB_SEND_FENCE)
  1792. hdr->cw |=
  1793. (OCRDMA_FLAG_FENCE_L << OCRDMA_WQE_FLAGS_SHIFT);
  1794. if (wr->send_flags & IB_SEND_SOLICITED)
  1795. hdr->cw |=
  1796. (OCRDMA_FLAG_SOLICIT << OCRDMA_WQE_FLAGS_SHIFT);
  1797. hdr->total_len = 0;
  1798. switch (wr->opcode) {
  1799. case IB_WR_SEND_WITH_IMM:
  1800. hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT);
  1801. hdr->immdt = ntohl(wr->ex.imm_data);
  1802. case IB_WR_SEND:
  1803. hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT);
  1804. ocrdma_build_send(qp, hdr, wr);
  1805. break;
  1806. case IB_WR_SEND_WITH_INV:
  1807. hdr->cw |= (OCRDMA_FLAG_INV << OCRDMA_WQE_FLAGS_SHIFT);
  1808. hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT);
  1809. hdr->lkey = wr->ex.invalidate_rkey;
  1810. status = ocrdma_build_send(qp, hdr, wr);
  1811. break;
  1812. case IB_WR_RDMA_WRITE_WITH_IMM:
  1813. hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT);
  1814. hdr->immdt = ntohl(wr->ex.imm_data);
  1815. case IB_WR_RDMA_WRITE:
  1816. hdr->cw |= (OCRDMA_WRITE << OCRDMA_WQE_OPCODE_SHIFT);
  1817. status = ocrdma_build_write(qp, hdr, wr);
  1818. break;
  1819. case IB_WR_RDMA_READ_WITH_INV:
  1820. hdr->cw |= (OCRDMA_FLAG_INV << OCRDMA_WQE_FLAGS_SHIFT);
  1821. case IB_WR_RDMA_READ:
  1822. ocrdma_build_read(qp, hdr, wr);
  1823. break;
  1824. case IB_WR_LOCAL_INV:
  1825. hdr->cw |=
  1826. (OCRDMA_LKEY_INV << OCRDMA_WQE_OPCODE_SHIFT);
  1827. hdr->cw |= ((sizeof(struct ocrdma_hdr_wqe) +
  1828. sizeof(struct ocrdma_sge)) /
  1829. OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT;
  1830. hdr->lkey = wr->ex.invalidate_rkey;
  1831. break;
  1832. case IB_WR_FAST_REG_MR:
  1833. status = ocrdma_build_fr(qp, hdr, wr);
  1834. break;
  1835. default:
  1836. status = -EINVAL;
  1837. break;
  1838. }
  1839. if (status) {
  1840. *bad_wr = wr;
  1841. break;
  1842. }
  1843. if (wr->send_flags & IB_SEND_SIGNALED || qp->signaled)
  1844. qp->wqe_wr_id_tbl[qp->sq.head].signaled = 1;
  1845. else
  1846. qp->wqe_wr_id_tbl[qp->sq.head].signaled = 0;
  1847. qp->wqe_wr_id_tbl[qp->sq.head].wrid = wr->wr_id;
  1848. ocrdma_cpu_to_le32(hdr, ((hdr->cw >> OCRDMA_WQE_SIZE_SHIFT) &
  1849. OCRDMA_WQE_SIZE_MASK) * OCRDMA_WQE_STRIDE);
  1850. /* make sure wqe is written before adapter can access it */
  1851. wmb();
  1852. /* inform hw to start processing it */
  1853. ocrdma_ring_sq_db(qp);
  1854. /* update pointer, counter for next wr */
  1855. ocrdma_hwq_inc_head(&qp->sq);
  1856. wr = wr->next;
  1857. }
  1858. spin_unlock_irqrestore(&qp->q_lock, flags);
  1859. return status;
  1860. }
  1861. static void ocrdma_ring_rq_db(struct ocrdma_qp *qp)
  1862. {
  1863. u32 val = qp->rq.dbid | (1 << ocrdma_get_num_posted_shift(qp));
  1864. if (qp->state != OCRDMA_QPS_INIT)
  1865. iowrite32(val, qp->rq_db);
  1866. else
  1867. qp->db_cache++;
  1868. }
  1869. static void ocrdma_build_rqe(struct ocrdma_hdr_wqe *rqe, struct ib_recv_wr *wr,
  1870. u16 tag)
  1871. {
  1872. u32 wqe_size = 0;
  1873. struct ocrdma_sge *sge;
  1874. if (wr->num_sge)
  1875. wqe_size = (wr->num_sge * sizeof(*sge)) + sizeof(*rqe);
  1876. else
  1877. wqe_size = sizeof(*sge) + sizeof(*rqe);
  1878. rqe->cw = ((wqe_size / OCRDMA_WQE_STRIDE) <<
  1879. OCRDMA_WQE_SIZE_SHIFT);
  1880. rqe->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT);
  1881. rqe->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
  1882. rqe->total_len = 0;
  1883. rqe->rsvd_tag = tag;
  1884. sge = (struct ocrdma_sge *)(rqe + 1);
  1885. ocrdma_build_sges(rqe, sge, wr->num_sge, wr->sg_list);
  1886. ocrdma_cpu_to_le32(rqe, wqe_size);
  1887. }
  1888. int ocrdma_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1889. struct ib_recv_wr **bad_wr)
  1890. {
  1891. int status = 0;
  1892. unsigned long flags;
  1893. struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
  1894. struct ocrdma_hdr_wqe *rqe;
  1895. spin_lock_irqsave(&qp->q_lock, flags);
  1896. if (qp->state == OCRDMA_QPS_RST || qp->state == OCRDMA_QPS_ERR) {
  1897. spin_unlock_irqrestore(&qp->q_lock, flags);
  1898. *bad_wr = wr;
  1899. return -EINVAL;
  1900. }
  1901. while (wr) {
  1902. if (ocrdma_hwq_free_cnt(&qp->rq) == 0 ||
  1903. wr->num_sge > qp->rq.max_sges) {
  1904. *bad_wr = wr;
  1905. status = -ENOMEM;
  1906. break;
  1907. }
  1908. rqe = ocrdma_hwq_head(&qp->rq);
  1909. ocrdma_build_rqe(rqe, wr, 0);
  1910. qp->rqe_wr_id_tbl[qp->rq.head] = wr->wr_id;
  1911. /* make sure rqe is written before adapter can access it */
  1912. wmb();
  1913. /* inform hw to start processing it */
  1914. ocrdma_ring_rq_db(qp);
  1915. /* update pointer, counter for next wr */
  1916. ocrdma_hwq_inc_head(&qp->rq);
  1917. wr = wr->next;
  1918. }
  1919. spin_unlock_irqrestore(&qp->q_lock, flags);
  1920. return status;
  1921. }
  1922. /* cqe for srq's rqe can potentially arrive out of order.
  1923. * index gives the entry in the shadow table where to store
  1924. * the wr_id. tag/index is returned in cqe to reference back
  1925. * for a given rqe.
  1926. */
  1927. static int ocrdma_srq_get_idx(struct ocrdma_srq *srq)
  1928. {
  1929. int row = 0;
  1930. int indx = 0;
  1931. for (row = 0; row < srq->bit_fields_len; row++) {
  1932. if (srq->idx_bit_fields[row]) {
  1933. indx = ffs(srq->idx_bit_fields[row]);
  1934. indx = (row * 32) + (indx - 1);
  1935. if (indx >= srq->rq.max_cnt)
  1936. BUG();
  1937. ocrdma_srq_toggle_bit(srq, indx);
  1938. break;
  1939. }
  1940. }
  1941. if (row == srq->bit_fields_len)
  1942. BUG();
  1943. return indx;
  1944. }
  1945. static void ocrdma_ring_srq_db(struct ocrdma_srq *srq)
  1946. {
  1947. u32 val = srq->rq.dbid | (1 << 16);
  1948. iowrite32(val, srq->db + OCRDMA_DB_GEN2_SRQ_OFFSET);
  1949. }
  1950. int ocrdma_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  1951. struct ib_recv_wr **bad_wr)
  1952. {
  1953. int status = 0;
  1954. unsigned long flags;
  1955. struct ocrdma_srq *srq;
  1956. struct ocrdma_hdr_wqe *rqe;
  1957. u16 tag;
  1958. srq = get_ocrdma_srq(ibsrq);
  1959. spin_lock_irqsave(&srq->q_lock, flags);
  1960. while (wr) {
  1961. if (ocrdma_hwq_free_cnt(&srq->rq) == 0 ||
  1962. wr->num_sge > srq->rq.max_sges) {
  1963. status = -ENOMEM;
  1964. *bad_wr = wr;
  1965. break;
  1966. }
  1967. tag = ocrdma_srq_get_idx(srq);
  1968. rqe = ocrdma_hwq_head(&srq->rq);
  1969. ocrdma_build_rqe(rqe, wr, tag);
  1970. srq->rqe_wr_id_tbl[tag] = wr->wr_id;
  1971. /* make sure rqe is written before adapter can perform DMA */
  1972. wmb();
  1973. /* inform hw to start processing it */
  1974. ocrdma_ring_srq_db(srq);
  1975. /* update pointer, counter for next wr */
  1976. ocrdma_hwq_inc_head(&srq->rq);
  1977. wr = wr->next;
  1978. }
  1979. spin_unlock_irqrestore(&srq->q_lock, flags);
  1980. return status;
  1981. }
  1982. static enum ib_wc_status ocrdma_to_ibwc_err(u16 status)
  1983. {
  1984. enum ib_wc_status ibwc_status;
  1985. switch (status) {
  1986. case OCRDMA_CQE_GENERAL_ERR:
  1987. ibwc_status = IB_WC_GENERAL_ERR;
  1988. break;
  1989. case OCRDMA_CQE_LOC_LEN_ERR:
  1990. ibwc_status = IB_WC_LOC_LEN_ERR;
  1991. break;
  1992. case OCRDMA_CQE_LOC_QP_OP_ERR:
  1993. ibwc_status = IB_WC_LOC_QP_OP_ERR;
  1994. break;
  1995. case OCRDMA_CQE_LOC_EEC_OP_ERR:
  1996. ibwc_status = IB_WC_LOC_EEC_OP_ERR;
  1997. break;
  1998. case OCRDMA_CQE_LOC_PROT_ERR:
  1999. ibwc_status = IB_WC_LOC_PROT_ERR;
  2000. break;
  2001. case OCRDMA_CQE_WR_FLUSH_ERR:
  2002. ibwc_status = IB_WC_WR_FLUSH_ERR;
  2003. break;
  2004. case OCRDMA_CQE_MW_BIND_ERR:
  2005. ibwc_status = IB_WC_MW_BIND_ERR;
  2006. break;
  2007. case OCRDMA_CQE_BAD_RESP_ERR:
  2008. ibwc_status = IB_WC_BAD_RESP_ERR;
  2009. break;
  2010. case OCRDMA_CQE_LOC_ACCESS_ERR:
  2011. ibwc_status = IB_WC_LOC_ACCESS_ERR;
  2012. break;
  2013. case OCRDMA_CQE_REM_INV_REQ_ERR:
  2014. ibwc_status = IB_WC_REM_INV_REQ_ERR;
  2015. break;
  2016. case OCRDMA_CQE_REM_ACCESS_ERR:
  2017. ibwc_status = IB_WC_REM_ACCESS_ERR;
  2018. break;
  2019. case OCRDMA_CQE_REM_OP_ERR:
  2020. ibwc_status = IB_WC_REM_OP_ERR;
  2021. break;
  2022. case OCRDMA_CQE_RETRY_EXC_ERR:
  2023. ibwc_status = IB_WC_RETRY_EXC_ERR;
  2024. break;
  2025. case OCRDMA_CQE_RNR_RETRY_EXC_ERR:
  2026. ibwc_status = IB_WC_RNR_RETRY_EXC_ERR;
  2027. break;
  2028. case OCRDMA_CQE_LOC_RDD_VIOL_ERR:
  2029. ibwc_status = IB_WC_LOC_RDD_VIOL_ERR;
  2030. break;
  2031. case OCRDMA_CQE_REM_INV_RD_REQ_ERR:
  2032. ibwc_status = IB_WC_REM_INV_RD_REQ_ERR;
  2033. break;
  2034. case OCRDMA_CQE_REM_ABORT_ERR:
  2035. ibwc_status = IB_WC_REM_ABORT_ERR;
  2036. break;
  2037. case OCRDMA_CQE_INV_EECN_ERR:
  2038. ibwc_status = IB_WC_INV_EECN_ERR;
  2039. break;
  2040. case OCRDMA_CQE_INV_EEC_STATE_ERR:
  2041. ibwc_status = IB_WC_INV_EEC_STATE_ERR;
  2042. break;
  2043. case OCRDMA_CQE_FATAL_ERR:
  2044. ibwc_status = IB_WC_FATAL_ERR;
  2045. break;
  2046. case OCRDMA_CQE_RESP_TIMEOUT_ERR:
  2047. ibwc_status = IB_WC_RESP_TIMEOUT_ERR;
  2048. break;
  2049. default:
  2050. ibwc_status = IB_WC_GENERAL_ERR;
  2051. break;
  2052. }
  2053. return ibwc_status;
  2054. }
  2055. static void ocrdma_update_wc(struct ocrdma_qp *qp, struct ib_wc *ibwc,
  2056. u32 wqe_idx)
  2057. {
  2058. struct ocrdma_hdr_wqe *hdr;
  2059. struct ocrdma_sge *rw;
  2060. int opcode;
  2061. hdr = ocrdma_hwq_head_from_idx(&qp->sq, wqe_idx);
  2062. ibwc->wr_id = qp->wqe_wr_id_tbl[wqe_idx].wrid;
  2063. /* Undo the hdr->cw swap */
  2064. opcode = le32_to_cpu(hdr->cw) & OCRDMA_WQE_OPCODE_MASK;
  2065. switch (opcode) {
  2066. case OCRDMA_WRITE:
  2067. ibwc->opcode = IB_WC_RDMA_WRITE;
  2068. break;
  2069. case OCRDMA_READ:
  2070. rw = (struct ocrdma_sge *)(hdr + 1);
  2071. ibwc->opcode = IB_WC_RDMA_READ;
  2072. ibwc->byte_len = rw->len;
  2073. break;
  2074. case OCRDMA_SEND:
  2075. ibwc->opcode = IB_WC_SEND;
  2076. break;
  2077. case OCRDMA_FR_MR:
  2078. ibwc->opcode = IB_WC_FAST_REG_MR;
  2079. break;
  2080. case OCRDMA_LKEY_INV:
  2081. ibwc->opcode = IB_WC_LOCAL_INV;
  2082. break;
  2083. default:
  2084. ibwc->status = IB_WC_GENERAL_ERR;
  2085. pr_err("%s() invalid opcode received = 0x%x\n",
  2086. __func__, hdr->cw & OCRDMA_WQE_OPCODE_MASK);
  2087. break;
  2088. }
  2089. }
  2090. static void ocrdma_set_cqe_status_flushed(struct ocrdma_qp *qp,
  2091. struct ocrdma_cqe *cqe)
  2092. {
  2093. if (is_cqe_for_sq(cqe)) {
  2094. cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
  2095. cqe->flags_status_srcqpn) &
  2096. ~OCRDMA_CQE_STATUS_MASK);
  2097. cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
  2098. cqe->flags_status_srcqpn) |
  2099. (OCRDMA_CQE_WR_FLUSH_ERR <<
  2100. OCRDMA_CQE_STATUS_SHIFT));
  2101. } else {
  2102. if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
  2103. cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
  2104. cqe->flags_status_srcqpn) &
  2105. ~OCRDMA_CQE_UD_STATUS_MASK);
  2106. cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
  2107. cqe->flags_status_srcqpn) |
  2108. (OCRDMA_CQE_WR_FLUSH_ERR <<
  2109. OCRDMA_CQE_UD_STATUS_SHIFT));
  2110. } else {
  2111. cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
  2112. cqe->flags_status_srcqpn) &
  2113. ~OCRDMA_CQE_STATUS_MASK);
  2114. cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
  2115. cqe->flags_status_srcqpn) |
  2116. (OCRDMA_CQE_WR_FLUSH_ERR <<
  2117. OCRDMA_CQE_STATUS_SHIFT));
  2118. }
  2119. }
  2120. }
  2121. static bool ocrdma_update_err_cqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
  2122. struct ocrdma_qp *qp, int status)
  2123. {
  2124. bool expand = false;
  2125. ibwc->byte_len = 0;
  2126. ibwc->qp = &qp->ibqp;
  2127. ibwc->status = ocrdma_to_ibwc_err(status);
  2128. ocrdma_flush_qp(qp);
  2129. ocrdma_qp_state_change(qp, IB_QPS_ERR, NULL);
  2130. /* if wqe/rqe pending for which cqe needs to be returned,
  2131. * trigger inflating it.
  2132. */
  2133. if (!is_hw_rq_empty(qp) || !is_hw_sq_empty(qp)) {
  2134. expand = true;
  2135. ocrdma_set_cqe_status_flushed(qp, cqe);
  2136. }
  2137. return expand;
  2138. }
  2139. static int ocrdma_update_err_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
  2140. struct ocrdma_qp *qp, int status)
  2141. {
  2142. ibwc->opcode = IB_WC_RECV;
  2143. ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
  2144. ocrdma_hwq_inc_tail(&qp->rq);
  2145. return ocrdma_update_err_cqe(ibwc, cqe, qp, status);
  2146. }
  2147. static int ocrdma_update_err_scqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
  2148. struct ocrdma_qp *qp, int status)
  2149. {
  2150. ocrdma_update_wc(qp, ibwc, qp->sq.tail);
  2151. ocrdma_hwq_inc_tail(&qp->sq);
  2152. return ocrdma_update_err_cqe(ibwc, cqe, qp, status);
  2153. }
  2154. static bool ocrdma_poll_err_scqe(struct ocrdma_qp *qp,
  2155. struct ocrdma_cqe *cqe, struct ib_wc *ibwc,
  2156. bool *polled, bool *stop)
  2157. {
  2158. bool expand;
  2159. int status = (le32_to_cpu(cqe->flags_status_srcqpn) &
  2160. OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
  2161. /* when hw sq is empty, but rq is not empty, so we continue
  2162. * to keep the cqe in order to get the cq event again.
  2163. */
  2164. if (is_hw_sq_empty(qp) && !is_hw_rq_empty(qp)) {
  2165. /* when cq for rq and sq is same, it is safe to return
  2166. * flush cqe for RQEs.
  2167. */
  2168. if (!qp->srq && (qp->sq_cq == qp->rq_cq)) {
  2169. *polled = true;
  2170. status = OCRDMA_CQE_WR_FLUSH_ERR;
  2171. expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status);
  2172. } else {
  2173. /* stop processing further cqe as this cqe is used for
  2174. * triggering cq event on buddy cq of RQ.
  2175. * When QP is destroyed, this cqe will be removed
  2176. * from the cq's hardware q.
  2177. */
  2178. *polled = false;
  2179. *stop = true;
  2180. expand = false;
  2181. }
  2182. } else {
  2183. *polled = true;
  2184. expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status);
  2185. }
  2186. return expand;
  2187. }
  2188. static bool ocrdma_poll_success_scqe(struct ocrdma_qp *qp,
  2189. struct ocrdma_cqe *cqe,
  2190. struct ib_wc *ibwc, bool *polled)
  2191. {
  2192. bool expand = false;
  2193. int tail = qp->sq.tail;
  2194. u32 wqe_idx;
  2195. if (!qp->wqe_wr_id_tbl[tail].signaled) {
  2196. *polled = false; /* WC cannot be consumed yet */
  2197. } else {
  2198. ibwc->status = IB_WC_SUCCESS;
  2199. ibwc->wc_flags = 0;
  2200. ibwc->qp = &qp->ibqp;
  2201. ocrdma_update_wc(qp, ibwc, tail);
  2202. *polled = true;
  2203. }
  2204. wqe_idx = (le32_to_cpu(cqe->wq.wqeidx) &
  2205. OCRDMA_CQE_WQEIDX_MASK) & qp->sq.max_wqe_idx;
  2206. if (tail != wqe_idx)
  2207. expand = true; /* Coalesced CQE can't be consumed yet */
  2208. ocrdma_hwq_inc_tail(&qp->sq);
  2209. return expand;
  2210. }
  2211. static bool ocrdma_poll_scqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
  2212. struct ib_wc *ibwc, bool *polled, bool *stop)
  2213. {
  2214. int status;
  2215. bool expand;
  2216. status = (le32_to_cpu(cqe->flags_status_srcqpn) &
  2217. OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
  2218. if (status == OCRDMA_CQE_SUCCESS)
  2219. expand = ocrdma_poll_success_scqe(qp, cqe, ibwc, polled);
  2220. else
  2221. expand = ocrdma_poll_err_scqe(qp, cqe, ibwc, polled, stop);
  2222. return expand;
  2223. }
  2224. static int ocrdma_update_ud_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe)
  2225. {
  2226. int status;
  2227. status = (le32_to_cpu(cqe->flags_status_srcqpn) &
  2228. OCRDMA_CQE_UD_STATUS_MASK) >> OCRDMA_CQE_UD_STATUS_SHIFT;
  2229. ibwc->src_qp = le32_to_cpu(cqe->flags_status_srcqpn) &
  2230. OCRDMA_CQE_SRCQP_MASK;
  2231. ibwc->pkey_index = le32_to_cpu(cqe->ud.rxlen_pkey) &
  2232. OCRDMA_CQE_PKEY_MASK;
  2233. ibwc->wc_flags = IB_WC_GRH;
  2234. ibwc->byte_len = (le32_to_cpu(cqe->ud.rxlen_pkey) >>
  2235. OCRDMA_CQE_UD_XFER_LEN_SHIFT);
  2236. return status;
  2237. }
  2238. static void ocrdma_update_free_srq_cqe(struct ib_wc *ibwc,
  2239. struct ocrdma_cqe *cqe,
  2240. struct ocrdma_qp *qp)
  2241. {
  2242. unsigned long flags;
  2243. struct ocrdma_srq *srq;
  2244. u32 wqe_idx;
  2245. srq = get_ocrdma_srq(qp->ibqp.srq);
  2246. wqe_idx = (le32_to_cpu(cqe->rq.buftag_qpn) >>
  2247. OCRDMA_CQE_BUFTAG_SHIFT) & srq->rq.max_wqe_idx;
  2248. ibwc->wr_id = srq->rqe_wr_id_tbl[wqe_idx];
  2249. spin_lock_irqsave(&srq->q_lock, flags);
  2250. ocrdma_srq_toggle_bit(srq, wqe_idx);
  2251. spin_unlock_irqrestore(&srq->q_lock, flags);
  2252. ocrdma_hwq_inc_tail(&srq->rq);
  2253. }
  2254. static bool ocrdma_poll_err_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
  2255. struct ib_wc *ibwc, bool *polled, bool *stop,
  2256. int status)
  2257. {
  2258. bool expand;
  2259. /* when hw_rq is empty, but wq is not empty, so continue
  2260. * to keep the cqe to get the cq event again.
  2261. */
  2262. if (is_hw_rq_empty(qp) && !is_hw_sq_empty(qp)) {
  2263. if (!qp->srq && (qp->sq_cq == qp->rq_cq)) {
  2264. *polled = true;
  2265. status = OCRDMA_CQE_WR_FLUSH_ERR;
  2266. expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status);
  2267. } else {
  2268. *polled = false;
  2269. *stop = true;
  2270. expand = false;
  2271. }
  2272. } else {
  2273. *polled = true;
  2274. expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status);
  2275. }
  2276. return expand;
  2277. }
  2278. static void ocrdma_poll_success_rcqe(struct ocrdma_qp *qp,
  2279. struct ocrdma_cqe *cqe, struct ib_wc *ibwc)
  2280. {
  2281. ibwc->opcode = IB_WC_RECV;
  2282. ibwc->qp = &qp->ibqp;
  2283. ibwc->status = IB_WC_SUCCESS;
  2284. if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI)
  2285. ocrdma_update_ud_rcqe(ibwc, cqe);
  2286. else
  2287. ibwc->byte_len = le32_to_cpu(cqe->rq.rxlen);
  2288. if (is_cqe_imm(cqe)) {
  2289. ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt));
  2290. ibwc->wc_flags |= IB_WC_WITH_IMM;
  2291. } else if (is_cqe_wr_imm(cqe)) {
  2292. ibwc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
  2293. ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt));
  2294. ibwc->wc_flags |= IB_WC_WITH_IMM;
  2295. } else if (is_cqe_invalidated(cqe)) {
  2296. ibwc->ex.invalidate_rkey = le32_to_cpu(cqe->rq.lkey_immdt);
  2297. ibwc->wc_flags |= IB_WC_WITH_INVALIDATE;
  2298. }
  2299. if (qp->ibqp.srq) {
  2300. ocrdma_update_free_srq_cqe(ibwc, cqe, qp);
  2301. } else {
  2302. ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
  2303. ocrdma_hwq_inc_tail(&qp->rq);
  2304. }
  2305. }
  2306. static bool ocrdma_poll_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
  2307. struct ib_wc *ibwc, bool *polled, bool *stop)
  2308. {
  2309. int status;
  2310. bool expand = false;
  2311. ibwc->wc_flags = 0;
  2312. if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
  2313. status = (le32_to_cpu(cqe->flags_status_srcqpn) &
  2314. OCRDMA_CQE_UD_STATUS_MASK) >>
  2315. OCRDMA_CQE_UD_STATUS_SHIFT;
  2316. } else {
  2317. status = (le32_to_cpu(cqe->flags_status_srcqpn) &
  2318. OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
  2319. }
  2320. if (status == OCRDMA_CQE_SUCCESS) {
  2321. *polled = true;
  2322. ocrdma_poll_success_rcqe(qp, cqe, ibwc);
  2323. } else {
  2324. expand = ocrdma_poll_err_rcqe(qp, cqe, ibwc, polled, stop,
  2325. status);
  2326. }
  2327. return expand;
  2328. }
  2329. static void ocrdma_change_cq_phase(struct ocrdma_cq *cq, struct ocrdma_cqe *cqe,
  2330. u16 cur_getp)
  2331. {
  2332. if (cq->phase_change) {
  2333. if (cur_getp == 0)
  2334. cq->phase = (~cq->phase & OCRDMA_CQE_VALID);
  2335. } else {
  2336. /* clear valid bit */
  2337. cqe->flags_status_srcqpn = 0;
  2338. }
  2339. }
  2340. static int ocrdma_poll_hwcq(struct ocrdma_cq *cq, int num_entries,
  2341. struct ib_wc *ibwc)
  2342. {
  2343. u16 qpn = 0;
  2344. int i = 0;
  2345. bool expand = false;
  2346. int polled_hw_cqes = 0;
  2347. struct ocrdma_qp *qp = NULL;
  2348. struct ocrdma_dev *dev = get_ocrdma_dev(cq->ibcq.device);
  2349. struct ocrdma_cqe *cqe;
  2350. u16 cur_getp; bool polled = false; bool stop = false;
  2351. cur_getp = cq->getp;
  2352. while (num_entries) {
  2353. cqe = cq->va + cur_getp;
  2354. /* check whether valid cqe or not */
  2355. if (!is_cqe_valid(cq, cqe))
  2356. break;
  2357. qpn = (le32_to_cpu(cqe->cmn.qpn) & OCRDMA_CQE_QPN_MASK);
  2358. /* ignore discarded cqe */
  2359. if (qpn == 0)
  2360. goto skip_cqe;
  2361. qp = dev->qp_tbl[qpn];
  2362. BUG_ON(qp == NULL);
  2363. if (is_cqe_for_sq(cqe)) {
  2364. expand = ocrdma_poll_scqe(qp, cqe, ibwc, &polled,
  2365. &stop);
  2366. } else {
  2367. expand = ocrdma_poll_rcqe(qp, cqe, ibwc, &polled,
  2368. &stop);
  2369. }
  2370. if (expand)
  2371. goto expand_cqe;
  2372. if (stop)
  2373. goto stop_cqe;
  2374. /* clear qpn to avoid duplicate processing by discard_cqe() */
  2375. cqe->cmn.qpn = 0;
  2376. skip_cqe:
  2377. polled_hw_cqes += 1;
  2378. cur_getp = (cur_getp + 1) % cq->max_hw_cqe;
  2379. ocrdma_change_cq_phase(cq, cqe, cur_getp);
  2380. expand_cqe:
  2381. if (polled) {
  2382. num_entries -= 1;
  2383. i += 1;
  2384. ibwc = ibwc + 1;
  2385. polled = false;
  2386. }
  2387. }
  2388. stop_cqe:
  2389. cq->getp = cur_getp;
  2390. if (polled_hw_cqes || expand || stop) {
  2391. ocrdma_ring_cq_db(dev, cq->id, cq->armed, cq->solicited,
  2392. polled_hw_cqes);
  2393. }
  2394. return i;
  2395. }
  2396. /* insert error cqe if the QP's SQ or RQ's CQ matches the CQ under poll. */
  2397. static int ocrdma_add_err_cqe(struct ocrdma_cq *cq, int num_entries,
  2398. struct ocrdma_qp *qp, struct ib_wc *ibwc)
  2399. {
  2400. int err_cqes = 0;
  2401. while (num_entries) {
  2402. if (is_hw_sq_empty(qp) && is_hw_rq_empty(qp))
  2403. break;
  2404. if (!is_hw_sq_empty(qp) && qp->sq_cq == cq) {
  2405. ocrdma_update_wc(qp, ibwc, qp->sq.tail);
  2406. ocrdma_hwq_inc_tail(&qp->sq);
  2407. } else if (!is_hw_rq_empty(qp) && qp->rq_cq == cq) {
  2408. ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
  2409. ocrdma_hwq_inc_tail(&qp->rq);
  2410. } else {
  2411. return err_cqes;
  2412. }
  2413. ibwc->byte_len = 0;
  2414. ibwc->status = IB_WC_WR_FLUSH_ERR;
  2415. ibwc = ibwc + 1;
  2416. err_cqes += 1;
  2417. num_entries -= 1;
  2418. }
  2419. return err_cqes;
  2420. }
  2421. int ocrdma_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
  2422. {
  2423. int cqes_to_poll = num_entries;
  2424. struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
  2425. struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
  2426. int num_os_cqe = 0, err_cqes = 0;
  2427. struct ocrdma_qp *qp;
  2428. unsigned long flags;
  2429. /* poll cqes from adapter CQ */
  2430. spin_lock_irqsave(&cq->cq_lock, flags);
  2431. num_os_cqe = ocrdma_poll_hwcq(cq, cqes_to_poll, wc);
  2432. spin_unlock_irqrestore(&cq->cq_lock, flags);
  2433. cqes_to_poll -= num_os_cqe;
  2434. if (cqes_to_poll) {
  2435. wc = wc + num_os_cqe;
  2436. /* adapter returns single error cqe when qp moves to
  2437. * error state. So insert error cqes with wc_status as
  2438. * FLUSHED for pending WQEs and RQEs of QP's SQ and RQ
  2439. * respectively which uses this CQ.
  2440. */
  2441. spin_lock_irqsave(&dev->flush_q_lock, flags);
  2442. list_for_each_entry(qp, &cq->sq_head, sq_entry) {
  2443. if (cqes_to_poll == 0)
  2444. break;
  2445. err_cqes = ocrdma_add_err_cqe(cq, cqes_to_poll, qp, wc);
  2446. cqes_to_poll -= err_cqes;
  2447. num_os_cqe += err_cqes;
  2448. wc = wc + err_cqes;
  2449. }
  2450. spin_unlock_irqrestore(&dev->flush_q_lock, flags);
  2451. }
  2452. return num_os_cqe;
  2453. }
  2454. int ocrdma_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags cq_flags)
  2455. {
  2456. struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
  2457. struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
  2458. u16 cq_id;
  2459. u16 cur_getp;
  2460. struct ocrdma_cqe *cqe;
  2461. unsigned long flags;
  2462. cq_id = cq->id;
  2463. spin_lock_irqsave(&cq->cq_lock, flags);
  2464. if (cq_flags & IB_CQ_NEXT_COMP || cq_flags & IB_CQ_SOLICITED)
  2465. cq->armed = true;
  2466. if (cq_flags & IB_CQ_SOLICITED)
  2467. cq->solicited = true;
  2468. cur_getp = cq->getp;
  2469. cqe = cq->va + cur_getp;
  2470. /* check whether any valid cqe exist or not, if not then safe to
  2471. * arm. If cqe is not yet consumed, then let it get consumed and then
  2472. * we arm it to avoid false interrupts.
  2473. */
  2474. if (!is_cqe_valid(cq, cqe) || cq->arm_needed) {
  2475. cq->arm_needed = false;
  2476. ocrdma_ring_cq_db(dev, cq_id, cq->armed, cq->solicited, 0);
  2477. }
  2478. spin_unlock_irqrestore(&cq->cq_lock, flags);
  2479. return 0;
  2480. }
  2481. struct ib_mr *ocrdma_alloc_frmr(struct ib_pd *ibpd, int max_page_list_len)
  2482. {
  2483. int status;
  2484. struct ocrdma_mr *mr;
  2485. struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
  2486. struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
  2487. if (max_page_list_len > dev->attr.max_pages_per_frmr)
  2488. return ERR_PTR(-EINVAL);
  2489. mr = kzalloc(sizeof(*mr), GFP_KERNEL);
  2490. if (!mr)
  2491. return ERR_PTR(-ENOMEM);
  2492. status = ocrdma_get_pbl_info(dev, mr, max_page_list_len);
  2493. if (status)
  2494. goto pbl_err;
  2495. mr->hwmr.fr_mr = 1;
  2496. mr->hwmr.remote_rd = 0;
  2497. mr->hwmr.remote_wr = 0;
  2498. mr->hwmr.local_rd = 0;
  2499. mr->hwmr.local_wr = 0;
  2500. mr->hwmr.mw_bind = 0;
  2501. status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
  2502. if (status)
  2503. goto pbl_err;
  2504. status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, 0);
  2505. if (status)
  2506. goto mbx_err;
  2507. mr->ibmr.rkey = mr->hwmr.lkey;
  2508. mr->ibmr.lkey = mr->hwmr.lkey;
  2509. dev->stag_arr[(mr->hwmr.lkey >> 8) & (OCRDMA_MAX_STAG - 1)] = mr;
  2510. return &mr->ibmr;
  2511. mbx_err:
  2512. ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
  2513. pbl_err:
  2514. kfree(mr);
  2515. return ERR_PTR(-ENOMEM);
  2516. }
  2517. struct ib_fast_reg_page_list *ocrdma_alloc_frmr_page_list(struct ib_device
  2518. *ibdev,
  2519. int page_list_len)
  2520. {
  2521. struct ib_fast_reg_page_list *frmr_list;
  2522. int size;
  2523. size = sizeof(*frmr_list) + (page_list_len * sizeof(u64));
  2524. frmr_list = kzalloc(size, GFP_KERNEL);
  2525. if (!frmr_list)
  2526. return ERR_PTR(-ENOMEM);
  2527. frmr_list->page_list = (u64 *)(frmr_list + 1);
  2528. return frmr_list;
  2529. }
  2530. void ocrdma_free_frmr_page_list(struct ib_fast_reg_page_list *page_list)
  2531. {
  2532. kfree(page_list);
  2533. }
  2534. #define MAX_KERNEL_PBE_SIZE 65536
  2535. static inline int count_kernel_pbes(struct ib_phys_buf *buf_list,
  2536. int buf_cnt, u32 *pbe_size)
  2537. {
  2538. u64 total_size = 0;
  2539. u64 buf_size = 0;
  2540. int i;
  2541. *pbe_size = roundup(buf_list[0].size, PAGE_SIZE);
  2542. *pbe_size = roundup_pow_of_two(*pbe_size);
  2543. /* find the smallest PBE size that we can have */
  2544. for (i = 0; i < buf_cnt; i++) {
  2545. /* first addr may not be page aligned, so ignore checking */
  2546. if ((i != 0) && ((buf_list[i].addr & ~PAGE_MASK) ||
  2547. (buf_list[i].size & ~PAGE_MASK))) {
  2548. return 0;
  2549. }
  2550. /* if configured PBE size is greater then the chosen one,
  2551. * reduce the PBE size.
  2552. */
  2553. buf_size = roundup(buf_list[i].size, PAGE_SIZE);
  2554. /* pbe_size has to be even multiple of 4K 1,2,4,8...*/
  2555. buf_size = roundup_pow_of_two(buf_size);
  2556. if (*pbe_size > buf_size)
  2557. *pbe_size = buf_size;
  2558. total_size += buf_size;
  2559. }
  2560. *pbe_size = *pbe_size > MAX_KERNEL_PBE_SIZE ?
  2561. (MAX_KERNEL_PBE_SIZE) : (*pbe_size);
  2562. /* num_pbes = total_size / (*pbe_size); this is implemented below. */
  2563. return total_size >> ilog2(*pbe_size);
  2564. }
  2565. static void build_kernel_pbes(struct ib_phys_buf *buf_list, int ib_buf_cnt,
  2566. u32 pbe_size, struct ocrdma_pbl *pbl_tbl,
  2567. struct ocrdma_hw_mr *hwmr)
  2568. {
  2569. int i;
  2570. int idx;
  2571. int pbes_per_buf = 0;
  2572. u64 buf_addr = 0;
  2573. int num_pbes;
  2574. struct ocrdma_pbe *pbe;
  2575. int total_num_pbes = 0;
  2576. if (!hwmr->num_pbes)
  2577. return;
  2578. pbe = (struct ocrdma_pbe *)pbl_tbl->va;
  2579. num_pbes = 0;
  2580. /* go through the OS phy regions & fill hw pbe entries into pbls. */
  2581. for (i = 0; i < ib_buf_cnt; i++) {
  2582. buf_addr = buf_list[i].addr;
  2583. pbes_per_buf =
  2584. roundup_pow_of_two(roundup(buf_list[i].size, PAGE_SIZE)) /
  2585. pbe_size;
  2586. hwmr->len += buf_list[i].size;
  2587. /* number of pbes can be more for one OS buf, when
  2588. * buffers are of different sizes.
  2589. * split the ib_buf to one or more pbes.
  2590. */
  2591. for (idx = 0; idx < pbes_per_buf; idx++) {
  2592. /* we program always page aligned addresses,
  2593. * first unaligned address is taken care by fbo.
  2594. */
  2595. if (i == 0) {
  2596. /* for non zero fbo, assign the
  2597. * start of the page.
  2598. */
  2599. pbe->pa_lo =
  2600. cpu_to_le32((u32) (buf_addr & PAGE_MASK));
  2601. pbe->pa_hi =
  2602. cpu_to_le32((u32) upper_32_bits(buf_addr));
  2603. } else {
  2604. pbe->pa_lo =
  2605. cpu_to_le32((u32) (buf_addr & 0xffffffff));
  2606. pbe->pa_hi =
  2607. cpu_to_le32((u32) upper_32_bits(buf_addr));
  2608. }
  2609. buf_addr += pbe_size;
  2610. num_pbes += 1;
  2611. total_num_pbes += 1;
  2612. pbe++;
  2613. if (total_num_pbes == hwmr->num_pbes)
  2614. goto mr_tbl_done;
  2615. /* if the pbl is full storing the pbes,
  2616. * move to next pbl.
  2617. */
  2618. if (num_pbes == (hwmr->pbl_size/sizeof(u64))) {
  2619. pbl_tbl++;
  2620. pbe = (struct ocrdma_pbe *)pbl_tbl->va;
  2621. num_pbes = 0;
  2622. }
  2623. }
  2624. }
  2625. mr_tbl_done:
  2626. return;
  2627. }
  2628. struct ib_mr *ocrdma_reg_kernel_mr(struct ib_pd *ibpd,
  2629. struct ib_phys_buf *buf_list,
  2630. int buf_cnt, int acc, u64 *iova_start)
  2631. {
  2632. int status = -ENOMEM;
  2633. struct ocrdma_mr *mr;
  2634. struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
  2635. struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
  2636. u32 num_pbes;
  2637. u32 pbe_size = 0;
  2638. if ((acc & IB_ACCESS_REMOTE_WRITE) && !(acc & IB_ACCESS_LOCAL_WRITE))
  2639. return ERR_PTR(-EINVAL);
  2640. mr = kzalloc(sizeof(*mr), GFP_KERNEL);
  2641. if (!mr)
  2642. return ERR_PTR(status);
  2643. num_pbes = count_kernel_pbes(buf_list, buf_cnt, &pbe_size);
  2644. if (num_pbes == 0) {
  2645. status = -EINVAL;
  2646. goto pbl_err;
  2647. }
  2648. status = ocrdma_get_pbl_info(dev, mr, num_pbes);
  2649. if (status)
  2650. goto pbl_err;
  2651. mr->hwmr.pbe_size = pbe_size;
  2652. mr->hwmr.fbo = *iova_start - (buf_list[0].addr & PAGE_MASK);
  2653. mr->hwmr.va = *iova_start;
  2654. mr->hwmr.local_rd = 1;
  2655. mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
  2656. mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
  2657. mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
  2658. mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
  2659. mr->hwmr.mw_bind = (acc & IB_ACCESS_MW_BIND) ? 1 : 0;
  2660. status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
  2661. if (status)
  2662. goto pbl_err;
  2663. build_kernel_pbes(buf_list, buf_cnt, pbe_size, mr->hwmr.pbl_table,
  2664. &mr->hwmr);
  2665. status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, acc);
  2666. if (status)
  2667. goto mbx_err;
  2668. mr->ibmr.lkey = mr->hwmr.lkey;
  2669. if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
  2670. mr->ibmr.rkey = mr->hwmr.lkey;
  2671. return &mr->ibmr;
  2672. mbx_err:
  2673. ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
  2674. pbl_err:
  2675. kfree(mr);
  2676. return ERR_PTR(status);
  2677. }