megaraid.c 114 KB

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