lpfc_init.c 85 KB

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