lpfc_init.c 85 KB

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