lpfc_init.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2006 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 <scsi/scsi.h>
  30. #include <scsi/scsi_device.h>
  31. #include <scsi/scsi_host.h>
  32. #include <scsi/scsi_transport_fc.h>
  33. #include "lpfc_hw.h"
  34. #include "lpfc_sli.h"
  35. #include "lpfc_disc.h"
  36. #include "lpfc_scsi.h"
  37. #include "lpfc.h"
  38. #include "lpfc_logmsg.h"
  39. #include "lpfc_crtn.h"
  40. #include "lpfc_version.h"
  41. static int lpfc_parse_vpd(struct lpfc_hba *, uint8_t *, int);
  42. static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
  43. static int lpfc_post_rcv_buf(struct lpfc_hba *);
  44. static struct scsi_transport_template *lpfc_transport_template = NULL;
  45. static DEFINE_IDR(lpfc_hba_index);
  46. /************************************************************************/
  47. /* */
  48. /* lpfc_config_port_prep */
  49. /* This routine will do LPFC initialization prior to the */
  50. /* CONFIG_PORT mailbox command. This will be initialized */
  51. /* as a SLI layer callback routine. */
  52. /* This routine returns 0 on success or -ERESTART if it wants */
  53. /* the SLI layer to reset the HBA and try again. Any */
  54. /* other return value indicates an error. */
  55. /* */
  56. /************************************************************************/
  57. int
  58. lpfc_config_port_prep(struct lpfc_hba * phba)
  59. {
  60. lpfc_vpd_t *vp = &phba->vpd;
  61. int i = 0, rc;
  62. LPFC_MBOXQ_t *pmb;
  63. MAILBOX_t *mb;
  64. char *lpfc_vpd_data = NULL;
  65. uint16_t offset = 0;
  66. static char licensed[56] =
  67. "key unlock for use with gnu public licensed code only\0";
  68. static int init_key = 1;
  69. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  70. if (!pmb) {
  71. phba->hba_state = LPFC_HBA_ERROR;
  72. return -ENOMEM;
  73. }
  74. mb = &pmb->mb;
  75. phba->hba_state = LPFC_INIT_MBX_CMDS;
  76. if (lpfc_is_LC_HBA(phba->pcidev->device)) {
  77. if (init_key) {
  78. uint32_t *ptext = (uint32_t *) licensed;
  79. for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
  80. *ptext = cpu_to_be32(*ptext);
  81. init_key = 0;
  82. }
  83. lpfc_read_nv(phba, pmb);
  84. memset((char*)mb->un.varRDnvp.rsvd3, 0,
  85. sizeof (mb->un.varRDnvp.rsvd3));
  86. memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
  87. sizeof (licensed));
  88. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  89. if (rc != MBX_SUCCESS) {
  90. lpfc_printf_log(phba,
  91. KERN_ERR,
  92. LOG_MBOX,
  93. "%d:0324 Config Port initialization "
  94. "error, mbxCmd x%x READ_NVPARM, "
  95. "mbxStatus x%x\n",
  96. phba->brd_no,
  97. mb->mbxCommand, mb->mbxStatus);
  98. mempool_free(pmb, phba->mbox_mem_pool);
  99. return -ERESTART;
  100. }
  101. memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
  102. sizeof (mb->un.varRDnvp.nodename));
  103. }
  104. /* Setup and issue mailbox READ REV command */
  105. lpfc_read_rev(phba, pmb);
  106. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  107. if (rc != MBX_SUCCESS) {
  108. lpfc_printf_log(phba,
  109. KERN_ERR,
  110. LOG_INIT,
  111. "%d:0439 Adapter failed to init, mbxCmd x%x "
  112. "READ_REV, mbxStatus x%x\n",
  113. phba->brd_no,
  114. mb->mbxCommand, mb->mbxStatus);
  115. mempool_free( pmb, phba->mbox_mem_pool);
  116. return -ERESTART;
  117. }
  118. /*
  119. * The value of rr must be 1 since the driver set the cv field to 1.
  120. * This setting requires the FW to set all revision fields.
  121. */
  122. if (mb->un.varRdRev.rr == 0) {
  123. vp->rev.rBit = 0;
  124. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  125. "%d:0440 Adapter failed to init, READ_REV has "
  126. "missing revision information.\n",
  127. phba->brd_no);
  128. mempool_free(pmb, phba->mbox_mem_pool);
  129. return -ERESTART;
  130. }
  131. /* Save information as VPD data */
  132. vp->rev.rBit = 1;
  133. vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
  134. memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
  135. vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
  136. memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
  137. vp->rev.biuRev = mb->un.varRdRev.biuRev;
  138. vp->rev.smRev = mb->un.varRdRev.smRev;
  139. vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
  140. vp->rev.endecRev = mb->un.varRdRev.endecRev;
  141. vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
  142. vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
  143. vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
  144. vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
  145. vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
  146. vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
  147. if (lpfc_is_LC_HBA(phba->pcidev->device))
  148. memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
  149. sizeof (phba->RandomData));
  150. /* Get adapter VPD information */
  151. pmb->context2 = kmalloc(DMP_RSP_SIZE, GFP_KERNEL);
  152. if (!pmb->context2)
  153. goto out_free_mbox;
  154. lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
  155. if (!lpfc_vpd_data)
  156. goto out_free_context2;
  157. do {
  158. lpfc_dump_mem(phba, pmb, offset);
  159. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  160. if (rc != MBX_SUCCESS) {
  161. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  162. "%d:0441 VPD not present on adapter, "
  163. "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
  164. phba->brd_no,
  165. mb->mbxCommand, mb->mbxStatus);
  166. mb->un.varDmp.word_cnt = 0;
  167. }
  168. if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
  169. mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
  170. lpfc_sli_pcimem_bcopy(pmb->context2, lpfc_vpd_data + offset,
  171. mb->un.varDmp.word_cnt);
  172. offset += mb->un.varDmp.word_cnt;
  173. } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
  174. lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
  175. kfree(lpfc_vpd_data);
  176. out_free_context2:
  177. kfree(pmb->context2);
  178. out_free_mbox:
  179. mempool_free(pmb, phba->mbox_mem_pool);
  180. return 0;
  181. }
  182. /************************************************************************/
  183. /* */
  184. /* lpfc_config_port_post */
  185. /* This routine will do LPFC initialization after the */
  186. /* CONFIG_PORT mailbox command. This will be initialized */
  187. /* as a SLI layer callback routine. */
  188. /* This routine returns 0 on success. Any other return value */
  189. /* indicates an error. */
  190. /* */
  191. /************************************************************************/
  192. int
  193. lpfc_config_port_post(struct lpfc_hba * phba)
  194. {
  195. LPFC_MBOXQ_t *pmb;
  196. MAILBOX_t *mb;
  197. struct lpfc_dmabuf *mp;
  198. struct lpfc_sli *psli = &phba->sli;
  199. uint32_t status, timeout;
  200. int i, j, rc;
  201. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  202. if (!pmb) {
  203. phba->hba_state = LPFC_HBA_ERROR;
  204. return -ENOMEM;
  205. }
  206. mb = &pmb->mb;
  207. lpfc_config_link(phba, pmb);
  208. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  209. if (rc != MBX_SUCCESS) {
  210. lpfc_printf_log(phba,
  211. KERN_ERR,
  212. LOG_INIT,
  213. "%d:0447 Adapter failed init, mbxCmd x%x "
  214. "CONFIG_LINK mbxStatus x%x\n",
  215. phba->brd_no,
  216. mb->mbxCommand, mb->mbxStatus);
  217. phba->hba_state = LPFC_HBA_ERROR;
  218. mempool_free( pmb, phba->mbox_mem_pool);
  219. return -EIO;
  220. }
  221. /* Get login parameters for NID. */
  222. lpfc_read_sparam(phba, pmb);
  223. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  224. lpfc_printf_log(phba,
  225. KERN_ERR,
  226. LOG_INIT,
  227. "%d:0448 Adapter failed init, mbxCmd x%x "
  228. "READ_SPARM mbxStatus x%x\n",
  229. phba->brd_no,
  230. mb->mbxCommand, mb->mbxStatus);
  231. phba->hba_state = LPFC_HBA_ERROR;
  232. mp = (struct lpfc_dmabuf *) pmb->context1;
  233. mempool_free( pmb, phba->mbox_mem_pool);
  234. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  235. kfree(mp);
  236. return -EIO;
  237. }
  238. mp = (struct lpfc_dmabuf *) pmb->context1;
  239. memcpy(&phba->fc_sparam, mp->virt, sizeof (struct serv_parm));
  240. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  241. kfree(mp);
  242. pmb->context1 = NULL;
  243. if (phba->cfg_soft_wwnn)
  244. u64_to_wwn(phba->cfg_soft_wwnn, phba->fc_sparam.nodeName.u.wwn);
  245. if (phba->cfg_soft_wwpn)
  246. u64_to_wwn(phba->cfg_soft_wwpn, phba->fc_sparam.portName.u.wwn);
  247. memcpy(&phba->fc_nodename, &phba->fc_sparam.nodeName,
  248. sizeof (struct lpfc_name));
  249. memcpy(&phba->fc_portname, &phba->fc_sparam.portName,
  250. sizeof (struct lpfc_name));
  251. /* If no serial number in VPD data, use low 6 bytes of WWNN */
  252. /* This should be consolidated into parse_vpd ? - mr */
  253. if (phba->SerialNumber[0] == 0) {
  254. uint8_t *outptr;
  255. outptr = &phba->fc_nodename.u.s.IEEE[0];
  256. for (i = 0; i < 12; i++) {
  257. status = *outptr++;
  258. j = ((status & 0xf0) >> 4);
  259. if (j <= 9)
  260. phba->SerialNumber[i] =
  261. (char)((uint8_t) 0x30 + (uint8_t) j);
  262. else
  263. phba->SerialNumber[i] =
  264. (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
  265. i++;
  266. j = (status & 0xf);
  267. if (j <= 9)
  268. phba->SerialNumber[i] =
  269. (char)((uint8_t) 0x30 + (uint8_t) j);
  270. else
  271. phba->SerialNumber[i] =
  272. (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
  273. }
  274. }
  275. lpfc_read_config(phba, pmb);
  276. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  277. lpfc_printf_log(phba,
  278. KERN_ERR,
  279. LOG_INIT,
  280. "%d:0453 Adapter failed to init, mbxCmd x%x "
  281. "READ_CONFIG, mbxStatus x%x\n",
  282. phba->brd_no,
  283. mb->mbxCommand, mb->mbxStatus);
  284. phba->hba_state = LPFC_HBA_ERROR;
  285. mempool_free( pmb, phba->mbox_mem_pool);
  286. return -EIO;
  287. }
  288. /* Reset the DFT_HBA_Q_DEPTH to the max xri */
  289. if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
  290. phba->cfg_hba_queue_depth =
  291. mb->un.varRdConfig.max_xri + 1;
  292. phba->lmt = mb->un.varRdConfig.lmt;
  293. /* Get the default values for Model Name and Description */
  294. lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
  295. if ((phba->cfg_link_speed > LINK_SPEED_10G)
  296. || ((phba->cfg_link_speed == LINK_SPEED_1G)
  297. && !(phba->lmt & LMT_1Gb))
  298. || ((phba->cfg_link_speed == LINK_SPEED_2G)
  299. && !(phba->lmt & LMT_2Gb))
  300. || ((phba->cfg_link_speed == LINK_SPEED_4G)
  301. && !(phba->lmt & LMT_4Gb))
  302. || ((phba->cfg_link_speed == LINK_SPEED_8G)
  303. && !(phba->lmt & LMT_8Gb))
  304. || ((phba->cfg_link_speed == LINK_SPEED_10G)
  305. && !(phba->lmt & LMT_10Gb))) {
  306. /* Reset link speed to auto */
  307. lpfc_printf_log(phba,
  308. KERN_WARNING,
  309. LOG_LINK_EVENT,
  310. "%d:1302 Invalid speed for this board: "
  311. "Reset link speed to auto: x%x\n",
  312. phba->brd_no,
  313. phba->cfg_link_speed);
  314. phba->cfg_link_speed = LINK_SPEED_AUTO;
  315. }
  316. phba->hba_state = LPFC_LINK_DOWN;
  317. /* Only process IOCBs on ring 0 till hba_state is READY */
  318. if (psli->ring[psli->extra_ring].cmdringaddr)
  319. psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
  320. if (psli->ring[psli->fcp_ring].cmdringaddr)
  321. psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
  322. if (psli->ring[psli->next_ring].cmdringaddr)
  323. psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
  324. /* Post receive buffers for desired rings */
  325. lpfc_post_rcv_buf(phba);
  326. /* Enable appropriate host interrupts */
  327. spin_lock_irq(phba->host->host_lock);
  328. status = readl(phba->HCregaddr);
  329. status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
  330. if (psli->num_rings > 0)
  331. status |= HC_R0INT_ENA;
  332. if (psli->num_rings > 1)
  333. status |= HC_R1INT_ENA;
  334. if (psli->num_rings > 2)
  335. status |= HC_R2INT_ENA;
  336. if (psli->num_rings > 3)
  337. status |= HC_R3INT_ENA;
  338. if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
  339. (phba->cfg_poll & DISABLE_FCP_RING_INT))
  340. status &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  341. writel(status, phba->HCregaddr);
  342. readl(phba->HCregaddr); /* flush */
  343. spin_unlock_irq(phba->host->host_lock);
  344. /*
  345. * Setup the ring 0 (els) timeout handler
  346. */
  347. timeout = phba->fc_ratov << 1;
  348. mod_timer(&phba->els_tmofunc, jiffies + HZ * timeout);
  349. lpfc_init_link(phba, pmb, phba->cfg_topology, phba->cfg_link_speed);
  350. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  351. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  352. lpfc_set_loopback_flag(phba);
  353. if (rc != MBX_SUCCESS) {
  354. lpfc_printf_log(phba,
  355. KERN_ERR,
  356. LOG_INIT,
  357. "%d:0454 Adapter failed to init, mbxCmd x%x "
  358. "INIT_LINK, mbxStatus x%x\n",
  359. phba->brd_no,
  360. mb->mbxCommand, mb->mbxStatus);
  361. /* Clear all interrupt enable conditions */
  362. writel(0, phba->HCregaddr);
  363. readl(phba->HCregaddr); /* flush */
  364. /* Clear all pending interrupts */
  365. writel(0xffffffff, phba->HAregaddr);
  366. readl(phba->HAregaddr); /* flush */
  367. phba->hba_state = LPFC_HBA_ERROR;
  368. if (rc != MBX_BUSY)
  369. mempool_free(pmb, phba->mbox_mem_pool);
  370. return -EIO;
  371. }
  372. /* MBOX buffer will be freed in mbox compl */
  373. return (0);
  374. }
  375. static int
  376. lpfc_discovery_wait(struct lpfc_hba *phba)
  377. {
  378. int i = 0;
  379. while ((phba->hba_state != LPFC_HBA_READY) ||
  380. (phba->num_disc_nodes) || (phba->fc_prli_sent) ||
  381. ((phba->fc_map_cnt == 0) && (i<2)) ||
  382. (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE)) {
  383. /* Check every second for 30 retries. */
  384. i++;
  385. if (i > 30) {
  386. return -ETIMEDOUT;
  387. }
  388. if ((i >= 15) && (phba->hba_state <= LPFC_LINK_DOWN)) {
  389. /* The link is down. Set linkdown timeout */
  390. return -ETIMEDOUT;
  391. }
  392. /* Delay for 1 second to give discovery time to complete. */
  393. msleep(1000);
  394. }
  395. return 0;
  396. }
  397. /************************************************************************/
  398. /* */
  399. /* lpfc_hba_down_prep */
  400. /* This routine will do LPFC uninitialization before the */
  401. /* HBA is reset when bringing down the SLI Layer. This will be */
  402. /* initialized as a SLI layer callback routine. */
  403. /* This routine returns 0 on success. Any other return value */
  404. /* indicates an error. */
  405. /* */
  406. /************************************************************************/
  407. int
  408. lpfc_hba_down_prep(struct lpfc_hba * phba)
  409. {
  410. /* Disable interrupts */
  411. writel(0, phba->HCregaddr);
  412. readl(phba->HCregaddr); /* flush */
  413. /* Cleanup potential discovery resources */
  414. lpfc_els_flush_rscn(phba);
  415. lpfc_els_flush_cmd(phba);
  416. lpfc_disc_flush_list(phba);
  417. return (0);
  418. }
  419. /************************************************************************/
  420. /* */
  421. /* lpfc_hba_down_post */
  422. /* This routine will do uninitialization after the HBA is reset */
  423. /* when bringing down the SLI Layer. */
  424. /* This routine returns 0 on success. Any other return value */
  425. /* indicates an error. */
  426. /* */
  427. /************************************************************************/
  428. int
  429. lpfc_hba_down_post(struct lpfc_hba * phba)
  430. {
  431. struct lpfc_sli *psli = &phba->sli;
  432. struct lpfc_sli_ring *pring;
  433. struct lpfc_dmabuf *mp, *next_mp;
  434. int i;
  435. /* Cleanup preposted buffers on the ELS ring */
  436. pring = &psli->ring[LPFC_ELS_RING];
  437. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  438. list_del(&mp->list);
  439. pring->postbufq_cnt--;
  440. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  441. kfree(mp);
  442. }
  443. for (i = 0; i < psli->num_rings; i++) {
  444. pring = &psli->ring[i];
  445. lpfc_sli_abort_iocb_ring(phba, pring);
  446. }
  447. return 0;
  448. }
  449. /************************************************************************/
  450. /* */
  451. /* lpfc_handle_eratt */
  452. /* This routine will handle processing a Host Attention */
  453. /* Error Status event. This will be initialized */
  454. /* as a SLI layer callback routine. */
  455. /* */
  456. /************************************************************************/
  457. void
  458. lpfc_handle_eratt(struct lpfc_hba * phba)
  459. {
  460. struct lpfc_sli *psli = &phba->sli;
  461. struct lpfc_sli_ring *pring;
  462. uint32_t event_data;
  463. /* If the pci channel is offline, ignore possible errors,
  464. * since we cannot communicate with the pci card anyway. */
  465. if (pci_channel_offline(phba->pcidev))
  466. return;
  467. if (phba->work_hs & HS_FFER6 ||
  468. phba->work_hs & HS_FFER5) {
  469. /* Re-establishing Link */
  470. lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
  471. "%d:1301 Re-establishing Link "
  472. "Data: x%x x%x x%x\n",
  473. phba->brd_no, phba->work_hs,
  474. phba->work_status[0], phba->work_status[1]);
  475. spin_lock_irq(phba->host->host_lock);
  476. phba->fc_flag |= FC_ESTABLISH_LINK;
  477. psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
  478. spin_unlock_irq(phba->host->host_lock);
  479. /*
  480. * Firmware stops when it triggled erratt with HS_FFER6.
  481. * That could cause the I/Os dropped by the firmware.
  482. * Error iocb (I/O) on txcmplq and let the SCSI layer
  483. * retry it after re-establishing link.
  484. */
  485. pring = &psli->ring[psli->fcp_ring];
  486. lpfc_sli_abort_iocb_ring(phba, pring);
  487. /*
  488. * There was a firmware error. Take the hba offline and then
  489. * attempt to restart it.
  490. */
  491. lpfc_offline_prep(phba);
  492. lpfc_offline(phba);
  493. lpfc_sli_brdrestart(phba);
  494. if (lpfc_online(phba) == 0) { /* Initialize the HBA */
  495. mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
  496. lpfc_unblock_mgmt_io(phba);
  497. return;
  498. }
  499. lpfc_unblock_mgmt_io(phba);
  500. } else {
  501. /* The if clause above forces this code path when the status
  502. * failure is a value other than FFER6. Do not call the offline
  503. * twice. This is the adapter hardware error path.
  504. */
  505. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  506. "%d:0457 Adapter Hardware Error "
  507. "Data: x%x x%x x%x\n",
  508. phba->brd_no, phba->work_hs,
  509. phba->work_status[0], phba->work_status[1]);
  510. event_data = FC_REG_DUMP_EVENT;
  511. fc_host_post_vendor_event(phba->host, fc_get_event_number(),
  512. sizeof(event_data), (char *) &event_data,
  513. SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
  514. psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
  515. lpfc_offline_prep(phba);
  516. lpfc_offline(phba);
  517. lpfc_unblock_mgmt_io(phba);
  518. phba->hba_state = LPFC_HBA_ERROR;
  519. lpfc_hba_down_post(phba);
  520. }
  521. }
  522. /************************************************************************/
  523. /* */
  524. /* lpfc_handle_latt */
  525. /* This routine will handle processing a Host Attention */
  526. /* Link Status event. This will be initialized */
  527. /* as a SLI layer callback routine. */
  528. /* */
  529. /************************************************************************/
  530. void
  531. lpfc_handle_latt(struct lpfc_hba * phba)
  532. {
  533. struct lpfc_sli *psli = &phba->sli;
  534. LPFC_MBOXQ_t *pmb;
  535. volatile uint32_t control;
  536. struct lpfc_dmabuf *mp;
  537. int rc = -ENOMEM;
  538. pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  539. if (!pmb)
  540. goto lpfc_handle_latt_err_exit;
  541. mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  542. if (!mp)
  543. goto lpfc_handle_latt_free_pmb;
  544. mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
  545. if (!mp->virt)
  546. goto lpfc_handle_latt_free_mp;
  547. rc = -EIO;
  548. /* Cleanup any outstanding ELS commands */
  549. lpfc_els_flush_cmd(phba);
  550. psli->slistat.link_event++;
  551. lpfc_read_la(phba, pmb, mp);
  552. pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
  553. rc = lpfc_sli_issue_mbox (phba, pmb, (MBX_NOWAIT | MBX_STOP_IOCB));
  554. if (rc == MBX_NOT_FINISHED)
  555. goto lpfc_handle_latt_free_mbuf;
  556. /* Clear Link Attention in HA REG */
  557. spin_lock_irq(phba->host->host_lock);
  558. writel(HA_LATT, phba->HAregaddr);
  559. readl(phba->HAregaddr); /* flush */
  560. spin_unlock_irq(phba->host->host_lock);
  561. return;
  562. lpfc_handle_latt_free_mbuf:
  563. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  564. lpfc_handle_latt_free_mp:
  565. kfree(mp);
  566. lpfc_handle_latt_free_pmb:
  567. mempool_free(pmb, phba->mbox_mem_pool);
  568. lpfc_handle_latt_err_exit:
  569. /* Enable Link attention interrupts */
  570. spin_lock_irq(phba->host->host_lock);
  571. psli->sli_flag |= LPFC_PROCESS_LA;
  572. control = readl(phba->HCregaddr);
  573. control |= HC_LAINT_ENA;
  574. writel(control, phba->HCregaddr);
  575. readl(phba->HCregaddr); /* flush */
  576. /* Clear Link Attention in HA REG */
  577. writel(HA_LATT, phba->HAregaddr);
  578. readl(phba->HAregaddr); /* flush */
  579. spin_unlock_irq(phba->host->host_lock);
  580. lpfc_linkdown(phba);
  581. phba->hba_state = LPFC_HBA_ERROR;
  582. /* The other case is an error from issue_mbox */
  583. if (rc == -ENOMEM)
  584. lpfc_printf_log(phba,
  585. KERN_WARNING,
  586. LOG_MBOX,
  587. "%d:0300 READ_LA: no buffers\n",
  588. phba->brd_no);
  589. return;
  590. }
  591. /************************************************************************/
  592. /* */
  593. /* lpfc_parse_vpd */
  594. /* This routine will parse the VPD data */
  595. /* */
  596. /************************************************************************/
  597. static int
  598. lpfc_parse_vpd(struct lpfc_hba * phba, uint8_t * vpd, int len)
  599. {
  600. uint8_t lenlo, lenhi;
  601. int Length;
  602. int i, j;
  603. int finished = 0;
  604. int index = 0;
  605. if (!vpd)
  606. return 0;
  607. /* Vital Product */
  608. lpfc_printf_log(phba,
  609. KERN_INFO,
  610. LOG_INIT,
  611. "%d:0455 Vital Product Data: x%x x%x x%x x%x\n",
  612. phba->brd_no,
  613. (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
  614. (uint32_t) vpd[3]);
  615. while (!finished && (index < (len - 4))) {
  616. switch (vpd[index]) {
  617. case 0x82:
  618. case 0x91:
  619. index += 1;
  620. lenlo = vpd[index];
  621. index += 1;
  622. lenhi = vpd[index];
  623. index += 1;
  624. i = ((((unsigned short)lenhi) << 8) + lenlo);
  625. index += i;
  626. break;
  627. case 0x90:
  628. index += 1;
  629. lenlo = vpd[index];
  630. index += 1;
  631. lenhi = vpd[index];
  632. index += 1;
  633. Length = ((((unsigned short)lenhi) << 8) + lenlo);
  634. if (Length > len - index)
  635. Length = len - index;
  636. while (Length > 0) {
  637. /* Look for Serial Number */
  638. if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
  639. index += 2;
  640. i = vpd[index];
  641. index += 1;
  642. j = 0;
  643. Length -= (3+i);
  644. while(i--) {
  645. phba->SerialNumber[j++] = vpd[index++];
  646. if (j == 31)
  647. break;
  648. }
  649. phba->SerialNumber[j] = 0;
  650. continue;
  651. }
  652. else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
  653. phba->vpd_flag |= VPD_MODEL_DESC;
  654. index += 2;
  655. i = vpd[index];
  656. index += 1;
  657. j = 0;
  658. Length -= (3+i);
  659. while(i--) {
  660. phba->ModelDesc[j++] = vpd[index++];
  661. if (j == 255)
  662. break;
  663. }
  664. phba->ModelDesc[j] = 0;
  665. continue;
  666. }
  667. else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
  668. phba->vpd_flag |= VPD_MODEL_NAME;
  669. index += 2;
  670. i = vpd[index];
  671. index += 1;
  672. j = 0;
  673. Length -= (3+i);
  674. while(i--) {
  675. phba->ModelName[j++] = vpd[index++];
  676. if (j == 79)
  677. break;
  678. }
  679. phba->ModelName[j] = 0;
  680. continue;
  681. }
  682. else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
  683. phba->vpd_flag |= VPD_PROGRAM_TYPE;
  684. index += 2;
  685. i = vpd[index];
  686. index += 1;
  687. j = 0;
  688. Length -= (3+i);
  689. while(i--) {
  690. phba->ProgramType[j++] = vpd[index++];
  691. if (j == 255)
  692. break;
  693. }
  694. phba->ProgramType[j] = 0;
  695. continue;
  696. }
  697. else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
  698. phba->vpd_flag |= VPD_PORT;
  699. index += 2;
  700. i = vpd[index];
  701. index += 1;
  702. j = 0;
  703. Length -= (3+i);
  704. while(i--) {
  705. phba->Port[j++] = vpd[index++];
  706. if (j == 19)
  707. break;
  708. }
  709. phba->Port[j] = 0;
  710. continue;
  711. }
  712. else {
  713. index += 2;
  714. i = vpd[index];
  715. index += 1;
  716. index += i;
  717. Length -= (3 + i);
  718. }
  719. }
  720. finished = 0;
  721. break;
  722. case 0x78:
  723. finished = 1;
  724. break;
  725. default:
  726. index ++;
  727. break;
  728. }
  729. }
  730. return(1);
  731. }
  732. static void
  733. lpfc_get_hba_model_desc(struct lpfc_hba * phba, uint8_t * mdp, uint8_t * descp)
  734. {
  735. lpfc_vpd_t *vp;
  736. uint16_t dev_id = phba->pcidev->device;
  737. int max_speed;
  738. struct {
  739. char * name;
  740. int max_speed;
  741. char * bus;
  742. } m = {"<Unknown>", 0, ""};
  743. if (mdp && mdp[0] != '\0'
  744. && descp && descp[0] != '\0')
  745. return;
  746. if (phba->lmt & LMT_10Gb)
  747. max_speed = 10;
  748. else if (phba->lmt & LMT_8Gb)
  749. max_speed = 8;
  750. else if (phba->lmt & LMT_4Gb)
  751. max_speed = 4;
  752. else if (phba->lmt & LMT_2Gb)
  753. max_speed = 2;
  754. else
  755. max_speed = 1;
  756. vp = &phba->vpd;
  757. switch (dev_id) {
  758. case PCI_DEVICE_ID_FIREFLY:
  759. m = (typeof(m)){"LP6000", max_speed, "PCI"};
  760. break;
  761. case PCI_DEVICE_ID_SUPERFLY:
  762. if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
  763. m = (typeof(m)){"LP7000", max_speed, "PCI"};
  764. else
  765. m = (typeof(m)){"LP7000E", max_speed, "PCI"};
  766. break;
  767. case PCI_DEVICE_ID_DRAGONFLY:
  768. m = (typeof(m)){"LP8000", max_speed, "PCI"};
  769. break;
  770. case PCI_DEVICE_ID_CENTAUR:
  771. if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
  772. m = (typeof(m)){"LP9002", max_speed, "PCI"};
  773. else
  774. m = (typeof(m)){"LP9000", max_speed, "PCI"};
  775. break;
  776. case PCI_DEVICE_ID_RFLY:
  777. m = (typeof(m)){"LP952", max_speed, "PCI"};
  778. break;
  779. case PCI_DEVICE_ID_PEGASUS:
  780. m = (typeof(m)){"LP9802", max_speed, "PCI-X"};
  781. break;
  782. case PCI_DEVICE_ID_THOR:
  783. m = (typeof(m)){"LP10000", max_speed, "PCI-X"};
  784. break;
  785. case PCI_DEVICE_ID_VIPER:
  786. m = (typeof(m)){"LPX1000", max_speed, "PCI-X"};
  787. break;
  788. case PCI_DEVICE_ID_PFLY:
  789. m = (typeof(m)){"LP982", max_speed, "PCI-X"};
  790. break;
  791. case PCI_DEVICE_ID_TFLY:
  792. m = (typeof(m)){"LP1050", max_speed, "PCI-X"};
  793. break;
  794. case PCI_DEVICE_ID_HELIOS:
  795. m = (typeof(m)){"LP11000", max_speed, "PCI-X2"};
  796. break;
  797. case PCI_DEVICE_ID_HELIOS_SCSP:
  798. m = (typeof(m)){"LP11000-SP", max_speed, "PCI-X2"};
  799. break;
  800. case PCI_DEVICE_ID_HELIOS_DCSP:
  801. m = (typeof(m)){"LP11002-SP", max_speed, "PCI-X2"};
  802. break;
  803. case PCI_DEVICE_ID_NEPTUNE:
  804. m = (typeof(m)){"LPe1000", max_speed, "PCIe"};
  805. break;
  806. case PCI_DEVICE_ID_NEPTUNE_SCSP:
  807. m = (typeof(m)){"LPe1000-SP", max_speed, "PCIe"};
  808. break;
  809. case PCI_DEVICE_ID_NEPTUNE_DCSP:
  810. m = (typeof(m)){"LPe1002-SP", max_speed, "PCIe"};
  811. break;
  812. case PCI_DEVICE_ID_BMID:
  813. m = (typeof(m)){"LP1150", max_speed, "PCI-X2"};
  814. break;
  815. case PCI_DEVICE_ID_BSMB:
  816. m = (typeof(m)){"LP111", max_speed, "PCI-X2"};
  817. break;
  818. case PCI_DEVICE_ID_ZEPHYR:
  819. m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
  820. break;
  821. case PCI_DEVICE_ID_ZEPHYR_SCSP:
  822. m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
  823. break;
  824. case PCI_DEVICE_ID_ZEPHYR_DCSP:
  825. m = (typeof(m)){"LPe11002-SP", max_speed, "PCIe"};
  826. break;
  827. case PCI_DEVICE_ID_ZMID:
  828. m = (typeof(m)){"LPe1150", max_speed, "PCIe"};
  829. break;
  830. case PCI_DEVICE_ID_ZSMB:
  831. m = (typeof(m)){"LPe111", max_speed, "PCIe"};
  832. break;
  833. case PCI_DEVICE_ID_LP101:
  834. m = (typeof(m)){"LP101", max_speed, "PCI-X"};
  835. break;
  836. case PCI_DEVICE_ID_LP10000S:
  837. m = (typeof(m)){"LP10000-S", max_speed, "PCI"};
  838. break;
  839. case PCI_DEVICE_ID_LP11000S:
  840. m = (typeof(m)){"LP11000-S", max_speed,
  841. "PCI-X2"};
  842. break;
  843. case PCI_DEVICE_ID_LPE11000S:
  844. m = (typeof(m)){"LPe11000-S", max_speed,
  845. "PCIe"};
  846. break;
  847. default:
  848. m = (typeof(m)){ NULL };
  849. break;
  850. }
  851. if (mdp && mdp[0] == '\0')
  852. snprintf(mdp, 79,"%s", m.name);
  853. if (descp && descp[0] == '\0')
  854. snprintf(descp, 255,
  855. "Emulex %s %dGb %s Fibre Channel Adapter",
  856. m.name, m.max_speed, m.bus);
  857. }
  858. /**************************************************/
  859. /* lpfc_post_buffer */
  860. /* */
  861. /* This routine will post count buffers to the */
  862. /* ring with the QUE_RING_BUF_CN command. This */
  863. /* allows 3 buffers / command to be posted. */
  864. /* Returns the number of buffers NOT posted. */
  865. /**************************************************/
  866. int
  867. lpfc_post_buffer(struct lpfc_hba * phba, struct lpfc_sli_ring * pring, int cnt,
  868. int type)
  869. {
  870. IOCB_t *icmd;
  871. struct lpfc_iocbq *iocb;
  872. struct lpfc_dmabuf *mp1, *mp2;
  873. cnt += pring->missbufcnt;
  874. /* While there are buffers to post */
  875. while (cnt > 0) {
  876. /* Allocate buffer for command iocb */
  877. spin_lock_irq(phba->host->host_lock);
  878. iocb = lpfc_sli_get_iocbq(phba);
  879. spin_unlock_irq(phba->host->host_lock);
  880. if (iocb == NULL) {
  881. pring->missbufcnt = cnt;
  882. return cnt;
  883. }
  884. icmd = &iocb->iocb;
  885. /* 2 buffers can be posted per command */
  886. /* Allocate buffer to post */
  887. mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
  888. if (mp1)
  889. mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
  890. &mp1->phys);
  891. if (mp1 == 0 || mp1->virt == 0) {
  892. kfree(mp1);
  893. spin_lock_irq(phba->host->host_lock);
  894. lpfc_sli_release_iocbq(phba, iocb);
  895. spin_unlock_irq(phba->host->host_lock);
  896. pring->missbufcnt = cnt;
  897. return cnt;
  898. }
  899. INIT_LIST_HEAD(&mp1->list);
  900. /* Allocate buffer to post */
  901. if (cnt > 1) {
  902. mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
  903. if (mp2)
  904. mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
  905. &mp2->phys);
  906. if (mp2 == 0 || mp2->virt == 0) {
  907. kfree(mp2);
  908. lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
  909. kfree(mp1);
  910. spin_lock_irq(phba->host->host_lock);
  911. lpfc_sli_release_iocbq(phba, iocb);
  912. spin_unlock_irq(phba->host->host_lock);
  913. pring->missbufcnt = cnt;
  914. return cnt;
  915. }
  916. INIT_LIST_HEAD(&mp2->list);
  917. } else {
  918. mp2 = NULL;
  919. }
  920. icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
  921. icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
  922. icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
  923. icmd->ulpBdeCount = 1;
  924. cnt--;
  925. if (mp2) {
  926. icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
  927. icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
  928. icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
  929. cnt--;
  930. icmd->ulpBdeCount = 2;
  931. }
  932. icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
  933. icmd->ulpLe = 1;
  934. spin_lock_irq(phba->host->host_lock);
  935. if (lpfc_sli_issue_iocb(phba, pring, iocb, 0) == IOCB_ERROR) {
  936. lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
  937. kfree(mp1);
  938. cnt++;
  939. if (mp2) {
  940. lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
  941. kfree(mp2);
  942. cnt++;
  943. }
  944. lpfc_sli_release_iocbq(phba, iocb);
  945. pring->missbufcnt = cnt;
  946. spin_unlock_irq(phba->host->host_lock);
  947. return cnt;
  948. }
  949. spin_unlock_irq(phba->host->host_lock);
  950. lpfc_sli_ringpostbuf_put(phba, pring, mp1);
  951. if (mp2) {
  952. lpfc_sli_ringpostbuf_put(phba, pring, mp2);
  953. }
  954. }
  955. pring->missbufcnt = 0;
  956. return 0;
  957. }
  958. /************************************************************************/
  959. /* */
  960. /* lpfc_post_rcv_buf */
  961. /* This routine post initial rcv buffers to the configured rings */
  962. /* */
  963. /************************************************************************/
  964. static int
  965. lpfc_post_rcv_buf(struct lpfc_hba * phba)
  966. {
  967. struct lpfc_sli *psli = &phba->sli;
  968. /* Ring 0, ELS / CT buffers */
  969. lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0, 1);
  970. /* Ring 2 - FCP no buffers needed */
  971. return 0;
  972. }
  973. #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
  974. /************************************************************************/
  975. /* */
  976. /* lpfc_sha_init */
  977. /* */
  978. /************************************************************************/
  979. static void
  980. lpfc_sha_init(uint32_t * HashResultPointer)
  981. {
  982. HashResultPointer[0] = 0x67452301;
  983. HashResultPointer[1] = 0xEFCDAB89;
  984. HashResultPointer[2] = 0x98BADCFE;
  985. HashResultPointer[3] = 0x10325476;
  986. HashResultPointer[4] = 0xC3D2E1F0;
  987. }
  988. /************************************************************************/
  989. /* */
  990. /* lpfc_sha_iterate */
  991. /* */
  992. /************************************************************************/
  993. static void
  994. lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
  995. {
  996. int t;
  997. uint32_t TEMP;
  998. uint32_t A, B, C, D, E;
  999. t = 16;
  1000. do {
  1001. HashWorkingPointer[t] =
  1002. S(1,
  1003. HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
  1004. 8] ^
  1005. HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
  1006. } while (++t <= 79);
  1007. t = 0;
  1008. A = HashResultPointer[0];
  1009. B = HashResultPointer[1];
  1010. C = HashResultPointer[2];
  1011. D = HashResultPointer[3];
  1012. E = HashResultPointer[4];
  1013. do {
  1014. if (t < 20) {
  1015. TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
  1016. } else if (t < 40) {
  1017. TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
  1018. } else if (t < 60) {
  1019. TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
  1020. } else {
  1021. TEMP = (B ^ C ^ D) + 0xCA62C1D6;
  1022. }
  1023. TEMP += S(5, A) + E + HashWorkingPointer[t];
  1024. E = D;
  1025. D = C;
  1026. C = S(30, B);
  1027. B = A;
  1028. A = TEMP;
  1029. } while (++t <= 79);
  1030. HashResultPointer[0] += A;
  1031. HashResultPointer[1] += B;
  1032. HashResultPointer[2] += C;
  1033. HashResultPointer[3] += D;
  1034. HashResultPointer[4] += E;
  1035. }
  1036. /************************************************************************/
  1037. /* */
  1038. /* lpfc_challenge_key */
  1039. /* */
  1040. /************************************************************************/
  1041. static void
  1042. lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
  1043. {
  1044. *HashWorking = (*RandomChallenge ^ *HashWorking);
  1045. }
  1046. /************************************************************************/
  1047. /* */
  1048. /* lpfc_hba_init */
  1049. /* */
  1050. /************************************************************************/
  1051. void
  1052. lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
  1053. {
  1054. int t;
  1055. uint32_t *HashWorking;
  1056. uint32_t *pwwnn = phba->wwnn;
  1057. HashWorking = kmalloc(80 * sizeof(uint32_t), GFP_KERNEL);
  1058. if (!HashWorking)
  1059. return;
  1060. memset(HashWorking, 0, (80 * sizeof(uint32_t)));
  1061. HashWorking[0] = HashWorking[78] = *pwwnn++;
  1062. HashWorking[1] = HashWorking[79] = *pwwnn;
  1063. for (t = 0; t < 7; t++)
  1064. lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
  1065. lpfc_sha_init(hbainit);
  1066. lpfc_sha_iterate(hbainit, HashWorking);
  1067. kfree(HashWorking);
  1068. }
  1069. static void
  1070. lpfc_cleanup(struct lpfc_hba * phba)
  1071. {
  1072. struct lpfc_nodelist *ndlp, *next_ndlp;
  1073. /* clean up phba - lpfc specific */
  1074. lpfc_can_disctmo(phba);
  1075. list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_nodes, nlp_listp)
  1076. lpfc_nlp_put(ndlp);
  1077. INIT_LIST_HEAD(&phba->fc_nodes);
  1078. return;
  1079. }
  1080. static void
  1081. lpfc_establish_link_tmo(unsigned long ptr)
  1082. {
  1083. struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
  1084. unsigned long iflag;
  1085. /* Re-establishing Link, timer expired */
  1086. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  1087. "%d:1300 Re-establishing Link, timer expired "
  1088. "Data: x%x x%x\n",
  1089. phba->brd_no, phba->fc_flag, phba->hba_state);
  1090. spin_lock_irqsave(phba->host->host_lock, iflag);
  1091. phba->fc_flag &= ~FC_ESTABLISH_LINK;
  1092. spin_unlock_irqrestore(phba->host->host_lock, iflag);
  1093. }
  1094. static int
  1095. lpfc_stop_timer(struct lpfc_hba * phba)
  1096. {
  1097. struct lpfc_sli *psli = &phba->sli;
  1098. del_timer_sync(&phba->fcp_poll_timer);
  1099. del_timer_sync(&phba->fc_estabtmo);
  1100. del_timer_sync(&phba->fc_disctmo);
  1101. del_timer_sync(&phba->fc_fdmitmo);
  1102. del_timer_sync(&phba->els_tmofunc);
  1103. psli = &phba->sli;
  1104. del_timer_sync(&psli->mbox_tmo);
  1105. return(1);
  1106. }
  1107. int
  1108. lpfc_online(struct lpfc_hba * phba)
  1109. {
  1110. if (!phba)
  1111. return 0;
  1112. if (!(phba->fc_flag & FC_OFFLINE_MODE))
  1113. return 0;
  1114. lpfc_printf_log(phba,
  1115. KERN_WARNING,
  1116. LOG_INIT,
  1117. "%d:0458 Bring Adapter online\n",
  1118. phba->brd_no);
  1119. lpfc_block_mgmt_io(phba);
  1120. if (!lpfc_sli_queue_setup(phba)) {
  1121. lpfc_unblock_mgmt_io(phba);
  1122. return 1;
  1123. }
  1124. if (lpfc_sli_hba_setup(phba)) { /* Initialize the HBA */
  1125. lpfc_unblock_mgmt_io(phba);
  1126. return 1;
  1127. }
  1128. spin_lock_irq(phba->host->host_lock);
  1129. phba->fc_flag &= ~FC_OFFLINE_MODE;
  1130. spin_unlock_irq(phba->host->host_lock);
  1131. lpfc_unblock_mgmt_io(phba);
  1132. return 0;
  1133. }
  1134. void
  1135. lpfc_block_mgmt_io(struct lpfc_hba * phba)
  1136. {
  1137. unsigned long iflag;
  1138. spin_lock_irqsave(phba->host->host_lock, iflag);
  1139. phba->fc_flag |= FC_BLOCK_MGMT_IO;
  1140. spin_unlock_irqrestore(phba->host->host_lock, iflag);
  1141. }
  1142. void
  1143. lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
  1144. {
  1145. unsigned long iflag;
  1146. spin_lock_irqsave(phba->host->host_lock, iflag);
  1147. phba->fc_flag &= ~FC_BLOCK_MGMT_IO;
  1148. spin_unlock_irqrestore(phba->host->host_lock, iflag);
  1149. }
  1150. void
  1151. lpfc_offline_prep(struct lpfc_hba * phba)
  1152. {
  1153. struct lpfc_nodelist *ndlp, *next_ndlp;
  1154. if (phba->fc_flag & FC_OFFLINE_MODE)
  1155. return;
  1156. lpfc_block_mgmt_io(phba);
  1157. lpfc_linkdown(phba);
  1158. /* Issue an unreg_login to all nodes */
  1159. list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_nodes, nlp_listp)
  1160. if (ndlp->nlp_state != NLP_STE_UNUSED_NODE)
  1161. lpfc_unreg_rpi(phba, ndlp);
  1162. lpfc_sli_flush_mbox_queue(phba);
  1163. }
  1164. void
  1165. lpfc_offline(struct lpfc_hba * phba)
  1166. {
  1167. unsigned long iflag;
  1168. if (phba->fc_flag & FC_OFFLINE_MODE)
  1169. return;
  1170. /* stop all timers associated with this hba */
  1171. lpfc_stop_timer(phba);
  1172. lpfc_printf_log(phba,
  1173. KERN_WARNING,
  1174. LOG_INIT,
  1175. "%d:0460 Bring Adapter offline\n",
  1176. phba->brd_no);
  1177. /* Bring down the SLI Layer and cleanup. The HBA is offline
  1178. now. */
  1179. lpfc_sli_hba_down(phba);
  1180. lpfc_cleanup(phba);
  1181. spin_lock_irqsave(phba->host->host_lock, iflag);
  1182. phba->work_hba_events = 0;
  1183. phba->work_ha = 0;
  1184. phba->fc_flag |= FC_OFFLINE_MODE;
  1185. spin_unlock_irqrestore(phba->host->host_lock, iflag);
  1186. }
  1187. /******************************************************************************
  1188. * Function name: lpfc_scsi_free
  1189. *
  1190. * Description: Called from lpfc_pci_remove_one free internal driver resources
  1191. *
  1192. ******************************************************************************/
  1193. static int
  1194. lpfc_scsi_free(struct lpfc_hba * phba)
  1195. {
  1196. struct lpfc_scsi_buf *sb, *sb_next;
  1197. struct lpfc_iocbq *io, *io_next;
  1198. spin_lock_irq(phba->host->host_lock);
  1199. /* Release all the lpfc_scsi_bufs maintained by this host. */
  1200. list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
  1201. list_del(&sb->list);
  1202. pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
  1203. sb->dma_handle);
  1204. kfree(sb);
  1205. phba->total_scsi_bufs--;
  1206. }
  1207. /* Release all the lpfc_iocbq entries maintained by this host. */
  1208. list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
  1209. list_del(&io->list);
  1210. kfree(io);
  1211. phba->total_iocbq_bufs--;
  1212. }
  1213. spin_unlock_irq(phba->host->host_lock);
  1214. return 0;
  1215. }
  1216. static int __devinit
  1217. lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
  1218. {
  1219. struct Scsi_Host *host;
  1220. struct lpfc_hba *phba;
  1221. struct lpfc_sli *psli;
  1222. struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
  1223. unsigned long bar0map_len, bar2map_len;
  1224. int error = -ENODEV, retval;
  1225. int i;
  1226. uint16_t iotag;
  1227. if (pci_enable_device(pdev))
  1228. goto out;
  1229. if (pci_request_regions(pdev, LPFC_DRIVER_NAME))
  1230. goto out_disable_device;
  1231. host = scsi_host_alloc(&lpfc_template, sizeof (struct lpfc_hba));
  1232. if (!host)
  1233. goto out_release_regions;
  1234. phba = (struct lpfc_hba*)host->hostdata;
  1235. memset(phba, 0, sizeof (struct lpfc_hba));
  1236. phba->host = host;
  1237. phba->fc_flag |= FC_LOADING;
  1238. phba->pcidev = pdev;
  1239. /* Assign an unused board number */
  1240. if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
  1241. goto out_put_host;
  1242. error = idr_get_new(&lpfc_hba_index, NULL, &phba->brd_no);
  1243. if (error)
  1244. goto out_put_host;
  1245. host->unique_id = phba->brd_no;
  1246. /* Initialize timers used by driver */
  1247. init_timer(&phba->fc_estabtmo);
  1248. phba->fc_estabtmo.function = lpfc_establish_link_tmo;
  1249. phba->fc_estabtmo.data = (unsigned long)phba;
  1250. init_timer(&phba->fc_disctmo);
  1251. phba->fc_disctmo.function = lpfc_disc_timeout;
  1252. phba->fc_disctmo.data = (unsigned long)phba;
  1253. init_timer(&phba->fc_fdmitmo);
  1254. phba->fc_fdmitmo.function = lpfc_fdmi_tmo;
  1255. phba->fc_fdmitmo.data = (unsigned long)phba;
  1256. init_timer(&phba->els_tmofunc);
  1257. phba->els_tmofunc.function = lpfc_els_timeout;
  1258. phba->els_tmofunc.data = (unsigned long)phba;
  1259. psli = &phba->sli;
  1260. init_timer(&psli->mbox_tmo);
  1261. psli->mbox_tmo.function = lpfc_mbox_timeout;
  1262. psli->mbox_tmo.data = (unsigned long)phba;
  1263. init_timer(&phba->fcp_poll_timer);
  1264. phba->fcp_poll_timer.function = lpfc_poll_timeout;
  1265. phba->fcp_poll_timer.data = (unsigned long)phba;
  1266. /*
  1267. * Get all the module params for configuring this host and then
  1268. * establish the host parameters.
  1269. */
  1270. lpfc_get_cfgparam(phba);
  1271. host->max_id = LPFC_MAX_TARGET;
  1272. host->max_lun = phba->cfg_max_luns;
  1273. host->this_id = -1;
  1274. INIT_LIST_HEAD(&phba->fc_nodes);
  1275. pci_set_master(pdev);
  1276. retval = pci_set_mwi(pdev);
  1277. if (retval)
  1278. dev_printk(KERN_WARNING, &pdev->dev,
  1279. "Warning: pci_set_mwi returned %d\n", retval);
  1280. if (pci_set_dma_mask(phba->pcidev, DMA_64BIT_MASK) != 0)
  1281. if (pci_set_dma_mask(phba->pcidev, DMA_32BIT_MASK) != 0)
  1282. goto out_idr_remove;
  1283. /*
  1284. * Get the bus address of Bar0 and Bar2 and the number of bytes
  1285. * required by each mapping.
  1286. */
  1287. phba->pci_bar0_map = pci_resource_start(phba->pcidev, 0);
  1288. bar0map_len = pci_resource_len(phba->pcidev, 0);
  1289. phba->pci_bar2_map = pci_resource_start(phba->pcidev, 2);
  1290. bar2map_len = pci_resource_len(phba->pcidev, 2);
  1291. /* Map HBA SLIM to a kernel virtual address. */
  1292. phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
  1293. if (!phba->slim_memmap_p) {
  1294. error = -ENODEV;
  1295. dev_printk(KERN_ERR, &pdev->dev,
  1296. "ioremap failed for SLIM memory.\n");
  1297. goto out_idr_remove;
  1298. }
  1299. /* Map HBA Control Registers to a kernel virtual address. */
  1300. phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
  1301. if (!phba->ctrl_regs_memmap_p) {
  1302. error = -ENODEV;
  1303. dev_printk(KERN_ERR, &pdev->dev,
  1304. "ioremap failed for HBA control registers.\n");
  1305. goto out_iounmap_slim;
  1306. }
  1307. /* Allocate memory for SLI-2 structures */
  1308. phba->slim2p = dma_alloc_coherent(&phba->pcidev->dev, SLI2_SLIM_SIZE,
  1309. &phba->slim2p_mapping, GFP_KERNEL);
  1310. if (!phba->slim2p)
  1311. goto out_iounmap;
  1312. memset(phba->slim2p, 0, SLI2_SLIM_SIZE);
  1313. /* Initialize the SLI Layer to run with lpfc HBAs. */
  1314. lpfc_sli_setup(phba);
  1315. lpfc_sli_queue_setup(phba);
  1316. error = lpfc_mem_alloc(phba);
  1317. if (error)
  1318. goto out_free_slim;
  1319. /* Initialize and populate the iocb list per host. */
  1320. INIT_LIST_HEAD(&phba->lpfc_iocb_list);
  1321. for (i = 0; i < LPFC_IOCB_LIST_CNT; i++) {
  1322. iocbq_entry = kmalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
  1323. if (iocbq_entry == NULL) {
  1324. printk(KERN_ERR "%s: only allocated %d iocbs of "
  1325. "expected %d count. Unloading driver.\n",
  1326. __FUNCTION__, i, LPFC_IOCB_LIST_CNT);
  1327. error = -ENOMEM;
  1328. goto out_free_iocbq;
  1329. }
  1330. memset(iocbq_entry, 0, sizeof(struct lpfc_iocbq));
  1331. iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
  1332. if (iotag == 0) {
  1333. kfree (iocbq_entry);
  1334. printk(KERN_ERR "%s: failed to allocate IOTAG. "
  1335. "Unloading driver.\n",
  1336. __FUNCTION__);
  1337. error = -ENOMEM;
  1338. goto out_free_iocbq;
  1339. }
  1340. spin_lock_irq(phba->host->host_lock);
  1341. list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
  1342. phba->total_iocbq_bufs++;
  1343. spin_unlock_irq(phba->host->host_lock);
  1344. }
  1345. /* Initialize HBA structure */
  1346. phba->fc_edtov = FF_DEF_EDTOV;
  1347. phba->fc_ratov = FF_DEF_RATOV;
  1348. phba->fc_altov = FF_DEF_ALTOV;
  1349. phba->fc_arbtov = FF_DEF_ARBTOV;
  1350. INIT_LIST_HEAD(&phba->work_list);
  1351. phba->work_ha_mask = (HA_ERATT|HA_MBATT|HA_LATT);
  1352. phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
  1353. /* Startup the kernel thread for this host adapter. */
  1354. phba->worker_thread = kthread_run(lpfc_do_work, phba,
  1355. "lpfc_worker_%d", phba->brd_no);
  1356. if (IS_ERR(phba->worker_thread)) {
  1357. error = PTR_ERR(phba->worker_thread);
  1358. goto out_free_iocbq;
  1359. }
  1360. /*
  1361. * Set initial can_queue value since 0 is no longer supported and
  1362. * scsi_add_host will fail. This will be adjusted later based on the
  1363. * max xri value determined in hba setup.
  1364. */
  1365. host->can_queue = phba->cfg_hba_queue_depth - 10;
  1366. /* Tell the midlayer we support 16 byte commands */
  1367. host->max_cmd_len = 16;
  1368. /* Initialize the list of scsi buffers used by driver for scsi IO. */
  1369. spin_lock_init(&phba->scsi_buf_list_lock);
  1370. INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
  1371. host->transportt = lpfc_transport_template;
  1372. pci_set_drvdata(pdev, host);
  1373. error = scsi_add_host(host, &pdev->dev);
  1374. if (error)
  1375. goto out_kthread_stop;
  1376. error = lpfc_alloc_sysfs_attr(phba);
  1377. if (error)
  1378. goto out_remove_host;
  1379. if (phba->cfg_use_msi) {
  1380. error = pci_enable_msi(phba->pcidev);
  1381. if (error)
  1382. lpfc_printf_log(phba, KERN_INFO, LOG_INIT, "%d:0452 "
  1383. "Enable MSI failed, continuing with "
  1384. "IRQ\n", phba->brd_no);
  1385. }
  1386. error = request_irq(phba->pcidev->irq, lpfc_intr_handler, IRQF_SHARED,
  1387. LPFC_DRIVER_NAME, phba);
  1388. if (error) {
  1389. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1390. "%d:0451 Enable interrupt handler failed\n",
  1391. phba->brd_no);
  1392. goto out_free_sysfs_attr;
  1393. }
  1394. phba->MBslimaddr = phba->slim_memmap_p;
  1395. phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
  1396. phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
  1397. phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
  1398. phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
  1399. error = lpfc_sli_hba_setup(phba);
  1400. if (error) {
  1401. error = -ENODEV;
  1402. goto out_free_irq;
  1403. }
  1404. /*
  1405. * hba setup may have changed the hba_queue_depth so we need to adjust
  1406. * the value of can_queue.
  1407. */
  1408. host->can_queue = phba->cfg_hba_queue_depth - 10;
  1409. lpfc_discovery_wait(phba);
  1410. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  1411. spin_lock_irq(phba->host->host_lock);
  1412. lpfc_poll_start_timer(phba);
  1413. spin_unlock_irq(phba->host->host_lock);
  1414. }
  1415. /*
  1416. * set fixed host attributes
  1417. * Must done after lpfc_sli_hba_setup()
  1418. */
  1419. fc_host_node_name(host) = wwn_to_u64(phba->fc_nodename.u.wwn);
  1420. fc_host_port_name(host) = wwn_to_u64(phba->fc_portname.u.wwn);
  1421. fc_host_supported_classes(host) = FC_COS_CLASS3;
  1422. memset(fc_host_supported_fc4s(host), 0,
  1423. sizeof(fc_host_supported_fc4s(host)));
  1424. fc_host_supported_fc4s(host)[2] = 1;
  1425. fc_host_supported_fc4s(host)[7] = 1;
  1426. lpfc_get_hba_sym_node_name(phba, fc_host_symbolic_name(host));
  1427. fc_host_supported_speeds(host) = 0;
  1428. if (phba->lmt & LMT_10Gb)
  1429. fc_host_supported_speeds(host) |= FC_PORTSPEED_10GBIT;
  1430. if (phba->lmt & LMT_4Gb)
  1431. fc_host_supported_speeds(host) |= FC_PORTSPEED_4GBIT;
  1432. if (phba->lmt & LMT_2Gb)
  1433. fc_host_supported_speeds(host) |= FC_PORTSPEED_2GBIT;
  1434. if (phba->lmt & LMT_1Gb)
  1435. fc_host_supported_speeds(host) |= FC_PORTSPEED_1GBIT;
  1436. fc_host_maxframe_size(host) =
  1437. ((((uint32_t) phba->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
  1438. (uint32_t) phba->fc_sparam.cmn.bbRcvSizeLsb);
  1439. /* This value is also unchanging */
  1440. memset(fc_host_active_fc4s(host), 0,
  1441. sizeof(fc_host_active_fc4s(host)));
  1442. fc_host_active_fc4s(host)[2] = 1;
  1443. fc_host_active_fc4s(host)[7] = 1;
  1444. spin_lock_irq(phba->host->host_lock);
  1445. phba->fc_flag &= ~FC_LOADING;
  1446. spin_unlock_irq(phba->host->host_lock);
  1447. return 0;
  1448. out_free_irq:
  1449. lpfc_stop_timer(phba);
  1450. phba->work_hba_events = 0;
  1451. free_irq(phba->pcidev->irq, phba);
  1452. pci_disable_msi(phba->pcidev);
  1453. out_free_sysfs_attr:
  1454. lpfc_free_sysfs_attr(phba);
  1455. out_remove_host:
  1456. fc_remove_host(phba->host);
  1457. scsi_remove_host(phba->host);
  1458. out_kthread_stop:
  1459. kthread_stop(phba->worker_thread);
  1460. out_free_iocbq:
  1461. list_for_each_entry_safe(iocbq_entry, iocbq_next,
  1462. &phba->lpfc_iocb_list, list) {
  1463. spin_lock_irq(phba->host->host_lock);
  1464. kfree(iocbq_entry);
  1465. phba->total_iocbq_bufs--;
  1466. spin_unlock_irq(phba->host->host_lock);
  1467. }
  1468. lpfc_mem_free(phba);
  1469. out_free_slim:
  1470. dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE, phba->slim2p,
  1471. phba->slim2p_mapping);
  1472. out_iounmap:
  1473. iounmap(phba->ctrl_regs_memmap_p);
  1474. out_iounmap_slim:
  1475. iounmap(phba->slim_memmap_p);
  1476. out_idr_remove:
  1477. idr_remove(&lpfc_hba_index, phba->brd_no);
  1478. out_put_host:
  1479. phba->host = NULL;
  1480. scsi_host_put(host);
  1481. out_release_regions:
  1482. pci_release_regions(pdev);
  1483. out_disable_device:
  1484. pci_disable_device(pdev);
  1485. out:
  1486. pci_set_drvdata(pdev, NULL);
  1487. return error;
  1488. }
  1489. static void __devexit
  1490. lpfc_pci_remove_one(struct pci_dev *pdev)
  1491. {
  1492. struct Scsi_Host *host = pci_get_drvdata(pdev);
  1493. struct lpfc_hba *phba = (struct lpfc_hba *)host->hostdata;
  1494. unsigned long iflag;
  1495. lpfc_free_sysfs_attr(phba);
  1496. spin_lock_irqsave(phba->host->host_lock, iflag);
  1497. phba->fc_flag |= FC_UNLOADING;
  1498. spin_unlock_irqrestore(phba->host->host_lock, iflag);
  1499. fc_remove_host(phba->host);
  1500. scsi_remove_host(phba->host);
  1501. kthread_stop(phba->worker_thread);
  1502. /*
  1503. * Bring down the SLI Layer. This step disable all interrupts,
  1504. * clears the rings, discards all mailbox commands, and resets
  1505. * the HBA.
  1506. */
  1507. lpfc_sli_hba_down(phba);
  1508. lpfc_sli_brdrestart(phba);
  1509. /* Release the irq reservation */
  1510. free_irq(phba->pcidev->irq, phba);
  1511. pci_disable_msi(phba->pcidev);
  1512. lpfc_cleanup(phba);
  1513. lpfc_stop_timer(phba);
  1514. phba->work_hba_events = 0;
  1515. /*
  1516. * Call scsi_free before mem_free since scsi bufs are released to their
  1517. * corresponding pools here.
  1518. */
  1519. lpfc_scsi_free(phba);
  1520. lpfc_mem_free(phba);
  1521. /* Free resources associated with SLI2 interface */
  1522. dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
  1523. phba->slim2p, phba->slim2p_mapping);
  1524. /* unmap adapter SLIM and Control Registers */
  1525. iounmap(phba->ctrl_regs_memmap_p);
  1526. iounmap(phba->slim_memmap_p);
  1527. pci_release_regions(phba->pcidev);
  1528. pci_disable_device(phba->pcidev);
  1529. idr_remove(&lpfc_hba_index, phba->brd_no);
  1530. scsi_host_put(phba->host);
  1531. pci_set_drvdata(pdev, NULL);
  1532. }
  1533. /**
  1534. * lpfc_io_error_detected - called when PCI error is detected
  1535. * @pdev: Pointer to PCI device
  1536. * @state: The current pci conneection state
  1537. *
  1538. * This function is called after a PCI bus error affecting
  1539. * this device has been detected.
  1540. */
  1541. static pci_ers_result_t lpfc_io_error_detected(struct pci_dev *pdev,
  1542. pci_channel_state_t state)
  1543. {
  1544. struct Scsi_Host *host = pci_get_drvdata(pdev);
  1545. struct lpfc_hba *phba = (struct lpfc_hba *)host->hostdata;
  1546. struct lpfc_sli *psli = &phba->sli;
  1547. struct lpfc_sli_ring *pring;
  1548. if (state == pci_channel_io_perm_failure)
  1549. return PCI_ERS_RESULT_DISCONNECT;
  1550. pci_disable_device(pdev);
  1551. /*
  1552. * There may be I/Os dropped by the firmware.
  1553. * Error iocb (I/O) on txcmplq and let the SCSI layer
  1554. * retry it after re-establishing link.
  1555. */
  1556. pring = &psli->ring[psli->fcp_ring];
  1557. lpfc_sli_abort_iocb_ring(phba, pring);
  1558. /* Request a slot reset. */
  1559. return PCI_ERS_RESULT_NEED_RESET;
  1560. }
  1561. /**
  1562. * lpfc_io_slot_reset - called after the pci bus has been reset.
  1563. * @pdev: Pointer to PCI device
  1564. *
  1565. * Restart the card from scratch, as if from a cold-boot.
  1566. */
  1567. static pci_ers_result_t lpfc_io_slot_reset(struct pci_dev *pdev)
  1568. {
  1569. struct Scsi_Host *host = pci_get_drvdata(pdev);
  1570. struct lpfc_hba *phba = (struct lpfc_hba *)host->hostdata;
  1571. struct lpfc_sli *psli = &phba->sli;
  1572. int bars = pci_select_bars(pdev, IORESOURCE_MEM);
  1573. dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
  1574. if (pci_enable_device_bars(pdev, bars)) {
  1575. printk(KERN_ERR "lpfc: Cannot re-enable "
  1576. "PCI device after reset.\n");
  1577. return PCI_ERS_RESULT_DISCONNECT;
  1578. }
  1579. pci_set_master(pdev);
  1580. /* Re-establishing Link */
  1581. spin_lock_irq(phba->host->host_lock);
  1582. phba->fc_flag |= FC_ESTABLISH_LINK;
  1583. psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
  1584. spin_unlock_irq(phba->host->host_lock);
  1585. /* Take device offline; this will perform cleanup */
  1586. lpfc_offline(phba);
  1587. lpfc_sli_brdrestart(phba);
  1588. return PCI_ERS_RESULT_RECOVERED;
  1589. }
  1590. /**
  1591. * lpfc_io_resume - called when traffic can start flowing again.
  1592. * @pdev: Pointer to PCI device
  1593. *
  1594. * This callback is called when the error recovery driver tells us that
  1595. * its OK to resume normal operation.
  1596. */
  1597. static void lpfc_io_resume(struct pci_dev *pdev)
  1598. {
  1599. struct Scsi_Host *host = pci_get_drvdata(pdev);
  1600. struct lpfc_hba *phba = (struct lpfc_hba *)host->hostdata;
  1601. if (lpfc_online(phba) == 0) {
  1602. mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
  1603. }
  1604. }
  1605. static struct pci_device_id lpfc_id_table[] = {
  1606. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
  1607. PCI_ANY_ID, PCI_ANY_ID, },
  1608. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
  1609. PCI_ANY_ID, PCI_ANY_ID, },
  1610. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
  1611. PCI_ANY_ID, PCI_ANY_ID, },
  1612. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
  1613. PCI_ANY_ID, PCI_ANY_ID, },
  1614. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
  1615. PCI_ANY_ID, PCI_ANY_ID, },
  1616. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
  1617. PCI_ANY_ID, PCI_ANY_ID, },
  1618. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
  1619. PCI_ANY_ID, PCI_ANY_ID, },
  1620. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
  1621. PCI_ANY_ID, PCI_ANY_ID, },
  1622. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
  1623. PCI_ANY_ID, PCI_ANY_ID, },
  1624. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
  1625. PCI_ANY_ID, PCI_ANY_ID, },
  1626. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
  1627. PCI_ANY_ID, PCI_ANY_ID, },
  1628. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
  1629. PCI_ANY_ID, PCI_ANY_ID, },
  1630. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
  1631. PCI_ANY_ID, PCI_ANY_ID, },
  1632. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
  1633. PCI_ANY_ID, PCI_ANY_ID, },
  1634. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
  1635. PCI_ANY_ID, PCI_ANY_ID, },
  1636. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
  1637. PCI_ANY_ID, PCI_ANY_ID, },
  1638. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
  1639. PCI_ANY_ID, PCI_ANY_ID, },
  1640. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
  1641. PCI_ANY_ID, PCI_ANY_ID, },
  1642. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
  1643. PCI_ANY_ID, PCI_ANY_ID, },
  1644. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
  1645. PCI_ANY_ID, PCI_ANY_ID, },
  1646. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
  1647. PCI_ANY_ID, PCI_ANY_ID, },
  1648. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
  1649. PCI_ANY_ID, PCI_ANY_ID, },
  1650. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
  1651. PCI_ANY_ID, PCI_ANY_ID, },
  1652. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
  1653. PCI_ANY_ID, PCI_ANY_ID, },
  1654. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
  1655. PCI_ANY_ID, PCI_ANY_ID, },
  1656. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
  1657. PCI_ANY_ID, PCI_ANY_ID, },
  1658. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
  1659. PCI_ANY_ID, PCI_ANY_ID, },
  1660. { 0 }
  1661. };
  1662. MODULE_DEVICE_TABLE(pci, lpfc_id_table);
  1663. static struct pci_error_handlers lpfc_err_handler = {
  1664. .error_detected = lpfc_io_error_detected,
  1665. .slot_reset = lpfc_io_slot_reset,
  1666. .resume = lpfc_io_resume,
  1667. };
  1668. static struct pci_driver lpfc_driver = {
  1669. .name = LPFC_DRIVER_NAME,
  1670. .id_table = lpfc_id_table,
  1671. .probe = lpfc_pci_probe_one,
  1672. .remove = __devexit_p(lpfc_pci_remove_one),
  1673. .err_handler = &lpfc_err_handler,
  1674. };
  1675. static int __init
  1676. lpfc_init(void)
  1677. {
  1678. int error = 0;
  1679. printk(LPFC_MODULE_DESC "\n");
  1680. printk(LPFC_COPYRIGHT "\n");
  1681. lpfc_transport_template =
  1682. fc_attach_transport(&lpfc_transport_functions);
  1683. if (!lpfc_transport_template)
  1684. return -ENOMEM;
  1685. error = pci_register_driver(&lpfc_driver);
  1686. if (error)
  1687. fc_release_transport(lpfc_transport_template);
  1688. return error;
  1689. }
  1690. static void __exit
  1691. lpfc_exit(void)
  1692. {
  1693. pci_unregister_driver(&lpfc_driver);
  1694. fc_release_transport(lpfc_transport_template);
  1695. }
  1696. module_init(lpfc_init);
  1697. module_exit(lpfc_exit);
  1698. MODULE_LICENSE("GPL");
  1699. MODULE_DESCRIPTION(LPFC_MODULE_DESC);
  1700. MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
  1701. MODULE_VERSION("0:" LPFC_DRIVER_VERSION);