megaraid.c 113 KB

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