lpfc_hbadisc.c 141 KB

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