cciss.c 121 KB

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