megaraid_mbox.c 100 KB

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