megaraid_mbox.c 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208
  1. /*
  2. *
  3. * Linux MegaRAID device driver
  4. *
  5. * Copyright (c) 2003-2004 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. * FILE : megaraid_mbox.c
  13. * Version : v2.20.4.6 (Mar 07 2005)
  14. *
  15. * Authors:
  16. * Atul Mukker <Atul.Mukker@lsil.com>
  17. * Sreenivas Bagalkote <Sreenivas.Bagalkote@lsil.com>
  18. * Manoj Jose <Manoj.Jose@lsil.com>
  19. *
  20. * List of supported controllers
  21. *
  22. * OEM Product Name VID DID SSVID SSID
  23. * --- ------------ --- --- ---- ----
  24. * Dell PERC3/QC 101E 1960 1028 0471
  25. * Dell PERC3/DC 101E 1960 1028 0493
  26. * Dell PERC3/SC 101E 1960 1028 0475
  27. * Dell PERC3/Di 1028 1960 1028 0123
  28. * Dell PERC4/SC 1000 1960 1028 0520
  29. * Dell PERC4/DC 1000 1960 1028 0518
  30. * Dell PERC4/QC 1000 0407 1028 0531
  31. * Dell PERC4/Di 1028 000F 1028 014A
  32. * Dell PERC 4e/Si 1028 0013 1028 016c
  33. * Dell PERC 4e/Di 1028 0013 1028 016d
  34. * Dell PERC 4e/Di 1028 0013 1028 016e
  35. * Dell PERC 4e/Di 1028 0013 1028 016f
  36. * Dell PERC 4e/Di 1028 0013 1028 0170
  37. * Dell PERC 4e/DC 1000 0408 1028 0002
  38. * Dell PERC 4e/SC 1000 0408 1028 0001
  39. *
  40. *
  41. * LSI MegaRAID SCSI 320-0 1000 1960 1000 A520
  42. * LSI MegaRAID SCSI 320-1 1000 1960 1000 0520
  43. * LSI MegaRAID SCSI 320-2 1000 1960 1000 0518
  44. * LSI MegaRAID SCSI 320-0X 1000 0407 1000 0530
  45. * LSI MegaRAID SCSI 320-2X 1000 0407 1000 0532
  46. * LSI MegaRAID SCSI 320-4X 1000 0407 1000 0531
  47. * LSI MegaRAID SCSI 320-1E 1000 0408 1000 0001
  48. * LSI MegaRAID SCSI 320-2E 1000 0408 1000 0002
  49. * LSI MegaRAID SATA 150-4 1000 1960 1000 4523
  50. * LSI MegaRAID SATA 150-6 1000 1960 1000 0523
  51. * LSI MegaRAID SATA 300-4X 1000 0409 1000 3004
  52. * LSI MegaRAID SATA 300-8X 1000 0409 1000 3008
  53. *
  54. * INTEL RAID Controller SRCU42X 1000 0407 8086 0532
  55. * INTEL RAID Controller SRCS16 1000 1960 8086 0523
  56. * INTEL RAID Controller SRCU42E 1000 0408 8086 0002
  57. * INTEL RAID Controller SRCZCRX 1000 0407 8086 0530
  58. * INTEL RAID Controller SRCS28X 1000 0409 8086 3008
  59. * INTEL RAID Controller SROMBU42E 1000 0408 8086 3431
  60. * INTEL RAID Controller SROMBU42E 1000 0408 8086 3499
  61. * INTEL RAID Controller SRCU51L 1000 1960 8086 0520
  62. *
  63. * FSC MegaRAID PCI Express ROMB 1000 0408 1734 1065
  64. *
  65. * ACER MegaRAID ROMB-2E 1000 0408 1025 004D
  66. *
  67. * NEC MegaRAID PCI Express ROMB 1000 0408 1033 8287
  68. *
  69. * For history of changes, see Documentation/ChangeLog.megaraid
  70. */
  71. #include "megaraid_mbox.h"
  72. static int megaraid_init(void);
  73. static void megaraid_exit(void);
  74. static int megaraid_probe_one(struct pci_dev*, const struct pci_device_id *);
  75. static void megaraid_detach_one(struct pci_dev *);
  76. static void megaraid_mbox_shutdown(struct device *);
  77. static int megaraid_io_attach(adapter_t *);
  78. static void megaraid_io_detach(adapter_t *);
  79. static int megaraid_init_mbox(adapter_t *);
  80. static void megaraid_fini_mbox(adapter_t *);
  81. static int megaraid_alloc_cmd_packets(adapter_t *);
  82. static void megaraid_free_cmd_packets(adapter_t *);
  83. static int megaraid_mbox_setup_dma_pools(adapter_t *);
  84. static void megaraid_mbox_teardown_dma_pools(adapter_t *);
  85. static int megaraid_sysfs_alloc_resources(adapter_t *);
  86. static void megaraid_sysfs_free_resources(adapter_t *);
  87. static int megaraid_abort_handler(struct scsi_cmnd *);
  88. static int megaraid_reset_handler(struct scsi_cmnd *);
  89. static int mbox_post_sync_cmd(adapter_t *, uint8_t []);
  90. static int mbox_post_sync_cmd_fast(adapter_t *, uint8_t []);
  91. static int megaraid_busywait_mbox(mraid_device_t *);
  92. static int megaraid_mbox_product_info(adapter_t *);
  93. static int megaraid_mbox_extended_cdb(adapter_t *);
  94. static int megaraid_mbox_support_ha(adapter_t *, uint16_t *);
  95. static int megaraid_mbox_support_random_del(adapter_t *);
  96. static int megaraid_mbox_get_max_sg(adapter_t *);
  97. static void megaraid_mbox_enum_raid_scsi(adapter_t *);
  98. static void megaraid_mbox_flush_cache(adapter_t *);
  99. static void megaraid_mbox_display_scb(adapter_t *, scb_t *);
  100. static void megaraid_mbox_setup_device_map(adapter_t *);
  101. static int megaraid_queue_command(struct scsi_cmnd *,
  102. void (*)(struct scsi_cmnd *));
  103. static scb_t *megaraid_mbox_build_cmd(adapter_t *, struct scsi_cmnd *, int *);
  104. static void megaraid_mbox_runpendq(adapter_t *, scb_t *);
  105. static void megaraid_mbox_prepare_pthru(adapter_t *, scb_t *,
  106. struct scsi_cmnd *);
  107. static void megaraid_mbox_prepare_epthru(adapter_t *, scb_t *,
  108. struct scsi_cmnd *);
  109. static irqreturn_t megaraid_isr(int, void *, struct pt_regs *);
  110. static void megaraid_mbox_dpc(unsigned long);
  111. static ssize_t megaraid_sysfs_show_app_hndl(struct class_device *, char *);
  112. static ssize_t megaraid_sysfs_show_ldnum(struct device *, struct device_attribute *attr, char *);
  113. static int megaraid_cmm_register(adapter_t *);
  114. static int megaraid_cmm_unregister(adapter_t *);
  115. static int megaraid_mbox_mm_handler(unsigned long, uioc_t *, uint32_t);
  116. static int megaraid_mbox_mm_command(adapter_t *, uioc_t *);
  117. static void megaraid_mbox_mm_done(adapter_t *, scb_t *);
  118. static int gather_hbainfo(adapter_t *, mraid_hba_info_t *);
  119. static int wait_till_fw_empty(adapter_t *);
  120. MODULE_AUTHOR("LSI Logic Corporation");
  121. MODULE_DESCRIPTION("LSI Logic MegaRAID Mailbox Driver");
  122. MODULE_LICENSE("GPL");
  123. MODULE_VERSION(MEGARAID_VERSION);
  124. /*
  125. * ### modules parameters for driver ###
  126. */
  127. /**
  128. * Set to enable driver to expose unconfigured disk to kernel
  129. */
  130. static int megaraid_expose_unconf_disks = 0;
  131. module_param_named(unconf_disks, megaraid_expose_unconf_disks, int, 0);
  132. MODULE_PARM_DESC(unconf_disks,
  133. "Set to expose unconfigured disks to kernel (default=0)");
  134. /**
  135. * driver wait time if the adapter's mailbox is busy
  136. */
  137. static unsigned int max_mbox_busy_wait = MBOX_BUSY_WAIT;
  138. module_param_named(busy_wait, max_mbox_busy_wait, int, 0);
  139. MODULE_PARM_DESC(busy_wait,
  140. "Max wait for mailbox in microseconds if busy (default=10)");
  141. /**
  142. * number of sectors per IO command
  143. */
  144. static unsigned int megaraid_max_sectors = MBOX_MAX_SECTORS;
  145. module_param_named(max_sectors, megaraid_max_sectors, int, 0);
  146. MODULE_PARM_DESC(max_sectors,
  147. "Maximum number of sectors per IO command (default=128)");
  148. /**
  149. * number of commands per logical unit
  150. */
  151. static unsigned int megaraid_cmd_per_lun = MBOX_DEF_CMD_PER_LUN;
  152. module_param_named(cmd_per_lun, megaraid_cmd_per_lun, int, 0);
  153. MODULE_PARM_DESC(cmd_per_lun,
  154. "Maximum number of commands per logical unit (default=64)");
  155. /**
  156. * Fast driver load option, skip scanning for physical devices during load.
  157. * This would result in non-disk devices being skipped during driver load
  158. * time. These can be later added though, using /proc/scsi/scsi
  159. */
  160. static unsigned int megaraid_fast_load = 0;
  161. module_param_named(fast_load, megaraid_fast_load, int, 0);
  162. MODULE_PARM_DESC(fast_load,
  163. "Faster loading of the driver, skips physical devices! (default=0)");
  164. /**
  165. * mraid_debug level - threshold for amount of information to be displayed by
  166. * the driver. This level can be changed through modules parameters, ioctl or
  167. * sysfs/proc interface. By default, print the announcement messages only.
  168. */
  169. int mraid_debug_level = CL_ANN;
  170. module_param_named(debug_level, mraid_debug_level, int, 0);
  171. MODULE_PARM_DESC(debug_level, "Debug level for driver (default=0)");
  172. /*
  173. * ### global data ###
  174. */
  175. static uint8_t megaraid_mbox_version[8] =
  176. { 0x02, 0x20, 0x04, 0x06, 3, 7, 20, 5 };
  177. /*
  178. * PCI table for all supported controllers.
  179. */
  180. static struct pci_device_id pci_id_table_g[] = {
  181. {
  182. PCI_VENDOR_ID_DELL,
  183. PCI_DEVICE_ID_PERC4_DI_DISCOVERY,
  184. PCI_VENDOR_ID_DELL,
  185. PCI_SUBSYS_ID_PERC4_DI_DISCOVERY,
  186. },
  187. {
  188. PCI_VENDOR_ID_LSI_LOGIC,
  189. PCI_DEVICE_ID_PERC4_SC,
  190. PCI_VENDOR_ID_DELL,
  191. PCI_SUBSYS_ID_PERC4_SC,
  192. },
  193. {
  194. PCI_VENDOR_ID_LSI_LOGIC,
  195. PCI_DEVICE_ID_PERC4_DC,
  196. PCI_VENDOR_ID_DELL,
  197. PCI_SUBSYS_ID_PERC4_DC,
  198. },
  199. {
  200. PCI_VENDOR_ID_LSI_LOGIC,
  201. PCI_DEVICE_ID_VERDE,
  202. PCI_ANY_ID,
  203. PCI_ANY_ID,
  204. },
  205. {
  206. PCI_VENDOR_ID_DELL,
  207. PCI_DEVICE_ID_PERC4_DI_EVERGLADES,
  208. PCI_VENDOR_ID_DELL,
  209. PCI_SUBSYS_ID_PERC4_DI_EVERGLADES,
  210. },
  211. {
  212. PCI_VENDOR_ID_DELL,
  213. PCI_DEVICE_ID_PERC4E_SI_BIGBEND,
  214. PCI_VENDOR_ID_DELL,
  215. PCI_SUBSYS_ID_PERC4E_SI_BIGBEND,
  216. },
  217. {
  218. PCI_VENDOR_ID_DELL,
  219. PCI_DEVICE_ID_PERC4E_DI_KOBUK,
  220. PCI_VENDOR_ID_DELL,
  221. PCI_SUBSYS_ID_PERC4E_DI_KOBUK,
  222. },
  223. {
  224. PCI_VENDOR_ID_DELL,
  225. PCI_DEVICE_ID_PERC4E_DI_CORVETTE,
  226. PCI_VENDOR_ID_DELL,
  227. PCI_SUBSYS_ID_PERC4E_DI_CORVETTE,
  228. },
  229. {
  230. PCI_VENDOR_ID_DELL,
  231. PCI_DEVICE_ID_PERC4E_DI_EXPEDITION,
  232. PCI_VENDOR_ID_DELL,
  233. PCI_SUBSYS_ID_PERC4E_DI_EXPEDITION,
  234. },
  235. {
  236. PCI_VENDOR_ID_DELL,
  237. PCI_DEVICE_ID_PERC4E_DI_GUADALUPE,
  238. PCI_VENDOR_ID_DELL,
  239. PCI_SUBSYS_ID_PERC4E_DI_GUADALUPE,
  240. },
  241. {
  242. PCI_VENDOR_ID_LSI_LOGIC,
  243. PCI_DEVICE_ID_DOBSON,
  244. PCI_ANY_ID,
  245. PCI_ANY_ID,
  246. },
  247. {
  248. PCI_VENDOR_ID_AMI,
  249. PCI_DEVICE_ID_AMI_MEGARAID3,
  250. PCI_VENDOR_ID_DELL,
  251. PCI_SUBSYS_ID_PERC3_QC,
  252. },
  253. {
  254. PCI_VENDOR_ID_AMI,
  255. PCI_DEVICE_ID_AMI_MEGARAID3,
  256. PCI_VENDOR_ID_DELL,
  257. PCI_SUBSYS_ID_PERC3_DC,
  258. },
  259. {
  260. PCI_VENDOR_ID_AMI,
  261. PCI_DEVICE_ID_AMI_MEGARAID3,
  262. PCI_VENDOR_ID_DELL,
  263. PCI_SUBSYS_ID_PERC3_SC,
  264. },
  265. {
  266. PCI_VENDOR_ID_AMI,
  267. PCI_DEVICE_ID_AMI_MEGARAID3,
  268. PCI_VENDOR_ID_AMI,
  269. PCI_SUBSYS_ID_PERC3_SC,
  270. },
  271. {
  272. PCI_VENDOR_ID_AMI,
  273. PCI_DEVICE_ID_AMI_MEGARAID3,
  274. PCI_VENDOR_ID_AMI,
  275. PCI_SUBSYS_ID_PERC3_DC,
  276. },
  277. {
  278. PCI_VENDOR_ID_LSI_LOGIC,
  279. PCI_DEVICE_ID_MEGARAID_SCSI_320_0,
  280. PCI_VENDOR_ID_LSI_LOGIC,
  281. PCI_SUBSYS_ID_MEGARAID_SCSI_320_0,
  282. },
  283. {
  284. PCI_VENDOR_ID_LSI_LOGIC,
  285. PCI_DEVICE_ID_MEGARAID_SCSI_320_1,
  286. PCI_VENDOR_ID_LSI_LOGIC,
  287. PCI_SUBSYS_ID_MEGARAID_SCSI_320_1,
  288. },
  289. {
  290. PCI_VENDOR_ID_LSI_LOGIC,
  291. PCI_DEVICE_ID_MEGARAID_SCSI_320_2,
  292. PCI_VENDOR_ID_LSI_LOGIC,
  293. PCI_SUBSYS_ID_MEGARAID_SCSI_320_2,
  294. },
  295. {
  296. PCI_VENDOR_ID_LSI_LOGIC,
  297. PCI_DEVICE_ID_MEGARAID_I4_133_RAID,
  298. PCI_VENDOR_ID_LSI_LOGIC,
  299. PCI_SUBSYS_ID_MEGARAID_I4_133_RAID,
  300. },
  301. {
  302. PCI_VENDOR_ID_LSI_LOGIC,
  303. PCI_DEVICE_ID_MEGARAID_SATA_150_4,
  304. PCI_VENDOR_ID_LSI_LOGIC,
  305. PCI_SUBSYS_ID_MEGARAID_SATA_150_4,
  306. },
  307. {
  308. PCI_VENDOR_ID_LSI_LOGIC,
  309. PCI_DEVICE_ID_MEGARAID_SATA_150_6,
  310. PCI_VENDOR_ID_LSI_LOGIC,
  311. PCI_SUBSYS_ID_MEGARAID_SATA_150_6,
  312. },
  313. {
  314. PCI_VENDOR_ID_LSI_LOGIC,
  315. PCI_DEVICE_ID_LINDSAY,
  316. PCI_ANY_ID,
  317. PCI_ANY_ID,
  318. },
  319. {
  320. PCI_VENDOR_ID_LSI_LOGIC,
  321. PCI_DEVICE_ID_INTEL_RAID_SRCS16,
  322. PCI_VENDOR_ID_INTEL,
  323. PCI_SUBSYS_ID_INTEL_RAID_SRCS16,
  324. },
  325. {
  326. PCI_VENDOR_ID_LSI_LOGIC,
  327. PCI_DEVICE_ID_INTEL_RAID_SRCU41L_LAKE_SHETEK,
  328. PCI_VENDOR_ID_INTEL,
  329. PCI_SUBSYS_ID_INTEL_RAID_SRCU41L_LAKE_SHETEK,
  330. },
  331. {0} /* Terminating entry */
  332. };
  333. MODULE_DEVICE_TABLE(pci, pci_id_table_g);
  334. static struct pci_driver megaraid_pci_driver_g = {
  335. .name = "megaraid",
  336. .id_table = pci_id_table_g,
  337. .probe = megaraid_probe_one,
  338. .remove = __devexit_p(megaraid_detach_one),
  339. .driver = {
  340. .shutdown = megaraid_mbox_shutdown,
  341. }
  342. };
  343. // definitions for the device attributes for exporting logical drive number
  344. // for a scsi address (Host, Channel, Id, Lun)
  345. CLASS_DEVICE_ATTR(megaraid_mbox_app_hndl, S_IRUSR, megaraid_sysfs_show_app_hndl,
  346. NULL);
  347. // Host template initializer for megaraid mbox sysfs device attributes
  348. static struct class_device_attribute *megaraid_shost_attrs[] = {
  349. &class_device_attr_megaraid_mbox_app_hndl,
  350. NULL,
  351. };
  352. DEVICE_ATTR(megaraid_mbox_ld, S_IRUSR, megaraid_sysfs_show_ldnum, NULL);
  353. // Host template initializer for megaraid mbox sysfs device attributes
  354. static struct device_attribute *megaraid_sdev_attrs[] = {
  355. &dev_attr_megaraid_mbox_ld,
  356. NULL,
  357. };
  358. /*
  359. * Scsi host template for megaraid unified driver
  360. */
  361. static struct scsi_host_template megaraid_template_g = {
  362. .module = THIS_MODULE,
  363. .name = "LSI Logic MegaRAID driver",
  364. .proc_name = "megaraid",
  365. .queuecommand = megaraid_queue_command,
  366. .eh_abort_handler = megaraid_abort_handler,
  367. .eh_device_reset_handler = megaraid_reset_handler,
  368. .eh_bus_reset_handler = megaraid_reset_handler,
  369. .eh_host_reset_handler = megaraid_reset_handler,
  370. .use_clustering = ENABLE_CLUSTERING,
  371. .sdev_attrs = megaraid_sdev_attrs,
  372. .shost_attrs = megaraid_shost_attrs,
  373. };
  374. /**
  375. * megaraid_init - module load hook
  376. *
  377. * We register ourselves as hotplug enabled module and let PCI subsystem
  378. * discover our adaters
  379. **/
  380. static int __init
  381. megaraid_init(void)
  382. {
  383. int rval;
  384. // Announce the driver version
  385. con_log(CL_ANN, (KERN_INFO "megaraid: %s %s\n", MEGARAID_VERSION,
  386. MEGARAID_EXT_VERSION));
  387. // check validity of module parameters
  388. if (megaraid_cmd_per_lun > MBOX_MAX_SCSI_CMDS) {
  389. con_log(CL_ANN, (KERN_WARNING
  390. "megaraid mailbox: max commands per lun reset to %d\n",
  391. MBOX_MAX_SCSI_CMDS));
  392. megaraid_cmd_per_lun = MBOX_MAX_SCSI_CMDS;
  393. }
  394. // register as a PCI hot-plug driver module
  395. rval = pci_register_driver(&megaraid_pci_driver_g);
  396. if (rval < 0) {
  397. con_log(CL_ANN, (KERN_WARNING
  398. "megaraid: could not register hotplug support.\n"));
  399. }
  400. return rval;
  401. }
  402. /**
  403. * megaraid_exit - driver unload entry point
  404. *
  405. * We simply unwrap the megaraid_init routine here
  406. */
  407. static void __exit
  408. megaraid_exit(void)
  409. {
  410. con_log(CL_DLEVEL1, (KERN_NOTICE "megaraid: unloading framework\n"));
  411. // unregister as PCI hotplug driver
  412. pci_unregister_driver(&megaraid_pci_driver_g);
  413. return;
  414. }
  415. /**
  416. * megaraid_probe_one - PCI hotplug entry point
  417. * @param pdev : handle to this controller's PCI configuration space
  418. * @param id : pci device id of the class of controllers
  419. *
  420. * This routine should be called whenever a new adapter is detected by the
  421. * PCI hotplug susbsytem.
  422. **/
  423. static int __devinit
  424. megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  425. {
  426. adapter_t *adapter;
  427. // detected a new controller
  428. con_log(CL_ANN, (KERN_INFO
  429. "megaraid: probe new device %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
  430. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  431. pdev->subsystem_device));
  432. con_log(CL_ANN, ("bus %d:slot %d:func %d\n", pdev->bus->number,
  433. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)));
  434. if (pci_enable_device(pdev)) {
  435. con_log(CL_ANN, (KERN_WARNING
  436. "megaraid: pci_enable_device failed\n"));
  437. return -ENODEV;
  438. }
  439. // Enable bus-mastering on this controller
  440. pci_set_master(pdev);
  441. // Allocate the per driver initialization structure
  442. adapter = kmalloc(sizeof(adapter_t), GFP_KERNEL);
  443. if (adapter == NULL) {
  444. con_log(CL_ANN, (KERN_WARNING
  445. "megaraid: out of memory, %s %d.\n", __FUNCTION__, __LINE__));
  446. goto out_probe_one;
  447. }
  448. memset(adapter, 0, sizeof(adapter_t));
  449. // set up PCI related soft state and other pre-known parameters
  450. adapter->unique_id = pdev->bus->number << 8 | pdev->devfn;
  451. adapter->irq = pdev->irq;
  452. adapter->pdev = pdev;
  453. atomic_set(&adapter->being_detached, 0);
  454. // Setup the default DMA mask. This would be changed later on
  455. // depending on hardware capabilities
  456. if (pci_set_dma_mask(adapter->pdev, DMA_32BIT_MASK) != 0) {
  457. con_log(CL_ANN, (KERN_WARNING
  458. "megaraid: pci_set_dma_mask failed:%d\n", __LINE__));
  459. goto out_free_adapter;
  460. }
  461. // Initialize the synchronization lock for kernel and LLD
  462. spin_lock_init(&adapter->lock);
  463. adapter->host_lock = &adapter->lock;
  464. // Initialize the command queues: the list of free SCBs and the list
  465. // of pending SCBs.
  466. INIT_LIST_HEAD(&adapter->kscb_pool);
  467. spin_lock_init(SCSI_FREE_LIST_LOCK(adapter));
  468. INIT_LIST_HEAD(&adapter->pend_list);
  469. spin_lock_init(PENDING_LIST_LOCK(adapter));
  470. INIT_LIST_HEAD(&adapter->completed_list);
  471. spin_lock_init(COMPLETED_LIST_LOCK(adapter));
  472. // Start the mailbox based controller
  473. if (megaraid_init_mbox(adapter) != 0) {
  474. con_log(CL_ANN, (KERN_WARNING
  475. "megaraid: maibox adapter did not initialize\n"));
  476. goto out_free_adapter;
  477. }
  478. // Register with LSI Common Management Module
  479. if (megaraid_cmm_register(adapter) != 0) {
  480. con_log(CL_ANN, (KERN_WARNING
  481. "megaraid: could not register with management module\n"));
  482. goto out_fini_mbox;
  483. }
  484. // setup adapter handle in PCI soft state
  485. pci_set_drvdata(pdev, adapter);
  486. // attach with scsi mid-layer
  487. if (megaraid_io_attach(adapter) != 0) {
  488. con_log(CL_ANN, (KERN_WARNING "megaraid: io attach failed\n"));
  489. goto out_cmm_unreg;
  490. }
  491. return 0;
  492. out_cmm_unreg:
  493. pci_set_drvdata(pdev, NULL);
  494. megaraid_cmm_unregister(adapter);
  495. out_fini_mbox:
  496. megaraid_fini_mbox(adapter);
  497. out_free_adapter:
  498. kfree(adapter);
  499. out_probe_one:
  500. pci_disable_device(pdev);
  501. return -ENODEV;
  502. }
  503. /**
  504. * megaraid_detach_one - release the framework resources and call LLD release
  505. * routine
  506. * @param pdev : handle for our PCI cofiguration space
  507. *
  508. * This routine is called during driver unload. We free all the allocated
  509. * resources and call the corresponding LLD so that it can also release all
  510. * its resources.
  511. *
  512. * This routine is also called from the PCI hotplug system
  513. **/
  514. static void
  515. megaraid_detach_one(struct pci_dev *pdev)
  516. {
  517. adapter_t *adapter;
  518. struct Scsi_Host *host;
  519. // Start a rollback on this adapter
  520. adapter = pci_get_drvdata(pdev);
  521. if (!adapter) {
  522. con_log(CL_ANN, (KERN_CRIT
  523. "megaraid: Invalid detach on %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
  524. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  525. pdev->subsystem_device));
  526. return;
  527. }
  528. else {
  529. con_log(CL_ANN, (KERN_NOTICE
  530. "megaraid: detaching device %#4.04x:%#4.04x:%#4.04x:%#4.04x\n",
  531. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  532. pdev->subsystem_device));
  533. }
  534. host = adapter->host;
  535. // do not allow any more requests from the management module for this
  536. // adapter.
  537. // FIXME: How do we account for the request which might still be
  538. // pending with us?
  539. atomic_set(&adapter->being_detached, 1);
  540. // detach from the IO sub-system
  541. megaraid_io_detach(adapter);
  542. // reset the device state in the PCI structure. We check this
  543. // condition when we enter here. If the device state is NULL,
  544. // that would mean the device has already been removed
  545. pci_set_drvdata(pdev, NULL);
  546. // Unregister from common management module
  547. //
  548. // FIXME: this must return success or failure for conditions if there
  549. // is a command pending with LLD or not.
  550. megaraid_cmm_unregister(adapter);
  551. // finalize the mailbox based controller and release all resources
  552. megaraid_fini_mbox(adapter);
  553. kfree(adapter);
  554. scsi_host_put(host);
  555. pci_disable_device(pdev);
  556. return;
  557. }
  558. /**
  559. * megaraid_mbox_shutdown - PCI shutdown for megaraid HBA
  560. * @param device : generice driver model device
  561. *
  562. * Shutdown notification, perform flush cache
  563. */
  564. static void
  565. megaraid_mbox_shutdown(struct device *device)
  566. {
  567. adapter_t *adapter = pci_get_drvdata(to_pci_dev(device));
  568. static int counter;
  569. if (!adapter) {
  570. con_log(CL_ANN, (KERN_WARNING
  571. "megaraid: null device in shutdown\n"));
  572. return;
  573. }
  574. // flush caches now
  575. con_log(CL_ANN, (KERN_INFO "megaraid: flushing adapter %d...",
  576. counter++));
  577. megaraid_mbox_flush_cache(adapter);
  578. con_log(CL_ANN, ("done\n"));
  579. }
  580. /**
  581. * megaraid_io_attach - attach a device with the IO subsystem
  582. * @param adapter : controller's soft state
  583. *
  584. * Attach this device with the IO subsystem
  585. **/
  586. static int
  587. megaraid_io_attach(adapter_t *adapter)
  588. {
  589. struct Scsi_Host *host;
  590. // Initialize SCSI Host structure
  591. host = scsi_host_alloc(&megaraid_template_g, 8);
  592. if (!host) {
  593. con_log(CL_ANN, (KERN_WARNING
  594. "megaraid mbox: scsi_register failed\n"));
  595. return -1;
  596. }
  597. SCSIHOST2ADAP(host) = (caddr_t)adapter;
  598. adapter->host = host;
  599. // export the parameters required by the mid-layer
  600. scsi_assign_lock(host, adapter->host_lock);
  601. scsi_set_device(host, &adapter->pdev->dev);
  602. host->irq = adapter->irq;
  603. host->unique_id = adapter->unique_id;
  604. host->can_queue = adapter->max_cmds;
  605. host->this_id = adapter->init_id;
  606. host->sg_tablesize = adapter->sglen;
  607. host->max_sectors = adapter->max_sectors;
  608. host->cmd_per_lun = adapter->cmd_per_lun;
  609. host->max_channel = adapter->max_channel;
  610. host->max_id = adapter->max_target;
  611. host->max_lun = adapter->max_lun;
  612. // notify mid-layer about the new controller
  613. if (scsi_add_host(host, &adapter->pdev->dev)) {
  614. con_log(CL_ANN, (KERN_WARNING
  615. "megaraid mbox: scsi_add_host failed\n"));
  616. scsi_host_put(host);
  617. return -1;
  618. }
  619. scsi_scan_host(host);
  620. return 0;
  621. }
  622. /**
  623. * megaraid_io_detach - detach a device from the IO subsystem
  624. * @param adapter : controller's soft state
  625. *
  626. * Detach this device from the IO subsystem
  627. **/
  628. static void
  629. megaraid_io_detach(adapter_t *adapter)
  630. {
  631. struct Scsi_Host *host;
  632. con_log(CL_DLEVEL1, (KERN_INFO "megaraid: io detach\n"));
  633. host = adapter->host;
  634. scsi_remove_host(host);
  635. return;
  636. }
  637. /*
  638. * START: Mailbox Low Level Driver
  639. *
  640. * This is section specific to the single mailbox based controllers
  641. */
  642. /**
  643. * megaraid_init_mbox - initialize controller
  644. * @param adapter - our soft state
  645. *
  646. * . Allocate 16-byte aligned mailbox memory for firmware handshake
  647. * . Allocate controller's memory resources
  648. * . Find out all initialization data
  649. * . Allocate memory required for all the commands
  650. * . Use internal library of FW routines, build up complete soft state
  651. */
  652. static int __init
  653. megaraid_init_mbox(adapter_t *adapter)
  654. {
  655. struct pci_dev *pdev;
  656. mraid_device_t *raid_dev;
  657. int i;
  658. adapter->ito = MBOX_TIMEOUT;
  659. pdev = adapter->pdev;
  660. /*
  661. * Allocate and initialize the init data structure for mailbox
  662. * controllers
  663. */
  664. raid_dev = kmalloc(sizeof(mraid_device_t), GFP_KERNEL);
  665. if (raid_dev == NULL) return -1;
  666. memset(raid_dev, 0, sizeof(mraid_device_t));
  667. /*
  668. * Attach the adapter soft state to raid device soft state
  669. */
  670. adapter->raid_device = (caddr_t)raid_dev;
  671. raid_dev->fast_load = megaraid_fast_load;
  672. // our baseport
  673. raid_dev->baseport = pci_resource_start(pdev, 0);
  674. if (pci_request_regions(pdev, "MegaRAID: LSI Logic Corporation") != 0) {
  675. con_log(CL_ANN, (KERN_WARNING
  676. "megaraid: mem region busy\n"));
  677. goto out_free_raid_dev;
  678. }
  679. raid_dev->baseaddr = ioremap_nocache(raid_dev->baseport, 128);
  680. if (!raid_dev->baseaddr) {
  681. con_log(CL_ANN, (KERN_WARNING
  682. "megaraid: could not map hba memory\n") );
  683. goto out_release_regions;
  684. }
  685. //
  686. // Setup the rest of the soft state using the library of FW routines
  687. //
  688. // request IRQ and register the interrupt service routine
  689. if (request_irq(adapter->irq, megaraid_isr, SA_SHIRQ, "megaraid",
  690. adapter)) {
  691. con_log(CL_ANN, (KERN_WARNING
  692. "megaraid: Couldn't register IRQ %d!\n", adapter->irq));
  693. goto out_iounmap;
  694. }
  695. // initialize the mutual exclusion lock for the mailbox
  696. spin_lock_init(&raid_dev->mailbox_lock);
  697. // allocate memory required for commands
  698. if (megaraid_alloc_cmd_packets(adapter) != 0) {
  699. goto out_free_irq;
  700. }
  701. // Product info
  702. if (megaraid_mbox_product_info(adapter) != 0) {
  703. goto out_alloc_cmds;
  704. }
  705. // Do we support extended CDBs
  706. adapter->max_cdb_sz = 10;
  707. if (megaraid_mbox_extended_cdb(adapter) == 0) {
  708. adapter->max_cdb_sz = 16;
  709. }
  710. /*
  711. * Do we support cluster environment, if we do, what is the initiator
  712. * id.
  713. * NOTE: In a non-cluster aware firmware environment, the LLD should
  714. * return 7 as initiator id.
  715. */
  716. adapter->ha = 0;
  717. adapter->init_id = -1;
  718. if (megaraid_mbox_support_ha(adapter, &adapter->init_id) == 0) {
  719. adapter->ha = 1;
  720. }
  721. /*
  722. * Prepare the device ids array to have the mapping between the kernel
  723. * device address and megaraid device address.
  724. * We export the physical devices on their actual addresses. The
  725. * logical drives are exported on a virtual SCSI channel
  726. */
  727. megaraid_mbox_setup_device_map(adapter);
  728. // If the firmware supports random deletion, update the device id map
  729. if (megaraid_mbox_support_random_del(adapter)) {
  730. // Change the logical drives numbers in device_ids array one
  731. // slot in device_ids is reserved for target id, that's why
  732. // "<=" below
  733. for (i = 0; i <= MAX_LOGICAL_DRIVES_40LD; i++) {
  734. adapter->device_ids[adapter->max_channel][i] += 0x80;
  735. }
  736. adapter->device_ids[adapter->max_channel][adapter->init_id] =
  737. 0xFF;
  738. raid_dev->random_del_supported = 1;
  739. }
  740. /*
  741. * find out the maximum number of scatter-gather elements supported by
  742. * this firmware
  743. */
  744. adapter->sglen = megaraid_mbox_get_max_sg(adapter);
  745. // enumerate RAID and SCSI channels so that all devices on SCSI
  746. // channels can later be exported, including disk devices
  747. megaraid_mbox_enum_raid_scsi(adapter);
  748. /*
  749. * Other parameters required by upper layer
  750. *
  751. * maximum number of sectors per IO command
  752. */
  753. adapter->max_sectors = megaraid_max_sectors;
  754. /*
  755. * number of queued commands per LUN.
  756. */
  757. adapter->cmd_per_lun = megaraid_cmd_per_lun;
  758. /*
  759. * Allocate resources required to issue FW calls, when sysfs is
  760. * accessed
  761. */
  762. if (megaraid_sysfs_alloc_resources(adapter) != 0) {
  763. goto out_alloc_cmds;
  764. }
  765. // Set the DMA mask to 64-bit. All supported controllers as capable of
  766. // DMA in this range
  767. if (pci_set_dma_mask(adapter->pdev, DMA_64BIT_MASK) != 0) {
  768. con_log(CL_ANN, (KERN_WARNING
  769. "megaraid: could not set DMA mask for 64-bit.\n"));
  770. goto out_free_sysfs_res;
  771. }
  772. // setup tasklet for DPC
  773. tasklet_init(&adapter->dpc_h, megaraid_mbox_dpc,
  774. (unsigned long)adapter);
  775. con_log(CL_DLEVEL1, (KERN_INFO
  776. "megaraid mbox hba successfully initialized\n"));
  777. return 0;
  778. out_free_sysfs_res:
  779. megaraid_sysfs_free_resources(adapter);
  780. out_alloc_cmds:
  781. megaraid_free_cmd_packets(adapter);
  782. out_free_irq:
  783. free_irq(adapter->irq, adapter);
  784. out_iounmap:
  785. iounmap(raid_dev->baseaddr);
  786. out_release_regions:
  787. pci_release_regions(pdev);
  788. out_free_raid_dev:
  789. kfree(raid_dev);
  790. return -1;
  791. }
  792. /**
  793. * megaraid_fini_mbox - undo controller initialization
  794. * @param adapter : our soft state
  795. */
  796. static void
  797. megaraid_fini_mbox(adapter_t *adapter)
  798. {
  799. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  800. // flush all caches
  801. megaraid_mbox_flush_cache(adapter);
  802. tasklet_kill(&adapter->dpc_h);
  803. megaraid_sysfs_free_resources(adapter);
  804. megaraid_free_cmd_packets(adapter);
  805. free_irq(adapter->irq, adapter);
  806. iounmap(raid_dev->baseaddr);
  807. pci_release_regions(adapter->pdev);
  808. kfree(raid_dev);
  809. return;
  810. }
  811. /**
  812. * megaraid_alloc_cmd_packets - allocate shared mailbox
  813. * @param adapter : soft state of the raid controller
  814. *
  815. * Allocate and align the shared mailbox. This maibox is used to issue
  816. * all the commands. For IO based controllers, the mailbox is also regsitered
  817. * with the FW. Allocate memory for all commands as well.
  818. * This is our big allocator
  819. */
  820. static int
  821. megaraid_alloc_cmd_packets(adapter_t *adapter)
  822. {
  823. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  824. struct pci_dev *pdev;
  825. unsigned long align;
  826. scb_t *scb;
  827. mbox_ccb_t *ccb;
  828. struct mraid_pci_blk *epthru_pci_blk;
  829. struct mraid_pci_blk *sg_pci_blk;
  830. struct mraid_pci_blk *mbox_pci_blk;
  831. int i;
  832. pdev = adapter->pdev;
  833. /*
  834. * Setup the mailbox
  835. * Allocate the common 16-byte aligned memory for the handshake
  836. * mailbox.
  837. */
  838. raid_dev->una_mbox64 = pci_alloc_consistent(adapter->pdev,
  839. sizeof(mbox64_t), &raid_dev->una_mbox64_dma);
  840. if (!raid_dev->una_mbox64) {
  841. con_log(CL_ANN, (KERN_WARNING
  842. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  843. __LINE__));
  844. return -1;
  845. }
  846. memset(raid_dev->una_mbox64, 0, sizeof(mbox64_t));
  847. /*
  848. * Align the mailbox at 16-byte boundary
  849. */
  850. raid_dev->mbox = &raid_dev->una_mbox64->mbox32;
  851. raid_dev->mbox = (mbox_t *)((((unsigned long)raid_dev->mbox) + 15) &
  852. (~0UL ^ 0xFUL));
  853. raid_dev->mbox64 = (mbox64_t *)(((unsigned long)raid_dev->mbox) - 8);
  854. align = ((void *)raid_dev->mbox -
  855. ((void *)&raid_dev->una_mbox64->mbox32));
  856. raid_dev->mbox_dma = (unsigned long)raid_dev->una_mbox64_dma + 8 +
  857. align;
  858. // Allocate memory for commands issued internally
  859. adapter->ibuf = pci_alloc_consistent(pdev, MBOX_IBUF_SIZE,
  860. &adapter->ibuf_dma_h);
  861. if (!adapter->ibuf) {
  862. con_log(CL_ANN, (KERN_WARNING
  863. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  864. __LINE__));
  865. goto out_free_common_mbox;
  866. }
  867. memset(adapter->ibuf, 0, MBOX_IBUF_SIZE);
  868. // Allocate memory for our SCSI Command Blocks and their associated
  869. // memory
  870. /*
  871. * Allocate memory for the base list of scb. Later allocate memory for
  872. * CCBs and embedded components of each CCB and point the pointers in
  873. * scb to the allocated components
  874. * NOTE: The code to allocate SCB will be duplicated in all the LLD
  875. * since the calling routine does not yet know the number of available
  876. * commands.
  877. */
  878. adapter->kscb_list = kmalloc(sizeof(scb_t) * MBOX_MAX_SCSI_CMDS,
  879. GFP_KERNEL);
  880. if (adapter->kscb_list == NULL) {
  881. con_log(CL_ANN, (KERN_WARNING
  882. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  883. __LINE__));
  884. goto out_free_ibuf;
  885. }
  886. memset(adapter->kscb_list, 0, sizeof(scb_t) * MBOX_MAX_SCSI_CMDS);
  887. // memory allocation for our command packets
  888. if (megaraid_mbox_setup_dma_pools(adapter) != 0) {
  889. con_log(CL_ANN, (KERN_WARNING
  890. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  891. __LINE__));
  892. goto out_free_scb_list;
  893. }
  894. // Adjust the scb pointers and link in the free pool
  895. epthru_pci_blk = raid_dev->epthru_pool;
  896. sg_pci_blk = raid_dev->sg_pool;
  897. mbox_pci_blk = raid_dev->mbox_pool;
  898. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  899. scb = adapter->kscb_list + i;
  900. ccb = raid_dev->ccb_list + i;
  901. ccb->mbox = (mbox_t *)(mbox_pci_blk[i].vaddr + 16);
  902. ccb->raw_mbox = (uint8_t *)ccb->mbox;
  903. ccb->mbox64 = (mbox64_t *)(mbox_pci_blk[i].vaddr + 8);
  904. ccb->mbox_dma_h = (unsigned long)mbox_pci_blk[i].dma_addr + 16;
  905. // make sure the mailbox is aligned properly
  906. if (ccb->mbox_dma_h & 0x0F) {
  907. con_log(CL_ANN, (KERN_CRIT
  908. "megaraid mbox: not aligned on 16-bytes\n"));
  909. goto out_teardown_dma_pools;
  910. }
  911. ccb->epthru = (mraid_epassthru_t *)
  912. epthru_pci_blk[i].vaddr;
  913. ccb->epthru_dma_h = epthru_pci_blk[i].dma_addr;
  914. ccb->pthru = (mraid_passthru_t *)ccb->epthru;
  915. ccb->pthru_dma_h = ccb->epthru_dma_h;
  916. ccb->sgl64 = (mbox_sgl64 *)sg_pci_blk[i].vaddr;
  917. ccb->sgl_dma_h = sg_pci_blk[i].dma_addr;
  918. ccb->sgl32 = (mbox_sgl32 *)ccb->sgl64;
  919. scb->ccb = (caddr_t)ccb;
  920. scb->gp = 0;
  921. scb->sno = i; // command index
  922. scb->scp = NULL;
  923. scb->state = SCB_FREE;
  924. scb->dma_direction = PCI_DMA_NONE;
  925. scb->dma_type = MRAID_DMA_NONE;
  926. scb->dev_channel = -1;
  927. scb->dev_target = -1;
  928. // put scb in the free pool
  929. list_add_tail(&scb->list, &adapter->kscb_pool);
  930. }
  931. return 0;
  932. out_teardown_dma_pools:
  933. megaraid_mbox_teardown_dma_pools(adapter);
  934. out_free_scb_list:
  935. kfree(adapter->kscb_list);
  936. out_free_ibuf:
  937. pci_free_consistent(pdev, MBOX_IBUF_SIZE, (void *)adapter->ibuf,
  938. adapter->ibuf_dma_h);
  939. out_free_common_mbox:
  940. pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
  941. (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
  942. return -1;
  943. }
  944. /**
  945. * megaraid_free_cmd_packets - free memory
  946. * @param adapter : soft state of the raid controller
  947. *
  948. * Release memory resources allocated for commands
  949. */
  950. static void
  951. megaraid_free_cmd_packets(adapter_t *adapter)
  952. {
  953. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  954. megaraid_mbox_teardown_dma_pools(adapter);
  955. kfree(adapter->kscb_list);
  956. pci_free_consistent(adapter->pdev, MBOX_IBUF_SIZE,
  957. (void *)adapter->ibuf, adapter->ibuf_dma_h);
  958. pci_free_consistent(adapter->pdev, sizeof(mbox64_t),
  959. (caddr_t)raid_dev->una_mbox64, raid_dev->una_mbox64_dma);
  960. return;
  961. }
  962. /**
  963. * megaraid_mbox_setup_dma_pools - setup dma pool for command packets
  964. * @param adapter : HBA soft state
  965. *
  966. * setup the dma pools for mailbox, passthru and extended passthru structures,
  967. * and scatter-gather lists
  968. */
  969. static int
  970. megaraid_mbox_setup_dma_pools(adapter_t *adapter)
  971. {
  972. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  973. struct mraid_pci_blk *epthru_pci_blk;
  974. struct mraid_pci_blk *sg_pci_blk;
  975. struct mraid_pci_blk *mbox_pci_blk;
  976. int i;
  977. // Allocate memory for 16-bytes aligned mailboxes
  978. raid_dev->mbox_pool_handle = pci_pool_create("megaraid mbox pool",
  979. adapter->pdev,
  980. sizeof(mbox64_t) + 16,
  981. 16, 0);
  982. if (raid_dev->mbox_pool_handle == NULL) {
  983. goto fail_setup_dma_pool;
  984. }
  985. mbox_pci_blk = raid_dev->mbox_pool;
  986. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  987. mbox_pci_blk[i].vaddr = pci_pool_alloc(
  988. raid_dev->mbox_pool_handle,
  989. GFP_KERNEL,
  990. &mbox_pci_blk[i].dma_addr);
  991. if (!mbox_pci_blk[i].vaddr) {
  992. goto fail_setup_dma_pool;
  993. }
  994. }
  995. /*
  996. * Allocate memory for each embedded passthru strucuture pointer
  997. * Request for a 128 bytes aligned structure for each passthru command
  998. * structure
  999. * Since passthru and extended passthru commands are exclusive, they
  1000. * share common memory pool. Passthru structures piggyback on memory
  1001. * allocted to extended passthru since passthru is smaller of the two
  1002. */
  1003. raid_dev->epthru_pool_handle = pci_pool_create("megaraid mbox pthru",
  1004. adapter->pdev, sizeof(mraid_epassthru_t), 128, 0);
  1005. if (raid_dev->epthru_pool_handle == NULL) {
  1006. goto fail_setup_dma_pool;
  1007. }
  1008. epthru_pci_blk = raid_dev->epthru_pool;
  1009. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  1010. epthru_pci_blk[i].vaddr = pci_pool_alloc(
  1011. raid_dev->epthru_pool_handle,
  1012. GFP_KERNEL,
  1013. &epthru_pci_blk[i].dma_addr);
  1014. if (!epthru_pci_blk[i].vaddr) {
  1015. goto fail_setup_dma_pool;
  1016. }
  1017. }
  1018. // Allocate memory for each scatter-gather list. Request for 512 bytes
  1019. // alignment for each sg list
  1020. raid_dev->sg_pool_handle = pci_pool_create("megaraid mbox sg",
  1021. adapter->pdev,
  1022. sizeof(mbox_sgl64) * MBOX_MAX_SG_SIZE,
  1023. 512, 0);
  1024. if (raid_dev->sg_pool_handle == NULL) {
  1025. goto fail_setup_dma_pool;
  1026. }
  1027. sg_pci_blk = raid_dev->sg_pool;
  1028. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  1029. sg_pci_blk[i].vaddr = pci_pool_alloc(
  1030. raid_dev->sg_pool_handle,
  1031. GFP_KERNEL,
  1032. &sg_pci_blk[i].dma_addr);
  1033. if (!sg_pci_blk[i].vaddr) {
  1034. goto fail_setup_dma_pool;
  1035. }
  1036. }
  1037. return 0;
  1038. fail_setup_dma_pool:
  1039. megaraid_mbox_teardown_dma_pools(adapter);
  1040. return -1;
  1041. }
  1042. /**
  1043. * megaraid_mbox_teardown_dma_pools - teardown dma pools for command packets
  1044. * @param adapter : HBA soft state
  1045. *
  1046. * teardown the dma pool for mailbox, passthru and extended passthru
  1047. * structures, and scatter-gather lists
  1048. */
  1049. static void
  1050. megaraid_mbox_teardown_dma_pools(adapter_t *adapter)
  1051. {
  1052. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  1053. struct mraid_pci_blk *epthru_pci_blk;
  1054. struct mraid_pci_blk *sg_pci_blk;
  1055. struct mraid_pci_blk *mbox_pci_blk;
  1056. int i;
  1057. sg_pci_blk = raid_dev->sg_pool;
  1058. for (i = 0; i < MBOX_MAX_SCSI_CMDS && sg_pci_blk[i].vaddr; i++) {
  1059. pci_pool_free(raid_dev->sg_pool_handle, sg_pci_blk[i].vaddr,
  1060. sg_pci_blk[i].dma_addr);
  1061. }
  1062. if (raid_dev->sg_pool_handle)
  1063. pci_pool_destroy(raid_dev->sg_pool_handle);
  1064. epthru_pci_blk = raid_dev->epthru_pool;
  1065. for (i = 0; i < MBOX_MAX_SCSI_CMDS && epthru_pci_blk[i].vaddr; i++) {
  1066. pci_pool_free(raid_dev->epthru_pool_handle,
  1067. epthru_pci_blk[i].vaddr, epthru_pci_blk[i].dma_addr);
  1068. }
  1069. if (raid_dev->epthru_pool_handle)
  1070. pci_pool_destroy(raid_dev->epthru_pool_handle);
  1071. mbox_pci_blk = raid_dev->mbox_pool;
  1072. for (i = 0; i < MBOX_MAX_SCSI_CMDS && mbox_pci_blk[i].vaddr; i++) {
  1073. pci_pool_free(raid_dev->mbox_pool_handle,
  1074. mbox_pci_blk[i].vaddr, mbox_pci_blk[i].dma_addr);
  1075. }
  1076. if (raid_dev->mbox_pool_handle)
  1077. pci_pool_destroy(raid_dev->mbox_pool_handle);
  1078. return;
  1079. }
  1080. /**
  1081. * megaraid_alloc_scb - detach and return a scb from the free list
  1082. * @adapter : controller's soft state
  1083. *
  1084. * return the scb from the head of the free list. NULL if there are none
  1085. * available
  1086. **/
  1087. static inline scb_t *
  1088. megaraid_alloc_scb(adapter_t *adapter, struct scsi_cmnd *scp)
  1089. {
  1090. struct list_head *head = &adapter->kscb_pool;
  1091. scb_t *scb = NULL;
  1092. unsigned long flags;
  1093. // detach scb from free pool
  1094. spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
  1095. if (list_empty(head)) {
  1096. spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
  1097. return NULL;
  1098. }
  1099. scb = list_entry(head->next, scb_t, list);
  1100. list_del_init(&scb->list);
  1101. spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
  1102. scb->state = SCB_ACTIVE;
  1103. scb->scp = scp;
  1104. scb->dma_type = MRAID_DMA_NONE;
  1105. return scb;
  1106. }
  1107. /**
  1108. * megaraid_dealloc_scb - return the scb to the free pool
  1109. * @adapter : controller's soft state
  1110. * @scb : scb to be freed
  1111. *
  1112. * return the scb back to the free list of scbs. The caller must 'flush' the
  1113. * SCB before calling us. E.g., performing pci_unamp and/or pci_sync etc.
  1114. * NOTE NOTE: Make sure the scb is not on any list before calling this
  1115. * routine.
  1116. **/
  1117. static inline void
  1118. megaraid_dealloc_scb(adapter_t *adapter, scb_t *scb)
  1119. {
  1120. unsigned long flags;
  1121. // put scb in the free pool
  1122. scb->state = SCB_FREE;
  1123. scb->scp = NULL;
  1124. spin_lock_irqsave(SCSI_FREE_LIST_LOCK(adapter), flags);
  1125. list_add(&scb->list, &adapter->kscb_pool);
  1126. spin_unlock_irqrestore(SCSI_FREE_LIST_LOCK(adapter), flags);
  1127. return;
  1128. }
  1129. /**
  1130. * megaraid_mbox_mksgl - make the scatter-gather list
  1131. * @adapter - controller's soft state
  1132. * @scb - scsi control block
  1133. *
  1134. * prepare the scatter-gather list
  1135. */
  1136. static inline int
  1137. megaraid_mbox_mksgl(adapter_t *adapter, scb_t *scb)
  1138. {
  1139. struct scatterlist *sgl;
  1140. mbox_ccb_t *ccb;
  1141. struct page *page;
  1142. unsigned long offset;
  1143. struct scsi_cmnd *scp;
  1144. int sgcnt;
  1145. int i;
  1146. scp = scb->scp;
  1147. ccb = (mbox_ccb_t *)scb->ccb;
  1148. // no mapping required if no data to be transferred
  1149. if (!scp->request_buffer || !scp->request_bufflen)
  1150. return 0;
  1151. if (!scp->use_sg) { /* scatter-gather list not used */
  1152. page = virt_to_page(scp->request_buffer);
  1153. offset = ((unsigned long)scp->request_buffer & ~PAGE_MASK);
  1154. ccb->buf_dma_h = pci_map_page(adapter->pdev, page, offset,
  1155. scp->request_bufflen,
  1156. scb->dma_direction);
  1157. scb->dma_type = MRAID_DMA_WBUF;
  1158. /*
  1159. * We need to handle special 64-bit commands that need a
  1160. * minimum of 1 SG
  1161. */
  1162. sgcnt = 1;
  1163. ccb->sgl64[0].address = ccb->buf_dma_h;
  1164. ccb->sgl64[0].length = scp->request_bufflen;
  1165. return sgcnt;
  1166. }
  1167. sgl = (struct scatterlist *)scp->request_buffer;
  1168. // The number of sg elements returned must not exceed our limit
  1169. sgcnt = pci_map_sg(adapter->pdev, sgl, scp->use_sg,
  1170. scb->dma_direction);
  1171. if (sgcnt > adapter->sglen) {
  1172. con_log(CL_ANN, (KERN_CRIT
  1173. "megaraid critical: too many sg elements:%d\n",
  1174. sgcnt));
  1175. BUG();
  1176. }
  1177. scb->dma_type = MRAID_DMA_WSG;
  1178. for (i = 0; i < sgcnt; i++, sgl++) {
  1179. ccb->sgl64[i].address = sg_dma_address(sgl);
  1180. ccb->sgl64[i].length = sg_dma_len(sgl);
  1181. }
  1182. // Return count of SG nodes
  1183. return sgcnt;
  1184. }
  1185. /**
  1186. * mbox_post_cmd - issue a mailbox command
  1187. * @adapter - controller's soft state
  1188. * @scb - command to be issued
  1189. *
  1190. * post the command to the controller if mailbox is availble.
  1191. */
  1192. static inline int
  1193. mbox_post_cmd(adapter_t *adapter, scb_t *scb)
  1194. {
  1195. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  1196. mbox64_t *mbox64;
  1197. mbox_t *mbox;
  1198. mbox_ccb_t *ccb;
  1199. unsigned long flags;
  1200. unsigned int i = 0;
  1201. ccb = (mbox_ccb_t *)scb->ccb;
  1202. mbox = raid_dev->mbox;
  1203. mbox64 = raid_dev->mbox64;
  1204. /*
  1205. * Check for busy mailbox. If it is, return failure - the caller
  1206. * should retry later.
  1207. */
  1208. spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
  1209. if (unlikely(mbox->busy)) {
  1210. do {
  1211. udelay(1);
  1212. i++;
  1213. rmb();
  1214. } while(mbox->busy && (i < max_mbox_busy_wait));
  1215. if (mbox->busy) {
  1216. spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
  1217. return -1;
  1218. }
  1219. }
  1220. // Copy this command's mailbox data into "adapter's" mailbox
  1221. memcpy((caddr_t)mbox64, (caddr_t)ccb->mbox64, 22);
  1222. mbox->cmdid = scb->sno;
  1223. adapter->outstanding_cmds++;
  1224. if (scb->dma_direction == PCI_DMA_TODEVICE) {
  1225. if (!scb->scp->use_sg) { // sg list not used
  1226. pci_dma_sync_single_for_device(adapter->pdev,
  1227. ccb->buf_dma_h,
  1228. scb->scp->request_bufflen,
  1229. PCI_DMA_TODEVICE);
  1230. }
  1231. else {
  1232. pci_dma_sync_sg_for_device(adapter->pdev,
  1233. scb->scp->request_buffer,
  1234. scb->scp->use_sg, PCI_DMA_TODEVICE);
  1235. }
  1236. }
  1237. mbox->busy = 1; // Set busy
  1238. mbox->poll = 0;
  1239. mbox->ack = 0;
  1240. wmb();
  1241. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  1242. spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
  1243. return 0;
  1244. }
  1245. /**
  1246. * megaraid_queue_command - generic queue entry point for all LLDs
  1247. * @scp : pointer to the scsi command to be executed
  1248. * @done : callback routine to be called after the cmd has be completed
  1249. *
  1250. * Queue entry point for mailbox based controllers.
  1251. */
  1252. static int
  1253. megaraid_queue_command(struct scsi_cmnd *scp, void (* done)(struct scsi_cmnd *))
  1254. {
  1255. adapter_t *adapter;
  1256. scb_t *scb;
  1257. int if_busy;
  1258. adapter = SCP2ADAPTER(scp);
  1259. scp->scsi_done = done;
  1260. scp->result = 0;
  1261. assert_spin_locked(adapter->host_lock);
  1262. spin_unlock(adapter->host_lock);
  1263. /*
  1264. * Allocate and build a SCB request
  1265. * if_busy flag will be set if megaraid_mbox_build_cmd() command could
  1266. * not allocate scb. We will return non-zero status in that case.
  1267. * NOTE: scb can be null even though certain commands completed
  1268. * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, it would
  1269. * return 0 in that case, and we would do the callback right away.
  1270. */
  1271. if_busy = 0;
  1272. scb = megaraid_mbox_build_cmd(adapter, scp, &if_busy);
  1273. if (scb) {
  1274. megaraid_mbox_runpendq(adapter, scb);
  1275. }
  1276. spin_lock(adapter->host_lock);
  1277. if (!scb) { // command already completed
  1278. done(scp);
  1279. return 0;
  1280. }
  1281. return if_busy;
  1282. }
  1283. /**
  1284. * megaraid_mbox_build_cmd - transform the mid-layer scsi command to megaraid
  1285. * firmware lingua
  1286. * @adapter - controller's soft state
  1287. * @scp - mid-layer scsi command pointer
  1288. * @busy - set if request could not be completed because of lack of
  1289. * resources
  1290. *
  1291. * convert the command issued by mid-layer to format understood by megaraid
  1292. * firmware. We also complete certain command without sending them to firmware
  1293. */
  1294. static scb_t *
  1295. megaraid_mbox_build_cmd(adapter_t *adapter, struct scsi_cmnd *scp, int *busy)
  1296. {
  1297. mraid_device_t *rdev = ADAP2RAIDDEV(adapter);
  1298. int channel;
  1299. int target;
  1300. int islogical;
  1301. mbox_ccb_t *ccb;
  1302. mraid_passthru_t *pthru;
  1303. mbox64_t *mbox64;
  1304. mbox_t *mbox;
  1305. scb_t *scb;
  1306. char skip[] = "skipping";
  1307. char scan[] = "scanning";
  1308. char *ss;
  1309. /*
  1310. * Get the appropriate device map for the device this command is
  1311. * intended for
  1312. */
  1313. MRAID_GET_DEVICE_MAP(adapter, scp, channel, target, islogical);
  1314. /*
  1315. * Logical drive commands
  1316. */
  1317. if (islogical) {
  1318. switch (scp->cmnd[0]) {
  1319. case TEST_UNIT_READY:
  1320. /*
  1321. * Do we support clustering and is the support enabled
  1322. * If no, return success always
  1323. */
  1324. if (!adapter->ha) {
  1325. scp->result = (DID_OK << 16);
  1326. return NULL;
  1327. }
  1328. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1329. scp->result = (DID_ERROR << 16);
  1330. *busy = 1;
  1331. return NULL;
  1332. }
  1333. scb->dma_direction = scp->sc_data_direction;
  1334. scb->dev_channel = 0xFF;
  1335. scb->dev_target = target;
  1336. ccb = (mbox_ccb_t *)scb->ccb;
  1337. /*
  1338. * The command id will be provided by the command
  1339. * issuance routine
  1340. */
  1341. ccb->raw_mbox[0] = CLUSTER_CMD;
  1342. ccb->raw_mbox[2] = RESERVATION_STATUS;
  1343. ccb->raw_mbox[3] = target;
  1344. return scb;
  1345. case MODE_SENSE:
  1346. if (scp->use_sg) {
  1347. struct scatterlist *sgl;
  1348. caddr_t vaddr;
  1349. sgl = (struct scatterlist *)scp->request_buffer;
  1350. if (sgl->page) {
  1351. vaddr = (caddr_t)
  1352. (page_address((&sgl[0])->page)
  1353. + (&sgl[0])->offset);
  1354. memset(vaddr, 0, scp->cmnd[4]);
  1355. }
  1356. else {
  1357. con_log(CL_ANN, (KERN_WARNING
  1358. "megaraid mailbox: invalid sg:%d\n",
  1359. __LINE__));
  1360. }
  1361. }
  1362. else {
  1363. memset(scp->request_buffer, 0, scp->cmnd[4]);
  1364. }
  1365. scp->result = (DID_OK << 16);
  1366. return NULL;
  1367. case INQUIRY:
  1368. /*
  1369. * Display the channel scan for logical drives
  1370. * Do not display scan for a channel if already done.
  1371. */
  1372. if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
  1373. con_log(CL_ANN, (KERN_INFO
  1374. "scsi[%d]: scanning scsi channel %d",
  1375. adapter->host->host_no,
  1376. SCP2CHANNEL(scp)));
  1377. con_log(CL_ANN, (
  1378. " [virtual] for logical drives\n"));
  1379. rdev->last_disp |= (1L << SCP2CHANNEL(scp));
  1380. }
  1381. /* Fall through */
  1382. case READ_CAPACITY:
  1383. /*
  1384. * Do not allow LUN > 0 for logical drives and
  1385. * requests for more than 40 logical drives
  1386. */
  1387. if (SCP2LUN(scp)) {
  1388. scp->result = (DID_BAD_TARGET << 16);
  1389. return NULL;
  1390. }
  1391. if ((target % 0x80) >= MAX_LOGICAL_DRIVES_40LD) {
  1392. scp->result = (DID_BAD_TARGET << 16);
  1393. return NULL;
  1394. }
  1395. /* Allocate a SCB and initialize passthru */
  1396. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1397. scp->result = (DID_ERROR << 16);
  1398. *busy = 1;
  1399. return NULL;
  1400. }
  1401. ccb = (mbox_ccb_t *)scb->ccb;
  1402. scb->dev_channel = 0xFF;
  1403. scb->dev_target = target;
  1404. pthru = ccb->pthru;
  1405. mbox = ccb->mbox;
  1406. mbox64 = ccb->mbox64;
  1407. pthru->timeout = 0;
  1408. pthru->ars = 1;
  1409. pthru->reqsenselen = 14;
  1410. pthru->islogical = 1;
  1411. pthru->logdrv = target;
  1412. pthru->cdblen = scp->cmd_len;
  1413. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  1414. mbox->cmd = MBOXCMD_PASSTHRU64;
  1415. scb->dma_direction = scp->sc_data_direction;
  1416. pthru->dataxferlen = scp->request_bufflen;
  1417. pthru->dataxferaddr = ccb->sgl_dma_h;
  1418. pthru->numsge = megaraid_mbox_mksgl(adapter,
  1419. scb);
  1420. mbox->xferaddr = 0xFFFFFFFF;
  1421. mbox64->xferaddr_lo = (uint32_t )ccb->pthru_dma_h;
  1422. mbox64->xferaddr_hi = 0;
  1423. return scb;
  1424. case READ_6:
  1425. case WRITE_6:
  1426. case READ_10:
  1427. case WRITE_10:
  1428. case READ_12:
  1429. case WRITE_12:
  1430. /*
  1431. * Allocate a SCB and initialize mailbox
  1432. */
  1433. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1434. scp->result = (DID_ERROR << 16);
  1435. *busy = 1;
  1436. return NULL;
  1437. }
  1438. ccb = (mbox_ccb_t *)scb->ccb;
  1439. scb->dev_channel = 0xFF;
  1440. scb->dev_target = target;
  1441. mbox = ccb->mbox;
  1442. mbox64 = ccb->mbox64;
  1443. mbox->logdrv = target;
  1444. /*
  1445. * A little HACK: 2nd bit is zero for all scsi read
  1446. * commands and is set for all scsi write commands
  1447. */
  1448. mbox->cmd = (scp->cmnd[0] & 0x02) ? MBOXCMD_LWRITE64:
  1449. MBOXCMD_LREAD64 ;
  1450. /*
  1451. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  1452. */
  1453. if (scp->cmd_len == 6) {
  1454. mbox->numsectors = (uint32_t)scp->cmnd[4];
  1455. mbox->lba =
  1456. ((uint32_t)scp->cmnd[1] << 16) |
  1457. ((uint32_t)scp->cmnd[2] << 8) |
  1458. (uint32_t)scp->cmnd[3];
  1459. mbox->lba &= 0x1FFFFF;
  1460. }
  1461. /*
  1462. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  1463. */
  1464. else if (scp->cmd_len == 10) {
  1465. mbox->numsectors =
  1466. (uint32_t)scp->cmnd[8] |
  1467. ((uint32_t)scp->cmnd[7] << 8);
  1468. mbox->lba =
  1469. ((uint32_t)scp->cmnd[2] << 24) |
  1470. ((uint32_t)scp->cmnd[3] << 16) |
  1471. ((uint32_t)scp->cmnd[4] << 8) |
  1472. (uint32_t)scp->cmnd[5];
  1473. }
  1474. /*
  1475. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  1476. */
  1477. else if (scp->cmd_len == 12) {
  1478. mbox->lba =
  1479. ((uint32_t)scp->cmnd[2] << 24) |
  1480. ((uint32_t)scp->cmnd[3] << 16) |
  1481. ((uint32_t)scp->cmnd[4] << 8) |
  1482. (uint32_t)scp->cmnd[5];
  1483. mbox->numsectors =
  1484. ((uint32_t)scp->cmnd[6] << 24) |
  1485. ((uint32_t)scp->cmnd[7] << 16) |
  1486. ((uint32_t)scp->cmnd[8] << 8) |
  1487. (uint32_t)scp->cmnd[9];
  1488. }
  1489. else {
  1490. con_log(CL_ANN, (KERN_WARNING
  1491. "megaraid: unsupported CDB length\n"));
  1492. megaraid_dealloc_scb(adapter, scb);
  1493. scp->result = (DID_ERROR << 16);
  1494. return NULL;
  1495. }
  1496. scb->dma_direction = scp->sc_data_direction;
  1497. // Calculate Scatter-Gather info
  1498. mbox64->xferaddr_lo = (uint32_t )ccb->sgl_dma_h;
  1499. mbox->numsge = megaraid_mbox_mksgl(adapter,
  1500. scb);
  1501. mbox->xferaddr = 0xFFFFFFFF;
  1502. mbox64->xferaddr_hi = 0;
  1503. return scb;
  1504. case RESERVE:
  1505. case RELEASE:
  1506. /*
  1507. * Do we support clustering and is the support enabled
  1508. */
  1509. if (!adapter->ha) {
  1510. scp->result = (DID_BAD_TARGET << 16);
  1511. return NULL;
  1512. }
  1513. /*
  1514. * Allocate a SCB and initialize mailbox
  1515. */
  1516. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1517. scp->result = (DID_ERROR << 16);
  1518. *busy = 1;
  1519. return NULL;
  1520. }
  1521. ccb = (mbox_ccb_t *)scb->ccb;
  1522. scb->dev_channel = 0xFF;
  1523. scb->dev_target = target;
  1524. ccb->raw_mbox[0] = CLUSTER_CMD;
  1525. ccb->raw_mbox[2] = (scp->cmnd[0] == RESERVE) ?
  1526. RESERVE_LD : RELEASE_LD;
  1527. ccb->raw_mbox[3] = target;
  1528. scb->dma_direction = scp->sc_data_direction;
  1529. return scb;
  1530. default:
  1531. scp->result = (DID_BAD_TARGET << 16);
  1532. return NULL;
  1533. }
  1534. }
  1535. else { // Passthru device commands
  1536. // Do not allow access to target id > 15 or LUN > 7
  1537. if (target > 15 || SCP2LUN(scp) > 7) {
  1538. scp->result = (DID_BAD_TARGET << 16);
  1539. return NULL;
  1540. }
  1541. // if fast load option was set and scan for last device is
  1542. // over, reset the fast_load flag so that during a possible
  1543. // next scan, devices can be made available
  1544. if (rdev->fast_load && (target == 15) &&
  1545. (SCP2CHANNEL(scp) == adapter->max_channel -1)) {
  1546. con_log(CL_ANN, (KERN_INFO
  1547. "megaraid[%d]: physical device scan re-enabled\n",
  1548. adapter->host->host_no));
  1549. rdev->fast_load = 0;
  1550. }
  1551. /*
  1552. * Display the channel scan for physical devices
  1553. */
  1554. if (!(rdev->last_disp & (1L << SCP2CHANNEL(scp)))) {
  1555. ss = rdev->fast_load ? skip : scan;
  1556. con_log(CL_ANN, (KERN_INFO
  1557. "scsi[%d]: %s scsi channel %d [Phy %d]",
  1558. adapter->host->host_no, ss, SCP2CHANNEL(scp),
  1559. channel));
  1560. con_log(CL_ANN, (
  1561. " for non-raid devices\n"));
  1562. rdev->last_disp |= (1L << SCP2CHANNEL(scp));
  1563. }
  1564. // disable channel sweep if fast load option given
  1565. if (rdev->fast_load) {
  1566. scp->result = (DID_BAD_TARGET << 16);
  1567. return NULL;
  1568. }
  1569. // Allocate a SCB and initialize passthru
  1570. if (!(scb = megaraid_alloc_scb(adapter, scp))) {
  1571. scp->result = (DID_ERROR << 16);
  1572. *busy = 1;
  1573. return NULL;
  1574. }
  1575. ccb = (mbox_ccb_t *)scb->ccb;
  1576. scb->dev_channel = channel;
  1577. scb->dev_target = target;
  1578. scb->dma_direction = scp->sc_data_direction;
  1579. mbox = ccb->mbox;
  1580. mbox64 = ccb->mbox64;
  1581. // Does this firmware support extended CDBs
  1582. if (adapter->max_cdb_sz == 16) {
  1583. mbox->cmd = MBOXCMD_EXTPTHRU;
  1584. megaraid_mbox_prepare_epthru(adapter, scb, scp);
  1585. mbox64->xferaddr_lo = (uint32_t)ccb->epthru_dma_h;
  1586. mbox64->xferaddr_hi = 0;
  1587. mbox->xferaddr = 0xFFFFFFFF;
  1588. }
  1589. else {
  1590. mbox->cmd = MBOXCMD_PASSTHRU64;
  1591. megaraid_mbox_prepare_pthru(adapter, scb, scp);
  1592. mbox64->xferaddr_lo = (uint32_t)ccb->pthru_dma_h;
  1593. mbox64->xferaddr_hi = 0;
  1594. mbox->xferaddr = 0xFFFFFFFF;
  1595. }
  1596. return scb;
  1597. }
  1598. // NOT REACHED
  1599. }
  1600. /**
  1601. * megaraid_mbox_runpendq - execute commands queued in the pending queue
  1602. * @adapter : controller's soft state
  1603. * @scb : SCB to be queued in the pending list
  1604. *
  1605. * scan the pending list for commands which are not yet issued and try to
  1606. * post to the controller. The SCB can be a null pointer, which would indicate
  1607. * no SCB to be queue, just try to execute the ones in the pending list.
  1608. *
  1609. * NOTE: We do not actually traverse the pending list. The SCBs are plucked
  1610. * out from the head of the pending list. If it is successfully issued, the
  1611. * next SCB is at the head now.
  1612. */
  1613. static void
  1614. megaraid_mbox_runpendq(adapter_t *adapter, scb_t *scb_q)
  1615. {
  1616. scb_t *scb;
  1617. unsigned long flags;
  1618. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  1619. if (scb_q) {
  1620. scb_q->state = SCB_PENDQ;
  1621. list_add_tail(&scb_q->list, &adapter->pend_list);
  1622. }
  1623. // if the adapter in not in quiescent mode, post the commands to FW
  1624. if (adapter->quiescent) {
  1625. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  1626. return;
  1627. }
  1628. while (!list_empty(&adapter->pend_list)) {
  1629. assert_spin_locked(PENDING_LIST_LOCK(adapter));
  1630. scb = list_entry(adapter->pend_list.next, scb_t, list);
  1631. // remove the scb from the pending list and try to
  1632. // issue. If we are unable to issue it, put back in
  1633. // the pending list and return
  1634. list_del_init(&scb->list);
  1635. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  1636. // if mailbox was busy, return SCB back to pending
  1637. // list. Make sure to add at the head, since that's
  1638. // where it would have been removed from
  1639. scb->state = SCB_ISSUED;
  1640. if (mbox_post_cmd(adapter, scb) != 0) {
  1641. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  1642. scb->state = SCB_PENDQ;
  1643. list_add(&scb->list, &adapter->pend_list);
  1644. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
  1645. flags);
  1646. return;
  1647. }
  1648. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  1649. }
  1650. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  1651. return;
  1652. }
  1653. /**
  1654. * megaraid_mbox_prepare_pthru - prepare a command for physical devices
  1655. * @adapter - pointer to controller's soft state
  1656. * @scb - scsi control block
  1657. * @scp - scsi command from the mid-layer
  1658. *
  1659. * prepare a command for the scsi physical devices
  1660. */
  1661. static void
  1662. megaraid_mbox_prepare_pthru(adapter_t *adapter, scb_t *scb,
  1663. struct scsi_cmnd *scp)
  1664. {
  1665. mbox_ccb_t *ccb;
  1666. mraid_passthru_t *pthru;
  1667. uint8_t channel;
  1668. uint8_t target;
  1669. ccb = (mbox_ccb_t *)scb->ccb;
  1670. pthru = ccb->pthru;
  1671. channel = scb->dev_channel;
  1672. target = scb->dev_target;
  1673. // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
  1674. pthru->timeout = 4;
  1675. pthru->ars = 1;
  1676. pthru->islogical = 0;
  1677. pthru->channel = 0;
  1678. pthru->target = (channel << 4) | target;
  1679. pthru->logdrv = SCP2LUN(scp);
  1680. pthru->reqsenselen = 14;
  1681. pthru->cdblen = scp->cmd_len;
  1682. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  1683. if (scp->request_bufflen) {
  1684. pthru->dataxferlen = scp->request_bufflen;
  1685. pthru->dataxferaddr = ccb->sgl_dma_h;
  1686. pthru->numsge = megaraid_mbox_mksgl(adapter, scb);
  1687. }
  1688. else {
  1689. pthru->dataxferaddr = 0;
  1690. pthru->dataxferlen = 0;
  1691. pthru->numsge = 0;
  1692. }
  1693. return;
  1694. }
  1695. /**
  1696. * megaraid_mbox_prepare_epthru - prepare a command for physical devices
  1697. * @adapter - pointer to controller's soft state
  1698. * @scb - scsi control block
  1699. * @scp - scsi command from the mid-layer
  1700. *
  1701. * prepare a command for the scsi physical devices. This rountine prepares
  1702. * commands for devices which can take extended CDBs (>10 bytes)
  1703. */
  1704. static void
  1705. megaraid_mbox_prepare_epthru(adapter_t *adapter, scb_t *scb,
  1706. struct scsi_cmnd *scp)
  1707. {
  1708. mbox_ccb_t *ccb;
  1709. mraid_epassthru_t *epthru;
  1710. uint8_t channel;
  1711. uint8_t target;
  1712. ccb = (mbox_ccb_t *)scb->ccb;
  1713. epthru = ccb->epthru;
  1714. channel = scb->dev_channel;
  1715. target = scb->dev_target;
  1716. // 0=6sec, 1=60sec, 2=10min, 3=3hrs, 4=NO timeout
  1717. epthru->timeout = 4;
  1718. epthru->ars = 1;
  1719. epthru->islogical = 0;
  1720. epthru->channel = 0;
  1721. epthru->target = (channel << 4) | target;
  1722. epthru->logdrv = SCP2LUN(scp);
  1723. epthru->reqsenselen = 14;
  1724. epthru->cdblen = scp->cmd_len;
  1725. memcpy(epthru->cdb, scp->cmnd, scp->cmd_len);
  1726. if (scp->request_bufflen) {
  1727. epthru->dataxferlen = scp->request_bufflen;
  1728. epthru->dataxferaddr = ccb->sgl_dma_h;
  1729. epthru->numsge = megaraid_mbox_mksgl(adapter, scb);
  1730. }
  1731. else {
  1732. epthru->dataxferaddr = 0;
  1733. epthru->dataxferlen = 0;
  1734. epthru->numsge = 0;
  1735. }
  1736. return;
  1737. }
  1738. /**
  1739. * megaraid_ack_sequence - interrupt ack sequence for memory mapped HBAs
  1740. * @adapter - controller's soft state
  1741. *
  1742. * Interrupt ackrowledgement sequence for memory mapped HBAs. Find out the
  1743. * completed command and put them on the completed list for later processing.
  1744. *
  1745. * Returns: 1 if the interrupt is valid, 0 otherwise
  1746. */
  1747. static inline int
  1748. megaraid_ack_sequence(adapter_t *adapter)
  1749. {
  1750. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  1751. mbox_t *mbox;
  1752. scb_t *scb;
  1753. uint8_t nstatus;
  1754. uint8_t completed[MBOX_MAX_FIRMWARE_STATUS];
  1755. struct list_head clist;
  1756. int handled;
  1757. uint32_t dword;
  1758. unsigned long flags;
  1759. int i, j;
  1760. mbox = raid_dev->mbox;
  1761. // move the SCBs from the firmware completed array to our local list
  1762. INIT_LIST_HEAD(&clist);
  1763. // loop till F/W has more commands for us to complete
  1764. handled = 0;
  1765. spin_lock_irqsave(MAILBOX_LOCK(raid_dev), flags);
  1766. do {
  1767. /*
  1768. * Check if a valid interrupt is pending. If found, force the
  1769. * interrupt line low.
  1770. */
  1771. dword = RDOUTDOOR(raid_dev);
  1772. if (dword != 0x10001234) break;
  1773. handled = 1;
  1774. WROUTDOOR(raid_dev, 0x10001234);
  1775. nstatus = 0;
  1776. // wait for valid numstatus to post
  1777. for (i = 0; i < 0xFFFFF; i++) {
  1778. if (mbox->numstatus != 0xFF) {
  1779. nstatus = mbox->numstatus;
  1780. break;
  1781. }
  1782. rmb();
  1783. }
  1784. mbox->numstatus = 0xFF;
  1785. adapter->outstanding_cmds -= nstatus;
  1786. for (i = 0; i < nstatus; i++) {
  1787. // wait for valid command index to post
  1788. for (j = 0; j < 0xFFFFF; j++) {
  1789. if (mbox->completed[i] != 0xFF) break;
  1790. rmb();
  1791. }
  1792. completed[i] = mbox->completed[i];
  1793. mbox->completed[i] = 0xFF;
  1794. if (completed[i] == 0xFF) {
  1795. con_log(CL_ANN, (KERN_CRIT
  1796. "megaraid: command posting timed out\n"));
  1797. BUG();
  1798. continue;
  1799. }
  1800. // Get SCB associated with this command id
  1801. if (completed[i] >= MBOX_MAX_SCSI_CMDS) {
  1802. // a cmm command
  1803. scb = adapter->uscb_list + (completed[i] -
  1804. MBOX_MAX_SCSI_CMDS);
  1805. }
  1806. else {
  1807. // an os command
  1808. scb = adapter->kscb_list + completed[i];
  1809. }
  1810. scb->status = mbox->status;
  1811. list_add_tail(&scb->list, &clist);
  1812. }
  1813. // Acknowledge interrupt
  1814. WRINDOOR(raid_dev, 0x02);
  1815. } while(1);
  1816. spin_unlock_irqrestore(MAILBOX_LOCK(raid_dev), flags);
  1817. // put the completed commands in the completed list. DPC would
  1818. // complete these commands later
  1819. spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
  1820. list_splice(&clist, &adapter->completed_list);
  1821. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
  1822. // schedule the DPC if there is some work for it
  1823. if (handled)
  1824. tasklet_schedule(&adapter->dpc_h);
  1825. return handled;
  1826. }
  1827. /**
  1828. * megaraid_isr - isr for memory based mailbox based controllers
  1829. * @irq - irq
  1830. * @devp - pointer to our soft state
  1831. * @regs - unused
  1832. *
  1833. * Interrupt service routine for memory-mapped mailbox controllers.
  1834. */
  1835. static irqreturn_t
  1836. megaraid_isr(int irq, void *devp, struct pt_regs *regs)
  1837. {
  1838. adapter_t *adapter = devp;
  1839. int handled;
  1840. handled = megaraid_ack_sequence(adapter);
  1841. /* Loop through any pending requests */
  1842. if (!adapter->quiescent) {
  1843. megaraid_mbox_runpendq(adapter, NULL);
  1844. }
  1845. return IRQ_RETVAL(handled);
  1846. }
  1847. /**
  1848. * megaraid_mbox_sync_scb - sync kernel buffers
  1849. * @adapter : controller's soft state
  1850. * @scb : pointer to the resource packet
  1851. *
  1852. * DMA sync if required.
  1853. */
  1854. static inline void
  1855. megaraid_mbox_sync_scb(adapter_t *adapter, scb_t *scb)
  1856. {
  1857. mbox_ccb_t *ccb;
  1858. ccb = (mbox_ccb_t *)scb->ccb;
  1859. switch (scb->dma_type) {
  1860. case MRAID_DMA_WBUF:
  1861. if (scb->dma_direction == PCI_DMA_FROMDEVICE) {
  1862. pci_dma_sync_single_for_cpu(adapter->pdev,
  1863. ccb->buf_dma_h,
  1864. scb->scp->request_bufflen,
  1865. PCI_DMA_FROMDEVICE);
  1866. }
  1867. pci_unmap_page(adapter->pdev, ccb->buf_dma_h,
  1868. scb->scp->request_bufflen, scb->dma_direction);
  1869. break;
  1870. case MRAID_DMA_WSG:
  1871. if (scb->dma_direction == PCI_DMA_FROMDEVICE) {
  1872. pci_dma_sync_sg_for_cpu(adapter->pdev,
  1873. scb->scp->request_buffer,
  1874. scb->scp->use_sg, PCI_DMA_FROMDEVICE);
  1875. }
  1876. pci_unmap_sg(adapter->pdev, scb->scp->request_buffer,
  1877. scb->scp->use_sg, scb->dma_direction);
  1878. break;
  1879. default:
  1880. break;
  1881. }
  1882. return;
  1883. }
  1884. /**
  1885. * megaraid_mbox_dpc - the tasklet to complete the commands from completed list
  1886. * @devp : pointer to HBA soft state
  1887. *
  1888. * Pick up the commands from the completed list and send back to the owners.
  1889. * This is a reentrant function and does not assume any locks are held while
  1890. * it is being called.
  1891. */
  1892. static void
  1893. megaraid_mbox_dpc(unsigned long devp)
  1894. {
  1895. adapter_t *adapter = (adapter_t *)devp;
  1896. mraid_device_t *raid_dev;
  1897. struct list_head clist;
  1898. struct scatterlist *sgl;
  1899. scb_t *scb;
  1900. scb_t *tmp;
  1901. struct scsi_cmnd *scp;
  1902. mraid_passthru_t *pthru;
  1903. mraid_epassthru_t *epthru;
  1904. mbox_ccb_t *ccb;
  1905. int islogical;
  1906. int pdev_index;
  1907. int pdev_state;
  1908. mbox_t *mbox;
  1909. unsigned long flags;
  1910. uint8_t c;
  1911. int status;
  1912. if (!adapter) return;
  1913. raid_dev = ADAP2RAIDDEV(adapter);
  1914. // move the SCBs from the completed list to our local list
  1915. INIT_LIST_HEAD(&clist);
  1916. spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
  1917. list_splice_init(&adapter->completed_list, &clist);
  1918. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
  1919. list_for_each_entry_safe(scb, tmp, &clist, list) {
  1920. status = scb->status;
  1921. scp = scb->scp;
  1922. ccb = (mbox_ccb_t *)scb->ccb;
  1923. pthru = ccb->pthru;
  1924. epthru = ccb->epthru;
  1925. mbox = ccb->mbox;
  1926. // Make sure f/w has completed a valid command
  1927. if (scb->state != SCB_ISSUED) {
  1928. con_log(CL_ANN, (KERN_CRIT
  1929. "megaraid critical err: invalid command %d:%d:%p\n",
  1930. scb->sno, scb->state, scp));
  1931. BUG();
  1932. continue; // Must never happen!
  1933. }
  1934. // check for the management command and complete it right away
  1935. if (scb->sno >= MBOX_MAX_SCSI_CMDS) {
  1936. scb->state = SCB_FREE;
  1937. scb->status = status;
  1938. // remove from local clist
  1939. list_del_init(&scb->list);
  1940. megaraid_mbox_mm_done(adapter, scb);
  1941. continue;
  1942. }
  1943. // Was an abort issued for this command earlier
  1944. if (scb->state & SCB_ABORT) {
  1945. con_log(CL_ANN, (KERN_NOTICE
  1946. "megaraid: aborted cmd %lx[%x] completed\n",
  1947. scp->serial_number, scb->sno));
  1948. }
  1949. /*
  1950. * If the inquiry came of a disk drive which is not part of
  1951. * any RAID array, expose it to the kernel. For this to be
  1952. * enabled, user must set the "megaraid_expose_unconf_disks"
  1953. * flag to 1 by specifying it on module parameter list.
  1954. * This would enable data migration off drives from other
  1955. * configurations.
  1956. */
  1957. islogical = MRAID_IS_LOGICAL(adapter, scp);
  1958. if (scp->cmnd[0] == INQUIRY && status == 0 && islogical == 0
  1959. && IS_RAID_CH(raid_dev, scb->dev_channel)) {
  1960. if (scp->use_sg) {
  1961. sgl = (struct scatterlist *)
  1962. scp->request_buffer;
  1963. if (sgl->page) {
  1964. c = *(unsigned char *)
  1965. (page_address((&sgl[0])->page) +
  1966. (&sgl[0])->offset);
  1967. }
  1968. else {
  1969. con_log(CL_ANN, (KERN_WARNING
  1970. "megaraid mailbox: invalid sg:%d\n",
  1971. __LINE__));
  1972. c = 0;
  1973. }
  1974. }
  1975. else {
  1976. c = *(uint8_t *)scp->request_buffer;
  1977. }
  1978. if ((c & 0x1F ) == TYPE_DISK) {
  1979. pdev_index = (scb->dev_channel * 16) +
  1980. scb->dev_target;
  1981. pdev_state =
  1982. raid_dev->pdrv_state[pdev_index] & 0x0F;
  1983. if (pdev_state == PDRV_ONLINE ||
  1984. pdev_state == PDRV_FAILED ||
  1985. pdev_state == PDRV_RBLD ||
  1986. pdev_state == PDRV_HOTSPARE ||
  1987. megaraid_expose_unconf_disks == 0) {
  1988. status = 0xF0;
  1989. }
  1990. }
  1991. }
  1992. // Convert MegaRAID status to Linux error code
  1993. switch (status) {
  1994. case 0x00:
  1995. scp->result = (DID_OK << 16);
  1996. break;
  1997. case 0x02:
  1998. /* set sense_buffer and result fields */
  1999. if (mbox->cmd == MBOXCMD_PASSTHRU ||
  2000. mbox->cmd == MBOXCMD_PASSTHRU64) {
  2001. memcpy(scp->sense_buffer, pthru->reqsensearea,
  2002. 14);
  2003. scp->result = DRIVER_SENSE << 24 |
  2004. DID_OK << 16 | CHECK_CONDITION << 1;
  2005. }
  2006. else {
  2007. if (mbox->cmd == MBOXCMD_EXTPTHRU) {
  2008. memcpy(scp->sense_buffer,
  2009. epthru->reqsensearea, 14);
  2010. scp->result = DRIVER_SENSE << 24 |
  2011. DID_OK << 16 |
  2012. CHECK_CONDITION << 1;
  2013. } else {
  2014. scp->sense_buffer[0] = 0x70;
  2015. scp->sense_buffer[2] = ABORTED_COMMAND;
  2016. scp->result = CHECK_CONDITION << 1;
  2017. }
  2018. }
  2019. break;
  2020. case 0x08:
  2021. scp->result = DID_BUS_BUSY << 16 | status;
  2022. break;
  2023. default:
  2024. /*
  2025. * If TEST_UNIT_READY fails, we know RESERVATION_STATUS
  2026. * failed
  2027. */
  2028. if (scp->cmnd[0] == TEST_UNIT_READY) {
  2029. scp->result = DID_ERROR << 16 |
  2030. RESERVATION_CONFLICT << 1;
  2031. }
  2032. else
  2033. /*
  2034. * Error code returned is 1 if Reserve or Release
  2035. * failed or the input parameter is invalid
  2036. */
  2037. if (status == 1 && (scp->cmnd[0] == RESERVE ||
  2038. scp->cmnd[0] == RELEASE)) {
  2039. scp->result = DID_ERROR << 16 |
  2040. RESERVATION_CONFLICT << 1;
  2041. }
  2042. else {
  2043. scp->result = DID_BAD_TARGET << 16 | status;
  2044. }
  2045. }
  2046. // print a debug message for all failed commands
  2047. if (status) {
  2048. megaraid_mbox_display_scb(adapter, scb);
  2049. }
  2050. // Free our internal resources and call the mid-layer callback
  2051. // routine
  2052. megaraid_mbox_sync_scb(adapter, scb);
  2053. // remove from local clist
  2054. list_del_init(&scb->list);
  2055. // put back in free list
  2056. megaraid_dealloc_scb(adapter, scb);
  2057. // send the scsi packet back to kernel
  2058. spin_lock(adapter->host_lock);
  2059. scp->scsi_done(scp);
  2060. spin_unlock(adapter->host_lock);
  2061. }
  2062. return;
  2063. }
  2064. /**
  2065. * megaraid_abort_handler - abort the scsi command
  2066. * @scp : command to be aborted
  2067. *
  2068. * Abort a previous SCSI request. Only commands on the pending list can be
  2069. * aborted. All the commands issued to the F/W must complete.
  2070. **/
  2071. static int
  2072. __megaraid_abort_handler(struct scsi_cmnd *scp)
  2073. {
  2074. adapter_t *adapter;
  2075. mraid_device_t *raid_dev;
  2076. scb_t *scb;
  2077. scb_t *tmp;
  2078. int found;
  2079. unsigned long flags;
  2080. int i;
  2081. adapter = SCP2ADAPTER(scp);
  2082. raid_dev = ADAP2RAIDDEV(adapter);
  2083. assert_spin_locked(adapter->host_lock);
  2084. con_log(CL_ANN, (KERN_WARNING
  2085. "megaraid: aborting-%ld cmd=%x <c=%d t=%d l=%d>\n",
  2086. scp->serial_number, scp->cmnd[0], SCP2CHANNEL(scp),
  2087. SCP2TARGET(scp), SCP2LUN(scp)));
  2088. // If FW has stopped responding, simply return failure
  2089. if (raid_dev->hw_error) {
  2090. con_log(CL_ANN, (KERN_NOTICE
  2091. "megaraid: hw error, not aborting\n"));
  2092. return FAILED;
  2093. }
  2094. // There might a race here, where the command was completed by the
  2095. // firmware and now it is on the completed list. Before we could
  2096. // complete the command to the kernel in dpc, the abort came.
  2097. // Find out if this is the case to avoid the race.
  2098. scb = NULL;
  2099. spin_lock_irqsave(COMPLETED_LIST_LOCK(adapter), flags);
  2100. list_for_each_entry_safe(scb, tmp, &adapter->completed_list, list) {
  2101. if (scb->scp == scp) { // Found command
  2102. list_del_init(&scb->list); // from completed list
  2103. con_log(CL_ANN, (KERN_WARNING
  2104. "megaraid: %ld:%d[%d:%d], abort from completed list\n",
  2105. scp->serial_number, scb->sno,
  2106. scb->dev_channel, scb->dev_target));
  2107. scp->result = (DID_ABORT << 16);
  2108. scp->scsi_done(scp);
  2109. megaraid_dealloc_scb(adapter, scb);
  2110. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter),
  2111. flags);
  2112. return SUCCESS;
  2113. }
  2114. }
  2115. spin_unlock_irqrestore(COMPLETED_LIST_LOCK(adapter), flags);
  2116. // Find out if this command is still on the pending list. If it is and
  2117. // was never issued, abort and return success. If the command is owned
  2118. // by the firmware, we must wait for it to complete by the FW.
  2119. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  2120. list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
  2121. if (scb->scp == scp) { // Found command
  2122. list_del_init(&scb->list); // from pending list
  2123. ASSERT(!(scb->state & SCB_ISSUED));
  2124. con_log(CL_ANN, (KERN_WARNING
  2125. "megaraid abort: %ld[%d:%d], driver owner\n",
  2126. scp->serial_number, scb->dev_channel,
  2127. scb->dev_target));
  2128. scp->result = (DID_ABORT << 16);
  2129. scp->scsi_done(scp);
  2130. megaraid_dealloc_scb(adapter, scb);
  2131. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter),
  2132. flags);
  2133. return SUCCESS;
  2134. }
  2135. }
  2136. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  2137. // Check do we even own this command, in which case this would be
  2138. // owned by the firmware. The only way to locate the FW scb is to
  2139. // traverse through the list of all SCB, since driver does not
  2140. // maintain these SCBs on any list
  2141. found = 0;
  2142. for (i = 0; i < MBOX_MAX_SCSI_CMDS; i++) {
  2143. scb = adapter->kscb_list + i;
  2144. if (scb->scp == scp) {
  2145. found = 1;
  2146. if (!(scb->state & SCB_ISSUED)) {
  2147. con_log(CL_ANN, (KERN_WARNING
  2148. "megaraid abort: %ld%d[%d:%d], invalid state\n",
  2149. scp->serial_number, scb->sno, scb->dev_channel,
  2150. scb->dev_target));
  2151. BUG();
  2152. }
  2153. else {
  2154. con_log(CL_ANN, (KERN_WARNING
  2155. "megaraid abort: %ld:%d[%d:%d], fw owner\n",
  2156. scp->serial_number, scb->sno, scb->dev_channel,
  2157. scb->dev_target));
  2158. }
  2159. }
  2160. }
  2161. if (!found) {
  2162. con_log(CL_ANN, (KERN_WARNING
  2163. "megaraid abort: scsi cmd:%ld, do now own\n",
  2164. scp->serial_number));
  2165. // FIXME: Should there be a callback for this command?
  2166. return SUCCESS;
  2167. }
  2168. // We cannot actually abort a command owned by firmware, return
  2169. // failure and wait for reset. In host reset handler, we will find out
  2170. // if the HBA is still live
  2171. return FAILED;
  2172. }
  2173. static int
  2174. megaraid_abort_handler(struct scsi_cmnd *scp)
  2175. {
  2176. adapter_t *adapter;
  2177. int rc;
  2178. adapter = SCP2ADAPTER(scp);
  2179. spin_lock_irq(adapter->host_lock);
  2180. rc = __megaraid_abort_handler(scp);
  2181. spin_unlock_irq(adapter->host_lock);
  2182. return rc;
  2183. }
  2184. /**
  2185. * megaraid_reset_handler - device reset hadler for mailbox based driver
  2186. * @scp : reference command
  2187. *
  2188. * Reset handler for the mailbox based controller. First try to find out if
  2189. * the FW is still live, in which case the outstanding commands counter mut go
  2190. * down to 0. If that happens, also issue the reservation reset command to
  2191. * relinquish (possible) reservations on the logical drives connected to this
  2192. * host
  2193. **/
  2194. static int
  2195. __megaraid_reset_handler(struct scsi_cmnd *scp)
  2196. {
  2197. adapter_t *adapter;
  2198. scb_t *scb;
  2199. scb_t *tmp;
  2200. mraid_device_t *raid_dev;
  2201. unsigned long flags;
  2202. uint8_t raw_mbox[sizeof(mbox_t)];
  2203. int rval;
  2204. int recovery_window;
  2205. int recovering;
  2206. int i;
  2207. adapter = SCP2ADAPTER(scp);
  2208. raid_dev = ADAP2RAIDDEV(adapter);
  2209. assert_spin_locked(adapter->host_lock);
  2210. con_log(CL_ANN, (KERN_WARNING "megaraid: reseting the host...\n"));
  2211. // return failure if adapter is not responding
  2212. if (raid_dev->hw_error) {
  2213. con_log(CL_ANN, (KERN_NOTICE
  2214. "megaraid: hw error, cannot reset\n"));
  2215. return FAILED;
  2216. }
  2217. // Under exceptional conditions, FW can take up to 3 minutes to
  2218. // complete command processing. Wait for additional 2 minutes for the
  2219. // pending commands counter to go down to 0. If it doesn't, let the
  2220. // controller be marked offline
  2221. // Also, reset all the commands currently owned by the driver
  2222. spin_lock_irqsave(PENDING_LIST_LOCK(adapter), flags);
  2223. list_for_each_entry_safe(scb, tmp, &adapter->pend_list, list) {
  2224. list_del_init(&scb->list); // from pending list
  2225. con_log(CL_ANN, (KERN_WARNING
  2226. "megaraid: %ld:%d[%d:%d], reset from pending list\n",
  2227. scp->serial_number, scb->sno,
  2228. scb->dev_channel, scb->dev_target));
  2229. scp->result = (DID_RESET << 16);
  2230. scp->scsi_done(scp);
  2231. megaraid_dealloc_scb(adapter, scb);
  2232. }
  2233. spin_unlock_irqrestore(PENDING_LIST_LOCK(adapter), flags);
  2234. if (adapter->outstanding_cmds) {
  2235. con_log(CL_ANN, (KERN_NOTICE
  2236. "megaraid: %d outstanding commands. Max wait %d sec\n",
  2237. adapter->outstanding_cmds, MBOX_RESET_WAIT));
  2238. }
  2239. spin_unlock(adapter->host_lock);
  2240. recovery_window = MBOX_RESET_WAIT + MBOX_RESET_EXT_WAIT;
  2241. recovering = adapter->outstanding_cmds;
  2242. for (i = 0; i < recovery_window && adapter->outstanding_cmds; i++) {
  2243. megaraid_ack_sequence(adapter);
  2244. // print a message once every 5 seconds only
  2245. if (!(i % 5)) {
  2246. con_log(CL_ANN, (
  2247. "megaraid mbox: Wait for %d commands to complete:%d\n",
  2248. adapter->outstanding_cmds,
  2249. MBOX_RESET_WAIT - i));
  2250. }
  2251. // bailout if no recovery happended in reset time
  2252. if ((i == MBOX_RESET_WAIT) &&
  2253. (recovering == adapter->outstanding_cmds)) {
  2254. break;
  2255. }
  2256. msleep(1000);
  2257. }
  2258. spin_lock(adapter->host_lock);
  2259. // If still outstanding commands, bail out
  2260. if (adapter->outstanding_cmds) {
  2261. con_log(CL_ANN, (KERN_WARNING
  2262. "megaraid mbox: critical hardware error!\n"));
  2263. raid_dev->hw_error = 1;
  2264. return FAILED;
  2265. }
  2266. else {
  2267. con_log(CL_ANN, (KERN_NOTICE
  2268. "megaraid mbox: reset sequence completed sucessfully\n"));
  2269. }
  2270. // If the controller supports clustering, reset reservations
  2271. if (!adapter->ha) return SUCCESS;
  2272. // clear reservations if any
  2273. raw_mbox[0] = CLUSTER_CMD;
  2274. raw_mbox[2] = RESET_RESERVATIONS;
  2275. rval = SUCCESS;
  2276. if (mbox_post_sync_cmd_fast(adapter, raw_mbox) == 0) {
  2277. con_log(CL_ANN,
  2278. (KERN_INFO "megaraid: reservation reset\n"));
  2279. }
  2280. else {
  2281. rval = FAILED;
  2282. con_log(CL_ANN, (KERN_WARNING
  2283. "megaraid: reservation reset failed\n"));
  2284. }
  2285. return rval;
  2286. }
  2287. static int
  2288. megaraid_reset_handler(struct scsi_cmnd *cmd)
  2289. {
  2290. int rc;
  2291. spin_lock_irq(cmd->device->host->host_lock);
  2292. rc = __megaraid_reset_handler(cmd);
  2293. spin_unlock_irq(cmd->device->host->host_lock);
  2294. return rc;
  2295. }
  2296. /*
  2297. * START: internal commands library
  2298. *
  2299. * This section of the driver has the common routine used by the driver and
  2300. * also has all the FW routines
  2301. */
  2302. /**
  2303. * mbox_post_sync_cmd() - blocking command to the mailbox based controllers
  2304. * @adapter - controller's soft state
  2305. * @raw_mbox - the mailbox
  2306. *
  2307. * Issue a scb in synchronous and non-interrupt mode for mailbox based
  2308. * controllers
  2309. */
  2310. static int
  2311. mbox_post_sync_cmd(adapter_t *adapter, uint8_t raw_mbox[])
  2312. {
  2313. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2314. mbox64_t *mbox64;
  2315. mbox_t *mbox;
  2316. uint8_t status;
  2317. int i;
  2318. mbox64 = raid_dev->mbox64;
  2319. mbox = raid_dev->mbox;
  2320. /*
  2321. * Wait until mailbox is free
  2322. */
  2323. if (megaraid_busywait_mbox(raid_dev) != 0)
  2324. goto blocked_mailbox;
  2325. /*
  2326. * Copy mailbox data into host structure
  2327. */
  2328. memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 16);
  2329. mbox->cmdid = 0xFE;
  2330. mbox->busy = 1;
  2331. mbox->poll = 0;
  2332. mbox->ack = 0;
  2333. mbox->numstatus = 0xFF;
  2334. mbox->status = 0xFF;
  2335. wmb();
  2336. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  2337. // wait for maximum 1 second for status to post. If the status is not
  2338. // available within 1 second, assume FW is initializing and wait
  2339. // for an extended amount of time
  2340. if (mbox->numstatus == 0xFF) { // status not yet available
  2341. udelay(25);;
  2342. for (i = 0; mbox->numstatus == 0xFF && i < 1000; i++) {
  2343. rmb();
  2344. msleep(1);
  2345. }
  2346. if (i == 1000) {
  2347. con_log(CL_ANN, (KERN_NOTICE
  2348. "megaraid mailbox: wait for FW to boot "));
  2349. for (i = 0; (mbox->numstatus == 0xFF) &&
  2350. (i < MBOX_RESET_WAIT); i++) {
  2351. rmb();
  2352. con_log(CL_ANN, ("\b\b\b\b\b[%03d]",
  2353. MBOX_RESET_WAIT - i));
  2354. msleep(1000);
  2355. }
  2356. if (i == MBOX_RESET_WAIT) {
  2357. con_log(CL_ANN, (
  2358. "\nmegaraid mailbox: status not available\n"));
  2359. return -1;
  2360. }
  2361. con_log(CL_ANN, ("\b\b\b\b\b[ok] \n"));
  2362. }
  2363. }
  2364. // wait for maximum 1 second for poll semaphore
  2365. if (mbox->poll != 0x77) {
  2366. udelay(25);
  2367. for (i = 0; (mbox->poll != 0x77) && (i < 1000); i++) {
  2368. rmb();
  2369. msleep(1);
  2370. }
  2371. if (i == 1000) {
  2372. con_log(CL_ANN, (KERN_WARNING
  2373. "megaraid mailbox: could not get poll semaphore\n"));
  2374. return -1;
  2375. }
  2376. }
  2377. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
  2378. wmb();
  2379. // wait for maximum 1 second for acknowledgement
  2380. if (RDINDOOR(raid_dev) & 0x2) {
  2381. udelay(25);
  2382. for (i = 0; (RDINDOOR(raid_dev) & 0x2) && (i < 1000); i++) {
  2383. rmb();
  2384. msleep(1);
  2385. }
  2386. if (i == 1000) {
  2387. con_log(CL_ANN, (KERN_WARNING
  2388. "megaraid mailbox: could not acknowledge\n"));
  2389. return -1;
  2390. }
  2391. }
  2392. mbox->poll = 0;
  2393. mbox->ack = 0x77;
  2394. status = mbox->status;
  2395. // invalidate the completed command id array. After command
  2396. // completion, firmware would write the valid id.
  2397. mbox->numstatus = 0xFF;
  2398. mbox->status = 0xFF;
  2399. for (i = 0; i < MBOX_MAX_FIRMWARE_STATUS; i++) {
  2400. mbox->completed[i] = 0xFF;
  2401. }
  2402. return status;
  2403. blocked_mailbox:
  2404. con_log(CL_ANN, (KERN_WARNING "megaraid: blocked mailbox\n") );
  2405. return -1;
  2406. }
  2407. /**
  2408. * mbox_post_sync_cmd_fast - blocking command to the mailbox based controllers
  2409. * @adapter - controller's soft state
  2410. * @raw_mbox - the mailbox
  2411. *
  2412. * Issue a scb in synchronous and non-interrupt mode for mailbox based
  2413. * controllers. This is a faster version of the synchronous command and
  2414. * therefore can be called in interrupt-context as well
  2415. */
  2416. static int
  2417. mbox_post_sync_cmd_fast(adapter_t *adapter, uint8_t raw_mbox[])
  2418. {
  2419. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2420. mbox_t *mbox;
  2421. long i;
  2422. mbox = raid_dev->mbox;
  2423. // return immediately if the mailbox is busy
  2424. if (mbox->busy) return -1;
  2425. // Copy mailbox data into host structure
  2426. memcpy((caddr_t)mbox, (caddr_t)raw_mbox, 14);
  2427. mbox->cmdid = 0xFE;
  2428. mbox->busy = 1;
  2429. mbox->poll = 0;
  2430. mbox->ack = 0;
  2431. mbox->numstatus = 0xFF;
  2432. mbox->status = 0xFF;
  2433. wmb();
  2434. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x1);
  2435. for (i = 0; i < 0xFFFFF; i++) {
  2436. if (mbox->numstatus != 0xFF) break;
  2437. }
  2438. if (i == 0xFFFFF) {
  2439. // We may need to re-calibrate the counter
  2440. con_log(CL_ANN, (KERN_CRIT
  2441. "megaraid: fast sync command timed out\n"));
  2442. }
  2443. WRINDOOR(raid_dev, raid_dev->mbox_dma | 0x2);
  2444. wmb();
  2445. return mbox->status;
  2446. }
  2447. /**
  2448. * megaraid_busywait_mbox() - Wait until the controller's mailbox is available
  2449. * @raid_dev - RAID device (HBA) soft state
  2450. *
  2451. * wait until the controller's mailbox is available to accept more commands.
  2452. * wait for at most 1 second
  2453. */
  2454. static int
  2455. megaraid_busywait_mbox(mraid_device_t *raid_dev)
  2456. {
  2457. mbox_t *mbox = raid_dev->mbox;
  2458. int i = 0;
  2459. if (mbox->busy) {
  2460. udelay(25);
  2461. for (i = 0; mbox->busy && i < 1000; i++)
  2462. msleep(1);
  2463. }
  2464. if (i < 1000) return 0;
  2465. else return -1;
  2466. }
  2467. /**
  2468. * megaraid_mbox_product_info - some static information about the controller
  2469. * @adapter - our soft state
  2470. *
  2471. * issue commands to the controller to grab some parameters required by our
  2472. * caller.
  2473. */
  2474. static int
  2475. megaraid_mbox_product_info(adapter_t *adapter)
  2476. {
  2477. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2478. mbox_t *mbox;
  2479. uint8_t raw_mbox[sizeof(mbox_t)];
  2480. mraid_pinfo_t *pinfo;
  2481. dma_addr_t pinfo_dma_h;
  2482. mraid_inquiry3_t *mraid_inq3;
  2483. int i;
  2484. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2485. mbox = (mbox_t *)raw_mbox;
  2486. /*
  2487. * Issue an ENQUIRY3 command to find out certain adapter parameters,
  2488. * e.g., max channels, max commands etc.
  2489. */
  2490. pinfo = pci_alloc_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
  2491. &pinfo_dma_h);
  2492. if (pinfo == NULL) {
  2493. con_log(CL_ANN, (KERN_WARNING
  2494. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  2495. __LINE__));
  2496. return -1;
  2497. }
  2498. memset(pinfo, 0, sizeof(mraid_pinfo_t));
  2499. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2500. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2501. raw_mbox[0] = FC_NEW_CONFIG;
  2502. raw_mbox[2] = NC_SUBOP_ENQUIRY3;
  2503. raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;
  2504. // Issue the command
  2505. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2506. con_log(CL_ANN, (KERN_WARNING "megaraid: Inquiry3 failed\n"));
  2507. pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
  2508. pinfo, pinfo_dma_h);
  2509. return -1;
  2510. }
  2511. /*
  2512. * Collect information about state of each physical drive
  2513. * attached to the controller. We will expose all the disks
  2514. * which are not part of RAID
  2515. */
  2516. mraid_inq3 = (mraid_inquiry3_t *)adapter->ibuf;
  2517. for (i = 0; i < MBOX_MAX_PHYSICAL_DRIVES; i++) {
  2518. raid_dev->pdrv_state[i] = mraid_inq3->pdrv_state[i];
  2519. }
  2520. /*
  2521. * Get product info for information like number of channels,
  2522. * maximum commands supported.
  2523. */
  2524. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2525. mbox->xferaddr = (uint32_t)pinfo_dma_h;
  2526. raw_mbox[0] = FC_NEW_CONFIG;
  2527. raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;
  2528. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2529. con_log(CL_ANN, (KERN_WARNING
  2530. "megaraid: product info failed\n"));
  2531. pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t),
  2532. pinfo, pinfo_dma_h);
  2533. return -1;
  2534. }
  2535. /*
  2536. * Setup some parameters for host, as required by our caller
  2537. */
  2538. adapter->max_channel = pinfo->nchannels;
  2539. /*
  2540. * we will export all the logical drives on a single channel.
  2541. * Add 1 since inquires do not come for inititor ID
  2542. */
  2543. adapter->max_target = MAX_LOGICAL_DRIVES_40LD + 1;
  2544. adapter->max_lun = 8; // up to 8 LUNs for non-disk devices
  2545. /*
  2546. * These are the maximum outstanding commands for the scsi-layer
  2547. */
  2548. adapter->max_cmds = MBOX_MAX_SCSI_CMDS;
  2549. memset(adapter->fw_version, 0, VERSION_SIZE);
  2550. memset(adapter->bios_version, 0, VERSION_SIZE);
  2551. memcpy(adapter->fw_version, pinfo->fw_version, 4);
  2552. adapter->fw_version[4] = 0;
  2553. memcpy(adapter->bios_version, pinfo->bios_version, 4);
  2554. adapter->bios_version[4] = 0;
  2555. con_log(CL_ANN, (KERN_NOTICE
  2556. "megaraid: fw version:[%s] bios version:[%s]\n",
  2557. adapter->fw_version, adapter->bios_version));
  2558. pci_free_consistent(adapter->pdev, sizeof(mraid_pinfo_t), pinfo,
  2559. pinfo_dma_h);
  2560. return 0;
  2561. }
  2562. /**
  2563. * megaraid_mbox_extended_cdb - check for support for extended CDBs
  2564. * @adapter - soft state for the controller
  2565. *
  2566. * this routine check whether the controller in question supports extended
  2567. * ( > 10 bytes ) CDBs
  2568. */
  2569. static int
  2570. megaraid_mbox_extended_cdb(adapter_t *adapter)
  2571. {
  2572. mbox_t *mbox;
  2573. uint8_t raw_mbox[sizeof(mbox_t)];
  2574. int rval;
  2575. mbox = (mbox_t *)raw_mbox;
  2576. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2577. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2578. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2579. raw_mbox[0] = MAIN_MISC_OPCODE;
  2580. raw_mbox[2] = SUPPORT_EXT_CDB;
  2581. /*
  2582. * Issue the command
  2583. */
  2584. rval = 0;
  2585. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2586. rval = -1;
  2587. }
  2588. return rval;
  2589. }
  2590. /**
  2591. * megaraid_mbox_support_ha - Do we support clustering
  2592. * @adapter - soft state for the controller
  2593. * @init_id - ID of the initiator
  2594. *
  2595. * Determine if the firmware supports clustering and the ID of the initiator.
  2596. */
  2597. static int
  2598. megaraid_mbox_support_ha(adapter_t *adapter, uint16_t *init_id)
  2599. {
  2600. mbox_t *mbox;
  2601. uint8_t raw_mbox[sizeof(mbox_t)];
  2602. int rval;
  2603. mbox = (mbox_t *)raw_mbox;
  2604. memset((caddr_t)raw_mbox, 0, sizeof(raw_mbox));
  2605. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2606. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2607. raw_mbox[0] = GET_TARGET_ID;
  2608. // Issue the command
  2609. *init_id = 7;
  2610. rval = -1;
  2611. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2612. *init_id = *(uint8_t *)adapter->ibuf;
  2613. con_log(CL_ANN, (KERN_INFO
  2614. "megaraid: cluster firmware, initiator ID: %d\n",
  2615. *init_id));
  2616. rval = 0;
  2617. }
  2618. return rval;
  2619. }
  2620. /**
  2621. * megaraid_mbox_support_random_del - Do we support random deletion
  2622. * @adapter - soft state for the controller
  2623. *
  2624. * Determine if the firmware supports random deletion
  2625. * Return: 1 is operation supported, 0 otherwise
  2626. */
  2627. static int
  2628. megaraid_mbox_support_random_del(adapter_t *adapter)
  2629. {
  2630. mbox_t *mbox;
  2631. uint8_t raw_mbox[sizeof(mbox_t)];
  2632. int rval;
  2633. mbox = (mbox_t *)raw_mbox;
  2634. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2635. raw_mbox[0] = FC_DEL_LOGDRV;
  2636. raw_mbox[2] = OP_SUP_DEL_LOGDRV;
  2637. // Issue the command
  2638. rval = 0;
  2639. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2640. con_log(CL_DLEVEL1, ("megaraid: supports random deletion\n"));
  2641. rval = 1;
  2642. }
  2643. return rval;
  2644. }
  2645. /**
  2646. * megaraid_mbox_get_max_sg - maximum sg elements supported by the firmware
  2647. * @adapter - soft state for the controller
  2648. *
  2649. * Find out the maximum number of scatter-gather elements supported by the
  2650. * firmware
  2651. */
  2652. static int
  2653. megaraid_mbox_get_max_sg(adapter_t *adapter)
  2654. {
  2655. mbox_t *mbox;
  2656. uint8_t raw_mbox[sizeof(mbox_t)];
  2657. int nsg;
  2658. mbox = (mbox_t *)raw_mbox;
  2659. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2660. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2661. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2662. raw_mbox[0] = MAIN_MISC_OPCODE;
  2663. raw_mbox[2] = GET_MAX_SG_SUPPORT;
  2664. // Issue the command
  2665. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2666. nsg = *(uint8_t *)adapter->ibuf;
  2667. }
  2668. else {
  2669. nsg = MBOX_DEFAULT_SG_SIZE;
  2670. }
  2671. if (nsg > MBOX_MAX_SG_SIZE) nsg = MBOX_MAX_SG_SIZE;
  2672. return nsg;
  2673. }
  2674. /**
  2675. * megaraid_mbox_enum_raid_scsi - enumerate the RAID and SCSI channels
  2676. * @adapter - soft state for the controller
  2677. *
  2678. * Enumerate the RAID and SCSI channels for ROMB platoforms so that channels
  2679. * can be exported as regular SCSI channels
  2680. */
  2681. static void
  2682. megaraid_mbox_enum_raid_scsi(adapter_t *adapter)
  2683. {
  2684. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2685. mbox_t *mbox;
  2686. uint8_t raw_mbox[sizeof(mbox_t)];
  2687. mbox = (mbox_t *)raw_mbox;
  2688. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2689. mbox->xferaddr = (uint32_t)adapter->ibuf_dma_h;
  2690. memset((void *)adapter->ibuf, 0, MBOX_IBUF_SIZE);
  2691. raw_mbox[0] = CHNL_CLASS;
  2692. raw_mbox[2] = GET_CHNL_CLASS;
  2693. // Issue the command. If the command fails, all channels are RAID
  2694. // channels
  2695. raid_dev->channel_class = 0xFF;
  2696. if (mbox_post_sync_cmd(adapter, raw_mbox) == 0) {
  2697. raid_dev->channel_class = *(uint8_t *)adapter->ibuf;
  2698. }
  2699. return;
  2700. }
  2701. /**
  2702. * megaraid_mbox_flush_cache - flush adapter and disks cache
  2703. * @param adapter : soft state for the controller
  2704. *
  2705. * Flush adapter cache followed by disks cache
  2706. */
  2707. static void
  2708. megaraid_mbox_flush_cache(adapter_t *adapter)
  2709. {
  2710. mbox_t *mbox;
  2711. uint8_t raw_mbox[sizeof(mbox_t)];
  2712. mbox = (mbox_t *)raw_mbox;
  2713. memset((caddr_t)raw_mbox, 0, sizeof(mbox_t));
  2714. raw_mbox[0] = FLUSH_ADAPTER;
  2715. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2716. con_log(CL_ANN, ("megaraid: flush adapter failed\n"));
  2717. }
  2718. raw_mbox[0] = FLUSH_SYSTEM;
  2719. if (mbox_post_sync_cmd(adapter, raw_mbox) != 0) {
  2720. con_log(CL_ANN, ("megaraid: flush disks cache failed\n"));
  2721. }
  2722. return;
  2723. }
  2724. /**
  2725. * megaraid_mbox_display_scb - display SCB information, mostly debug purposes
  2726. * @param adapter : controllers' soft state
  2727. * @param scb : SCB to be displayed
  2728. * @param level : debug level for console print
  2729. *
  2730. * Diplay information about the given SCB iff the current debug level is
  2731. * verbose
  2732. */
  2733. static void
  2734. megaraid_mbox_display_scb(adapter_t *adapter, scb_t *scb)
  2735. {
  2736. mbox_ccb_t *ccb;
  2737. struct scsi_cmnd *scp;
  2738. mbox_t *mbox;
  2739. int level;
  2740. int i;
  2741. ccb = (mbox_ccb_t *)scb->ccb;
  2742. scp = scb->scp;
  2743. mbox = ccb->mbox;
  2744. level = CL_DLEVEL3;
  2745. con_log(level, (KERN_NOTICE
  2746. "megaraid mailbox: status:%#x cmd:%#x id:%#x ", scb->status,
  2747. mbox->cmd, scb->sno));
  2748. con_log(level, ("sec:%#x lba:%#x addr:%#x ld:%d sg:%d\n",
  2749. mbox->numsectors, mbox->lba, mbox->xferaddr, mbox->logdrv,
  2750. mbox->numsge));
  2751. if (!scp) return;
  2752. con_log(level, (KERN_NOTICE "scsi cmnd: "));
  2753. for (i = 0; i < scp->cmd_len; i++) {
  2754. con_log(level, ("%#2.02x ", scp->cmnd[i]));
  2755. }
  2756. con_log(level, ("\n"));
  2757. return;
  2758. }
  2759. /**
  2760. * megaraid_mbox_setup_device_map - manage device ids
  2761. * @adapter : Driver's soft state
  2762. *
  2763. * Manange the device ids to have an appropraite mapping between the kernel
  2764. * scsi addresses and megaraid scsi and logical drive addresses. We export
  2765. * scsi devices on their actual addresses, whereas the logical drives are
  2766. * exported on a virtual scsi channel.
  2767. **/
  2768. static void
  2769. megaraid_mbox_setup_device_map(adapter_t *adapter)
  2770. {
  2771. uint8_t c;
  2772. uint8_t t;
  2773. /*
  2774. * First fill the values on the logical drive channel
  2775. */
  2776. for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
  2777. adapter->device_ids[adapter->max_channel][t] =
  2778. (t < adapter->init_id) ? t : t - 1;
  2779. adapter->device_ids[adapter->max_channel][adapter->init_id] = 0xFF;
  2780. /*
  2781. * Fill the values on the physical devices channels
  2782. */
  2783. for (c = 0; c < adapter->max_channel; c++)
  2784. for (t = 0; t < LSI_MAX_LOGICAL_DRIVES_64LD; t++)
  2785. adapter->device_ids[c][t] = (c << 8) | t;
  2786. }
  2787. /*
  2788. * END: internal commands library
  2789. */
  2790. /*
  2791. * START: Interface for the common management module
  2792. *
  2793. * This is the module, which interfaces with the common mangement module to
  2794. * provide support for ioctl and sysfs
  2795. */
  2796. /**
  2797. * megaraid_cmm_register - register with the mangement module
  2798. * @param adapter : HBA soft state
  2799. *
  2800. * Register with the management module, which allows applications to issue
  2801. * ioctl calls to the drivers. This interface is used by the management module
  2802. * to setup sysfs support as well.
  2803. */
  2804. static int
  2805. megaraid_cmm_register(adapter_t *adapter)
  2806. {
  2807. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  2808. mraid_mmadp_t adp;
  2809. scb_t *scb;
  2810. mbox_ccb_t *ccb;
  2811. int rval;
  2812. int i;
  2813. // Allocate memory for the base list of scb for management module.
  2814. adapter->uscb_list = kmalloc(sizeof(scb_t) * MBOX_MAX_USER_CMDS,
  2815. GFP_KERNEL);
  2816. if (adapter->uscb_list == NULL) {
  2817. con_log(CL_ANN, (KERN_WARNING
  2818. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  2819. __LINE__));
  2820. return -1;
  2821. }
  2822. memset(adapter->uscb_list, 0, sizeof(scb_t) * MBOX_MAX_USER_CMDS);
  2823. // Initialize the synchronization parameters for resources for
  2824. // commands for management module
  2825. INIT_LIST_HEAD(&adapter->uscb_pool);
  2826. spin_lock_init(USER_FREE_LIST_LOCK(adapter));
  2827. // link all the packets. Note, CCB for commands, coming from the
  2828. // commom management module, mailbox physical address are already
  2829. // setup by it. We just need placeholder for that in our local command
  2830. // control blocks
  2831. for (i = 0; i < MBOX_MAX_USER_CMDS; i++) {
  2832. scb = adapter->uscb_list + i;
  2833. ccb = raid_dev->uccb_list + i;
  2834. scb->ccb = (caddr_t)ccb;
  2835. ccb->mbox64 = raid_dev->umbox64 + i;
  2836. ccb->mbox = &ccb->mbox64->mbox32;
  2837. ccb->raw_mbox = (uint8_t *)ccb->mbox;
  2838. scb->gp = 0;
  2839. // COMMAND ID 0 - (MBOX_MAX_SCSI_CMDS-1) ARE RESERVED FOR
  2840. // COMMANDS COMING FROM IO SUBSYSTEM (MID-LAYER)
  2841. scb->sno = i + MBOX_MAX_SCSI_CMDS;
  2842. scb->scp = NULL;
  2843. scb->state = SCB_FREE;
  2844. scb->dma_direction = PCI_DMA_NONE;
  2845. scb->dma_type = MRAID_DMA_NONE;
  2846. scb->dev_channel = -1;
  2847. scb->dev_target = -1;
  2848. // put scb in the free pool
  2849. list_add_tail(&scb->list, &adapter->uscb_pool);
  2850. }
  2851. adp.unique_id = adapter->unique_id;
  2852. adp.drvr_type = DRVRTYPE_MBOX;
  2853. adp.drvr_data = (unsigned long)adapter;
  2854. adp.pdev = adapter->pdev;
  2855. adp.issue_uioc = megaraid_mbox_mm_handler;
  2856. adp.timeout = 300;
  2857. adp.max_kioc = MBOX_MAX_USER_CMDS;
  2858. if ((rval = mraid_mm_register_adp(&adp)) != 0) {
  2859. con_log(CL_ANN, (KERN_WARNING
  2860. "megaraid mbox: did not register with CMM\n"));
  2861. kfree(adapter->uscb_list);
  2862. }
  2863. return rval;
  2864. }
  2865. /**
  2866. * megaraid_cmm_unregister - un-register with the mangement module
  2867. * @param adapter : HBA soft state
  2868. *
  2869. * Un-register with the management module.
  2870. * FIXME: mgmt module must return failure for unregister if it has pending
  2871. * commands in LLD
  2872. */
  2873. static int
  2874. megaraid_cmm_unregister(adapter_t *adapter)
  2875. {
  2876. kfree(adapter->uscb_list);
  2877. mraid_mm_unregister_adp(adapter->unique_id);
  2878. return 0;
  2879. }
  2880. /**
  2881. * megaraid_mbox_mm_handler - interface for CMM to issue commands to LLD
  2882. * @param drvr_data : LLD specific data
  2883. * @param kioc : CMM interface packet
  2884. * @param action : command action
  2885. *
  2886. * This routine is invoked whenever the Common Mangement Module (CMM) has a
  2887. * command for us. The 'action' parameter specifies if this is a new command
  2888. * or otherwise.
  2889. */
  2890. static int
  2891. megaraid_mbox_mm_handler(unsigned long drvr_data, uioc_t *kioc, uint32_t action)
  2892. {
  2893. adapter_t *adapter;
  2894. if (action != IOCTL_ISSUE) {
  2895. con_log(CL_ANN, (KERN_WARNING
  2896. "megaraid: unsupported management action:%#2x\n",
  2897. action));
  2898. return (-ENOTSUPP);
  2899. }
  2900. adapter = (adapter_t *)drvr_data;
  2901. // make sure this adapter is not being detached right now.
  2902. if (atomic_read(&adapter->being_detached)) {
  2903. con_log(CL_ANN, (KERN_WARNING
  2904. "megaraid: reject management request, detaching\n"));
  2905. return (-ENODEV);
  2906. }
  2907. switch (kioc->opcode) {
  2908. case GET_ADAP_INFO:
  2909. kioc->status = gather_hbainfo(adapter, (mraid_hba_info_t *)
  2910. (unsigned long)kioc->buf_vaddr);
  2911. kioc->done(kioc);
  2912. return kioc->status;
  2913. case MBOX_CMD:
  2914. return megaraid_mbox_mm_command(adapter, kioc);
  2915. default:
  2916. kioc->status = (-EINVAL);
  2917. kioc->done(kioc);
  2918. return (-EINVAL);
  2919. }
  2920. return 0; // not reached
  2921. }
  2922. /**
  2923. * megaraid_mbox_mm_command - issues commands routed through CMM
  2924. * @param adapter : HBA soft state
  2925. * @param kioc : management command packet
  2926. *
  2927. * Issues commands, which are routed through the management module.
  2928. */
  2929. static int
  2930. megaraid_mbox_mm_command(adapter_t *adapter, uioc_t *kioc)
  2931. {
  2932. struct list_head *head = &adapter->uscb_pool;
  2933. mbox64_t *mbox64;
  2934. uint8_t *raw_mbox;
  2935. scb_t *scb;
  2936. mbox_ccb_t *ccb;
  2937. unsigned long flags;
  2938. // detach one scb from free pool
  2939. spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
  2940. if (list_empty(head)) { // should never happen because of CMM
  2941. con_log(CL_ANN, (KERN_WARNING
  2942. "megaraid mbox: bug in cmm handler, lost resources\n"));
  2943. spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
  2944. return (-EINVAL);
  2945. }
  2946. scb = list_entry(head->next, scb_t, list);
  2947. list_del_init(&scb->list);
  2948. spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
  2949. scb->state = SCB_ACTIVE;
  2950. scb->dma_type = MRAID_DMA_NONE;
  2951. scb->dma_direction = PCI_DMA_NONE;
  2952. ccb = (mbox_ccb_t *)scb->ccb;
  2953. mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
  2954. raw_mbox = (uint8_t *)&mbox64->mbox32;
  2955. memcpy(ccb->mbox64, mbox64, sizeof(mbox64_t));
  2956. scb->gp = (unsigned long)kioc;
  2957. /*
  2958. * If it is a logdrv random delete operation, we have to wait till
  2959. * there are no outstanding cmds at the fw and then issue it directly
  2960. */
  2961. if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
  2962. if (wait_till_fw_empty(adapter)) {
  2963. con_log(CL_ANN, (KERN_NOTICE
  2964. "megaraid mbox: LD delete, timed out\n"));
  2965. kioc->status = -ETIME;
  2966. scb->status = -1;
  2967. megaraid_mbox_mm_done(adapter, scb);
  2968. return (-ETIME);
  2969. }
  2970. INIT_LIST_HEAD(&scb->list);
  2971. scb->state = SCB_ISSUED;
  2972. if (mbox_post_cmd(adapter, scb) != 0) {
  2973. con_log(CL_ANN, (KERN_NOTICE
  2974. "megaraid mbox: LD delete, mailbox busy\n"));
  2975. kioc->status = -EBUSY;
  2976. scb->status = -1;
  2977. megaraid_mbox_mm_done(adapter, scb);
  2978. return (-EBUSY);
  2979. }
  2980. return 0;
  2981. }
  2982. // put the command on the pending list and execute
  2983. megaraid_mbox_runpendq(adapter, scb);
  2984. return 0;
  2985. }
  2986. static int
  2987. wait_till_fw_empty(adapter_t *adapter)
  2988. {
  2989. unsigned long flags = 0;
  2990. int i;
  2991. /*
  2992. * Set the quiescent flag to stop issuing cmds to FW.
  2993. */
  2994. spin_lock_irqsave(adapter->host_lock, flags);
  2995. adapter->quiescent++;
  2996. spin_unlock_irqrestore(adapter->host_lock, flags);
  2997. /*
  2998. * Wait till there are no more cmds outstanding at FW. Try for at most
  2999. * 60 seconds
  3000. */
  3001. for (i = 0; i < 60 && adapter->outstanding_cmds; i++) {
  3002. con_log(CL_DLEVEL1, (KERN_INFO
  3003. "megaraid: FW has %d pending commands\n",
  3004. adapter->outstanding_cmds));
  3005. msleep(1000);
  3006. }
  3007. return adapter->outstanding_cmds;
  3008. }
  3009. /**
  3010. * megaraid_mbox_mm_done - callback for CMM commands
  3011. * @adapter : HBA soft state
  3012. * @scb : completed command
  3013. *
  3014. * Callback routine for internal commands originated from the management
  3015. * module.
  3016. */
  3017. static void
  3018. megaraid_mbox_mm_done(adapter_t *adapter, scb_t *scb)
  3019. {
  3020. uioc_t *kioc;
  3021. mbox64_t *mbox64;
  3022. uint8_t *raw_mbox;
  3023. unsigned long flags;
  3024. kioc = (uioc_t *)scb->gp;
  3025. kioc->status = 0;
  3026. mbox64 = (mbox64_t *)(unsigned long)kioc->cmdbuf;
  3027. mbox64->mbox32.status = scb->status;
  3028. raw_mbox = (uint8_t *)&mbox64->mbox32;
  3029. // put scb in the free pool
  3030. scb->state = SCB_FREE;
  3031. scb->scp = NULL;
  3032. spin_lock_irqsave(USER_FREE_LIST_LOCK(adapter), flags);
  3033. list_add(&scb->list, &adapter->uscb_pool);
  3034. spin_unlock_irqrestore(USER_FREE_LIST_LOCK(adapter), flags);
  3035. // if a delete logical drive operation succeeded, restart the
  3036. // controller
  3037. if (raw_mbox[0] == FC_DEL_LOGDRV && raw_mbox[2] == OP_DEL_LOGDRV) {
  3038. adapter->quiescent--;
  3039. megaraid_mbox_runpendq(adapter, NULL);
  3040. }
  3041. kioc->done(kioc);
  3042. return;
  3043. }
  3044. /**
  3045. * gather_hbainfo - HBA characteristics for the applications
  3046. * @param adapter : HBA soft state
  3047. * @param hinfo : pointer to the caller's host info strucuture
  3048. */
  3049. static int
  3050. gather_hbainfo(adapter_t *adapter, mraid_hba_info_t *hinfo)
  3051. {
  3052. uint8_t dmajor;
  3053. dmajor = megaraid_mbox_version[0];
  3054. hinfo->pci_vendor_id = adapter->pdev->vendor;
  3055. hinfo->pci_device_id = adapter->pdev->device;
  3056. hinfo->subsys_vendor_id = adapter->pdev->subsystem_vendor;
  3057. hinfo->subsys_device_id = adapter->pdev->subsystem_device;
  3058. hinfo->pci_bus = adapter->pdev->bus->number;
  3059. hinfo->pci_dev_fn = adapter->pdev->devfn;
  3060. hinfo->pci_slot = PCI_SLOT(adapter->pdev->devfn);
  3061. hinfo->irq = adapter->host->irq;
  3062. hinfo->baseport = ADAP2RAIDDEV(adapter)->baseport;
  3063. hinfo->unique_id = (hinfo->pci_bus << 8) | adapter->pdev->devfn;
  3064. hinfo->host_no = adapter->host->host_no;
  3065. return 0;
  3066. }
  3067. /*
  3068. * END: Interface for the common management module
  3069. */
  3070. /**
  3071. * megaraid_sysfs_alloc_resources - allocate sysfs related resources
  3072. *
  3073. * Allocate packets required to issue FW calls whenever the sysfs attributes
  3074. * are read. These attributes would require up-to-date information from the
  3075. * FW. Also set up resources for mutual exclusion to share these resources and
  3076. * the wait queue.
  3077. *
  3078. * @param adapter : controller's soft state
  3079. *
  3080. * @return 0 on success
  3081. * @return -ERROR_CODE on failure
  3082. */
  3083. static int
  3084. megaraid_sysfs_alloc_resources(adapter_t *adapter)
  3085. {
  3086. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3087. int rval = 0;
  3088. raid_dev->sysfs_uioc = kmalloc(sizeof(uioc_t), GFP_KERNEL);
  3089. raid_dev->sysfs_mbox64 = kmalloc(sizeof(mbox64_t), GFP_KERNEL);
  3090. raid_dev->sysfs_buffer = pci_alloc_consistent(adapter->pdev,
  3091. PAGE_SIZE, &raid_dev->sysfs_buffer_dma);
  3092. if (!raid_dev->sysfs_uioc || !raid_dev->sysfs_mbox64 ||
  3093. !raid_dev->sysfs_buffer) {
  3094. con_log(CL_ANN, (KERN_WARNING
  3095. "megaraid: out of memory, %s %d\n", __FUNCTION__,
  3096. __LINE__));
  3097. rval = -ENOMEM;
  3098. megaraid_sysfs_free_resources(adapter);
  3099. }
  3100. sema_init(&raid_dev->sysfs_sem, 1);
  3101. init_waitqueue_head(&raid_dev->sysfs_wait_q);
  3102. return rval;
  3103. }
  3104. /**
  3105. * megaraid_sysfs_free_resources - free sysfs related resources
  3106. *
  3107. * Free packets allocated for sysfs FW commands
  3108. *
  3109. * @param adapter : controller's soft state
  3110. */
  3111. static void
  3112. megaraid_sysfs_free_resources(adapter_t *adapter)
  3113. {
  3114. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3115. if (raid_dev->sysfs_uioc) kfree(raid_dev->sysfs_uioc);
  3116. if (raid_dev->sysfs_mbox64) kfree(raid_dev->sysfs_mbox64);
  3117. if (raid_dev->sysfs_buffer) {
  3118. pci_free_consistent(adapter->pdev, PAGE_SIZE,
  3119. raid_dev->sysfs_buffer, raid_dev->sysfs_buffer_dma);
  3120. }
  3121. }
  3122. /**
  3123. * megaraid_sysfs_get_ldmap_done - callback for get ldmap
  3124. *
  3125. * Callback routine called in the ISR/tasklet context for get ldmap call
  3126. *
  3127. * @param uioc : completed packet
  3128. */
  3129. static void
  3130. megaraid_sysfs_get_ldmap_done(uioc_t *uioc)
  3131. {
  3132. adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
  3133. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3134. uioc->status = 0;
  3135. wake_up(&raid_dev->sysfs_wait_q);
  3136. }
  3137. /**
  3138. * megaraid_sysfs_get_ldmap_timeout - timeout handling for get ldmap
  3139. *
  3140. * Timeout routine to recover and return to application, in case the adapter
  3141. * has stopped responding. A timeout of 60 seconds for this command seem like
  3142. * a good value
  3143. *
  3144. * @param uioc : timed out packet
  3145. */
  3146. static void
  3147. megaraid_sysfs_get_ldmap_timeout(unsigned long data)
  3148. {
  3149. uioc_t *uioc = (uioc_t *)data;
  3150. adapter_t *adapter = (adapter_t *)uioc->buf_vaddr;
  3151. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3152. uioc->status = -ETIME;
  3153. wake_up(&raid_dev->sysfs_wait_q);
  3154. }
  3155. /**
  3156. * megaraid_sysfs_get_ldmap - get update logical drive map
  3157. *
  3158. * This routine will be called whenever user reads the logical drive
  3159. * attributes, go get the current logical drive mapping table from the
  3160. * firmware. We use the managment API's to issue commands to the controller.
  3161. *
  3162. * NOTE: The commands issuance functionality is not generalized and
  3163. * implemented in context of "get ld map" command only. If required, the
  3164. * command issuance logical can be trivially pulled out and implemented as a
  3165. * standalone libary. For now, this should suffice since there is no other
  3166. * user of this interface.
  3167. *
  3168. * @param adapter : controller's soft state
  3169. *
  3170. * @return 0 on success
  3171. * @return -1 on failure
  3172. */
  3173. static int
  3174. megaraid_sysfs_get_ldmap(adapter_t *adapter)
  3175. {
  3176. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3177. uioc_t *uioc;
  3178. mbox64_t *mbox64;
  3179. mbox_t *mbox;
  3180. char *raw_mbox;
  3181. struct timer_list sysfs_timer;
  3182. struct timer_list *timerp;
  3183. caddr_t ldmap;
  3184. int rval = 0;
  3185. /*
  3186. * Allow only one read at a time to go through the sysfs attributes
  3187. */
  3188. down(&raid_dev->sysfs_sem);
  3189. uioc = raid_dev->sysfs_uioc;
  3190. mbox64 = raid_dev->sysfs_mbox64;
  3191. ldmap = raid_dev->sysfs_buffer;
  3192. memset(uioc, 0, sizeof(uioc_t));
  3193. memset(mbox64, 0, sizeof(mbox64_t));
  3194. memset(ldmap, 0, sizeof(raid_dev->curr_ldmap));
  3195. mbox = &mbox64->mbox32;
  3196. raw_mbox = (char *)mbox;
  3197. uioc->cmdbuf = (uint64_t)(unsigned long)mbox64;
  3198. uioc->buf_vaddr = (caddr_t)adapter;
  3199. uioc->status = -ENODATA;
  3200. uioc->done = megaraid_sysfs_get_ldmap_done;
  3201. /*
  3202. * Prepare the mailbox packet to get the current logical drive mapping
  3203. * table
  3204. */
  3205. mbox->xferaddr = (uint32_t)raid_dev->sysfs_buffer_dma;
  3206. raw_mbox[0] = FC_DEL_LOGDRV;
  3207. raw_mbox[2] = OP_GET_LDID_MAP;
  3208. /*
  3209. * Setup a timer to recover from a non-responding controller
  3210. */
  3211. timerp = &sysfs_timer;
  3212. init_timer(timerp);
  3213. timerp->function = megaraid_sysfs_get_ldmap_timeout;
  3214. timerp->data = (unsigned long)uioc;
  3215. timerp->expires = jiffies + 60 * HZ;
  3216. add_timer(timerp);
  3217. /*
  3218. * Send the command to the firmware
  3219. */
  3220. rval = megaraid_mbox_mm_command(adapter, uioc);
  3221. if (rval == 0) { // command successfully issued
  3222. wait_event(raid_dev->sysfs_wait_q, (uioc->status != -ENODATA));
  3223. /*
  3224. * Check if the command timed out
  3225. */
  3226. if (uioc->status == -ETIME) {
  3227. con_log(CL_ANN, (KERN_NOTICE
  3228. "megaraid: sysfs get ld map timed out\n"));
  3229. rval = -ETIME;
  3230. }
  3231. else {
  3232. rval = mbox->status;
  3233. }
  3234. if (rval == 0) {
  3235. memcpy(raid_dev->curr_ldmap, ldmap,
  3236. sizeof(raid_dev->curr_ldmap));
  3237. }
  3238. else {
  3239. con_log(CL_ANN, (KERN_NOTICE
  3240. "megaraid: get ld map failed with %x\n", rval));
  3241. }
  3242. }
  3243. else {
  3244. con_log(CL_ANN, (KERN_NOTICE
  3245. "megaraid: could not issue ldmap command:%x\n", rval));
  3246. }
  3247. del_timer_sync(timerp);
  3248. up(&raid_dev->sysfs_sem);
  3249. return rval;
  3250. }
  3251. /**
  3252. * megaraid_sysfs_show_app_hndl - display application handle for this adapter
  3253. *
  3254. * Display the handle used by the applications while executing management
  3255. * tasks on the adapter. We invoke a management module API to get the adapter
  3256. * handle, since we do not interface with applications directly.
  3257. *
  3258. * @param cdev : class device object representation for the host
  3259. * @param buf : buffer to send data to
  3260. */
  3261. static ssize_t
  3262. megaraid_sysfs_show_app_hndl(struct class_device *cdev, char *buf)
  3263. {
  3264. struct Scsi_Host *shost = class_to_shost(cdev);
  3265. adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(shost);
  3266. uint32_t app_hndl;
  3267. app_hndl = mraid_mm_adapter_app_handle(adapter->unique_id);
  3268. return snprintf(buf, 8, "%u\n", app_hndl);
  3269. }
  3270. /**
  3271. * megaraid_sysfs_show_ldnum - display the logical drive number for this device
  3272. *
  3273. * Display the logical drive number for the device in question, if it a valid
  3274. * logical drive. For physical devices, "-1" is returned
  3275. * The logical drive number is displayed in following format
  3276. *
  3277. * <SCSI ID> <LD NUM> <LD STICKY ID> <APP ADAPTER HANDLE>
  3278. * <int> <int> <int> <int>
  3279. *
  3280. * @param dev : device object representation for the scsi device
  3281. * @param buf : buffer to send data to
  3282. */
  3283. static ssize_t
  3284. megaraid_sysfs_show_ldnum(struct device *dev, struct device_attribute *attr, char *buf)
  3285. {
  3286. struct scsi_device *sdev = to_scsi_device(dev);
  3287. adapter_t *adapter = (adapter_t *)SCSIHOST2ADAP(sdev->host);
  3288. mraid_device_t *raid_dev = ADAP2RAIDDEV(adapter);
  3289. int scsi_id = -1;
  3290. int logical_drv = -1;
  3291. int ldid_map = -1;
  3292. uint32_t app_hndl = 0;
  3293. int mapped_sdev_id;
  3294. int rval;
  3295. int i;
  3296. if (raid_dev->random_del_supported &&
  3297. MRAID_IS_LOGICAL_SDEV(adapter, sdev)) {
  3298. rval = megaraid_sysfs_get_ldmap(adapter);
  3299. if (rval == 0) {
  3300. for (i = 0; i < MAX_LOGICAL_DRIVES_40LD; i++) {
  3301. mapped_sdev_id = sdev->id;
  3302. if (sdev->id > adapter->init_id) {
  3303. mapped_sdev_id -= 1;
  3304. }
  3305. if (raid_dev->curr_ldmap[i] == mapped_sdev_id) {
  3306. scsi_id = sdev->id;
  3307. logical_drv = i;
  3308. ldid_map = raid_dev->curr_ldmap[i];
  3309. app_hndl = mraid_mm_adapter_app_handle(
  3310. adapter->unique_id);
  3311. break;
  3312. }
  3313. }
  3314. }
  3315. else {
  3316. con_log(CL_ANN, (KERN_NOTICE
  3317. "megaraid: sysfs get ld map failed: %x\n",
  3318. rval));
  3319. }
  3320. }
  3321. return snprintf(buf, 36, "%d %d %d %d\n", scsi_id, logical_drv,
  3322. ldid_map, app_hndl);
  3323. }
  3324. /*
  3325. * END: Mailbox Low Level Driver
  3326. */
  3327. module_init(megaraid_init);
  3328. module_exit(megaraid_exit);
  3329. /* vim: set ts=8 sw=8 tw=78 ai si: */