lpfc_init.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2007 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. /************************************************************************/
  376. /* */
  377. /* lpfc_hba_down_prep */
  378. /* This routine will do LPFC uninitialization before the */
  379. /* HBA is reset when bringing down the SLI Layer. This will be */
  380. /* initialized as a SLI layer callback routine. */
  381. /* This routine returns 0 on success. Any other return value */
  382. /* indicates an error. */
  383. /* */
  384. /************************************************************************/
  385. int
  386. lpfc_hba_down_prep(struct lpfc_hba * phba)
  387. {
  388. /* Disable interrupts */
  389. writel(0, phba->HCregaddr);
  390. readl(phba->HCregaddr); /* flush */
  391. /* Cleanup potential discovery resources */
  392. lpfc_els_flush_rscn(phba);
  393. lpfc_els_flush_cmd(phba);
  394. lpfc_disc_flush_list(phba);
  395. return (0);
  396. }
  397. /************************************************************************/
  398. /* */
  399. /* lpfc_hba_down_post */
  400. /* This routine will do uninitialization after the HBA is reset */
  401. /* when bringing down the SLI Layer. */
  402. /* This routine returns 0 on success. Any other return value */
  403. /* indicates an error. */
  404. /* */
  405. /************************************************************************/
  406. int
  407. lpfc_hba_down_post(struct lpfc_hba * phba)
  408. {
  409. struct lpfc_sli *psli = &phba->sli;
  410. struct lpfc_sli_ring *pring;
  411. struct lpfc_dmabuf *mp, *next_mp;
  412. int i;
  413. /* Cleanup preposted buffers on the ELS ring */
  414. pring = &psli->ring[LPFC_ELS_RING];
  415. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  416. list_del(&mp->list);
  417. pring->postbufq_cnt--;
  418. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  419. kfree(mp);
  420. }
  421. for (i = 0; i < psli->num_rings; i++) {
  422. pring = &psli->ring[i];
  423. lpfc_sli_abort_iocb_ring(phba, pring);
  424. }
  425. return 0;
  426. }
  427. /************************************************************************/
  428. /* */
  429. /* lpfc_handle_eratt */
  430. /* This routine will handle processing a Host Attention */
  431. /* Error Status event. This will be initialized */
  432. /* as a SLI layer callback routine. */
  433. /* */
  434. /************************************************************************/
  435. void
  436. lpfc_handle_eratt(struct lpfc_hba * phba)
  437. {
  438. struct lpfc_sli *psli = &phba->sli;
  439. struct lpfc_sli_ring *pring;
  440. uint32_t event_data;
  441. /* If the pci channel is offline, ignore possible errors,
  442. * since we cannot communicate with the pci card anyway. */
  443. if (pci_channel_offline(phba->pcidev))
  444. return;
  445. if (phba->work_hs & HS_FFER6 ||
  446. phba->work_hs & HS_FFER5) {
  447. /* Re-establishing Link */
  448. lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
  449. "%d:1301 Re-establishing Link "
  450. "Data: x%x x%x x%x\n",
  451. phba->brd_no, phba->work_hs,
  452. phba->work_status[0], phba->work_status[1]);
  453. spin_lock_irq(phba->host->host_lock);
  454. phba->fc_flag |= FC_ESTABLISH_LINK;
  455. psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
  456. spin_unlock_irq(phba->host->host_lock);
  457. /*
  458. * Firmware stops when it triggled erratt with HS_FFER6.
  459. * That could cause the I/Os dropped by the firmware.
  460. * Error iocb (I/O) on txcmplq and let the SCSI layer
  461. * retry it after re-establishing link.
  462. */
  463. pring = &psli->ring[psli->fcp_ring];
  464. lpfc_sli_abort_iocb_ring(phba, pring);
  465. /*
  466. * There was a firmware error. Take the hba offline and then
  467. * attempt to restart it.
  468. */
  469. lpfc_offline_prep(phba);
  470. lpfc_offline(phba);
  471. lpfc_sli_brdrestart(phba);
  472. if (lpfc_online(phba) == 0) { /* Initialize the HBA */
  473. mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
  474. lpfc_unblock_mgmt_io(phba);
  475. return;
  476. }
  477. lpfc_unblock_mgmt_io(phba);
  478. } else {
  479. /* The if clause above forces this code path when the status
  480. * failure is a value other than FFER6. Do not call the offline
  481. * twice. This is the adapter hardware error path.
  482. */
  483. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  484. "%d:0457 Adapter Hardware Error "
  485. "Data: x%x x%x x%x\n",
  486. phba->brd_no, phba->work_hs,
  487. phba->work_status[0], phba->work_status[1]);
  488. event_data = FC_REG_DUMP_EVENT;
  489. fc_host_post_vendor_event(phba->host, fc_get_event_number(),
  490. sizeof(event_data), (char *) &event_data,
  491. SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
  492. psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
  493. lpfc_offline_prep(phba);
  494. lpfc_offline(phba);
  495. lpfc_unblock_mgmt_io(phba);
  496. phba->hba_state = LPFC_HBA_ERROR;
  497. lpfc_hba_down_post(phba);
  498. }
  499. }
  500. /************************************************************************/
  501. /* */
  502. /* lpfc_handle_latt */
  503. /* This routine will handle processing a Host Attention */
  504. /* Link Status event. This will be initialized */
  505. /* as a SLI layer callback routine. */
  506. /* */
  507. /************************************************************************/
  508. void
  509. lpfc_handle_latt(struct lpfc_hba * phba)
  510. {
  511. struct lpfc_sli *psli = &phba->sli;
  512. LPFC_MBOXQ_t *pmb;
  513. volatile uint32_t control;
  514. struct lpfc_dmabuf *mp;
  515. int rc = -ENOMEM;
  516. pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  517. if (!pmb)
  518. goto lpfc_handle_latt_err_exit;
  519. mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  520. if (!mp)
  521. goto lpfc_handle_latt_free_pmb;
  522. mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
  523. if (!mp->virt)
  524. goto lpfc_handle_latt_free_mp;
  525. rc = -EIO;
  526. /* Cleanup any outstanding ELS commands */
  527. lpfc_els_flush_cmd(phba);
  528. psli->slistat.link_event++;
  529. lpfc_read_la(phba, pmb, mp);
  530. pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
  531. rc = lpfc_sli_issue_mbox (phba, pmb, (MBX_NOWAIT | MBX_STOP_IOCB));
  532. if (rc == MBX_NOT_FINISHED)
  533. goto lpfc_handle_latt_free_mbuf;
  534. /* Clear Link Attention in HA REG */
  535. spin_lock_irq(phba->host->host_lock);
  536. writel(HA_LATT, phba->HAregaddr);
  537. readl(phba->HAregaddr); /* flush */
  538. spin_unlock_irq(phba->host->host_lock);
  539. return;
  540. lpfc_handle_latt_free_mbuf:
  541. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  542. lpfc_handle_latt_free_mp:
  543. kfree(mp);
  544. lpfc_handle_latt_free_pmb:
  545. mempool_free(pmb, phba->mbox_mem_pool);
  546. lpfc_handle_latt_err_exit:
  547. /* Enable Link attention interrupts */
  548. spin_lock_irq(phba->host->host_lock);
  549. psli->sli_flag |= LPFC_PROCESS_LA;
  550. control = readl(phba->HCregaddr);
  551. control |= HC_LAINT_ENA;
  552. writel(control, phba->HCregaddr);
  553. readl(phba->HCregaddr); /* flush */
  554. /* Clear Link Attention in HA REG */
  555. writel(HA_LATT, phba->HAregaddr);
  556. readl(phba->HAregaddr); /* flush */
  557. spin_unlock_irq(phba->host->host_lock);
  558. lpfc_linkdown(phba);
  559. phba->hba_state = LPFC_HBA_ERROR;
  560. /* The other case is an error from issue_mbox */
  561. if (rc == -ENOMEM)
  562. lpfc_printf_log(phba,
  563. KERN_WARNING,
  564. LOG_MBOX,
  565. "%d:0300 READ_LA: no buffers\n",
  566. phba->brd_no);
  567. return;
  568. }
  569. /************************************************************************/
  570. /* */
  571. /* lpfc_parse_vpd */
  572. /* This routine will parse the VPD data */
  573. /* */
  574. /************************************************************************/
  575. static int
  576. lpfc_parse_vpd(struct lpfc_hba * phba, uint8_t * vpd, int len)
  577. {
  578. uint8_t lenlo, lenhi;
  579. int Length;
  580. int i, j;
  581. int finished = 0;
  582. int index = 0;
  583. if (!vpd)
  584. return 0;
  585. /* Vital Product */
  586. lpfc_printf_log(phba,
  587. KERN_INFO,
  588. LOG_INIT,
  589. "%d:0455 Vital Product Data: x%x x%x x%x x%x\n",
  590. phba->brd_no,
  591. (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
  592. (uint32_t) vpd[3]);
  593. while (!finished && (index < (len - 4))) {
  594. switch (vpd[index]) {
  595. case 0x82:
  596. case 0x91:
  597. index += 1;
  598. lenlo = vpd[index];
  599. index += 1;
  600. lenhi = vpd[index];
  601. index += 1;
  602. i = ((((unsigned short)lenhi) << 8) + lenlo);
  603. index += i;
  604. break;
  605. case 0x90:
  606. index += 1;
  607. lenlo = vpd[index];
  608. index += 1;
  609. lenhi = vpd[index];
  610. index += 1;
  611. Length = ((((unsigned short)lenhi) << 8) + lenlo);
  612. if (Length > len - index)
  613. Length = len - index;
  614. while (Length > 0) {
  615. /* Look for Serial Number */
  616. if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
  617. index += 2;
  618. i = vpd[index];
  619. index += 1;
  620. j = 0;
  621. Length -= (3+i);
  622. while(i--) {
  623. phba->SerialNumber[j++] = vpd[index++];
  624. if (j == 31)
  625. break;
  626. }
  627. phba->SerialNumber[j] = 0;
  628. continue;
  629. }
  630. else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
  631. phba->vpd_flag |= VPD_MODEL_DESC;
  632. index += 2;
  633. i = vpd[index];
  634. index += 1;
  635. j = 0;
  636. Length -= (3+i);
  637. while(i--) {
  638. phba->ModelDesc[j++] = vpd[index++];
  639. if (j == 255)
  640. break;
  641. }
  642. phba->ModelDesc[j] = 0;
  643. continue;
  644. }
  645. else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
  646. phba->vpd_flag |= VPD_MODEL_NAME;
  647. index += 2;
  648. i = vpd[index];
  649. index += 1;
  650. j = 0;
  651. Length -= (3+i);
  652. while(i--) {
  653. phba->ModelName[j++] = vpd[index++];
  654. if (j == 79)
  655. break;
  656. }
  657. phba->ModelName[j] = 0;
  658. continue;
  659. }
  660. else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
  661. phba->vpd_flag |= VPD_PROGRAM_TYPE;
  662. index += 2;
  663. i = vpd[index];
  664. index += 1;
  665. j = 0;
  666. Length -= (3+i);
  667. while(i--) {
  668. phba->ProgramType[j++] = vpd[index++];
  669. if (j == 255)
  670. break;
  671. }
  672. phba->ProgramType[j] = 0;
  673. continue;
  674. }
  675. else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
  676. phba->vpd_flag |= VPD_PORT;
  677. index += 2;
  678. i = vpd[index];
  679. index += 1;
  680. j = 0;
  681. Length -= (3+i);
  682. while(i--) {
  683. phba->Port[j++] = vpd[index++];
  684. if (j == 19)
  685. break;
  686. }
  687. phba->Port[j] = 0;
  688. continue;
  689. }
  690. else {
  691. index += 2;
  692. i = vpd[index];
  693. index += 1;
  694. index += i;
  695. Length -= (3 + i);
  696. }
  697. }
  698. finished = 0;
  699. break;
  700. case 0x78:
  701. finished = 1;
  702. break;
  703. default:
  704. index ++;
  705. break;
  706. }
  707. }
  708. return(1);
  709. }
  710. static void
  711. lpfc_get_hba_model_desc(struct lpfc_hba * phba, uint8_t * mdp, uint8_t * descp)
  712. {
  713. lpfc_vpd_t *vp;
  714. uint16_t dev_id = phba->pcidev->device;
  715. int max_speed;
  716. struct {
  717. char * name;
  718. int max_speed;
  719. char * bus;
  720. } m = {"<Unknown>", 0, ""};
  721. if (mdp && mdp[0] != '\0'
  722. && descp && descp[0] != '\0')
  723. return;
  724. if (phba->lmt & LMT_10Gb)
  725. max_speed = 10;
  726. else if (phba->lmt & LMT_8Gb)
  727. max_speed = 8;
  728. else if (phba->lmt & LMT_4Gb)
  729. max_speed = 4;
  730. else if (phba->lmt & LMT_2Gb)
  731. max_speed = 2;
  732. else
  733. max_speed = 1;
  734. vp = &phba->vpd;
  735. switch (dev_id) {
  736. case PCI_DEVICE_ID_FIREFLY:
  737. m = (typeof(m)){"LP6000", max_speed, "PCI"};
  738. break;
  739. case PCI_DEVICE_ID_SUPERFLY:
  740. if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
  741. m = (typeof(m)){"LP7000", max_speed, "PCI"};
  742. else
  743. m = (typeof(m)){"LP7000E", max_speed, "PCI"};
  744. break;
  745. case PCI_DEVICE_ID_DRAGONFLY:
  746. m = (typeof(m)){"LP8000", max_speed, "PCI"};
  747. break;
  748. case PCI_DEVICE_ID_CENTAUR:
  749. if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
  750. m = (typeof(m)){"LP9002", max_speed, "PCI"};
  751. else
  752. m = (typeof(m)){"LP9000", max_speed, "PCI"};
  753. break;
  754. case PCI_DEVICE_ID_RFLY:
  755. m = (typeof(m)){"LP952", max_speed, "PCI"};
  756. break;
  757. case PCI_DEVICE_ID_PEGASUS:
  758. m = (typeof(m)){"LP9802", max_speed, "PCI-X"};
  759. break;
  760. case PCI_DEVICE_ID_THOR:
  761. m = (typeof(m)){"LP10000", max_speed, "PCI-X"};
  762. break;
  763. case PCI_DEVICE_ID_VIPER:
  764. m = (typeof(m)){"LPX1000", max_speed, "PCI-X"};
  765. break;
  766. case PCI_DEVICE_ID_PFLY:
  767. m = (typeof(m)){"LP982", max_speed, "PCI-X"};
  768. break;
  769. case PCI_DEVICE_ID_TFLY:
  770. m = (typeof(m)){"LP1050", max_speed, "PCI-X"};
  771. break;
  772. case PCI_DEVICE_ID_HELIOS:
  773. m = (typeof(m)){"LP11000", max_speed, "PCI-X2"};
  774. break;
  775. case PCI_DEVICE_ID_HELIOS_SCSP:
  776. m = (typeof(m)){"LP11000-SP", max_speed, "PCI-X2"};
  777. break;
  778. case PCI_DEVICE_ID_HELIOS_DCSP:
  779. m = (typeof(m)){"LP11002-SP", max_speed, "PCI-X2"};
  780. break;
  781. case PCI_DEVICE_ID_NEPTUNE:
  782. m = (typeof(m)){"LPe1000", max_speed, "PCIe"};
  783. break;
  784. case PCI_DEVICE_ID_NEPTUNE_SCSP:
  785. m = (typeof(m)){"LPe1000-SP", max_speed, "PCIe"};
  786. break;
  787. case PCI_DEVICE_ID_NEPTUNE_DCSP:
  788. m = (typeof(m)){"LPe1002-SP", max_speed, "PCIe"};
  789. break;
  790. case PCI_DEVICE_ID_BMID:
  791. m = (typeof(m)){"LP1150", max_speed, "PCI-X2"};
  792. break;
  793. case PCI_DEVICE_ID_BSMB:
  794. m = (typeof(m)){"LP111", max_speed, "PCI-X2"};
  795. break;
  796. case PCI_DEVICE_ID_ZEPHYR:
  797. m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
  798. break;
  799. case PCI_DEVICE_ID_ZEPHYR_SCSP:
  800. m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
  801. break;
  802. case PCI_DEVICE_ID_ZEPHYR_DCSP:
  803. m = (typeof(m)){"LPe11002-SP", max_speed, "PCIe"};
  804. break;
  805. case PCI_DEVICE_ID_ZMID:
  806. m = (typeof(m)){"LPe1150", max_speed, "PCIe"};
  807. break;
  808. case PCI_DEVICE_ID_ZSMB:
  809. m = (typeof(m)){"LPe111", max_speed, "PCIe"};
  810. break;
  811. case PCI_DEVICE_ID_LP101:
  812. m = (typeof(m)){"LP101", max_speed, "PCI-X"};
  813. break;
  814. case PCI_DEVICE_ID_LP10000S:
  815. m = (typeof(m)){"LP10000-S", max_speed, "PCI"};
  816. break;
  817. case PCI_DEVICE_ID_LP11000S:
  818. m = (typeof(m)){"LP11000-S", max_speed,
  819. "PCI-X2"};
  820. break;
  821. case PCI_DEVICE_ID_LPE11000S:
  822. m = (typeof(m)){"LPe11000-S", max_speed,
  823. "PCIe"};
  824. break;
  825. case PCI_DEVICE_ID_SAT:
  826. m = (typeof(m)){"LPe12000", max_speed, "PCIe"};
  827. break;
  828. case PCI_DEVICE_ID_SAT_MID:
  829. m = (typeof(m)){"LPe1250", max_speed, "PCIe"};
  830. break;
  831. case PCI_DEVICE_ID_SAT_SMB:
  832. m = (typeof(m)){"LPe121", max_speed, "PCIe"};
  833. break;
  834. case PCI_DEVICE_ID_SAT_DCSP:
  835. m = (typeof(m)){"LPe12002-SP", max_speed, "PCIe"};
  836. break;
  837. case PCI_DEVICE_ID_SAT_SCSP:
  838. m = (typeof(m)){"LPe12000-SP", max_speed, "PCIe"};
  839. break;
  840. case PCI_DEVICE_ID_SAT_S:
  841. m = (typeof(m)){"LPe12000-S", max_speed, "PCIe"};
  842. break;
  843. default:
  844. m = (typeof(m)){ NULL };
  845. break;
  846. }
  847. if (mdp && mdp[0] == '\0')
  848. snprintf(mdp, 79,"%s", m.name);
  849. if (descp && descp[0] == '\0')
  850. snprintf(descp, 255,
  851. "Emulex %s %dGb %s Fibre Channel Adapter",
  852. m.name, m.max_speed, m.bus);
  853. }
  854. /**************************************************/
  855. /* lpfc_post_buffer */
  856. /* */
  857. /* This routine will post count buffers to the */
  858. /* ring with the QUE_RING_BUF_CN command. This */
  859. /* allows 3 buffers / command to be posted. */
  860. /* Returns the number of buffers NOT posted. */
  861. /**************************************************/
  862. int
  863. lpfc_post_buffer(struct lpfc_hba * phba, struct lpfc_sli_ring * pring, int cnt,
  864. int type)
  865. {
  866. IOCB_t *icmd;
  867. struct lpfc_iocbq *iocb;
  868. struct lpfc_dmabuf *mp1, *mp2;
  869. cnt += pring->missbufcnt;
  870. /* While there are buffers to post */
  871. while (cnt > 0) {
  872. /* Allocate buffer for command iocb */
  873. spin_lock_irq(phba->host->host_lock);
  874. iocb = lpfc_sli_get_iocbq(phba);
  875. spin_unlock_irq(phba->host->host_lock);
  876. if (iocb == NULL) {
  877. pring->missbufcnt = cnt;
  878. return cnt;
  879. }
  880. icmd = &iocb->iocb;
  881. /* 2 buffers can be posted per command */
  882. /* Allocate buffer to post */
  883. mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
  884. if (mp1)
  885. mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
  886. &mp1->phys);
  887. if (mp1 == 0 || mp1->virt == 0) {
  888. kfree(mp1);
  889. spin_lock_irq(phba->host->host_lock);
  890. lpfc_sli_release_iocbq(phba, iocb);
  891. spin_unlock_irq(phba->host->host_lock);
  892. pring->missbufcnt = cnt;
  893. return cnt;
  894. }
  895. INIT_LIST_HEAD(&mp1->list);
  896. /* Allocate buffer to post */
  897. if (cnt > 1) {
  898. mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
  899. if (mp2)
  900. mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
  901. &mp2->phys);
  902. if (mp2 == 0 || mp2->virt == 0) {
  903. kfree(mp2);
  904. lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
  905. kfree(mp1);
  906. spin_lock_irq(phba->host->host_lock);
  907. lpfc_sli_release_iocbq(phba, iocb);
  908. spin_unlock_irq(phba->host->host_lock);
  909. pring->missbufcnt = cnt;
  910. return cnt;
  911. }
  912. INIT_LIST_HEAD(&mp2->list);
  913. } else {
  914. mp2 = NULL;
  915. }
  916. icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
  917. icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
  918. icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
  919. icmd->ulpBdeCount = 1;
  920. cnt--;
  921. if (mp2) {
  922. icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
  923. icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
  924. icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
  925. cnt--;
  926. icmd->ulpBdeCount = 2;
  927. }
  928. icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
  929. icmd->ulpLe = 1;
  930. spin_lock_irq(phba->host->host_lock);
  931. if (lpfc_sli_issue_iocb(phba, pring, iocb, 0) == IOCB_ERROR) {
  932. lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
  933. kfree(mp1);
  934. cnt++;
  935. if (mp2) {
  936. lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
  937. kfree(mp2);
  938. cnt++;
  939. }
  940. lpfc_sli_release_iocbq(phba, iocb);
  941. pring->missbufcnt = cnt;
  942. spin_unlock_irq(phba->host->host_lock);
  943. return cnt;
  944. }
  945. spin_unlock_irq(phba->host->host_lock);
  946. lpfc_sli_ringpostbuf_put(phba, pring, mp1);
  947. if (mp2) {
  948. lpfc_sli_ringpostbuf_put(phba, pring, mp2);
  949. }
  950. }
  951. pring->missbufcnt = 0;
  952. return 0;
  953. }
  954. /************************************************************************/
  955. /* */
  956. /* lpfc_post_rcv_buf */
  957. /* This routine post initial rcv buffers to the configured rings */
  958. /* */
  959. /************************************************************************/
  960. static int
  961. lpfc_post_rcv_buf(struct lpfc_hba * phba)
  962. {
  963. struct lpfc_sli *psli = &phba->sli;
  964. /* Ring 0, ELS / CT buffers */
  965. lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0, 1);
  966. /* Ring 2 - FCP no buffers needed */
  967. return 0;
  968. }
  969. #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
  970. /************************************************************************/
  971. /* */
  972. /* lpfc_sha_init */
  973. /* */
  974. /************************************************************************/
  975. static void
  976. lpfc_sha_init(uint32_t * HashResultPointer)
  977. {
  978. HashResultPointer[0] = 0x67452301;
  979. HashResultPointer[1] = 0xEFCDAB89;
  980. HashResultPointer[2] = 0x98BADCFE;
  981. HashResultPointer[3] = 0x10325476;
  982. HashResultPointer[4] = 0xC3D2E1F0;
  983. }
  984. /************************************************************************/
  985. /* */
  986. /* lpfc_sha_iterate */
  987. /* */
  988. /************************************************************************/
  989. static void
  990. lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
  991. {
  992. int t;
  993. uint32_t TEMP;
  994. uint32_t A, B, C, D, E;
  995. t = 16;
  996. do {
  997. HashWorkingPointer[t] =
  998. S(1,
  999. HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
  1000. 8] ^
  1001. HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
  1002. } while (++t <= 79);
  1003. t = 0;
  1004. A = HashResultPointer[0];
  1005. B = HashResultPointer[1];
  1006. C = HashResultPointer[2];
  1007. D = HashResultPointer[3];
  1008. E = HashResultPointer[4];
  1009. do {
  1010. if (t < 20) {
  1011. TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
  1012. } else if (t < 40) {
  1013. TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
  1014. } else if (t < 60) {
  1015. TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
  1016. } else {
  1017. TEMP = (B ^ C ^ D) + 0xCA62C1D6;
  1018. }
  1019. TEMP += S(5, A) + E + HashWorkingPointer[t];
  1020. E = D;
  1021. D = C;
  1022. C = S(30, B);
  1023. B = A;
  1024. A = TEMP;
  1025. } while (++t <= 79);
  1026. HashResultPointer[0] += A;
  1027. HashResultPointer[1] += B;
  1028. HashResultPointer[2] += C;
  1029. HashResultPointer[3] += D;
  1030. HashResultPointer[4] += E;
  1031. }
  1032. /************************************************************************/
  1033. /* */
  1034. /* lpfc_challenge_key */
  1035. /* */
  1036. /************************************************************************/
  1037. static void
  1038. lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
  1039. {
  1040. *HashWorking = (*RandomChallenge ^ *HashWorking);
  1041. }
  1042. /************************************************************************/
  1043. /* */
  1044. /* lpfc_hba_init */
  1045. /* */
  1046. /************************************************************************/
  1047. void
  1048. lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
  1049. {
  1050. int t;
  1051. uint32_t *HashWorking;
  1052. uint32_t *pwwnn = phba->wwnn;
  1053. HashWorking = kmalloc(80 * sizeof(uint32_t), GFP_KERNEL);
  1054. if (!HashWorking)
  1055. return;
  1056. memset(HashWorking, 0, (80 * sizeof(uint32_t)));
  1057. HashWorking[0] = HashWorking[78] = *pwwnn++;
  1058. HashWorking[1] = HashWorking[79] = *pwwnn;
  1059. for (t = 0; t < 7; t++)
  1060. lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
  1061. lpfc_sha_init(hbainit);
  1062. lpfc_sha_iterate(hbainit, HashWorking);
  1063. kfree(HashWorking);
  1064. }
  1065. static void
  1066. lpfc_cleanup(struct lpfc_hba * phba)
  1067. {
  1068. struct lpfc_nodelist *ndlp, *next_ndlp;
  1069. /* clean up phba - lpfc specific */
  1070. lpfc_can_disctmo(phba);
  1071. list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_nodes, nlp_listp)
  1072. lpfc_nlp_put(ndlp);
  1073. INIT_LIST_HEAD(&phba->fc_nodes);
  1074. return;
  1075. }
  1076. static void
  1077. lpfc_establish_link_tmo(unsigned long ptr)
  1078. {
  1079. struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
  1080. unsigned long iflag;
  1081. /* Re-establishing Link, timer expired */
  1082. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  1083. "%d:1300 Re-establishing Link, timer expired "
  1084. "Data: x%x x%x\n",
  1085. phba->brd_no, phba->fc_flag, phba->hba_state);
  1086. spin_lock_irqsave(phba->host->host_lock, iflag);
  1087. phba->fc_flag &= ~FC_ESTABLISH_LINK;
  1088. spin_unlock_irqrestore(phba->host->host_lock, iflag);
  1089. }
  1090. static int
  1091. lpfc_stop_timer(struct lpfc_hba * phba)
  1092. {
  1093. struct lpfc_sli *psli = &phba->sli;
  1094. del_timer_sync(&phba->fcp_poll_timer);
  1095. del_timer_sync(&phba->fc_estabtmo);
  1096. del_timer_sync(&phba->fc_disctmo);
  1097. del_timer_sync(&phba->fc_fdmitmo);
  1098. del_timer_sync(&phba->els_tmofunc);
  1099. psli = &phba->sli;
  1100. del_timer_sync(&psli->mbox_tmo);
  1101. return(1);
  1102. }
  1103. int
  1104. lpfc_online(struct lpfc_hba * phba)
  1105. {
  1106. if (!phba)
  1107. return 0;
  1108. if (!(phba->fc_flag & FC_OFFLINE_MODE))
  1109. return 0;
  1110. lpfc_printf_log(phba,
  1111. KERN_WARNING,
  1112. LOG_INIT,
  1113. "%d:0458 Bring Adapter online\n",
  1114. phba->brd_no);
  1115. lpfc_block_mgmt_io(phba);
  1116. if (!lpfc_sli_queue_setup(phba)) {
  1117. lpfc_unblock_mgmt_io(phba);
  1118. return 1;
  1119. }
  1120. if (lpfc_sli_hba_setup(phba)) { /* Initialize the HBA */
  1121. lpfc_unblock_mgmt_io(phba);
  1122. return 1;
  1123. }
  1124. spin_lock_irq(phba->host->host_lock);
  1125. phba->fc_flag &= ~FC_OFFLINE_MODE;
  1126. spin_unlock_irq(phba->host->host_lock);
  1127. lpfc_unblock_mgmt_io(phba);
  1128. return 0;
  1129. }
  1130. void
  1131. lpfc_block_mgmt_io(struct lpfc_hba * phba)
  1132. {
  1133. unsigned long iflag;
  1134. spin_lock_irqsave(phba->host->host_lock, iflag);
  1135. phba->fc_flag |= FC_BLOCK_MGMT_IO;
  1136. spin_unlock_irqrestore(phba->host->host_lock, iflag);
  1137. }
  1138. void
  1139. lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
  1140. {
  1141. unsigned long iflag;
  1142. spin_lock_irqsave(phba->host->host_lock, iflag);
  1143. phba->fc_flag &= ~FC_BLOCK_MGMT_IO;
  1144. spin_unlock_irqrestore(phba->host->host_lock, iflag);
  1145. }
  1146. void
  1147. lpfc_offline_prep(struct lpfc_hba * phba)
  1148. {
  1149. struct lpfc_nodelist *ndlp, *next_ndlp;
  1150. if (phba->fc_flag & FC_OFFLINE_MODE)
  1151. return;
  1152. lpfc_block_mgmt_io(phba);
  1153. lpfc_linkdown(phba);
  1154. /* Issue an unreg_login to all nodes */
  1155. list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_nodes, nlp_listp)
  1156. if (ndlp->nlp_state != NLP_STE_UNUSED_NODE)
  1157. lpfc_unreg_rpi(phba, ndlp);
  1158. lpfc_sli_flush_mbox_queue(phba);
  1159. }
  1160. void
  1161. lpfc_offline(struct lpfc_hba * phba)
  1162. {
  1163. unsigned long iflag;
  1164. if (phba->fc_flag & FC_OFFLINE_MODE)
  1165. return;
  1166. /* stop all timers associated with this hba */
  1167. lpfc_stop_timer(phba);
  1168. lpfc_printf_log(phba,
  1169. KERN_WARNING,
  1170. LOG_INIT,
  1171. "%d:0460 Bring Adapter offline\n",
  1172. phba->brd_no);
  1173. /* Bring down the SLI Layer and cleanup. The HBA is offline
  1174. now. */
  1175. lpfc_sli_hba_down(phba);
  1176. lpfc_cleanup(phba);
  1177. spin_lock_irqsave(phba->host->host_lock, iflag);
  1178. phba->work_hba_events = 0;
  1179. phba->work_ha = 0;
  1180. phba->fc_flag |= FC_OFFLINE_MODE;
  1181. spin_unlock_irqrestore(phba->host->host_lock, iflag);
  1182. }
  1183. /******************************************************************************
  1184. * Function name: lpfc_scsi_free
  1185. *
  1186. * Description: Called from lpfc_pci_remove_one free internal driver resources
  1187. *
  1188. ******************************************************************************/
  1189. static int
  1190. lpfc_scsi_free(struct lpfc_hba * phba)
  1191. {
  1192. struct lpfc_scsi_buf *sb, *sb_next;
  1193. struct lpfc_iocbq *io, *io_next;
  1194. spin_lock_irq(phba->host->host_lock);
  1195. /* Release all the lpfc_scsi_bufs maintained by this host. */
  1196. list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
  1197. list_del(&sb->list);
  1198. pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
  1199. sb->dma_handle);
  1200. kfree(sb);
  1201. phba->total_scsi_bufs--;
  1202. }
  1203. /* Release all the lpfc_iocbq entries maintained by this host. */
  1204. list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
  1205. list_del(&io->list);
  1206. kfree(io);
  1207. phba->total_iocbq_bufs--;
  1208. }
  1209. spin_unlock_irq(phba->host->host_lock);
  1210. return 0;
  1211. }
  1212. void lpfc_remove_device(struct lpfc_hba *phba)
  1213. {
  1214. unsigned long iflag;
  1215. lpfc_free_sysfs_attr(phba);
  1216. spin_lock_irqsave(phba->host->host_lock, iflag);
  1217. phba->fc_flag |= FC_UNLOADING;
  1218. spin_unlock_irqrestore(phba->host->host_lock, iflag);
  1219. fc_remove_host(phba->host);
  1220. scsi_remove_host(phba->host);
  1221. kthread_stop(phba->worker_thread);
  1222. /*
  1223. * Bring down the SLI Layer. This step disable all interrupts,
  1224. * clears the rings, discards all mailbox commands, and resets
  1225. * the HBA.
  1226. */
  1227. lpfc_sli_hba_down(phba);
  1228. lpfc_sli_brdrestart(phba);
  1229. /* Release the irq reservation */
  1230. free_irq(phba->pcidev->irq, phba);
  1231. pci_disable_msi(phba->pcidev);
  1232. lpfc_cleanup(phba);
  1233. lpfc_stop_timer(phba);
  1234. phba->work_hba_events = 0;
  1235. /*
  1236. * Call scsi_free before mem_free since scsi bufs are released to their
  1237. * corresponding pools here.
  1238. */
  1239. lpfc_scsi_free(phba);
  1240. lpfc_mem_free(phba);
  1241. /* Free resources associated with SLI2 interface */
  1242. dma_free_coherent(&phba->pcidev->dev, SLI2_SLIM_SIZE,
  1243. phba->slim2p, phba->slim2p_mapping);
  1244. /* unmap adapter SLIM and Control Registers */
  1245. iounmap(phba->ctrl_regs_memmap_p);
  1246. iounmap(phba->slim_memmap_p);
  1247. pci_release_regions(phba->pcidev);
  1248. pci_disable_device(phba->pcidev);
  1249. idr_remove(&lpfc_hba_index, phba->brd_no);
  1250. scsi_host_put(phba->host);
  1251. }
  1252. void lpfc_scan_start(struct Scsi_Host *host)
  1253. {
  1254. struct lpfc_hba *phba = (struct lpfc_hba*)host->hostdata;
  1255. if (lpfc_alloc_sysfs_attr(phba))
  1256. goto error;
  1257. phba->MBslimaddr = phba->slim_memmap_p;
  1258. phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
  1259. phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
  1260. phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
  1261. phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
  1262. if (lpfc_sli_hba_setup(phba))
  1263. goto error;
  1264. /*
  1265. * hba setup may have changed the hba_queue_depth so we need to adjust
  1266. * the value of can_queue.
  1267. */
  1268. host->can_queue = phba->cfg_hba_queue_depth - 10;
  1269. return;
  1270. error:
  1271. lpfc_remove_device(phba);
  1272. }
  1273. int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
  1274. {
  1275. struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata;
  1276. if (!phba->host)
  1277. return 1;
  1278. if (time >= 30 * HZ)
  1279. goto finished;
  1280. if (phba->hba_state != LPFC_HBA_READY)
  1281. return 0;
  1282. if (phba->num_disc_nodes || phba->fc_prli_sent)
  1283. return 0;
  1284. if ((phba->fc_map_cnt == 0) && (time < 2 * HZ))
  1285. return 0;
  1286. if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE)
  1287. return 0;
  1288. if ((phba->hba_state > LPFC_LINK_DOWN) || (time < 15 * HZ))
  1289. return 0;
  1290. finished:
  1291. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  1292. spin_lock_irq(shost->host_lock);
  1293. lpfc_poll_start_timer(phba);
  1294. spin_unlock_irq(shost->host_lock);
  1295. }
  1296. /*
  1297. * set fixed host attributes
  1298. * Must done after lpfc_sli_hba_setup()
  1299. */
  1300. fc_host_node_name(shost) = wwn_to_u64(phba->fc_nodename.u.wwn);
  1301. fc_host_port_name(shost) = wwn_to_u64(phba->fc_portname.u.wwn);
  1302. fc_host_supported_classes(shost) = FC_COS_CLASS3;
  1303. memset(fc_host_supported_fc4s(shost), 0,
  1304. sizeof(fc_host_supported_fc4s(shost)));
  1305. fc_host_supported_fc4s(shost)[2] = 1;
  1306. fc_host_supported_fc4s(shost)[7] = 1;
  1307. lpfc_get_hba_sym_node_name(phba, fc_host_symbolic_name(shost));
  1308. fc_host_supported_speeds(shost) = 0;
  1309. if (phba->lmt & LMT_10Gb)
  1310. fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
  1311. if (phba->lmt & LMT_4Gb)
  1312. fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
  1313. if (phba->lmt & LMT_2Gb)
  1314. fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
  1315. if (phba->lmt & LMT_1Gb)
  1316. fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
  1317. fc_host_maxframe_size(shost) =
  1318. ((((uint32_t) phba->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
  1319. (uint32_t) phba->fc_sparam.cmn.bbRcvSizeLsb);
  1320. /* This value is also unchanging */
  1321. memset(fc_host_active_fc4s(shost), 0,
  1322. sizeof(fc_host_active_fc4s(shost)));
  1323. fc_host_active_fc4s(shost)[2] = 1;
  1324. fc_host_active_fc4s(shost)[7] = 1;
  1325. spin_lock_irq(shost->host_lock);
  1326. phba->fc_flag &= ~FC_LOADING;
  1327. spin_unlock_irq(shost->host_lock);
  1328. return 1;
  1329. }
  1330. static int __devinit
  1331. lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
  1332. {
  1333. struct Scsi_Host *host;
  1334. struct lpfc_hba *phba;
  1335. struct lpfc_sli *psli;
  1336. struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
  1337. unsigned long bar0map_len, bar2map_len;
  1338. int error = -ENODEV, retval;
  1339. int i;
  1340. uint16_t iotag;
  1341. if (pci_enable_device(pdev))
  1342. goto out;
  1343. if (pci_request_regions(pdev, LPFC_DRIVER_NAME))
  1344. goto out_disable_device;
  1345. host = scsi_host_alloc(&lpfc_template, sizeof (struct lpfc_hba));
  1346. if (!host)
  1347. goto out_release_regions;
  1348. phba = (struct lpfc_hba*)host->hostdata;
  1349. memset(phba, 0, sizeof (struct lpfc_hba));
  1350. phba->host = host;
  1351. phba->fc_flag |= FC_LOADING;
  1352. phba->pcidev = pdev;
  1353. /* Assign an unused board number */
  1354. if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
  1355. goto out_put_host;
  1356. error = idr_get_new(&lpfc_hba_index, NULL, &phba->brd_no);
  1357. if (error)
  1358. goto out_put_host;
  1359. host->unique_id = phba->brd_no;
  1360. /* Initialize timers used by driver */
  1361. init_timer(&phba->fc_estabtmo);
  1362. phba->fc_estabtmo.function = lpfc_establish_link_tmo;
  1363. phba->fc_estabtmo.data = (unsigned long)phba;
  1364. init_timer(&phba->fc_disctmo);
  1365. phba->fc_disctmo.function = lpfc_disc_timeout;
  1366. phba->fc_disctmo.data = (unsigned long)phba;
  1367. init_timer(&phba->fc_fdmitmo);
  1368. phba->fc_fdmitmo.function = lpfc_fdmi_tmo;
  1369. phba->fc_fdmitmo.data = (unsigned long)phba;
  1370. init_timer(&phba->els_tmofunc);
  1371. phba->els_tmofunc.function = lpfc_els_timeout;
  1372. phba->els_tmofunc.data = (unsigned long)phba;
  1373. psli = &phba->sli;
  1374. init_timer(&psli->mbox_tmo);
  1375. psli->mbox_tmo.function = lpfc_mbox_timeout;
  1376. psli->mbox_tmo.data = (unsigned long)phba;
  1377. init_timer(&phba->fcp_poll_timer);
  1378. phba->fcp_poll_timer.function = lpfc_poll_timeout;
  1379. phba->fcp_poll_timer.data = (unsigned long)phba;
  1380. /*
  1381. * Get all the module params for configuring this host and then
  1382. * establish the host parameters.
  1383. */
  1384. lpfc_get_cfgparam(phba);
  1385. host->max_id = LPFC_MAX_TARGET;
  1386. host->max_lun = phba->cfg_max_luns;
  1387. host->this_id = -1;
  1388. INIT_LIST_HEAD(&phba->fc_nodes);
  1389. pci_set_master(pdev);
  1390. retval = pci_set_mwi(pdev);
  1391. if (retval)
  1392. dev_printk(KERN_WARNING, &pdev->dev,
  1393. "Warning: pci_set_mwi returned %d\n", retval);
  1394. if (pci_set_dma_mask(phba->pcidev, DMA_64BIT_MASK) != 0)
  1395. if (pci_set_dma_mask(phba->pcidev, DMA_32BIT_MASK) != 0)
  1396. goto out_idr_remove;
  1397. /*
  1398. * Get the bus address of Bar0 and Bar2 and the number of bytes
  1399. * required by each mapping.
  1400. */
  1401. phba->pci_bar0_map = pci_resource_start(phba->pcidev, 0);
  1402. bar0map_len = pci_resource_len(phba->pcidev, 0);
  1403. phba->pci_bar2_map = pci_resource_start(phba->pcidev, 2);
  1404. bar2map_len = pci_resource_len(phba->pcidev, 2);
  1405. /* Map HBA SLIM to a kernel virtual address. */
  1406. phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
  1407. if (!phba->slim_memmap_p) {
  1408. error = -ENODEV;
  1409. dev_printk(KERN_ERR, &pdev->dev,
  1410. "ioremap failed for SLIM memory.\n");
  1411. goto out_idr_remove;
  1412. }
  1413. /* Map HBA Control Registers to a kernel virtual address. */
  1414. phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
  1415. if (!phba->ctrl_regs_memmap_p) {
  1416. error = -ENODEV;
  1417. dev_printk(KERN_ERR, &pdev->dev,
  1418. "ioremap failed for HBA control registers.\n");
  1419. goto out_iounmap_slim;
  1420. }
  1421. /* Allocate memory for SLI-2 structures */
  1422. phba->slim2p = dma_alloc_coherent(&phba->pcidev->dev, SLI2_SLIM_SIZE,
  1423. &phba->slim2p_mapping, GFP_KERNEL);
  1424. if (!phba->slim2p)
  1425. goto out_iounmap;
  1426. memset(phba->slim2p, 0, SLI2_SLIM_SIZE);
  1427. /* Initialize the SLI Layer to run with lpfc HBAs. */
  1428. lpfc_sli_setup(phba);
  1429. lpfc_sli_queue_setup(phba);
  1430. error = lpfc_mem_alloc(phba);
  1431. if (error)
  1432. goto out_free_slim;
  1433. /* Initialize and populate the iocb list per host. */
  1434. INIT_LIST_HEAD(&phba->lpfc_iocb_list);
  1435. for (i = 0; i < LPFC_IOCB_LIST_CNT; i++) {
  1436. iocbq_entry = kmalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
  1437. if (iocbq_entry == NULL) {
  1438. printk(KERN_ERR "%s: only allocated %d iocbs of "
  1439. "expected %d count. Unloading driver.\n",
  1440. __FUNCTION__, i, LPFC_IOCB_LIST_CNT);
  1441. error = -ENOMEM;
  1442. goto out_free_iocbq;
  1443. }
  1444. memset(iocbq_entry, 0, sizeof(struct lpfc_iocbq));
  1445. iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
  1446. if (iotag == 0) {
  1447. kfree (iocbq_entry);
  1448. printk(KERN_ERR "%s: failed to allocate IOTAG. "
  1449. "Unloading driver.\n",
  1450. __FUNCTION__);
  1451. error = -ENOMEM;
  1452. goto out_free_iocbq;
  1453. }
  1454. spin_lock_irq(phba->host->host_lock);
  1455. list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
  1456. phba->total_iocbq_bufs++;
  1457. spin_unlock_irq(phba->host->host_lock);
  1458. }
  1459. /* Initialize HBA structure */
  1460. phba->fc_edtov = FF_DEF_EDTOV;
  1461. phba->fc_ratov = FF_DEF_RATOV;
  1462. phba->fc_altov = FF_DEF_ALTOV;
  1463. phba->fc_arbtov = FF_DEF_ARBTOV;
  1464. INIT_LIST_HEAD(&phba->work_list);
  1465. phba->work_ha_mask = (HA_ERATT|HA_MBATT|HA_LATT);
  1466. phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
  1467. /* Startup the kernel thread for this host adapter. */
  1468. phba->worker_thread = kthread_run(lpfc_do_work, phba,
  1469. "lpfc_worker_%d", phba->brd_no);
  1470. if (IS_ERR(phba->worker_thread)) {
  1471. error = PTR_ERR(phba->worker_thread);
  1472. goto out_free_iocbq;
  1473. }
  1474. /*
  1475. * Set initial can_queue value since 0 is no longer supported and
  1476. * scsi_add_host will fail. This will be adjusted later based on the
  1477. * max xri value determined in hba setup.
  1478. */
  1479. host->can_queue = phba->cfg_hba_queue_depth - 10;
  1480. /* Tell the midlayer we support 16 byte commands */
  1481. host->max_cmd_len = 16;
  1482. /* Initialize the list of scsi buffers used by driver for scsi IO. */
  1483. spin_lock_init(&phba->scsi_buf_list_lock);
  1484. INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
  1485. host->transportt = lpfc_transport_template;
  1486. pci_set_drvdata(pdev, host);
  1487. if (phba->cfg_use_msi) {
  1488. error = pci_enable_msi(phba->pcidev);
  1489. if (error)
  1490. lpfc_printf_log(phba, KERN_INFO, LOG_INIT, "%d:0452 "
  1491. "Enable MSI failed, continuing with "
  1492. "IRQ\n", phba->brd_no);
  1493. }
  1494. error = request_irq(phba->pcidev->irq, lpfc_intr_handler, IRQF_SHARED,
  1495. LPFC_DRIVER_NAME, phba);
  1496. if (error) {
  1497. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1498. "%d:0451 Enable interrupt handler failed\n",
  1499. phba->brd_no);
  1500. goto out_kthread_stop;
  1501. }
  1502. error = scsi_add_host(host, &pdev->dev);
  1503. if (error)
  1504. goto out_free_irq;
  1505. scsi_scan_host(host);
  1506. return 0;
  1507. out_free_irq:
  1508. lpfc_stop_timer(phba);
  1509. phba->work_hba_events = 0;
  1510. free_irq(phba->pcidev->irq, phba);
  1511. pci_disable_msi(phba->pcidev);
  1512. out_kthread_stop:
  1513. kthread_stop(phba->worker_thread);
  1514. out_free_iocbq:
  1515. list_for_each_entry_safe(iocbq_entry, iocbq_next,
  1516. &phba->lpfc_iocb_list, list) {
  1517. spin_lock_irq(phba->host->host_lock);
  1518. kfree(iocbq_entry);
  1519. phba->total_iocbq_bufs--;
  1520. spin_unlock_irq(phba->host->host_lock);
  1521. }
  1522. lpfc_mem_free(phba);
  1523. out_free_slim:
  1524. dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE, phba->slim2p,
  1525. phba->slim2p_mapping);
  1526. out_iounmap:
  1527. iounmap(phba->ctrl_regs_memmap_p);
  1528. out_iounmap_slim:
  1529. iounmap(phba->slim_memmap_p);
  1530. out_idr_remove:
  1531. idr_remove(&lpfc_hba_index, phba->brd_no);
  1532. out_put_host:
  1533. phba->host = NULL;
  1534. scsi_host_put(host);
  1535. out_release_regions:
  1536. pci_release_regions(pdev);
  1537. out_disable_device:
  1538. pci_disable_device(pdev);
  1539. out:
  1540. pci_set_drvdata(pdev, NULL);
  1541. return error;
  1542. }
  1543. static void __devexit
  1544. lpfc_pci_remove_one(struct pci_dev *pdev)
  1545. {
  1546. struct Scsi_Host *host = pci_get_drvdata(pdev);
  1547. struct lpfc_hba *phba = (struct lpfc_hba *)host->hostdata;
  1548. lpfc_remove_device(phba);
  1549. pci_set_drvdata(pdev, NULL);
  1550. }
  1551. /**
  1552. * lpfc_io_error_detected - called when PCI error is detected
  1553. * @pdev: Pointer to PCI device
  1554. * @state: The current pci conneection state
  1555. *
  1556. * This function is called after a PCI bus error affecting
  1557. * this device has been detected.
  1558. */
  1559. static pci_ers_result_t lpfc_io_error_detected(struct pci_dev *pdev,
  1560. pci_channel_state_t state)
  1561. {
  1562. struct Scsi_Host *host = pci_get_drvdata(pdev);
  1563. struct lpfc_hba *phba = (struct lpfc_hba *)host->hostdata;
  1564. struct lpfc_sli *psli = &phba->sli;
  1565. struct lpfc_sli_ring *pring;
  1566. if (state == pci_channel_io_perm_failure)
  1567. return PCI_ERS_RESULT_DISCONNECT;
  1568. pci_disable_device(pdev);
  1569. /*
  1570. * There may be I/Os dropped by the firmware.
  1571. * Error iocb (I/O) on txcmplq and let the SCSI layer
  1572. * retry it after re-establishing link.
  1573. */
  1574. pring = &psli->ring[psli->fcp_ring];
  1575. lpfc_sli_abort_iocb_ring(phba, pring);
  1576. /* Request a slot reset. */
  1577. return PCI_ERS_RESULT_NEED_RESET;
  1578. }
  1579. /**
  1580. * lpfc_io_slot_reset - called after the pci bus has been reset.
  1581. * @pdev: Pointer to PCI device
  1582. *
  1583. * Restart the card from scratch, as if from a cold-boot.
  1584. */
  1585. static pci_ers_result_t lpfc_io_slot_reset(struct pci_dev *pdev)
  1586. {
  1587. struct Scsi_Host *host = pci_get_drvdata(pdev);
  1588. struct lpfc_hba *phba = (struct lpfc_hba *)host->hostdata;
  1589. struct lpfc_sli *psli = &phba->sli;
  1590. int bars = pci_select_bars(pdev, IORESOURCE_MEM);
  1591. dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
  1592. if (pci_enable_device_bars(pdev, bars)) {
  1593. printk(KERN_ERR "lpfc: Cannot re-enable "
  1594. "PCI device after reset.\n");
  1595. return PCI_ERS_RESULT_DISCONNECT;
  1596. }
  1597. pci_set_master(pdev);
  1598. /* Re-establishing Link */
  1599. spin_lock_irq(phba->host->host_lock);
  1600. phba->fc_flag |= FC_ESTABLISH_LINK;
  1601. psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
  1602. spin_unlock_irq(phba->host->host_lock);
  1603. /* Take device offline; this will perform cleanup */
  1604. lpfc_offline(phba);
  1605. lpfc_sli_brdrestart(phba);
  1606. return PCI_ERS_RESULT_RECOVERED;
  1607. }
  1608. /**
  1609. * lpfc_io_resume - called when traffic can start flowing again.
  1610. * @pdev: Pointer to PCI device
  1611. *
  1612. * This callback is called when the error recovery driver tells us that
  1613. * its OK to resume normal operation.
  1614. */
  1615. static void lpfc_io_resume(struct pci_dev *pdev)
  1616. {
  1617. struct Scsi_Host *host = pci_get_drvdata(pdev);
  1618. struct lpfc_hba *phba = (struct lpfc_hba *)host->hostdata;
  1619. if (lpfc_online(phba) == 0) {
  1620. mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
  1621. }
  1622. }
  1623. static struct pci_device_id lpfc_id_table[] = {
  1624. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
  1625. PCI_ANY_ID, PCI_ANY_ID, },
  1626. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
  1627. PCI_ANY_ID, PCI_ANY_ID, },
  1628. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
  1629. PCI_ANY_ID, PCI_ANY_ID, },
  1630. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
  1631. PCI_ANY_ID, PCI_ANY_ID, },
  1632. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
  1633. PCI_ANY_ID, PCI_ANY_ID, },
  1634. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
  1635. PCI_ANY_ID, PCI_ANY_ID, },
  1636. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
  1637. PCI_ANY_ID, PCI_ANY_ID, },
  1638. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
  1639. PCI_ANY_ID, PCI_ANY_ID, },
  1640. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
  1641. PCI_ANY_ID, PCI_ANY_ID, },
  1642. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
  1643. PCI_ANY_ID, PCI_ANY_ID, },
  1644. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
  1645. PCI_ANY_ID, PCI_ANY_ID, },
  1646. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
  1647. PCI_ANY_ID, PCI_ANY_ID, },
  1648. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
  1649. PCI_ANY_ID, PCI_ANY_ID, },
  1650. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
  1651. PCI_ANY_ID, PCI_ANY_ID, },
  1652. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
  1653. PCI_ANY_ID, PCI_ANY_ID, },
  1654. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
  1655. PCI_ANY_ID, PCI_ANY_ID, },
  1656. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
  1657. PCI_ANY_ID, PCI_ANY_ID, },
  1658. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
  1659. PCI_ANY_ID, PCI_ANY_ID, },
  1660. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
  1661. PCI_ANY_ID, PCI_ANY_ID, },
  1662. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
  1663. PCI_ANY_ID, PCI_ANY_ID, },
  1664. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
  1665. PCI_ANY_ID, PCI_ANY_ID, },
  1666. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
  1667. PCI_ANY_ID, PCI_ANY_ID, },
  1668. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
  1669. PCI_ANY_ID, PCI_ANY_ID, },
  1670. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
  1671. PCI_ANY_ID, PCI_ANY_ID, },
  1672. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
  1673. PCI_ANY_ID, PCI_ANY_ID, },
  1674. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
  1675. PCI_ANY_ID, PCI_ANY_ID, },
  1676. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
  1677. PCI_ANY_ID, PCI_ANY_ID, },
  1678. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
  1679. PCI_ANY_ID, PCI_ANY_ID, },
  1680. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
  1681. PCI_ANY_ID, PCI_ANY_ID, },
  1682. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
  1683. PCI_ANY_ID, PCI_ANY_ID, },
  1684. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
  1685. PCI_ANY_ID, PCI_ANY_ID, },
  1686. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
  1687. PCI_ANY_ID, PCI_ANY_ID, },
  1688. {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
  1689. PCI_ANY_ID, PCI_ANY_ID, },
  1690. { 0 }
  1691. };
  1692. MODULE_DEVICE_TABLE(pci, lpfc_id_table);
  1693. static struct pci_error_handlers lpfc_err_handler = {
  1694. .error_detected = lpfc_io_error_detected,
  1695. .slot_reset = lpfc_io_slot_reset,
  1696. .resume = lpfc_io_resume,
  1697. };
  1698. static struct pci_driver lpfc_driver = {
  1699. .name = LPFC_DRIVER_NAME,
  1700. .id_table = lpfc_id_table,
  1701. .probe = lpfc_pci_probe_one,
  1702. .remove = __devexit_p(lpfc_pci_remove_one),
  1703. .err_handler = &lpfc_err_handler,
  1704. };
  1705. static int __init
  1706. lpfc_init(void)
  1707. {
  1708. int error = 0;
  1709. printk(LPFC_MODULE_DESC "\n");
  1710. printk(LPFC_COPYRIGHT "\n");
  1711. lpfc_transport_template =
  1712. fc_attach_transport(&lpfc_transport_functions);
  1713. if (!lpfc_transport_template)
  1714. return -ENOMEM;
  1715. error = pci_register_driver(&lpfc_driver);
  1716. if (error)
  1717. fc_release_transport(lpfc_transport_template);
  1718. return error;
  1719. }
  1720. static void __exit
  1721. lpfc_exit(void)
  1722. {
  1723. pci_unregister_driver(&lpfc_driver);
  1724. fc_release_transport(lpfc_transport_template);
  1725. }
  1726. module_init(lpfc_init);
  1727. module_exit(lpfc_exit);
  1728. MODULE_LICENSE("GPL");
  1729. MODULE_DESCRIPTION(LPFC_MODULE_DESC);
  1730. MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
  1731. MODULE_VERSION("0:" LPFC_DRIVER_VERSION);