megaraid_sas.c 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412
  1. /*
  2. *
  3. * Linux MegaRAID driver for SAS based RAID controllers
  4. *
  5. * Copyright (c) 2003-2005 LSI 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_sas.c
  13. * Version : v00.00.04.17.1-rc1
  14. *
  15. * Authors:
  16. * (email-id : megaraidlinux@lsi.com)
  17. * Sreenivas Bagalkote
  18. * Sumant Patro
  19. * Bo Yang
  20. *
  21. * List of supported controllers
  22. *
  23. * OEM Product Name VID DID SSVID SSID
  24. * --- ------------ --- --- ---- ----
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/types.h>
  28. #include <linux/pci.h>
  29. #include <linux/list.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/module.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/delay.h>
  35. #include <linux/uio.h>
  36. #include <linux/slab.h>
  37. #include <asm/uaccess.h>
  38. #include <linux/fs.h>
  39. #include <linux/compat.h>
  40. #include <linux/blkdev.h>
  41. #include <linux/mutex.h>
  42. #include <linux/poll.h>
  43. #include <scsi/scsi.h>
  44. #include <scsi/scsi_cmnd.h>
  45. #include <scsi/scsi_device.h>
  46. #include <scsi/scsi_host.h>
  47. #include "megaraid_sas.h"
  48. /*
  49. * poll_mode_io:1- schedule complete completion from q cmd
  50. */
  51. static unsigned int poll_mode_io;
  52. module_param_named(poll_mode_io, poll_mode_io, int, 0);
  53. MODULE_PARM_DESC(poll_mode_io,
  54. "Complete cmds from IO path, (default=0)");
  55. MODULE_LICENSE("GPL");
  56. MODULE_VERSION(MEGASAS_VERSION);
  57. MODULE_AUTHOR("megaraidlinux@lsi.com");
  58. MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
  59. /*
  60. * PCI ID table for all supported controllers
  61. */
  62. static struct pci_device_id megasas_pci_table[] = {
  63. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
  64. /* xscale IOP */
  65. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
  66. /* ppc IOP */
  67. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
  68. /* ppc IOP */
  69. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
  70. /* gen2*/
  71. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
  72. /* gen2*/
  73. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
  74. /* skinny*/
  75. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
  76. /* skinny*/
  77. {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
  78. /* xscale IOP, vega */
  79. {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
  80. /* xscale IOP */
  81. {}
  82. };
  83. MODULE_DEVICE_TABLE(pci, megasas_pci_table);
  84. static int megasas_mgmt_majorno;
  85. static struct megasas_mgmt_info megasas_mgmt_info;
  86. static struct fasync_struct *megasas_async_queue;
  87. static DEFINE_MUTEX(megasas_async_queue_mutex);
  88. static int megasas_poll_wait_aen;
  89. static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
  90. static u32 support_poll_for_event;
  91. static u32 megasas_dbg_lvl;
  92. /* define lock for aen poll */
  93. spinlock_t poll_aen_lock;
  94. static void
  95. megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
  96. u8 alt_status);
  97. /**
  98. * megasas_get_cmd - Get a command from the free pool
  99. * @instance: Adapter soft state
  100. *
  101. * Returns a free command from the pool
  102. */
  103. static struct megasas_cmd *megasas_get_cmd(struct megasas_instance
  104. *instance)
  105. {
  106. unsigned long flags;
  107. struct megasas_cmd *cmd = NULL;
  108. spin_lock_irqsave(&instance->cmd_pool_lock, flags);
  109. if (!list_empty(&instance->cmd_pool)) {
  110. cmd = list_entry((&instance->cmd_pool)->next,
  111. struct megasas_cmd, list);
  112. list_del_init(&cmd->list);
  113. } else {
  114. printk(KERN_ERR "megasas: Command pool empty!\n");
  115. }
  116. spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
  117. return cmd;
  118. }
  119. /**
  120. * megasas_return_cmd - Return a cmd to free command pool
  121. * @instance: Adapter soft state
  122. * @cmd: Command packet to be returned to free command pool
  123. */
  124. static inline void
  125. megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
  126. {
  127. unsigned long flags;
  128. spin_lock_irqsave(&instance->cmd_pool_lock, flags);
  129. cmd->scmd = NULL;
  130. list_add_tail(&cmd->list, &instance->cmd_pool);
  131. spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
  132. }
  133. /**
  134. * The following functions are defined for xscale
  135. * (deviceid : 1064R, PERC5) controllers
  136. */
  137. /**
  138. * megasas_enable_intr_xscale - Enables interrupts
  139. * @regs: MFI register set
  140. */
  141. static inline void
  142. megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
  143. {
  144. writel(1, &(regs)->outbound_intr_mask);
  145. /* Dummy readl to force pci flush */
  146. readl(&regs->outbound_intr_mask);
  147. }
  148. /**
  149. * megasas_disable_intr_xscale -Disables interrupt
  150. * @regs: MFI register set
  151. */
  152. static inline void
  153. megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
  154. {
  155. u32 mask = 0x1f;
  156. writel(mask, &regs->outbound_intr_mask);
  157. /* Dummy readl to force pci flush */
  158. readl(&regs->outbound_intr_mask);
  159. }
  160. /**
  161. * megasas_read_fw_status_reg_xscale - returns the current FW status value
  162. * @regs: MFI register set
  163. */
  164. static u32
  165. megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
  166. {
  167. return readl(&(regs)->outbound_msg_0);
  168. }
  169. /**
  170. * megasas_clear_interrupt_xscale - Check & clear interrupt
  171. * @regs: MFI register set
  172. */
  173. static int
  174. megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
  175. {
  176. u32 status;
  177. /*
  178. * Check if it is our interrupt
  179. */
  180. status = readl(&regs->outbound_intr_status);
  181. if (!(status & MFI_OB_INTR_STATUS_MASK)) {
  182. return 1;
  183. }
  184. /*
  185. * Clear the interrupt by writing back the same value
  186. */
  187. writel(status, &regs->outbound_intr_status);
  188. /* Dummy readl to force pci flush */
  189. readl(&regs->outbound_intr_status);
  190. return 0;
  191. }
  192. /**
  193. * megasas_fire_cmd_xscale - Sends command to the FW
  194. * @frame_phys_addr : Physical address of cmd
  195. * @frame_count : Number of frames for the command
  196. * @regs : MFI register set
  197. */
  198. static inline void
  199. megasas_fire_cmd_xscale(struct megasas_instance *instance,
  200. dma_addr_t frame_phys_addr,
  201. u32 frame_count,
  202. struct megasas_register_set __iomem *regs)
  203. {
  204. writel((frame_phys_addr >> 3)|(frame_count),
  205. &(regs)->inbound_queue_port);
  206. }
  207. static struct megasas_instance_template megasas_instance_template_xscale = {
  208. .fire_cmd = megasas_fire_cmd_xscale,
  209. .enable_intr = megasas_enable_intr_xscale,
  210. .disable_intr = megasas_disable_intr_xscale,
  211. .clear_intr = megasas_clear_intr_xscale,
  212. .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
  213. };
  214. /**
  215. * This is the end of set of functions & definitions specific
  216. * to xscale (deviceid : 1064R, PERC5) controllers
  217. */
  218. /**
  219. * The following functions are defined for ppc (deviceid : 0x60)
  220. * controllers
  221. */
  222. /**
  223. * megasas_enable_intr_ppc - Enables interrupts
  224. * @regs: MFI register set
  225. */
  226. static inline void
  227. megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
  228. {
  229. writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
  230. writel(~0x80000004, &(regs)->outbound_intr_mask);
  231. /* Dummy readl to force pci flush */
  232. readl(&regs->outbound_intr_mask);
  233. }
  234. /**
  235. * megasas_disable_intr_ppc - Disable interrupt
  236. * @regs: MFI register set
  237. */
  238. static inline void
  239. megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
  240. {
  241. u32 mask = 0xFFFFFFFF;
  242. writel(mask, &regs->outbound_intr_mask);
  243. /* Dummy readl to force pci flush */
  244. readl(&regs->outbound_intr_mask);
  245. }
  246. /**
  247. * megasas_read_fw_status_reg_ppc - returns the current FW status value
  248. * @regs: MFI register set
  249. */
  250. static u32
  251. megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
  252. {
  253. return readl(&(regs)->outbound_scratch_pad);
  254. }
  255. /**
  256. * megasas_clear_interrupt_ppc - Check & clear interrupt
  257. * @regs: MFI register set
  258. */
  259. static int
  260. megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
  261. {
  262. u32 status;
  263. /*
  264. * Check if it is our interrupt
  265. */
  266. status = readl(&regs->outbound_intr_status);
  267. if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
  268. return 1;
  269. }
  270. /*
  271. * Clear the interrupt by writing back the same value
  272. */
  273. writel(status, &regs->outbound_doorbell_clear);
  274. /* Dummy readl to force pci flush */
  275. readl(&regs->outbound_doorbell_clear);
  276. return 0;
  277. }
  278. /**
  279. * megasas_fire_cmd_ppc - Sends command to the FW
  280. * @frame_phys_addr : Physical address of cmd
  281. * @frame_count : Number of frames for the command
  282. * @regs : MFI register set
  283. */
  284. static inline void
  285. megasas_fire_cmd_ppc(struct megasas_instance *instance,
  286. dma_addr_t frame_phys_addr,
  287. u32 frame_count,
  288. struct megasas_register_set __iomem *regs)
  289. {
  290. writel((frame_phys_addr | (frame_count<<1))|1,
  291. &(regs)->inbound_queue_port);
  292. }
  293. static struct megasas_instance_template megasas_instance_template_ppc = {
  294. .fire_cmd = megasas_fire_cmd_ppc,
  295. .enable_intr = megasas_enable_intr_ppc,
  296. .disable_intr = megasas_disable_intr_ppc,
  297. .clear_intr = megasas_clear_intr_ppc,
  298. .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
  299. };
  300. /**
  301. * megasas_enable_intr_skinny - Enables interrupts
  302. * @regs: MFI register set
  303. */
  304. static inline void
  305. megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
  306. {
  307. writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
  308. writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
  309. /* Dummy readl to force pci flush */
  310. readl(&regs->outbound_intr_mask);
  311. }
  312. /**
  313. * megasas_disable_intr_skinny - Disables interrupt
  314. * @regs: MFI register set
  315. */
  316. static inline void
  317. megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
  318. {
  319. u32 mask = 0xFFFFFFFF;
  320. writel(mask, &regs->outbound_intr_mask);
  321. /* Dummy readl to force pci flush */
  322. readl(&regs->outbound_intr_mask);
  323. }
  324. /**
  325. * megasas_read_fw_status_reg_skinny - returns the current FW status value
  326. * @regs: MFI register set
  327. */
  328. static u32
  329. megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
  330. {
  331. return readl(&(regs)->outbound_scratch_pad);
  332. }
  333. /**
  334. * megasas_clear_interrupt_skinny - Check & clear interrupt
  335. * @regs: MFI register set
  336. */
  337. static int
  338. megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
  339. {
  340. u32 status;
  341. /*
  342. * Check if it is our interrupt
  343. */
  344. status = readl(&regs->outbound_intr_status);
  345. if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
  346. return 1;
  347. }
  348. /*
  349. * Clear the interrupt by writing back the same value
  350. */
  351. writel(status, &regs->outbound_intr_status);
  352. /*
  353. * dummy read to flush PCI
  354. */
  355. readl(&regs->outbound_intr_status);
  356. return 0;
  357. }
  358. /**
  359. * megasas_fire_cmd_skinny - Sends command to the FW
  360. * @frame_phys_addr : Physical address of cmd
  361. * @frame_count : Number of frames for the command
  362. * @regs : MFI register set
  363. */
  364. static inline void
  365. megasas_fire_cmd_skinny(struct megasas_instance *instance,
  366. dma_addr_t frame_phys_addr,
  367. u32 frame_count,
  368. struct megasas_register_set __iomem *regs)
  369. {
  370. unsigned long flags;
  371. spin_lock_irqsave(&instance->fire_lock, flags);
  372. writel(0, &(regs)->inbound_high_queue_port);
  373. writel((frame_phys_addr | (frame_count<<1))|1,
  374. &(regs)->inbound_low_queue_port);
  375. spin_unlock_irqrestore(&instance->fire_lock, flags);
  376. }
  377. static struct megasas_instance_template megasas_instance_template_skinny = {
  378. .fire_cmd = megasas_fire_cmd_skinny,
  379. .enable_intr = megasas_enable_intr_skinny,
  380. .disable_intr = megasas_disable_intr_skinny,
  381. .clear_intr = megasas_clear_intr_skinny,
  382. .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
  383. };
  384. /**
  385. * The following functions are defined for gen2 (deviceid : 0x78 0x79)
  386. * controllers
  387. */
  388. /**
  389. * megasas_enable_intr_gen2 - Enables interrupts
  390. * @regs: MFI register set
  391. */
  392. static inline void
  393. megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
  394. {
  395. writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
  396. /* write ~0x00000005 (4 & 1) to the intr mask*/
  397. writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
  398. /* Dummy readl to force pci flush */
  399. readl(&regs->outbound_intr_mask);
  400. }
  401. /**
  402. * megasas_disable_intr_gen2 - Disables interrupt
  403. * @regs: MFI register set
  404. */
  405. static inline void
  406. megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
  407. {
  408. u32 mask = 0xFFFFFFFF;
  409. writel(mask, &regs->outbound_intr_mask);
  410. /* Dummy readl to force pci flush */
  411. readl(&regs->outbound_intr_mask);
  412. }
  413. /**
  414. * megasas_read_fw_status_reg_gen2 - returns the current FW status value
  415. * @regs: MFI register set
  416. */
  417. static u32
  418. megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
  419. {
  420. return readl(&(regs)->outbound_scratch_pad);
  421. }
  422. /**
  423. * megasas_clear_interrupt_gen2 - Check & clear interrupt
  424. * @regs: MFI register set
  425. */
  426. static int
  427. megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
  428. {
  429. u32 status;
  430. /*
  431. * Check if it is our interrupt
  432. */
  433. status = readl(&regs->outbound_intr_status);
  434. if (!(status & MFI_GEN2_ENABLE_INTERRUPT_MASK))
  435. return 1;
  436. /*
  437. * Clear the interrupt by writing back the same value
  438. */
  439. writel(status, &regs->outbound_doorbell_clear);
  440. /* Dummy readl to force pci flush */
  441. readl(&regs->outbound_intr_status);
  442. return 0;
  443. }
  444. /**
  445. * megasas_fire_cmd_gen2 - Sends command to the FW
  446. * @frame_phys_addr : Physical address of cmd
  447. * @frame_count : Number of frames for the command
  448. * @regs : MFI register set
  449. */
  450. static inline void
  451. megasas_fire_cmd_gen2(struct megasas_instance *instance,
  452. dma_addr_t frame_phys_addr,
  453. u32 frame_count,
  454. struct megasas_register_set __iomem *regs)
  455. {
  456. writel((frame_phys_addr | (frame_count<<1))|1,
  457. &(regs)->inbound_queue_port);
  458. }
  459. static struct megasas_instance_template megasas_instance_template_gen2 = {
  460. .fire_cmd = megasas_fire_cmd_gen2,
  461. .enable_intr = megasas_enable_intr_gen2,
  462. .disable_intr = megasas_disable_intr_gen2,
  463. .clear_intr = megasas_clear_intr_gen2,
  464. .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
  465. };
  466. /**
  467. * This is the end of set of functions & definitions
  468. * specific to ppc (deviceid : 0x60) controllers
  469. */
  470. /**
  471. * megasas_issue_polled - Issues a polling command
  472. * @instance: Adapter soft state
  473. * @cmd: Command packet to be issued
  474. *
  475. * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
  476. */
  477. static int
  478. megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
  479. {
  480. int i;
  481. u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
  482. struct megasas_header *frame_hdr = &cmd->frame->hdr;
  483. frame_hdr->cmd_status = 0xFF;
  484. frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
  485. /*
  486. * Issue the frame using inbound queue port
  487. */
  488. instance->instancet->fire_cmd(instance,
  489. cmd->frame_phys_addr, 0, instance->reg_set);
  490. /*
  491. * Wait for cmd_status to change
  492. */
  493. for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
  494. rmb();
  495. msleep(1);
  496. }
  497. if (frame_hdr->cmd_status == 0xff)
  498. return -ETIME;
  499. return 0;
  500. }
  501. /**
  502. * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
  503. * @instance: Adapter soft state
  504. * @cmd: Command to be issued
  505. *
  506. * This function waits on an event for the command to be returned from ISR.
  507. * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
  508. * Used to issue ioctl commands.
  509. */
  510. static int
  511. megasas_issue_blocked_cmd(struct megasas_instance *instance,
  512. struct megasas_cmd *cmd)
  513. {
  514. cmd->cmd_status = ENODATA;
  515. instance->instancet->fire_cmd(instance,
  516. cmd->frame_phys_addr, 0, instance->reg_set);
  517. wait_event_timeout(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA),
  518. MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
  519. return 0;
  520. }
  521. /**
  522. * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
  523. * @instance: Adapter soft state
  524. * @cmd_to_abort: Previously issued cmd to be aborted
  525. *
  526. * MFI firmware can abort previously issued AEN comamnd (automatic event
  527. * notification). The megasas_issue_blocked_abort_cmd() issues such abort
  528. * cmd and waits for return status.
  529. * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
  530. */
  531. static int
  532. megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
  533. struct megasas_cmd *cmd_to_abort)
  534. {
  535. struct megasas_cmd *cmd;
  536. struct megasas_abort_frame *abort_fr;
  537. cmd = megasas_get_cmd(instance);
  538. if (!cmd)
  539. return -1;
  540. abort_fr = &cmd->frame->abort;
  541. /*
  542. * Prepare and issue the abort frame
  543. */
  544. abort_fr->cmd = MFI_CMD_ABORT;
  545. abort_fr->cmd_status = 0xFF;
  546. abort_fr->flags = 0;
  547. abort_fr->abort_context = cmd_to_abort->index;
  548. abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
  549. abort_fr->abort_mfi_phys_addr_hi = 0;
  550. cmd->sync_cmd = 1;
  551. cmd->cmd_status = 0xFF;
  552. instance->instancet->fire_cmd(instance,
  553. cmd->frame_phys_addr, 0, instance->reg_set);
  554. /*
  555. * Wait for this cmd to complete
  556. */
  557. wait_event_timeout(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF),
  558. MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
  559. megasas_return_cmd(instance, cmd);
  560. return 0;
  561. }
  562. /**
  563. * megasas_make_sgl32 - Prepares 32-bit SGL
  564. * @instance: Adapter soft state
  565. * @scp: SCSI command from the mid-layer
  566. * @mfi_sgl: SGL to be filled in
  567. *
  568. * If successful, this function returns the number of SG elements. Otherwise,
  569. * it returnes -1.
  570. */
  571. static int
  572. megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
  573. union megasas_sgl *mfi_sgl)
  574. {
  575. int i;
  576. int sge_count;
  577. struct scatterlist *os_sgl;
  578. sge_count = scsi_dma_map(scp);
  579. BUG_ON(sge_count < 0);
  580. if (sge_count) {
  581. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  582. mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
  583. mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
  584. }
  585. }
  586. return sge_count;
  587. }
  588. /**
  589. * megasas_make_sgl64 - Prepares 64-bit SGL
  590. * @instance: Adapter soft state
  591. * @scp: SCSI command from the mid-layer
  592. * @mfi_sgl: SGL to be filled in
  593. *
  594. * If successful, this function returns the number of SG elements. Otherwise,
  595. * it returnes -1.
  596. */
  597. static int
  598. megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
  599. union megasas_sgl *mfi_sgl)
  600. {
  601. int i;
  602. int sge_count;
  603. struct scatterlist *os_sgl;
  604. sge_count = scsi_dma_map(scp);
  605. BUG_ON(sge_count < 0);
  606. if (sge_count) {
  607. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  608. mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
  609. mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
  610. }
  611. }
  612. return sge_count;
  613. }
  614. /**
  615. * megasas_make_sgl_skinny - Prepares IEEE SGL
  616. * @instance: Adapter soft state
  617. * @scp: SCSI command from the mid-layer
  618. * @mfi_sgl: SGL to be filled in
  619. *
  620. * If successful, this function returns the number of SG elements. Otherwise,
  621. * it returnes -1.
  622. */
  623. static int
  624. megasas_make_sgl_skinny(struct megasas_instance *instance,
  625. struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
  626. {
  627. int i;
  628. int sge_count;
  629. struct scatterlist *os_sgl;
  630. sge_count = scsi_dma_map(scp);
  631. if (sge_count) {
  632. scsi_for_each_sg(scp, os_sgl, sge_count, i) {
  633. mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
  634. mfi_sgl->sge_skinny[i].phys_addr =
  635. sg_dma_address(os_sgl);
  636. }
  637. }
  638. return sge_count;
  639. }
  640. /**
  641. * megasas_get_frame_count - Computes the number of frames
  642. * @frame_type : type of frame- io or pthru frame
  643. * @sge_count : number of sg elements
  644. *
  645. * Returns the number of frames required for numnber of sge's (sge_count)
  646. */
  647. static u32 megasas_get_frame_count(struct megasas_instance *instance,
  648. u8 sge_count, u8 frame_type)
  649. {
  650. int num_cnt;
  651. int sge_bytes;
  652. u32 sge_sz;
  653. u32 frame_count=0;
  654. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  655. sizeof(struct megasas_sge32);
  656. if (instance->flag_ieee) {
  657. sge_sz = sizeof(struct megasas_sge_skinny);
  658. }
  659. /*
  660. * Main frame can contain 2 SGEs for 64-bit SGLs and
  661. * 3 SGEs for 32-bit SGLs for ldio &
  662. * 1 SGEs for 64-bit SGLs and
  663. * 2 SGEs for 32-bit SGLs for pthru frame
  664. */
  665. if (unlikely(frame_type == PTHRU_FRAME)) {
  666. if (instance->flag_ieee == 1) {
  667. num_cnt = sge_count - 1;
  668. } else if (IS_DMA64)
  669. num_cnt = sge_count - 1;
  670. else
  671. num_cnt = sge_count - 2;
  672. } else {
  673. if (instance->flag_ieee == 1) {
  674. num_cnt = sge_count - 1;
  675. } else if (IS_DMA64)
  676. num_cnt = sge_count - 2;
  677. else
  678. num_cnt = sge_count - 3;
  679. }
  680. if(num_cnt>0){
  681. sge_bytes = sge_sz * num_cnt;
  682. frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
  683. ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
  684. }
  685. /* Main frame */
  686. frame_count +=1;
  687. if (frame_count > 7)
  688. frame_count = 8;
  689. return frame_count;
  690. }
  691. /**
  692. * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
  693. * @instance: Adapter soft state
  694. * @scp: SCSI command
  695. * @cmd: Command to be prepared in
  696. *
  697. * This function prepares CDB commands. These are typcially pass-through
  698. * commands to the devices.
  699. */
  700. static int
  701. megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
  702. struct megasas_cmd *cmd)
  703. {
  704. u32 is_logical;
  705. u32 device_id;
  706. u16 flags = 0;
  707. struct megasas_pthru_frame *pthru;
  708. is_logical = MEGASAS_IS_LOGICAL(scp);
  709. device_id = MEGASAS_DEV_INDEX(instance, scp);
  710. pthru = (struct megasas_pthru_frame *)cmd->frame;
  711. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  712. flags = MFI_FRAME_DIR_WRITE;
  713. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  714. flags = MFI_FRAME_DIR_READ;
  715. else if (scp->sc_data_direction == PCI_DMA_NONE)
  716. flags = MFI_FRAME_DIR_NONE;
  717. if (instance->flag_ieee == 1) {
  718. flags |= MFI_FRAME_IEEE;
  719. }
  720. /*
  721. * Prepare the DCDB frame
  722. */
  723. pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
  724. pthru->cmd_status = 0x0;
  725. pthru->scsi_status = 0x0;
  726. pthru->target_id = device_id;
  727. pthru->lun = scp->device->lun;
  728. pthru->cdb_len = scp->cmd_len;
  729. pthru->timeout = 0;
  730. pthru->pad_0 = 0;
  731. pthru->flags = flags;
  732. pthru->data_xfer_len = scsi_bufflen(scp);
  733. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  734. /*
  735. * If the command is for the tape device, set the
  736. * pthru timeout to the os layer timeout value.
  737. */
  738. if (scp->device->type == TYPE_TAPE) {
  739. if ((scp->request->timeout / HZ) > 0xFFFF)
  740. pthru->timeout = 0xFFFF;
  741. else
  742. pthru->timeout = scp->request->timeout / HZ;
  743. }
  744. /*
  745. * Construct SGL
  746. */
  747. if (instance->flag_ieee == 1) {
  748. pthru->flags |= MFI_FRAME_SGL64;
  749. pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
  750. &pthru->sgl);
  751. } else if (IS_DMA64) {
  752. pthru->flags |= MFI_FRAME_SGL64;
  753. pthru->sge_count = megasas_make_sgl64(instance, scp,
  754. &pthru->sgl);
  755. } else
  756. pthru->sge_count = megasas_make_sgl32(instance, scp,
  757. &pthru->sgl);
  758. if (pthru->sge_count > instance->max_num_sge) {
  759. printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
  760. pthru->sge_count);
  761. return 0;
  762. }
  763. /*
  764. * Sense info specific
  765. */
  766. pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
  767. pthru->sense_buf_phys_addr_hi = 0;
  768. pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
  769. /*
  770. * Compute the total number of frames this command consumes. FW uses
  771. * this number to pull sufficient number of frames from host memory.
  772. */
  773. cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
  774. PTHRU_FRAME);
  775. return cmd->frame_count;
  776. }
  777. /**
  778. * megasas_build_ldio - Prepares IOs to logical devices
  779. * @instance: Adapter soft state
  780. * @scp: SCSI command
  781. * @cmd: Command to be prepared
  782. *
  783. * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
  784. */
  785. static int
  786. megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
  787. struct megasas_cmd *cmd)
  788. {
  789. u32 device_id;
  790. u8 sc = scp->cmnd[0];
  791. u16 flags = 0;
  792. struct megasas_io_frame *ldio;
  793. device_id = MEGASAS_DEV_INDEX(instance, scp);
  794. ldio = (struct megasas_io_frame *)cmd->frame;
  795. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  796. flags = MFI_FRAME_DIR_WRITE;
  797. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  798. flags = MFI_FRAME_DIR_READ;
  799. if (instance->flag_ieee == 1) {
  800. flags |= MFI_FRAME_IEEE;
  801. }
  802. /*
  803. * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
  804. */
  805. ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
  806. ldio->cmd_status = 0x0;
  807. ldio->scsi_status = 0x0;
  808. ldio->target_id = device_id;
  809. ldio->timeout = 0;
  810. ldio->reserved_0 = 0;
  811. ldio->pad_0 = 0;
  812. ldio->flags = flags;
  813. ldio->start_lba_hi = 0;
  814. ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
  815. /*
  816. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  817. */
  818. if (scp->cmd_len == 6) {
  819. ldio->lba_count = (u32) scp->cmnd[4];
  820. ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
  821. ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
  822. ldio->start_lba_lo &= 0x1FFFFF;
  823. }
  824. /*
  825. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  826. */
  827. else if (scp->cmd_len == 10) {
  828. ldio->lba_count = (u32) scp->cmnd[8] |
  829. ((u32) scp->cmnd[7] << 8);
  830. ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  831. ((u32) scp->cmnd[3] << 16) |
  832. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  833. }
  834. /*
  835. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  836. */
  837. else if (scp->cmd_len == 12) {
  838. ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
  839. ((u32) scp->cmnd[7] << 16) |
  840. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  841. ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  842. ((u32) scp->cmnd[3] << 16) |
  843. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  844. }
  845. /*
  846. * 16-byte READ(0x88) or WRITE(0x8A) cdb
  847. */
  848. else if (scp->cmd_len == 16) {
  849. ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
  850. ((u32) scp->cmnd[11] << 16) |
  851. ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
  852. ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
  853. ((u32) scp->cmnd[7] << 16) |
  854. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  855. ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
  856. ((u32) scp->cmnd[3] << 16) |
  857. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  858. }
  859. /*
  860. * Construct SGL
  861. */
  862. if (instance->flag_ieee) {
  863. ldio->flags |= MFI_FRAME_SGL64;
  864. ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
  865. &ldio->sgl);
  866. } else if (IS_DMA64) {
  867. ldio->flags |= MFI_FRAME_SGL64;
  868. ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
  869. } else
  870. ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
  871. if (ldio->sge_count > instance->max_num_sge) {
  872. printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
  873. ldio->sge_count);
  874. return 0;
  875. }
  876. /*
  877. * Sense info specific
  878. */
  879. ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
  880. ldio->sense_buf_phys_addr_hi = 0;
  881. ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
  882. /*
  883. * Compute the total number of frames this command consumes. FW uses
  884. * this number to pull sufficient number of frames from host memory.
  885. */
  886. cmd->frame_count = megasas_get_frame_count(instance,
  887. ldio->sge_count, IO_FRAME);
  888. return cmd->frame_count;
  889. }
  890. /**
  891. * megasas_is_ldio - Checks if the cmd is for logical drive
  892. * @scmd: SCSI command
  893. *
  894. * Called by megasas_queue_command to find out if the command to be queued
  895. * is a logical drive command
  896. */
  897. static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
  898. {
  899. if (!MEGASAS_IS_LOGICAL(cmd))
  900. return 0;
  901. switch (cmd->cmnd[0]) {
  902. case READ_10:
  903. case WRITE_10:
  904. case READ_12:
  905. case WRITE_12:
  906. case READ_6:
  907. case WRITE_6:
  908. case READ_16:
  909. case WRITE_16:
  910. return 1;
  911. default:
  912. return 0;
  913. }
  914. }
  915. /**
  916. * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
  917. * in FW
  918. * @instance: Adapter soft state
  919. */
  920. static inline void
  921. megasas_dump_pending_frames(struct megasas_instance *instance)
  922. {
  923. struct megasas_cmd *cmd;
  924. int i,n;
  925. union megasas_sgl *mfi_sgl;
  926. struct megasas_io_frame *ldio;
  927. struct megasas_pthru_frame *pthru;
  928. u32 sgcount;
  929. u32 max_cmd = instance->max_fw_cmds;
  930. printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
  931. printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
  932. if (IS_DMA64)
  933. printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
  934. else
  935. printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
  936. printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
  937. for (i = 0; i < max_cmd; i++) {
  938. cmd = instance->cmd_list[i];
  939. if(!cmd->scmd)
  940. continue;
  941. printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
  942. if (megasas_is_ldio(cmd->scmd)){
  943. ldio = (struct megasas_io_frame *)cmd->frame;
  944. mfi_sgl = &ldio->sgl;
  945. sgcount = ldio->sge_count;
  946. printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",instance->host->host_no, cmd->frame_count,ldio->cmd,ldio->target_id, ldio->start_lba_lo,ldio->start_lba_hi,ldio->sense_buf_phys_addr_lo,sgcount);
  947. }
  948. else {
  949. pthru = (struct megasas_pthru_frame *) cmd->frame;
  950. mfi_sgl = &pthru->sgl;
  951. sgcount = pthru->sge_count;
  952. printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",instance->host->host_no,cmd->frame_count,pthru->cmd,pthru->target_id,pthru->lun,pthru->cdb_len , pthru->data_xfer_len,pthru->sense_buf_phys_addr_lo,sgcount);
  953. }
  954. if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
  955. for (n = 0; n < sgcount; n++){
  956. if (IS_DMA64)
  957. printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%08lx ",mfi_sgl->sge64[n].length , (unsigned long)mfi_sgl->sge64[n].phys_addr) ;
  958. else
  959. printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
  960. }
  961. }
  962. printk(KERN_ERR "\n");
  963. } /*for max_cmd*/
  964. printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
  965. for (i = 0; i < max_cmd; i++) {
  966. cmd = instance->cmd_list[i];
  967. if(cmd->sync_cmd == 1){
  968. printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
  969. }
  970. }
  971. printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
  972. }
  973. /**
  974. * megasas_queue_command - Queue entry point
  975. * @scmd: SCSI command to be queued
  976. * @done: Callback entry point
  977. */
  978. static int
  979. megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
  980. {
  981. u32 frame_count;
  982. struct megasas_cmd *cmd;
  983. struct megasas_instance *instance;
  984. instance = (struct megasas_instance *)
  985. scmd->device->host->hostdata;
  986. /* Don't process if we have already declared adapter dead */
  987. if (instance->hw_crit_error)
  988. return SCSI_MLQUEUE_HOST_BUSY;
  989. scmd->scsi_done = done;
  990. scmd->result = 0;
  991. if (MEGASAS_IS_LOGICAL(scmd) &&
  992. (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
  993. scmd->result = DID_BAD_TARGET << 16;
  994. goto out_done;
  995. }
  996. switch (scmd->cmnd[0]) {
  997. case SYNCHRONIZE_CACHE:
  998. /*
  999. * FW takes care of flush cache on its own
  1000. * No need to send it down
  1001. */
  1002. scmd->result = DID_OK << 16;
  1003. goto out_done;
  1004. default:
  1005. break;
  1006. }
  1007. cmd = megasas_get_cmd(instance);
  1008. if (!cmd)
  1009. return SCSI_MLQUEUE_HOST_BUSY;
  1010. /*
  1011. * Logical drive command
  1012. */
  1013. if (megasas_is_ldio(scmd))
  1014. frame_count = megasas_build_ldio(instance, scmd, cmd);
  1015. else
  1016. frame_count = megasas_build_dcdb(instance, scmd, cmd);
  1017. if (!frame_count)
  1018. goto out_return_cmd;
  1019. cmd->scmd = scmd;
  1020. scmd->SCp.ptr = (char *)cmd;
  1021. /*
  1022. * Issue the command to the FW
  1023. */
  1024. atomic_inc(&instance->fw_outstanding);
  1025. instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
  1026. cmd->frame_count-1, instance->reg_set);
  1027. /*
  1028. * Check if we have pend cmds to be completed
  1029. */
  1030. if (poll_mode_io && atomic_read(&instance->fw_outstanding))
  1031. tasklet_schedule(&instance->isr_tasklet);
  1032. return 0;
  1033. out_return_cmd:
  1034. megasas_return_cmd(instance, cmd);
  1035. out_done:
  1036. done(scmd);
  1037. return 0;
  1038. }
  1039. static struct megasas_instance *megasas_lookup_instance(u16 host_no)
  1040. {
  1041. int i;
  1042. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  1043. if ((megasas_mgmt_info.instance[i]) &&
  1044. (megasas_mgmt_info.instance[i]->host->host_no == host_no))
  1045. return megasas_mgmt_info.instance[i];
  1046. }
  1047. return NULL;
  1048. }
  1049. static int megasas_slave_configure(struct scsi_device *sdev)
  1050. {
  1051. u16 pd_index = 0;
  1052. struct megasas_instance *instance ;
  1053. instance = megasas_lookup_instance(sdev->host->host_no);
  1054. /*
  1055. * Don't export physical disk devices to the disk driver.
  1056. *
  1057. * FIXME: Currently we don't export them to the midlayer at all.
  1058. * That will be fixed once LSI engineers have audited the
  1059. * firmware for possible issues.
  1060. */
  1061. if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
  1062. sdev->type == TYPE_DISK) {
  1063. pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1064. sdev->id;
  1065. if (instance->pd_list[pd_index].driveState ==
  1066. MR_PD_STATE_SYSTEM) {
  1067. blk_queue_rq_timeout(sdev->request_queue,
  1068. MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
  1069. return 0;
  1070. }
  1071. return -ENXIO;
  1072. }
  1073. /*
  1074. * The RAID firmware may require extended timeouts.
  1075. */
  1076. blk_queue_rq_timeout(sdev->request_queue,
  1077. MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
  1078. return 0;
  1079. }
  1080. static int megasas_slave_alloc(struct scsi_device *sdev)
  1081. {
  1082. u16 pd_index = 0;
  1083. struct megasas_instance *instance ;
  1084. instance = megasas_lookup_instance(sdev->host->host_no);
  1085. if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
  1086. (sdev->type == TYPE_DISK)) {
  1087. /*
  1088. * Open the OS scan to the SYSTEM PD
  1089. */
  1090. pd_index =
  1091. (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1092. sdev->id;
  1093. if ((instance->pd_list[pd_index].driveState ==
  1094. MR_PD_STATE_SYSTEM) &&
  1095. (instance->pd_list[pd_index].driveType ==
  1096. TYPE_DISK)) {
  1097. return 0;
  1098. }
  1099. return -ENXIO;
  1100. }
  1101. return 0;
  1102. }
  1103. /**
  1104. * megasas_complete_cmd_dpc - Returns FW's controller structure
  1105. * @instance_addr: Address of adapter soft state
  1106. *
  1107. * Tasklet to complete cmds
  1108. */
  1109. static void megasas_complete_cmd_dpc(unsigned long instance_addr)
  1110. {
  1111. u32 producer;
  1112. u32 consumer;
  1113. u32 context;
  1114. struct megasas_cmd *cmd;
  1115. struct megasas_instance *instance =
  1116. (struct megasas_instance *)instance_addr;
  1117. unsigned long flags;
  1118. /* If we have already declared adapter dead, donot complete cmds */
  1119. if (instance->hw_crit_error)
  1120. return;
  1121. spin_lock_irqsave(&instance->completion_lock, flags);
  1122. producer = *instance->producer;
  1123. consumer = *instance->consumer;
  1124. while (consumer != producer) {
  1125. context = instance->reply_queue[consumer];
  1126. cmd = instance->cmd_list[context];
  1127. megasas_complete_cmd(instance, cmd, DID_OK);
  1128. consumer++;
  1129. if (consumer == (instance->max_fw_cmds + 1)) {
  1130. consumer = 0;
  1131. }
  1132. }
  1133. *instance->consumer = producer;
  1134. spin_unlock_irqrestore(&instance->completion_lock, flags);
  1135. /*
  1136. * Check if we can restore can_queue
  1137. */
  1138. if (instance->flag & MEGASAS_FW_BUSY
  1139. && time_after(jiffies, instance->last_time + 5 * HZ)
  1140. && atomic_read(&instance->fw_outstanding) < 17) {
  1141. spin_lock_irqsave(instance->host->host_lock, flags);
  1142. instance->flag &= ~MEGASAS_FW_BUSY;
  1143. if ((instance->pdev->device ==
  1144. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1145. (instance->pdev->device ==
  1146. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1147. instance->host->can_queue =
  1148. instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
  1149. } else
  1150. instance->host->can_queue =
  1151. instance->max_fw_cmds - MEGASAS_INT_CMDS;
  1152. spin_unlock_irqrestore(instance->host->host_lock, flags);
  1153. }
  1154. }
  1155. /**
  1156. * megasas_wait_for_outstanding - Wait for all outstanding cmds
  1157. * @instance: Adapter soft state
  1158. *
  1159. * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
  1160. * complete all its outstanding commands. Returns error if one or more IOs
  1161. * are pending after this time period. It also marks the controller dead.
  1162. */
  1163. static int megasas_wait_for_outstanding(struct megasas_instance *instance)
  1164. {
  1165. int i;
  1166. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  1167. for (i = 0; i < wait_time; i++) {
  1168. int outstanding = atomic_read(&instance->fw_outstanding);
  1169. if (!outstanding)
  1170. break;
  1171. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  1172. printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
  1173. "commands to complete\n",i,outstanding);
  1174. /*
  1175. * Call cmd completion routine. Cmd to be
  1176. * be completed directly without depending on isr.
  1177. */
  1178. megasas_complete_cmd_dpc((unsigned long)instance);
  1179. }
  1180. msleep(1000);
  1181. }
  1182. if (atomic_read(&instance->fw_outstanding)) {
  1183. /*
  1184. * Send signal to FW to stop processing any pending cmds.
  1185. * The controller will be taken offline by the OS now.
  1186. */
  1187. if ((instance->pdev->device ==
  1188. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1189. (instance->pdev->device ==
  1190. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1191. writel(MFI_STOP_ADP,
  1192. &instance->reg_set->reserved_0[0]);
  1193. } else {
  1194. writel(MFI_STOP_ADP,
  1195. &instance->reg_set->inbound_doorbell);
  1196. }
  1197. megasas_dump_pending_frames(instance);
  1198. instance->hw_crit_error = 1;
  1199. return FAILED;
  1200. }
  1201. return SUCCESS;
  1202. }
  1203. /**
  1204. * megasas_generic_reset - Generic reset routine
  1205. * @scmd: Mid-layer SCSI command
  1206. *
  1207. * This routine implements a generic reset handler for device, bus and host
  1208. * reset requests. Device, bus and host specific reset handlers can use this
  1209. * function after they do their specific tasks.
  1210. */
  1211. static int megasas_generic_reset(struct scsi_cmnd *scmd)
  1212. {
  1213. int ret_val;
  1214. struct megasas_instance *instance;
  1215. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  1216. scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
  1217. scmd->serial_number, scmd->cmnd[0], scmd->retries);
  1218. if (instance->hw_crit_error) {
  1219. printk(KERN_ERR "megasas: cannot recover from previous reset "
  1220. "failures\n");
  1221. return FAILED;
  1222. }
  1223. ret_val = megasas_wait_for_outstanding(instance);
  1224. if (ret_val == SUCCESS)
  1225. printk(KERN_NOTICE "megasas: reset successful \n");
  1226. else
  1227. printk(KERN_ERR "megasas: failed to do reset\n");
  1228. return ret_val;
  1229. }
  1230. /**
  1231. * megasas_reset_timer - quiesce the adapter if required
  1232. * @scmd: scsi cmnd
  1233. *
  1234. * Sets the FW busy flag and reduces the host->can_queue if the
  1235. * cmd has not been completed within the timeout period.
  1236. */
  1237. static enum
  1238. blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
  1239. {
  1240. struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
  1241. struct megasas_instance *instance;
  1242. unsigned long flags;
  1243. if (time_after(jiffies, scmd->jiffies_at_alloc +
  1244. (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
  1245. return BLK_EH_NOT_HANDLED;
  1246. }
  1247. instance = cmd->instance;
  1248. if (!(instance->flag & MEGASAS_FW_BUSY)) {
  1249. /* FW is busy, throttle IO */
  1250. spin_lock_irqsave(instance->host->host_lock, flags);
  1251. instance->host->can_queue = 16;
  1252. instance->last_time = jiffies;
  1253. instance->flag |= MEGASAS_FW_BUSY;
  1254. spin_unlock_irqrestore(instance->host->host_lock, flags);
  1255. }
  1256. return BLK_EH_RESET_TIMER;
  1257. }
  1258. /**
  1259. * megasas_reset_device - Device reset handler entry point
  1260. */
  1261. static int megasas_reset_device(struct scsi_cmnd *scmd)
  1262. {
  1263. int ret;
  1264. /*
  1265. * First wait for all commands to complete
  1266. */
  1267. ret = megasas_generic_reset(scmd);
  1268. return ret;
  1269. }
  1270. /**
  1271. * megasas_reset_bus_host - Bus & host reset handler entry point
  1272. */
  1273. static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
  1274. {
  1275. int ret;
  1276. /*
  1277. * First wait for all commands to complete
  1278. */
  1279. ret = megasas_generic_reset(scmd);
  1280. return ret;
  1281. }
  1282. /**
  1283. * megasas_bios_param - Returns disk geometry for a disk
  1284. * @sdev: device handle
  1285. * @bdev: block device
  1286. * @capacity: drive capacity
  1287. * @geom: geometry parameters
  1288. */
  1289. static int
  1290. megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  1291. sector_t capacity, int geom[])
  1292. {
  1293. int heads;
  1294. int sectors;
  1295. sector_t cylinders;
  1296. unsigned long tmp;
  1297. /* Default heads (64) & sectors (32) */
  1298. heads = 64;
  1299. sectors = 32;
  1300. tmp = heads * sectors;
  1301. cylinders = capacity;
  1302. sector_div(cylinders, tmp);
  1303. /*
  1304. * Handle extended translation size for logical drives > 1Gb
  1305. */
  1306. if (capacity >= 0x200000) {
  1307. heads = 255;
  1308. sectors = 63;
  1309. tmp = heads*sectors;
  1310. cylinders = capacity;
  1311. sector_div(cylinders, tmp);
  1312. }
  1313. geom[0] = heads;
  1314. geom[1] = sectors;
  1315. geom[2] = cylinders;
  1316. return 0;
  1317. }
  1318. static void megasas_aen_polling(struct work_struct *work);
  1319. /**
  1320. * megasas_service_aen - Processes an event notification
  1321. * @instance: Adapter soft state
  1322. * @cmd: AEN command completed by the ISR
  1323. *
  1324. * For AEN, driver sends a command down to FW that is held by the FW till an
  1325. * event occurs. When an event of interest occurs, FW completes the command
  1326. * that it was previously holding.
  1327. *
  1328. * This routines sends SIGIO signal to processes that have registered with the
  1329. * driver for AEN.
  1330. */
  1331. static void
  1332. megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
  1333. {
  1334. unsigned long flags;
  1335. /*
  1336. * Don't signal app if it is just an aborted previously registered aen
  1337. */
  1338. if ((!cmd->abort_aen) && (instance->unload == 0)) {
  1339. spin_lock_irqsave(&poll_aen_lock, flags);
  1340. megasas_poll_wait_aen = 1;
  1341. spin_unlock_irqrestore(&poll_aen_lock, flags);
  1342. wake_up(&megasas_poll_wait);
  1343. kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
  1344. }
  1345. else
  1346. cmd->abort_aen = 0;
  1347. instance->aen_cmd = NULL;
  1348. megasas_return_cmd(instance, cmd);
  1349. if (instance->unload == 0) {
  1350. struct megasas_aen_event *ev;
  1351. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  1352. if (!ev) {
  1353. printk(KERN_ERR "megasas_service_aen: out of memory\n");
  1354. } else {
  1355. ev->instance = instance;
  1356. instance->ev = ev;
  1357. INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
  1358. schedule_delayed_work(
  1359. (struct delayed_work *)&ev->hotplug_work, 0);
  1360. }
  1361. }
  1362. }
  1363. /*
  1364. * Scsi host template for megaraid_sas driver
  1365. */
  1366. static struct scsi_host_template megasas_template = {
  1367. .module = THIS_MODULE,
  1368. .name = "LSI SAS based MegaRAID driver",
  1369. .proc_name = "megaraid_sas",
  1370. .slave_configure = megasas_slave_configure,
  1371. .slave_alloc = megasas_slave_alloc,
  1372. .queuecommand = megasas_queue_command,
  1373. .eh_device_reset_handler = megasas_reset_device,
  1374. .eh_bus_reset_handler = megasas_reset_bus_host,
  1375. .eh_host_reset_handler = megasas_reset_bus_host,
  1376. .eh_timed_out = megasas_reset_timer,
  1377. .bios_param = megasas_bios_param,
  1378. .use_clustering = ENABLE_CLUSTERING,
  1379. };
  1380. /**
  1381. * megasas_complete_int_cmd - Completes an internal command
  1382. * @instance: Adapter soft state
  1383. * @cmd: Command to be completed
  1384. *
  1385. * The megasas_issue_blocked_cmd() function waits for a command to complete
  1386. * after it issues a command. This function wakes up that waiting routine by
  1387. * calling wake_up() on the wait queue.
  1388. */
  1389. static void
  1390. megasas_complete_int_cmd(struct megasas_instance *instance,
  1391. struct megasas_cmd *cmd)
  1392. {
  1393. cmd->cmd_status = cmd->frame->io.cmd_status;
  1394. if (cmd->cmd_status == ENODATA) {
  1395. cmd->cmd_status = 0;
  1396. }
  1397. wake_up(&instance->int_cmd_wait_q);
  1398. }
  1399. /**
  1400. * megasas_complete_abort - Completes aborting a command
  1401. * @instance: Adapter soft state
  1402. * @cmd: Cmd that was issued to abort another cmd
  1403. *
  1404. * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
  1405. * after it issues an abort on a previously issued command. This function
  1406. * wakes up all functions waiting on the same wait queue.
  1407. */
  1408. static void
  1409. megasas_complete_abort(struct megasas_instance *instance,
  1410. struct megasas_cmd *cmd)
  1411. {
  1412. if (cmd->sync_cmd) {
  1413. cmd->sync_cmd = 0;
  1414. cmd->cmd_status = 0;
  1415. wake_up(&instance->abort_cmd_wait_q);
  1416. }
  1417. return;
  1418. }
  1419. /**
  1420. * megasas_complete_cmd - Completes a command
  1421. * @instance: Adapter soft state
  1422. * @cmd: Command to be completed
  1423. * @alt_status: If non-zero, use this value as status to
  1424. * SCSI mid-layer instead of the value returned
  1425. * by the FW. This should be used if caller wants
  1426. * an alternate status (as in the case of aborted
  1427. * commands)
  1428. */
  1429. static void
  1430. megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
  1431. u8 alt_status)
  1432. {
  1433. int exception = 0;
  1434. struct megasas_header *hdr = &cmd->frame->hdr;
  1435. unsigned long flags;
  1436. if (cmd->scmd)
  1437. cmd->scmd->SCp.ptr = NULL;
  1438. switch (hdr->cmd) {
  1439. case MFI_CMD_PD_SCSI_IO:
  1440. case MFI_CMD_LD_SCSI_IO:
  1441. /*
  1442. * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
  1443. * issued either through an IO path or an IOCTL path. If it
  1444. * was via IOCTL, we will send it to internal completion.
  1445. */
  1446. if (cmd->sync_cmd) {
  1447. cmd->sync_cmd = 0;
  1448. megasas_complete_int_cmd(instance, cmd);
  1449. break;
  1450. }
  1451. case MFI_CMD_LD_READ:
  1452. case MFI_CMD_LD_WRITE:
  1453. if (alt_status) {
  1454. cmd->scmd->result = alt_status << 16;
  1455. exception = 1;
  1456. }
  1457. if (exception) {
  1458. atomic_dec(&instance->fw_outstanding);
  1459. scsi_dma_unmap(cmd->scmd);
  1460. cmd->scmd->scsi_done(cmd->scmd);
  1461. megasas_return_cmd(instance, cmd);
  1462. break;
  1463. }
  1464. switch (hdr->cmd_status) {
  1465. case MFI_STAT_OK:
  1466. cmd->scmd->result = DID_OK << 16;
  1467. break;
  1468. case MFI_STAT_SCSI_IO_FAILED:
  1469. case MFI_STAT_LD_INIT_IN_PROGRESS:
  1470. cmd->scmd->result =
  1471. (DID_ERROR << 16) | hdr->scsi_status;
  1472. break;
  1473. case MFI_STAT_SCSI_DONE_WITH_ERROR:
  1474. cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
  1475. if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
  1476. memset(cmd->scmd->sense_buffer, 0,
  1477. SCSI_SENSE_BUFFERSIZE);
  1478. memcpy(cmd->scmd->sense_buffer, cmd->sense,
  1479. hdr->sense_len);
  1480. cmd->scmd->result |= DRIVER_SENSE << 24;
  1481. }
  1482. break;
  1483. case MFI_STAT_LD_OFFLINE:
  1484. case MFI_STAT_DEVICE_NOT_FOUND:
  1485. cmd->scmd->result = DID_BAD_TARGET << 16;
  1486. break;
  1487. default:
  1488. printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
  1489. hdr->cmd_status);
  1490. cmd->scmd->result = DID_ERROR << 16;
  1491. break;
  1492. }
  1493. atomic_dec(&instance->fw_outstanding);
  1494. scsi_dma_unmap(cmd->scmd);
  1495. cmd->scmd->scsi_done(cmd->scmd);
  1496. megasas_return_cmd(instance, cmd);
  1497. break;
  1498. case MFI_CMD_SMP:
  1499. case MFI_CMD_STP:
  1500. case MFI_CMD_DCMD:
  1501. if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
  1502. cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
  1503. spin_lock_irqsave(&poll_aen_lock, flags);
  1504. megasas_poll_wait_aen = 0;
  1505. spin_unlock_irqrestore(&poll_aen_lock, flags);
  1506. }
  1507. /*
  1508. * See if got an event notification
  1509. */
  1510. if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
  1511. megasas_service_aen(instance, cmd);
  1512. else
  1513. megasas_complete_int_cmd(instance, cmd);
  1514. break;
  1515. case MFI_CMD_ABORT:
  1516. /*
  1517. * Cmd issued to abort another cmd returned
  1518. */
  1519. megasas_complete_abort(instance, cmd);
  1520. break;
  1521. default:
  1522. printk("megasas: Unknown command completed! [0x%X]\n",
  1523. hdr->cmd);
  1524. break;
  1525. }
  1526. }
  1527. /**
  1528. * megasas_deplete_reply_queue - Processes all completed commands
  1529. * @instance: Adapter soft state
  1530. * @alt_status: Alternate status to be returned to
  1531. * SCSI mid-layer instead of the status
  1532. * returned by the FW
  1533. */
  1534. static int
  1535. megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
  1536. {
  1537. /*
  1538. * Check if it is our interrupt
  1539. * Clear the interrupt
  1540. */
  1541. if(instance->instancet->clear_intr(instance->reg_set))
  1542. return IRQ_NONE;
  1543. if (instance->hw_crit_error)
  1544. goto out_done;
  1545. /*
  1546. * Schedule the tasklet for cmd completion
  1547. */
  1548. tasklet_schedule(&instance->isr_tasklet);
  1549. out_done:
  1550. return IRQ_HANDLED;
  1551. }
  1552. /**
  1553. * megasas_isr - isr entry point
  1554. */
  1555. static irqreturn_t megasas_isr(int irq, void *devp)
  1556. {
  1557. return megasas_deplete_reply_queue((struct megasas_instance *)devp,
  1558. DID_OK);
  1559. }
  1560. /**
  1561. * megasas_transition_to_ready - Move the FW to READY state
  1562. * @instance: Adapter soft state
  1563. *
  1564. * During the initialization, FW passes can potentially be in any one of
  1565. * several possible states. If the FW in operational, waiting-for-handshake
  1566. * states, driver must take steps to bring it to ready state. Otherwise, it
  1567. * has to wait for the ready state.
  1568. */
  1569. static int
  1570. megasas_transition_to_ready(struct megasas_instance* instance)
  1571. {
  1572. int i;
  1573. u8 max_wait;
  1574. u32 fw_state;
  1575. u32 cur_state;
  1576. u32 abs_state, curr_abs_state;
  1577. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
  1578. if (fw_state != MFI_STATE_READY)
  1579. printk(KERN_INFO "megasas: Waiting for FW to come to ready"
  1580. " state\n");
  1581. while (fw_state != MFI_STATE_READY) {
  1582. abs_state =
  1583. instance->instancet->read_fw_status_reg(instance->reg_set);
  1584. switch (fw_state) {
  1585. case MFI_STATE_FAULT:
  1586. printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
  1587. return -ENODEV;
  1588. case MFI_STATE_WAIT_HANDSHAKE:
  1589. /*
  1590. * Set the CLR bit in inbound doorbell
  1591. */
  1592. if ((instance->pdev->device ==
  1593. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1594. (instance->pdev->device ==
  1595. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1596. writel(
  1597. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  1598. &instance->reg_set->reserved_0[0]);
  1599. } else {
  1600. writel(
  1601. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  1602. &instance->reg_set->inbound_doorbell);
  1603. }
  1604. max_wait = MEGASAS_RESET_WAIT_TIME;
  1605. cur_state = MFI_STATE_WAIT_HANDSHAKE;
  1606. break;
  1607. case MFI_STATE_BOOT_MESSAGE_PENDING:
  1608. if ((instance->pdev->device ==
  1609. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1610. (instance->pdev->device ==
  1611. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1612. writel(MFI_INIT_HOTPLUG,
  1613. &instance->reg_set->reserved_0[0]);
  1614. } else
  1615. writel(MFI_INIT_HOTPLUG,
  1616. &instance->reg_set->inbound_doorbell);
  1617. max_wait = MEGASAS_RESET_WAIT_TIME;
  1618. cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
  1619. break;
  1620. case MFI_STATE_OPERATIONAL:
  1621. /*
  1622. * Bring it to READY state; assuming max wait 10 secs
  1623. */
  1624. instance->instancet->disable_intr(instance->reg_set);
  1625. if ((instance->pdev->device ==
  1626. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1627. (instance->pdev->device ==
  1628. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1629. writel(MFI_RESET_FLAGS,
  1630. &instance->reg_set->reserved_0[0]);
  1631. } else
  1632. writel(MFI_RESET_FLAGS,
  1633. &instance->reg_set->inbound_doorbell);
  1634. max_wait = MEGASAS_RESET_WAIT_TIME;
  1635. cur_state = MFI_STATE_OPERATIONAL;
  1636. break;
  1637. case MFI_STATE_UNDEFINED:
  1638. /*
  1639. * This state should not last for more than 2 seconds
  1640. */
  1641. max_wait = MEGASAS_RESET_WAIT_TIME;
  1642. cur_state = MFI_STATE_UNDEFINED;
  1643. break;
  1644. case MFI_STATE_BB_INIT:
  1645. max_wait = MEGASAS_RESET_WAIT_TIME;
  1646. cur_state = MFI_STATE_BB_INIT;
  1647. break;
  1648. case MFI_STATE_FW_INIT:
  1649. max_wait = MEGASAS_RESET_WAIT_TIME;
  1650. cur_state = MFI_STATE_FW_INIT;
  1651. break;
  1652. case MFI_STATE_FW_INIT_2:
  1653. max_wait = MEGASAS_RESET_WAIT_TIME;
  1654. cur_state = MFI_STATE_FW_INIT_2;
  1655. break;
  1656. case MFI_STATE_DEVICE_SCAN:
  1657. max_wait = MEGASAS_RESET_WAIT_TIME;
  1658. cur_state = MFI_STATE_DEVICE_SCAN;
  1659. break;
  1660. case MFI_STATE_FLUSH_CACHE:
  1661. max_wait = MEGASAS_RESET_WAIT_TIME;
  1662. cur_state = MFI_STATE_FLUSH_CACHE;
  1663. break;
  1664. default:
  1665. printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
  1666. fw_state);
  1667. return -ENODEV;
  1668. }
  1669. /*
  1670. * The cur_state should not last for more than max_wait secs
  1671. */
  1672. for (i = 0; i < (max_wait * 1000); i++) {
  1673. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
  1674. MFI_STATE_MASK ;
  1675. curr_abs_state =
  1676. instance->instancet->read_fw_status_reg(instance->reg_set);
  1677. if (abs_state == curr_abs_state) {
  1678. msleep(1);
  1679. } else
  1680. break;
  1681. }
  1682. /*
  1683. * Return error if fw_state hasn't changed after max_wait
  1684. */
  1685. if (curr_abs_state == abs_state) {
  1686. printk(KERN_DEBUG "FW state [%d] hasn't changed "
  1687. "in %d secs\n", fw_state, max_wait);
  1688. return -ENODEV;
  1689. }
  1690. };
  1691. printk(KERN_INFO "megasas: FW now in Ready state\n");
  1692. return 0;
  1693. }
  1694. /**
  1695. * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
  1696. * @instance: Adapter soft state
  1697. */
  1698. static void megasas_teardown_frame_pool(struct megasas_instance *instance)
  1699. {
  1700. int i;
  1701. u32 max_cmd = instance->max_fw_cmds;
  1702. struct megasas_cmd *cmd;
  1703. if (!instance->frame_dma_pool)
  1704. return;
  1705. /*
  1706. * Return all frames to pool
  1707. */
  1708. for (i = 0; i < max_cmd; i++) {
  1709. cmd = instance->cmd_list[i];
  1710. if (cmd->frame)
  1711. pci_pool_free(instance->frame_dma_pool, cmd->frame,
  1712. cmd->frame_phys_addr);
  1713. if (cmd->sense)
  1714. pci_pool_free(instance->sense_dma_pool, cmd->sense,
  1715. cmd->sense_phys_addr);
  1716. }
  1717. /*
  1718. * Now destroy the pool itself
  1719. */
  1720. pci_pool_destroy(instance->frame_dma_pool);
  1721. pci_pool_destroy(instance->sense_dma_pool);
  1722. instance->frame_dma_pool = NULL;
  1723. instance->sense_dma_pool = NULL;
  1724. }
  1725. /**
  1726. * megasas_create_frame_pool - Creates DMA pool for cmd frames
  1727. * @instance: Adapter soft state
  1728. *
  1729. * Each command packet has an embedded DMA memory buffer that is used for
  1730. * filling MFI frame and the SG list that immediately follows the frame. This
  1731. * function creates those DMA memory buffers for each command packet by using
  1732. * PCI pool facility.
  1733. */
  1734. static int megasas_create_frame_pool(struct megasas_instance *instance)
  1735. {
  1736. int i;
  1737. u32 max_cmd;
  1738. u32 sge_sz;
  1739. u32 sgl_sz;
  1740. u32 total_sz;
  1741. u32 frame_count;
  1742. struct megasas_cmd *cmd;
  1743. max_cmd = instance->max_fw_cmds;
  1744. /*
  1745. * Size of our frame is 64 bytes for MFI frame, followed by max SG
  1746. * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
  1747. */
  1748. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  1749. sizeof(struct megasas_sge32);
  1750. if (instance->flag_ieee) {
  1751. sge_sz = sizeof(struct megasas_sge_skinny);
  1752. }
  1753. /*
  1754. * Calculated the number of 64byte frames required for SGL
  1755. */
  1756. sgl_sz = sge_sz * instance->max_num_sge;
  1757. frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
  1758. /*
  1759. * We need one extra frame for the MFI command
  1760. */
  1761. frame_count++;
  1762. total_sz = MEGAMFI_FRAME_SIZE * frame_count;
  1763. /*
  1764. * Use DMA pool facility provided by PCI layer
  1765. */
  1766. instance->frame_dma_pool = pci_pool_create("megasas frame pool",
  1767. instance->pdev, total_sz, 64,
  1768. 0);
  1769. if (!instance->frame_dma_pool) {
  1770. printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
  1771. return -ENOMEM;
  1772. }
  1773. instance->sense_dma_pool = pci_pool_create("megasas sense pool",
  1774. instance->pdev, 128, 4, 0);
  1775. if (!instance->sense_dma_pool) {
  1776. printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
  1777. pci_pool_destroy(instance->frame_dma_pool);
  1778. instance->frame_dma_pool = NULL;
  1779. return -ENOMEM;
  1780. }
  1781. /*
  1782. * Allocate and attach a frame to each of the commands in cmd_list.
  1783. * By making cmd->index as the context instead of the &cmd, we can
  1784. * always use 32bit context regardless of the architecture
  1785. */
  1786. for (i = 0; i < max_cmd; i++) {
  1787. cmd = instance->cmd_list[i];
  1788. cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
  1789. GFP_KERNEL, &cmd->frame_phys_addr);
  1790. cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
  1791. GFP_KERNEL, &cmd->sense_phys_addr);
  1792. /*
  1793. * megasas_teardown_frame_pool() takes care of freeing
  1794. * whatever has been allocated
  1795. */
  1796. if (!cmd->frame || !cmd->sense) {
  1797. printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
  1798. megasas_teardown_frame_pool(instance);
  1799. return -ENOMEM;
  1800. }
  1801. cmd->frame->io.context = cmd->index;
  1802. cmd->frame->io.pad_0 = 0;
  1803. }
  1804. return 0;
  1805. }
  1806. /**
  1807. * megasas_free_cmds - Free all the cmds in the free cmd pool
  1808. * @instance: Adapter soft state
  1809. */
  1810. static void megasas_free_cmds(struct megasas_instance *instance)
  1811. {
  1812. int i;
  1813. /* First free the MFI frame pool */
  1814. megasas_teardown_frame_pool(instance);
  1815. /* Free all the commands in the cmd_list */
  1816. for (i = 0; i < instance->max_fw_cmds; i++)
  1817. kfree(instance->cmd_list[i]);
  1818. /* Free the cmd_list buffer itself */
  1819. kfree(instance->cmd_list);
  1820. instance->cmd_list = NULL;
  1821. INIT_LIST_HEAD(&instance->cmd_pool);
  1822. }
  1823. /**
  1824. * megasas_alloc_cmds - Allocates the command packets
  1825. * @instance: Adapter soft state
  1826. *
  1827. * Each command that is issued to the FW, whether IO commands from the OS or
  1828. * internal commands like IOCTLs, are wrapped in local data structure called
  1829. * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
  1830. * the FW.
  1831. *
  1832. * Each frame has a 32-bit field called context (tag). This context is used
  1833. * to get back the megasas_cmd from the frame when a frame gets completed in
  1834. * the ISR. Typically the address of the megasas_cmd itself would be used as
  1835. * the context. But we wanted to keep the differences between 32 and 64 bit
  1836. * systems to the mininum. We always use 32 bit integers for the context. In
  1837. * this driver, the 32 bit values are the indices into an array cmd_list.
  1838. * This array is used only to look up the megasas_cmd given the context. The
  1839. * free commands themselves are maintained in a linked list called cmd_pool.
  1840. */
  1841. static int megasas_alloc_cmds(struct megasas_instance *instance)
  1842. {
  1843. int i;
  1844. int j;
  1845. u32 max_cmd;
  1846. struct megasas_cmd *cmd;
  1847. max_cmd = instance->max_fw_cmds;
  1848. /*
  1849. * instance->cmd_list is an array of struct megasas_cmd pointers.
  1850. * Allocate the dynamic array first and then allocate individual
  1851. * commands.
  1852. */
  1853. instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
  1854. if (!instance->cmd_list) {
  1855. printk(KERN_DEBUG "megasas: out of memory\n");
  1856. return -ENOMEM;
  1857. }
  1858. for (i = 0; i < max_cmd; i++) {
  1859. instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
  1860. GFP_KERNEL);
  1861. if (!instance->cmd_list[i]) {
  1862. for (j = 0; j < i; j++)
  1863. kfree(instance->cmd_list[j]);
  1864. kfree(instance->cmd_list);
  1865. instance->cmd_list = NULL;
  1866. return -ENOMEM;
  1867. }
  1868. }
  1869. /*
  1870. * Add all the commands to command pool (instance->cmd_pool)
  1871. */
  1872. for (i = 0; i < max_cmd; i++) {
  1873. cmd = instance->cmd_list[i];
  1874. memset(cmd, 0, sizeof(struct megasas_cmd));
  1875. cmd->index = i;
  1876. cmd->instance = instance;
  1877. list_add_tail(&cmd->list, &instance->cmd_pool);
  1878. }
  1879. /*
  1880. * Create a frame pool and assign one frame to each cmd
  1881. */
  1882. if (megasas_create_frame_pool(instance)) {
  1883. printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
  1884. megasas_free_cmds(instance);
  1885. }
  1886. return 0;
  1887. }
  1888. /*
  1889. * megasas_get_pd_list_info - Returns FW's pd_list structure
  1890. * @instance: Adapter soft state
  1891. * @pd_list: pd_list structure
  1892. *
  1893. * Issues an internal command (DCMD) to get the FW's controller PD
  1894. * list structure. This information is mainly used to find out SYSTEM
  1895. * supported by the FW.
  1896. */
  1897. static int
  1898. megasas_get_pd_list(struct megasas_instance *instance)
  1899. {
  1900. int ret = 0, pd_index = 0;
  1901. struct megasas_cmd *cmd;
  1902. struct megasas_dcmd_frame *dcmd;
  1903. struct MR_PD_LIST *ci;
  1904. struct MR_PD_ADDRESS *pd_addr;
  1905. dma_addr_t ci_h = 0;
  1906. cmd = megasas_get_cmd(instance);
  1907. if (!cmd) {
  1908. printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
  1909. return -ENOMEM;
  1910. }
  1911. dcmd = &cmd->frame->dcmd;
  1912. ci = pci_alloc_consistent(instance->pdev,
  1913. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
  1914. if (!ci) {
  1915. printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
  1916. megasas_return_cmd(instance, cmd);
  1917. return -ENOMEM;
  1918. }
  1919. memset(ci, 0, sizeof(*ci));
  1920. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1921. dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
  1922. dcmd->mbox.b[1] = 0;
  1923. dcmd->cmd = MFI_CMD_DCMD;
  1924. dcmd->cmd_status = 0xFF;
  1925. dcmd->sge_count = 1;
  1926. dcmd->flags = MFI_FRAME_DIR_READ;
  1927. dcmd->timeout = 0;
  1928. dcmd->pad_0 = 0;
  1929. dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
  1930. dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
  1931. dcmd->sgl.sge32[0].phys_addr = ci_h;
  1932. dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
  1933. if (!megasas_issue_polled(instance, cmd)) {
  1934. ret = 0;
  1935. } else {
  1936. ret = -1;
  1937. }
  1938. /*
  1939. * the following function will get the instance PD LIST.
  1940. */
  1941. pd_addr = ci->addr;
  1942. if ( ret == 0 &&
  1943. (ci->count <
  1944. (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
  1945. memset(instance->pd_list, 0,
  1946. MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
  1947. for (pd_index = 0; pd_index < ci->count; pd_index++) {
  1948. instance->pd_list[pd_addr->deviceId].tid =
  1949. pd_addr->deviceId;
  1950. instance->pd_list[pd_addr->deviceId].driveType =
  1951. pd_addr->scsiDevType;
  1952. instance->pd_list[pd_addr->deviceId].driveState =
  1953. MR_PD_STATE_SYSTEM;
  1954. pd_addr++;
  1955. }
  1956. }
  1957. pci_free_consistent(instance->pdev,
  1958. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
  1959. ci, ci_h);
  1960. megasas_return_cmd(instance, cmd);
  1961. return ret;
  1962. }
  1963. /*
  1964. * megasas_get_ld_list_info - Returns FW's ld_list structure
  1965. * @instance: Adapter soft state
  1966. * @ld_list: ld_list structure
  1967. *
  1968. * Issues an internal command (DCMD) to get the FW's controller PD
  1969. * list structure. This information is mainly used to find out SYSTEM
  1970. * supported by the FW.
  1971. */
  1972. static int
  1973. megasas_get_ld_list(struct megasas_instance *instance)
  1974. {
  1975. int ret = 0, ld_index = 0, ids = 0;
  1976. struct megasas_cmd *cmd;
  1977. struct megasas_dcmd_frame *dcmd;
  1978. struct MR_LD_LIST *ci;
  1979. dma_addr_t ci_h = 0;
  1980. cmd = megasas_get_cmd(instance);
  1981. if (!cmd) {
  1982. printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
  1983. return -ENOMEM;
  1984. }
  1985. dcmd = &cmd->frame->dcmd;
  1986. ci = pci_alloc_consistent(instance->pdev,
  1987. sizeof(struct MR_LD_LIST),
  1988. &ci_h);
  1989. if (!ci) {
  1990. printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
  1991. megasas_return_cmd(instance, cmd);
  1992. return -ENOMEM;
  1993. }
  1994. memset(ci, 0, sizeof(*ci));
  1995. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1996. dcmd->cmd = MFI_CMD_DCMD;
  1997. dcmd->cmd_status = 0xFF;
  1998. dcmd->sge_count = 1;
  1999. dcmd->flags = MFI_FRAME_DIR_READ;
  2000. dcmd->timeout = 0;
  2001. dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
  2002. dcmd->opcode = MR_DCMD_LD_GET_LIST;
  2003. dcmd->sgl.sge32[0].phys_addr = ci_h;
  2004. dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
  2005. dcmd->pad_0 = 0;
  2006. if (!megasas_issue_polled(instance, cmd)) {
  2007. ret = 0;
  2008. } else {
  2009. ret = -1;
  2010. }
  2011. /* the following function will get the instance PD LIST */
  2012. if ((ret == 0) && (ci->ldCount < MAX_LOGICAL_DRIVES)) {
  2013. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  2014. for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
  2015. if (ci->ldList[ld_index].state != 0) {
  2016. ids = ci->ldList[ld_index].ref.targetId;
  2017. instance->ld_ids[ids] =
  2018. ci->ldList[ld_index].ref.targetId;
  2019. }
  2020. }
  2021. }
  2022. pci_free_consistent(instance->pdev,
  2023. sizeof(struct MR_LD_LIST),
  2024. ci,
  2025. ci_h);
  2026. megasas_return_cmd(instance, cmd);
  2027. return ret;
  2028. }
  2029. /**
  2030. * megasas_get_controller_info - Returns FW's controller structure
  2031. * @instance: Adapter soft state
  2032. * @ctrl_info: Controller information structure
  2033. *
  2034. * Issues an internal command (DCMD) to get the FW's controller structure.
  2035. * This information is mainly used to find out the maximum IO transfer per
  2036. * command supported by the FW.
  2037. */
  2038. static int
  2039. megasas_get_ctrl_info(struct megasas_instance *instance,
  2040. struct megasas_ctrl_info *ctrl_info)
  2041. {
  2042. int ret = 0;
  2043. struct megasas_cmd *cmd;
  2044. struct megasas_dcmd_frame *dcmd;
  2045. struct megasas_ctrl_info *ci;
  2046. dma_addr_t ci_h = 0;
  2047. cmd = megasas_get_cmd(instance);
  2048. if (!cmd) {
  2049. printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
  2050. return -ENOMEM;
  2051. }
  2052. dcmd = &cmd->frame->dcmd;
  2053. ci = pci_alloc_consistent(instance->pdev,
  2054. sizeof(struct megasas_ctrl_info), &ci_h);
  2055. if (!ci) {
  2056. printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
  2057. megasas_return_cmd(instance, cmd);
  2058. return -ENOMEM;
  2059. }
  2060. memset(ci, 0, sizeof(*ci));
  2061. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2062. dcmd->cmd = MFI_CMD_DCMD;
  2063. dcmd->cmd_status = 0xFF;
  2064. dcmd->sge_count = 1;
  2065. dcmd->flags = MFI_FRAME_DIR_READ;
  2066. dcmd->timeout = 0;
  2067. dcmd->pad_0 = 0;
  2068. dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
  2069. dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
  2070. dcmd->sgl.sge32[0].phys_addr = ci_h;
  2071. dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
  2072. if (!megasas_issue_polled(instance, cmd)) {
  2073. ret = 0;
  2074. memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
  2075. } else {
  2076. ret = -1;
  2077. }
  2078. pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
  2079. ci, ci_h);
  2080. megasas_return_cmd(instance, cmd);
  2081. return ret;
  2082. }
  2083. /**
  2084. * megasas_issue_init_mfi - Initializes the FW
  2085. * @instance: Adapter soft state
  2086. *
  2087. * Issues the INIT MFI cmd
  2088. */
  2089. static int
  2090. megasas_issue_init_mfi(struct megasas_instance *instance)
  2091. {
  2092. u32 context;
  2093. struct megasas_cmd *cmd;
  2094. struct megasas_init_frame *init_frame;
  2095. struct megasas_init_queue_info *initq_info;
  2096. dma_addr_t init_frame_h;
  2097. dma_addr_t initq_info_h;
  2098. /*
  2099. * Prepare a init frame. Note the init frame points to queue info
  2100. * structure. Each frame has SGL allocated after first 64 bytes. For
  2101. * this frame - since we don't need any SGL - we use SGL's space as
  2102. * queue info structure
  2103. *
  2104. * We will not get a NULL command below. We just created the pool.
  2105. */
  2106. cmd = megasas_get_cmd(instance);
  2107. init_frame = (struct megasas_init_frame *)cmd->frame;
  2108. initq_info = (struct megasas_init_queue_info *)
  2109. ((unsigned long)init_frame + 64);
  2110. init_frame_h = cmd->frame_phys_addr;
  2111. initq_info_h = init_frame_h + 64;
  2112. context = init_frame->context;
  2113. memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
  2114. memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
  2115. init_frame->context = context;
  2116. initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
  2117. initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
  2118. initq_info->producer_index_phys_addr_lo = instance->producer_h;
  2119. initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
  2120. init_frame->cmd = MFI_CMD_INIT;
  2121. init_frame->cmd_status = 0xFF;
  2122. init_frame->queue_info_new_phys_addr_lo = initq_info_h;
  2123. init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
  2124. /*
  2125. * disable the intr before firing the init frame to FW
  2126. */
  2127. instance->instancet->disable_intr(instance->reg_set);
  2128. /*
  2129. * Issue the init frame in polled mode
  2130. */
  2131. if (megasas_issue_polled(instance, cmd)) {
  2132. printk(KERN_ERR "megasas: Failed to init firmware\n");
  2133. megasas_return_cmd(instance, cmd);
  2134. goto fail_fw_init;
  2135. }
  2136. megasas_return_cmd(instance, cmd);
  2137. return 0;
  2138. fail_fw_init:
  2139. return -EINVAL;
  2140. }
  2141. /**
  2142. * megasas_start_timer - Initializes a timer object
  2143. * @instance: Adapter soft state
  2144. * @timer: timer object to be initialized
  2145. * @fn: timer function
  2146. * @interval: time interval between timer function call
  2147. */
  2148. static inline void
  2149. megasas_start_timer(struct megasas_instance *instance,
  2150. struct timer_list *timer,
  2151. void *fn, unsigned long interval)
  2152. {
  2153. init_timer(timer);
  2154. timer->expires = jiffies + interval;
  2155. timer->data = (unsigned long)instance;
  2156. timer->function = fn;
  2157. add_timer(timer);
  2158. }
  2159. /**
  2160. * megasas_io_completion_timer - Timer fn
  2161. * @instance_addr: Address of adapter soft state
  2162. *
  2163. * Schedules tasklet for cmd completion
  2164. * if poll_mode_io is set
  2165. */
  2166. static void
  2167. megasas_io_completion_timer(unsigned long instance_addr)
  2168. {
  2169. struct megasas_instance *instance =
  2170. (struct megasas_instance *)instance_addr;
  2171. if (atomic_read(&instance->fw_outstanding))
  2172. tasklet_schedule(&instance->isr_tasklet);
  2173. /* Restart timer */
  2174. if (poll_mode_io)
  2175. mod_timer(&instance->io_completion_timer,
  2176. jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
  2177. }
  2178. /**
  2179. * megasas_init_mfi - Initializes the FW
  2180. * @instance: Adapter soft state
  2181. *
  2182. * This is the main function for initializing MFI firmware.
  2183. */
  2184. static int megasas_init_mfi(struct megasas_instance *instance)
  2185. {
  2186. u32 context_sz;
  2187. u32 reply_q_sz;
  2188. u32 max_sectors_1;
  2189. u32 max_sectors_2;
  2190. u32 tmp_sectors;
  2191. struct megasas_register_set __iomem *reg_set;
  2192. struct megasas_ctrl_info *ctrl_info;
  2193. /*
  2194. * Map the message registers
  2195. */
  2196. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
  2197. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  2198. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2199. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0079GEN2)) {
  2200. instance->base_addr = pci_resource_start(instance->pdev, 1);
  2201. } else {
  2202. instance->base_addr = pci_resource_start(instance->pdev, 0);
  2203. }
  2204. if (pci_request_selected_regions(instance->pdev,
  2205. pci_select_bars(instance->pdev, IORESOURCE_MEM),
  2206. "megasas: LSI")) {
  2207. printk(KERN_DEBUG "megasas: IO memory region busy!\n");
  2208. return -EBUSY;
  2209. }
  2210. instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
  2211. if (!instance->reg_set) {
  2212. printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
  2213. goto fail_ioremap;
  2214. }
  2215. reg_set = instance->reg_set;
  2216. switch(instance->pdev->device)
  2217. {
  2218. case PCI_DEVICE_ID_LSI_SAS1078R:
  2219. case PCI_DEVICE_ID_LSI_SAS1078DE:
  2220. instance->instancet = &megasas_instance_template_ppc;
  2221. break;
  2222. case PCI_DEVICE_ID_LSI_SAS1078GEN2:
  2223. case PCI_DEVICE_ID_LSI_SAS0079GEN2:
  2224. instance->instancet = &megasas_instance_template_gen2;
  2225. break;
  2226. case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
  2227. case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
  2228. instance->instancet = &megasas_instance_template_skinny;
  2229. break;
  2230. case PCI_DEVICE_ID_LSI_SAS1064R:
  2231. case PCI_DEVICE_ID_DELL_PERC5:
  2232. default:
  2233. instance->instancet = &megasas_instance_template_xscale;
  2234. break;
  2235. }
  2236. /*
  2237. * We expect the FW state to be READY
  2238. */
  2239. if (megasas_transition_to_ready(instance))
  2240. goto fail_ready_state;
  2241. /*
  2242. * Get various operational parameters from status register
  2243. */
  2244. instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
  2245. /*
  2246. * Reduce the max supported cmds by 1. This is to ensure that the
  2247. * reply_q_sz (1 more than the max cmd that driver may send)
  2248. * does not exceed max cmds that the FW can support
  2249. */
  2250. instance->max_fw_cmds = instance->max_fw_cmds-1;
  2251. instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
  2252. 0x10;
  2253. /*
  2254. * Create a pool of commands
  2255. */
  2256. if (megasas_alloc_cmds(instance))
  2257. goto fail_alloc_cmds;
  2258. /*
  2259. * Allocate memory for reply queue. Length of reply queue should
  2260. * be _one_ more than the maximum commands handled by the firmware.
  2261. *
  2262. * Note: When FW completes commands, it places corresponding contex
  2263. * values in this circular reply queue. This circular queue is a fairly
  2264. * typical producer-consumer queue. FW is the producer (of completed
  2265. * commands) and the driver is the consumer.
  2266. */
  2267. context_sz = sizeof(u32);
  2268. reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
  2269. instance->reply_queue = pci_alloc_consistent(instance->pdev,
  2270. reply_q_sz,
  2271. &instance->reply_queue_h);
  2272. if (!instance->reply_queue) {
  2273. printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
  2274. goto fail_reply_queue;
  2275. }
  2276. if (megasas_issue_init_mfi(instance))
  2277. goto fail_fw_init;
  2278. memset(instance->pd_list, 0 ,
  2279. (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
  2280. megasas_get_pd_list(instance);
  2281. memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
  2282. megasas_get_ld_list(instance);
  2283. ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
  2284. /*
  2285. * Compute the max allowed sectors per IO: The controller info has two
  2286. * limits on max sectors. Driver should use the minimum of these two.
  2287. *
  2288. * 1 << stripe_sz_ops.min = max sectors per strip
  2289. *
  2290. * Note that older firmwares ( < FW ver 30) didn't report information
  2291. * to calculate max_sectors_1. So the number ended up as zero always.
  2292. */
  2293. tmp_sectors = 0;
  2294. if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
  2295. max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
  2296. ctrl_info->max_strips_per_io;
  2297. max_sectors_2 = ctrl_info->max_request_size;
  2298. tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
  2299. }
  2300. instance->max_sectors_per_req = instance->max_num_sge *
  2301. PAGE_SIZE / 512;
  2302. if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
  2303. instance->max_sectors_per_req = tmp_sectors;
  2304. kfree(ctrl_info);
  2305. /*
  2306. * Setup tasklet for cmd completion
  2307. */
  2308. tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
  2309. (unsigned long)instance);
  2310. /* Initialize the cmd completion timer */
  2311. if (poll_mode_io)
  2312. megasas_start_timer(instance, &instance->io_completion_timer,
  2313. megasas_io_completion_timer,
  2314. MEGASAS_COMPLETION_TIMER_INTERVAL);
  2315. return 0;
  2316. fail_fw_init:
  2317. pci_free_consistent(instance->pdev, reply_q_sz,
  2318. instance->reply_queue, instance->reply_queue_h);
  2319. fail_reply_queue:
  2320. megasas_free_cmds(instance);
  2321. fail_alloc_cmds:
  2322. fail_ready_state:
  2323. iounmap(instance->reg_set);
  2324. fail_ioremap:
  2325. pci_release_selected_regions(instance->pdev,
  2326. pci_select_bars(instance->pdev, IORESOURCE_MEM));
  2327. return -EINVAL;
  2328. }
  2329. /**
  2330. * megasas_release_mfi - Reverses the FW initialization
  2331. * @intance: Adapter soft state
  2332. */
  2333. static void megasas_release_mfi(struct megasas_instance *instance)
  2334. {
  2335. u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
  2336. pci_free_consistent(instance->pdev, reply_q_sz,
  2337. instance->reply_queue, instance->reply_queue_h);
  2338. megasas_free_cmds(instance);
  2339. iounmap(instance->reg_set);
  2340. pci_release_selected_regions(instance->pdev,
  2341. pci_select_bars(instance->pdev, IORESOURCE_MEM));
  2342. }
  2343. /**
  2344. * megasas_get_seq_num - Gets latest event sequence numbers
  2345. * @instance: Adapter soft state
  2346. * @eli: FW event log sequence numbers information
  2347. *
  2348. * FW maintains a log of all events in a non-volatile area. Upper layers would
  2349. * usually find out the latest sequence number of the events, the seq number at
  2350. * the boot etc. They would "read" all the events below the latest seq number
  2351. * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
  2352. * number), they would subsribe to AEN (asynchronous event notification) and
  2353. * wait for the events to happen.
  2354. */
  2355. static int
  2356. megasas_get_seq_num(struct megasas_instance *instance,
  2357. struct megasas_evt_log_info *eli)
  2358. {
  2359. struct megasas_cmd *cmd;
  2360. struct megasas_dcmd_frame *dcmd;
  2361. struct megasas_evt_log_info *el_info;
  2362. dma_addr_t el_info_h = 0;
  2363. cmd = megasas_get_cmd(instance);
  2364. if (!cmd) {
  2365. return -ENOMEM;
  2366. }
  2367. dcmd = &cmd->frame->dcmd;
  2368. el_info = pci_alloc_consistent(instance->pdev,
  2369. sizeof(struct megasas_evt_log_info),
  2370. &el_info_h);
  2371. if (!el_info) {
  2372. megasas_return_cmd(instance, cmd);
  2373. return -ENOMEM;
  2374. }
  2375. memset(el_info, 0, sizeof(*el_info));
  2376. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2377. dcmd->cmd = MFI_CMD_DCMD;
  2378. dcmd->cmd_status = 0x0;
  2379. dcmd->sge_count = 1;
  2380. dcmd->flags = MFI_FRAME_DIR_READ;
  2381. dcmd->timeout = 0;
  2382. dcmd->pad_0 = 0;
  2383. dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
  2384. dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
  2385. dcmd->sgl.sge32[0].phys_addr = el_info_h;
  2386. dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
  2387. megasas_issue_blocked_cmd(instance, cmd);
  2388. /*
  2389. * Copy the data back into callers buffer
  2390. */
  2391. memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
  2392. pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
  2393. el_info, el_info_h);
  2394. megasas_return_cmd(instance, cmd);
  2395. return 0;
  2396. }
  2397. /**
  2398. * megasas_register_aen - Registers for asynchronous event notification
  2399. * @instance: Adapter soft state
  2400. * @seq_num: The starting sequence number
  2401. * @class_locale: Class of the event
  2402. *
  2403. * This function subscribes for AEN for events beyond the @seq_num. It requests
  2404. * to be notified if and only if the event is of type @class_locale
  2405. */
  2406. static int
  2407. megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
  2408. u32 class_locale_word)
  2409. {
  2410. int ret_val;
  2411. struct megasas_cmd *cmd;
  2412. struct megasas_dcmd_frame *dcmd;
  2413. union megasas_evt_class_locale curr_aen;
  2414. union megasas_evt_class_locale prev_aen;
  2415. /*
  2416. * If there an AEN pending already (aen_cmd), check if the
  2417. * class_locale of that pending AEN is inclusive of the new
  2418. * AEN request we currently have. If it is, then we don't have
  2419. * to do anything. In other words, whichever events the current
  2420. * AEN request is subscribing to, have already been subscribed
  2421. * to.
  2422. *
  2423. * If the old_cmd is _not_ inclusive, then we have to abort
  2424. * that command, form a class_locale that is superset of both
  2425. * old and current and re-issue to the FW
  2426. */
  2427. curr_aen.word = class_locale_word;
  2428. if (instance->aen_cmd) {
  2429. prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
  2430. /*
  2431. * A class whose enum value is smaller is inclusive of all
  2432. * higher values. If a PROGRESS (= -1) was previously
  2433. * registered, then a new registration requests for higher
  2434. * classes need not be sent to FW. They are automatically
  2435. * included.
  2436. *
  2437. * Locale numbers don't have such hierarchy. They are bitmap
  2438. * values
  2439. */
  2440. if ((prev_aen.members.class <= curr_aen.members.class) &&
  2441. !((prev_aen.members.locale & curr_aen.members.locale) ^
  2442. curr_aen.members.locale)) {
  2443. /*
  2444. * Previously issued event registration includes
  2445. * current request. Nothing to do.
  2446. */
  2447. return 0;
  2448. } else {
  2449. curr_aen.members.locale |= prev_aen.members.locale;
  2450. if (prev_aen.members.class < curr_aen.members.class)
  2451. curr_aen.members.class = prev_aen.members.class;
  2452. instance->aen_cmd->abort_aen = 1;
  2453. ret_val = megasas_issue_blocked_abort_cmd(instance,
  2454. instance->
  2455. aen_cmd);
  2456. if (ret_val) {
  2457. printk(KERN_DEBUG "megasas: Failed to abort "
  2458. "previous AEN command\n");
  2459. return ret_val;
  2460. }
  2461. }
  2462. }
  2463. cmd = megasas_get_cmd(instance);
  2464. if (!cmd)
  2465. return -ENOMEM;
  2466. dcmd = &cmd->frame->dcmd;
  2467. memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
  2468. /*
  2469. * Prepare DCMD for aen registration
  2470. */
  2471. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2472. dcmd->cmd = MFI_CMD_DCMD;
  2473. dcmd->cmd_status = 0x0;
  2474. dcmd->sge_count = 1;
  2475. dcmd->flags = MFI_FRAME_DIR_READ;
  2476. dcmd->timeout = 0;
  2477. dcmd->pad_0 = 0;
  2478. dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
  2479. dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
  2480. dcmd->mbox.w[0] = seq_num;
  2481. dcmd->mbox.w[1] = curr_aen.word;
  2482. dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
  2483. dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
  2484. if (instance->aen_cmd != NULL) {
  2485. megasas_return_cmd(instance, cmd);
  2486. return 0;
  2487. }
  2488. /*
  2489. * Store reference to the cmd used to register for AEN. When an
  2490. * application wants us to register for AEN, we have to abort this
  2491. * cmd and re-register with a new EVENT LOCALE supplied by that app
  2492. */
  2493. instance->aen_cmd = cmd;
  2494. /*
  2495. * Issue the aen registration frame
  2496. */
  2497. instance->instancet->fire_cmd(instance,
  2498. cmd->frame_phys_addr, 0, instance->reg_set);
  2499. return 0;
  2500. }
  2501. /**
  2502. * megasas_start_aen - Subscribes to AEN during driver load time
  2503. * @instance: Adapter soft state
  2504. */
  2505. static int megasas_start_aen(struct megasas_instance *instance)
  2506. {
  2507. struct megasas_evt_log_info eli;
  2508. union megasas_evt_class_locale class_locale;
  2509. /*
  2510. * Get the latest sequence number from FW
  2511. */
  2512. memset(&eli, 0, sizeof(eli));
  2513. if (megasas_get_seq_num(instance, &eli))
  2514. return -1;
  2515. /*
  2516. * Register AEN with FW for latest sequence number plus 1
  2517. */
  2518. class_locale.members.reserved = 0;
  2519. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  2520. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  2521. return megasas_register_aen(instance, eli.newest_seq_num + 1,
  2522. class_locale.word);
  2523. }
  2524. /**
  2525. * megasas_io_attach - Attaches this driver to SCSI mid-layer
  2526. * @instance: Adapter soft state
  2527. */
  2528. static int megasas_io_attach(struct megasas_instance *instance)
  2529. {
  2530. struct Scsi_Host *host = instance->host;
  2531. /*
  2532. * Export parameters required by SCSI mid-layer
  2533. */
  2534. host->irq = instance->pdev->irq;
  2535. host->unique_id = instance->unique_id;
  2536. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2537. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  2538. host->can_queue =
  2539. instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
  2540. } else
  2541. host->can_queue =
  2542. instance->max_fw_cmds - MEGASAS_INT_CMDS;
  2543. host->this_id = instance->init_id;
  2544. host->sg_tablesize = instance->max_num_sge;
  2545. host->max_sectors = instance->max_sectors_per_req;
  2546. host->cmd_per_lun = 128;
  2547. host->max_channel = MEGASAS_MAX_CHANNELS - 1;
  2548. host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
  2549. host->max_lun = MEGASAS_MAX_LUN;
  2550. host->max_cmd_len = 16;
  2551. /*
  2552. * Notify the mid-layer about the new controller
  2553. */
  2554. if (scsi_add_host(host, &instance->pdev->dev)) {
  2555. printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
  2556. return -ENODEV;
  2557. }
  2558. /*
  2559. * Trigger SCSI to scan our drives
  2560. */
  2561. scsi_scan_host(host);
  2562. return 0;
  2563. }
  2564. static int
  2565. megasas_set_dma_mask(struct pci_dev *pdev)
  2566. {
  2567. /*
  2568. * All our contollers are capable of performing 64-bit DMA
  2569. */
  2570. if (IS_DMA64) {
  2571. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
  2572. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
  2573. goto fail_set_dma_mask;
  2574. }
  2575. } else {
  2576. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
  2577. goto fail_set_dma_mask;
  2578. }
  2579. return 0;
  2580. fail_set_dma_mask:
  2581. return 1;
  2582. }
  2583. /**
  2584. * megasas_probe_one - PCI hotplug entry point
  2585. * @pdev: PCI device structure
  2586. * @id: PCI ids of supported hotplugged adapter
  2587. */
  2588. static int __devinit
  2589. megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  2590. {
  2591. int rval;
  2592. struct Scsi_Host *host;
  2593. struct megasas_instance *instance;
  2594. /*
  2595. * Announce PCI information
  2596. */
  2597. printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
  2598. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  2599. pdev->subsystem_device);
  2600. printk("bus %d:slot %d:func %d\n",
  2601. pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  2602. /*
  2603. * PCI prepping: enable device set bus mastering and dma mask
  2604. */
  2605. rval = pci_enable_device_mem(pdev);
  2606. if (rval) {
  2607. return rval;
  2608. }
  2609. pci_set_master(pdev);
  2610. if (megasas_set_dma_mask(pdev))
  2611. goto fail_set_dma_mask;
  2612. host = scsi_host_alloc(&megasas_template,
  2613. sizeof(struct megasas_instance));
  2614. if (!host) {
  2615. printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
  2616. goto fail_alloc_instance;
  2617. }
  2618. instance = (struct megasas_instance *)host->hostdata;
  2619. memset(instance, 0, sizeof(*instance));
  2620. instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
  2621. &instance->producer_h);
  2622. instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
  2623. &instance->consumer_h);
  2624. if (!instance->producer || !instance->consumer) {
  2625. printk(KERN_DEBUG "megasas: Failed to allocate memory for "
  2626. "producer, consumer\n");
  2627. goto fail_alloc_dma_buf;
  2628. }
  2629. *instance->producer = 0;
  2630. *instance->consumer = 0;
  2631. megasas_poll_wait_aen = 0;
  2632. instance->flag_ieee = 0;
  2633. instance->ev = NULL;
  2634. instance->evt_detail = pci_alloc_consistent(pdev,
  2635. sizeof(struct
  2636. megasas_evt_detail),
  2637. &instance->evt_detail_h);
  2638. if (!instance->evt_detail) {
  2639. printk(KERN_DEBUG "megasas: Failed to allocate memory for "
  2640. "event detail structure\n");
  2641. goto fail_alloc_dma_buf;
  2642. }
  2643. /*
  2644. * Initialize locks and queues
  2645. */
  2646. INIT_LIST_HEAD(&instance->cmd_pool);
  2647. atomic_set(&instance->fw_outstanding,0);
  2648. init_waitqueue_head(&instance->int_cmd_wait_q);
  2649. init_waitqueue_head(&instance->abort_cmd_wait_q);
  2650. spin_lock_init(&instance->cmd_pool_lock);
  2651. spin_lock_init(&instance->fire_lock);
  2652. spin_lock_init(&instance->completion_lock);
  2653. spin_lock_init(&poll_aen_lock);
  2654. mutex_init(&instance->aen_mutex);
  2655. /*
  2656. * Initialize PCI related and misc parameters
  2657. */
  2658. instance->pdev = pdev;
  2659. instance->host = host;
  2660. instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
  2661. instance->init_id = MEGASAS_DEFAULT_INIT_ID;
  2662. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2663. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  2664. instance->flag_ieee = 1;
  2665. sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
  2666. } else
  2667. sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
  2668. megasas_dbg_lvl = 0;
  2669. instance->flag = 0;
  2670. instance->unload = 1;
  2671. instance->last_time = 0;
  2672. /*
  2673. * Initialize MFI Firmware
  2674. */
  2675. if (megasas_init_mfi(instance))
  2676. goto fail_init_mfi;
  2677. /*
  2678. * Register IRQ
  2679. */
  2680. if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
  2681. printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
  2682. goto fail_irq;
  2683. }
  2684. instance->instancet->enable_intr(instance->reg_set);
  2685. /*
  2686. * Store instance in PCI softstate
  2687. */
  2688. pci_set_drvdata(pdev, instance);
  2689. /*
  2690. * Add this controller to megasas_mgmt_info structure so that it
  2691. * can be exported to management applications
  2692. */
  2693. megasas_mgmt_info.count++;
  2694. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
  2695. megasas_mgmt_info.max_index++;
  2696. /*
  2697. * Initiate AEN (Asynchronous Event Notification)
  2698. */
  2699. if (megasas_start_aen(instance)) {
  2700. printk(KERN_DEBUG "megasas: start aen failed\n");
  2701. goto fail_start_aen;
  2702. }
  2703. /*
  2704. * Register with SCSI mid-layer
  2705. */
  2706. if (megasas_io_attach(instance))
  2707. goto fail_io_attach;
  2708. instance->unload = 0;
  2709. return 0;
  2710. fail_start_aen:
  2711. fail_io_attach:
  2712. megasas_mgmt_info.count--;
  2713. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
  2714. megasas_mgmt_info.max_index--;
  2715. pci_set_drvdata(pdev, NULL);
  2716. instance->instancet->disable_intr(instance->reg_set);
  2717. free_irq(instance->pdev->irq, instance);
  2718. megasas_release_mfi(instance);
  2719. fail_irq:
  2720. fail_init_mfi:
  2721. fail_alloc_dma_buf:
  2722. if (instance->evt_detail)
  2723. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  2724. instance->evt_detail,
  2725. instance->evt_detail_h);
  2726. if (instance->producer)
  2727. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  2728. instance->producer_h);
  2729. if (instance->consumer)
  2730. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  2731. instance->consumer_h);
  2732. scsi_host_put(host);
  2733. fail_alloc_instance:
  2734. fail_set_dma_mask:
  2735. pci_disable_device(pdev);
  2736. return -ENODEV;
  2737. }
  2738. /**
  2739. * megasas_flush_cache - Requests FW to flush all its caches
  2740. * @instance: Adapter soft state
  2741. */
  2742. static void megasas_flush_cache(struct megasas_instance *instance)
  2743. {
  2744. struct megasas_cmd *cmd;
  2745. struct megasas_dcmd_frame *dcmd;
  2746. cmd = megasas_get_cmd(instance);
  2747. if (!cmd)
  2748. return;
  2749. dcmd = &cmd->frame->dcmd;
  2750. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2751. dcmd->cmd = MFI_CMD_DCMD;
  2752. dcmd->cmd_status = 0x0;
  2753. dcmd->sge_count = 0;
  2754. dcmd->flags = MFI_FRAME_DIR_NONE;
  2755. dcmd->timeout = 0;
  2756. dcmd->pad_0 = 0;
  2757. dcmd->data_xfer_len = 0;
  2758. dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
  2759. dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
  2760. megasas_issue_blocked_cmd(instance, cmd);
  2761. megasas_return_cmd(instance, cmd);
  2762. return;
  2763. }
  2764. /**
  2765. * megasas_shutdown_controller - Instructs FW to shutdown the controller
  2766. * @instance: Adapter soft state
  2767. * @opcode: Shutdown/Hibernate
  2768. */
  2769. static void megasas_shutdown_controller(struct megasas_instance *instance,
  2770. u32 opcode)
  2771. {
  2772. struct megasas_cmd *cmd;
  2773. struct megasas_dcmd_frame *dcmd;
  2774. cmd = megasas_get_cmd(instance);
  2775. if (!cmd)
  2776. return;
  2777. if (instance->aen_cmd)
  2778. megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
  2779. dcmd = &cmd->frame->dcmd;
  2780. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2781. dcmd->cmd = MFI_CMD_DCMD;
  2782. dcmd->cmd_status = 0x0;
  2783. dcmd->sge_count = 0;
  2784. dcmd->flags = MFI_FRAME_DIR_NONE;
  2785. dcmd->timeout = 0;
  2786. dcmd->pad_0 = 0;
  2787. dcmd->data_xfer_len = 0;
  2788. dcmd->opcode = opcode;
  2789. megasas_issue_blocked_cmd(instance, cmd);
  2790. megasas_return_cmd(instance, cmd);
  2791. return;
  2792. }
  2793. #ifdef CONFIG_PM
  2794. /**
  2795. * megasas_suspend - driver suspend entry point
  2796. * @pdev: PCI device structure
  2797. * @state: PCI power state to suspend routine
  2798. */
  2799. static int
  2800. megasas_suspend(struct pci_dev *pdev, pm_message_t state)
  2801. {
  2802. struct Scsi_Host *host;
  2803. struct megasas_instance *instance;
  2804. instance = pci_get_drvdata(pdev);
  2805. host = instance->host;
  2806. instance->unload = 1;
  2807. if (poll_mode_io)
  2808. del_timer_sync(&instance->io_completion_timer);
  2809. megasas_flush_cache(instance);
  2810. megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
  2811. /* cancel the delayed work if this work still in queue */
  2812. if (instance->ev != NULL) {
  2813. struct megasas_aen_event *ev = instance->ev;
  2814. cancel_delayed_work(
  2815. (struct delayed_work *)&ev->hotplug_work);
  2816. flush_scheduled_work();
  2817. instance->ev = NULL;
  2818. }
  2819. tasklet_kill(&instance->isr_tasklet);
  2820. pci_set_drvdata(instance->pdev, instance);
  2821. instance->instancet->disable_intr(instance->reg_set);
  2822. free_irq(instance->pdev->irq, instance);
  2823. pci_save_state(pdev);
  2824. pci_disable_device(pdev);
  2825. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  2826. return 0;
  2827. }
  2828. /**
  2829. * megasas_resume- driver resume entry point
  2830. * @pdev: PCI device structure
  2831. */
  2832. static int
  2833. megasas_resume(struct pci_dev *pdev)
  2834. {
  2835. int rval;
  2836. struct Scsi_Host *host;
  2837. struct megasas_instance *instance;
  2838. instance = pci_get_drvdata(pdev);
  2839. host = instance->host;
  2840. pci_set_power_state(pdev, PCI_D0);
  2841. pci_enable_wake(pdev, PCI_D0, 0);
  2842. pci_restore_state(pdev);
  2843. /*
  2844. * PCI prepping: enable device set bus mastering and dma mask
  2845. */
  2846. rval = pci_enable_device_mem(pdev);
  2847. if (rval) {
  2848. printk(KERN_ERR "megasas: Enable device failed\n");
  2849. return rval;
  2850. }
  2851. pci_set_master(pdev);
  2852. if (megasas_set_dma_mask(pdev))
  2853. goto fail_set_dma_mask;
  2854. /*
  2855. * Initialize MFI Firmware
  2856. */
  2857. *instance->producer = 0;
  2858. *instance->consumer = 0;
  2859. atomic_set(&instance->fw_outstanding, 0);
  2860. /*
  2861. * We expect the FW state to be READY
  2862. */
  2863. if (megasas_transition_to_ready(instance))
  2864. goto fail_ready_state;
  2865. if (megasas_issue_init_mfi(instance))
  2866. goto fail_init_mfi;
  2867. tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
  2868. (unsigned long)instance);
  2869. /*
  2870. * Register IRQ
  2871. */
  2872. if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED,
  2873. "megasas", instance)) {
  2874. printk(KERN_ERR "megasas: Failed to register IRQ\n");
  2875. goto fail_irq;
  2876. }
  2877. instance->instancet->enable_intr(instance->reg_set);
  2878. /*
  2879. * Initiate AEN (Asynchronous Event Notification)
  2880. */
  2881. if (megasas_start_aen(instance))
  2882. printk(KERN_ERR "megasas: Start AEN failed\n");
  2883. /* Initialize the cmd completion timer */
  2884. if (poll_mode_io)
  2885. megasas_start_timer(instance, &instance->io_completion_timer,
  2886. megasas_io_completion_timer,
  2887. MEGASAS_COMPLETION_TIMER_INTERVAL);
  2888. instance->unload = 0;
  2889. return 0;
  2890. fail_irq:
  2891. fail_init_mfi:
  2892. if (instance->evt_detail)
  2893. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  2894. instance->evt_detail,
  2895. instance->evt_detail_h);
  2896. if (instance->producer)
  2897. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  2898. instance->producer_h);
  2899. if (instance->consumer)
  2900. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  2901. instance->consumer_h);
  2902. scsi_host_put(host);
  2903. fail_set_dma_mask:
  2904. fail_ready_state:
  2905. pci_disable_device(pdev);
  2906. return -ENODEV;
  2907. }
  2908. #else
  2909. #define megasas_suspend NULL
  2910. #define megasas_resume NULL
  2911. #endif
  2912. /**
  2913. * megasas_detach_one - PCI hot"un"plug entry point
  2914. * @pdev: PCI device structure
  2915. */
  2916. static void __devexit megasas_detach_one(struct pci_dev *pdev)
  2917. {
  2918. int i;
  2919. struct Scsi_Host *host;
  2920. struct megasas_instance *instance;
  2921. instance = pci_get_drvdata(pdev);
  2922. instance->unload = 1;
  2923. host = instance->host;
  2924. if (poll_mode_io)
  2925. del_timer_sync(&instance->io_completion_timer);
  2926. scsi_remove_host(instance->host);
  2927. megasas_flush_cache(instance);
  2928. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  2929. /* cancel the delayed work if this work still in queue*/
  2930. if (instance->ev != NULL) {
  2931. struct megasas_aen_event *ev = instance->ev;
  2932. cancel_delayed_work(
  2933. (struct delayed_work *)&ev->hotplug_work);
  2934. flush_scheduled_work();
  2935. instance->ev = NULL;
  2936. }
  2937. tasklet_kill(&instance->isr_tasklet);
  2938. /*
  2939. * Take the instance off the instance array. Note that we will not
  2940. * decrement the max_index. We let this array be sparse array
  2941. */
  2942. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  2943. if (megasas_mgmt_info.instance[i] == instance) {
  2944. megasas_mgmt_info.count--;
  2945. megasas_mgmt_info.instance[i] = NULL;
  2946. break;
  2947. }
  2948. }
  2949. pci_set_drvdata(instance->pdev, NULL);
  2950. instance->instancet->disable_intr(instance->reg_set);
  2951. free_irq(instance->pdev->irq, instance);
  2952. megasas_release_mfi(instance);
  2953. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  2954. instance->evt_detail, instance->evt_detail_h);
  2955. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  2956. instance->producer_h);
  2957. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  2958. instance->consumer_h);
  2959. scsi_host_put(host);
  2960. pci_set_drvdata(pdev, NULL);
  2961. pci_disable_device(pdev);
  2962. return;
  2963. }
  2964. /**
  2965. * megasas_shutdown - Shutdown entry point
  2966. * @device: Generic device structure
  2967. */
  2968. static void megasas_shutdown(struct pci_dev *pdev)
  2969. {
  2970. struct megasas_instance *instance = pci_get_drvdata(pdev);
  2971. instance->unload = 1;
  2972. megasas_flush_cache(instance);
  2973. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  2974. }
  2975. /**
  2976. * megasas_mgmt_open - char node "open" entry point
  2977. */
  2978. static int megasas_mgmt_open(struct inode *inode, struct file *filep)
  2979. {
  2980. /*
  2981. * Allow only those users with admin rights
  2982. */
  2983. if (!capable(CAP_SYS_ADMIN))
  2984. return -EACCES;
  2985. return 0;
  2986. }
  2987. /**
  2988. * megasas_mgmt_fasync - Async notifier registration from applications
  2989. *
  2990. * This function adds the calling process to a driver global queue. When an
  2991. * event occurs, SIGIO will be sent to all processes in this queue.
  2992. */
  2993. static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
  2994. {
  2995. int rc;
  2996. mutex_lock(&megasas_async_queue_mutex);
  2997. rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
  2998. mutex_unlock(&megasas_async_queue_mutex);
  2999. if (rc >= 0) {
  3000. /* For sanity check when we get ioctl */
  3001. filep->private_data = filep;
  3002. return 0;
  3003. }
  3004. printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
  3005. return rc;
  3006. }
  3007. /**
  3008. * megasas_mgmt_poll - char node "poll" entry point
  3009. * */
  3010. static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
  3011. {
  3012. unsigned int mask;
  3013. unsigned long flags;
  3014. poll_wait(file, &megasas_poll_wait, wait);
  3015. spin_lock_irqsave(&poll_aen_lock, flags);
  3016. if (megasas_poll_wait_aen)
  3017. mask = (POLLIN | POLLRDNORM);
  3018. else
  3019. mask = 0;
  3020. spin_unlock_irqrestore(&poll_aen_lock, flags);
  3021. return mask;
  3022. }
  3023. /**
  3024. * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
  3025. * @instance: Adapter soft state
  3026. * @argp: User's ioctl packet
  3027. */
  3028. static int
  3029. megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
  3030. struct megasas_iocpacket __user * user_ioc,
  3031. struct megasas_iocpacket *ioc)
  3032. {
  3033. struct megasas_sge32 *kern_sge32;
  3034. struct megasas_cmd *cmd;
  3035. void *kbuff_arr[MAX_IOCTL_SGE];
  3036. dma_addr_t buf_handle = 0;
  3037. int error = 0, i;
  3038. void *sense = NULL;
  3039. dma_addr_t sense_handle;
  3040. unsigned long *sense_ptr;
  3041. memset(kbuff_arr, 0, sizeof(kbuff_arr));
  3042. if (ioc->sge_count > MAX_IOCTL_SGE) {
  3043. printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
  3044. ioc->sge_count, MAX_IOCTL_SGE);
  3045. return -EINVAL;
  3046. }
  3047. cmd = megasas_get_cmd(instance);
  3048. if (!cmd) {
  3049. printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
  3050. return -ENOMEM;
  3051. }
  3052. /*
  3053. * User's IOCTL packet has 2 frames (maximum). Copy those two
  3054. * frames into our cmd's frames. cmd->frame's context will get
  3055. * overwritten when we copy from user's frames. So set that value
  3056. * alone separately
  3057. */
  3058. memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
  3059. cmd->frame->hdr.context = cmd->index;
  3060. cmd->frame->hdr.pad_0 = 0;
  3061. /*
  3062. * The management interface between applications and the fw uses
  3063. * MFI frames. E.g, RAID configuration changes, LD property changes
  3064. * etc are accomplishes through different kinds of MFI frames. The
  3065. * driver needs to care only about substituting user buffers with
  3066. * kernel buffers in SGLs. The location of SGL is embedded in the
  3067. * struct iocpacket itself.
  3068. */
  3069. kern_sge32 = (struct megasas_sge32 *)
  3070. ((unsigned long)cmd->frame + ioc->sgl_off);
  3071. /*
  3072. * For each user buffer, create a mirror buffer and copy in
  3073. */
  3074. for (i = 0; i < ioc->sge_count; i++) {
  3075. kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
  3076. ioc->sgl[i].iov_len,
  3077. &buf_handle, GFP_KERNEL);
  3078. if (!kbuff_arr[i]) {
  3079. printk(KERN_DEBUG "megasas: Failed to alloc "
  3080. "kernel SGL buffer for IOCTL \n");
  3081. error = -ENOMEM;
  3082. goto out;
  3083. }
  3084. /*
  3085. * We don't change the dma_coherent_mask, so
  3086. * pci_alloc_consistent only returns 32bit addresses
  3087. */
  3088. kern_sge32[i].phys_addr = (u32) buf_handle;
  3089. kern_sge32[i].length = ioc->sgl[i].iov_len;
  3090. /*
  3091. * We created a kernel buffer corresponding to the
  3092. * user buffer. Now copy in from the user buffer
  3093. */
  3094. if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
  3095. (u32) (ioc->sgl[i].iov_len))) {
  3096. error = -EFAULT;
  3097. goto out;
  3098. }
  3099. }
  3100. if (ioc->sense_len) {
  3101. sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
  3102. &sense_handle, GFP_KERNEL);
  3103. if (!sense) {
  3104. error = -ENOMEM;
  3105. goto out;
  3106. }
  3107. sense_ptr =
  3108. (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
  3109. *sense_ptr = sense_handle;
  3110. }
  3111. /*
  3112. * Set the sync_cmd flag so that the ISR knows not to complete this
  3113. * cmd to the SCSI mid-layer
  3114. */
  3115. cmd->sync_cmd = 1;
  3116. megasas_issue_blocked_cmd(instance, cmd);
  3117. cmd->sync_cmd = 0;
  3118. /*
  3119. * copy out the kernel buffers to user buffers
  3120. */
  3121. for (i = 0; i < ioc->sge_count; i++) {
  3122. if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
  3123. ioc->sgl[i].iov_len)) {
  3124. error = -EFAULT;
  3125. goto out;
  3126. }
  3127. }
  3128. /*
  3129. * copy out the sense
  3130. */
  3131. if (ioc->sense_len) {
  3132. /*
  3133. * sense_ptr points to the location that has the user
  3134. * sense buffer address
  3135. */
  3136. sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
  3137. ioc->sense_off);
  3138. if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
  3139. sense, ioc->sense_len)) {
  3140. printk(KERN_ERR "megasas: Failed to copy out to user "
  3141. "sense data\n");
  3142. error = -EFAULT;
  3143. goto out;
  3144. }
  3145. }
  3146. /*
  3147. * copy the status codes returned by the fw
  3148. */
  3149. if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
  3150. &cmd->frame->hdr.cmd_status, sizeof(u8))) {
  3151. printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
  3152. error = -EFAULT;
  3153. }
  3154. out:
  3155. if (sense) {
  3156. dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
  3157. sense, sense_handle);
  3158. }
  3159. for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
  3160. dma_free_coherent(&instance->pdev->dev,
  3161. kern_sge32[i].length,
  3162. kbuff_arr[i], kern_sge32[i].phys_addr);
  3163. }
  3164. megasas_return_cmd(instance, cmd);
  3165. return error;
  3166. }
  3167. static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
  3168. {
  3169. struct megasas_iocpacket __user *user_ioc =
  3170. (struct megasas_iocpacket __user *)arg;
  3171. struct megasas_iocpacket *ioc;
  3172. struct megasas_instance *instance;
  3173. int error;
  3174. ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
  3175. if (!ioc)
  3176. return -ENOMEM;
  3177. if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
  3178. error = -EFAULT;
  3179. goto out_kfree_ioc;
  3180. }
  3181. instance = megasas_lookup_instance(ioc->host_no);
  3182. if (!instance) {
  3183. error = -ENODEV;
  3184. goto out_kfree_ioc;
  3185. }
  3186. if (instance->hw_crit_error == 1) {
  3187. printk(KERN_DEBUG "Controller in Crit ERROR\n");
  3188. error = -ENODEV;
  3189. goto out_kfree_ioc;
  3190. }
  3191. if (instance->unload == 1) {
  3192. error = -ENODEV;
  3193. goto out_kfree_ioc;
  3194. }
  3195. /*
  3196. * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
  3197. */
  3198. if (down_interruptible(&instance->ioctl_sem)) {
  3199. error = -ERESTARTSYS;
  3200. goto out_kfree_ioc;
  3201. }
  3202. error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
  3203. up(&instance->ioctl_sem);
  3204. out_kfree_ioc:
  3205. kfree(ioc);
  3206. return error;
  3207. }
  3208. static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
  3209. {
  3210. struct megasas_instance *instance;
  3211. struct megasas_aen aen;
  3212. int error;
  3213. if (file->private_data != file) {
  3214. printk(KERN_DEBUG "megasas: fasync_helper was not "
  3215. "called first\n");
  3216. return -EINVAL;
  3217. }
  3218. if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
  3219. return -EFAULT;
  3220. instance = megasas_lookup_instance(aen.host_no);
  3221. if (!instance)
  3222. return -ENODEV;
  3223. if (instance->hw_crit_error == 1) {
  3224. error = -ENODEV;
  3225. }
  3226. if (instance->unload == 1) {
  3227. return -ENODEV;
  3228. }
  3229. mutex_lock(&instance->aen_mutex);
  3230. error = megasas_register_aen(instance, aen.seq_num,
  3231. aen.class_locale_word);
  3232. mutex_unlock(&instance->aen_mutex);
  3233. return error;
  3234. }
  3235. /**
  3236. * megasas_mgmt_ioctl - char node ioctl entry point
  3237. */
  3238. static long
  3239. megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  3240. {
  3241. switch (cmd) {
  3242. case MEGASAS_IOC_FIRMWARE:
  3243. return megasas_mgmt_ioctl_fw(file, arg);
  3244. case MEGASAS_IOC_GET_AEN:
  3245. return megasas_mgmt_ioctl_aen(file, arg);
  3246. }
  3247. return -ENOTTY;
  3248. }
  3249. #ifdef CONFIG_COMPAT
  3250. static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
  3251. {
  3252. struct compat_megasas_iocpacket __user *cioc =
  3253. (struct compat_megasas_iocpacket __user *)arg;
  3254. struct megasas_iocpacket __user *ioc =
  3255. compat_alloc_user_space(sizeof(struct megasas_iocpacket));
  3256. int i;
  3257. int error = 0;
  3258. compat_uptr_t ptr;
  3259. if (clear_user(ioc, sizeof(*ioc)))
  3260. return -EFAULT;
  3261. if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
  3262. copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
  3263. copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
  3264. copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
  3265. copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
  3266. copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
  3267. return -EFAULT;
  3268. /*
  3269. * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
  3270. * sense_len is not null, so prepare the 64bit value under
  3271. * the same condition.
  3272. */
  3273. if (ioc->sense_len) {
  3274. void __user **sense_ioc_ptr =
  3275. (void __user **)(ioc->frame.raw + ioc->sense_off);
  3276. compat_uptr_t *sense_cioc_ptr =
  3277. (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
  3278. if (get_user(ptr, sense_cioc_ptr) ||
  3279. put_user(compat_ptr(ptr), sense_ioc_ptr))
  3280. return -EFAULT;
  3281. }
  3282. for (i = 0; i < MAX_IOCTL_SGE; i++) {
  3283. if (get_user(ptr, &cioc->sgl[i].iov_base) ||
  3284. put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
  3285. copy_in_user(&ioc->sgl[i].iov_len,
  3286. &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
  3287. return -EFAULT;
  3288. }
  3289. error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
  3290. if (copy_in_user(&cioc->frame.hdr.cmd_status,
  3291. &ioc->frame.hdr.cmd_status, sizeof(u8))) {
  3292. printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
  3293. return -EFAULT;
  3294. }
  3295. return error;
  3296. }
  3297. static long
  3298. megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
  3299. unsigned long arg)
  3300. {
  3301. switch (cmd) {
  3302. case MEGASAS_IOC_FIRMWARE32:
  3303. return megasas_mgmt_compat_ioctl_fw(file, arg);
  3304. case MEGASAS_IOC_GET_AEN:
  3305. return megasas_mgmt_ioctl_aen(file, arg);
  3306. }
  3307. return -ENOTTY;
  3308. }
  3309. #endif
  3310. /*
  3311. * File operations structure for management interface
  3312. */
  3313. static const struct file_operations megasas_mgmt_fops = {
  3314. .owner = THIS_MODULE,
  3315. .open = megasas_mgmt_open,
  3316. .fasync = megasas_mgmt_fasync,
  3317. .unlocked_ioctl = megasas_mgmt_ioctl,
  3318. .poll = megasas_mgmt_poll,
  3319. #ifdef CONFIG_COMPAT
  3320. .compat_ioctl = megasas_mgmt_compat_ioctl,
  3321. #endif
  3322. };
  3323. /*
  3324. * PCI hotplug support registration structure
  3325. */
  3326. static struct pci_driver megasas_pci_driver = {
  3327. .name = "megaraid_sas",
  3328. .id_table = megasas_pci_table,
  3329. .probe = megasas_probe_one,
  3330. .remove = __devexit_p(megasas_detach_one),
  3331. .suspend = megasas_suspend,
  3332. .resume = megasas_resume,
  3333. .shutdown = megasas_shutdown,
  3334. };
  3335. /*
  3336. * Sysfs driver attributes
  3337. */
  3338. static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
  3339. {
  3340. return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
  3341. MEGASAS_VERSION);
  3342. }
  3343. static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
  3344. static ssize_t
  3345. megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
  3346. {
  3347. return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
  3348. MEGASAS_RELDATE);
  3349. }
  3350. static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
  3351. NULL);
  3352. static ssize_t
  3353. megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
  3354. {
  3355. return sprintf(buf, "%u\n", support_poll_for_event);
  3356. }
  3357. static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
  3358. megasas_sysfs_show_support_poll_for_event, NULL);
  3359. static ssize_t
  3360. megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
  3361. {
  3362. return sprintf(buf, "%u\n", megasas_dbg_lvl);
  3363. }
  3364. static ssize_t
  3365. megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
  3366. {
  3367. int retval = count;
  3368. if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
  3369. printk(KERN_ERR "megasas: could not set dbg_lvl\n");
  3370. retval = -EINVAL;
  3371. }
  3372. return retval;
  3373. }
  3374. static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
  3375. megasas_sysfs_set_dbg_lvl);
  3376. static ssize_t
  3377. megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
  3378. {
  3379. return sprintf(buf, "%u\n", poll_mode_io);
  3380. }
  3381. static ssize_t
  3382. megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
  3383. const char *buf, size_t count)
  3384. {
  3385. int retval = count;
  3386. int tmp = poll_mode_io;
  3387. int i;
  3388. struct megasas_instance *instance;
  3389. if (sscanf(buf, "%u", &poll_mode_io) < 1) {
  3390. printk(KERN_ERR "megasas: could not set poll_mode_io\n");
  3391. retval = -EINVAL;
  3392. }
  3393. /*
  3394. * Check if poll_mode_io is already set or is same as previous value
  3395. */
  3396. if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
  3397. goto out;
  3398. if (poll_mode_io) {
  3399. /*
  3400. * Start timers for all adapters
  3401. */
  3402. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  3403. instance = megasas_mgmt_info.instance[i];
  3404. if (instance) {
  3405. megasas_start_timer(instance,
  3406. &instance->io_completion_timer,
  3407. megasas_io_completion_timer,
  3408. MEGASAS_COMPLETION_TIMER_INTERVAL);
  3409. }
  3410. }
  3411. } else {
  3412. /*
  3413. * Delete timers for all adapters
  3414. */
  3415. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  3416. instance = megasas_mgmt_info.instance[i];
  3417. if (instance)
  3418. del_timer_sync(&instance->io_completion_timer);
  3419. }
  3420. }
  3421. out:
  3422. return retval;
  3423. }
  3424. static void
  3425. megasas_aen_polling(struct work_struct *work)
  3426. {
  3427. struct megasas_aen_event *ev =
  3428. container_of(work, struct megasas_aen_event, hotplug_work);
  3429. struct megasas_instance *instance = ev->instance;
  3430. union megasas_evt_class_locale class_locale;
  3431. struct Scsi_Host *host;
  3432. struct scsi_device *sdev1;
  3433. u16 pd_index = 0;
  3434. u16 ld_index = 0;
  3435. int i, j, doscan = 0;
  3436. u32 seq_num;
  3437. int error;
  3438. if (!instance) {
  3439. printk(KERN_ERR "invalid instance!\n");
  3440. kfree(ev);
  3441. return;
  3442. }
  3443. instance->ev = NULL;
  3444. host = instance->host;
  3445. if (instance->evt_detail) {
  3446. switch (instance->evt_detail->code) {
  3447. case MR_EVT_PD_INSERTED:
  3448. if (megasas_get_pd_list(instance) == 0) {
  3449. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  3450. for (j = 0;
  3451. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  3452. j++) {
  3453. pd_index =
  3454. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  3455. sdev1 =
  3456. scsi_device_lookup(host, i, j, 0);
  3457. if (instance->pd_list[pd_index].driveState
  3458. == MR_PD_STATE_SYSTEM) {
  3459. if (!sdev1) {
  3460. scsi_add_device(host, i, j, 0);
  3461. }
  3462. if (sdev1)
  3463. scsi_device_put(sdev1);
  3464. }
  3465. }
  3466. }
  3467. }
  3468. doscan = 0;
  3469. break;
  3470. case MR_EVT_PD_REMOVED:
  3471. if (megasas_get_pd_list(instance) == 0) {
  3472. megasas_get_pd_list(instance);
  3473. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  3474. for (j = 0;
  3475. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  3476. j++) {
  3477. pd_index =
  3478. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  3479. sdev1 =
  3480. scsi_device_lookup(host, i, j, 0);
  3481. if (instance->pd_list[pd_index].driveState
  3482. == MR_PD_STATE_SYSTEM) {
  3483. if (sdev1) {
  3484. scsi_device_put(sdev1);
  3485. }
  3486. } else {
  3487. if (sdev1) {
  3488. scsi_remove_device(sdev1);
  3489. scsi_device_put(sdev1);
  3490. }
  3491. }
  3492. }
  3493. }
  3494. }
  3495. doscan = 0;
  3496. break;
  3497. case MR_EVT_LD_OFFLINE:
  3498. case MR_EVT_LD_DELETED:
  3499. megasas_get_ld_list(instance);
  3500. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  3501. for (j = 0;
  3502. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  3503. j++) {
  3504. ld_index =
  3505. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  3506. sdev1 = scsi_device_lookup(host,
  3507. i + MEGASAS_MAX_LD_CHANNELS,
  3508. j,
  3509. 0);
  3510. if (instance->ld_ids[ld_index] != 0xff) {
  3511. if (sdev1) {
  3512. scsi_device_put(sdev1);
  3513. }
  3514. } else {
  3515. if (sdev1) {
  3516. scsi_remove_device(sdev1);
  3517. scsi_device_put(sdev1);
  3518. }
  3519. }
  3520. }
  3521. }
  3522. doscan = 0;
  3523. break;
  3524. case MR_EVT_LD_CREATED:
  3525. megasas_get_ld_list(instance);
  3526. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  3527. for (j = 0;
  3528. j < MEGASAS_MAX_DEV_PER_CHANNEL;
  3529. j++) {
  3530. ld_index =
  3531. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  3532. sdev1 = scsi_device_lookup(host,
  3533. i+MEGASAS_MAX_LD_CHANNELS,
  3534. j, 0);
  3535. if (instance->ld_ids[ld_index] !=
  3536. 0xff) {
  3537. if (!sdev1) {
  3538. scsi_add_device(host,
  3539. i + 2,
  3540. j, 0);
  3541. }
  3542. }
  3543. if (sdev1) {
  3544. scsi_device_put(sdev1);
  3545. }
  3546. }
  3547. }
  3548. doscan = 0;
  3549. break;
  3550. case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
  3551. case MR_EVT_FOREIGN_CFG_IMPORTED:
  3552. doscan = 1;
  3553. break;
  3554. default:
  3555. doscan = 0;
  3556. break;
  3557. }
  3558. } else {
  3559. printk(KERN_ERR "invalid evt_detail!\n");
  3560. kfree(ev);
  3561. return;
  3562. }
  3563. if (doscan) {
  3564. printk(KERN_INFO "scanning ...\n");
  3565. megasas_get_pd_list(instance);
  3566. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  3567. for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
  3568. pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
  3569. sdev1 = scsi_device_lookup(host, i, j, 0);
  3570. if (instance->pd_list[pd_index].driveState ==
  3571. MR_PD_STATE_SYSTEM) {
  3572. if (!sdev1) {
  3573. scsi_add_device(host, i, j, 0);
  3574. }
  3575. if (sdev1)
  3576. scsi_device_put(sdev1);
  3577. } else {
  3578. if (sdev1) {
  3579. scsi_remove_device(sdev1);
  3580. scsi_device_put(sdev1);
  3581. }
  3582. }
  3583. }
  3584. }
  3585. megasas_get_ld_list(instance);
  3586. for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
  3587. for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
  3588. ld_index =
  3589. (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
  3590. sdev1 = scsi_device_lookup(host,
  3591. i+MEGASAS_MAX_LD_CHANNELS, j, 0);
  3592. if (instance->ld_ids[ld_index] != 0xff) {
  3593. if (!sdev1) {
  3594. scsi_add_device(host,
  3595. i+2,
  3596. j, 0);
  3597. } else {
  3598. scsi_device_put(sdev1);
  3599. }
  3600. } else {
  3601. if (sdev1) {
  3602. scsi_remove_device(sdev1);
  3603. scsi_device_put(sdev1);
  3604. }
  3605. }
  3606. }
  3607. }
  3608. }
  3609. if ( instance->aen_cmd != NULL ) {
  3610. kfree(ev);
  3611. return ;
  3612. }
  3613. seq_num = instance->evt_detail->seq_num + 1;
  3614. /* Register AEN with FW for latest sequence number plus 1 */
  3615. class_locale.members.reserved = 0;
  3616. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  3617. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  3618. mutex_lock(&instance->aen_mutex);
  3619. error = megasas_register_aen(instance, seq_num,
  3620. class_locale.word);
  3621. mutex_unlock(&instance->aen_mutex);
  3622. if (error)
  3623. printk(KERN_ERR "register aen failed error %x\n", error);
  3624. kfree(ev);
  3625. }
  3626. static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUSR,
  3627. megasas_sysfs_show_poll_mode_io,
  3628. megasas_sysfs_set_poll_mode_io);
  3629. /**
  3630. * megasas_init - Driver load entry point
  3631. */
  3632. static int __init megasas_init(void)
  3633. {
  3634. int rval;
  3635. /*
  3636. * Announce driver version and other information
  3637. */
  3638. printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
  3639. MEGASAS_EXT_VERSION);
  3640. support_poll_for_event = 2;
  3641. memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
  3642. /*
  3643. * Register character device node
  3644. */
  3645. rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
  3646. if (rval < 0) {
  3647. printk(KERN_DEBUG "megasas: failed to open device node\n");
  3648. return rval;
  3649. }
  3650. megasas_mgmt_majorno = rval;
  3651. /*
  3652. * Register ourselves as PCI hotplug module
  3653. */
  3654. rval = pci_register_driver(&megasas_pci_driver);
  3655. if (rval) {
  3656. printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
  3657. goto err_pcidrv;
  3658. }
  3659. rval = driver_create_file(&megasas_pci_driver.driver,
  3660. &driver_attr_version);
  3661. if (rval)
  3662. goto err_dcf_attr_ver;
  3663. rval = driver_create_file(&megasas_pci_driver.driver,
  3664. &driver_attr_release_date);
  3665. if (rval)
  3666. goto err_dcf_rel_date;
  3667. rval = driver_create_file(&megasas_pci_driver.driver,
  3668. &driver_attr_support_poll_for_event);
  3669. if (rval)
  3670. goto err_dcf_support_poll_for_event;
  3671. rval = driver_create_file(&megasas_pci_driver.driver,
  3672. &driver_attr_dbg_lvl);
  3673. if (rval)
  3674. goto err_dcf_dbg_lvl;
  3675. rval = driver_create_file(&megasas_pci_driver.driver,
  3676. &driver_attr_poll_mode_io);
  3677. if (rval)
  3678. goto err_dcf_poll_mode_io;
  3679. return rval;
  3680. err_dcf_poll_mode_io:
  3681. driver_remove_file(&megasas_pci_driver.driver,
  3682. &driver_attr_dbg_lvl);
  3683. err_dcf_dbg_lvl:
  3684. driver_remove_file(&megasas_pci_driver.driver,
  3685. &driver_attr_support_poll_for_event);
  3686. err_dcf_support_poll_for_event:
  3687. driver_remove_file(&megasas_pci_driver.driver,
  3688. &driver_attr_release_date);
  3689. err_dcf_rel_date:
  3690. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  3691. err_dcf_attr_ver:
  3692. pci_unregister_driver(&megasas_pci_driver);
  3693. err_pcidrv:
  3694. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  3695. return rval;
  3696. }
  3697. /**
  3698. * megasas_exit - Driver unload entry point
  3699. */
  3700. static void __exit megasas_exit(void)
  3701. {
  3702. driver_remove_file(&megasas_pci_driver.driver,
  3703. &driver_attr_poll_mode_io);
  3704. driver_remove_file(&megasas_pci_driver.driver,
  3705. &driver_attr_dbg_lvl);
  3706. driver_remove_file(&megasas_pci_driver.driver,
  3707. &driver_attr_release_date);
  3708. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  3709. pci_unregister_driver(&megasas_pci_driver);
  3710. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  3711. }
  3712. module_init(megasas_init);
  3713. module_exit(megasas_exit);