cciss.c 137 KB

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