megaraid.c 108 KB

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