mtip32xx.c 84 KB

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