megaraid.c 114 KB

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