lpfc_init.c 99 KB

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