megaraid.c 115 KB

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