megaraid.c 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048
  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_p(adapter->mbox_dma & 0xFF,
  125. adapter->host->io_port + MBOX_PORT0);
  126. outb_p((adapter->mbox_dma >> 8) & 0xFF,
  127. adapter->host->io_port + MBOX_PORT1);
  128. outb_p((adapter->mbox_dma >> 16) & 0xFF,
  129. adapter->host->io_port + MBOX_PORT2);
  130. outb_p((adapter->mbox_dma >> 24) & 0xFF,
  131. adapter->host->io_port + MBOX_PORT3);
  132. outb_p(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, 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( sgl->page ) {
  1245. c = *(unsigned char *)
  1246. page_address((&sgl[0])->page) +
  1247. (&sgl[0])->offset;
  1248. } else {
  1249. printk(KERN_WARNING
  1250. "megaraid: invalid sg.\n");
  1251. c = 0;
  1252. }
  1253. if(IS_RAID_CH(adapter, cmd->device->channel) &&
  1254. ((c & 0x1F ) == TYPE_DISK)) {
  1255. status = 0xF0;
  1256. }
  1257. }
  1258. /* clear result; otherwise, success returns corrupt value */
  1259. cmd->result = 0;
  1260. /* Convert MegaRAID status to Linux error code */
  1261. switch (status) {
  1262. case 0x00: /* SUCCESS , i.e. SCSI_STATUS_GOOD */
  1263. cmd->result |= (DID_OK << 16);
  1264. break;
  1265. case 0x02: /* ERROR_ABORTED, i.e.
  1266. SCSI_STATUS_CHECK_CONDITION */
  1267. /* set sense_buffer and result fields */
  1268. if( mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU ||
  1269. mbox->m_out.cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
  1270. memcpy(cmd->sense_buffer, pthru->reqsensearea,
  1271. 14);
  1272. cmd->result = (DRIVER_SENSE << 24) |
  1273. (DID_OK << 16) |
  1274. (CHECK_CONDITION << 1);
  1275. }
  1276. else {
  1277. if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
  1278. memcpy(cmd->sense_buffer,
  1279. epthru->reqsensearea, 14);
  1280. cmd->result = (DRIVER_SENSE << 24) |
  1281. (DID_OK << 16) |
  1282. (CHECK_CONDITION << 1);
  1283. } else {
  1284. cmd->sense_buffer[0] = 0x70;
  1285. cmd->sense_buffer[2] = ABORTED_COMMAND;
  1286. cmd->result |= (CHECK_CONDITION << 1);
  1287. }
  1288. }
  1289. break;
  1290. case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e.
  1291. SCSI_STATUS_BUSY */
  1292. cmd->result |= (DID_BUS_BUSY << 16) | status;
  1293. break;
  1294. default:
  1295. #if MEGA_HAVE_CLUSTERING
  1296. /*
  1297. * If TEST_UNIT_READY fails, we know
  1298. * MEGA_RESERVATION_STATUS failed
  1299. */
  1300. if( cmd->cmnd[0] == TEST_UNIT_READY ) {
  1301. cmd->result |= (DID_ERROR << 16) |
  1302. (RESERVATION_CONFLICT << 1);
  1303. }
  1304. else
  1305. /*
  1306. * Error code returned is 1 if Reserve or Release
  1307. * failed or the input parameter is invalid
  1308. */
  1309. if( status == 1 &&
  1310. (cmd->cmnd[0] == RESERVE ||
  1311. cmd->cmnd[0] == RELEASE) ) {
  1312. cmd->result |= (DID_ERROR << 16) |
  1313. (RESERVATION_CONFLICT << 1);
  1314. }
  1315. else
  1316. #endif
  1317. cmd->result |= (DID_BAD_TARGET << 16)|status;
  1318. }
  1319. /*
  1320. * Only free SCBs for the commands coming down from the
  1321. * mid-layer, not for which were issued internally
  1322. *
  1323. * For internal command, restore the status returned by the
  1324. * firmware so that user can interpret it.
  1325. */
  1326. if( cmdid == CMDID_INT_CMDS ) { /* internal command */
  1327. cmd->result = status;
  1328. /*
  1329. * Remove the internal command from the pending list
  1330. */
  1331. list_del_init(&scb->list);
  1332. scb->state = SCB_FREE;
  1333. }
  1334. else {
  1335. mega_free_scb(adapter, scb);
  1336. }
  1337. /* Add Scsi_Command to end of completed queue */
  1338. list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
  1339. }
  1340. }
  1341. /*
  1342. * mega_runpendq()
  1343. *
  1344. * Run through the list of completed requests and finish it
  1345. */
  1346. static void
  1347. mega_rundoneq (adapter_t *adapter)
  1348. {
  1349. Scsi_Cmnd *cmd;
  1350. struct list_head *pos;
  1351. list_for_each(pos, &adapter->completed_list) {
  1352. struct scsi_pointer* spos = (struct scsi_pointer *)pos;
  1353. cmd = list_entry(spos, Scsi_Cmnd, SCp);
  1354. cmd->scsi_done(cmd);
  1355. }
  1356. INIT_LIST_HEAD(&adapter->completed_list);
  1357. }
  1358. /*
  1359. * Free a SCB structure
  1360. * Note: We assume the scsi commands associated with this scb is not free yet.
  1361. */
  1362. static void
  1363. mega_free_scb(adapter_t *adapter, scb_t *scb)
  1364. {
  1365. switch( scb->dma_type ) {
  1366. case MEGA_DMA_TYPE_NONE:
  1367. break;
  1368. case MEGA_SGLIST:
  1369. scsi_dma_unmap(scb->cmd);
  1370. break;
  1371. default:
  1372. break;
  1373. }
  1374. /*
  1375. * Remove from the pending list
  1376. */
  1377. list_del_init(&scb->list);
  1378. /* Link the scb back into free list */
  1379. scb->state = SCB_FREE;
  1380. scb->cmd = NULL;
  1381. list_add(&scb->list, &adapter->free_list);
  1382. }
  1383. static int
  1384. __mega_busywait_mbox (adapter_t *adapter)
  1385. {
  1386. volatile mbox_t *mbox = adapter->mbox;
  1387. long counter;
  1388. for (counter = 0; counter < 10000; counter++) {
  1389. if (!mbox->m_in.busy)
  1390. return 0;
  1391. udelay(100);
  1392. cond_resched();
  1393. }
  1394. return -1; /* give up after 1 second */
  1395. }
  1396. /*
  1397. * Copies data to SGLIST
  1398. * Note: For 64 bit cards, we need a minimum of one SG element for read/write
  1399. */
  1400. static int
  1401. mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
  1402. {
  1403. struct scatterlist *sg;
  1404. Scsi_Cmnd *cmd;
  1405. int sgcnt;
  1406. int idx;
  1407. cmd = scb->cmd;
  1408. /*
  1409. * Copy Scatter-Gather list info into controller structure.
  1410. *
  1411. * The number of sg elements returned must not exceed our limit
  1412. */
  1413. sgcnt = scsi_dma_map(cmd);
  1414. scb->dma_type = MEGA_SGLIST;
  1415. BUG_ON(sgcnt > adapter->sglen || sgcnt < 0);
  1416. *len = 0;
  1417. if (scsi_sg_count(cmd) == 1 && !adapter->has_64bit_addr) {
  1418. sg = scsi_sglist(cmd);
  1419. scb->dma_h_bulkdata = sg_dma_address(sg);
  1420. *buf = (u32)scb->dma_h_bulkdata;
  1421. *len = sg_dma_len(sg);
  1422. return 0;
  1423. }
  1424. scsi_for_each_sg(cmd, sg, sgcnt, idx) {
  1425. if (adapter->has_64bit_addr) {
  1426. scb->sgl64[idx].address = sg_dma_address(sg);
  1427. *len += scb->sgl64[idx].length = sg_dma_len(sg);
  1428. } else {
  1429. scb->sgl[idx].address = sg_dma_address(sg);
  1430. *len += scb->sgl[idx].length = sg_dma_len(sg);
  1431. }
  1432. }
  1433. /* Reset pointer and length fields */
  1434. *buf = scb->sgl_dma_addr;
  1435. /* Return count of SG requests */
  1436. return sgcnt;
  1437. }
  1438. /*
  1439. * mega_8_to_40ld()
  1440. *
  1441. * takes all info in AdapterInquiry structure and puts it into ProductInfo and
  1442. * Enquiry3 structures for later use
  1443. */
  1444. static void
  1445. mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
  1446. mega_product_info *product_info)
  1447. {
  1448. int i;
  1449. product_info->max_commands = inquiry->adapter_info.max_commands;
  1450. enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
  1451. product_info->nchannels = inquiry->adapter_info.nchannels;
  1452. for (i = 0; i < 4; i++) {
  1453. product_info->fw_version[i] =
  1454. inquiry->adapter_info.fw_version[i];
  1455. product_info->bios_version[i] =
  1456. inquiry->adapter_info.bios_version[i];
  1457. }
  1458. enquiry3->cache_flush_interval =
  1459. inquiry->adapter_info.cache_flush_interval;
  1460. product_info->dram_size = inquiry->adapter_info.dram_size;
  1461. enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
  1462. for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
  1463. enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
  1464. enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
  1465. enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
  1466. }
  1467. for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
  1468. enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
  1469. }
  1470. static inline void
  1471. mega_free_sgl(adapter_t *adapter)
  1472. {
  1473. scb_t *scb;
  1474. int i;
  1475. for(i = 0; i < adapter->max_cmds; i++) {
  1476. scb = &adapter->scb_list[i];
  1477. if( scb->sgl64 ) {
  1478. pci_free_consistent(adapter->dev,
  1479. sizeof(mega_sgl64) * adapter->sglen,
  1480. scb->sgl64,
  1481. scb->sgl_dma_addr);
  1482. scb->sgl64 = NULL;
  1483. }
  1484. if( scb->pthru ) {
  1485. pci_free_consistent(adapter->dev, sizeof(mega_passthru),
  1486. scb->pthru, scb->pthru_dma_addr);
  1487. scb->pthru = NULL;
  1488. }
  1489. if( scb->epthru ) {
  1490. pci_free_consistent(adapter->dev,
  1491. sizeof(mega_ext_passthru),
  1492. scb->epthru, scb->epthru_dma_addr);
  1493. scb->epthru = NULL;
  1494. }
  1495. }
  1496. }
  1497. /*
  1498. * Get information about the card/driver
  1499. */
  1500. const char *
  1501. megaraid_info(struct Scsi_Host *host)
  1502. {
  1503. static char buffer[512];
  1504. adapter_t *adapter;
  1505. adapter = (adapter_t *)host->hostdata;
  1506. sprintf (buffer,
  1507. "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
  1508. adapter->fw_version, adapter->product_info.max_commands,
  1509. adapter->host->max_id, adapter->host->max_channel,
  1510. adapter->host->max_lun);
  1511. return buffer;
  1512. }
  1513. /*
  1514. * Abort a previous SCSI request. Only commands on the pending list can be
  1515. * aborted. All the commands issued to the F/W must complete.
  1516. */
  1517. static int
  1518. megaraid_abort(Scsi_Cmnd *cmd)
  1519. {
  1520. adapter_t *adapter;
  1521. int rval;
  1522. adapter = (adapter_t *)cmd->device->host->hostdata;
  1523. rval = megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
  1524. /*
  1525. * This is required here to complete any completed requests
  1526. * to be communicated over to the mid layer.
  1527. */
  1528. mega_rundoneq(adapter);
  1529. return rval;
  1530. }
  1531. static int
  1532. megaraid_reset(struct scsi_cmnd *cmd)
  1533. {
  1534. adapter_t *adapter;
  1535. megacmd_t mc;
  1536. int rval;
  1537. adapter = (adapter_t *)cmd->device->host->hostdata;
  1538. #if MEGA_HAVE_CLUSTERING
  1539. mc.cmd = MEGA_CLUSTER_CMD;
  1540. mc.opcode = MEGA_RESET_RESERVATIONS;
  1541. if( mega_internal_command(adapter, &mc, NULL) != 0 ) {
  1542. printk(KERN_WARNING
  1543. "megaraid: reservation reset failed.\n");
  1544. }
  1545. else {
  1546. printk(KERN_INFO "megaraid: reservation reset.\n");
  1547. }
  1548. #endif
  1549. spin_lock_irq(&adapter->lock);
  1550. rval = megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
  1551. /*
  1552. * This is required here to complete any completed requests
  1553. * to be communicated over to the mid layer.
  1554. */
  1555. mega_rundoneq(adapter);
  1556. spin_unlock_irq(&adapter->lock);
  1557. return rval;
  1558. }
  1559. /**
  1560. * megaraid_abort_and_reset()
  1561. * @adapter - megaraid soft state
  1562. * @cmd - scsi command to be aborted or reset
  1563. * @aor - abort or reset flag
  1564. *
  1565. * Try to locate the scsi command in the pending queue. If found and is not
  1566. * issued to the controller, abort/reset it. Otherwise return failure
  1567. */
  1568. static int
  1569. megaraid_abort_and_reset(adapter_t *adapter, Scsi_Cmnd *cmd, int aor)
  1570. {
  1571. struct list_head *pos, *next;
  1572. scb_t *scb;
  1573. printk(KERN_WARNING "megaraid: %s-%lx cmd=%x <c=%d t=%d l=%d>\n",
  1574. (aor == SCB_ABORT)? "ABORTING":"RESET", cmd->serial_number,
  1575. cmd->cmnd[0], cmd->device->channel,
  1576. cmd->device->id, cmd->device->lun);
  1577. if(list_empty(&adapter->pending_list))
  1578. return FALSE;
  1579. list_for_each_safe(pos, next, &adapter->pending_list) {
  1580. scb = list_entry(pos, scb_t, list);
  1581. if (scb->cmd == cmd) { /* Found command */
  1582. scb->state |= aor;
  1583. /*
  1584. * Check if this command has firmare owenership. If
  1585. * yes, we cannot reset this command. Whenever, f/w
  1586. * completes this command, we will return appropriate
  1587. * status from ISR.
  1588. */
  1589. if( scb->state & SCB_ISSUED ) {
  1590. printk(KERN_WARNING
  1591. "megaraid: %s-%lx[%x], fw owner.\n",
  1592. (aor==SCB_ABORT) ? "ABORTING":"RESET",
  1593. cmd->serial_number, scb->idx);
  1594. return FALSE;
  1595. }
  1596. else {
  1597. /*
  1598. * Not yet issued! Remove from the pending
  1599. * list
  1600. */
  1601. printk(KERN_WARNING
  1602. "megaraid: %s-%lx[%x], driver owner.\n",
  1603. (aor==SCB_ABORT) ? "ABORTING":"RESET",
  1604. cmd->serial_number, scb->idx);
  1605. mega_free_scb(adapter, scb);
  1606. if( aor == SCB_ABORT ) {
  1607. cmd->result = (DID_ABORT << 16);
  1608. }
  1609. else {
  1610. cmd->result = (DID_RESET << 16);
  1611. }
  1612. list_add_tail(SCSI_LIST(cmd),
  1613. &adapter->completed_list);
  1614. return TRUE;
  1615. }
  1616. }
  1617. }
  1618. return FALSE;
  1619. }
  1620. static inline int
  1621. make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
  1622. {
  1623. *pdev = alloc_pci_dev();
  1624. if( *pdev == NULL ) return -1;
  1625. memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
  1626. if( pci_set_dma_mask(*pdev, DMA_32BIT_MASK) != 0 ) {
  1627. kfree(*pdev);
  1628. return -1;
  1629. }
  1630. return 0;
  1631. }
  1632. static inline void
  1633. free_local_pdev(struct pci_dev *pdev)
  1634. {
  1635. kfree(pdev);
  1636. }
  1637. /**
  1638. * mega_allocate_inquiry()
  1639. * @dma_handle - handle returned for dma address
  1640. * @pdev - handle to pci device
  1641. *
  1642. * allocates memory for inquiry structure
  1643. */
  1644. static inline void *
  1645. mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
  1646. {
  1647. return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
  1648. }
  1649. static inline void
  1650. mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
  1651. {
  1652. pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
  1653. }
  1654. #ifdef CONFIG_PROC_FS
  1655. /* Following code handles /proc fs */
  1656. #define CREATE_READ_PROC(string, func) create_proc_read_entry(string, \
  1657. S_IRUSR | S_IFREG, \
  1658. controller_proc_dir_entry, \
  1659. func, adapter)
  1660. /**
  1661. * mega_create_proc_entry()
  1662. * @index - index in soft state array
  1663. * @parent - parent node for this /proc entry
  1664. *
  1665. * Creates /proc entries for our controllers.
  1666. */
  1667. static void
  1668. mega_create_proc_entry(int index, struct proc_dir_entry *parent)
  1669. {
  1670. struct proc_dir_entry *controller_proc_dir_entry = NULL;
  1671. u8 string[64] = { 0 };
  1672. adapter_t *adapter = hba_soft_state[index];
  1673. sprintf(string, "hba%d", adapter->host->host_no);
  1674. controller_proc_dir_entry =
  1675. adapter->controller_proc_dir_entry = proc_mkdir(string, parent);
  1676. if(!controller_proc_dir_entry) {
  1677. printk(KERN_WARNING "\nmegaraid: proc_mkdir failed\n");
  1678. return;
  1679. }
  1680. adapter->proc_read = CREATE_READ_PROC("config", proc_read_config);
  1681. adapter->proc_stat = CREATE_READ_PROC("stat", proc_read_stat);
  1682. adapter->proc_mbox = CREATE_READ_PROC("mailbox", proc_read_mbox);
  1683. #if MEGA_HAVE_ENH_PROC
  1684. adapter->proc_rr = CREATE_READ_PROC("rebuild-rate", proc_rebuild_rate);
  1685. adapter->proc_battery = CREATE_READ_PROC("battery-status",
  1686. proc_battery);
  1687. /*
  1688. * Display each physical drive on its channel
  1689. */
  1690. adapter->proc_pdrvstat[0] = CREATE_READ_PROC("diskdrives-ch0",
  1691. proc_pdrv_ch0);
  1692. adapter->proc_pdrvstat[1] = CREATE_READ_PROC("diskdrives-ch1",
  1693. proc_pdrv_ch1);
  1694. adapter->proc_pdrvstat[2] = CREATE_READ_PROC("diskdrives-ch2",
  1695. proc_pdrv_ch2);
  1696. adapter->proc_pdrvstat[3] = CREATE_READ_PROC("diskdrives-ch3",
  1697. proc_pdrv_ch3);
  1698. /*
  1699. * Display a set of up to 10 logical drive through each of following
  1700. * /proc entries
  1701. */
  1702. adapter->proc_rdrvstat[0] = CREATE_READ_PROC("raiddrives-0-9",
  1703. proc_rdrv_10);
  1704. adapter->proc_rdrvstat[1] = CREATE_READ_PROC("raiddrives-10-19",
  1705. proc_rdrv_20);
  1706. adapter->proc_rdrvstat[2] = CREATE_READ_PROC("raiddrives-20-29",
  1707. proc_rdrv_30);
  1708. adapter->proc_rdrvstat[3] = CREATE_READ_PROC("raiddrives-30-39",
  1709. proc_rdrv_40);
  1710. #endif
  1711. }
  1712. /**
  1713. * proc_read_config()
  1714. * @page - buffer to write the data in
  1715. * @start - where the actual data has been written in page
  1716. * @offset - same meaning as the read system call
  1717. * @count - same meaning as the read system call
  1718. * @eof - set if no more data needs to be returned
  1719. * @data - pointer to our soft state
  1720. *
  1721. * Display configuration information about the controller.
  1722. */
  1723. static int
  1724. proc_read_config(char *page, char **start, off_t offset, int count, int *eof,
  1725. void *data)
  1726. {
  1727. adapter_t *adapter = (adapter_t *)data;
  1728. int len = 0;
  1729. len += sprintf(page+len, "%s", MEGARAID_VERSION);
  1730. if(adapter->product_info.product_name[0])
  1731. len += sprintf(page+len, "%s\n",
  1732. adapter->product_info.product_name);
  1733. len += sprintf(page+len, "Controller Type: ");
  1734. if( adapter->flag & BOARD_MEMMAP ) {
  1735. len += sprintf(page+len,
  1736. "438/466/467/471/493/518/520/531/532\n");
  1737. }
  1738. else {
  1739. len += sprintf(page+len,
  1740. "418/428/434\n");
  1741. }
  1742. if(adapter->flag & BOARD_40LD) {
  1743. len += sprintf(page+len,
  1744. "Controller Supports 40 Logical Drives\n");
  1745. }
  1746. if(adapter->flag & BOARD_64BIT) {
  1747. len += sprintf(page+len,
  1748. "Controller capable of 64-bit memory addressing\n");
  1749. }
  1750. if( adapter->has_64bit_addr ) {
  1751. len += sprintf(page+len,
  1752. "Controller using 64-bit memory addressing\n");
  1753. }
  1754. else {
  1755. len += sprintf(page+len,
  1756. "Controller is not using 64-bit memory addressing\n");
  1757. }
  1758. len += sprintf(page+len, "Base = %08lx, Irq = %d, ", adapter->base,
  1759. adapter->host->irq);
  1760. len += sprintf(page+len, "Logical Drives = %d, Channels = %d\n",
  1761. adapter->numldrv, adapter->product_info.nchannels);
  1762. len += sprintf(page+len, "Version =%s:%s, DRAM = %dMb\n",
  1763. adapter->fw_version, adapter->bios_version,
  1764. adapter->product_info.dram_size);
  1765. len += sprintf(page+len,
  1766. "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
  1767. adapter->product_info.max_commands, adapter->max_cmds);
  1768. len += sprintf(page+len, "support_ext_cdb = %d\n",
  1769. adapter->support_ext_cdb);
  1770. len += sprintf(page+len, "support_random_del = %d\n",
  1771. adapter->support_random_del);
  1772. len += sprintf(page+len, "boot_ldrv_enabled = %d\n",
  1773. adapter->boot_ldrv_enabled);
  1774. len += sprintf(page+len, "boot_ldrv = %d\n",
  1775. adapter->boot_ldrv);
  1776. len += sprintf(page+len, "boot_pdrv_enabled = %d\n",
  1777. adapter->boot_pdrv_enabled);
  1778. len += sprintf(page+len, "boot_pdrv_ch = %d\n",
  1779. adapter->boot_pdrv_ch);
  1780. len += sprintf(page+len, "boot_pdrv_tgt = %d\n",
  1781. adapter->boot_pdrv_tgt);
  1782. len += sprintf(page+len, "quiescent = %d\n",
  1783. atomic_read(&adapter->quiescent));
  1784. len += sprintf(page+len, "has_cluster = %d\n",
  1785. adapter->has_cluster);
  1786. len += sprintf(page+len, "\nModule Parameters:\n");
  1787. len += sprintf(page+len, "max_cmd_per_lun = %d\n",
  1788. max_cmd_per_lun);
  1789. len += sprintf(page+len, "max_sectors_per_io = %d\n",
  1790. max_sectors_per_io);
  1791. *eof = 1;
  1792. return len;
  1793. }
  1794. /**
  1795. * proc_read_stat()
  1796. * @page - buffer to write the data in
  1797. * @start - where the actual data has been written in page
  1798. * @offset - same meaning as the read system call
  1799. * @count - same meaning as the read system call
  1800. * @eof - set if no more data needs to be returned
  1801. * @data - pointer to our soft state
  1802. *
  1803. * Diaplay statistical information about the I/O activity.
  1804. */
  1805. static int
  1806. proc_read_stat(char *page, char **start, off_t offset, int count, int *eof,
  1807. void *data)
  1808. {
  1809. adapter_t *adapter;
  1810. int len;
  1811. int i;
  1812. i = 0; /* avoid compilation warnings */
  1813. len = 0;
  1814. adapter = (adapter_t *)data;
  1815. len = sprintf(page, "Statistical Information for this controller\n");
  1816. len += sprintf(page+len, "pend_cmds = %d\n",
  1817. atomic_read(&adapter->pend_cmds));
  1818. #if MEGA_HAVE_STATS
  1819. for(i = 0; i < adapter->numldrv; i++) {
  1820. len += sprintf(page+len, "Logical Drive %d:\n", i);
  1821. len += sprintf(page+len,
  1822. "\tReads Issued = %lu, Writes Issued = %lu\n",
  1823. adapter->nreads[i], adapter->nwrites[i]);
  1824. len += sprintf(page+len,
  1825. "\tSectors Read = %lu, Sectors Written = %lu\n",
  1826. adapter->nreadblocks[i], adapter->nwriteblocks[i]);
  1827. len += sprintf(page+len,
  1828. "\tRead errors = %lu, Write errors = %lu\n\n",
  1829. adapter->rd_errors[i], adapter->wr_errors[i]);
  1830. }
  1831. #else
  1832. len += sprintf(page+len,
  1833. "IO and error counters not compiled in driver.\n");
  1834. #endif
  1835. *eof = 1;
  1836. return len;
  1837. }
  1838. /**
  1839. * proc_read_mbox()
  1840. * @page - buffer to write the data in
  1841. * @start - where the actual data has been written in page
  1842. * @offset - same meaning as the read system call
  1843. * @count - same meaning as the read system call
  1844. * @eof - set if no more data needs to be returned
  1845. * @data - pointer to our soft state
  1846. *
  1847. * Display mailbox information for the last command issued. This information
  1848. * is good for debugging.
  1849. */
  1850. static int
  1851. proc_read_mbox(char *page, char **start, off_t offset, int count, int *eof,
  1852. void *data)
  1853. {
  1854. adapter_t *adapter = (adapter_t *)data;
  1855. volatile mbox_t *mbox = adapter->mbox;
  1856. int len = 0;
  1857. len = sprintf(page, "Contents of Mail Box Structure\n");
  1858. len += sprintf(page+len, " Fw Command = 0x%02x\n",
  1859. mbox->m_out.cmd);
  1860. len += sprintf(page+len, " Cmd Sequence = 0x%02x\n",
  1861. mbox->m_out.cmdid);
  1862. len += sprintf(page+len, " No of Sectors= %04d\n",
  1863. mbox->m_out.numsectors);
  1864. len += sprintf(page+len, " LBA = 0x%02x\n",
  1865. mbox->m_out.lba);
  1866. len += sprintf(page+len, " DTA = 0x%08x\n",
  1867. mbox->m_out.xferaddr);
  1868. len += sprintf(page+len, " Logical Drive= 0x%02x\n",
  1869. mbox->m_out.logdrv);
  1870. len += sprintf(page+len, " No of SG Elmt= 0x%02x\n",
  1871. mbox->m_out.numsgelements);
  1872. len += sprintf(page+len, " Busy = %01x\n",
  1873. mbox->m_in.busy);
  1874. len += sprintf(page+len, " Status = 0x%02x\n",
  1875. mbox->m_in.status);
  1876. *eof = 1;
  1877. return len;
  1878. }
  1879. /**
  1880. * proc_rebuild_rate()
  1881. * @page - buffer to write the data in
  1882. * @start - where the actual data has been written in page
  1883. * @offset - same meaning as the read system call
  1884. * @count - same meaning as the read system call
  1885. * @eof - set if no more data needs to be returned
  1886. * @data - pointer to our soft state
  1887. *
  1888. * Display current rebuild rate
  1889. */
  1890. static int
  1891. proc_rebuild_rate(char *page, char **start, off_t offset, int count, int *eof,
  1892. void *data)
  1893. {
  1894. adapter_t *adapter = (adapter_t *)data;
  1895. dma_addr_t dma_handle;
  1896. caddr_t inquiry;
  1897. struct pci_dev *pdev;
  1898. int len = 0;
  1899. if( make_local_pdev(adapter, &pdev) != 0 ) {
  1900. *eof = 1;
  1901. return len;
  1902. }
  1903. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  1904. free_local_pdev(pdev);
  1905. *eof = 1;
  1906. return len;
  1907. }
  1908. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  1909. len = sprintf(page, "Adapter inquiry failed.\n");
  1910. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  1911. mega_free_inquiry(inquiry, dma_handle, pdev);
  1912. free_local_pdev(pdev);
  1913. *eof = 1;
  1914. return len;
  1915. }
  1916. if( adapter->flag & BOARD_40LD ) {
  1917. len = sprintf(page, "Rebuild Rate: [%d%%]\n",
  1918. ((mega_inquiry3 *)inquiry)->rebuild_rate);
  1919. }
  1920. else {
  1921. len = sprintf(page, "Rebuild Rate: [%d%%]\n",
  1922. ((mraid_ext_inquiry *)
  1923. inquiry)->raid_inq.adapter_info.rebuild_rate);
  1924. }
  1925. mega_free_inquiry(inquiry, dma_handle, pdev);
  1926. free_local_pdev(pdev);
  1927. *eof = 1;
  1928. return len;
  1929. }
  1930. /**
  1931. * proc_battery()
  1932. * @page - buffer to write the data in
  1933. * @start - where the actual data has been written in page
  1934. * @offset - same meaning as the read system call
  1935. * @count - same meaning as the read system call
  1936. * @eof - set if no more data needs to be returned
  1937. * @data - pointer to our soft state
  1938. *
  1939. * Display information about the battery module on the controller.
  1940. */
  1941. static int
  1942. proc_battery(char *page, char **start, off_t offset, int count, int *eof,
  1943. void *data)
  1944. {
  1945. adapter_t *adapter = (adapter_t *)data;
  1946. dma_addr_t dma_handle;
  1947. caddr_t inquiry;
  1948. struct pci_dev *pdev;
  1949. u8 battery_status = 0;
  1950. char str[256];
  1951. int len = 0;
  1952. if( make_local_pdev(adapter, &pdev) != 0 ) {
  1953. *eof = 1;
  1954. return len;
  1955. }
  1956. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  1957. free_local_pdev(pdev);
  1958. *eof = 1;
  1959. return len;
  1960. }
  1961. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  1962. len = sprintf(page, "Adapter inquiry failed.\n");
  1963. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  1964. mega_free_inquiry(inquiry, dma_handle, pdev);
  1965. free_local_pdev(pdev);
  1966. *eof = 1;
  1967. return len;
  1968. }
  1969. if( adapter->flag & BOARD_40LD ) {
  1970. battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
  1971. }
  1972. else {
  1973. battery_status = ((mraid_ext_inquiry *)inquiry)->
  1974. raid_inq.adapter_info.battery_status;
  1975. }
  1976. /*
  1977. * Decode the battery status
  1978. */
  1979. sprintf(str, "Battery Status:[%d]", battery_status);
  1980. if(battery_status == MEGA_BATT_CHARGE_DONE)
  1981. strcat(str, " Charge Done");
  1982. if(battery_status & MEGA_BATT_MODULE_MISSING)
  1983. strcat(str, " Module Missing");
  1984. if(battery_status & MEGA_BATT_LOW_VOLTAGE)
  1985. strcat(str, " Low Voltage");
  1986. if(battery_status & MEGA_BATT_TEMP_HIGH)
  1987. strcat(str, " Temperature High");
  1988. if(battery_status & MEGA_BATT_PACK_MISSING)
  1989. strcat(str, " Pack Missing");
  1990. if(battery_status & MEGA_BATT_CHARGE_INPROG)
  1991. strcat(str, " Charge In-progress");
  1992. if(battery_status & MEGA_BATT_CHARGE_FAIL)
  1993. strcat(str, " Charge Fail");
  1994. if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
  1995. strcat(str, " Cycles Exceeded");
  1996. len = sprintf(page, "%s\n", str);
  1997. mega_free_inquiry(inquiry, dma_handle, pdev);
  1998. free_local_pdev(pdev);
  1999. *eof = 1;
  2000. return len;
  2001. }
  2002. /**
  2003. * proc_pdrv_ch0()
  2004. * @page - buffer to write the data in
  2005. * @start - where the actual data has been written in page
  2006. * @offset - same meaning as the read system call
  2007. * @count - same meaning as the read system call
  2008. * @eof - set if no more data needs to be returned
  2009. * @data - pointer to our soft state
  2010. *
  2011. * Display information about the physical drives on physical channel 0.
  2012. */
  2013. static int
  2014. proc_pdrv_ch0(char *page, char **start, off_t offset, int count, int *eof,
  2015. void *data)
  2016. {
  2017. adapter_t *adapter = (adapter_t *)data;
  2018. *eof = 1;
  2019. return (proc_pdrv(adapter, page, 0));
  2020. }
  2021. /**
  2022. * proc_pdrv_ch1()
  2023. * @page - buffer to write the data in
  2024. * @start - where the actual data has been written in page
  2025. * @offset - same meaning as the read system call
  2026. * @count - same meaning as the read system call
  2027. * @eof - set if no more data needs to be returned
  2028. * @data - pointer to our soft state
  2029. *
  2030. * Display information about the physical drives on physical channel 1.
  2031. */
  2032. static int
  2033. proc_pdrv_ch1(char *page, char **start, off_t offset, int count, int *eof,
  2034. void *data)
  2035. {
  2036. adapter_t *adapter = (adapter_t *)data;
  2037. *eof = 1;
  2038. return (proc_pdrv(adapter, page, 1));
  2039. }
  2040. /**
  2041. * proc_pdrv_ch2()
  2042. * @page - buffer to write the data in
  2043. * @start - where the actual data has been written in page
  2044. * @offset - same meaning as the read system call
  2045. * @count - same meaning as the read system call
  2046. * @eof - set if no more data needs to be returned
  2047. * @data - pointer to our soft state
  2048. *
  2049. * Display information about the physical drives on physical channel 2.
  2050. */
  2051. static int
  2052. proc_pdrv_ch2(char *page, char **start, off_t offset, int count, int *eof,
  2053. void *data)
  2054. {
  2055. adapter_t *adapter = (adapter_t *)data;
  2056. *eof = 1;
  2057. return (proc_pdrv(adapter, page, 2));
  2058. }
  2059. /**
  2060. * proc_pdrv_ch3()
  2061. * @page - buffer to write the data in
  2062. * @start - where the actual data has been written in page
  2063. * @offset - same meaning as the read system call
  2064. * @count - same meaning as the read system call
  2065. * @eof - set if no more data needs to be returned
  2066. * @data - pointer to our soft state
  2067. *
  2068. * Display information about the physical drives on physical channel 3.
  2069. */
  2070. static int
  2071. proc_pdrv_ch3(char *page, char **start, off_t offset, int count, int *eof,
  2072. void *data)
  2073. {
  2074. adapter_t *adapter = (adapter_t *)data;
  2075. *eof = 1;
  2076. return (proc_pdrv(adapter, page, 3));
  2077. }
  2078. /**
  2079. * proc_pdrv()
  2080. * @page - buffer to write the data in
  2081. * @adapter - pointer to our soft state
  2082. *
  2083. * Display information about the physical drives.
  2084. */
  2085. static int
  2086. proc_pdrv(adapter_t *adapter, char *page, int channel)
  2087. {
  2088. dma_addr_t dma_handle;
  2089. char *scsi_inq;
  2090. dma_addr_t scsi_inq_dma_handle;
  2091. caddr_t inquiry;
  2092. struct pci_dev *pdev;
  2093. u8 *pdrv_state;
  2094. u8 state;
  2095. int tgt;
  2096. int max_channels;
  2097. int len = 0;
  2098. char str[80];
  2099. int i;
  2100. if( make_local_pdev(adapter, &pdev) != 0 ) {
  2101. return len;
  2102. }
  2103. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  2104. goto free_pdev;
  2105. }
  2106. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  2107. len = sprintf(page, "Adapter inquiry failed.\n");
  2108. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  2109. goto free_inquiry;
  2110. }
  2111. scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
  2112. if( scsi_inq == NULL ) {
  2113. len = sprintf(page, "memory not available for scsi inq.\n");
  2114. goto free_inquiry;
  2115. }
  2116. if( adapter->flag & BOARD_40LD ) {
  2117. pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
  2118. }
  2119. else {
  2120. pdrv_state = ((mraid_ext_inquiry *)inquiry)->
  2121. raid_inq.pdrv_info.pdrv_state;
  2122. }
  2123. max_channels = adapter->product_info.nchannels;
  2124. if( channel >= max_channels ) {
  2125. goto free_pci;
  2126. }
  2127. for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
  2128. i = channel*16 + tgt;
  2129. state = *(pdrv_state + i);
  2130. switch( state & 0x0F ) {
  2131. case PDRV_ONLINE:
  2132. sprintf(str,
  2133. "Channel:%2d Id:%2d State: Online",
  2134. channel, tgt);
  2135. break;
  2136. case PDRV_FAILED:
  2137. sprintf(str,
  2138. "Channel:%2d Id:%2d State: Failed",
  2139. channel, tgt);
  2140. break;
  2141. case PDRV_RBLD:
  2142. sprintf(str,
  2143. "Channel:%2d Id:%2d State: Rebuild",
  2144. channel, tgt);
  2145. break;
  2146. case PDRV_HOTSPARE:
  2147. sprintf(str,
  2148. "Channel:%2d Id:%2d State: Hot spare",
  2149. channel, tgt);
  2150. break;
  2151. default:
  2152. sprintf(str,
  2153. "Channel:%2d Id:%2d State: Un-configured",
  2154. channel, tgt);
  2155. break;
  2156. }
  2157. /*
  2158. * This interface displays inquiries for disk drives
  2159. * only. Inquries for logical drives and non-disk
  2160. * devices are available through /proc/scsi/scsi
  2161. */
  2162. memset(scsi_inq, 0, 256);
  2163. if( mega_internal_dev_inquiry(adapter, channel, tgt,
  2164. scsi_inq_dma_handle) ||
  2165. (scsi_inq[0] & 0x1F) != TYPE_DISK ) {
  2166. continue;
  2167. }
  2168. /*
  2169. * Check for overflow. We print less than 240
  2170. * characters for inquiry
  2171. */
  2172. if( (len + 240) >= PAGE_SIZE ) break;
  2173. len += sprintf(page+len, "%s.\n", str);
  2174. len += mega_print_inquiry(page+len, scsi_inq);
  2175. }
  2176. free_pci:
  2177. pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
  2178. free_inquiry:
  2179. mega_free_inquiry(inquiry, dma_handle, pdev);
  2180. free_pdev:
  2181. free_local_pdev(pdev);
  2182. return len;
  2183. }
  2184. /*
  2185. * Display scsi inquiry
  2186. */
  2187. static int
  2188. mega_print_inquiry(char *page, char *scsi_inq)
  2189. {
  2190. int len = 0;
  2191. int i;
  2192. len = sprintf(page, " Vendor: ");
  2193. for( i = 8; i < 16; i++ ) {
  2194. len += sprintf(page+len, "%c", scsi_inq[i]);
  2195. }
  2196. len += sprintf(page+len, " Model: ");
  2197. for( i = 16; i < 32; i++ ) {
  2198. len += sprintf(page+len, "%c", scsi_inq[i]);
  2199. }
  2200. len += sprintf(page+len, " Rev: ");
  2201. for( i = 32; i < 36; i++ ) {
  2202. len += sprintf(page+len, "%c", scsi_inq[i]);
  2203. }
  2204. len += sprintf(page+len, "\n");
  2205. i = scsi_inq[0] & 0x1f;
  2206. len += sprintf(page+len, " Type: %s ", scsi_device_type(i));
  2207. len += sprintf(page+len,
  2208. " ANSI SCSI revision: %02x", scsi_inq[2] & 0x07);
  2209. if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
  2210. len += sprintf(page+len, " CCS\n");
  2211. else
  2212. len += sprintf(page+len, "\n");
  2213. return len;
  2214. }
  2215. /**
  2216. * proc_rdrv_10()
  2217. * @page - buffer to write the data in
  2218. * @start - where the actual data has been written in page
  2219. * @offset - same meaning as the read system call
  2220. * @count - same meaning as the read system call
  2221. * @eof - set if no more data needs to be returned
  2222. * @data - pointer to our soft state
  2223. *
  2224. * Display real time information about the logical drives 0 through 9.
  2225. */
  2226. static int
  2227. proc_rdrv_10(char *page, char **start, off_t offset, int count, int *eof,
  2228. void *data)
  2229. {
  2230. adapter_t *adapter = (adapter_t *)data;
  2231. *eof = 1;
  2232. return (proc_rdrv(adapter, page, 0, 9));
  2233. }
  2234. /**
  2235. * proc_rdrv_20()
  2236. * @page - buffer to write the data in
  2237. * @start - where the actual data has been written in page
  2238. * @offset - same meaning as the read system call
  2239. * @count - same meaning as the read system call
  2240. * @eof - set if no more data needs to be returned
  2241. * @data - pointer to our soft state
  2242. *
  2243. * Display real time information about the logical drives 0 through 9.
  2244. */
  2245. static int
  2246. proc_rdrv_20(char *page, char **start, off_t offset, int count, int *eof,
  2247. void *data)
  2248. {
  2249. adapter_t *adapter = (adapter_t *)data;
  2250. *eof = 1;
  2251. return (proc_rdrv(adapter, page, 10, 19));
  2252. }
  2253. /**
  2254. * proc_rdrv_30()
  2255. * @page - buffer to write the data in
  2256. * @start - where the actual data has been written in page
  2257. * @offset - same meaning as the read system call
  2258. * @count - same meaning as the read system call
  2259. * @eof - set if no more data needs to be returned
  2260. * @data - pointer to our soft state
  2261. *
  2262. * Display real time information about the logical drives 0 through 9.
  2263. */
  2264. static int
  2265. proc_rdrv_30(char *page, char **start, off_t offset, int count, int *eof,
  2266. void *data)
  2267. {
  2268. adapter_t *adapter = (adapter_t *)data;
  2269. *eof = 1;
  2270. return (proc_rdrv(adapter, page, 20, 29));
  2271. }
  2272. /**
  2273. * proc_rdrv_40()
  2274. * @page - buffer to write the data in
  2275. * @start - where the actual data has been written in page
  2276. * @offset - same meaning as the read system call
  2277. * @count - same meaning as the read system call
  2278. * @eof - set if no more data needs to be returned
  2279. * @data - pointer to our soft state
  2280. *
  2281. * Display real time information about the logical drives 0 through 9.
  2282. */
  2283. static int
  2284. proc_rdrv_40(char *page, char **start, off_t offset, int count, int *eof,
  2285. void *data)
  2286. {
  2287. adapter_t *adapter = (adapter_t *)data;
  2288. *eof = 1;
  2289. return (proc_rdrv(adapter, page, 30, 39));
  2290. }
  2291. /**
  2292. * proc_rdrv()
  2293. * @page - buffer to write the data in
  2294. * @adapter - pointer to our soft state
  2295. * @start - starting logical drive to display
  2296. * @end - ending logical drive to display
  2297. *
  2298. * We do not print the inquiry information since its already available through
  2299. * /proc/scsi/scsi interface
  2300. */
  2301. static int
  2302. proc_rdrv(adapter_t *adapter, char *page, int start, int end )
  2303. {
  2304. dma_addr_t dma_handle;
  2305. logdrv_param *lparam;
  2306. megacmd_t mc;
  2307. char *disk_array;
  2308. dma_addr_t disk_array_dma_handle;
  2309. caddr_t inquiry;
  2310. struct pci_dev *pdev;
  2311. u8 *rdrv_state;
  2312. int num_ldrv;
  2313. u32 array_sz;
  2314. int len = 0;
  2315. int i;
  2316. if( make_local_pdev(adapter, &pdev) != 0 ) {
  2317. return len;
  2318. }
  2319. if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
  2320. free_local_pdev(pdev);
  2321. return len;
  2322. }
  2323. if( mega_adapinq(adapter, dma_handle) != 0 ) {
  2324. len = sprintf(page, "Adapter inquiry failed.\n");
  2325. printk(KERN_WARNING "megaraid: inquiry failed.\n");
  2326. mega_free_inquiry(inquiry, dma_handle, pdev);
  2327. free_local_pdev(pdev);
  2328. return len;
  2329. }
  2330. memset(&mc, 0, sizeof(megacmd_t));
  2331. if( adapter->flag & BOARD_40LD ) {
  2332. array_sz = sizeof(disk_array_40ld);
  2333. rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
  2334. num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
  2335. }
  2336. else {
  2337. array_sz = sizeof(disk_array_8ld);
  2338. rdrv_state = ((mraid_ext_inquiry *)inquiry)->
  2339. raid_inq.logdrv_info.ldrv_state;
  2340. num_ldrv = ((mraid_ext_inquiry *)inquiry)->
  2341. raid_inq.logdrv_info.num_ldrv;
  2342. }
  2343. disk_array = pci_alloc_consistent(pdev, array_sz,
  2344. &disk_array_dma_handle);
  2345. if( disk_array == NULL ) {
  2346. len = sprintf(page, "memory not available.\n");
  2347. mega_free_inquiry(inquiry, dma_handle, pdev);
  2348. free_local_pdev(pdev);
  2349. return len;
  2350. }
  2351. mc.xferaddr = (u32)disk_array_dma_handle;
  2352. if( adapter->flag & BOARD_40LD ) {
  2353. mc.cmd = FC_NEW_CONFIG;
  2354. mc.opcode = OP_DCMD_READ_CONFIG;
  2355. if( mega_internal_command(adapter, &mc, NULL) ) {
  2356. len = sprintf(page, "40LD read config failed.\n");
  2357. mega_free_inquiry(inquiry, dma_handle, pdev);
  2358. pci_free_consistent(pdev, array_sz, disk_array,
  2359. disk_array_dma_handle);
  2360. free_local_pdev(pdev);
  2361. return len;
  2362. }
  2363. }
  2364. else {
  2365. mc.cmd = NEW_READ_CONFIG_8LD;
  2366. if( mega_internal_command(adapter, &mc, NULL) ) {
  2367. mc.cmd = READ_CONFIG_8LD;
  2368. if( mega_internal_command(adapter, &mc,
  2369. NULL) ){
  2370. len = sprintf(page,
  2371. "8LD read config failed.\n");
  2372. mega_free_inquiry(inquiry, dma_handle, pdev);
  2373. pci_free_consistent(pdev, array_sz,
  2374. disk_array,
  2375. disk_array_dma_handle);
  2376. free_local_pdev(pdev);
  2377. return len;
  2378. }
  2379. }
  2380. }
  2381. for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
  2382. if( adapter->flag & BOARD_40LD ) {
  2383. lparam =
  2384. &((disk_array_40ld *)disk_array)->ldrv[i].lparam;
  2385. }
  2386. else {
  2387. lparam =
  2388. &((disk_array_8ld *)disk_array)->ldrv[i].lparam;
  2389. }
  2390. /*
  2391. * Check for overflow. We print less than 240 characters for
  2392. * information about each logical drive.
  2393. */
  2394. if( (len + 240) >= PAGE_SIZE ) break;
  2395. len += sprintf(page+len, "Logical drive:%2d:, ", i);
  2396. switch( rdrv_state[i] & 0x0F ) {
  2397. case RDRV_OFFLINE:
  2398. len += sprintf(page+len, "state: offline");
  2399. break;
  2400. case RDRV_DEGRADED:
  2401. len += sprintf(page+len, "state: degraded");
  2402. break;
  2403. case RDRV_OPTIMAL:
  2404. len += sprintf(page+len, "state: optimal");
  2405. break;
  2406. case RDRV_DELETED:
  2407. len += sprintf(page+len, "state: deleted");
  2408. break;
  2409. default:
  2410. len += sprintf(page+len, "state: unknown");
  2411. break;
  2412. }
  2413. /*
  2414. * Check if check consistency or initialization is going on
  2415. * for this logical drive.
  2416. */
  2417. if( (rdrv_state[i] & 0xF0) == 0x20 ) {
  2418. len += sprintf(page+len,
  2419. ", check-consistency in progress");
  2420. }
  2421. else if( (rdrv_state[i] & 0xF0) == 0x10 ) {
  2422. len += sprintf(page+len,
  2423. ", initialization in progress");
  2424. }
  2425. len += sprintf(page+len, "\n");
  2426. len += sprintf(page+len, "Span depth:%3d, ",
  2427. lparam->span_depth);
  2428. len += sprintf(page+len, "RAID level:%3d, ",
  2429. lparam->level);
  2430. len += sprintf(page+len, "Stripe size:%3d, ",
  2431. lparam->stripe_sz ? lparam->stripe_sz/2: 128);
  2432. len += sprintf(page+len, "Row size:%3d\n",
  2433. lparam->row_size);
  2434. len += sprintf(page+len, "Read Policy: ");
  2435. switch(lparam->read_ahead) {
  2436. case NO_READ_AHEAD:
  2437. len += sprintf(page+len, "No read ahead, ");
  2438. break;
  2439. case READ_AHEAD:
  2440. len += sprintf(page+len, "Read ahead, ");
  2441. break;
  2442. case ADAP_READ_AHEAD:
  2443. len += sprintf(page+len, "Adaptive, ");
  2444. break;
  2445. }
  2446. len += sprintf(page+len, "Write Policy: ");
  2447. switch(lparam->write_mode) {
  2448. case WRMODE_WRITE_THRU:
  2449. len += sprintf(page+len, "Write thru, ");
  2450. break;
  2451. case WRMODE_WRITE_BACK:
  2452. len += sprintf(page+len, "Write back, ");
  2453. break;
  2454. }
  2455. len += sprintf(page+len, "Cache Policy: ");
  2456. switch(lparam->direct_io) {
  2457. case CACHED_IO:
  2458. len += sprintf(page+len, "Cached IO\n\n");
  2459. break;
  2460. case DIRECT_IO:
  2461. len += sprintf(page+len, "Direct IO\n\n");
  2462. break;
  2463. }
  2464. }
  2465. mega_free_inquiry(inquiry, dma_handle, pdev);
  2466. pci_free_consistent(pdev, array_sz, disk_array,
  2467. disk_array_dma_handle);
  2468. free_local_pdev(pdev);
  2469. return len;
  2470. }
  2471. #else
  2472. static inline void mega_create_proc_entry(int index, struct proc_dir_entry *parent)
  2473. {
  2474. }
  2475. #endif
  2476. /**
  2477. * megaraid_biosparam()
  2478. *
  2479. * Return the disk geometry for a particular disk
  2480. */
  2481. static int
  2482. megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
  2483. sector_t capacity, int geom[])
  2484. {
  2485. adapter_t *adapter;
  2486. unsigned char *bh;
  2487. int heads;
  2488. int sectors;
  2489. int cylinders;
  2490. int rval;
  2491. /* Get pointer to host config structure */
  2492. adapter = (adapter_t *)sdev->host->hostdata;
  2493. if (IS_RAID_CH(adapter, sdev->channel)) {
  2494. /* Default heads (64) & sectors (32) */
  2495. heads = 64;
  2496. sectors = 32;
  2497. cylinders = (ulong)capacity / (heads * sectors);
  2498. /*
  2499. * Handle extended translation size for logical drives
  2500. * > 1Gb
  2501. */
  2502. if ((ulong)capacity >= 0x200000) {
  2503. heads = 255;
  2504. sectors = 63;
  2505. cylinders = (ulong)capacity / (heads * sectors);
  2506. }
  2507. /* return result */
  2508. geom[0] = heads;
  2509. geom[1] = sectors;
  2510. geom[2] = cylinders;
  2511. }
  2512. else {
  2513. bh = scsi_bios_ptable(bdev);
  2514. if( bh ) {
  2515. rval = scsi_partsize(bh, capacity,
  2516. &geom[2], &geom[0], &geom[1]);
  2517. kfree(bh);
  2518. if( rval != -1 )
  2519. return rval;
  2520. }
  2521. printk(KERN_INFO
  2522. "megaraid: invalid partition on this disk on channel %d\n",
  2523. sdev->channel);
  2524. /* Default heads (64) & sectors (32) */
  2525. heads = 64;
  2526. sectors = 32;
  2527. cylinders = (ulong)capacity / (heads * sectors);
  2528. /* Handle extended translation size for logical drives > 1Gb */
  2529. if ((ulong)capacity >= 0x200000) {
  2530. heads = 255;
  2531. sectors = 63;
  2532. cylinders = (ulong)capacity / (heads * sectors);
  2533. }
  2534. /* return result */
  2535. geom[0] = heads;
  2536. geom[1] = sectors;
  2537. geom[2] = cylinders;
  2538. }
  2539. return 0;
  2540. }
  2541. /**
  2542. * mega_init_scb()
  2543. * @adapter - pointer to our soft state
  2544. *
  2545. * Allocate memory for the various pointers in the scb structures:
  2546. * scatter-gather list pointer, passthru and extended passthru structure
  2547. * pointers.
  2548. */
  2549. static int
  2550. mega_init_scb(adapter_t *adapter)
  2551. {
  2552. scb_t *scb;
  2553. int i;
  2554. for( i = 0; i < adapter->max_cmds; i++ ) {
  2555. scb = &adapter->scb_list[i];
  2556. scb->sgl64 = NULL;
  2557. scb->sgl = NULL;
  2558. scb->pthru = NULL;
  2559. scb->epthru = NULL;
  2560. }
  2561. for( i = 0; i < adapter->max_cmds; i++ ) {
  2562. scb = &adapter->scb_list[i];
  2563. scb->idx = i;
  2564. scb->sgl64 = pci_alloc_consistent(adapter->dev,
  2565. sizeof(mega_sgl64) * adapter->sglen,
  2566. &scb->sgl_dma_addr);
  2567. scb->sgl = (mega_sglist *)scb->sgl64;
  2568. if( !scb->sgl ) {
  2569. printk(KERN_WARNING "RAID: Can't allocate sglist.\n");
  2570. mega_free_sgl(adapter);
  2571. return -1;
  2572. }
  2573. scb->pthru = pci_alloc_consistent(adapter->dev,
  2574. sizeof(mega_passthru),
  2575. &scb->pthru_dma_addr);
  2576. if( !scb->pthru ) {
  2577. printk(KERN_WARNING "RAID: Can't allocate passthru.\n");
  2578. mega_free_sgl(adapter);
  2579. return -1;
  2580. }
  2581. scb->epthru = pci_alloc_consistent(adapter->dev,
  2582. sizeof(mega_ext_passthru),
  2583. &scb->epthru_dma_addr);
  2584. if( !scb->epthru ) {
  2585. printk(KERN_WARNING
  2586. "Can't allocate extended passthru.\n");
  2587. mega_free_sgl(adapter);
  2588. return -1;
  2589. }
  2590. scb->dma_type = MEGA_DMA_TYPE_NONE;
  2591. /*
  2592. * Link to free list
  2593. * lock not required since we are loading the driver, so no
  2594. * commands possible right now.
  2595. */
  2596. scb->state = SCB_FREE;
  2597. scb->cmd = NULL;
  2598. list_add(&scb->list, &adapter->free_list);
  2599. }
  2600. return 0;
  2601. }
  2602. /**
  2603. * megadev_open()
  2604. * @inode - unused
  2605. * @filep - unused
  2606. *
  2607. * Routines for the character/ioctl interface to the driver. Find out if this
  2608. * is a valid open. If yes, increment the module use count so that it cannot
  2609. * be unloaded.
  2610. */
  2611. static int
  2612. megadev_open (struct inode *inode, struct file *filep)
  2613. {
  2614. /*
  2615. * Only allow superuser to access private ioctl interface
  2616. */
  2617. if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
  2618. return 0;
  2619. }
  2620. /**
  2621. * megadev_ioctl()
  2622. * @inode - Our device inode
  2623. * @filep - unused
  2624. * @cmd - ioctl command
  2625. * @arg - user buffer
  2626. *
  2627. * ioctl entry point for our private ioctl interface. We move the data in from
  2628. * the user space, prepare the command (if necessary, convert the old MIMD
  2629. * ioctl to new ioctl command), and issue a synchronous command to the
  2630. * controller.
  2631. */
  2632. static int
  2633. megadev_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
  2634. unsigned long arg)
  2635. {
  2636. adapter_t *adapter;
  2637. nitioctl_t uioc;
  2638. int adapno;
  2639. int rval;
  2640. mega_passthru __user *upthru; /* user address for passthru */
  2641. mega_passthru *pthru; /* copy user passthru here */
  2642. dma_addr_t pthru_dma_hndl;
  2643. void *data = NULL; /* data to be transferred */
  2644. dma_addr_t data_dma_hndl; /* dma handle for data xfer area */
  2645. megacmd_t mc;
  2646. megastat_t __user *ustats;
  2647. int num_ldrv;
  2648. u32 uxferaddr = 0;
  2649. struct pci_dev *pdev;
  2650. ustats = NULL; /* avoid compilation warnings */
  2651. num_ldrv = 0;
  2652. /*
  2653. * Make sure only USCSICMD are issued through this interface.
  2654. * MIMD application would still fire different command.
  2655. */
  2656. if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
  2657. return -EINVAL;
  2658. }
  2659. /*
  2660. * Check and convert a possible MIMD command to NIT command.
  2661. * mega_m_to_n() copies the data from the user space, so we do not
  2662. * have to do it here.
  2663. * NOTE: We will need some user address to copyout the data, therefore
  2664. * the inteface layer will also provide us with the required user
  2665. * addresses.
  2666. */
  2667. memset(&uioc, 0, sizeof(nitioctl_t));
  2668. if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
  2669. return rval;
  2670. switch( uioc.opcode ) {
  2671. case GET_DRIVER_VER:
  2672. if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
  2673. return (-EFAULT);
  2674. break;
  2675. case GET_N_ADAP:
  2676. if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
  2677. return (-EFAULT);
  2678. /*
  2679. * Shucks. MIMD interface returns a positive value for number
  2680. * of adapters. TODO: Change it to return 0 when there is no
  2681. * applicatio using mimd interface.
  2682. */
  2683. return hba_count;
  2684. case GET_ADAP_INFO:
  2685. /*
  2686. * Which adapter
  2687. */
  2688. if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
  2689. return (-ENODEV);
  2690. if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
  2691. sizeof(struct mcontroller)) )
  2692. return (-EFAULT);
  2693. break;
  2694. #if MEGA_HAVE_STATS
  2695. case GET_STATS:
  2696. /*
  2697. * Which adapter
  2698. */
  2699. if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
  2700. return (-ENODEV);
  2701. adapter = hba_soft_state[adapno];
  2702. ustats = uioc.uioc_uaddr;
  2703. if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
  2704. return (-EFAULT);
  2705. /*
  2706. * Check for the validity of the logical drive number
  2707. */
  2708. if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
  2709. if( copy_to_user(ustats->nreads, adapter->nreads,
  2710. num_ldrv*sizeof(u32)) )
  2711. return -EFAULT;
  2712. if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
  2713. num_ldrv*sizeof(u32)) )
  2714. return -EFAULT;
  2715. if( copy_to_user(ustats->nwrites, adapter->nwrites,
  2716. num_ldrv*sizeof(u32)) )
  2717. return -EFAULT;
  2718. if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
  2719. num_ldrv*sizeof(u32)) )
  2720. return -EFAULT;
  2721. if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
  2722. num_ldrv*sizeof(u32)) )
  2723. return -EFAULT;
  2724. if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
  2725. num_ldrv*sizeof(u32)) )
  2726. return -EFAULT;
  2727. return 0;
  2728. #endif
  2729. case MBOX_CMD:
  2730. /*
  2731. * Which adapter
  2732. */
  2733. if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
  2734. return (-ENODEV);
  2735. adapter = hba_soft_state[adapno];
  2736. /*
  2737. * Deletion of logical drive is a special case. The adapter
  2738. * should be quiescent before this command is issued.
  2739. */
  2740. if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
  2741. uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
  2742. /*
  2743. * Do we support this feature
  2744. */
  2745. if( !adapter->support_random_del ) {
  2746. printk(KERN_WARNING "megaraid: logdrv ");
  2747. printk("delete on non-supporting F/W.\n");
  2748. return (-EINVAL);
  2749. }
  2750. rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
  2751. if( rval == 0 ) {
  2752. memset(&mc, 0, sizeof(megacmd_t));
  2753. mc.status = rval;
  2754. rval = mega_n_to_m((void __user *)arg, &mc);
  2755. }
  2756. return rval;
  2757. }
  2758. /*
  2759. * This interface only support the regular passthru commands.
  2760. * Reject extended passthru and 64-bit passthru
  2761. */
  2762. if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
  2763. uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
  2764. printk(KERN_WARNING "megaraid: rejected passthru.\n");
  2765. return (-EINVAL);
  2766. }
  2767. /*
  2768. * For all internal commands, the buffer must be allocated in
  2769. * <4GB address range
  2770. */
  2771. if( make_local_pdev(adapter, &pdev) != 0 )
  2772. return -EIO;
  2773. /* Is it a passthru command or a DCMD */
  2774. if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
  2775. /* Passthru commands */
  2776. pthru = pci_alloc_consistent(pdev,
  2777. sizeof(mega_passthru),
  2778. &pthru_dma_hndl);
  2779. if( pthru == NULL ) {
  2780. free_local_pdev(pdev);
  2781. return (-ENOMEM);
  2782. }
  2783. /*
  2784. * The user passthru structure
  2785. */
  2786. upthru = (mega_passthru __user *)(unsigned long)MBOX(uioc)->xferaddr;
  2787. /*
  2788. * Copy in the user passthru here.
  2789. */
  2790. if( copy_from_user(pthru, upthru,
  2791. sizeof(mega_passthru)) ) {
  2792. pci_free_consistent(pdev,
  2793. sizeof(mega_passthru), pthru,
  2794. pthru_dma_hndl);
  2795. free_local_pdev(pdev);
  2796. return (-EFAULT);
  2797. }
  2798. /*
  2799. * Is there a data transfer
  2800. */
  2801. if( pthru->dataxferlen ) {
  2802. data = pci_alloc_consistent(pdev,
  2803. pthru->dataxferlen,
  2804. &data_dma_hndl);
  2805. if( data == NULL ) {
  2806. pci_free_consistent(pdev,
  2807. sizeof(mega_passthru),
  2808. pthru,
  2809. pthru_dma_hndl);
  2810. free_local_pdev(pdev);
  2811. return (-ENOMEM);
  2812. }
  2813. /*
  2814. * Save the user address and point the kernel
  2815. * address at just allocated memory
  2816. */
  2817. uxferaddr = pthru->dataxferaddr;
  2818. pthru->dataxferaddr = data_dma_hndl;
  2819. }
  2820. /*
  2821. * Is data coming down-stream
  2822. */
  2823. if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
  2824. /*
  2825. * Get the user data
  2826. */
  2827. if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
  2828. pthru->dataxferlen) ) {
  2829. rval = (-EFAULT);
  2830. goto freemem_and_return;
  2831. }
  2832. }
  2833. memset(&mc, 0, sizeof(megacmd_t));
  2834. mc.cmd = MEGA_MBOXCMD_PASSTHRU;
  2835. mc.xferaddr = (u32)pthru_dma_hndl;
  2836. /*
  2837. * Issue the command
  2838. */
  2839. mega_internal_command(adapter, &mc, pthru);
  2840. rval = mega_n_to_m((void __user *)arg, &mc);
  2841. if( rval ) goto freemem_and_return;
  2842. /*
  2843. * Is data going up-stream
  2844. */
  2845. if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
  2846. if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
  2847. pthru->dataxferlen) ) {
  2848. rval = (-EFAULT);
  2849. }
  2850. }
  2851. /*
  2852. * Send the request sense data also, irrespective of
  2853. * whether the user has asked for it or not.
  2854. */
  2855. if (copy_to_user(upthru->reqsensearea,
  2856. pthru->reqsensearea, 14))
  2857. rval = -EFAULT;
  2858. freemem_and_return:
  2859. if( pthru->dataxferlen ) {
  2860. pci_free_consistent(pdev,
  2861. pthru->dataxferlen, data,
  2862. data_dma_hndl);
  2863. }
  2864. pci_free_consistent(pdev, sizeof(mega_passthru),
  2865. pthru, pthru_dma_hndl);
  2866. free_local_pdev(pdev);
  2867. return rval;
  2868. }
  2869. else {
  2870. /* DCMD commands */
  2871. /*
  2872. * Is there a data transfer
  2873. */
  2874. if( uioc.xferlen ) {
  2875. data = pci_alloc_consistent(pdev,
  2876. uioc.xferlen, &data_dma_hndl);
  2877. if( data == NULL ) {
  2878. free_local_pdev(pdev);
  2879. return (-ENOMEM);
  2880. }
  2881. uxferaddr = MBOX(uioc)->xferaddr;
  2882. }
  2883. /*
  2884. * Is data coming down-stream
  2885. */
  2886. if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
  2887. /*
  2888. * Get the user data
  2889. */
  2890. if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
  2891. uioc.xferlen) ) {
  2892. pci_free_consistent(pdev,
  2893. uioc.xferlen,
  2894. data, data_dma_hndl);
  2895. free_local_pdev(pdev);
  2896. return (-EFAULT);
  2897. }
  2898. }
  2899. memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
  2900. mc.xferaddr = (u32)data_dma_hndl;
  2901. /*
  2902. * Issue the command
  2903. */
  2904. mega_internal_command(adapter, &mc, NULL);
  2905. rval = mega_n_to_m((void __user *)arg, &mc);
  2906. if( rval ) {
  2907. if( uioc.xferlen ) {
  2908. pci_free_consistent(pdev,
  2909. uioc.xferlen, data,
  2910. data_dma_hndl);
  2911. }
  2912. free_local_pdev(pdev);
  2913. return rval;
  2914. }
  2915. /*
  2916. * Is data going up-stream
  2917. */
  2918. if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
  2919. if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
  2920. uioc.xferlen) ) {
  2921. rval = (-EFAULT);
  2922. }
  2923. }
  2924. if( uioc.xferlen ) {
  2925. pci_free_consistent(pdev,
  2926. uioc.xferlen, data,
  2927. data_dma_hndl);
  2928. }
  2929. free_local_pdev(pdev);
  2930. return rval;
  2931. }
  2932. default:
  2933. return (-EINVAL);
  2934. }
  2935. return 0;
  2936. }
  2937. /**
  2938. * mega_m_to_n()
  2939. * @arg - user address
  2940. * @uioc - new ioctl structure
  2941. *
  2942. * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
  2943. * structure
  2944. *
  2945. * Converts the older mimd ioctl structure to newer NIT structure
  2946. */
  2947. static int
  2948. mega_m_to_n(void __user *arg, nitioctl_t *uioc)
  2949. {
  2950. struct uioctl_t uioc_mimd;
  2951. char signature[8] = {0};
  2952. u8 opcode;
  2953. u8 subopcode;
  2954. /*
  2955. * check is the application conforms to NIT. We do not have to do much
  2956. * in that case.
  2957. * We exploit the fact that the signature is stored in the very
  2958. * begining of the structure.
  2959. */
  2960. if( copy_from_user(signature, arg, 7) )
  2961. return (-EFAULT);
  2962. if( memcmp(signature, "MEGANIT", 7) == 0 ) {
  2963. /*
  2964. * NOTE NOTE: The nit ioctl is still under flux because of
  2965. * change of mailbox definition, in HPE. No applications yet
  2966. * use this interface and let's not have applications use this
  2967. * interface till the new specifitions are in place.
  2968. */
  2969. return -EINVAL;
  2970. #if 0
  2971. if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
  2972. return (-EFAULT);
  2973. return 0;
  2974. #endif
  2975. }
  2976. /*
  2977. * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
  2978. *
  2979. * Get the user ioctl structure
  2980. */
  2981. if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
  2982. return (-EFAULT);
  2983. /*
  2984. * Get the opcode and subopcode for the commands
  2985. */
  2986. opcode = uioc_mimd.ui.fcs.opcode;
  2987. subopcode = uioc_mimd.ui.fcs.subopcode;
  2988. switch (opcode) {
  2989. case 0x82:
  2990. switch (subopcode) {
  2991. case MEGAIOC_QDRVRVER: /* Query driver version */
  2992. uioc->opcode = GET_DRIVER_VER;
  2993. uioc->uioc_uaddr = uioc_mimd.data;
  2994. break;
  2995. case MEGAIOC_QNADAP: /* Get # of adapters */
  2996. uioc->opcode = GET_N_ADAP;
  2997. uioc->uioc_uaddr = uioc_mimd.data;
  2998. break;
  2999. case MEGAIOC_QADAPINFO: /* Get adapter information */
  3000. uioc->opcode = GET_ADAP_INFO;
  3001. uioc->adapno = uioc_mimd.ui.fcs.adapno;
  3002. uioc->uioc_uaddr = uioc_mimd.data;
  3003. break;
  3004. default:
  3005. return(-EINVAL);
  3006. }
  3007. break;
  3008. case 0x81:
  3009. uioc->opcode = MBOX_CMD;
  3010. uioc->adapno = uioc_mimd.ui.fcs.adapno;
  3011. memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
  3012. uioc->xferlen = uioc_mimd.ui.fcs.length;
  3013. if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
  3014. if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
  3015. break;
  3016. case 0x80:
  3017. uioc->opcode = MBOX_CMD;
  3018. uioc->adapno = uioc_mimd.ui.fcs.adapno;
  3019. memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
  3020. /*
  3021. * Choose the xferlen bigger of input and output data
  3022. */
  3023. uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
  3024. uioc_mimd.outlen : uioc_mimd.inlen;
  3025. if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
  3026. if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
  3027. break;
  3028. default:
  3029. return (-EINVAL);
  3030. }
  3031. return 0;
  3032. }
  3033. /*
  3034. * mega_n_to_m()
  3035. * @arg - user address
  3036. * @mc - mailbox command
  3037. *
  3038. * Updates the status information to the application, depending on application
  3039. * conforms to older mimd ioctl interface or newer NIT ioctl interface
  3040. */
  3041. static int
  3042. mega_n_to_m(void __user *arg, megacmd_t *mc)
  3043. {
  3044. nitioctl_t __user *uiocp;
  3045. megacmd_t __user *umc;
  3046. mega_passthru __user *upthru;
  3047. struct uioctl_t __user *uioc_mimd;
  3048. char signature[8] = {0};
  3049. /*
  3050. * check is the application conforms to NIT.
  3051. */
  3052. if( copy_from_user(signature, arg, 7) )
  3053. return -EFAULT;
  3054. if( memcmp(signature, "MEGANIT", 7) == 0 ) {
  3055. uiocp = arg;
  3056. if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
  3057. return (-EFAULT);
  3058. if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
  3059. umc = MBOX_P(uiocp);
  3060. if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
  3061. return -EFAULT;
  3062. if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
  3063. return (-EFAULT);
  3064. }
  3065. }
  3066. else {
  3067. uioc_mimd = arg;
  3068. if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
  3069. return (-EFAULT);
  3070. if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
  3071. umc = (megacmd_t __user *)uioc_mimd->mbox;
  3072. if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
  3073. return (-EFAULT);
  3074. if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
  3075. return (-EFAULT);
  3076. }
  3077. }
  3078. return 0;
  3079. }
  3080. /*
  3081. * MEGARAID 'FW' commands.
  3082. */
  3083. /**
  3084. * mega_is_bios_enabled()
  3085. * @adapter - pointer to our soft state
  3086. *
  3087. * issue command to find out if the BIOS is enabled for this controller
  3088. */
  3089. static int
  3090. mega_is_bios_enabled(adapter_t *adapter)
  3091. {
  3092. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3093. mbox_t *mbox;
  3094. int ret;
  3095. mbox = (mbox_t *)raw_mbox;
  3096. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3097. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3098. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3099. raw_mbox[0] = IS_BIOS_ENABLED;
  3100. raw_mbox[2] = GET_BIOS;
  3101. ret = issue_scb_block(adapter, raw_mbox);
  3102. return *(char *)adapter->mega_buffer;
  3103. }
  3104. /**
  3105. * mega_enum_raid_scsi()
  3106. * @adapter - pointer to our soft state
  3107. *
  3108. * Find out what channels are RAID/SCSI. This information is used to
  3109. * differentiate the virtual channels and physical channels and to support
  3110. * ROMB feature and non-disk devices.
  3111. */
  3112. static void
  3113. mega_enum_raid_scsi(adapter_t *adapter)
  3114. {
  3115. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3116. mbox_t *mbox;
  3117. int i;
  3118. mbox = (mbox_t *)raw_mbox;
  3119. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3120. /*
  3121. * issue command to find out what channels are raid/scsi
  3122. */
  3123. raw_mbox[0] = CHNL_CLASS;
  3124. raw_mbox[2] = GET_CHNL_CLASS;
  3125. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3126. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3127. /*
  3128. * Non-ROMB firmware fail this command, so all channels
  3129. * must be shown RAID
  3130. */
  3131. adapter->mega_ch_class = 0xFF;
  3132. if(!issue_scb_block(adapter, raw_mbox)) {
  3133. adapter->mega_ch_class = *((char *)adapter->mega_buffer);
  3134. }
  3135. for( i = 0; i < adapter->product_info.nchannels; i++ ) {
  3136. if( (adapter->mega_ch_class >> i) & 0x01 ) {
  3137. printk(KERN_INFO "megaraid: channel[%d] is raid.\n",
  3138. i);
  3139. }
  3140. else {
  3141. printk(KERN_INFO "megaraid: channel[%d] is scsi.\n",
  3142. i);
  3143. }
  3144. }
  3145. return;
  3146. }
  3147. /**
  3148. * mega_get_boot_drv()
  3149. * @adapter - pointer to our soft state
  3150. *
  3151. * Find out which device is the boot device. Note, any logical drive or any
  3152. * phyical device (e.g., a CDROM) can be designated as a boot device.
  3153. */
  3154. static void
  3155. mega_get_boot_drv(adapter_t *adapter)
  3156. {
  3157. struct private_bios_data *prv_bios_data;
  3158. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3159. mbox_t *mbox;
  3160. u16 cksum = 0;
  3161. u8 *cksum_p;
  3162. u8 boot_pdrv;
  3163. int i;
  3164. mbox = (mbox_t *)raw_mbox;
  3165. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3166. raw_mbox[0] = BIOS_PVT_DATA;
  3167. raw_mbox[2] = GET_BIOS_PVT_DATA;
  3168. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3169. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3170. adapter->boot_ldrv_enabled = 0;
  3171. adapter->boot_ldrv = 0;
  3172. adapter->boot_pdrv_enabled = 0;
  3173. adapter->boot_pdrv_ch = 0;
  3174. adapter->boot_pdrv_tgt = 0;
  3175. if(issue_scb_block(adapter, raw_mbox) == 0) {
  3176. prv_bios_data =
  3177. (struct private_bios_data *)adapter->mega_buffer;
  3178. cksum = 0;
  3179. cksum_p = (char *)prv_bios_data;
  3180. for (i = 0; i < 14; i++ ) {
  3181. cksum += (u16)(*cksum_p++);
  3182. }
  3183. if (prv_bios_data->cksum == (u16)(0-cksum) ) {
  3184. /*
  3185. * If MSB is set, a physical drive is set as boot
  3186. * device
  3187. */
  3188. if( prv_bios_data->boot_drv & 0x80 ) {
  3189. adapter->boot_pdrv_enabled = 1;
  3190. boot_pdrv = prv_bios_data->boot_drv & 0x7F;
  3191. adapter->boot_pdrv_ch = boot_pdrv / 16;
  3192. adapter->boot_pdrv_tgt = boot_pdrv % 16;
  3193. }
  3194. else {
  3195. adapter->boot_ldrv_enabled = 1;
  3196. adapter->boot_ldrv = prv_bios_data->boot_drv;
  3197. }
  3198. }
  3199. }
  3200. }
  3201. /**
  3202. * mega_support_random_del()
  3203. * @adapter - pointer to our soft state
  3204. *
  3205. * Find out if this controller supports random deletion and addition of
  3206. * logical drives
  3207. */
  3208. static int
  3209. mega_support_random_del(adapter_t *adapter)
  3210. {
  3211. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3212. mbox_t *mbox;
  3213. int rval;
  3214. mbox = (mbox_t *)raw_mbox;
  3215. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3216. /*
  3217. * issue command
  3218. */
  3219. raw_mbox[0] = FC_DEL_LOGDRV;
  3220. raw_mbox[2] = OP_SUP_DEL_LOGDRV;
  3221. rval = issue_scb_block(adapter, raw_mbox);
  3222. return !rval;
  3223. }
  3224. /**
  3225. * mega_support_ext_cdb()
  3226. * @adapter - pointer to our soft state
  3227. *
  3228. * Find out if this firmware support cdblen > 10
  3229. */
  3230. static int
  3231. mega_support_ext_cdb(adapter_t *adapter)
  3232. {
  3233. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3234. mbox_t *mbox;
  3235. int rval;
  3236. mbox = (mbox_t *)raw_mbox;
  3237. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3238. /*
  3239. * issue command to find out if controller supports extended CDBs.
  3240. */
  3241. raw_mbox[0] = 0xA4;
  3242. raw_mbox[2] = 0x16;
  3243. rval = issue_scb_block(adapter, raw_mbox);
  3244. return !rval;
  3245. }
  3246. /**
  3247. * mega_del_logdrv()
  3248. * @adapter - pointer to our soft state
  3249. * @logdrv - logical drive to be deleted
  3250. *
  3251. * Delete the specified logical drive. It is the responsibility of the user
  3252. * app to let the OS know about this operation.
  3253. */
  3254. static int
  3255. mega_del_logdrv(adapter_t *adapter, int logdrv)
  3256. {
  3257. unsigned long flags;
  3258. scb_t *scb;
  3259. int rval;
  3260. /*
  3261. * Stop sending commands to the controller, queue them internally.
  3262. * When deletion is complete, ISR will flush the queue.
  3263. */
  3264. atomic_set(&adapter->quiescent, 1);
  3265. /*
  3266. * Wait till all the issued commands are complete and there are no
  3267. * commands in the pending queue
  3268. */
  3269. while (atomic_read(&adapter->pend_cmds) > 0 ||
  3270. !list_empty(&adapter->pending_list))
  3271. msleep(1000); /* sleep for 1s */
  3272. rval = mega_do_del_logdrv(adapter, logdrv);
  3273. spin_lock_irqsave(&adapter->lock, flags);
  3274. /*
  3275. * If delete operation was successful, add 0x80 to the logical drive
  3276. * ids for commands in the pending queue.
  3277. */
  3278. if (adapter->read_ldidmap) {
  3279. struct list_head *pos;
  3280. list_for_each(pos, &adapter->pending_list) {
  3281. scb = list_entry(pos, scb_t, list);
  3282. if (scb->pthru->logdrv < 0x80 )
  3283. scb->pthru->logdrv += 0x80;
  3284. }
  3285. }
  3286. atomic_set(&adapter->quiescent, 0);
  3287. mega_runpendq(adapter);
  3288. spin_unlock_irqrestore(&adapter->lock, flags);
  3289. return rval;
  3290. }
  3291. static int
  3292. mega_do_del_logdrv(adapter_t *adapter, int logdrv)
  3293. {
  3294. megacmd_t mc;
  3295. int rval;
  3296. memset( &mc, 0, sizeof(megacmd_t));
  3297. mc.cmd = FC_DEL_LOGDRV;
  3298. mc.opcode = OP_DEL_LOGDRV;
  3299. mc.subopcode = logdrv;
  3300. rval = mega_internal_command(adapter, &mc, NULL);
  3301. /* log this event */
  3302. if(rval) {
  3303. printk(KERN_WARNING "megaraid: Delete LD-%d failed.", logdrv);
  3304. return rval;
  3305. }
  3306. /*
  3307. * After deleting first logical drive, the logical drives must be
  3308. * addressed by adding 0x80 to the logical drive id.
  3309. */
  3310. adapter->read_ldidmap = 1;
  3311. return rval;
  3312. }
  3313. /**
  3314. * mega_get_max_sgl()
  3315. * @adapter - pointer to our soft state
  3316. *
  3317. * Find out the maximum number of scatter-gather elements supported by this
  3318. * version of the firmware
  3319. */
  3320. static void
  3321. mega_get_max_sgl(adapter_t *adapter)
  3322. {
  3323. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3324. mbox_t *mbox;
  3325. mbox = (mbox_t *)raw_mbox;
  3326. memset(mbox, 0, sizeof(raw_mbox));
  3327. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3328. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3329. raw_mbox[0] = MAIN_MISC_OPCODE;
  3330. raw_mbox[2] = GET_MAX_SG_SUPPORT;
  3331. if( issue_scb_block(adapter, raw_mbox) ) {
  3332. /*
  3333. * f/w does not support this command. Choose the default value
  3334. */
  3335. adapter->sglen = MIN_SGLIST;
  3336. }
  3337. else {
  3338. adapter->sglen = *((char *)adapter->mega_buffer);
  3339. /*
  3340. * Make sure this is not more than the resources we are
  3341. * planning to allocate
  3342. */
  3343. if ( adapter->sglen > MAX_SGLIST )
  3344. adapter->sglen = MAX_SGLIST;
  3345. }
  3346. return;
  3347. }
  3348. /**
  3349. * mega_support_cluster()
  3350. * @adapter - pointer to our soft state
  3351. *
  3352. * Find out if this firmware support cluster calls.
  3353. */
  3354. static int
  3355. mega_support_cluster(adapter_t *adapter)
  3356. {
  3357. unsigned char raw_mbox[sizeof(struct mbox_out)];
  3358. mbox_t *mbox;
  3359. mbox = (mbox_t *)raw_mbox;
  3360. memset(mbox, 0, sizeof(raw_mbox));
  3361. memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
  3362. mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
  3363. /*
  3364. * Try to get the initiator id. This command will succeed iff the
  3365. * clustering is available on this HBA.
  3366. */
  3367. raw_mbox[0] = MEGA_GET_TARGET_ID;
  3368. if( issue_scb_block(adapter, raw_mbox) == 0 ) {
  3369. /*
  3370. * Cluster support available. Get the initiator target id.
  3371. * Tell our id to mid-layer too.
  3372. */
  3373. adapter->this_id = *(u32 *)adapter->mega_buffer;
  3374. adapter->host->this_id = adapter->this_id;
  3375. return 1;
  3376. }
  3377. return 0;
  3378. }
  3379. #ifdef CONFIG_PROC_FS
  3380. /**
  3381. * mega_adapinq()
  3382. * @adapter - pointer to our soft state
  3383. * @dma_handle - DMA address of the buffer
  3384. *
  3385. * Issue internal comamnds while interrupts are available.
  3386. * We only issue direct mailbox commands from within the driver. ioctl()
  3387. * interface using these routines can issue passthru commands.
  3388. */
  3389. static int
  3390. mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
  3391. {
  3392. megacmd_t mc;
  3393. memset(&mc, 0, sizeof(megacmd_t));
  3394. if( adapter->flag & BOARD_40LD ) {
  3395. mc.cmd = FC_NEW_CONFIG;
  3396. mc.opcode = NC_SUBOP_ENQUIRY3;
  3397. mc.subopcode = ENQ3_GET_SOLICITED_FULL;
  3398. }
  3399. else {
  3400. mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
  3401. }
  3402. mc.xferaddr = (u32)dma_handle;
  3403. if ( mega_internal_command(adapter, &mc, NULL) != 0 ) {
  3404. return -1;
  3405. }
  3406. return 0;
  3407. }
  3408. /** mega_internal_dev_inquiry()
  3409. * @adapter - pointer to our soft state
  3410. * @ch - channel for this device
  3411. * @tgt - ID of this device
  3412. * @buf_dma_handle - DMA address of the buffer
  3413. *
  3414. * Issue the scsi inquiry for the specified device.
  3415. */
  3416. static int
  3417. mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
  3418. dma_addr_t buf_dma_handle)
  3419. {
  3420. mega_passthru *pthru;
  3421. dma_addr_t pthru_dma_handle;
  3422. megacmd_t mc;
  3423. int rval;
  3424. struct pci_dev *pdev;
  3425. /*
  3426. * For all internal commands, the buffer must be allocated in <4GB
  3427. * address range
  3428. */
  3429. if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
  3430. pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
  3431. &pthru_dma_handle);
  3432. if( pthru == NULL ) {
  3433. free_local_pdev(pdev);
  3434. return -1;
  3435. }
  3436. pthru->timeout = 2;
  3437. pthru->ars = 1;
  3438. pthru->reqsenselen = 14;
  3439. pthru->islogical = 0;
  3440. pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
  3441. pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
  3442. pthru->cdblen = 6;
  3443. pthru->cdb[0] = INQUIRY;
  3444. pthru->cdb[1] = 0;
  3445. pthru->cdb[2] = 0;
  3446. pthru->cdb[3] = 0;
  3447. pthru->cdb[4] = 255;
  3448. pthru->cdb[5] = 0;
  3449. pthru->dataxferaddr = (u32)buf_dma_handle;
  3450. pthru->dataxferlen = 256;
  3451. memset(&mc, 0, sizeof(megacmd_t));
  3452. mc.cmd = MEGA_MBOXCMD_PASSTHRU;
  3453. mc.xferaddr = (u32)pthru_dma_handle;
  3454. rval = mega_internal_command(adapter, &mc, pthru);
  3455. pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
  3456. pthru_dma_handle);
  3457. free_local_pdev(pdev);
  3458. return rval;
  3459. }
  3460. #endif
  3461. /**
  3462. * mega_internal_command()
  3463. * @adapter - pointer to our soft state
  3464. * @mc - the mailbox command
  3465. * @pthru - Passthru structure for DCDB commands
  3466. *
  3467. * Issue the internal commands in interrupt mode.
  3468. * The last argument is the address of the passthru structure if the command
  3469. * to be fired is a passthru command
  3470. *
  3471. * lockscope specifies whether the caller has already acquired the lock. Of
  3472. * course, the caller must know which lock we are talking about.
  3473. *
  3474. * Note: parameter 'pthru' is null for non-passthru commands.
  3475. */
  3476. static int
  3477. mega_internal_command(adapter_t *adapter, megacmd_t *mc, mega_passthru *pthru)
  3478. {
  3479. Scsi_Cmnd *scmd;
  3480. struct scsi_device *sdev;
  3481. scb_t *scb;
  3482. int rval;
  3483. /*
  3484. * The internal commands share one command id and hence are
  3485. * serialized. This is so because we want to reserve maximum number of
  3486. * available command ids for the I/O commands.
  3487. */
  3488. mutex_lock(&adapter->int_mtx);
  3489. scb = &adapter->int_scb;
  3490. memset(scb, 0, sizeof(scb_t));
  3491. scmd = &adapter->int_scmd;
  3492. memset(scmd, 0, sizeof(Scsi_Cmnd));
  3493. sdev = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
  3494. scmd->device = sdev;
  3495. scmd->device->host = adapter->host;
  3496. scmd->host_scribble = (void *)scb;
  3497. scmd->cmnd[0] = MEGA_INTERNAL_CMD;
  3498. scb->state |= SCB_ACTIVE;
  3499. scb->cmd = scmd;
  3500. memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
  3501. /*
  3502. * Is it a passthru command
  3503. */
  3504. if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
  3505. scb->pthru = pthru;
  3506. }
  3507. scb->idx = CMDID_INT_CMDS;
  3508. megaraid_queue(scmd, mega_internal_done);
  3509. wait_for_completion(&adapter->int_waitq);
  3510. rval = scmd->result;
  3511. mc->status = scmd->result;
  3512. kfree(sdev);
  3513. /*
  3514. * Print a debug message for all failed commands. Applications can use
  3515. * this information.
  3516. */
  3517. if( scmd->result && trace_level ) {
  3518. printk("megaraid: cmd [%x, %x, %x] status:[%x]\n",
  3519. mc->cmd, mc->opcode, mc->subopcode, scmd->result);
  3520. }
  3521. mutex_unlock(&adapter->int_mtx);
  3522. return rval;
  3523. }
  3524. /**
  3525. * mega_internal_done()
  3526. * @scmd - internal scsi command
  3527. *
  3528. * Callback routine for internal commands.
  3529. */
  3530. static void
  3531. mega_internal_done(Scsi_Cmnd *scmd)
  3532. {
  3533. adapter_t *adapter;
  3534. adapter = (adapter_t *)scmd->device->host->hostdata;
  3535. complete(&adapter->int_waitq);
  3536. }
  3537. static struct scsi_host_template megaraid_template = {
  3538. .module = THIS_MODULE,
  3539. .name = "MegaRAID",
  3540. .proc_name = "megaraid_legacy",
  3541. .info = megaraid_info,
  3542. .queuecommand = megaraid_queue,
  3543. .bios_param = megaraid_biosparam,
  3544. .max_sectors = MAX_SECTORS_PER_IO,
  3545. .can_queue = MAX_COMMANDS,
  3546. .this_id = DEFAULT_INITIATOR_ID,
  3547. .sg_tablesize = MAX_SGLIST,
  3548. .cmd_per_lun = DEF_CMD_PER_LUN,
  3549. .use_clustering = ENABLE_CLUSTERING,
  3550. .use_sg_chaining = ENABLE_SG_CHAINING,
  3551. .eh_abort_handler = megaraid_abort,
  3552. .eh_device_reset_handler = megaraid_reset,
  3553. .eh_bus_reset_handler = megaraid_reset,
  3554. .eh_host_reset_handler = megaraid_reset,
  3555. };
  3556. static int __devinit
  3557. megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  3558. {
  3559. struct Scsi_Host *host;
  3560. adapter_t *adapter;
  3561. unsigned long mega_baseport, tbase, flag = 0;
  3562. u16 subsysid, subsysvid;
  3563. u8 pci_bus, pci_dev_func;
  3564. int irq, i, j;
  3565. int error = -ENODEV;
  3566. if (pci_enable_device(pdev))
  3567. goto out;
  3568. pci_set_master(pdev);
  3569. pci_bus = pdev->bus->number;
  3570. pci_dev_func = pdev->devfn;
  3571. /*
  3572. * The megaraid3 stuff reports the ID of the Intel part which is not
  3573. * remotely specific to the megaraid
  3574. */
  3575. if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
  3576. u16 magic;
  3577. /*
  3578. * Don't fall over the Compaq management cards using the same
  3579. * PCI identifier
  3580. */
  3581. if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
  3582. pdev->subsystem_device == 0xC000)
  3583. return -ENODEV;
  3584. /* Now check the magic signature byte */
  3585. pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
  3586. if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
  3587. return -ENODEV;
  3588. /* Ok it is probably a megaraid */
  3589. }
  3590. /*
  3591. * For these vendor and device ids, signature offsets are not
  3592. * valid and 64 bit is implicit
  3593. */
  3594. if (id->driver_data & BOARD_64BIT)
  3595. flag |= BOARD_64BIT;
  3596. else {
  3597. u32 magic64;
  3598. pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
  3599. if (magic64 == HBA_SIGNATURE_64BIT)
  3600. flag |= BOARD_64BIT;
  3601. }
  3602. subsysvid = pdev->subsystem_vendor;
  3603. subsysid = pdev->subsystem_device;
  3604. printk(KERN_NOTICE "megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
  3605. id->vendor, id->device, pci_bus);
  3606. printk("slot %d:func %d\n",
  3607. PCI_SLOT(pci_dev_func), PCI_FUNC(pci_dev_func));
  3608. /* Read the base port and IRQ from PCI */
  3609. mega_baseport = pci_resource_start(pdev, 0);
  3610. irq = pdev->irq;
  3611. tbase = mega_baseport;
  3612. if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
  3613. flag |= BOARD_MEMMAP;
  3614. if (!request_mem_region(mega_baseport, 128, "megaraid")) {
  3615. printk(KERN_WARNING "megaraid: mem region busy!\n");
  3616. goto out_disable_device;
  3617. }
  3618. mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
  3619. if (!mega_baseport) {
  3620. printk(KERN_WARNING
  3621. "megaraid: could not map hba memory\n");
  3622. goto out_release_region;
  3623. }
  3624. } else {
  3625. flag |= BOARD_IOMAP;
  3626. mega_baseport += 0x10;
  3627. if (!request_region(mega_baseport, 16, "megaraid"))
  3628. goto out_disable_device;
  3629. }
  3630. /* Initialize SCSI Host structure */
  3631. host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
  3632. if (!host)
  3633. goto out_iounmap;
  3634. adapter = (adapter_t *)host->hostdata;
  3635. memset(adapter, 0, sizeof(adapter_t));
  3636. printk(KERN_NOTICE
  3637. "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
  3638. host->host_no, mega_baseport, irq);
  3639. adapter->base = mega_baseport;
  3640. if (flag & BOARD_MEMMAP)
  3641. adapter->mmio_base = (void __iomem *) mega_baseport;
  3642. INIT_LIST_HEAD(&adapter->free_list);
  3643. INIT_LIST_HEAD(&adapter->pending_list);
  3644. INIT_LIST_HEAD(&adapter->completed_list);
  3645. adapter->flag = flag;
  3646. spin_lock_init(&adapter->lock);
  3647. host->cmd_per_lun = max_cmd_per_lun;
  3648. host->max_sectors = max_sectors_per_io;
  3649. adapter->dev = pdev;
  3650. adapter->host = host;
  3651. adapter->host->irq = irq;
  3652. if (flag & BOARD_MEMMAP)
  3653. adapter->host->base = tbase;
  3654. else {
  3655. adapter->host->io_port = tbase;
  3656. adapter->host->n_io_port = 16;
  3657. }
  3658. adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
  3659. /*
  3660. * Allocate buffer to issue internal commands.
  3661. */
  3662. adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
  3663. MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
  3664. if (!adapter->mega_buffer) {
  3665. printk(KERN_WARNING "megaraid: out of RAM.\n");
  3666. goto out_host_put;
  3667. }
  3668. adapter->scb_list = kmalloc(sizeof(scb_t) * MAX_COMMANDS, GFP_KERNEL);
  3669. if (!adapter->scb_list) {
  3670. printk(KERN_WARNING "megaraid: out of RAM.\n");
  3671. goto out_free_cmd_buffer;
  3672. }
  3673. if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
  3674. megaraid_isr_memmapped : megaraid_isr_iomapped,
  3675. IRQF_SHARED, "megaraid", adapter)) {
  3676. printk(KERN_WARNING
  3677. "megaraid: Couldn't register IRQ %d!\n", irq);
  3678. goto out_free_scb_list;
  3679. }
  3680. if (mega_setup_mailbox(adapter))
  3681. goto out_free_irq;
  3682. if (mega_query_adapter(adapter))
  3683. goto out_free_mbox;
  3684. /*
  3685. * Have checks for some buggy f/w
  3686. */
  3687. if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
  3688. /*
  3689. * Which firmware
  3690. */
  3691. if (!strcmp(adapter->fw_version, "3.00") ||
  3692. !strcmp(adapter->fw_version, "3.01")) {
  3693. printk( KERN_WARNING
  3694. "megaraid: Your card is a Dell PERC "
  3695. "2/SC RAID controller with "
  3696. "firmware\nmegaraid: 3.00 or 3.01. "
  3697. "This driver is known to have "
  3698. "corruption issues\nmegaraid: with "
  3699. "those firmware versions on this "
  3700. "specific card. In order\nmegaraid: "
  3701. "to protect your data, please upgrade "
  3702. "your firmware to version\nmegaraid: "
  3703. "3.10 or later, available from the "
  3704. "Dell Technical Support web\n"
  3705. "megaraid: site at\nhttp://support."
  3706. "dell.com/us/en/filelib/download/"
  3707. "index.asp?fileid=2940\n"
  3708. );
  3709. }
  3710. }
  3711. /*
  3712. * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
  3713. * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
  3714. * support, since this firmware cannot handle 64 bit
  3715. * addressing
  3716. */
  3717. if ((subsysvid == HP_SUBSYS_VID) &&
  3718. ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
  3719. /*
  3720. * which firmware
  3721. */
  3722. if (!strcmp(adapter->fw_version, "H01.07") ||
  3723. !strcmp(adapter->fw_version, "H01.08") ||
  3724. !strcmp(adapter->fw_version, "H01.09") ) {
  3725. printk(KERN_WARNING
  3726. "megaraid: Firmware H.01.07, "
  3727. "H.01.08, and H.01.09 on 1M/2M "
  3728. "controllers\n"
  3729. "megaraid: do not support 64 bit "
  3730. "addressing.\nmegaraid: DISABLING "
  3731. "64 bit support.\n");
  3732. adapter->flag &= ~BOARD_64BIT;
  3733. }
  3734. }
  3735. if (mega_is_bios_enabled(adapter))
  3736. mega_hbas[hba_count].is_bios_enabled = 1;
  3737. mega_hbas[hba_count].hostdata_addr = adapter;
  3738. /*
  3739. * Find out which channel is raid and which is scsi. This is
  3740. * for ROMB support.
  3741. */
  3742. mega_enum_raid_scsi(adapter);
  3743. /*
  3744. * Find out if a logical drive is set as the boot drive. If
  3745. * there is one, will make that as the first logical drive.
  3746. * ROMB: Do we have to boot from a physical drive. Then all
  3747. * the physical drives would appear before the logical disks.
  3748. * Else, all the physical drives would be exported to the mid
  3749. * layer after logical drives.
  3750. */
  3751. mega_get_boot_drv(adapter);
  3752. if (adapter->boot_pdrv_enabled) {
  3753. j = adapter->product_info.nchannels;
  3754. for( i = 0; i < j; i++ )
  3755. adapter->logdrv_chan[i] = 0;
  3756. for( i = j; i < NVIRT_CHAN + j; i++ )
  3757. adapter->logdrv_chan[i] = 1;
  3758. } else {
  3759. for (i = 0; i < NVIRT_CHAN; i++)
  3760. adapter->logdrv_chan[i] = 1;
  3761. for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
  3762. adapter->logdrv_chan[i] = 0;
  3763. adapter->mega_ch_class <<= NVIRT_CHAN;
  3764. }
  3765. /*
  3766. * Do we support random deletion and addition of logical
  3767. * drives
  3768. */
  3769. adapter->read_ldidmap = 0; /* set it after first logdrv
  3770. delete cmd */
  3771. adapter->support_random_del = mega_support_random_del(adapter);
  3772. /* Initialize SCBs */
  3773. if (mega_init_scb(adapter))
  3774. goto out_free_mbox;
  3775. /*
  3776. * Reset the pending commands counter
  3777. */
  3778. atomic_set(&adapter->pend_cmds, 0);
  3779. /*
  3780. * Reset the adapter quiescent flag
  3781. */
  3782. atomic_set(&adapter->quiescent, 0);
  3783. hba_soft_state[hba_count] = adapter;
  3784. /*
  3785. * Fill in the structure which needs to be passed back to the
  3786. * application when it does an ioctl() for controller related
  3787. * information.
  3788. */
  3789. i = hba_count;
  3790. mcontroller[i].base = mega_baseport;
  3791. mcontroller[i].irq = irq;
  3792. mcontroller[i].numldrv = adapter->numldrv;
  3793. mcontroller[i].pcibus = pci_bus;
  3794. mcontroller[i].pcidev = id->device;
  3795. mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
  3796. mcontroller[i].pciid = -1;
  3797. mcontroller[i].pcivendor = id->vendor;
  3798. mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
  3799. mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
  3800. /* Set the Mode of addressing to 64 bit if we can */
  3801. if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
  3802. pci_set_dma_mask(pdev, DMA_64BIT_MASK);
  3803. adapter->has_64bit_addr = 1;
  3804. } else {
  3805. pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  3806. adapter->has_64bit_addr = 0;
  3807. }
  3808. mutex_init(&adapter->int_mtx);
  3809. init_completion(&adapter->int_waitq);
  3810. adapter->this_id = DEFAULT_INITIATOR_ID;
  3811. adapter->host->this_id = DEFAULT_INITIATOR_ID;
  3812. #if MEGA_HAVE_CLUSTERING
  3813. /*
  3814. * Is cluster support enabled on this controller
  3815. * Note: In a cluster the HBAs ( the initiators ) will have
  3816. * different target IDs and we cannot assume it to be 7. Call
  3817. * to mega_support_cluster() will get the target ids also if
  3818. * the cluster support is available
  3819. */
  3820. adapter->has_cluster = mega_support_cluster(adapter);
  3821. if (adapter->has_cluster) {
  3822. printk(KERN_NOTICE
  3823. "megaraid: Cluster driver, initiator id:%d\n",
  3824. adapter->this_id);
  3825. }
  3826. #endif
  3827. pci_set_drvdata(pdev, host);
  3828. mega_create_proc_entry(hba_count, mega_proc_dir_entry);
  3829. error = scsi_add_host(host, &pdev->dev);
  3830. if (error)
  3831. goto out_free_mbox;
  3832. scsi_scan_host(host);
  3833. hba_count++;
  3834. return 0;
  3835. out_free_mbox:
  3836. pci_free_consistent(adapter->dev, sizeof(mbox64_t),
  3837. adapter->una_mbox64, adapter->una_mbox64_dma);
  3838. out_free_irq:
  3839. free_irq(adapter->host->irq, adapter);
  3840. out_free_scb_list:
  3841. kfree(adapter->scb_list);
  3842. out_free_cmd_buffer:
  3843. pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
  3844. adapter->mega_buffer, adapter->buf_dma_handle);
  3845. out_host_put:
  3846. scsi_host_put(host);
  3847. out_iounmap:
  3848. if (flag & BOARD_MEMMAP)
  3849. iounmap((void *)mega_baseport);
  3850. out_release_region:
  3851. if (flag & BOARD_MEMMAP)
  3852. release_mem_region(tbase, 128);
  3853. else
  3854. release_region(mega_baseport, 16);
  3855. out_disable_device:
  3856. pci_disable_device(pdev);
  3857. out:
  3858. return error;
  3859. }
  3860. static void
  3861. __megaraid_shutdown(adapter_t *adapter)
  3862. {
  3863. u_char raw_mbox[sizeof(struct mbox_out)];
  3864. mbox_t *mbox = (mbox_t *)raw_mbox;
  3865. int i;
  3866. /* Flush adapter cache */
  3867. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3868. raw_mbox[0] = FLUSH_ADAPTER;
  3869. free_irq(adapter->host->irq, adapter);
  3870. /* Issue a blocking (interrupts disabled) command to the card */
  3871. issue_scb_block(adapter, raw_mbox);
  3872. /* Flush disks cache */
  3873. memset(&mbox->m_out, 0, sizeof(raw_mbox));
  3874. raw_mbox[0] = FLUSH_SYSTEM;
  3875. /* Issue a blocking (interrupts disabled) command to the card */
  3876. issue_scb_block(adapter, raw_mbox);
  3877. if (atomic_read(&adapter->pend_cmds) > 0)
  3878. printk(KERN_WARNING "megaraid: pending commands!!\n");
  3879. /*
  3880. * Have a delibrate delay to make sure all the caches are
  3881. * actually flushed.
  3882. */
  3883. for (i = 0; i <= 10; i++)
  3884. mdelay(1000);
  3885. }
  3886. static void
  3887. megaraid_remove_one(struct pci_dev *pdev)
  3888. {
  3889. struct Scsi_Host *host = pci_get_drvdata(pdev);
  3890. adapter_t *adapter = (adapter_t *)host->hostdata;
  3891. scsi_remove_host(host);
  3892. __megaraid_shutdown(adapter);
  3893. /* Free our resources */
  3894. if (adapter->flag & BOARD_MEMMAP) {
  3895. iounmap((void *)adapter->base);
  3896. release_mem_region(adapter->host->base, 128);
  3897. } else
  3898. release_region(adapter->base, 16);
  3899. mega_free_sgl(adapter);
  3900. #ifdef CONFIG_PROC_FS
  3901. if (adapter->controller_proc_dir_entry) {
  3902. remove_proc_entry("stat", adapter->controller_proc_dir_entry);
  3903. remove_proc_entry("config",
  3904. adapter->controller_proc_dir_entry);
  3905. remove_proc_entry("mailbox",
  3906. adapter->controller_proc_dir_entry);
  3907. #if MEGA_HAVE_ENH_PROC
  3908. remove_proc_entry("rebuild-rate",
  3909. adapter->controller_proc_dir_entry);
  3910. remove_proc_entry("battery-status",
  3911. adapter->controller_proc_dir_entry);
  3912. remove_proc_entry("diskdrives-ch0",
  3913. adapter->controller_proc_dir_entry);
  3914. remove_proc_entry("diskdrives-ch1",
  3915. adapter->controller_proc_dir_entry);
  3916. remove_proc_entry("diskdrives-ch2",
  3917. adapter->controller_proc_dir_entry);
  3918. remove_proc_entry("diskdrives-ch3",
  3919. adapter->controller_proc_dir_entry);
  3920. remove_proc_entry("raiddrives-0-9",
  3921. adapter->controller_proc_dir_entry);
  3922. remove_proc_entry("raiddrives-10-19",
  3923. adapter->controller_proc_dir_entry);
  3924. remove_proc_entry("raiddrives-20-29",
  3925. adapter->controller_proc_dir_entry);
  3926. remove_proc_entry("raiddrives-30-39",
  3927. adapter->controller_proc_dir_entry);
  3928. #endif
  3929. {
  3930. char buf[12] = { 0 };
  3931. sprintf(buf, "hba%d", adapter->host->host_no);
  3932. remove_proc_entry(buf, mega_proc_dir_entry);
  3933. }
  3934. }
  3935. #endif
  3936. pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
  3937. adapter->mega_buffer, adapter->buf_dma_handle);
  3938. kfree(adapter->scb_list);
  3939. pci_free_consistent(adapter->dev, sizeof(mbox64_t),
  3940. adapter->una_mbox64, adapter->una_mbox64_dma);
  3941. scsi_host_put(host);
  3942. pci_disable_device(pdev);
  3943. hba_count--;
  3944. }
  3945. static void
  3946. megaraid_shutdown(struct pci_dev *pdev)
  3947. {
  3948. struct Scsi_Host *host = pci_get_drvdata(pdev);
  3949. adapter_t *adapter = (adapter_t *)host->hostdata;
  3950. __megaraid_shutdown(adapter);
  3951. }
  3952. static struct pci_device_id megaraid_pci_tbl[] = {
  3953. {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
  3954. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  3955. {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
  3956. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  3957. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
  3958. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  3959. {0,}
  3960. };
  3961. MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
  3962. static struct pci_driver megaraid_pci_driver = {
  3963. .name = "megaraid_legacy",
  3964. .id_table = megaraid_pci_tbl,
  3965. .probe = megaraid_probe_one,
  3966. .remove = __devexit_p(megaraid_remove_one),
  3967. .shutdown = megaraid_shutdown,
  3968. };
  3969. static int __init megaraid_init(void)
  3970. {
  3971. int error;
  3972. if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
  3973. max_cmd_per_lun = MAX_CMD_PER_LUN;
  3974. if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
  3975. max_mbox_busy_wait = MBOX_BUSY_WAIT;
  3976. #ifdef CONFIG_PROC_FS
  3977. mega_proc_dir_entry = proc_mkdir("megaraid", &proc_root);
  3978. if (!mega_proc_dir_entry) {
  3979. printk(KERN_WARNING
  3980. "megaraid: failed to create megaraid root\n");
  3981. }
  3982. #endif
  3983. error = pci_register_driver(&megaraid_pci_driver);
  3984. if (error) {
  3985. #ifdef CONFIG_PROC_FS
  3986. remove_proc_entry("megaraid", &proc_root);
  3987. #endif
  3988. return error;
  3989. }
  3990. /*
  3991. * Register the driver as a character device, for applications
  3992. * to access it for ioctls.
  3993. * First argument (major) to register_chrdev implies a dynamic
  3994. * major number allocation.
  3995. */
  3996. major = register_chrdev(0, "megadev_legacy", &megadev_fops);
  3997. if (!major) {
  3998. printk(KERN_WARNING
  3999. "megaraid: failed to register char device\n");
  4000. }
  4001. return 0;
  4002. }
  4003. static void __exit megaraid_exit(void)
  4004. {
  4005. /*
  4006. * Unregister the character device interface to the driver.
  4007. */
  4008. unregister_chrdev(major, "megadev_legacy");
  4009. pci_unregister_driver(&megaraid_pci_driver);
  4010. #ifdef CONFIG_PROC_FS
  4011. remove_proc_entry("megaraid", &proc_root);
  4012. #endif
  4013. }
  4014. module_init(megaraid_init);
  4015. module_exit(megaraid_exit);
  4016. /* vi: set ts=8 sw=8 tw=78: */