pmcraid.c 169 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160
  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/version.h>
  43. #include <linux/io.h>
  44. #include <linux/slab.h>
  45. #include <asm/irq.h>
  46. #include <asm/processor.h>
  47. #include <linux/libata.h>
  48. #include <linux/mutex.h>
  49. #include <scsi/scsi.h>
  50. #include <scsi/scsi_host.h>
  51. #include <scsi/scsi_device.h>
  52. #include <scsi/scsi_tcq.h>
  53. #include <scsi/scsi_eh.h>
  54. #include <scsi/scsi_cmnd.h>
  55. #include <scsi/scsicam.h>
  56. #include "pmcraid.h"
  57. /*
  58. * Module configuration parameters
  59. */
  60. static unsigned int pmcraid_debug_log;
  61. static unsigned int pmcraid_disable_aen;
  62. static unsigned int pmcraid_log_level = IOASC_LOG_LEVEL_MUST;
  63. static unsigned int pmcraid_enable_msix;
  64. /*
  65. * Data structures to support multiple adapters by the LLD.
  66. * pmcraid_adapter_count - count of configured adapters
  67. */
  68. static atomic_t pmcraid_adapter_count = ATOMIC_INIT(0);
  69. /*
  70. * Supporting user-level control interface through IOCTL commands.
  71. * pmcraid_major - major number to use
  72. * pmcraid_minor - minor number(s) to use
  73. */
  74. static unsigned int pmcraid_major;
  75. static struct class *pmcraid_class;
  76. DECLARE_BITMAP(pmcraid_minor, PMCRAID_MAX_ADAPTERS);
  77. /*
  78. * Module parameters
  79. */
  80. MODULE_AUTHOR("Anil Ravindranath<anil_ravindranath@pmc-sierra.com>");
  81. MODULE_DESCRIPTION("PMC Sierra MaxRAID Controller Driver");
  82. MODULE_LICENSE("GPL");
  83. MODULE_VERSION(PMCRAID_DRIVER_VERSION);
  84. module_param_named(log_level, pmcraid_log_level, uint, (S_IRUGO | S_IWUSR));
  85. MODULE_PARM_DESC(log_level,
  86. "Enables firmware error code logging, default :1 high-severity"
  87. " errors, 2: all errors including high-severity errors,"
  88. " 0: disables logging");
  89. module_param_named(debug, pmcraid_debug_log, uint, (S_IRUGO | S_IWUSR));
  90. MODULE_PARM_DESC(debug,
  91. "Enable driver verbose message logging. Set 1 to enable."
  92. "(default: 0)");
  93. module_param_named(disable_aen, pmcraid_disable_aen, uint, (S_IRUGO | S_IWUSR));
  94. MODULE_PARM_DESC(disable_aen,
  95. "Disable driver aen notifications to apps. Set 1 to disable."
  96. "(default: 0)");
  97. /* chip specific constants for PMC MaxRAID controllers (same for
  98. * 0x5220 and 0x8010
  99. */
  100. static struct pmcraid_chip_details pmcraid_chip_cfg[] = {
  101. {
  102. .ioastatus = 0x0,
  103. .ioarrin = 0x00040,
  104. .mailbox = 0x7FC30,
  105. .global_intr_mask = 0x00034,
  106. .ioa_host_intr = 0x0009C,
  107. .ioa_host_intr_clr = 0x000A0,
  108. .ioa_host_msix_intr = 0x7FC40,
  109. .ioa_host_mask = 0x7FC28,
  110. .ioa_host_mask_clr = 0x7FC28,
  111. .host_ioa_intr = 0x00020,
  112. .host_ioa_intr_clr = 0x00020,
  113. .transop_timeout = 300
  114. }
  115. };
  116. /*
  117. * PCI device ids supported by pmcraid driver
  118. */
  119. static struct pci_device_id pmcraid_pci_table[] __devinitdata = {
  120. { PCI_DEVICE(PCI_VENDOR_ID_PMC, PCI_DEVICE_ID_PMC_MAXRAID),
  121. 0, 0, (kernel_ulong_t)&pmcraid_chip_cfg[0]
  122. },
  123. {}
  124. };
  125. MODULE_DEVICE_TABLE(pci, pmcraid_pci_table);
  126. /**
  127. * pmcraid_slave_alloc - Prepare for commands to a device
  128. * @scsi_dev: scsi device struct
  129. *
  130. * This function is called by mid-layer prior to sending any command to the new
  131. * device. Stores resource entry details of the device in scsi_device struct.
  132. * Queuecommand uses the resource handle and other details to fill up IOARCB
  133. * while sending commands to the device.
  134. *
  135. * Return value:
  136. * 0 on success / -ENXIO if device does not exist
  137. */
  138. static int pmcraid_slave_alloc(struct scsi_device *scsi_dev)
  139. {
  140. struct pmcraid_resource_entry *temp, *res = NULL;
  141. struct pmcraid_instance *pinstance;
  142. u8 target, bus, lun;
  143. unsigned long lock_flags;
  144. int rc = -ENXIO;
  145. u16 fw_version;
  146. pinstance = shost_priv(scsi_dev->host);
  147. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  148. /* Driver exposes VSET and GSCSI resources only; all other device types
  149. * are not exposed. Resource list is synchronized using resource lock
  150. * so any traversal or modifications to the list should be done inside
  151. * this lock
  152. */
  153. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  154. list_for_each_entry(temp, &pinstance->used_res_q, queue) {
  155. /* do not expose VSETs with order-ids > MAX_VSET_TARGETS */
  156. if (RES_IS_VSET(temp->cfg_entry)) {
  157. if (fw_version <= PMCRAID_FW_VERSION_1)
  158. target = temp->cfg_entry.unique_flags1;
  159. else
  160. target = temp->cfg_entry.array_id & 0xFF;
  161. if (target > PMCRAID_MAX_VSET_TARGETS)
  162. continue;
  163. bus = PMCRAID_VSET_BUS_ID;
  164. lun = 0;
  165. } else if (RES_IS_GSCSI(temp->cfg_entry)) {
  166. target = RES_TARGET(temp->cfg_entry.resource_address);
  167. bus = PMCRAID_PHYS_BUS_ID;
  168. lun = RES_LUN(temp->cfg_entry.resource_address);
  169. } else {
  170. continue;
  171. }
  172. if (bus == scsi_dev->channel &&
  173. target == scsi_dev->id &&
  174. lun == scsi_dev->lun) {
  175. res = temp;
  176. break;
  177. }
  178. }
  179. if (res) {
  180. res->scsi_dev = scsi_dev;
  181. scsi_dev->hostdata = res;
  182. res->change_detected = 0;
  183. atomic_set(&res->read_failures, 0);
  184. atomic_set(&res->write_failures, 0);
  185. rc = 0;
  186. }
  187. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  188. return rc;
  189. }
  190. /**
  191. * pmcraid_slave_configure - Configures a SCSI device
  192. * @scsi_dev: scsi device struct
  193. *
  194. * This function is executed by SCSI mid layer just after a device is first
  195. * scanned (i.e. it has responded to an INQUIRY). For VSET resources, the
  196. * timeout value (default 30s) will be over-written to a higher value (60s)
  197. * and max_sectors value will be over-written to 512. It also sets queue depth
  198. * to host->cmd_per_lun value
  199. *
  200. * Return value:
  201. * 0 on success
  202. */
  203. static int pmcraid_slave_configure(struct scsi_device *scsi_dev)
  204. {
  205. struct pmcraid_resource_entry *res = scsi_dev->hostdata;
  206. if (!res)
  207. return 0;
  208. /* LLD exposes VSETs and Enclosure devices only */
  209. if (RES_IS_GSCSI(res->cfg_entry) &&
  210. scsi_dev->type != TYPE_ENCLOSURE)
  211. return -ENXIO;
  212. pmcraid_info("configuring %x:%x:%x:%x\n",
  213. scsi_dev->host->unique_id,
  214. scsi_dev->channel,
  215. scsi_dev->id,
  216. scsi_dev->lun);
  217. if (RES_IS_GSCSI(res->cfg_entry)) {
  218. scsi_dev->allow_restart = 1;
  219. } else if (RES_IS_VSET(res->cfg_entry)) {
  220. scsi_dev->allow_restart = 1;
  221. blk_queue_rq_timeout(scsi_dev->request_queue,
  222. PMCRAID_VSET_IO_TIMEOUT);
  223. blk_queue_max_hw_sectors(scsi_dev->request_queue,
  224. PMCRAID_VSET_MAX_SECTORS);
  225. }
  226. if (scsi_dev->tagged_supported &&
  227. (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry))) {
  228. scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
  229. scsi_adjust_queue_depth(scsi_dev, MSG_SIMPLE_TAG,
  230. scsi_dev->host->cmd_per_lun);
  231. } else {
  232. scsi_adjust_queue_depth(scsi_dev, 0,
  233. scsi_dev->host->cmd_per_lun);
  234. }
  235. return 0;
  236. }
  237. /**
  238. * pmcraid_slave_destroy - Unconfigure a SCSI device before removing it
  239. *
  240. * @scsi_dev: scsi device struct
  241. *
  242. * This is called by mid-layer before removing a device. Pointer assignments
  243. * done in pmcraid_slave_alloc will be reset to NULL here.
  244. *
  245. * Return value
  246. * none
  247. */
  248. static void pmcraid_slave_destroy(struct scsi_device *scsi_dev)
  249. {
  250. struct pmcraid_resource_entry *res;
  251. res = (struct pmcraid_resource_entry *)scsi_dev->hostdata;
  252. if (res)
  253. res->scsi_dev = NULL;
  254. scsi_dev->hostdata = NULL;
  255. }
  256. /**
  257. * pmcraid_change_queue_depth - Change the device's queue depth
  258. * @scsi_dev: scsi device struct
  259. * @depth: depth to set
  260. * @reason: calling context
  261. *
  262. * Return value
  263. * actual depth set
  264. */
  265. static int pmcraid_change_queue_depth(struct scsi_device *scsi_dev, int depth,
  266. int reason)
  267. {
  268. if (reason != SCSI_QDEPTH_DEFAULT)
  269. return -EOPNOTSUPP;
  270. if (depth > PMCRAID_MAX_CMD_PER_LUN)
  271. depth = PMCRAID_MAX_CMD_PER_LUN;
  272. scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev), depth);
  273. return scsi_dev->queue_depth;
  274. }
  275. /**
  276. * pmcraid_change_queue_type - Change the device's queue type
  277. * @scsi_dev: scsi device struct
  278. * @tag: type of tags to use
  279. *
  280. * Return value:
  281. * actual queue type set
  282. */
  283. static int pmcraid_change_queue_type(struct scsi_device *scsi_dev, int tag)
  284. {
  285. struct pmcraid_resource_entry *res;
  286. res = (struct pmcraid_resource_entry *)scsi_dev->hostdata;
  287. if ((res) && scsi_dev->tagged_supported &&
  288. (RES_IS_GSCSI(res->cfg_entry) || RES_IS_VSET(res->cfg_entry))) {
  289. scsi_set_tag_type(scsi_dev, tag);
  290. if (tag)
  291. scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
  292. else
  293. scsi_deactivate_tcq(scsi_dev, scsi_dev->queue_depth);
  294. } else
  295. tag = 0;
  296. return tag;
  297. }
  298. /**
  299. * pmcraid_init_cmdblk - initializes a command block
  300. *
  301. * @cmd: pointer to struct pmcraid_cmd to be initialized
  302. * @index: if >=0 first time initialization; otherwise reinitialization
  303. *
  304. * Return Value
  305. * None
  306. */
  307. void pmcraid_init_cmdblk(struct pmcraid_cmd *cmd, int index)
  308. {
  309. struct pmcraid_ioarcb *ioarcb = &(cmd->ioa_cb->ioarcb);
  310. dma_addr_t dma_addr = cmd->ioa_cb_bus_addr;
  311. if (index >= 0) {
  312. /* first time initialization (called from probe) */
  313. u32 ioasa_offset =
  314. offsetof(struct pmcraid_control_block, ioasa);
  315. cmd->index = index;
  316. ioarcb->response_handle = cpu_to_le32(index << 2);
  317. ioarcb->ioarcb_bus_addr = cpu_to_le64(dma_addr);
  318. ioarcb->ioasa_bus_addr = cpu_to_le64(dma_addr + ioasa_offset);
  319. ioarcb->ioasa_len = cpu_to_le16(sizeof(struct pmcraid_ioasa));
  320. } else {
  321. /* re-initialization of various lengths, called once command is
  322. * processed by IOA
  323. */
  324. memset(&cmd->ioa_cb->ioarcb.cdb, 0, PMCRAID_MAX_CDB_LEN);
  325. ioarcb->hrrq_id = 0;
  326. ioarcb->request_flags0 = 0;
  327. ioarcb->request_flags1 = 0;
  328. ioarcb->cmd_timeout = 0;
  329. ioarcb->ioarcb_bus_addr &= (~0x1FULL);
  330. ioarcb->ioadl_bus_addr = 0;
  331. ioarcb->ioadl_length = 0;
  332. ioarcb->data_transfer_length = 0;
  333. ioarcb->add_cmd_param_length = 0;
  334. ioarcb->add_cmd_param_offset = 0;
  335. cmd->ioa_cb->ioasa.ioasc = 0;
  336. cmd->ioa_cb->ioasa.residual_data_length = 0;
  337. cmd->time_left = 0;
  338. }
  339. cmd->cmd_done = NULL;
  340. cmd->scsi_cmd = NULL;
  341. cmd->release = 0;
  342. cmd->completion_req = 0;
  343. cmd->sense_buffer = 0;
  344. cmd->sense_buffer_dma = 0;
  345. cmd->dma_handle = 0;
  346. init_timer(&cmd->timer);
  347. }
  348. /**
  349. * pmcraid_reinit_cmdblk - reinitialize a command block
  350. *
  351. * @cmd: pointer to struct pmcraid_cmd to be reinitialized
  352. *
  353. * Return Value
  354. * None
  355. */
  356. static void pmcraid_reinit_cmdblk(struct pmcraid_cmd *cmd)
  357. {
  358. pmcraid_init_cmdblk(cmd, -1);
  359. }
  360. /**
  361. * pmcraid_get_free_cmd - get a free cmd block from command block pool
  362. * @pinstance: adapter instance structure
  363. *
  364. * Return Value:
  365. * returns pointer to cmd block or NULL if no blocks are available
  366. */
  367. static struct pmcraid_cmd *pmcraid_get_free_cmd(
  368. struct pmcraid_instance *pinstance
  369. )
  370. {
  371. struct pmcraid_cmd *cmd = NULL;
  372. unsigned long lock_flags;
  373. /* free cmd block list is protected by free_pool_lock */
  374. spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
  375. if (!list_empty(&pinstance->free_cmd_pool)) {
  376. cmd = list_entry(pinstance->free_cmd_pool.next,
  377. struct pmcraid_cmd, free_list);
  378. list_del(&cmd->free_list);
  379. }
  380. spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
  381. /* Initialize the command block before giving it the caller */
  382. if (cmd != NULL)
  383. pmcraid_reinit_cmdblk(cmd);
  384. return cmd;
  385. }
  386. /**
  387. * pmcraid_return_cmd - return a completed command block back into free pool
  388. * @cmd: pointer to the command block
  389. *
  390. * Return Value:
  391. * nothing
  392. */
  393. void pmcraid_return_cmd(struct pmcraid_cmd *cmd)
  394. {
  395. struct pmcraid_instance *pinstance = cmd->drv_inst;
  396. unsigned long lock_flags;
  397. spin_lock_irqsave(&pinstance->free_pool_lock, lock_flags);
  398. list_add_tail(&cmd->free_list, &pinstance->free_cmd_pool);
  399. spin_unlock_irqrestore(&pinstance->free_pool_lock, lock_flags);
  400. }
  401. /**
  402. * pmcraid_read_interrupts - reads IOA interrupts
  403. *
  404. * @pinstance: pointer to adapter instance structure
  405. *
  406. * Return value
  407. * interrupts read from IOA
  408. */
  409. static u32 pmcraid_read_interrupts(struct pmcraid_instance *pinstance)
  410. {
  411. return (pinstance->interrupt_mode) ?
  412. ioread32(pinstance->int_regs.ioa_host_msix_interrupt_reg) :
  413. ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  414. }
  415. /**
  416. * pmcraid_disable_interrupts - Masks and clears all specified interrupts
  417. *
  418. * @pinstance: pointer to per adapter instance structure
  419. * @intrs: interrupts to disable
  420. *
  421. * Return Value
  422. * None
  423. */
  424. static void pmcraid_disable_interrupts(
  425. struct pmcraid_instance *pinstance,
  426. u32 intrs
  427. )
  428. {
  429. u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
  430. u32 nmask = gmask | GLOBAL_INTERRUPT_MASK;
  431. iowrite32(intrs, pinstance->int_regs.ioa_host_interrupt_clr_reg);
  432. iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
  433. ioread32(pinstance->int_regs.global_interrupt_mask_reg);
  434. if (!pinstance->interrupt_mode) {
  435. iowrite32(intrs,
  436. pinstance->int_regs.ioa_host_interrupt_mask_reg);
  437. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  438. }
  439. }
  440. /**
  441. * pmcraid_enable_interrupts - Enables specified interrupts
  442. *
  443. * @pinstance: pointer to per adapter instance structure
  444. * @intr: interrupts to enable
  445. *
  446. * Return Value
  447. * None
  448. */
  449. static void pmcraid_enable_interrupts(
  450. struct pmcraid_instance *pinstance,
  451. u32 intrs
  452. )
  453. {
  454. u32 gmask = ioread32(pinstance->int_regs.global_interrupt_mask_reg);
  455. u32 nmask = gmask & (~GLOBAL_INTERRUPT_MASK);
  456. iowrite32(nmask, pinstance->int_regs.global_interrupt_mask_reg);
  457. if (!pinstance->interrupt_mode) {
  458. iowrite32(~intrs,
  459. pinstance->int_regs.ioa_host_interrupt_mask_reg);
  460. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  461. }
  462. pmcraid_info("enabled interrupts global mask = %x intr_mask = %x\n",
  463. ioread32(pinstance->int_regs.global_interrupt_mask_reg),
  464. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg));
  465. }
  466. /**
  467. * pmcraid_clr_trans_op - clear trans to op interrupt
  468. *
  469. * @pinstance: pointer to per adapter instance structure
  470. *
  471. * Return Value
  472. * None
  473. */
  474. static void pmcraid_clr_trans_op(
  475. struct pmcraid_instance *pinstance
  476. )
  477. {
  478. unsigned long lock_flags;
  479. if (!pinstance->interrupt_mode) {
  480. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  481. pinstance->int_regs.ioa_host_interrupt_mask_reg);
  482. ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  483. iowrite32(INTRS_TRANSITION_TO_OPERATIONAL,
  484. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  485. ioread32(pinstance->int_regs.ioa_host_interrupt_clr_reg);
  486. }
  487. if (pinstance->reset_cmd != NULL) {
  488. del_timer(&pinstance->reset_cmd->timer);
  489. spin_lock_irqsave(
  490. pinstance->host->host_lock, lock_flags);
  491. pinstance->reset_cmd->cmd_done(pinstance->reset_cmd);
  492. spin_unlock_irqrestore(
  493. pinstance->host->host_lock, lock_flags);
  494. }
  495. }
  496. /**
  497. * pmcraid_reset_type - Determine the required reset type
  498. * @pinstance: pointer to adapter instance structure
  499. *
  500. * IOA requires hard reset if any of the following conditions is true.
  501. * 1. If HRRQ valid interrupt is not masked
  502. * 2. IOA reset alert doorbell is set
  503. * 3. If there are any error interrupts
  504. */
  505. static void pmcraid_reset_type(struct pmcraid_instance *pinstance)
  506. {
  507. u32 mask;
  508. u32 intrs;
  509. u32 alerts;
  510. mask = ioread32(pinstance->int_regs.ioa_host_interrupt_mask_reg);
  511. intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  512. alerts = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  513. if ((mask & INTRS_HRRQ_VALID) == 0 ||
  514. (alerts & DOORBELL_IOA_RESET_ALERT) ||
  515. (intrs & PMCRAID_ERROR_INTERRUPTS)) {
  516. pmcraid_info("IOA requires hard reset\n");
  517. pinstance->ioa_hard_reset = 1;
  518. }
  519. /* If unit check is active, trigger the dump */
  520. if (intrs & INTRS_IOA_UNIT_CHECK)
  521. pinstance->ioa_unit_check = 1;
  522. }
  523. /**
  524. * pmcraid_bist_done - completion function for PCI BIST
  525. * @cmd: pointer to reset command
  526. * Return Value
  527. * none
  528. */
  529. static void pmcraid_ioa_reset(struct pmcraid_cmd *);
  530. static void pmcraid_bist_done(struct pmcraid_cmd *cmd)
  531. {
  532. struct pmcraid_instance *pinstance = cmd->drv_inst;
  533. unsigned long lock_flags;
  534. int rc;
  535. u16 pci_reg;
  536. rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
  537. /* If PCI config space can't be accessed wait for another two secs */
  538. if ((rc != PCIBIOS_SUCCESSFUL || (!(pci_reg & PCI_COMMAND_MEMORY))) &&
  539. cmd->time_left > 0) {
  540. pmcraid_info("BIST not complete, waiting another 2 secs\n");
  541. cmd->timer.expires = jiffies + cmd->time_left;
  542. cmd->time_left = 0;
  543. cmd->timer.data = (unsigned long)cmd;
  544. cmd->timer.function =
  545. (void (*)(unsigned long))pmcraid_bist_done;
  546. add_timer(&cmd->timer);
  547. } else {
  548. cmd->time_left = 0;
  549. pmcraid_info("BIST is complete, proceeding with reset\n");
  550. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  551. pmcraid_ioa_reset(cmd);
  552. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  553. }
  554. }
  555. /**
  556. * pmcraid_start_bist - starts BIST
  557. * @cmd: pointer to reset cmd
  558. * Return Value
  559. * none
  560. */
  561. static void pmcraid_start_bist(struct pmcraid_cmd *cmd)
  562. {
  563. struct pmcraid_instance *pinstance = cmd->drv_inst;
  564. u32 doorbells, intrs;
  565. /* proceed with bist and wait for 2 seconds */
  566. iowrite32(DOORBELL_IOA_START_BIST,
  567. pinstance->int_regs.host_ioa_interrupt_reg);
  568. doorbells = ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  569. intrs = ioread32(pinstance->int_regs.ioa_host_interrupt_reg);
  570. pmcraid_info("doorbells after start bist: %x intrs: %x\n",
  571. doorbells, intrs);
  572. cmd->time_left = msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
  573. cmd->timer.data = (unsigned long)cmd;
  574. cmd->timer.expires = jiffies + msecs_to_jiffies(PMCRAID_BIST_TIMEOUT);
  575. cmd->timer.function = (void (*)(unsigned long))pmcraid_bist_done;
  576. add_timer(&cmd->timer);
  577. }
  578. /**
  579. * pmcraid_reset_alert_done - completion routine for reset_alert
  580. * @cmd: pointer to command block used in reset sequence
  581. * Return value
  582. * None
  583. */
  584. static void pmcraid_reset_alert_done(struct pmcraid_cmd *cmd)
  585. {
  586. struct pmcraid_instance *pinstance = cmd->drv_inst;
  587. u32 status = ioread32(pinstance->ioa_status);
  588. unsigned long lock_flags;
  589. /* if the critical operation in progress bit is set or the wait times
  590. * out, invoke reset engine to proceed with hard reset. If there is
  591. * some more time to wait, restart the timer
  592. */
  593. if (((status & INTRS_CRITICAL_OP_IN_PROGRESS) == 0) ||
  594. cmd->time_left <= 0) {
  595. pmcraid_info("critical op is reset proceeding with reset\n");
  596. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  597. pmcraid_ioa_reset(cmd);
  598. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  599. } else {
  600. pmcraid_info("critical op is not yet reset waiting again\n");
  601. /* restart timer if some more time is available to wait */
  602. cmd->time_left -= PMCRAID_CHECK_FOR_RESET_TIMEOUT;
  603. cmd->timer.data = (unsigned long)cmd;
  604. cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
  605. cmd->timer.function =
  606. (void (*)(unsigned long))pmcraid_reset_alert_done;
  607. add_timer(&cmd->timer);
  608. }
  609. }
  610. /**
  611. * pmcraid_reset_alert - alerts IOA for a possible reset
  612. * @cmd : command block to be used for reset sequence.
  613. *
  614. * Return Value
  615. * returns 0 if pci config-space is accessible and RESET_DOORBELL is
  616. * successfully written to IOA. Returns non-zero in case pci_config_space
  617. * is not accessible
  618. */
  619. static void pmcraid_notify_ioastate(struct pmcraid_instance *, u32);
  620. static void pmcraid_reset_alert(struct pmcraid_cmd *cmd)
  621. {
  622. struct pmcraid_instance *pinstance = cmd->drv_inst;
  623. u32 doorbells;
  624. int rc;
  625. u16 pci_reg;
  626. /* If we are able to access IOA PCI config space, alert IOA that we are
  627. * going to reset it soon. This enables IOA to preserv persistent error
  628. * data if any. In case memory space is not accessible, proceed with
  629. * BIST or slot_reset
  630. */
  631. rc = pci_read_config_word(pinstance->pdev, PCI_COMMAND, &pci_reg);
  632. if ((rc == PCIBIOS_SUCCESSFUL) && (pci_reg & PCI_COMMAND_MEMORY)) {
  633. /* wait for IOA permission i.e until CRITICAL_OPERATION bit is
  634. * reset IOA doesn't generate any interrupts when CRITICAL
  635. * OPERATION bit is reset. A timer is started to wait for this
  636. * bit to be reset.
  637. */
  638. cmd->time_left = PMCRAID_RESET_TIMEOUT;
  639. cmd->timer.data = (unsigned long)cmd;
  640. cmd->timer.expires = jiffies + PMCRAID_CHECK_FOR_RESET_TIMEOUT;
  641. cmd->timer.function =
  642. (void (*)(unsigned long))pmcraid_reset_alert_done;
  643. add_timer(&cmd->timer);
  644. iowrite32(DOORBELL_IOA_RESET_ALERT,
  645. pinstance->int_regs.host_ioa_interrupt_reg);
  646. doorbells =
  647. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  648. pmcraid_info("doorbells after reset alert: %x\n", doorbells);
  649. } else {
  650. pmcraid_info("PCI config is not accessible starting BIST\n");
  651. pinstance->ioa_state = IOA_STATE_IN_HARD_RESET;
  652. pmcraid_start_bist(cmd);
  653. }
  654. }
  655. /**
  656. * pmcraid_timeout_handler - Timeout handler for internally generated ops
  657. *
  658. * @cmd : pointer to command structure, that got timedout
  659. *
  660. * This function blocks host requests and initiates an adapter reset.
  661. *
  662. * Return value:
  663. * None
  664. */
  665. static void pmcraid_timeout_handler(struct pmcraid_cmd *cmd)
  666. {
  667. struct pmcraid_instance *pinstance = cmd->drv_inst;
  668. unsigned long lock_flags;
  669. dev_info(&pinstance->pdev->dev,
  670. "Adapter being reset due to cmd(CDB[0] = %x) timeout\n",
  671. cmd->ioa_cb->ioarcb.cdb[0]);
  672. /* Command timeouts result in hard reset sequence. The command that got
  673. * timed out may be the one used as part of reset sequence. In this
  674. * case restart reset sequence using the same command block even if
  675. * reset is in progress. Otherwise fail this command and get a free
  676. * command block to restart the reset sequence.
  677. */
  678. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  679. if (!pinstance->ioa_reset_in_progress) {
  680. pinstance->ioa_reset_attempts = 0;
  681. cmd = pmcraid_get_free_cmd(pinstance);
  682. /* If we are out of command blocks, just return here itself.
  683. * Some other command's timeout handler can do the reset job
  684. */
  685. if (cmd == NULL) {
  686. spin_unlock_irqrestore(pinstance->host->host_lock,
  687. lock_flags);
  688. pmcraid_err("no free cmnd block for timeout handler\n");
  689. return;
  690. }
  691. pinstance->reset_cmd = cmd;
  692. pinstance->ioa_reset_in_progress = 1;
  693. } else {
  694. pmcraid_info("reset is already in progress\n");
  695. if (pinstance->reset_cmd != cmd) {
  696. /* This command should have been given to IOA, this
  697. * command will be completed by fail_outstanding_cmds
  698. * anyway
  699. */
  700. pmcraid_err("cmd is pending but reset in progress\n");
  701. }
  702. /* If this command was being used as part of the reset
  703. * sequence, set cmd_done pointer to pmcraid_ioa_reset. This
  704. * causes fail_outstanding_commands not to return the command
  705. * block back to free pool
  706. */
  707. if (cmd == pinstance->reset_cmd)
  708. cmd->cmd_done = pmcraid_ioa_reset;
  709. }
  710. /* Notify apps of important IOA bringup/bringdown sequences */
  711. if (pinstance->scn.ioa_state != PMC_DEVICE_EVENT_RESET_START &&
  712. pinstance->scn.ioa_state != PMC_DEVICE_EVENT_SHUTDOWN_START)
  713. pmcraid_notify_ioastate(pinstance,
  714. PMC_DEVICE_EVENT_RESET_START);
  715. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  716. scsi_block_requests(pinstance->host);
  717. pmcraid_reset_alert(cmd);
  718. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  719. }
  720. /**
  721. * pmcraid_internal_done - completion routine for internally generated cmds
  722. *
  723. * @cmd: command that got response from IOA
  724. *
  725. * Return Value:
  726. * none
  727. */
  728. static void pmcraid_internal_done(struct pmcraid_cmd *cmd)
  729. {
  730. pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
  731. cmd->ioa_cb->ioarcb.cdb[0],
  732. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  733. /* Some of the internal commands are sent with callers blocking for the
  734. * response. Same will be indicated as part of cmd->completion_req
  735. * field. Response path needs to wake up any waiters waiting for cmd
  736. * completion if this flag is set.
  737. */
  738. if (cmd->completion_req) {
  739. cmd->completion_req = 0;
  740. complete(&cmd->wait_for_completion);
  741. }
  742. /* most of the internal commands are completed by caller itself, so
  743. * no need to return the command block back to free pool until we are
  744. * required to do so (e.g once done with initialization).
  745. */
  746. if (cmd->release) {
  747. cmd->release = 0;
  748. pmcraid_return_cmd(cmd);
  749. }
  750. }
  751. /**
  752. * pmcraid_reinit_cfgtable_done - done function for cfg table reinitialization
  753. *
  754. * @cmd: command that got response from IOA
  755. *
  756. * This routine is called after driver re-reads configuration table due to a
  757. * lost CCN. It returns the command block back to free pool and schedules
  758. * worker thread to add/delete devices into the system.
  759. *
  760. * Return Value:
  761. * none
  762. */
  763. static void pmcraid_reinit_cfgtable_done(struct pmcraid_cmd *cmd)
  764. {
  765. pmcraid_info("response internal cmd CDB[0] = %x ioasc = %x\n",
  766. cmd->ioa_cb->ioarcb.cdb[0],
  767. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  768. if (cmd->release) {
  769. cmd->release = 0;
  770. pmcraid_return_cmd(cmd);
  771. }
  772. pmcraid_info("scheduling worker for config table reinitialization\n");
  773. schedule_work(&cmd->drv_inst->worker_q);
  774. }
  775. /**
  776. * pmcraid_erp_done - Process completion of SCSI error response from device
  777. * @cmd: pmcraid_command
  778. *
  779. * This function copies the sense buffer into the scsi_cmd struct and completes
  780. * scsi_cmd by calling scsi_done function.
  781. *
  782. * Return value:
  783. * none
  784. */
  785. static void pmcraid_erp_done(struct pmcraid_cmd *cmd)
  786. {
  787. struct scsi_cmnd *scsi_cmd = cmd->scsi_cmd;
  788. struct pmcraid_instance *pinstance = cmd->drv_inst;
  789. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  790. if (PMCRAID_IOASC_SENSE_KEY(ioasc) > 0) {
  791. scsi_cmd->result |= (DID_ERROR << 16);
  792. scmd_printk(KERN_INFO, scsi_cmd,
  793. "command CDB[0] = %x failed with IOASC: 0x%08X\n",
  794. cmd->ioa_cb->ioarcb.cdb[0], ioasc);
  795. }
  796. /* if we had allocated sense buffers for request sense, copy the sense
  797. * release the buffers
  798. */
  799. if (cmd->sense_buffer != NULL) {
  800. memcpy(scsi_cmd->sense_buffer,
  801. cmd->sense_buffer,
  802. SCSI_SENSE_BUFFERSIZE);
  803. pci_free_consistent(pinstance->pdev,
  804. SCSI_SENSE_BUFFERSIZE,
  805. cmd->sense_buffer, cmd->sense_buffer_dma);
  806. cmd->sense_buffer = NULL;
  807. cmd->sense_buffer_dma = 0;
  808. }
  809. scsi_dma_unmap(scsi_cmd);
  810. pmcraid_return_cmd(cmd);
  811. scsi_cmd->scsi_done(scsi_cmd);
  812. }
  813. /**
  814. * pmcraid_fire_command - sends an IOA command to adapter
  815. *
  816. * This function adds the given block into pending command list
  817. * and returns without waiting
  818. *
  819. * @cmd : command to be sent to the device
  820. *
  821. * Return Value
  822. * None
  823. */
  824. static void _pmcraid_fire_command(struct pmcraid_cmd *cmd)
  825. {
  826. struct pmcraid_instance *pinstance = cmd->drv_inst;
  827. unsigned long lock_flags;
  828. /* Add this command block to pending cmd pool. We do this prior to
  829. * writting IOARCB to ioarrin because IOA might complete the command
  830. * by the time we are about to add it to the list. Response handler
  831. * (isr/tasklet) looks for cmd block in the pending pending list.
  832. */
  833. spin_lock_irqsave(&pinstance->pending_pool_lock, lock_flags);
  834. list_add_tail(&cmd->free_list, &pinstance->pending_cmd_pool);
  835. spin_unlock_irqrestore(&pinstance->pending_pool_lock, lock_flags);
  836. atomic_inc(&pinstance->outstanding_cmds);
  837. /* driver writes lower 32-bit value of IOARCB address only */
  838. mb();
  839. iowrite32(le32_to_cpu(cmd->ioa_cb->ioarcb.ioarcb_bus_addr),
  840. pinstance->ioarrin);
  841. }
  842. /**
  843. * pmcraid_send_cmd - fires a command to IOA
  844. *
  845. * This function also sets up timeout function, and command completion
  846. * function
  847. *
  848. * @cmd: pointer to the command block to be fired to IOA
  849. * @cmd_done: command completion function, called once IOA responds
  850. * @timeout: timeout to wait for this command completion
  851. * @timeout_func: timeout handler
  852. *
  853. * Return value
  854. * none
  855. */
  856. static void pmcraid_send_cmd(
  857. struct pmcraid_cmd *cmd,
  858. void (*cmd_done) (struct pmcraid_cmd *),
  859. unsigned long timeout,
  860. void (*timeout_func) (struct pmcraid_cmd *)
  861. )
  862. {
  863. /* initialize done function */
  864. cmd->cmd_done = cmd_done;
  865. if (timeout_func) {
  866. /* setup timeout handler */
  867. cmd->timer.data = (unsigned long)cmd;
  868. cmd->timer.expires = jiffies + timeout;
  869. cmd->timer.function = (void (*)(unsigned long))timeout_func;
  870. add_timer(&cmd->timer);
  871. }
  872. /* fire the command to IOA */
  873. _pmcraid_fire_command(cmd);
  874. }
  875. /**
  876. * pmcraid_ioa_shutdown_done - completion function for IOA shutdown command
  877. * @cmd: pointer to the command block used for sending IOA shutdown command
  878. *
  879. * Return value
  880. * None
  881. */
  882. static void pmcraid_ioa_shutdown_done(struct pmcraid_cmd *cmd)
  883. {
  884. struct pmcraid_instance *pinstance = cmd->drv_inst;
  885. unsigned long lock_flags;
  886. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  887. pmcraid_ioa_reset(cmd);
  888. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  889. }
  890. /**
  891. * pmcraid_ioa_shutdown - sends SHUTDOWN command to ioa
  892. *
  893. * @cmd: pointer to the command block used as part of reset sequence
  894. *
  895. * Return Value
  896. * None
  897. */
  898. static void pmcraid_ioa_shutdown(struct pmcraid_cmd *cmd)
  899. {
  900. pmcraid_info("response for Cancel CCN CDB[0] = %x ioasc = %x\n",
  901. cmd->ioa_cb->ioarcb.cdb[0],
  902. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  903. /* Note that commands sent during reset require next command to be sent
  904. * to IOA. Hence reinit the done function as well as timeout function
  905. */
  906. pmcraid_reinit_cmdblk(cmd);
  907. cmd->ioa_cb->ioarcb.request_type = REQ_TYPE_IOACMD;
  908. cmd->ioa_cb->ioarcb.resource_handle =
  909. cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  910. cmd->ioa_cb->ioarcb.cdb[0] = PMCRAID_IOA_SHUTDOWN;
  911. cmd->ioa_cb->ioarcb.cdb[1] = PMCRAID_SHUTDOWN_NORMAL;
  912. /* fire shutdown command to hardware. */
  913. pmcraid_info("firing normal shutdown command (%d) to IOA\n",
  914. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle));
  915. pmcraid_notify_ioastate(cmd->drv_inst, PMC_DEVICE_EVENT_SHUTDOWN_START);
  916. pmcraid_send_cmd(cmd, pmcraid_ioa_shutdown_done,
  917. PMCRAID_SHUTDOWN_TIMEOUT,
  918. pmcraid_timeout_handler);
  919. }
  920. /**
  921. * pmcraid_get_fwversion_done - completion function for get_fwversion
  922. *
  923. * @cmd: pointer to command block used to send INQUIRY command
  924. *
  925. * Return Value
  926. * none
  927. */
  928. static void pmcraid_querycfg(struct pmcraid_cmd *);
  929. static void pmcraid_get_fwversion_done(struct pmcraid_cmd *cmd)
  930. {
  931. struct pmcraid_instance *pinstance = cmd->drv_inst;
  932. u32 ioasc = le32_to_cpu(cmd->ioa_cb->ioasa.ioasc);
  933. unsigned long lock_flags;
  934. /* configuration table entry size depends on firmware version. If fw
  935. * version is not known, it is not possible to interpret IOA config
  936. * table
  937. */
  938. if (ioasc) {
  939. pmcraid_err("IOA Inquiry failed with %x\n", ioasc);
  940. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  941. pinstance->ioa_state = IOA_STATE_IN_RESET_ALERT;
  942. pmcraid_reset_alert(cmd);
  943. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  944. } else {
  945. pmcraid_querycfg(cmd);
  946. }
  947. }
  948. /**
  949. * pmcraid_get_fwversion - reads firmware version information
  950. *
  951. * @cmd: pointer to command block used to send INQUIRY command
  952. *
  953. * Return Value
  954. * none
  955. */
  956. static void pmcraid_get_fwversion(struct pmcraid_cmd *cmd)
  957. {
  958. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  959. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  960. struct pmcraid_instance *pinstance = cmd->drv_inst;
  961. u16 data_size = sizeof(struct pmcraid_inquiry_data);
  962. pmcraid_reinit_cmdblk(cmd);
  963. ioarcb->request_type = REQ_TYPE_SCSI;
  964. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  965. ioarcb->cdb[0] = INQUIRY;
  966. ioarcb->cdb[1] = 1;
  967. ioarcb->cdb[2] = 0xD0;
  968. ioarcb->cdb[3] = (data_size >> 8) & 0xFF;
  969. ioarcb->cdb[4] = data_size & 0xFF;
  970. /* Since entire inquiry data it can be part of IOARCB itself
  971. */
  972. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  973. offsetof(struct pmcraid_ioarcb,
  974. add_data.u.ioadl[0]));
  975. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  976. ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
  977. ioarcb->request_flags0 |= NO_LINK_DESCS;
  978. ioarcb->data_transfer_length = cpu_to_le32(data_size);
  979. ioadl = &(ioarcb->add_data.u.ioadl[0]);
  980. ioadl->flags = IOADL_FLAGS_LAST_DESC;
  981. ioadl->address = cpu_to_le64(pinstance->inq_data_baddr);
  982. ioadl->data_len = cpu_to_le32(data_size);
  983. pmcraid_send_cmd(cmd, pmcraid_get_fwversion_done,
  984. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  985. }
  986. /**
  987. * pmcraid_identify_hrrq - registers host rrq buffers with IOA
  988. * @cmd: pointer to command block to be used for identify hrrq
  989. *
  990. * Return Value
  991. * none
  992. */
  993. static void pmcraid_identify_hrrq(struct pmcraid_cmd *cmd)
  994. {
  995. struct pmcraid_instance *pinstance = cmd->drv_inst;
  996. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  997. int index = cmd->hrrq_index;
  998. __be64 hrrq_addr = cpu_to_be64(pinstance->hrrq_start_bus_addr[index]);
  999. u32 hrrq_size = cpu_to_be32(sizeof(u32) * PMCRAID_MAX_CMD);
  1000. void (*done_function)(struct pmcraid_cmd *);
  1001. pmcraid_reinit_cmdblk(cmd);
  1002. cmd->hrrq_index = index + 1;
  1003. if (cmd->hrrq_index < pinstance->num_hrrq) {
  1004. done_function = pmcraid_identify_hrrq;
  1005. } else {
  1006. cmd->hrrq_index = 0;
  1007. done_function = pmcraid_get_fwversion;
  1008. }
  1009. /* Initialize ioarcb */
  1010. ioarcb->request_type = REQ_TYPE_IOACMD;
  1011. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  1012. /* initialize the hrrq number where IOA will respond to this command */
  1013. ioarcb->hrrq_id = index;
  1014. ioarcb->cdb[0] = PMCRAID_IDENTIFY_HRRQ;
  1015. ioarcb->cdb[1] = index;
  1016. /* IOA expects 64-bit pci address to be written in B.E format
  1017. * (i.e cdb[2]=MSByte..cdb[9]=LSB.
  1018. */
  1019. pmcraid_info("HRRQ_IDENTIFY with hrrq:ioarcb:index => %llx:%llx:%x\n",
  1020. hrrq_addr, ioarcb->ioarcb_bus_addr, index);
  1021. memcpy(&(ioarcb->cdb[2]), &hrrq_addr, sizeof(hrrq_addr));
  1022. memcpy(&(ioarcb->cdb[10]), &hrrq_size, sizeof(hrrq_size));
  1023. /* Subsequent commands require HRRQ identification to be successful.
  1024. * Note that this gets called even during reset from SCSI mid-layer
  1025. * or tasklet
  1026. */
  1027. pmcraid_send_cmd(cmd, done_function,
  1028. PMCRAID_INTERNAL_TIMEOUT,
  1029. pmcraid_timeout_handler);
  1030. }
  1031. static void pmcraid_process_ccn(struct pmcraid_cmd *cmd);
  1032. static void pmcraid_process_ldn(struct pmcraid_cmd *cmd);
  1033. /**
  1034. * pmcraid_send_hcam_cmd - send an initialized command block(HCAM) to IOA
  1035. *
  1036. * @cmd: initialized command block pointer
  1037. *
  1038. * Return Value
  1039. * none
  1040. */
  1041. static void pmcraid_send_hcam_cmd(struct pmcraid_cmd *cmd)
  1042. {
  1043. if (cmd->ioa_cb->ioarcb.cdb[1] == PMCRAID_HCAM_CODE_CONFIG_CHANGE)
  1044. atomic_set(&(cmd->drv_inst->ccn.ignore), 0);
  1045. else
  1046. atomic_set(&(cmd->drv_inst->ldn.ignore), 0);
  1047. pmcraid_send_cmd(cmd, cmd->cmd_done, 0, NULL);
  1048. }
  1049. /**
  1050. * pmcraid_init_hcam - send an initialized command block(HCAM) to IOA
  1051. *
  1052. * @pinstance: pointer to adapter instance structure
  1053. * @type: HCAM type
  1054. *
  1055. * Return Value
  1056. * pointer to initialized pmcraid_cmd structure or NULL
  1057. */
  1058. static struct pmcraid_cmd *pmcraid_init_hcam
  1059. (
  1060. struct pmcraid_instance *pinstance,
  1061. u8 type
  1062. )
  1063. {
  1064. struct pmcraid_cmd *cmd;
  1065. struct pmcraid_ioarcb *ioarcb;
  1066. struct pmcraid_ioadl_desc *ioadl;
  1067. struct pmcraid_hostrcb *hcam;
  1068. void (*cmd_done) (struct pmcraid_cmd *);
  1069. dma_addr_t dma;
  1070. int rcb_size;
  1071. cmd = pmcraid_get_free_cmd(pinstance);
  1072. if (!cmd) {
  1073. pmcraid_err("no free command blocks for hcam\n");
  1074. return cmd;
  1075. }
  1076. if (type == PMCRAID_HCAM_CODE_CONFIG_CHANGE) {
  1077. rcb_size = sizeof(struct pmcraid_hcam_ccn_ext);
  1078. cmd_done = pmcraid_process_ccn;
  1079. dma = pinstance->ccn.baddr + PMCRAID_AEN_HDR_SIZE;
  1080. hcam = &pinstance->ccn;
  1081. } else {
  1082. rcb_size = sizeof(struct pmcraid_hcam_ldn);
  1083. cmd_done = pmcraid_process_ldn;
  1084. dma = pinstance->ldn.baddr + PMCRAID_AEN_HDR_SIZE;
  1085. hcam = &pinstance->ldn;
  1086. }
  1087. /* initialize command pointer used for HCAM registration */
  1088. hcam->cmd = cmd;
  1089. ioarcb = &cmd->ioa_cb->ioarcb;
  1090. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  1091. offsetof(struct pmcraid_ioarcb,
  1092. add_data.u.ioadl[0]));
  1093. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  1094. ioadl = ioarcb->add_data.u.ioadl;
  1095. /* Initialize ioarcb */
  1096. ioarcb->request_type = REQ_TYPE_HCAM;
  1097. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  1098. ioarcb->cdb[0] = PMCRAID_HOST_CONTROLLED_ASYNC;
  1099. ioarcb->cdb[1] = type;
  1100. ioarcb->cdb[7] = (rcb_size >> 8) & 0xFF;
  1101. ioarcb->cdb[8] = (rcb_size) & 0xFF;
  1102. ioarcb->data_transfer_length = cpu_to_le32(rcb_size);
  1103. ioadl[0].flags |= IOADL_FLAGS_READ_LAST;
  1104. ioadl[0].data_len = cpu_to_le32(rcb_size);
  1105. ioadl[0].address = cpu_to_le32(dma);
  1106. cmd->cmd_done = cmd_done;
  1107. return cmd;
  1108. }
  1109. /**
  1110. * pmcraid_send_hcam - Send an HCAM to IOA
  1111. * @pinstance: ioa config struct
  1112. * @type: HCAM type
  1113. *
  1114. * This function will send a Host Controlled Async command to IOA.
  1115. *
  1116. * Return value:
  1117. * none
  1118. */
  1119. static void pmcraid_send_hcam(struct pmcraid_instance *pinstance, u8 type)
  1120. {
  1121. struct pmcraid_cmd *cmd = pmcraid_init_hcam(pinstance, type);
  1122. pmcraid_send_hcam_cmd(cmd);
  1123. }
  1124. /**
  1125. * pmcraid_prepare_cancel_cmd - prepares a command block to abort another
  1126. *
  1127. * @cmd: pointer to cmd that is used as cancelling command
  1128. * @cmd_to_cancel: pointer to the command that needs to be cancelled
  1129. */
  1130. static void pmcraid_prepare_cancel_cmd(
  1131. struct pmcraid_cmd *cmd,
  1132. struct pmcraid_cmd *cmd_to_cancel
  1133. )
  1134. {
  1135. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  1136. __be64 ioarcb_addr = cmd_to_cancel->ioa_cb->ioarcb.ioarcb_bus_addr;
  1137. /* Get the resource handle to where the command to be aborted has been
  1138. * sent.
  1139. */
  1140. ioarcb->resource_handle = cmd_to_cancel->ioa_cb->ioarcb.resource_handle;
  1141. ioarcb->request_type = REQ_TYPE_IOACMD;
  1142. memset(ioarcb->cdb, 0, PMCRAID_MAX_CDB_LEN);
  1143. ioarcb->cdb[0] = PMCRAID_ABORT_CMD;
  1144. /* IOARCB address of the command to be cancelled is given in
  1145. * cdb[2]..cdb[9] is Big-Endian format. Note that length bits in
  1146. * IOARCB address are not masked.
  1147. */
  1148. ioarcb_addr = cpu_to_be64(ioarcb_addr);
  1149. memcpy(&(ioarcb->cdb[2]), &ioarcb_addr, sizeof(ioarcb_addr));
  1150. }
  1151. /**
  1152. * pmcraid_cancel_hcam - sends ABORT task to abort a given HCAM
  1153. *
  1154. * @cmd: command to be used as cancelling command
  1155. * @type: HCAM type
  1156. * @cmd_done: op done function for the cancelling command
  1157. */
  1158. static void pmcraid_cancel_hcam(
  1159. struct pmcraid_cmd *cmd,
  1160. u8 type,
  1161. void (*cmd_done) (struct pmcraid_cmd *)
  1162. )
  1163. {
  1164. struct pmcraid_instance *pinstance;
  1165. struct pmcraid_hostrcb *hcam;
  1166. pinstance = cmd->drv_inst;
  1167. hcam = (type == PMCRAID_HCAM_CODE_LOG_DATA) ?
  1168. &pinstance->ldn : &pinstance->ccn;
  1169. /* prepare for cancelling previous hcam command. If the HCAM is
  1170. * currently not pending with IOA, we would have hcam->cmd as non-null
  1171. */
  1172. if (hcam->cmd == NULL)
  1173. return;
  1174. pmcraid_prepare_cancel_cmd(cmd, hcam->cmd);
  1175. /* writing to IOARRIN must be protected by host_lock, as mid-layer
  1176. * schedule queuecommand while we are doing this
  1177. */
  1178. pmcraid_send_cmd(cmd, cmd_done,
  1179. PMCRAID_INTERNAL_TIMEOUT,
  1180. pmcraid_timeout_handler);
  1181. }
  1182. /**
  1183. * pmcraid_cancel_ccn - cancel CCN HCAM already registered with IOA
  1184. *
  1185. * @cmd: command block to be used for cancelling the HCAM
  1186. */
  1187. static void pmcraid_cancel_ccn(struct pmcraid_cmd *cmd)
  1188. {
  1189. pmcraid_info("response for Cancel LDN CDB[0] = %x ioasc = %x\n",
  1190. cmd->ioa_cb->ioarcb.cdb[0],
  1191. le32_to_cpu(cmd->ioa_cb->ioasa.ioasc));
  1192. pmcraid_reinit_cmdblk(cmd);
  1193. pmcraid_cancel_hcam(cmd,
  1194. PMCRAID_HCAM_CODE_CONFIG_CHANGE,
  1195. pmcraid_ioa_shutdown);
  1196. }
  1197. /**
  1198. * pmcraid_cancel_ldn - cancel LDN HCAM already registered with IOA
  1199. *
  1200. * @cmd: command block to be used for cancelling the HCAM
  1201. */
  1202. static void pmcraid_cancel_ldn(struct pmcraid_cmd *cmd)
  1203. {
  1204. pmcraid_cancel_hcam(cmd,
  1205. PMCRAID_HCAM_CODE_LOG_DATA,
  1206. pmcraid_cancel_ccn);
  1207. }
  1208. /**
  1209. * pmcraid_expose_resource - check if the resource can be exposed to OS
  1210. *
  1211. * @fw_version: firmware version code
  1212. * @cfgte: pointer to configuration table entry of the resource
  1213. *
  1214. * Return value:
  1215. * true if resource can be added to midlayer, false(0) otherwise
  1216. */
  1217. static int pmcraid_expose_resource(u16 fw_version,
  1218. struct pmcraid_config_table_entry *cfgte)
  1219. {
  1220. int retval = 0;
  1221. if (cfgte->resource_type == RES_TYPE_VSET) {
  1222. if (fw_version <= PMCRAID_FW_VERSION_1)
  1223. retval = ((cfgte->unique_flags1 & 0x80) == 0);
  1224. else
  1225. retval = ((cfgte->unique_flags0 & 0x80) == 0 &&
  1226. (cfgte->unique_flags1 & 0x80) == 0);
  1227. } else if (cfgte->resource_type == RES_TYPE_GSCSI)
  1228. retval = (RES_BUS(cfgte->resource_address) !=
  1229. PMCRAID_VIRTUAL_ENCL_BUS_ID);
  1230. return retval;
  1231. }
  1232. /* attributes supported by pmcraid_event_family */
  1233. enum {
  1234. PMCRAID_AEN_ATTR_UNSPEC,
  1235. PMCRAID_AEN_ATTR_EVENT,
  1236. __PMCRAID_AEN_ATTR_MAX,
  1237. };
  1238. #define PMCRAID_AEN_ATTR_MAX (__PMCRAID_AEN_ATTR_MAX - 1)
  1239. /* commands supported by pmcraid_event_family */
  1240. enum {
  1241. PMCRAID_AEN_CMD_UNSPEC,
  1242. PMCRAID_AEN_CMD_EVENT,
  1243. __PMCRAID_AEN_CMD_MAX,
  1244. };
  1245. #define PMCRAID_AEN_CMD_MAX (__PMCRAID_AEN_CMD_MAX - 1)
  1246. static struct genl_family pmcraid_event_family = {
  1247. .id = GENL_ID_GENERATE,
  1248. .name = "pmcraid",
  1249. .version = 1,
  1250. .maxattr = PMCRAID_AEN_ATTR_MAX
  1251. };
  1252. /**
  1253. * pmcraid_netlink_init - registers pmcraid_event_family
  1254. *
  1255. * Return value:
  1256. * 0 if the pmcraid_event_family is successfully registered
  1257. * with netlink generic, non-zero otherwise
  1258. */
  1259. static int pmcraid_netlink_init(void)
  1260. {
  1261. int result;
  1262. result = genl_register_family(&pmcraid_event_family);
  1263. if (result)
  1264. return result;
  1265. pmcraid_info("registered NETLINK GENERIC group: %d\n",
  1266. pmcraid_event_family.id);
  1267. return result;
  1268. }
  1269. /**
  1270. * pmcraid_netlink_release - unregisters pmcraid_event_family
  1271. *
  1272. * Return value:
  1273. * none
  1274. */
  1275. static void pmcraid_netlink_release(void)
  1276. {
  1277. genl_unregister_family(&pmcraid_event_family);
  1278. }
  1279. /**
  1280. * pmcraid_notify_aen - sends event msg to user space application
  1281. * @pinstance: pointer to adapter instance structure
  1282. * @type: HCAM type
  1283. *
  1284. * Return value:
  1285. * 0 if success, error value in case of any failure.
  1286. */
  1287. static int pmcraid_notify_aen(
  1288. struct pmcraid_instance *pinstance,
  1289. struct pmcraid_aen_msg *aen_msg,
  1290. u32 data_size
  1291. )
  1292. {
  1293. struct sk_buff *skb;
  1294. void *msg_header;
  1295. u32 total_size, nla_genl_hdr_total_size;
  1296. int result;
  1297. aen_msg->hostno = (pinstance->host->unique_id << 16 |
  1298. MINOR(pinstance->cdev.dev));
  1299. aen_msg->length = data_size;
  1300. data_size += sizeof(*aen_msg);
  1301. total_size = nla_total_size(data_size);
  1302. /* Add GENL_HDR to total_size */
  1303. nla_genl_hdr_total_size =
  1304. (total_size + (GENL_HDRLEN +
  1305. ((struct genl_family *)&pmcraid_event_family)->hdrsize)
  1306. + NLMSG_HDRLEN);
  1307. skb = genlmsg_new(nla_genl_hdr_total_size, GFP_ATOMIC);
  1308. if (!skb) {
  1309. pmcraid_err("Failed to allocate aen data SKB of size: %x\n",
  1310. total_size);
  1311. return -ENOMEM;
  1312. }
  1313. /* add the genetlink message header */
  1314. msg_header = genlmsg_put(skb, 0, 0,
  1315. &pmcraid_event_family, 0,
  1316. PMCRAID_AEN_CMD_EVENT);
  1317. if (!msg_header) {
  1318. pmcraid_err("failed to copy command details\n");
  1319. nlmsg_free(skb);
  1320. return -ENOMEM;
  1321. }
  1322. result = nla_put(skb, PMCRAID_AEN_ATTR_EVENT, data_size, aen_msg);
  1323. if (result) {
  1324. pmcraid_err("failed to copy AEN attribute data\n");
  1325. nlmsg_free(skb);
  1326. return -EINVAL;
  1327. }
  1328. /* send genetlink multicast message to notify appplications */
  1329. result = genlmsg_end(skb, msg_header);
  1330. if (result < 0) {
  1331. pmcraid_err("genlmsg_end failed\n");
  1332. nlmsg_free(skb);
  1333. return result;
  1334. }
  1335. result =
  1336. genlmsg_multicast(skb, 0, 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_release - char node "release" entry point
  3134. */
  3135. static int pmcraid_chr_release(struct inode *inode, struct file *filep)
  3136. {
  3137. struct pmcraid_instance *pinstance = filep->private_data;
  3138. filep->private_data = NULL;
  3139. fasync_helper(-1, filep, 0, &pinstance->aen_queue);
  3140. return 0;
  3141. }
  3142. /**
  3143. * pmcraid_fasync - Async notifier registration from applications
  3144. *
  3145. * This function adds the calling process to a driver global queue. When an
  3146. * event occurs, SIGIO will be sent to all processes in this queue.
  3147. */
  3148. static int pmcraid_chr_fasync(int fd, struct file *filep, int mode)
  3149. {
  3150. struct pmcraid_instance *pinstance;
  3151. int rc;
  3152. pinstance = filep->private_data;
  3153. mutex_lock(&pinstance->aen_queue_lock);
  3154. rc = fasync_helper(fd, filep, mode, &pinstance->aen_queue);
  3155. mutex_unlock(&pinstance->aen_queue_lock);
  3156. return rc;
  3157. }
  3158. /**
  3159. * pmcraid_build_passthrough_ioadls - builds SG elements for passthrough
  3160. * commands sent over IOCTL interface
  3161. *
  3162. * @cmd : pointer to struct pmcraid_cmd
  3163. * @buflen : length of the request buffer
  3164. * @direction : data transfer direction
  3165. *
  3166. * Return value
  3167. * 0 on success, non-zero error code on failure
  3168. */
  3169. static int pmcraid_build_passthrough_ioadls(
  3170. struct pmcraid_cmd *cmd,
  3171. int buflen,
  3172. int direction
  3173. )
  3174. {
  3175. struct pmcraid_sglist *sglist = NULL;
  3176. struct scatterlist *sg = NULL;
  3177. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  3178. struct pmcraid_ioadl_desc *ioadl;
  3179. int i;
  3180. sglist = pmcraid_alloc_sglist(buflen);
  3181. if (!sglist) {
  3182. pmcraid_err("can't allocate memory for passthrough SGls\n");
  3183. return -ENOMEM;
  3184. }
  3185. sglist->num_dma_sg = pci_map_sg(cmd->drv_inst->pdev,
  3186. sglist->scatterlist,
  3187. sglist->num_sg, direction);
  3188. if (!sglist->num_dma_sg || sglist->num_dma_sg > PMCRAID_MAX_IOADLS) {
  3189. dev_err(&cmd->drv_inst->pdev->dev,
  3190. "Failed to map passthrough buffer!\n");
  3191. pmcraid_free_sglist(sglist);
  3192. return -EIO;
  3193. }
  3194. cmd->sglist = sglist;
  3195. ioarcb->request_flags0 |= NO_LINK_DESCS;
  3196. ioadl = pmcraid_init_ioadls(cmd, sglist->num_dma_sg);
  3197. /* Initialize IOADL descriptor addresses */
  3198. for_each_sg(sglist->scatterlist, sg, sglist->num_dma_sg, i) {
  3199. ioadl[i].data_len = cpu_to_le32(sg_dma_len(sg));
  3200. ioadl[i].address = cpu_to_le64(sg_dma_address(sg));
  3201. ioadl[i].flags = 0;
  3202. }
  3203. /* setup the last descriptor */
  3204. ioadl[i - 1].flags = IOADL_FLAGS_LAST_DESC;
  3205. return 0;
  3206. }
  3207. /**
  3208. * pmcraid_release_passthrough_ioadls - release passthrough ioadls
  3209. *
  3210. * @cmd: pointer to struct pmcraid_cmd for which ioadls were allocated
  3211. * @buflen: size of the request buffer
  3212. * @direction: data transfer direction
  3213. *
  3214. * Return value
  3215. * 0 on success, non-zero error code on failure
  3216. */
  3217. static void pmcraid_release_passthrough_ioadls(
  3218. struct pmcraid_cmd *cmd,
  3219. int buflen,
  3220. int direction
  3221. )
  3222. {
  3223. struct pmcraid_sglist *sglist = cmd->sglist;
  3224. if (buflen > 0) {
  3225. pci_unmap_sg(cmd->drv_inst->pdev,
  3226. sglist->scatterlist,
  3227. sglist->num_sg,
  3228. direction);
  3229. pmcraid_free_sglist(sglist);
  3230. cmd->sglist = NULL;
  3231. }
  3232. }
  3233. /**
  3234. * pmcraid_ioctl_passthrough - handling passthrough IOCTL commands
  3235. *
  3236. * @pinstance: pointer to adapter instance structure
  3237. * @cmd: ioctl code
  3238. * @arg: pointer to pmcraid_passthrough_buffer user buffer
  3239. *
  3240. * Return value
  3241. * 0 on success, non-zero error code on failure
  3242. */
  3243. static long pmcraid_ioctl_passthrough(
  3244. struct pmcraid_instance *pinstance,
  3245. unsigned int ioctl_cmd,
  3246. unsigned int buflen,
  3247. unsigned long arg
  3248. )
  3249. {
  3250. struct pmcraid_passthrough_ioctl_buffer *buffer;
  3251. struct pmcraid_ioarcb *ioarcb;
  3252. struct pmcraid_cmd *cmd;
  3253. struct pmcraid_cmd *cancel_cmd;
  3254. unsigned long request_buffer;
  3255. unsigned long request_offset;
  3256. unsigned long lock_flags;
  3257. void *ioasa;
  3258. u32 ioasc;
  3259. int request_size;
  3260. int buffer_size;
  3261. u8 access, direction;
  3262. int rc = 0;
  3263. /* If IOA reset is in progress, wait 10 secs for reset to complete */
  3264. if (pinstance->ioa_reset_in_progress) {
  3265. rc = wait_event_interruptible_timeout(
  3266. pinstance->reset_wait_q,
  3267. !pinstance->ioa_reset_in_progress,
  3268. msecs_to_jiffies(10000));
  3269. if (!rc)
  3270. return -ETIMEDOUT;
  3271. else if (rc < 0)
  3272. return -ERESTARTSYS;
  3273. }
  3274. /* If adapter is not in operational state, return error */
  3275. if (pinstance->ioa_state != IOA_STATE_OPERATIONAL) {
  3276. pmcraid_err("IOA is not operational\n");
  3277. return -ENOTTY;
  3278. }
  3279. buffer_size = sizeof(struct pmcraid_passthrough_ioctl_buffer);
  3280. buffer = kmalloc(buffer_size, GFP_KERNEL);
  3281. if (!buffer) {
  3282. pmcraid_err("no memory for passthrough buffer\n");
  3283. return -ENOMEM;
  3284. }
  3285. request_offset =
  3286. offsetof(struct pmcraid_passthrough_ioctl_buffer, request_buffer);
  3287. request_buffer = arg + request_offset;
  3288. rc = __copy_from_user(buffer,
  3289. (struct pmcraid_passthrough_ioctl_buffer *) arg,
  3290. sizeof(struct pmcraid_passthrough_ioctl_buffer));
  3291. ioasa =
  3292. (void *)(arg +
  3293. offsetof(struct pmcraid_passthrough_ioctl_buffer, ioasa));
  3294. if (rc) {
  3295. pmcraid_err("ioctl: can't copy passthrough buffer\n");
  3296. rc = -EFAULT;
  3297. goto out_free_buffer;
  3298. }
  3299. request_size = buffer->ioarcb.data_transfer_length;
  3300. if (buffer->ioarcb.request_flags0 & TRANSFER_DIR_WRITE) {
  3301. access = VERIFY_READ;
  3302. direction = DMA_TO_DEVICE;
  3303. } else {
  3304. access = VERIFY_WRITE;
  3305. direction = DMA_FROM_DEVICE;
  3306. }
  3307. if (request_size > 0) {
  3308. rc = access_ok(access, arg, request_offset + request_size);
  3309. if (!rc) {
  3310. rc = -EFAULT;
  3311. goto out_free_buffer;
  3312. }
  3313. } else if (request_size < 0) {
  3314. rc = -EINVAL;
  3315. goto out_free_buffer;
  3316. }
  3317. /* check if we have any additional command parameters */
  3318. if (buffer->ioarcb.add_cmd_param_length > PMCRAID_ADD_CMD_PARAM_LEN) {
  3319. rc = -EINVAL;
  3320. goto out_free_buffer;
  3321. }
  3322. cmd = pmcraid_get_free_cmd(pinstance);
  3323. if (!cmd) {
  3324. pmcraid_err("free command block is not available\n");
  3325. rc = -ENOMEM;
  3326. goto out_free_buffer;
  3327. }
  3328. cmd->scsi_cmd = NULL;
  3329. ioarcb = &(cmd->ioa_cb->ioarcb);
  3330. /* Copy the user-provided IOARCB stuff field by field */
  3331. ioarcb->resource_handle = buffer->ioarcb.resource_handle;
  3332. ioarcb->data_transfer_length = buffer->ioarcb.data_transfer_length;
  3333. ioarcb->cmd_timeout = buffer->ioarcb.cmd_timeout;
  3334. ioarcb->request_type = buffer->ioarcb.request_type;
  3335. ioarcb->request_flags0 = buffer->ioarcb.request_flags0;
  3336. ioarcb->request_flags1 = buffer->ioarcb.request_flags1;
  3337. memcpy(ioarcb->cdb, buffer->ioarcb.cdb, PMCRAID_MAX_CDB_LEN);
  3338. if (buffer->ioarcb.add_cmd_param_length) {
  3339. ioarcb->add_cmd_param_length =
  3340. buffer->ioarcb.add_cmd_param_length;
  3341. ioarcb->add_cmd_param_offset =
  3342. buffer->ioarcb.add_cmd_param_offset;
  3343. memcpy(ioarcb->add_data.u.add_cmd_params,
  3344. buffer->ioarcb.add_data.u.add_cmd_params,
  3345. buffer->ioarcb.add_cmd_param_length);
  3346. }
  3347. /* set hrrq number where the IOA should respond to. Note that all cmds
  3348. * generated internally uses hrrq_id 0, exception to this is the cmd
  3349. * block of scsi_cmd which is re-used (e.g. cancel/abort), which uses
  3350. * hrrq_id assigned here in queuecommand
  3351. */
  3352. ioarcb->hrrq_id = atomic_add_return(1, &(pinstance->last_message_id)) %
  3353. pinstance->num_hrrq;
  3354. if (request_size) {
  3355. rc = pmcraid_build_passthrough_ioadls(cmd,
  3356. request_size,
  3357. direction);
  3358. if (rc) {
  3359. pmcraid_err("couldn't build passthrough ioadls\n");
  3360. goto out_free_buffer;
  3361. }
  3362. } else if (request_size < 0) {
  3363. rc = -EINVAL;
  3364. goto out_free_buffer;
  3365. }
  3366. /* If data is being written into the device, copy the data from user
  3367. * buffers
  3368. */
  3369. if (direction == DMA_TO_DEVICE && request_size > 0) {
  3370. rc = pmcraid_copy_sglist(cmd->sglist,
  3371. request_buffer,
  3372. request_size,
  3373. direction);
  3374. if (rc) {
  3375. pmcraid_err("failed to copy user buffer\n");
  3376. goto out_free_sglist;
  3377. }
  3378. }
  3379. /* passthrough ioctl is a blocking command so, put the user to sleep
  3380. * until timeout. Note that a timeout value of 0 means, do timeout.
  3381. */
  3382. cmd->cmd_done = pmcraid_internal_done;
  3383. init_completion(&cmd->wait_for_completion);
  3384. cmd->completion_req = 1;
  3385. pmcraid_info("command(%d) (CDB[0] = %x) for %x\n",
  3386. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle) >> 2,
  3387. cmd->ioa_cb->ioarcb.cdb[0],
  3388. le32_to_cpu(cmd->ioa_cb->ioarcb.resource_handle));
  3389. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  3390. _pmcraid_fire_command(cmd);
  3391. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  3392. /* NOTE ! Remove the below line once abort_task is implemented
  3393. * in firmware. This line disables ioctl command timeout handling logic
  3394. * similar to IO command timeout handling, making ioctl commands to wait
  3395. * until the command completion regardless of timeout value specified in
  3396. * ioarcb
  3397. */
  3398. buffer->ioarcb.cmd_timeout = 0;
  3399. /* If command timeout is specified put caller to wait till that time,
  3400. * otherwise it would be blocking wait. If command gets timed out, it
  3401. * will be aborted.
  3402. */
  3403. if (buffer->ioarcb.cmd_timeout == 0) {
  3404. wait_for_completion(&cmd->wait_for_completion);
  3405. } else if (!wait_for_completion_timeout(
  3406. &cmd->wait_for_completion,
  3407. msecs_to_jiffies(buffer->ioarcb.cmd_timeout * 1000))) {
  3408. pmcraid_info("aborting cmd %d (CDB[0] = %x) due to timeout\n",
  3409. le32_to_cpu(cmd->ioa_cb->ioarcb.response_handle >> 2),
  3410. cmd->ioa_cb->ioarcb.cdb[0]);
  3411. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  3412. cancel_cmd = pmcraid_abort_cmd(cmd);
  3413. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  3414. if (cancel_cmd) {
  3415. wait_for_completion(&cancel_cmd->wait_for_completion);
  3416. ioasc = cancel_cmd->ioa_cb->ioasa.ioasc;
  3417. pmcraid_return_cmd(cancel_cmd);
  3418. /* if abort task couldn't find the command i.e it got
  3419. * completed prior to aborting, return good completion.
  3420. * if command got aborted successfully or there was IOA
  3421. * reset due to abort task itself getting timedout then
  3422. * return -ETIMEDOUT
  3423. */
  3424. if (ioasc == PMCRAID_IOASC_IOA_WAS_RESET ||
  3425. PMCRAID_IOASC_SENSE_KEY(ioasc) == 0x00) {
  3426. if (ioasc != PMCRAID_IOASC_GC_IOARCB_NOTFOUND)
  3427. rc = -ETIMEDOUT;
  3428. goto out_handle_response;
  3429. }
  3430. }
  3431. /* no command block for abort task or abort task failed to abort
  3432. * the IOARCB, then wait for 150 more seconds and initiate reset
  3433. * sequence after timeout
  3434. */
  3435. if (!wait_for_completion_timeout(
  3436. &cmd->wait_for_completion,
  3437. msecs_to_jiffies(150 * 1000))) {
  3438. pmcraid_reset_bringup(cmd->drv_inst);
  3439. rc = -ETIMEDOUT;
  3440. }
  3441. }
  3442. out_handle_response:
  3443. /* copy entire IOASA buffer and return IOCTL success.
  3444. * If copying IOASA to user-buffer fails, return
  3445. * EFAULT
  3446. */
  3447. if (copy_to_user(ioasa, &cmd->ioa_cb->ioasa,
  3448. sizeof(struct pmcraid_ioasa))) {
  3449. pmcraid_err("failed to copy ioasa buffer to user\n");
  3450. rc = -EFAULT;
  3451. }
  3452. /* If the data transfer was from device, copy the data onto user
  3453. * buffers
  3454. */
  3455. else if (direction == DMA_FROM_DEVICE && request_size > 0) {
  3456. rc = pmcraid_copy_sglist(cmd->sglist,
  3457. request_buffer,
  3458. request_size,
  3459. direction);
  3460. if (rc) {
  3461. pmcraid_err("failed to copy user buffer\n");
  3462. rc = -EFAULT;
  3463. }
  3464. }
  3465. out_free_sglist:
  3466. pmcraid_release_passthrough_ioadls(cmd, request_size, direction);
  3467. pmcraid_return_cmd(cmd);
  3468. out_free_buffer:
  3469. kfree(buffer);
  3470. return rc;
  3471. }
  3472. /**
  3473. * pmcraid_ioctl_driver - ioctl handler for commands handled by driver itself
  3474. *
  3475. * @pinstance: pointer to adapter instance structure
  3476. * @cmd: ioctl command passed in
  3477. * @buflen: length of user_buffer
  3478. * @user_buffer: user buffer pointer
  3479. *
  3480. * Return Value
  3481. * 0 in case of success, otherwise appropriate error code
  3482. */
  3483. static long pmcraid_ioctl_driver(
  3484. struct pmcraid_instance *pinstance,
  3485. unsigned int cmd,
  3486. unsigned int buflen,
  3487. void __user *user_buffer
  3488. )
  3489. {
  3490. int rc = -ENOSYS;
  3491. if (!access_ok(VERIFY_READ, user_buffer, _IOC_SIZE(cmd))) {
  3492. pmcraid_err("ioctl_driver: access fault in request buffer\n");
  3493. return -EFAULT;
  3494. }
  3495. switch (cmd) {
  3496. case PMCRAID_IOCTL_RESET_ADAPTER:
  3497. pmcraid_reset_bringup(pinstance);
  3498. rc = 0;
  3499. break;
  3500. default:
  3501. break;
  3502. }
  3503. return rc;
  3504. }
  3505. /**
  3506. * pmcraid_check_ioctl_buffer - check for proper access to user buffer
  3507. *
  3508. * @cmd: ioctl command
  3509. * @arg: user buffer
  3510. * @hdr: pointer to kernel memory for pmcraid_ioctl_header
  3511. *
  3512. * Return Value
  3513. * negetive error code if there are access issues, otherwise zero.
  3514. * Upon success, returns ioctl header copied out of user buffer.
  3515. */
  3516. static int pmcraid_check_ioctl_buffer(
  3517. int cmd,
  3518. void __user *arg,
  3519. struct pmcraid_ioctl_header *hdr
  3520. )
  3521. {
  3522. int rc = 0;
  3523. int access = VERIFY_READ;
  3524. if (copy_from_user(hdr, arg, sizeof(struct pmcraid_ioctl_header))) {
  3525. pmcraid_err("couldn't copy ioctl header from user buffer\n");
  3526. return -EFAULT;
  3527. }
  3528. /* check for valid driver signature */
  3529. rc = memcmp(hdr->signature,
  3530. PMCRAID_IOCTL_SIGNATURE,
  3531. sizeof(hdr->signature));
  3532. if (rc) {
  3533. pmcraid_err("signature verification failed\n");
  3534. return -EINVAL;
  3535. }
  3536. /* check for appropriate buffer access */
  3537. if ((_IOC_DIR(cmd) & _IOC_READ) == _IOC_READ)
  3538. access = VERIFY_WRITE;
  3539. rc = access_ok(access,
  3540. (arg + sizeof(struct pmcraid_ioctl_header)),
  3541. hdr->buffer_length);
  3542. if (!rc) {
  3543. pmcraid_err("access failed for user buffer of size %d\n",
  3544. hdr->buffer_length);
  3545. return -EFAULT;
  3546. }
  3547. return 0;
  3548. }
  3549. /**
  3550. * pmcraid_ioctl - char node ioctl entry point
  3551. */
  3552. static long pmcraid_chr_ioctl(
  3553. struct file *filep,
  3554. unsigned int cmd,
  3555. unsigned long arg
  3556. )
  3557. {
  3558. struct pmcraid_instance *pinstance = NULL;
  3559. struct pmcraid_ioctl_header *hdr = NULL;
  3560. int retval = -ENOTTY;
  3561. hdr = kmalloc(GFP_KERNEL, sizeof(struct pmcraid_ioctl_header));
  3562. if (!hdr) {
  3563. pmcraid_err("faile to allocate memory for ioctl header\n");
  3564. return -ENOMEM;
  3565. }
  3566. retval = pmcraid_check_ioctl_buffer(cmd, (void *)arg, hdr);
  3567. if (retval) {
  3568. pmcraid_info("chr_ioctl: header check failed\n");
  3569. kfree(hdr);
  3570. return retval;
  3571. }
  3572. pinstance = filep->private_data;
  3573. if (!pinstance) {
  3574. pmcraid_info("adapter instance is not found\n");
  3575. kfree(hdr);
  3576. return -ENOTTY;
  3577. }
  3578. switch (_IOC_TYPE(cmd)) {
  3579. case PMCRAID_PASSTHROUGH_IOCTL:
  3580. /* If ioctl code is to download microcode, we need to block
  3581. * mid-layer requests.
  3582. */
  3583. if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
  3584. scsi_block_requests(pinstance->host);
  3585. retval = pmcraid_ioctl_passthrough(pinstance,
  3586. cmd,
  3587. hdr->buffer_length,
  3588. arg);
  3589. if (cmd == PMCRAID_IOCTL_DOWNLOAD_MICROCODE)
  3590. scsi_unblock_requests(pinstance->host);
  3591. break;
  3592. case PMCRAID_DRIVER_IOCTL:
  3593. arg += sizeof(struct pmcraid_ioctl_header);
  3594. retval = pmcraid_ioctl_driver(pinstance,
  3595. cmd,
  3596. hdr->buffer_length,
  3597. (void __user *)arg);
  3598. break;
  3599. default:
  3600. retval = -ENOTTY;
  3601. break;
  3602. }
  3603. kfree(hdr);
  3604. return retval;
  3605. }
  3606. /**
  3607. * File operations structure for management interface
  3608. */
  3609. static const struct file_operations pmcraid_fops = {
  3610. .owner = THIS_MODULE,
  3611. .open = pmcraid_chr_open,
  3612. .release = pmcraid_chr_release,
  3613. .fasync = pmcraid_chr_fasync,
  3614. .unlocked_ioctl = pmcraid_chr_ioctl,
  3615. #ifdef CONFIG_COMPAT
  3616. .compat_ioctl = pmcraid_chr_ioctl,
  3617. #endif
  3618. .llseek = noop_llseek,
  3619. };
  3620. /**
  3621. * pmcraid_show_log_level - Display adapter's error logging level
  3622. * @dev: class device struct
  3623. * @buf: buffer
  3624. *
  3625. * Return value:
  3626. * number of bytes printed to buffer
  3627. */
  3628. static ssize_t pmcraid_show_log_level(
  3629. struct device *dev,
  3630. struct device_attribute *attr,
  3631. char *buf)
  3632. {
  3633. struct Scsi_Host *shost = class_to_shost(dev);
  3634. struct pmcraid_instance *pinstance =
  3635. (struct pmcraid_instance *)shost->hostdata;
  3636. return snprintf(buf, PAGE_SIZE, "%d\n", pinstance->current_log_level);
  3637. }
  3638. /**
  3639. * pmcraid_store_log_level - Change the adapter's error logging level
  3640. * @dev: class device struct
  3641. * @buf: buffer
  3642. * @count: not used
  3643. *
  3644. * Return value:
  3645. * number of bytes printed to buffer
  3646. */
  3647. static ssize_t pmcraid_store_log_level(
  3648. struct device *dev,
  3649. struct device_attribute *attr,
  3650. const char *buf,
  3651. size_t count
  3652. )
  3653. {
  3654. struct Scsi_Host *shost;
  3655. struct pmcraid_instance *pinstance;
  3656. unsigned long val;
  3657. if (strict_strtoul(buf, 10, &val))
  3658. return -EINVAL;
  3659. /* log-level should be from 0 to 2 */
  3660. if (val > 2)
  3661. return -EINVAL;
  3662. shost = class_to_shost(dev);
  3663. pinstance = (struct pmcraid_instance *)shost->hostdata;
  3664. pinstance->current_log_level = val;
  3665. return strlen(buf);
  3666. }
  3667. static struct device_attribute pmcraid_log_level_attr = {
  3668. .attr = {
  3669. .name = "log_level",
  3670. .mode = S_IRUGO | S_IWUSR,
  3671. },
  3672. .show = pmcraid_show_log_level,
  3673. .store = pmcraid_store_log_level,
  3674. };
  3675. /**
  3676. * pmcraid_show_drv_version - Display driver version
  3677. * @dev: class device struct
  3678. * @buf: buffer
  3679. *
  3680. * Return value:
  3681. * number of bytes printed to buffer
  3682. */
  3683. static ssize_t pmcraid_show_drv_version(
  3684. struct device *dev,
  3685. struct device_attribute *attr,
  3686. char *buf
  3687. )
  3688. {
  3689. return snprintf(buf, PAGE_SIZE, "version: %s\n",
  3690. PMCRAID_DRIVER_VERSION);
  3691. }
  3692. static struct device_attribute pmcraid_driver_version_attr = {
  3693. .attr = {
  3694. .name = "drv_version",
  3695. .mode = S_IRUGO,
  3696. },
  3697. .show = pmcraid_show_drv_version,
  3698. };
  3699. /**
  3700. * pmcraid_show_io_adapter_id - Display driver assigned adapter id
  3701. * @dev: class device struct
  3702. * @buf: buffer
  3703. *
  3704. * Return value:
  3705. * number of bytes printed to buffer
  3706. */
  3707. static ssize_t pmcraid_show_adapter_id(
  3708. struct device *dev,
  3709. struct device_attribute *attr,
  3710. char *buf
  3711. )
  3712. {
  3713. struct Scsi_Host *shost = class_to_shost(dev);
  3714. struct pmcraid_instance *pinstance =
  3715. (struct pmcraid_instance *)shost->hostdata;
  3716. u32 adapter_id = (pinstance->pdev->bus->number << 8) |
  3717. pinstance->pdev->devfn;
  3718. u32 aen_group = pmcraid_event_family.id;
  3719. return snprintf(buf, PAGE_SIZE,
  3720. "adapter id: %d\nminor: %d\naen group: %d\n",
  3721. adapter_id, MINOR(pinstance->cdev.dev), aen_group);
  3722. }
  3723. static struct device_attribute pmcraid_adapter_id_attr = {
  3724. .attr = {
  3725. .name = "adapter_id",
  3726. .mode = S_IRUGO | S_IWUSR,
  3727. },
  3728. .show = pmcraid_show_adapter_id,
  3729. };
  3730. static struct device_attribute *pmcraid_host_attrs[] = {
  3731. &pmcraid_log_level_attr,
  3732. &pmcraid_driver_version_attr,
  3733. &pmcraid_adapter_id_attr,
  3734. NULL,
  3735. };
  3736. /* host template structure for pmcraid driver */
  3737. static struct scsi_host_template pmcraid_host_template = {
  3738. .module = THIS_MODULE,
  3739. .name = PMCRAID_DRIVER_NAME,
  3740. .queuecommand = pmcraid_queuecommand,
  3741. .eh_abort_handler = pmcraid_eh_abort_handler,
  3742. .eh_bus_reset_handler = pmcraid_eh_bus_reset_handler,
  3743. .eh_target_reset_handler = pmcraid_eh_target_reset_handler,
  3744. .eh_device_reset_handler = pmcraid_eh_device_reset_handler,
  3745. .eh_host_reset_handler = pmcraid_eh_host_reset_handler,
  3746. .slave_alloc = pmcraid_slave_alloc,
  3747. .slave_configure = pmcraid_slave_configure,
  3748. .slave_destroy = pmcraid_slave_destroy,
  3749. .change_queue_depth = pmcraid_change_queue_depth,
  3750. .change_queue_type = pmcraid_change_queue_type,
  3751. .can_queue = PMCRAID_MAX_IO_CMD,
  3752. .this_id = -1,
  3753. .sg_tablesize = PMCRAID_MAX_IOADLS,
  3754. .max_sectors = PMCRAID_IOA_MAX_SECTORS,
  3755. .cmd_per_lun = PMCRAID_MAX_CMD_PER_LUN,
  3756. .use_clustering = ENABLE_CLUSTERING,
  3757. .shost_attrs = pmcraid_host_attrs,
  3758. .proc_name = PMCRAID_DRIVER_NAME
  3759. };
  3760. /*
  3761. * pmcraid_isr_msix - implements MSI-X interrupt handling routine
  3762. * @irq: interrupt vector number
  3763. * @dev_id: pointer hrrq_vector
  3764. *
  3765. * Return Value
  3766. * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
  3767. */
  3768. static irqreturn_t pmcraid_isr_msix(int irq, void *dev_id)
  3769. {
  3770. struct pmcraid_isr_param *hrrq_vector;
  3771. struct pmcraid_instance *pinstance;
  3772. unsigned long lock_flags;
  3773. u32 intrs_val;
  3774. int hrrq_id;
  3775. hrrq_vector = (struct pmcraid_isr_param *)dev_id;
  3776. hrrq_id = hrrq_vector->hrrq_id;
  3777. pinstance = hrrq_vector->drv_inst;
  3778. if (!hrrq_id) {
  3779. /* Read the interrupt */
  3780. intrs_val = pmcraid_read_interrupts(pinstance);
  3781. if (intrs_val &&
  3782. ((ioread32(pinstance->int_regs.host_ioa_interrupt_reg)
  3783. & DOORBELL_INTR_MSIX_CLR) == 0)) {
  3784. /* Any error interrupts including unit_check,
  3785. * initiate IOA reset.In case of unit check indicate
  3786. * to reset_sequence that IOA unit checked and prepare
  3787. * for a dump during reset sequence
  3788. */
  3789. if (intrs_val & PMCRAID_ERROR_INTERRUPTS) {
  3790. if (intrs_val & INTRS_IOA_UNIT_CHECK)
  3791. pinstance->ioa_unit_check = 1;
  3792. pmcraid_err("ISR: error interrupts: %x \
  3793. initiating reset\n", intrs_val);
  3794. spin_lock_irqsave(pinstance->host->host_lock,
  3795. lock_flags);
  3796. pmcraid_initiate_reset(pinstance);
  3797. spin_unlock_irqrestore(
  3798. pinstance->host->host_lock,
  3799. lock_flags);
  3800. }
  3801. /* If interrupt was as part of the ioa initialization,
  3802. * clear it. Delete the timer and wakeup the
  3803. * reset engine to proceed with reset sequence
  3804. */
  3805. if (intrs_val & INTRS_TRANSITION_TO_OPERATIONAL)
  3806. pmcraid_clr_trans_op(pinstance);
  3807. /* Clear the interrupt register by writing
  3808. * to host to ioa doorbell. Once done
  3809. * FW will clear the interrupt.
  3810. */
  3811. iowrite32(DOORBELL_INTR_MSIX_CLR,
  3812. pinstance->int_regs.host_ioa_interrupt_reg);
  3813. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  3814. }
  3815. }
  3816. tasklet_schedule(&(pinstance->isr_tasklet[hrrq_id]));
  3817. return IRQ_HANDLED;
  3818. }
  3819. /**
  3820. * pmcraid_isr - implements legacy interrupt handling routine
  3821. *
  3822. * @irq: interrupt vector number
  3823. * @dev_id: pointer hrrq_vector
  3824. *
  3825. * Return Value
  3826. * IRQ_HANDLED if interrupt is handled or IRQ_NONE if ignored
  3827. */
  3828. static irqreturn_t pmcraid_isr(int irq, void *dev_id)
  3829. {
  3830. struct pmcraid_isr_param *hrrq_vector;
  3831. struct pmcraid_instance *pinstance;
  3832. u32 intrs;
  3833. unsigned long lock_flags;
  3834. int hrrq_id = 0;
  3835. /* In case of legacy interrupt mode where interrupts are shared across
  3836. * isrs, it may be possible that the current interrupt is not from IOA
  3837. */
  3838. if (!dev_id) {
  3839. printk(KERN_INFO "%s(): NULL host pointer\n", __func__);
  3840. return IRQ_NONE;
  3841. }
  3842. hrrq_vector = (struct pmcraid_isr_param *)dev_id;
  3843. pinstance = hrrq_vector->drv_inst;
  3844. intrs = pmcraid_read_interrupts(pinstance);
  3845. if (unlikely((intrs & PMCRAID_PCI_INTERRUPTS) == 0))
  3846. return IRQ_NONE;
  3847. /* Any error interrupts including unit_check, initiate IOA reset.
  3848. * In case of unit check indicate to reset_sequence that IOA unit
  3849. * checked and prepare for a dump during reset sequence
  3850. */
  3851. if (intrs & PMCRAID_ERROR_INTERRUPTS) {
  3852. if (intrs & INTRS_IOA_UNIT_CHECK)
  3853. pinstance->ioa_unit_check = 1;
  3854. iowrite32(intrs,
  3855. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3856. pmcraid_err("ISR: error interrupts: %x initiating reset\n",
  3857. intrs);
  3858. intrs = ioread32(
  3859. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3860. spin_lock_irqsave(pinstance->host->host_lock, lock_flags);
  3861. pmcraid_initiate_reset(pinstance);
  3862. spin_unlock_irqrestore(pinstance->host->host_lock, lock_flags);
  3863. } else {
  3864. /* If interrupt was as part of the ioa initialization,
  3865. * clear. Delete the timer and wakeup the
  3866. * reset engine to proceed with reset sequence
  3867. */
  3868. if (intrs & INTRS_TRANSITION_TO_OPERATIONAL) {
  3869. pmcraid_clr_trans_op(pinstance);
  3870. } else {
  3871. iowrite32(intrs,
  3872. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3873. ioread32(
  3874. pinstance->int_regs.ioa_host_interrupt_clr_reg);
  3875. tasklet_schedule(
  3876. &(pinstance->isr_tasklet[hrrq_id]));
  3877. }
  3878. }
  3879. return IRQ_HANDLED;
  3880. }
  3881. /**
  3882. * pmcraid_worker_function - worker thread function
  3883. *
  3884. * @workp: pointer to struct work queue
  3885. *
  3886. * Return Value
  3887. * None
  3888. */
  3889. static void pmcraid_worker_function(struct work_struct *workp)
  3890. {
  3891. struct pmcraid_instance *pinstance;
  3892. struct pmcraid_resource_entry *res;
  3893. struct pmcraid_resource_entry *temp;
  3894. struct scsi_device *sdev;
  3895. unsigned long lock_flags;
  3896. unsigned long host_lock_flags;
  3897. u16 fw_version;
  3898. u8 bus, target, lun;
  3899. pinstance = container_of(workp, struct pmcraid_instance, worker_q);
  3900. /* add resources only after host is added into system */
  3901. if (!atomic_read(&pinstance->expose_resources))
  3902. return;
  3903. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  3904. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  3905. list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue) {
  3906. if (res->change_detected == RES_CHANGE_DEL && res->scsi_dev) {
  3907. sdev = res->scsi_dev;
  3908. /* host_lock must be held before calling
  3909. * scsi_device_get
  3910. */
  3911. spin_lock_irqsave(pinstance->host->host_lock,
  3912. host_lock_flags);
  3913. if (!scsi_device_get(sdev)) {
  3914. spin_unlock_irqrestore(
  3915. pinstance->host->host_lock,
  3916. host_lock_flags);
  3917. pmcraid_info("deleting %x from midlayer\n",
  3918. res->cfg_entry.resource_address);
  3919. list_move_tail(&res->queue,
  3920. &pinstance->free_res_q);
  3921. spin_unlock_irqrestore(
  3922. &pinstance->resource_lock,
  3923. lock_flags);
  3924. scsi_remove_device(sdev);
  3925. scsi_device_put(sdev);
  3926. spin_lock_irqsave(&pinstance->resource_lock,
  3927. lock_flags);
  3928. res->change_detected = 0;
  3929. } else {
  3930. spin_unlock_irqrestore(
  3931. pinstance->host->host_lock,
  3932. host_lock_flags);
  3933. }
  3934. }
  3935. }
  3936. list_for_each_entry(res, &pinstance->used_res_q, queue) {
  3937. if (res->change_detected == RES_CHANGE_ADD) {
  3938. if (!pmcraid_expose_resource(fw_version,
  3939. &res->cfg_entry))
  3940. continue;
  3941. if (RES_IS_VSET(res->cfg_entry)) {
  3942. bus = PMCRAID_VSET_BUS_ID;
  3943. if (fw_version <= PMCRAID_FW_VERSION_1)
  3944. target = res->cfg_entry.unique_flags1;
  3945. else
  3946. target = res->cfg_entry.array_id & 0xFF;
  3947. lun = PMCRAID_VSET_LUN_ID;
  3948. } else {
  3949. bus = PMCRAID_PHYS_BUS_ID;
  3950. target =
  3951. RES_TARGET(
  3952. res->cfg_entry.resource_address);
  3953. lun = RES_LUN(res->cfg_entry.resource_address);
  3954. }
  3955. res->change_detected = 0;
  3956. spin_unlock_irqrestore(&pinstance->resource_lock,
  3957. lock_flags);
  3958. scsi_add_device(pinstance->host, bus, target, lun);
  3959. spin_lock_irqsave(&pinstance->resource_lock,
  3960. lock_flags);
  3961. }
  3962. }
  3963. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  3964. }
  3965. /**
  3966. * pmcraid_tasklet_function - Tasklet function
  3967. *
  3968. * @instance: pointer to msix param structure
  3969. *
  3970. * Return Value
  3971. * None
  3972. */
  3973. static void pmcraid_tasklet_function(unsigned long instance)
  3974. {
  3975. struct pmcraid_isr_param *hrrq_vector;
  3976. struct pmcraid_instance *pinstance;
  3977. unsigned long hrrq_lock_flags;
  3978. unsigned long pending_lock_flags;
  3979. unsigned long host_lock_flags;
  3980. spinlock_t *lockp; /* hrrq buffer lock */
  3981. int id;
  3982. __le32 resp;
  3983. hrrq_vector = (struct pmcraid_isr_param *)instance;
  3984. pinstance = hrrq_vector->drv_inst;
  3985. id = hrrq_vector->hrrq_id;
  3986. lockp = &(pinstance->hrrq_lock[id]);
  3987. /* loop through each of the commands responded by IOA. Each HRRQ buf is
  3988. * protected by its own lock. Traversals must be done within this lock
  3989. * as there may be multiple tasklets running on multiple CPUs. Note
  3990. * that the lock is held just for picking up the response handle and
  3991. * manipulating hrrq_curr/toggle_bit values.
  3992. */
  3993. spin_lock_irqsave(lockp, hrrq_lock_flags);
  3994. resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
  3995. while ((resp & HRRQ_TOGGLE_BIT) ==
  3996. pinstance->host_toggle_bit[id]) {
  3997. int cmd_index = resp >> 2;
  3998. struct pmcraid_cmd *cmd = NULL;
  3999. if (pinstance->hrrq_curr[id] < pinstance->hrrq_end[id]) {
  4000. pinstance->hrrq_curr[id]++;
  4001. } else {
  4002. pinstance->hrrq_curr[id] = pinstance->hrrq_start[id];
  4003. pinstance->host_toggle_bit[id] ^= 1u;
  4004. }
  4005. if (cmd_index >= PMCRAID_MAX_CMD) {
  4006. /* In case of invalid response handle, log message */
  4007. pmcraid_err("Invalid response handle %d\n", cmd_index);
  4008. resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
  4009. continue;
  4010. }
  4011. cmd = pinstance->cmd_list[cmd_index];
  4012. spin_unlock_irqrestore(lockp, hrrq_lock_flags);
  4013. spin_lock_irqsave(&pinstance->pending_pool_lock,
  4014. pending_lock_flags);
  4015. list_del(&cmd->free_list);
  4016. spin_unlock_irqrestore(&pinstance->pending_pool_lock,
  4017. pending_lock_flags);
  4018. del_timer(&cmd->timer);
  4019. atomic_dec(&pinstance->outstanding_cmds);
  4020. if (cmd->cmd_done == pmcraid_ioa_reset) {
  4021. spin_lock_irqsave(pinstance->host->host_lock,
  4022. host_lock_flags);
  4023. cmd->cmd_done(cmd);
  4024. spin_unlock_irqrestore(pinstance->host->host_lock,
  4025. host_lock_flags);
  4026. } else if (cmd->cmd_done != NULL) {
  4027. cmd->cmd_done(cmd);
  4028. }
  4029. /* loop over until we are done with all responses */
  4030. spin_lock_irqsave(lockp, hrrq_lock_flags);
  4031. resp = le32_to_cpu(*(pinstance->hrrq_curr[id]));
  4032. }
  4033. spin_unlock_irqrestore(lockp, hrrq_lock_flags);
  4034. }
  4035. /**
  4036. * pmcraid_unregister_interrupt_handler - de-register interrupts handlers
  4037. * @pinstance: pointer to adapter instance structure
  4038. *
  4039. * This routine un-registers registered interrupt handler and
  4040. * also frees irqs/vectors.
  4041. *
  4042. * Retun Value
  4043. * None
  4044. */
  4045. static
  4046. void pmcraid_unregister_interrupt_handler(struct pmcraid_instance *pinstance)
  4047. {
  4048. int i;
  4049. for (i = 0; i < pinstance->num_hrrq; i++)
  4050. free_irq(pinstance->hrrq_vector[i].vector,
  4051. &(pinstance->hrrq_vector[i]));
  4052. if (pinstance->interrupt_mode) {
  4053. pci_disable_msix(pinstance->pdev);
  4054. pinstance->interrupt_mode = 0;
  4055. }
  4056. }
  4057. /**
  4058. * pmcraid_register_interrupt_handler - registers interrupt handler
  4059. * @pinstance: pointer to per-adapter instance structure
  4060. *
  4061. * Return Value
  4062. * 0 on success, non-zero error code otherwise.
  4063. */
  4064. static int
  4065. pmcraid_register_interrupt_handler(struct pmcraid_instance *pinstance)
  4066. {
  4067. int rc;
  4068. struct pci_dev *pdev = pinstance->pdev;
  4069. if ((pmcraid_enable_msix) &&
  4070. (pci_find_capability(pdev, PCI_CAP_ID_MSIX))) {
  4071. int num_hrrq = PMCRAID_NUM_MSIX_VECTORS;
  4072. struct msix_entry entries[PMCRAID_NUM_MSIX_VECTORS];
  4073. int i;
  4074. for (i = 0; i < PMCRAID_NUM_MSIX_VECTORS; i++)
  4075. entries[i].entry = i;
  4076. rc = pci_enable_msix(pdev, entries, num_hrrq);
  4077. if (rc < 0)
  4078. goto pmcraid_isr_legacy;
  4079. /* Check how many MSIX vectors are allocated and register
  4080. * msi-x handlers for each of them giving appropriate buffer
  4081. */
  4082. if (rc > 0) {
  4083. num_hrrq = rc;
  4084. if (pci_enable_msix(pdev, entries, num_hrrq))
  4085. goto pmcraid_isr_legacy;
  4086. }
  4087. for (i = 0; i < num_hrrq; i++) {
  4088. pinstance->hrrq_vector[i].hrrq_id = i;
  4089. pinstance->hrrq_vector[i].drv_inst = pinstance;
  4090. pinstance->hrrq_vector[i].vector = entries[i].vector;
  4091. rc = request_irq(pinstance->hrrq_vector[i].vector,
  4092. pmcraid_isr_msix, 0,
  4093. PMCRAID_DRIVER_NAME,
  4094. &(pinstance->hrrq_vector[i]));
  4095. if (rc) {
  4096. int j;
  4097. for (j = 0; j < i; j++)
  4098. free_irq(entries[j].vector,
  4099. &(pinstance->hrrq_vector[j]));
  4100. pci_disable_msix(pdev);
  4101. goto pmcraid_isr_legacy;
  4102. }
  4103. }
  4104. pinstance->num_hrrq = num_hrrq;
  4105. pinstance->interrupt_mode = 1;
  4106. iowrite32(DOORBELL_INTR_MODE_MSIX,
  4107. pinstance->int_regs.host_ioa_interrupt_reg);
  4108. ioread32(pinstance->int_regs.host_ioa_interrupt_reg);
  4109. goto pmcraid_isr_out;
  4110. }
  4111. pmcraid_isr_legacy:
  4112. /* If MSI-X registration failed fallback to legacy mode, where
  4113. * only one hrrq entry will be used
  4114. */
  4115. pinstance->hrrq_vector[0].hrrq_id = 0;
  4116. pinstance->hrrq_vector[0].drv_inst = pinstance;
  4117. pinstance->hrrq_vector[0].vector = pdev->irq;
  4118. pinstance->num_hrrq = 1;
  4119. rc = 0;
  4120. rc = request_irq(pdev->irq, pmcraid_isr, IRQF_SHARED,
  4121. PMCRAID_DRIVER_NAME, &pinstance->hrrq_vector[0]);
  4122. pmcraid_isr_out:
  4123. return rc;
  4124. }
  4125. /**
  4126. * pmcraid_release_cmd_blocks - release buufers allocated for command blocks
  4127. * @pinstance: per adapter instance structure pointer
  4128. * @max_index: number of buffer blocks to release
  4129. *
  4130. * Return Value
  4131. * None
  4132. */
  4133. static void
  4134. pmcraid_release_cmd_blocks(struct pmcraid_instance *pinstance, int max_index)
  4135. {
  4136. int i;
  4137. for (i = 0; i < max_index; i++) {
  4138. kmem_cache_free(pinstance->cmd_cachep, pinstance->cmd_list[i]);
  4139. pinstance->cmd_list[i] = NULL;
  4140. }
  4141. kmem_cache_destroy(pinstance->cmd_cachep);
  4142. pinstance->cmd_cachep = NULL;
  4143. }
  4144. /**
  4145. * pmcraid_release_control_blocks - releases buffers alloced for control blocks
  4146. * @pinstance: pointer to per adapter instance structure
  4147. * @max_index: number of buffers (from 0 onwards) to release
  4148. *
  4149. * This function assumes that the command blocks for which control blocks are
  4150. * linked are not released.
  4151. *
  4152. * Return Value
  4153. * None
  4154. */
  4155. static void
  4156. pmcraid_release_control_blocks(
  4157. struct pmcraid_instance *pinstance,
  4158. int max_index
  4159. )
  4160. {
  4161. int i;
  4162. if (pinstance->control_pool == NULL)
  4163. return;
  4164. for (i = 0; i < max_index; i++) {
  4165. pci_pool_free(pinstance->control_pool,
  4166. pinstance->cmd_list[i]->ioa_cb,
  4167. pinstance->cmd_list[i]->ioa_cb_bus_addr);
  4168. pinstance->cmd_list[i]->ioa_cb = NULL;
  4169. pinstance->cmd_list[i]->ioa_cb_bus_addr = 0;
  4170. }
  4171. pci_pool_destroy(pinstance->control_pool);
  4172. pinstance->control_pool = NULL;
  4173. }
  4174. /**
  4175. * pmcraid_allocate_cmd_blocks - allocate memory for cmd block structures
  4176. * @pinstance - pointer to per adapter instance structure
  4177. *
  4178. * Allocates memory for command blocks using kernel slab allocator.
  4179. *
  4180. * Return Value
  4181. * 0 in case of success; -ENOMEM in case of failure
  4182. */
  4183. static int __devinit
  4184. pmcraid_allocate_cmd_blocks(struct pmcraid_instance *pinstance)
  4185. {
  4186. int i;
  4187. sprintf(pinstance->cmd_pool_name, "pmcraid_cmd_pool_%d",
  4188. pinstance->host->unique_id);
  4189. pinstance->cmd_cachep = kmem_cache_create(
  4190. pinstance->cmd_pool_name,
  4191. sizeof(struct pmcraid_cmd), 0,
  4192. SLAB_HWCACHE_ALIGN, NULL);
  4193. if (!pinstance->cmd_cachep)
  4194. return -ENOMEM;
  4195. for (i = 0; i < PMCRAID_MAX_CMD; i++) {
  4196. pinstance->cmd_list[i] =
  4197. kmem_cache_alloc(pinstance->cmd_cachep, GFP_KERNEL);
  4198. if (!pinstance->cmd_list[i]) {
  4199. pmcraid_release_cmd_blocks(pinstance, i);
  4200. return -ENOMEM;
  4201. }
  4202. }
  4203. return 0;
  4204. }
  4205. /**
  4206. * pmcraid_allocate_control_blocks - allocates memory control blocks
  4207. * @pinstance : pointer to per adapter instance structure
  4208. *
  4209. * This function allocates PCI memory for DMAable buffers like IOARCB, IOADLs
  4210. * and IOASAs. This is called after command blocks are already allocated.
  4211. *
  4212. * Return Value
  4213. * 0 in case it can allocate all control blocks, otherwise -ENOMEM
  4214. */
  4215. static int __devinit
  4216. pmcraid_allocate_control_blocks(struct pmcraid_instance *pinstance)
  4217. {
  4218. int i;
  4219. sprintf(pinstance->ctl_pool_name, "pmcraid_control_pool_%d",
  4220. pinstance->host->unique_id);
  4221. pinstance->control_pool =
  4222. pci_pool_create(pinstance->ctl_pool_name,
  4223. pinstance->pdev,
  4224. sizeof(struct pmcraid_control_block),
  4225. PMCRAID_IOARCB_ALIGNMENT, 0);
  4226. if (!pinstance->control_pool)
  4227. return -ENOMEM;
  4228. for (i = 0; i < PMCRAID_MAX_CMD; i++) {
  4229. pinstance->cmd_list[i]->ioa_cb =
  4230. pci_pool_alloc(
  4231. pinstance->control_pool,
  4232. GFP_KERNEL,
  4233. &(pinstance->cmd_list[i]->ioa_cb_bus_addr));
  4234. if (!pinstance->cmd_list[i]->ioa_cb) {
  4235. pmcraid_release_control_blocks(pinstance, i);
  4236. return -ENOMEM;
  4237. }
  4238. memset(pinstance->cmd_list[i]->ioa_cb, 0,
  4239. sizeof(struct pmcraid_control_block));
  4240. }
  4241. return 0;
  4242. }
  4243. /**
  4244. * pmcraid_release_host_rrqs - release memory allocated for hrrq buffer(s)
  4245. * @pinstance: pointer to per adapter instance structure
  4246. * @maxindex: size of hrrq buffer pointer array
  4247. *
  4248. * Return Value
  4249. * None
  4250. */
  4251. static void
  4252. pmcraid_release_host_rrqs(struct pmcraid_instance *pinstance, int maxindex)
  4253. {
  4254. int i;
  4255. for (i = 0; i < maxindex; i++) {
  4256. pci_free_consistent(pinstance->pdev,
  4257. HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD,
  4258. pinstance->hrrq_start[i],
  4259. pinstance->hrrq_start_bus_addr[i]);
  4260. /* reset pointers and toggle bit to zeros */
  4261. pinstance->hrrq_start[i] = NULL;
  4262. pinstance->hrrq_start_bus_addr[i] = 0;
  4263. pinstance->host_toggle_bit[i] = 0;
  4264. }
  4265. }
  4266. /**
  4267. * pmcraid_allocate_host_rrqs - Allocate and initialize host RRQ buffers
  4268. * @pinstance: pointer to per adapter instance structure
  4269. *
  4270. * Return value
  4271. * 0 hrrq buffers are allocated, -ENOMEM otherwise.
  4272. */
  4273. static int __devinit
  4274. pmcraid_allocate_host_rrqs(struct pmcraid_instance *pinstance)
  4275. {
  4276. int i, buffer_size;
  4277. buffer_size = HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD;
  4278. for (i = 0; i < pinstance->num_hrrq; i++) {
  4279. pinstance->hrrq_start[i] =
  4280. pci_alloc_consistent(
  4281. pinstance->pdev,
  4282. buffer_size,
  4283. &(pinstance->hrrq_start_bus_addr[i]));
  4284. if (pinstance->hrrq_start[i] == 0) {
  4285. pmcraid_err("pci_alloc failed for hrrq vector : %d\n",
  4286. i);
  4287. pmcraid_release_host_rrqs(pinstance, i);
  4288. return -ENOMEM;
  4289. }
  4290. memset(pinstance->hrrq_start[i], 0, buffer_size);
  4291. pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
  4292. pinstance->hrrq_end[i] =
  4293. pinstance->hrrq_start[i] + PMCRAID_MAX_CMD - 1;
  4294. pinstance->host_toggle_bit[i] = 1;
  4295. spin_lock_init(&pinstance->hrrq_lock[i]);
  4296. }
  4297. return 0;
  4298. }
  4299. /**
  4300. * pmcraid_release_hcams - release HCAM buffers
  4301. *
  4302. * @pinstance: pointer to per adapter instance structure
  4303. *
  4304. * Return value
  4305. * none
  4306. */
  4307. static void pmcraid_release_hcams(struct pmcraid_instance *pinstance)
  4308. {
  4309. if (pinstance->ccn.msg != NULL) {
  4310. pci_free_consistent(pinstance->pdev,
  4311. PMCRAID_AEN_HDR_SIZE +
  4312. sizeof(struct pmcraid_hcam_ccn_ext),
  4313. pinstance->ccn.msg,
  4314. pinstance->ccn.baddr);
  4315. pinstance->ccn.msg = NULL;
  4316. pinstance->ccn.hcam = NULL;
  4317. pinstance->ccn.baddr = 0;
  4318. }
  4319. if (pinstance->ldn.msg != NULL) {
  4320. pci_free_consistent(pinstance->pdev,
  4321. PMCRAID_AEN_HDR_SIZE +
  4322. sizeof(struct pmcraid_hcam_ldn),
  4323. pinstance->ldn.msg,
  4324. pinstance->ldn.baddr);
  4325. pinstance->ldn.msg = NULL;
  4326. pinstance->ldn.hcam = NULL;
  4327. pinstance->ldn.baddr = 0;
  4328. }
  4329. }
  4330. /**
  4331. * pmcraid_allocate_hcams - allocates HCAM buffers
  4332. * @pinstance : pointer to per adapter instance structure
  4333. *
  4334. * Return Value:
  4335. * 0 in case of successful allocation, non-zero otherwise
  4336. */
  4337. static int pmcraid_allocate_hcams(struct pmcraid_instance *pinstance)
  4338. {
  4339. pinstance->ccn.msg = pci_alloc_consistent(
  4340. pinstance->pdev,
  4341. PMCRAID_AEN_HDR_SIZE +
  4342. sizeof(struct pmcraid_hcam_ccn_ext),
  4343. &(pinstance->ccn.baddr));
  4344. pinstance->ldn.msg = pci_alloc_consistent(
  4345. pinstance->pdev,
  4346. PMCRAID_AEN_HDR_SIZE +
  4347. sizeof(struct pmcraid_hcam_ldn),
  4348. &(pinstance->ldn.baddr));
  4349. if (pinstance->ldn.msg == NULL || pinstance->ccn.msg == NULL) {
  4350. pmcraid_release_hcams(pinstance);
  4351. } else {
  4352. pinstance->ccn.hcam =
  4353. (void *)pinstance->ccn.msg + PMCRAID_AEN_HDR_SIZE;
  4354. pinstance->ldn.hcam =
  4355. (void *)pinstance->ldn.msg + PMCRAID_AEN_HDR_SIZE;
  4356. atomic_set(&pinstance->ccn.ignore, 0);
  4357. atomic_set(&pinstance->ldn.ignore, 0);
  4358. }
  4359. return (pinstance->ldn.msg == NULL) ? -ENOMEM : 0;
  4360. }
  4361. /**
  4362. * pmcraid_release_config_buffers - release config.table buffers
  4363. * @pinstance: pointer to per adapter instance structure
  4364. *
  4365. * Return Value
  4366. * none
  4367. */
  4368. static void pmcraid_release_config_buffers(struct pmcraid_instance *pinstance)
  4369. {
  4370. if (pinstance->cfg_table != NULL &&
  4371. pinstance->cfg_table_bus_addr != 0) {
  4372. pci_free_consistent(pinstance->pdev,
  4373. sizeof(struct pmcraid_config_table),
  4374. pinstance->cfg_table,
  4375. pinstance->cfg_table_bus_addr);
  4376. pinstance->cfg_table = NULL;
  4377. pinstance->cfg_table_bus_addr = 0;
  4378. }
  4379. if (pinstance->res_entries != NULL) {
  4380. int i;
  4381. for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
  4382. list_del(&pinstance->res_entries[i].queue);
  4383. kfree(pinstance->res_entries);
  4384. pinstance->res_entries = NULL;
  4385. }
  4386. pmcraid_release_hcams(pinstance);
  4387. }
  4388. /**
  4389. * pmcraid_allocate_config_buffers - allocates DMAable memory for config table
  4390. * @pinstance : pointer to per adapter instance structure
  4391. *
  4392. * Return Value
  4393. * 0 for successful allocation, -ENOMEM for any failure
  4394. */
  4395. static int __devinit
  4396. pmcraid_allocate_config_buffers(struct pmcraid_instance *pinstance)
  4397. {
  4398. int i;
  4399. pinstance->res_entries =
  4400. kzalloc(sizeof(struct pmcraid_resource_entry) *
  4401. PMCRAID_MAX_RESOURCES, GFP_KERNEL);
  4402. if (NULL == pinstance->res_entries) {
  4403. pmcraid_err("failed to allocate memory for resource table\n");
  4404. return -ENOMEM;
  4405. }
  4406. for (i = 0; i < PMCRAID_MAX_RESOURCES; i++)
  4407. list_add_tail(&pinstance->res_entries[i].queue,
  4408. &pinstance->free_res_q);
  4409. pinstance->cfg_table =
  4410. pci_alloc_consistent(pinstance->pdev,
  4411. sizeof(struct pmcraid_config_table),
  4412. &pinstance->cfg_table_bus_addr);
  4413. if (NULL == pinstance->cfg_table) {
  4414. pmcraid_err("couldn't alloc DMA memory for config table\n");
  4415. pmcraid_release_config_buffers(pinstance);
  4416. return -ENOMEM;
  4417. }
  4418. if (pmcraid_allocate_hcams(pinstance)) {
  4419. pmcraid_err("could not alloc DMA memory for HCAMS\n");
  4420. pmcraid_release_config_buffers(pinstance);
  4421. return -ENOMEM;
  4422. }
  4423. return 0;
  4424. }
  4425. /**
  4426. * pmcraid_init_tasklets - registers tasklets for response handling
  4427. *
  4428. * @pinstance: pointer adapter instance structure
  4429. *
  4430. * Return value
  4431. * none
  4432. */
  4433. static void pmcraid_init_tasklets(struct pmcraid_instance *pinstance)
  4434. {
  4435. int i;
  4436. for (i = 0; i < pinstance->num_hrrq; i++)
  4437. tasklet_init(&pinstance->isr_tasklet[i],
  4438. pmcraid_tasklet_function,
  4439. (unsigned long)&pinstance->hrrq_vector[i]);
  4440. }
  4441. /**
  4442. * pmcraid_kill_tasklets - destroys tasklets registered for response handling
  4443. *
  4444. * @pinstance: pointer to adapter instance structure
  4445. *
  4446. * Return value
  4447. * none
  4448. */
  4449. static void pmcraid_kill_tasklets(struct pmcraid_instance *pinstance)
  4450. {
  4451. int i;
  4452. for (i = 0; i < pinstance->num_hrrq; i++)
  4453. tasklet_kill(&pinstance->isr_tasklet[i]);
  4454. }
  4455. /**
  4456. * pmcraid_release_buffers - release per-adapter buffers allocated
  4457. *
  4458. * @pinstance: pointer to adapter soft state
  4459. *
  4460. * Return Value
  4461. * none
  4462. */
  4463. static void pmcraid_release_buffers(struct pmcraid_instance *pinstance)
  4464. {
  4465. pmcraid_release_config_buffers(pinstance);
  4466. pmcraid_release_control_blocks(pinstance, PMCRAID_MAX_CMD);
  4467. pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
  4468. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4469. if (pinstance->inq_data != NULL) {
  4470. pci_free_consistent(pinstance->pdev,
  4471. sizeof(struct pmcraid_inquiry_data),
  4472. pinstance->inq_data,
  4473. pinstance->inq_data_baddr);
  4474. pinstance->inq_data = NULL;
  4475. pinstance->inq_data_baddr = 0;
  4476. }
  4477. if (pinstance->timestamp_data != NULL) {
  4478. pci_free_consistent(pinstance->pdev,
  4479. sizeof(struct pmcraid_timestamp_data),
  4480. pinstance->timestamp_data,
  4481. pinstance->timestamp_data_baddr);
  4482. pinstance->timestamp_data = NULL;
  4483. pinstance->timestamp_data_baddr = 0;
  4484. }
  4485. }
  4486. /**
  4487. * pmcraid_init_buffers - allocates memory and initializes various structures
  4488. * @pinstance: pointer to per adapter instance structure
  4489. *
  4490. * This routine pre-allocates memory based on the type of block as below:
  4491. * cmdblocks(PMCRAID_MAX_CMD): kernel memory using kernel's slab_allocator,
  4492. * IOARCBs(PMCRAID_MAX_CMD) : DMAable memory, using pci pool allocator
  4493. * config-table entries : DMAable memory using pci_alloc_consistent
  4494. * HostRRQs : DMAable memory, using pci_alloc_consistent
  4495. *
  4496. * Return Value
  4497. * 0 in case all of the blocks are allocated, -ENOMEM otherwise.
  4498. */
  4499. static int __devinit pmcraid_init_buffers(struct pmcraid_instance *pinstance)
  4500. {
  4501. int i;
  4502. if (pmcraid_allocate_host_rrqs(pinstance)) {
  4503. pmcraid_err("couldn't allocate memory for %d host rrqs\n",
  4504. pinstance->num_hrrq);
  4505. return -ENOMEM;
  4506. }
  4507. if (pmcraid_allocate_config_buffers(pinstance)) {
  4508. pmcraid_err("couldn't allocate memory for config buffers\n");
  4509. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4510. return -ENOMEM;
  4511. }
  4512. if (pmcraid_allocate_cmd_blocks(pinstance)) {
  4513. pmcraid_err("couldn't allocate memory for cmd blocks\n");
  4514. pmcraid_release_config_buffers(pinstance);
  4515. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4516. return -ENOMEM;
  4517. }
  4518. if (pmcraid_allocate_control_blocks(pinstance)) {
  4519. pmcraid_err("couldn't allocate memory control blocks\n");
  4520. pmcraid_release_config_buffers(pinstance);
  4521. pmcraid_release_cmd_blocks(pinstance, PMCRAID_MAX_CMD);
  4522. pmcraid_release_host_rrqs(pinstance, pinstance->num_hrrq);
  4523. return -ENOMEM;
  4524. }
  4525. /* allocate DMAable memory for page D0 INQUIRY buffer */
  4526. pinstance->inq_data = pci_alloc_consistent(
  4527. pinstance->pdev,
  4528. sizeof(struct pmcraid_inquiry_data),
  4529. &pinstance->inq_data_baddr);
  4530. if (pinstance->inq_data == NULL) {
  4531. pmcraid_err("couldn't allocate DMA memory for INQUIRY\n");
  4532. pmcraid_release_buffers(pinstance);
  4533. return -ENOMEM;
  4534. }
  4535. /* allocate DMAable memory for set timestamp data buffer */
  4536. pinstance->timestamp_data = pci_alloc_consistent(
  4537. pinstance->pdev,
  4538. sizeof(struct pmcraid_timestamp_data),
  4539. &pinstance->timestamp_data_baddr);
  4540. if (pinstance->timestamp_data == NULL) {
  4541. pmcraid_err("couldn't allocate DMA memory for \
  4542. set time_stamp \n");
  4543. pmcraid_release_buffers(pinstance);
  4544. return -ENOMEM;
  4545. }
  4546. /* Initialize all the command blocks and add them to free pool. No
  4547. * need to lock (free_pool_lock) as this is done in initialization
  4548. * itself
  4549. */
  4550. for (i = 0; i < PMCRAID_MAX_CMD; i++) {
  4551. struct pmcraid_cmd *cmdp = pinstance->cmd_list[i];
  4552. pmcraid_init_cmdblk(cmdp, i);
  4553. cmdp->drv_inst = pinstance;
  4554. list_add_tail(&cmdp->free_list, &pinstance->free_cmd_pool);
  4555. }
  4556. return 0;
  4557. }
  4558. /**
  4559. * pmcraid_reinit_buffers - resets various buffer pointers
  4560. * @pinstance: pointer to adapter instance
  4561. * Return value
  4562. * none
  4563. */
  4564. static void pmcraid_reinit_buffers(struct pmcraid_instance *pinstance)
  4565. {
  4566. int i;
  4567. int buffer_size = HRRQ_ENTRY_SIZE * PMCRAID_MAX_CMD;
  4568. for (i = 0; i < pinstance->num_hrrq; i++) {
  4569. memset(pinstance->hrrq_start[i], 0, buffer_size);
  4570. pinstance->hrrq_curr[i] = pinstance->hrrq_start[i];
  4571. pinstance->hrrq_end[i] =
  4572. pinstance->hrrq_start[i] + PMCRAID_MAX_CMD - 1;
  4573. pinstance->host_toggle_bit[i] = 1;
  4574. }
  4575. }
  4576. /**
  4577. * pmcraid_init_instance - initialize per instance data structure
  4578. * @pdev: pointer to pci device structure
  4579. * @host: pointer to Scsi_Host structure
  4580. * @mapped_pci_addr: memory mapped IOA configuration registers
  4581. *
  4582. * Return Value
  4583. * 0 on success, non-zero in case of any failure
  4584. */
  4585. static int __devinit pmcraid_init_instance(
  4586. struct pci_dev *pdev,
  4587. struct Scsi_Host *host,
  4588. void __iomem *mapped_pci_addr
  4589. )
  4590. {
  4591. struct pmcraid_instance *pinstance =
  4592. (struct pmcraid_instance *)host->hostdata;
  4593. pinstance->host = host;
  4594. pinstance->pdev = pdev;
  4595. /* Initialize register addresses */
  4596. pinstance->mapped_dma_addr = mapped_pci_addr;
  4597. /* Initialize chip-specific details */
  4598. {
  4599. struct pmcraid_chip_details *chip_cfg = pinstance->chip_cfg;
  4600. struct pmcraid_interrupts *pint_regs = &pinstance->int_regs;
  4601. pinstance->ioarrin = mapped_pci_addr + chip_cfg->ioarrin;
  4602. pint_regs->ioa_host_interrupt_reg =
  4603. mapped_pci_addr + chip_cfg->ioa_host_intr;
  4604. pint_regs->ioa_host_interrupt_clr_reg =
  4605. mapped_pci_addr + chip_cfg->ioa_host_intr_clr;
  4606. pint_regs->ioa_host_msix_interrupt_reg =
  4607. mapped_pci_addr + chip_cfg->ioa_host_msix_intr;
  4608. pint_regs->host_ioa_interrupt_reg =
  4609. mapped_pci_addr + chip_cfg->host_ioa_intr;
  4610. pint_regs->host_ioa_interrupt_clr_reg =
  4611. mapped_pci_addr + chip_cfg->host_ioa_intr_clr;
  4612. /* Current version of firmware exposes interrupt mask set
  4613. * and mask clr registers through memory mapped bar0.
  4614. */
  4615. pinstance->mailbox = mapped_pci_addr + chip_cfg->mailbox;
  4616. pinstance->ioa_status = mapped_pci_addr + chip_cfg->ioastatus;
  4617. pint_regs->ioa_host_interrupt_mask_reg =
  4618. mapped_pci_addr + chip_cfg->ioa_host_mask;
  4619. pint_regs->ioa_host_interrupt_mask_clr_reg =
  4620. mapped_pci_addr + chip_cfg->ioa_host_mask_clr;
  4621. pint_regs->global_interrupt_mask_reg =
  4622. mapped_pci_addr + chip_cfg->global_intr_mask;
  4623. };
  4624. pinstance->ioa_reset_attempts = 0;
  4625. init_waitqueue_head(&pinstance->reset_wait_q);
  4626. atomic_set(&pinstance->outstanding_cmds, 0);
  4627. atomic_set(&pinstance->last_message_id, 0);
  4628. atomic_set(&pinstance->expose_resources, 0);
  4629. INIT_LIST_HEAD(&pinstance->free_res_q);
  4630. INIT_LIST_HEAD(&pinstance->used_res_q);
  4631. INIT_LIST_HEAD(&pinstance->free_cmd_pool);
  4632. INIT_LIST_HEAD(&pinstance->pending_cmd_pool);
  4633. spin_lock_init(&pinstance->free_pool_lock);
  4634. spin_lock_init(&pinstance->pending_pool_lock);
  4635. spin_lock_init(&pinstance->resource_lock);
  4636. mutex_init(&pinstance->aen_queue_lock);
  4637. /* Work-queue (Shared) for deferred processing error handling */
  4638. INIT_WORK(&pinstance->worker_q, pmcraid_worker_function);
  4639. /* Initialize the default log_level */
  4640. pinstance->current_log_level = pmcraid_log_level;
  4641. /* Setup variables required for reset engine */
  4642. pinstance->ioa_state = IOA_STATE_UNKNOWN;
  4643. pinstance->reset_cmd = NULL;
  4644. return 0;
  4645. }
  4646. /**
  4647. * pmcraid_shutdown - shutdown adapter controller.
  4648. * @pdev: pci device struct
  4649. *
  4650. * Issues an adapter shutdown to the card waits for its completion
  4651. *
  4652. * Return value
  4653. * none
  4654. */
  4655. static void pmcraid_shutdown(struct pci_dev *pdev)
  4656. {
  4657. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4658. pmcraid_reset_bringdown(pinstance);
  4659. }
  4660. /**
  4661. * pmcraid_get_minor - returns unused minor number from minor number bitmap
  4662. */
  4663. static unsigned short pmcraid_get_minor(void)
  4664. {
  4665. int minor;
  4666. minor = find_first_zero_bit(pmcraid_minor, sizeof(pmcraid_minor));
  4667. __set_bit(minor, pmcraid_minor);
  4668. return minor;
  4669. }
  4670. /**
  4671. * pmcraid_release_minor - releases given minor back to minor number bitmap
  4672. */
  4673. static void pmcraid_release_minor(unsigned short minor)
  4674. {
  4675. __clear_bit(minor, pmcraid_minor);
  4676. }
  4677. /**
  4678. * pmcraid_setup_chrdev - allocates a minor number and registers a char device
  4679. *
  4680. * @pinstance: pointer to adapter instance for which to register device
  4681. *
  4682. * Return value
  4683. * 0 in case of success, otherwise non-zero
  4684. */
  4685. static int pmcraid_setup_chrdev(struct pmcraid_instance *pinstance)
  4686. {
  4687. int minor;
  4688. int error;
  4689. minor = pmcraid_get_minor();
  4690. cdev_init(&pinstance->cdev, &pmcraid_fops);
  4691. pinstance->cdev.owner = THIS_MODULE;
  4692. error = cdev_add(&pinstance->cdev, MKDEV(pmcraid_major, minor), 1);
  4693. if (error)
  4694. pmcraid_release_minor(minor);
  4695. else
  4696. device_create(pmcraid_class, NULL, MKDEV(pmcraid_major, minor),
  4697. NULL, "%s%u", PMCRAID_DEVFILE, minor);
  4698. return error;
  4699. }
  4700. /**
  4701. * pmcraid_release_chrdev - unregisters per-adapter management interface
  4702. *
  4703. * @pinstance: pointer to adapter instance structure
  4704. *
  4705. * Return value
  4706. * none
  4707. */
  4708. static void pmcraid_release_chrdev(struct pmcraid_instance *pinstance)
  4709. {
  4710. pmcraid_release_minor(MINOR(pinstance->cdev.dev));
  4711. device_destroy(pmcraid_class,
  4712. MKDEV(pmcraid_major, MINOR(pinstance->cdev.dev)));
  4713. cdev_del(&pinstance->cdev);
  4714. }
  4715. /**
  4716. * pmcraid_remove - IOA hot plug remove entry point
  4717. * @pdev: pci device struct
  4718. *
  4719. * Return value
  4720. * none
  4721. */
  4722. static void __devexit pmcraid_remove(struct pci_dev *pdev)
  4723. {
  4724. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4725. /* remove the management interface (/dev file) for this device */
  4726. pmcraid_release_chrdev(pinstance);
  4727. /* remove host template from scsi midlayer */
  4728. scsi_remove_host(pinstance->host);
  4729. /* block requests from mid-layer */
  4730. scsi_block_requests(pinstance->host);
  4731. /* initiate shutdown adapter */
  4732. pmcraid_shutdown(pdev);
  4733. pmcraid_disable_interrupts(pinstance, ~0);
  4734. flush_work_sync(&pinstance->worker_q);
  4735. pmcraid_kill_tasklets(pinstance);
  4736. pmcraid_unregister_interrupt_handler(pinstance);
  4737. pmcraid_release_buffers(pinstance);
  4738. iounmap(pinstance->mapped_dma_addr);
  4739. pci_release_regions(pdev);
  4740. scsi_host_put(pinstance->host);
  4741. pci_disable_device(pdev);
  4742. return;
  4743. }
  4744. #ifdef CONFIG_PM
  4745. /**
  4746. * pmcraid_suspend - driver suspend entry point for power management
  4747. * @pdev: PCI device structure
  4748. * @state: PCI power state to suspend routine
  4749. *
  4750. * Return Value - 0 always
  4751. */
  4752. static int pmcraid_suspend(struct pci_dev *pdev, pm_message_t state)
  4753. {
  4754. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4755. pmcraid_shutdown(pdev);
  4756. pmcraid_disable_interrupts(pinstance, ~0);
  4757. pmcraid_kill_tasklets(pinstance);
  4758. pci_set_drvdata(pinstance->pdev, pinstance);
  4759. pmcraid_unregister_interrupt_handler(pinstance);
  4760. pci_save_state(pdev);
  4761. pci_disable_device(pdev);
  4762. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  4763. return 0;
  4764. }
  4765. /**
  4766. * pmcraid_resume - driver resume entry point PCI power management
  4767. * @pdev: PCI device structure
  4768. *
  4769. * Return Value - 0 in case of success. Error code in case of any failure
  4770. */
  4771. static int pmcraid_resume(struct pci_dev *pdev)
  4772. {
  4773. struct pmcraid_instance *pinstance = pci_get_drvdata(pdev);
  4774. struct Scsi_Host *host = pinstance->host;
  4775. int rc;
  4776. pci_set_power_state(pdev, PCI_D0);
  4777. pci_enable_wake(pdev, PCI_D0, 0);
  4778. pci_restore_state(pdev);
  4779. rc = pci_enable_device(pdev);
  4780. if (rc) {
  4781. dev_err(&pdev->dev, "resume: Enable device failed\n");
  4782. return rc;
  4783. }
  4784. pci_set_master(pdev);
  4785. if ((sizeof(dma_addr_t) == 4) ||
  4786. pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
  4787. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  4788. if (rc == 0)
  4789. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  4790. if (rc != 0) {
  4791. dev_err(&pdev->dev, "resume: Failed to set PCI DMA mask\n");
  4792. goto disable_device;
  4793. }
  4794. pmcraid_disable_interrupts(pinstance, ~0);
  4795. atomic_set(&pinstance->outstanding_cmds, 0);
  4796. rc = pmcraid_register_interrupt_handler(pinstance);
  4797. if (rc) {
  4798. dev_err(&pdev->dev,
  4799. "resume: couldn't register interrupt handlers\n");
  4800. rc = -ENODEV;
  4801. goto release_host;
  4802. }
  4803. pmcraid_init_tasklets(pinstance);
  4804. pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
  4805. /* Start with hard reset sequence which brings up IOA to operational
  4806. * state as well as completes the reset sequence.
  4807. */
  4808. pinstance->ioa_hard_reset = 1;
  4809. /* Start IOA firmware initialization and bring card to Operational
  4810. * state.
  4811. */
  4812. if (pmcraid_reset_bringup(pinstance)) {
  4813. dev_err(&pdev->dev, "couldn't initialize IOA\n");
  4814. rc = -ENODEV;
  4815. goto release_tasklets;
  4816. }
  4817. return 0;
  4818. release_tasklets:
  4819. pmcraid_disable_interrupts(pinstance, ~0);
  4820. pmcraid_kill_tasklets(pinstance);
  4821. pmcraid_unregister_interrupt_handler(pinstance);
  4822. release_host:
  4823. scsi_host_put(host);
  4824. disable_device:
  4825. pci_disable_device(pdev);
  4826. return rc;
  4827. }
  4828. #else
  4829. #define pmcraid_suspend NULL
  4830. #define pmcraid_resume NULL
  4831. #endif /* CONFIG_PM */
  4832. /**
  4833. * pmcraid_complete_ioa_reset - Called by either timer or tasklet during
  4834. * completion of the ioa reset
  4835. * @cmd: pointer to reset command block
  4836. */
  4837. static void pmcraid_complete_ioa_reset(struct pmcraid_cmd *cmd)
  4838. {
  4839. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4840. unsigned long flags;
  4841. spin_lock_irqsave(pinstance->host->host_lock, flags);
  4842. pmcraid_ioa_reset(cmd);
  4843. spin_unlock_irqrestore(pinstance->host->host_lock, flags);
  4844. scsi_unblock_requests(pinstance->host);
  4845. schedule_work(&pinstance->worker_q);
  4846. }
  4847. /**
  4848. * pmcraid_set_supported_devs - sends SET SUPPORTED DEVICES to IOAFP
  4849. *
  4850. * @cmd: pointer to pmcraid_cmd structure
  4851. *
  4852. * Return Value
  4853. * 0 for success or non-zero for failure cases
  4854. */
  4855. static void pmcraid_set_supported_devs(struct pmcraid_cmd *cmd)
  4856. {
  4857. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  4858. void (*cmd_done) (struct pmcraid_cmd *) = pmcraid_complete_ioa_reset;
  4859. pmcraid_reinit_cmdblk(cmd);
  4860. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  4861. ioarcb->request_type = REQ_TYPE_IOACMD;
  4862. ioarcb->cdb[0] = PMCRAID_SET_SUPPORTED_DEVICES;
  4863. ioarcb->cdb[1] = ALL_DEVICES_SUPPORTED;
  4864. /* If this was called as part of resource table reinitialization due to
  4865. * lost CCN, it is enough to return the command block back to free pool
  4866. * as part of set_supported_devs completion function.
  4867. */
  4868. if (cmd->drv_inst->reinit_cfg_table) {
  4869. cmd->drv_inst->reinit_cfg_table = 0;
  4870. cmd->release = 1;
  4871. cmd_done = pmcraid_reinit_cfgtable_done;
  4872. }
  4873. /* we will be done with the reset sequence after set supported devices,
  4874. * setup the done function to return the command block back to free
  4875. * pool
  4876. */
  4877. pmcraid_send_cmd(cmd,
  4878. cmd_done,
  4879. PMCRAID_SET_SUP_DEV_TIMEOUT,
  4880. pmcraid_timeout_handler);
  4881. return;
  4882. }
  4883. /**
  4884. * pmcraid_set_timestamp - set the timestamp to IOAFP
  4885. *
  4886. * @cmd: pointer to pmcraid_cmd structure
  4887. *
  4888. * Return Value
  4889. * 0 for success or non-zero for failure cases
  4890. */
  4891. static void pmcraid_set_timestamp(struct pmcraid_cmd *cmd)
  4892. {
  4893. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4894. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  4895. __be32 time_stamp_len = cpu_to_be32(PMCRAID_TIMESTAMP_LEN);
  4896. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  4897. struct timeval tv;
  4898. __le64 timestamp;
  4899. do_gettimeofday(&tv);
  4900. timestamp = tv.tv_sec * 1000;
  4901. pinstance->timestamp_data->timestamp[0] = (__u8)(timestamp);
  4902. pinstance->timestamp_data->timestamp[1] = (__u8)((timestamp) >> 8);
  4903. pinstance->timestamp_data->timestamp[2] = (__u8)((timestamp) >> 16);
  4904. pinstance->timestamp_data->timestamp[3] = (__u8)((timestamp) >> 24);
  4905. pinstance->timestamp_data->timestamp[4] = (__u8)((timestamp) >> 32);
  4906. pinstance->timestamp_data->timestamp[5] = (__u8)((timestamp) >> 40);
  4907. pmcraid_reinit_cmdblk(cmd);
  4908. ioarcb->request_type = REQ_TYPE_SCSI;
  4909. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  4910. ioarcb->cdb[0] = PMCRAID_SCSI_SET_TIMESTAMP;
  4911. ioarcb->cdb[1] = PMCRAID_SCSI_SERVICE_ACTION;
  4912. memcpy(&(ioarcb->cdb[6]), &time_stamp_len, sizeof(time_stamp_len));
  4913. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  4914. offsetof(struct pmcraid_ioarcb,
  4915. add_data.u.ioadl[0]));
  4916. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  4917. ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
  4918. ioarcb->request_flags0 |= NO_LINK_DESCS;
  4919. ioarcb->request_flags0 |= TRANSFER_DIR_WRITE;
  4920. ioarcb->data_transfer_length =
  4921. cpu_to_le32(sizeof(struct pmcraid_timestamp_data));
  4922. ioadl = &(ioarcb->add_data.u.ioadl[0]);
  4923. ioadl->flags = IOADL_FLAGS_LAST_DESC;
  4924. ioadl->address = cpu_to_le64(pinstance->timestamp_data_baddr);
  4925. ioadl->data_len = cpu_to_le32(sizeof(struct pmcraid_timestamp_data));
  4926. if (!pinstance->timestamp_error) {
  4927. pinstance->timestamp_error = 0;
  4928. pmcraid_send_cmd(cmd, pmcraid_set_supported_devs,
  4929. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  4930. } else {
  4931. pmcraid_send_cmd(cmd, pmcraid_return_cmd,
  4932. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  4933. return;
  4934. }
  4935. }
  4936. /**
  4937. * pmcraid_init_res_table - Initialize the resource table
  4938. * @cmd: pointer to pmcraid command struct
  4939. *
  4940. * This function looks through the existing resource table, comparing
  4941. * it with the config table. This function will take care of old/new
  4942. * devices and schedule adding/removing them from the mid-layer
  4943. * as appropriate.
  4944. *
  4945. * Return value
  4946. * None
  4947. */
  4948. static void pmcraid_init_res_table(struct pmcraid_cmd *cmd)
  4949. {
  4950. struct pmcraid_instance *pinstance = cmd->drv_inst;
  4951. struct pmcraid_resource_entry *res, *temp;
  4952. struct pmcraid_config_table_entry *cfgte;
  4953. unsigned long lock_flags;
  4954. int found, rc, i;
  4955. u16 fw_version;
  4956. LIST_HEAD(old_res);
  4957. if (pinstance->cfg_table->flags & MICROCODE_UPDATE_REQUIRED)
  4958. pmcraid_err("IOA requires microcode download\n");
  4959. fw_version = be16_to_cpu(pinstance->inq_data->fw_version);
  4960. /* resource list is protected by pinstance->resource_lock.
  4961. * init_res_table can be called from probe (user-thread) or runtime
  4962. * reset (timer/tasklet)
  4963. */
  4964. spin_lock_irqsave(&pinstance->resource_lock, lock_flags);
  4965. list_for_each_entry_safe(res, temp, &pinstance->used_res_q, queue)
  4966. list_move_tail(&res->queue, &old_res);
  4967. for (i = 0; i < pinstance->cfg_table->num_entries; i++) {
  4968. if (be16_to_cpu(pinstance->inq_data->fw_version) <=
  4969. PMCRAID_FW_VERSION_1)
  4970. cfgte = &pinstance->cfg_table->entries[i];
  4971. else
  4972. cfgte = (struct pmcraid_config_table_entry *)
  4973. &pinstance->cfg_table->entries_ext[i];
  4974. if (!pmcraid_expose_resource(fw_version, cfgte))
  4975. continue;
  4976. found = 0;
  4977. /* If this entry was already detected and initialized */
  4978. list_for_each_entry_safe(res, temp, &old_res, queue) {
  4979. rc = memcmp(&res->cfg_entry.resource_address,
  4980. &cfgte->resource_address,
  4981. sizeof(cfgte->resource_address));
  4982. if (!rc) {
  4983. list_move_tail(&res->queue,
  4984. &pinstance->used_res_q);
  4985. found = 1;
  4986. break;
  4987. }
  4988. }
  4989. /* If this is new entry, initialize it and add it the queue */
  4990. if (!found) {
  4991. if (list_empty(&pinstance->free_res_q)) {
  4992. pmcraid_err("Too many devices attached\n");
  4993. break;
  4994. }
  4995. found = 1;
  4996. res = list_entry(pinstance->free_res_q.next,
  4997. struct pmcraid_resource_entry, queue);
  4998. res->scsi_dev = NULL;
  4999. res->change_detected = RES_CHANGE_ADD;
  5000. res->reset_progress = 0;
  5001. list_move_tail(&res->queue, &pinstance->used_res_q);
  5002. }
  5003. /* copy new configuration table entry details into driver
  5004. * maintained resource entry
  5005. */
  5006. if (found) {
  5007. memcpy(&res->cfg_entry, cfgte,
  5008. pinstance->config_table_entry_size);
  5009. pmcraid_info("New res type:%x, vset:%x, addr:%x:\n",
  5010. res->cfg_entry.resource_type,
  5011. (fw_version <= PMCRAID_FW_VERSION_1 ?
  5012. res->cfg_entry.unique_flags1 :
  5013. res->cfg_entry.array_id & 0xFF),
  5014. le32_to_cpu(res->cfg_entry.resource_address));
  5015. }
  5016. }
  5017. /* Detect any deleted entries, mark them for deletion from mid-layer */
  5018. list_for_each_entry_safe(res, temp, &old_res, queue) {
  5019. if (res->scsi_dev) {
  5020. res->change_detected = RES_CHANGE_DEL;
  5021. res->cfg_entry.resource_handle =
  5022. PMCRAID_INVALID_RES_HANDLE;
  5023. list_move_tail(&res->queue, &pinstance->used_res_q);
  5024. } else {
  5025. list_move_tail(&res->queue, &pinstance->free_res_q);
  5026. }
  5027. }
  5028. /* release the resource list lock */
  5029. spin_unlock_irqrestore(&pinstance->resource_lock, lock_flags);
  5030. pmcraid_set_timestamp(cmd);
  5031. }
  5032. /**
  5033. * pmcraid_querycfg - Send a Query IOA Config to the adapter.
  5034. * @cmd: pointer pmcraid_cmd struct
  5035. *
  5036. * This function sends a Query IOA Configuration command to the adapter to
  5037. * retrieve the IOA configuration table.
  5038. *
  5039. * Return value:
  5040. * none
  5041. */
  5042. static void pmcraid_querycfg(struct pmcraid_cmd *cmd)
  5043. {
  5044. struct pmcraid_ioarcb *ioarcb = &cmd->ioa_cb->ioarcb;
  5045. struct pmcraid_ioadl_desc *ioadl = ioarcb->add_data.u.ioadl;
  5046. struct pmcraid_instance *pinstance = cmd->drv_inst;
  5047. int cfg_table_size = cpu_to_be32(sizeof(struct pmcraid_config_table));
  5048. if (be16_to_cpu(pinstance->inq_data->fw_version) <=
  5049. PMCRAID_FW_VERSION_1)
  5050. pinstance->config_table_entry_size =
  5051. sizeof(struct pmcraid_config_table_entry);
  5052. else
  5053. pinstance->config_table_entry_size =
  5054. sizeof(struct pmcraid_config_table_entry_ext);
  5055. ioarcb->request_type = REQ_TYPE_IOACMD;
  5056. ioarcb->resource_handle = cpu_to_le32(PMCRAID_IOA_RES_HANDLE);
  5057. ioarcb->cdb[0] = PMCRAID_QUERY_IOA_CONFIG;
  5058. /* firmware requires 4-byte length field, specified in B.E format */
  5059. memcpy(&(ioarcb->cdb[10]), &cfg_table_size, sizeof(cfg_table_size));
  5060. /* Since entire config table can be described by single IOADL, it can
  5061. * be part of IOARCB itself
  5062. */
  5063. ioarcb->ioadl_bus_addr = cpu_to_le64((cmd->ioa_cb_bus_addr) +
  5064. offsetof(struct pmcraid_ioarcb,
  5065. add_data.u.ioadl[0]));
  5066. ioarcb->ioadl_length = cpu_to_le32(sizeof(struct pmcraid_ioadl_desc));
  5067. ioarcb->ioarcb_bus_addr &= ~(0x1FULL);
  5068. ioarcb->request_flags0 |= NO_LINK_DESCS;
  5069. ioarcb->data_transfer_length =
  5070. cpu_to_le32(sizeof(struct pmcraid_config_table));
  5071. ioadl = &(ioarcb->add_data.u.ioadl[0]);
  5072. ioadl->flags = IOADL_FLAGS_LAST_DESC;
  5073. ioadl->address = cpu_to_le64(pinstance->cfg_table_bus_addr);
  5074. ioadl->data_len = cpu_to_le32(sizeof(struct pmcraid_config_table));
  5075. pmcraid_send_cmd(cmd, pmcraid_init_res_table,
  5076. PMCRAID_INTERNAL_TIMEOUT, pmcraid_timeout_handler);
  5077. }
  5078. /**
  5079. * pmcraid_probe - PCI probe entry pointer for PMC MaxRAID controller driver
  5080. * @pdev: pointer to pci device structure
  5081. * @dev_id: pointer to device ids structure
  5082. *
  5083. * Return Value
  5084. * returns 0 if the device is claimed and successfully configured.
  5085. * returns non-zero error code in case of any failure
  5086. */
  5087. static int __devinit pmcraid_probe(
  5088. struct pci_dev *pdev,
  5089. const struct pci_device_id *dev_id
  5090. )
  5091. {
  5092. struct pmcraid_instance *pinstance;
  5093. struct Scsi_Host *host;
  5094. void __iomem *mapped_pci_addr;
  5095. int rc = PCIBIOS_SUCCESSFUL;
  5096. if (atomic_read(&pmcraid_adapter_count) >= PMCRAID_MAX_ADAPTERS) {
  5097. pmcraid_err
  5098. ("maximum number(%d) of supported adapters reached\n",
  5099. atomic_read(&pmcraid_adapter_count));
  5100. return -ENOMEM;
  5101. }
  5102. atomic_inc(&pmcraid_adapter_count);
  5103. rc = pci_enable_device(pdev);
  5104. if (rc) {
  5105. dev_err(&pdev->dev, "Cannot enable adapter\n");
  5106. atomic_dec(&pmcraid_adapter_count);
  5107. return rc;
  5108. }
  5109. dev_info(&pdev->dev,
  5110. "Found new IOA(%x:%x), Total IOA count: %d\n",
  5111. pdev->vendor, pdev->device,
  5112. atomic_read(&pmcraid_adapter_count));
  5113. rc = pci_request_regions(pdev, PMCRAID_DRIVER_NAME);
  5114. if (rc < 0) {
  5115. dev_err(&pdev->dev,
  5116. "Couldn't register memory range of registers\n");
  5117. goto out_disable_device;
  5118. }
  5119. mapped_pci_addr = pci_iomap(pdev, 0, 0);
  5120. if (!mapped_pci_addr) {
  5121. dev_err(&pdev->dev, "Couldn't map PCI registers memory\n");
  5122. rc = -ENOMEM;
  5123. goto out_release_regions;
  5124. }
  5125. pci_set_master(pdev);
  5126. /* Firmware requires the system bus address of IOARCB to be within
  5127. * 32-bit addressable range though it has 64-bit IOARRIN register.
  5128. * However, firmware supports 64-bit streaming DMA buffers, whereas
  5129. * coherent buffers are to be 32-bit. Since pci_alloc_consistent always
  5130. * returns memory within 4GB (if not, change this logic), coherent
  5131. * buffers are within firmware acceptable address ranges.
  5132. */
  5133. if ((sizeof(dma_addr_t) == 4) ||
  5134. pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
  5135. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  5136. /* firmware expects 32-bit DMA addresses for IOARRIN register; set 32
  5137. * bit mask for pci_alloc_consistent to return addresses within 4GB
  5138. */
  5139. if (rc == 0)
  5140. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  5141. if (rc != 0) {
  5142. dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
  5143. goto cleanup_nomem;
  5144. }
  5145. host = scsi_host_alloc(&pmcraid_host_template,
  5146. sizeof(struct pmcraid_instance));
  5147. if (!host) {
  5148. dev_err(&pdev->dev, "scsi_host_alloc failed!\n");
  5149. rc = -ENOMEM;
  5150. goto cleanup_nomem;
  5151. }
  5152. host->max_id = PMCRAID_MAX_NUM_TARGETS_PER_BUS;
  5153. host->max_lun = PMCRAID_MAX_NUM_LUNS_PER_TARGET;
  5154. host->unique_id = host->host_no;
  5155. host->max_channel = PMCRAID_MAX_BUS_TO_SCAN;
  5156. host->max_cmd_len = PMCRAID_MAX_CDB_LEN;
  5157. /* zero out entire instance structure */
  5158. pinstance = (struct pmcraid_instance *)host->hostdata;
  5159. memset(pinstance, 0, sizeof(*pinstance));
  5160. pinstance->chip_cfg =
  5161. (struct pmcraid_chip_details *)(dev_id->driver_data);
  5162. rc = pmcraid_init_instance(pdev, host, mapped_pci_addr);
  5163. if (rc < 0) {
  5164. dev_err(&pdev->dev, "failed to initialize adapter instance\n");
  5165. goto out_scsi_host_put;
  5166. }
  5167. pci_set_drvdata(pdev, pinstance);
  5168. /* Save PCI config-space for use following the reset */
  5169. rc = pci_save_state(pinstance->pdev);
  5170. if (rc != 0) {
  5171. dev_err(&pdev->dev, "Failed to save PCI config space\n");
  5172. goto out_scsi_host_put;
  5173. }
  5174. pmcraid_disable_interrupts(pinstance, ~0);
  5175. rc = pmcraid_register_interrupt_handler(pinstance);
  5176. if (rc) {
  5177. dev_err(&pdev->dev, "couldn't register interrupt handler\n");
  5178. goto out_scsi_host_put;
  5179. }
  5180. pmcraid_init_tasklets(pinstance);
  5181. /* allocate verious buffers used by LLD.*/
  5182. rc = pmcraid_init_buffers(pinstance);
  5183. if (rc) {
  5184. pmcraid_err("couldn't allocate memory blocks\n");
  5185. goto out_unregister_isr;
  5186. }
  5187. /* check the reset type required */
  5188. pmcraid_reset_type(pinstance);
  5189. pmcraid_enable_interrupts(pinstance, PMCRAID_PCI_INTERRUPTS);
  5190. /* Start IOA firmware initialization and bring card to Operational
  5191. * state.
  5192. */
  5193. pmcraid_info("starting IOA initialization sequence\n");
  5194. if (pmcraid_reset_bringup(pinstance)) {
  5195. dev_err(&pdev->dev, "couldn't initialize IOA\n");
  5196. rc = 1;
  5197. goto out_release_bufs;
  5198. }
  5199. /* Add adapter instance into mid-layer list */
  5200. rc = scsi_add_host(pinstance->host, &pdev->dev);
  5201. if (rc != 0) {
  5202. pmcraid_err("couldn't add host into mid-layer: %d\n", rc);
  5203. goto out_release_bufs;
  5204. }
  5205. scsi_scan_host(pinstance->host);
  5206. rc = pmcraid_setup_chrdev(pinstance);
  5207. if (rc != 0) {
  5208. pmcraid_err("couldn't create mgmt interface, error: %x\n",
  5209. rc);
  5210. goto out_remove_host;
  5211. }
  5212. /* Schedule worker thread to handle CCN and take care of adding and
  5213. * removing devices to OS
  5214. */
  5215. atomic_set(&pinstance->expose_resources, 1);
  5216. schedule_work(&pinstance->worker_q);
  5217. return rc;
  5218. out_remove_host:
  5219. scsi_remove_host(host);
  5220. out_release_bufs:
  5221. pmcraid_release_buffers(pinstance);
  5222. out_unregister_isr:
  5223. pmcraid_kill_tasklets(pinstance);
  5224. pmcraid_unregister_interrupt_handler(pinstance);
  5225. out_scsi_host_put:
  5226. scsi_host_put(host);
  5227. cleanup_nomem:
  5228. iounmap(mapped_pci_addr);
  5229. out_release_regions:
  5230. pci_release_regions(pdev);
  5231. out_disable_device:
  5232. atomic_dec(&pmcraid_adapter_count);
  5233. pci_set_drvdata(pdev, NULL);
  5234. pci_disable_device(pdev);
  5235. return -ENODEV;
  5236. }
  5237. /*
  5238. * PCI driver structure of pcmraid driver
  5239. */
  5240. static struct pci_driver pmcraid_driver = {
  5241. .name = PMCRAID_DRIVER_NAME,
  5242. .id_table = pmcraid_pci_table,
  5243. .probe = pmcraid_probe,
  5244. .remove = pmcraid_remove,
  5245. .suspend = pmcraid_suspend,
  5246. .resume = pmcraid_resume,
  5247. .shutdown = pmcraid_shutdown
  5248. };
  5249. /**
  5250. * pmcraid_init - module load entry point
  5251. */
  5252. static int __init pmcraid_init(void)
  5253. {
  5254. dev_t dev;
  5255. int error;
  5256. pmcraid_info("%s Device Driver version: %s\n",
  5257. PMCRAID_DRIVER_NAME, PMCRAID_DRIVER_VERSION);
  5258. error = alloc_chrdev_region(&dev, 0,
  5259. PMCRAID_MAX_ADAPTERS,
  5260. PMCRAID_DEVFILE);
  5261. if (error) {
  5262. pmcraid_err("failed to get a major number for adapters\n");
  5263. goto out_init;
  5264. }
  5265. pmcraid_major = MAJOR(dev);
  5266. pmcraid_class = class_create(THIS_MODULE, PMCRAID_DEVFILE);
  5267. if (IS_ERR(pmcraid_class)) {
  5268. error = PTR_ERR(pmcraid_class);
  5269. pmcraid_err("failed to register with with sysfs, error = %x\n",
  5270. error);
  5271. goto out_unreg_chrdev;
  5272. }
  5273. error = pmcraid_netlink_init();
  5274. if (error)
  5275. goto out_unreg_chrdev;
  5276. error = pci_register_driver(&pmcraid_driver);
  5277. if (error == 0)
  5278. goto out_init;
  5279. pmcraid_err("failed to register pmcraid driver, error = %x\n",
  5280. error);
  5281. class_destroy(pmcraid_class);
  5282. pmcraid_netlink_release();
  5283. out_unreg_chrdev:
  5284. unregister_chrdev_region(MKDEV(pmcraid_major, 0), PMCRAID_MAX_ADAPTERS);
  5285. out_init:
  5286. return error;
  5287. }
  5288. /**
  5289. * pmcraid_exit - module unload entry point
  5290. */
  5291. static void __exit pmcraid_exit(void)
  5292. {
  5293. pmcraid_netlink_release();
  5294. unregister_chrdev_region(MKDEV(pmcraid_major, 0),
  5295. PMCRAID_MAX_ADAPTERS);
  5296. pci_unregister_driver(&pmcraid_driver);
  5297. class_destroy(pmcraid_class);
  5298. }
  5299. module_init(pmcraid_init);
  5300. module_exit(pmcraid_exit);