lpfc_hbadisc.c 153 KB

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