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: Completion handler for dump memory mailbox
  218. * command used for getting wake up parameters.
  219. * @phba: pointer to lpfc hba data structure.
  220. * @pmboxq: pointer to the driver internal queue element for mailbox command.
  221. *
  222. * This is the completion handler for dump mailbox command for getting
  223. * wake up parameters. When this command complete, the response contain
  224. * Option rom version of the HBA. This function translate the version number
  225. * into a human readable string and store it in OptionROMVersion.
  226. **/
  227. static void
  228. lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  229. {
  230. struct prog_id *prg;
  231. uint32_t prog_id_word;
  232. char dist = ' ';
  233. /* character array used for decoding dist type. */
  234. char dist_char[] = "nabx";
  235. if (pmboxq->mb.mbxStatus != MBX_SUCCESS) {
  236. mempool_free(pmboxq, phba->mbox_mem_pool);
  237. return;
  238. }
  239. prg = (struct prog_id *) &prog_id_word;
  240. /* word 7 contain option rom version */
  241. prog_id_word = pmboxq->mb.un.varWords[7];
  242. /* Decode the Option rom version word to a readable string */
  243. if (prg->dist < 4)
  244. dist = dist_char[prg->dist];
  245. if ((prg->dist == 3) && (prg->num == 0))
  246. sprintf(phba->OptionROMVersion, "%d.%d%d",
  247. prg->ver, prg->rev, prg->lev);
  248. else
  249. sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
  250. prg->ver, prg->rev, prg->lev,
  251. dist, prg->num);
  252. mempool_free(pmboxq, phba->mbox_mem_pool);
  253. return;
  254. }
  255. /**
  256. * lpfc_config_port_post: Perform lpfc initialization after config port.
  257. * @phba: pointer to lpfc hba data structure.
  258. *
  259. * This routine will do LPFC initialization after the CONFIG_PORT mailbox
  260. * command call. It performs all internal resource and state setups on the
  261. * port: post IOCB buffers, enable appropriate host interrupt attentions,
  262. * ELS ring timers, etc.
  263. *
  264. * Return codes
  265. * 0 - success.
  266. * Any other value - error.
  267. **/
  268. int
  269. lpfc_config_port_post(struct lpfc_hba *phba)
  270. {
  271. struct lpfc_vport *vport = phba->pport;
  272. LPFC_MBOXQ_t *pmb;
  273. MAILBOX_t *mb;
  274. struct lpfc_dmabuf *mp;
  275. struct lpfc_sli *psli = &phba->sli;
  276. uint32_t status, timeout;
  277. int i, j;
  278. int rc;
  279. spin_lock_irq(&phba->hbalock);
  280. /*
  281. * If the Config port completed correctly the HBA is not
  282. * over heated any more.
  283. */
  284. if (phba->over_temp_state == HBA_OVER_TEMP)
  285. phba->over_temp_state = HBA_NORMAL_TEMP;
  286. spin_unlock_irq(&phba->hbalock);
  287. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  288. if (!pmb) {
  289. phba->link_state = LPFC_HBA_ERROR;
  290. return -ENOMEM;
  291. }
  292. mb = &pmb->mb;
  293. /* Get login parameters for NID. */
  294. lpfc_read_sparam(phba, pmb, 0);
  295. pmb->vport = vport;
  296. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  297. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  298. "0448 Adapter failed init, mbxCmd x%x "
  299. "READ_SPARM mbxStatus x%x\n",
  300. mb->mbxCommand, mb->mbxStatus);
  301. phba->link_state = LPFC_HBA_ERROR;
  302. mp = (struct lpfc_dmabuf *) pmb->context1;
  303. mempool_free( pmb, phba->mbox_mem_pool);
  304. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  305. kfree(mp);
  306. return -EIO;
  307. }
  308. mp = (struct lpfc_dmabuf *) pmb->context1;
  309. memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
  310. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  311. kfree(mp);
  312. pmb->context1 = NULL;
  313. if (phba->cfg_soft_wwnn)
  314. u64_to_wwn(phba->cfg_soft_wwnn,
  315. vport->fc_sparam.nodeName.u.wwn);
  316. if (phba->cfg_soft_wwpn)
  317. u64_to_wwn(phba->cfg_soft_wwpn,
  318. vport->fc_sparam.portName.u.wwn);
  319. memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
  320. sizeof (struct lpfc_name));
  321. memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
  322. sizeof (struct lpfc_name));
  323. /* If no serial number in VPD data, use low 6 bytes of WWNN */
  324. /* This should be consolidated into parse_vpd ? - mr */
  325. if (phba->SerialNumber[0] == 0) {
  326. uint8_t *outptr;
  327. outptr = &vport->fc_nodename.u.s.IEEE[0];
  328. for (i = 0; i < 12; i++) {
  329. status = *outptr++;
  330. j = ((status & 0xf0) >> 4);
  331. if (j <= 9)
  332. phba->SerialNumber[i] =
  333. (char)((uint8_t) 0x30 + (uint8_t) j);
  334. else
  335. phba->SerialNumber[i] =
  336. (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
  337. i++;
  338. j = (status & 0xf);
  339. if (j <= 9)
  340. phba->SerialNumber[i] =
  341. (char)((uint8_t) 0x30 + (uint8_t) j);
  342. else
  343. phba->SerialNumber[i] =
  344. (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
  345. }
  346. }
  347. lpfc_read_config(phba, pmb);
  348. pmb->vport = vport;
  349. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  350. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  351. "0453 Adapter failed to init, mbxCmd x%x "
  352. "READ_CONFIG, mbxStatus x%x\n",
  353. mb->mbxCommand, mb->mbxStatus);
  354. phba->link_state = LPFC_HBA_ERROR;
  355. mempool_free( pmb, phba->mbox_mem_pool);
  356. return -EIO;
  357. }
  358. /* Reset the DFT_HBA_Q_DEPTH to the max xri */
  359. if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
  360. phba->cfg_hba_queue_depth =
  361. mb->un.varRdConfig.max_xri + 1;
  362. phba->lmt = mb->un.varRdConfig.lmt;
  363. /* Get the default values for Model Name and Description */
  364. lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
  365. if ((phba->cfg_link_speed > LINK_SPEED_10G)
  366. || ((phba->cfg_link_speed == LINK_SPEED_1G)
  367. && !(phba->lmt & LMT_1Gb))
  368. || ((phba->cfg_link_speed == LINK_SPEED_2G)
  369. && !(phba->lmt & LMT_2Gb))
  370. || ((phba->cfg_link_speed == LINK_SPEED_4G)
  371. && !(phba->lmt & LMT_4Gb))
  372. || ((phba->cfg_link_speed == LINK_SPEED_8G)
  373. && !(phba->lmt & LMT_8Gb))
  374. || ((phba->cfg_link_speed == LINK_SPEED_10G)
  375. && !(phba->lmt & LMT_10Gb))) {
  376. /* Reset link speed to auto */
  377. lpfc_printf_log(phba, KERN_WARNING, LOG_LINK_EVENT,
  378. "1302 Invalid speed for this board: "
  379. "Reset link speed to auto: x%x\n",
  380. phba->cfg_link_speed);
  381. phba->cfg_link_speed = LINK_SPEED_AUTO;
  382. }
  383. phba->link_state = LPFC_LINK_DOWN;
  384. /* Only process IOCBs on ELS ring till hba_state is READY */
  385. if (psli->ring[psli->extra_ring].cmdringaddr)
  386. psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
  387. if (psli->ring[psli->fcp_ring].cmdringaddr)
  388. psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
  389. if (psli->ring[psli->next_ring].cmdringaddr)
  390. psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
  391. /* Post receive buffers for desired rings */
  392. if (phba->sli_rev != 3)
  393. lpfc_post_rcv_buf(phba);
  394. /*
  395. * Configure HBA MSI-X attention conditions to messages if MSI-X mode
  396. */
  397. if (phba->intr_type == MSIX) {
  398. rc = lpfc_config_msi(phba, pmb);
  399. if (rc) {
  400. mempool_free(pmb, phba->mbox_mem_pool);
  401. return -EIO;
  402. }
  403. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  404. if (rc != MBX_SUCCESS) {
  405. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  406. "0352 Config MSI mailbox command "
  407. "failed, mbxCmd x%x, mbxStatus x%x\n",
  408. pmb->mb.mbxCommand, pmb->mb.mbxStatus);
  409. mempool_free(pmb, phba->mbox_mem_pool);
  410. return -EIO;
  411. }
  412. }
  413. /* Initialize ERATT handling flag */
  414. phba->hba_flag &= ~HBA_ERATT_HANDLED;
  415. /* Enable appropriate host interrupts */
  416. spin_lock_irq(&phba->hbalock);
  417. status = readl(phba->HCregaddr);
  418. status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
  419. if (psli->num_rings > 0)
  420. status |= HC_R0INT_ENA;
  421. if (psli->num_rings > 1)
  422. status |= HC_R1INT_ENA;
  423. if (psli->num_rings > 2)
  424. status |= HC_R2INT_ENA;
  425. if (psli->num_rings > 3)
  426. status |= HC_R3INT_ENA;
  427. if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
  428. (phba->cfg_poll & DISABLE_FCP_RING_INT))
  429. status &= ~(HC_R0INT_ENA);
  430. writel(status, phba->HCregaddr);
  431. readl(phba->HCregaddr); /* flush */
  432. spin_unlock_irq(&phba->hbalock);
  433. /* Set up ring-0 (ELS) timer */
  434. timeout = phba->fc_ratov * 2;
  435. mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
  436. /* Set up heart beat (HB) timer */
  437. mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  438. phba->hb_outstanding = 0;
  439. phba->last_completion_time = jiffies;
  440. /* Set up error attention (ERATT) polling timer */
  441. mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
  442. lpfc_init_link(phba, pmb, phba->cfg_topology, phba->cfg_link_speed);
  443. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  444. lpfc_set_loopback_flag(phba);
  445. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  446. if (rc != MBX_SUCCESS) {
  447. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  448. "0454 Adapter failed to init, mbxCmd x%x "
  449. "INIT_LINK, mbxStatus x%x\n",
  450. mb->mbxCommand, mb->mbxStatus);
  451. /* Clear all interrupt enable conditions */
  452. writel(0, phba->HCregaddr);
  453. readl(phba->HCregaddr); /* flush */
  454. /* Clear all pending interrupts */
  455. writel(0xffffffff, phba->HAregaddr);
  456. readl(phba->HAregaddr); /* flush */
  457. phba->link_state = LPFC_HBA_ERROR;
  458. if (rc != MBX_BUSY)
  459. mempool_free(pmb, phba->mbox_mem_pool);
  460. return -EIO;
  461. }
  462. /* MBOX buffer will be freed in mbox compl */
  463. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  464. lpfc_config_async(phba, pmb, LPFC_ELS_RING);
  465. pmb->mbox_cmpl = lpfc_config_async_cmpl;
  466. pmb->vport = phba->pport;
  467. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  468. if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
  469. lpfc_printf_log(phba,
  470. KERN_ERR,
  471. LOG_INIT,
  472. "0456 Adapter failed to issue "
  473. "ASYNCEVT_ENABLE mbox status x%x \n.",
  474. rc);
  475. mempool_free(pmb, phba->mbox_mem_pool);
  476. }
  477. /* Get Option rom version */
  478. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  479. lpfc_dump_wakeup_param(phba, pmb);
  480. pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
  481. pmb->vport = phba->pport;
  482. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  483. if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
  484. lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
  485. "to get Option ROM version status x%x\n.", rc);
  486. mempool_free(pmb, phba->mbox_mem_pool);
  487. }
  488. return 0;
  489. }
  490. /**
  491. * lpfc_hba_down_prep: Perform lpfc uninitialization prior to HBA reset.
  492. * @phba: pointer to lpfc HBA data structure.
  493. *
  494. * This routine will do LPFC uninitialization before the HBA is reset when
  495. * bringing down the SLI Layer.
  496. *
  497. * Return codes
  498. * 0 - success.
  499. * Any other value - error.
  500. **/
  501. int
  502. lpfc_hba_down_prep(struct lpfc_hba *phba)
  503. {
  504. struct lpfc_vport **vports;
  505. int i;
  506. /* Disable interrupts */
  507. writel(0, phba->HCregaddr);
  508. readl(phba->HCregaddr); /* flush */
  509. if (phba->pport->load_flag & FC_UNLOADING)
  510. lpfc_cleanup_discovery_resources(phba->pport);
  511. else {
  512. vports = lpfc_create_vport_work_array(phba);
  513. if (vports != NULL)
  514. for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
  515. lpfc_cleanup_discovery_resources(vports[i]);
  516. lpfc_destroy_vport_work_array(phba, vports);
  517. }
  518. return 0;
  519. }
  520. /**
  521. * lpfc_hba_down_post: Perform lpfc uninitialization after HBA reset.
  522. * @phba: pointer to lpfc HBA data structure.
  523. *
  524. * This routine will do uninitialization after the HBA is reset when bring
  525. * down the SLI Layer.
  526. *
  527. * Return codes
  528. * 0 - sucess.
  529. * Any other value - error.
  530. **/
  531. int
  532. lpfc_hba_down_post(struct lpfc_hba *phba)
  533. {
  534. struct lpfc_sli *psli = &phba->sli;
  535. struct lpfc_sli_ring *pring;
  536. struct lpfc_dmabuf *mp, *next_mp;
  537. struct lpfc_iocbq *iocb;
  538. IOCB_t *cmd = NULL;
  539. LIST_HEAD(completions);
  540. int i;
  541. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
  542. lpfc_sli_hbqbuf_free_all(phba);
  543. else {
  544. /* Cleanup preposted buffers on the ELS ring */
  545. pring = &psli->ring[LPFC_ELS_RING];
  546. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  547. list_del(&mp->list);
  548. pring->postbufq_cnt--;
  549. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  550. kfree(mp);
  551. }
  552. }
  553. spin_lock_irq(&phba->hbalock);
  554. for (i = 0; i < psli->num_rings; i++) {
  555. pring = &psli->ring[i];
  556. /* At this point in time the HBA is either reset or DOA. Either
  557. * way, nothing should be on txcmplq as it will NEVER complete.
  558. */
  559. list_splice_init(&pring->txcmplq, &completions);
  560. pring->txcmplq_cnt = 0;
  561. spin_unlock_irq(&phba->hbalock);
  562. while (!list_empty(&completions)) {
  563. iocb = list_get_first(&completions, struct lpfc_iocbq,
  564. list);
  565. cmd = &iocb->iocb;
  566. list_del_init(&iocb->list);
  567. if (!iocb->iocb_cmpl)
  568. lpfc_sli_release_iocbq(phba, iocb);
  569. else {
  570. cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
  571. cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
  572. (iocb->iocb_cmpl) (phba, iocb, iocb);
  573. }
  574. }
  575. lpfc_sli_abort_iocb_ring(phba, pring);
  576. spin_lock_irq(&phba->hbalock);
  577. }
  578. spin_unlock_irq(&phba->hbalock);
  579. return 0;
  580. }
  581. /**
  582. * lpfc_hb_timeout: The HBA-timer timeout handler.
  583. * @ptr: unsigned long holds the pointer to lpfc hba data structure.
  584. *
  585. * This is the HBA-timer timeout handler registered to the lpfc driver. When
  586. * this timer fires, a HBA timeout event shall be posted to the lpfc driver
  587. * work-port-events bitmap and the worker thread is notified. This timeout
  588. * event will be used by the worker thread to invoke the actual timeout
  589. * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
  590. * be performed in the timeout handler and the HBA timeout event bit shall
  591. * be cleared by the worker thread after it has taken the event bitmap out.
  592. **/
  593. static void
  594. lpfc_hb_timeout(unsigned long ptr)
  595. {
  596. struct lpfc_hba *phba;
  597. uint32_t tmo_posted;
  598. unsigned long iflag;
  599. phba = (struct lpfc_hba *)ptr;
  600. /* Check for heart beat timeout conditions */
  601. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  602. tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
  603. if (!tmo_posted)
  604. phba->pport->work_port_events |= WORKER_HB_TMO;
  605. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  606. /* Tell the worker thread there is work to do */
  607. if (!tmo_posted)
  608. lpfc_worker_wake_up(phba);
  609. return;
  610. }
  611. /**
  612. * lpfc_hb_mbox_cmpl: The lpfc heart-beat mailbox command callback function.
  613. * @phba: pointer to lpfc hba data structure.
  614. * @pmboxq: pointer to the driver internal queue element for mailbox command.
  615. *
  616. * This is the callback function to the lpfc heart-beat mailbox command.
  617. * If configured, the lpfc driver issues the heart-beat mailbox command to
  618. * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
  619. * heart-beat mailbox command is issued, the driver shall set up heart-beat
  620. * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
  621. * heart-beat outstanding state. Once the mailbox command comes back and
  622. * no error conditions detected, the heart-beat mailbox command timer is
  623. * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
  624. * state is cleared for the next heart-beat. If the timer expired with the
  625. * heart-beat outstanding state set, the driver will put the HBA offline.
  626. **/
  627. static void
  628. lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
  629. {
  630. unsigned long drvr_flag;
  631. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  632. phba->hb_outstanding = 0;
  633. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  634. /* Check and reset heart-beat timer is necessary */
  635. mempool_free(pmboxq, phba->mbox_mem_pool);
  636. if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
  637. !(phba->link_state == LPFC_HBA_ERROR) &&
  638. !(phba->pport->load_flag & FC_UNLOADING))
  639. mod_timer(&phba->hb_tmofunc,
  640. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  641. return;
  642. }
  643. /**
  644. * lpfc_hb_timeout_handler: The HBA-timer timeout handler.
  645. * @phba: pointer to lpfc hba data structure.
  646. *
  647. * This is the actual HBA-timer timeout handler to be invoked by the worker
  648. * thread whenever the HBA timer fired and HBA-timeout event posted. This
  649. * handler performs any periodic operations needed for the device. If such
  650. * periodic event has already been attended to either in the interrupt handler
  651. * or by processing slow-ring or fast-ring events within the HBA-timer
  652. * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
  653. * the timer for the next timeout period. If lpfc heart-beat mailbox command
  654. * is configured and there is no heart-beat mailbox command outstanding, a
  655. * heart-beat mailbox is issued and timer set properly. Otherwise, if there
  656. * has been a heart-beat mailbox command outstanding, the HBA shall be put
  657. * to offline.
  658. **/
  659. void
  660. lpfc_hb_timeout_handler(struct lpfc_hba *phba)
  661. {
  662. LPFC_MBOXQ_t *pmboxq;
  663. struct lpfc_dmabuf *buf_ptr;
  664. int retval;
  665. struct lpfc_sli *psli = &phba->sli;
  666. LIST_HEAD(completions);
  667. if ((phba->link_state == LPFC_HBA_ERROR) ||
  668. (phba->pport->load_flag & FC_UNLOADING) ||
  669. (phba->pport->fc_flag & FC_OFFLINE_MODE))
  670. return;
  671. spin_lock_irq(&phba->pport->work_port_lock);
  672. if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
  673. jiffies)) {
  674. spin_unlock_irq(&phba->pport->work_port_lock);
  675. if (!phba->hb_outstanding)
  676. mod_timer(&phba->hb_tmofunc,
  677. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  678. else
  679. mod_timer(&phba->hb_tmofunc,
  680. jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
  681. return;
  682. }
  683. spin_unlock_irq(&phba->pport->work_port_lock);
  684. if (phba->elsbuf_cnt &&
  685. (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
  686. spin_lock_irq(&phba->hbalock);
  687. list_splice_init(&phba->elsbuf, &completions);
  688. phba->elsbuf_cnt = 0;
  689. phba->elsbuf_prev_cnt = 0;
  690. spin_unlock_irq(&phba->hbalock);
  691. while (!list_empty(&completions)) {
  692. list_remove_head(&completions, buf_ptr,
  693. struct lpfc_dmabuf, list);
  694. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  695. kfree(buf_ptr);
  696. }
  697. }
  698. phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
  699. /* If there is no heart beat outstanding, issue a heartbeat command */
  700. if (phba->cfg_enable_hba_heartbeat) {
  701. if (!phba->hb_outstanding) {
  702. pmboxq = mempool_alloc(phba->mbox_mem_pool,GFP_KERNEL);
  703. if (!pmboxq) {
  704. mod_timer(&phba->hb_tmofunc,
  705. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  706. return;
  707. }
  708. lpfc_heart_beat(phba, pmboxq);
  709. pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
  710. pmboxq->vport = phba->pport;
  711. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  712. if (retval != MBX_BUSY && retval != MBX_SUCCESS) {
  713. mempool_free(pmboxq, phba->mbox_mem_pool);
  714. mod_timer(&phba->hb_tmofunc,
  715. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  716. return;
  717. }
  718. mod_timer(&phba->hb_tmofunc,
  719. jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
  720. phba->hb_outstanding = 1;
  721. return;
  722. } else {
  723. /*
  724. * If heart beat timeout called with hb_outstanding set
  725. * we need to take the HBA offline.
  726. */
  727. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  728. "0459 Adapter heartbeat failure, "
  729. "taking this port offline.\n");
  730. spin_lock_irq(&phba->hbalock);
  731. psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
  732. spin_unlock_irq(&phba->hbalock);
  733. lpfc_offline_prep(phba);
  734. lpfc_offline(phba);
  735. lpfc_unblock_mgmt_io(phba);
  736. phba->link_state = LPFC_HBA_ERROR;
  737. lpfc_hba_down_post(phba);
  738. }
  739. }
  740. }
  741. /**
  742. * lpfc_offline_eratt: Bring lpfc offline on hardware error attention.
  743. * @phba: pointer to lpfc hba data structure.
  744. *
  745. * This routine is called to bring the HBA offline when HBA hardware error
  746. * other than Port Error 6 has been detected.
  747. **/
  748. static void
  749. lpfc_offline_eratt(struct lpfc_hba *phba)
  750. {
  751. struct lpfc_sli *psli = &phba->sli;
  752. spin_lock_irq(&phba->hbalock);
  753. psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
  754. spin_unlock_irq(&phba->hbalock);
  755. lpfc_offline_prep(phba);
  756. lpfc_offline(phba);
  757. lpfc_reset_barrier(phba);
  758. lpfc_sli_brdreset(phba);
  759. lpfc_hba_down_post(phba);
  760. lpfc_sli_brdready(phba, HS_MBRDY);
  761. lpfc_unblock_mgmt_io(phba);
  762. phba->link_state = LPFC_HBA_ERROR;
  763. return;
  764. }
  765. /**
  766. * lpfc_handle_eratt: The HBA hardware error handler.
  767. * @phba: pointer to lpfc hba data structure.
  768. *
  769. * This routine is invoked to handle the following HBA hardware error
  770. * conditions:
  771. * 1 - HBA error attention interrupt
  772. * 2 - DMA ring index out of range
  773. * 3 - Mailbox command came back as unknown
  774. **/
  775. void
  776. lpfc_handle_eratt(struct lpfc_hba *phba)
  777. {
  778. struct lpfc_vport *vport = phba->pport;
  779. struct lpfc_sli *psli = &phba->sli;
  780. struct lpfc_sli_ring *pring;
  781. uint32_t event_data;
  782. unsigned long temperature;
  783. struct temp_event temp_event_data;
  784. struct Scsi_Host *shost;
  785. struct lpfc_board_event_header board_event;
  786. /* If the pci channel is offline, ignore possible errors,
  787. * since we cannot communicate with the pci card anyway. */
  788. if (pci_channel_offline(phba->pcidev))
  789. return;
  790. /* If resets are disabled then leave the HBA alone and return */
  791. if (!phba->cfg_enable_hba_reset)
  792. return;
  793. /* Send an internal error event to mgmt application */
  794. board_event.event_type = FC_REG_BOARD_EVENT;
  795. board_event.subcategory = LPFC_EVENT_PORTINTERR;
  796. shost = lpfc_shost_from_vport(phba->pport);
  797. fc_host_post_vendor_event(shost, fc_get_event_number(),
  798. sizeof(board_event),
  799. (char *) &board_event,
  800. LPFC_NL_VENDOR_ID);
  801. if (phba->work_hs & HS_FFER6) {
  802. /* Re-establishing Link */
  803. lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
  804. "1301 Re-establishing Link "
  805. "Data: x%x x%x x%x\n",
  806. phba->work_hs,
  807. phba->work_status[0], phba->work_status[1]);
  808. spin_lock_irq(&phba->hbalock);
  809. psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
  810. spin_unlock_irq(&phba->hbalock);
  811. /*
  812. * Firmware stops when it triggled erratt with HS_FFER6.
  813. * That could cause the I/Os dropped by the firmware.
  814. * Error iocb (I/O) on txcmplq and let the SCSI layer
  815. * retry it after re-establishing link.
  816. */
  817. pring = &psli->ring[psli->fcp_ring];
  818. lpfc_sli_abort_iocb_ring(phba, pring);
  819. /*
  820. * There was a firmware error. Take the hba offline and then
  821. * attempt to restart it.
  822. */
  823. lpfc_offline_prep(phba);
  824. lpfc_offline(phba);
  825. lpfc_sli_brdrestart(phba);
  826. if (lpfc_online(phba) == 0) { /* Initialize the HBA */
  827. lpfc_unblock_mgmt_io(phba);
  828. return;
  829. }
  830. lpfc_unblock_mgmt_io(phba);
  831. } else if (phba->work_hs & HS_CRIT_TEMP) {
  832. temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
  833. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  834. temp_event_data.event_code = LPFC_CRIT_TEMP;
  835. temp_event_data.data = (uint32_t)temperature;
  836. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  837. "0406 Adapter maximum temperature exceeded "
  838. "(%ld), taking this port offline "
  839. "Data: x%x x%x x%x\n",
  840. temperature, phba->work_hs,
  841. phba->work_status[0], phba->work_status[1]);
  842. shost = lpfc_shost_from_vport(phba->pport);
  843. fc_host_post_vendor_event(shost, fc_get_event_number(),
  844. sizeof(temp_event_data),
  845. (char *) &temp_event_data,
  846. SCSI_NL_VID_TYPE_PCI
  847. | PCI_VENDOR_ID_EMULEX);
  848. spin_lock_irq(&phba->hbalock);
  849. phba->over_temp_state = HBA_OVER_TEMP;
  850. spin_unlock_irq(&phba->hbalock);
  851. lpfc_offline_eratt(phba);
  852. } else {
  853. /* The if clause above forces this code path when the status
  854. * failure is a value other than FFER6. Do not call the offline
  855. * twice. This is the adapter hardware error path.
  856. */
  857. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  858. "0457 Adapter Hardware Error "
  859. "Data: x%x x%x x%x\n",
  860. phba->work_hs,
  861. phba->work_status[0], phba->work_status[1]);
  862. event_data = FC_REG_DUMP_EVENT;
  863. shost = lpfc_shost_from_vport(vport);
  864. fc_host_post_vendor_event(shost, fc_get_event_number(),
  865. sizeof(event_data), (char *) &event_data,
  866. SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
  867. lpfc_offline_eratt(phba);
  868. }
  869. return;
  870. }
  871. /**
  872. * lpfc_handle_latt: The HBA link event handler.
  873. * @phba: pointer to lpfc hba data structure.
  874. *
  875. * This routine is invoked from the worker thread to handle a HBA host
  876. * attention link event.
  877. **/
  878. void
  879. lpfc_handle_latt(struct lpfc_hba *phba)
  880. {
  881. struct lpfc_vport *vport = phba->pport;
  882. struct lpfc_sli *psli = &phba->sli;
  883. LPFC_MBOXQ_t *pmb;
  884. volatile uint32_t control;
  885. struct lpfc_dmabuf *mp;
  886. int rc = 0;
  887. pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  888. if (!pmb) {
  889. rc = 1;
  890. goto lpfc_handle_latt_err_exit;
  891. }
  892. mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  893. if (!mp) {
  894. rc = 2;
  895. goto lpfc_handle_latt_free_pmb;
  896. }
  897. mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
  898. if (!mp->virt) {
  899. rc = 3;
  900. goto lpfc_handle_latt_free_mp;
  901. }
  902. /* Cleanup any outstanding ELS commands */
  903. lpfc_els_flush_all_cmd(phba);
  904. psli->slistat.link_event++;
  905. lpfc_read_la(phba, pmb, mp);
  906. pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
  907. pmb->vport = vport;
  908. /* Block ELS IOCBs until we have processed this mbox command */
  909. phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
  910. rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
  911. if (rc == MBX_NOT_FINISHED) {
  912. rc = 4;
  913. goto lpfc_handle_latt_free_mbuf;
  914. }
  915. /* Clear Link Attention in HA REG */
  916. spin_lock_irq(&phba->hbalock);
  917. writel(HA_LATT, phba->HAregaddr);
  918. readl(phba->HAregaddr); /* flush */
  919. spin_unlock_irq(&phba->hbalock);
  920. return;
  921. lpfc_handle_latt_free_mbuf:
  922. phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
  923. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  924. lpfc_handle_latt_free_mp:
  925. kfree(mp);
  926. lpfc_handle_latt_free_pmb:
  927. mempool_free(pmb, phba->mbox_mem_pool);
  928. lpfc_handle_latt_err_exit:
  929. /* Enable Link attention interrupts */
  930. spin_lock_irq(&phba->hbalock);
  931. psli->sli_flag |= LPFC_PROCESS_LA;
  932. control = readl(phba->HCregaddr);
  933. control |= HC_LAINT_ENA;
  934. writel(control, phba->HCregaddr);
  935. readl(phba->HCregaddr); /* flush */
  936. /* Clear Link Attention in HA REG */
  937. writel(HA_LATT, phba->HAregaddr);
  938. readl(phba->HAregaddr); /* flush */
  939. spin_unlock_irq(&phba->hbalock);
  940. lpfc_linkdown(phba);
  941. phba->link_state = LPFC_HBA_ERROR;
  942. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  943. "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
  944. return;
  945. }
  946. /**
  947. * lpfc_parse_vpd: Parse VPD (Vital Product Data).
  948. * @phba: pointer to lpfc hba data structure.
  949. * @vpd: pointer to the vital product data.
  950. * @len: length of the vital product data in bytes.
  951. *
  952. * This routine parses the Vital Product Data (VPD). The VPD is treated as
  953. * an array of characters. In this routine, the ModelName, ProgramType, and
  954. * ModelDesc, etc. fields of the phba data structure will be populated.
  955. *
  956. * Return codes
  957. * 0 - pointer to the VPD passed in is NULL
  958. * 1 - success
  959. **/
  960. static int
  961. lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
  962. {
  963. uint8_t lenlo, lenhi;
  964. int Length;
  965. int i, j;
  966. int finished = 0;
  967. int index = 0;
  968. if (!vpd)
  969. return 0;
  970. /* Vital Product */
  971. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  972. "0455 Vital Product Data: x%x x%x x%x x%x\n",
  973. (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
  974. (uint32_t) vpd[3]);
  975. while (!finished && (index < (len - 4))) {
  976. switch (vpd[index]) {
  977. case 0x82:
  978. case 0x91:
  979. index += 1;
  980. lenlo = vpd[index];
  981. index += 1;
  982. lenhi = vpd[index];
  983. index += 1;
  984. i = ((((unsigned short)lenhi) << 8) + lenlo);
  985. index += i;
  986. break;
  987. case 0x90:
  988. index += 1;
  989. lenlo = vpd[index];
  990. index += 1;
  991. lenhi = vpd[index];
  992. index += 1;
  993. Length = ((((unsigned short)lenhi) << 8) + lenlo);
  994. if (Length > len - index)
  995. Length = len - index;
  996. while (Length > 0) {
  997. /* Look for Serial Number */
  998. if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
  999. index += 2;
  1000. i = vpd[index];
  1001. index += 1;
  1002. j = 0;
  1003. Length -= (3+i);
  1004. while(i--) {
  1005. phba->SerialNumber[j++] = vpd[index++];
  1006. if (j == 31)
  1007. break;
  1008. }
  1009. phba->SerialNumber[j] = 0;
  1010. continue;
  1011. }
  1012. else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
  1013. phba->vpd_flag |= VPD_MODEL_DESC;
  1014. index += 2;
  1015. i = vpd[index];
  1016. index += 1;
  1017. j = 0;
  1018. Length -= (3+i);
  1019. while(i--) {
  1020. phba->ModelDesc[j++] = vpd[index++];
  1021. if (j == 255)
  1022. break;
  1023. }
  1024. phba->ModelDesc[j] = 0;
  1025. continue;
  1026. }
  1027. else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
  1028. phba->vpd_flag |= VPD_MODEL_NAME;
  1029. index += 2;
  1030. i = vpd[index];
  1031. index += 1;
  1032. j = 0;
  1033. Length -= (3+i);
  1034. while(i--) {
  1035. phba->ModelName[j++] = vpd[index++];
  1036. if (j == 79)
  1037. break;
  1038. }
  1039. phba->ModelName[j] = 0;
  1040. continue;
  1041. }
  1042. else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
  1043. phba->vpd_flag |= VPD_PROGRAM_TYPE;
  1044. index += 2;
  1045. i = vpd[index];
  1046. index += 1;
  1047. j = 0;
  1048. Length -= (3+i);
  1049. while(i--) {
  1050. phba->ProgramType[j++] = vpd[index++];
  1051. if (j == 255)
  1052. break;
  1053. }
  1054. phba->ProgramType[j] = 0;
  1055. continue;
  1056. }
  1057. else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
  1058. phba->vpd_flag |= VPD_PORT;
  1059. index += 2;
  1060. i = vpd[index];
  1061. index += 1;
  1062. j = 0;
  1063. Length -= (3+i);
  1064. while(i--) {
  1065. phba->Port[j++] = vpd[index++];
  1066. if (j == 19)
  1067. break;
  1068. }
  1069. phba->Port[j] = 0;
  1070. continue;
  1071. }
  1072. else {
  1073. index += 2;
  1074. i = vpd[index];
  1075. index += 1;
  1076. index += i;
  1077. Length -= (3 + i);
  1078. }
  1079. }
  1080. finished = 0;
  1081. break;
  1082. case 0x78:
  1083. finished = 1;
  1084. break;
  1085. default:
  1086. index ++;
  1087. break;
  1088. }
  1089. }
  1090. return(1);
  1091. }
  1092. /**
  1093. * lpfc_get_hba_model_desc: Retrieve HBA device model name and description.
  1094. * @phba: pointer to lpfc hba data structure.
  1095. * @mdp: pointer to the data structure to hold the derived model name.
  1096. * @descp: pointer to the data structure to hold the derived description.
  1097. *
  1098. * This routine retrieves HBA's description based on its registered PCI device
  1099. * ID. The @descp passed into this function points to an array of 256 chars. It
  1100. * shall be returned with the model name, maximum speed, and the host bus type.
  1101. * The @mdp passed into this function points to an array of 80 chars. When the
  1102. * function returns, the @mdp will be filled with the model name.
  1103. **/
  1104. static void
  1105. lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
  1106. {
  1107. lpfc_vpd_t *vp;
  1108. uint16_t dev_id = phba->pcidev->device;
  1109. int max_speed;
  1110. int GE = 0;
  1111. struct {
  1112. char * name;
  1113. int max_speed;
  1114. char * bus;
  1115. } m = {"<Unknown>", 0, ""};
  1116. if (mdp && mdp[0] != '\0'
  1117. && descp && descp[0] != '\0')
  1118. return;
  1119. if (phba->lmt & LMT_10Gb)
  1120. max_speed = 10;
  1121. else if (phba->lmt & LMT_8Gb)
  1122. max_speed = 8;
  1123. else if (phba->lmt & LMT_4Gb)
  1124. max_speed = 4;
  1125. else if (phba->lmt & LMT_2Gb)
  1126. max_speed = 2;
  1127. else
  1128. max_speed = 1;
  1129. vp = &phba->vpd;
  1130. switch (dev_id) {
  1131. case PCI_DEVICE_ID_FIREFLY:
  1132. m = (typeof(m)){"LP6000", max_speed, "PCI"};
  1133. break;
  1134. case PCI_DEVICE_ID_SUPERFLY:
  1135. if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
  1136. m = (typeof(m)){"LP7000", max_speed, "PCI"};
  1137. else
  1138. m = (typeof(m)){"LP7000E", max_speed, "PCI"};
  1139. break;
  1140. case PCI_DEVICE_ID_DRAGONFLY:
  1141. m = (typeof(m)){"LP8000", max_speed, "PCI"};
  1142. break;
  1143. case PCI_DEVICE_ID_CENTAUR:
  1144. if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
  1145. m = (typeof(m)){"LP9002", max_speed, "PCI"};
  1146. else
  1147. m = (typeof(m)){"LP9000", max_speed, "PCI"};
  1148. break;
  1149. case PCI_DEVICE_ID_RFLY:
  1150. m = (typeof(m)){"LP952", max_speed, "PCI"};
  1151. break;
  1152. case PCI_DEVICE_ID_PEGASUS:
  1153. m = (typeof(m)){"LP9802", max_speed, "PCI-X"};
  1154. break;
  1155. case PCI_DEVICE_ID_THOR:
  1156. m = (typeof(m)){"LP10000", max_speed, "PCI-X"};
  1157. break;
  1158. case PCI_DEVICE_ID_VIPER:
  1159. m = (typeof(m)){"LPX1000", max_speed, "PCI-X"};
  1160. break;
  1161. case PCI_DEVICE_ID_PFLY:
  1162. m = (typeof(m)){"LP982", max_speed, "PCI-X"};
  1163. break;
  1164. case PCI_DEVICE_ID_TFLY:
  1165. m = (typeof(m)){"LP1050", max_speed, "PCI-X"};
  1166. break;
  1167. case PCI_DEVICE_ID_HELIOS:
  1168. m = (typeof(m)){"LP11000", max_speed, "PCI-X2"};
  1169. break;
  1170. case PCI_DEVICE_ID_HELIOS_SCSP:
  1171. m = (typeof(m)){"LP11000-SP", max_speed, "PCI-X2"};
  1172. break;
  1173. case PCI_DEVICE_ID_HELIOS_DCSP:
  1174. m = (typeof(m)){"LP11002-SP", max_speed, "PCI-X2"};
  1175. break;
  1176. case PCI_DEVICE_ID_NEPTUNE:
  1177. m = (typeof(m)){"LPe1000", max_speed, "PCIe"};
  1178. break;
  1179. case PCI_DEVICE_ID_NEPTUNE_SCSP:
  1180. m = (typeof(m)){"LPe1000-SP", max_speed, "PCIe"};
  1181. break;
  1182. case PCI_DEVICE_ID_NEPTUNE_DCSP:
  1183. m = (typeof(m)){"LPe1002-SP", max_speed, "PCIe"};
  1184. break;
  1185. case PCI_DEVICE_ID_BMID:
  1186. m = (typeof(m)){"LP1150", max_speed, "PCI-X2"};
  1187. break;
  1188. case PCI_DEVICE_ID_BSMB:
  1189. m = (typeof(m)){"LP111", max_speed, "PCI-X2"};
  1190. break;
  1191. case PCI_DEVICE_ID_ZEPHYR:
  1192. m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
  1193. break;
  1194. case PCI_DEVICE_ID_ZEPHYR_SCSP:
  1195. m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
  1196. break;
  1197. case PCI_DEVICE_ID_ZEPHYR_DCSP:
  1198. m = (typeof(m)){"LPe11002-SP", max_speed, "PCIe"};
  1199. break;
  1200. case PCI_DEVICE_ID_ZMID:
  1201. m = (typeof(m)){"LPe1150", max_speed, "PCIe"};
  1202. break;
  1203. case PCI_DEVICE_ID_ZSMB:
  1204. m = (typeof(m)){"LPe111", max_speed, "PCIe"};
  1205. break;
  1206. case PCI_DEVICE_ID_LP101:
  1207. m = (typeof(m)){"LP101", max_speed, "PCI-X"};
  1208. break;
  1209. case PCI_DEVICE_ID_LP10000S:
  1210. m = (typeof(m)){"LP10000-S", max_speed, "PCI"};
  1211. break;
  1212. case PCI_DEVICE_ID_LP11000S:
  1213. m = (typeof(m)){"LP11000-S", max_speed,
  1214. "PCI-X2"};
  1215. break;
  1216. case PCI_DEVICE_ID_LPE11000S:
  1217. m = (typeof(m)){"LPe11000-S", max_speed,
  1218. "PCIe"};
  1219. break;
  1220. case PCI_DEVICE_ID_SAT:
  1221. m = (typeof(m)){"LPe12000", max_speed, "PCIe"};
  1222. break;
  1223. case PCI_DEVICE_ID_SAT_MID:
  1224. m = (typeof(m)){"LPe1250", max_speed, "PCIe"};
  1225. break;
  1226. case PCI_DEVICE_ID_SAT_SMB:
  1227. m = (typeof(m)){"LPe121", max_speed, "PCIe"};
  1228. break;
  1229. case PCI_DEVICE_ID_SAT_DCSP:
  1230. m = (typeof(m)){"LPe12002-SP", max_speed, "PCIe"};
  1231. break;
  1232. case PCI_DEVICE_ID_SAT_SCSP:
  1233. m = (typeof(m)){"LPe12000-SP", max_speed, "PCIe"};
  1234. break;
  1235. case PCI_DEVICE_ID_SAT_S:
  1236. m = (typeof(m)){"LPe12000-S", max_speed, "PCIe"};
  1237. break;
  1238. case PCI_DEVICE_ID_HORNET:
  1239. m = (typeof(m)){"LP21000", max_speed, "PCIe"};
  1240. GE = 1;
  1241. break;
  1242. case PCI_DEVICE_ID_PROTEUS_VF:
  1243. m = (typeof(m)) {"LPev12000", max_speed, "PCIe IOV"};
  1244. break;
  1245. case PCI_DEVICE_ID_PROTEUS_PF:
  1246. m = (typeof(m)) {"LPev12000", max_speed, "PCIe IOV"};
  1247. break;
  1248. case PCI_DEVICE_ID_PROTEUS_S:
  1249. m = (typeof(m)) {"LPemv12002-S", max_speed, "PCIe IOV"};
  1250. break;
  1251. default:
  1252. m = (typeof(m)){ NULL };
  1253. break;
  1254. }
  1255. if (mdp && mdp[0] == '\0')
  1256. snprintf(mdp, 79,"%s", m.name);
  1257. if (descp && descp[0] == '\0')
  1258. snprintf(descp, 255,
  1259. "Emulex %s %d%s %s %s",
  1260. m.name, m.max_speed,
  1261. (GE) ? "GE" : "Gb",
  1262. m.bus,
  1263. (GE) ? "FCoE Adapter" : "Fibre Channel Adapter");
  1264. }
  1265. /**
  1266. * lpfc_post_buffer: Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring.
  1267. * @phba: pointer to lpfc hba data structure.
  1268. * @pring: pointer to a IOCB ring.
  1269. * @cnt: the number of IOCBs to be posted to the IOCB ring.
  1270. *
  1271. * This routine posts a given number of IOCBs with the associated DMA buffer
  1272. * descriptors specified by the cnt argument to the given IOCB ring.
  1273. *
  1274. * Return codes
  1275. * The number of IOCBs NOT able to be posted to the IOCB ring.
  1276. **/
  1277. int
  1278. lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
  1279. {
  1280. IOCB_t *icmd;
  1281. struct lpfc_iocbq *iocb;
  1282. struct lpfc_dmabuf *mp1, *mp2;
  1283. cnt += pring->missbufcnt;
  1284. /* While there are buffers to post */
  1285. while (cnt > 0) {
  1286. /* Allocate buffer for command iocb */
  1287. iocb = lpfc_sli_get_iocbq(phba);
  1288. if (iocb == NULL) {
  1289. pring->missbufcnt = cnt;
  1290. return cnt;
  1291. }
  1292. icmd = &iocb->iocb;
  1293. /* 2 buffers can be posted per command */
  1294. /* Allocate buffer to post */
  1295. mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
  1296. if (mp1)
  1297. mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
  1298. if (!mp1 || !mp1->virt) {
  1299. kfree(mp1);
  1300. lpfc_sli_release_iocbq(phba, iocb);
  1301. pring->missbufcnt = cnt;
  1302. return cnt;
  1303. }
  1304. INIT_LIST_HEAD(&mp1->list);
  1305. /* Allocate buffer to post */
  1306. if (cnt > 1) {
  1307. mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
  1308. if (mp2)
  1309. mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
  1310. &mp2->phys);
  1311. if (!mp2 || !mp2->virt) {
  1312. kfree(mp2);
  1313. lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
  1314. kfree(mp1);
  1315. lpfc_sli_release_iocbq(phba, iocb);
  1316. pring->missbufcnt = cnt;
  1317. return cnt;
  1318. }
  1319. INIT_LIST_HEAD(&mp2->list);
  1320. } else {
  1321. mp2 = NULL;
  1322. }
  1323. icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
  1324. icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
  1325. icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
  1326. icmd->ulpBdeCount = 1;
  1327. cnt--;
  1328. if (mp2) {
  1329. icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
  1330. icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
  1331. icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
  1332. cnt--;
  1333. icmd->ulpBdeCount = 2;
  1334. }
  1335. icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
  1336. icmd->ulpLe = 1;
  1337. if (lpfc_sli_issue_iocb(phba, pring, iocb, 0) == IOCB_ERROR) {
  1338. lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
  1339. kfree(mp1);
  1340. cnt++;
  1341. if (mp2) {
  1342. lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
  1343. kfree(mp2);
  1344. cnt++;
  1345. }
  1346. lpfc_sli_release_iocbq(phba, iocb);
  1347. pring->missbufcnt = cnt;
  1348. return cnt;
  1349. }
  1350. lpfc_sli_ringpostbuf_put(phba, pring, mp1);
  1351. if (mp2)
  1352. lpfc_sli_ringpostbuf_put(phba, pring, mp2);
  1353. }
  1354. pring->missbufcnt = 0;
  1355. return 0;
  1356. }
  1357. /**
  1358. * lpfc_post_rcv_buf: Post the initial receive IOCB buffers to ELS ring.
  1359. * @phba: pointer to lpfc hba data structure.
  1360. *
  1361. * This routine posts initial receive IOCB buffers to the ELS ring. The
  1362. * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
  1363. * set to 64 IOCBs.
  1364. *
  1365. * Return codes
  1366. * 0 - success (currently always success)
  1367. **/
  1368. static int
  1369. lpfc_post_rcv_buf(struct lpfc_hba *phba)
  1370. {
  1371. struct lpfc_sli *psli = &phba->sli;
  1372. /* Ring 0, ELS / CT buffers */
  1373. lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
  1374. /* Ring 2 - FCP no buffers needed */
  1375. return 0;
  1376. }
  1377. #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
  1378. /**
  1379. * lpfc_sha_init: Set up initial array of hash table entries.
  1380. * @HashResultPointer: pointer to an array as hash table.
  1381. *
  1382. * This routine sets up the initial values to the array of hash table entries
  1383. * for the LC HBAs.
  1384. **/
  1385. static void
  1386. lpfc_sha_init(uint32_t * HashResultPointer)
  1387. {
  1388. HashResultPointer[0] = 0x67452301;
  1389. HashResultPointer[1] = 0xEFCDAB89;
  1390. HashResultPointer[2] = 0x98BADCFE;
  1391. HashResultPointer[3] = 0x10325476;
  1392. HashResultPointer[4] = 0xC3D2E1F0;
  1393. }
  1394. /**
  1395. * lpfc_sha_iterate: Iterate initial hash table with the working hash table.
  1396. * @HashResultPointer: pointer to an initial/result hash table.
  1397. * @HashWorkingPointer: pointer to an working hash table.
  1398. *
  1399. * This routine iterates an initial hash table pointed by @HashResultPointer
  1400. * with the values from the working hash table pointeed by @HashWorkingPointer.
  1401. * The results are putting back to the initial hash table, returned through
  1402. * the @HashResultPointer as the result hash table.
  1403. **/
  1404. static void
  1405. lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
  1406. {
  1407. int t;
  1408. uint32_t TEMP;
  1409. uint32_t A, B, C, D, E;
  1410. t = 16;
  1411. do {
  1412. HashWorkingPointer[t] =
  1413. S(1,
  1414. HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
  1415. 8] ^
  1416. HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
  1417. } while (++t <= 79);
  1418. t = 0;
  1419. A = HashResultPointer[0];
  1420. B = HashResultPointer[1];
  1421. C = HashResultPointer[2];
  1422. D = HashResultPointer[3];
  1423. E = HashResultPointer[4];
  1424. do {
  1425. if (t < 20) {
  1426. TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
  1427. } else if (t < 40) {
  1428. TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
  1429. } else if (t < 60) {
  1430. TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
  1431. } else {
  1432. TEMP = (B ^ C ^ D) + 0xCA62C1D6;
  1433. }
  1434. TEMP += S(5, A) + E + HashWorkingPointer[t];
  1435. E = D;
  1436. D = C;
  1437. C = S(30, B);
  1438. B = A;
  1439. A = TEMP;
  1440. } while (++t <= 79);
  1441. HashResultPointer[0] += A;
  1442. HashResultPointer[1] += B;
  1443. HashResultPointer[2] += C;
  1444. HashResultPointer[3] += D;
  1445. HashResultPointer[4] += E;
  1446. }
  1447. /**
  1448. * lpfc_challenge_key: Create challenge key based on WWPN of the HBA.
  1449. * @RandomChallenge: pointer to the entry of host challenge random number array.
  1450. * @HashWorking: pointer to the entry of the working hash array.
  1451. *
  1452. * This routine calculates the working hash array referred by @HashWorking
  1453. * from the challenge random numbers associated with the host, referred by
  1454. * @RandomChallenge. The result is put into the entry of the working hash
  1455. * array and returned by reference through @HashWorking.
  1456. **/
  1457. static void
  1458. lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
  1459. {
  1460. *HashWorking = (*RandomChallenge ^ *HashWorking);
  1461. }
  1462. /**
  1463. * lpfc_hba_init: Perform special handling for LC HBA initialization.
  1464. * @phba: pointer to lpfc hba data structure.
  1465. * @hbainit: pointer to an array of unsigned 32-bit integers.
  1466. *
  1467. * This routine performs the special handling for LC HBA initialization.
  1468. **/
  1469. void
  1470. lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
  1471. {
  1472. int t;
  1473. uint32_t *HashWorking;
  1474. uint32_t *pwwnn = (uint32_t *) phba->wwnn;
  1475. HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
  1476. if (!HashWorking)
  1477. return;
  1478. HashWorking[0] = HashWorking[78] = *pwwnn++;
  1479. HashWorking[1] = HashWorking[79] = *pwwnn;
  1480. for (t = 0; t < 7; t++)
  1481. lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
  1482. lpfc_sha_init(hbainit);
  1483. lpfc_sha_iterate(hbainit, HashWorking);
  1484. kfree(HashWorking);
  1485. }
  1486. /**
  1487. * lpfc_cleanup: Performs vport cleanups before deleting a vport.
  1488. * @vport: pointer to a virtual N_Port data structure.
  1489. *
  1490. * This routine performs the necessary cleanups before deleting the @vport.
  1491. * It invokes the discovery state machine to perform necessary state
  1492. * transitions and to release the ndlps associated with the @vport. Note,
  1493. * the physical port is treated as @vport 0.
  1494. **/
  1495. void
  1496. lpfc_cleanup(struct lpfc_vport *vport)
  1497. {
  1498. struct lpfc_hba *phba = vport->phba;
  1499. struct lpfc_nodelist *ndlp, *next_ndlp;
  1500. int i = 0;
  1501. if (phba->link_state > LPFC_LINK_DOWN)
  1502. lpfc_port_link_failure(vport);
  1503. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  1504. if (!NLP_CHK_NODE_ACT(ndlp)) {
  1505. ndlp = lpfc_enable_node(vport, ndlp,
  1506. NLP_STE_UNUSED_NODE);
  1507. if (!ndlp)
  1508. continue;
  1509. spin_lock_irq(&phba->ndlp_lock);
  1510. NLP_SET_FREE_REQ(ndlp);
  1511. spin_unlock_irq(&phba->ndlp_lock);
  1512. /* Trigger the release of the ndlp memory */
  1513. lpfc_nlp_put(ndlp);
  1514. continue;
  1515. }
  1516. spin_lock_irq(&phba->ndlp_lock);
  1517. if (NLP_CHK_FREE_REQ(ndlp)) {
  1518. /* The ndlp should not be in memory free mode already */
  1519. spin_unlock_irq(&phba->ndlp_lock);
  1520. continue;
  1521. } else
  1522. /* Indicate request for freeing ndlp memory */
  1523. NLP_SET_FREE_REQ(ndlp);
  1524. spin_unlock_irq(&phba->ndlp_lock);
  1525. if (vport->port_type != LPFC_PHYSICAL_PORT &&
  1526. ndlp->nlp_DID == Fabric_DID) {
  1527. /* Just free up ndlp with Fabric_DID for vports */
  1528. lpfc_nlp_put(ndlp);
  1529. continue;
  1530. }
  1531. if (ndlp->nlp_type & NLP_FABRIC)
  1532. lpfc_disc_state_machine(vport, ndlp, NULL,
  1533. NLP_EVT_DEVICE_RECOVERY);
  1534. lpfc_disc_state_machine(vport, ndlp, NULL,
  1535. NLP_EVT_DEVICE_RM);
  1536. }
  1537. /* At this point, ALL ndlp's should be gone
  1538. * because of the previous NLP_EVT_DEVICE_RM.
  1539. * Lets wait for this to happen, if needed.
  1540. */
  1541. while (!list_empty(&vport->fc_nodes)) {
  1542. if (i++ > 3000) {
  1543. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  1544. "0233 Nodelist not empty\n");
  1545. list_for_each_entry_safe(ndlp, next_ndlp,
  1546. &vport->fc_nodes, nlp_listp) {
  1547. lpfc_printf_vlog(ndlp->vport, KERN_ERR,
  1548. LOG_NODE,
  1549. "0282 did:x%x ndlp:x%p "
  1550. "usgmap:x%x refcnt:%d\n",
  1551. ndlp->nlp_DID, (void *)ndlp,
  1552. ndlp->nlp_usg_map,
  1553. atomic_read(
  1554. &ndlp->kref.refcount));
  1555. }
  1556. break;
  1557. }
  1558. /* Wait for any activity on ndlps to settle */
  1559. msleep(10);
  1560. }
  1561. return;
  1562. }
  1563. /**
  1564. * lpfc_stop_vport_timers: Stop all the timers associated with a vport.
  1565. * @vport: pointer to a virtual N_Port data structure.
  1566. *
  1567. * This routine stops all the timers associated with a @vport. This function
  1568. * is invoked before disabling or deleting a @vport. Note that the physical
  1569. * port is treated as @vport 0.
  1570. **/
  1571. void
  1572. lpfc_stop_vport_timers(struct lpfc_vport *vport)
  1573. {
  1574. del_timer_sync(&vport->els_tmofunc);
  1575. del_timer_sync(&vport->fc_fdmitmo);
  1576. lpfc_can_disctmo(vport);
  1577. return;
  1578. }
  1579. /**
  1580. * lpfc_stop_phba_timers: Stop all the timers associated with an HBA.
  1581. * @phba: pointer to lpfc hba data structure.
  1582. *
  1583. * This routine stops all the timers associated with a HBA. This function is
  1584. * invoked before either putting a HBA offline or unloading the driver.
  1585. **/
  1586. static void
  1587. lpfc_stop_phba_timers(struct lpfc_hba *phba)
  1588. {
  1589. del_timer_sync(&phba->fcp_poll_timer);
  1590. lpfc_stop_vport_timers(phba->pport);
  1591. del_timer_sync(&phba->sli.mbox_tmo);
  1592. del_timer_sync(&phba->fabric_block_timer);
  1593. phba->hb_outstanding = 0;
  1594. del_timer_sync(&phba->hb_tmofunc);
  1595. del_timer_sync(&phba->eratt_poll);
  1596. return;
  1597. }
  1598. /**
  1599. * lpfc_block_mgmt_io: Mark a HBA's management interface as blocked.
  1600. * @phba: pointer to lpfc hba data structure.
  1601. *
  1602. * This routine marks a HBA's management interface as blocked. Once the HBA's
  1603. * management interface is marked as blocked, all the user space access to
  1604. * the HBA, whether they are from sysfs interface or libdfc interface will
  1605. * all be blocked. The HBA is set to block the management interface when the
  1606. * driver prepares the HBA interface for online or offline.
  1607. **/
  1608. static void
  1609. lpfc_block_mgmt_io(struct lpfc_hba * phba)
  1610. {
  1611. unsigned long iflag;
  1612. spin_lock_irqsave(&phba->hbalock, iflag);
  1613. phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
  1614. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1615. }
  1616. /**
  1617. * lpfc_online: Initialize and bring a HBA online.
  1618. * @phba: pointer to lpfc hba data structure.
  1619. *
  1620. * This routine initializes the HBA and brings a HBA online. During this
  1621. * process, the management interface is blocked to prevent user space access
  1622. * to the HBA interfering with the driver initialization.
  1623. *
  1624. * Return codes
  1625. * 0 - successful
  1626. * 1 - failed
  1627. **/
  1628. int
  1629. lpfc_online(struct lpfc_hba *phba)
  1630. {
  1631. struct lpfc_vport *vport = phba->pport;
  1632. struct lpfc_vport **vports;
  1633. int i;
  1634. if (!phba)
  1635. return 0;
  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_64BIT_MASK) != 0)
  2396. if (pci_set_dma_mask(phba->pcidev, DMA_32BIT_MASK) != 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);