lpfc_sli.c 77 KB

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