lpfc_init.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915
  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. struct {
  1025. char * name;
  1026. int max_speed;
  1027. char * bus;
  1028. } m = {"<Unknown>", 0, ""};
  1029. if (mdp && mdp[0] != '\0'
  1030. && descp && descp[0] != '\0')
  1031. return;
  1032. if (phba->lmt & LMT_10Gb)
  1033. max_speed = 10;
  1034. else if (phba->lmt & LMT_8Gb)
  1035. max_speed = 8;
  1036. else if (phba->lmt & LMT_4Gb)
  1037. max_speed = 4;
  1038. else if (phba->lmt & LMT_2Gb)
  1039. max_speed = 2;
  1040. else
  1041. max_speed = 1;
  1042. vp = &phba->vpd;
  1043. switch (dev_id) {
  1044. case PCI_DEVICE_ID_FIREFLY:
  1045. m = (typeof(m)){"LP6000", max_speed, "PCI"};
  1046. break;
  1047. case PCI_DEVICE_ID_SUPERFLY:
  1048. if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
  1049. m = (typeof(m)){"LP7000", max_speed, "PCI"};
  1050. else
  1051. m = (typeof(m)){"LP7000E", max_speed, "PCI"};
  1052. break;
  1053. case PCI_DEVICE_ID_DRAGONFLY:
  1054. m = (typeof(m)){"LP8000", max_speed, "PCI"};
  1055. break;
  1056. case PCI_DEVICE_ID_CENTAUR:
  1057. if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
  1058. m = (typeof(m)){"LP9002", max_speed, "PCI"};
  1059. else
  1060. m = (typeof(m)){"LP9000", max_speed, "PCI"};
  1061. break;
  1062. case PCI_DEVICE_ID_RFLY:
  1063. m = (typeof(m)){"LP952", max_speed, "PCI"};
  1064. break;
  1065. case PCI_DEVICE_ID_PEGASUS:
  1066. m = (typeof(m)){"LP9802", max_speed, "PCI-X"};
  1067. break;
  1068. case PCI_DEVICE_ID_THOR:
  1069. m = (typeof(m)){"LP10000", max_speed, "PCI-X"};
  1070. break;
  1071. case PCI_DEVICE_ID_VIPER:
  1072. m = (typeof(m)){"LPX1000", max_speed, "PCI-X"};
  1073. break;
  1074. case PCI_DEVICE_ID_PFLY:
  1075. m = (typeof(m)){"LP982", max_speed, "PCI-X"};
  1076. break;
  1077. case PCI_DEVICE_ID_TFLY:
  1078. m = (typeof(m)){"LP1050", max_speed, "PCI-X"};
  1079. break;
  1080. case PCI_DEVICE_ID_HELIOS:
  1081. m = (typeof(m)){"LP11000", max_speed, "PCI-X2"};
  1082. break;
  1083. case PCI_DEVICE_ID_HELIOS_SCSP:
  1084. m = (typeof(m)){"LP11000-SP", max_speed, "PCI-X2"};
  1085. break;
  1086. case PCI_DEVICE_ID_HELIOS_DCSP:
  1087. m = (typeof(m)){"LP11002-SP", max_speed, "PCI-X2"};
  1088. break;
  1089. case PCI_DEVICE_ID_NEPTUNE:
  1090. m = (typeof(m)){"LPe1000", max_speed, "PCIe"};
  1091. break;
  1092. case PCI_DEVICE_ID_NEPTUNE_SCSP:
  1093. m = (typeof(m)){"LPe1000-SP", max_speed, "PCIe"};
  1094. break;
  1095. case PCI_DEVICE_ID_NEPTUNE_DCSP:
  1096. m = (typeof(m)){"LPe1002-SP", max_speed, "PCIe"};
  1097. break;
  1098. case PCI_DEVICE_ID_BMID:
  1099. m = (typeof(m)){"LP1150", max_speed, "PCI-X2"};
  1100. break;
  1101. case PCI_DEVICE_ID_BSMB:
  1102. m = (typeof(m)){"LP111", max_speed, "PCI-X2"};
  1103. break;
  1104. case PCI_DEVICE_ID_ZEPHYR:
  1105. m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
  1106. break;
  1107. case PCI_DEVICE_ID_ZEPHYR_SCSP:
  1108. m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
  1109. break;
  1110. case PCI_DEVICE_ID_ZEPHYR_DCSP:
  1111. m = (typeof(m)){"LPe11002-SP", max_speed, "PCIe"};
  1112. break;
  1113. case PCI_DEVICE_ID_ZMID:
  1114. m = (typeof(m)){"LPe1150", max_speed, "PCIe"};
  1115. break;
  1116. case PCI_DEVICE_ID_ZSMB:
  1117. m = (typeof(m)){"LPe111", max_speed, "PCIe"};
  1118. break;
  1119. case PCI_DEVICE_ID_LP101:
  1120. m = (typeof(m)){"LP101", max_speed, "PCI-X"};
  1121. break;
  1122. case PCI_DEVICE_ID_LP10000S:
  1123. m = (typeof(m)){"LP10000-S", max_speed, "PCI"};
  1124. break;
  1125. case PCI_DEVICE_ID_LP11000S:
  1126. m = (typeof(m)){"LP11000-S", max_speed,
  1127. "PCI-X2"};
  1128. break;
  1129. case PCI_DEVICE_ID_LPE11000S:
  1130. m = (typeof(m)){"LPe11000-S", max_speed,
  1131. "PCIe"};
  1132. break;
  1133. case PCI_DEVICE_ID_SAT:
  1134. m = (typeof(m)){"LPe12000", max_speed, "PCIe"};
  1135. break;
  1136. case PCI_DEVICE_ID_SAT_MID:
  1137. m = (typeof(m)){"LPe1250", max_speed, "PCIe"};
  1138. break;
  1139. case PCI_DEVICE_ID_SAT_SMB:
  1140. m = (typeof(m)){"LPe121", max_speed, "PCIe"};
  1141. break;
  1142. case PCI_DEVICE_ID_SAT_DCSP:
  1143. m = (typeof(m)){"LPe12002-SP", max_speed, "PCIe"};
  1144. break;
  1145. case PCI_DEVICE_ID_SAT_SCSP:
  1146. m = (typeof(m)){"LPe12000-SP", max_speed, "PCIe"};
  1147. break;
  1148. case PCI_DEVICE_ID_SAT_S:
  1149. m = (typeof(m)){"LPe12000-S", max_speed, "PCIe"};
  1150. break;
  1151. default:
  1152. m = (typeof(m)){ NULL };
  1153. break;
  1154. }
  1155. if (mdp && mdp[0] == '\0')
  1156. snprintf(mdp, 79,"%s", m.name);
  1157. if (descp && descp[0] == '\0')
  1158. snprintf(descp, 255,
  1159. "Emulex %s %dGb %s Fibre Channel Adapter",
  1160. m.name, m.max_speed, m.bus);
  1161. }
  1162. /**
  1163. * lpfc_post_buffer: Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring.
  1164. * @phba: pointer to lpfc hba data structure.
  1165. * @pring: pointer to a IOCB ring.
  1166. * @cnt: the number of IOCBs to be posted to the IOCB ring.
  1167. *
  1168. * This routine posts a given number of IOCBs with the associated DMA buffer
  1169. * descriptors specified by the cnt argument to the given IOCB ring.
  1170. *
  1171. * Return codes
  1172. * The number of IOCBs NOT able to be posted to the IOCB ring.
  1173. **/
  1174. int
  1175. lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
  1176. {
  1177. IOCB_t *icmd;
  1178. struct lpfc_iocbq *iocb;
  1179. struct lpfc_dmabuf *mp1, *mp2;
  1180. cnt += pring->missbufcnt;
  1181. /* While there are buffers to post */
  1182. while (cnt > 0) {
  1183. /* Allocate buffer for command iocb */
  1184. iocb = lpfc_sli_get_iocbq(phba);
  1185. if (iocb == NULL) {
  1186. pring->missbufcnt = cnt;
  1187. return cnt;
  1188. }
  1189. icmd = &iocb->iocb;
  1190. /* 2 buffers can be posted per command */
  1191. /* Allocate buffer to post */
  1192. mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
  1193. if (mp1)
  1194. mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
  1195. if (!mp1 || !mp1->virt) {
  1196. kfree(mp1);
  1197. lpfc_sli_release_iocbq(phba, iocb);
  1198. pring->missbufcnt = cnt;
  1199. return cnt;
  1200. }
  1201. INIT_LIST_HEAD(&mp1->list);
  1202. /* Allocate buffer to post */
  1203. if (cnt > 1) {
  1204. mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
  1205. if (mp2)
  1206. mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
  1207. &mp2->phys);
  1208. if (!mp2 || !mp2->virt) {
  1209. kfree(mp2);
  1210. lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
  1211. kfree(mp1);
  1212. lpfc_sli_release_iocbq(phba, iocb);
  1213. pring->missbufcnt = cnt;
  1214. return cnt;
  1215. }
  1216. INIT_LIST_HEAD(&mp2->list);
  1217. } else {
  1218. mp2 = NULL;
  1219. }
  1220. icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
  1221. icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
  1222. icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
  1223. icmd->ulpBdeCount = 1;
  1224. cnt--;
  1225. if (mp2) {
  1226. icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
  1227. icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
  1228. icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
  1229. cnt--;
  1230. icmd->ulpBdeCount = 2;
  1231. }
  1232. icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
  1233. icmd->ulpLe = 1;
  1234. if (lpfc_sli_issue_iocb(phba, pring, iocb, 0) == IOCB_ERROR) {
  1235. lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
  1236. kfree(mp1);
  1237. cnt++;
  1238. if (mp2) {
  1239. lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
  1240. kfree(mp2);
  1241. cnt++;
  1242. }
  1243. lpfc_sli_release_iocbq(phba, iocb);
  1244. pring->missbufcnt = cnt;
  1245. return cnt;
  1246. }
  1247. lpfc_sli_ringpostbuf_put(phba, pring, mp1);
  1248. if (mp2)
  1249. lpfc_sli_ringpostbuf_put(phba, pring, mp2);
  1250. }
  1251. pring->missbufcnt = 0;
  1252. return 0;
  1253. }
  1254. /**
  1255. * lpfc_post_rcv_buf: Post the initial receive IOCB buffers to ELS ring.
  1256. * @phba: pointer to lpfc hba data structure.
  1257. *
  1258. * This routine posts initial receive IOCB buffers to the ELS ring. The
  1259. * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
  1260. * set to 64 IOCBs.
  1261. *
  1262. * Return codes
  1263. * 0 - success (currently always success)
  1264. **/
  1265. static int
  1266. lpfc_post_rcv_buf(struct lpfc_hba *phba)
  1267. {
  1268. struct lpfc_sli *psli = &phba->sli;
  1269. /* Ring 0, ELS / CT buffers */
  1270. lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
  1271. /* Ring 2 - FCP no buffers needed */
  1272. return 0;
  1273. }
  1274. #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
  1275. /**
  1276. * lpfc_sha_init: Set up initial array of hash table entries.
  1277. * @HashResultPointer: pointer to an array as hash table.
  1278. *
  1279. * This routine sets up the initial values to the array of hash table entries
  1280. * for the LC HBAs.
  1281. **/
  1282. static void
  1283. lpfc_sha_init(uint32_t * HashResultPointer)
  1284. {
  1285. HashResultPointer[0] = 0x67452301;
  1286. HashResultPointer[1] = 0xEFCDAB89;
  1287. HashResultPointer[2] = 0x98BADCFE;
  1288. HashResultPointer[3] = 0x10325476;
  1289. HashResultPointer[4] = 0xC3D2E1F0;
  1290. }
  1291. /**
  1292. * lpfc_sha_iterate: Iterate initial hash table with the working hash table.
  1293. * @HashResultPointer: pointer to an initial/result hash table.
  1294. * @HashWorkingPointer: pointer to an working hash table.
  1295. *
  1296. * This routine iterates an initial hash table pointed by @HashResultPointer
  1297. * with the values from the working hash table pointeed by @HashWorkingPointer.
  1298. * The results are putting back to the initial hash table, returned through
  1299. * the @HashResultPointer as the result hash table.
  1300. **/
  1301. static void
  1302. lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
  1303. {
  1304. int t;
  1305. uint32_t TEMP;
  1306. uint32_t A, B, C, D, E;
  1307. t = 16;
  1308. do {
  1309. HashWorkingPointer[t] =
  1310. S(1,
  1311. HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
  1312. 8] ^
  1313. HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
  1314. } while (++t <= 79);
  1315. t = 0;
  1316. A = HashResultPointer[0];
  1317. B = HashResultPointer[1];
  1318. C = HashResultPointer[2];
  1319. D = HashResultPointer[3];
  1320. E = HashResultPointer[4];
  1321. do {
  1322. if (t < 20) {
  1323. TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
  1324. } else if (t < 40) {
  1325. TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
  1326. } else if (t < 60) {
  1327. TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
  1328. } else {
  1329. TEMP = (B ^ C ^ D) + 0xCA62C1D6;
  1330. }
  1331. TEMP += S(5, A) + E + HashWorkingPointer[t];
  1332. E = D;
  1333. D = C;
  1334. C = S(30, B);
  1335. B = A;
  1336. A = TEMP;
  1337. } while (++t <= 79);
  1338. HashResultPointer[0] += A;
  1339. HashResultPointer[1] += B;
  1340. HashResultPointer[2] += C;
  1341. HashResultPointer[3] += D;
  1342. HashResultPointer[4] += E;
  1343. }
  1344. /**
  1345. * lpfc_challenge_key: Create challenge key based on WWPN of the HBA.
  1346. * @RandomChallenge: pointer to the entry of host challenge random number array.
  1347. * @HashWorking: pointer to the entry of the working hash array.
  1348. *
  1349. * This routine calculates the working hash array referred by @HashWorking
  1350. * from the challenge random numbers associated with the host, referred by
  1351. * @RandomChallenge. The result is put into the entry of the working hash
  1352. * array and returned by reference through @HashWorking.
  1353. **/
  1354. static void
  1355. lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
  1356. {
  1357. *HashWorking = (*RandomChallenge ^ *HashWorking);
  1358. }
  1359. /**
  1360. * lpfc_hba_init: Perform special handling for LC HBA initialization.
  1361. * @phba: pointer to lpfc hba data structure.
  1362. * @hbainit: pointer to an array of unsigned 32-bit integers.
  1363. *
  1364. * This routine performs the special handling for LC HBA initialization.
  1365. **/
  1366. void
  1367. lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
  1368. {
  1369. int t;
  1370. uint32_t *HashWorking;
  1371. uint32_t *pwwnn = (uint32_t *) phba->wwnn;
  1372. HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
  1373. if (!HashWorking)
  1374. return;
  1375. HashWorking[0] = HashWorking[78] = *pwwnn++;
  1376. HashWorking[1] = HashWorking[79] = *pwwnn;
  1377. for (t = 0; t < 7; t++)
  1378. lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
  1379. lpfc_sha_init(hbainit);
  1380. lpfc_sha_iterate(hbainit, HashWorking);
  1381. kfree(HashWorking);
  1382. }
  1383. /**
  1384. * lpfc_cleanup: Performs vport cleanups before deleting a vport.
  1385. * @vport: pointer to a virtual N_Port data structure.
  1386. *
  1387. * This routine performs the necessary cleanups before deleting the @vport.
  1388. * It invokes the discovery state machine to perform necessary state
  1389. * transitions and to release the ndlps associated with the @vport. Note,
  1390. * the physical port is treated as @vport 0.
  1391. **/
  1392. void
  1393. lpfc_cleanup(struct lpfc_vport *vport)
  1394. {
  1395. struct lpfc_hba *phba = vport->phba;
  1396. struct lpfc_nodelist *ndlp, *next_ndlp;
  1397. int i = 0;
  1398. if (phba->link_state > LPFC_LINK_DOWN)
  1399. lpfc_port_link_failure(vport);
  1400. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  1401. if (!NLP_CHK_NODE_ACT(ndlp)) {
  1402. ndlp = lpfc_enable_node(vport, ndlp,
  1403. NLP_STE_UNUSED_NODE);
  1404. if (!ndlp)
  1405. continue;
  1406. spin_lock_irq(&phba->ndlp_lock);
  1407. NLP_SET_FREE_REQ(ndlp);
  1408. spin_unlock_irq(&phba->ndlp_lock);
  1409. /* Trigger the release of the ndlp memory */
  1410. lpfc_nlp_put(ndlp);
  1411. continue;
  1412. }
  1413. spin_lock_irq(&phba->ndlp_lock);
  1414. if (NLP_CHK_FREE_REQ(ndlp)) {
  1415. /* The ndlp should not be in memory free mode already */
  1416. spin_unlock_irq(&phba->ndlp_lock);
  1417. continue;
  1418. } else
  1419. /* Indicate request for freeing ndlp memory */
  1420. NLP_SET_FREE_REQ(ndlp);
  1421. spin_unlock_irq(&phba->ndlp_lock);
  1422. if (vport->port_type != LPFC_PHYSICAL_PORT &&
  1423. ndlp->nlp_DID == Fabric_DID) {
  1424. /* Just free up ndlp with Fabric_DID for vports */
  1425. lpfc_nlp_put(ndlp);
  1426. continue;
  1427. }
  1428. if (ndlp->nlp_type & NLP_FABRIC)
  1429. lpfc_disc_state_machine(vport, ndlp, NULL,
  1430. NLP_EVT_DEVICE_RECOVERY);
  1431. lpfc_disc_state_machine(vport, ndlp, NULL,
  1432. NLP_EVT_DEVICE_RM);
  1433. }
  1434. /* At this point, ALL ndlp's should be gone
  1435. * because of the previous NLP_EVT_DEVICE_RM.
  1436. * Lets wait for this to happen, if needed.
  1437. */
  1438. while (!list_empty(&vport->fc_nodes)) {
  1439. if (i++ > 3000) {
  1440. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  1441. "0233 Nodelist not empty\n");
  1442. list_for_each_entry_safe(ndlp, next_ndlp,
  1443. &vport->fc_nodes, nlp_listp) {
  1444. lpfc_printf_vlog(ndlp->vport, KERN_ERR,
  1445. LOG_NODE,
  1446. "0282 did:x%x ndlp:x%p "
  1447. "usgmap:x%x refcnt:%d\n",
  1448. ndlp->nlp_DID, (void *)ndlp,
  1449. ndlp->nlp_usg_map,
  1450. atomic_read(
  1451. &ndlp->kref.refcount));
  1452. }
  1453. break;
  1454. }
  1455. /* Wait for any activity on ndlps to settle */
  1456. msleep(10);
  1457. }
  1458. return;
  1459. }
  1460. /**
  1461. * lpfc_stop_vport_timers: Stop all the timers associated with a vport.
  1462. * @vport: pointer to a virtual N_Port data structure.
  1463. *
  1464. * This routine stops all the timers associated with a @vport. This function
  1465. * is invoked before disabling or deleting a @vport. Note that the physical
  1466. * port is treated as @vport 0.
  1467. **/
  1468. void
  1469. lpfc_stop_vport_timers(struct lpfc_vport *vport)
  1470. {
  1471. del_timer_sync(&vport->els_tmofunc);
  1472. del_timer_sync(&vport->fc_fdmitmo);
  1473. lpfc_can_disctmo(vport);
  1474. return;
  1475. }
  1476. /**
  1477. * lpfc_stop_phba_timers: Stop all the timers associated with an HBA.
  1478. * @phba: pointer to lpfc hba data structure.
  1479. *
  1480. * This routine stops all the timers associated with a HBA. This function is
  1481. * invoked before either putting a HBA offline or unloading the driver.
  1482. **/
  1483. static void
  1484. lpfc_stop_phba_timers(struct lpfc_hba *phba)
  1485. {
  1486. del_timer_sync(&phba->fcp_poll_timer);
  1487. lpfc_stop_vport_timers(phba->pport);
  1488. del_timer_sync(&phba->sli.mbox_tmo);
  1489. del_timer_sync(&phba->fabric_block_timer);
  1490. phba->hb_outstanding = 0;
  1491. del_timer_sync(&phba->hb_tmofunc);
  1492. return;
  1493. }
  1494. /**
  1495. * lpfc_block_mgmt_io: Mark a HBA's management interface as blocked.
  1496. * @phba: pointer to lpfc hba data structure.
  1497. *
  1498. * This routine marks a HBA's management interface as blocked. Once the HBA's
  1499. * management interface is marked as blocked, all the user space access to
  1500. * the HBA, whether they are from sysfs interface or libdfc interface will
  1501. * all be blocked. The HBA is set to block the management interface when the
  1502. * driver prepares the HBA interface for online or offline.
  1503. **/
  1504. static void
  1505. lpfc_block_mgmt_io(struct lpfc_hba * phba)
  1506. {
  1507. unsigned long iflag;
  1508. spin_lock_irqsave(&phba->hbalock, iflag);
  1509. phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
  1510. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1511. }
  1512. /**
  1513. * lpfc_online: Initialize and bring a HBA online.
  1514. * @phba: pointer to lpfc hba data structure.
  1515. *
  1516. * This routine initializes the HBA and brings a HBA online. During this
  1517. * process, the management interface is blocked to prevent user space access
  1518. * to the HBA interfering with the driver initialization.
  1519. *
  1520. * Return codes
  1521. * 0 - successful
  1522. * 1 - failed
  1523. **/
  1524. int
  1525. lpfc_online(struct lpfc_hba *phba)
  1526. {
  1527. struct lpfc_vport *vport = phba->pport;
  1528. struct lpfc_vport **vports;
  1529. int i;
  1530. if (!phba)
  1531. return 0;
  1532. if (!(vport->fc_flag & FC_OFFLINE_MODE))
  1533. return 0;
  1534. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  1535. "0458 Bring Adapter online\n");
  1536. lpfc_block_mgmt_io(phba);
  1537. if (!lpfc_sli_queue_setup(phba)) {
  1538. lpfc_unblock_mgmt_io(phba);
  1539. return 1;
  1540. }
  1541. if (lpfc_sli_hba_setup(phba)) { /* Initialize the HBA */
  1542. lpfc_unblock_mgmt_io(phba);
  1543. return 1;
  1544. }
  1545. vports = lpfc_create_vport_work_array(phba);
  1546. if (vports != NULL)
  1547. for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  1548. struct Scsi_Host *shost;
  1549. shost = lpfc_shost_from_vport(vports[i]);
  1550. spin_lock_irq(shost->host_lock);
  1551. vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
  1552. if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
  1553. vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
  1554. spin_unlock_irq(shost->host_lock);
  1555. }
  1556. lpfc_destroy_vport_work_array(phba, vports);
  1557. lpfc_unblock_mgmt_io(phba);
  1558. return 0;
  1559. }
  1560. /**
  1561. * lpfc_unblock_mgmt_io: Mark a HBA's management interface to be not blocked.
  1562. * @phba: pointer to lpfc hba data structure.
  1563. *
  1564. * This routine marks a HBA's management interface as not blocked. Once the
  1565. * HBA's management interface is marked as not blocked, all the user space
  1566. * access to the HBA, whether they are from sysfs interface or libdfc
  1567. * interface will be allowed. The HBA is set to block the management interface
  1568. * when the driver prepares the HBA interface for online or offline and then
  1569. * set to unblock the management interface afterwards.
  1570. **/
  1571. void
  1572. lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
  1573. {
  1574. unsigned long iflag;
  1575. spin_lock_irqsave(&phba->hbalock, iflag);
  1576. phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
  1577. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1578. }
  1579. /**
  1580. * lpfc_offline_prep: Prepare a HBA to be brought offline.
  1581. * @phba: pointer to lpfc hba data structure.
  1582. *
  1583. * This routine is invoked to prepare a HBA to be brought offline. It performs
  1584. * unregistration login to all the nodes on all vports and flushes the mailbox
  1585. * queue to make it ready to be brought offline.
  1586. **/
  1587. void
  1588. lpfc_offline_prep(struct lpfc_hba * phba)
  1589. {
  1590. struct lpfc_vport *vport = phba->pport;
  1591. struct lpfc_nodelist *ndlp, *next_ndlp;
  1592. struct lpfc_vport **vports;
  1593. int i;
  1594. if (vport->fc_flag & FC_OFFLINE_MODE)
  1595. return;
  1596. lpfc_block_mgmt_io(phba);
  1597. lpfc_linkdown(phba);
  1598. /* Issue an unreg_login to all nodes on all vports */
  1599. vports = lpfc_create_vport_work_array(phba);
  1600. if (vports != NULL) {
  1601. for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  1602. struct Scsi_Host *shost;
  1603. if (vports[i]->load_flag & FC_UNLOADING)
  1604. continue;
  1605. shost = lpfc_shost_from_vport(vports[i]);
  1606. list_for_each_entry_safe(ndlp, next_ndlp,
  1607. &vports[i]->fc_nodes,
  1608. nlp_listp) {
  1609. if (!NLP_CHK_NODE_ACT(ndlp))
  1610. continue;
  1611. if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
  1612. continue;
  1613. if (ndlp->nlp_type & NLP_FABRIC) {
  1614. lpfc_disc_state_machine(vports[i], ndlp,
  1615. NULL, NLP_EVT_DEVICE_RECOVERY);
  1616. lpfc_disc_state_machine(vports[i], ndlp,
  1617. NULL, NLP_EVT_DEVICE_RM);
  1618. }
  1619. spin_lock_irq(shost->host_lock);
  1620. ndlp->nlp_flag &= ~NLP_NPR_ADISC;
  1621. spin_unlock_irq(shost->host_lock);
  1622. lpfc_unreg_rpi(vports[i], ndlp);
  1623. }
  1624. }
  1625. }
  1626. lpfc_destroy_vport_work_array(phba, vports);
  1627. lpfc_sli_flush_mbox_queue(phba);
  1628. }
  1629. /**
  1630. * lpfc_offline: Bring a HBA offline.
  1631. * @phba: pointer to lpfc hba data structure.
  1632. *
  1633. * This routine actually brings a HBA offline. It stops all the timers
  1634. * associated with the HBA, brings down the SLI layer, and eventually
  1635. * marks the HBA as in offline state for the upper layer protocol.
  1636. **/
  1637. void
  1638. lpfc_offline(struct lpfc_hba *phba)
  1639. {
  1640. struct Scsi_Host *shost;
  1641. struct lpfc_vport **vports;
  1642. int i;
  1643. if (phba->pport->fc_flag & FC_OFFLINE_MODE)
  1644. return;
  1645. /* stop all timers associated with this hba */
  1646. lpfc_stop_phba_timers(phba);
  1647. vports = lpfc_create_vport_work_array(phba);
  1648. if (vports != NULL)
  1649. for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
  1650. lpfc_stop_vport_timers(vports[i]);
  1651. lpfc_destroy_vport_work_array(phba, vports);
  1652. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  1653. "0460 Bring Adapter offline\n");
  1654. /* Bring down the SLI Layer and cleanup. The HBA is offline
  1655. now. */
  1656. lpfc_sli_hba_down(phba);
  1657. spin_lock_irq(&phba->hbalock);
  1658. phba->work_ha = 0;
  1659. spin_unlock_irq(&phba->hbalock);
  1660. vports = lpfc_create_vport_work_array(phba);
  1661. if (vports != NULL)
  1662. for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  1663. shost = lpfc_shost_from_vport(vports[i]);
  1664. spin_lock_irq(shost->host_lock);
  1665. vports[i]->work_port_events = 0;
  1666. vports[i]->fc_flag |= FC_OFFLINE_MODE;
  1667. spin_unlock_irq(shost->host_lock);
  1668. }
  1669. lpfc_destroy_vport_work_array(phba, vports);
  1670. }
  1671. /**
  1672. * lpfc_scsi_free: Free all the SCSI buffers and IOCBs from driver lists.
  1673. * @phba: pointer to lpfc hba data structure.
  1674. *
  1675. * This routine is to free all the SCSI buffers and IOCBs from the driver
  1676. * list back to kernel. It is called from lpfc_pci_remove_one to free
  1677. * the internal resources before the device is removed from the system.
  1678. *
  1679. * Return codes
  1680. * 0 - successful (for now, it always returns 0)
  1681. **/
  1682. static int
  1683. lpfc_scsi_free(struct lpfc_hba *phba)
  1684. {
  1685. struct lpfc_scsi_buf *sb, *sb_next;
  1686. struct lpfc_iocbq *io, *io_next;
  1687. spin_lock_irq(&phba->hbalock);
  1688. /* Release all the lpfc_scsi_bufs maintained by this host. */
  1689. list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
  1690. list_del(&sb->list);
  1691. pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
  1692. sb->dma_handle);
  1693. kfree(sb);
  1694. phba->total_scsi_bufs--;
  1695. }
  1696. /* Release all the lpfc_iocbq entries maintained by this host. */
  1697. list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
  1698. list_del(&io->list);
  1699. kfree(io);
  1700. phba->total_iocbq_bufs--;
  1701. }
  1702. spin_unlock_irq(&phba->hbalock);
  1703. return 0;
  1704. }
  1705. /**
  1706. * lpfc_create_port: Create an FC port.
  1707. * @phba: pointer to lpfc hba data structure.
  1708. * @instance: a unique integer ID to this FC port.
  1709. * @dev: pointer to the device data structure.
  1710. *
  1711. * This routine creates a FC port for the upper layer protocol. The FC port
  1712. * can be created on top of either a physical port or a virtual port provided
  1713. * by the HBA. This routine also allocates a SCSI host data structure (shost)
  1714. * and associates the FC port created before adding the shost into the SCSI
  1715. * layer.
  1716. *
  1717. * Return codes
  1718. * @vport - pointer to the virtual N_Port data structure.
  1719. * NULL - port create failed.
  1720. **/
  1721. struct lpfc_vport *
  1722. lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
  1723. {
  1724. struct lpfc_vport *vport;
  1725. struct Scsi_Host *shost;
  1726. int error = 0;
  1727. if (dev != &phba->pcidev->dev)
  1728. shost = scsi_host_alloc(&lpfc_vport_template,
  1729. sizeof(struct lpfc_vport));
  1730. else
  1731. shost = scsi_host_alloc(&lpfc_template,
  1732. sizeof(struct lpfc_vport));
  1733. if (!shost)
  1734. goto out;
  1735. vport = (struct lpfc_vport *) shost->hostdata;
  1736. vport->phba = phba;
  1737. vport->load_flag |= FC_LOADING;
  1738. vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
  1739. vport->fc_rscn_flush = 0;
  1740. lpfc_get_vport_cfgparam(vport);
  1741. shost->unique_id = instance;
  1742. shost->max_id = LPFC_MAX_TARGET;
  1743. shost->max_lun = vport->cfg_max_luns;
  1744. shost->this_id = -1;
  1745. shost->max_cmd_len = 16;
  1746. /*
  1747. * Set initial can_queue value since 0 is no longer supported and
  1748. * scsi_add_host will fail. This will be adjusted later based on the
  1749. * max xri value determined in hba setup.
  1750. */
  1751. shost->can_queue = phba->cfg_hba_queue_depth - 10;
  1752. if (dev != &phba->pcidev->dev) {
  1753. shost->transportt = lpfc_vport_transport_template;
  1754. vport->port_type = LPFC_NPIV_PORT;
  1755. } else {
  1756. shost->transportt = lpfc_transport_template;
  1757. vport->port_type = LPFC_PHYSICAL_PORT;
  1758. }
  1759. /* Initialize all internally managed lists. */
  1760. INIT_LIST_HEAD(&vport->fc_nodes);
  1761. spin_lock_init(&vport->work_port_lock);
  1762. init_timer(&vport->fc_disctmo);
  1763. vport->fc_disctmo.function = lpfc_disc_timeout;
  1764. vport->fc_disctmo.data = (unsigned long)vport;
  1765. init_timer(&vport->fc_fdmitmo);
  1766. vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
  1767. vport->fc_fdmitmo.data = (unsigned long)vport;
  1768. init_timer(&vport->els_tmofunc);
  1769. vport->els_tmofunc.function = lpfc_els_timeout;
  1770. vport->els_tmofunc.data = (unsigned long)vport;
  1771. error = scsi_add_host(shost, dev);
  1772. if (error)
  1773. goto out_put_shost;
  1774. spin_lock_irq(&phba->hbalock);
  1775. list_add_tail(&vport->listentry, &phba->port_list);
  1776. spin_unlock_irq(&phba->hbalock);
  1777. return vport;
  1778. out_put_shost:
  1779. scsi_host_put(shost);
  1780. out:
  1781. return NULL;
  1782. }
  1783. /**
  1784. * destroy_port: Destroy an FC port.
  1785. * @vport: pointer to an lpfc virtual N_Port data structure.
  1786. *
  1787. * This routine destroys a FC port from the upper layer protocol. All the
  1788. * resources associated with the port are released.
  1789. **/
  1790. void
  1791. destroy_port(struct lpfc_vport *vport)
  1792. {
  1793. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  1794. struct lpfc_hba *phba = vport->phba;
  1795. kfree(vport->vname);
  1796. lpfc_debugfs_terminate(vport);
  1797. fc_remove_host(shost);
  1798. scsi_remove_host(shost);
  1799. spin_lock_irq(&phba->hbalock);
  1800. list_del_init(&vport->listentry);
  1801. spin_unlock_irq(&phba->hbalock);
  1802. lpfc_cleanup(vport);
  1803. return;
  1804. }
  1805. /**
  1806. * lpfc_get_instance: Get a unique integer ID.
  1807. *
  1808. * This routine allocates a unique integer ID from lpfc_hba_index pool. It
  1809. * uses the kernel idr facility to perform the task.
  1810. *
  1811. * Return codes:
  1812. * instance - a unique integer ID allocated as the new instance.
  1813. * -1 - lpfc get instance failed.
  1814. **/
  1815. int
  1816. lpfc_get_instance(void)
  1817. {
  1818. int instance = 0;
  1819. /* Assign an unused number */
  1820. if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
  1821. return -1;
  1822. if (idr_get_new(&lpfc_hba_index, NULL, &instance))
  1823. return -1;
  1824. return instance;
  1825. }
  1826. /**
  1827. * lpfc_scan_finished: method for SCSI layer to detect whether scan is done.
  1828. * @shost: pointer to SCSI host data structure.
  1829. * @time: elapsed time of the scan in jiffies.
  1830. *
  1831. * This routine is called by the SCSI layer with a SCSI host to determine
  1832. * whether the scan host is finished.
  1833. *
  1834. * Note: there is no scan_start function as adapter initialization will have
  1835. * asynchronously kicked off the link initialization.
  1836. *
  1837. * Return codes
  1838. * 0 - SCSI host scan is not over yet.
  1839. * 1 - SCSI host scan is over.
  1840. **/
  1841. int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
  1842. {
  1843. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  1844. struct lpfc_hba *phba = vport->phba;
  1845. int stat = 0;
  1846. spin_lock_irq(shost->host_lock);
  1847. if (vport->load_flag & FC_UNLOADING) {
  1848. stat = 1;
  1849. goto finished;
  1850. }
  1851. if (time >= 30 * HZ) {
  1852. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  1853. "0461 Scanning longer than 30 "
  1854. "seconds. Continuing initialization\n");
  1855. stat = 1;
  1856. goto finished;
  1857. }
  1858. if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
  1859. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  1860. "0465 Link down longer than 15 "
  1861. "seconds. Continuing initialization\n");
  1862. stat = 1;
  1863. goto finished;
  1864. }
  1865. if (vport->port_state != LPFC_VPORT_READY)
  1866. goto finished;
  1867. if (vport->num_disc_nodes || vport->fc_prli_sent)
  1868. goto finished;
  1869. if (vport->fc_map_cnt == 0 && time < 2 * HZ)
  1870. goto finished;
  1871. if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
  1872. goto finished;
  1873. stat = 1;
  1874. finished:
  1875. spin_unlock_irq(shost->host_lock);
  1876. return stat;
  1877. }
  1878. /**
  1879. * lpfc_host_attrib_init: Initialize SCSI host attributes on a FC port.
  1880. * @shost: pointer to SCSI host data structure.
  1881. *
  1882. * This routine initializes a given SCSI host attributes on a FC port. The
  1883. * SCSI host can be either on top of a physical port or a virtual port.
  1884. **/
  1885. void lpfc_host_attrib_init(struct Scsi_Host *shost)
  1886. {
  1887. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  1888. struct lpfc_hba *phba = vport->phba;
  1889. /*
  1890. * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
  1891. */
  1892. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  1893. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  1894. fc_host_supported_classes(shost) = FC_COS_CLASS3;
  1895. memset(fc_host_supported_fc4s(shost), 0,
  1896. sizeof(fc_host_supported_fc4s(shost)));
  1897. fc_host_supported_fc4s(shost)[2] = 1;
  1898. fc_host_supported_fc4s(shost)[7] = 1;
  1899. lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
  1900. sizeof fc_host_symbolic_name(shost));
  1901. fc_host_supported_speeds(shost) = 0;
  1902. if (phba->lmt & LMT_10Gb)
  1903. fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
  1904. if (phba->lmt & LMT_8Gb)
  1905. fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
  1906. if (phba->lmt & LMT_4Gb)
  1907. fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
  1908. if (phba->lmt & LMT_2Gb)
  1909. fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
  1910. if (phba->lmt & LMT_1Gb)
  1911. fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
  1912. fc_host_maxframe_size(shost) =
  1913. (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
  1914. (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
  1915. /* This value is also unchanging */
  1916. memset(fc_host_active_fc4s(shost), 0,
  1917. sizeof(fc_host_active_fc4s(shost)));
  1918. fc_host_active_fc4s(shost)[2] = 1;
  1919. fc_host_active_fc4s(shost)[7] = 1;
  1920. fc_host_max_npiv_vports(shost) = phba->max_vpi;
  1921. spin_lock_irq(shost->host_lock);
  1922. vport->load_flag &= ~FC_LOADING;
  1923. spin_unlock_irq(shost->host_lock);
  1924. }
  1925. /**
  1926. * lpfc_enable_msix: Enable MSI-X interrupt mode.
  1927. * @phba: pointer to lpfc hba data structure.
  1928. *
  1929. * This routine is invoked to enable the MSI-X interrupt vectors. The kernel
  1930. * function pci_enable_msix() is called to enable the MSI-X vectors. Note that
  1931. * pci_enable_msix(), once invoked, enables either all or nothing, depending
  1932. * on the current availability of PCI vector resources. The device driver is
  1933. * responsible for calling the individual request_irq() to register each MSI-X
  1934. * vector with a interrupt handler, which is done in this function. Note that
  1935. * later when device is unloading, the driver should always call free_irq()
  1936. * on all MSI-X vectors it has done request_irq() on before calling
  1937. * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
  1938. * will be left with MSI-X enabled and leaks its vectors.
  1939. *
  1940. * Return codes
  1941. * 0 - sucessful
  1942. * other values - error
  1943. **/
  1944. static int
  1945. lpfc_enable_msix(struct lpfc_hba *phba)
  1946. {
  1947. int error;
  1948. phba->msix_entries[0].entry = 0;
  1949. phba->msix_entries[0].vector = 0;
  1950. error = pci_enable_msix(phba->pcidev, phba->msix_entries,
  1951. ARRAY_SIZE(phba->msix_entries));
  1952. if (error) {
  1953. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  1954. "0420 Enable MSI-X failed (%d), continuing "
  1955. "with MSI\n", error);
  1956. pci_disable_msix(phba->pcidev);
  1957. return error;
  1958. }
  1959. error = request_irq(phba->msix_entries[0].vector, lpfc_intr_handler, 0,
  1960. LPFC_DRIVER_NAME, phba);
  1961. if (error) {
  1962. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1963. "0421 MSI-X request_irq failed (%d), "
  1964. "continuing with MSI\n", error);
  1965. pci_disable_msix(phba->pcidev);
  1966. }
  1967. return error;
  1968. }
  1969. /**
  1970. * lpfc_disable_msix: Disable MSI-X interrupt mode.
  1971. * @phba: pointer to lpfc hba data structure.
  1972. *
  1973. * This routine is invoked to release the MSI-X vectors and then disable the
  1974. * MSI-X interrupt mode.
  1975. **/
  1976. static void
  1977. lpfc_disable_msix(struct lpfc_hba *phba)
  1978. {
  1979. free_irq(phba->msix_entries[0].vector, phba);
  1980. pci_disable_msix(phba->pcidev);
  1981. }
  1982. /**
  1983. * lpfc_pci_probe_one: lpfc PCI probe func to register device to PCI subsystem.
  1984. * @pdev: pointer to PCI device
  1985. * @pid: pointer to PCI device identifier
  1986. *
  1987. * This routine is to be registered to the kernel's PCI subsystem. When an
  1988. * Emulex HBA is presented in PCI bus, the kernel PCI subsystem looks at
  1989. * PCI device-specific information of the device and driver to see if the
  1990. * driver state that it can support this kind of device. If the match is
  1991. * successful, the driver core invokes this routine. If this routine
  1992. * determines it can claim the HBA, it does all the initialization that it
  1993. * needs to do to handle the HBA properly.
  1994. *
  1995. * Return code
  1996. * 0 - driver can claim the device
  1997. * negative value - driver can not claim the device
  1998. **/
  1999. static int __devinit
  2000. lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
  2001. {
  2002. struct lpfc_vport *vport = NULL;
  2003. struct lpfc_hba *phba;
  2004. struct lpfc_sli *psli;
  2005. struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
  2006. struct Scsi_Host *shost = NULL;
  2007. void *ptr;
  2008. unsigned long bar0map_len, bar2map_len;
  2009. int error = -ENODEV, retval;
  2010. int i, hbq_count;
  2011. uint16_t iotag;
  2012. int bars = pci_select_bars(pdev, IORESOURCE_MEM);
  2013. if (pci_enable_device_mem(pdev))
  2014. goto out;
  2015. if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
  2016. goto out_disable_device;
  2017. phba = kzalloc(sizeof (struct lpfc_hba), GFP_KERNEL);
  2018. if (!phba)
  2019. goto out_release_regions;
  2020. spin_lock_init(&phba->hbalock);
  2021. /* Initialize ndlp management spinlock */
  2022. spin_lock_init(&phba->ndlp_lock);
  2023. phba->pcidev = pdev;
  2024. /* Assign an unused board number */
  2025. if ((phba->brd_no = lpfc_get_instance()) < 0)
  2026. goto out_free_phba;
  2027. INIT_LIST_HEAD(&phba->port_list);
  2028. /*
  2029. * Get all the module params for configuring this host and then
  2030. * establish the host.
  2031. */
  2032. lpfc_get_cfgparam(phba);
  2033. phba->max_vpi = LPFC_MAX_VPI;
  2034. /* Initialize timers used by driver */
  2035. init_timer(&phba->hb_tmofunc);
  2036. phba->hb_tmofunc.function = lpfc_hb_timeout;
  2037. phba->hb_tmofunc.data = (unsigned long)phba;
  2038. psli = &phba->sli;
  2039. init_timer(&psli->mbox_tmo);
  2040. psli->mbox_tmo.function = lpfc_mbox_timeout;
  2041. psli->mbox_tmo.data = (unsigned long) phba;
  2042. init_timer(&phba->fcp_poll_timer);
  2043. phba->fcp_poll_timer.function = lpfc_poll_timeout;
  2044. phba->fcp_poll_timer.data = (unsigned long) phba;
  2045. init_timer(&phba->fabric_block_timer);
  2046. phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
  2047. phba->fabric_block_timer.data = (unsigned long) phba;
  2048. pci_set_master(pdev);
  2049. pci_try_set_mwi(pdev);
  2050. if (pci_set_dma_mask(phba->pcidev, DMA_64BIT_MASK) != 0)
  2051. if (pci_set_dma_mask(phba->pcidev, DMA_32BIT_MASK) != 0)
  2052. goto out_idr_remove;
  2053. /*
  2054. * Get the bus address of Bar0 and Bar2 and the number of bytes
  2055. * required by each mapping.
  2056. */
  2057. phba->pci_bar0_map = pci_resource_start(phba->pcidev, 0);
  2058. bar0map_len = pci_resource_len(phba->pcidev, 0);
  2059. phba->pci_bar2_map = pci_resource_start(phba->pcidev, 2);
  2060. bar2map_len = pci_resource_len(phba->pcidev, 2);
  2061. /* Map HBA SLIM to a kernel virtual address. */
  2062. phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
  2063. if (!phba->slim_memmap_p) {
  2064. error = -ENODEV;
  2065. dev_printk(KERN_ERR, &pdev->dev,
  2066. "ioremap failed for SLIM memory.\n");
  2067. goto out_idr_remove;
  2068. }
  2069. /* Map HBA Control Registers to a kernel virtual address. */
  2070. phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
  2071. if (!phba->ctrl_regs_memmap_p) {
  2072. error = -ENODEV;
  2073. dev_printk(KERN_ERR, &pdev->dev,
  2074. "ioremap failed for HBA control registers.\n");
  2075. goto out_iounmap_slim;
  2076. }
  2077. /* Allocate memory for SLI-2 structures */
  2078. phba->slim2p.virt = dma_alloc_coherent(&phba->pcidev->dev,
  2079. SLI2_SLIM_SIZE,
  2080. &phba->slim2p.phys,
  2081. GFP_KERNEL);
  2082. if (!phba->slim2p.virt)
  2083. goto out_iounmap;
  2084. memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
  2085. phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
  2086. phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
  2087. phba->IOCBs = (phba->slim2p.virt +
  2088. offsetof(struct lpfc_sli2_slim, IOCBs));
  2089. phba->hbqslimp.virt = dma_alloc_coherent(&phba->pcidev->dev,
  2090. lpfc_sli_hbq_size(),
  2091. &phba->hbqslimp.phys,
  2092. GFP_KERNEL);
  2093. if (!phba->hbqslimp.virt)
  2094. goto out_free_slim;
  2095. hbq_count = lpfc_sli_hbq_count();
  2096. ptr = phba->hbqslimp.virt;
  2097. for (i = 0; i < hbq_count; ++i) {
  2098. phba->hbqs[i].hbq_virt = ptr;
  2099. INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
  2100. ptr += (lpfc_hbq_defs[i]->entry_count *
  2101. sizeof(struct lpfc_hbq_entry));
  2102. }
  2103. phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
  2104. phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
  2105. memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
  2106. INIT_LIST_HEAD(&phba->hbqbuf_in_list);
  2107. /* Initialize the SLI Layer to run with lpfc HBAs. */
  2108. lpfc_sli_setup(phba);
  2109. lpfc_sli_queue_setup(phba);
  2110. retval = lpfc_mem_alloc(phba);
  2111. if (retval) {
  2112. error = retval;
  2113. goto out_free_hbqslimp;
  2114. }
  2115. /* Initialize and populate the iocb list per host. */
  2116. INIT_LIST_HEAD(&phba->lpfc_iocb_list);
  2117. for (i = 0; i < LPFC_IOCB_LIST_CNT; i++) {
  2118. iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
  2119. if (iocbq_entry == NULL) {
  2120. printk(KERN_ERR "%s: only allocated %d iocbs of "
  2121. "expected %d count. Unloading driver.\n",
  2122. __func__, i, LPFC_IOCB_LIST_CNT);
  2123. error = -ENOMEM;
  2124. goto out_free_iocbq;
  2125. }
  2126. iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
  2127. if (iotag == 0) {
  2128. kfree (iocbq_entry);
  2129. printk(KERN_ERR "%s: failed to allocate IOTAG. "
  2130. "Unloading driver.\n",
  2131. __func__);
  2132. error = -ENOMEM;
  2133. goto out_free_iocbq;
  2134. }
  2135. spin_lock_irq(&phba->hbalock);
  2136. list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
  2137. phba->total_iocbq_bufs++;
  2138. spin_unlock_irq(&phba->hbalock);
  2139. }
  2140. /* Initialize HBA structure */
  2141. phba->fc_edtov = FF_DEF_EDTOV;
  2142. phba->fc_ratov = FF_DEF_RATOV;
  2143. phba->fc_altov = FF_DEF_ALTOV;
  2144. phba->fc_arbtov = FF_DEF_ARBTOV;
  2145. INIT_LIST_HEAD(&phba->work_list);
  2146. phba->work_ha_mask = (HA_ERATT|HA_MBATT|HA_LATT);
  2147. phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
  2148. /* Initialize the wait queue head for the kernel thread */
  2149. init_waitqueue_head(&phba->work_waitq);
  2150. /* Startup the kernel thread for this host adapter. */
  2151. phba->worker_thread = kthread_run(lpfc_do_work, phba,
  2152. "lpfc_worker_%d", phba->brd_no);
  2153. if (IS_ERR(phba->worker_thread)) {
  2154. error = PTR_ERR(phba->worker_thread);
  2155. goto out_free_iocbq;
  2156. }
  2157. /* Initialize the list of scsi buffers used by driver for scsi IO. */
  2158. spin_lock_init(&phba->scsi_buf_list_lock);
  2159. INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
  2160. /* Initialize list of fabric iocbs */
  2161. INIT_LIST_HEAD(&phba->fabric_iocb_list);
  2162. /* Initialize list to save ELS buffers */
  2163. INIT_LIST_HEAD(&phba->elsbuf);
  2164. vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
  2165. if (!vport)
  2166. goto out_kthread_stop;
  2167. shost = lpfc_shost_from_vport(vport);
  2168. phba->pport = vport;
  2169. lpfc_debugfs_initialize(vport);
  2170. pci_set_drvdata(pdev, shost);
  2171. phba->intr_type = NONE;
  2172. if (phba->cfg_use_msi == 2) {
  2173. error = lpfc_enable_msix(phba);
  2174. if (!error)
  2175. phba->intr_type = MSIX;
  2176. }
  2177. /* Fallback to MSI if MSI-X initialization failed */
  2178. if (phba->cfg_use_msi >= 1 && phba->intr_type == NONE) {
  2179. retval = pci_enable_msi(phba->pcidev);
  2180. if (!retval)
  2181. phba->intr_type = MSI;
  2182. else
  2183. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  2184. "0452 Enable MSI failed, continuing "
  2185. "with IRQ\n");
  2186. }
  2187. /* MSI-X is the only case the doesn't need to call request_irq */
  2188. if (phba->intr_type != MSIX) {
  2189. retval = request_irq(phba->pcidev->irq, lpfc_intr_handler,
  2190. IRQF_SHARED, LPFC_DRIVER_NAME, phba);
  2191. if (retval) {
  2192. lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0451 Enable "
  2193. "interrupt handler failed\n");
  2194. error = retval;
  2195. goto out_disable_msi;
  2196. } else if (phba->intr_type != MSI)
  2197. phba->intr_type = INTx;
  2198. }
  2199. phba->MBslimaddr = phba->slim_memmap_p;
  2200. phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
  2201. phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
  2202. phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
  2203. phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
  2204. if (lpfc_alloc_sysfs_attr(vport)) {
  2205. error = -ENOMEM;
  2206. goto out_free_irq;
  2207. }
  2208. if (lpfc_sli_hba_setup(phba)) {
  2209. error = -ENODEV;
  2210. goto out_remove_device;
  2211. }
  2212. /*
  2213. * hba setup may have changed the hba_queue_depth so we need to adjust
  2214. * the value of can_queue.
  2215. */
  2216. shost->can_queue = phba->cfg_hba_queue_depth - 10;
  2217. lpfc_host_attrib_init(shost);
  2218. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  2219. spin_lock_irq(shost->host_lock);
  2220. lpfc_poll_start_timer(phba);
  2221. spin_unlock_irq(shost->host_lock);
  2222. }
  2223. scsi_scan_host(shost);
  2224. return 0;
  2225. out_remove_device:
  2226. lpfc_free_sysfs_attr(vport);
  2227. spin_lock_irq(shost->host_lock);
  2228. vport->load_flag |= FC_UNLOADING;
  2229. spin_unlock_irq(shost->host_lock);
  2230. out_free_irq:
  2231. lpfc_stop_phba_timers(phba);
  2232. phba->pport->work_port_events = 0;
  2233. if (phba->intr_type == MSIX)
  2234. lpfc_disable_msix(phba);
  2235. else
  2236. free_irq(phba->pcidev->irq, phba);
  2237. out_disable_msi:
  2238. if (phba->intr_type == MSI)
  2239. pci_disable_msi(phba->pcidev);
  2240. destroy_port(vport);
  2241. out_kthread_stop:
  2242. kthread_stop(phba->worker_thread);
  2243. out_free_iocbq:
  2244. list_for_each_entry_safe(iocbq_entry, iocbq_next,
  2245. &phba->lpfc_iocb_list, list) {
  2246. kfree(iocbq_entry);
  2247. phba->total_iocbq_bufs--;
  2248. }
  2249. lpfc_mem_free(phba);
  2250. out_free_hbqslimp:
  2251. dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
  2252. phba->hbqslimp.virt, phba->hbqslimp.phys);
  2253. out_free_slim:
  2254. dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
  2255. phba->slim2p.virt, phba->slim2p.phys);
  2256. out_iounmap:
  2257. iounmap(phba->ctrl_regs_memmap_p);
  2258. out_iounmap_slim:
  2259. iounmap(phba->slim_memmap_p);
  2260. out_idr_remove:
  2261. idr_remove(&lpfc_hba_index, phba->brd_no);
  2262. out_free_phba:
  2263. kfree(phba);
  2264. out_release_regions:
  2265. pci_release_selected_regions(pdev, bars);
  2266. out_disable_device:
  2267. pci_disable_device(pdev);
  2268. out:
  2269. pci_set_drvdata(pdev, NULL);
  2270. if (shost)
  2271. scsi_host_put(shost);
  2272. return error;
  2273. }
  2274. /**
  2275. * lpfc_pci_remove_one: lpfc PCI func to unregister device from PCI subsystem.
  2276. * @pdev: pointer to PCI device
  2277. *
  2278. * This routine is to be registered to the kernel's PCI subsystem. When an
  2279. * Emulex HBA is removed from PCI bus. It perform all the necessary cleanup
  2280. * for the HBA device to be removed from the PCI subsystem properly.
  2281. **/
  2282. static void __devexit
  2283. lpfc_pci_remove_one(struct pci_dev *pdev)
  2284. {
  2285. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  2286. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  2287. struct lpfc_hba *phba = vport->phba;
  2288. int bars = pci_select_bars(pdev, IORESOURCE_MEM);
  2289. spin_lock_irq(&phba->hbalock);
  2290. vport->load_flag |= FC_UNLOADING;
  2291. spin_unlock_irq(&phba->hbalock);
  2292. kfree(vport->vname);
  2293. lpfc_free_sysfs_attr(vport);
  2294. kthread_stop(phba->worker_thread);
  2295. fc_remove_host(shost);
  2296. scsi_remove_host(shost);
  2297. lpfc_cleanup(vport);
  2298. /*
  2299. * Bring down the SLI Layer. This step disable all interrupts,
  2300. * clears the rings, discards all mailbox commands, and resets
  2301. * the HBA.
  2302. */
  2303. lpfc_sli_hba_down(phba);
  2304. lpfc_sli_brdrestart(phba);
  2305. lpfc_stop_phba_timers(phba);
  2306. spin_lock_irq(&phba->hbalock);
  2307. list_del_init(&vport->listentry);
  2308. spin_unlock_irq(&phba->hbalock);
  2309. lpfc_debugfs_terminate(vport);
  2310. if (phba->intr_type == MSIX)
  2311. lpfc_disable_msix(phba);
  2312. else {
  2313. free_irq(phba->pcidev->irq, phba);
  2314. if (phba->intr_type == MSI)
  2315. pci_disable_msi(phba->pcidev);
  2316. }
  2317. pci_set_drvdata(pdev, NULL);
  2318. scsi_host_put(shost);
  2319. /*
  2320. * Call scsi_free before mem_free since scsi bufs are released to their
  2321. * corresponding pools here.
  2322. */
  2323. lpfc_scsi_free(phba);
  2324. lpfc_mem_free(phba);
  2325. dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
  2326. phba->hbqslimp.virt, phba->hbqslimp.phys);
  2327. /* Free resources associated with SLI2 interface */
  2328. dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
  2329. phba->slim2p.virt, phba->slim2p.phys);
  2330. /* unmap adapter SLIM and Control Registers */
  2331. iounmap(phba->ctrl_regs_memmap_p);
  2332. iounmap(phba->slim_memmap_p);
  2333. idr_remove(&lpfc_hba_index, phba->brd_no);
  2334. kfree(phba);
  2335. pci_release_selected_regions(pdev, bars);
  2336. pci_disable_device(pdev);
  2337. }
  2338. /**
  2339. * lpfc_io_error_detected: Driver method for handling PCI I/O error detected.
  2340. * @pdev: pointer to PCI device.
  2341. * @state: the current PCI connection state.
  2342. *
  2343. * This routine is registered to the PCI subsystem for error handling. This
  2344. * function is called by the PCI subsystem after a PCI bus error affecting
  2345. * this device has been detected. When this function is invoked, it will
  2346. * need to stop all the I/Os and interrupt(s) to the device. Once that is
  2347. * done, it will return PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to
  2348. * perform proper recovery as desired.
  2349. *
  2350. * Return codes
  2351. * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
  2352. * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
  2353. **/
  2354. static pci_ers_result_t lpfc_io_error_detected(struct pci_dev *pdev,
  2355. pci_channel_state_t state)
  2356. {
  2357. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  2358. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  2359. struct lpfc_sli *psli = &phba->sli;
  2360. struct lpfc_sli_ring *pring;
  2361. if (state == pci_channel_io_perm_failure)
  2362. return PCI_ERS_RESULT_DISCONNECT;
  2363. pci_disable_device(pdev);
  2364. /*
  2365. * There may be I/Os dropped by the firmware.
  2366. * Error iocb (I/O) on txcmplq and let the SCSI layer
  2367. * retry it after re-establishing link.
  2368. */
  2369. pring = &psli->ring[psli->fcp_ring];
  2370. lpfc_sli_abort_iocb_ring(phba, pring);
  2371. if (phba->intr_type == MSIX)
  2372. lpfc_disable_msix(phba);
  2373. else {
  2374. free_irq(phba->pcidev->irq, phba);
  2375. if (phba->intr_type == MSI)
  2376. pci_disable_msi(phba->pcidev);
  2377. }
  2378. /* Request a slot reset. */
  2379. return PCI_ERS_RESULT_NEED_RESET;
  2380. }
  2381. /**
  2382. * lpfc_io_slot_reset: Restart a PCI device from scratch.
  2383. * @pdev: pointer to PCI device.
  2384. *
  2385. * This routine is registered to the PCI subsystem for error handling. This is
  2386. * called after PCI bus has been reset to restart the PCI card from scratch,
  2387. * as if from a cold-boot. During the PCI subsystem error recovery, after the
  2388. * driver returns PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform
  2389. * proper error recovery and then call this routine before calling the .resume
  2390. * method to recover the device. This function will initialize the HBA device,
  2391. * enable the interrupt, but it will just put the HBA to offline state without
  2392. * passing any I/O traffic.
  2393. *
  2394. * Return codes
  2395. * PCI_ERS_RESULT_RECOVERED - the device has been recovered
  2396. * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
  2397. */
  2398. static pci_ers_result_t lpfc_io_slot_reset(struct pci_dev *pdev)
  2399. {
  2400. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  2401. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  2402. struct lpfc_sli *psli = &phba->sli;
  2403. int error, retval;
  2404. dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
  2405. if (pci_enable_device_mem(pdev)) {
  2406. printk(KERN_ERR "lpfc: Cannot re-enable "
  2407. "PCI device after reset.\n");
  2408. return PCI_ERS_RESULT_DISCONNECT;
  2409. }
  2410. pci_set_master(pdev);
  2411. spin_lock_irq(&phba->hbalock);
  2412. psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
  2413. spin_unlock_irq(&phba->hbalock);
  2414. /* Enable configured interrupt method */
  2415. phba->intr_type = NONE;
  2416. if (phba->cfg_use_msi == 2) {
  2417. error = lpfc_enable_msix(phba);
  2418. if (!error)
  2419. phba->intr_type = MSIX;
  2420. }
  2421. /* Fallback to MSI if MSI-X initialization failed */
  2422. if (phba->cfg_use_msi >= 1 && phba->intr_type == NONE) {
  2423. retval = pci_enable_msi(phba->pcidev);
  2424. if (!retval)
  2425. phba->intr_type = MSI;
  2426. else
  2427. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  2428. "0470 Enable MSI failed, continuing "
  2429. "with IRQ\n");
  2430. }
  2431. /* MSI-X is the only case the doesn't need to call request_irq */
  2432. if (phba->intr_type != MSIX) {
  2433. retval = request_irq(phba->pcidev->irq, lpfc_intr_handler,
  2434. IRQF_SHARED, LPFC_DRIVER_NAME, phba);
  2435. if (retval) {
  2436. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2437. "0471 Enable interrupt handler "
  2438. "failed\n");
  2439. } else if (phba->intr_type != MSI)
  2440. phba->intr_type = INTx;
  2441. }
  2442. /* Take device offline; this will perform cleanup */
  2443. lpfc_offline(phba);
  2444. lpfc_sli_brdrestart(phba);
  2445. return PCI_ERS_RESULT_RECOVERED;
  2446. }
  2447. /**
  2448. * lpfc_io_resume: Resume PCI I/O operation.
  2449. * @pdev: pointer to PCI device
  2450. *
  2451. * This routine is registered to the PCI subsystem for error handling. It is
  2452. * called when kernel error recovery tells the lpfc driver that it is ok to
  2453. * resume normal PCI operation after PCI bus error recovery. After this call,
  2454. * traffic can start to flow from this device again.
  2455. */
  2456. static void lpfc_io_resume(struct pci_dev *pdev)
  2457. {
  2458. struct Scsi_Host *shost = pci_get_drvdata(pdev);
  2459. struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
  2460. lpfc_online(phba);
  2461. }
  2462. static struct pci_device_id lpfc_id_table[] = {
  2463. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
  2464. PCI_ANY_ID, PCI_ANY_ID, },
  2465. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
  2466. PCI_ANY_ID, PCI_ANY_ID, },
  2467. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
  2468. PCI_ANY_ID, PCI_ANY_ID, },
  2469. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
  2470. PCI_ANY_ID, PCI_ANY_ID, },
  2471. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
  2472. PCI_ANY_ID, PCI_ANY_ID, },
  2473. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
  2474. PCI_ANY_ID, PCI_ANY_ID, },
  2475. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
  2476. PCI_ANY_ID, PCI_ANY_ID, },
  2477. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
  2478. PCI_ANY_ID, PCI_ANY_ID, },
  2479. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
  2480. PCI_ANY_ID, PCI_ANY_ID, },
  2481. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
  2482. PCI_ANY_ID, PCI_ANY_ID, },
  2483. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
  2484. PCI_ANY_ID, PCI_ANY_ID, },
  2485. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
  2486. PCI_ANY_ID, PCI_ANY_ID, },
  2487. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
  2488. PCI_ANY_ID, PCI_ANY_ID, },
  2489. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
  2490. PCI_ANY_ID, PCI_ANY_ID, },
  2491. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
  2492. PCI_ANY_ID, PCI_ANY_ID, },
  2493. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
  2494. PCI_ANY_ID, PCI_ANY_ID, },
  2495. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
  2496. PCI_ANY_ID, PCI_ANY_ID, },
  2497. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
  2498. PCI_ANY_ID, PCI_ANY_ID, },
  2499. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
  2500. PCI_ANY_ID, PCI_ANY_ID, },
  2501. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
  2502. PCI_ANY_ID, PCI_ANY_ID, },
  2503. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
  2504. PCI_ANY_ID, PCI_ANY_ID, },
  2505. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
  2506. PCI_ANY_ID, PCI_ANY_ID, },
  2507. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
  2508. PCI_ANY_ID, PCI_ANY_ID, },
  2509. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
  2510. PCI_ANY_ID, PCI_ANY_ID, },
  2511. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
  2512. PCI_ANY_ID, PCI_ANY_ID, },
  2513. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
  2514. PCI_ANY_ID, PCI_ANY_ID, },
  2515. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
  2516. PCI_ANY_ID, PCI_ANY_ID, },
  2517. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
  2518. PCI_ANY_ID, PCI_ANY_ID, },
  2519. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
  2520. PCI_ANY_ID, PCI_ANY_ID, },
  2521. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
  2522. PCI_ANY_ID, PCI_ANY_ID, },
  2523. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
  2524. PCI_ANY_ID, PCI_ANY_ID, },
  2525. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
  2526. PCI_ANY_ID, PCI_ANY_ID, },
  2527. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
  2528. PCI_ANY_ID, PCI_ANY_ID, },
  2529. { 0 }
  2530. };
  2531. MODULE_DEVICE_TABLE(pci, lpfc_id_table);
  2532. static struct pci_error_handlers lpfc_err_handler = {
  2533. .error_detected = lpfc_io_error_detected,
  2534. .slot_reset = lpfc_io_slot_reset,
  2535. .resume = lpfc_io_resume,
  2536. };
  2537. static struct pci_driver lpfc_driver = {
  2538. .name = LPFC_DRIVER_NAME,
  2539. .id_table = lpfc_id_table,
  2540. .probe = lpfc_pci_probe_one,
  2541. .remove = __devexit_p(lpfc_pci_remove_one),
  2542. .err_handler = &lpfc_err_handler,
  2543. };
  2544. /**
  2545. * lpfc_init: lpfc module initialization routine.
  2546. *
  2547. * This routine is to be invoked when the lpfc module is loaded into the
  2548. * kernel. The special kernel macro module_init() is used to indicate the
  2549. * role of this routine to the kernel as lpfc module entry point.
  2550. *
  2551. * Return codes
  2552. * 0 - successful
  2553. * -ENOMEM - FC attach transport failed
  2554. * all others - failed
  2555. */
  2556. static int __init
  2557. lpfc_init(void)
  2558. {
  2559. int error = 0;
  2560. printk(LPFC_MODULE_DESC "\n");
  2561. printk(LPFC_COPYRIGHT "\n");
  2562. if (lpfc_enable_npiv) {
  2563. lpfc_transport_functions.vport_create = lpfc_vport_create;
  2564. lpfc_transport_functions.vport_delete = lpfc_vport_delete;
  2565. }
  2566. lpfc_transport_template =
  2567. fc_attach_transport(&lpfc_transport_functions);
  2568. if (lpfc_transport_template == NULL)
  2569. return -ENOMEM;
  2570. if (lpfc_enable_npiv) {
  2571. lpfc_vport_transport_template =
  2572. fc_attach_transport(&lpfc_vport_transport_functions);
  2573. if (lpfc_vport_transport_template == NULL) {
  2574. fc_release_transport(lpfc_transport_template);
  2575. return -ENOMEM;
  2576. }
  2577. }
  2578. error = pci_register_driver(&lpfc_driver);
  2579. if (error) {
  2580. fc_release_transport(lpfc_transport_template);
  2581. if (lpfc_enable_npiv)
  2582. fc_release_transport(lpfc_vport_transport_template);
  2583. }
  2584. return error;
  2585. }
  2586. /**
  2587. * lpfc_exit: lpfc module removal routine.
  2588. *
  2589. * This routine is invoked when the lpfc module is removed from the kernel.
  2590. * The special kernel macro module_exit() is used to indicate the role of
  2591. * this routine to the kernel as lpfc module exit point.
  2592. */
  2593. static void __exit
  2594. lpfc_exit(void)
  2595. {
  2596. pci_unregister_driver(&lpfc_driver);
  2597. fc_release_transport(lpfc_transport_template);
  2598. if (lpfc_enable_npiv)
  2599. fc_release_transport(lpfc_vport_transport_template);
  2600. }
  2601. module_init(lpfc_init);
  2602. module_exit(lpfc_exit);
  2603. MODULE_LICENSE("GPL");
  2604. MODULE_DESCRIPTION(LPFC_MODULE_DESC);
  2605. MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
  2606. MODULE_VERSION("0:" LPFC_DRIVER_VERSION);