cciss.c 148 KB

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