ocrdma_hw.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252
  1. /* This file is part of the Emulex RoCE Device Driver for
  2. * RoCE (RDMA over Converged Ethernet) adapters.
  3. * Copyright (C) 2012-2015 Emulex. All rights reserved.
  4. * EMULEX and SLI are trademarks of Emulex.
  5. * www.emulex.com
  6. *
  7. * This software is available to you under a choice of one of two licenses.
  8. * You may choose to be licensed under the terms of the GNU General Public
  9. * License (GPL) Version 2, available from the file COPYING in the main
  10. * directory of this source tree, or the BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in
  21. * the documentation and/or other materials provided with the distribution.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  30. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  31. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  32. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  33. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * Contact Information:
  36. * linux-drivers@emulex.com
  37. *
  38. * Emulex
  39. * 3333 Susan Street
  40. * Costa Mesa, CA 92626
  41. */
  42. #include <linux/sched.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/log2.h>
  45. #include <linux/dma-mapping.h>
  46. #include <linux/if_ether.h>
  47. #include <rdma/ib_verbs.h>
  48. #include <rdma/ib_user_verbs.h>
  49. #include <rdma/ib_cache.h>
  50. #include "ocrdma.h"
  51. #include "ocrdma_hw.h"
  52. #include "ocrdma_verbs.h"
  53. #include "ocrdma_ah.h"
  54. enum mbx_status {
  55. OCRDMA_MBX_STATUS_FAILED = 1,
  56. OCRDMA_MBX_STATUS_ILLEGAL_FIELD = 3,
  57. OCRDMA_MBX_STATUS_OOR = 100,
  58. OCRDMA_MBX_STATUS_INVALID_PD = 101,
  59. OCRDMA_MBX_STATUS_PD_INUSE = 102,
  60. OCRDMA_MBX_STATUS_INVALID_CQ = 103,
  61. OCRDMA_MBX_STATUS_INVALID_QP = 104,
  62. OCRDMA_MBX_STATUS_INVALID_LKEY = 105,
  63. OCRDMA_MBX_STATUS_ORD_EXCEEDS = 106,
  64. OCRDMA_MBX_STATUS_IRD_EXCEEDS = 107,
  65. OCRDMA_MBX_STATUS_SENDQ_WQE_EXCEEDS = 108,
  66. OCRDMA_MBX_STATUS_RECVQ_RQE_EXCEEDS = 109,
  67. OCRDMA_MBX_STATUS_SGE_SEND_EXCEEDS = 110,
  68. OCRDMA_MBX_STATUS_SGE_WRITE_EXCEEDS = 111,
  69. OCRDMA_MBX_STATUS_SGE_RECV_EXCEEDS = 112,
  70. OCRDMA_MBX_STATUS_INVALID_STATE_CHANGE = 113,
  71. OCRDMA_MBX_STATUS_MW_BOUND = 114,
  72. OCRDMA_MBX_STATUS_INVALID_VA = 115,
  73. OCRDMA_MBX_STATUS_INVALID_LENGTH = 116,
  74. OCRDMA_MBX_STATUS_INVALID_FBO = 117,
  75. OCRDMA_MBX_STATUS_INVALID_ACC_RIGHTS = 118,
  76. OCRDMA_MBX_STATUS_INVALID_PBE_SIZE = 119,
  77. OCRDMA_MBX_STATUS_INVALID_PBL_ENTRY = 120,
  78. OCRDMA_MBX_STATUS_INVALID_PBL_SHIFT = 121,
  79. OCRDMA_MBX_STATUS_INVALID_SRQ_ID = 129,
  80. OCRDMA_MBX_STATUS_SRQ_ERROR = 133,
  81. OCRDMA_MBX_STATUS_RQE_EXCEEDS = 134,
  82. OCRDMA_MBX_STATUS_MTU_EXCEEDS = 135,
  83. OCRDMA_MBX_STATUS_MAX_QP_EXCEEDS = 136,
  84. OCRDMA_MBX_STATUS_SRQ_LIMIT_EXCEEDS = 137,
  85. OCRDMA_MBX_STATUS_SRQ_SIZE_UNDERUNS = 138,
  86. OCRDMA_MBX_STATUS_QP_BOUND = 130,
  87. OCRDMA_MBX_STATUS_INVALID_CHANGE = 139,
  88. OCRDMA_MBX_STATUS_ATOMIC_OPS_UNSUP = 140,
  89. OCRDMA_MBX_STATUS_INVALID_RNR_NAK_TIMER = 141,
  90. OCRDMA_MBX_STATUS_MW_STILL_BOUND = 142,
  91. OCRDMA_MBX_STATUS_PKEY_INDEX_INVALID = 143,
  92. OCRDMA_MBX_STATUS_PKEY_INDEX_EXCEEDS = 144
  93. };
  94. enum additional_status {
  95. OCRDMA_MBX_ADDI_STATUS_INSUFFICIENT_RESOURCES = 22
  96. };
  97. enum cqe_status {
  98. OCRDMA_MBX_CQE_STATUS_INSUFFICIENT_PRIVILEDGES = 1,
  99. OCRDMA_MBX_CQE_STATUS_INVALID_PARAMETER = 2,
  100. OCRDMA_MBX_CQE_STATUS_INSUFFICIENT_RESOURCES = 3,
  101. OCRDMA_MBX_CQE_STATUS_QUEUE_FLUSHING = 4,
  102. OCRDMA_MBX_CQE_STATUS_DMA_FAILED = 5
  103. };
  104. static inline void *ocrdma_get_eqe(struct ocrdma_eq *eq)
  105. {
  106. return eq->q.va + (eq->q.tail * sizeof(struct ocrdma_eqe));
  107. }
  108. static inline void ocrdma_eq_inc_tail(struct ocrdma_eq *eq)
  109. {
  110. eq->q.tail = (eq->q.tail + 1) & (OCRDMA_EQ_LEN - 1);
  111. }
  112. static inline void *ocrdma_get_mcqe(struct ocrdma_dev *dev)
  113. {
  114. struct ocrdma_mcqe *cqe = (struct ocrdma_mcqe *)
  115. (dev->mq.cq.va + (dev->mq.cq.tail * sizeof(struct ocrdma_mcqe)));
  116. if (!(le32_to_cpu(cqe->valid_ae_cmpl_cons) & OCRDMA_MCQE_VALID_MASK))
  117. return NULL;
  118. return cqe;
  119. }
  120. static inline void ocrdma_mcq_inc_tail(struct ocrdma_dev *dev)
  121. {
  122. dev->mq.cq.tail = (dev->mq.cq.tail + 1) & (OCRDMA_MQ_CQ_LEN - 1);
  123. }
  124. static inline struct ocrdma_mqe *ocrdma_get_mqe(struct ocrdma_dev *dev)
  125. {
  126. return dev->mq.sq.va + (dev->mq.sq.head * sizeof(struct ocrdma_mqe));
  127. }
  128. static inline void ocrdma_mq_inc_head(struct ocrdma_dev *dev)
  129. {
  130. dev->mq.sq.head = (dev->mq.sq.head + 1) & (OCRDMA_MQ_LEN - 1);
  131. }
  132. static inline void *ocrdma_get_mqe_rsp(struct ocrdma_dev *dev)
  133. {
  134. return dev->mq.sq.va + (dev->mqe_ctx.tag * sizeof(struct ocrdma_mqe));
  135. }
  136. enum ib_qp_state get_ibqp_state(enum ocrdma_qp_state qps)
  137. {
  138. switch (qps) {
  139. case OCRDMA_QPS_RST:
  140. return IB_QPS_RESET;
  141. case OCRDMA_QPS_INIT:
  142. return IB_QPS_INIT;
  143. case OCRDMA_QPS_RTR:
  144. return IB_QPS_RTR;
  145. case OCRDMA_QPS_RTS:
  146. return IB_QPS_RTS;
  147. case OCRDMA_QPS_SQD:
  148. case OCRDMA_QPS_SQ_DRAINING:
  149. return IB_QPS_SQD;
  150. case OCRDMA_QPS_SQE:
  151. return IB_QPS_SQE;
  152. case OCRDMA_QPS_ERR:
  153. return IB_QPS_ERR;
  154. }
  155. return IB_QPS_ERR;
  156. }
  157. static enum ocrdma_qp_state get_ocrdma_qp_state(enum ib_qp_state qps)
  158. {
  159. switch (qps) {
  160. case IB_QPS_RESET:
  161. return OCRDMA_QPS_RST;
  162. case IB_QPS_INIT:
  163. return OCRDMA_QPS_INIT;
  164. case IB_QPS_RTR:
  165. return OCRDMA_QPS_RTR;
  166. case IB_QPS_RTS:
  167. return OCRDMA_QPS_RTS;
  168. case IB_QPS_SQD:
  169. return OCRDMA_QPS_SQD;
  170. case IB_QPS_SQE:
  171. return OCRDMA_QPS_SQE;
  172. case IB_QPS_ERR:
  173. return OCRDMA_QPS_ERR;
  174. }
  175. return OCRDMA_QPS_ERR;
  176. }
  177. static int ocrdma_get_mbx_errno(u32 status)
  178. {
  179. int err_num;
  180. u8 mbox_status = (status & OCRDMA_MBX_RSP_STATUS_MASK) >>
  181. OCRDMA_MBX_RSP_STATUS_SHIFT;
  182. u8 add_status = (status & OCRDMA_MBX_RSP_ASTATUS_MASK) >>
  183. OCRDMA_MBX_RSP_ASTATUS_SHIFT;
  184. switch (mbox_status) {
  185. case OCRDMA_MBX_STATUS_OOR:
  186. case OCRDMA_MBX_STATUS_MAX_QP_EXCEEDS:
  187. err_num = -EAGAIN;
  188. break;
  189. case OCRDMA_MBX_STATUS_INVALID_PD:
  190. case OCRDMA_MBX_STATUS_INVALID_CQ:
  191. case OCRDMA_MBX_STATUS_INVALID_SRQ_ID:
  192. case OCRDMA_MBX_STATUS_INVALID_QP:
  193. case OCRDMA_MBX_STATUS_INVALID_CHANGE:
  194. case OCRDMA_MBX_STATUS_MTU_EXCEEDS:
  195. case OCRDMA_MBX_STATUS_INVALID_RNR_NAK_TIMER:
  196. case OCRDMA_MBX_STATUS_PKEY_INDEX_INVALID:
  197. case OCRDMA_MBX_STATUS_PKEY_INDEX_EXCEEDS:
  198. case OCRDMA_MBX_STATUS_ILLEGAL_FIELD:
  199. case OCRDMA_MBX_STATUS_INVALID_PBL_ENTRY:
  200. case OCRDMA_MBX_STATUS_INVALID_LKEY:
  201. case OCRDMA_MBX_STATUS_INVALID_VA:
  202. case OCRDMA_MBX_STATUS_INVALID_LENGTH:
  203. case OCRDMA_MBX_STATUS_INVALID_FBO:
  204. case OCRDMA_MBX_STATUS_INVALID_ACC_RIGHTS:
  205. case OCRDMA_MBX_STATUS_INVALID_PBE_SIZE:
  206. case OCRDMA_MBX_STATUS_ATOMIC_OPS_UNSUP:
  207. case OCRDMA_MBX_STATUS_SRQ_ERROR:
  208. case OCRDMA_MBX_STATUS_SRQ_SIZE_UNDERUNS:
  209. err_num = -EINVAL;
  210. break;
  211. case OCRDMA_MBX_STATUS_PD_INUSE:
  212. case OCRDMA_MBX_STATUS_QP_BOUND:
  213. case OCRDMA_MBX_STATUS_MW_STILL_BOUND:
  214. case OCRDMA_MBX_STATUS_MW_BOUND:
  215. err_num = -EBUSY;
  216. break;
  217. case OCRDMA_MBX_STATUS_RECVQ_RQE_EXCEEDS:
  218. case OCRDMA_MBX_STATUS_SGE_RECV_EXCEEDS:
  219. case OCRDMA_MBX_STATUS_RQE_EXCEEDS:
  220. case OCRDMA_MBX_STATUS_SRQ_LIMIT_EXCEEDS:
  221. case OCRDMA_MBX_STATUS_ORD_EXCEEDS:
  222. case OCRDMA_MBX_STATUS_IRD_EXCEEDS:
  223. case OCRDMA_MBX_STATUS_SENDQ_WQE_EXCEEDS:
  224. case OCRDMA_MBX_STATUS_SGE_SEND_EXCEEDS:
  225. case OCRDMA_MBX_STATUS_SGE_WRITE_EXCEEDS:
  226. err_num = -ENOBUFS;
  227. break;
  228. case OCRDMA_MBX_STATUS_FAILED:
  229. switch (add_status) {
  230. case OCRDMA_MBX_ADDI_STATUS_INSUFFICIENT_RESOURCES:
  231. err_num = -EAGAIN;
  232. break;
  233. default:
  234. err_num = -EFAULT;
  235. }
  236. break;
  237. default:
  238. err_num = -EFAULT;
  239. }
  240. return err_num;
  241. }
  242. char *port_speed_string(struct ocrdma_dev *dev)
  243. {
  244. char *str = "";
  245. u16 speeds_supported;
  246. speeds_supported = dev->phy.fixed_speeds_supported |
  247. dev->phy.auto_speeds_supported;
  248. if (speeds_supported & OCRDMA_PHY_SPEED_40GBPS)
  249. str = "40Gbps ";
  250. else if (speeds_supported & OCRDMA_PHY_SPEED_10GBPS)
  251. str = "10Gbps ";
  252. else if (speeds_supported & OCRDMA_PHY_SPEED_1GBPS)
  253. str = "1Gbps ";
  254. return str;
  255. }
  256. static int ocrdma_get_mbx_cqe_errno(u16 cqe_status)
  257. {
  258. int err_num = -EINVAL;
  259. switch (cqe_status) {
  260. case OCRDMA_MBX_CQE_STATUS_INSUFFICIENT_PRIVILEDGES:
  261. err_num = -EPERM;
  262. break;
  263. case OCRDMA_MBX_CQE_STATUS_INVALID_PARAMETER:
  264. err_num = -EINVAL;
  265. break;
  266. case OCRDMA_MBX_CQE_STATUS_INSUFFICIENT_RESOURCES:
  267. case OCRDMA_MBX_CQE_STATUS_QUEUE_FLUSHING:
  268. err_num = -EINVAL;
  269. break;
  270. case OCRDMA_MBX_CQE_STATUS_DMA_FAILED:
  271. default:
  272. err_num = -EINVAL;
  273. break;
  274. }
  275. return err_num;
  276. }
  277. void ocrdma_ring_cq_db(struct ocrdma_dev *dev, u16 cq_id, bool armed,
  278. bool solicited, u16 cqe_popped)
  279. {
  280. u32 val = cq_id & OCRDMA_DB_CQ_RING_ID_MASK;
  281. val |= ((cq_id & OCRDMA_DB_CQ_RING_ID_EXT_MASK) <<
  282. OCRDMA_DB_CQ_RING_ID_EXT_MASK_SHIFT);
  283. if (armed)
  284. val |= (1 << OCRDMA_DB_CQ_REARM_SHIFT);
  285. if (solicited)
  286. val |= (1 << OCRDMA_DB_CQ_SOLICIT_SHIFT);
  287. val |= (cqe_popped << OCRDMA_DB_CQ_NUM_POPPED_SHIFT);
  288. iowrite32(val, dev->nic_info.db + OCRDMA_DB_CQ_OFFSET);
  289. }
  290. static void ocrdma_ring_mq_db(struct ocrdma_dev *dev)
  291. {
  292. u32 val = 0;
  293. val |= dev->mq.sq.id & OCRDMA_MQ_ID_MASK;
  294. val |= 1 << OCRDMA_MQ_NUM_MQE_SHIFT;
  295. iowrite32(val, dev->nic_info.db + OCRDMA_DB_MQ_OFFSET);
  296. }
  297. static void ocrdma_ring_eq_db(struct ocrdma_dev *dev, u16 eq_id,
  298. bool arm, bool clear_int, u16 num_eqe)
  299. {
  300. u32 val = 0;
  301. val |= eq_id & OCRDMA_EQ_ID_MASK;
  302. val |= ((eq_id & OCRDMA_EQ_ID_EXT_MASK) << OCRDMA_EQ_ID_EXT_MASK_SHIFT);
  303. if (arm)
  304. val |= (1 << OCRDMA_REARM_SHIFT);
  305. if (clear_int)
  306. val |= (1 << OCRDMA_EQ_CLR_SHIFT);
  307. val |= (1 << OCRDMA_EQ_TYPE_SHIFT);
  308. val |= (num_eqe << OCRDMA_NUM_EQE_SHIFT);
  309. iowrite32(val, dev->nic_info.db + OCRDMA_DB_EQ_OFFSET);
  310. }
  311. static void ocrdma_init_mch(struct ocrdma_mbx_hdr *cmd_hdr,
  312. u8 opcode, u8 subsys, u32 cmd_len)
  313. {
  314. cmd_hdr->subsys_op = (opcode | (subsys << OCRDMA_MCH_SUBSYS_SHIFT));
  315. cmd_hdr->timeout = 20; /* seconds */
  316. cmd_hdr->cmd_len = cmd_len - sizeof(struct ocrdma_mbx_hdr);
  317. }
  318. static void *ocrdma_init_emb_mqe(u8 opcode, u32 cmd_len)
  319. {
  320. struct ocrdma_mqe *mqe;
  321. mqe = kzalloc(sizeof(struct ocrdma_mqe), GFP_KERNEL);
  322. if (!mqe)
  323. return NULL;
  324. mqe->hdr.spcl_sge_cnt_emb |=
  325. (OCRDMA_MQE_EMBEDDED << OCRDMA_MQE_HDR_EMB_SHIFT) &
  326. OCRDMA_MQE_HDR_EMB_MASK;
  327. mqe->hdr.pyld_len = cmd_len - sizeof(struct ocrdma_mqe_hdr);
  328. ocrdma_init_mch(&mqe->u.emb_req.mch, opcode, OCRDMA_SUBSYS_ROCE,
  329. mqe->hdr.pyld_len);
  330. return mqe;
  331. }
  332. static void ocrdma_free_q(struct ocrdma_dev *dev, struct ocrdma_queue_info *q)
  333. {
  334. dma_free_coherent(&dev->nic_info.pdev->dev, q->size, q->va, q->dma);
  335. }
  336. static int ocrdma_alloc_q(struct ocrdma_dev *dev,
  337. struct ocrdma_queue_info *q, u16 len, u16 entry_size)
  338. {
  339. memset(q, 0, sizeof(*q));
  340. q->len = len;
  341. q->entry_size = entry_size;
  342. q->size = len * entry_size;
  343. q->va = dma_zalloc_coherent(&dev->nic_info.pdev->dev, q->size,
  344. &q->dma, GFP_KERNEL);
  345. if (!q->va)
  346. return -ENOMEM;
  347. return 0;
  348. }
  349. static void ocrdma_build_q_pages(struct ocrdma_pa *q_pa, int cnt,
  350. dma_addr_t host_pa, int hw_page_size)
  351. {
  352. int i;
  353. for (i = 0; i < cnt; i++) {
  354. q_pa[i].lo = (u32) (host_pa & 0xffffffff);
  355. q_pa[i].hi = (u32) upper_32_bits(host_pa);
  356. host_pa += hw_page_size;
  357. }
  358. }
  359. static int ocrdma_mbx_delete_q(struct ocrdma_dev *dev,
  360. struct ocrdma_queue_info *q, int queue_type)
  361. {
  362. u8 opcode = 0;
  363. int status;
  364. struct ocrdma_delete_q_req *cmd = dev->mbx_cmd;
  365. switch (queue_type) {
  366. case QTYPE_MCCQ:
  367. opcode = OCRDMA_CMD_DELETE_MQ;
  368. break;
  369. case QTYPE_CQ:
  370. opcode = OCRDMA_CMD_DELETE_CQ;
  371. break;
  372. case QTYPE_EQ:
  373. opcode = OCRDMA_CMD_DELETE_EQ;
  374. break;
  375. default:
  376. BUG();
  377. }
  378. memset(cmd, 0, sizeof(*cmd));
  379. ocrdma_init_mch(&cmd->req, opcode, OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
  380. cmd->id = q->id;
  381. status = be_roce_mcc_cmd(dev->nic_info.netdev,
  382. cmd, sizeof(*cmd), NULL, NULL);
  383. if (!status)
  384. q->created = false;
  385. return status;
  386. }
  387. static int ocrdma_mbx_create_eq(struct ocrdma_dev *dev, struct ocrdma_eq *eq)
  388. {
  389. int status;
  390. struct ocrdma_create_eq_req *cmd = dev->mbx_cmd;
  391. struct ocrdma_create_eq_rsp *rsp = dev->mbx_cmd;
  392. memset(cmd, 0, sizeof(*cmd));
  393. ocrdma_init_mch(&cmd->req, OCRDMA_CMD_CREATE_EQ, OCRDMA_SUBSYS_COMMON,
  394. sizeof(*cmd));
  395. cmd->req.rsvd_version = 2;
  396. cmd->num_pages = 4;
  397. cmd->valid = OCRDMA_CREATE_EQ_VALID;
  398. cmd->cnt = 4 << OCRDMA_CREATE_EQ_CNT_SHIFT;
  399. ocrdma_build_q_pages(&cmd->pa[0], cmd->num_pages, eq->q.dma,
  400. PAGE_SIZE_4K);
  401. status = be_roce_mcc_cmd(dev->nic_info.netdev, cmd, sizeof(*cmd), NULL,
  402. NULL);
  403. if (!status) {
  404. eq->q.id = rsp->vector_eqid & 0xffff;
  405. eq->vector = (rsp->vector_eqid >> 16) & 0xffff;
  406. eq->q.created = true;
  407. }
  408. return status;
  409. }
  410. static int ocrdma_create_eq(struct ocrdma_dev *dev,
  411. struct ocrdma_eq *eq, u16 q_len)
  412. {
  413. int status;
  414. status = ocrdma_alloc_q(dev, &eq->q, OCRDMA_EQ_LEN,
  415. sizeof(struct ocrdma_eqe));
  416. if (status)
  417. return status;
  418. status = ocrdma_mbx_create_eq(dev, eq);
  419. if (status)
  420. goto mbx_err;
  421. eq->dev = dev;
  422. ocrdma_ring_eq_db(dev, eq->q.id, true, true, 0);
  423. return 0;
  424. mbx_err:
  425. ocrdma_free_q(dev, &eq->q);
  426. return status;
  427. }
  428. int ocrdma_get_irq(struct ocrdma_dev *dev, struct ocrdma_eq *eq)
  429. {
  430. int irq;
  431. if (dev->nic_info.intr_mode == BE_INTERRUPT_MODE_INTX)
  432. irq = dev->nic_info.pdev->irq;
  433. else
  434. irq = dev->nic_info.msix.vector_list[eq->vector];
  435. return irq;
  436. }
  437. static void _ocrdma_destroy_eq(struct ocrdma_dev *dev, struct ocrdma_eq *eq)
  438. {
  439. if (eq->q.created) {
  440. ocrdma_mbx_delete_q(dev, &eq->q, QTYPE_EQ);
  441. ocrdma_free_q(dev, &eq->q);
  442. }
  443. }
  444. static void ocrdma_destroy_eq(struct ocrdma_dev *dev, struct ocrdma_eq *eq)
  445. {
  446. int irq;
  447. /* disarm EQ so that interrupts are not generated
  448. * during freeing and EQ delete is in progress.
  449. */
  450. ocrdma_ring_eq_db(dev, eq->q.id, false, false, 0);
  451. irq = ocrdma_get_irq(dev, eq);
  452. free_irq(irq, eq);
  453. _ocrdma_destroy_eq(dev, eq);
  454. }
  455. static void ocrdma_destroy_eqs(struct ocrdma_dev *dev)
  456. {
  457. int i;
  458. for (i = 0; i < dev->eq_cnt; i++)
  459. ocrdma_destroy_eq(dev, &dev->eq_tbl[i]);
  460. }
  461. static int ocrdma_mbx_mq_cq_create(struct ocrdma_dev *dev,
  462. struct ocrdma_queue_info *cq,
  463. struct ocrdma_queue_info *eq)
  464. {
  465. struct ocrdma_create_cq_cmd *cmd = dev->mbx_cmd;
  466. struct ocrdma_create_cq_cmd_rsp *rsp = dev->mbx_cmd;
  467. int status;
  468. memset(cmd, 0, sizeof(*cmd));
  469. ocrdma_init_mch(&cmd->req, OCRDMA_CMD_CREATE_CQ,
  470. OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
  471. cmd->req.rsvd_version = OCRDMA_CREATE_CQ_VER2;
  472. cmd->pgsz_pgcnt = (cq->size / OCRDMA_MIN_Q_PAGE_SIZE) <<
  473. OCRDMA_CREATE_CQ_PAGE_SIZE_SHIFT;
  474. cmd->pgsz_pgcnt |= PAGES_4K_SPANNED(cq->va, cq->size);
  475. cmd->ev_cnt_flags = OCRDMA_CREATE_CQ_DEF_FLAGS;
  476. cmd->eqn = eq->id;
  477. cmd->pdid_cqecnt = cq->size / sizeof(struct ocrdma_mcqe);
  478. ocrdma_build_q_pages(&cmd->pa[0], cq->size / OCRDMA_MIN_Q_PAGE_SIZE,
  479. cq->dma, PAGE_SIZE_4K);
  480. status = be_roce_mcc_cmd(dev->nic_info.netdev,
  481. cmd, sizeof(*cmd), NULL, NULL);
  482. if (!status) {
  483. cq->id = (u16) (rsp->cq_id & OCRDMA_CREATE_CQ_RSP_CQ_ID_MASK);
  484. cq->created = true;
  485. }
  486. return status;
  487. }
  488. static u32 ocrdma_encoded_q_len(int q_len)
  489. {
  490. u32 len_encoded = fls(q_len); /* log2(len) + 1 */
  491. if (len_encoded == 16)
  492. len_encoded = 0;
  493. return len_encoded;
  494. }
  495. static int ocrdma_mbx_create_mq(struct ocrdma_dev *dev,
  496. struct ocrdma_queue_info *mq,
  497. struct ocrdma_queue_info *cq)
  498. {
  499. int num_pages, status;
  500. struct ocrdma_create_mq_req *cmd = dev->mbx_cmd;
  501. struct ocrdma_create_mq_rsp *rsp = dev->mbx_cmd;
  502. struct ocrdma_pa *pa;
  503. memset(cmd, 0, sizeof(*cmd));
  504. num_pages = PAGES_4K_SPANNED(mq->va, mq->size);
  505. ocrdma_init_mch(&cmd->req, OCRDMA_CMD_CREATE_MQ_EXT,
  506. OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
  507. cmd->req.rsvd_version = 1;
  508. cmd->cqid_pages = num_pages;
  509. cmd->cqid_pages |= (cq->id << OCRDMA_CREATE_MQ_CQ_ID_SHIFT);
  510. cmd->async_cqid_valid = OCRDMA_CREATE_MQ_ASYNC_CQ_VALID;
  511. cmd->async_event_bitmap = BIT(OCRDMA_ASYNC_GRP5_EVE_CODE);
  512. cmd->async_event_bitmap |= BIT(OCRDMA_ASYNC_RDMA_EVE_CODE);
  513. /* Request link events on this MQ. */
  514. cmd->async_event_bitmap |= BIT(OCRDMA_ASYNC_LINK_EVE_CODE);
  515. cmd->async_cqid_ringsize = cq->id;
  516. cmd->async_cqid_ringsize |= (ocrdma_encoded_q_len(mq->len) <<
  517. OCRDMA_CREATE_MQ_RING_SIZE_SHIFT);
  518. cmd->valid = OCRDMA_CREATE_MQ_VALID;
  519. pa = &cmd->pa[0];
  520. ocrdma_build_q_pages(pa, num_pages, mq->dma, PAGE_SIZE_4K);
  521. status = be_roce_mcc_cmd(dev->nic_info.netdev,
  522. cmd, sizeof(*cmd), NULL, NULL);
  523. if (!status) {
  524. mq->id = rsp->id;
  525. mq->created = true;
  526. }
  527. return status;
  528. }
  529. static int ocrdma_create_mq(struct ocrdma_dev *dev)
  530. {
  531. int status;
  532. /* Alloc completion queue for Mailbox queue */
  533. status = ocrdma_alloc_q(dev, &dev->mq.cq, OCRDMA_MQ_CQ_LEN,
  534. sizeof(struct ocrdma_mcqe));
  535. if (status)
  536. goto alloc_err;
  537. dev->eq_tbl[0].cq_cnt++;
  538. status = ocrdma_mbx_mq_cq_create(dev, &dev->mq.cq, &dev->eq_tbl[0].q);
  539. if (status)
  540. goto mbx_cq_free;
  541. memset(&dev->mqe_ctx, 0, sizeof(dev->mqe_ctx));
  542. init_waitqueue_head(&dev->mqe_ctx.cmd_wait);
  543. mutex_init(&dev->mqe_ctx.lock);
  544. /* Alloc Mailbox queue */
  545. status = ocrdma_alloc_q(dev, &dev->mq.sq, OCRDMA_MQ_LEN,
  546. sizeof(struct ocrdma_mqe));
  547. if (status)
  548. goto mbx_cq_destroy;
  549. status = ocrdma_mbx_create_mq(dev, &dev->mq.sq, &dev->mq.cq);
  550. if (status)
  551. goto mbx_q_free;
  552. ocrdma_ring_cq_db(dev, dev->mq.cq.id, true, false, 0);
  553. return 0;
  554. mbx_q_free:
  555. ocrdma_free_q(dev, &dev->mq.sq);
  556. mbx_cq_destroy:
  557. ocrdma_mbx_delete_q(dev, &dev->mq.cq, QTYPE_CQ);
  558. mbx_cq_free:
  559. ocrdma_free_q(dev, &dev->mq.cq);
  560. alloc_err:
  561. return status;
  562. }
  563. static void ocrdma_destroy_mq(struct ocrdma_dev *dev)
  564. {
  565. struct ocrdma_queue_info *mbxq, *cq;
  566. /* mqe_ctx lock synchronizes with any other pending cmds. */
  567. mutex_lock(&dev->mqe_ctx.lock);
  568. mbxq = &dev->mq.sq;
  569. if (mbxq->created) {
  570. ocrdma_mbx_delete_q(dev, mbxq, QTYPE_MCCQ);
  571. ocrdma_free_q(dev, mbxq);
  572. }
  573. mutex_unlock(&dev->mqe_ctx.lock);
  574. cq = &dev->mq.cq;
  575. if (cq->created) {
  576. ocrdma_mbx_delete_q(dev, cq, QTYPE_CQ);
  577. ocrdma_free_q(dev, cq);
  578. }
  579. }
  580. static void ocrdma_process_qpcat_error(struct ocrdma_dev *dev,
  581. struct ocrdma_qp *qp)
  582. {
  583. enum ib_qp_state new_ib_qps = IB_QPS_ERR;
  584. enum ib_qp_state old_ib_qps;
  585. if (qp == NULL)
  586. BUG();
  587. ocrdma_qp_state_change(qp, new_ib_qps, &old_ib_qps);
  588. }
  589. static void ocrdma_dispatch_ibevent(struct ocrdma_dev *dev,
  590. struct ocrdma_ae_mcqe *cqe)
  591. {
  592. struct ocrdma_qp *qp = NULL;
  593. struct ocrdma_cq *cq = NULL;
  594. struct ib_event ib_evt;
  595. int cq_event = 0;
  596. int qp_event = 1;
  597. int srq_event = 0;
  598. int dev_event = 0;
  599. int type = (cqe->valid_ae_event & OCRDMA_AE_MCQE_EVENT_TYPE_MASK) >>
  600. OCRDMA_AE_MCQE_EVENT_TYPE_SHIFT;
  601. u16 qpid = cqe->qpvalid_qpid & OCRDMA_AE_MCQE_QPID_MASK;
  602. u16 cqid = cqe->cqvalid_cqid & OCRDMA_AE_MCQE_CQID_MASK;
  603. /*
  604. * Some FW version returns wrong qp or cq ids in CQEs.
  605. * Checking whether the IDs are valid
  606. */
  607. if (cqe->qpvalid_qpid & OCRDMA_AE_MCQE_QPVALID) {
  608. if (qpid < dev->attr.max_qp)
  609. qp = dev->qp_tbl[qpid];
  610. if (qp == NULL) {
  611. pr_err("ocrdma%d:Async event - qpid %u is not valid\n",
  612. dev->id, qpid);
  613. return;
  614. }
  615. }
  616. if (cqe->cqvalid_cqid & OCRDMA_AE_MCQE_CQVALID) {
  617. if (cqid < dev->attr.max_cq)
  618. cq = dev->cq_tbl[cqid];
  619. if (cq == NULL) {
  620. pr_err("ocrdma%d:Async event - cqid %u is not valid\n",
  621. dev->id, cqid);
  622. return;
  623. }
  624. }
  625. memset(&ib_evt, 0, sizeof(ib_evt));
  626. ib_evt.device = &dev->ibdev;
  627. switch (type) {
  628. case OCRDMA_CQ_ERROR:
  629. ib_evt.element.cq = &cq->ibcq;
  630. ib_evt.event = IB_EVENT_CQ_ERR;
  631. cq_event = 1;
  632. qp_event = 0;
  633. break;
  634. case OCRDMA_CQ_OVERRUN_ERROR:
  635. ib_evt.element.cq = &cq->ibcq;
  636. ib_evt.event = IB_EVENT_CQ_ERR;
  637. cq_event = 1;
  638. qp_event = 0;
  639. break;
  640. case OCRDMA_CQ_QPCAT_ERROR:
  641. ib_evt.element.qp = &qp->ibqp;
  642. ib_evt.event = IB_EVENT_QP_FATAL;
  643. ocrdma_process_qpcat_error(dev, qp);
  644. break;
  645. case OCRDMA_QP_ACCESS_ERROR:
  646. ib_evt.element.qp = &qp->ibqp;
  647. ib_evt.event = IB_EVENT_QP_ACCESS_ERR;
  648. break;
  649. case OCRDMA_QP_COMM_EST_EVENT:
  650. ib_evt.element.qp = &qp->ibqp;
  651. ib_evt.event = IB_EVENT_COMM_EST;
  652. break;
  653. case OCRDMA_SQ_DRAINED_EVENT:
  654. ib_evt.element.qp = &qp->ibqp;
  655. ib_evt.event = IB_EVENT_SQ_DRAINED;
  656. break;
  657. case OCRDMA_DEVICE_FATAL_EVENT:
  658. ib_evt.element.port_num = 1;
  659. ib_evt.event = IB_EVENT_DEVICE_FATAL;
  660. qp_event = 0;
  661. dev_event = 1;
  662. break;
  663. case OCRDMA_SRQCAT_ERROR:
  664. ib_evt.element.srq = &qp->srq->ibsrq;
  665. ib_evt.event = IB_EVENT_SRQ_ERR;
  666. srq_event = 1;
  667. qp_event = 0;
  668. break;
  669. case OCRDMA_SRQ_LIMIT_EVENT:
  670. ib_evt.element.srq = &qp->srq->ibsrq;
  671. ib_evt.event = IB_EVENT_SRQ_LIMIT_REACHED;
  672. srq_event = 1;
  673. qp_event = 0;
  674. break;
  675. case OCRDMA_QP_LAST_WQE_EVENT:
  676. ib_evt.element.qp = &qp->ibqp;
  677. ib_evt.event = IB_EVENT_QP_LAST_WQE_REACHED;
  678. break;
  679. default:
  680. cq_event = 0;
  681. qp_event = 0;
  682. srq_event = 0;
  683. dev_event = 0;
  684. pr_err("%s() unknown type=0x%x\n", __func__, type);
  685. break;
  686. }
  687. if (type < OCRDMA_MAX_ASYNC_ERRORS)
  688. atomic_inc(&dev->async_err_stats[type]);
  689. if (qp_event) {
  690. if (qp->ibqp.event_handler)
  691. qp->ibqp.event_handler(&ib_evt, qp->ibqp.qp_context);
  692. } else if (cq_event) {
  693. if (cq->ibcq.event_handler)
  694. cq->ibcq.event_handler(&ib_evt, cq->ibcq.cq_context);
  695. } else if (srq_event) {
  696. if (qp->srq->ibsrq.event_handler)
  697. qp->srq->ibsrq.event_handler(&ib_evt,
  698. qp->srq->ibsrq.
  699. srq_context);
  700. } else if (dev_event) {
  701. pr_err("%s: Fatal event received\n", dev->ibdev.name);
  702. ib_dispatch_event(&ib_evt);
  703. }
  704. }
  705. static void ocrdma_process_grp5_aync(struct ocrdma_dev *dev,
  706. struct ocrdma_ae_mcqe *cqe)
  707. {
  708. struct ocrdma_ae_pvid_mcqe *evt;
  709. int type = (cqe->valid_ae_event & OCRDMA_AE_MCQE_EVENT_TYPE_MASK) >>
  710. OCRDMA_AE_MCQE_EVENT_TYPE_SHIFT;
  711. switch (type) {
  712. case OCRDMA_ASYNC_EVENT_PVID_STATE:
  713. evt = (struct ocrdma_ae_pvid_mcqe *)cqe;
  714. if ((evt->tag_enabled & OCRDMA_AE_PVID_MCQE_ENABLED_MASK) >>
  715. OCRDMA_AE_PVID_MCQE_ENABLED_SHIFT)
  716. dev->pvid = ((evt->tag_enabled &
  717. OCRDMA_AE_PVID_MCQE_TAG_MASK) >>
  718. OCRDMA_AE_PVID_MCQE_TAG_SHIFT);
  719. break;
  720. case OCRDMA_ASYNC_EVENT_COS_VALUE:
  721. atomic_set(&dev->update_sl, 1);
  722. break;
  723. default:
  724. /* Not interested evts. */
  725. break;
  726. }
  727. }
  728. static void ocrdma_process_link_state(struct ocrdma_dev *dev,
  729. struct ocrdma_ae_mcqe *cqe)
  730. {
  731. struct ocrdma_ae_lnkst_mcqe *evt;
  732. u8 lstate;
  733. evt = (struct ocrdma_ae_lnkst_mcqe *)cqe;
  734. lstate = ocrdma_get_ae_link_state(evt->speed_state_ptn);
  735. if (!(lstate & OCRDMA_AE_LSC_LLINK_MASK))
  736. return;
  737. if (dev->flags & OCRDMA_FLAGS_LINK_STATUS_INIT)
  738. ocrdma_update_link_state(dev, (lstate & OCRDMA_LINK_ST_MASK));
  739. }
  740. static void ocrdma_process_acqe(struct ocrdma_dev *dev, void *ae_cqe)
  741. {
  742. /* async CQE processing */
  743. struct ocrdma_ae_mcqe *cqe = ae_cqe;
  744. u32 evt_code = (cqe->valid_ae_event & OCRDMA_AE_MCQE_EVENT_CODE_MASK) >>
  745. OCRDMA_AE_MCQE_EVENT_CODE_SHIFT;
  746. switch (evt_code) {
  747. case OCRDMA_ASYNC_LINK_EVE_CODE:
  748. ocrdma_process_link_state(dev, cqe);
  749. break;
  750. case OCRDMA_ASYNC_RDMA_EVE_CODE:
  751. ocrdma_dispatch_ibevent(dev, cqe);
  752. break;
  753. case OCRDMA_ASYNC_GRP5_EVE_CODE:
  754. ocrdma_process_grp5_aync(dev, cqe);
  755. break;
  756. default:
  757. pr_err("%s(%d) invalid evt code=0x%x\n", __func__,
  758. dev->id, evt_code);
  759. }
  760. }
  761. static void ocrdma_process_mcqe(struct ocrdma_dev *dev, struct ocrdma_mcqe *cqe)
  762. {
  763. if (dev->mqe_ctx.tag == cqe->tag_lo && dev->mqe_ctx.cmd_done == false) {
  764. dev->mqe_ctx.cqe_status = (cqe->status &
  765. OCRDMA_MCQE_STATUS_MASK) >> OCRDMA_MCQE_STATUS_SHIFT;
  766. dev->mqe_ctx.ext_status =
  767. (cqe->status & OCRDMA_MCQE_ESTATUS_MASK)
  768. >> OCRDMA_MCQE_ESTATUS_SHIFT;
  769. dev->mqe_ctx.cmd_done = true;
  770. wake_up(&dev->mqe_ctx.cmd_wait);
  771. } else
  772. pr_err("%s() cqe for invalid tag0x%x.expected=0x%x\n",
  773. __func__, cqe->tag_lo, dev->mqe_ctx.tag);
  774. }
  775. static int ocrdma_mq_cq_handler(struct ocrdma_dev *dev, u16 cq_id)
  776. {
  777. u16 cqe_popped = 0;
  778. struct ocrdma_mcqe *cqe;
  779. while (1) {
  780. cqe = ocrdma_get_mcqe(dev);
  781. if (cqe == NULL)
  782. break;
  783. ocrdma_le32_to_cpu(cqe, sizeof(*cqe));
  784. cqe_popped += 1;
  785. if (cqe->valid_ae_cmpl_cons & OCRDMA_MCQE_AE_MASK)
  786. ocrdma_process_acqe(dev, cqe);
  787. else if (cqe->valid_ae_cmpl_cons & OCRDMA_MCQE_CMPL_MASK)
  788. ocrdma_process_mcqe(dev, cqe);
  789. memset(cqe, 0, sizeof(struct ocrdma_mcqe));
  790. ocrdma_mcq_inc_tail(dev);
  791. }
  792. ocrdma_ring_cq_db(dev, dev->mq.cq.id, true, false, cqe_popped);
  793. return 0;
  794. }
  795. static struct ocrdma_cq *_ocrdma_qp_buddy_cq_handler(struct ocrdma_dev *dev,
  796. struct ocrdma_cq *cq, bool sq)
  797. {
  798. struct ocrdma_qp *qp;
  799. struct list_head *cur;
  800. struct ocrdma_cq *bcq = NULL;
  801. struct list_head *head = sq?(&cq->sq_head):(&cq->rq_head);
  802. list_for_each(cur, head) {
  803. if (sq)
  804. qp = list_entry(cur, struct ocrdma_qp, sq_entry);
  805. else
  806. qp = list_entry(cur, struct ocrdma_qp, rq_entry);
  807. if (qp->srq)
  808. continue;
  809. /* if wq and rq share the same cq, than comp_handler
  810. * is already invoked.
  811. */
  812. if (qp->sq_cq == qp->rq_cq)
  813. continue;
  814. /* if completion came on sq, rq's cq is buddy cq.
  815. * if completion came on rq, sq's cq is buddy cq.
  816. */
  817. if (qp->sq_cq == cq)
  818. bcq = qp->rq_cq;
  819. else
  820. bcq = qp->sq_cq;
  821. return bcq;
  822. }
  823. return NULL;
  824. }
  825. static void ocrdma_qp_buddy_cq_handler(struct ocrdma_dev *dev,
  826. struct ocrdma_cq *cq)
  827. {
  828. unsigned long flags;
  829. struct ocrdma_cq *bcq = NULL;
  830. /* Go through list of QPs in error state which are using this CQ
  831. * and invoke its callback handler to trigger CQE processing for
  832. * error/flushed CQE. It is rare to find more than few entries in
  833. * this list as most consumers stops after getting error CQE.
  834. * List is traversed only once when a matching buddy cq found for a QP.
  835. */
  836. spin_lock_irqsave(&dev->flush_q_lock, flags);
  837. /* Check if buddy CQ is present.
  838. * true - Check for SQ CQ
  839. * false - Check for RQ CQ
  840. */
  841. bcq = _ocrdma_qp_buddy_cq_handler(dev, cq, true);
  842. if (bcq == NULL)
  843. bcq = _ocrdma_qp_buddy_cq_handler(dev, cq, false);
  844. spin_unlock_irqrestore(&dev->flush_q_lock, flags);
  845. /* if there is valid buddy cq, look for its completion handler */
  846. if (bcq && bcq->ibcq.comp_handler) {
  847. spin_lock_irqsave(&bcq->comp_handler_lock, flags);
  848. (*bcq->ibcq.comp_handler) (&bcq->ibcq, bcq->ibcq.cq_context);
  849. spin_unlock_irqrestore(&bcq->comp_handler_lock, flags);
  850. }
  851. }
  852. static void ocrdma_qp_cq_handler(struct ocrdma_dev *dev, u16 cq_idx)
  853. {
  854. unsigned long flags;
  855. struct ocrdma_cq *cq;
  856. if (cq_idx >= OCRDMA_MAX_CQ)
  857. BUG();
  858. cq = dev->cq_tbl[cq_idx];
  859. if (cq == NULL)
  860. return;
  861. if (cq->ibcq.comp_handler) {
  862. spin_lock_irqsave(&cq->comp_handler_lock, flags);
  863. (*cq->ibcq.comp_handler) (&cq->ibcq, cq->ibcq.cq_context);
  864. spin_unlock_irqrestore(&cq->comp_handler_lock, flags);
  865. }
  866. ocrdma_qp_buddy_cq_handler(dev, cq);
  867. }
  868. static void ocrdma_cq_handler(struct ocrdma_dev *dev, u16 cq_id)
  869. {
  870. /* process the MQ-CQE. */
  871. if (cq_id == dev->mq.cq.id)
  872. ocrdma_mq_cq_handler(dev, cq_id);
  873. else
  874. ocrdma_qp_cq_handler(dev, cq_id);
  875. }
  876. static irqreturn_t ocrdma_irq_handler(int irq, void *handle)
  877. {
  878. struct ocrdma_eq *eq = handle;
  879. struct ocrdma_dev *dev = eq->dev;
  880. struct ocrdma_eqe eqe;
  881. struct ocrdma_eqe *ptr;
  882. u16 cq_id;
  883. u8 mcode;
  884. int budget = eq->cq_cnt;
  885. do {
  886. ptr = ocrdma_get_eqe(eq);
  887. eqe = *ptr;
  888. ocrdma_le32_to_cpu(&eqe, sizeof(eqe));
  889. mcode = (eqe.id_valid & OCRDMA_EQE_MAJOR_CODE_MASK)
  890. >> OCRDMA_EQE_MAJOR_CODE_SHIFT;
  891. if (mcode == OCRDMA_MAJOR_CODE_SENTINAL)
  892. pr_err("EQ full on eqid = 0x%x, eqe = 0x%x\n",
  893. eq->q.id, eqe.id_valid);
  894. if ((eqe.id_valid & OCRDMA_EQE_VALID_MASK) == 0)
  895. break;
  896. ptr->id_valid = 0;
  897. /* ring eq doorbell as soon as its consumed. */
  898. ocrdma_ring_eq_db(dev, eq->q.id, false, true, 1);
  899. /* check whether its CQE or not. */
  900. if ((eqe.id_valid & OCRDMA_EQE_FOR_CQE_MASK) == 0) {
  901. cq_id = eqe.id_valid >> OCRDMA_EQE_RESOURCE_ID_SHIFT;
  902. ocrdma_cq_handler(dev, cq_id);
  903. }
  904. ocrdma_eq_inc_tail(eq);
  905. /* There can be a stale EQE after the last bound CQ is
  906. * destroyed. EQE valid and budget == 0 implies this.
  907. */
  908. if (budget)
  909. budget--;
  910. } while (budget);
  911. eq->aic_obj.eq_intr_cnt++;
  912. ocrdma_ring_eq_db(dev, eq->q.id, true, true, 0);
  913. return IRQ_HANDLED;
  914. }
  915. static void ocrdma_post_mqe(struct ocrdma_dev *dev, struct ocrdma_mqe *cmd)
  916. {
  917. struct ocrdma_mqe *mqe;
  918. dev->mqe_ctx.tag = dev->mq.sq.head;
  919. dev->mqe_ctx.cmd_done = false;
  920. mqe = ocrdma_get_mqe(dev);
  921. cmd->hdr.tag_lo = dev->mq.sq.head;
  922. ocrdma_copy_cpu_to_le32(mqe, cmd, sizeof(*mqe));
  923. /* make sure descriptor is written before ringing doorbell */
  924. wmb();
  925. ocrdma_mq_inc_head(dev);
  926. ocrdma_ring_mq_db(dev);
  927. }
  928. static int ocrdma_wait_mqe_cmpl(struct ocrdma_dev *dev)
  929. {
  930. long status;
  931. /* 30 sec timeout */
  932. status = wait_event_timeout(dev->mqe_ctx.cmd_wait,
  933. (dev->mqe_ctx.cmd_done != false),
  934. msecs_to_jiffies(30000));
  935. if (status)
  936. return 0;
  937. else {
  938. dev->mqe_ctx.fw_error_state = true;
  939. pr_err("%s(%d) mailbox timeout: fw not responding\n",
  940. __func__, dev->id);
  941. return -1;
  942. }
  943. }
  944. /* issue a mailbox command on the MQ */
  945. static int ocrdma_mbx_cmd(struct ocrdma_dev *dev, struct ocrdma_mqe *mqe)
  946. {
  947. int status = 0;
  948. u16 cqe_status, ext_status;
  949. struct ocrdma_mqe *rsp_mqe;
  950. struct ocrdma_mbx_rsp *rsp = NULL;
  951. mutex_lock(&dev->mqe_ctx.lock);
  952. if (dev->mqe_ctx.fw_error_state)
  953. goto mbx_err;
  954. ocrdma_post_mqe(dev, mqe);
  955. status = ocrdma_wait_mqe_cmpl(dev);
  956. if (status)
  957. goto mbx_err;
  958. cqe_status = dev->mqe_ctx.cqe_status;
  959. ext_status = dev->mqe_ctx.ext_status;
  960. rsp_mqe = ocrdma_get_mqe_rsp(dev);
  961. ocrdma_copy_le32_to_cpu(mqe, rsp_mqe, (sizeof(*mqe)));
  962. if ((mqe->hdr.spcl_sge_cnt_emb & OCRDMA_MQE_HDR_EMB_MASK) >>
  963. OCRDMA_MQE_HDR_EMB_SHIFT)
  964. rsp = &mqe->u.rsp;
  965. if (cqe_status || ext_status) {
  966. pr_err("%s() cqe_status=0x%x, ext_status=0x%x,\n",
  967. __func__, cqe_status, ext_status);
  968. if (rsp) {
  969. /* This is for embedded cmds. */
  970. pr_err("opcode=0x%x, subsystem=0x%x\n",
  971. (rsp->subsys_op & OCRDMA_MBX_RSP_OPCODE_MASK) >>
  972. OCRDMA_MBX_RSP_OPCODE_SHIFT,
  973. (rsp->subsys_op & OCRDMA_MBX_RSP_SUBSYS_MASK) >>
  974. OCRDMA_MBX_RSP_SUBSYS_SHIFT);
  975. }
  976. status = ocrdma_get_mbx_cqe_errno(cqe_status);
  977. goto mbx_err;
  978. }
  979. /* For non embedded, rsp errors are handled in ocrdma_nonemb_mbx_cmd */
  980. if (rsp && (mqe->u.rsp.status & OCRDMA_MBX_RSP_STATUS_MASK))
  981. status = ocrdma_get_mbx_errno(mqe->u.rsp.status);
  982. mbx_err:
  983. mutex_unlock(&dev->mqe_ctx.lock);
  984. return status;
  985. }
  986. static int ocrdma_nonemb_mbx_cmd(struct ocrdma_dev *dev, struct ocrdma_mqe *mqe,
  987. void *payload_va)
  988. {
  989. int status;
  990. struct ocrdma_mbx_rsp *rsp = payload_va;
  991. if ((mqe->hdr.spcl_sge_cnt_emb & OCRDMA_MQE_HDR_EMB_MASK) >>
  992. OCRDMA_MQE_HDR_EMB_SHIFT)
  993. BUG();
  994. status = ocrdma_mbx_cmd(dev, mqe);
  995. if (!status)
  996. /* For non embedded, only CQE failures are handled in
  997. * ocrdma_mbx_cmd. We need to check for RSP errors.
  998. */
  999. if (rsp->status & OCRDMA_MBX_RSP_STATUS_MASK)
  1000. status = ocrdma_get_mbx_errno(rsp->status);
  1001. if (status)
  1002. pr_err("opcode=0x%x, subsystem=0x%x\n",
  1003. (rsp->subsys_op & OCRDMA_MBX_RSP_OPCODE_MASK) >>
  1004. OCRDMA_MBX_RSP_OPCODE_SHIFT,
  1005. (rsp->subsys_op & OCRDMA_MBX_RSP_SUBSYS_MASK) >>
  1006. OCRDMA_MBX_RSP_SUBSYS_SHIFT);
  1007. return status;
  1008. }
  1009. static void ocrdma_get_attr(struct ocrdma_dev *dev,
  1010. struct ocrdma_dev_attr *attr,
  1011. struct ocrdma_mbx_query_config *rsp)
  1012. {
  1013. attr->max_pd =
  1014. (rsp->max_pd_ca_ack_delay & OCRDMA_MBX_QUERY_CFG_MAX_PD_MASK) >>
  1015. OCRDMA_MBX_QUERY_CFG_MAX_PD_SHIFT;
  1016. attr->udp_encap = (rsp->max_pd_ca_ack_delay &
  1017. OCRDMA_MBX_QUERY_CFG_L3_TYPE_MASK) >>
  1018. OCRDMA_MBX_QUERY_CFG_L3_TYPE_SHIFT;
  1019. attr->max_dpp_pds =
  1020. (rsp->max_dpp_pds_credits & OCRDMA_MBX_QUERY_CFG_MAX_DPP_PDS_MASK) >>
  1021. OCRDMA_MBX_QUERY_CFG_MAX_DPP_PDS_OFFSET;
  1022. attr->max_qp =
  1023. (rsp->qp_srq_cq_ird_ord & OCRDMA_MBX_QUERY_CFG_MAX_QP_MASK) >>
  1024. OCRDMA_MBX_QUERY_CFG_MAX_QP_SHIFT;
  1025. attr->max_srq =
  1026. (rsp->max_srq_rpir_qps & OCRDMA_MBX_QUERY_CFG_MAX_SRQ_MASK) >>
  1027. OCRDMA_MBX_QUERY_CFG_MAX_SRQ_OFFSET;
  1028. attr->max_send_sge = ((rsp->max_recv_send_sge &
  1029. OCRDMA_MBX_QUERY_CFG_MAX_SEND_SGE_MASK) >>
  1030. OCRDMA_MBX_QUERY_CFG_MAX_SEND_SGE_SHIFT);
  1031. attr->max_recv_sge = (rsp->max_recv_send_sge &
  1032. OCRDMA_MBX_QUERY_CFG_MAX_RECV_SGE_MASK) >>
  1033. OCRDMA_MBX_QUERY_CFG_MAX_RECV_SGE_SHIFT;
  1034. attr->max_srq_sge = (rsp->max_srq_rqe_sge &
  1035. OCRDMA_MBX_QUERY_CFG_MAX_SRQ_SGE_MASK) >>
  1036. OCRDMA_MBX_QUERY_CFG_MAX_SRQ_SGE_OFFSET;
  1037. attr->max_rdma_sge = (rsp->max_wr_rd_sge &
  1038. OCRDMA_MBX_QUERY_CFG_MAX_RD_SGE_MASK) >>
  1039. OCRDMA_MBX_QUERY_CFG_MAX_RD_SGE_SHIFT;
  1040. attr->max_ord_per_qp = (rsp->max_ird_ord_per_qp &
  1041. OCRDMA_MBX_QUERY_CFG_MAX_ORD_PER_QP_MASK) >>
  1042. OCRDMA_MBX_QUERY_CFG_MAX_ORD_PER_QP_SHIFT;
  1043. attr->max_ird_per_qp = (rsp->max_ird_ord_per_qp &
  1044. OCRDMA_MBX_QUERY_CFG_MAX_IRD_PER_QP_MASK) >>
  1045. OCRDMA_MBX_QUERY_CFG_MAX_IRD_PER_QP_SHIFT;
  1046. attr->cq_overflow_detect = (rsp->qp_srq_cq_ird_ord &
  1047. OCRDMA_MBX_QUERY_CFG_CQ_OVERFLOW_MASK) >>
  1048. OCRDMA_MBX_QUERY_CFG_CQ_OVERFLOW_SHIFT;
  1049. attr->srq_supported = (rsp->qp_srq_cq_ird_ord &
  1050. OCRDMA_MBX_QUERY_CFG_SRQ_SUPPORTED_MASK) >>
  1051. OCRDMA_MBX_QUERY_CFG_SRQ_SUPPORTED_SHIFT;
  1052. attr->local_ca_ack_delay = (rsp->max_pd_ca_ack_delay &
  1053. OCRDMA_MBX_QUERY_CFG_CA_ACK_DELAY_MASK) >>
  1054. OCRDMA_MBX_QUERY_CFG_CA_ACK_DELAY_SHIFT;
  1055. attr->max_mw = rsp->max_mw;
  1056. attr->max_mr = rsp->max_mr;
  1057. attr->max_mr_size = ((u64)rsp->max_mr_size_hi << 32) |
  1058. rsp->max_mr_size_lo;
  1059. attr->max_fmr = 0;
  1060. attr->max_pages_per_frmr = rsp->max_pages_per_frmr;
  1061. attr->max_num_mr_pbl = rsp->max_num_mr_pbl;
  1062. attr->max_cqe = rsp->max_cq_cqes_per_cq &
  1063. OCRDMA_MBX_QUERY_CFG_MAX_CQES_PER_CQ_MASK;
  1064. attr->max_cq = (rsp->max_cq_cqes_per_cq &
  1065. OCRDMA_MBX_QUERY_CFG_MAX_CQ_MASK) >>
  1066. OCRDMA_MBX_QUERY_CFG_MAX_CQ_OFFSET;
  1067. attr->wqe_size = ((rsp->wqe_rqe_stride_max_dpp_cqs &
  1068. OCRDMA_MBX_QUERY_CFG_MAX_WQE_SIZE_MASK) >>
  1069. OCRDMA_MBX_QUERY_CFG_MAX_WQE_SIZE_OFFSET) *
  1070. OCRDMA_WQE_STRIDE;
  1071. attr->rqe_size = ((rsp->wqe_rqe_stride_max_dpp_cqs &
  1072. OCRDMA_MBX_QUERY_CFG_MAX_RQE_SIZE_MASK) >>
  1073. OCRDMA_MBX_QUERY_CFG_MAX_RQE_SIZE_OFFSET) *
  1074. OCRDMA_WQE_STRIDE;
  1075. attr->max_inline_data =
  1076. attr->wqe_size - (sizeof(struct ocrdma_hdr_wqe) +
  1077. sizeof(struct ocrdma_sge));
  1078. if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
  1079. attr->ird = 1;
  1080. attr->ird_page_size = OCRDMA_MIN_Q_PAGE_SIZE;
  1081. attr->num_ird_pages = MAX_OCRDMA_IRD_PAGES;
  1082. }
  1083. dev->attr.max_wqe = rsp->max_wqes_rqes_per_q >>
  1084. OCRDMA_MBX_QUERY_CFG_MAX_WQES_PER_WQ_OFFSET;
  1085. dev->attr.max_rqe = rsp->max_wqes_rqes_per_q &
  1086. OCRDMA_MBX_QUERY_CFG_MAX_RQES_PER_RQ_MASK;
  1087. }
  1088. static int ocrdma_check_fw_config(struct ocrdma_dev *dev,
  1089. struct ocrdma_fw_conf_rsp *conf)
  1090. {
  1091. u32 fn_mode;
  1092. fn_mode = conf->fn_mode & OCRDMA_FN_MODE_RDMA;
  1093. if (fn_mode != OCRDMA_FN_MODE_RDMA)
  1094. return -EINVAL;
  1095. dev->base_eqid = conf->base_eqid;
  1096. dev->max_eq = conf->max_eq;
  1097. return 0;
  1098. }
  1099. /* can be issued only during init time. */
  1100. static int ocrdma_mbx_query_fw_ver(struct ocrdma_dev *dev)
  1101. {
  1102. int status = -ENOMEM;
  1103. struct ocrdma_mqe *cmd;
  1104. struct ocrdma_fw_ver_rsp *rsp;
  1105. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_GET_FW_VER, sizeof(*cmd));
  1106. if (!cmd)
  1107. return -ENOMEM;
  1108. ocrdma_init_mch((struct ocrdma_mbx_hdr *)&cmd->u.cmd[0],
  1109. OCRDMA_CMD_GET_FW_VER,
  1110. OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
  1111. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  1112. if (status)
  1113. goto mbx_err;
  1114. rsp = (struct ocrdma_fw_ver_rsp *)cmd;
  1115. memset(&dev->attr.fw_ver[0], 0, sizeof(dev->attr.fw_ver));
  1116. memcpy(&dev->attr.fw_ver[0], &rsp->running_ver[0],
  1117. sizeof(rsp->running_ver));
  1118. ocrdma_le32_to_cpu(dev->attr.fw_ver, sizeof(rsp->running_ver));
  1119. mbx_err:
  1120. kfree(cmd);
  1121. return status;
  1122. }
  1123. /* can be issued only during init time. */
  1124. static int ocrdma_mbx_query_fw_config(struct ocrdma_dev *dev)
  1125. {
  1126. int status = -ENOMEM;
  1127. struct ocrdma_mqe *cmd;
  1128. struct ocrdma_fw_conf_rsp *rsp;
  1129. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_GET_FW_CONFIG, sizeof(*cmd));
  1130. if (!cmd)
  1131. return -ENOMEM;
  1132. ocrdma_init_mch((struct ocrdma_mbx_hdr *)&cmd->u.cmd[0],
  1133. OCRDMA_CMD_GET_FW_CONFIG,
  1134. OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
  1135. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  1136. if (status)
  1137. goto mbx_err;
  1138. rsp = (struct ocrdma_fw_conf_rsp *)cmd;
  1139. status = ocrdma_check_fw_config(dev, rsp);
  1140. mbx_err:
  1141. kfree(cmd);
  1142. return status;
  1143. }
  1144. int ocrdma_mbx_rdma_stats(struct ocrdma_dev *dev, bool reset)
  1145. {
  1146. struct ocrdma_rdma_stats_req *req = dev->stats_mem.va;
  1147. struct ocrdma_mqe *mqe = &dev->stats_mem.mqe;
  1148. struct ocrdma_rdma_stats_resp *old_stats;
  1149. int status;
  1150. old_stats = kmalloc(sizeof(*old_stats), GFP_KERNEL);
  1151. if (old_stats == NULL)
  1152. return -ENOMEM;
  1153. memset(mqe, 0, sizeof(*mqe));
  1154. mqe->hdr.pyld_len = dev->stats_mem.size;
  1155. mqe->hdr.spcl_sge_cnt_emb |=
  1156. (1 << OCRDMA_MQE_HDR_SGE_CNT_SHIFT) &
  1157. OCRDMA_MQE_HDR_SGE_CNT_MASK;
  1158. mqe->u.nonemb_req.sge[0].pa_lo = (u32) (dev->stats_mem.pa & 0xffffffff);
  1159. mqe->u.nonemb_req.sge[0].pa_hi = (u32) upper_32_bits(dev->stats_mem.pa);
  1160. mqe->u.nonemb_req.sge[0].len = dev->stats_mem.size;
  1161. /* Cache the old stats */
  1162. memcpy(old_stats, req, sizeof(struct ocrdma_rdma_stats_resp));
  1163. memset(req, 0, dev->stats_mem.size);
  1164. ocrdma_init_mch((struct ocrdma_mbx_hdr *)req,
  1165. OCRDMA_CMD_GET_RDMA_STATS,
  1166. OCRDMA_SUBSYS_ROCE,
  1167. dev->stats_mem.size);
  1168. if (reset)
  1169. req->reset_stats = reset;
  1170. status = ocrdma_nonemb_mbx_cmd(dev, mqe, dev->stats_mem.va);
  1171. if (status)
  1172. /* Copy from cache, if mbox fails */
  1173. memcpy(req, old_stats, sizeof(struct ocrdma_rdma_stats_resp));
  1174. else
  1175. ocrdma_le32_to_cpu(req, dev->stats_mem.size);
  1176. kfree(old_stats);
  1177. return status;
  1178. }
  1179. static int ocrdma_mbx_get_ctrl_attribs(struct ocrdma_dev *dev)
  1180. {
  1181. int status = -ENOMEM;
  1182. struct ocrdma_dma_mem dma;
  1183. struct ocrdma_mqe *mqe;
  1184. struct ocrdma_get_ctrl_attribs_rsp *ctrl_attr_rsp;
  1185. struct mgmt_hba_attribs *hba_attribs;
  1186. mqe = kzalloc(sizeof(struct ocrdma_mqe), GFP_KERNEL);
  1187. if (!mqe)
  1188. return status;
  1189. dma.size = sizeof(struct ocrdma_get_ctrl_attribs_rsp);
  1190. dma.va = dma_alloc_coherent(&dev->nic_info.pdev->dev,
  1191. dma.size, &dma.pa, GFP_KERNEL);
  1192. if (!dma.va)
  1193. goto free_mqe;
  1194. mqe->hdr.pyld_len = dma.size;
  1195. mqe->hdr.spcl_sge_cnt_emb |=
  1196. (1 << OCRDMA_MQE_HDR_SGE_CNT_SHIFT) &
  1197. OCRDMA_MQE_HDR_SGE_CNT_MASK;
  1198. mqe->u.nonemb_req.sge[0].pa_lo = (u32) (dma.pa & 0xffffffff);
  1199. mqe->u.nonemb_req.sge[0].pa_hi = (u32) upper_32_bits(dma.pa);
  1200. mqe->u.nonemb_req.sge[0].len = dma.size;
  1201. memset(dma.va, 0, dma.size);
  1202. ocrdma_init_mch((struct ocrdma_mbx_hdr *)dma.va,
  1203. OCRDMA_CMD_GET_CTRL_ATTRIBUTES,
  1204. OCRDMA_SUBSYS_COMMON,
  1205. dma.size);
  1206. status = ocrdma_nonemb_mbx_cmd(dev, mqe, dma.va);
  1207. if (!status) {
  1208. ctrl_attr_rsp = (struct ocrdma_get_ctrl_attribs_rsp *)dma.va;
  1209. hba_attribs = &ctrl_attr_rsp->ctrl_attribs.hba_attribs;
  1210. dev->hba_port_num = (hba_attribs->ptpnum_maxdoms_hbast_cv &
  1211. OCRDMA_HBA_ATTRB_PTNUM_MASK)
  1212. >> OCRDMA_HBA_ATTRB_PTNUM_SHIFT;
  1213. strlcpy(dev->model_number,
  1214. hba_attribs->controller_model_number,
  1215. sizeof(dev->model_number));
  1216. }
  1217. dma_free_coherent(&dev->nic_info.pdev->dev, dma.size, dma.va, dma.pa);
  1218. free_mqe:
  1219. kfree(mqe);
  1220. return status;
  1221. }
  1222. static int ocrdma_mbx_query_dev(struct ocrdma_dev *dev)
  1223. {
  1224. int status = -ENOMEM;
  1225. struct ocrdma_mbx_query_config *rsp;
  1226. struct ocrdma_mqe *cmd;
  1227. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_QUERY_CONFIG, sizeof(*cmd));
  1228. if (!cmd)
  1229. return status;
  1230. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  1231. if (status)
  1232. goto mbx_err;
  1233. rsp = (struct ocrdma_mbx_query_config *)cmd;
  1234. ocrdma_get_attr(dev, &dev->attr, rsp);
  1235. mbx_err:
  1236. kfree(cmd);
  1237. return status;
  1238. }
  1239. int ocrdma_mbx_get_link_speed(struct ocrdma_dev *dev, u8 *lnk_speed,
  1240. u8 *lnk_state)
  1241. {
  1242. int status = -ENOMEM;
  1243. struct ocrdma_get_link_speed_rsp *rsp;
  1244. struct ocrdma_mqe *cmd;
  1245. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_QUERY_NTWK_LINK_CONFIG_V1,
  1246. sizeof(*cmd));
  1247. if (!cmd)
  1248. return status;
  1249. ocrdma_init_mch((struct ocrdma_mbx_hdr *)&cmd->u.cmd[0],
  1250. OCRDMA_CMD_QUERY_NTWK_LINK_CONFIG_V1,
  1251. OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
  1252. ((struct ocrdma_mbx_hdr *)cmd->u.cmd)->rsvd_version = 0x1;
  1253. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  1254. if (status)
  1255. goto mbx_err;
  1256. rsp = (struct ocrdma_get_link_speed_rsp *)cmd;
  1257. if (lnk_speed)
  1258. *lnk_speed = (rsp->pflt_pps_ld_pnum & OCRDMA_PHY_PS_MASK)
  1259. >> OCRDMA_PHY_PS_SHIFT;
  1260. if (lnk_state)
  1261. *lnk_state = (rsp->res_lnk_st & OCRDMA_LINK_ST_MASK);
  1262. mbx_err:
  1263. kfree(cmd);
  1264. return status;
  1265. }
  1266. static int ocrdma_mbx_get_phy_info(struct ocrdma_dev *dev)
  1267. {
  1268. int status = -ENOMEM;
  1269. struct ocrdma_mqe *cmd;
  1270. struct ocrdma_get_phy_info_rsp *rsp;
  1271. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_PHY_DETAILS, sizeof(*cmd));
  1272. if (!cmd)
  1273. return status;
  1274. ocrdma_init_mch((struct ocrdma_mbx_hdr *)&cmd->u.cmd[0],
  1275. OCRDMA_CMD_PHY_DETAILS, OCRDMA_SUBSYS_COMMON,
  1276. sizeof(*cmd));
  1277. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  1278. if (status)
  1279. goto mbx_err;
  1280. rsp = (struct ocrdma_get_phy_info_rsp *)cmd;
  1281. dev->phy.phy_type =
  1282. (rsp->ityp_ptyp & OCRDMA_PHY_TYPE_MASK);
  1283. dev->phy.interface_type =
  1284. (rsp->ityp_ptyp & OCRDMA_IF_TYPE_MASK)
  1285. >> OCRDMA_IF_TYPE_SHIFT;
  1286. dev->phy.auto_speeds_supported =
  1287. (rsp->fspeed_aspeed & OCRDMA_ASPEED_SUPP_MASK);
  1288. dev->phy.fixed_speeds_supported =
  1289. (rsp->fspeed_aspeed & OCRDMA_FSPEED_SUPP_MASK)
  1290. >> OCRDMA_FSPEED_SUPP_SHIFT;
  1291. mbx_err:
  1292. kfree(cmd);
  1293. return status;
  1294. }
  1295. int ocrdma_mbx_alloc_pd(struct ocrdma_dev *dev, struct ocrdma_pd *pd)
  1296. {
  1297. int status = -ENOMEM;
  1298. struct ocrdma_alloc_pd *cmd;
  1299. struct ocrdma_alloc_pd_rsp *rsp;
  1300. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_ALLOC_PD, sizeof(*cmd));
  1301. if (!cmd)
  1302. return status;
  1303. if (pd->dpp_enabled)
  1304. cmd->enable_dpp_rsvd |= OCRDMA_ALLOC_PD_ENABLE_DPP;
  1305. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  1306. if (status)
  1307. goto mbx_err;
  1308. rsp = (struct ocrdma_alloc_pd_rsp *)cmd;
  1309. pd->id = rsp->dpp_page_pdid & OCRDMA_ALLOC_PD_RSP_PDID_MASK;
  1310. if (rsp->dpp_page_pdid & OCRDMA_ALLOC_PD_RSP_DPP) {
  1311. pd->dpp_enabled = true;
  1312. pd->dpp_page = rsp->dpp_page_pdid >>
  1313. OCRDMA_ALLOC_PD_RSP_DPP_PAGE_SHIFT;
  1314. } else {
  1315. pd->dpp_enabled = false;
  1316. pd->num_dpp_qp = 0;
  1317. }
  1318. mbx_err:
  1319. kfree(cmd);
  1320. return status;
  1321. }
  1322. int ocrdma_mbx_dealloc_pd(struct ocrdma_dev *dev, struct ocrdma_pd *pd)
  1323. {
  1324. int status = -ENOMEM;
  1325. struct ocrdma_dealloc_pd *cmd;
  1326. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DEALLOC_PD, sizeof(*cmd));
  1327. if (!cmd)
  1328. return status;
  1329. cmd->id = pd->id;
  1330. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  1331. kfree(cmd);
  1332. return status;
  1333. }
  1334. static int ocrdma_mbx_alloc_pd_range(struct ocrdma_dev *dev)
  1335. {
  1336. int status = -ENOMEM;
  1337. size_t pd_bitmap_size;
  1338. struct ocrdma_alloc_pd_range *cmd;
  1339. struct ocrdma_alloc_pd_range_rsp *rsp;
  1340. /* Pre allocate the DPP PDs */
  1341. if (dev->attr.max_dpp_pds) {
  1342. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_ALLOC_PD_RANGE,
  1343. sizeof(*cmd));
  1344. if (!cmd)
  1345. return -ENOMEM;
  1346. cmd->pd_count = dev->attr.max_dpp_pds;
  1347. cmd->enable_dpp_rsvd |= OCRDMA_ALLOC_PD_ENABLE_DPP;
  1348. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  1349. rsp = (struct ocrdma_alloc_pd_range_rsp *)cmd;
  1350. if (!status && (rsp->dpp_page_pdid & OCRDMA_ALLOC_PD_RSP_DPP) &&
  1351. rsp->pd_count) {
  1352. dev->pd_mgr->dpp_page_index = rsp->dpp_page_pdid >>
  1353. OCRDMA_ALLOC_PD_RSP_DPP_PAGE_SHIFT;
  1354. dev->pd_mgr->pd_dpp_start = rsp->dpp_page_pdid &
  1355. OCRDMA_ALLOC_PD_RNG_RSP_START_PDID_MASK;
  1356. dev->pd_mgr->max_dpp_pd = rsp->pd_count;
  1357. pd_bitmap_size =
  1358. BITS_TO_LONGS(rsp->pd_count) * sizeof(long);
  1359. dev->pd_mgr->pd_dpp_bitmap = kzalloc(pd_bitmap_size,
  1360. GFP_KERNEL);
  1361. }
  1362. kfree(cmd);
  1363. }
  1364. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_ALLOC_PD_RANGE, sizeof(*cmd));
  1365. if (!cmd)
  1366. return -ENOMEM;
  1367. cmd->pd_count = dev->attr.max_pd - dev->attr.max_dpp_pds;
  1368. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  1369. rsp = (struct ocrdma_alloc_pd_range_rsp *)cmd;
  1370. if (!status && rsp->pd_count) {
  1371. dev->pd_mgr->pd_norm_start = rsp->dpp_page_pdid &
  1372. OCRDMA_ALLOC_PD_RNG_RSP_START_PDID_MASK;
  1373. dev->pd_mgr->max_normal_pd = rsp->pd_count;
  1374. pd_bitmap_size = BITS_TO_LONGS(rsp->pd_count) * sizeof(long);
  1375. dev->pd_mgr->pd_norm_bitmap = kzalloc(pd_bitmap_size,
  1376. GFP_KERNEL);
  1377. }
  1378. kfree(cmd);
  1379. if (dev->pd_mgr->pd_norm_bitmap || dev->pd_mgr->pd_dpp_bitmap) {
  1380. /* Enable PD resource manager */
  1381. dev->pd_mgr->pd_prealloc_valid = true;
  1382. return 0;
  1383. }
  1384. return status;
  1385. }
  1386. static void ocrdma_mbx_dealloc_pd_range(struct ocrdma_dev *dev)
  1387. {
  1388. struct ocrdma_dealloc_pd_range *cmd;
  1389. /* return normal PDs to firmware */
  1390. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DEALLOC_PD_RANGE, sizeof(*cmd));
  1391. if (!cmd)
  1392. goto mbx_err;
  1393. if (dev->pd_mgr->max_normal_pd) {
  1394. cmd->start_pd_id = dev->pd_mgr->pd_norm_start;
  1395. cmd->pd_count = dev->pd_mgr->max_normal_pd;
  1396. ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  1397. }
  1398. if (dev->pd_mgr->max_dpp_pd) {
  1399. kfree(cmd);
  1400. /* return DPP PDs to firmware */
  1401. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DEALLOC_PD_RANGE,
  1402. sizeof(*cmd));
  1403. if (!cmd)
  1404. goto mbx_err;
  1405. cmd->start_pd_id = dev->pd_mgr->pd_dpp_start;
  1406. cmd->pd_count = dev->pd_mgr->max_dpp_pd;
  1407. ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  1408. }
  1409. mbx_err:
  1410. kfree(cmd);
  1411. }
  1412. void ocrdma_alloc_pd_pool(struct ocrdma_dev *dev)
  1413. {
  1414. int status;
  1415. dev->pd_mgr = kzalloc(sizeof(struct ocrdma_pd_resource_mgr),
  1416. GFP_KERNEL);
  1417. if (!dev->pd_mgr)
  1418. return;
  1419. status = ocrdma_mbx_alloc_pd_range(dev);
  1420. if (status) {
  1421. pr_err("%s(%d) Unable to initialize PD pool, using default.\n",
  1422. __func__, dev->id);
  1423. }
  1424. }
  1425. static void ocrdma_free_pd_pool(struct ocrdma_dev *dev)
  1426. {
  1427. ocrdma_mbx_dealloc_pd_range(dev);
  1428. kfree(dev->pd_mgr->pd_norm_bitmap);
  1429. kfree(dev->pd_mgr->pd_dpp_bitmap);
  1430. kfree(dev->pd_mgr);
  1431. }
  1432. static int ocrdma_build_q_conf(u32 *num_entries, int entry_size,
  1433. int *num_pages, int *page_size)
  1434. {
  1435. int i;
  1436. int mem_size;
  1437. *num_entries = roundup_pow_of_two(*num_entries);
  1438. mem_size = *num_entries * entry_size;
  1439. /* find the possible lowest possible multiplier */
  1440. for (i = 0; i < OCRDMA_MAX_Q_PAGE_SIZE_CNT; i++) {
  1441. if (mem_size <= (OCRDMA_Q_PAGE_BASE_SIZE << i))
  1442. break;
  1443. }
  1444. if (i >= OCRDMA_MAX_Q_PAGE_SIZE_CNT)
  1445. return -EINVAL;
  1446. mem_size = roundup(mem_size,
  1447. ((OCRDMA_Q_PAGE_BASE_SIZE << i) / OCRDMA_MAX_Q_PAGES));
  1448. *num_pages =
  1449. mem_size / ((OCRDMA_Q_PAGE_BASE_SIZE << i) / OCRDMA_MAX_Q_PAGES);
  1450. *page_size = ((OCRDMA_Q_PAGE_BASE_SIZE << i) / OCRDMA_MAX_Q_PAGES);
  1451. *num_entries = mem_size / entry_size;
  1452. return 0;
  1453. }
  1454. static int ocrdma_mbx_create_ah_tbl(struct ocrdma_dev *dev)
  1455. {
  1456. int i;
  1457. int status = -ENOMEM;
  1458. int max_ah;
  1459. struct ocrdma_create_ah_tbl *cmd;
  1460. struct ocrdma_create_ah_tbl_rsp *rsp;
  1461. struct pci_dev *pdev = dev->nic_info.pdev;
  1462. dma_addr_t pa;
  1463. struct ocrdma_pbe *pbes;
  1464. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_AH_TBL, sizeof(*cmd));
  1465. if (!cmd)
  1466. return status;
  1467. max_ah = OCRDMA_MAX_AH;
  1468. dev->av_tbl.size = sizeof(struct ocrdma_av) * max_ah;
  1469. /* number of PBEs in PBL */
  1470. cmd->ah_conf = (OCRDMA_AH_TBL_PAGES <<
  1471. OCRDMA_CREATE_AH_NUM_PAGES_SHIFT) &
  1472. OCRDMA_CREATE_AH_NUM_PAGES_MASK;
  1473. /* page size */
  1474. for (i = 0; i < OCRDMA_MAX_Q_PAGE_SIZE_CNT; i++) {
  1475. if (PAGE_SIZE == (OCRDMA_MIN_Q_PAGE_SIZE << i))
  1476. break;
  1477. }
  1478. cmd->ah_conf |= (i << OCRDMA_CREATE_AH_PAGE_SIZE_SHIFT) &
  1479. OCRDMA_CREATE_AH_PAGE_SIZE_MASK;
  1480. /* ah_entry size */
  1481. cmd->ah_conf |= (sizeof(struct ocrdma_av) <<
  1482. OCRDMA_CREATE_AH_ENTRY_SIZE_SHIFT) &
  1483. OCRDMA_CREATE_AH_ENTRY_SIZE_MASK;
  1484. dev->av_tbl.pbl.va = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
  1485. &dev->av_tbl.pbl.pa,
  1486. GFP_KERNEL);
  1487. if (dev->av_tbl.pbl.va == NULL)
  1488. goto mem_err;
  1489. dev->av_tbl.va = dma_alloc_coherent(&pdev->dev, dev->av_tbl.size,
  1490. &pa, GFP_KERNEL);
  1491. if (dev->av_tbl.va == NULL)
  1492. goto mem_err_ah;
  1493. dev->av_tbl.pa = pa;
  1494. dev->av_tbl.num_ah = max_ah;
  1495. memset(dev->av_tbl.va, 0, dev->av_tbl.size);
  1496. pbes = (struct ocrdma_pbe *)dev->av_tbl.pbl.va;
  1497. for (i = 0; i < dev->av_tbl.size / OCRDMA_MIN_Q_PAGE_SIZE; i++) {
  1498. pbes[i].pa_lo = (u32)cpu_to_le32(pa & 0xffffffff);
  1499. pbes[i].pa_hi = (u32)cpu_to_le32(upper_32_bits(pa));
  1500. pa += PAGE_SIZE;
  1501. }
  1502. cmd->tbl_addr[0].lo = (u32)(dev->av_tbl.pbl.pa & 0xFFFFFFFF);
  1503. cmd->tbl_addr[0].hi = (u32)upper_32_bits(dev->av_tbl.pbl.pa);
  1504. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  1505. if (status)
  1506. goto mbx_err;
  1507. rsp = (struct ocrdma_create_ah_tbl_rsp *)cmd;
  1508. dev->av_tbl.ahid = rsp->ahid & 0xFFFF;
  1509. kfree(cmd);
  1510. return 0;
  1511. mbx_err:
  1512. dma_free_coherent(&pdev->dev, dev->av_tbl.size, dev->av_tbl.va,
  1513. dev->av_tbl.pa);
  1514. dev->av_tbl.va = NULL;
  1515. mem_err_ah:
  1516. dma_free_coherent(&pdev->dev, PAGE_SIZE, dev->av_tbl.pbl.va,
  1517. dev->av_tbl.pbl.pa);
  1518. dev->av_tbl.pbl.va = NULL;
  1519. dev->av_tbl.size = 0;
  1520. mem_err:
  1521. kfree(cmd);
  1522. return status;
  1523. }
  1524. static void ocrdma_mbx_delete_ah_tbl(struct ocrdma_dev *dev)
  1525. {
  1526. struct ocrdma_delete_ah_tbl *cmd;
  1527. struct pci_dev *pdev = dev->nic_info.pdev;
  1528. if (dev->av_tbl.va == NULL)
  1529. return;
  1530. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DELETE_AH_TBL, sizeof(*cmd));
  1531. if (!cmd)
  1532. return;
  1533. cmd->ahid = dev->av_tbl.ahid;
  1534. ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  1535. dma_free_coherent(&pdev->dev, dev->av_tbl.size, dev->av_tbl.va,
  1536. dev->av_tbl.pa);
  1537. dev->av_tbl.va = NULL;
  1538. dma_free_coherent(&pdev->dev, PAGE_SIZE, dev->av_tbl.pbl.va,
  1539. dev->av_tbl.pbl.pa);
  1540. kfree(cmd);
  1541. }
  1542. /* Multiple CQs uses the EQ. This routine returns least used
  1543. * EQ to associate with CQ. This will distributes the interrupt
  1544. * processing and CPU load to associated EQ, vector and so to that CPU.
  1545. */
  1546. static u16 ocrdma_bind_eq(struct ocrdma_dev *dev)
  1547. {
  1548. int i, selected_eq = 0, cq_cnt = 0;
  1549. u16 eq_id;
  1550. mutex_lock(&dev->dev_lock);
  1551. cq_cnt = dev->eq_tbl[0].cq_cnt;
  1552. eq_id = dev->eq_tbl[0].q.id;
  1553. /* find the EQ which is has the least number of
  1554. * CQs associated with it.
  1555. */
  1556. for (i = 0; i < dev->eq_cnt; i++) {
  1557. if (dev->eq_tbl[i].cq_cnt < cq_cnt) {
  1558. cq_cnt = dev->eq_tbl[i].cq_cnt;
  1559. eq_id = dev->eq_tbl[i].q.id;
  1560. selected_eq = i;
  1561. }
  1562. }
  1563. dev->eq_tbl[selected_eq].cq_cnt += 1;
  1564. mutex_unlock(&dev->dev_lock);
  1565. return eq_id;
  1566. }
  1567. static void ocrdma_unbind_eq(struct ocrdma_dev *dev, u16 eq_id)
  1568. {
  1569. int i;
  1570. mutex_lock(&dev->dev_lock);
  1571. i = ocrdma_get_eq_table_index(dev, eq_id);
  1572. if (i == -EINVAL)
  1573. BUG();
  1574. dev->eq_tbl[i].cq_cnt -= 1;
  1575. mutex_unlock(&dev->dev_lock);
  1576. }
  1577. int ocrdma_mbx_create_cq(struct ocrdma_dev *dev, struct ocrdma_cq *cq,
  1578. int entries, int dpp_cq, u16 pd_id)
  1579. {
  1580. int status = -ENOMEM; int max_hw_cqe;
  1581. struct pci_dev *pdev = dev->nic_info.pdev;
  1582. struct ocrdma_create_cq *cmd;
  1583. struct ocrdma_create_cq_rsp *rsp;
  1584. u32 hw_pages, cqe_size, page_size, cqe_count;
  1585. if (entries > dev->attr.max_cqe) {
  1586. pr_err("%s(%d) max_cqe=0x%x, requester_cqe=0x%x\n",
  1587. __func__, dev->id, dev->attr.max_cqe, entries);
  1588. return -EINVAL;
  1589. }
  1590. if (dpp_cq && (ocrdma_get_asic_type(dev) != OCRDMA_ASIC_GEN_SKH_R))
  1591. return -EINVAL;
  1592. if (dpp_cq) {
  1593. cq->max_hw_cqe = 1;
  1594. max_hw_cqe = 1;
  1595. cqe_size = OCRDMA_DPP_CQE_SIZE;
  1596. hw_pages = 1;
  1597. } else {
  1598. cq->max_hw_cqe = dev->attr.max_cqe;
  1599. max_hw_cqe = dev->attr.max_cqe;
  1600. cqe_size = sizeof(struct ocrdma_cqe);
  1601. hw_pages = OCRDMA_CREATE_CQ_MAX_PAGES;
  1602. }
  1603. cq->len = roundup(max_hw_cqe * cqe_size, OCRDMA_MIN_Q_PAGE_SIZE);
  1604. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_CQ, sizeof(*cmd));
  1605. if (!cmd)
  1606. return -ENOMEM;
  1607. ocrdma_init_mch(&cmd->cmd.req, OCRDMA_CMD_CREATE_CQ,
  1608. OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
  1609. cq->va = dma_zalloc_coherent(&pdev->dev, cq->len, &cq->pa, GFP_KERNEL);
  1610. if (!cq->va) {
  1611. status = -ENOMEM;
  1612. goto mem_err;
  1613. }
  1614. page_size = cq->len / hw_pages;
  1615. cmd->cmd.pgsz_pgcnt = (page_size / OCRDMA_MIN_Q_PAGE_SIZE) <<
  1616. OCRDMA_CREATE_CQ_PAGE_SIZE_SHIFT;
  1617. cmd->cmd.pgsz_pgcnt |= hw_pages;
  1618. cmd->cmd.ev_cnt_flags = OCRDMA_CREATE_CQ_DEF_FLAGS;
  1619. cq->eqn = ocrdma_bind_eq(dev);
  1620. cmd->cmd.req.rsvd_version = OCRDMA_CREATE_CQ_VER3;
  1621. cqe_count = cq->len / cqe_size;
  1622. cq->cqe_cnt = cqe_count;
  1623. if (cqe_count > 1024) {
  1624. /* Set cnt to 3 to indicate more than 1024 cq entries */
  1625. cmd->cmd.ev_cnt_flags |= (0x3 << OCRDMA_CREATE_CQ_CNT_SHIFT);
  1626. } else {
  1627. u8 count = 0;
  1628. switch (cqe_count) {
  1629. case 256:
  1630. count = 0;
  1631. break;
  1632. case 512:
  1633. count = 1;
  1634. break;
  1635. case 1024:
  1636. count = 2;
  1637. break;
  1638. default:
  1639. goto mbx_err;
  1640. }
  1641. cmd->cmd.ev_cnt_flags |= (count << OCRDMA_CREATE_CQ_CNT_SHIFT);
  1642. }
  1643. /* shared eq between all the consumer cqs. */
  1644. cmd->cmd.eqn = cq->eqn;
  1645. if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
  1646. if (dpp_cq)
  1647. cmd->cmd.pgsz_pgcnt |= OCRDMA_CREATE_CQ_DPP <<
  1648. OCRDMA_CREATE_CQ_TYPE_SHIFT;
  1649. cq->phase_change = false;
  1650. cmd->cmd.pdid_cqecnt = (cq->len / cqe_size);
  1651. } else {
  1652. cmd->cmd.pdid_cqecnt = (cq->len / cqe_size) - 1;
  1653. cmd->cmd.ev_cnt_flags |= OCRDMA_CREATE_CQ_FLAGS_AUTO_VALID;
  1654. cq->phase_change = true;
  1655. }
  1656. /* pd_id valid only for v3 */
  1657. cmd->cmd.pdid_cqecnt |= (pd_id <<
  1658. OCRDMA_CREATE_CQ_CMD_PDID_SHIFT);
  1659. ocrdma_build_q_pages(&cmd->cmd.pa[0], hw_pages, cq->pa, page_size);
  1660. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  1661. if (status)
  1662. goto mbx_err;
  1663. rsp = (struct ocrdma_create_cq_rsp *)cmd;
  1664. cq->id = (u16) (rsp->rsp.cq_id & OCRDMA_CREATE_CQ_RSP_CQ_ID_MASK);
  1665. kfree(cmd);
  1666. return 0;
  1667. mbx_err:
  1668. ocrdma_unbind_eq(dev, cq->eqn);
  1669. dma_free_coherent(&pdev->dev, cq->len, cq->va, cq->pa);
  1670. mem_err:
  1671. kfree(cmd);
  1672. return status;
  1673. }
  1674. int ocrdma_mbx_destroy_cq(struct ocrdma_dev *dev, struct ocrdma_cq *cq)
  1675. {
  1676. int status = -ENOMEM;
  1677. struct ocrdma_destroy_cq *cmd;
  1678. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DELETE_CQ, sizeof(*cmd));
  1679. if (!cmd)
  1680. return status;
  1681. ocrdma_init_mch(&cmd->req, OCRDMA_CMD_DELETE_CQ,
  1682. OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
  1683. cmd->bypass_flush_qid |=
  1684. (cq->id << OCRDMA_DESTROY_CQ_QID_SHIFT) &
  1685. OCRDMA_DESTROY_CQ_QID_MASK;
  1686. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  1687. ocrdma_unbind_eq(dev, cq->eqn);
  1688. dma_free_coherent(&dev->nic_info.pdev->dev, cq->len, cq->va, cq->pa);
  1689. kfree(cmd);
  1690. return status;
  1691. }
  1692. int ocrdma_mbx_alloc_lkey(struct ocrdma_dev *dev, struct ocrdma_hw_mr *hwmr,
  1693. u32 pdid, int addr_check)
  1694. {
  1695. int status = -ENOMEM;
  1696. struct ocrdma_alloc_lkey *cmd;
  1697. struct ocrdma_alloc_lkey_rsp *rsp;
  1698. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_ALLOC_LKEY, sizeof(*cmd));
  1699. if (!cmd)
  1700. return status;
  1701. cmd->pdid = pdid;
  1702. cmd->pbl_sz_flags |= addr_check;
  1703. cmd->pbl_sz_flags |= (hwmr->fr_mr << OCRDMA_ALLOC_LKEY_FMR_SHIFT);
  1704. cmd->pbl_sz_flags |=
  1705. (hwmr->remote_wr << OCRDMA_ALLOC_LKEY_REMOTE_WR_SHIFT);
  1706. cmd->pbl_sz_flags |=
  1707. (hwmr->remote_rd << OCRDMA_ALLOC_LKEY_REMOTE_RD_SHIFT);
  1708. cmd->pbl_sz_flags |=
  1709. (hwmr->local_wr << OCRDMA_ALLOC_LKEY_LOCAL_WR_SHIFT);
  1710. cmd->pbl_sz_flags |=
  1711. (hwmr->remote_atomic << OCRDMA_ALLOC_LKEY_REMOTE_ATOMIC_SHIFT);
  1712. cmd->pbl_sz_flags |=
  1713. (hwmr->num_pbls << OCRDMA_ALLOC_LKEY_PBL_SIZE_SHIFT);
  1714. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  1715. if (status)
  1716. goto mbx_err;
  1717. rsp = (struct ocrdma_alloc_lkey_rsp *)cmd;
  1718. hwmr->lkey = rsp->lrkey;
  1719. mbx_err:
  1720. kfree(cmd);
  1721. return status;
  1722. }
  1723. int ocrdma_mbx_dealloc_lkey(struct ocrdma_dev *dev, int fr_mr, u32 lkey)
  1724. {
  1725. int status;
  1726. struct ocrdma_dealloc_lkey *cmd;
  1727. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DEALLOC_LKEY, sizeof(*cmd));
  1728. if (!cmd)
  1729. return -ENOMEM;
  1730. cmd->lkey = lkey;
  1731. cmd->rsvd_frmr = fr_mr ? 1 : 0;
  1732. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  1733. kfree(cmd);
  1734. return status;
  1735. }
  1736. static int ocrdma_mbx_reg_mr(struct ocrdma_dev *dev, struct ocrdma_hw_mr *hwmr,
  1737. u32 pdid, u32 pbl_cnt, u32 pbe_size, u32 last)
  1738. {
  1739. int status = -ENOMEM;
  1740. int i;
  1741. struct ocrdma_reg_nsmr *cmd;
  1742. struct ocrdma_reg_nsmr_rsp *rsp;
  1743. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_REGISTER_NSMR, sizeof(*cmd));
  1744. if (!cmd)
  1745. return -ENOMEM;
  1746. cmd->num_pbl_pdid =
  1747. pdid | (hwmr->num_pbls << OCRDMA_REG_NSMR_NUM_PBL_SHIFT);
  1748. cmd->fr_mr = hwmr->fr_mr;
  1749. cmd->flags_hpage_pbe_sz |= (hwmr->remote_wr <<
  1750. OCRDMA_REG_NSMR_REMOTE_WR_SHIFT);
  1751. cmd->flags_hpage_pbe_sz |= (hwmr->remote_rd <<
  1752. OCRDMA_REG_NSMR_REMOTE_RD_SHIFT);
  1753. cmd->flags_hpage_pbe_sz |= (hwmr->local_wr <<
  1754. OCRDMA_REG_NSMR_LOCAL_WR_SHIFT);
  1755. cmd->flags_hpage_pbe_sz |= (hwmr->remote_atomic <<
  1756. OCRDMA_REG_NSMR_REMOTE_ATOMIC_SHIFT);
  1757. cmd->flags_hpage_pbe_sz |= (hwmr->mw_bind <<
  1758. OCRDMA_REG_NSMR_BIND_MEMWIN_SHIFT);
  1759. cmd->flags_hpage_pbe_sz |= (last << OCRDMA_REG_NSMR_LAST_SHIFT);
  1760. cmd->flags_hpage_pbe_sz |= (hwmr->pbe_size / OCRDMA_MIN_HPAGE_SIZE);
  1761. cmd->flags_hpage_pbe_sz |= (hwmr->pbl_size / OCRDMA_MIN_HPAGE_SIZE) <<
  1762. OCRDMA_REG_NSMR_HPAGE_SIZE_SHIFT;
  1763. cmd->totlen_low = hwmr->len;
  1764. cmd->totlen_high = upper_32_bits(hwmr->len);
  1765. cmd->fbo_low = (u32) (hwmr->fbo & 0xffffffff);
  1766. cmd->fbo_high = (u32) upper_32_bits(hwmr->fbo);
  1767. cmd->va_loaddr = (u32) hwmr->va;
  1768. cmd->va_hiaddr = (u32) upper_32_bits(hwmr->va);
  1769. for (i = 0; i < pbl_cnt; i++) {
  1770. cmd->pbl[i].lo = (u32) (hwmr->pbl_table[i].pa & 0xffffffff);
  1771. cmd->pbl[i].hi = upper_32_bits(hwmr->pbl_table[i].pa);
  1772. }
  1773. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  1774. if (status)
  1775. goto mbx_err;
  1776. rsp = (struct ocrdma_reg_nsmr_rsp *)cmd;
  1777. hwmr->lkey = rsp->lrkey;
  1778. mbx_err:
  1779. kfree(cmd);
  1780. return status;
  1781. }
  1782. static int ocrdma_mbx_reg_mr_cont(struct ocrdma_dev *dev,
  1783. struct ocrdma_hw_mr *hwmr, u32 pbl_cnt,
  1784. u32 pbl_offset, u32 last)
  1785. {
  1786. int status;
  1787. int i;
  1788. struct ocrdma_reg_nsmr_cont *cmd;
  1789. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_REGISTER_NSMR_CONT, sizeof(*cmd));
  1790. if (!cmd)
  1791. return -ENOMEM;
  1792. cmd->lrkey = hwmr->lkey;
  1793. cmd->num_pbl_offset = (pbl_cnt << OCRDMA_REG_NSMR_CONT_NUM_PBL_SHIFT) |
  1794. (pbl_offset & OCRDMA_REG_NSMR_CONT_PBL_SHIFT_MASK);
  1795. cmd->last = last << OCRDMA_REG_NSMR_CONT_LAST_SHIFT;
  1796. for (i = 0; i < pbl_cnt; i++) {
  1797. cmd->pbl[i].lo =
  1798. (u32) (hwmr->pbl_table[i + pbl_offset].pa & 0xffffffff);
  1799. cmd->pbl[i].hi =
  1800. upper_32_bits(hwmr->pbl_table[i + pbl_offset].pa);
  1801. }
  1802. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  1803. kfree(cmd);
  1804. return status;
  1805. }
  1806. int ocrdma_reg_mr(struct ocrdma_dev *dev,
  1807. struct ocrdma_hw_mr *hwmr, u32 pdid, int acc)
  1808. {
  1809. int status;
  1810. u32 last = 0;
  1811. u32 cur_pbl_cnt, pbl_offset;
  1812. u32 pending_pbl_cnt = hwmr->num_pbls;
  1813. pbl_offset = 0;
  1814. cur_pbl_cnt = min(pending_pbl_cnt, MAX_OCRDMA_NSMR_PBL);
  1815. if (cur_pbl_cnt == pending_pbl_cnt)
  1816. last = 1;
  1817. status = ocrdma_mbx_reg_mr(dev, hwmr, pdid,
  1818. cur_pbl_cnt, hwmr->pbe_size, last);
  1819. if (status) {
  1820. pr_err("%s() status=%d\n", __func__, status);
  1821. return status;
  1822. }
  1823. /* if there is no more pbls to register then exit. */
  1824. if (last)
  1825. return 0;
  1826. while (!last) {
  1827. pbl_offset += cur_pbl_cnt;
  1828. pending_pbl_cnt -= cur_pbl_cnt;
  1829. cur_pbl_cnt = min(pending_pbl_cnt, MAX_OCRDMA_NSMR_PBL);
  1830. /* if we reach the end of the pbls, then need to set the last
  1831. * bit, indicating no more pbls to register for this memory key.
  1832. */
  1833. if (cur_pbl_cnt == pending_pbl_cnt)
  1834. last = 1;
  1835. status = ocrdma_mbx_reg_mr_cont(dev, hwmr, cur_pbl_cnt,
  1836. pbl_offset, last);
  1837. if (status)
  1838. break;
  1839. }
  1840. if (status)
  1841. pr_err("%s() err. status=%d\n", __func__, status);
  1842. return status;
  1843. }
  1844. bool ocrdma_is_qp_in_sq_flushlist(struct ocrdma_cq *cq, struct ocrdma_qp *qp)
  1845. {
  1846. struct ocrdma_qp *tmp;
  1847. bool found = false;
  1848. list_for_each_entry(tmp, &cq->sq_head, sq_entry) {
  1849. if (qp == tmp) {
  1850. found = true;
  1851. break;
  1852. }
  1853. }
  1854. return found;
  1855. }
  1856. bool ocrdma_is_qp_in_rq_flushlist(struct ocrdma_cq *cq, struct ocrdma_qp *qp)
  1857. {
  1858. struct ocrdma_qp *tmp;
  1859. bool found = false;
  1860. list_for_each_entry(tmp, &cq->rq_head, rq_entry) {
  1861. if (qp == tmp) {
  1862. found = true;
  1863. break;
  1864. }
  1865. }
  1866. return found;
  1867. }
  1868. void ocrdma_flush_qp(struct ocrdma_qp *qp)
  1869. {
  1870. bool found;
  1871. unsigned long flags;
  1872. struct ocrdma_dev *dev = get_ocrdma_dev(qp->ibqp.device);
  1873. spin_lock_irqsave(&dev->flush_q_lock, flags);
  1874. found = ocrdma_is_qp_in_sq_flushlist(qp->sq_cq, qp);
  1875. if (!found)
  1876. list_add_tail(&qp->sq_entry, &qp->sq_cq->sq_head);
  1877. if (!qp->srq) {
  1878. found = ocrdma_is_qp_in_rq_flushlist(qp->rq_cq, qp);
  1879. if (!found)
  1880. list_add_tail(&qp->rq_entry, &qp->rq_cq->rq_head);
  1881. }
  1882. spin_unlock_irqrestore(&dev->flush_q_lock, flags);
  1883. }
  1884. static void ocrdma_init_hwq_ptr(struct ocrdma_qp *qp)
  1885. {
  1886. qp->sq.head = 0;
  1887. qp->sq.tail = 0;
  1888. qp->rq.head = 0;
  1889. qp->rq.tail = 0;
  1890. }
  1891. int ocrdma_qp_state_change(struct ocrdma_qp *qp, enum ib_qp_state new_ib_state,
  1892. enum ib_qp_state *old_ib_state)
  1893. {
  1894. unsigned long flags;
  1895. enum ocrdma_qp_state new_state;
  1896. new_state = get_ocrdma_qp_state(new_ib_state);
  1897. /* sync with wqe and rqe posting */
  1898. spin_lock_irqsave(&qp->q_lock, flags);
  1899. if (old_ib_state)
  1900. *old_ib_state = get_ibqp_state(qp->state);
  1901. if (new_state == qp->state) {
  1902. spin_unlock_irqrestore(&qp->q_lock, flags);
  1903. return 1;
  1904. }
  1905. if (new_state == OCRDMA_QPS_INIT) {
  1906. ocrdma_init_hwq_ptr(qp);
  1907. ocrdma_del_flush_qp(qp);
  1908. } else if (new_state == OCRDMA_QPS_ERR) {
  1909. ocrdma_flush_qp(qp);
  1910. }
  1911. qp->state = new_state;
  1912. spin_unlock_irqrestore(&qp->q_lock, flags);
  1913. return 0;
  1914. }
  1915. static u32 ocrdma_set_create_qp_mbx_access_flags(struct ocrdma_qp *qp)
  1916. {
  1917. u32 flags = 0;
  1918. if (qp->cap_flags & OCRDMA_QP_INB_RD)
  1919. flags |= OCRDMA_CREATE_QP_REQ_INB_RDEN_MASK;
  1920. if (qp->cap_flags & OCRDMA_QP_INB_WR)
  1921. flags |= OCRDMA_CREATE_QP_REQ_INB_WREN_MASK;
  1922. if (qp->cap_flags & OCRDMA_QP_MW_BIND)
  1923. flags |= OCRDMA_CREATE_QP_REQ_BIND_MEMWIN_MASK;
  1924. if (qp->cap_flags & OCRDMA_QP_LKEY0)
  1925. flags |= OCRDMA_CREATE_QP_REQ_ZERO_LKEYEN_MASK;
  1926. if (qp->cap_flags & OCRDMA_QP_FAST_REG)
  1927. flags |= OCRDMA_CREATE_QP_REQ_FMR_EN_MASK;
  1928. return flags;
  1929. }
  1930. static int ocrdma_set_create_qp_sq_cmd(struct ocrdma_create_qp_req *cmd,
  1931. struct ib_qp_init_attr *attrs,
  1932. struct ocrdma_qp *qp)
  1933. {
  1934. int status;
  1935. u32 len, hw_pages, hw_page_size;
  1936. dma_addr_t pa;
  1937. struct ocrdma_pd *pd = qp->pd;
  1938. struct ocrdma_dev *dev = get_ocrdma_dev(pd->ibpd.device);
  1939. struct pci_dev *pdev = dev->nic_info.pdev;
  1940. u32 max_wqe_allocated;
  1941. u32 max_sges = attrs->cap.max_send_sge;
  1942. /* QP1 may exceed 127 */
  1943. max_wqe_allocated = min_t(u32, attrs->cap.max_send_wr + 1,
  1944. dev->attr.max_wqe);
  1945. status = ocrdma_build_q_conf(&max_wqe_allocated,
  1946. dev->attr.wqe_size, &hw_pages, &hw_page_size);
  1947. if (status) {
  1948. pr_err("%s() req. max_send_wr=0x%x\n", __func__,
  1949. max_wqe_allocated);
  1950. return -EINVAL;
  1951. }
  1952. qp->sq.max_cnt = max_wqe_allocated;
  1953. len = (hw_pages * hw_page_size);
  1954. qp->sq.va = dma_zalloc_coherent(&pdev->dev, len, &pa, GFP_KERNEL);
  1955. if (!qp->sq.va)
  1956. return -EINVAL;
  1957. qp->sq.len = len;
  1958. qp->sq.pa = pa;
  1959. qp->sq.entry_size = dev->attr.wqe_size;
  1960. ocrdma_build_q_pages(&cmd->wq_addr[0], hw_pages, pa, hw_page_size);
  1961. cmd->type_pgsz_pdn |= (ilog2(hw_page_size / OCRDMA_MIN_Q_PAGE_SIZE)
  1962. << OCRDMA_CREATE_QP_REQ_SQ_PAGE_SIZE_SHIFT);
  1963. cmd->num_wq_rq_pages |= (hw_pages <<
  1964. OCRDMA_CREATE_QP_REQ_NUM_WQ_PAGES_SHIFT) &
  1965. OCRDMA_CREATE_QP_REQ_NUM_WQ_PAGES_MASK;
  1966. cmd->max_sge_send_write |= (max_sges <<
  1967. OCRDMA_CREATE_QP_REQ_MAX_SGE_SEND_SHIFT) &
  1968. OCRDMA_CREATE_QP_REQ_MAX_SGE_SEND_MASK;
  1969. cmd->max_sge_send_write |= (max_sges <<
  1970. OCRDMA_CREATE_QP_REQ_MAX_SGE_WRITE_SHIFT) &
  1971. OCRDMA_CREATE_QP_REQ_MAX_SGE_WRITE_MASK;
  1972. cmd->max_wqe_rqe |= (ilog2(qp->sq.max_cnt) <<
  1973. OCRDMA_CREATE_QP_REQ_MAX_WQE_SHIFT) &
  1974. OCRDMA_CREATE_QP_REQ_MAX_WQE_MASK;
  1975. cmd->wqe_rqe_size |= (dev->attr.wqe_size <<
  1976. OCRDMA_CREATE_QP_REQ_WQE_SIZE_SHIFT) &
  1977. OCRDMA_CREATE_QP_REQ_WQE_SIZE_MASK;
  1978. return 0;
  1979. }
  1980. static int ocrdma_set_create_qp_rq_cmd(struct ocrdma_create_qp_req *cmd,
  1981. struct ib_qp_init_attr *attrs,
  1982. struct ocrdma_qp *qp)
  1983. {
  1984. int status;
  1985. u32 len, hw_pages, hw_page_size;
  1986. dma_addr_t pa = 0;
  1987. struct ocrdma_pd *pd = qp->pd;
  1988. struct ocrdma_dev *dev = get_ocrdma_dev(pd->ibpd.device);
  1989. struct pci_dev *pdev = dev->nic_info.pdev;
  1990. u32 max_rqe_allocated = attrs->cap.max_recv_wr + 1;
  1991. status = ocrdma_build_q_conf(&max_rqe_allocated, dev->attr.rqe_size,
  1992. &hw_pages, &hw_page_size);
  1993. if (status) {
  1994. pr_err("%s() req. max_recv_wr=0x%x\n", __func__,
  1995. attrs->cap.max_recv_wr + 1);
  1996. return status;
  1997. }
  1998. qp->rq.max_cnt = max_rqe_allocated;
  1999. len = (hw_pages * hw_page_size);
  2000. qp->rq.va = dma_zalloc_coherent(&pdev->dev, len, &pa, GFP_KERNEL);
  2001. if (!qp->rq.va)
  2002. return -ENOMEM;
  2003. qp->rq.pa = pa;
  2004. qp->rq.len = len;
  2005. qp->rq.entry_size = dev->attr.rqe_size;
  2006. ocrdma_build_q_pages(&cmd->rq_addr[0], hw_pages, pa, hw_page_size);
  2007. cmd->type_pgsz_pdn |= (ilog2(hw_page_size / OCRDMA_MIN_Q_PAGE_SIZE) <<
  2008. OCRDMA_CREATE_QP_REQ_RQ_PAGE_SIZE_SHIFT);
  2009. cmd->num_wq_rq_pages |=
  2010. (hw_pages << OCRDMA_CREATE_QP_REQ_NUM_RQ_PAGES_SHIFT) &
  2011. OCRDMA_CREATE_QP_REQ_NUM_RQ_PAGES_MASK;
  2012. cmd->max_sge_recv_flags |= (attrs->cap.max_recv_sge <<
  2013. OCRDMA_CREATE_QP_REQ_MAX_SGE_RECV_SHIFT) &
  2014. OCRDMA_CREATE_QP_REQ_MAX_SGE_RECV_MASK;
  2015. cmd->max_wqe_rqe |= (ilog2(qp->rq.max_cnt) <<
  2016. OCRDMA_CREATE_QP_REQ_MAX_RQE_SHIFT) &
  2017. OCRDMA_CREATE_QP_REQ_MAX_RQE_MASK;
  2018. cmd->wqe_rqe_size |= (dev->attr.rqe_size <<
  2019. OCRDMA_CREATE_QP_REQ_RQE_SIZE_SHIFT) &
  2020. OCRDMA_CREATE_QP_REQ_RQE_SIZE_MASK;
  2021. return 0;
  2022. }
  2023. static void ocrdma_set_create_qp_dpp_cmd(struct ocrdma_create_qp_req *cmd,
  2024. struct ocrdma_pd *pd,
  2025. struct ocrdma_qp *qp,
  2026. u8 enable_dpp_cq, u16 dpp_cq_id)
  2027. {
  2028. pd->num_dpp_qp--;
  2029. qp->dpp_enabled = true;
  2030. cmd->max_sge_recv_flags |= OCRDMA_CREATE_QP_REQ_ENABLE_DPP_MASK;
  2031. if (!enable_dpp_cq)
  2032. return;
  2033. cmd->max_sge_recv_flags |= OCRDMA_CREATE_QP_REQ_ENABLE_DPP_MASK;
  2034. cmd->dpp_credits_cqid = dpp_cq_id;
  2035. cmd->dpp_credits_cqid |= OCRDMA_CREATE_QP_REQ_DPP_CREDIT_LIMIT <<
  2036. OCRDMA_CREATE_QP_REQ_DPP_CREDIT_SHIFT;
  2037. }
  2038. static int ocrdma_set_create_qp_ird_cmd(struct ocrdma_create_qp_req *cmd,
  2039. struct ocrdma_qp *qp)
  2040. {
  2041. struct ocrdma_pd *pd = qp->pd;
  2042. struct ocrdma_dev *dev = get_ocrdma_dev(pd->ibpd.device);
  2043. struct pci_dev *pdev = dev->nic_info.pdev;
  2044. dma_addr_t pa = 0;
  2045. int ird_page_size = dev->attr.ird_page_size;
  2046. int ird_q_len = dev->attr.num_ird_pages * ird_page_size;
  2047. struct ocrdma_hdr_wqe *rqe;
  2048. int i = 0;
  2049. if (dev->attr.ird == 0)
  2050. return 0;
  2051. qp->ird_q_va = dma_zalloc_coherent(&pdev->dev, ird_q_len, &pa,
  2052. GFP_KERNEL);
  2053. if (!qp->ird_q_va)
  2054. return -ENOMEM;
  2055. ocrdma_build_q_pages(&cmd->ird_addr[0], dev->attr.num_ird_pages,
  2056. pa, ird_page_size);
  2057. for (; i < ird_q_len / dev->attr.rqe_size; i++) {
  2058. rqe = (struct ocrdma_hdr_wqe *)(qp->ird_q_va +
  2059. (i * dev->attr.rqe_size));
  2060. rqe->cw = 0;
  2061. rqe->cw |= 2;
  2062. rqe->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
  2063. rqe->cw |= (8 << OCRDMA_WQE_SIZE_SHIFT);
  2064. rqe->cw |= (8 << OCRDMA_WQE_NXT_WQE_SIZE_SHIFT);
  2065. }
  2066. return 0;
  2067. }
  2068. static void ocrdma_get_create_qp_rsp(struct ocrdma_create_qp_rsp *rsp,
  2069. struct ocrdma_qp *qp,
  2070. struct ib_qp_init_attr *attrs,
  2071. u16 *dpp_offset, u16 *dpp_credit_lmt)
  2072. {
  2073. u32 max_wqe_allocated, max_rqe_allocated;
  2074. qp->id = rsp->qp_id & OCRDMA_CREATE_QP_RSP_QP_ID_MASK;
  2075. qp->rq.dbid = rsp->sq_rq_id & OCRDMA_CREATE_QP_RSP_RQ_ID_MASK;
  2076. qp->sq.dbid = rsp->sq_rq_id >> OCRDMA_CREATE_QP_RSP_SQ_ID_SHIFT;
  2077. qp->max_ird = rsp->max_ord_ird & OCRDMA_CREATE_QP_RSP_MAX_IRD_MASK;
  2078. qp->max_ord = (rsp->max_ord_ird >> OCRDMA_CREATE_QP_RSP_MAX_ORD_SHIFT);
  2079. qp->dpp_enabled = false;
  2080. if (rsp->dpp_response & OCRDMA_CREATE_QP_RSP_DPP_ENABLED_MASK) {
  2081. qp->dpp_enabled = true;
  2082. *dpp_credit_lmt = (rsp->dpp_response &
  2083. OCRDMA_CREATE_QP_RSP_DPP_CREDITS_MASK) >>
  2084. OCRDMA_CREATE_QP_RSP_DPP_CREDITS_SHIFT;
  2085. *dpp_offset = (rsp->dpp_response &
  2086. OCRDMA_CREATE_QP_RSP_DPP_PAGE_OFFSET_MASK) >>
  2087. OCRDMA_CREATE_QP_RSP_DPP_PAGE_OFFSET_SHIFT;
  2088. }
  2089. max_wqe_allocated =
  2090. rsp->max_wqe_rqe >> OCRDMA_CREATE_QP_RSP_MAX_WQE_SHIFT;
  2091. max_wqe_allocated = 1 << max_wqe_allocated;
  2092. max_rqe_allocated = 1 << ((u16)rsp->max_wqe_rqe);
  2093. qp->sq.max_cnt = max_wqe_allocated;
  2094. qp->sq.max_wqe_idx = max_wqe_allocated - 1;
  2095. if (!attrs->srq) {
  2096. qp->rq.max_cnt = max_rqe_allocated;
  2097. qp->rq.max_wqe_idx = max_rqe_allocated - 1;
  2098. }
  2099. }
  2100. int ocrdma_mbx_create_qp(struct ocrdma_qp *qp, struct ib_qp_init_attr *attrs,
  2101. u8 enable_dpp_cq, u16 dpp_cq_id, u16 *dpp_offset,
  2102. u16 *dpp_credit_lmt)
  2103. {
  2104. int status = -ENOMEM;
  2105. u32 flags = 0;
  2106. struct ocrdma_pd *pd = qp->pd;
  2107. struct ocrdma_dev *dev = get_ocrdma_dev(pd->ibpd.device);
  2108. struct pci_dev *pdev = dev->nic_info.pdev;
  2109. struct ocrdma_cq *cq;
  2110. struct ocrdma_create_qp_req *cmd;
  2111. struct ocrdma_create_qp_rsp *rsp;
  2112. int qptype;
  2113. switch (attrs->qp_type) {
  2114. case IB_QPT_GSI:
  2115. qptype = OCRDMA_QPT_GSI;
  2116. break;
  2117. case IB_QPT_RC:
  2118. qptype = OCRDMA_QPT_RC;
  2119. break;
  2120. case IB_QPT_UD:
  2121. qptype = OCRDMA_QPT_UD;
  2122. break;
  2123. default:
  2124. return -EINVAL;
  2125. }
  2126. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_QP, sizeof(*cmd));
  2127. if (!cmd)
  2128. return status;
  2129. cmd->type_pgsz_pdn |= (qptype << OCRDMA_CREATE_QP_REQ_QPT_SHIFT) &
  2130. OCRDMA_CREATE_QP_REQ_QPT_MASK;
  2131. status = ocrdma_set_create_qp_sq_cmd(cmd, attrs, qp);
  2132. if (status)
  2133. goto sq_err;
  2134. if (attrs->srq) {
  2135. struct ocrdma_srq *srq = get_ocrdma_srq(attrs->srq);
  2136. cmd->max_sge_recv_flags |= OCRDMA_CREATE_QP_REQ_USE_SRQ_MASK;
  2137. cmd->rq_addr[0].lo = srq->id;
  2138. qp->srq = srq;
  2139. } else {
  2140. status = ocrdma_set_create_qp_rq_cmd(cmd, attrs, qp);
  2141. if (status)
  2142. goto rq_err;
  2143. }
  2144. status = ocrdma_set_create_qp_ird_cmd(cmd, qp);
  2145. if (status)
  2146. goto mbx_err;
  2147. cmd->type_pgsz_pdn |= (pd->id << OCRDMA_CREATE_QP_REQ_PD_ID_SHIFT) &
  2148. OCRDMA_CREATE_QP_REQ_PD_ID_MASK;
  2149. flags = ocrdma_set_create_qp_mbx_access_flags(qp);
  2150. cmd->max_sge_recv_flags |= flags;
  2151. cmd->max_ord_ird |= (dev->attr.max_ord_per_qp <<
  2152. OCRDMA_CREATE_QP_REQ_MAX_ORD_SHIFT) &
  2153. OCRDMA_CREATE_QP_REQ_MAX_ORD_MASK;
  2154. cmd->max_ord_ird |= (dev->attr.max_ird_per_qp <<
  2155. OCRDMA_CREATE_QP_REQ_MAX_IRD_SHIFT) &
  2156. OCRDMA_CREATE_QP_REQ_MAX_IRD_MASK;
  2157. cq = get_ocrdma_cq(attrs->send_cq);
  2158. cmd->wq_rq_cqid |= (cq->id << OCRDMA_CREATE_QP_REQ_WQ_CQID_SHIFT) &
  2159. OCRDMA_CREATE_QP_REQ_WQ_CQID_MASK;
  2160. qp->sq_cq = cq;
  2161. cq = get_ocrdma_cq(attrs->recv_cq);
  2162. cmd->wq_rq_cqid |= (cq->id << OCRDMA_CREATE_QP_REQ_RQ_CQID_SHIFT) &
  2163. OCRDMA_CREATE_QP_REQ_RQ_CQID_MASK;
  2164. qp->rq_cq = cq;
  2165. if (pd->dpp_enabled && attrs->cap.max_inline_data && pd->num_dpp_qp &&
  2166. (attrs->cap.max_inline_data <= dev->attr.max_inline_data)) {
  2167. ocrdma_set_create_qp_dpp_cmd(cmd, pd, qp, enable_dpp_cq,
  2168. dpp_cq_id);
  2169. }
  2170. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  2171. if (status)
  2172. goto mbx_err;
  2173. rsp = (struct ocrdma_create_qp_rsp *)cmd;
  2174. ocrdma_get_create_qp_rsp(rsp, qp, attrs, dpp_offset, dpp_credit_lmt);
  2175. qp->state = OCRDMA_QPS_RST;
  2176. kfree(cmd);
  2177. return 0;
  2178. mbx_err:
  2179. if (qp->rq.va)
  2180. dma_free_coherent(&pdev->dev, qp->rq.len, qp->rq.va, qp->rq.pa);
  2181. rq_err:
  2182. pr_err("%s(%d) rq_err\n", __func__, dev->id);
  2183. dma_free_coherent(&pdev->dev, qp->sq.len, qp->sq.va, qp->sq.pa);
  2184. sq_err:
  2185. pr_err("%s(%d) sq_err\n", __func__, dev->id);
  2186. kfree(cmd);
  2187. return status;
  2188. }
  2189. int ocrdma_mbx_query_qp(struct ocrdma_dev *dev, struct ocrdma_qp *qp,
  2190. struct ocrdma_qp_params *param)
  2191. {
  2192. int status = -ENOMEM;
  2193. struct ocrdma_query_qp *cmd;
  2194. struct ocrdma_query_qp_rsp *rsp;
  2195. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_QUERY_QP, sizeof(*rsp));
  2196. if (!cmd)
  2197. return status;
  2198. cmd->qp_id = qp->id;
  2199. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  2200. if (status)
  2201. goto mbx_err;
  2202. rsp = (struct ocrdma_query_qp_rsp *)cmd;
  2203. memcpy(param, &rsp->params, sizeof(struct ocrdma_qp_params));
  2204. mbx_err:
  2205. kfree(cmd);
  2206. return status;
  2207. }
  2208. static int ocrdma_set_av_params(struct ocrdma_qp *qp,
  2209. struct ocrdma_modify_qp *cmd,
  2210. struct ib_qp_attr *attrs,
  2211. int attr_mask)
  2212. {
  2213. int status;
  2214. struct rdma_ah_attr *ah_attr = &attrs->ah_attr;
  2215. const struct ib_gid_attr *sgid_attr;
  2216. u32 vlan_id = 0xFFFF;
  2217. u8 mac_addr[6], hdr_type;
  2218. union {
  2219. struct sockaddr_in _sockaddr_in;
  2220. struct sockaddr_in6 _sockaddr_in6;
  2221. } sgid_addr, dgid_addr;
  2222. struct ocrdma_dev *dev = get_ocrdma_dev(qp->ibqp.device);
  2223. const struct ib_global_route *grh;
  2224. if ((rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH) == 0)
  2225. return -EINVAL;
  2226. grh = rdma_ah_read_grh(ah_attr);
  2227. if (atomic_cmpxchg(&dev->update_sl, 1, 0))
  2228. ocrdma_init_service_level(dev);
  2229. cmd->params.tclass_sq_psn |=
  2230. (grh->traffic_class << OCRDMA_QP_PARAMS_TCLASS_SHIFT);
  2231. cmd->params.rnt_rc_sl_fl |=
  2232. (grh->flow_label & OCRDMA_QP_PARAMS_FLOW_LABEL_MASK);
  2233. cmd->params.rnt_rc_sl_fl |= (rdma_ah_get_sl(ah_attr) <<
  2234. OCRDMA_QP_PARAMS_SL_SHIFT);
  2235. cmd->params.hop_lmt_rq_psn |=
  2236. (grh->hop_limit << OCRDMA_QP_PARAMS_HOP_LMT_SHIFT);
  2237. cmd->flags |= OCRDMA_QP_PARA_FLOW_LBL_VALID;
  2238. /* GIDs */
  2239. memcpy(&cmd->params.dgid[0], &grh->dgid.raw[0],
  2240. sizeof(cmd->params.dgid));
  2241. sgid_attr = ah_attr->grh.sgid_attr;
  2242. vlan_id = rdma_vlan_dev_vlan_id(sgid_attr->ndev);
  2243. memcpy(mac_addr, sgid_attr->ndev->dev_addr, ETH_ALEN);
  2244. qp->sgid_idx = grh->sgid_index;
  2245. memcpy(&cmd->params.sgid[0], &sgid_attr->gid.raw[0],
  2246. sizeof(cmd->params.sgid));
  2247. status = ocrdma_resolve_dmac(dev, ah_attr, &mac_addr[0]);
  2248. if (status)
  2249. return status;
  2250. cmd->params.dmac_b0_to_b3 = mac_addr[0] | (mac_addr[1] << 8) |
  2251. (mac_addr[2] << 16) | (mac_addr[3] << 24);
  2252. hdr_type = rdma_gid_attr_network_type(sgid_attr);
  2253. if (hdr_type == RDMA_NETWORK_IPV4) {
  2254. rdma_gid2ip((struct sockaddr *)&sgid_addr, &sgid_attr->gid);
  2255. rdma_gid2ip((struct sockaddr *)&dgid_addr, &grh->dgid);
  2256. memcpy(&cmd->params.dgid[0],
  2257. &dgid_addr._sockaddr_in.sin_addr.s_addr, 4);
  2258. memcpy(&cmd->params.sgid[0],
  2259. &sgid_addr._sockaddr_in.sin_addr.s_addr, 4);
  2260. }
  2261. /* convert them to LE format. */
  2262. ocrdma_cpu_to_le32(&cmd->params.dgid[0], sizeof(cmd->params.dgid));
  2263. ocrdma_cpu_to_le32(&cmd->params.sgid[0], sizeof(cmd->params.sgid));
  2264. cmd->params.vlan_dmac_b4_to_b5 = mac_addr[4] | (mac_addr[5] << 8);
  2265. if (vlan_id == 0xFFFF)
  2266. vlan_id = 0;
  2267. if (vlan_id || dev->pfc_state) {
  2268. if (!vlan_id) {
  2269. pr_err("ocrdma%d:Using VLAN with PFC is recommended\n",
  2270. dev->id);
  2271. pr_err("ocrdma%d:Using VLAN 0 for this connection\n",
  2272. dev->id);
  2273. }
  2274. cmd->params.vlan_dmac_b4_to_b5 |=
  2275. vlan_id << OCRDMA_QP_PARAMS_VLAN_SHIFT;
  2276. cmd->flags |= OCRDMA_QP_PARA_VLAN_EN_VALID;
  2277. cmd->params.rnt_rc_sl_fl |=
  2278. (dev->sl & 0x07) << OCRDMA_QP_PARAMS_SL_SHIFT;
  2279. }
  2280. cmd->params.max_sge_recv_flags |= ((hdr_type <<
  2281. OCRDMA_QP_PARAMS_FLAGS_L3_TYPE_SHIFT) &
  2282. OCRDMA_QP_PARAMS_FLAGS_L3_TYPE_MASK);
  2283. return 0;
  2284. }
  2285. static int ocrdma_set_qp_params(struct ocrdma_qp *qp,
  2286. struct ocrdma_modify_qp *cmd,
  2287. struct ib_qp_attr *attrs, int attr_mask)
  2288. {
  2289. int status = 0;
  2290. struct ocrdma_dev *dev = get_ocrdma_dev(qp->ibqp.device);
  2291. if (attr_mask & IB_QP_PKEY_INDEX) {
  2292. cmd->params.path_mtu_pkey_indx |= (attrs->pkey_index &
  2293. OCRDMA_QP_PARAMS_PKEY_INDEX_MASK);
  2294. cmd->flags |= OCRDMA_QP_PARA_PKEY_VALID;
  2295. }
  2296. if (attr_mask & IB_QP_QKEY) {
  2297. qp->qkey = attrs->qkey;
  2298. cmd->params.qkey = attrs->qkey;
  2299. cmd->flags |= OCRDMA_QP_PARA_QKEY_VALID;
  2300. }
  2301. if (attr_mask & IB_QP_AV) {
  2302. status = ocrdma_set_av_params(qp, cmd, attrs, attr_mask);
  2303. if (status)
  2304. return status;
  2305. } else if (qp->qp_type == IB_QPT_GSI || qp->qp_type == IB_QPT_UD) {
  2306. /* set the default mac address for UD, GSI QPs */
  2307. cmd->params.dmac_b0_to_b3 = dev->nic_info.mac_addr[0] |
  2308. (dev->nic_info.mac_addr[1] << 8) |
  2309. (dev->nic_info.mac_addr[2] << 16) |
  2310. (dev->nic_info.mac_addr[3] << 24);
  2311. cmd->params.vlan_dmac_b4_to_b5 = dev->nic_info.mac_addr[4] |
  2312. (dev->nic_info.mac_addr[5] << 8);
  2313. }
  2314. if ((attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY) &&
  2315. attrs->en_sqd_async_notify) {
  2316. cmd->params.max_sge_recv_flags |=
  2317. OCRDMA_QP_PARAMS_FLAGS_SQD_ASYNC;
  2318. cmd->flags |= OCRDMA_QP_PARA_DST_QPN_VALID;
  2319. }
  2320. if (attr_mask & IB_QP_DEST_QPN) {
  2321. cmd->params.ack_to_rnr_rtc_dest_qpn |= (attrs->dest_qp_num &
  2322. OCRDMA_QP_PARAMS_DEST_QPN_MASK);
  2323. cmd->flags |= OCRDMA_QP_PARA_DST_QPN_VALID;
  2324. }
  2325. if (attr_mask & IB_QP_PATH_MTU) {
  2326. if (attrs->path_mtu < IB_MTU_512 ||
  2327. attrs->path_mtu > IB_MTU_4096) {
  2328. pr_err("ocrdma%d: IB MTU %d is not supported\n",
  2329. dev->id, ib_mtu_enum_to_int(attrs->path_mtu));
  2330. status = -EINVAL;
  2331. goto pmtu_err;
  2332. }
  2333. cmd->params.path_mtu_pkey_indx |=
  2334. (ib_mtu_enum_to_int(attrs->path_mtu) <<
  2335. OCRDMA_QP_PARAMS_PATH_MTU_SHIFT) &
  2336. OCRDMA_QP_PARAMS_PATH_MTU_MASK;
  2337. cmd->flags |= OCRDMA_QP_PARA_PMTU_VALID;
  2338. }
  2339. if (attr_mask & IB_QP_TIMEOUT) {
  2340. cmd->params.ack_to_rnr_rtc_dest_qpn |= attrs->timeout <<
  2341. OCRDMA_QP_PARAMS_ACK_TIMEOUT_SHIFT;
  2342. cmd->flags |= OCRDMA_QP_PARA_ACK_TO_VALID;
  2343. }
  2344. if (attr_mask & IB_QP_RETRY_CNT) {
  2345. cmd->params.rnt_rc_sl_fl |= (attrs->retry_cnt <<
  2346. OCRDMA_QP_PARAMS_RETRY_CNT_SHIFT) &
  2347. OCRDMA_QP_PARAMS_RETRY_CNT_MASK;
  2348. cmd->flags |= OCRDMA_QP_PARA_RETRY_CNT_VALID;
  2349. }
  2350. if (attr_mask & IB_QP_MIN_RNR_TIMER) {
  2351. cmd->params.rnt_rc_sl_fl |= (attrs->min_rnr_timer <<
  2352. OCRDMA_QP_PARAMS_RNR_NAK_TIMER_SHIFT) &
  2353. OCRDMA_QP_PARAMS_RNR_NAK_TIMER_MASK;
  2354. cmd->flags |= OCRDMA_QP_PARA_RNT_VALID;
  2355. }
  2356. if (attr_mask & IB_QP_RNR_RETRY) {
  2357. cmd->params.ack_to_rnr_rtc_dest_qpn |= (attrs->rnr_retry <<
  2358. OCRDMA_QP_PARAMS_RNR_RETRY_CNT_SHIFT)
  2359. & OCRDMA_QP_PARAMS_RNR_RETRY_CNT_MASK;
  2360. cmd->flags |= OCRDMA_QP_PARA_RRC_VALID;
  2361. }
  2362. if (attr_mask & IB_QP_SQ_PSN) {
  2363. cmd->params.tclass_sq_psn |= (attrs->sq_psn & 0x00ffffff);
  2364. cmd->flags |= OCRDMA_QP_PARA_SQPSN_VALID;
  2365. }
  2366. if (attr_mask & IB_QP_RQ_PSN) {
  2367. cmd->params.hop_lmt_rq_psn |= (attrs->rq_psn & 0x00ffffff);
  2368. cmd->flags |= OCRDMA_QP_PARA_RQPSN_VALID;
  2369. }
  2370. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
  2371. if (attrs->max_rd_atomic > dev->attr.max_ord_per_qp) {
  2372. status = -EINVAL;
  2373. goto pmtu_err;
  2374. }
  2375. qp->max_ord = attrs->max_rd_atomic;
  2376. cmd->flags |= OCRDMA_QP_PARA_MAX_ORD_VALID;
  2377. }
  2378. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  2379. if (attrs->max_dest_rd_atomic > dev->attr.max_ird_per_qp) {
  2380. status = -EINVAL;
  2381. goto pmtu_err;
  2382. }
  2383. qp->max_ird = attrs->max_dest_rd_atomic;
  2384. cmd->flags |= OCRDMA_QP_PARA_MAX_IRD_VALID;
  2385. }
  2386. cmd->params.max_ord_ird = (qp->max_ord <<
  2387. OCRDMA_QP_PARAMS_MAX_ORD_SHIFT) |
  2388. (qp->max_ird & OCRDMA_QP_PARAMS_MAX_IRD_MASK);
  2389. pmtu_err:
  2390. return status;
  2391. }
  2392. int ocrdma_mbx_modify_qp(struct ocrdma_dev *dev, struct ocrdma_qp *qp,
  2393. struct ib_qp_attr *attrs, int attr_mask)
  2394. {
  2395. int status = -ENOMEM;
  2396. struct ocrdma_modify_qp *cmd;
  2397. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_MODIFY_QP, sizeof(*cmd));
  2398. if (!cmd)
  2399. return status;
  2400. cmd->params.id = qp->id;
  2401. cmd->flags = 0;
  2402. if (attr_mask & IB_QP_STATE) {
  2403. cmd->params.max_sge_recv_flags |=
  2404. (get_ocrdma_qp_state(attrs->qp_state) <<
  2405. OCRDMA_QP_PARAMS_STATE_SHIFT) &
  2406. OCRDMA_QP_PARAMS_STATE_MASK;
  2407. cmd->flags |= OCRDMA_QP_PARA_QPS_VALID;
  2408. } else {
  2409. cmd->params.max_sge_recv_flags |=
  2410. (qp->state << OCRDMA_QP_PARAMS_STATE_SHIFT) &
  2411. OCRDMA_QP_PARAMS_STATE_MASK;
  2412. }
  2413. status = ocrdma_set_qp_params(qp, cmd, attrs, attr_mask);
  2414. if (status)
  2415. goto mbx_err;
  2416. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  2417. if (status)
  2418. goto mbx_err;
  2419. mbx_err:
  2420. kfree(cmd);
  2421. return status;
  2422. }
  2423. int ocrdma_mbx_destroy_qp(struct ocrdma_dev *dev, struct ocrdma_qp *qp)
  2424. {
  2425. int status = -ENOMEM;
  2426. struct ocrdma_destroy_qp *cmd;
  2427. struct pci_dev *pdev = dev->nic_info.pdev;
  2428. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DELETE_QP, sizeof(*cmd));
  2429. if (!cmd)
  2430. return status;
  2431. cmd->qp_id = qp->id;
  2432. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  2433. if (status)
  2434. goto mbx_err;
  2435. mbx_err:
  2436. kfree(cmd);
  2437. if (qp->sq.va)
  2438. dma_free_coherent(&pdev->dev, qp->sq.len, qp->sq.va, qp->sq.pa);
  2439. if (!qp->srq && qp->rq.va)
  2440. dma_free_coherent(&pdev->dev, qp->rq.len, qp->rq.va, qp->rq.pa);
  2441. if (qp->dpp_enabled)
  2442. qp->pd->num_dpp_qp++;
  2443. return status;
  2444. }
  2445. int ocrdma_mbx_create_srq(struct ocrdma_dev *dev, struct ocrdma_srq *srq,
  2446. struct ib_srq_init_attr *srq_attr,
  2447. struct ocrdma_pd *pd)
  2448. {
  2449. int status = -ENOMEM;
  2450. int hw_pages, hw_page_size;
  2451. int len;
  2452. struct ocrdma_create_srq_rsp *rsp;
  2453. struct ocrdma_create_srq *cmd;
  2454. dma_addr_t pa;
  2455. struct pci_dev *pdev = dev->nic_info.pdev;
  2456. u32 max_rqe_allocated;
  2457. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_CREATE_SRQ, sizeof(*cmd));
  2458. if (!cmd)
  2459. return status;
  2460. cmd->pgsz_pdid = pd->id & OCRDMA_CREATE_SRQ_PD_ID_MASK;
  2461. max_rqe_allocated = srq_attr->attr.max_wr + 1;
  2462. status = ocrdma_build_q_conf(&max_rqe_allocated,
  2463. dev->attr.rqe_size,
  2464. &hw_pages, &hw_page_size);
  2465. if (status) {
  2466. pr_err("%s() req. max_wr=0x%x\n", __func__,
  2467. srq_attr->attr.max_wr);
  2468. status = -EINVAL;
  2469. goto ret;
  2470. }
  2471. len = hw_pages * hw_page_size;
  2472. srq->rq.va = dma_alloc_coherent(&pdev->dev, len, &pa, GFP_KERNEL);
  2473. if (!srq->rq.va) {
  2474. status = -ENOMEM;
  2475. goto ret;
  2476. }
  2477. ocrdma_build_q_pages(&cmd->rq_addr[0], hw_pages, pa, hw_page_size);
  2478. srq->rq.entry_size = dev->attr.rqe_size;
  2479. srq->rq.pa = pa;
  2480. srq->rq.len = len;
  2481. srq->rq.max_cnt = max_rqe_allocated;
  2482. cmd->max_sge_rqe = ilog2(max_rqe_allocated);
  2483. cmd->max_sge_rqe |= srq_attr->attr.max_sge <<
  2484. OCRDMA_CREATE_SRQ_MAX_SGE_RECV_SHIFT;
  2485. cmd->pgsz_pdid |= (ilog2(hw_page_size / OCRDMA_MIN_Q_PAGE_SIZE)
  2486. << OCRDMA_CREATE_SRQ_PG_SZ_SHIFT);
  2487. cmd->pages_rqe_sz |= (dev->attr.rqe_size
  2488. << OCRDMA_CREATE_SRQ_RQE_SIZE_SHIFT)
  2489. & OCRDMA_CREATE_SRQ_RQE_SIZE_MASK;
  2490. cmd->pages_rqe_sz |= hw_pages << OCRDMA_CREATE_SRQ_NUM_RQ_PAGES_SHIFT;
  2491. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  2492. if (status)
  2493. goto mbx_err;
  2494. rsp = (struct ocrdma_create_srq_rsp *)cmd;
  2495. srq->id = rsp->id;
  2496. srq->rq.dbid = rsp->id;
  2497. max_rqe_allocated = ((rsp->max_sge_rqe_allocated &
  2498. OCRDMA_CREATE_SRQ_RSP_MAX_RQE_ALLOCATED_MASK) >>
  2499. OCRDMA_CREATE_SRQ_RSP_MAX_RQE_ALLOCATED_SHIFT);
  2500. max_rqe_allocated = (1 << max_rqe_allocated);
  2501. srq->rq.max_cnt = max_rqe_allocated;
  2502. srq->rq.max_wqe_idx = max_rqe_allocated - 1;
  2503. srq->rq.max_sges = (rsp->max_sge_rqe_allocated &
  2504. OCRDMA_CREATE_SRQ_RSP_MAX_SGE_RECV_ALLOCATED_MASK) >>
  2505. OCRDMA_CREATE_SRQ_RSP_MAX_SGE_RECV_ALLOCATED_SHIFT;
  2506. goto ret;
  2507. mbx_err:
  2508. dma_free_coherent(&pdev->dev, srq->rq.len, srq->rq.va, pa);
  2509. ret:
  2510. kfree(cmd);
  2511. return status;
  2512. }
  2513. int ocrdma_mbx_modify_srq(struct ocrdma_srq *srq, struct ib_srq_attr *srq_attr)
  2514. {
  2515. int status = -ENOMEM;
  2516. struct ocrdma_modify_srq *cmd;
  2517. struct ocrdma_pd *pd = srq->pd;
  2518. struct ocrdma_dev *dev = get_ocrdma_dev(pd->ibpd.device);
  2519. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_MODIFY_SRQ, sizeof(*cmd));
  2520. if (!cmd)
  2521. return status;
  2522. cmd->id = srq->id;
  2523. cmd->limit_max_rqe |= srq_attr->srq_limit <<
  2524. OCRDMA_MODIFY_SRQ_LIMIT_SHIFT;
  2525. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  2526. kfree(cmd);
  2527. return status;
  2528. }
  2529. int ocrdma_mbx_query_srq(struct ocrdma_srq *srq, struct ib_srq_attr *srq_attr)
  2530. {
  2531. int status = -ENOMEM;
  2532. struct ocrdma_query_srq *cmd;
  2533. struct ocrdma_dev *dev = get_ocrdma_dev(srq->ibsrq.device);
  2534. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_QUERY_SRQ, sizeof(*cmd));
  2535. if (!cmd)
  2536. return status;
  2537. cmd->id = srq->rq.dbid;
  2538. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  2539. if (status == 0) {
  2540. struct ocrdma_query_srq_rsp *rsp =
  2541. (struct ocrdma_query_srq_rsp *)cmd;
  2542. srq_attr->max_sge =
  2543. rsp->srq_lmt_max_sge &
  2544. OCRDMA_QUERY_SRQ_RSP_MAX_SGE_RECV_MASK;
  2545. srq_attr->max_wr =
  2546. rsp->max_rqe_pdid >> OCRDMA_QUERY_SRQ_RSP_MAX_RQE_SHIFT;
  2547. srq_attr->srq_limit = rsp->srq_lmt_max_sge >>
  2548. OCRDMA_QUERY_SRQ_RSP_SRQ_LIMIT_SHIFT;
  2549. }
  2550. kfree(cmd);
  2551. return status;
  2552. }
  2553. int ocrdma_mbx_destroy_srq(struct ocrdma_dev *dev, struct ocrdma_srq *srq)
  2554. {
  2555. int status = -ENOMEM;
  2556. struct ocrdma_destroy_srq *cmd;
  2557. struct pci_dev *pdev = dev->nic_info.pdev;
  2558. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_DELETE_SRQ, sizeof(*cmd));
  2559. if (!cmd)
  2560. return status;
  2561. cmd->id = srq->id;
  2562. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  2563. if (srq->rq.va)
  2564. dma_free_coherent(&pdev->dev, srq->rq.len,
  2565. srq->rq.va, srq->rq.pa);
  2566. kfree(cmd);
  2567. return status;
  2568. }
  2569. static int ocrdma_mbx_get_dcbx_config(struct ocrdma_dev *dev, u32 ptype,
  2570. struct ocrdma_dcbx_cfg *dcbxcfg)
  2571. {
  2572. int status;
  2573. dma_addr_t pa;
  2574. struct ocrdma_mqe cmd;
  2575. struct ocrdma_get_dcbx_cfg_req *req = NULL;
  2576. struct ocrdma_get_dcbx_cfg_rsp *rsp = NULL;
  2577. struct pci_dev *pdev = dev->nic_info.pdev;
  2578. struct ocrdma_mqe_sge *mqe_sge = cmd.u.nonemb_req.sge;
  2579. memset(&cmd, 0, sizeof(struct ocrdma_mqe));
  2580. cmd.hdr.pyld_len = max_t (u32, sizeof(struct ocrdma_get_dcbx_cfg_rsp),
  2581. sizeof(struct ocrdma_get_dcbx_cfg_req));
  2582. req = dma_alloc_coherent(&pdev->dev, cmd.hdr.pyld_len, &pa, GFP_KERNEL);
  2583. if (!req) {
  2584. status = -ENOMEM;
  2585. goto mem_err;
  2586. }
  2587. cmd.hdr.spcl_sge_cnt_emb |= (1 << OCRDMA_MQE_HDR_SGE_CNT_SHIFT) &
  2588. OCRDMA_MQE_HDR_SGE_CNT_MASK;
  2589. mqe_sge->pa_lo = (u32) (pa & 0xFFFFFFFFUL);
  2590. mqe_sge->pa_hi = (u32) upper_32_bits(pa);
  2591. mqe_sge->len = cmd.hdr.pyld_len;
  2592. memset(req, 0, sizeof(struct ocrdma_get_dcbx_cfg_req));
  2593. ocrdma_init_mch(&req->hdr, OCRDMA_CMD_GET_DCBX_CONFIG,
  2594. OCRDMA_SUBSYS_DCBX, cmd.hdr.pyld_len);
  2595. req->param_type = ptype;
  2596. status = ocrdma_mbx_cmd(dev, &cmd);
  2597. if (status)
  2598. goto mbx_err;
  2599. rsp = (struct ocrdma_get_dcbx_cfg_rsp *)req;
  2600. ocrdma_le32_to_cpu(rsp, sizeof(struct ocrdma_get_dcbx_cfg_rsp));
  2601. memcpy(dcbxcfg, &rsp->cfg, sizeof(struct ocrdma_dcbx_cfg));
  2602. mbx_err:
  2603. dma_free_coherent(&pdev->dev, cmd.hdr.pyld_len, req, pa);
  2604. mem_err:
  2605. return status;
  2606. }
  2607. #define OCRDMA_MAX_SERVICE_LEVEL_INDEX 0x08
  2608. #define OCRDMA_DEFAULT_SERVICE_LEVEL 0x05
  2609. static int ocrdma_parse_dcbxcfg_rsp(struct ocrdma_dev *dev, int ptype,
  2610. struct ocrdma_dcbx_cfg *dcbxcfg,
  2611. u8 *srvc_lvl)
  2612. {
  2613. int status = -EINVAL, indx, slindx;
  2614. int ventry_cnt;
  2615. struct ocrdma_app_parameter *app_param;
  2616. u8 valid, proto_sel;
  2617. u8 app_prio, pfc_prio;
  2618. u16 proto;
  2619. if (!(dcbxcfg->tcv_aev_opv_st & OCRDMA_DCBX_STATE_MASK)) {
  2620. pr_info("%s ocrdma%d DCBX is disabled\n",
  2621. dev_name(&dev->nic_info.pdev->dev), dev->id);
  2622. goto out;
  2623. }
  2624. if (!ocrdma_is_enabled_and_synced(dcbxcfg->pfc_state)) {
  2625. pr_info("%s ocrdma%d priority flow control(%s) is %s%s\n",
  2626. dev_name(&dev->nic_info.pdev->dev), dev->id,
  2627. (ptype > 0 ? "operational" : "admin"),
  2628. (dcbxcfg->pfc_state & OCRDMA_STATE_FLAG_ENABLED) ?
  2629. "enabled" : "disabled",
  2630. (dcbxcfg->pfc_state & OCRDMA_STATE_FLAG_SYNC) ?
  2631. "" : ", not sync'ed");
  2632. goto out;
  2633. } else {
  2634. pr_info("%s ocrdma%d priority flow control is enabled and sync'ed\n",
  2635. dev_name(&dev->nic_info.pdev->dev), dev->id);
  2636. }
  2637. ventry_cnt = (dcbxcfg->tcv_aev_opv_st >>
  2638. OCRDMA_DCBX_APP_ENTRY_SHIFT)
  2639. & OCRDMA_DCBX_STATE_MASK;
  2640. for (indx = 0; indx < ventry_cnt; indx++) {
  2641. app_param = &dcbxcfg->app_param[indx];
  2642. valid = (app_param->valid_proto_app >>
  2643. OCRDMA_APP_PARAM_VALID_SHIFT)
  2644. & OCRDMA_APP_PARAM_VALID_MASK;
  2645. proto_sel = (app_param->valid_proto_app
  2646. >> OCRDMA_APP_PARAM_PROTO_SEL_SHIFT)
  2647. & OCRDMA_APP_PARAM_PROTO_SEL_MASK;
  2648. proto = app_param->valid_proto_app &
  2649. OCRDMA_APP_PARAM_APP_PROTO_MASK;
  2650. if (
  2651. valid && proto == ETH_P_IBOE &&
  2652. proto_sel == OCRDMA_PROTO_SELECT_L2) {
  2653. for (slindx = 0; slindx <
  2654. OCRDMA_MAX_SERVICE_LEVEL_INDEX; slindx++) {
  2655. app_prio = ocrdma_get_app_prio(
  2656. (u8 *)app_param->app_prio,
  2657. slindx);
  2658. pfc_prio = ocrdma_get_pfc_prio(
  2659. (u8 *)dcbxcfg->pfc_prio,
  2660. slindx);
  2661. if (app_prio && pfc_prio) {
  2662. *srvc_lvl = slindx;
  2663. status = 0;
  2664. goto out;
  2665. }
  2666. }
  2667. if (slindx == OCRDMA_MAX_SERVICE_LEVEL_INDEX) {
  2668. pr_info("%s ocrdma%d application priority not set for 0x%x protocol\n",
  2669. dev_name(&dev->nic_info.pdev->dev),
  2670. dev->id, proto);
  2671. }
  2672. }
  2673. }
  2674. out:
  2675. return status;
  2676. }
  2677. void ocrdma_init_service_level(struct ocrdma_dev *dev)
  2678. {
  2679. int status = 0, indx;
  2680. struct ocrdma_dcbx_cfg dcbxcfg;
  2681. u8 srvc_lvl = OCRDMA_DEFAULT_SERVICE_LEVEL;
  2682. int ptype = OCRDMA_PARAMETER_TYPE_OPER;
  2683. for (indx = 0; indx < 2; indx++) {
  2684. status = ocrdma_mbx_get_dcbx_config(dev, ptype, &dcbxcfg);
  2685. if (status) {
  2686. pr_err("%s(): status=%d\n", __func__, status);
  2687. ptype = OCRDMA_PARAMETER_TYPE_ADMIN;
  2688. continue;
  2689. }
  2690. status = ocrdma_parse_dcbxcfg_rsp(dev, ptype,
  2691. &dcbxcfg, &srvc_lvl);
  2692. if (status) {
  2693. ptype = OCRDMA_PARAMETER_TYPE_ADMIN;
  2694. continue;
  2695. }
  2696. break;
  2697. }
  2698. if (status)
  2699. pr_info("%s ocrdma%d service level default\n",
  2700. dev_name(&dev->nic_info.pdev->dev), dev->id);
  2701. else
  2702. pr_info("%s ocrdma%d service level %d\n",
  2703. dev_name(&dev->nic_info.pdev->dev), dev->id,
  2704. srvc_lvl);
  2705. dev->pfc_state = ocrdma_is_enabled_and_synced(dcbxcfg.pfc_state);
  2706. dev->sl = srvc_lvl;
  2707. }
  2708. int ocrdma_alloc_av(struct ocrdma_dev *dev, struct ocrdma_ah *ah)
  2709. {
  2710. int i;
  2711. int status = -EINVAL;
  2712. struct ocrdma_av *av;
  2713. unsigned long flags;
  2714. av = dev->av_tbl.va;
  2715. spin_lock_irqsave(&dev->av_tbl.lock, flags);
  2716. for (i = 0; i < dev->av_tbl.num_ah; i++) {
  2717. if (av->valid == 0) {
  2718. av->valid = OCRDMA_AV_VALID;
  2719. ah->av = av;
  2720. ah->id = i;
  2721. status = 0;
  2722. break;
  2723. }
  2724. av++;
  2725. }
  2726. if (i == dev->av_tbl.num_ah)
  2727. status = -EAGAIN;
  2728. spin_unlock_irqrestore(&dev->av_tbl.lock, flags);
  2729. return status;
  2730. }
  2731. int ocrdma_free_av(struct ocrdma_dev *dev, struct ocrdma_ah *ah)
  2732. {
  2733. unsigned long flags;
  2734. spin_lock_irqsave(&dev->av_tbl.lock, flags);
  2735. ah->av->valid = 0;
  2736. spin_unlock_irqrestore(&dev->av_tbl.lock, flags);
  2737. return 0;
  2738. }
  2739. static int ocrdma_create_eqs(struct ocrdma_dev *dev)
  2740. {
  2741. int num_eq, i, status = 0;
  2742. int irq;
  2743. unsigned long flags = 0;
  2744. num_eq = dev->nic_info.msix.num_vectors -
  2745. dev->nic_info.msix.start_vector;
  2746. if (dev->nic_info.intr_mode == BE_INTERRUPT_MODE_INTX) {
  2747. num_eq = 1;
  2748. flags = IRQF_SHARED;
  2749. } else {
  2750. num_eq = min_t(u32, num_eq, num_online_cpus());
  2751. }
  2752. if (!num_eq)
  2753. return -EINVAL;
  2754. dev->eq_tbl = kcalloc(num_eq, sizeof(struct ocrdma_eq), GFP_KERNEL);
  2755. if (!dev->eq_tbl)
  2756. return -ENOMEM;
  2757. for (i = 0; i < num_eq; i++) {
  2758. status = ocrdma_create_eq(dev, &dev->eq_tbl[i],
  2759. OCRDMA_EQ_LEN);
  2760. if (status) {
  2761. status = -EINVAL;
  2762. break;
  2763. }
  2764. sprintf(dev->eq_tbl[i].irq_name, "ocrdma%d-%d",
  2765. dev->id, i);
  2766. irq = ocrdma_get_irq(dev, &dev->eq_tbl[i]);
  2767. status = request_irq(irq, ocrdma_irq_handler, flags,
  2768. dev->eq_tbl[i].irq_name,
  2769. &dev->eq_tbl[i]);
  2770. if (status)
  2771. goto done;
  2772. dev->eq_cnt += 1;
  2773. }
  2774. /* one eq is sufficient for data path to work */
  2775. return 0;
  2776. done:
  2777. ocrdma_destroy_eqs(dev);
  2778. return status;
  2779. }
  2780. static int ocrdma_mbx_modify_eqd(struct ocrdma_dev *dev, struct ocrdma_eq *eq,
  2781. int num)
  2782. {
  2783. int i, status;
  2784. struct ocrdma_modify_eqd_req *cmd;
  2785. cmd = ocrdma_init_emb_mqe(OCRDMA_CMD_MODIFY_EQ_DELAY, sizeof(*cmd));
  2786. if (!cmd)
  2787. return -ENOMEM;
  2788. ocrdma_init_mch(&cmd->cmd.req, OCRDMA_CMD_MODIFY_EQ_DELAY,
  2789. OCRDMA_SUBSYS_COMMON, sizeof(*cmd));
  2790. cmd->cmd.num_eq = num;
  2791. for (i = 0; i < num; i++) {
  2792. cmd->cmd.set_eqd[i].eq_id = eq[i].q.id;
  2793. cmd->cmd.set_eqd[i].phase = 0;
  2794. cmd->cmd.set_eqd[i].delay_multiplier =
  2795. (eq[i].aic_obj.prev_eqd * 65)/100;
  2796. }
  2797. status = ocrdma_mbx_cmd(dev, (struct ocrdma_mqe *)cmd);
  2798. kfree(cmd);
  2799. return status;
  2800. }
  2801. static int ocrdma_modify_eqd(struct ocrdma_dev *dev, struct ocrdma_eq *eq,
  2802. int num)
  2803. {
  2804. int num_eqs, i = 0;
  2805. if (num > 8) {
  2806. while (num) {
  2807. num_eqs = min(num, 8);
  2808. ocrdma_mbx_modify_eqd(dev, &eq[i], num_eqs);
  2809. i += num_eqs;
  2810. num -= num_eqs;
  2811. }
  2812. } else {
  2813. ocrdma_mbx_modify_eqd(dev, eq, num);
  2814. }
  2815. return 0;
  2816. }
  2817. void ocrdma_eqd_set_task(struct work_struct *work)
  2818. {
  2819. struct ocrdma_dev *dev =
  2820. container_of(work, struct ocrdma_dev, eqd_work.work);
  2821. struct ocrdma_eq *eq = NULL;
  2822. int i, num = 0;
  2823. u64 eq_intr;
  2824. for (i = 0; i < dev->eq_cnt; i++) {
  2825. eq = &dev->eq_tbl[i];
  2826. if (eq->aic_obj.eq_intr_cnt > eq->aic_obj.prev_eq_intr_cnt) {
  2827. eq_intr = eq->aic_obj.eq_intr_cnt -
  2828. eq->aic_obj.prev_eq_intr_cnt;
  2829. if ((eq_intr > EQ_INTR_PER_SEC_THRSH_HI) &&
  2830. (eq->aic_obj.prev_eqd == EQ_AIC_MIN_EQD)) {
  2831. eq->aic_obj.prev_eqd = EQ_AIC_MAX_EQD;
  2832. num++;
  2833. } else if ((eq_intr < EQ_INTR_PER_SEC_THRSH_LOW) &&
  2834. (eq->aic_obj.prev_eqd == EQ_AIC_MAX_EQD)) {
  2835. eq->aic_obj.prev_eqd = EQ_AIC_MIN_EQD;
  2836. num++;
  2837. }
  2838. }
  2839. eq->aic_obj.prev_eq_intr_cnt = eq->aic_obj.eq_intr_cnt;
  2840. }
  2841. if (num)
  2842. ocrdma_modify_eqd(dev, &dev->eq_tbl[0], num);
  2843. schedule_delayed_work(&dev->eqd_work, msecs_to_jiffies(1000));
  2844. }
  2845. int ocrdma_init_hw(struct ocrdma_dev *dev)
  2846. {
  2847. int status;
  2848. /* create the eqs */
  2849. status = ocrdma_create_eqs(dev);
  2850. if (status)
  2851. goto qpeq_err;
  2852. status = ocrdma_create_mq(dev);
  2853. if (status)
  2854. goto mq_err;
  2855. status = ocrdma_mbx_query_fw_config(dev);
  2856. if (status)
  2857. goto conf_err;
  2858. status = ocrdma_mbx_query_dev(dev);
  2859. if (status)
  2860. goto conf_err;
  2861. status = ocrdma_mbx_query_fw_ver(dev);
  2862. if (status)
  2863. goto conf_err;
  2864. status = ocrdma_mbx_create_ah_tbl(dev);
  2865. if (status)
  2866. goto conf_err;
  2867. status = ocrdma_mbx_get_phy_info(dev);
  2868. if (status)
  2869. goto info_attrb_err;
  2870. status = ocrdma_mbx_get_ctrl_attribs(dev);
  2871. if (status)
  2872. goto info_attrb_err;
  2873. return 0;
  2874. info_attrb_err:
  2875. ocrdma_mbx_delete_ah_tbl(dev);
  2876. conf_err:
  2877. ocrdma_destroy_mq(dev);
  2878. mq_err:
  2879. ocrdma_destroy_eqs(dev);
  2880. qpeq_err:
  2881. pr_err("%s() status=%d\n", __func__, status);
  2882. return status;
  2883. }
  2884. void ocrdma_cleanup_hw(struct ocrdma_dev *dev)
  2885. {
  2886. ocrdma_free_pd_pool(dev);
  2887. ocrdma_mbx_delete_ah_tbl(dev);
  2888. /* cleanup the control path */
  2889. ocrdma_destroy_mq(dev);
  2890. /* cleanup the eqs */
  2891. ocrdma_destroy_eqs(dev);
  2892. }