megaraid_mbox.c 101 KB

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