megaraid.c 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121
  1. /*
  2. *
  3. * Linux MegaRAID device driver
  4. *
  5. * Copyright © 2002 LSI Logic Corporation.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
  13. * - fixes
  14. * - speed-ups (list handling fixes, issued_list, optimizations.)
  15. * - lots of cleanups.
  16. *
  17. * Copyright (c) 2003 Christoph Hellwig <hch@lst.de>
  18. * - new-style, hotplug-aware pci probing and scsi registration
  19. *
  20. * Version : v2.00.3 (Feb 19, 2003) - Atul Mukker <Atul.Mukker@lsil.com>
  21. *
  22. * Description: Linux device driver for LSI Logic MegaRAID controller
  23. *
  24. * Supported controllers: MegaRAID 418, 428, 438, 466, 762, 467, 471, 490, 493
  25. * 518, 520, 531, 532
  26. *
  27. * This driver is supported by LSI Logic, with assistance from Red Hat, Dell,
  28. * and others. Please send updates to the mailing list
  29. * linux-scsi@vger.kernel.org .
  30. *
  31. */
  32. #include <linux/mm.h>
  33. #include <linux/fs.h>
  34. #include <linux/blkdev.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/io.h>
  37. #include <linux/delay.h>
  38. #include <linux/proc_fs.h>
  39. #include <linux/reboot.h>
  40. #include <linux/module.h>
  41. #include <linux/list.h>
  42. #include <linux/interrupt.h>
  43. #include <linux/pci.h>
  44. #include <linux/init.h>
  45. #include <scsi/scsicam.h>
  46. #include "scsi.h"
  47. #include <scsi/scsi_host.h>
  48. #include "megaraid.h"
  49. #define MEGARAID_MODULE_VERSION "2.00.3"
  50. MODULE_AUTHOR ("LSI Logic Corporation");
  51. MODULE_DESCRIPTION ("LSI Logic MegaRAID driver");
  52. MODULE_LICENSE ("GPL");
  53. MODULE_VERSION(MEGARAID_MODULE_VERSION);
  54. static unsigned int max_cmd_per_lun = DEF_CMD_PER_LUN;
  55. module_param(max_cmd_per_lun, uint, 0);
  56. MODULE_PARM_DESC(max_cmd_per_lun, "Maximum number of commands which can be issued to a single LUN (default=DEF_CMD_PER_LUN=63)");
  57. static unsigned short int max_sectors_per_io = MAX_SECTORS_PER_IO;
  58. module_param(max_sectors_per_io, ushort, 0);
  59. MODULE_PARM_DESC(max_sectors_per_io, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
  60. static unsigned short int max_mbox_busy_wait = MBOX_BUSY_WAIT;
  61. module_param(max_mbox_busy_wait, ushort, 0);
  62. MODULE_PARM_DESC(max_mbox_busy_wait, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
  63. #define RDINDOOR(adapter) readl((adapter)->base + 0x20)
  64. #define RDOUTDOOR(adapter) readl((adapter)->base + 0x2C)
  65. #define WRINDOOR(adapter,value) writel(value, (adapter)->base + 0x20)
  66. #define WROUTDOOR(adapter,value) writel(value, (adapter)->base + 0x2C)
  67. /*
  68. * Global variables
  69. */
  70. static int hba_count;
  71. static adapter_t *hba_soft_state[MAX_CONTROLLERS];
  72. static struct proc_dir_entry *mega_proc_dir_entry;
  73. /* For controller re-ordering */
  74. static struct mega_hbas mega_hbas[MAX_CONTROLLERS];
  75. /*
  76. * The File Operations structure for the serial/ioctl interface of the driver
  77. */
  78. static struct file_operations megadev_fops = {
  79. .owner = THIS_MODULE,
  80. .ioctl = megadev_ioctl,
  81. .open = megadev_open,
  82. };
  83. /*
  84. * Array to structures for storing the information about the controllers. This
  85. * information is sent to the user level applications, when they do an ioctl
  86. * for this information.
  87. */
  88. static struct mcontroller mcontroller[MAX_CONTROLLERS];
  89. /* The current driver version */
  90. static u32 driver_ver = 0x02000000;
  91. /* major number used by the device for character interface */
  92. static int major;
  93. #define IS_RAID_CH(hba, ch) (((hba)->mega_ch_class >> (ch)) & 0x01)
  94. /*
  95. * Debug variable to print some diagnostic messages
  96. */
  97. static int trace_level;
  98. /**
  99. * mega_setup_mailbox()
  100. * @adapter - pointer to our soft state
  101. *
  102. * Allocates a 8 byte aligned memory for the handshake mailbox.
  103. */
  104. static int
  105. mega_setup_mailbox(adapter_t *adapter)
  106. {
  107. unsigned long align;
  108. adapter->una_mbox64 = pci_alloc_consistent(adapter->dev,
  109. sizeof(mbox64_t), &adapter->una_mbox64_dma);
  110. if( !adapter->una_mbox64 ) return -1;
  111. adapter->mbox = &adapter->una_mbox64->mbox;
  112. adapter->mbox = (mbox_t *)((((unsigned long) adapter->mbox) + 15) &
  113. (~0UL ^ 0xFUL));
  114. adapter->mbox64 = (mbox64_t *)(((unsigned long)adapter->mbox) - 8);
  115. align = ((void *)adapter->mbox) - ((void *)&adapter->una_mbox64->mbox);
  116. adapter->mbox_dma = adapter->una_mbox64_dma + 8 + align;
  117. /*
  118. * Register the mailbox if the controller is an io-mapped controller
  119. */
  120. if( adapter->flag & BOARD_IOMAP ) {
  121. outb_p(adapter->mbox_dma & 0xFF,
  122. adapter->host->io_port + MBOX_PORT0);
  123. outb_p((adapter->mbox_dma >> 8) & 0xFF,
  124. adapter->host->io_port + MBOX_PORT1);
  125. outb_p((adapter->mbox_dma >> 16) & 0xFF,
  126. adapter->host->io_port + MBOX_PORT2);
  127. outb_p((adapter->mbox_dma >> 24) & 0xFF,
  128. adapter->host->io_port + MBOX_PORT3);
  129. outb_p(ENABLE_MBOX_BYTE,
  130. adapter->host->io_port + ENABLE_MBOX_REGION);
  131. irq_ack(adapter);
  132. irq_enable(adapter);
  133. }
  134. return 0;
  135. }
  136. /*
  137. * mega_query_adapter()
  138. * @adapter - pointer to our soft state
  139. *
  140. * Issue the adapter inquiry commands to the controller and find out
  141. * information and parameter about the devices attached
  142. */
  143. static int
  144. mega_query_adapter(adapter_t *adapter)
  145. {
  146. dma_addr_t prod_info_dma_handle;
  147. mega_inquiry3 *inquiry3;
  148. u8 raw_mbox[sizeof(struct mbox_out)];
  149. mbox_t *mbox;
  150. int retval;
  151. /* Initialize adapter inquiry mailbox */
  152. mbox = (mbox_t *)raw_mbox;
  153. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  154. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  155. /*
  156. * Try to issue Inquiry3 command
  157. * if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
  158. * update enquiry3 structure
  159. */
  160. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  161. inquiry3 = (mega_inquiry3 *)adapter->mega_buffer;
  162. raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
  163. raw_mbox[2] = NC_SUBOP_ENQUIRY3; /* i.e. 0x0F */
  164. raw_mbox[3] = ENQ3_GET_SOLICITED_FULL; /* i.e. 0x02 */
  165. /* Issue a blocking command to the card */
  166. if ((retval = issue_scb_block(adapter, raw_mbox))) {
  167. /* the adapter does not support 40ld */
  168. mraid_ext_inquiry *ext_inq;
  169. mraid_inquiry *inq;
  170. dma_addr_t dma_handle;
  171. ext_inq = pci_alloc_consistent(adapter->dev,
  172. sizeof(mraid_ext_inquiry), &dma_handle);
  173. if( ext_inq == NULL ) return -1;
  174. inq = &ext_inq->raid_inq;
  175. mbox->m_out.xferaddr = (u32)dma_handle;
  176. /*issue old 0x04 command to adapter */
  177. mbox->m_out.cmd = MEGA_MBOXCMD_ADPEXTINQ;
  178. issue_scb_block(adapter, raw_mbox);
  179. /*
  180. * update Enquiry3 and ProductInfo structures with
  181. * mraid_inquiry structure
  182. */
  183. mega_8_to_40ld(inq, inquiry3,
  184. (mega_product_info *)&adapter->product_info);
  185. pci_free_consistent(adapter->dev, sizeof(mraid_ext_inquiry),
  186. ext_inq, dma_handle);
  187. } else { /*adapter supports 40ld */
  188. adapter->flag |= BOARD_40LD;
  189. /*
  190. * get product_info, which is static information and will be
  191. * unchanged
  192. */
  193. prod_info_dma_handle = pci_map_single(adapter->dev, (void *)
  194. &adapter->product_info,
  195. sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
  196. mbox->m_out.xferaddr = prod_info_dma_handle;
  197. raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
  198. raw_mbox[2] = NC_SUBOP_PRODUCT_INFO; /* i.e. 0x0E */
  199. if ((retval = issue_scb_block(adapter, raw_mbox)))
  200. printk(KERN_WARNING
  201. "megaraid: Product_info cmd failed with error: %d\n",
  202. retval);
  203. pci_unmap_single(adapter->dev, prod_info_dma_handle,
  204. sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
  205. }
  206. /*
  207. * kernel scans the channels from 0 to <= max_channel
  208. */
  209. adapter->host->max_channel =
  210. adapter->product_info.nchannels + NVIRT_CHAN -1;
  211. adapter->host->max_id = 16; /* max targets per channel */
  212. adapter->host->max_lun = 7; /* Upto 7 luns for non disk devices */
  213. adapter->host->cmd_per_lun = max_cmd_per_lun;
  214. adapter->numldrv = inquiry3->num_ldrv;
  215. adapter->max_cmds = adapter->product_info.max_commands;
  216. if(adapter->max_cmds > MAX_COMMANDS)
  217. adapter->max_cmds = MAX_COMMANDS;
  218. adapter->host->can_queue = adapter->max_cmds - 1;
  219. /*
  220. * Get the maximum number of scatter-gather elements supported by this
  221. * firmware
  222. */
  223. mega_get_max_sgl(adapter);
  224. adapter->host->sg_tablesize = adapter->sglen;
  225. /* use HP firmware and bios version encoding */
  226. if (adapter->product_info.subsysvid == HP_SUBSYS_VID) {
  227. sprintf (adapter->fw_version, "%c%d%d.%d%d",
  228. adapter->product_info.fw_version[2],
  229. adapter->product_info.fw_version[1] >> 8,
  230. adapter->product_info.fw_version[1] & 0x0f,
  231. adapter->product_info.fw_version[0] >> 8,
  232. adapter->product_info.fw_version[0] & 0x0f);
  233. sprintf (adapter->bios_version, "%c%d%d.%d%d",
  234. adapter->product_info.bios_version[2],
  235. adapter->product_info.bios_version[1] >> 8,
  236. adapter->product_info.bios_version[1] & 0x0f,
  237. adapter->product_info.bios_version[0] >> 8,
  238. adapter->product_info.bios_version[0] & 0x0f);
  239. } else {
  240. memcpy(adapter->fw_version,
  241. (char *)adapter->product_info.fw_version, 4);
  242. adapter->fw_version[4] = 0;
  243. memcpy(adapter->bios_version,
  244. (char *)adapter->product_info.bios_version, 4);
  245. adapter->bios_version[4] = 0;
  246. }
  247. printk(KERN_NOTICE "megaraid: [%s:%s] detected %d logical drives.\n",
  248. adapter->fw_version, adapter->bios_version, adapter->numldrv);
  249. /*
  250. * Do we support extended (>10 bytes) cdbs
  251. */
  252. adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
  253. if (adapter->support_ext_cdb)
  254. printk(KERN_NOTICE "megaraid: supports extended CDBs.\n");
  255. return 0;
  256. }
  257. /**
  258. * mega_runpendq()
  259. * @adapter - pointer to our soft state
  260. *
  261. * Runs through the list of pending requests.
  262. */
  263. static inline void
  264. mega_runpendq(adapter_t *adapter)
  265. {
  266. if(!list_empty(&adapter->pending_list))
  267. __mega_runpendq(adapter);
  268. }
  269. /*
  270. * megaraid_queue()
  271. * @scmd - Issue this scsi command
  272. * @done - the callback hook into the scsi mid-layer
  273. *
  274. * The command queuing entry point for the mid-layer.
  275. */
  276. static int
  277. megaraid_queue(Scsi_Cmnd *scmd, void (*done)(Scsi_Cmnd *))
  278. {
  279. adapter_t *adapter;
  280. scb_t *scb;
  281. int busy=0;
  282. adapter = (adapter_t *)scmd->device->host->hostdata;
  283. scmd->scsi_done = done;
  284. /*
  285. * Allocate and build a SCB request
  286. * busy flag will be set if mega_build_cmd() command could not
  287. * allocate scb. We will return non-zero status in that case.
  288. * NOTE: scb can be null even though certain commands completed
  289. * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
  290. * return 0 in that case.
  291. */
  292. scb = mega_build_cmd(adapter, scmd, &busy);
  293. if(scb) {
  294. scb->state |= SCB_PENDQ;
  295. list_add_tail(&scb->list, &adapter->pending_list);
  296. /*
  297. * Check if the HBA is in quiescent state, e.g., during a
  298. * delete logical drive opertion. If it is, don't run
  299. * the pending_list.
  300. */
  301. if(atomic_read(&adapter->quiescent) == 0) {
  302. mega_runpendq(adapter);
  303. }
  304. return 0;
  305. }
  306. return busy;
  307. }
  308. /**
  309. * mega_allocate_scb()
  310. * @adapter - pointer to our soft state
  311. * @cmd - scsi command from the mid-layer
  312. *
  313. * Allocate a SCB structure. This is the central structure for controller
  314. * commands.
  315. */
  316. static inline scb_t *
  317. mega_allocate_scb(adapter_t *adapter, Scsi_Cmnd *cmd)
  318. {
  319. struct list_head *head = &adapter->free_list;
  320. scb_t *scb;
  321. /* Unlink command from Free List */
  322. if( !list_empty(head) ) {
  323. scb = list_entry(head->next, scb_t, list);
  324. list_del_init(head->next);
  325. scb->state = SCB_ACTIVE;
  326. scb->cmd = cmd;
  327. scb->dma_type = MEGA_DMA_TYPE_NONE;
  328. return scb;
  329. }
  330. return NULL;
  331. }
  332. /**
  333. * mega_get_ldrv_num()
  334. * @adapter - pointer to our soft state
  335. * @cmd - scsi mid layer command
  336. * @channel - channel on the controller
  337. *
  338. * Calculate the logical drive number based on the information in scsi command
  339. * and the channel number.
  340. */
  341. static inline int
  342. mega_get_ldrv_num(adapter_t *adapter, Scsi_Cmnd *cmd, int channel)
  343. {
  344. int tgt;
  345. int ldrv_num;
  346. tgt = cmd->device->id;
  347. if ( tgt > adapter->this_id )
  348. tgt--; /* we do not get inquires for initiator id */
  349. ldrv_num = (channel * 15) + tgt;
  350. /*
  351. * If we have a logical drive with boot enabled, project it first
  352. */
  353. if( adapter->boot_ldrv_enabled ) {
  354. if( ldrv_num == 0 ) {
  355. ldrv_num = adapter->boot_ldrv;
  356. }
  357. else {
  358. if( ldrv_num <= adapter->boot_ldrv ) {
  359. ldrv_num--;
  360. }
  361. }
  362. }
  363. /*
  364. * If "delete logical drive" feature is enabled on this controller.
  365. * Do only if at least one delete logical drive operation was done.
  366. *
  367. * Also, after logical drive deletion, instead of logical drive number,
  368. * the value returned should be 0x80+logical drive id.
  369. *
  370. * These is valid only for IO commands.
  371. */
  372. if (adapter->support_random_del && adapter->read_ldidmap )
  373. switch (cmd->cmnd[0]) {
  374. case READ_6: /* fall through */
  375. case WRITE_6: /* fall through */
  376. case READ_10: /* fall through */
  377. case WRITE_10:
  378. ldrv_num += 0x80;
  379. }
  380. return ldrv_num;
  381. }
  382. /**
  383. * mega_build_cmd()
  384. * @adapter - pointer to our soft state
  385. * @cmd - Prepare using this scsi command
  386. * @busy - busy flag if no resources
  387. *
  388. * Prepares a command and scatter gather list for the controller. This routine
  389. * also finds out if the commands is intended for a logical drive or a
  390. * physical device and prepares the controller command accordingly.
  391. *
  392. * We also re-order the logical drives and physical devices based on their
  393. * boot settings.
  394. */
  395. static scb_t *
  396. mega_build_cmd(adapter_t *adapter, Scsi_Cmnd *cmd, int *busy)
  397. {
  398. mega_ext_passthru *epthru;
  399. mega_passthru *pthru;
  400. scb_t *scb;
  401. mbox_t *mbox;
  402. long seg;
  403. char islogical;
  404. int max_ldrv_num;
  405. int channel = 0;
  406. int target = 0;
  407. int ldrv_num = 0; /* logical drive number */
  408. /*
  409. * filter the internal and ioctl commands
  410. */
  411. if((cmd->cmnd[0] == MEGA_INTERNAL_CMD)) {
  412. return cmd->buffer;
  413. }
  414. /*
  415. * We know what channels our logical drives are on - mega_find_card()
  416. */
  417. islogical = adapter->logdrv_chan[cmd->device->channel];
  418. /*
  419. * The theory: If physical drive is chosen for boot, all the physical
  420. * devices are exported before the logical drives, otherwise physical
  421. * devices are pushed after logical drives, in which case - Kernel sees
  422. * the physical devices on virtual channel which is obviously converted
  423. * to actual channel on the HBA.
  424. */
  425. if( adapter->boot_pdrv_enabled ) {
  426. if( islogical ) {
  427. /* logical channel */
  428. channel = cmd->device->channel -
  429. adapter->product_info.nchannels;
  430. }
  431. else {
  432. /* this is physical channel */
  433. channel = cmd->device->channel;
  434. target = cmd->device->id;
  435. /*
  436. * boot from a physical disk, that disk needs to be
  437. * exposed first IF both the channels are SCSI, then
  438. * booting from the second channel is not allowed.
  439. */
  440. if( target == 0 ) {
  441. target = adapter->boot_pdrv_tgt;
  442. }
  443. else if( target == adapter->boot_pdrv_tgt ) {
  444. target = 0;
  445. }
  446. }
  447. }
  448. else {
  449. if( islogical ) {
  450. /* this is the logical channel */
  451. channel = cmd->device->channel;
  452. }
  453. else {
  454. /* physical channel */
  455. channel = cmd->device->channel - NVIRT_CHAN;
  456. target = cmd->device->id;
  457. }
  458. }
  459. if(islogical) {
  460. /* have just LUN 0 for each target on virtual channels */
  461. if (cmd->device->lun) {
  462. cmd->result = (DID_BAD_TARGET << 16);
  463. cmd->scsi_done(cmd);
  464. return NULL;
  465. }
  466. ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
  467. max_ldrv_num = (adapter->flag & BOARD_40LD) ?
  468. MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
  469. /*
  470. * max_ldrv_num increases by 0x80 if some logical drive was
  471. * deleted.
  472. */
  473. if(adapter->read_ldidmap)
  474. max_ldrv_num += 0x80;
  475. if(ldrv_num > max_ldrv_num ) {
  476. cmd->result = (DID_BAD_TARGET << 16);
  477. cmd->scsi_done(cmd);
  478. return NULL;
  479. }
  480. }
  481. else {
  482. if( cmd->device->lun > 7) {
  483. /*
  484. * Do not support lun >7 for physically accessed
  485. * devices
  486. */
  487. cmd->result = (DID_BAD_TARGET << 16);
  488. cmd->scsi_done(cmd);
  489. return NULL;
  490. }
  491. }
  492. /*
  493. *
  494. * Logical drive commands
  495. *
  496. */
  497. if(islogical) {
  498. switch (cmd->cmnd[0]) {
  499. case TEST_UNIT_READY:
  500. memset(cmd->request_buffer, 0, cmd->request_bufflen);
  501. #if MEGA_HAVE_CLUSTERING
  502. /*
  503. * Do we support clustering and is the support enabled
  504. * If no, return success always
  505. */
  506. if( !adapter->has_cluster ) {
  507. cmd->result = (DID_OK << 16);
  508. cmd->scsi_done(cmd);
  509. return NULL;
  510. }
  511. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  512. *busy = 1;
  513. return NULL;
  514. }
  515. scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
  516. scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
  517. scb->raw_mbox[3] = ldrv_num;
  518. scb->dma_direction = PCI_DMA_NONE;
  519. return scb;
  520. #else
  521. cmd->result = (DID_OK << 16);
  522. cmd->scsi_done(cmd);
  523. return NULL;
  524. #endif
  525. case MODE_SENSE:
  526. memset(cmd->request_buffer, 0, cmd->cmnd[4]);
  527. cmd->result = (DID_OK << 16);
  528. cmd->scsi_done(cmd);
  529. return NULL;
  530. case READ_CAPACITY:
  531. case INQUIRY:
  532. if(!(adapter->flag & (1L << cmd->device->channel))) {
  533. printk(KERN_NOTICE
  534. "scsi%d: scanning scsi channel %d ",
  535. adapter->host->host_no,
  536. cmd->device->channel);
  537. printk("for logical drives.\n");
  538. adapter->flag |= (1L << cmd->device->channel);
  539. }
  540. /* Allocate a SCB and initialize passthru */
  541. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  542. *busy = 1;
  543. return NULL;
  544. }
  545. pthru = scb->pthru;
  546. mbox = (mbox_t *)scb->raw_mbox;
  547. memset(mbox, 0, sizeof(scb->raw_mbox));
  548. memset(pthru, 0, sizeof(mega_passthru));
  549. pthru->timeout = 0;
  550. pthru->ars = 1;
  551. pthru->reqsenselen = 14;
  552. pthru->islogical = 1;
  553. pthru->logdrv = ldrv_num;
  554. pthru->cdblen = cmd->cmd_len;
  555. memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
  556. if( adapter->has_64bit_addr ) {
  557. mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
  558. }
  559. else {
  560. mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
  561. }
  562. scb->dma_direction = PCI_DMA_FROMDEVICE;
  563. pthru->numsgelements = mega_build_sglist(adapter, scb,
  564. &pthru->dataxferaddr, &pthru->dataxferlen);
  565. mbox->m_out.xferaddr = scb->pthru_dma_addr;
  566. return scb;
  567. case READ_6:
  568. case WRITE_6:
  569. case READ_10:
  570. case WRITE_10:
  571. case READ_12:
  572. case WRITE_12:
  573. /* Allocate a SCB and initialize mailbox */
  574. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  575. *busy = 1;
  576. return NULL;
  577. }
  578. mbox = (mbox_t *)scb->raw_mbox;
  579. memset(mbox, 0, sizeof(scb->raw_mbox));
  580. mbox->m_out.logdrv = ldrv_num;
  581. /*
  582. * A little hack: 2nd bit is zero for all scsi read
  583. * commands and is set for all scsi write commands
  584. */
  585. if( adapter->has_64bit_addr ) {
  586. mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
  587. MEGA_MBOXCMD_LWRITE64:
  588. MEGA_MBOXCMD_LREAD64 ;
  589. }
  590. else {
  591. mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
  592. MEGA_MBOXCMD_LWRITE:
  593. MEGA_MBOXCMD_LREAD ;
  594. }
  595. /*
  596. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  597. */
  598. if( cmd->cmd_len == 6 ) {
  599. mbox->m_out.numsectors = (u32) cmd->cmnd[4];
  600. mbox->m_out.lba =
  601. ((u32)cmd->cmnd[1] << 16) |
  602. ((u32)cmd->cmnd[2] << 8) |
  603. (u32)cmd->cmnd[3];
  604. mbox->m_out.lba &= 0x1FFFFF;
  605. #if MEGA_HAVE_STATS
  606. /*
  607. * Take modulo 0x80, since the logical drive
  608. * number increases by 0x80 when a logical
  609. * drive was deleted
  610. */
  611. if (*cmd->cmnd == READ_6) {
  612. adapter->nreads[ldrv_num%0x80]++;
  613. adapter->nreadblocks[ldrv_num%0x80] +=
  614. mbox->m_out.numsectors;
  615. } else {
  616. adapter->nwrites[ldrv_num%0x80]++;
  617. adapter->nwriteblocks[ldrv_num%0x80] +=
  618. mbox->m_out.numsectors;
  619. }
  620. #endif
  621. }
  622. /*
  623. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  624. */
  625. if( cmd->cmd_len == 10 ) {
  626. mbox->m_out.numsectors =
  627. (u32)cmd->cmnd[8] |
  628. ((u32)cmd->cmnd[7] << 8);
  629. mbox->m_out.lba =
  630. ((u32)cmd->cmnd[2] << 24) |
  631. ((u32)cmd->cmnd[3] << 16) |
  632. ((u32)cmd->cmnd[4] << 8) |
  633. (u32)cmd->cmnd[5];
  634. #if MEGA_HAVE_STATS
  635. if (*cmd->cmnd == READ_10) {
  636. adapter->nreads[ldrv_num%0x80]++;
  637. adapter->nreadblocks[ldrv_num%0x80] +=
  638. mbox->m_out.numsectors;
  639. } else {
  640. adapter->nwrites[ldrv_num%0x80]++;
  641. adapter->nwriteblocks[ldrv_num%0x80] +=
  642. mbox->m_out.numsectors;
  643. }
  644. #endif
  645. }
  646. /*
  647. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  648. */
  649. if( cmd->cmd_len == 12 ) {
  650. mbox->m_out.lba =
  651. ((u32)cmd->cmnd[2] << 24) |
  652. ((u32)cmd->cmnd[3] << 16) |
  653. ((u32)cmd->cmnd[4] << 8) |
  654. (u32)cmd->cmnd[5];
  655. mbox->m_out.numsectors =
  656. ((u32)cmd->cmnd[6] << 24) |
  657. ((u32)cmd->cmnd[7] << 16) |
  658. ((u32)cmd->cmnd[8] << 8) |
  659. (u32)cmd->cmnd[9];
  660. #if MEGA_HAVE_STATS
  661. if (*cmd->cmnd == READ_12) {
  662. adapter->nreads[ldrv_num%0x80]++;
  663. adapter->nreadblocks[ldrv_num%0x80] +=
  664. mbox->m_out.numsectors;
  665. } else {
  666. adapter->nwrites[ldrv_num%0x80]++;
  667. adapter->nwriteblocks[ldrv_num%0x80] +=
  668. mbox->m_out.numsectors;
  669. }
  670. #endif
  671. }
  672. /*
  673. * If it is a read command
  674. */
  675. if( (*cmd->cmnd & 0x0F) == 0x08 ) {
  676. scb->dma_direction = PCI_DMA_FROMDEVICE;
  677. }
  678. else {
  679. scb->dma_direction = PCI_DMA_TODEVICE;
  680. }
  681. /* Calculate Scatter-Gather info */
  682. mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
  683. (u32 *)&mbox->m_out.xferaddr, (u32 *)&seg);
  684. return scb;
  685. #if MEGA_HAVE_CLUSTERING
  686. case RESERVE: /* Fall through */
  687. case RELEASE:
  688. /*
  689. * Do we support clustering and is the support enabled
  690. */
  691. if( ! adapter->has_cluster ) {
  692. cmd->result = (DID_BAD_TARGET << 16);
  693. cmd->scsi_done(cmd);
  694. return NULL;
  695. }
  696. /* Allocate a SCB and initialize mailbox */
  697. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  698. *busy = 1;
  699. return NULL;
  700. }
  701. scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
  702. scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
  703. MEGA_RESERVE_LD : MEGA_RELEASE_LD;
  704. scb->raw_mbox[3] = ldrv_num;
  705. scb->dma_direction = PCI_DMA_NONE;
  706. return scb;
  707. #endif
  708. default:
  709. cmd->result = (DID_BAD_TARGET << 16);
  710. cmd->scsi_done(cmd);
  711. return NULL;
  712. }
  713. }
  714. /*
  715. * Passthru drive commands
  716. */
  717. else {
  718. /* Allocate a SCB and initialize passthru */
  719. if(!(scb = mega_allocate_scb(adapter, cmd))) {
  720. *busy = 1;
  721. return NULL;
  722. }
  723. mbox = (mbox_t *)scb->raw_mbox;
  724. memset(mbox, 0, sizeof(scb->raw_mbox));
  725. if( adapter->support_ext_cdb ) {
  726. epthru = mega_prepare_extpassthru(adapter, scb, cmd,
  727. channel, target);
  728. mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
  729. mbox->m_out.xferaddr = scb->epthru_dma_addr;
  730. }
  731. else {
  732. pthru = mega_prepare_passthru(adapter, scb, cmd,
  733. channel, target);
  734. /* Initialize mailbox */
  735. if( adapter->has_64bit_addr ) {
  736. mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
  737. }
  738. else {
  739. mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
  740. }
  741. mbox->m_out.xferaddr = scb->pthru_dma_addr;
  742. }
  743. return scb;
  744. }
  745. return NULL;
  746. }
  747. /**
  748. * mega_prepare_passthru()
  749. * @adapter - pointer to our soft state
  750. * @scb - our scsi control block
  751. * @cmd - scsi command from the mid-layer
  752. * @channel - actual channel on the controller
  753. * @target - actual id on the controller.
  754. *
  755. * prepare a command for the scsi physical devices.
  756. */
  757. static mega_passthru *
  758. mega_prepare_passthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
  759. int channel, int target)
  760. {
  761. mega_passthru *pthru;
  762. pthru = scb->pthru;
  763. memset(pthru, 0, sizeof (mega_passthru));
  764. /* 0=6sec/1=60sec/2=10min/3=3hrs */
  765. pthru->timeout = 2;
  766. pthru->ars = 1;
  767. pthru->reqsenselen = 14;
  768. pthru->islogical = 0;
  769. pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
  770. pthru->target = (adapter->flag & BOARD_40LD) ?
  771. (channel << 4) | target : target;
  772. pthru->cdblen = cmd->cmd_len;
  773. pthru->logdrv = cmd->device->lun;
  774. memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
  775. /* Not sure about the direction */
  776. scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
  777. /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
  778. switch (cmd->cmnd[0]) {
  779. case INQUIRY:
  780. case READ_CAPACITY:
  781. if(!(adapter->flag & (1L << cmd->device->channel))) {
  782. printk(KERN_NOTICE
  783. "scsi%d: scanning scsi channel %d [P%d] ",
  784. adapter->host->host_no,
  785. cmd->device->channel, channel);
  786. printk("for physical devices.\n");
  787. adapter->flag |= (1L << cmd->device->channel);
  788. }
  789. /* Fall through */
  790. default:
  791. pthru->numsgelements = mega_build_sglist(adapter, scb,
  792. &pthru->dataxferaddr, &pthru->dataxferlen);
  793. break;
  794. }
  795. return pthru;
  796. }
  797. /**
  798. * mega_prepare_extpassthru()
  799. * @adapter - pointer to our soft state
  800. * @scb - our scsi control block
  801. * @cmd - scsi command from the mid-layer
  802. * @channel - actual channel on the controller
  803. * @target - actual id on the controller.
  804. *
  805. * prepare a command for the scsi physical devices. This rountine prepares
  806. * commands for devices which can take extended CDBs (>10 bytes)
  807. */
  808. static mega_ext_passthru *
  809. mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
  810. int channel, int target)
  811. {
  812. mega_ext_passthru *epthru;
  813. epthru = scb->epthru;
  814. memset(epthru, 0, sizeof(mega_ext_passthru));
  815. /* 0=6sec/1=60sec/2=10min/3=3hrs */
  816. epthru->timeout = 2;
  817. epthru->ars = 1;
  818. epthru->reqsenselen = 14;
  819. epthru->islogical = 0;
  820. epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
  821. epthru->target = (adapter->flag & BOARD_40LD) ?
  822. (channel << 4) | target : target;
  823. epthru->cdblen = cmd->cmd_len;
  824. epthru->logdrv = cmd->device->lun;
  825. memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
  826. /* Not sure about the direction */
  827. scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
  828. switch(cmd->cmnd[0]) {
  829. case INQUIRY:
  830. case READ_CAPACITY:
  831. if(!(adapter->flag & (1L << cmd->device->channel))) {
  832. printk(KERN_NOTICE
  833. "scsi%d: scanning scsi channel %d [P%d] ",
  834. adapter->host->host_no,
  835. cmd->device->channel, channel);
  836. printk("for physical devices.\n");
  837. adapter->flag |= (1L << cmd->device->channel);
  838. }
  839. /* Fall through */
  840. default:
  841. epthru->numsgelements = mega_build_sglist(adapter, scb,
  842. &epthru->dataxferaddr, &epthru->dataxferlen);
  843. break;
  844. }
  845. return epthru;
  846. }
  847. static void
  848. __mega_runpendq(adapter_t *adapter)
  849. {
  850. scb_t *scb;
  851. struct list_head *pos, *next;
  852. /* Issue any pending commands to the card */
  853. list_for_each_safe(pos, next, &adapter->pending_list) {
  854. scb = list_entry(pos, scb_t, list);
  855. if( !(scb->state & SCB_ISSUED) ) {
  856. if( issue_scb(adapter, scb) != 0 )
  857. return;
  858. }
  859. }
  860. return;
  861. }
  862. /**
  863. * issue_scb()
  864. * @adapter - pointer to our soft state
  865. * @scb - scsi control block
  866. *
  867. * Post a command to the card if the mailbox is available, otherwise return
  868. * busy. We also take the scb from the pending list if the mailbox is
  869. * available.
  870. */
  871. static int
  872. issue_scb(adapter_t *adapter, scb_t *scb)
  873. {
  874. volatile mbox64_t *mbox64 = adapter->mbox64;
  875. volatile mbox_t *mbox = adapter->mbox;
  876. unsigned int i = 0;
  877. if(unlikely(mbox->m_in.busy)) {
  878. do {
  879. udelay(1);
  880. i++;
  881. } while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
  882. if(mbox->m_in.busy) return -1;
  883. }
  884. /* Copy mailbox data into host structure */
  885. memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox,
  886. sizeof(struct mbox_out));
  887. mbox->m_out.cmdid = scb->idx; /* Set cmdid */
  888. mbox->m_in.busy = 1; /* Set busy */
  889. /*
  890. * Increment the pending queue counter
  891. */
  892. atomic_inc(&adapter->pend_cmds);
  893. switch (mbox->m_out.cmd) {
  894. case MEGA_MBOXCMD_LREAD64:
  895. case MEGA_MBOXCMD_LWRITE64:
  896. case MEGA_MBOXCMD_PASSTHRU64:
  897. case MEGA_MBOXCMD_EXTPTHRU:
  898. mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
  899. mbox64->xfer_segment_hi = 0;
  900. mbox->m_out.xferaddr = 0xFFFFFFFF;
  901. break;
  902. default:
  903. mbox64->xfer_segment_lo = 0;
  904. mbox64->xfer_segment_hi = 0;
  905. }
  906. /*
  907. * post the command
  908. */
  909. scb->state |= SCB_ISSUED;
  910. if( likely(adapter->flag & BOARD_MEMMAP) ) {
  911. mbox->m_in.poll = 0;
  912. mbox->m_in.ack = 0;
  913. WRINDOOR(adapter, adapter->mbox_dma | 0x1);
  914. }
  915. else {
  916. irq_enable(adapter);
  917. issue_command(adapter);
  918. }
  919. return 0;
  920. }
  921. /*
  922. * Wait until the controller's mailbox is available
  923. */
  924. static inline int
  925. mega_busywait_mbox (adapter_t *adapter)
  926. {
  927. if (adapter->mbox->m_in.busy)
  928. return __mega_busywait_mbox(adapter);
  929. return 0;
  930. }
  931. /**
  932. * issue_scb_block()
  933. * @adapter - pointer to our soft state
  934. * @raw_mbox - the mailbox
  935. *
  936. * Issue a scb in synchronous and non-interrupt mode
  937. */
  938. static int
  939. issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
  940. {
  941. volatile mbox64_t *mbox64 = adapter->mbox64;
  942. volatile mbox_t *mbox = adapter->mbox;
  943. u8 byte;
  944. /* Wait until mailbox is free */
  945. if(mega_busywait_mbox (adapter))
  946. goto bug_blocked_mailbox;
  947. /* Copy mailbox data into host structure */
  948. memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
  949. mbox->m_out.cmdid = 0xFE;
  950. mbox->m_in.busy = 1;
  951. switch (raw_mbox[0]) {
  952. case MEGA_MBOXCMD_LREAD64:
  953. case MEGA_MBOXCMD_LWRITE64:
  954. case MEGA_MBOXCMD_PASSTHRU64:
  955. case MEGA_MBOXCMD_EXTPTHRU:
  956. mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
  957. mbox64->xfer_segment_hi = 0;
  958. mbox->m_out.xferaddr = 0xFFFFFFFF;
  959. break;
  960. default:
  961. mbox64->xfer_segment_lo = 0;
  962. mbox64->xfer_segment_hi = 0;
  963. }
  964. if( likely(adapter->flag & BOARD_MEMMAP) ) {
  965. mbox->m_in.poll = 0;
  966. mbox->m_in.ack = 0;
  967. mbox->m_in.numstatus = 0xFF;
  968. mbox->m_in.status = 0xFF;
  969. WRINDOOR(adapter, adapter->mbox_dma | 0x1);
  970. while((volatile u8)mbox->m_in.numstatus == 0xFF)
  971. cpu_relax();
  972. mbox->m_in.numstatus = 0xFF;
  973. while( (volatile u8)mbox->m_in.poll != 0x77 )
  974. cpu_relax();
  975. mbox->m_in.poll = 0;
  976. mbox->m_in.ack = 0x77;
  977. WRINDOOR(adapter, adapter->mbox_dma | 0x2);
  978. while(RDINDOOR(adapter) & 0x2)
  979. cpu_relax();
  980. }
  981. else {
  982. irq_disable(adapter);
  983. issue_command(adapter);
  984. while (!((byte = irq_state(adapter)) & INTR_VALID))
  985. cpu_relax();
  986. set_irq_state(adapter, byte);
  987. irq_enable(adapter);
  988. irq_ack(adapter);
  989. }
  990. return mbox->m_in.status;
  991. bug_blocked_mailbox:
  992. printk(KERN_WARNING "megaraid: Blocked mailbox......!!\n");
  993. udelay (1000);
  994. return -1;
  995. }
  996. /**
  997. * megaraid_isr_iomapped()
  998. * @irq - irq
  999. * @devp - pointer to our soft state
  1000. * @regs - unused
  1001. *
  1002. * Interrupt service routine for io-mapped controllers.
  1003. * Find out if our device is interrupting. If yes, acknowledge the interrupt
  1004. * and service the completed commands.
  1005. */
  1006. static irqreturn_t
  1007. megaraid_isr_iomapped(int irq, void *devp, struct pt_regs *regs)
  1008. {
  1009. adapter_t *adapter = devp;
  1010. unsigned long flags;
  1011. u8 status;
  1012. u8 nstatus;
  1013. u8 completed[MAX_FIRMWARE_STATUS];
  1014. u8 byte;
  1015. int handled = 0;
  1016. /*
  1017. * loop till F/W has more commands for us to complete.
  1018. */
  1019. spin_lock_irqsave(&adapter->lock, flags);
  1020. do {
  1021. /* Check if a valid interrupt is pending */
  1022. byte = irq_state(adapter);
  1023. if( (byte & VALID_INTR_BYTE) == 0 ) {
  1024. /*
  1025. * No more pending commands
  1026. */
  1027. goto out_unlock;
  1028. }
  1029. set_irq_state(adapter, byte);
  1030. while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
  1031. == 0xFF)
  1032. cpu_relax();
  1033. adapter->mbox->m_in.numstatus = 0xFF;
  1034. status = adapter->mbox->m_in.status;
  1035. /*
  1036. * decrement the pending queue counter
  1037. */
  1038. atomic_sub(nstatus, &adapter->pend_cmds);
  1039. memcpy(completed, (void *)adapter->mbox->m_in.completed,
  1040. nstatus);
  1041. /* Acknowledge interrupt */
  1042. irq_ack(adapter);
  1043. mega_cmd_done(adapter, completed, nstatus, status);
  1044. mega_rundoneq(adapter);
  1045. handled = 1;
  1046. /* Loop through any pending requests */
  1047. if(atomic_read(&adapter->quiescent) == 0) {
  1048. mega_runpendq(adapter);
  1049. }
  1050. } while(1);
  1051. out_unlock:
  1052. spin_unlock_irqrestore(&adapter->lock, flags);
  1053. return IRQ_RETVAL(handled);
  1054. }
  1055. /**
  1056. * megaraid_isr_memmapped()
  1057. * @irq - irq
  1058. * @devp - pointer to our soft state
  1059. * @regs - unused
  1060. *
  1061. * Interrupt service routine for memory-mapped controllers.
  1062. * Find out if our device is interrupting. If yes, acknowledge the interrupt
  1063. * and service the completed commands.
  1064. */
  1065. static irqreturn_t
  1066. megaraid_isr_memmapped(int irq, void *devp, struct pt_regs *regs)
  1067. {
  1068. adapter_t *adapter = devp;
  1069. unsigned long flags;
  1070. u8 status;
  1071. u32 dword = 0;
  1072. u8 nstatus;
  1073. u8 completed[MAX_FIRMWARE_STATUS];
  1074. int handled = 0;
  1075. /*
  1076. * loop till F/W has more commands for us to complete.
  1077. */
  1078. spin_lock_irqsave(&adapter->lock, flags);
  1079. do {
  1080. /* Check if a valid interrupt is pending */
  1081. dword = RDOUTDOOR(adapter);
  1082. if(dword != 0x10001234) {
  1083. /*
  1084. * No more pending commands
  1085. */
  1086. goto out_unlock;
  1087. }
  1088. WROUTDOOR(adapter, 0x10001234);
  1089. while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
  1090. == 0xFF) {
  1091. cpu_relax();
  1092. }
  1093. adapter->mbox->m_in.numstatus = 0xFF;
  1094. status = adapter->mbox->m_in.status;
  1095. /*
  1096. * decrement the pending queue counter
  1097. */
  1098. atomic_sub(nstatus, &adapter->pend_cmds);
  1099. memcpy(completed, (void *)adapter->mbox->m_in.completed,
  1100. nstatus);
  1101. /* Acknowledge interrupt */
  1102. WRINDOOR(adapter, 0x2);
  1103. handled = 1;
  1104. while( RDINDOOR(adapter) & 0x02 ) cpu_relax();
  1105. mega_cmd_done(adapter, completed, nstatus, status);
  1106. mega_rundoneq(adapter);
  1107. /* Loop through any pending requests */
  1108. if(atomic_read(&adapter->quiescent) == 0) {
  1109. mega_runpendq(adapter);
  1110. }
  1111. } while(1);
  1112. out_unlock:
  1113. spin_unlock_irqrestore(&adapter->lock, flags);
  1114. return IRQ_RETVAL(handled);
  1115. }
  1116. /**
  1117. * mega_cmd_done()
  1118. * @adapter - pointer to our soft state
  1119. * @completed - array of ids of completed commands
  1120. * @nstatus - number of completed commands
  1121. * @status - status of the last command completed
  1122. *
  1123. * Complete the comamnds and call the scsi mid-layer callback hooks.
  1124. */
  1125. static void
  1126. mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
  1127. {
  1128. mega_ext_passthru *epthru = NULL;
  1129. struct scatterlist *sgl;
  1130. Scsi_Cmnd *cmd = NULL;
  1131. mega_passthru *pthru = NULL;
  1132. mbox_t *mbox = NULL;
  1133. u8 c;
  1134. scb_t *scb;
  1135. int islogical;
  1136. int cmdid;
  1137. int i;
  1138. /*
  1139. * for all the commands completed, call the mid-layer callback routine
  1140. * and free the scb.
  1141. */
  1142. for( i = 0; i < nstatus; i++ ) {
  1143. cmdid = completed[i];
  1144. if( cmdid == CMDID_INT_CMDS ) { /* internal command */
  1145. scb = &adapter->int_scb;
  1146. cmd = scb->cmd;
  1147. mbox = (mbox_t *)scb->raw_mbox;
  1148. /*
  1149. * Internal command interface do not fire the extended
  1150. * passthru or 64-bit passthru
  1151. */
  1152. pthru = scb->pthru;
  1153. }
  1154. else {
  1155. scb = &adapter->scb_list[cmdid];
  1156. /*
  1157. * Make sure f/w has completed a valid command
  1158. */
  1159. if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
  1160. printk(KERN_CRIT
  1161. "megaraid: invalid command ");
  1162. printk("Id %d, scb->state:%x, scsi cmd:%p\n",
  1163. cmdid, scb->state, scb->cmd);
  1164. continue;
  1165. }
  1166. /*
  1167. * Was a abort issued for this command
  1168. */
  1169. if( scb->state & SCB_ABORT ) {
  1170. printk(KERN_WARNING
  1171. "megaraid: aborted cmd %lx[%x] complete.\n",
  1172. scb->cmd->serial_number, scb->idx);
  1173. scb->cmd->result = (DID_ABORT << 16);
  1174. list_add_tail(SCSI_LIST(scb->cmd),
  1175. &adapter->completed_list);
  1176. mega_free_scb(adapter, scb);
  1177. continue;
  1178. }
  1179. /*
  1180. * Was a reset issued for this command
  1181. */
  1182. if( scb->state & SCB_RESET ) {
  1183. printk(KERN_WARNING
  1184. "megaraid: reset cmd %lx[%x] complete.\n",
  1185. scb->cmd->serial_number, scb->idx);
  1186. scb->cmd->result = (DID_RESET << 16);
  1187. list_add_tail(SCSI_LIST(scb->cmd),
  1188. &adapter->completed_list);
  1189. mega_free_scb (adapter, scb);
  1190. continue;
  1191. }
  1192. cmd = scb->cmd;
  1193. pthru = scb->pthru;
  1194. epthru = scb->epthru;
  1195. mbox = (mbox_t *)scb->raw_mbox;
  1196. #if MEGA_HAVE_STATS
  1197. {
  1198. int logdrv = mbox->m_out.logdrv;
  1199. islogical = adapter->logdrv_chan[cmd->channel];
  1200. /*
  1201. * Maintain an error counter for the logical drive.
  1202. * Some application like SNMP agent need such
  1203. * statistics
  1204. */
  1205. if( status && islogical && (cmd->cmnd[0] == READ_6 ||
  1206. cmd->cmnd[0] == READ_10 ||
  1207. cmd->cmnd[0] == READ_12)) {
  1208. /*
  1209. * Logical drive number increases by 0x80 when
  1210. * a logical drive is deleted
  1211. */
  1212. adapter->rd_errors[logdrv%0x80]++;
  1213. }
  1214. if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
  1215. cmd->cmnd[0] == WRITE_10 ||
  1216. cmd->cmnd[0] == WRITE_12)) {
  1217. /*
  1218. * Logical drive number increases by 0x80 when
  1219. * a logical drive is deleted
  1220. */
  1221. adapter->wr_errors[logdrv%0x80]++;
  1222. }
  1223. }
  1224. #endif
  1225. }
  1226. /*
  1227. * Do not return the presence of hard disk on the channel so,
  1228. * inquiry sent, and returned data==hard disk or removable
  1229. * hard disk and not logical, request should return failure! -
  1230. * PJ
  1231. */
  1232. islogical = adapter->logdrv_chan[cmd->device->channel];
  1233. if( cmd->cmnd[0] == INQUIRY && !islogical ) {
  1234. if( cmd->use_sg ) {
  1235. sgl = (struct scatterlist *)
  1236. cmd->request_buffer;
  1237. if( sgl->page ) {
  1238. c = *(unsigned char *)
  1239. page_address((&sgl[0])->page) +
  1240. (&sgl[0])->offset;
  1241. }
  1242. else {
  1243. printk(KERN_WARNING
  1244. "megaraid: invalid sg.\n");
  1245. c = 0;
  1246. }
  1247. }
  1248. else {
  1249. c = *(u8 *)cmd->request_buffer;
  1250. }
  1251. if(IS_RAID_CH(adapter, cmd->device->channel) &&
  1252. ((c & 0x1F ) == TYPE_DISK)) {
  1253. status = 0xF0;
  1254. }
  1255. }
  1256. /* clear result; otherwise, success returns corrupt value */
  1257. cmd->result = 0;
  1258. /* Convert MegaRAID status to Linux error code */
  1259. switch (status) {
  1260. case 0x00: /* SUCCESS , i.e. SCSI_STATUS_GOOD */
  1261. cmd->result |= (DID_OK << 16);
  1262. break;
  1263. case 0x02: /* ERROR_ABORTED, i.e.
  1264. SCSI_STATUS_CHECK_CONDITION */
  1265. /* set sense_buffer and result fields */
  1266. if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
  1267. mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
  1268. memcpy(cmd->sense_buffer, pthru->reqsensearea,
  1269. 14);
  1270. cmd->result = (DRIVER_SENSE << 24) |
  1271. (DID_OK << 16) |
  1272. (CHECK_CONDITION << 1);
  1273. }
  1274. else {
  1275. if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
  1276. memcpy(cmd->sense_buffer,
  1277. epthru->reqsensearea, 14);
  1278. cmd->result = (DRIVER_SENSE << 24) |
  1279. (DID_OK << 16) |
  1280. (CHECK_CONDITION << 1);
  1281. } else {
  1282. cmd->sense_buffer[0] = 0x70;
  1283. cmd->sense_buffer[2] = ABORTED_COMMAND;
  1284. cmd->result |= (CHECK_CONDITION << 1);
  1285. }
  1286. }
  1287. break;
  1288. case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e.
  1289. SCSI_STATUS_BUSY */
  1290. cmd->result |= (DID_BUS_BUSY << 16) | status;
  1291. break;
  1292. default:
  1293. #if MEGA_HAVE_CLUSTERING
  1294. /*
  1295. * If TEST_UNIT_READY fails, we know
  1296. * MEGA_RESERVATION_STATUS failed
  1297. */
  1298. if( cmd->cmnd[0] == TEST_UNIT_READY ) {
  1299. cmd->result |= (DID_ERROR << 16) |
  1300. (RESERVATION_CONFLICT << 1);
  1301. }
  1302. else
  1303. /*
  1304. * Error code returned is 1 if Reserve or Release
  1305. * failed or the input parameter is invalid
  1306. */
  1307. if( status == 1 &&
  1308. (cmd->cmnd[0] == RESERVE ||
  1309. cmd->cmnd[0] == RELEASE) ) {
  1310. cmd->result |= (DID_ERROR << 16) |
  1311. (RESERVATION_CONFLICT << 1);
  1312. }
  1313. else
  1314. #endif
  1315. cmd->result |= (DID_BAD_TARGET << 16)|status;
  1316. }
  1317. /*
  1318. * Only free SCBs for the commands coming down from the
  1319. * mid-layer, not for which were issued internally
  1320. *
  1321. * For internal command, restore the status returned by the
  1322. * firmware so that user can interpret it.
  1323. */
  1324. if( cmdid == CMDID_INT_CMDS ) { /* internal command */
  1325. cmd->result = status;
  1326. /*
  1327. * Remove the internal command from the pending list
  1328. */
  1329. list_del_init(&scb->list);
  1330. scb->state = SCB_FREE;
  1331. }
  1332. else {
  1333. mega_free_scb(adapter, scb);
  1334. }
  1335. /* Add Scsi_Command to end of completed queue */
  1336. list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
  1337. }
  1338. }
  1339. /*
  1340. * mega_runpendq()
  1341. *
  1342. * Run through the list of completed requests and finish it
  1343. */
  1344. static void
  1345. mega_rundoneq (adapter_t *adapter)
  1346. {
  1347. Scsi_Cmnd *cmd;
  1348. struct list_head *pos;
  1349. list_for_each(pos, &adapter->completed_list) {
  1350. Scsi_Pointer* spos = (Scsi_Pointer *)pos;
  1351. cmd = list_entry(spos, Scsi_Cmnd, SCp);
  1352. cmd->scsi_done(cmd);
  1353. }
  1354. INIT_LIST_HEAD(&adapter->completed_list);
  1355. }
  1356. /*
  1357. * Free a SCB structure
  1358. * Note: We assume the scsi commands associated with this scb is not free yet.
  1359. */
  1360. static void
  1361. mega_free_scb(adapter_t *adapter, scb_t *scb)
  1362. {
  1363. switch( scb->dma_type ) {
  1364. case MEGA_DMA_TYPE_NONE:
  1365. break;
  1366. case MEGA_BULK_DATA:
  1367. pci_unmap_page(adapter->dev, scb->dma_h_bulkdata,
  1368. scb->cmd->request_bufflen, scb->dma_direction);
  1369. break;
  1370. case MEGA_SGLIST:
  1371. pci_unmap_sg(adapter->dev, scb->cmd->request_buffer,
  1372. scb->cmd->use_sg, scb->dma_direction);
  1373. break;
  1374. default:
  1375. break;
  1376. }
  1377. /*
  1378. * Remove from the pending list
  1379. */
  1380. list_del_init(&scb->list);
  1381. /* Link the scb back into free list */
  1382. scb->state = SCB_FREE;
  1383. scb->cmd = NULL;
  1384. list_add(&scb->list, &adapter->free_list);
  1385. }
  1386. static int
  1387. __mega_busywait_mbox (adapter_t *adapter)
  1388. {
  1389. volatile mbox_t *mbox = adapter->mbox;
  1390. long counter;
  1391. for (counter = 0; counter < 10000; counter++) {
  1392. if (!mbox->m_in.busy)
  1393. return 0;
  1394. udelay(100); yield();
  1395. }
  1396. return -1; /* give up after 1 second */
  1397. }
  1398. /*
  1399. * Copies data to SGLIST
  1400. * Note: For 64 bit cards, we need a minimum of one SG element for read/write
  1401. */
  1402. static int
  1403. mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
  1404. {
  1405. struct scatterlist *sgl;
  1406. struct page *page;
  1407. unsigned long offset;
  1408. Scsi_Cmnd *cmd;
  1409. int sgcnt;
  1410. int idx;
  1411. cmd = scb->cmd;
  1412. /* Scatter-gather not used */
  1413. if( !cmd->use_sg ) {
  1414. page = virt_to_page(cmd->request_buffer);
  1415. offset = offset_in_page(cmd->request_buffer);
  1416. scb->dma_h_bulkdata = pci_map_page(adapter->dev,
  1417. page, offset,
  1418. cmd->request_bufflen,
  1419. scb->dma_direction);
  1420. scb->dma_type = MEGA_BULK_DATA;
  1421. /*
  1422. * We need to handle special 64-bit commands that need a
  1423. * minimum of 1 SG
  1424. */
  1425. if( adapter->has_64bit_addr ) {
  1426. scb->sgl64[0].address = scb->dma_h_bulkdata;
  1427. scb->sgl64[0].length = cmd->request_bufflen;
  1428. *buf = (u32)scb->sgl_dma_addr;
  1429. *len = (u32)cmd->request_bufflen;
  1430. return 1;
  1431. }
  1432. else {
  1433. *buf = (u32)scb->dma_h_bulkdata;
  1434. *len = (u32)cmd->request_bufflen;
  1435. }
  1436. return 0;
  1437. }
  1438. sgl = (struct scatterlist *)cmd->request_buffer;
  1439. /*
  1440. * Copy Scatter-Gather list info into controller structure.
  1441. *
  1442. * The number of sg elements returned must not exceed our limit
  1443. */
  1444. sgcnt = pci_map_sg(adapter->dev, sgl, cmd->use_sg,
  1445. scb->dma_direction);
  1446. scb->dma_type = MEGA_SGLIST;
  1447. if( sgcnt > adapter->sglen ) BUG();
  1448. for( idx = 0; idx < sgcnt; idx++, sgl++ ) {
  1449. if( adapter->has_64bit_addr ) {
  1450. scb->sgl64[idx].address = sg_dma_address(sgl);
  1451. scb->sgl64[idx].length = sg_dma_len(sgl);
  1452. }
  1453. else {
  1454. scb->sgl[idx].address = sg_dma_address(sgl);
  1455. scb->sgl[idx].length = sg_dma_len(sgl);
  1456. }
  1457. }
  1458. /* Reset pointer and length fields */
  1459. *buf = scb->sgl_dma_addr;
  1460. /*
  1461. * For passthru command, dataxferlen must be set, even for commands
  1462. * with a sg list
  1463. */
  1464. *len = (u32)cmd->request_bufflen;
  1465. /* Return count of SG requests */
  1466. return sgcnt;
  1467. }
  1468. /*
  1469. * mega_8_to_40ld()
  1470. *
  1471. * takes all info in AdapterInquiry structure and puts it into ProductInfo and
  1472. * Enquiry3 structures for later use
  1473. */
  1474. static void
  1475. mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
  1476. mega_product_info *product_info)
  1477. {
  1478. int i;
  1479. product_info->max_commands = inquiry->adapter_info.max_commands;
  1480. enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
  1481. product_info->nchannels = inquiry->adapter_info.nchannels;
  1482. for (i = 0; i < 4; i++) {
  1483. product_info->fw_version[i] =
  1484. inquiry->adapter_info.fw_version[i];
  1485. product_info->bios_version[i] =
  1486. inquiry->adapter_info.bios_version[i];
  1487. }
  1488. enquiry3->cache_flush_interval =
  1489. inquiry->adapter_info.cache_flush_interval;
  1490. product_info->dram_size = inquiry->adapter_info.dram_size;
  1491. enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
  1492. for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
  1493. enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
  1494. enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
  1495. enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
  1496. }
  1497. for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
  1498. enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
  1499. }
  1500. static inline void
  1501. mega_free_sgl(adapter_t *adapter)
  1502. {
  1503. scb_t *scb;
  1504. int i;
  1505. for(i = 0; i < adapter->max_cmds; i++) {
  1506. scb = &adapter->scb_list[i];
  1507. if( scb->sgl64 ) {
  1508. pci_free_consistent(adapter->dev,
  1509. sizeof(mega_sgl64) * adapter->sglen,
  1510. scb->sgl64,
  1511. scb->sgl_dma_addr);
  1512. scb->sgl64 = NULL;
  1513. }
  1514. if( scb->pthru ) {
  1515. pci_free_consistent(adapter->dev, sizeof(mega_passthru),
  1516. scb->pthru, scb->pthru_dma_addr);
  1517. scb->pthru = NULL;
  1518. }
  1519. if( scb->epthru ) {
  1520. pci_free_consistent(adapter->dev,
  1521. sizeof(mega_ext_passthru),
  1522. scb->epthru, scb->epthru_dma_addr);
  1523. scb->epthru = NULL;
  1524. }
  1525. }
  1526. }
  1527. /*
  1528. * Get information about the card/driver
  1529. */
  1530. const char *
  1531. megaraid_info(struct Scsi_Host *host)
  1532. {
  1533. static char buffer[512];
  1534. adapter_t *adapter;
  1535. adapter = (adapter_t *)host->hostdata;
  1536. sprintf (buffer,
  1537. "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
  1538. adapter->fw_version, adapter->product_info.max_commands,
  1539. adapter->host->max_id, adapter->host->max_channel,
  1540. adapter->host->max_lun);
  1541. return buffer;
  1542. }
  1543. /*
  1544. * Abort a previous SCSI request. Only commands on the pending list can be
  1545. * aborted. All the commands issued to the F/W must complete.
  1546. */
  1547. static int
  1548. megaraid_abort(Scsi_Cmnd *cmd)
  1549. {
  1550. adapter_t *adapter;
  1551. int rval;
  1552. adapter = (adapter_t *)cmd->device->host->hostdata;
  1553. rval = megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
  1554. /*
  1555. * This is required here to complete any completed requests
  1556. * to be communicated over to the mid layer.
  1557. */
  1558. mega_rundoneq(adapter);
  1559. return rval;
  1560. }
  1561. static int
  1562. megaraid_reset(struct scsi_cmnd *cmd)
  1563. {
  1564. adapter_t *adapter;
  1565. megacmd_t mc;
  1566. int rval;
  1567. adapter = (adapter_t *)cmd->device->host->hostdata;
  1568. #if MEGA_HAVE_CLUSTERING
  1569. mc.cmd = MEGA_CLUSTER_CMD;
  1570. mc.opcode = MEGA_RESET_RESERVATIONS;
  1571. if( mega_internal_command(adapter, LOCK_INT, &mc, NULL) != 0 ) {
  1572. printk(KERN_WARNING
  1573. "megaraid: reservation reset failed.\n");
  1574. }
  1575. else {
  1576. printk(KERN_INFO "megaraid: reservation reset.\n");
  1577. }
  1578. #endif
  1579. spin_lock_irq(&adapter->lock);
  1580. rval = megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
  1581. /*
  1582. * This is required here to complete any completed requests
  1583. * to be communicated over to the mid layer.
  1584. */
  1585. mega_rundoneq(adapter);
  1586. spin_unlock_irq(&adapter->lock);
  1587. return rval;
  1588. }
  1589. /**
  1590. * megaraid_abort_and_reset()
  1591. * @adapter - megaraid soft state
  1592. * @cmd - scsi command to be aborted or reset
  1593. * @aor - abort or reset flag
  1594. *
  1595. * Try to locate the scsi command in the pending queue. If found and is not
  1596. * issued to the controller, abort/reset it. Otherwise return failure
  1597. */
  1598. static int
  1599. megaraid_abort_and_reset(adapter_t *adapter, Scsi_Cmnd *cmd, int aor)
  1600. {
  1601. struct list_head *pos, *next;
  1602. scb_t *scb;
  1603. printk(KERN_WARNING "megaraid: %s-%lx cmd=%x <c=%d t=%d l=%d>\n",
  1604. (aor == SCB_ABORT)? "ABORTING":"RESET", cmd->serial_number,
  1605. cmd->cmnd[0], cmd->device->channel,
  1606. cmd->device->id, cmd->device->lun);
  1607. if(list_empty(&adapter->pending_list))
  1608. return FALSE;
  1609. list_for_each_safe(pos, next, &adapter->pending_list) {
  1610. scb = list_entry(pos, scb_t, list);
  1611. if (scb->cmd == cmd) { /* Found command */
  1612. scb->state |= aor;
  1613. /*
  1614. * Check if this command has firmare owenership. If
  1615. * yes, we cannot reset this command. Whenever, f/w
  1616. * completes this command, we will return appropriate
  1617. * status from ISR.
  1618. */
  1619. if( scb->state & SCB_ISSUED ) {
  1620. printk(KERN_WARNING
  1621. "megaraid: %s-%lx[%x], fw owner.\n",
  1622. (aor==SCB_ABORT) ? "ABORTING":"RESET",
  1623. cmd->serial_number, scb->idx);
  1624. return FALSE;
  1625. }
  1626. else {
  1627. /*
  1628. * Not yet issued! Remove from the pending
  1629. * list
  1630. */
  1631. printk(KERN_WARNING
  1632. "megaraid: %s-%lx[%x], driver owner.\n",
  1633. (aor==SCB_ABORT) ? "ABORTING":"RESET",
  1634. cmd->serial_number, scb->idx);
  1635. mega_free_scb(adapter, scb);
  1636. if( aor == SCB_ABORT ) {
  1637. cmd->result = (DID_ABORT << 16);
  1638. }
  1639. else {
  1640. cmd->result = (DID_RESET << 16);
  1641. }
  1642. list_add_tail(SCSI_LIST(cmd),
  1643. &adapter->completed_list);
  1644. return TRUE;
  1645. }
  1646. }
  1647. }
  1648. return FALSE;
  1649. }
  1650. static inline int
  1651. make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
  1652. {
  1653. *pdev = kmalloc(sizeof(struct pci_dev), GFP_KERNEL);
  1654. if( *pdev == NULL ) return -1;
  1655. memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
  1656. if( pci_set_dma_mask(*pdev, 0xffffffff) != 0 ) {
  1657. kfree(*pdev);
  1658. return -1;
  1659. }
  1660. return 0;
  1661. }
  1662. static inline void
  1663. free_local_pdev(struct pci_dev *pdev)
  1664. {
  1665. kfree(pdev);
  1666. }
  1667. /**
  1668. * mega_allocate_inquiry()
  1669. * @dma_handle - handle returned for dma address
  1670. * @pdev - handle to pci device
  1671. *
  1672. * allocates memory for inquiry structure
  1673. */
  1674. static inline void *
  1675. mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
  1676. {
  1677. return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
  1678. }
  1679. static inline void
  1680. mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
  1681. {
  1682. pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
  1683. }
  1684. #ifdef CONFIG_PROC_FS
  1685. /* Following code handles /proc fs */
  1686. #define CREATE_READ_PROC(string, func) create_proc_read_entry(string, \
  1687. S_IRUSR | S_IFREG, \
  1688. controller_proc_dir_entry, \
  1689. func, adapter)
  1690. /**
  1691. * mega_create_proc_entry()
  1692. * @index - index in soft state array
  1693. * @parent - parent node for this /proc entry
  1694. *
  1695. * Creates /proc entries for our controllers.
  1696. */
  1697. static void
  1698. mega_create_proc_entry(int index, struct proc_dir_entry *parent)
  1699. {
  1700. struct proc_dir_entry *controller_proc_dir_entry = NULL;
  1701. u8 string[64] = { 0 };
  1702. adapter_t *adapter = hba_soft_state[index];
  1703. sprintf(string, "hba%d", adapter->host->host_no);
  1704. controller_proc_dir_entry =
  1705. adapter->controller_proc_dir_entry = proc_mkdir(string, parent);
  1706. if(!controller_proc_dir_entry) {
  1707. printk(KERN_WARNING "\nmegaraid: proc_mkdir failed\n");
  1708. return;
  1709. }
  1710. adapter->proc_read = CREATE_READ_PROC("config", proc_read_config);
  1711. adapter->proc_stat = CREATE_READ_PROC("stat", proc_read_stat);
  1712. adapter->proc_mbox = CREATE_READ_PROC("mailbox", proc_read_mbox);
  1713. #if MEGA_HAVE_ENH_PROC
  1714. adapter->proc_rr = CREATE_READ_PROC("rebuild-rate", proc_rebuild_rate);
  1715. adapter->proc_battery = CREATE_READ_PROC("battery-status",
  1716. proc_battery);
  1717. /*
  1718. * Display each physical drive on its channel
  1719. */
  1720. adapter->proc_pdrvstat[0] = CREATE_READ_PROC("diskdrives-ch0",
  1721. proc_pdrv_ch0);
  1722. adapter->proc_pdrvstat[1] = CREATE_READ_PROC("diskdrives-ch1",
  1723. proc_pdrv_ch1);
  1724. adapter->proc_pdrvstat[2] = CREATE_READ_PROC("diskdrives-ch2",
  1725. proc_pdrv_ch2);
  1726. adapter->proc_pdrvstat[3] = CREATE_READ_PROC("diskdrives-ch3",
  1727. proc_pdrv_ch3);
  1728. /*
  1729. * Display a set of up to 10 logical drive through each of following
  1730. * /proc entries
  1731. */
  1732. adapter->proc_rdrvstat[0] = CREATE_READ_PROC("raiddrives-0-9",
  1733. proc_rdrv_10);
  1734. adapter->proc_rdrvstat[1] = CREATE_READ_PROC("raiddrives-10-19",
  1735. proc_rdrv_20);
  1736. adapter->proc_rdrvstat[2] = CREATE_READ_PROC("raiddrives-20-29",
  1737. proc_rdrv_30);
  1738. adapter->proc_rdrvstat[3] = CREATE_READ_PROC("raiddrives-30-39",
  1739. proc_rdrv_40);
  1740. #endif
  1741. }
  1742. /**
  1743. * proc_read_config()
  1744. * @page - buffer to write the data in
  1745. * @start - where the actual data has been written in page
  1746. * @offset - same meaning as the read system call
  1747. * @count - same meaning as the read system call
  1748. * @eof - set if no more data needs to be returned
  1749. * @data - pointer to our soft state
  1750. *
  1751. * Display configuration information about the controller.
  1752. */
  1753. static int
  1754. proc_read_config(char *page, char **start, off_t offset, int count, int *eof,
  1755. void *data)
  1756. {
  1757. adapter_t *adapter = (adapter_t *)data;
  1758. int len = 0;
  1759. len += sprintf(page+len, "%s", MEGARAID_VERSION);
  1760. if(adapter->product_info.product_name[0])
  1761. len += sprintf(page+len, "%s\n",
  1762. adapter->product_info.product_name);
  1763. len += sprintf(page+len, "Controller Type: ");
  1764. if( adapter->flag & BOARD_MEMMAP ) {
  1765. len += sprintf(page+len,
  1766. "438/466/467/471/493/518/520/531/532\n");
  1767. }
  1768. else {
  1769. len += sprintf(page+len,
  1770. "418/428/434\n");
  1771. }
  1772. if(adapter->flag & BOARD_40LD) {
  1773. len += sprintf(page+len,
  1774. "Controller Supports 40 Logical Drives\n");
  1775. }
  1776. if(adapter->flag & BOARD_64BIT) {
  1777. len += sprintf(page+len,
  1778. "Controller capable of 64-bit memory addressing\n");
  1779. }
  1780. if( adapter->has_64bit_addr ) {
  1781. len += sprintf(page+len,
  1782. "Controller using 64-bit memory addressing\n");
  1783. }
  1784. else {
  1785. len += sprintf(page+len,
  1786. "Controller is not using 64-bit memory addressing\n");
  1787. }
  1788. len += sprintf(page+len, "Base = %08lx, Irq = %d, ", adapter->base,
  1789. adapter->host->irq);
  1790. len += sprintf(page+len, "Logical Drives = %d, Channels = %d\n",
  1791. adapter->numldrv, adapter->product_info.nchannels);
  1792. len += sprintf(page+len, "Version =%s:%s, DRAM = %dMb\n",
  1793. adapter->fw_version, adapter->bios_version,
  1794. adapter->product_info.dram_size);
  1795. len += sprintf(page+len,
  1796. "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
  1797. adapter->product_info.max_commands, adapter->max_cmds);
  1798. len += sprintf(page+len, "support_ext_cdb = %d\n",
  1799. adapter->support_ext_cdb);
  1800. len += sprintf(page+len, "support_random_del = %d\n",
  1801. adapter->support_random_del);
  1802. len += sprintf(page+len, "boot_ldrv_enabled = %d\n",
  1803. adapter->boot_ldrv_enabled);
  1804. len += sprintf(page+len, "boot_ldrv = %d\n",
  1805. adapter->boot_ldrv);
  1806. len += sprintf(page+len, "boot_pdrv_enabled = %d\n",
  1807. adapter->boot_pdrv_enabled);
  1808. len += sprintf(page+len, "boot_pdrv_ch = %d\n",
  1809. adapter->boot_pdrv_ch);
  1810. len += sprintf(page+len, "boot_pdrv_tgt = %d\n",
  1811. adapter->boot_pdrv_tgt);
  1812. len += sprintf(page+len, "quiescent = %d\n",
  1813. atomic_read(&adapter->quiescent));
  1814. len += sprintf(page+len, "has_cluster = %d\n",
  1815. adapter->has_cluster);
  1816. len += sprintf(page+len, "\nModule Parameters:\n");
  1817. len += sprintf(page+len, "max_cmd_per_lun = %d\n",
  1818. max_cmd_per_lun);
  1819. len += sprintf(page+len, "max_sectors_per_io = %d\n",
  1820. max_sectors_per_io);
  1821. *eof = 1;
  1822. return len;
  1823. }
  1824. /**
  1825. * proc_read_stat()
  1826. * @page - buffer to write the data in
  1827. * @start - where the actual data has been written in page
  1828. * @offset - same meaning as the read system call
  1829. * @count - same meaning as the read system call
  1830. * @eof - set if no more data needs to be returned
  1831. * @data - pointer to our soft state
  1832. *
  1833. * Diaplay statistical information about the I/O activity.
  1834. */
  1835. static int
  1836. proc_read_stat(char *page, char **start, off_t offset, int count, int *eof,
  1837. void *data)
  1838. {
  1839. adapter_t *adapter;
  1840. int len;
  1841. int i;
  1842. i = 0; /* avoid compilation warnings */
  1843. len = 0;
  1844. adapter = (adapter_t *)data;
  1845. len = sprintf(page, "Statistical Information for this controller\n");
  1846. len += sprintf(page+len, "pend_cmds = %d\n",
  1847. atomic_read(&adapter->pend_cmds));
  1848. #if MEGA_HAVE_STATS
  1849. for(i = 0; i < adapter->numldrv; i++) {
  1850. len += sprintf(page+len, "Logical Drive %d:\n", i);
  1851. len += sprintf(page+len,
  1852. "\tReads Issued = %lu, Writes Issued = %lu\n",
  1853. adapter->nreads[i], adapter->nwrites[i]);
  1854. len += sprintf(page+len,
  1855. "\tSectors Read = %lu, Sectors Written = %lu\n",
  1856. adapter->nreadblocks[i], adapter->nwriteblocks[i]);
  1857. len += sprintf(page+len,
  1858. "\tRead errors = %lu, Write errors = %lu\n\n",
  1859. adapter->rd_errors[i], adapter->wr_errors[i]);
  1860. }
  1861. #else
  1862. len += sprintf(page+len,
  1863. "IO and error counters not compiled in driver.\n");
  1864. #endif
  1865. *eof = 1;
  1866. return len;
  1867. }
  1868. /**
  1869. * proc_read_mbox()
  1870. * @page - buffer to write the data in
  1871. * @start - where the actual data has been written in page
  1872. * @offset - same meaning as the read system call
  1873. * @count - same meaning as the read system call
  1874. * @eof - set if no more data needs to be returned
  1875. * @data - pointer to our soft state
  1876. *
  1877. * Display mailbox information for the last command issued. This information
  1878. * is good for debugging.
  1879. */
  1880. static int
  1881. proc_read_mbox(char *page, char **start, off_t offset, int count, int *eof,
  1882. void *data)
  1883. {
  1884. adapter_t *adapter = (adapter_t *)data;
  1885. volatile mbox_t *mbox = adapter->mbox;
  1886. int len = 0;
  1887. len = sprintf(page, "Contents of Mail Box Structure\n");
  1888. len += sprintf(page+len, " Fw Command = 0x%02x\n",
  1889. mbox->m_out.cmd);
  1890. len += sprintf(page+len, " Cmd Sequence = 0x%02x\n",
  1891. mbox->m_out.cmdid);
  1892. len += sprintf(page+len, " No of Sectors= %04d\n",
  1893. mbox->m_out.numsectors);
  1894. len += sprintf(page+len, " LBA = 0x%02x\n",
  1895. mbox->m_out.lba);
  1896. len += sprintf(page+len, " DTA = 0x%08x\n",
  1897. mbox->m_out.xferaddr);
  1898. len += sprintf(page+len, " Logical Drive= 0x%02x\n",
  1899. mbox->m_out.logdrv);
  1900. len += sprintf(page+len, " No of SG Elmt= 0x%02x\n",
  1901. mbox->m_out.numsgelements);
  1902. len += sprintf(page+len, " Busy = %01x\n",
  1903. mbox->m_in.busy);
  1904. len += sprintf(page+len, " Status = 0x%02x\n",
  1905. mbox->m_in.status);
  1906. *eof = 1;
  1907. return len;
  1908. }
  1909. /**
  1910. * proc_rebuild_rate()
  1911. * @page - buffer to write the data in
  1912. * @start - where the actual data has been written in page
  1913. * @offset - same meaning as the read system call
  1914. * @count - same meaning as the read system call
  1915. * @eof - set if no more data needs to be returned
  1916. * @data - pointer to our soft state
  1917. *
  1918. * Display current rebuild rate
  1919. */
  1920. static int
  1921. proc_rebuild_rate(char *page, char **start, off_t offset, int count, int *eof,
  1922. void *data)
  1923. {
  1924. adapter_t *adapter = (adapter_t *)data;
  1925. dma_addr_t dma_handle;
  1926. caddr_t inquiry;
  1927. struct pci_dev *pdev;
  1928. int len = 0;
  1929. if( make_local_pdev(adapter, &pdev) != 0 ) {
  1930. *eof = 1;
  1931. return len;
  1932. }
  1933. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  1934. free_local_pdev(pdev);
  1935. *eof = 1;
  1936. return len;
  1937. }
  1938. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  1939. len = sprintf(page, "Adapter inquiry failed.\n");
  1940. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  1941. mega_free_inquiry(inquiry, dma_handle, pdev);
  1942. free_local_pdev(pdev);
  1943. *eof = 1;
  1944. return len;
  1945. }
  1946. if( adapter->flag & BOARD_40LD ) {
  1947. len = sprintf(page, "Rebuild Rate: [%d%%]\n",
  1948. ((mega_inquiry3 *)inquiry)->rebuild_rate);
  1949. }
  1950. else {
  1951. len = sprintf(page, "Rebuild Rate: [%d%%]\n",
  1952. ((mraid_ext_inquiry *)
  1953. inquiry)->raid_inq.adapter_info.rebuild_rate);
  1954. }
  1955. mega_free_inquiry(inquiry, dma_handle, pdev);
  1956. free_local_pdev(pdev);
  1957. *eof = 1;
  1958. return len;
  1959. }
  1960. /**
  1961. * proc_battery()
  1962. * @page - buffer to write the data in
  1963. * @start - where the actual data has been written in page
  1964. * @offset - same meaning as the read system call
  1965. * @count - same meaning as the read system call
  1966. * @eof - set if no more data needs to be returned
  1967. * @data - pointer to our soft state
  1968. *
  1969. * Display information about the battery module on the controller.
  1970. */
  1971. static int
  1972. proc_battery(char *page, char **start, off_t offset, int count, int *eof,
  1973. void *data)
  1974. {
  1975. adapter_t *adapter = (adapter_t *)data;
  1976. dma_addr_t dma_handle;
  1977. caddr_t inquiry;
  1978. struct pci_dev *pdev;
  1979. u8 battery_status = 0;
  1980. char str[256];
  1981. int len = 0;
  1982. if( make_local_pdev(adapter, &pdev) != 0 ) {
  1983. *eof = 1;
  1984. return len;
  1985. }
  1986. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  1987. free_local_pdev(pdev);
  1988. *eof = 1;
  1989. return len;
  1990. }
  1991. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  1992. len = sprintf(page, "Adapter inquiry failed.\n");
  1993. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  1994. mega_free_inquiry(inquiry, dma_handle, pdev);
  1995. free_local_pdev(pdev);
  1996. *eof = 1;
  1997. return len;
  1998. }
  1999. if( adapter->flag & BOARD_40LD ) {
  2000. battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
  2001. }
  2002. else {
  2003. battery_status = ((mraid_ext_inquiry *)inquiry)->
  2004. raid_inq.adapter_info.battery_status;
  2005. }
  2006. /*
  2007. * Decode the battery status
  2008. */
  2009. sprintf(str, "Battery Status:[%d]", battery_status);
  2010. if(battery_status == MEGA_BATT_CHARGE_DONE)
  2011. strcat(str, " Charge Done");
  2012. if(battery_status & MEGA_BATT_MODULE_MISSING)
  2013. strcat(str, " Module Missing");
  2014. if(battery_status & MEGA_BATT_LOW_VOLTAGE)
  2015. strcat(str, " Low Voltage");
  2016. if(battery_status & MEGA_BATT_TEMP_HIGH)
  2017. strcat(str, " Temperature High");
  2018. if(battery_status & MEGA_BATT_PACK_MISSING)
  2019. strcat(str, " Pack Missing");
  2020. if(battery_status & MEGA_BATT_CHARGE_INPROG)
  2021. strcat(str, " Charge In-progress");
  2022. if(battery_status & MEGA_BATT_CHARGE_FAIL)
  2023. strcat(str, " Charge Fail");
  2024. if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
  2025. strcat(str, " Cycles Exceeded");
  2026. len = sprintf(page, "%s\n", str);
  2027. mega_free_inquiry(inquiry, dma_handle, pdev);
  2028. free_local_pdev(pdev);
  2029. *eof = 1;
  2030. return len;
  2031. }
  2032. /**
  2033. * proc_pdrv_ch0()
  2034. * @page - buffer to write the data in
  2035. * @start - where the actual data has been written in page
  2036. * @offset - same meaning as the read system call
  2037. * @count - same meaning as the read system call
  2038. * @eof - set if no more data needs to be returned
  2039. * @data - pointer to our soft state
  2040. *
  2041. * Display information about the physical drives on physical channel 0.
  2042. */
  2043. static int
  2044. proc_pdrv_ch0(char *page, char **start, off_t offset, int count, int *eof,
  2045. void *data)
  2046. {
  2047. adapter_t *adapter = (adapter_t *)data;
  2048. *eof = 1;
  2049. return (proc_pdrv(adapter, page, 0));
  2050. }
  2051. /**
  2052. * proc_pdrv_ch1()
  2053. * @page - buffer to write the data in
  2054. * @start - where the actual data has been written in page
  2055. * @offset - same meaning as the read system call
  2056. * @count - same meaning as the read system call
  2057. * @eof - set if no more data needs to be returned
  2058. * @data - pointer to our soft state
  2059. *
  2060. * Display information about the physical drives on physical channel 1.
  2061. */
  2062. static int
  2063. proc_pdrv_ch1(char *page, char **start, off_t offset, int count, int *eof,
  2064. void *data)
  2065. {
  2066. adapter_t *adapter = (adapter_t *)data;
  2067. *eof = 1;
  2068. return (proc_pdrv(adapter, page, 1));
  2069. }
  2070. /**
  2071. * proc_pdrv_ch2()
  2072. * @page - buffer to write the data in
  2073. * @start - where the actual data has been written in page
  2074. * @offset - same meaning as the read system call
  2075. * @count - same meaning as the read system call
  2076. * @eof - set if no more data needs to be returned
  2077. * @data - pointer to our soft state
  2078. *
  2079. * Display information about the physical drives on physical channel 2.
  2080. */
  2081. static int
  2082. proc_pdrv_ch2(char *page, char **start, off_t offset, int count, int *eof,
  2083. void *data)
  2084. {
  2085. adapter_t *adapter = (adapter_t *)data;
  2086. *eof = 1;
  2087. return (proc_pdrv(adapter, page, 2));
  2088. }
  2089. /**
  2090. * proc_pdrv_ch3()
  2091. * @page - buffer to write the data in
  2092. * @start - where the actual data has been written in page
  2093. * @offset - same meaning as the read system call
  2094. * @count - same meaning as the read system call
  2095. * @eof - set if no more data needs to be returned
  2096. * @data - pointer to our soft state
  2097. *
  2098. * Display information about the physical drives on physical channel 3.
  2099. */
  2100. static int
  2101. proc_pdrv_ch3(char *page, char **start, off_t offset, int count, int *eof,
  2102. void *data)
  2103. {
  2104. adapter_t *adapter = (adapter_t *)data;
  2105. *eof = 1;
  2106. return (proc_pdrv(adapter, page, 3));
  2107. }
  2108. /**
  2109. * proc_pdrv()
  2110. * @page - buffer to write the data in
  2111. * @adapter - pointer to our soft state
  2112. *
  2113. * Display information about the physical drives.
  2114. */
  2115. static int
  2116. proc_pdrv(adapter_t *adapter, char *page, int channel)
  2117. {
  2118. dma_addr_t dma_handle;
  2119. char *scsi_inq;
  2120. dma_addr_t scsi_inq_dma_handle;
  2121. caddr_t inquiry;
  2122. struct pci_dev *pdev;
  2123. u8 *pdrv_state;
  2124. u8 state;
  2125. int tgt;
  2126. int max_channels;
  2127. int len = 0;
  2128. char str[80];
  2129. int i;
  2130. if( make_local_pdev(adapter, &pdev) != 0 ) {
  2131. return len;
  2132. }
  2133. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  2134. goto free_pdev;
  2135. }
  2136. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  2137. len = sprintf(page, "Adapter inquiry failed.\n");
  2138. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  2139. goto free_inquiry;
  2140. }
  2141. scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
  2142. if( scsi_inq == NULL ) {
  2143. len = sprintf(page, "memory not available for scsi inq.\n");
  2144. goto free_inquiry;
  2145. }
  2146. if( adapter->flag & BOARD_40LD ) {
  2147. pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
  2148. }
  2149. else {
  2150. pdrv_state = ((mraid_ext_inquiry *)inquiry)->
  2151. raid_inq.pdrv_info.pdrv_state;
  2152. }
  2153. max_channels = adapter->product_info.nchannels;
  2154. if( channel >= max_channels ) {
  2155. goto free_pci;
  2156. }
  2157. for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
  2158. i = channel*16 + tgt;
  2159. state = *(pdrv_state + i);
  2160. switch( state & 0x0F ) {
  2161. case PDRV_ONLINE:
  2162. sprintf(str,
  2163. "Channel:%2d Id:%2d State: Online",
  2164. channel, tgt);
  2165. break;
  2166. case PDRV_FAILED:
  2167. sprintf(str,
  2168. "Channel:%2d Id:%2d State: Failed",
  2169. channel, tgt);
  2170. break;
  2171. case PDRV_RBLD:
  2172. sprintf(str,
  2173. "Channel:%2d Id:%2d State: Rebuild",
  2174. channel, tgt);
  2175. break;
  2176. case PDRV_HOTSPARE:
  2177. sprintf(str,
  2178. "Channel:%2d Id:%2d State: Hot spare",
  2179. channel, tgt);
  2180. break;
  2181. default:
  2182. sprintf(str,
  2183. "Channel:%2d Id:%2d State: Un-configured",
  2184. channel, tgt);
  2185. break;
  2186. }
  2187. /*
  2188. * This interface displays inquiries for disk drives
  2189. * only. Inquries for logical drives and non-disk
  2190. * devices are available through /proc/scsi/scsi
  2191. */
  2192. memset(scsi_inq, 0, 256);
  2193. if( mega_internal_dev_inquiry(adapter, channel, tgt,
  2194. scsi_inq_dma_handle) ||
  2195. (scsi_inq[0] & 0x1F) != TYPE_DISK ) {
  2196. continue;
  2197. }
  2198. /*
  2199. * Check for overflow. We print less than 240
  2200. * characters for inquiry
  2201. */
  2202. if( (len + 240) >= PAGE_SIZE ) break;
  2203. len += sprintf(page+len, "%s.\n", str);
  2204. len += mega_print_inquiry(page+len, scsi_inq);
  2205. }
  2206. free_pci:
  2207. pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
  2208. free_inquiry:
  2209. mega_free_inquiry(inquiry, dma_handle, pdev);
  2210. free_pdev:
  2211. free_local_pdev(pdev);
  2212. return len;
  2213. }
  2214. /*
  2215. * Display scsi inquiry
  2216. */
  2217. static int
  2218. mega_print_inquiry(char *page, char *scsi_inq)
  2219. {
  2220. int len = 0;
  2221. int i;
  2222. len = sprintf(page, " Vendor: ");
  2223. for( i = 8; i < 16; i++ ) {
  2224. len += sprintf(page+len, "%c", scsi_inq[i]);
  2225. }
  2226. len += sprintf(page+len, " Model: ");
  2227. for( i = 16; i < 32; i++ ) {
  2228. len += sprintf(page+len, "%c", scsi_inq[i]);
  2229. }
  2230. len += sprintf(page+len, " Rev: ");
  2231. for( i = 32; i < 36; i++ ) {
  2232. len += sprintf(page+len, "%c", scsi_inq[i]);
  2233. }
  2234. len += sprintf(page+len, "\n");
  2235. i = scsi_inq[0] & 0x1f;
  2236. len += sprintf(page+len, " Type: %s ",
  2237. i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] :
  2238. "Unknown ");
  2239. len += sprintf(page+len,
  2240. " ANSI SCSI revision: %02x", scsi_inq[2] & 0x07);
  2241. if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
  2242. len += sprintf(page+len, " CCS\n");
  2243. else
  2244. len += sprintf(page+len, "\n");
  2245. return len;
  2246. }
  2247. /**
  2248. * proc_rdrv_10()
  2249. * @page - buffer to write the data in
  2250. * @start - where the actual data has been written in page
  2251. * @offset - same meaning as the read system call
  2252. * @count - same meaning as the read system call
  2253. * @eof - set if no more data needs to be returned
  2254. * @data - pointer to our soft state
  2255. *
  2256. * Display real time information about the logical drives 0 through 9.
  2257. */
  2258. static int
  2259. proc_rdrv_10(char *page, char **start, off_t offset, int count, int *eof,
  2260. void *data)
  2261. {
  2262. adapter_t *adapter = (adapter_t *)data;
  2263. *eof = 1;
  2264. return (proc_rdrv(adapter, page, 0, 9));
  2265. }
  2266. /**
  2267. * proc_rdrv_20()
  2268. * @page - buffer to write the data in
  2269. * @start - where the actual data has been written in page
  2270. * @offset - same meaning as the read system call
  2271. * @count - same meaning as the read system call
  2272. * @eof - set if no more data needs to be returned
  2273. * @data - pointer to our soft state
  2274. *
  2275. * Display real time information about the logical drives 0 through 9.
  2276. */
  2277. static int
  2278. proc_rdrv_20(char *page, char **start, off_t offset, int count, int *eof,
  2279. void *data)
  2280. {
  2281. adapter_t *adapter = (adapter_t *)data;
  2282. *eof = 1;
  2283. return (proc_rdrv(adapter, page, 10, 19));
  2284. }
  2285. /**
  2286. * proc_rdrv_30()
  2287. * @page - buffer to write the data in
  2288. * @start - where the actual data has been written in page
  2289. * @offset - same meaning as the read system call
  2290. * @count - same meaning as the read system call
  2291. * @eof - set if no more data needs to be returned
  2292. * @data - pointer to our soft state
  2293. *
  2294. * Display real time information about the logical drives 0 through 9.
  2295. */
  2296. static int
  2297. proc_rdrv_30(char *page, char **start, off_t offset, int count, int *eof,
  2298. void *data)
  2299. {
  2300. adapter_t *adapter = (adapter_t *)data;
  2301. *eof = 1;
  2302. return (proc_rdrv(adapter, page, 20, 29));
  2303. }
  2304. /**
  2305. * proc_rdrv_40()
  2306. * @page - buffer to write the data in
  2307. * @start - where the actual data has been written in page
  2308. * @offset - same meaning as the read system call
  2309. * @count - same meaning as the read system call
  2310. * @eof - set if no more data needs to be returned
  2311. * @data - pointer to our soft state
  2312. *
  2313. * Display real time information about the logical drives 0 through 9.
  2314. */
  2315. static int
  2316. proc_rdrv_40(char *page, char **start, off_t offset, int count, int *eof,
  2317. void *data)
  2318. {
  2319. adapter_t *adapter = (adapter_t *)data;
  2320. *eof = 1;
  2321. return (proc_rdrv(adapter, page, 30, 39));
  2322. }
  2323. /**
  2324. * proc_rdrv()
  2325. * @page - buffer to write the data in
  2326. * @adapter - pointer to our soft state
  2327. * @start - starting logical drive to display
  2328. * @end - ending logical drive to display
  2329. *
  2330. * We do not print the inquiry information since its already available through
  2331. * /proc/scsi/scsi interface
  2332. */
  2333. static int
  2334. proc_rdrv(adapter_t *adapter, char *page, int start, int end )
  2335. {
  2336. dma_addr_t dma_handle;
  2337. logdrv_param *lparam;
  2338. megacmd_t mc;
  2339. char *disk_array;
  2340. dma_addr_t disk_array_dma_handle;
  2341. caddr_t inquiry;
  2342. struct pci_dev *pdev;
  2343. u8 *rdrv_state;
  2344. int num_ldrv;
  2345. u32 array_sz;
  2346. int len = 0;
  2347. int i;
  2348. if( make_local_pdev(adapter, &pdev) != 0 ) {
  2349. return len;
  2350. }
  2351. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  2352. free_local_pdev(pdev);
  2353. return len;
  2354. }
  2355. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  2356. len = sprintf(page, "Adapter inquiry failed.\n");
  2357. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  2358. mega_free_inquiry(inquiry, dma_handle, pdev);
  2359. free_local_pdev(pdev);
  2360. return len;
  2361. }
  2362. memset(&mc, 0, sizeof(megacmd_t));
  2363. if( adapter->flag & BOARD_40LD ) {
  2364. array_sz = sizeof(disk_array_40ld);
  2365. rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
  2366. num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
  2367. }
  2368. else {
  2369. array_sz = sizeof(disk_array_8ld);
  2370. rdrv_state = ((mraid_ext_inquiry *)inquiry)->
  2371. raid_inq.logdrv_info.ldrv_state;
  2372. num_ldrv = ((mraid_ext_inquiry *)inquiry)->
  2373. raid_inq.logdrv_info.num_ldrv;
  2374. }
  2375. disk_array = pci_alloc_consistent(pdev, array_sz,
  2376. &disk_array_dma_handle);
  2377. if( disk_array == NULL ) {
  2378. len = sprintf(page, "memory not available.\n");
  2379. mega_free_inquiry(inquiry, dma_handle, pdev);
  2380. free_local_pdev(pdev);
  2381. return len;
  2382. }
  2383. mc.xferaddr = (u32)disk_array_dma_handle;
  2384. if( adapter->flag & BOARD_40LD ) {
  2385. mc.cmd = FC_NEW_CONFIG;
  2386. mc.opcode = OP_DCMD_READ_CONFIG;
  2387. if( mega_internal_command(adapter, LOCK_INT, &mc, NULL) ) {
  2388. len = sprintf(page, "40LD read config failed.\n");
  2389. mega_free_inquiry(inquiry, dma_handle, pdev);
  2390. pci_free_consistent(pdev, array_sz, disk_array,
  2391. disk_array_dma_handle);
  2392. free_local_pdev(pdev);
  2393. return len;
  2394. }
  2395. }
  2396. else {
  2397. mc.cmd = NEW_READ_CONFIG_8LD;
  2398. if( mega_internal_command(adapter, LOCK_INT, &mc, NULL) ) {
  2399. mc.cmd = READ_CONFIG_8LD;
  2400. if( mega_internal_command(adapter, LOCK_INT, &mc,
  2401. NULL) ){
  2402. len = sprintf(page,
  2403. "8LD read config failed.\n");
  2404. mega_free_inquiry(inquiry, dma_handle, pdev);
  2405. pci_free_consistent(pdev, array_sz,
  2406. disk_array,
  2407. disk_array_dma_handle);
  2408. free_local_pdev(pdev);
  2409. return len;
  2410. }
  2411. }
  2412. }
  2413. for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
  2414. if( adapter->flag & BOARD_40LD ) {
  2415. lparam =
  2416. &((disk_array_40ld *)disk_array)->ldrv[i].lparam;
  2417. }
  2418. else {
  2419. lparam =
  2420. &((disk_array_8ld *)disk_array)->ldrv[i].lparam;
  2421. }
  2422. /*
  2423. * Check for overflow. We print less than 240 characters for
  2424. * information about each logical drive.
  2425. */
  2426. if( (len + 240) >= PAGE_SIZE ) break;
  2427. len += sprintf(page+len, "Logical drive:%2d:, ", i);
  2428. switch( rdrv_state[i] & 0x0F ) {
  2429. case RDRV_OFFLINE:
  2430. len += sprintf(page+len, "state: offline");
  2431. break;
  2432. case RDRV_DEGRADED:
  2433. len += sprintf(page+len, "state: degraded");
  2434. break;
  2435. case RDRV_OPTIMAL:
  2436. len += sprintf(page+len, "state: optimal");
  2437. break;
  2438. case RDRV_DELETED:
  2439. len += sprintf(page+len, "state: deleted");
  2440. break;
  2441. default:
  2442. len += sprintf(page+len, "state: unknown");
  2443. break;
  2444. }
  2445. /*
  2446. * Check if check consistency or initialization is going on
  2447. * for this logical drive.
  2448. */
  2449. if( (rdrv_state[i] & 0xF0) == 0x20 ) {
  2450. len += sprintf(page+len,
  2451. ", check-consistency in progress");
  2452. }
  2453. else if( (rdrv_state[i] & 0xF0) == 0x10 ) {
  2454. len += sprintf(page+len,
  2455. ", initialization in progress");
  2456. }
  2457. len += sprintf(page+len, "\n");
  2458. len += sprintf(page+len, "Span depth:%3d, ",
  2459. lparam->span_depth);
  2460. len += sprintf(page+len, "RAID level:%3d, ",
  2461. lparam->level);
  2462. len += sprintf(page+len, "Stripe size:%3d, ",
  2463. lparam->stripe_sz ? lparam->stripe_sz/2: 128);
  2464. len += sprintf(page+len, "Row size:%3d\n",
  2465. lparam->row_size);
  2466. len += sprintf(page+len, "Read Policy: ");
  2467. switch(lparam->read_ahead) {
  2468. case NO_READ_AHEAD:
  2469. len += sprintf(page+len, "No read ahead, ");
  2470. break;
  2471. case READ_AHEAD:
  2472. len += sprintf(page+len, "Read ahead, ");
  2473. break;
  2474. case ADAP_READ_AHEAD:
  2475. len += sprintf(page+len, "Adaptive, ");
  2476. break;
  2477. }
  2478. len += sprintf(page+len, "Write Policy: ");
  2479. switch(lparam->write_mode) {
  2480. case WRMODE_WRITE_THRU:
  2481. len += sprintf(page+len, "Write thru, ");
  2482. break;
  2483. case WRMODE_WRITE_BACK:
  2484. len += sprintf(page+len, "Write back, ");
  2485. break;
  2486. }
  2487. len += sprintf(page+len, "Cache Policy: ");
  2488. switch(lparam->direct_io) {
  2489. case CACHED_IO:
  2490. len += sprintf(page+len, "Cached IO\n\n");
  2491. break;
  2492. case DIRECT_IO:
  2493. len += sprintf(page+len, "Direct IO\n\n");
  2494. break;
  2495. }
  2496. }
  2497. mega_free_inquiry(inquiry, dma_handle, pdev);
  2498. pci_free_consistent(pdev, array_sz, disk_array,
  2499. disk_array_dma_handle);
  2500. free_local_pdev(pdev);
  2501. return len;
  2502. }
  2503. #endif
  2504. /**
  2505. * megaraid_biosparam()
  2506. *
  2507. * Return the disk geometry for a particular disk
  2508. */
  2509. static int
  2510. megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
  2511. sector_t capacity, int geom[])
  2512. {
  2513. adapter_t *adapter;
  2514. unsigned char *bh;
  2515. int heads;
  2516. int sectors;
  2517. int cylinders;
  2518. int rval;
  2519. /* Get pointer to host config structure */
  2520. adapter = (adapter_t *)sdev->host->hostdata;
  2521. if (IS_RAID_CH(adapter, sdev->channel)) {
  2522. /* Default heads (64) & sectors (32) */
  2523. heads = 64;
  2524. sectors = 32;
  2525. cylinders = (ulong)capacity / (heads * sectors);
  2526. /*
  2527. * Handle extended translation size for logical drives
  2528. * > 1Gb
  2529. */
  2530. if ((ulong)capacity >= 0x200000) {
  2531. heads = 255;
  2532. sectors = 63;
  2533. cylinders = (ulong)capacity / (heads * sectors);
  2534. }
  2535. /* return result */
  2536. geom[0] = heads;
  2537. geom[1] = sectors;
  2538. geom[2] = cylinders;
  2539. }
  2540. else {
  2541. bh = scsi_bios_ptable(bdev);
  2542. if( bh ) {
  2543. rval = scsi_partsize(bh, capacity,
  2544. &geom[2], &geom[0], &geom[1]);
  2545. kfree(bh);
  2546. if( rval != -1 )
  2547. return rval;
  2548. }
  2549. printk(KERN_INFO
  2550. "megaraid: invalid partition on this disk on channel %d\n",
  2551. sdev->channel);
  2552. /* Default heads (64) & sectors (32) */
  2553. heads = 64;
  2554. sectors = 32;
  2555. cylinders = (ulong)capacity / (heads * sectors);
  2556. /* Handle extended translation size for logical drives > 1Gb */
  2557. if ((ulong)capacity >= 0x200000) {
  2558. heads = 255;
  2559. sectors = 63;
  2560. cylinders = (ulong)capacity / (heads * sectors);
  2561. }
  2562. /* return result */
  2563. geom[0] = heads;
  2564. geom[1] = sectors;
  2565. geom[2] = cylinders;
  2566. }
  2567. return 0;
  2568. }
  2569. /**
  2570. * mega_init_scb()
  2571. * @adapter - pointer to our soft state
  2572. *
  2573. * Allocate memory for the various pointers in the scb structures:
  2574. * scatter-gather list pointer, passthru and extended passthru structure
  2575. * pointers.
  2576. */
  2577. static int
  2578. mega_init_scb(adapter_t *adapter)
  2579. {
  2580. scb_t *scb;
  2581. int i;
  2582. for( i = 0; i < adapter->max_cmds; i++ ) {
  2583. scb = &adapter->scb_list[i];
  2584. scb->sgl64 = NULL;
  2585. scb->sgl = NULL;
  2586. scb->pthru = NULL;
  2587. scb->epthru = NULL;
  2588. }
  2589. for( i = 0; i < adapter->max_cmds; i++ ) {
  2590. scb = &adapter->scb_list[i];
  2591. scb->idx = i;
  2592. scb->sgl64 = pci_alloc_consistent(adapter->dev,
  2593. sizeof(mega_sgl64) * adapter->sglen,
  2594. &scb->sgl_dma_addr);
  2595. scb->sgl = (mega_sglist *)scb->sgl64;
  2596. if( !scb->sgl ) {
  2597. printk(KERN_WARNING "RAID: Can't allocate sglist.\n");
  2598. mega_free_sgl(adapter);
  2599. return -1;
  2600. }
  2601. scb->pthru = pci_alloc_consistent(adapter->dev,
  2602. sizeof(mega_passthru),
  2603. &scb->pthru_dma_addr);
  2604. if( !scb->pthru ) {
  2605. printk(KERN_WARNING "RAID: Can't allocate passthru.\n");
  2606. mega_free_sgl(adapter);
  2607. return -1;
  2608. }
  2609. scb->epthru = pci_alloc_consistent(adapter->dev,
  2610. sizeof(mega_ext_passthru),
  2611. &scb->epthru_dma_addr);
  2612. if( !scb->epthru ) {
  2613. printk(KERN_WARNING
  2614. "Can't allocate extended passthru.\n");
  2615. mega_free_sgl(adapter);
  2616. return -1;
  2617. }
  2618. scb->dma_type = MEGA_DMA_TYPE_NONE;
  2619. /*
  2620. * Link to free list
  2621. * lock not required since we are loading the driver, so no
  2622. * commands possible right now.
  2623. */
  2624. scb->state = SCB_FREE;
  2625. scb->cmd = NULL;
  2626. list_add(&scb->list, &adapter->free_list);
  2627. }
  2628. return 0;
  2629. }
  2630. /**
  2631. * megadev_open()
  2632. * @inode - unused
  2633. * @filep - unused
  2634. *
  2635. * Routines for the character/ioctl interface to the driver. Find out if this
  2636. * is a valid open. If yes, increment the module use count so that it cannot
  2637. * be unloaded.
  2638. */
  2639. static int
  2640. megadev_open (struct inode *inode, struct file *filep)
  2641. {
  2642. /*
  2643. * Only allow superuser to access private ioctl interface
  2644. */
  2645. if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
  2646. return 0;
  2647. }
  2648. /**
  2649. * megadev_ioctl()
  2650. * @inode - Our device inode
  2651. * @filep - unused
  2652. * @cmd - ioctl command
  2653. * @arg - user buffer
  2654. *
  2655. * ioctl entry point for our private ioctl interface. We move the data in from
  2656. * the user space, prepare the command (if necessary, convert the old MIMD
  2657. * ioctl to new ioctl command), and issue a synchronous command to the
  2658. * controller.
  2659. */
  2660. static int
  2661. megadev_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
  2662. unsigned long arg)
  2663. {
  2664. adapter_t *adapter;
  2665. nitioctl_t uioc;
  2666. int adapno;
  2667. int rval;
  2668. mega_passthru __user *upthru; /* user address for passthru */
  2669. mega_passthru *pthru; /* copy user passthru here */
  2670. dma_addr_t pthru_dma_hndl;
  2671. void *data = NULL; /* data to be transferred */
  2672. dma_addr_t data_dma_hndl; /* dma handle for data xfer area */
  2673. megacmd_t mc;
  2674. megastat_t __user *ustats;
  2675. int num_ldrv;
  2676. u32 uxferaddr = 0;
  2677. struct pci_dev *pdev;
  2678. ustats = NULL; /* avoid compilation warnings */
  2679. num_ldrv = 0;
  2680. /*
  2681. * Make sure only USCSICMD are issued through this interface.
  2682. * MIMD application would still fire different command.
  2683. */
  2684. if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
  2685. return -EINVAL;
  2686. }
  2687. /*
  2688. * Check and convert a possible MIMD command to NIT command.
  2689. * mega_m_to_n() copies the data from the user space, so we do not
  2690. * have to do it here.
  2691. * NOTE: We will need some user address to copyout the data, therefore
  2692. * the inteface layer will also provide us with the required user
  2693. * addresses.
  2694. */
  2695. memset(&uioc, 0, sizeof(nitioctl_t));
  2696. if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
  2697. return rval;
  2698. switch( uioc.opcode ) {
  2699. case GET_DRIVER_VER:
  2700. if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
  2701. return (-EFAULT);
  2702. break;
  2703. case GET_N_ADAP:
  2704. if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
  2705. return (-EFAULT);
  2706. /*
  2707. * Shucks. MIMD interface returns a positive value for number
  2708. * of adapters. TODO: Change it to return 0 when there is no
  2709. * applicatio using mimd interface.
  2710. */
  2711. return hba_count;
  2712. case GET_ADAP_INFO:
  2713. /*
  2714. * Which adapter
  2715. */
  2716. if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
  2717. return (-ENODEV);
  2718. if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
  2719. sizeof(struct mcontroller)) )
  2720. return (-EFAULT);
  2721. break;
  2722. #if MEGA_HAVE_STATS
  2723. case GET_STATS:
  2724. /*
  2725. * Which adapter
  2726. */
  2727. if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
  2728. return (-ENODEV);
  2729. adapter = hba_soft_state[adapno];
  2730. ustats = uioc.uioc_uaddr;
  2731. if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
  2732. return (-EFAULT);
  2733. /*
  2734. * Check for the validity of the logical drive number
  2735. */
  2736. if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
  2737. if( copy_to_user(ustats->nreads, adapter->nreads,
  2738. num_ldrv*sizeof(u32)) )
  2739. return -EFAULT;
  2740. if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
  2741. num_ldrv*sizeof(u32)) )
  2742. return -EFAULT;
  2743. if( copy_to_user(ustats->nwrites, adapter->nwrites,
  2744. num_ldrv*sizeof(u32)) )
  2745. return -EFAULT;
  2746. if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
  2747. num_ldrv*sizeof(u32)) )
  2748. return -EFAULT;
  2749. if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
  2750. num_ldrv*sizeof(u32)) )
  2751. return -EFAULT;
  2752. if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
  2753. num_ldrv*sizeof(u32)) )
  2754. return -EFAULT;
  2755. return 0;
  2756. #endif
  2757. case MBOX_CMD:
  2758. /*
  2759. * Which adapter
  2760. */
  2761. if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
  2762. return (-ENODEV);
  2763. adapter = hba_soft_state[adapno];
  2764. /*
  2765. * Deletion of logical drive is a special case. The adapter
  2766. * should be quiescent before this command is issued.
  2767. */
  2768. if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
  2769. uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
  2770. /*
  2771. * Do we support this feature
  2772. */
  2773. if( !adapter->support_random_del ) {
  2774. printk(KERN_WARNING "megaraid: logdrv ");
  2775. printk("delete on non-supporting F/W.\n");
  2776. return (-EINVAL);
  2777. }
  2778. rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
  2779. if( rval == 0 ) {
  2780. memset(&mc, 0, sizeof(megacmd_t));
  2781. mc.status = rval;
  2782. rval = mega_n_to_m((void __user *)arg, &mc);
  2783. }
  2784. return rval;
  2785. }
  2786. /*
  2787. * This interface only support the regular passthru commands.
  2788. * Reject extended passthru and 64-bit passthru
  2789. */
  2790. if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
  2791. uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
  2792. printk(KERN_WARNING "megaraid: rejected passthru.\n");
  2793. return (-EINVAL);
  2794. }
  2795. /*
  2796. * For all internal commands, the buffer must be allocated in
  2797. * <4GB address range
  2798. */
  2799. if( make_local_pdev(adapter, &pdev) != 0 )
  2800. return -EIO;
  2801. /* Is it a passthru command or a DCMD */
  2802. if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
  2803. /* Passthru commands */
  2804. pthru = pci_alloc_consistent(pdev,
  2805. sizeof(mega_passthru),
  2806. &pthru_dma_hndl);
  2807. if( pthru == NULL ) {
  2808. free_local_pdev(pdev);
  2809. return (-ENOMEM);
  2810. }
  2811. /*
  2812. * The user passthru structure
  2813. */
  2814. upthru = (mega_passthru __user *)MBOX(uioc)->xferaddr;
  2815. /*
  2816. * Copy in the user passthru here.
  2817. */
  2818. if( copy_from_user(pthru, upthru,
  2819. sizeof(mega_passthru)) ) {
  2820. pci_free_consistent(pdev,
  2821. sizeof(mega_passthru), pthru,
  2822. pthru_dma_hndl);
  2823. free_local_pdev(pdev);
  2824. return (-EFAULT);
  2825. }
  2826. /*
  2827. * Is there a data transfer
  2828. */
  2829. if( pthru->dataxferlen ) {
  2830. data = pci_alloc_consistent(pdev,
  2831. pthru->dataxferlen,
  2832. &data_dma_hndl);
  2833. if( data == NULL ) {
  2834. pci_free_consistent(pdev,
  2835. sizeof(mega_passthru),
  2836. pthru,
  2837. pthru_dma_hndl);
  2838. free_local_pdev(pdev);
  2839. return (-ENOMEM);
  2840. }
  2841. /*
  2842. * Save the user address and point the kernel
  2843. * address at just allocated memory
  2844. */
  2845. uxferaddr = pthru->dataxferaddr;
  2846. pthru->dataxferaddr = data_dma_hndl;
  2847. }
  2848. /*
  2849. * Is data coming down-stream
  2850. */
  2851. if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
  2852. /*
  2853. * Get the user data
  2854. */
  2855. if( copy_from_user(data, (char __user *)uxferaddr,
  2856. pthru->dataxferlen) ) {
  2857. rval = (-EFAULT);
  2858. goto freemem_and_return;
  2859. }
  2860. }
  2861. memset(&mc, 0, sizeof(megacmd_t));
  2862. mc.cmd = MEGA_MBOXCMD_PASSTHRU;
  2863. mc.xferaddr = (u32)pthru_dma_hndl;
  2864. /*
  2865. * Issue the command
  2866. */
  2867. mega_internal_command(adapter, LOCK_INT, &mc, pthru);
  2868. rval = mega_n_to_m((void __user *)arg, &mc);
  2869. if( rval ) goto freemem_and_return;
  2870. /*
  2871. * Is data going up-stream
  2872. */
  2873. if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
  2874. if( copy_to_user((char __user *)uxferaddr, data,
  2875. pthru->dataxferlen) ) {
  2876. rval = (-EFAULT);
  2877. }
  2878. }
  2879. /*
  2880. * Send the request sense data also, irrespective of
  2881. * whether the user has asked for it or not.
  2882. */
  2883. copy_to_user(upthru->reqsensearea,
  2884. pthru->reqsensearea, 14);
  2885. freemem_and_return:
  2886. if( pthru->dataxferlen ) {
  2887. pci_free_consistent(pdev,
  2888. pthru->dataxferlen, data,
  2889. data_dma_hndl);
  2890. }
  2891. pci_free_consistent(pdev, sizeof(mega_passthru),
  2892. pthru, pthru_dma_hndl);
  2893. free_local_pdev(pdev);
  2894. return rval;
  2895. }
  2896. else {
  2897. /* DCMD commands */
  2898. /*
  2899. * Is there a data transfer
  2900. */
  2901. if( uioc.xferlen ) {
  2902. data = pci_alloc_consistent(pdev,
  2903. uioc.xferlen, &data_dma_hndl);
  2904. if( data == NULL ) {
  2905. free_local_pdev(pdev);
  2906. return (-ENOMEM);
  2907. }
  2908. uxferaddr = MBOX(uioc)->xferaddr;
  2909. }
  2910. /*
  2911. * Is data coming down-stream
  2912. */
  2913. if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
  2914. /*
  2915. * Get the user data
  2916. */
  2917. if( copy_from_user(data, (char __user *)uxferaddr,
  2918. uioc.xferlen) ) {
  2919. pci_free_consistent(pdev,
  2920. uioc.xferlen,
  2921. data, data_dma_hndl);
  2922. free_local_pdev(pdev);
  2923. return (-EFAULT);
  2924. }
  2925. }
  2926. memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
  2927. mc.xferaddr = (u32)data_dma_hndl;
  2928. /*
  2929. * Issue the command
  2930. */
  2931. mega_internal_command(adapter, LOCK_INT, &mc, NULL);
  2932. rval = mega_n_to_m((void __user *)arg, &mc);
  2933. if( rval ) {
  2934. if( uioc.xferlen ) {
  2935. pci_free_consistent(pdev,
  2936. uioc.xferlen, data,
  2937. data_dma_hndl);
  2938. }
  2939. free_local_pdev(pdev);
  2940. return rval;
  2941. }
  2942. /*
  2943. * Is data going up-stream
  2944. */
  2945. if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
  2946. if( copy_to_user((char __user *)uxferaddr, data,
  2947. uioc.xferlen) ) {
  2948. rval = (-EFAULT);
  2949. }
  2950. }
  2951. if( uioc.xferlen ) {
  2952. pci_free_consistent(pdev,
  2953. uioc.xferlen, data,
  2954. data_dma_hndl);
  2955. }
  2956. free_local_pdev(pdev);
  2957. return rval;
  2958. }
  2959. default:
  2960. return (-EINVAL);
  2961. }
  2962. return 0;
  2963. }
  2964. /**
  2965. * mega_m_to_n()
  2966. * @arg - user address
  2967. * @uioc - new ioctl structure
  2968. *
  2969. * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
  2970. * structure
  2971. *
  2972. * Converts the older mimd ioctl structure to newer NIT structure
  2973. */
  2974. static int
  2975. mega_m_to_n(void __user *arg, nitioctl_t *uioc)
  2976. {
  2977. struct uioctl_t uioc_mimd;
  2978. char signature[8] = {0};
  2979. u8 opcode;
  2980. u8 subopcode;
  2981. /*
  2982. * check is the application conforms to NIT. We do not have to do much
  2983. * in that case.
  2984. * We exploit the fact that the signature is stored in the very
  2985. * begining of the structure.
  2986. */
  2987. if( copy_from_user(signature, arg, 7) )
  2988. return (-EFAULT);
  2989. if( memcmp(signature, "MEGANIT", 7) == 0 ) {
  2990. /*
  2991. * NOTE NOTE: The nit ioctl is still under flux because of
  2992. * change of mailbox definition, in HPE. No applications yet
  2993. * use this interface and let's not have applications use this
  2994. * interface till the new specifitions are in place.
  2995. */
  2996. return -EINVAL;
  2997. #if 0
  2998. if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
  2999. return (-EFAULT);
  3000. return 0;
  3001. #endif
  3002. }
  3003. /*
  3004. * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
  3005. *
  3006. * Get the user ioctl structure
  3007. */
  3008. if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
  3009. return (-EFAULT);
  3010. /*
  3011. * Get the opcode and subopcode for the commands
  3012. */
  3013. opcode = uioc_mimd.ui.fcs.opcode;
  3014. subopcode = uioc_mimd.ui.fcs.subopcode;
  3015. switch (opcode) {
  3016. case 0x82:
  3017. switch (subopcode) {
  3018. case MEGAIOC_QDRVRVER: /* Query driver version */
  3019. uioc->opcode = GET_DRIVER_VER;
  3020. uioc->uioc_uaddr = uioc_mimd.data;
  3021. break;
  3022. case MEGAIOC_QNADAP: /* Get # of adapters */
  3023. uioc->opcode = GET_N_ADAP;
  3024. uioc->uioc_uaddr = uioc_mimd.data;
  3025. break;
  3026. case MEGAIOC_QADAPINFO: /* Get adapter information */
  3027. uioc->opcode = GET_ADAP_INFO;
  3028. uioc->adapno = uioc_mimd.ui.fcs.adapno;
  3029. uioc->uioc_uaddr = uioc_mimd.data;
  3030. break;
  3031. default:
  3032. return(-EINVAL);
  3033. }
  3034. break;
  3035. case 0x81:
  3036. uioc->opcode = MBOX_CMD;
  3037. uioc->adapno = uioc_mimd.ui.fcs.adapno;
  3038. memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
  3039. uioc->xferlen = uioc_mimd.ui.fcs.length;
  3040. if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
  3041. if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
  3042. break;
  3043. case 0x80:
  3044. uioc->opcode = MBOX_CMD;
  3045. uioc->adapno = uioc_mimd.ui.fcs.adapno;
  3046. memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
  3047. /*
  3048. * Choose the xferlen bigger of input and output data
  3049. */
  3050. uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
  3051. uioc_mimd.outlen : uioc_mimd.inlen;
  3052. if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
  3053. if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
  3054. break;
  3055. default:
  3056. return (-EINVAL);
  3057. }
  3058. return 0;
  3059. }
  3060. /*
  3061. * mega_n_to_m()
  3062. * @arg - user address
  3063. * @mc - mailbox command
  3064. *
  3065. * Updates the status information to the application, depending on application
  3066. * conforms to older mimd ioctl interface or newer NIT ioctl interface
  3067. */
  3068. static int
  3069. mega_n_to_m(void __user *arg, megacmd_t *mc)
  3070. {
  3071. nitioctl_t __user *uiocp;
  3072. megacmd_t __user *umc;
  3073. mega_passthru __user *upthru;
  3074. struct uioctl_t __user *uioc_mimd;
  3075. char signature[8] = {0};
  3076. /*
  3077. * check is the application conforms to NIT.
  3078. */
  3079. if( copy_from_user(signature, arg, 7) )
  3080. return -EFAULT;
  3081. if( memcmp(signature, "MEGANIT", 7) == 0 ) {
  3082. uiocp = arg;
  3083. if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
  3084. return (-EFAULT);
  3085. if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
  3086. umc = MBOX_P(uiocp);
  3087. if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
  3088. return -EFAULT;
  3089. if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
  3090. return (-EFAULT);
  3091. }
  3092. }
  3093. else {
  3094. uioc_mimd = arg;
  3095. if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
  3096. return (-EFAULT);
  3097. if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
  3098. umc = (megacmd_t __user *)uioc_mimd->mbox;
  3099. if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
  3100. return (-EFAULT);
  3101. if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
  3102. return (-EFAULT);
  3103. }
  3104. }
  3105. return 0;
  3106. }
  3107. /*
  3108. * MEGARAID 'FW' commands.
  3109. */
  3110. /**
  3111. * mega_is_bios_enabled()
  3112. * @adapter - pointer to our soft state
  3113. *
  3114. * issue command to find out if the BIOS is enabled for this controller
  3115. */
  3116. static int
  3117. mega_is_bios_enabled(adapter_t *adapter)
  3118. {
  3119. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3120. mbox_t *mbox;
  3121. int ret;
  3122. mbox = (mbox_t *)raw_mbox;
  3123. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3124. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3125. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3126. raw_mbox[0] = IS_BIOS_ENABLED;
  3127. raw_mbox[2] = GET_BIOS;
  3128. ret = issue_scb_block(adapter, raw_mbox);
  3129. return *(char *)adapter->mega_buffer;
  3130. }
  3131. /**
  3132. * mega_enum_raid_scsi()
  3133. * @adapter - pointer to our soft state
  3134. *
  3135. * Find out what channels are RAID/SCSI. This information is used to
  3136. * differentiate the virtual channels and physical channels and to support
  3137. * ROMB feature and non-disk devices.
  3138. */
  3139. static void
  3140. mega_enum_raid_scsi(adapter_t *adapter)
  3141. {
  3142. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3143. mbox_t *mbox;
  3144. int i;
  3145. mbox = (mbox_t *)raw_mbox;
  3146. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3147. /*
  3148. * issue command to find out what channels are raid/scsi
  3149. */
  3150. raw_mbox[0] = CHNL_CLASS;
  3151. raw_mbox[2] = GET_CHNL_CLASS;
  3152. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3153. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3154. /*
  3155. * Non-ROMB firmware fail this command, so all channels
  3156. * must be shown RAID
  3157. */
  3158. adapter->mega_ch_class = 0xFF;
  3159. if(!issue_scb_block(adapter, raw_mbox)) {
  3160. adapter->mega_ch_class = *((char *)adapter->mega_buffer);
  3161. }
  3162. for( i = 0; i < adapter->product_info.nchannels; i++ ) {
  3163. if( (adapter->mega_ch_class >> i) & 0x01 ) {
  3164. printk(KERN_INFO "megaraid: channel[%d] is raid.\n",
  3165. i);
  3166. }
  3167. else {
  3168. printk(KERN_INFO "megaraid: channel[%d] is scsi.\n",
  3169. i);
  3170. }
  3171. }
  3172. return;
  3173. }
  3174. /**
  3175. * mega_get_boot_drv()
  3176. * @adapter - pointer to our soft state
  3177. *
  3178. * Find out which device is the boot device. Note, any logical drive or any
  3179. * phyical device (e.g., a CDROM) can be designated as a boot device.
  3180. */
  3181. static void
  3182. mega_get_boot_drv(adapter_t *adapter)
  3183. {
  3184. struct private_bios_data *prv_bios_data;
  3185. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3186. mbox_t *mbox;
  3187. u16 cksum = 0;
  3188. u8 *cksum_p;
  3189. u8 boot_pdrv;
  3190. int i;
  3191. mbox = (mbox_t *)raw_mbox;
  3192. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3193. raw_mbox[0] = BIOS_PVT_DATA;
  3194. raw_mbox[2] = GET_BIOS_PVT_DATA;
  3195. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3196. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3197. adapter->boot_ldrv_enabled = 0;
  3198. adapter->boot_ldrv = 0;
  3199. adapter->boot_pdrv_enabled = 0;
  3200. adapter->boot_pdrv_ch = 0;
  3201. adapter->boot_pdrv_tgt = 0;
  3202. if(issue_scb_block(adapter, raw_mbox) == 0) {
  3203. prv_bios_data =
  3204. (struct private_bios_data *)adapter->mega_buffer;
  3205. cksum = 0;
  3206. cksum_p = (char *)prv_bios_data;
  3207. for (i = 0; i < 14; i++ ) {
  3208. cksum += (u16)(*cksum_p++);
  3209. }
  3210. if (prv_bios_data->cksum == (u16)(0-cksum) ) {
  3211. /*
  3212. * If MSB is set, a physical drive is set as boot
  3213. * device
  3214. */
  3215. if( prv_bios_data->boot_drv & 0x80 ) {
  3216. adapter->boot_pdrv_enabled = 1;
  3217. boot_pdrv = prv_bios_data->boot_drv & 0x7F;
  3218. adapter->boot_pdrv_ch = boot_pdrv / 16;
  3219. adapter->boot_pdrv_tgt = boot_pdrv % 16;
  3220. }
  3221. else {
  3222. adapter->boot_ldrv_enabled = 1;
  3223. adapter->boot_ldrv = prv_bios_data->boot_drv;
  3224. }
  3225. }
  3226. }
  3227. }
  3228. /**
  3229. * mega_support_random_del()
  3230. * @adapter - pointer to our soft state
  3231. *
  3232. * Find out if this controller supports random deletion and addition of
  3233. * logical drives
  3234. */
  3235. static int
  3236. mega_support_random_del(adapter_t *adapter)
  3237. {
  3238. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3239. mbox_t *mbox;
  3240. int rval;
  3241. mbox = (mbox_t *)raw_mbox;
  3242. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3243. /*
  3244. * issue command
  3245. */
  3246. raw_mbox[0] = FC_DEL_LOGDRV;
  3247. raw_mbox[2] = OP_SUP_DEL_LOGDRV;
  3248. rval = issue_scb_block(adapter, raw_mbox);
  3249. return !rval;
  3250. }
  3251. /**
  3252. * mega_support_ext_cdb()
  3253. * @adapter - pointer to our soft state
  3254. *
  3255. * Find out if this firmware support cdblen > 10
  3256. */
  3257. static int
  3258. mega_support_ext_cdb(adapter_t *adapter)
  3259. {
  3260. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3261. mbox_t *mbox;
  3262. int rval;
  3263. mbox = (mbox_t *)raw_mbox;
  3264. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3265. /*
  3266. * issue command to find out if controller supports extended CDBs.
  3267. */
  3268. raw_mbox[0] = 0xA4;
  3269. raw_mbox[2] = 0x16;
  3270. rval = issue_scb_block(adapter, raw_mbox);
  3271. return !rval;
  3272. }
  3273. /**
  3274. * mega_del_logdrv()
  3275. * @adapter - pointer to our soft state
  3276. * @logdrv - logical drive to be deleted
  3277. *
  3278. * Delete the specified logical drive. It is the responsibility of the user
  3279. * app to let the OS know about this operation.
  3280. */
  3281. static int
  3282. mega_del_logdrv(adapter_t *adapter, int logdrv)
  3283. {
  3284. unsigned long flags;
  3285. scb_t *scb;
  3286. int rval;
  3287. /*
  3288. * Stop sending commands to the controller, queue them internally.
  3289. * When deletion is complete, ISR will flush the queue.
  3290. */
  3291. atomic_set(&adapter->quiescent, 1);
  3292. /*
  3293. * Wait till all the issued commands are complete and there are no
  3294. * commands in the pending queue
  3295. */
  3296. while (atomic_read(&adapter->pend_cmds) > 0 ||
  3297. !list_empty(&adapter->pending_list))
  3298. msleep(1000); /* sleep for 1s */
  3299. rval = mega_do_del_logdrv(adapter, logdrv);
  3300. spin_lock_irqsave(&adapter->lock, flags);
  3301. /*
  3302. * If delete operation was successful, add 0x80 to the logical drive
  3303. * ids for commands in the pending queue.
  3304. */
  3305. if (adapter->read_ldidmap) {
  3306. struct list_head *pos;
  3307. list_for_each(pos, &adapter->pending_list) {
  3308. scb = list_entry(pos, scb_t, list);
  3309. if (scb->pthru->logdrv < 0x80 )
  3310. scb->pthru->logdrv += 0x80;
  3311. }
  3312. }
  3313. atomic_set(&adapter->quiescent, 0);
  3314. mega_runpendq(adapter);
  3315. spin_unlock_irqrestore(&adapter->lock, flags);
  3316. return rval;
  3317. }
  3318. static int
  3319. mega_do_del_logdrv(adapter_t *adapter, int logdrv)
  3320. {
  3321. megacmd_t mc;
  3322. int rval;
  3323. memset( &mc, 0, sizeof(megacmd_t));
  3324. mc.cmd = FC_DEL_LOGDRV;
  3325. mc.opcode = OP_DEL_LOGDRV;
  3326. mc.subopcode = logdrv;
  3327. rval = mega_internal_command(adapter, LOCK_INT, &mc, NULL);
  3328. /* log this event */
  3329. if(rval) {
  3330. printk(KERN_WARNING "megaraid: Delete LD-%d failed.", logdrv);
  3331. return rval;
  3332. }
  3333. /*
  3334. * After deleting first logical drive, the logical drives must be
  3335. * addressed by adding 0x80 to the logical drive id.
  3336. */
  3337. adapter->read_ldidmap = 1;
  3338. return rval;
  3339. }
  3340. /**
  3341. * mega_get_max_sgl()
  3342. * @adapter - pointer to our soft state
  3343. *
  3344. * Find out the maximum number of scatter-gather elements supported by this
  3345. * version of the firmware
  3346. */
  3347. static void
  3348. mega_get_max_sgl(adapter_t *adapter)
  3349. {
  3350. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3351. mbox_t *mbox;
  3352. mbox = (mbox_t *)raw_mbox;
  3353. memset(mbox, 0, sizeof(raw_mbox));
  3354. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3355. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3356. raw_mbox[0] = MAIN_MISC_OPCODE;
  3357. raw_mbox[2] = GET_MAX_SG_SUPPORT;
  3358. if( issue_scb_block(adapter, raw_mbox) ) {
  3359. /*
  3360. * f/w does not support this command. Choose the default value
  3361. */
  3362. adapter->sglen = MIN_SGLIST;
  3363. }
  3364. else {
  3365. adapter->sglen = *((char *)adapter->mega_buffer);
  3366. /*
  3367. * Make sure this is not more than the resources we are
  3368. * planning to allocate
  3369. */
  3370. if ( adapter->sglen > MAX_SGLIST )
  3371. adapter->sglen = MAX_SGLIST;
  3372. }
  3373. return;
  3374. }
  3375. /**
  3376. * mega_support_cluster()
  3377. * @adapter - pointer to our soft state
  3378. *
  3379. * Find out if this firmware support cluster calls.
  3380. */
  3381. static int
  3382. mega_support_cluster(adapter_t *adapter)
  3383. {
  3384. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3385. mbox_t *mbox;
  3386. mbox = (mbox_t *)raw_mbox;
  3387. memset(mbox, 0, sizeof(raw_mbox));
  3388. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3389. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3390. /*
  3391. * Try to get the initiator id. This command will succeed iff the
  3392. * clustering is available on this HBA.
  3393. */
  3394. raw_mbox[0] = MEGA_GET_TARGET_ID;
  3395. if( issue_scb_block(adapter, raw_mbox) == 0 ) {
  3396. /*
  3397. * Cluster support available. Get the initiator target id.
  3398. * Tell our id to mid-layer too.
  3399. */
  3400. adapter->this_id = *(u32 *)adapter->mega_buffer;
  3401. adapter->host->this_id = adapter->this_id;
  3402. return 1;
  3403. }
  3404. return 0;
  3405. }
  3406. /**
  3407. * mega_adapinq()
  3408. * @adapter - pointer to our soft state
  3409. * @dma_handle - DMA address of the buffer
  3410. *
  3411. * Issue internal comamnds while interrupts are available.
  3412. * We only issue direct mailbox commands from within the driver. ioctl()
  3413. * interface using these routines can issue passthru commands.
  3414. */
  3415. static int
  3416. mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
  3417. {
  3418. megacmd_t mc;
  3419. memset(&mc, 0, sizeof(megacmd_t));
  3420. if( adapter->flag & BOARD_40LD ) {
  3421. mc.cmd = FC_NEW_CONFIG;
  3422. mc.opcode = NC_SUBOP_ENQUIRY3;
  3423. mc.subopcode = ENQ3_GET_SOLICITED_FULL;
  3424. }
  3425. else {
  3426. mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
  3427. }
  3428. mc.xferaddr = (u32)dma_handle;
  3429. if ( mega_internal_command(adapter, LOCK_INT, &mc, NULL) != 0 ) {
  3430. return -1;
  3431. }
  3432. return 0;
  3433. }
  3434. /** mega_internal_dev_inquiry()
  3435. * @adapter - pointer to our soft state
  3436. * @ch - channel for this device
  3437. * @tgt - ID of this device
  3438. * @buf_dma_handle - DMA address of the buffer
  3439. *
  3440. * Issue the scsi inquiry for the specified device.
  3441. */
  3442. static int
  3443. mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
  3444. dma_addr_t buf_dma_handle)
  3445. {
  3446. mega_passthru *pthru;
  3447. dma_addr_t pthru_dma_handle;
  3448. megacmd_t mc;
  3449. int rval;
  3450. struct pci_dev *pdev;
  3451. /*
  3452. * For all internal commands, the buffer must be allocated in <4GB
  3453. * address range
  3454. */
  3455. if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
  3456. pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
  3457. &pthru_dma_handle);
  3458. if( pthru == NULL ) {
  3459. free_local_pdev(pdev);
  3460. return -1;
  3461. }
  3462. pthru->timeout = 2;
  3463. pthru->ars = 1;
  3464. pthru->reqsenselen = 14;
  3465. pthru->islogical = 0;
  3466. pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
  3467. pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
  3468. pthru->cdblen = 6;
  3469. pthru->cdb[0] = INQUIRY;
  3470. pthru->cdb[1] = 0;
  3471. pthru->cdb[2] = 0;
  3472. pthru->cdb[3] = 0;
  3473. pthru->cdb[4] = 255;
  3474. pthru->cdb[5] = 0;
  3475. pthru->dataxferaddr = (u32)buf_dma_handle;
  3476. pthru->dataxferlen = 256;
  3477. memset(&mc, 0, sizeof(megacmd_t));
  3478. mc.cmd = MEGA_MBOXCMD_PASSTHRU;
  3479. mc.xferaddr = (u32)pthru_dma_handle;
  3480. rval = mega_internal_command(adapter, LOCK_INT, &mc, pthru);
  3481. pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
  3482. pthru_dma_handle);
  3483. free_local_pdev(pdev);
  3484. return rval;
  3485. }
  3486. /**
  3487. * mega_internal_command()
  3488. * @adapter - pointer to our soft state
  3489. * @ls - the scope of the exclusion lock.
  3490. * @mc - the mailbox command
  3491. * @pthru - Passthru structure for DCDB commands
  3492. *
  3493. * Issue the internal commands in interrupt mode.
  3494. * The last argument is the address of the passthru structure if the command
  3495. * to be fired is a passthru command
  3496. *
  3497. * lockscope specifies whether the caller has already acquired the lock. Of
  3498. * course, the caller must know which lock we are talking about.
  3499. *
  3500. * Note: parameter 'pthru' is null for non-passthru commands.
  3501. */
  3502. static int
  3503. mega_internal_command(adapter_t *adapter, lockscope_t ls, megacmd_t *mc,
  3504. mega_passthru *pthru )
  3505. {
  3506. Scsi_Cmnd *scmd;
  3507. struct scsi_device *sdev;
  3508. unsigned long flags = 0;
  3509. scb_t *scb;
  3510. int rval;
  3511. /*
  3512. * The internal commands share one command id and hence are
  3513. * serialized. This is so because we want to reserve maximum number of
  3514. * available command ids for the I/O commands.
  3515. */
  3516. down(&adapter->int_mtx);
  3517. scb = &adapter->int_scb;
  3518. memset(scb, 0, sizeof(scb_t));
  3519. scmd = &adapter->int_scmd;
  3520. memset(scmd, 0, sizeof(Scsi_Cmnd));
  3521. sdev = kmalloc(sizeof(struct scsi_device), GFP_KERNEL);
  3522. memset(sdev, 0, sizeof(struct scsi_device));
  3523. scmd->device = sdev;
  3524. scmd->device->host = adapter->host;
  3525. scmd->buffer = (void *)scb;
  3526. scmd->cmnd[0] = MEGA_INTERNAL_CMD;
  3527. scb->state |= SCB_ACTIVE;
  3528. scb->cmd = scmd;
  3529. memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
  3530. /*
  3531. * Is it a passthru command
  3532. */
  3533. if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
  3534. scb->pthru = pthru;
  3535. }
  3536. scb->idx = CMDID_INT_CMDS;
  3537. scmd->state = 0;
  3538. /*
  3539. * Get the lock only if the caller has not acquired it already
  3540. */
  3541. if( ls == LOCK_INT ) spin_lock_irqsave(&adapter->lock, flags);
  3542. megaraid_queue(scmd, mega_internal_done);
  3543. if( ls == LOCK_INT ) spin_unlock_irqrestore(&adapter->lock, flags);
  3544. /*
  3545. * Wait till this command finishes. Do not use
  3546. * wait_event_interruptible(). It causes panic if CTRL-C is hit when
  3547. * dumping e.g., physical disk information through /proc interface.
  3548. */
  3549. #if 0
  3550. wait_event_interruptible(adapter->int_waitq, scmd->state);
  3551. #endif
  3552. wait_event(adapter->int_waitq, scmd->state);
  3553. rval = scmd->result;
  3554. mc->status = scmd->result;
  3555. kfree(sdev);
  3556. /*
  3557. * Print a debug message for all failed commands. Applications can use
  3558. * this information.
  3559. */
  3560. if( scmd->result && trace_level ) {
  3561. printk("megaraid: cmd [%x, %x, %x] status:[%x]\n",
  3562. mc->cmd, mc->opcode, mc->subopcode, scmd->result);
  3563. }
  3564. up(&adapter->int_mtx);
  3565. return rval;
  3566. }
  3567. /**
  3568. * mega_internal_done()
  3569. * @scmd - internal scsi command
  3570. *
  3571. * Callback routine for internal commands.
  3572. */
  3573. static void
  3574. mega_internal_done(Scsi_Cmnd *scmd)
  3575. {
  3576. adapter_t *adapter;
  3577. adapter = (adapter_t *)scmd->device->host->hostdata;
  3578. scmd->state = 1; /* thread waiting for its command to complete */
  3579. /*
  3580. * See comment in mega_internal_command() routine for
  3581. * wait_event_interruptible()
  3582. */
  3583. #if 0
  3584. wake_up_interruptible(&adapter->int_waitq);
  3585. #endif
  3586. wake_up(&adapter->int_waitq);
  3587. }
  3588. static struct scsi_host_template megaraid_template = {
  3589. .module = THIS_MODULE,
  3590. .name = "MegaRAID",
  3591. .proc_name = "megaraid",
  3592. .info = megaraid_info,
  3593. .queuecommand = megaraid_queue,
  3594. .bios_param = megaraid_biosparam,
  3595. .max_sectors = MAX_SECTORS_PER_IO,
  3596. .can_queue = MAX_COMMANDS,
  3597. .this_id = DEFAULT_INITIATOR_ID,
  3598. .sg_tablesize = MAX_SGLIST,
  3599. .cmd_per_lun = DEF_CMD_PER_LUN,
  3600. .use_clustering = ENABLE_CLUSTERING,
  3601. .eh_abort_handler = megaraid_abort,
  3602. .eh_device_reset_handler = megaraid_reset,
  3603. .eh_bus_reset_handler = megaraid_reset,
  3604. .eh_host_reset_handler = megaraid_reset,
  3605. };
  3606. static int __devinit
  3607. megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  3608. {
  3609. struct Scsi_Host *host;
  3610. adapter_t *adapter;
  3611. unsigned long mega_baseport, tbase, flag = 0;
  3612. u16 subsysid, subsysvid;
  3613. u8 pci_bus, pci_dev_func;
  3614. int irq, i, j;
  3615. int error = -ENODEV;
  3616. if (pci_enable_device(pdev))
  3617. goto out;
  3618. pci_set_master(pdev);
  3619. pci_bus = pdev->bus->number;
  3620. pci_dev_func = pdev->devfn;
  3621. /*
  3622. * The megaraid3 stuff reports the ID of the Intel part which is not
  3623. * remotely specific to the megaraid
  3624. */
  3625. if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
  3626. u16 magic;
  3627. /*
  3628. * Don't fall over the Compaq management cards using the same
  3629. * PCI identifier
  3630. */
  3631. if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
  3632. pdev->subsystem_device == 0xC000)
  3633. return -ENODEV;
  3634. /* Now check the magic signature byte */
  3635. pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
  3636. if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
  3637. return -ENODEV;
  3638. /* Ok it is probably a megaraid */
  3639. }
  3640. /*
  3641. * For these vendor and device ids, signature offsets are not
  3642. * valid and 64 bit is implicit
  3643. */
  3644. if (id->driver_data & BOARD_64BIT)
  3645. flag |= BOARD_64BIT;
  3646. else {
  3647. u32 magic64;
  3648. pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
  3649. if (magic64 == HBA_SIGNATURE_64BIT)
  3650. flag |= BOARD_64BIT;
  3651. }
  3652. subsysvid = pdev->subsystem_vendor;
  3653. subsysid = pdev->subsystem_device;
  3654. printk(KERN_NOTICE "megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
  3655. id->vendor, id->device, pci_bus);
  3656. printk("slot %d:func %d\n",
  3657. PCI_SLOT(pci_dev_func), PCI_FUNC(pci_dev_func));
  3658. /* Read the base port and IRQ from PCI */
  3659. mega_baseport = pci_resource_start(pdev, 0);
  3660. irq = pdev->irq;
  3661. tbase = mega_baseport;
  3662. if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
  3663. flag |= BOARD_MEMMAP;
  3664. if (!request_mem_region(mega_baseport, 128, "megaraid")) {
  3665. printk(KERN_WARNING "megaraid: mem region busy!\n");
  3666. goto out_disable_device;
  3667. }
  3668. mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
  3669. if (!mega_baseport) {
  3670. printk(KERN_WARNING
  3671. "megaraid: could not map hba memory\n");
  3672. goto out_release_region;
  3673. }
  3674. } else {
  3675. flag |= BOARD_IOMAP;
  3676. mega_baseport += 0x10;
  3677. if (!request_region(mega_baseport, 16, "megaraid"))
  3678. goto out_disable_device;
  3679. }
  3680. /* Initialize SCSI Host structure */
  3681. host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
  3682. if (!host)
  3683. goto out_iounmap;
  3684. adapter = (adapter_t *)host->hostdata;
  3685. memset(adapter, 0, sizeof(adapter_t));
  3686. printk(KERN_NOTICE
  3687. "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
  3688. host->host_no, mega_baseport, irq);
  3689. adapter->base = mega_baseport;
  3690. INIT_LIST_HEAD(&adapter->free_list);
  3691. INIT_LIST_HEAD(&adapter->pending_list);
  3692. INIT_LIST_HEAD(&adapter->completed_list);
  3693. adapter->flag = flag;
  3694. spin_lock_init(&adapter->lock);
  3695. scsi_assign_lock(host, &adapter->lock);
  3696. host->cmd_per_lun = max_cmd_per_lun;
  3697. host->max_sectors = max_sectors_per_io;
  3698. adapter->dev = pdev;
  3699. adapter->host = host;
  3700. adapter->host->irq = irq;
  3701. if (flag & BOARD_MEMMAP)
  3702. adapter->host->base = tbase;
  3703. else {
  3704. adapter->host->io_port = tbase;
  3705. adapter->host->n_io_port = 16;
  3706. }
  3707. adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
  3708. /*
  3709. * Allocate buffer to issue internal commands.
  3710. */
  3711. adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
  3712. MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
  3713. if (!adapter->mega_buffer) {
  3714. printk(KERN_WARNING "megaraid: out of RAM.\n");
  3715. goto out_host_put;
  3716. }
  3717. adapter->scb_list = kmalloc(sizeof(scb_t) * MAX_COMMANDS, GFP_KERNEL);
  3718. if (!adapter->scb_list) {
  3719. printk(KERN_WARNING "megaraid: out of RAM.\n");
  3720. goto out_free_cmd_buffer;
  3721. }
  3722. if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
  3723. megaraid_isr_memmapped : megaraid_isr_iomapped,
  3724. SA_SHIRQ, "megaraid", adapter)) {
  3725. printk(KERN_WARNING
  3726. "megaraid: Couldn't register IRQ %d!\n", irq);
  3727. goto out_free_scb_list;
  3728. }
  3729. if (mega_setup_mailbox(adapter))
  3730. goto out_free_irq;
  3731. if (mega_query_adapter(adapter))
  3732. goto out_free_mbox;
  3733. /*
  3734. * Have checks for some buggy f/w
  3735. */
  3736. if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
  3737. /*
  3738. * Which firmware
  3739. */
  3740. if (!strcmp(adapter->fw_version, "3.00") ||
  3741. !strcmp(adapter->fw_version, "3.01")) {
  3742. printk( KERN_WARNING
  3743. "megaraid: Your card is a Dell PERC "
  3744. "2/SC RAID controller with "
  3745. "firmware\nmegaraid: 3.00 or 3.01. "
  3746. "This driver is known to have "
  3747. "corruption issues\nmegaraid: with "
  3748. "those firmware versions on this "
  3749. "specific card. In order\nmegaraid: "
  3750. "to protect your data, please upgrade "
  3751. "your firmware to version\nmegaraid: "
  3752. "3.10 or later, available from the "
  3753. "Dell Technical Support web\n"
  3754. "megaraid: site at\nhttp://support."
  3755. "dell.com/us/en/filelib/download/"
  3756. "index.asp?fileid=2940\n"
  3757. );
  3758. }
  3759. }
  3760. /*
  3761. * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
  3762. * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
  3763. * support, since this firmware cannot handle 64 bit
  3764. * addressing
  3765. */
  3766. if ((subsysvid == HP_SUBSYS_VID) &&
  3767. ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
  3768. /*
  3769. * which firmware
  3770. */
  3771. if (!strcmp(adapter->fw_version, "H01.07") ||
  3772. !strcmp(adapter->fw_version, "H01.08") ||
  3773. !strcmp(adapter->fw_version, "H01.09") ) {
  3774. printk(KERN_WARNING
  3775. "megaraid: Firmware H.01.07, "
  3776. "H.01.08, and H.01.09 on 1M/2M "
  3777. "controllers\n"
  3778. "megaraid: do not support 64 bit "
  3779. "addressing.\nmegaraid: DISABLING "
  3780. "64 bit support.\n");
  3781. adapter->flag &= ~BOARD_64BIT;
  3782. }
  3783. }
  3784. if (mega_is_bios_enabled(adapter))
  3785. mega_hbas[hba_count].is_bios_enabled = 1;
  3786. mega_hbas[hba_count].hostdata_addr = adapter;
  3787. /*
  3788. * Find out which channel is raid and which is scsi. This is
  3789. * for ROMB support.
  3790. */
  3791. mega_enum_raid_scsi(adapter);
  3792. /*
  3793. * Find out if a logical drive is set as the boot drive. If
  3794. * there is one, will make that as the first logical drive.
  3795. * ROMB: Do we have to boot from a physical drive. Then all
  3796. * the physical drives would appear before the logical disks.
  3797. * Else, all the physical drives would be exported to the mid
  3798. * layer after logical drives.
  3799. */
  3800. mega_get_boot_drv(adapter);
  3801. if (adapter->boot_pdrv_enabled) {
  3802. j = adapter->product_info.nchannels;
  3803. for( i = 0; i < j; i++ )
  3804. adapter->logdrv_chan[i] = 0;
  3805. for( i = j; i < NVIRT_CHAN + j; i++ )
  3806. adapter->logdrv_chan[i] = 1;
  3807. } else {
  3808. for (i = 0; i < NVIRT_CHAN; i++)
  3809. adapter->logdrv_chan[i] = 1;
  3810. for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
  3811. adapter->logdrv_chan[i] = 0;
  3812. adapter->mega_ch_class <<= NVIRT_CHAN;
  3813. }
  3814. /*
  3815. * Do we support random deletion and addition of logical
  3816. * drives
  3817. */
  3818. adapter->read_ldidmap = 0; /* set it after first logdrv
  3819. delete cmd */
  3820. adapter->support_random_del = mega_support_random_del(adapter);
  3821. /* Initialize SCBs */
  3822. if (mega_init_scb(adapter))
  3823. goto out_free_mbox;
  3824. /*
  3825. * Reset the pending commands counter
  3826. */
  3827. atomic_set(&adapter->pend_cmds, 0);
  3828. /*
  3829. * Reset the adapter quiescent flag
  3830. */
  3831. atomic_set(&adapter->quiescent, 0);
  3832. hba_soft_state[hba_count] = adapter;
  3833. /*
  3834. * Fill in the structure which needs to be passed back to the
  3835. * application when it does an ioctl() for controller related
  3836. * information.
  3837. */
  3838. i = hba_count;
  3839. mcontroller[i].base = mega_baseport;
  3840. mcontroller[i].irq = irq;
  3841. mcontroller[i].numldrv = adapter->numldrv;
  3842. mcontroller[i].pcibus = pci_bus;
  3843. mcontroller[i].pcidev = id->device;
  3844. mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
  3845. mcontroller[i].pciid = -1;
  3846. mcontroller[i].pcivendor = id->vendor;
  3847. mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
  3848. mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
  3849. /* Set the Mode of addressing to 64 bit if we can */
  3850. if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
  3851. pci_set_dma_mask(pdev, 0xffffffffffffffffULL);
  3852. adapter->has_64bit_addr = 1;
  3853. } else {
  3854. pci_set_dma_mask(pdev, 0xffffffff);
  3855. adapter->has_64bit_addr = 0;
  3856. }
  3857. init_MUTEX(&adapter->int_mtx);
  3858. init_waitqueue_head(&adapter->int_waitq);
  3859. adapter->this_id = DEFAULT_INITIATOR_ID;
  3860. adapter->host->this_id = DEFAULT_INITIATOR_ID;
  3861. #if MEGA_HAVE_CLUSTERING
  3862. /*
  3863. * Is cluster support enabled on this controller
  3864. * Note: In a cluster the HBAs ( the initiators ) will have
  3865. * different target IDs and we cannot assume it to be 7. Call
  3866. * to mega_support_cluster() will get the target ids also if
  3867. * the cluster support is available
  3868. */
  3869. adapter->has_cluster = mega_support_cluster(adapter);
  3870. if (adapter->has_cluster) {
  3871. printk(KERN_NOTICE
  3872. "megaraid: Cluster driver, initiator id:%d\n",
  3873. adapter->this_id);
  3874. }
  3875. #endif
  3876. pci_set_drvdata(pdev, host);
  3877. mega_create_proc_entry(hba_count, mega_proc_dir_entry);
  3878. error = scsi_add_host(host, &pdev->dev);
  3879. if (error)
  3880. goto out_free_mbox;
  3881. scsi_scan_host(host);
  3882. hba_count++;
  3883. return 0;
  3884. out_free_mbox:
  3885. pci_free_consistent(adapter->dev, sizeof(mbox64_t),
  3886. adapter->una_mbox64, adapter->una_mbox64_dma);
  3887. out_free_irq:
  3888. free_irq(adapter->host->irq, adapter);
  3889. out_free_scb_list:
  3890. kfree(adapter->scb_list);
  3891. out_free_cmd_buffer:
  3892. pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
  3893. adapter->mega_buffer, adapter->buf_dma_handle);
  3894. out_host_put:
  3895. scsi_host_put(host);
  3896. out_iounmap:
  3897. if (flag & BOARD_MEMMAP)
  3898. iounmap((void *)mega_baseport);
  3899. out_release_region:
  3900. if (flag & BOARD_MEMMAP)
  3901. release_mem_region(tbase, 128);
  3902. else
  3903. release_region(mega_baseport, 16);
  3904. out_disable_device:
  3905. pci_disable_device(pdev);
  3906. out:
  3907. return error;
  3908. }
  3909. static void
  3910. __megaraid_shutdown(adapter_t *adapter)
  3911. {
  3912. u_char raw_mbox[sizeof(struct mbox_out)];
  3913. mbox_t *mbox = (mbox_t *)raw_mbox;
  3914. int i;
  3915. /* Flush adapter cache */
  3916. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3917. raw_mbox[0] = FLUSH_ADAPTER;
  3918. free_irq(adapter->host->irq, adapter);
  3919. /* Issue a blocking (interrupts disabled) command to the card */
  3920. issue_scb_block(adapter, raw_mbox);
  3921. /* Flush disks cache */
  3922. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3923. raw_mbox[0] = FLUSH_SYSTEM;
  3924. /* Issue a blocking (interrupts disabled) command to the card */
  3925. issue_scb_block(adapter, raw_mbox);
  3926. if (atomic_read(&adapter->pend_cmds) > 0)
  3927. printk(KERN_WARNING "megaraid: pending commands!!\n");
  3928. /*
  3929. * Have a delibrate delay to make sure all the caches are
  3930. * actually flushed.
  3931. */
  3932. for (i = 0; i <= 10; i++)
  3933. mdelay(1000);
  3934. }
  3935. static void
  3936. megaraid_remove_one(struct pci_dev *pdev)
  3937. {
  3938. struct Scsi_Host *host = pci_get_drvdata(pdev);
  3939. adapter_t *adapter = (adapter_t *)host->hostdata;
  3940. char buf[12] = { 0 };
  3941. scsi_remove_host(host);
  3942. __megaraid_shutdown(adapter);
  3943. /* Free our resources */
  3944. if (adapter->flag & BOARD_MEMMAP) {
  3945. iounmap((void *)adapter->base);
  3946. release_mem_region(adapter->host->base, 128);
  3947. } else
  3948. release_region(adapter->base, 16);
  3949. mega_free_sgl(adapter);
  3950. #ifdef CONFIG_PROC_FS
  3951. if (adapter->controller_proc_dir_entry) {
  3952. remove_proc_entry("stat", adapter->controller_proc_dir_entry);
  3953. remove_proc_entry("config",
  3954. adapter->controller_proc_dir_entry);
  3955. remove_proc_entry("mailbox",
  3956. adapter->controller_proc_dir_entry);
  3957. #if MEGA_HAVE_ENH_PROC
  3958. remove_proc_entry("rebuild-rate",
  3959. adapter->controller_proc_dir_entry);
  3960. remove_proc_entry("battery-status",
  3961. adapter->controller_proc_dir_entry);
  3962. remove_proc_entry("diskdrives-ch0",
  3963. adapter->controller_proc_dir_entry);
  3964. remove_proc_entry("diskdrives-ch1",
  3965. adapter->controller_proc_dir_entry);
  3966. remove_proc_entry("diskdrives-ch2",
  3967. adapter->controller_proc_dir_entry);
  3968. remove_proc_entry("diskdrives-ch3",
  3969. adapter->controller_proc_dir_entry);
  3970. remove_proc_entry("raiddrives-0-9",
  3971. adapter->controller_proc_dir_entry);
  3972. remove_proc_entry("raiddrives-10-19",
  3973. adapter->controller_proc_dir_entry);
  3974. remove_proc_entry("raiddrives-20-29",
  3975. adapter->controller_proc_dir_entry);
  3976. remove_proc_entry("raiddrives-30-39",
  3977. adapter->controller_proc_dir_entry);
  3978. #endif
  3979. sprintf(buf, "hba%d", adapter->host->host_no);
  3980. remove_proc_entry(buf, mega_proc_dir_entry);
  3981. }
  3982. #endif
  3983. pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
  3984. adapter->mega_buffer, adapter->buf_dma_handle);
  3985. kfree(adapter->scb_list);
  3986. pci_free_consistent(adapter->dev, sizeof(mbox64_t),
  3987. adapter->una_mbox64, adapter->una_mbox64_dma);
  3988. scsi_host_put(host);
  3989. pci_disable_device(pdev);
  3990. hba_count--;
  3991. }
  3992. static void
  3993. megaraid_shutdown(struct device *dev)
  3994. {
  3995. struct Scsi_Host *host = pci_get_drvdata(to_pci_dev(dev));
  3996. adapter_t *adapter = (adapter_t *)host->hostdata;
  3997. __megaraid_shutdown(adapter);
  3998. }
  3999. static struct pci_device_id megaraid_pci_tbl[] = {
  4000. {PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DISCOVERY,
  4001. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4002. {PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_PERC4_DI,
  4003. PCI_ANY_ID, PCI_ANY_ID, 0, 0, BOARD_64BIT},
  4004. {PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_PERC4_QC_VERDE,
  4005. PCI_ANY_ID, PCI_ANY_ID, 0, 0, BOARD_64BIT},
  4006. {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
  4007. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4008. {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
  4009. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4010. {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID3,
  4011. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4012. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
  4013. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4014. {PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_AMI_MEGARAID3,
  4015. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  4016. {0,}
  4017. };
  4018. MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
  4019. static struct pci_driver megaraid_pci_driver = {
  4020. .name = "megaraid",
  4021. .id_table = megaraid_pci_tbl,
  4022. .probe = megaraid_probe_one,
  4023. .remove = __devexit_p(megaraid_remove_one),
  4024. .driver = {
  4025. .shutdown = megaraid_shutdown,
  4026. },
  4027. };
  4028. static int __init megaraid_init(void)
  4029. {
  4030. int error;
  4031. if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
  4032. max_cmd_per_lun = MAX_CMD_PER_LUN;
  4033. if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
  4034. max_mbox_busy_wait = MBOX_BUSY_WAIT;
  4035. #ifdef CONFIG_PROC_FS
  4036. mega_proc_dir_entry = proc_mkdir("megaraid", &proc_root);
  4037. if (!mega_proc_dir_entry) {
  4038. printk(KERN_WARNING
  4039. "megaraid: failed to create megaraid root\n");
  4040. }
  4041. #endif
  4042. error = pci_module_init(&megaraid_pci_driver);
  4043. if (error) {
  4044. #ifdef CONFIG_PROC_FS
  4045. remove_proc_entry("megaraid", &proc_root);
  4046. #endif
  4047. return error;
  4048. }
  4049. /*
  4050. * Register the driver as a character device, for applications
  4051. * to access it for ioctls.
  4052. * First argument (major) to register_chrdev implies a dynamic
  4053. * major number allocation.
  4054. */
  4055. major = register_chrdev(0, "megadev", &megadev_fops);
  4056. if (!major) {
  4057. printk(KERN_WARNING
  4058. "megaraid: failed to register char device\n");
  4059. }
  4060. return 0;
  4061. }
  4062. static void __exit megaraid_exit(void)
  4063. {
  4064. /*
  4065. * Unregister the character device interface to the driver.
  4066. */
  4067. unregister_chrdev(major, "megadev");
  4068. pci_unregister_driver(&megaraid_pci_driver);
  4069. #ifdef CONFIG_PROC_FS
  4070. remove_proc_entry("megaraid", &proc_root);
  4071. #endif
  4072. }
  4073. module_init(megaraid_init);
  4074. module_exit(megaraid_exit);
  4075. /* vi: set ts=8 sw=8 tw=78: */