megaraid.c 105 KB

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