megaraid.c 113 KB

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