megaraid.c 114 KB

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