lpfc_hbadisc.c 124 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2009 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/pci.h>
  23. #include <linux/kthread.h>
  24. #include <linux/interrupt.h>
  25. #include <scsi/scsi.h>
  26. #include <scsi/scsi_device.h>
  27. #include <scsi/scsi_host.h>
  28. #include <scsi/scsi_transport_fc.h>
  29. #include "lpfc_hw4.h"
  30. #include "lpfc_hw.h"
  31. #include "lpfc_nl.h"
  32. #include "lpfc_disc.h"
  33. #include "lpfc_sli.h"
  34. #include "lpfc_sli4.h"
  35. #include "lpfc_scsi.h"
  36. #include "lpfc.h"
  37. #include "lpfc_logmsg.h"
  38. #include "lpfc_crtn.h"
  39. #include "lpfc_vport.h"
  40. #include "lpfc_debugfs.h"
  41. /* AlpaArray for assignment of scsid for scan-down and bind_method */
  42. static uint8_t lpfcAlpaArray[] = {
  43. 0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
  44. 0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
  45. 0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
  46. 0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
  47. 0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
  48. 0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
  49. 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
  50. 0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
  51. 0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
  52. 0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
  53. 0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
  54. 0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
  55. 0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
  56. };
  57. static void lpfc_disc_timeout_handler(struct lpfc_vport *);
  58. static void lpfc_disc_flush_list(struct lpfc_vport *vport);
  59. void
  60. lpfc_terminate_rport_io(struct fc_rport *rport)
  61. {
  62. struct lpfc_rport_data *rdata;
  63. struct lpfc_nodelist * ndlp;
  64. struct lpfc_hba *phba;
  65. rdata = rport->dd_data;
  66. ndlp = rdata->pnode;
  67. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  68. if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
  69. printk(KERN_ERR "Cannot find remote node"
  70. " to terminate I/O Data x%x\n",
  71. rport->port_id);
  72. return;
  73. }
  74. phba = ndlp->phba;
  75. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
  76. "rport terminate: sid:x%x did:x%x flg:x%x",
  77. ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
  78. if (ndlp->nlp_sid != NLP_NO_SID) {
  79. lpfc_sli_abort_iocb(ndlp->vport,
  80. &phba->sli.ring[phba->sli.fcp_ring],
  81. ndlp->nlp_sid, 0, LPFC_CTX_TGT);
  82. }
  83. }
  84. /*
  85. * This function will be called when dev_loss_tmo fire.
  86. */
  87. void
  88. lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
  89. {
  90. struct lpfc_rport_data *rdata;
  91. struct lpfc_nodelist * ndlp;
  92. struct lpfc_vport *vport;
  93. struct lpfc_hba *phba;
  94. struct lpfc_work_evt *evtp;
  95. int put_node;
  96. int put_rport;
  97. rdata = rport->dd_data;
  98. ndlp = rdata->pnode;
  99. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
  100. return;
  101. vport = ndlp->vport;
  102. phba = vport->phba;
  103. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
  104. "rport devlosscb: sid:x%x did:x%x flg:x%x",
  105. ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
  106. /* Don't defer this if we are in the process of deleting the vport
  107. * or unloading the driver. The unload will cleanup the node
  108. * appropriately we just need to cleanup the ndlp rport info here.
  109. */
  110. if (vport->load_flag & FC_UNLOADING) {
  111. put_node = rdata->pnode != NULL;
  112. put_rport = ndlp->rport != NULL;
  113. rdata->pnode = NULL;
  114. ndlp->rport = NULL;
  115. if (put_node)
  116. lpfc_nlp_put(ndlp);
  117. if (put_rport)
  118. put_device(&rport->dev);
  119. return;
  120. }
  121. if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
  122. return;
  123. evtp = &ndlp->dev_loss_evt;
  124. if (!list_empty(&evtp->evt_listp))
  125. return;
  126. spin_lock_irq(&phba->hbalock);
  127. /* We need to hold the node by incrementing the reference
  128. * count until this queued work is done
  129. */
  130. evtp->evt_arg1 = lpfc_nlp_get(ndlp);
  131. if (evtp->evt_arg1) {
  132. evtp->evt = LPFC_EVT_DEV_LOSS;
  133. list_add_tail(&evtp->evt_listp, &phba->work_list);
  134. lpfc_worker_wake_up(phba);
  135. }
  136. spin_unlock_irq(&phba->hbalock);
  137. return;
  138. }
  139. /*
  140. * This function is called from the worker thread when dev_loss_tmo
  141. * expire.
  142. */
  143. static void
  144. lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
  145. {
  146. struct lpfc_rport_data *rdata;
  147. struct fc_rport *rport;
  148. struct lpfc_vport *vport;
  149. struct lpfc_hba *phba;
  150. uint8_t *name;
  151. int put_node;
  152. int put_rport;
  153. int warn_on = 0;
  154. rport = ndlp->rport;
  155. if (!rport)
  156. return;
  157. rdata = rport->dd_data;
  158. name = (uint8_t *) &ndlp->nlp_portname;
  159. vport = ndlp->vport;
  160. phba = vport->phba;
  161. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
  162. "rport devlosstmo:did:x%x type:x%x id:x%x",
  163. ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
  164. /* Don't defer this if we are in the process of deleting the vport
  165. * or unloading the driver. The unload will cleanup the node
  166. * appropriately we just need to cleanup the ndlp rport info here.
  167. */
  168. if (vport->load_flag & FC_UNLOADING) {
  169. if (ndlp->nlp_sid != NLP_NO_SID) {
  170. /* flush the target */
  171. lpfc_sli_abort_iocb(vport,
  172. &phba->sli.ring[phba->sli.fcp_ring],
  173. ndlp->nlp_sid, 0, LPFC_CTX_TGT);
  174. }
  175. put_node = rdata->pnode != NULL;
  176. put_rport = ndlp->rport != NULL;
  177. rdata->pnode = NULL;
  178. ndlp->rport = NULL;
  179. if (put_node)
  180. lpfc_nlp_put(ndlp);
  181. if (put_rport)
  182. put_device(&rport->dev);
  183. return;
  184. }
  185. if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
  186. lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
  187. "0284 Devloss timeout Ignored on "
  188. "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
  189. "NPort x%x\n",
  190. *name, *(name+1), *(name+2), *(name+3),
  191. *(name+4), *(name+5), *(name+6), *(name+7),
  192. ndlp->nlp_DID);
  193. return;
  194. }
  195. if (ndlp->nlp_type & NLP_FABRIC) {
  196. /* We will clean up these Nodes in linkup */
  197. put_node = rdata->pnode != NULL;
  198. put_rport = ndlp->rport != NULL;
  199. rdata->pnode = NULL;
  200. ndlp->rport = NULL;
  201. if (put_node)
  202. lpfc_nlp_put(ndlp);
  203. if (put_rport)
  204. put_device(&rport->dev);
  205. return;
  206. }
  207. if (ndlp->nlp_sid != NLP_NO_SID) {
  208. warn_on = 1;
  209. /* flush the target */
  210. lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
  211. ndlp->nlp_sid, 0, LPFC_CTX_TGT);
  212. }
  213. if (warn_on) {
  214. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  215. "0203 Devloss timeout on "
  216. "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
  217. "NPort x%06x Data: x%x x%x x%x\n",
  218. *name, *(name+1), *(name+2), *(name+3),
  219. *(name+4), *(name+5), *(name+6), *(name+7),
  220. ndlp->nlp_DID, ndlp->nlp_flag,
  221. ndlp->nlp_state, ndlp->nlp_rpi);
  222. } else {
  223. lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
  224. "0204 Devloss timeout on "
  225. "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
  226. "NPort x%06x Data: x%x x%x x%x\n",
  227. *name, *(name+1), *(name+2), *(name+3),
  228. *(name+4), *(name+5), *(name+6), *(name+7),
  229. ndlp->nlp_DID, ndlp->nlp_flag,
  230. ndlp->nlp_state, ndlp->nlp_rpi);
  231. }
  232. put_node = rdata->pnode != NULL;
  233. put_rport = ndlp->rport != NULL;
  234. rdata->pnode = NULL;
  235. ndlp->rport = NULL;
  236. if (put_node)
  237. lpfc_nlp_put(ndlp);
  238. if (put_rport)
  239. put_device(&rport->dev);
  240. if (!(vport->load_flag & FC_UNLOADING) &&
  241. !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
  242. !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
  243. (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE))
  244. lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
  245. lpfc_unregister_unused_fcf(phba);
  246. }
  247. /**
  248. * lpfc_alloc_fast_evt - Allocates data structure for posting event
  249. * @phba: Pointer to hba context object.
  250. *
  251. * This function is called from the functions which need to post
  252. * events from interrupt context. This function allocates data
  253. * structure required for posting event. It also keeps track of
  254. * number of events pending and prevent event storm when there are
  255. * too many events.
  256. **/
  257. struct lpfc_fast_path_event *
  258. lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
  259. struct lpfc_fast_path_event *ret;
  260. /* If there are lot of fast event do not exhaust memory due to this */
  261. if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
  262. return NULL;
  263. ret = kzalloc(sizeof(struct lpfc_fast_path_event),
  264. GFP_ATOMIC);
  265. if (ret) {
  266. atomic_inc(&phba->fast_event_count);
  267. INIT_LIST_HEAD(&ret->work_evt.evt_listp);
  268. ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
  269. }
  270. return ret;
  271. }
  272. /**
  273. * lpfc_free_fast_evt - Frees event data structure
  274. * @phba: Pointer to hba context object.
  275. * @evt: Event object which need to be freed.
  276. *
  277. * This function frees the data structure required for posting
  278. * events.
  279. **/
  280. void
  281. lpfc_free_fast_evt(struct lpfc_hba *phba,
  282. struct lpfc_fast_path_event *evt) {
  283. atomic_dec(&phba->fast_event_count);
  284. kfree(evt);
  285. }
  286. /**
  287. * lpfc_send_fastpath_evt - Posts events generated from fast path
  288. * @phba: Pointer to hba context object.
  289. * @evtp: Event data structure.
  290. *
  291. * This function is called from worker thread, when the interrupt
  292. * context need to post an event. This function posts the event
  293. * to fc transport netlink interface.
  294. **/
  295. static void
  296. lpfc_send_fastpath_evt(struct lpfc_hba *phba,
  297. struct lpfc_work_evt *evtp)
  298. {
  299. unsigned long evt_category, evt_sub_category;
  300. struct lpfc_fast_path_event *fast_evt_data;
  301. char *evt_data;
  302. uint32_t evt_data_size;
  303. struct Scsi_Host *shost;
  304. fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
  305. work_evt);
  306. evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
  307. evt_sub_category = (unsigned long) fast_evt_data->un.
  308. fabric_evt.subcategory;
  309. shost = lpfc_shost_from_vport(fast_evt_data->vport);
  310. if (evt_category == FC_REG_FABRIC_EVENT) {
  311. if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
  312. evt_data = (char *) &fast_evt_data->un.read_check_error;
  313. evt_data_size = sizeof(fast_evt_data->un.
  314. read_check_error);
  315. } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
  316. (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
  317. evt_data = (char *) &fast_evt_data->un.fabric_evt;
  318. evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
  319. } else {
  320. lpfc_free_fast_evt(phba, fast_evt_data);
  321. return;
  322. }
  323. } else if (evt_category == FC_REG_SCSI_EVENT) {
  324. switch (evt_sub_category) {
  325. case LPFC_EVENT_QFULL:
  326. case LPFC_EVENT_DEVBSY:
  327. evt_data = (char *) &fast_evt_data->un.scsi_evt;
  328. evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
  329. break;
  330. case LPFC_EVENT_CHECK_COND:
  331. evt_data = (char *) &fast_evt_data->un.check_cond_evt;
  332. evt_data_size = sizeof(fast_evt_data->un.
  333. check_cond_evt);
  334. break;
  335. case LPFC_EVENT_VARQUEDEPTH:
  336. evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
  337. evt_data_size = sizeof(fast_evt_data->un.
  338. queue_depth_evt);
  339. break;
  340. default:
  341. lpfc_free_fast_evt(phba, fast_evt_data);
  342. return;
  343. }
  344. } else {
  345. lpfc_free_fast_evt(phba, fast_evt_data);
  346. return;
  347. }
  348. fc_host_post_vendor_event(shost,
  349. fc_get_event_number(),
  350. evt_data_size,
  351. evt_data,
  352. LPFC_NL_VENDOR_ID);
  353. lpfc_free_fast_evt(phba, fast_evt_data);
  354. return;
  355. }
  356. static void
  357. lpfc_work_list_done(struct lpfc_hba *phba)
  358. {
  359. struct lpfc_work_evt *evtp = NULL;
  360. struct lpfc_nodelist *ndlp;
  361. int free_evt;
  362. spin_lock_irq(&phba->hbalock);
  363. while (!list_empty(&phba->work_list)) {
  364. list_remove_head((&phba->work_list), evtp, typeof(*evtp),
  365. evt_listp);
  366. spin_unlock_irq(&phba->hbalock);
  367. free_evt = 1;
  368. switch (evtp->evt) {
  369. case LPFC_EVT_ELS_RETRY:
  370. ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
  371. lpfc_els_retry_delay_handler(ndlp);
  372. free_evt = 0; /* evt is part of ndlp */
  373. /* decrement the node reference count held
  374. * for this queued work
  375. */
  376. lpfc_nlp_put(ndlp);
  377. break;
  378. case LPFC_EVT_DEV_LOSS:
  379. ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
  380. lpfc_dev_loss_tmo_handler(ndlp);
  381. free_evt = 0;
  382. /* decrement the node reference count held for
  383. * this queued work
  384. */
  385. lpfc_nlp_put(ndlp);
  386. break;
  387. case LPFC_EVT_ONLINE:
  388. if (phba->link_state < LPFC_LINK_DOWN)
  389. *(int *) (evtp->evt_arg1) = lpfc_online(phba);
  390. else
  391. *(int *) (evtp->evt_arg1) = 0;
  392. complete((struct completion *)(evtp->evt_arg2));
  393. break;
  394. case LPFC_EVT_OFFLINE_PREP:
  395. if (phba->link_state >= LPFC_LINK_DOWN)
  396. lpfc_offline_prep(phba);
  397. *(int *)(evtp->evt_arg1) = 0;
  398. complete((struct completion *)(evtp->evt_arg2));
  399. break;
  400. case LPFC_EVT_OFFLINE:
  401. lpfc_offline(phba);
  402. lpfc_sli_brdrestart(phba);
  403. *(int *)(evtp->evt_arg1) =
  404. lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
  405. lpfc_unblock_mgmt_io(phba);
  406. complete((struct completion *)(evtp->evt_arg2));
  407. break;
  408. case LPFC_EVT_WARM_START:
  409. lpfc_offline(phba);
  410. lpfc_reset_barrier(phba);
  411. lpfc_sli_brdreset(phba);
  412. lpfc_hba_down_post(phba);
  413. *(int *)(evtp->evt_arg1) =
  414. lpfc_sli_brdready(phba, HS_MBRDY);
  415. lpfc_unblock_mgmt_io(phba);
  416. complete((struct completion *)(evtp->evt_arg2));
  417. break;
  418. case LPFC_EVT_KILL:
  419. lpfc_offline(phba);
  420. *(int *)(evtp->evt_arg1)
  421. = (phba->pport->stopped)
  422. ? 0 : lpfc_sli_brdkill(phba);
  423. lpfc_unblock_mgmt_io(phba);
  424. complete((struct completion *)(evtp->evt_arg2));
  425. break;
  426. case LPFC_EVT_FASTPATH_MGMT_EVT:
  427. lpfc_send_fastpath_evt(phba, evtp);
  428. free_evt = 0;
  429. break;
  430. }
  431. if (free_evt)
  432. kfree(evtp);
  433. spin_lock_irq(&phba->hbalock);
  434. }
  435. spin_unlock_irq(&phba->hbalock);
  436. }
  437. static void
  438. lpfc_work_done(struct lpfc_hba *phba)
  439. {
  440. struct lpfc_sli_ring *pring;
  441. uint32_t ha_copy, status, control, work_port_events;
  442. struct lpfc_vport **vports;
  443. struct lpfc_vport *vport;
  444. int i;
  445. spin_lock_irq(&phba->hbalock);
  446. ha_copy = phba->work_ha;
  447. phba->work_ha = 0;
  448. spin_unlock_irq(&phba->hbalock);
  449. /* First, try to post the next mailbox command to SLI4 device */
  450. if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
  451. lpfc_sli4_post_async_mbox(phba);
  452. if (ha_copy & HA_ERATT)
  453. /* Handle the error attention event */
  454. lpfc_handle_eratt(phba);
  455. if (ha_copy & HA_MBATT)
  456. lpfc_sli_handle_mb_event(phba);
  457. if (ha_copy & HA_LATT)
  458. lpfc_handle_latt(phba);
  459. /* Process SLI4 events */
  460. if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
  461. if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
  462. lpfc_sli4_fcp_xri_abort_event_proc(phba);
  463. if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
  464. lpfc_sli4_els_xri_abort_event_proc(phba);
  465. if (phba->hba_flag & ASYNC_EVENT)
  466. lpfc_sli4_async_event_proc(phba);
  467. if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
  468. spin_lock_irq(&phba->hbalock);
  469. phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
  470. spin_unlock_irq(&phba->hbalock);
  471. lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
  472. }
  473. if (phba->hba_flag & HBA_RECEIVE_BUFFER)
  474. lpfc_sli4_handle_received_buffer(phba);
  475. }
  476. vports = lpfc_create_vport_work_array(phba);
  477. if (vports != NULL)
  478. for (i = 0; i <= phba->max_vports; i++) {
  479. /*
  480. * We could have no vports in array if unloading, so if
  481. * this happens then just use the pport
  482. */
  483. if (vports[i] == NULL && i == 0)
  484. vport = phba->pport;
  485. else
  486. vport = vports[i];
  487. if (vport == NULL)
  488. break;
  489. spin_lock_irq(&vport->work_port_lock);
  490. work_port_events = vport->work_port_events;
  491. vport->work_port_events &= ~work_port_events;
  492. spin_unlock_irq(&vport->work_port_lock);
  493. if (work_port_events & WORKER_DISC_TMO)
  494. lpfc_disc_timeout_handler(vport);
  495. if (work_port_events & WORKER_ELS_TMO)
  496. lpfc_els_timeout_handler(vport);
  497. if (work_port_events & WORKER_HB_TMO)
  498. lpfc_hb_timeout_handler(phba);
  499. if (work_port_events & WORKER_MBOX_TMO)
  500. lpfc_mbox_timeout_handler(phba);
  501. if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
  502. lpfc_unblock_fabric_iocbs(phba);
  503. if (work_port_events & WORKER_FDMI_TMO)
  504. lpfc_fdmi_timeout_handler(vport);
  505. if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
  506. lpfc_ramp_down_queue_handler(phba);
  507. if (work_port_events & WORKER_RAMP_UP_QUEUE)
  508. lpfc_ramp_up_queue_handler(phba);
  509. }
  510. lpfc_destroy_vport_work_array(phba, vports);
  511. pring = &phba->sli.ring[LPFC_ELS_RING];
  512. status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  513. status >>= (4*LPFC_ELS_RING);
  514. if ((status & HA_RXMASK)
  515. || (pring->flag & LPFC_DEFERRED_RING_EVENT)) {
  516. if (pring->flag & LPFC_STOP_IOCB_EVENT) {
  517. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  518. /* Set the lpfc data pending flag */
  519. set_bit(LPFC_DATA_READY, &phba->data_flags);
  520. } else {
  521. pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
  522. lpfc_sli_handle_slow_ring_event(phba, pring,
  523. (status &
  524. HA_RXMASK));
  525. }
  526. /*
  527. * Turn on Ring interrupts
  528. */
  529. if (phba->sli_rev <= LPFC_SLI_REV3) {
  530. spin_lock_irq(&phba->hbalock);
  531. control = readl(phba->HCregaddr);
  532. if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
  533. lpfc_debugfs_slow_ring_trc(phba,
  534. "WRK Enable ring: cntl:x%x hacopy:x%x",
  535. control, ha_copy, 0);
  536. control |= (HC_R0INT_ENA << LPFC_ELS_RING);
  537. writel(control, phba->HCregaddr);
  538. readl(phba->HCregaddr); /* flush */
  539. } else {
  540. lpfc_debugfs_slow_ring_trc(phba,
  541. "WRK Ring ok: cntl:x%x hacopy:x%x",
  542. control, ha_copy, 0);
  543. }
  544. spin_unlock_irq(&phba->hbalock);
  545. }
  546. }
  547. lpfc_work_list_done(phba);
  548. }
  549. int
  550. lpfc_do_work(void *p)
  551. {
  552. struct lpfc_hba *phba = p;
  553. int rc;
  554. set_user_nice(current, -20);
  555. phba->data_flags = 0;
  556. while (!kthread_should_stop()) {
  557. /* wait and check worker queue activities */
  558. rc = wait_event_interruptible(phba->work_waitq,
  559. (test_and_clear_bit(LPFC_DATA_READY,
  560. &phba->data_flags)
  561. || kthread_should_stop()));
  562. /* Signal wakeup shall terminate the worker thread */
  563. if (rc) {
  564. lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
  565. "0433 Wakeup on signal: rc=x%x\n", rc);
  566. break;
  567. }
  568. /* Attend pending lpfc data processing */
  569. lpfc_work_done(phba);
  570. }
  571. phba->worker_thread = NULL;
  572. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  573. "0432 Worker thread stopped.\n");
  574. return 0;
  575. }
  576. /*
  577. * This is only called to handle FC worker events. Since this a rare
  578. * occurance, we allocate a struct lpfc_work_evt structure here instead of
  579. * embedding it in the IOCB.
  580. */
  581. int
  582. lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
  583. uint32_t evt)
  584. {
  585. struct lpfc_work_evt *evtp;
  586. unsigned long flags;
  587. /*
  588. * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
  589. * be queued to worker thread for processing
  590. */
  591. evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
  592. if (!evtp)
  593. return 0;
  594. evtp->evt_arg1 = arg1;
  595. evtp->evt_arg2 = arg2;
  596. evtp->evt = evt;
  597. spin_lock_irqsave(&phba->hbalock, flags);
  598. list_add_tail(&evtp->evt_listp, &phba->work_list);
  599. spin_unlock_irqrestore(&phba->hbalock, flags);
  600. lpfc_worker_wake_up(phba);
  601. return 1;
  602. }
  603. void
  604. lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
  605. {
  606. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  607. struct lpfc_hba *phba = vport->phba;
  608. struct lpfc_nodelist *ndlp, *next_ndlp;
  609. int rc;
  610. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
  611. if (!NLP_CHK_NODE_ACT(ndlp))
  612. continue;
  613. if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
  614. continue;
  615. if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
  616. ((vport->port_type == LPFC_NPIV_PORT) &&
  617. (ndlp->nlp_DID == NameServer_DID)))
  618. lpfc_unreg_rpi(vport, ndlp);
  619. /* Leave Fabric nodes alone on link down */
  620. if (!remove && ndlp->nlp_type & NLP_FABRIC)
  621. continue;
  622. rc = lpfc_disc_state_machine(vport, ndlp, NULL,
  623. remove
  624. ? NLP_EVT_DEVICE_RM
  625. : NLP_EVT_DEVICE_RECOVERY);
  626. }
  627. if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
  628. lpfc_mbx_unreg_vpi(vport);
  629. spin_lock_irq(shost->host_lock);
  630. vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
  631. spin_unlock_irq(shost->host_lock);
  632. }
  633. }
  634. void
  635. lpfc_port_link_failure(struct lpfc_vport *vport)
  636. {
  637. /* Cleanup any outstanding RSCN activity */
  638. lpfc_els_flush_rscn(vport);
  639. /* Cleanup any outstanding ELS commands */
  640. lpfc_els_flush_cmd(vport);
  641. lpfc_cleanup_rpis(vport, 0);
  642. /* Turn off discovery timer if its running */
  643. lpfc_can_disctmo(vport);
  644. }
  645. void
  646. lpfc_linkdown_port(struct lpfc_vport *vport)
  647. {
  648. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  649. fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
  650. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
  651. "Link Down: state:x%x rtry:x%x flg:x%x",
  652. vport->port_state, vport->fc_ns_retry, vport->fc_flag);
  653. lpfc_port_link_failure(vport);
  654. }
  655. int
  656. lpfc_linkdown(struct lpfc_hba *phba)
  657. {
  658. struct lpfc_vport *vport = phba->pport;
  659. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  660. struct lpfc_vport **vports;
  661. LPFC_MBOXQ_t *mb;
  662. int i;
  663. if (phba->link_state == LPFC_LINK_DOWN)
  664. return 0;
  665. spin_lock_irq(&phba->hbalock);
  666. phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_DISCOVERED);
  667. if (phba->link_state > LPFC_LINK_DOWN) {
  668. phba->link_state = LPFC_LINK_DOWN;
  669. phba->pport->fc_flag &= ~FC_LBIT;
  670. }
  671. spin_unlock_irq(&phba->hbalock);
  672. vports = lpfc_create_vport_work_array(phba);
  673. if (vports != NULL)
  674. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  675. /* Issue a LINK DOWN event to all nodes */
  676. lpfc_linkdown_port(vports[i]);
  677. }
  678. lpfc_destroy_vport_work_array(phba, vports);
  679. /* Clean up any firmware default rpi's */
  680. mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  681. if (mb) {
  682. lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
  683. mb->vport = vport;
  684. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  685. if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
  686. == MBX_NOT_FINISHED) {
  687. mempool_free(mb, phba->mbox_mem_pool);
  688. }
  689. }
  690. /* Setup myDID for link up if we are in pt2pt mode */
  691. if (phba->pport->fc_flag & FC_PT2PT) {
  692. phba->pport->fc_myDID = 0;
  693. mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  694. if (mb) {
  695. lpfc_config_link(phba, mb);
  696. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  697. mb->vport = vport;
  698. if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
  699. == MBX_NOT_FINISHED) {
  700. mempool_free(mb, phba->mbox_mem_pool);
  701. }
  702. }
  703. spin_lock_irq(shost->host_lock);
  704. phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
  705. spin_unlock_irq(shost->host_lock);
  706. }
  707. return 0;
  708. }
  709. static void
  710. lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
  711. {
  712. struct lpfc_nodelist *ndlp;
  713. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  714. if (!NLP_CHK_NODE_ACT(ndlp))
  715. continue;
  716. if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
  717. continue;
  718. if (ndlp->nlp_type & NLP_FABRIC) {
  719. /* On Linkup its safe to clean up the ndlp
  720. * from Fabric connections.
  721. */
  722. if (ndlp->nlp_DID != Fabric_DID)
  723. lpfc_unreg_rpi(vport, ndlp);
  724. lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
  725. } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
  726. /* Fail outstanding IO now since device is
  727. * marked for PLOGI.
  728. */
  729. lpfc_unreg_rpi(vport, ndlp);
  730. }
  731. }
  732. }
  733. static void
  734. lpfc_linkup_port(struct lpfc_vport *vport)
  735. {
  736. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  737. struct lpfc_hba *phba = vport->phba;
  738. if ((vport->load_flag & FC_UNLOADING) != 0)
  739. return;
  740. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
  741. "Link Up: top:x%x speed:x%x flg:x%x",
  742. phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
  743. /* If NPIV is not enabled, only bring the physical port up */
  744. if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
  745. (vport != phba->pport))
  746. return;
  747. fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
  748. spin_lock_irq(shost->host_lock);
  749. vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
  750. FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
  751. vport->fc_flag |= FC_NDISC_ACTIVE;
  752. vport->fc_ns_retry = 0;
  753. spin_unlock_irq(shost->host_lock);
  754. if (vport->fc_flag & FC_LBIT)
  755. lpfc_linkup_cleanup_nodes(vport);
  756. }
  757. static int
  758. lpfc_linkup(struct lpfc_hba *phba)
  759. {
  760. struct lpfc_vport **vports;
  761. int i;
  762. phba->link_state = LPFC_LINK_UP;
  763. /* Unblock fabric iocbs if they are blocked */
  764. clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
  765. del_timer_sync(&phba->fabric_block_timer);
  766. vports = lpfc_create_vport_work_array(phba);
  767. if (vports != NULL)
  768. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
  769. lpfc_linkup_port(vports[i]);
  770. lpfc_destroy_vport_work_array(phba, vports);
  771. if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
  772. (phba->sli_rev < LPFC_SLI_REV4))
  773. lpfc_issue_clear_la(phba, phba->pport);
  774. return 0;
  775. }
  776. /*
  777. * This routine handles processing a CLEAR_LA mailbox
  778. * command upon completion. It is setup in the LPFC_MBOXQ
  779. * as the completion routine when the command is
  780. * handed off to the SLI layer.
  781. */
  782. static void
  783. lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  784. {
  785. struct lpfc_vport *vport = pmb->vport;
  786. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  787. struct lpfc_sli *psli = &phba->sli;
  788. MAILBOX_t *mb = &pmb->u.mb;
  789. uint32_t control;
  790. /* Since we don't do discovery right now, turn these off here */
  791. psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
  792. psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
  793. psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
  794. /* Check for error */
  795. if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
  796. /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
  797. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  798. "0320 CLEAR_LA mbxStatus error x%x hba "
  799. "state x%x\n",
  800. mb->mbxStatus, vport->port_state);
  801. phba->link_state = LPFC_HBA_ERROR;
  802. goto out;
  803. }
  804. if (vport->port_type == LPFC_PHYSICAL_PORT)
  805. phba->link_state = LPFC_HBA_READY;
  806. spin_lock_irq(&phba->hbalock);
  807. psli->sli_flag |= LPFC_PROCESS_LA;
  808. control = readl(phba->HCregaddr);
  809. control |= HC_LAINT_ENA;
  810. writel(control, phba->HCregaddr);
  811. readl(phba->HCregaddr); /* flush */
  812. spin_unlock_irq(&phba->hbalock);
  813. mempool_free(pmb, phba->mbox_mem_pool);
  814. return;
  815. out:
  816. /* Device Discovery completes */
  817. lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
  818. "0225 Device Discovery completes\n");
  819. mempool_free(pmb, phba->mbox_mem_pool);
  820. spin_lock_irq(shost->host_lock);
  821. vport->fc_flag &= ~FC_ABORT_DISCOVERY;
  822. spin_unlock_irq(shost->host_lock);
  823. lpfc_can_disctmo(vport);
  824. /* turn on Link Attention interrupts */
  825. spin_lock_irq(&phba->hbalock);
  826. psli->sli_flag |= LPFC_PROCESS_LA;
  827. control = readl(phba->HCregaddr);
  828. control |= HC_LAINT_ENA;
  829. writel(control, phba->HCregaddr);
  830. readl(phba->HCregaddr); /* flush */
  831. spin_unlock_irq(&phba->hbalock);
  832. return;
  833. }
  834. static void
  835. lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  836. {
  837. struct lpfc_vport *vport = pmb->vport;
  838. if (pmb->u.mb.mbxStatus)
  839. goto out;
  840. mempool_free(pmb, phba->mbox_mem_pool);
  841. if (phba->fc_topology == TOPOLOGY_LOOP &&
  842. vport->fc_flag & FC_PUBLIC_LOOP &&
  843. !(vport->fc_flag & FC_LBIT)) {
  844. /* Need to wait for FAN - use discovery timer
  845. * for timeout. port_state is identically
  846. * LPFC_LOCAL_CFG_LINK while waiting for FAN
  847. */
  848. lpfc_set_disctmo(vport);
  849. return;
  850. }
  851. /* Start discovery by sending a FLOGI. port_state is identically
  852. * LPFC_FLOGI while waiting for FLOGI cmpl
  853. */
  854. if (vport->port_state != LPFC_FLOGI) {
  855. lpfc_initial_flogi(vport);
  856. }
  857. return;
  858. out:
  859. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  860. "0306 CONFIG_LINK mbxStatus error x%x "
  861. "HBA state x%x\n",
  862. pmb->u.mb.mbxStatus, vport->port_state);
  863. mempool_free(pmb, phba->mbox_mem_pool);
  864. lpfc_linkdown(phba);
  865. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  866. "0200 CONFIG_LINK bad hba state x%x\n",
  867. vport->port_state);
  868. lpfc_issue_clear_la(phba, vport);
  869. return;
  870. }
  871. static void
  872. lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  873. {
  874. struct lpfc_vport *vport = mboxq->vport;
  875. unsigned long flags;
  876. if (mboxq->u.mb.mbxStatus) {
  877. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  878. "2017 REG_FCFI mbxStatus error x%x "
  879. "HBA state x%x\n",
  880. mboxq->u.mb.mbxStatus, vport->port_state);
  881. mempool_free(mboxq, phba->mbox_mem_pool);
  882. return;
  883. }
  884. /* Start FCoE discovery by sending a FLOGI. */
  885. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
  886. /* Set the FCFI registered flag */
  887. spin_lock_irqsave(&phba->hbalock, flags);
  888. phba->fcf.fcf_flag |= FCF_REGISTERED;
  889. spin_unlock_irqrestore(&phba->hbalock, flags);
  890. if (vport->port_state != LPFC_FLOGI) {
  891. spin_lock_irqsave(&phba->hbalock, flags);
  892. phba->fcf.fcf_flag |= (FCF_DISCOVERED | FCF_IN_USE);
  893. spin_unlock_irqrestore(&phba->hbalock, flags);
  894. lpfc_initial_flogi(vport);
  895. }
  896. mempool_free(mboxq, phba->mbox_mem_pool);
  897. return;
  898. }
  899. /**
  900. * lpfc_fab_name_match - Check if the fcf fabric name match.
  901. * @fab_name: pointer to fabric name.
  902. * @new_fcf_record: pointer to fcf record.
  903. *
  904. * This routine compare the fcf record's fabric name with provided
  905. * fabric name. If the fabric name are identical this function
  906. * returns 1 else return 0.
  907. **/
  908. static uint32_t
  909. lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
  910. {
  911. if ((fab_name[0] ==
  912. bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record)) &&
  913. (fab_name[1] ==
  914. bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record)) &&
  915. (fab_name[2] ==
  916. bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record)) &&
  917. (fab_name[3] ==
  918. bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record)) &&
  919. (fab_name[4] ==
  920. bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record)) &&
  921. (fab_name[5] ==
  922. bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record)) &&
  923. (fab_name[6] ==
  924. bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record)) &&
  925. (fab_name[7] ==
  926. bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record)))
  927. return 1;
  928. else
  929. return 0;
  930. }
  931. /**
  932. * lpfc_mac_addr_match - Check if the fcf mac address match.
  933. * @phba: pointer to lpfc hba data structure.
  934. * @new_fcf_record: pointer to fcf record.
  935. *
  936. * This routine compare the fcf record's mac address with HBA's
  937. * FCF mac address. If the mac addresses are identical this function
  938. * returns 1 else return 0.
  939. **/
  940. static uint32_t
  941. lpfc_mac_addr_match(struct lpfc_hba *phba, struct fcf_record *new_fcf_record)
  942. {
  943. if ((phba->fcf.mac_addr[0] ==
  944. bf_get(lpfc_fcf_record_mac_0, new_fcf_record)) &&
  945. (phba->fcf.mac_addr[1] ==
  946. bf_get(lpfc_fcf_record_mac_1, new_fcf_record)) &&
  947. (phba->fcf.mac_addr[2] ==
  948. bf_get(lpfc_fcf_record_mac_2, new_fcf_record)) &&
  949. (phba->fcf.mac_addr[3] ==
  950. bf_get(lpfc_fcf_record_mac_3, new_fcf_record)) &&
  951. (phba->fcf.mac_addr[4] ==
  952. bf_get(lpfc_fcf_record_mac_4, new_fcf_record)) &&
  953. (phba->fcf.mac_addr[5] ==
  954. bf_get(lpfc_fcf_record_mac_5, new_fcf_record)))
  955. return 1;
  956. else
  957. return 0;
  958. }
  959. /**
  960. * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
  961. * @phba: pointer to lpfc hba data structure.
  962. * @new_fcf_record: pointer to fcf record.
  963. *
  964. * This routine copies the FCF information from the FCF
  965. * record to lpfc_hba data structure.
  966. **/
  967. static void
  968. lpfc_copy_fcf_record(struct lpfc_hba *phba, struct fcf_record *new_fcf_record)
  969. {
  970. phba->fcf.fabric_name[0] =
  971. bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
  972. phba->fcf.fabric_name[1] =
  973. bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
  974. phba->fcf.fabric_name[2] =
  975. bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
  976. phba->fcf.fabric_name[3] =
  977. bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
  978. phba->fcf.fabric_name[4] =
  979. bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
  980. phba->fcf.fabric_name[5] =
  981. bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
  982. phba->fcf.fabric_name[6] =
  983. bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
  984. phba->fcf.fabric_name[7] =
  985. bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
  986. phba->fcf.mac_addr[0] =
  987. bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
  988. phba->fcf.mac_addr[1] =
  989. bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
  990. phba->fcf.mac_addr[2] =
  991. bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
  992. phba->fcf.mac_addr[3] =
  993. bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
  994. phba->fcf.mac_addr[4] =
  995. bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
  996. phba->fcf.mac_addr[5] =
  997. bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
  998. phba->fcf.fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
  999. phba->fcf.priority = new_fcf_record->fip_priority;
  1000. }
  1001. /**
  1002. * lpfc_register_fcf - Register the FCF with hba.
  1003. * @phba: pointer to lpfc hba data structure.
  1004. *
  1005. * This routine issues a register fcfi mailbox command to register
  1006. * the fcf with HBA.
  1007. **/
  1008. static void
  1009. lpfc_register_fcf(struct lpfc_hba *phba)
  1010. {
  1011. LPFC_MBOXQ_t *fcf_mbxq;
  1012. int rc;
  1013. unsigned long flags;
  1014. spin_lock_irqsave(&phba->hbalock, flags);
  1015. /* If the FCF is not availabe do nothing. */
  1016. if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
  1017. spin_unlock_irqrestore(&phba->hbalock, flags);
  1018. return;
  1019. }
  1020. /* The FCF is already registered, start discovery */
  1021. if (phba->fcf.fcf_flag & FCF_REGISTERED) {
  1022. phba->fcf.fcf_flag |= (FCF_DISCOVERED | FCF_IN_USE);
  1023. spin_unlock_irqrestore(&phba->hbalock, flags);
  1024. if (phba->pport->port_state != LPFC_FLOGI)
  1025. lpfc_initial_flogi(phba->pport);
  1026. return;
  1027. }
  1028. spin_unlock_irqrestore(&phba->hbalock, flags);
  1029. fcf_mbxq = mempool_alloc(phba->mbox_mem_pool,
  1030. GFP_KERNEL);
  1031. if (!fcf_mbxq)
  1032. return;
  1033. lpfc_reg_fcfi(phba, fcf_mbxq);
  1034. fcf_mbxq->vport = phba->pport;
  1035. fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
  1036. rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
  1037. if (rc == MBX_NOT_FINISHED)
  1038. mempool_free(fcf_mbxq, phba->mbox_mem_pool);
  1039. return;
  1040. }
  1041. /**
  1042. * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
  1043. * @phba: pointer to lpfc hba data structure.
  1044. * @new_fcf_record: pointer to fcf record.
  1045. * @boot_flag: Indicates if this record used by boot bios.
  1046. * @addr_mode: The address mode to be used by this FCF
  1047. *
  1048. * This routine compare the fcf record with connect list obtained from the
  1049. * config region to decide if this FCF can be used for SAN discovery. It returns
  1050. * 1 if this record can be used for SAN discovery else return zero. If this FCF
  1051. * record can be used for SAN discovery, the boot_flag will indicate if this FCF
  1052. * is used by boot bios and addr_mode will indicate the addressing mode to be
  1053. * used for this FCF when the function returns.
  1054. * If the FCF record need to be used with a particular vlan id, the vlan is
  1055. * set in the vlan_id on return of the function. If not VLAN tagging need to
  1056. * be used with the FCF vlan_id will be set to 0xFFFF;
  1057. **/
  1058. static int
  1059. lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
  1060. struct fcf_record *new_fcf_record,
  1061. uint32_t *boot_flag, uint32_t *addr_mode,
  1062. uint16_t *vlan_id)
  1063. {
  1064. struct lpfc_fcf_conn_entry *conn_entry;
  1065. /* If FCF not available return 0 */
  1066. if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
  1067. !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record))
  1068. return 0;
  1069. if (!phba->cfg_enable_fip) {
  1070. *boot_flag = 0;
  1071. *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
  1072. new_fcf_record);
  1073. if (phba->valid_vlan)
  1074. *vlan_id = phba->vlan_id;
  1075. else
  1076. *vlan_id = 0xFFFF;
  1077. return 1;
  1078. }
  1079. /*
  1080. * If there are no FCF connection table entry, driver connect to all
  1081. * FCFs.
  1082. */
  1083. if (list_empty(&phba->fcf_conn_rec_list)) {
  1084. *boot_flag = 0;
  1085. *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
  1086. new_fcf_record);
  1087. /*
  1088. * When there are no FCF connect entries, use driver's default
  1089. * addressing mode - FPMA.
  1090. */
  1091. if (*addr_mode & LPFC_FCF_FPMA)
  1092. *addr_mode = LPFC_FCF_FPMA;
  1093. *vlan_id = 0xFFFF;
  1094. return 1;
  1095. }
  1096. list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list, list) {
  1097. if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
  1098. continue;
  1099. if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
  1100. !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
  1101. new_fcf_record))
  1102. continue;
  1103. if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
  1104. /*
  1105. * If the vlan bit map does not have the bit set for the
  1106. * vlan id to be used, then it is not a match.
  1107. */
  1108. if (!(new_fcf_record->vlan_bitmap
  1109. [conn_entry->conn_rec.vlan_tag / 8] &
  1110. (1 << (conn_entry->conn_rec.vlan_tag % 8))))
  1111. continue;
  1112. }
  1113. /*
  1114. * If connection record does not support any addressing mode,
  1115. * skip the FCF record.
  1116. */
  1117. if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
  1118. & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
  1119. continue;
  1120. /*
  1121. * Check if the connection record specifies a required
  1122. * addressing mode.
  1123. */
  1124. if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
  1125. !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
  1126. /*
  1127. * If SPMA required but FCF not support this continue.
  1128. */
  1129. if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
  1130. !(bf_get(lpfc_fcf_record_mac_addr_prov,
  1131. new_fcf_record) & LPFC_FCF_SPMA))
  1132. continue;
  1133. /*
  1134. * If FPMA required but FCF not support this continue.
  1135. */
  1136. if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
  1137. !(bf_get(lpfc_fcf_record_mac_addr_prov,
  1138. new_fcf_record) & LPFC_FCF_FPMA))
  1139. continue;
  1140. }
  1141. /*
  1142. * This fcf record matches filtering criteria.
  1143. */
  1144. if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
  1145. *boot_flag = 1;
  1146. else
  1147. *boot_flag = 0;
  1148. /*
  1149. * If user did not specify any addressing mode, or if the
  1150. * prefered addressing mode specified by user is not supported
  1151. * by FCF, allow fabric to pick the addressing mode.
  1152. */
  1153. *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
  1154. new_fcf_record);
  1155. /*
  1156. * If the user specified a required address mode, assign that
  1157. * address mode
  1158. */
  1159. if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
  1160. (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
  1161. *addr_mode = (conn_entry->conn_rec.flags &
  1162. FCFCNCT_AM_SPMA) ?
  1163. LPFC_FCF_SPMA : LPFC_FCF_FPMA;
  1164. /*
  1165. * If the user specified a prefered address mode, use the
  1166. * addr mode only if FCF support the addr_mode.
  1167. */
  1168. else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
  1169. (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
  1170. (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
  1171. (*addr_mode & LPFC_FCF_SPMA))
  1172. *addr_mode = LPFC_FCF_SPMA;
  1173. else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
  1174. (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
  1175. !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
  1176. (*addr_mode & LPFC_FCF_FPMA))
  1177. *addr_mode = LPFC_FCF_FPMA;
  1178. if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
  1179. *vlan_id = conn_entry->conn_rec.vlan_tag;
  1180. else
  1181. *vlan_id = 0xFFFF;
  1182. return 1;
  1183. }
  1184. return 0;
  1185. }
  1186. /**
  1187. * lpfc_mbx_cmpl_read_fcf_record - Completion handler for read_fcf mbox.
  1188. * @phba: pointer to lpfc hba data structure.
  1189. * @mboxq: pointer to mailbox object.
  1190. *
  1191. * This function iterate through all the fcf records available in
  1192. * HBA and choose the optimal FCF record for discovery. After finding
  1193. * the FCF for discovery it register the FCF record and kick start
  1194. * discovery.
  1195. * If FCF_IN_USE flag is set in currently used FCF, the routine try to
  1196. * use a FCF record which match fabric name and mac address of the
  1197. * currently used FCF record.
  1198. * If the driver support only one FCF, it will try to use the FCF record
  1199. * used by BOOT_BIOS.
  1200. */
  1201. void
  1202. lpfc_mbx_cmpl_read_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  1203. {
  1204. void *virt_addr;
  1205. dma_addr_t phys_addr;
  1206. uint8_t *bytep;
  1207. struct lpfc_mbx_sge sge;
  1208. struct lpfc_mbx_read_fcf_tbl *read_fcf;
  1209. uint32_t shdr_status, shdr_add_status;
  1210. union lpfc_sli4_cfg_shdr *shdr;
  1211. struct fcf_record *new_fcf_record;
  1212. int rc;
  1213. uint32_t boot_flag, addr_mode;
  1214. uint32_t next_fcf_index;
  1215. unsigned long flags;
  1216. uint16_t vlan_id;
  1217. /* Get the first SGE entry from the non-embedded DMA memory. This
  1218. * routine only uses a single SGE.
  1219. */
  1220. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  1221. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  1222. if (unlikely(!mboxq->sge_array)) {
  1223. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  1224. "2524 Failed to get the non-embedded SGE "
  1225. "virtual address\n");
  1226. goto out;
  1227. }
  1228. virt_addr = mboxq->sge_array->addr[0];
  1229. shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
  1230. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  1231. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  1232. &shdr->response);
  1233. /*
  1234. * The FCF Record was read and there is no reason for the driver
  1235. * to maintain the FCF record data or memory. Instead, just need
  1236. * to book keeping the FCFIs can be used.
  1237. */
  1238. if (shdr_status || shdr_add_status) {
  1239. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1240. "2521 READ_FCF_RECORD mailbox failed "
  1241. "with status x%x add_status x%x, mbx\n",
  1242. shdr_status, shdr_add_status);
  1243. goto out;
  1244. }
  1245. /* Interpreting the returned information of FCF records */
  1246. read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
  1247. lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
  1248. sizeof(struct lpfc_mbx_read_fcf_tbl));
  1249. next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
  1250. new_fcf_record = (struct fcf_record *)(virt_addr +
  1251. sizeof(struct lpfc_mbx_read_fcf_tbl));
  1252. lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
  1253. sizeof(struct fcf_record));
  1254. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  1255. rc = lpfc_match_fcf_conn_list(phba, new_fcf_record,
  1256. &boot_flag, &addr_mode,
  1257. &vlan_id);
  1258. /*
  1259. * If the fcf record does not match with connect list entries
  1260. * read the next entry.
  1261. */
  1262. if (!rc)
  1263. goto read_next_fcf;
  1264. /*
  1265. * If this is not the first FCF discovery of the HBA, use last
  1266. * FCF record for the discovery.
  1267. */
  1268. spin_lock_irqsave(&phba->hbalock, flags);
  1269. if (phba->fcf.fcf_flag & FCF_IN_USE) {
  1270. if (lpfc_fab_name_match(phba->fcf.fabric_name,
  1271. new_fcf_record) &&
  1272. lpfc_mac_addr_match(phba, new_fcf_record)) {
  1273. phba->fcf.fcf_flag |= FCF_AVAILABLE;
  1274. spin_unlock_irqrestore(&phba->hbalock, flags);
  1275. goto out;
  1276. }
  1277. spin_unlock_irqrestore(&phba->hbalock, flags);
  1278. goto read_next_fcf;
  1279. }
  1280. if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
  1281. /*
  1282. * If the current FCF record does not have boot flag
  1283. * set and new fcf record has boot flag set, use the
  1284. * new fcf record.
  1285. */
  1286. if (boot_flag && !(phba->fcf.fcf_flag & FCF_BOOT_ENABLE)) {
  1287. /* Use this FCF record */
  1288. lpfc_copy_fcf_record(phba, new_fcf_record);
  1289. phba->fcf.addr_mode = addr_mode;
  1290. phba->fcf.fcf_flag |= FCF_BOOT_ENABLE;
  1291. if (vlan_id != 0xFFFF) {
  1292. phba->fcf.fcf_flag |= FCF_VALID_VLAN;
  1293. phba->fcf.vlan_id = vlan_id;
  1294. }
  1295. spin_unlock_irqrestore(&phba->hbalock, flags);
  1296. goto read_next_fcf;
  1297. }
  1298. /*
  1299. * If the current FCF record has boot flag set and the
  1300. * new FCF record does not have boot flag, read the next
  1301. * FCF record.
  1302. */
  1303. if (!boot_flag && (phba->fcf.fcf_flag & FCF_BOOT_ENABLE)) {
  1304. spin_unlock_irqrestore(&phba->hbalock, flags);
  1305. goto read_next_fcf;
  1306. }
  1307. /*
  1308. * If there is a record with lower priority value for
  1309. * the current FCF, use that record.
  1310. */
  1311. if (lpfc_fab_name_match(phba->fcf.fabric_name, new_fcf_record)
  1312. && (new_fcf_record->fip_priority <
  1313. phba->fcf.priority)) {
  1314. /* Use this FCF record */
  1315. lpfc_copy_fcf_record(phba, new_fcf_record);
  1316. phba->fcf.addr_mode = addr_mode;
  1317. if (vlan_id != 0xFFFF) {
  1318. phba->fcf.fcf_flag |= FCF_VALID_VLAN;
  1319. phba->fcf.vlan_id = vlan_id;
  1320. }
  1321. spin_unlock_irqrestore(&phba->hbalock, flags);
  1322. goto read_next_fcf;
  1323. }
  1324. spin_unlock_irqrestore(&phba->hbalock, flags);
  1325. goto read_next_fcf;
  1326. }
  1327. /*
  1328. * This is the first available FCF record, use this
  1329. * record.
  1330. */
  1331. lpfc_copy_fcf_record(phba, new_fcf_record);
  1332. phba->fcf.addr_mode = addr_mode;
  1333. if (boot_flag)
  1334. phba->fcf.fcf_flag |= FCF_BOOT_ENABLE;
  1335. phba->fcf.fcf_flag |= FCF_AVAILABLE;
  1336. if (vlan_id != 0xFFFF) {
  1337. phba->fcf.fcf_flag |= FCF_VALID_VLAN;
  1338. phba->fcf.vlan_id = vlan_id;
  1339. }
  1340. spin_unlock_irqrestore(&phba->hbalock, flags);
  1341. goto read_next_fcf;
  1342. read_next_fcf:
  1343. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  1344. if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0)
  1345. lpfc_register_fcf(phba);
  1346. else
  1347. lpfc_sli4_read_fcf_record(phba, next_fcf_index);
  1348. return;
  1349. out:
  1350. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  1351. lpfc_register_fcf(phba);
  1352. return;
  1353. }
  1354. /**
  1355. * lpfc_start_fdiscs - send fdiscs for each vports on this port.
  1356. * @phba: pointer to lpfc hba data structure.
  1357. *
  1358. * This function loops through the list of vports on the @phba and issues an
  1359. * FDISC if possible.
  1360. */
  1361. void
  1362. lpfc_start_fdiscs(struct lpfc_hba *phba)
  1363. {
  1364. struct lpfc_vport **vports;
  1365. int i;
  1366. vports = lpfc_create_vport_work_array(phba);
  1367. if (vports != NULL) {
  1368. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  1369. if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
  1370. continue;
  1371. /* There are no vpi for this vport */
  1372. if (vports[i]->vpi > phba->max_vpi) {
  1373. lpfc_vport_set_state(vports[i],
  1374. FC_VPORT_FAILED);
  1375. continue;
  1376. }
  1377. if (phba->fc_topology == TOPOLOGY_LOOP) {
  1378. lpfc_vport_set_state(vports[i],
  1379. FC_VPORT_LINKDOWN);
  1380. continue;
  1381. }
  1382. if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
  1383. lpfc_initial_fdisc(vports[i]);
  1384. else {
  1385. lpfc_vport_set_state(vports[i],
  1386. FC_VPORT_NO_FABRIC_SUPP);
  1387. lpfc_printf_vlog(vports[i], KERN_ERR,
  1388. LOG_ELS,
  1389. "0259 No NPIV "
  1390. "Fabric support\n");
  1391. }
  1392. }
  1393. }
  1394. lpfc_destroy_vport_work_array(phba, vports);
  1395. }
  1396. void
  1397. lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  1398. {
  1399. struct lpfc_dmabuf *dmabuf = mboxq->context1;
  1400. struct lpfc_vport *vport = mboxq->vport;
  1401. if (mboxq->u.mb.mbxStatus) {
  1402. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  1403. "2018 REG_VFI mbxStatus error x%x "
  1404. "HBA state x%x\n",
  1405. mboxq->u.mb.mbxStatus, vport->port_state);
  1406. if (phba->fc_topology == TOPOLOGY_LOOP) {
  1407. /* FLOGI failed, use loop map to make discovery list */
  1408. lpfc_disc_list_loopmap(vport);
  1409. /* Start discovery */
  1410. lpfc_disc_start(vport);
  1411. goto fail_free_mem;
  1412. }
  1413. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  1414. goto fail_free_mem;
  1415. }
  1416. /* Mark the vport has registered with its VFI */
  1417. vport->vfi_state |= LPFC_VFI_REGISTERED;
  1418. if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
  1419. lpfc_start_fdiscs(phba);
  1420. lpfc_do_scr_ns_plogi(phba, vport);
  1421. }
  1422. fail_free_mem:
  1423. mempool_free(mboxq, phba->mbox_mem_pool);
  1424. lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
  1425. kfree(dmabuf);
  1426. return;
  1427. }
  1428. static void
  1429. lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1430. {
  1431. MAILBOX_t *mb = &pmb->u.mb;
  1432. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
  1433. struct lpfc_vport *vport = pmb->vport;
  1434. /* Check for error */
  1435. if (mb->mbxStatus) {
  1436. /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
  1437. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  1438. "0319 READ_SPARAM mbxStatus error x%x "
  1439. "hba state x%x>\n",
  1440. mb->mbxStatus, vport->port_state);
  1441. lpfc_linkdown(phba);
  1442. goto out;
  1443. }
  1444. memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
  1445. sizeof (struct serv_parm));
  1446. if (phba->cfg_soft_wwnn)
  1447. u64_to_wwn(phba->cfg_soft_wwnn,
  1448. vport->fc_sparam.nodeName.u.wwn);
  1449. if (phba->cfg_soft_wwpn)
  1450. u64_to_wwn(phba->cfg_soft_wwpn,
  1451. vport->fc_sparam.portName.u.wwn);
  1452. memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
  1453. sizeof(vport->fc_nodename));
  1454. memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
  1455. sizeof(vport->fc_portname));
  1456. if (vport->port_type == LPFC_PHYSICAL_PORT) {
  1457. memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
  1458. memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
  1459. }
  1460. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1461. kfree(mp);
  1462. mempool_free(pmb, phba->mbox_mem_pool);
  1463. return;
  1464. out:
  1465. pmb->context1 = NULL;
  1466. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1467. kfree(mp);
  1468. lpfc_issue_clear_la(phba, vport);
  1469. mempool_free(pmb, phba->mbox_mem_pool);
  1470. return;
  1471. }
  1472. static void
  1473. lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
  1474. {
  1475. struct lpfc_vport *vport = phba->pport;
  1476. LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
  1477. int i;
  1478. struct lpfc_dmabuf *mp;
  1479. int rc;
  1480. struct fcf_record *fcf_record;
  1481. sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1482. spin_lock_irq(&phba->hbalock);
  1483. switch (la->UlnkSpeed) {
  1484. case LA_1GHZ_LINK:
  1485. phba->fc_linkspeed = LA_1GHZ_LINK;
  1486. break;
  1487. case LA_2GHZ_LINK:
  1488. phba->fc_linkspeed = LA_2GHZ_LINK;
  1489. break;
  1490. case LA_4GHZ_LINK:
  1491. phba->fc_linkspeed = LA_4GHZ_LINK;
  1492. break;
  1493. case LA_8GHZ_LINK:
  1494. phba->fc_linkspeed = LA_8GHZ_LINK;
  1495. break;
  1496. case LA_10GHZ_LINK:
  1497. phba->fc_linkspeed = LA_10GHZ_LINK;
  1498. break;
  1499. default:
  1500. phba->fc_linkspeed = LA_UNKNW_LINK;
  1501. break;
  1502. }
  1503. phba->fc_topology = la->topology;
  1504. phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
  1505. if (phba->fc_topology == TOPOLOGY_LOOP) {
  1506. phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
  1507. if (phba->cfg_enable_npiv)
  1508. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  1509. "1309 Link Up Event npiv not supported in loop "
  1510. "topology\n");
  1511. /* Get Loop Map information */
  1512. if (la->il)
  1513. vport->fc_flag |= FC_LBIT;
  1514. vport->fc_myDID = la->granted_AL_PA;
  1515. i = la->un.lilpBde64.tus.f.bdeSize;
  1516. if (i == 0) {
  1517. phba->alpa_map[0] = 0;
  1518. } else {
  1519. if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
  1520. int numalpa, j, k;
  1521. union {
  1522. uint8_t pamap[16];
  1523. struct {
  1524. uint32_t wd1;
  1525. uint32_t wd2;
  1526. uint32_t wd3;
  1527. uint32_t wd4;
  1528. } pa;
  1529. } un;
  1530. numalpa = phba->alpa_map[0];
  1531. j = 0;
  1532. while (j < numalpa) {
  1533. memset(un.pamap, 0, 16);
  1534. for (k = 1; j < numalpa; k++) {
  1535. un.pamap[k - 1] =
  1536. phba->alpa_map[j + 1];
  1537. j++;
  1538. if (k == 16)
  1539. break;
  1540. }
  1541. /* Link Up Event ALPA map */
  1542. lpfc_printf_log(phba,
  1543. KERN_WARNING,
  1544. LOG_LINK_EVENT,
  1545. "1304 Link Up Event "
  1546. "ALPA map Data: x%x "
  1547. "x%x x%x x%x\n",
  1548. un.pa.wd1, un.pa.wd2,
  1549. un.pa.wd3, un.pa.wd4);
  1550. }
  1551. }
  1552. }
  1553. } else {
  1554. if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
  1555. if (phba->max_vpi && phba->cfg_enable_npiv &&
  1556. (phba->sli_rev == 3))
  1557. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  1558. }
  1559. vport->fc_myDID = phba->fc_pref_DID;
  1560. vport->fc_flag |= FC_LBIT;
  1561. }
  1562. spin_unlock_irq(&phba->hbalock);
  1563. lpfc_linkup(phba);
  1564. if (sparam_mbox) {
  1565. lpfc_read_sparam(phba, sparam_mbox, 0);
  1566. sparam_mbox->vport = vport;
  1567. sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
  1568. rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
  1569. if (rc == MBX_NOT_FINISHED) {
  1570. mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
  1571. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1572. kfree(mp);
  1573. mempool_free(sparam_mbox, phba->mbox_mem_pool);
  1574. goto out;
  1575. }
  1576. }
  1577. if (!(phba->hba_flag & HBA_FCOE_SUPPORT)) {
  1578. cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1579. if (!cfglink_mbox)
  1580. goto out;
  1581. vport->port_state = LPFC_LOCAL_CFG_LINK;
  1582. lpfc_config_link(phba, cfglink_mbox);
  1583. cfglink_mbox->vport = vport;
  1584. cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
  1585. rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
  1586. if (rc == MBX_NOT_FINISHED) {
  1587. mempool_free(cfglink_mbox, phba->mbox_mem_pool);
  1588. goto out;
  1589. }
  1590. } else {
  1591. /*
  1592. * Add the driver's default FCF record at FCF index 0 now. This
  1593. * is phase 1 implementation that support FCF index 0 and driver
  1594. * defaults.
  1595. */
  1596. if (phba->cfg_enable_fip == 0) {
  1597. fcf_record = kzalloc(sizeof(struct fcf_record),
  1598. GFP_KERNEL);
  1599. if (unlikely(!fcf_record)) {
  1600. lpfc_printf_log(phba, KERN_ERR,
  1601. LOG_MBOX | LOG_SLI,
  1602. "2554 Could not allocate memmory for "
  1603. "fcf record\n");
  1604. rc = -ENODEV;
  1605. goto out;
  1606. }
  1607. lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
  1608. LPFC_FCOE_FCF_DEF_INDEX);
  1609. rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
  1610. if (unlikely(rc)) {
  1611. lpfc_printf_log(phba, KERN_ERR,
  1612. LOG_MBOX | LOG_SLI,
  1613. "2013 Could not manually add FCF "
  1614. "record 0, status %d\n", rc);
  1615. rc = -ENODEV;
  1616. kfree(fcf_record);
  1617. goto out;
  1618. }
  1619. kfree(fcf_record);
  1620. }
  1621. /*
  1622. * The driver is expected to do FIP/FCF. Call the port
  1623. * and get the FCF Table.
  1624. */
  1625. rc = lpfc_sli4_read_fcf_record(phba,
  1626. LPFC_FCOE_FCF_GET_FIRST);
  1627. if (rc)
  1628. goto out;
  1629. }
  1630. return;
  1631. out:
  1632. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  1633. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  1634. "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
  1635. vport->port_state, sparam_mbox, cfglink_mbox);
  1636. lpfc_issue_clear_la(phba, vport);
  1637. return;
  1638. }
  1639. static void
  1640. lpfc_enable_la(struct lpfc_hba *phba)
  1641. {
  1642. uint32_t control;
  1643. struct lpfc_sli *psli = &phba->sli;
  1644. spin_lock_irq(&phba->hbalock);
  1645. psli->sli_flag |= LPFC_PROCESS_LA;
  1646. if (phba->sli_rev <= LPFC_SLI_REV3) {
  1647. control = readl(phba->HCregaddr);
  1648. control |= HC_LAINT_ENA;
  1649. writel(control, phba->HCregaddr);
  1650. readl(phba->HCregaddr); /* flush */
  1651. }
  1652. spin_unlock_irq(&phba->hbalock);
  1653. }
  1654. static void
  1655. lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
  1656. {
  1657. lpfc_linkdown(phba);
  1658. lpfc_enable_la(phba);
  1659. lpfc_unregister_unused_fcf(phba);
  1660. /* turn on Link Attention interrupts - no CLEAR_LA needed */
  1661. }
  1662. /*
  1663. * This routine handles processing a READ_LA mailbox
  1664. * command upon completion. It is setup in the LPFC_MBOXQ
  1665. * as the completion routine when the command is
  1666. * handed off to the SLI layer.
  1667. */
  1668. void
  1669. lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1670. {
  1671. struct lpfc_vport *vport = pmb->vport;
  1672. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  1673. READ_LA_VAR *la;
  1674. MAILBOX_t *mb = &pmb->u.mb;
  1675. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  1676. /* Unblock ELS traffic */
  1677. phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
  1678. /* Check for error */
  1679. if (mb->mbxStatus) {
  1680. lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
  1681. "1307 READ_LA mbox error x%x state x%x\n",
  1682. mb->mbxStatus, vport->port_state);
  1683. lpfc_mbx_issue_link_down(phba);
  1684. phba->link_state = LPFC_HBA_ERROR;
  1685. goto lpfc_mbx_cmpl_read_la_free_mbuf;
  1686. }
  1687. la = (READ_LA_VAR *) &pmb->u.mb.un.varReadLA;
  1688. memcpy(&phba->alpa_map[0], mp->virt, 128);
  1689. spin_lock_irq(shost->host_lock);
  1690. if (la->pb)
  1691. vport->fc_flag |= FC_BYPASSED_MODE;
  1692. else
  1693. vport->fc_flag &= ~FC_BYPASSED_MODE;
  1694. spin_unlock_irq(shost->host_lock);
  1695. if ((phba->fc_eventTag < la->eventTag) ||
  1696. (phba->fc_eventTag == la->eventTag)) {
  1697. phba->fc_stat.LinkMultiEvent++;
  1698. if (la->attType == AT_LINK_UP)
  1699. if (phba->fc_eventTag != 0)
  1700. lpfc_linkdown(phba);
  1701. }
  1702. phba->fc_eventTag = la->eventTag;
  1703. if (la->mm)
  1704. phba->sli.sli_flag |= LPFC_MENLO_MAINT;
  1705. else
  1706. phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
  1707. if (la->attType == AT_LINK_UP && (!la->mm)) {
  1708. phba->fc_stat.LinkUp++;
  1709. if (phba->link_flag & LS_LOOPBACK_MODE) {
  1710. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  1711. "1306 Link Up Event in loop back mode "
  1712. "x%x received Data: x%x x%x x%x x%x\n",
  1713. la->eventTag, phba->fc_eventTag,
  1714. la->granted_AL_PA, la->UlnkSpeed,
  1715. phba->alpa_map[0]);
  1716. } else {
  1717. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  1718. "1303 Link Up Event x%x received "
  1719. "Data: x%x x%x x%x x%x x%x x%x %d\n",
  1720. la->eventTag, phba->fc_eventTag,
  1721. la->granted_AL_PA, la->UlnkSpeed,
  1722. phba->alpa_map[0],
  1723. la->mm, la->fa,
  1724. phba->wait_4_mlo_maint_flg);
  1725. }
  1726. lpfc_mbx_process_link_up(phba, la);
  1727. } else if (la->attType == AT_LINK_DOWN) {
  1728. phba->fc_stat.LinkDown++;
  1729. if (phba->link_flag & LS_LOOPBACK_MODE) {
  1730. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  1731. "1308 Link Down Event in loop back mode "
  1732. "x%x received "
  1733. "Data: x%x x%x x%x\n",
  1734. la->eventTag, phba->fc_eventTag,
  1735. phba->pport->port_state, vport->fc_flag);
  1736. }
  1737. else {
  1738. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  1739. "1305 Link Down Event x%x received "
  1740. "Data: x%x x%x x%x x%x x%x\n",
  1741. la->eventTag, phba->fc_eventTag,
  1742. phba->pport->port_state, vport->fc_flag,
  1743. la->mm, la->fa);
  1744. }
  1745. lpfc_mbx_issue_link_down(phba);
  1746. }
  1747. if (la->mm && la->attType == AT_LINK_UP) {
  1748. if (phba->link_state != LPFC_LINK_DOWN) {
  1749. phba->fc_stat.LinkDown++;
  1750. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  1751. "1312 Link Down Event x%x received "
  1752. "Data: x%x x%x x%x\n",
  1753. la->eventTag, phba->fc_eventTag,
  1754. phba->pport->port_state, vport->fc_flag);
  1755. lpfc_mbx_issue_link_down(phba);
  1756. } else
  1757. lpfc_enable_la(phba);
  1758. lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
  1759. "1310 Menlo Maint Mode Link up Event x%x rcvd "
  1760. "Data: x%x x%x x%x\n",
  1761. la->eventTag, phba->fc_eventTag,
  1762. phba->pport->port_state, vport->fc_flag);
  1763. /*
  1764. * The cmnd that triggered this will be waiting for this
  1765. * signal.
  1766. */
  1767. /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
  1768. if (phba->wait_4_mlo_maint_flg) {
  1769. phba->wait_4_mlo_maint_flg = 0;
  1770. wake_up_interruptible(&phba->wait_4_mlo_m_q);
  1771. }
  1772. }
  1773. if (la->fa) {
  1774. if (la->mm)
  1775. lpfc_issue_clear_la(phba, vport);
  1776. lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
  1777. "1311 fa %d\n", la->fa);
  1778. }
  1779. lpfc_mbx_cmpl_read_la_free_mbuf:
  1780. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1781. kfree(mp);
  1782. mempool_free(pmb, phba->mbox_mem_pool);
  1783. return;
  1784. }
  1785. /*
  1786. * This routine handles processing a REG_LOGIN mailbox
  1787. * command upon completion. It is setup in the LPFC_MBOXQ
  1788. * as the completion routine when the command is
  1789. * handed off to the SLI layer.
  1790. */
  1791. void
  1792. lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1793. {
  1794. struct lpfc_vport *vport = pmb->vport;
  1795. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  1796. struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
  1797. pmb->context1 = NULL;
  1798. /* Good status, call state machine */
  1799. lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
  1800. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1801. kfree(mp);
  1802. mempool_free(pmb, phba->mbox_mem_pool);
  1803. /* decrement the node reference count held for this callback
  1804. * function.
  1805. */
  1806. lpfc_nlp_put(ndlp);
  1807. return;
  1808. }
  1809. static void
  1810. lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1811. {
  1812. MAILBOX_t *mb = &pmb->u.mb;
  1813. struct lpfc_vport *vport = pmb->vport;
  1814. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  1815. switch (mb->mbxStatus) {
  1816. case 0x0011:
  1817. case 0x0020:
  1818. case 0x9700:
  1819. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  1820. "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
  1821. mb->mbxStatus);
  1822. break;
  1823. }
  1824. vport->unreg_vpi_cmpl = VPORT_OK;
  1825. mempool_free(pmb, phba->mbox_mem_pool);
  1826. /*
  1827. * This shost reference might have been taken at the beginning of
  1828. * lpfc_vport_delete()
  1829. */
  1830. if (vport->load_flag & FC_UNLOADING)
  1831. scsi_host_put(shost);
  1832. }
  1833. int
  1834. lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
  1835. {
  1836. struct lpfc_hba *phba = vport->phba;
  1837. LPFC_MBOXQ_t *mbox;
  1838. int rc;
  1839. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1840. if (!mbox)
  1841. return 1;
  1842. lpfc_unreg_vpi(phba, vport->vpi, mbox);
  1843. mbox->vport = vport;
  1844. mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
  1845. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  1846. if (rc == MBX_NOT_FINISHED) {
  1847. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
  1848. "1800 Could not issue unreg_vpi\n");
  1849. mempool_free(mbox, phba->mbox_mem_pool);
  1850. vport->unreg_vpi_cmpl = VPORT_ERROR;
  1851. return rc;
  1852. }
  1853. return 0;
  1854. }
  1855. static void
  1856. lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1857. {
  1858. struct lpfc_vport *vport = pmb->vport;
  1859. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  1860. MAILBOX_t *mb = &pmb->u.mb;
  1861. switch (mb->mbxStatus) {
  1862. case 0x0011:
  1863. case 0x9601:
  1864. case 0x9602:
  1865. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  1866. "0912 cmpl_reg_vpi, mb status = 0x%x\n",
  1867. mb->mbxStatus);
  1868. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  1869. spin_lock_irq(shost->host_lock);
  1870. vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
  1871. spin_unlock_irq(shost->host_lock);
  1872. vport->fc_myDID = 0;
  1873. goto out;
  1874. }
  1875. vport->num_disc_nodes = 0;
  1876. /* go thru NPR list and issue ELS PLOGIs */
  1877. if (vport->fc_npr_cnt)
  1878. lpfc_els_disc_plogi(vport);
  1879. if (!vport->num_disc_nodes) {
  1880. spin_lock_irq(shost->host_lock);
  1881. vport->fc_flag &= ~FC_NDISC_ACTIVE;
  1882. spin_unlock_irq(shost->host_lock);
  1883. lpfc_can_disctmo(vport);
  1884. }
  1885. vport->port_state = LPFC_VPORT_READY;
  1886. out:
  1887. mempool_free(pmb, phba->mbox_mem_pool);
  1888. return;
  1889. }
  1890. /**
  1891. * lpfc_create_static_vport - Read HBA config region to create static vports.
  1892. * @phba: pointer to lpfc hba data structure.
  1893. *
  1894. * This routine issue a DUMP mailbox command for config region 22 to get
  1895. * the list of static vports to be created. The function create vports
  1896. * based on the information returned from the HBA.
  1897. **/
  1898. void
  1899. lpfc_create_static_vport(struct lpfc_hba *phba)
  1900. {
  1901. LPFC_MBOXQ_t *pmb = NULL;
  1902. MAILBOX_t *mb;
  1903. struct static_vport_info *vport_info;
  1904. int rc, i;
  1905. struct fc_vport_identifiers vport_id;
  1906. struct fc_vport *new_fc_vport;
  1907. struct Scsi_Host *shost;
  1908. struct lpfc_vport *vport;
  1909. uint16_t offset = 0;
  1910. uint8_t *vport_buff;
  1911. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1912. if (!pmb) {
  1913. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1914. "0542 lpfc_create_static_vport failed to"
  1915. " allocate mailbox memory\n");
  1916. return;
  1917. }
  1918. mb = &pmb->u.mb;
  1919. vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
  1920. if (!vport_info) {
  1921. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1922. "0543 lpfc_create_static_vport failed to"
  1923. " allocate vport_info\n");
  1924. mempool_free(pmb, phba->mbox_mem_pool);
  1925. return;
  1926. }
  1927. vport_buff = (uint8_t *) vport_info;
  1928. do {
  1929. lpfc_dump_static_vport(phba, pmb, offset);
  1930. pmb->vport = phba->pport;
  1931. rc = lpfc_sli_issue_mbox_wait(phba, pmb, LPFC_MBOX_TMO);
  1932. if ((rc != MBX_SUCCESS) || mb->mbxStatus) {
  1933. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  1934. "0544 lpfc_create_static_vport failed to"
  1935. " issue dump mailbox command ret 0x%x "
  1936. "status 0x%x\n",
  1937. rc, mb->mbxStatus);
  1938. goto out;
  1939. }
  1940. if (mb->un.varDmp.word_cnt >
  1941. sizeof(struct static_vport_info) - offset)
  1942. mb->un.varDmp.word_cnt =
  1943. sizeof(struct static_vport_info) - offset;
  1944. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  1945. vport_buff + offset,
  1946. mb->un.varDmp.word_cnt);
  1947. offset += mb->un.varDmp.word_cnt;
  1948. } while (mb->un.varDmp.word_cnt &&
  1949. offset < sizeof(struct static_vport_info));
  1950. if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
  1951. ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
  1952. != VPORT_INFO_REV)) {
  1953. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1954. "0545 lpfc_create_static_vport bad"
  1955. " information header 0x%x 0x%x\n",
  1956. le32_to_cpu(vport_info->signature),
  1957. le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
  1958. goto out;
  1959. }
  1960. shost = lpfc_shost_from_vport(phba->pport);
  1961. for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
  1962. memset(&vport_id, 0, sizeof(vport_id));
  1963. vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
  1964. vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
  1965. if (!vport_id.port_name || !vport_id.node_name)
  1966. continue;
  1967. vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
  1968. vport_id.vport_type = FC_PORTTYPE_NPIV;
  1969. vport_id.disable = false;
  1970. new_fc_vport = fc_vport_create(shost, 0, &vport_id);
  1971. if (!new_fc_vport) {
  1972. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  1973. "0546 lpfc_create_static_vport failed to"
  1974. " create vport \n");
  1975. continue;
  1976. }
  1977. vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
  1978. vport->vport_flag |= STATIC_VPORT;
  1979. }
  1980. out:
  1981. /*
  1982. * If this is timed out command, setting NULL to context2 tell SLI
  1983. * layer not to use this buffer.
  1984. */
  1985. spin_lock_irq(&phba->hbalock);
  1986. pmb->context2 = NULL;
  1987. spin_unlock_irq(&phba->hbalock);
  1988. kfree(vport_info);
  1989. if (rc != MBX_TIMEOUT)
  1990. mempool_free(pmb, phba->mbox_mem_pool);
  1991. return;
  1992. }
  1993. /*
  1994. * This routine handles processing a Fabric REG_LOGIN mailbox
  1995. * command upon completion. It is setup in the LPFC_MBOXQ
  1996. * as the completion routine when the command is
  1997. * handed off to the SLI layer.
  1998. */
  1999. void
  2000. lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  2001. {
  2002. struct lpfc_vport *vport = pmb->vport;
  2003. MAILBOX_t *mb = &pmb->u.mb;
  2004. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  2005. struct lpfc_nodelist *ndlp;
  2006. ndlp = (struct lpfc_nodelist *) pmb->context2;
  2007. pmb->context1 = NULL;
  2008. pmb->context2 = NULL;
  2009. if (mb->mbxStatus) {
  2010. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
  2011. "0258 Register Fabric login error: 0x%x\n",
  2012. mb->mbxStatus);
  2013. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2014. kfree(mp);
  2015. mempool_free(pmb, phba->mbox_mem_pool);
  2016. if (phba->fc_topology == TOPOLOGY_LOOP) {
  2017. /* FLOGI failed, use loop map to make discovery list */
  2018. lpfc_disc_list_loopmap(vport);
  2019. /* Start discovery */
  2020. lpfc_disc_start(vport);
  2021. /* Decrement the reference count to ndlp after the
  2022. * reference to the ndlp are done.
  2023. */
  2024. lpfc_nlp_put(ndlp);
  2025. return;
  2026. }
  2027. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  2028. /* Decrement the reference count to ndlp after the reference
  2029. * to the ndlp are done.
  2030. */
  2031. lpfc_nlp_put(ndlp);
  2032. return;
  2033. }
  2034. ndlp->nlp_rpi = mb->un.varWords[0];
  2035. ndlp->nlp_flag |= NLP_RPI_VALID;
  2036. ndlp->nlp_type |= NLP_FABRIC;
  2037. lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
  2038. if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
  2039. lpfc_start_fdiscs(phba);
  2040. lpfc_do_scr_ns_plogi(phba, vport);
  2041. }
  2042. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2043. kfree(mp);
  2044. mempool_free(pmb, phba->mbox_mem_pool);
  2045. /* Drop the reference count from the mbox at the end after
  2046. * all the current reference to the ndlp have been done.
  2047. */
  2048. lpfc_nlp_put(ndlp);
  2049. return;
  2050. }
  2051. /*
  2052. * This routine handles processing a NameServer REG_LOGIN mailbox
  2053. * command upon completion. It is setup in the LPFC_MBOXQ
  2054. * as the completion routine when the command is
  2055. * handed off to the SLI layer.
  2056. */
  2057. void
  2058. lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  2059. {
  2060. MAILBOX_t *mb = &pmb->u.mb;
  2061. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  2062. struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
  2063. struct lpfc_vport *vport = pmb->vport;
  2064. if (mb->mbxStatus) {
  2065. out:
  2066. lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
  2067. "0260 Register NameServer error: 0x%x\n",
  2068. mb->mbxStatus);
  2069. /* decrement the node reference count held for this
  2070. * callback function.
  2071. */
  2072. lpfc_nlp_put(ndlp);
  2073. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2074. kfree(mp);
  2075. mempool_free(pmb, phba->mbox_mem_pool);
  2076. /* If no other thread is using the ndlp, free it */
  2077. lpfc_nlp_not_used(ndlp);
  2078. if (phba->fc_topology == TOPOLOGY_LOOP) {
  2079. /*
  2080. * RegLogin failed, use loop map to make discovery
  2081. * list
  2082. */
  2083. lpfc_disc_list_loopmap(vport);
  2084. /* Start discovery */
  2085. lpfc_disc_start(vport);
  2086. return;
  2087. }
  2088. lpfc_vport_set_state(vport, FC_VPORT_FAILED);
  2089. return;
  2090. }
  2091. pmb->context1 = NULL;
  2092. ndlp->nlp_rpi = mb->un.varWords[0];
  2093. ndlp->nlp_flag |= NLP_RPI_VALID;
  2094. ndlp->nlp_type |= NLP_FABRIC;
  2095. lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
  2096. if (vport->port_state < LPFC_VPORT_READY) {
  2097. /* Link up discovery requires Fabric registration. */
  2098. lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
  2099. lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
  2100. lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
  2101. lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
  2102. lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
  2103. /* Issue SCR just before NameServer GID_FT Query */
  2104. lpfc_issue_els_scr(vport, SCR_DID, 0);
  2105. }
  2106. vport->fc_ns_retry = 0;
  2107. /* Good status, issue CT Request to NameServer */
  2108. if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
  2109. /* Cannot issue NameServer Query, so finish up discovery */
  2110. goto out;
  2111. }
  2112. /* decrement the node reference count held for this
  2113. * callback function.
  2114. */
  2115. lpfc_nlp_put(ndlp);
  2116. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2117. kfree(mp);
  2118. mempool_free(pmb, phba->mbox_mem_pool);
  2119. return;
  2120. }
  2121. static void
  2122. lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  2123. {
  2124. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2125. struct fc_rport *rport;
  2126. struct lpfc_rport_data *rdata;
  2127. struct fc_rport_identifiers rport_ids;
  2128. struct lpfc_hba *phba = vport->phba;
  2129. /* Remote port has reappeared. Re-register w/ FC transport */
  2130. rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
  2131. rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
  2132. rport_ids.port_id = ndlp->nlp_DID;
  2133. rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
  2134. /*
  2135. * We leave our node pointer in rport->dd_data when we unregister a
  2136. * FCP target port. But fc_remote_port_add zeros the space to which
  2137. * rport->dd_data points. So, if we're reusing a previously
  2138. * registered port, drop the reference that we took the last time we
  2139. * registered the port.
  2140. */
  2141. if (ndlp->rport && ndlp->rport->dd_data &&
  2142. ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
  2143. lpfc_nlp_put(ndlp);
  2144. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
  2145. "rport add: did:x%x flg:x%x type x%x",
  2146. ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
  2147. ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
  2148. if (!rport || !get_device(&rport->dev)) {
  2149. dev_printk(KERN_WARNING, &phba->pcidev->dev,
  2150. "Warning: fc_remote_port_add failed\n");
  2151. return;
  2152. }
  2153. /* initialize static port data */
  2154. rport->maxframe_size = ndlp->nlp_maxframe;
  2155. rport->supported_classes = ndlp->nlp_class_sup;
  2156. rdata = rport->dd_data;
  2157. rdata->pnode = lpfc_nlp_get(ndlp);
  2158. if (ndlp->nlp_type & NLP_FCP_TARGET)
  2159. rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
  2160. if (ndlp->nlp_type & NLP_FCP_INITIATOR)
  2161. rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
  2162. if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
  2163. fc_remote_port_rolechg(rport, rport_ids.roles);
  2164. if ((rport->scsi_target_id != -1) &&
  2165. (rport->scsi_target_id < LPFC_MAX_TARGET)) {
  2166. ndlp->nlp_sid = rport->scsi_target_id;
  2167. }
  2168. return;
  2169. }
  2170. static void
  2171. lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
  2172. {
  2173. struct fc_rport *rport = ndlp->rport;
  2174. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
  2175. "rport delete: did:x%x flg:x%x type x%x",
  2176. ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
  2177. fc_remote_port_delete(rport);
  2178. return;
  2179. }
  2180. static void
  2181. lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
  2182. {
  2183. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2184. spin_lock_irq(shost->host_lock);
  2185. switch (state) {
  2186. case NLP_STE_UNUSED_NODE:
  2187. vport->fc_unused_cnt += count;
  2188. break;
  2189. case NLP_STE_PLOGI_ISSUE:
  2190. vport->fc_plogi_cnt += count;
  2191. break;
  2192. case NLP_STE_ADISC_ISSUE:
  2193. vport->fc_adisc_cnt += count;
  2194. break;
  2195. case NLP_STE_REG_LOGIN_ISSUE:
  2196. vport->fc_reglogin_cnt += count;
  2197. break;
  2198. case NLP_STE_PRLI_ISSUE:
  2199. vport->fc_prli_cnt += count;
  2200. break;
  2201. case NLP_STE_UNMAPPED_NODE:
  2202. vport->fc_unmap_cnt += count;
  2203. break;
  2204. case NLP_STE_MAPPED_NODE:
  2205. vport->fc_map_cnt += count;
  2206. break;
  2207. case NLP_STE_NPR_NODE:
  2208. vport->fc_npr_cnt += count;
  2209. break;
  2210. }
  2211. spin_unlock_irq(shost->host_lock);
  2212. }
  2213. static void
  2214. lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  2215. int old_state, int new_state)
  2216. {
  2217. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2218. if (new_state == NLP_STE_UNMAPPED_NODE) {
  2219. ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
  2220. ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
  2221. ndlp->nlp_type |= NLP_FC_NODE;
  2222. }
  2223. if (new_state == NLP_STE_MAPPED_NODE)
  2224. ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
  2225. if (new_state == NLP_STE_NPR_NODE)
  2226. ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
  2227. /* Transport interface */
  2228. if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
  2229. old_state == NLP_STE_UNMAPPED_NODE)) {
  2230. vport->phba->nport_event_cnt++;
  2231. lpfc_unregister_remote_port(ndlp);
  2232. }
  2233. if (new_state == NLP_STE_MAPPED_NODE ||
  2234. new_state == NLP_STE_UNMAPPED_NODE) {
  2235. vport->phba->nport_event_cnt++;
  2236. /*
  2237. * Tell the fc transport about the port, if we haven't
  2238. * already. If we have, and it's a scsi entity, be
  2239. * sure to unblock any attached scsi devices
  2240. */
  2241. lpfc_register_remote_port(vport, ndlp);
  2242. }
  2243. if ((new_state == NLP_STE_MAPPED_NODE) &&
  2244. (vport->stat_data_enabled)) {
  2245. /*
  2246. * A new target is discovered, if there is no buffer for
  2247. * statistical data collection allocate buffer.
  2248. */
  2249. ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
  2250. sizeof(struct lpfc_scsicmd_bkt),
  2251. GFP_KERNEL);
  2252. if (!ndlp->lat_data)
  2253. lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
  2254. "0286 lpfc_nlp_state_cleanup failed to "
  2255. "allocate statistical data buffer DID "
  2256. "0x%x\n", ndlp->nlp_DID);
  2257. }
  2258. /*
  2259. * if we added to Mapped list, but the remote port
  2260. * registration failed or assigned a target id outside
  2261. * our presentable range - move the node to the
  2262. * Unmapped List
  2263. */
  2264. if (new_state == NLP_STE_MAPPED_NODE &&
  2265. (!ndlp->rport ||
  2266. ndlp->rport->scsi_target_id == -1 ||
  2267. ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
  2268. spin_lock_irq(shost->host_lock);
  2269. ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
  2270. spin_unlock_irq(shost->host_lock);
  2271. lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
  2272. }
  2273. }
  2274. static char *
  2275. lpfc_nlp_state_name(char *buffer, size_t size, int state)
  2276. {
  2277. static char *states[] = {
  2278. [NLP_STE_UNUSED_NODE] = "UNUSED",
  2279. [NLP_STE_PLOGI_ISSUE] = "PLOGI",
  2280. [NLP_STE_ADISC_ISSUE] = "ADISC",
  2281. [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
  2282. [NLP_STE_PRLI_ISSUE] = "PRLI",
  2283. [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
  2284. [NLP_STE_MAPPED_NODE] = "MAPPED",
  2285. [NLP_STE_NPR_NODE] = "NPR",
  2286. };
  2287. if (state < NLP_STE_MAX_STATE && states[state])
  2288. strlcpy(buffer, states[state], size);
  2289. else
  2290. snprintf(buffer, size, "unknown (%d)", state);
  2291. return buffer;
  2292. }
  2293. void
  2294. lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  2295. int state)
  2296. {
  2297. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2298. int old_state = ndlp->nlp_state;
  2299. char name1[16], name2[16];
  2300. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  2301. "0904 NPort state transition x%06x, %s -> %s\n",
  2302. ndlp->nlp_DID,
  2303. lpfc_nlp_state_name(name1, sizeof(name1), old_state),
  2304. lpfc_nlp_state_name(name2, sizeof(name2), state));
  2305. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
  2306. "node statechg did:x%x old:%d ste:%d",
  2307. ndlp->nlp_DID, old_state, state);
  2308. if (old_state == NLP_STE_NPR_NODE &&
  2309. state != NLP_STE_NPR_NODE)
  2310. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  2311. if (old_state == NLP_STE_UNMAPPED_NODE) {
  2312. ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
  2313. ndlp->nlp_type &= ~NLP_FC_NODE;
  2314. }
  2315. if (list_empty(&ndlp->nlp_listp)) {
  2316. spin_lock_irq(shost->host_lock);
  2317. list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
  2318. spin_unlock_irq(shost->host_lock);
  2319. } else if (old_state)
  2320. lpfc_nlp_counters(vport, old_state, -1);
  2321. ndlp->nlp_state = state;
  2322. lpfc_nlp_counters(vport, state, 1);
  2323. lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
  2324. }
  2325. void
  2326. lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  2327. {
  2328. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2329. if (list_empty(&ndlp->nlp_listp)) {
  2330. spin_lock_irq(shost->host_lock);
  2331. list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
  2332. spin_unlock_irq(shost->host_lock);
  2333. }
  2334. }
  2335. void
  2336. lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  2337. {
  2338. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2339. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  2340. if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
  2341. lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
  2342. spin_lock_irq(shost->host_lock);
  2343. list_del_init(&ndlp->nlp_listp);
  2344. spin_unlock_irq(shost->host_lock);
  2345. lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
  2346. NLP_STE_UNUSED_NODE);
  2347. }
  2348. static void
  2349. lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  2350. {
  2351. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  2352. if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
  2353. lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
  2354. lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
  2355. NLP_STE_UNUSED_NODE);
  2356. }
  2357. /**
  2358. * lpfc_initialize_node - Initialize all fields of node object
  2359. * @vport: Pointer to Virtual Port object.
  2360. * @ndlp: Pointer to FC node object.
  2361. * @did: FC_ID of the node.
  2362. *
  2363. * This function is always called when node object need to be initialized.
  2364. * It initializes all the fields of the node object. Although the reference
  2365. * to phba from @ndlp can be obtained indirectly through it's reference to
  2366. * @vport, a direct reference to phba is taken here by @ndlp. This is due
  2367. * to the life-span of the @ndlp might go beyond the existence of @vport as
  2368. * the final release of ndlp is determined by its reference count. And, the
  2369. * operation on @ndlp needs the reference to phba.
  2370. **/
  2371. static inline void
  2372. lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  2373. uint32_t did)
  2374. {
  2375. INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
  2376. INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
  2377. init_timer(&ndlp->nlp_delayfunc);
  2378. ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
  2379. ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
  2380. ndlp->nlp_DID = did;
  2381. ndlp->vport = vport;
  2382. ndlp->phba = vport->phba;
  2383. ndlp->nlp_sid = NLP_NO_SID;
  2384. kref_init(&ndlp->kref);
  2385. NLP_INT_NODE_ACT(ndlp);
  2386. atomic_set(&ndlp->cmd_pending, 0);
  2387. ndlp->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
  2388. }
  2389. struct lpfc_nodelist *
  2390. lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  2391. int state)
  2392. {
  2393. struct lpfc_hba *phba = vport->phba;
  2394. uint32_t did;
  2395. unsigned long flags;
  2396. if (!ndlp)
  2397. return NULL;
  2398. spin_lock_irqsave(&phba->ndlp_lock, flags);
  2399. /* The ndlp should not be in memory free mode */
  2400. if (NLP_CHK_FREE_REQ(ndlp)) {
  2401. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  2402. lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
  2403. "0277 lpfc_enable_node: ndlp:x%p "
  2404. "usgmap:x%x refcnt:%d\n",
  2405. (void *)ndlp, ndlp->nlp_usg_map,
  2406. atomic_read(&ndlp->kref.refcount));
  2407. return NULL;
  2408. }
  2409. /* The ndlp should not already be in active mode */
  2410. if (NLP_CHK_NODE_ACT(ndlp)) {
  2411. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  2412. lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
  2413. "0278 lpfc_enable_node: ndlp:x%p "
  2414. "usgmap:x%x refcnt:%d\n",
  2415. (void *)ndlp, ndlp->nlp_usg_map,
  2416. atomic_read(&ndlp->kref.refcount));
  2417. return NULL;
  2418. }
  2419. /* Keep the original DID */
  2420. did = ndlp->nlp_DID;
  2421. /* re-initialize ndlp except of ndlp linked list pointer */
  2422. memset((((char *)ndlp) + sizeof (struct list_head)), 0,
  2423. sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
  2424. lpfc_initialize_node(vport, ndlp, did);
  2425. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  2426. if (state != NLP_STE_UNUSED_NODE)
  2427. lpfc_nlp_set_state(vport, ndlp, state);
  2428. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
  2429. "node enable: did:x%x",
  2430. ndlp->nlp_DID, 0, 0);
  2431. return ndlp;
  2432. }
  2433. void
  2434. lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  2435. {
  2436. /*
  2437. * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
  2438. * be used if we wish to issue the "last" lpfc_nlp_put() to remove
  2439. * the ndlp from the vport. The ndlp marked as UNUSED on the list
  2440. * until ALL other outstanding threads have completed. We check
  2441. * that the ndlp not already in the UNUSED state before we proceed.
  2442. */
  2443. if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
  2444. return;
  2445. lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
  2446. lpfc_nlp_put(ndlp);
  2447. return;
  2448. }
  2449. /*
  2450. * Start / ReStart rescue timer for Discovery / RSCN handling
  2451. */
  2452. void
  2453. lpfc_set_disctmo(struct lpfc_vport *vport)
  2454. {
  2455. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2456. struct lpfc_hba *phba = vport->phba;
  2457. uint32_t tmo;
  2458. if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
  2459. /* For FAN, timeout should be greater than edtov */
  2460. tmo = (((phba->fc_edtov + 999) / 1000) + 1);
  2461. } else {
  2462. /* Normal discovery timeout should be > than ELS/CT timeout
  2463. * FC spec states we need 3 * ratov for CT requests
  2464. */
  2465. tmo = ((phba->fc_ratov * 3) + 3);
  2466. }
  2467. if (!timer_pending(&vport->fc_disctmo)) {
  2468. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
  2469. "set disc timer: tmo:x%x state:x%x flg:x%x",
  2470. tmo, vport->port_state, vport->fc_flag);
  2471. }
  2472. mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
  2473. spin_lock_irq(shost->host_lock);
  2474. vport->fc_flag |= FC_DISC_TMO;
  2475. spin_unlock_irq(shost->host_lock);
  2476. /* Start Discovery Timer state <hba_state> */
  2477. lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
  2478. "0247 Start Discovery Timer state x%x "
  2479. "Data: x%x x%lx x%x x%x\n",
  2480. vport->port_state, tmo,
  2481. (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
  2482. vport->fc_adisc_cnt);
  2483. return;
  2484. }
  2485. /*
  2486. * Cancel rescue timer for Discovery / RSCN handling
  2487. */
  2488. int
  2489. lpfc_can_disctmo(struct lpfc_vport *vport)
  2490. {
  2491. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2492. unsigned long iflags;
  2493. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
  2494. "can disc timer: state:x%x rtry:x%x flg:x%x",
  2495. vport->port_state, vport->fc_ns_retry, vport->fc_flag);
  2496. /* Turn off discovery timer if its running */
  2497. if (vport->fc_flag & FC_DISC_TMO) {
  2498. spin_lock_irqsave(shost->host_lock, iflags);
  2499. vport->fc_flag &= ~FC_DISC_TMO;
  2500. spin_unlock_irqrestore(shost->host_lock, iflags);
  2501. del_timer_sync(&vport->fc_disctmo);
  2502. spin_lock_irqsave(&vport->work_port_lock, iflags);
  2503. vport->work_port_events &= ~WORKER_DISC_TMO;
  2504. spin_unlock_irqrestore(&vport->work_port_lock, iflags);
  2505. }
  2506. /* Cancel Discovery Timer state <hba_state> */
  2507. lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
  2508. "0248 Cancel Discovery Timer state x%x "
  2509. "Data: x%x x%x x%x\n",
  2510. vport->port_state, vport->fc_flag,
  2511. vport->fc_plogi_cnt, vport->fc_adisc_cnt);
  2512. return 0;
  2513. }
  2514. /*
  2515. * Check specified ring for outstanding IOCB on the SLI queue
  2516. * Return true if iocb matches the specified nport
  2517. */
  2518. int
  2519. lpfc_check_sli_ndlp(struct lpfc_hba *phba,
  2520. struct lpfc_sli_ring *pring,
  2521. struct lpfc_iocbq *iocb,
  2522. struct lpfc_nodelist *ndlp)
  2523. {
  2524. struct lpfc_sli *psli = &phba->sli;
  2525. IOCB_t *icmd = &iocb->iocb;
  2526. struct lpfc_vport *vport = ndlp->vport;
  2527. if (iocb->vport != vport)
  2528. return 0;
  2529. if (pring->ringno == LPFC_ELS_RING) {
  2530. switch (icmd->ulpCommand) {
  2531. case CMD_GEN_REQUEST64_CR:
  2532. if (iocb->context_un.ndlp == ndlp)
  2533. return 1;
  2534. case CMD_ELS_REQUEST64_CR:
  2535. if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
  2536. return 1;
  2537. case CMD_XMIT_ELS_RSP64_CX:
  2538. if (iocb->context1 == (uint8_t *) ndlp)
  2539. return 1;
  2540. }
  2541. } else if (pring->ringno == psli->extra_ring) {
  2542. } else if (pring->ringno == psli->fcp_ring) {
  2543. /* Skip match check if waiting to relogin to FCP target */
  2544. if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
  2545. (ndlp->nlp_flag & NLP_DELAY_TMO)) {
  2546. return 0;
  2547. }
  2548. if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
  2549. return 1;
  2550. }
  2551. } else if (pring->ringno == psli->next_ring) {
  2552. }
  2553. return 0;
  2554. }
  2555. /*
  2556. * Free resources / clean up outstanding I/Os
  2557. * associated with nlp_rpi in the LPFC_NODELIST entry.
  2558. */
  2559. static int
  2560. lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
  2561. {
  2562. LIST_HEAD(completions);
  2563. struct lpfc_sli *psli;
  2564. struct lpfc_sli_ring *pring;
  2565. struct lpfc_iocbq *iocb, *next_iocb;
  2566. uint32_t rpi, i;
  2567. lpfc_fabric_abort_nport(ndlp);
  2568. /*
  2569. * Everything that matches on txcmplq will be returned
  2570. * by firmware with a no rpi error.
  2571. */
  2572. psli = &phba->sli;
  2573. rpi = ndlp->nlp_rpi;
  2574. if (ndlp->nlp_flag & NLP_RPI_VALID) {
  2575. /* Now process each ring */
  2576. for (i = 0; i < psli->num_rings; i++) {
  2577. pring = &psli->ring[i];
  2578. spin_lock_irq(&phba->hbalock);
  2579. list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
  2580. list) {
  2581. /*
  2582. * Check to see if iocb matches the nport we are
  2583. * looking for
  2584. */
  2585. if ((lpfc_check_sli_ndlp(phba, pring, iocb,
  2586. ndlp))) {
  2587. /* It matches, so deque and call compl
  2588. with an error */
  2589. list_move_tail(&iocb->list,
  2590. &completions);
  2591. pring->txq_cnt--;
  2592. }
  2593. }
  2594. spin_unlock_irq(&phba->hbalock);
  2595. }
  2596. }
  2597. /* Cancel all the IOCBs from the completions list */
  2598. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  2599. IOERR_SLI_ABORTED);
  2600. return 0;
  2601. }
  2602. /*
  2603. * Free rpi associated with LPFC_NODELIST entry.
  2604. * This routine is called from lpfc_freenode(), when we are removing
  2605. * a LPFC_NODELIST entry. It is also called if the driver initiates a
  2606. * LOGO that completes successfully, and we are waiting to PLOGI back
  2607. * to the remote NPort. In addition, it is called after we receive
  2608. * and unsolicated ELS cmd, send back a rsp, the rsp completes and
  2609. * we are waiting to PLOGI back to the remote NPort.
  2610. */
  2611. int
  2612. lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  2613. {
  2614. struct lpfc_hba *phba = vport->phba;
  2615. LPFC_MBOXQ_t *mbox;
  2616. int rc;
  2617. if (ndlp->nlp_flag & NLP_RPI_VALID) {
  2618. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2619. if (mbox) {
  2620. lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
  2621. mbox->vport = vport;
  2622. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  2623. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  2624. if (rc == MBX_NOT_FINISHED)
  2625. mempool_free(mbox, phba->mbox_mem_pool);
  2626. }
  2627. lpfc_no_rpi(phba, ndlp);
  2628. ndlp->nlp_rpi = 0;
  2629. ndlp->nlp_flag &= ~NLP_RPI_VALID;
  2630. ndlp->nlp_flag &= ~NLP_NPR_ADISC;
  2631. return 1;
  2632. }
  2633. return 0;
  2634. }
  2635. void
  2636. lpfc_unreg_all_rpis(struct lpfc_vport *vport)
  2637. {
  2638. struct lpfc_hba *phba = vport->phba;
  2639. LPFC_MBOXQ_t *mbox;
  2640. int rc;
  2641. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2642. if (mbox) {
  2643. lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
  2644. mbox->vport = vport;
  2645. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  2646. mbox->context1 = NULL;
  2647. rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
  2648. if (rc != MBX_TIMEOUT)
  2649. mempool_free(mbox, phba->mbox_mem_pool);
  2650. if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
  2651. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
  2652. "1836 Could not issue "
  2653. "unreg_login(all_rpis) status %d\n", rc);
  2654. }
  2655. }
  2656. void
  2657. lpfc_unreg_default_rpis(struct lpfc_vport *vport)
  2658. {
  2659. struct lpfc_hba *phba = vport->phba;
  2660. LPFC_MBOXQ_t *mbox;
  2661. int rc;
  2662. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2663. if (mbox) {
  2664. lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
  2665. mbox->vport = vport;
  2666. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  2667. mbox->context1 = NULL;
  2668. rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
  2669. if (rc != MBX_TIMEOUT)
  2670. mempool_free(mbox, phba->mbox_mem_pool);
  2671. if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
  2672. lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
  2673. "1815 Could not issue "
  2674. "unreg_did (default rpis) status %d\n",
  2675. rc);
  2676. }
  2677. }
  2678. /*
  2679. * Free resources associated with LPFC_NODELIST entry
  2680. * so it can be freed.
  2681. */
  2682. static int
  2683. lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  2684. {
  2685. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2686. struct lpfc_hba *phba = vport->phba;
  2687. LPFC_MBOXQ_t *mb, *nextmb;
  2688. struct lpfc_dmabuf *mp;
  2689. /* Cleanup node for NPort <nlp_DID> */
  2690. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  2691. "0900 Cleanup node for NPort x%x "
  2692. "Data: x%x x%x x%x\n",
  2693. ndlp->nlp_DID, ndlp->nlp_flag,
  2694. ndlp->nlp_state, ndlp->nlp_rpi);
  2695. if (NLP_CHK_FREE_REQ(ndlp)) {
  2696. lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
  2697. "0280 lpfc_cleanup_node: ndlp:x%p "
  2698. "usgmap:x%x refcnt:%d\n",
  2699. (void *)ndlp, ndlp->nlp_usg_map,
  2700. atomic_read(&ndlp->kref.refcount));
  2701. lpfc_dequeue_node(vport, ndlp);
  2702. } else {
  2703. lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
  2704. "0281 lpfc_cleanup_node: ndlp:x%p "
  2705. "usgmap:x%x refcnt:%d\n",
  2706. (void *)ndlp, ndlp->nlp_usg_map,
  2707. atomic_read(&ndlp->kref.refcount));
  2708. lpfc_disable_node(vport, ndlp);
  2709. }
  2710. /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
  2711. if ((mb = phba->sli.mbox_active)) {
  2712. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
  2713. (ndlp == (struct lpfc_nodelist *) mb->context2)) {
  2714. mb->context2 = NULL;
  2715. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  2716. }
  2717. }
  2718. spin_lock_irq(&phba->hbalock);
  2719. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  2720. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
  2721. (ndlp == (struct lpfc_nodelist *) mb->context2)) {
  2722. mp = (struct lpfc_dmabuf *) (mb->context1);
  2723. if (mp) {
  2724. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2725. kfree(mp);
  2726. }
  2727. list_del(&mb->list);
  2728. mempool_free(mb, phba->mbox_mem_pool);
  2729. /* We shall not invoke the lpfc_nlp_put to decrement
  2730. * the ndlp reference count as we are in the process
  2731. * of lpfc_nlp_release.
  2732. */
  2733. }
  2734. }
  2735. spin_unlock_irq(&phba->hbalock);
  2736. lpfc_els_abort(phba, ndlp);
  2737. spin_lock_irq(shost->host_lock);
  2738. ndlp->nlp_flag &= ~NLP_DELAY_TMO;
  2739. spin_unlock_irq(shost->host_lock);
  2740. ndlp->nlp_last_elscmd = 0;
  2741. del_timer_sync(&ndlp->nlp_delayfunc);
  2742. list_del_init(&ndlp->els_retry_evt.evt_listp);
  2743. list_del_init(&ndlp->dev_loss_evt.evt_listp);
  2744. lpfc_unreg_rpi(vport, ndlp);
  2745. return 0;
  2746. }
  2747. /*
  2748. * Check to see if we can free the nlp back to the freelist.
  2749. * If we are in the middle of using the nlp in the discovery state
  2750. * machine, defer the free till we reach the end of the state machine.
  2751. */
  2752. static void
  2753. lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  2754. {
  2755. struct lpfc_hba *phba = vport->phba;
  2756. struct lpfc_rport_data *rdata;
  2757. LPFC_MBOXQ_t *mbox;
  2758. int rc;
  2759. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  2760. if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
  2761. !(ndlp->nlp_flag & NLP_RPI_VALID)) {
  2762. /* For this case we need to cleanup the default rpi
  2763. * allocated by the firmware.
  2764. */
  2765. if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
  2766. != NULL) {
  2767. rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
  2768. (uint8_t *) &vport->fc_sparam, mbox, 0);
  2769. if (rc) {
  2770. mempool_free(mbox, phba->mbox_mem_pool);
  2771. }
  2772. else {
  2773. mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
  2774. mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  2775. mbox->vport = vport;
  2776. mbox->context2 = NULL;
  2777. rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  2778. if (rc == MBX_NOT_FINISHED) {
  2779. mempool_free(mbox, phba->mbox_mem_pool);
  2780. }
  2781. }
  2782. }
  2783. }
  2784. lpfc_cleanup_node(vport, ndlp);
  2785. /*
  2786. * We can get here with a non-NULL ndlp->rport because when we
  2787. * unregister a rport we don't break the rport/node linkage. So if we
  2788. * do, make sure we don't leaving any dangling pointers behind.
  2789. */
  2790. if (ndlp->rport) {
  2791. rdata = ndlp->rport->dd_data;
  2792. rdata->pnode = NULL;
  2793. ndlp->rport = NULL;
  2794. }
  2795. }
  2796. static int
  2797. lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  2798. uint32_t did)
  2799. {
  2800. D_ID mydid, ndlpdid, matchdid;
  2801. if (did == Bcast_DID)
  2802. return 0;
  2803. /* First check for Direct match */
  2804. if (ndlp->nlp_DID == did)
  2805. return 1;
  2806. /* Next check for area/domain identically equals 0 match */
  2807. mydid.un.word = vport->fc_myDID;
  2808. if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
  2809. return 0;
  2810. }
  2811. matchdid.un.word = did;
  2812. ndlpdid.un.word = ndlp->nlp_DID;
  2813. if (matchdid.un.b.id == ndlpdid.un.b.id) {
  2814. if ((mydid.un.b.domain == matchdid.un.b.domain) &&
  2815. (mydid.un.b.area == matchdid.un.b.area)) {
  2816. if ((ndlpdid.un.b.domain == 0) &&
  2817. (ndlpdid.un.b.area == 0)) {
  2818. if (ndlpdid.un.b.id)
  2819. return 1;
  2820. }
  2821. return 0;
  2822. }
  2823. matchdid.un.word = ndlp->nlp_DID;
  2824. if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
  2825. (mydid.un.b.area == ndlpdid.un.b.area)) {
  2826. if ((matchdid.un.b.domain == 0) &&
  2827. (matchdid.un.b.area == 0)) {
  2828. if (matchdid.un.b.id)
  2829. return 1;
  2830. }
  2831. }
  2832. }
  2833. return 0;
  2834. }
  2835. /* Search for a nodelist entry */
  2836. static struct lpfc_nodelist *
  2837. __lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
  2838. {
  2839. struct lpfc_nodelist *ndlp;
  2840. uint32_t data1;
  2841. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  2842. if (lpfc_matchdid(vport, ndlp, did)) {
  2843. data1 = (((uint32_t) ndlp->nlp_state << 24) |
  2844. ((uint32_t) ndlp->nlp_xri << 16) |
  2845. ((uint32_t) ndlp->nlp_type << 8) |
  2846. ((uint32_t) ndlp->nlp_rpi & 0xff));
  2847. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  2848. "0929 FIND node DID "
  2849. "Data: x%p x%x x%x x%x\n",
  2850. ndlp, ndlp->nlp_DID,
  2851. ndlp->nlp_flag, data1);
  2852. return ndlp;
  2853. }
  2854. }
  2855. /* FIND node did <did> NOT FOUND */
  2856. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
  2857. "0932 FIND node did x%x NOT FOUND.\n", did);
  2858. return NULL;
  2859. }
  2860. struct lpfc_nodelist *
  2861. lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
  2862. {
  2863. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2864. struct lpfc_nodelist *ndlp;
  2865. spin_lock_irq(shost->host_lock);
  2866. ndlp = __lpfc_findnode_did(vport, did);
  2867. spin_unlock_irq(shost->host_lock);
  2868. return ndlp;
  2869. }
  2870. struct lpfc_nodelist *
  2871. lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
  2872. {
  2873. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  2874. struct lpfc_nodelist *ndlp;
  2875. ndlp = lpfc_findnode_did(vport, did);
  2876. if (!ndlp) {
  2877. if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
  2878. lpfc_rscn_payload_check(vport, did) == 0)
  2879. return NULL;
  2880. ndlp = (struct lpfc_nodelist *)
  2881. mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
  2882. if (!ndlp)
  2883. return NULL;
  2884. lpfc_nlp_init(vport, ndlp, did);
  2885. lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
  2886. spin_lock_irq(shost->host_lock);
  2887. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  2888. spin_unlock_irq(shost->host_lock);
  2889. return ndlp;
  2890. } else if (!NLP_CHK_NODE_ACT(ndlp)) {
  2891. ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
  2892. if (!ndlp)
  2893. return NULL;
  2894. spin_lock_irq(shost->host_lock);
  2895. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  2896. spin_unlock_irq(shost->host_lock);
  2897. return ndlp;
  2898. }
  2899. if ((vport->fc_flag & FC_RSCN_MODE) &&
  2900. !(vport->fc_flag & FC_NDISC_ACTIVE)) {
  2901. if (lpfc_rscn_payload_check(vport, did)) {
  2902. /* If we've already recieved a PLOGI from this NPort
  2903. * we don't need to try to discover it again.
  2904. */
  2905. if (ndlp->nlp_flag & NLP_RCV_PLOGI)
  2906. return NULL;
  2907. /* Since this node is marked for discovery,
  2908. * delay timeout is not needed.
  2909. */
  2910. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  2911. spin_lock_irq(shost->host_lock);
  2912. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  2913. spin_unlock_irq(shost->host_lock);
  2914. } else
  2915. ndlp = NULL;
  2916. } else {
  2917. /* If we've already recieved a PLOGI from this NPort,
  2918. * or we are already in the process of discovery on it,
  2919. * we don't need to try to discover it again.
  2920. */
  2921. if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
  2922. ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
  2923. ndlp->nlp_flag & NLP_RCV_PLOGI)
  2924. return NULL;
  2925. lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
  2926. spin_lock_irq(shost->host_lock);
  2927. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  2928. spin_unlock_irq(shost->host_lock);
  2929. }
  2930. return ndlp;
  2931. }
  2932. /* Build a list of nodes to discover based on the loopmap */
  2933. void
  2934. lpfc_disc_list_loopmap(struct lpfc_vport *vport)
  2935. {
  2936. struct lpfc_hba *phba = vport->phba;
  2937. int j;
  2938. uint32_t alpa, index;
  2939. if (!lpfc_is_link_up(phba))
  2940. return;
  2941. if (phba->fc_topology != TOPOLOGY_LOOP)
  2942. return;
  2943. /* Check for loop map present or not */
  2944. if (phba->alpa_map[0]) {
  2945. for (j = 1; j <= phba->alpa_map[0]; j++) {
  2946. alpa = phba->alpa_map[j];
  2947. if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
  2948. continue;
  2949. lpfc_setup_disc_node(vport, alpa);
  2950. }
  2951. } else {
  2952. /* No alpamap, so try all alpa's */
  2953. for (j = 0; j < FC_MAXLOOP; j++) {
  2954. /* If cfg_scan_down is set, start from highest
  2955. * ALPA (0xef) to lowest (0x1).
  2956. */
  2957. if (vport->cfg_scan_down)
  2958. index = j;
  2959. else
  2960. index = FC_MAXLOOP - j - 1;
  2961. alpa = lpfcAlpaArray[index];
  2962. if ((vport->fc_myDID & 0xff) == alpa)
  2963. continue;
  2964. lpfc_setup_disc_node(vport, alpa);
  2965. }
  2966. }
  2967. return;
  2968. }
  2969. void
  2970. lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
  2971. {
  2972. LPFC_MBOXQ_t *mbox;
  2973. struct lpfc_sli *psli = &phba->sli;
  2974. struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
  2975. struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
  2976. struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
  2977. int rc;
  2978. /*
  2979. * if it's not a physical port or if we already send
  2980. * clear_la then don't send it.
  2981. */
  2982. if ((phba->link_state >= LPFC_CLEAR_LA) ||
  2983. (vport->port_type != LPFC_PHYSICAL_PORT) ||
  2984. (phba->sli_rev == LPFC_SLI_REV4))
  2985. return;
  2986. /* Link up discovery */
  2987. if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
  2988. phba->link_state = LPFC_CLEAR_LA;
  2989. lpfc_clear_la(phba, mbox);
  2990. mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
  2991. mbox->vport = vport;
  2992. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  2993. if (rc == MBX_NOT_FINISHED) {
  2994. mempool_free(mbox, phba->mbox_mem_pool);
  2995. lpfc_disc_flush_list(vport);
  2996. extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
  2997. fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
  2998. next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
  2999. phba->link_state = LPFC_HBA_ERROR;
  3000. }
  3001. }
  3002. }
  3003. /* Reg_vpi to tell firmware to resume normal operations */
  3004. void
  3005. lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
  3006. {
  3007. LPFC_MBOXQ_t *regvpimbox;
  3008. regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3009. if (regvpimbox) {
  3010. lpfc_reg_vpi(vport, regvpimbox);
  3011. regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
  3012. regvpimbox->vport = vport;
  3013. if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
  3014. == MBX_NOT_FINISHED) {
  3015. mempool_free(regvpimbox, phba->mbox_mem_pool);
  3016. }
  3017. }
  3018. }
  3019. /* Start Link up / RSCN discovery on NPR nodes */
  3020. void
  3021. lpfc_disc_start(struct lpfc_vport *vport)
  3022. {
  3023. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3024. struct lpfc_hba *phba = vport->phba;
  3025. uint32_t num_sent;
  3026. uint32_t clear_la_pending;
  3027. int did_changed;
  3028. if (!lpfc_is_link_up(phba))
  3029. return;
  3030. if (phba->link_state == LPFC_CLEAR_LA)
  3031. clear_la_pending = 1;
  3032. else
  3033. clear_la_pending = 0;
  3034. if (vport->port_state < LPFC_VPORT_READY)
  3035. vport->port_state = LPFC_DISC_AUTH;
  3036. lpfc_set_disctmo(vport);
  3037. if (vport->fc_prevDID == vport->fc_myDID)
  3038. did_changed = 0;
  3039. else
  3040. did_changed = 1;
  3041. vport->fc_prevDID = vport->fc_myDID;
  3042. vport->num_disc_nodes = 0;
  3043. /* Start Discovery state <hba_state> */
  3044. lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
  3045. "0202 Start Discovery hba state x%x "
  3046. "Data: x%x x%x x%x\n",
  3047. vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
  3048. vport->fc_adisc_cnt);
  3049. /* First do ADISCs - if any */
  3050. num_sent = lpfc_els_disc_adisc(vport);
  3051. if (num_sent)
  3052. return;
  3053. /*
  3054. * For SLI3, cmpl_reg_vpi will set port_state to READY, and
  3055. * continue discovery.
  3056. */
  3057. if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
  3058. !(vport->fc_flag & FC_PT2PT) &&
  3059. !(vport->fc_flag & FC_RSCN_MODE) &&
  3060. (phba->sli_rev < LPFC_SLI_REV4)) {
  3061. lpfc_issue_reg_vpi(phba, vport);
  3062. return;
  3063. }
  3064. /*
  3065. * For SLI2, we need to set port_state to READY and continue
  3066. * discovery.
  3067. */
  3068. if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
  3069. /* If we get here, there is nothing to ADISC */
  3070. if (vport->port_type == LPFC_PHYSICAL_PORT)
  3071. lpfc_issue_clear_la(phba, vport);
  3072. if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
  3073. vport->num_disc_nodes = 0;
  3074. /* go thru NPR nodes and issue ELS PLOGIs */
  3075. if (vport->fc_npr_cnt)
  3076. lpfc_els_disc_plogi(vport);
  3077. if (!vport->num_disc_nodes) {
  3078. spin_lock_irq(shost->host_lock);
  3079. vport->fc_flag &= ~FC_NDISC_ACTIVE;
  3080. spin_unlock_irq(shost->host_lock);
  3081. lpfc_can_disctmo(vport);
  3082. }
  3083. }
  3084. vport->port_state = LPFC_VPORT_READY;
  3085. } else {
  3086. /* Next do PLOGIs - if any */
  3087. num_sent = lpfc_els_disc_plogi(vport);
  3088. if (num_sent)
  3089. return;
  3090. if (vport->fc_flag & FC_RSCN_MODE) {
  3091. /* Check to see if more RSCNs came in while we
  3092. * were processing this one.
  3093. */
  3094. if ((vport->fc_rscn_id_cnt == 0) &&
  3095. (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
  3096. spin_lock_irq(shost->host_lock);
  3097. vport->fc_flag &= ~FC_RSCN_MODE;
  3098. spin_unlock_irq(shost->host_lock);
  3099. lpfc_can_disctmo(vport);
  3100. } else
  3101. lpfc_els_handle_rscn(vport);
  3102. }
  3103. }
  3104. return;
  3105. }
  3106. /*
  3107. * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
  3108. * ring the match the sppecified nodelist.
  3109. */
  3110. static void
  3111. lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
  3112. {
  3113. LIST_HEAD(completions);
  3114. struct lpfc_sli *psli;
  3115. IOCB_t *icmd;
  3116. struct lpfc_iocbq *iocb, *next_iocb;
  3117. struct lpfc_sli_ring *pring;
  3118. psli = &phba->sli;
  3119. pring = &psli->ring[LPFC_ELS_RING];
  3120. /* Error matching iocb on txq or txcmplq
  3121. * First check the txq.
  3122. */
  3123. spin_lock_irq(&phba->hbalock);
  3124. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  3125. if (iocb->context1 != ndlp) {
  3126. continue;
  3127. }
  3128. icmd = &iocb->iocb;
  3129. if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
  3130. (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
  3131. list_move_tail(&iocb->list, &completions);
  3132. pring->txq_cnt--;
  3133. }
  3134. }
  3135. /* Next check the txcmplq */
  3136. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
  3137. if (iocb->context1 != ndlp) {
  3138. continue;
  3139. }
  3140. icmd = &iocb->iocb;
  3141. if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
  3142. icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
  3143. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3144. }
  3145. }
  3146. spin_unlock_irq(&phba->hbalock);
  3147. /* Cancel all the IOCBs from the completions list */
  3148. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  3149. IOERR_SLI_ABORTED);
  3150. }
  3151. static void
  3152. lpfc_disc_flush_list(struct lpfc_vport *vport)
  3153. {
  3154. struct lpfc_nodelist *ndlp, *next_ndlp;
  3155. struct lpfc_hba *phba = vport->phba;
  3156. if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
  3157. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
  3158. nlp_listp) {
  3159. if (!NLP_CHK_NODE_ACT(ndlp))
  3160. continue;
  3161. if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
  3162. ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
  3163. lpfc_free_tx(phba, ndlp);
  3164. }
  3165. }
  3166. }
  3167. }
  3168. void
  3169. lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
  3170. {
  3171. lpfc_els_flush_rscn(vport);
  3172. lpfc_els_flush_cmd(vport);
  3173. lpfc_disc_flush_list(vport);
  3174. }
  3175. /*****************************************************************************/
  3176. /*
  3177. * NAME: lpfc_disc_timeout
  3178. *
  3179. * FUNCTION: Fibre Channel driver discovery timeout routine.
  3180. *
  3181. * EXECUTION ENVIRONMENT: interrupt only
  3182. *
  3183. * CALLED FROM:
  3184. * Timer function
  3185. *
  3186. * RETURNS:
  3187. * none
  3188. */
  3189. /*****************************************************************************/
  3190. void
  3191. lpfc_disc_timeout(unsigned long ptr)
  3192. {
  3193. struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
  3194. struct lpfc_hba *phba = vport->phba;
  3195. uint32_t tmo_posted;
  3196. unsigned long flags = 0;
  3197. if (unlikely(!phba))
  3198. return;
  3199. spin_lock_irqsave(&vport->work_port_lock, flags);
  3200. tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
  3201. if (!tmo_posted)
  3202. vport->work_port_events |= WORKER_DISC_TMO;
  3203. spin_unlock_irqrestore(&vport->work_port_lock, flags);
  3204. if (!tmo_posted)
  3205. lpfc_worker_wake_up(phba);
  3206. return;
  3207. }
  3208. static void
  3209. lpfc_disc_timeout_handler(struct lpfc_vport *vport)
  3210. {
  3211. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3212. struct lpfc_hba *phba = vport->phba;
  3213. struct lpfc_sli *psli = &phba->sli;
  3214. struct lpfc_nodelist *ndlp, *next_ndlp;
  3215. LPFC_MBOXQ_t *initlinkmbox;
  3216. int rc, clrlaerr = 0;
  3217. if (!(vport->fc_flag & FC_DISC_TMO))
  3218. return;
  3219. spin_lock_irq(shost->host_lock);
  3220. vport->fc_flag &= ~FC_DISC_TMO;
  3221. spin_unlock_irq(shost->host_lock);
  3222. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
  3223. "disc timeout: state:x%x rtry:x%x flg:x%x",
  3224. vport->port_state, vport->fc_ns_retry, vport->fc_flag);
  3225. switch (vport->port_state) {
  3226. case LPFC_LOCAL_CFG_LINK:
  3227. /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
  3228. * FAN
  3229. */
  3230. /* FAN timeout */
  3231. lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
  3232. "0221 FAN timeout\n");
  3233. /* Start discovery by sending FLOGI, clean up old rpis */
  3234. list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
  3235. nlp_listp) {
  3236. if (!NLP_CHK_NODE_ACT(ndlp))
  3237. continue;
  3238. if (ndlp->nlp_state != NLP_STE_NPR_NODE)
  3239. continue;
  3240. if (ndlp->nlp_type & NLP_FABRIC) {
  3241. /* Clean up the ndlp on Fabric connections */
  3242. lpfc_drop_node(vport, ndlp);
  3243. } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
  3244. /* Fail outstanding IO now since device
  3245. * is marked for PLOGI.
  3246. */
  3247. lpfc_unreg_rpi(vport, ndlp);
  3248. }
  3249. }
  3250. if (vport->port_state != LPFC_FLOGI) {
  3251. lpfc_initial_flogi(vport);
  3252. return;
  3253. }
  3254. break;
  3255. case LPFC_FDISC:
  3256. case LPFC_FLOGI:
  3257. /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
  3258. /* Initial FLOGI timeout */
  3259. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  3260. "0222 Initial %s timeout\n",
  3261. vport->vpi ? "FDISC" : "FLOGI");
  3262. /* Assume no Fabric and go on with discovery.
  3263. * Check for outstanding ELS FLOGI to abort.
  3264. */
  3265. /* FLOGI failed, so just use loop map to make discovery list */
  3266. lpfc_disc_list_loopmap(vport);
  3267. /* Start discovery */
  3268. lpfc_disc_start(vport);
  3269. break;
  3270. case LPFC_FABRIC_CFG_LINK:
  3271. /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
  3272. NameServer login */
  3273. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  3274. "0223 Timeout while waiting for "
  3275. "NameServer login\n");
  3276. /* Next look for NameServer ndlp */
  3277. ndlp = lpfc_findnode_did(vport, NameServer_DID);
  3278. if (ndlp && NLP_CHK_NODE_ACT(ndlp))
  3279. lpfc_els_abort(phba, ndlp);
  3280. /* ReStart discovery */
  3281. goto restart_disc;
  3282. case LPFC_NS_QRY:
  3283. /* Check for wait for NameServer Rsp timeout */
  3284. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  3285. "0224 NameServer Query timeout "
  3286. "Data: x%x x%x\n",
  3287. vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
  3288. if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
  3289. /* Try it one more time */
  3290. vport->fc_ns_retry++;
  3291. rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
  3292. vport->fc_ns_retry, 0);
  3293. if (rc == 0)
  3294. break;
  3295. }
  3296. vport->fc_ns_retry = 0;
  3297. restart_disc:
  3298. /*
  3299. * Discovery is over.
  3300. * set port_state to PORT_READY if SLI2.
  3301. * cmpl_reg_vpi will set port_state to READY for SLI3.
  3302. */
  3303. if (phba->sli_rev < LPFC_SLI_REV4) {
  3304. if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
  3305. lpfc_issue_reg_vpi(phba, vport);
  3306. else { /* NPIV Not enabled */
  3307. lpfc_issue_clear_la(phba, vport);
  3308. vport->port_state = LPFC_VPORT_READY;
  3309. }
  3310. }
  3311. /* Setup and issue mailbox INITIALIZE LINK command */
  3312. initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3313. if (!initlinkmbox) {
  3314. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  3315. "0206 Device Discovery "
  3316. "completion error\n");
  3317. phba->link_state = LPFC_HBA_ERROR;
  3318. break;
  3319. }
  3320. lpfc_linkdown(phba);
  3321. lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
  3322. phba->cfg_link_speed);
  3323. initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
  3324. initlinkmbox->vport = vport;
  3325. initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3326. rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
  3327. lpfc_set_loopback_flag(phba);
  3328. if (rc == MBX_NOT_FINISHED)
  3329. mempool_free(initlinkmbox, phba->mbox_mem_pool);
  3330. break;
  3331. case LPFC_DISC_AUTH:
  3332. /* Node Authentication timeout */
  3333. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  3334. "0227 Node Authentication timeout\n");
  3335. lpfc_disc_flush_list(vport);
  3336. /*
  3337. * set port_state to PORT_READY if SLI2.
  3338. * cmpl_reg_vpi will set port_state to READY for SLI3.
  3339. */
  3340. if (phba->sli_rev < LPFC_SLI_REV4) {
  3341. if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
  3342. lpfc_issue_reg_vpi(phba, vport);
  3343. else { /* NPIV Not enabled */
  3344. lpfc_issue_clear_la(phba, vport);
  3345. vport->port_state = LPFC_VPORT_READY;
  3346. }
  3347. }
  3348. break;
  3349. case LPFC_VPORT_READY:
  3350. if (vport->fc_flag & FC_RSCN_MODE) {
  3351. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  3352. "0231 RSCN timeout Data: x%x "
  3353. "x%x\n",
  3354. vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
  3355. /* Cleanup any outstanding ELS commands */
  3356. lpfc_els_flush_cmd(vport);
  3357. lpfc_els_flush_rscn(vport);
  3358. lpfc_disc_flush_list(vport);
  3359. }
  3360. break;
  3361. default:
  3362. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  3363. "0273 Unexpected discovery timeout, "
  3364. "vport State x%x\n", vport->port_state);
  3365. break;
  3366. }
  3367. switch (phba->link_state) {
  3368. case LPFC_CLEAR_LA:
  3369. /* CLEAR LA timeout */
  3370. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  3371. "0228 CLEAR LA timeout\n");
  3372. clrlaerr = 1;
  3373. break;
  3374. case LPFC_LINK_UP:
  3375. lpfc_issue_clear_la(phba, vport);
  3376. /* Drop thru */
  3377. case LPFC_LINK_UNKNOWN:
  3378. case LPFC_WARM_START:
  3379. case LPFC_INIT_START:
  3380. case LPFC_INIT_MBX_CMDS:
  3381. case LPFC_LINK_DOWN:
  3382. case LPFC_HBA_ERROR:
  3383. lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
  3384. "0230 Unexpected timeout, hba link "
  3385. "state x%x\n", phba->link_state);
  3386. clrlaerr = 1;
  3387. break;
  3388. case LPFC_HBA_READY:
  3389. break;
  3390. }
  3391. if (clrlaerr) {
  3392. lpfc_disc_flush_list(vport);
  3393. psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
  3394. psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
  3395. psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
  3396. vport->port_state = LPFC_VPORT_READY;
  3397. }
  3398. return;
  3399. }
  3400. /*
  3401. * This routine handles processing a NameServer REG_LOGIN mailbox
  3402. * command upon completion. It is setup in the LPFC_MBOXQ
  3403. * as the completion routine when the command is
  3404. * handed off to the SLI layer.
  3405. */
  3406. void
  3407. lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  3408. {
  3409. MAILBOX_t *mb = &pmb->u.mb;
  3410. struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
  3411. struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
  3412. struct lpfc_vport *vport = pmb->vport;
  3413. pmb->context1 = NULL;
  3414. ndlp->nlp_rpi = mb->un.varWords[0];
  3415. ndlp->nlp_flag |= NLP_RPI_VALID;
  3416. ndlp->nlp_type |= NLP_FABRIC;
  3417. lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
  3418. /*
  3419. * Start issuing Fabric-Device Management Interface (FDMI) command to
  3420. * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
  3421. * fdmi-on=2 (supporting RPA/hostnmae)
  3422. */
  3423. if (vport->cfg_fdmi_on == 1)
  3424. lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
  3425. else
  3426. mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
  3427. /* decrement the node reference count held for this callback
  3428. * function.
  3429. */
  3430. lpfc_nlp_put(ndlp);
  3431. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  3432. kfree(mp);
  3433. mempool_free(pmb, phba->mbox_mem_pool);
  3434. return;
  3435. }
  3436. static int
  3437. lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
  3438. {
  3439. uint16_t *rpi = param;
  3440. return ndlp->nlp_rpi == *rpi;
  3441. }
  3442. static int
  3443. lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
  3444. {
  3445. return memcmp(&ndlp->nlp_portname, param,
  3446. sizeof(ndlp->nlp_portname)) == 0;
  3447. }
  3448. static struct lpfc_nodelist *
  3449. __lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
  3450. {
  3451. struct lpfc_nodelist *ndlp;
  3452. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  3453. if (filter(ndlp, param))
  3454. return ndlp;
  3455. }
  3456. return NULL;
  3457. }
  3458. /*
  3459. * This routine looks up the ndlp lists for the given RPI. If rpi found it
  3460. * returns the node list element pointer else return NULL.
  3461. */
  3462. struct lpfc_nodelist *
  3463. __lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
  3464. {
  3465. return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
  3466. }
  3467. /*
  3468. * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
  3469. * returns the node element list pointer else return NULL.
  3470. */
  3471. struct lpfc_nodelist *
  3472. lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
  3473. {
  3474. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  3475. struct lpfc_nodelist *ndlp;
  3476. spin_lock_irq(shost->host_lock);
  3477. ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
  3478. spin_unlock_irq(shost->host_lock);
  3479. return ndlp;
  3480. }
  3481. void
  3482. lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
  3483. uint32_t did)
  3484. {
  3485. memset(ndlp, 0, sizeof (struct lpfc_nodelist));
  3486. lpfc_initialize_node(vport, ndlp, did);
  3487. INIT_LIST_HEAD(&ndlp->nlp_listp);
  3488. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
  3489. "node init: did:x%x",
  3490. ndlp->nlp_DID, 0, 0);
  3491. return;
  3492. }
  3493. /* This routine releases all resources associated with a specifc NPort's ndlp
  3494. * and mempool_free's the nodelist.
  3495. */
  3496. static void
  3497. lpfc_nlp_release(struct kref *kref)
  3498. {
  3499. struct lpfc_hba *phba;
  3500. unsigned long flags;
  3501. struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
  3502. kref);
  3503. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
  3504. "node release: did:x%x flg:x%x type:x%x",
  3505. ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
  3506. lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
  3507. "0279 lpfc_nlp_release: ndlp:x%p "
  3508. "usgmap:x%x refcnt:%d\n",
  3509. (void *)ndlp, ndlp->nlp_usg_map,
  3510. atomic_read(&ndlp->kref.refcount));
  3511. /* remove ndlp from action. */
  3512. lpfc_nlp_remove(ndlp->vport, ndlp);
  3513. /* clear the ndlp active flag for all release cases */
  3514. phba = ndlp->phba;
  3515. spin_lock_irqsave(&phba->ndlp_lock, flags);
  3516. NLP_CLR_NODE_ACT(ndlp);
  3517. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  3518. /* free ndlp memory for final ndlp release */
  3519. if (NLP_CHK_FREE_REQ(ndlp)) {
  3520. kfree(ndlp->lat_data);
  3521. mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
  3522. }
  3523. }
  3524. /* This routine bumps the reference count for a ndlp structure to ensure
  3525. * that one discovery thread won't free a ndlp while another discovery thread
  3526. * is using it.
  3527. */
  3528. struct lpfc_nodelist *
  3529. lpfc_nlp_get(struct lpfc_nodelist *ndlp)
  3530. {
  3531. struct lpfc_hba *phba;
  3532. unsigned long flags;
  3533. if (ndlp) {
  3534. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
  3535. "node get: did:x%x flg:x%x refcnt:x%x",
  3536. ndlp->nlp_DID, ndlp->nlp_flag,
  3537. atomic_read(&ndlp->kref.refcount));
  3538. /* The check of ndlp usage to prevent incrementing the
  3539. * ndlp reference count that is in the process of being
  3540. * released.
  3541. */
  3542. phba = ndlp->phba;
  3543. spin_lock_irqsave(&phba->ndlp_lock, flags);
  3544. if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
  3545. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  3546. lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
  3547. "0276 lpfc_nlp_get: ndlp:x%p "
  3548. "usgmap:x%x refcnt:%d\n",
  3549. (void *)ndlp, ndlp->nlp_usg_map,
  3550. atomic_read(&ndlp->kref.refcount));
  3551. return NULL;
  3552. } else
  3553. kref_get(&ndlp->kref);
  3554. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  3555. }
  3556. return ndlp;
  3557. }
  3558. /* This routine decrements the reference count for a ndlp structure. If the
  3559. * count goes to 0, this indicates the the associated nodelist should be
  3560. * freed. Returning 1 indicates the ndlp resource has been released; on the
  3561. * other hand, returning 0 indicates the ndlp resource has not been released
  3562. * yet.
  3563. */
  3564. int
  3565. lpfc_nlp_put(struct lpfc_nodelist *ndlp)
  3566. {
  3567. struct lpfc_hba *phba;
  3568. unsigned long flags;
  3569. if (!ndlp)
  3570. return 1;
  3571. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
  3572. "node put: did:x%x flg:x%x refcnt:x%x",
  3573. ndlp->nlp_DID, ndlp->nlp_flag,
  3574. atomic_read(&ndlp->kref.refcount));
  3575. phba = ndlp->phba;
  3576. spin_lock_irqsave(&phba->ndlp_lock, flags);
  3577. /* Check the ndlp memory free acknowledge flag to avoid the
  3578. * possible race condition that kref_put got invoked again
  3579. * after previous one has done ndlp memory free.
  3580. */
  3581. if (NLP_CHK_FREE_ACK(ndlp)) {
  3582. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  3583. lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
  3584. "0274 lpfc_nlp_put: ndlp:x%p "
  3585. "usgmap:x%x refcnt:%d\n",
  3586. (void *)ndlp, ndlp->nlp_usg_map,
  3587. atomic_read(&ndlp->kref.refcount));
  3588. return 1;
  3589. }
  3590. /* Check the ndlp inactivate log flag to avoid the possible
  3591. * race condition that kref_put got invoked again after ndlp
  3592. * is already in inactivating state.
  3593. */
  3594. if (NLP_CHK_IACT_REQ(ndlp)) {
  3595. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  3596. lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
  3597. "0275 lpfc_nlp_put: ndlp:x%p "
  3598. "usgmap:x%x refcnt:%d\n",
  3599. (void *)ndlp, ndlp->nlp_usg_map,
  3600. atomic_read(&ndlp->kref.refcount));
  3601. return 1;
  3602. }
  3603. /* For last put, mark the ndlp usage flags to make sure no
  3604. * other kref_get and kref_put on the same ndlp shall get
  3605. * in between the process when the final kref_put has been
  3606. * invoked on this ndlp.
  3607. */
  3608. if (atomic_read(&ndlp->kref.refcount) == 1) {
  3609. /* Indicate ndlp is put to inactive state. */
  3610. NLP_SET_IACT_REQ(ndlp);
  3611. /* Acknowledge ndlp memory free has been seen. */
  3612. if (NLP_CHK_FREE_REQ(ndlp))
  3613. NLP_SET_FREE_ACK(ndlp);
  3614. }
  3615. spin_unlock_irqrestore(&phba->ndlp_lock, flags);
  3616. /* Note, the kref_put returns 1 when decrementing a reference
  3617. * count that was 1, it invokes the release callback function,
  3618. * but it still left the reference count as 1 (not actually
  3619. * performs the last decrementation). Otherwise, it actually
  3620. * decrements the reference count and returns 0.
  3621. */
  3622. return kref_put(&ndlp->kref, lpfc_nlp_release);
  3623. }
  3624. /* This routine free's the specified nodelist if it is not in use
  3625. * by any other discovery thread. This routine returns 1 if the
  3626. * ndlp has been freed. A return value of 0 indicates the ndlp is
  3627. * not yet been released.
  3628. */
  3629. int
  3630. lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
  3631. {
  3632. lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
  3633. "node not used: did:x%x flg:x%x refcnt:x%x",
  3634. ndlp->nlp_DID, ndlp->nlp_flag,
  3635. atomic_read(&ndlp->kref.refcount));
  3636. if (atomic_read(&ndlp->kref.refcount) == 1)
  3637. if (lpfc_nlp_put(ndlp))
  3638. return 1;
  3639. return 0;
  3640. }
  3641. /**
  3642. * lpfc_fcf_inuse - Check if FCF can be unregistered.
  3643. * @phba: Pointer to hba context object.
  3644. *
  3645. * This function iterate through all FC nodes associated
  3646. * will all vports to check if there is any node with
  3647. * fc_rports associated with it. If there is an fc_rport
  3648. * associated with the node, then the node is either in
  3649. * discovered state or its devloss_timer is pending.
  3650. */
  3651. static int
  3652. lpfc_fcf_inuse(struct lpfc_hba *phba)
  3653. {
  3654. struct lpfc_vport **vports;
  3655. int i, ret = 0;
  3656. struct lpfc_nodelist *ndlp;
  3657. struct Scsi_Host *shost;
  3658. vports = lpfc_create_vport_work_array(phba);
  3659. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  3660. shost = lpfc_shost_from_vport(vports[i]);
  3661. spin_lock_irq(shost->host_lock);
  3662. list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
  3663. if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
  3664. (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
  3665. ret = 1;
  3666. spin_unlock_irq(shost->host_lock);
  3667. goto out;
  3668. }
  3669. }
  3670. spin_unlock_irq(shost->host_lock);
  3671. }
  3672. out:
  3673. lpfc_destroy_vport_work_array(phba, vports);
  3674. return ret;
  3675. }
  3676. /**
  3677. * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
  3678. * @phba: Pointer to hba context object.
  3679. * @mboxq: Pointer to mailbox object.
  3680. *
  3681. * This function frees memory associated with the mailbox command.
  3682. */
  3683. static void
  3684. lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  3685. {
  3686. struct lpfc_vport *vport = mboxq->vport;
  3687. if (mboxq->u.mb.mbxStatus) {
  3688. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  3689. "2555 UNREG_VFI mbxStatus error x%x "
  3690. "HBA state x%x\n",
  3691. mboxq->u.mb.mbxStatus, vport->port_state);
  3692. }
  3693. mempool_free(mboxq, phba->mbox_mem_pool);
  3694. return;
  3695. }
  3696. /**
  3697. * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
  3698. * @phba: Pointer to hba context object.
  3699. * @mboxq: Pointer to mailbox object.
  3700. *
  3701. * This function frees memory associated with the mailbox command.
  3702. */
  3703. static void
  3704. lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  3705. {
  3706. struct lpfc_vport *vport = mboxq->vport;
  3707. if (mboxq->u.mb.mbxStatus) {
  3708. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  3709. "2550 UNREG_FCFI mbxStatus error x%x "
  3710. "HBA state x%x\n",
  3711. mboxq->u.mb.mbxStatus, vport->port_state);
  3712. }
  3713. mempool_free(mboxq, phba->mbox_mem_pool);
  3714. return;
  3715. }
  3716. /**
  3717. * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
  3718. * @phba: Pointer to hba context object.
  3719. *
  3720. * This function check if there are any connected remote port for the FCF and
  3721. * if all the devices are disconnected, this function unregister FCFI.
  3722. * This function also tries to use another FCF for discovery.
  3723. */
  3724. void
  3725. lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
  3726. {
  3727. LPFC_MBOXQ_t *mbox;
  3728. int rc;
  3729. struct lpfc_vport **vports;
  3730. int i;
  3731. spin_lock_irq(&phba->hbalock);
  3732. /*
  3733. * If HBA is not running in FIP mode or
  3734. * If HBA does not support FCoE or
  3735. * If FCF is not registered.
  3736. * do nothing.
  3737. */
  3738. if (!(phba->hba_flag & HBA_FCOE_SUPPORT) ||
  3739. !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
  3740. (phba->cfg_enable_fip == 0)) {
  3741. spin_unlock_irq(&phba->hbalock);
  3742. return;
  3743. }
  3744. spin_unlock_irq(&phba->hbalock);
  3745. if (lpfc_fcf_inuse(phba))
  3746. return;
  3747. /* Unregister VPIs */
  3748. vports = lpfc_create_vport_work_array(phba);
  3749. if (vports &&
  3750. (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
  3751. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  3752. lpfc_mbx_unreg_vpi(vports[i]);
  3753. vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
  3754. vports[i]->vfi_state &= ~LPFC_VFI_REGISTERED;
  3755. }
  3756. lpfc_destroy_vport_work_array(phba, vports);
  3757. /* Unregister VFI */
  3758. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3759. if (!mbox) {
  3760. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  3761. "2556 UNREG_VFI mbox allocation failed"
  3762. "HBA state x%x\n",
  3763. phba->pport->port_state);
  3764. return;
  3765. }
  3766. lpfc_unreg_vfi(mbox, phba->pport->vfi);
  3767. mbox->vport = phba->pport;
  3768. mbox->mbox_cmpl = lpfc_unregister_vfi_cmpl;
  3769. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  3770. if (rc == MBX_NOT_FINISHED) {
  3771. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  3772. "2557 UNREG_VFI issue mbox failed rc x%x "
  3773. "HBA state x%x\n",
  3774. rc, phba->pport->port_state);
  3775. mempool_free(mbox, phba->mbox_mem_pool);
  3776. return;
  3777. }
  3778. /* Unregister FCF */
  3779. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3780. if (!mbox) {
  3781. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  3782. "2551 UNREG_FCFI mbox allocation failed"
  3783. "HBA state x%x\n",
  3784. phba->pport->port_state);
  3785. return;
  3786. }
  3787. lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
  3788. mbox->vport = phba->pport;
  3789. mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
  3790. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  3791. if (rc == MBX_NOT_FINISHED) {
  3792. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  3793. "2552 UNREG_FCFI issue mbox failed rc x%x "
  3794. "HBA state x%x\n",
  3795. rc, phba->pport->port_state);
  3796. mempool_free(mbox, phba->mbox_mem_pool);
  3797. return;
  3798. }
  3799. spin_lock_irq(&phba->hbalock);
  3800. phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_REGISTERED |
  3801. FCF_DISCOVERED | FCF_BOOT_ENABLE | FCF_IN_USE |
  3802. FCF_VALID_VLAN);
  3803. spin_unlock_irq(&phba->hbalock);
  3804. /*
  3805. * If driver is not unloading, check if there is any other
  3806. * FCF record that can be used for discovery.
  3807. */
  3808. if ((phba->pport->load_flag & FC_UNLOADING) ||
  3809. (phba->link_state < LPFC_LINK_UP))
  3810. return;
  3811. rc = lpfc_sli4_read_fcf_record(phba, LPFC_FCOE_FCF_GET_FIRST);
  3812. if (rc)
  3813. lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
  3814. "2553 lpfc_unregister_unused_fcf failed to read FCF"
  3815. " record HBA state x%x\n",
  3816. phba->pport->port_state);
  3817. }
  3818. /**
  3819. * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
  3820. * @phba: Pointer to hba context object.
  3821. * @buff: Buffer containing the FCF connection table as in the config
  3822. * region.
  3823. * This function create driver data structure for the FCF connection
  3824. * record table read from config region 23.
  3825. */
  3826. static void
  3827. lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
  3828. uint8_t *buff)
  3829. {
  3830. struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
  3831. struct lpfc_fcf_conn_hdr *conn_hdr;
  3832. struct lpfc_fcf_conn_rec *conn_rec;
  3833. uint32_t record_count;
  3834. int i;
  3835. /* Free the current connect table */
  3836. list_for_each_entry_safe(conn_entry, next_conn_entry,
  3837. &phba->fcf_conn_rec_list, list)
  3838. kfree(conn_entry);
  3839. conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
  3840. record_count = conn_hdr->length * sizeof(uint32_t)/
  3841. sizeof(struct lpfc_fcf_conn_rec);
  3842. conn_rec = (struct lpfc_fcf_conn_rec *)
  3843. (buff + sizeof(struct lpfc_fcf_conn_hdr));
  3844. for (i = 0; i < record_count; i++) {
  3845. if (!(conn_rec[i].flags & FCFCNCT_VALID))
  3846. continue;
  3847. conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
  3848. GFP_KERNEL);
  3849. if (!conn_entry) {
  3850. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3851. "2566 Failed to allocate connection"
  3852. " table entry\n");
  3853. return;
  3854. }
  3855. memcpy(&conn_entry->conn_rec, &conn_rec[i],
  3856. sizeof(struct lpfc_fcf_conn_rec));
  3857. conn_entry->conn_rec.vlan_tag =
  3858. le16_to_cpu(conn_entry->conn_rec.vlan_tag) & 0xFFF;
  3859. conn_entry->conn_rec.flags =
  3860. le16_to_cpu(conn_entry->conn_rec.flags);
  3861. list_add_tail(&conn_entry->list,
  3862. &phba->fcf_conn_rec_list);
  3863. }
  3864. }
  3865. /**
  3866. * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
  3867. * @phba: Pointer to hba context object.
  3868. * @buff: Buffer containing the FCoE parameter data structure.
  3869. *
  3870. * This function update driver data structure with config
  3871. * parameters read from config region 23.
  3872. */
  3873. static void
  3874. lpfc_read_fcoe_param(struct lpfc_hba *phba,
  3875. uint8_t *buff)
  3876. {
  3877. struct lpfc_fip_param_hdr *fcoe_param_hdr;
  3878. struct lpfc_fcoe_params *fcoe_param;
  3879. fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
  3880. buff;
  3881. fcoe_param = (struct lpfc_fcoe_params *)
  3882. buff + sizeof(struct lpfc_fip_param_hdr);
  3883. if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
  3884. (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
  3885. return;
  3886. if (bf_get(lpfc_fip_param_hdr_fipp_mode, fcoe_param_hdr) ==
  3887. FIPP_MODE_ON)
  3888. phba->cfg_enable_fip = 1;
  3889. if (bf_get(lpfc_fip_param_hdr_fipp_mode, fcoe_param_hdr) ==
  3890. FIPP_MODE_OFF)
  3891. phba->cfg_enable_fip = 0;
  3892. if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
  3893. phba->valid_vlan = 1;
  3894. phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
  3895. 0xFFF;
  3896. }
  3897. phba->fc_map[0] = fcoe_param->fc_map[0];
  3898. phba->fc_map[1] = fcoe_param->fc_map[1];
  3899. phba->fc_map[2] = fcoe_param->fc_map[2];
  3900. return;
  3901. }
  3902. /**
  3903. * lpfc_get_rec_conf23 - Get a record type in config region data.
  3904. * @buff: Buffer containing config region 23 data.
  3905. * @size: Size of the data buffer.
  3906. * @rec_type: Record type to be searched.
  3907. *
  3908. * This function searches config region data to find the begining
  3909. * of the record specified by record_type. If record found, this
  3910. * function return pointer to the record else return NULL.
  3911. */
  3912. static uint8_t *
  3913. lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
  3914. {
  3915. uint32_t offset = 0, rec_length;
  3916. if ((buff[0] == LPFC_REGION23_LAST_REC) ||
  3917. (size < sizeof(uint32_t)))
  3918. return NULL;
  3919. rec_length = buff[offset + 1];
  3920. /*
  3921. * One TLV record has one word header and number of data words
  3922. * specified in the rec_length field of the record header.
  3923. */
  3924. while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
  3925. <= size) {
  3926. if (buff[offset] == rec_type)
  3927. return &buff[offset];
  3928. if (buff[offset] == LPFC_REGION23_LAST_REC)
  3929. return NULL;
  3930. offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
  3931. rec_length = buff[offset + 1];
  3932. }
  3933. return NULL;
  3934. }
  3935. /**
  3936. * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
  3937. * @phba: Pointer to lpfc_hba data structure.
  3938. * @buff: Buffer containing config region 23 data.
  3939. * @size: Size of the data buffer.
  3940. *
  3941. * This fuction parse the FCoE config parameters in config region 23 and
  3942. * populate driver data structure with the parameters.
  3943. */
  3944. void
  3945. lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
  3946. uint8_t *buff,
  3947. uint32_t size)
  3948. {
  3949. uint32_t offset = 0, rec_length;
  3950. uint8_t *rec_ptr;
  3951. /*
  3952. * If data size is less than 2 words signature and version cannot be
  3953. * verified.
  3954. */
  3955. if (size < 2*sizeof(uint32_t))
  3956. return;
  3957. /* Check the region signature first */
  3958. if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
  3959. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3960. "2567 Config region 23 has bad signature\n");
  3961. return;
  3962. }
  3963. offset += 4;
  3964. /* Check the data structure version */
  3965. if (buff[offset] != LPFC_REGION23_VERSION) {
  3966. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3967. "2568 Config region 23 has bad version\n");
  3968. return;
  3969. }
  3970. offset += 4;
  3971. rec_length = buff[offset + 1];
  3972. /* Read FCoE param record */
  3973. rec_ptr = lpfc_get_rec_conf23(&buff[offset],
  3974. size - offset, FCOE_PARAM_TYPE);
  3975. if (rec_ptr)
  3976. lpfc_read_fcoe_param(phba, rec_ptr);
  3977. /* Read FCF connection table */
  3978. rec_ptr = lpfc_get_rec_conf23(&buff[offset],
  3979. size - offset, FCOE_CONN_TBL_TYPE);
  3980. if (rec_ptr)
  3981. lpfc_read_fcf_conn_tbl(phba, rec_ptr);
  3982. }