lpfc_hbadisc.c 154 KB

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