megaraid.c 115 KB

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