libata-sff.c 84 KB

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