lpfc_sli.c 165 KB

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