mtip32xx.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568
  1. /*
  2. * Driver for the Micron P320 SSD
  3. * Copyright (C) 2011 Micron Technology, Inc.
  4. *
  5. * Portions of this code were derived from works subjected to the
  6. * following copyright:
  7. * Copyright (C) 2009 Integrated Device Technology, Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. */
  20. #include <linux/pci.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/ata.h>
  23. #include <linux/delay.h>
  24. #include <linux/hdreg.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/random.h>
  27. #include <linux/smp.h>
  28. #include <linux/compat.h>
  29. #include <linux/fs.h>
  30. #include <linux/genhd.h>
  31. #include <linux/blkdev.h>
  32. #include <linux/bio.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/idr.h>
  35. #include <../drivers/ata/ahci.h>
  36. #include "mtip32xx.h"
  37. #define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
  38. #define HW_CMD_TBL_SZ (AHCI_CMD_TBL_HDR_SZ + (MTIP_MAX_SG * 16))
  39. #define HW_CMD_TBL_AR_SZ (HW_CMD_TBL_SZ * MTIP_MAX_COMMAND_SLOTS)
  40. #define HW_PORT_PRIV_DMA_SZ \
  41. (HW_CMD_SLOT_SZ + HW_CMD_TBL_AR_SZ + AHCI_RX_FIS_SZ)
  42. #define HOST_HSORG 0xFC
  43. #define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
  44. #define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
  45. #define HSORG_HWREV 0xFF00
  46. #define HSORG_STYLE 0x8
  47. #define HSORG_SLOTGROUPS 0x7
  48. #define PORT_COMMAND_ISSUE 0x38
  49. #define PORT_SDBV 0x7C
  50. #define PORT_OFFSET 0x100
  51. #define PORT_MEM_SIZE 0x80
  52. #define PORT_IRQ_ERR \
  53. (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
  54. PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
  55. PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
  56. PORT_IRQ_OVERFLOW)
  57. #define PORT_IRQ_LEGACY \
  58. (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
  59. #define PORT_IRQ_HANDLED \
  60. (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
  61. PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
  62. PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
  63. #define DEF_PORT_IRQ \
  64. (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
  65. /* product numbers */
  66. #define MTIP_PRODUCT_UNKNOWN 0x00
  67. #define MTIP_PRODUCT_ASICFPGA 0x11
  68. /* Device instance number, incremented each time a device is probed. */
  69. static int instance;
  70. /*
  71. * Global variable used to hold the major block device number
  72. * allocated in mtip_init().
  73. */
  74. int mtip_major;
  75. static DEFINE_SPINLOCK(rssd_index_lock);
  76. static DEFINE_IDA(rssd_index_ida);
  77. #ifdef CONFIG_COMPAT
  78. struct mtip_compat_ide_task_request_s {
  79. __u8 io_ports[8];
  80. __u8 hob_ports[8];
  81. ide_reg_valid_t out_flags;
  82. ide_reg_valid_t in_flags;
  83. int data_phase;
  84. int req_cmd;
  85. compat_ulong_t out_size;
  86. compat_ulong_t in_size;
  87. };
  88. #endif
  89. static int mtip_exec_internal_command(struct mtip_port *port,
  90. void *fis,
  91. int fisLen,
  92. dma_addr_t buffer,
  93. int bufLen,
  94. u32 opts,
  95. gfp_t atomic,
  96. unsigned long timeout);
  97. /*
  98. * This function check_for_surprise_removal is called
  99. * while card is removed from the system and it will
  100. * read the vendor id from the configration space
  101. *
  102. * @pdev Pointer to the pci_dev structure.
  103. *
  104. * return value
  105. * true if device removed, else false
  106. */
  107. static bool mtip_check_surprise_removal(struct pci_dev *pdev)
  108. {
  109. u16 vendor_id = 0;
  110. /* Read the vendorID from the configuration space */
  111. pci_read_config_word(pdev, 0x00, &vendor_id);
  112. if (vendor_id == 0xFFFF)
  113. return true; /* device removed */
  114. return false; /* device present */
  115. }
  116. /*
  117. * This function is called for clean the pending command in the
  118. * command slot during the surprise removal of device and return
  119. * error to the upper layer.
  120. *
  121. * @dd Pointer to the DRIVER_DATA structure.
  122. *
  123. * return value
  124. * None
  125. */
  126. static void mtip_command_cleanup(struct driver_data *dd)
  127. {
  128. int group = 0, commandslot = 0, commandindex = 0;
  129. struct mtip_cmd *command;
  130. struct mtip_port *port = dd->port;
  131. for (group = 0; group < 4; group++) {
  132. for (commandslot = 0; commandslot < 32; commandslot++) {
  133. if (!(port->allocated[group] & (1 << commandslot)))
  134. continue;
  135. commandindex = group << 5 | commandslot;
  136. command = &port->commands[commandindex];
  137. if (atomic_read(&command->active)
  138. && (command->async_callback)) {
  139. command->async_callback(command->async_data,
  140. -ENODEV);
  141. command->async_callback = NULL;
  142. command->async_data = NULL;
  143. }
  144. dma_unmap_sg(&port->dd->pdev->dev,
  145. command->sg,
  146. command->scatter_ents,
  147. command->direction);
  148. }
  149. }
  150. up(&port->cmd_slot);
  151. atomic_set(&dd->drv_cleanup_done, true);
  152. }
  153. /*
  154. * Obtain an empty command slot.
  155. *
  156. * This function needs to be reentrant since it could be called
  157. * at the same time on multiple CPUs. The allocation of the
  158. * command slot must be atomic.
  159. *
  160. * @port Pointer to the port data structure.
  161. *
  162. * return value
  163. * >= 0 Index of command slot obtained.
  164. * -1 No command slots available.
  165. */
  166. static int get_slot(struct mtip_port *port)
  167. {
  168. int slot, i;
  169. unsigned int num_command_slots = port->dd->slot_groups * 32;
  170. /*
  171. * Try 10 times, because there is a small race here.
  172. * that's ok, because it's still cheaper than a lock.
  173. *
  174. * Race: Since this section is not protected by lock, same bit
  175. * could be chosen by different process contexts running in
  176. * different processor. So instead of costly lock, we are going
  177. * with loop.
  178. */
  179. for (i = 0; i < 10; i++) {
  180. slot = find_next_zero_bit(port->allocated,
  181. num_command_slots, 1);
  182. if ((slot < num_command_slots) &&
  183. (!test_and_set_bit(slot, port->allocated)))
  184. return slot;
  185. }
  186. dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
  187. if (mtip_check_surprise_removal(port->dd->pdev)) {
  188. /* Device not present, clean outstanding commands */
  189. mtip_command_cleanup(port->dd);
  190. }
  191. return -1;
  192. }
  193. /*
  194. * Release a command slot.
  195. *
  196. * @port Pointer to the port data structure.
  197. * @tag Tag of command to release
  198. *
  199. * return value
  200. * None
  201. */
  202. static inline void release_slot(struct mtip_port *port, int tag)
  203. {
  204. smp_mb__before_clear_bit();
  205. clear_bit(tag, port->allocated);
  206. smp_mb__after_clear_bit();
  207. }
  208. /*
  209. * Reset the HBA (without sleeping)
  210. *
  211. * Just like hba_reset, except does not call sleep, so can be
  212. * run from interrupt/tasklet context.
  213. *
  214. * @dd Pointer to the driver data structure.
  215. *
  216. * return value
  217. * 0 The reset was successful.
  218. * -1 The HBA Reset bit did not clear.
  219. */
  220. static int hba_reset_nosleep(struct driver_data *dd)
  221. {
  222. unsigned long timeout;
  223. /* Chip quirk: quiesce any chip function */
  224. mdelay(10);
  225. /* Set the reset bit */
  226. writel(HOST_RESET, dd->mmio + HOST_CTL);
  227. /* Flush */
  228. readl(dd->mmio + HOST_CTL);
  229. /*
  230. * Wait 10ms then spin for up to 1 second
  231. * waiting for reset acknowledgement
  232. */
  233. timeout = jiffies + msecs_to_jiffies(1000);
  234. mdelay(10);
  235. while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
  236. && time_before(jiffies, timeout))
  237. mdelay(1);
  238. if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
  239. return -1;
  240. return 0;
  241. }
  242. /*
  243. * Issue a command to the hardware.
  244. *
  245. * Set the appropriate bit in the s_active and Command Issue hardware
  246. * registers, causing hardware command processing to begin.
  247. *
  248. * @port Pointer to the port structure.
  249. * @tag The tag of the command to be issued.
  250. *
  251. * return value
  252. * None
  253. */
  254. static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
  255. {
  256. unsigned long flags = 0;
  257. atomic_set(&port->commands[tag].active, 1);
  258. spin_lock_irqsave(&port->cmd_issue_lock, flags);
  259. writel((1 << MTIP_TAG_BIT(tag)),
  260. port->s_active[MTIP_TAG_INDEX(tag)]);
  261. writel((1 << MTIP_TAG_BIT(tag)),
  262. port->cmd_issue[MTIP_TAG_INDEX(tag)]);
  263. spin_unlock_irqrestore(&port->cmd_issue_lock, flags);
  264. }
  265. /*
  266. * Enable/disable the reception of FIS
  267. *
  268. * @port Pointer to the port data structure
  269. * @enable 1 to enable, 0 to disable
  270. *
  271. * return value
  272. * Previous state: 1 enabled, 0 disabled
  273. */
  274. static int mtip_enable_fis(struct mtip_port *port, int enable)
  275. {
  276. u32 tmp;
  277. /* enable FIS reception */
  278. tmp = readl(port->mmio + PORT_CMD);
  279. if (enable)
  280. writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
  281. else
  282. writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
  283. /* Flush */
  284. readl(port->mmio + PORT_CMD);
  285. return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
  286. }
  287. /*
  288. * Enable/disable the DMA engine
  289. *
  290. * @port Pointer to the port data structure
  291. * @enable 1 to enable, 0 to disable
  292. *
  293. * return value
  294. * Previous state: 1 enabled, 0 disabled.
  295. */
  296. static int mtip_enable_engine(struct mtip_port *port, int enable)
  297. {
  298. u32 tmp;
  299. /* enable FIS reception */
  300. tmp = readl(port->mmio + PORT_CMD);
  301. if (enable)
  302. writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
  303. else
  304. writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
  305. readl(port->mmio + PORT_CMD);
  306. return (((tmp & PORT_CMD_START) == PORT_CMD_START));
  307. }
  308. /*
  309. * Enables the port DMA engine and FIS reception.
  310. *
  311. * return value
  312. * None
  313. */
  314. static inline void mtip_start_port(struct mtip_port *port)
  315. {
  316. /* Enable FIS reception */
  317. mtip_enable_fis(port, 1);
  318. /* Enable the DMA engine */
  319. mtip_enable_engine(port, 1);
  320. }
  321. /*
  322. * Deinitialize a port by disabling port interrupts, the DMA engine,
  323. * and FIS reception.
  324. *
  325. * @port Pointer to the port structure
  326. *
  327. * return value
  328. * None
  329. */
  330. static inline void mtip_deinit_port(struct mtip_port *port)
  331. {
  332. /* Disable interrupts on this port */
  333. writel(0, port->mmio + PORT_IRQ_MASK);
  334. /* Disable the DMA engine */
  335. mtip_enable_engine(port, 0);
  336. /* Disable FIS reception */
  337. mtip_enable_fis(port, 0);
  338. }
  339. /*
  340. * Initialize a port.
  341. *
  342. * This function deinitializes the port by calling mtip_deinit_port() and
  343. * then initializes it by setting the command header and RX FIS addresses,
  344. * clearing the SError register and any pending port interrupts before
  345. * re-enabling the default set of port interrupts.
  346. *
  347. * @port Pointer to the port structure.
  348. *
  349. * return value
  350. * None
  351. */
  352. static void mtip_init_port(struct mtip_port *port)
  353. {
  354. int i;
  355. mtip_deinit_port(port);
  356. /* Program the command list base and FIS base addresses */
  357. if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
  358. writel((port->command_list_dma >> 16) >> 16,
  359. port->mmio + PORT_LST_ADDR_HI);
  360. writel((port->rxfis_dma >> 16) >> 16,
  361. port->mmio + PORT_FIS_ADDR_HI);
  362. }
  363. writel(port->command_list_dma & 0xffffffff,
  364. port->mmio + PORT_LST_ADDR);
  365. writel(port->rxfis_dma & 0xffffffff, port->mmio + PORT_FIS_ADDR);
  366. /* Clear SError */
  367. writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
  368. /* reset the completed registers.*/
  369. for (i = 0; i < port->dd->slot_groups; i++)
  370. writel(0xFFFFFFFF, port->completed[i]);
  371. /* Clear any pending interrupts for this port */
  372. writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
  373. /* Enable port interrupts */
  374. writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
  375. }
  376. /*
  377. * Restart a port
  378. *
  379. * @port Pointer to the port data structure.
  380. *
  381. * return value
  382. * None
  383. */
  384. static void mtip_restart_port(struct mtip_port *port)
  385. {
  386. unsigned long timeout;
  387. /* Disable the DMA engine */
  388. mtip_enable_engine(port, 0);
  389. /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
  390. timeout = jiffies + msecs_to_jiffies(500);
  391. while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
  392. && time_before(jiffies, timeout))
  393. ;
  394. /*
  395. * Chip quirk: escalate to hba reset if
  396. * PxCMD.CR not clear after 500 ms
  397. */
  398. if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
  399. dev_warn(&port->dd->pdev->dev,
  400. "PxCMD.CR not clear, escalating reset\n");
  401. if (hba_reset_nosleep(port->dd))
  402. dev_err(&port->dd->pdev->dev,
  403. "HBA reset escalation failed.\n");
  404. /* 30 ms delay before com reset to quiesce chip */
  405. mdelay(30);
  406. }
  407. dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
  408. /* Set PxSCTL.DET */
  409. writel(readl(port->mmio + PORT_SCR_CTL) |
  410. 1, port->mmio + PORT_SCR_CTL);
  411. readl(port->mmio + PORT_SCR_CTL);
  412. /* Wait 1 ms to quiesce chip function */
  413. timeout = jiffies + msecs_to_jiffies(1);
  414. while (time_before(jiffies, timeout))
  415. ;
  416. /* Clear PxSCTL.DET */
  417. writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
  418. port->mmio + PORT_SCR_CTL);
  419. readl(port->mmio + PORT_SCR_CTL);
  420. /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
  421. timeout = jiffies + msecs_to_jiffies(500);
  422. while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
  423. && time_before(jiffies, timeout))
  424. ;
  425. if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
  426. dev_warn(&port->dd->pdev->dev,
  427. "COM reset failed\n");
  428. /* Clear SError, the PxSERR.DIAG.x should be set so clear it */
  429. writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
  430. /* Enable the DMA engine */
  431. mtip_enable_engine(port, 1);
  432. }
  433. /*
  434. * Called periodically to see if any read/write commands are
  435. * taking too long to complete.
  436. *
  437. * @data Pointer to the PORT data structure.
  438. *
  439. * return value
  440. * None
  441. */
  442. static void mtip_timeout_function(unsigned long int data)
  443. {
  444. struct mtip_port *port = (struct mtip_port *) data;
  445. struct host_to_dev_fis *fis;
  446. struct mtip_cmd *command;
  447. int tag, cmdto_cnt = 0;
  448. unsigned int bit, group;
  449. unsigned int num_command_slots = port->dd->slot_groups * 32;
  450. if (unlikely(!port))
  451. return;
  452. if (atomic_read(&port->dd->resumeflag) == true) {
  453. mod_timer(&port->cmd_timer,
  454. jiffies + msecs_to_jiffies(30000));
  455. return;
  456. }
  457. for (tag = 0; tag < num_command_slots; tag++) {
  458. /*
  459. * Skip internal command slot as it has
  460. * its own timeout mechanism
  461. */
  462. if (tag == MTIP_TAG_INTERNAL)
  463. continue;
  464. if (atomic_read(&port->commands[tag].active) &&
  465. (time_after(jiffies, port->commands[tag].comp_time))) {
  466. group = tag >> 5;
  467. bit = tag & 0x1f;
  468. command = &port->commands[tag];
  469. fis = (struct host_to_dev_fis *) command->command;
  470. dev_warn(&port->dd->pdev->dev,
  471. "Timeout for command tag %d\n", tag);
  472. cmdto_cnt++;
  473. if (cmdto_cnt == 1)
  474. atomic_inc(&port->dd->eh_active);
  475. /*
  476. * Clear the completed bit. This should prevent
  477. * any interrupt handlers from trying to retire
  478. * the command.
  479. */
  480. writel(1 << bit, port->completed[group]);
  481. /* Call the async completion callback. */
  482. if (likely(command->async_callback))
  483. command->async_callback(command->async_data,
  484. -EIO);
  485. command->async_callback = NULL;
  486. command->comp_func = NULL;
  487. /* Unmap the DMA scatter list entries */
  488. dma_unmap_sg(&port->dd->pdev->dev,
  489. command->sg,
  490. command->scatter_ents,
  491. command->direction);
  492. /*
  493. * Clear the allocated bit and active tag for the
  494. * command.
  495. */
  496. atomic_set(&port->commands[tag].active, 0);
  497. release_slot(port, tag);
  498. up(&port->cmd_slot);
  499. }
  500. }
  501. if (cmdto_cnt) {
  502. dev_warn(&port->dd->pdev->dev,
  503. "%d commands timed out: restarting port",
  504. cmdto_cnt);
  505. mtip_restart_port(port);
  506. atomic_dec(&port->dd->eh_active);
  507. }
  508. /* Restart the timer */
  509. mod_timer(&port->cmd_timer,
  510. jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
  511. }
  512. /*
  513. * IO completion function.
  514. *
  515. * This completion function is called by the driver ISR when a
  516. * command that was issued by the kernel completes. It first calls the
  517. * asynchronous completion function which normally calls back into the block
  518. * layer passing the asynchronous callback data, then unmaps the
  519. * scatter list associated with the completed command, and finally
  520. * clears the allocated bit associated with the completed command.
  521. *
  522. * @port Pointer to the port data structure.
  523. * @tag Tag of the command.
  524. * @data Pointer to driver_data.
  525. * @status Completion status.
  526. *
  527. * return value
  528. * None
  529. */
  530. static void mtip_async_complete(struct mtip_port *port,
  531. int tag,
  532. void *data,
  533. int status)
  534. {
  535. struct mtip_cmd *command;
  536. struct driver_data *dd = data;
  537. int cb_status = status ? -EIO : 0;
  538. if (unlikely(!dd) || unlikely(!port))
  539. return;
  540. command = &port->commands[tag];
  541. if (unlikely(status == PORT_IRQ_TF_ERR)) {
  542. dev_warn(&port->dd->pdev->dev,
  543. "Command tag %d failed due to TFE\n", tag);
  544. }
  545. /* Upper layer callback */
  546. if (likely(command->async_callback))
  547. command->async_callback(command->async_data, cb_status);
  548. command->async_callback = NULL;
  549. command->comp_func = NULL;
  550. /* Unmap the DMA scatter list entries */
  551. dma_unmap_sg(&dd->pdev->dev,
  552. command->sg,
  553. command->scatter_ents,
  554. command->direction);
  555. /* Clear the allocated and active bits for the command */
  556. atomic_set(&port->commands[tag].active, 0);
  557. release_slot(port, tag);
  558. up(&port->cmd_slot);
  559. }
  560. /*
  561. * Internal command completion callback function.
  562. *
  563. * This function is normally called by the driver ISR when an internal
  564. * command completed. This function signals the command completion by
  565. * calling complete().
  566. *
  567. * @port Pointer to the port data structure.
  568. * @tag Tag of the command that has completed.
  569. * @data Pointer to a completion structure.
  570. * @status Completion status.
  571. *
  572. * return value
  573. * None
  574. */
  575. static void mtip_completion(struct mtip_port *port,
  576. int tag,
  577. void *data,
  578. int status)
  579. {
  580. struct mtip_cmd *command = &port->commands[tag];
  581. struct completion *waiting = data;
  582. if (unlikely(status == PORT_IRQ_TF_ERR))
  583. dev_warn(&port->dd->pdev->dev,
  584. "Internal command %d completed with TFE\n", tag);
  585. command->async_callback = NULL;
  586. command->comp_func = NULL;
  587. complete(waiting);
  588. }
  589. /*
  590. * Helper function for tag logging
  591. */
  592. static void print_tags(struct driver_data *dd,
  593. char *msg,
  594. unsigned long *tagbits)
  595. {
  596. unsigned int tag, count = 0;
  597. for (tag = 0; tag < (dd->slot_groups) * 32; tag++) {
  598. if (test_bit(tag, tagbits))
  599. count++;
  600. }
  601. if (count)
  602. dev_info(&dd->pdev->dev, "%s [%i tags]\n", msg, count);
  603. }
  604. /*
  605. * Handle an error.
  606. *
  607. * @dd Pointer to the DRIVER_DATA structure.
  608. *
  609. * return value
  610. * None
  611. */
  612. static void mtip_handle_tfe(struct driver_data *dd)
  613. {
  614. int group, tag, bit, reissue;
  615. struct mtip_port *port;
  616. struct mtip_cmd *command;
  617. u32 completed;
  618. struct host_to_dev_fis *fis;
  619. unsigned long tagaccum[SLOTBITS_IN_LONGS];
  620. dev_warn(&dd->pdev->dev, "Taskfile error\n");
  621. port = dd->port;
  622. /* Stop the timer to prevent command timeouts. */
  623. del_timer(&port->cmd_timer);
  624. /* Set eh_active */
  625. atomic_inc(&dd->eh_active);
  626. /* Loop through all the groups */
  627. for (group = 0; group < dd->slot_groups; group++) {
  628. completed = readl(port->completed[group]);
  629. /* clear completed status register in the hardware.*/
  630. writel(completed, port->completed[group]);
  631. /* clear the tag accumulator */
  632. memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
  633. /* Process successfully completed commands */
  634. for (bit = 0; bit < 32 && completed; bit++) {
  635. if (!(completed & (1<<bit)))
  636. continue;
  637. tag = (group << 5) + bit;
  638. /* Skip the internal command slot */
  639. if (tag == MTIP_TAG_INTERNAL)
  640. continue;
  641. command = &port->commands[tag];
  642. if (likely(command->comp_func)) {
  643. set_bit(tag, tagaccum);
  644. atomic_set(&port->commands[tag].active, 0);
  645. command->comp_func(port,
  646. tag,
  647. command->comp_data,
  648. 0);
  649. } else {
  650. dev_err(&port->dd->pdev->dev,
  651. "Missing completion func for tag %d",
  652. tag);
  653. if (mtip_check_surprise_removal(dd->pdev)) {
  654. mtip_command_cleanup(dd);
  655. /* don't proceed further */
  656. return;
  657. }
  658. }
  659. }
  660. }
  661. print_tags(dd, "TFE tags completed:", tagaccum);
  662. /* Restart the port */
  663. mdelay(20);
  664. mtip_restart_port(port);
  665. /* clear the tag accumulator */
  666. memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
  667. /* Loop through all the groups */
  668. for (group = 0; group < dd->slot_groups; group++) {
  669. for (bit = 0; bit < 32; bit++) {
  670. reissue = 1;
  671. tag = (group << 5) + bit;
  672. /* If the active bit is set re-issue the command */
  673. if (atomic_read(&port->commands[tag].active) == 0)
  674. continue;
  675. fis = (struct host_to_dev_fis *)
  676. port->commands[tag].command;
  677. /* Should re-issue? */
  678. if (tag == MTIP_TAG_INTERNAL ||
  679. fis->command == ATA_CMD_SET_FEATURES)
  680. reissue = 0;
  681. /*
  682. * First check if this command has
  683. * exceeded its retries.
  684. */
  685. if (reissue &&
  686. (port->commands[tag].retries-- > 0)) {
  687. set_bit(tag, tagaccum);
  688. /* Update the timeout value. */
  689. port->commands[tag].comp_time =
  690. jiffies + msecs_to_jiffies(
  691. MTIP_NCQ_COMMAND_TIMEOUT_MS);
  692. /* Re-issue the command. */
  693. mtip_issue_ncq_command(port, tag);
  694. continue;
  695. }
  696. /* Retire a command that will not be reissued */
  697. dev_warn(&port->dd->pdev->dev,
  698. "retiring tag %d\n", tag);
  699. atomic_set(&port->commands[tag].active, 0);
  700. if (port->commands[tag].comp_func)
  701. port->commands[tag].comp_func(
  702. port,
  703. tag,
  704. port->commands[tag].comp_data,
  705. PORT_IRQ_TF_ERR);
  706. else
  707. dev_warn(&port->dd->pdev->dev,
  708. "Bad completion for tag %d\n",
  709. tag);
  710. }
  711. }
  712. print_tags(dd, "TFE tags reissued:", tagaccum);
  713. /* Decrement eh_active */
  714. atomic_dec(&dd->eh_active);
  715. mod_timer(&port->cmd_timer,
  716. jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
  717. }
  718. /*
  719. * Handle a set device bits interrupt
  720. */
  721. static inline void mtip_process_sdbf(struct driver_data *dd)
  722. {
  723. struct mtip_port *port = dd->port;
  724. int group, tag, bit;
  725. u32 completed;
  726. struct mtip_cmd *command;
  727. /* walk all bits in all slot groups */
  728. for (group = 0; group < dd->slot_groups; group++) {
  729. completed = readl(port->completed[group]);
  730. /* clear completed status register in the hardware.*/
  731. writel(completed, port->completed[group]);
  732. /* Process completed commands. */
  733. for (bit = 0;
  734. (bit < 32) && completed;
  735. bit++, completed >>= 1) {
  736. if (completed & 0x01) {
  737. tag = (group << 5) | bit;
  738. /* skip internal command slot. */
  739. if (unlikely(tag == MTIP_TAG_INTERNAL))
  740. continue;
  741. command = &port->commands[tag];
  742. /* make internal callback */
  743. if (likely(command->comp_func)) {
  744. command->comp_func(
  745. port,
  746. tag,
  747. command->comp_data,
  748. 0);
  749. } else {
  750. dev_warn(&dd->pdev->dev,
  751. "Null completion "
  752. "for tag %d",
  753. tag);
  754. if (mtip_check_surprise_removal(
  755. dd->pdev)) {
  756. mtip_command_cleanup(dd);
  757. return;
  758. }
  759. }
  760. }
  761. }
  762. }
  763. }
  764. /*
  765. * Process legacy pio and d2h interrupts
  766. */
  767. static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
  768. {
  769. struct mtip_port *port = dd->port;
  770. struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
  771. if (port->internal_cmd_in_progress &&
  772. cmd != NULL &&
  773. !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
  774. & (1 << MTIP_TAG_INTERNAL))) {
  775. if (cmd->comp_func) {
  776. cmd->comp_func(port,
  777. MTIP_TAG_INTERNAL,
  778. cmd->comp_data,
  779. 0);
  780. return;
  781. }
  782. }
  783. dev_warn(&dd->pdev->dev, "IRQ status 0x%x ignored.\n", port_stat);
  784. return;
  785. }
  786. /*
  787. * Demux and handle errors
  788. */
  789. static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
  790. {
  791. if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
  792. mtip_handle_tfe(dd);
  793. if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
  794. dev_warn(&dd->pdev->dev,
  795. "Clearing PxSERR.DIAG.x\n");
  796. writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
  797. }
  798. if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
  799. dev_warn(&dd->pdev->dev,
  800. "Clearing PxSERR.DIAG.n\n");
  801. writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
  802. }
  803. if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
  804. dev_warn(&dd->pdev->dev,
  805. "Port stat errors %x unhandled\n",
  806. (port_stat & ~PORT_IRQ_HANDLED));
  807. }
  808. }
  809. static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
  810. {
  811. struct driver_data *dd = (struct driver_data *) data;
  812. struct mtip_port *port = dd->port;
  813. u32 hba_stat, port_stat;
  814. int rv = IRQ_NONE;
  815. hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
  816. if (hba_stat) {
  817. rv = IRQ_HANDLED;
  818. /* Acknowledge the interrupt status on the port.*/
  819. port_stat = readl(port->mmio + PORT_IRQ_STAT);
  820. writel(port_stat, port->mmio + PORT_IRQ_STAT);
  821. /* Demux port status */
  822. if (likely(port_stat & PORT_IRQ_SDB_FIS))
  823. mtip_process_sdbf(dd);
  824. if (unlikely(port_stat & PORT_IRQ_ERR)) {
  825. if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
  826. mtip_command_cleanup(dd);
  827. /* don't proceed further */
  828. return IRQ_HANDLED;
  829. }
  830. mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
  831. }
  832. if (unlikely(port_stat & PORT_IRQ_LEGACY))
  833. mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
  834. }
  835. /* acknowledge interrupt */
  836. writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
  837. return rv;
  838. }
  839. /*
  840. * Wrapper for mtip_handle_irq
  841. * (ignores return code)
  842. */
  843. static void mtip_tasklet(unsigned long data)
  844. {
  845. mtip_handle_irq((struct driver_data *) data);
  846. }
  847. /*
  848. * HBA interrupt subroutine.
  849. *
  850. * @irq IRQ number.
  851. * @instance Pointer to the driver data structure.
  852. *
  853. * return value
  854. * IRQ_HANDLED A HBA interrupt was pending and handled.
  855. * IRQ_NONE This interrupt was not for the HBA.
  856. */
  857. static irqreturn_t mtip_irq_handler(int irq, void *instance)
  858. {
  859. struct driver_data *dd = instance;
  860. tasklet_schedule(&dd->tasklet);
  861. return IRQ_HANDLED;
  862. }
  863. static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
  864. {
  865. atomic_set(&port->commands[tag].active, 1);
  866. writel(1 << MTIP_TAG_BIT(tag),
  867. port->cmd_issue[MTIP_TAG_INDEX(tag)]);
  868. }
  869. /*
  870. * Wait for port to quiesce
  871. *
  872. * @port Pointer to port data structure
  873. * @timeout Max duration to wait (ms)
  874. *
  875. * return value
  876. * 0 Success
  877. * -EBUSY Commands still active
  878. */
  879. static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
  880. {
  881. unsigned long to;
  882. unsigned int n, active;
  883. to = jiffies + msecs_to_jiffies(timeout);
  884. do {
  885. /*
  886. * Ignore s_active bit 0 of array element 0.
  887. * This bit will always be set
  888. */
  889. active = readl(port->s_active[0]) & 0xfffffffe;
  890. for (n = 1; n < port->dd->slot_groups; n++)
  891. active |= readl(port->s_active[n]);
  892. if (!active)
  893. break;
  894. msleep(20);
  895. } while (time_before(jiffies, to));
  896. return active ? -EBUSY : 0;
  897. }
  898. /*
  899. * Execute an internal command and wait for the completion.
  900. *
  901. * @port Pointer to the port data structure.
  902. * @fis Pointer to the FIS that describes the command.
  903. * @fisLen Length in WORDS of the FIS.
  904. * @buffer DMA accessible for command data.
  905. * @bufLen Length, in bytes, of the data buffer.
  906. * @opts Command header options, excluding the FIS length
  907. * and the number of PRD entries.
  908. * @timeout Time in ms to wait for the command to complete.
  909. *
  910. * return value
  911. * 0 Command completed successfully.
  912. * -EFAULT The buffer address is not correctly aligned.
  913. * -EBUSY Internal command or other IO in progress.
  914. * -EAGAIN Time out waiting for command to complete.
  915. */
  916. static int mtip_exec_internal_command(struct mtip_port *port,
  917. void *fis,
  918. int fisLen,
  919. dma_addr_t buffer,
  920. int bufLen,
  921. u32 opts,
  922. gfp_t atomic,
  923. unsigned long timeout)
  924. {
  925. struct mtip_cmd_sg *command_sg;
  926. DECLARE_COMPLETION_ONSTACK(wait);
  927. int rv = 0;
  928. struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
  929. /* Make sure the buffer is 8 byte aligned. This is asic specific. */
  930. if (buffer & 0x00000007) {
  931. dev_err(&port->dd->pdev->dev,
  932. "SG buffer is not 8 byte aligned\n");
  933. return -EFAULT;
  934. }
  935. /* Only one internal command should be running at a time */
  936. if (test_and_set_bit(MTIP_TAG_INTERNAL, port->allocated)) {
  937. dev_warn(&port->dd->pdev->dev,
  938. "Internal command already active\n");
  939. return -EBUSY;
  940. }
  941. port->internal_cmd_in_progress = 1;
  942. if (atomic == GFP_KERNEL) {
  943. /* wait for io to complete if non atomic */
  944. if (mtip_quiesce_io(port, 5000) < 0) {
  945. dev_warn(&port->dd->pdev->dev,
  946. "Failed to quiesce IO\n");
  947. release_slot(port, MTIP_TAG_INTERNAL);
  948. port->internal_cmd_in_progress = 0;
  949. return -EBUSY;
  950. }
  951. /* Set the completion function and data for the command. */
  952. int_cmd->comp_data = &wait;
  953. int_cmd->comp_func = mtip_completion;
  954. } else {
  955. /* Clear completion - we're going to poll */
  956. int_cmd->comp_data = NULL;
  957. int_cmd->comp_func = NULL;
  958. }
  959. /* Copy the command to the command table */
  960. memcpy(int_cmd->command, fis, fisLen*4);
  961. /* Populate the SG list */
  962. int_cmd->command_header->opts =
  963. cpu_to_le32(opts | fisLen);
  964. if (bufLen) {
  965. command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
  966. command_sg->info = cpu_to_le32((bufLen-1) & 0x3fffff);
  967. command_sg->dba = cpu_to_le32(buffer & 0xffffffff);
  968. command_sg->dba_upper = cpu_to_le32((buffer >> 16) >> 16);
  969. int_cmd->command_header->opts |= cpu_to_le32((1 << 16));
  970. }
  971. /* Populate the command header */
  972. int_cmd->command_header->byte_count = 0;
  973. /* Issue the command to the hardware */
  974. mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
  975. /* Poll if atomic, wait_for_completion otherwise */
  976. if (atomic == GFP_KERNEL) {
  977. /* Wait for the command to complete or timeout. */
  978. if (wait_for_completion_timeout(
  979. &wait,
  980. msecs_to_jiffies(timeout)) == 0) {
  981. dev_err(&port->dd->pdev->dev,
  982. "Internal command did not complete [%d]\n",
  983. atomic);
  984. rv = -EAGAIN;
  985. }
  986. if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
  987. & (1 << MTIP_TAG_INTERNAL)) {
  988. dev_warn(&port->dd->pdev->dev,
  989. "Retiring internal command but CI is 1.\n");
  990. }
  991. } else {
  992. /* Spin for <timeout> checking if command still outstanding */
  993. timeout = jiffies + msecs_to_jiffies(timeout);
  994. while ((readl(
  995. port->cmd_issue[MTIP_TAG_INTERNAL])
  996. & (1 << MTIP_TAG_INTERNAL))
  997. && time_before(jiffies, timeout))
  998. ;
  999. if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
  1000. & (1 << MTIP_TAG_INTERNAL)) {
  1001. dev_err(&port->dd->pdev->dev,
  1002. "Internal command did not complete [%d]\n",
  1003. atomic);
  1004. rv = -EAGAIN;
  1005. }
  1006. }
  1007. /* Clear the allocated and active bits for the internal command. */
  1008. atomic_set(&int_cmd->active, 0);
  1009. release_slot(port, MTIP_TAG_INTERNAL);
  1010. port->internal_cmd_in_progress = 0;
  1011. return rv;
  1012. }
  1013. /*
  1014. * Byte-swap ATA ID strings.
  1015. *
  1016. * ATA identify data contains strings in byte-swapped 16-bit words.
  1017. * They must be swapped (on all architectures) to be usable as C strings.
  1018. * This function swaps bytes in-place.
  1019. *
  1020. * @buf The buffer location of the string
  1021. * @len The number of bytes to swap
  1022. *
  1023. * return value
  1024. * None
  1025. */
  1026. static inline void ata_swap_string(u16 *buf, unsigned int len)
  1027. {
  1028. int i;
  1029. for (i = 0; i < (len/2); i++)
  1030. be16_to_cpus(&buf[i]);
  1031. }
  1032. /*
  1033. * Request the device identity information.
  1034. *
  1035. * If a user space buffer is not specified, i.e. is NULL, the
  1036. * identify information is still read from the drive and placed
  1037. * into the identify data buffer (@e port->identify) in the
  1038. * port data structure.
  1039. * When the identify buffer contains valid identify information @e
  1040. * port->identify_valid is non-zero.
  1041. *
  1042. * @port Pointer to the port structure.
  1043. * @user_buffer A user space buffer where the identify data should be
  1044. * copied.
  1045. *
  1046. * return value
  1047. * 0 Command completed successfully.
  1048. * -EFAULT An error occurred while coping data to the user buffer.
  1049. * -1 Command failed.
  1050. */
  1051. static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
  1052. {
  1053. int rv = 0;
  1054. struct host_to_dev_fis fis;
  1055. down_write(&port->dd->internal_sem);
  1056. /* Build the FIS. */
  1057. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1058. fis.type = 0x27;
  1059. fis.opts = 1 << 7;
  1060. fis.command = ATA_CMD_ID_ATA;
  1061. /* Set the identify information as invalid. */
  1062. port->identify_valid = 0;
  1063. /* Clear the identify information. */
  1064. memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
  1065. /* Execute the command. */
  1066. if (mtip_exec_internal_command(port,
  1067. &fis,
  1068. 5,
  1069. port->identify_dma,
  1070. sizeof(u16) * ATA_ID_WORDS,
  1071. 0,
  1072. GFP_KERNEL,
  1073. MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
  1074. < 0) {
  1075. rv = -1;
  1076. goto out;
  1077. }
  1078. /*
  1079. * Perform any necessary byte-swapping. Yes, the kernel does in fact
  1080. * perform field-sensitive swapping on the string fields.
  1081. * See the kernel use of ata_id_string() for proof of this.
  1082. */
  1083. #ifdef __LITTLE_ENDIAN
  1084. ata_swap_string(port->identify + 27, 40); /* model string*/
  1085. ata_swap_string(port->identify + 23, 8); /* firmware string*/
  1086. ata_swap_string(port->identify + 10, 20); /* serial# string*/
  1087. #else
  1088. {
  1089. int i;
  1090. for (i = 0; i < ATA_ID_WORDS; i++)
  1091. port->identify[i] = le16_to_cpu(port->identify[i]);
  1092. }
  1093. #endif
  1094. /* Set the identify buffer as valid. */
  1095. port->identify_valid = 1;
  1096. if (user_buffer) {
  1097. if (copy_to_user(
  1098. user_buffer,
  1099. port->identify,
  1100. ATA_ID_WORDS * sizeof(u16))) {
  1101. rv = -EFAULT;
  1102. goto out;
  1103. }
  1104. }
  1105. out:
  1106. up_write(&port->dd->internal_sem);
  1107. return rv;
  1108. }
  1109. /*
  1110. * Issue a standby immediate command to the device.
  1111. *
  1112. * @port Pointer to the port structure.
  1113. *
  1114. * return value
  1115. * 0 Command was executed successfully.
  1116. * -1 An error occurred while executing the command.
  1117. */
  1118. static int mtip_standby_immediate(struct mtip_port *port)
  1119. {
  1120. int rv;
  1121. struct host_to_dev_fis fis;
  1122. down_write(&port->dd->internal_sem);
  1123. /* Build the FIS. */
  1124. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1125. fis.type = 0x27;
  1126. fis.opts = 1 << 7;
  1127. fis.command = ATA_CMD_STANDBYNOW1;
  1128. /* Execute the command. Use a 15-second timeout for large drives. */
  1129. rv = mtip_exec_internal_command(port,
  1130. &fis,
  1131. 5,
  1132. 0,
  1133. 0,
  1134. 0,
  1135. GFP_KERNEL,
  1136. 15000);
  1137. up_write(&port->dd->internal_sem);
  1138. return rv;
  1139. }
  1140. /*
  1141. * Get the drive capacity.
  1142. *
  1143. * @dd Pointer to the device data structure.
  1144. * @sectors Pointer to the variable that will receive the sector count.
  1145. *
  1146. * return value
  1147. * 1 Capacity was returned successfully.
  1148. * 0 The identify information is invalid.
  1149. */
  1150. static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
  1151. {
  1152. struct mtip_port *port = dd->port;
  1153. u64 total, raw0, raw1, raw2, raw3;
  1154. raw0 = port->identify[100];
  1155. raw1 = port->identify[101];
  1156. raw2 = port->identify[102];
  1157. raw3 = port->identify[103];
  1158. total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
  1159. *sectors = total;
  1160. return (bool) !!port->identify_valid;
  1161. }
  1162. /*
  1163. * Reset the HBA.
  1164. *
  1165. * Resets the HBA by setting the HBA Reset bit in the Global
  1166. * HBA Control register. After setting the HBA Reset bit the
  1167. * function waits for 1 second before reading the HBA Reset
  1168. * bit to make sure it has cleared. If HBA Reset is not clear
  1169. * an error is returned. Cannot be used in non-blockable
  1170. * context.
  1171. *
  1172. * @dd Pointer to the driver data structure.
  1173. *
  1174. * return value
  1175. * 0 The reset was successful.
  1176. * -1 The HBA Reset bit did not clear.
  1177. */
  1178. static int mtip_hba_reset(struct driver_data *dd)
  1179. {
  1180. mtip_deinit_port(dd->port);
  1181. /* Set the reset bit */
  1182. writel(HOST_RESET, dd->mmio + HOST_CTL);
  1183. /* Flush */
  1184. readl(dd->mmio + HOST_CTL);
  1185. /* Wait for reset to clear */
  1186. ssleep(1);
  1187. /* Check the bit has cleared */
  1188. if (readl(dd->mmio + HOST_CTL) & HOST_RESET) {
  1189. dev_err(&dd->pdev->dev,
  1190. "Reset bit did not clear.\n");
  1191. return -1;
  1192. }
  1193. return 0;
  1194. }
  1195. /*
  1196. * Display the identify command data.
  1197. *
  1198. * @port Pointer to the port data structure.
  1199. *
  1200. * return value
  1201. * None
  1202. */
  1203. static void mtip_dump_identify(struct mtip_port *port)
  1204. {
  1205. sector_t sectors;
  1206. unsigned short revid;
  1207. char cbuf[42];
  1208. if (!port->identify_valid)
  1209. return;
  1210. strlcpy(cbuf, (char *)(port->identify+10), 21);
  1211. dev_info(&port->dd->pdev->dev,
  1212. "Serial No.: %s\n", cbuf);
  1213. strlcpy(cbuf, (char *)(port->identify+23), 9);
  1214. dev_info(&port->dd->pdev->dev,
  1215. "Firmware Ver.: %s\n", cbuf);
  1216. strlcpy(cbuf, (char *)(port->identify+27), 41);
  1217. dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
  1218. if (mtip_hw_get_capacity(port->dd, &sectors))
  1219. dev_info(&port->dd->pdev->dev,
  1220. "Capacity: %llu sectors (%llu MB)\n",
  1221. (u64)sectors,
  1222. ((u64)sectors) * ATA_SECT_SIZE >> 20);
  1223. pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
  1224. switch (revid & 0xff) {
  1225. case 0x1:
  1226. strlcpy(cbuf, "A0", 3);
  1227. break;
  1228. case 0x3:
  1229. strlcpy(cbuf, "A2", 3);
  1230. break;
  1231. default:
  1232. strlcpy(cbuf, "?", 2);
  1233. break;
  1234. }
  1235. dev_info(&port->dd->pdev->dev,
  1236. "Card Type: %s\n", cbuf);
  1237. }
  1238. /*
  1239. * Map the commands scatter list into the command table.
  1240. *
  1241. * @command Pointer to the command.
  1242. * @nents Number of scatter list entries.
  1243. *
  1244. * return value
  1245. * None
  1246. */
  1247. static inline void fill_command_sg(struct driver_data *dd,
  1248. struct mtip_cmd *command,
  1249. int nents)
  1250. {
  1251. int n;
  1252. unsigned int dma_len;
  1253. struct mtip_cmd_sg *command_sg;
  1254. struct scatterlist *sg = command->sg;
  1255. command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
  1256. for (n = 0; n < nents; n++) {
  1257. dma_len = sg_dma_len(sg);
  1258. if (dma_len > 0x400000)
  1259. dev_err(&dd->pdev->dev,
  1260. "DMA segment length truncated\n");
  1261. command_sg->info = cpu_to_le32((dma_len-1) & 0x3fffff);
  1262. #if (BITS_PER_LONG == 64)
  1263. *((unsigned long *) &command_sg->dba) =
  1264. cpu_to_le64(sg_dma_address(sg));
  1265. #else
  1266. command_sg->dba = cpu_to_le32(sg_dma_address(sg));
  1267. command_sg->dba_upper =
  1268. cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
  1269. #endif
  1270. command_sg++;
  1271. sg++;
  1272. }
  1273. }
  1274. /*
  1275. * @brief Execute a drive command.
  1276. *
  1277. * return value 0 The command completed successfully.
  1278. * return value -1 An error occurred while executing the command.
  1279. */
  1280. static int exec_drive_task(struct mtip_port *port, u8 *command)
  1281. {
  1282. struct host_to_dev_fis fis;
  1283. struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
  1284. /* Lock the internal command semaphore. */
  1285. down_write(&port->dd->internal_sem);
  1286. /* Build the FIS. */
  1287. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1288. fis.type = 0x27;
  1289. fis.opts = 1 << 7;
  1290. fis.command = command[0];
  1291. fis.features = command[1];
  1292. fis.sect_count = command[2];
  1293. fis.sector = command[3];
  1294. fis.cyl_low = command[4];
  1295. fis.cyl_hi = command[5];
  1296. fis.device = command[6] & ~0x10; /* Clear the dev bit*/
  1297. dbg_printk(MTIP_DRV_NAME "%s: User Command: cmd %x, feat %x, "
  1298. "nsect %x, sect %x, lcyl %x, "
  1299. "hcyl %x, sel %x\n",
  1300. __func__,
  1301. command[0],
  1302. command[1],
  1303. command[2],
  1304. command[3],
  1305. command[4],
  1306. command[5],
  1307. command[6]);
  1308. /* Execute the command. */
  1309. if (mtip_exec_internal_command(port,
  1310. &fis,
  1311. 5,
  1312. 0,
  1313. 0,
  1314. 0,
  1315. GFP_KERNEL,
  1316. MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
  1317. up_write(&port->dd->internal_sem);
  1318. return -1;
  1319. }
  1320. command[0] = reply->command; /* Status*/
  1321. command[1] = reply->features; /* Error*/
  1322. command[4] = reply->cyl_low;
  1323. command[5] = reply->cyl_hi;
  1324. dbg_printk(MTIP_DRV_NAME "%s: Completion Status: stat %x, "
  1325. "err %x , cyl_lo %x cyl_hi %x\n",
  1326. __func__,
  1327. command[0],
  1328. command[1],
  1329. command[4],
  1330. command[5]);
  1331. up_write(&port->dd->internal_sem);
  1332. return 0;
  1333. }
  1334. /*
  1335. * @brief Execute a drive command.
  1336. *
  1337. * @param port Pointer to the port data structure.
  1338. * @param command Pointer to the user specified command parameters.
  1339. * @param user_buffer Pointer to the user space buffer where read sector
  1340. * data should be copied.
  1341. *
  1342. * return value 0 The command completed successfully.
  1343. * return value -EFAULT An error occurred while copying the completion
  1344. * data to the user space buffer.
  1345. * return value -1 An error occurred while executing the command.
  1346. */
  1347. static int exec_drive_command(struct mtip_port *port, u8 *command,
  1348. void __user *user_buffer)
  1349. {
  1350. struct host_to_dev_fis fis;
  1351. struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
  1352. /* Lock the internal command semaphore. */
  1353. down_write(&port->dd->internal_sem);
  1354. /* Build the FIS. */
  1355. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1356. fis.type = 0x27;
  1357. fis.opts = 1 << 7;
  1358. fis.command = command[0];
  1359. fis.features = command[2];
  1360. fis.sect_count = command[3];
  1361. if (fis.command == ATA_CMD_SMART) {
  1362. fis.sector = command[1];
  1363. fis.cyl_low = 0x4f;
  1364. fis.cyl_hi = 0xc2;
  1365. }
  1366. dbg_printk(MTIP_DRV_NAME
  1367. "%s: User Command: cmd %x, sect %x, "
  1368. "feat %x, sectcnt %x\n",
  1369. __func__,
  1370. command[0],
  1371. command[1],
  1372. command[2],
  1373. command[3]);
  1374. memset(port->sector_buffer, 0x00, ATA_SECT_SIZE);
  1375. /* Execute the command. */
  1376. if (mtip_exec_internal_command(port,
  1377. &fis,
  1378. 5,
  1379. port->sector_buffer_dma,
  1380. (command[3] != 0) ? ATA_SECT_SIZE : 0,
  1381. 0,
  1382. GFP_KERNEL,
  1383. MTIP_IOCTL_COMMAND_TIMEOUT_MS)
  1384. < 0) {
  1385. up_write(&port->dd->internal_sem);
  1386. return -1;
  1387. }
  1388. /* Collect the completion status. */
  1389. command[0] = reply->command; /* Status*/
  1390. command[1] = reply->features; /* Error*/
  1391. command[2] = command[3];
  1392. dbg_printk(MTIP_DRV_NAME
  1393. "%s: Completion Status: stat %x, "
  1394. "err %x, cmd %x\n",
  1395. __func__,
  1396. command[0],
  1397. command[1],
  1398. command[2]);
  1399. if (user_buffer && command[3]) {
  1400. if (copy_to_user(user_buffer,
  1401. port->sector_buffer,
  1402. ATA_SECT_SIZE * command[3])) {
  1403. up_write(&port->dd->internal_sem);
  1404. return -EFAULT;
  1405. }
  1406. }
  1407. up_write(&port->dd->internal_sem);
  1408. return 0;
  1409. }
  1410. /*
  1411. * Indicates whether a command has a single sector payload.
  1412. *
  1413. * @command passed to the device to perform the certain event.
  1414. * @features passed to the device to perform the certain event.
  1415. *
  1416. * return value
  1417. * 1 command is one that always has a single sector payload,
  1418. * regardless of the value in the Sector Count field.
  1419. * 0 otherwise
  1420. *
  1421. */
  1422. static unsigned int implicit_sector(unsigned char command,
  1423. unsigned char features)
  1424. {
  1425. unsigned int rv = 0;
  1426. /* list of commands that have an implicit sector count of 1 */
  1427. switch (command) {
  1428. case 0xF1:
  1429. case 0xF2:
  1430. case 0xF3:
  1431. case 0xF4:
  1432. case 0xF5:
  1433. case 0xF6:
  1434. case 0xE4:
  1435. case 0xE8:
  1436. rv = 1;
  1437. break;
  1438. case 0xF9:
  1439. if (features == 0x03)
  1440. rv = 1;
  1441. break;
  1442. case 0xB0:
  1443. if ((features == 0xD0) || (features == 0xD1))
  1444. rv = 1;
  1445. break;
  1446. case 0xB1:
  1447. if ((features == 0xC2) || (features == 0xC3))
  1448. rv = 1;
  1449. break;
  1450. }
  1451. return rv;
  1452. }
  1453. /*
  1454. * Executes a taskfile
  1455. * See ide_taskfile_ioctl() for derivation
  1456. */
  1457. static int exec_drive_taskfile(struct driver_data *dd,
  1458. void __user *buf,
  1459. ide_task_request_t *req_task,
  1460. int outtotal)
  1461. {
  1462. struct host_to_dev_fis fis;
  1463. struct host_to_dev_fis *reply;
  1464. u8 *outbuf = NULL;
  1465. u8 *inbuf = NULL;
  1466. dma_addr_t outbuf_dma = 0;
  1467. dma_addr_t inbuf_dma = 0;
  1468. dma_addr_t dma_buffer = 0;
  1469. int err = 0;
  1470. unsigned int taskin = 0;
  1471. unsigned int taskout = 0;
  1472. u8 nsect = 0;
  1473. unsigned int timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
  1474. unsigned int force_single_sector;
  1475. unsigned int transfer_size;
  1476. unsigned long task_file_data;
  1477. int intotal = outtotal + req_task->out_size;
  1478. taskout = req_task->out_size;
  1479. taskin = req_task->in_size;
  1480. /* 130560 = 512 * 0xFF*/
  1481. if (taskin > 130560 || taskout > 130560) {
  1482. err = -EINVAL;
  1483. goto abort;
  1484. }
  1485. if (taskout) {
  1486. outbuf = kzalloc(taskout, GFP_KERNEL);
  1487. if (outbuf == NULL) {
  1488. err = -ENOMEM;
  1489. goto abort;
  1490. }
  1491. if (copy_from_user(outbuf, buf + outtotal, taskout)) {
  1492. err = -EFAULT;
  1493. goto abort;
  1494. }
  1495. outbuf_dma = pci_map_single(dd->pdev,
  1496. outbuf,
  1497. taskout,
  1498. DMA_TO_DEVICE);
  1499. if (outbuf_dma == 0) {
  1500. err = -ENOMEM;
  1501. goto abort;
  1502. }
  1503. dma_buffer = outbuf_dma;
  1504. }
  1505. if (taskin) {
  1506. inbuf = kzalloc(taskin, GFP_KERNEL);
  1507. if (inbuf == NULL) {
  1508. err = -ENOMEM;
  1509. goto abort;
  1510. }
  1511. if (copy_from_user(inbuf, buf + intotal, taskin)) {
  1512. err = -EFAULT;
  1513. goto abort;
  1514. }
  1515. inbuf_dma = pci_map_single(dd->pdev,
  1516. inbuf,
  1517. taskin, DMA_FROM_DEVICE);
  1518. if (inbuf_dma == 0) {
  1519. err = -ENOMEM;
  1520. goto abort;
  1521. }
  1522. dma_buffer = inbuf_dma;
  1523. }
  1524. /* only supports PIO and non-data commands from this ioctl. */
  1525. switch (req_task->data_phase) {
  1526. case TASKFILE_OUT:
  1527. nsect = taskout / ATA_SECT_SIZE;
  1528. reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
  1529. break;
  1530. case TASKFILE_IN:
  1531. reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
  1532. break;
  1533. case TASKFILE_NO_DATA:
  1534. reply = (dd->port->rxfis + RX_FIS_D2H_REG);
  1535. break;
  1536. default:
  1537. err = -EINVAL;
  1538. goto abort;
  1539. }
  1540. /* Lock the internal command semaphore. */
  1541. down_write(&dd->internal_sem);
  1542. /* Build the FIS. */
  1543. memset(&fis, 0, sizeof(struct host_to_dev_fis));
  1544. fis.type = 0x27;
  1545. fis.opts = 1 << 7;
  1546. fis.command = req_task->io_ports[7];
  1547. fis.features = req_task->io_ports[1];
  1548. fis.sect_count = req_task->io_ports[2];
  1549. fis.lba_low = req_task->io_ports[3];
  1550. fis.lba_mid = req_task->io_ports[4];
  1551. fis.lba_hi = req_task->io_ports[5];
  1552. /* Clear the dev bit*/
  1553. fis.device = req_task->io_ports[6] & ~0x10;
  1554. if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
  1555. req_task->in_flags.all =
  1556. IDE_TASKFILE_STD_IN_FLAGS |
  1557. (IDE_HOB_STD_IN_FLAGS << 8);
  1558. fis.lba_low_ex = req_task->hob_ports[3];
  1559. fis.lba_mid_ex = req_task->hob_ports[4];
  1560. fis.lba_hi_ex = req_task->hob_ports[5];
  1561. fis.features_ex = req_task->hob_ports[1];
  1562. fis.sect_cnt_ex = req_task->hob_ports[2];
  1563. } else {
  1564. req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
  1565. }
  1566. force_single_sector = implicit_sector(fis.command, fis.features);
  1567. if ((taskin || taskout) && (!fis.sect_count)) {
  1568. if (nsect)
  1569. fis.sect_count = nsect;
  1570. else {
  1571. if (!force_single_sector) {
  1572. dev_warn(&dd->pdev->dev,
  1573. "data movement but "
  1574. "sect_count is 0\n");
  1575. up_write(&dd->internal_sem);
  1576. err = -EINVAL;
  1577. goto abort;
  1578. }
  1579. }
  1580. }
  1581. dbg_printk(MTIP_DRV_NAME
  1582. "taskfile: cmd %x, feat %x, nsect %x,"
  1583. " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
  1584. " head/dev %x\n",
  1585. fis.command,
  1586. fis.features,
  1587. fis.sect_count,
  1588. fis.lba_low,
  1589. fis.lba_mid,
  1590. fis.lba_hi,
  1591. fis.device);
  1592. switch (fis.command) {
  1593. case 0x92: /* Change timeout for Download Microcode to 60 seconds.*/
  1594. timeout = 60000;
  1595. break;
  1596. case 0xf4: /* Change timeout for Security Erase Unit to 4 minutes.*/
  1597. timeout = 240000;
  1598. break;
  1599. case 0xe0: /* Change timeout for standby immediate to 10 seconds.*/
  1600. timeout = 10000;
  1601. break;
  1602. case 0xf7: /* Change timeout for vendor unique command to 10 secs */
  1603. timeout = 10000;
  1604. break;
  1605. case 0xfa: /* Change timeout for vendor unique command to 10 secs */
  1606. timeout = 10000;
  1607. break;
  1608. default:
  1609. timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
  1610. break;
  1611. }
  1612. /* Determine the correct transfer size.*/
  1613. if (force_single_sector)
  1614. transfer_size = ATA_SECT_SIZE;
  1615. else
  1616. transfer_size = ATA_SECT_SIZE * fis.sect_count;
  1617. /* Execute the command.*/
  1618. if (mtip_exec_internal_command(dd->port,
  1619. &fis,
  1620. 5,
  1621. dma_buffer,
  1622. transfer_size,
  1623. 0,
  1624. GFP_KERNEL,
  1625. timeout) < 0) {
  1626. up_write(&dd->internal_sem);
  1627. err = -EIO;
  1628. goto abort;
  1629. }
  1630. task_file_data = readl(dd->port->mmio+PORT_TFDATA);
  1631. if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
  1632. reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
  1633. req_task->io_ports[7] = reply->control;
  1634. } else {
  1635. reply = dd->port->rxfis + RX_FIS_D2H_REG;
  1636. req_task->io_ports[7] = reply->command;
  1637. }
  1638. /* reclaim the DMA buffers.*/
  1639. if (inbuf_dma)
  1640. pci_unmap_single(dd->pdev, inbuf_dma,
  1641. taskin, DMA_FROM_DEVICE);
  1642. if (outbuf_dma)
  1643. pci_unmap_single(dd->pdev, outbuf_dma,
  1644. taskout, DMA_TO_DEVICE);
  1645. inbuf_dma = 0;
  1646. outbuf_dma = 0;
  1647. /* return the ATA registers to the caller.*/
  1648. req_task->io_ports[1] = reply->features;
  1649. req_task->io_ports[2] = reply->sect_count;
  1650. req_task->io_ports[3] = reply->lba_low;
  1651. req_task->io_ports[4] = reply->lba_mid;
  1652. req_task->io_ports[5] = reply->lba_hi;
  1653. req_task->io_ports[6] = reply->device;
  1654. if (req_task->out_flags.all & 1) {
  1655. req_task->hob_ports[3] = reply->lba_low_ex;
  1656. req_task->hob_ports[4] = reply->lba_mid_ex;
  1657. req_task->hob_ports[5] = reply->lba_hi_ex;
  1658. req_task->hob_ports[1] = reply->features_ex;
  1659. req_task->hob_ports[2] = reply->sect_cnt_ex;
  1660. }
  1661. /* Com rest after secure erase or lowlevel format */
  1662. if (((fis.command == 0xF4) ||
  1663. ((fis.command == 0xFC) &&
  1664. (fis.features == 0x27 || fis.features == 0x72 ||
  1665. fis.features == 0x62 || fis.features == 0x26))) &&
  1666. !(reply->command & 1)) {
  1667. mtip_restart_port(dd->port);
  1668. }
  1669. dbg_printk(MTIP_DRV_NAME
  1670. "%s: Completion: stat %x,"
  1671. "err %x, sect_cnt %x, lbalo %x,"
  1672. "lbamid %x, lbahi %x, dev %x\n",
  1673. __func__,
  1674. req_task->io_ports[7],
  1675. req_task->io_ports[1],
  1676. req_task->io_ports[2],
  1677. req_task->io_ports[3],
  1678. req_task->io_ports[4],
  1679. req_task->io_ports[5],
  1680. req_task->io_ports[6]);
  1681. up_write(&dd->internal_sem);
  1682. if (taskout) {
  1683. if (copy_to_user(buf + outtotal, outbuf, taskout)) {
  1684. err = -EFAULT;
  1685. goto abort;
  1686. }
  1687. }
  1688. if (taskin) {
  1689. if (copy_to_user(buf + intotal, inbuf, taskin)) {
  1690. err = -EFAULT;
  1691. goto abort;
  1692. }
  1693. }
  1694. abort:
  1695. if (inbuf_dma)
  1696. pci_unmap_single(dd->pdev, inbuf_dma,
  1697. taskin, DMA_FROM_DEVICE);
  1698. if (outbuf_dma)
  1699. pci_unmap_single(dd->pdev, outbuf_dma,
  1700. taskout, DMA_TO_DEVICE);
  1701. kfree(outbuf);
  1702. kfree(inbuf);
  1703. return err;
  1704. }
  1705. /*
  1706. * Handle IOCTL calls from the Block Layer.
  1707. *
  1708. * This function is called by the Block Layer when it receives an IOCTL
  1709. * command that it does not understand. If the IOCTL command is not supported
  1710. * this function returns -ENOTTY.
  1711. *
  1712. * @dd Pointer to the driver data structure.
  1713. * @cmd IOCTL command passed from the Block Layer.
  1714. * @arg IOCTL argument passed from the Block Layer.
  1715. *
  1716. * return value
  1717. * 0 The IOCTL completed successfully.
  1718. * -ENOTTY The specified command is not supported.
  1719. * -EFAULT An error occurred copying data to a user space buffer.
  1720. * -EIO An error occurred while executing the command.
  1721. */
  1722. static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
  1723. unsigned long arg)
  1724. {
  1725. switch (cmd) {
  1726. case HDIO_GET_IDENTITY:
  1727. if (mtip_get_identify(dd->port, (void __user *) arg) < 0) {
  1728. dev_warn(&dd->pdev->dev,
  1729. "Unable to read identity\n");
  1730. return -EIO;
  1731. }
  1732. break;
  1733. case HDIO_DRIVE_CMD:
  1734. {
  1735. u8 drive_command[4];
  1736. /* Copy the user command info to our buffer. */
  1737. if (copy_from_user(drive_command,
  1738. (void __user *) arg,
  1739. sizeof(drive_command)))
  1740. return -EFAULT;
  1741. /* Execute the drive command. */
  1742. if (exec_drive_command(dd->port,
  1743. drive_command,
  1744. (void __user *) (arg+4)))
  1745. return -EIO;
  1746. /* Copy the status back to the users buffer. */
  1747. if (copy_to_user((void __user *) arg,
  1748. drive_command,
  1749. sizeof(drive_command)))
  1750. return -EFAULT;
  1751. break;
  1752. }
  1753. case HDIO_DRIVE_TASK:
  1754. {
  1755. u8 drive_command[7];
  1756. /* Copy the user command info to our buffer. */
  1757. if (copy_from_user(drive_command,
  1758. (void __user *) arg,
  1759. sizeof(drive_command)))
  1760. return -EFAULT;
  1761. /* Execute the drive command. */
  1762. if (exec_drive_task(dd->port, drive_command))
  1763. return -EIO;
  1764. /* Copy the status back to the users buffer. */
  1765. if (copy_to_user((void __user *) arg,
  1766. drive_command,
  1767. sizeof(drive_command)))
  1768. return -EFAULT;
  1769. break;
  1770. }
  1771. case HDIO_DRIVE_TASKFILE: {
  1772. ide_task_request_t req_task;
  1773. int ret, outtotal;
  1774. if (copy_from_user(&req_task, (void __user *) arg,
  1775. sizeof(req_task)))
  1776. return -EFAULT;
  1777. outtotal = sizeof(req_task);
  1778. ret = exec_drive_taskfile(dd, (void __user *) arg,
  1779. &req_task, outtotal);
  1780. if (copy_to_user((void __user *) arg, &req_task, sizeof(req_task)))
  1781. return -EFAULT;
  1782. return ret;
  1783. }
  1784. default:
  1785. return -EINVAL;
  1786. }
  1787. return 0;
  1788. }
  1789. /*
  1790. * Submit an IO to the hw
  1791. *
  1792. * This function is called by the block layer to issue an io
  1793. * to the device. Upon completion, the callback function will
  1794. * be called with the data parameter passed as the callback data.
  1795. *
  1796. * @dd Pointer to the driver data structure.
  1797. * @start First sector to read.
  1798. * @nsect Number of sectors to read.
  1799. * @nents Number of entries in scatter list for the read command.
  1800. * @tag The tag of this read command.
  1801. * @callback Pointer to the function that should be called
  1802. * when the read completes.
  1803. * @data Callback data passed to the callback function
  1804. * when the read completes.
  1805. * @barrier If non-zero, this command must be completed before
  1806. * issuing any other commands.
  1807. * @dir Direction (read or write)
  1808. *
  1809. * return value
  1810. * None
  1811. */
  1812. static void mtip_hw_submit_io(struct driver_data *dd, sector_t start,
  1813. int nsect, int nents, int tag, void *callback,
  1814. void *data, int barrier, int dir)
  1815. {
  1816. struct host_to_dev_fis *fis;
  1817. struct mtip_port *port = dd->port;
  1818. struct mtip_cmd *command = &port->commands[tag];
  1819. /* Map the scatter list for DMA access */
  1820. if (dir == READ)
  1821. nents = dma_map_sg(&dd->pdev->dev, command->sg,
  1822. nents, DMA_FROM_DEVICE);
  1823. else
  1824. nents = dma_map_sg(&dd->pdev->dev, command->sg,
  1825. nents, DMA_TO_DEVICE);
  1826. command->scatter_ents = nents;
  1827. /*
  1828. * The number of retries for this command before it is
  1829. * reported as a failure to the upper layers.
  1830. */
  1831. command->retries = MTIP_MAX_RETRIES;
  1832. /* Fill out fis */
  1833. fis = command->command;
  1834. fis->type = 0x27;
  1835. fis->opts = 1 << 7;
  1836. fis->command =
  1837. (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
  1838. *((unsigned int *) &fis->lba_low) = (start & 0xffffff);
  1839. *((unsigned int *) &fis->lba_low_ex) = ((start >> 24) & 0xffffff);
  1840. fis->device = 1 << 6;
  1841. if (barrier)
  1842. fis->device |= FUA_BIT;
  1843. fis->features = nsect & 0xff;
  1844. fis->features_ex = (nsect >> 8) & 0xff;
  1845. fis->sect_count = ((tag << 3) | (tag >> 5));
  1846. fis->sect_cnt_ex = 0;
  1847. fis->control = 0;
  1848. fis->res2 = 0;
  1849. fis->res3 = 0;
  1850. fill_command_sg(dd, command, nents);
  1851. /* Populate the command header */
  1852. command->command_header->opts = cpu_to_le32(
  1853. (nents << 16) | 5 | AHCI_CMD_PREFETCH);
  1854. command->command_header->byte_count = 0;
  1855. /*
  1856. * Set the completion function and data for the command
  1857. * within this layer.
  1858. */
  1859. command->comp_data = dd;
  1860. command->comp_func = mtip_async_complete;
  1861. command->direction = (dir == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  1862. /*
  1863. * Set the completion function and data for the command passed
  1864. * from the upper layer.
  1865. */
  1866. command->async_data = data;
  1867. command->async_callback = callback;
  1868. /*
  1869. * Lock used to prevent this command from being issued
  1870. * if an internal command is in progress.
  1871. */
  1872. down_read(&port->dd->internal_sem);
  1873. /* Issue the command to the hardware */
  1874. mtip_issue_ncq_command(port, tag);
  1875. /* Set the command's timeout value.*/
  1876. port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
  1877. MTIP_NCQ_COMMAND_TIMEOUT_MS);
  1878. up_read(&port->dd->internal_sem);
  1879. }
  1880. /*
  1881. * Release a command slot.
  1882. *
  1883. * @dd Pointer to the driver data structure.
  1884. * @tag Slot tag
  1885. *
  1886. * return value
  1887. * None
  1888. */
  1889. static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
  1890. {
  1891. release_slot(dd->port, tag);
  1892. }
  1893. /*
  1894. * Obtain a command slot and return its associated scatter list.
  1895. *
  1896. * @dd Pointer to the driver data structure.
  1897. * @tag Pointer to an int that will receive the allocated command
  1898. * slot tag.
  1899. *
  1900. * return value
  1901. * Pointer to the scatter list for the allocated command slot
  1902. * or NULL if no command slots are available.
  1903. */
  1904. static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
  1905. int *tag)
  1906. {
  1907. /*
  1908. * It is possible that, even with this semaphore, a thread
  1909. * may think that no command slots are available. Therefore, we
  1910. * need to make an attempt to get_slot().
  1911. */
  1912. down(&dd->port->cmd_slot);
  1913. *tag = get_slot(dd->port);
  1914. if (unlikely(*tag < 0))
  1915. return NULL;
  1916. return dd->port->commands[*tag].sg;
  1917. }
  1918. /*
  1919. * Sysfs register/status dump.
  1920. *
  1921. * @dev Pointer to the device structure, passed by the kernrel.
  1922. * @attr Pointer to the device_attribute structure passed by the kernel.
  1923. * @buf Pointer to the char buffer that will receive the stats info.
  1924. *
  1925. * return value
  1926. * The size, in bytes, of the data copied into buf.
  1927. */
  1928. static ssize_t hw_show_registers(struct device *dev,
  1929. struct device_attribute *attr,
  1930. char *buf)
  1931. {
  1932. u32 group_allocated;
  1933. struct driver_data *dd = dev_to_disk(dev)->private_data;
  1934. int size = 0;
  1935. int n;
  1936. size += sprintf(&buf[size], "%s:\ns_active:\n", __func__);
  1937. for (n = 0; n < dd->slot_groups; n++)
  1938. size += sprintf(&buf[size], "0x%08x\n",
  1939. readl(dd->port->s_active[n]));
  1940. size += sprintf(&buf[size], "Command Issue:\n");
  1941. for (n = 0; n < dd->slot_groups; n++)
  1942. size += sprintf(&buf[size], "0x%08x\n",
  1943. readl(dd->port->cmd_issue[n]));
  1944. size += sprintf(&buf[size], "Allocated:\n");
  1945. for (n = 0; n < dd->slot_groups; n++) {
  1946. if (sizeof(long) > sizeof(u32))
  1947. group_allocated =
  1948. dd->port->allocated[n/2] >> (32*(n&1));
  1949. else
  1950. group_allocated = dd->port->allocated[n];
  1951. size += sprintf(&buf[size], "0x%08x\n",
  1952. group_allocated);
  1953. }
  1954. size += sprintf(&buf[size], "completed:\n");
  1955. for (n = 0; n < dd->slot_groups; n++)
  1956. size += sprintf(&buf[size], "0x%08x\n",
  1957. readl(dd->port->completed[n]));
  1958. size += sprintf(&buf[size], "PORT_IRQ_STAT 0x%08x\n",
  1959. readl(dd->port->mmio + PORT_IRQ_STAT));
  1960. size += sprintf(&buf[size], "HOST_IRQ_STAT 0x%08x\n",
  1961. readl(dd->mmio + HOST_IRQ_STAT));
  1962. return size;
  1963. }
  1964. static DEVICE_ATTR(registers, S_IRUGO, hw_show_registers, NULL);
  1965. /*
  1966. * Create the sysfs related attributes.
  1967. *
  1968. * @dd Pointer to the driver data structure.
  1969. * @kobj Pointer to the kobj for the block device.
  1970. *
  1971. * return value
  1972. * 0 Operation completed successfully.
  1973. * -EINVAL Invalid parameter.
  1974. */
  1975. static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
  1976. {
  1977. if (!kobj || !dd)
  1978. return -EINVAL;
  1979. if (sysfs_create_file(kobj, &dev_attr_registers.attr))
  1980. dev_warn(&dd->pdev->dev,
  1981. "Error creating registers sysfs entry\n");
  1982. return 0;
  1983. }
  1984. /*
  1985. * Remove the sysfs related attributes.
  1986. *
  1987. * @dd Pointer to the driver data structure.
  1988. * @kobj Pointer to the kobj for the block device.
  1989. *
  1990. * return value
  1991. * 0 Operation completed successfully.
  1992. * -EINVAL Invalid parameter.
  1993. */
  1994. static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
  1995. {
  1996. if (!kobj || !dd)
  1997. return -EINVAL;
  1998. sysfs_remove_file(kobj, &dev_attr_registers.attr);
  1999. return 0;
  2000. }
  2001. /*
  2002. * Perform any init/resume time hardware setup
  2003. *
  2004. * @dd Pointer to the driver data structure.
  2005. *
  2006. * return value
  2007. * None
  2008. */
  2009. static inline void hba_setup(struct driver_data *dd)
  2010. {
  2011. u32 hwdata;
  2012. hwdata = readl(dd->mmio + HOST_HSORG);
  2013. /* interrupt bug workaround: use only 1 IS bit.*/
  2014. writel(hwdata |
  2015. HSORG_DISABLE_SLOTGRP_INTR |
  2016. HSORG_DISABLE_SLOTGRP_PXIS,
  2017. dd->mmio + HOST_HSORG);
  2018. }
  2019. /*
  2020. * Detect the details of the product, and store anything needed
  2021. * into the driver data structure. This includes product type and
  2022. * version and number of slot groups.
  2023. *
  2024. * @dd Pointer to the driver data structure.
  2025. *
  2026. * return value
  2027. * None
  2028. */
  2029. static void mtip_detect_product(struct driver_data *dd)
  2030. {
  2031. u32 hwdata;
  2032. unsigned int rev, slotgroups;
  2033. /*
  2034. * HBA base + 0xFC [15:0] - vendor-specific hardware interface
  2035. * info register:
  2036. * [15:8] hardware/software interface rev#
  2037. * [ 3] asic-style interface
  2038. * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
  2039. */
  2040. hwdata = readl(dd->mmio + HOST_HSORG);
  2041. dd->product_type = MTIP_PRODUCT_UNKNOWN;
  2042. dd->slot_groups = 1;
  2043. if (hwdata & 0x8) {
  2044. dd->product_type = MTIP_PRODUCT_ASICFPGA;
  2045. rev = (hwdata & HSORG_HWREV) >> 8;
  2046. slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
  2047. dev_info(&dd->pdev->dev,
  2048. "ASIC-FPGA design, HS rev 0x%x, "
  2049. "%i slot groups [%i slots]\n",
  2050. rev,
  2051. slotgroups,
  2052. slotgroups * 32);
  2053. if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
  2054. dev_warn(&dd->pdev->dev,
  2055. "Warning: driver only supports "
  2056. "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
  2057. slotgroups = MTIP_MAX_SLOT_GROUPS;
  2058. }
  2059. dd->slot_groups = slotgroups;
  2060. return;
  2061. }
  2062. dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
  2063. }
  2064. /*
  2065. * Blocking wait for FTL rebuild to complete
  2066. *
  2067. * @dd Pointer to the DRIVER_DATA structure.
  2068. *
  2069. * return value
  2070. * 0 FTL rebuild completed successfully
  2071. * -EFAULT FTL rebuild error/timeout/interruption
  2072. */
  2073. static int mtip_ftl_rebuild_poll(struct driver_data *dd)
  2074. {
  2075. unsigned long timeout, cnt = 0, start;
  2076. dev_warn(&dd->pdev->dev,
  2077. "FTL rebuild in progress. Polling for completion.\n");
  2078. start = jiffies;
  2079. dd->ftlrebuildflag = 1;
  2080. timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
  2081. do {
  2082. #ifdef CONFIG_HOTPLUG
  2083. if (mtip_check_surprise_removal(dd->pdev))
  2084. return -EFAULT;
  2085. #endif
  2086. if (mtip_get_identify(dd->port, NULL) < 0)
  2087. return -EFAULT;
  2088. if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
  2089. MTIP_FTL_REBUILD_MAGIC) {
  2090. ssleep(1);
  2091. /* Print message every 3 minutes */
  2092. if (cnt++ >= 180) {
  2093. dev_warn(&dd->pdev->dev,
  2094. "FTL rebuild in progress (%d secs).\n",
  2095. jiffies_to_msecs(jiffies - start) / 1000);
  2096. cnt = 0;
  2097. }
  2098. } else {
  2099. dev_warn(&dd->pdev->dev,
  2100. "FTL rebuild complete (%d secs).\n",
  2101. jiffies_to_msecs(jiffies - start) / 1000);
  2102. dd->ftlrebuildflag = 0;
  2103. break;
  2104. }
  2105. ssleep(10);
  2106. } while (time_before(jiffies, timeout));
  2107. /* Check for timeout */
  2108. if (dd->ftlrebuildflag) {
  2109. dev_err(&dd->pdev->dev,
  2110. "Timed out waiting for FTL rebuild to complete (%d secs).\n",
  2111. jiffies_to_msecs(jiffies - start) / 1000);
  2112. return -EFAULT;
  2113. }
  2114. return 0;
  2115. }
  2116. /*
  2117. * Called once for each card.
  2118. *
  2119. * @dd Pointer to the driver data structure.
  2120. *
  2121. * return value
  2122. * 0 on success, else an error code.
  2123. */
  2124. static int mtip_hw_init(struct driver_data *dd)
  2125. {
  2126. int i;
  2127. int rv;
  2128. unsigned int num_command_slots;
  2129. dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
  2130. mtip_detect_product(dd);
  2131. if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
  2132. rv = -EIO;
  2133. goto out1;
  2134. }
  2135. num_command_slots = dd->slot_groups * 32;
  2136. hba_setup(dd);
  2137. /*
  2138. * Initialize the internal semaphore
  2139. * Use a rw semaphore to enable prioritization of
  2140. * mgmnt ioctl traffic during heavy IO load
  2141. */
  2142. init_rwsem(&dd->internal_sem);
  2143. tasklet_init(&dd->tasklet, mtip_tasklet, (unsigned long)dd);
  2144. dd->port = kzalloc(sizeof(struct mtip_port), GFP_KERNEL);
  2145. if (!dd->port) {
  2146. dev_err(&dd->pdev->dev,
  2147. "Memory allocation: port structure\n");
  2148. return -ENOMEM;
  2149. }
  2150. /* Counting semaphore to track command slot usage */
  2151. sema_init(&dd->port->cmd_slot, num_command_slots - 1);
  2152. /* Spinlock to prevent concurrent issue */
  2153. spin_lock_init(&dd->port->cmd_issue_lock);
  2154. /* Set the port mmio base address. */
  2155. dd->port->mmio = dd->mmio + PORT_OFFSET;
  2156. dd->port->dd = dd;
  2157. /* Allocate memory for the command list. */
  2158. dd->port->command_list =
  2159. dmam_alloc_coherent(&dd->pdev->dev,
  2160. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
  2161. &dd->port->command_list_dma,
  2162. GFP_KERNEL);
  2163. if (!dd->port->command_list) {
  2164. dev_err(&dd->pdev->dev,
  2165. "Memory allocation: command list\n");
  2166. rv = -ENOMEM;
  2167. goto out1;
  2168. }
  2169. /* Clear the memory we have allocated. */
  2170. memset(dd->port->command_list,
  2171. 0,
  2172. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2));
  2173. /* Setup the addresse of the RX FIS. */
  2174. dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
  2175. dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
  2176. /* Setup the address of the command tables. */
  2177. dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
  2178. dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
  2179. /* Setup the address of the identify data. */
  2180. dd->port->identify = dd->port->command_table +
  2181. HW_CMD_TBL_AR_SZ;
  2182. dd->port->identify_dma = dd->port->command_tbl_dma +
  2183. HW_CMD_TBL_AR_SZ;
  2184. /* Setup the address of the sector buffer. */
  2185. dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
  2186. dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
  2187. /* Point the command headers at the command tables. */
  2188. for (i = 0; i < num_command_slots; i++) {
  2189. dd->port->commands[i].command_header =
  2190. dd->port->command_list +
  2191. (sizeof(struct mtip_cmd_hdr) * i);
  2192. dd->port->commands[i].command_header_dma =
  2193. dd->port->command_list_dma +
  2194. (sizeof(struct mtip_cmd_hdr) * i);
  2195. dd->port->commands[i].command =
  2196. dd->port->command_table + (HW_CMD_TBL_SZ * i);
  2197. dd->port->commands[i].command_dma =
  2198. dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
  2199. if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
  2200. dd->port->commands[i].command_header->ctbau =
  2201. cpu_to_le32(
  2202. (dd->port->commands[i].command_dma >> 16) >> 16);
  2203. dd->port->commands[i].command_header->ctba = cpu_to_le32(
  2204. dd->port->commands[i].command_dma & 0xffffffff);
  2205. /*
  2206. * If this is not done, a bug is reported by the stock
  2207. * FC11 i386. Due to the fact that it has lots of kernel
  2208. * debugging enabled.
  2209. */
  2210. sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
  2211. /* Mark all commands as currently inactive.*/
  2212. atomic_set(&dd->port->commands[i].active, 0);
  2213. }
  2214. /* Setup the pointers to the extended s_active and CI registers. */
  2215. for (i = 0; i < dd->slot_groups; i++) {
  2216. dd->port->s_active[i] =
  2217. dd->port->mmio + i*0x80 + PORT_SCR_ACT;
  2218. dd->port->cmd_issue[i] =
  2219. dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
  2220. dd->port->completed[i] =
  2221. dd->port->mmio + i*0x80 + PORT_SDBV;
  2222. }
  2223. /* Reset the HBA. */
  2224. if (mtip_hba_reset(dd) < 0) {
  2225. dev_err(&dd->pdev->dev,
  2226. "Card did not reset within timeout\n");
  2227. rv = -EIO;
  2228. goto out2;
  2229. }
  2230. mtip_init_port(dd->port);
  2231. mtip_start_port(dd->port);
  2232. /* Setup the ISR and enable interrupts. */
  2233. rv = devm_request_irq(&dd->pdev->dev,
  2234. dd->pdev->irq,
  2235. mtip_irq_handler,
  2236. IRQF_SHARED,
  2237. dev_driver_string(&dd->pdev->dev),
  2238. dd);
  2239. if (rv) {
  2240. dev_err(&dd->pdev->dev,
  2241. "Unable to allocate IRQ %d\n", dd->pdev->irq);
  2242. goto out2;
  2243. }
  2244. /* Enable interrupts on the HBA. */
  2245. writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
  2246. dd->mmio + HOST_CTL);
  2247. init_timer(&dd->port->cmd_timer);
  2248. dd->port->cmd_timer.data = (unsigned long int) dd->port;
  2249. dd->port->cmd_timer.function = mtip_timeout_function;
  2250. mod_timer(&dd->port->cmd_timer,
  2251. jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
  2252. if (mtip_get_identify(dd->port, NULL) < 0) {
  2253. rv = -EFAULT;
  2254. goto out3;
  2255. }
  2256. mtip_dump_identify(dd->port);
  2257. if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
  2258. MTIP_FTL_REBUILD_MAGIC) {
  2259. return mtip_ftl_rebuild_poll(dd);
  2260. }
  2261. return rv;
  2262. out3:
  2263. del_timer_sync(&dd->port->cmd_timer);
  2264. /* Disable interrupts on the HBA. */
  2265. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  2266. dd->mmio + HOST_CTL);
  2267. /*Release the IRQ. */
  2268. devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
  2269. out2:
  2270. mtip_deinit_port(dd->port);
  2271. /* Free the command/command header memory. */
  2272. dmam_free_coherent(&dd->pdev->dev,
  2273. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
  2274. dd->port->command_list,
  2275. dd->port->command_list_dma);
  2276. out1:
  2277. /* Free the memory allocated for the for structure. */
  2278. kfree(dd->port);
  2279. return rv;
  2280. }
  2281. /*
  2282. * Called to deinitialize an interface.
  2283. *
  2284. * @dd Pointer to the driver data structure.
  2285. *
  2286. * return value
  2287. * 0
  2288. */
  2289. static int mtip_hw_exit(struct driver_data *dd)
  2290. {
  2291. /*
  2292. * Send standby immediate (E0h) to the drive so that it
  2293. * saves its state.
  2294. */
  2295. if (atomic_read(&dd->drv_cleanup_done) != true) {
  2296. mtip_standby_immediate(dd->port);
  2297. /* de-initialize the port. */
  2298. mtip_deinit_port(dd->port);
  2299. /* Disable interrupts on the HBA. */
  2300. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  2301. dd->mmio + HOST_CTL);
  2302. }
  2303. del_timer_sync(&dd->port->cmd_timer);
  2304. /* Stop the bottom half tasklet. */
  2305. tasklet_kill(&dd->tasklet);
  2306. /* Release the IRQ. */
  2307. devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
  2308. /* Free the command/command header memory. */
  2309. dmam_free_coherent(&dd->pdev->dev,
  2310. HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
  2311. dd->port->command_list,
  2312. dd->port->command_list_dma);
  2313. /* Free the memory allocated for the for structure. */
  2314. kfree(dd->port);
  2315. return 0;
  2316. }
  2317. /*
  2318. * Issue a Standby Immediate command to the device.
  2319. *
  2320. * This function is called by the Block Layer just before the
  2321. * system powers off during a shutdown.
  2322. *
  2323. * @dd Pointer to the driver data structure.
  2324. *
  2325. * return value
  2326. * 0
  2327. */
  2328. static int mtip_hw_shutdown(struct driver_data *dd)
  2329. {
  2330. /*
  2331. * Send standby immediate (E0h) to the drive so that it
  2332. * saves its state.
  2333. */
  2334. mtip_standby_immediate(dd->port);
  2335. return 0;
  2336. }
  2337. /*
  2338. * Suspend function
  2339. *
  2340. * This function is called by the Block Layer just before the
  2341. * system hibernates.
  2342. *
  2343. * @dd Pointer to the driver data structure.
  2344. *
  2345. * return value
  2346. * 0 Suspend was successful
  2347. * -EFAULT Suspend was not successful
  2348. */
  2349. static int mtip_hw_suspend(struct driver_data *dd)
  2350. {
  2351. /*
  2352. * Send standby immediate (E0h) to the drive
  2353. * so that it saves its state.
  2354. */
  2355. if (mtip_standby_immediate(dd->port) != 0) {
  2356. dev_err(&dd->pdev->dev,
  2357. "Failed standby-immediate command\n");
  2358. return -EFAULT;
  2359. }
  2360. /* Disable interrupts on the HBA.*/
  2361. writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
  2362. dd->mmio + HOST_CTL);
  2363. mtip_deinit_port(dd->port);
  2364. return 0;
  2365. }
  2366. /*
  2367. * Resume function
  2368. *
  2369. * This function is called by the Block Layer as the
  2370. * system resumes.
  2371. *
  2372. * @dd Pointer to the driver data structure.
  2373. *
  2374. * return value
  2375. * 0 Resume was successful
  2376. * -EFAULT Resume was not successful
  2377. */
  2378. static int mtip_hw_resume(struct driver_data *dd)
  2379. {
  2380. /* Perform any needed hardware setup steps */
  2381. hba_setup(dd);
  2382. /* Reset the HBA */
  2383. if (mtip_hba_reset(dd) != 0) {
  2384. dev_err(&dd->pdev->dev,
  2385. "Unable to reset the HBA\n");
  2386. return -EFAULT;
  2387. }
  2388. /*
  2389. * Enable the port, DMA engine, and FIS reception specific
  2390. * h/w in controller.
  2391. */
  2392. mtip_init_port(dd->port);
  2393. mtip_start_port(dd->port);
  2394. /* Enable interrupts on the HBA.*/
  2395. writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
  2396. dd->mmio + HOST_CTL);
  2397. return 0;
  2398. }
  2399. /*
  2400. * Helper function for reusing disk name
  2401. * upon hot insertion.
  2402. */
  2403. static int rssd_disk_name_format(char *prefix,
  2404. int index,
  2405. char *buf,
  2406. int buflen)
  2407. {
  2408. const int base = 'z' - 'a' + 1;
  2409. char *begin = buf + strlen(prefix);
  2410. char *end = buf + buflen;
  2411. char *p;
  2412. int unit;
  2413. p = end - 1;
  2414. *p = '\0';
  2415. unit = base;
  2416. do {
  2417. if (p == begin)
  2418. return -EINVAL;
  2419. *--p = 'a' + (index % unit);
  2420. index = (index / unit) - 1;
  2421. } while (index >= 0);
  2422. memmove(begin, p, end - p);
  2423. memcpy(buf, prefix, strlen(prefix));
  2424. return 0;
  2425. }
  2426. /*
  2427. * Block layer IOCTL handler.
  2428. *
  2429. * @dev Pointer to the block_device structure.
  2430. * @mode ignored
  2431. * @cmd IOCTL command passed from the user application.
  2432. * @arg Argument passed from the user application.
  2433. *
  2434. * return value
  2435. * 0 IOCTL completed successfully.
  2436. * -ENOTTY IOCTL not supported or invalid driver data
  2437. * structure pointer.
  2438. */
  2439. static int mtip_block_ioctl(struct block_device *dev,
  2440. fmode_t mode,
  2441. unsigned cmd,
  2442. unsigned long arg)
  2443. {
  2444. struct driver_data *dd = dev->bd_disk->private_data;
  2445. if (!capable(CAP_SYS_ADMIN))
  2446. return -EACCES;
  2447. if (!dd)
  2448. return -ENOTTY;
  2449. switch (cmd) {
  2450. case BLKFLSBUF:
  2451. return 0;
  2452. default:
  2453. return mtip_hw_ioctl(dd, cmd, arg);
  2454. }
  2455. }
  2456. #ifdef CONFIG_COMPAT
  2457. /*
  2458. * Block layer compat IOCTL handler.
  2459. *
  2460. * @dev Pointer to the block_device structure.
  2461. * @mode ignored
  2462. * @cmd IOCTL command passed from the user application.
  2463. * @arg Argument passed from the user application.
  2464. *
  2465. * return value
  2466. * 0 IOCTL completed successfully.
  2467. * -ENOTTY IOCTL not supported or invalid driver data
  2468. * structure pointer.
  2469. */
  2470. static int mtip_block_compat_ioctl(struct block_device *dev,
  2471. fmode_t mode,
  2472. unsigned cmd,
  2473. unsigned long arg)
  2474. {
  2475. struct driver_data *dd = dev->bd_disk->private_data;
  2476. if (!capable(CAP_SYS_ADMIN))
  2477. return -EACCES;
  2478. if (!dd)
  2479. return -ENOTTY;
  2480. switch (cmd) {
  2481. case BLKFLSBUF:
  2482. return 0;
  2483. case HDIO_DRIVE_TASKFILE: {
  2484. struct mtip_compat_ide_task_request_s *compat_req_task;
  2485. ide_task_request_t req_task;
  2486. int compat_tasksize, outtotal, ret;
  2487. compat_tasksize = sizeof(struct mtip_compat_ide_task_request_s);
  2488. compat_req_task =
  2489. (struct mtip_compat_ide_task_request_s __user *) arg;
  2490. if (copy_from_user(&req_task, (void __user *) arg,
  2491. compat_tasksize - (2 * sizeof(compat_long_t))))
  2492. return -EFAULT;
  2493. if (get_user(req_task.out_size, &compat_req_task->out_size))
  2494. return -EFAULT;
  2495. if (get_user(req_task.in_size, &compat_req_task->in_size))
  2496. return -EFAULT;
  2497. outtotal = sizeof(struct mtip_compat_ide_task_request_s);
  2498. ret = exec_drive_taskfile(dd, (void __user *) arg,
  2499. &req_task, outtotal);
  2500. if (copy_to_user((void __user *) arg, &req_task,
  2501. compat_tasksize -
  2502. (2 * sizeof(compat_long_t))))
  2503. return -EFAULT;
  2504. if (put_user(req_task.out_size, &compat_req_task->out_size))
  2505. return -EFAULT;
  2506. if (put_user(req_task.in_size, &compat_req_task->in_size))
  2507. return -EFAULT;
  2508. return ret;
  2509. }
  2510. default:
  2511. return mtip_hw_ioctl(dd, cmd, arg);
  2512. }
  2513. }
  2514. #endif
  2515. /*
  2516. * Obtain the geometry of the device.
  2517. *
  2518. * You may think that this function is obsolete, but some applications,
  2519. * fdisk for example still used CHS values. This function describes the
  2520. * device as having 224 heads and 56 sectors per cylinder. These values are
  2521. * chosen so that each cylinder is aligned on a 4KB boundary. Since a
  2522. * partition is described in terms of a start and end cylinder this means
  2523. * that each partition is also 4KB aligned. Non-aligned partitions adversely
  2524. * affects performance.
  2525. *
  2526. * @dev Pointer to the block_device strucutre.
  2527. * @geo Pointer to a hd_geometry structure.
  2528. *
  2529. * return value
  2530. * 0 Operation completed successfully.
  2531. * -ENOTTY An error occurred while reading the drive capacity.
  2532. */
  2533. static int mtip_block_getgeo(struct block_device *dev,
  2534. struct hd_geometry *geo)
  2535. {
  2536. struct driver_data *dd = dev->bd_disk->private_data;
  2537. sector_t capacity;
  2538. if (!dd)
  2539. return -ENOTTY;
  2540. if (!(mtip_hw_get_capacity(dd, &capacity))) {
  2541. dev_warn(&dd->pdev->dev,
  2542. "Could not get drive capacity.\n");
  2543. return -ENOTTY;
  2544. }
  2545. geo->heads = 224;
  2546. geo->sectors = 56;
  2547. #if BITS_PER_LONG == 64
  2548. geo->cylinders = capacity / (geo->heads * geo->sectors);
  2549. #else
  2550. do_div(capacity, (geo->heads * geo->sectors));
  2551. geo->cylinders = capacity;
  2552. #endif
  2553. return 0;
  2554. }
  2555. /*
  2556. * Block device operation function.
  2557. *
  2558. * This structure contains pointers to the functions required by the block
  2559. * layer.
  2560. */
  2561. static const struct block_device_operations mtip_block_ops = {
  2562. .ioctl = mtip_block_ioctl,
  2563. #ifdef CONFIG_COMPAT
  2564. .compat_ioctl = mtip_block_compat_ioctl,
  2565. #endif
  2566. .getgeo = mtip_block_getgeo,
  2567. .owner = THIS_MODULE
  2568. };
  2569. /*
  2570. * Block layer make request function.
  2571. *
  2572. * This function is called by the kernel to process a BIO for
  2573. * the P320 device.
  2574. *
  2575. * @queue Pointer to the request queue. Unused other than to obtain
  2576. * the driver data structure.
  2577. * @bio Pointer to the BIO.
  2578. *
  2579. * return value
  2580. * 0
  2581. */
  2582. static int mtip_make_request(struct request_queue *queue, struct bio *bio)
  2583. {
  2584. struct driver_data *dd = queue->queuedata;
  2585. struct scatterlist *sg;
  2586. struct bio_vec *bvec;
  2587. int nents = 0;
  2588. int tag = 0;
  2589. if (unlikely(!bio_has_data(bio))) {
  2590. blk_queue_flush(queue, 0);
  2591. bio_endio(bio, 0);
  2592. return 0;
  2593. }
  2594. if (unlikely(atomic_read(&dd->eh_active))) {
  2595. bio_endio(bio, -EBUSY);
  2596. return 0;
  2597. }
  2598. sg = mtip_hw_get_scatterlist(dd, &tag);
  2599. if (likely(sg != NULL)) {
  2600. blk_queue_bounce(queue, &bio);
  2601. if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
  2602. dev_warn(&dd->pdev->dev,
  2603. "Maximum number of SGL entries exceeded");
  2604. bio_io_error(bio);
  2605. mtip_hw_release_scatterlist(dd, tag);
  2606. return 0;
  2607. }
  2608. /* Create the scatter list for this bio. */
  2609. bio_for_each_segment(bvec, bio, nents) {
  2610. sg_set_page(&sg[nents],
  2611. bvec->bv_page,
  2612. bvec->bv_len,
  2613. bvec->bv_offset);
  2614. }
  2615. /* Issue the read/write. */
  2616. mtip_hw_submit_io(dd,
  2617. bio->bi_sector,
  2618. bio_sectors(bio),
  2619. nents,
  2620. tag,
  2621. bio_endio,
  2622. bio,
  2623. bio->bi_rw & REQ_FLUSH,
  2624. bio_data_dir(bio));
  2625. } else {
  2626. bio_io_error(bio);
  2627. }
  2628. return 0;
  2629. }
  2630. /*
  2631. * Block layer initialization function.
  2632. *
  2633. * This function is called once by the PCI layer for each P320
  2634. * device that is connected to the system.
  2635. *
  2636. * @dd Pointer to the driver data structure.
  2637. *
  2638. * return value
  2639. * 0 on success else an error code.
  2640. */
  2641. static int mtip_block_initialize(struct driver_data *dd)
  2642. {
  2643. int rv = 0;
  2644. sector_t capacity;
  2645. unsigned int index = 0;
  2646. struct kobject *kobj;
  2647. /* Initialize the protocol layer. */
  2648. rv = mtip_hw_init(dd);
  2649. if (rv < 0) {
  2650. dev_err(&dd->pdev->dev,
  2651. "Protocol layer initialization failed\n");
  2652. rv = -EINVAL;
  2653. goto protocol_init_error;
  2654. }
  2655. /* Allocate the request queue. */
  2656. dd->queue = blk_alloc_queue(GFP_KERNEL);
  2657. if (dd->queue == NULL) {
  2658. dev_err(&dd->pdev->dev,
  2659. "Unable to allocate request queue\n");
  2660. rv = -ENOMEM;
  2661. goto block_queue_alloc_init_error;
  2662. }
  2663. /* Attach our request function to the request queue. */
  2664. blk_queue_make_request(dd->queue, mtip_make_request);
  2665. /* Set device limits. */
  2666. set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
  2667. blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
  2668. blk_queue_physical_block_size(dd->queue, 4096);
  2669. blk_queue_io_min(dd->queue, 4096);
  2670. dd->disk = alloc_disk(MTIP_MAX_MINORS);
  2671. if (dd->disk == NULL) {
  2672. dev_err(&dd->pdev->dev,
  2673. "Unable to allocate gendisk structure\n");
  2674. rv = -EINVAL;
  2675. goto alloc_disk_error;
  2676. }
  2677. /* Generate the disk name, implemented same as in sd.c */
  2678. do {
  2679. if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
  2680. goto ida_get_error;
  2681. spin_lock(&rssd_index_lock);
  2682. rv = ida_get_new(&rssd_index_ida, &index);
  2683. spin_unlock(&rssd_index_lock);
  2684. } while (rv == -EAGAIN);
  2685. if (rv)
  2686. goto ida_get_error;
  2687. rv = rssd_disk_name_format("rssd",
  2688. index,
  2689. dd->disk->disk_name,
  2690. DISK_NAME_LEN);
  2691. if (rv)
  2692. goto disk_index_error;
  2693. dd->disk->driverfs_dev = &dd->pdev->dev;
  2694. dd->disk->major = dd->major;
  2695. dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
  2696. dd->disk->fops = &mtip_block_ops;
  2697. dd->disk->queue = dd->queue;
  2698. dd->disk->private_data = dd;
  2699. dd->queue->queuedata = dd;
  2700. dd->index = index;
  2701. /* Set the capacity of the device in 512 byte sectors. */
  2702. if (!(mtip_hw_get_capacity(dd, &capacity))) {
  2703. dev_warn(&dd->pdev->dev,
  2704. "Could not read drive capacity\n");
  2705. rv = -EIO;
  2706. goto read_capacity_error;
  2707. }
  2708. set_capacity(dd->disk, capacity);
  2709. /* Enable the block device and add it to /dev */
  2710. add_disk(dd->disk);
  2711. /*
  2712. * Now that the disk is active, initialize any sysfs attributes
  2713. * managed by the protocol layer.
  2714. */
  2715. kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
  2716. if (kobj) {
  2717. mtip_hw_sysfs_init(dd, kobj);
  2718. kobject_put(kobj);
  2719. }
  2720. return rv;
  2721. read_capacity_error:
  2722. /*
  2723. * Delete our gendisk structure. This also removes the device
  2724. * from /dev
  2725. */
  2726. del_gendisk(dd->disk);
  2727. disk_index_error:
  2728. spin_lock(&rssd_index_lock);
  2729. ida_remove(&rssd_index_ida, index);
  2730. spin_unlock(&rssd_index_lock);
  2731. ida_get_error:
  2732. put_disk(dd->disk);
  2733. alloc_disk_error:
  2734. blk_cleanup_queue(dd->queue);
  2735. block_queue_alloc_init_error:
  2736. /* De-initialize the protocol layer. */
  2737. mtip_hw_exit(dd);
  2738. protocol_init_error:
  2739. return rv;
  2740. }
  2741. /*
  2742. * Block layer deinitialization function.
  2743. *
  2744. * Called by the PCI layer as each P320 device is removed.
  2745. *
  2746. * @dd Pointer to the driver data structure.
  2747. *
  2748. * return value
  2749. * 0
  2750. */
  2751. static int mtip_block_remove(struct driver_data *dd)
  2752. {
  2753. struct kobject *kobj;
  2754. /* Clean up the sysfs attributes managed by the protocol layer. */
  2755. kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
  2756. if (kobj) {
  2757. mtip_hw_sysfs_exit(dd, kobj);
  2758. kobject_put(kobj);
  2759. }
  2760. /*
  2761. * Delete our gendisk structure. This also removes the device
  2762. * from /dev
  2763. */
  2764. del_gendisk(dd->disk);
  2765. blk_cleanup_queue(dd->queue);
  2766. dd->disk = NULL;
  2767. dd->queue = NULL;
  2768. /* De-initialize the protocol layer. */
  2769. mtip_hw_exit(dd);
  2770. return 0;
  2771. }
  2772. /*
  2773. * Function called by the PCI layer when just before the
  2774. * machine shuts down.
  2775. *
  2776. * If a protocol layer shutdown function is present it will be called
  2777. * by this function.
  2778. *
  2779. * @dd Pointer to the driver data structure.
  2780. *
  2781. * return value
  2782. * 0
  2783. */
  2784. static int mtip_block_shutdown(struct driver_data *dd)
  2785. {
  2786. dev_info(&dd->pdev->dev,
  2787. "Shutting down %s ...\n", dd->disk->disk_name);
  2788. /* Delete our gendisk structure, and cleanup the blk queue. */
  2789. del_gendisk(dd->disk);
  2790. blk_cleanup_queue(dd->queue);
  2791. dd->disk = NULL;
  2792. dd->queue = NULL;
  2793. mtip_hw_shutdown(dd);
  2794. return 0;
  2795. }
  2796. static int mtip_block_suspend(struct driver_data *dd)
  2797. {
  2798. dev_info(&dd->pdev->dev,
  2799. "Suspending %s ...\n", dd->disk->disk_name);
  2800. mtip_hw_suspend(dd);
  2801. return 0;
  2802. }
  2803. static int mtip_block_resume(struct driver_data *dd)
  2804. {
  2805. dev_info(&dd->pdev->dev, "Resuming %s ...\n",
  2806. dd->disk->disk_name);
  2807. mtip_hw_resume(dd);
  2808. return 0;
  2809. }
  2810. /*
  2811. * Called for each supported PCI device detected.
  2812. *
  2813. * This function allocates the private data structure, enables the
  2814. * PCI device and then calls the block layer initialization function.
  2815. *
  2816. * return value
  2817. * 0 on success else an error code.
  2818. */
  2819. static int mtip_pci_probe(struct pci_dev *pdev,
  2820. const struct pci_device_id *ent)
  2821. {
  2822. int rv = 0;
  2823. struct driver_data *dd = NULL;
  2824. /* Allocate memory for this devices private data. */
  2825. dd = kzalloc(sizeof(struct driver_data), GFP_KERNEL);
  2826. if (dd == NULL) {
  2827. dev_err(&pdev->dev,
  2828. "Unable to allocate memory for driver data\n");
  2829. return -ENOMEM;
  2830. }
  2831. /* Set the atomic variable as 1 in case of SRSI */
  2832. atomic_set(&dd->drv_cleanup_done, true);
  2833. atomic_set(&dd->resumeflag, false);
  2834. atomic_set(&dd->eh_active, 0);
  2835. /* Attach the private data to this PCI device. */
  2836. pci_set_drvdata(pdev, dd);
  2837. rv = pcim_enable_device(pdev);
  2838. if (rv < 0) {
  2839. dev_err(&pdev->dev, "Unable to enable device\n");
  2840. goto iomap_err;
  2841. }
  2842. /* Map BAR5 to memory. */
  2843. rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
  2844. if (rv < 0) {
  2845. dev_err(&pdev->dev, "Unable to map regions\n");
  2846. goto iomap_err;
  2847. }
  2848. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  2849. rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  2850. if (rv) {
  2851. rv = pci_set_consistent_dma_mask(pdev,
  2852. DMA_BIT_MASK(32));
  2853. if (rv) {
  2854. dev_warn(&pdev->dev,
  2855. "64-bit DMA enable failed\n");
  2856. goto setmask_err;
  2857. }
  2858. }
  2859. }
  2860. pci_set_master(pdev);
  2861. if (pci_enable_msi(pdev)) {
  2862. dev_warn(&pdev->dev,
  2863. "Unable to enable MSI interrupt.\n");
  2864. goto block_initialize_err;
  2865. }
  2866. /* Copy the info we may need later into the private data structure. */
  2867. dd->major = mtip_major;
  2868. dd->protocol = ent->driver_data;
  2869. dd->instance = instance;
  2870. dd->pdev = pdev;
  2871. /* Initialize the block layer. */
  2872. rv = mtip_block_initialize(dd);
  2873. if (rv < 0) {
  2874. dev_err(&pdev->dev,
  2875. "Unable to initialize block layer\n");
  2876. goto block_initialize_err;
  2877. }
  2878. /*
  2879. * Increment the instance count so that each device has a unique
  2880. * instance number.
  2881. */
  2882. instance++;
  2883. goto done;
  2884. block_initialize_err:
  2885. pci_disable_msi(pdev);
  2886. setmask_err:
  2887. pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
  2888. iomap_err:
  2889. kfree(dd);
  2890. pci_set_drvdata(pdev, NULL);
  2891. return rv;
  2892. done:
  2893. /* Set the atomic variable as 0 in case of SRSI */
  2894. atomic_set(&dd->drv_cleanup_done, true);
  2895. return rv;
  2896. }
  2897. /*
  2898. * Called for each probed device when the device is removed or the
  2899. * driver is unloaded.
  2900. *
  2901. * return value
  2902. * None
  2903. */
  2904. static void mtip_pci_remove(struct pci_dev *pdev)
  2905. {
  2906. struct driver_data *dd = pci_get_drvdata(pdev);
  2907. int counter = 0;
  2908. if (mtip_check_surprise_removal(pdev)) {
  2909. while (atomic_read(&dd->drv_cleanup_done) == false) {
  2910. counter++;
  2911. msleep(20);
  2912. if (counter == 10) {
  2913. /* Cleanup the outstanding commands */
  2914. mtip_command_cleanup(dd);
  2915. break;
  2916. }
  2917. }
  2918. }
  2919. /* Set the atomic variable as 1 in case of SRSI */
  2920. atomic_set(&dd->drv_cleanup_done, true);
  2921. /* Clean up the block layer. */
  2922. mtip_block_remove(dd);
  2923. pci_disable_msi(pdev);
  2924. kfree(dd);
  2925. pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
  2926. }
  2927. /*
  2928. * Called for each probed device when the device is suspended.
  2929. *
  2930. * return value
  2931. * 0 Success
  2932. * <0 Error
  2933. */
  2934. static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
  2935. {
  2936. int rv = 0;
  2937. struct driver_data *dd = pci_get_drvdata(pdev);
  2938. if (!dd) {
  2939. dev_err(&pdev->dev,
  2940. "Driver private datastructure is NULL\n");
  2941. return -EFAULT;
  2942. }
  2943. atomic_set(&dd->resumeflag, true);
  2944. /* Disable ports & interrupts then send standby immediate */
  2945. rv = mtip_block_suspend(dd);
  2946. if (rv < 0) {
  2947. dev_err(&pdev->dev,
  2948. "Failed to suspend controller\n");
  2949. return rv;
  2950. }
  2951. /*
  2952. * Save the pci config space to pdev structure &
  2953. * disable the device
  2954. */
  2955. pci_save_state(pdev);
  2956. pci_disable_device(pdev);
  2957. /* Move to Low power state*/
  2958. pci_set_power_state(pdev, PCI_D3hot);
  2959. return rv;
  2960. }
  2961. /*
  2962. * Called for each probed device when the device is resumed.
  2963. *
  2964. * return value
  2965. * 0 Success
  2966. * <0 Error
  2967. */
  2968. static int mtip_pci_resume(struct pci_dev *pdev)
  2969. {
  2970. int rv = 0;
  2971. struct driver_data *dd;
  2972. dd = pci_get_drvdata(pdev);
  2973. if (!dd) {
  2974. dev_err(&pdev->dev,
  2975. "Driver private datastructure is NULL\n");
  2976. return -EFAULT;
  2977. }
  2978. /* Move the device to active State */
  2979. pci_set_power_state(pdev, PCI_D0);
  2980. /* Restore PCI configuration space */
  2981. pci_restore_state(pdev);
  2982. /* Enable the PCI device*/
  2983. rv = pcim_enable_device(pdev);
  2984. if (rv < 0) {
  2985. dev_err(&pdev->dev,
  2986. "Failed to enable card during resume\n");
  2987. goto err;
  2988. }
  2989. pci_set_master(pdev);
  2990. /*
  2991. * Calls hbaReset, initPort, & startPort function
  2992. * then enables interrupts
  2993. */
  2994. rv = mtip_block_resume(dd);
  2995. if (rv < 0)
  2996. dev_err(&pdev->dev, "Unable to resume\n");
  2997. err:
  2998. atomic_set(&dd->resumeflag, false);
  2999. return rv;
  3000. }
  3001. /*
  3002. * Shutdown routine
  3003. *
  3004. * return value
  3005. * None
  3006. */
  3007. static void mtip_pci_shutdown(struct pci_dev *pdev)
  3008. {
  3009. struct driver_data *dd = pci_get_drvdata(pdev);
  3010. if (dd)
  3011. mtip_block_shutdown(dd);
  3012. }
  3013. /* Table of device ids supported by this driver. */
  3014. static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
  3015. { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320_DEVICE_ID) },
  3016. { 0 }
  3017. };
  3018. /* Structure that describes the PCI driver functions. */
  3019. struct pci_driver mtip_pci_driver = {
  3020. .name = MTIP_DRV_NAME,
  3021. .id_table = mtip_pci_tbl,
  3022. .probe = mtip_pci_probe,
  3023. .remove = mtip_pci_remove,
  3024. .suspend = mtip_pci_suspend,
  3025. .resume = mtip_pci_resume,
  3026. .shutdown = mtip_pci_shutdown,
  3027. };
  3028. MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
  3029. /*
  3030. * Module initialization function.
  3031. *
  3032. * Called once when the module is loaded. This function allocates a major
  3033. * block device number to the Cyclone devices and registers the PCI layer
  3034. * of the driver.
  3035. *
  3036. * Return value
  3037. * 0 on success else error code.
  3038. */
  3039. static int __init mtip_init(void)
  3040. {
  3041. printk(KERN_INFO MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
  3042. /* Allocate a major block device number to use with this driver. */
  3043. mtip_major = register_blkdev(0, MTIP_DRV_NAME);
  3044. if (mtip_major < 0) {
  3045. printk(KERN_ERR "Unable to register block device (%d)\n",
  3046. mtip_major);
  3047. return -EBUSY;
  3048. }
  3049. /* Register our PCI operations. */
  3050. return pci_register_driver(&mtip_pci_driver);
  3051. }
  3052. /*
  3053. * Module de-initialization function.
  3054. *
  3055. * Called once when the module is unloaded. This function deallocates
  3056. * the major block device number allocated by mtip_init() and
  3057. * unregisters the PCI layer of the driver.
  3058. *
  3059. * Return value
  3060. * none
  3061. */
  3062. static void __exit mtip_exit(void)
  3063. {
  3064. /* Release the allocated major block device number. */
  3065. unregister_blkdev(mtip_major, MTIP_DRV_NAME);
  3066. /* Unregister the PCI driver. */
  3067. pci_unregister_driver(&mtip_pci_driver);
  3068. }
  3069. MODULE_AUTHOR("Micron Technology, Inc");
  3070. MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
  3071. MODULE_LICENSE("GPL");
  3072. MODULE_VERSION(MTIP_DRV_VERSION);
  3073. module_init(mtip_init);
  3074. module_exit(mtip_exit);