lpfc_sli.c 81 KB

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