megaraid_sas.c 85 KB

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