libata-sff.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773
  1. /*
  2. * libata-sff.c - helper library for PCI IDE BMDMA
  3. *
  4. * Maintained by: Jeff Garzik <jgarzik@pobox.com>
  5. * Please ALWAYS copy linux-ide@vger.kernel.org
  6. * on emails.
  7. *
  8. * Copyright 2003-2006 Red Hat, Inc. All rights reserved.
  9. * Copyright 2003-2006 Jeff Garzik
  10. *
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; see the file COPYING. If not, write to
  24. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. *
  27. * libata documentation is available via 'make {ps|pdf}docs',
  28. * as Documentation/DocBook/libata.*
  29. *
  30. * Hardware documentation available from http://www.t13.org/ and
  31. * http://www.sata-io.org/
  32. *
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/pci.h>
  36. #include <linux/libata.h>
  37. #include <linux/highmem.h>
  38. #include "libata.h"
  39. const struct ata_port_operations ata_sff_port_ops = {
  40. .inherits = &ata_base_port_ops,
  41. .qc_prep = ata_sff_qc_prep,
  42. .qc_issue = ata_sff_qc_issue,
  43. .freeze = ata_sff_freeze,
  44. .thaw = ata_sff_thaw,
  45. .softreset = ata_sff_softreset,
  46. .error_handler = ata_sff_error_handler,
  47. .post_internal_cmd = ata_sff_post_internal_cmd,
  48. .sff_dev_select = ata_sff_dev_select,
  49. .sff_check_status = ata_sff_check_status,
  50. .sff_tf_load = ata_sff_tf_load,
  51. .sff_tf_read = ata_sff_tf_read,
  52. .sff_exec_command = ata_sff_exec_command,
  53. .sff_data_xfer = ata_sff_data_xfer,
  54. .sff_irq_on = ata_sff_irq_on,
  55. .sff_irq_clear = ata_sff_irq_clear,
  56. .port_start = ata_sff_port_start,
  57. };
  58. const struct ata_port_operations ata_bmdma_port_ops = {
  59. .inherits = &ata_sff_port_ops,
  60. .mode_filter = ata_bmdma_mode_filter,
  61. .bmdma_setup = ata_bmdma_setup,
  62. .bmdma_start = ata_bmdma_start,
  63. .bmdma_stop = ata_bmdma_stop,
  64. .bmdma_status = ata_bmdma_status,
  65. };
  66. /**
  67. * ata_fill_sg - Fill PCI IDE PRD table
  68. * @qc: Metadata associated with taskfile to be transferred
  69. *
  70. * Fill PCI IDE PRD (scatter-gather) table with segments
  71. * associated with the current disk command.
  72. *
  73. * LOCKING:
  74. * spin_lock_irqsave(host lock)
  75. *
  76. */
  77. static void ata_fill_sg(struct ata_queued_cmd *qc)
  78. {
  79. struct ata_port *ap = qc->ap;
  80. struct scatterlist *sg;
  81. unsigned int si, pi;
  82. pi = 0;
  83. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  84. u32 addr, offset;
  85. u32 sg_len, len;
  86. /* determine if physical DMA addr spans 64K boundary.
  87. * Note h/w doesn't support 64-bit, so we unconditionally
  88. * truncate dma_addr_t to u32.
  89. */
  90. addr = (u32) sg_dma_address(sg);
  91. sg_len = sg_dma_len(sg);
  92. while (sg_len) {
  93. offset = addr & 0xffff;
  94. len = sg_len;
  95. if ((offset + sg_len) > 0x10000)
  96. len = 0x10000 - offset;
  97. ap->prd[pi].addr = cpu_to_le32(addr);
  98. ap->prd[pi].flags_len = cpu_to_le32(len & 0xffff);
  99. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
  100. pi++;
  101. sg_len -= len;
  102. addr += len;
  103. }
  104. }
  105. ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
  106. }
  107. /**
  108. * ata_fill_sg_dumb - Fill PCI IDE PRD table
  109. * @qc: Metadata associated with taskfile to be transferred
  110. *
  111. * Fill PCI IDE PRD (scatter-gather) table with segments
  112. * associated with the current disk command. Perform the fill
  113. * so that we avoid writing any length 64K records for
  114. * controllers that don't follow the spec.
  115. *
  116. * LOCKING:
  117. * spin_lock_irqsave(host lock)
  118. *
  119. */
  120. static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
  121. {
  122. struct ata_port *ap = qc->ap;
  123. struct scatterlist *sg;
  124. unsigned int si, pi;
  125. pi = 0;
  126. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  127. u32 addr, offset;
  128. u32 sg_len, len, blen;
  129. /* determine if physical DMA addr spans 64K boundary.
  130. * Note h/w doesn't support 64-bit, so we unconditionally
  131. * truncate dma_addr_t to u32.
  132. */
  133. addr = (u32) sg_dma_address(sg);
  134. sg_len = sg_dma_len(sg);
  135. while (sg_len) {
  136. offset = addr & 0xffff;
  137. len = sg_len;
  138. if ((offset + sg_len) > 0x10000)
  139. len = 0x10000 - offset;
  140. blen = len & 0xffff;
  141. ap->prd[pi].addr = cpu_to_le32(addr);
  142. if (blen == 0) {
  143. /* Some PATA chipsets like the CS5530 can't
  144. cope with 0x0000 meaning 64K as the spec says */
  145. ap->prd[pi].flags_len = cpu_to_le32(0x8000);
  146. blen = 0x8000;
  147. ap->prd[++pi].addr = cpu_to_le32(addr + 0x8000);
  148. }
  149. ap->prd[pi].flags_len = cpu_to_le32(blen);
  150. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
  151. pi++;
  152. sg_len -= len;
  153. addr += len;
  154. }
  155. }
  156. ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
  157. }
  158. /**
  159. * ata_sff_qc_prep - Prepare taskfile for submission
  160. * @qc: Metadata associated with taskfile to be prepared
  161. *
  162. * Prepare ATA taskfile for submission.
  163. *
  164. * LOCKING:
  165. * spin_lock_irqsave(host lock)
  166. */
  167. void ata_sff_qc_prep(struct ata_queued_cmd *qc)
  168. {
  169. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  170. return;
  171. ata_fill_sg(qc);
  172. }
  173. /**
  174. * ata_sff_dumb_qc_prep - Prepare taskfile for submission
  175. * @qc: Metadata associated with taskfile to be prepared
  176. *
  177. * Prepare ATA taskfile for submission.
  178. *
  179. * LOCKING:
  180. * spin_lock_irqsave(host lock)
  181. */
  182. void ata_sff_dumb_qc_prep(struct ata_queued_cmd *qc)
  183. {
  184. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  185. return;
  186. ata_fill_sg_dumb(qc);
  187. }
  188. /**
  189. * ata_sff_check_status - Read device status reg & clear interrupt
  190. * @ap: port where the device is
  191. *
  192. * Reads ATA taskfile status register for currently-selected device
  193. * and return its value. This also clears pending interrupts
  194. * from this device
  195. *
  196. * LOCKING:
  197. * Inherited from caller.
  198. */
  199. u8 ata_sff_check_status(struct ata_port *ap)
  200. {
  201. return ioread8(ap->ioaddr.status_addr);
  202. }
  203. /**
  204. * ata_sff_altstatus - Read device alternate status reg
  205. * @ap: port where the device is
  206. *
  207. * Reads ATA taskfile alternate status register for
  208. * currently-selected device and return its value.
  209. *
  210. * Note: may NOT be used as the check_altstatus() entry in
  211. * ata_port_operations.
  212. *
  213. * LOCKING:
  214. * Inherited from caller.
  215. */
  216. u8 ata_sff_altstatus(struct ata_port *ap)
  217. {
  218. if (ap->ops->sff_check_altstatus)
  219. return ap->ops->sff_check_altstatus(ap);
  220. return ioread8(ap->ioaddr.altstatus_addr);
  221. }
  222. /**
  223. * ata_sff_busy_sleep - sleep until BSY clears, or timeout
  224. * @ap: port containing status register to be polled
  225. * @tmout_pat: impatience timeout
  226. * @tmout: overall timeout
  227. *
  228. * Sleep until ATA Status register bit BSY clears,
  229. * or a timeout occurs.
  230. *
  231. * LOCKING:
  232. * Kernel thread context (may sleep).
  233. *
  234. * RETURNS:
  235. * 0 on success, -errno otherwise.
  236. */
  237. int ata_sff_busy_sleep(struct ata_port *ap,
  238. unsigned long tmout_pat, unsigned long tmout)
  239. {
  240. unsigned long timer_start, timeout;
  241. u8 status;
  242. status = ata_sff_busy_wait(ap, ATA_BUSY, 300);
  243. timer_start = jiffies;
  244. timeout = timer_start + tmout_pat;
  245. while (status != 0xff && (status & ATA_BUSY) &&
  246. time_before(jiffies, timeout)) {
  247. msleep(50);
  248. status = ata_sff_busy_wait(ap, ATA_BUSY, 3);
  249. }
  250. if (status != 0xff && (status & ATA_BUSY))
  251. ata_port_printk(ap, KERN_WARNING,
  252. "port is slow to respond, please be patient "
  253. "(Status 0x%x)\n", status);
  254. timeout = timer_start + tmout;
  255. while (status != 0xff && (status & ATA_BUSY) &&
  256. time_before(jiffies, timeout)) {
  257. msleep(50);
  258. status = ap->ops->sff_check_status(ap);
  259. }
  260. if (status == 0xff)
  261. return -ENODEV;
  262. if (status & ATA_BUSY) {
  263. ata_port_printk(ap, KERN_ERR, "port failed to respond "
  264. "(%lu secs, Status 0x%x)\n",
  265. tmout / HZ, status);
  266. return -EBUSY;
  267. }
  268. return 0;
  269. }
  270. /**
  271. * ata_sff_wait_ready - sleep until BSY clears, or timeout
  272. * @ap: port containing status register to be polled
  273. * @deadline: deadline jiffies for the operation
  274. *
  275. * Sleep until ATA Status register bit BSY clears, or timeout
  276. * occurs.
  277. *
  278. * LOCKING:
  279. * Kernel thread context (may sleep).
  280. *
  281. * RETURNS:
  282. * 0 on success, -errno otherwise.
  283. */
  284. int ata_sff_wait_ready(struct ata_port *ap, unsigned long deadline)
  285. {
  286. unsigned long start = jiffies;
  287. int warned = 0;
  288. while (1) {
  289. u8 status = ap->ops->sff_check_status(ap);
  290. unsigned long now = jiffies;
  291. if (!(status & ATA_BUSY))
  292. return 0;
  293. if (!ata_link_online(&ap->link) && status == 0xff)
  294. return -ENODEV;
  295. if (time_after(now, deadline))
  296. return -EBUSY;
  297. if (!warned && time_after(now, start + 5 * HZ) &&
  298. (deadline - now > 3 * HZ)) {
  299. ata_port_printk(ap, KERN_WARNING,
  300. "port is slow to respond, please be patient "
  301. "(Status 0x%x)\n", status);
  302. warned = 1;
  303. }
  304. msleep(50);
  305. }
  306. }
  307. /**
  308. * ata_sff_dev_select - Select device 0/1 on ATA bus
  309. * @ap: ATA channel to manipulate
  310. * @device: ATA device (numbered from zero) to select
  311. *
  312. * Use the method defined in the ATA specification to
  313. * make either device 0, or device 1, active on the
  314. * ATA channel. Works with both PIO and MMIO.
  315. *
  316. * May be used as the dev_select() entry in ata_port_operations.
  317. *
  318. * LOCKING:
  319. * caller.
  320. */
  321. void ata_sff_dev_select(struct ata_port *ap, unsigned int device)
  322. {
  323. u8 tmp;
  324. if (device == 0)
  325. tmp = ATA_DEVICE_OBS;
  326. else
  327. tmp = ATA_DEVICE_OBS | ATA_DEV1;
  328. iowrite8(tmp, ap->ioaddr.device_addr);
  329. ata_sff_pause(ap); /* needed; also flushes, for mmio */
  330. }
  331. /**
  332. * ata_dev_select - Select device 0/1 on ATA bus
  333. * @ap: ATA channel to manipulate
  334. * @device: ATA device (numbered from zero) to select
  335. * @wait: non-zero to wait for Status register BSY bit to clear
  336. * @can_sleep: non-zero if context allows sleeping
  337. *
  338. * Use the method defined in the ATA specification to
  339. * make either device 0, or device 1, active on the
  340. * ATA channel.
  341. *
  342. * This is a high-level version of ata_sff_dev_select(), which
  343. * additionally provides the services of inserting the proper
  344. * pauses and status polling, where needed.
  345. *
  346. * LOCKING:
  347. * caller.
  348. */
  349. void ata_dev_select(struct ata_port *ap, unsigned int device,
  350. unsigned int wait, unsigned int can_sleep)
  351. {
  352. if (ata_msg_probe(ap))
  353. ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, "
  354. "device %u, wait %u\n", device, wait);
  355. if (wait)
  356. ata_wait_idle(ap);
  357. ap->ops->sff_dev_select(ap, device);
  358. if (wait) {
  359. if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI)
  360. msleep(150);
  361. ata_wait_idle(ap);
  362. }
  363. }
  364. /**
  365. * ata_sff_irq_on - Enable interrupts on a port.
  366. * @ap: Port on which interrupts are enabled.
  367. *
  368. * Enable interrupts on a legacy IDE device using MMIO or PIO,
  369. * wait for idle, clear any pending interrupts.
  370. *
  371. * LOCKING:
  372. * Inherited from caller.
  373. */
  374. u8 ata_sff_irq_on(struct ata_port *ap)
  375. {
  376. struct ata_ioports *ioaddr = &ap->ioaddr;
  377. u8 tmp;
  378. ap->ctl &= ~ATA_NIEN;
  379. ap->last_ctl = ap->ctl;
  380. if (ioaddr->ctl_addr)
  381. iowrite8(ap->ctl, ioaddr->ctl_addr);
  382. tmp = ata_wait_idle(ap);
  383. ap->ops->sff_irq_clear(ap);
  384. return tmp;
  385. }
  386. /**
  387. * ata_sff_irq_clear - Clear PCI IDE BMDMA interrupt.
  388. * @ap: Port associated with this ATA transaction.
  389. *
  390. * Clear interrupt and error flags in DMA status register.
  391. *
  392. * May be used as the irq_clear() entry in ata_port_operations.
  393. *
  394. * LOCKING:
  395. * spin_lock_irqsave(host lock)
  396. */
  397. void ata_sff_irq_clear(struct ata_port *ap)
  398. {
  399. void __iomem *mmio = ap->ioaddr.bmdma_addr;
  400. if (!mmio)
  401. return;
  402. iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS);
  403. }
  404. /**
  405. * ata_sff_tf_load - send taskfile registers to host controller
  406. * @ap: Port to which output is sent
  407. * @tf: ATA taskfile register set
  408. *
  409. * Outputs ATA taskfile to standard ATA host controller.
  410. *
  411. * LOCKING:
  412. * Inherited from caller.
  413. */
  414. void ata_sff_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
  415. {
  416. struct ata_ioports *ioaddr = &ap->ioaddr;
  417. unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
  418. if (tf->ctl != ap->last_ctl) {
  419. if (ioaddr->ctl_addr)
  420. iowrite8(tf->ctl, ioaddr->ctl_addr);
  421. ap->last_ctl = tf->ctl;
  422. ata_wait_idle(ap);
  423. }
  424. if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
  425. WARN_ON(!ioaddr->ctl_addr);
  426. iowrite8(tf->hob_feature, ioaddr->feature_addr);
  427. iowrite8(tf->hob_nsect, ioaddr->nsect_addr);
  428. iowrite8(tf->hob_lbal, ioaddr->lbal_addr);
  429. iowrite8(tf->hob_lbam, ioaddr->lbam_addr);
  430. iowrite8(tf->hob_lbah, ioaddr->lbah_addr);
  431. VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
  432. tf->hob_feature,
  433. tf->hob_nsect,
  434. tf->hob_lbal,
  435. tf->hob_lbam,
  436. tf->hob_lbah);
  437. }
  438. if (is_addr) {
  439. iowrite8(tf->feature, ioaddr->feature_addr);
  440. iowrite8(tf->nsect, ioaddr->nsect_addr);
  441. iowrite8(tf->lbal, ioaddr->lbal_addr);
  442. iowrite8(tf->lbam, ioaddr->lbam_addr);
  443. iowrite8(tf->lbah, ioaddr->lbah_addr);
  444. VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
  445. tf->feature,
  446. tf->nsect,
  447. tf->lbal,
  448. tf->lbam,
  449. tf->lbah);
  450. }
  451. if (tf->flags & ATA_TFLAG_DEVICE) {
  452. iowrite8(tf->device, ioaddr->device_addr);
  453. VPRINTK("device 0x%X\n", tf->device);
  454. }
  455. ata_wait_idle(ap);
  456. }
  457. /**
  458. * ata_sff_tf_read - input device's ATA taskfile shadow registers
  459. * @ap: Port from which input is read
  460. * @tf: ATA taskfile register set for storing input
  461. *
  462. * Reads ATA taskfile registers for currently-selected device
  463. * into @tf. Assumes the device has a fully SFF compliant task file
  464. * layout and behaviour. If you device does not (eg has a different
  465. * status method) then you will need to provide a replacement tf_read
  466. *
  467. * LOCKING:
  468. * Inherited from caller.
  469. */
  470. void ata_sff_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  471. {
  472. struct ata_ioports *ioaddr = &ap->ioaddr;
  473. tf->command = ata_sff_check_status(ap);
  474. tf->feature = ioread8(ioaddr->error_addr);
  475. tf->nsect = ioread8(ioaddr->nsect_addr);
  476. tf->lbal = ioread8(ioaddr->lbal_addr);
  477. tf->lbam = ioread8(ioaddr->lbam_addr);
  478. tf->lbah = ioread8(ioaddr->lbah_addr);
  479. tf->device = ioread8(ioaddr->device_addr);
  480. if (tf->flags & ATA_TFLAG_LBA48) {
  481. if (likely(ioaddr->ctl_addr)) {
  482. iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
  483. tf->hob_feature = ioread8(ioaddr->error_addr);
  484. tf->hob_nsect = ioread8(ioaddr->nsect_addr);
  485. tf->hob_lbal = ioread8(ioaddr->lbal_addr);
  486. tf->hob_lbam = ioread8(ioaddr->lbam_addr);
  487. tf->hob_lbah = ioread8(ioaddr->lbah_addr);
  488. iowrite8(tf->ctl, ioaddr->ctl_addr);
  489. ap->last_ctl = tf->ctl;
  490. } else
  491. WARN_ON(1);
  492. }
  493. }
  494. /**
  495. * ata_sff_exec_command - issue ATA command to host controller
  496. * @ap: port to which command is being issued
  497. * @tf: ATA taskfile register set
  498. *
  499. * Issues ATA command, with proper synchronization with interrupt
  500. * handler / other threads.
  501. *
  502. * LOCKING:
  503. * spin_lock_irqsave(host lock)
  504. */
  505. void ata_sff_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
  506. {
  507. DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
  508. iowrite8(tf->command, ap->ioaddr.command_addr);
  509. ata_sff_pause(ap);
  510. }
  511. /**
  512. * ata_tf_to_host - issue ATA taskfile to host controller
  513. * @ap: port to which command is being issued
  514. * @tf: ATA taskfile register set
  515. *
  516. * Issues ATA taskfile register set to ATA host controller,
  517. * with proper synchronization with interrupt handler and
  518. * other threads.
  519. *
  520. * LOCKING:
  521. * spin_lock_irqsave(host lock)
  522. */
  523. static inline void ata_tf_to_host(struct ata_port *ap,
  524. const struct ata_taskfile *tf)
  525. {
  526. ap->ops->sff_tf_load(ap, tf);
  527. ap->ops->sff_exec_command(ap, tf);
  528. }
  529. /**
  530. * ata_sff_data_xfer - Transfer data by PIO
  531. * @dev: device to target
  532. * @buf: data buffer
  533. * @buflen: buffer length
  534. * @rw: read/write
  535. *
  536. * Transfer data from/to the device data register by PIO.
  537. *
  538. * LOCKING:
  539. * Inherited from caller.
  540. *
  541. * RETURNS:
  542. * Bytes consumed.
  543. */
  544. unsigned int ata_sff_data_xfer(struct ata_device *dev, unsigned char *buf,
  545. unsigned int buflen, int rw)
  546. {
  547. struct ata_port *ap = dev->link->ap;
  548. void __iomem *data_addr = ap->ioaddr.data_addr;
  549. unsigned int words = buflen >> 1;
  550. /* Transfer multiple of 2 bytes */
  551. if (rw == READ)
  552. ioread16_rep(data_addr, buf, words);
  553. else
  554. iowrite16_rep(data_addr, buf, words);
  555. /* Transfer trailing 1 byte, if any. */
  556. if (unlikely(buflen & 0x01)) {
  557. __le16 align_buf[1] = { 0 };
  558. unsigned char *trailing_buf = buf + buflen - 1;
  559. if (rw == READ) {
  560. align_buf[0] = cpu_to_le16(ioread16(data_addr));
  561. memcpy(trailing_buf, align_buf, 1);
  562. } else {
  563. memcpy(align_buf, trailing_buf, 1);
  564. iowrite16(le16_to_cpu(align_buf[0]), data_addr);
  565. }
  566. words++;
  567. }
  568. return words << 1;
  569. }
  570. /**
  571. * ata_sff_data_xfer_noirq - Transfer data by PIO
  572. * @dev: device to target
  573. * @buf: data buffer
  574. * @buflen: buffer length
  575. * @rw: read/write
  576. *
  577. * Transfer data from/to the device data register by PIO. Do the
  578. * transfer with interrupts disabled.
  579. *
  580. * LOCKING:
  581. * Inherited from caller.
  582. *
  583. * RETURNS:
  584. * Bytes consumed.
  585. */
  586. unsigned int ata_sff_data_xfer_noirq(struct ata_device *dev, unsigned char *buf,
  587. unsigned int buflen, int rw)
  588. {
  589. unsigned long flags;
  590. unsigned int consumed;
  591. local_irq_save(flags);
  592. consumed = ata_sff_data_xfer(dev, buf, buflen, rw);
  593. local_irq_restore(flags);
  594. return consumed;
  595. }
  596. /**
  597. * ata_pio_sector - Transfer a sector of data.
  598. * @qc: Command on going
  599. *
  600. * Transfer qc->sect_size bytes of data from/to the ATA device.
  601. *
  602. * LOCKING:
  603. * Inherited from caller.
  604. */
  605. static void ata_pio_sector(struct ata_queued_cmd *qc)
  606. {
  607. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  608. struct ata_port *ap = qc->ap;
  609. struct page *page;
  610. unsigned int offset;
  611. unsigned char *buf;
  612. if (qc->curbytes == qc->nbytes - qc->sect_size)
  613. ap->hsm_task_state = HSM_ST_LAST;
  614. page = sg_page(qc->cursg);
  615. offset = qc->cursg->offset + qc->cursg_ofs;
  616. /* get the current page and offset */
  617. page = nth_page(page, (offset >> PAGE_SHIFT));
  618. offset %= PAGE_SIZE;
  619. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  620. if (PageHighMem(page)) {
  621. unsigned long flags;
  622. /* FIXME: use a bounce buffer */
  623. local_irq_save(flags);
  624. buf = kmap_atomic(page, KM_IRQ0);
  625. /* do the actual data transfer */
  626. ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
  627. do_write);
  628. kunmap_atomic(buf, KM_IRQ0);
  629. local_irq_restore(flags);
  630. } else {
  631. buf = page_address(page);
  632. ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
  633. do_write);
  634. }
  635. qc->curbytes += qc->sect_size;
  636. qc->cursg_ofs += qc->sect_size;
  637. if (qc->cursg_ofs == qc->cursg->length) {
  638. qc->cursg = sg_next(qc->cursg);
  639. qc->cursg_ofs = 0;
  640. }
  641. }
  642. /**
  643. * ata_pio_sectors - Transfer one or many sectors.
  644. * @qc: Command on going
  645. *
  646. * Transfer one or many sectors of data from/to the
  647. * ATA device for the DRQ request.
  648. *
  649. * LOCKING:
  650. * Inherited from caller.
  651. */
  652. static void ata_pio_sectors(struct ata_queued_cmd *qc)
  653. {
  654. if (is_multi_taskfile(&qc->tf)) {
  655. /* READ/WRITE MULTIPLE */
  656. unsigned int nsect;
  657. WARN_ON(qc->dev->multi_count == 0);
  658. nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
  659. qc->dev->multi_count);
  660. while (nsect--)
  661. ata_pio_sector(qc);
  662. } else
  663. ata_pio_sector(qc);
  664. ata_sff_altstatus(qc->ap); /* flush */
  665. }
  666. /**
  667. * atapi_send_cdb - Write CDB bytes to hardware
  668. * @ap: Port to which ATAPI device is attached.
  669. * @qc: Taskfile currently active
  670. *
  671. * When device has indicated its readiness to accept
  672. * a CDB, this function is called. Send the CDB.
  673. *
  674. * LOCKING:
  675. * caller.
  676. */
  677. static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
  678. {
  679. /* send SCSI cdb */
  680. DPRINTK("send cdb\n");
  681. WARN_ON(qc->dev->cdb_len < 12);
  682. ap->ops->sff_data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
  683. ata_sff_altstatus(ap); /* flush */
  684. switch (qc->tf.protocol) {
  685. case ATAPI_PROT_PIO:
  686. ap->hsm_task_state = HSM_ST;
  687. break;
  688. case ATAPI_PROT_NODATA:
  689. ap->hsm_task_state = HSM_ST_LAST;
  690. break;
  691. case ATAPI_PROT_DMA:
  692. ap->hsm_task_state = HSM_ST_LAST;
  693. /* initiate bmdma */
  694. ap->ops->bmdma_start(qc);
  695. break;
  696. }
  697. }
  698. /**
  699. * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
  700. * @qc: Command on going
  701. * @bytes: number of bytes
  702. *
  703. * Transfer Transfer data from/to the ATAPI device.
  704. *
  705. * LOCKING:
  706. * Inherited from caller.
  707. *
  708. */
  709. static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
  710. {
  711. int rw = (qc->tf.flags & ATA_TFLAG_WRITE) ? WRITE : READ;
  712. struct ata_port *ap = qc->ap;
  713. struct ata_device *dev = qc->dev;
  714. struct ata_eh_info *ehi = &dev->link->eh_info;
  715. struct scatterlist *sg;
  716. struct page *page;
  717. unsigned char *buf;
  718. unsigned int offset, count, consumed;
  719. next_sg:
  720. sg = qc->cursg;
  721. if (unlikely(!sg)) {
  722. ata_ehi_push_desc(ehi, "unexpected or too much trailing data "
  723. "buf=%u cur=%u bytes=%u",
  724. qc->nbytes, qc->curbytes, bytes);
  725. return -1;
  726. }
  727. page = sg_page(sg);
  728. offset = sg->offset + qc->cursg_ofs;
  729. /* get the current page and offset */
  730. page = nth_page(page, (offset >> PAGE_SHIFT));
  731. offset %= PAGE_SIZE;
  732. /* don't overrun current sg */
  733. count = min(sg->length - qc->cursg_ofs, bytes);
  734. /* don't cross page boundaries */
  735. count = min(count, (unsigned int)PAGE_SIZE - offset);
  736. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  737. if (PageHighMem(page)) {
  738. unsigned long flags;
  739. /* FIXME: use bounce buffer */
  740. local_irq_save(flags);
  741. buf = kmap_atomic(page, KM_IRQ0);
  742. /* do the actual data transfer */
  743. consumed = ap->ops->sff_data_xfer(dev, buf + offset, count, rw);
  744. kunmap_atomic(buf, KM_IRQ0);
  745. local_irq_restore(flags);
  746. } else {
  747. buf = page_address(page);
  748. consumed = ap->ops->sff_data_xfer(dev, buf + offset, count, rw);
  749. }
  750. bytes -= min(bytes, consumed);
  751. qc->curbytes += count;
  752. qc->cursg_ofs += count;
  753. if (qc->cursg_ofs == sg->length) {
  754. qc->cursg = sg_next(qc->cursg);
  755. qc->cursg_ofs = 0;
  756. }
  757. /* consumed can be larger than count only for the last transfer */
  758. WARN_ON(qc->cursg && count != consumed);
  759. if (bytes)
  760. goto next_sg;
  761. return 0;
  762. }
  763. /**
  764. * atapi_pio_bytes - Transfer data from/to the ATAPI device.
  765. * @qc: Command on going
  766. *
  767. * Transfer Transfer data from/to the ATAPI device.
  768. *
  769. * LOCKING:
  770. * Inherited from caller.
  771. */
  772. static void atapi_pio_bytes(struct ata_queued_cmd *qc)
  773. {
  774. struct ata_port *ap = qc->ap;
  775. struct ata_device *dev = qc->dev;
  776. struct ata_eh_info *ehi = &dev->link->eh_info;
  777. unsigned int ireason, bc_lo, bc_hi, bytes;
  778. int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
  779. /* Abuse qc->result_tf for temp storage of intermediate TF
  780. * here to save some kernel stack usage.
  781. * For normal completion, qc->result_tf is not relevant. For
  782. * error, qc->result_tf is later overwritten by ata_qc_complete().
  783. * So, the correctness of qc->result_tf is not affected.
  784. */
  785. ap->ops->sff_tf_read(ap, &qc->result_tf);
  786. ireason = qc->result_tf.nsect;
  787. bc_lo = qc->result_tf.lbam;
  788. bc_hi = qc->result_tf.lbah;
  789. bytes = (bc_hi << 8) | bc_lo;
  790. /* shall be cleared to zero, indicating xfer of data */
  791. if (unlikely(ireason & (1 << 0)))
  792. goto atapi_check;
  793. /* make sure transfer direction matches expected */
  794. i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
  795. if (unlikely(do_write != i_write))
  796. goto atapi_check;
  797. if (unlikely(!bytes))
  798. goto atapi_check;
  799. VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
  800. if (unlikely(__atapi_pio_bytes(qc, bytes)))
  801. goto err_out;
  802. ata_sff_altstatus(ap); /* flush */
  803. return;
  804. atapi_check:
  805. ata_ehi_push_desc(ehi, "ATAPI check failed (ireason=0x%x bytes=%u)",
  806. ireason, bytes);
  807. err_out:
  808. qc->err_mask |= AC_ERR_HSM;
  809. ap->hsm_task_state = HSM_ST_ERR;
  810. }
  811. /**
  812. * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
  813. * @ap: the target ata_port
  814. * @qc: qc on going
  815. *
  816. * RETURNS:
  817. * 1 if ok in workqueue, 0 otherwise.
  818. */
  819. static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
  820. {
  821. if (qc->tf.flags & ATA_TFLAG_POLLING)
  822. return 1;
  823. if (ap->hsm_task_state == HSM_ST_FIRST) {
  824. if (qc->tf.protocol == ATA_PROT_PIO &&
  825. (qc->tf.flags & ATA_TFLAG_WRITE))
  826. return 1;
  827. if (ata_is_atapi(qc->tf.protocol) &&
  828. !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  829. return 1;
  830. }
  831. return 0;
  832. }
  833. /**
  834. * ata_hsm_qc_complete - finish a qc running on standard HSM
  835. * @qc: Command to complete
  836. * @in_wq: 1 if called from workqueue, 0 otherwise
  837. *
  838. * Finish @qc which is running on standard HSM.
  839. *
  840. * LOCKING:
  841. * If @in_wq is zero, spin_lock_irqsave(host lock).
  842. * Otherwise, none on entry and grabs host lock.
  843. */
  844. static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
  845. {
  846. struct ata_port *ap = qc->ap;
  847. unsigned long flags;
  848. if (ap->ops->error_handler) {
  849. if (in_wq) {
  850. spin_lock_irqsave(ap->lock, flags);
  851. /* EH might have kicked in while host lock is
  852. * released.
  853. */
  854. qc = ata_qc_from_tag(ap, qc->tag);
  855. if (qc) {
  856. if (likely(!(qc->err_mask & AC_ERR_HSM))) {
  857. ap->ops->sff_irq_on(ap);
  858. ata_qc_complete(qc);
  859. } else
  860. ata_port_freeze(ap);
  861. }
  862. spin_unlock_irqrestore(ap->lock, flags);
  863. } else {
  864. if (likely(!(qc->err_mask & AC_ERR_HSM)))
  865. ata_qc_complete(qc);
  866. else
  867. ata_port_freeze(ap);
  868. }
  869. } else {
  870. if (in_wq) {
  871. spin_lock_irqsave(ap->lock, flags);
  872. ap->ops->sff_irq_on(ap);
  873. ata_qc_complete(qc);
  874. spin_unlock_irqrestore(ap->lock, flags);
  875. } else
  876. ata_qc_complete(qc);
  877. }
  878. }
  879. /**
  880. * ata_sff_hsm_move - move the HSM to the next state.
  881. * @ap: the target ata_port
  882. * @qc: qc on going
  883. * @status: current device status
  884. * @in_wq: 1 if called from workqueue, 0 otherwise
  885. *
  886. * RETURNS:
  887. * 1 when poll next status needed, 0 otherwise.
  888. */
  889. int ata_sff_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
  890. u8 status, int in_wq)
  891. {
  892. unsigned long flags = 0;
  893. int poll_next;
  894. WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
  895. /* Make sure ata_sff_qc_issue() does not throw things
  896. * like DMA polling into the workqueue. Notice that
  897. * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
  898. */
  899. WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
  900. fsm_start:
  901. DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
  902. ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
  903. switch (ap->hsm_task_state) {
  904. case HSM_ST_FIRST:
  905. /* Send first data block or PACKET CDB */
  906. /* If polling, we will stay in the work queue after
  907. * sending the data. Otherwise, interrupt handler
  908. * takes over after sending the data.
  909. */
  910. poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
  911. /* check device status */
  912. if (unlikely((status & ATA_DRQ) == 0)) {
  913. /* handle BSY=0, DRQ=0 as error */
  914. if (likely(status & (ATA_ERR | ATA_DF)))
  915. /* device stops HSM for abort/error */
  916. qc->err_mask |= AC_ERR_DEV;
  917. else
  918. /* HSM violation. Let EH handle this */
  919. qc->err_mask |= AC_ERR_HSM;
  920. ap->hsm_task_state = HSM_ST_ERR;
  921. goto fsm_start;
  922. }
  923. /* Device should not ask for data transfer (DRQ=1)
  924. * when it finds something wrong.
  925. * We ignore DRQ here and stop the HSM by
  926. * changing hsm_task_state to HSM_ST_ERR and
  927. * let the EH abort the command or reset the device.
  928. */
  929. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  930. /* Some ATAPI tape drives forget to clear the ERR bit
  931. * when doing the next command (mostly request sense).
  932. * We ignore ERR here to workaround and proceed sending
  933. * the CDB.
  934. */
  935. if (!(qc->dev->horkage & ATA_HORKAGE_STUCK_ERR)) {
  936. ata_port_printk(ap, KERN_WARNING,
  937. "DRQ=1 with device error, "
  938. "dev_stat 0x%X\n", status);
  939. qc->err_mask |= AC_ERR_HSM;
  940. ap->hsm_task_state = HSM_ST_ERR;
  941. goto fsm_start;
  942. }
  943. }
  944. /* Send the CDB (atapi) or the first data block (ata pio out).
  945. * During the state transition, interrupt handler shouldn't
  946. * be invoked before the data transfer is complete and
  947. * hsm_task_state is changed. Hence, the following locking.
  948. */
  949. if (in_wq)
  950. spin_lock_irqsave(ap->lock, flags);
  951. if (qc->tf.protocol == ATA_PROT_PIO) {
  952. /* PIO data out protocol.
  953. * send first data block.
  954. */
  955. /* ata_pio_sectors() might change the state
  956. * to HSM_ST_LAST. so, the state is changed here
  957. * before ata_pio_sectors().
  958. */
  959. ap->hsm_task_state = HSM_ST;
  960. ata_pio_sectors(qc);
  961. } else
  962. /* send CDB */
  963. atapi_send_cdb(ap, qc);
  964. if (in_wq)
  965. spin_unlock_irqrestore(ap->lock, flags);
  966. /* if polling, ata_pio_task() handles the rest.
  967. * otherwise, interrupt handler takes over from here.
  968. */
  969. break;
  970. case HSM_ST:
  971. /* complete command or read/write the data register */
  972. if (qc->tf.protocol == ATAPI_PROT_PIO) {
  973. /* ATAPI PIO protocol */
  974. if ((status & ATA_DRQ) == 0) {
  975. /* No more data to transfer or device error.
  976. * Device error will be tagged in HSM_ST_LAST.
  977. */
  978. ap->hsm_task_state = HSM_ST_LAST;
  979. goto fsm_start;
  980. }
  981. /* Device should not ask for data transfer (DRQ=1)
  982. * when it finds something wrong.
  983. * We ignore DRQ here and stop the HSM by
  984. * changing hsm_task_state to HSM_ST_ERR and
  985. * let the EH abort the command or reset the device.
  986. */
  987. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  988. ata_port_printk(ap, KERN_WARNING, "DRQ=1 with "
  989. "device error, dev_stat 0x%X\n",
  990. status);
  991. qc->err_mask |= AC_ERR_HSM;
  992. ap->hsm_task_state = HSM_ST_ERR;
  993. goto fsm_start;
  994. }
  995. atapi_pio_bytes(qc);
  996. if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
  997. /* bad ireason reported by device */
  998. goto fsm_start;
  999. } else {
  1000. /* ATA PIO protocol */
  1001. if (unlikely((status & ATA_DRQ) == 0)) {
  1002. /* handle BSY=0, DRQ=0 as error */
  1003. if (likely(status & (ATA_ERR | ATA_DF)))
  1004. /* device stops HSM for abort/error */
  1005. qc->err_mask |= AC_ERR_DEV;
  1006. else
  1007. /* HSM violation. Let EH handle this.
  1008. * Phantom devices also trigger this
  1009. * condition. Mark hint.
  1010. */
  1011. qc->err_mask |= AC_ERR_HSM |
  1012. AC_ERR_NODEV_HINT;
  1013. ap->hsm_task_state = HSM_ST_ERR;
  1014. goto fsm_start;
  1015. }
  1016. /* For PIO reads, some devices may ask for
  1017. * data transfer (DRQ=1) alone with ERR=1.
  1018. * We respect DRQ here and transfer one
  1019. * block of junk data before changing the
  1020. * hsm_task_state to HSM_ST_ERR.
  1021. *
  1022. * For PIO writes, ERR=1 DRQ=1 doesn't make
  1023. * sense since the data block has been
  1024. * transferred to the device.
  1025. */
  1026. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  1027. /* data might be corrputed */
  1028. qc->err_mask |= AC_ERR_DEV;
  1029. if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
  1030. ata_pio_sectors(qc);
  1031. status = ata_wait_idle(ap);
  1032. }
  1033. if (status & (ATA_BUSY | ATA_DRQ))
  1034. qc->err_mask |= AC_ERR_HSM;
  1035. /* ata_pio_sectors() might change the
  1036. * state to HSM_ST_LAST. so, the state
  1037. * is changed after ata_pio_sectors().
  1038. */
  1039. ap->hsm_task_state = HSM_ST_ERR;
  1040. goto fsm_start;
  1041. }
  1042. ata_pio_sectors(qc);
  1043. if (ap->hsm_task_state == HSM_ST_LAST &&
  1044. (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
  1045. /* all data read */
  1046. status = ata_wait_idle(ap);
  1047. goto fsm_start;
  1048. }
  1049. }
  1050. poll_next = 1;
  1051. break;
  1052. case HSM_ST_LAST:
  1053. if (unlikely(!ata_ok(status))) {
  1054. qc->err_mask |= __ac_err_mask(status);
  1055. ap->hsm_task_state = HSM_ST_ERR;
  1056. goto fsm_start;
  1057. }
  1058. /* no more data to transfer */
  1059. DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
  1060. ap->print_id, qc->dev->devno, status);
  1061. WARN_ON(qc->err_mask);
  1062. ap->hsm_task_state = HSM_ST_IDLE;
  1063. /* complete taskfile transaction */
  1064. ata_hsm_qc_complete(qc, in_wq);
  1065. poll_next = 0;
  1066. break;
  1067. case HSM_ST_ERR:
  1068. /* make sure qc->err_mask is available to
  1069. * know what's wrong and recover
  1070. */
  1071. WARN_ON(qc->err_mask == 0);
  1072. ap->hsm_task_state = HSM_ST_IDLE;
  1073. /* complete taskfile transaction */
  1074. ata_hsm_qc_complete(qc, in_wq);
  1075. poll_next = 0;
  1076. break;
  1077. default:
  1078. poll_next = 0;
  1079. BUG();
  1080. }
  1081. return poll_next;
  1082. }
  1083. void ata_pio_task(struct work_struct *work)
  1084. {
  1085. struct ata_port *ap =
  1086. container_of(work, struct ata_port, port_task.work);
  1087. struct ata_queued_cmd *qc = ap->port_task_data;
  1088. u8 status;
  1089. int poll_next;
  1090. fsm_start:
  1091. WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
  1092. /*
  1093. * This is purely heuristic. This is a fast path.
  1094. * Sometimes when we enter, BSY will be cleared in
  1095. * a chk-status or two. If not, the drive is probably seeking
  1096. * or something. Snooze for a couple msecs, then
  1097. * chk-status again. If still busy, queue delayed work.
  1098. */
  1099. status = ata_sff_busy_wait(ap, ATA_BUSY, 5);
  1100. if (status & ATA_BUSY) {
  1101. msleep(2);
  1102. status = ata_sff_busy_wait(ap, ATA_BUSY, 10);
  1103. if (status & ATA_BUSY) {
  1104. ata_pio_queue_task(ap, qc, ATA_SHORT_PAUSE);
  1105. return;
  1106. }
  1107. }
  1108. /* move the HSM */
  1109. poll_next = ata_sff_hsm_move(ap, qc, status, 1);
  1110. /* another command or interrupt handler
  1111. * may be running at this point.
  1112. */
  1113. if (poll_next)
  1114. goto fsm_start;
  1115. }
  1116. /**
  1117. * ata_sff_qc_issue - issue taskfile to device in proto-dependent manner
  1118. * @qc: command to issue to device
  1119. *
  1120. * Using various libata functions and hooks, this function
  1121. * starts an ATA command. ATA commands are grouped into
  1122. * classes called "protocols", and issuing each type of protocol
  1123. * is slightly different.
  1124. *
  1125. * May be used as the qc_issue() entry in ata_port_operations.
  1126. *
  1127. * LOCKING:
  1128. * spin_lock_irqsave(host lock)
  1129. *
  1130. * RETURNS:
  1131. * Zero on success, AC_ERR_* mask on failure
  1132. */
  1133. unsigned int ata_sff_qc_issue(struct ata_queued_cmd *qc)
  1134. {
  1135. struct ata_port *ap = qc->ap;
  1136. /* Use polling pio if the LLD doesn't handle
  1137. * interrupt driven pio and atapi CDB interrupt.
  1138. */
  1139. if (ap->flags & ATA_FLAG_PIO_POLLING) {
  1140. switch (qc->tf.protocol) {
  1141. case ATA_PROT_PIO:
  1142. case ATA_PROT_NODATA:
  1143. case ATAPI_PROT_PIO:
  1144. case ATAPI_PROT_NODATA:
  1145. qc->tf.flags |= ATA_TFLAG_POLLING;
  1146. break;
  1147. case ATAPI_PROT_DMA:
  1148. if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
  1149. /* see ata_dma_blacklisted() */
  1150. BUG();
  1151. break;
  1152. default:
  1153. break;
  1154. }
  1155. }
  1156. /* select the device */
  1157. ata_dev_select(ap, qc->dev->devno, 1, 0);
  1158. /* start the command */
  1159. switch (qc->tf.protocol) {
  1160. case ATA_PROT_NODATA:
  1161. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1162. ata_qc_set_polling(qc);
  1163. ata_tf_to_host(ap, &qc->tf);
  1164. ap->hsm_task_state = HSM_ST_LAST;
  1165. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1166. ata_pio_queue_task(ap, qc, 0);
  1167. break;
  1168. case ATA_PROT_DMA:
  1169. WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
  1170. ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
  1171. ap->ops->bmdma_setup(qc); /* set up bmdma */
  1172. ap->ops->bmdma_start(qc); /* initiate bmdma */
  1173. ap->hsm_task_state = HSM_ST_LAST;
  1174. break;
  1175. case ATA_PROT_PIO:
  1176. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1177. ata_qc_set_polling(qc);
  1178. ata_tf_to_host(ap, &qc->tf);
  1179. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  1180. /* PIO data out protocol */
  1181. ap->hsm_task_state = HSM_ST_FIRST;
  1182. ata_pio_queue_task(ap, qc, 0);
  1183. /* always send first data block using
  1184. * the ata_pio_task() codepath.
  1185. */
  1186. } else {
  1187. /* PIO data in protocol */
  1188. ap->hsm_task_state = HSM_ST;
  1189. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1190. ata_pio_queue_task(ap, qc, 0);
  1191. /* if polling, ata_pio_task() handles the rest.
  1192. * otherwise, interrupt handler takes over from here.
  1193. */
  1194. }
  1195. break;
  1196. case ATAPI_PROT_PIO:
  1197. case ATAPI_PROT_NODATA:
  1198. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1199. ata_qc_set_polling(qc);
  1200. ata_tf_to_host(ap, &qc->tf);
  1201. ap->hsm_task_state = HSM_ST_FIRST;
  1202. /* send cdb by polling if no cdb interrupt */
  1203. if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
  1204. (qc->tf.flags & ATA_TFLAG_POLLING))
  1205. ata_pio_queue_task(ap, qc, 0);
  1206. break;
  1207. case ATAPI_PROT_DMA:
  1208. WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
  1209. ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
  1210. ap->ops->bmdma_setup(qc); /* set up bmdma */
  1211. ap->hsm_task_state = HSM_ST_FIRST;
  1212. /* send cdb by polling if no cdb interrupt */
  1213. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  1214. ata_pio_queue_task(ap, qc, 0);
  1215. break;
  1216. default:
  1217. WARN_ON(1);
  1218. return AC_ERR_SYSTEM;
  1219. }
  1220. return 0;
  1221. }
  1222. /**
  1223. * ata_sff_host_intr - Handle host interrupt for given (port, task)
  1224. * @ap: Port on which interrupt arrived (possibly...)
  1225. * @qc: Taskfile currently active in engine
  1226. *
  1227. * Handle host interrupt for given queued command. Currently,
  1228. * only DMA interrupts are handled. All other commands are
  1229. * handled via polling with interrupts disabled (nIEN bit).
  1230. *
  1231. * LOCKING:
  1232. * spin_lock_irqsave(host lock)
  1233. *
  1234. * RETURNS:
  1235. * One if interrupt was handled, zero if not (shared irq).
  1236. */
  1237. inline unsigned int ata_sff_host_intr(struct ata_port *ap,
  1238. struct ata_queued_cmd *qc)
  1239. {
  1240. struct ata_eh_info *ehi = &ap->link.eh_info;
  1241. u8 status, host_stat = 0;
  1242. VPRINTK("ata%u: protocol %d task_state %d\n",
  1243. ap->print_id, qc->tf.protocol, ap->hsm_task_state);
  1244. /* Check whether we are expecting interrupt in this state */
  1245. switch (ap->hsm_task_state) {
  1246. case HSM_ST_FIRST:
  1247. /* Some pre-ATAPI-4 devices assert INTRQ
  1248. * at this state when ready to receive CDB.
  1249. */
  1250. /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
  1251. * The flag was turned on only for atapi devices. No
  1252. * need to check ata_is_atapi(qc->tf.protocol) again.
  1253. */
  1254. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  1255. goto idle_irq;
  1256. break;
  1257. case HSM_ST_LAST:
  1258. if (qc->tf.protocol == ATA_PROT_DMA ||
  1259. qc->tf.protocol == ATAPI_PROT_DMA) {
  1260. /* check status of DMA engine */
  1261. host_stat = ap->ops->bmdma_status(ap);
  1262. VPRINTK("ata%u: host_stat 0x%X\n",
  1263. ap->print_id, host_stat);
  1264. /* if it's not our irq... */
  1265. if (!(host_stat & ATA_DMA_INTR))
  1266. goto idle_irq;
  1267. /* before we do anything else, clear DMA-Start bit */
  1268. ap->ops->bmdma_stop(qc);
  1269. if (unlikely(host_stat & ATA_DMA_ERR)) {
  1270. /* error when transfering data to/from memory */
  1271. qc->err_mask |= AC_ERR_HOST_BUS;
  1272. ap->hsm_task_state = HSM_ST_ERR;
  1273. }
  1274. }
  1275. break;
  1276. case HSM_ST:
  1277. break;
  1278. default:
  1279. goto idle_irq;
  1280. }
  1281. /* check altstatus */
  1282. status = ata_sff_altstatus(ap);
  1283. if (status & ATA_BUSY)
  1284. goto idle_irq;
  1285. /* check main status, clearing INTRQ */
  1286. status = ap->ops->sff_check_status(ap);
  1287. if (unlikely(status & ATA_BUSY))
  1288. goto idle_irq;
  1289. /* ack bmdma irq events */
  1290. ap->ops->sff_irq_clear(ap);
  1291. ata_sff_hsm_move(ap, qc, status, 0);
  1292. if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
  1293. qc->tf.protocol == ATAPI_PROT_DMA))
  1294. ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
  1295. return 1; /* irq handled */
  1296. idle_irq:
  1297. ap->stats.idle_irq++;
  1298. #ifdef ATA_IRQ_TRAP
  1299. if ((ap->stats.idle_irq % 1000) == 0) {
  1300. ap->ops->sff_check_status(ap);
  1301. ap->ops->sff_irq_clear(ap);
  1302. ata_port_printk(ap, KERN_WARNING, "irq trap\n");
  1303. return 1;
  1304. }
  1305. #endif
  1306. return 0; /* irq not handled */
  1307. }
  1308. /**
  1309. * ata_sff_interrupt - Default ATA host interrupt handler
  1310. * @irq: irq line (unused)
  1311. * @dev_instance: pointer to our ata_host information structure
  1312. *
  1313. * Default interrupt handler for PCI IDE devices. Calls
  1314. * ata_sff_host_intr() for each port that is not disabled.
  1315. *
  1316. * LOCKING:
  1317. * Obtains host lock during operation.
  1318. *
  1319. * RETURNS:
  1320. * IRQ_NONE or IRQ_HANDLED.
  1321. */
  1322. irqreturn_t ata_sff_interrupt(int irq, void *dev_instance)
  1323. {
  1324. struct ata_host *host = dev_instance;
  1325. unsigned int i;
  1326. unsigned int handled = 0;
  1327. unsigned long flags;
  1328. /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
  1329. spin_lock_irqsave(&host->lock, flags);
  1330. for (i = 0; i < host->n_ports; i++) {
  1331. struct ata_port *ap;
  1332. ap = host->ports[i];
  1333. if (ap &&
  1334. !(ap->flags & ATA_FLAG_DISABLED)) {
  1335. struct ata_queued_cmd *qc;
  1336. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  1337. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
  1338. (qc->flags & ATA_QCFLAG_ACTIVE))
  1339. handled |= ata_sff_host_intr(ap, qc);
  1340. }
  1341. }
  1342. spin_unlock_irqrestore(&host->lock, flags);
  1343. return IRQ_RETVAL(handled);
  1344. }
  1345. /**
  1346. * ata_sff_freeze - Freeze SFF controller port
  1347. * @ap: port to freeze
  1348. *
  1349. * Freeze BMDMA controller port.
  1350. *
  1351. * LOCKING:
  1352. * Inherited from caller.
  1353. */
  1354. void ata_sff_freeze(struct ata_port *ap)
  1355. {
  1356. struct ata_ioports *ioaddr = &ap->ioaddr;
  1357. ap->ctl |= ATA_NIEN;
  1358. ap->last_ctl = ap->ctl;
  1359. if (ioaddr->ctl_addr)
  1360. iowrite8(ap->ctl, ioaddr->ctl_addr);
  1361. /* Under certain circumstances, some controllers raise IRQ on
  1362. * ATA_NIEN manipulation. Also, many controllers fail to mask
  1363. * previously pending IRQ on ATA_NIEN assertion. Clear it.
  1364. */
  1365. ap->ops->sff_check_status(ap);
  1366. ap->ops->sff_irq_clear(ap);
  1367. }
  1368. /**
  1369. * ata_sff_thaw - Thaw SFF controller port
  1370. * @ap: port to thaw
  1371. *
  1372. * Thaw SFF controller port.
  1373. *
  1374. * LOCKING:
  1375. * Inherited from caller.
  1376. */
  1377. void ata_sff_thaw(struct ata_port *ap)
  1378. {
  1379. /* clear & re-enable interrupts */
  1380. ap->ops->sff_check_status(ap);
  1381. ap->ops->sff_irq_clear(ap);
  1382. ap->ops->sff_irq_on(ap);
  1383. }
  1384. /**
  1385. * ata_devchk - PATA device presence detection
  1386. * @ap: ATA channel to examine
  1387. * @device: Device to examine (starting at zero)
  1388. *
  1389. * This technique was originally described in
  1390. * Hale Landis's ATADRVR (www.ata-atapi.com), and
  1391. * later found its way into the ATA/ATAPI spec.
  1392. *
  1393. * Write a pattern to the ATA shadow registers,
  1394. * and if a device is present, it will respond by
  1395. * correctly storing and echoing back the
  1396. * ATA shadow register contents.
  1397. *
  1398. * LOCKING:
  1399. * caller.
  1400. */
  1401. static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
  1402. {
  1403. struct ata_ioports *ioaddr = &ap->ioaddr;
  1404. u8 nsect, lbal;
  1405. ap->ops->sff_dev_select(ap, device);
  1406. iowrite8(0x55, ioaddr->nsect_addr);
  1407. iowrite8(0xaa, ioaddr->lbal_addr);
  1408. iowrite8(0xaa, ioaddr->nsect_addr);
  1409. iowrite8(0x55, ioaddr->lbal_addr);
  1410. iowrite8(0x55, ioaddr->nsect_addr);
  1411. iowrite8(0xaa, ioaddr->lbal_addr);
  1412. nsect = ioread8(ioaddr->nsect_addr);
  1413. lbal = ioread8(ioaddr->lbal_addr);
  1414. if ((nsect == 0x55) && (lbal == 0xaa))
  1415. return 1; /* we found a device */
  1416. return 0; /* nothing found */
  1417. }
  1418. /**
  1419. * ata_sff_dev_classify - Parse returned ATA device signature
  1420. * @dev: ATA device to classify (starting at zero)
  1421. * @present: device seems present
  1422. * @r_err: Value of error register on completion
  1423. *
  1424. * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
  1425. * an ATA/ATAPI-defined set of values is placed in the ATA
  1426. * shadow registers, indicating the results of device detection
  1427. * and diagnostics.
  1428. *
  1429. * Select the ATA device, and read the values from the ATA shadow
  1430. * registers. Then parse according to the Error register value,
  1431. * and the spec-defined values examined by ata_dev_classify().
  1432. *
  1433. * LOCKING:
  1434. * caller.
  1435. *
  1436. * RETURNS:
  1437. * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
  1438. */
  1439. unsigned int ata_sff_dev_classify(struct ata_device *dev, int present,
  1440. u8 *r_err)
  1441. {
  1442. struct ata_port *ap = dev->link->ap;
  1443. struct ata_taskfile tf;
  1444. unsigned int class;
  1445. u8 err;
  1446. ap->ops->sff_dev_select(ap, dev->devno);
  1447. memset(&tf, 0, sizeof(tf));
  1448. ap->ops->sff_tf_read(ap, &tf);
  1449. err = tf.feature;
  1450. if (r_err)
  1451. *r_err = err;
  1452. /* see if device passed diags: continue and warn later */
  1453. if (err == 0)
  1454. /* diagnostic fail : do nothing _YET_ */
  1455. dev->horkage |= ATA_HORKAGE_DIAGNOSTIC;
  1456. else if (err == 1)
  1457. /* do nothing */ ;
  1458. else if ((dev->devno == 0) && (err == 0x81))
  1459. /* do nothing */ ;
  1460. else
  1461. return ATA_DEV_NONE;
  1462. /* determine if device is ATA or ATAPI */
  1463. class = ata_dev_classify(&tf);
  1464. if (class == ATA_DEV_UNKNOWN) {
  1465. /* If the device failed diagnostic, it's likely to
  1466. * have reported incorrect device signature too.
  1467. * Assume ATA device if the device seems present but
  1468. * device signature is invalid with diagnostic
  1469. * failure.
  1470. */
  1471. if (present && (dev->horkage & ATA_HORKAGE_DIAGNOSTIC))
  1472. class = ATA_DEV_ATA;
  1473. else
  1474. class = ATA_DEV_NONE;
  1475. } else if ((class == ATA_DEV_ATA) &&
  1476. (ap->ops->sff_check_status(ap) == 0))
  1477. class = ATA_DEV_NONE;
  1478. return class;
  1479. }
  1480. static int ata_bus_post_reset(struct ata_port *ap, unsigned int devmask,
  1481. unsigned long deadline)
  1482. {
  1483. struct ata_ioports *ioaddr = &ap->ioaddr;
  1484. unsigned int dev0 = devmask & (1 << 0);
  1485. unsigned int dev1 = devmask & (1 << 1);
  1486. int rc, ret = 0;
  1487. /* if device 0 was found in ata_devchk, wait for its
  1488. * BSY bit to clear
  1489. */
  1490. if (dev0) {
  1491. rc = ata_sff_wait_ready(ap, deadline);
  1492. if (rc) {
  1493. if (rc != -ENODEV)
  1494. return rc;
  1495. ret = rc;
  1496. }
  1497. }
  1498. /* if device 1 was found in ata_devchk, wait for register
  1499. * access briefly, then wait for BSY to clear.
  1500. */
  1501. if (dev1) {
  1502. int i;
  1503. ap->ops->sff_dev_select(ap, 1);
  1504. /* Wait for register access. Some ATAPI devices fail
  1505. * to set nsect/lbal after reset, so don't waste too
  1506. * much time on it. We're gonna wait for !BSY anyway.
  1507. */
  1508. for (i = 0; i < 2; i++) {
  1509. u8 nsect, lbal;
  1510. nsect = ioread8(ioaddr->nsect_addr);
  1511. lbal = ioread8(ioaddr->lbal_addr);
  1512. if ((nsect == 1) && (lbal == 1))
  1513. break;
  1514. msleep(50); /* give drive a breather */
  1515. }
  1516. rc = ata_sff_wait_ready(ap, deadline);
  1517. if (rc) {
  1518. if (rc != -ENODEV)
  1519. return rc;
  1520. ret = rc;
  1521. }
  1522. }
  1523. /* is all this really necessary? */
  1524. ap->ops->sff_dev_select(ap, 0);
  1525. if (dev1)
  1526. ap->ops->sff_dev_select(ap, 1);
  1527. if (dev0)
  1528. ap->ops->sff_dev_select(ap, 0);
  1529. return ret;
  1530. }
  1531. /**
  1532. * ata_sff_wait_after_reset - wait before checking status after reset
  1533. * @ap: port containing status register to be polled
  1534. * @deadline: deadline jiffies for the operation
  1535. *
  1536. * After reset, we need to pause a while before reading status.
  1537. * Also, certain combination of controller and device report 0xff
  1538. * for some duration (e.g. until SATA PHY is up and running)
  1539. * which is interpreted as empty port in ATA world. This
  1540. * function also waits for such devices to get out of 0xff
  1541. * status.
  1542. *
  1543. * LOCKING:
  1544. * Kernel thread context (may sleep).
  1545. */
  1546. void ata_sff_wait_after_reset(struct ata_port *ap, unsigned long deadline)
  1547. {
  1548. unsigned long until = jiffies + ATA_TMOUT_FF_WAIT;
  1549. if (time_before(until, deadline))
  1550. deadline = until;
  1551. /* Spec mandates ">= 2ms" before checking status. We wait
  1552. * 150ms, because that was the magic delay used for ATAPI
  1553. * devices in Hale Landis's ATADRVR, for the period of time
  1554. * between when the ATA command register is written, and then
  1555. * status is checked. Because waiting for "a while" before
  1556. * checking status is fine, post SRST, we perform this magic
  1557. * delay here as well.
  1558. *
  1559. * Old drivers/ide uses the 2mS rule and then waits for ready.
  1560. */
  1561. msleep(150);
  1562. /* Wait for 0xff to clear. Some SATA devices take a long time
  1563. * to clear 0xff after reset. For example, HHD424020F7SV00
  1564. * iVDR needs >= 800ms while. Quantum GoVault needs even more
  1565. * than that.
  1566. *
  1567. * Note that some PATA controllers (pata_ali) explode if
  1568. * status register is read more than once when there's no
  1569. * device attached.
  1570. */
  1571. if (ap->flags & ATA_FLAG_SATA) {
  1572. while (1) {
  1573. u8 status = ap->ops->sff_check_status(ap);
  1574. if (status != 0xff || time_after(jiffies, deadline))
  1575. return;
  1576. msleep(50);
  1577. }
  1578. }
  1579. }
  1580. static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
  1581. unsigned long deadline)
  1582. {
  1583. struct ata_ioports *ioaddr = &ap->ioaddr;
  1584. DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
  1585. /* software reset. causes dev0 to be selected */
  1586. iowrite8(ap->ctl, ioaddr->ctl_addr);
  1587. udelay(20); /* FIXME: flush */
  1588. iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
  1589. udelay(20); /* FIXME: flush */
  1590. iowrite8(ap->ctl, ioaddr->ctl_addr);
  1591. /* wait a while before checking status */
  1592. ata_sff_wait_after_reset(ap, deadline);
  1593. /* Before we perform post reset processing we want to see if
  1594. * the bus shows 0xFF because the odd clown forgets the D7
  1595. * pulldown resistor.
  1596. */
  1597. if (ap->ops->sff_check_status(ap) == 0xFF)
  1598. return -ENODEV;
  1599. return ata_bus_post_reset(ap, devmask, deadline);
  1600. }
  1601. /**
  1602. * ata_sff_softreset - reset host port via ATA SRST
  1603. * @link: ATA link to reset
  1604. * @classes: resulting classes of attached devices
  1605. * @deadline: deadline jiffies for the operation
  1606. *
  1607. * Reset host port using ATA SRST.
  1608. *
  1609. * LOCKING:
  1610. * Kernel thread context (may sleep)
  1611. *
  1612. * RETURNS:
  1613. * 0 on success, -errno otherwise.
  1614. */
  1615. int ata_sff_softreset(struct ata_link *link, unsigned int *classes,
  1616. unsigned long deadline)
  1617. {
  1618. struct ata_port *ap = link->ap;
  1619. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  1620. unsigned int devmask = 0;
  1621. int rc;
  1622. u8 err;
  1623. DPRINTK("ENTER\n");
  1624. if (ata_link_offline(link)) {
  1625. classes[0] = ATA_DEV_NONE;
  1626. goto out;
  1627. }
  1628. /* determine if device 0/1 are present */
  1629. if (ata_devchk(ap, 0))
  1630. devmask |= (1 << 0);
  1631. if (slave_possible && ata_devchk(ap, 1))
  1632. devmask |= (1 << 1);
  1633. /* select device 0 again */
  1634. ap->ops->sff_dev_select(ap, 0);
  1635. /* issue bus reset */
  1636. DPRINTK("about to softreset, devmask=%x\n", devmask);
  1637. rc = ata_bus_softreset(ap, devmask, deadline);
  1638. /* if link is occupied, -ENODEV too is an error */
  1639. if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
  1640. ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
  1641. return rc;
  1642. }
  1643. /* determine by signature whether we have ATA or ATAPI devices */
  1644. classes[0] = ata_sff_dev_classify(&link->device[0],
  1645. devmask & (1 << 0), &err);
  1646. if (slave_possible && err != 0x81)
  1647. classes[1] = ata_sff_dev_classify(&link->device[1],
  1648. devmask & (1 << 1), &err);
  1649. out:
  1650. DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
  1651. return 0;
  1652. }
  1653. /**
  1654. * sata_sff_hardreset - reset host port via SATA phy reset
  1655. * @link: link to reset
  1656. * @class: resulting class of attached device
  1657. * @deadline: deadline jiffies for the operation
  1658. *
  1659. * SATA phy-reset host port using DET bits of SControl register,
  1660. * wait for !BSY and classify the attached device.
  1661. *
  1662. * LOCKING:
  1663. * Kernel thread context (may sleep)
  1664. *
  1665. * RETURNS:
  1666. * 0 on success, -errno otherwise.
  1667. */
  1668. int sata_sff_hardreset(struct ata_link *link, unsigned int *class,
  1669. unsigned long deadline)
  1670. {
  1671. struct ata_port *ap = link->ap;
  1672. const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
  1673. int rc;
  1674. DPRINTK("ENTER\n");
  1675. /* do hardreset */
  1676. rc = sata_link_hardreset(link, timing, deadline);
  1677. if (rc) {
  1678. ata_link_printk(link, KERN_ERR,
  1679. "COMRESET failed (errno=%d)\n", rc);
  1680. return rc;
  1681. }
  1682. /* TODO: phy layer with polling, timeouts, etc. */
  1683. if (ata_link_offline(link)) {
  1684. *class = ATA_DEV_NONE;
  1685. DPRINTK("EXIT, link offline\n");
  1686. return 0;
  1687. }
  1688. /* wait a while before checking status */
  1689. ata_sff_wait_after_reset(ap, deadline);
  1690. /* If PMP is supported, we have to do follow-up SRST. Note
  1691. * that some PMPs don't send D2H Reg FIS after hardreset at
  1692. * all if the first port is empty. Wait for it just for a
  1693. * second and request follow-up SRST.
  1694. */
  1695. if (ap->flags & ATA_FLAG_PMP) {
  1696. ata_sff_wait_ready(ap, jiffies + HZ);
  1697. return -EAGAIN;
  1698. }
  1699. rc = ata_sff_wait_ready(ap, deadline);
  1700. /* link occupied, -ENODEV too is an error */
  1701. if (rc) {
  1702. ata_link_printk(link, KERN_ERR,
  1703. "COMRESET failed (errno=%d)\n", rc);
  1704. return rc;
  1705. }
  1706. ap->ops->sff_dev_select(ap, 0); /* probably unnecessary */
  1707. *class = ata_sff_dev_classify(link->device, 1, NULL);
  1708. DPRINTK("EXIT, class=%u\n", *class);
  1709. return 0;
  1710. }
  1711. /**
  1712. * ata_sff_error_handler - Stock error handler for BMDMA controller
  1713. * @ap: port to handle error for
  1714. *
  1715. * Stock error handler for SFF controller. It can handle both
  1716. * PATA and SATA controllers. Many controllers should be able to
  1717. * use this EH as-is or with some added handling before and
  1718. * after.
  1719. *
  1720. * LOCKING:
  1721. * Kernel thread context (may sleep)
  1722. */
  1723. void ata_sff_error_handler(struct ata_port *ap)
  1724. {
  1725. ata_reset_fn_t softreset = ap->ops->softreset;
  1726. ata_reset_fn_t hardreset = ap->ops->hardreset;
  1727. struct ata_queued_cmd *qc;
  1728. unsigned long flags;
  1729. int thaw = 0;
  1730. qc = __ata_qc_from_tag(ap, ap->link.active_tag);
  1731. if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
  1732. qc = NULL;
  1733. /* reset PIO HSM and stop DMA engine */
  1734. spin_lock_irqsave(ap->lock, flags);
  1735. ap->hsm_task_state = HSM_ST_IDLE;
  1736. if (ap->ioaddr.bmdma_addr &&
  1737. qc && (qc->tf.protocol == ATA_PROT_DMA ||
  1738. qc->tf.protocol == ATAPI_PROT_DMA)) {
  1739. u8 host_stat;
  1740. host_stat = ap->ops->bmdma_status(ap);
  1741. /* BMDMA controllers indicate host bus error by
  1742. * setting DMA_ERR bit and timing out. As it wasn't
  1743. * really a timeout event, adjust error mask and
  1744. * cancel frozen state.
  1745. */
  1746. if (qc->err_mask == AC_ERR_TIMEOUT && (host_stat & ATA_DMA_ERR)) {
  1747. qc->err_mask = AC_ERR_HOST_BUS;
  1748. thaw = 1;
  1749. }
  1750. ap->ops->bmdma_stop(qc);
  1751. }
  1752. ata_sff_altstatus(ap);
  1753. ap->ops->sff_check_status(ap);
  1754. ap->ops->sff_irq_clear(ap);
  1755. spin_unlock_irqrestore(ap->lock, flags);
  1756. if (thaw)
  1757. ata_eh_thaw_port(ap);
  1758. /* PIO and DMA engines have been stopped, perform recovery */
  1759. /* ata_sff_softreset and sata_sff_hardreset are inherited to
  1760. * all SFF drivers from ata_sff_port_ops. Ignore softreset if
  1761. * ctl isn't accessible. Ignore hardreset if SCR access isn't
  1762. * available.
  1763. */
  1764. if (softreset == ata_sff_softreset && !ap->ioaddr.ctl_addr)
  1765. softreset = NULL;
  1766. if (hardreset == sata_sff_hardreset && !sata_scr_valid(&ap->link))
  1767. hardreset = NULL;
  1768. ata_do_eh(ap, ap->ops->prereset, softreset, hardreset,
  1769. ap->ops->postreset);
  1770. }
  1771. /**
  1772. * ata_sff_post_internal_cmd - Stock post_internal_cmd for SFF controller
  1773. * @qc: internal command to clean up
  1774. *
  1775. * LOCKING:
  1776. * Kernel thread context (may sleep)
  1777. */
  1778. void ata_sff_post_internal_cmd(struct ata_queued_cmd *qc)
  1779. {
  1780. if (qc->ap->ioaddr.bmdma_addr)
  1781. ata_bmdma_stop(qc);
  1782. }
  1783. /**
  1784. * ata_sff_port_start - Set port up for dma.
  1785. * @ap: Port to initialize
  1786. *
  1787. * Called just after data structures for each port are
  1788. * initialized. Allocates space for PRD table if the device
  1789. * is DMA capable SFF.
  1790. *
  1791. * May be used as the port_start() entry in ata_port_operations.
  1792. *
  1793. * LOCKING:
  1794. * Inherited from caller.
  1795. */
  1796. int ata_sff_port_start(struct ata_port *ap)
  1797. {
  1798. if (ap->ioaddr.bmdma_addr)
  1799. return ata_port_start(ap);
  1800. return 0;
  1801. }
  1802. /**
  1803. * ata_sff_std_ports - initialize ioaddr with standard port offsets.
  1804. * @ioaddr: IO address structure to be initialized
  1805. *
  1806. * Utility function which initializes data_addr, error_addr,
  1807. * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
  1808. * device_addr, status_addr, and command_addr to standard offsets
  1809. * relative to cmd_addr.
  1810. *
  1811. * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
  1812. */
  1813. void ata_sff_std_ports(struct ata_ioports *ioaddr)
  1814. {
  1815. ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
  1816. ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
  1817. ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
  1818. ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
  1819. ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
  1820. ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
  1821. ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
  1822. ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
  1823. ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
  1824. ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
  1825. }
  1826. unsigned long ata_bmdma_mode_filter(struct ata_device *adev,
  1827. unsigned long xfer_mask)
  1828. {
  1829. /* Filter out DMA modes if the device has been configured by
  1830. the BIOS as PIO only */
  1831. if (adev->link->ap->ioaddr.bmdma_addr == NULL)
  1832. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  1833. return xfer_mask;
  1834. }
  1835. /**
  1836. * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
  1837. * @qc: Info associated with this ATA transaction.
  1838. *
  1839. * LOCKING:
  1840. * spin_lock_irqsave(host lock)
  1841. */
  1842. void ata_bmdma_setup(struct ata_queued_cmd *qc)
  1843. {
  1844. struct ata_port *ap = qc->ap;
  1845. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  1846. u8 dmactl;
  1847. /* load PRD table addr. */
  1848. mb(); /* make sure PRD table writes are visible to controller */
  1849. iowrite32(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
  1850. /* specify data direction, triple-check start bit is clear */
  1851. dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  1852. dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
  1853. if (!rw)
  1854. dmactl |= ATA_DMA_WR;
  1855. iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  1856. /* issue r/w command */
  1857. ap->ops->sff_exec_command(ap, &qc->tf);
  1858. }
  1859. /**
  1860. * ata_bmdma_start - Start a PCI IDE BMDMA transaction
  1861. * @qc: Info associated with this ATA transaction.
  1862. *
  1863. * LOCKING:
  1864. * spin_lock_irqsave(host lock)
  1865. */
  1866. void ata_bmdma_start(struct ata_queued_cmd *qc)
  1867. {
  1868. struct ata_port *ap = qc->ap;
  1869. u8 dmactl;
  1870. /* start host DMA transaction */
  1871. dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  1872. iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  1873. /* Strictly, one may wish to issue an ioread8() here, to
  1874. * flush the mmio write. However, control also passes
  1875. * to the hardware at this point, and it will interrupt
  1876. * us when we are to resume control. So, in effect,
  1877. * we don't care when the mmio write flushes.
  1878. * Further, a read of the DMA status register _immediately_
  1879. * following the write may not be what certain flaky hardware
  1880. * is expected, so I think it is best to not add a readb()
  1881. * without first all the MMIO ATA cards/mobos.
  1882. * Or maybe I'm just being paranoid.
  1883. *
  1884. * FIXME: The posting of this write means I/O starts are
  1885. * unneccessarily delayed for MMIO
  1886. */
  1887. }
  1888. /**
  1889. * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
  1890. * @qc: Command we are ending DMA for
  1891. *
  1892. * Clears the ATA_DMA_START flag in the dma control register
  1893. *
  1894. * May be used as the bmdma_stop() entry in ata_port_operations.
  1895. *
  1896. * LOCKING:
  1897. * spin_lock_irqsave(host lock)
  1898. */
  1899. void ata_bmdma_stop(struct ata_queued_cmd *qc)
  1900. {
  1901. struct ata_port *ap = qc->ap;
  1902. void __iomem *mmio = ap->ioaddr.bmdma_addr;
  1903. /* clear start/stop bit */
  1904. iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
  1905. mmio + ATA_DMA_CMD);
  1906. /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
  1907. ata_sff_altstatus(ap); /* dummy read */
  1908. }
  1909. /**
  1910. * ata_bmdma_status - Read PCI IDE BMDMA status
  1911. * @ap: Port associated with this ATA transaction.
  1912. *
  1913. * Read and return BMDMA status register.
  1914. *
  1915. * May be used as the bmdma_status() entry in ata_port_operations.
  1916. *
  1917. * LOCKING:
  1918. * spin_lock_irqsave(host lock)
  1919. */
  1920. u8 ata_bmdma_status(struct ata_port *ap)
  1921. {
  1922. return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
  1923. }
  1924. /**
  1925. * ata_bus_reset - reset host port and associated ATA channel
  1926. * @ap: port to reset
  1927. *
  1928. * This is typically the first time we actually start issuing
  1929. * commands to the ATA channel. We wait for BSY to clear, then
  1930. * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
  1931. * result. Determine what devices, if any, are on the channel
  1932. * by looking at the device 0/1 error register. Look at the signature
  1933. * stored in each device's taskfile registers, to determine if
  1934. * the device is ATA or ATAPI.
  1935. *
  1936. * LOCKING:
  1937. * PCI/etc. bus probe sem.
  1938. * Obtains host lock.
  1939. *
  1940. * SIDE EFFECTS:
  1941. * Sets ATA_FLAG_DISABLED if bus reset fails.
  1942. *
  1943. * DEPRECATED:
  1944. * This function is only for drivers which still use old EH and
  1945. * will be removed soon.
  1946. */
  1947. void ata_bus_reset(struct ata_port *ap)
  1948. {
  1949. struct ata_device *device = ap->link.device;
  1950. struct ata_ioports *ioaddr = &ap->ioaddr;
  1951. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  1952. u8 err;
  1953. unsigned int dev0, dev1 = 0, devmask = 0;
  1954. int rc;
  1955. DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
  1956. /* determine if device 0/1 are present */
  1957. if (ap->flags & ATA_FLAG_SATA_RESET)
  1958. dev0 = 1;
  1959. else {
  1960. dev0 = ata_devchk(ap, 0);
  1961. if (slave_possible)
  1962. dev1 = ata_devchk(ap, 1);
  1963. }
  1964. if (dev0)
  1965. devmask |= (1 << 0);
  1966. if (dev1)
  1967. devmask |= (1 << 1);
  1968. /* select device 0 again */
  1969. ap->ops->sff_dev_select(ap, 0);
  1970. /* issue bus reset */
  1971. if (ap->flags & ATA_FLAG_SRST) {
  1972. rc = ata_bus_softreset(ap, devmask, jiffies + 40 * HZ);
  1973. if (rc && rc != -ENODEV)
  1974. goto err_out;
  1975. }
  1976. /*
  1977. * determine by signature whether we have ATA or ATAPI devices
  1978. */
  1979. device[0].class = ata_sff_dev_classify(&device[0], dev0, &err);
  1980. if ((slave_possible) && (err != 0x81))
  1981. device[1].class = ata_sff_dev_classify(&device[1], dev1, &err);
  1982. /* is double-select really necessary? */
  1983. if (device[1].class != ATA_DEV_NONE)
  1984. ap->ops->sff_dev_select(ap, 1);
  1985. if (device[0].class != ATA_DEV_NONE)
  1986. ap->ops->sff_dev_select(ap, 0);
  1987. /* if no devices were detected, disable this port */
  1988. if ((device[0].class == ATA_DEV_NONE) &&
  1989. (device[1].class == ATA_DEV_NONE))
  1990. goto err_out;
  1991. if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
  1992. /* set up device control for ATA_FLAG_SATA_RESET */
  1993. iowrite8(ap->ctl, ioaddr->ctl_addr);
  1994. }
  1995. DPRINTK("EXIT\n");
  1996. return;
  1997. err_out:
  1998. ata_port_printk(ap, KERN_ERR, "disabling port\n");
  1999. ata_port_disable(ap);
  2000. DPRINTK("EXIT\n");
  2001. }
  2002. #ifdef CONFIG_PCI
  2003. /**
  2004. * ata_pci_bmdma_clear_simplex - attempt to kick device out of simplex
  2005. * @pdev: PCI device
  2006. *
  2007. * Some PCI ATA devices report simplex mode but in fact can be told to
  2008. * enter non simplex mode. This implements the necessary logic to
  2009. * perform the task on such devices. Calling it on other devices will
  2010. * have -undefined- behaviour.
  2011. */
  2012. int ata_pci_bmdma_clear_simplex(struct pci_dev *pdev)
  2013. {
  2014. unsigned long bmdma = pci_resource_start(pdev, 4);
  2015. u8 simplex;
  2016. if (bmdma == 0)
  2017. return -ENOENT;
  2018. simplex = inb(bmdma + 0x02);
  2019. outb(simplex & 0x60, bmdma + 0x02);
  2020. simplex = inb(bmdma + 0x02);
  2021. if (simplex & 0x80)
  2022. return -EOPNOTSUPP;
  2023. return 0;
  2024. }
  2025. /**
  2026. * ata_pci_bmdma_init - acquire PCI BMDMA resources and init ATA host
  2027. * @host: target ATA host
  2028. *
  2029. * Acquire PCI BMDMA resources and initialize @host accordingly.
  2030. *
  2031. * LOCKING:
  2032. * Inherited from calling layer (may sleep).
  2033. *
  2034. * RETURNS:
  2035. * 0 on success, -errno otherwise.
  2036. */
  2037. int ata_pci_bmdma_init(struct ata_host *host)
  2038. {
  2039. struct device *gdev = host->dev;
  2040. struct pci_dev *pdev = to_pci_dev(gdev);
  2041. int i, rc;
  2042. /* No BAR4 allocation: No DMA */
  2043. if (pci_resource_start(pdev, 4) == 0)
  2044. return 0;
  2045. /* TODO: If we get no DMA mask we should fall back to PIO */
  2046. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  2047. if (rc)
  2048. return rc;
  2049. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  2050. if (rc)
  2051. return rc;
  2052. /* request and iomap DMA region */
  2053. rc = pcim_iomap_regions(pdev, 1 << 4, dev_driver_string(gdev));
  2054. if (rc) {
  2055. dev_printk(KERN_ERR, gdev, "failed to request/iomap BAR4\n");
  2056. return -ENOMEM;
  2057. }
  2058. host->iomap = pcim_iomap_table(pdev);
  2059. for (i = 0; i < 2; i++) {
  2060. struct ata_port *ap = host->ports[i];
  2061. void __iomem *bmdma = host->iomap[4] + 8 * i;
  2062. if (ata_port_is_dummy(ap))
  2063. continue;
  2064. ap->ioaddr.bmdma_addr = bmdma;
  2065. if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  2066. (ioread8(bmdma + 2) & 0x80))
  2067. host->flags |= ATA_HOST_SIMPLEX;
  2068. ata_port_desc(ap, "bmdma 0x%llx",
  2069. (unsigned long long)pci_resource_start(pdev, 4) + 8 * i);
  2070. }
  2071. return 0;
  2072. }
  2073. static int ata_resources_present(struct pci_dev *pdev, int port)
  2074. {
  2075. int i;
  2076. /* Check the PCI resources for this channel are enabled */
  2077. port = port * 2;
  2078. for (i = 0; i < 2; i ++) {
  2079. if (pci_resource_start(pdev, port + i) == 0 ||
  2080. pci_resource_len(pdev, port + i) == 0)
  2081. return 0;
  2082. }
  2083. return 1;
  2084. }
  2085. /**
  2086. * ata_pci_sff_init_host - acquire native PCI ATA resources and init host
  2087. * @host: target ATA host
  2088. *
  2089. * Acquire native PCI ATA resources for @host and initialize the
  2090. * first two ports of @host accordingly. Ports marked dummy are
  2091. * skipped and allocation failure makes the port dummy.
  2092. *
  2093. * Note that native PCI resources are valid even for legacy hosts
  2094. * as we fix up pdev resources array early in boot, so this
  2095. * function can be used for both native and legacy SFF hosts.
  2096. *
  2097. * LOCKING:
  2098. * Inherited from calling layer (may sleep).
  2099. *
  2100. * RETURNS:
  2101. * 0 if at least one port is initialized, -ENODEV if no port is
  2102. * available.
  2103. */
  2104. int ata_pci_sff_init_host(struct ata_host *host)
  2105. {
  2106. struct device *gdev = host->dev;
  2107. struct pci_dev *pdev = to_pci_dev(gdev);
  2108. unsigned int mask = 0;
  2109. int i, rc;
  2110. /* request, iomap BARs and init port addresses accordingly */
  2111. for (i = 0; i < 2; i++) {
  2112. struct ata_port *ap = host->ports[i];
  2113. int base = i * 2;
  2114. void __iomem * const *iomap;
  2115. if (ata_port_is_dummy(ap))
  2116. continue;
  2117. /* Discard disabled ports. Some controllers show
  2118. * their unused channels this way. Disabled ports are
  2119. * made dummy.
  2120. */
  2121. if (!ata_resources_present(pdev, i)) {
  2122. ap->ops = &ata_dummy_port_ops;
  2123. continue;
  2124. }
  2125. rc = pcim_iomap_regions(pdev, 0x3 << base,
  2126. dev_driver_string(gdev));
  2127. if (rc) {
  2128. dev_printk(KERN_WARNING, gdev,
  2129. "failed to request/iomap BARs for port %d "
  2130. "(errno=%d)\n", i, rc);
  2131. if (rc == -EBUSY)
  2132. pcim_pin_device(pdev);
  2133. ap->ops = &ata_dummy_port_ops;
  2134. continue;
  2135. }
  2136. host->iomap = iomap = pcim_iomap_table(pdev);
  2137. ap->ioaddr.cmd_addr = iomap[base];
  2138. ap->ioaddr.altstatus_addr =
  2139. ap->ioaddr.ctl_addr = (void __iomem *)
  2140. ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS);
  2141. ata_sff_std_ports(&ap->ioaddr);
  2142. ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
  2143. (unsigned long long)pci_resource_start(pdev, base),
  2144. (unsigned long long)pci_resource_start(pdev, base + 1));
  2145. mask |= 1 << i;
  2146. }
  2147. if (!mask) {
  2148. dev_printk(KERN_ERR, gdev, "no available native port\n");
  2149. return -ENODEV;
  2150. }
  2151. return 0;
  2152. }
  2153. /**
  2154. * ata_pci_sff_prepare_host - helper to prepare native PCI ATA host
  2155. * @pdev: target PCI device
  2156. * @ppi: array of port_info, must be enough for two ports
  2157. * @r_host: out argument for the initialized ATA host
  2158. *
  2159. * Helper to allocate ATA host for @pdev, acquire all native PCI
  2160. * resources and initialize it accordingly in one go.
  2161. *
  2162. * LOCKING:
  2163. * Inherited from calling layer (may sleep).
  2164. *
  2165. * RETURNS:
  2166. * 0 on success, -errno otherwise.
  2167. */
  2168. int ata_pci_sff_prepare_host(struct pci_dev *pdev,
  2169. const struct ata_port_info * const * ppi,
  2170. struct ata_host **r_host)
  2171. {
  2172. struct ata_host *host;
  2173. int rc;
  2174. if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
  2175. return -ENOMEM;
  2176. host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
  2177. if (!host) {
  2178. dev_printk(KERN_ERR, &pdev->dev,
  2179. "failed to allocate ATA host\n");
  2180. rc = -ENOMEM;
  2181. goto err_out;
  2182. }
  2183. rc = ata_pci_sff_init_host(host);
  2184. if (rc)
  2185. goto err_out;
  2186. /* init DMA related stuff */
  2187. rc = ata_pci_bmdma_init(host);
  2188. if (rc)
  2189. goto err_bmdma;
  2190. devres_remove_group(&pdev->dev, NULL);
  2191. *r_host = host;
  2192. return 0;
  2193. err_bmdma:
  2194. /* This is necessary because PCI and iomap resources are
  2195. * merged and releasing the top group won't release the
  2196. * acquired resources if some of those have been acquired
  2197. * before entering this function.
  2198. */
  2199. pcim_iounmap_regions(pdev, 0xf);
  2200. err_out:
  2201. devres_release_group(&pdev->dev, NULL);
  2202. return rc;
  2203. }
  2204. /**
  2205. * ata_pci_sff_activate_host - start SFF host, request IRQ and register it
  2206. * @host: target SFF ATA host
  2207. * @irq_handler: irq_handler used when requesting IRQ(s)
  2208. * @sht: scsi_host_template to use when registering the host
  2209. *
  2210. * This is the counterpart of ata_host_activate() for SFF ATA
  2211. * hosts. This separate helper is necessary because SFF hosts
  2212. * use two separate interrupts in legacy mode.
  2213. *
  2214. * LOCKING:
  2215. * Inherited from calling layer (may sleep).
  2216. *
  2217. * RETURNS:
  2218. * 0 on success, -errno otherwise.
  2219. */
  2220. int ata_pci_sff_activate_host(struct ata_host *host,
  2221. irq_handler_t irq_handler,
  2222. struct scsi_host_template *sht)
  2223. {
  2224. struct device *dev = host->dev;
  2225. struct pci_dev *pdev = to_pci_dev(dev);
  2226. const char *drv_name = dev_driver_string(host->dev);
  2227. int legacy_mode = 0, rc;
  2228. rc = ata_host_start(host);
  2229. if (rc)
  2230. return rc;
  2231. if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
  2232. u8 tmp8, mask;
  2233. /* TODO: What if one channel is in native mode ... */
  2234. pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
  2235. mask = (1 << 2) | (1 << 0);
  2236. if ((tmp8 & mask) != mask)
  2237. legacy_mode = 1;
  2238. #if defined(CONFIG_NO_ATA_LEGACY)
  2239. /* Some platforms with PCI limits cannot address compat
  2240. port space. In that case we punt if their firmware has
  2241. left a device in compatibility mode */
  2242. if (legacy_mode) {
  2243. printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
  2244. return -EOPNOTSUPP;
  2245. }
  2246. #endif
  2247. }
  2248. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  2249. return -ENOMEM;
  2250. if (!legacy_mode && pdev->irq) {
  2251. rc = devm_request_irq(dev, pdev->irq, irq_handler,
  2252. IRQF_SHARED, drv_name, host);
  2253. if (rc)
  2254. goto out;
  2255. ata_port_desc(host->ports[0], "irq %d", pdev->irq);
  2256. ata_port_desc(host->ports[1], "irq %d", pdev->irq);
  2257. } else if (legacy_mode) {
  2258. if (!ata_port_is_dummy(host->ports[0])) {
  2259. rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev),
  2260. irq_handler, IRQF_SHARED,
  2261. drv_name, host);
  2262. if (rc)
  2263. goto out;
  2264. ata_port_desc(host->ports[0], "irq %d",
  2265. ATA_PRIMARY_IRQ(pdev));
  2266. }
  2267. if (!ata_port_is_dummy(host->ports[1])) {
  2268. rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev),
  2269. irq_handler, IRQF_SHARED,
  2270. drv_name, host);
  2271. if (rc)
  2272. goto out;
  2273. ata_port_desc(host->ports[1], "irq %d",
  2274. ATA_SECONDARY_IRQ(pdev));
  2275. }
  2276. }
  2277. rc = ata_host_register(host, sht);
  2278. out:
  2279. if (rc == 0)
  2280. devres_remove_group(dev, NULL);
  2281. else
  2282. devres_release_group(dev, NULL);
  2283. return rc;
  2284. }
  2285. /**
  2286. * ata_pci_sff_init_one - Initialize/register PCI IDE host controller
  2287. * @pdev: Controller to be initialized
  2288. * @ppi: array of port_info, must be enough for two ports
  2289. * @sht: scsi_host_template to use when registering the host
  2290. * @host_priv: host private_data
  2291. *
  2292. * This is a helper function which can be called from a driver's
  2293. * xxx_init_one() probe function if the hardware uses traditional
  2294. * IDE taskfile registers.
  2295. *
  2296. * This function calls pci_enable_device(), reserves its register
  2297. * regions, sets the dma mask, enables bus master mode, and calls
  2298. * ata_device_add()
  2299. *
  2300. * ASSUMPTION:
  2301. * Nobody makes a single channel controller that appears solely as
  2302. * the secondary legacy port on PCI.
  2303. *
  2304. * LOCKING:
  2305. * Inherited from PCI layer (may sleep).
  2306. *
  2307. * RETURNS:
  2308. * Zero on success, negative on errno-based value on error.
  2309. */
  2310. int ata_pci_sff_init_one(struct pci_dev *pdev,
  2311. const struct ata_port_info * const * ppi,
  2312. struct scsi_host_template *sht, void *host_priv)
  2313. {
  2314. struct device *dev = &pdev->dev;
  2315. const struct ata_port_info *pi = NULL;
  2316. struct ata_host *host = NULL;
  2317. int i, rc;
  2318. DPRINTK("ENTER\n");
  2319. /* look up the first valid port_info */
  2320. for (i = 0; i < 2 && ppi[i]; i++) {
  2321. if (ppi[i]->port_ops != &ata_dummy_port_ops) {
  2322. pi = ppi[i];
  2323. break;
  2324. }
  2325. }
  2326. if (!pi) {
  2327. dev_printk(KERN_ERR, &pdev->dev,
  2328. "no valid port_info specified\n");
  2329. return -EINVAL;
  2330. }
  2331. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  2332. return -ENOMEM;
  2333. rc = pcim_enable_device(pdev);
  2334. if (rc)
  2335. goto out;
  2336. /* prepare and activate SFF host */
  2337. rc = ata_pci_sff_prepare_host(pdev, ppi, &host);
  2338. if (rc)
  2339. goto out;
  2340. host->private_data = host_priv;
  2341. pci_set_master(pdev);
  2342. rc = ata_pci_sff_activate_host(host, ata_sff_interrupt, sht);
  2343. out:
  2344. if (rc == 0)
  2345. devres_remove_group(&pdev->dev, NULL);
  2346. else
  2347. devres_release_group(&pdev->dev, NULL);
  2348. return rc;
  2349. }
  2350. #endif /* CONFIG_PCI */
  2351. EXPORT_SYMBOL_GPL(ata_sff_port_ops);
  2352. EXPORT_SYMBOL_GPL(ata_bmdma_port_ops);
  2353. EXPORT_SYMBOL_GPL(ata_sff_qc_prep);
  2354. EXPORT_SYMBOL_GPL(ata_sff_dumb_qc_prep);
  2355. EXPORT_SYMBOL_GPL(ata_sff_dev_select);
  2356. EXPORT_SYMBOL_GPL(ata_sff_check_status);
  2357. EXPORT_SYMBOL_GPL(ata_sff_altstatus);
  2358. EXPORT_SYMBOL_GPL(ata_sff_busy_sleep);
  2359. EXPORT_SYMBOL_GPL(ata_sff_wait_ready);
  2360. EXPORT_SYMBOL_GPL(ata_sff_tf_load);
  2361. EXPORT_SYMBOL_GPL(ata_sff_tf_read);
  2362. EXPORT_SYMBOL_GPL(ata_sff_exec_command);
  2363. EXPORT_SYMBOL_GPL(ata_sff_data_xfer);
  2364. EXPORT_SYMBOL_GPL(ata_sff_data_xfer_noirq);
  2365. EXPORT_SYMBOL_GPL(ata_sff_irq_on);
  2366. EXPORT_SYMBOL_GPL(ata_sff_irq_clear);
  2367. EXPORT_SYMBOL_GPL(ata_sff_hsm_move);
  2368. EXPORT_SYMBOL_GPL(ata_sff_qc_issue);
  2369. EXPORT_SYMBOL_GPL(ata_sff_host_intr);
  2370. EXPORT_SYMBOL_GPL(ata_sff_interrupt);
  2371. EXPORT_SYMBOL_GPL(ata_sff_freeze);
  2372. EXPORT_SYMBOL_GPL(ata_sff_thaw);
  2373. EXPORT_SYMBOL_GPL(ata_sff_prereset);
  2374. EXPORT_SYMBOL_GPL(ata_sff_dev_classify);
  2375. EXPORT_SYMBOL_GPL(ata_sff_wait_after_reset);
  2376. EXPORT_SYMBOL_GPL(ata_sff_softreset);
  2377. EXPORT_SYMBOL_GPL(sata_sff_hardreset);
  2378. EXPORT_SYMBOL_GPL(ata_sff_postreset);
  2379. EXPORT_SYMBOL_GPL(ata_sff_error_handler);
  2380. EXPORT_SYMBOL_GPL(ata_sff_post_internal_cmd);
  2381. EXPORT_SYMBOL_GPL(ata_sff_port_start);
  2382. EXPORT_SYMBOL_GPL(ata_sff_std_ports);
  2383. EXPORT_SYMBOL_GPL(ata_bmdma_mode_filter);
  2384. EXPORT_SYMBOL_GPL(ata_bmdma_setup);
  2385. EXPORT_SYMBOL_GPL(ata_bmdma_start);
  2386. EXPORT_SYMBOL_GPL(ata_bmdma_stop);
  2387. EXPORT_SYMBOL_GPL(ata_bmdma_status);
  2388. EXPORT_SYMBOL_GPL(ata_bus_reset);
  2389. #ifdef CONFIG_PCI
  2390. EXPORT_SYMBOL_GPL(ata_pci_bmdma_clear_simplex);
  2391. EXPORT_SYMBOL_GPL(ata_pci_bmdma_init);
  2392. EXPORT_SYMBOL_GPL(ata_pci_sff_init_host);
  2393. EXPORT_SYMBOL_GPL(ata_pci_sff_prepare_host);
  2394. EXPORT_SYMBOL_GPL(ata_pci_sff_activate_host);
  2395. EXPORT_SYMBOL_GPL(ata_pci_sff_init_one);
  2396. #endif /* CONFIG_PCI */