cciss.c 123 KB

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