cciss.c 148 KB

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