megaraid.c 114 KB

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