cciss.c 128 KB

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