cciss.c 149 KB

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