megaraid.c 116 KB

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