libata-sff.c 84 KB

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