pmcraid.c 167 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076
  1. /*
  2. * pmcraid.c -- driver for PMC Sierra MaxRAID controller adapters
  3. *
  4. * Written By: Anil Ravindranath<anil_ravindranath@pmc-sierra.com>
  5. * PMC-Sierra Inc
  6. *
  7. * Copyright (C) 2008, 2009 PMC Sierra Inc
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
  22. * USA
  23. *
  24. */
  25. #include <linux/fs.h>
  26. #include <linux/init.h>
  27. #include <linux/types.h>
  28. #include <linux/errno.h>
  29. #include <linux/kernel.h>
  30. #include <linux/ioport.h>
  31. #include <linux/delay.h>
  32. #include <linux/pci.h>
  33. #include <linux/wait.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/sched.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/blkdev.h>
  38. #include <linux/firmware.h>
  39. #include <linux/module.h>
  40. #include <linux/moduleparam.h>
  41. #include <linux/hdreg.h>
  42. #include <linux/io.h>
  43. #include <linux/slab.h>
  44. #include <asm/irq.h>
  45. #include <asm/processor.h>
  46. #include <linux/libata.h>
  47. #include <linux/mutex.h>
  48. #include <scsi/scsi.h>
  49. #include <scsi/scsi_host.h>
  50. #include <scsi/scsi_device.h>
  51. #include <scsi/scsi_tcq.h>
  52. #include <scsi/scsi_eh.h>
  53. #include <scsi/scsi_cmnd.h>
  54. #include <scsi/scsicam.h>
  55. #include "pmcraid.h"
  56. /*
  57. * Module configuration parameters
  58. */
  59. static unsigned int pmcraid_debug_log;
  60. static unsigned int pmcraid_disable_aen;
  61. static unsigned int pmcraid_log_level = IOASC_LOG_LEVEL_MUST;
  62. static unsigned int pmcraid_enable_msix;
  63. /*
  64. * Data structures to support multiple adapters by the LLD.
  65. * pmcraid_adapter_count - count of configured adapters
  66. */
  67. static atomic_t pmcraid_adapter_count = ATOMIC_INIT(0);
  68. /*
  69. * Supporting user-level control interface through IOCTL commands.
  70. * pmcraid_major - major number to use
  71. * pmcraid_minor - minor number(s) to use
  72. */
  73. static unsigned int pmcraid_major;
  74. static struct class *pmcraid_class;
  75. DECLARE_BITMAP(pmcraid_minor, PMCRAID_MAX_ADAPTERS);
  76. /*
  77. * Module parameters
  78. */
  79. MODULE_AUTHOR("Anil Ravindranath<anil_ravindranath@pmc-sierra.com>");
  80. MODULE_DESCRIPTION("PMC Sierra MaxRAID Controller Driver");
  81. MODULE_LICENSE("GPL");
  82. MODULE_VERSION(PMCRAID_DRIVER_VERSION);
  83. module_param_named(log_level, pmcraid_log_level, uint, (S_IRUGO | S_IWUSR));
  84. MODULE_PARM_DESC(log_level,
  85. "Enables firmware error code logging, default :1 high-severity"
  86. " errors, 2: all errors including high-severity errors,"
  87. " 0: disables logging");
  88. module_param_named(debug, pmcraid_debug_log, uint, (S_IRUGO | S_IWUSR));
  89. MODULE_PARM_DESC(debug,
  90. "Enable driver verbose message logging. Set 1 to enable."
  91. "(default: 0)");
  92. module_param_named(disable_aen, pmcraid_disable_aen, uint, (S_IRUGO | S_IWUSR));
  93. MODULE_PARM_DESC(disable_aen,
  94. "Disable driver aen notifications to apps. Set 1 to disable."
  95. "(default: 0)");
  96. /* chip specific constants for PMC MaxRAID controllers (same for
  97. * 0x5220 and 0x8010
  98. */
  99. static struct pmcraid_chip_details pmcraid_chip_cfg[] = {
  100. {
  101. .ioastatus = 0x0,
  102. .ioarrin = 0x00040,
  103. .mailbox = 0x7FC30,
  104. .global_intr_mask = 0x00034,
  105. .ioa_host_intr = 0x0009C,
  106. .ioa_host_intr_clr = 0x000A0,
  107. .ioa_host_msix_intr = 0x7FC40,
  108. .ioa_host_mask = 0x7FC28,
  109. .ioa_host_mask_clr = 0x7FC28,
  110. .host_ioa_intr = 0x00020,
  111. .host_ioa_intr_clr = 0x00020,
  112. .transop_timeout = 300
  113. }
  114. };
  115. /*
  116. * PCI device ids supported by pmcraid driver
  117. */
  118. static struct pci_device_id pmcraid_pci_table[] = {
  119. { PCI_DEVICE(PCI_VENDOR_ID_PMC, PCI_DEVICE_ID_PMC_MAXRAID),
  120. 0, 0, (kernel_ulong_t)&pmcraid_chip_cfg[0]
  121. },
  122. {}
  123. };
  124. MODULE_DEVICE_TABLE(pci, pmcraid_pci_table);
  125. /**
  126. * pmcraid_slave_alloc - Prepare for commands to a device
  127. * @scsi_dev: scsi device struct
  128. *
  129. * This function is called by mid-layer prior to sending any command to the new
  130. * device. Stores resource entry details of the device in scsi_device struct.
  131. * Queuecommand uses the resource handle and other details to fill up IOARCB
  132. * while sending commands to the device.
  133. *
  134. * Return value:
  135. * 0 on success / -ENXIO if device does not exist
  136. */
  137. static int pmcraid_slave_alloc(struct scsi_device *scsi_dev)
  138. {
  139. struct pmcraid_resource_entry *temp, *res = NULL;
  140. struct pmcraid_instance *pinstance;
  141. u8 target, bus, lun;
  142. unsigned long lock_flags;
  143. int rc = -ENXIO;
  144. u16 fw_version;
  145. pinstance = shost_priv(scsi_dev->host);
  146. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  147. /* Driver exposes VSET and GSCSI resources only; all other device types
  148. * are not exposed. Resource list is synchronized using resource lock
  149. * so any traversal or modifications to the list should be done inside
  150. * this lock
  151. */
  152. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  153. list_for_each_entry(temp, &pinstance->used_res_q, queue) {
  154. /* do not expose VSETs with order-ids > MAX_VSET_TARGETS */
  155. if (RES_IS_VSET(temp->cfg_entry)) {
  156. if (fw_version <= PMCRAID_FW_VERSION_1)
  157. target = temp->cfg_entry.unique_flags1;
  158. else
  159. target = temp->cfg_entry.array_id & 0xFF;
  160. if (target > PMCRAID_MAX_VSET_TARGETS)
  161. continue;
  162. bus = PMCRAID_VSET_BUS_ID;
  163. lun = 0;
  164. } else if (RES_IS_GSCSI(temp->cfg_entry)) {
  165. target = RES_TARGET(temp->cfg_entry.resource_address);
  166. bus = PMCRAID_PHYS_BUS_ID;
  167. lun = RES_LUN(temp->cfg_entry.resource_address);
  168. } else {
  169. continue;
  170. }
  171. if (bus == scsi_dev->channel &&
  172. target == scsi_dev->id &&
  173. lun == scsi_dev->lun) {
  174. res = temp;
  175. break;
  176. }
  177. }
  178. if (res) {
  179. res->scsi_dev = scsi_dev;
  180. scsi_dev->hostdata = res;
  181. res->change_detected = 0;
  182. atomic_set(&res->read_failures, 0);
  183. atomic_set(&res->write_failures, 0);
  184. rc = 0;
  185. }
  186. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  187. return rc;
  188. }
  189. /**
  190. * pmcraid_slave_configure - Configures a SCSI device
  191. * @scsi_dev: scsi device struct
  192. *
  193. * This function is executed by SCSI mid layer just after a device is first
  194. * scanned (i.e. it has responded to an INQUIRY). For VSET resources, the
  195. * timeout value (default 30s) will be over-written to a higher value (60s)
  196. * and max_sectors value will be over-written to 512. It also sets queue depth
  197. * to host->cmd_per_lun value
  198. *
  199. * Return value:
  200. * 0 on success
  201. */
  202. static int pmcraid_slave_configure(struct scsi_device *scsi_dev)
  203. {
  204. struct pmcraid_resource_entry *res = scsi_dev->hostdata;
  205. if (!res)
  206. return 0;
  207. /* LLD exposes VSETs and Enclosure devices only */
  208. if (RES_IS_GSCSI(res->cfg_entry) &&
  209. scsi_dev->type != TYPE_ENCLOSURE)
  210. return -ENXIO;
  211. pmcraid_info("configuring %x:%x:%x:%x\n",
  212. scsi_dev->host->unique_id,
  213. scsi_dev->channel,
  214. scsi_dev->id,
  215. (u8)scsi_dev->lun);
  216. if (RES_IS_GSCSI(res->cfg_entry)) {
  217. scsi_dev->allow_restart = 1;
  218. } else if (RES_IS_VSET(res->cfg_entry)) {
  219. scsi_dev->allow_restart = 1;
  220. blk_queue_rq_timeout(scsi_dev->request_queue,
  221. PMCRAID_VSET_IO_TIMEOUT);
  222. blk_queue_max_hw_sectors(scsi_dev->request_queue,
  223. PMCRAID_VSET_MAX_SECTORS);
  224. }
  225. /*
  226. * We never want to report TCQ support for these types of devices.
  227. */
  228. if (!RES_IS_GSCSI(res->cfg_entry) && !RES_IS_VSET(res->cfg_entry))
  229. scsi_dev->tagged_supported = 0;
  230. return 0;
  231. }
  232. /**
  233. * pmcraid_slave_destroy - Unconfigure a SCSI device before removing it
  234. *
  235. * @scsi_dev: scsi device struct
  236. *
  237. * This is called by mid-layer before removing a device. Pointer assignments
  238. * done in pmcraid_slave_alloc will be reset to NULL here.
  239. *
  240. * Return value
  241. * none
  242. */
  243. static void pmcraid_slave_destroy(struct scsi_device *scsi_dev)
  244. {
  245. struct pmcraid_resource_entry *res;
  246. res = (struct pmcraid_resource_entry *)scsi_dev->hostdata;
  247. if (res)
  248. res->scsi_dev = NULL;
  249. scsi_dev->hostdata = NULL;
  250. }
  251. /**
  252. * pmcraid_change_queue_depth - Change the device's queue depth
  253. * @scsi_dev: scsi device struct
  254. * @depth: depth to set
  255. * @reason: calling context
  256. *
  257. * Return value
  258. * actual depth set
  259. */
  260. static int pmcraid_change_queue_depth(struct scsi_device *scsi_dev, int depth,
  261. int reason)
  262. {
  263. if (reason != SCSI_QDEPTH_DEFAULT)
  264. return -EOPNOTSUPP;
  265. if (depth > PMCRAID_MAX_CMD_PER_LUN)
  266. depth = PMCRAID_MAX_CMD_PER_LUN;
  267. scsi_adjust_queue_depth(scsi_dev, depth);
  268. return scsi_dev->queue_depth;
  269. }
  270. /**
  271. * pmcraid_init_cmdblk - initializes a command block
  272. *
  273. * @cmd: pointer to struct pmcraid_cmd to be initialized
  274. * @index: if >=0 first time initialization; otherwise reinitialization
  275. *
  276. * Return Value
  277. * None
  278. */
  279. void pmcraid_init_cmdblk(struct pmcraid_cmd *cmd, int index)
  280. {
  281. struct pmcraid_ioarcb *ioarcb = &(cmd->ioa_cb->ioarcb);
  282. dma_addr_t dma_addr = cmd->ioa_cb_bus_addr;
  283. if (index >= 0) {
  284. /* first time initialization (called from probe) */
  285. u32 ioasa_offset =
  286. offsetof(struct pmcraid_control_block, ioasa);
  287. cmd->index = index;
  288. ioarcb->response_handle = cpu_to_le32(index << 2);
  289. ioarcb->ioarcb_bus_addr = cpu_to_le64(dma_addr);
  290. ioarcb->ioasa_bus_addr = cpu_to_le64(dma_addr + ioasa_offset);
  291. ioarcb->ioasa_len = cpu_to_le16(sizeof(struct pmcraid_ioasa));
  292. } else {
  293. /* re-initialization of various lengths, called once command is
  294. * processed by IOA
  295. */
  296. memset(&cmd->ioa_cb->ioarcb.cdb, 0, PMCRAID_MAX_CDB_LEN);
  297. ioarcb->hrrq_id = 0;
  298. ioarcb->request_flags0 = 0;
  299. ioarcb->request_flags1 = 0;
  300. ioarcb->cmd_timeout = 0;
  301. ioarcb->ioarcb_bus_addr &= (~0x1FULL);
  302. ioarcb->ioadl_bus_addr = 0;
  303. ioarcb->ioadl_length = 0;
  304. ioarcb->data_transfer_length = 0;
  305. ioarcb->add_cmd_param_length = 0;
  306. ioarcb->add_cmd_param_offset = 0;
  307. cmd->ioa_cb->ioasa.ioasc = 0;
  308. cmd->ioa_cb->ioasa.residual_data_length = 0;
  309. cmd->time_left = 0;
  310. }
  311. cmd->cmd_done = NULL;
  312. cmd->scsi_cmd = NULL;
  313. cmd->release = 0;
  314. cmd->completion_req = 0;
  315. cmd->sense_buffer = 0;
  316. cmd->sense_buffer_dma = 0;
  317. cmd->dma_handle = 0;
  318. init_timer(&cmd->timer);
  319. }
  320. /**
  321. * pmcraid_reinit_cmdblk - reinitialize a command block
  322. *
  323. * @cmd: pointer to struct pmcraid_cmd to be reinitialized
  324. *
  325. * Return Value
  326. * None
  327. */
  328. static void pmcraid_reinit_cmdblk(struct pmcraid_cmd *cmd)
  329. {
  330. pmcraid_init_cmdblk(cmd, -1);
  331. }
  332. /**
  333. * pmcraid_get_free_cmd - get a free cmd block from command block pool
  334. * @pinstance: adapter instance structure
  335. *
  336. * Return Value:
  337. * returns pointer to cmd block or NULL if no blocks are available
  338. */
  339. static struct pmcraid_cmd *pmcraid_get_free_cmd(
  340. struct pmcraid_instance *pinstance
  341. )
  342. {
  343. struct pmcraid_cmd *cmd = NULL;
  344. unsigned long lock_flags;
  345. /* free cmd block list is protected by free_pool_lock */
  346. spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
  347. if (!list_empty(&pinstance->free_cmd_pool)) {
  348. cmd = list_entry(pinstance->free_cmd_pool.next,
  349. struct pmcraid_cmd, free_list);
  350. list_del(&cmd->free_list);
  351. }
  352. spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
  353. /* Initialize the command block before giving it the caller */
  354. if (cmd != NULL)
  355. pmcraid_reinit_cmdblk(cmd);
  356. return cmd;
  357. }
  358. /**
  359. * pmcraid_return_cmd - return a completed command block back into free pool
  360. * @cmd: pointer to the command block
  361. *
  362. * Return Value:
  363. * nothing
  364. */
  365. void pmcraid_return_cmd(struct pmcraid_cmd *cmd)
  366. {
  367. struct pmcraid_instance *pinstance = cmd->drv_inst;
  368. unsigned long lock_flags;
  369. spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
  370. list_add_tail(&cmd->free_list, &pinstance->free_cmd_pool);
  371. spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
  372. }
  373. /**
  374. * pmcraid_read_interrupts - reads IOA interrupts
  375. *
  376. * @pinstance: pointer to adapter instance structure
  377. *
  378. * Return value
  379. * interrupts read from IOA
  380. */
  381. static u32 pmcraid_read_interrupts(struct pmcraid_instance *pinstance)
  382. {
  383. return (pinstance->interrupt_mode) ?
  384. ioread32(pinstance->int_regs.ioa_host_msix_interrupt_reg) :
  385. ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  386. }
  387. /**
  388. * pmcraid_disable_interrupts - Masks and clears all specified interrupts
  389. *
  390. * @pinstance: pointer to per adapter instance structure
  391. * @intrs: interrupts to disable
  392. *
  393. * Return Value
  394. * None
  395. */
  396. static void pmcraid_disable_interrupts(
  397. struct pmcraid_instance *pinstance,
  398. u32 intrs
  399. )
  400. {
  401. u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
  402. u32 nmask = gmask | GLOBAL_INTERRUPT_MASK;
  403. iowrite32(intrs, pinstance->int_regs.ioa_host_interrupt_clr_reg);
  404. iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
  405. ioread32(pinstance->int_regs.global_interrupt_mask_reg);
  406. if (!pinstance->interrupt_mode) {
  407. iowrite32(intrs,
  408. pinstance->int_regs.ioa_host_interrupt_mask_reg);
  409. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  410. }
  411. }
  412. /**
  413. * pmcraid_enable_interrupts - Enables specified interrupts
  414. *
  415. * @pinstance: pointer to per adapter instance structure
  416. * @intr: interrupts to enable
  417. *
  418. * Return Value
  419. * None
  420. */
  421. static void pmcraid_enable_interrupts(
  422. struct pmcraid_instance *pinstance,
  423. u32 intrs
  424. )
  425. {
  426. u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
  427. u32 nmask = gmask & (~GLOBAL_INTERRUPT_MASK);
  428. iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
  429. if (!pinstance->interrupt_mode) {
  430. iowrite32(~intrs,
  431. pinstance->int_regs.ioa_host_interrupt_mask_reg);
  432. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  433. }
  434. pmcraid_info("enabled interrupts global mask = %x intr_mask = %x\n",
  435. ioread32(pinstance->int_regs.global_interrupt_mask_reg),
  436. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg));
  437. }
  438. /**
  439. * pmcraid_clr_trans_op - clear trans to op interrupt
  440. *
  441. * @pinstance: pointer to per adapter instance structure
  442. *
  443. * Return Value
  444. * None
  445. */
  446. static void pmcraid_clr_trans_op(
  447. struct pmcraid_instance *pinstance
  448. )
  449. {
  450. unsigned long lock_flags;
  451. if (!pinstance->interrupt_mode) {
  452. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  453. pinstance->int_regs.ioa_host_interrupt_mask_reg);
  454. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  455. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  456. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  457. ioread32(pinstance->int_regs.ioa_host_interrupt_clr_reg);
  458. }
  459. if (pinstance->reset_cmd != NULL) {
  460. del_timer(&pinstance->reset_cmd->timer);
  461. spin_lock_irqsave(
  462. pinstance->host->host_lock, lock_flags);
  463. pinstance->reset_cmd->cmd_done(pinstance->reset_cmd);
  464. spin_unlock_irqrestore(
  465. pinstance->host->host_lock, lock_flags);
  466. }
  467. }
  468. /**
  469. * pmcraid_reset_type - Determine the required reset type
  470. * @pinstance: pointer to adapter instance structure
  471. *
  472. * IOA requires hard reset if any of the following conditions is true.
  473. * 1. If HRRQ valid interrupt is not masked
  474. * 2. IOA reset alert doorbell is set
  475. * 3. If there are any error interrupts
  476. */
  477. static void pmcraid_reset_type(struct pmcraid_instance *pinstance)
  478. {
  479. u32 mask;
  480. u32 intrs;
  481. u32 alerts;
  482. mask = ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  483. intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  484. alerts = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  485. if ((mask & INTRS_HRRQ_VALID) == 0 ||
  486. (alerts & DOORBELL_IOA_RESET_ALERT) ||
  487. (intrs & PMCRAID_ERROR_INTERRUPTS)) {
  488. pmcraid_info("IOA requires hard reset\n");
  489. pinstance->ioa_hard_reset = 1;
  490. }
  491. /* If unit check is active, trigger the dump */
  492. if (intrs & INTRS_IOA_UNIT_CHECK)
  493. pinstance->ioa_unit_check = 1;
  494. }
  495. /**
  496. * pmcraid_bist_done - completion function for PCI BIST
  497. * @cmd: pointer to reset command
  498. * Return Value
  499. * none
  500. */
  501. static void pmcraid_ioa_reset(struct pmcraid_cmd *);
  502. static void pmcraid_bist_done(struct pmcraid_cmd *cmd)
  503. {
  504. struct pmcraid_instance *pinstance = cmd->drv_inst;
  505. unsigned long lock_flags;
  506. int rc;
  507. u16 pci_reg;
  508. rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
  509. /* If PCI config space can't be accessed wait for another two secs */
  510. if ((rc != PCIBIOS_SUCCESSFUL || (!(pci_reg & PCI_COMMAND_MEMORY))) &&
  511. cmd->time_left > 0) {
  512. pmcraid_info("BIST not complete, waiting another 2 secs\n");
  513. cmd->timer.expires = jiffies + cmd->time_left;
  514. cmd->time_left = 0;
  515. cmd->timer.data = (unsigned long)cmd;
  516. cmd->timer.function =
  517. (void (*)(unsigned long))pmcraid_bist_done;
  518. add_timer(&cmd->timer);
  519. } else {
  520. cmd->time_left = 0;
  521. pmcraid_info("BIST is complete, proceeding with reset\n");
  522. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  523. pmcraid_ioa_reset(cmd);
  524. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  525. }
  526. }
  527. /**
  528. * pmcraid_start_bist - starts BIST
  529. * @cmd: pointer to reset cmd
  530. * Return Value
  531. * none
  532. */
  533. static void pmcraid_start_bist(struct pmcraid_cmd *cmd)
  534. {
  535. struct pmcraid_instance *pinstance = cmd->drv_inst;
  536. u32 doorbells, intrs;
  537. /* proceed with bist and wait for 2 seconds */
  538. iowrite32(DOORBELL_IOA_START_BIST,
  539. pinstance->int_regs.host_ioa_interrupt_reg);
  540. doorbells = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  541. intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  542. pmcraid_info("doorbells after start bist: %x intrs: %x\n",
  543. doorbells, intrs);
  544. cmd->time_left = msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
  545. cmd->timer.data = (unsigned long)cmd;
  546. cmd->timer.expires = jiffies + msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
  547. cmd->timer.function = (void (*)(unsigned long))pmcraid_bist_done;
  548. add_timer(&cmd->timer);
  549. }
  550. /**
  551. * pmcraid_reset_alert_done - completion routine for reset_alert
  552. * @cmd: pointer to command block used in reset sequence
  553. * Return value
  554. * None
  555. */
  556. static void pmcraid_reset_alert_done(struct pmcraid_cmd *cmd)
  557. {
  558. struct pmcraid_instance *pinstance = cmd->drv_inst;
  559. u32 status = ioread32(pinstance->ioa_status);
  560. unsigned long lock_flags;
  561. /* if the critical operation in progress bit is set or the wait times
  562. * out, invoke reset engine to proceed with hard reset. If there is
  563. * some more time to wait, restart the timer
  564. */
  565. if (((status & INTRS_CRITICAL_OP_IN_PROGRESS) == 0) ||
  566. cmd->time_left <= 0) {
  567. pmcraid_info("critical op is reset proceeding with reset\n");
  568. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  569. pmcraid_ioa_reset(cmd);
  570. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  571. } else {
  572. pmcraid_info("critical op is not yet reset waiting again\n");
  573. /* restart timer if some more time is available to wait */
  574. cmd->time_left -= PMCRAID_CHECK_FOR_RESET_TIMEOUT;
  575. cmd->timer.data = (unsigned long)cmd;
  576. cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
  577. cmd->timer.function =
  578. (void (*)(unsigned long))pmcraid_reset_alert_done;
  579. add_timer(&cmd->timer);
  580. }
  581. }
  582. /**
  583. * pmcraid_reset_alert - alerts IOA for a possible reset
  584. * @cmd : command block to be used for reset sequence.
  585. *
  586. * Return Value
  587. * returns 0 if pci config-space is accessible and RESET_DOORBELL is
  588. * successfully written to IOA. Returns non-zero in case pci_config_space
  589. * is not accessible
  590. */
  591. static void pmcraid_notify_ioastate(struct pmcraid_instance *, u32);
  592. static void pmcraid_reset_alert(struct pmcraid_cmd *cmd)
  593. {
  594. struct pmcraid_instance *pinstance = cmd->drv_inst;
  595. u32 doorbells;
  596. int rc;
  597. u16 pci_reg;
  598. /* If we are able to access IOA PCI config space, alert IOA that we are
  599. * going to reset it soon. This enables IOA to preserv persistent error
  600. * data if any. In case memory space is not accessible, proceed with
  601. * BIST or slot_reset
  602. */
  603. rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
  604. if ((rc == PCIBIOS_SUCCESSFUL) && (pci_reg & PCI_COMMAND_MEMORY)) {
  605. /* wait for IOA permission i.e until CRITICAL_OPERATION bit is
  606. * reset IOA doesn't generate any interrupts when CRITICAL
  607. * OPERATION bit is reset. A timer is started to wait for this
  608. * bit to be reset.
  609. */
  610. cmd->time_left = PMCRAID_RESET_TIMEOUT;
  611. cmd->timer.data = (unsigned long)cmd;
  612. cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
  613. cmd->timer.function =
  614. (void (*)(unsigned long))pmcraid_reset_alert_done;
  615. add_timer(&cmd->timer);
  616. iowrite32(DOORBELL_IOA_RESET_ALERT,
  617. pinstance->int_regs.host_ioa_interrupt_reg);
  618. doorbells =
  619. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  620. pmcraid_info("doorbells after reset alert: %x\n", doorbells);
  621. } else {
  622. pmcraid_info("PCI config is not accessible starting BIST\n");
  623. pinstance->ioa_state = IOA_STATE_IN_HARD_RESET;
  624. pmcraid_start_bist(cmd);
  625. }
  626. }
  627. /**
  628. * pmcraid_timeout_handler - Timeout handler for internally generated ops
  629. *
  630. * @cmd : pointer to command structure, that got timedout
  631. *
  632. * This function blocks host requests and initiates an adapter reset.
  633. *
  634. * Return value:
  635. * None
  636. */
  637. static void pmcraid_timeout_handler(struct pmcraid_cmd *cmd)
  638. {
  639. struct pmcraid_instance *pinstance = cmd->drv_inst;
  640. unsigned long lock_flags;
  641. dev_info(&pinstance->pdev->dev,
  642. "Adapter being reset due to cmd(CDB[0] = %x) timeout\n",
  643. cmd->ioa_cb->ioarcb.cdb[0]);
  644. /* Command timeouts result in hard reset sequence. The command that got
  645. * timed out may be the one used as part of reset sequence. In this
  646. * case restart reset sequence using the same command block even if
  647. * reset is in progress. Otherwise fail this command and get a free
  648. * command block to restart the reset sequence.
  649. */
  650. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  651. if (!pinstance->ioa_reset_in_progress) {
  652. pinstance->ioa_reset_attempts = 0;
  653. cmd = pmcraid_get_free_cmd(pinstance);
  654. /* If we are out of command blocks, just return here itself.
  655. * Some other command's timeout handler can do the reset job
  656. */
  657. if (cmd == NULL) {
  658. spin_unlock_irqrestore(pinstance->host->host_lock,
  659. lock_flags);
  660. pmcraid_err("no free cmnd block for timeout handler\n");
  661. return;
  662. }
  663. pinstance->reset_cmd = cmd;
  664. pinstance->ioa_reset_in_progress = 1;
  665. } else {
  666. pmcraid_info("reset is already in progress\n");
  667. if (pinstance->reset_cmd != cmd) {
  668. /* This command should have been given to IOA, this
  669. * command will be completed by fail_outstanding_cmds
  670. * anyway
  671. */
  672. pmcraid_err("cmd is pending but reset in progress\n");
  673. }
  674. /* If this command was being used as part of the reset
  675. * sequence, set cmd_done pointer to pmcraid_ioa_reset. This
  676. * causes fail_outstanding_commands not to return the command
  677. * block back to free pool
  678. */
  679. if (cmd == pinstance->reset_cmd)
  680. cmd->cmd_done = pmcraid_ioa_reset;
  681. }
  682. /* Notify apps of important IOA bringup/bringdown sequences */
  683. if (pinstance->scn.ioa_state != PMC_DEVICE_EVENT_RESET_START &&
  684. pinstance->scn.ioa_state != PMC_DEVICE_EVENT_SHUTDOWN_START)
  685. pmcraid_notify_ioastate(pinstance,
  686. PMC_DEVICE_EVENT_RESET_START);
  687. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  688. scsi_block_requests(pinstance->host);
  689. pmcraid_reset_alert(cmd);
  690. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  691. }
  692. /**
  693. * pmcraid_internal_done - completion routine for internally generated cmds
  694. *
  695. * @cmd: command that got response from IOA
  696. *
  697. * Return Value:
  698. * none
  699. */
  700. static void pmcraid_internal_done(struct pmcraid_cmd *cmd)
  701. {
  702. pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
  703. cmd->ioa_cb->ioarcb.cdb[0],
  704. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  705. /* Some of the internal commands are sent with callers blocking for the
  706. * response. Same will be indicated as part of cmd->completion_req
  707. * field. Response path needs to wake up any waiters waiting for cmd
  708. * completion if this flag is set.
  709. */
  710. if (cmd->completion_req) {
  711. cmd->completion_req = 0;
  712. complete(&cmd->wait_for_completion);
  713. }
  714. /* most of the internal commands are completed by caller itself, so
  715. * no need to return the command block back to free pool until we are
  716. * required to do so (e.g once done with initialization).
  717. */
  718. if (cmd->release) {
  719. cmd->release = 0;
  720. pmcraid_return_cmd(cmd);
  721. }
  722. }
  723. /**
  724. * pmcraid_reinit_cfgtable_done - done function for cfg table reinitialization
  725. *
  726. * @cmd: command that got response from IOA
  727. *
  728. * This routine is called after driver re-reads configuration table due to a
  729. * lost CCN. It returns the command block back to free pool and schedules
  730. * worker thread to add/delete devices into the system.
  731. *
  732. * Return Value:
  733. * none
  734. */
  735. static void pmcraid_reinit_cfgtable_done(struct pmcraid_cmd *cmd)
  736. {
  737. pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
  738. cmd->ioa_cb->ioarcb.cdb[0],
  739. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  740. if (cmd->release) {
  741. cmd->release = 0;
  742. pmcraid_return_cmd(cmd);
  743. }
  744. pmcraid_info("scheduling worker for config table reinitialization\n");
  745. schedule_work(&cmd->drv_inst->worker_q);
  746. }
  747. /**
  748. * pmcraid_erp_done - Process completion of SCSI error response from device
  749. * @cmd: pmcraid_command
  750. *
  751. * This function copies the sense buffer into the scsi_cmd struct and completes
  752. * scsi_cmd by calling scsi_done function.
  753. *
  754. * Return value:
  755. * none
  756. */
  757. static void pmcraid_erp_done(struct pmcraid_cmd *cmd)
  758. {
  759. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  760. struct pmcraid_instance *pinstance = cmd->drv_inst;
  761. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  762. if (PMCRAID_IOASC_SENSE_KEY(ioasc) > 0) {
  763. scsi_cmd->result |= (DID_ERROR << 16);
  764. scmd_printk(KERN_INFO, scsi_cmd,
  765. "command CDB[0] = %x failed with IOASC: 0x%08X\n",
  766. cmd->ioa_cb->ioarcb.cdb[0], ioasc);
  767. }
  768. /* if we had allocated sense buffers for request sense, copy the sense
  769. * release the buffers
  770. */
  771. if (cmd->sense_buffer != NULL) {
  772. memcpy(scsi_cmd->sense_buffer,
  773. cmd->sense_buffer,
  774. SCSI_SENSE_BUFFERSIZE);
  775. pci_free_consistent(pinstance->pdev,
  776. SCSI_SENSE_BUFFERSIZE,
  777. cmd->sense_buffer, cmd->sense_buffer_dma);
  778. cmd->sense_buffer = NULL;
  779. cmd->sense_buffer_dma = 0;
  780. }
  781. scsi_dma_unmap(scsi_cmd);
  782. pmcraid_return_cmd(cmd);
  783. scsi_cmd->scsi_done(scsi_cmd);
  784. }
  785. /**
  786. * pmcraid_fire_command - sends an IOA command to adapter
  787. *
  788. * This function adds the given block into pending command list
  789. * and returns without waiting
  790. *
  791. * @cmd : command to be sent to the device
  792. *
  793. * Return Value
  794. * None
  795. */
  796. static void _pmcraid_fire_command(struct pmcraid_cmd *cmd)
  797. {
  798. struct pmcraid_instance *pinstance = cmd->drv_inst;
  799. unsigned long lock_flags;
  800. /* Add this command block to pending cmd pool. We do this prior to
  801. * writting IOARCB to ioarrin because IOA might complete the command
  802. * by the time we are about to add it to the list. Response handler
  803. * (isr/tasklet) looks for cmd block in the pending pending list.
  804. */
  805. spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
  806. list_add_tail(&cmd->free_list, &pinstance->pending_cmd_pool);
  807. spin_unlock_irqrestore(&pinstance->pending_pool_lock, lock_flags);
  808. atomic_inc(&pinstance->outstanding_cmds);
  809. /* driver writes lower 32-bit value of IOARCB address only */
  810. mb();
  811. iowrite32(le32_to_cpu(cmd->ioa_cb->ioarcb.ioarcb_bus_addr),
  812. pinstance->ioarrin);
  813. }
  814. /**
  815. * pmcraid_send_cmd - fires a command to IOA
  816. *
  817. * This function also sets up timeout function, and command completion
  818. * function
  819. *
  820. * @cmd: pointer to the command block to be fired to IOA
  821. * @cmd_done: command completion function, called once IOA responds
  822. * @timeout: timeout to wait for this command completion
  823. * @timeout_func: timeout handler
  824. *
  825. * Return value
  826. * none
  827. */
  828. static void pmcraid_send_cmd(
  829. struct pmcraid_cmd *cmd,
  830. void (*cmd_done) (struct pmcraid_cmd *),
  831. unsigned long timeout,
  832. void (*timeout_func) (struct pmcraid_cmd *)
  833. )
  834. {
  835. /* initialize done function */
  836. cmd->cmd_done = cmd_done;
  837. if (timeout_func) {
  838. /* setup timeout handler */
  839. cmd->timer.data = (unsigned long)cmd;
  840. cmd->timer.expires = jiffies + timeout;
  841. cmd->timer.function = (void (*)(unsigned long))timeout_func;
  842. add_timer(&cmd->timer);
  843. }
  844. /* fire the command to IOA */
  845. _pmcraid_fire_command(cmd);
  846. }
  847. /**
  848. * pmcraid_ioa_shutdown_done - completion function for IOA shutdown command
  849. * @cmd: pointer to the command block used for sending IOA shutdown command
  850. *
  851. * Return value
  852. * None
  853. */
  854. static void pmcraid_ioa_shutdown_done(struct pmcraid_cmd *cmd)
  855. {
  856. struct pmcraid_instance *pinstance = cmd->drv_inst;
  857. unsigned long lock_flags;
  858. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  859. pmcraid_ioa_reset(cmd);
  860. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  861. }
  862. /**
  863. * pmcraid_ioa_shutdown - sends SHUTDOWN command to ioa
  864. *
  865. * @cmd: pointer to the command block used as part of reset sequence
  866. *
  867. * Return Value
  868. * None
  869. */
  870. static void pmcraid_ioa_shutdown(struct pmcraid_cmd *cmd)
  871. {
  872. pmcraid_info("response for Cancel CCN CDB[0] = %x ioasc = %x\n",
  873. cmd->ioa_cb->ioarcb.cdb[0],
  874. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  875. /* Note that commands sent during reset require next command to be sent
  876. * to IOA. Hence reinit the done function as well as timeout function
  877. */
  878. pmcraid_reinit_cmdblk(cmd);
  879. cmd->ioa_cb->ioarcb.request_type = REQ_TYPE_IOACMD;
  880. cmd->ioa_cb->ioarcb.resource_handle =
  881. cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  882. cmd->ioa_cb->ioarcb.cdb[0] = PMCRAID_IOA_SHUTDOWN;
  883. cmd->ioa_cb->ioarcb.cdb[1] = PMCRAID_SHUTDOWN_NORMAL;
  884. /* fire shutdown command to hardware. */
  885. pmcraid_info("firing normal shutdown command (%d) to IOA\n",
  886. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle));
  887. pmcraid_notify_ioastate(cmd->drv_inst, PMC_DEVICE_EVENT_SHUTDOWN_START);
  888. pmcraid_send_cmd(cmd, pmcraid_ioa_shutdown_done,
  889. PMCRAID_SHUTDOWN_TIMEOUT,
  890. pmcraid_timeout_handler);
  891. }
  892. /**
  893. * pmcraid_get_fwversion_done - completion function for get_fwversion
  894. *
  895. * @cmd: pointer to command block used to send INQUIRY command
  896. *
  897. * Return Value
  898. * none
  899. */
  900. static void pmcraid_querycfg(struct pmcraid_cmd *);
  901. static void pmcraid_get_fwversion_done(struct pmcraid_cmd *cmd)
  902. {
  903. struct pmcraid_instance *pinstance = cmd->drv_inst;
  904. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  905. unsigned long lock_flags;
  906. /* configuration table entry size depends on firmware version. If fw
  907. * version is not known, it is not possible to interpret IOA config
  908. * table
  909. */
  910. if (ioasc) {
  911. pmcraid_err("IOA Inquiry failed with %x\n", ioasc);
  912. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  913. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  914. pmcraid_reset_alert(cmd);
  915. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  916. } else {
  917. pmcraid_querycfg(cmd);
  918. }
  919. }
  920. /**
  921. * pmcraid_get_fwversion - reads firmware version information
  922. *
  923. * @cmd: pointer to command block used to send INQUIRY command
  924. *
  925. * Return Value
  926. * none
  927. */
  928. static void pmcraid_get_fwversion(struct pmcraid_cmd *cmd)
  929. {
  930. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  931. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  932. struct pmcraid_instance *pinstance = cmd->drv_inst;
  933. u16 data_size = sizeof(struct pmcraid_inquiry_data);
  934. pmcraid_reinit_cmdblk(cmd);
  935. ioarcb->request_type = REQ_TYPE_SCSI;
  936. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  937. ioarcb->cdb[0] = INQUIRY;
  938. ioarcb->cdb[1] = 1;
  939. ioarcb->cdb[2] = 0xD0;
  940. ioarcb->cdb[3] = (data_size >> 8) & 0xFF;
  941. ioarcb->cdb[4] = data_size & 0xFF;
  942. /* Since entire inquiry data it can be part of IOARCB itself
  943. */
  944. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  945. offsetof(struct pmcraid_ioarcb,
  946. add_data.u.ioadl[0]));
  947. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  948. ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
  949. ioarcb->request_flags0 |= NO_LINK_DESCS;
  950. ioarcb->data_transfer_length = cpu_to_le32(data_size);
  951. ioadl = &(ioarcb->add_data.u.ioadl[0]);
  952. ioadl->flags = IOADL_FLAGS_LAST_DESC;
  953. ioadl->address = cpu_to_le64(pinstance->inq_data_baddr);
  954. ioadl->data_len = cpu_to_le32(data_size);
  955. pmcraid_send_cmd(cmd, pmcraid_get_fwversion_done,
  956. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  957. }
  958. /**
  959. * pmcraid_identify_hrrq - registers host rrq buffers with IOA
  960. * @cmd: pointer to command block to be used for identify hrrq
  961. *
  962. * Return Value
  963. * none
  964. */
  965. static void pmcraid_identify_hrrq(struct pmcraid_cmd *cmd)
  966. {
  967. struct pmcraid_instance *pinstance = cmd->drv_inst;
  968. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  969. int index = cmd->hrrq_index;
  970. __be64 hrrq_addr = cpu_to_be64(pinstance->hrrq_start_bus_addr[index]);
  971. u32 hrrq_size = cpu_to_be32(sizeof(u32) * PMCRAID_MAX_CMD);
  972. void (*done_function)(struct pmcraid_cmd *);
  973. pmcraid_reinit_cmdblk(cmd);
  974. cmd->hrrq_index = index + 1;
  975. if (cmd->hrrq_index < pinstance->num_hrrq) {
  976. done_function = pmcraid_identify_hrrq;
  977. } else {
  978. cmd->hrrq_index = 0;
  979. done_function = pmcraid_get_fwversion;
  980. }
  981. /* Initialize ioarcb */
  982. ioarcb->request_type = REQ_TYPE_IOACMD;
  983. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  984. /* initialize the hrrq number where IOA will respond to this command */
  985. ioarcb->hrrq_id = index;
  986. ioarcb->cdb[0] = PMCRAID_IDENTIFY_HRRQ;
  987. ioarcb->cdb[1] = index;
  988. /* IOA expects 64-bit pci address to be written in B.E format
  989. * (i.e cdb[2]=MSByte..cdb[9]=LSB.
  990. */
  991. pmcraid_info("HRRQ_IDENTIFY with hrrq:ioarcb:index => %llx:%llx:%x\n",
  992. hrrq_addr, ioarcb->ioarcb_bus_addr, index);
  993. memcpy(&(ioarcb->cdb[2]), &hrrq_addr, sizeof(hrrq_addr));
  994. memcpy(&(ioarcb->cdb[10]), &hrrq_size, sizeof(hrrq_size));
  995. /* Subsequent commands require HRRQ identification to be successful.
  996. * Note that this gets called even during reset from SCSI mid-layer
  997. * or tasklet
  998. */
  999. pmcraid_send_cmd(cmd, done_function,
  1000. PMCRAID_INTERNAL_TIMEOUT,
  1001. pmcraid_timeout_handler);
  1002. }
  1003. static void pmcraid_process_ccn(struct pmcraid_cmd *cmd);
  1004. static void pmcraid_process_ldn(struct pmcraid_cmd *cmd);
  1005. /**
  1006. * pmcraid_send_hcam_cmd - send an initialized command block(HCAM) to IOA
  1007. *
  1008. * @cmd: initialized command block pointer
  1009. *
  1010. * Return Value
  1011. * none
  1012. */
  1013. static void pmcraid_send_hcam_cmd(struct pmcraid_cmd *cmd)
  1014. {
  1015. if (cmd->ioa_cb->ioarcb.cdb[1] == PMCRAID_HCAM_CODE_CONFIG_CHANGE)
  1016. atomic_set(&(cmd->drv_inst->ccn.ignore), 0);
  1017. else
  1018. atomic_set(&(cmd->drv_inst->ldn.ignore), 0);
  1019. pmcraid_send_cmd(cmd, cmd->cmd_done, 0, NULL);
  1020. }
  1021. /**
  1022. * pmcraid_init_hcam - send an initialized command block(HCAM) to IOA
  1023. *
  1024. * @pinstance: pointer to adapter instance structure
  1025. * @type: HCAM type
  1026. *
  1027. * Return Value
  1028. * pointer to initialized pmcraid_cmd structure or NULL
  1029. */
  1030. static struct pmcraid_cmd *pmcraid_init_hcam
  1031. (
  1032. struct pmcraid_instance *pinstance,
  1033. u8 type
  1034. )
  1035. {
  1036. struct pmcraid_cmd *cmd;
  1037. struct pmcraid_ioarcb *ioarcb;
  1038. struct pmcraid_ioadl_desc *ioadl;
  1039. struct pmcraid_hostrcb *hcam;
  1040. void (*cmd_done) (struct pmcraid_cmd *);
  1041. dma_addr_t dma;
  1042. int rcb_size;
  1043. cmd = pmcraid_get_free_cmd(pinstance);
  1044. if (!cmd) {
  1045. pmcraid_err("no free command blocks for hcam\n");
  1046. return cmd;
  1047. }
  1048. if (type == PMCRAID_HCAM_CODE_CONFIG_CHANGE) {
  1049. rcb_size = sizeof(struct pmcraid_hcam_ccn_ext);
  1050. cmd_done = pmcraid_process_ccn;
  1051. dma = pinstance->ccn.baddr + PMCRAID_AEN_HDR_SIZE;
  1052. hcam = &pinstance->ccn;
  1053. } else {
  1054. rcb_size = sizeof(struct pmcraid_hcam_ldn);
  1055. cmd_done = pmcraid_process_ldn;
  1056. dma = pinstance->ldn.baddr + PMCRAID_AEN_HDR_SIZE;
  1057. hcam = &pinstance->ldn;
  1058. }
  1059. /* initialize command pointer used for HCAM registration */
  1060. hcam->cmd = cmd;
  1061. ioarcb = &cmd->ioa_cb->ioarcb;
  1062. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  1063. offsetof(struct pmcraid_ioarcb,
  1064. add_data.u.ioadl[0]));
  1065. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  1066. ioadl = ioarcb->add_data.u.ioadl;
  1067. /* Initialize ioarcb */
  1068. ioarcb->request_type = REQ_TYPE_HCAM;
  1069. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  1070. ioarcb->cdb[0] = PMCRAID_HOST_CONTROLLED_ASYNC;
  1071. ioarcb->cdb[1] = type;
  1072. ioarcb->cdb[7] = (rcb_size >> 8) & 0xFF;
  1073. ioarcb->cdb[8] = (rcb_size) & 0xFF;
  1074. ioarcb->data_transfer_length = cpu_to_le32(rcb_size);
  1075. ioadl[0].flags |= IOADL_FLAGS_READ_LAST;
  1076. ioadl[0].data_len = cpu_to_le32(rcb_size);
  1077. ioadl[0].address = cpu_to_le32(dma);
  1078. cmd->cmd_done = cmd_done;
  1079. return cmd;
  1080. }
  1081. /**
  1082. * pmcraid_send_hcam - Send an HCAM to IOA
  1083. * @pinstance: ioa config struct
  1084. * @type: HCAM type
  1085. *
  1086. * This function will send a Host Controlled Async command to IOA.
  1087. *
  1088. * Return value:
  1089. * none
  1090. */
  1091. static void pmcraid_send_hcam(struct pmcraid_instance *pinstance, u8 type)
  1092. {
  1093. struct pmcraid_cmd *cmd = pmcraid_init_hcam(pinstance, type);
  1094. pmcraid_send_hcam_cmd(cmd);
  1095. }
  1096. /**
  1097. * pmcraid_prepare_cancel_cmd - prepares a command block to abort another
  1098. *
  1099. * @cmd: pointer to cmd that is used as cancelling command
  1100. * @cmd_to_cancel: pointer to the command that needs to be cancelled
  1101. */
  1102. static void pmcraid_prepare_cancel_cmd(
  1103. struct pmcraid_cmd *cmd,
  1104. struct pmcraid_cmd *cmd_to_cancel
  1105. )
  1106. {
  1107. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  1108. __be64 ioarcb_addr = cmd_to_cancel->ioa_cb->ioarcb.ioarcb_bus_addr;
  1109. /* Get the resource handle to where the command to be aborted has been
  1110. * sent.
  1111. */
  1112. ioarcb->resource_handle = cmd_to_cancel->ioa_cb->ioarcb.resource_handle;
  1113. ioarcb->request_type = REQ_TYPE_IOACMD;
  1114. memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
  1115. ioarcb->cdb[0] = PMCRAID_ABORT_CMD;
  1116. /* IOARCB address of the command to be cancelled is given in
  1117. * cdb[2]..cdb[9] is Big-Endian format. Note that length bits in
  1118. * IOARCB address are not masked.
  1119. */
  1120. ioarcb_addr = cpu_to_be64(ioarcb_addr);
  1121. memcpy(&(ioarcb->cdb[2]), &ioarcb_addr, sizeof(ioarcb_addr));
  1122. }
  1123. /**
  1124. * pmcraid_cancel_hcam - sends ABORT task to abort a given HCAM
  1125. *
  1126. * @cmd: command to be used as cancelling command
  1127. * @type: HCAM type
  1128. * @cmd_done: op done function for the cancelling command
  1129. */
  1130. static void pmcraid_cancel_hcam(
  1131. struct pmcraid_cmd *cmd,
  1132. u8 type,
  1133. void (*cmd_done) (struct pmcraid_cmd *)
  1134. )
  1135. {
  1136. struct pmcraid_instance *pinstance;
  1137. struct pmcraid_hostrcb *hcam;
  1138. pinstance = cmd->drv_inst;
  1139. hcam = (type == PMCRAID_HCAM_CODE_LOG_DATA) ?
  1140. &pinstance->ldn : &pinstance->ccn;
  1141. /* prepare for cancelling previous hcam command. If the HCAM is
  1142. * currently not pending with IOA, we would have hcam->cmd as non-null
  1143. */
  1144. if (hcam->cmd == NULL)
  1145. return;
  1146. pmcraid_prepare_cancel_cmd(cmd, hcam->cmd);
  1147. /* writing to IOARRIN must be protected by host_lock, as mid-layer
  1148. * schedule queuecommand while we are doing this
  1149. */
  1150. pmcraid_send_cmd(cmd, cmd_done,
  1151. PMCRAID_INTERNAL_TIMEOUT,
  1152. pmcraid_timeout_handler);
  1153. }
  1154. /**
  1155. * pmcraid_cancel_ccn - cancel CCN HCAM already registered with IOA
  1156. *
  1157. * @cmd: command block to be used for cancelling the HCAM
  1158. */
  1159. static void pmcraid_cancel_ccn(struct pmcraid_cmd *cmd)
  1160. {
  1161. pmcraid_info("response for Cancel LDN CDB[0] = %x ioasc = %x\n",
  1162. cmd->ioa_cb->ioarcb.cdb[0],
  1163. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  1164. pmcraid_reinit_cmdblk(cmd);
  1165. pmcraid_cancel_hcam(cmd,
  1166. PMCRAID_HCAM_CODE_CONFIG_CHANGE,
  1167. pmcraid_ioa_shutdown);
  1168. }
  1169. /**
  1170. * pmcraid_cancel_ldn - cancel LDN HCAM already registered with IOA
  1171. *
  1172. * @cmd: command block to be used for cancelling the HCAM
  1173. */
  1174. static void pmcraid_cancel_ldn(struct pmcraid_cmd *cmd)
  1175. {
  1176. pmcraid_cancel_hcam(cmd,
  1177. PMCRAID_HCAM_CODE_LOG_DATA,
  1178. pmcraid_cancel_ccn);
  1179. }
  1180. /**
  1181. * pmcraid_expose_resource - check if the resource can be exposed to OS
  1182. *
  1183. * @fw_version: firmware version code
  1184. * @cfgte: pointer to configuration table entry of the resource
  1185. *
  1186. * Return value:
  1187. * true if resource can be added to midlayer, false(0) otherwise
  1188. */
  1189. static int pmcraid_expose_resource(u16 fw_version,
  1190. struct pmcraid_config_table_entry *cfgte)
  1191. {
  1192. int retval = 0;
  1193. if (cfgte->resource_type == RES_TYPE_VSET) {
  1194. if (fw_version <= PMCRAID_FW_VERSION_1)
  1195. retval = ((cfgte->unique_flags1 & 0x80) == 0);
  1196. else
  1197. retval = ((cfgte->unique_flags0 & 0x80) == 0 &&
  1198. (cfgte->unique_flags1 & 0x80) == 0);
  1199. } else if (cfgte->resource_type == RES_TYPE_GSCSI)
  1200. retval = (RES_BUS(cfgte->resource_address) !=
  1201. PMCRAID_VIRTUAL_ENCL_BUS_ID);
  1202. return retval;
  1203. }
  1204. /* attributes supported by pmcraid_event_family */
  1205. enum {
  1206. PMCRAID_AEN_ATTR_UNSPEC,
  1207. PMCRAID_AEN_ATTR_EVENT,
  1208. __PMCRAID_AEN_ATTR_MAX,
  1209. };
  1210. #define PMCRAID_AEN_ATTR_MAX (__PMCRAID_AEN_ATTR_MAX - 1)
  1211. /* commands supported by pmcraid_event_family */
  1212. enum {
  1213. PMCRAID_AEN_CMD_UNSPEC,
  1214. PMCRAID_AEN_CMD_EVENT,
  1215. __PMCRAID_AEN_CMD_MAX,
  1216. };
  1217. #define PMCRAID_AEN_CMD_MAX (__PMCRAID_AEN_CMD_MAX - 1)
  1218. static struct genl_multicast_group pmcraid_mcgrps[] = {
  1219. { .name = "events", /* not really used - see ID discussion below */ },
  1220. };
  1221. static struct genl_family pmcraid_event_family = {
  1222. /*
  1223. * Due to prior multicast group abuse (the code having assumed that
  1224. * the family ID can be used as a multicast group ID) we need to
  1225. * statically allocate a family (and thus group) ID.
  1226. */
  1227. .id = GENL_ID_PMCRAID,
  1228. .name = "pmcraid",
  1229. .version = 1,
  1230. .maxattr = PMCRAID_AEN_ATTR_MAX,
  1231. .mcgrps = pmcraid_mcgrps,
  1232. .n_mcgrps = ARRAY_SIZE(pmcraid_mcgrps),
  1233. };
  1234. /**
  1235. * pmcraid_netlink_init - registers pmcraid_event_family
  1236. *
  1237. * Return value:
  1238. * 0 if the pmcraid_event_family is successfully registered
  1239. * with netlink generic, non-zero otherwise
  1240. */
  1241. static int pmcraid_netlink_init(void)
  1242. {
  1243. int result;
  1244. result = genl_register_family(&pmcraid_event_family);
  1245. if (result)
  1246. return result;
  1247. pmcraid_info("registered NETLINK GENERIC group: %d\n",
  1248. pmcraid_event_family.id);
  1249. return result;
  1250. }
  1251. /**
  1252. * pmcraid_netlink_release - unregisters pmcraid_event_family
  1253. *
  1254. * Return value:
  1255. * none
  1256. */
  1257. static void pmcraid_netlink_release(void)
  1258. {
  1259. genl_unregister_family(&pmcraid_event_family);
  1260. }
  1261. /**
  1262. * pmcraid_notify_aen - sends event msg to user space application
  1263. * @pinstance: pointer to adapter instance structure
  1264. * @type: HCAM type
  1265. *
  1266. * Return value:
  1267. * 0 if success, error value in case of any failure.
  1268. */
  1269. static int pmcraid_notify_aen(
  1270. struct pmcraid_instance *pinstance,
  1271. struct pmcraid_aen_msg *aen_msg,
  1272. u32 data_size
  1273. )
  1274. {
  1275. struct sk_buff *skb;
  1276. void *msg_header;
  1277. u32 total_size, nla_genl_hdr_total_size;
  1278. int result;
  1279. aen_msg->hostno = (pinstance->host->unique_id << 16 |
  1280. MINOR(pinstance->cdev.dev));
  1281. aen_msg->length = data_size;
  1282. data_size += sizeof(*aen_msg);
  1283. total_size = nla_total_size(data_size);
  1284. /* Add GENL_HDR to total_size */
  1285. nla_genl_hdr_total_size =
  1286. (total_size + (GENL_HDRLEN +
  1287. ((struct genl_family *)&pmcraid_event_family)->hdrsize)
  1288. + NLMSG_HDRLEN);
  1289. skb = genlmsg_new(nla_genl_hdr_total_size, GFP_ATOMIC);
  1290. if (!skb) {
  1291. pmcraid_err("Failed to allocate aen data SKB of size: %x\n",
  1292. total_size);
  1293. return -ENOMEM;
  1294. }
  1295. /* add the genetlink message header */
  1296. msg_header = genlmsg_put(skb, 0, 0,
  1297. &pmcraid_event_family, 0,
  1298. PMCRAID_AEN_CMD_EVENT);
  1299. if (!msg_header) {
  1300. pmcraid_err("failed to copy command details\n");
  1301. nlmsg_free(skb);
  1302. return -ENOMEM;
  1303. }
  1304. result = nla_put(skb, PMCRAID_AEN_ATTR_EVENT, data_size, aen_msg);
  1305. if (result) {
  1306. pmcraid_err("failed to copy AEN attribute data\n");
  1307. nlmsg_free(skb);
  1308. return -EINVAL;
  1309. }
  1310. /* send genetlink multicast message to notify appplications */
  1311. result = genlmsg_end(skb, msg_header);
  1312. if (result < 0) {
  1313. pmcraid_err("genlmsg_end failed\n");
  1314. nlmsg_free(skb);
  1315. return result;
  1316. }
  1317. result = genlmsg_multicast(&pmcraid_event_family, skb,
  1318. 0, 0, GFP_ATOMIC);
  1319. /* If there are no listeners, genlmsg_multicast may return non-zero
  1320. * value.
  1321. */
  1322. if (result)
  1323. pmcraid_info("error (%x) sending aen event message\n", result);
  1324. return result;
  1325. }
  1326. /**
  1327. * pmcraid_notify_ccn - notifies about CCN event msg to user space
  1328. * @pinstance: pointer adapter instance structure
  1329. *
  1330. * Return value:
  1331. * 0 if success, error value in case of any failure
  1332. */
  1333. static int pmcraid_notify_ccn(struct pmcraid_instance *pinstance)
  1334. {
  1335. return pmcraid_notify_aen(pinstance,
  1336. pinstance->ccn.msg,
  1337. pinstance->ccn.hcam->data_len +
  1338. sizeof(struct pmcraid_hcam_hdr));
  1339. }
  1340. /**
  1341. * pmcraid_notify_ldn - notifies about CCN event msg to user space
  1342. * @pinstance: pointer adapter instance structure
  1343. *
  1344. * Return value:
  1345. * 0 if success, error value in case of any failure
  1346. */
  1347. static int pmcraid_notify_ldn(struct pmcraid_instance *pinstance)
  1348. {
  1349. return pmcraid_notify_aen(pinstance,
  1350. pinstance->ldn.msg,
  1351. pinstance->ldn.hcam->data_len +
  1352. sizeof(struct pmcraid_hcam_hdr));
  1353. }
  1354. /**
  1355. * pmcraid_notify_ioastate - sends IOA state event msg to user space
  1356. * @pinstance: pointer adapter instance structure
  1357. * @evt: controller state event to be sent
  1358. *
  1359. * Return value:
  1360. * 0 if success, error value in case of any failure
  1361. */
  1362. static void pmcraid_notify_ioastate(struct pmcraid_instance *pinstance, u32 evt)
  1363. {
  1364. pinstance->scn.ioa_state = evt;
  1365. pmcraid_notify_aen(pinstance,
  1366. &pinstance->scn.msg,
  1367. sizeof(u32));
  1368. }
  1369. /**
  1370. * pmcraid_handle_config_change - Handle a config change from the adapter
  1371. * @pinstance: pointer to per adapter instance structure
  1372. *
  1373. * Return value:
  1374. * none
  1375. */
  1376. static void pmcraid_handle_config_change(struct pmcraid_instance *pinstance)
  1377. {
  1378. struct pmcraid_config_table_entry *cfg_entry;
  1379. struct pmcraid_hcam_ccn *ccn_hcam;
  1380. struct pmcraid_cmd *cmd;
  1381. struct pmcraid_cmd *cfgcmd;
  1382. struct pmcraid_resource_entry *res = NULL;
  1383. unsigned long lock_flags;
  1384. unsigned long host_lock_flags;
  1385. u32 new_entry = 1;
  1386. u32 hidden_entry = 0;
  1387. u16 fw_version;
  1388. int rc;
  1389. ccn_hcam = (struct pmcraid_hcam_ccn *)pinstance->ccn.hcam;
  1390. cfg_entry = &ccn_hcam->cfg_entry;
  1391. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  1392. pmcraid_info("CCN(%x): %x timestamp: %llx type: %x lost: %x flags: %x \
  1393. res: %x:%x:%x:%x\n",
  1394. pinstance->ccn.hcam->ilid,
  1395. pinstance->ccn.hcam->op_code,
  1396. ((pinstance->ccn.hcam->timestamp1) |
  1397. ((pinstance->ccn.hcam->timestamp2 & 0xffffffffLL) << 32)),
  1398. pinstance->ccn.hcam->notification_type,
  1399. pinstance->ccn.hcam->notification_lost,
  1400. pinstance->ccn.hcam->flags,
  1401. pinstance->host->unique_id,
  1402. RES_IS_VSET(*cfg_entry) ? PMCRAID_VSET_BUS_ID :
  1403. (RES_IS_GSCSI(*cfg_entry) ? PMCRAID_PHYS_BUS_ID :
  1404. RES_BUS(cfg_entry->resource_address)),
  1405. RES_IS_VSET(*cfg_entry) ?
  1406. (fw_version <= PMCRAID_FW_VERSION_1 ?
  1407. cfg_entry->unique_flags1 :
  1408. cfg_entry->array_id & 0xFF) :
  1409. RES_TARGET(cfg_entry->resource_address),
  1410. RES_LUN(cfg_entry->resource_address));
  1411. /* If this HCAM indicates a lost notification, read the config table */
  1412. if (pinstance->ccn.hcam->notification_lost) {
  1413. cfgcmd = pmcraid_get_free_cmd(pinstance);
  1414. if (cfgcmd) {
  1415. pmcraid_info("lost CCN, reading config table\b");
  1416. pinstance->reinit_cfg_table = 1;
  1417. pmcraid_querycfg(cfgcmd);
  1418. } else {
  1419. pmcraid_err("lost CCN, no free cmd for querycfg\n");
  1420. }
  1421. goto out_notify_apps;
  1422. }
  1423. /* If this resource is not going to be added to mid-layer, just notify
  1424. * applications and return. If this notification is about hiding a VSET
  1425. * resource, check if it was exposed already.
  1426. */
  1427. if (pinstance->ccn.hcam->notification_type ==
  1428. NOTIFICATION_TYPE_ENTRY_CHANGED &&
  1429. cfg_entry->resource_type == RES_TYPE_VSET) {
  1430. if (fw_version <= PMCRAID_FW_VERSION_1)
  1431. hidden_entry = (cfg_entry->unique_flags1 & 0x80) != 0;
  1432. else
  1433. hidden_entry = (cfg_entry->unique_flags1 & 0x80) != 0;
  1434. } else if (!pmcraid_expose_resource(fw_version, cfg_entry)) {
  1435. goto out_notify_apps;
  1436. }
  1437. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  1438. list_for_each_entry(res, &pinstance->used_res_q, queue) {
  1439. rc = memcmp(&res->cfg_entry.resource_address,
  1440. &cfg_entry->resource_address,
  1441. sizeof(cfg_entry->resource_address));
  1442. if (!rc) {
  1443. new_entry = 0;
  1444. break;
  1445. }
  1446. }
  1447. if (new_entry) {
  1448. if (hidden_entry) {
  1449. spin_unlock_irqrestore(&pinstance->resource_lock,
  1450. lock_flags);
  1451. goto out_notify_apps;
  1452. }
  1453. /* If there are more number of resources than what driver can
  1454. * manage, do not notify the applications about the CCN. Just
  1455. * ignore this notifications and re-register the same HCAM
  1456. */
  1457. if (list_empty(&pinstance->free_res_q)) {
  1458. spin_unlock_irqrestore(&pinstance->resource_lock,
  1459. lock_flags);
  1460. pmcraid_err("too many resources attached\n");
  1461. spin_lock_irqsave(pinstance->host->host_lock,
  1462. host_lock_flags);
  1463. pmcraid_send_hcam(pinstance,
  1464. PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1465. spin_unlock_irqrestore(pinstance->host->host_lock,
  1466. host_lock_flags);
  1467. return;
  1468. }
  1469. res = list_entry(pinstance->free_res_q.next,
  1470. struct pmcraid_resource_entry, queue);
  1471. list_del(&res->queue);
  1472. res->scsi_dev = NULL;
  1473. res->reset_progress = 0;
  1474. list_add_tail(&res->queue, &pinstance->used_res_q);
  1475. }
  1476. memcpy(&res->cfg_entry, cfg_entry, pinstance->config_table_entry_size);
  1477. if (pinstance->ccn.hcam->notification_type ==
  1478. NOTIFICATION_TYPE_ENTRY_DELETED || hidden_entry) {
  1479. if (res->scsi_dev) {
  1480. if (fw_version <= PMCRAID_FW_VERSION_1)
  1481. res->cfg_entry.unique_flags1 &= 0x7F;
  1482. else
  1483. res->cfg_entry.array_id &= 0xFF;
  1484. res->change_detected = RES_CHANGE_DEL;
  1485. res->cfg_entry.resource_handle =
  1486. PMCRAID_INVALID_RES_HANDLE;
  1487. schedule_work(&pinstance->worker_q);
  1488. } else {
  1489. /* This may be one of the non-exposed resources */
  1490. list_move_tail(&res->queue, &pinstance->free_res_q);
  1491. }
  1492. } else if (!res->scsi_dev) {
  1493. res->change_detected = RES_CHANGE_ADD;
  1494. schedule_work(&pinstance->worker_q);
  1495. }
  1496. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  1497. out_notify_apps:
  1498. /* Notify configuration changes to registered applications.*/
  1499. if (!pmcraid_disable_aen)
  1500. pmcraid_notify_ccn(pinstance);
  1501. cmd = pmcraid_init_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1502. if (cmd)
  1503. pmcraid_send_hcam_cmd(cmd);
  1504. }
  1505. /**
  1506. * pmcraid_get_error_info - return error string for an ioasc
  1507. * @ioasc: ioasc code
  1508. * Return Value
  1509. * none
  1510. */
  1511. static struct pmcraid_ioasc_error *pmcraid_get_error_info(u32 ioasc)
  1512. {
  1513. int i;
  1514. for (i = 0; i < ARRAY_SIZE(pmcraid_ioasc_error_table); i++) {
  1515. if (pmcraid_ioasc_error_table[i].ioasc_code == ioasc)
  1516. return &pmcraid_ioasc_error_table[i];
  1517. }
  1518. return NULL;
  1519. }
  1520. /**
  1521. * pmcraid_ioasc_logger - log IOASC information based user-settings
  1522. * @ioasc: ioasc code
  1523. * @cmd: pointer to command that resulted in 'ioasc'
  1524. */
  1525. void pmcraid_ioasc_logger(u32 ioasc, struct pmcraid_cmd *cmd)
  1526. {
  1527. struct pmcraid_ioasc_error *error_info = pmcraid_get_error_info(ioasc);
  1528. if (error_info == NULL ||
  1529. cmd->drv_inst->current_log_level < error_info->log_level)
  1530. return;
  1531. /* log the error string */
  1532. pmcraid_err("cmd [%x] for resource %x failed with %x(%s)\n",
  1533. cmd->ioa_cb->ioarcb.cdb[0],
  1534. cmd->ioa_cb->ioarcb.resource_handle,
  1535. le32_to_cpu(ioasc), error_info->error_string);
  1536. }
  1537. /**
  1538. * pmcraid_handle_error_log - Handle a config change (error log) from the IOA
  1539. *
  1540. * @pinstance: pointer to per adapter instance structure
  1541. *
  1542. * Return value:
  1543. * none
  1544. */
  1545. static void pmcraid_handle_error_log(struct pmcraid_instance *pinstance)
  1546. {
  1547. struct pmcraid_hcam_ldn *hcam_ldn;
  1548. u32 ioasc;
  1549. hcam_ldn = (struct pmcraid_hcam_ldn *)pinstance->ldn.hcam;
  1550. pmcraid_info
  1551. ("LDN(%x): %x type: %x lost: %x flags: %x overlay id: %x\n",
  1552. pinstance->ldn.hcam->ilid,
  1553. pinstance->ldn.hcam->op_code,
  1554. pinstance->ldn.hcam->notification_type,
  1555. pinstance->ldn.hcam->notification_lost,
  1556. pinstance->ldn.hcam->flags,
  1557. pinstance->ldn.hcam->overlay_id);
  1558. /* log only the errors, no need to log informational log entries */
  1559. if (pinstance->ldn.hcam->notification_type !=
  1560. NOTIFICATION_TYPE_ERROR_LOG)
  1561. return;
  1562. if (pinstance->ldn.hcam->notification_lost ==
  1563. HOSTRCB_NOTIFICATIONS_LOST)
  1564. dev_info(&pinstance->pdev->dev, "Error notifications lost\n");
  1565. ioasc = le32_to_cpu(hcam_ldn->error_log.fd_ioasc);
  1566. if (ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET ||
  1567. ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER) {
  1568. dev_info(&pinstance->pdev->dev,
  1569. "UnitAttention due to IOA Bus Reset\n");
  1570. scsi_report_bus_reset(
  1571. pinstance->host,
  1572. RES_BUS(hcam_ldn->error_log.fd_ra));
  1573. }
  1574. return;
  1575. }
  1576. /**
  1577. * pmcraid_process_ccn - Op done function for a CCN.
  1578. * @cmd: pointer to command struct
  1579. *
  1580. * This function is the op done function for a configuration
  1581. * change notification
  1582. *
  1583. * Return value:
  1584. * none
  1585. */
  1586. static void pmcraid_process_ccn(struct pmcraid_cmd *cmd)
  1587. {
  1588. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1589. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  1590. unsigned long lock_flags;
  1591. pinstance->ccn.cmd = NULL;
  1592. pmcraid_return_cmd(cmd);
  1593. /* If driver initiated IOA reset happened while this hcam was pending
  1594. * with IOA, or IOA bringdown sequence is in progress, no need to
  1595. * re-register the hcam
  1596. */
  1597. if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
  1598. atomic_read(&pinstance->ccn.ignore) == 1) {
  1599. return;
  1600. } else if (ioasc) {
  1601. dev_info(&pinstance->pdev->dev,
  1602. "Host RCB (CCN) failed with IOASC: 0x%08X\n", ioasc);
  1603. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  1604. pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1605. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  1606. } else {
  1607. pmcraid_handle_config_change(pinstance);
  1608. }
  1609. }
  1610. /**
  1611. * pmcraid_process_ldn - op done function for an LDN
  1612. * @cmd: pointer to command block
  1613. *
  1614. * Return value
  1615. * none
  1616. */
  1617. static void pmcraid_initiate_reset(struct pmcraid_instance *);
  1618. static void pmcraid_set_timestamp(struct pmcraid_cmd *cmd);
  1619. static void pmcraid_process_ldn(struct pmcraid_cmd *cmd)
  1620. {
  1621. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1622. struct pmcraid_hcam_ldn *ldn_hcam =
  1623. (struct pmcraid_hcam_ldn *)pinstance->ldn.hcam;
  1624. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  1625. u32 fd_ioasc = le32_to_cpu(ldn_hcam->error_log.fd_ioasc);
  1626. unsigned long lock_flags;
  1627. /* return the command block back to freepool */
  1628. pinstance->ldn.cmd = NULL;
  1629. pmcraid_return_cmd(cmd);
  1630. /* If driver initiated IOA reset happened while this hcam was pending
  1631. * with IOA, no need to re-register the hcam as reset engine will do it
  1632. * once reset sequence is complete
  1633. */
  1634. if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
  1635. atomic_read(&pinstance->ccn.ignore) == 1) {
  1636. return;
  1637. } else if (!ioasc) {
  1638. pmcraid_handle_error_log(pinstance);
  1639. if (fd_ioasc == PMCRAID_IOASC_NR_IOA_RESET_REQUIRED) {
  1640. spin_lock_irqsave(pinstance->host->host_lock,
  1641. lock_flags);
  1642. pmcraid_initiate_reset(pinstance);
  1643. spin_unlock_irqrestore(pinstance->host->host_lock,
  1644. lock_flags);
  1645. return;
  1646. }
  1647. if (fd_ioasc == PMCRAID_IOASC_TIME_STAMP_OUT_OF_SYNC) {
  1648. pinstance->timestamp_error = 1;
  1649. pmcraid_set_timestamp(cmd);
  1650. }
  1651. } else {
  1652. dev_info(&pinstance->pdev->dev,
  1653. "Host RCB(LDN) failed with IOASC: 0x%08X\n", ioasc);
  1654. }
  1655. /* send netlink message for HCAM notification if enabled */
  1656. if (!pmcraid_disable_aen)
  1657. pmcraid_notify_ldn(pinstance);
  1658. cmd = pmcraid_init_hcam(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
  1659. if (cmd)
  1660. pmcraid_send_hcam_cmd(cmd);
  1661. }
  1662. /**
  1663. * pmcraid_register_hcams - register HCAMs for CCN and LDN
  1664. *
  1665. * @pinstance: pointer per adapter instance structure
  1666. *
  1667. * Return Value
  1668. * none
  1669. */
  1670. static void pmcraid_register_hcams(struct pmcraid_instance *pinstance)
  1671. {
  1672. pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_CONFIG_CHANGE);
  1673. pmcraid_send_hcam(pinstance, PMCRAID_HCAM_CODE_LOG_DATA);
  1674. }
  1675. /**
  1676. * pmcraid_unregister_hcams - cancel HCAMs registered already
  1677. * @cmd: pointer to command used as part of reset sequence
  1678. */
  1679. static void pmcraid_unregister_hcams(struct pmcraid_cmd *cmd)
  1680. {
  1681. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1682. /* During IOA bringdown, HCAM gets fired and tasklet proceeds with
  1683. * handling hcam response though it is not necessary. In order to
  1684. * prevent this, set 'ignore', so that bring-down sequence doesn't
  1685. * re-send any more hcams
  1686. */
  1687. atomic_set(&pinstance->ccn.ignore, 1);
  1688. atomic_set(&pinstance->ldn.ignore, 1);
  1689. /* If adapter reset was forced as part of runtime reset sequence,
  1690. * start the reset sequence. Reset will be triggered even in case
  1691. * IOA unit_check.
  1692. */
  1693. if ((pinstance->force_ioa_reset && !pinstance->ioa_bringdown) ||
  1694. pinstance->ioa_unit_check) {
  1695. pinstance->force_ioa_reset = 0;
  1696. pinstance->ioa_unit_check = 0;
  1697. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1698. pmcraid_reset_alert(cmd);
  1699. return;
  1700. }
  1701. /* Driver tries to cancel HCAMs by sending ABORT TASK for each HCAM
  1702. * one after the other. So CCN cancellation will be triggered by
  1703. * pmcraid_cancel_ldn itself.
  1704. */
  1705. pmcraid_cancel_ldn(cmd);
  1706. }
  1707. /**
  1708. * pmcraid_reset_enable_ioa - re-enable IOA after a hard reset
  1709. * @pinstance: pointer to adapter instance structure
  1710. * Return Value
  1711. * 1 if TRANSITION_TO_OPERATIONAL is active, otherwise 0
  1712. */
  1713. static void pmcraid_reinit_buffers(struct pmcraid_instance *);
  1714. static int pmcraid_reset_enable_ioa(struct pmcraid_instance *pinstance)
  1715. {
  1716. u32 intrs;
  1717. pmcraid_reinit_buffers(pinstance);
  1718. intrs = pmcraid_read_interrupts(pinstance);
  1719. pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
  1720. if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
  1721. if (!pinstance->interrupt_mode) {
  1722. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  1723. pinstance->int_regs.
  1724. ioa_host_interrupt_mask_reg);
  1725. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  1726. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  1727. }
  1728. return 1;
  1729. } else {
  1730. return 0;
  1731. }
  1732. }
  1733. /**
  1734. * pmcraid_soft_reset - performs a soft reset and makes IOA become ready
  1735. * @cmd : pointer to reset command block
  1736. *
  1737. * Return Value
  1738. * none
  1739. */
  1740. static void pmcraid_soft_reset(struct pmcraid_cmd *cmd)
  1741. {
  1742. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1743. u32 int_reg;
  1744. u32 doorbell;
  1745. /* There will be an interrupt when Transition to Operational bit is
  1746. * set so tasklet would execute next reset task. The timeout handler
  1747. * would re-initiate a reset
  1748. */
  1749. cmd->cmd_done = pmcraid_ioa_reset;
  1750. cmd->timer.data = (unsigned long)cmd;
  1751. cmd->timer.expires = jiffies +
  1752. msecs_to_jiffies(PMCRAID_TRANSOP_TIMEOUT);
  1753. cmd->timer.function = (void (*)(unsigned long))pmcraid_timeout_handler;
  1754. if (!timer_pending(&cmd->timer))
  1755. add_timer(&cmd->timer);
  1756. /* Enable destructive diagnostics on IOA if it is not yet in
  1757. * operational state
  1758. */
  1759. doorbell = DOORBELL_RUNTIME_RESET |
  1760. DOORBELL_ENABLE_DESTRUCTIVE_DIAGS;
  1761. /* Since we do RESET_ALERT and Start BIST we have to again write
  1762. * MSIX Doorbell to indicate the interrupt mode
  1763. */
  1764. if (pinstance->interrupt_mode) {
  1765. iowrite32(DOORBELL_INTR_MODE_MSIX,
  1766. pinstance->int_regs.host_ioa_interrupt_reg);
  1767. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  1768. }
  1769. iowrite32(doorbell, pinstance->int_regs.host_ioa_interrupt_reg);
  1770. ioread32(pinstance->int_regs.host_ioa_interrupt_reg),
  1771. int_reg = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  1772. pmcraid_info("Waiting for IOA to become operational %x:%x\n",
  1773. ioread32(pinstance->int_regs.host_ioa_interrupt_reg),
  1774. int_reg);
  1775. }
  1776. /**
  1777. * pmcraid_get_dump - retrieves IOA dump in case of Unit Check interrupt
  1778. *
  1779. * @pinstance: pointer to adapter instance structure
  1780. *
  1781. * Return Value
  1782. * none
  1783. */
  1784. static void pmcraid_get_dump(struct pmcraid_instance *pinstance)
  1785. {
  1786. pmcraid_info("%s is not yet implemented\n", __func__);
  1787. }
  1788. /**
  1789. * pmcraid_fail_outstanding_cmds - Fails all outstanding ops.
  1790. * @pinstance: pointer to adapter instance structure
  1791. *
  1792. * This function fails all outstanding ops. If they are submitted to IOA
  1793. * already, it sends cancel all messages if IOA is still accepting IOARCBs,
  1794. * otherwise just completes the commands and returns the cmd blocks to free
  1795. * pool.
  1796. *
  1797. * Return value:
  1798. * none
  1799. */
  1800. static void pmcraid_fail_outstanding_cmds(struct pmcraid_instance *pinstance)
  1801. {
  1802. struct pmcraid_cmd *cmd, *temp;
  1803. unsigned long lock_flags;
  1804. /* pending command list is protected by pending_pool_lock. Its
  1805. * traversal must be done as within this lock
  1806. */
  1807. spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
  1808. list_for_each_entry_safe(cmd, temp, &pinstance->pending_cmd_pool,
  1809. free_list) {
  1810. list_del(&cmd->free_list);
  1811. spin_unlock_irqrestore(&pinstance->pending_pool_lock,
  1812. lock_flags);
  1813. cmd->ioa_cb->ioasa.ioasc =
  1814. cpu_to_le32(PMCRAID_IOASC_IOA_WAS_RESET);
  1815. cmd->ioa_cb->ioasa.ilid =
  1816. cpu_to_be32(PMCRAID_DRIVER_ILID);
  1817. /* In case the command timer is still running */
  1818. del_timer(&cmd->timer);
  1819. /* If this is an IO command, complete it by invoking scsi_done
  1820. * function. If this is one of the internal commands other
  1821. * than pmcraid_ioa_reset and HCAM commands invoke cmd_done to
  1822. * complete it
  1823. */
  1824. if (cmd->scsi_cmd) {
  1825. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  1826. __le32 resp = cmd->ioa_cb->ioarcb.response_handle;
  1827. scsi_cmd->result |= DID_ERROR << 16;
  1828. scsi_dma_unmap(scsi_cmd);
  1829. pmcraid_return_cmd(cmd);
  1830. pmcraid_info("failing(%d) CDB[0] = %x result: %x\n",
  1831. le32_to_cpu(resp) >> 2,
  1832. cmd->ioa_cb->ioarcb.cdb[0],
  1833. scsi_cmd->result);
  1834. scsi_cmd->scsi_done(scsi_cmd);
  1835. } else if (cmd->cmd_done == pmcraid_internal_done ||
  1836. cmd->cmd_done == pmcraid_erp_done) {
  1837. cmd->cmd_done(cmd);
  1838. } else if (cmd->cmd_done != pmcraid_ioa_reset &&
  1839. cmd->cmd_done != pmcraid_ioa_shutdown_done) {
  1840. pmcraid_return_cmd(cmd);
  1841. }
  1842. atomic_dec(&pinstance->outstanding_cmds);
  1843. spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
  1844. }
  1845. spin_unlock_irqrestore(&pinstance->pending_pool_lock, lock_flags);
  1846. }
  1847. /**
  1848. * pmcraid_ioa_reset - Implementation of IOA reset logic
  1849. *
  1850. * @cmd: pointer to the cmd block to be used for entire reset process
  1851. *
  1852. * This function executes most of the steps required for IOA reset. This gets
  1853. * called by user threads (modprobe/insmod/rmmod) timer, tasklet and midlayer's
  1854. * 'eh_' thread. Access to variables used for controlling the reset sequence is
  1855. * synchronized using host lock. Various functions called during reset process
  1856. * would make use of a single command block, pointer to which is also stored in
  1857. * adapter instance structure.
  1858. *
  1859. * Return Value
  1860. * None
  1861. */
  1862. static void pmcraid_ioa_reset(struct pmcraid_cmd *cmd)
  1863. {
  1864. struct pmcraid_instance *pinstance = cmd->drv_inst;
  1865. u8 reset_complete = 0;
  1866. pinstance->ioa_reset_in_progress = 1;
  1867. if (pinstance->reset_cmd != cmd) {
  1868. pmcraid_err("reset is called with different command block\n");
  1869. pinstance->reset_cmd = cmd;
  1870. }
  1871. pmcraid_info("reset_engine: state = %d, command = %p\n",
  1872. pinstance->ioa_state, cmd);
  1873. switch (pinstance->ioa_state) {
  1874. case IOA_STATE_DEAD:
  1875. /* If IOA is offline, whatever may be the reset reason, just
  1876. * return. callers might be waiting on the reset wait_q, wake
  1877. * up them
  1878. */
  1879. pmcraid_err("IOA is offline no reset is possible\n");
  1880. reset_complete = 1;
  1881. break;
  1882. case IOA_STATE_IN_BRINGDOWN:
  1883. /* we enter here, once ioa shutdown command is processed by IOA
  1884. * Alert IOA for a possible reset. If reset alert fails, IOA
  1885. * goes through hard-reset
  1886. */
  1887. pmcraid_disable_interrupts(pinstance, ~0);
  1888. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1889. pmcraid_reset_alert(cmd);
  1890. break;
  1891. case IOA_STATE_UNKNOWN:
  1892. /* We may be called during probe or resume. Some pre-processing
  1893. * is required for prior to reset
  1894. */
  1895. scsi_block_requests(pinstance->host);
  1896. /* If asked to reset while IOA was processing responses or
  1897. * there are any error responses then IOA may require
  1898. * hard-reset.
  1899. */
  1900. if (pinstance->ioa_hard_reset == 0) {
  1901. if (ioread32(pinstance->ioa_status) &
  1902. INTRS_TRANSITION_TO_OPERATIONAL) {
  1903. pmcraid_info("sticky bit set, bring-up\n");
  1904. pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
  1905. pmcraid_reinit_cmdblk(cmd);
  1906. pmcraid_identify_hrrq(cmd);
  1907. } else {
  1908. pinstance->ioa_state = IOA_STATE_IN_SOFT_RESET;
  1909. pmcraid_soft_reset(cmd);
  1910. }
  1911. } else {
  1912. /* Alert IOA of a possible reset and wait for critical
  1913. * operation in progress bit to reset
  1914. */
  1915. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1916. pmcraid_reset_alert(cmd);
  1917. }
  1918. break;
  1919. case IOA_STATE_IN_RESET_ALERT:
  1920. /* If critical operation in progress bit is reset or wait gets
  1921. * timed out, reset proceeds with starting BIST on the IOA.
  1922. * pmcraid_ioa_hard_reset keeps a count of reset attempts. If
  1923. * they are 3 or more, reset engine marks IOA dead and returns
  1924. */
  1925. pinstance->ioa_state = IOA_STATE_IN_HARD_RESET;
  1926. pmcraid_start_bist(cmd);
  1927. break;
  1928. case IOA_STATE_IN_HARD_RESET:
  1929. pinstance->ioa_reset_attempts++;
  1930. /* retry reset if we haven't reached maximum allowed limit */
  1931. if (pinstance->ioa_reset_attempts > PMCRAID_RESET_ATTEMPTS) {
  1932. pinstance->ioa_reset_attempts = 0;
  1933. pmcraid_err("IOA didn't respond marking it as dead\n");
  1934. pinstance->ioa_state = IOA_STATE_DEAD;
  1935. if (pinstance->ioa_bringdown)
  1936. pmcraid_notify_ioastate(pinstance,
  1937. PMC_DEVICE_EVENT_SHUTDOWN_FAILED);
  1938. else
  1939. pmcraid_notify_ioastate(pinstance,
  1940. PMC_DEVICE_EVENT_RESET_FAILED);
  1941. reset_complete = 1;
  1942. break;
  1943. }
  1944. /* Once either bist or pci reset is done, restore PCI config
  1945. * space. If this fails, proceed with hard reset again
  1946. */
  1947. pci_restore_state(pinstance->pdev);
  1948. /* fail all pending commands */
  1949. pmcraid_fail_outstanding_cmds(pinstance);
  1950. /* check if unit check is active, if so extract dump */
  1951. if (pinstance->ioa_unit_check) {
  1952. pmcraid_info("unit check is active\n");
  1953. pinstance->ioa_unit_check = 0;
  1954. pmcraid_get_dump(pinstance);
  1955. pinstance->ioa_reset_attempts--;
  1956. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  1957. pmcraid_reset_alert(cmd);
  1958. break;
  1959. }
  1960. /* if the reset reason is to bring-down the ioa, we might be
  1961. * done with the reset restore pci_config_space and complete
  1962. * the reset
  1963. */
  1964. if (pinstance->ioa_bringdown) {
  1965. pmcraid_info("bringing down the adapter\n");
  1966. pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
  1967. pinstance->ioa_bringdown = 0;
  1968. pinstance->ioa_state = IOA_STATE_UNKNOWN;
  1969. pmcraid_notify_ioastate(pinstance,
  1970. PMC_DEVICE_EVENT_SHUTDOWN_SUCCESS);
  1971. reset_complete = 1;
  1972. } else {
  1973. /* bring-up IOA, so proceed with soft reset
  1974. * Reinitialize hrrq_buffers and their indices also
  1975. * enable interrupts after a pci_restore_state
  1976. */
  1977. if (pmcraid_reset_enable_ioa(pinstance)) {
  1978. pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
  1979. pmcraid_info("bringing up the adapter\n");
  1980. pmcraid_reinit_cmdblk(cmd);
  1981. pmcraid_identify_hrrq(cmd);
  1982. } else {
  1983. pinstance->ioa_state = IOA_STATE_IN_SOFT_RESET;
  1984. pmcraid_soft_reset(cmd);
  1985. }
  1986. }
  1987. break;
  1988. case IOA_STATE_IN_SOFT_RESET:
  1989. /* TRANSITION TO OPERATIONAL is on so start initialization
  1990. * sequence
  1991. */
  1992. pmcraid_info("In softreset proceeding with bring-up\n");
  1993. pinstance->ioa_state = IOA_STATE_IN_BRINGUP;
  1994. /* Initialization commands start with HRRQ identification. From
  1995. * now on tasklet completes most of the commands as IOA is up
  1996. * and intrs are enabled
  1997. */
  1998. pmcraid_identify_hrrq(cmd);
  1999. break;
  2000. case IOA_STATE_IN_BRINGUP:
  2001. /* we are done with bringing up of IOA, change the ioa_state to
  2002. * operational and wake up any waiters
  2003. */
  2004. pinstance->ioa_state = IOA_STATE_OPERATIONAL;
  2005. reset_complete = 1;
  2006. break;
  2007. case IOA_STATE_OPERATIONAL:
  2008. default:
  2009. /* When IOA is operational and a reset is requested, check for
  2010. * the reset reason. If reset is to bring down IOA, unregister
  2011. * HCAMs and initiate shutdown; if adapter reset is forced then
  2012. * restart reset sequence again
  2013. */
  2014. if (pinstance->ioa_shutdown_type == SHUTDOWN_NONE &&
  2015. pinstance->force_ioa_reset == 0) {
  2016. pmcraid_notify_ioastate(pinstance,
  2017. PMC_DEVICE_EVENT_RESET_SUCCESS);
  2018. reset_complete = 1;
  2019. } else {
  2020. if (pinstance->ioa_shutdown_type != SHUTDOWN_NONE)
  2021. pinstance->ioa_state = IOA_STATE_IN_BRINGDOWN;
  2022. pmcraid_reinit_cmdblk(cmd);
  2023. pmcraid_unregister_hcams(cmd);
  2024. }
  2025. break;
  2026. }
  2027. /* reset will be completed if ioa_state is either DEAD or UNKNOWN or
  2028. * OPERATIONAL. Reset all control variables used during reset, wake up
  2029. * any waiting threads and let the SCSI mid-layer send commands. Note
  2030. * that host_lock must be held before invoking scsi_report_bus_reset.
  2031. */
  2032. if (reset_complete) {
  2033. pinstance->ioa_reset_in_progress = 0;
  2034. pinstance->ioa_reset_attempts = 0;
  2035. pinstance->reset_cmd = NULL;
  2036. pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
  2037. pinstance->ioa_bringdown = 0;
  2038. pmcraid_return_cmd(cmd);
  2039. /* If target state is to bring up the adapter, proceed with
  2040. * hcam registration and resource exposure to mid-layer.
  2041. */
  2042. if (pinstance->ioa_state == IOA_STATE_OPERATIONAL)
  2043. pmcraid_register_hcams(pinstance);
  2044. wake_up_all(&pinstance->reset_wait_q);
  2045. }
  2046. return;
  2047. }
  2048. /**
  2049. * pmcraid_initiate_reset - initiates reset sequence. This is called from
  2050. * ISR/tasklet during error interrupts including IOA unit check. If reset
  2051. * is already in progress, it just returns, otherwise initiates IOA reset
  2052. * to bring IOA up to operational state.
  2053. *
  2054. * @pinstance: pointer to adapter instance structure
  2055. *
  2056. * Return value
  2057. * none
  2058. */
  2059. static void pmcraid_initiate_reset(struct pmcraid_instance *pinstance)
  2060. {
  2061. struct pmcraid_cmd *cmd;
  2062. /* If the reset is already in progress, just return, otherwise start
  2063. * reset sequence and return
  2064. */
  2065. if (!pinstance->ioa_reset_in_progress) {
  2066. scsi_block_requests(pinstance->host);
  2067. cmd = pmcraid_get_free_cmd(pinstance);
  2068. if (cmd == NULL) {
  2069. pmcraid_err("no cmnd blocks for initiate_reset\n");
  2070. return;
  2071. }
  2072. pinstance->ioa_shutdown_type = SHUTDOWN_NONE;
  2073. pinstance->reset_cmd = cmd;
  2074. pinstance->force_ioa_reset = 1;
  2075. pmcraid_notify_ioastate(pinstance,
  2076. PMC_DEVICE_EVENT_RESET_START);
  2077. pmcraid_ioa_reset(cmd);
  2078. }
  2079. }
  2080. /**
  2081. * pmcraid_reset_reload - utility routine for doing IOA reset either to bringup
  2082. * or bringdown IOA
  2083. * @pinstance: pointer adapter instance structure
  2084. * @shutdown_type: shutdown type to be used NONE, NORMAL or ABRREV
  2085. * @target_state: expected target state after reset
  2086. *
  2087. * Note: This command initiates reset and waits for its completion. Hence this
  2088. * should not be called from isr/timer/tasklet functions (timeout handlers,
  2089. * error response handlers and interrupt handlers).
  2090. *
  2091. * Return Value
  2092. * 1 in case ioa_state is not target_state, 0 otherwise.
  2093. */
  2094. static int pmcraid_reset_reload(
  2095. struct pmcraid_instance *pinstance,
  2096. u8 shutdown_type,
  2097. u8 target_state
  2098. )
  2099. {
  2100. struct pmcraid_cmd *reset_cmd = NULL;
  2101. unsigned long lock_flags;
  2102. int reset = 1;
  2103. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  2104. if (pinstance->ioa_reset_in_progress) {
  2105. pmcraid_info("reset_reload: reset is already in progress\n");
  2106. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2107. wait_event(pinstance->reset_wait_q,
  2108. !pinstance->ioa_reset_in_progress);
  2109. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  2110. if (pinstance->ioa_state == IOA_STATE_DEAD) {
  2111. spin_unlock_irqrestore(pinstance->host->host_lock,
  2112. lock_flags);
  2113. pmcraid_info("reset_reload: IOA is dead\n");
  2114. return reset;
  2115. } else if (pinstance->ioa_state == target_state) {
  2116. reset = 0;
  2117. }
  2118. }
  2119. if (reset) {
  2120. pmcraid_info("reset_reload: proceeding with reset\n");
  2121. scsi_block_requests(pinstance->host);
  2122. reset_cmd = pmcraid_get_free_cmd(pinstance);
  2123. if (reset_cmd == NULL) {
  2124. pmcraid_err("no free cmnd for reset_reload\n");
  2125. spin_unlock_irqrestore(pinstance->host->host_lock,
  2126. lock_flags);
  2127. return reset;
  2128. }
  2129. if (shutdown_type == SHUTDOWN_NORMAL)
  2130. pinstance->ioa_bringdown = 1;
  2131. pinstance->ioa_shutdown_type = shutdown_type;
  2132. pinstance->reset_cmd = reset_cmd;
  2133. pinstance->force_ioa_reset = reset;
  2134. pmcraid_info("reset_reload: initiating reset\n");
  2135. pmcraid_ioa_reset(reset_cmd);
  2136. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2137. pmcraid_info("reset_reload: waiting for reset to complete\n");
  2138. wait_event(pinstance->reset_wait_q,
  2139. !pinstance->ioa_reset_in_progress);
  2140. pmcraid_info("reset_reload: reset is complete !!\n");
  2141. scsi_unblock_requests(pinstance->host);
  2142. if (pinstance->ioa_state == target_state)
  2143. reset = 0;
  2144. }
  2145. return reset;
  2146. }
  2147. /**
  2148. * pmcraid_reset_bringdown - wrapper over pmcraid_reset_reload to bringdown IOA
  2149. *
  2150. * @pinstance: pointer to adapter instance structure
  2151. *
  2152. * Return Value
  2153. * whatever is returned from pmcraid_reset_reload
  2154. */
  2155. static int pmcraid_reset_bringdown(struct pmcraid_instance *pinstance)
  2156. {
  2157. return pmcraid_reset_reload(pinstance,
  2158. SHUTDOWN_NORMAL,
  2159. IOA_STATE_UNKNOWN);
  2160. }
  2161. /**
  2162. * pmcraid_reset_bringup - wrapper over pmcraid_reset_reload to bring up IOA
  2163. *
  2164. * @pinstance: pointer to adapter instance structure
  2165. *
  2166. * Return Value
  2167. * whatever is returned from pmcraid_reset_reload
  2168. */
  2169. static int pmcraid_reset_bringup(struct pmcraid_instance *pinstance)
  2170. {
  2171. pmcraid_notify_ioastate(pinstance, PMC_DEVICE_EVENT_RESET_START);
  2172. return pmcraid_reset_reload(pinstance,
  2173. SHUTDOWN_NONE,
  2174. IOA_STATE_OPERATIONAL);
  2175. }
  2176. /**
  2177. * pmcraid_request_sense - Send request sense to a device
  2178. * @cmd: pmcraid command struct
  2179. *
  2180. * This function sends a request sense to a device as a result of a check
  2181. * condition. This method re-uses the same command block that failed earlier.
  2182. */
  2183. static void pmcraid_request_sense(struct pmcraid_cmd *cmd)
  2184. {
  2185. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  2186. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  2187. /* allocate DMAable memory for sense buffers */
  2188. cmd->sense_buffer = pci_alloc_consistent(cmd->drv_inst->pdev,
  2189. SCSI_SENSE_BUFFERSIZE,
  2190. &cmd->sense_buffer_dma);
  2191. if (cmd->sense_buffer == NULL) {
  2192. pmcraid_err
  2193. ("couldn't allocate sense buffer for request sense\n");
  2194. pmcraid_erp_done(cmd);
  2195. return;
  2196. }
  2197. /* re-use the command block */
  2198. memset(&cmd->ioa_cb->ioasa, 0, sizeof(struct pmcraid_ioasa));
  2199. memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
  2200. ioarcb->request_flags0 = (SYNC_COMPLETE |
  2201. NO_LINK_DESCS |
  2202. INHIBIT_UL_CHECK);
  2203. ioarcb->request_type = REQ_TYPE_SCSI;
  2204. ioarcb->cdb[0] = REQUEST_SENSE;
  2205. ioarcb->cdb[4] = SCSI_SENSE_BUFFERSIZE;
  2206. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  2207. offsetof(struct pmcraid_ioarcb,
  2208. add_data.u.ioadl[0]));
  2209. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  2210. ioarcb->data_transfer_length = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
  2211. ioadl->address = cpu_to_le64(cmd->sense_buffer_dma);
  2212. ioadl->data_len = cpu_to_le32(SCSI_SENSE_BUFFERSIZE);
  2213. ioadl->flags = IOADL_FLAGS_LAST_DESC;
  2214. /* request sense might be called as part of error response processing
  2215. * which runs in tasklets context. It is possible that mid-layer might
  2216. * schedule queuecommand during this time, hence, writting to IOARRIN
  2217. * must be protect by host_lock
  2218. */
  2219. pmcraid_send_cmd(cmd, pmcraid_erp_done,
  2220. PMCRAID_REQUEST_SENSE_TIMEOUT,
  2221. pmcraid_timeout_handler);
  2222. }
  2223. /**
  2224. * pmcraid_cancel_all - cancel all outstanding IOARCBs as part of error recovery
  2225. * @cmd: command that failed
  2226. * @sense: true if request_sense is required after cancel all
  2227. *
  2228. * This function sends a cancel all to a device to clear the queue.
  2229. */
  2230. static void pmcraid_cancel_all(struct pmcraid_cmd *cmd, u32 sense)
  2231. {
  2232. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  2233. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  2234. struct pmcraid_resource_entry *res = scsi_cmd->device->hostdata;
  2235. void (*cmd_done) (struct pmcraid_cmd *) = sense ? pmcraid_erp_done
  2236. : pmcraid_request_sense;
  2237. memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
  2238. ioarcb->request_flags0 = SYNC_OVERRIDE;
  2239. ioarcb->request_type = REQ_TYPE_IOACMD;
  2240. ioarcb->cdb[0] = PMCRAID_CANCEL_ALL_REQUESTS;
  2241. if (RES_IS_GSCSI(res->cfg_entry))
  2242. ioarcb->cdb[1] = PMCRAID_SYNC_COMPLETE_AFTER_CANCEL;
  2243. ioarcb->ioadl_bus_addr = 0;
  2244. ioarcb->ioadl_length = 0;
  2245. ioarcb->data_transfer_length = 0;
  2246. ioarcb->ioarcb_bus_addr &= (~0x1FULL);
  2247. /* writing to IOARRIN must be protected by host_lock, as mid-layer
  2248. * schedule queuecommand while we are doing this
  2249. */
  2250. pmcraid_send_cmd(cmd, cmd_done,
  2251. PMCRAID_REQUEST_SENSE_TIMEOUT,
  2252. pmcraid_timeout_handler);
  2253. }
  2254. /**
  2255. * pmcraid_frame_auto_sense: frame fixed format sense information
  2256. *
  2257. * @cmd: pointer to failing command block
  2258. *
  2259. * Return value
  2260. * none
  2261. */
  2262. static void pmcraid_frame_auto_sense(struct pmcraid_cmd *cmd)
  2263. {
  2264. u8 *sense_buf = cmd->scsi_cmd->sense_buffer;
  2265. struct pmcraid_resource_entry *res = cmd->scsi_cmd->device->hostdata;
  2266. struct pmcraid_ioasa *ioasa = &cmd->ioa_cb->ioasa;
  2267. u32 ioasc = le32_to_cpu(ioasa->ioasc);
  2268. u32 failing_lba = 0;
  2269. memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
  2270. cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
  2271. if (RES_IS_VSET(res->cfg_entry) &&
  2272. ioasc == PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC &&
  2273. ioasa->u.vset.failing_lba_hi != 0) {
  2274. sense_buf[0] = 0x72;
  2275. sense_buf[1] = PMCRAID_IOASC_SENSE_KEY(ioasc);
  2276. sense_buf[2] = PMCRAID_IOASC_SENSE_CODE(ioasc);
  2277. sense_buf[3] = PMCRAID_IOASC_SENSE_QUAL(ioasc);
  2278. sense_buf[7] = 12;
  2279. sense_buf[8] = 0;
  2280. sense_buf[9] = 0x0A;
  2281. sense_buf[10] = 0x80;
  2282. failing_lba = le32_to_cpu(ioasa->u.vset.failing_lba_hi);
  2283. sense_buf[12] = (failing_lba & 0xff000000) >> 24;
  2284. sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
  2285. sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
  2286. sense_buf[15] = failing_lba & 0x000000ff;
  2287. failing_lba = le32_to_cpu(ioasa->u.vset.failing_lba_lo);
  2288. sense_buf[16] = (failing_lba & 0xff000000) >> 24;
  2289. sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
  2290. sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
  2291. sense_buf[19] = failing_lba & 0x000000ff;
  2292. } else {
  2293. sense_buf[0] = 0x70;
  2294. sense_buf[2] = PMCRAID_IOASC_SENSE_KEY(ioasc);
  2295. sense_buf[12] = PMCRAID_IOASC_SENSE_CODE(ioasc);
  2296. sense_buf[13] = PMCRAID_IOASC_SENSE_QUAL(ioasc);
  2297. if (ioasc == PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC) {
  2298. if (RES_IS_VSET(res->cfg_entry))
  2299. failing_lba =
  2300. le32_to_cpu(ioasa->u.
  2301. vset.failing_lba_lo);
  2302. sense_buf[0] |= 0x80;
  2303. sense_buf[3] = (failing_lba >> 24) & 0xff;
  2304. sense_buf[4] = (failing_lba >> 16) & 0xff;
  2305. sense_buf[5] = (failing_lba >> 8) & 0xff;
  2306. sense_buf[6] = failing_lba & 0xff;
  2307. }
  2308. sense_buf[7] = 6; /* additional length */
  2309. }
  2310. }
  2311. /**
  2312. * pmcraid_error_handler - Error response handlers for a SCSI op
  2313. * @cmd: pointer to pmcraid_cmd that has failed
  2314. *
  2315. * This function determines whether or not to initiate ERP on the affected
  2316. * device. This is called from a tasklet, which doesn't hold any locks.
  2317. *
  2318. * Return value:
  2319. * 0 it caller can complete the request, otherwise 1 where in error
  2320. * handler itself completes the request and returns the command block
  2321. * back to free-pool
  2322. */
  2323. static int pmcraid_error_handler(struct pmcraid_cmd *cmd)
  2324. {
  2325. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  2326. struct pmcraid_resource_entry *res = scsi_cmd->device->hostdata;
  2327. struct pmcraid_instance *pinstance = cmd->drv_inst;
  2328. struct pmcraid_ioasa *ioasa = &cmd->ioa_cb->ioasa;
  2329. u32 ioasc = le32_to_cpu(ioasa->ioasc);
  2330. u32 masked_ioasc = ioasc & PMCRAID_IOASC_SENSE_MASK;
  2331. u32 sense_copied = 0;
  2332. if (!res) {
  2333. pmcraid_info("resource pointer is NULL\n");
  2334. return 0;
  2335. }
  2336. /* If this was a SCSI read/write command keep count of errors */
  2337. if (SCSI_CMD_TYPE(scsi_cmd->cmnd[0]) == SCSI_READ_CMD)
  2338. atomic_inc(&res->read_failures);
  2339. else if (SCSI_CMD_TYPE(scsi_cmd->cmnd[0]) == SCSI_WRITE_CMD)
  2340. atomic_inc(&res->write_failures);
  2341. if (!RES_IS_GSCSI(res->cfg_entry) &&
  2342. masked_ioasc != PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR) {
  2343. pmcraid_frame_auto_sense(cmd);
  2344. }
  2345. /* Log IOASC/IOASA information based on user settings */
  2346. pmcraid_ioasc_logger(ioasc, cmd);
  2347. switch (masked_ioasc) {
  2348. case PMCRAID_IOASC_AC_TERMINATED_BY_HOST:
  2349. scsi_cmd->result |= (DID_ABORT << 16);
  2350. break;
  2351. case PMCRAID_IOASC_IR_INVALID_RESOURCE_HANDLE:
  2352. case PMCRAID_IOASC_HW_CANNOT_COMMUNICATE:
  2353. scsi_cmd->result |= (DID_NO_CONNECT << 16);
  2354. break;
  2355. case PMCRAID_IOASC_NR_SYNC_REQUIRED:
  2356. res->sync_reqd = 1;
  2357. scsi_cmd->result |= (DID_IMM_RETRY << 16);
  2358. break;
  2359. case PMCRAID_IOASC_ME_READ_ERROR_NO_REALLOC:
  2360. scsi_cmd->result |= (DID_PASSTHROUGH << 16);
  2361. break;
  2362. case PMCRAID_IOASC_UA_BUS_WAS_RESET:
  2363. case PMCRAID_IOASC_UA_BUS_WAS_RESET_BY_OTHER:
  2364. if (!res->reset_progress)
  2365. scsi_report_bus_reset(pinstance->host,
  2366. scsi_cmd->device->channel);
  2367. scsi_cmd->result |= (DID_ERROR << 16);
  2368. break;
  2369. case PMCRAID_IOASC_HW_DEVICE_BUS_STATUS_ERROR:
  2370. scsi_cmd->result |= PMCRAID_IOASC_SENSE_STATUS(ioasc);
  2371. res->sync_reqd = 1;
  2372. /* if check_condition is not active return with error otherwise
  2373. * get/frame the sense buffer
  2374. */
  2375. if (PMCRAID_IOASC_SENSE_STATUS(ioasc) !=
  2376. SAM_STAT_CHECK_CONDITION &&
  2377. PMCRAID_IOASC_SENSE_STATUS(ioasc) != SAM_STAT_ACA_ACTIVE)
  2378. return 0;
  2379. /* If we have auto sense data as part of IOASA pass it to
  2380. * mid-layer
  2381. */
  2382. if (ioasa->auto_sense_length != 0) {
  2383. short sense_len = ioasa->auto_sense_length;
  2384. int data_size = min_t(u16, le16_to_cpu(sense_len),
  2385. SCSI_SENSE_BUFFERSIZE);
  2386. memcpy(scsi_cmd->sense_buffer,
  2387. ioasa->sense_data,
  2388. data_size);
  2389. sense_copied = 1;
  2390. }
  2391. if (RES_IS_GSCSI(res->cfg_entry))
  2392. pmcraid_cancel_all(cmd, sense_copied);
  2393. else if (sense_copied)
  2394. pmcraid_erp_done(cmd);
  2395. else
  2396. pmcraid_request_sense(cmd);
  2397. return 1;
  2398. case PMCRAID_IOASC_NR_INIT_CMD_REQUIRED:
  2399. break;
  2400. default:
  2401. if (PMCRAID_IOASC_SENSE_KEY(ioasc) > RECOVERED_ERROR)
  2402. scsi_cmd->result |= (DID_ERROR << 16);
  2403. break;
  2404. }
  2405. return 0;
  2406. }
  2407. /**
  2408. * pmcraid_reset_device - device reset handler functions
  2409. *
  2410. * @scsi_cmd: scsi command struct
  2411. * @modifier: reset modifier indicating the reset sequence to be performed
  2412. *
  2413. * This function issues a device reset to the affected device.
  2414. * A LUN reset will be sent to the device first. If that does
  2415. * not work, a target reset will be sent.
  2416. *
  2417. * Return value:
  2418. * SUCCESS / FAILED
  2419. */
  2420. static int pmcraid_reset_device(
  2421. struct scsi_cmnd *scsi_cmd,
  2422. unsigned long timeout,
  2423. u8 modifier
  2424. )
  2425. {
  2426. struct pmcraid_cmd *cmd;
  2427. struct pmcraid_instance *pinstance;
  2428. struct pmcraid_resource_entry *res;
  2429. struct pmcraid_ioarcb *ioarcb;
  2430. unsigned long lock_flags;
  2431. u32 ioasc;
  2432. pinstance =
  2433. (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
  2434. res = scsi_cmd->device->hostdata;
  2435. if (!res) {
  2436. sdev_printk(KERN_ERR, scsi_cmd->device,
  2437. "reset_device: NULL resource pointer\n");
  2438. return FAILED;
  2439. }
  2440. /* If adapter is currently going through reset/reload, return failed.
  2441. * This will force the mid-layer to call _eh_bus/host reset, which
  2442. * will then go to sleep and wait for the reset to complete
  2443. */
  2444. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  2445. if (pinstance->ioa_reset_in_progress ||
  2446. pinstance->ioa_state == IOA_STATE_DEAD) {
  2447. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2448. return FAILED;
  2449. }
  2450. res->reset_progress = 1;
  2451. pmcraid_info("Resetting %s resource with addr %x\n",
  2452. ((modifier & RESET_DEVICE_LUN) ? "LUN" :
  2453. ((modifier & RESET_DEVICE_TARGET) ? "TARGET" : "BUS")),
  2454. le32_to_cpu(res->cfg_entry.resource_address));
  2455. /* get a free cmd block */
  2456. cmd = pmcraid_get_free_cmd(pinstance);
  2457. if (cmd == NULL) {
  2458. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2459. pmcraid_err("%s: no cmd blocks are available\n", __func__);
  2460. return FAILED;
  2461. }
  2462. ioarcb = &cmd->ioa_cb->ioarcb;
  2463. ioarcb->resource_handle = res->cfg_entry.resource_handle;
  2464. ioarcb->request_type = REQ_TYPE_IOACMD;
  2465. ioarcb->cdb[0] = PMCRAID_RESET_DEVICE;
  2466. /* Initialize reset modifier bits */
  2467. if (modifier)
  2468. modifier = ENABLE_RESET_MODIFIER | modifier;
  2469. ioarcb->cdb[1] = modifier;
  2470. init_completion(&cmd->wait_for_completion);
  2471. cmd->completion_req = 1;
  2472. pmcraid_info("cmd(CDB[0] = %x) for %x with index = %d\n",
  2473. cmd->ioa_cb->ioarcb.cdb[0],
  2474. le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle),
  2475. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2);
  2476. pmcraid_send_cmd(cmd,
  2477. pmcraid_internal_done,
  2478. timeout,
  2479. pmcraid_timeout_handler);
  2480. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  2481. /* RESET_DEVICE command completes after all pending IOARCBs are
  2482. * completed. Once this command is completed, pmcraind_internal_done
  2483. * will wake up the 'completion' queue.
  2484. */
  2485. wait_for_completion(&cmd->wait_for_completion);
  2486. /* complete the command here itself and return the command block
  2487. * to free list
  2488. */
  2489. pmcraid_return_cmd(cmd);
  2490. res->reset_progress = 0;
  2491. ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  2492. /* set the return value based on the returned ioasc */
  2493. return PMCRAID_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
  2494. }
  2495. /**
  2496. * _pmcraid_io_done - helper for pmcraid_io_done function
  2497. *
  2498. * @cmd: pointer to pmcraid command struct
  2499. * @reslen: residual data length to be set in the ioasa
  2500. * @ioasc: ioasc either returned by IOA or set by driver itself.
  2501. *
  2502. * This function is invoked by pmcraid_io_done to complete mid-layer
  2503. * scsi ops.
  2504. *
  2505. * Return value:
  2506. * 0 if caller is required to return it to free_pool. Returns 1 if
  2507. * caller need not worry about freeing command block as error handler
  2508. * will take care of that.
  2509. */
  2510. static int _pmcraid_io_done(struct pmcraid_cmd *cmd, int reslen, int ioasc)
  2511. {
  2512. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  2513. int rc = 0;
  2514. scsi_set_resid(scsi_cmd, reslen);
  2515. pmcraid_info("response(%d) CDB[0] = %x ioasc:result: %x:%x\n",
  2516. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
  2517. cmd->ioa_cb->ioarcb.cdb[0],
  2518. ioasc, scsi_cmd->result);
  2519. if (PMCRAID_IOASC_SENSE_KEY(ioasc) != 0)
  2520. rc = pmcraid_error_handler(cmd);
  2521. if (rc == 0) {
  2522. scsi_dma_unmap(scsi_cmd);
  2523. scsi_cmd->scsi_done(scsi_cmd);
  2524. }
  2525. return rc;
  2526. }
  2527. /**
  2528. * pmcraid_io_done - SCSI completion function
  2529. *
  2530. * @cmd: pointer to pmcraid command struct
  2531. *
  2532. * This function is invoked by tasklet/mid-layer error handler to completing
  2533. * the SCSI ops sent from mid-layer.
  2534. *
  2535. * Return value
  2536. * none
  2537. */
  2538. static void pmcraid_io_done(struct pmcraid_cmd *cmd)
  2539. {
  2540. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  2541. u32 reslen = le32_to_cpu(cmd->ioa_cb->ioasa.residual_data_length);
  2542. if (_pmcraid_io_done(cmd, reslen, ioasc) == 0)
  2543. pmcraid_return_cmd(cmd);
  2544. }
  2545. /**
  2546. * pmcraid_abort_cmd - Aborts a single IOARCB already submitted to IOA
  2547. *
  2548. * @cmd: command block of the command to be aborted
  2549. *
  2550. * Return Value:
  2551. * returns pointer to command structure used as cancelling cmd
  2552. */
  2553. static struct pmcraid_cmd *pmcraid_abort_cmd(struct pmcraid_cmd *cmd)
  2554. {
  2555. struct pmcraid_cmd *cancel_cmd;
  2556. struct pmcraid_instance *pinstance;
  2557. struct pmcraid_resource_entry *res;
  2558. pinstance = (struct pmcraid_instance *)cmd->drv_inst;
  2559. res = cmd->scsi_cmd->device->hostdata;
  2560. cancel_cmd = pmcraid_get_free_cmd(pinstance);
  2561. if (cancel_cmd == NULL) {
  2562. pmcraid_err("%s: no cmd blocks are available\n", __func__);
  2563. return NULL;
  2564. }
  2565. pmcraid_prepare_cancel_cmd(cancel_cmd, cmd);
  2566. pmcraid_info("aborting command CDB[0]= %x with index = %d\n",
  2567. cmd->ioa_cb->ioarcb.cdb[0],
  2568. cmd->ioa_cb->ioarcb.response_handle >> 2);
  2569. init_completion(&cancel_cmd->wait_for_completion);
  2570. cancel_cmd->completion_req = 1;
  2571. pmcraid_info("command (%d) CDB[0] = %x for %x\n",
  2572. le32_to_cpu(cancel_cmd->ioa_cb->ioarcb.response_handle) >> 2,
  2573. cancel_cmd->ioa_cb->ioarcb.cdb[0],
  2574. le32_to_cpu(cancel_cmd->ioa_cb->ioarcb.resource_handle));
  2575. pmcraid_send_cmd(cancel_cmd,
  2576. pmcraid_internal_done,
  2577. PMCRAID_INTERNAL_TIMEOUT,
  2578. pmcraid_timeout_handler);
  2579. return cancel_cmd;
  2580. }
  2581. /**
  2582. * pmcraid_abort_complete - Waits for ABORT TASK completion
  2583. *
  2584. * @cancel_cmd: command block use as cancelling command
  2585. *
  2586. * Return Value:
  2587. * returns SUCCESS if ABORT TASK has good completion
  2588. * otherwise FAILED
  2589. */
  2590. static int pmcraid_abort_complete(struct pmcraid_cmd *cancel_cmd)
  2591. {
  2592. struct pmcraid_resource_entry *res;
  2593. u32 ioasc;
  2594. wait_for_completion(&cancel_cmd->wait_for_completion);
  2595. res = cancel_cmd->res;
  2596. cancel_cmd->res = NULL;
  2597. ioasc = le32_to_cpu(cancel_cmd->ioa_cb->ioasa.ioasc);
  2598. /* If the abort task is not timed out we will get a Good completion
  2599. * as sense_key, otherwise we may get one the following responses
  2600. * due to subsequent bus reset or device reset. In case IOASC is
  2601. * NR_SYNC_REQUIRED, set sync_reqd flag for the corresponding resource
  2602. */
  2603. if (ioasc == PMCRAID_IOASC_UA_BUS_WAS_RESET ||
  2604. ioasc == PMCRAID_IOASC_NR_SYNC_REQUIRED) {
  2605. if (ioasc == PMCRAID_IOASC_NR_SYNC_REQUIRED)
  2606. res->sync_reqd = 1;
  2607. ioasc = 0;
  2608. }
  2609. /* complete the command here itself */
  2610. pmcraid_return_cmd(cancel_cmd);
  2611. return PMCRAID_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS;
  2612. }
  2613. /**
  2614. * pmcraid_eh_abort_handler - entry point for aborting a single task on errors
  2615. *
  2616. * @scsi_cmd: scsi command struct given by mid-layer. When this is called
  2617. * mid-layer ensures that no other commands are queued. This
  2618. * never gets called under interrupt, but a separate eh thread.
  2619. *
  2620. * Return value:
  2621. * SUCCESS / FAILED
  2622. */
  2623. static int pmcraid_eh_abort_handler(struct scsi_cmnd *scsi_cmd)
  2624. {
  2625. struct pmcraid_instance *pinstance;
  2626. struct pmcraid_cmd *cmd;
  2627. struct pmcraid_resource_entry *res;
  2628. unsigned long host_lock_flags;
  2629. unsigned long pending_lock_flags;
  2630. struct pmcraid_cmd *cancel_cmd = NULL;
  2631. int cmd_found = 0;
  2632. int rc = FAILED;
  2633. pinstance =
  2634. (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
  2635. scmd_printk(KERN_INFO, scsi_cmd,
  2636. "I/O command timed out, aborting it.\n");
  2637. res = scsi_cmd->device->hostdata;
  2638. if (res == NULL)
  2639. return rc;
  2640. /* If we are currently going through reset/reload, return failed.
  2641. * This will force the mid-layer to eventually call
  2642. * pmcraid_eh_host_reset which will then go to sleep and wait for the
  2643. * reset to complete
  2644. */
  2645. spin_lock_irqsave(pinstance->host->host_lock, host_lock_flags);
  2646. if (pinstance->ioa_reset_in_progress ||
  2647. pinstance->ioa_state == IOA_STATE_DEAD) {
  2648. spin_unlock_irqrestore(pinstance->host->host_lock,
  2649. host_lock_flags);
  2650. return rc;
  2651. }
  2652. /* loop over pending cmd list to find cmd corresponding to this
  2653. * scsi_cmd. Note that this command might not have been completed
  2654. * already. locking: all pending commands are protected with
  2655. * pending_pool_lock.
  2656. */
  2657. spin_lock_irqsave(&pinstance->pending_pool_lock, pending_lock_flags);
  2658. list_for_each_entry(cmd, &pinstance->pending_cmd_pool, free_list) {
  2659. if (cmd->scsi_cmd == scsi_cmd) {
  2660. cmd_found = 1;
  2661. break;
  2662. }
  2663. }
  2664. spin_unlock_irqrestore(&pinstance->pending_pool_lock,
  2665. pending_lock_flags);
  2666. /* If the command to be aborted was given to IOA and still pending with
  2667. * it, send ABORT_TASK to abort this and wait for its completion
  2668. */
  2669. if (cmd_found)
  2670. cancel_cmd = pmcraid_abort_cmd(cmd);
  2671. spin_unlock_irqrestore(pinstance->host->host_lock,
  2672. host_lock_flags);
  2673. if (cancel_cmd) {
  2674. cancel_cmd->res = cmd->scsi_cmd->device->hostdata;
  2675. rc = pmcraid_abort_complete(cancel_cmd);
  2676. }
  2677. return cmd_found ? rc : SUCCESS;
  2678. }
  2679. /**
  2680. * pmcraid_eh_xxxx_reset_handler - bus/target/device reset handler callbacks
  2681. *
  2682. * @scmd: pointer to scsi_cmd that was sent to the resource to be reset.
  2683. *
  2684. * All these routines invokve pmcraid_reset_device with appropriate parameters.
  2685. * Since these are called from mid-layer EH thread, no other IO will be queued
  2686. * to the resource being reset. However, control path (IOCTL) may be active so
  2687. * it is necessary to synchronize IOARRIN writes which pmcraid_reset_device
  2688. * takes care by locking/unlocking host_lock.
  2689. *
  2690. * Return value
  2691. * SUCCESS or FAILED
  2692. */
  2693. static int pmcraid_eh_device_reset_handler(struct scsi_cmnd *scmd)
  2694. {
  2695. scmd_printk(KERN_INFO, scmd,
  2696. "resetting device due to an I/O command timeout.\n");
  2697. return pmcraid_reset_device(scmd,
  2698. PMCRAID_INTERNAL_TIMEOUT,
  2699. RESET_DEVICE_LUN);
  2700. }
  2701. static int pmcraid_eh_bus_reset_handler(struct scsi_cmnd *scmd)
  2702. {
  2703. scmd_printk(KERN_INFO, scmd,
  2704. "Doing bus reset due to an I/O command timeout.\n");
  2705. return pmcraid_reset_device(scmd,
  2706. PMCRAID_RESET_BUS_TIMEOUT,
  2707. RESET_DEVICE_BUS);
  2708. }
  2709. static int pmcraid_eh_target_reset_handler(struct scsi_cmnd *scmd)
  2710. {
  2711. scmd_printk(KERN_INFO, scmd,
  2712. "Doing target reset due to an I/O command timeout.\n");
  2713. return pmcraid_reset_device(scmd,
  2714. PMCRAID_INTERNAL_TIMEOUT,
  2715. RESET_DEVICE_TARGET);
  2716. }
  2717. /**
  2718. * pmcraid_eh_host_reset_handler - adapter reset handler callback
  2719. *
  2720. * @scmd: pointer to scsi_cmd that was sent to a resource of adapter
  2721. *
  2722. * Initiates adapter reset to bring it up to operational state
  2723. *
  2724. * Return value
  2725. * SUCCESS or FAILED
  2726. */
  2727. static int pmcraid_eh_host_reset_handler(struct scsi_cmnd *scmd)
  2728. {
  2729. unsigned long interval = 10000; /* 10 seconds interval */
  2730. int waits = jiffies_to_msecs(PMCRAID_RESET_HOST_TIMEOUT) / interval;
  2731. struct pmcraid_instance *pinstance =
  2732. (struct pmcraid_instance *)(scmd->device->host->hostdata);
  2733. /* wait for an additional 150 seconds just in case firmware could come
  2734. * up and if it could complete all the pending commands excluding the
  2735. * two HCAM (CCN and LDN).
  2736. */
  2737. while (waits--) {
  2738. if (atomic_read(&pinstance->outstanding_cmds) <=
  2739. PMCRAID_MAX_HCAM_CMD)
  2740. return SUCCESS;
  2741. msleep(interval);
  2742. }
  2743. dev_err(&pinstance->pdev->dev,
  2744. "Adapter being reset due to an I/O command timeout.\n");
  2745. return pmcraid_reset_bringup(pinstance) == 0 ? SUCCESS : FAILED;
  2746. }
  2747. /**
  2748. * pmcraid_init_ioadls - initializes IOADL related fields in IOARCB
  2749. * @cmd: pmcraid command struct
  2750. * @sgcount: count of scatter-gather elements
  2751. *
  2752. * Return value
  2753. * returns pointer pmcraid_ioadl_desc, initialized to point to internal
  2754. * or external IOADLs
  2755. */
  2756. struct pmcraid_ioadl_desc *
  2757. pmcraid_init_ioadls(struct pmcraid_cmd *cmd, int sgcount)
  2758. {
  2759. struct pmcraid_ioadl_desc *ioadl;
  2760. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  2761. int ioadl_count = 0;
  2762. if (ioarcb->add_cmd_param_length)
  2763. ioadl_count = DIV_ROUND_UP(ioarcb->add_cmd_param_length, 16);
  2764. ioarcb->ioadl_length =
  2765. sizeof(struct pmcraid_ioadl_desc) * sgcount;
  2766. if ((sgcount + ioadl_count) > (ARRAY_SIZE(ioarcb->add_data.u.ioadl))) {
  2767. /* external ioadls start at offset 0x80 from control_block
  2768. * structure, re-using 24 out of 27 ioadls part of IOARCB.
  2769. * It is necessary to indicate to firmware that driver is
  2770. * using ioadls to be treated as external to IOARCB.
  2771. */
  2772. ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
  2773. ioarcb->ioadl_bus_addr =
  2774. cpu_to_le64((cmd->ioa_cb_bus_addr) +
  2775. offsetof(struct pmcraid_ioarcb,
  2776. add_data.u.ioadl[3]));
  2777. ioadl = &ioarcb->add_data.u.ioadl[3];
  2778. } else {
  2779. ioarcb->ioadl_bus_addr =
  2780. cpu_to_le64((cmd->ioa_cb_bus_addr) +
  2781. offsetof(struct pmcraid_ioarcb,
  2782. add_data.u.ioadl[ioadl_count]));
  2783. ioadl = &ioarcb->add_data.u.ioadl[ioadl_count];
  2784. ioarcb->ioarcb_bus_addr |=
  2785. DIV_ROUND_CLOSEST(sgcount + ioadl_count, 8);
  2786. }
  2787. return ioadl;
  2788. }
  2789. /**
  2790. * pmcraid_build_ioadl - Build a scatter/gather list and map the buffer
  2791. * @pinstance: pointer to adapter instance structure
  2792. * @cmd: pmcraid command struct
  2793. *
  2794. * This function is invoked by queuecommand entry point while sending a command
  2795. * to firmware. This builds ioadl descriptors and sets up ioarcb fields.
  2796. *
  2797. * Return value:
  2798. * 0 on success or -1 on failure
  2799. */
  2800. static int pmcraid_build_ioadl(
  2801. struct pmcraid_instance *pinstance,
  2802. struct pmcraid_cmd *cmd
  2803. )
  2804. {
  2805. int i, nseg;
  2806. struct scatterlist *sglist;
  2807. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  2808. struct pmcraid_ioarcb *ioarcb = &(cmd->ioa_cb->ioarcb);
  2809. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  2810. u32 length = scsi_bufflen(scsi_cmd);
  2811. if (!length)
  2812. return 0;
  2813. nseg = scsi_dma_map(scsi_cmd);
  2814. if (nseg < 0) {
  2815. scmd_printk(KERN_ERR, scsi_cmd, "scsi_map_dma failed!\n");
  2816. return -1;
  2817. } else if (nseg > PMCRAID_MAX_IOADLS) {
  2818. scsi_dma_unmap(scsi_cmd);
  2819. scmd_printk(KERN_ERR, scsi_cmd,
  2820. "sg count is (%d) more than allowed!\n", nseg);
  2821. return -1;
  2822. }
  2823. /* Initialize IOARCB data transfer length fields */
  2824. if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE)
  2825. ioarcb->request_flags0 |= TRANSFER_DIR_WRITE;
  2826. ioarcb->request_flags0 |= NO_LINK_DESCS;
  2827. ioarcb->data_transfer_length = cpu_to_le32(length);
  2828. ioadl = pmcraid_init_ioadls(cmd, nseg);
  2829. /* Initialize IOADL descriptor addresses */
  2830. scsi_for_each_sg(scsi_cmd, sglist, nseg, i) {
  2831. ioadl[i].data_len = cpu_to_le32(sg_dma_len(sglist));
  2832. ioadl[i].address = cpu_to_le64(sg_dma_address(sglist));
  2833. ioadl[i].flags = 0;
  2834. }
  2835. /* setup last descriptor */
  2836. ioadl[i - 1].flags = IOADL_FLAGS_LAST_DESC;
  2837. return 0;
  2838. }
  2839. /**
  2840. * pmcraid_free_sglist - Frees an allocated SG buffer list
  2841. * @sglist: scatter/gather list pointer
  2842. *
  2843. * Free a DMA'able memory previously allocated with pmcraid_alloc_sglist
  2844. *
  2845. * Return value:
  2846. * none
  2847. */
  2848. static void pmcraid_free_sglist(struct pmcraid_sglist *sglist)
  2849. {
  2850. int i;
  2851. for (i = 0; i < sglist->num_sg; i++)
  2852. __free_pages(sg_page(&(sglist->scatterlist[i])),
  2853. sglist->order);
  2854. kfree(sglist);
  2855. }
  2856. /**
  2857. * pmcraid_alloc_sglist - Allocates memory for a SG list
  2858. * @buflen: buffer length
  2859. *
  2860. * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
  2861. * list.
  2862. *
  2863. * Return value
  2864. * pointer to sglist / NULL on failure
  2865. */
  2866. static struct pmcraid_sglist *pmcraid_alloc_sglist(int buflen)
  2867. {
  2868. struct pmcraid_sglist *sglist;
  2869. struct scatterlist *scatterlist;
  2870. struct page *page;
  2871. int num_elem, i, j;
  2872. int sg_size;
  2873. int order;
  2874. int bsize_elem;
  2875. sg_size = buflen / (PMCRAID_MAX_IOADLS - 1);
  2876. order = (sg_size > 0) ? get_order(sg_size) : 0;
  2877. bsize_elem = PAGE_SIZE * (1 << order);
  2878. /* Determine the actual number of sg entries needed */
  2879. if (buflen % bsize_elem)
  2880. num_elem = (buflen / bsize_elem) + 1;
  2881. else
  2882. num_elem = buflen / bsize_elem;
  2883. /* Allocate a scatter/gather list for the DMA */
  2884. sglist = kzalloc(sizeof(struct pmcraid_sglist) +
  2885. (sizeof(struct scatterlist) * (num_elem - 1)),
  2886. GFP_KERNEL);
  2887. if (sglist == NULL)
  2888. return NULL;
  2889. scatterlist = sglist->scatterlist;
  2890. sg_init_table(scatterlist, num_elem);
  2891. sglist->order = order;
  2892. sglist->num_sg = num_elem;
  2893. sg_size = buflen;
  2894. for (i = 0; i < num_elem; i++) {
  2895. page = alloc_pages(GFP_KERNEL|GFP_DMA|__GFP_ZERO, order);
  2896. if (!page) {
  2897. for (j = i - 1; j >= 0; j--)
  2898. __free_pages(sg_page(&scatterlist[j]), order);
  2899. kfree(sglist);
  2900. return NULL;
  2901. }
  2902. sg_set_page(&scatterlist[i], page,
  2903. sg_size < bsize_elem ? sg_size : bsize_elem, 0);
  2904. sg_size -= bsize_elem;
  2905. }
  2906. return sglist;
  2907. }
  2908. /**
  2909. * pmcraid_copy_sglist - Copy user buffer to kernel buffer's SG list
  2910. * @sglist: scatter/gather list pointer
  2911. * @buffer: buffer pointer
  2912. * @len: buffer length
  2913. * @direction: data transfer direction
  2914. *
  2915. * Copy a user buffer into a buffer allocated by pmcraid_alloc_sglist
  2916. *
  2917. * Return value:
  2918. * 0 on success / other on failure
  2919. */
  2920. static int pmcraid_copy_sglist(
  2921. struct pmcraid_sglist *sglist,
  2922. unsigned long buffer,
  2923. u32 len,
  2924. int direction
  2925. )
  2926. {
  2927. struct scatterlist *scatterlist;
  2928. void *kaddr;
  2929. int bsize_elem;
  2930. int i;
  2931. int rc = 0;
  2932. /* Determine the actual number of bytes per element */
  2933. bsize_elem = PAGE_SIZE * (1 << sglist->order);
  2934. scatterlist = sglist->scatterlist;
  2935. for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
  2936. struct page *page = sg_page(&scatterlist[i]);
  2937. kaddr = kmap(page);
  2938. if (direction == DMA_TO_DEVICE)
  2939. rc = __copy_from_user(kaddr,
  2940. (void *)buffer,
  2941. bsize_elem);
  2942. else
  2943. rc = __copy_to_user((void *)buffer, kaddr, bsize_elem);
  2944. kunmap(page);
  2945. if (rc) {
  2946. pmcraid_err("failed to copy user data into sg list\n");
  2947. return -EFAULT;
  2948. }
  2949. scatterlist[i].length = bsize_elem;
  2950. }
  2951. if (len % bsize_elem) {
  2952. struct page *page = sg_page(&scatterlist[i]);
  2953. kaddr = kmap(page);
  2954. if (direction == DMA_TO_DEVICE)
  2955. rc = __copy_from_user(kaddr,
  2956. (void *)buffer,
  2957. len % bsize_elem);
  2958. else
  2959. rc = __copy_to_user((void *)buffer,
  2960. kaddr,
  2961. len % bsize_elem);
  2962. kunmap(page);
  2963. scatterlist[i].length = len % bsize_elem;
  2964. }
  2965. if (rc) {
  2966. pmcraid_err("failed to copy user data into sg list\n");
  2967. rc = -EFAULT;
  2968. }
  2969. return rc;
  2970. }
  2971. /**
  2972. * pmcraid_queuecommand - Queue a mid-layer request
  2973. * @scsi_cmd: scsi command struct
  2974. * @done: done function
  2975. *
  2976. * This function queues a request generated by the mid-layer. Midlayer calls
  2977. * this routine within host->lock. Some of the functions called by queuecommand
  2978. * would use cmd block queue locks (free_pool_lock and pending_pool_lock)
  2979. *
  2980. * Return value:
  2981. * 0 on success
  2982. * SCSI_MLQUEUE_DEVICE_BUSY if device is busy
  2983. * SCSI_MLQUEUE_HOST_BUSY if host is busy
  2984. */
  2985. static int pmcraid_queuecommand_lck(
  2986. struct scsi_cmnd *scsi_cmd,
  2987. void (*done) (struct scsi_cmnd *)
  2988. )
  2989. {
  2990. struct pmcraid_instance *pinstance;
  2991. struct pmcraid_resource_entry *res;
  2992. struct pmcraid_ioarcb *ioarcb;
  2993. struct pmcraid_cmd *cmd;
  2994. u32 fw_version;
  2995. int rc = 0;
  2996. pinstance =
  2997. (struct pmcraid_instance *)scsi_cmd->device->host->hostdata;
  2998. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  2999. scsi_cmd->scsi_done = done;
  3000. res = scsi_cmd->device->hostdata;
  3001. scsi_cmd->result = (DID_OK << 16);
  3002. /* if adapter is marked as dead, set result to DID_NO_CONNECT complete
  3003. * the command
  3004. */
  3005. if (pinstance->ioa_state == IOA_STATE_DEAD) {
  3006. pmcraid_info("IOA is dead, but queuecommand is scheduled\n");
  3007. scsi_cmd->result = (DID_NO_CONNECT << 16);
  3008. scsi_cmd->scsi_done(scsi_cmd);
  3009. return 0;
  3010. }
  3011. /* If IOA reset is in progress, can't queue the commands */
  3012. if (pinstance->ioa_reset_in_progress)
  3013. return SCSI_MLQUEUE_HOST_BUSY;
  3014. /* Firmware doesn't support SYNCHRONIZE_CACHE command (0x35), complete
  3015. * the command here itself with success return
  3016. */
  3017. if (scsi_cmd->cmnd[0] == SYNCHRONIZE_CACHE) {
  3018. pmcraid_info("SYNC_CACHE(0x35), completing in driver itself\n");
  3019. scsi_cmd->scsi_done(scsi_cmd);
  3020. return 0;
  3021. }
  3022. /* initialize the command and IOARCB to be sent to IOA */
  3023. cmd = pmcraid_get_free_cmd(pinstance);
  3024. if (cmd == NULL) {
  3025. pmcraid_err("free command block is not available\n");
  3026. return SCSI_MLQUEUE_HOST_BUSY;
  3027. }
  3028. cmd->scsi_cmd = scsi_cmd;
  3029. ioarcb = &(cmd->ioa_cb->ioarcb);
  3030. memcpy(ioarcb->cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
  3031. ioarcb->resource_handle = res->cfg_entry.resource_handle;
  3032. ioarcb->request_type = REQ_TYPE_SCSI;
  3033. /* set hrrq number where the IOA should respond to. Note that all cmds
  3034. * generated internally uses hrrq_id 0, exception to this is the cmd
  3035. * block of scsi_cmd which is re-used (e.g. cancel/abort), which uses
  3036. * hrrq_id assigned here in queuecommand
  3037. */
  3038. ioarcb->hrrq_id = atomic_add_return(1, &(pinstance->last_message_id)) %
  3039. pinstance->num_hrrq;
  3040. cmd->cmd_done = pmcraid_io_done;
  3041. if (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry)) {
  3042. if (scsi_cmd->underflow == 0)
  3043. ioarcb->request_flags0 |= INHIBIT_UL_CHECK;
  3044. if (res->sync_reqd) {
  3045. ioarcb->request_flags0 |= SYNC_COMPLETE;
  3046. res->sync_reqd = 0;
  3047. }
  3048. ioarcb->request_flags0 |= NO_LINK_DESCS;
  3049. if (scsi_cmd->flags & SCMD_TAGGED)
  3050. ioarcb->request_flags1 |= TASK_TAG_SIMPLE;
  3051. if (RES_IS_GSCSI(res->cfg_entry))
  3052. ioarcb->request_flags1 |= DELAY_AFTER_RESET;
  3053. }
  3054. rc = pmcraid_build_ioadl(pinstance, cmd);
  3055. pmcraid_info("command (%d) CDB[0] = %x for %x:%x:%x:%x\n",
  3056. le32_to_cpu(ioarcb->response_handle) >> 2,
  3057. scsi_cmd->cmnd[0], pinstance->host->unique_id,
  3058. RES_IS_VSET(res->cfg_entry) ? PMCRAID_VSET_BUS_ID :
  3059. PMCRAID_PHYS_BUS_ID,
  3060. RES_IS_VSET(res->cfg_entry) ?
  3061. (fw_version <= PMCRAID_FW_VERSION_1 ?
  3062. res->cfg_entry.unique_flags1 :
  3063. res->cfg_entry.array_id & 0xFF) :
  3064. RES_TARGET(res->cfg_entry.resource_address),
  3065. RES_LUN(res->cfg_entry.resource_address));
  3066. if (likely(rc == 0)) {
  3067. _pmcraid_fire_command(cmd);
  3068. } else {
  3069. pmcraid_err("queuecommand could not build ioadl\n");
  3070. pmcraid_return_cmd(cmd);
  3071. rc = SCSI_MLQUEUE_HOST_BUSY;
  3072. }
  3073. return rc;
  3074. }
  3075. static DEF_SCSI_QCMD(pmcraid_queuecommand)
  3076. /**
  3077. * pmcraid_open -char node "open" entry, allowed only users with admin access
  3078. */
  3079. static int pmcraid_chr_open(struct inode *inode, struct file *filep)
  3080. {
  3081. struct pmcraid_instance *pinstance;
  3082. if (!capable(CAP_SYS_ADMIN))
  3083. return -EACCES;
  3084. /* Populate adapter instance * pointer for use by ioctl */
  3085. pinstance = container_of(inode->i_cdev, struct pmcraid_instance, cdev);
  3086. filep->private_data = pinstance;
  3087. return 0;
  3088. }
  3089. /**
  3090. * pmcraid_fasync - Async notifier registration from applications
  3091. *
  3092. * This function adds the calling process to a driver global queue. When an
  3093. * event occurs, SIGIO will be sent to all processes in this queue.
  3094. */
  3095. static int pmcraid_chr_fasync(int fd, struct file *filep, int mode)
  3096. {
  3097. struct pmcraid_instance *pinstance;
  3098. int rc;
  3099. pinstance = filep->private_data;
  3100. mutex_lock(&pinstance->aen_queue_lock);
  3101. rc = fasync_helper(fd, filep, mode, &pinstance->aen_queue);
  3102. mutex_unlock(&pinstance->aen_queue_lock);
  3103. return rc;
  3104. }
  3105. /**
  3106. * pmcraid_build_passthrough_ioadls - builds SG elements for passthrough
  3107. * commands sent over IOCTL interface
  3108. *
  3109. * @cmd : pointer to struct pmcraid_cmd
  3110. * @buflen : length of the request buffer
  3111. * @direction : data transfer direction
  3112. *
  3113. * Return value
  3114. * 0 on success, non-zero error code on failure
  3115. */
  3116. static int pmcraid_build_passthrough_ioadls(
  3117. struct pmcraid_cmd *cmd,
  3118. int buflen,
  3119. int direction
  3120. )
  3121. {
  3122. struct pmcraid_sglist *sglist = NULL;
  3123. struct scatterlist *sg = NULL;
  3124. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  3125. struct pmcraid_ioadl_desc *ioadl;
  3126. int i;
  3127. sglist = pmcraid_alloc_sglist(buflen);
  3128. if (!sglist) {
  3129. pmcraid_err("can't allocate memory for passthrough SGls\n");
  3130. return -ENOMEM;
  3131. }
  3132. sglist->num_dma_sg = pci_map_sg(cmd->drv_inst->pdev,
  3133. sglist->scatterlist,
  3134. sglist->num_sg, direction);
  3135. if (!sglist->num_dma_sg || sglist->num_dma_sg > PMCRAID_MAX_IOADLS) {
  3136. dev_err(&cmd->drv_inst->pdev->dev,
  3137. "Failed to map passthrough buffer!\n");
  3138. pmcraid_free_sglist(sglist);
  3139. return -EIO;
  3140. }
  3141. cmd->sglist = sglist;
  3142. ioarcb->request_flags0 |= NO_LINK_DESCS;
  3143. ioadl = pmcraid_init_ioadls(cmd, sglist->num_dma_sg);
  3144. /* Initialize IOADL descriptor addresses */
  3145. for_each_sg(sglist->scatterlist, sg, sglist->num_dma_sg, i) {
  3146. ioadl[i].data_len = cpu_to_le32(sg_dma_len(sg));
  3147. ioadl[i].address = cpu_to_le64(sg_dma_address(sg));
  3148. ioadl[i].flags = 0;
  3149. }
  3150. /* setup the last descriptor */
  3151. ioadl[i - 1].flags = IOADL_FLAGS_LAST_DESC;
  3152. return 0;
  3153. }
  3154. /**
  3155. * pmcraid_release_passthrough_ioadls - release passthrough ioadls
  3156. *
  3157. * @cmd: pointer to struct pmcraid_cmd for which ioadls were allocated
  3158. * @buflen: size of the request buffer
  3159. * @direction: data transfer direction
  3160. *
  3161. * Return value
  3162. * 0 on success, non-zero error code on failure
  3163. */
  3164. static void pmcraid_release_passthrough_ioadls(
  3165. struct pmcraid_cmd *cmd,
  3166. int buflen,
  3167. int direction
  3168. )
  3169. {
  3170. struct pmcraid_sglist *sglist = cmd->sglist;
  3171. if (buflen > 0) {
  3172. pci_unmap_sg(cmd->drv_inst->pdev,
  3173. sglist->scatterlist,
  3174. sglist->num_sg,
  3175. direction);
  3176. pmcraid_free_sglist(sglist);
  3177. cmd->sglist = NULL;
  3178. }
  3179. }
  3180. /**
  3181. * pmcraid_ioctl_passthrough - handling passthrough IOCTL commands
  3182. *
  3183. * @pinstance: pointer to adapter instance structure
  3184. * @cmd: ioctl code
  3185. * @arg: pointer to pmcraid_passthrough_buffer user buffer
  3186. *
  3187. * Return value
  3188. * 0 on success, non-zero error code on failure
  3189. */
  3190. static long pmcraid_ioctl_passthrough(
  3191. struct pmcraid_instance *pinstance,
  3192. unsigned int ioctl_cmd,
  3193. unsigned int buflen,
  3194. unsigned long arg
  3195. )
  3196. {
  3197. struct pmcraid_passthrough_ioctl_buffer *buffer;
  3198. struct pmcraid_ioarcb *ioarcb;
  3199. struct pmcraid_cmd *cmd;
  3200. struct pmcraid_cmd *cancel_cmd;
  3201. unsigned long request_buffer;
  3202. unsigned long request_offset;
  3203. unsigned long lock_flags;
  3204. void *ioasa;
  3205. u32 ioasc;
  3206. int request_size;
  3207. int buffer_size;
  3208. u8 access, direction;
  3209. int rc = 0;
  3210. /* If IOA reset is in progress, wait 10 secs for reset to complete */
  3211. if (pinstance->ioa_reset_in_progress) {
  3212. rc = wait_event_interruptible_timeout(
  3213. pinstance->reset_wait_q,
  3214. !pinstance->ioa_reset_in_progress,
  3215. msecs_to_jiffies(10000));
  3216. if (!rc)
  3217. return -ETIMEDOUT;
  3218. else if (rc < 0)
  3219. return -ERESTARTSYS;
  3220. }
  3221. /* If adapter is not in operational state, return error */
  3222. if (pinstance->ioa_state != IOA_STATE_OPERATIONAL) {
  3223. pmcraid_err("IOA is not operational\n");
  3224. return -ENOTTY;
  3225. }
  3226. buffer_size = sizeof(struct pmcraid_passthrough_ioctl_buffer);
  3227. buffer = kmalloc(buffer_size, GFP_KERNEL);
  3228. if (!buffer) {
  3229. pmcraid_err("no memory for passthrough buffer\n");
  3230. return -ENOMEM;
  3231. }
  3232. request_offset =
  3233. offsetof(struct pmcraid_passthrough_ioctl_buffer, request_buffer);
  3234. request_buffer = arg + request_offset;
  3235. rc = __copy_from_user(buffer,
  3236. (struct pmcraid_passthrough_ioctl_buffer *) arg,
  3237. sizeof(struct pmcraid_passthrough_ioctl_buffer));
  3238. ioasa =
  3239. (void *)(arg +
  3240. offsetof(struct pmcraid_passthrough_ioctl_buffer, ioasa));
  3241. if (rc) {
  3242. pmcraid_err("ioctl: can't copy passthrough buffer\n");
  3243. rc = -EFAULT;
  3244. goto out_free_buffer;
  3245. }
  3246. request_size = buffer->ioarcb.data_transfer_length;
  3247. if (buffer->ioarcb.request_flags0 & TRANSFER_DIR_WRITE) {
  3248. access = VERIFY_READ;
  3249. direction = DMA_TO_DEVICE;
  3250. } else {
  3251. access = VERIFY_WRITE;
  3252. direction = DMA_FROM_DEVICE;
  3253. }
  3254. if (request_size > 0) {
  3255. rc = access_ok(access, arg, request_offset + request_size);
  3256. if (!rc) {
  3257. rc = -EFAULT;
  3258. goto out_free_buffer;
  3259. }
  3260. } else if (request_size < 0) {
  3261. rc = -EINVAL;
  3262. goto out_free_buffer;
  3263. }
  3264. /* check if we have any additional command parameters */
  3265. if (buffer->ioarcb.add_cmd_param_length > PMCRAID_ADD_CMD_PARAM_LEN) {
  3266. rc = -EINVAL;
  3267. goto out_free_buffer;
  3268. }
  3269. cmd = pmcraid_get_free_cmd(pinstance);
  3270. if (!cmd) {
  3271. pmcraid_err("free command block is not available\n");
  3272. rc = -ENOMEM;
  3273. goto out_free_buffer;
  3274. }
  3275. cmd->scsi_cmd = NULL;
  3276. ioarcb = &(cmd->ioa_cb->ioarcb);
  3277. /* Copy the user-provided IOARCB stuff field by field */
  3278. ioarcb->resource_handle = buffer->ioarcb.resource_handle;
  3279. ioarcb->data_transfer_length = buffer->ioarcb.data_transfer_length;
  3280. ioarcb->cmd_timeout = buffer->ioarcb.cmd_timeout;
  3281. ioarcb->request_type = buffer->ioarcb.request_type;
  3282. ioarcb->request_flags0 = buffer->ioarcb.request_flags0;
  3283. ioarcb->request_flags1 = buffer->ioarcb.request_flags1;
  3284. memcpy(ioarcb->cdb, buffer->ioarcb.cdb, PMCRAID_MAX_CDB_LEN);
  3285. if (buffer->ioarcb.add_cmd_param_length) {
  3286. ioarcb->add_cmd_param_length =
  3287. buffer->ioarcb.add_cmd_param_length;
  3288. ioarcb->add_cmd_param_offset =
  3289. buffer->ioarcb.add_cmd_param_offset;
  3290. memcpy(ioarcb->add_data.u.add_cmd_params,
  3291. buffer->ioarcb.add_data.u.add_cmd_params,
  3292. buffer->ioarcb.add_cmd_param_length);
  3293. }
  3294. /* set hrrq number where the IOA should respond to. Note that all cmds
  3295. * generated internally uses hrrq_id 0, exception to this is the cmd
  3296. * block of scsi_cmd which is re-used (e.g. cancel/abort), which uses
  3297. * hrrq_id assigned here in queuecommand
  3298. */
  3299. ioarcb->hrrq_id = atomic_add_return(1, &(pinstance->last_message_id)) %
  3300. pinstance->num_hrrq;
  3301. if (request_size) {
  3302. rc = pmcraid_build_passthrough_ioadls(cmd,
  3303. request_size,
  3304. direction);
  3305. if (rc) {
  3306. pmcraid_err("couldn't build passthrough ioadls\n");
  3307. goto out_free_buffer;
  3308. }
  3309. } else if (request_size < 0) {
  3310. rc = -EINVAL;
  3311. goto out_free_buffer;
  3312. }
  3313. /* If data is being written into the device, copy the data from user
  3314. * buffers
  3315. */
  3316. if (direction == DMA_TO_DEVICE && request_size > 0) {
  3317. rc = pmcraid_copy_sglist(cmd->sglist,
  3318. request_buffer,
  3319. request_size,
  3320. direction);
  3321. if (rc) {
  3322. pmcraid_err("failed to copy user buffer\n");
  3323. goto out_free_sglist;
  3324. }
  3325. }
  3326. /* passthrough ioctl is a blocking command so, put the user to sleep
  3327. * until timeout. Note that a timeout value of 0 means, do timeout.
  3328. */
  3329. cmd->cmd_done = pmcraid_internal_done;
  3330. init_completion(&cmd->wait_for_completion);
  3331. cmd->completion_req = 1;
  3332. pmcraid_info("command(%d) (CDB[0] = %x) for %x\n",
  3333. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
  3334. cmd->ioa_cb->ioarcb.cdb[0],
  3335. le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle));
  3336. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  3337. _pmcraid_fire_command(cmd);
  3338. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  3339. /* NOTE ! Remove the below line once abort_task is implemented
  3340. * in firmware. This line disables ioctl command timeout handling logic
  3341. * similar to IO command timeout handling, making ioctl commands to wait
  3342. * until the command completion regardless of timeout value specified in
  3343. * ioarcb
  3344. */
  3345. buffer->ioarcb.cmd_timeout = 0;
  3346. /* If command timeout is specified put caller to wait till that time,
  3347. * otherwise it would be blocking wait. If command gets timed out, it
  3348. * will be aborted.
  3349. */
  3350. if (buffer->ioarcb.cmd_timeout == 0) {
  3351. wait_for_completion(&cmd->wait_for_completion);
  3352. } else if (!wait_for_completion_timeout(
  3353. &cmd->wait_for_completion,
  3354. msecs_to_jiffies(buffer->ioarcb.cmd_timeout * 1000))) {
  3355. pmcraid_info("aborting cmd %d (CDB[0] = %x) due to timeout\n",
  3356. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle >> 2),
  3357. cmd->ioa_cb->ioarcb.cdb[0]);
  3358. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  3359. cancel_cmd = pmcraid_abort_cmd(cmd);
  3360. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  3361. if (cancel_cmd) {
  3362. wait_for_completion(&cancel_cmd->wait_for_completion);
  3363. ioasc = cancel_cmd->ioa_cb->ioasa.ioasc;
  3364. pmcraid_return_cmd(cancel_cmd);
  3365. /* if abort task couldn't find the command i.e it got
  3366. * completed prior to aborting, return good completion.
  3367. * if command got aborted successfully or there was IOA
  3368. * reset due to abort task itself getting timedout then
  3369. * return -ETIMEDOUT
  3370. */
  3371. if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
  3372. PMCRAID_IOASC_SENSE_KEY(ioasc) == 0x00) {
  3373. if (ioasc != PMCRAID_IOASC_GC_IOARCB_NOTFOUND)
  3374. rc = -ETIMEDOUT;
  3375. goto out_handle_response;
  3376. }
  3377. }
  3378. /* no command block for abort task or abort task failed to abort
  3379. * the IOARCB, then wait for 150 more seconds and initiate reset
  3380. * sequence after timeout
  3381. */
  3382. if (!wait_for_completion_timeout(
  3383. &cmd->wait_for_completion,
  3384. msecs_to_jiffies(150 * 1000))) {
  3385. pmcraid_reset_bringup(cmd->drv_inst);
  3386. rc = -ETIMEDOUT;
  3387. }
  3388. }
  3389. out_handle_response:
  3390. /* copy entire IOASA buffer and return IOCTL success.
  3391. * If copying IOASA to user-buffer fails, return
  3392. * EFAULT
  3393. */
  3394. if (copy_to_user(ioasa, &cmd->ioa_cb->ioasa,
  3395. sizeof(struct pmcraid_ioasa))) {
  3396. pmcraid_err("failed to copy ioasa buffer to user\n");
  3397. rc = -EFAULT;
  3398. }
  3399. /* If the data transfer was from device, copy the data onto user
  3400. * buffers
  3401. */
  3402. else if (direction == DMA_FROM_DEVICE && request_size > 0) {
  3403. rc = pmcraid_copy_sglist(cmd->sglist,
  3404. request_buffer,
  3405. request_size,
  3406. direction);
  3407. if (rc) {
  3408. pmcraid_err("failed to copy user buffer\n");
  3409. rc = -EFAULT;
  3410. }
  3411. }
  3412. out_free_sglist:
  3413. pmcraid_release_passthrough_ioadls(cmd, request_size, direction);
  3414. pmcraid_return_cmd(cmd);
  3415. out_free_buffer:
  3416. kfree(buffer);
  3417. return rc;
  3418. }
  3419. /**
  3420. * pmcraid_ioctl_driver - ioctl handler for commands handled by driver itself
  3421. *
  3422. * @pinstance: pointer to adapter instance structure
  3423. * @cmd: ioctl command passed in
  3424. * @buflen: length of user_buffer
  3425. * @user_buffer: user buffer pointer
  3426. *
  3427. * Return Value
  3428. * 0 in case of success, otherwise appropriate error code
  3429. */
  3430. static long pmcraid_ioctl_driver(
  3431. struct pmcraid_instance *pinstance,
  3432. unsigned int cmd,
  3433. unsigned int buflen,
  3434. void __user *user_buffer
  3435. )
  3436. {
  3437. int rc = -ENOSYS;
  3438. if (!access_ok(VERIFY_READ, user_buffer, _IOC_SIZE(cmd))) {
  3439. pmcraid_err("ioctl_driver: access fault in request buffer\n");
  3440. return -EFAULT;
  3441. }
  3442. switch (cmd) {
  3443. case PMCRAID_IOCTL_RESET_ADAPTER:
  3444. pmcraid_reset_bringup(pinstance);
  3445. rc = 0;
  3446. break;
  3447. default:
  3448. break;
  3449. }
  3450. return rc;
  3451. }
  3452. /**
  3453. * pmcraid_check_ioctl_buffer - check for proper access to user buffer
  3454. *
  3455. * @cmd: ioctl command
  3456. * @arg: user buffer
  3457. * @hdr: pointer to kernel memory for pmcraid_ioctl_header
  3458. *
  3459. * Return Value
  3460. * negetive error code if there are access issues, otherwise zero.
  3461. * Upon success, returns ioctl header copied out of user buffer.
  3462. */
  3463. static int pmcraid_check_ioctl_buffer(
  3464. int cmd,
  3465. void __user *arg,
  3466. struct pmcraid_ioctl_header *hdr
  3467. )
  3468. {
  3469. int rc = 0;
  3470. int access = VERIFY_READ;
  3471. if (copy_from_user(hdr, arg, sizeof(struct pmcraid_ioctl_header))) {
  3472. pmcraid_err("couldn't copy ioctl header from user buffer\n");
  3473. return -EFAULT;
  3474. }
  3475. /* check for valid driver signature */
  3476. rc = memcmp(hdr->signature,
  3477. PMCRAID_IOCTL_SIGNATURE,
  3478. sizeof(hdr->signature));
  3479. if (rc) {
  3480. pmcraid_err("signature verification failed\n");
  3481. return -EINVAL;
  3482. }
  3483. /* check for appropriate buffer access */
  3484. if ((_IOC_DIR(cmd) & _IOC_READ) == _IOC_READ)
  3485. access = VERIFY_WRITE;
  3486. rc = access_ok(access,
  3487. (arg + sizeof(struct pmcraid_ioctl_header)),
  3488. hdr->buffer_length);
  3489. if (!rc) {
  3490. pmcraid_err("access failed for user buffer of size %d\n",
  3491. hdr->buffer_length);
  3492. return -EFAULT;
  3493. }
  3494. return 0;
  3495. }
  3496. /**
  3497. * pmcraid_ioctl - char node ioctl entry point
  3498. */
  3499. static long pmcraid_chr_ioctl(
  3500. struct file *filep,
  3501. unsigned int cmd,
  3502. unsigned long arg
  3503. )
  3504. {
  3505. struct pmcraid_instance *pinstance = NULL;
  3506. struct pmcraid_ioctl_header *hdr = NULL;
  3507. int retval = -ENOTTY;
  3508. hdr = kmalloc(sizeof(struct pmcraid_ioctl_header), GFP_KERNEL);
  3509. if (!hdr) {
  3510. pmcraid_err("failed to allocate memory for ioctl header\n");
  3511. return -ENOMEM;
  3512. }
  3513. retval = pmcraid_check_ioctl_buffer(cmd, (void *)arg, hdr);
  3514. if (retval) {
  3515. pmcraid_info("chr_ioctl: header check failed\n");
  3516. kfree(hdr);
  3517. return retval;
  3518. }
  3519. pinstance = filep->private_data;
  3520. if (!pinstance) {
  3521. pmcraid_info("adapter instance is not found\n");
  3522. kfree(hdr);
  3523. return -ENOTTY;
  3524. }
  3525. switch (_IOC_TYPE(cmd)) {
  3526. case PMCRAID_PASSTHROUGH_IOCTL:
  3527. /* If ioctl code is to download microcode, we need to block
  3528. * mid-layer requests.
  3529. */
  3530. if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
  3531. scsi_block_requests(pinstance->host);
  3532. retval = pmcraid_ioctl_passthrough(pinstance,
  3533. cmd,
  3534. hdr->buffer_length,
  3535. arg);
  3536. if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
  3537. scsi_unblock_requests(pinstance->host);
  3538. break;
  3539. case PMCRAID_DRIVER_IOCTL:
  3540. arg += sizeof(struct pmcraid_ioctl_header);
  3541. retval = pmcraid_ioctl_driver(pinstance,
  3542. cmd,
  3543. hdr->buffer_length,
  3544. (void __user *)arg);
  3545. break;
  3546. default:
  3547. retval = -ENOTTY;
  3548. break;
  3549. }
  3550. kfree(hdr);
  3551. return retval;
  3552. }
  3553. /**
  3554. * File operations structure for management interface
  3555. */
  3556. static const struct file_operations pmcraid_fops = {
  3557. .owner = THIS_MODULE,
  3558. .open = pmcraid_chr_open,
  3559. .fasync = pmcraid_chr_fasync,
  3560. .unlocked_ioctl = pmcraid_chr_ioctl,
  3561. #ifdef CONFIG_COMPAT
  3562. .compat_ioctl = pmcraid_chr_ioctl,
  3563. #endif
  3564. .llseek = noop_llseek,
  3565. };
  3566. /**
  3567. * pmcraid_show_log_level - Display adapter's error logging level
  3568. * @dev: class device struct
  3569. * @buf: buffer
  3570. *
  3571. * Return value:
  3572. * number of bytes printed to buffer
  3573. */
  3574. static ssize_t pmcraid_show_log_level(
  3575. struct device *dev,
  3576. struct device_attribute *attr,
  3577. char *buf)
  3578. {
  3579. struct Scsi_Host *shost = class_to_shost(dev);
  3580. struct pmcraid_instance *pinstance =
  3581. (struct pmcraid_instance *)shost->hostdata;
  3582. return snprintf(buf, PAGE_SIZE, "%d\n", pinstance->current_log_level);
  3583. }
  3584. /**
  3585. * pmcraid_store_log_level - Change the adapter's error logging level
  3586. * @dev: class device struct
  3587. * @buf: buffer
  3588. * @count: not used
  3589. *
  3590. * Return value:
  3591. * number of bytes printed to buffer
  3592. */
  3593. static ssize_t pmcraid_store_log_level(
  3594. struct device *dev,
  3595. struct device_attribute *attr,
  3596. const char *buf,
  3597. size_t count
  3598. )
  3599. {
  3600. struct Scsi_Host *shost;
  3601. struct pmcraid_instance *pinstance;
  3602. u8 val;
  3603. if (kstrtou8(buf, 10, &val))
  3604. return -EINVAL;
  3605. /* log-level should be from 0 to 2 */
  3606. if (val > 2)
  3607. return -EINVAL;
  3608. shost = class_to_shost(dev);
  3609. pinstance = (struct pmcraid_instance *)shost->hostdata;
  3610. pinstance->current_log_level = val;
  3611. return strlen(buf);
  3612. }
  3613. static struct device_attribute pmcraid_log_level_attr = {
  3614. .attr = {
  3615. .name = "log_level",
  3616. .mode = S_IRUGO | S_IWUSR,
  3617. },
  3618. .show = pmcraid_show_log_level,
  3619. .store = pmcraid_store_log_level,
  3620. };
  3621. /**
  3622. * pmcraid_show_drv_version - Display driver version
  3623. * @dev: class device struct
  3624. * @buf: buffer
  3625. *
  3626. * Return value:
  3627. * number of bytes printed to buffer
  3628. */
  3629. static ssize_t pmcraid_show_drv_version(
  3630. struct device *dev,
  3631. struct device_attribute *attr,
  3632. char *buf
  3633. )
  3634. {
  3635. return snprintf(buf, PAGE_SIZE, "version: %s\n",
  3636. PMCRAID_DRIVER_VERSION);
  3637. }
  3638. static struct device_attribute pmcraid_driver_version_attr = {
  3639. .attr = {
  3640. .name = "drv_version",
  3641. .mode = S_IRUGO,
  3642. },
  3643. .show = pmcraid_show_drv_version,
  3644. };
  3645. /**
  3646. * pmcraid_show_io_adapter_id - Display driver assigned adapter id
  3647. * @dev: class device struct
  3648. * @buf: buffer
  3649. *
  3650. * Return value:
  3651. * number of bytes printed to buffer
  3652. */
  3653. static ssize_t pmcraid_show_adapter_id(
  3654. struct device *dev,
  3655. struct device_attribute *attr,
  3656. char *buf
  3657. )
  3658. {
  3659. struct Scsi_Host *shost = class_to_shost(dev);
  3660. struct pmcraid_instance *pinstance =
  3661. (struct pmcraid_instance *)shost->hostdata;
  3662. u32 adapter_id = (pinstance->pdev->bus->number << 8) |
  3663. pinstance->pdev->devfn;
  3664. u32 aen_group = pmcraid_event_family.id;
  3665. return snprintf(buf, PAGE_SIZE,
  3666. "adapter id: %d\nminor: %d\naen group: %d\n",
  3667. adapter_id, MINOR(pinstance->cdev.dev), aen_group);
  3668. }
  3669. static struct device_attribute pmcraid_adapter_id_attr = {
  3670. .attr = {
  3671. .name = "adapter_id",
  3672. .mode = S_IRUGO | S_IWUSR,
  3673. },
  3674. .show = pmcraid_show_adapter_id,
  3675. };
  3676. static struct device_attribute *pmcraid_host_attrs[] = {
  3677. &pmcraid_log_level_attr,
  3678. &pmcraid_driver_version_attr,
  3679. &pmcraid_adapter_id_attr,
  3680. NULL,
  3681. };
  3682. /* host template structure for pmcraid driver */
  3683. static struct scsi_host_template pmcraid_host_template = {
  3684. .module = THIS_MODULE,
  3685. .name = PMCRAID_DRIVER_NAME,
  3686. .queuecommand = pmcraid_queuecommand,
  3687. .eh_abort_handler = pmcraid_eh_abort_handler,
  3688. .eh_bus_reset_handler = pmcraid_eh_bus_reset_handler,
  3689. .eh_target_reset_handler = pmcraid_eh_target_reset_handler,
  3690. .eh_device_reset_handler = pmcraid_eh_device_reset_handler,
  3691. .eh_host_reset_handler = pmcraid_eh_host_reset_handler,
  3692. .slave_alloc = pmcraid_slave_alloc,
  3693. .slave_configure = pmcraid_slave_configure,
  3694. .slave_destroy = pmcraid_slave_destroy,
  3695. .change_queue_depth = pmcraid_change_queue_depth,
  3696. .change_queue_type = scsi_change_queue_type,
  3697. .can_queue = PMCRAID_MAX_IO_CMD,
  3698. .this_id = -1,
  3699. .sg_tablesize = PMCRAID_MAX_IOADLS,
  3700. .max_sectors = PMCRAID_IOA_MAX_SECTORS,
  3701. .no_write_same = 1,
  3702. .cmd_per_lun = PMCRAID_MAX_CMD_PER_LUN,
  3703. .use_clustering = ENABLE_CLUSTERING,
  3704. .shost_attrs = pmcraid_host_attrs,
  3705. .proc_name = PMCRAID_DRIVER_NAME,
  3706. .use_blk_tags = 1,
  3707. };
  3708. /*
  3709. * pmcraid_isr_msix - implements MSI-X interrupt handling routine
  3710. * @irq: interrupt vector number
  3711. * @dev_id: pointer hrrq_vector
  3712. *
  3713. * Return Value
  3714. * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
  3715. */
  3716. static irqreturn_t pmcraid_isr_msix(int irq, void *dev_id)
  3717. {
  3718. struct pmcraid_isr_param *hrrq_vector;
  3719. struct pmcraid_instance *pinstance;
  3720. unsigned long lock_flags;
  3721. u32 intrs_val;
  3722. int hrrq_id;
  3723. hrrq_vector = (struct pmcraid_isr_param *)dev_id;
  3724. hrrq_id = hrrq_vector->hrrq_id;
  3725. pinstance = hrrq_vector->drv_inst;
  3726. if (!hrrq_id) {
  3727. /* Read the interrupt */
  3728. intrs_val = pmcraid_read_interrupts(pinstance);
  3729. if (intrs_val &&
  3730. ((ioread32(pinstance->int_regs.host_ioa_interrupt_reg)
  3731. & DOORBELL_INTR_MSIX_CLR) == 0)) {
  3732. /* Any error interrupts including unit_check,
  3733. * initiate IOA reset.In case of unit check indicate
  3734. * to reset_sequence that IOA unit checked and prepare
  3735. * for a dump during reset sequence
  3736. */
  3737. if (intrs_val & PMCRAID_ERROR_INTERRUPTS) {
  3738. if (intrs_val & INTRS_IOA_UNIT_CHECK)
  3739. pinstance->ioa_unit_check = 1;
  3740. pmcraid_err("ISR: error interrupts: %x \
  3741. initiating reset\n", intrs_val);
  3742. spin_lock_irqsave(pinstance->host->host_lock,
  3743. lock_flags);
  3744. pmcraid_initiate_reset(pinstance);
  3745. spin_unlock_irqrestore(
  3746. pinstance->host->host_lock,
  3747. lock_flags);
  3748. }
  3749. /* If interrupt was as part of the ioa initialization,
  3750. * clear it. Delete the timer and wakeup the
  3751. * reset engine to proceed with reset sequence
  3752. */
  3753. if (intrs_val & INTRS_TRANSITION_TO_OPERATIONAL)
  3754. pmcraid_clr_trans_op(pinstance);
  3755. /* Clear the interrupt register by writing
  3756. * to host to ioa doorbell. Once done
  3757. * FW will clear the interrupt.
  3758. */
  3759. iowrite32(DOORBELL_INTR_MSIX_CLR,
  3760. pinstance->int_regs.host_ioa_interrupt_reg);
  3761. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  3762. }
  3763. }
  3764. tasklet_schedule(&(pinstance->isr_tasklet[hrrq_id]));
  3765. return IRQ_HANDLED;
  3766. }
  3767. /**
  3768. * pmcraid_isr - implements legacy interrupt handling routine
  3769. *
  3770. * @irq: interrupt vector number
  3771. * @dev_id: pointer hrrq_vector
  3772. *
  3773. * Return Value
  3774. * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
  3775. */
  3776. static irqreturn_t pmcraid_isr(int irq, void *dev_id)
  3777. {
  3778. struct pmcraid_isr_param *hrrq_vector;
  3779. struct pmcraid_instance *pinstance;
  3780. u32 intrs;
  3781. unsigned long lock_flags;
  3782. int hrrq_id = 0;
  3783. /* In case of legacy interrupt mode where interrupts are shared across
  3784. * isrs, it may be possible that the current interrupt is not from IOA
  3785. */
  3786. if (!dev_id) {
  3787. printk(KERN_INFO "%s(): NULL host pointer\n", __func__);
  3788. return IRQ_NONE;
  3789. }
  3790. hrrq_vector = (struct pmcraid_isr_param *)dev_id;
  3791. pinstance = hrrq_vector->drv_inst;
  3792. intrs = pmcraid_read_interrupts(pinstance);
  3793. if (unlikely((intrs & PMCRAID_PCI_INTERRUPTS) == 0))
  3794. return IRQ_NONE;
  3795. /* Any error interrupts including unit_check, initiate IOA reset.
  3796. * In case of unit check indicate to reset_sequence that IOA unit
  3797. * checked and prepare for a dump during reset sequence
  3798. */
  3799. if (intrs & PMCRAID_ERROR_INTERRUPTS) {
  3800. if (intrs & INTRS_IOA_UNIT_CHECK)
  3801. pinstance->ioa_unit_check = 1;
  3802. iowrite32(intrs,
  3803. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3804. pmcraid_err("ISR: error interrupts: %x initiating reset\n",
  3805. intrs);
  3806. intrs = ioread32(
  3807. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3808. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  3809. pmcraid_initiate_reset(pinstance);
  3810. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  3811. } else {
  3812. /* If interrupt was as part of the ioa initialization,
  3813. * clear. Delete the timer and wakeup the
  3814. * reset engine to proceed with reset sequence
  3815. */
  3816. if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
  3817. pmcraid_clr_trans_op(pinstance);
  3818. } else {
  3819. iowrite32(intrs,
  3820. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3821. ioread32(
  3822. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3823. tasklet_schedule(
  3824. &(pinstance->isr_tasklet[hrrq_id]));
  3825. }
  3826. }
  3827. return IRQ_HANDLED;
  3828. }
  3829. /**
  3830. * pmcraid_worker_function - worker thread function
  3831. *
  3832. * @workp: pointer to struct work queue
  3833. *
  3834. * Return Value
  3835. * None
  3836. */
  3837. static void pmcraid_worker_function(struct work_struct *workp)
  3838. {
  3839. struct pmcraid_instance *pinstance;
  3840. struct pmcraid_resource_entry *res;
  3841. struct pmcraid_resource_entry *temp;
  3842. struct scsi_device *sdev;
  3843. unsigned long lock_flags;
  3844. unsigned long host_lock_flags;
  3845. u16 fw_version;
  3846. u8 bus, target, lun;
  3847. pinstance = container_of(workp, struct pmcraid_instance, worker_q);
  3848. /* add resources only after host is added into system */
  3849. if (!atomic_read(&pinstance->expose_resources))
  3850. return;
  3851. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  3852. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  3853. list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue) {
  3854. if (res->change_detected == RES_CHANGE_DEL && res->scsi_dev) {
  3855. sdev = res->scsi_dev;
  3856. /* host_lock must be held before calling
  3857. * scsi_device_get
  3858. */
  3859. spin_lock_irqsave(pinstance->host->host_lock,
  3860. host_lock_flags);
  3861. if (!scsi_device_get(sdev)) {
  3862. spin_unlock_irqrestore(
  3863. pinstance->host->host_lock,
  3864. host_lock_flags);
  3865. pmcraid_info("deleting %x from midlayer\n",
  3866. res->cfg_entry.resource_address);
  3867. list_move_tail(&res->queue,
  3868. &pinstance->free_res_q);
  3869. spin_unlock_irqrestore(
  3870. &pinstance->resource_lock,
  3871. lock_flags);
  3872. scsi_remove_device(sdev);
  3873. scsi_device_put(sdev);
  3874. spin_lock_irqsave(&pinstance->resource_lock,
  3875. lock_flags);
  3876. res->change_detected = 0;
  3877. } else {
  3878. spin_unlock_irqrestore(
  3879. pinstance->host->host_lock,
  3880. host_lock_flags);
  3881. }
  3882. }
  3883. }
  3884. list_for_each_entry(res, &pinstance->used_res_q, queue) {
  3885. if (res->change_detected == RES_CHANGE_ADD) {
  3886. if (!pmcraid_expose_resource(fw_version,
  3887. &res->cfg_entry))
  3888. continue;
  3889. if (RES_IS_VSET(res->cfg_entry)) {
  3890. bus = PMCRAID_VSET_BUS_ID;
  3891. if (fw_version <= PMCRAID_FW_VERSION_1)
  3892. target = res->cfg_entry.unique_flags1;
  3893. else
  3894. target = res->cfg_entry.array_id & 0xFF;
  3895. lun = PMCRAID_VSET_LUN_ID;
  3896. } else {
  3897. bus = PMCRAID_PHYS_BUS_ID;
  3898. target =
  3899. RES_TARGET(
  3900. res->cfg_entry.resource_address);
  3901. lun = RES_LUN(res->cfg_entry.resource_address);
  3902. }
  3903. res->change_detected = 0;
  3904. spin_unlock_irqrestore(&pinstance->resource_lock,
  3905. lock_flags);
  3906. scsi_add_device(pinstance->host, bus, target, lun);
  3907. spin_lock_irqsave(&pinstance->resource_lock,
  3908. lock_flags);
  3909. }
  3910. }
  3911. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  3912. }
  3913. /**
  3914. * pmcraid_tasklet_function - Tasklet function
  3915. *
  3916. * @instance: pointer to msix param structure
  3917. *
  3918. * Return Value
  3919. * None
  3920. */
  3921. static void pmcraid_tasklet_function(unsigned long instance)
  3922. {
  3923. struct pmcraid_isr_param *hrrq_vector;
  3924. struct pmcraid_instance *pinstance;
  3925. unsigned long hrrq_lock_flags;
  3926. unsigned long pending_lock_flags;
  3927. unsigned long host_lock_flags;
  3928. spinlock_t *lockp; /* hrrq buffer lock */
  3929. int id;
  3930. __le32 resp;
  3931. hrrq_vector = (struct pmcraid_isr_param *)instance;
  3932. pinstance = hrrq_vector->drv_inst;
  3933. id = hrrq_vector->hrrq_id;
  3934. lockp = &(pinstance->hrrq_lock[id]);
  3935. /* loop through each of the commands responded by IOA. Each HRRQ buf is
  3936. * protected by its own lock. Traversals must be done within this lock
  3937. * as there may be multiple tasklets running on multiple CPUs. Note
  3938. * that the lock is held just for picking up the response handle and
  3939. * manipulating hrrq_curr/toggle_bit values.
  3940. */
  3941. spin_lock_irqsave(lockp, hrrq_lock_flags);
  3942. resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
  3943. while ((resp & HRRQ_TOGGLE_BIT) ==
  3944. pinstance->host_toggle_bit[id]) {
  3945. int cmd_index = resp >> 2;
  3946. struct pmcraid_cmd *cmd = NULL;
  3947. if (pinstance->hrrq_curr[id] < pinstance->hrrq_end[id]) {
  3948. pinstance->hrrq_curr[id]++;
  3949. } else {
  3950. pinstance->hrrq_curr[id] = pinstance->hrrq_start[id];
  3951. pinstance->host_toggle_bit[id] ^= 1u;
  3952. }
  3953. if (cmd_index >= PMCRAID_MAX_CMD) {
  3954. /* In case of invalid response handle, log message */
  3955. pmcraid_err("Invalid response handle %d\n", cmd_index);
  3956. resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
  3957. continue;
  3958. }
  3959. cmd = pinstance->cmd_list[cmd_index];
  3960. spin_unlock_irqrestore(lockp, hrrq_lock_flags);
  3961. spin_lock_irqsave(&pinstance->pending_pool_lock,
  3962. pending_lock_flags);
  3963. list_del(&cmd->free_list);
  3964. spin_unlock_irqrestore(&pinstance->pending_pool_lock,
  3965. pending_lock_flags);
  3966. del_timer(&cmd->timer);
  3967. atomic_dec(&pinstance->outstanding_cmds);
  3968. if (cmd->cmd_done == pmcraid_ioa_reset) {
  3969. spin_lock_irqsave(pinstance->host->host_lock,
  3970. host_lock_flags);
  3971. cmd->cmd_done(cmd);
  3972. spin_unlock_irqrestore(pinstance->host->host_lock,
  3973. host_lock_flags);
  3974. } else if (cmd->cmd_done != NULL) {
  3975. cmd->cmd_done(cmd);
  3976. }
  3977. /* loop over until we are done with all responses */
  3978. spin_lock_irqsave(lockp, hrrq_lock_flags);
  3979. resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
  3980. }
  3981. spin_unlock_irqrestore(lockp, hrrq_lock_flags);
  3982. }
  3983. /**
  3984. * pmcraid_unregister_interrupt_handler - de-register interrupts handlers
  3985. * @pinstance: pointer to adapter instance structure
  3986. *
  3987. * This routine un-registers registered interrupt handler and
  3988. * also frees irqs/vectors.
  3989. *
  3990. * Retun Value
  3991. * None
  3992. */
  3993. static
  3994. void pmcraid_unregister_interrupt_handler(struct pmcraid_instance *pinstance)
  3995. {
  3996. int i;
  3997. for (i = 0; i < pinstance->num_hrrq; i++)
  3998. free_irq(pinstance->hrrq_vector[i].vector,
  3999. &(pinstance->hrrq_vector[i]));
  4000. if (pinstance->interrupt_mode) {
  4001. pci_disable_msix(pinstance->pdev);
  4002. pinstance->interrupt_mode = 0;
  4003. }
  4004. }
  4005. /**
  4006. * pmcraid_register_interrupt_handler - registers interrupt handler
  4007. * @pinstance: pointer to per-adapter instance structure
  4008. *
  4009. * Return Value
  4010. * 0 on success, non-zero error code otherwise.
  4011. */
  4012. static int
  4013. pmcraid_register_interrupt_handler(struct pmcraid_instance *pinstance)
  4014. {
  4015. int rc;
  4016. struct pci_dev *pdev = pinstance->pdev;
  4017. if ((pmcraid_enable_msix) &&
  4018. (pci_find_capability(pdev, PCI_CAP_ID_MSIX))) {
  4019. int num_hrrq = PMCRAID_NUM_MSIX_VECTORS;
  4020. struct msix_entry entries[PMCRAID_NUM_MSIX_VECTORS];
  4021. int i;
  4022. for (i = 0; i < PMCRAID_NUM_MSIX_VECTORS; i++)
  4023. entries[i].entry = i;
  4024. num_hrrq = pci_enable_msix_range(pdev, entries, 1, num_hrrq);
  4025. if (num_hrrq < 0)
  4026. goto pmcraid_isr_legacy;
  4027. for (i = 0; i < num_hrrq; i++) {
  4028. pinstance->hrrq_vector[i].hrrq_id = i;
  4029. pinstance->hrrq_vector[i].drv_inst = pinstance;
  4030. pinstance->hrrq_vector[i].vector = entries[i].vector;
  4031. rc = request_irq(pinstance->hrrq_vector[i].vector,
  4032. pmcraid_isr_msix, 0,
  4033. PMCRAID_DRIVER_NAME,
  4034. &(pinstance->hrrq_vector[i]));
  4035. if (rc) {
  4036. int j;
  4037. for (j = 0; j < i; j++)
  4038. free_irq(entries[j].vector,
  4039. &(pinstance->hrrq_vector[j]));
  4040. pci_disable_msix(pdev);
  4041. goto pmcraid_isr_legacy;
  4042. }
  4043. }
  4044. pinstance->num_hrrq = num_hrrq;
  4045. pinstance->interrupt_mode = 1;
  4046. iowrite32(DOORBELL_INTR_MODE_MSIX,
  4047. pinstance->int_regs.host_ioa_interrupt_reg);
  4048. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  4049. goto pmcraid_isr_out;
  4050. }
  4051. pmcraid_isr_legacy:
  4052. /* If MSI-X registration failed fallback to legacy mode, where
  4053. * only one hrrq entry will be used
  4054. */
  4055. pinstance->hrrq_vector[0].hrrq_id = 0;
  4056. pinstance->hrrq_vector[0].drv_inst = pinstance;
  4057. pinstance->hrrq_vector[0].vector = pdev->irq;
  4058. pinstance->num_hrrq = 1;
  4059. rc = request_irq(pdev->irq, pmcraid_isr, IRQF_SHARED,
  4060. PMCRAID_DRIVER_NAME, &pinstance->hrrq_vector[0]);
  4061. pmcraid_isr_out:
  4062. return rc;
  4063. }
  4064. /**
  4065. * pmcraid_release_cmd_blocks - release buufers allocated for command blocks
  4066. * @pinstance: per adapter instance structure pointer
  4067. * @max_index: number of buffer blocks to release
  4068. *
  4069. * Return Value
  4070. * None
  4071. */
  4072. static void
  4073. pmcraid_release_cmd_blocks(struct pmcraid_instance *pinstance, int max_index)
  4074. {
  4075. int i;
  4076. for (i = 0; i < max_index; i++) {
  4077. kmem_cache_free(pinstance->cmd_cachep, pinstance->cmd_list[i]);
  4078. pinstance->cmd_list[i] = NULL;
  4079. }
  4080. kmem_cache_destroy(pinstance->cmd_cachep);
  4081. pinstance->cmd_cachep = NULL;
  4082. }
  4083. /**
  4084. * pmcraid_release_control_blocks - releases buffers alloced for control blocks
  4085. * @pinstance: pointer to per adapter instance structure
  4086. * @max_index: number of buffers (from 0 onwards) to release
  4087. *
  4088. * This function assumes that the command blocks for which control blocks are
  4089. * linked are not released.
  4090. *
  4091. * Return Value
  4092. * None
  4093. */
  4094. static void
  4095. pmcraid_release_control_blocks(
  4096. struct pmcraid_instance *pinstance,
  4097. int max_index
  4098. )
  4099. {
  4100. int i;
  4101. if (pinstance->control_pool == NULL)
  4102. return;
  4103. for (i = 0; i < max_index; i++) {
  4104. pci_pool_free(pinstance->control_pool,
  4105. pinstance->cmd_list[i]->ioa_cb,
  4106. pinstance->cmd_list[i]->ioa_cb_bus_addr);
  4107. pinstance->cmd_list[i]->ioa_cb = NULL;
  4108. pinstance->cmd_list[i]->ioa_cb_bus_addr = 0;
  4109. }
  4110. pci_pool_destroy(pinstance->control_pool);
  4111. pinstance->control_pool = NULL;
  4112. }
  4113. /**
  4114. * pmcraid_allocate_cmd_blocks - allocate memory for cmd block structures
  4115. * @pinstance - pointer to per adapter instance structure
  4116. *
  4117. * Allocates memory for command blocks using kernel slab allocator.
  4118. *
  4119. * Return Value
  4120. * 0 in case of success; -ENOMEM in case of failure
  4121. */
  4122. static int pmcraid_allocate_cmd_blocks(struct pmcraid_instance *pinstance)
  4123. {
  4124. int i;
  4125. sprintf(pinstance->cmd_pool_name, "pmcraid_cmd_pool_%d",
  4126. pinstance->host->unique_id);
  4127. pinstance->cmd_cachep = kmem_cache_create(
  4128. pinstance->cmd_pool_name,
  4129. sizeof(struct pmcraid_cmd), 0,
  4130. SLAB_HWCACHE_ALIGN, NULL);
  4131. if (!pinstance->cmd_cachep)
  4132. return -ENOMEM;
  4133. for (i = 0; i < PMCRAID_MAX_CMD; i++) {
  4134. pinstance->cmd_list[i] =
  4135. kmem_cache_alloc(pinstance->cmd_cachep, GFP_KERNEL);
  4136. if (!pinstance->cmd_list[i]) {
  4137. pmcraid_release_cmd_blocks(pinstance, i);
  4138. return -ENOMEM;
  4139. }
  4140. }
  4141. return 0;
  4142. }
  4143. /**
  4144. * pmcraid_allocate_control_blocks - allocates memory control blocks
  4145. * @pinstance : pointer to per adapter instance structure
  4146. *
  4147. * This function allocates PCI memory for DMAable buffers like IOARCB, IOADLs
  4148. * and IOASAs. This is called after command blocks are already allocated.
  4149. *
  4150. * Return Value
  4151. * 0 in case it can allocate all control blocks, otherwise -ENOMEM
  4152. */
  4153. static int pmcraid_allocate_control_blocks(struct pmcraid_instance *pinstance)
  4154. {
  4155. int i;
  4156. sprintf(pinstance->ctl_pool_name, "pmcraid_control_pool_%d",
  4157. pinstance->host->unique_id);
  4158. pinstance->control_pool =
  4159. pci_pool_create(pinstance->ctl_pool_name,
  4160. pinstance->pdev,
  4161. sizeof(struct pmcraid_control_block),
  4162. PMCRAID_IOARCB_ALIGNMENT, 0);
  4163. if (!pinstance->control_pool)
  4164. return -ENOMEM;
  4165. for (i = 0; i < PMCRAID_MAX_CMD; i++) {
  4166. pinstance->cmd_list[i]->ioa_cb =
  4167. pci_pool_alloc(
  4168. pinstance->control_pool,
  4169. GFP_KERNEL,
  4170. &(pinstance->cmd_list[i]->ioa_cb_bus_addr));
  4171. if (!pinstance->cmd_list[i]->ioa_cb) {
  4172. pmcraid_release_control_blocks(pinstance, i);
  4173. return -ENOMEM;
  4174. }
  4175. memset(pinstance->cmd_list[i]->ioa_cb, 0,
  4176. sizeof(struct pmcraid_control_block));
  4177. }
  4178. return 0;
  4179. }
  4180. /**
  4181. * pmcraid_release_host_rrqs - release memory allocated for hrrq buffer(s)
  4182. * @pinstance: pointer to per adapter instance structure
  4183. * @maxindex: size of hrrq buffer pointer array
  4184. *
  4185. * Return Value
  4186. * None
  4187. */
  4188. static void
  4189. pmcraid_release_host_rrqs(struct pmcraid_instance *pinstance, int maxindex)
  4190. {
  4191. int i;
  4192. for (i = 0; i < maxindex; i++) {
  4193. pci_free_consistent(pinstance->pdev,
  4194. HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD,
  4195. pinstance->hrrq_start[i],
  4196. pinstance->hrrq_start_bus_addr[i]);
  4197. /* reset pointers and toggle bit to zeros */
  4198. pinstance->hrrq_start[i] = NULL;
  4199. pinstance->hrrq_start_bus_addr[i] = 0;
  4200. pinstance->host_toggle_bit[i] = 0;
  4201. }
  4202. }
  4203. /**
  4204. * pmcraid_allocate_host_rrqs - Allocate and initialize host RRQ buffers
  4205. * @pinstance: pointer to per adapter instance structure
  4206. *
  4207. * Return value
  4208. * 0 hrrq buffers are allocated, -ENOMEM otherwise.
  4209. */
  4210. static int pmcraid_allocate_host_rrqs(struct pmcraid_instance *pinstance)
  4211. {
  4212. int i, buffer_size;
  4213. buffer_size = HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD;
  4214. for (i = 0; i < pinstance->num_hrrq; i++) {
  4215. pinstance->hrrq_start[i] =
  4216. pci_alloc_consistent(
  4217. pinstance->pdev,
  4218. buffer_size,
  4219. &(pinstance->hrrq_start_bus_addr[i]));
  4220. if (pinstance->hrrq_start[i] == 0) {
  4221. pmcraid_err("pci_alloc failed for hrrq vector : %d\n",
  4222. i);
  4223. pmcraid_release_host_rrqs(pinstance, i);
  4224. return -ENOMEM;
  4225. }
  4226. memset(pinstance->hrrq_start[i], 0, buffer_size);
  4227. pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
  4228. pinstance->hrrq_end[i] =
  4229. pinstance->hrrq_start[i] + PMCRAID_MAX_CMD - 1;
  4230. pinstance->host_toggle_bit[i] = 1;
  4231. spin_lock_init(&pinstance->hrrq_lock[i]);
  4232. }
  4233. return 0;
  4234. }
  4235. /**
  4236. * pmcraid_release_hcams - release HCAM buffers
  4237. *
  4238. * @pinstance: pointer to per adapter instance structure
  4239. *
  4240. * Return value
  4241. * none
  4242. */
  4243. static void pmcraid_release_hcams(struct pmcraid_instance *pinstance)
  4244. {
  4245. if (pinstance->ccn.msg != NULL) {
  4246. pci_free_consistent(pinstance->pdev,
  4247. PMCRAID_AEN_HDR_SIZE +
  4248. sizeof(struct pmcraid_hcam_ccn_ext),
  4249. pinstance->ccn.msg,
  4250. pinstance->ccn.baddr);
  4251. pinstance->ccn.msg = NULL;
  4252. pinstance->ccn.hcam = NULL;
  4253. pinstance->ccn.baddr = 0;
  4254. }
  4255. if (pinstance->ldn.msg != NULL) {
  4256. pci_free_consistent(pinstance->pdev,
  4257. PMCRAID_AEN_HDR_SIZE +
  4258. sizeof(struct pmcraid_hcam_ldn),
  4259. pinstance->ldn.msg,
  4260. pinstance->ldn.baddr);
  4261. pinstance->ldn.msg = NULL;
  4262. pinstance->ldn.hcam = NULL;
  4263. pinstance->ldn.baddr = 0;
  4264. }
  4265. }
  4266. /**
  4267. * pmcraid_allocate_hcams - allocates HCAM buffers
  4268. * @pinstance : pointer to per adapter instance structure
  4269. *
  4270. * Return Value:
  4271. * 0 in case of successful allocation, non-zero otherwise
  4272. */
  4273. static int pmcraid_allocate_hcams(struct pmcraid_instance *pinstance)
  4274. {
  4275. pinstance->ccn.msg = pci_alloc_consistent(
  4276. pinstance->pdev,
  4277. PMCRAID_AEN_HDR_SIZE +
  4278. sizeof(struct pmcraid_hcam_ccn_ext),
  4279. &(pinstance->ccn.baddr));
  4280. pinstance->ldn.msg = pci_alloc_consistent(
  4281. pinstance->pdev,
  4282. PMCRAID_AEN_HDR_SIZE +
  4283. sizeof(struct pmcraid_hcam_ldn),
  4284. &(pinstance->ldn.baddr));
  4285. if (pinstance->ldn.msg == NULL || pinstance->ccn.msg == NULL) {
  4286. pmcraid_release_hcams(pinstance);
  4287. } else {
  4288. pinstance->ccn.hcam =
  4289. (void *)pinstance->ccn.msg + PMCRAID_AEN_HDR_SIZE;
  4290. pinstance->ldn.hcam =
  4291. (void *)pinstance->ldn.msg + PMCRAID_AEN_HDR_SIZE;
  4292. atomic_set(&pinstance->ccn.ignore, 0);
  4293. atomic_set(&pinstance->ldn.ignore, 0);
  4294. }
  4295. return (pinstance->ldn.msg == NULL) ? -ENOMEM : 0;
  4296. }
  4297. /**
  4298. * pmcraid_release_config_buffers - release config.table buffers
  4299. * @pinstance: pointer to per adapter instance structure
  4300. *
  4301. * Return Value
  4302. * none
  4303. */
  4304. static void pmcraid_release_config_buffers(struct pmcraid_instance *pinstance)
  4305. {
  4306. if (pinstance->cfg_table != NULL &&
  4307. pinstance->cfg_table_bus_addr != 0) {
  4308. pci_free_consistent(pinstance->pdev,
  4309. sizeof(struct pmcraid_config_table),
  4310. pinstance->cfg_table,
  4311. pinstance->cfg_table_bus_addr);
  4312. pinstance->cfg_table = NULL;
  4313. pinstance->cfg_table_bus_addr = 0;
  4314. }
  4315. if (pinstance->res_entries != NULL) {
  4316. int i;
  4317. for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
  4318. list_del(&pinstance->res_entries[i].queue);
  4319. kfree(pinstance->res_entries);
  4320. pinstance->res_entries = NULL;
  4321. }
  4322. pmcraid_release_hcams(pinstance);
  4323. }
  4324. /**
  4325. * pmcraid_allocate_config_buffers - allocates DMAable memory for config table
  4326. * @pinstance : pointer to per adapter instance structure
  4327. *
  4328. * Return Value
  4329. * 0 for successful allocation, -ENOMEM for any failure
  4330. */
  4331. static int pmcraid_allocate_config_buffers(struct pmcraid_instance *pinstance)
  4332. {
  4333. int i;
  4334. pinstance->res_entries =
  4335. kzalloc(sizeof(struct pmcraid_resource_entry) *
  4336. PMCRAID_MAX_RESOURCES, GFP_KERNEL);
  4337. if (NULL == pinstance->res_entries) {
  4338. pmcraid_err("failed to allocate memory for resource table\n");
  4339. return -ENOMEM;
  4340. }
  4341. for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
  4342. list_add_tail(&pinstance->res_entries[i].queue,
  4343. &pinstance->free_res_q);
  4344. pinstance->cfg_table =
  4345. pci_alloc_consistent(pinstance->pdev,
  4346. sizeof(struct pmcraid_config_table),
  4347. &pinstance->cfg_table_bus_addr);
  4348. if (NULL == pinstance->cfg_table) {
  4349. pmcraid_err("couldn't alloc DMA memory for config table\n");
  4350. pmcraid_release_config_buffers(pinstance);
  4351. return -ENOMEM;
  4352. }
  4353. if (pmcraid_allocate_hcams(pinstance)) {
  4354. pmcraid_err("could not alloc DMA memory for HCAMS\n");
  4355. pmcraid_release_config_buffers(pinstance);
  4356. return -ENOMEM;
  4357. }
  4358. return 0;
  4359. }
  4360. /**
  4361. * pmcraid_init_tasklets - registers tasklets for response handling
  4362. *
  4363. * @pinstance: pointer adapter instance structure
  4364. *
  4365. * Return value
  4366. * none
  4367. */
  4368. static void pmcraid_init_tasklets(struct pmcraid_instance *pinstance)
  4369. {
  4370. int i;
  4371. for (i = 0; i < pinstance->num_hrrq; i++)
  4372. tasklet_init(&pinstance->isr_tasklet[i],
  4373. pmcraid_tasklet_function,
  4374. (unsigned long)&pinstance->hrrq_vector[i]);
  4375. }
  4376. /**
  4377. * pmcraid_kill_tasklets - destroys tasklets registered for response handling
  4378. *
  4379. * @pinstance: pointer to adapter instance structure
  4380. *
  4381. * Return value
  4382. * none
  4383. */
  4384. static void pmcraid_kill_tasklets(struct pmcraid_instance *pinstance)
  4385. {
  4386. int i;
  4387. for (i = 0; i < pinstance->num_hrrq; i++)
  4388. tasklet_kill(&pinstance->isr_tasklet[i]);
  4389. }
  4390. /**
  4391. * pmcraid_release_buffers - release per-adapter buffers allocated
  4392. *
  4393. * @pinstance: pointer to adapter soft state
  4394. *
  4395. * Return Value
  4396. * none
  4397. */
  4398. static void pmcraid_release_buffers(struct pmcraid_instance *pinstance)
  4399. {
  4400. pmcraid_release_config_buffers(pinstance);
  4401. pmcraid_release_control_blocks(pinstance, PMCRAID_MAX_CMD);
  4402. pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
  4403. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4404. if (pinstance->inq_data != NULL) {
  4405. pci_free_consistent(pinstance->pdev,
  4406. sizeof(struct pmcraid_inquiry_data),
  4407. pinstance->inq_data,
  4408. pinstance->inq_data_baddr);
  4409. pinstance->inq_data = NULL;
  4410. pinstance->inq_data_baddr = 0;
  4411. }
  4412. if (pinstance->timestamp_data != NULL) {
  4413. pci_free_consistent(pinstance->pdev,
  4414. sizeof(struct pmcraid_timestamp_data),
  4415. pinstance->timestamp_data,
  4416. pinstance->timestamp_data_baddr);
  4417. pinstance->timestamp_data = NULL;
  4418. pinstance->timestamp_data_baddr = 0;
  4419. }
  4420. }
  4421. /**
  4422. * pmcraid_init_buffers - allocates memory and initializes various structures
  4423. * @pinstance: pointer to per adapter instance structure
  4424. *
  4425. * This routine pre-allocates memory based on the type of block as below:
  4426. * cmdblocks(PMCRAID_MAX_CMD): kernel memory using kernel's slab_allocator,
  4427. * IOARCBs(PMCRAID_MAX_CMD) : DMAable memory, using pci pool allocator
  4428. * config-table entries : DMAable memory using pci_alloc_consistent
  4429. * HostRRQs : DMAable memory, using pci_alloc_consistent
  4430. *
  4431. * Return Value
  4432. * 0 in case all of the blocks are allocated, -ENOMEM otherwise.
  4433. */
  4434. static int pmcraid_init_buffers(struct pmcraid_instance *pinstance)
  4435. {
  4436. int i;
  4437. if (pmcraid_allocate_host_rrqs(pinstance)) {
  4438. pmcraid_err("couldn't allocate memory for %d host rrqs\n",
  4439. pinstance->num_hrrq);
  4440. return -ENOMEM;
  4441. }
  4442. if (pmcraid_allocate_config_buffers(pinstance)) {
  4443. pmcraid_err("couldn't allocate memory for config buffers\n");
  4444. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4445. return -ENOMEM;
  4446. }
  4447. if (pmcraid_allocate_cmd_blocks(pinstance)) {
  4448. pmcraid_err("couldn't allocate memory for cmd blocks\n");
  4449. pmcraid_release_config_buffers(pinstance);
  4450. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4451. return -ENOMEM;
  4452. }
  4453. if (pmcraid_allocate_control_blocks(pinstance)) {
  4454. pmcraid_err("couldn't allocate memory control blocks\n");
  4455. pmcraid_release_config_buffers(pinstance);
  4456. pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
  4457. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4458. return -ENOMEM;
  4459. }
  4460. /* allocate DMAable memory for page D0 INQUIRY buffer */
  4461. pinstance->inq_data = pci_alloc_consistent(
  4462. pinstance->pdev,
  4463. sizeof(struct pmcraid_inquiry_data),
  4464. &pinstance->inq_data_baddr);
  4465. if (pinstance->inq_data == NULL) {
  4466. pmcraid_err("couldn't allocate DMA memory for INQUIRY\n");
  4467. pmcraid_release_buffers(pinstance);
  4468. return -ENOMEM;
  4469. }
  4470. /* allocate DMAable memory for set timestamp data buffer */
  4471. pinstance->timestamp_data = pci_alloc_consistent(
  4472. pinstance->pdev,
  4473. sizeof(struct pmcraid_timestamp_data),
  4474. &pinstance->timestamp_data_baddr);
  4475. if (pinstance->timestamp_data == NULL) {
  4476. pmcraid_err("couldn't allocate DMA memory for \
  4477. set time_stamp \n");
  4478. pmcraid_release_buffers(pinstance);
  4479. return -ENOMEM;
  4480. }
  4481. /* Initialize all the command blocks and add them to free pool. No
  4482. * need to lock (free_pool_lock) as this is done in initialization
  4483. * itself
  4484. */
  4485. for (i = 0; i < PMCRAID_MAX_CMD; i++) {
  4486. struct pmcraid_cmd *cmdp = pinstance->cmd_list[i];
  4487. pmcraid_init_cmdblk(cmdp, i);
  4488. cmdp->drv_inst = pinstance;
  4489. list_add_tail(&cmdp->free_list, &pinstance->free_cmd_pool);
  4490. }
  4491. return 0;
  4492. }
  4493. /**
  4494. * pmcraid_reinit_buffers - resets various buffer pointers
  4495. * @pinstance: pointer to adapter instance
  4496. * Return value
  4497. * none
  4498. */
  4499. static void pmcraid_reinit_buffers(struct pmcraid_instance *pinstance)
  4500. {
  4501. int i;
  4502. int buffer_size = HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD;
  4503. for (i = 0; i < pinstance->num_hrrq; i++) {
  4504. memset(pinstance->hrrq_start[i], 0, buffer_size);
  4505. pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
  4506. pinstance->hrrq_end[i] =
  4507. pinstance->hrrq_start[i] + PMCRAID_MAX_CMD - 1;
  4508. pinstance->host_toggle_bit[i] = 1;
  4509. }
  4510. }
  4511. /**
  4512. * pmcraid_init_instance - initialize per instance data structure
  4513. * @pdev: pointer to pci device structure
  4514. * @host: pointer to Scsi_Host structure
  4515. * @mapped_pci_addr: memory mapped IOA configuration registers
  4516. *
  4517. * Return Value
  4518. * 0 on success, non-zero in case of any failure
  4519. */
  4520. static int pmcraid_init_instance(struct pci_dev *pdev, struct Scsi_Host *host,
  4521. void __iomem *mapped_pci_addr)
  4522. {
  4523. struct pmcraid_instance *pinstance =
  4524. (struct pmcraid_instance *)host->hostdata;
  4525. pinstance->host = host;
  4526. pinstance->pdev = pdev;
  4527. /* Initialize register addresses */
  4528. pinstance->mapped_dma_addr = mapped_pci_addr;
  4529. /* Initialize chip-specific details */
  4530. {
  4531. struct pmcraid_chip_details *chip_cfg = pinstance->chip_cfg;
  4532. struct pmcraid_interrupts *pint_regs = &pinstance->int_regs;
  4533. pinstance->ioarrin = mapped_pci_addr + chip_cfg->ioarrin;
  4534. pint_regs->ioa_host_interrupt_reg =
  4535. mapped_pci_addr + chip_cfg->ioa_host_intr;
  4536. pint_regs->ioa_host_interrupt_clr_reg =
  4537. mapped_pci_addr + chip_cfg->ioa_host_intr_clr;
  4538. pint_regs->ioa_host_msix_interrupt_reg =
  4539. mapped_pci_addr + chip_cfg->ioa_host_msix_intr;
  4540. pint_regs->host_ioa_interrupt_reg =
  4541. mapped_pci_addr + chip_cfg->host_ioa_intr;
  4542. pint_regs->host_ioa_interrupt_clr_reg =
  4543. mapped_pci_addr + chip_cfg->host_ioa_intr_clr;
  4544. /* Current version of firmware exposes interrupt mask set
  4545. * and mask clr registers through memory mapped bar0.
  4546. */
  4547. pinstance->mailbox = mapped_pci_addr + chip_cfg->mailbox;
  4548. pinstance->ioa_status = mapped_pci_addr + chip_cfg->ioastatus;
  4549. pint_regs->ioa_host_interrupt_mask_reg =
  4550. mapped_pci_addr + chip_cfg->ioa_host_mask;
  4551. pint_regs->ioa_host_interrupt_mask_clr_reg =
  4552. mapped_pci_addr + chip_cfg->ioa_host_mask_clr;
  4553. pint_regs->global_interrupt_mask_reg =
  4554. mapped_pci_addr + chip_cfg->global_intr_mask;
  4555. };
  4556. pinstance->ioa_reset_attempts = 0;
  4557. init_waitqueue_head(&pinstance->reset_wait_q);
  4558. atomic_set(&pinstance->outstanding_cmds, 0);
  4559. atomic_set(&pinstance->last_message_id, 0);
  4560. atomic_set(&pinstance->expose_resources, 0);
  4561. INIT_LIST_HEAD(&pinstance->free_res_q);
  4562. INIT_LIST_HEAD(&pinstance->used_res_q);
  4563. INIT_LIST_HEAD(&pinstance->free_cmd_pool);
  4564. INIT_LIST_HEAD(&pinstance->pending_cmd_pool);
  4565. spin_lock_init(&pinstance->free_pool_lock);
  4566. spin_lock_init(&pinstance->pending_pool_lock);
  4567. spin_lock_init(&pinstance->resource_lock);
  4568. mutex_init(&pinstance->aen_queue_lock);
  4569. /* Work-queue (Shared) for deferred processing error handling */
  4570. INIT_WORK(&pinstance->worker_q, pmcraid_worker_function);
  4571. /* Initialize the default log_level */
  4572. pinstance->current_log_level = pmcraid_log_level;
  4573. /* Setup variables required for reset engine */
  4574. pinstance->ioa_state = IOA_STATE_UNKNOWN;
  4575. pinstance->reset_cmd = NULL;
  4576. return 0;
  4577. }
  4578. /**
  4579. * pmcraid_shutdown - shutdown adapter controller.
  4580. * @pdev: pci device struct
  4581. *
  4582. * Issues an adapter shutdown to the card waits for its completion
  4583. *
  4584. * Return value
  4585. * none
  4586. */
  4587. static void pmcraid_shutdown(struct pci_dev *pdev)
  4588. {
  4589. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4590. pmcraid_reset_bringdown(pinstance);
  4591. }
  4592. /**
  4593. * pmcraid_get_minor - returns unused minor number from minor number bitmap
  4594. */
  4595. static unsigned short pmcraid_get_minor(void)
  4596. {
  4597. int minor;
  4598. minor = find_first_zero_bit(pmcraid_minor, sizeof(pmcraid_minor));
  4599. __set_bit(minor, pmcraid_minor);
  4600. return minor;
  4601. }
  4602. /**
  4603. * pmcraid_release_minor - releases given minor back to minor number bitmap
  4604. */
  4605. static void pmcraid_release_minor(unsigned short minor)
  4606. {
  4607. __clear_bit(minor, pmcraid_minor);
  4608. }
  4609. /**
  4610. * pmcraid_setup_chrdev - allocates a minor number and registers a char device
  4611. *
  4612. * @pinstance: pointer to adapter instance for which to register device
  4613. *
  4614. * Return value
  4615. * 0 in case of success, otherwise non-zero
  4616. */
  4617. static int pmcraid_setup_chrdev(struct pmcraid_instance *pinstance)
  4618. {
  4619. int minor;
  4620. int error;
  4621. minor = pmcraid_get_minor();
  4622. cdev_init(&pinstance->cdev, &pmcraid_fops);
  4623. pinstance->cdev.owner = THIS_MODULE;
  4624. error = cdev_add(&pinstance->cdev, MKDEV(pmcraid_major, minor), 1);
  4625. if (error)
  4626. pmcraid_release_minor(minor);
  4627. else
  4628. device_create(pmcraid_class, NULL, MKDEV(pmcraid_major, minor),
  4629. NULL, "%s%u", PMCRAID_DEVFILE, minor);
  4630. return error;
  4631. }
  4632. /**
  4633. * pmcraid_release_chrdev - unregisters per-adapter management interface
  4634. *
  4635. * @pinstance: pointer to adapter instance structure
  4636. *
  4637. * Return value
  4638. * none
  4639. */
  4640. static void pmcraid_release_chrdev(struct pmcraid_instance *pinstance)
  4641. {
  4642. pmcraid_release_minor(MINOR(pinstance->cdev.dev));
  4643. device_destroy(pmcraid_class,
  4644. MKDEV(pmcraid_major, MINOR(pinstance->cdev.dev)));
  4645. cdev_del(&pinstance->cdev);
  4646. }
  4647. /**
  4648. * pmcraid_remove - IOA hot plug remove entry point
  4649. * @pdev: pci device struct
  4650. *
  4651. * Return value
  4652. * none
  4653. */
  4654. static void pmcraid_remove(struct pci_dev *pdev)
  4655. {
  4656. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4657. /* remove the management interface (/dev file) for this device */
  4658. pmcraid_release_chrdev(pinstance);
  4659. /* remove host template from scsi midlayer */
  4660. scsi_remove_host(pinstance->host);
  4661. /* block requests from mid-layer */
  4662. scsi_block_requests(pinstance->host);
  4663. /* initiate shutdown adapter */
  4664. pmcraid_shutdown(pdev);
  4665. pmcraid_disable_interrupts(pinstance, ~0);
  4666. flush_work(&pinstance->worker_q);
  4667. pmcraid_kill_tasklets(pinstance);
  4668. pmcraid_unregister_interrupt_handler(pinstance);
  4669. pmcraid_release_buffers(pinstance);
  4670. iounmap(pinstance->mapped_dma_addr);
  4671. pci_release_regions(pdev);
  4672. scsi_host_put(pinstance->host);
  4673. pci_disable_device(pdev);
  4674. return;
  4675. }
  4676. #ifdef CONFIG_PM
  4677. /**
  4678. * pmcraid_suspend - driver suspend entry point for power management
  4679. * @pdev: PCI device structure
  4680. * @state: PCI power state to suspend routine
  4681. *
  4682. * Return Value - 0 always
  4683. */
  4684. static int pmcraid_suspend(struct pci_dev *pdev, pm_message_t state)
  4685. {
  4686. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4687. pmcraid_shutdown(pdev);
  4688. pmcraid_disable_interrupts(pinstance, ~0);
  4689. pmcraid_kill_tasklets(pinstance);
  4690. pci_set_drvdata(pinstance->pdev, pinstance);
  4691. pmcraid_unregister_interrupt_handler(pinstance);
  4692. pci_save_state(pdev);
  4693. pci_disable_device(pdev);
  4694. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  4695. return 0;
  4696. }
  4697. /**
  4698. * pmcraid_resume - driver resume entry point PCI power management
  4699. * @pdev: PCI device structure
  4700. *
  4701. * Return Value - 0 in case of success. Error code in case of any failure
  4702. */
  4703. static int pmcraid_resume(struct pci_dev *pdev)
  4704. {
  4705. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4706. struct Scsi_Host *host = pinstance->host;
  4707. int rc;
  4708. pci_set_power_state(pdev, PCI_D0);
  4709. pci_enable_wake(pdev, PCI_D0, 0);
  4710. pci_restore_state(pdev);
  4711. rc = pci_enable_device(pdev);
  4712. if (rc) {
  4713. dev_err(&pdev->dev, "resume: Enable device failed\n");
  4714. return rc;
  4715. }
  4716. pci_set_master(pdev);
  4717. if ((sizeof(dma_addr_t) == 4) ||
  4718. pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
  4719. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  4720. if (rc == 0)
  4721. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  4722. if (rc != 0) {
  4723. dev_err(&pdev->dev, "resume: Failed to set PCI DMA mask\n");
  4724. goto disable_device;
  4725. }
  4726. pmcraid_disable_interrupts(pinstance, ~0);
  4727. atomic_set(&pinstance->outstanding_cmds, 0);
  4728. rc = pmcraid_register_interrupt_handler(pinstance);
  4729. if (rc) {
  4730. dev_err(&pdev->dev,
  4731. "resume: couldn't register interrupt handlers\n");
  4732. rc = -ENODEV;
  4733. goto release_host;
  4734. }
  4735. pmcraid_init_tasklets(pinstance);
  4736. pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
  4737. /* Start with hard reset sequence which brings up IOA to operational
  4738. * state as well as completes the reset sequence.
  4739. */
  4740. pinstance->ioa_hard_reset = 1;
  4741. /* Start IOA firmware initialization and bring card to Operational
  4742. * state.
  4743. */
  4744. if (pmcraid_reset_bringup(pinstance)) {
  4745. dev_err(&pdev->dev, "couldn't initialize IOA\n");
  4746. rc = -ENODEV;
  4747. goto release_tasklets;
  4748. }
  4749. return 0;
  4750. release_tasklets:
  4751. pmcraid_disable_interrupts(pinstance, ~0);
  4752. pmcraid_kill_tasklets(pinstance);
  4753. pmcraid_unregister_interrupt_handler(pinstance);
  4754. release_host:
  4755. scsi_host_put(host);
  4756. disable_device:
  4757. pci_disable_device(pdev);
  4758. return rc;
  4759. }
  4760. #else
  4761. #define pmcraid_suspend NULL
  4762. #define pmcraid_resume NULL
  4763. #endif /* CONFIG_PM */
  4764. /**
  4765. * pmcraid_complete_ioa_reset - Called by either timer or tasklet during
  4766. * completion of the ioa reset
  4767. * @cmd: pointer to reset command block
  4768. */
  4769. static void pmcraid_complete_ioa_reset(struct pmcraid_cmd *cmd)
  4770. {
  4771. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4772. unsigned long flags;
  4773. spin_lock_irqsave(pinstance->host->host_lock, flags);
  4774. pmcraid_ioa_reset(cmd);
  4775. spin_unlock_irqrestore(pinstance->host->host_lock, flags);
  4776. scsi_unblock_requests(pinstance->host);
  4777. schedule_work(&pinstance->worker_q);
  4778. }
  4779. /**
  4780. * pmcraid_set_supported_devs - sends SET SUPPORTED DEVICES to IOAFP
  4781. *
  4782. * @cmd: pointer to pmcraid_cmd structure
  4783. *
  4784. * Return Value
  4785. * 0 for success or non-zero for failure cases
  4786. */
  4787. static void pmcraid_set_supported_devs(struct pmcraid_cmd *cmd)
  4788. {
  4789. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  4790. void (*cmd_done) (struct pmcraid_cmd *) = pmcraid_complete_ioa_reset;
  4791. pmcraid_reinit_cmdblk(cmd);
  4792. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  4793. ioarcb->request_type = REQ_TYPE_IOACMD;
  4794. ioarcb->cdb[0] = PMCRAID_SET_SUPPORTED_DEVICES;
  4795. ioarcb->cdb[1] = ALL_DEVICES_SUPPORTED;
  4796. /* If this was called as part of resource table reinitialization due to
  4797. * lost CCN, it is enough to return the command block back to free pool
  4798. * as part of set_supported_devs completion function.
  4799. */
  4800. if (cmd->drv_inst->reinit_cfg_table) {
  4801. cmd->drv_inst->reinit_cfg_table = 0;
  4802. cmd->release = 1;
  4803. cmd_done = pmcraid_reinit_cfgtable_done;
  4804. }
  4805. /* we will be done with the reset sequence after set supported devices,
  4806. * setup the done function to return the command block back to free
  4807. * pool
  4808. */
  4809. pmcraid_send_cmd(cmd,
  4810. cmd_done,
  4811. PMCRAID_SET_SUP_DEV_TIMEOUT,
  4812. pmcraid_timeout_handler);
  4813. return;
  4814. }
  4815. /**
  4816. * pmcraid_set_timestamp - set the timestamp to IOAFP
  4817. *
  4818. * @cmd: pointer to pmcraid_cmd structure
  4819. *
  4820. * Return Value
  4821. * 0 for success or non-zero for failure cases
  4822. */
  4823. static void pmcraid_set_timestamp(struct pmcraid_cmd *cmd)
  4824. {
  4825. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4826. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  4827. __be32 time_stamp_len = cpu_to_be32(PMCRAID_TIMESTAMP_LEN);
  4828. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  4829. struct timeval tv;
  4830. __le64 timestamp;
  4831. do_gettimeofday(&tv);
  4832. timestamp = tv.tv_sec * 1000;
  4833. pinstance->timestamp_data->timestamp[0] = (__u8)(timestamp);
  4834. pinstance->timestamp_data->timestamp[1] = (__u8)((timestamp) >> 8);
  4835. pinstance->timestamp_data->timestamp[2] = (__u8)((timestamp) >> 16);
  4836. pinstance->timestamp_data->timestamp[3] = (__u8)((timestamp) >> 24);
  4837. pinstance->timestamp_data->timestamp[4] = (__u8)((timestamp) >> 32);
  4838. pinstance->timestamp_data->timestamp[5] = (__u8)((timestamp) >> 40);
  4839. pmcraid_reinit_cmdblk(cmd);
  4840. ioarcb->request_type = REQ_TYPE_SCSI;
  4841. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  4842. ioarcb->cdb[0] = PMCRAID_SCSI_SET_TIMESTAMP;
  4843. ioarcb->cdb[1] = PMCRAID_SCSI_SERVICE_ACTION;
  4844. memcpy(&(ioarcb->cdb[6]), &time_stamp_len, sizeof(time_stamp_len));
  4845. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  4846. offsetof(struct pmcraid_ioarcb,
  4847. add_data.u.ioadl[0]));
  4848. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  4849. ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
  4850. ioarcb->request_flags0 |= NO_LINK_DESCS;
  4851. ioarcb->request_flags0 |= TRANSFER_DIR_WRITE;
  4852. ioarcb->data_transfer_length =
  4853. cpu_to_le32(sizeof(struct pmcraid_timestamp_data));
  4854. ioadl = &(ioarcb->add_data.u.ioadl[0]);
  4855. ioadl->flags = IOADL_FLAGS_LAST_DESC;
  4856. ioadl->address = cpu_to_le64(pinstance->timestamp_data_baddr);
  4857. ioadl->data_len = cpu_to_le32(sizeof(struct pmcraid_timestamp_data));
  4858. if (!pinstance->timestamp_error) {
  4859. pinstance->timestamp_error = 0;
  4860. pmcraid_send_cmd(cmd, pmcraid_set_supported_devs,
  4861. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  4862. } else {
  4863. pmcraid_send_cmd(cmd, pmcraid_return_cmd,
  4864. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  4865. return;
  4866. }
  4867. }
  4868. /**
  4869. * pmcraid_init_res_table - Initialize the resource table
  4870. * @cmd: pointer to pmcraid command struct
  4871. *
  4872. * This function looks through the existing resource table, comparing
  4873. * it with the config table. This function will take care of old/new
  4874. * devices and schedule adding/removing them from the mid-layer
  4875. * as appropriate.
  4876. *
  4877. * Return value
  4878. * None
  4879. */
  4880. static void pmcraid_init_res_table(struct pmcraid_cmd *cmd)
  4881. {
  4882. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4883. struct pmcraid_resource_entry *res, *temp;
  4884. struct pmcraid_config_table_entry *cfgte;
  4885. unsigned long lock_flags;
  4886. int found, rc, i;
  4887. u16 fw_version;
  4888. LIST_HEAD(old_res);
  4889. if (pinstance->cfg_table->flags & MICROCODE_UPDATE_REQUIRED)
  4890. pmcraid_err("IOA requires microcode download\n");
  4891. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  4892. /* resource list is protected by pinstance->resource_lock.
  4893. * init_res_table can be called from probe (user-thread) or runtime
  4894. * reset (timer/tasklet)
  4895. */
  4896. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  4897. list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue)
  4898. list_move_tail(&res->queue, &old_res);
  4899. for (i = 0; i < pinstance->cfg_table->num_entries; i++) {
  4900. if (be16_to_cpu(pinstance->inq_data->fw_version) <=
  4901. PMCRAID_FW_VERSION_1)
  4902. cfgte = &pinstance->cfg_table->entries[i];
  4903. else
  4904. cfgte = (struct pmcraid_config_table_entry *)
  4905. &pinstance->cfg_table->entries_ext[i];
  4906. if (!pmcraid_expose_resource(fw_version, cfgte))
  4907. continue;
  4908. found = 0;
  4909. /* If this entry was already detected and initialized */
  4910. list_for_each_entry_safe(res, temp, &old_res, queue) {
  4911. rc = memcmp(&res->cfg_entry.resource_address,
  4912. &cfgte->resource_address,
  4913. sizeof(cfgte->resource_address));
  4914. if (!rc) {
  4915. list_move_tail(&res->queue,
  4916. &pinstance->used_res_q);
  4917. found = 1;
  4918. break;
  4919. }
  4920. }
  4921. /* If this is new entry, initialize it and add it the queue */
  4922. if (!found) {
  4923. if (list_empty(&pinstance->free_res_q)) {
  4924. pmcraid_err("Too many devices attached\n");
  4925. break;
  4926. }
  4927. found = 1;
  4928. res = list_entry(pinstance->free_res_q.next,
  4929. struct pmcraid_resource_entry, queue);
  4930. res->scsi_dev = NULL;
  4931. res->change_detected = RES_CHANGE_ADD;
  4932. res->reset_progress = 0;
  4933. list_move_tail(&res->queue, &pinstance->used_res_q);
  4934. }
  4935. /* copy new configuration table entry details into driver
  4936. * maintained resource entry
  4937. */
  4938. if (found) {
  4939. memcpy(&res->cfg_entry, cfgte,
  4940. pinstance->config_table_entry_size);
  4941. pmcraid_info("New res type:%x, vset:%x, addr:%x:\n",
  4942. res->cfg_entry.resource_type,
  4943. (fw_version <= PMCRAID_FW_VERSION_1 ?
  4944. res->cfg_entry.unique_flags1 :
  4945. res->cfg_entry.array_id & 0xFF),
  4946. le32_to_cpu(res->cfg_entry.resource_address));
  4947. }
  4948. }
  4949. /* Detect any deleted entries, mark them for deletion from mid-layer */
  4950. list_for_each_entry_safe(res, temp, &old_res, queue) {
  4951. if (res->scsi_dev) {
  4952. res->change_detected = RES_CHANGE_DEL;
  4953. res->cfg_entry.resource_handle =
  4954. PMCRAID_INVALID_RES_HANDLE;
  4955. list_move_tail(&res->queue, &pinstance->used_res_q);
  4956. } else {
  4957. list_move_tail(&res->queue, &pinstance->free_res_q);
  4958. }
  4959. }
  4960. /* release the resource list lock */
  4961. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  4962. pmcraid_set_timestamp(cmd);
  4963. }
  4964. /**
  4965. * pmcraid_querycfg - Send a Query IOA Config to the adapter.
  4966. * @cmd: pointer pmcraid_cmd struct
  4967. *
  4968. * This function sends a Query IOA Configuration command to the adapter to
  4969. * retrieve the IOA configuration table.
  4970. *
  4971. * Return value:
  4972. * none
  4973. */
  4974. static void pmcraid_querycfg(struct pmcraid_cmd *cmd)
  4975. {
  4976. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  4977. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  4978. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4979. int cfg_table_size = cpu_to_be32(sizeof(struct pmcraid_config_table));
  4980. if (be16_to_cpu(pinstance->inq_data->fw_version) <=
  4981. PMCRAID_FW_VERSION_1)
  4982. pinstance->config_table_entry_size =
  4983. sizeof(struct pmcraid_config_table_entry);
  4984. else
  4985. pinstance->config_table_entry_size =
  4986. sizeof(struct pmcraid_config_table_entry_ext);
  4987. ioarcb->request_type = REQ_TYPE_IOACMD;
  4988. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  4989. ioarcb->cdb[0] = PMCRAID_QUERY_IOA_CONFIG;
  4990. /* firmware requires 4-byte length field, specified in B.E format */
  4991. memcpy(&(ioarcb->cdb[10]), &cfg_table_size, sizeof(cfg_table_size));
  4992. /* Since entire config table can be described by single IOADL, it can
  4993. * be part of IOARCB itself
  4994. */
  4995. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  4996. offsetof(struct pmcraid_ioarcb,
  4997. add_data.u.ioadl[0]));
  4998. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  4999. ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
  5000. ioarcb->request_flags0 |= NO_LINK_DESCS;
  5001. ioarcb->data_transfer_length =
  5002. cpu_to_le32(sizeof(struct pmcraid_config_table));
  5003. ioadl = &(ioarcb->add_data.u.ioadl[0]);
  5004. ioadl->flags = IOADL_FLAGS_LAST_DESC;
  5005. ioadl->address = cpu_to_le64(pinstance->cfg_table_bus_addr);
  5006. ioadl->data_len = cpu_to_le32(sizeof(struct pmcraid_config_table));
  5007. pmcraid_send_cmd(cmd, pmcraid_init_res_table,
  5008. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  5009. }
  5010. /**
  5011. * pmcraid_probe - PCI probe entry pointer for PMC MaxRAID controller driver
  5012. * @pdev: pointer to pci device structure
  5013. * @dev_id: pointer to device ids structure
  5014. *
  5015. * Return Value
  5016. * returns 0 if the device is claimed and successfully configured.
  5017. * returns non-zero error code in case of any failure
  5018. */
  5019. static int pmcraid_probe(struct pci_dev *pdev,
  5020. const struct pci_device_id *dev_id)
  5021. {
  5022. struct pmcraid_instance *pinstance;
  5023. struct Scsi_Host *host;
  5024. void __iomem *mapped_pci_addr;
  5025. int rc = PCIBIOS_SUCCESSFUL;
  5026. if (atomic_read(&pmcraid_adapter_count) >= PMCRAID_MAX_ADAPTERS) {
  5027. pmcraid_err
  5028. ("maximum number(%d) of supported adapters reached\n",
  5029. atomic_read(&pmcraid_adapter_count));
  5030. return -ENOMEM;
  5031. }
  5032. atomic_inc(&pmcraid_adapter_count);
  5033. rc = pci_enable_device(pdev);
  5034. if (rc) {
  5035. dev_err(&pdev->dev, "Cannot enable adapter\n");
  5036. atomic_dec(&pmcraid_adapter_count);
  5037. return rc;
  5038. }
  5039. dev_info(&pdev->dev,
  5040. "Found new IOA(%x:%x), Total IOA count: %d\n",
  5041. pdev->vendor, pdev->device,
  5042. atomic_read(&pmcraid_adapter_count));
  5043. rc = pci_request_regions(pdev, PMCRAID_DRIVER_NAME);
  5044. if (rc < 0) {
  5045. dev_err(&pdev->dev,
  5046. "Couldn't register memory range of registers\n");
  5047. goto out_disable_device;
  5048. }
  5049. mapped_pci_addr = pci_iomap(pdev, 0, 0);
  5050. if (!mapped_pci_addr) {
  5051. dev_err(&pdev->dev, "Couldn't map PCI registers memory\n");
  5052. rc = -ENOMEM;
  5053. goto out_release_regions;
  5054. }
  5055. pci_set_master(pdev);
  5056. /* Firmware requires the system bus address of IOARCB to be within
  5057. * 32-bit addressable range though it has 64-bit IOARRIN register.
  5058. * However, firmware supports 64-bit streaming DMA buffers, whereas
  5059. * coherent buffers are to be 32-bit. Since pci_alloc_consistent always
  5060. * returns memory within 4GB (if not, change this logic), coherent
  5061. * buffers are within firmware acceptable address ranges.
  5062. */
  5063. if ((sizeof(dma_addr_t) == 4) ||
  5064. pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
  5065. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  5066. /* firmware expects 32-bit DMA addresses for IOARRIN register; set 32
  5067. * bit mask for pci_alloc_consistent to return addresses within 4GB
  5068. */
  5069. if (rc == 0)
  5070. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  5071. if (rc != 0) {
  5072. dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
  5073. goto cleanup_nomem;
  5074. }
  5075. host = scsi_host_alloc(&pmcraid_host_template,
  5076. sizeof(struct pmcraid_instance));
  5077. if (!host) {
  5078. dev_err(&pdev->dev, "scsi_host_alloc failed!\n");
  5079. rc = -ENOMEM;
  5080. goto cleanup_nomem;
  5081. }
  5082. host->max_id = PMCRAID_MAX_NUM_TARGETS_PER_BUS;
  5083. host->max_lun = PMCRAID_MAX_NUM_LUNS_PER_TARGET;
  5084. host->unique_id = host->host_no;
  5085. host->max_channel = PMCRAID_MAX_BUS_TO_SCAN;
  5086. host->max_cmd_len = PMCRAID_MAX_CDB_LEN;
  5087. /* zero out entire instance structure */
  5088. pinstance = (struct pmcraid_instance *)host->hostdata;
  5089. memset(pinstance, 0, sizeof(*pinstance));
  5090. pinstance->chip_cfg =
  5091. (struct pmcraid_chip_details *)(dev_id->driver_data);
  5092. rc = pmcraid_init_instance(pdev, host, mapped_pci_addr);
  5093. if (rc < 0) {
  5094. dev_err(&pdev->dev, "failed to initialize adapter instance\n");
  5095. goto out_scsi_host_put;
  5096. }
  5097. pci_set_drvdata(pdev, pinstance);
  5098. /* Save PCI config-space for use following the reset */
  5099. rc = pci_save_state(pinstance->pdev);
  5100. if (rc != 0) {
  5101. dev_err(&pdev->dev, "Failed to save PCI config space\n");
  5102. goto out_scsi_host_put;
  5103. }
  5104. pmcraid_disable_interrupts(pinstance, ~0);
  5105. rc = pmcraid_register_interrupt_handler(pinstance);
  5106. if (rc) {
  5107. dev_err(&pdev->dev, "couldn't register interrupt handler\n");
  5108. goto out_scsi_host_put;
  5109. }
  5110. pmcraid_init_tasklets(pinstance);
  5111. /* allocate verious buffers used by LLD.*/
  5112. rc = pmcraid_init_buffers(pinstance);
  5113. if (rc) {
  5114. pmcraid_err("couldn't allocate memory blocks\n");
  5115. goto out_unregister_isr;
  5116. }
  5117. /* check the reset type required */
  5118. pmcraid_reset_type(pinstance);
  5119. pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
  5120. /* Start IOA firmware initialization and bring card to Operational
  5121. * state.
  5122. */
  5123. pmcraid_info("starting IOA initialization sequence\n");
  5124. if (pmcraid_reset_bringup(pinstance)) {
  5125. dev_err(&pdev->dev, "couldn't initialize IOA\n");
  5126. rc = 1;
  5127. goto out_release_bufs;
  5128. }
  5129. /* Add adapter instance into mid-layer list */
  5130. rc = scsi_add_host(pinstance->host, &pdev->dev);
  5131. if (rc != 0) {
  5132. pmcraid_err("couldn't add host into mid-layer: %d\n", rc);
  5133. goto out_release_bufs;
  5134. }
  5135. scsi_scan_host(pinstance->host);
  5136. rc = pmcraid_setup_chrdev(pinstance);
  5137. if (rc != 0) {
  5138. pmcraid_err("couldn't create mgmt interface, error: %x\n",
  5139. rc);
  5140. goto out_remove_host;
  5141. }
  5142. /* Schedule worker thread to handle CCN and take care of adding and
  5143. * removing devices to OS
  5144. */
  5145. atomic_set(&pinstance->expose_resources, 1);
  5146. schedule_work(&pinstance->worker_q);
  5147. return rc;
  5148. out_remove_host:
  5149. scsi_remove_host(host);
  5150. out_release_bufs:
  5151. pmcraid_release_buffers(pinstance);
  5152. out_unregister_isr:
  5153. pmcraid_kill_tasklets(pinstance);
  5154. pmcraid_unregister_interrupt_handler(pinstance);
  5155. out_scsi_host_put:
  5156. scsi_host_put(host);
  5157. cleanup_nomem:
  5158. iounmap(mapped_pci_addr);
  5159. out_release_regions:
  5160. pci_release_regions(pdev);
  5161. out_disable_device:
  5162. atomic_dec(&pmcraid_adapter_count);
  5163. pci_disable_device(pdev);
  5164. return -ENODEV;
  5165. }
  5166. /*
  5167. * PCI driver structure of pcmraid driver
  5168. */
  5169. static struct pci_driver pmcraid_driver = {
  5170. .name = PMCRAID_DRIVER_NAME,
  5171. .id_table = pmcraid_pci_table,
  5172. .probe = pmcraid_probe,
  5173. .remove = pmcraid_remove,
  5174. .suspend = pmcraid_suspend,
  5175. .resume = pmcraid_resume,
  5176. .shutdown = pmcraid_shutdown
  5177. };
  5178. /**
  5179. * pmcraid_init - module load entry point
  5180. */
  5181. static int __init pmcraid_init(void)
  5182. {
  5183. dev_t dev;
  5184. int error;
  5185. pmcraid_info("%s Device Driver version: %s\n",
  5186. PMCRAID_DRIVER_NAME, PMCRAID_DRIVER_VERSION);
  5187. error = alloc_chrdev_region(&dev, 0,
  5188. PMCRAID_MAX_ADAPTERS,
  5189. PMCRAID_DEVFILE);
  5190. if (error) {
  5191. pmcraid_err("failed to get a major number for adapters\n");
  5192. goto out_init;
  5193. }
  5194. pmcraid_major = MAJOR(dev);
  5195. pmcraid_class = class_create(THIS_MODULE, PMCRAID_DEVFILE);
  5196. if (IS_ERR(pmcraid_class)) {
  5197. error = PTR_ERR(pmcraid_class);
  5198. pmcraid_err("failed to register with sysfs, error = %x\n",
  5199. error);
  5200. goto out_unreg_chrdev;
  5201. }
  5202. error = pmcraid_netlink_init();
  5203. if (error)
  5204. goto out_unreg_chrdev;
  5205. error = pci_register_driver(&pmcraid_driver);
  5206. if (error == 0)
  5207. goto out_init;
  5208. pmcraid_err("failed to register pmcraid driver, error = %x\n",
  5209. error);
  5210. class_destroy(pmcraid_class);
  5211. pmcraid_netlink_release();
  5212. out_unreg_chrdev:
  5213. unregister_chrdev_region(MKDEV(pmcraid_major, 0), PMCRAID_MAX_ADAPTERS);
  5214. out_init:
  5215. return error;
  5216. }
  5217. /**
  5218. * pmcraid_exit - module unload entry point
  5219. */
  5220. static void __exit pmcraid_exit(void)
  5221. {
  5222. pmcraid_netlink_release();
  5223. unregister_chrdev_region(MKDEV(pmcraid_major, 0),
  5224. PMCRAID_MAX_ADAPTERS);
  5225. pci_unregister_driver(&pmcraid_driver);
  5226. class_destroy(pmcraid_class);
  5227. }
  5228. module_init(pmcraid_init);
  5229. module_exit(pmcraid_exit);