megaraid_mbox.c 99 KB

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