megaraid.c 113 KB

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