megaraid.c 107 KB

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