hpsa.c 271 KB

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