pmcraid.c 168 KB

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