libata-sff.c 80 KB

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