megaraid.c 105 KB

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