libata-sff.c 80 KB

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