lpfc_init.c 73 KB

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