lpfc_init.c 103 KB

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