lpfc_sli.c 165 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2008 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/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <scsi/scsi.h>
  26. #include <scsi/scsi_cmnd.h>
  27. #include <scsi/scsi_device.h>
  28. #include <scsi/scsi_host.h>
  29. #include <scsi/scsi_transport_fc.h>
  30. #include "lpfc_hw.h"
  31. #include "lpfc_sli.h"
  32. #include "lpfc_nl.h"
  33. #include "lpfc_disc.h"
  34. #include "lpfc_scsi.h"
  35. #include "lpfc.h"
  36. #include "lpfc_crtn.h"
  37. #include "lpfc_logmsg.h"
  38. #include "lpfc_compat.h"
  39. #include "lpfc_debugfs.h"
  40. /*
  41. * Define macro to log: Mailbox command x%x cannot issue Data
  42. * This allows multiple uses of lpfc_msgBlk0311
  43. * w/o perturbing log msg utility.
  44. */
  45. #define LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag) \
  46. lpfc_printf_log(phba, \
  47. KERN_INFO, \
  48. LOG_MBOX | LOG_SLI, \
  49. "(%d):0311 Mailbox command x%x cannot " \
  50. "issue Data: x%x x%x x%x\n", \
  51. pmbox->vport ? pmbox->vport->vpi : 0, \
  52. pmbox->mb.mbxCommand, \
  53. phba->pport->port_state, \
  54. psli->sli_flag, \
  55. flag)
  56. /* There are only four IOCB completion types. */
  57. typedef enum _lpfc_iocb_type {
  58. LPFC_UNKNOWN_IOCB,
  59. LPFC_UNSOL_IOCB,
  60. LPFC_SOL_IOCB,
  61. LPFC_ABORT_IOCB
  62. } lpfc_iocb_type;
  63. /**
  64. * lpfc_cmd_iocb: Get next command iocb entry in the ring.
  65. * @phba: Pointer to HBA context object.
  66. * @pring: Pointer to driver SLI ring object.
  67. *
  68. * This function returns pointer to next command iocb entry
  69. * in the command ring. The caller must hold hbalock to prevent
  70. * other threads consume the next command iocb.
  71. * SLI-2/SLI-3 provide different sized iocbs.
  72. **/
  73. static inline IOCB_t *
  74. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  75. {
  76. return (IOCB_t *) (((char *) pring->cmdringaddr) +
  77. pring->cmdidx * phba->iocb_cmd_size);
  78. }
  79. /**
  80. * lpfc_resp_iocb: Get next response iocb entry in the ring.
  81. * @phba: Pointer to HBA context object.
  82. * @pring: Pointer to driver SLI ring object.
  83. *
  84. * This function returns pointer to next response iocb entry
  85. * in the response ring. The caller must hold hbalock to make sure
  86. * that no other thread consume the next response iocb.
  87. * SLI-2/SLI-3 provide different sized iocbs.
  88. **/
  89. static inline IOCB_t *
  90. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  91. {
  92. return (IOCB_t *) (((char *) pring->rspringaddr) +
  93. pring->rspidx * phba->iocb_rsp_size);
  94. }
  95. /**
  96. * __lpfc_sli_get_iocbq: Allocates an iocb object from iocb pool.
  97. * @phba: Pointer to HBA context object.
  98. *
  99. * This function is called with hbalock held. This function
  100. * allocates a new driver iocb object from the iocb pool. If the
  101. * allocation is successful, it returns pointer to the newly
  102. * allocated iocb object else it returns NULL.
  103. **/
  104. static struct lpfc_iocbq *
  105. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  106. {
  107. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  108. struct lpfc_iocbq * iocbq = NULL;
  109. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  110. return iocbq;
  111. }
  112. /**
  113. * lpfc_sli_get_iocbq: Allocates an iocb object from iocb pool.
  114. * @phba: Pointer to HBA context object.
  115. *
  116. * This function is called with no lock held. This function
  117. * allocates a new driver iocb object from the iocb pool. If the
  118. * allocation is successful, it returns pointer to the newly
  119. * allocated iocb object else it returns NULL.
  120. **/
  121. struct lpfc_iocbq *
  122. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  123. {
  124. struct lpfc_iocbq * iocbq = NULL;
  125. unsigned long iflags;
  126. spin_lock_irqsave(&phba->hbalock, iflags);
  127. iocbq = __lpfc_sli_get_iocbq(phba);
  128. spin_unlock_irqrestore(&phba->hbalock, iflags);
  129. return iocbq;
  130. }
  131. /**
  132. * __lpfc_sli_release_iocbq: Release iocb to the iocb pool.
  133. * @phba: Pointer to HBA context object.
  134. * @iocbq: Pointer to driver iocb object.
  135. *
  136. * This function is called with hbalock held to release driver
  137. * iocb object to the iocb pool. The iotag in the iocb object
  138. * does not change for each use of the iocb object. This function
  139. * clears all other fields of the iocb object when it is freed.
  140. **/
  141. static void
  142. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  143. {
  144. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  145. /*
  146. * Clean all volatile data fields, preserve iotag and node struct.
  147. */
  148. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  149. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  150. }
  151. /**
  152. * lpfc_sli_release_iocbq: Release iocb to the iocb pool.
  153. * @phba: Pointer to HBA context object.
  154. * @iocbq: Pointer to driver iocb object.
  155. *
  156. * This function is called with no lock held to release the iocb to
  157. * iocb pool.
  158. **/
  159. void
  160. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  161. {
  162. unsigned long iflags;
  163. /*
  164. * Clean all volatile data fields, preserve iotag and node struct.
  165. */
  166. spin_lock_irqsave(&phba->hbalock, iflags);
  167. __lpfc_sli_release_iocbq(phba, iocbq);
  168. spin_unlock_irqrestore(&phba->hbalock, iflags);
  169. }
  170. /**
  171. * lpfc_sli_iocb_cmd_type: Get the iocb type.
  172. * @iocb_cmnd : iocb command code.
  173. *
  174. * This function is called by ring event handler function to get the iocb type.
  175. * This function translates the iocb command to an iocb command type used to
  176. * decide the final disposition of each completed IOCB.
  177. * The function returns
  178. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  179. * LPFC_SOL_IOCB if it is a solicited iocb completion
  180. * LPFC_ABORT_IOCB if it is an abort iocb
  181. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  182. *
  183. * The caller is not required to hold any lock.
  184. **/
  185. static lpfc_iocb_type
  186. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  187. {
  188. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  189. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  190. return 0;
  191. switch (iocb_cmnd) {
  192. case CMD_XMIT_SEQUENCE_CR:
  193. case CMD_XMIT_SEQUENCE_CX:
  194. case CMD_XMIT_BCAST_CN:
  195. case CMD_XMIT_BCAST_CX:
  196. case CMD_ELS_REQUEST_CR:
  197. case CMD_ELS_REQUEST_CX:
  198. case CMD_CREATE_XRI_CR:
  199. case CMD_CREATE_XRI_CX:
  200. case CMD_GET_RPI_CN:
  201. case CMD_XMIT_ELS_RSP_CX:
  202. case CMD_GET_RPI_CR:
  203. case CMD_FCP_IWRITE_CR:
  204. case CMD_FCP_IWRITE_CX:
  205. case CMD_FCP_IREAD_CR:
  206. case CMD_FCP_IREAD_CX:
  207. case CMD_FCP_ICMND_CR:
  208. case CMD_FCP_ICMND_CX:
  209. case CMD_FCP_TSEND_CX:
  210. case CMD_FCP_TRSP_CX:
  211. case CMD_FCP_TRECEIVE_CX:
  212. case CMD_FCP_AUTO_TRSP_CX:
  213. case CMD_ADAPTER_MSG:
  214. case CMD_ADAPTER_DUMP:
  215. case CMD_XMIT_SEQUENCE64_CR:
  216. case CMD_XMIT_SEQUENCE64_CX:
  217. case CMD_XMIT_BCAST64_CN:
  218. case CMD_XMIT_BCAST64_CX:
  219. case CMD_ELS_REQUEST64_CR:
  220. case CMD_ELS_REQUEST64_CX:
  221. case CMD_FCP_IWRITE64_CR:
  222. case CMD_FCP_IWRITE64_CX:
  223. case CMD_FCP_IREAD64_CR:
  224. case CMD_FCP_IREAD64_CX:
  225. case CMD_FCP_ICMND64_CR:
  226. case CMD_FCP_ICMND64_CX:
  227. case CMD_FCP_TSEND64_CX:
  228. case CMD_FCP_TRSP64_CX:
  229. case CMD_FCP_TRECEIVE64_CX:
  230. case CMD_GEN_REQUEST64_CR:
  231. case CMD_GEN_REQUEST64_CX:
  232. case CMD_XMIT_ELS_RSP64_CX:
  233. type = LPFC_SOL_IOCB;
  234. break;
  235. case CMD_ABORT_XRI_CN:
  236. case CMD_ABORT_XRI_CX:
  237. case CMD_CLOSE_XRI_CN:
  238. case CMD_CLOSE_XRI_CX:
  239. case CMD_XRI_ABORTED_CX:
  240. case CMD_ABORT_MXRI64_CN:
  241. type = LPFC_ABORT_IOCB;
  242. break;
  243. case CMD_RCV_SEQUENCE_CX:
  244. case CMD_RCV_ELS_REQ_CX:
  245. case CMD_RCV_SEQUENCE64_CX:
  246. case CMD_RCV_ELS_REQ64_CX:
  247. case CMD_ASYNC_STATUS:
  248. case CMD_IOCB_RCV_SEQ64_CX:
  249. case CMD_IOCB_RCV_ELS64_CX:
  250. case CMD_IOCB_RCV_CONT64_CX:
  251. case CMD_IOCB_RET_XRI64_CX:
  252. type = LPFC_UNSOL_IOCB;
  253. break;
  254. case CMD_IOCB_XMIT_MSEQ64_CR:
  255. case CMD_IOCB_XMIT_MSEQ64_CX:
  256. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  257. case CMD_IOCB_RCV_ELS_LIST64_CX:
  258. case CMD_IOCB_CLOSE_EXTENDED_CN:
  259. case CMD_IOCB_ABORT_EXTENDED_CN:
  260. case CMD_IOCB_RET_HBQE64_CN:
  261. case CMD_IOCB_FCP_IBIDIR64_CR:
  262. case CMD_IOCB_FCP_IBIDIR64_CX:
  263. case CMD_IOCB_FCP_ITASKMGT64_CX:
  264. case CMD_IOCB_LOGENTRY_CN:
  265. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  266. printk("%s - Unhandled SLI-3 Command x%x\n",
  267. __func__, iocb_cmnd);
  268. type = LPFC_UNKNOWN_IOCB;
  269. break;
  270. default:
  271. type = LPFC_UNKNOWN_IOCB;
  272. break;
  273. }
  274. return type;
  275. }
  276. /**
  277. * lpfc_sli_ring_map: Issue config_ring mbox for all rings.
  278. * @phba: Pointer to HBA context object.
  279. *
  280. * This function is called from SLI initialization code
  281. * to configure every ring of the HBA's SLI interface. The
  282. * caller is not required to hold any lock. This function issues
  283. * a config_ring mailbox command for each ring.
  284. * This function returns zero if successful else returns a negative
  285. * error code.
  286. **/
  287. static int
  288. lpfc_sli_ring_map(struct lpfc_hba *phba)
  289. {
  290. struct lpfc_sli *psli = &phba->sli;
  291. LPFC_MBOXQ_t *pmb;
  292. MAILBOX_t *pmbox;
  293. int i, rc, ret = 0;
  294. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  295. if (!pmb)
  296. return -ENOMEM;
  297. pmbox = &pmb->mb;
  298. phba->link_state = LPFC_INIT_MBX_CMDS;
  299. for (i = 0; i < psli->num_rings; i++) {
  300. lpfc_config_ring(phba, i, pmb);
  301. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  302. if (rc != MBX_SUCCESS) {
  303. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  304. "0446 Adapter failed to init (%d), "
  305. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  306. "ring %d\n",
  307. rc, pmbox->mbxCommand,
  308. pmbox->mbxStatus, i);
  309. phba->link_state = LPFC_HBA_ERROR;
  310. ret = -ENXIO;
  311. break;
  312. }
  313. }
  314. mempool_free(pmb, phba->mbox_mem_pool);
  315. return ret;
  316. }
  317. /**
  318. * lpfc_sli_ringtxcmpl_put: Adds new iocb to the txcmplq.
  319. * @phba: Pointer to HBA context object.
  320. * @pring: Pointer to driver SLI ring object.
  321. * @piocb: Pointer to the driver iocb object.
  322. *
  323. * This function is called with hbalock held. The function adds the
  324. * new iocb to txcmplq of the given ring. This function always returns
  325. * 0. If this function is called for ELS ring, this function checks if
  326. * there is a vport associated with the ELS command. This function also
  327. * starts els_tmofunc timer if this is an ELS command.
  328. **/
  329. static int
  330. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  331. struct lpfc_iocbq *piocb)
  332. {
  333. list_add_tail(&piocb->list, &pring->txcmplq);
  334. pring->txcmplq_cnt++;
  335. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  336. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  337. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  338. if (!piocb->vport)
  339. BUG();
  340. else
  341. mod_timer(&piocb->vport->els_tmofunc,
  342. jiffies + HZ * (phba->fc_ratov << 1));
  343. }
  344. return 0;
  345. }
  346. /**
  347. * lpfc_sli_ringtx_get: Get first element of the txq.
  348. * @phba: Pointer to HBA context object.
  349. * @pring: Pointer to driver SLI ring object.
  350. *
  351. * This function is called with hbalock held to get next
  352. * iocb in txq of the given ring. If there is any iocb in
  353. * the txq, the function returns first iocb in the list after
  354. * removing the iocb from the list, else it returns NULL.
  355. **/
  356. static struct lpfc_iocbq *
  357. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  358. {
  359. struct lpfc_iocbq *cmd_iocb;
  360. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  361. if (cmd_iocb != NULL)
  362. pring->txq_cnt--;
  363. return cmd_iocb;
  364. }
  365. /**
  366. * lpfc_sli_next_iocb_slot: Get next iocb slot in the ring.
  367. * @phba: Pointer to HBA context object.
  368. * @pring: Pointer to driver SLI ring object.
  369. *
  370. * This function is called with hbalock held and the caller must post the
  371. * iocb without releasing the lock. If the caller releases the lock,
  372. * iocb slot returned by the function is not guaranteed to be available.
  373. * The function returns pointer to the next available iocb slot if there
  374. * is available slot in the ring, else it returns NULL.
  375. * If the get index of the ring is ahead of the put index, the function
  376. * will post an error attention event to the worker thread to take the
  377. * HBA to offline state.
  378. **/
  379. static IOCB_t *
  380. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  381. {
  382. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  383. uint32_t max_cmd_idx = pring->numCiocb;
  384. if ((pring->next_cmdidx == pring->cmdidx) &&
  385. (++pring->next_cmdidx >= max_cmd_idx))
  386. pring->next_cmdidx = 0;
  387. if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
  388. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  389. if (unlikely(pring->local_getidx >= max_cmd_idx)) {
  390. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  391. "0315 Ring %d issue: portCmdGet %d "
  392. "is bigger then cmd ring %d\n",
  393. pring->ringno,
  394. pring->local_getidx, max_cmd_idx);
  395. phba->link_state = LPFC_HBA_ERROR;
  396. /*
  397. * All error attention handlers are posted to
  398. * worker thread
  399. */
  400. phba->work_ha |= HA_ERATT;
  401. phba->work_hs = HS_FFER3;
  402. lpfc_worker_wake_up(phba);
  403. return NULL;
  404. }
  405. if (pring->local_getidx == pring->next_cmdidx)
  406. return NULL;
  407. }
  408. return lpfc_cmd_iocb(phba, pring);
  409. }
  410. /**
  411. * lpfc_sli_next_iotag: Get an iotag for the iocb.
  412. * @phba: Pointer to HBA context object.
  413. * @iocbq: Pointer to driver iocb object.
  414. *
  415. * This function gets an iotag for the iocb. If there is no unused iotag and
  416. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  417. * array and assigns a new iotag.
  418. * The function returns the allocated iotag if successful, else returns zero.
  419. * Zero is not a valid iotag.
  420. * The caller is not required to hold any lock.
  421. **/
  422. uint16_t
  423. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  424. {
  425. struct lpfc_iocbq **new_arr;
  426. struct lpfc_iocbq **old_arr;
  427. size_t new_len;
  428. struct lpfc_sli *psli = &phba->sli;
  429. uint16_t iotag;
  430. spin_lock_irq(&phba->hbalock);
  431. iotag = psli->last_iotag;
  432. if(++iotag < psli->iocbq_lookup_len) {
  433. psli->last_iotag = iotag;
  434. psli->iocbq_lookup[iotag] = iocbq;
  435. spin_unlock_irq(&phba->hbalock);
  436. iocbq->iotag = iotag;
  437. return iotag;
  438. } else if (psli->iocbq_lookup_len < (0xffff
  439. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  440. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  441. spin_unlock_irq(&phba->hbalock);
  442. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  443. GFP_KERNEL);
  444. if (new_arr) {
  445. spin_lock_irq(&phba->hbalock);
  446. old_arr = psli->iocbq_lookup;
  447. if (new_len <= psli->iocbq_lookup_len) {
  448. /* highly unprobable case */
  449. kfree(new_arr);
  450. iotag = psli->last_iotag;
  451. if(++iotag < psli->iocbq_lookup_len) {
  452. psli->last_iotag = iotag;
  453. psli->iocbq_lookup[iotag] = iocbq;
  454. spin_unlock_irq(&phba->hbalock);
  455. iocbq->iotag = iotag;
  456. return iotag;
  457. }
  458. spin_unlock_irq(&phba->hbalock);
  459. return 0;
  460. }
  461. if (psli->iocbq_lookup)
  462. memcpy(new_arr, old_arr,
  463. ((psli->last_iotag + 1) *
  464. sizeof (struct lpfc_iocbq *)));
  465. psli->iocbq_lookup = new_arr;
  466. psli->iocbq_lookup_len = new_len;
  467. psli->last_iotag = iotag;
  468. psli->iocbq_lookup[iotag] = iocbq;
  469. spin_unlock_irq(&phba->hbalock);
  470. iocbq->iotag = iotag;
  471. kfree(old_arr);
  472. return iotag;
  473. }
  474. } else
  475. spin_unlock_irq(&phba->hbalock);
  476. lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
  477. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  478. psli->last_iotag);
  479. return 0;
  480. }
  481. /**
  482. * lpfc_sli_submit_iocb: Submit an iocb to the firmware.
  483. * @phba: Pointer to HBA context object.
  484. * @pring: Pointer to driver SLI ring object.
  485. * @iocb: Pointer to iocb slot in the ring.
  486. * @nextiocb: Pointer to driver iocb object which need to be
  487. * posted to firmware.
  488. *
  489. * This function is called with hbalock held to post a new iocb to
  490. * the firmware. This function copies the new iocb to ring iocb slot and
  491. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  492. * a completion call back for this iocb else the function will free the
  493. * iocb object.
  494. **/
  495. static void
  496. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  497. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  498. {
  499. /*
  500. * Set up an iotag
  501. */
  502. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  503. if (pring->ringno == LPFC_ELS_RING) {
  504. lpfc_debugfs_slow_ring_trc(phba,
  505. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  506. *(((uint32_t *) &nextiocb->iocb) + 4),
  507. *(((uint32_t *) &nextiocb->iocb) + 6),
  508. *(((uint32_t *) &nextiocb->iocb) + 7));
  509. }
  510. /*
  511. * Issue iocb command to adapter
  512. */
  513. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  514. wmb();
  515. pring->stats.iocb_cmd++;
  516. /*
  517. * If there is no completion routine to call, we can release the
  518. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  519. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  520. */
  521. if (nextiocb->iocb_cmpl)
  522. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  523. else
  524. __lpfc_sli_release_iocbq(phba, nextiocb);
  525. /*
  526. * Let the HBA know what IOCB slot will be the next one the
  527. * driver will put a command into.
  528. */
  529. pring->cmdidx = pring->next_cmdidx;
  530. writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  531. }
  532. /**
  533. * lpfc_sli_update_full_ring: Update the chip attention register.
  534. * @phba: Pointer to HBA context object.
  535. * @pring: Pointer to driver SLI ring object.
  536. *
  537. * The caller is not required to hold any lock for calling this function.
  538. * This function updates the chip attention bits for the ring to inform firmware
  539. * that there are pending work to be done for this ring and requests an
  540. * interrupt when there is space available in the ring. This function is
  541. * called when the driver is unable to post more iocbs to the ring due
  542. * to unavailability of space in the ring.
  543. **/
  544. static void
  545. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  546. {
  547. int ringno = pring->ringno;
  548. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  549. wmb();
  550. /*
  551. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  552. * The HBA will tell us when an IOCB entry is available.
  553. */
  554. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  555. readl(phba->CAregaddr); /* flush */
  556. pring->stats.iocb_cmd_full++;
  557. }
  558. /**
  559. * lpfc_sli_update_ring: Update chip attention register.
  560. * @phba: Pointer to HBA context object.
  561. * @pring: Pointer to driver SLI ring object.
  562. *
  563. * This function updates the chip attention register bit for the
  564. * given ring to inform HBA that there is more work to be done
  565. * in this ring. The caller is not required to hold any lock.
  566. **/
  567. static void
  568. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  569. {
  570. int ringno = pring->ringno;
  571. /*
  572. * Tell the HBA that there is work to do in this ring.
  573. */
  574. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  575. wmb();
  576. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  577. readl(phba->CAregaddr); /* flush */
  578. }
  579. }
  580. /**
  581. * lpfc_sli_resume_iocb: Process iocbs in the txq.
  582. * @phba: Pointer to HBA context object.
  583. * @pring: Pointer to driver SLI ring object.
  584. *
  585. * This function is called with hbalock held to post pending iocbs
  586. * in the txq to the firmware. This function is called when driver
  587. * detects space available in the ring.
  588. **/
  589. static void
  590. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  591. {
  592. IOCB_t *iocb;
  593. struct lpfc_iocbq *nextiocb;
  594. /*
  595. * Check to see if:
  596. * (a) there is anything on the txq to send
  597. * (b) link is up
  598. * (c) link attention events can be processed (fcp ring only)
  599. * (d) IOCB processing is not blocked by the outstanding mbox command.
  600. */
  601. if (pring->txq_cnt &&
  602. lpfc_is_link_up(phba) &&
  603. (pring->ringno != phba->sli.fcp_ring ||
  604. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  605. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  606. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  607. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  608. if (iocb)
  609. lpfc_sli_update_ring(phba, pring);
  610. else
  611. lpfc_sli_update_full_ring(phba, pring);
  612. }
  613. return;
  614. }
  615. /**
  616. * lpfc_sli_next_hbq_slot: Get next hbq entry for the HBQ.
  617. * @phba: Pointer to HBA context object.
  618. * @hbqno: HBQ number.
  619. *
  620. * This function is called with hbalock held to get the next
  621. * available slot for the given HBQ. If there is free slot
  622. * available for the HBQ it will return pointer to the next available
  623. * HBQ entry else it will return NULL.
  624. **/
  625. static struct lpfc_hbq_entry *
  626. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  627. {
  628. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  629. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  630. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  631. hbqp->next_hbqPutIdx = 0;
  632. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  633. uint32_t raw_index = phba->hbq_get[hbqno];
  634. uint32_t getidx = le32_to_cpu(raw_index);
  635. hbqp->local_hbqGetIdx = getidx;
  636. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  637. lpfc_printf_log(phba, KERN_ERR,
  638. LOG_SLI | LOG_VPORT,
  639. "1802 HBQ %d: local_hbqGetIdx "
  640. "%u is > than hbqp->entry_count %u\n",
  641. hbqno, hbqp->local_hbqGetIdx,
  642. hbqp->entry_count);
  643. phba->link_state = LPFC_HBA_ERROR;
  644. return NULL;
  645. }
  646. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  647. return NULL;
  648. }
  649. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  650. hbqp->hbqPutIdx;
  651. }
  652. /**
  653. * lpfc_sli_hbqbuf_free_all: Free all the hbq buffers.
  654. * @phba: Pointer to HBA context object.
  655. *
  656. * This function is called with no lock held to free all the
  657. * hbq buffers while uninitializing the SLI interface. It also
  658. * frees the HBQ buffers returned by the firmware but not yet
  659. * processed by the upper layers.
  660. **/
  661. void
  662. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  663. {
  664. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  665. struct hbq_dmabuf *hbq_buf;
  666. unsigned long flags;
  667. int i, hbq_count;
  668. uint32_t hbqno;
  669. hbq_count = lpfc_sli_hbq_count();
  670. /* Return all memory used by all HBQs */
  671. spin_lock_irqsave(&phba->hbalock, flags);
  672. for (i = 0; i < hbq_count; ++i) {
  673. list_for_each_entry_safe(dmabuf, next_dmabuf,
  674. &phba->hbqs[i].hbq_buffer_list, list) {
  675. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  676. list_del(&hbq_buf->dbuf.list);
  677. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  678. }
  679. phba->hbqs[i].buffer_count = 0;
  680. }
  681. /* Return all HBQ buffer that are in-fly */
  682. list_for_each_entry_safe(dmabuf, next_dmabuf,
  683. &phba->hbqbuf_in_list, list) {
  684. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  685. list_del(&hbq_buf->dbuf.list);
  686. if (hbq_buf->tag == -1) {
  687. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  688. (phba, hbq_buf);
  689. } else {
  690. hbqno = hbq_buf->tag >> 16;
  691. if (hbqno >= LPFC_MAX_HBQS)
  692. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  693. (phba, hbq_buf);
  694. else
  695. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  696. hbq_buf);
  697. }
  698. }
  699. /* Mark the HBQs not in use */
  700. phba->hbq_in_use = 0;
  701. spin_unlock_irqrestore(&phba->hbalock, flags);
  702. }
  703. /**
  704. * lpfc_sli_hbq_to_firmware: Post the hbq buffer to firmware.
  705. * @phba: Pointer to HBA context object.
  706. * @hbqno: HBQ number.
  707. * @hbq_buf: Pointer to HBQ buffer.
  708. *
  709. * This function is called with the hbalock held to post a
  710. * hbq buffer to the firmware. If the function finds an empty
  711. * slot in the HBQ, it will post the buffer. The function will return
  712. * pointer to the hbq entry if it successfully post the buffer
  713. * else it will return NULL.
  714. **/
  715. static struct lpfc_hbq_entry *
  716. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  717. struct hbq_dmabuf *hbq_buf)
  718. {
  719. struct lpfc_hbq_entry *hbqe;
  720. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  721. /* Get next HBQ entry slot to use */
  722. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  723. if (hbqe) {
  724. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  725. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  726. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  727. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  728. hbqe->bde.tus.f.bdeFlags = 0;
  729. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  730. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  731. /* Sync SLIM */
  732. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  733. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  734. /* flush */
  735. readl(phba->hbq_put + hbqno);
  736. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  737. }
  738. return hbqe;
  739. }
  740. /* HBQ for ELS and CT traffic. */
  741. static struct lpfc_hbq_init lpfc_els_hbq = {
  742. .rn = 1,
  743. .entry_count = 200,
  744. .mask_count = 0,
  745. .profile = 0,
  746. .ring_mask = (1 << LPFC_ELS_RING),
  747. .buffer_count = 0,
  748. .init_count = 20,
  749. .add_count = 5,
  750. };
  751. /* HBQ for the extra ring if needed */
  752. static struct lpfc_hbq_init lpfc_extra_hbq = {
  753. .rn = 1,
  754. .entry_count = 200,
  755. .mask_count = 0,
  756. .profile = 0,
  757. .ring_mask = (1 << LPFC_EXTRA_RING),
  758. .buffer_count = 0,
  759. .init_count = 0,
  760. .add_count = 5,
  761. };
  762. /* Array of HBQs */
  763. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  764. &lpfc_els_hbq,
  765. &lpfc_extra_hbq,
  766. };
  767. /**
  768. * lpfc_sli_hbqbuf_fill_hbqs: Post more hbq buffers to HBQ.
  769. * @phba: Pointer to HBA context object.
  770. * @hbqno: HBQ number.
  771. * @count: Number of HBQ buffers to be posted.
  772. *
  773. * This function is called with no lock held to post more hbq buffers to the
  774. * given HBQ. The function returns the number of HBQ buffers successfully
  775. * posted.
  776. **/
  777. static int
  778. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  779. {
  780. uint32_t i, posted = 0;
  781. unsigned long flags;
  782. struct hbq_dmabuf *hbq_buffer;
  783. LIST_HEAD(hbq_buf_list);
  784. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  785. return 0;
  786. if ((phba->hbqs[hbqno].buffer_count + count) >
  787. lpfc_hbq_defs[hbqno]->entry_count)
  788. count = lpfc_hbq_defs[hbqno]->entry_count -
  789. phba->hbqs[hbqno].buffer_count;
  790. if (!count)
  791. return 0;
  792. /* Allocate HBQ entries */
  793. for (i = 0; i < count; i++) {
  794. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  795. if (!hbq_buffer)
  796. break;
  797. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  798. }
  799. /* Check whether HBQ is still in use */
  800. spin_lock_irqsave(&phba->hbalock, flags);
  801. if (!phba->hbq_in_use)
  802. goto err;
  803. while (!list_empty(&hbq_buf_list)) {
  804. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  805. dbuf.list);
  806. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  807. (hbqno << 16));
  808. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  809. phba->hbqs[hbqno].buffer_count++;
  810. posted++;
  811. } else
  812. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  813. }
  814. spin_unlock_irqrestore(&phba->hbalock, flags);
  815. return posted;
  816. err:
  817. spin_unlock_irqrestore(&phba->hbalock, flags);
  818. while (!list_empty(&hbq_buf_list)) {
  819. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  820. dbuf.list);
  821. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  822. }
  823. return 0;
  824. }
  825. /**
  826. * lpfc_sli_hbqbuf_add_hbqs: Post more HBQ buffers to firmware.
  827. * @phba: Pointer to HBA context object.
  828. * @qno: HBQ number.
  829. *
  830. * This function posts more buffers to the HBQ. This function
  831. * is called with no lock held. The function returns the number of HBQ entries
  832. * successfully allocated.
  833. **/
  834. int
  835. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  836. {
  837. return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  838. lpfc_hbq_defs[qno]->add_count));
  839. }
  840. /**
  841. * lpfc_sli_hbqbuf_init_hbqs: Post initial buffers to the HBQ.
  842. * @phba: Pointer to HBA context object.
  843. * @qno: HBQ queue number.
  844. *
  845. * This function is called from SLI initialization code path with
  846. * no lock held to post initial HBQ buffers to firmware. The
  847. * function returns the number of HBQ entries successfully allocated.
  848. **/
  849. static int
  850. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  851. {
  852. return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  853. lpfc_hbq_defs[qno]->init_count));
  854. }
  855. /**
  856. * lpfc_sli_hbqbuf_find: Find the hbq buffer associated with a tag.
  857. * @phba: Pointer to HBA context object.
  858. * @tag: Tag of the hbq buffer.
  859. *
  860. * This function is called with hbalock held. This function searches
  861. * for the hbq buffer associated with the given tag in the hbq buffer
  862. * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
  863. * it returns NULL.
  864. **/
  865. static struct hbq_dmabuf *
  866. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  867. {
  868. struct lpfc_dmabuf *d_buf;
  869. struct hbq_dmabuf *hbq_buf;
  870. uint32_t hbqno;
  871. hbqno = tag >> 16;
  872. if (hbqno >= LPFC_MAX_HBQS)
  873. return NULL;
  874. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  875. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  876. if (hbq_buf->tag == tag) {
  877. return hbq_buf;
  878. }
  879. }
  880. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  881. "1803 Bad hbq tag. Data: x%x x%x\n",
  882. tag, phba->hbqs[tag >> 16].buffer_count);
  883. return NULL;
  884. }
  885. /**
  886. * lpfc_sli_free_hbq: Give back the hbq buffer to firmware.
  887. * @phba: Pointer to HBA context object.
  888. * @hbq_buffer: Pointer to HBQ buffer.
  889. *
  890. * This function is called with hbalock. This function gives back
  891. * the hbq buffer to firmware. If the HBQ does not have space to
  892. * post the buffer, it will free the buffer.
  893. **/
  894. void
  895. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  896. {
  897. uint32_t hbqno;
  898. if (hbq_buffer) {
  899. hbqno = hbq_buffer->tag >> 16;
  900. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  901. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  902. }
  903. }
  904. }
  905. /**
  906. * lpfc_sli_chk_mbx_command: Check if the mailbox is a legitimate mailbox.
  907. * @mbxCommand: mailbox command code.
  908. *
  909. * This function is called by the mailbox event handler function to verify
  910. * that the completed mailbox command is a legitimate mailbox command. If the
  911. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  912. * and the mailbox event handler will take the HBA offline.
  913. **/
  914. static int
  915. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  916. {
  917. uint8_t ret;
  918. switch (mbxCommand) {
  919. case MBX_LOAD_SM:
  920. case MBX_READ_NV:
  921. case MBX_WRITE_NV:
  922. case MBX_WRITE_VPARMS:
  923. case MBX_RUN_BIU_DIAG:
  924. case MBX_INIT_LINK:
  925. case MBX_DOWN_LINK:
  926. case MBX_CONFIG_LINK:
  927. case MBX_CONFIG_RING:
  928. case MBX_RESET_RING:
  929. case MBX_READ_CONFIG:
  930. case MBX_READ_RCONFIG:
  931. case MBX_READ_SPARM:
  932. case MBX_READ_STATUS:
  933. case MBX_READ_RPI:
  934. case MBX_READ_XRI:
  935. case MBX_READ_REV:
  936. case MBX_READ_LNK_STAT:
  937. case MBX_REG_LOGIN:
  938. case MBX_UNREG_LOGIN:
  939. case MBX_READ_LA:
  940. case MBX_CLEAR_LA:
  941. case MBX_DUMP_MEMORY:
  942. case MBX_DUMP_CONTEXT:
  943. case MBX_RUN_DIAGS:
  944. case MBX_RESTART:
  945. case MBX_UPDATE_CFG:
  946. case MBX_DOWN_LOAD:
  947. case MBX_DEL_LD_ENTRY:
  948. case MBX_RUN_PROGRAM:
  949. case MBX_SET_MASK:
  950. case MBX_SET_VARIABLE:
  951. case MBX_UNREG_D_ID:
  952. case MBX_KILL_BOARD:
  953. case MBX_CONFIG_FARP:
  954. case MBX_BEACON:
  955. case MBX_LOAD_AREA:
  956. case MBX_RUN_BIU_DIAG64:
  957. case MBX_CONFIG_PORT:
  958. case MBX_READ_SPARM64:
  959. case MBX_READ_RPI64:
  960. case MBX_REG_LOGIN64:
  961. case MBX_READ_LA64:
  962. case MBX_WRITE_WWN:
  963. case MBX_SET_DEBUG:
  964. case MBX_LOAD_EXP_ROM:
  965. case MBX_ASYNCEVT_ENABLE:
  966. case MBX_REG_VPI:
  967. case MBX_UNREG_VPI:
  968. case MBX_HEARTBEAT:
  969. case MBX_PORT_CAPABILITIES:
  970. case MBX_PORT_IOV_CONTROL:
  971. ret = mbxCommand;
  972. break;
  973. default:
  974. ret = MBX_SHUTDOWN;
  975. break;
  976. }
  977. return ret;
  978. }
  979. /**
  980. * lpfc_sli_wake_mbox_wait: Completion handler for mbox issued from
  981. * lpfc_sli_issue_mbox_wait.
  982. * @phba: Pointer to HBA context object.
  983. * @pmboxq: Pointer to mailbox command.
  984. *
  985. * This is completion handler function for mailbox commands issued from
  986. * lpfc_sli_issue_mbox_wait function. This function is called by the
  987. * mailbox event handler function with no lock held. This function
  988. * will wake up thread waiting on the wait queue pointed by context1
  989. * of the mailbox.
  990. **/
  991. static void
  992. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  993. {
  994. wait_queue_head_t *pdone_q;
  995. unsigned long drvr_flag;
  996. /*
  997. * If pdone_q is empty, the driver thread gave up waiting and
  998. * continued running.
  999. */
  1000. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  1001. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1002. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  1003. if (pdone_q)
  1004. wake_up_interruptible(pdone_q);
  1005. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1006. return;
  1007. }
  1008. /**
  1009. * lpfc_sli_def_mbox_cmpl: Default mailbox completion handler.
  1010. * @phba: Pointer to HBA context object.
  1011. * @pmb: Pointer to mailbox object.
  1012. *
  1013. * This function is the default mailbox completion handler. It
  1014. * frees the memory resources associated with the completed mailbox
  1015. * command. If the completed command is a REG_LOGIN mailbox command,
  1016. * this function will issue a UREG_LOGIN to re-claim the RPI.
  1017. **/
  1018. void
  1019. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1020. {
  1021. struct lpfc_dmabuf *mp;
  1022. uint16_t rpi;
  1023. int rc;
  1024. mp = (struct lpfc_dmabuf *) (pmb->context1);
  1025. if (mp) {
  1026. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1027. kfree(mp);
  1028. }
  1029. /*
  1030. * If a REG_LOGIN succeeded after node is destroyed or node
  1031. * is in re-discovery driver need to cleanup the RPI.
  1032. */
  1033. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  1034. pmb->mb.mbxCommand == MBX_REG_LOGIN64 &&
  1035. !pmb->mb.mbxStatus) {
  1036. rpi = pmb->mb.un.varWords[0];
  1037. lpfc_unreg_login(phba, pmb->mb.un.varRegLogin.vpi, rpi, pmb);
  1038. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  1039. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1040. if (rc != MBX_NOT_FINISHED)
  1041. return;
  1042. }
  1043. mempool_free(pmb, phba->mbox_mem_pool);
  1044. return;
  1045. }
  1046. /**
  1047. * lpfc_sli_handle_mb_event: Handle mailbox completions from firmware.
  1048. * @phba: Pointer to HBA context object.
  1049. *
  1050. * This function is called with no lock held. This function processes all
  1051. * the completed mailbox commands and gives it to upper layers. The interrupt
  1052. * service routine processes mailbox completion interrupt and adds completed
  1053. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  1054. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  1055. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  1056. * function returns the mailbox commands to the upper layer by calling the
  1057. * completion handler function of each mailbox.
  1058. **/
  1059. int
  1060. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  1061. {
  1062. MAILBOX_t *pmbox;
  1063. LPFC_MBOXQ_t *pmb;
  1064. int rc;
  1065. LIST_HEAD(cmplq);
  1066. phba->sli.slistat.mbox_event++;
  1067. /* Get all completed mailboxe buffers into the cmplq */
  1068. spin_lock_irq(&phba->hbalock);
  1069. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  1070. spin_unlock_irq(&phba->hbalock);
  1071. /* Get a Mailbox buffer to setup mailbox commands for callback */
  1072. do {
  1073. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  1074. if (pmb == NULL)
  1075. break;
  1076. pmbox = &pmb->mb;
  1077. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  1078. if (pmb->vport) {
  1079. lpfc_debugfs_disc_trc(pmb->vport,
  1080. LPFC_DISC_TRC_MBOX_VPORT,
  1081. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  1082. (uint32_t)pmbox->mbxCommand,
  1083. pmbox->un.varWords[0],
  1084. pmbox->un.varWords[1]);
  1085. }
  1086. else {
  1087. lpfc_debugfs_disc_trc(phba->pport,
  1088. LPFC_DISC_TRC_MBOX,
  1089. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  1090. (uint32_t)pmbox->mbxCommand,
  1091. pmbox->un.varWords[0],
  1092. pmbox->un.varWords[1]);
  1093. }
  1094. }
  1095. /*
  1096. * It is a fatal error if unknown mbox command completion.
  1097. */
  1098. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  1099. MBX_SHUTDOWN) {
  1100. /* Unknow mailbox command compl */
  1101. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  1102. "(%d):0323 Unknown Mailbox command "
  1103. "%x Cmpl\n",
  1104. pmb->vport ? pmb->vport->vpi : 0,
  1105. pmbox->mbxCommand);
  1106. phba->link_state = LPFC_HBA_ERROR;
  1107. phba->work_hs = HS_FFER3;
  1108. lpfc_handle_eratt(phba);
  1109. continue;
  1110. }
  1111. if (pmbox->mbxStatus) {
  1112. phba->sli.slistat.mbox_stat_err++;
  1113. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  1114. /* Mbox cmd cmpl error - RETRYing */
  1115. lpfc_printf_log(phba, KERN_INFO,
  1116. LOG_MBOX | LOG_SLI,
  1117. "(%d):0305 Mbox cmd cmpl "
  1118. "error - RETRYing Data: x%x "
  1119. "x%x x%x x%x\n",
  1120. pmb->vport ? pmb->vport->vpi :0,
  1121. pmbox->mbxCommand,
  1122. pmbox->mbxStatus,
  1123. pmbox->un.varWords[0],
  1124. pmb->vport->port_state);
  1125. pmbox->mbxStatus = 0;
  1126. pmbox->mbxOwner = OWN_HOST;
  1127. spin_lock_irq(&phba->hbalock);
  1128. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  1129. spin_unlock_irq(&phba->hbalock);
  1130. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1131. if (rc == MBX_SUCCESS)
  1132. continue;
  1133. }
  1134. }
  1135. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  1136. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  1137. "(%d):0307 Mailbox cmd x%x Cmpl x%p "
  1138. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
  1139. pmb->vport ? pmb->vport->vpi : 0,
  1140. pmbox->mbxCommand,
  1141. pmb->mbox_cmpl,
  1142. *((uint32_t *) pmbox),
  1143. pmbox->un.varWords[0],
  1144. pmbox->un.varWords[1],
  1145. pmbox->un.varWords[2],
  1146. pmbox->un.varWords[3],
  1147. pmbox->un.varWords[4],
  1148. pmbox->un.varWords[5],
  1149. pmbox->un.varWords[6],
  1150. pmbox->un.varWords[7]);
  1151. if (pmb->mbox_cmpl)
  1152. pmb->mbox_cmpl(phba,pmb);
  1153. } while (1);
  1154. return 0;
  1155. }
  1156. /**
  1157. * lpfc_sli_replace_hbqbuff: Replace the HBQ buffer with a new buffer.
  1158. * @phba: Pointer to HBA context object.
  1159. * @tag: Tag for the HBQ buffer.
  1160. *
  1161. * This function is called from unsolicited event handler code path to get the
  1162. * HBQ buffer associated with an unsolicited iocb. This function is called with
  1163. * no lock held. It returns the buffer associated with the given tag and posts
  1164. * another buffer to the firmware. Note that the new buffer must be allocated
  1165. * before taking the hbalock and that the hba lock must be held until it is
  1166. * finished with the hbq entry swap.
  1167. **/
  1168. static struct lpfc_dmabuf *
  1169. lpfc_sli_replace_hbqbuff(struct lpfc_hba *phba, uint32_t tag)
  1170. {
  1171. struct hbq_dmabuf *hbq_entry, *new_hbq_entry;
  1172. uint32_t hbqno;
  1173. void *virt; /* virtual address ptr */
  1174. dma_addr_t phys; /* mapped address */
  1175. unsigned long flags;
  1176. hbqno = tag >> 16;
  1177. new_hbq_entry = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  1178. /* Check whether HBQ is still in use */
  1179. spin_lock_irqsave(&phba->hbalock, flags);
  1180. if (!phba->hbq_in_use) {
  1181. if (new_hbq_entry)
  1182. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1183. new_hbq_entry);
  1184. spin_unlock_irqrestore(&phba->hbalock, flags);
  1185. return NULL;
  1186. }
  1187. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  1188. if (hbq_entry == NULL) {
  1189. if (new_hbq_entry)
  1190. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1191. new_hbq_entry);
  1192. spin_unlock_irqrestore(&phba->hbalock, flags);
  1193. return NULL;
  1194. }
  1195. list_del(&hbq_entry->dbuf.list);
  1196. if (new_hbq_entry == NULL) {
  1197. list_add_tail(&hbq_entry->dbuf.list, &phba->hbqbuf_in_list);
  1198. spin_unlock_irqrestore(&phba->hbalock, flags);
  1199. return &hbq_entry->dbuf;
  1200. }
  1201. new_hbq_entry->tag = -1;
  1202. phys = new_hbq_entry->dbuf.phys;
  1203. virt = new_hbq_entry->dbuf.virt;
  1204. new_hbq_entry->dbuf.phys = hbq_entry->dbuf.phys;
  1205. new_hbq_entry->dbuf.virt = hbq_entry->dbuf.virt;
  1206. hbq_entry->dbuf.phys = phys;
  1207. hbq_entry->dbuf.virt = virt;
  1208. lpfc_sli_free_hbq(phba, hbq_entry);
  1209. list_add_tail(&new_hbq_entry->dbuf.list, &phba->hbqbuf_in_list);
  1210. spin_unlock_irqrestore(&phba->hbalock, flags);
  1211. return &new_hbq_entry->dbuf;
  1212. }
  1213. /**
  1214. * lpfc_sli_get_buff: Get the buffer associated with the buffer tag.
  1215. * @phba: Pointer to HBA context object.
  1216. * @pring: Pointer to driver SLI ring object.
  1217. * @tag: buffer tag.
  1218. *
  1219. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  1220. * is set in the tag the buffer is posted for a particular exchange,
  1221. * the function will return the buffer without replacing the buffer.
  1222. * If the buffer is for unsolicited ELS or CT traffic, this function
  1223. * returns the buffer and also posts another buffer to the firmware.
  1224. **/
  1225. static struct lpfc_dmabuf *
  1226. lpfc_sli_get_buff(struct lpfc_hba *phba,
  1227. struct lpfc_sli_ring *pring,
  1228. uint32_t tag)
  1229. {
  1230. if (tag & QUE_BUFTAG_BIT)
  1231. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  1232. else
  1233. return lpfc_sli_replace_hbqbuff(phba, tag);
  1234. }
  1235. /**
  1236. * lpfc_sli_process_unsol_iocb: Unsolicited iocb handler.
  1237. * @phba: Pointer to HBA context object.
  1238. * @pring: Pointer to driver SLI ring object.
  1239. * @saveq: Pointer to the unsolicited iocb.
  1240. *
  1241. * This function is called with no lock held by the ring event handler
  1242. * when there is an unsolicited iocb posted to the response ring by the
  1243. * firmware. This function gets the buffer associated with the iocbs
  1244. * and calls the event handler for the ring. This function handles both
  1245. * qring buffers and hbq buffers.
  1246. * When the function returns 1 the caller can free the iocb object otherwise
  1247. * upper layer functions will free the iocb objects.
  1248. **/
  1249. static int
  1250. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1251. struct lpfc_iocbq *saveq)
  1252. {
  1253. IOCB_t * irsp;
  1254. WORD5 * w5p;
  1255. uint32_t Rctl, Type;
  1256. uint32_t match, i;
  1257. struct lpfc_iocbq *iocbq;
  1258. struct lpfc_dmabuf *dmzbuf;
  1259. match = 0;
  1260. irsp = &(saveq->iocb);
  1261. if (irsp->ulpStatus == IOSTAT_NEED_BUFFER)
  1262. return 1;
  1263. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  1264. if (pring->lpfc_sli_rcv_async_status)
  1265. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  1266. else
  1267. lpfc_printf_log(phba,
  1268. KERN_WARNING,
  1269. LOG_SLI,
  1270. "0316 Ring %d handler: unexpected "
  1271. "ASYNC_STATUS iocb received evt_code "
  1272. "0x%x\n",
  1273. pring->ringno,
  1274. irsp->un.asyncstat.evt_code);
  1275. return 1;
  1276. }
  1277. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  1278. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  1279. if (irsp->ulpBdeCount > 0) {
  1280. dmzbuf = lpfc_sli_get_buff(phba, pring,
  1281. irsp->un.ulpWord[3]);
  1282. lpfc_in_buf_free(phba, dmzbuf);
  1283. }
  1284. if (irsp->ulpBdeCount > 1) {
  1285. dmzbuf = lpfc_sli_get_buff(phba, pring,
  1286. irsp->unsli3.sli3Words[3]);
  1287. lpfc_in_buf_free(phba, dmzbuf);
  1288. }
  1289. if (irsp->ulpBdeCount > 2) {
  1290. dmzbuf = lpfc_sli_get_buff(phba, pring,
  1291. irsp->unsli3.sli3Words[7]);
  1292. lpfc_in_buf_free(phba, dmzbuf);
  1293. }
  1294. return 1;
  1295. }
  1296. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  1297. if (irsp->ulpBdeCount != 0) {
  1298. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  1299. irsp->un.ulpWord[3]);
  1300. if (!saveq->context2)
  1301. lpfc_printf_log(phba,
  1302. KERN_ERR,
  1303. LOG_SLI,
  1304. "0341 Ring %d Cannot find buffer for "
  1305. "an unsolicited iocb. tag 0x%x\n",
  1306. pring->ringno,
  1307. irsp->un.ulpWord[3]);
  1308. }
  1309. if (irsp->ulpBdeCount == 2) {
  1310. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  1311. irsp->unsli3.sli3Words[7]);
  1312. if (!saveq->context3)
  1313. lpfc_printf_log(phba,
  1314. KERN_ERR,
  1315. LOG_SLI,
  1316. "0342 Ring %d Cannot find buffer for an"
  1317. " unsolicited iocb. tag 0x%x\n",
  1318. pring->ringno,
  1319. irsp->unsli3.sli3Words[7]);
  1320. }
  1321. list_for_each_entry(iocbq, &saveq->list, list) {
  1322. irsp = &(iocbq->iocb);
  1323. if (irsp->ulpBdeCount != 0) {
  1324. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  1325. irsp->un.ulpWord[3]);
  1326. if (!iocbq->context2)
  1327. lpfc_printf_log(phba,
  1328. KERN_ERR,
  1329. LOG_SLI,
  1330. "0343 Ring %d Cannot find "
  1331. "buffer for an unsolicited iocb"
  1332. ". tag 0x%x\n", pring->ringno,
  1333. irsp->un.ulpWord[3]);
  1334. }
  1335. if (irsp->ulpBdeCount == 2) {
  1336. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  1337. irsp->unsli3.sli3Words[7]);
  1338. if (!iocbq->context3)
  1339. lpfc_printf_log(phba,
  1340. KERN_ERR,
  1341. LOG_SLI,
  1342. "0344 Ring %d Cannot find "
  1343. "buffer for an unsolicited "
  1344. "iocb. tag 0x%x\n",
  1345. pring->ringno,
  1346. irsp->unsli3.sli3Words[7]);
  1347. }
  1348. }
  1349. }
  1350. if (irsp->ulpBdeCount != 0 &&
  1351. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  1352. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  1353. int found = 0;
  1354. /* search continue save q for same XRI */
  1355. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  1356. if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
  1357. list_add_tail(&saveq->list, &iocbq->list);
  1358. found = 1;
  1359. break;
  1360. }
  1361. }
  1362. if (!found)
  1363. list_add_tail(&saveq->clist,
  1364. &pring->iocb_continue_saveq);
  1365. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  1366. list_del_init(&iocbq->clist);
  1367. saveq = iocbq;
  1368. irsp = &(saveq->iocb);
  1369. } else
  1370. return 0;
  1371. }
  1372. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  1373. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  1374. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  1375. Rctl = FC_ELS_REQ;
  1376. Type = FC_ELS_DATA;
  1377. } else {
  1378. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  1379. Rctl = w5p->hcsw.Rctl;
  1380. Type = w5p->hcsw.Type;
  1381. /* Firmware Workaround */
  1382. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  1383. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  1384. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  1385. Rctl = FC_ELS_REQ;
  1386. Type = FC_ELS_DATA;
  1387. w5p->hcsw.Rctl = Rctl;
  1388. w5p->hcsw.Type = Type;
  1389. }
  1390. }
  1391. /* unSolicited Responses */
  1392. if (pring->prt[0].profile) {
  1393. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  1394. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  1395. saveq);
  1396. match = 1;
  1397. } else {
  1398. /* We must search, based on rctl / type
  1399. for the right routine */
  1400. for (i = 0; i < pring->num_mask; i++) {
  1401. if ((pring->prt[i].rctl == Rctl)
  1402. && (pring->prt[i].type == Type)) {
  1403. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  1404. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  1405. (phba, pring, saveq);
  1406. match = 1;
  1407. break;
  1408. }
  1409. }
  1410. }
  1411. if (match == 0) {
  1412. /* Unexpected Rctl / Type received */
  1413. /* Ring <ringno> handler: unexpected
  1414. Rctl <Rctl> Type <Type> received */
  1415. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1416. "0313 Ring %d handler: unexpected Rctl x%x "
  1417. "Type x%x received\n",
  1418. pring->ringno, Rctl, Type);
  1419. }
  1420. return 1;
  1421. }
  1422. /**
  1423. * lpfc_sli_iocbq_lookup: Find command iocb for the given response iocb.
  1424. * @phba: Pointer to HBA context object.
  1425. * @pring: Pointer to driver SLI ring object.
  1426. * @prspiocb: Pointer to response iocb object.
  1427. *
  1428. * This function looks up the iocb_lookup table to get the command iocb
  1429. * corresponding to the given response iocb using the iotag of the
  1430. * response iocb. This function is called with the hbalock held.
  1431. * This function returns the command iocb object if it finds the command
  1432. * iocb else returns NULL.
  1433. **/
  1434. static struct lpfc_iocbq *
  1435. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  1436. struct lpfc_sli_ring *pring,
  1437. struct lpfc_iocbq *prspiocb)
  1438. {
  1439. struct lpfc_iocbq *cmd_iocb = NULL;
  1440. uint16_t iotag;
  1441. iotag = prspiocb->iocb.ulpIoTag;
  1442. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  1443. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  1444. list_del_init(&cmd_iocb->list);
  1445. pring->txcmplq_cnt--;
  1446. return cmd_iocb;
  1447. }
  1448. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1449. "0317 iotag x%x is out off "
  1450. "range: max iotag x%x wd0 x%x\n",
  1451. iotag, phba->sli.last_iotag,
  1452. *(((uint32_t *) &prspiocb->iocb) + 7));
  1453. return NULL;
  1454. }
  1455. /**
  1456. * lpfc_sli_process_sol_iocb: process solicited iocb completion.
  1457. * @phba: Pointer to HBA context object.
  1458. * @pring: Pointer to driver SLI ring object.
  1459. * @saveq: Pointer to the response iocb to be processed.
  1460. *
  1461. * This function is called by the ring event handler for non-fcp
  1462. * rings when there is a new response iocb in the response ring.
  1463. * The caller is not required to hold any locks. This function
  1464. * gets the command iocb associated with the response iocb and
  1465. * calls the completion handler for the command iocb. If there
  1466. * is no completion handler, the function will free the resources
  1467. * associated with command iocb. If the response iocb is for
  1468. * an already aborted command iocb, the status of the completion
  1469. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  1470. * This function always returns 1.
  1471. **/
  1472. static int
  1473. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1474. struct lpfc_iocbq *saveq)
  1475. {
  1476. struct lpfc_iocbq *cmdiocbp;
  1477. int rc = 1;
  1478. unsigned long iflag;
  1479. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  1480. spin_lock_irqsave(&phba->hbalock, iflag);
  1481. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  1482. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1483. if (cmdiocbp) {
  1484. if (cmdiocbp->iocb_cmpl) {
  1485. /*
  1486. * If an ELS command failed send an event to mgmt
  1487. * application.
  1488. */
  1489. if (saveq->iocb.ulpStatus &&
  1490. (pring->ringno == LPFC_ELS_RING) &&
  1491. (cmdiocbp->iocb.ulpCommand ==
  1492. CMD_ELS_REQUEST64_CR))
  1493. lpfc_send_els_failure_event(phba,
  1494. cmdiocbp, saveq);
  1495. /*
  1496. * Post all ELS completions to the worker thread.
  1497. * All other are passed to the completion callback.
  1498. */
  1499. if (pring->ringno == LPFC_ELS_RING) {
  1500. if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
  1501. cmdiocbp->iocb_flag &=
  1502. ~LPFC_DRIVER_ABORTED;
  1503. saveq->iocb.ulpStatus =
  1504. IOSTAT_LOCAL_REJECT;
  1505. saveq->iocb.un.ulpWord[4] =
  1506. IOERR_SLI_ABORTED;
  1507. /* Firmware could still be in progress
  1508. * of DMAing payload, so don't free data
  1509. * buffer till after a hbeat.
  1510. */
  1511. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  1512. }
  1513. }
  1514. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  1515. } else
  1516. lpfc_sli_release_iocbq(phba, cmdiocbp);
  1517. } else {
  1518. /*
  1519. * Unknown initiating command based on the response iotag.
  1520. * This could be the case on the ELS ring because of
  1521. * lpfc_els_abort().
  1522. */
  1523. if (pring->ringno != LPFC_ELS_RING) {
  1524. /*
  1525. * Ring <ringno> handler: unexpected completion IoTag
  1526. * <IoTag>
  1527. */
  1528. lpfc_printf_vlog(cmdiocbp->vport, KERN_WARNING, LOG_SLI,
  1529. "0322 Ring %d handler: "
  1530. "unexpected completion IoTag x%x "
  1531. "Data: x%x x%x x%x x%x\n",
  1532. pring->ringno,
  1533. saveq->iocb.ulpIoTag,
  1534. saveq->iocb.ulpStatus,
  1535. saveq->iocb.un.ulpWord[4],
  1536. saveq->iocb.ulpCommand,
  1537. saveq->iocb.ulpContext);
  1538. }
  1539. }
  1540. return rc;
  1541. }
  1542. /**
  1543. * lpfc_sli_rsp_pointers_error: Response ring pointer error handler.
  1544. * @phba: Pointer to HBA context object.
  1545. * @pring: Pointer to driver SLI ring object.
  1546. *
  1547. * This function is called from the iocb ring event handlers when
  1548. * put pointer is ahead of the get pointer for a ring. This function signal
  1549. * an error attention condition to the worker thread and the worker
  1550. * thread will transition the HBA to offline state.
  1551. **/
  1552. static void
  1553. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1554. {
  1555. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1556. /*
  1557. * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
  1558. * rsp ring <portRspMax>
  1559. */
  1560. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1561. "0312 Ring %d handler: portRspPut %d "
  1562. "is bigger then rsp ring %d\n",
  1563. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  1564. pring->numRiocb);
  1565. phba->link_state = LPFC_HBA_ERROR;
  1566. /*
  1567. * All error attention handlers are posted to
  1568. * worker thread
  1569. */
  1570. phba->work_ha |= HA_ERATT;
  1571. phba->work_hs = HS_FFER3;
  1572. lpfc_worker_wake_up(phba);
  1573. return;
  1574. }
  1575. /**
  1576. * lpfc_poll_eratt: Error attention polling timer timeout handler.
  1577. * @ptr: Pointer to address of HBA context object.
  1578. *
  1579. * This function is invoked by the Error Attention polling timer when the
  1580. * timer times out. It will check the SLI Error Attention register for
  1581. * possible attention events. If so, it will post an Error Attention event
  1582. * and wake up worker thread to process it. Otherwise, it will set up the
  1583. * Error Attention polling timer for the next poll.
  1584. **/
  1585. void lpfc_poll_eratt(unsigned long ptr)
  1586. {
  1587. struct lpfc_hba *phba;
  1588. uint32_t eratt = 0;
  1589. phba = (struct lpfc_hba *)ptr;
  1590. /* Check chip HA register for error event */
  1591. eratt = lpfc_sli_check_eratt(phba);
  1592. if (eratt)
  1593. /* Tell the worker thread there is work to do */
  1594. lpfc_worker_wake_up(phba);
  1595. else
  1596. /* Restart the timer for next eratt poll */
  1597. mod_timer(&phba->eratt_poll, jiffies +
  1598. HZ * LPFC_ERATT_POLL_INTERVAL);
  1599. return;
  1600. }
  1601. /**
  1602. * lpfc_sli_poll_fcp_ring: Handle FCP ring completion in polling mode.
  1603. * @phba: Pointer to HBA context object.
  1604. *
  1605. * This function is called from lpfc_queuecommand, lpfc_poll_timeout,
  1606. * lpfc_abort_handler and lpfc_slave_configure when FCP_RING_POLLING
  1607. * is enabled.
  1608. *
  1609. * The caller does not hold any lock.
  1610. * The function processes each response iocb in the response ring until it
  1611. * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
  1612. * LE bit set. The function will call the completion handler of the command iocb
  1613. * if the response iocb indicates a completion for a command iocb or it is
  1614. * an abort completion.
  1615. **/
  1616. void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
  1617. {
  1618. struct lpfc_sli *psli = &phba->sli;
  1619. struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
  1620. IOCB_t *irsp = NULL;
  1621. IOCB_t *entry = NULL;
  1622. struct lpfc_iocbq *cmdiocbq = NULL;
  1623. struct lpfc_iocbq rspiocbq;
  1624. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1625. uint32_t status;
  1626. uint32_t portRspPut, portRspMax;
  1627. int type;
  1628. uint32_t rsp_cmpl = 0;
  1629. uint32_t ha_copy;
  1630. unsigned long iflags;
  1631. pring->stats.iocb_event++;
  1632. /*
  1633. * The next available response entry should never exceed the maximum
  1634. * entries. If it does, treat it as an adapter hardware error.
  1635. */
  1636. portRspMax = pring->numRiocb;
  1637. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1638. if (unlikely(portRspPut >= portRspMax)) {
  1639. lpfc_sli_rsp_pointers_error(phba, pring);
  1640. return;
  1641. }
  1642. rmb();
  1643. while (pring->rspidx != portRspPut) {
  1644. entry = lpfc_resp_iocb(phba, pring);
  1645. if (++pring->rspidx >= portRspMax)
  1646. pring->rspidx = 0;
  1647. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  1648. (uint32_t *) &rspiocbq.iocb,
  1649. phba->iocb_rsp_size);
  1650. irsp = &rspiocbq.iocb;
  1651. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  1652. pring->stats.iocb_rsp++;
  1653. rsp_cmpl++;
  1654. if (unlikely(irsp->ulpStatus)) {
  1655. /* Rsp ring <ringno> error: IOCB */
  1656. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1657. "0326 Rsp Ring %d error: IOCB Data: "
  1658. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  1659. pring->ringno,
  1660. irsp->un.ulpWord[0],
  1661. irsp->un.ulpWord[1],
  1662. irsp->un.ulpWord[2],
  1663. irsp->un.ulpWord[3],
  1664. irsp->un.ulpWord[4],
  1665. irsp->un.ulpWord[5],
  1666. *(uint32_t *)&irsp->un1,
  1667. *((uint32_t *)&irsp->un1 + 1));
  1668. }
  1669. switch (type) {
  1670. case LPFC_ABORT_IOCB:
  1671. case LPFC_SOL_IOCB:
  1672. /*
  1673. * Idle exchange closed via ABTS from port. No iocb
  1674. * resources need to be recovered.
  1675. */
  1676. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  1677. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  1678. "0314 IOCB cmd 0x%x "
  1679. "processed. Skipping "
  1680. "completion",
  1681. irsp->ulpCommand);
  1682. break;
  1683. }
  1684. spin_lock_irqsave(&phba->hbalock, iflags);
  1685. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  1686. &rspiocbq);
  1687. spin_unlock_irqrestore(&phba->hbalock, iflags);
  1688. if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
  1689. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  1690. &rspiocbq);
  1691. }
  1692. break;
  1693. default:
  1694. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  1695. char adaptermsg[LPFC_MAX_ADPTMSG];
  1696. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  1697. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  1698. MAX_MSG_DATA);
  1699. dev_warn(&((phba->pcidev)->dev),
  1700. "lpfc%d: %s\n",
  1701. phba->brd_no, adaptermsg);
  1702. } else {
  1703. /* Unknown IOCB command */
  1704. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1705. "0321 Unknown IOCB command "
  1706. "Data: x%x, x%x x%x x%x x%x\n",
  1707. type, irsp->ulpCommand,
  1708. irsp->ulpStatus,
  1709. irsp->ulpIoTag,
  1710. irsp->ulpContext);
  1711. }
  1712. break;
  1713. }
  1714. /*
  1715. * The response IOCB has been processed. Update the ring
  1716. * pointer in SLIM. If the port response put pointer has not
  1717. * been updated, sync the pgp->rspPutInx and fetch the new port
  1718. * response put pointer.
  1719. */
  1720. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  1721. if (pring->rspidx == portRspPut)
  1722. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1723. }
  1724. ha_copy = readl(phba->HAregaddr);
  1725. ha_copy >>= (LPFC_FCP_RING * 4);
  1726. if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
  1727. spin_lock_irqsave(&phba->hbalock, iflags);
  1728. pring->stats.iocb_rsp_full++;
  1729. status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
  1730. writel(status, phba->CAregaddr);
  1731. readl(phba->CAregaddr);
  1732. spin_unlock_irqrestore(&phba->hbalock, iflags);
  1733. }
  1734. if ((ha_copy & HA_R0CE_RSP) &&
  1735. (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  1736. spin_lock_irqsave(&phba->hbalock, iflags);
  1737. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  1738. pring->stats.iocb_cmd_empty++;
  1739. /* Force update of the local copy of cmdGetInx */
  1740. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1741. lpfc_sli_resume_iocb(phba, pring);
  1742. if ((pring->lpfc_sli_cmd_available))
  1743. (pring->lpfc_sli_cmd_available) (phba, pring);
  1744. spin_unlock_irqrestore(&phba->hbalock, iflags);
  1745. }
  1746. return;
  1747. }
  1748. /**
  1749. * lpfc_sli_handle_fast_ring_event: Handle ring events on FCP ring.
  1750. * @phba: Pointer to HBA context object.
  1751. * @pring: Pointer to driver SLI ring object.
  1752. * @mask: Host attention register mask for this ring.
  1753. *
  1754. * This function is called from the interrupt context when there is a ring
  1755. * event for the fcp ring. The caller does not hold any lock.
  1756. * The function processes each response iocb in the response ring until it
  1757. * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
  1758. * LE bit set. The function will call the completion handler of the command iocb
  1759. * if the response iocb indicates a completion for a command iocb or it is
  1760. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  1761. * function if this is an unsolicited iocb.
  1762. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  1763. * to check it explicitly. This function always returns 1.
  1764. **/
  1765. static int
  1766. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  1767. struct lpfc_sli_ring *pring, uint32_t mask)
  1768. {
  1769. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1770. IOCB_t *irsp = NULL;
  1771. IOCB_t *entry = NULL;
  1772. struct lpfc_iocbq *cmdiocbq = NULL;
  1773. struct lpfc_iocbq rspiocbq;
  1774. uint32_t status;
  1775. uint32_t portRspPut, portRspMax;
  1776. int rc = 1;
  1777. lpfc_iocb_type type;
  1778. unsigned long iflag;
  1779. uint32_t rsp_cmpl = 0;
  1780. spin_lock_irqsave(&phba->hbalock, iflag);
  1781. pring->stats.iocb_event++;
  1782. /*
  1783. * The next available response entry should never exceed the maximum
  1784. * entries. If it does, treat it as an adapter hardware error.
  1785. */
  1786. portRspMax = pring->numRiocb;
  1787. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1788. if (unlikely(portRspPut >= portRspMax)) {
  1789. lpfc_sli_rsp_pointers_error(phba, pring);
  1790. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1791. return 1;
  1792. }
  1793. rmb();
  1794. while (pring->rspidx != portRspPut) {
  1795. /*
  1796. * Fetch an entry off the ring and copy it into a local data
  1797. * structure. The copy involves a byte-swap since the
  1798. * network byte order and pci byte orders are different.
  1799. */
  1800. entry = lpfc_resp_iocb(phba, pring);
  1801. phba->last_completion_time = jiffies;
  1802. if (++pring->rspidx >= portRspMax)
  1803. pring->rspidx = 0;
  1804. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  1805. (uint32_t *) &rspiocbq.iocb,
  1806. phba->iocb_rsp_size);
  1807. INIT_LIST_HEAD(&(rspiocbq.list));
  1808. irsp = &rspiocbq.iocb;
  1809. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  1810. pring->stats.iocb_rsp++;
  1811. rsp_cmpl++;
  1812. if (unlikely(irsp->ulpStatus)) {
  1813. /*
  1814. * If resource errors reported from HBA, reduce
  1815. * queuedepths of the SCSI device.
  1816. */
  1817. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  1818. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  1819. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1820. lpfc_adjust_queue_depth(phba);
  1821. spin_lock_irqsave(&phba->hbalock, iflag);
  1822. }
  1823. /* Rsp ring <ringno> error: IOCB */
  1824. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1825. "0336 Rsp Ring %d error: IOCB Data: "
  1826. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  1827. pring->ringno,
  1828. irsp->un.ulpWord[0],
  1829. irsp->un.ulpWord[1],
  1830. irsp->un.ulpWord[2],
  1831. irsp->un.ulpWord[3],
  1832. irsp->un.ulpWord[4],
  1833. irsp->un.ulpWord[5],
  1834. *(uint32_t *)&irsp->un1,
  1835. *((uint32_t *)&irsp->un1 + 1));
  1836. }
  1837. switch (type) {
  1838. case LPFC_ABORT_IOCB:
  1839. case LPFC_SOL_IOCB:
  1840. /*
  1841. * Idle exchange closed via ABTS from port. No iocb
  1842. * resources need to be recovered.
  1843. */
  1844. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  1845. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  1846. "0333 IOCB cmd 0x%x"
  1847. " processed. Skipping"
  1848. " completion\n",
  1849. irsp->ulpCommand);
  1850. break;
  1851. }
  1852. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  1853. &rspiocbq);
  1854. if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
  1855. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  1856. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  1857. &rspiocbq);
  1858. } else {
  1859. spin_unlock_irqrestore(&phba->hbalock,
  1860. iflag);
  1861. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  1862. &rspiocbq);
  1863. spin_lock_irqsave(&phba->hbalock,
  1864. iflag);
  1865. }
  1866. }
  1867. break;
  1868. case LPFC_UNSOL_IOCB:
  1869. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1870. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  1871. spin_lock_irqsave(&phba->hbalock, iflag);
  1872. break;
  1873. default:
  1874. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  1875. char adaptermsg[LPFC_MAX_ADPTMSG];
  1876. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  1877. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  1878. MAX_MSG_DATA);
  1879. dev_warn(&((phba->pcidev)->dev),
  1880. "lpfc%d: %s\n",
  1881. phba->brd_no, adaptermsg);
  1882. } else {
  1883. /* Unknown IOCB command */
  1884. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1885. "0334 Unknown IOCB command "
  1886. "Data: x%x, x%x x%x x%x x%x\n",
  1887. type, irsp->ulpCommand,
  1888. irsp->ulpStatus,
  1889. irsp->ulpIoTag,
  1890. irsp->ulpContext);
  1891. }
  1892. break;
  1893. }
  1894. /*
  1895. * The response IOCB has been processed. Update the ring
  1896. * pointer in SLIM. If the port response put pointer has not
  1897. * been updated, sync the pgp->rspPutInx and fetch the new port
  1898. * response put pointer.
  1899. */
  1900. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  1901. if (pring->rspidx == portRspPut)
  1902. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1903. }
  1904. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  1905. pring->stats.iocb_rsp_full++;
  1906. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  1907. writel(status, phba->CAregaddr);
  1908. readl(phba->CAregaddr);
  1909. }
  1910. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  1911. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  1912. pring->stats.iocb_cmd_empty++;
  1913. /* Force update of the local copy of cmdGetInx */
  1914. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1915. lpfc_sli_resume_iocb(phba, pring);
  1916. if ((pring->lpfc_sli_cmd_available))
  1917. (pring->lpfc_sli_cmd_available) (phba, pring);
  1918. }
  1919. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1920. return rc;
  1921. }
  1922. /**
  1923. * lpfc_sli_handle_slow_ring_event: Handle ring events for non-FCP rings.
  1924. * @phba: Pointer to HBA context object.
  1925. * @pring: Pointer to driver SLI ring object.
  1926. * @mask: Host attention register mask for this ring.
  1927. *
  1928. * This function is called from the worker thread when there is a ring
  1929. * event for non-fcp rings. The caller does not hold any lock .
  1930. * The function processes each response iocb in the response ring until it
  1931. * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
  1932. * LE bit set. The function will call lpfc_sli_process_sol_iocb function if the
  1933. * response iocb indicates a completion of a command iocb. The function
  1934. * will call lpfc_sli_process_unsol_iocb function if this is an unsolicited
  1935. * iocb. The function frees the resources or calls the completion handler if
  1936. * this iocb is an abort completion. The function returns 0 when the allocated
  1937. * iocbs are not freed, otherwise returns 1.
  1938. **/
  1939. int
  1940. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  1941. struct lpfc_sli_ring *pring, uint32_t mask)
  1942. {
  1943. struct lpfc_pgp *pgp;
  1944. IOCB_t *entry;
  1945. IOCB_t *irsp = NULL;
  1946. struct lpfc_iocbq *rspiocbp = NULL;
  1947. struct lpfc_iocbq *next_iocb;
  1948. struct lpfc_iocbq *cmdiocbp;
  1949. struct lpfc_iocbq *saveq;
  1950. uint8_t iocb_cmd_type;
  1951. lpfc_iocb_type type;
  1952. uint32_t status, free_saveq;
  1953. uint32_t portRspPut, portRspMax;
  1954. int rc = 1;
  1955. unsigned long iflag;
  1956. pgp = &phba->port_gp[pring->ringno];
  1957. spin_lock_irqsave(&phba->hbalock, iflag);
  1958. pring->stats.iocb_event++;
  1959. /*
  1960. * The next available response entry should never exceed the maximum
  1961. * entries. If it does, treat it as an adapter hardware error.
  1962. */
  1963. portRspMax = pring->numRiocb;
  1964. portRspPut = le32_to_cpu(pgp->rspPutInx);
  1965. if (portRspPut >= portRspMax) {
  1966. /*
  1967. * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
  1968. * rsp ring <portRspMax>
  1969. */
  1970. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1971. "0303 Ring %d handler: portRspPut %d "
  1972. "is bigger then rsp ring %d\n",
  1973. pring->ringno, portRspPut, portRspMax);
  1974. phba->link_state = LPFC_HBA_ERROR;
  1975. spin_unlock_irqrestore(&phba->hbalock, iflag);
  1976. phba->work_hs = HS_FFER3;
  1977. lpfc_handle_eratt(phba);
  1978. return 1;
  1979. }
  1980. rmb();
  1981. while (pring->rspidx != portRspPut) {
  1982. /*
  1983. * Build a completion list and call the appropriate handler.
  1984. * The process is to get the next available response iocb, get
  1985. * a free iocb from the list, copy the response data into the
  1986. * free iocb, insert to the continuation list, and update the
  1987. * next response index to slim. This process makes response
  1988. * iocb's in the ring available to DMA as fast as possible but
  1989. * pays a penalty for a copy operation. Since the iocb is
  1990. * only 32 bytes, this penalty is considered small relative to
  1991. * the PCI reads for register values and a slim write. When
  1992. * the ulpLe field is set, the entire Command has been
  1993. * received.
  1994. */
  1995. entry = lpfc_resp_iocb(phba, pring);
  1996. phba->last_completion_time = jiffies;
  1997. rspiocbp = __lpfc_sli_get_iocbq(phba);
  1998. if (rspiocbp == NULL) {
  1999. printk(KERN_ERR "%s: out of buffers! Failing "
  2000. "completion.\n", __func__);
  2001. break;
  2002. }
  2003. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  2004. phba->iocb_rsp_size);
  2005. irsp = &rspiocbp->iocb;
  2006. if (++pring->rspidx >= portRspMax)
  2007. pring->rspidx = 0;
  2008. if (pring->ringno == LPFC_ELS_RING) {
  2009. lpfc_debugfs_slow_ring_trc(phba,
  2010. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  2011. *(((uint32_t *) irsp) + 4),
  2012. *(((uint32_t *) irsp) + 6),
  2013. *(((uint32_t *) irsp) + 7));
  2014. }
  2015. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  2016. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  2017. pring->iocb_continueq_cnt++;
  2018. if (irsp->ulpLe) {
  2019. /*
  2020. * By default, the driver expects to free all resources
  2021. * associated with this iocb completion.
  2022. */
  2023. free_saveq = 1;
  2024. saveq = list_get_first(&pring->iocb_continueq,
  2025. struct lpfc_iocbq, list);
  2026. irsp = &(saveq->iocb);
  2027. list_del_init(&pring->iocb_continueq);
  2028. pring->iocb_continueq_cnt = 0;
  2029. pring->stats.iocb_rsp++;
  2030. /*
  2031. * If resource errors reported from HBA, reduce
  2032. * queuedepths of the SCSI device.
  2033. */
  2034. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2035. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2036. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2037. lpfc_adjust_queue_depth(phba);
  2038. spin_lock_irqsave(&phba->hbalock, iflag);
  2039. }
  2040. if (irsp->ulpStatus) {
  2041. /* Rsp ring <ringno> error: IOCB */
  2042. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2043. "0328 Rsp Ring %d error: "
  2044. "IOCB Data: "
  2045. "x%x x%x x%x x%x "
  2046. "x%x x%x x%x x%x "
  2047. "x%x x%x x%x x%x "
  2048. "x%x x%x x%x x%x\n",
  2049. pring->ringno,
  2050. irsp->un.ulpWord[0],
  2051. irsp->un.ulpWord[1],
  2052. irsp->un.ulpWord[2],
  2053. irsp->un.ulpWord[3],
  2054. irsp->un.ulpWord[4],
  2055. irsp->un.ulpWord[5],
  2056. *(((uint32_t *) irsp) + 6),
  2057. *(((uint32_t *) irsp) + 7),
  2058. *(((uint32_t *) irsp) + 8),
  2059. *(((uint32_t *) irsp) + 9),
  2060. *(((uint32_t *) irsp) + 10),
  2061. *(((uint32_t *) irsp) + 11),
  2062. *(((uint32_t *) irsp) + 12),
  2063. *(((uint32_t *) irsp) + 13),
  2064. *(((uint32_t *) irsp) + 14),
  2065. *(((uint32_t *) irsp) + 15));
  2066. }
  2067. /*
  2068. * Fetch the IOCB command type and call the correct
  2069. * completion routine. Solicited and Unsolicited
  2070. * IOCBs on the ELS ring get freed back to the
  2071. * lpfc_iocb_list by the discovery kernel thread.
  2072. */
  2073. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2074. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2075. if (type == LPFC_SOL_IOCB) {
  2076. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2077. rc = lpfc_sli_process_sol_iocb(phba, pring,
  2078. saveq);
  2079. spin_lock_irqsave(&phba->hbalock, iflag);
  2080. } else if (type == LPFC_UNSOL_IOCB) {
  2081. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2082. rc = lpfc_sli_process_unsol_iocb(phba, pring,
  2083. saveq);
  2084. spin_lock_irqsave(&phba->hbalock, iflag);
  2085. if (!rc)
  2086. free_saveq = 0;
  2087. } else if (type == LPFC_ABORT_IOCB) {
  2088. if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
  2089. ((cmdiocbp =
  2090. lpfc_sli_iocbq_lookup(phba, pring,
  2091. saveq)))) {
  2092. /* Call the specified completion
  2093. routine */
  2094. if (cmdiocbp->iocb_cmpl) {
  2095. spin_unlock_irqrestore(
  2096. &phba->hbalock,
  2097. iflag);
  2098. (cmdiocbp->iocb_cmpl) (phba,
  2099. cmdiocbp, saveq);
  2100. spin_lock_irqsave(
  2101. &phba->hbalock,
  2102. iflag);
  2103. } else
  2104. __lpfc_sli_release_iocbq(phba,
  2105. cmdiocbp);
  2106. }
  2107. } else if (type == LPFC_UNKNOWN_IOCB) {
  2108. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2109. char adaptermsg[LPFC_MAX_ADPTMSG];
  2110. memset(adaptermsg, 0,
  2111. LPFC_MAX_ADPTMSG);
  2112. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2113. MAX_MSG_DATA);
  2114. dev_warn(&((phba->pcidev)->dev),
  2115. "lpfc%d: %s\n",
  2116. phba->brd_no, adaptermsg);
  2117. } else {
  2118. /* Unknown IOCB command */
  2119. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2120. "0335 Unknown IOCB "
  2121. "command Data: x%x "
  2122. "x%x x%x x%x\n",
  2123. irsp->ulpCommand,
  2124. irsp->ulpStatus,
  2125. irsp->ulpIoTag,
  2126. irsp->ulpContext);
  2127. }
  2128. }
  2129. if (free_saveq) {
  2130. list_for_each_entry_safe(rspiocbp, next_iocb,
  2131. &saveq->list, list) {
  2132. list_del(&rspiocbp->list);
  2133. __lpfc_sli_release_iocbq(phba,
  2134. rspiocbp);
  2135. }
  2136. __lpfc_sli_release_iocbq(phba, saveq);
  2137. }
  2138. rspiocbp = NULL;
  2139. }
  2140. /*
  2141. * If the port response put pointer has not been updated, sync
  2142. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  2143. * response put pointer.
  2144. */
  2145. if (pring->rspidx == portRspPut) {
  2146. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2147. }
  2148. } /* while (pring->rspidx != portRspPut) */
  2149. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  2150. /* At least one response entry has been freed */
  2151. pring->stats.iocb_rsp_full++;
  2152. /* SET RxRE_RSP in Chip Att register */
  2153. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2154. writel(status, phba->CAregaddr);
  2155. readl(phba->CAregaddr); /* flush */
  2156. }
  2157. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2158. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2159. pring->stats.iocb_cmd_empty++;
  2160. /* Force update of the local copy of cmdGetInx */
  2161. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2162. lpfc_sli_resume_iocb(phba, pring);
  2163. if ((pring->lpfc_sli_cmd_available))
  2164. (pring->lpfc_sli_cmd_available) (phba, pring);
  2165. }
  2166. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2167. return rc;
  2168. }
  2169. /**
  2170. * lpfc_sli_abort_iocb_ring: Abort all iocbs in the ring.
  2171. * @phba: Pointer to HBA context object.
  2172. * @pring: Pointer to driver SLI ring object.
  2173. *
  2174. * This function aborts all iocbs in the given ring and frees all the iocb
  2175. * objects in txq. This function issues an abort iocb for all the iocb commands
  2176. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  2177. * the return of this function. The caller is not required to hold any locks.
  2178. **/
  2179. void
  2180. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2181. {
  2182. LIST_HEAD(completions);
  2183. struct lpfc_iocbq *iocb, *next_iocb;
  2184. IOCB_t *cmd = NULL;
  2185. if (pring->ringno == LPFC_ELS_RING) {
  2186. lpfc_fabric_abort_hba(phba);
  2187. }
  2188. /* Error everything on txq and txcmplq
  2189. * First do the txq.
  2190. */
  2191. spin_lock_irq(&phba->hbalock);
  2192. list_splice_init(&pring->txq, &completions);
  2193. pring->txq_cnt = 0;
  2194. /* Next issue ABTS for everything on the txcmplq */
  2195. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  2196. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  2197. spin_unlock_irq(&phba->hbalock);
  2198. while (!list_empty(&completions)) {
  2199. iocb = list_get_first(&completions, struct lpfc_iocbq, list);
  2200. cmd = &iocb->iocb;
  2201. list_del_init(&iocb->list);
  2202. if (!iocb->iocb_cmpl)
  2203. lpfc_sli_release_iocbq(phba, iocb);
  2204. else {
  2205. cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
  2206. cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
  2207. (iocb->iocb_cmpl) (phba, iocb, iocb);
  2208. }
  2209. }
  2210. }
  2211. /**
  2212. * lpfc_sli_flush_fcp_rings: flush all iocbs in the fcp ring.
  2213. * @phba: Pointer to HBA context object.
  2214. *
  2215. * This function flushes all iocbs in the fcp ring and frees all the iocb
  2216. * objects in txq and txcmplq. This function will not issue abort iocbs
  2217. * for all the iocb commands in txcmplq, they will just be returned with
  2218. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  2219. * slot has been permanently disabled.
  2220. **/
  2221. void
  2222. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  2223. {
  2224. LIST_HEAD(txq);
  2225. LIST_HEAD(txcmplq);
  2226. struct lpfc_iocbq *iocb;
  2227. IOCB_t *cmd = NULL;
  2228. struct lpfc_sli *psli = &phba->sli;
  2229. struct lpfc_sli_ring *pring;
  2230. /* Currently, only one fcp ring */
  2231. pring = &psli->ring[psli->fcp_ring];
  2232. spin_lock_irq(&phba->hbalock);
  2233. /* Retrieve everything on txq */
  2234. list_splice_init(&pring->txq, &txq);
  2235. pring->txq_cnt = 0;
  2236. /* Retrieve everything on the txcmplq */
  2237. list_splice_init(&pring->txcmplq, &txcmplq);
  2238. pring->txcmplq_cnt = 0;
  2239. spin_unlock_irq(&phba->hbalock);
  2240. /* Flush the txq */
  2241. while (!list_empty(&txq)) {
  2242. iocb = list_get_first(&txq, struct lpfc_iocbq, list);
  2243. cmd = &iocb->iocb;
  2244. list_del_init(&iocb->list);
  2245. if (!iocb->iocb_cmpl)
  2246. lpfc_sli_release_iocbq(phba, iocb);
  2247. else {
  2248. cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
  2249. cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
  2250. (iocb->iocb_cmpl) (phba, iocb, iocb);
  2251. }
  2252. }
  2253. /* Flush the txcmpq */
  2254. while (!list_empty(&txcmplq)) {
  2255. iocb = list_get_first(&txcmplq, struct lpfc_iocbq, list);
  2256. cmd = &iocb->iocb;
  2257. list_del_init(&iocb->list);
  2258. if (!iocb->iocb_cmpl)
  2259. lpfc_sli_release_iocbq(phba, iocb);
  2260. else {
  2261. cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
  2262. cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
  2263. (iocb->iocb_cmpl) (phba, iocb, iocb);
  2264. }
  2265. }
  2266. }
  2267. /**
  2268. * lpfc_sli_brdready: Check for host status bits.
  2269. * @phba: Pointer to HBA context object.
  2270. * @mask: Bit mask to be checked.
  2271. *
  2272. * This function reads the host status register and compares
  2273. * with the provided bit mask to check if HBA completed
  2274. * the restart. This function will wait in a loop for the
  2275. * HBA to complete restart. If the HBA does not restart within
  2276. * 15 iterations, the function will reset the HBA again. The
  2277. * function returns 1 when HBA fail to restart otherwise returns
  2278. * zero.
  2279. **/
  2280. int
  2281. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  2282. {
  2283. uint32_t status;
  2284. int i = 0;
  2285. int retval = 0;
  2286. /* Read the HBA Host Status Register */
  2287. status = readl(phba->HSregaddr);
  2288. /*
  2289. * Check status register every 100ms for 5 retries, then every
  2290. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  2291. * every 2.5 sec for 4.
  2292. * Break our of the loop if errors occurred during init.
  2293. */
  2294. while (((status & mask) != mask) &&
  2295. !(status & HS_FFERM) &&
  2296. i++ < 20) {
  2297. if (i <= 5)
  2298. msleep(10);
  2299. else if (i <= 10)
  2300. msleep(500);
  2301. else
  2302. msleep(2500);
  2303. if (i == 15) {
  2304. /* Do post */
  2305. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  2306. lpfc_sli_brdrestart(phba);
  2307. }
  2308. /* Read the HBA Host Status Register */
  2309. status = readl(phba->HSregaddr);
  2310. }
  2311. /* Check to see if any errors occurred during init */
  2312. if ((status & HS_FFERM) || (i >= 20)) {
  2313. phba->link_state = LPFC_HBA_ERROR;
  2314. retval = 1;
  2315. }
  2316. return retval;
  2317. }
  2318. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  2319. /**
  2320. * lpfc_reset_barrier: Make HBA ready for HBA reset.
  2321. * @phba: Pointer to HBA context object.
  2322. *
  2323. * This function is called before resetting an HBA. This
  2324. * function requests HBA to quiesce DMAs before a reset.
  2325. **/
  2326. void lpfc_reset_barrier(struct lpfc_hba *phba)
  2327. {
  2328. uint32_t __iomem *resp_buf;
  2329. uint32_t __iomem *mbox_buf;
  2330. volatile uint32_t mbox;
  2331. uint32_t hc_copy;
  2332. int i;
  2333. uint8_t hdrtype;
  2334. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  2335. if (hdrtype != 0x80 ||
  2336. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  2337. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  2338. return;
  2339. /*
  2340. * Tell the other part of the chip to suspend temporarily all
  2341. * its DMA activity.
  2342. */
  2343. resp_buf = phba->MBslimaddr;
  2344. /* Disable the error attention */
  2345. hc_copy = readl(phba->HCregaddr);
  2346. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  2347. readl(phba->HCregaddr); /* flush */
  2348. phba->link_flag |= LS_IGNORE_ERATT;
  2349. if (readl(phba->HAregaddr) & HA_ERATT) {
  2350. /* Clear Chip error bit */
  2351. writel(HA_ERATT, phba->HAregaddr);
  2352. phba->pport->stopped = 1;
  2353. }
  2354. mbox = 0;
  2355. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  2356. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  2357. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  2358. mbox_buf = phba->MBslimaddr;
  2359. writel(mbox, mbox_buf);
  2360. for (i = 0;
  2361. readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
  2362. mdelay(1);
  2363. if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
  2364. if (phba->sli.sli_flag & LPFC_SLI2_ACTIVE ||
  2365. phba->pport->stopped)
  2366. goto restore_hc;
  2367. else
  2368. goto clear_errat;
  2369. }
  2370. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  2371. for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
  2372. mdelay(1);
  2373. clear_errat:
  2374. while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
  2375. mdelay(1);
  2376. if (readl(phba->HAregaddr) & HA_ERATT) {
  2377. writel(HA_ERATT, phba->HAregaddr);
  2378. phba->pport->stopped = 1;
  2379. }
  2380. restore_hc:
  2381. phba->link_flag &= ~LS_IGNORE_ERATT;
  2382. writel(hc_copy, phba->HCregaddr);
  2383. readl(phba->HCregaddr); /* flush */
  2384. }
  2385. /**
  2386. * lpfc_sli_brdkill: Issue a kill_board mailbox command.
  2387. * @phba: Pointer to HBA context object.
  2388. *
  2389. * This function issues a kill_board mailbox command and waits for
  2390. * the error attention interrupt. This function is called for stopping
  2391. * the firmware processing. The caller is not required to hold any
  2392. * locks. This function calls lpfc_hba_down_post function to free
  2393. * any pending commands after the kill. The function will return 1 when it
  2394. * fails to kill the board else will return 0.
  2395. **/
  2396. int
  2397. lpfc_sli_brdkill(struct lpfc_hba *phba)
  2398. {
  2399. struct lpfc_sli *psli;
  2400. LPFC_MBOXQ_t *pmb;
  2401. uint32_t status;
  2402. uint32_t ha_copy;
  2403. int retval;
  2404. int i = 0;
  2405. psli = &phba->sli;
  2406. /* Kill HBA */
  2407. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2408. "0329 Kill HBA Data: x%x x%x\n",
  2409. phba->pport->port_state, psli->sli_flag);
  2410. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2411. if (!pmb)
  2412. return 1;
  2413. /* Disable the error attention */
  2414. spin_lock_irq(&phba->hbalock);
  2415. status = readl(phba->HCregaddr);
  2416. status &= ~HC_ERINT_ENA;
  2417. writel(status, phba->HCregaddr);
  2418. readl(phba->HCregaddr); /* flush */
  2419. phba->link_flag |= LS_IGNORE_ERATT;
  2420. spin_unlock_irq(&phba->hbalock);
  2421. lpfc_kill_board(phba, pmb);
  2422. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  2423. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2424. if (retval != MBX_SUCCESS) {
  2425. if (retval != MBX_BUSY)
  2426. mempool_free(pmb, phba->mbox_mem_pool);
  2427. spin_lock_irq(&phba->hbalock);
  2428. phba->link_flag &= ~LS_IGNORE_ERATT;
  2429. spin_unlock_irq(&phba->hbalock);
  2430. return 1;
  2431. }
  2432. psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
  2433. mempool_free(pmb, phba->mbox_mem_pool);
  2434. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  2435. * attention every 100ms for 3 seconds. If we don't get ERATT after
  2436. * 3 seconds we still set HBA_ERROR state because the status of the
  2437. * board is now undefined.
  2438. */
  2439. ha_copy = readl(phba->HAregaddr);
  2440. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  2441. mdelay(100);
  2442. ha_copy = readl(phba->HAregaddr);
  2443. }
  2444. del_timer_sync(&psli->mbox_tmo);
  2445. if (ha_copy & HA_ERATT) {
  2446. writel(HA_ERATT, phba->HAregaddr);
  2447. phba->pport->stopped = 1;
  2448. }
  2449. spin_lock_irq(&phba->hbalock);
  2450. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  2451. phba->link_flag &= ~LS_IGNORE_ERATT;
  2452. spin_unlock_irq(&phba->hbalock);
  2453. psli->mbox_active = NULL;
  2454. lpfc_hba_down_post(phba);
  2455. phba->link_state = LPFC_HBA_ERROR;
  2456. return ha_copy & HA_ERATT ? 0 : 1;
  2457. }
  2458. /**
  2459. * lpfc_sli_brdreset: Reset the HBA.
  2460. * @phba: Pointer to HBA context object.
  2461. *
  2462. * This function resets the HBA by writing HC_INITFF to the control
  2463. * register. After the HBA resets, this function resets all the iocb ring
  2464. * indices. This function disables PCI layer parity checking during
  2465. * the reset.
  2466. * This function returns 0 always.
  2467. * The caller is not required to hold any locks.
  2468. **/
  2469. int
  2470. lpfc_sli_brdreset(struct lpfc_hba *phba)
  2471. {
  2472. struct lpfc_sli *psli;
  2473. struct lpfc_sli_ring *pring;
  2474. uint16_t cfg_value;
  2475. int i;
  2476. psli = &phba->sli;
  2477. /* Reset HBA */
  2478. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2479. "0325 Reset HBA Data: x%x x%x\n",
  2480. phba->pport->port_state, psli->sli_flag);
  2481. /* perform board reset */
  2482. phba->fc_eventTag = 0;
  2483. phba->pport->fc_myDID = 0;
  2484. phba->pport->fc_prevDID = 0;
  2485. /* Turn off parity checking and serr during the physical reset */
  2486. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  2487. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  2488. (cfg_value &
  2489. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  2490. psli->sli_flag &= ~(LPFC_SLI2_ACTIVE | LPFC_PROCESS_LA);
  2491. /* Now toggle INITFF bit in the Host Control Register */
  2492. writel(HC_INITFF, phba->HCregaddr);
  2493. mdelay(1);
  2494. readl(phba->HCregaddr); /* flush */
  2495. writel(0, phba->HCregaddr);
  2496. readl(phba->HCregaddr); /* flush */
  2497. /* Restore PCI cmd register */
  2498. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  2499. /* Initialize relevant SLI info */
  2500. for (i = 0; i < psli->num_rings; i++) {
  2501. pring = &psli->ring[i];
  2502. pring->flag = 0;
  2503. pring->rspidx = 0;
  2504. pring->next_cmdidx = 0;
  2505. pring->local_getidx = 0;
  2506. pring->cmdidx = 0;
  2507. pring->missbufcnt = 0;
  2508. }
  2509. phba->link_state = LPFC_WARM_START;
  2510. return 0;
  2511. }
  2512. /**
  2513. * lpfc_sli_brdrestart: Restart the HBA.
  2514. * @phba: Pointer to HBA context object.
  2515. *
  2516. * This function is called in the SLI initialization code path to
  2517. * restart the HBA. The caller is not required to hold any lock.
  2518. * This function writes MBX_RESTART mailbox command to the SLIM and
  2519. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  2520. * function to free any pending commands. The function enables
  2521. * POST only during the first initialization. The function returns zero.
  2522. * The function does not guarantee completion of MBX_RESTART mailbox
  2523. * command before the return of this function.
  2524. **/
  2525. int
  2526. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  2527. {
  2528. MAILBOX_t *mb;
  2529. struct lpfc_sli *psli;
  2530. uint16_t skip_post;
  2531. volatile uint32_t word0;
  2532. void __iomem *to_slim;
  2533. spin_lock_irq(&phba->hbalock);
  2534. psli = &phba->sli;
  2535. /* Restart HBA */
  2536. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2537. "0337 Restart HBA Data: x%x x%x\n",
  2538. phba->pport->port_state, psli->sli_flag);
  2539. word0 = 0;
  2540. mb = (MAILBOX_t *) &word0;
  2541. mb->mbxCommand = MBX_RESTART;
  2542. mb->mbxHc = 1;
  2543. lpfc_reset_barrier(phba);
  2544. to_slim = phba->MBslimaddr;
  2545. writel(*(uint32_t *) mb, to_slim);
  2546. readl(to_slim); /* flush */
  2547. /* Only skip post after fc_ffinit is completed */
  2548. if (phba->pport->port_state) {
  2549. skip_post = 1;
  2550. word0 = 1; /* This is really setting up word1 */
  2551. } else {
  2552. skip_post = 0;
  2553. word0 = 0; /* This is really setting up word1 */
  2554. }
  2555. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  2556. writel(*(uint32_t *) mb, to_slim);
  2557. readl(to_slim); /* flush */
  2558. lpfc_sli_brdreset(phba);
  2559. phba->pport->stopped = 0;
  2560. phba->link_state = LPFC_INIT_START;
  2561. spin_unlock_irq(&phba->hbalock);
  2562. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  2563. psli->stats_start = get_seconds();
  2564. if (skip_post)
  2565. mdelay(100);
  2566. else
  2567. mdelay(2000);
  2568. lpfc_hba_down_post(phba);
  2569. return 0;
  2570. }
  2571. /**
  2572. * lpfc_sli_chipset_init: Wait for the restart of the HBA after a restart.
  2573. * @phba: Pointer to HBA context object.
  2574. *
  2575. * This function is called after a HBA restart to wait for successful
  2576. * restart of the HBA. Successful restart of the HBA is indicated by
  2577. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  2578. * iteration, the function will restart the HBA again. The function returns
  2579. * zero if HBA successfully restarted else returns negative error code.
  2580. **/
  2581. static int
  2582. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  2583. {
  2584. uint32_t status, i = 0;
  2585. /* Read the HBA Host Status Register */
  2586. status = readl(phba->HSregaddr);
  2587. /* Check status register to see what current state is */
  2588. i = 0;
  2589. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  2590. /* Check every 100ms for 5 retries, then every 500ms for 5, then
  2591. * every 2.5 sec for 5, then reset board and every 2.5 sec for
  2592. * 4.
  2593. */
  2594. if (i++ >= 20) {
  2595. /* Adapter failed to init, timeout, status reg
  2596. <status> */
  2597. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2598. "0436 Adapter failed to init, "
  2599. "timeout, status reg x%x, "
  2600. "FW Data: A8 x%x AC x%x\n", status,
  2601. readl(phba->MBslimaddr + 0xa8),
  2602. readl(phba->MBslimaddr + 0xac));
  2603. phba->link_state = LPFC_HBA_ERROR;
  2604. return -ETIMEDOUT;
  2605. }
  2606. /* Check to see if any errors occurred during init */
  2607. if (status & HS_FFERM) {
  2608. /* ERROR: During chipset initialization */
  2609. /* Adapter failed to init, chipset, status reg
  2610. <status> */
  2611. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2612. "0437 Adapter failed to init, "
  2613. "chipset, status reg x%x, "
  2614. "FW Data: A8 x%x AC x%x\n", status,
  2615. readl(phba->MBslimaddr + 0xa8),
  2616. readl(phba->MBslimaddr + 0xac));
  2617. phba->link_state = LPFC_HBA_ERROR;
  2618. return -EIO;
  2619. }
  2620. if (i <= 5) {
  2621. msleep(10);
  2622. } else if (i <= 10) {
  2623. msleep(500);
  2624. } else {
  2625. msleep(2500);
  2626. }
  2627. if (i == 15) {
  2628. /* Do post */
  2629. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  2630. lpfc_sli_brdrestart(phba);
  2631. }
  2632. /* Read the HBA Host Status Register */
  2633. status = readl(phba->HSregaddr);
  2634. }
  2635. /* Check to see if any errors occurred during init */
  2636. if (status & HS_FFERM) {
  2637. /* ERROR: During chipset initialization */
  2638. /* Adapter failed to init, chipset, status reg <status> */
  2639. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2640. "0438 Adapter failed to init, chipset, "
  2641. "status reg x%x, "
  2642. "FW Data: A8 x%x AC x%x\n", status,
  2643. readl(phba->MBslimaddr + 0xa8),
  2644. readl(phba->MBslimaddr + 0xac));
  2645. phba->link_state = LPFC_HBA_ERROR;
  2646. return -EIO;
  2647. }
  2648. /* Clear all interrupt enable conditions */
  2649. writel(0, phba->HCregaddr);
  2650. readl(phba->HCregaddr); /* flush */
  2651. /* setup host attn register */
  2652. writel(0xffffffff, phba->HAregaddr);
  2653. readl(phba->HAregaddr); /* flush */
  2654. return 0;
  2655. }
  2656. /**
  2657. * lpfc_sli_hbq_count: Get the number of HBQs to be configured.
  2658. *
  2659. * This function calculates and returns the number of HBQs required to be
  2660. * configured.
  2661. **/
  2662. int
  2663. lpfc_sli_hbq_count(void)
  2664. {
  2665. return ARRAY_SIZE(lpfc_hbq_defs);
  2666. }
  2667. /**
  2668. * lpfc_sli_hbq_entry_count: Calculate total number of hbq entries.
  2669. *
  2670. * This function adds the number of hbq entries in every HBQ to get
  2671. * the total number of hbq entries required for the HBA and returns
  2672. * the total count.
  2673. **/
  2674. static int
  2675. lpfc_sli_hbq_entry_count(void)
  2676. {
  2677. int hbq_count = lpfc_sli_hbq_count();
  2678. int count = 0;
  2679. int i;
  2680. for (i = 0; i < hbq_count; ++i)
  2681. count += lpfc_hbq_defs[i]->entry_count;
  2682. return count;
  2683. }
  2684. /**
  2685. * lpfc_sli_hbq_size: Calculate memory required for all hbq entries.
  2686. *
  2687. * This function calculates amount of memory required for all hbq entries
  2688. * to be configured and returns the total memory required.
  2689. **/
  2690. int
  2691. lpfc_sli_hbq_size(void)
  2692. {
  2693. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  2694. }
  2695. /**
  2696. * lpfc_sli_hbq_setup: configure and initialize HBQs.
  2697. * @phba: Pointer to HBA context object.
  2698. *
  2699. * This function is called during the SLI initialization to configure
  2700. * all the HBQs and post buffers to the HBQ. The caller is not
  2701. * required to hold any locks. This function will return zero if successful
  2702. * else it will return negative error code.
  2703. **/
  2704. static int
  2705. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  2706. {
  2707. int hbq_count = lpfc_sli_hbq_count();
  2708. LPFC_MBOXQ_t *pmb;
  2709. MAILBOX_t *pmbox;
  2710. uint32_t hbqno;
  2711. uint32_t hbq_entry_index;
  2712. /* Get a Mailbox buffer to setup mailbox
  2713. * commands for HBA initialization
  2714. */
  2715. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2716. if (!pmb)
  2717. return -ENOMEM;
  2718. pmbox = &pmb->mb;
  2719. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  2720. phba->link_state = LPFC_INIT_MBX_CMDS;
  2721. phba->hbq_in_use = 1;
  2722. hbq_entry_index = 0;
  2723. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  2724. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  2725. phba->hbqs[hbqno].hbqPutIdx = 0;
  2726. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  2727. phba->hbqs[hbqno].entry_count =
  2728. lpfc_hbq_defs[hbqno]->entry_count;
  2729. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  2730. hbq_entry_index, pmb);
  2731. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  2732. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  2733. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  2734. mbxStatus <status>, ring <num> */
  2735. lpfc_printf_log(phba, KERN_ERR,
  2736. LOG_SLI | LOG_VPORT,
  2737. "1805 Adapter failed to init. "
  2738. "Data: x%x x%x x%x\n",
  2739. pmbox->mbxCommand,
  2740. pmbox->mbxStatus, hbqno);
  2741. phba->link_state = LPFC_HBA_ERROR;
  2742. mempool_free(pmb, phba->mbox_mem_pool);
  2743. return ENXIO;
  2744. }
  2745. }
  2746. phba->hbq_count = hbq_count;
  2747. mempool_free(pmb, phba->mbox_mem_pool);
  2748. /* Initially populate or replenish the HBQs */
  2749. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  2750. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  2751. return 0;
  2752. }
  2753. /**
  2754. * lpfc_sli_config_port: Issue config port mailbox command.
  2755. * @phba: Pointer to HBA context object.
  2756. * @sli_mode: sli mode - 2/3
  2757. *
  2758. * This function is called by the sli intialization code path
  2759. * to issue config_port mailbox command. This function restarts the
  2760. * HBA firmware and issues a config_port mailbox command to configure
  2761. * the SLI interface in the sli mode specified by sli_mode
  2762. * variable. The caller is not required to hold any locks.
  2763. * The function returns 0 if successful, else returns negative error
  2764. * code.
  2765. **/
  2766. int
  2767. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  2768. {
  2769. LPFC_MBOXQ_t *pmb;
  2770. uint32_t resetcount = 0, rc = 0, done = 0;
  2771. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  2772. if (!pmb) {
  2773. phba->link_state = LPFC_HBA_ERROR;
  2774. return -ENOMEM;
  2775. }
  2776. phba->sli_rev = sli_mode;
  2777. while (resetcount < 2 && !done) {
  2778. spin_lock_irq(&phba->hbalock);
  2779. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  2780. spin_unlock_irq(&phba->hbalock);
  2781. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  2782. lpfc_sli_brdrestart(phba);
  2783. msleep(2500);
  2784. rc = lpfc_sli_chipset_init(phba);
  2785. if (rc)
  2786. break;
  2787. spin_lock_irq(&phba->hbalock);
  2788. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  2789. spin_unlock_irq(&phba->hbalock);
  2790. resetcount++;
  2791. /* Call pre CONFIG_PORT mailbox command initialization. A
  2792. * value of 0 means the call was successful. Any other
  2793. * nonzero value is a failure, but if ERESTART is returned,
  2794. * the driver may reset the HBA and try again.
  2795. */
  2796. rc = lpfc_config_port_prep(phba);
  2797. if (rc == -ERESTART) {
  2798. phba->link_state = LPFC_LINK_UNKNOWN;
  2799. continue;
  2800. } else if (rc)
  2801. break;
  2802. phba->link_state = LPFC_INIT_MBX_CMDS;
  2803. lpfc_config_port(phba, pmb);
  2804. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  2805. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  2806. LPFC_SLI3_HBQ_ENABLED |
  2807. LPFC_SLI3_CRP_ENABLED |
  2808. LPFC_SLI3_INB_ENABLED);
  2809. if (rc != MBX_SUCCESS) {
  2810. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2811. "0442 Adapter failed to init, mbxCmd x%x "
  2812. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  2813. pmb->mb.mbxCommand, pmb->mb.mbxStatus, 0);
  2814. spin_lock_irq(&phba->hbalock);
  2815. phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
  2816. spin_unlock_irq(&phba->hbalock);
  2817. rc = -ENXIO;
  2818. } else
  2819. done = 1;
  2820. }
  2821. if (!done) {
  2822. rc = -EINVAL;
  2823. goto do_prep_failed;
  2824. }
  2825. if (pmb->mb.un.varCfgPort.sli_mode == 3) {
  2826. if (!pmb->mb.un.varCfgPort.cMA) {
  2827. rc = -ENXIO;
  2828. goto do_prep_failed;
  2829. }
  2830. if (phba->max_vpi && pmb->mb.un.varCfgPort.gmv) {
  2831. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  2832. phba->max_vpi = pmb->mb.un.varCfgPort.max_vpi;
  2833. } else
  2834. phba->max_vpi = 0;
  2835. if (pmb->mb.un.varCfgPort.gerbm)
  2836. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  2837. if (pmb->mb.un.varCfgPort.gcrp)
  2838. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  2839. if (pmb->mb.un.varCfgPort.ginb) {
  2840. phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
  2841. phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
  2842. phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
  2843. phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
  2844. phba->inb_last_counter =
  2845. phba->mbox->us.s3_inb_pgp.counter;
  2846. } else {
  2847. phba->port_gp = phba->mbox->us.s3_pgp.port;
  2848. phba->inb_ha_copy = NULL;
  2849. phba->inb_counter = NULL;
  2850. }
  2851. } else {
  2852. phba->port_gp = phba->mbox->us.s2.port;
  2853. phba->inb_ha_copy = NULL;
  2854. phba->inb_counter = NULL;
  2855. phba->max_vpi = 0;
  2856. }
  2857. do_prep_failed:
  2858. mempool_free(pmb, phba->mbox_mem_pool);
  2859. return rc;
  2860. }
  2861. /**
  2862. * lpfc_sli_hba_setup: SLI intialization function.
  2863. * @phba: Pointer to HBA context object.
  2864. *
  2865. * This function is the main SLI intialization function. This function
  2866. * is called by the HBA intialization code, HBA reset code and HBA
  2867. * error attention handler code. Caller is not required to hold any
  2868. * locks. This function issues config_port mailbox command to configure
  2869. * the SLI, setup iocb rings and HBQ rings. In the end the function
  2870. * calls the config_port_post function to issue init_link mailbox
  2871. * command and to start the discovery. The function will return zero
  2872. * if successful, else it will return negative error code.
  2873. **/
  2874. int
  2875. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  2876. {
  2877. uint32_t rc;
  2878. int mode = 3;
  2879. switch (lpfc_sli_mode) {
  2880. case 2:
  2881. if (phba->cfg_enable_npiv) {
  2882. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  2883. "1824 NPIV enabled: Override lpfc_sli_mode "
  2884. "parameter (%d) to auto (0).\n",
  2885. lpfc_sli_mode);
  2886. break;
  2887. }
  2888. mode = 2;
  2889. break;
  2890. case 0:
  2891. case 3:
  2892. break;
  2893. default:
  2894. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  2895. "1819 Unrecognized lpfc_sli_mode "
  2896. "parameter: %d.\n", lpfc_sli_mode);
  2897. break;
  2898. }
  2899. rc = lpfc_sli_config_port(phba, mode);
  2900. if (rc && lpfc_sli_mode == 3)
  2901. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  2902. "1820 Unable to select SLI-3. "
  2903. "Not supported by adapter.\n");
  2904. if (rc && mode != 2)
  2905. rc = lpfc_sli_config_port(phba, 2);
  2906. if (rc)
  2907. goto lpfc_sli_hba_setup_error;
  2908. if (phba->sli_rev == 3) {
  2909. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  2910. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  2911. } else {
  2912. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  2913. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  2914. phba->sli3_options = 0;
  2915. }
  2916. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  2917. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  2918. phba->sli_rev, phba->max_vpi);
  2919. rc = lpfc_sli_ring_map(phba);
  2920. if (rc)
  2921. goto lpfc_sli_hba_setup_error;
  2922. /* Init HBQs */
  2923. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2924. rc = lpfc_sli_hbq_setup(phba);
  2925. if (rc)
  2926. goto lpfc_sli_hba_setup_error;
  2927. }
  2928. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  2929. rc = lpfc_config_port_post(phba);
  2930. if (rc)
  2931. goto lpfc_sli_hba_setup_error;
  2932. return rc;
  2933. lpfc_sli_hba_setup_error:
  2934. phba->link_state = LPFC_HBA_ERROR;
  2935. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  2936. "0445 Firmware initialization failed\n");
  2937. return rc;
  2938. }
  2939. /**
  2940. * lpfc_mbox_timeout: Timeout call back function for mbox timer.
  2941. * @ptr: context object - pointer to hba structure.
  2942. *
  2943. * This is the callback function for mailbox timer. The mailbox
  2944. * timer is armed when a new mailbox command is issued and the timer
  2945. * is deleted when the mailbox complete. The function is called by
  2946. * the kernel timer code when a mailbox does not complete within
  2947. * expected time. This function wakes up the worker thread to
  2948. * process the mailbox timeout and returns. All the processing is
  2949. * done by the worker thread function lpfc_mbox_timeout_handler.
  2950. **/
  2951. void
  2952. lpfc_mbox_timeout(unsigned long ptr)
  2953. {
  2954. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  2955. unsigned long iflag;
  2956. uint32_t tmo_posted;
  2957. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  2958. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  2959. if (!tmo_posted)
  2960. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  2961. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  2962. if (!tmo_posted)
  2963. lpfc_worker_wake_up(phba);
  2964. return;
  2965. }
  2966. /**
  2967. * lpfc_mbox_timeout_handler: Worker thread function to handle mailbox timeout.
  2968. * @phba: Pointer to HBA context object.
  2969. *
  2970. * This function is called from worker thread when a mailbox command times out.
  2971. * The caller is not required to hold any locks. This function will reset the
  2972. * HBA and recover all the pending commands.
  2973. **/
  2974. void
  2975. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  2976. {
  2977. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  2978. MAILBOX_t *mb = &pmbox->mb;
  2979. struct lpfc_sli *psli = &phba->sli;
  2980. struct lpfc_sli_ring *pring;
  2981. if (!(phba->pport->work_port_events & WORKER_MBOX_TMO)) {
  2982. return;
  2983. }
  2984. /* Mbox cmd <mbxCommand> timeout */
  2985. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2986. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  2987. mb->mbxCommand,
  2988. phba->pport->port_state,
  2989. phba->sli.sli_flag,
  2990. phba->sli.mbox_active);
  2991. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  2992. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  2993. * it to fail all oustanding SCSI IO.
  2994. */
  2995. spin_lock_irq(&phba->pport->work_port_lock);
  2996. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  2997. spin_unlock_irq(&phba->pport->work_port_lock);
  2998. spin_lock_irq(&phba->hbalock);
  2999. phba->link_state = LPFC_LINK_UNKNOWN;
  3000. psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
  3001. spin_unlock_irq(&phba->hbalock);
  3002. pring = &psli->ring[psli->fcp_ring];
  3003. lpfc_sli_abort_iocb_ring(phba, pring);
  3004. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  3005. "0345 Resetting board due to mailbox timeout\n");
  3006. /*
  3007. * lpfc_offline calls lpfc_sli_hba_down which will clean up
  3008. * on oustanding mailbox commands.
  3009. */
  3010. /* If resets are disabled then set error state and return. */
  3011. if (!phba->cfg_enable_hba_reset) {
  3012. phba->link_state = LPFC_HBA_ERROR;
  3013. return;
  3014. }
  3015. lpfc_offline_prep(phba);
  3016. lpfc_offline(phba);
  3017. lpfc_sli_brdrestart(phba);
  3018. lpfc_online(phba);
  3019. lpfc_unblock_mgmt_io(phba);
  3020. return;
  3021. }
  3022. /**
  3023. * lpfc_sli_issue_mbox: Issue a mailbox command to firmware.
  3024. * @phba: Pointer to HBA context object.
  3025. * @pmbox: Pointer to mailbox object.
  3026. * @flag: Flag indicating how the mailbox need to be processed.
  3027. *
  3028. * This function is called by discovery code and HBA management code
  3029. * to submit a mailbox command to firmware. This function gets the
  3030. * hbalock to protect the data structures.
  3031. * The mailbox command can be submitted in polling mode, in which case
  3032. * this function will wait in a polling loop for the completion of the
  3033. * mailbox.
  3034. * If the mailbox is submitted in no_wait mode (not polling) the
  3035. * function will submit the command and returns immediately without waiting
  3036. * for the mailbox completion. The no_wait is supported only when HBA
  3037. * is in SLI2/SLI3 mode - interrupts are enabled.
  3038. * The SLI interface allows only one mailbox pending at a time. If the
  3039. * mailbox is issued in polling mode and there is already a mailbox
  3040. * pending, then the function will return an error. If the mailbox is issued
  3041. * in NO_WAIT mode and there is a mailbox pending already, the function
  3042. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  3043. * The sli layer owns the mailbox object until the completion of mailbox
  3044. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  3045. * return codes the caller owns the mailbox command after the return of
  3046. * the function.
  3047. **/
  3048. int
  3049. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  3050. {
  3051. MAILBOX_t *mb;
  3052. struct lpfc_sli *psli = &phba->sli;
  3053. uint32_t status, evtctr;
  3054. uint32_t ha_copy;
  3055. int i;
  3056. unsigned long timeout;
  3057. unsigned long drvr_flag = 0;
  3058. uint32_t word0, ldata;
  3059. void __iomem *to_slim;
  3060. int processing_queue = 0;
  3061. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  3062. if (!pmbox) {
  3063. /* processing mbox queue from intr_handler */
  3064. processing_queue = 1;
  3065. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3066. pmbox = lpfc_mbox_get(phba);
  3067. if (!pmbox) {
  3068. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3069. return MBX_SUCCESS;
  3070. }
  3071. }
  3072. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  3073. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  3074. if(!pmbox->vport) {
  3075. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3076. lpfc_printf_log(phba, KERN_ERR,
  3077. LOG_MBOX | LOG_VPORT,
  3078. "1806 Mbox x%x failed. No vport\n",
  3079. pmbox->mb.mbxCommand);
  3080. dump_stack();
  3081. goto out_not_finished;
  3082. }
  3083. }
  3084. /* If the PCI channel is in offline state, do not post mbox. */
  3085. if (unlikely(pci_channel_offline(phba->pcidev))) {
  3086. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3087. goto out_not_finished;
  3088. }
  3089. psli = &phba->sli;
  3090. mb = &pmbox->mb;
  3091. status = MBX_SUCCESS;
  3092. if (phba->link_state == LPFC_HBA_ERROR) {
  3093. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3094. /* Mbox command <mbxCommand> cannot issue */
  3095. LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
  3096. goto out_not_finished;
  3097. }
  3098. if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
  3099. !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
  3100. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3101. LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
  3102. goto out_not_finished;
  3103. }
  3104. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  3105. /* Polling for a mbox command when another one is already active
  3106. * is not allowed in SLI. Also, the driver must have established
  3107. * SLI2 mode to queue and process multiple mbox commands.
  3108. */
  3109. if (flag & MBX_POLL) {
  3110. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3111. /* Mbox command <mbxCommand> cannot issue */
  3112. LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
  3113. goto out_not_finished;
  3114. }
  3115. if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
  3116. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3117. /* Mbox command <mbxCommand> cannot issue */
  3118. LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
  3119. goto out_not_finished;
  3120. }
  3121. /* Another mailbox command is still being processed, queue this
  3122. * command to be processed later.
  3123. */
  3124. lpfc_mbox_put(phba, pmbox);
  3125. /* Mbox cmd issue - BUSY */
  3126. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  3127. "(%d):0308 Mbox cmd issue - BUSY Data: "
  3128. "x%x x%x x%x x%x\n",
  3129. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  3130. mb->mbxCommand, phba->pport->port_state,
  3131. psli->sli_flag, flag);
  3132. psli->slistat.mbox_busy++;
  3133. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3134. if (pmbox->vport) {
  3135. lpfc_debugfs_disc_trc(pmbox->vport,
  3136. LPFC_DISC_TRC_MBOX_VPORT,
  3137. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  3138. (uint32_t)mb->mbxCommand,
  3139. mb->un.varWords[0], mb->un.varWords[1]);
  3140. }
  3141. else {
  3142. lpfc_debugfs_disc_trc(phba->pport,
  3143. LPFC_DISC_TRC_MBOX,
  3144. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  3145. (uint32_t)mb->mbxCommand,
  3146. mb->un.varWords[0], mb->un.varWords[1]);
  3147. }
  3148. return MBX_BUSY;
  3149. }
  3150. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  3151. /* If we are not polling, we MUST be in SLI2 mode */
  3152. if (flag != MBX_POLL) {
  3153. if (!(psli->sli_flag & LPFC_SLI2_ACTIVE) &&
  3154. (mb->mbxCommand != MBX_KILL_BOARD)) {
  3155. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3156. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3157. /* Mbox command <mbxCommand> cannot issue */
  3158. LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
  3159. goto out_not_finished;
  3160. }
  3161. /* timeout active mbox command */
  3162. mod_timer(&psli->mbox_tmo, (jiffies +
  3163. (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
  3164. }
  3165. /* Mailbox cmd <cmd> issue */
  3166. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  3167. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  3168. "x%x\n",
  3169. pmbox->vport ? pmbox->vport->vpi : 0,
  3170. mb->mbxCommand, phba->pport->port_state,
  3171. psli->sli_flag, flag);
  3172. if (mb->mbxCommand != MBX_HEARTBEAT) {
  3173. if (pmbox->vport) {
  3174. lpfc_debugfs_disc_trc(pmbox->vport,
  3175. LPFC_DISC_TRC_MBOX_VPORT,
  3176. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  3177. (uint32_t)mb->mbxCommand,
  3178. mb->un.varWords[0], mb->un.varWords[1]);
  3179. }
  3180. else {
  3181. lpfc_debugfs_disc_trc(phba->pport,
  3182. LPFC_DISC_TRC_MBOX,
  3183. "MBOX Send: cmd:x%x mb:x%x x%x",
  3184. (uint32_t)mb->mbxCommand,
  3185. mb->un.varWords[0], mb->un.varWords[1]);
  3186. }
  3187. }
  3188. psli->slistat.mbox_cmd++;
  3189. evtctr = psli->slistat.mbox_event;
  3190. /* next set own bit for the adapter and copy over command word */
  3191. mb->mbxOwner = OWN_CHIP;
  3192. if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
  3193. /* First copy command data to host SLIM area */
  3194. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  3195. } else {
  3196. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  3197. /* copy command data into host mbox for cmpl */
  3198. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  3199. }
  3200. /* First copy mbox command data to HBA SLIM, skip past first
  3201. word */
  3202. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3203. lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
  3204. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  3205. /* Next copy over first word, with mbxOwner set */
  3206. ldata = *((uint32_t *)mb);
  3207. to_slim = phba->MBslimaddr;
  3208. writel(ldata, to_slim);
  3209. readl(to_slim); /* flush */
  3210. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  3211. /* switch over to host mailbox */
  3212. psli->sli_flag |= LPFC_SLI2_ACTIVE;
  3213. }
  3214. }
  3215. wmb();
  3216. switch (flag) {
  3217. case MBX_NOWAIT:
  3218. /* Set up reference to mailbox command */
  3219. psli->mbox_active = pmbox;
  3220. /* Interrupt board to do it */
  3221. writel(CA_MBATT, phba->CAregaddr);
  3222. readl(phba->CAregaddr); /* flush */
  3223. /* Don't wait for it to finish, just return */
  3224. break;
  3225. case MBX_POLL:
  3226. /* Set up null reference to mailbox command */
  3227. psli->mbox_active = NULL;
  3228. /* Interrupt board to do it */
  3229. writel(CA_MBATT, phba->CAregaddr);
  3230. readl(phba->CAregaddr); /* flush */
  3231. if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
  3232. /* First read mbox status word */
  3233. word0 = *((uint32_t *)phba->mbox);
  3234. word0 = le32_to_cpu(word0);
  3235. } else {
  3236. /* First read mbox status word */
  3237. word0 = readl(phba->MBslimaddr);
  3238. }
  3239. /* Read the HBA Host Attention Register */
  3240. ha_copy = readl(phba->HAregaddr);
  3241. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  3242. mb->mbxCommand) *
  3243. 1000) + jiffies;
  3244. i = 0;
  3245. /* Wait for command to complete */
  3246. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  3247. (!(ha_copy & HA_MBATT) &&
  3248. (phba->link_state > LPFC_WARM_START))) {
  3249. if (time_after(jiffies, timeout)) {
  3250. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3251. spin_unlock_irqrestore(&phba->hbalock,
  3252. drvr_flag);
  3253. goto out_not_finished;
  3254. }
  3255. /* Check if we took a mbox interrupt while we were
  3256. polling */
  3257. if (((word0 & OWN_CHIP) != OWN_CHIP)
  3258. && (evtctr != psli->slistat.mbox_event))
  3259. break;
  3260. if (i++ > 10) {
  3261. spin_unlock_irqrestore(&phba->hbalock,
  3262. drvr_flag);
  3263. msleep(1);
  3264. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  3265. }
  3266. if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
  3267. /* First copy command data */
  3268. word0 = *((uint32_t *)phba->mbox);
  3269. word0 = le32_to_cpu(word0);
  3270. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  3271. MAILBOX_t *slimmb;
  3272. uint32_t slimword0;
  3273. /* Check real SLIM for any errors */
  3274. slimword0 = readl(phba->MBslimaddr);
  3275. slimmb = (MAILBOX_t *) & slimword0;
  3276. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  3277. && slimmb->mbxStatus) {
  3278. psli->sli_flag &=
  3279. ~LPFC_SLI2_ACTIVE;
  3280. word0 = slimword0;
  3281. }
  3282. }
  3283. } else {
  3284. /* First copy command data */
  3285. word0 = readl(phba->MBslimaddr);
  3286. }
  3287. /* Read the HBA Host Attention Register */
  3288. ha_copy = readl(phba->HAregaddr);
  3289. }
  3290. if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
  3291. /* copy results back to user */
  3292. lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
  3293. } else {
  3294. /* First copy command data */
  3295. lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
  3296. MAILBOX_CMD_SIZE);
  3297. if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
  3298. pmbox->context2) {
  3299. lpfc_memcpy_from_slim((void *)pmbox->context2,
  3300. phba->MBslimaddr + DMP_RSP_OFFSET,
  3301. mb->un.varDmp.word_cnt);
  3302. }
  3303. }
  3304. writel(HA_MBATT, phba->HAregaddr);
  3305. readl(phba->HAregaddr); /* flush */
  3306. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3307. status = mb->mbxStatus;
  3308. }
  3309. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  3310. return status;
  3311. out_not_finished:
  3312. if (processing_queue) {
  3313. pmbox->mb.mbxStatus = MBX_NOT_FINISHED;
  3314. lpfc_mbox_cmpl_put(phba, pmbox);
  3315. }
  3316. return MBX_NOT_FINISHED;
  3317. }
  3318. /**
  3319. * __lpfc_sli_ringtx_put: Add an iocb to the txq.
  3320. * @phba: Pointer to HBA context object.
  3321. * @pring: Pointer to driver SLI ring object.
  3322. * @piocb: Pointer to address of newly added command iocb.
  3323. *
  3324. * This function is called with hbalock held to add a command
  3325. * iocb to the txq when SLI layer cannot submit the command iocb
  3326. * to the ring.
  3327. **/
  3328. static void
  3329. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  3330. struct lpfc_iocbq *piocb)
  3331. {
  3332. /* Insert the caller's iocb in the txq tail for later processing. */
  3333. list_add_tail(&piocb->list, &pring->txq);
  3334. pring->txq_cnt++;
  3335. }
  3336. /**
  3337. * lpfc_sli_next_iocb: Get the next iocb in the txq.
  3338. * @phba: Pointer to HBA context object.
  3339. * @pring: Pointer to driver SLI ring object.
  3340. * @piocb: Pointer to address of newly added command iocb.
  3341. *
  3342. * This function is called with hbalock held before a new
  3343. * iocb is submitted to the firmware. This function checks
  3344. * txq to flush the iocbs in txq to Firmware before
  3345. * submitting new iocbs to the Firmware.
  3346. * If there are iocbs in the txq which need to be submitted
  3347. * to firmware, lpfc_sli_next_iocb returns the first element
  3348. * of the txq after dequeuing it from txq.
  3349. * If there is no iocb in the txq then the function will return
  3350. * *piocb and *piocb is set to NULL. Caller needs to check
  3351. * *piocb to find if there are more commands in the txq.
  3352. **/
  3353. static struct lpfc_iocbq *
  3354. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  3355. struct lpfc_iocbq **piocb)
  3356. {
  3357. struct lpfc_iocbq * nextiocb;
  3358. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  3359. if (!nextiocb) {
  3360. nextiocb = *piocb;
  3361. *piocb = NULL;
  3362. }
  3363. return nextiocb;
  3364. }
  3365. /**
  3366. * __lpfc_sli_issue_iocb: Lockless version of lpfc_sli_issue_iocb.
  3367. * @phba: Pointer to HBA context object.
  3368. * @pring: Pointer to driver SLI ring object.
  3369. * @piocb: Pointer to command iocb.
  3370. * @flag: Flag indicating if this command can be put into txq.
  3371. *
  3372. * __lpfc_sli_issue_iocb is used by other functions in the driver
  3373. * to issue an iocb command to the HBA. If the PCI slot is recovering
  3374. * from error state or if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  3375. * flag is turned on, the function returns IOCB_ERROR.
  3376. * When the link is down, this function allows only iocbs for
  3377. * posting buffers.
  3378. * This function finds next available slot in the command ring and
  3379. * posts the command to the available slot and writes the port
  3380. * attention register to request HBA start processing new iocb.
  3381. * If there is no slot available in the ring and
  3382. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the
  3383. * txq, otherwise the function returns IOCB_BUSY.
  3384. *
  3385. * This function is called with hbalock held.
  3386. * The function will return success after it successfully submit the
  3387. * iocb to firmware or after adding to the txq.
  3388. **/
  3389. static int
  3390. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  3391. struct lpfc_iocbq *piocb, uint32_t flag)
  3392. {
  3393. struct lpfc_iocbq *nextiocb;
  3394. IOCB_t *iocb;
  3395. if (piocb->iocb_cmpl && (!piocb->vport) &&
  3396. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  3397. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  3398. lpfc_printf_log(phba, KERN_ERR,
  3399. LOG_SLI | LOG_VPORT,
  3400. "1807 IOCB x%x failed. No vport\n",
  3401. piocb->iocb.ulpCommand);
  3402. dump_stack();
  3403. return IOCB_ERROR;
  3404. }
  3405. /* If the PCI channel is in offline state, do not post iocbs. */
  3406. if (unlikely(pci_channel_offline(phba->pcidev)))
  3407. return IOCB_ERROR;
  3408. /*
  3409. * We should never get an IOCB if we are in a < LINK_DOWN state
  3410. */
  3411. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  3412. return IOCB_ERROR;
  3413. /*
  3414. * Check to see if we are blocking IOCB processing because of a
  3415. * outstanding event.
  3416. */
  3417. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  3418. goto iocb_busy;
  3419. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  3420. /*
  3421. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  3422. * can be issued if the link is not up.
  3423. */
  3424. switch (piocb->iocb.ulpCommand) {
  3425. case CMD_GEN_REQUEST64_CR:
  3426. case CMD_GEN_REQUEST64_CX:
  3427. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  3428. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  3429. FC_FCP_CMND) ||
  3430. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  3431. MENLO_TRANSPORT_TYPE))
  3432. goto iocb_busy;
  3433. break;
  3434. case CMD_QUE_RING_BUF_CN:
  3435. case CMD_QUE_RING_BUF64_CN:
  3436. /*
  3437. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  3438. * completion, iocb_cmpl MUST be 0.
  3439. */
  3440. if (piocb->iocb_cmpl)
  3441. piocb->iocb_cmpl = NULL;
  3442. /*FALLTHROUGH*/
  3443. case CMD_CREATE_XRI_CR:
  3444. case CMD_CLOSE_XRI_CN:
  3445. case CMD_CLOSE_XRI_CX:
  3446. break;
  3447. default:
  3448. goto iocb_busy;
  3449. }
  3450. /*
  3451. * For FCP commands, we must be in a state where we can process link
  3452. * attention events.
  3453. */
  3454. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  3455. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  3456. goto iocb_busy;
  3457. }
  3458. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  3459. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  3460. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  3461. if (iocb)
  3462. lpfc_sli_update_ring(phba, pring);
  3463. else
  3464. lpfc_sli_update_full_ring(phba, pring);
  3465. if (!piocb)
  3466. return IOCB_SUCCESS;
  3467. goto out_busy;
  3468. iocb_busy:
  3469. pring->stats.iocb_cmd_delay++;
  3470. out_busy:
  3471. if (!(flag & SLI_IOCB_RET_IOCB)) {
  3472. __lpfc_sli_ringtx_put(phba, pring, piocb);
  3473. return IOCB_SUCCESS;
  3474. }
  3475. return IOCB_BUSY;
  3476. }
  3477. /**
  3478. * lpfc_sli_issue_iocb: Wrapper function for __lpfc_sli_issue_iocb.
  3479. * @phba: Pointer to HBA context object.
  3480. * @pring: Pointer to driver SLI ring object.
  3481. * @piocb: Pointer to command iocb.
  3482. * @flag: Flag indicating if this command can be put into txq.
  3483. *
  3484. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  3485. * function. This function gets the hbalock and calls
  3486. * __lpfc_sli_issue_iocb function and will return the error returned
  3487. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  3488. * functions which do not hold hbalock.
  3489. **/
  3490. int
  3491. lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  3492. struct lpfc_iocbq *piocb, uint32_t flag)
  3493. {
  3494. unsigned long iflags;
  3495. int rc;
  3496. spin_lock_irqsave(&phba->hbalock, iflags);
  3497. rc = __lpfc_sli_issue_iocb(phba, pring, piocb, flag);
  3498. spin_unlock_irqrestore(&phba->hbalock, iflags);
  3499. return rc;
  3500. }
  3501. /**
  3502. * lpfc_extra_ring_setup: Extra ring setup function.
  3503. * @phba: Pointer to HBA context object.
  3504. *
  3505. * This function is called while driver attaches with the
  3506. * HBA to setup the extra ring. The extra ring is used
  3507. * only when driver needs to support target mode functionality
  3508. * or IP over FC functionalities.
  3509. *
  3510. * This function is called with no lock held.
  3511. **/
  3512. static int
  3513. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  3514. {
  3515. struct lpfc_sli *psli;
  3516. struct lpfc_sli_ring *pring;
  3517. psli = &phba->sli;
  3518. /* Adjust cmd/rsp ring iocb entries more evenly */
  3519. /* Take some away from the FCP ring */
  3520. pring = &psli->ring[psli->fcp_ring];
  3521. pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  3522. pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  3523. pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  3524. pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  3525. /* and give them to the extra ring */
  3526. pring = &psli->ring[psli->extra_ring];
  3527. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  3528. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  3529. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  3530. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  3531. /* Setup default profile for this ring */
  3532. pring->iotag_max = 4096;
  3533. pring->num_mask = 1;
  3534. pring->prt[0].profile = 0; /* Mask 0 */
  3535. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  3536. pring->prt[0].type = phba->cfg_multi_ring_type;
  3537. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  3538. return 0;
  3539. }
  3540. /**
  3541. * lpfc_sli_async_event_handler: ASYNC iocb handler function.
  3542. * @phba: Pointer to HBA context object.
  3543. * @pring: Pointer to driver SLI ring object.
  3544. * @iocbq: Pointer to iocb object.
  3545. *
  3546. * This function is called by the slow ring event handler
  3547. * function when there is an ASYNC event iocb in the ring.
  3548. * This function is called with no lock held.
  3549. * Currently this function handles only temperature related
  3550. * ASYNC events. The function decodes the temperature sensor
  3551. * event message and posts events for the management applications.
  3552. **/
  3553. static void
  3554. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  3555. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  3556. {
  3557. IOCB_t *icmd;
  3558. uint16_t evt_code;
  3559. uint16_t temp;
  3560. struct temp_event temp_event_data;
  3561. struct Scsi_Host *shost;
  3562. icmd = &iocbq->iocb;
  3563. evt_code = icmd->un.asyncstat.evt_code;
  3564. temp = icmd->ulpContext;
  3565. if ((evt_code != ASYNC_TEMP_WARN) &&
  3566. (evt_code != ASYNC_TEMP_SAFE)) {
  3567. lpfc_printf_log(phba,
  3568. KERN_ERR,
  3569. LOG_SLI,
  3570. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  3571. " evt_code 0x%x\n",
  3572. pring->ringno,
  3573. icmd->un.asyncstat.evt_code);
  3574. return;
  3575. }
  3576. temp_event_data.data = (uint32_t)temp;
  3577. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  3578. if (evt_code == ASYNC_TEMP_WARN) {
  3579. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  3580. lpfc_printf_log(phba,
  3581. KERN_ERR,
  3582. LOG_TEMP,
  3583. "0347 Adapter is very hot, please take "
  3584. "corrective action. temperature : %d Celsius\n",
  3585. temp);
  3586. }
  3587. if (evt_code == ASYNC_TEMP_SAFE) {
  3588. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  3589. lpfc_printf_log(phba,
  3590. KERN_ERR,
  3591. LOG_TEMP,
  3592. "0340 Adapter temperature is OK now. "
  3593. "temperature : %d Celsius\n",
  3594. temp);
  3595. }
  3596. /* Send temperature change event to applications */
  3597. shost = lpfc_shost_from_vport(phba->pport);
  3598. fc_host_post_vendor_event(shost, fc_get_event_number(),
  3599. sizeof(temp_event_data), (char *) &temp_event_data,
  3600. SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
  3601. }
  3602. /**
  3603. * lpfc_sli_setup: SLI ring setup function.
  3604. * @phba: Pointer to HBA context object.
  3605. *
  3606. * lpfc_sli_setup sets up rings of the SLI interface with
  3607. * number of iocbs per ring and iotags. This function is
  3608. * called while driver attach to the HBA and before the
  3609. * interrupts are enabled. So there is no need for locking.
  3610. *
  3611. * This function always returns 0.
  3612. **/
  3613. int
  3614. lpfc_sli_setup(struct lpfc_hba *phba)
  3615. {
  3616. int i, totiocbsize = 0;
  3617. struct lpfc_sli *psli = &phba->sli;
  3618. struct lpfc_sli_ring *pring;
  3619. psli->num_rings = MAX_CONFIGURED_RINGS;
  3620. psli->sli_flag = 0;
  3621. psli->fcp_ring = LPFC_FCP_RING;
  3622. psli->next_ring = LPFC_FCP_NEXT_RING;
  3623. psli->extra_ring = LPFC_EXTRA_RING;
  3624. psli->iocbq_lookup = NULL;
  3625. psli->iocbq_lookup_len = 0;
  3626. psli->last_iotag = 0;
  3627. for (i = 0; i < psli->num_rings; i++) {
  3628. pring = &psli->ring[i];
  3629. switch (i) {
  3630. case LPFC_FCP_RING: /* ring 0 - FCP */
  3631. /* numCiocb and numRiocb are used in config_port */
  3632. pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  3633. pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  3634. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  3635. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  3636. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  3637. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  3638. pring->sizeCiocb = (phba->sli_rev == 3) ?
  3639. SLI3_IOCB_CMD_SIZE :
  3640. SLI2_IOCB_CMD_SIZE;
  3641. pring->sizeRiocb = (phba->sli_rev == 3) ?
  3642. SLI3_IOCB_RSP_SIZE :
  3643. SLI2_IOCB_RSP_SIZE;
  3644. pring->iotag_ctr = 0;
  3645. pring->iotag_max =
  3646. (phba->cfg_hba_queue_depth * 2);
  3647. pring->fast_iotag = pring->iotag_max;
  3648. pring->num_mask = 0;
  3649. break;
  3650. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  3651. /* numCiocb and numRiocb are used in config_port */
  3652. pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  3653. pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  3654. pring->sizeCiocb = (phba->sli_rev == 3) ?
  3655. SLI3_IOCB_CMD_SIZE :
  3656. SLI2_IOCB_CMD_SIZE;
  3657. pring->sizeRiocb = (phba->sli_rev == 3) ?
  3658. SLI3_IOCB_RSP_SIZE :
  3659. SLI2_IOCB_RSP_SIZE;
  3660. pring->iotag_max = phba->cfg_hba_queue_depth;
  3661. pring->num_mask = 0;
  3662. break;
  3663. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  3664. /* numCiocb and numRiocb are used in config_port */
  3665. pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  3666. pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  3667. pring->sizeCiocb = (phba->sli_rev == 3) ?
  3668. SLI3_IOCB_CMD_SIZE :
  3669. SLI2_IOCB_CMD_SIZE;
  3670. pring->sizeRiocb = (phba->sli_rev == 3) ?
  3671. SLI3_IOCB_RSP_SIZE :
  3672. SLI2_IOCB_RSP_SIZE;
  3673. pring->fast_iotag = 0;
  3674. pring->iotag_ctr = 0;
  3675. pring->iotag_max = 4096;
  3676. pring->lpfc_sli_rcv_async_status =
  3677. lpfc_sli_async_event_handler;
  3678. pring->num_mask = 4;
  3679. pring->prt[0].profile = 0; /* Mask 0 */
  3680. pring->prt[0].rctl = FC_ELS_REQ;
  3681. pring->prt[0].type = FC_ELS_DATA;
  3682. pring->prt[0].lpfc_sli_rcv_unsol_event =
  3683. lpfc_els_unsol_event;
  3684. pring->prt[1].profile = 0; /* Mask 1 */
  3685. pring->prt[1].rctl = FC_ELS_RSP;
  3686. pring->prt[1].type = FC_ELS_DATA;
  3687. pring->prt[1].lpfc_sli_rcv_unsol_event =
  3688. lpfc_els_unsol_event;
  3689. pring->prt[2].profile = 0; /* Mask 2 */
  3690. /* NameServer Inquiry */
  3691. pring->prt[2].rctl = FC_UNSOL_CTL;
  3692. /* NameServer */
  3693. pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
  3694. pring->prt[2].lpfc_sli_rcv_unsol_event =
  3695. lpfc_ct_unsol_event;
  3696. pring->prt[3].profile = 0; /* Mask 3 */
  3697. /* NameServer response */
  3698. pring->prt[3].rctl = FC_SOL_CTL;
  3699. /* NameServer */
  3700. pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
  3701. pring->prt[3].lpfc_sli_rcv_unsol_event =
  3702. lpfc_ct_unsol_event;
  3703. break;
  3704. }
  3705. totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
  3706. (pring->numRiocb * pring->sizeRiocb);
  3707. }
  3708. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  3709. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  3710. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  3711. "SLI2 SLIM Data: x%x x%lx\n",
  3712. phba->brd_no, totiocbsize,
  3713. (unsigned long) MAX_SLIM_IOCB_SIZE);
  3714. }
  3715. if (phba->cfg_multi_ring_support == 2)
  3716. lpfc_extra_ring_setup(phba);
  3717. return 0;
  3718. }
  3719. /**
  3720. * lpfc_sli_queue_setup: Queue initialization function.
  3721. * @phba: Pointer to HBA context object.
  3722. *
  3723. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  3724. * ring. This function also initializes ring indices of each ring.
  3725. * This function is called during the initialization of the SLI
  3726. * interface of an HBA.
  3727. * This function is called with no lock held and always returns
  3728. * 1.
  3729. **/
  3730. int
  3731. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  3732. {
  3733. struct lpfc_sli *psli;
  3734. struct lpfc_sli_ring *pring;
  3735. int i;
  3736. psli = &phba->sli;
  3737. spin_lock_irq(&phba->hbalock);
  3738. INIT_LIST_HEAD(&psli->mboxq);
  3739. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  3740. /* Initialize list headers for txq and txcmplq as double linked lists */
  3741. for (i = 0; i < psli->num_rings; i++) {
  3742. pring = &psli->ring[i];
  3743. pring->ringno = i;
  3744. pring->next_cmdidx = 0;
  3745. pring->local_getidx = 0;
  3746. pring->cmdidx = 0;
  3747. INIT_LIST_HEAD(&pring->txq);
  3748. INIT_LIST_HEAD(&pring->txcmplq);
  3749. INIT_LIST_HEAD(&pring->iocb_continueq);
  3750. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  3751. INIT_LIST_HEAD(&pring->postbufq);
  3752. }
  3753. spin_unlock_irq(&phba->hbalock);
  3754. return 1;
  3755. }
  3756. /**
  3757. * lpfc_sli_host_down: Vport cleanup function.
  3758. * @vport: Pointer to virtual port object.
  3759. *
  3760. * lpfc_sli_host_down is called to clean up the resources
  3761. * associated with a vport before destroying virtual
  3762. * port data structures.
  3763. * This function does following operations:
  3764. * - Free discovery resources associated with this virtual
  3765. * port.
  3766. * - Free iocbs associated with this virtual port in
  3767. * the txq.
  3768. * - Send abort for all iocb commands associated with this
  3769. * vport in txcmplq.
  3770. *
  3771. * This function is called with no lock held and always returns 1.
  3772. **/
  3773. int
  3774. lpfc_sli_host_down(struct lpfc_vport *vport)
  3775. {
  3776. LIST_HEAD(completions);
  3777. struct lpfc_hba *phba = vport->phba;
  3778. struct lpfc_sli *psli = &phba->sli;
  3779. struct lpfc_sli_ring *pring;
  3780. struct lpfc_iocbq *iocb, *next_iocb;
  3781. int i;
  3782. unsigned long flags = 0;
  3783. uint16_t prev_pring_flag;
  3784. lpfc_cleanup_discovery_resources(vport);
  3785. spin_lock_irqsave(&phba->hbalock, flags);
  3786. for (i = 0; i < psli->num_rings; i++) {
  3787. pring = &psli->ring[i];
  3788. prev_pring_flag = pring->flag;
  3789. /* Only slow rings */
  3790. if (pring->ringno == LPFC_ELS_RING) {
  3791. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  3792. /* Set the lpfc data pending flag */
  3793. set_bit(LPFC_DATA_READY, &phba->data_flags);
  3794. }
  3795. /*
  3796. * Error everything on the txq since these iocbs have not been
  3797. * given to the FW yet.
  3798. */
  3799. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  3800. if (iocb->vport != vport)
  3801. continue;
  3802. list_move_tail(&iocb->list, &completions);
  3803. pring->txq_cnt--;
  3804. }
  3805. /* Next issue ABTS for everything on the txcmplq */
  3806. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  3807. list) {
  3808. if (iocb->vport != vport)
  3809. continue;
  3810. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3811. }
  3812. pring->flag = prev_pring_flag;
  3813. }
  3814. spin_unlock_irqrestore(&phba->hbalock, flags);
  3815. while (!list_empty(&completions)) {
  3816. list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
  3817. if (!iocb->iocb_cmpl)
  3818. lpfc_sli_release_iocbq(phba, iocb);
  3819. else {
  3820. iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  3821. iocb->iocb.un.ulpWord[4] = IOERR_SLI_DOWN;
  3822. (iocb->iocb_cmpl) (phba, iocb, iocb);
  3823. }
  3824. }
  3825. return 1;
  3826. }
  3827. /**
  3828. * lpfc_sli_hba_down: Resource cleanup function for the HBA.
  3829. * @phba: Pointer to HBA context object.
  3830. *
  3831. * This function cleans up all iocb, buffers, mailbox commands
  3832. * while shutting down the HBA. This function is called with no
  3833. * lock held and always returns 1.
  3834. * This function does the following to cleanup driver resources:
  3835. * - Free discovery resources for each virtual port
  3836. * - Cleanup any pending fabric iocbs
  3837. * - Iterate through the iocb txq and free each entry
  3838. * in the list.
  3839. * - Free up any buffer posted to the HBA
  3840. * - Free mailbox commands in the mailbox queue.
  3841. **/
  3842. int
  3843. lpfc_sli_hba_down(struct lpfc_hba *phba)
  3844. {
  3845. LIST_HEAD(completions);
  3846. struct lpfc_sli *psli = &phba->sli;
  3847. struct lpfc_sli_ring *pring;
  3848. struct lpfc_dmabuf *buf_ptr;
  3849. LPFC_MBOXQ_t *pmb;
  3850. struct lpfc_iocbq *iocb;
  3851. IOCB_t *cmd = NULL;
  3852. int i;
  3853. unsigned long flags = 0;
  3854. lpfc_hba_down_prep(phba);
  3855. lpfc_fabric_abort_hba(phba);
  3856. spin_lock_irqsave(&phba->hbalock, flags);
  3857. for (i = 0; i < psli->num_rings; i++) {
  3858. pring = &psli->ring[i];
  3859. /* Only slow rings */
  3860. if (pring->ringno == LPFC_ELS_RING) {
  3861. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  3862. /* Set the lpfc data pending flag */
  3863. set_bit(LPFC_DATA_READY, &phba->data_flags);
  3864. }
  3865. /*
  3866. * Error everything on the txq since these iocbs have not been
  3867. * given to the FW yet.
  3868. */
  3869. list_splice_init(&pring->txq, &completions);
  3870. pring->txq_cnt = 0;
  3871. }
  3872. spin_unlock_irqrestore(&phba->hbalock, flags);
  3873. while (!list_empty(&completions)) {
  3874. list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
  3875. cmd = &iocb->iocb;
  3876. if (!iocb->iocb_cmpl)
  3877. lpfc_sli_release_iocbq(phba, iocb);
  3878. else {
  3879. cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
  3880. cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
  3881. (iocb->iocb_cmpl) (phba, iocb, iocb);
  3882. }
  3883. }
  3884. spin_lock_irqsave(&phba->hbalock, flags);
  3885. list_splice_init(&phba->elsbuf, &completions);
  3886. phba->elsbuf_cnt = 0;
  3887. phba->elsbuf_prev_cnt = 0;
  3888. spin_unlock_irqrestore(&phba->hbalock, flags);
  3889. while (!list_empty(&completions)) {
  3890. list_remove_head(&completions, buf_ptr,
  3891. struct lpfc_dmabuf, list);
  3892. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  3893. kfree(buf_ptr);
  3894. }
  3895. /* Return any active mbox cmds */
  3896. del_timer_sync(&psli->mbox_tmo);
  3897. spin_lock_irqsave(&phba->hbalock, flags);
  3898. spin_lock(&phba->pport->work_port_lock);
  3899. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  3900. spin_unlock(&phba->pport->work_port_lock);
  3901. /* Return any pending or completed mbox cmds */
  3902. list_splice_init(&phba->sli.mboxq, &completions);
  3903. if (psli->mbox_active) {
  3904. list_add_tail(&psli->mbox_active->list, &completions);
  3905. psli->mbox_active = NULL;
  3906. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3907. }
  3908. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  3909. spin_unlock_irqrestore(&phba->hbalock, flags);
  3910. while (!list_empty(&completions)) {
  3911. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  3912. pmb->mb.mbxStatus = MBX_NOT_FINISHED;
  3913. if (pmb->mbox_cmpl)
  3914. pmb->mbox_cmpl(phba,pmb);
  3915. }
  3916. return 1;
  3917. }
  3918. /**
  3919. * lpfc_sli_pcimem_bcopy: SLI memory copy function.
  3920. * @srcp: Source memory pointer.
  3921. * @destp: Destination memory pointer.
  3922. * @cnt: Number of words required to be copied.
  3923. *
  3924. * This function is used for copying data between driver memory
  3925. * and the SLI memory. This function also changes the endianness
  3926. * of each word if native endianness is different from SLI
  3927. * endianness. This function can be called with or without
  3928. * lock.
  3929. **/
  3930. void
  3931. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  3932. {
  3933. uint32_t *src = srcp;
  3934. uint32_t *dest = destp;
  3935. uint32_t ldata;
  3936. int i;
  3937. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  3938. ldata = *src;
  3939. ldata = le32_to_cpu(ldata);
  3940. *dest = ldata;
  3941. src++;
  3942. dest++;
  3943. }
  3944. }
  3945. /**
  3946. * lpfc_sli_ringpostbuf_put: Function to add a buffer to postbufq.
  3947. * @phba: Pointer to HBA context object.
  3948. * @pring: Pointer to driver SLI ring object.
  3949. * @mp: Pointer to driver buffer object.
  3950. *
  3951. * This function is called with no lock held.
  3952. * It always return zero after adding the buffer to the postbufq
  3953. * buffer list.
  3954. **/
  3955. int
  3956. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  3957. struct lpfc_dmabuf *mp)
  3958. {
  3959. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  3960. later */
  3961. spin_lock_irq(&phba->hbalock);
  3962. list_add_tail(&mp->list, &pring->postbufq);
  3963. pring->postbufq_cnt++;
  3964. spin_unlock_irq(&phba->hbalock);
  3965. return 0;
  3966. }
  3967. /**
  3968. * lpfc_sli_get_buffer_tag: Tag allocation function for a buffer posted
  3969. * using CMD_QUE_XRI64_CX iocb.
  3970. * @phba: Pointer to HBA context object.
  3971. *
  3972. * When HBQ is enabled, buffers are searched based on tags. This function
  3973. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  3974. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  3975. * does not conflict with tags of buffer posted for unsolicited events.
  3976. * The function returns the allocated tag. The function is called with
  3977. * no locks held.
  3978. **/
  3979. uint32_t
  3980. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  3981. {
  3982. spin_lock_irq(&phba->hbalock);
  3983. phba->buffer_tag_count++;
  3984. /*
  3985. * Always set the QUE_BUFTAG_BIT to distiguish between
  3986. * a tag assigned by HBQ.
  3987. */
  3988. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  3989. spin_unlock_irq(&phba->hbalock);
  3990. return phba->buffer_tag_count;
  3991. }
  3992. /**
  3993. * lpfc_sli_ring_taggedbuf_get: Search HBQ buffer associated with
  3994. * posted using CMD_QUE_XRI64_CX iocb.
  3995. * @phba: Pointer to HBA context object.
  3996. * @pring: Pointer to driver SLI ring object.
  3997. * @tag: Buffer tag.
  3998. *
  3999. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  4000. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  4001. * iocb is posted to the response ring with the tag of the buffer.
  4002. * This function searches the pring->postbufq list using the tag
  4003. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  4004. * iocb. If the buffer is found then lpfc_dmabuf object of the
  4005. * buffer is returned to the caller else NULL is returned.
  4006. * This function is called with no lock held.
  4007. **/
  4008. struct lpfc_dmabuf *
  4009. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  4010. uint32_t tag)
  4011. {
  4012. struct lpfc_dmabuf *mp, *next_mp;
  4013. struct list_head *slp = &pring->postbufq;
  4014. /* Search postbufq, from the begining, looking for a match on tag */
  4015. spin_lock_irq(&phba->hbalock);
  4016. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  4017. if (mp->buffer_tag == tag) {
  4018. list_del_init(&mp->list);
  4019. pring->postbufq_cnt--;
  4020. spin_unlock_irq(&phba->hbalock);
  4021. return mp;
  4022. }
  4023. }
  4024. spin_unlock_irq(&phba->hbalock);
  4025. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4026. "0402 Cannot find virtual addr for buffer tag on "
  4027. "ring %d Data x%lx x%p x%p x%x\n",
  4028. pring->ringno, (unsigned long) tag,
  4029. slp->next, slp->prev, pring->postbufq_cnt);
  4030. return NULL;
  4031. }
  4032. /**
  4033. * lpfc_sli_ringpostbuf_get: SLI2 buffer search function for
  4034. * unsolicited ct and els events.
  4035. * @phba: Pointer to HBA context object.
  4036. * @pring: Pointer to driver SLI ring object.
  4037. * @phys: DMA address of the buffer.
  4038. *
  4039. * This function searches the buffer list using the dma_address
  4040. * of unsolicited event to find the driver's lpfc_dmabuf object
  4041. * corresponding to the dma_address. The function returns the
  4042. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  4043. * This function is called by the ct and els unsolicited event
  4044. * handlers to get the buffer associated with the unsolicited
  4045. * event.
  4046. *
  4047. * This function is called with no lock held.
  4048. **/
  4049. struct lpfc_dmabuf *
  4050. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  4051. dma_addr_t phys)
  4052. {
  4053. struct lpfc_dmabuf *mp, *next_mp;
  4054. struct list_head *slp = &pring->postbufq;
  4055. /* Search postbufq, from the begining, looking for a match on phys */
  4056. spin_lock_irq(&phba->hbalock);
  4057. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  4058. if (mp->phys == phys) {
  4059. list_del_init(&mp->list);
  4060. pring->postbufq_cnt--;
  4061. spin_unlock_irq(&phba->hbalock);
  4062. return mp;
  4063. }
  4064. }
  4065. spin_unlock_irq(&phba->hbalock);
  4066. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4067. "0410 Cannot find virtual addr for mapped buf on "
  4068. "ring %d Data x%llx x%p x%p x%x\n",
  4069. pring->ringno, (unsigned long long)phys,
  4070. slp->next, slp->prev, pring->postbufq_cnt);
  4071. return NULL;
  4072. }
  4073. /**
  4074. * lpfc_sli_abort_els_cmpl: Completion handler for the els abort iocbs.
  4075. * @phba: Pointer to HBA context object.
  4076. * @cmdiocb: Pointer to driver command iocb object.
  4077. * @rspiocb: Pointer to driver response iocb object.
  4078. *
  4079. * This function is the completion handler for the abort iocbs for
  4080. * ELS commands. This function is called from the ELS ring event
  4081. * handler with no lock held. This function frees memory resources
  4082. * associated with the abort iocb.
  4083. **/
  4084. static void
  4085. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  4086. struct lpfc_iocbq *rspiocb)
  4087. {
  4088. IOCB_t *irsp = &rspiocb->iocb;
  4089. uint16_t abort_iotag, abort_context;
  4090. struct lpfc_iocbq *abort_iocb;
  4091. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  4092. abort_iocb = NULL;
  4093. if (irsp->ulpStatus) {
  4094. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  4095. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  4096. spin_lock_irq(&phba->hbalock);
  4097. if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
  4098. abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
  4099. lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
  4100. "0327 Cannot abort els iocb %p "
  4101. "with tag %x context %x, abort status %x, "
  4102. "abort code %x\n",
  4103. abort_iocb, abort_iotag, abort_context,
  4104. irsp->ulpStatus, irsp->un.ulpWord[4]);
  4105. /*
  4106. * If the iocb is not found in Firmware queue the iocb
  4107. * might have completed already. Do not free it again.
  4108. */
  4109. if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
  4110. spin_unlock_irq(&phba->hbalock);
  4111. lpfc_sli_release_iocbq(phba, cmdiocb);
  4112. return;
  4113. }
  4114. /*
  4115. * make sure we have the right iocbq before taking it
  4116. * off the txcmplq and try to call completion routine.
  4117. */
  4118. if (!abort_iocb ||
  4119. abort_iocb->iocb.ulpContext != abort_context ||
  4120. (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
  4121. spin_unlock_irq(&phba->hbalock);
  4122. else {
  4123. list_del_init(&abort_iocb->list);
  4124. pring->txcmplq_cnt--;
  4125. spin_unlock_irq(&phba->hbalock);
  4126. /* Firmware could still be in progress of DMAing
  4127. * payload, so don't free data buffer till after
  4128. * a hbeat.
  4129. */
  4130. abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
  4131. abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  4132. abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  4133. abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
  4134. (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
  4135. }
  4136. }
  4137. lpfc_sli_release_iocbq(phba, cmdiocb);
  4138. return;
  4139. }
  4140. /**
  4141. * lpfc_ignore_els_cmpl: Completion handler for aborted ELS command.
  4142. * @phba: Pointer to HBA context object.
  4143. * @cmdiocb: Pointer to driver command iocb object.
  4144. * @rspiocb: Pointer to driver response iocb object.
  4145. *
  4146. * The function is called from SLI ring event handler with no
  4147. * lock held. This function is the completion handler for ELS commands
  4148. * which are aborted. The function frees memory resources used for
  4149. * the aborted ELS commands.
  4150. **/
  4151. static void
  4152. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  4153. struct lpfc_iocbq *rspiocb)
  4154. {
  4155. IOCB_t *irsp = &rspiocb->iocb;
  4156. /* ELS cmd tag <ulpIoTag> completes */
  4157. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  4158. "0139 Ignoring ELS cmd tag x%x completion Data: "
  4159. "x%x x%x x%x\n",
  4160. irsp->ulpIoTag, irsp->ulpStatus,
  4161. irsp->un.ulpWord[4], irsp->ulpTimeout);
  4162. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  4163. lpfc_ct_free_iocb(phba, cmdiocb);
  4164. else
  4165. lpfc_els_free_iocb(phba, cmdiocb);
  4166. return;
  4167. }
  4168. /**
  4169. * lpfc_sli_issue_abort_iotag: Abort function for a command iocb.
  4170. * @phba: Pointer to HBA context object.
  4171. * @pring: Pointer to driver SLI ring object.
  4172. * @cmdiocb: Pointer to driver command iocb object.
  4173. *
  4174. * This function issues an abort iocb for the provided command
  4175. * iocb. This function is called with hbalock held.
  4176. * The function returns 0 when it fails due to memory allocation
  4177. * failure or when the command iocb is an abort request.
  4178. **/
  4179. int
  4180. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  4181. struct lpfc_iocbq *cmdiocb)
  4182. {
  4183. struct lpfc_vport *vport = cmdiocb->vport;
  4184. struct lpfc_iocbq *abtsiocbp;
  4185. IOCB_t *icmd = NULL;
  4186. IOCB_t *iabt = NULL;
  4187. int retval = IOCB_ERROR;
  4188. /*
  4189. * There are certain command types we don't want to abort. And we
  4190. * don't want to abort commands that are already in the process of
  4191. * being aborted.
  4192. */
  4193. icmd = &cmdiocb->iocb;
  4194. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  4195. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  4196. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  4197. return 0;
  4198. /* If we're unloading, don't abort iocb on the ELS ring, but change the
  4199. * callback so that nothing happens when it finishes.
  4200. */
  4201. if ((vport->load_flag & FC_UNLOADING) &&
  4202. (pring->ringno == LPFC_ELS_RING)) {
  4203. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  4204. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  4205. else
  4206. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  4207. goto abort_iotag_exit;
  4208. }
  4209. /* issue ABTS for this IOCB based on iotag */
  4210. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  4211. if (abtsiocbp == NULL)
  4212. return 0;
  4213. /* This signals the response to set the correct status
  4214. * before calling the completion handler.
  4215. */
  4216. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  4217. iabt = &abtsiocbp->iocb;
  4218. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  4219. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  4220. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  4221. iabt->ulpLe = 1;
  4222. iabt->ulpClass = icmd->ulpClass;
  4223. if (phba->link_state >= LPFC_LINK_UP)
  4224. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  4225. else
  4226. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  4227. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  4228. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  4229. "0339 Abort xri x%x, original iotag x%x, "
  4230. "abort cmd iotag x%x\n",
  4231. iabt->un.acxri.abortContextTag,
  4232. iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
  4233. retval = __lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0);
  4234. if (retval)
  4235. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  4236. abort_iotag_exit:
  4237. /*
  4238. * Caller to this routine should check for IOCB_ERROR
  4239. * and handle it properly. This routine no longer removes
  4240. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  4241. */
  4242. return retval;
  4243. }
  4244. /**
  4245. * lpfc_sli_validate_fcp_iocb: Filtering function, used to find commands
  4246. * associated with a vport/SCSI target/lun.
  4247. * @iocbq: Pointer to driver iocb object.
  4248. * @vport: Pointer to driver virtual port object.
  4249. * @tgt_id: SCSI ID of the target.
  4250. * @lun_id: LUN ID of the scsi device.
  4251. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  4252. *
  4253. * This function acts as iocb filter for functions which abort or count
  4254. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  4255. * 0 if the filtering criteria is met for the given iocb and will return
  4256. * 1 if the filtering criteria is not met.
  4257. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  4258. * given iocb is for the SCSI device specified by vport, tgt_id and
  4259. * lun_id parameter.
  4260. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  4261. * given iocb is for the SCSI target specified by vport and tgt_id
  4262. * parameters.
  4263. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  4264. * given iocb is for the SCSI host associated with the given vport.
  4265. * This function is called with no locks held.
  4266. **/
  4267. static int
  4268. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  4269. uint16_t tgt_id, uint64_t lun_id,
  4270. lpfc_ctx_cmd ctx_cmd)
  4271. {
  4272. struct lpfc_scsi_buf *lpfc_cmd;
  4273. int rc = 1;
  4274. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  4275. return rc;
  4276. if (iocbq->vport != vport)
  4277. return rc;
  4278. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  4279. if (lpfc_cmd->pCmd == NULL)
  4280. return rc;
  4281. switch (ctx_cmd) {
  4282. case LPFC_CTX_LUN:
  4283. if ((lpfc_cmd->rdata->pnode) &&
  4284. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  4285. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  4286. rc = 0;
  4287. break;
  4288. case LPFC_CTX_TGT:
  4289. if ((lpfc_cmd->rdata->pnode) &&
  4290. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  4291. rc = 0;
  4292. break;
  4293. case LPFC_CTX_HOST:
  4294. rc = 0;
  4295. break;
  4296. default:
  4297. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  4298. __func__, ctx_cmd);
  4299. break;
  4300. }
  4301. return rc;
  4302. }
  4303. /**
  4304. * lpfc_sli_sum_iocb: Function to count the number of FCP iocbs pending.
  4305. * @vport: Pointer to virtual port.
  4306. * @tgt_id: SCSI ID of the target.
  4307. * @lun_id: LUN ID of the scsi device.
  4308. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  4309. *
  4310. * This function returns number of FCP commands pending for the vport.
  4311. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  4312. * commands pending on the vport associated with SCSI device specified
  4313. * by tgt_id and lun_id parameters.
  4314. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  4315. * commands pending on the vport associated with SCSI target specified
  4316. * by tgt_id parameter.
  4317. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  4318. * commands pending on the vport.
  4319. * This function returns the number of iocbs which satisfy the filter.
  4320. * This function is called without any lock held.
  4321. **/
  4322. int
  4323. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  4324. lpfc_ctx_cmd ctx_cmd)
  4325. {
  4326. struct lpfc_hba *phba = vport->phba;
  4327. struct lpfc_iocbq *iocbq;
  4328. int sum, i;
  4329. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  4330. iocbq = phba->sli.iocbq_lookup[i];
  4331. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  4332. ctx_cmd) == 0)
  4333. sum++;
  4334. }
  4335. return sum;
  4336. }
  4337. /**
  4338. * lpfc_sli_abort_fcp_cmpl: Completion handler function for an aborted
  4339. * FCP iocb.
  4340. * @phba: Pointer to HBA context object
  4341. * @cmdiocb: Pointer to command iocb object.
  4342. * @rspiocb: Pointer to response iocb object.
  4343. *
  4344. * This function is called when an aborted FCP iocb completes. This
  4345. * function is called by the ring event handler with no lock held.
  4346. * This function frees the iocb.
  4347. **/
  4348. void
  4349. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  4350. struct lpfc_iocbq *rspiocb)
  4351. {
  4352. lpfc_sli_release_iocbq(phba, cmdiocb);
  4353. return;
  4354. }
  4355. /**
  4356. * lpfc_sli_abort_iocb: This function issue abort for all SCSI commands
  4357. * pending on a SCSI host(vport)/target/lun.
  4358. * @vport: Pointer to virtual port.
  4359. * @pring: Pointer to driver SLI ring object.
  4360. * @tgt_id: SCSI ID of the target.
  4361. * @lun_id: LUN ID of the scsi device.
  4362. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  4363. *
  4364. * This function sends an abort command for every SCSI command
  4365. * associated with the given virtual port pending on the ring
  4366. * filtered by lpfc_sli_validate_fcp_iocb function.
  4367. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  4368. * FCP iocbs associated with lun specified by tgt_id and lun_id
  4369. * parameters
  4370. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  4371. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  4372. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  4373. * FCP iocbs associated with virtual port.
  4374. * This function returns number of iocbs it failed to abort.
  4375. * This function is called with no locks held.
  4376. **/
  4377. int
  4378. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  4379. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  4380. {
  4381. struct lpfc_hba *phba = vport->phba;
  4382. struct lpfc_iocbq *iocbq;
  4383. struct lpfc_iocbq *abtsiocb;
  4384. IOCB_t *cmd = NULL;
  4385. int errcnt = 0, ret_val = 0;
  4386. int i;
  4387. for (i = 1; i <= phba->sli.last_iotag; i++) {
  4388. iocbq = phba->sli.iocbq_lookup[i];
  4389. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  4390. abort_cmd) != 0)
  4391. continue;
  4392. /* issue ABTS for this IOCB based on iotag */
  4393. abtsiocb = lpfc_sli_get_iocbq(phba);
  4394. if (abtsiocb == NULL) {
  4395. errcnt++;
  4396. continue;
  4397. }
  4398. cmd = &iocbq->iocb;
  4399. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  4400. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  4401. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  4402. abtsiocb->iocb.ulpLe = 1;
  4403. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  4404. abtsiocb->vport = phba->pport;
  4405. if (lpfc_is_link_up(phba))
  4406. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  4407. else
  4408. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  4409. /* Setup callback routine and issue the command. */
  4410. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  4411. ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
  4412. if (ret_val == IOCB_ERROR) {
  4413. lpfc_sli_release_iocbq(phba, abtsiocb);
  4414. errcnt++;
  4415. continue;
  4416. }
  4417. }
  4418. return errcnt;
  4419. }
  4420. /**
  4421. * lpfc_sli_wake_iocb_wait: iocb completion handler for iocb issued using
  4422. * lpfc_sli_issue_iocb_wait.
  4423. * @phba: Pointer to HBA context object.
  4424. * @cmdiocbq: Pointer to command iocb.
  4425. * @rspiocbq: Pointer to response iocb.
  4426. *
  4427. * This function is the completion handler for iocbs issued using
  4428. * lpfc_sli_issue_iocb_wait function. This function is called by the
  4429. * ring event handler function without any lock held. This function
  4430. * can be called from both worker thread context and interrupt
  4431. * context. This function also can be called from other thread which
  4432. * cleans up the SLI layer objects.
  4433. * This function copy the contents of the response iocb to the
  4434. * response iocb memory object provided by the caller of
  4435. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  4436. * sleeps for the iocb completion.
  4437. **/
  4438. static void
  4439. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  4440. struct lpfc_iocbq *cmdiocbq,
  4441. struct lpfc_iocbq *rspiocbq)
  4442. {
  4443. wait_queue_head_t *pdone_q;
  4444. unsigned long iflags;
  4445. spin_lock_irqsave(&phba->hbalock, iflags);
  4446. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  4447. if (cmdiocbq->context2 && rspiocbq)
  4448. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  4449. &rspiocbq->iocb, sizeof(IOCB_t));
  4450. pdone_q = cmdiocbq->context_un.wait_queue;
  4451. if (pdone_q)
  4452. wake_up(pdone_q);
  4453. spin_unlock_irqrestore(&phba->hbalock, iflags);
  4454. return;
  4455. }
  4456. /**
  4457. * lpfc_sli_issue_iocb_wait: Synchronous function to issue iocb commands.
  4458. * @phba: Pointer to HBA context object..
  4459. * @pring: Pointer to sli ring.
  4460. * @piocb: Pointer to command iocb.
  4461. * @prspiocbq: Pointer to response iocb.
  4462. * @timeout: Timeout in number of seconds.
  4463. *
  4464. * This function issues the iocb to firmware and waits for the
  4465. * iocb to complete. If the iocb command is not
  4466. * completed within timeout seconds, it returns IOCB_TIMEDOUT.
  4467. * Caller should not free the iocb resources if this function
  4468. * returns IOCB_TIMEDOUT.
  4469. * The function waits for the iocb completion using an
  4470. * non-interruptible wait.
  4471. * This function will sleep while waiting for iocb completion.
  4472. * So, this function should not be called from any context which
  4473. * does not allow sleeping. Due to the same reason, this function
  4474. * cannot be called with interrupt disabled.
  4475. * This function assumes that the iocb completions occur while
  4476. * this function sleep. So, this function cannot be called from
  4477. * the thread which process iocb completion for this ring.
  4478. * This function clears the iocb_flag of the iocb object before
  4479. * issuing the iocb and the iocb completion handler sets this
  4480. * flag and wakes this thread when the iocb completes.
  4481. * The contents of the response iocb will be copied to prspiocbq
  4482. * by the completion handler when the command completes.
  4483. * This function returns IOCB_SUCCESS when success.
  4484. * This function is called with no lock held.
  4485. **/
  4486. int
  4487. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  4488. struct lpfc_sli_ring *pring,
  4489. struct lpfc_iocbq *piocb,
  4490. struct lpfc_iocbq *prspiocbq,
  4491. uint32_t timeout)
  4492. {
  4493. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  4494. long timeleft, timeout_req = 0;
  4495. int retval = IOCB_SUCCESS;
  4496. uint32_t creg_val;
  4497. /*
  4498. * If the caller has provided a response iocbq buffer, then context2
  4499. * is NULL or its an error.
  4500. */
  4501. if (prspiocbq) {
  4502. if (piocb->context2)
  4503. return IOCB_ERROR;
  4504. piocb->context2 = prspiocbq;
  4505. }
  4506. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  4507. piocb->context_un.wait_queue = &done_q;
  4508. piocb->iocb_flag &= ~LPFC_IO_WAKE;
  4509. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  4510. creg_val = readl(phba->HCregaddr);
  4511. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  4512. writel(creg_val, phba->HCregaddr);
  4513. readl(phba->HCregaddr); /* flush */
  4514. }
  4515. retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
  4516. if (retval == IOCB_SUCCESS) {
  4517. timeout_req = timeout * HZ;
  4518. timeleft = wait_event_timeout(done_q,
  4519. piocb->iocb_flag & LPFC_IO_WAKE,
  4520. timeout_req);
  4521. if (piocb->iocb_flag & LPFC_IO_WAKE) {
  4522. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4523. "0331 IOCB wake signaled\n");
  4524. } else if (timeleft == 0) {
  4525. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4526. "0338 IOCB wait timeout error - no "
  4527. "wake response Data x%x\n", timeout);
  4528. retval = IOCB_TIMEDOUT;
  4529. } else {
  4530. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4531. "0330 IOCB wake NOT set, "
  4532. "Data x%x x%lx\n",
  4533. timeout, (timeleft / jiffies));
  4534. retval = IOCB_TIMEDOUT;
  4535. }
  4536. } else {
  4537. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4538. "0332 IOCB wait issue failed, Data x%x\n",
  4539. retval);
  4540. retval = IOCB_ERROR;
  4541. }
  4542. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  4543. creg_val = readl(phba->HCregaddr);
  4544. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  4545. writel(creg_val, phba->HCregaddr);
  4546. readl(phba->HCregaddr); /* flush */
  4547. }
  4548. if (prspiocbq)
  4549. piocb->context2 = NULL;
  4550. piocb->context_un.wait_queue = NULL;
  4551. piocb->iocb_cmpl = NULL;
  4552. return retval;
  4553. }
  4554. /**
  4555. * lpfc_sli_issue_mbox_wait: Synchronous function to issue mailbox.
  4556. * @phba: Pointer to HBA context object.
  4557. * @pmboxq: Pointer to driver mailbox object.
  4558. * @timeout: Timeout in number of seconds.
  4559. *
  4560. * This function issues the mailbox to firmware and waits for the
  4561. * mailbox command to complete. If the mailbox command is not
  4562. * completed within timeout seconds, it returns MBX_TIMEOUT.
  4563. * The function waits for the mailbox completion using an
  4564. * interruptible wait. If the thread is woken up due to a
  4565. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  4566. * should not free the mailbox resources, if this function returns
  4567. * MBX_TIMEOUT.
  4568. * This function will sleep while waiting for mailbox completion.
  4569. * So, this function should not be called from any context which
  4570. * does not allow sleeping. Due to the same reason, this function
  4571. * cannot be called with interrupt disabled.
  4572. * This function assumes that the mailbox completion occurs while
  4573. * this function sleep. So, this function cannot be called from
  4574. * the worker thread which processes mailbox completion.
  4575. * This function is called in the context of HBA management
  4576. * applications.
  4577. * This function returns MBX_SUCCESS when successful.
  4578. * This function is called with no lock held.
  4579. **/
  4580. int
  4581. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  4582. uint32_t timeout)
  4583. {
  4584. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  4585. int retval;
  4586. unsigned long flag;
  4587. /* The caller must leave context1 empty. */
  4588. if (pmboxq->context1)
  4589. return MBX_NOT_FINISHED;
  4590. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  4591. /* setup wake call as IOCB callback */
  4592. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  4593. /* setup context field to pass wait_queue pointer to wake function */
  4594. pmboxq->context1 = &done_q;
  4595. /* now issue the command */
  4596. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  4597. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  4598. wait_event_interruptible_timeout(done_q,
  4599. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  4600. timeout * HZ);
  4601. spin_lock_irqsave(&phba->hbalock, flag);
  4602. pmboxq->context1 = NULL;
  4603. /*
  4604. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  4605. * else do not free the resources.
  4606. */
  4607. if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
  4608. retval = MBX_SUCCESS;
  4609. else {
  4610. retval = MBX_TIMEOUT;
  4611. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  4612. }
  4613. spin_unlock_irqrestore(&phba->hbalock, flag);
  4614. }
  4615. return retval;
  4616. }
  4617. /**
  4618. * lpfc_sli_flush_mbox_queue: mailbox queue cleanup function.
  4619. * @phba: Pointer to HBA context.
  4620. *
  4621. * This function is called to cleanup any pending mailbox
  4622. * objects in the driver queue before bringing the HBA offline.
  4623. * This function is called while resetting the HBA.
  4624. * The function is called without any lock held. The function
  4625. * takes hbalock to update SLI data structure.
  4626. * This function returns 1 when there is an active mailbox
  4627. * command pending else returns 0.
  4628. **/
  4629. int
  4630. lpfc_sli_flush_mbox_queue(struct lpfc_hba * phba)
  4631. {
  4632. struct lpfc_vport *vport = phba->pport;
  4633. int i = 0;
  4634. uint32_t ha_copy;
  4635. while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE && !vport->stopped) {
  4636. if (i++ > LPFC_MBOX_TMO * 1000)
  4637. return 1;
  4638. /*
  4639. * Call lpfc_sli_handle_mb_event only if a mailbox cmd
  4640. * did finish. This way we won't get the misleading
  4641. * "Stray Mailbox Interrupt" message.
  4642. */
  4643. spin_lock_irq(&phba->hbalock);
  4644. ha_copy = phba->work_ha;
  4645. phba->work_ha &= ~HA_MBATT;
  4646. spin_unlock_irq(&phba->hbalock);
  4647. if (ha_copy & HA_MBATT)
  4648. if (lpfc_sli_handle_mb_event(phba) == 0)
  4649. i = 0;
  4650. msleep(1);
  4651. }
  4652. return (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) ? 1 : 0;
  4653. }
  4654. /**
  4655. * lpfc_sli_check_eratt: check error attention events
  4656. * @phba: Pointer to HBA context.
  4657. *
  4658. * This function is called form timer soft interrupt context to check HBA's
  4659. * error attention register bit for error attention events.
  4660. *
  4661. * This fucntion returns 1 when there is Error Attention in the Host Attention
  4662. * Register and returns 0 otherwise.
  4663. **/
  4664. int
  4665. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  4666. {
  4667. uint32_t ha_copy;
  4668. /* If somebody is waiting to handle an eratt, don't process it
  4669. * here. The brdkill function will do this.
  4670. */
  4671. if (phba->link_flag & LS_IGNORE_ERATT)
  4672. return 0;
  4673. /* Check if interrupt handler handles this ERATT */
  4674. spin_lock_irq(&phba->hbalock);
  4675. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  4676. /* Interrupt handler has handled ERATT */
  4677. spin_unlock_irq(&phba->hbalock);
  4678. return 0;
  4679. }
  4680. /* Read chip Host Attention (HA) register */
  4681. ha_copy = readl(phba->HAregaddr);
  4682. if (ha_copy & HA_ERATT) {
  4683. /* Read host status register to retrieve error event */
  4684. lpfc_sli_read_hs(phba);
  4685. /* Set the driver HA work bitmap */
  4686. phba->work_ha |= HA_ERATT;
  4687. /* Indicate polling handles this ERATT */
  4688. phba->hba_flag |= HBA_ERATT_HANDLED;
  4689. spin_unlock_irq(&phba->hbalock);
  4690. return 1;
  4691. }
  4692. spin_unlock_irq(&phba->hbalock);
  4693. return 0;
  4694. }
  4695. /**
  4696. * lpfc_sp_intr_handler: The slow-path interrupt handler of lpfc driver.
  4697. * @irq: Interrupt number.
  4698. * @dev_id: The device context pointer.
  4699. *
  4700. * This function is directly called from the PCI layer as an interrupt
  4701. * service routine when the device is enabled with MSI-X multi-message
  4702. * interrupt mode and there are slow-path events in the HBA. However,
  4703. * when the device is enabled with either MSI or Pin-IRQ interrupt mode,
  4704. * this function is called as part of the device-level interrupt handler.
  4705. * When the PCI slot is in error recovery or the HBA is undergoing
  4706. * initialization, the interrupt handler will not process the interrupt.
  4707. * The link attention and ELS ring attention events are handled by the
  4708. * worker thread. The interrupt handler signals the worker thread and
  4709. * and returns for these events. This function is called without any
  4710. * lock held. It gets the hbalock to access and update SLI data
  4711. * structures.
  4712. *
  4713. * This function returns IRQ_HANDLED when interrupt is handled else it
  4714. * returns IRQ_NONE.
  4715. **/
  4716. irqreturn_t
  4717. lpfc_sp_intr_handler(int irq, void *dev_id)
  4718. {
  4719. struct lpfc_hba *phba;
  4720. uint32_t ha_copy;
  4721. uint32_t work_ha_copy;
  4722. unsigned long status;
  4723. uint32_t control;
  4724. MAILBOX_t *mbox, *pmbox;
  4725. struct lpfc_vport *vport;
  4726. struct lpfc_nodelist *ndlp;
  4727. struct lpfc_dmabuf *mp;
  4728. LPFC_MBOXQ_t *pmb;
  4729. int rc;
  4730. /*
  4731. * Get the driver's phba structure from the dev_id and
  4732. * assume the HBA is not interrupting.
  4733. */
  4734. phba = (struct lpfc_hba *)dev_id;
  4735. if (unlikely(!phba))
  4736. return IRQ_NONE;
  4737. /*
  4738. * Stuff needs to be attented to when this function is invoked as an
  4739. * individual interrupt handler in MSI-X multi-message interrupt mode
  4740. */
  4741. if (phba->intr_type == MSIX) {
  4742. /* If the pci channel is offline, ignore all the interrupts */
  4743. if (unlikely(pci_channel_offline(phba->pcidev)))
  4744. return IRQ_NONE;
  4745. /* Update device-level interrupt statistics */
  4746. phba->sli.slistat.sli_intr++;
  4747. /* Ignore all interrupts during initialization. */
  4748. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  4749. return IRQ_NONE;
  4750. /* Need to read HA REG for slow-path events */
  4751. spin_lock(&phba->hbalock);
  4752. ha_copy = readl(phba->HAregaddr);
  4753. /* If somebody is waiting to handle an eratt don't process it
  4754. * here. The brdkill function will do this.
  4755. */
  4756. if (phba->link_flag & LS_IGNORE_ERATT)
  4757. ha_copy &= ~HA_ERATT;
  4758. /* Check the need for handling ERATT in interrupt handler */
  4759. if (ha_copy & HA_ERATT) {
  4760. if (phba->hba_flag & HBA_ERATT_HANDLED)
  4761. /* ERATT polling has handled ERATT */
  4762. ha_copy &= ~HA_ERATT;
  4763. else
  4764. /* Indicate interrupt handler handles ERATT */
  4765. phba->hba_flag |= HBA_ERATT_HANDLED;
  4766. }
  4767. /* Clear up only attention source related to slow-path */
  4768. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  4769. phba->HAregaddr);
  4770. readl(phba->HAregaddr); /* flush */
  4771. spin_unlock(&phba->hbalock);
  4772. } else
  4773. ha_copy = phba->ha_copy;
  4774. work_ha_copy = ha_copy & phba->work_ha_mask;
  4775. if (work_ha_copy) {
  4776. if (work_ha_copy & HA_LATT) {
  4777. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  4778. /*
  4779. * Turn off Link Attention interrupts
  4780. * until CLEAR_LA done
  4781. */
  4782. spin_lock(&phba->hbalock);
  4783. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  4784. control = readl(phba->HCregaddr);
  4785. control &= ~HC_LAINT_ENA;
  4786. writel(control, phba->HCregaddr);
  4787. readl(phba->HCregaddr); /* flush */
  4788. spin_unlock(&phba->hbalock);
  4789. }
  4790. else
  4791. work_ha_copy &= ~HA_LATT;
  4792. }
  4793. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  4794. /*
  4795. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  4796. * the only slow ring.
  4797. */
  4798. status = (work_ha_copy &
  4799. (HA_RXMASK << (4*LPFC_ELS_RING)));
  4800. status >>= (4*LPFC_ELS_RING);
  4801. if (status & HA_RXMASK) {
  4802. spin_lock(&phba->hbalock);
  4803. control = readl(phba->HCregaddr);
  4804. lpfc_debugfs_slow_ring_trc(phba,
  4805. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  4806. control, status,
  4807. (uint32_t)phba->sli.slistat.sli_intr);
  4808. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  4809. lpfc_debugfs_slow_ring_trc(phba,
  4810. "ISR Disable ring:"
  4811. "pwork:x%x hawork:x%x wait:x%x",
  4812. phba->work_ha, work_ha_copy,
  4813. (uint32_t)((unsigned long)
  4814. &phba->work_waitq));
  4815. control &=
  4816. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  4817. writel(control, phba->HCregaddr);
  4818. readl(phba->HCregaddr); /* flush */
  4819. }
  4820. else {
  4821. lpfc_debugfs_slow_ring_trc(phba,
  4822. "ISR slow ring: pwork:"
  4823. "x%x hawork:x%x wait:x%x",
  4824. phba->work_ha, work_ha_copy,
  4825. (uint32_t)((unsigned long)
  4826. &phba->work_waitq));
  4827. }
  4828. spin_unlock(&phba->hbalock);
  4829. }
  4830. }
  4831. spin_lock(&phba->hbalock);
  4832. if (work_ha_copy & HA_ERATT)
  4833. lpfc_sli_read_hs(phba);
  4834. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  4835. pmb = phba->sli.mbox_active;
  4836. pmbox = &pmb->mb;
  4837. mbox = phba->mbox;
  4838. vport = pmb->vport;
  4839. /* First check out the status word */
  4840. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  4841. if (pmbox->mbxOwner != OWN_HOST) {
  4842. spin_unlock(&phba->hbalock);
  4843. /*
  4844. * Stray Mailbox Interrupt, mbxCommand <cmd>
  4845. * mbxStatus <status>
  4846. */
  4847. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  4848. LOG_SLI,
  4849. "(%d):0304 Stray Mailbox "
  4850. "Interrupt mbxCommand x%x "
  4851. "mbxStatus x%x\n",
  4852. (vport ? vport->vpi : 0),
  4853. pmbox->mbxCommand,
  4854. pmbox->mbxStatus);
  4855. /* clear mailbox attention bit */
  4856. work_ha_copy &= ~HA_MBATT;
  4857. } else {
  4858. phba->sli.mbox_active = NULL;
  4859. spin_unlock(&phba->hbalock);
  4860. phba->last_completion_time = jiffies;
  4861. del_timer(&phba->sli.mbox_tmo);
  4862. if (pmb->mbox_cmpl) {
  4863. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  4864. MAILBOX_CMD_SIZE);
  4865. }
  4866. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  4867. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  4868. lpfc_debugfs_disc_trc(vport,
  4869. LPFC_DISC_TRC_MBOX_VPORT,
  4870. "MBOX dflt rpi: : "
  4871. "status:x%x rpi:x%x",
  4872. (uint32_t)pmbox->mbxStatus,
  4873. pmbox->un.varWords[0], 0);
  4874. if (!pmbox->mbxStatus) {
  4875. mp = (struct lpfc_dmabuf *)
  4876. (pmb->context1);
  4877. ndlp = (struct lpfc_nodelist *)
  4878. pmb->context2;
  4879. /* Reg_LOGIN of dflt RPI was
  4880. * successful. new lets get
  4881. * rid of the RPI using the
  4882. * same mbox buffer.
  4883. */
  4884. lpfc_unreg_login(phba,
  4885. vport->vpi,
  4886. pmbox->un.varWords[0],
  4887. pmb);
  4888. pmb->mbox_cmpl =
  4889. lpfc_mbx_cmpl_dflt_rpi;
  4890. pmb->context1 = mp;
  4891. pmb->context2 = ndlp;
  4892. pmb->vport = vport;
  4893. rc = lpfc_sli_issue_mbox(phba,
  4894. pmb,
  4895. MBX_NOWAIT);
  4896. if (rc != MBX_BUSY)
  4897. lpfc_printf_log(phba,
  4898. KERN_ERR,
  4899. LOG_MBOX | LOG_SLI,
  4900. "0350 rc should have"
  4901. "been MBX_BUSY");
  4902. goto send_current_mbox;
  4903. }
  4904. }
  4905. spin_lock(&phba->pport->work_port_lock);
  4906. phba->pport->work_port_events &=
  4907. ~WORKER_MBOX_TMO;
  4908. spin_unlock(&phba->pport->work_port_lock);
  4909. lpfc_mbox_cmpl_put(phba, pmb);
  4910. }
  4911. } else
  4912. spin_unlock(&phba->hbalock);
  4913. if ((work_ha_copy & HA_MBATT) &&
  4914. (phba->sli.mbox_active == NULL)) {
  4915. send_current_mbox:
  4916. /* Process next mailbox command if there is one */
  4917. do {
  4918. rc = lpfc_sli_issue_mbox(phba, NULL,
  4919. MBX_NOWAIT);
  4920. } while (rc == MBX_NOT_FINISHED);
  4921. if (rc != MBX_SUCCESS)
  4922. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  4923. LOG_SLI, "0349 rc should be "
  4924. "MBX_SUCCESS");
  4925. }
  4926. spin_lock(&phba->hbalock);
  4927. phba->work_ha |= work_ha_copy;
  4928. spin_unlock(&phba->hbalock);
  4929. lpfc_worker_wake_up(phba);
  4930. }
  4931. return IRQ_HANDLED;
  4932. } /* lpfc_sp_intr_handler */
  4933. /**
  4934. * lpfc_fp_intr_handler: The fast-path interrupt handler of lpfc driver.
  4935. * @irq: Interrupt number.
  4936. * @dev_id: The device context pointer.
  4937. *
  4938. * This function is directly called from the PCI layer as an interrupt
  4939. * service routine when the device is enabled with MSI-X multi-message
  4940. * interrupt mode and there is a fast-path FCP IOCB ring event in the
  4941. * HBA. However, when the device is enabled with either MSI or Pin-IRQ
  4942. * interrupt mode, this function is called as part of the device-level
  4943. * interrupt handler. When the PCI slot is in error recovery or the HBA
  4944. * is undergoing initialization, the interrupt handler will not process
  4945. * the interrupt. The SCSI FCP fast-path ring event are handled in the
  4946. * intrrupt context. This function is called without any lock held. It
  4947. * gets the hbalock to access and update SLI data structures.
  4948. *
  4949. * This function returns IRQ_HANDLED when interrupt is handled else it
  4950. * returns IRQ_NONE.
  4951. **/
  4952. irqreturn_t
  4953. lpfc_fp_intr_handler(int irq, void *dev_id)
  4954. {
  4955. struct lpfc_hba *phba;
  4956. uint32_t ha_copy;
  4957. unsigned long status;
  4958. /* Get the driver's phba structure from the dev_id and
  4959. * assume the HBA is not interrupting.
  4960. */
  4961. phba = (struct lpfc_hba *) dev_id;
  4962. if (unlikely(!phba))
  4963. return IRQ_NONE;
  4964. /*
  4965. * Stuff needs to be attented to when this function is invoked as an
  4966. * individual interrupt handler in MSI-X multi-message interrupt mode
  4967. */
  4968. if (phba->intr_type == MSIX) {
  4969. /* If pci channel is offline, ignore all the interrupts */
  4970. if (unlikely(pci_channel_offline(phba->pcidev)))
  4971. return IRQ_NONE;
  4972. /* Update device-level interrupt statistics */
  4973. phba->sli.slistat.sli_intr++;
  4974. /* Ignore all interrupts during initialization. */
  4975. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  4976. return IRQ_NONE;
  4977. /* Need to read HA REG for FCP ring and other ring events */
  4978. ha_copy = readl(phba->HAregaddr);
  4979. /* Clear up only attention source related to fast-path */
  4980. spin_lock(&phba->hbalock);
  4981. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  4982. phba->HAregaddr);
  4983. readl(phba->HAregaddr); /* flush */
  4984. spin_unlock(&phba->hbalock);
  4985. } else
  4986. ha_copy = phba->ha_copy;
  4987. /*
  4988. * Process all events on FCP ring. Take the optimized path for FCP IO.
  4989. */
  4990. ha_copy &= ~(phba->work_ha_mask);
  4991. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  4992. status >>= (4*LPFC_FCP_RING);
  4993. if (status & HA_RXMASK)
  4994. lpfc_sli_handle_fast_ring_event(phba,
  4995. &phba->sli.ring[LPFC_FCP_RING],
  4996. status);
  4997. if (phba->cfg_multi_ring_support == 2) {
  4998. /*
  4999. * Process all events on extra ring. Take the optimized path
  5000. * for extra ring IO.
  5001. */
  5002. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  5003. status >>= (4*LPFC_EXTRA_RING);
  5004. if (status & HA_RXMASK) {
  5005. lpfc_sli_handle_fast_ring_event(phba,
  5006. &phba->sli.ring[LPFC_EXTRA_RING],
  5007. status);
  5008. }
  5009. }
  5010. return IRQ_HANDLED;
  5011. } /* lpfc_fp_intr_handler */
  5012. /**
  5013. * lpfc_intr_handler: The device-level interrupt handler of lpfc driver.
  5014. * @irq: Interrupt number.
  5015. * @dev_id: The device context pointer.
  5016. *
  5017. * This function is the device-level interrupt handler called from the PCI
  5018. * layer when either MSI or Pin-IRQ interrupt mode is enabled and there is
  5019. * an event in the HBA which requires driver attention. This function
  5020. * invokes the slow-path interrupt attention handling function and fast-path
  5021. * interrupt attention handling function in turn to process the relevant
  5022. * HBA attention events. This function is called without any lock held. It
  5023. * gets the hbalock to access and update SLI data structures.
  5024. *
  5025. * This function returns IRQ_HANDLED when interrupt is handled, else it
  5026. * returns IRQ_NONE.
  5027. **/
  5028. irqreturn_t
  5029. lpfc_intr_handler(int irq, void *dev_id)
  5030. {
  5031. struct lpfc_hba *phba;
  5032. irqreturn_t sp_irq_rc, fp_irq_rc;
  5033. unsigned long status1, status2;
  5034. /*
  5035. * Get the driver's phba structure from the dev_id and
  5036. * assume the HBA is not interrupting.
  5037. */
  5038. phba = (struct lpfc_hba *) dev_id;
  5039. if (unlikely(!phba))
  5040. return IRQ_NONE;
  5041. /* If the pci channel is offline, ignore all the interrupts. */
  5042. if (unlikely(pci_channel_offline(phba->pcidev)))
  5043. return IRQ_NONE;
  5044. /* Update device level interrupt statistics */
  5045. phba->sli.slistat.sli_intr++;
  5046. /* Ignore all interrupts during initialization. */
  5047. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  5048. return IRQ_NONE;
  5049. spin_lock(&phba->hbalock);
  5050. phba->ha_copy = readl(phba->HAregaddr);
  5051. if (unlikely(!phba->ha_copy)) {
  5052. spin_unlock(&phba->hbalock);
  5053. return IRQ_NONE;
  5054. } else if (phba->ha_copy & HA_ERATT) {
  5055. if (phba->hba_flag & HBA_ERATT_HANDLED)
  5056. /* ERATT polling has handled ERATT */
  5057. phba->ha_copy &= ~HA_ERATT;
  5058. else
  5059. /* Indicate interrupt handler handles ERATT */
  5060. phba->hba_flag |= HBA_ERATT_HANDLED;
  5061. }
  5062. /* Clear attention sources except link and error attentions */
  5063. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  5064. readl(phba->HAregaddr); /* flush */
  5065. spin_unlock(&phba->hbalock);
  5066. /*
  5067. * Invokes slow-path host attention interrupt handling as appropriate.
  5068. */
  5069. /* status of events with mailbox and link attention */
  5070. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  5071. /* status of events with ELS ring */
  5072. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  5073. status2 >>= (4*LPFC_ELS_RING);
  5074. if (status1 || (status2 & HA_RXMASK))
  5075. sp_irq_rc = lpfc_sp_intr_handler(irq, dev_id);
  5076. else
  5077. sp_irq_rc = IRQ_NONE;
  5078. /*
  5079. * Invoke fast-path host attention interrupt handling as appropriate.
  5080. */
  5081. /* status of events with FCP ring */
  5082. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  5083. status1 >>= (4*LPFC_FCP_RING);
  5084. /* status of events with extra ring */
  5085. if (phba->cfg_multi_ring_support == 2) {
  5086. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  5087. status2 >>= (4*LPFC_EXTRA_RING);
  5088. } else
  5089. status2 = 0;
  5090. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  5091. fp_irq_rc = lpfc_fp_intr_handler(irq, dev_id);
  5092. else
  5093. fp_irq_rc = IRQ_NONE;
  5094. /* Return device-level interrupt handling status */
  5095. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  5096. } /* lpfc_intr_handler */