megaraid.c 114 KB

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