megaraid.c 115 KB

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