megaraid_sas.c 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151
  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.12-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/smp_lock.h>
  36. #include <linux/uio.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->flags = flags;
  731. pthru->data_xfer_len = scsi_bufflen(scp);
  732. memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
  733. /*
  734. * If the command is for the tape device, set the
  735. * pthru timeout to the os layer timeout value.
  736. */
  737. if (scp->device->type == TYPE_TAPE) {
  738. if ((scp->request->timeout / HZ) > 0xFFFF)
  739. pthru->timeout = 0xFFFF;
  740. else
  741. pthru->timeout = scp->request->timeout / HZ;
  742. }
  743. /*
  744. * Construct SGL
  745. */
  746. if (instance->flag_ieee == 1) {
  747. pthru->flags |= MFI_FRAME_SGL64;
  748. pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
  749. &pthru->sgl);
  750. } else if (IS_DMA64) {
  751. pthru->flags |= MFI_FRAME_SGL64;
  752. pthru->sge_count = megasas_make_sgl64(instance, scp,
  753. &pthru->sgl);
  754. } else
  755. pthru->sge_count = megasas_make_sgl32(instance, scp,
  756. &pthru->sgl);
  757. /*
  758. * Sense info specific
  759. */
  760. pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
  761. pthru->sense_buf_phys_addr_hi = 0;
  762. pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
  763. /*
  764. * Compute the total number of frames this command consumes. FW uses
  765. * this number to pull sufficient number of frames from host memory.
  766. */
  767. cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
  768. PTHRU_FRAME);
  769. return cmd->frame_count;
  770. }
  771. /**
  772. * megasas_build_ldio - Prepares IOs to logical devices
  773. * @instance: Adapter soft state
  774. * @scp: SCSI command
  775. * @cmd: Command to be prepared
  776. *
  777. * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
  778. */
  779. static int
  780. megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
  781. struct megasas_cmd *cmd)
  782. {
  783. u32 device_id;
  784. u8 sc = scp->cmnd[0];
  785. u16 flags = 0;
  786. struct megasas_io_frame *ldio;
  787. device_id = MEGASAS_DEV_INDEX(instance, scp);
  788. ldio = (struct megasas_io_frame *)cmd->frame;
  789. if (scp->sc_data_direction == PCI_DMA_TODEVICE)
  790. flags = MFI_FRAME_DIR_WRITE;
  791. else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
  792. flags = MFI_FRAME_DIR_READ;
  793. if (instance->flag_ieee == 1) {
  794. flags |= MFI_FRAME_IEEE;
  795. }
  796. /*
  797. * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
  798. */
  799. ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
  800. ldio->cmd_status = 0x0;
  801. ldio->scsi_status = 0x0;
  802. ldio->target_id = device_id;
  803. ldio->timeout = 0;
  804. ldio->reserved_0 = 0;
  805. ldio->pad_0 = 0;
  806. ldio->flags = flags;
  807. ldio->start_lba_hi = 0;
  808. ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
  809. /*
  810. * 6-byte READ(0x08) or WRITE(0x0A) cdb
  811. */
  812. if (scp->cmd_len == 6) {
  813. ldio->lba_count = (u32) scp->cmnd[4];
  814. ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
  815. ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
  816. ldio->start_lba_lo &= 0x1FFFFF;
  817. }
  818. /*
  819. * 10-byte READ(0x28) or WRITE(0x2A) cdb
  820. */
  821. else if (scp->cmd_len == 10) {
  822. ldio->lba_count = (u32) scp->cmnd[8] |
  823. ((u32) scp->cmnd[7] << 8);
  824. ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  825. ((u32) scp->cmnd[3] << 16) |
  826. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  827. }
  828. /*
  829. * 12-byte READ(0xA8) or WRITE(0xAA) cdb
  830. */
  831. else if (scp->cmd_len == 12) {
  832. ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
  833. ((u32) scp->cmnd[7] << 16) |
  834. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  835. ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
  836. ((u32) scp->cmnd[3] << 16) |
  837. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  838. }
  839. /*
  840. * 16-byte READ(0x88) or WRITE(0x8A) cdb
  841. */
  842. else if (scp->cmd_len == 16) {
  843. ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
  844. ((u32) scp->cmnd[11] << 16) |
  845. ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
  846. ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
  847. ((u32) scp->cmnd[7] << 16) |
  848. ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
  849. ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
  850. ((u32) scp->cmnd[3] << 16) |
  851. ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
  852. }
  853. /*
  854. * Construct SGL
  855. */
  856. if (instance->flag_ieee) {
  857. ldio->flags |= MFI_FRAME_SGL64;
  858. ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
  859. &ldio->sgl);
  860. } else if (IS_DMA64) {
  861. ldio->flags |= MFI_FRAME_SGL64;
  862. ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
  863. } else
  864. ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
  865. /*
  866. * Sense info specific
  867. */
  868. ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
  869. ldio->sense_buf_phys_addr_hi = 0;
  870. ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
  871. /*
  872. * Compute the total number of frames this command consumes. FW uses
  873. * this number to pull sufficient number of frames from host memory.
  874. */
  875. cmd->frame_count = megasas_get_frame_count(instance,
  876. ldio->sge_count, IO_FRAME);
  877. return cmd->frame_count;
  878. }
  879. /**
  880. * megasas_is_ldio - Checks if the cmd is for logical drive
  881. * @scmd: SCSI command
  882. *
  883. * Called by megasas_queue_command to find out if the command to be queued
  884. * is a logical drive command
  885. */
  886. static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
  887. {
  888. if (!MEGASAS_IS_LOGICAL(cmd))
  889. return 0;
  890. switch (cmd->cmnd[0]) {
  891. case READ_10:
  892. case WRITE_10:
  893. case READ_12:
  894. case WRITE_12:
  895. case READ_6:
  896. case WRITE_6:
  897. case READ_16:
  898. case WRITE_16:
  899. return 1;
  900. default:
  901. return 0;
  902. }
  903. }
  904. /**
  905. * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
  906. * in FW
  907. * @instance: Adapter soft state
  908. */
  909. static inline void
  910. megasas_dump_pending_frames(struct megasas_instance *instance)
  911. {
  912. struct megasas_cmd *cmd;
  913. int i,n;
  914. union megasas_sgl *mfi_sgl;
  915. struct megasas_io_frame *ldio;
  916. struct megasas_pthru_frame *pthru;
  917. u32 sgcount;
  918. u32 max_cmd = instance->max_fw_cmds;
  919. printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
  920. printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
  921. if (IS_DMA64)
  922. printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
  923. else
  924. printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
  925. printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
  926. for (i = 0; i < max_cmd; i++) {
  927. cmd = instance->cmd_list[i];
  928. if(!cmd->scmd)
  929. continue;
  930. printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
  931. if (megasas_is_ldio(cmd->scmd)){
  932. ldio = (struct megasas_io_frame *)cmd->frame;
  933. mfi_sgl = &ldio->sgl;
  934. sgcount = ldio->sge_count;
  935. 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);
  936. }
  937. else {
  938. pthru = (struct megasas_pthru_frame *) cmd->frame;
  939. mfi_sgl = &pthru->sgl;
  940. sgcount = pthru->sge_count;
  941. 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);
  942. }
  943. if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
  944. for (n = 0; n < sgcount; n++){
  945. if (IS_DMA64)
  946. 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) ;
  947. else
  948. printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
  949. }
  950. }
  951. printk(KERN_ERR "\n");
  952. } /*for max_cmd*/
  953. printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
  954. for (i = 0; i < max_cmd; i++) {
  955. cmd = instance->cmd_list[i];
  956. if(cmd->sync_cmd == 1){
  957. printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
  958. }
  959. }
  960. printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
  961. }
  962. /**
  963. * megasas_queue_command - Queue entry point
  964. * @scmd: SCSI command to be queued
  965. * @done: Callback entry point
  966. */
  967. static int
  968. megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
  969. {
  970. u32 frame_count;
  971. struct megasas_cmd *cmd;
  972. struct megasas_instance *instance;
  973. instance = (struct megasas_instance *)
  974. scmd->device->host->hostdata;
  975. /* Don't process if we have already declared adapter dead */
  976. if (instance->hw_crit_error)
  977. return SCSI_MLQUEUE_HOST_BUSY;
  978. scmd->scsi_done = done;
  979. scmd->result = 0;
  980. if (MEGASAS_IS_LOGICAL(scmd) &&
  981. (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
  982. scmd->result = DID_BAD_TARGET << 16;
  983. goto out_done;
  984. }
  985. switch (scmd->cmnd[0]) {
  986. case SYNCHRONIZE_CACHE:
  987. /*
  988. * FW takes care of flush cache on its own
  989. * No need to send it down
  990. */
  991. scmd->result = DID_OK << 16;
  992. goto out_done;
  993. default:
  994. break;
  995. }
  996. cmd = megasas_get_cmd(instance);
  997. if (!cmd)
  998. return SCSI_MLQUEUE_HOST_BUSY;
  999. /*
  1000. * Logical drive command
  1001. */
  1002. if (megasas_is_ldio(scmd))
  1003. frame_count = megasas_build_ldio(instance, scmd, cmd);
  1004. else
  1005. frame_count = megasas_build_dcdb(instance, scmd, cmd);
  1006. if (!frame_count)
  1007. goto out_return_cmd;
  1008. cmd->scmd = scmd;
  1009. scmd->SCp.ptr = (char *)cmd;
  1010. /*
  1011. * Issue the command to the FW
  1012. */
  1013. atomic_inc(&instance->fw_outstanding);
  1014. instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
  1015. cmd->frame_count-1, instance->reg_set);
  1016. /*
  1017. * Check if we have pend cmds to be completed
  1018. */
  1019. if (poll_mode_io && atomic_read(&instance->fw_outstanding))
  1020. tasklet_schedule(&instance->isr_tasklet);
  1021. return 0;
  1022. out_return_cmd:
  1023. megasas_return_cmd(instance, cmd);
  1024. out_done:
  1025. done(scmd);
  1026. return 0;
  1027. }
  1028. static struct megasas_instance *megasas_lookup_instance(u16 host_no)
  1029. {
  1030. int i;
  1031. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  1032. if ((megasas_mgmt_info.instance[i]) &&
  1033. (megasas_mgmt_info.instance[i]->host->host_no == host_no))
  1034. return megasas_mgmt_info.instance[i];
  1035. }
  1036. return NULL;
  1037. }
  1038. static int megasas_slave_configure(struct scsi_device *sdev)
  1039. {
  1040. u16 pd_index = 0;
  1041. struct megasas_instance *instance ;
  1042. instance = megasas_lookup_instance(sdev->host->host_no);
  1043. /*
  1044. * Don't export physical disk devices to the disk driver.
  1045. *
  1046. * FIXME: Currently we don't export them to the midlayer at all.
  1047. * That will be fixed once LSI engineers have audited the
  1048. * firmware for possible issues.
  1049. */
  1050. if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
  1051. sdev->type == TYPE_DISK) {
  1052. pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1053. sdev->id;
  1054. if (instance->pd_list[pd_index].driveState ==
  1055. MR_PD_STATE_SYSTEM) {
  1056. blk_queue_rq_timeout(sdev->request_queue,
  1057. MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
  1058. return 0;
  1059. }
  1060. return -ENXIO;
  1061. }
  1062. /*
  1063. * The RAID firmware may require extended timeouts.
  1064. */
  1065. blk_queue_rq_timeout(sdev->request_queue,
  1066. MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
  1067. return 0;
  1068. }
  1069. static int megasas_slave_alloc(struct scsi_device *sdev)
  1070. {
  1071. u16 pd_index = 0;
  1072. struct megasas_instance *instance ;
  1073. instance = megasas_lookup_instance(sdev->host->host_no);
  1074. if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
  1075. (sdev->type == TYPE_DISK)) {
  1076. /*
  1077. * Open the OS scan to the SYSTEM PD
  1078. */
  1079. pd_index =
  1080. (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
  1081. sdev->id;
  1082. if ((instance->pd_list[pd_index].driveState ==
  1083. MR_PD_STATE_SYSTEM) &&
  1084. (instance->pd_list[pd_index].driveType ==
  1085. TYPE_DISK)) {
  1086. return 0;
  1087. }
  1088. return -ENXIO;
  1089. }
  1090. return 0;
  1091. }
  1092. /**
  1093. * megasas_complete_cmd_dpc - Returns FW's controller structure
  1094. * @instance_addr: Address of adapter soft state
  1095. *
  1096. * Tasklet to complete cmds
  1097. */
  1098. static void megasas_complete_cmd_dpc(unsigned long instance_addr)
  1099. {
  1100. u32 producer;
  1101. u32 consumer;
  1102. u32 context;
  1103. struct megasas_cmd *cmd;
  1104. struct megasas_instance *instance =
  1105. (struct megasas_instance *)instance_addr;
  1106. unsigned long flags;
  1107. /* If we have already declared adapter dead, donot complete cmds */
  1108. if (instance->hw_crit_error)
  1109. return;
  1110. spin_lock_irqsave(&instance->completion_lock, flags);
  1111. producer = *instance->producer;
  1112. consumer = *instance->consumer;
  1113. while (consumer != producer) {
  1114. context = instance->reply_queue[consumer];
  1115. cmd = instance->cmd_list[context];
  1116. megasas_complete_cmd(instance, cmd, DID_OK);
  1117. consumer++;
  1118. if (consumer == (instance->max_fw_cmds + 1)) {
  1119. consumer = 0;
  1120. }
  1121. }
  1122. *instance->consumer = producer;
  1123. spin_unlock_irqrestore(&instance->completion_lock, flags);
  1124. /*
  1125. * Check if we can restore can_queue
  1126. */
  1127. if (instance->flag & MEGASAS_FW_BUSY
  1128. && time_after(jiffies, instance->last_time + 5 * HZ)
  1129. && atomic_read(&instance->fw_outstanding) < 17) {
  1130. spin_lock_irqsave(instance->host->host_lock, flags);
  1131. instance->flag &= ~MEGASAS_FW_BUSY;
  1132. if ((instance->pdev->device ==
  1133. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1134. (instance->pdev->device ==
  1135. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1136. instance->host->can_queue =
  1137. instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
  1138. } else
  1139. instance->host->can_queue =
  1140. instance->max_fw_cmds - MEGASAS_INT_CMDS;
  1141. spin_unlock_irqrestore(instance->host->host_lock, flags);
  1142. }
  1143. }
  1144. /**
  1145. * megasas_wait_for_outstanding - Wait for all outstanding cmds
  1146. * @instance: Adapter soft state
  1147. *
  1148. * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
  1149. * complete all its outstanding commands. Returns error if one or more IOs
  1150. * are pending after this time period. It also marks the controller dead.
  1151. */
  1152. static int megasas_wait_for_outstanding(struct megasas_instance *instance)
  1153. {
  1154. int i;
  1155. u32 wait_time = MEGASAS_RESET_WAIT_TIME;
  1156. for (i = 0; i < wait_time; i++) {
  1157. int outstanding = atomic_read(&instance->fw_outstanding);
  1158. if (!outstanding)
  1159. break;
  1160. if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
  1161. printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
  1162. "commands to complete\n",i,outstanding);
  1163. /*
  1164. * Call cmd completion routine. Cmd to be
  1165. * be completed directly without depending on isr.
  1166. */
  1167. megasas_complete_cmd_dpc((unsigned long)instance);
  1168. }
  1169. msleep(1000);
  1170. }
  1171. if (atomic_read(&instance->fw_outstanding)) {
  1172. /*
  1173. * Send signal to FW to stop processing any pending cmds.
  1174. * The controller will be taken offline by the OS now.
  1175. */
  1176. if ((instance->pdev->device ==
  1177. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1178. (instance->pdev->device ==
  1179. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1180. writel(MFI_STOP_ADP,
  1181. &instance->reg_set->reserved_0[0]);
  1182. } else {
  1183. writel(MFI_STOP_ADP,
  1184. &instance->reg_set->inbound_doorbell);
  1185. }
  1186. megasas_dump_pending_frames(instance);
  1187. instance->hw_crit_error = 1;
  1188. return FAILED;
  1189. }
  1190. return SUCCESS;
  1191. }
  1192. /**
  1193. * megasas_generic_reset - Generic reset routine
  1194. * @scmd: Mid-layer SCSI command
  1195. *
  1196. * This routine implements a generic reset handler for device, bus and host
  1197. * reset requests. Device, bus and host specific reset handlers can use this
  1198. * function after they do their specific tasks.
  1199. */
  1200. static int megasas_generic_reset(struct scsi_cmnd *scmd)
  1201. {
  1202. int ret_val;
  1203. struct megasas_instance *instance;
  1204. instance = (struct megasas_instance *)scmd->device->host->hostdata;
  1205. scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
  1206. scmd->serial_number, scmd->cmnd[0], scmd->retries);
  1207. if (instance->hw_crit_error) {
  1208. printk(KERN_ERR "megasas: cannot recover from previous reset "
  1209. "failures\n");
  1210. return FAILED;
  1211. }
  1212. ret_val = megasas_wait_for_outstanding(instance);
  1213. if (ret_val == SUCCESS)
  1214. printk(KERN_NOTICE "megasas: reset successful \n");
  1215. else
  1216. printk(KERN_ERR "megasas: failed to do reset\n");
  1217. return ret_val;
  1218. }
  1219. /**
  1220. * megasas_reset_timer - quiesce the adapter if required
  1221. * @scmd: scsi cmnd
  1222. *
  1223. * Sets the FW busy flag and reduces the host->can_queue if the
  1224. * cmd has not been completed within the timeout period.
  1225. */
  1226. static enum
  1227. blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
  1228. {
  1229. struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
  1230. struct megasas_instance *instance;
  1231. unsigned long flags;
  1232. if (time_after(jiffies, scmd->jiffies_at_alloc +
  1233. (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
  1234. return BLK_EH_NOT_HANDLED;
  1235. }
  1236. instance = cmd->instance;
  1237. if (!(instance->flag & MEGASAS_FW_BUSY)) {
  1238. /* FW is busy, throttle IO */
  1239. spin_lock_irqsave(instance->host->host_lock, flags);
  1240. instance->host->can_queue = 16;
  1241. instance->last_time = jiffies;
  1242. instance->flag |= MEGASAS_FW_BUSY;
  1243. spin_unlock_irqrestore(instance->host->host_lock, flags);
  1244. }
  1245. return BLK_EH_RESET_TIMER;
  1246. }
  1247. /**
  1248. * megasas_reset_device - Device reset handler entry point
  1249. */
  1250. static int megasas_reset_device(struct scsi_cmnd *scmd)
  1251. {
  1252. int ret;
  1253. /*
  1254. * First wait for all commands to complete
  1255. */
  1256. ret = megasas_generic_reset(scmd);
  1257. return ret;
  1258. }
  1259. /**
  1260. * megasas_reset_bus_host - Bus & host reset handler entry point
  1261. */
  1262. static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
  1263. {
  1264. int ret;
  1265. /*
  1266. * First wait for all commands to complete
  1267. */
  1268. ret = megasas_generic_reset(scmd);
  1269. return ret;
  1270. }
  1271. /**
  1272. * megasas_bios_param - Returns disk geometry for a disk
  1273. * @sdev: device handle
  1274. * @bdev: block device
  1275. * @capacity: drive capacity
  1276. * @geom: geometry parameters
  1277. */
  1278. static int
  1279. megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  1280. sector_t capacity, int geom[])
  1281. {
  1282. int heads;
  1283. int sectors;
  1284. sector_t cylinders;
  1285. unsigned long tmp;
  1286. /* Default heads (64) & sectors (32) */
  1287. heads = 64;
  1288. sectors = 32;
  1289. tmp = heads * sectors;
  1290. cylinders = capacity;
  1291. sector_div(cylinders, tmp);
  1292. /*
  1293. * Handle extended translation size for logical drives > 1Gb
  1294. */
  1295. if (capacity >= 0x200000) {
  1296. heads = 255;
  1297. sectors = 63;
  1298. tmp = heads*sectors;
  1299. cylinders = capacity;
  1300. sector_div(cylinders, tmp);
  1301. }
  1302. geom[0] = heads;
  1303. geom[1] = sectors;
  1304. geom[2] = cylinders;
  1305. return 0;
  1306. }
  1307. static void megasas_aen_polling(struct work_struct *work);
  1308. /**
  1309. * megasas_service_aen - Processes an event notification
  1310. * @instance: Adapter soft state
  1311. * @cmd: AEN command completed by the ISR
  1312. *
  1313. * For AEN, driver sends a command down to FW that is held by the FW till an
  1314. * event occurs. When an event of interest occurs, FW completes the command
  1315. * that it was previously holding.
  1316. *
  1317. * This routines sends SIGIO signal to processes that have registered with the
  1318. * driver for AEN.
  1319. */
  1320. static void
  1321. megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
  1322. {
  1323. unsigned long flags;
  1324. /*
  1325. * Don't signal app if it is just an aborted previously registered aen
  1326. */
  1327. if ((!cmd->abort_aen) && (instance->unload == 0)) {
  1328. spin_lock_irqsave(&poll_aen_lock, flags);
  1329. megasas_poll_wait_aen = 1;
  1330. spin_unlock_irqrestore(&poll_aen_lock, flags);
  1331. wake_up(&megasas_poll_wait);
  1332. kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
  1333. }
  1334. else
  1335. cmd->abort_aen = 0;
  1336. instance->aen_cmd = NULL;
  1337. megasas_return_cmd(instance, cmd);
  1338. if (instance->unload == 0) {
  1339. struct megasas_aen_event *ev;
  1340. ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
  1341. if (!ev) {
  1342. printk(KERN_ERR "megasas_service_aen: out of memory\n");
  1343. } else {
  1344. ev->instance = instance;
  1345. instance->ev = ev;
  1346. INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
  1347. schedule_delayed_work(
  1348. (struct delayed_work *)&ev->hotplug_work, 0);
  1349. }
  1350. }
  1351. }
  1352. /*
  1353. * Scsi host template for megaraid_sas driver
  1354. */
  1355. static struct scsi_host_template megasas_template = {
  1356. .module = THIS_MODULE,
  1357. .name = "LSI SAS based MegaRAID driver",
  1358. .proc_name = "megaraid_sas",
  1359. .slave_configure = megasas_slave_configure,
  1360. .slave_alloc = megasas_slave_alloc,
  1361. .queuecommand = megasas_queue_command,
  1362. .eh_device_reset_handler = megasas_reset_device,
  1363. .eh_bus_reset_handler = megasas_reset_bus_host,
  1364. .eh_host_reset_handler = megasas_reset_bus_host,
  1365. .eh_timed_out = megasas_reset_timer,
  1366. .bios_param = megasas_bios_param,
  1367. .use_clustering = ENABLE_CLUSTERING,
  1368. };
  1369. /**
  1370. * megasas_complete_int_cmd - Completes an internal command
  1371. * @instance: Adapter soft state
  1372. * @cmd: Command to be completed
  1373. *
  1374. * The megasas_issue_blocked_cmd() function waits for a command to complete
  1375. * after it issues a command. This function wakes up that waiting routine by
  1376. * calling wake_up() on the wait queue.
  1377. */
  1378. static void
  1379. megasas_complete_int_cmd(struct megasas_instance *instance,
  1380. struct megasas_cmd *cmd)
  1381. {
  1382. cmd->cmd_status = cmd->frame->io.cmd_status;
  1383. if (cmd->cmd_status == ENODATA) {
  1384. cmd->cmd_status = 0;
  1385. }
  1386. wake_up(&instance->int_cmd_wait_q);
  1387. }
  1388. /**
  1389. * megasas_complete_abort - Completes aborting a command
  1390. * @instance: Adapter soft state
  1391. * @cmd: Cmd that was issued to abort another cmd
  1392. *
  1393. * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
  1394. * after it issues an abort on a previously issued command. This function
  1395. * wakes up all functions waiting on the same wait queue.
  1396. */
  1397. static void
  1398. megasas_complete_abort(struct megasas_instance *instance,
  1399. struct megasas_cmd *cmd)
  1400. {
  1401. if (cmd->sync_cmd) {
  1402. cmd->sync_cmd = 0;
  1403. cmd->cmd_status = 0;
  1404. wake_up(&instance->abort_cmd_wait_q);
  1405. }
  1406. return;
  1407. }
  1408. /**
  1409. * megasas_complete_cmd - Completes a command
  1410. * @instance: Adapter soft state
  1411. * @cmd: Command to be completed
  1412. * @alt_status: If non-zero, use this value as status to
  1413. * SCSI mid-layer instead of the value returned
  1414. * by the FW. This should be used if caller wants
  1415. * an alternate status (as in the case of aborted
  1416. * commands)
  1417. */
  1418. static void
  1419. megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
  1420. u8 alt_status)
  1421. {
  1422. int exception = 0;
  1423. struct megasas_header *hdr = &cmd->frame->hdr;
  1424. unsigned long flags;
  1425. if (cmd->scmd)
  1426. cmd->scmd->SCp.ptr = NULL;
  1427. switch (hdr->cmd) {
  1428. case MFI_CMD_PD_SCSI_IO:
  1429. case MFI_CMD_LD_SCSI_IO:
  1430. /*
  1431. * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
  1432. * issued either through an IO path or an IOCTL path. If it
  1433. * was via IOCTL, we will send it to internal completion.
  1434. */
  1435. if (cmd->sync_cmd) {
  1436. cmd->sync_cmd = 0;
  1437. megasas_complete_int_cmd(instance, cmd);
  1438. break;
  1439. }
  1440. case MFI_CMD_LD_READ:
  1441. case MFI_CMD_LD_WRITE:
  1442. if (alt_status) {
  1443. cmd->scmd->result = alt_status << 16;
  1444. exception = 1;
  1445. }
  1446. if (exception) {
  1447. atomic_dec(&instance->fw_outstanding);
  1448. scsi_dma_unmap(cmd->scmd);
  1449. cmd->scmd->scsi_done(cmd->scmd);
  1450. megasas_return_cmd(instance, cmd);
  1451. break;
  1452. }
  1453. switch (hdr->cmd_status) {
  1454. case MFI_STAT_OK:
  1455. cmd->scmd->result = DID_OK << 16;
  1456. break;
  1457. case MFI_STAT_SCSI_IO_FAILED:
  1458. case MFI_STAT_LD_INIT_IN_PROGRESS:
  1459. cmd->scmd->result =
  1460. (DID_ERROR << 16) | hdr->scsi_status;
  1461. break;
  1462. case MFI_STAT_SCSI_DONE_WITH_ERROR:
  1463. cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
  1464. if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
  1465. memset(cmd->scmd->sense_buffer, 0,
  1466. SCSI_SENSE_BUFFERSIZE);
  1467. memcpy(cmd->scmd->sense_buffer, cmd->sense,
  1468. hdr->sense_len);
  1469. cmd->scmd->result |= DRIVER_SENSE << 24;
  1470. }
  1471. break;
  1472. case MFI_STAT_LD_OFFLINE:
  1473. case MFI_STAT_DEVICE_NOT_FOUND:
  1474. cmd->scmd->result = DID_BAD_TARGET << 16;
  1475. break;
  1476. default:
  1477. printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
  1478. hdr->cmd_status);
  1479. cmd->scmd->result = DID_ERROR << 16;
  1480. break;
  1481. }
  1482. atomic_dec(&instance->fw_outstanding);
  1483. scsi_dma_unmap(cmd->scmd);
  1484. cmd->scmd->scsi_done(cmd->scmd);
  1485. megasas_return_cmd(instance, cmd);
  1486. break;
  1487. case MFI_CMD_SMP:
  1488. case MFI_CMD_STP:
  1489. case MFI_CMD_DCMD:
  1490. if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
  1491. cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
  1492. spin_lock_irqsave(&poll_aen_lock, flags);
  1493. megasas_poll_wait_aen = 0;
  1494. spin_unlock_irqrestore(&poll_aen_lock, flags);
  1495. }
  1496. /*
  1497. * See if got an event notification
  1498. */
  1499. if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
  1500. megasas_service_aen(instance, cmd);
  1501. else
  1502. megasas_complete_int_cmd(instance, cmd);
  1503. break;
  1504. case MFI_CMD_ABORT:
  1505. /*
  1506. * Cmd issued to abort another cmd returned
  1507. */
  1508. megasas_complete_abort(instance, cmd);
  1509. break;
  1510. default:
  1511. printk("megasas: Unknown command completed! [0x%X]\n",
  1512. hdr->cmd);
  1513. break;
  1514. }
  1515. }
  1516. /**
  1517. * megasas_deplete_reply_queue - Processes all completed commands
  1518. * @instance: Adapter soft state
  1519. * @alt_status: Alternate status to be returned to
  1520. * SCSI mid-layer instead of the status
  1521. * returned by the FW
  1522. */
  1523. static int
  1524. megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
  1525. {
  1526. /*
  1527. * Check if it is our interrupt
  1528. * Clear the interrupt
  1529. */
  1530. if(instance->instancet->clear_intr(instance->reg_set))
  1531. return IRQ_NONE;
  1532. if (instance->hw_crit_error)
  1533. goto out_done;
  1534. /*
  1535. * Schedule the tasklet for cmd completion
  1536. */
  1537. tasklet_schedule(&instance->isr_tasklet);
  1538. out_done:
  1539. return IRQ_HANDLED;
  1540. }
  1541. /**
  1542. * megasas_isr - isr entry point
  1543. */
  1544. static irqreturn_t megasas_isr(int irq, void *devp)
  1545. {
  1546. return megasas_deplete_reply_queue((struct megasas_instance *)devp,
  1547. DID_OK);
  1548. }
  1549. /**
  1550. * megasas_transition_to_ready - Move the FW to READY state
  1551. * @instance: Adapter soft state
  1552. *
  1553. * During the initialization, FW passes can potentially be in any one of
  1554. * several possible states. If the FW in operational, waiting-for-handshake
  1555. * states, driver must take steps to bring it to ready state. Otherwise, it
  1556. * has to wait for the ready state.
  1557. */
  1558. static int
  1559. megasas_transition_to_ready(struct megasas_instance* instance)
  1560. {
  1561. int i;
  1562. u8 max_wait;
  1563. u32 fw_state;
  1564. u32 cur_state;
  1565. u32 abs_state, curr_abs_state;
  1566. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
  1567. if (fw_state != MFI_STATE_READY)
  1568. printk(KERN_INFO "megasas: Waiting for FW to come to ready"
  1569. " state\n");
  1570. while (fw_state != MFI_STATE_READY) {
  1571. abs_state =
  1572. instance->instancet->read_fw_status_reg(instance->reg_set);
  1573. switch (fw_state) {
  1574. case MFI_STATE_FAULT:
  1575. printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
  1576. return -ENODEV;
  1577. case MFI_STATE_WAIT_HANDSHAKE:
  1578. /*
  1579. * Set the CLR bit in inbound doorbell
  1580. */
  1581. if ((instance->pdev->device ==
  1582. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1583. (instance->pdev->device ==
  1584. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1585. writel(
  1586. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  1587. &instance->reg_set->reserved_0[0]);
  1588. } else {
  1589. writel(
  1590. MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
  1591. &instance->reg_set->inbound_doorbell);
  1592. }
  1593. max_wait = MEGASAS_RESET_WAIT_TIME;
  1594. cur_state = MFI_STATE_WAIT_HANDSHAKE;
  1595. break;
  1596. case MFI_STATE_BOOT_MESSAGE_PENDING:
  1597. if ((instance->pdev->device ==
  1598. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1599. (instance->pdev->device ==
  1600. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1601. writel(MFI_INIT_HOTPLUG,
  1602. &instance->reg_set->reserved_0[0]);
  1603. } else
  1604. writel(MFI_INIT_HOTPLUG,
  1605. &instance->reg_set->inbound_doorbell);
  1606. max_wait = MEGASAS_RESET_WAIT_TIME;
  1607. cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
  1608. break;
  1609. case MFI_STATE_OPERATIONAL:
  1610. /*
  1611. * Bring it to READY state; assuming max wait 10 secs
  1612. */
  1613. instance->instancet->disable_intr(instance->reg_set);
  1614. if ((instance->pdev->device ==
  1615. PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  1616. (instance->pdev->device ==
  1617. PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  1618. writel(MFI_RESET_FLAGS,
  1619. &instance->reg_set->reserved_0[0]);
  1620. } else
  1621. writel(MFI_RESET_FLAGS,
  1622. &instance->reg_set->inbound_doorbell);
  1623. max_wait = MEGASAS_RESET_WAIT_TIME;
  1624. cur_state = MFI_STATE_OPERATIONAL;
  1625. break;
  1626. case MFI_STATE_UNDEFINED:
  1627. /*
  1628. * This state should not last for more than 2 seconds
  1629. */
  1630. max_wait = MEGASAS_RESET_WAIT_TIME;
  1631. cur_state = MFI_STATE_UNDEFINED;
  1632. break;
  1633. case MFI_STATE_BB_INIT:
  1634. max_wait = MEGASAS_RESET_WAIT_TIME;
  1635. cur_state = MFI_STATE_BB_INIT;
  1636. break;
  1637. case MFI_STATE_FW_INIT:
  1638. max_wait = MEGASAS_RESET_WAIT_TIME;
  1639. cur_state = MFI_STATE_FW_INIT;
  1640. break;
  1641. case MFI_STATE_FW_INIT_2:
  1642. max_wait = MEGASAS_RESET_WAIT_TIME;
  1643. cur_state = MFI_STATE_FW_INIT_2;
  1644. break;
  1645. case MFI_STATE_DEVICE_SCAN:
  1646. max_wait = MEGASAS_RESET_WAIT_TIME;
  1647. cur_state = MFI_STATE_DEVICE_SCAN;
  1648. break;
  1649. case MFI_STATE_FLUSH_CACHE:
  1650. max_wait = MEGASAS_RESET_WAIT_TIME;
  1651. cur_state = MFI_STATE_FLUSH_CACHE;
  1652. break;
  1653. default:
  1654. printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
  1655. fw_state);
  1656. return -ENODEV;
  1657. }
  1658. /*
  1659. * The cur_state should not last for more than max_wait secs
  1660. */
  1661. for (i = 0; i < (max_wait * 1000); i++) {
  1662. fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
  1663. MFI_STATE_MASK ;
  1664. curr_abs_state =
  1665. instance->instancet->read_fw_status_reg(instance->reg_set);
  1666. if (abs_state == curr_abs_state) {
  1667. msleep(1);
  1668. } else
  1669. break;
  1670. }
  1671. /*
  1672. * Return error if fw_state hasn't changed after max_wait
  1673. */
  1674. if (curr_abs_state == abs_state) {
  1675. printk(KERN_DEBUG "FW state [%d] hasn't changed "
  1676. "in %d secs\n", fw_state, max_wait);
  1677. return -ENODEV;
  1678. }
  1679. };
  1680. printk(KERN_INFO "megasas: FW now in Ready state\n");
  1681. return 0;
  1682. }
  1683. /**
  1684. * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
  1685. * @instance: Adapter soft state
  1686. */
  1687. static void megasas_teardown_frame_pool(struct megasas_instance *instance)
  1688. {
  1689. int i;
  1690. u32 max_cmd = instance->max_fw_cmds;
  1691. struct megasas_cmd *cmd;
  1692. if (!instance->frame_dma_pool)
  1693. return;
  1694. /*
  1695. * Return all frames to pool
  1696. */
  1697. for (i = 0; i < max_cmd; i++) {
  1698. cmd = instance->cmd_list[i];
  1699. if (cmd->frame)
  1700. pci_pool_free(instance->frame_dma_pool, cmd->frame,
  1701. cmd->frame_phys_addr);
  1702. if (cmd->sense)
  1703. pci_pool_free(instance->sense_dma_pool, cmd->sense,
  1704. cmd->sense_phys_addr);
  1705. }
  1706. /*
  1707. * Now destroy the pool itself
  1708. */
  1709. pci_pool_destroy(instance->frame_dma_pool);
  1710. pci_pool_destroy(instance->sense_dma_pool);
  1711. instance->frame_dma_pool = NULL;
  1712. instance->sense_dma_pool = NULL;
  1713. }
  1714. /**
  1715. * megasas_create_frame_pool - Creates DMA pool for cmd frames
  1716. * @instance: Adapter soft state
  1717. *
  1718. * Each command packet has an embedded DMA memory buffer that is used for
  1719. * filling MFI frame and the SG list that immediately follows the frame. This
  1720. * function creates those DMA memory buffers for each command packet by using
  1721. * PCI pool facility.
  1722. */
  1723. static int megasas_create_frame_pool(struct megasas_instance *instance)
  1724. {
  1725. int i;
  1726. u32 max_cmd;
  1727. u32 sge_sz;
  1728. u32 sgl_sz;
  1729. u32 total_sz;
  1730. u32 frame_count;
  1731. struct megasas_cmd *cmd;
  1732. max_cmd = instance->max_fw_cmds;
  1733. /*
  1734. * Size of our frame is 64 bytes for MFI frame, followed by max SG
  1735. * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
  1736. */
  1737. sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
  1738. sizeof(struct megasas_sge32);
  1739. if (instance->flag_ieee) {
  1740. sge_sz = sizeof(struct megasas_sge_skinny);
  1741. }
  1742. /*
  1743. * Calculated the number of 64byte frames required for SGL
  1744. */
  1745. sgl_sz = sge_sz * instance->max_num_sge;
  1746. frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
  1747. /*
  1748. * We need one extra frame for the MFI command
  1749. */
  1750. frame_count++;
  1751. total_sz = MEGAMFI_FRAME_SIZE * frame_count;
  1752. /*
  1753. * Use DMA pool facility provided by PCI layer
  1754. */
  1755. instance->frame_dma_pool = pci_pool_create("megasas frame pool",
  1756. instance->pdev, total_sz, 64,
  1757. 0);
  1758. if (!instance->frame_dma_pool) {
  1759. printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
  1760. return -ENOMEM;
  1761. }
  1762. instance->sense_dma_pool = pci_pool_create("megasas sense pool",
  1763. instance->pdev, 128, 4, 0);
  1764. if (!instance->sense_dma_pool) {
  1765. printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
  1766. pci_pool_destroy(instance->frame_dma_pool);
  1767. instance->frame_dma_pool = NULL;
  1768. return -ENOMEM;
  1769. }
  1770. /*
  1771. * Allocate and attach a frame to each of the commands in cmd_list.
  1772. * By making cmd->index as the context instead of the &cmd, we can
  1773. * always use 32bit context regardless of the architecture
  1774. */
  1775. for (i = 0; i < max_cmd; i++) {
  1776. cmd = instance->cmd_list[i];
  1777. cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
  1778. GFP_KERNEL, &cmd->frame_phys_addr);
  1779. cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
  1780. GFP_KERNEL, &cmd->sense_phys_addr);
  1781. /*
  1782. * megasas_teardown_frame_pool() takes care of freeing
  1783. * whatever has been allocated
  1784. */
  1785. if (!cmd->frame || !cmd->sense) {
  1786. printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
  1787. megasas_teardown_frame_pool(instance);
  1788. return -ENOMEM;
  1789. }
  1790. cmd->frame->io.context = cmd->index;
  1791. cmd->frame->io.pad_0 = 0;
  1792. }
  1793. return 0;
  1794. }
  1795. /**
  1796. * megasas_free_cmds - Free all the cmds in the free cmd pool
  1797. * @instance: Adapter soft state
  1798. */
  1799. static void megasas_free_cmds(struct megasas_instance *instance)
  1800. {
  1801. int i;
  1802. /* First free the MFI frame pool */
  1803. megasas_teardown_frame_pool(instance);
  1804. /* Free all the commands in the cmd_list */
  1805. for (i = 0; i < instance->max_fw_cmds; i++)
  1806. kfree(instance->cmd_list[i]);
  1807. /* Free the cmd_list buffer itself */
  1808. kfree(instance->cmd_list);
  1809. instance->cmd_list = NULL;
  1810. INIT_LIST_HEAD(&instance->cmd_pool);
  1811. }
  1812. /**
  1813. * megasas_alloc_cmds - Allocates the command packets
  1814. * @instance: Adapter soft state
  1815. *
  1816. * Each command that is issued to the FW, whether IO commands from the OS or
  1817. * internal commands like IOCTLs, are wrapped in local data structure called
  1818. * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
  1819. * the FW.
  1820. *
  1821. * Each frame has a 32-bit field called context (tag). This context is used
  1822. * to get back the megasas_cmd from the frame when a frame gets completed in
  1823. * the ISR. Typically the address of the megasas_cmd itself would be used as
  1824. * the context. But we wanted to keep the differences between 32 and 64 bit
  1825. * systems to the mininum. We always use 32 bit integers for the context. In
  1826. * this driver, the 32 bit values are the indices into an array cmd_list.
  1827. * This array is used only to look up the megasas_cmd given the context. The
  1828. * free commands themselves are maintained in a linked list called cmd_pool.
  1829. */
  1830. static int megasas_alloc_cmds(struct megasas_instance *instance)
  1831. {
  1832. int i;
  1833. int j;
  1834. u32 max_cmd;
  1835. struct megasas_cmd *cmd;
  1836. max_cmd = instance->max_fw_cmds;
  1837. /*
  1838. * instance->cmd_list is an array of struct megasas_cmd pointers.
  1839. * Allocate the dynamic array first and then allocate individual
  1840. * commands.
  1841. */
  1842. instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
  1843. if (!instance->cmd_list) {
  1844. printk(KERN_DEBUG "megasas: out of memory\n");
  1845. return -ENOMEM;
  1846. }
  1847. for (i = 0; i < max_cmd; i++) {
  1848. instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
  1849. GFP_KERNEL);
  1850. if (!instance->cmd_list[i]) {
  1851. for (j = 0; j < i; j++)
  1852. kfree(instance->cmd_list[j]);
  1853. kfree(instance->cmd_list);
  1854. instance->cmd_list = NULL;
  1855. return -ENOMEM;
  1856. }
  1857. }
  1858. /*
  1859. * Add all the commands to command pool (instance->cmd_pool)
  1860. */
  1861. for (i = 0; i < max_cmd; i++) {
  1862. cmd = instance->cmd_list[i];
  1863. memset(cmd, 0, sizeof(struct megasas_cmd));
  1864. cmd->index = i;
  1865. cmd->instance = instance;
  1866. list_add_tail(&cmd->list, &instance->cmd_pool);
  1867. }
  1868. /*
  1869. * Create a frame pool and assign one frame to each cmd
  1870. */
  1871. if (megasas_create_frame_pool(instance)) {
  1872. printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
  1873. megasas_free_cmds(instance);
  1874. }
  1875. return 0;
  1876. }
  1877. /*
  1878. * megasas_get_pd_list_info - Returns FW's pd_list structure
  1879. * @instance: Adapter soft state
  1880. * @pd_list: pd_list structure
  1881. *
  1882. * Issues an internal command (DCMD) to get the FW's controller PD
  1883. * list structure. This information is mainly used to find out SYSTEM
  1884. * supported by the FW.
  1885. */
  1886. static int
  1887. megasas_get_pd_list(struct megasas_instance *instance)
  1888. {
  1889. int ret = 0, pd_index = 0;
  1890. struct megasas_cmd *cmd;
  1891. struct megasas_dcmd_frame *dcmd;
  1892. struct MR_PD_LIST *ci;
  1893. struct MR_PD_ADDRESS *pd_addr;
  1894. dma_addr_t ci_h = 0;
  1895. cmd = megasas_get_cmd(instance);
  1896. if (!cmd) {
  1897. printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
  1898. return -ENOMEM;
  1899. }
  1900. dcmd = &cmd->frame->dcmd;
  1901. ci = pci_alloc_consistent(instance->pdev,
  1902. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
  1903. if (!ci) {
  1904. printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
  1905. megasas_return_cmd(instance, cmd);
  1906. return -ENOMEM;
  1907. }
  1908. memset(ci, 0, sizeof(*ci));
  1909. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1910. dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
  1911. dcmd->mbox.b[1] = 0;
  1912. dcmd->cmd = MFI_CMD_DCMD;
  1913. dcmd->cmd_status = 0xFF;
  1914. dcmd->sge_count = 1;
  1915. dcmd->flags = MFI_FRAME_DIR_READ;
  1916. dcmd->timeout = 0;
  1917. dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
  1918. dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
  1919. dcmd->sgl.sge32[0].phys_addr = ci_h;
  1920. dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
  1921. if (!megasas_issue_polled(instance, cmd)) {
  1922. ret = 0;
  1923. } else {
  1924. ret = -1;
  1925. }
  1926. /*
  1927. * the following function will get the instance PD LIST.
  1928. */
  1929. pd_addr = ci->addr;
  1930. if ( ret == 0 &&
  1931. (ci->count <
  1932. (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
  1933. memset(instance->pd_list, 0,
  1934. MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
  1935. for (pd_index = 0; pd_index < ci->count; pd_index++) {
  1936. instance->pd_list[pd_addr->deviceId].tid =
  1937. pd_addr->deviceId;
  1938. instance->pd_list[pd_addr->deviceId].driveType =
  1939. pd_addr->scsiDevType;
  1940. instance->pd_list[pd_addr->deviceId].driveState =
  1941. MR_PD_STATE_SYSTEM;
  1942. pd_addr++;
  1943. }
  1944. }
  1945. pci_free_consistent(instance->pdev,
  1946. MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
  1947. ci, ci_h);
  1948. megasas_return_cmd(instance, cmd);
  1949. return ret;
  1950. }
  1951. /**
  1952. * megasas_get_controller_info - Returns FW's controller structure
  1953. * @instance: Adapter soft state
  1954. * @ctrl_info: Controller information structure
  1955. *
  1956. * Issues an internal command (DCMD) to get the FW's controller structure.
  1957. * This information is mainly used to find out the maximum IO transfer per
  1958. * command supported by the FW.
  1959. */
  1960. static int
  1961. megasas_get_ctrl_info(struct megasas_instance *instance,
  1962. struct megasas_ctrl_info *ctrl_info)
  1963. {
  1964. int ret = 0;
  1965. struct megasas_cmd *cmd;
  1966. struct megasas_dcmd_frame *dcmd;
  1967. struct megasas_ctrl_info *ci;
  1968. dma_addr_t ci_h = 0;
  1969. cmd = megasas_get_cmd(instance);
  1970. if (!cmd) {
  1971. printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
  1972. return -ENOMEM;
  1973. }
  1974. dcmd = &cmd->frame->dcmd;
  1975. ci = pci_alloc_consistent(instance->pdev,
  1976. sizeof(struct megasas_ctrl_info), &ci_h);
  1977. if (!ci) {
  1978. printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
  1979. megasas_return_cmd(instance, cmd);
  1980. return -ENOMEM;
  1981. }
  1982. memset(ci, 0, sizeof(*ci));
  1983. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  1984. dcmd->cmd = MFI_CMD_DCMD;
  1985. dcmd->cmd_status = 0xFF;
  1986. dcmd->sge_count = 1;
  1987. dcmd->flags = MFI_FRAME_DIR_READ;
  1988. dcmd->timeout = 0;
  1989. dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
  1990. dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
  1991. dcmd->sgl.sge32[0].phys_addr = ci_h;
  1992. dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
  1993. if (!megasas_issue_polled(instance, cmd)) {
  1994. ret = 0;
  1995. memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
  1996. } else {
  1997. ret = -1;
  1998. }
  1999. pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
  2000. ci, ci_h);
  2001. megasas_return_cmd(instance, cmd);
  2002. return ret;
  2003. }
  2004. /**
  2005. * megasas_issue_init_mfi - Initializes the FW
  2006. * @instance: Adapter soft state
  2007. *
  2008. * Issues the INIT MFI cmd
  2009. */
  2010. static int
  2011. megasas_issue_init_mfi(struct megasas_instance *instance)
  2012. {
  2013. u32 context;
  2014. struct megasas_cmd *cmd;
  2015. struct megasas_init_frame *init_frame;
  2016. struct megasas_init_queue_info *initq_info;
  2017. dma_addr_t init_frame_h;
  2018. dma_addr_t initq_info_h;
  2019. /*
  2020. * Prepare a init frame. Note the init frame points to queue info
  2021. * structure. Each frame has SGL allocated after first 64 bytes. For
  2022. * this frame - since we don't need any SGL - we use SGL's space as
  2023. * queue info structure
  2024. *
  2025. * We will not get a NULL command below. We just created the pool.
  2026. */
  2027. cmd = megasas_get_cmd(instance);
  2028. init_frame = (struct megasas_init_frame *)cmd->frame;
  2029. initq_info = (struct megasas_init_queue_info *)
  2030. ((unsigned long)init_frame + 64);
  2031. init_frame_h = cmd->frame_phys_addr;
  2032. initq_info_h = init_frame_h + 64;
  2033. context = init_frame->context;
  2034. memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
  2035. memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
  2036. init_frame->context = context;
  2037. initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
  2038. initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
  2039. initq_info->producer_index_phys_addr_lo = instance->producer_h;
  2040. initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
  2041. init_frame->cmd = MFI_CMD_INIT;
  2042. init_frame->cmd_status = 0xFF;
  2043. init_frame->queue_info_new_phys_addr_lo = initq_info_h;
  2044. init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
  2045. /*
  2046. * disable the intr before firing the init frame to FW
  2047. */
  2048. instance->instancet->disable_intr(instance->reg_set);
  2049. /*
  2050. * Issue the init frame in polled mode
  2051. */
  2052. if (megasas_issue_polled(instance, cmd)) {
  2053. printk(KERN_ERR "megasas: Failed to init firmware\n");
  2054. megasas_return_cmd(instance, cmd);
  2055. goto fail_fw_init;
  2056. }
  2057. megasas_return_cmd(instance, cmd);
  2058. return 0;
  2059. fail_fw_init:
  2060. return -EINVAL;
  2061. }
  2062. /**
  2063. * megasas_start_timer - Initializes a timer object
  2064. * @instance: Adapter soft state
  2065. * @timer: timer object to be initialized
  2066. * @fn: timer function
  2067. * @interval: time interval between timer function call
  2068. */
  2069. static inline void
  2070. megasas_start_timer(struct megasas_instance *instance,
  2071. struct timer_list *timer,
  2072. void *fn, unsigned long interval)
  2073. {
  2074. init_timer(timer);
  2075. timer->expires = jiffies + interval;
  2076. timer->data = (unsigned long)instance;
  2077. timer->function = fn;
  2078. add_timer(timer);
  2079. }
  2080. /**
  2081. * megasas_io_completion_timer - Timer fn
  2082. * @instance_addr: Address of adapter soft state
  2083. *
  2084. * Schedules tasklet for cmd completion
  2085. * if poll_mode_io is set
  2086. */
  2087. static void
  2088. megasas_io_completion_timer(unsigned long instance_addr)
  2089. {
  2090. struct megasas_instance *instance =
  2091. (struct megasas_instance *)instance_addr;
  2092. if (atomic_read(&instance->fw_outstanding))
  2093. tasklet_schedule(&instance->isr_tasklet);
  2094. /* Restart timer */
  2095. if (poll_mode_io)
  2096. mod_timer(&instance->io_completion_timer,
  2097. jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
  2098. }
  2099. /**
  2100. * megasas_init_mfi - Initializes the FW
  2101. * @instance: Adapter soft state
  2102. *
  2103. * This is the main function for initializing MFI firmware.
  2104. */
  2105. static int megasas_init_mfi(struct megasas_instance *instance)
  2106. {
  2107. u32 context_sz;
  2108. u32 reply_q_sz;
  2109. u32 max_sectors_1;
  2110. u32 max_sectors_2;
  2111. u32 tmp_sectors;
  2112. struct megasas_register_set __iomem *reg_set;
  2113. struct megasas_ctrl_info *ctrl_info;
  2114. /*
  2115. * Map the message registers
  2116. */
  2117. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
  2118. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
  2119. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2120. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0079GEN2)) {
  2121. instance->base_addr = pci_resource_start(instance->pdev, 1);
  2122. } else {
  2123. instance->base_addr = pci_resource_start(instance->pdev, 0);
  2124. }
  2125. if (pci_request_regions(instance->pdev, "megasas: LSI")) {
  2126. printk(KERN_DEBUG "megasas: IO memory region busy!\n");
  2127. return -EBUSY;
  2128. }
  2129. instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
  2130. if (!instance->reg_set) {
  2131. printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
  2132. goto fail_ioremap;
  2133. }
  2134. reg_set = instance->reg_set;
  2135. switch(instance->pdev->device)
  2136. {
  2137. case PCI_DEVICE_ID_LSI_SAS1078R:
  2138. case PCI_DEVICE_ID_LSI_SAS1078DE:
  2139. instance->instancet = &megasas_instance_template_ppc;
  2140. break;
  2141. case PCI_DEVICE_ID_LSI_SAS1078GEN2:
  2142. case PCI_DEVICE_ID_LSI_SAS0079GEN2:
  2143. instance->instancet = &megasas_instance_template_gen2;
  2144. break;
  2145. case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
  2146. case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
  2147. instance->instancet = &megasas_instance_template_skinny;
  2148. break;
  2149. case PCI_DEVICE_ID_LSI_SAS1064R:
  2150. case PCI_DEVICE_ID_DELL_PERC5:
  2151. default:
  2152. instance->instancet = &megasas_instance_template_xscale;
  2153. break;
  2154. }
  2155. /*
  2156. * We expect the FW state to be READY
  2157. */
  2158. if (megasas_transition_to_ready(instance))
  2159. goto fail_ready_state;
  2160. /*
  2161. * Get various operational parameters from status register
  2162. */
  2163. instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
  2164. /*
  2165. * Reduce the max supported cmds by 1. This is to ensure that the
  2166. * reply_q_sz (1 more than the max cmd that driver may send)
  2167. * does not exceed max cmds that the FW can support
  2168. */
  2169. instance->max_fw_cmds = instance->max_fw_cmds-1;
  2170. instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
  2171. 0x10;
  2172. /*
  2173. * Create a pool of commands
  2174. */
  2175. if (megasas_alloc_cmds(instance))
  2176. goto fail_alloc_cmds;
  2177. /*
  2178. * Allocate memory for reply queue. Length of reply queue should
  2179. * be _one_ more than the maximum commands handled by the firmware.
  2180. *
  2181. * Note: When FW completes commands, it places corresponding contex
  2182. * values in this circular reply queue. This circular queue is a fairly
  2183. * typical producer-consumer queue. FW is the producer (of completed
  2184. * commands) and the driver is the consumer.
  2185. */
  2186. context_sz = sizeof(u32);
  2187. reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
  2188. instance->reply_queue = pci_alloc_consistent(instance->pdev,
  2189. reply_q_sz,
  2190. &instance->reply_queue_h);
  2191. if (!instance->reply_queue) {
  2192. printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
  2193. goto fail_reply_queue;
  2194. }
  2195. if (megasas_issue_init_mfi(instance))
  2196. goto fail_fw_init;
  2197. memset(instance->pd_list, 0 ,
  2198. (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
  2199. megasas_get_pd_list(instance);
  2200. ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
  2201. /*
  2202. * Compute the max allowed sectors per IO: The controller info has two
  2203. * limits on max sectors. Driver should use the minimum of these two.
  2204. *
  2205. * 1 << stripe_sz_ops.min = max sectors per strip
  2206. *
  2207. * Note that older firmwares ( < FW ver 30) didn't report information
  2208. * to calculate max_sectors_1. So the number ended up as zero always.
  2209. */
  2210. tmp_sectors = 0;
  2211. if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
  2212. max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
  2213. ctrl_info->max_strips_per_io;
  2214. max_sectors_2 = ctrl_info->max_request_size;
  2215. tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
  2216. }
  2217. instance->max_sectors_per_req = instance->max_num_sge *
  2218. PAGE_SIZE / 512;
  2219. if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
  2220. instance->max_sectors_per_req = tmp_sectors;
  2221. kfree(ctrl_info);
  2222. /*
  2223. * Setup tasklet for cmd completion
  2224. */
  2225. tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
  2226. (unsigned long)instance);
  2227. /* Initialize the cmd completion timer */
  2228. if (poll_mode_io)
  2229. megasas_start_timer(instance, &instance->io_completion_timer,
  2230. megasas_io_completion_timer,
  2231. MEGASAS_COMPLETION_TIMER_INTERVAL);
  2232. return 0;
  2233. fail_fw_init:
  2234. pci_free_consistent(instance->pdev, reply_q_sz,
  2235. instance->reply_queue, instance->reply_queue_h);
  2236. fail_reply_queue:
  2237. megasas_free_cmds(instance);
  2238. fail_alloc_cmds:
  2239. fail_ready_state:
  2240. iounmap(instance->reg_set);
  2241. fail_ioremap:
  2242. pci_release_regions(instance->pdev);
  2243. return -EINVAL;
  2244. }
  2245. /**
  2246. * megasas_release_mfi - Reverses the FW initialization
  2247. * @intance: Adapter soft state
  2248. */
  2249. static void megasas_release_mfi(struct megasas_instance *instance)
  2250. {
  2251. u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
  2252. pci_free_consistent(instance->pdev, reply_q_sz,
  2253. instance->reply_queue, instance->reply_queue_h);
  2254. megasas_free_cmds(instance);
  2255. iounmap(instance->reg_set);
  2256. pci_release_regions(instance->pdev);
  2257. }
  2258. /**
  2259. * megasas_get_seq_num - Gets latest event sequence numbers
  2260. * @instance: Adapter soft state
  2261. * @eli: FW event log sequence numbers information
  2262. *
  2263. * FW maintains a log of all events in a non-volatile area. Upper layers would
  2264. * usually find out the latest sequence number of the events, the seq number at
  2265. * the boot etc. They would "read" all the events below the latest seq number
  2266. * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
  2267. * number), they would subsribe to AEN (asynchronous event notification) and
  2268. * wait for the events to happen.
  2269. */
  2270. static int
  2271. megasas_get_seq_num(struct megasas_instance *instance,
  2272. struct megasas_evt_log_info *eli)
  2273. {
  2274. struct megasas_cmd *cmd;
  2275. struct megasas_dcmd_frame *dcmd;
  2276. struct megasas_evt_log_info *el_info;
  2277. dma_addr_t el_info_h = 0;
  2278. cmd = megasas_get_cmd(instance);
  2279. if (!cmd) {
  2280. return -ENOMEM;
  2281. }
  2282. dcmd = &cmd->frame->dcmd;
  2283. el_info = pci_alloc_consistent(instance->pdev,
  2284. sizeof(struct megasas_evt_log_info),
  2285. &el_info_h);
  2286. if (!el_info) {
  2287. megasas_return_cmd(instance, cmd);
  2288. return -ENOMEM;
  2289. }
  2290. memset(el_info, 0, sizeof(*el_info));
  2291. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2292. dcmd->cmd = MFI_CMD_DCMD;
  2293. dcmd->cmd_status = 0x0;
  2294. dcmd->sge_count = 1;
  2295. dcmd->flags = MFI_FRAME_DIR_READ;
  2296. dcmd->timeout = 0;
  2297. dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
  2298. dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
  2299. dcmd->sgl.sge32[0].phys_addr = el_info_h;
  2300. dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
  2301. megasas_issue_blocked_cmd(instance, cmd);
  2302. /*
  2303. * Copy the data back into callers buffer
  2304. */
  2305. memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
  2306. pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
  2307. el_info, el_info_h);
  2308. megasas_return_cmd(instance, cmd);
  2309. return 0;
  2310. }
  2311. /**
  2312. * megasas_register_aen - Registers for asynchronous event notification
  2313. * @instance: Adapter soft state
  2314. * @seq_num: The starting sequence number
  2315. * @class_locale: Class of the event
  2316. *
  2317. * This function subscribes for AEN for events beyond the @seq_num. It requests
  2318. * to be notified if and only if the event is of type @class_locale
  2319. */
  2320. static int
  2321. megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
  2322. u32 class_locale_word)
  2323. {
  2324. int ret_val;
  2325. struct megasas_cmd *cmd;
  2326. struct megasas_dcmd_frame *dcmd;
  2327. union megasas_evt_class_locale curr_aen;
  2328. union megasas_evt_class_locale prev_aen;
  2329. /*
  2330. * If there an AEN pending already (aen_cmd), check if the
  2331. * class_locale of that pending AEN is inclusive of the new
  2332. * AEN request we currently have. If it is, then we don't have
  2333. * to do anything. In other words, whichever events the current
  2334. * AEN request is subscribing to, have already been subscribed
  2335. * to.
  2336. *
  2337. * If the old_cmd is _not_ inclusive, then we have to abort
  2338. * that command, form a class_locale that is superset of both
  2339. * old and current and re-issue to the FW
  2340. */
  2341. curr_aen.word = class_locale_word;
  2342. if (instance->aen_cmd) {
  2343. prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
  2344. /*
  2345. * A class whose enum value is smaller is inclusive of all
  2346. * higher values. If a PROGRESS (= -1) was previously
  2347. * registered, then a new registration requests for higher
  2348. * classes need not be sent to FW. They are automatically
  2349. * included.
  2350. *
  2351. * Locale numbers don't have such hierarchy. They are bitmap
  2352. * values
  2353. */
  2354. if ((prev_aen.members.class <= curr_aen.members.class) &&
  2355. !((prev_aen.members.locale & curr_aen.members.locale) ^
  2356. curr_aen.members.locale)) {
  2357. /*
  2358. * Previously issued event registration includes
  2359. * current request. Nothing to do.
  2360. */
  2361. return 0;
  2362. } else {
  2363. curr_aen.members.locale |= prev_aen.members.locale;
  2364. if (prev_aen.members.class < curr_aen.members.class)
  2365. curr_aen.members.class = prev_aen.members.class;
  2366. instance->aen_cmd->abort_aen = 1;
  2367. ret_val = megasas_issue_blocked_abort_cmd(instance,
  2368. instance->
  2369. aen_cmd);
  2370. if (ret_val) {
  2371. printk(KERN_DEBUG "megasas: Failed to abort "
  2372. "previous AEN command\n");
  2373. return ret_val;
  2374. }
  2375. }
  2376. }
  2377. cmd = megasas_get_cmd(instance);
  2378. if (!cmd)
  2379. return -ENOMEM;
  2380. dcmd = &cmd->frame->dcmd;
  2381. memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
  2382. /*
  2383. * Prepare DCMD for aen registration
  2384. */
  2385. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2386. dcmd->cmd = MFI_CMD_DCMD;
  2387. dcmd->cmd_status = 0x0;
  2388. dcmd->sge_count = 1;
  2389. dcmd->flags = MFI_FRAME_DIR_READ;
  2390. dcmd->timeout = 0;
  2391. dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
  2392. dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
  2393. dcmd->mbox.w[0] = seq_num;
  2394. dcmd->mbox.w[1] = curr_aen.word;
  2395. dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
  2396. dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
  2397. if (instance->aen_cmd != NULL) {
  2398. megasas_return_cmd(instance, cmd);
  2399. return 0;
  2400. }
  2401. /*
  2402. * Store reference to the cmd used to register for AEN. When an
  2403. * application wants us to register for AEN, we have to abort this
  2404. * cmd and re-register with a new EVENT LOCALE supplied by that app
  2405. */
  2406. instance->aen_cmd = cmd;
  2407. /*
  2408. * Issue the aen registration frame
  2409. */
  2410. instance->instancet->fire_cmd(instance,
  2411. cmd->frame_phys_addr, 0, instance->reg_set);
  2412. return 0;
  2413. }
  2414. /**
  2415. * megasas_start_aen - Subscribes to AEN during driver load time
  2416. * @instance: Adapter soft state
  2417. */
  2418. static int megasas_start_aen(struct megasas_instance *instance)
  2419. {
  2420. struct megasas_evt_log_info eli;
  2421. union megasas_evt_class_locale class_locale;
  2422. /*
  2423. * Get the latest sequence number from FW
  2424. */
  2425. memset(&eli, 0, sizeof(eli));
  2426. if (megasas_get_seq_num(instance, &eli))
  2427. return -1;
  2428. /*
  2429. * Register AEN with FW for latest sequence number plus 1
  2430. */
  2431. class_locale.members.reserved = 0;
  2432. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  2433. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  2434. return megasas_register_aen(instance, eli.newest_seq_num + 1,
  2435. class_locale.word);
  2436. }
  2437. /**
  2438. * megasas_io_attach - Attaches this driver to SCSI mid-layer
  2439. * @instance: Adapter soft state
  2440. */
  2441. static int megasas_io_attach(struct megasas_instance *instance)
  2442. {
  2443. struct Scsi_Host *host = instance->host;
  2444. /*
  2445. * Export parameters required by SCSI mid-layer
  2446. */
  2447. host->irq = instance->pdev->irq;
  2448. host->unique_id = instance->unique_id;
  2449. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2450. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  2451. host->can_queue =
  2452. instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
  2453. } else
  2454. host->can_queue =
  2455. instance->max_fw_cmds - MEGASAS_INT_CMDS;
  2456. host->this_id = instance->init_id;
  2457. host->sg_tablesize = instance->max_num_sge;
  2458. host->max_sectors = instance->max_sectors_per_req;
  2459. host->cmd_per_lun = 128;
  2460. host->max_channel = MEGASAS_MAX_CHANNELS - 1;
  2461. host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
  2462. host->max_lun = MEGASAS_MAX_LUN;
  2463. host->max_cmd_len = 16;
  2464. /*
  2465. * Notify the mid-layer about the new controller
  2466. */
  2467. if (scsi_add_host(host, &instance->pdev->dev)) {
  2468. printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
  2469. return -ENODEV;
  2470. }
  2471. /*
  2472. * Trigger SCSI to scan our drives
  2473. */
  2474. scsi_scan_host(host);
  2475. return 0;
  2476. }
  2477. static int
  2478. megasas_set_dma_mask(struct pci_dev *pdev)
  2479. {
  2480. /*
  2481. * All our contollers are capable of performing 64-bit DMA
  2482. */
  2483. if (IS_DMA64) {
  2484. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
  2485. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
  2486. goto fail_set_dma_mask;
  2487. }
  2488. } else {
  2489. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
  2490. goto fail_set_dma_mask;
  2491. }
  2492. return 0;
  2493. fail_set_dma_mask:
  2494. return 1;
  2495. }
  2496. /**
  2497. * megasas_probe_one - PCI hotplug entry point
  2498. * @pdev: PCI device structure
  2499. * @id: PCI ids of supported hotplugged adapter
  2500. */
  2501. static int __devinit
  2502. megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
  2503. {
  2504. int rval;
  2505. struct Scsi_Host *host;
  2506. struct megasas_instance *instance;
  2507. /*
  2508. * Announce PCI information
  2509. */
  2510. printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
  2511. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  2512. pdev->subsystem_device);
  2513. printk("bus %d:slot %d:func %d\n",
  2514. pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  2515. /*
  2516. * PCI prepping: enable device set bus mastering and dma mask
  2517. */
  2518. rval = pci_enable_device(pdev);
  2519. if (rval) {
  2520. return rval;
  2521. }
  2522. pci_set_master(pdev);
  2523. if (megasas_set_dma_mask(pdev))
  2524. goto fail_set_dma_mask;
  2525. host = scsi_host_alloc(&megasas_template,
  2526. sizeof(struct megasas_instance));
  2527. if (!host) {
  2528. printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
  2529. goto fail_alloc_instance;
  2530. }
  2531. instance = (struct megasas_instance *)host->hostdata;
  2532. memset(instance, 0, sizeof(*instance));
  2533. instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
  2534. &instance->producer_h);
  2535. instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
  2536. &instance->consumer_h);
  2537. if (!instance->producer || !instance->consumer) {
  2538. printk(KERN_DEBUG "megasas: Failed to allocate memory for "
  2539. "producer, consumer\n");
  2540. goto fail_alloc_dma_buf;
  2541. }
  2542. *instance->producer = 0;
  2543. *instance->consumer = 0;
  2544. megasas_poll_wait_aen = 0;
  2545. instance->flag_ieee = 0;
  2546. instance->ev = NULL;
  2547. instance->evt_detail = pci_alloc_consistent(pdev,
  2548. sizeof(struct
  2549. megasas_evt_detail),
  2550. &instance->evt_detail_h);
  2551. if (!instance->evt_detail) {
  2552. printk(KERN_DEBUG "megasas: Failed to allocate memory for "
  2553. "event detail structure\n");
  2554. goto fail_alloc_dma_buf;
  2555. }
  2556. /*
  2557. * Initialize locks and queues
  2558. */
  2559. INIT_LIST_HEAD(&instance->cmd_pool);
  2560. atomic_set(&instance->fw_outstanding,0);
  2561. init_waitqueue_head(&instance->int_cmd_wait_q);
  2562. init_waitqueue_head(&instance->abort_cmd_wait_q);
  2563. spin_lock_init(&instance->cmd_pool_lock);
  2564. spin_lock_init(&instance->fire_lock);
  2565. spin_lock_init(&instance->completion_lock);
  2566. spin_lock_init(&poll_aen_lock);
  2567. mutex_init(&instance->aen_mutex);
  2568. /*
  2569. * Initialize PCI related and misc parameters
  2570. */
  2571. instance->pdev = pdev;
  2572. instance->host = host;
  2573. instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
  2574. instance->init_id = MEGASAS_DEFAULT_INIT_ID;
  2575. if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
  2576. (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
  2577. instance->flag_ieee = 1;
  2578. sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
  2579. } else
  2580. sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
  2581. megasas_dbg_lvl = 0;
  2582. instance->flag = 0;
  2583. instance->unload = 1;
  2584. instance->last_time = 0;
  2585. /*
  2586. * Initialize MFI Firmware
  2587. */
  2588. if (megasas_init_mfi(instance))
  2589. goto fail_init_mfi;
  2590. /*
  2591. * Register IRQ
  2592. */
  2593. if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
  2594. printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
  2595. goto fail_irq;
  2596. }
  2597. instance->instancet->enable_intr(instance->reg_set);
  2598. /*
  2599. * Store instance in PCI softstate
  2600. */
  2601. pci_set_drvdata(pdev, instance);
  2602. /*
  2603. * Add this controller to megasas_mgmt_info structure so that it
  2604. * can be exported to management applications
  2605. */
  2606. megasas_mgmt_info.count++;
  2607. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
  2608. megasas_mgmt_info.max_index++;
  2609. /*
  2610. * Initiate AEN (Asynchronous Event Notification)
  2611. */
  2612. if (megasas_start_aen(instance)) {
  2613. printk(KERN_DEBUG "megasas: start aen failed\n");
  2614. goto fail_start_aen;
  2615. }
  2616. /*
  2617. * Register with SCSI mid-layer
  2618. */
  2619. if (megasas_io_attach(instance))
  2620. goto fail_io_attach;
  2621. instance->unload = 0;
  2622. return 0;
  2623. fail_start_aen:
  2624. fail_io_attach:
  2625. megasas_mgmt_info.count--;
  2626. megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
  2627. megasas_mgmt_info.max_index--;
  2628. pci_set_drvdata(pdev, NULL);
  2629. instance->instancet->disable_intr(instance->reg_set);
  2630. free_irq(instance->pdev->irq, instance);
  2631. megasas_release_mfi(instance);
  2632. fail_irq:
  2633. fail_init_mfi:
  2634. fail_alloc_dma_buf:
  2635. if (instance->evt_detail)
  2636. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  2637. instance->evt_detail,
  2638. instance->evt_detail_h);
  2639. if (instance->producer)
  2640. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  2641. instance->producer_h);
  2642. if (instance->consumer)
  2643. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  2644. instance->consumer_h);
  2645. scsi_host_put(host);
  2646. fail_alloc_instance:
  2647. fail_set_dma_mask:
  2648. pci_disable_device(pdev);
  2649. return -ENODEV;
  2650. }
  2651. /**
  2652. * megasas_flush_cache - Requests FW to flush all its caches
  2653. * @instance: Adapter soft state
  2654. */
  2655. static void megasas_flush_cache(struct megasas_instance *instance)
  2656. {
  2657. struct megasas_cmd *cmd;
  2658. struct megasas_dcmd_frame *dcmd;
  2659. cmd = megasas_get_cmd(instance);
  2660. if (!cmd)
  2661. return;
  2662. dcmd = &cmd->frame->dcmd;
  2663. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2664. dcmd->cmd = MFI_CMD_DCMD;
  2665. dcmd->cmd_status = 0x0;
  2666. dcmd->sge_count = 0;
  2667. dcmd->flags = MFI_FRAME_DIR_NONE;
  2668. dcmd->timeout = 0;
  2669. dcmd->data_xfer_len = 0;
  2670. dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
  2671. dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
  2672. megasas_issue_blocked_cmd(instance, cmd);
  2673. megasas_return_cmd(instance, cmd);
  2674. return;
  2675. }
  2676. /**
  2677. * megasas_shutdown_controller - Instructs FW to shutdown the controller
  2678. * @instance: Adapter soft state
  2679. * @opcode: Shutdown/Hibernate
  2680. */
  2681. static void megasas_shutdown_controller(struct megasas_instance *instance,
  2682. u32 opcode)
  2683. {
  2684. struct megasas_cmd *cmd;
  2685. struct megasas_dcmd_frame *dcmd;
  2686. cmd = megasas_get_cmd(instance);
  2687. if (!cmd)
  2688. return;
  2689. if (instance->aen_cmd)
  2690. megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
  2691. dcmd = &cmd->frame->dcmd;
  2692. memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
  2693. dcmd->cmd = MFI_CMD_DCMD;
  2694. dcmd->cmd_status = 0x0;
  2695. dcmd->sge_count = 0;
  2696. dcmd->flags = MFI_FRAME_DIR_NONE;
  2697. dcmd->timeout = 0;
  2698. dcmd->data_xfer_len = 0;
  2699. dcmd->opcode = opcode;
  2700. megasas_issue_blocked_cmd(instance, cmd);
  2701. megasas_return_cmd(instance, cmd);
  2702. return;
  2703. }
  2704. #ifdef CONFIG_PM
  2705. /**
  2706. * megasas_suspend - driver suspend entry point
  2707. * @pdev: PCI device structure
  2708. * @state: PCI power state to suspend routine
  2709. */
  2710. static int
  2711. megasas_suspend(struct pci_dev *pdev, pm_message_t state)
  2712. {
  2713. struct Scsi_Host *host;
  2714. struct megasas_instance *instance;
  2715. instance = pci_get_drvdata(pdev);
  2716. host = instance->host;
  2717. instance->unload = 1;
  2718. if (poll_mode_io)
  2719. del_timer_sync(&instance->io_completion_timer);
  2720. megasas_flush_cache(instance);
  2721. megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
  2722. /* cancel the delayed work if this work still in queue */
  2723. if (instance->ev != NULL) {
  2724. struct megasas_aen_event *ev = instance->ev;
  2725. cancel_delayed_work(
  2726. (struct delayed_work *)&ev->hotplug_work);
  2727. flush_scheduled_work();
  2728. instance->ev = NULL;
  2729. }
  2730. tasklet_kill(&instance->isr_tasklet);
  2731. pci_set_drvdata(instance->pdev, instance);
  2732. instance->instancet->disable_intr(instance->reg_set);
  2733. free_irq(instance->pdev->irq, instance);
  2734. pci_save_state(pdev);
  2735. pci_disable_device(pdev);
  2736. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  2737. return 0;
  2738. }
  2739. /**
  2740. * megasas_resume- driver resume entry point
  2741. * @pdev: PCI device structure
  2742. */
  2743. static int
  2744. megasas_resume(struct pci_dev *pdev)
  2745. {
  2746. int rval;
  2747. struct Scsi_Host *host;
  2748. struct megasas_instance *instance;
  2749. instance = pci_get_drvdata(pdev);
  2750. host = instance->host;
  2751. pci_set_power_state(pdev, PCI_D0);
  2752. pci_enable_wake(pdev, PCI_D0, 0);
  2753. pci_restore_state(pdev);
  2754. /*
  2755. * PCI prepping: enable device set bus mastering and dma mask
  2756. */
  2757. rval = pci_enable_device(pdev);
  2758. if (rval) {
  2759. printk(KERN_ERR "megasas: Enable device failed\n");
  2760. return rval;
  2761. }
  2762. pci_set_master(pdev);
  2763. if (megasas_set_dma_mask(pdev))
  2764. goto fail_set_dma_mask;
  2765. /*
  2766. * Initialize MFI Firmware
  2767. */
  2768. *instance->producer = 0;
  2769. *instance->consumer = 0;
  2770. atomic_set(&instance->fw_outstanding, 0);
  2771. /*
  2772. * We expect the FW state to be READY
  2773. */
  2774. if (megasas_transition_to_ready(instance))
  2775. goto fail_ready_state;
  2776. if (megasas_issue_init_mfi(instance))
  2777. goto fail_init_mfi;
  2778. tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
  2779. (unsigned long)instance);
  2780. /*
  2781. * Register IRQ
  2782. */
  2783. if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED,
  2784. "megasas", instance)) {
  2785. printk(KERN_ERR "megasas: Failed to register IRQ\n");
  2786. goto fail_irq;
  2787. }
  2788. instance->instancet->enable_intr(instance->reg_set);
  2789. /*
  2790. * Initiate AEN (Asynchronous Event Notification)
  2791. */
  2792. if (megasas_start_aen(instance))
  2793. printk(KERN_ERR "megasas: Start AEN failed\n");
  2794. /* Initialize the cmd completion timer */
  2795. if (poll_mode_io)
  2796. megasas_start_timer(instance, &instance->io_completion_timer,
  2797. megasas_io_completion_timer,
  2798. MEGASAS_COMPLETION_TIMER_INTERVAL);
  2799. instance->unload = 0;
  2800. return 0;
  2801. fail_irq:
  2802. fail_init_mfi:
  2803. if (instance->evt_detail)
  2804. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  2805. instance->evt_detail,
  2806. instance->evt_detail_h);
  2807. if (instance->producer)
  2808. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  2809. instance->producer_h);
  2810. if (instance->consumer)
  2811. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  2812. instance->consumer_h);
  2813. scsi_host_put(host);
  2814. fail_set_dma_mask:
  2815. fail_ready_state:
  2816. pci_disable_device(pdev);
  2817. return -ENODEV;
  2818. }
  2819. #else
  2820. #define megasas_suspend NULL
  2821. #define megasas_resume NULL
  2822. #endif
  2823. /**
  2824. * megasas_detach_one - PCI hot"un"plug entry point
  2825. * @pdev: PCI device structure
  2826. */
  2827. static void __devexit megasas_detach_one(struct pci_dev *pdev)
  2828. {
  2829. int i;
  2830. struct Scsi_Host *host;
  2831. struct megasas_instance *instance;
  2832. instance = pci_get_drvdata(pdev);
  2833. instance->unload = 1;
  2834. host = instance->host;
  2835. if (poll_mode_io)
  2836. del_timer_sync(&instance->io_completion_timer);
  2837. scsi_remove_host(instance->host);
  2838. megasas_flush_cache(instance);
  2839. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  2840. /* cancel the delayed work if this work still in queue*/
  2841. if (instance->ev != NULL) {
  2842. struct megasas_aen_event *ev = instance->ev;
  2843. cancel_delayed_work(
  2844. (struct delayed_work *)&ev->hotplug_work);
  2845. flush_scheduled_work();
  2846. instance->ev = NULL;
  2847. }
  2848. tasklet_kill(&instance->isr_tasklet);
  2849. /*
  2850. * Take the instance off the instance array. Note that we will not
  2851. * decrement the max_index. We let this array be sparse array
  2852. */
  2853. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  2854. if (megasas_mgmt_info.instance[i] == instance) {
  2855. megasas_mgmt_info.count--;
  2856. megasas_mgmt_info.instance[i] = NULL;
  2857. break;
  2858. }
  2859. }
  2860. pci_set_drvdata(instance->pdev, NULL);
  2861. instance->instancet->disable_intr(instance->reg_set);
  2862. free_irq(instance->pdev->irq, instance);
  2863. megasas_release_mfi(instance);
  2864. pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
  2865. instance->evt_detail, instance->evt_detail_h);
  2866. pci_free_consistent(pdev, sizeof(u32), instance->producer,
  2867. instance->producer_h);
  2868. pci_free_consistent(pdev, sizeof(u32), instance->consumer,
  2869. instance->consumer_h);
  2870. scsi_host_put(host);
  2871. pci_set_drvdata(pdev, NULL);
  2872. pci_disable_device(pdev);
  2873. return;
  2874. }
  2875. /**
  2876. * megasas_shutdown - Shutdown entry point
  2877. * @device: Generic device structure
  2878. */
  2879. static void megasas_shutdown(struct pci_dev *pdev)
  2880. {
  2881. struct megasas_instance *instance = pci_get_drvdata(pdev);
  2882. instance->unload = 1;
  2883. megasas_flush_cache(instance);
  2884. megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
  2885. }
  2886. /**
  2887. * megasas_mgmt_open - char node "open" entry point
  2888. */
  2889. static int megasas_mgmt_open(struct inode *inode, struct file *filep)
  2890. {
  2891. cycle_kernel_lock();
  2892. /*
  2893. * Allow only those users with admin rights
  2894. */
  2895. if (!capable(CAP_SYS_ADMIN))
  2896. return -EACCES;
  2897. return 0;
  2898. }
  2899. /**
  2900. * megasas_mgmt_fasync - Async notifier registration from applications
  2901. *
  2902. * This function adds the calling process to a driver global queue. When an
  2903. * event occurs, SIGIO will be sent to all processes in this queue.
  2904. */
  2905. static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
  2906. {
  2907. int rc;
  2908. mutex_lock(&megasas_async_queue_mutex);
  2909. rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
  2910. mutex_unlock(&megasas_async_queue_mutex);
  2911. if (rc >= 0) {
  2912. /* For sanity check when we get ioctl */
  2913. filep->private_data = filep;
  2914. return 0;
  2915. }
  2916. printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
  2917. return rc;
  2918. }
  2919. /**
  2920. * megasas_mgmt_poll - char node "poll" entry point
  2921. * */
  2922. static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
  2923. {
  2924. unsigned int mask;
  2925. unsigned long flags;
  2926. poll_wait(file, &megasas_poll_wait, wait);
  2927. spin_lock_irqsave(&poll_aen_lock, flags);
  2928. if (megasas_poll_wait_aen)
  2929. mask = (POLLIN | POLLRDNORM);
  2930. else
  2931. mask = 0;
  2932. spin_unlock_irqrestore(&poll_aen_lock, flags);
  2933. return mask;
  2934. }
  2935. /**
  2936. * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
  2937. * @instance: Adapter soft state
  2938. * @argp: User's ioctl packet
  2939. */
  2940. static int
  2941. megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
  2942. struct megasas_iocpacket __user * user_ioc,
  2943. struct megasas_iocpacket *ioc)
  2944. {
  2945. struct megasas_sge32 *kern_sge32;
  2946. struct megasas_cmd *cmd;
  2947. void *kbuff_arr[MAX_IOCTL_SGE];
  2948. dma_addr_t buf_handle = 0;
  2949. int error = 0, i;
  2950. void *sense = NULL;
  2951. dma_addr_t sense_handle;
  2952. unsigned long *sense_ptr;
  2953. memset(kbuff_arr, 0, sizeof(kbuff_arr));
  2954. if (ioc->sge_count > MAX_IOCTL_SGE) {
  2955. printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
  2956. ioc->sge_count, MAX_IOCTL_SGE);
  2957. return -EINVAL;
  2958. }
  2959. cmd = megasas_get_cmd(instance);
  2960. if (!cmd) {
  2961. printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
  2962. return -ENOMEM;
  2963. }
  2964. /*
  2965. * User's IOCTL packet has 2 frames (maximum). Copy those two
  2966. * frames into our cmd's frames. cmd->frame's context will get
  2967. * overwritten when we copy from user's frames. So set that value
  2968. * alone separately
  2969. */
  2970. memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
  2971. cmd->frame->hdr.context = cmd->index;
  2972. cmd->frame->hdr.pad_0 = 0;
  2973. /*
  2974. * The management interface between applications and the fw uses
  2975. * MFI frames. E.g, RAID configuration changes, LD property changes
  2976. * etc are accomplishes through different kinds of MFI frames. The
  2977. * driver needs to care only about substituting user buffers with
  2978. * kernel buffers in SGLs. The location of SGL is embedded in the
  2979. * struct iocpacket itself.
  2980. */
  2981. kern_sge32 = (struct megasas_sge32 *)
  2982. ((unsigned long)cmd->frame + ioc->sgl_off);
  2983. /*
  2984. * For each user buffer, create a mirror buffer and copy in
  2985. */
  2986. for (i = 0; i < ioc->sge_count; i++) {
  2987. kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
  2988. ioc->sgl[i].iov_len,
  2989. &buf_handle, GFP_KERNEL);
  2990. if (!kbuff_arr[i]) {
  2991. printk(KERN_DEBUG "megasas: Failed to alloc "
  2992. "kernel SGL buffer for IOCTL \n");
  2993. error = -ENOMEM;
  2994. goto out;
  2995. }
  2996. /*
  2997. * We don't change the dma_coherent_mask, so
  2998. * pci_alloc_consistent only returns 32bit addresses
  2999. */
  3000. kern_sge32[i].phys_addr = (u32) buf_handle;
  3001. kern_sge32[i].length = ioc->sgl[i].iov_len;
  3002. /*
  3003. * We created a kernel buffer corresponding to the
  3004. * user buffer. Now copy in from the user buffer
  3005. */
  3006. if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
  3007. (u32) (ioc->sgl[i].iov_len))) {
  3008. error = -EFAULT;
  3009. goto out;
  3010. }
  3011. }
  3012. if (ioc->sense_len) {
  3013. sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
  3014. &sense_handle, GFP_KERNEL);
  3015. if (!sense) {
  3016. error = -ENOMEM;
  3017. goto out;
  3018. }
  3019. sense_ptr =
  3020. (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
  3021. *sense_ptr = sense_handle;
  3022. }
  3023. /*
  3024. * Set the sync_cmd flag so that the ISR knows not to complete this
  3025. * cmd to the SCSI mid-layer
  3026. */
  3027. cmd->sync_cmd = 1;
  3028. megasas_issue_blocked_cmd(instance, cmd);
  3029. cmd->sync_cmd = 0;
  3030. /*
  3031. * copy out the kernel buffers to user buffers
  3032. */
  3033. for (i = 0; i < ioc->sge_count; i++) {
  3034. if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
  3035. ioc->sgl[i].iov_len)) {
  3036. error = -EFAULT;
  3037. goto out;
  3038. }
  3039. }
  3040. /*
  3041. * copy out the sense
  3042. */
  3043. if (ioc->sense_len) {
  3044. /*
  3045. * sense_ptr points to the location that has the user
  3046. * sense buffer address
  3047. */
  3048. sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
  3049. ioc->sense_off);
  3050. if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
  3051. sense, ioc->sense_len)) {
  3052. printk(KERN_ERR "megasas: Failed to copy out to user "
  3053. "sense data\n");
  3054. error = -EFAULT;
  3055. goto out;
  3056. }
  3057. }
  3058. /*
  3059. * copy the status codes returned by the fw
  3060. */
  3061. if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
  3062. &cmd->frame->hdr.cmd_status, sizeof(u8))) {
  3063. printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
  3064. error = -EFAULT;
  3065. }
  3066. out:
  3067. if (sense) {
  3068. dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
  3069. sense, sense_handle);
  3070. }
  3071. for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
  3072. dma_free_coherent(&instance->pdev->dev,
  3073. kern_sge32[i].length,
  3074. kbuff_arr[i], kern_sge32[i].phys_addr);
  3075. }
  3076. megasas_return_cmd(instance, cmd);
  3077. return error;
  3078. }
  3079. static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
  3080. {
  3081. struct megasas_iocpacket __user *user_ioc =
  3082. (struct megasas_iocpacket __user *)arg;
  3083. struct megasas_iocpacket *ioc;
  3084. struct megasas_instance *instance;
  3085. int error;
  3086. ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
  3087. if (!ioc)
  3088. return -ENOMEM;
  3089. if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
  3090. error = -EFAULT;
  3091. goto out_kfree_ioc;
  3092. }
  3093. instance = megasas_lookup_instance(ioc->host_no);
  3094. if (!instance) {
  3095. error = -ENODEV;
  3096. goto out_kfree_ioc;
  3097. }
  3098. if (instance->hw_crit_error == 1) {
  3099. printk(KERN_DEBUG "Controller in Crit ERROR\n");
  3100. error = -ENODEV;
  3101. goto out_kfree_ioc;
  3102. }
  3103. if (instance->unload == 1) {
  3104. error = -ENODEV;
  3105. goto out_kfree_ioc;
  3106. }
  3107. /*
  3108. * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
  3109. */
  3110. if (down_interruptible(&instance->ioctl_sem)) {
  3111. error = -ERESTARTSYS;
  3112. goto out_kfree_ioc;
  3113. }
  3114. error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
  3115. up(&instance->ioctl_sem);
  3116. out_kfree_ioc:
  3117. kfree(ioc);
  3118. return error;
  3119. }
  3120. static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
  3121. {
  3122. struct megasas_instance *instance;
  3123. struct megasas_aen aen;
  3124. int error;
  3125. if (file->private_data != file) {
  3126. printk(KERN_DEBUG "megasas: fasync_helper was not "
  3127. "called first\n");
  3128. return -EINVAL;
  3129. }
  3130. if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
  3131. return -EFAULT;
  3132. instance = megasas_lookup_instance(aen.host_no);
  3133. if (!instance)
  3134. return -ENODEV;
  3135. if (instance->hw_crit_error == 1) {
  3136. error = -ENODEV;
  3137. }
  3138. if (instance->unload == 1) {
  3139. return -ENODEV;
  3140. }
  3141. mutex_lock(&instance->aen_mutex);
  3142. error = megasas_register_aen(instance, aen.seq_num,
  3143. aen.class_locale_word);
  3144. mutex_unlock(&instance->aen_mutex);
  3145. return error;
  3146. }
  3147. /**
  3148. * megasas_mgmt_ioctl - char node ioctl entry point
  3149. */
  3150. static long
  3151. megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  3152. {
  3153. switch (cmd) {
  3154. case MEGASAS_IOC_FIRMWARE:
  3155. return megasas_mgmt_ioctl_fw(file, arg);
  3156. case MEGASAS_IOC_GET_AEN:
  3157. return megasas_mgmt_ioctl_aen(file, arg);
  3158. }
  3159. return -ENOTTY;
  3160. }
  3161. #ifdef CONFIG_COMPAT
  3162. static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
  3163. {
  3164. struct compat_megasas_iocpacket __user *cioc =
  3165. (struct compat_megasas_iocpacket __user *)arg;
  3166. struct megasas_iocpacket __user *ioc =
  3167. compat_alloc_user_space(sizeof(struct megasas_iocpacket));
  3168. int i;
  3169. int error = 0;
  3170. if (clear_user(ioc, sizeof(*ioc)))
  3171. return -EFAULT;
  3172. if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
  3173. copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
  3174. copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
  3175. copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
  3176. copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
  3177. copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
  3178. return -EFAULT;
  3179. for (i = 0; i < MAX_IOCTL_SGE; i++) {
  3180. compat_uptr_t ptr;
  3181. if (get_user(ptr, &cioc->sgl[i].iov_base) ||
  3182. put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
  3183. copy_in_user(&ioc->sgl[i].iov_len,
  3184. &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
  3185. return -EFAULT;
  3186. }
  3187. error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
  3188. if (copy_in_user(&cioc->frame.hdr.cmd_status,
  3189. &ioc->frame.hdr.cmd_status, sizeof(u8))) {
  3190. printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
  3191. return -EFAULT;
  3192. }
  3193. return error;
  3194. }
  3195. static long
  3196. megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
  3197. unsigned long arg)
  3198. {
  3199. switch (cmd) {
  3200. case MEGASAS_IOC_FIRMWARE32:
  3201. return megasas_mgmt_compat_ioctl_fw(file, arg);
  3202. case MEGASAS_IOC_GET_AEN:
  3203. return megasas_mgmt_ioctl_aen(file, arg);
  3204. }
  3205. return -ENOTTY;
  3206. }
  3207. #endif
  3208. /*
  3209. * File operations structure for management interface
  3210. */
  3211. static const struct file_operations megasas_mgmt_fops = {
  3212. .owner = THIS_MODULE,
  3213. .open = megasas_mgmt_open,
  3214. .fasync = megasas_mgmt_fasync,
  3215. .unlocked_ioctl = megasas_mgmt_ioctl,
  3216. .poll = megasas_mgmt_poll,
  3217. #ifdef CONFIG_COMPAT
  3218. .compat_ioctl = megasas_mgmt_compat_ioctl,
  3219. #endif
  3220. };
  3221. /*
  3222. * PCI hotplug support registration structure
  3223. */
  3224. static struct pci_driver megasas_pci_driver = {
  3225. .name = "megaraid_sas",
  3226. .id_table = megasas_pci_table,
  3227. .probe = megasas_probe_one,
  3228. .remove = __devexit_p(megasas_detach_one),
  3229. .suspend = megasas_suspend,
  3230. .resume = megasas_resume,
  3231. .shutdown = megasas_shutdown,
  3232. };
  3233. /*
  3234. * Sysfs driver attributes
  3235. */
  3236. static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
  3237. {
  3238. return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
  3239. MEGASAS_VERSION);
  3240. }
  3241. static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
  3242. static ssize_t
  3243. megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
  3244. {
  3245. return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
  3246. MEGASAS_RELDATE);
  3247. }
  3248. static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
  3249. NULL);
  3250. static ssize_t
  3251. megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
  3252. {
  3253. return sprintf(buf, "%u\n", support_poll_for_event);
  3254. }
  3255. static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
  3256. megasas_sysfs_show_support_poll_for_event, NULL);
  3257. static ssize_t
  3258. megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
  3259. {
  3260. return sprintf(buf, "%u\n", megasas_dbg_lvl);
  3261. }
  3262. static ssize_t
  3263. megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
  3264. {
  3265. int retval = count;
  3266. if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
  3267. printk(KERN_ERR "megasas: could not set dbg_lvl\n");
  3268. retval = -EINVAL;
  3269. }
  3270. return retval;
  3271. }
  3272. static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
  3273. megasas_sysfs_set_dbg_lvl);
  3274. static ssize_t
  3275. megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
  3276. {
  3277. return sprintf(buf, "%u\n", poll_mode_io);
  3278. }
  3279. static ssize_t
  3280. megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
  3281. const char *buf, size_t count)
  3282. {
  3283. int retval = count;
  3284. int tmp = poll_mode_io;
  3285. int i;
  3286. struct megasas_instance *instance;
  3287. if (sscanf(buf, "%u", &poll_mode_io) < 1) {
  3288. printk(KERN_ERR "megasas: could not set poll_mode_io\n");
  3289. retval = -EINVAL;
  3290. }
  3291. /*
  3292. * Check if poll_mode_io is already set or is same as previous value
  3293. */
  3294. if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
  3295. goto out;
  3296. if (poll_mode_io) {
  3297. /*
  3298. * Start timers for all adapters
  3299. */
  3300. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  3301. instance = megasas_mgmt_info.instance[i];
  3302. if (instance) {
  3303. megasas_start_timer(instance,
  3304. &instance->io_completion_timer,
  3305. megasas_io_completion_timer,
  3306. MEGASAS_COMPLETION_TIMER_INTERVAL);
  3307. }
  3308. }
  3309. } else {
  3310. /*
  3311. * Delete timers for all adapters
  3312. */
  3313. for (i = 0; i < megasas_mgmt_info.max_index; i++) {
  3314. instance = megasas_mgmt_info.instance[i];
  3315. if (instance)
  3316. del_timer_sync(&instance->io_completion_timer);
  3317. }
  3318. }
  3319. out:
  3320. return retval;
  3321. }
  3322. static void
  3323. megasas_aen_polling(struct work_struct *work)
  3324. {
  3325. struct megasas_aen_event *ev =
  3326. container_of(work, struct megasas_aen_event, hotplug_work);
  3327. struct megasas_instance *instance = ev->instance;
  3328. union megasas_evt_class_locale class_locale;
  3329. struct Scsi_Host *host;
  3330. struct scsi_device *sdev1;
  3331. u16 pd_index = 0;
  3332. int i, j, doscan = 0;
  3333. u32 seq_num;
  3334. int error;
  3335. if (!instance) {
  3336. printk(KERN_ERR "invalid instance!\n");
  3337. kfree(ev);
  3338. return;
  3339. }
  3340. instance->ev = NULL;
  3341. host = instance->host;
  3342. if (instance->evt_detail) {
  3343. switch (instance->evt_detail->code) {
  3344. case MR_EVT_PD_INSERTED:
  3345. case MR_EVT_PD_REMOVED:
  3346. case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
  3347. doscan = 1;
  3348. break;
  3349. default:
  3350. doscan = 0;
  3351. break;
  3352. }
  3353. } else {
  3354. printk(KERN_ERR "invalid evt_detail!\n");
  3355. kfree(ev);
  3356. return;
  3357. }
  3358. if (doscan) {
  3359. printk(KERN_INFO "scanning ...\n");
  3360. megasas_get_pd_list(instance);
  3361. for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
  3362. for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
  3363. pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
  3364. sdev1 = scsi_device_lookup(host, i, j, 0);
  3365. if (instance->pd_list[pd_index].driveState ==
  3366. MR_PD_STATE_SYSTEM) {
  3367. if (!sdev1) {
  3368. scsi_add_device(host, i, j, 0);
  3369. }
  3370. if (sdev1)
  3371. scsi_device_put(sdev1);
  3372. } else {
  3373. if (sdev1) {
  3374. scsi_remove_device(sdev1);
  3375. scsi_device_put(sdev1);
  3376. }
  3377. }
  3378. }
  3379. }
  3380. }
  3381. if ( instance->aen_cmd != NULL ) {
  3382. kfree(ev);
  3383. return ;
  3384. }
  3385. seq_num = instance->evt_detail->seq_num + 1;
  3386. /* Register AEN with FW for latest sequence number plus 1 */
  3387. class_locale.members.reserved = 0;
  3388. class_locale.members.locale = MR_EVT_LOCALE_ALL;
  3389. class_locale.members.class = MR_EVT_CLASS_DEBUG;
  3390. mutex_lock(&instance->aen_mutex);
  3391. error = megasas_register_aen(instance, seq_num,
  3392. class_locale.word);
  3393. mutex_unlock(&instance->aen_mutex);
  3394. if (error)
  3395. printk(KERN_ERR "register aen failed error %x\n", error);
  3396. kfree(ev);
  3397. }
  3398. static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUGO,
  3399. megasas_sysfs_show_poll_mode_io,
  3400. megasas_sysfs_set_poll_mode_io);
  3401. /**
  3402. * megasas_init - Driver load entry point
  3403. */
  3404. static int __init megasas_init(void)
  3405. {
  3406. int rval;
  3407. /*
  3408. * Announce driver version and other information
  3409. */
  3410. printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
  3411. MEGASAS_EXT_VERSION);
  3412. support_poll_for_event = 2;
  3413. memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
  3414. /*
  3415. * Register character device node
  3416. */
  3417. rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
  3418. if (rval < 0) {
  3419. printk(KERN_DEBUG "megasas: failed to open device node\n");
  3420. return rval;
  3421. }
  3422. megasas_mgmt_majorno = rval;
  3423. /*
  3424. * Register ourselves as PCI hotplug module
  3425. */
  3426. rval = pci_register_driver(&megasas_pci_driver);
  3427. if (rval) {
  3428. printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
  3429. goto err_pcidrv;
  3430. }
  3431. rval = driver_create_file(&megasas_pci_driver.driver,
  3432. &driver_attr_version);
  3433. if (rval)
  3434. goto err_dcf_attr_ver;
  3435. rval = driver_create_file(&megasas_pci_driver.driver,
  3436. &driver_attr_release_date);
  3437. if (rval)
  3438. goto err_dcf_rel_date;
  3439. rval = driver_create_file(&megasas_pci_driver.driver,
  3440. &driver_attr_support_poll_for_event);
  3441. if (rval)
  3442. goto err_dcf_support_poll_for_event;
  3443. rval = driver_create_file(&megasas_pci_driver.driver,
  3444. &driver_attr_dbg_lvl);
  3445. if (rval)
  3446. goto err_dcf_dbg_lvl;
  3447. rval = driver_create_file(&megasas_pci_driver.driver,
  3448. &driver_attr_poll_mode_io);
  3449. if (rval)
  3450. goto err_dcf_poll_mode_io;
  3451. return rval;
  3452. err_dcf_poll_mode_io:
  3453. driver_remove_file(&megasas_pci_driver.driver,
  3454. &driver_attr_dbg_lvl);
  3455. err_dcf_dbg_lvl:
  3456. driver_remove_file(&megasas_pci_driver.driver,
  3457. &driver_attr_support_poll_for_event);
  3458. err_dcf_support_poll_for_event:
  3459. driver_remove_file(&megasas_pci_driver.driver,
  3460. &driver_attr_release_date);
  3461. err_dcf_rel_date:
  3462. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  3463. err_dcf_attr_ver:
  3464. pci_unregister_driver(&megasas_pci_driver);
  3465. err_pcidrv:
  3466. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  3467. return rval;
  3468. }
  3469. /**
  3470. * megasas_exit - Driver unload entry point
  3471. */
  3472. static void __exit megasas_exit(void)
  3473. {
  3474. driver_remove_file(&megasas_pci_driver.driver,
  3475. &driver_attr_poll_mode_io);
  3476. driver_remove_file(&megasas_pci_driver.driver,
  3477. &driver_attr_dbg_lvl);
  3478. driver_remove_file(&megasas_pci_driver.driver,
  3479. &driver_attr_release_date);
  3480. driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
  3481. pci_unregister_driver(&megasas_pci_driver);
  3482. unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
  3483. }
  3484. module_init(megasas_init);
  3485. module_exit(megasas_exit);