libata-sff.c 79 KB

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