libata-sff.c 82 KB

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