cciss.c 138 KB

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