lpfc_init.c 58 KB

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