lpfc_init.c 99 KB

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