lpfc_hbadisc.c 152 KB

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