cciss.c 139 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980
  1. /*
  2. * Disk Array driver for HP Smart Array controllers.
  3. * (C) Copyright 2000, 2007 Hewlett-Packard Development Company, L.P.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; version 2 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  17. * 02111-1307, USA.
  18. *
  19. * Questions/Comments/Bugfixes to iss_storagedev@hp.com
  20. *
  21. */
  22. #include <linux/module.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/types.h>
  25. #include <linux/pci.h>
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/smp_lock.h>
  29. #include <linux/delay.h>
  30. #include <linux/major.h>
  31. #include <linux/fs.h>
  32. #include <linux/bio.h>
  33. #include <linux/blkpg.h>
  34. #include <linux/timer.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/seq_file.h>
  37. #include <linux/init.h>
  38. #include <linux/jiffies.h>
  39. #include <linux/hdreg.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/compat.h>
  42. #include <linux/mutex.h>
  43. #include <asm/uaccess.h>
  44. #include <asm/io.h>
  45. #include <linux/dma-mapping.h>
  46. #include <linux/blkdev.h>
  47. #include <linux/genhd.h>
  48. #include <linux/completion.h>
  49. #include <scsi/scsi.h>
  50. #include <scsi/sg.h>
  51. #include <scsi/scsi_ioctl.h>
  52. #include <linux/cdrom.h>
  53. #include <linux/scatterlist.h>
  54. #include <linux/kthread.h>
  55. #define CCISS_DRIVER_VERSION(maj,min,submin) ((maj<<16)|(min<<8)|(submin))
  56. #define DRIVER_NAME "HP CISS Driver (v 3.6.26)"
  57. #define DRIVER_VERSION CCISS_DRIVER_VERSION(3, 6, 26)
  58. /* Embedded module documentation macros - see modules.h */
  59. MODULE_AUTHOR("Hewlett-Packard Company");
  60. MODULE_DESCRIPTION("Driver for HP Smart Array Controllers");
  61. MODULE_SUPPORTED_DEVICE("HP Smart Array Controllers");
  62. MODULE_VERSION("3.6.26");
  63. MODULE_LICENSE("GPL");
  64. static int cciss_allow_hpsa;
  65. module_param(cciss_allow_hpsa, int, S_IRUGO|S_IWUSR);
  66. MODULE_PARM_DESC(cciss_allow_hpsa,
  67. "Prevent cciss driver from accessing hardware known to be "
  68. " supported by the hpsa driver");
  69. #include "cciss_cmd.h"
  70. #include "cciss.h"
  71. #include <linux/cciss_ioctl.h>
  72. /* define the PCI info for the cards we can control */
  73. static const struct pci_device_id cciss_pci_device_id[] = {
  74. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISS, 0x0E11, 0x4070},
  75. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4080},
  76. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4082},
  77. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSB, 0x0E11, 0x4083},
  78. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x4091},
  79. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409A},
  80. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409B},
  81. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409C},
  82. {PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_CISSC, 0x0E11, 0x409D},
  83. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSA, 0x103C, 0x3225},
  84. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3223},
  85. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3234},
  86. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3235},
  87. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3211},
  88. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3212},
  89. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3213},
  90. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3214},
  91. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSD, 0x103C, 0x3215},
  92. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x3237},
  93. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSC, 0x103C, 0x323D},
  94. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3241},
  95. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3243},
  96. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3245},
  97. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3247},
  98. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3249},
  99. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324A},
  100. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x324B},
  101. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3250},
  102. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3251},
  103. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3252},
  104. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3253},
  105. {PCI_VENDOR_ID_HP, PCI_DEVICE_ID_HP_CISSE, 0x103C, 0x3254},
  106. {0,}
  107. };
  108. MODULE_DEVICE_TABLE(pci, cciss_pci_device_id);
  109. /* board_id = Subsystem Device ID & Vendor ID
  110. * product = Marketing Name for the board
  111. * access = Address of the struct of function pointers
  112. */
  113. static struct board_type products[] = {
  114. {0x40700E11, "Smart Array 5300", &SA5_access},
  115. {0x40800E11, "Smart Array 5i", &SA5B_access},
  116. {0x40820E11, "Smart Array 532", &SA5B_access},
  117. {0x40830E11, "Smart Array 5312", &SA5B_access},
  118. {0x409A0E11, "Smart Array 641", &SA5_access},
  119. {0x409B0E11, "Smart Array 642", &SA5_access},
  120. {0x409C0E11, "Smart Array 6400", &SA5_access},
  121. {0x409D0E11, "Smart Array 6400 EM", &SA5_access},
  122. {0x40910E11, "Smart Array 6i", &SA5_access},
  123. {0x3225103C, "Smart Array P600", &SA5_access},
  124. {0x3235103C, "Smart Array P400i", &SA5_access},
  125. {0x3211103C, "Smart Array E200i", &SA5_access},
  126. {0x3212103C, "Smart Array E200", &SA5_access},
  127. {0x3213103C, "Smart Array E200i", &SA5_access},
  128. {0x3214103C, "Smart Array E200i", &SA5_access},
  129. {0x3215103C, "Smart Array E200i", &SA5_access},
  130. {0x3237103C, "Smart Array E500", &SA5_access},
  131. /* controllers below this line are also supported by the hpsa driver. */
  132. #define HPSA_BOUNDARY 0x3223103C
  133. {0x3223103C, "Smart Array P800", &SA5_access},
  134. {0x3234103C, "Smart Array P400", &SA5_access},
  135. {0x323D103C, "Smart Array P700m", &SA5_access},
  136. {0x3241103C, "Smart Array P212", &SA5_access},
  137. {0x3243103C, "Smart Array P410", &SA5_access},
  138. {0x3245103C, "Smart Array P410i", &SA5_access},
  139. {0x3247103C, "Smart Array P411", &SA5_access},
  140. {0x3249103C, "Smart Array P812", &SA5_access},
  141. {0x324A103C, "Smart Array P712m", &SA5_access},
  142. {0x324B103C, "Smart Array P711m", &SA5_access},
  143. {0x3250103C, "Smart Array", &SA5_access},
  144. {0x3251103C, "Smart Array", &SA5_access},
  145. {0x3252103C, "Smart Array", &SA5_access},
  146. {0x3253103C, "Smart Array", &SA5_access},
  147. {0x3254103C, "Smart Array", &SA5_access},
  148. };
  149. /* How long to wait (in milliseconds) for board to go into simple mode */
  150. #define MAX_CONFIG_WAIT 30000
  151. #define MAX_IOCTL_CONFIG_WAIT 1000
  152. /*define how many times we will try a command because of bus resets */
  153. #define MAX_CMD_RETRIES 3
  154. #define MAX_CTLR 32
  155. /* Originally cciss driver only supports 8 major numbers */
  156. #define MAX_CTLR_ORIG 8
  157. static ctlr_info_t *hba[MAX_CTLR];
  158. static struct task_struct *cciss_scan_thread;
  159. static DEFINE_MUTEX(scan_mutex);
  160. static LIST_HEAD(scan_q);
  161. static void do_cciss_request(struct request_queue *q);
  162. static irqreturn_t do_cciss_intx(int irq, void *dev_id);
  163. static irqreturn_t do_cciss_msix_intr(int irq, void *dev_id);
  164. static int cciss_open(struct block_device *bdev, fmode_t mode);
  165. static int cciss_unlocked_open(struct block_device *bdev, fmode_t mode);
  166. static int cciss_release(struct gendisk *disk, fmode_t mode);
  167. static int do_ioctl(struct block_device *bdev, fmode_t mode,
  168. unsigned int cmd, unsigned long arg);
  169. static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
  170. unsigned int cmd, unsigned long arg);
  171. static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo);
  172. static int cciss_revalidate(struct gendisk *disk);
  173. static int rebuild_lun_table(ctlr_info_t *h, int first_time, int via_ioctl);
  174. static int deregister_disk(ctlr_info_t *h, int drv_index,
  175. int clear_all, int via_ioctl);
  176. static void cciss_read_capacity(ctlr_info_t *h, int logvol,
  177. sector_t *total_size, unsigned int *block_size);
  178. static void cciss_read_capacity_16(ctlr_info_t *h, int logvol,
  179. sector_t *total_size, unsigned int *block_size);
  180. static void cciss_geometry_inquiry(ctlr_info_t *h, int logvol,
  181. sector_t total_size,
  182. unsigned int block_size, InquiryData_struct *inq_buff,
  183. drive_info_struct *drv);
  184. static void __devinit cciss_interrupt_mode(ctlr_info_t *);
  185. static void start_io(ctlr_info_t *h);
  186. static int sendcmd_withirq(ctlr_info_t *h, __u8 cmd, void *buff, size_t size,
  187. __u8 page_code, unsigned char scsi3addr[],
  188. int cmd_type);
  189. static int sendcmd_withirq_core(ctlr_info_t *h, CommandList_struct *c,
  190. int attempt_retry);
  191. static int process_sendcmd_error(ctlr_info_t *h, CommandList_struct *c);
  192. static int add_to_scan_list(struct ctlr_info *h);
  193. static int scan_thread(void *data);
  194. static int check_for_unit_attention(ctlr_info_t *h, CommandList_struct *c);
  195. static void cciss_hba_release(struct device *dev);
  196. static void cciss_device_release(struct device *dev);
  197. static void cciss_free_gendisk(ctlr_info_t *h, int drv_index);
  198. static void cciss_free_drive_info(ctlr_info_t *h, int drv_index);
  199. static inline u32 next_command(ctlr_info_t *h);
  200. static int __devinit cciss_find_cfg_addrs(struct pci_dev *pdev,
  201. void __iomem *vaddr, u32 *cfg_base_addr, u64 *cfg_base_addr_index,
  202. u64 *cfg_offset);
  203. static int __devinit cciss_pci_find_memory_BAR(struct pci_dev *pdev,
  204. unsigned long *memory_bar);
  205. /* performant mode helper functions */
  206. static void calc_bucket_map(int *bucket, int num_buckets, int nsgs,
  207. int *bucket_map);
  208. static void cciss_put_controller_into_performant_mode(ctlr_info_t *h);
  209. #ifdef CONFIG_PROC_FS
  210. static void cciss_procinit(ctlr_info_t *h);
  211. #else
  212. static void cciss_procinit(ctlr_info_t *h)
  213. {
  214. }
  215. #endif /* CONFIG_PROC_FS */
  216. #ifdef CONFIG_COMPAT
  217. static int cciss_compat_ioctl(struct block_device *, fmode_t,
  218. unsigned, unsigned long);
  219. #endif
  220. static const struct block_device_operations cciss_fops = {
  221. .owner = THIS_MODULE,
  222. .open = cciss_unlocked_open,
  223. .release = cciss_release,
  224. .ioctl = do_ioctl,
  225. .getgeo = cciss_getgeo,
  226. #ifdef CONFIG_COMPAT
  227. .compat_ioctl = cciss_compat_ioctl,
  228. #endif
  229. .revalidate_disk = cciss_revalidate,
  230. };
  231. /* set_performant_mode: Modify the tag for cciss performant
  232. * set bit 0 for pull model, bits 3-1 for block fetch
  233. * register number
  234. */
  235. static void set_performant_mode(ctlr_info_t *h, CommandList_struct *c)
  236. {
  237. if (likely(h->transMethod == CFGTBL_Trans_Performant))
  238. c->busaddr |= 1 | (h->blockFetchTable[c->Header.SGList] << 1);
  239. }
  240. /*
  241. * Enqueuing and dequeuing functions for cmdlists.
  242. */
  243. static inline void addQ(struct hlist_head *list, CommandList_struct *c)
  244. {
  245. hlist_add_head(&c->list, list);
  246. }
  247. static inline void removeQ(CommandList_struct *c)
  248. {
  249. /*
  250. * After kexec/dump some commands might still
  251. * be in flight, which the firmware will try
  252. * to complete. Resetting the firmware doesn't work
  253. * with old fw revisions, so we have to mark
  254. * them off as 'stale' to prevent the driver from
  255. * falling over.
  256. */
  257. if (WARN_ON(hlist_unhashed(&c->list))) {
  258. c->cmd_type = CMD_MSG_STALE;
  259. return;
  260. }
  261. hlist_del_init(&c->list);
  262. }
  263. static void enqueue_cmd_and_start_io(ctlr_info_t *h,
  264. CommandList_struct *c)
  265. {
  266. unsigned long flags;
  267. set_performant_mode(h, c);
  268. spin_lock_irqsave(&h->lock, flags);
  269. addQ(&h->reqQ, c);
  270. h->Qdepth++;
  271. start_io(h);
  272. spin_unlock_irqrestore(&h->lock, flags);
  273. }
  274. static void cciss_free_sg_chain_blocks(SGDescriptor_struct **cmd_sg_list,
  275. int nr_cmds)
  276. {
  277. int i;
  278. if (!cmd_sg_list)
  279. return;
  280. for (i = 0; i < nr_cmds; i++) {
  281. kfree(cmd_sg_list[i]);
  282. cmd_sg_list[i] = NULL;
  283. }
  284. kfree(cmd_sg_list);
  285. }
  286. static SGDescriptor_struct **cciss_allocate_sg_chain_blocks(
  287. ctlr_info_t *h, int chainsize, int nr_cmds)
  288. {
  289. int j;
  290. SGDescriptor_struct **cmd_sg_list;
  291. if (chainsize <= 0)
  292. return NULL;
  293. cmd_sg_list = kmalloc(sizeof(*cmd_sg_list) * nr_cmds, GFP_KERNEL);
  294. if (!cmd_sg_list)
  295. return NULL;
  296. /* Build up chain blocks for each command */
  297. for (j = 0; j < nr_cmds; j++) {
  298. /* Need a block of chainsized s/g elements. */
  299. cmd_sg_list[j] = kmalloc((chainsize *
  300. sizeof(*cmd_sg_list[j])), GFP_KERNEL);
  301. if (!cmd_sg_list[j]) {
  302. dev_err(&h->pdev->dev, "Cannot get memory "
  303. "for s/g chains.\n");
  304. goto clean;
  305. }
  306. }
  307. return cmd_sg_list;
  308. clean:
  309. cciss_free_sg_chain_blocks(cmd_sg_list, nr_cmds);
  310. return NULL;
  311. }
  312. static void cciss_unmap_sg_chain_block(ctlr_info_t *h, CommandList_struct *c)
  313. {
  314. SGDescriptor_struct *chain_sg;
  315. u64bit temp64;
  316. if (c->Header.SGTotal <= h->max_cmd_sgentries)
  317. return;
  318. chain_sg = &c->SG[h->max_cmd_sgentries - 1];
  319. temp64.val32.lower = chain_sg->Addr.lower;
  320. temp64.val32.upper = chain_sg->Addr.upper;
  321. pci_unmap_single(h->pdev, temp64.val, chain_sg->Len, PCI_DMA_TODEVICE);
  322. }
  323. static void cciss_map_sg_chain_block(ctlr_info_t *h, CommandList_struct *c,
  324. SGDescriptor_struct *chain_block, int len)
  325. {
  326. SGDescriptor_struct *chain_sg;
  327. u64bit temp64;
  328. chain_sg = &c->SG[h->max_cmd_sgentries - 1];
  329. chain_sg->Ext = CCISS_SG_CHAIN;
  330. chain_sg->Len = len;
  331. temp64.val = pci_map_single(h->pdev, chain_block, len,
  332. PCI_DMA_TODEVICE);
  333. chain_sg->Addr.lower = temp64.val32.lower;
  334. chain_sg->Addr.upper = temp64.val32.upper;
  335. }
  336. #include "cciss_scsi.c" /* For SCSI tape support */
  337. static const char *raid_label[] = { "0", "4", "1(1+0)", "5", "5+1", "ADG",
  338. "UNKNOWN"
  339. };
  340. #define RAID_UNKNOWN (ARRAY_SIZE(raid_label)-1)
  341. #ifdef CONFIG_PROC_FS
  342. /*
  343. * Report information about this controller.
  344. */
  345. #define ENG_GIG 1000000000
  346. #define ENG_GIG_FACTOR (ENG_GIG/512)
  347. #define ENGAGE_SCSI "engage scsi"
  348. static struct proc_dir_entry *proc_cciss;
  349. static void cciss_seq_show_header(struct seq_file *seq)
  350. {
  351. ctlr_info_t *h = seq->private;
  352. seq_printf(seq, "%s: HP %s Controller\n"
  353. "Board ID: 0x%08lx\n"
  354. "Firmware Version: %c%c%c%c\n"
  355. "IRQ: %d\n"
  356. "Logical drives: %d\n"
  357. "Current Q depth: %d\n"
  358. "Current # commands on controller: %d\n"
  359. "Max Q depth since init: %d\n"
  360. "Max # commands on controller since init: %d\n"
  361. "Max SG entries since init: %d\n",
  362. h->devname,
  363. h->product_name,
  364. (unsigned long)h->board_id,
  365. h->firm_ver[0], h->firm_ver[1], h->firm_ver[2],
  366. h->firm_ver[3], (unsigned int)h->intr[PERF_MODE_INT],
  367. h->num_luns,
  368. h->Qdepth, h->commands_outstanding,
  369. h->maxQsinceinit, h->max_outstanding, h->maxSG);
  370. #ifdef CONFIG_CISS_SCSI_TAPE
  371. cciss_seq_tape_report(seq, h);
  372. #endif /* CONFIG_CISS_SCSI_TAPE */
  373. }
  374. static void *cciss_seq_start(struct seq_file *seq, loff_t *pos)
  375. {
  376. ctlr_info_t *h = seq->private;
  377. unsigned long flags;
  378. /* prevent displaying bogus info during configuration
  379. * or deconfiguration of a logical volume
  380. */
  381. spin_lock_irqsave(&h->lock, flags);
  382. if (h->busy_configuring) {
  383. spin_unlock_irqrestore(&h->lock, flags);
  384. return ERR_PTR(-EBUSY);
  385. }
  386. h->busy_configuring = 1;
  387. spin_unlock_irqrestore(&h->lock, flags);
  388. if (*pos == 0)
  389. cciss_seq_show_header(seq);
  390. return pos;
  391. }
  392. static int cciss_seq_show(struct seq_file *seq, void *v)
  393. {
  394. sector_t vol_sz, vol_sz_frac;
  395. ctlr_info_t *h = seq->private;
  396. unsigned ctlr = h->ctlr;
  397. loff_t *pos = v;
  398. drive_info_struct *drv = h->drv[*pos];
  399. if (*pos > h->highest_lun)
  400. return 0;
  401. if (drv == NULL) /* it's possible for h->drv[] to have holes. */
  402. return 0;
  403. if (drv->heads == 0)
  404. return 0;
  405. vol_sz = drv->nr_blocks;
  406. vol_sz_frac = sector_div(vol_sz, ENG_GIG_FACTOR);
  407. vol_sz_frac *= 100;
  408. sector_div(vol_sz_frac, ENG_GIG_FACTOR);
  409. if (drv->raid_level < 0 || drv->raid_level > RAID_UNKNOWN)
  410. drv->raid_level = RAID_UNKNOWN;
  411. seq_printf(seq, "cciss/c%dd%d:"
  412. "\t%4u.%02uGB\tRAID %s\n",
  413. ctlr, (int) *pos, (int)vol_sz, (int)vol_sz_frac,
  414. raid_label[drv->raid_level]);
  415. return 0;
  416. }
  417. static void *cciss_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  418. {
  419. ctlr_info_t *h = seq->private;
  420. if (*pos > h->highest_lun)
  421. return NULL;
  422. *pos += 1;
  423. return pos;
  424. }
  425. static void cciss_seq_stop(struct seq_file *seq, void *v)
  426. {
  427. ctlr_info_t *h = seq->private;
  428. /* Only reset h->busy_configuring if we succeeded in setting
  429. * it during cciss_seq_start. */
  430. if (v == ERR_PTR(-EBUSY))
  431. return;
  432. h->busy_configuring = 0;
  433. }
  434. static const struct seq_operations cciss_seq_ops = {
  435. .start = cciss_seq_start,
  436. .show = cciss_seq_show,
  437. .next = cciss_seq_next,
  438. .stop = cciss_seq_stop,
  439. };
  440. static int cciss_seq_open(struct inode *inode, struct file *file)
  441. {
  442. int ret = seq_open(file, &cciss_seq_ops);
  443. struct seq_file *seq = file->private_data;
  444. if (!ret)
  445. seq->private = PDE(inode)->data;
  446. return ret;
  447. }
  448. static ssize_t
  449. cciss_proc_write(struct file *file, const char __user *buf,
  450. size_t length, loff_t *ppos)
  451. {
  452. int err;
  453. char *buffer;
  454. #ifndef CONFIG_CISS_SCSI_TAPE
  455. return -EINVAL;
  456. #endif
  457. if (!buf || length > PAGE_SIZE - 1)
  458. return -EINVAL;
  459. buffer = (char *)__get_free_page(GFP_KERNEL);
  460. if (!buffer)
  461. return -ENOMEM;
  462. err = -EFAULT;
  463. if (copy_from_user(buffer, buf, length))
  464. goto out;
  465. buffer[length] = '\0';
  466. #ifdef CONFIG_CISS_SCSI_TAPE
  467. if (strncmp(ENGAGE_SCSI, buffer, sizeof ENGAGE_SCSI - 1) == 0) {
  468. struct seq_file *seq = file->private_data;
  469. ctlr_info_t *h = seq->private;
  470. err = cciss_engage_scsi(h);
  471. if (err == 0)
  472. err = length;
  473. } else
  474. #endif /* CONFIG_CISS_SCSI_TAPE */
  475. err = -EINVAL;
  476. /* might be nice to have "disengage" too, but it's not
  477. safely possible. (only 1 module use count, lock issues.) */
  478. out:
  479. free_page((unsigned long)buffer);
  480. return err;
  481. }
  482. static const struct file_operations cciss_proc_fops = {
  483. .owner = THIS_MODULE,
  484. .open = cciss_seq_open,
  485. .read = seq_read,
  486. .llseek = seq_lseek,
  487. .release = seq_release,
  488. .write = cciss_proc_write,
  489. };
  490. static void __devinit cciss_procinit(ctlr_info_t *h)
  491. {
  492. struct proc_dir_entry *pde;
  493. if (proc_cciss == NULL)
  494. proc_cciss = proc_mkdir("driver/cciss", NULL);
  495. if (!proc_cciss)
  496. return;
  497. pde = proc_create_data(h->devname, S_IWUSR | S_IRUSR | S_IRGRP |
  498. S_IROTH, proc_cciss,
  499. &cciss_proc_fops, h);
  500. }
  501. #endif /* CONFIG_PROC_FS */
  502. #define MAX_PRODUCT_NAME_LEN 19
  503. #define to_hba(n) container_of(n, struct ctlr_info, dev)
  504. #define to_drv(n) container_of(n, drive_info_struct, dev)
  505. static ssize_t host_store_rescan(struct device *dev,
  506. struct device_attribute *attr,
  507. const char *buf, size_t count)
  508. {
  509. struct ctlr_info *h = to_hba(dev);
  510. add_to_scan_list(h);
  511. wake_up_process(cciss_scan_thread);
  512. wait_for_completion_interruptible(&h->scan_wait);
  513. return count;
  514. }
  515. static DEVICE_ATTR(rescan, S_IWUSR, NULL, host_store_rescan);
  516. static ssize_t dev_show_unique_id(struct device *dev,
  517. struct device_attribute *attr,
  518. char *buf)
  519. {
  520. drive_info_struct *drv = to_drv(dev);
  521. struct ctlr_info *h = to_hba(drv->dev.parent);
  522. __u8 sn[16];
  523. unsigned long flags;
  524. int ret = 0;
  525. spin_lock_irqsave(&h->lock, flags);
  526. if (h->busy_configuring)
  527. ret = -EBUSY;
  528. else
  529. memcpy(sn, drv->serial_no, sizeof(sn));
  530. spin_unlock_irqrestore(&h->lock, flags);
  531. if (ret)
  532. return ret;
  533. else
  534. return snprintf(buf, 16 * 2 + 2,
  535. "%02X%02X%02X%02X%02X%02X%02X%02X"
  536. "%02X%02X%02X%02X%02X%02X%02X%02X\n",
  537. sn[0], sn[1], sn[2], sn[3],
  538. sn[4], sn[5], sn[6], sn[7],
  539. sn[8], sn[9], sn[10], sn[11],
  540. sn[12], sn[13], sn[14], sn[15]);
  541. }
  542. static DEVICE_ATTR(unique_id, S_IRUGO, dev_show_unique_id, NULL);
  543. static ssize_t dev_show_vendor(struct device *dev,
  544. struct device_attribute *attr,
  545. char *buf)
  546. {
  547. drive_info_struct *drv = to_drv(dev);
  548. struct ctlr_info *h = to_hba(drv->dev.parent);
  549. char vendor[VENDOR_LEN + 1];
  550. unsigned long flags;
  551. int ret = 0;
  552. spin_lock_irqsave(&h->lock, flags);
  553. if (h->busy_configuring)
  554. ret = -EBUSY;
  555. else
  556. memcpy(vendor, drv->vendor, VENDOR_LEN + 1);
  557. spin_unlock_irqrestore(&h->lock, flags);
  558. if (ret)
  559. return ret;
  560. else
  561. return snprintf(buf, sizeof(vendor) + 1, "%s\n", drv->vendor);
  562. }
  563. static DEVICE_ATTR(vendor, S_IRUGO, dev_show_vendor, NULL);
  564. static ssize_t dev_show_model(struct device *dev,
  565. struct device_attribute *attr,
  566. char *buf)
  567. {
  568. drive_info_struct *drv = to_drv(dev);
  569. struct ctlr_info *h = to_hba(drv->dev.parent);
  570. char model[MODEL_LEN + 1];
  571. unsigned long flags;
  572. int ret = 0;
  573. spin_lock_irqsave(&h->lock, flags);
  574. if (h->busy_configuring)
  575. ret = -EBUSY;
  576. else
  577. memcpy(model, drv->model, MODEL_LEN + 1);
  578. spin_unlock_irqrestore(&h->lock, flags);
  579. if (ret)
  580. return ret;
  581. else
  582. return snprintf(buf, sizeof(model) + 1, "%s\n", drv->model);
  583. }
  584. static DEVICE_ATTR(model, S_IRUGO, dev_show_model, NULL);
  585. static ssize_t dev_show_rev(struct device *dev,
  586. struct device_attribute *attr,
  587. char *buf)
  588. {
  589. drive_info_struct *drv = to_drv(dev);
  590. struct ctlr_info *h = to_hba(drv->dev.parent);
  591. char rev[REV_LEN + 1];
  592. unsigned long flags;
  593. int ret = 0;
  594. spin_lock_irqsave(&h->lock, flags);
  595. if (h->busy_configuring)
  596. ret = -EBUSY;
  597. else
  598. memcpy(rev, drv->rev, REV_LEN + 1);
  599. spin_unlock_irqrestore(&h->lock, flags);
  600. if (ret)
  601. return ret;
  602. else
  603. return snprintf(buf, sizeof(rev) + 1, "%s\n", drv->rev);
  604. }
  605. static DEVICE_ATTR(rev, S_IRUGO, dev_show_rev, NULL);
  606. static ssize_t cciss_show_lunid(struct device *dev,
  607. struct device_attribute *attr, char *buf)
  608. {
  609. drive_info_struct *drv = to_drv(dev);
  610. struct ctlr_info *h = to_hba(drv->dev.parent);
  611. unsigned long flags;
  612. unsigned char lunid[8];
  613. spin_lock_irqsave(&h->lock, flags);
  614. if (h->busy_configuring) {
  615. spin_unlock_irqrestore(&h->lock, flags);
  616. return -EBUSY;
  617. }
  618. if (!drv->heads) {
  619. spin_unlock_irqrestore(&h->lock, flags);
  620. return -ENOTTY;
  621. }
  622. memcpy(lunid, drv->LunID, sizeof(lunid));
  623. spin_unlock_irqrestore(&h->lock, flags);
  624. return snprintf(buf, 20, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
  625. lunid[0], lunid[1], lunid[2], lunid[3],
  626. lunid[4], lunid[5], lunid[6], lunid[7]);
  627. }
  628. static DEVICE_ATTR(lunid, S_IRUGO, cciss_show_lunid, NULL);
  629. static ssize_t cciss_show_raid_level(struct device *dev,
  630. struct device_attribute *attr, char *buf)
  631. {
  632. drive_info_struct *drv = to_drv(dev);
  633. struct ctlr_info *h = to_hba(drv->dev.parent);
  634. int raid;
  635. unsigned long flags;
  636. spin_lock_irqsave(&h->lock, flags);
  637. if (h->busy_configuring) {
  638. spin_unlock_irqrestore(&h->lock, flags);
  639. return -EBUSY;
  640. }
  641. raid = drv->raid_level;
  642. spin_unlock_irqrestore(&h->lock, flags);
  643. if (raid < 0 || raid > RAID_UNKNOWN)
  644. raid = RAID_UNKNOWN;
  645. return snprintf(buf, strlen(raid_label[raid]) + 7, "RAID %s\n",
  646. raid_label[raid]);
  647. }
  648. static DEVICE_ATTR(raid_level, S_IRUGO, cciss_show_raid_level, NULL);
  649. static ssize_t cciss_show_usage_count(struct device *dev,
  650. struct device_attribute *attr, char *buf)
  651. {
  652. drive_info_struct *drv = to_drv(dev);
  653. struct ctlr_info *h = to_hba(drv->dev.parent);
  654. unsigned long flags;
  655. int count;
  656. spin_lock_irqsave(&h->lock, flags);
  657. if (h->busy_configuring) {
  658. spin_unlock_irqrestore(&h->lock, flags);
  659. return -EBUSY;
  660. }
  661. count = drv->usage_count;
  662. spin_unlock_irqrestore(&h->lock, flags);
  663. return snprintf(buf, 20, "%d\n", count);
  664. }
  665. static DEVICE_ATTR(usage_count, S_IRUGO, cciss_show_usage_count, NULL);
  666. static struct attribute *cciss_host_attrs[] = {
  667. &dev_attr_rescan.attr,
  668. NULL
  669. };
  670. static struct attribute_group cciss_host_attr_group = {
  671. .attrs = cciss_host_attrs,
  672. };
  673. static const struct attribute_group *cciss_host_attr_groups[] = {
  674. &cciss_host_attr_group,
  675. NULL
  676. };
  677. static struct device_type cciss_host_type = {
  678. .name = "cciss_host",
  679. .groups = cciss_host_attr_groups,
  680. .release = cciss_hba_release,
  681. };
  682. static struct attribute *cciss_dev_attrs[] = {
  683. &dev_attr_unique_id.attr,
  684. &dev_attr_model.attr,
  685. &dev_attr_vendor.attr,
  686. &dev_attr_rev.attr,
  687. &dev_attr_lunid.attr,
  688. &dev_attr_raid_level.attr,
  689. &dev_attr_usage_count.attr,
  690. NULL
  691. };
  692. static struct attribute_group cciss_dev_attr_group = {
  693. .attrs = cciss_dev_attrs,
  694. };
  695. static const struct attribute_group *cciss_dev_attr_groups[] = {
  696. &cciss_dev_attr_group,
  697. NULL
  698. };
  699. static struct device_type cciss_dev_type = {
  700. .name = "cciss_device",
  701. .groups = cciss_dev_attr_groups,
  702. .release = cciss_device_release,
  703. };
  704. static struct bus_type cciss_bus_type = {
  705. .name = "cciss",
  706. };
  707. /*
  708. * cciss_hba_release is called when the reference count
  709. * of h->dev goes to zero.
  710. */
  711. static void cciss_hba_release(struct device *dev)
  712. {
  713. /*
  714. * nothing to do, but need this to avoid a warning
  715. * about not having a release handler from lib/kref.c.
  716. */
  717. }
  718. /*
  719. * Initialize sysfs entry for each controller. This sets up and registers
  720. * the 'cciss#' directory for each individual controller under
  721. * /sys/bus/pci/devices/<dev>/.
  722. */
  723. static int cciss_create_hba_sysfs_entry(struct ctlr_info *h)
  724. {
  725. device_initialize(&h->dev);
  726. h->dev.type = &cciss_host_type;
  727. h->dev.bus = &cciss_bus_type;
  728. dev_set_name(&h->dev, "%s", h->devname);
  729. h->dev.parent = &h->pdev->dev;
  730. return device_add(&h->dev);
  731. }
  732. /*
  733. * Remove sysfs entries for an hba.
  734. */
  735. static void cciss_destroy_hba_sysfs_entry(struct ctlr_info *h)
  736. {
  737. device_del(&h->dev);
  738. put_device(&h->dev); /* final put. */
  739. }
  740. /* cciss_device_release is called when the reference count
  741. * of h->drv[x]dev goes to zero.
  742. */
  743. static void cciss_device_release(struct device *dev)
  744. {
  745. drive_info_struct *drv = to_drv(dev);
  746. kfree(drv);
  747. }
  748. /*
  749. * Initialize sysfs for each logical drive. This sets up and registers
  750. * the 'c#d#' directory for each individual logical drive under
  751. * /sys/bus/pci/devices/<dev/ccis#/. We also create a link from
  752. * /sys/block/cciss!c#d# to this entry.
  753. */
  754. static long cciss_create_ld_sysfs_entry(struct ctlr_info *h,
  755. int drv_index)
  756. {
  757. struct device *dev;
  758. if (h->drv[drv_index]->device_initialized)
  759. return 0;
  760. dev = &h->drv[drv_index]->dev;
  761. device_initialize(dev);
  762. dev->type = &cciss_dev_type;
  763. dev->bus = &cciss_bus_type;
  764. dev_set_name(dev, "c%dd%d", h->ctlr, drv_index);
  765. dev->parent = &h->dev;
  766. h->drv[drv_index]->device_initialized = 1;
  767. return device_add(dev);
  768. }
  769. /*
  770. * Remove sysfs entries for a logical drive.
  771. */
  772. static void cciss_destroy_ld_sysfs_entry(struct ctlr_info *h, int drv_index,
  773. int ctlr_exiting)
  774. {
  775. struct device *dev = &h->drv[drv_index]->dev;
  776. /* special case for c*d0, we only destroy it on controller exit */
  777. if (drv_index == 0 && !ctlr_exiting)
  778. return;
  779. device_del(dev);
  780. put_device(dev); /* the "final" put. */
  781. h->drv[drv_index] = NULL;
  782. }
  783. /*
  784. * For operations that cannot sleep, a command block is allocated at init,
  785. * and managed by cmd_alloc() and cmd_free() using a simple bitmap to track
  786. * which ones are free or in use.
  787. */
  788. static CommandList_struct *cmd_alloc(ctlr_info_t *h)
  789. {
  790. CommandList_struct *c;
  791. int i;
  792. u64bit temp64;
  793. dma_addr_t cmd_dma_handle, err_dma_handle;
  794. do {
  795. i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
  796. if (i == h->nr_cmds)
  797. return NULL;
  798. } while (test_and_set_bit(i & (BITS_PER_LONG - 1),
  799. h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
  800. c = h->cmd_pool + i;
  801. memset(c, 0, sizeof(CommandList_struct));
  802. cmd_dma_handle = h->cmd_pool_dhandle + i * sizeof(CommandList_struct);
  803. c->err_info = h->errinfo_pool + i;
  804. memset(c->err_info, 0, sizeof(ErrorInfo_struct));
  805. err_dma_handle = h->errinfo_pool_dhandle
  806. + i * sizeof(ErrorInfo_struct);
  807. h->nr_allocs++;
  808. c->cmdindex = i;
  809. INIT_HLIST_NODE(&c->list);
  810. c->busaddr = (__u32) cmd_dma_handle;
  811. temp64.val = (__u64) err_dma_handle;
  812. c->ErrDesc.Addr.lower = temp64.val32.lower;
  813. c->ErrDesc.Addr.upper = temp64.val32.upper;
  814. c->ErrDesc.Len = sizeof(ErrorInfo_struct);
  815. c->ctlr = h->ctlr;
  816. return c;
  817. }
  818. /* allocate a command using pci_alloc_consistent, used for ioctls,
  819. * etc., not for the main i/o path.
  820. */
  821. static CommandList_struct *cmd_special_alloc(ctlr_info_t *h)
  822. {
  823. CommandList_struct *c;
  824. u64bit temp64;
  825. dma_addr_t cmd_dma_handle, err_dma_handle;
  826. c = (CommandList_struct *) pci_alloc_consistent(h->pdev,
  827. sizeof(CommandList_struct), &cmd_dma_handle);
  828. if (c == NULL)
  829. return NULL;
  830. memset(c, 0, sizeof(CommandList_struct));
  831. c->cmdindex = -1;
  832. c->err_info = (ErrorInfo_struct *)
  833. pci_alloc_consistent(h->pdev, sizeof(ErrorInfo_struct),
  834. &err_dma_handle);
  835. if (c->err_info == NULL) {
  836. pci_free_consistent(h->pdev,
  837. sizeof(CommandList_struct), c, cmd_dma_handle);
  838. return NULL;
  839. }
  840. memset(c->err_info, 0, sizeof(ErrorInfo_struct));
  841. INIT_HLIST_NODE(&c->list);
  842. c->busaddr = (__u32) cmd_dma_handle;
  843. temp64.val = (__u64) err_dma_handle;
  844. c->ErrDesc.Addr.lower = temp64.val32.lower;
  845. c->ErrDesc.Addr.upper = temp64.val32.upper;
  846. c->ErrDesc.Len = sizeof(ErrorInfo_struct);
  847. c->ctlr = h->ctlr;
  848. return c;
  849. }
  850. static void cmd_free(ctlr_info_t *h, CommandList_struct *c)
  851. {
  852. int i;
  853. i = c - h->cmd_pool;
  854. clear_bit(i & (BITS_PER_LONG - 1),
  855. h->cmd_pool_bits + (i / BITS_PER_LONG));
  856. h->nr_frees++;
  857. }
  858. static void cmd_special_free(ctlr_info_t *h, CommandList_struct *c)
  859. {
  860. u64bit temp64;
  861. temp64.val32.lower = c->ErrDesc.Addr.lower;
  862. temp64.val32.upper = c->ErrDesc.Addr.upper;
  863. pci_free_consistent(h->pdev, sizeof(ErrorInfo_struct),
  864. c->err_info, (dma_addr_t) temp64.val);
  865. pci_free_consistent(h->pdev, sizeof(CommandList_struct),
  866. c, (dma_addr_t) c->busaddr);
  867. }
  868. static inline ctlr_info_t *get_host(struct gendisk *disk)
  869. {
  870. return disk->queue->queuedata;
  871. }
  872. static inline drive_info_struct *get_drv(struct gendisk *disk)
  873. {
  874. return disk->private_data;
  875. }
  876. /*
  877. * Open. Make sure the device is really there.
  878. */
  879. static int cciss_open(struct block_device *bdev, fmode_t mode)
  880. {
  881. ctlr_info_t *h = get_host(bdev->bd_disk);
  882. drive_info_struct *drv = get_drv(bdev->bd_disk);
  883. dev_dbg(&h->pdev->dev, "cciss_open %s\n", bdev->bd_disk->disk_name);
  884. if (drv->busy_configuring)
  885. return -EBUSY;
  886. /*
  887. * Root is allowed to open raw volume zero even if it's not configured
  888. * so array config can still work. Root is also allowed to open any
  889. * volume that has a LUN ID, so it can issue IOCTL to reread the
  890. * disk information. I don't think I really like this
  891. * but I'm already using way to many device nodes to claim another one
  892. * for "raw controller".
  893. */
  894. if (drv->heads == 0) {
  895. if (MINOR(bdev->bd_dev) != 0) { /* not node 0? */
  896. /* if not node 0 make sure it is a partition = 0 */
  897. if (MINOR(bdev->bd_dev) & 0x0f) {
  898. return -ENXIO;
  899. /* if it is, make sure we have a LUN ID */
  900. } else if (memcmp(drv->LunID, CTLR_LUNID,
  901. sizeof(drv->LunID))) {
  902. return -ENXIO;
  903. }
  904. }
  905. if (!capable(CAP_SYS_ADMIN))
  906. return -EPERM;
  907. }
  908. drv->usage_count++;
  909. h->usage_count++;
  910. return 0;
  911. }
  912. static int cciss_unlocked_open(struct block_device *bdev, fmode_t mode)
  913. {
  914. int ret;
  915. lock_kernel();
  916. ret = cciss_open(bdev, mode);
  917. unlock_kernel();
  918. return ret;
  919. }
  920. /*
  921. * Close. Sync first.
  922. */
  923. static int cciss_release(struct gendisk *disk, fmode_t mode)
  924. {
  925. ctlr_info_t *h;
  926. drive_info_struct *drv;
  927. lock_kernel();
  928. h = get_host(disk);
  929. drv = get_drv(disk);
  930. dev_dbg(&h->pdev->dev, "cciss_release %s\n", disk->disk_name);
  931. drv->usage_count--;
  932. h->usage_count--;
  933. unlock_kernel();
  934. return 0;
  935. }
  936. static int do_ioctl(struct block_device *bdev, fmode_t mode,
  937. unsigned cmd, unsigned long arg)
  938. {
  939. int ret;
  940. lock_kernel();
  941. ret = cciss_ioctl(bdev, mode, cmd, arg);
  942. unlock_kernel();
  943. return ret;
  944. }
  945. #ifdef CONFIG_COMPAT
  946. static int cciss_ioctl32_passthru(struct block_device *bdev, fmode_t mode,
  947. unsigned cmd, unsigned long arg);
  948. static int cciss_ioctl32_big_passthru(struct block_device *bdev, fmode_t mode,
  949. unsigned cmd, unsigned long arg);
  950. static int cciss_compat_ioctl(struct block_device *bdev, fmode_t mode,
  951. unsigned cmd, unsigned long arg)
  952. {
  953. switch (cmd) {
  954. case CCISS_GETPCIINFO:
  955. case CCISS_GETINTINFO:
  956. case CCISS_SETINTINFO:
  957. case CCISS_GETNODENAME:
  958. case CCISS_SETNODENAME:
  959. case CCISS_GETHEARTBEAT:
  960. case CCISS_GETBUSTYPES:
  961. case CCISS_GETFIRMVER:
  962. case CCISS_GETDRIVVER:
  963. case CCISS_REVALIDVOLS:
  964. case CCISS_DEREGDISK:
  965. case CCISS_REGNEWDISK:
  966. case CCISS_REGNEWD:
  967. case CCISS_RESCANDISK:
  968. case CCISS_GETLUNINFO:
  969. return do_ioctl(bdev, mode, cmd, arg);
  970. case CCISS_PASSTHRU32:
  971. return cciss_ioctl32_passthru(bdev, mode, cmd, arg);
  972. case CCISS_BIG_PASSTHRU32:
  973. return cciss_ioctl32_big_passthru(bdev, mode, cmd, arg);
  974. default:
  975. return -ENOIOCTLCMD;
  976. }
  977. }
  978. static int cciss_ioctl32_passthru(struct block_device *bdev, fmode_t mode,
  979. unsigned cmd, unsigned long arg)
  980. {
  981. IOCTL32_Command_struct __user *arg32 =
  982. (IOCTL32_Command_struct __user *) arg;
  983. IOCTL_Command_struct arg64;
  984. IOCTL_Command_struct __user *p = compat_alloc_user_space(sizeof(arg64));
  985. int err;
  986. u32 cp;
  987. err = 0;
  988. err |=
  989. copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
  990. sizeof(arg64.LUN_info));
  991. err |=
  992. copy_from_user(&arg64.Request, &arg32->Request,
  993. sizeof(arg64.Request));
  994. err |=
  995. copy_from_user(&arg64.error_info, &arg32->error_info,
  996. sizeof(arg64.error_info));
  997. err |= get_user(arg64.buf_size, &arg32->buf_size);
  998. err |= get_user(cp, &arg32->buf);
  999. arg64.buf = compat_ptr(cp);
  1000. err |= copy_to_user(p, &arg64, sizeof(arg64));
  1001. if (err)
  1002. return -EFAULT;
  1003. err = do_ioctl(bdev, mode, CCISS_PASSTHRU, (unsigned long)p);
  1004. if (err)
  1005. return err;
  1006. err |=
  1007. copy_in_user(&arg32->error_info, &p->error_info,
  1008. sizeof(arg32->error_info));
  1009. if (err)
  1010. return -EFAULT;
  1011. return err;
  1012. }
  1013. static int cciss_ioctl32_big_passthru(struct block_device *bdev, fmode_t mode,
  1014. unsigned cmd, unsigned long arg)
  1015. {
  1016. BIG_IOCTL32_Command_struct __user *arg32 =
  1017. (BIG_IOCTL32_Command_struct __user *) arg;
  1018. BIG_IOCTL_Command_struct arg64;
  1019. BIG_IOCTL_Command_struct __user *p =
  1020. compat_alloc_user_space(sizeof(arg64));
  1021. int err;
  1022. u32 cp;
  1023. err = 0;
  1024. err |=
  1025. copy_from_user(&arg64.LUN_info, &arg32->LUN_info,
  1026. sizeof(arg64.LUN_info));
  1027. err |=
  1028. copy_from_user(&arg64.Request, &arg32->Request,
  1029. sizeof(arg64.Request));
  1030. err |=
  1031. copy_from_user(&arg64.error_info, &arg32->error_info,
  1032. sizeof(arg64.error_info));
  1033. err |= get_user(arg64.buf_size, &arg32->buf_size);
  1034. err |= get_user(arg64.malloc_size, &arg32->malloc_size);
  1035. err |= get_user(cp, &arg32->buf);
  1036. arg64.buf = compat_ptr(cp);
  1037. err |= copy_to_user(p, &arg64, sizeof(arg64));
  1038. if (err)
  1039. return -EFAULT;
  1040. err = do_ioctl(bdev, mode, CCISS_BIG_PASSTHRU, (unsigned long)p);
  1041. if (err)
  1042. return err;
  1043. err |=
  1044. copy_in_user(&arg32->error_info, &p->error_info,
  1045. sizeof(arg32->error_info));
  1046. if (err)
  1047. return -EFAULT;
  1048. return err;
  1049. }
  1050. #endif
  1051. static int cciss_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  1052. {
  1053. drive_info_struct *drv = get_drv(bdev->bd_disk);
  1054. if (!drv->cylinders)
  1055. return -ENXIO;
  1056. geo->heads = drv->heads;
  1057. geo->sectors = drv->sectors;
  1058. geo->cylinders = drv->cylinders;
  1059. return 0;
  1060. }
  1061. static void check_ioctl_unit_attention(ctlr_info_t *h, CommandList_struct *c)
  1062. {
  1063. if (c->err_info->CommandStatus == CMD_TARGET_STATUS &&
  1064. c->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION)
  1065. (void)check_for_unit_attention(h, c);
  1066. }
  1067. static int cciss_getpciinfo(ctlr_info_t *h, void __user *argp)
  1068. {
  1069. cciss_pci_info_struct pciinfo;
  1070. if (!argp)
  1071. return -EINVAL;
  1072. pciinfo.domain = pci_domain_nr(h->pdev->bus);
  1073. pciinfo.bus = h->pdev->bus->number;
  1074. pciinfo.dev_fn = h->pdev->devfn;
  1075. pciinfo.board_id = h->board_id;
  1076. if (copy_to_user(argp, &pciinfo, sizeof(cciss_pci_info_struct)))
  1077. return -EFAULT;
  1078. return 0;
  1079. }
  1080. static int cciss_getintinfo(ctlr_info_t *h, void __user *argp)
  1081. {
  1082. cciss_coalint_struct intinfo;
  1083. if (!argp)
  1084. return -EINVAL;
  1085. intinfo.delay = readl(&h->cfgtable->HostWrite.CoalIntDelay);
  1086. intinfo.count = readl(&h->cfgtable->HostWrite.CoalIntCount);
  1087. if (copy_to_user
  1088. (argp, &intinfo, sizeof(cciss_coalint_struct)))
  1089. return -EFAULT;
  1090. return 0;
  1091. }
  1092. static int cciss_setintinfo(ctlr_info_t *h, void __user *argp)
  1093. {
  1094. cciss_coalint_struct intinfo;
  1095. unsigned long flags;
  1096. int i;
  1097. if (!argp)
  1098. return -EINVAL;
  1099. if (!capable(CAP_SYS_ADMIN))
  1100. return -EPERM;
  1101. if (copy_from_user(&intinfo, argp, sizeof(intinfo)))
  1102. return -EFAULT;
  1103. if ((intinfo.delay == 0) && (intinfo.count == 0))
  1104. return -EINVAL;
  1105. spin_lock_irqsave(&h->lock, flags);
  1106. /* Update the field, and then ring the doorbell */
  1107. writel(intinfo.delay, &(h->cfgtable->HostWrite.CoalIntDelay));
  1108. writel(intinfo.count, &(h->cfgtable->HostWrite.CoalIntCount));
  1109. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  1110. for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) {
  1111. if (!(readl(h->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
  1112. break;
  1113. udelay(1000); /* delay and try again */
  1114. }
  1115. spin_unlock_irqrestore(&h->lock, flags);
  1116. if (i >= MAX_IOCTL_CONFIG_WAIT)
  1117. return -EAGAIN;
  1118. return 0;
  1119. }
  1120. static int cciss_getnodename(ctlr_info_t *h, void __user *argp)
  1121. {
  1122. NodeName_type NodeName;
  1123. int i;
  1124. if (!argp)
  1125. return -EINVAL;
  1126. for (i = 0; i < 16; i++)
  1127. NodeName[i] = readb(&h->cfgtable->ServerName[i]);
  1128. if (copy_to_user(argp, NodeName, sizeof(NodeName_type)))
  1129. return -EFAULT;
  1130. return 0;
  1131. }
  1132. static int cciss_setnodename(ctlr_info_t *h, void __user *argp)
  1133. {
  1134. NodeName_type NodeName;
  1135. unsigned long flags;
  1136. int i;
  1137. if (!argp)
  1138. return -EINVAL;
  1139. if (!capable(CAP_SYS_ADMIN))
  1140. return -EPERM;
  1141. if (copy_from_user(NodeName, argp, sizeof(NodeName_type)))
  1142. return -EFAULT;
  1143. spin_lock_irqsave(&h->lock, flags);
  1144. /* Update the field, and then ring the doorbell */
  1145. for (i = 0; i < 16; i++)
  1146. writeb(NodeName[i], &h->cfgtable->ServerName[i]);
  1147. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  1148. for (i = 0; i < MAX_IOCTL_CONFIG_WAIT; i++) {
  1149. if (!(readl(h->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
  1150. break;
  1151. udelay(1000); /* delay and try again */
  1152. }
  1153. spin_unlock_irqrestore(&h->lock, flags);
  1154. if (i >= MAX_IOCTL_CONFIG_WAIT)
  1155. return -EAGAIN;
  1156. return 0;
  1157. }
  1158. static int cciss_getheartbeat(ctlr_info_t *h, void __user *argp)
  1159. {
  1160. Heartbeat_type heartbeat;
  1161. if (!argp)
  1162. return -EINVAL;
  1163. heartbeat = readl(&h->cfgtable->HeartBeat);
  1164. if (copy_to_user(argp, &heartbeat, sizeof(Heartbeat_type)))
  1165. return -EFAULT;
  1166. return 0;
  1167. }
  1168. static int cciss_getbustypes(ctlr_info_t *h, void __user *argp)
  1169. {
  1170. BusTypes_type BusTypes;
  1171. if (!argp)
  1172. return -EINVAL;
  1173. BusTypes = readl(&h->cfgtable->BusTypes);
  1174. if (copy_to_user(argp, &BusTypes, sizeof(BusTypes_type)))
  1175. return -EFAULT;
  1176. return 0;
  1177. }
  1178. static int cciss_getfirmver(ctlr_info_t *h, void __user *argp)
  1179. {
  1180. FirmwareVer_type firmware;
  1181. if (!argp)
  1182. return -EINVAL;
  1183. memcpy(firmware, h->firm_ver, 4);
  1184. if (copy_to_user
  1185. (argp, firmware, sizeof(FirmwareVer_type)))
  1186. return -EFAULT;
  1187. return 0;
  1188. }
  1189. static int cciss_getdrivver(ctlr_info_t *h, void __user *argp)
  1190. {
  1191. DriverVer_type DriverVer = DRIVER_VERSION;
  1192. if (!argp)
  1193. return -EINVAL;
  1194. if (copy_to_user(argp, &DriverVer, sizeof(DriverVer_type)))
  1195. return -EFAULT;
  1196. return 0;
  1197. }
  1198. static int cciss_getluninfo(ctlr_info_t *h,
  1199. struct gendisk *disk, void __user *argp)
  1200. {
  1201. LogvolInfo_struct luninfo;
  1202. drive_info_struct *drv = get_drv(disk);
  1203. if (!argp)
  1204. return -EINVAL;
  1205. memcpy(&luninfo.LunID, drv->LunID, sizeof(luninfo.LunID));
  1206. luninfo.num_opens = drv->usage_count;
  1207. luninfo.num_parts = 0;
  1208. if (copy_to_user(argp, &luninfo, sizeof(LogvolInfo_struct)))
  1209. return -EFAULT;
  1210. return 0;
  1211. }
  1212. static int cciss_passthru(ctlr_info_t *h, void __user *argp)
  1213. {
  1214. IOCTL_Command_struct iocommand;
  1215. CommandList_struct *c;
  1216. char *buff = NULL;
  1217. u64bit temp64;
  1218. DECLARE_COMPLETION_ONSTACK(wait);
  1219. if (!argp)
  1220. return -EINVAL;
  1221. if (!capable(CAP_SYS_RAWIO))
  1222. return -EPERM;
  1223. if (copy_from_user
  1224. (&iocommand, argp, sizeof(IOCTL_Command_struct)))
  1225. return -EFAULT;
  1226. if ((iocommand.buf_size < 1) &&
  1227. (iocommand.Request.Type.Direction != XFER_NONE)) {
  1228. return -EINVAL;
  1229. }
  1230. if (iocommand.buf_size > 0) {
  1231. buff = kmalloc(iocommand.buf_size, GFP_KERNEL);
  1232. if (buff == NULL)
  1233. return -EFAULT;
  1234. }
  1235. if (iocommand.Request.Type.Direction == XFER_WRITE) {
  1236. /* Copy the data into the buffer we created */
  1237. if (copy_from_user(buff, iocommand.buf, iocommand.buf_size)) {
  1238. kfree(buff);
  1239. return -EFAULT;
  1240. }
  1241. } else {
  1242. memset(buff, 0, iocommand.buf_size);
  1243. }
  1244. c = cmd_special_alloc(h);
  1245. if (!c) {
  1246. kfree(buff);
  1247. return -ENOMEM;
  1248. }
  1249. /* Fill in the command type */
  1250. c->cmd_type = CMD_IOCTL_PEND;
  1251. /* Fill in Command Header */
  1252. c->Header.ReplyQueue = 0; /* unused in simple mode */
  1253. if (iocommand.buf_size > 0) { /* buffer to fill */
  1254. c->Header.SGList = 1;
  1255. c->Header.SGTotal = 1;
  1256. } else { /* no buffers to fill */
  1257. c->Header.SGList = 0;
  1258. c->Header.SGTotal = 0;
  1259. }
  1260. c->Header.LUN = iocommand.LUN_info;
  1261. /* use the kernel address the cmd block for tag */
  1262. c->Header.Tag.lower = c->busaddr;
  1263. /* Fill in Request block */
  1264. c->Request = iocommand.Request;
  1265. /* Fill in the scatter gather information */
  1266. if (iocommand.buf_size > 0) {
  1267. temp64.val = pci_map_single(h->pdev, buff,
  1268. iocommand.buf_size, PCI_DMA_BIDIRECTIONAL);
  1269. c->SG[0].Addr.lower = temp64.val32.lower;
  1270. c->SG[0].Addr.upper = temp64.val32.upper;
  1271. c->SG[0].Len = iocommand.buf_size;
  1272. c->SG[0].Ext = 0; /* we are not chaining */
  1273. }
  1274. c->waiting = &wait;
  1275. enqueue_cmd_and_start_io(h, c);
  1276. wait_for_completion(&wait);
  1277. /* unlock the buffers from DMA */
  1278. temp64.val32.lower = c->SG[0].Addr.lower;
  1279. temp64.val32.upper = c->SG[0].Addr.upper;
  1280. pci_unmap_single(h->pdev, (dma_addr_t) temp64.val, iocommand.buf_size,
  1281. PCI_DMA_BIDIRECTIONAL);
  1282. check_ioctl_unit_attention(h, c);
  1283. /* Copy the error information out */
  1284. iocommand.error_info = *(c->err_info);
  1285. if (copy_to_user(argp, &iocommand, sizeof(IOCTL_Command_struct))) {
  1286. kfree(buff);
  1287. cmd_special_free(h, c);
  1288. return -EFAULT;
  1289. }
  1290. if (iocommand.Request.Type.Direction == XFER_READ) {
  1291. /* Copy the data out of the buffer we created */
  1292. if (copy_to_user(iocommand.buf, buff, iocommand.buf_size)) {
  1293. kfree(buff);
  1294. cmd_special_free(h, c);
  1295. return -EFAULT;
  1296. }
  1297. }
  1298. kfree(buff);
  1299. cmd_special_free(h, c);
  1300. return 0;
  1301. }
  1302. static int cciss_ioctl(struct block_device *bdev, fmode_t mode,
  1303. unsigned int cmd, unsigned long arg)
  1304. {
  1305. struct gendisk *disk = bdev->bd_disk;
  1306. ctlr_info_t *h = get_host(disk);
  1307. void __user *argp = (void __user *)arg;
  1308. dev_dbg(&h->pdev->dev, "cciss_ioctl: Called with cmd=%x %lx\n",
  1309. cmd, arg);
  1310. switch (cmd) {
  1311. case CCISS_GETPCIINFO:
  1312. return cciss_getpciinfo(h, argp);
  1313. case CCISS_GETINTINFO:
  1314. return cciss_getintinfo(h, argp);
  1315. case CCISS_SETINTINFO:
  1316. return cciss_setintinfo(h, argp);
  1317. case CCISS_GETNODENAME:
  1318. return cciss_getnodename(h, argp);
  1319. case CCISS_SETNODENAME:
  1320. return cciss_setnodename(h, argp);
  1321. case CCISS_GETHEARTBEAT:
  1322. return cciss_getheartbeat(h, argp);
  1323. case CCISS_GETBUSTYPES:
  1324. return cciss_getbustypes(h, argp);
  1325. case CCISS_GETFIRMVER:
  1326. return cciss_getfirmver(h, argp);
  1327. case CCISS_GETDRIVVER:
  1328. return cciss_getdrivver(h, argp);
  1329. case CCISS_DEREGDISK:
  1330. case CCISS_REGNEWD:
  1331. case CCISS_REVALIDVOLS:
  1332. return rebuild_lun_table(h, 0, 1);
  1333. case CCISS_GETLUNINFO:
  1334. return cciss_getluninfo(h, disk, argp);
  1335. case CCISS_PASSTHRU:
  1336. return cciss_passthru(h, argp);
  1337. case CCISS_BIG_PASSTHRU:{
  1338. BIG_IOCTL_Command_struct *ioc;
  1339. CommandList_struct *c;
  1340. unsigned char **buff = NULL;
  1341. int *buff_size = NULL;
  1342. u64bit temp64;
  1343. BYTE sg_used = 0;
  1344. int status = 0;
  1345. int i;
  1346. DECLARE_COMPLETION_ONSTACK(wait);
  1347. __u32 left;
  1348. __u32 sz;
  1349. BYTE __user *data_ptr;
  1350. if (!arg)
  1351. return -EINVAL;
  1352. if (!capable(CAP_SYS_RAWIO))
  1353. return -EPERM;
  1354. ioc = (BIG_IOCTL_Command_struct *)
  1355. kmalloc(sizeof(*ioc), GFP_KERNEL);
  1356. if (!ioc) {
  1357. status = -ENOMEM;
  1358. goto cleanup1;
  1359. }
  1360. if (copy_from_user(ioc, argp, sizeof(*ioc))) {
  1361. status = -EFAULT;
  1362. goto cleanup1;
  1363. }
  1364. if ((ioc->buf_size < 1) &&
  1365. (ioc->Request.Type.Direction != XFER_NONE)) {
  1366. status = -EINVAL;
  1367. goto cleanup1;
  1368. }
  1369. /* Check kmalloc limits using all SGs */
  1370. if (ioc->malloc_size > MAX_KMALLOC_SIZE) {
  1371. status = -EINVAL;
  1372. goto cleanup1;
  1373. }
  1374. if (ioc->buf_size > ioc->malloc_size * MAXSGENTRIES) {
  1375. status = -EINVAL;
  1376. goto cleanup1;
  1377. }
  1378. buff =
  1379. kzalloc(MAXSGENTRIES * sizeof(char *), GFP_KERNEL);
  1380. if (!buff) {
  1381. status = -ENOMEM;
  1382. goto cleanup1;
  1383. }
  1384. buff_size = kmalloc(MAXSGENTRIES * sizeof(int),
  1385. GFP_KERNEL);
  1386. if (!buff_size) {
  1387. status = -ENOMEM;
  1388. goto cleanup1;
  1389. }
  1390. left = ioc->buf_size;
  1391. data_ptr = ioc->buf;
  1392. while (left) {
  1393. sz = (left >
  1394. ioc->malloc_size) ? ioc->
  1395. malloc_size : left;
  1396. buff_size[sg_used] = sz;
  1397. buff[sg_used] = kmalloc(sz, GFP_KERNEL);
  1398. if (buff[sg_used] == NULL) {
  1399. status = -ENOMEM;
  1400. goto cleanup1;
  1401. }
  1402. if (ioc->Request.Type.Direction == XFER_WRITE) {
  1403. if (copy_from_user
  1404. (buff[sg_used], data_ptr, sz)) {
  1405. status = -EFAULT;
  1406. goto cleanup1;
  1407. }
  1408. } else {
  1409. memset(buff[sg_used], 0, sz);
  1410. }
  1411. left -= sz;
  1412. data_ptr += sz;
  1413. sg_used++;
  1414. }
  1415. c = cmd_special_alloc(h);
  1416. if (!c) {
  1417. status = -ENOMEM;
  1418. goto cleanup1;
  1419. }
  1420. c->cmd_type = CMD_IOCTL_PEND;
  1421. c->Header.ReplyQueue = 0;
  1422. if (ioc->buf_size > 0) {
  1423. c->Header.SGList = sg_used;
  1424. c->Header.SGTotal = sg_used;
  1425. } else {
  1426. c->Header.SGList = 0;
  1427. c->Header.SGTotal = 0;
  1428. }
  1429. c->Header.LUN = ioc->LUN_info;
  1430. c->Header.Tag.lower = c->busaddr;
  1431. c->Request = ioc->Request;
  1432. if (ioc->buf_size > 0) {
  1433. for (i = 0; i < sg_used; i++) {
  1434. temp64.val =
  1435. pci_map_single(h->pdev, buff[i],
  1436. buff_size[i],
  1437. PCI_DMA_BIDIRECTIONAL);
  1438. c->SG[i].Addr.lower =
  1439. temp64.val32.lower;
  1440. c->SG[i].Addr.upper =
  1441. temp64.val32.upper;
  1442. c->SG[i].Len = buff_size[i];
  1443. c->SG[i].Ext = 0; /* we are not chaining */
  1444. }
  1445. }
  1446. c->waiting = &wait;
  1447. enqueue_cmd_and_start_io(h, c);
  1448. wait_for_completion(&wait);
  1449. /* unlock the buffers from DMA */
  1450. for (i = 0; i < sg_used; i++) {
  1451. temp64.val32.lower = c->SG[i].Addr.lower;
  1452. temp64.val32.upper = c->SG[i].Addr.upper;
  1453. pci_unmap_single(h->pdev,
  1454. (dma_addr_t) temp64.val, buff_size[i],
  1455. PCI_DMA_BIDIRECTIONAL);
  1456. }
  1457. check_ioctl_unit_attention(h, c);
  1458. /* Copy the error information out */
  1459. ioc->error_info = *(c->err_info);
  1460. if (copy_to_user(argp, ioc, sizeof(*ioc))) {
  1461. cmd_special_free(h, c);
  1462. status = -EFAULT;
  1463. goto cleanup1;
  1464. }
  1465. if (ioc->Request.Type.Direction == XFER_READ) {
  1466. /* Copy the data out of the buffer we created */
  1467. BYTE __user *ptr = ioc->buf;
  1468. for (i = 0; i < sg_used; i++) {
  1469. if (copy_to_user
  1470. (ptr, buff[i], buff_size[i])) {
  1471. cmd_special_free(h, c);
  1472. status = -EFAULT;
  1473. goto cleanup1;
  1474. }
  1475. ptr += buff_size[i];
  1476. }
  1477. }
  1478. cmd_special_free(h, c);
  1479. status = 0;
  1480. cleanup1:
  1481. if (buff) {
  1482. for (i = 0; i < sg_used; i++)
  1483. kfree(buff[i]);
  1484. kfree(buff);
  1485. }
  1486. kfree(buff_size);
  1487. kfree(ioc);
  1488. return status;
  1489. }
  1490. /* scsi_cmd_ioctl handles these, below, though some are not */
  1491. /* very meaningful for cciss. SG_IO is the main one people want. */
  1492. case SG_GET_VERSION_NUM:
  1493. case SG_SET_TIMEOUT:
  1494. case SG_GET_TIMEOUT:
  1495. case SG_GET_RESERVED_SIZE:
  1496. case SG_SET_RESERVED_SIZE:
  1497. case SG_EMULATED_HOST:
  1498. case SG_IO:
  1499. case SCSI_IOCTL_SEND_COMMAND:
  1500. return scsi_cmd_ioctl(disk->queue, disk, mode, cmd, argp);
  1501. /* scsi_cmd_ioctl would normally handle these, below, but */
  1502. /* they aren't a good fit for cciss, as CD-ROMs are */
  1503. /* not supported, and we don't have any bus/target/lun */
  1504. /* which we present to the kernel. */
  1505. case CDROM_SEND_PACKET:
  1506. case CDROMCLOSETRAY:
  1507. case CDROMEJECT:
  1508. case SCSI_IOCTL_GET_IDLUN:
  1509. case SCSI_IOCTL_GET_BUS_NUMBER:
  1510. default:
  1511. return -ENOTTY;
  1512. }
  1513. }
  1514. static void cciss_check_queues(ctlr_info_t *h)
  1515. {
  1516. int start_queue = h->next_to_run;
  1517. int i;
  1518. /* check to see if we have maxed out the number of commands that can
  1519. * be placed on the queue. If so then exit. We do this check here
  1520. * in case the interrupt we serviced was from an ioctl and did not
  1521. * free any new commands.
  1522. */
  1523. if ((find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds)) == h->nr_cmds)
  1524. return;
  1525. /* We have room on the queue for more commands. Now we need to queue
  1526. * them up. We will also keep track of the next queue to run so
  1527. * that every queue gets a chance to be started first.
  1528. */
  1529. for (i = 0; i < h->highest_lun + 1; i++) {
  1530. int curr_queue = (start_queue + i) % (h->highest_lun + 1);
  1531. /* make sure the disk has been added and the drive is real
  1532. * because this can be called from the middle of init_one.
  1533. */
  1534. if (!h->drv[curr_queue])
  1535. continue;
  1536. if (!(h->drv[curr_queue]->queue) ||
  1537. !(h->drv[curr_queue]->heads))
  1538. continue;
  1539. blk_start_queue(h->gendisk[curr_queue]->queue);
  1540. /* check to see if we have maxed out the number of commands
  1541. * that can be placed on the queue.
  1542. */
  1543. if ((find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds)) == h->nr_cmds) {
  1544. if (curr_queue == start_queue) {
  1545. h->next_to_run =
  1546. (start_queue + 1) % (h->highest_lun + 1);
  1547. break;
  1548. } else {
  1549. h->next_to_run = curr_queue;
  1550. break;
  1551. }
  1552. }
  1553. }
  1554. }
  1555. static void cciss_softirq_done(struct request *rq)
  1556. {
  1557. CommandList_struct *c = rq->completion_data;
  1558. ctlr_info_t *h = hba[c->ctlr];
  1559. SGDescriptor_struct *curr_sg = c->SG;
  1560. u64bit temp64;
  1561. unsigned long flags;
  1562. int i, ddir;
  1563. int sg_index = 0;
  1564. if (c->Request.Type.Direction == XFER_READ)
  1565. ddir = PCI_DMA_FROMDEVICE;
  1566. else
  1567. ddir = PCI_DMA_TODEVICE;
  1568. /* command did not need to be retried */
  1569. /* unmap the DMA mapping for all the scatter gather elements */
  1570. for (i = 0; i < c->Header.SGList; i++) {
  1571. if (curr_sg[sg_index].Ext == CCISS_SG_CHAIN) {
  1572. cciss_unmap_sg_chain_block(h, c);
  1573. /* Point to the next block */
  1574. curr_sg = h->cmd_sg_list[c->cmdindex];
  1575. sg_index = 0;
  1576. }
  1577. temp64.val32.lower = curr_sg[sg_index].Addr.lower;
  1578. temp64.val32.upper = curr_sg[sg_index].Addr.upper;
  1579. pci_unmap_page(h->pdev, temp64.val, curr_sg[sg_index].Len,
  1580. ddir);
  1581. ++sg_index;
  1582. }
  1583. dev_dbg(&h->pdev->dev, "Done with %p\n", rq);
  1584. /* set the residual count for pc requests */
  1585. if (rq->cmd_type == REQ_TYPE_BLOCK_PC)
  1586. rq->resid_len = c->err_info->ResidualCnt;
  1587. blk_end_request_all(rq, (rq->errors == 0) ? 0 : -EIO);
  1588. spin_lock_irqsave(&h->lock, flags);
  1589. cmd_free(h, c);
  1590. cciss_check_queues(h);
  1591. spin_unlock_irqrestore(&h->lock, flags);
  1592. }
  1593. static inline void log_unit_to_scsi3addr(ctlr_info_t *h,
  1594. unsigned char scsi3addr[], uint32_t log_unit)
  1595. {
  1596. memcpy(scsi3addr, h->drv[log_unit]->LunID,
  1597. sizeof(h->drv[log_unit]->LunID));
  1598. }
  1599. /* This function gets the SCSI vendor, model, and revision of a logical drive
  1600. * via the inquiry page 0. Model, vendor, and rev are set to empty strings if
  1601. * they cannot be read.
  1602. */
  1603. static void cciss_get_device_descr(ctlr_info_t *h, int logvol,
  1604. char *vendor, char *model, char *rev)
  1605. {
  1606. int rc;
  1607. InquiryData_struct *inq_buf;
  1608. unsigned char scsi3addr[8];
  1609. *vendor = '\0';
  1610. *model = '\0';
  1611. *rev = '\0';
  1612. inq_buf = kzalloc(sizeof(InquiryData_struct), GFP_KERNEL);
  1613. if (!inq_buf)
  1614. return;
  1615. log_unit_to_scsi3addr(h, scsi3addr, logvol);
  1616. rc = sendcmd_withirq(h, CISS_INQUIRY, inq_buf, sizeof(*inq_buf), 0,
  1617. scsi3addr, TYPE_CMD);
  1618. if (rc == IO_OK) {
  1619. memcpy(vendor, &inq_buf->data_byte[8], VENDOR_LEN);
  1620. vendor[VENDOR_LEN] = '\0';
  1621. memcpy(model, &inq_buf->data_byte[16], MODEL_LEN);
  1622. model[MODEL_LEN] = '\0';
  1623. memcpy(rev, &inq_buf->data_byte[32], REV_LEN);
  1624. rev[REV_LEN] = '\0';
  1625. }
  1626. kfree(inq_buf);
  1627. return;
  1628. }
  1629. /* This function gets the serial number of a logical drive via
  1630. * inquiry page 0x83. Serial no. is 16 bytes. If the serial
  1631. * number cannot be had, for whatever reason, 16 bytes of 0xff
  1632. * are returned instead.
  1633. */
  1634. static void cciss_get_serial_no(ctlr_info_t *h, int logvol,
  1635. unsigned char *serial_no, int buflen)
  1636. {
  1637. #define PAGE_83_INQ_BYTES 64
  1638. int rc;
  1639. unsigned char *buf;
  1640. unsigned char scsi3addr[8];
  1641. if (buflen > 16)
  1642. buflen = 16;
  1643. memset(serial_no, 0xff, buflen);
  1644. buf = kzalloc(PAGE_83_INQ_BYTES, GFP_KERNEL);
  1645. if (!buf)
  1646. return;
  1647. memset(serial_no, 0, buflen);
  1648. log_unit_to_scsi3addr(h, scsi3addr, logvol);
  1649. rc = sendcmd_withirq(h, CISS_INQUIRY, buf,
  1650. PAGE_83_INQ_BYTES, 0x83, scsi3addr, TYPE_CMD);
  1651. if (rc == IO_OK)
  1652. memcpy(serial_no, &buf[8], buflen);
  1653. kfree(buf);
  1654. return;
  1655. }
  1656. /*
  1657. * cciss_add_disk sets up the block device queue for a logical drive
  1658. */
  1659. static int cciss_add_disk(ctlr_info_t *h, struct gendisk *disk,
  1660. int drv_index)
  1661. {
  1662. disk->queue = blk_init_queue(do_cciss_request, &h->lock);
  1663. if (!disk->queue)
  1664. goto init_queue_failure;
  1665. sprintf(disk->disk_name, "cciss/c%dd%d", h->ctlr, drv_index);
  1666. disk->major = h->major;
  1667. disk->first_minor = drv_index << NWD_SHIFT;
  1668. disk->fops = &cciss_fops;
  1669. if (cciss_create_ld_sysfs_entry(h, drv_index))
  1670. goto cleanup_queue;
  1671. disk->private_data = h->drv[drv_index];
  1672. disk->driverfs_dev = &h->drv[drv_index]->dev;
  1673. /* Set up queue information */
  1674. blk_queue_bounce_limit(disk->queue, h->pdev->dma_mask);
  1675. /* This is a hardware imposed limit. */
  1676. blk_queue_max_segments(disk->queue, h->maxsgentries);
  1677. blk_queue_max_hw_sectors(disk->queue, h->cciss_max_sectors);
  1678. blk_queue_softirq_done(disk->queue, cciss_softirq_done);
  1679. disk->queue->queuedata = h;
  1680. blk_queue_logical_block_size(disk->queue,
  1681. h->drv[drv_index]->block_size);
  1682. /* Make sure all queue data is written out before */
  1683. /* setting h->drv[drv_index]->queue, as setting this */
  1684. /* allows the interrupt handler to start the queue */
  1685. wmb();
  1686. h->drv[drv_index]->queue = disk->queue;
  1687. add_disk(disk);
  1688. return 0;
  1689. cleanup_queue:
  1690. blk_cleanup_queue(disk->queue);
  1691. disk->queue = NULL;
  1692. init_queue_failure:
  1693. return -1;
  1694. }
  1695. /* This function will check the usage_count of the drive to be updated/added.
  1696. * If the usage_count is zero and it is a heretofore unknown drive, or,
  1697. * the drive's capacity, geometry, or serial number has changed,
  1698. * then the drive information will be updated and the disk will be
  1699. * re-registered with the kernel. If these conditions don't hold,
  1700. * then it will be left alone for the next reboot. The exception to this
  1701. * is disk 0 which will always be left registered with the kernel since it
  1702. * is also the controller node. Any changes to disk 0 will show up on
  1703. * the next reboot.
  1704. */
  1705. static void cciss_update_drive_info(ctlr_info_t *h, int drv_index,
  1706. int first_time, int via_ioctl)
  1707. {
  1708. struct gendisk *disk;
  1709. InquiryData_struct *inq_buff = NULL;
  1710. unsigned int block_size;
  1711. sector_t total_size;
  1712. unsigned long flags = 0;
  1713. int ret = 0;
  1714. drive_info_struct *drvinfo;
  1715. /* Get information about the disk and modify the driver structure */
  1716. inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
  1717. drvinfo = kzalloc(sizeof(*drvinfo), GFP_KERNEL);
  1718. if (inq_buff == NULL || drvinfo == NULL)
  1719. goto mem_msg;
  1720. /* testing to see if 16-byte CDBs are already being used */
  1721. if (h->cciss_read == CCISS_READ_16) {
  1722. cciss_read_capacity_16(h, drv_index,
  1723. &total_size, &block_size);
  1724. } else {
  1725. cciss_read_capacity(h, drv_index, &total_size, &block_size);
  1726. /* if read_capacity returns all F's this volume is >2TB */
  1727. /* in size so we switch to 16-byte CDB's for all */
  1728. /* read/write ops */
  1729. if (total_size == 0xFFFFFFFFULL) {
  1730. cciss_read_capacity_16(h, drv_index,
  1731. &total_size, &block_size);
  1732. h->cciss_read = CCISS_READ_16;
  1733. h->cciss_write = CCISS_WRITE_16;
  1734. } else {
  1735. h->cciss_read = CCISS_READ_10;
  1736. h->cciss_write = CCISS_WRITE_10;
  1737. }
  1738. }
  1739. cciss_geometry_inquiry(h, drv_index, total_size, block_size,
  1740. inq_buff, drvinfo);
  1741. drvinfo->block_size = block_size;
  1742. drvinfo->nr_blocks = total_size + 1;
  1743. cciss_get_device_descr(h, drv_index, drvinfo->vendor,
  1744. drvinfo->model, drvinfo->rev);
  1745. cciss_get_serial_no(h, drv_index, drvinfo->serial_no,
  1746. sizeof(drvinfo->serial_no));
  1747. /* Save the lunid in case we deregister the disk, below. */
  1748. memcpy(drvinfo->LunID, h->drv[drv_index]->LunID,
  1749. sizeof(drvinfo->LunID));
  1750. /* Is it the same disk we already know, and nothing's changed? */
  1751. if (h->drv[drv_index]->raid_level != -1 &&
  1752. ((memcmp(drvinfo->serial_no,
  1753. h->drv[drv_index]->serial_no, 16) == 0) &&
  1754. drvinfo->block_size == h->drv[drv_index]->block_size &&
  1755. drvinfo->nr_blocks == h->drv[drv_index]->nr_blocks &&
  1756. drvinfo->heads == h->drv[drv_index]->heads &&
  1757. drvinfo->sectors == h->drv[drv_index]->sectors &&
  1758. drvinfo->cylinders == h->drv[drv_index]->cylinders))
  1759. /* The disk is unchanged, nothing to update */
  1760. goto freeret;
  1761. /* If we get here it's not the same disk, or something's changed,
  1762. * so we need to * deregister it, and re-register it, if it's not
  1763. * in use.
  1764. * If the disk already exists then deregister it before proceeding
  1765. * (unless it's the first disk (for the controller node).
  1766. */
  1767. if (h->drv[drv_index]->raid_level != -1 && drv_index != 0) {
  1768. dev_warn(&h->pdev->dev, "disk %d has changed.\n", drv_index);
  1769. spin_lock_irqsave(&h->lock, flags);
  1770. h->drv[drv_index]->busy_configuring = 1;
  1771. spin_unlock_irqrestore(&h->lock, flags);
  1772. /* deregister_disk sets h->drv[drv_index]->queue = NULL
  1773. * which keeps the interrupt handler from starting
  1774. * the queue.
  1775. */
  1776. ret = deregister_disk(h, drv_index, 0, via_ioctl);
  1777. }
  1778. /* If the disk is in use return */
  1779. if (ret)
  1780. goto freeret;
  1781. /* Save the new information from cciss_geometry_inquiry
  1782. * and serial number inquiry. If the disk was deregistered
  1783. * above, then h->drv[drv_index] will be NULL.
  1784. */
  1785. if (h->drv[drv_index] == NULL) {
  1786. drvinfo->device_initialized = 0;
  1787. h->drv[drv_index] = drvinfo;
  1788. drvinfo = NULL; /* so it won't be freed below. */
  1789. } else {
  1790. /* special case for cxd0 */
  1791. h->drv[drv_index]->block_size = drvinfo->block_size;
  1792. h->drv[drv_index]->nr_blocks = drvinfo->nr_blocks;
  1793. h->drv[drv_index]->heads = drvinfo->heads;
  1794. h->drv[drv_index]->sectors = drvinfo->sectors;
  1795. h->drv[drv_index]->cylinders = drvinfo->cylinders;
  1796. h->drv[drv_index]->raid_level = drvinfo->raid_level;
  1797. memcpy(h->drv[drv_index]->serial_no, drvinfo->serial_no, 16);
  1798. memcpy(h->drv[drv_index]->vendor, drvinfo->vendor,
  1799. VENDOR_LEN + 1);
  1800. memcpy(h->drv[drv_index]->model, drvinfo->model, MODEL_LEN + 1);
  1801. memcpy(h->drv[drv_index]->rev, drvinfo->rev, REV_LEN + 1);
  1802. }
  1803. ++h->num_luns;
  1804. disk = h->gendisk[drv_index];
  1805. set_capacity(disk, h->drv[drv_index]->nr_blocks);
  1806. /* If it's not disk 0 (drv_index != 0)
  1807. * or if it was disk 0, but there was previously
  1808. * no actual corresponding configured logical drive
  1809. * (raid_leve == -1) then we want to update the
  1810. * logical drive's information.
  1811. */
  1812. if (drv_index || first_time) {
  1813. if (cciss_add_disk(h, disk, drv_index) != 0) {
  1814. cciss_free_gendisk(h, drv_index);
  1815. cciss_free_drive_info(h, drv_index);
  1816. dev_warn(&h->pdev->dev, "could not update disk %d\n",
  1817. drv_index);
  1818. --h->num_luns;
  1819. }
  1820. }
  1821. freeret:
  1822. kfree(inq_buff);
  1823. kfree(drvinfo);
  1824. return;
  1825. mem_msg:
  1826. dev_err(&h->pdev->dev, "out of memory\n");
  1827. goto freeret;
  1828. }
  1829. /* This function will find the first index of the controllers drive array
  1830. * that has a null drv pointer and allocate the drive info struct and
  1831. * will return that index This is where new drives will be added.
  1832. * If the index to be returned is greater than the highest_lun index for
  1833. * the controller then highest_lun is set * to this new index.
  1834. * If there are no available indexes or if tha allocation fails, then -1
  1835. * is returned. * "controller_node" is used to know if this is a real
  1836. * logical drive, or just the controller node, which determines if this
  1837. * counts towards highest_lun.
  1838. */
  1839. static int cciss_alloc_drive_info(ctlr_info_t *h, int controller_node)
  1840. {
  1841. int i;
  1842. drive_info_struct *drv;
  1843. /* Search for an empty slot for our drive info */
  1844. for (i = 0; i < CISS_MAX_LUN; i++) {
  1845. /* if not cxd0 case, and it's occupied, skip it. */
  1846. if (h->drv[i] && i != 0)
  1847. continue;
  1848. /*
  1849. * If it's cxd0 case, and drv is alloc'ed already, and a
  1850. * disk is configured there, skip it.
  1851. */
  1852. if (i == 0 && h->drv[i] && h->drv[i]->raid_level != -1)
  1853. continue;
  1854. /*
  1855. * We've found an empty slot. Update highest_lun
  1856. * provided this isn't just the fake cxd0 controller node.
  1857. */
  1858. if (i > h->highest_lun && !controller_node)
  1859. h->highest_lun = i;
  1860. /* If adding a real disk at cxd0, and it's already alloc'ed */
  1861. if (i == 0 && h->drv[i] != NULL)
  1862. return i;
  1863. /*
  1864. * Found an empty slot, not already alloc'ed. Allocate it.
  1865. * Mark it with raid_level == -1, so we know it's new later on.
  1866. */
  1867. drv = kzalloc(sizeof(*drv), GFP_KERNEL);
  1868. if (!drv)
  1869. return -1;
  1870. drv->raid_level = -1; /* so we know it's new */
  1871. h->drv[i] = drv;
  1872. return i;
  1873. }
  1874. return -1;
  1875. }
  1876. static void cciss_free_drive_info(ctlr_info_t *h, int drv_index)
  1877. {
  1878. kfree(h->drv[drv_index]);
  1879. h->drv[drv_index] = NULL;
  1880. }
  1881. static void cciss_free_gendisk(ctlr_info_t *h, int drv_index)
  1882. {
  1883. put_disk(h->gendisk[drv_index]);
  1884. h->gendisk[drv_index] = NULL;
  1885. }
  1886. /* cciss_add_gendisk finds a free hba[]->drv structure
  1887. * and allocates a gendisk if needed, and sets the lunid
  1888. * in the drvinfo structure. It returns the index into
  1889. * the ->drv[] array, or -1 if none are free.
  1890. * is_controller_node indicates whether highest_lun should
  1891. * count this disk, or if it's only being added to provide
  1892. * a means to talk to the controller in case no logical
  1893. * drives have yet been configured.
  1894. */
  1895. static int cciss_add_gendisk(ctlr_info_t *h, unsigned char lunid[],
  1896. int controller_node)
  1897. {
  1898. int drv_index;
  1899. drv_index = cciss_alloc_drive_info(h, controller_node);
  1900. if (drv_index == -1)
  1901. return -1;
  1902. /*Check if the gendisk needs to be allocated */
  1903. if (!h->gendisk[drv_index]) {
  1904. h->gendisk[drv_index] =
  1905. alloc_disk(1 << NWD_SHIFT);
  1906. if (!h->gendisk[drv_index]) {
  1907. dev_err(&h->pdev->dev,
  1908. "could not allocate a new disk %d\n",
  1909. drv_index);
  1910. goto err_free_drive_info;
  1911. }
  1912. }
  1913. memcpy(h->drv[drv_index]->LunID, lunid,
  1914. sizeof(h->drv[drv_index]->LunID));
  1915. if (cciss_create_ld_sysfs_entry(h, drv_index))
  1916. goto err_free_disk;
  1917. /* Don't need to mark this busy because nobody */
  1918. /* else knows about this disk yet to contend */
  1919. /* for access to it. */
  1920. h->drv[drv_index]->busy_configuring = 0;
  1921. wmb();
  1922. return drv_index;
  1923. err_free_disk:
  1924. cciss_free_gendisk(h, drv_index);
  1925. err_free_drive_info:
  1926. cciss_free_drive_info(h, drv_index);
  1927. return -1;
  1928. }
  1929. /* This is for the special case of a controller which
  1930. * has no logical drives. In this case, we still need
  1931. * to register a disk so the controller can be accessed
  1932. * by the Array Config Utility.
  1933. */
  1934. static void cciss_add_controller_node(ctlr_info_t *h)
  1935. {
  1936. struct gendisk *disk;
  1937. int drv_index;
  1938. if (h->gendisk[0] != NULL) /* already did this? Then bail. */
  1939. return;
  1940. drv_index = cciss_add_gendisk(h, CTLR_LUNID, 1);
  1941. if (drv_index == -1)
  1942. goto error;
  1943. h->drv[drv_index]->block_size = 512;
  1944. h->drv[drv_index]->nr_blocks = 0;
  1945. h->drv[drv_index]->heads = 0;
  1946. h->drv[drv_index]->sectors = 0;
  1947. h->drv[drv_index]->cylinders = 0;
  1948. h->drv[drv_index]->raid_level = -1;
  1949. memset(h->drv[drv_index]->serial_no, 0, 16);
  1950. disk = h->gendisk[drv_index];
  1951. if (cciss_add_disk(h, disk, drv_index) == 0)
  1952. return;
  1953. cciss_free_gendisk(h, drv_index);
  1954. cciss_free_drive_info(h, drv_index);
  1955. error:
  1956. dev_warn(&h->pdev->dev, "could not add disk 0.\n");
  1957. return;
  1958. }
  1959. /* This function will add and remove logical drives from the Logical
  1960. * drive array of the controller and maintain persistency of ordering
  1961. * so that mount points are preserved until the next reboot. This allows
  1962. * for the removal of logical drives in the middle of the drive array
  1963. * without a re-ordering of those drives.
  1964. * INPUT
  1965. * h = The controller to perform the operations on
  1966. */
  1967. static int rebuild_lun_table(ctlr_info_t *h, int first_time,
  1968. int via_ioctl)
  1969. {
  1970. int num_luns;
  1971. ReportLunData_struct *ld_buff = NULL;
  1972. int return_code;
  1973. int listlength = 0;
  1974. int i;
  1975. int drv_found;
  1976. int drv_index = 0;
  1977. unsigned char lunid[8] = CTLR_LUNID;
  1978. unsigned long flags;
  1979. if (!capable(CAP_SYS_RAWIO))
  1980. return -EPERM;
  1981. /* Set busy_configuring flag for this operation */
  1982. spin_lock_irqsave(&h->lock, flags);
  1983. if (h->busy_configuring) {
  1984. spin_unlock_irqrestore(&h->lock, flags);
  1985. return -EBUSY;
  1986. }
  1987. h->busy_configuring = 1;
  1988. spin_unlock_irqrestore(&h->lock, flags);
  1989. ld_buff = kzalloc(sizeof(ReportLunData_struct), GFP_KERNEL);
  1990. if (ld_buff == NULL)
  1991. goto mem_msg;
  1992. return_code = sendcmd_withirq(h, CISS_REPORT_LOG, ld_buff,
  1993. sizeof(ReportLunData_struct),
  1994. 0, CTLR_LUNID, TYPE_CMD);
  1995. if (return_code == IO_OK)
  1996. listlength = be32_to_cpu(*(__be32 *) ld_buff->LUNListLength);
  1997. else { /* reading number of logical volumes failed */
  1998. dev_warn(&h->pdev->dev,
  1999. "report logical volume command failed\n");
  2000. listlength = 0;
  2001. goto freeret;
  2002. }
  2003. num_luns = listlength / 8; /* 8 bytes per entry */
  2004. if (num_luns > CISS_MAX_LUN) {
  2005. num_luns = CISS_MAX_LUN;
  2006. dev_warn(&h->pdev->dev, "more luns configured"
  2007. " on controller than can be handled by"
  2008. " this driver.\n");
  2009. }
  2010. if (num_luns == 0)
  2011. cciss_add_controller_node(h);
  2012. /* Compare controller drive array to driver's drive array
  2013. * to see if any drives are missing on the controller due
  2014. * to action of Array Config Utility (user deletes drive)
  2015. * and deregister logical drives which have disappeared.
  2016. */
  2017. for (i = 0; i <= h->highest_lun; i++) {
  2018. int j;
  2019. drv_found = 0;
  2020. /* skip holes in the array from already deleted drives */
  2021. if (h->drv[i] == NULL)
  2022. continue;
  2023. for (j = 0; j < num_luns; j++) {
  2024. memcpy(lunid, &ld_buff->LUN[j][0], sizeof(lunid));
  2025. if (memcmp(h->drv[i]->LunID, lunid,
  2026. sizeof(lunid)) == 0) {
  2027. drv_found = 1;
  2028. break;
  2029. }
  2030. }
  2031. if (!drv_found) {
  2032. /* Deregister it from the OS, it's gone. */
  2033. spin_lock_irqsave(&h->lock, flags);
  2034. h->drv[i]->busy_configuring = 1;
  2035. spin_unlock_irqrestore(&h->lock, flags);
  2036. return_code = deregister_disk(h, i, 1, via_ioctl);
  2037. if (h->drv[i] != NULL)
  2038. h->drv[i]->busy_configuring = 0;
  2039. }
  2040. }
  2041. /* Compare controller drive array to driver's drive array.
  2042. * Check for updates in the drive information and any new drives
  2043. * on the controller due to ACU adding logical drives, or changing
  2044. * a logical drive's size, etc. Reregister any new/changed drives
  2045. */
  2046. for (i = 0; i < num_luns; i++) {
  2047. int j;
  2048. drv_found = 0;
  2049. memcpy(lunid, &ld_buff->LUN[i][0], sizeof(lunid));
  2050. /* Find if the LUN is already in the drive array
  2051. * of the driver. If so then update its info
  2052. * if not in use. If it does not exist then find
  2053. * the first free index and add it.
  2054. */
  2055. for (j = 0; j <= h->highest_lun; j++) {
  2056. if (h->drv[j] != NULL &&
  2057. memcmp(h->drv[j]->LunID, lunid,
  2058. sizeof(h->drv[j]->LunID)) == 0) {
  2059. drv_index = j;
  2060. drv_found = 1;
  2061. break;
  2062. }
  2063. }
  2064. /* check if the drive was found already in the array */
  2065. if (!drv_found) {
  2066. drv_index = cciss_add_gendisk(h, lunid, 0);
  2067. if (drv_index == -1)
  2068. goto freeret;
  2069. }
  2070. cciss_update_drive_info(h, drv_index, first_time, via_ioctl);
  2071. } /* end for */
  2072. freeret:
  2073. kfree(ld_buff);
  2074. h->busy_configuring = 0;
  2075. /* We return -1 here to tell the ACU that we have registered/updated
  2076. * all of the drives that we can and to keep it from calling us
  2077. * additional times.
  2078. */
  2079. return -1;
  2080. mem_msg:
  2081. dev_err(&h->pdev->dev, "out of memory\n");
  2082. h->busy_configuring = 0;
  2083. goto freeret;
  2084. }
  2085. static void cciss_clear_drive_info(drive_info_struct *drive_info)
  2086. {
  2087. /* zero out the disk size info */
  2088. drive_info->nr_blocks = 0;
  2089. drive_info->block_size = 0;
  2090. drive_info->heads = 0;
  2091. drive_info->sectors = 0;
  2092. drive_info->cylinders = 0;
  2093. drive_info->raid_level = -1;
  2094. memset(drive_info->serial_no, 0, sizeof(drive_info->serial_no));
  2095. memset(drive_info->model, 0, sizeof(drive_info->model));
  2096. memset(drive_info->rev, 0, sizeof(drive_info->rev));
  2097. memset(drive_info->vendor, 0, sizeof(drive_info->vendor));
  2098. /*
  2099. * don't clear the LUNID though, we need to remember which
  2100. * one this one is.
  2101. */
  2102. }
  2103. /* This function will deregister the disk and it's queue from the
  2104. * kernel. It must be called with the controller lock held and the
  2105. * drv structures busy_configuring flag set. It's parameters are:
  2106. *
  2107. * disk = This is the disk to be deregistered
  2108. * drv = This is the drive_info_struct associated with the disk to be
  2109. * deregistered. It contains information about the disk used
  2110. * by the driver.
  2111. * clear_all = This flag determines whether or not the disk information
  2112. * is going to be completely cleared out and the highest_lun
  2113. * reset. Sometimes we want to clear out information about
  2114. * the disk in preparation for re-adding it. In this case
  2115. * the highest_lun should be left unchanged and the LunID
  2116. * should not be cleared.
  2117. * via_ioctl
  2118. * This indicates whether we've reached this path via ioctl.
  2119. * This affects the maximum usage count allowed for c0d0 to be messed with.
  2120. * If this path is reached via ioctl(), then the max_usage_count will
  2121. * be 1, as the process calling ioctl() has got to have the device open.
  2122. * If we get here via sysfs, then the max usage count will be zero.
  2123. */
  2124. static int deregister_disk(ctlr_info_t *h, int drv_index,
  2125. int clear_all, int via_ioctl)
  2126. {
  2127. int i;
  2128. struct gendisk *disk;
  2129. drive_info_struct *drv;
  2130. int recalculate_highest_lun;
  2131. if (!capable(CAP_SYS_RAWIO))
  2132. return -EPERM;
  2133. drv = h->drv[drv_index];
  2134. disk = h->gendisk[drv_index];
  2135. /* make sure logical volume is NOT is use */
  2136. if (clear_all || (h->gendisk[0] == disk)) {
  2137. if (drv->usage_count > via_ioctl)
  2138. return -EBUSY;
  2139. } else if (drv->usage_count > 0)
  2140. return -EBUSY;
  2141. recalculate_highest_lun = (drv == h->drv[h->highest_lun]);
  2142. /* invalidate the devices and deregister the disk. If it is disk
  2143. * zero do not deregister it but just zero out it's values. This
  2144. * allows us to delete disk zero but keep the controller registered.
  2145. */
  2146. if (h->gendisk[0] != disk) {
  2147. struct request_queue *q = disk->queue;
  2148. if (disk->flags & GENHD_FL_UP) {
  2149. cciss_destroy_ld_sysfs_entry(h, drv_index, 0);
  2150. del_gendisk(disk);
  2151. }
  2152. if (q)
  2153. blk_cleanup_queue(q);
  2154. /* If clear_all is set then we are deleting the logical
  2155. * drive, not just refreshing its info. For drives
  2156. * other than disk 0 we will call put_disk. We do not
  2157. * do this for disk 0 as we need it to be able to
  2158. * configure the controller.
  2159. */
  2160. if (clear_all){
  2161. /* This isn't pretty, but we need to find the
  2162. * disk in our array and NULL our the pointer.
  2163. * This is so that we will call alloc_disk if
  2164. * this index is used again later.
  2165. */
  2166. for (i=0; i < CISS_MAX_LUN; i++){
  2167. if (h->gendisk[i] == disk) {
  2168. h->gendisk[i] = NULL;
  2169. break;
  2170. }
  2171. }
  2172. put_disk(disk);
  2173. }
  2174. } else {
  2175. set_capacity(disk, 0);
  2176. cciss_clear_drive_info(drv);
  2177. }
  2178. --h->num_luns;
  2179. /* if it was the last disk, find the new hightest lun */
  2180. if (clear_all && recalculate_highest_lun) {
  2181. int newhighest = -1;
  2182. for (i = 0; i <= h->highest_lun; i++) {
  2183. /* if the disk has size > 0, it is available */
  2184. if (h->drv[i] && h->drv[i]->heads)
  2185. newhighest = i;
  2186. }
  2187. h->highest_lun = newhighest;
  2188. }
  2189. return 0;
  2190. }
  2191. static int fill_cmd(ctlr_info_t *h, CommandList_struct *c, __u8 cmd, void *buff,
  2192. size_t size, __u8 page_code, unsigned char *scsi3addr,
  2193. int cmd_type)
  2194. {
  2195. u64bit buff_dma_handle;
  2196. int status = IO_OK;
  2197. c->cmd_type = CMD_IOCTL_PEND;
  2198. c->Header.ReplyQueue = 0;
  2199. if (buff != NULL) {
  2200. c->Header.SGList = 1;
  2201. c->Header.SGTotal = 1;
  2202. } else {
  2203. c->Header.SGList = 0;
  2204. c->Header.SGTotal = 0;
  2205. }
  2206. c->Header.Tag.lower = c->busaddr;
  2207. memcpy(c->Header.LUN.LunAddrBytes, scsi3addr, 8);
  2208. c->Request.Type.Type = cmd_type;
  2209. if (cmd_type == TYPE_CMD) {
  2210. switch (cmd) {
  2211. case CISS_INQUIRY:
  2212. /* are we trying to read a vital product page */
  2213. if (page_code != 0) {
  2214. c->Request.CDB[1] = 0x01;
  2215. c->Request.CDB[2] = page_code;
  2216. }
  2217. c->Request.CDBLen = 6;
  2218. c->Request.Type.Attribute = ATTR_SIMPLE;
  2219. c->Request.Type.Direction = XFER_READ;
  2220. c->Request.Timeout = 0;
  2221. c->Request.CDB[0] = CISS_INQUIRY;
  2222. c->Request.CDB[4] = size & 0xFF;
  2223. break;
  2224. case CISS_REPORT_LOG:
  2225. case CISS_REPORT_PHYS:
  2226. /* Talking to controller so It's a physical command
  2227. mode = 00 target = 0. Nothing to write.
  2228. */
  2229. c->Request.CDBLen = 12;
  2230. c->Request.Type.Attribute = ATTR_SIMPLE;
  2231. c->Request.Type.Direction = XFER_READ;
  2232. c->Request.Timeout = 0;
  2233. c->Request.CDB[0] = cmd;
  2234. c->Request.CDB[6] = (size >> 24) & 0xFF; /* MSB */
  2235. c->Request.CDB[7] = (size >> 16) & 0xFF;
  2236. c->Request.CDB[8] = (size >> 8) & 0xFF;
  2237. c->Request.CDB[9] = size & 0xFF;
  2238. break;
  2239. case CCISS_READ_CAPACITY:
  2240. c->Request.CDBLen = 10;
  2241. c->Request.Type.Attribute = ATTR_SIMPLE;
  2242. c->Request.Type.Direction = XFER_READ;
  2243. c->Request.Timeout = 0;
  2244. c->Request.CDB[0] = cmd;
  2245. break;
  2246. case CCISS_READ_CAPACITY_16:
  2247. c->Request.CDBLen = 16;
  2248. c->Request.Type.Attribute = ATTR_SIMPLE;
  2249. c->Request.Type.Direction = XFER_READ;
  2250. c->Request.Timeout = 0;
  2251. c->Request.CDB[0] = cmd;
  2252. c->Request.CDB[1] = 0x10;
  2253. c->Request.CDB[10] = (size >> 24) & 0xFF;
  2254. c->Request.CDB[11] = (size >> 16) & 0xFF;
  2255. c->Request.CDB[12] = (size >> 8) & 0xFF;
  2256. c->Request.CDB[13] = size & 0xFF;
  2257. c->Request.Timeout = 0;
  2258. c->Request.CDB[0] = cmd;
  2259. break;
  2260. case CCISS_CACHE_FLUSH:
  2261. c->Request.CDBLen = 12;
  2262. c->Request.Type.Attribute = ATTR_SIMPLE;
  2263. c->Request.Type.Direction = XFER_WRITE;
  2264. c->Request.Timeout = 0;
  2265. c->Request.CDB[0] = BMIC_WRITE;
  2266. c->Request.CDB[6] = BMIC_CACHE_FLUSH;
  2267. break;
  2268. case TEST_UNIT_READY:
  2269. c->Request.CDBLen = 6;
  2270. c->Request.Type.Attribute = ATTR_SIMPLE;
  2271. c->Request.Type.Direction = XFER_NONE;
  2272. c->Request.Timeout = 0;
  2273. break;
  2274. default:
  2275. dev_warn(&h->pdev->dev, "Unknown Command 0x%c\n", cmd);
  2276. return IO_ERROR;
  2277. }
  2278. } else if (cmd_type == TYPE_MSG) {
  2279. switch (cmd) {
  2280. case 0: /* ABORT message */
  2281. c->Request.CDBLen = 12;
  2282. c->Request.Type.Attribute = ATTR_SIMPLE;
  2283. c->Request.Type.Direction = XFER_WRITE;
  2284. c->Request.Timeout = 0;
  2285. c->Request.CDB[0] = cmd; /* abort */
  2286. c->Request.CDB[1] = 0; /* abort a command */
  2287. /* buff contains the tag of the command to abort */
  2288. memcpy(&c->Request.CDB[4], buff, 8);
  2289. break;
  2290. case 1: /* RESET message */
  2291. c->Request.CDBLen = 16;
  2292. c->Request.Type.Attribute = ATTR_SIMPLE;
  2293. c->Request.Type.Direction = XFER_NONE;
  2294. c->Request.Timeout = 0;
  2295. memset(&c->Request.CDB[0], 0, sizeof(c->Request.CDB));
  2296. c->Request.CDB[0] = cmd; /* reset */
  2297. c->Request.CDB[1] = 0x03; /* reset a target */
  2298. break;
  2299. case 3: /* No-Op message */
  2300. c->Request.CDBLen = 1;
  2301. c->Request.Type.Attribute = ATTR_SIMPLE;
  2302. c->Request.Type.Direction = XFER_WRITE;
  2303. c->Request.Timeout = 0;
  2304. c->Request.CDB[0] = cmd;
  2305. break;
  2306. default:
  2307. dev_warn(&h->pdev->dev,
  2308. "unknown message type %d\n", cmd);
  2309. return IO_ERROR;
  2310. }
  2311. } else {
  2312. dev_warn(&h->pdev->dev, "unknown command type %d\n", cmd_type);
  2313. return IO_ERROR;
  2314. }
  2315. /* Fill in the scatter gather information */
  2316. if (size > 0) {
  2317. buff_dma_handle.val = (__u64) pci_map_single(h->pdev,
  2318. buff, size,
  2319. PCI_DMA_BIDIRECTIONAL);
  2320. c->SG[0].Addr.lower = buff_dma_handle.val32.lower;
  2321. c->SG[0].Addr.upper = buff_dma_handle.val32.upper;
  2322. c->SG[0].Len = size;
  2323. c->SG[0].Ext = 0; /* we are not chaining */
  2324. }
  2325. return status;
  2326. }
  2327. static int check_target_status(ctlr_info_t *h, CommandList_struct *c)
  2328. {
  2329. switch (c->err_info->ScsiStatus) {
  2330. case SAM_STAT_GOOD:
  2331. return IO_OK;
  2332. case SAM_STAT_CHECK_CONDITION:
  2333. switch (0xf & c->err_info->SenseInfo[2]) {
  2334. case 0: return IO_OK; /* no sense */
  2335. case 1: return IO_OK; /* recovered error */
  2336. default:
  2337. if (check_for_unit_attention(h, c))
  2338. return IO_NEEDS_RETRY;
  2339. dev_warn(&h->pdev->dev, "cmd 0x%02x "
  2340. "check condition, sense key = 0x%02x\n",
  2341. c->Request.CDB[0], c->err_info->SenseInfo[2]);
  2342. }
  2343. break;
  2344. default:
  2345. dev_warn(&h->pdev->dev, "cmd 0x%02x"
  2346. "scsi status = 0x%02x\n",
  2347. c->Request.CDB[0], c->err_info->ScsiStatus);
  2348. break;
  2349. }
  2350. return IO_ERROR;
  2351. }
  2352. static int process_sendcmd_error(ctlr_info_t *h, CommandList_struct *c)
  2353. {
  2354. int return_status = IO_OK;
  2355. if (c->err_info->CommandStatus == CMD_SUCCESS)
  2356. return IO_OK;
  2357. switch (c->err_info->CommandStatus) {
  2358. case CMD_TARGET_STATUS:
  2359. return_status = check_target_status(h, c);
  2360. break;
  2361. case CMD_DATA_UNDERRUN:
  2362. case CMD_DATA_OVERRUN:
  2363. /* expected for inquiry and report lun commands */
  2364. break;
  2365. case CMD_INVALID:
  2366. dev_warn(&h->pdev->dev, "cmd 0x%02x is "
  2367. "reported invalid\n", c->Request.CDB[0]);
  2368. return_status = IO_ERROR;
  2369. break;
  2370. case CMD_PROTOCOL_ERR:
  2371. dev_warn(&h->pdev->dev, "cmd 0x%02x has "
  2372. "protocol error\n", c->Request.CDB[0]);
  2373. return_status = IO_ERROR;
  2374. break;
  2375. case CMD_HARDWARE_ERR:
  2376. dev_warn(&h->pdev->dev, "cmd 0x%02x had "
  2377. " hardware error\n", c->Request.CDB[0]);
  2378. return_status = IO_ERROR;
  2379. break;
  2380. case CMD_CONNECTION_LOST:
  2381. dev_warn(&h->pdev->dev, "cmd 0x%02x had "
  2382. "connection lost\n", c->Request.CDB[0]);
  2383. return_status = IO_ERROR;
  2384. break;
  2385. case CMD_ABORTED:
  2386. dev_warn(&h->pdev->dev, "cmd 0x%02x was "
  2387. "aborted\n", c->Request.CDB[0]);
  2388. return_status = IO_ERROR;
  2389. break;
  2390. case CMD_ABORT_FAILED:
  2391. dev_warn(&h->pdev->dev, "cmd 0x%02x reports "
  2392. "abort failed\n", c->Request.CDB[0]);
  2393. return_status = IO_ERROR;
  2394. break;
  2395. case CMD_UNSOLICITED_ABORT:
  2396. dev_warn(&h->pdev->dev, "unsolicited abort 0x%02x\n",
  2397. c->Request.CDB[0]);
  2398. return_status = IO_NEEDS_RETRY;
  2399. break;
  2400. default:
  2401. dev_warn(&h->pdev->dev, "cmd 0x%02x returned "
  2402. "unknown status %x\n", c->Request.CDB[0],
  2403. c->err_info->CommandStatus);
  2404. return_status = IO_ERROR;
  2405. }
  2406. return return_status;
  2407. }
  2408. static int sendcmd_withirq_core(ctlr_info_t *h, CommandList_struct *c,
  2409. int attempt_retry)
  2410. {
  2411. DECLARE_COMPLETION_ONSTACK(wait);
  2412. u64bit buff_dma_handle;
  2413. int return_status = IO_OK;
  2414. resend_cmd2:
  2415. c->waiting = &wait;
  2416. enqueue_cmd_and_start_io(h, c);
  2417. wait_for_completion(&wait);
  2418. if (c->err_info->CommandStatus == 0 || !attempt_retry)
  2419. goto command_done;
  2420. return_status = process_sendcmd_error(h, c);
  2421. if (return_status == IO_NEEDS_RETRY &&
  2422. c->retry_count < MAX_CMD_RETRIES) {
  2423. dev_warn(&h->pdev->dev, "retrying 0x%02x\n",
  2424. c->Request.CDB[0]);
  2425. c->retry_count++;
  2426. /* erase the old error information */
  2427. memset(c->err_info, 0, sizeof(ErrorInfo_struct));
  2428. return_status = IO_OK;
  2429. INIT_COMPLETION(wait);
  2430. goto resend_cmd2;
  2431. }
  2432. command_done:
  2433. /* unlock the buffers from DMA */
  2434. buff_dma_handle.val32.lower = c->SG[0].Addr.lower;
  2435. buff_dma_handle.val32.upper = c->SG[0].Addr.upper;
  2436. pci_unmap_single(h->pdev, (dma_addr_t) buff_dma_handle.val,
  2437. c->SG[0].Len, PCI_DMA_BIDIRECTIONAL);
  2438. return return_status;
  2439. }
  2440. static int sendcmd_withirq(ctlr_info_t *h, __u8 cmd, void *buff, size_t size,
  2441. __u8 page_code, unsigned char scsi3addr[],
  2442. int cmd_type)
  2443. {
  2444. CommandList_struct *c;
  2445. int return_status;
  2446. c = cmd_special_alloc(h);
  2447. if (!c)
  2448. return -ENOMEM;
  2449. return_status = fill_cmd(h, c, cmd, buff, size, page_code,
  2450. scsi3addr, cmd_type);
  2451. if (return_status == IO_OK)
  2452. return_status = sendcmd_withirq_core(h, c, 1);
  2453. cmd_special_free(h, c);
  2454. return return_status;
  2455. }
  2456. static void cciss_geometry_inquiry(ctlr_info_t *h, int logvol,
  2457. sector_t total_size,
  2458. unsigned int block_size,
  2459. InquiryData_struct *inq_buff,
  2460. drive_info_struct *drv)
  2461. {
  2462. int return_code;
  2463. unsigned long t;
  2464. unsigned char scsi3addr[8];
  2465. memset(inq_buff, 0, sizeof(InquiryData_struct));
  2466. log_unit_to_scsi3addr(h, scsi3addr, logvol);
  2467. return_code = sendcmd_withirq(h, CISS_INQUIRY, inq_buff,
  2468. sizeof(*inq_buff), 0xC1, scsi3addr, TYPE_CMD);
  2469. if (return_code == IO_OK) {
  2470. if (inq_buff->data_byte[8] == 0xFF) {
  2471. dev_warn(&h->pdev->dev,
  2472. "reading geometry failed, volume "
  2473. "does not support reading geometry\n");
  2474. drv->heads = 255;
  2475. drv->sectors = 32; /* Sectors per track */
  2476. drv->cylinders = total_size + 1;
  2477. drv->raid_level = RAID_UNKNOWN;
  2478. } else {
  2479. drv->heads = inq_buff->data_byte[6];
  2480. drv->sectors = inq_buff->data_byte[7];
  2481. drv->cylinders = (inq_buff->data_byte[4] & 0xff) << 8;
  2482. drv->cylinders += inq_buff->data_byte[5];
  2483. drv->raid_level = inq_buff->data_byte[8];
  2484. }
  2485. drv->block_size = block_size;
  2486. drv->nr_blocks = total_size + 1;
  2487. t = drv->heads * drv->sectors;
  2488. if (t > 1) {
  2489. sector_t real_size = total_size + 1;
  2490. unsigned long rem = sector_div(real_size, t);
  2491. if (rem)
  2492. real_size++;
  2493. drv->cylinders = real_size;
  2494. }
  2495. } else { /* Get geometry failed */
  2496. dev_warn(&h->pdev->dev, "reading geometry failed\n");
  2497. }
  2498. }
  2499. static void
  2500. cciss_read_capacity(ctlr_info_t *h, int logvol, sector_t *total_size,
  2501. unsigned int *block_size)
  2502. {
  2503. ReadCapdata_struct *buf;
  2504. int return_code;
  2505. unsigned char scsi3addr[8];
  2506. buf = kzalloc(sizeof(ReadCapdata_struct), GFP_KERNEL);
  2507. if (!buf) {
  2508. dev_warn(&h->pdev->dev, "out of memory\n");
  2509. return;
  2510. }
  2511. log_unit_to_scsi3addr(h, scsi3addr, logvol);
  2512. return_code = sendcmd_withirq(h, CCISS_READ_CAPACITY, buf,
  2513. sizeof(ReadCapdata_struct), 0, scsi3addr, TYPE_CMD);
  2514. if (return_code == IO_OK) {
  2515. *total_size = be32_to_cpu(*(__be32 *) buf->total_size);
  2516. *block_size = be32_to_cpu(*(__be32 *) buf->block_size);
  2517. } else { /* read capacity command failed */
  2518. dev_warn(&h->pdev->dev, "read capacity failed\n");
  2519. *total_size = 0;
  2520. *block_size = BLOCK_SIZE;
  2521. }
  2522. kfree(buf);
  2523. }
  2524. static void cciss_read_capacity_16(ctlr_info_t *h, int logvol,
  2525. sector_t *total_size, unsigned int *block_size)
  2526. {
  2527. ReadCapdata_struct_16 *buf;
  2528. int return_code;
  2529. unsigned char scsi3addr[8];
  2530. buf = kzalloc(sizeof(ReadCapdata_struct_16), GFP_KERNEL);
  2531. if (!buf) {
  2532. dev_warn(&h->pdev->dev, "out of memory\n");
  2533. return;
  2534. }
  2535. log_unit_to_scsi3addr(h, scsi3addr, logvol);
  2536. return_code = sendcmd_withirq(h, CCISS_READ_CAPACITY_16,
  2537. buf, sizeof(ReadCapdata_struct_16),
  2538. 0, scsi3addr, TYPE_CMD);
  2539. if (return_code == IO_OK) {
  2540. *total_size = be64_to_cpu(*(__be64 *) buf->total_size);
  2541. *block_size = be32_to_cpu(*(__be32 *) buf->block_size);
  2542. } else { /* read capacity command failed */
  2543. dev_warn(&h->pdev->dev, "read capacity failed\n");
  2544. *total_size = 0;
  2545. *block_size = BLOCK_SIZE;
  2546. }
  2547. dev_info(&h->pdev->dev, " blocks= %llu block_size= %d\n",
  2548. (unsigned long long)*total_size+1, *block_size);
  2549. kfree(buf);
  2550. }
  2551. static int cciss_revalidate(struct gendisk *disk)
  2552. {
  2553. ctlr_info_t *h = get_host(disk);
  2554. drive_info_struct *drv = get_drv(disk);
  2555. int logvol;
  2556. int FOUND = 0;
  2557. unsigned int block_size;
  2558. sector_t total_size;
  2559. InquiryData_struct *inq_buff = NULL;
  2560. for (logvol = 0; logvol < CISS_MAX_LUN; logvol++) {
  2561. if (memcmp(h->drv[logvol]->LunID, drv->LunID,
  2562. sizeof(drv->LunID)) == 0) {
  2563. FOUND = 1;
  2564. break;
  2565. }
  2566. }
  2567. if (!FOUND)
  2568. return 1;
  2569. inq_buff = kmalloc(sizeof(InquiryData_struct), GFP_KERNEL);
  2570. if (inq_buff == NULL) {
  2571. dev_warn(&h->pdev->dev, "out of memory\n");
  2572. return 1;
  2573. }
  2574. if (h->cciss_read == CCISS_READ_10) {
  2575. cciss_read_capacity(h, logvol,
  2576. &total_size, &block_size);
  2577. } else {
  2578. cciss_read_capacity_16(h, logvol,
  2579. &total_size, &block_size);
  2580. }
  2581. cciss_geometry_inquiry(h, logvol, total_size, block_size,
  2582. inq_buff, drv);
  2583. blk_queue_logical_block_size(drv->queue, drv->block_size);
  2584. set_capacity(disk, drv->nr_blocks);
  2585. kfree(inq_buff);
  2586. return 0;
  2587. }
  2588. /*
  2589. * Map (physical) PCI mem into (virtual) kernel space
  2590. */
  2591. static void __iomem *remap_pci_mem(ulong base, ulong size)
  2592. {
  2593. ulong page_base = ((ulong) base) & PAGE_MASK;
  2594. ulong page_offs = ((ulong) base) - page_base;
  2595. void __iomem *page_remapped = ioremap(page_base, page_offs + size);
  2596. return page_remapped ? (page_remapped + page_offs) : NULL;
  2597. }
  2598. /*
  2599. * Takes jobs of the Q and sends them to the hardware, then puts it on
  2600. * the Q to wait for completion.
  2601. */
  2602. static void start_io(ctlr_info_t *h)
  2603. {
  2604. CommandList_struct *c;
  2605. while (!hlist_empty(&h->reqQ)) {
  2606. c = hlist_entry(h->reqQ.first, CommandList_struct, list);
  2607. /* can't do anything if fifo is full */
  2608. if ((h->access.fifo_full(h))) {
  2609. dev_warn(&h->pdev->dev, "fifo full\n");
  2610. break;
  2611. }
  2612. /* Get the first entry from the Request Q */
  2613. removeQ(c);
  2614. h->Qdepth--;
  2615. /* Tell the controller execute command */
  2616. h->access.submit_command(h, c);
  2617. /* Put job onto the completed Q */
  2618. addQ(&h->cmpQ, c);
  2619. }
  2620. }
  2621. /* Assumes that h->lock is held. */
  2622. /* Zeros out the error record and then resends the command back */
  2623. /* to the controller */
  2624. static inline void resend_cciss_cmd(ctlr_info_t *h, CommandList_struct *c)
  2625. {
  2626. /* erase the old error information */
  2627. memset(c->err_info, 0, sizeof(ErrorInfo_struct));
  2628. /* add it to software queue and then send it to the controller */
  2629. addQ(&h->reqQ, c);
  2630. h->Qdepth++;
  2631. if (h->Qdepth > h->maxQsinceinit)
  2632. h->maxQsinceinit = h->Qdepth;
  2633. start_io(h);
  2634. }
  2635. static inline unsigned int make_status_bytes(unsigned int scsi_status_byte,
  2636. unsigned int msg_byte, unsigned int host_byte,
  2637. unsigned int driver_byte)
  2638. {
  2639. /* inverse of macros in scsi.h */
  2640. return (scsi_status_byte & 0xff) |
  2641. ((msg_byte & 0xff) << 8) |
  2642. ((host_byte & 0xff) << 16) |
  2643. ((driver_byte & 0xff) << 24);
  2644. }
  2645. static inline int evaluate_target_status(ctlr_info_t *h,
  2646. CommandList_struct *cmd, int *retry_cmd)
  2647. {
  2648. unsigned char sense_key;
  2649. unsigned char status_byte, msg_byte, host_byte, driver_byte;
  2650. int error_value;
  2651. *retry_cmd = 0;
  2652. /* If we get in here, it means we got "target status", that is, scsi status */
  2653. status_byte = cmd->err_info->ScsiStatus;
  2654. driver_byte = DRIVER_OK;
  2655. msg_byte = cmd->err_info->CommandStatus; /* correct? seems too device specific */
  2656. if (cmd->rq->cmd_type == REQ_TYPE_BLOCK_PC)
  2657. host_byte = DID_PASSTHROUGH;
  2658. else
  2659. host_byte = DID_OK;
  2660. error_value = make_status_bytes(status_byte, msg_byte,
  2661. host_byte, driver_byte);
  2662. if (cmd->err_info->ScsiStatus != SAM_STAT_CHECK_CONDITION) {
  2663. if (cmd->rq->cmd_type != REQ_TYPE_BLOCK_PC)
  2664. dev_warn(&h->pdev->dev, "cmd %p "
  2665. "has SCSI Status 0x%x\n",
  2666. cmd, cmd->err_info->ScsiStatus);
  2667. return error_value;
  2668. }
  2669. /* check the sense key */
  2670. sense_key = 0xf & cmd->err_info->SenseInfo[2];
  2671. /* no status or recovered error */
  2672. if (((sense_key == 0x0) || (sense_key == 0x1)) &&
  2673. (cmd->rq->cmd_type != REQ_TYPE_BLOCK_PC))
  2674. error_value = 0;
  2675. if (check_for_unit_attention(h, cmd)) {
  2676. *retry_cmd = !(cmd->rq->cmd_type == REQ_TYPE_BLOCK_PC);
  2677. return 0;
  2678. }
  2679. /* Not SG_IO or similar? */
  2680. if (cmd->rq->cmd_type != REQ_TYPE_BLOCK_PC) {
  2681. if (error_value != 0)
  2682. dev_warn(&h->pdev->dev, "cmd %p has CHECK CONDITION"
  2683. " sense key = 0x%x\n", cmd, sense_key);
  2684. return error_value;
  2685. }
  2686. /* SG_IO or similar, copy sense data back */
  2687. if (cmd->rq->sense) {
  2688. if (cmd->rq->sense_len > cmd->err_info->SenseLen)
  2689. cmd->rq->sense_len = cmd->err_info->SenseLen;
  2690. memcpy(cmd->rq->sense, cmd->err_info->SenseInfo,
  2691. cmd->rq->sense_len);
  2692. } else
  2693. cmd->rq->sense_len = 0;
  2694. return error_value;
  2695. }
  2696. /* checks the status of the job and calls complete buffers to mark all
  2697. * buffers for the completed job. Note that this function does not need
  2698. * to hold the hba/queue lock.
  2699. */
  2700. static inline void complete_command(ctlr_info_t *h, CommandList_struct *cmd,
  2701. int timeout)
  2702. {
  2703. int retry_cmd = 0;
  2704. struct request *rq = cmd->rq;
  2705. rq->errors = 0;
  2706. if (timeout)
  2707. rq->errors = make_status_bytes(0, 0, 0, DRIVER_TIMEOUT);
  2708. if (cmd->err_info->CommandStatus == 0) /* no error has occurred */
  2709. goto after_error_processing;
  2710. switch (cmd->err_info->CommandStatus) {
  2711. case CMD_TARGET_STATUS:
  2712. rq->errors = evaluate_target_status(h, cmd, &retry_cmd);
  2713. break;
  2714. case CMD_DATA_UNDERRUN:
  2715. if (cmd->rq->cmd_type == REQ_TYPE_FS) {
  2716. dev_warn(&h->pdev->dev, "cmd %p has"
  2717. " completed with data underrun "
  2718. "reported\n", cmd);
  2719. cmd->rq->resid_len = cmd->err_info->ResidualCnt;
  2720. }
  2721. break;
  2722. case CMD_DATA_OVERRUN:
  2723. if (cmd->rq->cmd_type == REQ_TYPE_FS)
  2724. dev_warn(&h->pdev->dev, "cciss: cmd %p has"
  2725. " completed with data overrun "
  2726. "reported\n", cmd);
  2727. break;
  2728. case CMD_INVALID:
  2729. dev_warn(&h->pdev->dev, "cciss: cmd %p is "
  2730. "reported invalid\n", cmd);
  2731. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2732. cmd->err_info->CommandStatus, DRIVER_OK,
  2733. (cmd->rq->cmd_type == REQ_TYPE_BLOCK_PC) ?
  2734. DID_PASSTHROUGH : DID_ERROR);
  2735. break;
  2736. case CMD_PROTOCOL_ERR:
  2737. dev_warn(&h->pdev->dev, "cciss: cmd %p has "
  2738. "protocol error\n", cmd);
  2739. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2740. cmd->err_info->CommandStatus, DRIVER_OK,
  2741. (cmd->rq->cmd_type == REQ_TYPE_BLOCK_PC) ?
  2742. DID_PASSTHROUGH : DID_ERROR);
  2743. break;
  2744. case CMD_HARDWARE_ERR:
  2745. dev_warn(&h->pdev->dev, "cciss: cmd %p had "
  2746. " hardware error\n", cmd);
  2747. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2748. cmd->err_info->CommandStatus, DRIVER_OK,
  2749. (cmd->rq->cmd_type == REQ_TYPE_BLOCK_PC) ?
  2750. DID_PASSTHROUGH : DID_ERROR);
  2751. break;
  2752. case CMD_CONNECTION_LOST:
  2753. dev_warn(&h->pdev->dev, "cciss: cmd %p had "
  2754. "connection lost\n", cmd);
  2755. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2756. cmd->err_info->CommandStatus, DRIVER_OK,
  2757. (cmd->rq->cmd_type == REQ_TYPE_BLOCK_PC) ?
  2758. DID_PASSTHROUGH : DID_ERROR);
  2759. break;
  2760. case CMD_ABORTED:
  2761. dev_warn(&h->pdev->dev, "cciss: cmd %p was "
  2762. "aborted\n", cmd);
  2763. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2764. cmd->err_info->CommandStatus, DRIVER_OK,
  2765. (cmd->rq->cmd_type == REQ_TYPE_BLOCK_PC) ?
  2766. DID_PASSTHROUGH : DID_ABORT);
  2767. break;
  2768. case CMD_ABORT_FAILED:
  2769. dev_warn(&h->pdev->dev, "cciss: cmd %p reports "
  2770. "abort failed\n", cmd);
  2771. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2772. cmd->err_info->CommandStatus, DRIVER_OK,
  2773. (cmd->rq->cmd_type == REQ_TYPE_BLOCK_PC) ?
  2774. DID_PASSTHROUGH : DID_ERROR);
  2775. break;
  2776. case CMD_UNSOLICITED_ABORT:
  2777. dev_warn(&h->pdev->dev, "cciss%d: unsolicited "
  2778. "abort %p\n", h->ctlr, cmd);
  2779. if (cmd->retry_count < MAX_CMD_RETRIES) {
  2780. retry_cmd = 1;
  2781. dev_warn(&h->pdev->dev, "retrying %p\n", cmd);
  2782. cmd->retry_count++;
  2783. } else
  2784. dev_warn(&h->pdev->dev,
  2785. "%p retried too many times\n", cmd);
  2786. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2787. cmd->err_info->CommandStatus, DRIVER_OK,
  2788. (cmd->rq->cmd_type == REQ_TYPE_BLOCK_PC) ?
  2789. DID_PASSTHROUGH : DID_ABORT);
  2790. break;
  2791. case CMD_TIMEOUT:
  2792. dev_warn(&h->pdev->dev, "cmd %p timedout\n", cmd);
  2793. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2794. cmd->err_info->CommandStatus, DRIVER_OK,
  2795. (cmd->rq->cmd_type == REQ_TYPE_BLOCK_PC) ?
  2796. DID_PASSTHROUGH : DID_ERROR);
  2797. break;
  2798. default:
  2799. dev_warn(&h->pdev->dev, "cmd %p returned "
  2800. "unknown status %x\n", cmd,
  2801. cmd->err_info->CommandStatus);
  2802. rq->errors = make_status_bytes(SAM_STAT_GOOD,
  2803. cmd->err_info->CommandStatus, DRIVER_OK,
  2804. (cmd->rq->cmd_type == REQ_TYPE_BLOCK_PC) ?
  2805. DID_PASSTHROUGH : DID_ERROR);
  2806. }
  2807. after_error_processing:
  2808. /* We need to return this command */
  2809. if (retry_cmd) {
  2810. resend_cciss_cmd(h, cmd);
  2811. return;
  2812. }
  2813. cmd->rq->completion_data = cmd;
  2814. blk_complete_request(cmd->rq);
  2815. }
  2816. static inline u32 cciss_tag_contains_index(u32 tag)
  2817. {
  2818. #define DIRECT_LOOKUP_BIT 0x10
  2819. return tag & DIRECT_LOOKUP_BIT;
  2820. }
  2821. static inline u32 cciss_tag_to_index(u32 tag)
  2822. {
  2823. #define DIRECT_LOOKUP_SHIFT 5
  2824. return tag >> DIRECT_LOOKUP_SHIFT;
  2825. }
  2826. static inline u32 cciss_tag_discard_error_bits(u32 tag)
  2827. {
  2828. #define CCISS_ERROR_BITS 0x03
  2829. return tag & ~CCISS_ERROR_BITS;
  2830. }
  2831. static inline void cciss_mark_tag_indexed(u32 *tag)
  2832. {
  2833. *tag |= DIRECT_LOOKUP_BIT;
  2834. }
  2835. static inline void cciss_set_tag_index(u32 *tag, u32 index)
  2836. {
  2837. *tag |= (index << DIRECT_LOOKUP_SHIFT);
  2838. }
  2839. /*
  2840. * Get a request and submit it to the controller.
  2841. */
  2842. static void do_cciss_request(struct request_queue *q)
  2843. {
  2844. ctlr_info_t *h = q->queuedata;
  2845. CommandList_struct *c;
  2846. sector_t start_blk;
  2847. int seg;
  2848. struct request *creq;
  2849. u64bit temp64;
  2850. struct scatterlist *tmp_sg;
  2851. SGDescriptor_struct *curr_sg;
  2852. drive_info_struct *drv;
  2853. int i, dir;
  2854. int sg_index = 0;
  2855. int chained = 0;
  2856. /* We call start_io here in case there is a command waiting on the
  2857. * queue that has not been sent.
  2858. */
  2859. if (blk_queue_plugged(q))
  2860. goto startio;
  2861. queue:
  2862. creq = blk_peek_request(q);
  2863. if (!creq)
  2864. goto startio;
  2865. BUG_ON(creq->nr_phys_segments > h->maxsgentries);
  2866. c = cmd_alloc(h);
  2867. if (!c)
  2868. goto full;
  2869. blk_start_request(creq);
  2870. tmp_sg = h->scatter_list[c->cmdindex];
  2871. spin_unlock_irq(q->queue_lock);
  2872. c->cmd_type = CMD_RWREQ;
  2873. c->rq = creq;
  2874. /* fill in the request */
  2875. drv = creq->rq_disk->private_data;
  2876. c->Header.ReplyQueue = 0; /* unused in simple mode */
  2877. /* got command from pool, so use the command block index instead */
  2878. /* for direct lookups. */
  2879. /* The first 2 bits are reserved for controller error reporting. */
  2880. cciss_set_tag_index(&c->Header.Tag.lower, c->cmdindex);
  2881. cciss_mark_tag_indexed(&c->Header.Tag.lower);
  2882. memcpy(&c->Header.LUN, drv->LunID, sizeof(drv->LunID));
  2883. c->Request.CDBLen = 10; /* 12 byte commands not in FW yet; */
  2884. c->Request.Type.Type = TYPE_CMD; /* It is a command. */
  2885. c->Request.Type.Attribute = ATTR_SIMPLE;
  2886. c->Request.Type.Direction =
  2887. (rq_data_dir(creq) == READ) ? XFER_READ : XFER_WRITE;
  2888. c->Request.Timeout = 0; /* Don't time out */
  2889. c->Request.CDB[0] =
  2890. (rq_data_dir(creq) == READ) ? h->cciss_read : h->cciss_write;
  2891. start_blk = blk_rq_pos(creq);
  2892. dev_dbg(&h->pdev->dev, "sector =%d nr_sectors=%d\n",
  2893. (int)blk_rq_pos(creq), (int)blk_rq_sectors(creq));
  2894. sg_init_table(tmp_sg, h->maxsgentries);
  2895. seg = blk_rq_map_sg(q, creq, tmp_sg);
  2896. /* get the DMA records for the setup */
  2897. if (c->Request.Type.Direction == XFER_READ)
  2898. dir = PCI_DMA_FROMDEVICE;
  2899. else
  2900. dir = PCI_DMA_TODEVICE;
  2901. curr_sg = c->SG;
  2902. sg_index = 0;
  2903. chained = 0;
  2904. for (i = 0; i < seg; i++) {
  2905. if (((sg_index+1) == (h->max_cmd_sgentries)) &&
  2906. !chained && ((seg - i) > 1)) {
  2907. /* Point to next chain block. */
  2908. curr_sg = h->cmd_sg_list[c->cmdindex];
  2909. sg_index = 0;
  2910. chained = 1;
  2911. }
  2912. curr_sg[sg_index].Len = tmp_sg[i].length;
  2913. temp64.val = (__u64) pci_map_page(h->pdev, sg_page(&tmp_sg[i]),
  2914. tmp_sg[i].offset,
  2915. tmp_sg[i].length, dir);
  2916. curr_sg[sg_index].Addr.lower = temp64.val32.lower;
  2917. curr_sg[sg_index].Addr.upper = temp64.val32.upper;
  2918. curr_sg[sg_index].Ext = 0; /* we are not chaining */
  2919. ++sg_index;
  2920. }
  2921. if (chained)
  2922. cciss_map_sg_chain_block(h, c, h->cmd_sg_list[c->cmdindex],
  2923. (seg - (h->max_cmd_sgentries - 1)) *
  2924. sizeof(SGDescriptor_struct));
  2925. /* track how many SG entries we are using */
  2926. if (seg > h->maxSG)
  2927. h->maxSG = seg;
  2928. dev_dbg(&h->pdev->dev, "Submitting %u sectors in %d segments "
  2929. "chained[%d]\n",
  2930. blk_rq_sectors(creq), seg, chained);
  2931. c->Header.SGTotal = seg + chained;
  2932. if (seg <= h->max_cmd_sgentries)
  2933. c->Header.SGList = c->Header.SGTotal;
  2934. else
  2935. c->Header.SGList = h->max_cmd_sgentries;
  2936. set_performant_mode(h, c);
  2937. if (likely(creq->cmd_type == REQ_TYPE_FS)) {
  2938. if(h->cciss_read == CCISS_READ_10) {
  2939. c->Request.CDB[1] = 0;
  2940. c->Request.CDB[2] = (start_blk >> 24) & 0xff; /* MSB */
  2941. c->Request.CDB[3] = (start_blk >> 16) & 0xff;
  2942. c->Request.CDB[4] = (start_blk >> 8) & 0xff;
  2943. c->Request.CDB[5] = start_blk & 0xff;
  2944. c->Request.CDB[6] = 0; /* (sect >> 24) & 0xff; MSB */
  2945. c->Request.CDB[7] = (blk_rq_sectors(creq) >> 8) & 0xff;
  2946. c->Request.CDB[8] = blk_rq_sectors(creq) & 0xff;
  2947. c->Request.CDB[9] = c->Request.CDB[11] = c->Request.CDB[12] = 0;
  2948. } else {
  2949. u32 upper32 = upper_32_bits(start_blk);
  2950. c->Request.CDBLen = 16;
  2951. c->Request.CDB[1]= 0;
  2952. c->Request.CDB[2]= (upper32 >> 24) & 0xff; /* MSB */
  2953. c->Request.CDB[3]= (upper32 >> 16) & 0xff;
  2954. c->Request.CDB[4]= (upper32 >> 8) & 0xff;
  2955. c->Request.CDB[5]= upper32 & 0xff;
  2956. c->Request.CDB[6]= (start_blk >> 24) & 0xff;
  2957. c->Request.CDB[7]= (start_blk >> 16) & 0xff;
  2958. c->Request.CDB[8]= (start_blk >> 8) & 0xff;
  2959. c->Request.CDB[9]= start_blk & 0xff;
  2960. c->Request.CDB[10]= (blk_rq_sectors(creq) >> 24) & 0xff;
  2961. c->Request.CDB[11]= (blk_rq_sectors(creq) >> 16) & 0xff;
  2962. c->Request.CDB[12]= (blk_rq_sectors(creq) >> 8) & 0xff;
  2963. c->Request.CDB[13]= blk_rq_sectors(creq) & 0xff;
  2964. c->Request.CDB[14] = c->Request.CDB[15] = 0;
  2965. }
  2966. } else if (creq->cmd_type == REQ_TYPE_BLOCK_PC) {
  2967. c->Request.CDBLen = creq->cmd_len;
  2968. memcpy(c->Request.CDB, creq->cmd, BLK_MAX_CDB);
  2969. } else {
  2970. dev_warn(&h->pdev->dev, "bad request type %d\n",
  2971. creq->cmd_type);
  2972. BUG();
  2973. }
  2974. spin_lock_irq(q->queue_lock);
  2975. addQ(&h->reqQ, c);
  2976. h->Qdepth++;
  2977. if (h->Qdepth > h->maxQsinceinit)
  2978. h->maxQsinceinit = h->Qdepth;
  2979. goto queue;
  2980. full:
  2981. blk_stop_queue(q);
  2982. startio:
  2983. /* We will already have the driver lock here so not need
  2984. * to lock it.
  2985. */
  2986. start_io(h);
  2987. }
  2988. static inline unsigned long get_next_completion(ctlr_info_t *h)
  2989. {
  2990. return h->access.command_completed(h);
  2991. }
  2992. static inline int interrupt_pending(ctlr_info_t *h)
  2993. {
  2994. return h->access.intr_pending(h);
  2995. }
  2996. static inline long interrupt_not_for_us(ctlr_info_t *h)
  2997. {
  2998. return ((h->access.intr_pending(h) == 0) ||
  2999. (h->interrupts_enabled == 0));
  3000. }
  3001. static inline int bad_tag(ctlr_info_t *h, u32 tag_index,
  3002. u32 raw_tag)
  3003. {
  3004. if (unlikely(tag_index >= h->nr_cmds)) {
  3005. dev_warn(&h->pdev->dev, "bad tag 0x%08x ignored.\n", raw_tag);
  3006. return 1;
  3007. }
  3008. return 0;
  3009. }
  3010. static inline void finish_cmd(ctlr_info_t *h, CommandList_struct *c,
  3011. u32 raw_tag)
  3012. {
  3013. removeQ(c);
  3014. if (likely(c->cmd_type == CMD_RWREQ))
  3015. complete_command(h, c, 0);
  3016. else if (c->cmd_type == CMD_IOCTL_PEND)
  3017. complete(c->waiting);
  3018. #ifdef CONFIG_CISS_SCSI_TAPE
  3019. else if (c->cmd_type == CMD_SCSI)
  3020. complete_scsi_command(c, 0, raw_tag);
  3021. #endif
  3022. }
  3023. static inline u32 next_command(ctlr_info_t *h)
  3024. {
  3025. u32 a;
  3026. if (unlikely(h->transMethod != CFGTBL_Trans_Performant))
  3027. return h->access.command_completed(h);
  3028. if ((*(h->reply_pool_head) & 1) == (h->reply_pool_wraparound)) {
  3029. a = *(h->reply_pool_head); /* Next cmd in ring buffer */
  3030. (h->reply_pool_head)++;
  3031. h->commands_outstanding--;
  3032. } else {
  3033. a = FIFO_EMPTY;
  3034. }
  3035. /* Check for wraparound */
  3036. if (h->reply_pool_head == (h->reply_pool + h->max_commands)) {
  3037. h->reply_pool_head = h->reply_pool;
  3038. h->reply_pool_wraparound ^= 1;
  3039. }
  3040. return a;
  3041. }
  3042. /* process completion of an indexed ("direct lookup") command */
  3043. static inline u32 process_indexed_cmd(ctlr_info_t *h, u32 raw_tag)
  3044. {
  3045. u32 tag_index;
  3046. CommandList_struct *c;
  3047. tag_index = cciss_tag_to_index(raw_tag);
  3048. if (bad_tag(h, tag_index, raw_tag))
  3049. return next_command(h);
  3050. c = h->cmd_pool + tag_index;
  3051. finish_cmd(h, c, raw_tag);
  3052. return next_command(h);
  3053. }
  3054. /* process completion of a non-indexed command */
  3055. static inline u32 process_nonindexed_cmd(ctlr_info_t *h, u32 raw_tag)
  3056. {
  3057. u32 tag;
  3058. CommandList_struct *c = NULL;
  3059. struct hlist_node *tmp;
  3060. __u32 busaddr_masked, tag_masked;
  3061. tag = cciss_tag_discard_error_bits(raw_tag);
  3062. hlist_for_each_entry(c, tmp, &h->cmpQ, list) {
  3063. busaddr_masked = cciss_tag_discard_error_bits(c->busaddr);
  3064. tag_masked = cciss_tag_discard_error_bits(tag);
  3065. if (busaddr_masked == tag_masked) {
  3066. finish_cmd(h, c, raw_tag);
  3067. return next_command(h);
  3068. }
  3069. }
  3070. bad_tag(h, h->nr_cmds + 1, raw_tag);
  3071. return next_command(h);
  3072. }
  3073. static irqreturn_t do_cciss_intx(int irq, void *dev_id)
  3074. {
  3075. ctlr_info_t *h = dev_id;
  3076. unsigned long flags;
  3077. u32 raw_tag;
  3078. if (interrupt_not_for_us(h))
  3079. return IRQ_NONE;
  3080. spin_lock_irqsave(&h->lock, flags);
  3081. while (interrupt_pending(h)) {
  3082. raw_tag = get_next_completion(h);
  3083. while (raw_tag != FIFO_EMPTY) {
  3084. if (cciss_tag_contains_index(raw_tag))
  3085. raw_tag = process_indexed_cmd(h, raw_tag);
  3086. else
  3087. raw_tag = process_nonindexed_cmd(h, raw_tag);
  3088. }
  3089. }
  3090. spin_unlock_irqrestore(&h->lock, flags);
  3091. return IRQ_HANDLED;
  3092. }
  3093. /* Add a second interrupt handler for MSI/MSI-X mode. In this mode we never
  3094. * check the interrupt pending register because it is not set.
  3095. */
  3096. static irqreturn_t do_cciss_msix_intr(int irq, void *dev_id)
  3097. {
  3098. ctlr_info_t *h = dev_id;
  3099. unsigned long flags;
  3100. u32 raw_tag;
  3101. spin_lock_irqsave(&h->lock, flags);
  3102. raw_tag = get_next_completion(h);
  3103. while (raw_tag != FIFO_EMPTY) {
  3104. if (cciss_tag_contains_index(raw_tag))
  3105. raw_tag = process_indexed_cmd(h, raw_tag);
  3106. else
  3107. raw_tag = process_nonindexed_cmd(h, raw_tag);
  3108. }
  3109. spin_unlock_irqrestore(&h->lock, flags);
  3110. return IRQ_HANDLED;
  3111. }
  3112. /**
  3113. * add_to_scan_list() - add controller to rescan queue
  3114. * @h: Pointer to the controller.
  3115. *
  3116. * Adds the controller to the rescan queue if not already on the queue.
  3117. *
  3118. * returns 1 if added to the queue, 0 if skipped (could be on the
  3119. * queue already, or the controller could be initializing or shutting
  3120. * down).
  3121. **/
  3122. static int add_to_scan_list(struct ctlr_info *h)
  3123. {
  3124. struct ctlr_info *test_h;
  3125. int found = 0;
  3126. int ret = 0;
  3127. if (h->busy_initializing)
  3128. return 0;
  3129. if (!mutex_trylock(&h->busy_shutting_down))
  3130. return 0;
  3131. mutex_lock(&scan_mutex);
  3132. list_for_each_entry(test_h, &scan_q, scan_list) {
  3133. if (test_h == h) {
  3134. found = 1;
  3135. break;
  3136. }
  3137. }
  3138. if (!found && !h->busy_scanning) {
  3139. INIT_COMPLETION(h->scan_wait);
  3140. list_add_tail(&h->scan_list, &scan_q);
  3141. ret = 1;
  3142. }
  3143. mutex_unlock(&scan_mutex);
  3144. mutex_unlock(&h->busy_shutting_down);
  3145. return ret;
  3146. }
  3147. /**
  3148. * remove_from_scan_list() - remove controller from rescan queue
  3149. * @h: Pointer to the controller.
  3150. *
  3151. * Removes the controller from the rescan queue if present. Blocks if
  3152. * the controller is currently conducting a rescan. The controller
  3153. * can be in one of three states:
  3154. * 1. Doesn't need a scan
  3155. * 2. On the scan list, but not scanning yet (we remove it)
  3156. * 3. Busy scanning (and not on the list). In this case we want to wait for
  3157. * the scan to complete to make sure the scanning thread for this
  3158. * controller is completely idle.
  3159. **/
  3160. static void remove_from_scan_list(struct ctlr_info *h)
  3161. {
  3162. struct ctlr_info *test_h, *tmp_h;
  3163. mutex_lock(&scan_mutex);
  3164. list_for_each_entry_safe(test_h, tmp_h, &scan_q, scan_list) {
  3165. if (test_h == h) { /* state 2. */
  3166. list_del(&h->scan_list);
  3167. complete_all(&h->scan_wait);
  3168. mutex_unlock(&scan_mutex);
  3169. return;
  3170. }
  3171. }
  3172. if (h->busy_scanning) { /* state 3. */
  3173. mutex_unlock(&scan_mutex);
  3174. wait_for_completion(&h->scan_wait);
  3175. } else { /* state 1, nothing to do. */
  3176. mutex_unlock(&scan_mutex);
  3177. }
  3178. }
  3179. /**
  3180. * scan_thread() - kernel thread used to rescan controllers
  3181. * @data: Ignored.
  3182. *
  3183. * A kernel thread used scan for drive topology changes on
  3184. * controllers. The thread processes only one controller at a time
  3185. * using a queue. Controllers are added to the queue using
  3186. * add_to_scan_list() and removed from the queue either after done
  3187. * processing or using remove_from_scan_list().
  3188. *
  3189. * returns 0.
  3190. **/
  3191. static int scan_thread(void *data)
  3192. {
  3193. struct ctlr_info *h;
  3194. while (1) {
  3195. set_current_state(TASK_INTERRUPTIBLE);
  3196. schedule();
  3197. if (kthread_should_stop())
  3198. break;
  3199. while (1) {
  3200. mutex_lock(&scan_mutex);
  3201. if (list_empty(&scan_q)) {
  3202. mutex_unlock(&scan_mutex);
  3203. break;
  3204. }
  3205. h = list_entry(scan_q.next,
  3206. struct ctlr_info,
  3207. scan_list);
  3208. list_del(&h->scan_list);
  3209. h->busy_scanning = 1;
  3210. mutex_unlock(&scan_mutex);
  3211. rebuild_lun_table(h, 0, 0);
  3212. complete_all(&h->scan_wait);
  3213. mutex_lock(&scan_mutex);
  3214. h->busy_scanning = 0;
  3215. mutex_unlock(&scan_mutex);
  3216. }
  3217. }
  3218. return 0;
  3219. }
  3220. static int check_for_unit_attention(ctlr_info_t *h, CommandList_struct *c)
  3221. {
  3222. if (c->err_info->SenseInfo[2] != UNIT_ATTENTION)
  3223. return 0;
  3224. switch (c->err_info->SenseInfo[12]) {
  3225. case STATE_CHANGED:
  3226. dev_warn(&h->pdev->dev, "a state change "
  3227. "detected, command retried\n");
  3228. return 1;
  3229. break;
  3230. case LUN_FAILED:
  3231. dev_warn(&h->pdev->dev, "LUN failure "
  3232. "detected, action required\n");
  3233. return 1;
  3234. break;
  3235. case REPORT_LUNS_CHANGED:
  3236. dev_warn(&h->pdev->dev, "report LUN data changed\n");
  3237. /*
  3238. * Here, we could call add_to_scan_list and wake up the scan thread,
  3239. * except that it's quite likely that we will get more than one
  3240. * REPORT_LUNS_CHANGED condition in quick succession, which means
  3241. * that those which occur after the first one will likely happen
  3242. * *during* the scan_thread's rescan. And the rescan code is not
  3243. * robust enough to restart in the middle, undoing what it has already
  3244. * done, and it's not clear that it's even possible to do this, since
  3245. * part of what it does is notify the block layer, which starts
  3246. * doing it's own i/o to read partition tables and so on, and the
  3247. * driver doesn't have visibility to know what might need undoing.
  3248. * In any event, if possible, it is horribly complicated to get right
  3249. * so we just don't do it for now.
  3250. *
  3251. * Note: this REPORT_LUNS_CHANGED condition only occurs on the MSA2012.
  3252. */
  3253. return 1;
  3254. break;
  3255. case POWER_OR_RESET:
  3256. dev_warn(&h->pdev->dev,
  3257. "a power on or device reset detected\n");
  3258. return 1;
  3259. break;
  3260. case UNIT_ATTENTION_CLEARED:
  3261. dev_warn(&h->pdev->dev,
  3262. "unit attention cleared by another initiator\n");
  3263. return 1;
  3264. break;
  3265. default:
  3266. dev_warn(&h->pdev->dev, "unknown unit attention detected\n");
  3267. return 1;
  3268. }
  3269. }
  3270. /*
  3271. * We cannot read the structure directly, for portability we must use
  3272. * the io functions.
  3273. * This is for debug only.
  3274. */
  3275. static void print_cfg_table(ctlr_info_t *h)
  3276. {
  3277. int i;
  3278. char temp_name[17];
  3279. CfgTable_struct *tb = h->cfgtable;
  3280. dev_dbg(&h->pdev->dev, "Controller Configuration information\n");
  3281. dev_dbg(&h->pdev->dev, "------------------------------------\n");
  3282. for (i = 0; i < 4; i++)
  3283. temp_name[i] = readb(&(tb->Signature[i]));
  3284. temp_name[4] = '\0';
  3285. dev_dbg(&h->pdev->dev, " Signature = %s\n", temp_name);
  3286. dev_dbg(&h->pdev->dev, " Spec Number = %d\n",
  3287. readl(&(tb->SpecValence)));
  3288. dev_dbg(&h->pdev->dev, " Transport methods supported = 0x%x\n",
  3289. readl(&(tb->TransportSupport)));
  3290. dev_dbg(&h->pdev->dev, " Transport methods active = 0x%x\n",
  3291. readl(&(tb->TransportActive)));
  3292. dev_dbg(&h->pdev->dev, " Requested transport Method = 0x%x\n",
  3293. readl(&(tb->HostWrite.TransportRequest)));
  3294. dev_dbg(&h->pdev->dev, " Coalesce Interrupt Delay = 0x%x\n",
  3295. readl(&(tb->HostWrite.CoalIntDelay)));
  3296. dev_dbg(&h->pdev->dev, " Coalesce Interrupt Count = 0x%x\n",
  3297. readl(&(tb->HostWrite.CoalIntCount)));
  3298. dev_dbg(&h->pdev->dev, " Max outstanding commands = 0x%d\n",
  3299. readl(&(tb->CmdsOutMax)));
  3300. dev_dbg(&h->pdev->dev, " Bus Types = 0x%x\n",
  3301. readl(&(tb->BusTypes)));
  3302. for (i = 0; i < 16; i++)
  3303. temp_name[i] = readb(&(tb->ServerName[i]));
  3304. temp_name[16] = '\0';
  3305. dev_dbg(&h->pdev->dev, " Server Name = %s\n", temp_name);
  3306. dev_dbg(&h->pdev->dev, " Heartbeat Counter = 0x%x\n\n\n",
  3307. readl(&(tb->HeartBeat)));
  3308. }
  3309. static int find_PCI_BAR_index(struct pci_dev *pdev, unsigned long pci_bar_addr)
  3310. {
  3311. int i, offset, mem_type, bar_type;
  3312. if (pci_bar_addr == PCI_BASE_ADDRESS_0) /* looking for BAR zero? */
  3313. return 0;
  3314. offset = 0;
  3315. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  3316. bar_type = pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE;
  3317. if (bar_type == PCI_BASE_ADDRESS_SPACE_IO)
  3318. offset += 4;
  3319. else {
  3320. mem_type = pci_resource_flags(pdev, i) &
  3321. PCI_BASE_ADDRESS_MEM_TYPE_MASK;
  3322. switch (mem_type) {
  3323. case PCI_BASE_ADDRESS_MEM_TYPE_32:
  3324. case PCI_BASE_ADDRESS_MEM_TYPE_1M:
  3325. offset += 4; /* 32 bit */
  3326. break;
  3327. case PCI_BASE_ADDRESS_MEM_TYPE_64:
  3328. offset += 8;
  3329. break;
  3330. default: /* reserved in PCI 2.2 */
  3331. dev_warn(&pdev->dev,
  3332. "Base address is invalid\n");
  3333. return -1;
  3334. break;
  3335. }
  3336. }
  3337. if (offset == pci_bar_addr - PCI_BASE_ADDRESS_0)
  3338. return i + 1;
  3339. }
  3340. return -1;
  3341. }
  3342. /* Fill in bucket_map[], given nsgs (the max number of
  3343. * scatter gather elements supported) and bucket[],
  3344. * which is an array of 8 integers. The bucket[] array
  3345. * contains 8 different DMA transfer sizes (in 16
  3346. * byte increments) which the controller uses to fetch
  3347. * commands. This function fills in bucket_map[], which
  3348. * maps a given number of scatter gather elements to one of
  3349. * the 8 DMA transfer sizes. The point of it is to allow the
  3350. * controller to only do as much DMA as needed to fetch the
  3351. * command, with the DMA transfer size encoded in the lower
  3352. * bits of the command address.
  3353. */
  3354. static void calc_bucket_map(int bucket[], int num_buckets,
  3355. int nsgs, int *bucket_map)
  3356. {
  3357. int i, j, b, size;
  3358. /* even a command with 0 SGs requires 4 blocks */
  3359. #define MINIMUM_TRANSFER_BLOCKS 4
  3360. #define NUM_BUCKETS 8
  3361. /* Note, bucket_map must have nsgs+1 entries. */
  3362. for (i = 0; i <= nsgs; i++) {
  3363. /* Compute size of a command with i SG entries */
  3364. size = i + MINIMUM_TRANSFER_BLOCKS;
  3365. b = num_buckets; /* Assume the biggest bucket */
  3366. /* Find the bucket that is just big enough */
  3367. for (j = 0; j < 8; j++) {
  3368. if (bucket[j] >= size) {
  3369. b = j;
  3370. break;
  3371. }
  3372. }
  3373. /* for a command with i SG entries, use bucket b. */
  3374. bucket_map[i] = b;
  3375. }
  3376. }
  3377. static void __devinit cciss_wait_for_mode_change_ack(ctlr_info_t *h)
  3378. {
  3379. int i;
  3380. /* under certain very rare conditions, this can take awhile.
  3381. * (e.g.: hot replace a failed 144GB drive in a RAID 5 set right
  3382. * as we enter this code.) */
  3383. for (i = 0; i < MAX_CONFIG_WAIT; i++) {
  3384. if (!(readl(h->vaddr + SA5_DOORBELL) & CFGTBL_ChangeReq))
  3385. break;
  3386. msleep(10);
  3387. }
  3388. }
  3389. static __devinit void cciss_enter_performant_mode(ctlr_info_t *h)
  3390. {
  3391. /* This is a bit complicated. There are 8 registers on
  3392. * the controller which we write to to tell it 8 different
  3393. * sizes of commands which there may be. It's a way of
  3394. * reducing the DMA done to fetch each command. Encoded into
  3395. * each command's tag are 3 bits which communicate to the controller
  3396. * which of the eight sizes that command fits within. The size of
  3397. * each command depends on how many scatter gather entries there are.
  3398. * Each SG entry requires 16 bytes. The eight registers are programmed
  3399. * with the number of 16-byte blocks a command of that size requires.
  3400. * The smallest command possible requires 5 such 16 byte blocks.
  3401. * the largest command possible requires MAXSGENTRIES + 4 16-byte
  3402. * blocks. Note, this only extends to the SG entries contained
  3403. * within the command block, and does not extend to chained blocks
  3404. * of SG elements. bft[] contains the eight values we write to
  3405. * the registers. They are not evenly distributed, but have more
  3406. * sizes for small commands, and fewer sizes for larger commands.
  3407. */
  3408. __u32 trans_offset;
  3409. int bft[8] = { 5, 6, 8, 10, 12, 20, 28, MAXSGENTRIES + 4};
  3410. /*
  3411. * 5 = 1 s/g entry or 4k
  3412. * 6 = 2 s/g entry or 8k
  3413. * 8 = 4 s/g entry or 16k
  3414. * 10 = 6 s/g entry or 24k
  3415. */
  3416. unsigned long register_value;
  3417. BUILD_BUG_ON(28 > MAXSGENTRIES + 4);
  3418. h->reply_pool_wraparound = 1; /* spec: init to 1 */
  3419. /* Controller spec: zero out this buffer. */
  3420. memset(h->reply_pool, 0, h->max_commands * sizeof(__u64));
  3421. h->reply_pool_head = h->reply_pool;
  3422. trans_offset = readl(&(h->cfgtable->TransMethodOffset));
  3423. calc_bucket_map(bft, ARRAY_SIZE(bft), h->maxsgentries,
  3424. h->blockFetchTable);
  3425. writel(bft[0], &h->transtable->BlockFetch0);
  3426. writel(bft[1], &h->transtable->BlockFetch1);
  3427. writel(bft[2], &h->transtable->BlockFetch2);
  3428. writel(bft[3], &h->transtable->BlockFetch3);
  3429. writel(bft[4], &h->transtable->BlockFetch4);
  3430. writel(bft[5], &h->transtable->BlockFetch5);
  3431. writel(bft[6], &h->transtable->BlockFetch6);
  3432. writel(bft[7], &h->transtable->BlockFetch7);
  3433. /* size of controller ring buffer */
  3434. writel(h->max_commands, &h->transtable->RepQSize);
  3435. writel(1, &h->transtable->RepQCount);
  3436. writel(0, &h->transtable->RepQCtrAddrLow32);
  3437. writel(0, &h->transtable->RepQCtrAddrHigh32);
  3438. writel(h->reply_pool_dhandle, &h->transtable->RepQAddr0Low32);
  3439. writel(0, &h->transtable->RepQAddr0High32);
  3440. writel(CFGTBL_Trans_Performant,
  3441. &(h->cfgtable->HostWrite.TransportRequest));
  3442. writel(CFGTBL_ChangeReq, h->vaddr + SA5_DOORBELL);
  3443. cciss_wait_for_mode_change_ack(h);
  3444. register_value = readl(&(h->cfgtable->TransportActive));
  3445. if (!(register_value & CFGTBL_Trans_Performant))
  3446. dev_warn(&h->pdev->dev, "cciss: unable to get board into"
  3447. " performant mode\n");
  3448. }
  3449. static void __devinit cciss_put_controller_into_performant_mode(ctlr_info_t *h)
  3450. {
  3451. __u32 trans_support;
  3452. dev_dbg(&h->pdev->dev, "Trying to put board into Performant mode\n");
  3453. /* Attempt to put controller into performant mode if supported */
  3454. /* Does board support performant mode? */
  3455. trans_support = readl(&(h->cfgtable->TransportSupport));
  3456. if (!(trans_support & PERFORMANT_MODE))
  3457. return;
  3458. dev_dbg(&h->pdev->dev, "Placing controller into performant mode\n");
  3459. /* Performant mode demands commands on a 32 byte boundary
  3460. * pci_alloc_consistent aligns on page boundarys already.
  3461. * Just need to check if divisible by 32
  3462. */
  3463. if ((sizeof(CommandList_struct) % 32) != 0) {
  3464. dev_warn(&h->pdev->dev, "%s %d %s\n",
  3465. "cciss info: command size[",
  3466. (int)sizeof(CommandList_struct),
  3467. "] not divisible by 32, no performant mode..\n");
  3468. return;
  3469. }
  3470. /* Performant mode ring buffer and supporting data structures */
  3471. h->reply_pool = (__u64 *)pci_alloc_consistent(
  3472. h->pdev, h->max_commands * sizeof(__u64),
  3473. &(h->reply_pool_dhandle));
  3474. /* Need a block fetch table for performant mode */
  3475. h->blockFetchTable = kmalloc(((h->maxsgentries+1) *
  3476. sizeof(__u32)), GFP_KERNEL);
  3477. if ((h->reply_pool == NULL) || (h->blockFetchTable == NULL))
  3478. goto clean_up;
  3479. cciss_enter_performant_mode(h);
  3480. /* Change the access methods to the performant access methods */
  3481. h->access = SA5_performant_access;
  3482. h->transMethod = CFGTBL_Trans_Performant;
  3483. return;
  3484. clean_up:
  3485. kfree(h->blockFetchTable);
  3486. if (h->reply_pool)
  3487. pci_free_consistent(h->pdev,
  3488. h->max_commands * sizeof(__u64),
  3489. h->reply_pool,
  3490. h->reply_pool_dhandle);
  3491. return;
  3492. } /* cciss_put_controller_into_performant_mode */
  3493. /* If MSI/MSI-X is supported by the kernel we will try to enable it on
  3494. * controllers that are capable. If not, we use IO-APIC mode.
  3495. */
  3496. static void __devinit cciss_interrupt_mode(ctlr_info_t *h)
  3497. {
  3498. #ifdef CONFIG_PCI_MSI
  3499. int err;
  3500. struct msix_entry cciss_msix_entries[4] = { {0, 0}, {0, 1},
  3501. {0, 2}, {0, 3}
  3502. };
  3503. /* Some boards advertise MSI but don't really support it */
  3504. if ((h->board_id == 0x40700E11) || (h->board_id == 0x40800E11) ||
  3505. (h->board_id == 0x40820E11) || (h->board_id == 0x40830E11))
  3506. goto default_int_mode;
  3507. if (pci_find_capability(h->pdev, PCI_CAP_ID_MSIX)) {
  3508. err = pci_enable_msix(h->pdev, cciss_msix_entries, 4);
  3509. if (!err) {
  3510. h->intr[0] = cciss_msix_entries[0].vector;
  3511. h->intr[1] = cciss_msix_entries[1].vector;
  3512. h->intr[2] = cciss_msix_entries[2].vector;
  3513. h->intr[3] = cciss_msix_entries[3].vector;
  3514. h->msix_vector = 1;
  3515. return;
  3516. }
  3517. if (err > 0) {
  3518. dev_warn(&h->pdev->dev,
  3519. "only %d MSI-X vectors available\n", err);
  3520. goto default_int_mode;
  3521. } else {
  3522. dev_warn(&h->pdev->dev,
  3523. "MSI-X init failed %d\n", err);
  3524. goto default_int_mode;
  3525. }
  3526. }
  3527. if (pci_find_capability(h->pdev, PCI_CAP_ID_MSI)) {
  3528. if (!pci_enable_msi(h->pdev))
  3529. h->msi_vector = 1;
  3530. else
  3531. dev_warn(&h->pdev->dev, "MSI init failed\n");
  3532. }
  3533. default_int_mode:
  3534. #endif /* CONFIG_PCI_MSI */
  3535. /* if we get here we're going to use the default interrupt mode */
  3536. h->intr[PERF_MODE_INT] = h->pdev->irq;
  3537. return;
  3538. }
  3539. static int __devinit cciss_lookup_board_id(struct pci_dev *pdev, u32 *board_id)
  3540. {
  3541. int i;
  3542. u32 subsystem_vendor_id, subsystem_device_id;
  3543. subsystem_vendor_id = pdev->subsystem_vendor;
  3544. subsystem_device_id = pdev->subsystem_device;
  3545. *board_id = ((subsystem_device_id << 16) & 0xffff0000) |
  3546. subsystem_vendor_id;
  3547. for (i = 0; i < ARRAY_SIZE(products); i++) {
  3548. /* Stand aside for hpsa driver on request */
  3549. if (cciss_allow_hpsa && products[i].board_id == HPSA_BOUNDARY)
  3550. return -ENODEV;
  3551. if (*board_id == products[i].board_id)
  3552. return i;
  3553. }
  3554. dev_warn(&pdev->dev, "unrecognized board ID: 0x%08x, ignoring.\n",
  3555. *board_id);
  3556. return -ENODEV;
  3557. }
  3558. static inline bool cciss_board_disabled(ctlr_info_t *h)
  3559. {
  3560. u16 command;
  3561. (void) pci_read_config_word(h->pdev, PCI_COMMAND, &command);
  3562. return ((command & PCI_COMMAND_MEMORY) == 0);
  3563. }
  3564. static int __devinit cciss_pci_find_memory_BAR(struct pci_dev *pdev,
  3565. unsigned long *memory_bar)
  3566. {
  3567. int i;
  3568. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  3569. if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
  3570. /* addressing mode bits already removed */
  3571. *memory_bar = pci_resource_start(pdev, i);
  3572. dev_dbg(&pdev->dev, "memory BAR = %lx\n",
  3573. *memory_bar);
  3574. return 0;
  3575. }
  3576. dev_warn(&pdev->dev, "no memory BAR found\n");
  3577. return -ENODEV;
  3578. }
  3579. static int __devinit cciss_wait_for_board_ready(ctlr_info_t *h)
  3580. {
  3581. int i;
  3582. u32 scratchpad;
  3583. for (i = 0; i < CCISS_BOARD_READY_ITERATIONS; i++) {
  3584. scratchpad = readl(h->vaddr + SA5_SCRATCHPAD_OFFSET);
  3585. if (scratchpad == CCISS_FIRMWARE_READY)
  3586. return 0;
  3587. msleep(CCISS_BOARD_READY_POLL_INTERVAL_MSECS);
  3588. }
  3589. dev_warn(&h->pdev->dev, "board not ready, timed out.\n");
  3590. return -ENODEV;
  3591. }
  3592. static int __devinit cciss_find_cfg_addrs(struct pci_dev *pdev,
  3593. void __iomem *vaddr, u32 *cfg_base_addr, u64 *cfg_base_addr_index,
  3594. u64 *cfg_offset)
  3595. {
  3596. *cfg_base_addr = readl(vaddr + SA5_CTCFG_OFFSET);
  3597. *cfg_offset = readl(vaddr + SA5_CTMEM_OFFSET);
  3598. *cfg_base_addr &= (u32) 0x0000ffff;
  3599. *cfg_base_addr_index = find_PCI_BAR_index(pdev, *cfg_base_addr);
  3600. if (*cfg_base_addr_index == -1) {
  3601. dev_warn(&pdev->dev, "cannot find cfg_base_addr_index, "
  3602. "*cfg_base_addr = 0x%08x\n", *cfg_base_addr);
  3603. return -ENODEV;
  3604. }
  3605. return 0;
  3606. }
  3607. static int __devinit cciss_find_cfgtables(ctlr_info_t *h)
  3608. {
  3609. u64 cfg_offset;
  3610. u32 cfg_base_addr;
  3611. u64 cfg_base_addr_index;
  3612. u32 trans_offset;
  3613. int rc;
  3614. rc = cciss_find_cfg_addrs(h->pdev, h->vaddr, &cfg_base_addr,
  3615. &cfg_base_addr_index, &cfg_offset);
  3616. if (rc)
  3617. return rc;
  3618. h->cfgtable = remap_pci_mem(pci_resource_start(h->pdev,
  3619. cfg_base_addr_index) + cfg_offset, sizeof(h->cfgtable));
  3620. if (!h->cfgtable)
  3621. return -ENOMEM;
  3622. /* Find performant mode table. */
  3623. trans_offset = readl(&h->cfgtable->TransMethodOffset);
  3624. h->transtable = remap_pci_mem(pci_resource_start(h->pdev,
  3625. cfg_base_addr_index)+cfg_offset+trans_offset,
  3626. sizeof(*h->transtable));
  3627. if (!h->transtable)
  3628. return -ENOMEM;
  3629. return 0;
  3630. }
  3631. static void __devinit cciss_get_max_perf_mode_cmds(struct ctlr_info *h)
  3632. {
  3633. h->max_commands = readl(&(h->cfgtable->MaxPerformantModeCommands));
  3634. if (h->max_commands < 16) {
  3635. dev_warn(&h->pdev->dev, "Controller reports "
  3636. "max supported commands of %d, an obvious lie. "
  3637. "Using 16. Ensure that firmware is up to date.\n",
  3638. h->max_commands);
  3639. h->max_commands = 16;
  3640. }
  3641. }
  3642. /* Interrogate the hardware for some limits:
  3643. * max commands, max SG elements without chaining, and with chaining,
  3644. * SG chain block size, etc.
  3645. */
  3646. static void __devinit cciss_find_board_params(ctlr_info_t *h)
  3647. {
  3648. cciss_get_max_perf_mode_cmds(h);
  3649. h->nr_cmds = h->max_commands - 4; /* Allow room for some ioctls */
  3650. h->maxsgentries = readl(&(h->cfgtable->MaxSGElements));
  3651. /*
  3652. * Limit in-command s/g elements to 32 save dma'able memory.
  3653. * Howvever spec says if 0, use 31
  3654. */
  3655. h->max_cmd_sgentries = 31;
  3656. if (h->maxsgentries > 512) {
  3657. h->max_cmd_sgentries = 32;
  3658. h->chainsize = h->maxsgentries - h->max_cmd_sgentries + 1;
  3659. h->maxsgentries--; /* save one for chain pointer */
  3660. } else {
  3661. h->maxsgentries = 31; /* default to traditional values */
  3662. h->chainsize = 0;
  3663. }
  3664. }
  3665. static inline bool CISS_signature_present(ctlr_info_t *h)
  3666. {
  3667. if ((readb(&h->cfgtable->Signature[0]) != 'C') ||
  3668. (readb(&h->cfgtable->Signature[1]) != 'I') ||
  3669. (readb(&h->cfgtable->Signature[2]) != 'S') ||
  3670. (readb(&h->cfgtable->Signature[3]) != 'S')) {
  3671. dev_warn(&h->pdev->dev, "not a valid CISS config table\n");
  3672. return false;
  3673. }
  3674. return true;
  3675. }
  3676. /* Need to enable prefetch in the SCSI core for 6400 in x86 */
  3677. static inline void cciss_enable_scsi_prefetch(ctlr_info_t *h)
  3678. {
  3679. #ifdef CONFIG_X86
  3680. u32 prefetch;
  3681. prefetch = readl(&(h->cfgtable->SCSI_Prefetch));
  3682. prefetch |= 0x100;
  3683. writel(prefetch, &(h->cfgtable->SCSI_Prefetch));
  3684. #endif
  3685. }
  3686. /* Disable DMA prefetch for the P600. Otherwise an ASIC bug may result
  3687. * in a prefetch beyond physical memory.
  3688. */
  3689. static inline void cciss_p600_dma_prefetch_quirk(ctlr_info_t *h)
  3690. {
  3691. u32 dma_prefetch;
  3692. __u32 dma_refetch;
  3693. if (h->board_id != 0x3225103C)
  3694. return;
  3695. dma_prefetch = readl(h->vaddr + I2O_DMA1_CFG);
  3696. dma_prefetch |= 0x8000;
  3697. writel(dma_prefetch, h->vaddr + I2O_DMA1_CFG);
  3698. pci_read_config_dword(h->pdev, PCI_COMMAND_PARITY, &dma_refetch);
  3699. dma_refetch |= 0x1;
  3700. pci_write_config_dword(h->pdev, PCI_COMMAND_PARITY, dma_refetch);
  3701. }
  3702. static int __devinit cciss_pci_init(ctlr_info_t *h)
  3703. {
  3704. int prod_index, err;
  3705. prod_index = cciss_lookup_board_id(h->pdev, &h->board_id);
  3706. if (prod_index < 0)
  3707. return -ENODEV;
  3708. h->product_name = products[prod_index].product_name;
  3709. h->access = *(products[prod_index].access);
  3710. if (cciss_board_disabled(h)) {
  3711. dev_warn(&h->pdev->dev, "controller appears to be disabled\n");
  3712. return -ENODEV;
  3713. }
  3714. err = pci_enable_device(h->pdev);
  3715. if (err) {
  3716. dev_warn(&h->pdev->dev, "Unable to Enable PCI device\n");
  3717. return err;
  3718. }
  3719. err = pci_request_regions(h->pdev, "cciss");
  3720. if (err) {
  3721. dev_warn(&h->pdev->dev,
  3722. "Cannot obtain PCI resources, aborting\n");
  3723. return err;
  3724. }
  3725. dev_dbg(&h->pdev->dev, "irq = %x\n", h->pdev->irq);
  3726. dev_dbg(&h->pdev->dev, "board_id = %x\n", h->board_id);
  3727. /* If the kernel supports MSI/MSI-X we will try to enable that functionality,
  3728. * else we use the IO-APIC interrupt assigned to us by system ROM.
  3729. */
  3730. cciss_interrupt_mode(h);
  3731. err = cciss_pci_find_memory_BAR(h->pdev, &h->paddr);
  3732. if (err)
  3733. goto err_out_free_res;
  3734. h->vaddr = remap_pci_mem(h->paddr, 0x250);
  3735. if (!h->vaddr) {
  3736. err = -ENOMEM;
  3737. goto err_out_free_res;
  3738. }
  3739. err = cciss_wait_for_board_ready(h);
  3740. if (err)
  3741. goto err_out_free_res;
  3742. err = cciss_find_cfgtables(h);
  3743. if (err)
  3744. goto err_out_free_res;
  3745. print_cfg_table(h);
  3746. cciss_find_board_params(h);
  3747. if (!CISS_signature_present(h)) {
  3748. err = -ENODEV;
  3749. goto err_out_free_res;
  3750. }
  3751. cciss_enable_scsi_prefetch(h);
  3752. cciss_p600_dma_prefetch_quirk(h);
  3753. cciss_put_controller_into_performant_mode(h);
  3754. return 0;
  3755. err_out_free_res:
  3756. /*
  3757. * Deliberately omit pci_disable_device(): it does something nasty to
  3758. * Smart Array controllers that pci_enable_device does not undo
  3759. */
  3760. if (h->transtable)
  3761. iounmap(h->transtable);
  3762. if (h->cfgtable)
  3763. iounmap(h->cfgtable);
  3764. if (h->vaddr)
  3765. iounmap(h->vaddr);
  3766. pci_release_regions(h->pdev);
  3767. return err;
  3768. }
  3769. /* Function to find the first free pointer into our hba[] array
  3770. * Returns -1 if no free entries are left.
  3771. */
  3772. static int alloc_cciss_hba(struct pci_dev *pdev)
  3773. {
  3774. int i;
  3775. for (i = 0; i < MAX_CTLR; i++) {
  3776. if (!hba[i]) {
  3777. ctlr_info_t *h;
  3778. h = kzalloc(sizeof(ctlr_info_t), GFP_KERNEL);
  3779. if (!h)
  3780. goto Enomem;
  3781. hba[i] = h;
  3782. return i;
  3783. }
  3784. }
  3785. dev_warn(&pdev->dev, "This driver supports a maximum"
  3786. " of %d controllers.\n", MAX_CTLR);
  3787. return -1;
  3788. Enomem:
  3789. dev_warn(&pdev->dev, "out of memory.\n");
  3790. return -1;
  3791. }
  3792. static void free_hba(ctlr_info_t *h)
  3793. {
  3794. int i;
  3795. hba[h->ctlr] = NULL;
  3796. for (i = 0; i < h->highest_lun + 1; i++)
  3797. if (h->gendisk[i] != NULL)
  3798. put_disk(h->gendisk[i]);
  3799. kfree(h);
  3800. }
  3801. /* Send a message CDB to the firmware. */
  3802. static __devinit int cciss_message(struct pci_dev *pdev, unsigned char opcode, unsigned char type)
  3803. {
  3804. typedef struct {
  3805. CommandListHeader_struct CommandHeader;
  3806. RequestBlock_struct Request;
  3807. ErrDescriptor_struct ErrorDescriptor;
  3808. } Command;
  3809. static const size_t cmd_sz = sizeof(Command) + sizeof(ErrorInfo_struct);
  3810. Command *cmd;
  3811. dma_addr_t paddr64;
  3812. uint32_t paddr32, tag;
  3813. void __iomem *vaddr;
  3814. int i, err;
  3815. vaddr = ioremap_nocache(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
  3816. if (vaddr == NULL)
  3817. return -ENOMEM;
  3818. /* The Inbound Post Queue only accepts 32-bit physical addresses for the
  3819. CCISS commands, so they must be allocated from the lower 4GiB of
  3820. memory. */
  3821. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  3822. if (err) {
  3823. iounmap(vaddr);
  3824. return -ENOMEM;
  3825. }
  3826. cmd = pci_alloc_consistent(pdev, cmd_sz, &paddr64);
  3827. if (cmd == NULL) {
  3828. iounmap(vaddr);
  3829. return -ENOMEM;
  3830. }
  3831. /* This must fit, because of the 32-bit consistent DMA mask. Also,
  3832. although there's no guarantee, we assume that the address is at
  3833. least 4-byte aligned (most likely, it's page-aligned). */
  3834. paddr32 = paddr64;
  3835. cmd->CommandHeader.ReplyQueue = 0;
  3836. cmd->CommandHeader.SGList = 0;
  3837. cmd->CommandHeader.SGTotal = 0;
  3838. cmd->CommandHeader.Tag.lower = paddr32;
  3839. cmd->CommandHeader.Tag.upper = 0;
  3840. memset(&cmd->CommandHeader.LUN.LunAddrBytes, 0, 8);
  3841. cmd->Request.CDBLen = 16;
  3842. cmd->Request.Type.Type = TYPE_MSG;
  3843. cmd->Request.Type.Attribute = ATTR_HEADOFQUEUE;
  3844. cmd->Request.Type.Direction = XFER_NONE;
  3845. cmd->Request.Timeout = 0; /* Don't time out */
  3846. cmd->Request.CDB[0] = opcode;
  3847. cmd->Request.CDB[1] = type;
  3848. memset(&cmd->Request.CDB[2], 0, 14); /* the rest of the CDB is reserved */
  3849. cmd->ErrorDescriptor.Addr.lower = paddr32 + sizeof(Command);
  3850. cmd->ErrorDescriptor.Addr.upper = 0;
  3851. cmd->ErrorDescriptor.Len = sizeof(ErrorInfo_struct);
  3852. writel(paddr32, vaddr + SA5_REQUEST_PORT_OFFSET);
  3853. for (i = 0; i < 10; i++) {
  3854. tag = readl(vaddr + SA5_REPLY_PORT_OFFSET);
  3855. if ((tag & ~3) == paddr32)
  3856. break;
  3857. schedule_timeout_uninterruptible(HZ);
  3858. }
  3859. iounmap(vaddr);
  3860. /* we leak the DMA buffer here ... no choice since the controller could
  3861. still complete the command. */
  3862. if (i == 10) {
  3863. dev_err(&pdev->dev,
  3864. "controller message %02x:%02x timed out\n",
  3865. opcode, type);
  3866. return -ETIMEDOUT;
  3867. }
  3868. pci_free_consistent(pdev, cmd_sz, cmd, paddr64);
  3869. if (tag & 2) {
  3870. dev_err(&pdev->dev, "controller message %02x:%02x failed\n",
  3871. opcode, type);
  3872. return -EIO;
  3873. }
  3874. dev_info(&pdev->dev, "controller message %02x:%02x succeeded\n",
  3875. opcode, type);
  3876. return 0;
  3877. }
  3878. #define cciss_soft_reset_controller(p) cciss_message(p, 1, 0)
  3879. #define cciss_noop(p) cciss_message(p, 3, 0)
  3880. static __devinit int cciss_reset_msi(struct pci_dev *pdev)
  3881. {
  3882. /* the #defines are stolen from drivers/pci/msi.h. */
  3883. #define msi_control_reg(base) (base + PCI_MSI_FLAGS)
  3884. #define PCI_MSIX_FLAGS_ENABLE (1 << 15)
  3885. int pos;
  3886. u16 control = 0;
  3887. pos = pci_find_capability(pdev, PCI_CAP_ID_MSI);
  3888. if (pos) {
  3889. pci_read_config_word(pdev, msi_control_reg(pos), &control);
  3890. if (control & PCI_MSI_FLAGS_ENABLE) {
  3891. dev_info(&pdev->dev, "resetting MSI\n");
  3892. pci_write_config_word(pdev, msi_control_reg(pos), control & ~PCI_MSI_FLAGS_ENABLE);
  3893. }
  3894. }
  3895. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  3896. if (pos) {
  3897. pci_read_config_word(pdev, msi_control_reg(pos), &control);
  3898. if (control & PCI_MSIX_FLAGS_ENABLE) {
  3899. dev_info(&pdev->dev, "resetting MSI-X\n");
  3900. pci_write_config_word(pdev, msi_control_reg(pos), control & ~PCI_MSIX_FLAGS_ENABLE);
  3901. }
  3902. }
  3903. return 0;
  3904. }
  3905. static int cciss_controller_hard_reset(struct pci_dev *pdev,
  3906. void * __iomem vaddr, bool use_doorbell)
  3907. {
  3908. u16 pmcsr;
  3909. int pos;
  3910. if (use_doorbell) {
  3911. /* For everything after the P600, the PCI power state method
  3912. * of resetting the controller doesn't work, so we have this
  3913. * other way using the doorbell register.
  3914. */
  3915. dev_info(&pdev->dev, "using doorbell to reset controller\n");
  3916. writel(DOORBELL_CTLR_RESET, vaddr + SA5_DOORBELL);
  3917. msleep(1000);
  3918. } else { /* Try to do it the PCI power state way */
  3919. /* Quoting from the Open CISS Specification: "The Power
  3920. * Management Control/Status Register (CSR) controls the power
  3921. * state of the device. The normal operating state is D0,
  3922. * CSR=00h. The software off state is D3, CSR=03h. To reset
  3923. * the controller, place the interface device in D3 then to D0,
  3924. * this causes a secondary PCI reset which will reset the
  3925. * controller." */
  3926. pos = pci_find_capability(pdev, PCI_CAP_ID_PM);
  3927. if (pos == 0) {
  3928. dev_err(&pdev->dev,
  3929. "cciss_controller_hard_reset: "
  3930. "PCI PM not supported\n");
  3931. return -ENODEV;
  3932. }
  3933. dev_info(&pdev->dev, "using PCI PM to reset controller\n");
  3934. /* enter the D3hot power management state */
  3935. pci_read_config_word(pdev, pos + PCI_PM_CTRL, &pmcsr);
  3936. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  3937. pmcsr |= PCI_D3hot;
  3938. pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
  3939. msleep(500);
  3940. /* enter the D0 power management state */
  3941. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  3942. pmcsr |= PCI_D0;
  3943. pci_write_config_word(pdev, pos + PCI_PM_CTRL, pmcsr);
  3944. msleep(500);
  3945. }
  3946. return 0;
  3947. }
  3948. /* This does a hard reset of the controller using PCI power management
  3949. * states or using the doorbell register. */
  3950. static __devinit int cciss_kdump_hard_reset_controller(struct pci_dev *pdev)
  3951. {
  3952. u16 saved_config_space[32];
  3953. u64 cfg_offset;
  3954. u32 cfg_base_addr;
  3955. u64 cfg_base_addr_index;
  3956. void __iomem *vaddr;
  3957. unsigned long paddr;
  3958. u32 misc_fw_support, active_transport;
  3959. int rc, i;
  3960. CfgTable_struct __iomem *cfgtable;
  3961. bool use_doorbell;
  3962. u32 board_id;
  3963. /* For controllers as old a the p600, this is very nearly
  3964. * the same thing as
  3965. *
  3966. * pci_save_state(pci_dev);
  3967. * pci_set_power_state(pci_dev, PCI_D3hot);
  3968. * pci_set_power_state(pci_dev, PCI_D0);
  3969. * pci_restore_state(pci_dev);
  3970. *
  3971. * but we can't use these nice canned kernel routines on
  3972. * kexec, because they also check the MSI/MSI-X state in PCI
  3973. * configuration space and do the wrong thing when it is
  3974. * set/cleared. Also, the pci_save/restore_state functions
  3975. * violate the ordering requirements for restoring the
  3976. * configuration space from the CCISS document (see the
  3977. * comment below). So we roll our own ....
  3978. *
  3979. * For controllers newer than the P600, the pci power state
  3980. * method of resetting doesn't work so we have another way
  3981. * using the doorbell register.
  3982. */
  3983. /* Exclude 640x boards. These are two pci devices in one slot
  3984. * which share a battery backed cache module. One controls the
  3985. * cache, the other accesses the cache through the one that controls
  3986. * it. If we reset the one controlling the cache, the other will
  3987. * likely not be happy. Just forbid resetting this conjoined mess.
  3988. */
  3989. cciss_lookup_board_id(pdev, &board_id);
  3990. if (board_id == 0x409C0E11 || board_id == 0x409D0E11) {
  3991. dev_warn(&pdev->dev, "Cannot reset Smart Array 640x "
  3992. "due to shared cache module.");
  3993. return -ENODEV;
  3994. }
  3995. for (i = 0; i < 32; i++)
  3996. pci_read_config_word(pdev, 2*i, &saved_config_space[i]);
  3997. /* find the first memory BAR, so we can find the cfg table */
  3998. rc = cciss_pci_find_memory_BAR(pdev, &paddr);
  3999. if (rc)
  4000. return rc;
  4001. vaddr = remap_pci_mem(paddr, 0x250);
  4002. if (!vaddr)
  4003. return -ENOMEM;
  4004. /* find cfgtable in order to check if reset via doorbell is supported */
  4005. rc = cciss_find_cfg_addrs(pdev, vaddr, &cfg_base_addr,
  4006. &cfg_base_addr_index, &cfg_offset);
  4007. if (rc)
  4008. goto unmap_vaddr;
  4009. cfgtable = remap_pci_mem(pci_resource_start(pdev,
  4010. cfg_base_addr_index) + cfg_offset, sizeof(*cfgtable));
  4011. if (!cfgtable) {
  4012. rc = -ENOMEM;
  4013. goto unmap_vaddr;
  4014. }
  4015. /* If reset via doorbell register is supported, use that. */
  4016. misc_fw_support = readl(&cfgtable->misc_fw_support);
  4017. use_doorbell = misc_fw_support & MISC_FW_DOORBELL_RESET;
  4018. rc = cciss_controller_hard_reset(pdev, vaddr, use_doorbell);
  4019. if (rc)
  4020. goto unmap_cfgtable;
  4021. /* Restore the PCI configuration space. The Open CISS
  4022. * Specification says, "Restore the PCI Configuration
  4023. * Registers, offsets 00h through 60h. It is important to
  4024. * restore the command register, 16-bits at offset 04h,
  4025. * last. Do not restore the configuration status register,
  4026. * 16-bits at offset 06h." Note that the offset is 2*i.
  4027. */
  4028. for (i = 0; i < 32; i++) {
  4029. if (i == 2 || i == 3)
  4030. continue;
  4031. pci_write_config_word(pdev, 2*i, saved_config_space[i]);
  4032. }
  4033. wmb();
  4034. pci_write_config_word(pdev, 4, saved_config_space[2]);
  4035. /* Some devices (notably the HP Smart Array 5i Controller)
  4036. need a little pause here */
  4037. msleep(CCISS_POST_RESET_PAUSE_MSECS);
  4038. /* Controller should be in simple mode at this point. If it's not,
  4039. * It means we're on one of those controllers which doesn't support
  4040. * the doorbell reset method and on which the PCI power management reset
  4041. * method doesn't work (P800, for example.)
  4042. * In those cases, don't try to proceed, as it generally doesn't work.
  4043. */
  4044. active_transport = readl(&cfgtable->TransportActive);
  4045. if (active_transport & PERFORMANT_MODE) {
  4046. dev_warn(&pdev->dev, "Unable to successfully reset controller,"
  4047. " Ignoring controller.\n");
  4048. rc = -ENODEV;
  4049. }
  4050. unmap_cfgtable:
  4051. iounmap(cfgtable);
  4052. unmap_vaddr:
  4053. iounmap(vaddr);
  4054. return rc;
  4055. }
  4056. static __devinit int cciss_init_reset_devices(struct pci_dev *pdev)
  4057. {
  4058. int rc, i;
  4059. if (!reset_devices)
  4060. return 0;
  4061. /* Reset the controller with a PCI power-cycle or via doorbell */
  4062. rc = cciss_kdump_hard_reset_controller(pdev);
  4063. /* -ENOTSUPP here means we cannot reset the controller
  4064. * but it's already (and still) up and running in
  4065. * "performant mode". Or, it might be 640x, which can't reset
  4066. * due to concerns about shared bbwc between 6402/6404 pair.
  4067. */
  4068. if (rc == -ENOTSUPP)
  4069. return 0; /* just try to do the kdump anyhow. */
  4070. if (rc)
  4071. return -ENODEV;
  4072. if (cciss_reset_msi(pdev))
  4073. return -ENODEV;
  4074. /* Now try to get the controller to respond to a no-op */
  4075. for (i = 0; i < CCISS_POST_RESET_NOOP_RETRIES; i++) {
  4076. if (cciss_noop(pdev) == 0)
  4077. break;
  4078. else
  4079. dev_warn(&pdev->dev, "no-op failed%s\n",
  4080. (i < CCISS_POST_RESET_NOOP_RETRIES - 1 ?
  4081. "; re-trying" : ""));
  4082. msleep(CCISS_POST_RESET_NOOP_INTERVAL_MSECS);
  4083. }
  4084. return 0;
  4085. }
  4086. /*
  4087. * This is it. Find all the controllers and register them. I really hate
  4088. * stealing all these major device numbers.
  4089. * returns the number of block devices registered.
  4090. */
  4091. static int __devinit cciss_init_one(struct pci_dev *pdev,
  4092. const struct pci_device_id *ent)
  4093. {
  4094. int i;
  4095. int j = 0;
  4096. int k = 0;
  4097. int rc;
  4098. int dac, return_code;
  4099. InquiryData_struct *inq_buff;
  4100. ctlr_info_t *h;
  4101. rc = cciss_init_reset_devices(pdev);
  4102. if (rc)
  4103. return rc;
  4104. i = alloc_cciss_hba(pdev);
  4105. if (i < 0)
  4106. return -1;
  4107. h = hba[i];
  4108. h->pdev = pdev;
  4109. h->busy_initializing = 1;
  4110. INIT_HLIST_HEAD(&h->cmpQ);
  4111. INIT_HLIST_HEAD(&h->reqQ);
  4112. mutex_init(&h->busy_shutting_down);
  4113. if (cciss_pci_init(h) != 0)
  4114. goto clean_no_release_regions;
  4115. sprintf(h->devname, "cciss%d", i);
  4116. h->ctlr = i;
  4117. init_completion(&h->scan_wait);
  4118. if (cciss_create_hba_sysfs_entry(h))
  4119. goto clean0;
  4120. /* configure PCI DMA stuff */
  4121. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)))
  4122. dac = 1;
  4123. else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)))
  4124. dac = 0;
  4125. else {
  4126. dev_err(&h->pdev->dev, "no suitable DMA available\n");
  4127. goto clean1;
  4128. }
  4129. /*
  4130. * register with the major number, or get a dynamic major number
  4131. * by passing 0 as argument. This is done for greater than
  4132. * 8 controller support.
  4133. */
  4134. if (i < MAX_CTLR_ORIG)
  4135. h->major = COMPAQ_CISS_MAJOR + i;
  4136. rc = register_blkdev(h->major, h->devname);
  4137. if (rc == -EBUSY || rc == -EINVAL) {
  4138. dev_err(&h->pdev->dev,
  4139. "Unable to get major number %d for %s "
  4140. "on hba %d\n", h->major, h->devname, i);
  4141. goto clean1;
  4142. } else {
  4143. if (i >= MAX_CTLR_ORIG)
  4144. h->major = rc;
  4145. }
  4146. /* make sure the board interrupts are off */
  4147. h->access.set_intr_mask(h, CCISS_INTR_OFF);
  4148. if (h->msi_vector || h->msix_vector) {
  4149. if (request_irq(h->intr[PERF_MODE_INT],
  4150. do_cciss_msix_intr,
  4151. IRQF_DISABLED, h->devname, h)) {
  4152. dev_err(&h->pdev->dev, "Unable to get irq %d for %s\n",
  4153. h->intr[PERF_MODE_INT], h->devname);
  4154. goto clean2;
  4155. }
  4156. } else {
  4157. if (request_irq(h->intr[PERF_MODE_INT], do_cciss_intx,
  4158. IRQF_DISABLED, h->devname, h)) {
  4159. dev_err(&h->pdev->dev, "Unable to get irq %d for %s\n",
  4160. h->intr[PERF_MODE_INT], h->devname);
  4161. goto clean2;
  4162. }
  4163. }
  4164. dev_info(&h->pdev->dev, "%s: <0x%x> at PCI %s IRQ %d%s using DAC\n",
  4165. h->devname, pdev->device, pci_name(pdev),
  4166. h->intr[PERF_MODE_INT], dac ? "" : " not");
  4167. h->cmd_pool_bits =
  4168. kmalloc(DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG)
  4169. * sizeof(unsigned long), GFP_KERNEL);
  4170. h->cmd_pool = (CommandList_struct *)
  4171. pci_alloc_consistent(h->pdev,
  4172. h->nr_cmds * sizeof(CommandList_struct),
  4173. &(h->cmd_pool_dhandle));
  4174. h->errinfo_pool = (ErrorInfo_struct *)
  4175. pci_alloc_consistent(h->pdev,
  4176. h->nr_cmds * sizeof(ErrorInfo_struct),
  4177. &(h->errinfo_pool_dhandle));
  4178. if ((h->cmd_pool_bits == NULL)
  4179. || (h->cmd_pool == NULL)
  4180. || (h->errinfo_pool == NULL)) {
  4181. dev_err(&h->pdev->dev, "out of memory");
  4182. goto clean4;
  4183. }
  4184. /* Need space for temp scatter list */
  4185. h->scatter_list = kmalloc(h->max_commands *
  4186. sizeof(struct scatterlist *),
  4187. GFP_KERNEL);
  4188. for (k = 0; k < h->nr_cmds; k++) {
  4189. h->scatter_list[k] = kmalloc(sizeof(struct scatterlist) *
  4190. h->maxsgentries,
  4191. GFP_KERNEL);
  4192. if (h->scatter_list[k] == NULL) {
  4193. dev_err(&h->pdev->dev,
  4194. "could not allocate s/g lists\n");
  4195. goto clean4;
  4196. }
  4197. }
  4198. h->cmd_sg_list = cciss_allocate_sg_chain_blocks(h,
  4199. h->chainsize, h->nr_cmds);
  4200. if (!h->cmd_sg_list && h->chainsize > 0)
  4201. goto clean4;
  4202. spin_lock_init(&h->lock);
  4203. /* Initialize the pdev driver private data.
  4204. have it point to h. */
  4205. pci_set_drvdata(pdev, h);
  4206. /* command and error info recs zeroed out before
  4207. they are used */
  4208. memset(h->cmd_pool_bits, 0,
  4209. DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG)
  4210. * sizeof(unsigned long));
  4211. h->num_luns = 0;
  4212. h->highest_lun = -1;
  4213. for (j = 0; j < CISS_MAX_LUN; j++) {
  4214. h->drv[j] = NULL;
  4215. h->gendisk[j] = NULL;
  4216. }
  4217. cciss_scsi_setup(h);
  4218. /* Turn the interrupts on so we can service requests */
  4219. h->access.set_intr_mask(h, CCISS_INTR_ON);
  4220. /* Get the firmware version */
  4221. inq_buff = kzalloc(sizeof(InquiryData_struct), GFP_KERNEL);
  4222. if (inq_buff == NULL) {
  4223. dev_err(&h->pdev->dev, "out of memory\n");
  4224. goto clean4;
  4225. }
  4226. return_code = sendcmd_withirq(h, CISS_INQUIRY, inq_buff,
  4227. sizeof(InquiryData_struct), 0, CTLR_LUNID, TYPE_CMD);
  4228. if (return_code == IO_OK) {
  4229. h->firm_ver[0] = inq_buff->data_byte[32];
  4230. h->firm_ver[1] = inq_buff->data_byte[33];
  4231. h->firm_ver[2] = inq_buff->data_byte[34];
  4232. h->firm_ver[3] = inq_buff->data_byte[35];
  4233. } else { /* send command failed */
  4234. dev_warn(&h->pdev->dev, "unable to determine firmware"
  4235. " version of controller\n");
  4236. }
  4237. kfree(inq_buff);
  4238. cciss_procinit(h);
  4239. h->cciss_max_sectors = 8192;
  4240. rebuild_lun_table(h, 1, 0);
  4241. h->busy_initializing = 0;
  4242. return 1;
  4243. clean4:
  4244. kfree(h->cmd_pool_bits);
  4245. /* Free up sg elements */
  4246. for (k = 0; k < h->nr_cmds; k++)
  4247. kfree(h->scatter_list[k]);
  4248. kfree(h->scatter_list);
  4249. cciss_free_sg_chain_blocks(h->cmd_sg_list, h->nr_cmds);
  4250. if (h->cmd_pool)
  4251. pci_free_consistent(h->pdev,
  4252. h->nr_cmds * sizeof(CommandList_struct),
  4253. h->cmd_pool, h->cmd_pool_dhandle);
  4254. if (h->errinfo_pool)
  4255. pci_free_consistent(h->pdev,
  4256. h->nr_cmds * sizeof(ErrorInfo_struct),
  4257. h->errinfo_pool,
  4258. h->errinfo_pool_dhandle);
  4259. free_irq(h->intr[PERF_MODE_INT], h);
  4260. clean2:
  4261. unregister_blkdev(h->major, h->devname);
  4262. clean1:
  4263. cciss_destroy_hba_sysfs_entry(h);
  4264. clean0:
  4265. pci_release_regions(pdev);
  4266. clean_no_release_regions:
  4267. h->busy_initializing = 0;
  4268. /*
  4269. * Deliberately omit pci_disable_device(): it does something nasty to
  4270. * Smart Array controllers that pci_enable_device does not undo
  4271. */
  4272. pci_set_drvdata(pdev, NULL);
  4273. free_hba(h);
  4274. return -1;
  4275. }
  4276. static void cciss_shutdown(struct pci_dev *pdev)
  4277. {
  4278. ctlr_info_t *h;
  4279. char *flush_buf;
  4280. int return_code;
  4281. h = pci_get_drvdata(pdev);
  4282. flush_buf = kzalloc(4, GFP_KERNEL);
  4283. if (!flush_buf) {
  4284. dev_warn(&h->pdev->dev, "cache not flushed, out of memory.\n");
  4285. return;
  4286. }
  4287. /* write all data in the battery backed cache to disk */
  4288. memset(flush_buf, 0, 4);
  4289. return_code = sendcmd_withirq(h, CCISS_CACHE_FLUSH, flush_buf,
  4290. 4, 0, CTLR_LUNID, TYPE_CMD);
  4291. kfree(flush_buf);
  4292. if (return_code != IO_OK)
  4293. dev_warn(&h->pdev->dev, "Error flushing cache\n");
  4294. h->access.set_intr_mask(h, CCISS_INTR_OFF);
  4295. free_irq(h->intr[PERF_MODE_INT], h);
  4296. }
  4297. static void __devexit cciss_remove_one(struct pci_dev *pdev)
  4298. {
  4299. ctlr_info_t *h;
  4300. int i, j;
  4301. if (pci_get_drvdata(pdev) == NULL) {
  4302. dev_err(&pdev->dev, "Unable to remove device\n");
  4303. return;
  4304. }
  4305. h = pci_get_drvdata(pdev);
  4306. i = h->ctlr;
  4307. if (hba[i] == NULL) {
  4308. dev_err(&pdev->dev, "device appears to already be removed\n");
  4309. return;
  4310. }
  4311. mutex_lock(&h->busy_shutting_down);
  4312. remove_from_scan_list(h);
  4313. remove_proc_entry(h->devname, proc_cciss);
  4314. unregister_blkdev(h->major, h->devname);
  4315. /* remove it from the disk list */
  4316. for (j = 0; j < CISS_MAX_LUN; j++) {
  4317. struct gendisk *disk = h->gendisk[j];
  4318. if (disk) {
  4319. struct request_queue *q = disk->queue;
  4320. if (disk->flags & GENHD_FL_UP) {
  4321. cciss_destroy_ld_sysfs_entry(h, j, 1);
  4322. del_gendisk(disk);
  4323. }
  4324. if (q)
  4325. blk_cleanup_queue(q);
  4326. }
  4327. }
  4328. #ifdef CONFIG_CISS_SCSI_TAPE
  4329. cciss_unregister_scsi(h); /* unhook from SCSI subsystem */
  4330. #endif
  4331. cciss_shutdown(pdev);
  4332. #ifdef CONFIG_PCI_MSI
  4333. if (h->msix_vector)
  4334. pci_disable_msix(h->pdev);
  4335. else if (h->msi_vector)
  4336. pci_disable_msi(h->pdev);
  4337. #endif /* CONFIG_PCI_MSI */
  4338. iounmap(h->transtable);
  4339. iounmap(h->cfgtable);
  4340. iounmap(h->vaddr);
  4341. pci_free_consistent(h->pdev, h->nr_cmds * sizeof(CommandList_struct),
  4342. h->cmd_pool, h->cmd_pool_dhandle);
  4343. pci_free_consistent(h->pdev, h->nr_cmds * sizeof(ErrorInfo_struct),
  4344. h->errinfo_pool, h->errinfo_pool_dhandle);
  4345. kfree(h->cmd_pool_bits);
  4346. /* Free up sg elements */
  4347. for (j = 0; j < h->nr_cmds; j++)
  4348. kfree(h->scatter_list[j]);
  4349. kfree(h->scatter_list);
  4350. cciss_free_sg_chain_blocks(h->cmd_sg_list, h->nr_cmds);
  4351. /*
  4352. * Deliberately omit pci_disable_device(): it does something nasty to
  4353. * Smart Array controllers that pci_enable_device does not undo
  4354. */
  4355. pci_release_regions(pdev);
  4356. pci_set_drvdata(pdev, NULL);
  4357. cciss_destroy_hba_sysfs_entry(h);
  4358. mutex_unlock(&h->busy_shutting_down);
  4359. free_hba(h);
  4360. }
  4361. static struct pci_driver cciss_pci_driver = {
  4362. .name = "cciss",
  4363. .probe = cciss_init_one,
  4364. .remove = __devexit_p(cciss_remove_one),
  4365. .id_table = cciss_pci_device_id, /* id_table */
  4366. .shutdown = cciss_shutdown,
  4367. };
  4368. /*
  4369. * This is it. Register the PCI driver information for the cards we control
  4370. * the OS will call our registered routines when it finds one of our cards.
  4371. */
  4372. static int __init cciss_init(void)
  4373. {
  4374. int err;
  4375. /*
  4376. * The hardware requires that commands are aligned on a 64-bit
  4377. * boundary. Given that we use pci_alloc_consistent() to allocate an
  4378. * array of them, the size must be a multiple of 8 bytes.
  4379. */
  4380. BUILD_BUG_ON(sizeof(CommandList_struct) % COMMANDLIST_ALIGNMENT);
  4381. printk(KERN_INFO DRIVER_NAME "\n");
  4382. err = bus_register(&cciss_bus_type);
  4383. if (err)
  4384. return err;
  4385. /* Start the scan thread */
  4386. cciss_scan_thread = kthread_run(scan_thread, NULL, "cciss_scan");
  4387. if (IS_ERR(cciss_scan_thread)) {
  4388. err = PTR_ERR(cciss_scan_thread);
  4389. goto err_bus_unregister;
  4390. }
  4391. /* Register for our PCI devices */
  4392. err = pci_register_driver(&cciss_pci_driver);
  4393. if (err)
  4394. goto err_thread_stop;
  4395. return err;
  4396. err_thread_stop:
  4397. kthread_stop(cciss_scan_thread);
  4398. err_bus_unregister:
  4399. bus_unregister(&cciss_bus_type);
  4400. return err;
  4401. }
  4402. static void __exit cciss_cleanup(void)
  4403. {
  4404. int i;
  4405. pci_unregister_driver(&cciss_pci_driver);
  4406. /* double check that all controller entrys have been removed */
  4407. for (i = 0; i < MAX_CTLR; i++) {
  4408. if (hba[i] != NULL) {
  4409. dev_warn(&hba[i]->pdev->dev,
  4410. "had to remove controller\n");
  4411. cciss_remove_one(hba[i]->pdev);
  4412. }
  4413. }
  4414. kthread_stop(cciss_scan_thread);
  4415. remove_proc_entry("driver/cciss", NULL);
  4416. bus_unregister(&cciss_bus_type);
  4417. }
  4418. module_init(cciss_init);
  4419. module_exit(cciss_cleanup);