lpfc_init.c 101 KB

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