lpfc_hbadisc.c 158 KB

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