megaraid_sas.c 70 KB

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