megaraid.c 107 KB

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