libata-sff.c 72 KB

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