sata_nv.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524
  1. /*
  2. * sata_nv.c - NVIDIA nForce SATA
  3. *
  4. * Copyright 2004 NVIDIA Corp. All rights reserved.
  5. * Copyright 2004 Andrew Chew
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; see the file COPYING. If not, write to
  20. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. *
  23. * libata documentation is available via 'make {ps|pdf}docs',
  24. * as Documentation/driver-api/libata.rst
  25. *
  26. * No hardware documentation available outside of NVIDIA.
  27. * This driver programs the NVIDIA SATA controller in a similar
  28. * fashion as with other PCI IDE BMDMA controllers, with a few
  29. * NV-specific details such as register offsets, SATA phy location,
  30. * hotplug info, etc.
  31. *
  32. * CK804/MCP04 controllers support an alternate programming interface
  33. * similar to the ADMA specification (with some modifications).
  34. * This allows the use of NCQ. Non-DMA-mapped ATA commands are still
  35. * sent through the legacy interface.
  36. *
  37. */
  38. #include <linux/kernel.h>
  39. #include <linux/module.h>
  40. #include <linux/gfp.h>
  41. #include <linux/pci.h>
  42. #include <linux/blkdev.h>
  43. #include <linux/delay.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/device.h>
  46. #include <scsi/scsi_host.h>
  47. #include <scsi/scsi_device.h>
  48. #include <linux/libata.h>
  49. #define DRV_NAME "sata_nv"
  50. #define DRV_VERSION "3.5"
  51. #define NV_ADMA_DMA_BOUNDARY 0xffffffffUL
  52. enum {
  53. NV_MMIO_BAR = 5,
  54. NV_PORTS = 2,
  55. NV_PIO_MASK = ATA_PIO4,
  56. NV_MWDMA_MASK = ATA_MWDMA2,
  57. NV_UDMA_MASK = ATA_UDMA6,
  58. NV_PORT0_SCR_REG_OFFSET = 0x00,
  59. NV_PORT1_SCR_REG_OFFSET = 0x40,
  60. /* INT_STATUS/ENABLE */
  61. NV_INT_STATUS = 0x10,
  62. NV_INT_ENABLE = 0x11,
  63. NV_INT_STATUS_CK804 = 0x440,
  64. NV_INT_ENABLE_CK804 = 0x441,
  65. /* INT_STATUS/ENABLE bits */
  66. NV_INT_DEV = 0x01,
  67. NV_INT_PM = 0x02,
  68. NV_INT_ADDED = 0x04,
  69. NV_INT_REMOVED = 0x08,
  70. NV_INT_PORT_SHIFT = 4, /* each port occupies 4 bits */
  71. NV_INT_ALL = 0x0f,
  72. NV_INT_MASK = NV_INT_DEV |
  73. NV_INT_ADDED | NV_INT_REMOVED,
  74. /* INT_CONFIG */
  75. NV_INT_CONFIG = 0x12,
  76. NV_INT_CONFIG_METHD = 0x01, // 0 = INT, 1 = SMI
  77. // For PCI config register 20
  78. NV_MCP_SATA_CFG_20 = 0x50,
  79. NV_MCP_SATA_CFG_20_SATA_SPACE_EN = 0x04,
  80. NV_MCP_SATA_CFG_20_PORT0_EN = (1 << 17),
  81. NV_MCP_SATA_CFG_20_PORT1_EN = (1 << 16),
  82. NV_MCP_SATA_CFG_20_PORT0_PWB_EN = (1 << 14),
  83. NV_MCP_SATA_CFG_20_PORT1_PWB_EN = (1 << 12),
  84. NV_ADMA_MAX_CPBS = 32,
  85. NV_ADMA_CPB_SZ = 128,
  86. NV_ADMA_APRD_SZ = 16,
  87. NV_ADMA_SGTBL_LEN = (1024 - NV_ADMA_CPB_SZ) /
  88. NV_ADMA_APRD_SZ,
  89. NV_ADMA_SGTBL_TOTAL_LEN = NV_ADMA_SGTBL_LEN + 5,
  90. NV_ADMA_SGTBL_SZ = NV_ADMA_SGTBL_LEN * NV_ADMA_APRD_SZ,
  91. NV_ADMA_PORT_PRIV_DMA_SZ = NV_ADMA_MAX_CPBS *
  92. (NV_ADMA_CPB_SZ + NV_ADMA_SGTBL_SZ),
  93. /* BAR5 offset to ADMA general registers */
  94. NV_ADMA_GEN = 0x400,
  95. NV_ADMA_GEN_CTL = 0x00,
  96. NV_ADMA_NOTIFIER_CLEAR = 0x30,
  97. /* BAR5 offset to ADMA ports */
  98. NV_ADMA_PORT = 0x480,
  99. /* size of ADMA port register space */
  100. NV_ADMA_PORT_SIZE = 0x100,
  101. /* ADMA port registers */
  102. NV_ADMA_CTL = 0x40,
  103. NV_ADMA_CPB_COUNT = 0x42,
  104. NV_ADMA_NEXT_CPB_IDX = 0x43,
  105. NV_ADMA_STAT = 0x44,
  106. NV_ADMA_CPB_BASE_LOW = 0x48,
  107. NV_ADMA_CPB_BASE_HIGH = 0x4C,
  108. NV_ADMA_APPEND = 0x50,
  109. NV_ADMA_NOTIFIER = 0x68,
  110. NV_ADMA_NOTIFIER_ERROR = 0x6C,
  111. /* NV_ADMA_CTL register bits */
  112. NV_ADMA_CTL_HOTPLUG_IEN = (1 << 0),
  113. NV_ADMA_CTL_CHANNEL_RESET = (1 << 5),
  114. NV_ADMA_CTL_GO = (1 << 7),
  115. NV_ADMA_CTL_AIEN = (1 << 8),
  116. NV_ADMA_CTL_READ_NON_COHERENT = (1 << 11),
  117. NV_ADMA_CTL_WRITE_NON_COHERENT = (1 << 12),
  118. /* CPB response flag bits */
  119. NV_CPB_RESP_DONE = (1 << 0),
  120. NV_CPB_RESP_ATA_ERR = (1 << 3),
  121. NV_CPB_RESP_CMD_ERR = (1 << 4),
  122. NV_CPB_RESP_CPB_ERR = (1 << 7),
  123. /* CPB control flag bits */
  124. NV_CPB_CTL_CPB_VALID = (1 << 0),
  125. NV_CPB_CTL_QUEUE = (1 << 1),
  126. NV_CPB_CTL_APRD_VALID = (1 << 2),
  127. NV_CPB_CTL_IEN = (1 << 3),
  128. NV_CPB_CTL_FPDMA = (1 << 4),
  129. /* APRD flags */
  130. NV_APRD_WRITE = (1 << 1),
  131. NV_APRD_END = (1 << 2),
  132. NV_APRD_CONT = (1 << 3),
  133. /* NV_ADMA_STAT flags */
  134. NV_ADMA_STAT_TIMEOUT = (1 << 0),
  135. NV_ADMA_STAT_HOTUNPLUG = (1 << 1),
  136. NV_ADMA_STAT_HOTPLUG = (1 << 2),
  137. NV_ADMA_STAT_CPBERR = (1 << 4),
  138. NV_ADMA_STAT_SERROR = (1 << 5),
  139. NV_ADMA_STAT_CMD_COMPLETE = (1 << 6),
  140. NV_ADMA_STAT_IDLE = (1 << 8),
  141. NV_ADMA_STAT_LEGACY = (1 << 9),
  142. NV_ADMA_STAT_STOPPED = (1 << 10),
  143. NV_ADMA_STAT_DONE = (1 << 12),
  144. NV_ADMA_STAT_ERR = NV_ADMA_STAT_CPBERR |
  145. NV_ADMA_STAT_TIMEOUT,
  146. /* port flags */
  147. NV_ADMA_PORT_REGISTER_MODE = (1 << 0),
  148. NV_ADMA_ATAPI_SETUP_COMPLETE = (1 << 1),
  149. /* MCP55 reg offset */
  150. NV_CTL_MCP55 = 0x400,
  151. NV_INT_STATUS_MCP55 = 0x440,
  152. NV_INT_ENABLE_MCP55 = 0x444,
  153. NV_NCQ_REG_MCP55 = 0x448,
  154. /* MCP55 */
  155. NV_INT_ALL_MCP55 = 0xffff,
  156. NV_INT_PORT_SHIFT_MCP55 = 16, /* each port occupies 16 bits */
  157. NV_INT_MASK_MCP55 = NV_INT_ALL_MCP55 & 0xfffd,
  158. /* SWNCQ ENABLE BITS*/
  159. NV_CTL_PRI_SWNCQ = 0x02,
  160. NV_CTL_SEC_SWNCQ = 0x04,
  161. /* SW NCQ status bits*/
  162. NV_SWNCQ_IRQ_DEV = (1 << 0),
  163. NV_SWNCQ_IRQ_PM = (1 << 1),
  164. NV_SWNCQ_IRQ_ADDED = (1 << 2),
  165. NV_SWNCQ_IRQ_REMOVED = (1 << 3),
  166. NV_SWNCQ_IRQ_BACKOUT = (1 << 4),
  167. NV_SWNCQ_IRQ_SDBFIS = (1 << 5),
  168. NV_SWNCQ_IRQ_DHREGFIS = (1 << 6),
  169. NV_SWNCQ_IRQ_DMASETUP = (1 << 7),
  170. NV_SWNCQ_IRQ_HOTPLUG = NV_SWNCQ_IRQ_ADDED |
  171. NV_SWNCQ_IRQ_REMOVED,
  172. };
  173. /* ADMA Physical Region Descriptor - one SG segment */
  174. struct nv_adma_prd {
  175. __le64 addr;
  176. __le32 len;
  177. u8 flags;
  178. u8 packet_len;
  179. __le16 reserved;
  180. };
  181. enum nv_adma_regbits {
  182. CMDEND = (1 << 15), /* end of command list */
  183. WNB = (1 << 14), /* wait-not-BSY */
  184. IGN = (1 << 13), /* ignore this entry */
  185. CS1n = (1 << (4 + 8)), /* std. PATA signals follow... */
  186. DA2 = (1 << (2 + 8)),
  187. DA1 = (1 << (1 + 8)),
  188. DA0 = (1 << (0 + 8)),
  189. };
  190. /* ADMA Command Parameter Block
  191. The first 5 SG segments are stored inside the Command Parameter Block itself.
  192. If there are more than 5 segments the remainder are stored in a separate
  193. memory area indicated by next_aprd. */
  194. struct nv_adma_cpb {
  195. u8 resp_flags; /* 0 */
  196. u8 reserved1; /* 1 */
  197. u8 ctl_flags; /* 2 */
  198. /* len is length of taskfile in 64 bit words */
  199. u8 len; /* 3 */
  200. u8 tag; /* 4 */
  201. u8 next_cpb_idx; /* 5 */
  202. __le16 reserved2; /* 6-7 */
  203. __le16 tf[12]; /* 8-31 */
  204. struct nv_adma_prd aprd[5]; /* 32-111 */
  205. __le64 next_aprd; /* 112-119 */
  206. __le64 reserved3; /* 120-127 */
  207. };
  208. struct nv_adma_port_priv {
  209. struct nv_adma_cpb *cpb;
  210. dma_addr_t cpb_dma;
  211. struct nv_adma_prd *aprd;
  212. dma_addr_t aprd_dma;
  213. void __iomem *ctl_block;
  214. void __iomem *gen_block;
  215. void __iomem *notifier_clear_block;
  216. u64 adma_dma_mask;
  217. u8 flags;
  218. int last_issue_ncq;
  219. };
  220. struct nv_host_priv {
  221. unsigned long type;
  222. };
  223. struct defer_queue {
  224. u32 defer_bits;
  225. unsigned int head;
  226. unsigned int tail;
  227. unsigned int tag[ATA_MAX_QUEUE];
  228. };
  229. enum ncq_saw_flag_list {
  230. ncq_saw_d2h = (1U << 0),
  231. ncq_saw_dmas = (1U << 1),
  232. ncq_saw_sdb = (1U << 2),
  233. ncq_saw_backout = (1U << 3),
  234. };
  235. struct nv_swncq_port_priv {
  236. struct ata_bmdma_prd *prd; /* our SG list */
  237. dma_addr_t prd_dma; /* and its DMA mapping */
  238. void __iomem *sactive_block;
  239. void __iomem *irq_block;
  240. void __iomem *tag_block;
  241. u32 qc_active;
  242. unsigned int last_issue_tag;
  243. /* fifo circular queue to store deferral command */
  244. struct defer_queue defer_queue;
  245. /* for NCQ interrupt analysis */
  246. u32 dhfis_bits;
  247. u32 dmafis_bits;
  248. u32 sdbfis_bits;
  249. unsigned int ncq_flags;
  250. };
  251. #define NV_ADMA_CHECK_INTR(GCTL, PORT) ((GCTL) & (1 << (19 + (12 * (PORT)))))
  252. static int nv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
  253. #ifdef CONFIG_PM_SLEEP
  254. static int nv_pci_device_resume(struct pci_dev *pdev);
  255. #endif
  256. static void nv_ck804_host_stop(struct ata_host *host);
  257. static irqreturn_t nv_generic_interrupt(int irq, void *dev_instance);
  258. static irqreturn_t nv_nf2_interrupt(int irq, void *dev_instance);
  259. static irqreturn_t nv_ck804_interrupt(int irq, void *dev_instance);
  260. static int nv_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val);
  261. static int nv_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val);
  262. static int nv_hardreset(struct ata_link *link, unsigned int *class,
  263. unsigned long deadline);
  264. static void nv_nf2_freeze(struct ata_port *ap);
  265. static void nv_nf2_thaw(struct ata_port *ap);
  266. static void nv_ck804_freeze(struct ata_port *ap);
  267. static void nv_ck804_thaw(struct ata_port *ap);
  268. static int nv_adma_slave_config(struct scsi_device *sdev);
  269. static int nv_adma_check_atapi_dma(struct ata_queued_cmd *qc);
  270. static enum ata_completion_errors nv_adma_qc_prep(struct ata_queued_cmd *qc);
  271. static unsigned int nv_adma_qc_issue(struct ata_queued_cmd *qc);
  272. static irqreturn_t nv_adma_interrupt(int irq, void *dev_instance);
  273. static void nv_adma_irq_clear(struct ata_port *ap);
  274. static int nv_adma_port_start(struct ata_port *ap);
  275. static void nv_adma_port_stop(struct ata_port *ap);
  276. #ifdef CONFIG_PM
  277. static int nv_adma_port_suspend(struct ata_port *ap, pm_message_t mesg);
  278. static int nv_adma_port_resume(struct ata_port *ap);
  279. #endif
  280. static void nv_adma_freeze(struct ata_port *ap);
  281. static void nv_adma_thaw(struct ata_port *ap);
  282. static void nv_adma_error_handler(struct ata_port *ap);
  283. static void nv_adma_host_stop(struct ata_host *host);
  284. static void nv_adma_post_internal_cmd(struct ata_queued_cmd *qc);
  285. static void nv_adma_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
  286. static void nv_mcp55_thaw(struct ata_port *ap);
  287. static void nv_mcp55_freeze(struct ata_port *ap);
  288. static void nv_swncq_error_handler(struct ata_port *ap);
  289. static int nv_swncq_slave_config(struct scsi_device *sdev);
  290. static int nv_swncq_port_start(struct ata_port *ap);
  291. static enum ata_completion_errors nv_swncq_qc_prep(struct ata_queued_cmd *qc);
  292. static void nv_swncq_fill_sg(struct ata_queued_cmd *qc);
  293. static unsigned int nv_swncq_qc_issue(struct ata_queued_cmd *qc);
  294. static void nv_swncq_irq_clear(struct ata_port *ap, u16 fis);
  295. static irqreturn_t nv_swncq_interrupt(int irq, void *dev_instance);
  296. #ifdef CONFIG_PM
  297. static int nv_swncq_port_suspend(struct ata_port *ap, pm_message_t mesg);
  298. static int nv_swncq_port_resume(struct ata_port *ap);
  299. #endif
  300. enum nv_host_type
  301. {
  302. GENERIC,
  303. NFORCE2,
  304. NFORCE3 = NFORCE2, /* NF2 == NF3 as far as sata_nv is concerned */
  305. CK804,
  306. ADMA,
  307. MCP5x,
  308. SWNCQ,
  309. };
  310. static const struct pci_device_id nv_pci_tbl[] = {
  311. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE2S_SATA), NFORCE2 },
  312. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA), NFORCE3 },
  313. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA2), NFORCE3 },
  314. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_SATA), CK804 },
  315. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_SATA2), CK804 },
  316. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SATA), CK804 },
  317. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SATA2), CK804 },
  318. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA), MCP5x },
  319. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA2), MCP5x },
  320. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA), MCP5x },
  321. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA2), MCP5x },
  322. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA), GENERIC },
  323. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA2), GENERIC },
  324. { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA3), GENERIC },
  325. { } /* terminate list */
  326. };
  327. static struct pci_driver nv_pci_driver = {
  328. .name = DRV_NAME,
  329. .id_table = nv_pci_tbl,
  330. .probe = nv_init_one,
  331. #ifdef CONFIG_PM_SLEEP
  332. .suspend = ata_pci_device_suspend,
  333. .resume = nv_pci_device_resume,
  334. #endif
  335. .remove = ata_pci_remove_one,
  336. };
  337. static struct scsi_host_template nv_sht = {
  338. ATA_BMDMA_SHT(DRV_NAME),
  339. };
  340. static struct scsi_host_template nv_adma_sht = {
  341. ATA_NCQ_SHT(DRV_NAME),
  342. .can_queue = NV_ADMA_MAX_CPBS,
  343. .sg_tablesize = NV_ADMA_SGTBL_TOTAL_LEN,
  344. .dma_boundary = NV_ADMA_DMA_BOUNDARY,
  345. .slave_configure = nv_adma_slave_config,
  346. };
  347. static struct scsi_host_template nv_swncq_sht = {
  348. ATA_NCQ_SHT(DRV_NAME),
  349. .can_queue = ATA_MAX_QUEUE,
  350. .sg_tablesize = LIBATA_MAX_PRD,
  351. .dma_boundary = ATA_DMA_BOUNDARY,
  352. .slave_configure = nv_swncq_slave_config,
  353. };
  354. /*
  355. * NV SATA controllers have various different problems with hardreset
  356. * protocol depending on the specific controller and device.
  357. *
  358. * GENERIC:
  359. *
  360. * bko11195 reports that link doesn't come online after hardreset on
  361. * generic nv's and there have been several other similar reports on
  362. * linux-ide.
  363. *
  364. * bko12351#c23 reports that warmplug on MCP61 doesn't work with
  365. * softreset.
  366. *
  367. * NF2/3:
  368. *
  369. * bko3352 reports nf2/3 controllers can't determine device signature
  370. * reliably after hardreset. The following thread reports detection
  371. * failure on cold boot with the standard debouncing timing.
  372. *
  373. * http://thread.gmane.org/gmane.linux.ide/34098
  374. *
  375. * bko12176 reports that hardreset fails to bring up the link during
  376. * boot on nf2.
  377. *
  378. * CK804:
  379. *
  380. * For initial probing after boot and hot plugging, hardreset mostly
  381. * works fine on CK804 but curiously, reprobing on the initial port
  382. * by rescanning or rmmod/insmod fails to acquire the initial D2H Reg
  383. * FIS in somewhat undeterministic way.
  384. *
  385. * SWNCQ:
  386. *
  387. * bko12351 reports that when SWNCQ is enabled, for hotplug to work,
  388. * hardreset should be used and hardreset can't report proper
  389. * signature, which suggests that mcp5x is closer to nf2 as long as
  390. * reset quirkiness is concerned.
  391. *
  392. * bko12703 reports that boot probing fails for intel SSD with
  393. * hardreset. Link fails to come online. Softreset works fine.
  394. *
  395. * The failures are varied but the following patterns seem true for
  396. * all flavors.
  397. *
  398. * - Softreset during boot always works.
  399. *
  400. * - Hardreset during boot sometimes fails to bring up the link on
  401. * certain comibnations and device signature acquisition is
  402. * unreliable.
  403. *
  404. * - Hardreset is often necessary after hotplug.
  405. *
  406. * So, preferring softreset for boot probing and error handling (as
  407. * hardreset might bring down the link) but using hardreset for
  408. * post-boot probing should work around the above issues in most
  409. * cases. Define nv_hardreset() which only kicks in for post-boot
  410. * probing and use it for all variants.
  411. */
  412. static struct ata_port_operations nv_generic_ops = {
  413. .inherits = &ata_bmdma_port_ops,
  414. .lost_interrupt = ATA_OP_NULL,
  415. .scr_read = nv_scr_read,
  416. .scr_write = nv_scr_write,
  417. .hardreset = nv_hardreset,
  418. };
  419. static struct ata_port_operations nv_nf2_ops = {
  420. .inherits = &nv_generic_ops,
  421. .freeze = nv_nf2_freeze,
  422. .thaw = nv_nf2_thaw,
  423. };
  424. static struct ata_port_operations nv_ck804_ops = {
  425. .inherits = &nv_generic_ops,
  426. .freeze = nv_ck804_freeze,
  427. .thaw = nv_ck804_thaw,
  428. .host_stop = nv_ck804_host_stop,
  429. };
  430. static struct ata_port_operations nv_adma_ops = {
  431. .inherits = &nv_ck804_ops,
  432. .check_atapi_dma = nv_adma_check_atapi_dma,
  433. .sff_tf_read = nv_adma_tf_read,
  434. .qc_defer = ata_std_qc_defer,
  435. .qc_prep = nv_adma_qc_prep,
  436. .qc_issue = nv_adma_qc_issue,
  437. .sff_irq_clear = nv_adma_irq_clear,
  438. .freeze = nv_adma_freeze,
  439. .thaw = nv_adma_thaw,
  440. .error_handler = nv_adma_error_handler,
  441. .post_internal_cmd = nv_adma_post_internal_cmd,
  442. .port_start = nv_adma_port_start,
  443. .port_stop = nv_adma_port_stop,
  444. #ifdef CONFIG_PM
  445. .port_suspend = nv_adma_port_suspend,
  446. .port_resume = nv_adma_port_resume,
  447. #endif
  448. .host_stop = nv_adma_host_stop,
  449. };
  450. static struct ata_port_operations nv_swncq_ops = {
  451. .inherits = &nv_generic_ops,
  452. .qc_defer = ata_std_qc_defer,
  453. .qc_prep = nv_swncq_qc_prep,
  454. .qc_issue = nv_swncq_qc_issue,
  455. .freeze = nv_mcp55_freeze,
  456. .thaw = nv_mcp55_thaw,
  457. .error_handler = nv_swncq_error_handler,
  458. #ifdef CONFIG_PM
  459. .port_suspend = nv_swncq_port_suspend,
  460. .port_resume = nv_swncq_port_resume,
  461. #endif
  462. .port_start = nv_swncq_port_start,
  463. };
  464. struct nv_pi_priv {
  465. irq_handler_t irq_handler;
  466. struct scsi_host_template *sht;
  467. };
  468. #define NV_PI_PRIV(_irq_handler, _sht) \
  469. &(struct nv_pi_priv){ .irq_handler = _irq_handler, .sht = _sht }
  470. static const struct ata_port_info nv_port_info[] = {
  471. /* generic */
  472. {
  473. .flags = ATA_FLAG_SATA,
  474. .pio_mask = NV_PIO_MASK,
  475. .mwdma_mask = NV_MWDMA_MASK,
  476. .udma_mask = NV_UDMA_MASK,
  477. .port_ops = &nv_generic_ops,
  478. .private_data = NV_PI_PRIV(nv_generic_interrupt, &nv_sht),
  479. },
  480. /* nforce2/3 */
  481. {
  482. .flags = ATA_FLAG_SATA,
  483. .pio_mask = NV_PIO_MASK,
  484. .mwdma_mask = NV_MWDMA_MASK,
  485. .udma_mask = NV_UDMA_MASK,
  486. .port_ops = &nv_nf2_ops,
  487. .private_data = NV_PI_PRIV(nv_nf2_interrupt, &nv_sht),
  488. },
  489. /* ck804 */
  490. {
  491. .flags = ATA_FLAG_SATA,
  492. .pio_mask = NV_PIO_MASK,
  493. .mwdma_mask = NV_MWDMA_MASK,
  494. .udma_mask = NV_UDMA_MASK,
  495. .port_ops = &nv_ck804_ops,
  496. .private_data = NV_PI_PRIV(nv_ck804_interrupt, &nv_sht),
  497. },
  498. /* ADMA */
  499. {
  500. .flags = ATA_FLAG_SATA | ATA_FLAG_NCQ,
  501. .pio_mask = NV_PIO_MASK,
  502. .mwdma_mask = NV_MWDMA_MASK,
  503. .udma_mask = NV_UDMA_MASK,
  504. .port_ops = &nv_adma_ops,
  505. .private_data = NV_PI_PRIV(nv_adma_interrupt, &nv_adma_sht),
  506. },
  507. /* MCP5x */
  508. {
  509. .flags = ATA_FLAG_SATA,
  510. .pio_mask = NV_PIO_MASK,
  511. .mwdma_mask = NV_MWDMA_MASK,
  512. .udma_mask = NV_UDMA_MASK,
  513. .port_ops = &nv_generic_ops,
  514. .private_data = NV_PI_PRIV(nv_generic_interrupt, &nv_sht),
  515. },
  516. /* SWNCQ */
  517. {
  518. .flags = ATA_FLAG_SATA | ATA_FLAG_NCQ,
  519. .pio_mask = NV_PIO_MASK,
  520. .mwdma_mask = NV_MWDMA_MASK,
  521. .udma_mask = NV_UDMA_MASK,
  522. .port_ops = &nv_swncq_ops,
  523. .private_data = NV_PI_PRIV(nv_swncq_interrupt, &nv_swncq_sht),
  524. },
  525. };
  526. MODULE_AUTHOR("NVIDIA");
  527. MODULE_DESCRIPTION("low-level driver for NVIDIA nForce SATA controller");
  528. MODULE_LICENSE("GPL");
  529. MODULE_DEVICE_TABLE(pci, nv_pci_tbl);
  530. MODULE_VERSION(DRV_VERSION);
  531. static bool adma_enabled;
  532. static bool swncq_enabled = true;
  533. static bool msi_enabled;
  534. static void nv_adma_register_mode(struct ata_port *ap)
  535. {
  536. struct nv_adma_port_priv *pp = ap->private_data;
  537. void __iomem *mmio = pp->ctl_block;
  538. u16 tmp, status;
  539. int count = 0;
  540. if (pp->flags & NV_ADMA_PORT_REGISTER_MODE)
  541. return;
  542. status = readw(mmio + NV_ADMA_STAT);
  543. while (!(status & NV_ADMA_STAT_IDLE) && count < 20) {
  544. ndelay(50);
  545. status = readw(mmio + NV_ADMA_STAT);
  546. count++;
  547. }
  548. if (count == 20)
  549. ata_port_warn(ap, "timeout waiting for ADMA IDLE, stat=0x%hx\n",
  550. status);
  551. tmp = readw(mmio + NV_ADMA_CTL);
  552. writew(tmp & ~NV_ADMA_CTL_GO, mmio + NV_ADMA_CTL);
  553. count = 0;
  554. status = readw(mmio + NV_ADMA_STAT);
  555. while (!(status & NV_ADMA_STAT_LEGACY) && count < 20) {
  556. ndelay(50);
  557. status = readw(mmio + NV_ADMA_STAT);
  558. count++;
  559. }
  560. if (count == 20)
  561. ata_port_warn(ap,
  562. "timeout waiting for ADMA LEGACY, stat=0x%hx\n",
  563. status);
  564. pp->flags |= NV_ADMA_PORT_REGISTER_MODE;
  565. }
  566. static void nv_adma_mode(struct ata_port *ap)
  567. {
  568. struct nv_adma_port_priv *pp = ap->private_data;
  569. void __iomem *mmio = pp->ctl_block;
  570. u16 tmp, status;
  571. int count = 0;
  572. if (!(pp->flags & NV_ADMA_PORT_REGISTER_MODE))
  573. return;
  574. WARN_ON(pp->flags & NV_ADMA_ATAPI_SETUP_COMPLETE);
  575. tmp = readw(mmio + NV_ADMA_CTL);
  576. writew(tmp | NV_ADMA_CTL_GO, mmio + NV_ADMA_CTL);
  577. status = readw(mmio + NV_ADMA_STAT);
  578. while (((status & NV_ADMA_STAT_LEGACY) ||
  579. !(status & NV_ADMA_STAT_IDLE)) && count < 20) {
  580. ndelay(50);
  581. status = readw(mmio + NV_ADMA_STAT);
  582. count++;
  583. }
  584. if (count == 20)
  585. ata_port_warn(ap,
  586. "timeout waiting for ADMA LEGACY clear and IDLE, stat=0x%hx\n",
  587. status);
  588. pp->flags &= ~NV_ADMA_PORT_REGISTER_MODE;
  589. }
  590. static int nv_adma_slave_config(struct scsi_device *sdev)
  591. {
  592. struct ata_port *ap = ata_shost_to_port(sdev->host);
  593. struct nv_adma_port_priv *pp = ap->private_data;
  594. struct nv_adma_port_priv *port0, *port1;
  595. struct scsi_device *sdev0, *sdev1;
  596. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  597. unsigned long segment_boundary, flags;
  598. unsigned short sg_tablesize;
  599. int rc;
  600. int adma_enable;
  601. u32 current_reg, new_reg, config_mask;
  602. rc = ata_scsi_slave_config(sdev);
  603. if (sdev->id >= ATA_MAX_DEVICES || sdev->channel || sdev->lun)
  604. /* Not a proper libata device, ignore */
  605. return rc;
  606. spin_lock_irqsave(ap->lock, flags);
  607. if (ap->link.device[sdev->id].class == ATA_DEV_ATAPI) {
  608. /*
  609. * NVIDIA reports that ADMA mode does not support ATAPI commands.
  610. * Therefore ATAPI commands are sent through the legacy interface.
  611. * However, the legacy interface only supports 32-bit DMA.
  612. * Restrict DMA parameters as required by the legacy interface
  613. * when an ATAPI device is connected.
  614. */
  615. segment_boundary = ATA_DMA_BOUNDARY;
  616. /* Subtract 1 since an extra entry may be needed for padding, see
  617. libata-scsi.c */
  618. sg_tablesize = LIBATA_MAX_PRD - 1;
  619. /* Since the legacy DMA engine is in use, we need to disable ADMA
  620. on the port. */
  621. adma_enable = 0;
  622. nv_adma_register_mode(ap);
  623. } else {
  624. segment_boundary = NV_ADMA_DMA_BOUNDARY;
  625. sg_tablesize = NV_ADMA_SGTBL_TOTAL_LEN;
  626. adma_enable = 1;
  627. }
  628. pci_read_config_dword(pdev, NV_MCP_SATA_CFG_20, &current_reg);
  629. if (ap->port_no == 1)
  630. config_mask = NV_MCP_SATA_CFG_20_PORT1_EN |
  631. NV_MCP_SATA_CFG_20_PORT1_PWB_EN;
  632. else
  633. config_mask = NV_MCP_SATA_CFG_20_PORT0_EN |
  634. NV_MCP_SATA_CFG_20_PORT0_PWB_EN;
  635. if (adma_enable) {
  636. new_reg = current_reg | config_mask;
  637. pp->flags &= ~NV_ADMA_ATAPI_SETUP_COMPLETE;
  638. } else {
  639. new_reg = current_reg & ~config_mask;
  640. pp->flags |= NV_ADMA_ATAPI_SETUP_COMPLETE;
  641. }
  642. if (current_reg != new_reg)
  643. pci_write_config_dword(pdev, NV_MCP_SATA_CFG_20, new_reg);
  644. port0 = ap->host->ports[0]->private_data;
  645. port1 = ap->host->ports[1]->private_data;
  646. sdev0 = ap->host->ports[0]->link.device[0].sdev;
  647. sdev1 = ap->host->ports[1]->link.device[0].sdev;
  648. if ((port0->flags & NV_ADMA_ATAPI_SETUP_COMPLETE) ||
  649. (port1->flags & NV_ADMA_ATAPI_SETUP_COMPLETE)) {
  650. /** We have to set the DMA mask to 32-bit if either port is in
  651. ATAPI mode, since they are on the same PCI device which is
  652. used for DMA mapping. If we set the mask we also need to set
  653. the bounce limit on both ports to ensure that the block
  654. layer doesn't feed addresses that cause DMA mapping to
  655. choke. If either SCSI device is not allocated yet, it's OK
  656. since that port will discover its correct setting when it
  657. does get allocated.
  658. Note: Setting 32-bit mask should not fail. */
  659. if (sdev0)
  660. blk_queue_bounce_limit(sdev0->request_queue,
  661. ATA_DMA_MASK);
  662. if (sdev1)
  663. blk_queue_bounce_limit(sdev1->request_queue,
  664. ATA_DMA_MASK);
  665. dma_set_mask(&pdev->dev, ATA_DMA_MASK);
  666. } else {
  667. /** This shouldn't fail as it was set to this value before */
  668. dma_set_mask(&pdev->dev, pp->adma_dma_mask);
  669. if (sdev0)
  670. blk_queue_bounce_limit(sdev0->request_queue,
  671. pp->adma_dma_mask);
  672. if (sdev1)
  673. blk_queue_bounce_limit(sdev1->request_queue,
  674. pp->adma_dma_mask);
  675. }
  676. blk_queue_segment_boundary(sdev->request_queue, segment_boundary);
  677. blk_queue_max_segments(sdev->request_queue, sg_tablesize);
  678. ata_port_info(ap,
  679. "DMA mask 0x%llX, segment boundary 0x%lX, hw segs %hu\n",
  680. (unsigned long long)*ap->host->dev->dma_mask,
  681. segment_boundary, sg_tablesize);
  682. spin_unlock_irqrestore(ap->lock, flags);
  683. return rc;
  684. }
  685. static int nv_adma_check_atapi_dma(struct ata_queued_cmd *qc)
  686. {
  687. struct nv_adma_port_priv *pp = qc->ap->private_data;
  688. return !(pp->flags & NV_ADMA_ATAPI_SETUP_COMPLETE);
  689. }
  690. static void nv_adma_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  691. {
  692. /* Other than when internal or pass-through commands are executed,
  693. the only time this function will be called in ADMA mode will be
  694. if a command fails. In the failure case we don't care about going
  695. into register mode with ADMA commands pending, as the commands will
  696. all shortly be aborted anyway. We assume that NCQ commands are not
  697. issued via passthrough, which is the only way that switching into
  698. ADMA mode could abort outstanding commands. */
  699. nv_adma_register_mode(ap);
  700. ata_sff_tf_read(ap, tf);
  701. }
  702. static unsigned int nv_adma_tf_to_cpb(struct ata_taskfile *tf, __le16 *cpb)
  703. {
  704. unsigned int idx = 0;
  705. if (tf->flags & ATA_TFLAG_ISADDR) {
  706. if (tf->flags & ATA_TFLAG_LBA48) {
  707. cpb[idx++] = cpu_to_le16((ATA_REG_ERR << 8) | tf->hob_feature | WNB);
  708. cpb[idx++] = cpu_to_le16((ATA_REG_NSECT << 8) | tf->hob_nsect);
  709. cpb[idx++] = cpu_to_le16((ATA_REG_LBAL << 8) | tf->hob_lbal);
  710. cpb[idx++] = cpu_to_le16((ATA_REG_LBAM << 8) | tf->hob_lbam);
  711. cpb[idx++] = cpu_to_le16((ATA_REG_LBAH << 8) | tf->hob_lbah);
  712. cpb[idx++] = cpu_to_le16((ATA_REG_ERR << 8) | tf->feature);
  713. } else
  714. cpb[idx++] = cpu_to_le16((ATA_REG_ERR << 8) | tf->feature | WNB);
  715. cpb[idx++] = cpu_to_le16((ATA_REG_NSECT << 8) | tf->nsect);
  716. cpb[idx++] = cpu_to_le16((ATA_REG_LBAL << 8) | tf->lbal);
  717. cpb[idx++] = cpu_to_le16((ATA_REG_LBAM << 8) | tf->lbam);
  718. cpb[idx++] = cpu_to_le16((ATA_REG_LBAH << 8) | tf->lbah);
  719. }
  720. if (tf->flags & ATA_TFLAG_DEVICE)
  721. cpb[idx++] = cpu_to_le16((ATA_REG_DEVICE << 8) | tf->device);
  722. cpb[idx++] = cpu_to_le16((ATA_REG_CMD << 8) | tf->command | CMDEND);
  723. while (idx < 12)
  724. cpb[idx++] = cpu_to_le16(IGN);
  725. return idx;
  726. }
  727. static int nv_adma_check_cpb(struct ata_port *ap, int cpb_num, int force_err)
  728. {
  729. struct nv_adma_port_priv *pp = ap->private_data;
  730. u8 flags = pp->cpb[cpb_num].resp_flags;
  731. VPRINTK("CPB %d, flags=0x%x\n", cpb_num, flags);
  732. if (unlikely((force_err ||
  733. flags & (NV_CPB_RESP_ATA_ERR |
  734. NV_CPB_RESP_CMD_ERR |
  735. NV_CPB_RESP_CPB_ERR)))) {
  736. struct ata_eh_info *ehi = &ap->link.eh_info;
  737. int freeze = 0;
  738. ata_ehi_clear_desc(ehi);
  739. __ata_ehi_push_desc(ehi, "CPB resp_flags 0x%x: ", flags);
  740. if (flags & NV_CPB_RESP_ATA_ERR) {
  741. ata_ehi_push_desc(ehi, "ATA error");
  742. ehi->err_mask |= AC_ERR_DEV;
  743. } else if (flags & NV_CPB_RESP_CMD_ERR) {
  744. ata_ehi_push_desc(ehi, "CMD error");
  745. ehi->err_mask |= AC_ERR_DEV;
  746. } else if (flags & NV_CPB_RESP_CPB_ERR) {
  747. ata_ehi_push_desc(ehi, "CPB error");
  748. ehi->err_mask |= AC_ERR_SYSTEM;
  749. freeze = 1;
  750. } else {
  751. /* notifier error, but no error in CPB flags? */
  752. ata_ehi_push_desc(ehi, "unknown");
  753. ehi->err_mask |= AC_ERR_OTHER;
  754. freeze = 1;
  755. }
  756. /* Kill all commands. EH will determine what actually failed. */
  757. if (freeze)
  758. ata_port_freeze(ap);
  759. else
  760. ata_port_abort(ap);
  761. return -1;
  762. }
  763. if (likely(flags & NV_CPB_RESP_DONE))
  764. return 1;
  765. return 0;
  766. }
  767. static int nv_host_intr(struct ata_port *ap, u8 irq_stat)
  768. {
  769. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->link.active_tag);
  770. /* freeze if hotplugged */
  771. if (unlikely(irq_stat & (NV_INT_ADDED | NV_INT_REMOVED))) {
  772. ata_port_freeze(ap);
  773. return 1;
  774. }
  775. /* bail out if not our interrupt */
  776. if (!(irq_stat & NV_INT_DEV))
  777. return 0;
  778. /* DEV interrupt w/ no active qc? */
  779. if (unlikely(!qc || (qc->tf.flags & ATA_TFLAG_POLLING))) {
  780. ata_sff_check_status(ap);
  781. return 1;
  782. }
  783. /* handle interrupt */
  784. return ata_bmdma_port_intr(ap, qc);
  785. }
  786. static irqreturn_t nv_adma_interrupt(int irq, void *dev_instance)
  787. {
  788. struct ata_host *host = dev_instance;
  789. int i, handled = 0;
  790. u32 notifier_clears[2];
  791. spin_lock(&host->lock);
  792. for (i = 0; i < host->n_ports; i++) {
  793. struct ata_port *ap = host->ports[i];
  794. struct nv_adma_port_priv *pp = ap->private_data;
  795. void __iomem *mmio = pp->ctl_block;
  796. u16 status;
  797. u32 gen_ctl;
  798. u32 notifier, notifier_error;
  799. notifier_clears[i] = 0;
  800. /* if ADMA is disabled, use standard ata interrupt handler */
  801. if (pp->flags & NV_ADMA_ATAPI_SETUP_COMPLETE) {
  802. u8 irq_stat = readb(host->iomap[NV_MMIO_BAR] + NV_INT_STATUS_CK804)
  803. >> (NV_INT_PORT_SHIFT * i);
  804. handled += nv_host_intr(ap, irq_stat);
  805. continue;
  806. }
  807. /* if in ATA register mode, check for standard interrupts */
  808. if (pp->flags & NV_ADMA_PORT_REGISTER_MODE) {
  809. u8 irq_stat = readb(host->iomap[NV_MMIO_BAR] + NV_INT_STATUS_CK804)
  810. >> (NV_INT_PORT_SHIFT * i);
  811. if (ata_tag_valid(ap->link.active_tag))
  812. /** NV_INT_DEV indication seems unreliable
  813. at times at least in ADMA mode. Force it
  814. on always when a command is active, to
  815. prevent losing interrupts. */
  816. irq_stat |= NV_INT_DEV;
  817. handled += nv_host_intr(ap, irq_stat);
  818. }
  819. notifier = readl(mmio + NV_ADMA_NOTIFIER);
  820. notifier_error = readl(mmio + NV_ADMA_NOTIFIER_ERROR);
  821. notifier_clears[i] = notifier | notifier_error;
  822. gen_ctl = readl(pp->gen_block + NV_ADMA_GEN_CTL);
  823. if (!NV_ADMA_CHECK_INTR(gen_ctl, ap->port_no) && !notifier &&
  824. !notifier_error)
  825. /* Nothing to do */
  826. continue;
  827. status = readw(mmio + NV_ADMA_STAT);
  828. /*
  829. * Clear status. Ensure the controller sees the
  830. * clearing before we start looking at any of the CPB
  831. * statuses, so that any CPB completions after this
  832. * point in the handler will raise another interrupt.
  833. */
  834. writew(status, mmio + NV_ADMA_STAT);
  835. readw(mmio + NV_ADMA_STAT); /* flush posted write */
  836. rmb();
  837. handled++; /* irq handled if we got here */
  838. /* freeze if hotplugged or controller error */
  839. if (unlikely(status & (NV_ADMA_STAT_HOTPLUG |
  840. NV_ADMA_STAT_HOTUNPLUG |
  841. NV_ADMA_STAT_TIMEOUT |
  842. NV_ADMA_STAT_SERROR))) {
  843. struct ata_eh_info *ehi = &ap->link.eh_info;
  844. ata_ehi_clear_desc(ehi);
  845. __ata_ehi_push_desc(ehi, "ADMA status 0x%08x: ", status);
  846. if (status & NV_ADMA_STAT_TIMEOUT) {
  847. ehi->err_mask |= AC_ERR_SYSTEM;
  848. ata_ehi_push_desc(ehi, "timeout");
  849. } else if (status & NV_ADMA_STAT_HOTPLUG) {
  850. ata_ehi_hotplugged(ehi);
  851. ata_ehi_push_desc(ehi, "hotplug");
  852. } else if (status & NV_ADMA_STAT_HOTUNPLUG) {
  853. ata_ehi_hotplugged(ehi);
  854. ata_ehi_push_desc(ehi, "hot unplug");
  855. } else if (status & NV_ADMA_STAT_SERROR) {
  856. /* let EH analyze SError and figure out cause */
  857. ata_ehi_push_desc(ehi, "SError");
  858. } else
  859. ata_ehi_push_desc(ehi, "unknown");
  860. ata_port_freeze(ap);
  861. continue;
  862. }
  863. if (status & (NV_ADMA_STAT_DONE |
  864. NV_ADMA_STAT_CPBERR |
  865. NV_ADMA_STAT_CMD_COMPLETE)) {
  866. u32 check_commands = notifier_clears[i];
  867. u32 done_mask = 0;
  868. int pos, rc;
  869. if (status & NV_ADMA_STAT_CPBERR) {
  870. /* check all active commands */
  871. if (ata_tag_valid(ap->link.active_tag))
  872. check_commands = 1 <<
  873. ap->link.active_tag;
  874. else
  875. check_commands = ap->link.sactive;
  876. }
  877. /* check CPBs for completed commands */
  878. while ((pos = ffs(check_commands))) {
  879. pos--;
  880. rc = nv_adma_check_cpb(ap, pos,
  881. notifier_error & (1 << pos));
  882. if (rc > 0)
  883. done_mask |= 1 << pos;
  884. else if (unlikely(rc < 0))
  885. check_commands = 0;
  886. check_commands &= ~(1 << pos);
  887. }
  888. ata_qc_complete_multiple(ap, ap->qc_active ^ done_mask);
  889. }
  890. }
  891. if (notifier_clears[0] || notifier_clears[1]) {
  892. /* Note: Both notifier clear registers must be written
  893. if either is set, even if one is zero, according to NVIDIA. */
  894. struct nv_adma_port_priv *pp = host->ports[0]->private_data;
  895. writel(notifier_clears[0], pp->notifier_clear_block);
  896. pp = host->ports[1]->private_data;
  897. writel(notifier_clears[1], pp->notifier_clear_block);
  898. }
  899. spin_unlock(&host->lock);
  900. return IRQ_RETVAL(handled);
  901. }
  902. static void nv_adma_freeze(struct ata_port *ap)
  903. {
  904. struct nv_adma_port_priv *pp = ap->private_data;
  905. void __iomem *mmio = pp->ctl_block;
  906. u16 tmp;
  907. nv_ck804_freeze(ap);
  908. if (pp->flags & NV_ADMA_ATAPI_SETUP_COMPLETE)
  909. return;
  910. /* clear any outstanding CK804 notifications */
  911. writeb(NV_INT_ALL << (ap->port_no * NV_INT_PORT_SHIFT),
  912. ap->host->iomap[NV_MMIO_BAR] + NV_INT_STATUS_CK804);
  913. /* Disable interrupt */
  914. tmp = readw(mmio + NV_ADMA_CTL);
  915. writew(tmp & ~(NV_ADMA_CTL_AIEN | NV_ADMA_CTL_HOTPLUG_IEN),
  916. mmio + NV_ADMA_CTL);
  917. readw(mmio + NV_ADMA_CTL); /* flush posted write */
  918. }
  919. static void nv_adma_thaw(struct ata_port *ap)
  920. {
  921. struct nv_adma_port_priv *pp = ap->private_data;
  922. void __iomem *mmio = pp->ctl_block;
  923. u16 tmp;
  924. nv_ck804_thaw(ap);
  925. if (pp->flags & NV_ADMA_ATAPI_SETUP_COMPLETE)
  926. return;
  927. /* Enable interrupt */
  928. tmp = readw(mmio + NV_ADMA_CTL);
  929. writew(tmp | (NV_ADMA_CTL_AIEN | NV_ADMA_CTL_HOTPLUG_IEN),
  930. mmio + NV_ADMA_CTL);
  931. readw(mmio + NV_ADMA_CTL); /* flush posted write */
  932. }
  933. static void nv_adma_irq_clear(struct ata_port *ap)
  934. {
  935. struct nv_adma_port_priv *pp = ap->private_data;
  936. void __iomem *mmio = pp->ctl_block;
  937. u32 notifier_clears[2];
  938. if (pp->flags & NV_ADMA_ATAPI_SETUP_COMPLETE) {
  939. ata_bmdma_irq_clear(ap);
  940. return;
  941. }
  942. /* clear any outstanding CK804 notifications */
  943. writeb(NV_INT_ALL << (ap->port_no * NV_INT_PORT_SHIFT),
  944. ap->host->iomap[NV_MMIO_BAR] + NV_INT_STATUS_CK804);
  945. /* clear ADMA status */
  946. writew(0xffff, mmio + NV_ADMA_STAT);
  947. /* clear notifiers - note both ports need to be written with
  948. something even though we are only clearing on one */
  949. if (ap->port_no == 0) {
  950. notifier_clears[0] = 0xFFFFFFFF;
  951. notifier_clears[1] = 0;
  952. } else {
  953. notifier_clears[0] = 0;
  954. notifier_clears[1] = 0xFFFFFFFF;
  955. }
  956. pp = ap->host->ports[0]->private_data;
  957. writel(notifier_clears[0], pp->notifier_clear_block);
  958. pp = ap->host->ports[1]->private_data;
  959. writel(notifier_clears[1], pp->notifier_clear_block);
  960. }
  961. static void nv_adma_post_internal_cmd(struct ata_queued_cmd *qc)
  962. {
  963. struct nv_adma_port_priv *pp = qc->ap->private_data;
  964. if (pp->flags & NV_ADMA_PORT_REGISTER_MODE)
  965. ata_bmdma_post_internal_cmd(qc);
  966. }
  967. static int nv_adma_port_start(struct ata_port *ap)
  968. {
  969. struct device *dev = ap->host->dev;
  970. struct nv_adma_port_priv *pp;
  971. int rc;
  972. void *mem;
  973. dma_addr_t mem_dma;
  974. void __iomem *mmio;
  975. struct pci_dev *pdev = to_pci_dev(dev);
  976. u16 tmp;
  977. VPRINTK("ENTER\n");
  978. /* Ensure DMA mask is set to 32-bit before allocating legacy PRD and
  979. pad buffers */
  980. rc = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  981. if (rc)
  982. return rc;
  983. rc = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  984. if (rc)
  985. return rc;
  986. /* we might fallback to bmdma, allocate bmdma resources */
  987. rc = ata_bmdma_port_start(ap);
  988. if (rc)
  989. return rc;
  990. pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
  991. if (!pp)
  992. return -ENOMEM;
  993. mmio = ap->host->iomap[NV_MMIO_BAR] + NV_ADMA_PORT +
  994. ap->port_no * NV_ADMA_PORT_SIZE;
  995. pp->ctl_block = mmio;
  996. pp->gen_block = ap->host->iomap[NV_MMIO_BAR] + NV_ADMA_GEN;
  997. pp->notifier_clear_block = pp->gen_block +
  998. NV_ADMA_NOTIFIER_CLEAR + (4 * ap->port_no);
  999. /* Now that the legacy PRD and padding buffer are allocated we can
  1000. safely raise the DMA mask to allocate the CPB/APRD table.
  1001. These are allowed to fail since we store the value that ends up
  1002. being used to set as the bounce limit in slave_config later if
  1003. needed. */
  1004. dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
  1005. dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
  1006. pp->adma_dma_mask = *dev->dma_mask;
  1007. mem = dmam_alloc_coherent(dev, NV_ADMA_PORT_PRIV_DMA_SZ,
  1008. &mem_dma, GFP_KERNEL);
  1009. if (!mem)
  1010. return -ENOMEM;
  1011. memset(mem, 0, NV_ADMA_PORT_PRIV_DMA_SZ);
  1012. /*
  1013. * First item in chunk of DMA memory:
  1014. * 128-byte command parameter block (CPB)
  1015. * one for each command tag
  1016. */
  1017. pp->cpb = mem;
  1018. pp->cpb_dma = mem_dma;
  1019. writel(mem_dma & 0xFFFFFFFF, mmio + NV_ADMA_CPB_BASE_LOW);
  1020. writel((mem_dma >> 16) >> 16, mmio + NV_ADMA_CPB_BASE_HIGH);
  1021. mem += NV_ADMA_MAX_CPBS * NV_ADMA_CPB_SZ;
  1022. mem_dma += NV_ADMA_MAX_CPBS * NV_ADMA_CPB_SZ;
  1023. /*
  1024. * Second item: block of ADMA_SGTBL_LEN s/g entries
  1025. */
  1026. pp->aprd = mem;
  1027. pp->aprd_dma = mem_dma;
  1028. ap->private_data = pp;
  1029. /* clear any outstanding interrupt conditions */
  1030. writew(0xffff, mmio + NV_ADMA_STAT);
  1031. /* initialize port variables */
  1032. pp->flags = NV_ADMA_PORT_REGISTER_MODE;
  1033. /* clear CPB fetch count */
  1034. writew(0, mmio + NV_ADMA_CPB_COUNT);
  1035. /* clear GO for register mode, enable interrupt */
  1036. tmp = readw(mmio + NV_ADMA_CTL);
  1037. writew((tmp & ~NV_ADMA_CTL_GO) | NV_ADMA_CTL_AIEN |
  1038. NV_ADMA_CTL_HOTPLUG_IEN, mmio + NV_ADMA_CTL);
  1039. tmp = readw(mmio + NV_ADMA_CTL);
  1040. writew(tmp | NV_ADMA_CTL_CHANNEL_RESET, mmio + NV_ADMA_CTL);
  1041. readw(mmio + NV_ADMA_CTL); /* flush posted write */
  1042. udelay(1);
  1043. writew(tmp & ~NV_ADMA_CTL_CHANNEL_RESET, mmio + NV_ADMA_CTL);
  1044. readw(mmio + NV_ADMA_CTL); /* flush posted write */
  1045. return 0;
  1046. }
  1047. static void nv_adma_port_stop(struct ata_port *ap)
  1048. {
  1049. struct nv_adma_port_priv *pp = ap->private_data;
  1050. void __iomem *mmio = pp->ctl_block;
  1051. VPRINTK("ENTER\n");
  1052. writew(0, mmio + NV_ADMA_CTL);
  1053. }
  1054. #ifdef CONFIG_PM
  1055. static int nv_adma_port_suspend(struct ata_port *ap, pm_message_t mesg)
  1056. {
  1057. struct nv_adma_port_priv *pp = ap->private_data;
  1058. void __iomem *mmio = pp->ctl_block;
  1059. /* Go to register mode - clears GO */
  1060. nv_adma_register_mode(ap);
  1061. /* clear CPB fetch count */
  1062. writew(0, mmio + NV_ADMA_CPB_COUNT);
  1063. /* disable interrupt, shut down port */
  1064. writew(0, mmio + NV_ADMA_CTL);
  1065. return 0;
  1066. }
  1067. static int nv_adma_port_resume(struct ata_port *ap)
  1068. {
  1069. struct nv_adma_port_priv *pp = ap->private_data;
  1070. void __iomem *mmio = pp->ctl_block;
  1071. u16 tmp;
  1072. /* set CPB block location */
  1073. writel(pp->cpb_dma & 0xFFFFFFFF, mmio + NV_ADMA_CPB_BASE_LOW);
  1074. writel((pp->cpb_dma >> 16) >> 16, mmio + NV_ADMA_CPB_BASE_HIGH);
  1075. /* clear any outstanding interrupt conditions */
  1076. writew(0xffff, mmio + NV_ADMA_STAT);
  1077. /* initialize port variables */
  1078. pp->flags |= NV_ADMA_PORT_REGISTER_MODE;
  1079. /* clear CPB fetch count */
  1080. writew(0, mmio + NV_ADMA_CPB_COUNT);
  1081. /* clear GO for register mode, enable interrupt */
  1082. tmp = readw(mmio + NV_ADMA_CTL);
  1083. writew((tmp & ~NV_ADMA_CTL_GO) | NV_ADMA_CTL_AIEN |
  1084. NV_ADMA_CTL_HOTPLUG_IEN, mmio + NV_ADMA_CTL);
  1085. tmp = readw(mmio + NV_ADMA_CTL);
  1086. writew(tmp | NV_ADMA_CTL_CHANNEL_RESET, mmio + NV_ADMA_CTL);
  1087. readw(mmio + NV_ADMA_CTL); /* flush posted write */
  1088. udelay(1);
  1089. writew(tmp & ~NV_ADMA_CTL_CHANNEL_RESET, mmio + NV_ADMA_CTL);
  1090. readw(mmio + NV_ADMA_CTL); /* flush posted write */
  1091. return 0;
  1092. }
  1093. #endif
  1094. static void nv_adma_setup_port(struct ata_port *ap)
  1095. {
  1096. void __iomem *mmio = ap->host->iomap[NV_MMIO_BAR];
  1097. struct ata_ioports *ioport = &ap->ioaddr;
  1098. VPRINTK("ENTER\n");
  1099. mmio += NV_ADMA_PORT + ap->port_no * NV_ADMA_PORT_SIZE;
  1100. ioport->cmd_addr = mmio;
  1101. ioport->data_addr = mmio + (ATA_REG_DATA * 4);
  1102. ioport->error_addr =
  1103. ioport->feature_addr = mmio + (ATA_REG_ERR * 4);
  1104. ioport->nsect_addr = mmio + (ATA_REG_NSECT * 4);
  1105. ioport->lbal_addr = mmio + (ATA_REG_LBAL * 4);
  1106. ioport->lbam_addr = mmio + (ATA_REG_LBAM * 4);
  1107. ioport->lbah_addr = mmio + (ATA_REG_LBAH * 4);
  1108. ioport->device_addr = mmio + (ATA_REG_DEVICE * 4);
  1109. ioport->status_addr =
  1110. ioport->command_addr = mmio + (ATA_REG_STATUS * 4);
  1111. ioport->altstatus_addr =
  1112. ioport->ctl_addr = mmio + 0x20;
  1113. }
  1114. static int nv_adma_host_init(struct ata_host *host)
  1115. {
  1116. struct pci_dev *pdev = to_pci_dev(host->dev);
  1117. unsigned int i;
  1118. u32 tmp32;
  1119. VPRINTK("ENTER\n");
  1120. /* enable ADMA on the ports */
  1121. pci_read_config_dword(pdev, NV_MCP_SATA_CFG_20, &tmp32);
  1122. tmp32 |= NV_MCP_SATA_CFG_20_PORT0_EN |
  1123. NV_MCP_SATA_CFG_20_PORT0_PWB_EN |
  1124. NV_MCP_SATA_CFG_20_PORT1_EN |
  1125. NV_MCP_SATA_CFG_20_PORT1_PWB_EN;
  1126. pci_write_config_dword(pdev, NV_MCP_SATA_CFG_20, tmp32);
  1127. for (i = 0; i < host->n_ports; i++)
  1128. nv_adma_setup_port(host->ports[i]);
  1129. return 0;
  1130. }
  1131. static void nv_adma_fill_aprd(struct ata_queued_cmd *qc,
  1132. struct scatterlist *sg,
  1133. int idx,
  1134. struct nv_adma_prd *aprd)
  1135. {
  1136. u8 flags = 0;
  1137. if (qc->tf.flags & ATA_TFLAG_WRITE)
  1138. flags |= NV_APRD_WRITE;
  1139. if (idx == qc->n_elem - 1)
  1140. flags |= NV_APRD_END;
  1141. else if (idx != 4)
  1142. flags |= NV_APRD_CONT;
  1143. aprd->addr = cpu_to_le64(((u64)sg_dma_address(sg)));
  1144. aprd->len = cpu_to_le32(((u32)sg_dma_len(sg))); /* len in bytes */
  1145. aprd->flags = flags;
  1146. aprd->packet_len = 0;
  1147. }
  1148. static void nv_adma_fill_sg(struct ata_queued_cmd *qc, struct nv_adma_cpb *cpb)
  1149. {
  1150. struct nv_adma_port_priv *pp = qc->ap->private_data;
  1151. struct nv_adma_prd *aprd;
  1152. struct scatterlist *sg;
  1153. unsigned int si;
  1154. VPRINTK("ENTER\n");
  1155. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  1156. aprd = (si < 5) ? &cpb->aprd[si] :
  1157. &pp->aprd[NV_ADMA_SGTBL_LEN * qc->tag + (si-5)];
  1158. nv_adma_fill_aprd(qc, sg, si, aprd);
  1159. }
  1160. if (si > 5)
  1161. cpb->next_aprd = cpu_to_le64(((u64)(pp->aprd_dma + NV_ADMA_SGTBL_SZ * qc->tag)));
  1162. else
  1163. cpb->next_aprd = cpu_to_le64(0);
  1164. }
  1165. static int nv_adma_use_reg_mode(struct ata_queued_cmd *qc)
  1166. {
  1167. struct nv_adma_port_priv *pp = qc->ap->private_data;
  1168. /* ADMA engine can only be used for non-ATAPI DMA commands,
  1169. or interrupt-driven no-data commands. */
  1170. if ((pp->flags & NV_ADMA_ATAPI_SETUP_COMPLETE) ||
  1171. (qc->tf.flags & ATA_TFLAG_POLLING))
  1172. return 1;
  1173. if ((qc->flags & ATA_QCFLAG_DMAMAP) ||
  1174. (qc->tf.protocol == ATA_PROT_NODATA))
  1175. return 0;
  1176. return 1;
  1177. }
  1178. static enum ata_completion_errors nv_adma_qc_prep(struct ata_queued_cmd *qc)
  1179. {
  1180. struct nv_adma_port_priv *pp = qc->ap->private_data;
  1181. struct nv_adma_cpb *cpb = &pp->cpb[qc->tag];
  1182. u8 ctl_flags = NV_CPB_CTL_CPB_VALID |
  1183. NV_CPB_CTL_IEN;
  1184. if (nv_adma_use_reg_mode(qc)) {
  1185. BUG_ON(!(pp->flags & NV_ADMA_ATAPI_SETUP_COMPLETE) &&
  1186. (qc->flags & ATA_QCFLAG_DMAMAP));
  1187. nv_adma_register_mode(qc->ap);
  1188. ata_bmdma_qc_prep(qc);
  1189. return AC_ERR_OK;
  1190. }
  1191. cpb->resp_flags = NV_CPB_RESP_DONE;
  1192. wmb();
  1193. cpb->ctl_flags = 0;
  1194. wmb();
  1195. cpb->len = 3;
  1196. cpb->tag = qc->tag;
  1197. cpb->next_cpb_idx = 0;
  1198. /* turn on NCQ flags for NCQ commands */
  1199. if (qc->tf.protocol == ATA_PROT_NCQ)
  1200. ctl_flags |= NV_CPB_CTL_QUEUE | NV_CPB_CTL_FPDMA;
  1201. VPRINTK("qc->flags = 0x%lx\n", qc->flags);
  1202. nv_adma_tf_to_cpb(&qc->tf, cpb->tf);
  1203. if (qc->flags & ATA_QCFLAG_DMAMAP) {
  1204. nv_adma_fill_sg(qc, cpb);
  1205. ctl_flags |= NV_CPB_CTL_APRD_VALID;
  1206. } else
  1207. memset(&cpb->aprd[0], 0, sizeof(struct nv_adma_prd) * 5);
  1208. /* Be paranoid and don't let the device see NV_CPB_CTL_CPB_VALID
  1209. until we are finished filling in all of the contents */
  1210. wmb();
  1211. cpb->ctl_flags = ctl_flags;
  1212. wmb();
  1213. cpb->resp_flags = 0;
  1214. return AC_ERR_OK;
  1215. }
  1216. static unsigned int nv_adma_qc_issue(struct ata_queued_cmd *qc)
  1217. {
  1218. struct nv_adma_port_priv *pp = qc->ap->private_data;
  1219. void __iomem *mmio = pp->ctl_block;
  1220. int curr_ncq = (qc->tf.protocol == ATA_PROT_NCQ);
  1221. VPRINTK("ENTER\n");
  1222. /* We can't handle result taskfile with NCQ commands, since
  1223. retrieving the taskfile switches us out of ADMA mode and would abort
  1224. existing commands. */
  1225. if (unlikely(qc->tf.protocol == ATA_PROT_NCQ &&
  1226. (qc->flags & ATA_QCFLAG_RESULT_TF))) {
  1227. ata_dev_err(qc->dev, "NCQ w/ RESULT_TF not allowed\n");
  1228. return AC_ERR_SYSTEM;
  1229. }
  1230. if (nv_adma_use_reg_mode(qc)) {
  1231. /* use ATA register mode */
  1232. VPRINTK("using ATA register mode: 0x%lx\n", qc->flags);
  1233. BUG_ON(!(pp->flags & NV_ADMA_ATAPI_SETUP_COMPLETE) &&
  1234. (qc->flags & ATA_QCFLAG_DMAMAP));
  1235. nv_adma_register_mode(qc->ap);
  1236. return ata_bmdma_qc_issue(qc);
  1237. } else
  1238. nv_adma_mode(qc->ap);
  1239. /* write append register, command tag in lower 8 bits
  1240. and (number of cpbs to append -1) in top 8 bits */
  1241. wmb();
  1242. if (curr_ncq != pp->last_issue_ncq) {
  1243. /* Seems to need some delay before switching between NCQ and
  1244. non-NCQ commands, else we get command timeouts and such. */
  1245. udelay(20);
  1246. pp->last_issue_ncq = curr_ncq;
  1247. }
  1248. writew(qc->tag, mmio + NV_ADMA_APPEND);
  1249. DPRINTK("Issued tag %u\n", qc->tag);
  1250. return 0;
  1251. }
  1252. static irqreturn_t nv_generic_interrupt(int irq, void *dev_instance)
  1253. {
  1254. struct ata_host *host = dev_instance;
  1255. unsigned int i;
  1256. unsigned int handled = 0;
  1257. unsigned long flags;
  1258. spin_lock_irqsave(&host->lock, flags);
  1259. for (i = 0; i < host->n_ports; i++) {
  1260. struct ata_port *ap = host->ports[i];
  1261. struct ata_queued_cmd *qc;
  1262. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  1263. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING))) {
  1264. handled += ata_bmdma_port_intr(ap, qc);
  1265. } else {
  1266. /*
  1267. * No request pending? Clear interrupt status
  1268. * anyway, in case there's one pending.
  1269. */
  1270. ap->ops->sff_check_status(ap);
  1271. }
  1272. }
  1273. spin_unlock_irqrestore(&host->lock, flags);
  1274. return IRQ_RETVAL(handled);
  1275. }
  1276. static irqreturn_t nv_do_interrupt(struct ata_host *host, u8 irq_stat)
  1277. {
  1278. int i, handled = 0;
  1279. for (i = 0; i < host->n_ports; i++) {
  1280. handled += nv_host_intr(host->ports[i], irq_stat);
  1281. irq_stat >>= NV_INT_PORT_SHIFT;
  1282. }
  1283. return IRQ_RETVAL(handled);
  1284. }
  1285. static irqreturn_t nv_nf2_interrupt(int irq, void *dev_instance)
  1286. {
  1287. struct ata_host *host = dev_instance;
  1288. u8 irq_stat;
  1289. irqreturn_t ret;
  1290. spin_lock(&host->lock);
  1291. irq_stat = ioread8(host->ports[0]->ioaddr.scr_addr + NV_INT_STATUS);
  1292. ret = nv_do_interrupt(host, irq_stat);
  1293. spin_unlock(&host->lock);
  1294. return ret;
  1295. }
  1296. static irqreturn_t nv_ck804_interrupt(int irq, void *dev_instance)
  1297. {
  1298. struct ata_host *host = dev_instance;
  1299. u8 irq_stat;
  1300. irqreturn_t ret;
  1301. spin_lock(&host->lock);
  1302. irq_stat = readb(host->iomap[NV_MMIO_BAR] + NV_INT_STATUS_CK804);
  1303. ret = nv_do_interrupt(host, irq_stat);
  1304. spin_unlock(&host->lock);
  1305. return ret;
  1306. }
  1307. static int nv_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val)
  1308. {
  1309. if (sc_reg > SCR_CONTROL)
  1310. return -EINVAL;
  1311. *val = ioread32(link->ap->ioaddr.scr_addr + (sc_reg * 4));
  1312. return 0;
  1313. }
  1314. static int nv_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val)
  1315. {
  1316. if (sc_reg > SCR_CONTROL)
  1317. return -EINVAL;
  1318. iowrite32(val, link->ap->ioaddr.scr_addr + (sc_reg * 4));
  1319. return 0;
  1320. }
  1321. static int nv_hardreset(struct ata_link *link, unsigned int *class,
  1322. unsigned long deadline)
  1323. {
  1324. struct ata_eh_context *ehc = &link->eh_context;
  1325. /* Do hardreset iff it's post-boot probing, please read the
  1326. * comment above port ops for details.
  1327. */
  1328. if (!(link->ap->pflags & ATA_PFLAG_LOADING) &&
  1329. !ata_dev_enabled(link->device))
  1330. sata_link_hardreset(link, sata_deb_timing_hotplug, deadline,
  1331. NULL, NULL);
  1332. else {
  1333. const unsigned long *timing = sata_ehc_deb_timing(ehc);
  1334. int rc;
  1335. if (!(ehc->i.flags & ATA_EHI_QUIET))
  1336. ata_link_info(link,
  1337. "nv: skipping hardreset on occupied port\n");
  1338. /* make sure the link is online */
  1339. rc = sata_link_resume(link, timing, deadline);
  1340. /* whine about phy resume failure but proceed */
  1341. if (rc && rc != -EOPNOTSUPP)
  1342. ata_link_warn(link, "failed to resume link (errno=%d)\n",
  1343. rc);
  1344. }
  1345. /* device signature acquisition is unreliable */
  1346. return -EAGAIN;
  1347. }
  1348. static void nv_nf2_freeze(struct ata_port *ap)
  1349. {
  1350. void __iomem *scr_addr = ap->host->ports[0]->ioaddr.scr_addr;
  1351. int shift = ap->port_no * NV_INT_PORT_SHIFT;
  1352. u8 mask;
  1353. mask = ioread8(scr_addr + NV_INT_ENABLE);
  1354. mask &= ~(NV_INT_ALL << shift);
  1355. iowrite8(mask, scr_addr + NV_INT_ENABLE);
  1356. }
  1357. static void nv_nf2_thaw(struct ata_port *ap)
  1358. {
  1359. void __iomem *scr_addr = ap->host->ports[0]->ioaddr.scr_addr;
  1360. int shift = ap->port_no * NV_INT_PORT_SHIFT;
  1361. u8 mask;
  1362. iowrite8(NV_INT_ALL << shift, scr_addr + NV_INT_STATUS);
  1363. mask = ioread8(scr_addr + NV_INT_ENABLE);
  1364. mask |= (NV_INT_MASK << shift);
  1365. iowrite8(mask, scr_addr + NV_INT_ENABLE);
  1366. }
  1367. static void nv_ck804_freeze(struct ata_port *ap)
  1368. {
  1369. void __iomem *mmio_base = ap->host->iomap[NV_MMIO_BAR];
  1370. int shift = ap->port_no * NV_INT_PORT_SHIFT;
  1371. u8 mask;
  1372. mask = readb(mmio_base + NV_INT_ENABLE_CK804);
  1373. mask &= ~(NV_INT_ALL << shift);
  1374. writeb(mask, mmio_base + NV_INT_ENABLE_CK804);
  1375. }
  1376. static void nv_ck804_thaw(struct ata_port *ap)
  1377. {
  1378. void __iomem *mmio_base = ap->host->iomap[NV_MMIO_BAR];
  1379. int shift = ap->port_no * NV_INT_PORT_SHIFT;
  1380. u8 mask;
  1381. writeb(NV_INT_ALL << shift, mmio_base + NV_INT_STATUS_CK804);
  1382. mask = readb(mmio_base + NV_INT_ENABLE_CK804);
  1383. mask |= (NV_INT_MASK << shift);
  1384. writeb(mask, mmio_base + NV_INT_ENABLE_CK804);
  1385. }
  1386. static void nv_mcp55_freeze(struct ata_port *ap)
  1387. {
  1388. void __iomem *mmio_base = ap->host->iomap[NV_MMIO_BAR];
  1389. int shift = ap->port_no * NV_INT_PORT_SHIFT_MCP55;
  1390. u32 mask;
  1391. writel(NV_INT_ALL_MCP55 << shift, mmio_base + NV_INT_STATUS_MCP55);
  1392. mask = readl(mmio_base + NV_INT_ENABLE_MCP55);
  1393. mask &= ~(NV_INT_ALL_MCP55 << shift);
  1394. writel(mask, mmio_base + NV_INT_ENABLE_MCP55);
  1395. }
  1396. static void nv_mcp55_thaw(struct ata_port *ap)
  1397. {
  1398. void __iomem *mmio_base = ap->host->iomap[NV_MMIO_BAR];
  1399. int shift = ap->port_no * NV_INT_PORT_SHIFT_MCP55;
  1400. u32 mask;
  1401. writel(NV_INT_ALL_MCP55 << shift, mmio_base + NV_INT_STATUS_MCP55);
  1402. mask = readl(mmio_base + NV_INT_ENABLE_MCP55);
  1403. mask |= (NV_INT_MASK_MCP55 << shift);
  1404. writel(mask, mmio_base + NV_INT_ENABLE_MCP55);
  1405. }
  1406. static void nv_adma_error_handler(struct ata_port *ap)
  1407. {
  1408. struct nv_adma_port_priv *pp = ap->private_data;
  1409. if (!(pp->flags & NV_ADMA_PORT_REGISTER_MODE)) {
  1410. void __iomem *mmio = pp->ctl_block;
  1411. int i;
  1412. u16 tmp;
  1413. if (ata_tag_valid(ap->link.active_tag) || ap->link.sactive) {
  1414. u32 notifier = readl(mmio + NV_ADMA_NOTIFIER);
  1415. u32 notifier_error = readl(mmio + NV_ADMA_NOTIFIER_ERROR);
  1416. u32 gen_ctl = readl(pp->gen_block + NV_ADMA_GEN_CTL);
  1417. u32 status = readw(mmio + NV_ADMA_STAT);
  1418. u8 cpb_count = readb(mmio + NV_ADMA_CPB_COUNT);
  1419. u8 next_cpb_idx = readb(mmio + NV_ADMA_NEXT_CPB_IDX);
  1420. ata_port_err(ap,
  1421. "EH in ADMA mode, notifier 0x%X "
  1422. "notifier_error 0x%X gen_ctl 0x%X status 0x%X "
  1423. "next cpb count 0x%X next cpb idx 0x%x\n",
  1424. notifier, notifier_error, gen_ctl, status,
  1425. cpb_count, next_cpb_idx);
  1426. for (i = 0; i < NV_ADMA_MAX_CPBS; i++) {
  1427. struct nv_adma_cpb *cpb = &pp->cpb[i];
  1428. if ((ata_tag_valid(ap->link.active_tag) && i == ap->link.active_tag) ||
  1429. ap->link.sactive & (1 << i))
  1430. ata_port_err(ap,
  1431. "CPB %d: ctl_flags 0x%x, resp_flags 0x%x\n",
  1432. i, cpb->ctl_flags, cpb->resp_flags);
  1433. }
  1434. }
  1435. /* Push us back into port register mode for error handling. */
  1436. nv_adma_register_mode(ap);
  1437. /* Mark all of the CPBs as invalid to prevent them from
  1438. being executed */
  1439. for (i = 0; i < NV_ADMA_MAX_CPBS; i++)
  1440. pp->cpb[i].ctl_flags &= ~NV_CPB_CTL_CPB_VALID;
  1441. /* clear CPB fetch count */
  1442. writew(0, mmio + NV_ADMA_CPB_COUNT);
  1443. /* Reset channel */
  1444. tmp = readw(mmio + NV_ADMA_CTL);
  1445. writew(tmp | NV_ADMA_CTL_CHANNEL_RESET, mmio + NV_ADMA_CTL);
  1446. readw(mmio + NV_ADMA_CTL); /* flush posted write */
  1447. udelay(1);
  1448. writew(tmp & ~NV_ADMA_CTL_CHANNEL_RESET, mmio + NV_ADMA_CTL);
  1449. readw(mmio + NV_ADMA_CTL); /* flush posted write */
  1450. }
  1451. ata_bmdma_error_handler(ap);
  1452. }
  1453. static void nv_swncq_qc_to_dq(struct ata_port *ap, struct ata_queued_cmd *qc)
  1454. {
  1455. struct nv_swncq_port_priv *pp = ap->private_data;
  1456. struct defer_queue *dq = &pp->defer_queue;
  1457. /* queue is full */
  1458. WARN_ON(dq->tail - dq->head == ATA_MAX_QUEUE);
  1459. dq->defer_bits |= (1 << qc->tag);
  1460. dq->tag[dq->tail++ & (ATA_MAX_QUEUE - 1)] = qc->tag;
  1461. }
  1462. static struct ata_queued_cmd *nv_swncq_qc_from_dq(struct ata_port *ap)
  1463. {
  1464. struct nv_swncq_port_priv *pp = ap->private_data;
  1465. struct defer_queue *dq = &pp->defer_queue;
  1466. unsigned int tag;
  1467. if (dq->head == dq->tail) /* null queue */
  1468. return NULL;
  1469. tag = dq->tag[dq->head & (ATA_MAX_QUEUE - 1)];
  1470. dq->tag[dq->head++ & (ATA_MAX_QUEUE - 1)] = ATA_TAG_POISON;
  1471. WARN_ON(!(dq->defer_bits & (1 << tag)));
  1472. dq->defer_bits &= ~(1 << tag);
  1473. return ata_qc_from_tag(ap, tag);
  1474. }
  1475. static void nv_swncq_fis_reinit(struct ata_port *ap)
  1476. {
  1477. struct nv_swncq_port_priv *pp = ap->private_data;
  1478. pp->dhfis_bits = 0;
  1479. pp->dmafis_bits = 0;
  1480. pp->sdbfis_bits = 0;
  1481. pp->ncq_flags = 0;
  1482. }
  1483. static void nv_swncq_pp_reinit(struct ata_port *ap)
  1484. {
  1485. struct nv_swncq_port_priv *pp = ap->private_data;
  1486. struct defer_queue *dq = &pp->defer_queue;
  1487. dq->head = 0;
  1488. dq->tail = 0;
  1489. dq->defer_bits = 0;
  1490. pp->qc_active = 0;
  1491. pp->last_issue_tag = ATA_TAG_POISON;
  1492. nv_swncq_fis_reinit(ap);
  1493. }
  1494. static void nv_swncq_irq_clear(struct ata_port *ap, u16 fis)
  1495. {
  1496. struct nv_swncq_port_priv *pp = ap->private_data;
  1497. writew(fis, pp->irq_block);
  1498. }
  1499. static void __ata_bmdma_stop(struct ata_port *ap)
  1500. {
  1501. struct ata_queued_cmd qc;
  1502. qc.ap = ap;
  1503. ata_bmdma_stop(&qc);
  1504. }
  1505. static void nv_swncq_ncq_stop(struct ata_port *ap)
  1506. {
  1507. struct nv_swncq_port_priv *pp = ap->private_data;
  1508. unsigned int i;
  1509. u32 sactive;
  1510. u32 done_mask;
  1511. ata_port_err(ap, "EH in SWNCQ mode,QC:qc_active 0x%X sactive 0x%X\n",
  1512. ap->qc_active, ap->link.sactive);
  1513. ata_port_err(ap,
  1514. "SWNCQ:qc_active 0x%X defer_bits 0x%X last_issue_tag 0x%x\n "
  1515. "dhfis 0x%X dmafis 0x%X sdbfis 0x%X\n",
  1516. pp->qc_active, pp->defer_queue.defer_bits, pp->last_issue_tag,
  1517. pp->dhfis_bits, pp->dmafis_bits, pp->sdbfis_bits);
  1518. ata_port_err(ap, "ATA_REG 0x%X ERR_REG 0x%X\n",
  1519. ap->ops->sff_check_status(ap),
  1520. ioread8(ap->ioaddr.error_addr));
  1521. sactive = readl(pp->sactive_block);
  1522. done_mask = pp->qc_active ^ sactive;
  1523. ata_port_err(ap, "tag : dhfis dmafis sdbfis sactive\n");
  1524. for (i = 0; i < ATA_MAX_QUEUE; i++) {
  1525. u8 err = 0;
  1526. if (pp->qc_active & (1 << i))
  1527. err = 0;
  1528. else if (done_mask & (1 << i))
  1529. err = 1;
  1530. else
  1531. continue;
  1532. ata_port_err(ap,
  1533. "tag 0x%x: %01x %01x %01x %01x %s\n", i,
  1534. (pp->dhfis_bits >> i) & 0x1,
  1535. (pp->dmafis_bits >> i) & 0x1,
  1536. (pp->sdbfis_bits >> i) & 0x1,
  1537. (sactive >> i) & 0x1,
  1538. (err ? "error! tag doesn't exit" : " "));
  1539. }
  1540. nv_swncq_pp_reinit(ap);
  1541. ap->ops->sff_irq_clear(ap);
  1542. __ata_bmdma_stop(ap);
  1543. nv_swncq_irq_clear(ap, 0xffff);
  1544. }
  1545. static void nv_swncq_error_handler(struct ata_port *ap)
  1546. {
  1547. struct ata_eh_context *ehc = &ap->link.eh_context;
  1548. if (ap->link.sactive) {
  1549. nv_swncq_ncq_stop(ap);
  1550. ehc->i.action |= ATA_EH_RESET;
  1551. }
  1552. ata_bmdma_error_handler(ap);
  1553. }
  1554. #ifdef CONFIG_PM
  1555. static int nv_swncq_port_suspend(struct ata_port *ap, pm_message_t mesg)
  1556. {
  1557. void __iomem *mmio = ap->host->iomap[NV_MMIO_BAR];
  1558. u32 tmp;
  1559. /* clear irq */
  1560. writel(~0, mmio + NV_INT_STATUS_MCP55);
  1561. /* disable irq */
  1562. writel(0, mmio + NV_INT_ENABLE_MCP55);
  1563. /* disable swncq */
  1564. tmp = readl(mmio + NV_CTL_MCP55);
  1565. tmp &= ~(NV_CTL_PRI_SWNCQ | NV_CTL_SEC_SWNCQ);
  1566. writel(tmp, mmio + NV_CTL_MCP55);
  1567. return 0;
  1568. }
  1569. static int nv_swncq_port_resume(struct ata_port *ap)
  1570. {
  1571. void __iomem *mmio = ap->host->iomap[NV_MMIO_BAR];
  1572. u32 tmp;
  1573. /* clear irq */
  1574. writel(~0, mmio + NV_INT_STATUS_MCP55);
  1575. /* enable irq */
  1576. writel(0x00fd00fd, mmio + NV_INT_ENABLE_MCP55);
  1577. /* enable swncq */
  1578. tmp = readl(mmio + NV_CTL_MCP55);
  1579. writel(tmp | NV_CTL_PRI_SWNCQ | NV_CTL_SEC_SWNCQ, mmio + NV_CTL_MCP55);
  1580. return 0;
  1581. }
  1582. #endif
  1583. static void nv_swncq_host_init(struct ata_host *host)
  1584. {
  1585. u32 tmp;
  1586. void __iomem *mmio = host->iomap[NV_MMIO_BAR];
  1587. struct pci_dev *pdev = to_pci_dev(host->dev);
  1588. u8 regval;
  1589. /* disable ECO 398 */
  1590. pci_read_config_byte(pdev, 0x7f, &regval);
  1591. regval &= ~(1 << 7);
  1592. pci_write_config_byte(pdev, 0x7f, regval);
  1593. /* enable swncq */
  1594. tmp = readl(mmio + NV_CTL_MCP55);
  1595. VPRINTK("HOST_CTL:0x%X\n", tmp);
  1596. writel(tmp | NV_CTL_PRI_SWNCQ | NV_CTL_SEC_SWNCQ, mmio + NV_CTL_MCP55);
  1597. /* enable irq intr */
  1598. tmp = readl(mmio + NV_INT_ENABLE_MCP55);
  1599. VPRINTK("HOST_ENABLE:0x%X\n", tmp);
  1600. writel(tmp | 0x00fd00fd, mmio + NV_INT_ENABLE_MCP55);
  1601. /* clear port irq */
  1602. writel(~0x0, mmio + NV_INT_STATUS_MCP55);
  1603. }
  1604. static int nv_swncq_slave_config(struct scsi_device *sdev)
  1605. {
  1606. struct ata_port *ap = ata_shost_to_port(sdev->host);
  1607. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  1608. struct ata_device *dev;
  1609. int rc;
  1610. u8 rev;
  1611. u8 check_maxtor = 0;
  1612. unsigned char model_num[ATA_ID_PROD_LEN + 1];
  1613. rc = ata_scsi_slave_config(sdev);
  1614. if (sdev->id >= ATA_MAX_DEVICES || sdev->channel || sdev->lun)
  1615. /* Not a proper libata device, ignore */
  1616. return rc;
  1617. dev = &ap->link.device[sdev->id];
  1618. if (!(ap->flags & ATA_FLAG_NCQ) || dev->class == ATA_DEV_ATAPI)
  1619. return rc;
  1620. /* if MCP51 and Maxtor, then disable ncq */
  1621. if (pdev->device == PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA ||
  1622. pdev->device == PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA2)
  1623. check_maxtor = 1;
  1624. /* if MCP55 and rev <= a2 and Maxtor, then disable ncq */
  1625. if (pdev->device == PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA ||
  1626. pdev->device == PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA2) {
  1627. pci_read_config_byte(pdev, 0x8, &rev);
  1628. if (rev <= 0xa2)
  1629. check_maxtor = 1;
  1630. }
  1631. if (!check_maxtor)
  1632. return rc;
  1633. ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
  1634. if (strncmp(model_num, "Maxtor", 6) == 0) {
  1635. ata_scsi_change_queue_depth(sdev, 1);
  1636. ata_dev_notice(dev, "Disabling SWNCQ mode (depth %x)\n",
  1637. sdev->queue_depth);
  1638. }
  1639. return rc;
  1640. }
  1641. static int nv_swncq_port_start(struct ata_port *ap)
  1642. {
  1643. struct device *dev = ap->host->dev;
  1644. void __iomem *mmio = ap->host->iomap[NV_MMIO_BAR];
  1645. struct nv_swncq_port_priv *pp;
  1646. int rc;
  1647. /* we might fallback to bmdma, allocate bmdma resources */
  1648. rc = ata_bmdma_port_start(ap);
  1649. if (rc)
  1650. return rc;
  1651. pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
  1652. if (!pp)
  1653. return -ENOMEM;
  1654. pp->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ * ATA_MAX_QUEUE,
  1655. &pp->prd_dma, GFP_KERNEL);
  1656. if (!pp->prd)
  1657. return -ENOMEM;
  1658. memset(pp->prd, 0, ATA_PRD_TBL_SZ * ATA_MAX_QUEUE);
  1659. ap->private_data = pp;
  1660. pp->sactive_block = ap->ioaddr.scr_addr + 4 * SCR_ACTIVE;
  1661. pp->irq_block = mmio + NV_INT_STATUS_MCP55 + ap->port_no * 2;
  1662. pp->tag_block = mmio + NV_NCQ_REG_MCP55 + ap->port_no * 2;
  1663. return 0;
  1664. }
  1665. static enum ata_completion_errors nv_swncq_qc_prep(struct ata_queued_cmd *qc)
  1666. {
  1667. if (qc->tf.protocol != ATA_PROT_NCQ) {
  1668. ata_bmdma_qc_prep(qc);
  1669. return AC_ERR_OK;
  1670. }
  1671. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  1672. return AC_ERR_OK;
  1673. nv_swncq_fill_sg(qc);
  1674. return AC_ERR_OK;
  1675. }
  1676. static void nv_swncq_fill_sg(struct ata_queued_cmd *qc)
  1677. {
  1678. struct ata_port *ap = qc->ap;
  1679. struct scatterlist *sg;
  1680. struct nv_swncq_port_priv *pp = ap->private_data;
  1681. struct ata_bmdma_prd *prd;
  1682. unsigned int si, idx;
  1683. prd = pp->prd + ATA_MAX_PRD * qc->tag;
  1684. idx = 0;
  1685. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  1686. u32 addr, offset;
  1687. u32 sg_len, len;
  1688. addr = (u32)sg_dma_address(sg);
  1689. sg_len = sg_dma_len(sg);
  1690. while (sg_len) {
  1691. offset = addr & 0xffff;
  1692. len = sg_len;
  1693. if ((offset + sg_len) > 0x10000)
  1694. len = 0x10000 - offset;
  1695. prd[idx].addr = cpu_to_le32(addr);
  1696. prd[idx].flags_len = cpu_to_le32(len & 0xffff);
  1697. idx++;
  1698. sg_len -= len;
  1699. addr += len;
  1700. }
  1701. }
  1702. prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
  1703. }
  1704. static unsigned int nv_swncq_issue_atacmd(struct ata_port *ap,
  1705. struct ata_queued_cmd *qc)
  1706. {
  1707. struct nv_swncq_port_priv *pp = ap->private_data;
  1708. if (qc == NULL)
  1709. return 0;
  1710. DPRINTK("Enter\n");
  1711. writel((1 << qc->tag), pp->sactive_block);
  1712. pp->last_issue_tag = qc->tag;
  1713. pp->dhfis_bits &= ~(1 << qc->tag);
  1714. pp->dmafis_bits &= ~(1 << qc->tag);
  1715. pp->qc_active |= (0x1 << qc->tag);
  1716. ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
  1717. ap->ops->sff_exec_command(ap, &qc->tf);
  1718. DPRINTK("Issued tag %u\n", qc->tag);
  1719. return 0;
  1720. }
  1721. static unsigned int nv_swncq_qc_issue(struct ata_queued_cmd *qc)
  1722. {
  1723. struct ata_port *ap = qc->ap;
  1724. struct nv_swncq_port_priv *pp = ap->private_data;
  1725. if (qc->tf.protocol != ATA_PROT_NCQ)
  1726. return ata_bmdma_qc_issue(qc);
  1727. DPRINTK("Enter\n");
  1728. if (!pp->qc_active)
  1729. nv_swncq_issue_atacmd(ap, qc);
  1730. else
  1731. nv_swncq_qc_to_dq(ap, qc); /* add qc to defer queue */
  1732. return 0;
  1733. }
  1734. static void nv_swncq_hotplug(struct ata_port *ap, u32 fis)
  1735. {
  1736. u32 serror;
  1737. struct ata_eh_info *ehi = &ap->link.eh_info;
  1738. ata_ehi_clear_desc(ehi);
  1739. /* AHCI needs SError cleared; otherwise, it might lock up */
  1740. sata_scr_read(&ap->link, SCR_ERROR, &serror);
  1741. sata_scr_write(&ap->link, SCR_ERROR, serror);
  1742. /* analyze @irq_stat */
  1743. if (fis & NV_SWNCQ_IRQ_ADDED)
  1744. ata_ehi_push_desc(ehi, "hot plug");
  1745. else if (fis & NV_SWNCQ_IRQ_REMOVED)
  1746. ata_ehi_push_desc(ehi, "hot unplug");
  1747. ata_ehi_hotplugged(ehi);
  1748. /* okay, let's hand over to EH */
  1749. ehi->serror |= serror;
  1750. ata_port_freeze(ap);
  1751. }
  1752. static int nv_swncq_sdbfis(struct ata_port *ap)
  1753. {
  1754. struct ata_queued_cmd *qc;
  1755. struct nv_swncq_port_priv *pp = ap->private_data;
  1756. struct ata_eh_info *ehi = &ap->link.eh_info;
  1757. u32 sactive;
  1758. u32 done_mask;
  1759. u8 host_stat;
  1760. u8 lack_dhfis = 0;
  1761. host_stat = ap->ops->bmdma_status(ap);
  1762. if (unlikely(host_stat & ATA_DMA_ERR)) {
  1763. /* error when transferring data to/from memory */
  1764. ata_ehi_clear_desc(ehi);
  1765. ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
  1766. ehi->err_mask |= AC_ERR_HOST_BUS;
  1767. ehi->action |= ATA_EH_RESET;
  1768. return -EINVAL;
  1769. }
  1770. ap->ops->sff_irq_clear(ap);
  1771. __ata_bmdma_stop(ap);
  1772. sactive = readl(pp->sactive_block);
  1773. done_mask = pp->qc_active ^ sactive;
  1774. pp->qc_active &= ~done_mask;
  1775. pp->dhfis_bits &= ~done_mask;
  1776. pp->dmafis_bits &= ~done_mask;
  1777. pp->sdbfis_bits |= done_mask;
  1778. ata_qc_complete_multiple(ap, ap->qc_active ^ done_mask);
  1779. if (!ap->qc_active) {
  1780. DPRINTK("over\n");
  1781. nv_swncq_pp_reinit(ap);
  1782. return 0;
  1783. }
  1784. if (pp->qc_active & pp->dhfis_bits)
  1785. return 0;
  1786. if ((pp->ncq_flags & ncq_saw_backout) ||
  1787. (pp->qc_active ^ pp->dhfis_bits))
  1788. /* if the controller can't get a device to host register FIS,
  1789. * The driver needs to reissue the new command.
  1790. */
  1791. lack_dhfis = 1;
  1792. DPRINTK("id 0x%x QC: qc_active 0x%x,"
  1793. "SWNCQ:qc_active 0x%X defer_bits %X "
  1794. "dhfis 0x%X dmafis 0x%X last_issue_tag %x\n",
  1795. ap->print_id, ap->qc_active, pp->qc_active,
  1796. pp->defer_queue.defer_bits, pp->dhfis_bits,
  1797. pp->dmafis_bits, pp->last_issue_tag);
  1798. nv_swncq_fis_reinit(ap);
  1799. if (lack_dhfis) {
  1800. qc = ata_qc_from_tag(ap, pp->last_issue_tag);
  1801. nv_swncq_issue_atacmd(ap, qc);
  1802. return 0;
  1803. }
  1804. if (pp->defer_queue.defer_bits) {
  1805. /* send deferral queue command */
  1806. qc = nv_swncq_qc_from_dq(ap);
  1807. WARN_ON(qc == NULL);
  1808. nv_swncq_issue_atacmd(ap, qc);
  1809. }
  1810. return 0;
  1811. }
  1812. static inline u32 nv_swncq_tag(struct ata_port *ap)
  1813. {
  1814. struct nv_swncq_port_priv *pp = ap->private_data;
  1815. u32 tag;
  1816. tag = readb(pp->tag_block) >> 2;
  1817. return (tag & 0x1f);
  1818. }
  1819. static void nv_swncq_dmafis(struct ata_port *ap)
  1820. {
  1821. struct ata_queued_cmd *qc;
  1822. unsigned int rw;
  1823. u8 dmactl;
  1824. u32 tag;
  1825. struct nv_swncq_port_priv *pp = ap->private_data;
  1826. __ata_bmdma_stop(ap);
  1827. tag = nv_swncq_tag(ap);
  1828. DPRINTK("dma setup tag 0x%x\n", tag);
  1829. qc = ata_qc_from_tag(ap, tag);
  1830. if (unlikely(!qc))
  1831. return;
  1832. rw = qc->tf.flags & ATA_TFLAG_WRITE;
  1833. /* load PRD table addr. */
  1834. iowrite32(pp->prd_dma + ATA_PRD_TBL_SZ * qc->tag,
  1835. ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
  1836. /* specify data direction, triple-check start bit is clear */
  1837. dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  1838. dmactl &= ~ATA_DMA_WR;
  1839. if (!rw)
  1840. dmactl |= ATA_DMA_WR;
  1841. iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  1842. }
  1843. static void nv_swncq_host_interrupt(struct ata_port *ap, u16 fis)
  1844. {
  1845. struct nv_swncq_port_priv *pp = ap->private_data;
  1846. struct ata_queued_cmd *qc;
  1847. struct ata_eh_info *ehi = &ap->link.eh_info;
  1848. u32 serror;
  1849. u8 ata_stat;
  1850. ata_stat = ap->ops->sff_check_status(ap);
  1851. nv_swncq_irq_clear(ap, fis);
  1852. if (!fis)
  1853. return;
  1854. if (ap->pflags & ATA_PFLAG_FROZEN)
  1855. return;
  1856. if (fis & NV_SWNCQ_IRQ_HOTPLUG) {
  1857. nv_swncq_hotplug(ap, fis);
  1858. return;
  1859. }
  1860. if (!pp->qc_active)
  1861. return;
  1862. if (ap->ops->scr_read(&ap->link, SCR_ERROR, &serror))
  1863. return;
  1864. ap->ops->scr_write(&ap->link, SCR_ERROR, serror);
  1865. if (ata_stat & ATA_ERR) {
  1866. ata_ehi_clear_desc(ehi);
  1867. ata_ehi_push_desc(ehi, "Ata error. fis:0x%X", fis);
  1868. ehi->err_mask |= AC_ERR_DEV;
  1869. ehi->serror |= serror;
  1870. ehi->action |= ATA_EH_RESET;
  1871. ata_port_freeze(ap);
  1872. return;
  1873. }
  1874. if (fis & NV_SWNCQ_IRQ_BACKOUT) {
  1875. /* If the IRQ is backout, driver must issue
  1876. * the new command again some time later.
  1877. */
  1878. pp->ncq_flags |= ncq_saw_backout;
  1879. }
  1880. if (fis & NV_SWNCQ_IRQ_SDBFIS) {
  1881. pp->ncq_flags |= ncq_saw_sdb;
  1882. DPRINTK("id 0x%x SWNCQ: qc_active 0x%X "
  1883. "dhfis 0x%X dmafis 0x%X sactive 0x%X\n",
  1884. ap->print_id, pp->qc_active, pp->dhfis_bits,
  1885. pp->dmafis_bits, readl(pp->sactive_block));
  1886. if (nv_swncq_sdbfis(ap) < 0)
  1887. goto irq_error;
  1888. }
  1889. if (fis & NV_SWNCQ_IRQ_DHREGFIS) {
  1890. /* The interrupt indicates the new command
  1891. * was transmitted correctly to the drive.
  1892. */
  1893. pp->dhfis_bits |= (0x1 << pp->last_issue_tag);
  1894. pp->ncq_flags |= ncq_saw_d2h;
  1895. if (pp->ncq_flags & (ncq_saw_sdb | ncq_saw_backout)) {
  1896. ata_ehi_push_desc(ehi, "illegal fis transaction");
  1897. ehi->err_mask |= AC_ERR_HSM;
  1898. ehi->action |= ATA_EH_RESET;
  1899. goto irq_error;
  1900. }
  1901. if (!(fis & NV_SWNCQ_IRQ_DMASETUP) &&
  1902. !(pp->ncq_flags & ncq_saw_dmas)) {
  1903. ata_stat = ap->ops->sff_check_status(ap);
  1904. if (ata_stat & ATA_BUSY)
  1905. goto irq_exit;
  1906. if (pp->defer_queue.defer_bits) {
  1907. DPRINTK("send next command\n");
  1908. qc = nv_swncq_qc_from_dq(ap);
  1909. nv_swncq_issue_atacmd(ap, qc);
  1910. }
  1911. }
  1912. }
  1913. if (fis & NV_SWNCQ_IRQ_DMASETUP) {
  1914. /* program the dma controller with appropriate PRD buffers
  1915. * and start the DMA transfer for requested command.
  1916. */
  1917. pp->dmafis_bits |= (0x1 << nv_swncq_tag(ap));
  1918. pp->ncq_flags |= ncq_saw_dmas;
  1919. nv_swncq_dmafis(ap);
  1920. }
  1921. irq_exit:
  1922. return;
  1923. irq_error:
  1924. ata_ehi_push_desc(ehi, "fis:0x%x", fis);
  1925. ata_port_freeze(ap);
  1926. return;
  1927. }
  1928. static irqreturn_t nv_swncq_interrupt(int irq, void *dev_instance)
  1929. {
  1930. struct ata_host *host = dev_instance;
  1931. unsigned int i;
  1932. unsigned int handled = 0;
  1933. unsigned long flags;
  1934. u32 irq_stat;
  1935. spin_lock_irqsave(&host->lock, flags);
  1936. irq_stat = readl(host->iomap[NV_MMIO_BAR] + NV_INT_STATUS_MCP55);
  1937. for (i = 0; i < host->n_ports; i++) {
  1938. struct ata_port *ap = host->ports[i];
  1939. if (ap->link.sactive) {
  1940. nv_swncq_host_interrupt(ap, (u16)irq_stat);
  1941. handled = 1;
  1942. } else {
  1943. if (irq_stat) /* reserve Hotplug */
  1944. nv_swncq_irq_clear(ap, 0xfff0);
  1945. handled += nv_host_intr(ap, (u8)irq_stat);
  1946. }
  1947. irq_stat >>= NV_INT_PORT_SHIFT_MCP55;
  1948. }
  1949. spin_unlock_irqrestore(&host->lock, flags);
  1950. return IRQ_RETVAL(handled);
  1951. }
  1952. static int nv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  1953. {
  1954. const struct ata_port_info *ppi[] = { NULL, NULL };
  1955. struct nv_pi_priv *ipriv;
  1956. struct ata_host *host;
  1957. struct nv_host_priv *hpriv;
  1958. int rc;
  1959. u32 bar;
  1960. void __iomem *base;
  1961. unsigned long type = ent->driver_data;
  1962. // Make sure this is a SATA controller by counting the number of bars
  1963. // (NVIDIA SATA controllers will always have six bars). Otherwise,
  1964. // it's an IDE controller and we ignore it.
  1965. for (bar = 0; bar < 6; bar++)
  1966. if (pci_resource_start(pdev, bar) == 0)
  1967. return -ENODEV;
  1968. ata_print_version_once(&pdev->dev, DRV_VERSION);
  1969. rc = pcim_enable_device(pdev);
  1970. if (rc)
  1971. return rc;
  1972. /* determine type and allocate host */
  1973. if (type == CK804 && adma_enabled) {
  1974. dev_notice(&pdev->dev, "Using ADMA mode\n");
  1975. type = ADMA;
  1976. } else if (type == MCP5x && swncq_enabled) {
  1977. dev_notice(&pdev->dev, "Using SWNCQ mode\n");
  1978. type = SWNCQ;
  1979. }
  1980. ppi[0] = &nv_port_info[type];
  1981. ipriv = ppi[0]->private_data;
  1982. rc = ata_pci_bmdma_prepare_host(pdev, ppi, &host);
  1983. if (rc)
  1984. return rc;
  1985. hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
  1986. if (!hpriv)
  1987. return -ENOMEM;
  1988. hpriv->type = type;
  1989. host->private_data = hpriv;
  1990. /* request and iomap NV_MMIO_BAR */
  1991. rc = pcim_iomap_regions(pdev, 1 << NV_MMIO_BAR, DRV_NAME);
  1992. if (rc)
  1993. return rc;
  1994. /* configure SCR access */
  1995. base = host->iomap[NV_MMIO_BAR];
  1996. host->ports[0]->ioaddr.scr_addr = base + NV_PORT0_SCR_REG_OFFSET;
  1997. host->ports[1]->ioaddr.scr_addr = base + NV_PORT1_SCR_REG_OFFSET;
  1998. /* enable SATA space for CK804 */
  1999. if (type >= CK804) {
  2000. u8 regval;
  2001. pci_read_config_byte(pdev, NV_MCP_SATA_CFG_20, &regval);
  2002. regval |= NV_MCP_SATA_CFG_20_SATA_SPACE_EN;
  2003. pci_write_config_byte(pdev, NV_MCP_SATA_CFG_20, regval);
  2004. }
  2005. /* init ADMA */
  2006. if (type == ADMA) {
  2007. rc = nv_adma_host_init(host);
  2008. if (rc)
  2009. return rc;
  2010. } else if (type == SWNCQ)
  2011. nv_swncq_host_init(host);
  2012. if (msi_enabled) {
  2013. dev_notice(&pdev->dev, "Using MSI\n");
  2014. pci_enable_msi(pdev);
  2015. }
  2016. pci_set_master(pdev);
  2017. return ata_pci_sff_activate_host(host, ipriv->irq_handler, ipriv->sht);
  2018. }
  2019. #ifdef CONFIG_PM_SLEEP
  2020. static int nv_pci_device_resume(struct pci_dev *pdev)
  2021. {
  2022. struct ata_host *host = pci_get_drvdata(pdev);
  2023. struct nv_host_priv *hpriv = host->private_data;
  2024. int rc;
  2025. rc = ata_pci_device_do_resume(pdev);
  2026. if (rc)
  2027. return rc;
  2028. if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
  2029. if (hpriv->type >= CK804) {
  2030. u8 regval;
  2031. pci_read_config_byte(pdev, NV_MCP_SATA_CFG_20, &regval);
  2032. regval |= NV_MCP_SATA_CFG_20_SATA_SPACE_EN;
  2033. pci_write_config_byte(pdev, NV_MCP_SATA_CFG_20, regval);
  2034. }
  2035. if (hpriv->type == ADMA) {
  2036. u32 tmp32;
  2037. struct nv_adma_port_priv *pp;
  2038. /* enable/disable ADMA on the ports appropriately */
  2039. pci_read_config_dword(pdev, NV_MCP_SATA_CFG_20, &tmp32);
  2040. pp = host->ports[0]->private_data;
  2041. if (pp->flags & NV_ADMA_ATAPI_SETUP_COMPLETE)
  2042. tmp32 &= ~(NV_MCP_SATA_CFG_20_PORT0_EN |
  2043. NV_MCP_SATA_CFG_20_PORT0_PWB_EN);
  2044. else
  2045. tmp32 |= (NV_MCP_SATA_CFG_20_PORT0_EN |
  2046. NV_MCP_SATA_CFG_20_PORT0_PWB_EN);
  2047. pp = host->ports[1]->private_data;
  2048. if (pp->flags & NV_ADMA_ATAPI_SETUP_COMPLETE)
  2049. tmp32 &= ~(NV_MCP_SATA_CFG_20_PORT1_EN |
  2050. NV_MCP_SATA_CFG_20_PORT1_PWB_EN);
  2051. else
  2052. tmp32 |= (NV_MCP_SATA_CFG_20_PORT1_EN |
  2053. NV_MCP_SATA_CFG_20_PORT1_PWB_EN);
  2054. pci_write_config_dword(pdev, NV_MCP_SATA_CFG_20, tmp32);
  2055. }
  2056. }
  2057. ata_host_resume(host);
  2058. return 0;
  2059. }
  2060. #endif
  2061. static void nv_ck804_host_stop(struct ata_host *host)
  2062. {
  2063. struct pci_dev *pdev = to_pci_dev(host->dev);
  2064. u8 regval;
  2065. /* disable SATA space for CK804 */
  2066. pci_read_config_byte(pdev, NV_MCP_SATA_CFG_20, &regval);
  2067. regval &= ~NV_MCP_SATA_CFG_20_SATA_SPACE_EN;
  2068. pci_write_config_byte(pdev, NV_MCP_SATA_CFG_20, regval);
  2069. }
  2070. static void nv_adma_host_stop(struct ata_host *host)
  2071. {
  2072. struct pci_dev *pdev = to_pci_dev(host->dev);
  2073. u32 tmp32;
  2074. /* disable ADMA on the ports */
  2075. pci_read_config_dword(pdev, NV_MCP_SATA_CFG_20, &tmp32);
  2076. tmp32 &= ~(NV_MCP_SATA_CFG_20_PORT0_EN |
  2077. NV_MCP_SATA_CFG_20_PORT0_PWB_EN |
  2078. NV_MCP_SATA_CFG_20_PORT1_EN |
  2079. NV_MCP_SATA_CFG_20_PORT1_PWB_EN);
  2080. pci_write_config_dword(pdev, NV_MCP_SATA_CFG_20, tmp32);
  2081. nv_ck804_host_stop(host);
  2082. }
  2083. module_pci_driver(nv_pci_driver);
  2084. module_param_named(adma, adma_enabled, bool, 0444);
  2085. MODULE_PARM_DESC(adma, "Enable use of ADMA (Default: false)");
  2086. module_param_named(swncq, swncq_enabled, bool, 0444);
  2087. MODULE_PARM_DESC(swncq, "Enable use of SWNCQ (Default: true)");
  2088. module_param_named(msi, msi_enabled, bool, 0444);
  2089. MODULE_PARM_DESC(msi, "Enable use of MSI (Default: false)");