libata-sff.c 70 KB

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