megaraid.c 113 KB

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