lpfc_init.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2008 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/delay.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/idr.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/kthread.h>
  27. #include <linux/pci.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/ctype.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_host.h>
  33. #include <scsi/scsi_transport_fc.h>
  34. #include "lpfc_hw.h"
  35. #include "lpfc_sli.h"
  36. #include "lpfc_disc.h"
  37. #include "lpfc_scsi.h"
  38. #include "lpfc.h"
  39. #include "lpfc_logmsg.h"
  40. #include "lpfc_crtn.h"
  41. #include "lpfc_vport.h"
  42. #include "lpfc_version.h"
  43. static int lpfc_parse_vpd(struct lpfc_hba *, uint8_t *, int);
  44. static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
  45. static int lpfc_post_rcv_buf(struct lpfc_hba *);
  46. static struct scsi_transport_template *lpfc_transport_template = NULL;
  47. static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
  48. static DEFINE_IDR(lpfc_hba_index);
  49. /**
  50. * lpfc_config_port_prep: Perform lpfc initialization prior to config port.
  51. * @phba: pointer to lpfc hba data structure.
  52. *
  53. * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
  54. * mailbox command. It retrieves the revision information from the HBA and
  55. * collects the Vital Product Data (VPD) about the HBA for preparing the
  56. * configuration of the HBA.
  57. *
  58. * Return codes:
  59. * 0 - success.
  60. * -ERESTART - requests the SLI layer to reset the HBA and try again.
  61. * Any other value - indicates an error.
  62. **/
  63. int
  64. lpfc_config_port_prep(struct lpfc_hba *phba)
  65. {
  66. lpfc_vpd_t *vp = &phba->vpd;
  67. int i = 0, rc;
  68. LPFC_MBOXQ_t *pmb;
  69. MAILBOX_t *mb;
  70. char *lpfc_vpd_data = NULL;
  71. uint16_t offset = 0;
  72. static char licensed[56] =
  73. "key unlock for use with gnu public licensed code only\0";
  74. static int init_key = 1;
  75. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  76. if (!pmb) {
  77. phba->link_state = LPFC_HBA_ERROR;
  78. return -ENOMEM;
  79. }
  80. mb = &pmb->mb;
  81. phba->link_state = LPFC_INIT_MBX_CMDS;
  82. if (lpfc_is_LC_HBA(phba->pcidev->device)) {
  83. if (init_key) {
  84. uint32_t *ptext = (uint32_t *) licensed;
  85. for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
  86. *ptext = cpu_to_be32(*ptext);
  87. init_key = 0;
  88. }
  89. lpfc_read_nv(phba, pmb);
  90. memset((char*)mb->un.varRDnvp.rsvd3, 0,
  91. sizeof (mb->un.varRDnvp.rsvd3));
  92. memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
  93. sizeof (licensed));
  94. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  95. if (rc != MBX_SUCCESS) {
  96. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  97. "0324 Config Port initialization "
  98. "error, mbxCmd x%x READ_NVPARM, "
  99. "mbxStatus x%x\n",
  100. mb->mbxCommand, mb->mbxStatus);
  101. mempool_free(pmb, phba->mbox_mem_pool);
  102. return -ERESTART;
  103. }
  104. memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
  105. sizeof(phba->wwnn));
  106. memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
  107. sizeof(phba->wwpn));
  108. }
  109. phba->sli3_options = 0x0;
  110. /* Setup and issue mailbox READ REV command */
  111. lpfc_read_rev(phba, pmb);
  112. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  113. if (rc != MBX_SUCCESS) {
  114. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  115. "0439 Adapter failed to init, mbxCmd x%x "
  116. "READ_REV, mbxStatus x%x\n",
  117. mb->mbxCommand, mb->mbxStatus);
  118. mempool_free( pmb, phba->mbox_mem_pool);
  119. return -ERESTART;
  120. }
  121. /*
  122. * The value of rr must be 1 since the driver set the cv field to 1.
  123. * This setting requires the FW to set all revision fields.
  124. */
  125. if (mb->un.varRdRev.rr == 0) {
  126. vp->rev.rBit = 0;
  127. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  128. "0440 Adapter failed to init, READ_REV has "
  129. "missing revision information.\n");
  130. mempool_free(pmb, phba->mbox_mem_pool);
  131. return -ERESTART;
  132. }
  133. if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
  134. mempool_free(pmb, phba->mbox_mem_pool);
  135. return -EINVAL;
  136. }
  137. /* Save information as VPD data */
  138. vp->rev.rBit = 1;
  139. memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
  140. vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
  141. memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
  142. vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
  143. memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
  144. vp->rev.biuRev = mb->un.varRdRev.biuRev;
  145. vp->rev.smRev = mb->un.varRdRev.smRev;
  146. vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
  147. vp->rev.endecRev = mb->un.varRdRev.endecRev;
  148. vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
  149. vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
  150. vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
  151. vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
  152. vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
  153. vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
  154. /* If the sli feature level is less then 9, we must
  155. * tear down all RPIs and VPIs on link down if NPIV
  156. * is enabled.
  157. */
  158. if (vp->rev.feaLevelHigh < 9)
  159. phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
  160. if (lpfc_is_LC_HBA(phba->pcidev->device))
  161. memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
  162. sizeof (phba->RandomData));
  163. /* Get adapter VPD information */
  164. pmb->context2 = kmalloc(DMP_RSP_SIZE, GFP_KERNEL);
  165. if (!pmb->context2)
  166. goto out_free_mbox;
  167. lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
  168. if (!lpfc_vpd_data)
  169. goto out_free_context2;
  170. do {
  171. lpfc_dump_mem(phba, pmb, offset);
  172. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  173. if (rc != MBX_SUCCESS) {
  174. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  175. "0441 VPD not present on adapter, "
  176. "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
  177. mb->mbxCommand, mb->mbxStatus);
  178. mb->un.varDmp.word_cnt = 0;
  179. }
  180. if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
  181. mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
  182. lpfc_sli_pcimem_bcopy(pmb->context2, lpfc_vpd_data + offset,
  183. mb->un.varDmp.word_cnt);
  184. offset += mb->un.varDmp.word_cnt;
  185. } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
  186. lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
  187. kfree(lpfc_vpd_data);
  188. out_free_context2:
  189. kfree(pmb->context2);
  190. out_free_mbox:
  191. mempool_free(pmb, phba->mbox_mem_pool);
  192. return 0;
  193. }
  194. /**
  195. * lpfc_config_async_cmpl: Completion handler for config async event mbox cmd.
  196. * @phba: pointer to lpfc hba data structure.
  197. * @pmboxq: pointer to the driver internal queue element for mailbox command.
  198. *
  199. * This is the completion handler for driver's configuring asynchronous event
  200. * mailbox command to the device. If the mailbox command returns successfully,
  201. * it will set internal async event support flag to 1; otherwise, it will
  202. * set internal async event support flag to 0.
  203. **/
  204. static void
  205. lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
  206. {
  207. if (pmboxq->mb.mbxStatus == MBX_SUCCESS)
  208. phba->temp_sensor_support = 1;
  209. else
  210. phba->temp_sensor_support = 0;
  211. mempool_free(pmboxq, phba->mbox_mem_pool);
  212. return;
  213. }
  214. /**
  215. * lpfc_config_port_post: Perform lpfc initialization after config port.
  216. * @phba: pointer to lpfc hba data structure.
  217. *
  218. * This routine will do LPFC initialization after the CONFIG_PORT mailbox
  219. * command call. It performs all internal resource and state setups on the
  220. * port: post IOCB buffers, enable appropriate host interrupt attentions,
  221. * ELS ring timers, etc.
  222. *
  223. * Return codes
  224. * 0 - success.
  225. * Any other value - error.
  226. **/
  227. int
  228. lpfc_config_port_post(struct lpfc_hba *phba)
  229. {
  230. struct lpfc_vport *vport = phba->pport;
  231. LPFC_MBOXQ_t *pmb;
  232. MAILBOX_t *mb;
  233. struct lpfc_dmabuf *mp;
  234. struct lpfc_sli *psli = &phba->sli;
  235. uint32_t status, timeout;
  236. int i, j;
  237. int rc;
  238. spin_lock_irq(&phba->hbalock);
  239. /*
  240. * If the Config port completed correctly the HBA is not
  241. * over heated any more.
  242. */
  243. if (phba->over_temp_state == HBA_OVER_TEMP)
  244. phba->over_temp_state = HBA_NORMAL_TEMP;
  245. spin_unlock_irq(&phba->hbalock);
  246. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  247. if (!pmb) {
  248. phba->link_state = LPFC_HBA_ERROR;
  249. return -ENOMEM;
  250. }
  251. mb = &pmb->mb;
  252. /* Get login parameters for NID. */
  253. lpfc_read_sparam(phba, pmb, 0);
  254. pmb->vport = vport;
  255. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  256. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  257. "0448 Adapter failed init, mbxCmd x%x "
  258. "READ_SPARM mbxStatus x%x\n",
  259. mb->mbxCommand, mb->mbxStatus);
  260. phba->link_state = LPFC_HBA_ERROR;
  261. mp = (struct lpfc_dmabuf *) pmb->context1;
  262. mempool_free( pmb, phba->mbox_mem_pool);
  263. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  264. kfree(mp);
  265. return -EIO;
  266. }
  267. mp = (struct lpfc_dmabuf *) pmb->context1;
  268. memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
  269. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  270. kfree(mp);
  271. pmb->context1 = NULL;
  272. if (phba->cfg_soft_wwnn)
  273. u64_to_wwn(phba->cfg_soft_wwnn,
  274. vport->fc_sparam.nodeName.u.wwn);
  275. if (phba->cfg_soft_wwpn)
  276. u64_to_wwn(phba->cfg_soft_wwpn,
  277. vport->fc_sparam.portName.u.wwn);
  278. memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
  279. sizeof (struct lpfc_name));
  280. memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
  281. sizeof (struct lpfc_name));
  282. /* If no serial number in VPD data, use low 6 bytes of WWNN */
  283. /* This should be consolidated into parse_vpd ? - mr */
  284. if (phba->SerialNumber[0] == 0) {
  285. uint8_t *outptr;
  286. outptr = &vport->fc_nodename.u.s.IEEE[0];
  287. for (i = 0; i < 12; i++) {
  288. status = *outptr++;
  289. j = ((status & 0xf0) >> 4);
  290. if (j <= 9)
  291. phba->SerialNumber[i] =
  292. (char)((uint8_t) 0x30 + (uint8_t) j);
  293. else
  294. phba->SerialNumber[i] =
  295. (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
  296. i++;
  297. j = (status & 0xf);
  298. if (j <= 9)
  299. phba->SerialNumber[i] =
  300. (char)((uint8_t) 0x30 + (uint8_t) j);
  301. else
  302. phba->SerialNumber[i] =
  303. (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
  304. }
  305. }
  306. lpfc_read_config(phba, pmb);
  307. pmb->vport = vport;
  308. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  309. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  310. "0453 Adapter failed to init, mbxCmd x%x "
  311. "READ_CONFIG, mbxStatus x%x\n",
  312. mb->mbxCommand, mb->mbxStatus);
  313. phba->link_state = LPFC_HBA_ERROR;
  314. mempool_free( pmb, phba->mbox_mem_pool);
  315. return -EIO;
  316. }
  317. /* Reset the DFT_HBA_Q_DEPTH to the max xri */
  318. if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
  319. phba->cfg_hba_queue_depth =
  320. mb->un.varRdConfig.max_xri + 1;
  321. phba->lmt = mb->un.varRdConfig.lmt;
  322. /* Get the default values for Model Name and Description */
  323. lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
  324. if ((phba->cfg_link_speed > LINK_SPEED_10G)
  325. || ((phba->cfg_link_speed == LINK_SPEED_1G)
  326. && !(phba->lmt & LMT_1Gb))
  327. || ((phba->cfg_link_speed == LINK_SPEED_2G)
  328. && !(phba->lmt & LMT_2Gb))
  329. || ((phba->cfg_link_speed == LINK_SPEED_4G)
  330. && !(phba->lmt & LMT_4Gb))
  331. || ((phba->cfg_link_speed == LINK_SPEED_8G)
  332. && !(phba->lmt & LMT_8Gb))
  333. || ((phba->cfg_link_speed == LINK_SPEED_10G)
  334. && !(phba->lmt & LMT_10Gb))) {
  335. /* Reset link speed to auto */
  336. lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
  337. "1302 Invalid speed for this board: "
  338. "Reset link speed to auto: x%x\n",
  339. phba->cfg_link_speed);
  340. phba->cfg_link_speed = LINK_SPEED_AUTO;
  341. }
  342. phba->link_state = LPFC_LINK_DOWN;
  343. /* Only process IOCBs on ELS ring till hba_state is READY */
  344. if (psli->ring[psli->extra_ring].cmdringaddr)
  345. psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
  346. if (psli->ring[psli->fcp_ring].cmdringaddr)
  347. psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
  348. if (psli->ring[psli->next_ring].cmdringaddr)
  349. psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
  350. /* Post receive buffers for desired rings */
  351. if (phba->sli_rev != 3)
  352. lpfc_post_rcv_buf(phba);
  353. /* Enable appropriate host interrupts */
  354. spin_lock_irq(&phba->hbalock);
  355. status = readl(phba->HCregaddr);
  356. status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
  357. if (psli->num_rings > 0)
  358. status |= HC_R0INT_ENA;
  359. if (psli->num_rings > 1)
  360. status |= HC_R1INT_ENA;
  361. if (psli->num_rings > 2)
  362. status |= HC_R2INT_ENA;
  363. if (psli->num_rings > 3)
  364. status |= HC_R3INT_ENA;
  365. if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
  366. (phba->cfg_poll & DISABLE_FCP_RING_INT))
  367. status &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  368. writel(status, phba->HCregaddr);
  369. readl(phba->HCregaddr); /* flush */
  370. spin_unlock_irq(&phba->hbalock);
  371. /*
  372. * Setup the ring 0 (els) timeout handler
  373. */
  374. timeout = phba->fc_ratov << 1;
  375. mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
  376. mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  377. phba->hb_outstanding = 0;
  378. phba->last_completion_time = jiffies;
  379. lpfc_init_link(phba, pmb, phba->cfg_topology, phba->cfg_link_speed);
  380. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  381. pmb->vport = vport;
  382. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  383. lpfc_set_loopback_flag(phba);
  384. if (rc != MBX_SUCCESS) {
  385. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  386. "0454 Adapter failed to init, mbxCmd x%x "
  387. "INIT_LINK, mbxStatus x%x\n",
  388. mb->mbxCommand, mb->mbxStatus);
  389. /* Clear all interrupt enable conditions */
  390. writel(0, phba->HCregaddr);
  391. readl(phba->HCregaddr); /* flush */
  392. /* Clear all pending interrupts */
  393. writel(0xffffffff, phba->HAregaddr);
  394. readl(phba->HAregaddr); /* flush */
  395. phba->link_state = LPFC_HBA_ERROR;
  396. if (rc != MBX_BUSY)
  397. mempool_free(pmb, phba->mbox_mem_pool);
  398. return -EIO;
  399. }
  400. /* MBOX buffer will be freed in mbox compl */
  401. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  402. lpfc_config_async(phba, pmb, LPFC_ELS_RING);
  403. pmb->mbox_cmpl = lpfc_config_async_cmpl;
  404. pmb->vport = phba->pport;
  405. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  406. if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
  407. lpfc_printf_log(phba,
  408. KERN_ERR,
  409. LOG_INIT,
  410. "0456 Adapter failed to issue "
  411. "ASYNCEVT_ENABLE mbox status x%x \n.",
  412. rc);
  413. mempool_free(pmb, phba->mbox_mem_pool);
  414. }
  415. return (0);
  416. }
  417. /**
  418. * lpfc_hba_down_prep: Perform lpfc uninitialization prior to HBA reset.
  419. * @phba: pointer to lpfc HBA data structure.
  420. *
  421. * This routine will do LPFC uninitialization before the HBA is reset when
  422. * bringing down the SLI Layer.
  423. *
  424. * Return codes
  425. * 0 - success.
  426. * Any other value - error.
  427. **/
  428. int
  429. lpfc_hba_down_prep(struct lpfc_hba *phba)
  430. {
  431. struct lpfc_vport **vports;
  432. int i;
  433. /* Disable interrupts */
  434. writel(0, phba->HCregaddr);
  435. readl(phba->HCregaddr); /* flush */
  436. if (phba->pport->load_flag & FC_UNLOADING)
  437. lpfc_cleanup_discovery_resources(phba->pport);
  438. else {
  439. vports = lpfc_create_vport_work_array(phba);
  440. if (vports != NULL)
  441. for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
  442. lpfc_cleanup_discovery_resources(vports[i]);
  443. lpfc_destroy_vport_work_array(phba, vports);
  444. }
  445. return 0;
  446. }
  447. /**
  448. * lpfc_hba_down_post: Perform lpfc uninitialization after HBA reset.
  449. * @phba: pointer to lpfc HBA data structure.
  450. *
  451. * This routine will do uninitialization after the HBA is reset when bring
  452. * down the SLI Layer.
  453. *
  454. * Return codes
  455. * 0 - sucess.
  456. * Any other value - error.
  457. **/
  458. int
  459. lpfc_hba_down_post(struct lpfc_hba *phba)
  460. {
  461. struct lpfc_sli *psli = &phba->sli;
  462. struct lpfc_sli_ring *pring;
  463. struct lpfc_dmabuf *mp, *next_mp;
  464. struct lpfc_iocbq *iocb;
  465. IOCB_t *cmd = NULL;
  466. LIST_HEAD(completions);
  467. int i;
  468. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
  469. lpfc_sli_hbqbuf_free_all(phba);
  470. else {
  471. /* Cleanup preposted buffers on the ELS ring */
  472. pring = &psli->ring[LPFC_ELS_RING];
  473. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  474. list_del(&mp->list);
  475. pring->postbufq_cnt--;
  476. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  477. kfree(mp);
  478. }
  479. }
  480. spin_lock_irq(&phba->hbalock);
  481. for (i = 0; i < psli->num_rings; i++) {
  482. pring = &psli->ring[i];
  483. /* At this point in time the HBA is either reset or DOA. Either
  484. * way, nothing should be on txcmplq as it will NEVER complete.
  485. */
  486. list_splice_init(&pring->txcmplq, &completions);
  487. pring->txcmplq_cnt = 0;
  488. spin_unlock_irq(&phba->hbalock);
  489. while (!list_empty(&completions)) {
  490. iocb = list_get_first(&completions, struct lpfc_iocbq,
  491. list);
  492. cmd = &iocb->iocb;
  493. list_del_init(&iocb->list);
  494. if (!iocb->iocb_cmpl)
  495. lpfc_sli_release_iocbq(phba, iocb);
  496. else {
  497. cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
  498. cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
  499. (iocb->iocb_cmpl) (phba, iocb, iocb);
  500. }
  501. }
  502. lpfc_sli_abort_iocb_ring(phba, pring);
  503. spin_lock_irq(&phba->hbalock);
  504. }
  505. spin_unlock_irq(&phba->hbalock);
  506. return 0;
  507. }
  508. /**
  509. * lpfc_hb_timeout: The HBA-timer timeout handler.
  510. * @ptr: unsigned long holds the pointer to lpfc hba data structure.
  511. *
  512. * This is the HBA-timer timeout handler registered to the lpfc driver. When
  513. * this timer fires, a HBA timeout event shall be posted to the lpfc driver
  514. * work-port-events bitmap and the worker thread is notified. This timeout
  515. * event will be used by the worker thread to invoke the actual timeout
  516. * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
  517. * be performed in the timeout handler and the HBA timeout event bit shall
  518. * be cleared by the worker thread after it has taken the event bitmap out.
  519. **/
  520. static void
  521. lpfc_hb_timeout(unsigned long ptr)
  522. {
  523. struct lpfc_hba *phba;
  524. uint32_t tmo_posted;
  525. unsigned long iflag;
  526. phba = (struct lpfc_hba *)ptr;
  527. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  528. tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
  529. if (!tmo_posted)
  530. phba->pport->work_port_events |= WORKER_HB_TMO;
  531. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  532. if (!tmo_posted)
  533. lpfc_worker_wake_up(phba);
  534. return;
  535. }
  536. /**
  537. * lpfc_hb_mbox_cmpl: The lpfc heart-beat mailbox command callback function.
  538. * @phba: pointer to lpfc hba data structure.
  539. * @pmboxq: pointer to the driver internal queue element for mailbox command.
  540. *
  541. * This is the callback function to the lpfc heart-beat mailbox command.
  542. * If configured, the lpfc driver issues the heart-beat mailbox command to
  543. * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
  544. * heart-beat mailbox command is issued, the driver shall set up heart-beat
  545. * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
  546. * heart-beat outstanding state. Once the mailbox command comes back and
  547. * no error conditions detected, the heart-beat mailbox command timer is
  548. * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
  549. * state is cleared for the next heart-beat. If the timer expired with the
  550. * heart-beat outstanding state set, the driver will put the HBA offline.
  551. **/
  552. static void
  553. lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
  554. {
  555. unsigned long drvr_flag;
  556. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  557. phba->hb_outstanding = 0;
  558. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  559. mempool_free(pmboxq, phba->mbox_mem_pool);
  560. if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
  561. !(phba->link_state == LPFC_HBA_ERROR) &&
  562. !(phba->pport->load_flag & FC_UNLOADING))
  563. mod_timer(&phba->hb_tmofunc,
  564. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  565. return;
  566. }
  567. /**
  568. * lpfc_hb_timeout_handler: The HBA-timer timeout handler.
  569. * @phba: pointer to lpfc hba data structure.
  570. *
  571. * This is the actual HBA-timer timeout handler to be invoked by the worker
  572. * thread whenever the HBA timer fired and HBA-timeout event posted. This
  573. * handler performs any periodic operations needed for the device. If such
  574. * periodic event has already been attended to either in the interrupt handler
  575. * or by processing slow-ring or fast-ring events within the HBA-timer
  576. * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
  577. * the timer for the next timeout period. If lpfc heart-beat mailbox command
  578. * is configured and there is no heart-beat mailbox command outstanding, a
  579. * heart-beat mailbox is issued and timer set properly. Otherwise, if there
  580. * has been a heart-beat mailbox command outstanding, the HBA shall be put
  581. * to offline.
  582. **/
  583. void
  584. lpfc_hb_timeout_handler(struct lpfc_hba *phba)
  585. {
  586. LPFC_MBOXQ_t *pmboxq;
  587. struct lpfc_dmabuf *buf_ptr;
  588. int retval;
  589. struct lpfc_sli *psli = &phba->sli;
  590. LIST_HEAD(completions);
  591. if ((phba->link_state == LPFC_HBA_ERROR) ||
  592. (phba->pport->load_flag & FC_UNLOADING) ||
  593. (phba->pport->fc_flag & FC_OFFLINE_MODE))
  594. return;
  595. spin_lock_irq(&phba->pport->work_port_lock);
  596. /* If the timer is already canceled do nothing */
  597. if (!(phba->pport->work_port_events & WORKER_HB_TMO)) {
  598. spin_unlock_irq(&phba->pport->work_port_lock);
  599. return;
  600. }
  601. if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
  602. jiffies)) {
  603. spin_unlock_irq(&phba->pport->work_port_lock);
  604. if (!phba->hb_outstanding)
  605. mod_timer(&phba->hb_tmofunc,
  606. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  607. else
  608. mod_timer(&phba->hb_tmofunc,
  609. jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
  610. return;
  611. }
  612. spin_unlock_irq(&phba->pport->work_port_lock);
  613. if (phba->elsbuf_cnt &&
  614. (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
  615. spin_lock_irq(&phba->hbalock);
  616. list_splice_init(&phba->elsbuf, &completions);
  617. phba->elsbuf_cnt = 0;
  618. phba->elsbuf_prev_cnt = 0;
  619. spin_unlock_irq(&phba->hbalock);
  620. while (!list_empty(&completions)) {
  621. list_remove_head(&completions, buf_ptr,
  622. struct lpfc_dmabuf, list);
  623. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  624. kfree(buf_ptr);
  625. }
  626. }
  627. phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
  628. /* If there is no heart beat outstanding, issue a heartbeat command */
  629. if (phba->cfg_enable_hba_heartbeat) {
  630. if (!phba->hb_outstanding) {
  631. pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
  632. if (!pmboxq) {
  633. mod_timer(&phba->hb_tmofunc,
  634. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  635. return;
  636. }
  637. lpfc_heart_beat(phba, pmboxq);
  638. pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
  639. pmboxq->vport = phba->pport;
  640. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  641. if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
  642. mempool_free(pmboxq, phba->mbox_mem_pool);
  643. mod_timer(&phba->hb_tmofunc,
  644. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  645. return;
  646. }
  647. mod_timer(&phba->hb_tmofunc,
  648. jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
  649. phba->hb_outstanding = 1;
  650. return;
  651. } else {
  652. /*
  653. * If heart beat timeout called with hb_outstanding set
  654. * we need to take the HBA offline.
  655. */
  656. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  657. "0459 Adapter heartbeat failure, "
  658. "taking this port offline.\n");
  659. spin_lock_irq(&phba->hbalock);
  660. psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
  661. spin_unlock_irq(&phba->hbalock);
  662. lpfc_offline_prep(phba);
  663. lpfc_offline(phba);
  664. lpfc_unblock_mgmt_io(phba);
  665. phba->link_state = LPFC_HBA_ERROR;
  666. lpfc_hba_down_post(phba);
  667. }
  668. }
  669. }
  670. /**
  671. * lpfc_offline_eratt: Bring lpfc offline on hardware error attention.
  672. * @phba: pointer to lpfc hba data structure.
  673. *
  674. * This routine is called to bring the HBA offline when HBA hardware error
  675. * other than Port Error 6 has been detected.
  676. **/
  677. static void
  678. lpfc_offline_eratt(struct lpfc_hba *phba)
  679. {
  680. struct lpfc_sli *psli = &phba->sli;
  681. spin_lock_irq(&phba->hbalock);
  682. psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
  683. spin_unlock_irq(&phba->hbalock);
  684. lpfc_offline_prep(phba);
  685. lpfc_offline(phba);
  686. lpfc_reset_barrier(phba);
  687. lpfc_sli_brdreset(phba);
  688. lpfc_hba_down_post(phba);
  689. lpfc_sli_brdready(phba, HS_MBRDY);
  690. lpfc_unblock_mgmt_io(phba);
  691. phba->link_state = LPFC_HBA_ERROR;
  692. return;
  693. }
  694. /**
  695. * lpfc_handle_eratt: The HBA hardware error handler.
  696. * @phba: pointer to lpfc hba data structure.
  697. *
  698. * This routine is invoked to handle the following HBA hardware error
  699. * conditions:
  700. * 1 - HBA error attention interrupt
  701. * 2 - DMA ring index out of range
  702. * 3 - Mailbox command came back as unknown
  703. **/
  704. void
  705. lpfc_handle_eratt(struct lpfc_hba *phba)
  706. {
  707. struct lpfc_vport *vport = phba->pport;
  708. struct lpfc_sli *psli = &phba->sli;
  709. struct lpfc_sli_ring *pring;
  710. uint32_t event_data;
  711. unsigned long temperature;
  712. struct temp_event temp_event_data;
  713. struct Scsi_Host *shost;
  714. /* If the pci channel is offline, ignore possible errors,
  715. * since we cannot communicate with the pci card anyway. */
  716. if (pci_channel_offline(phba->pcidev))
  717. return;
  718. /* If resets are disabled then leave the HBA alone and return */
  719. if (!phba->cfg_enable_hba_reset)
  720. return;
  721. if (phba->work_hs & HS_FFER6) {
  722. /* Re-establishing Link */
  723. lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
  724. "1301 Re-establishing Link "
  725. "Data: x%x x%x x%x\n",
  726. phba->work_hs,
  727. phba->work_status[0], phba->work_status[1]);
  728. spin_lock_irq(&phba->hbalock);
  729. psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
  730. spin_unlock_irq(&phba->hbalock);
  731. /*
  732. * Firmware stops when it triggled erratt with HS_FFER6.
  733. * That could cause the I/Os dropped by the firmware.
  734. * Error iocb (I/O) on txcmplq and let the SCSI layer
  735. * retry it after re-establishing link.
  736. */
  737. pring = &psli->ring[psli->fcp_ring];
  738. lpfc_sli_abort_iocb_ring(phba, pring);
  739. /*
  740. * There was a firmware error. Take the hba offline and then
  741. * attempt to restart it.
  742. */
  743. lpfc_offline_prep(phba);
  744. lpfc_offline(phba);
  745. lpfc_sli_brdrestart(phba);
  746. if (lpfc_online(phba) == 0) { /* Initialize the HBA */
  747. lpfc_unblock_mgmt_io(phba);
  748. return;
  749. }
  750. lpfc_unblock_mgmt_io(phba);
  751. } else if (phba->work_hs & HS_CRIT_TEMP) {
  752. temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
  753. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  754. temp_event_data.event_code = LPFC_CRIT_TEMP;
  755. temp_event_data.data = (uint32_t)temperature;
  756. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  757. "0459 Adapter maximum temperature exceeded "
  758. "(%ld), taking this port offline "
  759. "Data: x%x x%x x%x\n",
  760. temperature, phba->work_hs,
  761. phba->work_status[0], phba->work_status[1]);
  762. shost = lpfc_shost_from_vport(phba->pport);
  763. fc_host_post_vendor_event(shost, fc_get_event_number(),
  764. sizeof(temp_event_data),
  765. (char *) &temp_event_data,
  766. SCSI_NL_VID_TYPE_PCI
  767. | PCI_VENDOR_ID_EMULEX);
  768. spin_lock_irq(&phba->hbalock);
  769. phba->over_temp_state = HBA_OVER_TEMP;
  770. spin_unlock_irq(&phba->hbalock);
  771. lpfc_offline_eratt(phba);
  772. } else {
  773. /* The if clause above forces this code path when the status
  774. * failure is a value other than FFER6. Do not call the offline
  775. * twice. This is the adapter hardware error path.
  776. */
  777. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  778. "0457 Adapter Hardware Error "
  779. "Data: x%x x%x x%x\n",
  780. phba->work_hs,
  781. phba->work_status[0], phba->work_status[1]);
  782. event_data = FC_REG_DUMP_EVENT;
  783. shost = lpfc_shost_from_vport(vport);
  784. fc_host_post_vendor_event(shost, fc_get_event_number(),
  785. sizeof(event_data), (char *) &event_data,
  786. SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
  787. lpfc_offline_eratt(phba);
  788. }
  789. }
  790. /**
  791. * lpfc_handle_latt: The HBA link event handler.
  792. * @phba: pointer to lpfc hba data structure.
  793. *
  794. * This routine is invoked from the worker thread to handle a HBA host
  795. * attention link event.
  796. **/
  797. void
  798. lpfc_handle_latt(struct lpfc_hba *phba)
  799. {
  800. struct lpfc_vport *vport = phba->pport;
  801. struct lpfc_sli *psli = &phba->sli;
  802. LPFC_MBOXQ_t *pmb;
  803. volatile uint32_t control;
  804. struct lpfc_dmabuf *mp;
  805. int rc = 0;
  806. pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  807. if (!pmb) {
  808. rc = 1;
  809. goto lpfc_handle_latt_err_exit;
  810. }
  811. mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  812. if (!mp) {
  813. rc = 2;
  814. goto lpfc_handle_latt_free_pmb;
  815. }
  816. mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
  817. if (!mp->virt) {
  818. rc = 3;
  819. goto lpfc_handle_latt_free_mp;
  820. }
  821. /* Cleanup any outstanding ELS commands */
  822. lpfc_els_flush_all_cmd(phba);
  823. psli->slistat.link_event++;
  824. lpfc_read_la(phba, pmb, mp);
  825. pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
  826. pmb->vport = vport;
  827. /* Block ELS IOCBs until we have processed this mbox command */
  828. phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
  829. rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
  830. if (rc == MBX_NOT_FINISHED) {
  831. rc = 4;
  832. goto lpfc_handle_latt_free_mbuf;
  833. }
  834. /* Clear Link Attention in HA REG */
  835. spin_lock_irq(&phba->hbalock);
  836. writel(HA_LATT, phba->HAregaddr);
  837. readl(phba->HAregaddr); /* flush */
  838. spin_unlock_irq(&phba->hbalock);
  839. return;
  840. lpfc_handle_latt_free_mbuf:
  841. phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
  842. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  843. lpfc_handle_latt_free_mp:
  844. kfree(mp);
  845. lpfc_handle_latt_free_pmb:
  846. mempool_free(pmb, phba->mbox_mem_pool);
  847. lpfc_handle_latt_err_exit:
  848. /* Enable Link attention interrupts */
  849. spin_lock_irq(&phba->hbalock);
  850. psli->sli_flag |= LPFC_PROCESS_LA;
  851. control = readl(phba->HCregaddr);
  852. control |= HC_LAINT_ENA;
  853. writel(control, phba->HCregaddr);
  854. readl(phba->HCregaddr); /* flush */
  855. /* Clear Link Attention in HA REG */
  856. writel(HA_LATT, phba->HAregaddr);
  857. readl(phba->HAregaddr); /* flush */
  858. spin_unlock_irq(&phba->hbalock);
  859. lpfc_linkdown(phba);
  860. phba->link_state = LPFC_HBA_ERROR;
  861. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  862. "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
  863. return;
  864. }
  865. /**
  866. * lpfc_parse_vpd: Parse VPD (Vital Product Data).
  867. * @phba: pointer to lpfc hba data structure.
  868. * @vpd: pointer to the vital product data.
  869. * @len: length of the vital product data in bytes.
  870. *
  871. * This routine parses the Vital Product Data (VPD). The VPD is treated as
  872. * an array of characters. In this routine, the ModelName, ProgramType, and
  873. * ModelDesc, etc. fields of the phba data structure will be populated.
  874. *
  875. * Return codes
  876. * 0 - pointer to the VPD passed in is NULL
  877. * 1 - success
  878. **/
  879. static int
  880. lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
  881. {
  882. uint8_t lenlo, lenhi;
  883. int Length;
  884. int i, j;
  885. int finished = 0;
  886. int index = 0;
  887. if (!vpd)
  888. return 0;
  889. /* Vital Product */
  890. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  891. "0455 Vital Product Data: x%x x%x x%x x%x\n",
  892. (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
  893. (uint32_t) vpd[3]);
  894. while (!finished && (index < (len - 4))) {
  895. switch (vpd[index]) {
  896. case 0x82:
  897. case 0x91:
  898. index += 1;
  899. lenlo = vpd[index];
  900. index += 1;
  901. lenhi = vpd[index];
  902. index += 1;
  903. i = ((((unsigned short)lenhi) << 8) + lenlo);
  904. index += i;
  905. break;
  906. case 0x90:
  907. index += 1;
  908. lenlo = vpd[index];
  909. index += 1;
  910. lenhi = vpd[index];
  911. index += 1;
  912. Length = ((((unsigned short)lenhi) << 8) + lenlo);
  913. if (Length > len - index)
  914. Length = len - index;
  915. while (Length > 0) {
  916. /* Look for Serial Number */
  917. if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
  918. index += 2;
  919. i = vpd[index];
  920. index += 1;
  921. j = 0;
  922. Length -= (3+i);
  923. while(i--) {
  924. phba->SerialNumber[j++] = vpd[index++];
  925. if (j == 31)
  926. break;
  927. }
  928. phba->SerialNumber[j] = 0;
  929. continue;
  930. }
  931. else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
  932. phba->vpd_flag |= VPD_MODEL_DESC;
  933. index += 2;
  934. i = vpd[index];
  935. index += 1;
  936. j = 0;
  937. Length -= (3+i);
  938. while(i--) {
  939. phba->ModelDesc[j++] = vpd[index++];
  940. if (j == 255)
  941. break;
  942. }
  943. phba->ModelDesc[j] = 0;
  944. continue;
  945. }
  946. else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
  947. phba->vpd_flag |= VPD_MODEL_NAME;
  948. index += 2;
  949. i = vpd[index];
  950. index += 1;
  951. j = 0;
  952. Length -= (3+i);
  953. while(i--) {
  954. phba->ModelName[j++] = vpd[index++];
  955. if (j == 79)
  956. break;
  957. }
  958. phba->ModelName[j] = 0;
  959. continue;
  960. }
  961. else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
  962. phba->vpd_flag |= VPD_PROGRAM_TYPE;
  963. index += 2;
  964. i = vpd[index];
  965. index += 1;
  966. j = 0;
  967. Length -= (3+i);
  968. while(i--) {
  969. phba->ProgramType[j++] = vpd[index++];
  970. if (j == 255)
  971. break;
  972. }
  973. phba->ProgramType[j] = 0;
  974. continue;
  975. }
  976. else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
  977. phba->vpd_flag |= VPD_PORT;
  978. index += 2;
  979. i = vpd[index];
  980. index += 1;
  981. j = 0;
  982. Length -= (3+i);
  983. while(i--) {
  984. phba->Port[j++] = vpd[index++];
  985. if (j == 19)
  986. break;
  987. }
  988. phba->Port[j] = 0;
  989. continue;
  990. }
  991. else {
  992. index += 2;
  993. i = vpd[index];
  994. index += 1;
  995. index += i;
  996. Length -= (3 + i);
  997. }
  998. }
  999. finished = 0;
  1000. break;
  1001. case 0x78:
  1002. finished = 1;
  1003. break;
  1004. default:
  1005. index ++;
  1006. break;
  1007. }
  1008. }
  1009. return(1);
  1010. }
  1011. /**
  1012. * lpfc_get_hba_model_desc: Retrieve HBA device model name and description.
  1013. * @phba: pointer to lpfc hba data structure.
  1014. * @mdp: pointer to the data structure to hold the derived model name.
  1015. * @descp: pointer to the data structure to hold the derived description.
  1016. *
  1017. * This routine retrieves HBA's description based on its registered PCI device
  1018. * ID. The @descp passed into this function points to an array of 256 chars. It
  1019. * shall be returned with the model name, maximum speed, and the host bus type.
  1020. * The @mdp passed into this function points to an array of 80 chars. When the
  1021. * function returns, the @mdp will be filled with the model name.
  1022. **/
  1023. static void
  1024. lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
  1025. {
  1026. lpfc_vpd_t *vp;
  1027. uint16_t dev_id = phba->pcidev->device;
  1028. int max_speed;
  1029. struct {
  1030. char * name;
  1031. int max_speed;
  1032. char * bus;
  1033. } m = {"<Unknown>", 0, ""};
  1034. if (mdp && mdp[0] != '\0'
  1035. && descp && descp[0] != '\0')
  1036. return;
  1037. if (phba->lmt & LMT_10Gb)
  1038. max_speed = 10;
  1039. else if (phba->lmt & LMT_8Gb)
  1040. max_speed = 8;
  1041. else if (phba->lmt & LMT_4Gb)
  1042. max_speed = 4;
  1043. else if (phba->lmt & LMT_2Gb)
  1044. max_speed = 2;
  1045. else
  1046. max_speed = 1;
  1047. vp = &phba->vpd;
  1048. switch (dev_id) {
  1049. case PCI_DEVICE_ID_FIREFLY:
  1050. m = (typeof(m)){"LP6000", max_speed, "PCI"};
  1051. break;
  1052. case PCI_DEVICE_ID_SUPERFLY:
  1053. if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
  1054. m = (typeof(m)){"LP7000", max_speed, "PCI"};
  1055. else
  1056. m = (typeof(m)){"LP7000E", max_speed, "PCI"};
  1057. break;
  1058. case PCI_DEVICE_ID_DRAGONFLY:
  1059. m = (typeof(m)){"LP8000", max_speed, "PCI"};
  1060. break;
  1061. case PCI_DEVICE_ID_CENTAUR:
  1062. if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
  1063. m = (typeof(m)){"LP9002", max_speed, "PCI"};
  1064. else
  1065. m = (typeof(m)){"LP9000", max_speed, "PCI"};
  1066. break;
  1067. case PCI_DEVICE_ID_RFLY:
  1068. m = (typeof(m)){"LP952", max_speed, "PCI"};
  1069. break;
  1070. case PCI_DEVICE_ID_PEGASUS:
  1071. m = (typeof(m)){"LP9802", max_speed, "PCI-X"};
  1072. break;
  1073. case PCI_DEVICE_ID_THOR:
  1074. m = (typeof(m)){"LP10000", max_speed, "PCI-X"};
  1075. break;
  1076. case PCI_DEVICE_ID_VIPER:
  1077. m = (typeof(m)){"LPX1000", max_speed, "PCI-X"};
  1078. break;
  1079. case PCI_DEVICE_ID_PFLY:
  1080. m = (typeof(m)){"LP982", max_speed, "PCI-X"};
  1081. break;
  1082. case PCI_DEVICE_ID_TFLY:
  1083. m = (typeof(m)){"LP1050", max_speed, "PCI-X"};
  1084. break;
  1085. case PCI_DEVICE_ID_HELIOS:
  1086. m = (typeof(m)){"LP11000", max_speed, "PCI-X2"};
  1087. break;
  1088. case PCI_DEVICE_ID_HELIOS_SCSP:
  1089. m = (typeof(m)){"LP11000-SP", max_speed, "PCI-X2"};
  1090. break;
  1091. case PCI_DEVICE_ID_HELIOS_DCSP:
  1092. m = (typeof(m)){"LP11002-SP", max_speed, "PCI-X2"};
  1093. break;
  1094. case PCI_DEVICE_ID_NEPTUNE:
  1095. m = (typeof(m)){"LPe1000", max_speed, "PCIe"};
  1096. break;
  1097. case PCI_DEVICE_ID_NEPTUNE_SCSP:
  1098. m = (typeof(m)){"LPe1000-SP", max_speed, "PCIe"};
  1099. break;
  1100. case PCI_DEVICE_ID_NEPTUNE_DCSP:
  1101. m = (typeof(m)){"LPe1002-SP", max_speed, "PCIe"};
  1102. break;
  1103. case PCI_DEVICE_ID_BMID:
  1104. m = (typeof(m)){"LP1150", max_speed, "PCI-X2"};
  1105. break;
  1106. case PCI_DEVICE_ID_BSMB:
  1107. m = (typeof(m)){"LP111", max_speed, "PCI-X2"};
  1108. break;
  1109. case PCI_DEVICE_ID_ZEPHYR:
  1110. m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
  1111. break;
  1112. case PCI_DEVICE_ID_ZEPHYR_SCSP:
  1113. m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
  1114. break;
  1115. case PCI_DEVICE_ID_ZEPHYR_DCSP:
  1116. m = (typeof(m)){"LPe11002-SP", max_speed, "PCIe"};
  1117. break;
  1118. case PCI_DEVICE_ID_ZMID:
  1119. m = (typeof(m)){"LPe1150", max_speed, "PCIe"};
  1120. break;
  1121. case PCI_DEVICE_ID_ZSMB:
  1122. m = (typeof(m)){"LPe111", max_speed, "PCIe"};
  1123. break;
  1124. case PCI_DEVICE_ID_LP101:
  1125. m = (typeof(m)){"LP101", max_speed, "PCI-X"};
  1126. break;
  1127. case PCI_DEVICE_ID_LP10000S:
  1128. m = (typeof(m)){"LP10000-S", max_speed, "PCI"};
  1129. break;
  1130. case PCI_DEVICE_ID_LP11000S:
  1131. m = (typeof(m)){"LP11000-S", max_speed,
  1132. "PCI-X2"};
  1133. break;
  1134. case PCI_DEVICE_ID_LPE11000S:
  1135. m = (typeof(m)){"LPe11000-S", max_speed,
  1136. "PCIe"};
  1137. break;
  1138. case PCI_DEVICE_ID_SAT:
  1139. m = (typeof(m)){"LPe12000", max_speed, "PCIe"};
  1140. break;
  1141. case PCI_DEVICE_ID_SAT_MID:
  1142. m = (typeof(m)){"LPe1250", max_speed, "PCIe"};
  1143. break;
  1144. case PCI_DEVICE_ID_SAT_SMB:
  1145. m = (typeof(m)){"LPe121", max_speed, "PCIe"};
  1146. break;
  1147. case PCI_DEVICE_ID_SAT_DCSP:
  1148. m = (typeof(m)){"LPe12002-SP", max_speed, "PCIe"};
  1149. break;
  1150. case PCI_DEVICE_ID_SAT_SCSP:
  1151. m = (typeof(m)){"LPe12000-SP", max_speed, "PCIe"};
  1152. break;
  1153. case PCI_DEVICE_ID_SAT_S:
  1154. m = (typeof(m)){"LPe12000-S", max_speed, "PCIe"};
  1155. break;
  1156. default:
  1157. m = (typeof(m)){ NULL };
  1158. break;
  1159. }
  1160. if (mdp && mdp[0] == '\0')
  1161. snprintf(mdp, 79,"%s", m.name);
  1162. if (descp && descp[0] == '\0')
  1163. snprintf(descp, 255,
  1164. "Emulex %s %dGb %s Fibre Channel Adapter",
  1165. m.name, m.max_speed, m.bus);
  1166. }
  1167. /**
  1168. * lpfc_post_buffer: Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring.
  1169. * @phba: pointer to lpfc hba data structure.
  1170. * @pring: pointer to a IOCB ring.
  1171. * @cnt: the number of IOCBs to be posted to the IOCB ring.
  1172. *
  1173. * This routine posts a given number of IOCBs with the associated DMA buffer
  1174. * descriptors specified by the cnt argument to the given IOCB ring.
  1175. *
  1176. * Return codes
  1177. * The number of IOCBs NOT able to be posted to the IOCB ring.
  1178. **/
  1179. int
  1180. lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
  1181. {
  1182. IOCB_t *icmd;
  1183. struct lpfc_iocbq *iocb;
  1184. struct lpfc_dmabuf *mp1, *mp2;
  1185. cnt += pring->missbufcnt;
  1186. /* While there are buffers to post */
  1187. while (cnt > 0) {
  1188. /* Allocate buffer for command iocb */
  1189. iocb = lpfc_sli_get_iocbq(phba);
  1190. if (iocb == NULL) {
  1191. pring->missbufcnt = cnt;
  1192. return cnt;
  1193. }
  1194. icmd = &iocb->iocb;
  1195. /* 2 buffers can be posted per command */
  1196. /* Allocate buffer to post */
  1197. mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
  1198. if (mp1)
  1199. mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
  1200. if (!mp1 || !mp1->virt) {
  1201. kfree(mp1);
  1202. lpfc_sli_release_iocbq(phba, iocb);
  1203. pring->missbufcnt = cnt;
  1204. return cnt;
  1205. }
  1206. INIT_LIST_HEAD(&mp1->list);
  1207. /* Allocate buffer to post */
  1208. if (cnt > 1) {
  1209. mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
  1210. if (mp2)
  1211. mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
  1212. &mp2->phys);
  1213. if (!mp2 || !mp2->virt) {
  1214. kfree(mp2);
  1215. lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
  1216. kfree(mp1);
  1217. lpfc_sli_release_iocbq(phba, iocb);
  1218. pring->missbufcnt = cnt;
  1219. return cnt;
  1220. }
  1221. INIT_LIST_HEAD(&mp2->list);
  1222. } else {
  1223. mp2 = NULL;
  1224. }
  1225. icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
  1226. icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
  1227. icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
  1228. icmd->ulpBdeCount = 1;
  1229. cnt--;
  1230. if (mp2) {
  1231. icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
  1232. icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
  1233. icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
  1234. cnt--;
  1235. icmd->ulpBdeCount = 2;
  1236. }
  1237. icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
  1238. icmd->ulpLe = 1;
  1239. if (lpfc_sli_issue_iocb(phba, pring, iocb, 0) == IOCB_ERROR) {
  1240. lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
  1241. kfree(mp1);
  1242. cnt++;
  1243. if (mp2) {
  1244. lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
  1245. kfree(mp2);
  1246. cnt++;
  1247. }
  1248. lpfc_sli_release_iocbq(phba, iocb);
  1249. pring->missbufcnt = cnt;
  1250. return cnt;
  1251. }
  1252. lpfc_sli_ringpostbuf_put(phba, pring, mp1);
  1253. if (mp2)
  1254. lpfc_sli_ringpostbuf_put(phba, pring, mp2);
  1255. }
  1256. pring->missbufcnt = 0;
  1257. return 0;
  1258. }
  1259. /**
  1260. * lpfc_post_rcv_buf: Post the initial receive IOCB buffers to ELS ring.
  1261. * @phba: pointer to lpfc hba data structure.
  1262. *
  1263. * This routine posts initial receive IOCB buffers to the ELS ring. The
  1264. * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
  1265. * set to 64 IOCBs.
  1266. *
  1267. * Return codes
  1268. * 0 - success (currently always success)
  1269. **/
  1270. static int
  1271. lpfc_post_rcv_buf(struct lpfc_hba *phba)
  1272. {
  1273. struct lpfc_sli *psli = &phba->sli;
  1274. /* Ring 0, ELS / CT buffers */
  1275. lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
  1276. /* Ring 2 - FCP no buffers needed */
  1277. return 0;
  1278. }
  1279. #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
  1280. /**
  1281. * lpfc_sha_init: Set up initial array of hash table entries.
  1282. * @HashResultPointer: pointer to an array as hash table.
  1283. *
  1284. * This routine sets up the initial values to the array of hash table entries
  1285. * for the LC HBAs.
  1286. **/
  1287. static void
  1288. lpfc_sha_init(uint32_t * HashResultPointer)
  1289. {
  1290. HashResultPointer[0] = 0x67452301;
  1291. HashResultPointer[1] = 0xEFCDAB89;
  1292. HashResultPointer[2] = 0x98BADCFE;
  1293. HashResultPointer[3] = 0x10325476;
  1294. HashResultPointer[4] = 0xC3D2E1F0;
  1295. }
  1296. /**
  1297. * lpfc_sha_iterate: Iterate initial hash table with the working hash table.
  1298. * @HashResultPointer: pointer to an initial/result hash table.
  1299. * @HashWorkingPointer: pointer to an working hash table.
  1300. *
  1301. * This routine iterates an initial hash table pointed by @HashResultPointer
  1302. * with the values from the working hash table pointeed by @HashWorkingPointer.
  1303. * The results are putting back to the initial hash table, returned through
  1304. * the @HashResultPointer as the result hash table.
  1305. **/
  1306. static void
  1307. lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
  1308. {
  1309. int t;
  1310. uint32_t TEMP;
  1311. uint32_t A, B, C, D, E;
  1312. t = 16;
  1313. do {
  1314. HashWorkingPointer[t] =
  1315. S(1,
  1316. HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
  1317. 8] ^
  1318. HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
  1319. } while (++t <= 79);
  1320. t = 0;
  1321. A = HashResultPointer[0];
  1322. B = HashResultPointer[1];
  1323. C = HashResultPointer[2];
  1324. D = HashResultPointer[3];
  1325. E = HashResultPointer[4];
  1326. do {
  1327. if (t < 20) {
  1328. TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
  1329. } else if (t < 40) {
  1330. TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
  1331. } else if (t < 60) {
  1332. TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
  1333. } else {
  1334. TEMP = (B ^ C ^ D) + 0xCA62C1D6;
  1335. }
  1336. TEMP += S(5, A) + E + HashWorkingPointer[t];
  1337. E = D;
  1338. D = C;
  1339. C = S(30, B);
  1340. B = A;
  1341. A = TEMP;
  1342. } while (++t <= 79);
  1343. HashResultPointer[0] += A;
  1344. HashResultPointer[1] += B;
  1345. HashResultPointer[2] += C;
  1346. HashResultPointer[3] += D;
  1347. HashResultPointer[4] += E;
  1348. }
  1349. /**
  1350. * lpfc_challenge_key: Create challenge key based on WWPN of the HBA.
  1351. * @RandomChallenge: pointer to the entry of host challenge random number array.
  1352. * @HashWorking: pointer to the entry of the working hash array.
  1353. *
  1354. * This routine calculates the working hash array referred by @HashWorking
  1355. * from the challenge random numbers associated with the host, referred by
  1356. * @RandomChallenge. The result is put into the entry of the working hash
  1357. * array and returned by reference through @HashWorking.
  1358. **/
  1359. static void
  1360. lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
  1361. {
  1362. *HashWorking = (*RandomChallenge ^ *HashWorking);
  1363. }
  1364. /**
  1365. * lpfc_hba_init: Perform special handling for LC HBA initialization.
  1366. * @phba: pointer to lpfc hba data structure.
  1367. * @hbainit: pointer to an array of unsigned 32-bit integers.
  1368. *
  1369. * This routine performs the special handling for LC HBA initialization.
  1370. **/
  1371. void
  1372. lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
  1373. {
  1374. int t;
  1375. uint32_t *HashWorking;
  1376. uint32_t *pwwnn = (uint32_t *) phba->wwnn;
  1377. HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
  1378. if (!HashWorking)
  1379. return;
  1380. HashWorking[0] = HashWorking[78] = *pwwnn++;
  1381. HashWorking[1] = HashWorking[79] = *pwwnn;
  1382. for (t = 0; t < 7; t++)
  1383. lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
  1384. lpfc_sha_init(hbainit);
  1385. lpfc_sha_iterate(hbainit, HashWorking);
  1386. kfree(HashWorking);
  1387. }
  1388. /**
  1389. * lpfc_cleanup: Performs vport cleanups before deleting a vport.
  1390. * @vport: pointer to a virtual N_Port data structure.
  1391. *
  1392. * This routine performs the necessary cleanups before deleting the @vport.
  1393. * It invokes the discovery state machine to perform necessary state
  1394. * transitions and to release the ndlps associated with the @vport. Note,
  1395. * the physical port is treated as @vport 0.
  1396. **/
  1397. void
  1398. lpfc_cleanup(struct lpfc_vport *vport)
  1399. {
  1400. struct lpfc_hba *phba = vport->phba;
  1401. struct lpfc_nodelist *ndlp, *next_ndlp;
  1402. int i = 0;
  1403. if (phba->link_state > LPFC_LINK_DOWN)
  1404. lpfc_port_link_failure(vport);
  1405. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  1406. if (!NLP_CHK_NODE_ACT(ndlp)) {
  1407. ndlp = lpfc_enable_node(vport, ndlp,
  1408. NLP_STE_UNUSED_NODE);
  1409. if (!ndlp)
  1410. continue;
  1411. spin_lock_irq(&phba->ndlp_lock);
  1412. NLP_SET_FREE_REQ(ndlp);
  1413. spin_unlock_irq(&phba->ndlp_lock);
  1414. /* Trigger the release of the ndlp memory */
  1415. lpfc_nlp_put(ndlp);
  1416. continue;
  1417. }
  1418. spin_lock_irq(&phba->ndlp_lock);
  1419. if (NLP_CHK_FREE_REQ(ndlp)) {
  1420. /* The ndlp should not be in memory free mode already */
  1421. spin_unlock_irq(&phba->ndlp_lock);
  1422. continue;
  1423. } else
  1424. /* Indicate request for freeing ndlp memory */
  1425. NLP_SET_FREE_REQ(ndlp);
  1426. spin_unlock_irq(&phba->ndlp_lock);
  1427. if (vport->port_type != LPFC_PHYSICAL_PORT &&
  1428. ndlp->nlp_DID == Fabric_DID) {
  1429. /* Just free up ndlp with Fabric_DID for vports */
  1430. lpfc_nlp_put(ndlp);
  1431. continue;
  1432. }
  1433. if (ndlp->nlp_type & NLP_FABRIC)
  1434. lpfc_disc_state_machine(vport, ndlp, NULL,
  1435. NLP_EVT_DEVICE_RECOVERY);
  1436. lpfc_disc_state_machine(vport, ndlp, NULL,
  1437. NLP_EVT_DEVICE_RM);
  1438. }
  1439. /* At this point, ALL ndlp's should be gone
  1440. * because of the previous NLP_EVT_DEVICE_RM.
  1441. * Lets wait for this to happen, if needed.
  1442. */
  1443. while (!list_empty(&vport->fc_nodes)) {
  1444. if (i++ > 3000) {
  1445. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  1446. "0233 Nodelist not empty\n");
  1447. list_for_each_entry_safe(ndlp, next_ndlp,
  1448. &vport->fc_nodes, nlp_listp) {
  1449. lpfc_printf_vlog(ndlp->vport, KERN_ERR,
  1450. LOG_NODE,
  1451. "0282: did:x%x ndlp:x%p "
  1452. "usgmap:x%x refcnt:%d\n",
  1453. ndlp->nlp_DID, (void *)ndlp,
  1454. ndlp->nlp_usg_map,
  1455. atomic_read(
  1456. &ndlp->kref.refcount));
  1457. }
  1458. break;
  1459. }
  1460. /* Wait for any activity on ndlps to settle */
  1461. msleep(10);
  1462. }
  1463. return;
  1464. }
  1465. /**
  1466. * lpfc_stop_vport_timers: Stop all the timers associated with a vport.
  1467. * @vport: pointer to a virtual N_Port data structure.
  1468. *
  1469. * This routine stops all the timers associated with a @vport. This function
  1470. * is invoked before disabling or deleting a @vport. Note that the physical
  1471. * port is treated as @vport 0.
  1472. **/
  1473. void
  1474. lpfc_stop_vport_timers(struct lpfc_vport *vport)
  1475. {
  1476. del_timer_sync(&vport->els_tmofunc);
  1477. del_timer_sync(&vport->fc_fdmitmo);
  1478. lpfc_can_disctmo(vport);
  1479. return;
  1480. }
  1481. /**
  1482. * lpfc_stop_phba_timers: Stop all the timers associated with an HBA.
  1483. * @phba: pointer to lpfc hba data structure.
  1484. *
  1485. * This routine stops all the timers associated with a HBA. This function is
  1486. * invoked before either putting a HBA offline or unloading the driver.
  1487. **/
  1488. static void
  1489. lpfc_stop_phba_timers(struct lpfc_hba *phba)
  1490. {
  1491. del_timer_sync(&phba->fcp_poll_timer);
  1492. lpfc_stop_vport_timers(phba->pport);
  1493. del_timer_sync(&phba->sli.mbox_tmo);
  1494. del_timer_sync(&phba->fabric_block_timer);
  1495. phba->hb_outstanding = 0;
  1496. del_timer_sync(&phba->hb_tmofunc);
  1497. return;
  1498. }
  1499. /**
  1500. * lpfc_block_mgmt_io: Mark a HBA's management interface as blocked.
  1501. * @phba: pointer to lpfc hba data structure.
  1502. *
  1503. * This routine marks a HBA's management interface as blocked. Once the HBA's
  1504. * management interface is marked as blocked, all the user space access to
  1505. * the HBA, whether they are from sysfs interface or libdfc interface will
  1506. * all be blocked. The HBA is set to block the management interface when the
  1507. * driver prepares the HBA interface for online or offline.
  1508. **/
  1509. static void
  1510. lpfc_block_mgmt_io(struct lpfc_hba * phba)
  1511. {
  1512. unsigned long iflag;
  1513. spin_lock_irqsave(&phba->hbalock, iflag);
  1514. phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
  1515. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1516. }
  1517. /**
  1518. * lpfc_online: Initialize and bring a HBA online.
  1519. * @phba: pointer to lpfc hba data structure.
  1520. *
  1521. * This routine initializes the HBA and brings a HBA online. During this
  1522. * process, the management interface is blocked to prevent user space access
  1523. * to the HBA interfering with the driver initialization.
  1524. *
  1525. * Return codes
  1526. * 0 - successful
  1527. * 1 - failed
  1528. **/
  1529. int
  1530. lpfc_online(struct lpfc_hba *phba)
  1531. {
  1532. struct lpfc_vport *vport = phba->pport;
  1533. struct lpfc_vport **vports;
  1534. int i;
  1535. if (!phba)
  1536. return 0;
  1537. if (!(vport->fc_flag & FC_OFFLINE_MODE))
  1538. return 0;
  1539. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  1540. "0458 Bring Adapter online\n");
  1541. lpfc_block_mgmt_io(phba);
  1542. if (!lpfc_sli_queue_setup(phba)) {
  1543. lpfc_unblock_mgmt_io(phba);
  1544. return 1;
  1545. }
  1546. if (lpfc_sli_hba_setup(phba)) { /* Initialize the HBA */
  1547. lpfc_unblock_mgmt_io(phba);
  1548. return 1;
  1549. }
  1550. vports = lpfc_create_vport_work_array(phba);
  1551. if (vports != NULL)
  1552. for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  1553. struct Scsi_Host *shost;
  1554. shost = lpfc_shost_from_vport(vports[i]);
  1555. spin_lock_irq(shost->host_lock);
  1556. vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
  1557. if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
  1558. vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
  1559. spin_unlock_irq(shost->host_lock);
  1560. }
  1561. lpfc_destroy_vport_work_array(phba, vports);
  1562. lpfc_unblock_mgmt_io(phba);
  1563. return 0;
  1564. }
  1565. /**
  1566. * lpfc_unblock_mgmt_io: Mark a HBA's management interface to be not blocked.
  1567. * @phba: pointer to lpfc hba data structure.
  1568. *
  1569. * This routine marks a HBA's management interface as not blocked. Once the
  1570. * HBA's management interface is marked as not blocked, all the user space
  1571. * access to the HBA, whether they are from sysfs interface or libdfc
  1572. * interface will be allowed. The HBA is set to block the management interface
  1573. * when the driver prepares the HBA interface for online or offline and then
  1574. * set to unblock the management interface afterwards.
  1575. **/
  1576. void
  1577. lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
  1578. {
  1579. unsigned long iflag;
  1580. spin_lock_irqsave(&phba->hbalock, iflag);
  1581. phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
  1582. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1583. }
  1584. /**
  1585. * lpfc_offline_prep: Prepare a HBA to be brought offline.
  1586. * @phba: pointer to lpfc hba data structure.
  1587. *
  1588. * This routine is invoked to prepare a HBA to be brought offline. It performs
  1589. * unregistration login to all the nodes on all vports and flushes the mailbox
  1590. * queue to make it ready to be brought offline.
  1591. **/
  1592. void
  1593. lpfc_offline_prep(struct lpfc_hba * phba)
  1594. {
  1595. struct lpfc_vport *vport = phba->pport;
  1596. struct lpfc_nodelist *ndlp, *next_ndlp;
  1597. struct lpfc_vport **vports;
  1598. int i;
  1599. if (vport->fc_flag & FC_OFFLINE_MODE)
  1600. return;
  1601. lpfc_block_mgmt_io(phba);
  1602. lpfc_linkdown(phba);
  1603. /* Issue an unreg_login to all nodes on all vports */
  1604. vports = lpfc_create_vport_work_array(phba);
  1605. if (vports != NULL) {
  1606. for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  1607. struct Scsi_Host *shost;
  1608. if (vports[i]->load_flag & FC_UNLOADING)
  1609. continue;
  1610. shost = lpfc_shost_from_vport(vports[i]);
  1611. list_for_each_entry_safe(ndlp, next_ndlp,
  1612. &vports[i]->fc_nodes,
  1613. nlp_listp) {
  1614. if (!NLP_CHK_NODE_ACT(ndlp))
  1615. continue;
  1616. if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
  1617. continue;
  1618. if (ndlp->nlp_type & NLP_FABRIC) {
  1619. lpfc_disc_state_machine(vports[i], ndlp,
  1620. NULL, NLP_EVT_DEVICE_RECOVERY);
  1621. lpfc_disc_state_machine(vports[i], ndlp,
  1622. NULL, NLP_EVT_DEVICE_RM);
  1623. }
  1624. spin_lock_irq(shost->host_lock);
  1625. ndlp->nlp_flag &= ~NLP_NPR_ADISC;
  1626. spin_unlock_irq(shost->host_lock);
  1627. lpfc_unreg_rpi(vports[i], ndlp);
  1628. }
  1629. }
  1630. }
  1631. lpfc_destroy_vport_work_array(phba, vports);
  1632. lpfc_sli_flush_mbox_queue(phba);
  1633. }
  1634. /**
  1635. * lpfc_offline: Bring a HBA offline.
  1636. * @phba: pointer to lpfc hba data structure.
  1637. *
  1638. * This routine actually brings a HBA offline. It stops all the timers
  1639. * associated with the HBA, brings down the SLI layer, and eventually
  1640. * marks the HBA as in offline state for the upper layer protocol.
  1641. **/
  1642. void
  1643. lpfc_offline(struct lpfc_hba *phba)
  1644. {
  1645. struct Scsi_Host *shost;
  1646. struct lpfc_vport **vports;
  1647. int i;
  1648. if (phba->pport->fc_flag & FC_OFFLINE_MODE)
  1649. return;
  1650. /* stop all timers associated with this hba */
  1651. lpfc_stop_phba_timers(phba);
  1652. vports = lpfc_create_vport_work_array(phba);
  1653. if (vports != NULL)
  1654. for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
  1655. lpfc_stop_vport_timers(vports[i]);
  1656. lpfc_destroy_vport_work_array(phba, vports);
  1657. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  1658. "0460 Bring Adapter offline\n");
  1659. /* Bring down the SLI Layer and cleanup. The HBA is offline
  1660. now. */
  1661. lpfc_sli_hba_down(phba);
  1662. spin_lock_irq(&phba->hbalock);
  1663. phba->work_ha = 0;
  1664. spin_unlock_irq(&phba->hbalock);
  1665. vports = lpfc_create_vport_work_array(phba);
  1666. if (vports != NULL)
  1667. for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  1668. shost = lpfc_shost_from_vport(vports[i]);
  1669. spin_lock_irq(shost->host_lock);
  1670. vports[i]->work_port_events = 0;
  1671. vports[i]->fc_flag |= FC_OFFLINE_MODE;
  1672. spin_unlock_irq(shost->host_lock);
  1673. }
  1674. lpfc_destroy_vport_work_array(phba, vports);
  1675. }
  1676. /**
  1677. * lpfc_scsi_free: Free all the SCSI buffers and IOCBs from driver lists.
  1678. * @phba: pointer to lpfc hba data structure.
  1679. *
  1680. * This routine is to free all the SCSI buffers and IOCBs from the driver
  1681. * list back to kernel. It is called from lpfc_pci_remove_one to free
  1682. * the internal resources before the device is removed from the system.
  1683. *
  1684. * Return codes
  1685. * 0 - successful (for now, it always returns 0)
  1686. **/
  1687. static int
  1688. lpfc_scsi_free(struct lpfc_hba *phba)
  1689. {
  1690. struct lpfc_scsi_buf *sb, *sb_next;
  1691. struct lpfc_iocbq *io, *io_next;
  1692. spin_lock_irq(&phba->hbalock);
  1693. /* Release all the lpfc_scsi_bufs maintained by this host. */
  1694. list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
  1695. list_del(&sb->list);
  1696. pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
  1697. sb->dma_handle);
  1698. kfree(sb);
  1699. phba->total_scsi_bufs--;
  1700. }
  1701. /* Release all the lpfc_iocbq entries maintained by this host. */
  1702. list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
  1703. list_del(&io->list);
  1704. kfree(io);
  1705. phba->total_iocbq_bufs--;
  1706. }
  1707. spin_unlock_irq(&phba->hbalock);
  1708. return 0;
  1709. }
  1710. /**
  1711. * lpfc_create_port: Create an FC port.
  1712. * @phba: pointer to lpfc hba data structure.
  1713. * @instance: a unique integer ID to this FC port.
  1714. * @dev: pointer to the device data structure.
  1715. *
  1716. * This routine creates a FC port for the upper layer protocol. The FC port
  1717. * can be created on top of either a physical port or a virtual port provided
  1718. * by the HBA. This routine also allocates a SCSI host data structure (shost)
  1719. * and associates the FC port created before adding the shost into the SCSI
  1720. * layer.
  1721. *
  1722. * Return codes
  1723. * @vport - pointer to the virtual N_Port data structure.
  1724. * NULL - port create failed.
  1725. **/
  1726. struct lpfc_vport *
  1727. lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
  1728. {
  1729. struct lpfc_vport *vport;
  1730. struct Scsi_Host *shost;
  1731. int error = 0;
  1732. if (dev != &phba->pcidev->dev)
  1733. shost = scsi_host_alloc(&lpfc_vport_template,
  1734. sizeof(struct lpfc_vport));
  1735. else
  1736. shost = scsi_host_alloc(&lpfc_template,
  1737. sizeof(struct lpfc_vport));
  1738. if (!shost)
  1739. goto out;
  1740. vport = (struct lpfc_vport *) shost->hostdata;
  1741. vport->phba = phba;
  1742. vport->load_flag |= FC_LOADING;
  1743. vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
  1744. vport->fc_rscn_flush = 0;
  1745. lpfc_get_vport_cfgparam(vport);
  1746. shost->unique_id = instance;
  1747. shost->max_id = LPFC_MAX_TARGET;
  1748. shost->max_lun = vport->cfg_max_luns;
  1749. shost->this_id = -1;
  1750. shost->max_cmd_len = 16;
  1751. /*
  1752. * Set initial can_queue value since 0 is no longer supported and
  1753. * scsi_add_host will fail. This will be adjusted later based on the
  1754. * max xri value determined in hba setup.
  1755. */
  1756. shost->can_queue = phba->cfg_hba_queue_depth - 10;
  1757. if (dev != &phba->pcidev->dev) {
  1758. shost->transportt = lpfc_vport_transport_template;
  1759. vport->port_type = LPFC_NPIV_PORT;
  1760. } else {
  1761. shost->transportt = lpfc_transport_template;
  1762. vport->port_type = LPFC_PHYSICAL_PORT;
  1763. }
  1764. /* Initialize all internally managed lists. */
  1765. INIT_LIST_HEAD(&vport->fc_nodes);
  1766. spin_lock_init(&vport->work_port_lock);
  1767. init_timer(&vport->fc_disctmo);
  1768. vport->fc_disctmo.function = lpfc_disc_timeout;
  1769. vport->fc_disctmo.data = (unsigned long)vport;
  1770. init_timer(&vport->fc_fdmitmo);
  1771. vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
  1772. vport->fc_fdmitmo.data = (unsigned long)vport;
  1773. init_timer(&vport->els_tmofunc);
  1774. vport->els_tmofunc.function = lpfc_els_timeout;
  1775. vport->els_tmofunc.data = (unsigned long)vport;
  1776. error = scsi_add_host(shost, dev);
  1777. if (error)
  1778. goto out_put_shost;
  1779. spin_lock_irq(&phba->hbalock);
  1780. list_add_tail(&vport->listentry, &phba->port_list);
  1781. spin_unlock_irq(&phba->hbalock);
  1782. return vport;
  1783. out_put_shost:
  1784. scsi_host_put(shost);
  1785. out:
  1786. return NULL;
  1787. }
  1788. /**
  1789. * destroy_port: Destroy an FC port.
  1790. * @vport: pointer to an lpfc virtual N_Port data structure.
  1791. *
  1792. * This routine destroys a FC port from the upper layer protocol. All the
  1793. * resources associated with the port are released.
  1794. **/
  1795. void
  1796. destroy_port(struct lpfc_vport *vport)
  1797. {
  1798. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  1799. struct lpfc_hba *phba = vport->phba;
  1800. kfree(vport->vname);
  1801. lpfc_debugfs_terminate(vport);
  1802. fc_remove_host(shost);
  1803. scsi_remove_host(shost);
  1804. spin_lock_irq(&phba->hbalock);
  1805. list_del_init(&vport->listentry);
  1806. spin_unlock_irq(&phba->hbalock);
  1807. lpfc_cleanup(vport);
  1808. return;
  1809. }
  1810. /**
  1811. * lpfc_get_instance: Get a unique integer ID.
  1812. *
  1813. * This routine allocates a unique integer ID from lpfc_hba_index pool. It
  1814. * uses the kernel idr facility to perform the task.
  1815. *
  1816. * Return codes:
  1817. * instance - a unique integer ID allocated as the new instance.
  1818. * -1 - lpfc get instance failed.
  1819. **/
  1820. int
  1821. lpfc_get_instance(void)
  1822. {
  1823. int instance = 0;
  1824. /* Assign an unused number */
  1825. if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
  1826. return -1;
  1827. if (idr_get_new(&lpfc_hba_index, NULL, &instance))
  1828. return -1;
  1829. return instance;
  1830. }
  1831. /**
  1832. * lpfc_scan_finished: method for SCSI layer to detect whether scan is done.
  1833. * @shost: pointer to SCSI host data structure.
  1834. * @time: elapsed time of the scan in jiffies.
  1835. *
  1836. * This routine is called by the SCSI layer with a SCSI host to determine
  1837. * whether the scan host is finished.
  1838. *
  1839. * Note: there is no scan_start function as adapter initialization will have
  1840. * asynchronously kicked off the link initialization.
  1841. *
  1842. * Return codes
  1843. * 0 - SCSI host scan is not over yet.
  1844. * 1 - SCSI host scan is over.
  1845. **/
  1846. int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
  1847. {
  1848. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  1849. struct lpfc_hba *phba = vport->phba;
  1850. int stat = 0;
  1851. spin_lock_irq(shost->host_lock);
  1852. if (vport->load_flag & FC_UNLOADING) {
  1853. stat = 1;
  1854. goto finished;
  1855. }
  1856. if (time >= 30 * HZ) {
  1857. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  1858. "0461 Scanning longer than 30 "
  1859. "seconds. Continuing initialization\n");
  1860. stat = 1;
  1861. goto finished;
  1862. }
  1863. if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
  1864. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  1865. "0465 Link down longer than 15 "
  1866. "seconds. Continuing initialization\n");
  1867. stat = 1;
  1868. goto finished;
  1869. }
  1870. if (vport->port_state != LPFC_VPORT_READY)
  1871. goto finished;
  1872. if (vport->num_disc_nodes || vport->fc_prli_sent)
  1873. goto finished;
  1874. if (vport->fc_map_cnt == 0 && time < 2 * HZ)
  1875. goto finished;
  1876. if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
  1877. goto finished;
  1878. stat = 1;
  1879. finished:
  1880. spin_unlock_irq(shost->host_lock);
  1881. return stat;
  1882. }
  1883. /**
  1884. * lpfc_host_attrib_init: Initialize SCSI host attributes on a FC port.
  1885. * @shost: pointer to SCSI host data structure.
  1886. *
  1887. * This routine initializes a given SCSI host attributes on a FC port. The
  1888. * SCSI host can be either on top of a physical port or a virtual port.
  1889. **/
  1890. void lpfc_host_attrib_init(struct Scsi_Host *shost)
  1891. {
  1892. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  1893. struct lpfc_hba *phba = vport->phba;
  1894. /*
  1895. * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
  1896. */
  1897. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  1898. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  1899. fc_host_supported_classes(shost) = FC_COS_CLASS3;
  1900. memset(fc_host_supported_fc4s(shost), 0,
  1901. sizeof(fc_host_supported_fc4s(shost)));
  1902. fc_host_supported_fc4s(shost)[2] = 1;
  1903. fc_host_supported_fc4s(shost)[7] = 1;
  1904. lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
  1905. sizeof fc_host_symbolic_name(shost));
  1906. fc_host_supported_speeds(shost) = 0;
  1907. if (phba->lmt & LMT_10Gb)
  1908. fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
  1909. if (phba->lmt & LMT_8Gb)
  1910. fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
  1911. if (phba->lmt & LMT_4Gb)
  1912. fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
  1913. if (phba->lmt & LMT_2Gb)
  1914. fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
  1915. if (phba->lmt & LMT_1Gb)
  1916. fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
  1917. fc_host_maxframe_size(shost) =
  1918. (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
  1919. (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
  1920. /* This value is also unchanging */
  1921. memset(fc_host_active_fc4s(shost), 0,
  1922. sizeof(fc_host_active_fc4s(shost)));
  1923. fc_host_active_fc4s(shost)[2] = 1;
  1924. fc_host_active_fc4s(shost)[7] = 1;
  1925. fc_host_max_npiv_vports(shost) = phba->max_vpi;
  1926. spin_lock_irq(shost->host_lock);
  1927. vport->load_flag &= ~FC_LOADING;
  1928. spin_unlock_irq(shost->host_lock);
  1929. }
  1930. /**
  1931. * lpfc_enable_msix: Enable MSI-X interrupt mode.
  1932. * @phba: pointer to lpfc hba data structure.
  1933. *
  1934. * This routine is invoked to enable the MSI-X interrupt vectors. The kernel
  1935. * function pci_enable_msix() is called to enable the MSI-X vectors. Note that
  1936. * pci_enable_msix(), once invoked, enables either all or nothing, depending
  1937. * on the current availability of PCI vector resources. The device driver is
  1938. * responsible for calling the individual request_irq() to register each MSI-X
  1939. * vector with a interrupt handler, which is done in this function. Note that
  1940. * later when device is unloading, the driver should always call free_irq()
  1941. * on all MSI-X vectors it has done request_irq() on before calling
  1942. * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
  1943. * will be left with MSI-X enabled and leaks its vectors.
  1944. *
  1945. * Return codes
  1946. * 0 - sucessful
  1947. * other values - error
  1948. **/
  1949. static int
  1950. lpfc_enable_msix(struct lpfc_hba *phba)
  1951. {
  1952. int error;
  1953. phba->msix_entries[0].entry = 0;
  1954. phba->msix_entries[0].vector = 0;
  1955. error = pci_enable_msix(phba->pcidev, phba->msix_entries,
  1956. ARRAY_SIZE(phba->msix_entries));
  1957. if (error) {
  1958. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  1959. "0420 Enable MSI-X failed (%d), continuing "
  1960. "with MSI\n", error);
  1961. pci_disable_msix(phba->pcidev);
  1962. return error;
  1963. }
  1964. error = request_irq(phba->msix_entries[0].vector, lpfc_intr_handler, 0,
  1965. LPFC_DRIVER_NAME, phba);
  1966. if (error) {
  1967. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1968. "0421 MSI-X request_irq failed (%d), "
  1969. "continuing with MSI\n", error);
  1970. pci_disable_msix(phba->pcidev);
  1971. }
  1972. return error;
  1973. }
  1974. /**
  1975. * lpfc_disable_msix: Disable MSI-X interrupt mode.
  1976. * @phba: pointer to lpfc hba data structure.
  1977. *
  1978. * This routine is invoked to release the MSI-X vectors and then disable the
  1979. * MSI-X interrupt mode.
  1980. **/
  1981. static void
  1982. lpfc_disable_msix(struct lpfc_hba *phba)
  1983. {
  1984. free_irq(phba->msix_entries[0].vector, phba);
  1985. pci_disable_msix(phba->pcidev);
  1986. }
  1987. /**
  1988. * lpfc_pci_probe_one: lpfc PCI probe func to register device to PCI subsystem.
  1989. * @pdev: pointer to PCI device
  1990. * @pid: pointer to PCI device identifier
  1991. *
  1992. * This routine is to be registered to the kernel's PCI subsystem. When an
  1993. * Emulex HBA is presented in PCI bus, the kernel PCI subsystem looks at
  1994. * PCI device-specific information of the device and driver to see if the
  1995. * driver state that it can support this kind of device. If the match is
  1996. * successful, the driver core invokes this routine. If this routine
  1997. * determines it can claim the HBA, it does all the initialization that it
  1998. * needs to do to handle the HBA properly.
  1999. *
  2000. * Return code
  2001. * 0 - driver can claim the device
  2002. * negative value - driver can not claim the device
  2003. **/
  2004. static int __devinit
  2005. lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
  2006. {
  2007. struct lpfc_vport *vport = NULL;
  2008. struct lpfc_hba *phba;
  2009. struct lpfc_sli *psli;
  2010. struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
  2011. struct Scsi_Host *shost = NULL;
  2012. void *ptr;
  2013. unsigned long bar0map_len, bar2map_len;
  2014. int error = -ENODEV, retval;
  2015. int i, hbq_count;
  2016. uint16_t iotag;
  2017. int bars = pci_select_bars(pdev, IORESOURCE_MEM);
  2018. if (pci_enable_device_mem(pdev))
  2019. goto out;
  2020. if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
  2021. goto out_disable_device;
  2022. phba = kzalloc(sizeof (struct lpfc_hba), GFP_KERNEL);
  2023. if (!phba)
  2024. goto out_release_regions;
  2025. spin_lock_init(&phba->hbalock);
  2026. /* Initialize ndlp management spinlock */
  2027. spin_lock_init(&phba->ndlp_lock);
  2028. phba->pcidev = pdev;
  2029. /* Assign an unused board number */
  2030. if ((phba->brd_no = lpfc_get_instance()) < 0)
  2031. goto out_free_phba;
  2032. INIT_LIST_HEAD(&phba->port_list);
  2033. /*
  2034. * Get all the module params for configuring this host and then
  2035. * establish the host.
  2036. */
  2037. lpfc_get_cfgparam(phba);
  2038. phba->max_vpi = LPFC_MAX_VPI;
  2039. /* Initialize timers used by driver */
  2040. init_timer(&phba->hb_tmofunc);
  2041. phba->hb_tmofunc.function = lpfc_hb_timeout;
  2042. phba->hb_tmofunc.data = (unsigned long)phba;
  2043. psli = &phba->sli;
  2044. init_timer(&psli->mbox_tmo);
  2045. psli->mbox_tmo.function = lpfc_mbox_timeout;
  2046. psli->mbox_tmo.data = (unsigned long) phba;
  2047. init_timer(&phba->fcp_poll_timer);
  2048. phba->fcp_poll_timer.function = lpfc_poll_timeout;
  2049. phba->fcp_poll_timer.data = (unsigned long) phba;
  2050. init_timer(&phba->fabric_block_timer);
  2051. phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
  2052. phba->fabric_block_timer.data = (unsigned long) phba;
  2053. pci_set_master(pdev);
  2054. pci_try_set_mwi(pdev);
  2055. if (pci_set_dma_mask(phba->pcidev, DMA_64BIT_MASK) != 0)
  2056. if (pci_set_dma_mask(phba->pcidev, DMA_32BIT_MASK) != 0)
  2057. goto out_idr_remove;
  2058. /*
  2059. * Get the bus address of Bar0 and Bar2 and the number of bytes
  2060. * required by each mapping.
  2061. */
  2062. phba->pci_bar0_map = pci_resource_start(phba->pcidev, 0);
  2063. bar0map_len = pci_resource_len(phba->pcidev, 0);
  2064. phba->pci_bar2_map = pci_resource_start(phba->pcidev, 2);
  2065. bar2map_len = pci_resource_len(phba->pcidev, 2);
  2066. /* Map HBA SLIM to a kernel virtual address. */
  2067. phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
  2068. if (!phba->slim_memmap_p) {
  2069. error = -ENODEV;
  2070. dev_printk(KERN_ERR, &pdev->dev,
  2071. "ioremap failed for SLIM memory.\n");
  2072. goto out_idr_remove;
  2073. }
  2074. /* Map HBA Control Registers to a kernel virtual address. */
  2075. phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
  2076. if (!phba->ctrl_regs_memmap_p) {
  2077. error = -ENODEV;
  2078. dev_printk(KERN_ERR, &pdev->dev,
  2079. "ioremap failed for HBA control registers.\n");
  2080. goto out_iounmap_slim;
  2081. }
  2082. /* Allocate memory for SLI-2 structures */
  2083. phba->slim2p.virt = dma_alloc_coherent(&phba->pcidev->dev,
  2084. SLI2_SLIM_SIZE,
  2085. &phba->slim2p.phys,
  2086. GFP_KERNEL);
  2087. if (!phba->slim2p.virt)
  2088. goto out_iounmap;
  2089. memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
  2090. phba->mbox = phba->slim2p.virt;
  2091. phba->pcb = (phba->slim2p.virt + sizeof(MAILBOX_t));
  2092. phba->IOCBs = (phba->slim2p.virt + sizeof(MAILBOX_t) +
  2093. sizeof(struct _PCB));
  2094. phba->hbqslimp.virt = dma_alloc_coherent(&phba->pcidev->dev,
  2095. lpfc_sli_hbq_size(),
  2096. &phba->hbqslimp.phys,
  2097. GFP_KERNEL);
  2098. if (!phba->hbqslimp.virt)
  2099. goto out_free_slim;
  2100. hbq_count = lpfc_sli_hbq_count();
  2101. ptr = phba->hbqslimp.virt;
  2102. for (i = 0; i < hbq_count; ++i) {
  2103. phba->hbqs[i].hbq_virt = ptr;
  2104. INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
  2105. ptr += (lpfc_hbq_defs[i]->entry_count *
  2106. sizeof(struct lpfc_hbq_entry));
  2107. }
  2108. phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
  2109. phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
  2110. memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
  2111. INIT_LIST_HEAD(&phba->hbqbuf_in_list);
  2112. /* Initialize the SLI Layer to run with lpfc HBAs. */
  2113. lpfc_sli_setup(phba);
  2114. lpfc_sli_queue_setup(phba);
  2115. retval = lpfc_mem_alloc(phba);
  2116. if (retval) {
  2117. error = retval;
  2118. goto out_free_hbqslimp;
  2119. }
  2120. /* Initialize and populate the iocb list per host. */
  2121. INIT_LIST_HEAD(&phba->lpfc_iocb_list);
  2122. for (i = 0; i < LPFC_IOCB_LIST_CNT; i++) {
  2123. iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
  2124. if (iocbq_entry == NULL) {
  2125. printk(KERN_ERR "%s: only allocated %d iocbs of "
  2126. "expected %d count. Unloading driver.\n",
  2127. __func__, i, LPFC_IOCB_LIST_CNT);
  2128. error = -ENOMEM;
  2129. goto out_free_iocbq;
  2130. }
  2131. iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
  2132. if (iotag == 0) {
  2133. kfree (iocbq_entry);
  2134. printk(KERN_ERR "%s: failed to allocate IOTAG. "
  2135. "Unloading driver.\n",
  2136. __func__);
  2137. error = -ENOMEM;
  2138. goto out_free_iocbq;
  2139. }
  2140. spin_lock_irq(&phba->hbalock);
  2141. list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
  2142. phba->total_iocbq_bufs++;
  2143. spin_unlock_irq(&phba->hbalock);
  2144. }
  2145. /* Initialize HBA structure */
  2146. phba->fc_edtov = FF_DEF_EDTOV;
  2147. phba->fc_ratov = FF_DEF_RATOV;
  2148. phba->fc_altov = FF_DEF_ALTOV;
  2149. phba->fc_arbtov = FF_DEF_ARBTOV;
  2150. INIT_LIST_HEAD(&phba->work_list);
  2151. phba->work_ha_mask = (HA_ERATT|HA_MBATT|HA_LATT);
  2152. phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
  2153. /* Initialize the wait queue head for the kernel thread */
  2154. init_waitqueue_head(&phba->work_waitq);
  2155. /* Startup the kernel thread for this host adapter. */
  2156. phba->worker_thread = kthread_run(lpfc_do_work, phba,
  2157. "lpfc_worker_%d", phba->brd_no);
  2158. if (IS_ERR(phba->worker_thread)) {
  2159. error = PTR_ERR(phba->worker_thread);
  2160. goto out_free_iocbq;
  2161. }
  2162. /* Initialize the list of scsi buffers used by driver for scsi IO. */
  2163. spin_lock_init(&phba->scsi_buf_list_lock);
  2164. INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
  2165. /* Initialize list of fabric iocbs */
  2166. INIT_LIST_HEAD(&phba->fabric_iocb_list);
  2167. /* Initialize list to save ELS buffers */
  2168. INIT_LIST_HEAD(&phba->elsbuf);
  2169. vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
  2170. if (!vport)
  2171. goto out_kthread_stop;
  2172. shost = lpfc_shost_from_vport(vport);
  2173. phba->pport = vport;
  2174. lpfc_debugfs_initialize(vport);
  2175. pci_set_drvdata(pdev, shost);
  2176. phba->intr_type = NONE;
  2177. if (phba->cfg_use_msi == 2) {
  2178. error = lpfc_enable_msix(phba);
  2179. if (!error)
  2180. phba->intr_type = MSIX;
  2181. }
  2182. /* Fallback to MSI if MSI-X initialization failed */
  2183. if (phba->cfg_use_msi >= 1 && phba->intr_type == NONE) {
  2184. retval = pci_enable_msi(phba->pcidev);
  2185. if (!retval)
  2186. phba->intr_type = MSI;
  2187. else
  2188. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  2189. "0452 Enable MSI failed, continuing "
  2190. "with IRQ\n");
  2191. }
  2192. /* MSI-X is the only case the doesn't need to call request_irq */
  2193. if (phba->intr_type != MSIX) {
  2194. retval = request_irq(phba->pcidev->irq, lpfc_intr_handler,
  2195. IRQF_SHARED, LPFC_DRIVER_NAME, phba);
  2196. if (retval) {
  2197. lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0451 Enable "
  2198. "interrupt handler failed\n");
  2199. error = retval;
  2200. goto out_disable_msi;
  2201. } else if (phba->intr_type != MSI)
  2202. phba->intr_type = INTx;
  2203. }
  2204. phba->MBslimaddr = phba->slim_memmap_p;
  2205. phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
  2206. phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
  2207. phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
  2208. phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
  2209. if (lpfc_alloc_sysfs_attr(vport)) {
  2210. error = -ENOMEM;
  2211. goto out_free_irq;
  2212. }
  2213. if (lpfc_sli_hba_setup(phba)) {
  2214. error = -ENODEV;
  2215. goto out_remove_device;
  2216. }
  2217. /*
  2218. * hba setup may have changed the hba_queue_depth so we need to adjust
  2219. * the value of can_queue.
  2220. */
  2221. shost->can_queue = phba->cfg_hba_queue_depth - 10;
  2222. lpfc_host_attrib_init(shost);
  2223. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  2224. spin_lock_irq(shost->host_lock);
  2225. lpfc_poll_start_timer(phba);
  2226. spin_unlock_irq(shost->host_lock);
  2227. }
  2228. scsi_scan_host(shost);
  2229. return 0;
  2230. out_remove_device:
  2231. lpfc_free_sysfs_attr(vport);
  2232. spin_lock_irq(shost->host_lock);
  2233. vport->load_flag |= FC_UNLOADING;
  2234. spin_unlock_irq(shost->host_lock);
  2235. out_free_irq:
  2236. lpfc_stop_phba_timers(phba);
  2237. phba->pport->work_port_events = 0;
  2238. if (phba->intr_type == MSIX)
  2239. lpfc_disable_msix(phba);
  2240. else
  2241. free_irq(phba->pcidev->irq, phba);
  2242. out_disable_msi:
  2243. if (phba->intr_type == MSI)
  2244. pci_disable_msi(phba->pcidev);
  2245. destroy_port(vport);
  2246. out_kthread_stop:
  2247. kthread_stop(phba->worker_thread);
  2248. out_free_iocbq:
  2249. list_for_each_entry_safe(iocbq_entry, iocbq_next,
  2250. &phba->lpfc_iocb_list, list) {
  2251. kfree(iocbq_entry);
  2252. phba->total_iocbq_bufs--;
  2253. }
  2254. lpfc_mem_free(phba);
  2255. out_free_hbqslimp:
  2256. dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
  2257. phba->hbqslimp.virt, phba->hbqslimp.phys);
  2258. out_free_slim:
  2259. dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
  2260. phba->slim2p.virt, phba->slim2p.phys);
  2261. out_iounmap:
  2262. iounmap(phba->ctrl_regs_memmap_p);
  2263. out_iounmap_slim:
  2264. iounmap(phba->slim_memmap_p);
  2265. out_idr_remove:
  2266. idr_remove(&lpfc_hba_index, phba->brd_no);
  2267. out_free_phba:
  2268. kfree(phba);
  2269. out_release_regions:
  2270. pci_release_selected_regions(pdev, bars);
  2271. out_disable_device:
  2272. pci_disable_device(pdev);
  2273. out:
  2274. pci_set_drvdata(pdev, NULL);
  2275. if (shost)
  2276. scsi_host_put(shost);
  2277. return error;
  2278. }
  2279. /**
  2280. * lpfc_pci_remove_one: lpfc PCI func to unregister device from PCI subsystem.
  2281. * @pdev: pointer to PCI device
  2282. *
  2283. * This routine is to be registered to the kernel's PCI subsystem. When an
  2284. * Emulex HBA is removed from PCI bus. It perform all the necessary cleanup
  2285. * for the HBA device to be removed from the PCI subsystem properly.
  2286. **/
  2287. static void __devexit
  2288. lpfc_pci_remove_one(struct pci_dev *pdev)
  2289. {
  2290. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  2291. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  2292. struct lpfc_hba *phba = vport->phba;
  2293. int bars = pci_select_bars(pdev, IORESOURCE_MEM);
  2294. spin_lock_irq(&phba->hbalock);
  2295. vport->load_flag |= FC_UNLOADING;
  2296. spin_unlock_irq(&phba->hbalock);
  2297. kfree(vport->vname);
  2298. lpfc_free_sysfs_attr(vport);
  2299. kthread_stop(phba->worker_thread);
  2300. fc_remove_host(shost);
  2301. scsi_remove_host(shost);
  2302. lpfc_cleanup(vport);
  2303. /*
  2304. * Bring down the SLI Layer. This step disable all interrupts,
  2305. * clears the rings, discards all mailbox commands, and resets
  2306. * the HBA.
  2307. */
  2308. lpfc_sli_hba_down(phba);
  2309. lpfc_sli_brdrestart(phba);
  2310. lpfc_stop_phba_timers(phba);
  2311. spin_lock_irq(&phba->hbalock);
  2312. list_del_init(&vport->listentry);
  2313. spin_unlock_irq(&phba->hbalock);
  2314. lpfc_debugfs_terminate(vport);
  2315. if (phba->intr_type == MSIX)
  2316. lpfc_disable_msix(phba);
  2317. else {
  2318. free_irq(phba->pcidev->irq, phba);
  2319. if (phba->intr_type == MSI)
  2320. pci_disable_msi(phba->pcidev);
  2321. }
  2322. pci_set_drvdata(pdev, NULL);
  2323. scsi_host_put(shost);
  2324. /*
  2325. * Call scsi_free before mem_free since scsi bufs are released to their
  2326. * corresponding pools here.
  2327. */
  2328. lpfc_scsi_free(phba);
  2329. lpfc_mem_free(phba);
  2330. dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
  2331. phba->hbqslimp.virt, phba->hbqslimp.phys);
  2332. /* Free resources associated with SLI2 interface */
  2333. dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
  2334. phba->slim2p.virt, phba->slim2p.phys);
  2335. /* unmap adapter SLIM and Control Registers */
  2336. iounmap(phba->ctrl_regs_memmap_p);
  2337. iounmap(phba->slim_memmap_p);
  2338. idr_remove(&lpfc_hba_index, phba->brd_no);
  2339. kfree(phba);
  2340. pci_release_selected_regions(pdev, bars);
  2341. pci_disable_device(pdev);
  2342. }
  2343. /**
  2344. * lpfc_io_error_detected: Driver method for handling PCI I/O error detected.
  2345. * @pdev: pointer to PCI device.
  2346. * @state: the current PCI connection state.
  2347. *
  2348. * This routine is registered to the PCI subsystem for error handling. This
  2349. * function is called by the PCI subsystem after a PCI bus error affecting
  2350. * this device has been detected. When this function is invoked, it will
  2351. * need to stop all the I/Os and interrupt(s) to the device. Once that is
  2352. * done, it will return PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to
  2353. * perform proper recovery as desired.
  2354. *
  2355. * Return codes
  2356. * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
  2357. * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
  2358. **/
  2359. static pci_ers_result_t lpfc_io_error_detected(struct pci_dev *pdev,
  2360. pci_channel_state_t state)
  2361. {
  2362. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  2363. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  2364. struct lpfc_sli *psli = &phba->sli;
  2365. struct lpfc_sli_ring *pring;
  2366. if (state == pci_channel_io_perm_failure)
  2367. return PCI_ERS_RESULT_DISCONNECT;
  2368. pci_disable_device(pdev);
  2369. /*
  2370. * There may be I/Os dropped by the firmware.
  2371. * Error iocb (I/O) on txcmplq and let the SCSI layer
  2372. * retry it after re-establishing link.
  2373. */
  2374. pring = &psli->ring[psli->fcp_ring];
  2375. lpfc_sli_abort_iocb_ring(phba, pring);
  2376. if (phba->intr_type == MSIX)
  2377. lpfc_disable_msix(phba);
  2378. else {
  2379. free_irq(phba->pcidev->irq, phba);
  2380. if (phba->intr_type == MSI)
  2381. pci_disable_msi(phba->pcidev);
  2382. }
  2383. /* Request a slot reset. */
  2384. return PCI_ERS_RESULT_NEED_RESET;
  2385. }
  2386. /**
  2387. * lpfc_io_slot_reset: Restart a PCI device from scratch.
  2388. * @pdev: pointer to PCI device.
  2389. *
  2390. * This routine is registered to the PCI subsystem for error handling. This is
  2391. * called after PCI bus has been reset to restart the PCI card from scratch,
  2392. * as if from a cold-boot. During the PCI subsystem error recovery, after the
  2393. * driver returns PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform
  2394. * proper error recovery and then call this routine before calling the .resume
  2395. * method to recover the device. This function will initialize the HBA device,
  2396. * enable the interrupt, but it will just put the HBA to offline state without
  2397. * passing any I/O traffic.
  2398. *
  2399. * Return codes
  2400. * PCI_ERS_RESULT_RECOVERED - the device has been recovered
  2401. * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
  2402. */
  2403. static pci_ers_result_t lpfc_io_slot_reset(struct pci_dev *pdev)
  2404. {
  2405. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  2406. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  2407. struct lpfc_sli *psli = &phba->sli;
  2408. int error, retval;
  2409. dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
  2410. if (pci_enable_device_mem(pdev)) {
  2411. printk(KERN_ERR "lpfc: Cannot re-enable "
  2412. "PCI device after reset.\n");
  2413. return PCI_ERS_RESULT_DISCONNECT;
  2414. }
  2415. pci_set_master(pdev);
  2416. spin_lock_irq(&phba->hbalock);
  2417. psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
  2418. spin_unlock_irq(&phba->hbalock);
  2419. /* Enable configured interrupt method */
  2420. phba->intr_type = NONE;
  2421. if (phba->cfg_use_msi == 2) {
  2422. error = lpfc_enable_msix(phba);
  2423. if (!error)
  2424. phba->intr_type = MSIX;
  2425. }
  2426. /* Fallback to MSI if MSI-X initialization failed */
  2427. if (phba->cfg_use_msi >= 1 && phba->intr_type == NONE) {
  2428. retval = pci_enable_msi(phba->pcidev);
  2429. if (!retval)
  2430. phba->intr_type = MSI;
  2431. else
  2432. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  2433. "0470 Enable MSI failed, continuing "
  2434. "with IRQ\n");
  2435. }
  2436. /* MSI-X is the only case the doesn't need to call request_irq */
  2437. if (phba->intr_type != MSIX) {
  2438. retval = request_irq(phba->pcidev->irq, lpfc_intr_handler,
  2439. IRQF_SHARED, LPFC_DRIVER_NAME, phba);
  2440. if (retval) {
  2441. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2442. "0471 Enable interrupt handler "
  2443. "failed\n");
  2444. } else if (phba->intr_type != MSI)
  2445. phba->intr_type = INTx;
  2446. }
  2447. /* Take device offline; this will perform cleanup */
  2448. lpfc_offline(phba);
  2449. lpfc_sli_brdrestart(phba);
  2450. return PCI_ERS_RESULT_RECOVERED;
  2451. }
  2452. /**
  2453. * lpfc_io_resume: Resume PCI I/O operation.
  2454. * @pdev: pointer to PCI device
  2455. *
  2456. * This routine is registered to the PCI subsystem for error handling. It is
  2457. * called when kernel error recovery tells the lpfc driver that it is ok to
  2458. * resume normal PCI operation after PCI bus error recovery. After this call,
  2459. * traffic can start to flow from this device again.
  2460. */
  2461. static void lpfc_io_resume(struct pci_dev *pdev)
  2462. {
  2463. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  2464. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  2465. lpfc_online(phba);
  2466. }
  2467. static struct pci_device_id lpfc_id_table[] = {
  2468. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
  2469. PCI_ANY_ID, PCI_ANY_ID, },
  2470. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
  2471. PCI_ANY_ID, PCI_ANY_ID, },
  2472. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
  2473. PCI_ANY_ID, PCI_ANY_ID, },
  2474. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
  2475. PCI_ANY_ID, PCI_ANY_ID, },
  2476. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
  2477. PCI_ANY_ID, PCI_ANY_ID, },
  2478. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
  2479. PCI_ANY_ID, PCI_ANY_ID, },
  2480. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
  2481. PCI_ANY_ID, PCI_ANY_ID, },
  2482. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
  2483. PCI_ANY_ID, PCI_ANY_ID, },
  2484. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
  2485. PCI_ANY_ID, PCI_ANY_ID, },
  2486. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
  2487. PCI_ANY_ID, PCI_ANY_ID, },
  2488. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
  2489. PCI_ANY_ID, PCI_ANY_ID, },
  2490. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
  2491. PCI_ANY_ID, PCI_ANY_ID, },
  2492. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
  2493. PCI_ANY_ID, PCI_ANY_ID, },
  2494. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
  2495. PCI_ANY_ID, PCI_ANY_ID, },
  2496. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
  2497. PCI_ANY_ID, PCI_ANY_ID, },
  2498. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
  2499. PCI_ANY_ID, PCI_ANY_ID, },
  2500. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
  2501. PCI_ANY_ID, PCI_ANY_ID, },
  2502. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
  2503. PCI_ANY_ID, PCI_ANY_ID, },
  2504. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
  2505. PCI_ANY_ID, PCI_ANY_ID, },
  2506. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
  2507. PCI_ANY_ID, PCI_ANY_ID, },
  2508. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
  2509. PCI_ANY_ID, PCI_ANY_ID, },
  2510. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
  2511. PCI_ANY_ID, PCI_ANY_ID, },
  2512. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
  2513. PCI_ANY_ID, PCI_ANY_ID, },
  2514. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
  2515. PCI_ANY_ID, PCI_ANY_ID, },
  2516. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
  2517. PCI_ANY_ID, PCI_ANY_ID, },
  2518. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
  2519. PCI_ANY_ID, PCI_ANY_ID, },
  2520. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
  2521. PCI_ANY_ID, PCI_ANY_ID, },
  2522. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
  2523. PCI_ANY_ID, PCI_ANY_ID, },
  2524. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
  2525. PCI_ANY_ID, PCI_ANY_ID, },
  2526. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
  2527. PCI_ANY_ID, PCI_ANY_ID, },
  2528. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
  2529. PCI_ANY_ID, PCI_ANY_ID, },
  2530. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
  2531. PCI_ANY_ID, PCI_ANY_ID, },
  2532. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
  2533. PCI_ANY_ID, PCI_ANY_ID, },
  2534. { 0 }
  2535. };
  2536. MODULE_DEVICE_TABLE(pci, lpfc_id_table);
  2537. static struct pci_error_handlers lpfc_err_handler = {
  2538. .error_detected = lpfc_io_error_detected,
  2539. .slot_reset = lpfc_io_slot_reset,
  2540. .resume = lpfc_io_resume,
  2541. };
  2542. static struct pci_driver lpfc_driver = {
  2543. .name = LPFC_DRIVER_NAME,
  2544. .id_table = lpfc_id_table,
  2545. .probe = lpfc_pci_probe_one,
  2546. .remove = __devexit_p(lpfc_pci_remove_one),
  2547. .err_handler = &lpfc_err_handler,
  2548. };
  2549. /**
  2550. * lpfc_init: lpfc module initialization routine.
  2551. *
  2552. * This routine is to be invoked when the lpfc module is loaded into the
  2553. * kernel. The special kernel macro module_init() is used to indicate the
  2554. * role of this routine to the kernel as lpfc module entry point.
  2555. *
  2556. * Return codes
  2557. * 0 - successful
  2558. * -ENOMEM - FC attach transport failed
  2559. * all others - failed
  2560. */
  2561. static int __init
  2562. lpfc_init(void)
  2563. {
  2564. int error = 0;
  2565. printk(LPFC_MODULE_DESC "\n");
  2566. printk(LPFC_COPYRIGHT "\n");
  2567. if (lpfc_enable_npiv) {
  2568. lpfc_transport_functions.vport_create = lpfc_vport_create;
  2569. lpfc_transport_functions.vport_delete = lpfc_vport_delete;
  2570. }
  2571. lpfc_transport_template =
  2572. fc_attach_transport(&lpfc_transport_functions);
  2573. if (lpfc_transport_template == NULL)
  2574. return -ENOMEM;
  2575. if (lpfc_enable_npiv) {
  2576. lpfc_vport_transport_template =
  2577. fc_attach_transport(&lpfc_vport_transport_functions);
  2578. if (lpfc_vport_transport_template == NULL) {
  2579. fc_release_transport(lpfc_transport_template);
  2580. return -ENOMEM;
  2581. }
  2582. }
  2583. error = pci_register_driver(&lpfc_driver);
  2584. if (error) {
  2585. fc_release_transport(lpfc_transport_template);
  2586. fc_release_transport(lpfc_vport_transport_template);
  2587. }
  2588. return error;
  2589. }
  2590. /**
  2591. * lpfc_exit: lpfc module removal routine.
  2592. *
  2593. * This routine is invoked when the lpfc module is removed from the kernel.
  2594. * The special kernel macro module_exit() is used to indicate the role of
  2595. * this routine to the kernel as lpfc module exit point.
  2596. */
  2597. static void __exit
  2598. lpfc_exit(void)
  2599. {
  2600. pci_unregister_driver(&lpfc_driver);
  2601. fc_release_transport(lpfc_transport_template);
  2602. if (lpfc_enable_npiv)
  2603. fc_release_transport(lpfc_vport_transport_template);
  2604. }
  2605. module_init(lpfc_init);
  2606. module_exit(lpfc_exit);
  2607. MODULE_LICENSE("GPL");
  2608. MODULE_DESCRIPTION(LPFC_MODULE_DESC);
  2609. MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
  2610. MODULE_VERSION("0:" LPFC_DRIVER_VERSION);