megaraid_sas.c 76 KB

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