megaraid.c 107 KB

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