cciss.c 128 KB

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