libata-sff.c 84 KB

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