cciss.c 140 KB

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