lpfc_sli.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2005 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  8. * *
  9. * This program is free software; you can redistribute it and/or *
  10. * modify it under the terms of version 2 of the GNU General *
  11. * Public License as published by the Free Software Foundation. *
  12. * This program is distributed in the hope that it will be useful. *
  13. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  14. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  15. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  16. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  17. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  18. * more details, a copy of which can be found in the file COPYING *
  19. * included with this package. *
  20. *******************************************************************/
  21. #include <linux/blkdev.h>
  22. #include <linux/pci.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <scsi/scsi.h>
  26. #include <scsi/scsi_cmnd.h>
  27. #include <scsi/scsi_device.h>
  28. #include <scsi/scsi_host.h>
  29. #include <scsi/scsi_transport_fc.h>
  30. #include "lpfc_hw.h"
  31. #include "lpfc_sli.h"
  32. #include "lpfc_disc.h"
  33. #include "lpfc_scsi.h"
  34. #include "lpfc.h"
  35. #include "lpfc_crtn.h"
  36. #include "lpfc_logmsg.h"
  37. #include "lpfc_compat.h"
  38. /*
  39. * Define macro to log: Mailbox command x%x cannot issue Data
  40. * This allows multiple uses of lpfc_msgBlk0311
  41. * w/o perturbing log msg utility.
  42. */
  43. #define LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag) \
  44. lpfc_printf_log(phba, \
  45. KERN_INFO, \
  46. LOG_MBOX | LOG_SLI, \
  47. "%d:0311 Mailbox command x%x cannot issue " \
  48. "Data: x%x x%x x%x\n", \
  49. phba->brd_no, \
  50. mb->mbxCommand, \
  51. phba->hba_state, \
  52. psli->sli_flag, \
  53. flag);
  54. /* There are only four IOCB completion types. */
  55. typedef enum _lpfc_iocb_type {
  56. LPFC_UNKNOWN_IOCB,
  57. LPFC_UNSOL_IOCB,
  58. LPFC_SOL_IOCB,
  59. LPFC_ABORT_IOCB
  60. } lpfc_iocb_type;
  61. struct lpfc_iocbq *
  62. lpfc_sli_get_iocbq(struct lpfc_hba * phba)
  63. {
  64. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  65. struct lpfc_iocbq * iocbq = NULL;
  66. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  67. return iocbq;
  68. }
  69. void
  70. lpfc_sli_release_iocbq(struct lpfc_hba * phba, struct lpfc_iocbq * iocbq)
  71. {
  72. size_t start_clean = (size_t)(&((struct lpfc_iocbq *)NULL)->iocb);
  73. /*
  74. * Clean all volatile data fields, preserve iotag and node struct.
  75. */
  76. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  77. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  78. }
  79. /*
  80. * Translate the iocb command to an iocb command type used to decide the final
  81. * disposition of each completed IOCB.
  82. */
  83. static lpfc_iocb_type
  84. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  85. {
  86. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  87. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  88. return 0;
  89. switch (iocb_cmnd) {
  90. case CMD_XMIT_SEQUENCE_CR:
  91. case CMD_XMIT_SEQUENCE_CX:
  92. case CMD_XMIT_BCAST_CN:
  93. case CMD_XMIT_BCAST_CX:
  94. case CMD_ELS_REQUEST_CR:
  95. case CMD_ELS_REQUEST_CX:
  96. case CMD_CREATE_XRI_CR:
  97. case CMD_CREATE_XRI_CX:
  98. case CMD_GET_RPI_CN:
  99. case CMD_XMIT_ELS_RSP_CX:
  100. case CMD_GET_RPI_CR:
  101. case CMD_FCP_IWRITE_CR:
  102. case CMD_FCP_IWRITE_CX:
  103. case CMD_FCP_IREAD_CR:
  104. case CMD_FCP_IREAD_CX:
  105. case CMD_FCP_ICMND_CR:
  106. case CMD_FCP_ICMND_CX:
  107. case CMD_ADAPTER_MSG:
  108. case CMD_ADAPTER_DUMP:
  109. case CMD_XMIT_SEQUENCE64_CR:
  110. case CMD_XMIT_SEQUENCE64_CX:
  111. case CMD_XMIT_BCAST64_CN:
  112. case CMD_XMIT_BCAST64_CX:
  113. case CMD_ELS_REQUEST64_CR:
  114. case CMD_ELS_REQUEST64_CX:
  115. case CMD_FCP_IWRITE64_CR:
  116. case CMD_FCP_IWRITE64_CX:
  117. case CMD_FCP_IREAD64_CR:
  118. case CMD_FCP_IREAD64_CX:
  119. case CMD_FCP_ICMND64_CR:
  120. case CMD_FCP_ICMND64_CX:
  121. case CMD_GEN_REQUEST64_CR:
  122. case CMD_GEN_REQUEST64_CX:
  123. case CMD_XMIT_ELS_RSP64_CX:
  124. type = LPFC_SOL_IOCB;
  125. break;
  126. case CMD_ABORT_XRI_CN:
  127. case CMD_ABORT_XRI_CX:
  128. case CMD_CLOSE_XRI_CN:
  129. case CMD_CLOSE_XRI_CX:
  130. case CMD_XRI_ABORTED_CX:
  131. case CMD_ABORT_MXRI64_CN:
  132. type = LPFC_ABORT_IOCB;
  133. break;
  134. case CMD_RCV_SEQUENCE_CX:
  135. case CMD_RCV_ELS_REQ_CX:
  136. case CMD_RCV_SEQUENCE64_CX:
  137. case CMD_RCV_ELS_REQ64_CX:
  138. type = LPFC_UNSOL_IOCB;
  139. break;
  140. default:
  141. type = LPFC_UNKNOWN_IOCB;
  142. break;
  143. }
  144. return type;
  145. }
  146. static int
  147. lpfc_sli_ring_map(struct lpfc_hba * phba, LPFC_MBOXQ_t *pmb)
  148. {
  149. struct lpfc_sli *psli = &phba->sli;
  150. MAILBOX_t *pmbox = &pmb->mb;
  151. int i, rc;
  152. for (i = 0; i < psli->num_rings; i++) {
  153. phba->hba_state = LPFC_INIT_MBX_CMDS;
  154. lpfc_config_ring(phba, i, pmb);
  155. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  156. if (rc != MBX_SUCCESS) {
  157. lpfc_printf_log(phba,
  158. KERN_ERR,
  159. LOG_INIT,
  160. "%d:0446 Adapter failed to init, "
  161. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  162. "ring %d\n",
  163. phba->brd_no,
  164. pmbox->mbxCommand,
  165. pmbox->mbxStatus,
  166. i);
  167. phba->hba_state = LPFC_HBA_ERROR;
  168. return -ENXIO;
  169. }
  170. }
  171. return 0;
  172. }
  173. static int
  174. lpfc_sli_ringtxcmpl_put(struct lpfc_hba * phba,
  175. struct lpfc_sli_ring * pring, struct lpfc_iocbq * piocb)
  176. {
  177. uint16_t iotag;
  178. list_add_tail(&piocb->list, &pring->txcmplq);
  179. pring->txcmplq_cnt++;
  180. if (unlikely(pring->ringno == LPFC_ELS_RING))
  181. mod_timer(&phba->els_tmofunc,
  182. jiffies + HZ * (phba->fc_ratov << 1));
  183. if (pring->fast_lookup) {
  184. /* Setup fast lookup based on iotag for completion */
  185. iotag = piocb->iocb.ulpIoTag;
  186. if (iotag && (iotag < pring->fast_iotag))
  187. *(pring->fast_lookup + iotag) = piocb;
  188. else {
  189. /* Cmd ring <ringno> put: iotag <iotag> greater then
  190. configured max <fast_iotag> wd0 <icmd> */
  191. lpfc_printf_log(phba,
  192. KERN_ERR,
  193. LOG_SLI,
  194. "%d:0316 Cmd ring %d put: iotag x%x "
  195. "greater then configured max x%x "
  196. "wd0 x%x\n",
  197. phba->brd_no,
  198. pring->ringno, iotag,
  199. pring->fast_iotag,
  200. *(((uint32_t *)(&piocb->iocb)) + 7));
  201. }
  202. }
  203. return (0);
  204. }
  205. static struct lpfc_iocbq *
  206. lpfc_sli_ringtx_get(struct lpfc_hba * phba, struct lpfc_sli_ring * pring)
  207. {
  208. struct list_head *dlp;
  209. struct lpfc_iocbq *cmd_iocb;
  210. dlp = &pring->txq;
  211. cmd_iocb = NULL;
  212. list_remove_head((&pring->txq), cmd_iocb,
  213. struct lpfc_iocbq,
  214. list);
  215. if (cmd_iocb) {
  216. /* If the first ptr is not equal to the list header,
  217. * deque the IOCBQ_t and return it.
  218. */
  219. pring->txq_cnt--;
  220. }
  221. return (cmd_iocb);
  222. }
  223. static IOCB_t *
  224. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  225. {
  226. struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[pring->ringno];
  227. uint32_t max_cmd_idx = pring->numCiocb;
  228. IOCB_t *iocb = NULL;
  229. if ((pring->next_cmdidx == pring->cmdidx) &&
  230. (++pring->next_cmdidx >= max_cmd_idx))
  231. pring->next_cmdidx = 0;
  232. if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
  233. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  234. if (unlikely(pring->local_getidx >= max_cmd_idx)) {
  235. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  236. "%d:0315 Ring %d issue: portCmdGet %d "
  237. "is bigger then cmd ring %d\n",
  238. phba->brd_no, pring->ringno,
  239. pring->local_getidx, max_cmd_idx);
  240. phba->hba_state = LPFC_HBA_ERROR;
  241. /*
  242. * All error attention handlers are posted to
  243. * worker thread
  244. */
  245. phba->work_ha |= HA_ERATT;
  246. phba->work_hs = HS_FFER3;
  247. if (phba->work_wait)
  248. wake_up(phba->work_wait);
  249. return NULL;
  250. }
  251. if (pring->local_getidx == pring->next_cmdidx)
  252. return NULL;
  253. }
  254. iocb = IOCB_ENTRY(pring->cmdringaddr, pring->cmdidx);
  255. return iocb;
  256. }
  257. uint16_t
  258. lpfc_sli_next_iotag(struct lpfc_hba * phba, struct lpfc_iocbq * iocbq)
  259. {
  260. struct lpfc_iocbq ** new_arr;
  261. struct lpfc_iocbq ** old_arr;
  262. size_t new_len;
  263. struct lpfc_sli *psli = &phba->sli;
  264. uint16_t iotag;
  265. spin_lock_irq(phba->host->host_lock);
  266. iotag = psli->last_iotag;
  267. if(++iotag < psli->iocbq_lookup_len) {
  268. psli->last_iotag = iotag;
  269. psli->iocbq_lookup[iotag] = iocbq;
  270. spin_unlock_irq(phba->host->host_lock);
  271. iocbq->iotag = iotag;
  272. return iotag;
  273. }
  274. else if (psli->iocbq_lookup_len < (0xffff
  275. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  276. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  277. spin_unlock_irq(phba->host->host_lock);
  278. new_arr = kmalloc(new_len * sizeof (struct lpfc_iocbq *),
  279. GFP_KERNEL);
  280. if (new_arr) {
  281. memset((char *)new_arr, 0,
  282. new_len * sizeof (struct lpfc_iocbq *));
  283. spin_lock_irq(phba->host->host_lock);
  284. old_arr = psli->iocbq_lookup;
  285. if (new_len <= psli->iocbq_lookup_len) {
  286. /* highly unprobable case */
  287. kfree(new_arr);
  288. iotag = psli->last_iotag;
  289. if(++iotag < psli->iocbq_lookup_len) {
  290. psli->last_iotag = iotag;
  291. psli->iocbq_lookup[iotag] = iocbq;
  292. spin_unlock_irq(phba->host->host_lock);
  293. iocbq->iotag = iotag;
  294. return iotag;
  295. }
  296. spin_unlock_irq(phba->host->host_lock);
  297. return 0;
  298. }
  299. if (psli->iocbq_lookup)
  300. memcpy(new_arr, old_arr,
  301. ((psli->last_iotag + 1) *
  302. sizeof (struct lpfc_iocbq *)));
  303. psli->iocbq_lookup = new_arr;
  304. psli->iocbq_lookup_len = new_len;
  305. psli->last_iotag = iotag;
  306. psli->iocbq_lookup[iotag] = iocbq;
  307. spin_unlock_irq(phba->host->host_lock);
  308. iocbq->iotag = iotag;
  309. kfree(old_arr);
  310. return iotag;
  311. }
  312. }
  313. lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
  314. "%d:0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  315. phba->brd_no, psli->last_iotag);
  316. return 0;
  317. }
  318. static void
  319. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  320. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  321. {
  322. /*
  323. * Set up an iotag
  324. */
  325. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  326. /*
  327. * Issue iocb command to adapter
  328. */
  329. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, sizeof (IOCB_t));
  330. wmb();
  331. pring->stats.iocb_cmd++;
  332. /*
  333. * If there is no completion routine to call, we can release the
  334. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  335. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  336. */
  337. if (nextiocb->iocb_cmpl)
  338. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  339. else
  340. lpfc_sli_release_iocbq(phba, nextiocb);
  341. /*
  342. * Let the HBA know what IOCB slot will be the next one the
  343. * driver will put a command into.
  344. */
  345. pring->cmdidx = pring->next_cmdidx;
  346. writel(pring->cmdidx, phba->MBslimaddr
  347. + (SLIMOFF + (pring->ringno * 2)) * 4);
  348. }
  349. static void
  350. lpfc_sli_update_full_ring(struct lpfc_hba * phba,
  351. struct lpfc_sli_ring *pring)
  352. {
  353. int ringno = pring->ringno;
  354. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  355. wmb();
  356. /*
  357. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  358. * The HBA will tell us when an IOCB entry is available.
  359. */
  360. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  361. readl(phba->CAregaddr); /* flush */
  362. pring->stats.iocb_cmd_full++;
  363. }
  364. static void
  365. lpfc_sli_update_ring(struct lpfc_hba * phba,
  366. struct lpfc_sli_ring *pring)
  367. {
  368. int ringno = pring->ringno;
  369. /*
  370. * Tell the HBA that there is work to do in this ring.
  371. */
  372. wmb();
  373. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  374. readl(phba->CAregaddr); /* flush */
  375. }
  376. static void
  377. lpfc_sli_resume_iocb(struct lpfc_hba * phba, struct lpfc_sli_ring * pring)
  378. {
  379. IOCB_t *iocb;
  380. struct lpfc_iocbq *nextiocb;
  381. /*
  382. * Check to see if:
  383. * (a) there is anything on the txq to send
  384. * (b) link is up
  385. * (c) link attention events can be processed (fcp ring only)
  386. * (d) IOCB processing is not blocked by the outstanding mbox command.
  387. */
  388. if (pring->txq_cnt &&
  389. (phba->hba_state > LPFC_LINK_DOWN) &&
  390. (pring->ringno != phba->sli.fcp_ring ||
  391. phba->sli.sli_flag & LPFC_PROCESS_LA) &&
  392. !(pring->flag & LPFC_STOP_IOCB_MBX)) {
  393. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  394. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  395. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  396. if (iocb)
  397. lpfc_sli_update_ring(phba, pring);
  398. else
  399. lpfc_sli_update_full_ring(phba, pring);
  400. }
  401. return;
  402. }
  403. /* lpfc_sli_turn_on_ring is only called by lpfc_sli_handle_mb_event below */
  404. static void
  405. lpfc_sli_turn_on_ring(struct lpfc_hba * phba, int ringno)
  406. {
  407. struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[ringno];
  408. /* If the ring is active, flag it */
  409. if (phba->sli.ring[ringno].cmdringaddr) {
  410. if (phba->sli.ring[ringno].flag & LPFC_STOP_IOCB_MBX) {
  411. phba->sli.ring[ringno].flag &= ~LPFC_STOP_IOCB_MBX;
  412. /*
  413. * Force update of the local copy of cmdGetInx
  414. */
  415. phba->sli.ring[ringno].local_getidx
  416. = le32_to_cpu(pgp->cmdGetInx);
  417. spin_lock_irq(phba->host->host_lock);
  418. lpfc_sli_resume_iocb(phba, &phba->sli.ring[ringno]);
  419. spin_unlock_irq(phba->host->host_lock);
  420. }
  421. }
  422. }
  423. static int
  424. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  425. {
  426. uint8_t ret;
  427. switch (mbxCommand) {
  428. case MBX_LOAD_SM:
  429. case MBX_READ_NV:
  430. case MBX_WRITE_NV:
  431. case MBX_RUN_BIU_DIAG:
  432. case MBX_INIT_LINK:
  433. case MBX_DOWN_LINK:
  434. case MBX_CONFIG_LINK:
  435. case MBX_CONFIG_RING:
  436. case MBX_RESET_RING:
  437. case MBX_READ_CONFIG:
  438. case MBX_READ_RCONFIG:
  439. case MBX_READ_SPARM:
  440. case MBX_READ_STATUS:
  441. case MBX_READ_RPI:
  442. case MBX_READ_XRI:
  443. case MBX_READ_REV:
  444. case MBX_READ_LNK_STAT:
  445. case MBX_REG_LOGIN:
  446. case MBX_UNREG_LOGIN:
  447. case MBX_READ_LA:
  448. case MBX_CLEAR_LA:
  449. case MBX_DUMP_MEMORY:
  450. case MBX_DUMP_CONTEXT:
  451. case MBX_RUN_DIAGS:
  452. case MBX_RESTART:
  453. case MBX_UPDATE_CFG:
  454. case MBX_DOWN_LOAD:
  455. case MBX_DEL_LD_ENTRY:
  456. case MBX_RUN_PROGRAM:
  457. case MBX_SET_MASK:
  458. case MBX_SET_SLIM:
  459. case MBX_UNREG_D_ID:
  460. case MBX_CONFIG_FARP:
  461. case MBX_LOAD_AREA:
  462. case MBX_RUN_BIU_DIAG64:
  463. case MBX_CONFIG_PORT:
  464. case MBX_READ_SPARM64:
  465. case MBX_READ_RPI64:
  466. case MBX_REG_LOGIN64:
  467. case MBX_READ_LA64:
  468. case MBX_FLASH_WR_ULA:
  469. case MBX_SET_DEBUG:
  470. case MBX_LOAD_EXP_ROM:
  471. ret = mbxCommand;
  472. break;
  473. default:
  474. ret = MBX_SHUTDOWN;
  475. break;
  476. }
  477. return (ret);
  478. }
  479. static void
  480. lpfc_sli_wake_mbox_wait(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
  481. {
  482. wait_queue_head_t *pdone_q;
  483. /*
  484. * If pdone_q is empty, the driver thread gave up waiting and
  485. * continued running.
  486. */
  487. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  488. if (pdone_q)
  489. wake_up_interruptible(pdone_q);
  490. return;
  491. }
  492. void
  493. lpfc_sli_def_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmb)
  494. {
  495. struct lpfc_dmabuf *mp;
  496. mp = (struct lpfc_dmabuf *) (pmb->context1);
  497. if (mp) {
  498. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  499. kfree(mp);
  500. }
  501. mempool_free( pmb, phba->mbox_mem_pool);
  502. return;
  503. }
  504. int
  505. lpfc_sli_handle_mb_event(struct lpfc_hba * phba)
  506. {
  507. MAILBOX_t *mbox;
  508. MAILBOX_t *pmbox;
  509. LPFC_MBOXQ_t *pmb;
  510. struct lpfc_sli *psli;
  511. int i, rc;
  512. uint32_t process_next;
  513. psli = &phba->sli;
  514. /* We should only get here if we are in SLI2 mode */
  515. if (!(phba->sli.sli_flag & LPFC_SLI2_ACTIVE)) {
  516. return (1);
  517. }
  518. phba->sli.slistat.mbox_event++;
  519. /* Get a Mailbox buffer to setup mailbox commands for callback */
  520. if ((pmb = phba->sli.mbox_active)) {
  521. pmbox = &pmb->mb;
  522. mbox = &phba->slim2p->mbx;
  523. /* First check out the status word */
  524. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof (uint32_t));
  525. /* Sanity check to ensure the host owns the mailbox */
  526. if (pmbox->mbxOwner != OWN_HOST) {
  527. /* Lets try for a while */
  528. for (i = 0; i < 10240; i++) {
  529. /* First copy command data */
  530. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  531. sizeof (uint32_t));
  532. if (pmbox->mbxOwner == OWN_HOST)
  533. goto mbout;
  534. }
  535. /* Stray Mailbox Interrupt, mbxCommand <cmd> mbxStatus
  536. <status> */
  537. lpfc_printf_log(phba,
  538. KERN_ERR,
  539. LOG_MBOX | LOG_SLI,
  540. "%d:0304 Stray Mailbox Interrupt "
  541. "mbxCommand x%x mbxStatus x%x\n",
  542. phba->brd_no,
  543. pmbox->mbxCommand,
  544. pmbox->mbxStatus);
  545. spin_lock_irq(phba->host->host_lock);
  546. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  547. spin_unlock_irq(phba->host->host_lock);
  548. return (1);
  549. }
  550. mbout:
  551. del_timer_sync(&phba->sli.mbox_tmo);
  552. phba->work_hba_events &= ~WORKER_MBOX_TMO;
  553. /*
  554. * It is a fatal error if unknown mbox command completion.
  555. */
  556. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  557. MBX_SHUTDOWN) {
  558. /* Unknow mailbox command compl */
  559. lpfc_printf_log(phba,
  560. KERN_ERR,
  561. LOG_MBOX | LOG_SLI,
  562. "%d:0323 Unknown Mailbox command %x Cmpl\n",
  563. phba->brd_no,
  564. pmbox->mbxCommand);
  565. phba->hba_state = LPFC_HBA_ERROR;
  566. phba->work_hs = HS_FFER3;
  567. lpfc_handle_eratt(phba);
  568. return (0);
  569. }
  570. phba->sli.mbox_active = NULL;
  571. if (pmbox->mbxStatus) {
  572. phba->sli.slistat.mbox_stat_err++;
  573. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  574. /* Mbox cmd cmpl error - RETRYing */
  575. lpfc_printf_log(phba,
  576. KERN_INFO,
  577. LOG_MBOX | LOG_SLI,
  578. "%d:0305 Mbox cmd cmpl error - "
  579. "RETRYing Data: x%x x%x x%x x%x\n",
  580. phba->brd_no,
  581. pmbox->mbxCommand,
  582. pmbox->mbxStatus,
  583. pmbox->un.varWords[0],
  584. phba->hba_state);
  585. pmbox->mbxStatus = 0;
  586. pmbox->mbxOwner = OWN_HOST;
  587. spin_lock_irq(phba->host->host_lock);
  588. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  589. spin_unlock_irq(phba->host->host_lock);
  590. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  591. if (rc == MBX_SUCCESS)
  592. return (0);
  593. }
  594. }
  595. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  596. lpfc_printf_log(phba,
  597. KERN_INFO,
  598. LOG_MBOX | LOG_SLI,
  599. "%d:0307 Mailbox cmd x%x Cmpl x%p "
  600. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
  601. phba->brd_no,
  602. pmbox->mbxCommand,
  603. pmb->mbox_cmpl,
  604. *((uint32_t *) pmbox),
  605. pmbox->un.varWords[0],
  606. pmbox->un.varWords[1],
  607. pmbox->un.varWords[2],
  608. pmbox->un.varWords[3],
  609. pmbox->un.varWords[4],
  610. pmbox->un.varWords[5],
  611. pmbox->un.varWords[6],
  612. pmbox->un.varWords[7]);
  613. if (pmb->mbox_cmpl) {
  614. lpfc_sli_pcimem_bcopy(mbox, pmbox, MAILBOX_CMD_SIZE);
  615. pmb->mbox_cmpl(phba,pmb);
  616. }
  617. }
  618. do {
  619. process_next = 0; /* by default don't loop */
  620. spin_lock_irq(phba->host->host_lock);
  621. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  622. /* Process next mailbox command if there is one */
  623. if ((pmb = lpfc_mbox_get(phba))) {
  624. spin_unlock_irq(phba->host->host_lock);
  625. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  626. if (rc == MBX_NOT_FINISHED) {
  627. pmb->mb.mbxStatus = MBX_NOT_FINISHED;
  628. pmb->mbox_cmpl(phba,pmb);
  629. process_next = 1;
  630. continue; /* loop back */
  631. }
  632. } else {
  633. spin_unlock_irq(phba->host->host_lock);
  634. /* Turn on IOCB processing */
  635. for (i = 0; i < phba->sli.num_rings; i++) {
  636. lpfc_sli_turn_on_ring(phba, i);
  637. }
  638. /* Free any lpfc_dmabuf's waiting for mbox cmd cmpls */
  639. while (!list_empty(&phba->freebufList)) {
  640. struct lpfc_dmabuf *mp;
  641. mp = NULL;
  642. list_remove_head((&phba->freebufList),
  643. mp,
  644. struct lpfc_dmabuf,
  645. list);
  646. if (mp) {
  647. lpfc_mbuf_free(phba, mp->virt,
  648. mp->phys);
  649. kfree(mp);
  650. }
  651. }
  652. }
  653. } while (process_next);
  654. return (0);
  655. }
  656. static int
  657. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  658. struct lpfc_iocbq *saveq)
  659. {
  660. IOCB_t * irsp;
  661. WORD5 * w5p;
  662. uint32_t Rctl, Type;
  663. uint32_t match, i;
  664. match = 0;
  665. irsp = &(saveq->iocb);
  666. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX)
  667. || (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX)) {
  668. Rctl = FC_ELS_REQ;
  669. Type = FC_ELS_DATA;
  670. } else {
  671. w5p =
  672. (WORD5 *) & (saveq->iocb.un.
  673. ulpWord[5]);
  674. Rctl = w5p->hcsw.Rctl;
  675. Type = w5p->hcsw.Type;
  676. /* Firmware Workaround */
  677. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  678. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX)) {
  679. Rctl = FC_ELS_REQ;
  680. Type = FC_ELS_DATA;
  681. w5p->hcsw.Rctl = Rctl;
  682. w5p->hcsw.Type = Type;
  683. }
  684. }
  685. /* unSolicited Responses */
  686. if (pring->prt[0].profile) {
  687. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring, saveq);
  688. match = 1;
  689. } else {
  690. /* We must search, based on rctl / type
  691. for the right routine */
  692. for (i = 0; i < pring->num_mask;
  693. i++) {
  694. if ((pring->prt[i].rctl ==
  695. Rctl)
  696. && (pring->prt[i].
  697. type == Type)) {
  698. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  699. (phba, pring, saveq);
  700. match = 1;
  701. break;
  702. }
  703. }
  704. }
  705. if (match == 0) {
  706. /* Unexpected Rctl / Type received */
  707. /* Ring <ringno> handler: unexpected
  708. Rctl <Rctl> Type <Type> received */
  709. lpfc_printf_log(phba,
  710. KERN_WARNING,
  711. LOG_SLI,
  712. "%d:0313 Ring %d handler: unexpected Rctl x%x "
  713. "Type x%x received \n",
  714. phba->brd_no,
  715. pring->ringno,
  716. Rctl,
  717. Type);
  718. }
  719. return(1);
  720. }
  721. static struct lpfc_iocbq *
  722. lpfc_sli_iocbq_lookup(struct lpfc_hba * phba,
  723. struct lpfc_sli_ring * pring,
  724. struct lpfc_iocbq * prspiocb)
  725. {
  726. struct lpfc_iocbq *cmd_iocb = NULL;
  727. uint16_t iotag;
  728. iotag = prspiocb->iocb.ulpIoTag;
  729. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  730. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  731. list_del(&cmd_iocb->list);
  732. pring->txcmplq_cnt--;
  733. return cmd_iocb;
  734. }
  735. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  736. "%d:0317 iotag x%x is out off "
  737. "range: max iotag x%x wd0 x%x\n",
  738. phba->brd_no, iotag,
  739. phba->sli.last_iotag,
  740. *(((uint32_t *) &prspiocb->iocb) + 7));
  741. return NULL;
  742. }
  743. static int
  744. lpfc_sli_process_sol_iocb(struct lpfc_hba * phba, struct lpfc_sli_ring * pring,
  745. struct lpfc_iocbq *saveq)
  746. {
  747. struct lpfc_iocbq * cmdiocbp;
  748. int rc = 1;
  749. unsigned long iflag;
  750. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  751. spin_lock_irqsave(phba->host->host_lock, iflag);
  752. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  753. if (cmdiocbp) {
  754. if (cmdiocbp->iocb_cmpl) {
  755. /*
  756. * Post all ELS completions to the worker thread.
  757. * All other are passed to the completion callback.
  758. */
  759. if (pring->ringno == LPFC_ELS_RING) {
  760. spin_unlock_irqrestore(phba->host->host_lock,
  761. iflag);
  762. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  763. spin_lock_irqsave(phba->host->host_lock, iflag);
  764. }
  765. else {
  766. spin_unlock_irqrestore(phba->host->host_lock,
  767. iflag);
  768. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  769. spin_lock_irqsave(phba->host->host_lock, iflag);
  770. }
  771. } else
  772. lpfc_sli_release_iocbq(phba, cmdiocbp);
  773. } else {
  774. /*
  775. * Unknown initiating command based on the response iotag.
  776. * This could be the case on the ELS ring because of
  777. * lpfc_els_abort().
  778. */
  779. if (pring->ringno != LPFC_ELS_RING) {
  780. /*
  781. * Ring <ringno> handler: unexpected completion IoTag
  782. * <IoTag>
  783. */
  784. lpfc_printf_log(phba,
  785. KERN_WARNING,
  786. LOG_SLI,
  787. "%d:0322 Ring %d handler: unexpected "
  788. "completion IoTag x%x Data: x%x x%x x%x x%x\n",
  789. phba->brd_no,
  790. pring->ringno,
  791. saveq->iocb.ulpIoTag,
  792. saveq->iocb.ulpStatus,
  793. saveq->iocb.un.ulpWord[4],
  794. saveq->iocb.ulpCommand,
  795. saveq->iocb.ulpContext);
  796. }
  797. }
  798. spin_unlock_irqrestore(phba->host->host_lock, iflag);
  799. return rc;
  800. }
  801. static void lpfc_sli_rsp_pointers_error(struct lpfc_hba * phba,
  802. struct lpfc_sli_ring * pring)
  803. {
  804. struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[pring->ringno];
  805. /*
  806. * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
  807. * rsp ring <portRspMax>
  808. */
  809. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  810. "%d:0312 Ring %d handler: portRspPut %d "
  811. "is bigger then rsp ring %d\n",
  812. phba->brd_no, pring->ringno,
  813. le32_to_cpu(pgp->rspPutInx),
  814. pring->numRiocb);
  815. phba->hba_state = LPFC_HBA_ERROR;
  816. /*
  817. * All error attention handlers are posted to
  818. * worker thread
  819. */
  820. phba->work_ha |= HA_ERATT;
  821. phba->work_hs = HS_FFER3;
  822. if (phba->work_wait)
  823. wake_up(phba->work_wait);
  824. return;
  825. }
  826. void lpfc_sli_poll_fcp_ring(struct lpfc_hba * phba)
  827. {
  828. struct lpfc_sli * psli = &phba->sli;
  829. struct lpfc_sli_ring * pring = &psli->ring[LPFC_FCP_RING];
  830. IOCB_t *irsp = NULL;
  831. IOCB_t *entry = NULL;
  832. struct lpfc_iocbq *cmdiocbq = NULL;
  833. struct lpfc_iocbq rspiocbq;
  834. struct lpfc_pgp *pgp;
  835. uint32_t status;
  836. uint32_t portRspPut, portRspMax;
  837. int type;
  838. uint32_t rsp_cmpl = 0;
  839. void __iomem *to_slim;
  840. uint32_t ha_copy;
  841. pring->stats.iocb_event++;
  842. /* The driver assumes SLI-2 mode */
  843. pgp = &phba->slim2p->mbx.us.s2.port[pring->ringno];
  844. /*
  845. * The next available response entry should never exceed the maximum
  846. * entries. If it does, treat it as an adapter hardware error.
  847. */
  848. portRspMax = pring->numRiocb;
  849. portRspPut = le32_to_cpu(pgp->rspPutInx);
  850. if (unlikely(portRspPut >= portRspMax)) {
  851. lpfc_sli_rsp_pointers_error(phba, pring);
  852. return;
  853. }
  854. rmb();
  855. while (pring->rspidx != portRspPut) {
  856. entry = IOCB_ENTRY(pring->rspringaddr, pring->rspidx);
  857. if (++pring->rspidx >= portRspMax)
  858. pring->rspidx = 0;
  859. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  860. (uint32_t *) &rspiocbq.iocb,
  861. sizeof (IOCB_t));
  862. irsp = &rspiocbq.iocb;
  863. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  864. pring->stats.iocb_rsp++;
  865. rsp_cmpl++;
  866. if (unlikely(irsp->ulpStatus)) {
  867. /* Rsp ring <ringno> error: IOCB */
  868. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  869. "%d:0326 Rsp Ring %d error: IOCB Data: "
  870. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  871. phba->brd_no, pring->ringno,
  872. irsp->un.ulpWord[0],
  873. irsp->un.ulpWord[1],
  874. irsp->un.ulpWord[2],
  875. irsp->un.ulpWord[3],
  876. irsp->un.ulpWord[4],
  877. irsp->un.ulpWord[5],
  878. *(((uint32_t *) irsp) + 6),
  879. *(((uint32_t *) irsp) + 7));
  880. }
  881. switch (type) {
  882. case LPFC_ABORT_IOCB:
  883. case LPFC_SOL_IOCB:
  884. /*
  885. * Idle exchange closed via ABTS from port. No iocb
  886. * resources need to be recovered.
  887. */
  888. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  889. printk(KERN_INFO "%s: IOCB cmd 0x%x processed."
  890. " Skipping completion\n", __FUNCTION__,
  891. irsp->ulpCommand);
  892. break;
  893. }
  894. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  895. &rspiocbq);
  896. if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
  897. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  898. &rspiocbq);
  899. }
  900. break;
  901. default:
  902. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  903. char adaptermsg[LPFC_MAX_ADPTMSG];
  904. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  905. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  906. MAX_MSG_DATA);
  907. dev_warn(&((phba->pcidev)->dev), "lpfc%d: %s",
  908. phba->brd_no, adaptermsg);
  909. } else {
  910. /* Unknown IOCB command */
  911. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  912. "%d:0321 Unknown IOCB command "
  913. "Data: x%x, x%x x%x x%x x%x\n",
  914. phba->brd_no, type,
  915. irsp->ulpCommand,
  916. irsp->ulpStatus,
  917. irsp->ulpIoTag,
  918. irsp->ulpContext);
  919. }
  920. break;
  921. }
  922. /*
  923. * The response IOCB has been processed. Update the ring
  924. * pointer in SLIM. If the port response put pointer has not
  925. * been updated, sync the pgp->rspPutInx and fetch the new port
  926. * response put pointer.
  927. */
  928. to_slim = phba->MBslimaddr +
  929. (SLIMOFF + (pring->ringno * 2) + 1) * 4;
  930. writeb(pring->rspidx, to_slim);
  931. if (pring->rspidx == portRspPut)
  932. portRspPut = le32_to_cpu(pgp->rspPutInx);
  933. }
  934. ha_copy = readl(phba->HAregaddr);
  935. ha_copy >>= (LPFC_FCP_RING * 4);
  936. if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
  937. pring->stats.iocb_rsp_full++;
  938. status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
  939. writel(status, phba->CAregaddr);
  940. readl(phba->CAregaddr);
  941. }
  942. if ((ha_copy & HA_R0CE_RSP) &&
  943. (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  944. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  945. pring->stats.iocb_cmd_empty++;
  946. /* Force update of the local copy of cmdGetInx */
  947. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  948. lpfc_sli_resume_iocb(phba, pring);
  949. if ((pring->lpfc_sli_cmd_available))
  950. (pring->lpfc_sli_cmd_available) (phba, pring);
  951. }
  952. return;
  953. }
  954. /*
  955. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  956. * to check it explicitly.
  957. */
  958. static int
  959. lpfc_sli_handle_fast_ring_event(struct lpfc_hba * phba,
  960. struct lpfc_sli_ring * pring, uint32_t mask)
  961. {
  962. struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[pring->ringno];
  963. IOCB_t *irsp = NULL;
  964. IOCB_t *entry = NULL;
  965. struct lpfc_iocbq *cmdiocbq = NULL;
  966. struct lpfc_iocbq rspiocbq;
  967. uint32_t status;
  968. uint32_t portRspPut, portRspMax;
  969. int rc = 1;
  970. lpfc_iocb_type type;
  971. unsigned long iflag;
  972. uint32_t rsp_cmpl = 0;
  973. void __iomem *to_slim;
  974. spin_lock_irqsave(phba->host->host_lock, iflag);
  975. pring->stats.iocb_event++;
  976. /*
  977. * The next available response entry should never exceed the maximum
  978. * entries. If it does, treat it as an adapter hardware error.
  979. */
  980. portRspMax = pring->numRiocb;
  981. portRspPut = le32_to_cpu(pgp->rspPutInx);
  982. if (unlikely(portRspPut >= portRspMax)) {
  983. lpfc_sli_rsp_pointers_error(phba, pring);
  984. spin_unlock_irqrestore(phba->host->host_lock, iflag);
  985. return 1;
  986. }
  987. rmb();
  988. while (pring->rspidx != portRspPut) {
  989. /*
  990. * Fetch an entry off the ring and copy it into a local data
  991. * structure. The copy involves a byte-swap since the
  992. * network byte order and pci byte orders are different.
  993. */
  994. entry = IOCB_ENTRY(pring->rspringaddr, pring->rspidx);
  995. if (++pring->rspidx >= portRspMax)
  996. pring->rspidx = 0;
  997. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  998. (uint32_t *) &rspiocbq.iocb,
  999. sizeof (IOCB_t));
  1000. irsp = &rspiocbq.iocb;
  1001. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  1002. pring->stats.iocb_rsp++;
  1003. rsp_cmpl++;
  1004. if (unlikely(irsp->ulpStatus)) {
  1005. /* Rsp ring <ringno> error: IOCB */
  1006. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1007. "%d:0326 Rsp Ring %d error: IOCB Data: "
  1008. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  1009. phba->brd_no, pring->ringno,
  1010. irsp->un.ulpWord[0], irsp->un.ulpWord[1],
  1011. irsp->un.ulpWord[2], irsp->un.ulpWord[3],
  1012. irsp->un.ulpWord[4], irsp->un.ulpWord[5],
  1013. *(((uint32_t *) irsp) + 6),
  1014. *(((uint32_t *) irsp) + 7));
  1015. }
  1016. switch (type) {
  1017. case LPFC_ABORT_IOCB:
  1018. case LPFC_SOL_IOCB:
  1019. /*
  1020. * Idle exchange closed via ABTS from port. No iocb
  1021. * resources need to be recovered.
  1022. */
  1023. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  1024. printk(KERN_INFO "%s: IOCB cmd 0x%x processed. "
  1025. "Skipping completion\n", __FUNCTION__,
  1026. irsp->ulpCommand);
  1027. break;
  1028. }
  1029. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  1030. &rspiocbq);
  1031. if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
  1032. spin_unlock_irqrestore(
  1033. phba->host->host_lock, iflag);
  1034. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  1035. &rspiocbq);
  1036. spin_lock_irqsave(phba->host->host_lock,
  1037. iflag);
  1038. }
  1039. break;
  1040. default:
  1041. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  1042. char adaptermsg[LPFC_MAX_ADPTMSG];
  1043. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  1044. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  1045. MAX_MSG_DATA);
  1046. dev_warn(&((phba->pcidev)->dev), "lpfc%d: %s",
  1047. phba->brd_no, adaptermsg);
  1048. } else {
  1049. /* Unknown IOCB command */
  1050. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1051. "%d:0321 Unknown IOCB command "
  1052. "Data: x%x, x%x x%x x%x x%x\n",
  1053. phba->brd_no, type, irsp->ulpCommand,
  1054. irsp->ulpStatus, irsp->ulpIoTag,
  1055. irsp->ulpContext);
  1056. }
  1057. break;
  1058. }
  1059. /*
  1060. * The response IOCB has been processed. Update the ring
  1061. * pointer in SLIM. If the port response put pointer has not
  1062. * been updated, sync the pgp->rspPutInx and fetch the new port
  1063. * response put pointer.
  1064. */
  1065. to_slim = phba->MBslimaddr +
  1066. (SLIMOFF + (pring->ringno * 2) + 1) * 4;
  1067. writel(pring->rspidx, to_slim);
  1068. if (pring->rspidx == portRspPut)
  1069. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1070. }
  1071. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  1072. pring->stats.iocb_rsp_full++;
  1073. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  1074. writel(status, phba->CAregaddr);
  1075. readl(phba->CAregaddr);
  1076. }
  1077. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  1078. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  1079. pring->stats.iocb_cmd_empty++;
  1080. /* Force update of the local copy of cmdGetInx */
  1081. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1082. lpfc_sli_resume_iocb(phba, pring);
  1083. if ((pring->lpfc_sli_cmd_available))
  1084. (pring->lpfc_sli_cmd_available) (phba, pring);
  1085. }
  1086. spin_unlock_irqrestore(phba->host->host_lock, iflag);
  1087. return rc;
  1088. }
  1089. int
  1090. lpfc_sli_handle_slow_ring_event(struct lpfc_hba * phba,
  1091. struct lpfc_sli_ring * pring, uint32_t mask)
  1092. {
  1093. IOCB_t *entry;
  1094. IOCB_t *irsp = NULL;
  1095. struct lpfc_iocbq *rspiocbp = NULL;
  1096. struct lpfc_iocbq *next_iocb;
  1097. struct lpfc_iocbq *cmdiocbp;
  1098. struct lpfc_iocbq *saveq;
  1099. struct lpfc_pgp *pgp = &phba->slim2p->mbx.us.s2.port[pring->ringno];
  1100. uint8_t iocb_cmd_type;
  1101. lpfc_iocb_type type;
  1102. uint32_t status, free_saveq;
  1103. uint32_t portRspPut, portRspMax;
  1104. int rc = 1;
  1105. unsigned long iflag;
  1106. void __iomem *to_slim;
  1107. spin_lock_irqsave(phba->host->host_lock, iflag);
  1108. pring->stats.iocb_event++;
  1109. /*
  1110. * The next available response entry should never exceed the maximum
  1111. * entries. If it does, treat it as an adapter hardware error.
  1112. */
  1113. portRspMax = pring->numRiocb;
  1114. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1115. if (portRspPut >= portRspMax) {
  1116. /*
  1117. * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
  1118. * rsp ring <portRspMax>
  1119. */
  1120. lpfc_printf_log(phba,
  1121. KERN_ERR,
  1122. LOG_SLI,
  1123. "%d:0312 Ring %d handler: portRspPut %d "
  1124. "is bigger then rsp ring %d\n",
  1125. phba->brd_no,
  1126. pring->ringno, portRspPut, portRspMax);
  1127. phba->hba_state = LPFC_HBA_ERROR;
  1128. spin_unlock_irqrestore(phba->host->host_lock, iflag);
  1129. phba->work_hs = HS_FFER3;
  1130. lpfc_handle_eratt(phba);
  1131. return 1;
  1132. }
  1133. rmb();
  1134. while (pring->rspidx != portRspPut) {
  1135. /*
  1136. * Build a completion list and call the appropriate handler.
  1137. * The process is to get the next available response iocb, get
  1138. * a free iocb from the list, copy the response data into the
  1139. * free iocb, insert to the continuation list, and update the
  1140. * next response index to slim. This process makes response
  1141. * iocb's in the ring available to DMA as fast as possible but
  1142. * pays a penalty for a copy operation. Since the iocb is
  1143. * only 32 bytes, this penalty is considered small relative to
  1144. * the PCI reads for register values and a slim write. When
  1145. * the ulpLe field is set, the entire Command has been
  1146. * received.
  1147. */
  1148. entry = IOCB_ENTRY(pring->rspringaddr, pring->rspidx);
  1149. rspiocbp = lpfc_sli_get_iocbq(phba);
  1150. if (rspiocbp == NULL) {
  1151. printk(KERN_ERR "%s: out of buffers! Failing "
  1152. "completion.\n", __FUNCTION__);
  1153. break;
  1154. }
  1155. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb, sizeof (IOCB_t));
  1156. irsp = &rspiocbp->iocb;
  1157. if (++pring->rspidx >= portRspMax)
  1158. pring->rspidx = 0;
  1159. to_slim = phba->MBslimaddr + (SLIMOFF + (pring->ringno * 2)
  1160. + 1) * 4;
  1161. writel(pring->rspidx, to_slim);
  1162. if (list_empty(&(pring->iocb_continueq))) {
  1163. list_add(&rspiocbp->list, &(pring->iocb_continueq));
  1164. } else {
  1165. list_add_tail(&rspiocbp->list,
  1166. &(pring->iocb_continueq));
  1167. }
  1168. pring->iocb_continueq_cnt++;
  1169. if (irsp->ulpLe) {
  1170. /*
  1171. * By default, the driver expects to free all resources
  1172. * associated with this iocb completion.
  1173. */
  1174. free_saveq = 1;
  1175. saveq = list_get_first(&pring->iocb_continueq,
  1176. struct lpfc_iocbq, list);
  1177. irsp = &(saveq->iocb);
  1178. list_del_init(&pring->iocb_continueq);
  1179. pring->iocb_continueq_cnt = 0;
  1180. pring->stats.iocb_rsp++;
  1181. if (irsp->ulpStatus) {
  1182. /* Rsp ring <ringno> error: IOCB */
  1183. lpfc_printf_log(phba,
  1184. KERN_WARNING,
  1185. LOG_SLI,
  1186. "%d:0328 Rsp Ring %d error: IOCB Data: "
  1187. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  1188. phba->brd_no,
  1189. pring->ringno,
  1190. irsp->un.ulpWord[0],
  1191. irsp->un.ulpWord[1],
  1192. irsp->un.ulpWord[2],
  1193. irsp->un.ulpWord[3],
  1194. irsp->un.ulpWord[4],
  1195. irsp->un.ulpWord[5],
  1196. *(((uint32_t *) irsp) + 6),
  1197. *(((uint32_t *) irsp) + 7));
  1198. }
  1199. /*
  1200. * Fetch the IOCB command type and call the correct
  1201. * completion routine. Solicited and Unsolicited
  1202. * IOCBs on the ELS ring get freed back to the
  1203. * lpfc_iocb_list by the discovery kernel thread.
  1204. */
  1205. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  1206. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  1207. if (type == LPFC_SOL_IOCB) {
  1208. spin_unlock_irqrestore(phba->host->host_lock,
  1209. iflag);
  1210. rc = lpfc_sli_process_sol_iocb(phba, pring,
  1211. saveq);
  1212. spin_lock_irqsave(phba->host->host_lock, iflag);
  1213. } else if (type == LPFC_UNSOL_IOCB) {
  1214. spin_unlock_irqrestore(phba->host->host_lock,
  1215. iflag);
  1216. rc = lpfc_sli_process_unsol_iocb(phba, pring,
  1217. saveq);
  1218. spin_lock_irqsave(phba->host->host_lock, iflag);
  1219. } else if (type == LPFC_ABORT_IOCB) {
  1220. if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
  1221. ((cmdiocbp =
  1222. lpfc_sli_iocbq_lookup(phba, pring,
  1223. saveq)))) {
  1224. /* Call the specified completion
  1225. routine */
  1226. if (cmdiocbp->iocb_cmpl) {
  1227. spin_unlock_irqrestore(
  1228. phba->host->host_lock,
  1229. iflag);
  1230. (cmdiocbp->iocb_cmpl) (phba,
  1231. cmdiocbp, saveq);
  1232. spin_lock_irqsave(
  1233. phba->host->host_lock,
  1234. iflag);
  1235. } else
  1236. lpfc_sli_release_iocbq(phba,
  1237. cmdiocbp);
  1238. }
  1239. } else if (type == LPFC_UNKNOWN_IOCB) {
  1240. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  1241. char adaptermsg[LPFC_MAX_ADPTMSG];
  1242. memset(adaptermsg, 0,
  1243. LPFC_MAX_ADPTMSG);
  1244. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  1245. MAX_MSG_DATA);
  1246. dev_warn(&((phba->pcidev)->dev),
  1247. "lpfc%d: %s",
  1248. phba->brd_no, adaptermsg);
  1249. } else {
  1250. /* Unknown IOCB command */
  1251. lpfc_printf_log(phba,
  1252. KERN_ERR,
  1253. LOG_SLI,
  1254. "%d:0321 Unknown IOCB command "
  1255. "Data: x%x x%x x%x x%x\n",
  1256. phba->brd_no,
  1257. irsp->ulpCommand,
  1258. irsp->ulpStatus,
  1259. irsp->ulpIoTag,
  1260. irsp->ulpContext);
  1261. }
  1262. }
  1263. if (free_saveq) {
  1264. if (!list_empty(&saveq->list)) {
  1265. list_for_each_entry_safe(rspiocbp,
  1266. next_iocb,
  1267. &saveq->list,
  1268. list) {
  1269. lpfc_sli_release_iocbq(phba,
  1270. rspiocbp);
  1271. }
  1272. }
  1273. lpfc_sli_release_iocbq(phba, saveq);
  1274. }
  1275. }
  1276. /*
  1277. * If the port response put pointer has not been updated, sync
  1278. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  1279. * response put pointer.
  1280. */
  1281. if (pring->rspidx == portRspPut) {
  1282. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1283. }
  1284. } /* while (pring->rspidx != portRspPut) */
  1285. if ((rspiocbp != 0) && (mask & HA_R0RE_REQ)) {
  1286. /* At least one response entry has been freed */
  1287. pring->stats.iocb_rsp_full++;
  1288. /* SET RxRE_RSP in Chip Att register */
  1289. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  1290. writel(status, phba->CAregaddr);
  1291. readl(phba->CAregaddr); /* flush */
  1292. }
  1293. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  1294. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  1295. pring->stats.iocb_cmd_empty++;
  1296. /* Force update of the local copy of cmdGetInx */
  1297. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1298. lpfc_sli_resume_iocb(phba, pring);
  1299. if ((pring->lpfc_sli_cmd_available))
  1300. (pring->lpfc_sli_cmd_available) (phba, pring);
  1301. }
  1302. spin_unlock_irqrestore(phba->host->host_lock, iflag);
  1303. return rc;
  1304. }
  1305. int
  1306. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1307. {
  1308. struct lpfc_iocbq *iocb, *next_iocb;
  1309. IOCB_t *icmd = NULL, *cmd = NULL;
  1310. int errcnt;
  1311. errcnt = 0;
  1312. /* Error everything on txq and txcmplq
  1313. * First do the txq.
  1314. */
  1315. spin_lock_irq(phba->host->host_lock);
  1316. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  1317. list_del_init(&iocb->list);
  1318. if (iocb->iocb_cmpl) {
  1319. icmd = &iocb->iocb;
  1320. icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
  1321. icmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
  1322. spin_unlock_irq(phba->host->host_lock);
  1323. (iocb->iocb_cmpl) (phba, iocb, iocb);
  1324. spin_lock_irq(phba->host->host_lock);
  1325. } else
  1326. lpfc_sli_release_iocbq(phba, iocb);
  1327. }
  1328. pring->txq_cnt = 0;
  1329. INIT_LIST_HEAD(&(pring->txq));
  1330. /* Next issue ABTS for everything on the txcmplq */
  1331. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
  1332. cmd = &iocb->iocb;
  1333. /*
  1334. * Imediate abort of IOCB, deque and call compl
  1335. */
  1336. list_del_init(&iocb->list);
  1337. pring->txcmplq_cnt--;
  1338. if (iocb->iocb_cmpl) {
  1339. cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
  1340. cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
  1341. spin_unlock_irq(phba->host->host_lock);
  1342. (iocb->iocb_cmpl) (phba, iocb, iocb);
  1343. spin_lock_irq(phba->host->host_lock);
  1344. } else
  1345. lpfc_sli_release_iocbq(phba, iocb);
  1346. }
  1347. INIT_LIST_HEAD(&pring->txcmplq);
  1348. pring->txcmplq_cnt = 0;
  1349. spin_unlock_irq(phba->host->host_lock);
  1350. return errcnt;
  1351. }
  1352. /******************************************************************************
  1353. * lpfc_sli_send_reset
  1354. *
  1355. * Note: After returning from this function, the HBA cannot be accessed for
  1356. * 1 ms. Since we do not wish to delay in interrupt context, it is the
  1357. * responsibility of the caller to perform the mdelay(1) and flush via readl().
  1358. ******************************************************************************/
  1359. static int
  1360. lpfc_sli_send_reset(struct lpfc_hba * phba, uint16_t skip_post)
  1361. {
  1362. MAILBOX_t *swpmb;
  1363. volatile uint32_t word0;
  1364. void __iomem *to_slim;
  1365. unsigned long flags = 0;
  1366. spin_lock_irqsave(phba->host->host_lock, flags);
  1367. /* A board reset must use REAL SLIM. */
  1368. phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
  1369. word0 = 0;
  1370. swpmb = (MAILBOX_t *) & word0;
  1371. swpmb->mbxCommand = MBX_RESTART;
  1372. swpmb->mbxHc = 1;
  1373. to_slim = phba->MBslimaddr;
  1374. writel(*(uint32_t *) swpmb, to_slim);
  1375. readl(to_slim); /* flush */
  1376. /* Only skip post after fc_ffinit is completed */
  1377. if (skip_post) {
  1378. word0 = 1; /* This is really setting up word1 */
  1379. } else {
  1380. word0 = 0; /* This is really setting up word1 */
  1381. }
  1382. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  1383. writel(*(uint32_t *) swpmb, to_slim);
  1384. readl(to_slim); /* flush */
  1385. /* Turn off parity checking and serr during the physical reset */
  1386. pci_read_config_word(phba->pcidev, PCI_COMMAND, &phba->pci_cfg_value);
  1387. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  1388. (phba->pci_cfg_value &
  1389. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  1390. writel(HC_INITFF, phba->HCregaddr);
  1391. phba->hba_state = LPFC_INIT_START;
  1392. spin_unlock_irqrestore(phba->host->host_lock, flags);
  1393. return 0;
  1394. }
  1395. static int
  1396. lpfc_sli_brdreset(struct lpfc_hba * phba, uint16_t skip_post)
  1397. {
  1398. struct lpfc_sli_ring *pring;
  1399. int i;
  1400. struct lpfc_dmabuf *mp, *next_mp;
  1401. unsigned long flags = 0;
  1402. lpfc_sli_send_reset(phba, skip_post);
  1403. mdelay(1);
  1404. spin_lock_irqsave(phba->host->host_lock, flags);
  1405. /* Risk the write on flush case ie no delay after the readl */
  1406. readl(phba->HCregaddr); /* flush */
  1407. /* Now toggle INITFF bit set by lpfc_sli_send_reset */
  1408. writel(0, phba->HCregaddr);
  1409. readl(phba->HCregaddr); /* flush */
  1410. /* Restore PCI cmd register */
  1411. pci_write_config_word(phba->pcidev, PCI_COMMAND, phba->pci_cfg_value);
  1412. /* perform board reset */
  1413. phba->fc_eventTag = 0;
  1414. phba->fc_myDID = 0;
  1415. phba->fc_prevDID = Mask_DID;
  1416. /* Reset HBA */
  1417. lpfc_printf_log(phba,
  1418. KERN_INFO,
  1419. LOG_SLI,
  1420. "%d:0325 Reset HBA Data: x%x x%x x%x\n",
  1421. phba->brd_no,
  1422. phba->hba_state,
  1423. phba->sli.sli_flag,
  1424. skip_post);
  1425. /* Initialize relevant SLI info */
  1426. for (i = 0; i < phba->sli.num_rings; i++) {
  1427. pring = &phba->sli.ring[i];
  1428. pring->flag = 0;
  1429. pring->rspidx = 0;
  1430. pring->next_cmdidx = 0;
  1431. pring->local_getidx = 0;
  1432. pring->cmdidx = 0;
  1433. pring->missbufcnt = 0;
  1434. }
  1435. spin_unlock_irqrestore(phba->host->host_lock, flags);
  1436. if (skip_post) {
  1437. mdelay(100);
  1438. } else {
  1439. mdelay(2000);
  1440. }
  1441. spin_lock_irqsave(phba->host->host_lock, flags);
  1442. /* Cleanup preposted buffers on the ELS ring */
  1443. pring = &phba->sli.ring[LPFC_ELS_RING];
  1444. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  1445. list_del(&mp->list);
  1446. pring->postbufq_cnt--;
  1447. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1448. kfree(mp);
  1449. }
  1450. spin_unlock_irqrestore(phba->host->host_lock, flags);
  1451. for (i = 0; i < phba->sli.num_rings; i++)
  1452. lpfc_sli_abort_iocb_ring(phba, &phba->sli.ring[i]);
  1453. return 0;
  1454. }
  1455. static int
  1456. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  1457. {
  1458. uint32_t status, i = 0;
  1459. /* Read the HBA Host Status Register */
  1460. status = readl(phba->HSregaddr);
  1461. /* Check status register to see what current state is */
  1462. i = 0;
  1463. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  1464. /* Check every 100ms for 5 retries, then every 500ms for 5, then
  1465. * every 2.5 sec for 5, then reset board and every 2.5 sec for
  1466. * 4.
  1467. */
  1468. if (i++ >= 20) {
  1469. /* Adapter failed to init, timeout, status reg
  1470. <status> */
  1471. lpfc_printf_log(phba,
  1472. KERN_ERR,
  1473. LOG_INIT,
  1474. "%d:0436 Adapter failed to init, "
  1475. "timeout, status reg x%x\n",
  1476. phba->brd_no,
  1477. status);
  1478. phba->hba_state = LPFC_HBA_ERROR;
  1479. return -ETIMEDOUT;
  1480. }
  1481. /* Check to see if any errors occurred during init */
  1482. if (status & HS_FFERM) {
  1483. /* ERROR: During chipset initialization */
  1484. /* Adapter failed to init, chipset, status reg
  1485. <status> */
  1486. lpfc_printf_log(phba,
  1487. KERN_ERR,
  1488. LOG_INIT,
  1489. "%d:0437 Adapter failed to init, "
  1490. "chipset, status reg x%x\n",
  1491. phba->brd_no,
  1492. status);
  1493. phba->hba_state = LPFC_HBA_ERROR;
  1494. return -EIO;
  1495. }
  1496. if (i <= 5) {
  1497. msleep(10);
  1498. } else if (i <= 10) {
  1499. msleep(500);
  1500. } else {
  1501. msleep(2500);
  1502. }
  1503. if (i == 15) {
  1504. lpfc_sli_brdreset(phba, 0);
  1505. }
  1506. /* Read the HBA Host Status Register */
  1507. status = readl(phba->HSregaddr);
  1508. }
  1509. /* Check to see if any errors occurred during init */
  1510. if (status & HS_FFERM) {
  1511. /* ERROR: During chipset initialization */
  1512. /* Adapter failed to init, chipset, status reg <status> */
  1513. lpfc_printf_log(phba,
  1514. KERN_ERR,
  1515. LOG_INIT,
  1516. "%d:0438 Adapter failed to init, chipset, "
  1517. "status reg x%x\n",
  1518. phba->brd_no,
  1519. status);
  1520. phba->hba_state = LPFC_HBA_ERROR;
  1521. return -EIO;
  1522. }
  1523. /* Clear all interrupt enable conditions */
  1524. writel(0, phba->HCregaddr);
  1525. readl(phba->HCregaddr); /* flush */
  1526. /* setup host attn register */
  1527. writel(0xffffffff, phba->HAregaddr);
  1528. readl(phba->HAregaddr); /* flush */
  1529. return 0;
  1530. }
  1531. int
  1532. lpfc_sli_hba_setup(struct lpfc_hba * phba)
  1533. {
  1534. LPFC_MBOXQ_t *pmb;
  1535. uint32_t resetcount = 0, rc = 0, done = 0;
  1536. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1537. if (!pmb) {
  1538. phba->hba_state = LPFC_HBA_ERROR;
  1539. return -ENOMEM;
  1540. }
  1541. while (resetcount < 2 && !done) {
  1542. phba->hba_state = 0;
  1543. lpfc_sli_brdreset(phba, 0);
  1544. msleep(2500);
  1545. rc = lpfc_sli_chipset_init(phba);
  1546. if (rc)
  1547. break;
  1548. resetcount++;
  1549. /* Call pre CONFIG_PORT mailbox command initialization. A value of 0
  1550. * means the call was successful. Any other nonzero value is a failure,
  1551. * but if ERESTART is returned, the driver may reset the HBA and try
  1552. * again.
  1553. */
  1554. rc = lpfc_config_port_prep(phba);
  1555. if (rc == -ERESTART) {
  1556. phba->hba_state = 0;
  1557. continue;
  1558. } else if (rc) {
  1559. break;
  1560. }
  1561. phba->hba_state = LPFC_INIT_MBX_CMDS;
  1562. lpfc_config_port(phba, pmb);
  1563. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  1564. if (rc == MBX_SUCCESS)
  1565. done = 1;
  1566. else {
  1567. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1568. "%d:0442 Adapter failed to init, mbxCmd x%x "
  1569. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  1570. phba->brd_no, pmb->mb.mbxCommand,
  1571. pmb->mb.mbxStatus, 0);
  1572. phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
  1573. }
  1574. }
  1575. if (!done)
  1576. goto lpfc_sli_hba_setup_error;
  1577. rc = lpfc_sli_ring_map(phba, pmb);
  1578. if (rc)
  1579. goto lpfc_sli_hba_setup_error;
  1580. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  1581. rc = lpfc_config_port_post(phba);
  1582. if (rc)
  1583. goto lpfc_sli_hba_setup_error;
  1584. goto lpfc_sli_hba_setup_exit;
  1585. lpfc_sli_hba_setup_error:
  1586. phba->hba_state = LPFC_HBA_ERROR;
  1587. lpfc_sli_hba_setup_exit:
  1588. mempool_free(pmb, phba->mbox_mem_pool);
  1589. return rc;
  1590. }
  1591. static void
  1592. lpfc_mbox_abort(struct lpfc_hba * phba)
  1593. {
  1594. LPFC_MBOXQ_t *pmbox;
  1595. MAILBOX_t *mb;
  1596. if (phba->sli.mbox_active) {
  1597. del_timer_sync(&phba->sli.mbox_tmo);
  1598. phba->work_hba_events &= ~WORKER_MBOX_TMO;
  1599. pmbox = phba->sli.mbox_active;
  1600. mb = &pmbox->mb;
  1601. phba->sli.mbox_active = NULL;
  1602. if (pmbox->mbox_cmpl) {
  1603. mb->mbxStatus = MBX_NOT_FINISHED;
  1604. (pmbox->mbox_cmpl) (phba, pmbox);
  1605. }
  1606. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  1607. }
  1608. /* Abort all the non active mailbox commands. */
  1609. spin_lock_irq(phba->host->host_lock);
  1610. pmbox = lpfc_mbox_get(phba);
  1611. while (pmbox) {
  1612. mb = &pmbox->mb;
  1613. if (pmbox->mbox_cmpl) {
  1614. mb->mbxStatus = MBX_NOT_FINISHED;
  1615. spin_unlock_irq(phba->host->host_lock);
  1616. (pmbox->mbox_cmpl) (phba, pmbox);
  1617. spin_lock_irq(phba->host->host_lock);
  1618. }
  1619. pmbox = lpfc_mbox_get(phba);
  1620. }
  1621. spin_unlock_irq(phba->host->host_lock);
  1622. return;
  1623. }
  1624. /*! lpfc_mbox_timeout
  1625. *
  1626. * \pre
  1627. * \post
  1628. * \param hba Pointer to per struct lpfc_hba structure
  1629. * \param l1 Pointer to the driver's mailbox queue.
  1630. * \return
  1631. * void
  1632. *
  1633. * \b Description:
  1634. *
  1635. * This routine handles mailbox timeout events at timer interrupt context.
  1636. */
  1637. void
  1638. lpfc_mbox_timeout(unsigned long ptr)
  1639. {
  1640. struct lpfc_hba *phba;
  1641. unsigned long iflag;
  1642. phba = (struct lpfc_hba *)ptr;
  1643. spin_lock_irqsave(phba->host->host_lock, iflag);
  1644. if (!(phba->work_hba_events & WORKER_MBOX_TMO)) {
  1645. phba->work_hba_events |= WORKER_MBOX_TMO;
  1646. if (phba->work_wait)
  1647. wake_up(phba->work_wait);
  1648. }
  1649. spin_unlock_irqrestore(phba->host->host_lock, iflag);
  1650. }
  1651. void
  1652. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  1653. {
  1654. LPFC_MBOXQ_t *pmbox;
  1655. MAILBOX_t *mb;
  1656. spin_lock_irq(phba->host->host_lock);
  1657. if (!(phba->work_hba_events & WORKER_MBOX_TMO)) {
  1658. spin_unlock_irq(phba->host->host_lock);
  1659. return;
  1660. }
  1661. phba->work_hba_events &= ~WORKER_MBOX_TMO;
  1662. pmbox = phba->sli.mbox_active;
  1663. mb = &pmbox->mb;
  1664. /* Mbox cmd <mbxCommand> timeout */
  1665. lpfc_printf_log(phba,
  1666. KERN_ERR,
  1667. LOG_MBOX | LOG_SLI,
  1668. "%d:0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  1669. phba->brd_no,
  1670. mb->mbxCommand,
  1671. phba->hba_state,
  1672. phba->sli.sli_flag,
  1673. phba->sli.mbox_active);
  1674. phba->sli.mbox_active = NULL;
  1675. if (pmbox->mbox_cmpl) {
  1676. mb->mbxStatus = MBX_NOT_FINISHED;
  1677. spin_unlock_irq(phba->host->host_lock);
  1678. (pmbox->mbox_cmpl) (phba, pmbox);
  1679. spin_lock_irq(phba->host->host_lock);
  1680. }
  1681. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  1682. spin_unlock_irq(phba->host->host_lock);
  1683. lpfc_mbox_abort(phba);
  1684. return;
  1685. }
  1686. int
  1687. lpfc_sli_issue_mbox(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmbox, uint32_t flag)
  1688. {
  1689. MAILBOX_t *mb;
  1690. struct lpfc_sli *psli;
  1691. uint32_t status, evtctr;
  1692. uint32_t ha_copy;
  1693. int i;
  1694. unsigned long drvr_flag = 0;
  1695. volatile uint32_t word0, ldata;
  1696. void __iomem *to_slim;
  1697. psli = &phba->sli;
  1698. spin_lock_irqsave(phba->host->host_lock, drvr_flag);
  1699. mb = &pmbox->mb;
  1700. status = MBX_SUCCESS;
  1701. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  1702. /* Polling for a mbox command when another one is already active
  1703. * is not allowed in SLI. Also, the driver must have established
  1704. * SLI2 mode to queue and process multiple mbox commands.
  1705. */
  1706. if (flag & MBX_POLL) {
  1707. spin_unlock_irqrestore(phba->host->host_lock,
  1708. drvr_flag);
  1709. /* Mbox command <mbxCommand> cannot issue */
  1710. LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag)
  1711. return (MBX_NOT_FINISHED);
  1712. }
  1713. if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
  1714. spin_unlock_irqrestore(phba->host->host_lock,
  1715. drvr_flag);
  1716. /* Mbox command <mbxCommand> cannot issue */
  1717. LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag)
  1718. return (MBX_NOT_FINISHED);
  1719. }
  1720. /* Handle STOP IOCB processing flag. This is only meaningful
  1721. * if we are not polling for mbox completion.
  1722. */
  1723. if (flag & MBX_STOP_IOCB) {
  1724. flag &= ~MBX_STOP_IOCB;
  1725. /* Now flag each ring */
  1726. for (i = 0; i < psli->num_rings; i++) {
  1727. /* If the ring is active, flag it */
  1728. if (psli->ring[i].cmdringaddr) {
  1729. psli->ring[i].flag |=
  1730. LPFC_STOP_IOCB_MBX;
  1731. }
  1732. }
  1733. }
  1734. /* Another mailbox command is still being processed, queue this
  1735. * command to be processed later.
  1736. */
  1737. lpfc_mbox_put(phba, pmbox);
  1738. /* Mbox cmd issue - BUSY */
  1739. lpfc_printf_log(phba,
  1740. KERN_INFO,
  1741. LOG_MBOX | LOG_SLI,
  1742. "%d:0308 Mbox cmd issue - BUSY Data: x%x x%x x%x x%x\n",
  1743. phba->brd_no,
  1744. mb->mbxCommand,
  1745. phba->hba_state,
  1746. psli->sli_flag,
  1747. flag);
  1748. psli->slistat.mbox_busy++;
  1749. spin_unlock_irqrestore(phba->host->host_lock,
  1750. drvr_flag);
  1751. return (MBX_BUSY);
  1752. }
  1753. /* Handle STOP IOCB processing flag. This is only meaningful
  1754. * if we are not polling for mbox completion.
  1755. */
  1756. if (flag & MBX_STOP_IOCB) {
  1757. flag &= ~MBX_STOP_IOCB;
  1758. if (flag == MBX_NOWAIT) {
  1759. /* Now flag each ring */
  1760. for (i = 0; i < psli->num_rings; i++) {
  1761. /* If the ring is active, flag it */
  1762. if (psli->ring[i].cmdringaddr) {
  1763. psli->ring[i].flag |=
  1764. LPFC_STOP_IOCB_MBX;
  1765. }
  1766. }
  1767. }
  1768. }
  1769. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  1770. /* If we are not polling, we MUST be in SLI2 mode */
  1771. if (flag != MBX_POLL) {
  1772. if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
  1773. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  1774. spin_unlock_irqrestore(phba->host->host_lock,
  1775. drvr_flag);
  1776. /* Mbox command <mbxCommand> cannot issue */
  1777. LOG_MBOX_CANNOT_ISSUE_DATA( phba, mb, psli, flag);
  1778. return (MBX_NOT_FINISHED);
  1779. }
  1780. /* timeout active mbox command */
  1781. mod_timer(&psli->mbox_tmo, jiffies + HZ * LPFC_MBOX_TMO);
  1782. }
  1783. /* Mailbox cmd <cmd> issue */
  1784. lpfc_printf_log(phba,
  1785. KERN_INFO,
  1786. LOG_MBOX | LOG_SLI,
  1787. "%d:0309 Mailbox cmd x%x issue Data: x%x x%x x%x\n",
  1788. phba->brd_no,
  1789. mb->mbxCommand,
  1790. phba->hba_state,
  1791. psli->sli_flag,
  1792. flag);
  1793. psli->slistat.mbox_cmd++;
  1794. evtctr = psli->slistat.mbox_event;
  1795. /* next set own bit for the adapter and copy over command word */
  1796. mb->mbxOwner = OWN_CHIP;
  1797. if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
  1798. /* First copy command data to host SLIM area */
  1799. lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx, MAILBOX_CMD_SIZE);
  1800. } else {
  1801. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  1802. /* copy command data into host mbox for cmpl */
  1803. lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx,
  1804. MAILBOX_CMD_SIZE);
  1805. }
  1806. /* First copy mbox command data to HBA SLIM, skip past first
  1807. word */
  1808. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  1809. lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
  1810. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  1811. /* Next copy over first word, with mbxOwner set */
  1812. ldata = *((volatile uint32_t *)mb);
  1813. to_slim = phba->MBslimaddr;
  1814. writel(ldata, to_slim);
  1815. readl(to_slim); /* flush */
  1816. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  1817. /* switch over to host mailbox */
  1818. psli->sli_flag |= LPFC_SLI2_ACTIVE;
  1819. }
  1820. }
  1821. wmb();
  1822. /* interrupt board to doit right away */
  1823. writel(CA_MBATT, phba->CAregaddr);
  1824. readl(phba->CAregaddr); /* flush */
  1825. switch (flag) {
  1826. case MBX_NOWAIT:
  1827. /* Don't wait for it to finish, just return */
  1828. psli->mbox_active = pmbox;
  1829. break;
  1830. case MBX_POLL:
  1831. i = 0;
  1832. psli->mbox_active = NULL;
  1833. if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
  1834. /* First read mbox status word */
  1835. word0 = *((volatile uint32_t *)&phba->slim2p->mbx);
  1836. word0 = le32_to_cpu(word0);
  1837. } else {
  1838. /* First read mbox status word */
  1839. word0 = readl(phba->MBslimaddr);
  1840. }
  1841. /* Read the HBA Host Attention Register */
  1842. ha_copy = readl(phba->HAregaddr);
  1843. /* Wait for command to complete */
  1844. while (((word0 & OWN_CHIP) == OWN_CHIP)
  1845. || !(ha_copy & HA_MBATT)) {
  1846. if (i++ >= 100) {
  1847. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  1848. spin_unlock_irqrestore(phba->host->host_lock,
  1849. drvr_flag);
  1850. return (MBX_NOT_FINISHED);
  1851. }
  1852. /* Check if we took a mbox interrupt while we were
  1853. polling */
  1854. if (((word0 & OWN_CHIP) != OWN_CHIP)
  1855. && (evtctr != psli->slistat.mbox_event))
  1856. break;
  1857. spin_unlock_irqrestore(phba->host->host_lock,
  1858. drvr_flag);
  1859. /* Can be in interrupt context, do not sleep */
  1860. /* (or might be called with interrupts disabled) */
  1861. mdelay(i);
  1862. spin_lock_irqsave(phba->host->host_lock, drvr_flag);
  1863. if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
  1864. /* First copy command data */
  1865. word0 = *((volatile uint32_t *)
  1866. &phba->slim2p->mbx);
  1867. word0 = le32_to_cpu(word0);
  1868. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  1869. MAILBOX_t *slimmb;
  1870. volatile uint32_t slimword0;
  1871. /* Check real SLIM for any errors */
  1872. slimword0 = readl(phba->MBslimaddr);
  1873. slimmb = (MAILBOX_t *) & slimword0;
  1874. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  1875. && slimmb->mbxStatus) {
  1876. psli->sli_flag &=
  1877. ~LPFC_SLI2_ACTIVE;
  1878. word0 = slimword0;
  1879. }
  1880. }
  1881. } else {
  1882. /* First copy command data */
  1883. word0 = readl(phba->MBslimaddr);
  1884. }
  1885. /* Read the HBA Host Attention Register */
  1886. ha_copy = readl(phba->HAregaddr);
  1887. }
  1888. if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
  1889. /* copy results back to user */
  1890. lpfc_sli_pcimem_bcopy(&phba->slim2p->mbx, mb,
  1891. MAILBOX_CMD_SIZE);
  1892. } else {
  1893. /* First copy command data */
  1894. lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
  1895. MAILBOX_CMD_SIZE);
  1896. if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
  1897. pmbox->context2) {
  1898. lpfc_memcpy_from_slim((void *)pmbox->context2,
  1899. phba->MBslimaddr + DMP_RSP_OFFSET,
  1900. mb->un.varDmp.word_cnt);
  1901. }
  1902. }
  1903. writel(HA_MBATT, phba->HAregaddr);
  1904. readl(phba->HAregaddr); /* flush */
  1905. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  1906. status = mb->mbxStatus;
  1907. }
  1908. spin_unlock_irqrestore(phba->host->host_lock, drvr_flag);
  1909. return (status);
  1910. }
  1911. static int
  1912. lpfc_sli_ringtx_put(struct lpfc_hba * phba, struct lpfc_sli_ring * pring,
  1913. struct lpfc_iocbq * piocb)
  1914. {
  1915. /* Insert the caller's iocb in the txq tail for later processing. */
  1916. list_add_tail(&piocb->list, &pring->txq);
  1917. pring->txq_cnt++;
  1918. return (0);
  1919. }
  1920. static struct lpfc_iocbq *
  1921. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1922. struct lpfc_iocbq ** piocb)
  1923. {
  1924. struct lpfc_iocbq * nextiocb;
  1925. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  1926. if (!nextiocb) {
  1927. nextiocb = *piocb;
  1928. *piocb = NULL;
  1929. }
  1930. return nextiocb;
  1931. }
  1932. int
  1933. lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1934. struct lpfc_iocbq *piocb, uint32_t flag)
  1935. {
  1936. struct lpfc_iocbq *nextiocb;
  1937. IOCB_t *iocb;
  1938. /*
  1939. * We should never get an IOCB if we are in a < LINK_DOWN state
  1940. */
  1941. if (unlikely(phba->hba_state < LPFC_LINK_DOWN))
  1942. return IOCB_ERROR;
  1943. /*
  1944. * Check to see if we are blocking IOCB processing because of a
  1945. * outstanding mbox command.
  1946. */
  1947. if (unlikely(pring->flag & LPFC_STOP_IOCB_MBX))
  1948. goto iocb_busy;
  1949. if (unlikely(phba->hba_state == LPFC_LINK_DOWN)) {
  1950. /*
  1951. * Only CREATE_XRI, CLOSE_XRI, ABORT_XRI, and QUE_RING_BUF
  1952. * can be issued if the link is not up.
  1953. */
  1954. switch (piocb->iocb.ulpCommand) {
  1955. case CMD_QUE_RING_BUF_CN:
  1956. case CMD_QUE_RING_BUF64_CN:
  1957. /*
  1958. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  1959. * completion, iocb_cmpl MUST be 0.
  1960. */
  1961. if (piocb->iocb_cmpl)
  1962. piocb->iocb_cmpl = NULL;
  1963. /*FALLTHROUGH*/
  1964. case CMD_CREATE_XRI_CR:
  1965. break;
  1966. default:
  1967. goto iocb_busy;
  1968. }
  1969. /*
  1970. * For FCP commands, we must be in a state where we can process link
  1971. * attention events.
  1972. */
  1973. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  1974. !(phba->sli.sli_flag & LPFC_PROCESS_LA)))
  1975. goto iocb_busy;
  1976. /*
  1977. * Check to see if this is a high priority command.
  1978. * If so bypass tx queue processing.
  1979. */
  1980. if (unlikely((flag & SLI_IOCB_HIGH_PRIORITY) &&
  1981. (iocb = lpfc_sli_next_iocb_slot(phba, pring)))) {
  1982. lpfc_sli_submit_iocb(phba, pring, iocb, piocb);
  1983. piocb = NULL;
  1984. }
  1985. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  1986. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  1987. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  1988. if (iocb)
  1989. lpfc_sli_update_ring(phba, pring);
  1990. else
  1991. lpfc_sli_update_full_ring(phba, pring);
  1992. if (!piocb)
  1993. return IOCB_SUCCESS;
  1994. goto out_busy;
  1995. iocb_busy:
  1996. pring->stats.iocb_cmd_delay++;
  1997. out_busy:
  1998. if (!(flag & SLI_IOCB_RET_IOCB)) {
  1999. lpfc_sli_ringtx_put(phba, pring, piocb);
  2000. return IOCB_SUCCESS;
  2001. }
  2002. return IOCB_BUSY;
  2003. }
  2004. int
  2005. lpfc_sli_setup(struct lpfc_hba *phba)
  2006. {
  2007. int i, totiocb = 0;
  2008. struct lpfc_sli *psli = &phba->sli;
  2009. struct lpfc_sli_ring *pring;
  2010. psli->num_rings = MAX_CONFIGURED_RINGS;
  2011. psli->sli_flag = 0;
  2012. psli->fcp_ring = LPFC_FCP_RING;
  2013. psli->next_ring = LPFC_FCP_NEXT_RING;
  2014. psli->ip_ring = LPFC_IP_RING;
  2015. psli->iocbq_lookup = NULL;
  2016. psli->iocbq_lookup_len = 0;
  2017. psli->last_iotag = 0;
  2018. for (i = 0; i < psli->num_rings; i++) {
  2019. pring = &psli->ring[i];
  2020. switch (i) {
  2021. case LPFC_FCP_RING: /* ring 0 - FCP */
  2022. /* numCiocb and numRiocb are used in config_port */
  2023. pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  2024. pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  2025. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  2026. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  2027. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  2028. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  2029. pring->iotag_ctr = 0;
  2030. pring->iotag_max =
  2031. (phba->cfg_hba_queue_depth * 2);
  2032. pring->fast_iotag = pring->iotag_max;
  2033. pring->num_mask = 0;
  2034. break;
  2035. case LPFC_IP_RING: /* ring 1 - IP */
  2036. /* numCiocb and numRiocb are used in config_port */
  2037. pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  2038. pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  2039. pring->num_mask = 0;
  2040. break;
  2041. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  2042. /* numCiocb and numRiocb are used in config_port */
  2043. pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  2044. pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  2045. pring->fast_iotag = 0;
  2046. pring->iotag_ctr = 0;
  2047. pring->iotag_max = 4096;
  2048. pring->num_mask = 4;
  2049. pring->prt[0].profile = 0; /* Mask 0 */
  2050. pring->prt[0].rctl = FC_ELS_REQ;
  2051. pring->prt[0].type = FC_ELS_DATA;
  2052. pring->prt[0].lpfc_sli_rcv_unsol_event =
  2053. lpfc_els_unsol_event;
  2054. pring->prt[1].profile = 0; /* Mask 1 */
  2055. pring->prt[1].rctl = FC_ELS_RSP;
  2056. pring->prt[1].type = FC_ELS_DATA;
  2057. pring->prt[1].lpfc_sli_rcv_unsol_event =
  2058. lpfc_els_unsol_event;
  2059. pring->prt[2].profile = 0; /* Mask 2 */
  2060. /* NameServer Inquiry */
  2061. pring->prt[2].rctl = FC_UNSOL_CTL;
  2062. /* NameServer */
  2063. pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
  2064. pring->prt[2].lpfc_sli_rcv_unsol_event =
  2065. lpfc_ct_unsol_event;
  2066. pring->prt[3].profile = 0; /* Mask 3 */
  2067. /* NameServer response */
  2068. pring->prt[3].rctl = FC_SOL_CTL;
  2069. /* NameServer */
  2070. pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
  2071. pring->prt[3].lpfc_sli_rcv_unsol_event =
  2072. lpfc_ct_unsol_event;
  2073. break;
  2074. }
  2075. totiocb += (pring->numCiocb + pring->numRiocb);
  2076. }
  2077. if (totiocb > MAX_SLI2_IOCB) {
  2078. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  2079. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2080. "%d:0462 Too many cmd / rsp ring entries in "
  2081. "SLI2 SLIM Data: x%x x%x\n",
  2082. phba->brd_no, totiocb, MAX_SLI2_IOCB);
  2083. }
  2084. return 0;
  2085. }
  2086. int
  2087. lpfc_sli_queue_setup(struct lpfc_hba * phba)
  2088. {
  2089. struct lpfc_sli *psli;
  2090. struct lpfc_sli_ring *pring;
  2091. int i;
  2092. psli = &phba->sli;
  2093. spin_lock_irq(phba->host->host_lock);
  2094. INIT_LIST_HEAD(&psli->mboxq);
  2095. /* Initialize list headers for txq and txcmplq as double linked lists */
  2096. for (i = 0; i < psli->num_rings; i++) {
  2097. pring = &psli->ring[i];
  2098. pring->ringno = i;
  2099. pring->next_cmdidx = 0;
  2100. pring->local_getidx = 0;
  2101. pring->cmdidx = 0;
  2102. INIT_LIST_HEAD(&pring->txq);
  2103. INIT_LIST_HEAD(&pring->txcmplq);
  2104. INIT_LIST_HEAD(&pring->iocb_continueq);
  2105. INIT_LIST_HEAD(&pring->postbufq);
  2106. }
  2107. spin_unlock_irq(phba->host->host_lock);
  2108. return (1);
  2109. }
  2110. int
  2111. lpfc_sli_hba_down(struct lpfc_hba * phba)
  2112. {
  2113. struct lpfc_sli *psli;
  2114. struct lpfc_sli_ring *pring;
  2115. LPFC_MBOXQ_t *pmb;
  2116. struct lpfc_iocbq *iocb, *next_iocb;
  2117. IOCB_t *icmd = NULL;
  2118. int i;
  2119. unsigned long flags = 0;
  2120. psli = &phba->sli;
  2121. lpfc_hba_down_prep(phba);
  2122. spin_lock_irqsave(phba->host->host_lock, flags);
  2123. for (i = 0; i < psli->num_rings; i++) {
  2124. pring = &psli->ring[i];
  2125. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  2126. /*
  2127. * Error everything on the txq since these iocbs have not been
  2128. * given to the FW yet.
  2129. */
  2130. pring->txq_cnt = 0;
  2131. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  2132. list_del_init(&iocb->list);
  2133. if (iocb->iocb_cmpl) {
  2134. icmd = &iocb->iocb;
  2135. icmd->ulpStatus = IOSTAT_LOCAL_REJECT;
  2136. icmd->un.ulpWord[4] = IOERR_SLI_DOWN;
  2137. spin_unlock_irqrestore(phba->host->host_lock,
  2138. flags);
  2139. (iocb->iocb_cmpl) (phba, iocb, iocb);
  2140. spin_lock_irqsave(phba->host->host_lock, flags);
  2141. } else
  2142. lpfc_sli_release_iocbq(phba, iocb);
  2143. }
  2144. INIT_LIST_HEAD(&(pring->txq));
  2145. kfree(pring->fast_lookup);
  2146. pring->fast_lookup = NULL;
  2147. }
  2148. spin_unlock_irqrestore(phba->host->host_lock, flags);
  2149. /* Return any active mbox cmds */
  2150. del_timer_sync(&psli->mbox_tmo);
  2151. spin_lock_irqsave(phba->host->host_lock, flags);
  2152. phba->work_hba_events &= ~WORKER_MBOX_TMO;
  2153. if (psli->mbox_active) {
  2154. pmb = psli->mbox_active;
  2155. pmb->mb.mbxStatus = MBX_NOT_FINISHED;
  2156. if (pmb->mbox_cmpl) {
  2157. spin_unlock_irqrestore(phba->host->host_lock, flags);
  2158. pmb->mbox_cmpl(phba,pmb);
  2159. spin_lock_irqsave(phba->host->host_lock, flags);
  2160. }
  2161. }
  2162. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  2163. psli->mbox_active = NULL;
  2164. /* Return any pending mbox cmds */
  2165. while ((pmb = lpfc_mbox_get(phba)) != NULL) {
  2166. pmb->mb.mbxStatus = MBX_NOT_FINISHED;
  2167. if (pmb->mbox_cmpl) {
  2168. spin_unlock_irqrestore(phba->host->host_lock, flags);
  2169. pmb->mbox_cmpl(phba,pmb);
  2170. spin_lock_irqsave(phba->host->host_lock, flags);
  2171. }
  2172. }
  2173. INIT_LIST_HEAD(&psli->mboxq);
  2174. spin_unlock_irqrestore(phba->host->host_lock, flags);
  2175. /*
  2176. * Provided the hba is not in an error state, reset it. It is not
  2177. * capable of IO anymore.
  2178. */
  2179. if (phba->hba_state != LPFC_HBA_ERROR) {
  2180. phba->hba_state = LPFC_INIT_START;
  2181. lpfc_sli_brdreset(phba, 1);
  2182. }
  2183. return 1;
  2184. }
  2185. void
  2186. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  2187. {
  2188. uint32_t *src = srcp;
  2189. uint32_t *dest = destp;
  2190. uint32_t ldata;
  2191. int i;
  2192. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  2193. ldata = *src;
  2194. ldata = le32_to_cpu(ldata);
  2195. *dest = ldata;
  2196. src++;
  2197. dest++;
  2198. }
  2199. }
  2200. int
  2201. lpfc_sli_ringpostbuf_put(struct lpfc_hba * phba, struct lpfc_sli_ring * pring,
  2202. struct lpfc_dmabuf * mp)
  2203. {
  2204. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  2205. later */
  2206. list_add_tail(&mp->list, &pring->postbufq);
  2207. pring->postbufq_cnt++;
  2208. return 0;
  2209. }
  2210. struct lpfc_dmabuf *
  2211. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2212. dma_addr_t phys)
  2213. {
  2214. struct lpfc_dmabuf *mp, *next_mp;
  2215. struct list_head *slp = &pring->postbufq;
  2216. /* Search postbufq, from the begining, looking for a match on phys */
  2217. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  2218. if (mp->phys == phys) {
  2219. list_del_init(&mp->list);
  2220. pring->postbufq_cnt--;
  2221. return mp;
  2222. }
  2223. }
  2224. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2225. "%d:0410 Cannot find virtual addr for mapped buf on "
  2226. "ring %d Data x%llx x%p x%p x%x\n",
  2227. phba->brd_no, pring->ringno, (unsigned long long)phys,
  2228. slp->next, slp->prev, pring->postbufq_cnt);
  2229. return NULL;
  2230. }
  2231. static void
  2232. lpfc_sli_abort_elsreq_cmpl(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
  2233. struct lpfc_iocbq * rspiocb)
  2234. {
  2235. struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
  2236. /* Free the resources associated with the ELS_REQUEST64 IOCB the driver
  2237. * just aborted.
  2238. * In this case, context2 = cmd, context2->next = rsp, context3 = bpl
  2239. */
  2240. if (cmdiocb->context2) {
  2241. buf_ptr1 = (struct lpfc_dmabuf *) cmdiocb->context2;
  2242. /* Free the response IOCB before completing the abort
  2243. command. */
  2244. buf_ptr = NULL;
  2245. list_remove_head((&buf_ptr1->list), buf_ptr,
  2246. struct lpfc_dmabuf, list);
  2247. if (buf_ptr) {
  2248. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  2249. kfree(buf_ptr);
  2250. }
  2251. lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
  2252. kfree(buf_ptr1);
  2253. }
  2254. if (cmdiocb->context3) {
  2255. buf_ptr = (struct lpfc_dmabuf *) cmdiocb->context3;
  2256. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  2257. kfree(buf_ptr);
  2258. }
  2259. lpfc_sli_release_iocbq(phba, cmdiocb);
  2260. return;
  2261. }
  2262. int
  2263. lpfc_sli_issue_abort_iotag32(struct lpfc_hba * phba,
  2264. struct lpfc_sli_ring * pring,
  2265. struct lpfc_iocbq * cmdiocb)
  2266. {
  2267. struct lpfc_iocbq *abtsiocbp;
  2268. IOCB_t *icmd = NULL;
  2269. IOCB_t *iabt = NULL;
  2270. /* issue ABTS for this IOCB based on iotag */
  2271. abtsiocbp = lpfc_sli_get_iocbq(phba);
  2272. if (abtsiocbp == NULL)
  2273. return 0;
  2274. iabt = &abtsiocbp->iocb;
  2275. icmd = &cmdiocb->iocb;
  2276. switch (icmd->ulpCommand) {
  2277. case CMD_ELS_REQUEST64_CR:
  2278. /* Even though we abort the ELS command, the firmware may access
  2279. * the BPL or other resources before it processes our
  2280. * ABORT_MXRI64. Thus we must delay reusing the cmdiocb
  2281. * resources till the actual abort request completes.
  2282. */
  2283. abtsiocbp->context1 = (void *)((unsigned long)icmd->ulpCommand);
  2284. abtsiocbp->context2 = cmdiocb->context2;
  2285. abtsiocbp->context3 = cmdiocb->context3;
  2286. cmdiocb->context2 = NULL;
  2287. cmdiocb->context3 = NULL;
  2288. abtsiocbp->iocb_cmpl = lpfc_sli_abort_elsreq_cmpl;
  2289. break;
  2290. default:
  2291. lpfc_sli_release_iocbq(phba, abtsiocbp);
  2292. return 0;
  2293. }
  2294. iabt->un.amxri.abortType = ABORT_TYPE_ABTS;
  2295. iabt->un.amxri.iotag32 = icmd->un.elsreq64.bdl.ulpIoTag32;
  2296. iabt->ulpLe = 1;
  2297. iabt->ulpClass = CLASS3;
  2298. iabt->ulpCommand = CMD_ABORT_MXRI64_CN;
  2299. if (lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0) == IOCB_ERROR) {
  2300. lpfc_sli_release_iocbq(phba, abtsiocbp);
  2301. return 0;
  2302. }
  2303. return 1;
  2304. }
  2305. static int
  2306. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, uint16_t tgt_id,
  2307. uint64_t lun_id, uint32_t ctx,
  2308. lpfc_ctx_cmd ctx_cmd)
  2309. {
  2310. struct lpfc_scsi_buf *lpfc_cmd;
  2311. struct scsi_cmnd *cmnd;
  2312. int rc = 1;
  2313. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  2314. return rc;
  2315. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  2316. cmnd = lpfc_cmd->pCmd;
  2317. if (cmnd == NULL)
  2318. return rc;
  2319. switch (ctx_cmd) {
  2320. case LPFC_CTX_LUN:
  2321. if ((cmnd->device->id == tgt_id) &&
  2322. (cmnd->device->lun == lun_id))
  2323. rc = 0;
  2324. break;
  2325. case LPFC_CTX_TGT:
  2326. if (cmnd->device->id == tgt_id)
  2327. rc = 0;
  2328. break;
  2329. case LPFC_CTX_CTX:
  2330. if (iocbq->iocb.ulpContext == ctx)
  2331. rc = 0;
  2332. break;
  2333. case LPFC_CTX_HOST:
  2334. rc = 0;
  2335. break;
  2336. default:
  2337. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  2338. __FUNCTION__, ctx_cmd);
  2339. break;
  2340. }
  2341. return rc;
  2342. }
  2343. int
  2344. lpfc_sli_sum_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2345. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd ctx_cmd)
  2346. {
  2347. struct lpfc_iocbq *iocbq;
  2348. int sum, i;
  2349. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  2350. iocbq = phba->sli.iocbq_lookup[i];
  2351. if (lpfc_sli_validate_fcp_iocb (iocbq, tgt_id, lun_id,
  2352. 0, ctx_cmd) == 0)
  2353. sum++;
  2354. }
  2355. return sum;
  2356. }
  2357. void
  2358. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba * phba, struct lpfc_iocbq * cmdiocb,
  2359. struct lpfc_iocbq * rspiocb)
  2360. {
  2361. spin_lock_irq(phba->host->host_lock);
  2362. lpfc_sli_release_iocbq(phba, cmdiocb);
  2363. spin_unlock_irq(phba->host->host_lock);
  2364. return;
  2365. }
  2366. int
  2367. lpfc_sli_abort_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2368. uint16_t tgt_id, uint64_t lun_id, uint32_t ctx,
  2369. lpfc_ctx_cmd abort_cmd)
  2370. {
  2371. struct lpfc_iocbq *iocbq;
  2372. struct lpfc_iocbq *abtsiocb;
  2373. IOCB_t *cmd = NULL;
  2374. int errcnt = 0, ret_val = 0;
  2375. int i;
  2376. for (i = 1; i <= phba->sli.last_iotag; i++) {
  2377. iocbq = phba->sli.iocbq_lookup[i];
  2378. if (lpfc_sli_validate_fcp_iocb (iocbq, tgt_id, lun_id,
  2379. 0, abort_cmd) != 0)
  2380. continue;
  2381. /* issue ABTS for this IOCB based on iotag */
  2382. abtsiocb = lpfc_sli_get_iocbq(phba);
  2383. if (abtsiocb == NULL) {
  2384. errcnt++;
  2385. continue;
  2386. }
  2387. cmd = &iocbq->iocb;
  2388. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  2389. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  2390. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  2391. abtsiocb->iocb.ulpLe = 1;
  2392. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  2393. if (phba->hba_state >= LPFC_LINK_UP)
  2394. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  2395. else
  2396. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  2397. /* Setup callback routine and issue the command. */
  2398. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  2399. ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
  2400. if (ret_val == IOCB_ERROR) {
  2401. lpfc_sli_release_iocbq(phba, abtsiocb);
  2402. errcnt++;
  2403. continue;
  2404. }
  2405. }
  2406. return errcnt;
  2407. }
  2408. static void
  2409. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  2410. struct lpfc_iocbq *cmdiocbq,
  2411. struct lpfc_iocbq *rspiocbq)
  2412. {
  2413. wait_queue_head_t *pdone_q;
  2414. unsigned long iflags;
  2415. spin_lock_irqsave(phba->host->host_lock, iflags);
  2416. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  2417. if (cmdiocbq->context2 && rspiocbq)
  2418. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  2419. &rspiocbq->iocb, sizeof(IOCB_t));
  2420. pdone_q = cmdiocbq->context_un.wait_queue;
  2421. spin_unlock_irqrestore(phba->host->host_lock, iflags);
  2422. if (pdone_q)
  2423. wake_up(pdone_q);
  2424. return;
  2425. }
  2426. /*
  2427. * Issue the caller's iocb and wait for its completion, but no longer than the
  2428. * caller's timeout. Note that iocb_flags is cleared before the
  2429. * lpfc_sli_issue_call since the wake routine sets a unique value and by
  2430. * definition this is a wait function.
  2431. */
  2432. int
  2433. lpfc_sli_issue_iocb_wait(struct lpfc_hba * phba,
  2434. struct lpfc_sli_ring * pring,
  2435. struct lpfc_iocbq * piocb,
  2436. struct lpfc_iocbq * prspiocbq,
  2437. uint32_t timeout)
  2438. {
  2439. DECLARE_WAIT_QUEUE_HEAD(done_q);
  2440. long timeleft, timeout_req = 0;
  2441. int retval = IOCB_SUCCESS;
  2442. uint32_t creg_val;
  2443. /*
  2444. * If the caller has provided a response iocbq buffer, then context2
  2445. * is NULL or its an error.
  2446. */
  2447. if (prspiocbq) {
  2448. if (piocb->context2)
  2449. return IOCB_ERROR;
  2450. piocb->context2 = prspiocbq;
  2451. }
  2452. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  2453. piocb->context_un.wait_queue = &done_q;
  2454. piocb->iocb_flag &= ~LPFC_IO_WAKE;
  2455. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  2456. creg_val = readl(phba->HCregaddr);
  2457. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  2458. writel(creg_val, phba->HCregaddr);
  2459. readl(phba->HCregaddr); /* flush */
  2460. }
  2461. retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
  2462. if (retval == IOCB_SUCCESS) {
  2463. timeout_req = timeout * HZ;
  2464. spin_unlock_irq(phba->host->host_lock);
  2465. timeleft = wait_event_timeout(done_q,
  2466. piocb->iocb_flag & LPFC_IO_WAKE,
  2467. timeout_req);
  2468. spin_lock_irq(phba->host->host_lock);
  2469. if (timeleft == 0) {
  2470. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2471. "%d:0329 IOCB wait timeout error - no "
  2472. "wake response Data x%x\n",
  2473. phba->brd_no, timeout);
  2474. retval = IOCB_TIMEDOUT;
  2475. } else if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
  2476. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2477. "%d:0330 IOCB wake NOT set, "
  2478. "Data x%x x%lx\n", phba->brd_no,
  2479. timeout, (timeleft / jiffies));
  2480. retval = IOCB_TIMEDOUT;
  2481. } else {
  2482. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2483. "%d:0331 IOCB wake signaled\n",
  2484. phba->brd_no);
  2485. }
  2486. } else {
  2487. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2488. "%d:0332 IOCB wait issue failed, Data x%x\n",
  2489. phba->brd_no, retval);
  2490. retval = IOCB_ERROR;
  2491. }
  2492. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  2493. creg_val = readl(phba->HCregaddr);
  2494. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  2495. writel(creg_val, phba->HCregaddr);
  2496. readl(phba->HCregaddr); /* flush */
  2497. }
  2498. if (prspiocbq)
  2499. piocb->context2 = NULL;
  2500. piocb->context_un.wait_queue = NULL;
  2501. piocb->iocb_cmpl = NULL;
  2502. return retval;
  2503. }
  2504. int
  2505. lpfc_sli_issue_mbox_wait(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq,
  2506. uint32_t timeout)
  2507. {
  2508. DECLARE_WAIT_QUEUE_HEAD(done_q);
  2509. DECLARE_WAITQUEUE(wq_entry, current);
  2510. uint32_t timeleft = 0;
  2511. int retval;
  2512. /* The caller must leave context1 empty. */
  2513. if (pmboxq->context1 != 0) {
  2514. return (MBX_NOT_FINISHED);
  2515. }
  2516. /* setup wake call as IOCB callback */
  2517. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  2518. /* setup context field to pass wait_queue pointer to wake function */
  2519. pmboxq->context1 = &done_q;
  2520. /* start to sleep before we wait, to avoid races */
  2521. set_current_state(TASK_INTERRUPTIBLE);
  2522. add_wait_queue(&done_q, &wq_entry);
  2523. /* now issue the command */
  2524. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  2525. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  2526. timeleft = schedule_timeout(timeout * HZ);
  2527. pmboxq->context1 = NULL;
  2528. /* if schedule_timeout returns 0, we timed out and were not
  2529. woken up */
  2530. if (timeleft == 0) {
  2531. retval = MBX_TIMEOUT;
  2532. } else {
  2533. retval = MBX_SUCCESS;
  2534. }
  2535. }
  2536. set_current_state(TASK_RUNNING);
  2537. remove_wait_queue(&done_q, &wq_entry);
  2538. return retval;
  2539. }
  2540. irqreturn_t
  2541. lpfc_intr_handler(int irq, void *dev_id, struct pt_regs * regs)
  2542. {
  2543. struct lpfc_hba *phba;
  2544. uint32_t ha_copy;
  2545. uint32_t work_ha_copy;
  2546. unsigned long status;
  2547. int i;
  2548. uint32_t control;
  2549. /*
  2550. * Get the driver's phba structure from the dev_id and
  2551. * assume the HBA is not interrupting.
  2552. */
  2553. phba = (struct lpfc_hba *) dev_id;
  2554. if (unlikely(!phba))
  2555. return IRQ_NONE;
  2556. phba->sli.slistat.sli_intr++;
  2557. /*
  2558. * Call the HBA to see if it is interrupting. If not, don't claim
  2559. * the interrupt
  2560. */
  2561. /* Ignore all interrupts during initialization. */
  2562. if (unlikely(phba->hba_state < LPFC_LINK_DOWN))
  2563. return IRQ_NONE;
  2564. /*
  2565. * Read host attention register to determine interrupt source
  2566. * Clear Attention Sources, except Error Attention (to
  2567. * preserve status) and Link Attention
  2568. */
  2569. spin_lock(phba->host->host_lock);
  2570. ha_copy = readl(phba->HAregaddr);
  2571. writel((ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  2572. readl(phba->HAregaddr); /* flush */
  2573. spin_unlock(phba->host->host_lock);
  2574. if (unlikely(!ha_copy))
  2575. return IRQ_NONE;
  2576. work_ha_copy = ha_copy & phba->work_ha_mask;
  2577. if (unlikely(work_ha_copy)) {
  2578. if (work_ha_copy & HA_LATT) {
  2579. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  2580. /*
  2581. * Turn off Link Attention interrupts
  2582. * until CLEAR_LA done
  2583. */
  2584. spin_lock(phba->host->host_lock);
  2585. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  2586. control = readl(phba->HCregaddr);
  2587. control &= ~HC_LAINT_ENA;
  2588. writel(control, phba->HCregaddr);
  2589. readl(phba->HCregaddr); /* flush */
  2590. spin_unlock(phba->host->host_lock);
  2591. }
  2592. else
  2593. work_ha_copy &= ~HA_LATT;
  2594. }
  2595. if (work_ha_copy & ~(HA_ERATT|HA_MBATT|HA_LATT)) {
  2596. for (i = 0; i < phba->sli.num_rings; i++) {
  2597. if (work_ha_copy & (HA_RXATT << (4*i))) {
  2598. /*
  2599. * Turn off Slow Rings interrupts
  2600. */
  2601. spin_lock(phba->host->host_lock);
  2602. control = readl(phba->HCregaddr);
  2603. control &= ~(HC_R0INT_ENA << i);
  2604. writel(control, phba->HCregaddr);
  2605. readl(phba->HCregaddr); /* flush */
  2606. spin_unlock(phba->host->host_lock);
  2607. }
  2608. }
  2609. }
  2610. if (work_ha_copy & HA_ERATT) {
  2611. phba->hba_state = LPFC_HBA_ERROR;
  2612. /*
  2613. * There was a link/board error. Read the
  2614. * status register to retrieve the error event
  2615. * and process it.
  2616. */
  2617. phba->sli.slistat.err_attn_event++;
  2618. /* Save status info */
  2619. phba->work_hs = readl(phba->HSregaddr);
  2620. phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
  2621. phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
  2622. /* Clear Chip error bit */
  2623. writel(HA_ERATT, phba->HAregaddr);
  2624. readl(phba->HAregaddr); /* flush */
  2625. /*
  2626. * Reseting the HBA is the only reliable way
  2627. * to shutdown interrupt when there is a
  2628. * ERROR.
  2629. */
  2630. lpfc_sli_send_reset(phba, phba->hba_state);
  2631. }
  2632. spin_lock(phba->host->host_lock);
  2633. phba->work_ha |= work_ha_copy;
  2634. if (phba->work_wait)
  2635. wake_up(phba->work_wait);
  2636. spin_unlock(phba->host->host_lock);
  2637. }
  2638. ha_copy &= ~(phba->work_ha_mask);
  2639. /*
  2640. * Process all events on FCP ring. Take the optimized path for
  2641. * FCP IO. Any other IO is slow path and is handled by
  2642. * the worker thread.
  2643. */
  2644. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  2645. status >>= (4*LPFC_FCP_RING);
  2646. if (status & HA_RXATT)
  2647. lpfc_sli_handle_fast_ring_event(phba,
  2648. &phba->sli.ring[LPFC_FCP_RING],
  2649. status);
  2650. return IRQ_HANDLED;
  2651. } /* lpfc_intr_handler */