hpsa.c 279 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034
  1. /*
  2. * Disk Array driver for HP Smart Array SAS controllers
  3. * Copyright 2016 Microsemi Corporation
  4. * Copyright 2014-2015 PMC-Sierra, Inc.
  5. * Copyright 2000,2009-2015 Hewlett-Packard Development Company, L.P.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  14. * NON INFRINGEMENT. See the GNU General Public License for more details.
  15. *
  16. * Questions/Comments/Bugfixes to esc.storagedev@microsemi.com
  17. *
  18. */
  19. #include <linux/module.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/types.h>
  22. #include <linux/pci.h>
  23. #include <linux/pci-aspm.h>
  24. #include <linux/kernel.h>
  25. #include <linux/slab.h>
  26. #include <linux/delay.h>
  27. #include <linux/fs.h>
  28. #include <linux/timer.h>
  29. #include <linux/init.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/compat.h>
  32. #include <linux/blktrace_api.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/io.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/completion.h>
  37. #include <linux/moduleparam.h>
  38. #include <scsi/scsi.h>
  39. #include <scsi/scsi_cmnd.h>
  40. #include <scsi/scsi_device.h>
  41. #include <scsi/scsi_host.h>
  42. #include <scsi/scsi_tcq.h>
  43. #include <scsi/scsi_eh.h>
  44. #include <scsi/scsi_transport_sas.h>
  45. #include <scsi/scsi_dbg.h>
  46. #include <linux/cciss_ioctl.h>
  47. #include <linux/string.h>
  48. #include <linux/bitmap.h>
  49. #include <linux/atomic.h>
  50. #include <linux/jiffies.h>
  51. #include <linux/percpu-defs.h>
  52. #include <linux/percpu.h>
  53. #include <asm/unaligned.h>
  54. #include <asm/div64.h>
  55. #include "hpsa_cmd.h"
  56. #include "hpsa.h"
  57. /*
  58. * HPSA_DRIVER_VERSION must be 3 byte values (0-255) separated by '.'
  59. * with an optional trailing '-' followed by a byte value (0-255).
  60. */
  61. #define HPSA_DRIVER_VERSION "3.4.16-0"
  62. #define DRIVER_NAME "HP HPSA Driver (v " HPSA_DRIVER_VERSION ")"
  63. #define HPSA "hpsa"
  64. /* How long to wait for CISS doorbell communication */
  65. #define CLEAR_EVENT_WAIT_INTERVAL 20 /* ms for each msleep() call */
  66. #define MODE_CHANGE_WAIT_INTERVAL 10 /* ms for each msleep() call */
  67. #define MAX_CLEAR_EVENT_WAIT 30000 /* times 20 ms = 600 s */
  68. #define MAX_MODE_CHANGE_WAIT 2000 /* times 10 ms = 20 s */
  69. #define MAX_IOCTL_CONFIG_WAIT 1000
  70. /*define how many times we will try a command because of bus resets */
  71. #define MAX_CMD_RETRIES 3
  72. /* Embedded module documentation macros - see modules.h */
  73. MODULE_AUTHOR("Hewlett-Packard Company");
  74. MODULE_DESCRIPTION("Driver for HP Smart Array Controller version " \
  75. HPSA_DRIVER_VERSION);
  76. MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
  77. MODULE_VERSION(HPSA_DRIVER_VERSION);
  78. MODULE_LICENSE("GPL");
  79. static int hpsa_allow_any;
  80. module_param(hpsa_allow_any, int, S_IRUGO|S_IWUSR);
  81. MODULE_PARM_DESC(hpsa_allow_any,
  82. "Allow hpsa driver to access unknown HP Smart Array hardware");
  83. static int hpsa_simple_mode;
  84. module_param(hpsa_simple_mode, int, S_IRUGO|S_IWUSR);
  85. MODULE_PARM_DESC(hpsa_simple_mode,
  86. "Use 'simple mode' rather than 'performant mode'");
  87. /* define the PCI info for the cards we can control */
  88. static const struct pci_device_id hpsa_pci_device_id[] = {
  89. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
  90. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
  91. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
  92. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
  93. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
  94. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324A},
  95. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324B},
  96. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3233},
  97. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3350},
  98. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3351},
  99. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3352},
  100. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3353},
  101. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3354},
  102. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3355},
  103. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSF, 0x103C, 0x3356},
  104. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1921},
  105. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1922},
  106. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1923},
  107. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1924},
  108. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1926},
  109. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1928},
  110. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSH, 0x103C, 0x1929},
  111. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BD},
  112. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BE},
  113. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21BF},
  114. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C0},
  115. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C1},
  116. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C2},
  117. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C3},
  118. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C4},
  119. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C5},
  120. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C6},
  121. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C7},
  122. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C8},
  123. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21C9},
  124. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CA},
  125. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CB},
  126. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CC},
  127. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CD},
  128. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSI, 0x103C, 0x21CE},
  129. {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0580},
  130. {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0581},
  131. {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0582},
  132. {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0583},
  133. {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0584},
  134. {PCI_VENDOR_ID_ADAPTEC2, 0x0290, 0x9005, 0x0585},
  135. {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0076},
  136. {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0087},
  137. {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x007D},
  138. {PCI_VENDOR_ID_HP_3PAR, 0x0075, 0x1590, 0x0088},
  139. {PCI_VENDOR_ID_HP, 0x333f, 0x103c, 0x333f},
  140. {PCI_VENDOR_ID_HP, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  141. PCI_CLASS_STORAGE_RAID << 8, 0xffff << 8, 0},
  142. {0,}
  143. };
  144. MODULE_DEVICE_TABLE(pci, hpsa_pci_device_id);
  145. /* board_id = Subsystem Device ID & Vendor ID
  146. * product = Marketing Name for the board
  147. * access = Address of the struct of function pointers
  148. */
  149. static struct board_type products[] = {
  150. {0x3241103C, "Smart Array P212", &SA5_access},
  151. {0x3243103C, "Smart Array P410", &SA5_access},
  152. {0x3245103C, "Smart Array P410i", &SA5_access},
  153. {0x3247103C, "Smart Array P411", &SA5_access},
  154. {0x3249103C, "Smart Array P812", &SA5_access},
  155. {0x324A103C, "Smart Array P712m", &SA5_access},
  156. {0x324B103C, "Smart Array P711m", &SA5_access},
  157. {0x3233103C, "HP StorageWorks 1210m", &SA5_access}, /* alias of 333f */
  158. {0x3350103C, "Smart Array P222", &SA5_access},
  159. {0x3351103C, "Smart Array P420", &SA5_access},
  160. {0x3352103C, "Smart Array P421", &SA5_access},
  161. {0x3353103C, "Smart Array P822", &SA5_access},
  162. {0x3354103C, "Smart Array P420i", &SA5_access},
  163. {0x3355103C, "Smart Array P220i", &SA5_access},
  164. {0x3356103C, "Smart Array P721m", &SA5_access},
  165. {0x1921103C, "Smart Array P830i", &SA5_access},
  166. {0x1922103C, "Smart Array P430", &SA5_access},
  167. {0x1923103C, "Smart Array P431", &SA5_access},
  168. {0x1924103C, "Smart Array P830", &SA5_access},
  169. {0x1926103C, "Smart Array P731m", &SA5_access},
  170. {0x1928103C, "Smart Array P230i", &SA5_access},
  171. {0x1929103C, "Smart Array P530", &SA5_access},
  172. {0x21BD103C, "Smart Array P244br", &SA5_access},
  173. {0x21BE103C, "Smart Array P741m", &SA5_access},
  174. {0x21BF103C, "Smart HBA H240ar", &SA5_access},
  175. {0x21C0103C, "Smart Array P440ar", &SA5_access},
  176. {0x21C1103C, "Smart Array P840ar", &SA5_access},
  177. {0x21C2103C, "Smart Array P440", &SA5_access},
  178. {0x21C3103C, "Smart Array P441", &SA5_access},
  179. {0x21C4103C, "Smart Array", &SA5_access},
  180. {0x21C5103C, "Smart Array P841", &SA5_access},
  181. {0x21C6103C, "Smart HBA H244br", &SA5_access},
  182. {0x21C7103C, "Smart HBA H240", &SA5_access},
  183. {0x21C8103C, "Smart HBA H241", &SA5_access},
  184. {0x21C9103C, "Smart Array", &SA5_access},
  185. {0x21CA103C, "Smart Array P246br", &SA5_access},
  186. {0x21CB103C, "Smart Array P840", &SA5_access},
  187. {0x21CC103C, "Smart Array", &SA5_access},
  188. {0x21CD103C, "Smart Array", &SA5_access},
  189. {0x21CE103C, "Smart HBA", &SA5_access},
  190. {0x05809005, "SmartHBA-SA", &SA5_access},
  191. {0x05819005, "SmartHBA-SA 8i", &SA5_access},
  192. {0x05829005, "SmartHBA-SA 8i8e", &SA5_access},
  193. {0x05839005, "SmartHBA-SA 8e", &SA5_access},
  194. {0x05849005, "SmartHBA-SA 16i", &SA5_access},
  195. {0x05859005, "SmartHBA-SA 4i4e", &SA5_access},
  196. {0x00761590, "HP Storage P1224 Array Controller", &SA5_access},
  197. {0x00871590, "HP Storage P1224e Array Controller", &SA5_access},
  198. {0x007D1590, "HP Storage P1228 Array Controller", &SA5_access},
  199. {0x00881590, "HP Storage P1228e Array Controller", &SA5_access},
  200. {0x333f103c, "HP StorageWorks 1210m Array Controller", &SA5_access},
  201. {0xFFFF103C, "Unknown Smart Array", &SA5_access},
  202. };
  203. static struct scsi_transport_template *hpsa_sas_transport_template;
  204. static int hpsa_add_sas_host(struct ctlr_info *h);
  205. static void hpsa_delete_sas_host(struct ctlr_info *h);
  206. static int hpsa_add_sas_device(struct hpsa_sas_node *hpsa_sas_node,
  207. struct hpsa_scsi_dev_t *device);
  208. static void hpsa_remove_sas_device(struct hpsa_scsi_dev_t *device);
  209. static struct hpsa_scsi_dev_t
  210. *hpsa_find_device_by_sas_rphy(struct ctlr_info *h,
  211. struct sas_rphy *rphy);
  212. #define SCSI_CMD_BUSY ((struct scsi_cmnd *)&hpsa_cmd_busy)
  213. static const struct scsi_cmnd hpsa_cmd_busy;
  214. #define SCSI_CMD_IDLE ((struct scsi_cmnd *)&hpsa_cmd_idle)
  215. static const struct scsi_cmnd hpsa_cmd_idle;
  216. static int number_of_controllers;
  217. static irqreturn_t do_hpsa_intr_intx(int irq, void *dev_id);
  218. static irqreturn_t do_hpsa_intr_msi(int irq, void *dev_id);
  219. static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg);
  220. #ifdef CONFIG_COMPAT
  221. static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd,
  222. void __user *arg);
  223. #endif
  224. static void cmd_free(struct ctlr_info *h, struct CommandList *c);
  225. static struct CommandList *cmd_alloc(struct ctlr_info *h);
  226. static void cmd_tagged_free(struct ctlr_info *h, struct CommandList *c);
  227. static struct CommandList *cmd_tagged_alloc(struct ctlr_info *h,
  228. struct scsi_cmnd *scmd);
  229. static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
  230. void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
  231. int cmd_type);
  232. static void hpsa_free_cmd_pool(struct ctlr_info *h);
  233. #define VPD_PAGE (1 << 8)
  234. #define HPSA_SIMPLE_ERROR_BITS 0x03
  235. static int hpsa_scsi_queue_command(struct Scsi_Host *h, struct scsi_cmnd *cmd);
  236. static void hpsa_scan_start(struct Scsi_Host *);
  237. static int hpsa_scan_finished(struct Scsi_Host *sh,
  238. unsigned long elapsed_time);
  239. static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth);
  240. static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd);
  241. static int hpsa_eh_abort_handler(struct scsi_cmnd *scsicmd);
  242. static int hpsa_slave_alloc(struct scsi_device *sdev);
  243. static int hpsa_slave_configure(struct scsi_device *sdev);
  244. static void hpsa_slave_destroy(struct scsi_device *sdev);
  245. static void hpsa_update_scsi_devices(struct ctlr_info *h);
  246. static int check_for_unit_attention(struct ctlr_info *h,
  247. struct CommandList *c);
  248. static void check_ioctl_unit_attention(struct ctlr_info *h,
  249. struct CommandList *c);
  250. /* performant mode helper functions */
  251. static void calc_bucket_map(int *bucket, int num_buckets,
  252. int nsgs, int min_blocks, u32 *bucket_map);
  253. static void hpsa_free_performant_mode(struct ctlr_info *h);
  254. static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h);
  255. static inline u32 next_command(struct ctlr_info *h, u8 q);
  256. static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
  257. u32 *cfg_base_addr, u64 *cfg_base_addr_index,
  258. u64 *cfg_offset);
  259. static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
  260. unsigned long *memory_bar);
  261. static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id);
  262. static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
  263. int wait_for_ready);
  264. static inline void finish_cmd(struct CommandList *c);
  265. static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h);
  266. #define BOARD_NOT_READY 0
  267. #define BOARD_READY 1
  268. static void hpsa_drain_accel_commands(struct ctlr_info *h);
  269. static void hpsa_flush_cache(struct ctlr_info *h);
  270. static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
  271. struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
  272. u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk);
  273. static void hpsa_command_resubmit_worker(struct work_struct *work);
  274. static u32 lockup_detected(struct ctlr_info *h);
  275. static int detect_controller_lockup(struct ctlr_info *h);
  276. static void hpsa_disable_rld_caching(struct ctlr_info *h);
  277. static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
  278. struct ReportExtendedLUNdata *buf, int bufsize);
  279. static bool hpsa_vpd_page_supported(struct ctlr_info *h,
  280. unsigned char scsi3addr[], u8 page);
  281. static int hpsa_luns_changed(struct ctlr_info *h);
  282. static bool hpsa_cmd_dev_match(struct ctlr_info *h, struct CommandList *c,
  283. struct hpsa_scsi_dev_t *dev,
  284. unsigned char *scsi3addr);
  285. static inline struct ctlr_info *sdev_to_hba(struct scsi_device *sdev)
  286. {
  287. unsigned long *priv = shost_priv(sdev->host);
  288. return (struct ctlr_info *) *priv;
  289. }
  290. static inline struct ctlr_info *shost_to_hba(struct Scsi_Host *sh)
  291. {
  292. unsigned long *priv = shost_priv(sh);
  293. return (struct ctlr_info *) *priv;
  294. }
  295. static inline bool hpsa_is_cmd_idle(struct CommandList *c)
  296. {
  297. return c->scsi_cmd == SCSI_CMD_IDLE;
  298. }
  299. static inline bool hpsa_is_pending_event(struct CommandList *c)
  300. {
  301. return c->abort_pending || c->reset_pending;
  302. }
  303. /* extract sense key, asc, and ascq from sense data. -1 means invalid. */
  304. static void decode_sense_data(const u8 *sense_data, int sense_data_len,
  305. u8 *sense_key, u8 *asc, u8 *ascq)
  306. {
  307. struct scsi_sense_hdr sshdr;
  308. bool rc;
  309. *sense_key = -1;
  310. *asc = -1;
  311. *ascq = -1;
  312. if (sense_data_len < 1)
  313. return;
  314. rc = scsi_normalize_sense(sense_data, sense_data_len, &sshdr);
  315. if (rc) {
  316. *sense_key = sshdr.sense_key;
  317. *asc = sshdr.asc;
  318. *ascq = sshdr.ascq;
  319. }
  320. }
  321. static int check_for_unit_attention(struct ctlr_info *h,
  322. struct CommandList *c)
  323. {
  324. u8 sense_key, asc, ascq;
  325. int sense_len;
  326. if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
  327. sense_len = sizeof(c->err_info->SenseInfo);
  328. else
  329. sense_len = c->err_info->SenseLen;
  330. decode_sense_data(c->err_info->SenseInfo, sense_len,
  331. &sense_key, &asc, &ascq);
  332. if (sense_key != UNIT_ATTENTION || asc == 0xff)
  333. return 0;
  334. switch (asc) {
  335. case STATE_CHANGED:
  336. dev_warn(&h->pdev->dev,
  337. "%s: a state change detected, command retried\n",
  338. h->devname);
  339. break;
  340. case LUN_FAILED:
  341. dev_warn(&h->pdev->dev,
  342. "%s: LUN failure detected\n", h->devname);
  343. break;
  344. case REPORT_LUNS_CHANGED:
  345. dev_warn(&h->pdev->dev,
  346. "%s: report LUN data changed\n", h->devname);
  347. /*
  348. * Note: this REPORT_LUNS_CHANGED condition only occurs on the external
  349. * target (array) devices.
  350. */
  351. break;
  352. case POWER_OR_RESET:
  353. dev_warn(&h->pdev->dev,
  354. "%s: a power on or device reset detected\n",
  355. h->devname);
  356. break;
  357. case UNIT_ATTENTION_CLEARED:
  358. dev_warn(&h->pdev->dev,
  359. "%s: unit attention cleared by another initiator\n",
  360. h->devname);
  361. break;
  362. default:
  363. dev_warn(&h->pdev->dev,
  364. "%s: unknown unit attention detected\n",
  365. h->devname);
  366. break;
  367. }
  368. return 1;
  369. }
  370. static int check_for_busy(struct ctlr_info *h, struct CommandList *c)
  371. {
  372. if (c->err_info->CommandStatus != CMD_TARGET_STATUS ||
  373. (c->err_info->ScsiStatus != SAM_STAT_BUSY &&
  374. c->err_info->ScsiStatus != SAM_STAT_TASK_SET_FULL))
  375. return 0;
  376. dev_warn(&h->pdev->dev, HPSA "device busy");
  377. return 1;
  378. }
  379. static u32 lockup_detected(struct ctlr_info *h);
  380. static ssize_t host_show_lockup_detected(struct device *dev,
  381. struct device_attribute *attr, char *buf)
  382. {
  383. int ld;
  384. struct ctlr_info *h;
  385. struct Scsi_Host *shost = class_to_shost(dev);
  386. h = shost_to_hba(shost);
  387. ld = lockup_detected(h);
  388. return sprintf(buf, "ld=%d\n", ld);
  389. }
  390. static ssize_t host_store_hp_ssd_smart_path_status(struct device *dev,
  391. struct device_attribute *attr,
  392. const char *buf, size_t count)
  393. {
  394. int status, len;
  395. struct ctlr_info *h;
  396. struct Scsi_Host *shost = class_to_shost(dev);
  397. char tmpbuf[10];
  398. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  399. return -EACCES;
  400. len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
  401. strncpy(tmpbuf, buf, len);
  402. tmpbuf[len] = '\0';
  403. if (sscanf(tmpbuf, "%d", &status) != 1)
  404. return -EINVAL;
  405. h = shost_to_hba(shost);
  406. h->acciopath_status = !!status;
  407. dev_warn(&h->pdev->dev,
  408. "hpsa: HP SSD Smart Path %s via sysfs update.\n",
  409. h->acciopath_status ? "enabled" : "disabled");
  410. return count;
  411. }
  412. static ssize_t host_store_raid_offload_debug(struct device *dev,
  413. struct device_attribute *attr,
  414. const char *buf, size_t count)
  415. {
  416. int debug_level, len;
  417. struct ctlr_info *h;
  418. struct Scsi_Host *shost = class_to_shost(dev);
  419. char tmpbuf[10];
  420. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  421. return -EACCES;
  422. len = count > sizeof(tmpbuf) - 1 ? sizeof(tmpbuf) - 1 : count;
  423. strncpy(tmpbuf, buf, len);
  424. tmpbuf[len] = '\0';
  425. if (sscanf(tmpbuf, "%d", &debug_level) != 1)
  426. return -EINVAL;
  427. if (debug_level < 0)
  428. debug_level = 0;
  429. h = shost_to_hba(shost);
  430. h->raid_offload_debug = debug_level;
  431. dev_warn(&h->pdev->dev, "hpsa: Set raid_offload_debug level = %d\n",
  432. h->raid_offload_debug);
  433. return count;
  434. }
  435. static ssize_t host_store_rescan(struct device *dev,
  436. struct device_attribute *attr,
  437. const char *buf, size_t count)
  438. {
  439. struct ctlr_info *h;
  440. struct Scsi_Host *shost = class_to_shost(dev);
  441. h = shost_to_hba(shost);
  442. hpsa_scan_start(h->scsi_host);
  443. return count;
  444. }
  445. static ssize_t host_show_firmware_revision(struct device *dev,
  446. struct device_attribute *attr, char *buf)
  447. {
  448. struct ctlr_info *h;
  449. struct Scsi_Host *shost = class_to_shost(dev);
  450. unsigned char *fwrev;
  451. h = shost_to_hba(shost);
  452. if (!h->hba_inquiry_data)
  453. return 0;
  454. fwrev = &h->hba_inquiry_data[32];
  455. return snprintf(buf, 20, "%c%c%c%c\n",
  456. fwrev[0], fwrev[1], fwrev[2], fwrev[3]);
  457. }
  458. static ssize_t host_show_commands_outstanding(struct device *dev,
  459. struct device_attribute *attr, char *buf)
  460. {
  461. struct Scsi_Host *shost = class_to_shost(dev);
  462. struct ctlr_info *h = shost_to_hba(shost);
  463. return snprintf(buf, 20, "%d\n",
  464. atomic_read(&h->commands_outstanding));
  465. }
  466. static ssize_t host_show_transport_mode(struct device *dev,
  467. struct device_attribute *attr, char *buf)
  468. {
  469. struct ctlr_info *h;
  470. struct Scsi_Host *shost = class_to_shost(dev);
  471. h = shost_to_hba(shost);
  472. return snprintf(buf, 20, "%s\n",
  473. h->transMethod & CFGTBL_Trans_Performant ?
  474. "performant" : "simple");
  475. }
  476. static ssize_t host_show_hp_ssd_smart_path_status(struct device *dev,
  477. struct device_attribute *attr, char *buf)
  478. {
  479. struct ctlr_info *h;
  480. struct Scsi_Host *shost = class_to_shost(dev);
  481. h = shost_to_hba(shost);
  482. return snprintf(buf, 30, "HP SSD Smart Path %s\n",
  483. (h->acciopath_status == 1) ? "enabled" : "disabled");
  484. }
  485. /* List of controllers which cannot be hard reset on kexec with reset_devices */
  486. static u32 unresettable_controller[] = {
  487. 0x324a103C, /* Smart Array P712m */
  488. 0x324b103C, /* Smart Array P711m */
  489. 0x3223103C, /* Smart Array P800 */
  490. 0x3234103C, /* Smart Array P400 */
  491. 0x3235103C, /* Smart Array P400i */
  492. 0x3211103C, /* Smart Array E200i */
  493. 0x3212103C, /* Smart Array E200 */
  494. 0x3213103C, /* Smart Array E200i */
  495. 0x3214103C, /* Smart Array E200i */
  496. 0x3215103C, /* Smart Array E200i */
  497. 0x3237103C, /* Smart Array E500 */
  498. 0x323D103C, /* Smart Array P700m */
  499. 0x40800E11, /* Smart Array 5i */
  500. 0x409C0E11, /* Smart Array 6400 */
  501. 0x409D0E11, /* Smart Array 6400 EM */
  502. 0x40700E11, /* Smart Array 5300 */
  503. 0x40820E11, /* Smart Array 532 */
  504. 0x40830E11, /* Smart Array 5312 */
  505. 0x409A0E11, /* Smart Array 641 */
  506. 0x409B0E11, /* Smart Array 642 */
  507. 0x40910E11, /* Smart Array 6i */
  508. };
  509. /* List of controllers which cannot even be soft reset */
  510. static u32 soft_unresettable_controller[] = {
  511. 0x40800E11, /* Smart Array 5i */
  512. 0x40700E11, /* Smart Array 5300 */
  513. 0x40820E11, /* Smart Array 532 */
  514. 0x40830E11, /* Smart Array 5312 */
  515. 0x409A0E11, /* Smart Array 641 */
  516. 0x409B0E11, /* Smart Array 642 */
  517. 0x40910E11, /* Smart Array 6i */
  518. /* Exclude 640x boards. These are two pci devices in one slot
  519. * which share a battery backed cache module. One controls the
  520. * cache, the other accesses the cache through the one that controls
  521. * it. If we reset the one controlling the cache, the other will
  522. * likely not be happy. Just forbid resetting this conjoined mess.
  523. * The 640x isn't really supported by hpsa anyway.
  524. */
  525. 0x409C0E11, /* Smart Array 6400 */
  526. 0x409D0E11, /* Smart Array 6400 EM */
  527. };
  528. static u32 needs_abort_tags_swizzled[] = {
  529. 0x323D103C, /* Smart Array P700m */
  530. 0x324a103C, /* Smart Array P712m */
  531. 0x324b103C, /* SmartArray P711m */
  532. };
  533. static int board_id_in_array(u32 a[], int nelems, u32 board_id)
  534. {
  535. int i;
  536. for (i = 0; i < nelems; i++)
  537. if (a[i] == board_id)
  538. return 1;
  539. return 0;
  540. }
  541. static int ctlr_is_hard_resettable(u32 board_id)
  542. {
  543. return !board_id_in_array(unresettable_controller,
  544. ARRAY_SIZE(unresettable_controller), board_id);
  545. }
  546. static int ctlr_is_soft_resettable(u32 board_id)
  547. {
  548. return !board_id_in_array(soft_unresettable_controller,
  549. ARRAY_SIZE(soft_unresettable_controller), board_id);
  550. }
  551. static int ctlr_is_resettable(u32 board_id)
  552. {
  553. return ctlr_is_hard_resettable(board_id) ||
  554. ctlr_is_soft_resettable(board_id);
  555. }
  556. static int ctlr_needs_abort_tags_swizzled(u32 board_id)
  557. {
  558. return board_id_in_array(needs_abort_tags_swizzled,
  559. ARRAY_SIZE(needs_abort_tags_swizzled), board_id);
  560. }
  561. static ssize_t host_show_resettable(struct device *dev,
  562. struct device_attribute *attr, char *buf)
  563. {
  564. struct ctlr_info *h;
  565. struct Scsi_Host *shost = class_to_shost(dev);
  566. h = shost_to_hba(shost);
  567. return snprintf(buf, 20, "%d\n", ctlr_is_resettable(h->board_id));
  568. }
  569. static inline int is_logical_dev_addr_mode(unsigned char scsi3addr[])
  570. {
  571. return (scsi3addr[3] & 0xC0) == 0x40;
  572. }
  573. static const char * const raid_label[] = { "0", "4", "1(+0)", "5", "5+1", "6",
  574. "1(+0)ADM", "UNKNOWN", "PHYS DRV"
  575. };
  576. #define HPSA_RAID_0 0
  577. #define HPSA_RAID_4 1
  578. #define HPSA_RAID_1 2 /* also used for RAID 10 */
  579. #define HPSA_RAID_5 3 /* also used for RAID 50 */
  580. #define HPSA_RAID_51 4
  581. #define HPSA_RAID_6 5 /* also used for RAID 60 */
  582. #define HPSA_RAID_ADM 6 /* also used for RAID 1+0 ADM */
  583. #define RAID_UNKNOWN (ARRAY_SIZE(raid_label) - 2)
  584. #define PHYSICAL_DRIVE (ARRAY_SIZE(raid_label) - 1)
  585. static inline bool is_logical_device(struct hpsa_scsi_dev_t *device)
  586. {
  587. return !device->physical_device;
  588. }
  589. static ssize_t raid_level_show(struct device *dev,
  590. struct device_attribute *attr, char *buf)
  591. {
  592. ssize_t l = 0;
  593. unsigned char rlevel;
  594. struct ctlr_info *h;
  595. struct scsi_device *sdev;
  596. struct hpsa_scsi_dev_t *hdev;
  597. unsigned long flags;
  598. sdev = to_scsi_device(dev);
  599. h = sdev_to_hba(sdev);
  600. spin_lock_irqsave(&h->lock, flags);
  601. hdev = sdev->hostdata;
  602. if (!hdev) {
  603. spin_unlock_irqrestore(&h->lock, flags);
  604. return -ENODEV;
  605. }
  606. /* Is this even a logical drive? */
  607. if (!is_logical_device(hdev)) {
  608. spin_unlock_irqrestore(&h->lock, flags);
  609. l = snprintf(buf, PAGE_SIZE, "N/A\n");
  610. return l;
  611. }
  612. rlevel = hdev->raid_level;
  613. spin_unlock_irqrestore(&h->lock, flags);
  614. if (rlevel > RAID_UNKNOWN)
  615. rlevel = RAID_UNKNOWN;
  616. l = snprintf(buf, PAGE_SIZE, "RAID %s\n", raid_label[rlevel]);
  617. return l;
  618. }
  619. static ssize_t lunid_show(struct device *dev,
  620. struct device_attribute *attr, char *buf)
  621. {
  622. struct ctlr_info *h;
  623. struct scsi_device *sdev;
  624. struct hpsa_scsi_dev_t *hdev;
  625. unsigned long flags;
  626. unsigned char lunid[8];
  627. sdev = to_scsi_device(dev);
  628. h = sdev_to_hba(sdev);
  629. spin_lock_irqsave(&h->lock, flags);
  630. hdev = sdev->hostdata;
  631. if (!hdev) {
  632. spin_unlock_irqrestore(&h->lock, flags);
  633. return -ENODEV;
  634. }
  635. memcpy(lunid, hdev->scsi3addr, sizeof(lunid));
  636. spin_unlock_irqrestore(&h->lock, flags);
  637. return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  638. lunid[0], lunid[1], lunid[2], lunid[3],
  639. lunid[4], lunid[5], lunid[6], lunid[7]);
  640. }
  641. static ssize_t unique_id_show(struct device *dev,
  642. struct device_attribute *attr, char *buf)
  643. {
  644. struct ctlr_info *h;
  645. struct scsi_device *sdev;
  646. struct hpsa_scsi_dev_t *hdev;
  647. unsigned long flags;
  648. unsigned char sn[16];
  649. sdev = to_scsi_device(dev);
  650. h = sdev_to_hba(sdev);
  651. spin_lock_irqsave(&h->lock, flags);
  652. hdev = sdev->hostdata;
  653. if (!hdev) {
  654. spin_unlock_irqrestore(&h->lock, flags);
  655. return -ENODEV;
  656. }
  657. memcpy(sn, hdev->device_id, sizeof(sn));
  658. spin_unlock_irqrestore(&h->lock, flags);
  659. return snprintf(buf, 16 * 2 + 2,
  660. "%02X%02X%02X%02X%02X%02X%02X%02X"
  661. "%02X%02X%02X%02X%02X%02X%02X%02X\n",
  662. sn[0], sn[1], sn[2], sn[3],
  663. sn[4], sn[5], sn[6], sn[7],
  664. sn[8], sn[9], sn[10], sn[11],
  665. sn[12], sn[13], sn[14], sn[15]);
  666. }
  667. static ssize_t sas_address_show(struct device *dev,
  668. struct device_attribute *attr, char *buf)
  669. {
  670. struct ctlr_info *h;
  671. struct scsi_device *sdev;
  672. struct hpsa_scsi_dev_t *hdev;
  673. unsigned long flags;
  674. u64 sas_address;
  675. sdev = to_scsi_device(dev);
  676. h = sdev_to_hba(sdev);
  677. spin_lock_irqsave(&h->lock, flags);
  678. hdev = sdev->hostdata;
  679. if (!hdev || is_logical_device(hdev) || !hdev->expose_device) {
  680. spin_unlock_irqrestore(&h->lock, flags);
  681. return -ENODEV;
  682. }
  683. sas_address = hdev->sas_address;
  684. spin_unlock_irqrestore(&h->lock, flags);
  685. return snprintf(buf, PAGE_SIZE, "0x%016llx\n", sas_address);
  686. }
  687. static ssize_t host_show_hp_ssd_smart_path_enabled(struct device *dev,
  688. struct device_attribute *attr, char *buf)
  689. {
  690. struct ctlr_info *h;
  691. struct scsi_device *sdev;
  692. struct hpsa_scsi_dev_t *hdev;
  693. unsigned long flags;
  694. int offload_enabled;
  695. sdev = to_scsi_device(dev);
  696. h = sdev_to_hba(sdev);
  697. spin_lock_irqsave(&h->lock, flags);
  698. hdev = sdev->hostdata;
  699. if (!hdev) {
  700. spin_unlock_irqrestore(&h->lock, flags);
  701. return -ENODEV;
  702. }
  703. offload_enabled = hdev->offload_enabled;
  704. spin_unlock_irqrestore(&h->lock, flags);
  705. return snprintf(buf, 20, "%d\n", offload_enabled);
  706. }
  707. #define MAX_PATHS 8
  708. static ssize_t path_info_show(struct device *dev,
  709. struct device_attribute *attr, char *buf)
  710. {
  711. struct ctlr_info *h;
  712. struct scsi_device *sdev;
  713. struct hpsa_scsi_dev_t *hdev;
  714. unsigned long flags;
  715. int i;
  716. int output_len = 0;
  717. u8 box;
  718. u8 bay;
  719. u8 path_map_index = 0;
  720. char *active;
  721. unsigned char phys_connector[2];
  722. sdev = to_scsi_device(dev);
  723. h = sdev_to_hba(sdev);
  724. spin_lock_irqsave(&h->devlock, flags);
  725. hdev = sdev->hostdata;
  726. if (!hdev) {
  727. spin_unlock_irqrestore(&h->devlock, flags);
  728. return -ENODEV;
  729. }
  730. bay = hdev->bay;
  731. for (i = 0; i < MAX_PATHS; i++) {
  732. path_map_index = 1<<i;
  733. if (i == hdev->active_path_index)
  734. active = "Active";
  735. else if (hdev->path_map & path_map_index)
  736. active = "Inactive";
  737. else
  738. continue;
  739. output_len += scnprintf(buf + output_len,
  740. PAGE_SIZE - output_len,
  741. "[%d:%d:%d:%d] %20.20s ",
  742. h->scsi_host->host_no,
  743. hdev->bus, hdev->target, hdev->lun,
  744. scsi_device_type(hdev->devtype));
  745. if (hdev->devtype == TYPE_RAID || is_logical_device(hdev)) {
  746. output_len += scnprintf(buf + output_len,
  747. PAGE_SIZE - output_len,
  748. "%s\n", active);
  749. continue;
  750. }
  751. box = hdev->box[i];
  752. memcpy(&phys_connector, &hdev->phys_connector[i],
  753. sizeof(phys_connector));
  754. if (phys_connector[0] < '0')
  755. phys_connector[0] = '0';
  756. if (phys_connector[1] < '0')
  757. phys_connector[1] = '0';
  758. output_len += scnprintf(buf + output_len,
  759. PAGE_SIZE - output_len,
  760. "PORT: %.2s ",
  761. phys_connector);
  762. if ((hdev->devtype == TYPE_DISK || hdev->devtype == TYPE_ZBC) &&
  763. hdev->expose_device) {
  764. if (box == 0 || box == 0xFF) {
  765. output_len += scnprintf(buf + output_len,
  766. PAGE_SIZE - output_len,
  767. "BAY: %hhu %s\n",
  768. bay, active);
  769. } else {
  770. output_len += scnprintf(buf + output_len,
  771. PAGE_SIZE - output_len,
  772. "BOX: %hhu BAY: %hhu %s\n",
  773. box, bay, active);
  774. }
  775. } else if (box != 0 && box != 0xFF) {
  776. output_len += scnprintf(buf + output_len,
  777. PAGE_SIZE - output_len, "BOX: %hhu %s\n",
  778. box, active);
  779. } else
  780. output_len += scnprintf(buf + output_len,
  781. PAGE_SIZE - output_len, "%s\n", active);
  782. }
  783. spin_unlock_irqrestore(&h->devlock, flags);
  784. return output_len;
  785. }
  786. static DEVICE_ATTR(raid_level, S_IRUGO, raid_level_show, NULL);
  787. static DEVICE_ATTR(lunid, S_IRUGO, lunid_show, NULL);
  788. static DEVICE_ATTR(unique_id, S_IRUGO, unique_id_show, NULL);
  789. static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
  790. static DEVICE_ATTR(sas_address, S_IRUGO, sas_address_show, NULL);
  791. static DEVICE_ATTR(hp_ssd_smart_path_enabled, S_IRUGO,
  792. host_show_hp_ssd_smart_path_enabled, NULL);
  793. static DEVICE_ATTR(path_info, S_IRUGO, path_info_show, NULL);
  794. static DEVICE_ATTR(hp_ssd_smart_path_status, S_IWUSR|S_IRUGO|S_IROTH,
  795. host_show_hp_ssd_smart_path_status,
  796. host_store_hp_ssd_smart_path_status);
  797. static DEVICE_ATTR(raid_offload_debug, S_IWUSR, NULL,
  798. host_store_raid_offload_debug);
  799. static DEVICE_ATTR(firmware_revision, S_IRUGO,
  800. host_show_firmware_revision, NULL);
  801. static DEVICE_ATTR(commands_outstanding, S_IRUGO,
  802. host_show_commands_outstanding, NULL);
  803. static DEVICE_ATTR(transport_mode, S_IRUGO,
  804. host_show_transport_mode, NULL);
  805. static DEVICE_ATTR(resettable, S_IRUGO,
  806. host_show_resettable, NULL);
  807. static DEVICE_ATTR(lockup_detected, S_IRUGO,
  808. host_show_lockup_detected, NULL);
  809. static struct device_attribute *hpsa_sdev_attrs[] = {
  810. &dev_attr_raid_level,
  811. &dev_attr_lunid,
  812. &dev_attr_unique_id,
  813. &dev_attr_hp_ssd_smart_path_enabled,
  814. &dev_attr_path_info,
  815. &dev_attr_sas_address,
  816. NULL,
  817. };
  818. static struct device_attribute *hpsa_shost_attrs[] = {
  819. &dev_attr_rescan,
  820. &dev_attr_firmware_revision,
  821. &dev_attr_commands_outstanding,
  822. &dev_attr_transport_mode,
  823. &dev_attr_resettable,
  824. &dev_attr_hp_ssd_smart_path_status,
  825. &dev_attr_raid_offload_debug,
  826. &dev_attr_lockup_detected,
  827. NULL,
  828. };
  829. #define HPSA_NRESERVED_CMDS (HPSA_CMDS_RESERVED_FOR_ABORTS + \
  830. HPSA_CMDS_RESERVED_FOR_DRIVER + HPSA_MAX_CONCURRENT_PASSTHRUS)
  831. static struct scsi_host_template hpsa_driver_template = {
  832. .module = THIS_MODULE,
  833. .name = HPSA,
  834. .proc_name = HPSA,
  835. .queuecommand = hpsa_scsi_queue_command,
  836. .scan_start = hpsa_scan_start,
  837. .scan_finished = hpsa_scan_finished,
  838. .change_queue_depth = hpsa_change_queue_depth,
  839. .this_id = -1,
  840. .use_clustering = ENABLE_CLUSTERING,
  841. .eh_abort_handler = hpsa_eh_abort_handler,
  842. .eh_device_reset_handler = hpsa_eh_device_reset_handler,
  843. .ioctl = hpsa_ioctl,
  844. .slave_alloc = hpsa_slave_alloc,
  845. .slave_configure = hpsa_slave_configure,
  846. .slave_destroy = hpsa_slave_destroy,
  847. #ifdef CONFIG_COMPAT
  848. .compat_ioctl = hpsa_compat_ioctl,
  849. #endif
  850. .sdev_attrs = hpsa_sdev_attrs,
  851. .shost_attrs = hpsa_shost_attrs,
  852. .max_sectors = 8192,
  853. .no_write_same = 1,
  854. };
  855. static inline u32 next_command(struct ctlr_info *h, u8 q)
  856. {
  857. u32 a;
  858. struct reply_queue_buffer *rq = &h->reply_queue[q];
  859. if (h->transMethod & CFGTBL_Trans_io_accel1)
  860. return h->access.command_completed(h, q);
  861. if (unlikely(!(h->transMethod & CFGTBL_Trans_Performant)))
  862. return h->access.command_completed(h, q);
  863. if ((rq->head[rq->current_entry] & 1) == rq->wraparound) {
  864. a = rq->head[rq->current_entry];
  865. rq->current_entry++;
  866. atomic_dec(&h->commands_outstanding);
  867. } else {
  868. a = FIFO_EMPTY;
  869. }
  870. /* Check for wraparound */
  871. if (rq->current_entry == h->max_commands) {
  872. rq->current_entry = 0;
  873. rq->wraparound ^= 1;
  874. }
  875. return a;
  876. }
  877. /*
  878. * There are some special bits in the bus address of the
  879. * command that we have to set for the controller to know
  880. * how to process the command:
  881. *
  882. * Normal performant mode:
  883. * bit 0: 1 means performant mode, 0 means simple mode.
  884. * bits 1-3 = block fetch table entry
  885. * bits 4-6 = command type (== 0)
  886. *
  887. * ioaccel1 mode:
  888. * bit 0 = "performant mode" bit.
  889. * bits 1-3 = block fetch table entry
  890. * bits 4-6 = command type (== 110)
  891. * (command type is needed because ioaccel1 mode
  892. * commands are submitted through the same register as normal
  893. * mode commands, so this is how the controller knows whether
  894. * the command is normal mode or ioaccel1 mode.)
  895. *
  896. * ioaccel2 mode:
  897. * bit 0 = "performant mode" bit.
  898. * bits 1-4 = block fetch table entry (note extra bit)
  899. * bits 4-6 = not needed, because ioaccel2 mode has
  900. * a separate special register for submitting commands.
  901. */
  902. /*
  903. * set_performant_mode: Modify the tag for cciss performant
  904. * set bit 0 for pull model, bits 3-1 for block fetch
  905. * register number
  906. */
  907. #define DEFAULT_REPLY_QUEUE (-1)
  908. static void set_performant_mode(struct ctlr_info *h, struct CommandList *c,
  909. int reply_queue)
  910. {
  911. if (likely(h->transMethod & CFGTBL_Trans_Performant)) {
  912. c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
  913. if (unlikely(!h->msix_vector))
  914. return;
  915. if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
  916. c->Header.ReplyQueue =
  917. raw_smp_processor_id() % h->nreply_queues;
  918. else
  919. c->Header.ReplyQueue = reply_queue % h->nreply_queues;
  920. }
  921. }
  922. static void set_ioaccel1_performant_mode(struct ctlr_info *h,
  923. struct CommandList *c,
  924. int reply_queue)
  925. {
  926. struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
  927. /*
  928. * Tell the controller to post the reply to the queue for this
  929. * processor. This seems to give the best I/O throughput.
  930. */
  931. if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
  932. cp->ReplyQueue = smp_processor_id() % h->nreply_queues;
  933. else
  934. cp->ReplyQueue = reply_queue % h->nreply_queues;
  935. /*
  936. * Set the bits in the address sent down to include:
  937. * - performant mode bit (bit 0)
  938. * - pull count (bits 1-3)
  939. * - command type (bits 4-6)
  940. */
  941. c->busaddr |= 1 | (h->ioaccel1_blockFetchTable[c->Header.SGList] << 1) |
  942. IOACCEL1_BUSADDR_CMDTYPE;
  943. }
  944. static void set_ioaccel2_tmf_performant_mode(struct ctlr_info *h,
  945. struct CommandList *c,
  946. int reply_queue)
  947. {
  948. struct hpsa_tmf_struct *cp = (struct hpsa_tmf_struct *)
  949. &h->ioaccel2_cmd_pool[c->cmdindex];
  950. /* Tell the controller to post the reply to the queue for this
  951. * processor. This seems to give the best I/O throughput.
  952. */
  953. if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
  954. cp->reply_queue = smp_processor_id() % h->nreply_queues;
  955. else
  956. cp->reply_queue = reply_queue % h->nreply_queues;
  957. /* Set the bits in the address sent down to include:
  958. * - performant mode bit not used in ioaccel mode 2
  959. * - pull count (bits 0-3)
  960. * - command type isn't needed for ioaccel2
  961. */
  962. c->busaddr |= h->ioaccel2_blockFetchTable[0];
  963. }
  964. static void set_ioaccel2_performant_mode(struct ctlr_info *h,
  965. struct CommandList *c,
  966. int reply_queue)
  967. {
  968. struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
  969. /*
  970. * Tell the controller to post the reply to the queue for this
  971. * processor. This seems to give the best I/O throughput.
  972. */
  973. if (likely(reply_queue == DEFAULT_REPLY_QUEUE))
  974. cp->reply_queue = smp_processor_id() % h->nreply_queues;
  975. else
  976. cp->reply_queue = reply_queue % h->nreply_queues;
  977. /*
  978. * Set the bits in the address sent down to include:
  979. * - performant mode bit not used in ioaccel mode 2
  980. * - pull count (bits 0-3)
  981. * - command type isn't needed for ioaccel2
  982. */
  983. c->busaddr |= (h->ioaccel2_blockFetchTable[cp->sg_count]);
  984. }
  985. static int is_firmware_flash_cmd(u8 *cdb)
  986. {
  987. return cdb[0] == BMIC_WRITE && cdb[6] == BMIC_FLASH_FIRMWARE;
  988. }
  989. /*
  990. * During firmware flash, the heartbeat register may not update as frequently
  991. * as it should. So we dial down lockup detection during firmware flash. and
  992. * dial it back up when firmware flash completes.
  993. */
  994. #define HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH (240 * HZ)
  995. #define HEARTBEAT_SAMPLE_INTERVAL (30 * HZ)
  996. static void dial_down_lockup_detection_during_fw_flash(struct ctlr_info *h,
  997. struct CommandList *c)
  998. {
  999. if (!is_firmware_flash_cmd(c->Request.CDB))
  1000. return;
  1001. atomic_inc(&h->firmware_flash_in_progress);
  1002. h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL_DURING_FLASH;
  1003. }
  1004. static void dial_up_lockup_detection_on_fw_flash_complete(struct ctlr_info *h,
  1005. struct CommandList *c)
  1006. {
  1007. if (is_firmware_flash_cmd(c->Request.CDB) &&
  1008. atomic_dec_and_test(&h->firmware_flash_in_progress))
  1009. h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
  1010. }
  1011. static void __enqueue_cmd_and_start_io(struct ctlr_info *h,
  1012. struct CommandList *c, int reply_queue)
  1013. {
  1014. dial_down_lockup_detection_during_fw_flash(h, c);
  1015. atomic_inc(&h->commands_outstanding);
  1016. switch (c->cmd_type) {
  1017. case CMD_IOACCEL1:
  1018. set_ioaccel1_performant_mode(h, c, reply_queue);
  1019. writel(c->busaddr, h->vaddr + SA5_REQUEST_PORT_OFFSET);
  1020. break;
  1021. case CMD_IOACCEL2:
  1022. set_ioaccel2_performant_mode(h, c, reply_queue);
  1023. writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
  1024. break;
  1025. case IOACCEL2_TMF:
  1026. set_ioaccel2_tmf_performant_mode(h, c, reply_queue);
  1027. writel(c->busaddr, h->vaddr + IOACCEL2_INBOUND_POSTQ_32);
  1028. break;
  1029. default:
  1030. set_performant_mode(h, c, reply_queue);
  1031. h->access.submit_command(h, c);
  1032. }
  1033. }
  1034. static void enqueue_cmd_and_start_io(struct ctlr_info *h, struct CommandList *c)
  1035. {
  1036. if (unlikely(hpsa_is_pending_event(c)))
  1037. return finish_cmd(c);
  1038. __enqueue_cmd_and_start_io(h, c, DEFAULT_REPLY_QUEUE);
  1039. }
  1040. static inline int is_hba_lunid(unsigned char scsi3addr[])
  1041. {
  1042. return memcmp(scsi3addr, RAID_CTLR_LUNID, 8) == 0;
  1043. }
  1044. static inline int is_scsi_rev_5(struct ctlr_info *h)
  1045. {
  1046. if (!h->hba_inquiry_data)
  1047. return 0;
  1048. if ((h->hba_inquiry_data[2] & 0x07) == 5)
  1049. return 1;
  1050. return 0;
  1051. }
  1052. static int hpsa_find_target_lun(struct ctlr_info *h,
  1053. unsigned char scsi3addr[], int bus, int *target, int *lun)
  1054. {
  1055. /* finds an unused bus, target, lun for a new physical device
  1056. * assumes h->devlock is held
  1057. */
  1058. int i, found = 0;
  1059. DECLARE_BITMAP(lun_taken, HPSA_MAX_DEVICES);
  1060. bitmap_zero(lun_taken, HPSA_MAX_DEVICES);
  1061. for (i = 0; i < h->ndevices; i++) {
  1062. if (h->dev[i]->bus == bus && h->dev[i]->target != -1)
  1063. __set_bit(h->dev[i]->target, lun_taken);
  1064. }
  1065. i = find_first_zero_bit(lun_taken, HPSA_MAX_DEVICES);
  1066. if (i < HPSA_MAX_DEVICES) {
  1067. /* *bus = 1; */
  1068. *target = i;
  1069. *lun = 0;
  1070. found = 1;
  1071. }
  1072. return !found;
  1073. }
  1074. static void hpsa_show_dev_msg(const char *level, struct ctlr_info *h,
  1075. struct hpsa_scsi_dev_t *dev, char *description)
  1076. {
  1077. #define LABEL_SIZE 25
  1078. char label[LABEL_SIZE];
  1079. if (h == NULL || h->pdev == NULL || h->scsi_host == NULL)
  1080. return;
  1081. switch (dev->devtype) {
  1082. case TYPE_RAID:
  1083. snprintf(label, LABEL_SIZE, "controller");
  1084. break;
  1085. case TYPE_ENCLOSURE:
  1086. snprintf(label, LABEL_SIZE, "enclosure");
  1087. break;
  1088. case TYPE_DISK:
  1089. case TYPE_ZBC:
  1090. if (dev->external)
  1091. snprintf(label, LABEL_SIZE, "external");
  1092. else if (!is_logical_dev_addr_mode(dev->scsi3addr))
  1093. snprintf(label, LABEL_SIZE, "%s",
  1094. raid_label[PHYSICAL_DRIVE]);
  1095. else
  1096. snprintf(label, LABEL_SIZE, "RAID-%s",
  1097. dev->raid_level > RAID_UNKNOWN ? "?" :
  1098. raid_label[dev->raid_level]);
  1099. break;
  1100. case TYPE_ROM:
  1101. snprintf(label, LABEL_SIZE, "rom");
  1102. break;
  1103. case TYPE_TAPE:
  1104. snprintf(label, LABEL_SIZE, "tape");
  1105. break;
  1106. case TYPE_MEDIUM_CHANGER:
  1107. snprintf(label, LABEL_SIZE, "changer");
  1108. break;
  1109. default:
  1110. snprintf(label, LABEL_SIZE, "UNKNOWN");
  1111. break;
  1112. }
  1113. dev_printk(level, &h->pdev->dev,
  1114. "scsi %d:%d:%d:%d: %s %s %.8s %.16s %s SSDSmartPathCap%c En%c Exp=%d\n",
  1115. h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
  1116. description,
  1117. scsi_device_type(dev->devtype),
  1118. dev->vendor,
  1119. dev->model,
  1120. label,
  1121. dev->offload_config ? '+' : '-',
  1122. dev->offload_enabled ? '+' : '-',
  1123. dev->expose_device);
  1124. }
  1125. /* Add an entry into h->dev[] array. */
  1126. static int hpsa_scsi_add_entry(struct ctlr_info *h,
  1127. struct hpsa_scsi_dev_t *device,
  1128. struct hpsa_scsi_dev_t *added[], int *nadded)
  1129. {
  1130. /* assumes h->devlock is held */
  1131. int n = h->ndevices;
  1132. int i;
  1133. unsigned char addr1[8], addr2[8];
  1134. struct hpsa_scsi_dev_t *sd;
  1135. if (n >= HPSA_MAX_DEVICES) {
  1136. dev_err(&h->pdev->dev, "too many devices, some will be "
  1137. "inaccessible.\n");
  1138. return -1;
  1139. }
  1140. /* physical devices do not have lun or target assigned until now. */
  1141. if (device->lun != -1)
  1142. /* Logical device, lun is already assigned. */
  1143. goto lun_assigned;
  1144. /* If this device a non-zero lun of a multi-lun device
  1145. * byte 4 of the 8-byte LUN addr will contain the logical
  1146. * unit no, zero otherwise.
  1147. */
  1148. if (device->scsi3addr[4] == 0) {
  1149. /* This is not a non-zero lun of a multi-lun device */
  1150. if (hpsa_find_target_lun(h, device->scsi3addr,
  1151. device->bus, &device->target, &device->lun) != 0)
  1152. return -1;
  1153. goto lun_assigned;
  1154. }
  1155. /* This is a non-zero lun of a multi-lun device.
  1156. * Search through our list and find the device which
  1157. * has the same 8 byte LUN address, excepting byte 4 and 5.
  1158. * Assign the same bus and target for this new LUN.
  1159. * Use the logical unit number from the firmware.
  1160. */
  1161. memcpy(addr1, device->scsi3addr, 8);
  1162. addr1[4] = 0;
  1163. addr1[5] = 0;
  1164. for (i = 0; i < n; i++) {
  1165. sd = h->dev[i];
  1166. memcpy(addr2, sd->scsi3addr, 8);
  1167. addr2[4] = 0;
  1168. addr2[5] = 0;
  1169. /* differ only in byte 4 and 5? */
  1170. if (memcmp(addr1, addr2, 8) == 0) {
  1171. device->bus = sd->bus;
  1172. device->target = sd->target;
  1173. device->lun = device->scsi3addr[4];
  1174. break;
  1175. }
  1176. }
  1177. if (device->lun == -1) {
  1178. dev_warn(&h->pdev->dev, "physical device with no LUN=0,"
  1179. " suspect firmware bug or unsupported hardware "
  1180. "configuration.\n");
  1181. return -1;
  1182. }
  1183. lun_assigned:
  1184. h->dev[n] = device;
  1185. h->ndevices++;
  1186. added[*nadded] = device;
  1187. (*nadded)++;
  1188. hpsa_show_dev_msg(KERN_INFO, h, device,
  1189. device->expose_device ? "added" : "masked");
  1190. device->offload_to_be_enabled = device->offload_enabled;
  1191. device->offload_enabled = 0;
  1192. return 0;
  1193. }
  1194. /* Update an entry in h->dev[] array. */
  1195. static void hpsa_scsi_update_entry(struct ctlr_info *h,
  1196. int entry, struct hpsa_scsi_dev_t *new_entry)
  1197. {
  1198. int offload_enabled;
  1199. /* assumes h->devlock is held */
  1200. BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
  1201. /* Raid level changed. */
  1202. h->dev[entry]->raid_level = new_entry->raid_level;
  1203. /* Raid offload parameters changed. Careful about the ordering. */
  1204. if (new_entry->offload_config && new_entry->offload_enabled) {
  1205. /*
  1206. * if drive is newly offload_enabled, we want to copy the
  1207. * raid map data first. If previously offload_enabled and
  1208. * offload_config were set, raid map data had better be
  1209. * the same as it was before. if raid map data is changed
  1210. * then it had better be the case that
  1211. * h->dev[entry]->offload_enabled is currently 0.
  1212. */
  1213. h->dev[entry]->raid_map = new_entry->raid_map;
  1214. h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
  1215. }
  1216. if (new_entry->hba_ioaccel_enabled) {
  1217. h->dev[entry]->ioaccel_handle = new_entry->ioaccel_handle;
  1218. wmb(); /* set ioaccel_handle *before* hba_ioaccel_enabled */
  1219. }
  1220. h->dev[entry]->hba_ioaccel_enabled = new_entry->hba_ioaccel_enabled;
  1221. h->dev[entry]->offload_config = new_entry->offload_config;
  1222. h->dev[entry]->offload_to_mirror = new_entry->offload_to_mirror;
  1223. h->dev[entry]->queue_depth = new_entry->queue_depth;
  1224. /*
  1225. * We can turn off ioaccel offload now, but need to delay turning
  1226. * it on until we can update h->dev[entry]->phys_disk[], but we
  1227. * can't do that until all the devices are updated.
  1228. */
  1229. h->dev[entry]->offload_to_be_enabled = new_entry->offload_enabled;
  1230. if (!new_entry->offload_enabled)
  1231. h->dev[entry]->offload_enabled = 0;
  1232. offload_enabled = h->dev[entry]->offload_enabled;
  1233. h->dev[entry]->offload_enabled = h->dev[entry]->offload_to_be_enabled;
  1234. hpsa_show_dev_msg(KERN_INFO, h, h->dev[entry], "updated");
  1235. h->dev[entry]->offload_enabled = offload_enabled;
  1236. }
  1237. /* Replace an entry from h->dev[] array. */
  1238. static void hpsa_scsi_replace_entry(struct ctlr_info *h,
  1239. int entry, struct hpsa_scsi_dev_t *new_entry,
  1240. struct hpsa_scsi_dev_t *added[], int *nadded,
  1241. struct hpsa_scsi_dev_t *removed[], int *nremoved)
  1242. {
  1243. /* assumes h->devlock is held */
  1244. BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
  1245. removed[*nremoved] = h->dev[entry];
  1246. (*nremoved)++;
  1247. /*
  1248. * New physical devices won't have target/lun assigned yet
  1249. * so we need to preserve the values in the slot we are replacing.
  1250. */
  1251. if (new_entry->target == -1) {
  1252. new_entry->target = h->dev[entry]->target;
  1253. new_entry->lun = h->dev[entry]->lun;
  1254. }
  1255. h->dev[entry] = new_entry;
  1256. added[*nadded] = new_entry;
  1257. (*nadded)++;
  1258. hpsa_show_dev_msg(KERN_INFO, h, new_entry, "replaced");
  1259. new_entry->offload_to_be_enabled = new_entry->offload_enabled;
  1260. new_entry->offload_enabled = 0;
  1261. }
  1262. /* Remove an entry from h->dev[] array. */
  1263. static void hpsa_scsi_remove_entry(struct ctlr_info *h, int entry,
  1264. struct hpsa_scsi_dev_t *removed[], int *nremoved)
  1265. {
  1266. /* assumes h->devlock is held */
  1267. int i;
  1268. struct hpsa_scsi_dev_t *sd;
  1269. BUG_ON(entry < 0 || entry >= HPSA_MAX_DEVICES);
  1270. sd = h->dev[entry];
  1271. removed[*nremoved] = h->dev[entry];
  1272. (*nremoved)++;
  1273. for (i = entry; i < h->ndevices-1; i++)
  1274. h->dev[i] = h->dev[i+1];
  1275. h->ndevices--;
  1276. hpsa_show_dev_msg(KERN_INFO, h, sd, "removed");
  1277. }
  1278. #define SCSI3ADDR_EQ(a, b) ( \
  1279. (a)[7] == (b)[7] && \
  1280. (a)[6] == (b)[6] && \
  1281. (a)[5] == (b)[5] && \
  1282. (a)[4] == (b)[4] && \
  1283. (a)[3] == (b)[3] && \
  1284. (a)[2] == (b)[2] && \
  1285. (a)[1] == (b)[1] && \
  1286. (a)[0] == (b)[0])
  1287. static void fixup_botched_add(struct ctlr_info *h,
  1288. struct hpsa_scsi_dev_t *added)
  1289. {
  1290. /* called when scsi_add_device fails in order to re-adjust
  1291. * h->dev[] to match the mid layer's view.
  1292. */
  1293. unsigned long flags;
  1294. int i, j;
  1295. spin_lock_irqsave(&h->lock, flags);
  1296. for (i = 0; i < h->ndevices; i++) {
  1297. if (h->dev[i] == added) {
  1298. for (j = i; j < h->ndevices-1; j++)
  1299. h->dev[j] = h->dev[j+1];
  1300. h->ndevices--;
  1301. break;
  1302. }
  1303. }
  1304. spin_unlock_irqrestore(&h->lock, flags);
  1305. kfree(added);
  1306. }
  1307. static inline int device_is_the_same(struct hpsa_scsi_dev_t *dev1,
  1308. struct hpsa_scsi_dev_t *dev2)
  1309. {
  1310. /* we compare everything except lun and target as these
  1311. * are not yet assigned. Compare parts likely
  1312. * to differ first
  1313. */
  1314. if (memcmp(dev1->scsi3addr, dev2->scsi3addr,
  1315. sizeof(dev1->scsi3addr)) != 0)
  1316. return 0;
  1317. if (memcmp(dev1->device_id, dev2->device_id,
  1318. sizeof(dev1->device_id)) != 0)
  1319. return 0;
  1320. if (memcmp(dev1->model, dev2->model, sizeof(dev1->model)) != 0)
  1321. return 0;
  1322. if (memcmp(dev1->vendor, dev2->vendor, sizeof(dev1->vendor)) != 0)
  1323. return 0;
  1324. if (dev1->devtype != dev2->devtype)
  1325. return 0;
  1326. if (dev1->bus != dev2->bus)
  1327. return 0;
  1328. return 1;
  1329. }
  1330. static inline int device_updated(struct hpsa_scsi_dev_t *dev1,
  1331. struct hpsa_scsi_dev_t *dev2)
  1332. {
  1333. /* Device attributes that can change, but don't mean
  1334. * that the device is a different device, nor that the OS
  1335. * needs to be told anything about the change.
  1336. */
  1337. if (dev1->raid_level != dev2->raid_level)
  1338. return 1;
  1339. if (dev1->offload_config != dev2->offload_config)
  1340. return 1;
  1341. if (dev1->offload_enabled != dev2->offload_enabled)
  1342. return 1;
  1343. if (!is_logical_dev_addr_mode(dev1->scsi3addr))
  1344. if (dev1->queue_depth != dev2->queue_depth)
  1345. return 1;
  1346. return 0;
  1347. }
  1348. /* Find needle in haystack. If exact match found, return DEVICE_SAME,
  1349. * and return needle location in *index. If scsi3addr matches, but not
  1350. * vendor, model, serial num, etc. return DEVICE_CHANGED, and return needle
  1351. * location in *index.
  1352. * In the case of a minor device attribute change, such as RAID level, just
  1353. * return DEVICE_UPDATED, along with the updated device's location in index.
  1354. * If needle not found, return DEVICE_NOT_FOUND.
  1355. */
  1356. static int hpsa_scsi_find_entry(struct hpsa_scsi_dev_t *needle,
  1357. struct hpsa_scsi_dev_t *haystack[], int haystack_size,
  1358. int *index)
  1359. {
  1360. int i;
  1361. #define DEVICE_NOT_FOUND 0
  1362. #define DEVICE_CHANGED 1
  1363. #define DEVICE_SAME 2
  1364. #define DEVICE_UPDATED 3
  1365. if (needle == NULL)
  1366. return DEVICE_NOT_FOUND;
  1367. for (i = 0; i < haystack_size; i++) {
  1368. if (haystack[i] == NULL) /* previously removed. */
  1369. continue;
  1370. if (SCSI3ADDR_EQ(needle->scsi3addr, haystack[i]->scsi3addr)) {
  1371. *index = i;
  1372. if (device_is_the_same(needle, haystack[i])) {
  1373. if (device_updated(needle, haystack[i]))
  1374. return DEVICE_UPDATED;
  1375. return DEVICE_SAME;
  1376. } else {
  1377. /* Keep offline devices offline */
  1378. if (needle->volume_offline)
  1379. return DEVICE_NOT_FOUND;
  1380. return DEVICE_CHANGED;
  1381. }
  1382. }
  1383. }
  1384. *index = -1;
  1385. return DEVICE_NOT_FOUND;
  1386. }
  1387. static void hpsa_monitor_offline_device(struct ctlr_info *h,
  1388. unsigned char scsi3addr[])
  1389. {
  1390. struct offline_device_entry *device;
  1391. unsigned long flags;
  1392. /* Check to see if device is already on the list */
  1393. spin_lock_irqsave(&h->offline_device_lock, flags);
  1394. list_for_each_entry(device, &h->offline_device_list, offline_list) {
  1395. if (memcmp(device->scsi3addr, scsi3addr,
  1396. sizeof(device->scsi3addr)) == 0) {
  1397. spin_unlock_irqrestore(&h->offline_device_lock, flags);
  1398. return;
  1399. }
  1400. }
  1401. spin_unlock_irqrestore(&h->offline_device_lock, flags);
  1402. /* Device is not on the list, add it. */
  1403. device = kmalloc(sizeof(*device), GFP_KERNEL);
  1404. if (!device) {
  1405. dev_warn(&h->pdev->dev, "out of memory in %s\n", __func__);
  1406. return;
  1407. }
  1408. memcpy(device->scsi3addr, scsi3addr, sizeof(device->scsi3addr));
  1409. spin_lock_irqsave(&h->offline_device_lock, flags);
  1410. list_add_tail(&device->offline_list, &h->offline_device_list);
  1411. spin_unlock_irqrestore(&h->offline_device_lock, flags);
  1412. }
  1413. /* Print a message explaining various offline volume states */
  1414. static void hpsa_show_volume_status(struct ctlr_info *h,
  1415. struct hpsa_scsi_dev_t *sd)
  1416. {
  1417. if (sd->volume_offline == HPSA_VPD_LV_STATUS_UNSUPPORTED)
  1418. dev_info(&h->pdev->dev,
  1419. "C%d:B%d:T%d:L%d Volume status is not available through vital product data pages.\n",
  1420. h->scsi_host->host_no,
  1421. sd->bus, sd->target, sd->lun);
  1422. switch (sd->volume_offline) {
  1423. case HPSA_LV_OK:
  1424. break;
  1425. case HPSA_LV_UNDERGOING_ERASE:
  1426. dev_info(&h->pdev->dev,
  1427. "C%d:B%d:T%d:L%d Volume is undergoing background erase process.\n",
  1428. h->scsi_host->host_no,
  1429. sd->bus, sd->target, sd->lun);
  1430. break;
  1431. case HPSA_LV_NOT_AVAILABLE:
  1432. dev_info(&h->pdev->dev,
  1433. "C%d:B%d:T%d:L%d Volume is waiting for transforming volume.\n",
  1434. h->scsi_host->host_no,
  1435. sd->bus, sd->target, sd->lun);
  1436. break;
  1437. case HPSA_LV_UNDERGOING_RPI:
  1438. dev_info(&h->pdev->dev,
  1439. "C%d:B%d:T%d:L%d Volume is undergoing rapid parity init.\n",
  1440. h->scsi_host->host_no,
  1441. sd->bus, sd->target, sd->lun);
  1442. break;
  1443. case HPSA_LV_PENDING_RPI:
  1444. dev_info(&h->pdev->dev,
  1445. "C%d:B%d:T%d:L%d Volume is queued for rapid parity initialization process.\n",
  1446. h->scsi_host->host_no,
  1447. sd->bus, sd->target, sd->lun);
  1448. break;
  1449. case HPSA_LV_ENCRYPTED_NO_KEY:
  1450. dev_info(&h->pdev->dev,
  1451. "C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because key is not present.\n",
  1452. h->scsi_host->host_no,
  1453. sd->bus, sd->target, sd->lun);
  1454. break;
  1455. case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
  1456. dev_info(&h->pdev->dev,
  1457. "C%d:B%d:T%d:L%d Volume is not encrypted and cannot be accessed because controller is in encryption-only mode.\n",
  1458. h->scsi_host->host_no,
  1459. sd->bus, sd->target, sd->lun);
  1460. break;
  1461. case HPSA_LV_UNDERGOING_ENCRYPTION:
  1462. dev_info(&h->pdev->dev,
  1463. "C%d:B%d:T%d:L%d Volume is undergoing encryption process.\n",
  1464. h->scsi_host->host_no,
  1465. sd->bus, sd->target, sd->lun);
  1466. break;
  1467. case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
  1468. dev_info(&h->pdev->dev,
  1469. "C%d:B%d:T%d:L%d Volume is undergoing encryption re-keying process.\n",
  1470. h->scsi_host->host_no,
  1471. sd->bus, sd->target, sd->lun);
  1472. break;
  1473. case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
  1474. dev_info(&h->pdev->dev,
  1475. "C%d:B%d:T%d:L%d Volume is encrypted and cannot be accessed because controller does not have encryption enabled.\n",
  1476. h->scsi_host->host_no,
  1477. sd->bus, sd->target, sd->lun);
  1478. break;
  1479. case HPSA_LV_PENDING_ENCRYPTION:
  1480. dev_info(&h->pdev->dev,
  1481. "C%d:B%d:T%d:L%d Volume is pending migration to encrypted state, but process has not started.\n",
  1482. h->scsi_host->host_no,
  1483. sd->bus, sd->target, sd->lun);
  1484. break;
  1485. case HPSA_LV_PENDING_ENCRYPTION_REKEYING:
  1486. dev_info(&h->pdev->dev,
  1487. "C%d:B%d:T%d:L%d Volume is encrypted and is pending encryption rekeying.\n",
  1488. h->scsi_host->host_no,
  1489. sd->bus, sd->target, sd->lun);
  1490. break;
  1491. }
  1492. }
  1493. /*
  1494. * Figure the list of physical drive pointers for a logical drive with
  1495. * raid offload configured.
  1496. */
  1497. static void hpsa_figure_phys_disk_ptrs(struct ctlr_info *h,
  1498. struct hpsa_scsi_dev_t *dev[], int ndevices,
  1499. struct hpsa_scsi_dev_t *logical_drive)
  1500. {
  1501. struct raid_map_data *map = &logical_drive->raid_map;
  1502. struct raid_map_disk_data *dd = &map->data[0];
  1503. int i, j;
  1504. int total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
  1505. le16_to_cpu(map->metadata_disks_per_row);
  1506. int nraid_map_entries = le16_to_cpu(map->row_cnt) *
  1507. le16_to_cpu(map->layout_map_count) *
  1508. total_disks_per_row;
  1509. int nphys_disk = le16_to_cpu(map->layout_map_count) *
  1510. total_disks_per_row;
  1511. int qdepth;
  1512. if (nraid_map_entries > RAID_MAP_MAX_ENTRIES)
  1513. nraid_map_entries = RAID_MAP_MAX_ENTRIES;
  1514. logical_drive->nphysical_disks = nraid_map_entries;
  1515. qdepth = 0;
  1516. for (i = 0; i < nraid_map_entries; i++) {
  1517. logical_drive->phys_disk[i] = NULL;
  1518. if (!logical_drive->offload_config)
  1519. continue;
  1520. for (j = 0; j < ndevices; j++) {
  1521. if (dev[j] == NULL)
  1522. continue;
  1523. if (dev[j]->devtype != TYPE_DISK &&
  1524. dev[j]->devtype != TYPE_ZBC)
  1525. continue;
  1526. if (is_logical_device(dev[j]))
  1527. continue;
  1528. if (dev[j]->ioaccel_handle != dd[i].ioaccel_handle)
  1529. continue;
  1530. logical_drive->phys_disk[i] = dev[j];
  1531. if (i < nphys_disk)
  1532. qdepth = min(h->nr_cmds, qdepth +
  1533. logical_drive->phys_disk[i]->queue_depth);
  1534. break;
  1535. }
  1536. /*
  1537. * This can happen if a physical drive is removed and
  1538. * the logical drive is degraded. In that case, the RAID
  1539. * map data will refer to a physical disk which isn't actually
  1540. * present. And in that case offload_enabled should already
  1541. * be 0, but we'll turn it off here just in case
  1542. */
  1543. if (!logical_drive->phys_disk[i]) {
  1544. logical_drive->offload_enabled = 0;
  1545. logical_drive->offload_to_be_enabled = 0;
  1546. logical_drive->queue_depth = 8;
  1547. }
  1548. }
  1549. if (nraid_map_entries)
  1550. /*
  1551. * This is correct for reads, too high for full stripe writes,
  1552. * way too high for partial stripe writes
  1553. */
  1554. logical_drive->queue_depth = qdepth;
  1555. else
  1556. logical_drive->queue_depth = h->nr_cmds;
  1557. }
  1558. static void hpsa_update_log_drive_phys_drive_ptrs(struct ctlr_info *h,
  1559. struct hpsa_scsi_dev_t *dev[], int ndevices)
  1560. {
  1561. int i;
  1562. for (i = 0; i < ndevices; i++) {
  1563. if (dev[i] == NULL)
  1564. continue;
  1565. if (dev[i]->devtype != TYPE_DISK &&
  1566. dev[i]->devtype != TYPE_ZBC)
  1567. continue;
  1568. if (!is_logical_device(dev[i]))
  1569. continue;
  1570. /*
  1571. * If offload is currently enabled, the RAID map and
  1572. * phys_disk[] assignment *better* not be changing
  1573. * and since it isn't changing, we do not need to
  1574. * update it.
  1575. */
  1576. if (dev[i]->offload_enabled)
  1577. continue;
  1578. hpsa_figure_phys_disk_ptrs(h, dev, ndevices, dev[i]);
  1579. }
  1580. }
  1581. static int hpsa_add_device(struct ctlr_info *h, struct hpsa_scsi_dev_t *device)
  1582. {
  1583. int rc = 0;
  1584. if (!h->scsi_host)
  1585. return 1;
  1586. if (is_logical_device(device)) /* RAID */
  1587. rc = scsi_add_device(h->scsi_host, device->bus,
  1588. device->target, device->lun);
  1589. else /* HBA */
  1590. rc = hpsa_add_sas_device(h->sas_host, device);
  1591. return rc;
  1592. }
  1593. static int hpsa_find_outstanding_commands_for_dev(struct ctlr_info *h,
  1594. struct hpsa_scsi_dev_t *dev)
  1595. {
  1596. int i;
  1597. int count = 0;
  1598. for (i = 0; i < h->nr_cmds; i++) {
  1599. struct CommandList *c = h->cmd_pool + i;
  1600. int refcount = atomic_inc_return(&c->refcount);
  1601. if (refcount > 1 && hpsa_cmd_dev_match(h, c, dev,
  1602. dev->scsi3addr)) {
  1603. unsigned long flags;
  1604. spin_lock_irqsave(&h->lock, flags); /* Implied MB */
  1605. if (!hpsa_is_cmd_idle(c))
  1606. ++count;
  1607. spin_unlock_irqrestore(&h->lock, flags);
  1608. }
  1609. cmd_free(h, c);
  1610. }
  1611. return count;
  1612. }
  1613. static void hpsa_wait_for_outstanding_commands_for_dev(struct ctlr_info *h,
  1614. struct hpsa_scsi_dev_t *device)
  1615. {
  1616. int cmds = 0;
  1617. int waits = 0;
  1618. while (1) {
  1619. cmds = hpsa_find_outstanding_commands_for_dev(h, device);
  1620. if (cmds == 0)
  1621. break;
  1622. if (++waits > 20)
  1623. break;
  1624. dev_warn(&h->pdev->dev,
  1625. "%s: removing device with %d outstanding commands!\n",
  1626. __func__, cmds);
  1627. msleep(1000);
  1628. }
  1629. }
  1630. static void hpsa_remove_device(struct ctlr_info *h,
  1631. struct hpsa_scsi_dev_t *device)
  1632. {
  1633. struct scsi_device *sdev = NULL;
  1634. if (!h->scsi_host)
  1635. return;
  1636. if (is_logical_device(device)) { /* RAID */
  1637. sdev = scsi_device_lookup(h->scsi_host, device->bus,
  1638. device->target, device->lun);
  1639. if (sdev) {
  1640. scsi_remove_device(sdev);
  1641. scsi_device_put(sdev);
  1642. } else {
  1643. /*
  1644. * We don't expect to get here. Future commands
  1645. * to this device will get a selection timeout as
  1646. * if the device were gone.
  1647. */
  1648. hpsa_show_dev_msg(KERN_WARNING, h, device,
  1649. "didn't find device for removal.");
  1650. }
  1651. } else { /* HBA */
  1652. device->removed = 1;
  1653. hpsa_wait_for_outstanding_commands_for_dev(h, device);
  1654. hpsa_remove_sas_device(device);
  1655. }
  1656. }
  1657. static void adjust_hpsa_scsi_table(struct ctlr_info *h,
  1658. struct hpsa_scsi_dev_t *sd[], int nsds)
  1659. {
  1660. /* sd contains scsi3 addresses and devtypes, and inquiry
  1661. * data. This function takes what's in sd to be the current
  1662. * reality and updates h->dev[] to reflect that reality.
  1663. */
  1664. int i, entry, device_change, changes = 0;
  1665. struct hpsa_scsi_dev_t *csd;
  1666. unsigned long flags;
  1667. struct hpsa_scsi_dev_t **added, **removed;
  1668. int nadded, nremoved;
  1669. /*
  1670. * A reset can cause a device status to change
  1671. * re-schedule the scan to see what happened.
  1672. */
  1673. if (h->reset_in_progress) {
  1674. h->drv_req_rescan = 1;
  1675. return;
  1676. }
  1677. added = kzalloc(sizeof(*added) * HPSA_MAX_DEVICES, GFP_KERNEL);
  1678. removed = kzalloc(sizeof(*removed) * HPSA_MAX_DEVICES, GFP_KERNEL);
  1679. if (!added || !removed) {
  1680. dev_warn(&h->pdev->dev, "out of memory in "
  1681. "adjust_hpsa_scsi_table\n");
  1682. goto free_and_out;
  1683. }
  1684. spin_lock_irqsave(&h->devlock, flags);
  1685. /* find any devices in h->dev[] that are not in
  1686. * sd[] and remove them from h->dev[], and for any
  1687. * devices which have changed, remove the old device
  1688. * info and add the new device info.
  1689. * If minor device attributes change, just update
  1690. * the existing device structure.
  1691. */
  1692. i = 0;
  1693. nremoved = 0;
  1694. nadded = 0;
  1695. while (i < h->ndevices) {
  1696. csd = h->dev[i];
  1697. device_change = hpsa_scsi_find_entry(csd, sd, nsds, &entry);
  1698. if (device_change == DEVICE_NOT_FOUND) {
  1699. changes++;
  1700. hpsa_scsi_remove_entry(h, i, removed, &nremoved);
  1701. continue; /* remove ^^^, hence i not incremented */
  1702. } else if (device_change == DEVICE_CHANGED) {
  1703. changes++;
  1704. hpsa_scsi_replace_entry(h, i, sd[entry],
  1705. added, &nadded, removed, &nremoved);
  1706. /* Set it to NULL to prevent it from being freed
  1707. * at the bottom of hpsa_update_scsi_devices()
  1708. */
  1709. sd[entry] = NULL;
  1710. } else if (device_change == DEVICE_UPDATED) {
  1711. hpsa_scsi_update_entry(h, i, sd[entry]);
  1712. }
  1713. i++;
  1714. }
  1715. /* Now, make sure every device listed in sd[] is also
  1716. * listed in h->dev[], adding them if they aren't found
  1717. */
  1718. for (i = 0; i < nsds; i++) {
  1719. if (!sd[i]) /* if already added above. */
  1720. continue;
  1721. /* Don't add devices which are NOT READY, FORMAT IN PROGRESS
  1722. * as the SCSI mid-layer does not handle such devices well.
  1723. * It relentlessly loops sending TUR at 3Hz, then READ(10)
  1724. * at 160Hz, and prevents the system from coming up.
  1725. */
  1726. if (sd[i]->volume_offline) {
  1727. hpsa_show_volume_status(h, sd[i]);
  1728. hpsa_show_dev_msg(KERN_INFO, h, sd[i], "offline");
  1729. continue;
  1730. }
  1731. device_change = hpsa_scsi_find_entry(sd[i], h->dev,
  1732. h->ndevices, &entry);
  1733. if (device_change == DEVICE_NOT_FOUND) {
  1734. changes++;
  1735. if (hpsa_scsi_add_entry(h, sd[i], added, &nadded) != 0)
  1736. break;
  1737. sd[i] = NULL; /* prevent from being freed later. */
  1738. } else if (device_change == DEVICE_CHANGED) {
  1739. /* should never happen... */
  1740. changes++;
  1741. dev_warn(&h->pdev->dev,
  1742. "device unexpectedly changed.\n");
  1743. /* but if it does happen, we just ignore that device */
  1744. }
  1745. }
  1746. hpsa_update_log_drive_phys_drive_ptrs(h, h->dev, h->ndevices);
  1747. /* Now that h->dev[]->phys_disk[] is coherent, we can enable
  1748. * any logical drives that need it enabled.
  1749. */
  1750. for (i = 0; i < h->ndevices; i++) {
  1751. if (h->dev[i] == NULL)
  1752. continue;
  1753. h->dev[i]->offload_enabled = h->dev[i]->offload_to_be_enabled;
  1754. }
  1755. spin_unlock_irqrestore(&h->devlock, flags);
  1756. /* Monitor devices which are in one of several NOT READY states to be
  1757. * brought online later. This must be done without holding h->devlock,
  1758. * so don't touch h->dev[]
  1759. */
  1760. for (i = 0; i < nsds; i++) {
  1761. if (!sd[i]) /* if already added above. */
  1762. continue;
  1763. if (sd[i]->volume_offline)
  1764. hpsa_monitor_offline_device(h, sd[i]->scsi3addr);
  1765. }
  1766. /* Don't notify scsi mid layer of any changes the first time through
  1767. * (or if there are no changes) scsi_scan_host will do it later the
  1768. * first time through.
  1769. */
  1770. if (!changes)
  1771. goto free_and_out;
  1772. /* Notify scsi mid layer of any removed devices */
  1773. for (i = 0; i < nremoved; i++) {
  1774. if (removed[i] == NULL)
  1775. continue;
  1776. if (removed[i]->expose_device)
  1777. hpsa_remove_device(h, removed[i]);
  1778. kfree(removed[i]);
  1779. removed[i] = NULL;
  1780. }
  1781. /* Notify scsi mid layer of any added devices */
  1782. for (i = 0; i < nadded; i++) {
  1783. int rc = 0;
  1784. if (added[i] == NULL)
  1785. continue;
  1786. if (!(added[i]->expose_device))
  1787. continue;
  1788. rc = hpsa_add_device(h, added[i]);
  1789. if (!rc)
  1790. continue;
  1791. dev_warn(&h->pdev->dev,
  1792. "addition failed %d, device not added.", rc);
  1793. /* now we have to remove it from h->dev,
  1794. * since it didn't get added to scsi mid layer
  1795. */
  1796. fixup_botched_add(h, added[i]);
  1797. h->drv_req_rescan = 1;
  1798. }
  1799. free_and_out:
  1800. kfree(added);
  1801. kfree(removed);
  1802. }
  1803. /*
  1804. * Lookup bus/target/lun and return corresponding struct hpsa_scsi_dev_t *
  1805. * Assume's h->devlock is held.
  1806. */
  1807. static struct hpsa_scsi_dev_t *lookup_hpsa_scsi_dev(struct ctlr_info *h,
  1808. int bus, int target, int lun)
  1809. {
  1810. int i;
  1811. struct hpsa_scsi_dev_t *sd;
  1812. for (i = 0; i < h->ndevices; i++) {
  1813. sd = h->dev[i];
  1814. if (sd->bus == bus && sd->target == target && sd->lun == lun)
  1815. return sd;
  1816. }
  1817. return NULL;
  1818. }
  1819. static int hpsa_slave_alloc(struct scsi_device *sdev)
  1820. {
  1821. struct hpsa_scsi_dev_t *sd;
  1822. unsigned long flags;
  1823. struct ctlr_info *h;
  1824. h = sdev_to_hba(sdev);
  1825. spin_lock_irqsave(&h->devlock, flags);
  1826. if (sdev_channel(sdev) == HPSA_PHYSICAL_DEVICE_BUS) {
  1827. struct scsi_target *starget;
  1828. struct sas_rphy *rphy;
  1829. starget = scsi_target(sdev);
  1830. rphy = target_to_rphy(starget);
  1831. sd = hpsa_find_device_by_sas_rphy(h, rphy);
  1832. if (sd) {
  1833. sd->target = sdev_id(sdev);
  1834. sd->lun = sdev->lun;
  1835. }
  1836. } else
  1837. sd = lookup_hpsa_scsi_dev(h, sdev_channel(sdev),
  1838. sdev_id(sdev), sdev->lun);
  1839. if (sd && sd->expose_device) {
  1840. atomic_set(&sd->ioaccel_cmds_out, 0);
  1841. sdev->hostdata = sd;
  1842. } else
  1843. sdev->hostdata = NULL;
  1844. spin_unlock_irqrestore(&h->devlock, flags);
  1845. return 0;
  1846. }
  1847. /* configure scsi device based on internal per-device structure */
  1848. static int hpsa_slave_configure(struct scsi_device *sdev)
  1849. {
  1850. struct hpsa_scsi_dev_t *sd;
  1851. int queue_depth;
  1852. sd = sdev->hostdata;
  1853. sdev->no_uld_attach = !sd || !sd->expose_device;
  1854. if (sd)
  1855. queue_depth = sd->queue_depth != 0 ?
  1856. sd->queue_depth : sdev->host->can_queue;
  1857. else
  1858. queue_depth = sdev->host->can_queue;
  1859. scsi_change_queue_depth(sdev, queue_depth);
  1860. return 0;
  1861. }
  1862. static void hpsa_slave_destroy(struct scsi_device *sdev)
  1863. {
  1864. /* nothing to do. */
  1865. }
  1866. static void hpsa_free_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
  1867. {
  1868. int i;
  1869. if (!h->ioaccel2_cmd_sg_list)
  1870. return;
  1871. for (i = 0; i < h->nr_cmds; i++) {
  1872. kfree(h->ioaccel2_cmd_sg_list[i]);
  1873. h->ioaccel2_cmd_sg_list[i] = NULL;
  1874. }
  1875. kfree(h->ioaccel2_cmd_sg_list);
  1876. h->ioaccel2_cmd_sg_list = NULL;
  1877. }
  1878. static int hpsa_allocate_ioaccel2_sg_chain_blocks(struct ctlr_info *h)
  1879. {
  1880. int i;
  1881. if (h->chainsize <= 0)
  1882. return 0;
  1883. h->ioaccel2_cmd_sg_list =
  1884. kzalloc(sizeof(*h->ioaccel2_cmd_sg_list) * h->nr_cmds,
  1885. GFP_KERNEL);
  1886. if (!h->ioaccel2_cmd_sg_list)
  1887. return -ENOMEM;
  1888. for (i = 0; i < h->nr_cmds; i++) {
  1889. h->ioaccel2_cmd_sg_list[i] =
  1890. kmalloc(sizeof(*h->ioaccel2_cmd_sg_list[i]) *
  1891. h->maxsgentries, GFP_KERNEL);
  1892. if (!h->ioaccel2_cmd_sg_list[i])
  1893. goto clean;
  1894. }
  1895. return 0;
  1896. clean:
  1897. hpsa_free_ioaccel2_sg_chain_blocks(h);
  1898. return -ENOMEM;
  1899. }
  1900. static void hpsa_free_sg_chain_blocks(struct ctlr_info *h)
  1901. {
  1902. int i;
  1903. if (!h->cmd_sg_list)
  1904. return;
  1905. for (i = 0; i < h->nr_cmds; i++) {
  1906. kfree(h->cmd_sg_list[i]);
  1907. h->cmd_sg_list[i] = NULL;
  1908. }
  1909. kfree(h->cmd_sg_list);
  1910. h->cmd_sg_list = NULL;
  1911. }
  1912. static int hpsa_alloc_sg_chain_blocks(struct ctlr_info *h)
  1913. {
  1914. int i;
  1915. if (h->chainsize <= 0)
  1916. return 0;
  1917. h->cmd_sg_list = kzalloc(sizeof(*h->cmd_sg_list) * h->nr_cmds,
  1918. GFP_KERNEL);
  1919. if (!h->cmd_sg_list) {
  1920. dev_err(&h->pdev->dev, "Failed to allocate SG list\n");
  1921. return -ENOMEM;
  1922. }
  1923. for (i = 0; i < h->nr_cmds; i++) {
  1924. h->cmd_sg_list[i] = kmalloc(sizeof(*h->cmd_sg_list[i]) *
  1925. h->chainsize, GFP_KERNEL);
  1926. if (!h->cmd_sg_list[i]) {
  1927. dev_err(&h->pdev->dev, "Failed to allocate cmd SG\n");
  1928. goto clean;
  1929. }
  1930. }
  1931. return 0;
  1932. clean:
  1933. hpsa_free_sg_chain_blocks(h);
  1934. return -ENOMEM;
  1935. }
  1936. static int hpsa_map_ioaccel2_sg_chain_block(struct ctlr_info *h,
  1937. struct io_accel2_cmd *cp, struct CommandList *c)
  1938. {
  1939. struct ioaccel2_sg_element *chain_block;
  1940. u64 temp64;
  1941. u32 chain_size;
  1942. chain_block = h->ioaccel2_cmd_sg_list[c->cmdindex];
  1943. chain_size = le32_to_cpu(cp->sg[0].length);
  1944. temp64 = pci_map_single(h->pdev, chain_block, chain_size,
  1945. PCI_DMA_TODEVICE);
  1946. if (dma_mapping_error(&h->pdev->dev, temp64)) {
  1947. /* prevent subsequent unmapping */
  1948. cp->sg->address = 0;
  1949. return -1;
  1950. }
  1951. cp->sg->address = cpu_to_le64(temp64);
  1952. return 0;
  1953. }
  1954. static void hpsa_unmap_ioaccel2_sg_chain_block(struct ctlr_info *h,
  1955. struct io_accel2_cmd *cp)
  1956. {
  1957. struct ioaccel2_sg_element *chain_sg;
  1958. u64 temp64;
  1959. u32 chain_size;
  1960. chain_sg = cp->sg;
  1961. temp64 = le64_to_cpu(chain_sg->address);
  1962. chain_size = le32_to_cpu(cp->sg[0].length);
  1963. pci_unmap_single(h->pdev, temp64, chain_size, PCI_DMA_TODEVICE);
  1964. }
  1965. static int hpsa_map_sg_chain_block(struct ctlr_info *h,
  1966. struct CommandList *c)
  1967. {
  1968. struct SGDescriptor *chain_sg, *chain_block;
  1969. u64 temp64;
  1970. u32 chain_len;
  1971. chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
  1972. chain_block = h->cmd_sg_list[c->cmdindex];
  1973. chain_sg->Ext = cpu_to_le32(HPSA_SG_CHAIN);
  1974. chain_len = sizeof(*chain_sg) *
  1975. (le16_to_cpu(c->Header.SGTotal) - h->max_cmd_sg_entries);
  1976. chain_sg->Len = cpu_to_le32(chain_len);
  1977. temp64 = pci_map_single(h->pdev, chain_block, chain_len,
  1978. PCI_DMA_TODEVICE);
  1979. if (dma_mapping_error(&h->pdev->dev, temp64)) {
  1980. /* prevent subsequent unmapping */
  1981. chain_sg->Addr = cpu_to_le64(0);
  1982. return -1;
  1983. }
  1984. chain_sg->Addr = cpu_to_le64(temp64);
  1985. return 0;
  1986. }
  1987. static void hpsa_unmap_sg_chain_block(struct ctlr_info *h,
  1988. struct CommandList *c)
  1989. {
  1990. struct SGDescriptor *chain_sg;
  1991. if (le16_to_cpu(c->Header.SGTotal) <= h->max_cmd_sg_entries)
  1992. return;
  1993. chain_sg = &c->SG[h->max_cmd_sg_entries - 1];
  1994. pci_unmap_single(h->pdev, le64_to_cpu(chain_sg->Addr),
  1995. le32_to_cpu(chain_sg->Len), PCI_DMA_TODEVICE);
  1996. }
  1997. /* Decode the various types of errors on ioaccel2 path.
  1998. * Return 1 for any error that should generate a RAID path retry.
  1999. * Return 0 for errors that don't require a RAID path retry.
  2000. */
  2001. static int handle_ioaccel_mode2_error(struct ctlr_info *h,
  2002. struct CommandList *c,
  2003. struct scsi_cmnd *cmd,
  2004. struct io_accel2_cmd *c2,
  2005. struct hpsa_scsi_dev_t *dev)
  2006. {
  2007. int data_len;
  2008. int retry = 0;
  2009. u32 ioaccel2_resid = 0;
  2010. switch (c2->error_data.serv_response) {
  2011. case IOACCEL2_SERV_RESPONSE_COMPLETE:
  2012. switch (c2->error_data.status) {
  2013. case IOACCEL2_STATUS_SR_TASK_COMP_GOOD:
  2014. break;
  2015. case IOACCEL2_STATUS_SR_TASK_COMP_CHK_COND:
  2016. cmd->result |= SAM_STAT_CHECK_CONDITION;
  2017. if (c2->error_data.data_present !=
  2018. IOACCEL2_SENSE_DATA_PRESENT) {
  2019. memset(cmd->sense_buffer, 0,
  2020. SCSI_SENSE_BUFFERSIZE);
  2021. break;
  2022. }
  2023. /* copy the sense data */
  2024. data_len = c2->error_data.sense_data_len;
  2025. if (data_len > SCSI_SENSE_BUFFERSIZE)
  2026. data_len = SCSI_SENSE_BUFFERSIZE;
  2027. if (data_len > sizeof(c2->error_data.sense_data_buff))
  2028. data_len =
  2029. sizeof(c2->error_data.sense_data_buff);
  2030. memcpy(cmd->sense_buffer,
  2031. c2->error_data.sense_data_buff, data_len);
  2032. retry = 1;
  2033. break;
  2034. case IOACCEL2_STATUS_SR_TASK_COMP_BUSY:
  2035. retry = 1;
  2036. break;
  2037. case IOACCEL2_STATUS_SR_TASK_COMP_RES_CON:
  2038. retry = 1;
  2039. break;
  2040. case IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL:
  2041. retry = 1;
  2042. break;
  2043. case IOACCEL2_STATUS_SR_TASK_COMP_ABORTED:
  2044. retry = 1;
  2045. break;
  2046. default:
  2047. retry = 1;
  2048. break;
  2049. }
  2050. break;
  2051. case IOACCEL2_SERV_RESPONSE_FAILURE:
  2052. switch (c2->error_data.status) {
  2053. case IOACCEL2_STATUS_SR_IO_ERROR:
  2054. case IOACCEL2_STATUS_SR_IO_ABORTED:
  2055. case IOACCEL2_STATUS_SR_OVERRUN:
  2056. retry = 1;
  2057. break;
  2058. case IOACCEL2_STATUS_SR_UNDERRUN:
  2059. cmd->result = (DID_OK << 16); /* host byte */
  2060. cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
  2061. ioaccel2_resid = get_unaligned_le32(
  2062. &c2->error_data.resid_cnt[0]);
  2063. scsi_set_resid(cmd, ioaccel2_resid);
  2064. break;
  2065. case IOACCEL2_STATUS_SR_NO_PATH_TO_DEVICE:
  2066. case IOACCEL2_STATUS_SR_INVALID_DEVICE:
  2067. case IOACCEL2_STATUS_SR_IOACCEL_DISABLED:
  2068. /*
  2069. * Did an HBA disk disappear? We will eventually
  2070. * get a state change event from the controller but
  2071. * in the meantime, we need to tell the OS that the
  2072. * HBA disk is no longer there and stop I/O
  2073. * from going down. This allows the potential re-insert
  2074. * of the disk to get the same device node.
  2075. */
  2076. if (dev->physical_device && dev->expose_device) {
  2077. cmd->result = DID_NO_CONNECT << 16;
  2078. dev->removed = 1;
  2079. h->drv_req_rescan = 1;
  2080. dev_warn(&h->pdev->dev,
  2081. "%s: device is gone!\n", __func__);
  2082. } else
  2083. /*
  2084. * Retry by sending down the RAID path.
  2085. * We will get an event from ctlr to
  2086. * trigger rescan regardless.
  2087. */
  2088. retry = 1;
  2089. break;
  2090. default:
  2091. retry = 1;
  2092. }
  2093. break;
  2094. case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
  2095. break;
  2096. case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
  2097. break;
  2098. case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
  2099. retry = 1;
  2100. break;
  2101. case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
  2102. break;
  2103. default:
  2104. retry = 1;
  2105. break;
  2106. }
  2107. return retry; /* retry on raid path? */
  2108. }
  2109. static void hpsa_cmd_resolve_events(struct ctlr_info *h,
  2110. struct CommandList *c)
  2111. {
  2112. bool do_wake = false;
  2113. /*
  2114. * Prevent the following race in the abort handler:
  2115. *
  2116. * 1. LLD is requested to abort a SCSI command
  2117. * 2. The SCSI command completes
  2118. * 3. The struct CommandList associated with step 2 is made available
  2119. * 4. New I/O request to LLD to another LUN re-uses struct CommandList
  2120. * 5. Abort handler follows scsi_cmnd->host_scribble and
  2121. * finds struct CommandList and tries to aborts it
  2122. * Now we have aborted the wrong command.
  2123. *
  2124. * Reset c->scsi_cmd here so that the abort or reset handler will know
  2125. * this command has completed. Then, check to see if the handler is
  2126. * waiting for this command, and, if so, wake it.
  2127. */
  2128. c->scsi_cmd = SCSI_CMD_IDLE;
  2129. mb(); /* Declare command idle before checking for pending events. */
  2130. if (c->abort_pending) {
  2131. do_wake = true;
  2132. c->abort_pending = false;
  2133. }
  2134. if (c->reset_pending) {
  2135. unsigned long flags;
  2136. struct hpsa_scsi_dev_t *dev;
  2137. /*
  2138. * There appears to be a reset pending; lock the lock and
  2139. * reconfirm. If so, then decrement the count of outstanding
  2140. * commands and wake the reset command if this is the last one.
  2141. */
  2142. spin_lock_irqsave(&h->lock, flags);
  2143. dev = c->reset_pending; /* Re-fetch under the lock. */
  2144. if (dev && atomic_dec_and_test(&dev->reset_cmds_out))
  2145. do_wake = true;
  2146. c->reset_pending = NULL;
  2147. spin_unlock_irqrestore(&h->lock, flags);
  2148. }
  2149. if (do_wake)
  2150. wake_up_all(&h->event_sync_wait_queue);
  2151. }
  2152. static void hpsa_cmd_resolve_and_free(struct ctlr_info *h,
  2153. struct CommandList *c)
  2154. {
  2155. hpsa_cmd_resolve_events(h, c);
  2156. cmd_tagged_free(h, c);
  2157. }
  2158. static void hpsa_cmd_free_and_done(struct ctlr_info *h,
  2159. struct CommandList *c, struct scsi_cmnd *cmd)
  2160. {
  2161. hpsa_cmd_resolve_and_free(h, c);
  2162. if (cmd && cmd->scsi_done)
  2163. cmd->scsi_done(cmd);
  2164. }
  2165. static void hpsa_retry_cmd(struct ctlr_info *h, struct CommandList *c)
  2166. {
  2167. INIT_WORK(&c->work, hpsa_command_resubmit_worker);
  2168. queue_work_on(raw_smp_processor_id(), h->resubmit_wq, &c->work);
  2169. }
  2170. static void hpsa_set_scsi_cmd_aborted(struct scsi_cmnd *cmd)
  2171. {
  2172. cmd->result = DID_ABORT << 16;
  2173. }
  2174. static void hpsa_cmd_abort_and_free(struct ctlr_info *h, struct CommandList *c,
  2175. struct scsi_cmnd *cmd)
  2176. {
  2177. hpsa_set_scsi_cmd_aborted(cmd);
  2178. dev_warn(&h->pdev->dev, "CDB %16phN was aborted with status 0x%x\n",
  2179. c->Request.CDB, c->err_info->ScsiStatus);
  2180. hpsa_cmd_resolve_and_free(h, c);
  2181. }
  2182. static void process_ioaccel2_completion(struct ctlr_info *h,
  2183. struct CommandList *c, struct scsi_cmnd *cmd,
  2184. struct hpsa_scsi_dev_t *dev)
  2185. {
  2186. struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
  2187. /* check for good status */
  2188. if (likely(c2->error_data.serv_response == 0 &&
  2189. c2->error_data.status == 0))
  2190. return hpsa_cmd_free_and_done(h, c, cmd);
  2191. /*
  2192. * Any RAID offload error results in retry which will use
  2193. * the normal I/O path so the controller can handle whatever's
  2194. * wrong.
  2195. */
  2196. if (is_logical_device(dev) &&
  2197. c2->error_data.serv_response ==
  2198. IOACCEL2_SERV_RESPONSE_FAILURE) {
  2199. if (c2->error_data.status ==
  2200. IOACCEL2_STATUS_SR_IOACCEL_DISABLED) {
  2201. dev->offload_enabled = 0;
  2202. dev->offload_to_be_enabled = 0;
  2203. }
  2204. return hpsa_retry_cmd(h, c);
  2205. }
  2206. if (handle_ioaccel_mode2_error(h, c, cmd, c2, dev))
  2207. return hpsa_retry_cmd(h, c);
  2208. return hpsa_cmd_free_and_done(h, c, cmd);
  2209. }
  2210. /* Returns 0 on success, < 0 otherwise. */
  2211. static int hpsa_evaluate_tmf_status(struct ctlr_info *h,
  2212. struct CommandList *cp)
  2213. {
  2214. u8 tmf_status = cp->err_info->ScsiStatus;
  2215. switch (tmf_status) {
  2216. case CISS_TMF_COMPLETE:
  2217. /*
  2218. * CISS_TMF_COMPLETE never happens, instead,
  2219. * ei->CommandStatus == 0 for this case.
  2220. */
  2221. case CISS_TMF_SUCCESS:
  2222. return 0;
  2223. case CISS_TMF_INVALID_FRAME:
  2224. case CISS_TMF_NOT_SUPPORTED:
  2225. case CISS_TMF_FAILED:
  2226. case CISS_TMF_WRONG_LUN:
  2227. case CISS_TMF_OVERLAPPED_TAG:
  2228. break;
  2229. default:
  2230. dev_warn(&h->pdev->dev, "Unknown TMF status: 0x%02x\n",
  2231. tmf_status);
  2232. break;
  2233. }
  2234. return -tmf_status;
  2235. }
  2236. static void complete_scsi_command(struct CommandList *cp)
  2237. {
  2238. struct scsi_cmnd *cmd;
  2239. struct ctlr_info *h;
  2240. struct ErrorInfo *ei;
  2241. struct hpsa_scsi_dev_t *dev;
  2242. struct io_accel2_cmd *c2;
  2243. u8 sense_key;
  2244. u8 asc; /* additional sense code */
  2245. u8 ascq; /* additional sense code qualifier */
  2246. unsigned long sense_data_size;
  2247. ei = cp->err_info;
  2248. cmd = cp->scsi_cmd;
  2249. h = cp->h;
  2250. if (!cmd->device) {
  2251. cmd->result = DID_NO_CONNECT << 16;
  2252. return hpsa_cmd_free_and_done(h, cp, cmd);
  2253. }
  2254. dev = cmd->device->hostdata;
  2255. if (!dev) {
  2256. cmd->result = DID_NO_CONNECT << 16;
  2257. return hpsa_cmd_free_and_done(h, cp, cmd);
  2258. }
  2259. c2 = &h->ioaccel2_cmd_pool[cp->cmdindex];
  2260. scsi_dma_unmap(cmd); /* undo the DMA mappings */
  2261. if ((cp->cmd_type == CMD_SCSI) &&
  2262. (le16_to_cpu(cp->Header.SGTotal) > h->max_cmd_sg_entries))
  2263. hpsa_unmap_sg_chain_block(h, cp);
  2264. if ((cp->cmd_type == CMD_IOACCEL2) &&
  2265. (c2->sg[0].chain_indicator == IOACCEL2_CHAIN))
  2266. hpsa_unmap_ioaccel2_sg_chain_block(h, c2);
  2267. cmd->result = (DID_OK << 16); /* host byte */
  2268. cmd->result |= (COMMAND_COMPLETE << 8); /* msg byte */
  2269. if (cp->cmd_type == CMD_IOACCEL2 || cp->cmd_type == CMD_IOACCEL1) {
  2270. if (dev->physical_device && dev->expose_device &&
  2271. dev->removed) {
  2272. cmd->result = DID_NO_CONNECT << 16;
  2273. return hpsa_cmd_free_and_done(h, cp, cmd);
  2274. }
  2275. if (likely(cp->phys_disk != NULL))
  2276. atomic_dec(&cp->phys_disk->ioaccel_cmds_out);
  2277. }
  2278. /*
  2279. * We check for lockup status here as it may be set for
  2280. * CMD_SCSI, CMD_IOACCEL1 and CMD_IOACCEL2 commands by
  2281. * fail_all_oustanding_cmds()
  2282. */
  2283. if (unlikely(ei->CommandStatus == CMD_CTLR_LOCKUP)) {
  2284. /* DID_NO_CONNECT will prevent a retry */
  2285. cmd->result = DID_NO_CONNECT << 16;
  2286. return hpsa_cmd_free_and_done(h, cp, cmd);
  2287. }
  2288. if ((unlikely(hpsa_is_pending_event(cp)))) {
  2289. if (cp->reset_pending)
  2290. return hpsa_cmd_resolve_and_free(h, cp);
  2291. if (cp->abort_pending)
  2292. return hpsa_cmd_abort_and_free(h, cp, cmd);
  2293. }
  2294. if (cp->cmd_type == CMD_IOACCEL2)
  2295. return process_ioaccel2_completion(h, cp, cmd, dev);
  2296. scsi_set_resid(cmd, ei->ResidualCnt);
  2297. if (ei->CommandStatus == 0)
  2298. return hpsa_cmd_free_and_done(h, cp, cmd);
  2299. /* For I/O accelerator commands, copy over some fields to the normal
  2300. * CISS header used below for error handling.
  2301. */
  2302. if (cp->cmd_type == CMD_IOACCEL1) {
  2303. struct io_accel1_cmd *c = &h->ioaccel_cmd_pool[cp->cmdindex];
  2304. cp->Header.SGList = scsi_sg_count(cmd);
  2305. cp->Header.SGTotal = cpu_to_le16(cp->Header.SGList);
  2306. cp->Request.CDBLen = le16_to_cpu(c->io_flags) &
  2307. IOACCEL1_IOFLAGS_CDBLEN_MASK;
  2308. cp->Header.tag = c->tag;
  2309. memcpy(cp->Header.LUN.LunAddrBytes, c->CISS_LUN, 8);
  2310. memcpy(cp->Request.CDB, c->CDB, cp->Request.CDBLen);
  2311. /* Any RAID offload error results in retry which will use
  2312. * the normal I/O path so the controller can handle whatever's
  2313. * wrong.
  2314. */
  2315. if (is_logical_device(dev)) {
  2316. if (ei->CommandStatus == CMD_IOACCEL_DISABLED)
  2317. dev->offload_enabled = 0;
  2318. return hpsa_retry_cmd(h, cp);
  2319. }
  2320. }
  2321. /* an error has occurred */
  2322. switch (ei->CommandStatus) {
  2323. case CMD_TARGET_STATUS:
  2324. cmd->result |= ei->ScsiStatus;
  2325. /* copy the sense data */
  2326. if (SCSI_SENSE_BUFFERSIZE < sizeof(ei->SenseInfo))
  2327. sense_data_size = SCSI_SENSE_BUFFERSIZE;
  2328. else
  2329. sense_data_size = sizeof(ei->SenseInfo);
  2330. if (ei->SenseLen < sense_data_size)
  2331. sense_data_size = ei->SenseLen;
  2332. memcpy(cmd->sense_buffer, ei->SenseInfo, sense_data_size);
  2333. if (ei->ScsiStatus)
  2334. decode_sense_data(ei->SenseInfo, sense_data_size,
  2335. &sense_key, &asc, &ascq);
  2336. if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION) {
  2337. if (sense_key == ABORTED_COMMAND) {
  2338. cmd->result |= DID_SOFT_ERROR << 16;
  2339. break;
  2340. }
  2341. break;
  2342. }
  2343. /* Problem was not a check condition
  2344. * Pass it up to the upper layers...
  2345. */
  2346. if (ei->ScsiStatus) {
  2347. dev_warn(&h->pdev->dev, "cp %p has status 0x%x "
  2348. "Sense: 0x%x, ASC: 0x%x, ASCQ: 0x%x, "
  2349. "Returning result: 0x%x\n",
  2350. cp, ei->ScsiStatus,
  2351. sense_key, asc, ascq,
  2352. cmd->result);
  2353. } else { /* scsi status is zero??? How??? */
  2354. dev_warn(&h->pdev->dev, "cp %p SCSI status was 0. "
  2355. "Returning no connection.\n", cp),
  2356. /* Ordinarily, this case should never happen,
  2357. * but there is a bug in some released firmware
  2358. * revisions that allows it to happen if, for
  2359. * example, a 4100 backplane loses power and
  2360. * the tape drive is in it. We assume that
  2361. * it's a fatal error of some kind because we
  2362. * can't show that it wasn't. We will make it
  2363. * look like selection timeout since that is
  2364. * the most common reason for this to occur,
  2365. * and it's severe enough.
  2366. */
  2367. cmd->result = DID_NO_CONNECT << 16;
  2368. }
  2369. break;
  2370. case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
  2371. break;
  2372. case CMD_DATA_OVERRUN:
  2373. dev_warn(&h->pdev->dev,
  2374. "CDB %16phN data overrun\n", cp->Request.CDB);
  2375. break;
  2376. case CMD_INVALID: {
  2377. /* print_bytes(cp, sizeof(*cp), 1, 0);
  2378. print_cmd(cp); */
  2379. /* We get CMD_INVALID if you address a non-existent device
  2380. * instead of a selection timeout (no response). You will
  2381. * see this if you yank out a drive, then try to access it.
  2382. * This is kind of a shame because it means that any other
  2383. * CMD_INVALID (e.g. driver bug) will get interpreted as a
  2384. * missing target. */
  2385. cmd->result = DID_NO_CONNECT << 16;
  2386. }
  2387. break;
  2388. case CMD_PROTOCOL_ERR:
  2389. cmd->result = DID_ERROR << 16;
  2390. dev_warn(&h->pdev->dev, "CDB %16phN : protocol error\n",
  2391. cp->Request.CDB);
  2392. break;
  2393. case CMD_HARDWARE_ERR:
  2394. cmd->result = DID_ERROR << 16;
  2395. dev_warn(&h->pdev->dev, "CDB %16phN : hardware error\n",
  2396. cp->Request.CDB);
  2397. break;
  2398. case CMD_CONNECTION_LOST:
  2399. cmd->result = DID_ERROR << 16;
  2400. dev_warn(&h->pdev->dev, "CDB %16phN : connection lost\n",
  2401. cp->Request.CDB);
  2402. break;
  2403. case CMD_ABORTED:
  2404. /* Return now to avoid calling scsi_done(). */
  2405. return hpsa_cmd_abort_and_free(h, cp, cmd);
  2406. case CMD_ABORT_FAILED:
  2407. cmd->result = DID_ERROR << 16;
  2408. dev_warn(&h->pdev->dev, "CDB %16phN : abort failed\n",
  2409. cp->Request.CDB);
  2410. break;
  2411. case CMD_UNSOLICITED_ABORT:
  2412. cmd->result = DID_SOFT_ERROR << 16; /* retry the command */
  2413. dev_warn(&h->pdev->dev, "CDB %16phN : unsolicited abort\n",
  2414. cp->Request.CDB);
  2415. break;
  2416. case CMD_TIMEOUT:
  2417. cmd->result = DID_TIME_OUT << 16;
  2418. dev_warn(&h->pdev->dev, "CDB %16phN timed out\n",
  2419. cp->Request.CDB);
  2420. break;
  2421. case CMD_UNABORTABLE:
  2422. cmd->result = DID_ERROR << 16;
  2423. dev_warn(&h->pdev->dev, "Command unabortable\n");
  2424. break;
  2425. case CMD_TMF_STATUS:
  2426. if (hpsa_evaluate_tmf_status(h, cp)) /* TMF failed? */
  2427. cmd->result = DID_ERROR << 16;
  2428. break;
  2429. case CMD_IOACCEL_DISABLED:
  2430. /* This only handles the direct pass-through case since RAID
  2431. * offload is handled above. Just attempt a retry.
  2432. */
  2433. cmd->result = DID_SOFT_ERROR << 16;
  2434. dev_warn(&h->pdev->dev,
  2435. "cp %p had HP SSD Smart Path error\n", cp);
  2436. break;
  2437. default:
  2438. cmd->result = DID_ERROR << 16;
  2439. dev_warn(&h->pdev->dev, "cp %p returned unknown status %x\n",
  2440. cp, ei->CommandStatus);
  2441. }
  2442. return hpsa_cmd_free_and_done(h, cp, cmd);
  2443. }
  2444. static void hpsa_pci_unmap(struct pci_dev *pdev,
  2445. struct CommandList *c, int sg_used, int data_direction)
  2446. {
  2447. int i;
  2448. for (i = 0; i < sg_used; i++)
  2449. pci_unmap_single(pdev, (dma_addr_t) le64_to_cpu(c->SG[i].Addr),
  2450. le32_to_cpu(c->SG[i].Len),
  2451. data_direction);
  2452. }
  2453. static int hpsa_map_one(struct pci_dev *pdev,
  2454. struct CommandList *cp,
  2455. unsigned char *buf,
  2456. size_t buflen,
  2457. int data_direction)
  2458. {
  2459. u64 addr64;
  2460. if (buflen == 0 || data_direction == PCI_DMA_NONE) {
  2461. cp->Header.SGList = 0;
  2462. cp->Header.SGTotal = cpu_to_le16(0);
  2463. return 0;
  2464. }
  2465. addr64 = pci_map_single(pdev, buf, buflen, data_direction);
  2466. if (dma_mapping_error(&pdev->dev, addr64)) {
  2467. /* Prevent subsequent unmap of something never mapped */
  2468. cp->Header.SGList = 0;
  2469. cp->Header.SGTotal = cpu_to_le16(0);
  2470. return -1;
  2471. }
  2472. cp->SG[0].Addr = cpu_to_le64(addr64);
  2473. cp->SG[0].Len = cpu_to_le32(buflen);
  2474. cp->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* we are not chaining */
  2475. cp->Header.SGList = 1; /* no. SGs contig in this cmd */
  2476. cp->Header.SGTotal = cpu_to_le16(1); /* total sgs in cmd list */
  2477. return 0;
  2478. }
  2479. #define NO_TIMEOUT ((unsigned long) -1)
  2480. #define DEFAULT_TIMEOUT 30000 /* milliseconds */
  2481. static int hpsa_scsi_do_simple_cmd_core(struct ctlr_info *h,
  2482. struct CommandList *c, int reply_queue, unsigned long timeout_msecs)
  2483. {
  2484. DECLARE_COMPLETION_ONSTACK(wait);
  2485. c->waiting = &wait;
  2486. __enqueue_cmd_and_start_io(h, c, reply_queue);
  2487. if (timeout_msecs == NO_TIMEOUT) {
  2488. /* TODO: get rid of this no-timeout thing */
  2489. wait_for_completion_io(&wait);
  2490. return IO_OK;
  2491. }
  2492. if (!wait_for_completion_io_timeout(&wait,
  2493. msecs_to_jiffies(timeout_msecs))) {
  2494. dev_warn(&h->pdev->dev, "Command timed out.\n");
  2495. return -ETIMEDOUT;
  2496. }
  2497. return IO_OK;
  2498. }
  2499. static int hpsa_scsi_do_simple_cmd(struct ctlr_info *h, struct CommandList *c,
  2500. int reply_queue, unsigned long timeout_msecs)
  2501. {
  2502. if (unlikely(lockup_detected(h))) {
  2503. c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
  2504. return IO_OK;
  2505. }
  2506. return hpsa_scsi_do_simple_cmd_core(h, c, reply_queue, timeout_msecs);
  2507. }
  2508. static u32 lockup_detected(struct ctlr_info *h)
  2509. {
  2510. int cpu;
  2511. u32 rc, *lockup_detected;
  2512. cpu = get_cpu();
  2513. lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
  2514. rc = *lockup_detected;
  2515. put_cpu();
  2516. return rc;
  2517. }
  2518. #define MAX_DRIVER_CMD_RETRIES 25
  2519. static int hpsa_scsi_do_simple_cmd_with_retry(struct ctlr_info *h,
  2520. struct CommandList *c, int data_direction, unsigned long timeout_msecs)
  2521. {
  2522. int backoff_time = 10, retry_count = 0;
  2523. int rc;
  2524. do {
  2525. memset(c->err_info, 0, sizeof(*c->err_info));
  2526. rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
  2527. timeout_msecs);
  2528. if (rc)
  2529. break;
  2530. retry_count++;
  2531. if (retry_count > 3) {
  2532. msleep(backoff_time);
  2533. if (backoff_time < 1000)
  2534. backoff_time *= 2;
  2535. }
  2536. } while ((check_for_unit_attention(h, c) ||
  2537. check_for_busy(h, c)) &&
  2538. retry_count <= MAX_DRIVER_CMD_RETRIES);
  2539. hpsa_pci_unmap(h->pdev, c, 1, data_direction);
  2540. if (retry_count > MAX_DRIVER_CMD_RETRIES)
  2541. rc = -EIO;
  2542. return rc;
  2543. }
  2544. static void hpsa_print_cmd(struct ctlr_info *h, char *txt,
  2545. struct CommandList *c)
  2546. {
  2547. const u8 *cdb = c->Request.CDB;
  2548. const u8 *lun = c->Header.LUN.LunAddrBytes;
  2549. dev_warn(&h->pdev->dev, "%s: LUN:%02x%02x%02x%02x%02x%02x%02x%02x"
  2550. " CDB:%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  2551. txt, lun[0], lun[1], lun[2], lun[3],
  2552. lun[4], lun[5], lun[6], lun[7],
  2553. cdb[0], cdb[1], cdb[2], cdb[3],
  2554. cdb[4], cdb[5], cdb[6], cdb[7],
  2555. cdb[8], cdb[9], cdb[10], cdb[11],
  2556. cdb[12], cdb[13], cdb[14], cdb[15]);
  2557. }
  2558. static void hpsa_scsi_interpret_error(struct ctlr_info *h,
  2559. struct CommandList *cp)
  2560. {
  2561. const struct ErrorInfo *ei = cp->err_info;
  2562. struct device *d = &cp->h->pdev->dev;
  2563. u8 sense_key, asc, ascq;
  2564. int sense_len;
  2565. switch (ei->CommandStatus) {
  2566. case CMD_TARGET_STATUS:
  2567. if (ei->SenseLen > sizeof(ei->SenseInfo))
  2568. sense_len = sizeof(ei->SenseInfo);
  2569. else
  2570. sense_len = ei->SenseLen;
  2571. decode_sense_data(ei->SenseInfo, sense_len,
  2572. &sense_key, &asc, &ascq);
  2573. hpsa_print_cmd(h, "SCSI status", cp);
  2574. if (ei->ScsiStatus == SAM_STAT_CHECK_CONDITION)
  2575. dev_warn(d, "SCSI Status = 02, Sense key = 0x%02x, ASC = 0x%02x, ASCQ = 0x%02x\n",
  2576. sense_key, asc, ascq);
  2577. else
  2578. dev_warn(d, "SCSI Status = 0x%02x\n", ei->ScsiStatus);
  2579. if (ei->ScsiStatus == 0)
  2580. dev_warn(d, "SCSI status is abnormally zero. "
  2581. "(probably indicates selection timeout "
  2582. "reported incorrectly due to a known "
  2583. "firmware bug, circa July, 2001.)\n");
  2584. break;
  2585. case CMD_DATA_UNDERRUN: /* let mid layer handle it. */
  2586. break;
  2587. case CMD_DATA_OVERRUN:
  2588. hpsa_print_cmd(h, "overrun condition", cp);
  2589. break;
  2590. case CMD_INVALID: {
  2591. /* controller unfortunately reports SCSI passthru's
  2592. * to non-existent targets as invalid commands.
  2593. */
  2594. hpsa_print_cmd(h, "invalid command", cp);
  2595. dev_warn(d, "probably means device no longer present\n");
  2596. }
  2597. break;
  2598. case CMD_PROTOCOL_ERR:
  2599. hpsa_print_cmd(h, "protocol error", cp);
  2600. break;
  2601. case CMD_HARDWARE_ERR:
  2602. hpsa_print_cmd(h, "hardware error", cp);
  2603. break;
  2604. case CMD_CONNECTION_LOST:
  2605. hpsa_print_cmd(h, "connection lost", cp);
  2606. break;
  2607. case CMD_ABORTED:
  2608. hpsa_print_cmd(h, "aborted", cp);
  2609. break;
  2610. case CMD_ABORT_FAILED:
  2611. hpsa_print_cmd(h, "abort failed", cp);
  2612. break;
  2613. case CMD_UNSOLICITED_ABORT:
  2614. hpsa_print_cmd(h, "unsolicited abort", cp);
  2615. break;
  2616. case CMD_TIMEOUT:
  2617. hpsa_print_cmd(h, "timed out", cp);
  2618. break;
  2619. case CMD_UNABORTABLE:
  2620. hpsa_print_cmd(h, "unabortable", cp);
  2621. break;
  2622. case CMD_CTLR_LOCKUP:
  2623. hpsa_print_cmd(h, "controller lockup detected", cp);
  2624. break;
  2625. default:
  2626. hpsa_print_cmd(h, "unknown status", cp);
  2627. dev_warn(d, "Unknown command status %x\n",
  2628. ei->CommandStatus);
  2629. }
  2630. }
  2631. static int hpsa_scsi_do_inquiry(struct ctlr_info *h, unsigned char *scsi3addr,
  2632. u16 page, unsigned char *buf,
  2633. unsigned char bufsize)
  2634. {
  2635. int rc = IO_OK;
  2636. struct CommandList *c;
  2637. struct ErrorInfo *ei;
  2638. c = cmd_alloc(h);
  2639. if (fill_cmd(c, HPSA_INQUIRY, h, buf, bufsize,
  2640. page, scsi3addr, TYPE_CMD)) {
  2641. rc = -1;
  2642. goto out;
  2643. }
  2644. rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
  2645. PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
  2646. if (rc)
  2647. goto out;
  2648. ei = c->err_info;
  2649. if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
  2650. hpsa_scsi_interpret_error(h, c);
  2651. rc = -1;
  2652. }
  2653. out:
  2654. cmd_free(h, c);
  2655. return rc;
  2656. }
  2657. static int hpsa_send_reset(struct ctlr_info *h, unsigned char *scsi3addr,
  2658. u8 reset_type, int reply_queue)
  2659. {
  2660. int rc = IO_OK;
  2661. struct CommandList *c;
  2662. struct ErrorInfo *ei;
  2663. c = cmd_alloc(h);
  2664. /* fill_cmd can't fail here, no data buffer to map. */
  2665. (void) fill_cmd(c, reset_type, h, NULL, 0, 0,
  2666. scsi3addr, TYPE_MSG);
  2667. rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, DEFAULT_TIMEOUT);
  2668. if (rc) {
  2669. dev_warn(&h->pdev->dev, "Failed to send reset command\n");
  2670. goto out;
  2671. }
  2672. /* no unmap needed here because no data xfer. */
  2673. ei = c->err_info;
  2674. if (ei->CommandStatus != 0) {
  2675. hpsa_scsi_interpret_error(h, c);
  2676. rc = -1;
  2677. }
  2678. out:
  2679. cmd_free(h, c);
  2680. return rc;
  2681. }
  2682. static bool hpsa_cmd_dev_match(struct ctlr_info *h, struct CommandList *c,
  2683. struct hpsa_scsi_dev_t *dev,
  2684. unsigned char *scsi3addr)
  2685. {
  2686. int i;
  2687. bool match = false;
  2688. struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
  2689. struct hpsa_tmf_struct *ac = (struct hpsa_tmf_struct *) c2;
  2690. if (hpsa_is_cmd_idle(c))
  2691. return false;
  2692. switch (c->cmd_type) {
  2693. case CMD_SCSI:
  2694. case CMD_IOCTL_PEND:
  2695. match = !memcmp(scsi3addr, &c->Header.LUN.LunAddrBytes,
  2696. sizeof(c->Header.LUN.LunAddrBytes));
  2697. break;
  2698. case CMD_IOACCEL1:
  2699. case CMD_IOACCEL2:
  2700. if (c->phys_disk == dev) {
  2701. /* HBA mode match */
  2702. match = true;
  2703. } else {
  2704. /* Possible RAID mode -- check each phys dev. */
  2705. /* FIXME: Do we need to take out a lock here? If
  2706. * so, we could just call hpsa_get_pdisk_of_ioaccel2()
  2707. * instead. */
  2708. for (i = 0; i < dev->nphysical_disks && !match; i++) {
  2709. /* FIXME: an alternate test might be
  2710. *
  2711. * match = dev->phys_disk[i]->ioaccel_handle
  2712. * == c2->scsi_nexus; */
  2713. match = dev->phys_disk[i] == c->phys_disk;
  2714. }
  2715. }
  2716. break;
  2717. case IOACCEL2_TMF:
  2718. for (i = 0; i < dev->nphysical_disks && !match; i++) {
  2719. match = dev->phys_disk[i]->ioaccel_handle ==
  2720. le32_to_cpu(ac->it_nexus);
  2721. }
  2722. break;
  2723. case 0: /* The command is in the middle of being initialized. */
  2724. match = false;
  2725. break;
  2726. default:
  2727. dev_err(&h->pdev->dev, "unexpected cmd_type: %d\n",
  2728. c->cmd_type);
  2729. BUG();
  2730. }
  2731. return match;
  2732. }
  2733. static int hpsa_do_reset(struct ctlr_info *h, struct hpsa_scsi_dev_t *dev,
  2734. unsigned char *scsi3addr, u8 reset_type, int reply_queue)
  2735. {
  2736. int i;
  2737. int rc = 0;
  2738. /* We can really only handle one reset at a time */
  2739. if (mutex_lock_interruptible(&h->reset_mutex) == -EINTR) {
  2740. dev_warn(&h->pdev->dev, "concurrent reset wait interrupted.\n");
  2741. return -EINTR;
  2742. }
  2743. BUG_ON(atomic_read(&dev->reset_cmds_out) != 0);
  2744. for (i = 0; i < h->nr_cmds; i++) {
  2745. struct CommandList *c = h->cmd_pool + i;
  2746. int refcount = atomic_inc_return(&c->refcount);
  2747. if (refcount > 1 && hpsa_cmd_dev_match(h, c, dev, scsi3addr)) {
  2748. unsigned long flags;
  2749. /*
  2750. * Mark the target command as having a reset pending,
  2751. * then lock a lock so that the command cannot complete
  2752. * while we're considering it. If the command is not
  2753. * idle then count it; otherwise revoke the event.
  2754. */
  2755. c->reset_pending = dev;
  2756. spin_lock_irqsave(&h->lock, flags); /* Implied MB */
  2757. if (!hpsa_is_cmd_idle(c))
  2758. atomic_inc(&dev->reset_cmds_out);
  2759. else
  2760. c->reset_pending = NULL;
  2761. spin_unlock_irqrestore(&h->lock, flags);
  2762. }
  2763. cmd_free(h, c);
  2764. }
  2765. rc = hpsa_send_reset(h, scsi3addr, reset_type, reply_queue);
  2766. if (!rc)
  2767. wait_event(h->event_sync_wait_queue,
  2768. atomic_read(&dev->reset_cmds_out) == 0 ||
  2769. lockup_detected(h));
  2770. if (unlikely(lockup_detected(h))) {
  2771. dev_warn(&h->pdev->dev,
  2772. "Controller lockup detected during reset wait\n");
  2773. rc = -ENODEV;
  2774. }
  2775. if (unlikely(rc))
  2776. atomic_set(&dev->reset_cmds_out, 0);
  2777. mutex_unlock(&h->reset_mutex);
  2778. return rc;
  2779. }
  2780. static void hpsa_get_raid_level(struct ctlr_info *h,
  2781. unsigned char *scsi3addr, unsigned char *raid_level)
  2782. {
  2783. int rc;
  2784. unsigned char *buf;
  2785. *raid_level = RAID_UNKNOWN;
  2786. buf = kzalloc(64, GFP_KERNEL);
  2787. if (!buf)
  2788. return;
  2789. if (!hpsa_vpd_page_supported(h, scsi3addr,
  2790. HPSA_VPD_LV_DEVICE_GEOMETRY))
  2791. goto exit;
  2792. rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE |
  2793. HPSA_VPD_LV_DEVICE_GEOMETRY, buf, 64);
  2794. if (rc == 0)
  2795. *raid_level = buf[8];
  2796. if (*raid_level > RAID_UNKNOWN)
  2797. *raid_level = RAID_UNKNOWN;
  2798. exit:
  2799. kfree(buf);
  2800. return;
  2801. }
  2802. #define HPSA_MAP_DEBUG
  2803. #ifdef HPSA_MAP_DEBUG
  2804. static void hpsa_debug_map_buff(struct ctlr_info *h, int rc,
  2805. struct raid_map_data *map_buff)
  2806. {
  2807. struct raid_map_disk_data *dd = &map_buff->data[0];
  2808. int map, row, col;
  2809. u16 map_cnt, row_cnt, disks_per_row;
  2810. if (rc != 0)
  2811. return;
  2812. /* Show details only if debugging has been activated. */
  2813. if (h->raid_offload_debug < 2)
  2814. return;
  2815. dev_info(&h->pdev->dev, "structure_size = %u\n",
  2816. le32_to_cpu(map_buff->structure_size));
  2817. dev_info(&h->pdev->dev, "volume_blk_size = %u\n",
  2818. le32_to_cpu(map_buff->volume_blk_size));
  2819. dev_info(&h->pdev->dev, "volume_blk_cnt = 0x%llx\n",
  2820. le64_to_cpu(map_buff->volume_blk_cnt));
  2821. dev_info(&h->pdev->dev, "physicalBlockShift = %u\n",
  2822. map_buff->phys_blk_shift);
  2823. dev_info(&h->pdev->dev, "parity_rotation_shift = %u\n",
  2824. map_buff->parity_rotation_shift);
  2825. dev_info(&h->pdev->dev, "strip_size = %u\n",
  2826. le16_to_cpu(map_buff->strip_size));
  2827. dev_info(&h->pdev->dev, "disk_starting_blk = 0x%llx\n",
  2828. le64_to_cpu(map_buff->disk_starting_blk));
  2829. dev_info(&h->pdev->dev, "disk_blk_cnt = 0x%llx\n",
  2830. le64_to_cpu(map_buff->disk_blk_cnt));
  2831. dev_info(&h->pdev->dev, "data_disks_per_row = %u\n",
  2832. le16_to_cpu(map_buff->data_disks_per_row));
  2833. dev_info(&h->pdev->dev, "metadata_disks_per_row = %u\n",
  2834. le16_to_cpu(map_buff->metadata_disks_per_row));
  2835. dev_info(&h->pdev->dev, "row_cnt = %u\n",
  2836. le16_to_cpu(map_buff->row_cnt));
  2837. dev_info(&h->pdev->dev, "layout_map_count = %u\n",
  2838. le16_to_cpu(map_buff->layout_map_count));
  2839. dev_info(&h->pdev->dev, "flags = 0x%x\n",
  2840. le16_to_cpu(map_buff->flags));
  2841. dev_info(&h->pdev->dev, "encrypytion = %s\n",
  2842. le16_to_cpu(map_buff->flags) &
  2843. RAID_MAP_FLAG_ENCRYPT_ON ? "ON" : "OFF");
  2844. dev_info(&h->pdev->dev, "dekindex = %u\n",
  2845. le16_to_cpu(map_buff->dekindex));
  2846. map_cnt = le16_to_cpu(map_buff->layout_map_count);
  2847. for (map = 0; map < map_cnt; map++) {
  2848. dev_info(&h->pdev->dev, "Map%u:\n", map);
  2849. row_cnt = le16_to_cpu(map_buff->row_cnt);
  2850. for (row = 0; row < row_cnt; row++) {
  2851. dev_info(&h->pdev->dev, " Row%u:\n", row);
  2852. disks_per_row =
  2853. le16_to_cpu(map_buff->data_disks_per_row);
  2854. for (col = 0; col < disks_per_row; col++, dd++)
  2855. dev_info(&h->pdev->dev,
  2856. " D%02u: h=0x%04x xor=%u,%u\n",
  2857. col, dd->ioaccel_handle,
  2858. dd->xor_mult[0], dd->xor_mult[1]);
  2859. disks_per_row =
  2860. le16_to_cpu(map_buff->metadata_disks_per_row);
  2861. for (col = 0; col < disks_per_row; col++, dd++)
  2862. dev_info(&h->pdev->dev,
  2863. " M%02u: h=0x%04x xor=%u,%u\n",
  2864. col, dd->ioaccel_handle,
  2865. dd->xor_mult[0], dd->xor_mult[1]);
  2866. }
  2867. }
  2868. }
  2869. #else
  2870. static void hpsa_debug_map_buff(__attribute__((unused)) struct ctlr_info *h,
  2871. __attribute__((unused)) int rc,
  2872. __attribute__((unused)) struct raid_map_data *map_buff)
  2873. {
  2874. }
  2875. #endif
  2876. static int hpsa_get_raid_map(struct ctlr_info *h,
  2877. unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
  2878. {
  2879. int rc = 0;
  2880. struct CommandList *c;
  2881. struct ErrorInfo *ei;
  2882. c = cmd_alloc(h);
  2883. if (fill_cmd(c, HPSA_GET_RAID_MAP, h, &this_device->raid_map,
  2884. sizeof(this_device->raid_map), 0,
  2885. scsi3addr, TYPE_CMD)) {
  2886. dev_warn(&h->pdev->dev, "hpsa_get_raid_map fill_cmd failed\n");
  2887. cmd_free(h, c);
  2888. return -1;
  2889. }
  2890. rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
  2891. PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
  2892. if (rc)
  2893. goto out;
  2894. ei = c->err_info;
  2895. if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
  2896. hpsa_scsi_interpret_error(h, c);
  2897. rc = -1;
  2898. goto out;
  2899. }
  2900. cmd_free(h, c);
  2901. /* @todo in the future, dynamically allocate RAID map memory */
  2902. if (le32_to_cpu(this_device->raid_map.structure_size) >
  2903. sizeof(this_device->raid_map)) {
  2904. dev_warn(&h->pdev->dev, "RAID map size is too large!\n");
  2905. rc = -1;
  2906. }
  2907. hpsa_debug_map_buff(h, rc, &this_device->raid_map);
  2908. return rc;
  2909. out:
  2910. cmd_free(h, c);
  2911. return rc;
  2912. }
  2913. static int hpsa_bmic_sense_subsystem_information(struct ctlr_info *h,
  2914. unsigned char scsi3addr[], u16 bmic_device_index,
  2915. struct bmic_sense_subsystem_info *buf, size_t bufsize)
  2916. {
  2917. int rc = IO_OK;
  2918. struct CommandList *c;
  2919. struct ErrorInfo *ei;
  2920. c = cmd_alloc(h);
  2921. rc = fill_cmd(c, BMIC_SENSE_SUBSYSTEM_INFORMATION, h, buf, bufsize,
  2922. 0, RAID_CTLR_LUNID, TYPE_CMD);
  2923. if (rc)
  2924. goto out;
  2925. c->Request.CDB[2] = bmic_device_index & 0xff;
  2926. c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
  2927. rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
  2928. PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
  2929. if (rc)
  2930. goto out;
  2931. ei = c->err_info;
  2932. if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
  2933. hpsa_scsi_interpret_error(h, c);
  2934. rc = -1;
  2935. }
  2936. out:
  2937. cmd_free(h, c);
  2938. return rc;
  2939. }
  2940. static int hpsa_bmic_id_controller(struct ctlr_info *h,
  2941. struct bmic_identify_controller *buf, size_t bufsize)
  2942. {
  2943. int rc = IO_OK;
  2944. struct CommandList *c;
  2945. struct ErrorInfo *ei;
  2946. c = cmd_alloc(h);
  2947. rc = fill_cmd(c, BMIC_IDENTIFY_CONTROLLER, h, buf, bufsize,
  2948. 0, RAID_CTLR_LUNID, TYPE_CMD);
  2949. if (rc)
  2950. goto out;
  2951. rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
  2952. PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
  2953. if (rc)
  2954. goto out;
  2955. ei = c->err_info;
  2956. if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
  2957. hpsa_scsi_interpret_error(h, c);
  2958. rc = -1;
  2959. }
  2960. out:
  2961. cmd_free(h, c);
  2962. return rc;
  2963. }
  2964. static int hpsa_bmic_id_physical_device(struct ctlr_info *h,
  2965. unsigned char scsi3addr[], u16 bmic_device_index,
  2966. struct bmic_identify_physical_device *buf, size_t bufsize)
  2967. {
  2968. int rc = IO_OK;
  2969. struct CommandList *c;
  2970. struct ErrorInfo *ei;
  2971. c = cmd_alloc(h);
  2972. rc = fill_cmd(c, BMIC_IDENTIFY_PHYSICAL_DEVICE, h, buf, bufsize,
  2973. 0, RAID_CTLR_LUNID, TYPE_CMD);
  2974. if (rc)
  2975. goto out;
  2976. c->Request.CDB[2] = bmic_device_index & 0xff;
  2977. c->Request.CDB[9] = (bmic_device_index >> 8) & 0xff;
  2978. hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE,
  2979. DEFAULT_TIMEOUT);
  2980. ei = c->err_info;
  2981. if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
  2982. hpsa_scsi_interpret_error(h, c);
  2983. rc = -1;
  2984. }
  2985. out:
  2986. cmd_free(h, c);
  2987. return rc;
  2988. }
  2989. /*
  2990. * get enclosure information
  2991. * struct ReportExtendedLUNdata *rlep - Used for BMIC drive number
  2992. * struct hpsa_scsi_dev_t *encl_dev - device entry for enclosure
  2993. * Uses id_physical_device to determine the box_index.
  2994. */
  2995. static void hpsa_get_enclosure_info(struct ctlr_info *h,
  2996. unsigned char *scsi3addr,
  2997. struct ReportExtendedLUNdata *rlep, int rle_index,
  2998. struct hpsa_scsi_dev_t *encl_dev)
  2999. {
  3000. int rc = -1;
  3001. struct CommandList *c = NULL;
  3002. struct ErrorInfo *ei = NULL;
  3003. struct bmic_sense_storage_box_params *bssbp = NULL;
  3004. struct bmic_identify_physical_device *id_phys = NULL;
  3005. struct ext_report_lun_entry *rle = &rlep->LUN[rle_index];
  3006. u16 bmic_device_index = 0;
  3007. bmic_device_index = GET_BMIC_DRIVE_NUMBER(&rle->lunid[0]);
  3008. if (bmic_device_index == 0xFF00 || MASKED_DEVICE(&rle->lunid[0])) {
  3009. rc = IO_OK;
  3010. goto out;
  3011. }
  3012. bssbp = kzalloc(sizeof(*bssbp), GFP_KERNEL);
  3013. if (!bssbp)
  3014. goto out;
  3015. id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
  3016. if (!id_phys)
  3017. goto out;
  3018. rc = hpsa_bmic_id_physical_device(h, scsi3addr, bmic_device_index,
  3019. id_phys, sizeof(*id_phys));
  3020. if (rc) {
  3021. dev_warn(&h->pdev->dev, "%s: id_phys failed %d bdi[0x%x]\n",
  3022. __func__, encl_dev->external, bmic_device_index);
  3023. goto out;
  3024. }
  3025. c = cmd_alloc(h);
  3026. rc = fill_cmd(c, BMIC_SENSE_STORAGE_BOX_PARAMS, h, bssbp,
  3027. sizeof(*bssbp), 0, RAID_CTLR_LUNID, TYPE_CMD);
  3028. if (rc)
  3029. goto out;
  3030. if (id_phys->phys_connector[1] == 'E')
  3031. c->Request.CDB[5] = id_phys->box_index;
  3032. else
  3033. c->Request.CDB[5] = 0;
  3034. rc = hpsa_scsi_do_simple_cmd_with_retry(h, c, PCI_DMA_FROMDEVICE,
  3035. DEFAULT_TIMEOUT);
  3036. if (rc)
  3037. goto out;
  3038. ei = c->err_info;
  3039. if (ei->CommandStatus != 0 && ei->CommandStatus != CMD_DATA_UNDERRUN) {
  3040. rc = -1;
  3041. goto out;
  3042. }
  3043. encl_dev->box[id_phys->active_path_number] = bssbp->phys_box_on_port;
  3044. memcpy(&encl_dev->phys_connector[id_phys->active_path_number],
  3045. bssbp->phys_connector, sizeof(bssbp->phys_connector));
  3046. rc = IO_OK;
  3047. out:
  3048. kfree(bssbp);
  3049. kfree(id_phys);
  3050. if (c)
  3051. cmd_free(h, c);
  3052. if (rc != IO_OK)
  3053. hpsa_show_dev_msg(KERN_INFO, h, encl_dev,
  3054. "Error, could not get enclosure information\n");
  3055. }
  3056. static u64 hpsa_get_sas_address_from_report_physical(struct ctlr_info *h,
  3057. unsigned char *scsi3addr)
  3058. {
  3059. struct ReportExtendedLUNdata *physdev;
  3060. u32 nphysicals;
  3061. u64 sa = 0;
  3062. int i;
  3063. physdev = kzalloc(sizeof(*physdev), GFP_KERNEL);
  3064. if (!physdev)
  3065. return 0;
  3066. if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
  3067. dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
  3068. kfree(physdev);
  3069. return 0;
  3070. }
  3071. nphysicals = get_unaligned_be32(physdev->LUNListLength) / 24;
  3072. for (i = 0; i < nphysicals; i++)
  3073. if (!memcmp(&physdev->LUN[i].lunid[0], scsi3addr, 8)) {
  3074. sa = get_unaligned_be64(&physdev->LUN[i].wwid[0]);
  3075. break;
  3076. }
  3077. kfree(physdev);
  3078. return sa;
  3079. }
  3080. static void hpsa_get_sas_address(struct ctlr_info *h, unsigned char *scsi3addr,
  3081. struct hpsa_scsi_dev_t *dev)
  3082. {
  3083. int rc;
  3084. u64 sa = 0;
  3085. if (is_hba_lunid(scsi3addr)) {
  3086. struct bmic_sense_subsystem_info *ssi;
  3087. ssi = kzalloc(sizeof(*ssi), GFP_KERNEL);
  3088. if (ssi == NULL) {
  3089. dev_warn(&h->pdev->dev,
  3090. "%s: out of memory\n", __func__);
  3091. return;
  3092. }
  3093. rc = hpsa_bmic_sense_subsystem_information(h,
  3094. scsi3addr, 0, ssi, sizeof(*ssi));
  3095. if (rc == 0) {
  3096. sa = get_unaligned_be64(ssi->primary_world_wide_id);
  3097. h->sas_address = sa;
  3098. }
  3099. kfree(ssi);
  3100. } else
  3101. sa = hpsa_get_sas_address_from_report_physical(h, scsi3addr);
  3102. dev->sas_address = sa;
  3103. }
  3104. /* Get a device id from inquiry page 0x83 */
  3105. static bool hpsa_vpd_page_supported(struct ctlr_info *h,
  3106. unsigned char scsi3addr[], u8 page)
  3107. {
  3108. int rc;
  3109. int i;
  3110. int pages;
  3111. unsigned char *buf, bufsize;
  3112. buf = kzalloc(256, GFP_KERNEL);
  3113. if (!buf)
  3114. return false;
  3115. /* Get the size of the page list first */
  3116. rc = hpsa_scsi_do_inquiry(h, scsi3addr,
  3117. VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
  3118. buf, HPSA_VPD_HEADER_SZ);
  3119. if (rc != 0)
  3120. goto exit_unsupported;
  3121. pages = buf[3];
  3122. if ((pages + HPSA_VPD_HEADER_SZ) <= 255)
  3123. bufsize = pages + HPSA_VPD_HEADER_SZ;
  3124. else
  3125. bufsize = 255;
  3126. /* Get the whole VPD page list */
  3127. rc = hpsa_scsi_do_inquiry(h, scsi3addr,
  3128. VPD_PAGE | HPSA_VPD_SUPPORTED_PAGES,
  3129. buf, bufsize);
  3130. if (rc != 0)
  3131. goto exit_unsupported;
  3132. pages = buf[3];
  3133. for (i = 1; i <= pages; i++)
  3134. if (buf[3 + i] == page)
  3135. goto exit_supported;
  3136. exit_unsupported:
  3137. kfree(buf);
  3138. return false;
  3139. exit_supported:
  3140. kfree(buf);
  3141. return true;
  3142. }
  3143. static void hpsa_get_ioaccel_status(struct ctlr_info *h,
  3144. unsigned char *scsi3addr, struct hpsa_scsi_dev_t *this_device)
  3145. {
  3146. int rc;
  3147. unsigned char *buf;
  3148. u8 ioaccel_status;
  3149. this_device->offload_config = 0;
  3150. this_device->offload_enabled = 0;
  3151. this_device->offload_to_be_enabled = 0;
  3152. buf = kzalloc(64, GFP_KERNEL);
  3153. if (!buf)
  3154. return;
  3155. if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_IOACCEL_STATUS))
  3156. goto out;
  3157. rc = hpsa_scsi_do_inquiry(h, scsi3addr,
  3158. VPD_PAGE | HPSA_VPD_LV_IOACCEL_STATUS, buf, 64);
  3159. if (rc != 0)
  3160. goto out;
  3161. #define IOACCEL_STATUS_BYTE 4
  3162. #define OFFLOAD_CONFIGURED_BIT 0x01
  3163. #define OFFLOAD_ENABLED_BIT 0x02
  3164. ioaccel_status = buf[IOACCEL_STATUS_BYTE];
  3165. this_device->offload_config =
  3166. !!(ioaccel_status & OFFLOAD_CONFIGURED_BIT);
  3167. if (this_device->offload_config) {
  3168. this_device->offload_enabled =
  3169. !!(ioaccel_status & OFFLOAD_ENABLED_BIT);
  3170. if (hpsa_get_raid_map(h, scsi3addr, this_device))
  3171. this_device->offload_enabled = 0;
  3172. }
  3173. this_device->offload_to_be_enabled = this_device->offload_enabled;
  3174. out:
  3175. kfree(buf);
  3176. return;
  3177. }
  3178. /* Get the device id from inquiry page 0x83 */
  3179. static int hpsa_get_device_id(struct ctlr_info *h, unsigned char *scsi3addr,
  3180. unsigned char *device_id, int index, int buflen)
  3181. {
  3182. int rc;
  3183. unsigned char *buf;
  3184. /* Does controller have VPD for device id? */
  3185. if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_DEVICE_ID))
  3186. return 1; /* not supported */
  3187. buf = kzalloc(64, GFP_KERNEL);
  3188. if (!buf)
  3189. return -ENOMEM;
  3190. rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE |
  3191. HPSA_VPD_LV_DEVICE_ID, buf, 64);
  3192. if (rc == 0) {
  3193. if (buflen > 16)
  3194. buflen = 16;
  3195. memcpy(device_id, &buf[8], buflen);
  3196. }
  3197. kfree(buf);
  3198. return rc; /*0 - got id, otherwise, didn't */
  3199. }
  3200. static int hpsa_scsi_do_report_luns(struct ctlr_info *h, int logical,
  3201. void *buf, int bufsize,
  3202. int extended_response)
  3203. {
  3204. int rc = IO_OK;
  3205. struct CommandList *c;
  3206. unsigned char scsi3addr[8];
  3207. struct ErrorInfo *ei;
  3208. c = cmd_alloc(h);
  3209. /* address the controller */
  3210. memset(scsi3addr, 0, sizeof(scsi3addr));
  3211. if (fill_cmd(c, logical ? HPSA_REPORT_LOG : HPSA_REPORT_PHYS, h,
  3212. buf, bufsize, 0, scsi3addr, TYPE_CMD)) {
  3213. rc = -1;
  3214. goto out;
  3215. }
  3216. if (extended_response)
  3217. c->Request.CDB[1] = extended_response;
  3218. rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
  3219. PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
  3220. if (rc)
  3221. goto out;
  3222. ei = c->err_info;
  3223. if (ei->CommandStatus != 0 &&
  3224. ei->CommandStatus != CMD_DATA_UNDERRUN) {
  3225. hpsa_scsi_interpret_error(h, c);
  3226. rc = -1;
  3227. } else {
  3228. struct ReportLUNdata *rld = buf;
  3229. if (rld->extended_response_flag != extended_response) {
  3230. dev_err(&h->pdev->dev,
  3231. "report luns requested format %u, got %u\n",
  3232. extended_response,
  3233. rld->extended_response_flag);
  3234. rc = -1;
  3235. }
  3236. }
  3237. out:
  3238. cmd_free(h, c);
  3239. return rc;
  3240. }
  3241. static inline int hpsa_scsi_do_report_phys_luns(struct ctlr_info *h,
  3242. struct ReportExtendedLUNdata *buf, int bufsize)
  3243. {
  3244. return hpsa_scsi_do_report_luns(h, 0, buf, bufsize,
  3245. HPSA_REPORT_PHYS_EXTENDED);
  3246. }
  3247. static inline int hpsa_scsi_do_report_log_luns(struct ctlr_info *h,
  3248. struct ReportLUNdata *buf, int bufsize)
  3249. {
  3250. return hpsa_scsi_do_report_luns(h, 1, buf, bufsize, 0);
  3251. }
  3252. static inline void hpsa_set_bus_target_lun(struct hpsa_scsi_dev_t *device,
  3253. int bus, int target, int lun)
  3254. {
  3255. device->bus = bus;
  3256. device->target = target;
  3257. device->lun = lun;
  3258. }
  3259. /* Use VPD inquiry to get details of volume status */
  3260. static int hpsa_get_volume_status(struct ctlr_info *h,
  3261. unsigned char scsi3addr[])
  3262. {
  3263. int rc;
  3264. int status;
  3265. int size;
  3266. unsigned char *buf;
  3267. buf = kzalloc(64, GFP_KERNEL);
  3268. if (!buf)
  3269. return HPSA_VPD_LV_STATUS_UNSUPPORTED;
  3270. /* Does controller have VPD for logical volume status? */
  3271. if (!hpsa_vpd_page_supported(h, scsi3addr, HPSA_VPD_LV_STATUS))
  3272. goto exit_failed;
  3273. /* Get the size of the VPD return buffer */
  3274. rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
  3275. buf, HPSA_VPD_HEADER_SZ);
  3276. if (rc != 0)
  3277. goto exit_failed;
  3278. size = buf[3];
  3279. /* Now get the whole VPD buffer */
  3280. rc = hpsa_scsi_do_inquiry(h, scsi3addr, VPD_PAGE | HPSA_VPD_LV_STATUS,
  3281. buf, size + HPSA_VPD_HEADER_SZ);
  3282. if (rc != 0)
  3283. goto exit_failed;
  3284. status = buf[4]; /* status byte */
  3285. kfree(buf);
  3286. return status;
  3287. exit_failed:
  3288. kfree(buf);
  3289. return HPSA_VPD_LV_STATUS_UNSUPPORTED;
  3290. }
  3291. /* Determine offline status of a volume.
  3292. * Return either:
  3293. * 0 (not offline)
  3294. * 0xff (offline for unknown reasons)
  3295. * # (integer code indicating one of several NOT READY states
  3296. * describing why a volume is to be kept offline)
  3297. */
  3298. static int hpsa_volume_offline(struct ctlr_info *h,
  3299. unsigned char scsi3addr[])
  3300. {
  3301. struct CommandList *c;
  3302. unsigned char *sense;
  3303. u8 sense_key, asc, ascq;
  3304. int sense_len;
  3305. int rc, ldstat = 0;
  3306. u16 cmd_status;
  3307. u8 scsi_status;
  3308. #define ASC_LUN_NOT_READY 0x04
  3309. #define ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS 0x04
  3310. #define ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ 0x02
  3311. c = cmd_alloc(h);
  3312. (void) fill_cmd(c, TEST_UNIT_READY, h, NULL, 0, 0, scsi3addr, TYPE_CMD);
  3313. rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
  3314. DEFAULT_TIMEOUT);
  3315. if (rc) {
  3316. cmd_free(h, c);
  3317. return 0;
  3318. }
  3319. sense = c->err_info->SenseInfo;
  3320. if (c->err_info->SenseLen > sizeof(c->err_info->SenseInfo))
  3321. sense_len = sizeof(c->err_info->SenseInfo);
  3322. else
  3323. sense_len = c->err_info->SenseLen;
  3324. decode_sense_data(sense, sense_len, &sense_key, &asc, &ascq);
  3325. cmd_status = c->err_info->CommandStatus;
  3326. scsi_status = c->err_info->ScsiStatus;
  3327. cmd_free(h, c);
  3328. /* Is the volume 'not ready'? */
  3329. if (cmd_status != CMD_TARGET_STATUS ||
  3330. scsi_status != SAM_STAT_CHECK_CONDITION ||
  3331. sense_key != NOT_READY ||
  3332. asc != ASC_LUN_NOT_READY) {
  3333. return 0;
  3334. }
  3335. /* Determine the reason for not ready state */
  3336. ldstat = hpsa_get_volume_status(h, scsi3addr);
  3337. /* Keep volume offline in certain cases: */
  3338. switch (ldstat) {
  3339. case HPSA_LV_UNDERGOING_ERASE:
  3340. case HPSA_LV_NOT_AVAILABLE:
  3341. case HPSA_LV_UNDERGOING_RPI:
  3342. case HPSA_LV_PENDING_RPI:
  3343. case HPSA_LV_ENCRYPTED_NO_KEY:
  3344. case HPSA_LV_PLAINTEXT_IN_ENCRYPT_ONLY_CONTROLLER:
  3345. case HPSA_LV_UNDERGOING_ENCRYPTION:
  3346. case HPSA_LV_UNDERGOING_ENCRYPTION_REKEYING:
  3347. case HPSA_LV_ENCRYPTED_IN_NON_ENCRYPTED_CONTROLLER:
  3348. return ldstat;
  3349. case HPSA_VPD_LV_STATUS_UNSUPPORTED:
  3350. /* If VPD status page isn't available,
  3351. * use ASC/ASCQ to determine state
  3352. */
  3353. if ((ascq == ASCQ_LUN_NOT_READY_FORMAT_IN_PROGRESS) ||
  3354. (ascq == ASCQ_LUN_NOT_READY_INITIALIZING_CMD_REQ))
  3355. return ldstat;
  3356. break;
  3357. default:
  3358. break;
  3359. }
  3360. return 0;
  3361. }
  3362. /*
  3363. * Find out if a logical device supports aborts by simply trying one.
  3364. * Smart Array may claim not to support aborts on logical drives, but
  3365. * if a MSA2000 * is connected, the drives on that will be presented
  3366. * by the Smart Array as logical drives, and aborts may be sent to
  3367. * those devices successfully. So the simplest way to find out is
  3368. * to simply try an abort and see how the device responds.
  3369. */
  3370. static int hpsa_device_supports_aborts(struct ctlr_info *h,
  3371. unsigned char *scsi3addr)
  3372. {
  3373. struct CommandList *c;
  3374. struct ErrorInfo *ei;
  3375. int rc = 0;
  3376. u64 tag = (u64) -1; /* bogus tag */
  3377. /* Assume that physical devices support aborts */
  3378. if (!is_logical_dev_addr_mode(scsi3addr))
  3379. return 1;
  3380. c = cmd_alloc(h);
  3381. (void) fill_cmd(c, HPSA_ABORT_MSG, h, &tag, 0, 0, scsi3addr, TYPE_MSG);
  3382. (void) hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
  3383. DEFAULT_TIMEOUT);
  3384. /* no unmap needed here because no data xfer. */
  3385. ei = c->err_info;
  3386. switch (ei->CommandStatus) {
  3387. case CMD_INVALID:
  3388. rc = 0;
  3389. break;
  3390. case CMD_UNABORTABLE:
  3391. case CMD_ABORT_FAILED:
  3392. rc = 1;
  3393. break;
  3394. case CMD_TMF_STATUS:
  3395. rc = hpsa_evaluate_tmf_status(h, c);
  3396. break;
  3397. default:
  3398. rc = 0;
  3399. break;
  3400. }
  3401. cmd_free(h, c);
  3402. return rc;
  3403. }
  3404. static int hpsa_update_device_info(struct ctlr_info *h,
  3405. unsigned char scsi3addr[], struct hpsa_scsi_dev_t *this_device,
  3406. unsigned char *is_OBDR_device)
  3407. {
  3408. #define OBDR_SIG_OFFSET 43
  3409. #define OBDR_TAPE_SIG "$DR-10"
  3410. #define OBDR_SIG_LEN (sizeof(OBDR_TAPE_SIG) - 1)
  3411. #define OBDR_TAPE_INQ_SIZE (OBDR_SIG_OFFSET + OBDR_SIG_LEN)
  3412. unsigned char *inq_buff;
  3413. unsigned char *obdr_sig;
  3414. int rc = 0;
  3415. inq_buff = kzalloc(OBDR_TAPE_INQ_SIZE, GFP_KERNEL);
  3416. if (!inq_buff) {
  3417. rc = -ENOMEM;
  3418. goto bail_out;
  3419. }
  3420. /* Do an inquiry to the device to see what it is. */
  3421. if (hpsa_scsi_do_inquiry(h, scsi3addr, 0, inq_buff,
  3422. (unsigned char) OBDR_TAPE_INQ_SIZE) != 0) {
  3423. /* Inquiry failed (msg printed already) */
  3424. dev_err(&h->pdev->dev,
  3425. "hpsa_update_device_info: inquiry failed\n");
  3426. rc = -EIO;
  3427. goto bail_out;
  3428. }
  3429. scsi_sanitize_inquiry_string(&inq_buff[8], 8);
  3430. scsi_sanitize_inquiry_string(&inq_buff[16], 16);
  3431. this_device->devtype = (inq_buff[0] & 0x1f);
  3432. memcpy(this_device->scsi3addr, scsi3addr, 8);
  3433. memcpy(this_device->vendor, &inq_buff[8],
  3434. sizeof(this_device->vendor));
  3435. memcpy(this_device->model, &inq_buff[16],
  3436. sizeof(this_device->model));
  3437. memset(this_device->device_id, 0,
  3438. sizeof(this_device->device_id));
  3439. if (hpsa_get_device_id(h, scsi3addr, this_device->device_id, 8,
  3440. sizeof(this_device->device_id)))
  3441. dev_err(&h->pdev->dev,
  3442. "hpsa%d: %s: can't get device id for host %d:C0:T%d:L%d\t%s\t%.16s\n",
  3443. h->ctlr, __func__,
  3444. h->scsi_host->host_no,
  3445. this_device->target, this_device->lun,
  3446. scsi_device_type(this_device->devtype),
  3447. this_device->model);
  3448. if ((this_device->devtype == TYPE_DISK ||
  3449. this_device->devtype == TYPE_ZBC) &&
  3450. is_logical_dev_addr_mode(scsi3addr)) {
  3451. int volume_offline;
  3452. hpsa_get_raid_level(h, scsi3addr, &this_device->raid_level);
  3453. if (h->fw_support & MISC_FW_RAID_OFFLOAD_BASIC)
  3454. hpsa_get_ioaccel_status(h, scsi3addr, this_device);
  3455. volume_offline = hpsa_volume_offline(h, scsi3addr);
  3456. if (volume_offline < 0 || volume_offline > 0xff)
  3457. volume_offline = HPSA_VPD_LV_STATUS_UNSUPPORTED;
  3458. this_device->volume_offline = volume_offline & 0xff;
  3459. } else {
  3460. this_device->raid_level = RAID_UNKNOWN;
  3461. this_device->offload_config = 0;
  3462. this_device->offload_enabled = 0;
  3463. this_device->offload_to_be_enabled = 0;
  3464. this_device->hba_ioaccel_enabled = 0;
  3465. this_device->volume_offline = 0;
  3466. this_device->queue_depth = h->nr_cmds;
  3467. }
  3468. if (is_OBDR_device) {
  3469. /* See if this is a One-Button-Disaster-Recovery device
  3470. * by looking for "$DR-10" at offset 43 in inquiry data.
  3471. */
  3472. obdr_sig = &inq_buff[OBDR_SIG_OFFSET];
  3473. *is_OBDR_device = (this_device->devtype == TYPE_ROM &&
  3474. strncmp(obdr_sig, OBDR_TAPE_SIG,
  3475. OBDR_SIG_LEN) == 0);
  3476. }
  3477. kfree(inq_buff);
  3478. return 0;
  3479. bail_out:
  3480. kfree(inq_buff);
  3481. return rc;
  3482. }
  3483. static void hpsa_update_device_supports_aborts(struct ctlr_info *h,
  3484. struct hpsa_scsi_dev_t *dev, u8 *scsi3addr)
  3485. {
  3486. unsigned long flags;
  3487. int rc, entry;
  3488. /*
  3489. * See if this device supports aborts. If we already know
  3490. * the device, we already know if it supports aborts, otherwise
  3491. * we have to find out if it supports aborts by trying one.
  3492. */
  3493. spin_lock_irqsave(&h->devlock, flags);
  3494. rc = hpsa_scsi_find_entry(dev, h->dev, h->ndevices, &entry);
  3495. if ((rc == DEVICE_SAME || rc == DEVICE_UPDATED) &&
  3496. entry >= 0 && entry < h->ndevices) {
  3497. dev->supports_aborts = h->dev[entry]->supports_aborts;
  3498. spin_unlock_irqrestore(&h->devlock, flags);
  3499. } else {
  3500. spin_unlock_irqrestore(&h->devlock, flags);
  3501. dev->supports_aborts =
  3502. hpsa_device_supports_aborts(h, scsi3addr);
  3503. if (dev->supports_aborts < 0)
  3504. dev->supports_aborts = 0;
  3505. }
  3506. }
  3507. /*
  3508. * Helper function to assign bus, target, lun mapping of devices.
  3509. * Logical drive target and lun are assigned at this time, but
  3510. * physical device lun and target assignment are deferred (assigned
  3511. * in hpsa_find_target_lun, called by hpsa_scsi_add_entry.)
  3512. */
  3513. static void figure_bus_target_lun(struct ctlr_info *h,
  3514. u8 *lunaddrbytes, struct hpsa_scsi_dev_t *device)
  3515. {
  3516. u32 lunid = get_unaligned_le32(lunaddrbytes);
  3517. if (!is_logical_dev_addr_mode(lunaddrbytes)) {
  3518. /* physical device, target and lun filled in later */
  3519. if (is_hba_lunid(lunaddrbytes))
  3520. hpsa_set_bus_target_lun(device,
  3521. HPSA_HBA_BUS, 0, lunid & 0x3fff);
  3522. else
  3523. /* defer target, lun assignment for physical devices */
  3524. hpsa_set_bus_target_lun(device,
  3525. HPSA_PHYSICAL_DEVICE_BUS, -1, -1);
  3526. return;
  3527. }
  3528. /* It's a logical device */
  3529. if (device->external) {
  3530. hpsa_set_bus_target_lun(device,
  3531. HPSA_EXTERNAL_RAID_VOLUME_BUS, (lunid >> 16) & 0x3fff,
  3532. lunid & 0x00ff);
  3533. return;
  3534. }
  3535. hpsa_set_bus_target_lun(device, HPSA_RAID_VOLUME_BUS,
  3536. 0, lunid & 0x3fff);
  3537. }
  3538. /*
  3539. * Get address of physical disk used for an ioaccel2 mode command:
  3540. * 1. Extract ioaccel2 handle from the command.
  3541. * 2. Find a matching ioaccel2 handle from list of physical disks.
  3542. * 3. Return:
  3543. * 1 and set scsi3addr to address of matching physical
  3544. * 0 if no matching physical disk was found.
  3545. */
  3546. static int hpsa_get_pdisk_of_ioaccel2(struct ctlr_info *h,
  3547. struct CommandList *ioaccel2_cmd_to_abort, unsigned char *scsi3addr)
  3548. {
  3549. struct io_accel2_cmd *c2 =
  3550. &h->ioaccel2_cmd_pool[ioaccel2_cmd_to_abort->cmdindex];
  3551. unsigned long flags;
  3552. int i;
  3553. spin_lock_irqsave(&h->devlock, flags);
  3554. for (i = 0; i < h->ndevices; i++)
  3555. if (h->dev[i]->ioaccel_handle == le32_to_cpu(c2->scsi_nexus)) {
  3556. memcpy(scsi3addr, h->dev[i]->scsi3addr,
  3557. sizeof(h->dev[i]->scsi3addr));
  3558. spin_unlock_irqrestore(&h->devlock, flags);
  3559. return 1;
  3560. }
  3561. spin_unlock_irqrestore(&h->devlock, flags);
  3562. return 0;
  3563. }
  3564. static int figure_external_status(struct ctlr_info *h, int raid_ctlr_position,
  3565. int i, int nphysicals, int nlocal_logicals)
  3566. {
  3567. /* In report logicals, local logicals are listed first,
  3568. * then any externals.
  3569. */
  3570. int logicals_start = nphysicals + (raid_ctlr_position == 0);
  3571. if (i == raid_ctlr_position)
  3572. return 0;
  3573. if (i < logicals_start)
  3574. return 0;
  3575. /* i is in logicals range, but still within local logicals */
  3576. if ((i - nphysicals - (raid_ctlr_position == 0)) < nlocal_logicals)
  3577. return 0;
  3578. return 1; /* it's an external lun */
  3579. }
  3580. /*
  3581. * Do CISS_REPORT_PHYS and CISS_REPORT_LOG. Data is returned in physdev,
  3582. * logdev. The number of luns in physdev and logdev are returned in
  3583. * *nphysicals and *nlogicals, respectively.
  3584. * Returns 0 on success, -1 otherwise.
  3585. */
  3586. static int hpsa_gather_lun_info(struct ctlr_info *h,
  3587. struct ReportExtendedLUNdata *physdev, u32 *nphysicals,
  3588. struct ReportLUNdata *logdev, u32 *nlogicals)
  3589. {
  3590. if (hpsa_scsi_do_report_phys_luns(h, physdev, sizeof(*physdev))) {
  3591. dev_err(&h->pdev->dev, "report physical LUNs failed.\n");
  3592. return -1;
  3593. }
  3594. *nphysicals = be32_to_cpu(*((__be32 *)physdev->LUNListLength)) / 24;
  3595. if (*nphysicals > HPSA_MAX_PHYS_LUN) {
  3596. dev_warn(&h->pdev->dev, "maximum physical LUNs (%d) exceeded. %d LUNs ignored.\n",
  3597. HPSA_MAX_PHYS_LUN, *nphysicals - HPSA_MAX_PHYS_LUN);
  3598. *nphysicals = HPSA_MAX_PHYS_LUN;
  3599. }
  3600. if (hpsa_scsi_do_report_log_luns(h, logdev, sizeof(*logdev))) {
  3601. dev_err(&h->pdev->dev, "report logical LUNs failed.\n");
  3602. return -1;
  3603. }
  3604. *nlogicals = be32_to_cpu(*((__be32 *) logdev->LUNListLength)) / 8;
  3605. /* Reject Logicals in excess of our max capability. */
  3606. if (*nlogicals > HPSA_MAX_LUN) {
  3607. dev_warn(&h->pdev->dev,
  3608. "maximum logical LUNs (%d) exceeded. "
  3609. "%d LUNs ignored.\n", HPSA_MAX_LUN,
  3610. *nlogicals - HPSA_MAX_LUN);
  3611. *nlogicals = HPSA_MAX_LUN;
  3612. }
  3613. if (*nlogicals + *nphysicals > HPSA_MAX_PHYS_LUN) {
  3614. dev_warn(&h->pdev->dev,
  3615. "maximum logical + physical LUNs (%d) exceeded. "
  3616. "%d LUNs ignored.\n", HPSA_MAX_PHYS_LUN,
  3617. *nphysicals + *nlogicals - HPSA_MAX_PHYS_LUN);
  3618. *nlogicals = HPSA_MAX_PHYS_LUN - *nphysicals;
  3619. }
  3620. return 0;
  3621. }
  3622. static u8 *figure_lunaddrbytes(struct ctlr_info *h, int raid_ctlr_position,
  3623. int i, int nphysicals, int nlogicals,
  3624. struct ReportExtendedLUNdata *physdev_list,
  3625. struct ReportLUNdata *logdev_list)
  3626. {
  3627. /* Helper function, figure out where the LUN ID info is coming from
  3628. * given index i, lists of physical and logical devices, where in
  3629. * the list the raid controller is supposed to appear (first or last)
  3630. */
  3631. int logicals_start = nphysicals + (raid_ctlr_position == 0);
  3632. int last_device = nphysicals + nlogicals + (raid_ctlr_position == 0);
  3633. if (i == raid_ctlr_position)
  3634. return RAID_CTLR_LUNID;
  3635. if (i < logicals_start)
  3636. return &physdev_list->LUN[i -
  3637. (raid_ctlr_position == 0)].lunid[0];
  3638. if (i < last_device)
  3639. return &logdev_list->LUN[i - nphysicals -
  3640. (raid_ctlr_position == 0)][0];
  3641. BUG();
  3642. return NULL;
  3643. }
  3644. /* get physical drive ioaccel handle and queue depth */
  3645. static void hpsa_get_ioaccel_drive_info(struct ctlr_info *h,
  3646. struct hpsa_scsi_dev_t *dev,
  3647. struct ReportExtendedLUNdata *rlep, int rle_index,
  3648. struct bmic_identify_physical_device *id_phys)
  3649. {
  3650. int rc;
  3651. struct ext_report_lun_entry *rle;
  3652. /*
  3653. * external targets don't support BMIC
  3654. */
  3655. if (dev->external) {
  3656. dev->queue_depth = 7;
  3657. return;
  3658. }
  3659. rle = &rlep->LUN[rle_index];
  3660. dev->ioaccel_handle = rle->ioaccel_handle;
  3661. if ((rle->device_flags & 0x08) && dev->ioaccel_handle)
  3662. dev->hba_ioaccel_enabled = 1;
  3663. memset(id_phys, 0, sizeof(*id_phys));
  3664. rc = hpsa_bmic_id_physical_device(h, &rle->lunid[0],
  3665. GET_BMIC_DRIVE_NUMBER(&rle->lunid[0]), id_phys,
  3666. sizeof(*id_phys));
  3667. if (!rc)
  3668. /* Reserve space for FW operations */
  3669. #define DRIVE_CMDS_RESERVED_FOR_FW 2
  3670. #define DRIVE_QUEUE_DEPTH 7
  3671. dev->queue_depth =
  3672. le16_to_cpu(id_phys->current_queue_depth_limit) -
  3673. DRIVE_CMDS_RESERVED_FOR_FW;
  3674. else
  3675. dev->queue_depth = DRIVE_QUEUE_DEPTH; /* conservative */
  3676. }
  3677. static void hpsa_get_path_info(struct hpsa_scsi_dev_t *this_device,
  3678. struct ReportExtendedLUNdata *rlep, int rle_index,
  3679. struct bmic_identify_physical_device *id_phys)
  3680. {
  3681. struct ext_report_lun_entry *rle = &rlep->LUN[rle_index];
  3682. if ((rle->device_flags & 0x08) && this_device->ioaccel_handle)
  3683. this_device->hba_ioaccel_enabled = 1;
  3684. memcpy(&this_device->active_path_index,
  3685. &id_phys->active_path_number,
  3686. sizeof(this_device->active_path_index));
  3687. memcpy(&this_device->path_map,
  3688. &id_phys->redundant_path_present_map,
  3689. sizeof(this_device->path_map));
  3690. memcpy(&this_device->box,
  3691. &id_phys->alternate_paths_phys_box_on_port,
  3692. sizeof(this_device->box));
  3693. memcpy(&this_device->phys_connector,
  3694. &id_phys->alternate_paths_phys_connector,
  3695. sizeof(this_device->phys_connector));
  3696. memcpy(&this_device->bay,
  3697. &id_phys->phys_bay_in_box,
  3698. sizeof(this_device->bay));
  3699. }
  3700. /* get number of local logical disks. */
  3701. static int hpsa_set_local_logical_count(struct ctlr_info *h,
  3702. struct bmic_identify_controller *id_ctlr,
  3703. u32 *nlocals)
  3704. {
  3705. int rc;
  3706. if (!id_ctlr) {
  3707. dev_warn(&h->pdev->dev, "%s: id_ctlr buffer is NULL.\n",
  3708. __func__);
  3709. return -ENOMEM;
  3710. }
  3711. memset(id_ctlr, 0, sizeof(*id_ctlr));
  3712. rc = hpsa_bmic_id_controller(h, id_ctlr, sizeof(*id_ctlr));
  3713. if (!rc)
  3714. if (id_ctlr->configured_logical_drive_count < 256)
  3715. *nlocals = id_ctlr->configured_logical_drive_count;
  3716. else
  3717. *nlocals = le16_to_cpu(
  3718. id_ctlr->extended_logical_unit_count);
  3719. else
  3720. *nlocals = -1;
  3721. return rc;
  3722. }
  3723. static bool hpsa_is_disk_spare(struct ctlr_info *h, u8 *lunaddrbytes)
  3724. {
  3725. struct bmic_identify_physical_device *id_phys;
  3726. bool is_spare = false;
  3727. int rc;
  3728. id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
  3729. if (!id_phys)
  3730. return false;
  3731. rc = hpsa_bmic_id_physical_device(h,
  3732. lunaddrbytes,
  3733. GET_BMIC_DRIVE_NUMBER(lunaddrbytes),
  3734. id_phys, sizeof(*id_phys));
  3735. if (rc == 0)
  3736. is_spare = (id_phys->more_flags >> 6) & 0x01;
  3737. kfree(id_phys);
  3738. return is_spare;
  3739. }
  3740. #define RPL_DEV_FLAG_NON_DISK 0x1
  3741. #define RPL_DEV_FLAG_UNCONFIG_DISK_REPORTING_SUPPORTED 0x2
  3742. #define RPL_DEV_FLAG_UNCONFIG_DISK 0x4
  3743. #define BMIC_DEVICE_TYPE_ENCLOSURE 6
  3744. static bool hpsa_skip_device(struct ctlr_info *h, u8 *lunaddrbytes,
  3745. struct ext_report_lun_entry *rle)
  3746. {
  3747. u8 device_flags;
  3748. u8 device_type;
  3749. if (!MASKED_DEVICE(lunaddrbytes))
  3750. return false;
  3751. device_flags = rle->device_flags;
  3752. device_type = rle->device_type;
  3753. if (device_flags & RPL_DEV_FLAG_NON_DISK) {
  3754. if (device_type == BMIC_DEVICE_TYPE_ENCLOSURE)
  3755. return false;
  3756. return true;
  3757. }
  3758. if (!(device_flags & RPL_DEV_FLAG_UNCONFIG_DISK_REPORTING_SUPPORTED))
  3759. return false;
  3760. if (device_flags & RPL_DEV_FLAG_UNCONFIG_DISK)
  3761. return false;
  3762. /*
  3763. * Spares may be spun down, we do not want to
  3764. * do an Inquiry to a RAID set spare drive as
  3765. * that would have them spun up, that is a
  3766. * performance hit because I/O to the RAID device
  3767. * stops while the spin up occurs which can take
  3768. * over 50 seconds.
  3769. */
  3770. if (hpsa_is_disk_spare(h, lunaddrbytes))
  3771. return true;
  3772. return false;
  3773. }
  3774. static void hpsa_update_scsi_devices(struct ctlr_info *h)
  3775. {
  3776. /* the idea here is we could get notified
  3777. * that some devices have changed, so we do a report
  3778. * physical luns and report logical luns cmd, and adjust
  3779. * our list of devices accordingly.
  3780. *
  3781. * The scsi3addr's of devices won't change so long as the
  3782. * adapter is not reset. That means we can rescan and
  3783. * tell which devices we already know about, vs. new
  3784. * devices, vs. disappearing devices.
  3785. */
  3786. struct ReportExtendedLUNdata *physdev_list = NULL;
  3787. struct ReportLUNdata *logdev_list = NULL;
  3788. struct bmic_identify_physical_device *id_phys = NULL;
  3789. struct bmic_identify_controller *id_ctlr = NULL;
  3790. u32 nphysicals = 0;
  3791. u32 nlogicals = 0;
  3792. u32 nlocal_logicals = 0;
  3793. u32 ndev_allocated = 0;
  3794. struct hpsa_scsi_dev_t **currentsd, *this_device, *tmpdevice;
  3795. int ncurrent = 0;
  3796. int i, n_ext_target_devs, ndevs_to_allocate;
  3797. int raid_ctlr_position;
  3798. bool physical_device;
  3799. DECLARE_BITMAP(lunzerobits, MAX_EXT_TARGETS);
  3800. currentsd = kzalloc(sizeof(*currentsd) * HPSA_MAX_DEVICES, GFP_KERNEL);
  3801. physdev_list = kzalloc(sizeof(*physdev_list), GFP_KERNEL);
  3802. logdev_list = kzalloc(sizeof(*logdev_list), GFP_KERNEL);
  3803. tmpdevice = kzalloc(sizeof(*tmpdevice), GFP_KERNEL);
  3804. id_phys = kzalloc(sizeof(*id_phys), GFP_KERNEL);
  3805. id_ctlr = kzalloc(sizeof(*id_ctlr), GFP_KERNEL);
  3806. if (!currentsd || !physdev_list || !logdev_list ||
  3807. !tmpdevice || !id_phys || !id_ctlr) {
  3808. dev_err(&h->pdev->dev, "out of memory\n");
  3809. goto out;
  3810. }
  3811. memset(lunzerobits, 0, sizeof(lunzerobits));
  3812. h->drv_req_rescan = 0; /* cancel scheduled rescan - we're doing it. */
  3813. if (hpsa_gather_lun_info(h, physdev_list, &nphysicals,
  3814. logdev_list, &nlogicals)) {
  3815. h->drv_req_rescan = 1;
  3816. goto out;
  3817. }
  3818. /* Set number of local logicals (non PTRAID) */
  3819. if (hpsa_set_local_logical_count(h, id_ctlr, &nlocal_logicals)) {
  3820. dev_warn(&h->pdev->dev,
  3821. "%s: Can't determine number of local logical devices.\n",
  3822. __func__);
  3823. }
  3824. /* We might see up to the maximum number of logical and physical disks
  3825. * plus external target devices, and a device for the local RAID
  3826. * controller.
  3827. */
  3828. ndevs_to_allocate = nphysicals + nlogicals + MAX_EXT_TARGETS + 1;
  3829. /* Allocate the per device structures */
  3830. for (i = 0; i < ndevs_to_allocate; i++) {
  3831. if (i >= HPSA_MAX_DEVICES) {
  3832. dev_warn(&h->pdev->dev, "maximum devices (%d) exceeded."
  3833. " %d devices ignored.\n", HPSA_MAX_DEVICES,
  3834. ndevs_to_allocate - HPSA_MAX_DEVICES);
  3835. break;
  3836. }
  3837. currentsd[i] = kzalloc(sizeof(*currentsd[i]), GFP_KERNEL);
  3838. if (!currentsd[i]) {
  3839. dev_warn(&h->pdev->dev, "out of memory at %s:%d\n",
  3840. __FILE__, __LINE__);
  3841. h->drv_req_rescan = 1;
  3842. goto out;
  3843. }
  3844. ndev_allocated++;
  3845. }
  3846. if (is_scsi_rev_5(h))
  3847. raid_ctlr_position = 0;
  3848. else
  3849. raid_ctlr_position = nphysicals + nlogicals;
  3850. /* adjust our table of devices */
  3851. n_ext_target_devs = 0;
  3852. for (i = 0; i < nphysicals + nlogicals + 1; i++) {
  3853. u8 *lunaddrbytes, is_OBDR = 0;
  3854. int rc = 0;
  3855. int phys_dev_index = i - (raid_ctlr_position == 0);
  3856. bool skip_device = false;
  3857. physical_device = i < nphysicals + (raid_ctlr_position == 0);
  3858. /* Figure out where the LUN ID info is coming from */
  3859. lunaddrbytes = figure_lunaddrbytes(h, raid_ctlr_position,
  3860. i, nphysicals, nlogicals, physdev_list, logdev_list);
  3861. /* Determine if this is a lun from an external target array */
  3862. tmpdevice->external =
  3863. figure_external_status(h, raid_ctlr_position, i,
  3864. nphysicals, nlocal_logicals);
  3865. /*
  3866. * Skip over some devices such as a spare.
  3867. */
  3868. if (!tmpdevice->external && physical_device) {
  3869. skip_device = hpsa_skip_device(h, lunaddrbytes,
  3870. &physdev_list->LUN[phys_dev_index]);
  3871. if (skip_device)
  3872. continue;
  3873. }
  3874. /* Get device type, vendor, model, device id */
  3875. rc = hpsa_update_device_info(h, lunaddrbytes, tmpdevice,
  3876. &is_OBDR);
  3877. if (rc == -ENOMEM) {
  3878. dev_warn(&h->pdev->dev,
  3879. "Out of memory, rescan deferred.\n");
  3880. h->drv_req_rescan = 1;
  3881. goto out;
  3882. }
  3883. if (rc) {
  3884. dev_warn(&h->pdev->dev,
  3885. "Inquiry failed, skipping device.\n");
  3886. continue;
  3887. }
  3888. figure_bus_target_lun(h, lunaddrbytes, tmpdevice);
  3889. hpsa_update_device_supports_aborts(h, tmpdevice, lunaddrbytes);
  3890. this_device = currentsd[ncurrent];
  3891. /* Turn on discovery_polling if there are ext target devices.
  3892. * Event-based change notification is unreliable for those.
  3893. */
  3894. if (!h->discovery_polling) {
  3895. if (tmpdevice->external) {
  3896. h->discovery_polling = 1;
  3897. dev_info(&h->pdev->dev,
  3898. "External target, activate discovery polling.\n");
  3899. }
  3900. }
  3901. *this_device = *tmpdevice;
  3902. this_device->physical_device = physical_device;
  3903. /*
  3904. * Expose all devices except for physical devices that
  3905. * are masked.
  3906. */
  3907. if (MASKED_DEVICE(lunaddrbytes) && this_device->physical_device)
  3908. this_device->expose_device = 0;
  3909. else
  3910. this_device->expose_device = 1;
  3911. /*
  3912. * Get the SAS address for physical devices that are exposed.
  3913. */
  3914. if (this_device->physical_device && this_device->expose_device)
  3915. hpsa_get_sas_address(h, lunaddrbytes, this_device);
  3916. switch (this_device->devtype) {
  3917. case TYPE_ROM:
  3918. /* We don't *really* support actual CD-ROM devices,
  3919. * just "One Button Disaster Recovery" tape drive
  3920. * which temporarily pretends to be a CD-ROM drive.
  3921. * So we check that the device is really an OBDR tape
  3922. * device by checking for "$DR-10" in bytes 43-48 of
  3923. * the inquiry data.
  3924. */
  3925. if (is_OBDR)
  3926. ncurrent++;
  3927. break;
  3928. case TYPE_DISK:
  3929. case TYPE_ZBC:
  3930. if (this_device->physical_device) {
  3931. /* The disk is in HBA mode. */
  3932. /* Never use RAID mapper in HBA mode. */
  3933. this_device->offload_enabled = 0;
  3934. hpsa_get_ioaccel_drive_info(h, this_device,
  3935. physdev_list, phys_dev_index, id_phys);
  3936. hpsa_get_path_info(this_device,
  3937. physdev_list, phys_dev_index, id_phys);
  3938. }
  3939. ncurrent++;
  3940. break;
  3941. case TYPE_TAPE:
  3942. case TYPE_MEDIUM_CHANGER:
  3943. ncurrent++;
  3944. break;
  3945. case TYPE_ENCLOSURE:
  3946. if (!this_device->external)
  3947. hpsa_get_enclosure_info(h, lunaddrbytes,
  3948. physdev_list, phys_dev_index,
  3949. this_device);
  3950. ncurrent++;
  3951. break;
  3952. case TYPE_RAID:
  3953. /* Only present the Smartarray HBA as a RAID controller.
  3954. * If it's a RAID controller other than the HBA itself
  3955. * (an external RAID controller, MSA500 or similar)
  3956. * don't present it.
  3957. */
  3958. if (!is_hba_lunid(lunaddrbytes))
  3959. break;
  3960. ncurrent++;
  3961. break;
  3962. default:
  3963. break;
  3964. }
  3965. if (ncurrent >= HPSA_MAX_DEVICES)
  3966. break;
  3967. }
  3968. if (h->sas_host == NULL) {
  3969. int rc = 0;
  3970. rc = hpsa_add_sas_host(h);
  3971. if (rc) {
  3972. dev_warn(&h->pdev->dev,
  3973. "Could not add sas host %d\n", rc);
  3974. goto out;
  3975. }
  3976. }
  3977. adjust_hpsa_scsi_table(h, currentsd, ncurrent);
  3978. out:
  3979. kfree(tmpdevice);
  3980. for (i = 0; i < ndev_allocated; i++)
  3981. kfree(currentsd[i]);
  3982. kfree(currentsd);
  3983. kfree(physdev_list);
  3984. kfree(logdev_list);
  3985. kfree(id_ctlr);
  3986. kfree(id_phys);
  3987. }
  3988. static void hpsa_set_sg_descriptor(struct SGDescriptor *desc,
  3989. struct scatterlist *sg)
  3990. {
  3991. u64 addr64 = (u64) sg_dma_address(sg);
  3992. unsigned int len = sg_dma_len(sg);
  3993. desc->Addr = cpu_to_le64(addr64);
  3994. desc->Len = cpu_to_le32(len);
  3995. desc->Ext = 0;
  3996. }
  3997. /*
  3998. * hpsa_scatter_gather takes a struct scsi_cmnd, (cmd), and does the pci
  3999. * dma mapping and fills in the scatter gather entries of the
  4000. * hpsa command, cp.
  4001. */
  4002. static int hpsa_scatter_gather(struct ctlr_info *h,
  4003. struct CommandList *cp,
  4004. struct scsi_cmnd *cmd)
  4005. {
  4006. struct scatterlist *sg;
  4007. int use_sg, i, sg_limit, chained, last_sg;
  4008. struct SGDescriptor *curr_sg;
  4009. BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
  4010. use_sg = scsi_dma_map(cmd);
  4011. if (use_sg < 0)
  4012. return use_sg;
  4013. if (!use_sg)
  4014. goto sglist_finished;
  4015. /*
  4016. * If the number of entries is greater than the max for a single list,
  4017. * then we have a chained list; we will set up all but one entry in the
  4018. * first list (the last entry is saved for link information);
  4019. * otherwise, we don't have a chained list and we'll set up at each of
  4020. * the entries in the one list.
  4021. */
  4022. curr_sg = cp->SG;
  4023. chained = use_sg > h->max_cmd_sg_entries;
  4024. sg_limit = chained ? h->max_cmd_sg_entries - 1 : use_sg;
  4025. last_sg = scsi_sg_count(cmd) - 1;
  4026. scsi_for_each_sg(cmd, sg, sg_limit, i) {
  4027. hpsa_set_sg_descriptor(curr_sg, sg);
  4028. curr_sg++;
  4029. }
  4030. if (chained) {
  4031. /*
  4032. * Continue with the chained list. Set curr_sg to the chained
  4033. * list. Modify the limit to the total count less the entries
  4034. * we've already set up. Resume the scan at the list entry
  4035. * where the previous loop left off.
  4036. */
  4037. curr_sg = h->cmd_sg_list[cp->cmdindex];
  4038. sg_limit = use_sg - sg_limit;
  4039. for_each_sg(sg, sg, sg_limit, i) {
  4040. hpsa_set_sg_descriptor(curr_sg, sg);
  4041. curr_sg++;
  4042. }
  4043. }
  4044. /* Back the pointer up to the last entry and mark it as "last". */
  4045. (curr_sg - 1)->Ext = cpu_to_le32(HPSA_SG_LAST);
  4046. if (use_sg + chained > h->maxSG)
  4047. h->maxSG = use_sg + chained;
  4048. if (chained) {
  4049. cp->Header.SGList = h->max_cmd_sg_entries;
  4050. cp->Header.SGTotal = cpu_to_le16(use_sg + 1);
  4051. if (hpsa_map_sg_chain_block(h, cp)) {
  4052. scsi_dma_unmap(cmd);
  4053. return -1;
  4054. }
  4055. return 0;
  4056. }
  4057. sglist_finished:
  4058. cp->Header.SGList = (u8) use_sg; /* no. SGs contig in this cmd */
  4059. cp->Header.SGTotal = cpu_to_le16(use_sg); /* total sgs in cmd list */
  4060. return 0;
  4061. }
  4062. #define IO_ACCEL_INELIGIBLE (1)
  4063. static int fixup_ioaccel_cdb(u8 *cdb, int *cdb_len)
  4064. {
  4065. int is_write = 0;
  4066. u32 block;
  4067. u32 block_cnt;
  4068. /* Perform some CDB fixups if needed using 10 byte reads/writes only */
  4069. switch (cdb[0]) {
  4070. case WRITE_6:
  4071. case WRITE_12:
  4072. is_write = 1;
  4073. case READ_6:
  4074. case READ_12:
  4075. if (*cdb_len == 6) {
  4076. block = (((cdb[1] & 0x1F) << 16) |
  4077. (cdb[2] << 8) |
  4078. cdb[3]);
  4079. block_cnt = cdb[4];
  4080. if (block_cnt == 0)
  4081. block_cnt = 256;
  4082. } else {
  4083. BUG_ON(*cdb_len != 12);
  4084. block = get_unaligned_be32(&cdb[2]);
  4085. block_cnt = get_unaligned_be32(&cdb[6]);
  4086. }
  4087. if (block_cnt > 0xffff)
  4088. return IO_ACCEL_INELIGIBLE;
  4089. cdb[0] = is_write ? WRITE_10 : READ_10;
  4090. cdb[1] = 0;
  4091. cdb[2] = (u8) (block >> 24);
  4092. cdb[3] = (u8) (block >> 16);
  4093. cdb[4] = (u8) (block >> 8);
  4094. cdb[5] = (u8) (block);
  4095. cdb[6] = 0;
  4096. cdb[7] = (u8) (block_cnt >> 8);
  4097. cdb[8] = (u8) (block_cnt);
  4098. cdb[9] = 0;
  4099. *cdb_len = 10;
  4100. break;
  4101. }
  4102. return 0;
  4103. }
  4104. static int hpsa_scsi_ioaccel1_queue_command(struct ctlr_info *h,
  4105. struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
  4106. u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
  4107. {
  4108. struct scsi_cmnd *cmd = c->scsi_cmd;
  4109. struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[c->cmdindex];
  4110. unsigned int len;
  4111. unsigned int total_len = 0;
  4112. struct scatterlist *sg;
  4113. u64 addr64;
  4114. int use_sg, i;
  4115. struct SGDescriptor *curr_sg;
  4116. u32 control = IOACCEL1_CONTROL_SIMPLEQUEUE;
  4117. /* TODO: implement chaining support */
  4118. if (scsi_sg_count(cmd) > h->ioaccel_maxsg) {
  4119. atomic_dec(&phys_disk->ioaccel_cmds_out);
  4120. return IO_ACCEL_INELIGIBLE;
  4121. }
  4122. BUG_ON(cmd->cmd_len > IOACCEL1_IOFLAGS_CDBLEN_MAX);
  4123. if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
  4124. atomic_dec(&phys_disk->ioaccel_cmds_out);
  4125. return IO_ACCEL_INELIGIBLE;
  4126. }
  4127. c->cmd_type = CMD_IOACCEL1;
  4128. /* Adjust the DMA address to point to the accelerated command buffer */
  4129. c->busaddr = (u32) h->ioaccel_cmd_pool_dhandle +
  4130. (c->cmdindex * sizeof(*cp));
  4131. BUG_ON(c->busaddr & 0x0000007F);
  4132. use_sg = scsi_dma_map(cmd);
  4133. if (use_sg < 0) {
  4134. atomic_dec(&phys_disk->ioaccel_cmds_out);
  4135. return use_sg;
  4136. }
  4137. if (use_sg) {
  4138. curr_sg = cp->SG;
  4139. scsi_for_each_sg(cmd, sg, use_sg, i) {
  4140. addr64 = (u64) sg_dma_address(sg);
  4141. len = sg_dma_len(sg);
  4142. total_len += len;
  4143. curr_sg->Addr = cpu_to_le64(addr64);
  4144. curr_sg->Len = cpu_to_le32(len);
  4145. curr_sg->Ext = cpu_to_le32(0);
  4146. curr_sg++;
  4147. }
  4148. (--curr_sg)->Ext = cpu_to_le32(HPSA_SG_LAST);
  4149. switch (cmd->sc_data_direction) {
  4150. case DMA_TO_DEVICE:
  4151. control |= IOACCEL1_CONTROL_DATA_OUT;
  4152. break;
  4153. case DMA_FROM_DEVICE:
  4154. control |= IOACCEL1_CONTROL_DATA_IN;
  4155. break;
  4156. case DMA_NONE:
  4157. control |= IOACCEL1_CONTROL_NODATAXFER;
  4158. break;
  4159. default:
  4160. dev_err(&h->pdev->dev, "unknown data direction: %d\n",
  4161. cmd->sc_data_direction);
  4162. BUG();
  4163. break;
  4164. }
  4165. } else {
  4166. control |= IOACCEL1_CONTROL_NODATAXFER;
  4167. }
  4168. c->Header.SGList = use_sg;
  4169. /* Fill out the command structure to submit */
  4170. cp->dev_handle = cpu_to_le16(ioaccel_handle & 0xFFFF);
  4171. cp->transfer_len = cpu_to_le32(total_len);
  4172. cp->io_flags = cpu_to_le16(IOACCEL1_IOFLAGS_IO_REQ |
  4173. (cdb_len & IOACCEL1_IOFLAGS_CDBLEN_MASK));
  4174. cp->control = cpu_to_le32(control);
  4175. memcpy(cp->CDB, cdb, cdb_len);
  4176. memcpy(cp->CISS_LUN, scsi3addr, 8);
  4177. /* Tag was already set at init time. */
  4178. enqueue_cmd_and_start_io(h, c);
  4179. return 0;
  4180. }
  4181. /*
  4182. * Queue a command directly to a device behind the controller using the
  4183. * I/O accelerator path.
  4184. */
  4185. static int hpsa_scsi_ioaccel_direct_map(struct ctlr_info *h,
  4186. struct CommandList *c)
  4187. {
  4188. struct scsi_cmnd *cmd = c->scsi_cmd;
  4189. struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
  4190. if (!dev)
  4191. return -1;
  4192. c->phys_disk = dev;
  4193. return hpsa_scsi_ioaccel_queue_command(h, c, dev->ioaccel_handle,
  4194. cmd->cmnd, cmd->cmd_len, dev->scsi3addr, dev);
  4195. }
  4196. /*
  4197. * Set encryption parameters for the ioaccel2 request
  4198. */
  4199. static void set_encrypt_ioaccel2(struct ctlr_info *h,
  4200. struct CommandList *c, struct io_accel2_cmd *cp)
  4201. {
  4202. struct scsi_cmnd *cmd = c->scsi_cmd;
  4203. struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
  4204. struct raid_map_data *map = &dev->raid_map;
  4205. u64 first_block;
  4206. /* Are we doing encryption on this device */
  4207. if (!(le16_to_cpu(map->flags) & RAID_MAP_FLAG_ENCRYPT_ON))
  4208. return;
  4209. /* Set the data encryption key index. */
  4210. cp->dekindex = map->dekindex;
  4211. /* Set the encryption enable flag, encoded into direction field. */
  4212. cp->direction |= IOACCEL2_DIRECTION_ENCRYPT_MASK;
  4213. /* Set encryption tweak values based on logical block address
  4214. * If block size is 512, tweak value is LBA.
  4215. * For other block sizes, tweak is (LBA * block size)/ 512)
  4216. */
  4217. switch (cmd->cmnd[0]) {
  4218. /* Required? 6-byte cdbs eliminated by fixup_ioaccel_cdb */
  4219. case READ_6:
  4220. case WRITE_6:
  4221. first_block = (((cmd->cmnd[1] & 0x1F) << 16) |
  4222. (cmd->cmnd[2] << 8) |
  4223. cmd->cmnd[3]);
  4224. break;
  4225. case WRITE_10:
  4226. case READ_10:
  4227. /* Required? 12-byte cdbs eliminated by fixup_ioaccel_cdb */
  4228. case WRITE_12:
  4229. case READ_12:
  4230. first_block = get_unaligned_be32(&cmd->cmnd[2]);
  4231. break;
  4232. case WRITE_16:
  4233. case READ_16:
  4234. first_block = get_unaligned_be64(&cmd->cmnd[2]);
  4235. break;
  4236. default:
  4237. dev_err(&h->pdev->dev,
  4238. "ERROR: %s: size (0x%x) not supported for encryption\n",
  4239. __func__, cmd->cmnd[0]);
  4240. BUG();
  4241. break;
  4242. }
  4243. if (le32_to_cpu(map->volume_blk_size) != 512)
  4244. first_block = first_block *
  4245. le32_to_cpu(map->volume_blk_size)/512;
  4246. cp->tweak_lower = cpu_to_le32(first_block);
  4247. cp->tweak_upper = cpu_to_le32(first_block >> 32);
  4248. }
  4249. static int hpsa_scsi_ioaccel2_queue_command(struct ctlr_info *h,
  4250. struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
  4251. u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
  4252. {
  4253. struct scsi_cmnd *cmd = c->scsi_cmd;
  4254. struct io_accel2_cmd *cp = &h->ioaccel2_cmd_pool[c->cmdindex];
  4255. struct ioaccel2_sg_element *curr_sg;
  4256. int use_sg, i;
  4257. struct scatterlist *sg;
  4258. u64 addr64;
  4259. u32 len;
  4260. u32 total_len = 0;
  4261. if (!cmd->device)
  4262. return -1;
  4263. if (!cmd->device->hostdata)
  4264. return -1;
  4265. BUG_ON(scsi_sg_count(cmd) > h->maxsgentries);
  4266. if (fixup_ioaccel_cdb(cdb, &cdb_len)) {
  4267. atomic_dec(&phys_disk->ioaccel_cmds_out);
  4268. return IO_ACCEL_INELIGIBLE;
  4269. }
  4270. c->cmd_type = CMD_IOACCEL2;
  4271. /* Adjust the DMA address to point to the accelerated command buffer */
  4272. c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
  4273. (c->cmdindex * sizeof(*cp));
  4274. BUG_ON(c->busaddr & 0x0000007F);
  4275. memset(cp, 0, sizeof(*cp));
  4276. cp->IU_type = IOACCEL2_IU_TYPE;
  4277. use_sg = scsi_dma_map(cmd);
  4278. if (use_sg < 0) {
  4279. atomic_dec(&phys_disk->ioaccel_cmds_out);
  4280. return use_sg;
  4281. }
  4282. if (use_sg) {
  4283. curr_sg = cp->sg;
  4284. if (use_sg > h->ioaccel_maxsg) {
  4285. addr64 = le64_to_cpu(
  4286. h->ioaccel2_cmd_sg_list[c->cmdindex]->address);
  4287. curr_sg->address = cpu_to_le64(addr64);
  4288. curr_sg->length = 0;
  4289. curr_sg->reserved[0] = 0;
  4290. curr_sg->reserved[1] = 0;
  4291. curr_sg->reserved[2] = 0;
  4292. curr_sg->chain_indicator = 0x80;
  4293. curr_sg = h->ioaccel2_cmd_sg_list[c->cmdindex];
  4294. }
  4295. scsi_for_each_sg(cmd, sg, use_sg, i) {
  4296. addr64 = (u64) sg_dma_address(sg);
  4297. len = sg_dma_len(sg);
  4298. total_len += len;
  4299. curr_sg->address = cpu_to_le64(addr64);
  4300. curr_sg->length = cpu_to_le32(len);
  4301. curr_sg->reserved[0] = 0;
  4302. curr_sg->reserved[1] = 0;
  4303. curr_sg->reserved[2] = 0;
  4304. curr_sg->chain_indicator = 0;
  4305. curr_sg++;
  4306. }
  4307. switch (cmd->sc_data_direction) {
  4308. case DMA_TO_DEVICE:
  4309. cp->direction &= ~IOACCEL2_DIRECTION_MASK;
  4310. cp->direction |= IOACCEL2_DIR_DATA_OUT;
  4311. break;
  4312. case DMA_FROM_DEVICE:
  4313. cp->direction &= ~IOACCEL2_DIRECTION_MASK;
  4314. cp->direction |= IOACCEL2_DIR_DATA_IN;
  4315. break;
  4316. case DMA_NONE:
  4317. cp->direction &= ~IOACCEL2_DIRECTION_MASK;
  4318. cp->direction |= IOACCEL2_DIR_NO_DATA;
  4319. break;
  4320. default:
  4321. dev_err(&h->pdev->dev, "unknown data direction: %d\n",
  4322. cmd->sc_data_direction);
  4323. BUG();
  4324. break;
  4325. }
  4326. } else {
  4327. cp->direction &= ~IOACCEL2_DIRECTION_MASK;
  4328. cp->direction |= IOACCEL2_DIR_NO_DATA;
  4329. }
  4330. /* Set encryption parameters, if necessary */
  4331. set_encrypt_ioaccel2(h, c, cp);
  4332. cp->scsi_nexus = cpu_to_le32(ioaccel_handle);
  4333. cp->Tag = cpu_to_le32(c->cmdindex << DIRECT_LOOKUP_SHIFT);
  4334. memcpy(cp->cdb, cdb, sizeof(cp->cdb));
  4335. cp->data_len = cpu_to_le32(total_len);
  4336. cp->err_ptr = cpu_to_le64(c->busaddr +
  4337. offsetof(struct io_accel2_cmd, error_data));
  4338. cp->err_len = cpu_to_le32(sizeof(cp->error_data));
  4339. /* fill in sg elements */
  4340. if (use_sg > h->ioaccel_maxsg) {
  4341. cp->sg_count = 1;
  4342. cp->sg[0].length = cpu_to_le32(use_sg * sizeof(cp->sg[0]));
  4343. if (hpsa_map_ioaccel2_sg_chain_block(h, cp, c)) {
  4344. atomic_dec(&phys_disk->ioaccel_cmds_out);
  4345. scsi_dma_unmap(cmd);
  4346. return -1;
  4347. }
  4348. } else
  4349. cp->sg_count = (u8) use_sg;
  4350. enqueue_cmd_and_start_io(h, c);
  4351. return 0;
  4352. }
  4353. /*
  4354. * Queue a command to the correct I/O accelerator path.
  4355. */
  4356. static int hpsa_scsi_ioaccel_queue_command(struct ctlr_info *h,
  4357. struct CommandList *c, u32 ioaccel_handle, u8 *cdb, int cdb_len,
  4358. u8 *scsi3addr, struct hpsa_scsi_dev_t *phys_disk)
  4359. {
  4360. if (!c->scsi_cmd->device)
  4361. return -1;
  4362. if (!c->scsi_cmd->device->hostdata)
  4363. return -1;
  4364. /* Try to honor the device's queue depth */
  4365. if (atomic_inc_return(&phys_disk->ioaccel_cmds_out) >
  4366. phys_disk->queue_depth) {
  4367. atomic_dec(&phys_disk->ioaccel_cmds_out);
  4368. return IO_ACCEL_INELIGIBLE;
  4369. }
  4370. if (h->transMethod & CFGTBL_Trans_io_accel1)
  4371. return hpsa_scsi_ioaccel1_queue_command(h, c, ioaccel_handle,
  4372. cdb, cdb_len, scsi3addr,
  4373. phys_disk);
  4374. else
  4375. return hpsa_scsi_ioaccel2_queue_command(h, c, ioaccel_handle,
  4376. cdb, cdb_len, scsi3addr,
  4377. phys_disk);
  4378. }
  4379. static void raid_map_helper(struct raid_map_data *map,
  4380. int offload_to_mirror, u32 *map_index, u32 *current_group)
  4381. {
  4382. if (offload_to_mirror == 0) {
  4383. /* use physical disk in the first mirrored group. */
  4384. *map_index %= le16_to_cpu(map->data_disks_per_row);
  4385. return;
  4386. }
  4387. do {
  4388. /* determine mirror group that *map_index indicates */
  4389. *current_group = *map_index /
  4390. le16_to_cpu(map->data_disks_per_row);
  4391. if (offload_to_mirror == *current_group)
  4392. continue;
  4393. if (*current_group < le16_to_cpu(map->layout_map_count) - 1) {
  4394. /* select map index from next group */
  4395. *map_index += le16_to_cpu(map->data_disks_per_row);
  4396. (*current_group)++;
  4397. } else {
  4398. /* select map index from first group */
  4399. *map_index %= le16_to_cpu(map->data_disks_per_row);
  4400. *current_group = 0;
  4401. }
  4402. } while (offload_to_mirror != *current_group);
  4403. }
  4404. /*
  4405. * Attempt to perform offload RAID mapping for a logical volume I/O.
  4406. */
  4407. static int hpsa_scsi_ioaccel_raid_map(struct ctlr_info *h,
  4408. struct CommandList *c)
  4409. {
  4410. struct scsi_cmnd *cmd = c->scsi_cmd;
  4411. struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
  4412. struct raid_map_data *map = &dev->raid_map;
  4413. struct raid_map_disk_data *dd = &map->data[0];
  4414. int is_write = 0;
  4415. u32 map_index;
  4416. u64 first_block, last_block;
  4417. u32 block_cnt;
  4418. u32 blocks_per_row;
  4419. u64 first_row, last_row;
  4420. u32 first_row_offset, last_row_offset;
  4421. u32 first_column, last_column;
  4422. u64 r0_first_row, r0_last_row;
  4423. u32 r5or6_blocks_per_row;
  4424. u64 r5or6_first_row, r5or6_last_row;
  4425. u32 r5or6_first_row_offset, r5or6_last_row_offset;
  4426. u32 r5or6_first_column, r5or6_last_column;
  4427. u32 total_disks_per_row;
  4428. u32 stripesize;
  4429. u32 first_group, last_group, current_group;
  4430. u32 map_row;
  4431. u32 disk_handle;
  4432. u64 disk_block;
  4433. u32 disk_block_cnt;
  4434. u8 cdb[16];
  4435. u8 cdb_len;
  4436. u16 strip_size;
  4437. #if BITS_PER_LONG == 32
  4438. u64 tmpdiv;
  4439. #endif
  4440. int offload_to_mirror;
  4441. if (!dev)
  4442. return -1;
  4443. /* check for valid opcode, get LBA and block count */
  4444. switch (cmd->cmnd[0]) {
  4445. case WRITE_6:
  4446. is_write = 1;
  4447. case READ_6:
  4448. first_block = (((cmd->cmnd[1] & 0x1F) << 16) |
  4449. (cmd->cmnd[2] << 8) |
  4450. cmd->cmnd[3]);
  4451. block_cnt = cmd->cmnd[4];
  4452. if (block_cnt == 0)
  4453. block_cnt = 256;
  4454. break;
  4455. case WRITE_10:
  4456. is_write = 1;
  4457. case READ_10:
  4458. first_block =
  4459. (((u64) cmd->cmnd[2]) << 24) |
  4460. (((u64) cmd->cmnd[3]) << 16) |
  4461. (((u64) cmd->cmnd[4]) << 8) |
  4462. cmd->cmnd[5];
  4463. block_cnt =
  4464. (((u32) cmd->cmnd[7]) << 8) |
  4465. cmd->cmnd[8];
  4466. break;
  4467. case WRITE_12:
  4468. is_write = 1;
  4469. case READ_12:
  4470. first_block =
  4471. (((u64) cmd->cmnd[2]) << 24) |
  4472. (((u64) cmd->cmnd[3]) << 16) |
  4473. (((u64) cmd->cmnd[4]) << 8) |
  4474. cmd->cmnd[5];
  4475. block_cnt =
  4476. (((u32) cmd->cmnd[6]) << 24) |
  4477. (((u32) cmd->cmnd[7]) << 16) |
  4478. (((u32) cmd->cmnd[8]) << 8) |
  4479. cmd->cmnd[9];
  4480. break;
  4481. case WRITE_16:
  4482. is_write = 1;
  4483. case READ_16:
  4484. first_block =
  4485. (((u64) cmd->cmnd[2]) << 56) |
  4486. (((u64) cmd->cmnd[3]) << 48) |
  4487. (((u64) cmd->cmnd[4]) << 40) |
  4488. (((u64) cmd->cmnd[5]) << 32) |
  4489. (((u64) cmd->cmnd[6]) << 24) |
  4490. (((u64) cmd->cmnd[7]) << 16) |
  4491. (((u64) cmd->cmnd[8]) << 8) |
  4492. cmd->cmnd[9];
  4493. block_cnt =
  4494. (((u32) cmd->cmnd[10]) << 24) |
  4495. (((u32) cmd->cmnd[11]) << 16) |
  4496. (((u32) cmd->cmnd[12]) << 8) |
  4497. cmd->cmnd[13];
  4498. break;
  4499. default:
  4500. return IO_ACCEL_INELIGIBLE; /* process via normal I/O path */
  4501. }
  4502. last_block = first_block + block_cnt - 1;
  4503. /* check for write to non-RAID-0 */
  4504. if (is_write && dev->raid_level != 0)
  4505. return IO_ACCEL_INELIGIBLE;
  4506. /* check for invalid block or wraparound */
  4507. if (last_block >= le64_to_cpu(map->volume_blk_cnt) ||
  4508. last_block < first_block)
  4509. return IO_ACCEL_INELIGIBLE;
  4510. /* calculate stripe information for the request */
  4511. blocks_per_row = le16_to_cpu(map->data_disks_per_row) *
  4512. le16_to_cpu(map->strip_size);
  4513. strip_size = le16_to_cpu(map->strip_size);
  4514. #if BITS_PER_LONG == 32
  4515. tmpdiv = first_block;
  4516. (void) do_div(tmpdiv, blocks_per_row);
  4517. first_row = tmpdiv;
  4518. tmpdiv = last_block;
  4519. (void) do_div(tmpdiv, blocks_per_row);
  4520. last_row = tmpdiv;
  4521. first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
  4522. last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
  4523. tmpdiv = first_row_offset;
  4524. (void) do_div(tmpdiv, strip_size);
  4525. first_column = tmpdiv;
  4526. tmpdiv = last_row_offset;
  4527. (void) do_div(tmpdiv, strip_size);
  4528. last_column = tmpdiv;
  4529. #else
  4530. first_row = first_block / blocks_per_row;
  4531. last_row = last_block / blocks_per_row;
  4532. first_row_offset = (u32) (first_block - (first_row * blocks_per_row));
  4533. last_row_offset = (u32) (last_block - (last_row * blocks_per_row));
  4534. first_column = first_row_offset / strip_size;
  4535. last_column = last_row_offset / strip_size;
  4536. #endif
  4537. /* if this isn't a single row/column then give to the controller */
  4538. if ((first_row != last_row) || (first_column != last_column))
  4539. return IO_ACCEL_INELIGIBLE;
  4540. /* proceeding with driver mapping */
  4541. total_disks_per_row = le16_to_cpu(map->data_disks_per_row) +
  4542. le16_to_cpu(map->metadata_disks_per_row);
  4543. map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
  4544. le16_to_cpu(map->row_cnt);
  4545. map_index = (map_row * total_disks_per_row) + first_column;
  4546. switch (dev->raid_level) {
  4547. case HPSA_RAID_0:
  4548. break; /* nothing special to do */
  4549. case HPSA_RAID_1:
  4550. /* Handles load balance across RAID 1 members.
  4551. * (2-drive R1 and R10 with even # of drives.)
  4552. * Appropriate for SSDs, not optimal for HDDs
  4553. */
  4554. BUG_ON(le16_to_cpu(map->layout_map_count) != 2);
  4555. if (dev->offload_to_mirror)
  4556. map_index += le16_to_cpu(map->data_disks_per_row);
  4557. dev->offload_to_mirror = !dev->offload_to_mirror;
  4558. break;
  4559. case HPSA_RAID_ADM:
  4560. /* Handles N-way mirrors (R1-ADM)
  4561. * and R10 with # of drives divisible by 3.)
  4562. */
  4563. BUG_ON(le16_to_cpu(map->layout_map_count) != 3);
  4564. offload_to_mirror = dev->offload_to_mirror;
  4565. raid_map_helper(map, offload_to_mirror,
  4566. &map_index, &current_group);
  4567. /* set mirror group to use next time */
  4568. offload_to_mirror =
  4569. (offload_to_mirror >=
  4570. le16_to_cpu(map->layout_map_count) - 1)
  4571. ? 0 : offload_to_mirror + 1;
  4572. dev->offload_to_mirror = offload_to_mirror;
  4573. /* Avoid direct use of dev->offload_to_mirror within this
  4574. * function since multiple threads might simultaneously
  4575. * increment it beyond the range of dev->layout_map_count -1.
  4576. */
  4577. break;
  4578. case HPSA_RAID_5:
  4579. case HPSA_RAID_6:
  4580. if (le16_to_cpu(map->layout_map_count) <= 1)
  4581. break;
  4582. /* Verify first and last block are in same RAID group */
  4583. r5or6_blocks_per_row =
  4584. le16_to_cpu(map->strip_size) *
  4585. le16_to_cpu(map->data_disks_per_row);
  4586. BUG_ON(r5or6_blocks_per_row == 0);
  4587. stripesize = r5or6_blocks_per_row *
  4588. le16_to_cpu(map->layout_map_count);
  4589. #if BITS_PER_LONG == 32
  4590. tmpdiv = first_block;
  4591. first_group = do_div(tmpdiv, stripesize);
  4592. tmpdiv = first_group;
  4593. (void) do_div(tmpdiv, r5or6_blocks_per_row);
  4594. first_group = tmpdiv;
  4595. tmpdiv = last_block;
  4596. last_group = do_div(tmpdiv, stripesize);
  4597. tmpdiv = last_group;
  4598. (void) do_div(tmpdiv, r5or6_blocks_per_row);
  4599. last_group = tmpdiv;
  4600. #else
  4601. first_group = (first_block % stripesize) / r5or6_blocks_per_row;
  4602. last_group = (last_block % stripesize) / r5or6_blocks_per_row;
  4603. #endif
  4604. if (first_group != last_group)
  4605. return IO_ACCEL_INELIGIBLE;
  4606. /* Verify request is in a single row of RAID 5/6 */
  4607. #if BITS_PER_LONG == 32
  4608. tmpdiv = first_block;
  4609. (void) do_div(tmpdiv, stripesize);
  4610. first_row = r5or6_first_row = r0_first_row = tmpdiv;
  4611. tmpdiv = last_block;
  4612. (void) do_div(tmpdiv, stripesize);
  4613. r5or6_last_row = r0_last_row = tmpdiv;
  4614. #else
  4615. first_row = r5or6_first_row = r0_first_row =
  4616. first_block / stripesize;
  4617. r5or6_last_row = r0_last_row = last_block / stripesize;
  4618. #endif
  4619. if (r5or6_first_row != r5or6_last_row)
  4620. return IO_ACCEL_INELIGIBLE;
  4621. /* Verify request is in a single column */
  4622. #if BITS_PER_LONG == 32
  4623. tmpdiv = first_block;
  4624. first_row_offset = do_div(tmpdiv, stripesize);
  4625. tmpdiv = first_row_offset;
  4626. first_row_offset = (u32) do_div(tmpdiv, r5or6_blocks_per_row);
  4627. r5or6_first_row_offset = first_row_offset;
  4628. tmpdiv = last_block;
  4629. r5or6_last_row_offset = do_div(tmpdiv, stripesize);
  4630. tmpdiv = r5or6_last_row_offset;
  4631. r5or6_last_row_offset = do_div(tmpdiv, r5or6_blocks_per_row);
  4632. tmpdiv = r5or6_first_row_offset;
  4633. (void) do_div(tmpdiv, map->strip_size);
  4634. first_column = r5or6_first_column = tmpdiv;
  4635. tmpdiv = r5or6_last_row_offset;
  4636. (void) do_div(tmpdiv, map->strip_size);
  4637. r5or6_last_column = tmpdiv;
  4638. #else
  4639. first_row_offset = r5or6_first_row_offset =
  4640. (u32)((first_block % stripesize) %
  4641. r5or6_blocks_per_row);
  4642. r5or6_last_row_offset =
  4643. (u32)((last_block % stripesize) %
  4644. r5or6_blocks_per_row);
  4645. first_column = r5or6_first_column =
  4646. r5or6_first_row_offset / le16_to_cpu(map->strip_size);
  4647. r5or6_last_column =
  4648. r5or6_last_row_offset / le16_to_cpu(map->strip_size);
  4649. #endif
  4650. if (r5or6_first_column != r5or6_last_column)
  4651. return IO_ACCEL_INELIGIBLE;
  4652. /* Request is eligible */
  4653. map_row = ((u32)(first_row >> map->parity_rotation_shift)) %
  4654. le16_to_cpu(map->row_cnt);
  4655. map_index = (first_group *
  4656. (le16_to_cpu(map->row_cnt) * total_disks_per_row)) +
  4657. (map_row * total_disks_per_row) + first_column;
  4658. break;
  4659. default:
  4660. return IO_ACCEL_INELIGIBLE;
  4661. }
  4662. if (unlikely(map_index >= RAID_MAP_MAX_ENTRIES))
  4663. return IO_ACCEL_INELIGIBLE;
  4664. c->phys_disk = dev->phys_disk[map_index];
  4665. if (!c->phys_disk)
  4666. return IO_ACCEL_INELIGIBLE;
  4667. disk_handle = dd[map_index].ioaccel_handle;
  4668. disk_block = le64_to_cpu(map->disk_starting_blk) +
  4669. first_row * le16_to_cpu(map->strip_size) +
  4670. (first_row_offset - first_column *
  4671. le16_to_cpu(map->strip_size));
  4672. disk_block_cnt = block_cnt;
  4673. /* handle differing logical/physical block sizes */
  4674. if (map->phys_blk_shift) {
  4675. disk_block <<= map->phys_blk_shift;
  4676. disk_block_cnt <<= map->phys_blk_shift;
  4677. }
  4678. BUG_ON(disk_block_cnt > 0xffff);
  4679. /* build the new CDB for the physical disk I/O */
  4680. if (disk_block > 0xffffffff) {
  4681. cdb[0] = is_write ? WRITE_16 : READ_16;
  4682. cdb[1] = 0;
  4683. cdb[2] = (u8) (disk_block >> 56);
  4684. cdb[3] = (u8) (disk_block >> 48);
  4685. cdb[4] = (u8) (disk_block >> 40);
  4686. cdb[5] = (u8) (disk_block >> 32);
  4687. cdb[6] = (u8) (disk_block >> 24);
  4688. cdb[7] = (u8) (disk_block >> 16);
  4689. cdb[8] = (u8) (disk_block >> 8);
  4690. cdb[9] = (u8) (disk_block);
  4691. cdb[10] = (u8) (disk_block_cnt >> 24);
  4692. cdb[11] = (u8) (disk_block_cnt >> 16);
  4693. cdb[12] = (u8) (disk_block_cnt >> 8);
  4694. cdb[13] = (u8) (disk_block_cnt);
  4695. cdb[14] = 0;
  4696. cdb[15] = 0;
  4697. cdb_len = 16;
  4698. } else {
  4699. cdb[0] = is_write ? WRITE_10 : READ_10;
  4700. cdb[1] = 0;
  4701. cdb[2] = (u8) (disk_block >> 24);
  4702. cdb[3] = (u8) (disk_block >> 16);
  4703. cdb[4] = (u8) (disk_block >> 8);
  4704. cdb[5] = (u8) (disk_block);
  4705. cdb[6] = 0;
  4706. cdb[7] = (u8) (disk_block_cnt >> 8);
  4707. cdb[8] = (u8) (disk_block_cnt);
  4708. cdb[9] = 0;
  4709. cdb_len = 10;
  4710. }
  4711. return hpsa_scsi_ioaccel_queue_command(h, c, disk_handle, cdb, cdb_len,
  4712. dev->scsi3addr,
  4713. dev->phys_disk[map_index]);
  4714. }
  4715. /*
  4716. * Submit commands down the "normal" RAID stack path
  4717. * All callers to hpsa_ciss_submit must check lockup_detected
  4718. * beforehand, before (opt.) and after calling cmd_alloc
  4719. */
  4720. static int hpsa_ciss_submit(struct ctlr_info *h,
  4721. struct CommandList *c, struct scsi_cmnd *cmd,
  4722. unsigned char scsi3addr[])
  4723. {
  4724. cmd->host_scribble = (unsigned char *) c;
  4725. c->cmd_type = CMD_SCSI;
  4726. c->scsi_cmd = cmd;
  4727. c->Header.ReplyQueue = 0; /* unused in simple mode */
  4728. memcpy(&c->Header.LUN.LunAddrBytes[0], &scsi3addr[0], 8);
  4729. c->Header.tag = cpu_to_le64((c->cmdindex << DIRECT_LOOKUP_SHIFT));
  4730. /* Fill in the request block... */
  4731. c->Request.Timeout = 0;
  4732. BUG_ON(cmd->cmd_len > sizeof(c->Request.CDB));
  4733. c->Request.CDBLen = cmd->cmd_len;
  4734. memcpy(c->Request.CDB, cmd->cmnd, cmd->cmd_len);
  4735. switch (cmd->sc_data_direction) {
  4736. case DMA_TO_DEVICE:
  4737. c->Request.type_attr_dir =
  4738. TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_WRITE);
  4739. break;
  4740. case DMA_FROM_DEVICE:
  4741. c->Request.type_attr_dir =
  4742. TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_READ);
  4743. break;
  4744. case DMA_NONE:
  4745. c->Request.type_attr_dir =
  4746. TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_NONE);
  4747. break;
  4748. case DMA_BIDIRECTIONAL:
  4749. /* This can happen if a buggy application does a scsi passthru
  4750. * and sets both inlen and outlen to non-zero. ( see
  4751. * ../scsi/scsi_ioctl.c:scsi_ioctl_send_command() )
  4752. */
  4753. c->Request.type_attr_dir =
  4754. TYPE_ATTR_DIR(TYPE_CMD, ATTR_SIMPLE, XFER_RSVD);
  4755. /* This is technically wrong, and hpsa controllers should
  4756. * reject it with CMD_INVALID, which is the most correct
  4757. * response, but non-fibre backends appear to let it
  4758. * slide by, and give the same results as if this field
  4759. * were set correctly. Either way is acceptable for
  4760. * our purposes here.
  4761. */
  4762. break;
  4763. default:
  4764. dev_err(&h->pdev->dev, "unknown data direction: %d\n",
  4765. cmd->sc_data_direction);
  4766. BUG();
  4767. break;
  4768. }
  4769. if (hpsa_scatter_gather(h, c, cmd) < 0) { /* Fill SG list */
  4770. hpsa_cmd_resolve_and_free(h, c);
  4771. return SCSI_MLQUEUE_HOST_BUSY;
  4772. }
  4773. enqueue_cmd_and_start_io(h, c);
  4774. /* the cmd'll come back via intr handler in complete_scsi_command() */
  4775. return 0;
  4776. }
  4777. static void hpsa_cmd_init(struct ctlr_info *h, int index,
  4778. struct CommandList *c)
  4779. {
  4780. dma_addr_t cmd_dma_handle, err_dma_handle;
  4781. /* Zero out all of commandlist except the last field, refcount */
  4782. memset(c, 0, offsetof(struct CommandList, refcount));
  4783. c->Header.tag = cpu_to_le64((u64) (index << DIRECT_LOOKUP_SHIFT));
  4784. cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
  4785. c->err_info = h->errinfo_pool + index;
  4786. memset(c->err_info, 0, sizeof(*c->err_info));
  4787. err_dma_handle = h->errinfo_pool_dhandle
  4788. + index * sizeof(*c->err_info);
  4789. c->cmdindex = index;
  4790. c->busaddr = (u32) cmd_dma_handle;
  4791. c->ErrDesc.Addr = cpu_to_le64((u64) err_dma_handle);
  4792. c->ErrDesc.Len = cpu_to_le32((u32) sizeof(*c->err_info));
  4793. c->h = h;
  4794. c->scsi_cmd = SCSI_CMD_IDLE;
  4795. }
  4796. static void hpsa_preinitialize_commands(struct ctlr_info *h)
  4797. {
  4798. int i;
  4799. for (i = 0; i < h->nr_cmds; i++) {
  4800. struct CommandList *c = h->cmd_pool + i;
  4801. hpsa_cmd_init(h, i, c);
  4802. atomic_set(&c->refcount, 0);
  4803. }
  4804. }
  4805. static inline void hpsa_cmd_partial_init(struct ctlr_info *h, int index,
  4806. struct CommandList *c)
  4807. {
  4808. dma_addr_t cmd_dma_handle = h->cmd_pool_dhandle + index * sizeof(*c);
  4809. BUG_ON(c->cmdindex != index);
  4810. memset(c->Request.CDB, 0, sizeof(c->Request.CDB));
  4811. memset(c->err_info, 0, sizeof(*c->err_info));
  4812. c->busaddr = (u32) cmd_dma_handle;
  4813. }
  4814. static int hpsa_ioaccel_submit(struct ctlr_info *h,
  4815. struct CommandList *c, struct scsi_cmnd *cmd,
  4816. unsigned char *scsi3addr)
  4817. {
  4818. struct hpsa_scsi_dev_t *dev = cmd->device->hostdata;
  4819. int rc = IO_ACCEL_INELIGIBLE;
  4820. if (!dev)
  4821. return SCSI_MLQUEUE_HOST_BUSY;
  4822. cmd->host_scribble = (unsigned char *) c;
  4823. if (dev->offload_enabled) {
  4824. hpsa_cmd_init(h, c->cmdindex, c);
  4825. c->cmd_type = CMD_SCSI;
  4826. c->scsi_cmd = cmd;
  4827. rc = hpsa_scsi_ioaccel_raid_map(h, c);
  4828. if (rc < 0) /* scsi_dma_map failed. */
  4829. rc = SCSI_MLQUEUE_HOST_BUSY;
  4830. } else if (dev->hba_ioaccel_enabled) {
  4831. hpsa_cmd_init(h, c->cmdindex, c);
  4832. c->cmd_type = CMD_SCSI;
  4833. c->scsi_cmd = cmd;
  4834. rc = hpsa_scsi_ioaccel_direct_map(h, c);
  4835. if (rc < 0) /* scsi_dma_map failed. */
  4836. rc = SCSI_MLQUEUE_HOST_BUSY;
  4837. }
  4838. return rc;
  4839. }
  4840. static void hpsa_command_resubmit_worker(struct work_struct *work)
  4841. {
  4842. struct scsi_cmnd *cmd;
  4843. struct hpsa_scsi_dev_t *dev;
  4844. struct CommandList *c = container_of(work, struct CommandList, work);
  4845. cmd = c->scsi_cmd;
  4846. dev = cmd->device->hostdata;
  4847. if (!dev) {
  4848. cmd->result = DID_NO_CONNECT << 16;
  4849. return hpsa_cmd_free_and_done(c->h, c, cmd);
  4850. }
  4851. if (c->reset_pending)
  4852. return hpsa_cmd_resolve_and_free(c->h, c);
  4853. if (c->abort_pending)
  4854. return hpsa_cmd_abort_and_free(c->h, c, cmd);
  4855. if (c->cmd_type == CMD_IOACCEL2) {
  4856. struct ctlr_info *h = c->h;
  4857. struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
  4858. int rc;
  4859. if (c2->error_data.serv_response ==
  4860. IOACCEL2_STATUS_SR_TASK_COMP_SET_FULL) {
  4861. rc = hpsa_ioaccel_submit(h, c, cmd, dev->scsi3addr);
  4862. if (rc == 0)
  4863. return;
  4864. if (rc == SCSI_MLQUEUE_HOST_BUSY) {
  4865. /*
  4866. * If we get here, it means dma mapping failed.
  4867. * Try again via scsi mid layer, which will
  4868. * then get SCSI_MLQUEUE_HOST_BUSY.
  4869. */
  4870. cmd->result = DID_IMM_RETRY << 16;
  4871. return hpsa_cmd_free_and_done(h, c, cmd);
  4872. }
  4873. /* else, fall thru and resubmit down CISS path */
  4874. }
  4875. }
  4876. hpsa_cmd_partial_init(c->h, c->cmdindex, c);
  4877. if (hpsa_ciss_submit(c->h, c, cmd, dev->scsi3addr)) {
  4878. /*
  4879. * If we get here, it means dma mapping failed. Try
  4880. * again via scsi mid layer, which will then get
  4881. * SCSI_MLQUEUE_HOST_BUSY.
  4882. *
  4883. * hpsa_ciss_submit will have already freed c
  4884. * if it encountered a dma mapping failure.
  4885. */
  4886. cmd->result = DID_IMM_RETRY << 16;
  4887. cmd->scsi_done(cmd);
  4888. }
  4889. }
  4890. /* Running in struct Scsi_Host->host_lock less mode */
  4891. static int hpsa_scsi_queue_command(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
  4892. {
  4893. struct ctlr_info *h;
  4894. struct hpsa_scsi_dev_t *dev;
  4895. unsigned char scsi3addr[8];
  4896. struct CommandList *c;
  4897. int rc = 0;
  4898. /* Get the ptr to our adapter structure out of cmd->host. */
  4899. h = sdev_to_hba(cmd->device);
  4900. BUG_ON(cmd->request->tag < 0);
  4901. dev = cmd->device->hostdata;
  4902. if (!dev) {
  4903. cmd->result = NOT_READY << 16; /* host byte */
  4904. cmd->scsi_done(cmd);
  4905. return 0;
  4906. }
  4907. if (dev->removed) {
  4908. cmd->result = DID_NO_CONNECT << 16;
  4909. cmd->scsi_done(cmd);
  4910. return 0;
  4911. }
  4912. memcpy(scsi3addr, dev->scsi3addr, sizeof(scsi3addr));
  4913. if (unlikely(lockup_detected(h))) {
  4914. cmd->result = DID_NO_CONNECT << 16;
  4915. cmd->scsi_done(cmd);
  4916. return 0;
  4917. }
  4918. c = cmd_tagged_alloc(h, cmd);
  4919. /*
  4920. * Call alternate submit routine for I/O accelerated commands.
  4921. * Retries always go down the normal I/O path.
  4922. */
  4923. if (likely(cmd->retries == 0 &&
  4924. cmd->request->cmd_type == REQ_TYPE_FS &&
  4925. h->acciopath_status)) {
  4926. rc = hpsa_ioaccel_submit(h, c, cmd, scsi3addr);
  4927. if (rc == 0)
  4928. return 0;
  4929. if (rc == SCSI_MLQUEUE_HOST_BUSY) {
  4930. hpsa_cmd_resolve_and_free(h, c);
  4931. return SCSI_MLQUEUE_HOST_BUSY;
  4932. }
  4933. }
  4934. return hpsa_ciss_submit(h, c, cmd, scsi3addr);
  4935. }
  4936. static void hpsa_scan_complete(struct ctlr_info *h)
  4937. {
  4938. unsigned long flags;
  4939. spin_lock_irqsave(&h->scan_lock, flags);
  4940. h->scan_finished = 1;
  4941. wake_up_all(&h->scan_wait_queue);
  4942. spin_unlock_irqrestore(&h->scan_lock, flags);
  4943. }
  4944. static void hpsa_scan_start(struct Scsi_Host *sh)
  4945. {
  4946. struct ctlr_info *h = shost_to_hba(sh);
  4947. unsigned long flags;
  4948. /*
  4949. * Don't let rescans be initiated on a controller known to be locked
  4950. * up. If the controller locks up *during* a rescan, that thread is
  4951. * probably hosed, but at least we can prevent new rescan threads from
  4952. * piling up on a locked up controller.
  4953. */
  4954. if (unlikely(lockup_detected(h)))
  4955. return hpsa_scan_complete(h);
  4956. /* wait until any scan already in progress is finished. */
  4957. while (1) {
  4958. spin_lock_irqsave(&h->scan_lock, flags);
  4959. if (h->scan_finished)
  4960. break;
  4961. spin_unlock_irqrestore(&h->scan_lock, flags);
  4962. wait_event(h->scan_wait_queue, h->scan_finished);
  4963. /* Note: We don't need to worry about a race between this
  4964. * thread and driver unload because the midlayer will
  4965. * have incremented the reference count, so unload won't
  4966. * happen if we're in here.
  4967. */
  4968. }
  4969. h->scan_finished = 0; /* mark scan as in progress */
  4970. spin_unlock_irqrestore(&h->scan_lock, flags);
  4971. if (unlikely(lockup_detected(h)))
  4972. return hpsa_scan_complete(h);
  4973. hpsa_update_scsi_devices(h);
  4974. hpsa_scan_complete(h);
  4975. }
  4976. static int hpsa_change_queue_depth(struct scsi_device *sdev, int qdepth)
  4977. {
  4978. struct hpsa_scsi_dev_t *logical_drive = sdev->hostdata;
  4979. if (!logical_drive)
  4980. return -ENODEV;
  4981. if (qdepth < 1)
  4982. qdepth = 1;
  4983. else if (qdepth > logical_drive->queue_depth)
  4984. qdepth = logical_drive->queue_depth;
  4985. return scsi_change_queue_depth(sdev, qdepth);
  4986. }
  4987. static int hpsa_scan_finished(struct Scsi_Host *sh,
  4988. unsigned long elapsed_time)
  4989. {
  4990. struct ctlr_info *h = shost_to_hba(sh);
  4991. unsigned long flags;
  4992. int finished;
  4993. spin_lock_irqsave(&h->scan_lock, flags);
  4994. finished = h->scan_finished;
  4995. spin_unlock_irqrestore(&h->scan_lock, flags);
  4996. return finished;
  4997. }
  4998. static int hpsa_scsi_host_alloc(struct ctlr_info *h)
  4999. {
  5000. struct Scsi_Host *sh;
  5001. sh = scsi_host_alloc(&hpsa_driver_template, sizeof(h));
  5002. if (sh == NULL) {
  5003. dev_err(&h->pdev->dev, "scsi_host_alloc failed\n");
  5004. return -ENOMEM;
  5005. }
  5006. sh->io_port = 0;
  5007. sh->n_io_port = 0;
  5008. sh->this_id = -1;
  5009. sh->max_channel = 3;
  5010. sh->max_cmd_len = MAX_COMMAND_SIZE;
  5011. sh->max_lun = HPSA_MAX_LUN;
  5012. sh->max_id = HPSA_MAX_LUN;
  5013. sh->can_queue = h->nr_cmds - HPSA_NRESERVED_CMDS;
  5014. sh->cmd_per_lun = sh->can_queue;
  5015. sh->sg_tablesize = h->maxsgentries;
  5016. sh->transportt = hpsa_sas_transport_template;
  5017. sh->hostdata[0] = (unsigned long) h;
  5018. sh->irq = h->intr[h->intr_mode];
  5019. sh->unique_id = sh->irq;
  5020. h->scsi_host = sh;
  5021. return 0;
  5022. }
  5023. static int hpsa_scsi_add_host(struct ctlr_info *h)
  5024. {
  5025. int rv;
  5026. rv = scsi_add_host(h->scsi_host, &h->pdev->dev);
  5027. if (rv) {
  5028. dev_err(&h->pdev->dev, "scsi_add_host failed\n");
  5029. return rv;
  5030. }
  5031. scsi_scan_host(h->scsi_host);
  5032. return 0;
  5033. }
  5034. /*
  5035. * The block layer has already gone to the trouble of picking out a unique,
  5036. * small-integer tag for this request. We use an offset from that value as
  5037. * an index to select our command block. (The offset allows us to reserve the
  5038. * low-numbered entries for our own uses.)
  5039. */
  5040. static int hpsa_get_cmd_index(struct scsi_cmnd *scmd)
  5041. {
  5042. int idx = scmd->request->tag;
  5043. if (idx < 0)
  5044. return idx;
  5045. /* Offset to leave space for internal cmds. */
  5046. return idx += HPSA_NRESERVED_CMDS;
  5047. }
  5048. /*
  5049. * Send a TEST_UNIT_READY command to the specified LUN using the specified
  5050. * reply queue; returns zero if the unit is ready, and non-zero otherwise.
  5051. */
  5052. static int hpsa_send_test_unit_ready(struct ctlr_info *h,
  5053. struct CommandList *c, unsigned char lunaddr[],
  5054. int reply_queue)
  5055. {
  5056. int rc;
  5057. /* Send the Test Unit Ready, fill_cmd can't fail, no mapping */
  5058. (void) fill_cmd(c, TEST_UNIT_READY, h,
  5059. NULL, 0, 0, lunaddr, TYPE_CMD);
  5060. rc = hpsa_scsi_do_simple_cmd(h, c, reply_queue, DEFAULT_TIMEOUT);
  5061. if (rc)
  5062. return rc;
  5063. /* no unmap needed here because no data xfer. */
  5064. /* Check if the unit is already ready. */
  5065. if (c->err_info->CommandStatus == CMD_SUCCESS)
  5066. return 0;
  5067. /*
  5068. * The first command sent after reset will receive "unit attention" to
  5069. * indicate that the LUN has been reset...this is actually what we're
  5070. * looking for (but, success is good too).
  5071. */
  5072. if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
  5073. c->err_info->ScsiStatus == SAM_STAT_CHECK_CONDITION &&
  5074. (c->err_info->SenseInfo[2] == NO_SENSE ||
  5075. c->err_info->SenseInfo[2] == UNIT_ATTENTION))
  5076. return 0;
  5077. return 1;
  5078. }
  5079. /*
  5080. * Wait for a TEST_UNIT_READY command to complete, retrying as necessary;
  5081. * returns zero when the unit is ready, and non-zero when giving up.
  5082. */
  5083. static int hpsa_wait_for_test_unit_ready(struct ctlr_info *h,
  5084. struct CommandList *c,
  5085. unsigned char lunaddr[], int reply_queue)
  5086. {
  5087. int rc;
  5088. int count = 0;
  5089. int waittime = 1; /* seconds */
  5090. /* Send test unit ready until device ready, or give up. */
  5091. for (count = 0; count < HPSA_TUR_RETRY_LIMIT; count++) {
  5092. /*
  5093. * Wait for a bit. do this first, because if we send
  5094. * the TUR right away, the reset will just abort it.
  5095. */
  5096. msleep(1000 * waittime);
  5097. rc = hpsa_send_test_unit_ready(h, c, lunaddr, reply_queue);
  5098. if (!rc)
  5099. break;
  5100. /* Increase wait time with each try, up to a point. */
  5101. if (waittime < HPSA_MAX_WAIT_INTERVAL_SECS)
  5102. waittime *= 2;
  5103. dev_warn(&h->pdev->dev,
  5104. "waiting %d secs for device to become ready.\n",
  5105. waittime);
  5106. }
  5107. return rc;
  5108. }
  5109. static int wait_for_device_to_become_ready(struct ctlr_info *h,
  5110. unsigned char lunaddr[],
  5111. int reply_queue)
  5112. {
  5113. int first_queue;
  5114. int last_queue;
  5115. int rq;
  5116. int rc = 0;
  5117. struct CommandList *c;
  5118. c = cmd_alloc(h);
  5119. /*
  5120. * If no specific reply queue was requested, then send the TUR
  5121. * repeatedly, requesting a reply on each reply queue; otherwise execute
  5122. * the loop exactly once using only the specified queue.
  5123. */
  5124. if (reply_queue == DEFAULT_REPLY_QUEUE) {
  5125. first_queue = 0;
  5126. last_queue = h->nreply_queues - 1;
  5127. } else {
  5128. first_queue = reply_queue;
  5129. last_queue = reply_queue;
  5130. }
  5131. for (rq = first_queue; rq <= last_queue; rq++) {
  5132. rc = hpsa_wait_for_test_unit_ready(h, c, lunaddr, rq);
  5133. if (rc)
  5134. break;
  5135. }
  5136. if (rc)
  5137. dev_warn(&h->pdev->dev, "giving up on device.\n");
  5138. else
  5139. dev_warn(&h->pdev->dev, "device is ready.\n");
  5140. cmd_free(h, c);
  5141. return rc;
  5142. }
  5143. /* Need at least one of these error handlers to keep ../scsi/hosts.c from
  5144. * complaining. Doing a host- or bus-reset can't do anything good here.
  5145. */
  5146. static int hpsa_eh_device_reset_handler(struct scsi_cmnd *scsicmd)
  5147. {
  5148. int rc;
  5149. struct ctlr_info *h;
  5150. struct hpsa_scsi_dev_t *dev;
  5151. u8 reset_type;
  5152. char msg[48];
  5153. /* find the controller to which the command to be aborted was sent */
  5154. h = sdev_to_hba(scsicmd->device);
  5155. if (h == NULL) /* paranoia */
  5156. return FAILED;
  5157. if (lockup_detected(h))
  5158. return FAILED;
  5159. dev = scsicmd->device->hostdata;
  5160. if (!dev) {
  5161. dev_err(&h->pdev->dev, "%s: device lookup failed\n", __func__);
  5162. return FAILED;
  5163. }
  5164. /* if controller locked up, we can guarantee command won't complete */
  5165. if (lockup_detected(h)) {
  5166. snprintf(msg, sizeof(msg),
  5167. "cmd %d RESET FAILED, lockup detected",
  5168. hpsa_get_cmd_index(scsicmd));
  5169. hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
  5170. return FAILED;
  5171. }
  5172. /* this reset request might be the result of a lockup; check */
  5173. if (detect_controller_lockup(h)) {
  5174. snprintf(msg, sizeof(msg),
  5175. "cmd %d RESET FAILED, new lockup detected",
  5176. hpsa_get_cmd_index(scsicmd));
  5177. hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
  5178. return FAILED;
  5179. }
  5180. /* Do not attempt on controller */
  5181. if (is_hba_lunid(dev->scsi3addr))
  5182. return SUCCESS;
  5183. if (is_logical_dev_addr_mode(dev->scsi3addr))
  5184. reset_type = HPSA_DEVICE_RESET_MSG;
  5185. else
  5186. reset_type = HPSA_PHYS_TARGET_RESET;
  5187. sprintf(msg, "resetting %s",
  5188. reset_type == HPSA_DEVICE_RESET_MSG ? "logical " : "physical ");
  5189. hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
  5190. h->reset_in_progress = 1;
  5191. /* send a reset to the SCSI LUN which the command was sent to */
  5192. rc = hpsa_do_reset(h, dev, dev->scsi3addr, reset_type,
  5193. DEFAULT_REPLY_QUEUE);
  5194. sprintf(msg, "reset %s %s",
  5195. reset_type == HPSA_DEVICE_RESET_MSG ? "logical " : "physical ",
  5196. rc == 0 ? "completed successfully" : "failed");
  5197. hpsa_show_dev_msg(KERN_WARNING, h, dev, msg);
  5198. h->reset_in_progress = 0;
  5199. return rc == 0 ? SUCCESS : FAILED;
  5200. }
  5201. static void swizzle_abort_tag(u8 *tag)
  5202. {
  5203. u8 original_tag[8];
  5204. memcpy(original_tag, tag, 8);
  5205. tag[0] = original_tag[3];
  5206. tag[1] = original_tag[2];
  5207. tag[2] = original_tag[1];
  5208. tag[3] = original_tag[0];
  5209. tag[4] = original_tag[7];
  5210. tag[5] = original_tag[6];
  5211. tag[6] = original_tag[5];
  5212. tag[7] = original_tag[4];
  5213. }
  5214. static void hpsa_get_tag(struct ctlr_info *h,
  5215. struct CommandList *c, __le32 *taglower, __le32 *tagupper)
  5216. {
  5217. u64 tag;
  5218. if (c->cmd_type == CMD_IOACCEL1) {
  5219. struct io_accel1_cmd *cm1 = (struct io_accel1_cmd *)
  5220. &h->ioaccel_cmd_pool[c->cmdindex];
  5221. tag = le64_to_cpu(cm1->tag);
  5222. *tagupper = cpu_to_le32(tag >> 32);
  5223. *taglower = cpu_to_le32(tag);
  5224. return;
  5225. }
  5226. if (c->cmd_type == CMD_IOACCEL2) {
  5227. struct io_accel2_cmd *cm2 = (struct io_accel2_cmd *)
  5228. &h->ioaccel2_cmd_pool[c->cmdindex];
  5229. /* upper tag not used in ioaccel2 mode */
  5230. memset(tagupper, 0, sizeof(*tagupper));
  5231. *taglower = cm2->Tag;
  5232. return;
  5233. }
  5234. tag = le64_to_cpu(c->Header.tag);
  5235. *tagupper = cpu_to_le32(tag >> 32);
  5236. *taglower = cpu_to_le32(tag);
  5237. }
  5238. static int hpsa_send_abort(struct ctlr_info *h, unsigned char *scsi3addr,
  5239. struct CommandList *abort, int reply_queue)
  5240. {
  5241. int rc = IO_OK;
  5242. struct CommandList *c;
  5243. struct ErrorInfo *ei;
  5244. __le32 tagupper, taglower;
  5245. c = cmd_alloc(h);
  5246. /* fill_cmd can't fail here, no buffer to map */
  5247. (void) fill_cmd(c, HPSA_ABORT_MSG, h, &abort->Header.tag,
  5248. 0, 0, scsi3addr, TYPE_MSG);
  5249. if (h->needs_abort_tags_swizzled)
  5250. swizzle_abort_tag(&c->Request.CDB[4]);
  5251. (void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, DEFAULT_TIMEOUT);
  5252. hpsa_get_tag(h, abort, &taglower, &tagupper);
  5253. dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: do_simple_cmd(abort) completed.\n",
  5254. __func__, tagupper, taglower);
  5255. /* no unmap needed here because no data xfer. */
  5256. ei = c->err_info;
  5257. switch (ei->CommandStatus) {
  5258. case CMD_SUCCESS:
  5259. break;
  5260. case CMD_TMF_STATUS:
  5261. rc = hpsa_evaluate_tmf_status(h, c);
  5262. break;
  5263. case CMD_UNABORTABLE: /* Very common, don't make noise. */
  5264. rc = -1;
  5265. break;
  5266. default:
  5267. dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: interpreting error.\n",
  5268. __func__, tagupper, taglower);
  5269. hpsa_scsi_interpret_error(h, c);
  5270. rc = -1;
  5271. break;
  5272. }
  5273. cmd_free(h, c);
  5274. dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n",
  5275. __func__, tagupper, taglower);
  5276. return rc;
  5277. }
  5278. static void setup_ioaccel2_abort_cmd(struct CommandList *c, struct ctlr_info *h,
  5279. struct CommandList *command_to_abort, int reply_queue)
  5280. {
  5281. struct io_accel2_cmd *c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
  5282. struct hpsa_tmf_struct *ac = (struct hpsa_tmf_struct *) c2;
  5283. struct io_accel2_cmd *c2a =
  5284. &h->ioaccel2_cmd_pool[command_to_abort->cmdindex];
  5285. struct scsi_cmnd *scmd = command_to_abort->scsi_cmd;
  5286. struct hpsa_scsi_dev_t *dev = scmd->device->hostdata;
  5287. if (!dev)
  5288. return;
  5289. /*
  5290. * We're overlaying struct hpsa_tmf_struct on top of something which
  5291. * was allocated as a struct io_accel2_cmd, so we better be sure it
  5292. * actually fits, and doesn't overrun the error info space.
  5293. */
  5294. BUILD_BUG_ON(sizeof(struct hpsa_tmf_struct) >
  5295. sizeof(struct io_accel2_cmd));
  5296. BUG_ON(offsetof(struct io_accel2_cmd, error_data) <
  5297. offsetof(struct hpsa_tmf_struct, error_len) +
  5298. sizeof(ac->error_len));
  5299. c->cmd_type = IOACCEL2_TMF;
  5300. c->scsi_cmd = SCSI_CMD_BUSY;
  5301. /* Adjust the DMA address to point to the accelerated command buffer */
  5302. c->busaddr = (u32) h->ioaccel2_cmd_pool_dhandle +
  5303. (c->cmdindex * sizeof(struct io_accel2_cmd));
  5304. BUG_ON(c->busaddr & 0x0000007F);
  5305. memset(ac, 0, sizeof(*c2)); /* yes this is correct */
  5306. ac->iu_type = IOACCEL2_IU_TMF_TYPE;
  5307. ac->reply_queue = reply_queue;
  5308. ac->tmf = IOACCEL2_TMF_ABORT;
  5309. ac->it_nexus = cpu_to_le32(dev->ioaccel_handle);
  5310. memset(ac->lun_id, 0, sizeof(ac->lun_id));
  5311. ac->tag = cpu_to_le64(c->cmdindex << DIRECT_LOOKUP_SHIFT);
  5312. ac->abort_tag = cpu_to_le64(le32_to_cpu(c2a->Tag));
  5313. ac->error_ptr = cpu_to_le64(c->busaddr +
  5314. offsetof(struct io_accel2_cmd, error_data));
  5315. ac->error_len = cpu_to_le32(sizeof(c2->error_data));
  5316. }
  5317. /* ioaccel2 path firmware cannot handle abort task requests.
  5318. * Change abort requests to physical target reset, and send to the
  5319. * address of the physical disk used for the ioaccel 2 command.
  5320. * Return 0 on success (IO_OK)
  5321. * -1 on failure
  5322. */
  5323. static int hpsa_send_reset_as_abort_ioaccel2(struct ctlr_info *h,
  5324. unsigned char *scsi3addr, struct CommandList *abort, int reply_queue)
  5325. {
  5326. int rc = IO_OK;
  5327. struct scsi_cmnd *scmd; /* scsi command within request being aborted */
  5328. struct hpsa_scsi_dev_t *dev; /* device to which scsi cmd was sent */
  5329. unsigned char phys_scsi3addr[8]; /* addr of phys disk with volume */
  5330. unsigned char *psa = &phys_scsi3addr[0];
  5331. /* Get a pointer to the hpsa logical device. */
  5332. scmd = abort->scsi_cmd;
  5333. dev = (struct hpsa_scsi_dev_t *)(scmd->device->hostdata);
  5334. if (dev == NULL) {
  5335. dev_warn(&h->pdev->dev,
  5336. "Cannot abort: no device pointer for command.\n");
  5337. return -1; /* not abortable */
  5338. }
  5339. if (h->raid_offload_debug > 0)
  5340. dev_info(&h->pdev->dev,
  5341. "scsi %d:%d:%d:%d %s scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  5342. h->scsi_host->host_no, dev->bus, dev->target, dev->lun,
  5343. "Reset as abort",
  5344. scsi3addr[0], scsi3addr[1], scsi3addr[2], scsi3addr[3],
  5345. scsi3addr[4], scsi3addr[5], scsi3addr[6], scsi3addr[7]);
  5346. if (!dev->offload_enabled) {
  5347. dev_warn(&h->pdev->dev,
  5348. "Can't abort: device is not operating in HP SSD Smart Path mode.\n");
  5349. return -1; /* not abortable */
  5350. }
  5351. /* Incoming scsi3addr is logical addr. We need physical disk addr. */
  5352. if (!hpsa_get_pdisk_of_ioaccel2(h, abort, psa)) {
  5353. dev_warn(&h->pdev->dev, "Can't abort: Failed lookup of physical address.\n");
  5354. return -1; /* not abortable */
  5355. }
  5356. /* send the reset */
  5357. if (h->raid_offload_debug > 0)
  5358. dev_info(&h->pdev->dev,
  5359. "Reset as abort: Resetting physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  5360. psa[0], psa[1], psa[2], psa[3],
  5361. psa[4], psa[5], psa[6], psa[7]);
  5362. rc = hpsa_do_reset(h, dev, psa, HPSA_PHYS_TARGET_RESET, reply_queue);
  5363. if (rc != 0) {
  5364. dev_warn(&h->pdev->dev,
  5365. "Reset as abort: Failed on physical device at scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  5366. psa[0], psa[1], psa[2], psa[3],
  5367. psa[4], psa[5], psa[6], psa[7]);
  5368. return rc; /* failed to reset */
  5369. }
  5370. /* wait for device to recover */
  5371. if (wait_for_device_to_become_ready(h, psa, reply_queue) != 0) {
  5372. dev_warn(&h->pdev->dev,
  5373. "Reset as abort: Failed: Device never recovered from reset: 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  5374. psa[0], psa[1], psa[2], psa[3],
  5375. psa[4], psa[5], psa[6], psa[7]);
  5376. return -1; /* failed to recover */
  5377. }
  5378. /* device recovered */
  5379. dev_info(&h->pdev->dev,
  5380. "Reset as abort: Device recovered from reset: scsi3addr 0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  5381. psa[0], psa[1], psa[2], psa[3],
  5382. psa[4], psa[5], psa[6], psa[7]);
  5383. return rc; /* success */
  5384. }
  5385. static int hpsa_send_abort_ioaccel2(struct ctlr_info *h,
  5386. struct CommandList *abort, int reply_queue)
  5387. {
  5388. int rc = IO_OK;
  5389. struct CommandList *c;
  5390. __le32 taglower, tagupper;
  5391. struct hpsa_scsi_dev_t *dev;
  5392. struct io_accel2_cmd *c2;
  5393. dev = abort->scsi_cmd->device->hostdata;
  5394. if (!dev)
  5395. return -1;
  5396. if (!dev->offload_enabled && !dev->hba_ioaccel_enabled)
  5397. return -1;
  5398. c = cmd_alloc(h);
  5399. setup_ioaccel2_abort_cmd(c, h, abort, reply_queue);
  5400. c2 = &h->ioaccel2_cmd_pool[c->cmdindex];
  5401. (void) hpsa_scsi_do_simple_cmd(h, c, reply_queue, DEFAULT_TIMEOUT);
  5402. hpsa_get_tag(h, abort, &taglower, &tagupper);
  5403. dev_dbg(&h->pdev->dev,
  5404. "%s: Tag:0x%08x:%08x: do_simple_cmd(ioaccel2 abort) completed.\n",
  5405. __func__, tagupper, taglower);
  5406. /* no unmap needed here because no data xfer. */
  5407. dev_dbg(&h->pdev->dev,
  5408. "%s: Tag:0x%08x:%08x: abort service response = 0x%02x.\n",
  5409. __func__, tagupper, taglower, c2->error_data.serv_response);
  5410. switch (c2->error_data.serv_response) {
  5411. case IOACCEL2_SERV_RESPONSE_TMF_COMPLETE:
  5412. case IOACCEL2_SERV_RESPONSE_TMF_SUCCESS:
  5413. rc = 0;
  5414. break;
  5415. case IOACCEL2_SERV_RESPONSE_TMF_REJECTED:
  5416. case IOACCEL2_SERV_RESPONSE_FAILURE:
  5417. case IOACCEL2_SERV_RESPONSE_TMF_WRONG_LUN:
  5418. rc = -1;
  5419. break;
  5420. default:
  5421. dev_warn(&h->pdev->dev,
  5422. "%s: Tag:0x%08x:%08x: unknown abort service response 0x%02x\n",
  5423. __func__, tagupper, taglower,
  5424. c2->error_data.serv_response);
  5425. rc = -1;
  5426. }
  5427. cmd_free(h, c);
  5428. dev_dbg(&h->pdev->dev, "%s: Tag:0x%08x:%08x: Finished.\n", __func__,
  5429. tagupper, taglower);
  5430. return rc;
  5431. }
  5432. static int hpsa_send_abort_both_ways(struct ctlr_info *h,
  5433. struct hpsa_scsi_dev_t *dev, struct CommandList *abort, int reply_queue)
  5434. {
  5435. /*
  5436. * ioccelerator mode 2 commands should be aborted via the
  5437. * accelerated path, since RAID path is unaware of these commands,
  5438. * but not all underlying firmware can handle abort TMF.
  5439. * Change abort to physical device reset when abort TMF is unsupported.
  5440. */
  5441. if (abort->cmd_type == CMD_IOACCEL2) {
  5442. if ((HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags) ||
  5443. dev->physical_device)
  5444. return hpsa_send_abort_ioaccel2(h, abort,
  5445. reply_queue);
  5446. else
  5447. return hpsa_send_reset_as_abort_ioaccel2(h,
  5448. dev->scsi3addr,
  5449. abort, reply_queue);
  5450. }
  5451. return hpsa_send_abort(h, dev->scsi3addr, abort, reply_queue);
  5452. }
  5453. /* Find out which reply queue a command was meant to return on */
  5454. static int hpsa_extract_reply_queue(struct ctlr_info *h,
  5455. struct CommandList *c)
  5456. {
  5457. if (c->cmd_type == CMD_IOACCEL2)
  5458. return h->ioaccel2_cmd_pool[c->cmdindex].reply_queue;
  5459. return c->Header.ReplyQueue;
  5460. }
  5461. /*
  5462. * Limit concurrency of abort commands to prevent
  5463. * over-subscription of commands
  5464. */
  5465. static inline int wait_for_available_abort_cmd(struct ctlr_info *h)
  5466. {
  5467. #define ABORT_CMD_WAIT_MSECS 5000
  5468. return !wait_event_timeout(h->abort_cmd_wait_queue,
  5469. atomic_dec_if_positive(&h->abort_cmds_available) >= 0,
  5470. msecs_to_jiffies(ABORT_CMD_WAIT_MSECS));
  5471. }
  5472. /* Send an abort for the specified command.
  5473. * If the device and controller support it,
  5474. * send a task abort request.
  5475. */
  5476. static int hpsa_eh_abort_handler(struct scsi_cmnd *sc)
  5477. {
  5478. int rc;
  5479. struct ctlr_info *h;
  5480. struct hpsa_scsi_dev_t *dev;
  5481. struct CommandList *abort; /* pointer to command to be aborted */
  5482. struct scsi_cmnd *as; /* ptr to scsi cmd inside aborted command. */
  5483. char msg[256]; /* For debug messaging. */
  5484. int ml = 0;
  5485. __le32 tagupper, taglower;
  5486. int refcount, reply_queue;
  5487. if (sc == NULL)
  5488. return FAILED;
  5489. if (sc->device == NULL)
  5490. return FAILED;
  5491. /* Find the controller of the command to be aborted */
  5492. h = sdev_to_hba(sc->device);
  5493. if (h == NULL)
  5494. return FAILED;
  5495. /* Find the device of the command to be aborted */
  5496. dev = sc->device->hostdata;
  5497. if (!dev) {
  5498. dev_err(&h->pdev->dev, "%s FAILED, Device lookup failed.\n",
  5499. msg);
  5500. return FAILED;
  5501. }
  5502. /* If controller locked up, we can guarantee command won't complete */
  5503. if (lockup_detected(h)) {
  5504. hpsa_show_dev_msg(KERN_WARNING, h, dev,
  5505. "ABORT FAILED, lockup detected");
  5506. return FAILED;
  5507. }
  5508. /* This is a good time to check if controller lockup has occurred */
  5509. if (detect_controller_lockup(h)) {
  5510. hpsa_show_dev_msg(KERN_WARNING, h, dev,
  5511. "ABORT FAILED, new lockup detected");
  5512. return FAILED;
  5513. }
  5514. /* Check that controller supports some kind of task abort */
  5515. if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags) &&
  5516. !(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
  5517. return FAILED;
  5518. memset(msg, 0, sizeof(msg));
  5519. ml += sprintf(msg+ml, "scsi %d:%d:%d:%llu %s %p",
  5520. h->scsi_host->host_no, sc->device->channel,
  5521. sc->device->id, sc->device->lun,
  5522. "Aborting command", sc);
  5523. /* Get SCSI command to be aborted */
  5524. abort = (struct CommandList *) sc->host_scribble;
  5525. if (abort == NULL) {
  5526. /* This can happen if the command already completed. */
  5527. return SUCCESS;
  5528. }
  5529. refcount = atomic_inc_return(&abort->refcount);
  5530. if (refcount == 1) { /* Command is done already. */
  5531. cmd_free(h, abort);
  5532. return SUCCESS;
  5533. }
  5534. /* Don't bother trying the abort if we know it won't work. */
  5535. if (abort->cmd_type != CMD_IOACCEL2 &&
  5536. abort->cmd_type != CMD_IOACCEL1 && !dev->supports_aborts) {
  5537. cmd_free(h, abort);
  5538. return FAILED;
  5539. }
  5540. /*
  5541. * Check that we're aborting the right command.
  5542. * It's possible the CommandList already completed and got re-used.
  5543. */
  5544. if (abort->scsi_cmd != sc) {
  5545. cmd_free(h, abort);
  5546. return SUCCESS;
  5547. }
  5548. abort->abort_pending = true;
  5549. hpsa_get_tag(h, abort, &taglower, &tagupper);
  5550. reply_queue = hpsa_extract_reply_queue(h, abort);
  5551. ml += sprintf(msg+ml, "Tag:0x%08x:%08x ", tagupper, taglower);
  5552. as = abort->scsi_cmd;
  5553. if (as != NULL)
  5554. ml += sprintf(msg+ml,
  5555. "CDBLen: %d CDB: 0x%02x%02x... SN: 0x%lx ",
  5556. as->cmd_len, as->cmnd[0], as->cmnd[1],
  5557. as->serial_number);
  5558. dev_warn(&h->pdev->dev, "%s BEING SENT\n", msg);
  5559. hpsa_show_dev_msg(KERN_WARNING, h, dev, "Aborting command");
  5560. /*
  5561. * Command is in flight, or possibly already completed
  5562. * by the firmware (but not to the scsi mid layer) but we can't
  5563. * distinguish which. Send the abort down.
  5564. */
  5565. if (wait_for_available_abort_cmd(h)) {
  5566. dev_warn(&h->pdev->dev,
  5567. "%s FAILED, timeout waiting for an abort command to become available.\n",
  5568. msg);
  5569. cmd_free(h, abort);
  5570. return FAILED;
  5571. }
  5572. rc = hpsa_send_abort_both_ways(h, dev, abort, reply_queue);
  5573. atomic_inc(&h->abort_cmds_available);
  5574. wake_up_all(&h->abort_cmd_wait_queue);
  5575. if (rc != 0) {
  5576. dev_warn(&h->pdev->dev, "%s SENT, FAILED\n", msg);
  5577. hpsa_show_dev_msg(KERN_WARNING, h, dev,
  5578. "FAILED to abort command");
  5579. cmd_free(h, abort);
  5580. return FAILED;
  5581. }
  5582. dev_info(&h->pdev->dev, "%s SENT, SUCCESS\n", msg);
  5583. wait_event(h->event_sync_wait_queue,
  5584. abort->scsi_cmd != sc || lockup_detected(h));
  5585. cmd_free(h, abort);
  5586. return !lockup_detected(h) ? SUCCESS : FAILED;
  5587. }
  5588. /*
  5589. * For operations with an associated SCSI command, a command block is allocated
  5590. * at init, and managed by cmd_tagged_alloc() and cmd_tagged_free() using the
  5591. * block request tag as an index into a table of entries. cmd_tagged_free() is
  5592. * the complement, although cmd_free() may be called instead.
  5593. */
  5594. static struct CommandList *cmd_tagged_alloc(struct ctlr_info *h,
  5595. struct scsi_cmnd *scmd)
  5596. {
  5597. int idx = hpsa_get_cmd_index(scmd);
  5598. struct CommandList *c = h->cmd_pool + idx;
  5599. if (idx < HPSA_NRESERVED_CMDS || idx >= h->nr_cmds) {
  5600. dev_err(&h->pdev->dev, "Bad block tag: %d not in [%d..%d]\n",
  5601. idx, HPSA_NRESERVED_CMDS, h->nr_cmds - 1);
  5602. /* The index value comes from the block layer, so if it's out of
  5603. * bounds, it's probably not our bug.
  5604. */
  5605. BUG();
  5606. }
  5607. atomic_inc(&c->refcount);
  5608. if (unlikely(!hpsa_is_cmd_idle(c))) {
  5609. /*
  5610. * We expect that the SCSI layer will hand us a unique tag
  5611. * value. Thus, there should never be a collision here between
  5612. * two requests...because if the selected command isn't idle
  5613. * then someone is going to be very disappointed.
  5614. */
  5615. dev_err(&h->pdev->dev,
  5616. "tag collision (tag=%d) in cmd_tagged_alloc().\n",
  5617. idx);
  5618. if (c->scsi_cmd != NULL)
  5619. scsi_print_command(c->scsi_cmd);
  5620. scsi_print_command(scmd);
  5621. }
  5622. hpsa_cmd_partial_init(h, idx, c);
  5623. return c;
  5624. }
  5625. static void cmd_tagged_free(struct ctlr_info *h, struct CommandList *c)
  5626. {
  5627. /*
  5628. * Release our reference to the block. We don't need to do anything
  5629. * else to free it, because it is accessed by index. (There's no point
  5630. * in checking the result of the decrement, since we cannot guarantee
  5631. * that there isn't a concurrent abort which is also accessing it.)
  5632. */
  5633. (void)atomic_dec(&c->refcount);
  5634. }
  5635. /*
  5636. * For operations that cannot sleep, a command block is allocated at init,
  5637. * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
  5638. * which ones are free or in use. Lock must be held when calling this.
  5639. * cmd_free() is the complement.
  5640. * This function never gives up and returns NULL. If it hangs,
  5641. * another thread must call cmd_free() to free some tags.
  5642. */
  5643. static struct CommandList *cmd_alloc(struct ctlr_info *h)
  5644. {
  5645. struct CommandList *c;
  5646. int refcount, i;
  5647. int offset = 0;
  5648. /*
  5649. * There is some *extremely* small but non-zero chance that that
  5650. * multiple threads could get in here, and one thread could
  5651. * be scanning through the list of bits looking for a free
  5652. * one, but the free ones are always behind him, and other
  5653. * threads sneak in behind him and eat them before he can
  5654. * get to them, so that while there is always a free one, a
  5655. * very unlucky thread might be starved anyway, never able to
  5656. * beat the other threads. In reality, this happens so
  5657. * infrequently as to be indistinguishable from never.
  5658. *
  5659. * Note that we start allocating commands before the SCSI host structure
  5660. * is initialized. Since the search starts at bit zero, this
  5661. * all works, since we have at least one command structure available;
  5662. * however, it means that the structures with the low indexes have to be
  5663. * reserved for driver-initiated requests, while requests from the block
  5664. * layer will use the higher indexes.
  5665. */
  5666. for (;;) {
  5667. i = find_next_zero_bit(h->cmd_pool_bits,
  5668. HPSA_NRESERVED_CMDS,
  5669. offset);
  5670. if (unlikely(i >= HPSA_NRESERVED_CMDS)) {
  5671. offset = 0;
  5672. continue;
  5673. }
  5674. c = h->cmd_pool + i;
  5675. refcount = atomic_inc_return(&c->refcount);
  5676. if (unlikely(refcount > 1)) {
  5677. cmd_free(h, c); /* already in use */
  5678. offset = (i + 1) % HPSA_NRESERVED_CMDS;
  5679. continue;
  5680. }
  5681. set_bit(i & (BITS_PER_LONG - 1),
  5682. h->cmd_pool_bits + (i / BITS_PER_LONG));
  5683. break; /* it's ours now. */
  5684. }
  5685. hpsa_cmd_partial_init(h, i, c);
  5686. return c;
  5687. }
  5688. /*
  5689. * This is the complementary operation to cmd_alloc(). Note, however, in some
  5690. * corner cases it may also be used to free blocks allocated by
  5691. * cmd_tagged_alloc() in which case the ref-count decrement does the trick and
  5692. * the clear-bit is harmless.
  5693. */
  5694. static void cmd_free(struct ctlr_info *h, struct CommandList *c)
  5695. {
  5696. if (atomic_dec_and_test(&c->refcount)) {
  5697. int i;
  5698. i = c - h->cmd_pool;
  5699. clear_bit(i & (BITS_PER_LONG - 1),
  5700. h->cmd_pool_bits + (i / BITS_PER_LONG));
  5701. }
  5702. }
  5703. #ifdef CONFIG_COMPAT
  5704. static int hpsa_ioctl32_passthru(struct scsi_device *dev, int cmd,
  5705. void __user *arg)
  5706. {
  5707. IOCTL32_Command_struct __user *arg32 =
  5708. (IOCTL32_Command_struct __user *) arg;
  5709. IOCTL_Command_struct arg64;
  5710. IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
  5711. int err;
  5712. u32 cp;
  5713. memset(&arg64, 0, sizeof(arg64));
  5714. err = 0;
  5715. err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
  5716. sizeof(arg64.LUN_info));
  5717. err |= copy_from_user(&arg64.Request, &arg32->Request,
  5718. sizeof(arg64.Request));
  5719. err |= copy_from_user(&arg64.error_info, &arg32->error_info,
  5720. sizeof(arg64.error_info));
  5721. err |= get_user(arg64.buf_size, &arg32->buf_size);
  5722. err |= get_user(cp, &arg32->buf);
  5723. arg64.buf = compat_ptr(cp);
  5724. err |= copy_to_user(p, &arg64, sizeof(arg64));
  5725. if (err)
  5726. return -EFAULT;
  5727. err = hpsa_ioctl(dev, CCISS_PASSTHRU, p);
  5728. if (err)
  5729. return err;
  5730. err |= copy_in_user(&arg32->error_info, &p->error_info,
  5731. sizeof(arg32->error_info));
  5732. if (err)
  5733. return -EFAULT;
  5734. return err;
  5735. }
  5736. static int hpsa_ioctl32_big_passthru(struct scsi_device *dev,
  5737. int cmd, void __user *arg)
  5738. {
  5739. BIG_IOCTL32_Command_struct __user *arg32 =
  5740. (BIG_IOCTL32_Command_struct __user *) arg;
  5741. BIG_IOCTL_Command_struct arg64;
  5742. BIG_IOCTL_Command_struct __user *p =
  5743. compat_alloc_user_space(sizeof(arg64));
  5744. int err;
  5745. u32 cp;
  5746. memset(&arg64, 0, sizeof(arg64));
  5747. err = 0;
  5748. err |= copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
  5749. sizeof(arg64.LUN_info));
  5750. err |= copy_from_user(&arg64.Request, &arg32->Request,
  5751. sizeof(arg64.Request));
  5752. err |= copy_from_user(&arg64.error_info, &arg32->error_info,
  5753. sizeof(arg64.error_info));
  5754. err |= get_user(arg64.buf_size, &arg32->buf_size);
  5755. err |= get_user(arg64.malloc_size, &arg32->malloc_size);
  5756. err |= get_user(cp, &arg32->buf);
  5757. arg64.buf = compat_ptr(cp);
  5758. err |= copy_to_user(p, &arg64, sizeof(arg64));
  5759. if (err)
  5760. return -EFAULT;
  5761. err = hpsa_ioctl(dev, CCISS_BIG_PASSTHRU, p);
  5762. if (err)
  5763. return err;
  5764. err |= copy_in_user(&arg32->error_info, &p->error_info,
  5765. sizeof(arg32->error_info));
  5766. if (err)
  5767. return -EFAULT;
  5768. return err;
  5769. }
  5770. static int hpsa_compat_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
  5771. {
  5772. switch (cmd) {
  5773. case CCISS_GETPCIINFO:
  5774. case CCISS_GETINTINFO:
  5775. case CCISS_SETINTINFO:
  5776. case CCISS_GETNODENAME:
  5777. case CCISS_SETNODENAME:
  5778. case CCISS_GETHEARTBEAT:
  5779. case CCISS_GETBUSTYPES:
  5780. case CCISS_GETFIRMVER:
  5781. case CCISS_GETDRIVVER:
  5782. case CCISS_REVALIDVOLS:
  5783. case CCISS_DEREGDISK:
  5784. case CCISS_REGNEWDISK:
  5785. case CCISS_REGNEWD:
  5786. case CCISS_RESCANDISK:
  5787. case CCISS_GETLUNINFO:
  5788. return hpsa_ioctl(dev, cmd, arg);
  5789. case CCISS_PASSTHRU32:
  5790. return hpsa_ioctl32_passthru(dev, cmd, arg);
  5791. case CCISS_BIG_PASSTHRU32:
  5792. return hpsa_ioctl32_big_passthru(dev, cmd, arg);
  5793. default:
  5794. return -ENOIOCTLCMD;
  5795. }
  5796. }
  5797. #endif
  5798. static int hpsa_getpciinfo_ioctl(struct ctlr_info *h, void __user *argp)
  5799. {
  5800. struct hpsa_pci_info pciinfo;
  5801. if (!argp)
  5802. return -EINVAL;
  5803. pciinfo.domain = pci_domain_nr(h->pdev->bus);
  5804. pciinfo.bus = h->pdev->bus->number;
  5805. pciinfo.dev_fn = h->pdev->devfn;
  5806. pciinfo.board_id = h->board_id;
  5807. if (copy_to_user(argp, &pciinfo, sizeof(pciinfo)))
  5808. return -EFAULT;
  5809. return 0;
  5810. }
  5811. static int hpsa_getdrivver_ioctl(struct ctlr_info *h, void __user *argp)
  5812. {
  5813. DriverVer_type DriverVer;
  5814. unsigned char vmaj, vmin, vsubmin;
  5815. int rc;
  5816. rc = sscanf(HPSA_DRIVER_VERSION, "%hhu.%hhu.%hhu",
  5817. &vmaj, &vmin, &vsubmin);
  5818. if (rc != 3) {
  5819. dev_info(&h->pdev->dev, "driver version string '%s' "
  5820. "unrecognized.", HPSA_DRIVER_VERSION);
  5821. vmaj = 0;
  5822. vmin = 0;
  5823. vsubmin = 0;
  5824. }
  5825. DriverVer = (vmaj << 16) | (vmin << 8) | vsubmin;
  5826. if (!argp)
  5827. return -EINVAL;
  5828. if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
  5829. return -EFAULT;
  5830. return 0;
  5831. }
  5832. static int hpsa_passthru_ioctl(struct ctlr_info *h, void __user *argp)
  5833. {
  5834. IOCTL_Command_struct iocommand;
  5835. struct CommandList *c;
  5836. char *buff = NULL;
  5837. u64 temp64;
  5838. int rc = 0;
  5839. if (!argp)
  5840. return -EINVAL;
  5841. if (!capable(CAP_SYS_RAWIO))
  5842. return -EPERM;
  5843. if (copy_from_user(&iocommand, argp, sizeof(iocommand)))
  5844. return -EFAULT;
  5845. if ((iocommand.buf_size < 1) &&
  5846. (iocommand.Request.Type.Direction != XFER_NONE)) {
  5847. return -EINVAL;
  5848. }
  5849. if (iocommand.buf_size > 0) {
  5850. buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
  5851. if (buff == NULL)
  5852. return -ENOMEM;
  5853. if (iocommand.Request.Type.Direction & XFER_WRITE) {
  5854. /* Copy the data into the buffer we created */
  5855. if (copy_from_user(buff, iocommand.buf,
  5856. iocommand.buf_size)) {
  5857. rc = -EFAULT;
  5858. goto out_kfree;
  5859. }
  5860. } else {
  5861. memset(buff, 0, iocommand.buf_size);
  5862. }
  5863. }
  5864. c = cmd_alloc(h);
  5865. /* Fill in the command type */
  5866. c->cmd_type = CMD_IOCTL_PEND;
  5867. c->scsi_cmd = SCSI_CMD_BUSY;
  5868. /* Fill in Command Header */
  5869. c->Header.ReplyQueue = 0; /* unused in simple mode */
  5870. if (iocommand.buf_size > 0) { /* buffer to fill */
  5871. c->Header.SGList = 1;
  5872. c->Header.SGTotal = cpu_to_le16(1);
  5873. } else { /* no buffers to fill */
  5874. c->Header.SGList = 0;
  5875. c->Header.SGTotal = cpu_to_le16(0);
  5876. }
  5877. memcpy(&c->Header.LUN, &iocommand.LUN_info, sizeof(c->Header.LUN));
  5878. /* Fill in Request block */
  5879. memcpy(&c->Request, &iocommand.Request,
  5880. sizeof(c->Request));
  5881. /* Fill in the scatter gather information */
  5882. if (iocommand.buf_size > 0) {
  5883. temp64 = pci_map_single(h->pdev, buff,
  5884. iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
  5885. if (dma_mapping_error(&h->pdev->dev, (dma_addr_t) temp64)) {
  5886. c->SG[0].Addr = cpu_to_le64(0);
  5887. c->SG[0].Len = cpu_to_le32(0);
  5888. rc = -ENOMEM;
  5889. goto out;
  5890. }
  5891. c->SG[0].Addr = cpu_to_le64(temp64);
  5892. c->SG[0].Len = cpu_to_le32(iocommand.buf_size);
  5893. c->SG[0].Ext = cpu_to_le32(HPSA_SG_LAST); /* not chaining */
  5894. }
  5895. rc = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
  5896. NO_TIMEOUT);
  5897. if (iocommand.buf_size > 0)
  5898. hpsa_pci_unmap(h->pdev, c, 1, PCI_DMA_BIDIRECTIONAL);
  5899. check_ioctl_unit_attention(h, c);
  5900. if (rc) {
  5901. rc = -EIO;
  5902. goto out;
  5903. }
  5904. /* Copy the error information out */
  5905. memcpy(&iocommand.error_info, c->err_info,
  5906. sizeof(iocommand.error_info));
  5907. if (copy_to_user(argp, &iocommand, sizeof(iocommand))) {
  5908. rc = -EFAULT;
  5909. goto out;
  5910. }
  5911. if ((iocommand.Request.Type.Direction & XFER_READ) &&
  5912. iocommand.buf_size > 0) {
  5913. /* Copy the data out of the buffer we created */
  5914. if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
  5915. rc = -EFAULT;
  5916. goto out;
  5917. }
  5918. }
  5919. out:
  5920. cmd_free(h, c);
  5921. out_kfree:
  5922. kfree(buff);
  5923. return rc;
  5924. }
  5925. static int hpsa_big_passthru_ioctl(struct ctlr_info *h, void __user *argp)
  5926. {
  5927. BIG_IOCTL_Command_struct *ioc;
  5928. struct CommandList *c;
  5929. unsigned char **buff = NULL;
  5930. int *buff_size = NULL;
  5931. u64 temp64;
  5932. BYTE sg_used = 0;
  5933. int status = 0;
  5934. u32 left;
  5935. u32 sz;
  5936. BYTE __user *data_ptr;
  5937. if (!argp)
  5938. return -EINVAL;
  5939. if (!capable(CAP_SYS_RAWIO))
  5940. return -EPERM;
  5941. ioc = (BIG_IOCTL_Command_struct *)
  5942. kmalloc(sizeof(*ioc), GFP_KERNEL);
  5943. if (!ioc) {
  5944. status = -ENOMEM;
  5945. goto cleanup1;
  5946. }
  5947. if (copy_from_user(ioc, argp, sizeof(*ioc))) {
  5948. status = -EFAULT;
  5949. goto cleanup1;
  5950. }
  5951. if ((ioc->buf_size < 1) &&
  5952. (ioc->Request.Type.Direction != XFER_NONE)) {
  5953. status = -EINVAL;
  5954. goto cleanup1;
  5955. }
  5956. /* Check kmalloc limits using all SGs */
  5957. if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
  5958. status = -EINVAL;
  5959. goto cleanup1;
  5960. }
  5961. if (ioc->buf_size > ioc->malloc_size * SG_ENTRIES_IN_CMD) {
  5962. status = -EINVAL;
  5963. goto cleanup1;
  5964. }
  5965. buff = kzalloc(SG_ENTRIES_IN_CMD * sizeof(char *), GFP_KERNEL);
  5966. if (!buff) {
  5967. status = -ENOMEM;
  5968. goto cleanup1;
  5969. }
  5970. buff_size = kmalloc(SG_ENTRIES_IN_CMD * sizeof(int), GFP_KERNEL);
  5971. if (!buff_size) {
  5972. status = -ENOMEM;
  5973. goto cleanup1;
  5974. }
  5975. left = ioc->buf_size;
  5976. data_ptr = ioc->buf;
  5977. while (left) {
  5978. sz = (left > ioc->malloc_size) ? ioc->malloc_size : left;
  5979. buff_size[sg_used] = sz;
  5980. buff[sg_used] = kmalloc(sz, GFP_KERNEL);
  5981. if (buff[sg_used] == NULL) {
  5982. status = -ENOMEM;
  5983. goto cleanup1;
  5984. }
  5985. if (ioc->Request.Type.Direction & XFER_WRITE) {
  5986. if (copy_from_user(buff[sg_used], data_ptr, sz)) {
  5987. status = -EFAULT;
  5988. goto cleanup1;
  5989. }
  5990. } else
  5991. memset(buff[sg_used], 0, sz);
  5992. left -= sz;
  5993. data_ptr += sz;
  5994. sg_used++;
  5995. }
  5996. c = cmd_alloc(h);
  5997. c->cmd_type = CMD_IOCTL_PEND;
  5998. c->scsi_cmd = SCSI_CMD_BUSY;
  5999. c->Header.ReplyQueue = 0;
  6000. c->Header.SGList = (u8) sg_used;
  6001. c->Header.SGTotal = cpu_to_le16(sg_used);
  6002. memcpy(&c->Header.LUN, &ioc->LUN_info, sizeof(c->Header.LUN));
  6003. memcpy(&c->Request, &ioc->Request, sizeof(c->Request));
  6004. if (ioc->buf_size > 0) {
  6005. int i;
  6006. for (i = 0; i < sg_used; i++) {
  6007. temp64 = pci_map_single(h->pdev, buff[i],
  6008. buff_size[i], PCI_DMA_BIDIRECTIONAL);
  6009. if (dma_mapping_error(&h->pdev->dev,
  6010. (dma_addr_t) temp64)) {
  6011. c->SG[i].Addr = cpu_to_le64(0);
  6012. c->SG[i].Len = cpu_to_le32(0);
  6013. hpsa_pci_unmap(h->pdev, c, i,
  6014. PCI_DMA_BIDIRECTIONAL);
  6015. status = -ENOMEM;
  6016. goto cleanup0;
  6017. }
  6018. c->SG[i].Addr = cpu_to_le64(temp64);
  6019. c->SG[i].Len = cpu_to_le32(buff_size[i]);
  6020. c->SG[i].Ext = cpu_to_le32(0);
  6021. }
  6022. c->SG[--i].Ext = cpu_to_le32(HPSA_SG_LAST);
  6023. }
  6024. status = hpsa_scsi_do_simple_cmd(h, c, DEFAULT_REPLY_QUEUE,
  6025. NO_TIMEOUT);
  6026. if (sg_used)
  6027. hpsa_pci_unmap(h->pdev, c, sg_used, PCI_DMA_BIDIRECTIONAL);
  6028. check_ioctl_unit_attention(h, c);
  6029. if (status) {
  6030. status = -EIO;
  6031. goto cleanup0;
  6032. }
  6033. /* Copy the error information out */
  6034. memcpy(&ioc->error_info, c->err_info, sizeof(ioc->error_info));
  6035. if (copy_to_user(argp, ioc, sizeof(*ioc))) {
  6036. status = -EFAULT;
  6037. goto cleanup0;
  6038. }
  6039. if ((ioc->Request.Type.Direction & XFER_READ) && ioc->buf_size > 0) {
  6040. int i;
  6041. /* Copy the data out of the buffer we created */
  6042. BYTE __user *ptr = ioc->buf;
  6043. for (i = 0; i < sg_used; i++) {
  6044. if (copy_to_user(ptr, buff[i], buff_size[i])) {
  6045. status = -EFAULT;
  6046. goto cleanup0;
  6047. }
  6048. ptr += buff_size[i];
  6049. }
  6050. }
  6051. status = 0;
  6052. cleanup0:
  6053. cmd_free(h, c);
  6054. cleanup1:
  6055. if (buff) {
  6056. int i;
  6057. for (i = 0; i < sg_used; i++)
  6058. kfree(buff[i]);
  6059. kfree(buff);
  6060. }
  6061. kfree(buff_size);
  6062. kfree(ioc);
  6063. return status;
  6064. }
  6065. static void check_ioctl_unit_attention(struct ctlr_info *h,
  6066. struct CommandList *c)
  6067. {
  6068. if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
  6069. c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
  6070. (void) check_for_unit_attention(h, c);
  6071. }
  6072. /*
  6073. * ioctl
  6074. */
  6075. static int hpsa_ioctl(struct scsi_device *dev, int cmd, void __user *arg)
  6076. {
  6077. struct ctlr_info *h;
  6078. void __user *argp = (void __user *)arg;
  6079. int rc;
  6080. h = sdev_to_hba(dev);
  6081. switch (cmd) {
  6082. case CCISS_DEREGDISK:
  6083. case CCISS_REGNEWDISK:
  6084. case CCISS_REGNEWD:
  6085. hpsa_scan_start(h->scsi_host);
  6086. return 0;
  6087. case CCISS_GETPCIINFO:
  6088. return hpsa_getpciinfo_ioctl(h, argp);
  6089. case CCISS_GETDRIVVER:
  6090. return hpsa_getdrivver_ioctl(h, argp);
  6091. case CCISS_PASSTHRU:
  6092. if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
  6093. return -EAGAIN;
  6094. rc = hpsa_passthru_ioctl(h, argp);
  6095. atomic_inc(&h->passthru_cmds_avail);
  6096. return rc;
  6097. case CCISS_BIG_PASSTHRU:
  6098. if (atomic_dec_if_positive(&h->passthru_cmds_avail) < 0)
  6099. return -EAGAIN;
  6100. rc = hpsa_big_passthru_ioctl(h, argp);
  6101. atomic_inc(&h->passthru_cmds_avail);
  6102. return rc;
  6103. default:
  6104. return -ENOTTY;
  6105. }
  6106. }
  6107. static void hpsa_send_host_reset(struct ctlr_info *h, unsigned char *scsi3addr,
  6108. u8 reset_type)
  6109. {
  6110. struct CommandList *c;
  6111. c = cmd_alloc(h);
  6112. /* fill_cmd can't fail here, no data buffer to map */
  6113. (void) fill_cmd(c, HPSA_DEVICE_RESET_MSG, h, NULL, 0, 0,
  6114. RAID_CTLR_LUNID, TYPE_MSG);
  6115. c->Request.CDB[1] = reset_type; /* fill_cmd defaults to target reset */
  6116. c->waiting = NULL;
  6117. enqueue_cmd_and_start_io(h, c);
  6118. /* Don't wait for completion, the reset won't complete. Don't free
  6119. * the command either. This is the last command we will send before
  6120. * re-initializing everything, so it doesn't matter and won't leak.
  6121. */
  6122. return;
  6123. }
  6124. static int fill_cmd(struct CommandList *c, u8 cmd, struct ctlr_info *h,
  6125. void *buff, size_t size, u16 page_code, unsigned char *scsi3addr,
  6126. int cmd_type)
  6127. {
  6128. int pci_dir = XFER_NONE;
  6129. u64 tag; /* for commands to be aborted */
  6130. c->cmd_type = CMD_IOCTL_PEND;
  6131. c->scsi_cmd = SCSI_CMD_BUSY;
  6132. c->Header.ReplyQueue = 0;
  6133. if (buff != NULL && size > 0) {
  6134. c->Header.SGList = 1;
  6135. c->Header.SGTotal = cpu_to_le16(1);
  6136. } else {
  6137. c->Header.SGList = 0;
  6138. c->Header.SGTotal = cpu_to_le16(0);
  6139. }
  6140. memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
  6141. if (cmd_type == TYPE_CMD) {
  6142. switch (cmd) {
  6143. case HPSA_INQUIRY:
  6144. /* are we trying to read a vital product page */
  6145. if (page_code & VPD_PAGE) {
  6146. c->Request.CDB[1] = 0x01;
  6147. c->Request.CDB[2] = (page_code & 0xff);
  6148. }
  6149. c->Request.CDBLen = 6;
  6150. c->Request.type_attr_dir =
  6151. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
  6152. c->Request.Timeout = 0;
  6153. c->Request.CDB[0] = HPSA_INQUIRY;
  6154. c->Request.CDB[4] = size & 0xFF;
  6155. break;
  6156. case HPSA_REPORT_LOG:
  6157. case HPSA_REPORT_PHYS:
  6158. /* Talking to controller so It's a physical command
  6159. mode = 00 target = 0. Nothing to write.
  6160. */
  6161. c->Request.CDBLen = 12;
  6162. c->Request.type_attr_dir =
  6163. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
  6164. c->Request.Timeout = 0;
  6165. c->Request.CDB[0] = cmd;
  6166. c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
  6167. c->Request.CDB[7] = (size >> 16) & 0xFF;
  6168. c->Request.CDB[8] = (size >> 8) & 0xFF;
  6169. c->Request.CDB[9] = size & 0xFF;
  6170. break;
  6171. case BMIC_SENSE_DIAG_OPTIONS:
  6172. c->Request.CDBLen = 16;
  6173. c->Request.type_attr_dir =
  6174. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
  6175. c->Request.Timeout = 0;
  6176. /* Spec says this should be BMIC_WRITE */
  6177. c->Request.CDB[0] = BMIC_READ;
  6178. c->Request.CDB[6] = BMIC_SENSE_DIAG_OPTIONS;
  6179. break;
  6180. case BMIC_SET_DIAG_OPTIONS:
  6181. c->Request.CDBLen = 16;
  6182. c->Request.type_attr_dir =
  6183. TYPE_ATTR_DIR(cmd_type,
  6184. ATTR_SIMPLE, XFER_WRITE);
  6185. c->Request.Timeout = 0;
  6186. c->Request.CDB[0] = BMIC_WRITE;
  6187. c->Request.CDB[6] = BMIC_SET_DIAG_OPTIONS;
  6188. break;
  6189. case HPSA_CACHE_FLUSH:
  6190. c->Request.CDBLen = 12;
  6191. c->Request.type_attr_dir =
  6192. TYPE_ATTR_DIR(cmd_type,
  6193. ATTR_SIMPLE, XFER_WRITE);
  6194. c->Request.Timeout = 0;
  6195. c->Request.CDB[0] = BMIC_WRITE;
  6196. c->Request.CDB[6] = BMIC_CACHE_FLUSH;
  6197. c->Request.CDB[7] = (size >> 8) & 0xFF;
  6198. c->Request.CDB[8] = size & 0xFF;
  6199. break;
  6200. case TEST_UNIT_READY:
  6201. c->Request.CDBLen = 6;
  6202. c->Request.type_attr_dir =
  6203. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
  6204. c->Request.Timeout = 0;
  6205. break;
  6206. case HPSA_GET_RAID_MAP:
  6207. c->Request.CDBLen = 12;
  6208. c->Request.type_attr_dir =
  6209. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
  6210. c->Request.Timeout = 0;
  6211. c->Request.CDB[0] = HPSA_CISS_READ;
  6212. c->Request.CDB[1] = cmd;
  6213. c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
  6214. c->Request.CDB[7] = (size >> 16) & 0xFF;
  6215. c->Request.CDB[8] = (size >> 8) & 0xFF;
  6216. c->Request.CDB[9] = size & 0xFF;
  6217. break;
  6218. case BMIC_SENSE_CONTROLLER_PARAMETERS:
  6219. c->Request.CDBLen = 10;
  6220. c->Request.type_attr_dir =
  6221. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
  6222. c->Request.Timeout = 0;
  6223. c->Request.CDB[0] = BMIC_READ;
  6224. c->Request.CDB[6] = BMIC_SENSE_CONTROLLER_PARAMETERS;
  6225. c->Request.CDB[7] = (size >> 16) & 0xFF;
  6226. c->Request.CDB[8] = (size >> 8) & 0xFF;
  6227. break;
  6228. case BMIC_IDENTIFY_PHYSICAL_DEVICE:
  6229. c->Request.CDBLen = 10;
  6230. c->Request.type_attr_dir =
  6231. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
  6232. c->Request.Timeout = 0;
  6233. c->Request.CDB[0] = BMIC_READ;
  6234. c->Request.CDB[6] = BMIC_IDENTIFY_PHYSICAL_DEVICE;
  6235. c->Request.CDB[7] = (size >> 16) & 0xFF;
  6236. c->Request.CDB[8] = (size >> 8) & 0XFF;
  6237. break;
  6238. case BMIC_SENSE_SUBSYSTEM_INFORMATION:
  6239. c->Request.CDBLen = 10;
  6240. c->Request.type_attr_dir =
  6241. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
  6242. c->Request.Timeout = 0;
  6243. c->Request.CDB[0] = BMIC_READ;
  6244. c->Request.CDB[6] = BMIC_SENSE_SUBSYSTEM_INFORMATION;
  6245. c->Request.CDB[7] = (size >> 16) & 0xFF;
  6246. c->Request.CDB[8] = (size >> 8) & 0XFF;
  6247. break;
  6248. case BMIC_SENSE_STORAGE_BOX_PARAMS:
  6249. c->Request.CDBLen = 10;
  6250. c->Request.type_attr_dir =
  6251. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
  6252. c->Request.Timeout = 0;
  6253. c->Request.CDB[0] = BMIC_READ;
  6254. c->Request.CDB[6] = BMIC_SENSE_STORAGE_BOX_PARAMS;
  6255. c->Request.CDB[7] = (size >> 16) & 0xFF;
  6256. c->Request.CDB[8] = (size >> 8) & 0XFF;
  6257. break;
  6258. case BMIC_IDENTIFY_CONTROLLER:
  6259. c->Request.CDBLen = 10;
  6260. c->Request.type_attr_dir =
  6261. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_READ);
  6262. c->Request.Timeout = 0;
  6263. c->Request.CDB[0] = BMIC_READ;
  6264. c->Request.CDB[1] = 0;
  6265. c->Request.CDB[2] = 0;
  6266. c->Request.CDB[3] = 0;
  6267. c->Request.CDB[4] = 0;
  6268. c->Request.CDB[5] = 0;
  6269. c->Request.CDB[6] = BMIC_IDENTIFY_CONTROLLER;
  6270. c->Request.CDB[7] = (size >> 16) & 0xFF;
  6271. c->Request.CDB[8] = (size >> 8) & 0XFF;
  6272. c->Request.CDB[9] = 0;
  6273. break;
  6274. default:
  6275. dev_warn(&h->pdev->dev, "unknown command 0x%c\n", cmd);
  6276. BUG();
  6277. return -1;
  6278. }
  6279. } else if (cmd_type == TYPE_MSG) {
  6280. switch (cmd) {
  6281. case HPSA_PHYS_TARGET_RESET:
  6282. c->Request.CDBLen = 16;
  6283. c->Request.type_attr_dir =
  6284. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
  6285. c->Request.Timeout = 0; /* Don't time out */
  6286. memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
  6287. c->Request.CDB[0] = HPSA_RESET;
  6288. c->Request.CDB[1] = HPSA_TARGET_RESET_TYPE;
  6289. /* Physical target reset needs no control bytes 4-7*/
  6290. c->Request.CDB[4] = 0x00;
  6291. c->Request.CDB[5] = 0x00;
  6292. c->Request.CDB[6] = 0x00;
  6293. c->Request.CDB[7] = 0x00;
  6294. break;
  6295. case HPSA_DEVICE_RESET_MSG:
  6296. c->Request.CDBLen = 16;
  6297. c->Request.type_attr_dir =
  6298. TYPE_ATTR_DIR(cmd_type, ATTR_SIMPLE, XFER_NONE);
  6299. c->Request.Timeout = 0; /* Don't time out */
  6300. memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
  6301. c->Request.CDB[0] = cmd;
  6302. c->Request.CDB[1] = HPSA_RESET_TYPE_LUN;
  6303. /* If bytes 4-7 are zero, it means reset the */
  6304. /* LunID device */
  6305. c->Request.CDB[4] = 0x00;
  6306. c->Request.CDB[5] = 0x00;
  6307. c->Request.CDB[6] = 0x00;
  6308. c->Request.CDB[7] = 0x00;
  6309. break;
  6310. case HPSA_ABORT_MSG:
  6311. memcpy(&tag, buff, sizeof(tag));
  6312. dev_dbg(&h->pdev->dev,
  6313. "Abort Tag:0x%016llx using rqst Tag:0x%016llx",
  6314. tag, c->Header.tag);
  6315. c->Request.CDBLen = 16;
  6316. c->Request.type_attr_dir =
  6317. TYPE_ATTR_DIR(cmd_type,
  6318. ATTR_SIMPLE, XFER_WRITE);
  6319. c->Request.Timeout = 0; /* Don't time out */
  6320. c->Request.CDB[0] = HPSA_TASK_MANAGEMENT;
  6321. c->Request.CDB[1] = HPSA_TMF_ABORT_TASK;
  6322. c->Request.CDB[2] = 0x00; /* reserved */
  6323. c->Request.CDB[3] = 0x00; /* reserved */
  6324. /* Tag to abort goes in CDB[4]-CDB[11] */
  6325. memcpy(&c->Request.CDB[4], &tag, sizeof(tag));
  6326. c->Request.CDB[12] = 0x00; /* reserved */
  6327. c->Request.CDB[13] = 0x00; /* reserved */
  6328. c->Request.CDB[14] = 0x00; /* reserved */
  6329. c->Request.CDB[15] = 0x00; /* reserved */
  6330. break;
  6331. default:
  6332. dev_warn(&h->pdev->dev, "unknown message type %d\n",
  6333. cmd);
  6334. BUG();
  6335. }
  6336. } else {
  6337. dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
  6338. BUG();
  6339. }
  6340. switch (GET_DIR(c->Request.type_attr_dir)) {
  6341. case XFER_READ:
  6342. pci_dir = PCI_DMA_FROMDEVICE;
  6343. break;
  6344. case XFER_WRITE:
  6345. pci_dir = PCI_DMA_TODEVICE;
  6346. break;
  6347. case XFER_NONE:
  6348. pci_dir = PCI_DMA_NONE;
  6349. break;
  6350. default:
  6351. pci_dir = PCI_DMA_BIDIRECTIONAL;
  6352. }
  6353. if (hpsa_map_one(h->pdev, c, buff, size, pci_dir))
  6354. return -1;
  6355. return 0;
  6356. }
  6357. /*
  6358. * Map (physical) PCI mem into (virtual) kernel space
  6359. */
  6360. static void __iomem *remap_pci_mem(ulong base, ulong size)
  6361. {
  6362. ulong page_base = ((ulong) base) & PAGE_MASK;
  6363. ulong page_offs = ((ulong) base) - page_base;
  6364. void __iomem *page_remapped = ioremap_nocache(page_base,
  6365. page_offs + size);
  6366. return page_remapped ? (page_remapped + page_offs) : NULL;
  6367. }
  6368. static inline unsigned long get_next_completion(struct ctlr_info *h, u8 q)
  6369. {
  6370. return h->access.command_completed(h, q);
  6371. }
  6372. static inline bool interrupt_pending(struct ctlr_info *h)
  6373. {
  6374. return h->access.intr_pending(h);
  6375. }
  6376. static inline long interrupt_not_for_us(struct ctlr_info *h)
  6377. {
  6378. return (h->access.intr_pending(h) == 0) ||
  6379. (h->interrupts_enabled == 0);
  6380. }
  6381. static inline int bad_tag(struct ctlr_info *h, u32 tag_index,
  6382. u32 raw_tag)
  6383. {
  6384. if (unlikely(tag_index >= h->nr_cmds)) {
  6385. dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
  6386. return 1;
  6387. }
  6388. return 0;
  6389. }
  6390. static inline void finish_cmd(struct CommandList *c)
  6391. {
  6392. dial_up_lockup_detection_on_fw_flash_complete(c->h, c);
  6393. if (likely(c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_SCSI
  6394. || c->cmd_type == CMD_IOACCEL2))
  6395. complete_scsi_command(c);
  6396. else if (c->cmd_type == CMD_IOCTL_PEND || c->cmd_type == IOACCEL2_TMF)
  6397. complete(c->waiting);
  6398. }
  6399. /* process completion of an indexed ("direct lookup") command */
  6400. static inline void process_indexed_cmd(struct ctlr_info *h,
  6401. u32 raw_tag)
  6402. {
  6403. u32 tag_index;
  6404. struct CommandList *c;
  6405. tag_index = raw_tag >> DIRECT_LOOKUP_SHIFT;
  6406. if (!bad_tag(h, tag_index, raw_tag)) {
  6407. c = h->cmd_pool + tag_index;
  6408. finish_cmd(c);
  6409. }
  6410. }
  6411. /* Some controllers, like p400, will give us one interrupt
  6412. * after a soft reset, even if we turned interrupts off.
  6413. * Only need to check for this in the hpsa_xxx_discard_completions
  6414. * functions.
  6415. */
  6416. static int ignore_bogus_interrupt(struct ctlr_info *h)
  6417. {
  6418. if (likely(!reset_devices))
  6419. return 0;
  6420. if (likely(h->interrupts_enabled))
  6421. return 0;
  6422. dev_info(&h->pdev->dev, "Received interrupt while interrupts disabled "
  6423. "(known firmware bug.) Ignoring.\n");
  6424. return 1;
  6425. }
  6426. /*
  6427. * Convert &h->q[x] (passed to interrupt handlers) back to h.
  6428. * Relies on (h-q[x] == x) being true for x such that
  6429. * 0 <= x < MAX_REPLY_QUEUES.
  6430. */
  6431. static struct ctlr_info *queue_to_hba(u8 *queue)
  6432. {
  6433. return container_of((queue - *queue), struct ctlr_info, q[0]);
  6434. }
  6435. static irqreturn_t hpsa_intx_discard_completions(int irq, void *queue)
  6436. {
  6437. struct ctlr_info *h = queue_to_hba(queue);
  6438. u8 q = *(u8 *) queue;
  6439. u32 raw_tag;
  6440. if (ignore_bogus_interrupt(h))
  6441. return IRQ_NONE;
  6442. if (interrupt_not_for_us(h))
  6443. return IRQ_NONE;
  6444. h->last_intr_timestamp = get_jiffies_64();
  6445. while (interrupt_pending(h)) {
  6446. raw_tag = get_next_completion(h, q);
  6447. while (raw_tag != FIFO_EMPTY)
  6448. raw_tag = next_command(h, q);
  6449. }
  6450. return IRQ_HANDLED;
  6451. }
  6452. static irqreturn_t hpsa_msix_discard_completions(int irq, void *queue)
  6453. {
  6454. struct ctlr_info *h = queue_to_hba(queue);
  6455. u32 raw_tag;
  6456. u8 q = *(u8 *) queue;
  6457. if (ignore_bogus_interrupt(h))
  6458. return IRQ_NONE;
  6459. h->last_intr_timestamp = get_jiffies_64();
  6460. raw_tag = get_next_completion(h, q);
  6461. while (raw_tag != FIFO_EMPTY)
  6462. raw_tag = next_command(h, q);
  6463. return IRQ_HANDLED;
  6464. }
  6465. static irqreturn_t do_hpsa_intr_intx(int irq, void *queue)
  6466. {
  6467. struct ctlr_info *h = queue_to_hba((u8 *) queue);
  6468. u32 raw_tag;
  6469. u8 q = *(u8 *) queue;
  6470. if (interrupt_not_for_us(h))
  6471. return IRQ_NONE;
  6472. h->last_intr_timestamp = get_jiffies_64();
  6473. while (interrupt_pending(h)) {
  6474. raw_tag = get_next_completion(h, q);
  6475. while (raw_tag != FIFO_EMPTY) {
  6476. process_indexed_cmd(h, raw_tag);
  6477. raw_tag = next_command(h, q);
  6478. }
  6479. }
  6480. return IRQ_HANDLED;
  6481. }
  6482. static irqreturn_t do_hpsa_intr_msi(int irq, void *queue)
  6483. {
  6484. struct ctlr_info *h = queue_to_hba(queue);
  6485. u32 raw_tag;
  6486. u8 q = *(u8 *) queue;
  6487. h->last_intr_timestamp = get_jiffies_64();
  6488. raw_tag = get_next_completion(h, q);
  6489. while (raw_tag != FIFO_EMPTY) {
  6490. process_indexed_cmd(h, raw_tag);
  6491. raw_tag = next_command(h, q);
  6492. }
  6493. return IRQ_HANDLED;
  6494. }
  6495. /* Send a message CDB to the firmware. Careful, this only works
  6496. * in simple mode, not performant mode due to the tag lookup.
  6497. * We only ever use this immediately after a controller reset.
  6498. */
  6499. static int hpsa_message(struct pci_dev *pdev, unsigned char opcode,
  6500. unsigned char type)
  6501. {
  6502. struct Command {
  6503. struct CommandListHeader CommandHeader;
  6504. struct RequestBlock Request;
  6505. struct ErrDescriptor ErrorDescriptor;
  6506. };
  6507. struct Command *cmd;
  6508. static const size_t cmd_sz = sizeof(*cmd) +
  6509. sizeof(cmd->ErrorDescriptor);
  6510. dma_addr_t paddr64;
  6511. __le32 paddr32;
  6512. u32 tag;
  6513. void __iomem *vaddr;
  6514. int i, err;
  6515. vaddr = pci_ioremap_bar(pdev, 0);
  6516. if (vaddr == NULL)
  6517. return -ENOMEM;
  6518. /* The Inbound Post Queue only accepts 32-bit physical addresses for the
  6519. * CCISS commands, so they must be allocated from the lower 4GiB of
  6520. * memory.
  6521. */
  6522. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  6523. if (err) {
  6524. iounmap(vaddr);
  6525. return err;
  6526. }
  6527. cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
  6528. if (cmd == NULL) {
  6529. iounmap(vaddr);
  6530. return -ENOMEM;
  6531. }
  6532. /* This must fit, because of the 32-bit consistent DMA mask. Also,
  6533. * although there's no guarantee, we assume that the address is at
  6534. * least 4-byte aligned (most likely, it's page-aligned).
  6535. */
  6536. paddr32 = cpu_to_le32(paddr64);
  6537. cmd->CommandHeader.ReplyQueue = 0;
  6538. cmd->CommandHeader.SGList = 0;
  6539. cmd->CommandHeader.SGTotal = cpu_to_le16(0);
  6540. cmd->CommandHeader.tag = cpu_to_le64(paddr64);
  6541. memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
  6542. cmd->Request.CDBLen = 16;
  6543. cmd->Request.type_attr_dir =
  6544. TYPE_ATTR_DIR(TYPE_MSG, ATTR_HEADOFQUEUE, XFER_NONE);
  6545. cmd->Request.Timeout = 0; /* Don't time out */
  6546. cmd->Request.CDB[0] = opcode;
  6547. cmd->Request.CDB[1] = type;
  6548. memset(&cmd->Request.CDB[2], 0, 14); /* rest of the CDB is reserved */
  6549. cmd->ErrorDescriptor.Addr =
  6550. cpu_to_le64((le32_to_cpu(paddr32) + sizeof(*cmd)));
  6551. cmd->ErrorDescriptor.Len = cpu_to_le32(sizeof(struct ErrorInfo));
  6552. writel(le32_to_cpu(paddr32), vaddr + SA5_REQUEST_PORT_OFFSET);
  6553. for (i = 0; i < HPSA_MSG_SEND_RETRY_LIMIT; i++) {
  6554. tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
  6555. if ((tag & ~HPSA_SIMPLE_ERROR_BITS) == paddr64)
  6556. break;
  6557. msleep(HPSA_MSG_SEND_RETRY_INTERVAL_MSECS);
  6558. }
  6559. iounmap(vaddr);
  6560. /* we leak the DMA buffer here ... no choice since the controller could
  6561. * still complete the command.
  6562. */
  6563. if (i == HPSA_MSG_SEND_RETRY_LIMIT) {
  6564. dev_err(&pdev->dev, "controller message %02x:%02x timed out\n",
  6565. opcode, type);
  6566. return -ETIMEDOUT;
  6567. }
  6568. pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
  6569. if (tag & HPSA_ERROR_BIT) {
  6570. dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
  6571. opcode, type);
  6572. return -EIO;
  6573. }
  6574. dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
  6575. opcode, type);
  6576. return 0;
  6577. }
  6578. #define hpsa_noop(p) hpsa_message(p, 3, 0)
  6579. static int hpsa_controller_hard_reset(struct pci_dev *pdev,
  6580. void __iomem *vaddr, u32 use_doorbell)
  6581. {
  6582. if (use_doorbell) {
  6583. /* For everything after the P600, the PCI power state method
  6584. * of resetting the controller doesn't work, so we have this
  6585. * other way using the doorbell register.
  6586. */
  6587. dev_info(&pdev->dev, "using doorbell to reset controller\n");
  6588. writel(use_doorbell, vaddr + SA5_DOORBELL);
  6589. /* PMC hardware guys tell us we need a 10 second delay after
  6590. * doorbell reset and before any attempt to talk to the board
  6591. * at all to ensure that this actually works and doesn't fall
  6592. * over in some weird corner cases.
  6593. */
  6594. msleep(10000);
  6595. } else { /* Try to do it the PCI power state way */
  6596. /* Quoting from the Open CISS Specification: "The Power
  6597. * Management Control/Status Register (CSR) controls the power
  6598. * state of the device. The normal operating state is D0,
  6599. * CSR=00h. The software off state is D3, CSR=03h. To reset
  6600. * the controller, place the interface device in D3 then to D0,
  6601. * this causes a secondary PCI reset which will reset the
  6602. * controller." */
  6603. int rc = 0;
  6604. dev_info(&pdev->dev, "using PCI PM to reset controller\n");
  6605. /* enter the D3hot power management state */
  6606. rc = pci_set_power_state(pdev, PCI_D3hot);
  6607. if (rc)
  6608. return rc;
  6609. msleep(500);
  6610. /* enter the D0 power management state */
  6611. rc = pci_set_power_state(pdev, PCI_D0);
  6612. if (rc)
  6613. return rc;
  6614. /*
  6615. * The P600 requires a small delay when changing states.
  6616. * Otherwise we may think the board did not reset and we bail.
  6617. * This for kdump only and is particular to the P600.
  6618. */
  6619. msleep(500);
  6620. }
  6621. return 0;
  6622. }
  6623. static void init_driver_version(char *driver_version, int len)
  6624. {
  6625. memset(driver_version, 0, len);
  6626. strncpy(driver_version, HPSA " " HPSA_DRIVER_VERSION, len - 1);
  6627. }
  6628. static int write_driver_ver_to_cfgtable(struct CfgTable __iomem *cfgtable)
  6629. {
  6630. char *driver_version;
  6631. int i, size = sizeof(cfgtable->driver_version);
  6632. driver_version = kmalloc(size, GFP_KERNEL);
  6633. if (!driver_version)
  6634. return -ENOMEM;
  6635. init_driver_version(driver_version, size);
  6636. for (i = 0; i < size; i++)
  6637. writeb(driver_version[i], &cfgtable->driver_version[i]);
  6638. kfree(driver_version);
  6639. return 0;
  6640. }
  6641. static void read_driver_ver_from_cfgtable(struct CfgTable __iomem *cfgtable,
  6642. unsigned char *driver_ver)
  6643. {
  6644. int i;
  6645. for (i = 0; i < sizeof(cfgtable->driver_version); i++)
  6646. driver_ver[i] = readb(&cfgtable->driver_version[i]);
  6647. }
  6648. static int controller_reset_failed(struct CfgTable __iomem *cfgtable)
  6649. {
  6650. char *driver_ver, *old_driver_ver;
  6651. int rc, size = sizeof(cfgtable->driver_version);
  6652. old_driver_ver = kmalloc(2 * size, GFP_KERNEL);
  6653. if (!old_driver_ver)
  6654. return -ENOMEM;
  6655. driver_ver = old_driver_ver + size;
  6656. /* After a reset, the 32 bytes of "driver version" in the cfgtable
  6657. * should have been changed, otherwise we know the reset failed.
  6658. */
  6659. init_driver_version(old_driver_ver, size);
  6660. read_driver_ver_from_cfgtable(cfgtable, driver_ver);
  6661. rc = !memcmp(driver_ver, old_driver_ver, size);
  6662. kfree(old_driver_ver);
  6663. return rc;
  6664. }
  6665. /* This does a hard reset of the controller using PCI power management
  6666. * states or the using the doorbell register.
  6667. */
  6668. static int hpsa_kdump_hard_reset_controller(struct pci_dev *pdev, u32 board_id)
  6669. {
  6670. u64 cfg_offset;
  6671. u32 cfg_base_addr;
  6672. u64 cfg_base_addr_index;
  6673. void __iomem *vaddr;
  6674. unsigned long paddr;
  6675. u32 misc_fw_support;
  6676. int rc;
  6677. struct CfgTable __iomem *cfgtable;
  6678. u32 use_doorbell;
  6679. u16 command_register;
  6680. /* For controllers as old as the P600, this is very nearly
  6681. * the same thing as
  6682. *
  6683. * pci_save_state(pci_dev);
  6684. * pci_set_power_state(pci_dev, PCI_D3hot);
  6685. * pci_set_power_state(pci_dev, PCI_D0);
  6686. * pci_restore_state(pci_dev);
  6687. *
  6688. * For controllers newer than the P600, the pci power state
  6689. * method of resetting doesn't work so we have another way
  6690. * using the doorbell register.
  6691. */
  6692. if (!ctlr_is_resettable(board_id)) {
  6693. dev_warn(&pdev->dev, "Controller not resettable\n");
  6694. return -ENODEV;
  6695. }
  6696. /* if controller is soft- but not hard resettable... */
  6697. if (!ctlr_is_hard_resettable(board_id))
  6698. return -ENOTSUPP; /* try soft reset later. */
  6699. /* Save the PCI command register */
  6700. pci_read_config_word(pdev, 4, &command_register);
  6701. pci_save_state(pdev);
  6702. /* find the first memory BAR, so we can find the cfg table */
  6703. rc = hpsa_pci_find_memory_BAR(pdev, &paddr);
  6704. if (rc)
  6705. return rc;
  6706. vaddr = remap_pci_mem(paddr, 0x250);
  6707. if (!vaddr)
  6708. return -ENOMEM;
  6709. /* find cfgtable in order to check if reset via doorbell is supported */
  6710. rc = hpsa_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
  6711. &cfg_base_addr_index, &cfg_offset);
  6712. if (rc)
  6713. goto unmap_vaddr;
  6714. cfgtable = remap_pci_mem(pci_resource_start(pdev,
  6715. cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
  6716. if (!cfgtable) {
  6717. rc = -ENOMEM;
  6718. goto unmap_vaddr;
  6719. }
  6720. rc = write_driver_ver_to_cfgtable(cfgtable);
  6721. if (rc)
  6722. goto unmap_cfgtable;
  6723. /* If reset via doorbell register is supported, use that.
  6724. * There are two such methods. Favor the newest method.
  6725. */
  6726. misc_fw_support = readl(&cfgtable->misc_fw_support);
  6727. use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET2;
  6728. if (use_doorbell) {
  6729. use_doorbell = DOORBELL_CTLR_RESET2;
  6730. } else {
  6731. use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
  6732. if (use_doorbell) {
  6733. dev_warn(&pdev->dev,
  6734. "Soft reset not supported. Firmware update is required.\n");
  6735. rc = -ENOTSUPP; /* try soft reset */
  6736. goto unmap_cfgtable;
  6737. }
  6738. }
  6739. rc = hpsa_controller_hard_reset(pdev, vaddr, use_doorbell);
  6740. if (rc)
  6741. goto unmap_cfgtable;
  6742. pci_restore_state(pdev);
  6743. pci_write_config_word(pdev, 4, command_register);
  6744. /* Some devices (notably the HP Smart Array 5i Controller)
  6745. need a little pause here */
  6746. msleep(HPSA_POST_RESET_PAUSE_MSECS);
  6747. rc = hpsa_wait_for_board_state(pdev, vaddr, BOARD_READY);
  6748. if (rc) {
  6749. dev_warn(&pdev->dev,
  6750. "Failed waiting for board to become ready after hard reset\n");
  6751. goto unmap_cfgtable;
  6752. }
  6753. rc = controller_reset_failed(vaddr);
  6754. if (rc < 0)
  6755. goto unmap_cfgtable;
  6756. if (rc) {
  6757. dev_warn(&pdev->dev, "Unable to successfully reset "
  6758. "controller. Will try soft reset.\n");
  6759. rc = -ENOTSUPP;
  6760. } else {
  6761. dev_info(&pdev->dev, "board ready after hard reset.\n");
  6762. }
  6763. unmap_cfgtable:
  6764. iounmap(cfgtable);
  6765. unmap_vaddr:
  6766. iounmap(vaddr);
  6767. return rc;
  6768. }
  6769. /*
  6770. * We cannot read the structure directly, for portability we must use
  6771. * the io functions.
  6772. * This is for debug only.
  6773. */
  6774. static void print_cfg_table(struct device *dev, struct CfgTable __iomem *tb)
  6775. {
  6776. #ifdef HPSA_DEBUG
  6777. int i;
  6778. char temp_name[17];
  6779. dev_info(dev, "Controller Configuration information\n");
  6780. dev_info(dev, "------------------------------------\n");
  6781. for (i = 0; i < 4; i++)
  6782. temp_name[i] = readb(&(tb->Signature[i]));
  6783. temp_name[4] = '\0';
  6784. dev_info(dev, " Signature = %s\n", temp_name);
  6785. dev_info(dev, " Spec Number = %d\n", readl(&(tb->SpecValence)));
  6786. dev_info(dev, " Transport methods supported = 0x%x\n",
  6787. readl(&(tb->TransportSupport)));
  6788. dev_info(dev, " Transport methods active = 0x%x\n",
  6789. readl(&(tb->TransportActive)));
  6790. dev_info(dev, " Requested transport Method = 0x%x\n",
  6791. readl(&(tb->HostWrite.TransportRequest)));
  6792. dev_info(dev, " Coalesce Interrupt Delay = 0x%x\n",
  6793. readl(&(tb->HostWrite.CoalIntDelay)));
  6794. dev_info(dev, " Coalesce Interrupt Count = 0x%x\n",
  6795. readl(&(tb->HostWrite.CoalIntCount)));
  6796. dev_info(dev, " Max outstanding commands = %d\n",
  6797. readl(&(tb->CmdsOutMax)));
  6798. dev_info(dev, " Bus Types = 0x%x\n", readl(&(tb->BusTypes)));
  6799. for (i = 0; i < 16; i++)
  6800. temp_name[i] = readb(&(tb->ServerName[i]));
  6801. temp_name[16] = '\0';
  6802. dev_info(dev, " Server Name = %s\n", temp_name);
  6803. dev_info(dev, " Heartbeat Counter = 0x%x\n\n\n",
  6804. readl(&(tb->HeartBeat)));
  6805. #endif /* HPSA_DEBUG */
  6806. }
  6807. static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
  6808. {
  6809. int i, offset, mem_type, bar_type;
  6810. if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
  6811. return 0;
  6812. offset = 0;
  6813. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  6814. bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
  6815. if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
  6816. offset += 4;
  6817. else {
  6818. mem_type = pci_resource_flags(pdev, i) &
  6819. PCI_BASE_ADDRESS_MEM_TYPE_MASK;
  6820. switch (mem_type) {
  6821. case PCI_BASE_ADDRESS_MEM_TYPE_32:
  6822. case PCI_BASE_ADDRESS_MEM_TYPE_1M:
  6823. offset += 4; /* 32 bit */
  6824. break;
  6825. case PCI_BASE_ADDRESS_MEM_TYPE_64:
  6826. offset += 8;
  6827. break;
  6828. default: /* reserved in PCI 2.2 */
  6829. dev_warn(&pdev->dev,
  6830. "base address is invalid\n");
  6831. return -1;
  6832. break;
  6833. }
  6834. }
  6835. if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
  6836. return i + 1;
  6837. }
  6838. return -1;
  6839. }
  6840. static void hpsa_disable_interrupt_mode(struct ctlr_info *h)
  6841. {
  6842. if (h->msix_vector) {
  6843. if (h->pdev->msix_enabled)
  6844. pci_disable_msix(h->pdev);
  6845. h->msix_vector = 0;
  6846. } else if (h->msi_vector) {
  6847. if (h->pdev->msi_enabled)
  6848. pci_disable_msi(h->pdev);
  6849. h->msi_vector = 0;
  6850. }
  6851. }
  6852. /* If MSI/MSI-X is supported by the kernel we will try to enable it on
  6853. * controllers that are capable. If not, we use legacy INTx mode.
  6854. */
  6855. static void hpsa_interrupt_mode(struct ctlr_info *h)
  6856. {
  6857. #ifdef CONFIG_PCI_MSI
  6858. int err, i;
  6859. struct msix_entry hpsa_msix_entries[MAX_REPLY_QUEUES];
  6860. for (i = 0; i < MAX_REPLY_QUEUES; i++) {
  6861. hpsa_msix_entries[i].vector = 0;
  6862. hpsa_msix_entries[i].entry = i;
  6863. }
  6864. /* Some boards advertise MSI but don't really support it */
  6865. if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
  6866. (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
  6867. goto default_int_mode;
  6868. if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
  6869. dev_info(&h->pdev->dev, "MSI-X capable controller\n");
  6870. h->msix_vector = MAX_REPLY_QUEUES;
  6871. if (h->msix_vector > num_online_cpus())
  6872. h->msix_vector = num_online_cpus();
  6873. err = pci_enable_msix_range(h->pdev, hpsa_msix_entries,
  6874. 1, h->msix_vector);
  6875. if (err < 0) {
  6876. dev_warn(&h->pdev->dev, "MSI-X init failed %d\n", err);
  6877. h->msix_vector = 0;
  6878. goto single_msi_mode;
  6879. } else if (err < h->msix_vector) {
  6880. dev_warn(&h->pdev->dev, "only %d MSI-X vectors "
  6881. "available\n", err);
  6882. }
  6883. h->msix_vector = err;
  6884. for (i = 0; i < h->msix_vector; i++)
  6885. h->intr[i] = hpsa_msix_entries[i].vector;
  6886. return;
  6887. }
  6888. single_msi_mode:
  6889. if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
  6890. dev_info(&h->pdev->dev, "MSI capable controller\n");
  6891. if (!pci_enable_msi(h->pdev))
  6892. h->msi_vector = 1;
  6893. else
  6894. dev_warn(&h->pdev->dev, "MSI init failed\n");
  6895. }
  6896. default_int_mode:
  6897. #endif /* CONFIG_PCI_MSI */
  6898. /* if we get here we're going to use the default interrupt mode */
  6899. h->intr[h->intr_mode] = h->pdev->irq;
  6900. }
  6901. static int hpsa_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
  6902. {
  6903. int i;
  6904. u32 subsystem_vendor_id, subsystem_device_id;
  6905. subsystem_vendor_id = pdev->subsystem_vendor;
  6906. subsystem_device_id = pdev->subsystem_device;
  6907. *board_id = ((subsystem_device_id << 16) & 0xffff0000) |
  6908. subsystem_vendor_id;
  6909. for (i = 0; i < ARRAY_SIZE(products); i++)
  6910. if (*board_id == products[i].board_id)
  6911. return i;
  6912. if ((subsystem_vendor_id != PCI_VENDOR_ID_HP &&
  6913. subsystem_vendor_id != PCI_VENDOR_ID_COMPAQ) ||
  6914. !hpsa_allow_any) {
  6915. dev_warn(&pdev->dev, "unrecognized board ID: "
  6916. "0x%08x, ignoring.\n", *board_id);
  6917. return -ENODEV;
  6918. }
  6919. return ARRAY_SIZE(products) - 1; /* generic unknown smart array */
  6920. }
  6921. static int hpsa_pci_find_memory_BAR(struct pci_dev *pdev,
  6922. unsigned long *memory_bar)
  6923. {
  6924. int i;
  6925. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  6926. if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
  6927. /* addressing mode bits already removed */
  6928. *memory_bar = pci_resource_start(pdev, i);
  6929. dev_dbg(&pdev->dev, "memory BAR = %lx\n",
  6930. *memory_bar);
  6931. return 0;
  6932. }
  6933. dev_warn(&pdev->dev, "no memory BAR found\n");
  6934. return -ENODEV;
  6935. }
  6936. static int hpsa_wait_for_board_state(struct pci_dev *pdev, void __iomem *vaddr,
  6937. int wait_for_ready)
  6938. {
  6939. int i, iterations;
  6940. u32 scratchpad;
  6941. if (wait_for_ready)
  6942. iterations = HPSA_BOARD_READY_ITERATIONS;
  6943. else
  6944. iterations = HPSA_BOARD_NOT_READY_ITERATIONS;
  6945. for (i = 0; i < iterations; i++) {
  6946. scratchpad = readl(vaddr + SA5_SCRATCHPAD_OFFSET);
  6947. if (wait_for_ready) {
  6948. if (scratchpad == HPSA_FIRMWARE_READY)
  6949. return 0;
  6950. } else {
  6951. if (scratchpad != HPSA_FIRMWARE_READY)
  6952. return 0;
  6953. }
  6954. msleep(HPSA_BOARD_READY_POLL_INTERVAL_MSECS);
  6955. }
  6956. dev_warn(&pdev->dev, "board not ready, timed out.\n");
  6957. return -ENODEV;
  6958. }
  6959. static int hpsa_find_cfg_addrs(struct pci_dev *pdev, void __iomem *vaddr,
  6960. u32 *cfg_base_addr, u64 *cfg_base_addr_index,
  6961. u64 *cfg_offset)
  6962. {
  6963. *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
  6964. *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
  6965. *cfg_base_addr &= (u32) 0x0000ffff;
  6966. *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
  6967. if (*cfg_base_addr_index == -1) {
  6968. dev_warn(&pdev->dev, "cannot find cfg_base_addr_index\n");
  6969. return -ENODEV;
  6970. }
  6971. return 0;
  6972. }
  6973. static void hpsa_free_cfgtables(struct ctlr_info *h)
  6974. {
  6975. if (h->transtable) {
  6976. iounmap(h->transtable);
  6977. h->transtable = NULL;
  6978. }
  6979. if (h->cfgtable) {
  6980. iounmap(h->cfgtable);
  6981. h->cfgtable = NULL;
  6982. }
  6983. }
  6984. /* Find and map CISS config table and transfer table
  6985. + * several items must be unmapped (freed) later
  6986. + * */
  6987. static int hpsa_find_cfgtables(struct ctlr_info *h)
  6988. {
  6989. u64 cfg_offset;
  6990. u32 cfg_base_addr;
  6991. u64 cfg_base_addr_index;
  6992. u32 trans_offset;
  6993. int rc;
  6994. rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
  6995. &cfg_base_addr_index, &cfg_offset);
  6996. if (rc)
  6997. return rc;
  6998. h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
  6999. cfg_base_addr_index) + cfg_offset, sizeof(*h->cfgtable));
  7000. if (!h->cfgtable) {
  7001. dev_err(&h->pdev->dev, "Failed mapping cfgtable\n");
  7002. return -ENOMEM;
  7003. }
  7004. rc = write_driver_ver_to_cfgtable(h->cfgtable);
  7005. if (rc)
  7006. return rc;
  7007. /* Find performant mode table. */
  7008. trans_offset = readl(&h->cfgtable->TransMethodOffset);
  7009. h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
  7010. cfg_base_addr_index)+cfg_offset+trans_offset,
  7011. sizeof(*h->transtable));
  7012. if (!h->transtable) {
  7013. dev_err(&h->pdev->dev, "Failed mapping transfer table\n");
  7014. hpsa_free_cfgtables(h);
  7015. return -ENOMEM;
  7016. }
  7017. return 0;
  7018. }
  7019. static void hpsa_get_max_perf_mode_cmds(struct ctlr_info *h)
  7020. {
  7021. #define MIN_MAX_COMMANDS 16
  7022. BUILD_BUG_ON(MIN_MAX_COMMANDS <= HPSA_NRESERVED_CMDS);
  7023. h->max_commands = readl(&h->cfgtable->MaxPerformantModeCommands);
  7024. /* Limit commands in memory limited kdump scenario. */
  7025. if (reset_devices && h->max_commands > 32)
  7026. h->max_commands = 32;
  7027. if (h->max_commands < MIN_MAX_COMMANDS) {
  7028. dev_warn(&h->pdev->dev,
  7029. "Controller reports max supported commands of %d Using %d instead. Ensure that firmware is up to date.\n",
  7030. h->max_commands,
  7031. MIN_MAX_COMMANDS);
  7032. h->max_commands = MIN_MAX_COMMANDS;
  7033. }
  7034. }
  7035. /* If the controller reports that the total max sg entries is greater than 512,
  7036. * then we know that chained SG blocks work. (Original smart arrays did not
  7037. * support chained SG blocks and would return zero for max sg entries.)
  7038. */
  7039. static int hpsa_supports_chained_sg_blocks(struct ctlr_info *h)
  7040. {
  7041. return h->maxsgentries > 512;
  7042. }
  7043. /* Interrogate the hardware for some limits:
  7044. * max commands, max SG elements without chaining, and with chaining,
  7045. * SG chain block size, etc.
  7046. */
  7047. static void hpsa_find_board_params(struct ctlr_info *h)
  7048. {
  7049. hpsa_get_max_perf_mode_cmds(h);
  7050. h->nr_cmds = h->max_commands;
  7051. h->maxsgentries = readl(&(h->cfgtable->MaxScatterGatherElements));
  7052. h->fw_support = readl(&(h->cfgtable->misc_fw_support));
  7053. if (hpsa_supports_chained_sg_blocks(h)) {
  7054. /* Limit in-command s/g elements to 32 save dma'able memory. */
  7055. h->max_cmd_sg_entries = 32;
  7056. h->chainsize = h->maxsgentries - h->max_cmd_sg_entries;
  7057. h->maxsgentries--; /* save one for chain pointer */
  7058. } else {
  7059. /*
  7060. * Original smart arrays supported at most 31 s/g entries
  7061. * embedded inline in the command (trying to use more
  7062. * would lock up the controller)
  7063. */
  7064. h->max_cmd_sg_entries = 31;
  7065. h->maxsgentries = 31; /* default to traditional values */
  7066. h->chainsize = 0;
  7067. }
  7068. /* Find out what task management functions are supported and cache */
  7069. h->TMFSupportFlags = readl(&(h->cfgtable->TMFSupportFlags));
  7070. if (!(HPSATMF_PHYS_TASK_ABORT & h->TMFSupportFlags))
  7071. dev_warn(&h->pdev->dev, "Physical aborts not supported\n");
  7072. if (!(HPSATMF_LOG_TASK_ABORT & h->TMFSupportFlags))
  7073. dev_warn(&h->pdev->dev, "Logical aborts not supported\n");
  7074. if (!(HPSATMF_IOACCEL_ENABLED & h->TMFSupportFlags))
  7075. dev_warn(&h->pdev->dev, "HP SSD Smart Path aborts not supported\n");
  7076. }
  7077. static inline bool hpsa_CISS_signature_present(struct ctlr_info *h)
  7078. {
  7079. if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
  7080. dev_err(&h->pdev->dev, "not a valid CISS config table\n");
  7081. return false;
  7082. }
  7083. return true;
  7084. }
  7085. static inline void hpsa_set_driver_support_bits(struct ctlr_info *h)
  7086. {
  7087. u32 driver_support;
  7088. driver_support = readl(&(h->cfgtable->driver_support));
  7089. /* Need to enable prefetch in the SCSI core for 6400 in x86 */
  7090. #ifdef CONFIG_X86
  7091. driver_support |= ENABLE_SCSI_PREFETCH;
  7092. #endif
  7093. driver_support |= ENABLE_UNIT_ATTN;
  7094. writel(driver_support, &(h->cfgtable->driver_support));
  7095. }
  7096. /* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result
  7097. * in a prefetch beyond physical memory.
  7098. */
  7099. static inline void hpsa_p600_dma_prefetch_quirk(struct ctlr_info *h)
  7100. {
  7101. u32 dma_prefetch;
  7102. if (h->board_id != 0x3225103C)
  7103. return;
  7104. dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
  7105. dma_prefetch |= 0x8000;
  7106. writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
  7107. }
  7108. static int hpsa_wait_for_clear_event_notify_ack(struct ctlr_info *h)
  7109. {
  7110. int i;
  7111. u32 doorbell_value;
  7112. unsigned long flags;
  7113. /* wait until the clear_event_notify bit 6 is cleared by controller. */
  7114. for (i = 0; i < MAX_CLEAR_EVENT_WAIT; i++) {
  7115. spin_lock_irqsave(&h->lock, flags);
  7116. doorbell_value = readl(h->vaddr + SA5_DOORBELL);
  7117. spin_unlock_irqrestore(&h->lock, flags);
  7118. if (!(doorbell_value & DOORBELL_CLEAR_EVENTS))
  7119. goto done;
  7120. /* delay and try again */
  7121. msleep(CLEAR_EVENT_WAIT_INTERVAL);
  7122. }
  7123. return -ENODEV;
  7124. done:
  7125. return 0;
  7126. }
  7127. static int hpsa_wait_for_mode_change_ack(struct ctlr_info *h)
  7128. {
  7129. int i;
  7130. u32 doorbell_value;
  7131. unsigned long flags;
  7132. /* under certain very rare conditions, this can take awhile.
  7133. * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
  7134. * as we enter this code.)
  7135. */
  7136. for (i = 0; i < MAX_MODE_CHANGE_WAIT; i++) {
  7137. if (h->remove_in_progress)
  7138. goto done;
  7139. spin_lock_irqsave(&h->lock, flags);
  7140. doorbell_value = readl(h->vaddr + SA5_DOORBELL);
  7141. spin_unlock_irqrestore(&h->lock, flags);
  7142. if (!(doorbell_value & CFGTBL_ChangeReq))
  7143. goto done;
  7144. /* delay and try again */
  7145. msleep(MODE_CHANGE_WAIT_INTERVAL);
  7146. }
  7147. return -ENODEV;
  7148. done:
  7149. return 0;
  7150. }
  7151. /* return -ENODEV or other reason on error, 0 on success */
  7152. static int hpsa_enter_simple_mode(struct ctlr_info *h)
  7153. {
  7154. u32 trans_support;
  7155. trans_support = readl(&(h->cfgtable->TransportSupport));
  7156. if (!(trans_support & SIMPLE_MODE))
  7157. return -ENOTSUPP;
  7158. h->max_commands = readl(&(h->cfgtable->CmdsOutMax));
  7159. /* Update the field, and then ring the doorbell */
  7160. writel(CFGTBL_Trans_Simple, &(h->cfgtable->HostWrite.TransportRequest));
  7161. writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
  7162. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  7163. if (hpsa_wait_for_mode_change_ack(h))
  7164. goto error;
  7165. print_cfg_table(&h->pdev->dev, h->cfgtable);
  7166. if (!(readl(&(h->cfgtable->TransportActive)) & CFGTBL_Trans_Simple))
  7167. goto error;
  7168. h->transMethod = CFGTBL_Trans_Simple;
  7169. return 0;
  7170. error:
  7171. dev_err(&h->pdev->dev, "failed to enter simple mode\n");
  7172. return -ENODEV;
  7173. }
  7174. /* free items allocated or mapped by hpsa_pci_init */
  7175. static void hpsa_free_pci_init(struct ctlr_info *h)
  7176. {
  7177. hpsa_free_cfgtables(h); /* pci_init 4 */
  7178. iounmap(h->vaddr); /* pci_init 3 */
  7179. h->vaddr = NULL;
  7180. hpsa_disable_interrupt_mode(h); /* pci_init 2 */
  7181. /*
  7182. * call pci_disable_device before pci_release_regions per
  7183. * Documentation/PCI/pci.txt
  7184. */
  7185. pci_disable_device(h->pdev); /* pci_init 1 */
  7186. pci_release_regions(h->pdev); /* pci_init 2 */
  7187. }
  7188. /* several items must be freed later */
  7189. static int hpsa_pci_init(struct ctlr_info *h)
  7190. {
  7191. int prod_index, err;
  7192. prod_index = hpsa_lookup_board_id(h->pdev, &h->board_id);
  7193. if (prod_index < 0)
  7194. return prod_index;
  7195. h->product_name = products[prod_index].product_name;
  7196. h->access = *(products[prod_index].access);
  7197. h->needs_abort_tags_swizzled =
  7198. ctlr_needs_abort_tags_swizzled(h->board_id);
  7199. pci_disable_link_state(h->pdev, PCIE_LINK_STATE_L0S |
  7200. PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
  7201. err = pci_enable_device(h->pdev);
  7202. if (err) {
  7203. dev_err(&h->pdev->dev, "failed to enable PCI device\n");
  7204. pci_disable_device(h->pdev);
  7205. return err;
  7206. }
  7207. err = pci_request_regions(h->pdev, HPSA);
  7208. if (err) {
  7209. dev_err(&h->pdev->dev,
  7210. "failed to obtain PCI resources\n");
  7211. pci_disable_device(h->pdev);
  7212. return err;
  7213. }
  7214. pci_set_master(h->pdev);
  7215. hpsa_interrupt_mode(h);
  7216. err = hpsa_pci_find_memory_BAR(h->pdev, &h->paddr);
  7217. if (err)
  7218. goto clean2; /* intmode+region, pci */
  7219. h->vaddr = remap_pci_mem(h->paddr, 0x250);
  7220. if (!h->vaddr) {
  7221. dev_err(&h->pdev->dev, "failed to remap PCI mem\n");
  7222. err = -ENOMEM;
  7223. goto clean2; /* intmode+region, pci */
  7224. }
  7225. err = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
  7226. if (err)
  7227. goto clean3; /* vaddr, intmode+region, pci */
  7228. err = hpsa_find_cfgtables(h);
  7229. if (err)
  7230. goto clean3; /* vaddr, intmode+region, pci */
  7231. hpsa_find_board_params(h);
  7232. if (!hpsa_CISS_signature_present(h)) {
  7233. err = -ENODEV;
  7234. goto clean4; /* cfgtables, vaddr, intmode+region, pci */
  7235. }
  7236. hpsa_set_driver_support_bits(h);
  7237. hpsa_p600_dma_prefetch_quirk(h);
  7238. err = hpsa_enter_simple_mode(h);
  7239. if (err)
  7240. goto clean4; /* cfgtables, vaddr, intmode+region, pci */
  7241. return 0;
  7242. clean4: /* cfgtables, vaddr, intmode+region, pci */
  7243. hpsa_free_cfgtables(h);
  7244. clean3: /* vaddr, intmode+region, pci */
  7245. iounmap(h->vaddr);
  7246. h->vaddr = NULL;
  7247. clean2: /* intmode+region, pci */
  7248. hpsa_disable_interrupt_mode(h);
  7249. /*
  7250. * call pci_disable_device before pci_release_regions per
  7251. * Documentation/PCI/pci.txt
  7252. */
  7253. pci_disable_device(h->pdev);
  7254. pci_release_regions(h->pdev);
  7255. return err;
  7256. }
  7257. static void hpsa_hba_inquiry(struct ctlr_info *h)
  7258. {
  7259. int rc;
  7260. #define HBA_INQUIRY_BYTE_COUNT 64
  7261. h->hba_inquiry_data = kmalloc(HBA_INQUIRY_BYTE_COUNT, GFP_KERNEL);
  7262. if (!h->hba_inquiry_data)
  7263. return;
  7264. rc = hpsa_scsi_do_inquiry(h, RAID_CTLR_LUNID, 0,
  7265. h->hba_inquiry_data, HBA_INQUIRY_BYTE_COUNT);
  7266. if (rc != 0) {
  7267. kfree(h->hba_inquiry_data);
  7268. h->hba_inquiry_data = NULL;
  7269. }
  7270. }
  7271. static int hpsa_init_reset_devices(struct pci_dev *pdev, u32 board_id)
  7272. {
  7273. int rc, i;
  7274. void __iomem *vaddr;
  7275. if (!reset_devices)
  7276. return 0;
  7277. /* kdump kernel is loading, we don't know in which state is
  7278. * the pci interface. The dev->enable_cnt is equal zero
  7279. * so we call enable+disable, wait a while and switch it on.
  7280. */
  7281. rc = pci_enable_device(pdev);
  7282. if (rc) {
  7283. dev_warn(&pdev->dev, "Failed to enable PCI device\n");
  7284. return -ENODEV;
  7285. }
  7286. pci_disable_device(pdev);
  7287. msleep(260); /* a randomly chosen number */
  7288. rc = pci_enable_device(pdev);
  7289. if (rc) {
  7290. dev_warn(&pdev->dev, "failed to enable device.\n");
  7291. return -ENODEV;
  7292. }
  7293. pci_set_master(pdev);
  7294. vaddr = pci_ioremap_bar(pdev, 0);
  7295. if (vaddr == NULL) {
  7296. rc = -ENOMEM;
  7297. goto out_disable;
  7298. }
  7299. writel(SA5_INTR_OFF, vaddr + SA5_REPLY_INTR_MASK_OFFSET);
  7300. iounmap(vaddr);
  7301. /* Reset the controller with a PCI power-cycle or via doorbell */
  7302. rc = hpsa_kdump_hard_reset_controller(pdev, board_id);
  7303. /* -ENOTSUPP here means we cannot reset the controller
  7304. * but it's already (and still) up and running in
  7305. * "performant mode". Or, it might be 640x, which can't reset
  7306. * due to concerns about shared bbwc between 6402/6404 pair.
  7307. */
  7308. if (rc)
  7309. goto out_disable;
  7310. /* Now try to get the controller to respond to a no-op */
  7311. dev_info(&pdev->dev, "Waiting for controller to respond to no-op\n");
  7312. for (i = 0; i < HPSA_POST_RESET_NOOP_RETRIES; i++) {
  7313. if (hpsa_noop(pdev) == 0)
  7314. break;
  7315. else
  7316. dev_warn(&pdev->dev, "no-op failed%s\n",
  7317. (i < 11 ? "; re-trying" : ""));
  7318. }
  7319. out_disable:
  7320. pci_disable_device(pdev);
  7321. return rc;
  7322. }
  7323. static void hpsa_free_cmd_pool(struct ctlr_info *h)
  7324. {
  7325. kfree(h->cmd_pool_bits);
  7326. h->cmd_pool_bits = NULL;
  7327. if (h->cmd_pool) {
  7328. pci_free_consistent(h->pdev,
  7329. h->nr_cmds * sizeof(struct CommandList),
  7330. h->cmd_pool,
  7331. h->cmd_pool_dhandle);
  7332. h->cmd_pool = NULL;
  7333. h->cmd_pool_dhandle = 0;
  7334. }
  7335. if (h->errinfo_pool) {
  7336. pci_free_consistent(h->pdev,
  7337. h->nr_cmds * sizeof(struct ErrorInfo),
  7338. h->errinfo_pool,
  7339. h->errinfo_pool_dhandle);
  7340. h->errinfo_pool = NULL;
  7341. h->errinfo_pool_dhandle = 0;
  7342. }
  7343. }
  7344. static int hpsa_alloc_cmd_pool(struct ctlr_info *h)
  7345. {
  7346. h->cmd_pool_bits = kzalloc(
  7347. DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
  7348. sizeof(unsigned long), GFP_KERNEL);
  7349. h->cmd_pool = pci_alloc_consistent(h->pdev,
  7350. h->nr_cmds * sizeof(*h->cmd_pool),
  7351. &(h->cmd_pool_dhandle));
  7352. h->errinfo_pool = pci_alloc_consistent(h->pdev,
  7353. h->nr_cmds * sizeof(*h->errinfo_pool),
  7354. &(h->errinfo_pool_dhandle));
  7355. if ((h->cmd_pool_bits == NULL)
  7356. || (h->cmd_pool == NULL)
  7357. || (h->errinfo_pool == NULL)) {
  7358. dev_err(&h->pdev->dev, "out of memory in %s", __func__);
  7359. goto clean_up;
  7360. }
  7361. hpsa_preinitialize_commands(h);
  7362. return 0;
  7363. clean_up:
  7364. hpsa_free_cmd_pool(h);
  7365. return -ENOMEM;
  7366. }
  7367. static void hpsa_irq_affinity_hints(struct ctlr_info *h)
  7368. {
  7369. int i, cpu;
  7370. cpu = cpumask_first(cpu_online_mask);
  7371. for (i = 0; i < h->msix_vector; i++) {
  7372. irq_set_affinity_hint(h->intr[i], get_cpu_mask(cpu));
  7373. cpu = cpumask_next(cpu, cpu_online_mask);
  7374. }
  7375. }
  7376. /* clear affinity hints and free MSI-X, MSI, or legacy INTx vectors */
  7377. static void hpsa_free_irqs(struct ctlr_info *h)
  7378. {
  7379. int i;
  7380. if (!h->msix_vector || h->intr_mode != PERF_MODE_INT) {
  7381. /* Single reply queue, only one irq to free */
  7382. i = h->intr_mode;
  7383. irq_set_affinity_hint(h->intr[i], NULL);
  7384. free_irq(h->intr[i], &h->q[i]);
  7385. h->q[i] = 0;
  7386. return;
  7387. }
  7388. for (i = 0; i < h->msix_vector; i++) {
  7389. irq_set_affinity_hint(h->intr[i], NULL);
  7390. free_irq(h->intr[i], &h->q[i]);
  7391. h->q[i] = 0;
  7392. }
  7393. for (; i < MAX_REPLY_QUEUES; i++)
  7394. h->q[i] = 0;
  7395. }
  7396. /* returns 0 on success; cleans up and returns -Enn on error */
  7397. static int hpsa_request_irqs(struct ctlr_info *h,
  7398. irqreturn_t (*msixhandler)(int, void *),
  7399. irqreturn_t (*intxhandler)(int, void *))
  7400. {
  7401. int rc, i;
  7402. /*
  7403. * initialize h->q[x] = x so that interrupt handlers know which
  7404. * queue to process.
  7405. */
  7406. for (i = 0; i < MAX_REPLY_QUEUES; i++)
  7407. h->q[i] = (u8) i;
  7408. if (h->intr_mode == PERF_MODE_INT && h->msix_vector > 0) {
  7409. /* If performant mode and MSI-X, use multiple reply queues */
  7410. for (i = 0; i < h->msix_vector; i++) {
  7411. sprintf(h->intrname[i], "%s-msix%d", h->devname, i);
  7412. rc = request_irq(h->intr[i], msixhandler,
  7413. 0, h->intrname[i],
  7414. &h->q[i]);
  7415. if (rc) {
  7416. int j;
  7417. dev_err(&h->pdev->dev,
  7418. "failed to get irq %d for %s\n",
  7419. h->intr[i], h->devname);
  7420. for (j = 0; j < i; j++) {
  7421. free_irq(h->intr[j], &h->q[j]);
  7422. h->q[j] = 0;
  7423. }
  7424. for (; j < MAX_REPLY_QUEUES; j++)
  7425. h->q[j] = 0;
  7426. return rc;
  7427. }
  7428. }
  7429. hpsa_irq_affinity_hints(h);
  7430. } else {
  7431. /* Use single reply pool */
  7432. if (h->msix_vector > 0 || h->msi_vector) {
  7433. if (h->msix_vector)
  7434. sprintf(h->intrname[h->intr_mode],
  7435. "%s-msix", h->devname);
  7436. else
  7437. sprintf(h->intrname[h->intr_mode],
  7438. "%s-msi", h->devname);
  7439. rc = request_irq(h->intr[h->intr_mode],
  7440. msixhandler, 0,
  7441. h->intrname[h->intr_mode],
  7442. &h->q[h->intr_mode]);
  7443. } else {
  7444. sprintf(h->intrname[h->intr_mode],
  7445. "%s-intx", h->devname);
  7446. rc = request_irq(h->intr[h->intr_mode],
  7447. intxhandler, IRQF_SHARED,
  7448. h->intrname[h->intr_mode],
  7449. &h->q[h->intr_mode]);
  7450. }
  7451. irq_set_affinity_hint(h->intr[h->intr_mode], NULL);
  7452. }
  7453. if (rc) {
  7454. dev_err(&h->pdev->dev, "failed to get irq %d for %s\n",
  7455. h->intr[h->intr_mode], h->devname);
  7456. hpsa_free_irqs(h);
  7457. return -ENODEV;
  7458. }
  7459. return 0;
  7460. }
  7461. static int hpsa_kdump_soft_reset(struct ctlr_info *h)
  7462. {
  7463. int rc;
  7464. hpsa_send_host_reset(h, RAID_CTLR_LUNID, HPSA_RESET_TYPE_CONTROLLER);
  7465. dev_info(&h->pdev->dev, "Waiting for board to soft reset.\n");
  7466. rc = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_NOT_READY);
  7467. if (rc) {
  7468. dev_warn(&h->pdev->dev, "Soft reset had no effect.\n");
  7469. return rc;
  7470. }
  7471. dev_info(&h->pdev->dev, "Board reset, awaiting READY status.\n");
  7472. rc = hpsa_wait_for_board_state(h->pdev, h->vaddr, BOARD_READY);
  7473. if (rc) {
  7474. dev_warn(&h->pdev->dev, "Board failed to become ready "
  7475. "after soft reset.\n");
  7476. return rc;
  7477. }
  7478. return 0;
  7479. }
  7480. static void hpsa_free_reply_queues(struct ctlr_info *h)
  7481. {
  7482. int i;
  7483. for (i = 0; i < h->nreply_queues; i++) {
  7484. if (!h->reply_queue[i].head)
  7485. continue;
  7486. pci_free_consistent(h->pdev,
  7487. h->reply_queue_size,
  7488. h->reply_queue[i].head,
  7489. h->reply_queue[i].busaddr);
  7490. h->reply_queue[i].head = NULL;
  7491. h->reply_queue[i].busaddr = 0;
  7492. }
  7493. h->reply_queue_size = 0;
  7494. }
  7495. static void hpsa_undo_allocations_after_kdump_soft_reset(struct ctlr_info *h)
  7496. {
  7497. hpsa_free_performant_mode(h); /* init_one 7 */
  7498. hpsa_free_sg_chain_blocks(h); /* init_one 6 */
  7499. hpsa_free_cmd_pool(h); /* init_one 5 */
  7500. hpsa_free_irqs(h); /* init_one 4 */
  7501. scsi_host_put(h->scsi_host); /* init_one 3 */
  7502. h->scsi_host = NULL; /* init_one 3 */
  7503. hpsa_free_pci_init(h); /* init_one 2_5 */
  7504. free_percpu(h->lockup_detected); /* init_one 2 */
  7505. h->lockup_detected = NULL; /* init_one 2 */
  7506. if (h->resubmit_wq) {
  7507. destroy_workqueue(h->resubmit_wq); /* init_one 1 */
  7508. h->resubmit_wq = NULL;
  7509. }
  7510. if (h->rescan_ctlr_wq) {
  7511. destroy_workqueue(h->rescan_ctlr_wq);
  7512. h->rescan_ctlr_wq = NULL;
  7513. }
  7514. kfree(h); /* init_one 1 */
  7515. }
  7516. /* Called when controller lockup detected. */
  7517. static void fail_all_outstanding_cmds(struct ctlr_info *h)
  7518. {
  7519. int i, refcount;
  7520. struct CommandList *c;
  7521. int failcount = 0;
  7522. flush_workqueue(h->resubmit_wq); /* ensure all cmds are fully built */
  7523. for (i = 0; i < h->nr_cmds; i++) {
  7524. c = h->cmd_pool + i;
  7525. refcount = atomic_inc_return(&c->refcount);
  7526. if (refcount > 1) {
  7527. c->err_info->CommandStatus = CMD_CTLR_LOCKUP;
  7528. finish_cmd(c);
  7529. atomic_dec(&h->commands_outstanding);
  7530. failcount++;
  7531. }
  7532. cmd_free(h, c);
  7533. }
  7534. dev_warn(&h->pdev->dev,
  7535. "failed %d commands in fail_all\n", failcount);
  7536. }
  7537. static void set_lockup_detected_for_all_cpus(struct ctlr_info *h, u32 value)
  7538. {
  7539. int cpu;
  7540. for_each_online_cpu(cpu) {
  7541. u32 *lockup_detected;
  7542. lockup_detected = per_cpu_ptr(h->lockup_detected, cpu);
  7543. *lockup_detected = value;
  7544. }
  7545. wmb(); /* be sure the per-cpu variables are out to memory */
  7546. }
  7547. static void controller_lockup_detected(struct ctlr_info *h)
  7548. {
  7549. unsigned long flags;
  7550. u32 lockup_detected;
  7551. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  7552. spin_lock_irqsave(&h->lock, flags);
  7553. lockup_detected = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
  7554. if (!lockup_detected) {
  7555. /* no heartbeat, but controller gave us a zero. */
  7556. dev_warn(&h->pdev->dev,
  7557. "lockup detected after %d but scratchpad register is zero\n",
  7558. h->heartbeat_sample_interval / HZ);
  7559. lockup_detected = 0xffffffff;
  7560. }
  7561. set_lockup_detected_for_all_cpus(h, lockup_detected);
  7562. spin_unlock_irqrestore(&h->lock, flags);
  7563. dev_warn(&h->pdev->dev, "Controller lockup detected: 0x%08x after %d\n",
  7564. lockup_detected, h->heartbeat_sample_interval / HZ);
  7565. pci_disable_device(h->pdev);
  7566. fail_all_outstanding_cmds(h);
  7567. }
  7568. static int detect_controller_lockup(struct ctlr_info *h)
  7569. {
  7570. u64 now;
  7571. u32 heartbeat;
  7572. unsigned long flags;
  7573. now = get_jiffies_64();
  7574. /* If we've received an interrupt recently, we're ok. */
  7575. if (time_after64(h->last_intr_timestamp +
  7576. (h->heartbeat_sample_interval), now))
  7577. return false;
  7578. /*
  7579. * If we've already checked the heartbeat recently, we're ok.
  7580. * This could happen if someone sends us a signal. We
  7581. * otherwise don't care about signals in this thread.
  7582. */
  7583. if (time_after64(h->last_heartbeat_timestamp +
  7584. (h->heartbeat_sample_interval), now))
  7585. return false;
  7586. /* If heartbeat has not changed since we last looked, we're not ok. */
  7587. spin_lock_irqsave(&h->lock, flags);
  7588. heartbeat = readl(&h->cfgtable->HeartBeat);
  7589. spin_unlock_irqrestore(&h->lock, flags);
  7590. if (h->last_heartbeat == heartbeat) {
  7591. controller_lockup_detected(h);
  7592. return true;
  7593. }
  7594. /* We're ok. */
  7595. h->last_heartbeat = heartbeat;
  7596. h->last_heartbeat_timestamp = now;
  7597. return false;
  7598. }
  7599. static void hpsa_ack_ctlr_events(struct ctlr_info *h)
  7600. {
  7601. int i;
  7602. char *event_type;
  7603. if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
  7604. return;
  7605. /* Ask the controller to clear the events we're handling. */
  7606. if ((h->transMethod & (CFGTBL_Trans_io_accel1
  7607. | CFGTBL_Trans_io_accel2)) &&
  7608. (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE ||
  7609. h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)) {
  7610. if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_STATE_CHANGE)
  7611. event_type = "state change";
  7612. if (h->events & HPSA_EVENT_NOTIFY_ACCEL_IO_PATH_CONFIG_CHANGE)
  7613. event_type = "configuration change";
  7614. /* Stop sending new RAID offload reqs via the IO accelerator */
  7615. scsi_block_requests(h->scsi_host);
  7616. for (i = 0; i < h->ndevices; i++) {
  7617. h->dev[i]->offload_enabled = 0;
  7618. h->dev[i]->offload_to_be_enabled = 0;
  7619. }
  7620. hpsa_drain_accel_commands(h);
  7621. /* Set 'accelerator path config change' bit */
  7622. dev_warn(&h->pdev->dev,
  7623. "Acknowledging event: 0x%08x (HP SSD Smart Path %s)\n",
  7624. h->events, event_type);
  7625. writel(h->events, &(h->cfgtable->clear_event_notify));
  7626. /* Set the "clear event notify field update" bit 6 */
  7627. writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
  7628. /* Wait until ctlr clears 'clear event notify field', bit 6 */
  7629. hpsa_wait_for_clear_event_notify_ack(h);
  7630. scsi_unblock_requests(h->scsi_host);
  7631. } else {
  7632. /* Acknowledge controller notification events. */
  7633. writel(h->events, &(h->cfgtable->clear_event_notify));
  7634. writel(DOORBELL_CLEAR_EVENTS, h->vaddr + SA5_DOORBELL);
  7635. hpsa_wait_for_clear_event_notify_ack(h);
  7636. #if 0
  7637. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  7638. hpsa_wait_for_mode_change_ack(h);
  7639. #endif
  7640. }
  7641. return;
  7642. }
  7643. /* Check a register on the controller to see if there are configuration
  7644. * changes (added/changed/removed logical drives, etc.) which mean that
  7645. * we should rescan the controller for devices.
  7646. * Also check flag for driver-initiated rescan.
  7647. */
  7648. static int hpsa_ctlr_needs_rescan(struct ctlr_info *h)
  7649. {
  7650. if (h->drv_req_rescan) {
  7651. h->drv_req_rescan = 0;
  7652. return 1;
  7653. }
  7654. if (!(h->fw_support & MISC_FW_EVENT_NOTIFY))
  7655. return 0;
  7656. h->events = readl(&(h->cfgtable->event_notify));
  7657. return h->events & RESCAN_REQUIRED_EVENT_BITS;
  7658. }
  7659. /*
  7660. * Check if any of the offline devices have become ready
  7661. */
  7662. static int hpsa_offline_devices_ready(struct ctlr_info *h)
  7663. {
  7664. unsigned long flags;
  7665. struct offline_device_entry *d;
  7666. struct list_head *this, *tmp;
  7667. spin_lock_irqsave(&h->offline_device_lock, flags);
  7668. list_for_each_safe(this, tmp, &h->offline_device_list) {
  7669. d = list_entry(this, struct offline_device_entry,
  7670. offline_list);
  7671. spin_unlock_irqrestore(&h->offline_device_lock, flags);
  7672. if (!hpsa_volume_offline(h, d->scsi3addr)) {
  7673. spin_lock_irqsave(&h->offline_device_lock, flags);
  7674. list_del(&d->offline_list);
  7675. spin_unlock_irqrestore(&h->offline_device_lock, flags);
  7676. return 1;
  7677. }
  7678. spin_lock_irqsave(&h->offline_device_lock, flags);
  7679. }
  7680. spin_unlock_irqrestore(&h->offline_device_lock, flags);
  7681. return 0;
  7682. }
  7683. static int hpsa_luns_changed(struct ctlr_info *h)
  7684. {
  7685. int rc = 1; /* assume there are changes */
  7686. struct ReportLUNdata *logdev = NULL;
  7687. /* if we can't find out if lun data has changed,
  7688. * assume that it has.
  7689. */
  7690. if (!h->lastlogicals)
  7691. goto out;
  7692. logdev = kzalloc(sizeof(*logdev), GFP_KERNEL);
  7693. if (!logdev) {
  7694. dev_warn(&h->pdev->dev,
  7695. "Out of memory, can't track lun changes.\n");
  7696. goto out;
  7697. }
  7698. if (hpsa_scsi_do_report_luns(h, 1, logdev, sizeof(*logdev), 0)) {
  7699. dev_warn(&h->pdev->dev,
  7700. "report luns failed, can't track lun changes.\n");
  7701. goto out;
  7702. }
  7703. if (memcmp(logdev, h->lastlogicals, sizeof(*logdev))) {
  7704. dev_info(&h->pdev->dev,
  7705. "Lun changes detected.\n");
  7706. memcpy(h->lastlogicals, logdev, sizeof(*logdev));
  7707. goto out;
  7708. } else
  7709. rc = 0; /* no changes detected. */
  7710. out:
  7711. kfree(logdev);
  7712. return rc;
  7713. }
  7714. static void hpsa_rescan_ctlr_worker(struct work_struct *work)
  7715. {
  7716. unsigned long flags;
  7717. struct ctlr_info *h = container_of(to_delayed_work(work),
  7718. struct ctlr_info, rescan_ctlr_work);
  7719. if (h->remove_in_progress)
  7720. return;
  7721. if (hpsa_ctlr_needs_rescan(h) || hpsa_offline_devices_ready(h)) {
  7722. scsi_host_get(h->scsi_host);
  7723. hpsa_ack_ctlr_events(h);
  7724. hpsa_scan_start(h->scsi_host);
  7725. scsi_host_put(h->scsi_host);
  7726. } else if (h->discovery_polling) {
  7727. hpsa_disable_rld_caching(h);
  7728. if (hpsa_luns_changed(h)) {
  7729. struct Scsi_Host *sh = NULL;
  7730. dev_info(&h->pdev->dev,
  7731. "driver discovery polling rescan.\n");
  7732. sh = scsi_host_get(h->scsi_host);
  7733. if (sh != NULL) {
  7734. hpsa_scan_start(sh);
  7735. scsi_host_put(sh);
  7736. }
  7737. }
  7738. }
  7739. spin_lock_irqsave(&h->lock, flags);
  7740. if (!h->remove_in_progress)
  7741. queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
  7742. h->heartbeat_sample_interval);
  7743. spin_unlock_irqrestore(&h->lock, flags);
  7744. }
  7745. static void hpsa_monitor_ctlr_worker(struct work_struct *work)
  7746. {
  7747. unsigned long flags;
  7748. struct ctlr_info *h = container_of(to_delayed_work(work),
  7749. struct ctlr_info, monitor_ctlr_work);
  7750. detect_controller_lockup(h);
  7751. if (lockup_detected(h))
  7752. return;
  7753. spin_lock_irqsave(&h->lock, flags);
  7754. if (!h->remove_in_progress)
  7755. schedule_delayed_work(&h->monitor_ctlr_work,
  7756. h->heartbeat_sample_interval);
  7757. spin_unlock_irqrestore(&h->lock, flags);
  7758. }
  7759. static struct workqueue_struct *hpsa_create_controller_wq(struct ctlr_info *h,
  7760. char *name)
  7761. {
  7762. struct workqueue_struct *wq = NULL;
  7763. wq = alloc_ordered_workqueue("%s_%d_hpsa", 0, name, h->ctlr);
  7764. if (!wq)
  7765. dev_err(&h->pdev->dev, "failed to create %s workqueue\n", name);
  7766. return wq;
  7767. }
  7768. static int hpsa_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  7769. {
  7770. int dac, rc;
  7771. struct ctlr_info *h;
  7772. int try_soft_reset = 0;
  7773. unsigned long flags;
  7774. u32 board_id;
  7775. if (number_of_controllers == 0)
  7776. printk(KERN_INFO DRIVER_NAME "\n");
  7777. rc = hpsa_lookup_board_id(pdev, &board_id);
  7778. if (rc < 0) {
  7779. dev_warn(&pdev->dev, "Board ID not found\n");
  7780. return rc;
  7781. }
  7782. rc = hpsa_init_reset_devices(pdev, board_id);
  7783. if (rc) {
  7784. if (rc != -ENOTSUPP)
  7785. return rc;
  7786. /* If the reset fails in a particular way (it has no way to do
  7787. * a proper hard reset, so returns -ENOTSUPP) we can try to do
  7788. * a soft reset once we get the controller configured up to the
  7789. * point that it can accept a command.
  7790. */
  7791. try_soft_reset = 1;
  7792. rc = 0;
  7793. }
  7794. reinit_after_soft_reset:
  7795. /* Command structures must be aligned on a 32-byte boundary because
  7796. * the 5 lower bits of the address are used by the hardware. and by
  7797. * the driver. See comments in hpsa.h for more info.
  7798. */
  7799. BUILD_BUG_ON(sizeof(struct CommandList) % COMMANDLIST_ALIGNMENT);
  7800. h = kzalloc(sizeof(*h), GFP_KERNEL);
  7801. if (!h) {
  7802. dev_err(&pdev->dev, "Failed to allocate controller head\n");
  7803. return -ENOMEM;
  7804. }
  7805. h->pdev = pdev;
  7806. h->intr_mode = hpsa_simple_mode ? SIMPLE_MODE_INT : PERF_MODE_INT;
  7807. INIT_LIST_HEAD(&h->offline_device_list);
  7808. spin_lock_init(&h->lock);
  7809. spin_lock_init(&h->offline_device_lock);
  7810. spin_lock_init(&h->scan_lock);
  7811. atomic_set(&h->passthru_cmds_avail, HPSA_MAX_CONCURRENT_PASSTHRUS);
  7812. atomic_set(&h->abort_cmds_available, HPSA_CMDS_RESERVED_FOR_ABORTS);
  7813. /* Allocate and clear per-cpu variable lockup_detected */
  7814. h->lockup_detected = alloc_percpu(u32);
  7815. if (!h->lockup_detected) {
  7816. dev_err(&h->pdev->dev, "Failed to allocate lockup detector\n");
  7817. rc = -ENOMEM;
  7818. goto clean1; /* aer/h */
  7819. }
  7820. set_lockup_detected_for_all_cpus(h, 0);
  7821. rc = hpsa_pci_init(h);
  7822. if (rc)
  7823. goto clean2; /* lu, aer/h */
  7824. /* relies on h-> settings made by hpsa_pci_init, including
  7825. * interrupt_mode h->intr */
  7826. rc = hpsa_scsi_host_alloc(h);
  7827. if (rc)
  7828. goto clean2_5; /* pci, lu, aer/h */
  7829. sprintf(h->devname, HPSA "%d", h->scsi_host->host_no);
  7830. h->ctlr = number_of_controllers;
  7831. number_of_controllers++;
  7832. /* configure PCI DMA stuff */
  7833. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  7834. if (rc == 0) {
  7835. dac = 1;
  7836. } else {
  7837. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  7838. if (rc == 0) {
  7839. dac = 0;
  7840. } else {
  7841. dev_err(&pdev->dev, "no suitable DMA available\n");
  7842. goto clean3; /* shost, pci, lu, aer/h */
  7843. }
  7844. }
  7845. /* make sure the board interrupts are off */
  7846. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  7847. rc = hpsa_request_irqs(h, do_hpsa_intr_msi, do_hpsa_intr_intx);
  7848. if (rc)
  7849. goto clean3; /* shost, pci, lu, aer/h */
  7850. rc = hpsa_alloc_cmd_pool(h);
  7851. if (rc)
  7852. goto clean4; /* irq, shost, pci, lu, aer/h */
  7853. rc = hpsa_alloc_sg_chain_blocks(h);
  7854. if (rc)
  7855. goto clean5; /* cmd, irq, shost, pci, lu, aer/h */
  7856. init_waitqueue_head(&h->scan_wait_queue);
  7857. init_waitqueue_head(&h->abort_cmd_wait_queue);
  7858. init_waitqueue_head(&h->event_sync_wait_queue);
  7859. mutex_init(&h->reset_mutex);
  7860. h->scan_finished = 1; /* no scan currently in progress */
  7861. pci_set_drvdata(pdev, h);
  7862. h->ndevices = 0;
  7863. spin_lock_init(&h->devlock);
  7864. rc = hpsa_put_ctlr_into_performant_mode(h);
  7865. if (rc)
  7866. goto clean6; /* sg, cmd, irq, shost, pci, lu, aer/h */
  7867. /* create the resubmit workqueue */
  7868. h->rescan_ctlr_wq = hpsa_create_controller_wq(h, "rescan");
  7869. if (!h->rescan_ctlr_wq) {
  7870. rc = -ENOMEM;
  7871. goto clean7;
  7872. }
  7873. h->resubmit_wq = hpsa_create_controller_wq(h, "resubmit");
  7874. if (!h->resubmit_wq) {
  7875. rc = -ENOMEM;
  7876. goto clean7; /* aer/h */
  7877. }
  7878. /*
  7879. * At this point, the controller is ready to take commands.
  7880. * Now, if reset_devices and the hard reset didn't work, try
  7881. * the soft reset and see if that works.
  7882. */
  7883. if (try_soft_reset) {
  7884. /* This is kind of gross. We may or may not get a completion
  7885. * from the soft reset command, and if we do, then the value
  7886. * from the fifo may or may not be valid. So, we wait 10 secs
  7887. * after the reset throwing away any completions we get during
  7888. * that time. Unregister the interrupt handler and register
  7889. * fake ones to scoop up any residual completions.
  7890. */
  7891. spin_lock_irqsave(&h->lock, flags);
  7892. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  7893. spin_unlock_irqrestore(&h->lock, flags);
  7894. hpsa_free_irqs(h);
  7895. rc = hpsa_request_irqs(h, hpsa_msix_discard_completions,
  7896. hpsa_intx_discard_completions);
  7897. if (rc) {
  7898. dev_warn(&h->pdev->dev,
  7899. "Failed to request_irq after soft reset.\n");
  7900. /*
  7901. * cannot goto clean7 or free_irqs will be called
  7902. * again. Instead, do its work
  7903. */
  7904. hpsa_free_performant_mode(h); /* clean7 */
  7905. hpsa_free_sg_chain_blocks(h); /* clean6 */
  7906. hpsa_free_cmd_pool(h); /* clean5 */
  7907. /*
  7908. * skip hpsa_free_irqs(h) clean4 since that
  7909. * was just called before request_irqs failed
  7910. */
  7911. goto clean3;
  7912. }
  7913. rc = hpsa_kdump_soft_reset(h);
  7914. if (rc)
  7915. /* Neither hard nor soft reset worked, we're hosed. */
  7916. goto clean7;
  7917. dev_info(&h->pdev->dev, "Board READY.\n");
  7918. dev_info(&h->pdev->dev,
  7919. "Waiting for stale completions to drain.\n");
  7920. h->access.set_intr_mask(h, HPSA_INTR_ON);
  7921. msleep(10000);
  7922. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  7923. rc = controller_reset_failed(h->cfgtable);
  7924. if (rc)
  7925. dev_info(&h->pdev->dev,
  7926. "Soft reset appears to have failed.\n");
  7927. /* since the controller's reset, we have to go back and re-init
  7928. * everything. Easiest to just forget what we've done and do it
  7929. * all over again.
  7930. */
  7931. hpsa_undo_allocations_after_kdump_soft_reset(h);
  7932. try_soft_reset = 0;
  7933. if (rc)
  7934. /* don't goto clean, we already unallocated */
  7935. return -ENODEV;
  7936. goto reinit_after_soft_reset;
  7937. }
  7938. /* Enable Accelerated IO path at driver layer */
  7939. h->acciopath_status = 1;
  7940. /* Disable discovery polling.*/
  7941. h->discovery_polling = 0;
  7942. /* Turn the interrupts on so we can service requests */
  7943. h->access.set_intr_mask(h, HPSA_INTR_ON);
  7944. hpsa_hba_inquiry(h);
  7945. h->lastlogicals = kzalloc(sizeof(*(h->lastlogicals)), GFP_KERNEL);
  7946. if (!h->lastlogicals)
  7947. dev_info(&h->pdev->dev,
  7948. "Can't track change to report lun data\n");
  7949. /* hook into SCSI subsystem */
  7950. rc = hpsa_scsi_add_host(h);
  7951. if (rc)
  7952. goto clean7; /* perf, sg, cmd, irq, shost, pci, lu, aer/h */
  7953. /* Monitor the controller for firmware lockups */
  7954. h->heartbeat_sample_interval = HEARTBEAT_SAMPLE_INTERVAL;
  7955. INIT_DELAYED_WORK(&h->monitor_ctlr_work, hpsa_monitor_ctlr_worker);
  7956. schedule_delayed_work(&h->monitor_ctlr_work,
  7957. h->heartbeat_sample_interval);
  7958. INIT_DELAYED_WORK(&h->rescan_ctlr_work, hpsa_rescan_ctlr_worker);
  7959. queue_delayed_work(h->rescan_ctlr_wq, &h->rescan_ctlr_work,
  7960. h->heartbeat_sample_interval);
  7961. return 0;
  7962. clean7: /* perf, sg, cmd, irq, shost, pci, lu, aer/h */
  7963. hpsa_free_performant_mode(h);
  7964. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  7965. clean6: /* sg, cmd, irq, pci, lockup, wq/aer/h */
  7966. hpsa_free_sg_chain_blocks(h);
  7967. clean5: /* cmd, irq, shost, pci, lu, aer/h */
  7968. hpsa_free_cmd_pool(h);
  7969. clean4: /* irq, shost, pci, lu, aer/h */
  7970. hpsa_free_irqs(h);
  7971. clean3: /* shost, pci, lu, aer/h */
  7972. scsi_host_put(h->scsi_host);
  7973. h->scsi_host = NULL;
  7974. clean2_5: /* pci, lu, aer/h */
  7975. hpsa_free_pci_init(h);
  7976. clean2: /* lu, aer/h */
  7977. if (h->lockup_detected) {
  7978. free_percpu(h->lockup_detected);
  7979. h->lockup_detected = NULL;
  7980. }
  7981. clean1: /* wq/aer/h */
  7982. if (h->resubmit_wq) {
  7983. destroy_workqueue(h->resubmit_wq);
  7984. h->resubmit_wq = NULL;
  7985. }
  7986. if (h->rescan_ctlr_wq) {
  7987. destroy_workqueue(h->rescan_ctlr_wq);
  7988. h->rescan_ctlr_wq = NULL;
  7989. }
  7990. kfree(h);
  7991. return rc;
  7992. }
  7993. static void hpsa_flush_cache(struct ctlr_info *h)
  7994. {
  7995. char *flush_buf;
  7996. struct CommandList *c;
  7997. int rc;
  7998. if (unlikely(lockup_detected(h)))
  7999. return;
  8000. flush_buf = kzalloc(4, GFP_KERNEL);
  8001. if (!flush_buf)
  8002. return;
  8003. c = cmd_alloc(h);
  8004. if (fill_cmd(c, HPSA_CACHE_FLUSH, h, flush_buf, 4, 0,
  8005. RAID_CTLR_LUNID, TYPE_CMD)) {
  8006. goto out;
  8007. }
  8008. rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
  8009. PCI_DMA_TODEVICE, DEFAULT_TIMEOUT);
  8010. if (rc)
  8011. goto out;
  8012. if (c->err_info->CommandStatus != 0)
  8013. out:
  8014. dev_warn(&h->pdev->dev,
  8015. "error flushing cache on controller\n");
  8016. cmd_free(h, c);
  8017. kfree(flush_buf);
  8018. }
  8019. /* Make controller gather fresh report lun data each time we
  8020. * send down a report luns request
  8021. */
  8022. static void hpsa_disable_rld_caching(struct ctlr_info *h)
  8023. {
  8024. u32 *options;
  8025. struct CommandList *c;
  8026. int rc;
  8027. /* Don't bother trying to set diag options if locked up */
  8028. if (unlikely(h->lockup_detected))
  8029. return;
  8030. options = kzalloc(sizeof(*options), GFP_KERNEL);
  8031. if (!options) {
  8032. dev_err(&h->pdev->dev,
  8033. "Error: failed to disable rld caching, during alloc.\n");
  8034. return;
  8035. }
  8036. c = cmd_alloc(h);
  8037. /* first, get the current diag options settings */
  8038. if (fill_cmd(c, BMIC_SENSE_DIAG_OPTIONS, h, options, 4, 0,
  8039. RAID_CTLR_LUNID, TYPE_CMD))
  8040. goto errout;
  8041. rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
  8042. PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
  8043. if ((rc != 0) || (c->err_info->CommandStatus != 0))
  8044. goto errout;
  8045. /* Now, set the bit for disabling the RLD caching */
  8046. *options |= HPSA_DIAG_OPTS_DISABLE_RLD_CACHING;
  8047. if (fill_cmd(c, BMIC_SET_DIAG_OPTIONS, h, options, 4, 0,
  8048. RAID_CTLR_LUNID, TYPE_CMD))
  8049. goto errout;
  8050. rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
  8051. PCI_DMA_TODEVICE, DEFAULT_TIMEOUT);
  8052. if ((rc != 0) || (c->err_info->CommandStatus != 0))
  8053. goto errout;
  8054. /* Now verify that it got set: */
  8055. if (fill_cmd(c, BMIC_SENSE_DIAG_OPTIONS, h, options, 4, 0,
  8056. RAID_CTLR_LUNID, TYPE_CMD))
  8057. goto errout;
  8058. rc = hpsa_scsi_do_simple_cmd_with_retry(h, c,
  8059. PCI_DMA_FROMDEVICE, DEFAULT_TIMEOUT);
  8060. if ((rc != 0) || (c->err_info->CommandStatus != 0))
  8061. goto errout;
  8062. if (*options & HPSA_DIAG_OPTS_DISABLE_RLD_CACHING)
  8063. goto out;
  8064. errout:
  8065. dev_err(&h->pdev->dev,
  8066. "Error: failed to disable report lun data caching.\n");
  8067. out:
  8068. cmd_free(h, c);
  8069. kfree(options);
  8070. }
  8071. static void hpsa_shutdown(struct pci_dev *pdev)
  8072. {
  8073. struct ctlr_info *h;
  8074. h = pci_get_drvdata(pdev);
  8075. /* Turn board interrupts off and send the flush cache command
  8076. * sendcmd will turn off interrupt, and send the flush...
  8077. * To write all data in the battery backed cache to disks
  8078. */
  8079. hpsa_flush_cache(h);
  8080. h->access.set_intr_mask(h, HPSA_INTR_OFF);
  8081. hpsa_free_irqs(h); /* init_one 4 */
  8082. hpsa_disable_interrupt_mode(h); /* pci_init 2 */
  8083. }
  8084. static void hpsa_free_device_info(struct ctlr_info *h)
  8085. {
  8086. int i;
  8087. for (i = 0; i < h->ndevices; i++) {
  8088. kfree(h->dev[i]);
  8089. h->dev[i] = NULL;
  8090. }
  8091. }
  8092. static void hpsa_remove_one(struct pci_dev *pdev)
  8093. {
  8094. struct ctlr_info *h;
  8095. unsigned long flags;
  8096. if (pci_get_drvdata(pdev) == NULL) {
  8097. dev_err(&pdev->dev, "unable to remove device\n");
  8098. return;
  8099. }
  8100. h = pci_get_drvdata(pdev);
  8101. /* Get rid of any controller monitoring work items */
  8102. spin_lock_irqsave(&h->lock, flags);
  8103. h->remove_in_progress = 1;
  8104. spin_unlock_irqrestore(&h->lock, flags);
  8105. cancel_delayed_work_sync(&h->monitor_ctlr_work);
  8106. cancel_delayed_work_sync(&h->rescan_ctlr_work);
  8107. destroy_workqueue(h->rescan_ctlr_wq);
  8108. destroy_workqueue(h->resubmit_wq);
  8109. /*
  8110. * Call before disabling interrupts.
  8111. * scsi_remove_host can trigger I/O operations especially
  8112. * when multipath is enabled. There can be SYNCHRONIZE CACHE
  8113. * operations which cannot complete and will hang the system.
  8114. */
  8115. if (h->scsi_host)
  8116. scsi_remove_host(h->scsi_host); /* init_one 8 */
  8117. /* includes hpsa_free_irqs - init_one 4 */
  8118. /* includes hpsa_disable_interrupt_mode - pci_init 2 */
  8119. hpsa_shutdown(pdev);
  8120. hpsa_free_device_info(h); /* scan */
  8121. kfree(h->hba_inquiry_data); /* init_one 10 */
  8122. h->hba_inquiry_data = NULL; /* init_one 10 */
  8123. hpsa_free_ioaccel2_sg_chain_blocks(h);
  8124. hpsa_free_performant_mode(h); /* init_one 7 */
  8125. hpsa_free_sg_chain_blocks(h); /* init_one 6 */
  8126. hpsa_free_cmd_pool(h); /* init_one 5 */
  8127. kfree(h->lastlogicals);
  8128. /* hpsa_free_irqs already called via hpsa_shutdown init_one 4 */
  8129. scsi_host_put(h->scsi_host); /* init_one 3 */
  8130. h->scsi_host = NULL; /* init_one 3 */
  8131. /* includes hpsa_disable_interrupt_mode - pci_init 2 */
  8132. hpsa_free_pci_init(h); /* init_one 2.5 */
  8133. free_percpu(h->lockup_detected); /* init_one 2 */
  8134. h->lockup_detected = NULL; /* init_one 2 */
  8135. /* (void) pci_disable_pcie_error_reporting(pdev); */ /* init_one 1 */
  8136. hpsa_delete_sas_host(h);
  8137. kfree(h); /* init_one 1 */
  8138. }
  8139. static int hpsa_suspend(__attribute__((unused)) struct pci_dev *pdev,
  8140. __attribute__((unused)) pm_message_t state)
  8141. {
  8142. return -ENOSYS;
  8143. }
  8144. static int hpsa_resume(__attribute__((unused)) struct pci_dev *pdev)
  8145. {
  8146. return -ENOSYS;
  8147. }
  8148. static struct pci_driver hpsa_pci_driver = {
  8149. .name = HPSA,
  8150. .probe = hpsa_init_one,
  8151. .remove = hpsa_remove_one,
  8152. .id_table = hpsa_pci_device_id, /* id_table */
  8153. .shutdown = hpsa_shutdown,
  8154. .suspend = hpsa_suspend,
  8155. .resume = hpsa_resume,
  8156. };
  8157. /* Fill in bucket_map[], given nsgs (the max number of
  8158. * scatter gather elements supported) and bucket[],
  8159. * which is an array of 8 integers. The bucket[] array
  8160. * contains 8 different DMA transfer sizes (in 16
  8161. * byte increments) which the controller uses to fetch
  8162. * commands. This function fills in bucket_map[], which
  8163. * maps a given number of scatter gather elements to one of
  8164. * the 8 DMA transfer sizes. The point of it is to allow the
  8165. * controller to only do as much DMA as needed to fetch the
  8166. * command, with the DMA transfer size encoded in the lower
  8167. * bits of the command address.
  8168. */
  8169. static void calc_bucket_map(int bucket[], int num_buckets,
  8170. int nsgs, int min_blocks, u32 *bucket_map)
  8171. {
  8172. int i, j, b, size;
  8173. /* Note, bucket_map must have nsgs+1 entries. */
  8174. for (i = 0; i <= nsgs; i++) {
  8175. /* Compute size of a command with i SG entries */
  8176. size = i + min_blocks;
  8177. b = num_buckets; /* Assume the biggest bucket */
  8178. /* Find the bucket that is just big enough */
  8179. for (j = 0; j < num_buckets; j++) {
  8180. if (bucket[j] >= size) {
  8181. b = j;
  8182. break;
  8183. }
  8184. }
  8185. /* for a command with i SG entries, use bucket b. */
  8186. bucket_map[i] = b;
  8187. }
  8188. }
  8189. /*
  8190. * return -ENODEV on err, 0 on success (or no action)
  8191. * allocates numerous items that must be freed later
  8192. */
  8193. static int hpsa_enter_performant_mode(struct ctlr_info *h, u32 trans_support)
  8194. {
  8195. int i;
  8196. unsigned long register_value;
  8197. unsigned long transMethod = CFGTBL_Trans_Performant |
  8198. (trans_support & CFGTBL_Trans_use_short_tags) |
  8199. CFGTBL_Trans_enable_directed_msix |
  8200. (trans_support & (CFGTBL_Trans_io_accel1 |
  8201. CFGTBL_Trans_io_accel2));
  8202. struct access_method access = SA5_performant_access;
  8203. /* This is a bit complicated. There are 8 registers on
  8204. * the controller which we write to to tell it 8 different
  8205. * sizes of commands which there may be. It's a way of
  8206. * reducing the DMA done to fetch each command. Encoded into
  8207. * each command's tag are 3 bits which communicate to the controller
  8208. * which of the eight sizes that command fits within. The size of
  8209. * each command depends on how many scatter gather entries there are.
  8210. * Each SG entry requires 16 bytes. The eight registers are programmed
  8211. * with the number of 16-byte blocks a command of that size requires.
  8212. * The smallest command possible requires 5 such 16 byte blocks.
  8213. * the largest command possible requires SG_ENTRIES_IN_CMD + 4 16-byte
  8214. * blocks. Note, this only extends to the SG entries contained
  8215. * within the command block, and does not extend to chained blocks
  8216. * of SG elements. bft[] contains the eight values we write to
  8217. * the registers. They are not evenly distributed, but have more
  8218. * sizes for small commands, and fewer sizes for larger commands.
  8219. */
  8220. int bft[8] = {5, 6, 8, 10, 12, 20, 28, SG_ENTRIES_IN_CMD + 4};
  8221. #define MIN_IOACCEL2_BFT_ENTRY 5
  8222. #define HPSA_IOACCEL2_HEADER_SZ 4
  8223. int bft2[16] = {MIN_IOACCEL2_BFT_ENTRY, 6, 7, 8, 9, 10, 11, 12,
  8224. 13, 14, 15, 16, 17, 18, 19,
  8225. HPSA_IOACCEL2_HEADER_SZ + IOACCEL2_MAXSGENTRIES};
  8226. BUILD_BUG_ON(ARRAY_SIZE(bft2) != 16);
  8227. BUILD_BUG_ON(ARRAY_SIZE(bft) != 8);
  8228. BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) >
  8229. 16 * MIN_IOACCEL2_BFT_ENTRY);
  8230. BUILD_BUG_ON(sizeof(struct ioaccel2_sg_element) != 16);
  8231. BUILD_BUG_ON(28 > SG_ENTRIES_IN_CMD + 4);
  8232. /* 5 = 1 s/g entry or 4k
  8233. * 6 = 2 s/g entry or 8k
  8234. * 8 = 4 s/g entry or 16k
  8235. * 10 = 6 s/g entry or 24k
  8236. */
  8237. /* If the controller supports either ioaccel method then
  8238. * we can also use the RAID stack submit path that does not
  8239. * perform the superfluous readl() after each command submission.
  8240. */
  8241. if (trans_support & (CFGTBL_Trans_io_accel1 | CFGTBL_Trans_io_accel2))
  8242. access = SA5_performant_access_no_read;
  8243. /* Controller spec: zero out this buffer. */
  8244. for (i = 0; i < h->nreply_queues; i++)
  8245. memset(h->reply_queue[i].head, 0, h->reply_queue_size);
  8246. bft[7] = SG_ENTRIES_IN_CMD + 4;
  8247. calc_bucket_map(bft, ARRAY_SIZE(bft),
  8248. SG_ENTRIES_IN_CMD, 4, h->blockFetchTable);
  8249. for (i = 0; i < 8; i++)
  8250. writel(bft[i], &h->transtable->BlockFetch[i]);
  8251. /* size of controller ring buffer */
  8252. writel(h->max_commands, &h->transtable->RepQSize);
  8253. writel(h->nreply_queues, &h->transtable->RepQCount);
  8254. writel(0, &h->transtable->RepQCtrAddrLow32);
  8255. writel(0, &h->transtable->RepQCtrAddrHigh32);
  8256. for (i = 0; i < h->nreply_queues; i++) {
  8257. writel(0, &h->transtable->RepQAddr[i].upper);
  8258. writel(h->reply_queue[i].busaddr,
  8259. &h->transtable->RepQAddr[i].lower);
  8260. }
  8261. writel(0, &h->cfgtable->HostWrite.command_pool_addr_hi);
  8262. writel(transMethod, &(h->cfgtable->HostWrite.TransportRequest));
  8263. /*
  8264. * enable outbound interrupt coalescing in accelerator mode;
  8265. */
  8266. if (trans_support & CFGTBL_Trans_io_accel1) {
  8267. access = SA5_ioaccel_mode1_access;
  8268. writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
  8269. writel(4, &h->cfgtable->HostWrite.CoalIntCount);
  8270. } else {
  8271. if (trans_support & CFGTBL_Trans_io_accel2) {
  8272. access = SA5_ioaccel_mode2_access;
  8273. writel(10, &h->cfgtable->HostWrite.CoalIntDelay);
  8274. writel(4, &h->cfgtable->HostWrite.CoalIntCount);
  8275. }
  8276. }
  8277. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  8278. if (hpsa_wait_for_mode_change_ack(h)) {
  8279. dev_err(&h->pdev->dev,
  8280. "performant mode problem - doorbell timeout\n");
  8281. return -ENODEV;
  8282. }
  8283. register_value = readl(&(h->cfgtable->TransportActive));
  8284. if (!(register_value & CFGTBL_Trans_Performant)) {
  8285. dev_err(&h->pdev->dev,
  8286. "performant mode problem - transport not active\n");
  8287. return -ENODEV;
  8288. }
  8289. /* Change the access methods to the performant access methods */
  8290. h->access = access;
  8291. h->transMethod = transMethod;
  8292. if (!((trans_support & CFGTBL_Trans_io_accel1) ||
  8293. (trans_support & CFGTBL_Trans_io_accel2)))
  8294. return 0;
  8295. if (trans_support & CFGTBL_Trans_io_accel1) {
  8296. /* Set up I/O accelerator mode */
  8297. for (i = 0; i < h->nreply_queues; i++) {
  8298. writel(i, h->vaddr + IOACCEL_MODE1_REPLY_QUEUE_INDEX);
  8299. h->reply_queue[i].current_entry =
  8300. readl(h->vaddr + IOACCEL_MODE1_PRODUCER_INDEX);
  8301. }
  8302. bft[7] = h->ioaccel_maxsg + 8;
  8303. calc_bucket_map(bft, ARRAY_SIZE(bft), h->ioaccel_maxsg, 8,
  8304. h->ioaccel1_blockFetchTable);
  8305. /* initialize all reply queue entries to unused */
  8306. for (i = 0; i < h->nreply_queues; i++)
  8307. memset(h->reply_queue[i].head,
  8308. (u8) IOACCEL_MODE1_REPLY_UNUSED,
  8309. h->reply_queue_size);
  8310. /* set all the constant fields in the accelerator command
  8311. * frames once at init time to save CPU cycles later.
  8312. */
  8313. for (i = 0; i < h->nr_cmds; i++) {
  8314. struct io_accel1_cmd *cp = &h->ioaccel_cmd_pool[i];
  8315. cp->function = IOACCEL1_FUNCTION_SCSIIO;
  8316. cp->err_info = (u32) (h->errinfo_pool_dhandle +
  8317. (i * sizeof(struct ErrorInfo)));
  8318. cp->err_info_len = sizeof(struct ErrorInfo);
  8319. cp->sgl_offset = IOACCEL1_SGLOFFSET;
  8320. cp->host_context_flags =
  8321. cpu_to_le16(IOACCEL1_HCFLAGS_CISS_FORMAT);
  8322. cp->timeout_sec = 0;
  8323. cp->ReplyQueue = 0;
  8324. cp->tag =
  8325. cpu_to_le64((i << DIRECT_LOOKUP_SHIFT));
  8326. cp->host_addr =
  8327. cpu_to_le64(h->ioaccel_cmd_pool_dhandle +
  8328. (i * sizeof(struct io_accel1_cmd)));
  8329. }
  8330. } else if (trans_support & CFGTBL_Trans_io_accel2) {
  8331. u64 cfg_offset, cfg_base_addr_index;
  8332. u32 bft2_offset, cfg_base_addr;
  8333. int rc;
  8334. rc = hpsa_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
  8335. &cfg_base_addr_index, &cfg_offset);
  8336. BUILD_BUG_ON(offsetof(struct io_accel2_cmd, sg) != 64);
  8337. bft2[15] = h->ioaccel_maxsg + HPSA_IOACCEL2_HEADER_SZ;
  8338. calc_bucket_map(bft2, ARRAY_SIZE(bft2), h->ioaccel_maxsg,
  8339. 4, h->ioaccel2_blockFetchTable);
  8340. bft2_offset = readl(&h->cfgtable->io_accel_request_size_offset);
  8341. BUILD_BUG_ON(offsetof(struct CfgTable,
  8342. io_accel_request_size_offset) != 0xb8);
  8343. h->ioaccel2_bft2_regs =
  8344. remap_pci_mem(pci_resource_start(h->pdev,
  8345. cfg_base_addr_index) +
  8346. cfg_offset + bft2_offset,
  8347. ARRAY_SIZE(bft2) *
  8348. sizeof(*h->ioaccel2_bft2_regs));
  8349. for (i = 0; i < ARRAY_SIZE(bft2); i++)
  8350. writel(bft2[i], &h->ioaccel2_bft2_regs[i]);
  8351. }
  8352. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  8353. if (hpsa_wait_for_mode_change_ack(h)) {
  8354. dev_err(&h->pdev->dev,
  8355. "performant mode problem - enabling ioaccel mode\n");
  8356. return -ENODEV;
  8357. }
  8358. return 0;
  8359. }
  8360. /* Free ioaccel1 mode command blocks and block fetch table */
  8361. static void hpsa_free_ioaccel1_cmd_and_bft(struct ctlr_info *h)
  8362. {
  8363. if (h->ioaccel_cmd_pool) {
  8364. pci_free_consistent(h->pdev,
  8365. h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
  8366. h->ioaccel_cmd_pool,
  8367. h->ioaccel_cmd_pool_dhandle);
  8368. h->ioaccel_cmd_pool = NULL;
  8369. h->ioaccel_cmd_pool_dhandle = 0;
  8370. }
  8371. kfree(h->ioaccel1_blockFetchTable);
  8372. h->ioaccel1_blockFetchTable = NULL;
  8373. }
  8374. /* Allocate ioaccel1 mode command blocks and block fetch table */
  8375. static int hpsa_alloc_ioaccel1_cmd_and_bft(struct ctlr_info *h)
  8376. {
  8377. h->ioaccel_maxsg =
  8378. readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
  8379. if (h->ioaccel_maxsg > IOACCEL1_MAXSGENTRIES)
  8380. h->ioaccel_maxsg = IOACCEL1_MAXSGENTRIES;
  8381. /* Command structures must be aligned on a 128-byte boundary
  8382. * because the 7 lower bits of the address are used by the
  8383. * hardware.
  8384. */
  8385. BUILD_BUG_ON(sizeof(struct io_accel1_cmd) %
  8386. IOACCEL1_COMMANDLIST_ALIGNMENT);
  8387. h->ioaccel_cmd_pool =
  8388. pci_alloc_consistent(h->pdev,
  8389. h->nr_cmds * sizeof(*h->ioaccel_cmd_pool),
  8390. &(h->ioaccel_cmd_pool_dhandle));
  8391. h->ioaccel1_blockFetchTable =
  8392. kmalloc(((h->ioaccel_maxsg + 1) *
  8393. sizeof(u32)), GFP_KERNEL);
  8394. if ((h->ioaccel_cmd_pool == NULL) ||
  8395. (h->ioaccel1_blockFetchTable == NULL))
  8396. goto clean_up;
  8397. memset(h->ioaccel_cmd_pool, 0,
  8398. h->nr_cmds * sizeof(*h->ioaccel_cmd_pool));
  8399. return 0;
  8400. clean_up:
  8401. hpsa_free_ioaccel1_cmd_and_bft(h);
  8402. return -ENOMEM;
  8403. }
  8404. /* Free ioaccel2 mode command blocks and block fetch table */
  8405. static void hpsa_free_ioaccel2_cmd_and_bft(struct ctlr_info *h)
  8406. {
  8407. hpsa_free_ioaccel2_sg_chain_blocks(h);
  8408. if (h->ioaccel2_cmd_pool) {
  8409. pci_free_consistent(h->pdev,
  8410. h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
  8411. h->ioaccel2_cmd_pool,
  8412. h->ioaccel2_cmd_pool_dhandle);
  8413. h->ioaccel2_cmd_pool = NULL;
  8414. h->ioaccel2_cmd_pool_dhandle = 0;
  8415. }
  8416. kfree(h->ioaccel2_blockFetchTable);
  8417. h->ioaccel2_blockFetchTable = NULL;
  8418. }
  8419. /* Allocate ioaccel2 mode command blocks and block fetch table */
  8420. static int hpsa_alloc_ioaccel2_cmd_and_bft(struct ctlr_info *h)
  8421. {
  8422. int rc;
  8423. /* Allocate ioaccel2 mode command blocks and block fetch table */
  8424. h->ioaccel_maxsg =
  8425. readl(&(h->cfgtable->io_accel_max_embedded_sg_count));
  8426. if (h->ioaccel_maxsg > IOACCEL2_MAXSGENTRIES)
  8427. h->ioaccel_maxsg = IOACCEL2_MAXSGENTRIES;
  8428. BUILD_BUG_ON(sizeof(struct io_accel2_cmd) %
  8429. IOACCEL2_COMMANDLIST_ALIGNMENT);
  8430. h->ioaccel2_cmd_pool =
  8431. pci_alloc_consistent(h->pdev,
  8432. h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool),
  8433. &(h->ioaccel2_cmd_pool_dhandle));
  8434. h->ioaccel2_blockFetchTable =
  8435. kmalloc(((h->ioaccel_maxsg + 1) *
  8436. sizeof(u32)), GFP_KERNEL);
  8437. if ((h->ioaccel2_cmd_pool == NULL) ||
  8438. (h->ioaccel2_blockFetchTable == NULL)) {
  8439. rc = -ENOMEM;
  8440. goto clean_up;
  8441. }
  8442. rc = hpsa_allocate_ioaccel2_sg_chain_blocks(h);
  8443. if (rc)
  8444. goto clean_up;
  8445. memset(h->ioaccel2_cmd_pool, 0,
  8446. h->nr_cmds * sizeof(*h->ioaccel2_cmd_pool));
  8447. return 0;
  8448. clean_up:
  8449. hpsa_free_ioaccel2_cmd_and_bft(h);
  8450. return rc;
  8451. }
  8452. /* Free items allocated by hpsa_put_ctlr_into_performant_mode */
  8453. static void hpsa_free_performant_mode(struct ctlr_info *h)
  8454. {
  8455. kfree(h->blockFetchTable);
  8456. h->blockFetchTable = NULL;
  8457. hpsa_free_reply_queues(h);
  8458. hpsa_free_ioaccel1_cmd_and_bft(h);
  8459. hpsa_free_ioaccel2_cmd_and_bft(h);
  8460. }
  8461. /* return -ENODEV on error, 0 on success (or no action)
  8462. * allocates numerous items that must be freed later
  8463. */
  8464. static int hpsa_put_ctlr_into_performant_mode(struct ctlr_info *h)
  8465. {
  8466. u32 trans_support;
  8467. unsigned long transMethod = CFGTBL_Trans_Performant |
  8468. CFGTBL_Trans_use_short_tags;
  8469. int i, rc;
  8470. if (hpsa_simple_mode)
  8471. return 0;
  8472. trans_support = readl(&(h->cfgtable->TransportSupport));
  8473. if (!(trans_support & PERFORMANT_MODE))
  8474. return 0;
  8475. /* Check for I/O accelerator mode support */
  8476. if (trans_support & CFGTBL_Trans_io_accel1) {
  8477. transMethod |= CFGTBL_Trans_io_accel1 |
  8478. CFGTBL_Trans_enable_directed_msix;
  8479. rc = hpsa_alloc_ioaccel1_cmd_and_bft(h);
  8480. if (rc)
  8481. return rc;
  8482. } else if (trans_support & CFGTBL_Trans_io_accel2) {
  8483. transMethod |= CFGTBL_Trans_io_accel2 |
  8484. CFGTBL_Trans_enable_directed_msix;
  8485. rc = hpsa_alloc_ioaccel2_cmd_and_bft(h);
  8486. if (rc)
  8487. return rc;
  8488. }
  8489. h->nreply_queues = h->msix_vector > 0 ? h->msix_vector : 1;
  8490. hpsa_get_max_perf_mode_cmds(h);
  8491. /* Performant mode ring buffer and supporting data structures */
  8492. h->reply_queue_size = h->max_commands * sizeof(u64);
  8493. for (i = 0; i < h->nreply_queues; i++) {
  8494. h->reply_queue[i].head = pci_alloc_consistent(h->pdev,
  8495. h->reply_queue_size,
  8496. &(h->reply_queue[i].busaddr));
  8497. if (!h->reply_queue[i].head) {
  8498. rc = -ENOMEM;
  8499. goto clean1; /* rq, ioaccel */
  8500. }
  8501. h->reply_queue[i].size = h->max_commands;
  8502. h->reply_queue[i].wraparound = 1; /* spec: init to 1 */
  8503. h->reply_queue[i].current_entry = 0;
  8504. }
  8505. /* Need a block fetch table for performant mode */
  8506. h->blockFetchTable = kmalloc(((SG_ENTRIES_IN_CMD + 1) *
  8507. sizeof(u32)), GFP_KERNEL);
  8508. if (!h->blockFetchTable) {
  8509. rc = -ENOMEM;
  8510. goto clean1; /* rq, ioaccel */
  8511. }
  8512. rc = hpsa_enter_performant_mode(h, trans_support);
  8513. if (rc)
  8514. goto clean2; /* bft, rq, ioaccel */
  8515. return 0;
  8516. clean2: /* bft, rq, ioaccel */
  8517. kfree(h->blockFetchTable);
  8518. h->blockFetchTable = NULL;
  8519. clean1: /* rq, ioaccel */
  8520. hpsa_free_reply_queues(h);
  8521. hpsa_free_ioaccel1_cmd_and_bft(h);
  8522. hpsa_free_ioaccel2_cmd_and_bft(h);
  8523. return rc;
  8524. }
  8525. static int is_accelerated_cmd(struct CommandList *c)
  8526. {
  8527. return c->cmd_type == CMD_IOACCEL1 || c->cmd_type == CMD_IOACCEL2;
  8528. }
  8529. static void hpsa_drain_accel_commands(struct ctlr_info *h)
  8530. {
  8531. struct CommandList *c = NULL;
  8532. int i, accel_cmds_out;
  8533. int refcount;
  8534. do { /* wait for all outstanding ioaccel commands to drain out */
  8535. accel_cmds_out = 0;
  8536. for (i = 0; i < h->nr_cmds; i++) {
  8537. c = h->cmd_pool + i;
  8538. refcount = atomic_inc_return(&c->refcount);
  8539. if (refcount > 1) /* Command is allocated */
  8540. accel_cmds_out += is_accelerated_cmd(c);
  8541. cmd_free(h, c);
  8542. }
  8543. if (accel_cmds_out <= 0)
  8544. break;
  8545. msleep(100);
  8546. } while (1);
  8547. }
  8548. static struct hpsa_sas_phy *hpsa_alloc_sas_phy(
  8549. struct hpsa_sas_port *hpsa_sas_port)
  8550. {
  8551. struct hpsa_sas_phy *hpsa_sas_phy;
  8552. struct sas_phy *phy;
  8553. hpsa_sas_phy = kzalloc(sizeof(*hpsa_sas_phy), GFP_KERNEL);
  8554. if (!hpsa_sas_phy)
  8555. return NULL;
  8556. phy = sas_phy_alloc(hpsa_sas_port->parent_node->parent_dev,
  8557. hpsa_sas_port->next_phy_index);
  8558. if (!phy) {
  8559. kfree(hpsa_sas_phy);
  8560. return NULL;
  8561. }
  8562. hpsa_sas_port->next_phy_index++;
  8563. hpsa_sas_phy->phy = phy;
  8564. hpsa_sas_phy->parent_port = hpsa_sas_port;
  8565. return hpsa_sas_phy;
  8566. }
  8567. static void hpsa_free_sas_phy(struct hpsa_sas_phy *hpsa_sas_phy)
  8568. {
  8569. struct sas_phy *phy = hpsa_sas_phy->phy;
  8570. sas_port_delete_phy(hpsa_sas_phy->parent_port->port, phy);
  8571. sas_phy_free(phy);
  8572. if (hpsa_sas_phy->added_to_port)
  8573. list_del(&hpsa_sas_phy->phy_list_entry);
  8574. kfree(hpsa_sas_phy);
  8575. }
  8576. static int hpsa_sas_port_add_phy(struct hpsa_sas_phy *hpsa_sas_phy)
  8577. {
  8578. int rc;
  8579. struct hpsa_sas_port *hpsa_sas_port;
  8580. struct sas_phy *phy;
  8581. struct sas_identify *identify;
  8582. hpsa_sas_port = hpsa_sas_phy->parent_port;
  8583. phy = hpsa_sas_phy->phy;
  8584. identify = &phy->identify;
  8585. memset(identify, 0, sizeof(*identify));
  8586. identify->sas_address = hpsa_sas_port->sas_address;
  8587. identify->device_type = SAS_END_DEVICE;
  8588. identify->initiator_port_protocols = SAS_PROTOCOL_STP;
  8589. identify->target_port_protocols = SAS_PROTOCOL_STP;
  8590. phy->minimum_linkrate_hw = SAS_LINK_RATE_UNKNOWN;
  8591. phy->maximum_linkrate_hw = SAS_LINK_RATE_UNKNOWN;
  8592. phy->minimum_linkrate = SAS_LINK_RATE_UNKNOWN;
  8593. phy->maximum_linkrate = SAS_LINK_RATE_UNKNOWN;
  8594. phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
  8595. rc = sas_phy_add(hpsa_sas_phy->phy);
  8596. if (rc)
  8597. return rc;
  8598. sas_port_add_phy(hpsa_sas_port->port, hpsa_sas_phy->phy);
  8599. list_add_tail(&hpsa_sas_phy->phy_list_entry,
  8600. &hpsa_sas_port->phy_list_head);
  8601. hpsa_sas_phy->added_to_port = true;
  8602. return 0;
  8603. }
  8604. static int
  8605. hpsa_sas_port_add_rphy(struct hpsa_sas_port *hpsa_sas_port,
  8606. struct sas_rphy *rphy)
  8607. {
  8608. struct sas_identify *identify;
  8609. identify = &rphy->identify;
  8610. identify->sas_address = hpsa_sas_port->sas_address;
  8611. identify->initiator_port_protocols = SAS_PROTOCOL_STP;
  8612. identify->target_port_protocols = SAS_PROTOCOL_STP;
  8613. return sas_rphy_add(rphy);
  8614. }
  8615. static struct hpsa_sas_port
  8616. *hpsa_alloc_sas_port(struct hpsa_sas_node *hpsa_sas_node,
  8617. u64 sas_address)
  8618. {
  8619. int rc;
  8620. struct hpsa_sas_port *hpsa_sas_port;
  8621. struct sas_port *port;
  8622. hpsa_sas_port = kzalloc(sizeof(*hpsa_sas_port), GFP_KERNEL);
  8623. if (!hpsa_sas_port)
  8624. return NULL;
  8625. INIT_LIST_HEAD(&hpsa_sas_port->phy_list_head);
  8626. hpsa_sas_port->parent_node = hpsa_sas_node;
  8627. port = sas_port_alloc_num(hpsa_sas_node->parent_dev);
  8628. if (!port)
  8629. goto free_hpsa_port;
  8630. rc = sas_port_add(port);
  8631. if (rc)
  8632. goto free_sas_port;
  8633. hpsa_sas_port->port = port;
  8634. hpsa_sas_port->sas_address = sas_address;
  8635. list_add_tail(&hpsa_sas_port->port_list_entry,
  8636. &hpsa_sas_node->port_list_head);
  8637. return hpsa_sas_port;
  8638. free_sas_port:
  8639. sas_port_free(port);
  8640. free_hpsa_port:
  8641. kfree(hpsa_sas_port);
  8642. return NULL;
  8643. }
  8644. static void hpsa_free_sas_port(struct hpsa_sas_port *hpsa_sas_port)
  8645. {
  8646. struct hpsa_sas_phy *hpsa_sas_phy;
  8647. struct hpsa_sas_phy *next;
  8648. list_for_each_entry_safe(hpsa_sas_phy, next,
  8649. &hpsa_sas_port->phy_list_head, phy_list_entry)
  8650. hpsa_free_sas_phy(hpsa_sas_phy);
  8651. sas_port_delete(hpsa_sas_port->port);
  8652. list_del(&hpsa_sas_port->port_list_entry);
  8653. kfree(hpsa_sas_port);
  8654. }
  8655. static struct hpsa_sas_node *hpsa_alloc_sas_node(struct device *parent_dev)
  8656. {
  8657. struct hpsa_sas_node *hpsa_sas_node;
  8658. hpsa_sas_node = kzalloc(sizeof(*hpsa_sas_node), GFP_KERNEL);
  8659. if (hpsa_sas_node) {
  8660. hpsa_sas_node->parent_dev = parent_dev;
  8661. INIT_LIST_HEAD(&hpsa_sas_node->port_list_head);
  8662. }
  8663. return hpsa_sas_node;
  8664. }
  8665. static void hpsa_free_sas_node(struct hpsa_sas_node *hpsa_sas_node)
  8666. {
  8667. struct hpsa_sas_port *hpsa_sas_port;
  8668. struct hpsa_sas_port *next;
  8669. if (!hpsa_sas_node)
  8670. return;
  8671. list_for_each_entry_safe(hpsa_sas_port, next,
  8672. &hpsa_sas_node->port_list_head, port_list_entry)
  8673. hpsa_free_sas_port(hpsa_sas_port);
  8674. kfree(hpsa_sas_node);
  8675. }
  8676. static struct hpsa_scsi_dev_t
  8677. *hpsa_find_device_by_sas_rphy(struct ctlr_info *h,
  8678. struct sas_rphy *rphy)
  8679. {
  8680. int i;
  8681. struct hpsa_scsi_dev_t *device;
  8682. for (i = 0; i < h->ndevices; i++) {
  8683. device = h->dev[i];
  8684. if (!device->sas_port)
  8685. continue;
  8686. if (device->sas_port->rphy == rphy)
  8687. return device;
  8688. }
  8689. return NULL;
  8690. }
  8691. static int hpsa_add_sas_host(struct ctlr_info *h)
  8692. {
  8693. int rc;
  8694. struct device *parent_dev;
  8695. struct hpsa_sas_node *hpsa_sas_node;
  8696. struct hpsa_sas_port *hpsa_sas_port;
  8697. struct hpsa_sas_phy *hpsa_sas_phy;
  8698. parent_dev = &h->scsi_host->shost_gendev;
  8699. hpsa_sas_node = hpsa_alloc_sas_node(parent_dev);
  8700. if (!hpsa_sas_node)
  8701. return -ENOMEM;
  8702. hpsa_sas_port = hpsa_alloc_sas_port(hpsa_sas_node, h->sas_address);
  8703. if (!hpsa_sas_port) {
  8704. rc = -ENODEV;
  8705. goto free_sas_node;
  8706. }
  8707. hpsa_sas_phy = hpsa_alloc_sas_phy(hpsa_sas_port);
  8708. if (!hpsa_sas_phy) {
  8709. rc = -ENODEV;
  8710. goto free_sas_port;
  8711. }
  8712. rc = hpsa_sas_port_add_phy(hpsa_sas_phy);
  8713. if (rc)
  8714. goto free_sas_phy;
  8715. h->sas_host = hpsa_sas_node;
  8716. return 0;
  8717. free_sas_phy:
  8718. hpsa_free_sas_phy(hpsa_sas_phy);
  8719. free_sas_port:
  8720. hpsa_free_sas_port(hpsa_sas_port);
  8721. free_sas_node:
  8722. hpsa_free_sas_node(hpsa_sas_node);
  8723. return rc;
  8724. }
  8725. static void hpsa_delete_sas_host(struct ctlr_info *h)
  8726. {
  8727. hpsa_free_sas_node(h->sas_host);
  8728. }
  8729. static int hpsa_add_sas_device(struct hpsa_sas_node *hpsa_sas_node,
  8730. struct hpsa_scsi_dev_t *device)
  8731. {
  8732. int rc;
  8733. struct hpsa_sas_port *hpsa_sas_port;
  8734. struct sas_rphy *rphy;
  8735. hpsa_sas_port = hpsa_alloc_sas_port(hpsa_sas_node, device->sas_address);
  8736. if (!hpsa_sas_port)
  8737. return -ENOMEM;
  8738. rphy = sas_end_device_alloc(hpsa_sas_port->port);
  8739. if (!rphy) {
  8740. rc = -ENODEV;
  8741. goto free_sas_port;
  8742. }
  8743. hpsa_sas_port->rphy = rphy;
  8744. device->sas_port = hpsa_sas_port;
  8745. rc = hpsa_sas_port_add_rphy(hpsa_sas_port, rphy);
  8746. if (rc)
  8747. goto free_sas_port;
  8748. return 0;
  8749. free_sas_port:
  8750. hpsa_free_sas_port(hpsa_sas_port);
  8751. device->sas_port = NULL;
  8752. return rc;
  8753. }
  8754. static void hpsa_remove_sas_device(struct hpsa_scsi_dev_t *device)
  8755. {
  8756. if (device->sas_port) {
  8757. hpsa_free_sas_port(device->sas_port);
  8758. device->sas_port = NULL;
  8759. }
  8760. }
  8761. static int
  8762. hpsa_sas_get_linkerrors(struct sas_phy *phy)
  8763. {
  8764. return 0;
  8765. }
  8766. static int
  8767. hpsa_sas_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
  8768. {
  8769. *identifier = 0;
  8770. return 0;
  8771. }
  8772. static int
  8773. hpsa_sas_get_bay_identifier(struct sas_rphy *rphy)
  8774. {
  8775. return -ENXIO;
  8776. }
  8777. static int
  8778. hpsa_sas_phy_reset(struct sas_phy *phy, int hard_reset)
  8779. {
  8780. return 0;
  8781. }
  8782. static int
  8783. hpsa_sas_phy_enable(struct sas_phy *phy, int enable)
  8784. {
  8785. return 0;
  8786. }
  8787. static int
  8788. hpsa_sas_phy_setup(struct sas_phy *phy)
  8789. {
  8790. return 0;
  8791. }
  8792. static void
  8793. hpsa_sas_phy_release(struct sas_phy *phy)
  8794. {
  8795. }
  8796. static int
  8797. hpsa_sas_phy_speed(struct sas_phy *phy, struct sas_phy_linkrates *rates)
  8798. {
  8799. return -EINVAL;
  8800. }
  8801. /* SMP = Serial Management Protocol */
  8802. static int
  8803. hpsa_sas_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
  8804. struct request *req)
  8805. {
  8806. return -EINVAL;
  8807. }
  8808. static struct sas_function_template hpsa_sas_transport_functions = {
  8809. .get_linkerrors = hpsa_sas_get_linkerrors,
  8810. .get_enclosure_identifier = hpsa_sas_get_enclosure_identifier,
  8811. .get_bay_identifier = hpsa_sas_get_bay_identifier,
  8812. .phy_reset = hpsa_sas_phy_reset,
  8813. .phy_enable = hpsa_sas_phy_enable,
  8814. .phy_setup = hpsa_sas_phy_setup,
  8815. .phy_release = hpsa_sas_phy_release,
  8816. .set_phy_speed = hpsa_sas_phy_speed,
  8817. .smp_handler = hpsa_sas_smp_handler,
  8818. };
  8819. /*
  8820. * This is it. Register the PCI driver information for the cards we control
  8821. * the OS will call our registered routines when it finds one of our cards.
  8822. */
  8823. static int __init hpsa_init(void)
  8824. {
  8825. int rc;
  8826. hpsa_sas_transport_template =
  8827. sas_attach_transport(&hpsa_sas_transport_functions);
  8828. if (!hpsa_sas_transport_template)
  8829. return -ENODEV;
  8830. rc = pci_register_driver(&hpsa_pci_driver);
  8831. if (rc)
  8832. sas_release_transport(hpsa_sas_transport_template);
  8833. return rc;
  8834. }
  8835. static void __exit hpsa_cleanup(void)
  8836. {
  8837. pci_unregister_driver(&hpsa_pci_driver);
  8838. sas_release_transport(hpsa_sas_transport_template);
  8839. }
  8840. static void __attribute__((unused)) verify_offsets(void)
  8841. {
  8842. #define VERIFY_OFFSET(member, offset) \
  8843. BUILD_BUG_ON(offsetof(struct raid_map_data, member) != offset)
  8844. VERIFY_OFFSET(structure_size, 0);
  8845. VERIFY_OFFSET(volume_blk_size, 4);
  8846. VERIFY_OFFSET(volume_blk_cnt, 8);
  8847. VERIFY_OFFSET(phys_blk_shift, 16);
  8848. VERIFY_OFFSET(parity_rotation_shift, 17);
  8849. VERIFY_OFFSET(strip_size, 18);
  8850. VERIFY_OFFSET(disk_starting_blk, 20);
  8851. VERIFY_OFFSET(disk_blk_cnt, 28);
  8852. VERIFY_OFFSET(data_disks_per_row, 36);
  8853. VERIFY_OFFSET(metadata_disks_per_row, 38);
  8854. VERIFY_OFFSET(row_cnt, 40);
  8855. VERIFY_OFFSET(layout_map_count, 42);
  8856. VERIFY_OFFSET(flags, 44);
  8857. VERIFY_OFFSET(dekindex, 46);
  8858. /* VERIFY_OFFSET(reserved, 48 */
  8859. VERIFY_OFFSET(data, 64);
  8860. #undef VERIFY_OFFSET
  8861. #define VERIFY_OFFSET(member, offset) \
  8862. BUILD_BUG_ON(offsetof(struct io_accel2_cmd, member) != offset)
  8863. VERIFY_OFFSET(IU_type, 0);
  8864. VERIFY_OFFSET(direction, 1);
  8865. VERIFY_OFFSET(reply_queue, 2);
  8866. /* VERIFY_OFFSET(reserved1, 3); */
  8867. VERIFY_OFFSET(scsi_nexus, 4);
  8868. VERIFY_OFFSET(Tag, 8);
  8869. VERIFY_OFFSET(cdb, 16);
  8870. VERIFY_OFFSET(cciss_lun, 32);
  8871. VERIFY_OFFSET(data_len, 40);
  8872. VERIFY_OFFSET(cmd_priority_task_attr, 44);
  8873. VERIFY_OFFSET(sg_count, 45);
  8874. /* VERIFY_OFFSET(reserved3 */
  8875. VERIFY_OFFSET(err_ptr, 48);
  8876. VERIFY_OFFSET(err_len, 56);
  8877. /* VERIFY_OFFSET(reserved4 */
  8878. VERIFY_OFFSET(sg, 64);
  8879. #undef VERIFY_OFFSET
  8880. #define VERIFY_OFFSET(member, offset) \
  8881. BUILD_BUG_ON(offsetof(struct io_accel1_cmd, member) != offset)
  8882. VERIFY_OFFSET(dev_handle, 0x00);
  8883. VERIFY_OFFSET(reserved1, 0x02);
  8884. VERIFY_OFFSET(function, 0x03);
  8885. VERIFY_OFFSET(reserved2, 0x04);
  8886. VERIFY_OFFSET(err_info, 0x0C);
  8887. VERIFY_OFFSET(reserved3, 0x10);
  8888. VERIFY_OFFSET(err_info_len, 0x12);
  8889. VERIFY_OFFSET(reserved4, 0x13);
  8890. VERIFY_OFFSET(sgl_offset, 0x14);
  8891. VERIFY_OFFSET(reserved5, 0x15);
  8892. VERIFY_OFFSET(transfer_len, 0x1C);
  8893. VERIFY_OFFSET(reserved6, 0x20);
  8894. VERIFY_OFFSET(io_flags, 0x24);
  8895. VERIFY_OFFSET(reserved7, 0x26);
  8896. VERIFY_OFFSET(LUN, 0x34);
  8897. VERIFY_OFFSET(control, 0x3C);
  8898. VERIFY_OFFSET(CDB, 0x40);
  8899. VERIFY_OFFSET(reserved8, 0x50);
  8900. VERIFY_OFFSET(host_context_flags, 0x60);
  8901. VERIFY_OFFSET(timeout_sec, 0x62);
  8902. VERIFY_OFFSET(ReplyQueue, 0x64);
  8903. VERIFY_OFFSET(reserved9, 0x65);
  8904. VERIFY_OFFSET(tag, 0x68);
  8905. VERIFY_OFFSET(host_addr, 0x70);
  8906. VERIFY_OFFSET(CISS_LUN, 0x78);
  8907. VERIFY_OFFSET(SG, 0x78 + 8);
  8908. #undef VERIFY_OFFSET
  8909. }
  8910. module_init(hpsa_init);
  8911. module_exit(hpsa_cleanup);