libata-sff.c 77 KB

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