libata-sff.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755
  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/pci.h>
  36. #include <linux/libata.h>
  37. #include <linux/highmem.h>
  38. #include "libata.h"
  39. const struct ata_port_operations ata_sff_port_ops = {
  40. .inherits = &ata_base_port_ops,
  41. .qc_prep = ata_sff_qc_prep,
  42. .qc_issue = ata_sff_qc_issue,
  43. .freeze = ata_sff_freeze,
  44. .thaw = ata_sff_thaw,
  45. .prereset = ata_sff_prereset,
  46. .softreset = ata_sff_softreset,
  47. .hardreset = sata_sff_hardreset,
  48. .postreset = ata_sff_postreset,
  49. .error_handler = ata_sff_error_handler,
  50. .post_internal_cmd = ata_sff_post_internal_cmd,
  51. .sff_dev_select = ata_sff_dev_select,
  52. .sff_check_status = ata_sff_check_status,
  53. .sff_tf_load = ata_sff_tf_load,
  54. .sff_tf_read = ata_sff_tf_read,
  55. .sff_exec_command = ata_sff_exec_command,
  56. .sff_data_xfer = ata_sff_data_xfer,
  57. .sff_irq_on = ata_sff_irq_on,
  58. .sff_irq_clear = ata_sff_irq_clear,
  59. .port_start = ata_sff_port_start,
  60. };
  61. const struct ata_port_operations ata_bmdma_port_ops = {
  62. .inherits = &ata_sff_port_ops,
  63. .mode_filter = ata_bmdma_mode_filter,
  64. .bmdma_setup = ata_bmdma_setup,
  65. .bmdma_start = ata_bmdma_start,
  66. .bmdma_stop = ata_bmdma_stop,
  67. .bmdma_status = ata_bmdma_status,
  68. };
  69. /**
  70. * ata_fill_sg - Fill PCI IDE PRD table
  71. * @qc: Metadata associated with taskfile to be transferred
  72. *
  73. * Fill PCI IDE PRD (scatter-gather) table with segments
  74. * associated with the current disk command.
  75. *
  76. * LOCKING:
  77. * spin_lock_irqsave(host lock)
  78. *
  79. */
  80. static void ata_fill_sg(struct ata_queued_cmd *qc)
  81. {
  82. struct ata_port *ap = qc->ap;
  83. struct scatterlist *sg;
  84. unsigned int si, pi;
  85. pi = 0;
  86. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  87. u32 addr, offset;
  88. u32 sg_len, len;
  89. /* determine if physical DMA addr spans 64K boundary.
  90. * Note h/w doesn't support 64-bit, so we unconditionally
  91. * truncate dma_addr_t to u32.
  92. */
  93. addr = (u32) sg_dma_address(sg);
  94. sg_len = sg_dma_len(sg);
  95. while (sg_len) {
  96. offset = addr & 0xffff;
  97. len = sg_len;
  98. if ((offset + sg_len) > 0x10000)
  99. len = 0x10000 - offset;
  100. ap->prd[pi].addr = cpu_to_le32(addr);
  101. ap->prd[pi].flags_len = cpu_to_le32(len & 0xffff);
  102. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
  103. pi++;
  104. sg_len -= len;
  105. addr += len;
  106. }
  107. }
  108. ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
  109. }
  110. /**
  111. * ata_fill_sg_dumb - Fill PCI IDE PRD table
  112. * @qc: Metadata associated with taskfile to be transferred
  113. *
  114. * Fill PCI IDE PRD (scatter-gather) table with segments
  115. * associated with the current disk command. Perform the fill
  116. * so that we avoid writing any length 64K records for
  117. * controllers that don't follow the spec.
  118. *
  119. * LOCKING:
  120. * spin_lock_irqsave(host lock)
  121. *
  122. */
  123. static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
  124. {
  125. struct ata_port *ap = qc->ap;
  126. struct scatterlist *sg;
  127. unsigned int si, pi;
  128. pi = 0;
  129. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  130. u32 addr, offset;
  131. u32 sg_len, len, blen;
  132. /* determine if physical DMA addr spans 64K boundary.
  133. * Note h/w doesn't support 64-bit, so we unconditionally
  134. * truncate dma_addr_t to u32.
  135. */
  136. addr = (u32) sg_dma_address(sg);
  137. sg_len = sg_dma_len(sg);
  138. while (sg_len) {
  139. offset = addr & 0xffff;
  140. len = sg_len;
  141. if ((offset + sg_len) > 0x10000)
  142. len = 0x10000 - offset;
  143. blen = len & 0xffff;
  144. ap->prd[pi].addr = cpu_to_le32(addr);
  145. if (blen == 0) {
  146. /* Some PATA chipsets like the CS5530 can't
  147. cope with 0x0000 meaning 64K as the spec says */
  148. ap->prd[pi].flags_len = cpu_to_le32(0x8000);
  149. blen = 0x8000;
  150. ap->prd[++pi].addr = cpu_to_le32(addr + 0x8000);
  151. }
  152. ap->prd[pi].flags_len = cpu_to_le32(blen);
  153. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
  154. pi++;
  155. sg_len -= len;
  156. addr += len;
  157. }
  158. }
  159. ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
  160. }
  161. /**
  162. * ata_sff_qc_prep - Prepare taskfile for submission
  163. * @qc: Metadata associated with taskfile to be prepared
  164. *
  165. * Prepare ATA taskfile for submission.
  166. *
  167. * LOCKING:
  168. * spin_lock_irqsave(host lock)
  169. */
  170. void ata_sff_qc_prep(struct ata_queued_cmd *qc)
  171. {
  172. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  173. return;
  174. ata_fill_sg(qc);
  175. }
  176. /**
  177. * ata_sff_dumb_qc_prep - Prepare taskfile for submission
  178. * @qc: Metadata associated with taskfile to be prepared
  179. *
  180. * Prepare ATA taskfile for submission.
  181. *
  182. * LOCKING:
  183. * spin_lock_irqsave(host lock)
  184. */
  185. void ata_sff_dumb_qc_prep(struct ata_queued_cmd *qc)
  186. {
  187. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  188. return;
  189. ata_fill_sg_dumb(qc);
  190. }
  191. /**
  192. * ata_sff_check_status - Read device status reg & clear interrupt
  193. * @ap: port where the device is
  194. *
  195. * Reads ATA taskfile status register for currently-selected device
  196. * and return its value. This also clears pending interrupts
  197. * from this device
  198. *
  199. * LOCKING:
  200. * Inherited from caller.
  201. */
  202. u8 ata_sff_check_status(struct ata_port *ap)
  203. {
  204. return ioread8(ap->ioaddr.status_addr);
  205. }
  206. /**
  207. * ata_sff_altstatus - Read device alternate status reg
  208. * @ap: port where the device is
  209. *
  210. * Reads ATA taskfile alternate status register for
  211. * currently-selected device and return its value.
  212. *
  213. * Note: may NOT be used as the check_altstatus() entry in
  214. * ata_port_operations.
  215. *
  216. * LOCKING:
  217. * Inherited from caller.
  218. */
  219. u8 ata_sff_altstatus(struct ata_port *ap)
  220. {
  221. if (ap->ops->sff_check_altstatus)
  222. return ap->ops->sff_check_altstatus(ap);
  223. return ioread8(ap->ioaddr.altstatus_addr);
  224. }
  225. /**
  226. * ata_sff_busy_sleep - sleep until BSY clears, or timeout
  227. * @ap: port containing status register to be polled
  228. * @tmout_pat: impatience timeout
  229. * @tmout: overall timeout
  230. *
  231. * Sleep until ATA Status register bit BSY clears,
  232. * or a timeout occurs.
  233. *
  234. * LOCKING:
  235. * Kernel thread context (may sleep).
  236. *
  237. * RETURNS:
  238. * 0 on success, -errno otherwise.
  239. */
  240. int ata_sff_busy_sleep(struct ata_port *ap,
  241. unsigned long tmout_pat, unsigned long tmout)
  242. {
  243. unsigned long timer_start, timeout;
  244. u8 status;
  245. status = ata_sff_busy_wait(ap, ATA_BUSY, 300);
  246. timer_start = jiffies;
  247. timeout = timer_start + tmout_pat;
  248. while (status != 0xff && (status & ATA_BUSY) &&
  249. time_before(jiffies, timeout)) {
  250. msleep(50);
  251. status = ata_sff_busy_wait(ap, ATA_BUSY, 3);
  252. }
  253. if (status != 0xff && (status & ATA_BUSY))
  254. ata_port_printk(ap, KERN_WARNING,
  255. "port is slow to respond, please be patient "
  256. "(Status 0x%x)\n", status);
  257. timeout = timer_start + tmout;
  258. while (status != 0xff && (status & ATA_BUSY) &&
  259. time_before(jiffies, timeout)) {
  260. msleep(50);
  261. status = ap->ops->sff_check_status(ap);
  262. }
  263. if (status == 0xff)
  264. return -ENODEV;
  265. if (status & ATA_BUSY) {
  266. ata_port_printk(ap, KERN_ERR, "port failed to respond "
  267. "(%lu secs, Status 0x%x)\n",
  268. tmout / HZ, status);
  269. return -EBUSY;
  270. }
  271. return 0;
  272. }
  273. static int ata_sff_check_ready(struct ata_link *link)
  274. {
  275. u8 status = link->ap->ops->sff_check_status(link->ap);
  276. if (!(status & ATA_BUSY))
  277. return 1;
  278. if (status == 0xff)
  279. return -ENODEV;
  280. return 0;
  281. }
  282. /**
  283. * ata_sff_wait_ready - sleep until BSY clears, or timeout
  284. * @link: SFF link to wait ready status for
  285. * @deadline: deadline jiffies for the operation
  286. *
  287. * Sleep until ATA Status register bit BSY clears, or timeout
  288. * occurs.
  289. *
  290. * LOCKING:
  291. * Kernel thread context (may sleep).
  292. *
  293. * RETURNS:
  294. * 0 on success, -errno otherwise.
  295. */
  296. int ata_sff_wait_ready(struct ata_link *link, unsigned long deadline)
  297. {
  298. return ata_wait_ready(link, deadline, ata_sff_check_ready);
  299. }
  300. /**
  301. * ata_sff_dev_select - Select device 0/1 on ATA bus
  302. * @ap: ATA channel to manipulate
  303. * @device: ATA device (numbered from zero) to select
  304. *
  305. * Use the method defined in the ATA specification to
  306. * make either device 0, or device 1, active on the
  307. * ATA channel. Works with both PIO and MMIO.
  308. *
  309. * May be used as the dev_select() entry in ata_port_operations.
  310. *
  311. * LOCKING:
  312. * caller.
  313. */
  314. void ata_sff_dev_select(struct ata_port *ap, unsigned int device)
  315. {
  316. u8 tmp;
  317. if (device == 0)
  318. tmp = ATA_DEVICE_OBS;
  319. else
  320. tmp = ATA_DEVICE_OBS | ATA_DEV1;
  321. iowrite8(tmp, ap->ioaddr.device_addr);
  322. ata_sff_pause(ap); /* needed; also flushes, for mmio */
  323. }
  324. /**
  325. * ata_dev_select - Select device 0/1 on ATA bus
  326. * @ap: ATA channel to manipulate
  327. * @device: ATA device (numbered from zero) to select
  328. * @wait: non-zero to wait for Status register BSY bit to clear
  329. * @can_sleep: non-zero if context allows sleeping
  330. *
  331. * Use the method defined in the ATA specification to
  332. * make either device 0, or device 1, active on the
  333. * ATA channel.
  334. *
  335. * This is a high-level version of ata_sff_dev_select(), which
  336. * additionally provides the services of inserting the proper
  337. * pauses and status polling, where needed.
  338. *
  339. * LOCKING:
  340. * caller.
  341. */
  342. void ata_dev_select(struct ata_port *ap, unsigned int device,
  343. unsigned int wait, unsigned int can_sleep)
  344. {
  345. if (ata_msg_probe(ap))
  346. ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, "
  347. "device %u, wait %u\n", device, wait);
  348. if (wait)
  349. ata_wait_idle(ap);
  350. ap->ops->sff_dev_select(ap, device);
  351. if (wait) {
  352. if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI)
  353. msleep(150);
  354. ata_wait_idle(ap);
  355. }
  356. }
  357. /**
  358. * ata_sff_irq_on - Enable interrupts on a port.
  359. * @ap: Port on which interrupts are enabled.
  360. *
  361. * Enable interrupts on a legacy IDE device using MMIO or PIO,
  362. * wait for idle, clear any pending interrupts.
  363. *
  364. * LOCKING:
  365. * Inherited from caller.
  366. */
  367. u8 ata_sff_irq_on(struct ata_port *ap)
  368. {
  369. struct ata_ioports *ioaddr = &ap->ioaddr;
  370. u8 tmp;
  371. ap->ctl &= ~ATA_NIEN;
  372. ap->last_ctl = ap->ctl;
  373. if (ioaddr->ctl_addr)
  374. iowrite8(ap->ctl, ioaddr->ctl_addr);
  375. tmp = ata_wait_idle(ap);
  376. ap->ops->sff_irq_clear(ap);
  377. return tmp;
  378. }
  379. /**
  380. * ata_sff_irq_clear - Clear PCI IDE BMDMA interrupt.
  381. * @ap: Port associated with this ATA transaction.
  382. *
  383. * Clear interrupt and error flags in DMA status register.
  384. *
  385. * May be used as the irq_clear() entry in ata_port_operations.
  386. *
  387. * LOCKING:
  388. * spin_lock_irqsave(host lock)
  389. */
  390. void ata_sff_irq_clear(struct ata_port *ap)
  391. {
  392. void __iomem *mmio = ap->ioaddr.bmdma_addr;
  393. if (!mmio)
  394. return;
  395. iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS);
  396. }
  397. /**
  398. * ata_sff_tf_load - send taskfile registers to host controller
  399. * @ap: Port to which output is sent
  400. * @tf: ATA taskfile register set
  401. *
  402. * Outputs ATA taskfile to standard ATA host controller.
  403. *
  404. * LOCKING:
  405. * Inherited from caller.
  406. */
  407. void ata_sff_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
  408. {
  409. struct ata_ioports *ioaddr = &ap->ioaddr;
  410. unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
  411. if (tf->ctl != ap->last_ctl) {
  412. if (ioaddr->ctl_addr)
  413. iowrite8(tf->ctl, ioaddr->ctl_addr);
  414. ap->last_ctl = tf->ctl;
  415. ata_wait_idle(ap);
  416. }
  417. if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
  418. WARN_ON(!ioaddr->ctl_addr);
  419. iowrite8(tf->hob_feature, ioaddr->feature_addr);
  420. iowrite8(tf->hob_nsect, ioaddr->nsect_addr);
  421. iowrite8(tf->hob_lbal, ioaddr->lbal_addr);
  422. iowrite8(tf->hob_lbam, ioaddr->lbam_addr);
  423. iowrite8(tf->hob_lbah, ioaddr->lbah_addr);
  424. VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
  425. tf->hob_feature,
  426. tf->hob_nsect,
  427. tf->hob_lbal,
  428. tf->hob_lbam,
  429. tf->hob_lbah);
  430. }
  431. if (is_addr) {
  432. iowrite8(tf->feature, ioaddr->feature_addr);
  433. iowrite8(tf->nsect, ioaddr->nsect_addr);
  434. iowrite8(tf->lbal, ioaddr->lbal_addr);
  435. iowrite8(tf->lbam, ioaddr->lbam_addr);
  436. iowrite8(tf->lbah, ioaddr->lbah_addr);
  437. VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
  438. tf->feature,
  439. tf->nsect,
  440. tf->lbal,
  441. tf->lbam,
  442. tf->lbah);
  443. }
  444. if (tf->flags & ATA_TFLAG_DEVICE) {
  445. iowrite8(tf->device, ioaddr->device_addr);
  446. VPRINTK("device 0x%X\n", tf->device);
  447. }
  448. ata_wait_idle(ap);
  449. }
  450. /**
  451. * ata_sff_tf_read - input device's ATA taskfile shadow registers
  452. * @ap: Port from which input is read
  453. * @tf: ATA taskfile register set for storing input
  454. *
  455. * Reads ATA taskfile registers for currently-selected device
  456. * into @tf. Assumes the device has a fully SFF compliant task file
  457. * layout and behaviour. If you device does not (eg has a different
  458. * status method) then you will need to provide a replacement tf_read
  459. *
  460. * LOCKING:
  461. * Inherited from caller.
  462. */
  463. void ata_sff_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  464. {
  465. struct ata_ioports *ioaddr = &ap->ioaddr;
  466. tf->command = ata_sff_check_status(ap);
  467. tf->feature = ioread8(ioaddr->error_addr);
  468. tf->nsect = ioread8(ioaddr->nsect_addr);
  469. tf->lbal = ioread8(ioaddr->lbal_addr);
  470. tf->lbam = ioread8(ioaddr->lbam_addr);
  471. tf->lbah = ioread8(ioaddr->lbah_addr);
  472. tf->device = ioread8(ioaddr->device_addr);
  473. if (tf->flags & ATA_TFLAG_LBA48) {
  474. if (likely(ioaddr->ctl_addr)) {
  475. iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
  476. tf->hob_feature = ioread8(ioaddr->error_addr);
  477. tf->hob_nsect = ioread8(ioaddr->nsect_addr);
  478. tf->hob_lbal = ioread8(ioaddr->lbal_addr);
  479. tf->hob_lbam = ioread8(ioaddr->lbam_addr);
  480. tf->hob_lbah = ioread8(ioaddr->lbah_addr);
  481. iowrite8(tf->ctl, ioaddr->ctl_addr);
  482. ap->last_ctl = tf->ctl;
  483. } else
  484. WARN_ON(1);
  485. }
  486. }
  487. /**
  488. * ata_sff_exec_command - issue ATA command to host controller
  489. * @ap: port to which command is being issued
  490. * @tf: ATA taskfile register set
  491. *
  492. * Issues ATA command, with proper synchronization with interrupt
  493. * handler / other threads.
  494. *
  495. * LOCKING:
  496. * spin_lock_irqsave(host lock)
  497. */
  498. void ata_sff_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
  499. {
  500. DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
  501. iowrite8(tf->command, ap->ioaddr.command_addr);
  502. ata_sff_pause(ap);
  503. }
  504. /**
  505. * ata_tf_to_host - issue ATA taskfile to host controller
  506. * @ap: port to which command is being issued
  507. * @tf: ATA taskfile register set
  508. *
  509. * Issues ATA taskfile register set to ATA host controller,
  510. * with proper synchronization with interrupt handler and
  511. * other threads.
  512. *
  513. * LOCKING:
  514. * spin_lock_irqsave(host lock)
  515. */
  516. static inline void ata_tf_to_host(struct ata_port *ap,
  517. const struct ata_taskfile *tf)
  518. {
  519. ap->ops->sff_tf_load(ap, tf);
  520. ap->ops->sff_exec_command(ap, tf);
  521. }
  522. /**
  523. * ata_sff_data_xfer - Transfer data by PIO
  524. * @dev: device to target
  525. * @buf: data buffer
  526. * @buflen: buffer length
  527. * @rw: read/write
  528. *
  529. * Transfer data from/to the device data register by PIO.
  530. *
  531. * LOCKING:
  532. * Inherited from caller.
  533. *
  534. * RETURNS:
  535. * Bytes consumed.
  536. */
  537. unsigned int ata_sff_data_xfer(struct ata_device *dev, unsigned char *buf,
  538. unsigned int buflen, int rw)
  539. {
  540. struct ata_port *ap = dev->link->ap;
  541. void __iomem *data_addr = ap->ioaddr.data_addr;
  542. unsigned int words = buflen >> 1;
  543. /* Transfer multiple of 2 bytes */
  544. if (rw == READ)
  545. ioread16_rep(data_addr, buf, words);
  546. else
  547. iowrite16_rep(data_addr, buf, words);
  548. /* Transfer trailing 1 byte, if any. */
  549. if (unlikely(buflen & 0x01)) {
  550. __le16 align_buf[1] = { 0 };
  551. unsigned char *trailing_buf = buf + buflen - 1;
  552. if (rw == READ) {
  553. align_buf[0] = cpu_to_le16(ioread16(data_addr));
  554. memcpy(trailing_buf, align_buf, 1);
  555. } else {
  556. memcpy(align_buf, trailing_buf, 1);
  557. iowrite16(le16_to_cpu(align_buf[0]), data_addr);
  558. }
  559. words++;
  560. }
  561. return words << 1;
  562. }
  563. /**
  564. * ata_sff_data_xfer_noirq - Transfer data by PIO
  565. * @dev: device to target
  566. * @buf: data buffer
  567. * @buflen: buffer length
  568. * @rw: read/write
  569. *
  570. * Transfer data from/to the device data register by PIO. Do the
  571. * transfer with interrupts disabled.
  572. *
  573. * LOCKING:
  574. * Inherited from caller.
  575. *
  576. * RETURNS:
  577. * Bytes consumed.
  578. */
  579. unsigned int ata_sff_data_xfer_noirq(struct ata_device *dev, unsigned char *buf,
  580. unsigned int buflen, int rw)
  581. {
  582. unsigned long flags;
  583. unsigned int consumed;
  584. local_irq_save(flags);
  585. consumed = ata_sff_data_xfer(dev, buf, buflen, rw);
  586. local_irq_restore(flags);
  587. return consumed;
  588. }
  589. /**
  590. * ata_pio_sector - Transfer a sector of data.
  591. * @qc: Command on going
  592. *
  593. * Transfer qc->sect_size bytes of data from/to the ATA device.
  594. *
  595. * LOCKING:
  596. * Inherited from caller.
  597. */
  598. static void ata_pio_sector(struct ata_queued_cmd *qc)
  599. {
  600. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  601. struct ata_port *ap = qc->ap;
  602. struct page *page;
  603. unsigned int offset;
  604. unsigned char *buf;
  605. if (qc->curbytes == qc->nbytes - qc->sect_size)
  606. ap->hsm_task_state = HSM_ST_LAST;
  607. page = sg_page(qc->cursg);
  608. offset = qc->cursg->offset + qc->cursg_ofs;
  609. /* get the current page and offset */
  610. page = nth_page(page, (offset >> PAGE_SHIFT));
  611. offset %= PAGE_SIZE;
  612. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  613. if (PageHighMem(page)) {
  614. unsigned long flags;
  615. /* FIXME: use a bounce buffer */
  616. local_irq_save(flags);
  617. buf = kmap_atomic(page, KM_IRQ0);
  618. /* do the actual data transfer */
  619. ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
  620. do_write);
  621. kunmap_atomic(buf, KM_IRQ0);
  622. local_irq_restore(flags);
  623. } else {
  624. buf = page_address(page);
  625. ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
  626. do_write);
  627. }
  628. qc->curbytes += qc->sect_size;
  629. qc->cursg_ofs += qc->sect_size;
  630. if (qc->cursg_ofs == qc->cursg->length) {
  631. qc->cursg = sg_next(qc->cursg);
  632. qc->cursg_ofs = 0;
  633. }
  634. }
  635. /**
  636. * ata_pio_sectors - Transfer one or many sectors.
  637. * @qc: Command on going
  638. *
  639. * Transfer one or many sectors of data from/to the
  640. * ATA device for the DRQ request.
  641. *
  642. * LOCKING:
  643. * Inherited from caller.
  644. */
  645. static void ata_pio_sectors(struct ata_queued_cmd *qc)
  646. {
  647. if (is_multi_taskfile(&qc->tf)) {
  648. /* READ/WRITE MULTIPLE */
  649. unsigned int nsect;
  650. WARN_ON(qc->dev->multi_count == 0);
  651. nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
  652. qc->dev->multi_count);
  653. while (nsect--)
  654. ata_pio_sector(qc);
  655. } else
  656. ata_pio_sector(qc);
  657. ata_sff_altstatus(qc->ap); /* flush */
  658. }
  659. /**
  660. * atapi_send_cdb - Write CDB bytes to hardware
  661. * @ap: Port to which ATAPI device is attached.
  662. * @qc: Taskfile currently active
  663. *
  664. * When device has indicated its readiness to accept
  665. * a CDB, this function is called. Send the CDB.
  666. *
  667. * LOCKING:
  668. * caller.
  669. */
  670. static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
  671. {
  672. /* send SCSI cdb */
  673. DPRINTK("send cdb\n");
  674. WARN_ON(qc->dev->cdb_len < 12);
  675. ap->ops->sff_data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
  676. ata_sff_altstatus(ap); /* flush */
  677. switch (qc->tf.protocol) {
  678. case ATAPI_PROT_PIO:
  679. ap->hsm_task_state = HSM_ST;
  680. break;
  681. case ATAPI_PROT_NODATA:
  682. ap->hsm_task_state = HSM_ST_LAST;
  683. break;
  684. case ATAPI_PROT_DMA:
  685. ap->hsm_task_state = HSM_ST_LAST;
  686. /* initiate bmdma */
  687. ap->ops->bmdma_start(qc);
  688. break;
  689. }
  690. }
  691. /**
  692. * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
  693. * @qc: Command on going
  694. * @bytes: number of bytes
  695. *
  696. * Transfer Transfer data from/to the ATAPI device.
  697. *
  698. * LOCKING:
  699. * Inherited from caller.
  700. *
  701. */
  702. static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
  703. {
  704. int rw = (qc->tf.flags & ATA_TFLAG_WRITE) ? WRITE : READ;
  705. struct ata_port *ap = qc->ap;
  706. struct ata_device *dev = qc->dev;
  707. struct ata_eh_info *ehi = &dev->link->eh_info;
  708. struct scatterlist *sg;
  709. struct page *page;
  710. unsigned char *buf;
  711. unsigned int offset, count, consumed;
  712. next_sg:
  713. sg = qc->cursg;
  714. if (unlikely(!sg)) {
  715. ata_ehi_push_desc(ehi, "unexpected or too much trailing data "
  716. "buf=%u cur=%u bytes=%u",
  717. qc->nbytes, qc->curbytes, bytes);
  718. return -1;
  719. }
  720. page = sg_page(sg);
  721. offset = sg->offset + qc->cursg_ofs;
  722. /* get the current page and offset */
  723. page = nth_page(page, (offset >> PAGE_SHIFT));
  724. offset %= PAGE_SIZE;
  725. /* don't overrun current sg */
  726. count = min(sg->length - qc->cursg_ofs, bytes);
  727. /* don't cross page boundaries */
  728. count = min(count, (unsigned int)PAGE_SIZE - offset);
  729. DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
  730. if (PageHighMem(page)) {
  731. unsigned long flags;
  732. /* FIXME: use bounce buffer */
  733. local_irq_save(flags);
  734. buf = kmap_atomic(page, KM_IRQ0);
  735. /* do the actual data transfer */
  736. consumed = ap->ops->sff_data_xfer(dev, buf + offset, count, rw);
  737. kunmap_atomic(buf, KM_IRQ0);
  738. local_irq_restore(flags);
  739. } else {
  740. buf = page_address(page);
  741. consumed = ap->ops->sff_data_xfer(dev, buf + offset, count, rw);
  742. }
  743. bytes -= min(bytes, consumed);
  744. qc->curbytes += count;
  745. qc->cursg_ofs += count;
  746. if (qc->cursg_ofs == sg->length) {
  747. qc->cursg = sg_next(qc->cursg);
  748. qc->cursg_ofs = 0;
  749. }
  750. /* consumed can be larger than count only for the last transfer */
  751. WARN_ON(qc->cursg && count != consumed);
  752. if (bytes)
  753. goto next_sg;
  754. return 0;
  755. }
  756. /**
  757. * atapi_pio_bytes - Transfer data from/to the ATAPI device.
  758. * @qc: Command on going
  759. *
  760. * Transfer Transfer data from/to the ATAPI device.
  761. *
  762. * LOCKING:
  763. * Inherited from caller.
  764. */
  765. static void atapi_pio_bytes(struct ata_queued_cmd *qc)
  766. {
  767. struct ata_port *ap = qc->ap;
  768. struct ata_device *dev = qc->dev;
  769. struct ata_eh_info *ehi = &dev->link->eh_info;
  770. unsigned int ireason, bc_lo, bc_hi, bytes;
  771. int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
  772. /* Abuse qc->result_tf for temp storage of intermediate TF
  773. * here to save some kernel stack usage.
  774. * For normal completion, qc->result_tf is not relevant. For
  775. * error, qc->result_tf is later overwritten by ata_qc_complete().
  776. * So, the correctness of qc->result_tf is not affected.
  777. */
  778. ap->ops->sff_tf_read(ap, &qc->result_tf);
  779. ireason = qc->result_tf.nsect;
  780. bc_lo = qc->result_tf.lbam;
  781. bc_hi = qc->result_tf.lbah;
  782. bytes = (bc_hi << 8) | bc_lo;
  783. /* shall be cleared to zero, indicating xfer of data */
  784. if (unlikely(ireason & (1 << 0)))
  785. goto atapi_check;
  786. /* make sure transfer direction matches expected */
  787. i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
  788. if (unlikely(do_write != i_write))
  789. goto atapi_check;
  790. if (unlikely(!bytes))
  791. goto atapi_check;
  792. VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
  793. if (unlikely(__atapi_pio_bytes(qc, bytes)))
  794. goto err_out;
  795. ata_sff_altstatus(ap); /* flush */
  796. return;
  797. atapi_check:
  798. ata_ehi_push_desc(ehi, "ATAPI check failed (ireason=0x%x bytes=%u)",
  799. ireason, bytes);
  800. err_out:
  801. qc->err_mask |= AC_ERR_HSM;
  802. ap->hsm_task_state = HSM_ST_ERR;
  803. }
  804. /**
  805. * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
  806. * @ap: the target ata_port
  807. * @qc: qc on going
  808. *
  809. * RETURNS:
  810. * 1 if ok in workqueue, 0 otherwise.
  811. */
  812. static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
  813. {
  814. if (qc->tf.flags & ATA_TFLAG_POLLING)
  815. return 1;
  816. if (ap->hsm_task_state == HSM_ST_FIRST) {
  817. if (qc->tf.protocol == ATA_PROT_PIO &&
  818. (qc->tf.flags & ATA_TFLAG_WRITE))
  819. return 1;
  820. if (ata_is_atapi(qc->tf.protocol) &&
  821. !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  822. return 1;
  823. }
  824. return 0;
  825. }
  826. /**
  827. * ata_hsm_qc_complete - finish a qc running on standard HSM
  828. * @qc: Command to complete
  829. * @in_wq: 1 if called from workqueue, 0 otherwise
  830. *
  831. * Finish @qc which is running on standard HSM.
  832. *
  833. * LOCKING:
  834. * If @in_wq is zero, spin_lock_irqsave(host lock).
  835. * Otherwise, none on entry and grabs host lock.
  836. */
  837. static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
  838. {
  839. struct ata_port *ap = qc->ap;
  840. unsigned long flags;
  841. if (ap->ops->error_handler) {
  842. if (in_wq) {
  843. spin_lock_irqsave(ap->lock, flags);
  844. /* EH might have kicked in while host lock is
  845. * released.
  846. */
  847. qc = ata_qc_from_tag(ap, qc->tag);
  848. if (qc) {
  849. if (likely(!(qc->err_mask & AC_ERR_HSM))) {
  850. ap->ops->sff_irq_on(ap);
  851. ata_qc_complete(qc);
  852. } else
  853. ata_port_freeze(ap);
  854. }
  855. spin_unlock_irqrestore(ap->lock, flags);
  856. } else {
  857. if (likely(!(qc->err_mask & AC_ERR_HSM)))
  858. ata_qc_complete(qc);
  859. else
  860. ata_port_freeze(ap);
  861. }
  862. } else {
  863. if (in_wq) {
  864. spin_lock_irqsave(ap->lock, flags);
  865. ap->ops->sff_irq_on(ap);
  866. ata_qc_complete(qc);
  867. spin_unlock_irqrestore(ap->lock, flags);
  868. } else
  869. ata_qc_complete(qc);
  870. }
  871. }
  872. /**
  873. * ata_sff_hsm_move - move the HSM to the next state.
  874. * @ap: the target ata_port
  875. * @qc: qc on going
  876. * @status: current device status
  877. * @in_wq: 1 if called from workqueue, 0 otherwise
  878. *
  879. * RETURNS:
  880. * 1 when poll next status needed, 0 otherwise.
  881. */
  882. int ata_sff_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
  883. u8 status, int in_wq)
  884. {
  885. unsigned long flags = 0;
  886. int poll_next;
  887. WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
  888. /* Make sure ata_sff_qc_issue() does not throw things
  889. * like DMA polling into the workqueue. Notice that
  890. * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
  891. */
  892. WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
  893. fsm_start:
  894. DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
  895. ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
  896. switch (ap->hsm_task_state) {
  897. case HSM_ST_FIRST:
  898. /* Send first data block or PACKET CDB */
  899. /* If polling, we will stay in the work queue after
  900. * sending the data. Otherwise, interrupt handler
  901. * takes over after sending the data.
  902. */
  903. poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
  904. /* check device status */
  905. if (unlikely((status & ATA_DRQ) == 0)) {
  906. /* handle BSY=0, DRQ=0 as error */
  907. if (likely(status & (ATA_ERR | ATA_DF)))
  908. /* device stops HSM for abort/error */
  909. qc->err_mask |= AC_ERR_DEV;
  910. else
  911. /* HSM violation. Let EH handle this */
  912. qc->err_mask |= AC_ERR_HSM;
  913. ap->hsm_task_state = HSM_ST_ERR;
  914. goto fsm_start;
  915. }
  916. /* Device should not ask for data transfer (DRQ=1)
  917. * when it finds something wrong.
  918. * We ignore DRQ here and stop the HSM by
  919. * changing hsm_task_state to HSM_ST_ERR and
  920. * let the EH abort the command or reset the device.
  921. */
  922. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  923. /* Some ATAPI tape drives forget to clear the ERR bit
  924. * when doing the next command (mostly request sense).
  925. * We ignore ERR here to workaround and proceed sending
  926. * the CDB.
  927. */
  928. if (!(qc->dev->horkage & ATA_HORKAGE_STUCK_ERR)) {
  929. ata_port_printk(ap, KERN_WARNING,
  930. "DRQ=1 with device error, "
  931. "dev_stat 0x%X\n", status);
  932. qc->err_mask |= AC_ERR_HSM;
  933. ap->hsm_task_state = HSM_ST_ERR;
  934. goto fsm_start;
  935. }
  936. }
  937. /* Send the CDB (atapi) or the first data block (ata pio out).
  938. * During the state transition, interrupt handler shouldn't
  939. * be invoked before the data transfer is complete and
  940. * hsm_task_state is changed. Hence, the following locking.
  941. */
  942. if (in_wq)
  943. spin_lock_irqsave(ap->lock, flags);
  944. if (qc->tf.protocol == ATA_PROT_PIO) {
  945. /* PIO data out protocol.
  946. * send first data block.
  947. */
  948. /* ata_pio_sectors() might change the state
  949. * to HSM_ST_LAST. so, the state is changed here
  950. * before ata_pio_sectors().
  951. */
  952. ap->hsm_task_state = HSM_ST;
  953. ata_pio_sectors(qc);
  954. } else
  955. /* send CDB */
  956. atapi_send_cdb(ap, qc);
  957. if (in_wq)
  958. spin_unlock_irqrestore(ap->lock, flags);
  959. /* if polling, ata_pio_task() handles the rest.
  960. * otherwise, interrupt handler takes over from here.
  961. */
  962. break;
  963. case HSM_ST:
  964. /* complete command or read/write the data register */
  965. if (qc->tf.protocol == ATAPI_PROT_PIO) {
  966. /* ATAPI PIO protocol */
  967. if ((status & ATA_DRQ) == 0) {
  968. /* No more data to transfer or device error.
  969. * Device error will be tagged in HSM_ST_LAST.
  970. */
  971. ap->hsm_task_state = HSM_ST_LAST;
  972. goto fsm_start;
  973. }
  974. /* Device should not ask for data transfer (DRQ=1)
  975. * when it finds something wrong.
  976. * We ignore DRQ here and stop the HSM by
  977. * changing hsm_task_state to HSM_ST_ERR and
  978. * let the EH abort the command or reset the device.
  979. */
  980. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  981. ata_port_printk(ap, KERN_WARNING, "DRQ=1 with "
  982. "device error, dev_stat 0x%X\n",
  983. status);
  984. qc->err_mask |= AC_ERR_HSM;
  985. ap->hsm_task_state = HSM_ST_ERR;
  986. goto fsm_start;
  987. }
  988. atapi_pio_bytes(qc);
  989. if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
  990. /* bad ireason reported by device */
  991. goto fsm_start;
  992. } else {
  993. /* ATA PIO protocol */
  994. if (unlikely((status & ATA_DRQ) == 0)) {
  995. /* handle BSY=0, DRQ=0 as error */
  996. if (likely(status & (ATA_ERR | ATA_DF)))
  997. /* device stops HSM for abort/error */
  998. qc->err_mask |= AC_ERR_DEV;
  999. else
  1000. /* HSM violation. Let EH handle this.
  1001. * Phantom devices also trigger this
  1002. * condition. Mark hint.
  1003. */
  1004. qc->err_mask |= AC_ERR_HSM |
  1005. AC_ERR_NODEV_HINT;
  1006. ap->hsm_task_state = HSM_ST_ERR;
  1007. goto fsm_start;
  1008. }
  1009. /* For PIO reads, some devices may ask for
  1010. * data transfer (DRQ=1) alone with ERR=1.
  1011. * We respect DRQ here and transfer one
  1012. * block of junk data before changing the
  1013. * hsm_task_state to HSM_ST_ERR.
  1014. *
  1015. * For PIO writes, ERR=1 DRQ=1 doesn't make
  1016. * sense since the data block has been
  1017. * transferred to the device.
  1018. */
  1019. if (unlikely(status & (ATA_ERR | ATA_DF))) {
  1020. /* data might be corrputed */
  1021. qc->err_mask |= AC_ERR_DEV;
  1022. if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
  1023. ata_pio_sectors(qc);
  1024. status = ata_wait_idle(ap);
  1025. }
  1026. if (status & (ATA_BUSY | ATA_DRQ))
  1027. qc->err_mask |= AC_ERR_HSM;
  1028. /* ata_pio_sectors() might change the
  1029. * state to HSM_ST_LAST. so, the state
  1030. * is changed after ata_pio_sectors().
  1031. */
  1032. ap->hsm_task_state = HSM_ST_ERR;
  1033. goto fsm_start;
  1034. }
  1035. ata_pio_sectors(qc);
  1036. if (ap->hsm_task_state == HSM_ST_LAST &&
  1037. (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
  1038. /* all data read */
  1039. status = ata_wait_idle(ap);
  1040. goto fsm_start;
  1041. }
  1042. }
  1043. poll_next = 1;
  1044. break;
  1045. case HSM_ST_LAST:
  1046. if (unlikely(!ata_ok(status))) {
  1047. qc->err_mask |= __ac_err_mask(status);
  1048. ap->hsm_task_state = HSM_ST_ERR;
  1049. goto fsm_start;
  1050. }
  1051. /* no more data to transfer */
  1052. DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
  1053. ap->print_id, qc->dev->devno, status);
  1054. WARN_ON(qc->err_mask);
  1055. ap->hsm_task_state = HSM_ST_IDLE;
  1056. /* complete taskfile transaction */
  1057. ata_hsm_qc_complete(qc, in_wq);
  1058. poll_next = 0;
  1059. break;
  1060. case HSM_ST_ERR:
  1061. /* make sure qc->err_mask is available to
  1062. * know what's wrong and recover
  1063. */
  1064. WARN_ON(qc->err_mask == 0);
  1065. ap->hsm_task_state = HSM_ST_IDLE;
  1066. /* complete taskfile transaction */
  1067. ata_hsm_qc_complete(qc, in_wq);
  1068. poll_next = 0;
  1069. break;
  1070. default:
  1071. poll_next = 0;
  1072. BUG();
  1073. }
  1074. return poll_next;
  1075. }
  1076. void ata_pio_task(struct work_struct *work)
  1077. {
  1078. struct ata_port *ap =
  1079. container_of(work, struct ata_port, port_task.work);
  1080. struct ata_queued_cmd *qc = ap->port_task_data;
  1081. u8 status;
  1082. int poll_next;
  1083. fsm_start:
  1084. WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
  1085. /*
  1086. * This is purely heuristic. This is a fast path.
  1087. * Sometimes when we enter, BSY will be cleared in
  1088. * a chk-status or two. If not, the drive is probably seeking
  1089. * or something. Snooze for a couple msecs, then
  1090. * chk-status again. If still busy, queue delayed work.
  1091. */
  1092. status = ata_sff_busy_wait(ap, ATA_BUSY, 5);
  1093. if (status & ATA_BUSY) {
  1094. msleep(2);
  1095. status = ata_sff_busy_wait(ap, ATA_BUSY, 10);
  1096. if (status & ATA_BUSY) {
  1097. ata_pio_queue_task(ap, qc, ATA_SHORT_PAUSE);
  1098. return;
  1099. }
  1100. }
  1101. /* move the HSM */
  1102. poll_next = ata_sff_hsm_move(ap, qc, status, 1);
  1103. /* another command or interrupt handler
  1104. * may be running at this point.
  1105. */
  1106. if (poll_next)
  1107. goto fsm_start;
  1108. }
  1109. /**
  1110. * ata_sff_qc_issue - issue taskfile to device in proto-dependent manner
  1111. * @qc: command to issue to device
  1112. *
  1113. * Using various libata functions and hooks, this function
  1114. * starts an ATA command. ATA commands are grouped into
  1115. * classes called "protocols", and issuing each type of protocol
  1116. * is slightly different.
  1117. *
  1118. * May be used as the qc_issue() entry in ata_port_operations.
  1119. *
  1120. * LOCKING:
  1121. * spin_lock_irqsave(host lock)
  1122. *
  1123. * RETURNS:
  1124. * Zero on success, AC_ERR_* mask on failure
  1125. */
  1126. unsigned int ata_sff_qc_issue(struct ata_queued_cmd *qc)
  1127. {
  1128. struct ata_port *ap = qc->ap;
  1129. /* Use polling pio if the LLD doesn't handle
  1130. * interrupt driven pio and atapi CDB interrupt.
  1131. */
  1132. if (ap->flags & ATA_FLAG_PIO_POLLING) {
  1133. switch (qc->tf.protocol) {
  1134. case ATA_PROT_PIO:
  1135. case ATA_PROT_NODATA:
  1136. case ATAPI_PROT_PIO:
  1137. case ATAPI_PROT_NODATA:
  1138. qc->tf.flags |= ATA_TFLAG_POLLING;
  1139. break;
  1140. case ATAPI_PROT_DMA:
  1141. if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
  1142. /* see ata_dma_blacklisted() */
  1143. BUG();
  1144. break;
  1145. default:
  1146. break;
  1147. }
  1148. }
  1149. /* select the device */
  1150. ata_dev_select(ap, qc->dev->devno, 1, 0);
  1151. /* start the command */
  1152. switch (qc->tf.protocol) {
  1153. case ATA_PROT_NODATA:
  1154. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1155. ata_qc_set_polling(qc);
  1156. ata_tf_to_host(ap, &qc->tf);
  1157. ap->hsm_task_state = HSM_ST_LAST;
  1158. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1159. ata_pio_queue_task(ap, qc, 0);
  1160. break;
  1161. case ATA_PROT_DMA:
  1162. WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
  1163. ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
  1164. ap->ops->bmdma_setup(qc); /* set up bmdma */
  1165. ap->ops->bmdma_start(qc); /* initiate bmdma */
  1166. ap->hsm_task_state = HSM_ST_LAST;
  1167. break;
  1168. case ATA_PROT_PIO:
  1169. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1170. ata_qc_set_polling(qc);
  1171. ata_tf_to_host(ap, &qc->tf);
  1172. if (qc->tf.flags & ATA_TFLAG_WRITE) {
  1173. /* PIO data out protocol */
  1174. ap->hsm_task_state = HSM_ST_FIRST;
  1175. ata_pio_queue_task(ap, qc, 0);
  1176. /* always send first data block using
  1177. * the ata_pio_task() codepath.
  1178. */
  1179. } else {
  1180. /* PIO data in protocol */
  1181. ap->hsm_task_state = HSM_ST;
  1182. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1183. ata_pio_queue_task(ap, qc, 0);
  1184. /* if polling, ata_pio_task() handles the rest.
  1185. * otherwise, interrupt handler takes over from here.
  1186. */
  1187. }
  1188. break;
  1189. case ATAPI_PROT_PIO:
  1190. case ATAPI_PROT_NODATA:
  1191. if (qc->tf.flags & ATA_TFLAG_POLLING)
  1192. ata_qc_set_polling(qc);
  1193. ata_tf_to_host(ap, &qc->tf);
  1194. ap->hsm_task_state = HSM_ST_FIRST;
  1195. /* send cdb by polling if no cdb interrupt */
  1196. if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
  1197. (qc->tf.flags & ATA_TFLAG_POLLING))
  1198. ata_pio_queue_task(ap, qc, 0);
  1199. break;
  1200. case ATAPI_PROT_DMA:
  1201. WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
  1202. ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
  1203. ap->ops->bmdma_setup(qc); /* set up bmdma */
  1204. ap->hsm_task_state = HSM_ST_FIRST;
  1205. /* send cdb by polling if no cdb interrupt */
  1206. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  1207. ata_pio_queue_task(ap, qc, 0);
  1208. break;
  1209. default:
  1210. WARN_ON(1);
  1211. return AC_ERR_SYSTEM;
  1212. }
  1213. return 0;
  1214. }
  1215. /**
  1216. * ata_sff_host_intr - Handle host interrupt for given (port, task)
  1217. * @ap: Port on which interrupt arrived (possibly...)
  1218. * @qc: Taskfile currently active in engine
  1219. *
  1220. * Handle host interrupt for given queued command. Currently,
  1221. * only DMA interrupts are handled. All other commands are
  1222. * handled via polling with interrupts disabled (nIEN bit).
  1223. *
  1224. * LOCKING:
  1225. * spin_lock_irqsave(host lock)
  1226. *
  1227. * RETURNS:
  1228. * One if interrupt was handled, zero if not (shared irq).
  1229. */
  1230. inline unsigned int ata_sff_host_intr(struct ata_port *ap,
  1231. struct ata_queued_cmd *qc)
  1232. {
  1233. struct ata_eh_info *ehi = &ap->link.eh_info;
  1234. u8 status, host_stat = 0;
  1235. VPRINTK("ata%u: protocol %d task_state %d\n",
  1236. ap->print_id, qc->tf.protocol, ap->hsm_task_state);
  1237. /* Check whether we are expecting interrupt in this state */
  1238. switch (ap->hsm_task_state) {
  1239. case HSM_ST_FIRST:
  1240. /* Some pre-ATAPI-4 devices assert INTRQ
  1241. * at this state when ready to receive CDB.
  1242. */
  1243. /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
  1244. * The flag was turned on only for atapi devices. No
  1245. * need to check ata_is_atapi(qc->tf.protocol) again.
  1246. */
  1247. if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
  1248. goto idle_irq;
  1249. break;
  1250. case HSM_ST_LAST:
  1251. if (qc->tf.protocol == ATA_PROT_DMA ||
  1252. qc->tf.protocol == ATAPI_PROT_DMA) {
  1253. /* check status of DMA engine */
  1254. host_stat = ap->ops->bmdma_status(ap);
  1255. VPRINTK("ata%u: host_stat 0x%X\n",
  1256. ap->print_id, host_stat);
  1257. /* if it's not our irq... */
  1258. if (!(host_stat & ATA_DMA_INTR))
  1259. goto idle_irq;
  1260. /* before we do anything else, clear DMA-Start bit */
  1261. ap->ops->bmdma_stop(qc);
  1262. if (unlikely(host_stat & ATA_DMA_ERR)) {
  1263. /* error when transfering data to/from memory */
  1264. qc->err_mask |= AC_ERR_HOST_BUS;
  1265. ap->hsm_task_state = HSM_ST_ERR;
  1266. }
  1267. }
  1268. break;
  1269. case HSM_ST:
  1270. break;
  1271. default:
  1272. goto idle_irq;
  1273. }
  1274. /* check altstatus */
  1275. status = ata_sff_altstatus(ap);
  1276. if (status & ATA_BUSY)
  1277. goto idle_irq;
  1278. /* check main status, clearing INTRQ */
  1279. status = ap->ops->sff_check_status(ap);
  1280. if (unlikely(status & ATA_BUSY))
  1281. goto idle_irq;
  1282. /* ack bmdma irq events */
  1283. ap->ops->sff_irq_clear(ap);
  1284. ata_sff_hsm_move(ap, qc, status, 0);
  1285. if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
  1286. qc->tf.protocol == ATAPI_PROT_DMA))
  1287. ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
  1288. return 1; /* irq handled */
  1289. idle_irq:
  1290. ap->stats.idle_irq++;
  1291. #ifdef ATA_IRQ_TRAP
  1292. if ((ap->stats.idle_irq % 1000) == 0) {
  1293. ap->ops->sff_check_status(ap);
  1294. ap->ops->sff_irq_clear(ap);
  1295. ata_port_printk(ap, KERN_WARNING, "irq trap\n");
  1296. return 1;
  1297. }
  1298. #endif
  1299. return 0; /* irq not handled */
  1300. }
  1301. /**
  1302. * ata_sff_interrupt - Default ATA host interrupt handler
  1303. * @irq: irq line (unused)
  1304. * @dev_instance: pointer to our ata_host information structure
  1305. *
  1306. * Default interrupt handler for PCI IDE devices. Calls
  1307. * ata_sff_host_intr() for each port that is not disabled.
  1308. *
  1309. * LOCKING:
  1310. * Obtains host lock during operation.
  1311. *
  1312. * RETURNS:
  1313. * IRQ_NONE or IRQ_HANDLED.
  1314. */
  1315. irqreturn_t ata_sff_interrupt(int irq, void *dev_instance)
  1316. {
  1317. struct ata_host *host = dev_instance;
  1318. unsigned int i;
  1319. unsigned int handled = 0;
  1320. unsigned long flags;
  1321. /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
  1322. spin_lock_irqsave(&host->lock, flags);
  1323. for (i = 0; i < host->n_ports; i++) {
  1324. struct ata_port *ap;
  1325. ap = host->ports[i];
  1326. if (ap &&
  1327. !(ap->flags & ATA_FLAG_DISABLED)) {
  1328. struct ata_queued_cmd *qc;
  1329. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  1330. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
  1331. (qc->flags & ATA_QCFLAG_ACTIVE))
  1332. handled |= ata_sff_host_intr(ap, qc);
  1333. }
  1334. }
  1335. spin_unlock_irqrestore(&host->lock, flags);
  1336. return IRQ_RETVAL(handled);
  1337. }
  1338. /**
  1339. * ata_sff_freeze - Freeze SFF controller port
  1340. * @ap: port to freeze
  1341. *
  1342. * Freeze BMDMA controller port.
  1343. *
  1344. * LOCKING:
  1345. * Inherited from caller.
  1346. */
  1347. void ata_sff_freeze(struct ata_port *ap)
  1348. {
  1349. struct ata_ioports *ioaddr = &ap->ioaddr;
  1350. ap->ctl |= ATA_NIEN;
  1351. ap->last_ctl = ap->ctl;
  1352. if (ioaddr->ctl_addr)
  1353. iowrite8(ap->ctl, ioaddr->ctl_addr);
  1354. /* Under certain circumstances, some controllers raise IRQ on
  1355. * ATA_NIEN manipulation. Also, many controllers fail to mask
  1356. * previously pending IRQ on ATA_NIEN assertion. Clear it.
  1357. */
  1358. ap->ops->sff_check_status(ap);
  1359. ap->ops->sff_irq_clear(ap);
  1360. }
  1361. /**
  1362. * ata_sff_thaw - Thaw SFF controller port
  1363. * @ap: port to thaw
  1364. *
  1365. * Thaw SFF controller port.
  1366. *
  1367. * LOCKING:
  1368. * Inherited from caller.
  1369. */
  1370. void ata_sff_thaw(struct ata_port *ap)
  1371. {
  1372. /* clear & re-enable interrupts */
  1373. ap->ops->sff_check_status(ap);
  1374. ap->ops->sff_irq_clear(ap);
  1375. ap->ops->sff_irq_on(ap);
  1376. }
  1377. /**
  1378. * ata_sff_prereset - prepare SFF link for reset
  1379. * @link: SFF link to be reset
  1380. * @deadline: deadline jiffies for the operation
  1381. *
  1382. * SFF link @link is about to be reset. Initialize it. It first
  1383. * calls ata_std_prereset() and wait for !BSY if the port is
  1384. * being softreset.
  1385. *
  1386. * LOCKING:
  1387. * Kernel thread context (may sleep)
  1388. *
  1389. * RETURNS:
  1390. * 0 on success, -errno otherwise.
  1391. */
  1392. int ata_sff_prereset(struct ata_link *link, unsigned long deadline)
  1393. {
  1394. struct ata_eh_context *ehc = &link->eh_context;
  1395. int rc;
  1396. rc = ata_std_prereset(link, deadline);
  1397. if (rc)
  1398. return rc;
  1399. /* if we're about to do hardreset, nothing more to do */
  1400. if (ehc->i.action & ATA_EH_HARDRESET)
  1401. return 0;
  1402. /* wait for !BSY if we don't know that no device is attached */
  1403. if (!ata_link_offline(link)) {
  1404. rc = ata_sff_wait_ready(link, deadline);
  1405. if (rc && rc != -ENODEV) {
  1406. ata_link_printk(link, KERN_WARNING, "device not ready "
  1407. "(errno=%d), forcing hardreset\n", rc);
  1408. ehc->i.action |= ATA_EH_HARDRESET;
  1409. }
  1410. }
  1411. return 0;
  1412. }
  1413. /**
  1414. * ata_devchk - PATA device presence detection
  1415. * @ap: ATA channel to examine
  1416. * @device: Device to examine (starting at zero)
  1417. *
  1418. * This technique was originally described in
  1419. * Hale Landis's ATADRVR (www.ata-atapi.com), and
  1420. * later found its way into the ATA/ATAPI spec.
  1421. *
  1422. * Write a pattern to the ATA shadow registers,
  1423. * and if a device is present, it will respond by
  1424. * correctly storing and echoing back the
  1425. * ATA shadow register contents.
  1426. *
  1427. * LOCKING:
  1428. * caller.
  1429. */
  1430. static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
  1431. {
  1432. struct ata_ioports *ioaddr = &ap->ioaddr;
  1433. u8 nsect, lbal;
  1434. ap->ops->sff_dev_select(ap, device);
  1435. iowrite8(0x55, ioaddr->nsect_addr);
  1436. iowrite8(0xaa, ioaddr->lbal_addr);
  1437. iowrite8(0xaa, ioaddr->nsect_addr);
  1438. iowrite8(0x55, ioaddr->lbal_addr);
  1439. iowrite8(0x55, ioaddr->nsect_addr);
  1440. iowrite8(0xaa, ioaddr->lbal_addr);
  1441. nsect = ioread8(ioaddr->nsect_addr);
  1442. lbal = ioread8(ioaddr->lbal_addr);
  1443. if ((nsect == 0x55) && (lbal == 0xaa))
  1444. return 1; /* we found a device */
  1445. return 0; /* nothing found */
  1446. }
  1447. /**
  1448. * ata_sff_dev_classify - Parse returned ATA device signature
  1449. * @dev: ATA device to classify (starting at zero)
  1450. * @present: device seems present
  1451. * @r_err: Value of error register on completion
  1452. *
  1453. * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
  1454. * an ATA/ATAPI-defined set of values is placed in the ATA
  1455. * shadow registers, indicating the results of device detection
  1456. * and diagnostics.
  1457. *
  1458. * Select the ATA device, and read the values from the ATA shadow
  1459. * registers. Then parse according to the Error register value,
  1460. * and the spec-defined values examined by ata_dev_classify().
  1461. *
  1462. * LOCKING:
  1463. * caller.
  1464. *
  1465. * RETURNS:
  1466. * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
  1467. */
  1468. unsigned int ata_sff_dev_classify(struct ata_device *dev, int present,
  1469. u8 *r_err)
  1470. {
  1471. struct ata_port *ap = dev->link->ap;
  1472. struct ata_taskfile tf;
  1473. unsigned int class;
  1474. u8 err;
  1475. ap->ops->sff_dev_select(ap, dev->devno);
  1476. memset(&tf, 0, sizeof(tf));
  1477. ap->ops->sff_tf_read(ap, &tf);
  1478. err = tf.feature;
  1479. if (r_err)
  1480. *r_err = err;
  1481. /* see if device passed diags: continue and warn later */
  1482. if (err == 0)
  1483. /* diagnostic fail : do nothing _YET_ */
  1484. dev->horkage |= ATA_HORKAGE_DIAGNOSTIC;
  1485. else if (err == 1)
  1486. /* do nothing */ ;
  1487. else if ((dev->devno == 0) && (err == 0x81))
  1488. /* do nothing */ ;
  1489. else
  1490. return ATA_DEV_NONE;
  1491. /* determine if device is ATA or ATAPI */
  1492. class = ata_dev_classify(&tf);
  1493. if (class == ATA_DEV_UNKNOWN) {
  1494. /* If the device failed diagnostic, it's likely to
  1495. * have reported incorrect device signature too.
  1496. * Assume ATA device if the device seems present but
  1497. * device signature is invalid with diagnostic
  1498. * failure.
  1499. */
  1500. if (present && (dev->horkage & ATA_HORKAGE_DIAGNOSTIC))
  1501. class = ATA_DEV_ATA;
  1502. else
  1503. class = ATA_DEV_NONE;
  1504. } else if ((class == ATA_DEV_ATA) &&
  1505. (ap->ops->sff_check_status(ap) == 0))
  1506. class = ATA_DEV_NONE;
  1507. return class;
  1508. }
  1509. /**
  1510. * ata_sff_wait_after_reset - wait for devices to become ready after reset
  1511. * @link: SFF link which is just reset
  1512. * @devmask: mask of present devices
  1513. * @deadline: deadline jiffies for the operation
  1514. *
  1515. * Wait devices attached to SFF @link to become ready after
  1516. * reset. It contains preceding 150ms wait to avoid accessing TF
  1517. * status register too early.
  1518. *
  1519. * LOCKING:
  1520. * Kernel thread context (may sleep).
  1521. *
  1522. * RETURNS:
  1523. * 0 on success, -ENODEV if some or all of devices in @devmask
  1524. * don't seem to exist. -errno on other errors.
  1525. */
  1526. int ata_sff_wait_after_reset(struct ata_link *link, unsigned int devmask,
  1527. unsigned long deadline)
  1528. {
  1529. struct ata_port *ap = link->ap;
  1530. struct ata_ioports *ioaddr = &ap->ioaddr;
  1531. unsigned int dev0 = devmask & (1 << 0);
  1532. unsigned int dev1 = devmask & (1 << 1);
  1533. int rc, ret = 0;
  1534. msleep(ATA_WAIT_AFTER_RESET_MSECS);
  1535. /* always check readiness of the master device */
  1536. rc = ata_sff_wait_ready(link, deadline);
  1537. /* -ENODEV means the odd clown forgot the D7 pulldown resistor
  1538. * and TF status is 0xff, bail out on it too.
  1539. */
  1540. if (rc)
  1541. return rc;
  1542. /* if device 1 was found in ata_devchk, wait for register
  1543. * access briefly, then wait for BSY to clear.
  1544. */
  1545. if (dev1) {
  1546. int i;
  1547. ap->ops->sff_dev_select(ap, 1);
  1548. /* Wait for register access. Some ATAPI devices fail
  1549. * to set nsect/lbal after reset, so don't waste too
  1550. * much time on it. We're gonna wait for !BSY anyway.
  1551. */
  1552. for (i = 0; i < 2; i++) {
  1553. u8 nsect, lbal;
  1554. nsect = ioread8(ioaddr->nsect_addr);
  1555. lbal = ioread8(ioaddr->lbal_addr);
  1556. if ((nsect == 1) && (lbal == 1))
  1557. break;
  1558. msleep(50); /* give drive a breather */
  1559. }
  1560. rc = ata_sff_wait_ready(link, deadline);
  1561. if (rc) {
  1562. if (rc != -ENODEV)
  1563. return rc;
  1564. ret = rc;
  1565. }
  1566. }
  1567. /* is all this really necessary? */
  1568. ap->ops->sff_dev_select(ap, 0);
  1569. if (dev1)
  1570. ap->ops->sff_dev_select(ap, 1);
  1571. if (dev0)
  1572. ap->ops->sff_dev_select(ap, 0);
  1573. return ret;
  1574. }
  1575. static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
  1576. unsigned long deadline)
  1577. {
  1578. struct ata_ioports *ioaddr = &ap->ioaddr;
  1579. DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
  1580. /* software reset. causes dev0 to be selected */
  1581. iowrite8(ap->ctl, ioaddr->ctl_addr);
  1582. udelay(20); /* FIXME: flush */
  1583. iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
  1584. udelay(20); /* FIXME: flush */
  1585. iowrite8(ap->ctl, ioaddr->ctl_addr);
  1586. /* wait the port to become ready */
  1587. return ata_sff_wait_after_reset(&ap->link, devmask, deadline);
  1588. }
  1589. /**
  1590. * ata_sff_softreset - reset host port via ATA SRST
  1591. * @link: ATA link to reset
  1592. * @classes: resulting classes of attached devices
  1593. * @deadline: deadline jiffies for the operation
  1594. *
  1595. * Reset host port using ATA SRST.
  1596. *
  1597. * LOCKING:
  1598. * Kernel thread context (may sleep)
  1599. *
  1600. * RETURNS:
  1601. * 0 on success, -errno otherwise.
  1602. */
  1603. int ata_sff_softreset(struct ata_link *link, unsigned int *classes,
  1604. unsigned long deadline)
  1605. {
  1606. struct ata_port *ap = link->ap;
  1607. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  1608. unsigned int devmask = 0;
  1609. int rc;
  1610. u8 err;
  1611. DPRINTK("ENTER\n");
  1612. if (ata_link_offline(link)) {
  1613. classes[0] = ATA_DEV_NONE;
  1614. goto out;
  1615. }
  1616. /* determine if device 0/1 are present */
  1617. if (ata_devchk(ap, 0))
  1618. devmask |= (1 << 0);
  1619. if (slave_possible && ata_devchk(ap, 1))
  1620. devmask |= (1 << 1);
  1621. /* select device 0 again */
  1622. ap->ops->sff_dev_select(ap, 0);
  1623. /* issue bus reset */
  1624. DPRINTK("about to softreset, devmask=%x\n", devmask);
  1625. rc = ata_bus_softreset(ap, devmask, deadline);
  1626. /* if link is occupied, -ENODEV too is an error */
  1627. if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
  1628. ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
  1629. return rc;
  1630. }
  1631. /* determine by signature whether we have ATA or ATAPI devices */
  1632. classes[0] = ata_sff_dev_classify(&link->device[0],
  1633. devmask & (1 << 0), &err);
  1634. if (slave_possible && err != 0x81)
  1635. classes[1] = ata_sff_dev_classify(&link->device[1],
  1636. devmask & (1 << 1), &err);
  1637. out:
  1638. DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
  1639. return 0;
  1640. }
  1641. /**
  1642. * sata_sff_hardreset - reset host port via SATA phy reset
  1643. * @link: link to reset
  1644. * @class: resulting class of attached device
  1645. * @deadline: deadline jiffies for the operation
  1646. *
  1647. * SATA phy-reset host port using DET bits of SControl register,
  1648. * wait for !BSY and classify the attached device.
  1649. *
  1650. * LOCKING:
  1651. * Kernel thread context (may sleep)
  1652. *
  1653. * RETURNS:
  1654. * 0 on success, -errno otherwise.
  1655. */
  1656. int sata_sff_hardreset(struct ata_link *link, unsigned int *class,
  1657. unsigned long deadline)
  1658. {
  1659. struct ata_eh_context *ehc = &link->eh_context;
  1660. const unsigned long *timing = sata_ehc_deb_timing(ehc);
  1661. bool online;
  1662. int rc;
  1663. rc = sata_link_hardreset(link, timing, deadline, &online,
  1664. ata_sff_check_ready);
  1665. *class = ATA_DEV_NONE;
  1666. if (online)
  1667. *class = ata_sff_dev_classify(link->device, 1, NULL);
  1668. DPRINTK("EXIT, class=%u\n", *class);
  1669. return rc;
  1670. }
  1671. /**
  1672. * ata_sff_postreset - SFF postreset callback
  1673. * @link: the target SFF ata_link
  1674. * @classes: classes of attached devices
  1675. *
  1676. * This function is invoked after a successful reset. It first
  1677. * calls ata_std_postreset() and performs SFF specific postreset
  1678. * processing.
  1679. *
  1680. * LOCKING:
  1681. * Kernel thread context (may sleep)
  1682. */
  1683. void ata_sff_postreset(struct ata_link *link, unsigned int *classes)
  1684. {
  1685. struct ata_port *ap = link->ap;
  1686. ata_std_postreset(link, classes);
  1687. /* is double-select really necessary? */
  1688. if (classes[0] != ATA_DEV_NONE)
  1689. ap->ops->sff_dev_select(ap, 1);
  1690. if (classes[1] != ATA_DEV_NONE)
  1691. ap->ops->sff_dev_select(ap, 0);
  1692. /* bail out if no device is present */
  1693. if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
  1694. DPRINTK("EXIT, no device\n");
  1695. return;
  1696. }
  1697. /* set up device control */
  1698. if (ap->ioaddr.ctl_addr)
  1699. iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
  1700. }
  1701. /**
  1702. * ata_sff_error_handler - Stock error handler for BMDMA controller
  1703. * @ap: port to handle error for
  1704. *
  1705. * Stock error handler for SFF controller. It can handle both
  1706. * PATA and SATA controllers. Many controllers should be able to
  1707. * use this EH as-is or with some added handling before and
  1708. * after.
  1709. *
  1710. * LOCKING:
  1711. * Kernel thread context (may sleep)
  1712. */
  1713. void ata_sff_error_handler(struct ata_port *ap)
  1714. {
  1715. ata_reset_fn_t softreset = ap->ops->softreset;
  1716. ata_reset_fn_t hardreset = ap->ops->hardreset;
  1717. struct ata_queued_cmd *qc;
  1718. unsigned long flags;
  1719. int thaw = 0;
  1720. qc = __ata_qc_from_tag(ap, ap->link.active_tag);
  1721. if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
  1722. qc = NULL;
  1723. /* reset PIO HSM and stop DMA engine */
  1724. spin_lock_irqsave(ap->lock, flags);
  1725. ap->hsm_task_state = HSM_ST_IDLE;
  1726. if (ap->ioaddr.bmdma_addr &&
  1727. qc && (qc->tf.protocol == ATA_PROT_DMA ||
  1728. qc->tf.protocol == ATAPI_PROT_DMA)) {
  1729. u8 host_stat;
  1730. host_stat = ap->ops->bmdma_status(ap);
  1731. /* BMDMA controllers indicate host bus error by
  1732. * setting DMA_ERR bit and timing out. As it wasn't
  1733. * really a timeout event, adjust error mask and
  1734. * cancel frozen state.
  1735. */
  1736. if (qc->err_mask == AC_ERR_TIMEOUT && (host_stat & ATA_DMA_ERR)) {
  1737. qc->err_mask = AC_ERR_HOST_BUS;
  1738. thaw = 1;
  1739. }
  1740. ap->ops->bmdma_stop(qc);
  1741. }
  1742. ata_sff_altstatus(ap);
  1743. ap->ops->sff_check_status(ap);
  1744. ap->ops->sff_irq_clear(ap);
  1745. spin_unlock_irqrestore(ap->lock, flags);
  1746. if (thaw)
  1747. ata_eh_thaw_port(ap);
  1748. /* PIO and DMA engines have been stopped, perform recovery */
  1749. /* Ignore ata_sff_softreset if ctl isn't accessible and
  1750. * built-in hardresets if SCR access isn't available.
  1751. */
  1752. if (softreset == ata_sff_softreset && !ap->ioaddr.ctl_addr)
  1753. softreset = NULL;
  1754. if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link))
  1755. hardreset = NULL;
  1756. ata_do_eh(ap, ap->ops->prereset, softreset, hardreset,
  1757. ap->ops->postreset);
  1758. }
  1759. /**
  1760. * ata_sff_post_internal_cmd - Stock post_internal_cmd for SFF controller
  1761. * @qc: internal command to clean up
  1762. *
  1763. * LOCKING:
  1764. * Kernel thread context (may sleep)
  1765. */
  1766. void ata_sff_post_internal_cmd(struct ata_queued_cmd *qc)
  1767. {
  1768. if (qc->ap->ioaddr.bmdma_addr)
  1769. ata_bmdma_stop(qc);
  1770. }
  1771. /**
  1772. * ata_sff_port_start - Set port up for dma.
  1773. * @ap: Port to initialize
  1774. *
  1775. * Called just after data structures for each port are
  1776. * initialized. Allocates space for PRD table if the device
  1777. * is DMA capable SFF.
  1778. *
  1779. * May be used as the port_start() entry in ata_port_operations.
  1780. *
  1781. * LOCKING:
  1782. * Inherited from caller.
  1783. */
  1784. int ata_sff_port_start(struct ata_port *ap)
  1785. {
  1786. if (ap->ioaddr.bmdma_addr)
  1787. return ata_port_start(ap);
  1788. return 0;
  1789. }
  1790. /**
  1791. * ata_sff_std_ports - initialize ioaddr with standard port offsets.
  1792. * @ioaddr: IO address structure to be initialized
  1793. *
  1794. * Utility function which initializes data_addr, error_addr,
  1795. * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
  1796. * device_addr, status_addr, and command_addr to standard offsets
  1797. * relative to cmd_addr.
  1798. *
  1799. * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
  1800. */
  1801. void ata_sff_std_ports(struct ata_ioports *ioaddr)
  1802. {
  1803. ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
  1804. ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
  1805. ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
  1806. ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
  1807. ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
  1808. ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
  1809. ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
  1810. ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
  1811. ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
  1812. ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
  1813. }
  1814. unsigned long ata_bmdma_mode_filter(struct ata_device *adev,
  1815. unsigned long xfer_mask)
  1816. {
  1817. /* Filter out DMA modes if the device has been configured by
  1818. the BIOS as PIO only */
  1819. if (adev->link->ap->ioaddr.bmdma_addr == NULL)
  1820. xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
  1821. return xfer_mask;
  1822. }
  1823. /**
  1824. * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
  1825. * @qc: Info associated with this ATA transaction.
  1826. *
  1827. * LOCKING:
  1828. * spin_lock_irqsave(host lock)
  1829. */
  1830. void ata_bmdma_setup(struct ata_queued_cmd *qc)
  1831. {
  1832. struct ata_port *ap = qc->ap;
  1833. unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
  1834. u8 dmactl;
  1835. /* load PRD table addr. */
  1836. mb(); /* make sure PRD table writes are visible to controller */
  1837. iowrite32(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
  1838. /* specify data direction, triple-check start bit is clear */
  1839. dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  1840. dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
  1841. if (!rw)
  1842. dmactl |= ATA_DMA_WR;
  1843. iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  1844. /* issue r/w command */
  1845. ap->ops->sff_exec_command(ap, &qc->tf);
  1846. }
  1847. /**
  1848. * ata_bmdma_start - Start a PCI IDE BMDMA transaction
  1849. * @qc: Info associated with this ATA transaction.
  1850. *
  1851. * LOCKING:
  1852. * spin_lock_irqsave(host lock)
  1853. */
  1854. void ata_bmdma_start(struct ata_queued_cmd *qc)
  1855. {
  1856. struct ata_port *ap = qc->ap;
  1857. u8 dmactl;
  1858. /* start host DMA transaction */
  1859. dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  1860. iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  1861. /* Strictly, one may wish to issue an ioread8() here, to
  1862. * flush the mmio write. However, control also passes
  1863. * to the hardware at this point, and it will interrupt
  1864. * us when we are to resume control. So, in effect,
  1865. * we don't care when the mmio write flushes.
  1866. * Further, a read of the DMA status register _immediately_
  1867. * following the write may not be what certain flaky hardware
  1868. * is expected, so I think it is best to not add a readb()
  1869. * without first all the MMIO ATA cards/mobos.
  1870. * Or maybe I'm just being paranoid.
  1871. *
  1872. * FIXME: The posting of this write means I/O starts are
  1873. * unneccessarily delayed for MMIO
  1874. */
  1875. }
  1876. /**
  1877. * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
  1878. * @qc: Command we are ending DMA for
  1879. *
  1880. * Clears the ATA_DMA_START flag in the dma control register
  1881. *
  1882. * May be used as the bmdma_stop() entry in ata_port_operations.
  1883. *
  1884. * LOCKING:
  1885. * spin_lock_irqsave(host lock)
  1886. */
  1887. void ata_bmdma_stop(struct ata_queued_cmd *qc)
  1888. {
  1889. struct ata_port *ap = qc->ap;
  1890. void __iomem *mmio = ap->ioaddr.bmdma_addr;
  1891. /* clear start/stop bit */
  1892. iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
  1893. mmio + ATA_DMA_CMD);
  1894. /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
  1895. ata_sff_altstatus(ap); /* dummy read */
  1896. }
  1897. /**
  1898. * ata_bmdma_status - Read PCI IDE BMDMA status
  1899. * @ap: Port associated with this ATA transaction.
  1900. *
  1901. * Read and return BMDMA status register.
  1902. *
  1903. * May be used as the bmdma_status() entry in ata_port_operations.
  1904. *
  1905. * LOCKING:
  1906. * spin_lock_irqsave(host lock)
  1907. */
  1908. u8 ata_bmdma_status(struct ata_port *ap)
  1909. {
  1910. return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
  1911. }
  1912. /**
  1913. * ata_bus_reset - reset host port and associated ATA channel
  1914. * @ap: port to reset
  1915. *
  1916. * This is typically the first time we actually start issuing
  1917. * commands to the ATA channel. We wait for BSY to clear, then
  1918. * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
  1919. * result. Determine what devices, if any, are on the channel
  1920. * by looking at the device 0/1 error register. Look at the signature
  1921. * stored in each device's taskfile registers, to determine if
  1922. * the device is ATA or ATAPI.
  1923. *
  1924. * LOCKING:
  1925. * PCI/etc. bus probe sem.
  1926. * Obtains host lock.
  1927. *
  1928. * SIDE EFFECTS:
  1929. * Sets ATA_FLAG_DISABLED if bus reset fails.
  1930. *
  1931. * DEPRECATED:
  1932. * This function is only for drivers which still use old EH and
  1933. * will be removed soon.
  1934. */
  1935. void ata_bus_reset(struct ata_port *ap)
  1936. {
  1937. struct ata_device *device = ap->link.device;
  1938. struct ata_ioports *ioaddr = &ap->ioaddr;
  1939. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  1940. u8 err;
  1941. unsigned int dev0, dev1 = 0, devmask = 0;
  1942. int rc;
  1943. DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
  1944. /* determine if device 0/1 are present */
  1945. if (ap->flags & ATA_FLAG_SATA_RESET)
  1946. dev0 = 1;
  1947. else {
  1948. dev0 = ata_devchk(ap, 0);
  1949. if (slave_possible)
  1950. dev1 = ata_devchk(ap, 1);
  1951. }
  1952. if (dev0)
  1953. devmask |= (1 << 0);
  1954. if (dev1)
  1955. devmask |= (1 << 1);
  1956. /* select device 0 again */
  1957. ap->ops->sff_dev_select(ap, 0);
  1958. /* issue bus reset */
  1959. if (ap->flags & ATA_FLAG_SRST) {
  1960. rc = ata_bus_softreset(ap, devmask, jiffies + 40 * HZ);
  1961. if (rc && rc != -ENODEV)
  1962. goto err_out;
  1963. }
  1964. /*
  1965. * determine by signature whether we have ATA or ATAPI devices
  1966. */
  1967. device[0].class = ata_sff_dev_classify(&device[0], dev0, &err);
  1968. if ((slave_possible) && (err != 0x81))
  1969. device[1].class = ata_sff_dev_classify(&device[1], dev1, &err);
  1970. /* is double-select really necessary? */
  1971. if (device[1].class != ATA_DEV_NONE)
  1972. ap->ops->sff_dev_select(ap, 1);
  1973. if (device[0].class != ATA_DEV_NONE)
  1974. ap->ops->sff_dev_select(ap, 0);
  1975. /* if no devices were detected, disable this port */
  1976. if ((device[0].class == ATA_DEV_NONE) &&
  1977. (device[1].class == ATA_DEV_NONE))
  1978. goto err_out;
  1979. if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
  1980. /* set up device control for ATA_FLAG_SATA_RESET */
  1981. iowrite8(ap->ctl, ioaddr->ctl_addr);
  1982. }
  1983. DPRINTK("EXIT\n");
  1984. return;
  1985. err_out:
  1986. ata_port_printk(ap, KERN_ERR, "disabling port\n");
  1987. ata_port_disable(ap);
  1988. DPRINTK("EXIT\n");
  1989. }
  1990. #ifdef CONFIG_PCI
  1991. /**
  1992. * ata_pci_bmdma_clear_simplex - attempt to kick device out of simplex
  1993. * @pdev: PCI device
  1994. *
  1995. * Some PCI ATA devices report simplex mode but in fact can be told to
  1996. * enter non simplex mode. This implements the necessary logic to
  1997. * perform the task on such devices. Calling it on other devices will
  1998. * have -undefined- behaviour.
  1999. */
  2000. int ata_pci_bmdma_clear_simplex(struct pci_dev *pdev)
  2001. {
  2002. unsigned long bmdma = pci_resource_start(pdev, 4);
  2003. u8 simplex;
  2004. if (bmdma == 0)
  2005. return -ENOENT;
  2006. simplex = inb(bmdma + 0x02);
  2007. outb(simplex & 0x60, bmdma + 0x02);
  2008. simplex = inb(bmdma + 0x02);
  2009. if (simplex & 0x80)
  2010. return -EOPNOTSUPP;
  2011. return 0;
  2012. }
  2013. /**
  2014. * ata_pci_bmdma_init - acquire PCI BMDMA resources and init ATA host
  2015. * @host: target ATA host
  2016. *
  2017. * Acquire PCI BMDMA resources and initialize @host accordingly.
  2018. *
  2019. * LOCKING:
  2020. * Inherited from calling layer (may sleep).
  2021. *
  2022. * RETURNS:
  2023. * 0 on success, -errno otherwise.
  2024. */
  2025. int ata_pci_bmdma_init(struct ata_host *host)
  2026. {
  2027. struct device *gdev = host->dev;
  2028. struct pci_dev *pdev = to_pci_dev(gdev);
  2029. int i, rc;
  2030. /* No BAR4 allocation: No DMA */
  2031. if (pci_resource_start(pdev, 4) == 0)
  2032. return 0;
  2033. /* TODO: If we get no DMA mask we should fall back to PIO */
  2034. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  2035. if (rc)
  2036. return rc;
  2037. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  2038. if (rc)
  2039. return rc;
  2040. /* request and iomap DMA region */
  2041. rc = pcim_iomap_regions(pdev, 1 << 4, dev_driver_string(gdev));
  2042. if (rc) {
  2043. dev_printk(KERN_ERR, gdev, "failed to request/iomap BAR4\n");
  2044. return -ENOMEM;
  2045. }
  2046. host->iomap = pcim_iomap_table(pdev);
  2047. for (i = 0; i < 2; i++) {
  2048. struct ata_port *ap = host->ports[i];
  2049. void __iomem *bmdma = host->iomap[4] + 8 * i;
  2050. if (ata_port_is_dummy(ap))
  2051. continue;
  2052. ap->ioaddr.bmdma_addr = bmdma;
  2053. if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
  2054. (ioread8(bmdma + 2) & 0x80))
  2055. host->flags |= ATA_HOST_SIMPLEX;
  2056. ata_port_desc(ap, "bmdma 0x%llx",
  2057. (unsigned long long)pci_resource_start(pdev, 4) + 8 * i);
  2058. }
  2059. return 0;
  2060. }
  2061. static int ata_resources_present(struct pci_dev *pdev, int port)
  2062. {
  2063. int i;
  2064. /* Check the PCI resources for this channel are enabled */
  2065. port = port * 2;
  2066. for (i = 0; i < 2; i ++) {
  2067. if (pci_resource_start(pdev, port + i) == 0 ||
  2068. pci_resource_len(pdev, port + i) == 0)
  2069. return 0;
  2070. }
  2071. return 1;
  2072. }
  2073. /**
  2074. * ata_pci_sff_init_host - acquire native PCI ATA resources and init host
  2075. * @host: target ATA host
  2076. *
  2077. * Acquire native PCI ATA resources for @host and initialize the
  2078. * first two ports of @host accordingly. Ports marked dummy are
  2079. * skipped and allocation failure makes the port dummy.
  2080. *
  2081. * Note that native PCI resources are valid even for legacy hosts
  2082. * as we fix up pdev resources array early in boot, so this
  2083. * function can be used for both native and legacy SFF hosts.
  2084. *
  2085. * LOCKING:
  2086. * Inherited from calling layer (may sleep).
  2087. *
  2088. * RETURNS:
  2089. * 0 if at least one port is initialized, -ENODEV if no port is
  2090. * available.
  2091. */
  2092. int ata_pci_sff_init_host(struct ata_host *host)
  2093. {
  2094. struct device *gdev = host->dev;
  2095. struct pci_dev *pdev = to_pci_dev(gdev);
  2096. unsigned int mask = 0;
  2097. int i, rc;
  2098. /* request, iomap BARs and init port addresses accordingly */
  2099. for (i = 0; i < 2; i++) {
  2100. struct ata_port *ap = host->ports[i];
  2101. int base = i * 2;
  2102. void __iomem * const *iomap;
  2103. if (ata_port_is_dummy(ap))
  2104. continue;
  2105. /* Discard disabled ports. Some controllers show
  2106. * their unused channels this way. Disabled ports are
  2107. * made dummy.
  2108. */
  2109. if (!ata_resources_present(pdev, i)) {
  2110. ap->ops = &ata_dummy_port_ops;
  2111. continue;
  2112. }
  2113. rc = pcim_iomap_regions(pdev, 0x3 << base,
  2114. dev_driver_string(gdev));
  2115. if (rc) {
  2116. dev_printk(KERN_WARNING, gdev,
  2117. "failed to request/iomap BARs for port %d "
  2118. "(errno=%d)\n", i, rc);
  2119. if (rc == -EBUSY)
  2120. pcim_pin_device(pdev);
  2121. ap->ops = &ata_dummy_port_ops;
  2122. continue;
  2123. }
  2124. host->iomap = iomap = pcim_iomap_table(pdev);
  2125. ap->ioaddr.cmd_addr = iomap[base];
  2126. ap->ioaddr.altstatus_addr =
  2127. ap->ioaddr.ctl_addr = (void __iomem *)
  2128. ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS);
  2129. ata_sff_std_ports(&ap->ioaddr);
  2130. ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
  2131. (unsigned long long)pci_resource_start(pdev, base),
  2132. (unsigned long long)pci_resource_start(pdev, base + 1));
  2133. mask |= 1 << i;
  2134. }
  2135. if (!mask) {
  2136. dev_printk(KERN_ERR, gdev, "no available native port\n");
  2137. return -ENODEV;
  2138. }
  2139. return 0;
  2140. }
  2141. /**
  2142. * ata_pci_sff_prepare_host - helper to prepare native PCI ATA host
  2143. * @pdev: target PCI device
  2144. * @ppi: array of port_info, must be enough for two ports
  2145. * @r_host: out argument for the initialized ATA host
  2146. *
  2147. * Helper to allocate ATA host for @pdev, acquire all native PCI
  2148. * resources and initialize it accordingly in one go.
  2149. *
  2150. * LOCKING:
  2151. * Inherited from calling layer (may sleep).
  2152. *
  2153. * RETURNS:
  2154. * 0 on success, -errno otherwise.
  2155. */
  2156. int ata_pci_sff_prepare_host(struct pci_dev *pdev,
  2157. const struct ata_port_info * const * ppi,
  2158. struct ata_host **r_host)
  2159. {
  2160. struct ata_host *host;
  2161. int rc;
  2162. if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
  2163. return -ENOMEM;
  2164. host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
  2165. if (!host) {
  2166. dev_printk(KERN_ERR, &pdev->dev,
  2167. "failed to allocate ATA host\n");
  2168. rc = -ENOMEM;
  2169. goto err_out;
  2170. }
  2171. rc = ata_pci_sff_init_host(host);
  2172. if (rc)
  2173. goto err_out;
  2174. /* init DMA related stuff */
  2175. rc = ata_pci_bmdma_init(host);
  2176. if (rc)
  2177. goto err_bmdma;
  2178. devres_remove_group(&pdev->dev, NULL);
  2179. *r_host = host;
  2180. return 0;
  2181. err_bmdma:
  2182. /* This is necessary because PCI and iomap resources are
  2183. * merged and releasing the top group won't release the
  2184. * acquired resources if some of those have been acquired
  2185. * before entering this function.
  2186. */
  2187. pcim_iounmap_regions(pdev, 0xf);
  2188. err_out:
  2189. devres_release_group(&pdev->dev, NULL);
  2190. return rc;
  2191. }
  2192. /**
  2193. * ata_pci_sff_activate_host - start SFF host, request IRQ and register it
  2194. * @host: target SFF ATA host
  2195. * @irq_handler: irq_handler used when requesting IRQ(s)
  2196. * @sht: scsi_host_template to use when registering the host
  2197. *
  2198. * This is the counterpart of ata_host_activate() for SFF ATA
  2199. * hosts. This separate helper is necessary because SFF hosts
  2200. * use two separate interrupts in legacy mode.
  2201. *
  2202. * LOCKING:
  2203. * Inherited from calling layer (may sleep).
  2204. *
  2205. * RETURNS:
  2206. * 0 on success, -errno otherwise.
  2207. */
  2208. int ata_pci_sff_activate_host(struct ata_host *host,
  2209. irq_handler_t irq_handler,
  2210. struct scsi_host_template *sht)
  2211. {
  2212. struct device *dev = host->dev;
  2213. struct pci_dev *pdev = to_pci_dev(dev);
  2214. const char *drv_name = dev_driver_string(host->dev);
  2215. int legacy_mode = 0, rc;
  2216. rc = ata_host_start(host);
  2217. if (rc)
  2218. return rc;
  2219. if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
  2220. u8 tmp8, mask;
  2221. /* TODO: What if one channel is in native mode ... */
  2222. pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
  2223. mask = (1 << 2) | (1 << 0);
  2224. if ((tmp8 & mask) != mask)
  2225. legacy_mode = 1;
  2226. #if defined(CONFIG_NO_ATA_LEGACY)
  2227. /* Some platforms with PCI limits cannot address compat
  2228. port space. In that case we punt if their firmware has
  2229. left a device in compatibility mode */
  2230. if (legacy_mode) {
  2231. printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
  2232. return -EOPNOTSUPP;
  2233. }
  2234. #endif
  2235. }
  2236. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  2237. return -ENOMEM;
  2238. if (!legacy_mode && pdev->irq) {
  2239. rc = devm_request_irq(dev, pdev->irq, irq_handler,
  2240. IRQF_SHARED, drv_name, host);
  2241. if (rc)
  2242. goto out;
  2243. ata_port_desc(host->ports[0], "irq %d", pdev->irq);
  2244. ata_port_desc(host->ports[1], "irq %d", pdev->irq);
  2245. } else if (legacy_mode) {
  2246. if (!ata_port_is_dummy(host->ports[0])) {
  2247. rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev),
  2248. irq_handler, IRQF_SHARED,
  2249. drv_name, host);
  2250. if (rc)
  2251. goto out;
  2252. ata_port_desc(host->ports[0], "irq %d",
  2253. ATA_PRIMARY_IRQ(pdev));
  2254. }
  2255. if (!ata_port_is_dummy(host->ports[1])) {
  2256. rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev),
  2257. irq_handler, IRQF_SHARED,
  2258. drv_name, host);
  2259. if (rc)
  2260. goto out;
  2261. ata_port_desc(host->ports[1], "irq %d",
  2262. ATA_SECONDARY_IRQ(pdev));
  2263. }
  2264. }
  2265. rc = ata_host_register(host, sht);
  2266. out:
  2267. if (rc == 0)
  2268. devres_remove_group(dev, NULL);
  2269. else
  2270. devres_release_group(dev, NULL);
  2271. return rc;
  2272. }
  2273. /**
  2274. * ata_pci_sff_init_one - Initialize/register PCI IDE host controller
  2275. * @pdev: Controller to be initialized
  2276. * @ppi: array of port_info, must be enough for two ports
  2277. * @sht: scsi_host_template to use when registering the host
  2278. * @host_priv: host private_data
  2279. *
  2280. * This is a helper function which can be called from a driver's
  2281. * xxx_init_one() probe function if the hardware uses traditional
  2282. * IDE taskfile registers.
  2283. *
  2284. * This function calls pci_enable_device(), reserves its register
  2285. * regions, sets the dma mask, enables bus master mode, and calls
  2286. * ata_device_add()
  2287. *
  2288. * ASSUMPTION:
  2289. * Nobody makes a single channel controller that appears solely as
  2290. * the secondary legacy port on PCI.
  2291. *
  2292. * LOCKING:
  2293. * Inherited from PCI layer (may sleep).
  2294. *
  2295. * RETURNS:
  2296. * Zero on success, negative on errno-based value on error.
  2297. */
  2298. int ata_pci_sff_init_one(struct pci_dev *pdev,
  2299. const struct ata_port_info * const * ppi,
  2300. struct scsi_host_template *sht, void *host_priv)
  2301. {
  2302. struct device *dev = &pdev->dev;
  2303. const struct ata_port_info *pi = NULL;
  2304. struct ata_host *host = NULL;
  2305. int i, rc;
  2306. DPRINTK("ENTER\n");
  2307. /* look up the first valid port_info */
  2308. for (i = 0; i < 2 && ppi[i]; i++) {
  2309. if (ppi[i]->port_ops != &ata_dummy_port_ops) {
  2310. pi = ppi[i];
  2311. break;
  2312. }
  2313. }
  2314. if (!pi) {
  2315. dev_printk(KERN_ERR, &pdev->dev,
  2316. "no valid port_info specified\n");
  2317. return -EINVAL;
  2318. }
  2319. if (!devres_open_group(dev, NULL, GFP_KERNEL))
  2320. return -ENOMEM;
  2321. rc = pcim_enable_device(pdev);
  2322. if (rc)
  2323. goto out;
  2324. /* prepare and activate SFF host */
  2325. rc = ata_pci_sff_prepare_host(pdev, ppi, &host);
  2326. if (rc)
  2327. goto out;
  2328. host->private_data = host_priv;
  2329. pci_set_master(pdev);
  2330. rc = ata_pci_sff_activate_host(host, ata_sff_interrupt, sht);
  2331. out:
  2332. if (rc == 0)
  2333. devres_remove_group(&pdev->dev, NULL);
  2334. else
  2335. devres_release_group(&pdev->dev, NULL);
  2336. return rc;
  2337. }
  2338. #endif /* CONFIG_PCI */
  2339. EXPORT_SYMBOL_GPL(ata_sff_port_ops);
  2340. EXPORT_SYMBOL_GPL(ata_bmdma_port_ops);
  2341. EXPORT_SYMBOL_GPL(ata_sff_qc_prep);
  2342. EXPORT_SYMBOL_GPL(ata_sff_dumb_qc_prep);
  2343. EXPORT_SYMBOL_GPL(ata_sff_dev_select);
  2344. EXPORT_SYMBOL_GPL(ata_sff_check_status);
  2345. EXPORT_SYMBOL_GPL(ata_sff_altstatus);
  2346. EXPORT_SYMBOL_GPL(ata_sff_busy_sleep);
  2347. EXPORT_SYMBOL_GPL(ata_sff_wait_ready);
  2348. EXPORT_SYMBOL_GPL(ata_sff_tf_load);
  2349. EXPORT_SYMBOL_GPL(ata_sff_tf_read);
  2350. EXPORT_SYMBOL_GPL(ata_sff_exec_command);
  2351. EXPORT_SYMBOL_GPL(ata_sff_data_xfer);
  2352. EXPORT_SYMBOL_GPL(ata_sff_data_xfer_noirq);
  2353. EXPORT_SYMBOL_GPL(ata_sff_irq_on);
  2354. EXPORT_SYMBOL_GPL(ata_sff_irq_clear);
  2355. EXPORT_SYMBOL_GPL(ata_sff_hsm_move);
  2356. EXPORT_SYMBOL_GPL(ata_sff_qc_issue);
  2357. EXPORT_SYMBOL_GPL(ata_sff_host_intr);
  2358. EXPORT_SYMBOL_GPL(ata_sff_interrupt);
  2359. EXPORT_SYMBOL_GPL(ata_sff_freeze);
  2360. EXPORT_SYMBOL_GPL(ata_sff_thaw);
  2361. EXPORT_SYMBOL_GPL(ata_sff_prereset);
  2362. EXPORT_SYMBOL_GPL(ata_sff_dev_classify);
  2363. EXPORT_SYMBOL_GPL(ata_sff_wait_after_reset);
  2364. EXPORT_SYMBOL_GPL(ata_sff_softreset);
  2365. EXPORT_SYMBOL_GPL(sata_sff_hardreset);
  2366. EXPORT_SYMBOL_GPL(ata_sff_postreset);
  2367. EXPORT_SYMBOL_GPL(ata_sff_error_handler);
  2368. EXPORT_SYMBOL_GPL(ata_sff_post_internal_cmd);
  2369. EXPORT_SYMBOL_GPL(ata_sff_port_start);
  2370. EXPORT_SYMBOL_GPL(ata_sff_std_ports);
  2371. EXPORT_SYMBOL_GPL(ata_bmdma_mode_filter);
  2372. EXPORT_SYMBOL_GPL(ata_bmdma_setup);
  2373. EXPORT_SYMBOL_GPL(ata_bmdma_start);
  2374. EXPORT_SYMBOL_GPL(ata_bmdma_stop);
  2375. EXPORT_SYMBOL_GPL(ata_bmdma_status);
  2376. EXPORT_SYMBOL_GPL(ata_bus_reset);
  2377. #ifdef CONFIG_PCI
  2378. EXPORT_SYMBOL_GPL(ata_pci_bmdma_clear_simplex);
  2379. EXPORT_SYMBOL_GPL(ata_pci_bmdma_init);
  2380. EXPORT_SYMBOL_GPL(ata_pci_sff_init_host);
  2381. EXPORT_SYMBOL_GPL(ata_pci_sff_prepare_host);
  2382. EXPORT_SYMBOL_GPL(ata_pci_sff_activate_host);
  2383. EXPORT_SYMBOL_GPL(ata_pci_sff_init_one);
  2384. #endif /* CONFIG_PCI */