cciss.c 148 KB

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