ide-iops.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. /*
  2. * Copyright (C) 2000-2002 Andre Hedrick <andre@linux-ide.org>
  3. * Copyright (C) 2003 Red Hat <alan@redhat.com>
  4. *
  5. */
  6. #include <linux/module.h>
  7. #include <linux/types.h>
  8. #include <linux/string.h>
  9. #include <linux/kernel.h>
  10. #include <linux/timer.h>
  11. #include <linux/mm.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/major.h>
  14. #include <linux/errno.h>
  15. #include <linux/genhd.h>
  16. #include <linux/blkpg.h>
  17. #include <linux/slab.h>
  18. #include <linux/pci.h>
  19. #include <linux/delay.h>
  20. #include <linux/hdreg.h>
  21. #include <linux/ide.h>
  22. #include <linux/bitops.h>
  23. #include <linux/nmi.h>
  24. #include <asm/byteorder.h>
  25. #include <asm/irq.h>
  26. #include <asm/uaccess.h>
  27. #include <asm/io.h>
  28. /*
  29. * Conventional PIO operations for ATA devices
  30. */
  31. static u8 ide_inb (unsigned long port)
  32. {
  33. return (u8) inb(port);
  34. }
  35. static void ide_outb (u8 val, unsigned long port)
  36. {
  37. outb(val, port);
  38. }
  39. static void ide_outbsync(ide_hwif_t *hwif, u8 addr, unsigned long port)
  40. {
  41. outb(addr, port);
  42. }
  43. void default_hwif_iops (ide_hwif_t *hwif)
  44. {
  45. hwif->OUTB = ide_outb;
  46. hwif->OUTBSYNC = ide_outbsync;
  47. hwif->INB = ide_inb;
  48. }
  49. /*
  50. * MMIO operations, typically used for SATA controllers
  51. */
  52. static u8 ide_mm_inb (unsigned long port)
  53. {
  54. return (u8) readb((void __iomem *) port);
  55. }
  56. static void ide_mm_outb (u8 value, unsigned long port)
  57. {
  58. writeb(value, (void __iomem *) port);
  59. }
  60. static void ide_mm_outbsync(ide_hwif_t *hwif, u8 value, unsigned long port)
  61. {
  62. writeb(value, (void __iomem *) port);
  63. }
  64. void default_hwif_mmiops (ide_hwif_t *hwif)
  65. {
  66. hwif->OUTB = ide_mm_outb;
  67. /* Most systems will need to override OUTBSYNC, alas however
  68. this one is controller specific! */
  69. hwif->OUTBSYNC = ide_mm_outbsync;
  70. hwif->INB = ide_mm_inb;
  71. }
  72. EXPORT_SYMBOL(default_hwif_mmiops);
  73. void SELECT_DRIVE (ide_drive_t *drive)
  74. {
  75. ide_hwif_t *hwif = drive->hwif;
  76. const struct ide_port_ops *port_ops = hwif->port_ops;
  77. if (port_ops && port_ops->selectproc)
  78. port_ops->selectproc(drive);
  79. hwif->OUTB(drive->select.all, hwif->io_ports.device_addr);
  80. }
  81. void SELECT_MASK(ide_drive_t *drive, int mask)
  82. {
  83. const struct ide_port_ops *port_ops = drive->hwif->port_ops;
  84. if (port_ops && port_ops->maskproc)
  85. port_ops->maskproc(drive, mask);
  86. }
  87. static void ide_exec_command(ide_hwif_t *hwif, u8 cmd)
  88. {
  89. if (hwif->host_flags & IDE_HFLAG_MMIO)
  90. writeb(cmd, (void __iomem *)hwif->io_ports.command_addr);
  91. else
  92. outb(cmd, hwif->io_ports.command_addr);
  93. }
  94. static u8 ide_read_sff_dma_status(ide_hwif_t *hwif)
  95. {
  96. if (hwif->host_flags & IDE_HFLAG_MMIO)
  97. return readb((void __iomem *)(hwif->dma_base + ATA_DMA_STATUS));
  98. else
  99. return inb(hwif->dma_base + ATA_DMA_STATUS);
  100. }
  101. static void ide_tf_load(ide_drive_t *drive, ide_task_t *task)
  102. {
  103. ide_hwif_t *hwif = drive->hwif;
  104. struct ide_io_ports *io_ports = &hwif->io_ports;
  105. struct ide_taskfile *tf = &task->tf;
  106. void (*tf_outb)(u8 addr, unsigned long port);
  107. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  108. u8 HIHI = (task->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
  109. if (mmio)
  110. tf_outb = ide_mm_outb;
  111. else
  112. tf_outb = ide_outb;
  113. if (task->tf_flags & IDE_TFLAG_FLAGGED)
  114. HIHI = 0xFF;
  115. if (task->tf_flags & IDE_TFLAG_OUT_DATA) {
  116. u16 data = (tf->hob_data << 8) | tf->data;
  117. if (mmio)
  118. writew(data, (void __iomem *)io_ports->data_addr);
  119. else
  120. outw(data, io_ports->data_addr);
  121. }
  122. if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
  123. tf_outb(tf->hob_feature, io_ports->feature_addr);
  124. if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
  125. tf_outb(tf->hob_nsect, io_ports->nsect_addr);
  126. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
  127. tf_outb(tf->hob_lbal, io_ports->lbal_addr);
  128. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
  129. tf_outb(tf->hob_lbam, io_ports->lbam_addr);
  130. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
  131. tf_outb(tf->hob_lbah, io_ports->lbah_addr);
  132. if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
  133. tf_outb(tf->feature, io_ports->feature_addr);
  134. if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
  135. tf_outb(tf->nsect, io_ports->nsect_addr);
  136. if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
  137. tf_outb(tf->lbal, io_ports->lbal_addr);
  138. if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
  139. tf_outb(tf->lbam, io_ports->lbam_addr);
  140. if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
  141. tf_outb(tf->lbah, io_ports->lbah_addr);
  142. if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
  143. tf_outb((tf->device & HIHI) | drive->select.all,
  144. io_ports->device_addr);
  145. }
  146. static void ide_tf_read(ide_drive_t *drive, ide_task_t *task)
  147. {
  148. ide_hwif_t *hwif = drive->hwif;
  149. struct ide_io_ports *io_ports = &hwif->io_ports;
  150. struct ide_taskfile *tf = &task->tf;
  151. void (*tf_outb)(u8 addr, unsigned long port);
  152. u8 (*tf_inb)(unsigned long port);
  153. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  154. if (mmio) {
  155. tf_outb = ide_mm_outb;
  156. tf_inb = ide_mm_inb;
  157. } else {
  158. tf_outb = ide_outb;
  159. tf_inb = ide_inb;
  160. }
  161. if (task->tf_flags & IDE_TFLAG_IN_DATA) {
  162. u16 data;
  163. if (mmio)
  164. data = readw((void __iomem *)io_ports->data_addr);
  165. else
  166. data = inw(io_ports->data_addr);
  167. tf->data = data & 0xff;
  168. tf->hob_data = (data >> 8) & 0xff;
  169. }
  170. /* be sure we're looking at the low order bits */
  171. tf_outb(ATA_DEVCTL_OBS & ~0x80, io_ports->ctl_addr);
  172. if (task->tf_flags & IDE_TFLAG_IN_NSECT)
  173. tf->nsect = tf_inb(io_ports->nsect_addr);
  174. if (task->tf_flags & IDE_TFLAG_IN_LBAL)
  175. tf->lbal = tf_inb(io_ports->lbal_addr);
  176. if (task->tf_flags & IDE_TFLAG_IN_LBAM)
  177. tf->lbam = tf_inb(io_ports->lbam_addr);
  178. if (task->tf_flags & IDE_TFLAG_IN_LBAH)
  179. tf->lbah = tf_inb(io_ports->lbah_addr);
  180. if (task->tf_flags & IDE_TFLAG_IN_DEVICE)
  181. tf->device = tf_inb(io_ports->device_addr);
  182. if (task->tf_flags & IDE_TFLAG_LBA48) {
  183. tf_outb(ATA_DEVCTL_OBS | 0x80, io_ports->ctl_addr);
  184. if (task->tf_flags & IDE_TFLAG_IN_HOB_FEATURE)
  185. tf->hob_feature = tf_inb(io_ports->feature_addr);
  186. if (task->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
  187. tf->hob_nsect = tf_inb(io_ports->nsect_addr);
  188. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
  189. tf->hob_lbal = tf_inb(io_ports->lbal_addr);
  190. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
  191. tf->hob_lbam = tf_inb(io_ports->lbam_addr);
  192. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
  193. tf->hob_lbah = tf_inb(io_ports->lbah_addr);
  194. }
  195. }
  196. /*
  197. * Some localbus EIDE interfaces require a special access sequence
  198. * when using 32-bit I/O instructions to transfer data. We call this
  199. * the "vlb_sync" sequence, which consists of three successive reads
  200. * of the sector count register location, with interrupts disabled
  201. * to ensure that the reads all happen together.
  202. */
  203. static void ata_vlb_sync(unsigned long port)
  204. {
  205. (void)inb(port);
  206. (void)inb(port);
  207. (void)inb(port);
  208. }
  209. /*
  210. * This is used for most PIO data transfers *from* the IDE interface
  211. *
  212. * These routines will round up any request for an odd number of bytes,
  213. * so if an odd len is specified, be sure that there's at least one
  214. * extra byte allocated for the buffer.
  215. */
  216. static void ata_input_data(ide_drive_t *drive, struct request *rq,
  217. void *buf, unsigned int len)
  218. {
  219. ide_hwif_t *hwif = drive->hwif;
  220. struct ide_io_ports *io_ports = &hwif->io_ports;
  221. unsigned long data_addr = io_ports->data_addr;
  222. u8 io_32bit = drive->io_32bit;
  223. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  224. len++;
  225. if (io_32bit) {
  226. unsigned long uninitialized_var(flags);
  227. if ((io_32bit & 2) && !mmio) {
  228. local_irq_save(flags);
  229. ata_vlb_sync(io_ports->nsect_addr);
  230. }
  231. if (mmio)
  232. __ide_mm_insl((void __iomem *)data_addr, buf, len / 4);
  233. else
  234. insl(data_addr, buf, len / 4);
  235. if ((io_32bit & 2) && !mmio)
  236. local_irq_restore(flags);
  237. if ((len & 3) >= 2) {
  238. if (mmio)
  239. __ide_mm_insw((void __iomem *)data_addr,
  240. (u8 *)buf + (len & ~3), 1);
  241. else
  242. insw(data_addr, (u8 *)buf + (len & ~3), 1);
  243. }
  244. } else {
  245. if (mmio)
  246. __ide_mm_insw((void __iomem *)data_addr, buf, len / 2);
  247. else
  248. insw(data_addr, buf, len / 2);
  249. }
  250. }
  251. /*
  252. * This is used for most PIO data transfers *to* the IDE interface
  253. */
  254. static void ata_output_data(ide_drive_t *drive, struct request *rq,
  255. void *buf, unsigned int len)
  256. {
  257. ide_hwif_t *hwif = drive->hwif;
  258. struct ide_io_ports *io_ports = &hwif->io_ports;
  259. unsigned long data_addr = io_ports->data_addr;
  260. u8 io_32bit = drive->io_32bit;
  261. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  262. if (io_32bit) {
  263. unsigned long uninitialized_var(flags);
  264. if ((io_32bit & 2) && !mmio) {
  265. local_irq_save(flags);
  266. ata_vlb_sync(io_ports->nsect_addr);
  267. }
  268. if (mmio)
  269. __ide_mm_outsl((void __iomem *)data_addr, buf, len / 4);
  270. else
  271. outsl(data_addr, buf, len / 4);
  272. if ((io_32bit & 2) && !mmio)
  273. local_irq_restore(flags);
  274. if ((len & 3) >= 2) {
  275. if (mmio)
  276. __ide_mm_outsw((void __iomem *)data_addr,
  277. (u8 *)buf + (len & ~3), 1);
  278. else
  279. outsw(data_addr, (u8 *)buf + (len & ~3), 1);
  280. }
  281. } else {
  282. if (mmio)
  283. __ide_mm_outsw((void __iomem *)data_addr, buf, len / 2);
  284. else
  285. outsw(data_addr, buf, len / 2);
  286. }
  287. }
  288. void default_hwif_transport(ide_hwif_t *hwif)
  289. {
  290. hwif->exec_command = ide_exec_command;
  291. hwif->read_sff_dma_status = ide_read_sff_dma_status;
  292. hwif->tf_load = ide_tf_load;
  293. hwif->tf_read = ide_tf_read;
  294. hwif->input_data = ata_input_data;
  295. hwif->output_data = ata_output_data;
  296. }
  297. void ide_fix_driveid (struct hd_driveid *id)
  298. {
  299. #ifndef __LITTLE_ENDIAN
  300. # ifdef __BIG_ENDIAN
  301. int i;
  302. u16 *stringcast;
  303. id->config = __le16_to_cpu(id->config);
  304. id->cyls = __le16_to_cpu(id->cyls);
  305. id->reserved2 = __le16_to_cpu(id->reserved2);
  306. id->heads = __le16_to_cpu(id->heads);
  307. id->track_bytes = __le16_to_cpu(id->track_bytes);
  308. id->sector_bytes = __le16_to_cpu(id->sector_bytes);
  309. id->sectors = __le16_to_cpu(id->sectors);
  310. id->vendor0 = __le16_to_cpu(id->vendor0);
  311. id->vendor1 = __le16_to_cpu(id->vendor1);
  312. id->vendor2 = __le16_to_cpu(id->vendor2);
  313. stringcast = (u16 *)&id->serial_no[0];
  314. for (i = 0; i < (20/2); i++)
  315. stringcast[i] = __le16_to_cpu(stringcast[i]);
  316. id->buf_type = __le16_to_cpu(id->buf_type);
  317. id->buf_size = __le16_to_cpu(id->buf_size);
  318. id->ecc_bytes = __le16_to_cpu(id->ecc_bytes);
  319. stringcast = (u16 *)&id->fw_rev[0];
  320. for (i = 0; i < (8/2); i++)
  321. stringcast[i] = __le16_to_cpu(stringcast[i]);
  322. stringcast = (u16 *)&id->model[0];
  323. for (i = 0; i < (40/2); i++)
  324. stringcast[i] = __le16_to_cpu(stringcast[i]);
  325. id->dword_io = __le16_to_cpu(id->dword_io);
  326. id->reserved50 = __le16_to_cpu(id->reserved50);
  327. id->field_valid = __le16_to_cpu(id->field_valid);
  328. id->cur_cyls = __le16_to_cpu(id->cur_cyls);
  329. id->cur_heads = __le16_to_cpu(id->cur_heads);
  330. id->cur_sectors = __le16_to_cpu(id->cur_sectors);
  331. id->cur_capacity0 = __le16_to_cpu(id->cur_capacity0);
  332. id->cur_capacity1 = __le16_to_cpu(id->cur_capacity1);
  333. id->lba_capacity = __le32_to_cpu(id->lba_capacity);
  334. id->dma_1word = __le16_to_cpu(id->dma_1word);
  335. id->dma_mword = __le16_to_cpu(id->dma_mword);
  336. id->eide_pio_modes = __le16_to_cpu(id->eide_pio_modes);
  337. id->eide_dma_min = __le16_to_cpu(id->eide_dma_min);
  338. id->eide_dma_time = __le16_to_cpu(id->eide_dma_time);
  339. id->eide_pio = __le16_to_cpu(id->eide_pio);
  340. id->eide_pio_iordy = __le16_to_cpu(id->eide_pio_iordy);
  341. for (i = 0; i < 2; ++i)
  342. id->words69_70[i] = __le16_to_cpu(id->words69_70[i]);
  343. for (i = 0; i < 4; ++i)
  344. id->words71_74[i] = __le16_to_cpu(id->words71_74[i]);
  345. id->queue_depth = __le16_to_cpu(id->queue_depth);
  346. for (i = 0; i < 4; ++i)
  347. id->words76_79[i] = __le16_to_cpu(id->words76_79[i]);
  348. id->major_rev_num = __le16_to_cpu(id->major_rev_num);
  349. id->minor_rev_num = __le16_to_cpu(id->minor_rev_num);
  350. id->command_set_1 = __le16_to_cpu(id->command_set_1);
  351. id->command_set_2 = __le16_to_cpu(id->command_set_2);
  352. id->cfsse = __le16_to_cpu(id->cfsse);
  353. id->cfs_enable_1 = __le16_to_cpu(id->cfs_enable_1);
  354. id->cfs_enable_2 = __le16_to_cpu(id->cfs_enable_2);
  355. id->csf_default = __le16_to_cpu(id->csf_default);
  356. id->dma_ultra = __le16_to_cpu(id->dma_ultra);
  357. id->trseuc = __le16_to_cpu(id->trseuc);
  358. id->trsEuc = __le16_to_cpu(id->trsEuc);
  359. id->CurAPMvalues = __le16_to_cpu(id->CurAPMvalues);
  360. id->mprc = __le16_to_cpu(id->mprc);
  361. id->hw_config = __le16_to_cpu(id->hw_config);
  362. id->acoustic = __le16_to_cpu(id->acoustic);
  363. id->msrqs = __le16_to_cpu(id->msrqs);
  364. id->sxfert = __le16_to_cpu(id->sxfert);
  365. id->sal = __le16_to_cpu(id->sal);
  366. id->spg = __le32_to_cpu(id->spg);
  367. id->lba_capacity_2 = __le64_to_cpu(id->lba_capacity_2);
  368. for (i = 0; i < 22; i++)
  369. id->words104_125[i] = __le16_to_cpu(id->words104_125[i]);
  370. id->last_lun = __le16_to_cpu(id->last_lun);
  371. id->word127 = __le16_to_cpu(id->word127);
  372. id->dlf = __le16_to_cpu(id->dlf);
  373. id->csfo = __le16_to_cpu(id->csfo);
  374. for (i = 0; i < 26; i++)
  375. id->words130_155[i] = __le16_to_cpu(id->words130_155[i]);
  376. id->word156 = __le16_to_cpu(id->word156);
  377. for (i = 0; i < 3; i++)
  378. id->words157_159[i] = __le16_to_cpu(id->words157_159[i]);
  379. id->cfa_power = __le16_to_cpu(id->cfa_power);
  380. for (i = 0; i < 14; i++)
  381. id->words161_175[i] = __le16_to_cpu(id->words161_175[i]);
  382. for (i = 0; i < 31; i++)
  383. id->words176_205[i] = __le16_to_cpu(id->words176_205[i]);
  384. for (i = 0; i < 48; i++)
  385. id->words206_254[i] = __le16_to_cpu(id->words206_254[i]);
  386. id->integrity_word = __le16_to_cpu(id->integrity_word);
  387. # else
  388. # error "Please fix <asm/byteorder.h>"
  389. # endif
  390. #endif
  391. }
  392. /*
  393. * ide_fixstring() cleans up and (optionally) byte-swaps a text string,
  394. * removing leading/trailing blanks and compressing internal blanks.
  395. * It is primarily used to tidy up the model name/number fields as
  396. * returned by the WIN_[P]IDENTIFY commands.
  397. */
  398. void ide_fixstring (u8 *s, const int bytecount, const int byteswap)
  399. {
  400. u8 *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
  401. if (byteswap) {
  402. /* convert from big-endian to host byte order */
  403. for (p = end ; p != s;) {
  404. unsigned short *pp = (unsigned short *) (p -= 2);
  405. *pp = ntohs(*pp);
  406. }
  407. }
  408. /* strip leading blanks */
  409. while (s != end && *s == ' ')
  410. ++s;
  411. /* compress internal blanks and strip trailing blanks */
  412. while (s != end && *s) {
  413. if (*s++ != ' ' || (s != end && *s && *s != ' '))
  414. *p++ = *(s-1);
  415. }
  416. /* wipe out trailing garbage */
  417. while (p != end)
  418. *p++ = '\0';
  419. }
  420. EXPORT_SYMBOL(ide_fixstring);
  421. /*
  422. * Needed for PCI irq sharing
  423. */
  424. int drive_is_ready (ide_drive_t *drive)
  425. {
  426. ide_hwif_t *hwif = HWIF(drive);
  427. u8 stat = 0;
  428. if (drive->waiting_for_dma)
  429. return hwif->dma_ops->dma_test_irq(drive);
  430. #if 0
  431. /* need to guarantee 400ns since last command was issued */
  432. udelay(1);
  433. #endif
  434. /*
  435. * We do a passive status test under shared PCI interrupts on
  436. * cards that truly share the ATA side interrupt, but may also share
  437. * an interrupt with another pci card/device. We make no assumptions
  438. * about possible isa-pnp and pci-pnp issues yet.
  439. */
  440. if (hwif->io_ports.ctl_addr)
  441. stat = ide_read_altstatus(drive);
  442. else
  443. /* Note: this may clear a pending IRQ!! */
  444. stat = ide_read_status(drive);
  445. if (stat & BUSY_STAT)
  446. /* drive busy: definitely not interrupting */
  447. return 0;
  448. /* drive ready: *might* be interrupting */
  449. return 1;
  450. }
  451. EXPORT_SYMBOL(drive_is_ready);
  452. /*
  453. * This routine busy-waits for the drive status to be not "busy".
  454. * It then checks the status for all of the "good" bits and none
  455. * of the "bad" bits, and if all is okay it returns 0. All other
  456. * cases return error -- caller may then invoke ide_error().
  457. *
  458. * This routine should get fixed to not hog the cpu during extra long waits..
  459. * That could be done by busy-waiting for the first jiffy or two, and then
  460. * setting a timer to wake up at half second intervals thereafter,
  461. * until timeout is achieved, before timing out.
  462. */
  463. static int __ide_wait_stat(ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout, u8 *rstat)
  464. {
  465. unsigned long flags;
  466. int i;
  467. u8 stat;
  468. udelay(1); /* spec allows drive 400ns to assert "BUSY" */
  469. stat = ide_read_status(drive);
  470. if (stat & BUSY_STAT) {
  471. local_irq_set(flags);
  472. timeout += jiffies;
  473. while ((stat = ide_read_status(drive)) & BUSY_STAT) {
  474. if (time_after(jiffies, timeout)) {
  475. /*
  476. * One last read after the timeout in case
  477. * heavy interrupt load made us not make any
  478. * progress during the timeout..
  479. */
  480. stat = ide_read_status(drive);
  481. if (!(stat & BUSY_STAT))
  482. break;
  483. local_irq_restore(flags);
  484. *rstat = stat;
  485. return -EBUSY;
  486. }
  487. }
  488. local_irq_restore(flags);
  489. }
  490. /*
  491. * Allow status to settle, then read it again.
  492. * A few rare drives vastly violate the 400ns spec here,
  493. * so we'll wait up to 10usec for a "good" status
  494. * rather than expensively fail things immediately.
  495. * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
  496. */
  497. for (i = 0; i < 10; i++) {
  498. udelay(1);
  499. stat = ide_read_status(drive);
  500. if (OK_STAT(stat, good, bad)) {
  501. *rstat = stat;
  502. return 0;
  503. }
  504. }
  505. *rstat = stat;
  506. return -EFAULT;
  507. }
  508. /*
  509. * In case of error returns error value after doing "*startstop = ide_error()".
  510. * The caller should return the updated value of "startstop" in this case,
  511. * "startstop" is unchanged when the function returns 0.
  512. */
  513. int ide_wait_stat(ide_startstop_t *startstop, ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout)
  514. {
  515. int err;
  516. u8 stat;
  517. /* bail early if we've exceeded max_failures */
  518. if (drive->max_failures && (drive->failures > drive->max_failures)) {
  519. *startstop = ide_stopped;
  520. return 1;
  521. }
  522. err = __ide_wait_stat(drive, good, bad, timeout, &stat);
  523. if (err) {
  524. char *s = (err == -EBUSY) ? "status timeout" : "status error";
  525. *startstop = ide_error(drive, s, stat);
  526. }
  527. return err;
  528. }
  529. EXPORT_SYMBOL(ide_wait_stat);
  530. /**
  531. * ide_in_drive_list - look for drive in black/white list
  532. * @id: drive identifier
  533. * @drive_table: list to inspect
  534. *
  535. * Look for a drive in the blacklist and the whitelist tables
  536. * Returns 1 if the drive is found in the table.
  537. */
  538. int ide_in_drive_list(struct hd_driveid *id, const struct drive_list_entry *drive_table)
  539. {
  540. for ( ; drive_table->id_model; drive_table++)
  541. if ((!strcmp(drive_table->id_model, id->model)) &&
  542. (!drive_table->id_firmware ||
  543. strstr(id->fw_rev, drive_table->id_firmware)))
  544. return 1;
  545. return 0;
  546. }
  547. EXPORT_SYMBOL_GPL(ide_in_drive_list);
  548. /*
  549. * Early UDMA66 devices don't set bit14 to 1, only bit13 is valid.
  550. * We list them here and depend on the device side cable detection for them.
  551. *
  552. * Some optical devices with the buggy firmwares have the same problem.
  553. */
  554. static const struct drive_list_entry ivb_list[] = {
  555. { "QUANTUM FIREBALLlct10 05" , "A03.0900" },
  556. { "TSSTcorp CDDVDW SH-S202J" , "SB00" },
  557. { "TSSTcorp CDDVDW SH-S202J" , "SB01" },
  558. { "TSSTcorp CDDVDW SH-S202N" , "SB00" },
  559. { "TSSTcorp CDDVDW SH-S202N" , "SB01" },
  560. { "TSSTcorp CDDVDW SH-S202H" , "SB00" },
  561. { "TSSTcorp CDDVDW SH-S202H" , "SB01" },
  562. { NULL , NULL }
  563. };
  564. /*
  565. * All hosts that use the 80c ribbon must use!
  566. * The name is derived from upper byte of word 93 and the 80c ribbon.
  567. */
  568. u8 eighty_ninty_three (ide_drive_t *drive)
  569. {
  570. ide_hwif_t *hwif = drive->hwif;
  571. struct hd_driveid *id = drive->id;
  572. int ivb = ide_in_drive_list(id, ivb_list);
  573. if (hwif->cbl == ATA_CBL_PATA40_SHORT)
  574. return 1;
  575. if (ivb)
  576. printk(KERN_DEBUG "%s: skipping word 93 validity check\n",
  577. drive->name);
  578. if (ide_dev_is_sata(id) && !ivb)
  579. return 1;
  580. if (hwif->cbl != ATA_CBL_PATA80 && !ivb)
  581. goto no_80w;
  582. /*
  583. * FIXME:
  584. * - change master/slave IDENTIFY order
  585. * - force bit13 (80c cable present) check also for !ivb devices
  586. * (unless the slave device is pre-ATA3)
  587. */
  588. if ((id->hw_config & 0x4000) || (ivb && (id->hw_config & 0x2000)))
  589. return 1;
  590. no_80w:
  591. if (drive->udma33_warned == 1)
  592. return 0;
  593. printk(KERN_WARNING "%s: %s side 80-wire cable detection failed, "
  594. "limiting max speed to UDMA33\n",
  595. drive->name,
  596. hwif->cbl == ATA_CBL_PATA80 ? "drive" : "host");
  597. drive->udma33_warned = 1;
  598. return 0;
  599. }
  600. int ide_driveid_update(ide_drive_t *drive)
  601. {
  602. ide_hwif_t *hwif = drive->hwif;
  603. struct hd_driveid *id;
  604. unsigned long timeout, flags;
  605. u8 stat;
  606. /*
  607. * Re-read drive->id for possible DMA mode
  608. * change (copied from ide-probe.c)
  609. */
  610. SELECT_MASK(drive, 1);
  611. ide_set_irq(drive, 0);
  612. msleep(50);
  613. hwif->exec_command(hwif, WIN_IDENTIFY);
  614. timeout = jiffies + WAIT_WORSTCASE;
  615. do {
  616. if (time_after(jiffies, timeout)) {
  617. SELECT_MASK(drive, 0);
  618. return 0; /* drive timed-out */
  619. }
  620. msleep(50); /* give drive a breather */
  621. stat = ide_read_altstatus(drive);
  622. } while (stat & BUSY_STAT);
  623. msleep(50); /* wait for IRQ and DRQ_STAT */
  624. stat = ide_read_status(drive);
  625. if (!OK_STAT(stat, DRQ_STAT, BAD_R_STAT)) {
  626. SELECT_MASK(drive, 0);
  627. printk("%s: CHECK for good STATUS\n", drive->name);
  628. return 0;
  629. }
  630. local_irq_save(flags);
  631. SELECT_MASK(drive, 0);
  632. id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
  633. if (!id) {
  634. local_irq_restore(flags);
  635. return 0;
  636. }
  637. hwif->input_data(drive, NULL, id, SECTOR_SIZE);
  638. (void)ide_read_status(drive); /* clear drive IRQ */
  639. local_irq_enable();
  640. local_irq_restore(flags);
  641. ide_fix_driveid(id);
  642. if (id) {
  643. drive->id->dma_ultra = id->dma_ultra;
  644. drive->id->dma_mword = id->dma_mword;
  645. drive->id->dma_1word = id->dma_1word;
  646. /* anything more ? */
  647. kfree(id);
  648. if (drive->using_dma && ide_id_dma_bug(drive))
  649. ide_dma_off(drive);
  650. }
  651. return 1;
  652. }
  653. int ide_config_drive_speed(ide_drive_t *drive, u8 speed)
  654. {
  655. ide_hwif_t *hwif = drive->hwif;
  656. struct ide_io_ports *io_ports = &hwif->io_ports;
  657. int error = 0;
  658. u8 stat;
  659. #ifdef CONFIG_BLK_DEV_IDEDMA
  660. if (hwif->dma_ops) /* check if host supports DMA */
  661. hwif->dma_ops->dma_host_set(drive, 0);
  662. #endif
  663. /* Skip setting PIO flow-control modes on pre-EIDE drives */
  664. if ((speed & 0xf8) == XFER_PIO_0 && !(drive->id->capability & 0x08))
  665. goto skip;
  666. /*
  667. * Don't use ide_wait_cmd here - it will
  668. * attempt to set_geometry and recalibrate,
  669. * but for some reason these don't work at
  670. * this point (lost interrupt).
  671. */
  672. /*
  673. * Select the drive, and issue the SETFEATURES command
  674. */
  675. disable_irq_nosync(hwif->irq);
  676. /*
  677. * FIXME: we race against the running IRQ here if
  678. * this is called from non IRQ context. If we use
  679. * disable_irq() we hang on the error path. Work
  680. * is needed.
  681. */
  682. udelay(1);
  683. SELECT_DRIVE(drive);
  684. SELECT_MASK(drive, 0);
  685. udelay(1);
  686. ide_set_irq(drive, 0);
  687. hwif->OUTB(speed, io_ports->nsect_addr);
  688. hwif->OUTB(SETFEATURES_XFER, io_ports->feature_addr);
  689. hwif->exec_command(hwif, WIN_SETFEATURES);
  690. if (drive->quirk_list == 2)
  691. ide_set_irq(drive, 1);
  692. error = __ide_wait_stat(drive, drive->ready_stat,
  693. BUSY_STAT|DRQ_STAT|ERR_STAT,
  694. WAIT_CMD, &stat);
  695. SELECT_MASK(drive, 0);
  696. enable_irq(hwif->irq);
  697. if (error) {
  698. (void) ide_dump_status(drive, "set_drive_speed_status", stat);
  699. return error;
  700. }
  701. drive->id->dma_ultra &= ~0xFF00;
  702. drive->id->dma_mword &= ~0x0F00;
  703. drive->id->dma_1word &= ~0x0F00;
  704. skip:
  705. #ifdef CONFIG_BLK_DEV_IDEDMA
  706. if ((speed >= XFER_SW_DMA_0 || (hwif->host_flags & IDE_HFLAG_VDMA)) &&
  707. drive->using_dma)
  708. hwif->dma_ops->dma_host_set(drive, 1);
  709. else if (hwif->dma_ops) /* check if host supports DMA */
  710. ide_dma_off_quietly(drive);
  711. #endif
  712. switch(speed) {
  713. case XFER_UDMA_7: drive->id->dma_ultra |= 0x8080; break;
  714. case XFER_UDMA_6: drive->id->dma_ultra |= 0x4040; break;
  715. case XFER_UDMA_5: drive->id->dma_ultra |= 0x2020; break;
  716. case XFER_UDMA_4: drive->id->dma_ultra |= 0x1010; break;
  717. case XFER_UDMA_3: drive->id->dma_ultra |= 0x0808; break;
  718. case XFER_UDMA_2: drive->id->dma_ultra |= 0x0404; break;
  719. case XFER_UDMA_1: drive->id->dma_ultra |= 0x0202; break;
  720. case XFER_UDMA_0: drive->id->dma_ultra |= 0x0101; break;
  721. case XFER_MW_DMA_2: drive->id->dma_mword |= 0x0404; break;
  722. case XFER_MW_DMA_1: drive->id->dma_mword |= 0x0202; break;
  723. case XFER_MW_DMA_0: drive->id->dma_mword |= 0x0101; break;
  724. case XFER_SW_DMA_2: drive->id->dma_1word |= 0x0404; break;
  725. case XFER_SW_DMA_1: drive->id->dma_1word |= 0x0202; break;
  726. case XFER_SW_DMA_0: drive->id->dma_1word |= 0x0101; break;
  727. default: break;
  728. }
  729. if (!drive->init_speed)
  730. drive->init_speed = speed;
  731. drive->current_speed = speed;
  732. return error;
  733. }
  734. /*
  735. * This should get invoked any time we exit the driver to
  736. * wait for an interrupt response from a drive. handler() points
  737. * at the appropriate code to handle the next interrupt, and a
  738. * timer is started to prevent us from waiting forever in case
  739. * something goes wrong (see the ide_timer_expiry() handler later on).
  740. *
  741. * See also ide_execute_command
  742. */
  743. static void __ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
  744. unsigned int timeout, ide_expiry_t *expiry)
  745. {
  746. ide_hwgroup_t *hwgroup = HWGROUP(drive);
  747. BUG_ON(hwgroup->handler);
  748. hwgroup->handler = handler;
  749. hwgroup->expiry = expiry;
  750. hwgroup->timer.expires = jiffies + timeout;
  751. hwgroup->req_gen_timer = hwgroup->req_gen;
  752. add_timer(&hwgroup->timer);
  753. }
  754. void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
  755. unsigned int timeout, ide_expiry_t *expiry)
  756. {
  757. unsigned long flags;
  758. spin_lock_irqsave(&ide_lock, flags);
  759. __ide_set_handler(drive, handler, timeout, expiry);
  760. spin_unlock_irqrestore(&ide_lock, flags);
  761. }
  762. EXPORT_SYMBOL(ide_set_handler);
  763. /**
  764. * ide_execute_command - execute an IDE command
  765. * @drive: IDE drive to issue the command against
  766. * @command: command byte to write
  767. * @handler: handler for next phase
  768. * @timeout: timeout for command
  769. * @expiry: handler to run on timeout
  770. *
  771. * Helper function to issue an IDE command. This handles the
  772. * atomicity requirements, command timing and ensures that the
  773. * handler and IRQ setup do not race. All IDE command kick off
  774. * should go via this function or do equivalent locking.
  775. */
  776. void ide_execute_command(ide_drive_t *drive, u8 cmd, ide_handler_t *handler,
  777. unsigned timeout, ide_expiry_t *expiry)
  778. {
  779. unsigned long flags;
  780. ide_hwif_t *hwif = HWIF(drive);
  781. spin_lock_irqsave(&ide_lock, flags);
  782. __ide_set_handler(drive, handler, timeout, expiry);
  783. hwif->exec_command(hwif, cmd);
  784. /*
  785. * Drive takes 400nS to respond, we must avoid the IRQ being
  786. * serviced before that.
  787. *
  788. * FIXME: we could skip this delay with care on non shared devices
  789. */
  790. ndelay(400);
  791. spin_unlock_irqrestore(&ide_lock, flags);
  792. }
  793. EXPORT_SYMBOL(ide_execute_command);
  794. void ide_execute_pkt_cmd(ide_drive_t *drive)
  795. {
  796. ide_hwif_t *hwif = drive->hwif;
  797. unsigned long flags;
  798. spin_lock_irqsave(&ide_lock, flags);
  799. hwif->exec_command(hwif, WIN_PACKETCMD);
  800. ndelay(400);
  801. spin_unlock_irqrestore(&ide_lock, flags);
  802. }
  803. EXPORT_SYMBOL_GPL(ide_execute_pkt_cmd);
  804. static inline void ide_complete_drive_reset(ide_drive_t *drive, int err)
  805. {
  806. struct request *rq = drive->hwif->hwgroup->rq;
  807. if (rq && blk_special_request(rq) && rq->cmd[0] == REQ_DRIVE_RESET)
  808. ide_end_request(drive, err ? err : 1, 0);
  809. }
  810. /* needed below */
  811. static ide_startstop_t do_reset1 (ide_drive_t *, int);
  812. /*
  813. * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms
  814. * during an atapi drive reset operation. If the drive has not yet responded,
  815. * and we have not yet hit our maximum waiting time, then the timer is restarted
  816. * for another 50ms.
  817. */
  818. static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive)
  819. {
  820. ide_hwgroup_t *hwgroup = HWGROUP(drive);
  821. u8 stat;
  822. SELECT_DRIVE(drive);
  823. udelay (10);
  824. stat = ide_read_status(drive);
  825. if (OK_STAT(stat, 0, BUSY_STAT))
  826. printk("%s: ATAPI reset complete\n", drive->name);
  827. else {
  828. if (time_before(jiffies, hwgroup->poll_timeout)) {
  829. ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
  830. /* continue polling */
  831. return ide_started;
  832. }
  833. /* end of polling */
  834. hwgroup->polling = 0;
  835. printk("%s: ATAPI reset timed-out, status=0x%02x\n",
  836. drive->name, stat);
  837. /* do it the old fashioned way */
  838. return do_reset1(drive, 1);
  839. }
  840. /* done polling */
  841. hwgroup->polling = 0;
  842. ide_complete_drive_reset(drive, 0);
  843. return ide_stopped;
  844. }
  845. /*
  846. * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
  847. * during an ide reset operation. If the drives have not yet responded,
  848. * and we have not yet hit our maximum waiting time, then the timer is restarted
  849. * for another 50ms.
  850. */
  851. static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
  852. {
  853. ide_hwgroup_t *hwgroup = HWGROUP(drive);
  854. ide_hwif_t *hwif = HWIF(drive);
  855. const struct ide_port_ops *port_ops = hwif->port_ops;
  856. u8 tmp;
  857. int err = 0;
  858. if (port_ops && port_ops->reset_poll) {
  859. err = port_ops->reset_poll(drive);
  860. if (err) {
  861. printk(KERN_ERR "%s: host reset_poll failure for %s.\n",
  862. hwif->name, drive->name);
  863. goto out;
  864. }
  865. }
  866. tmp = ide_read_status(drive);
  867. if (!OK_STAT(tmp, 0, BUSY_STAT)) {
  868. if (time_before(jiffies, hwgroup->poll_timeout)) {
  869. ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
  870. /* continue polling */
  871. return ide_started;
  872. }
  873. printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
  874. drive->failures++;
  875. err = -EIO;
  876. } else {
  877. printk("%s: reset: ", hwif->name);
  878. tmp = ide_read_error(drive);
  879. if (tmp == 1) {
  880. printk("success\n");
  881. drive->failures = 0;
  882. } else {
  883. drive->failures++;
  884. printk("master: ");
  885. switch (tmp & 0x7f) {
  886. case 1: printk("passed");
  887. break;
  888. case 2: printk("formatter device error");
  889. break;
  890. case 3: printk("sector buffer error");
  891. break;
  892. case 4: printk("ECC circuitry error");
  893. break;
  894. case 5: printk("controlling MPU error");
  895. break;
  896. default:printk("error (0x%02x?)", tmp);
  897. }
  898. if (tmp & 0x80)
  899. printk("; slave: failed");
  900. printk("\n");
  901. err = -EIO;
  902. }
  903. }
  904. out:
  905. hwgroup->polling = 0; /* done polling */
  906. ide_complete_drive_reset(drive, err);
  907. return ide_stopped;
  908. }
  909. static void ide_disk_pre_reset(ide_drive_t *drive)
  910. {
  911. int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
  912. drive->special.all = 0;
  913. drive->special.b.set_geometry = legacy;
  914. drive->special.b.recalibrate = legacy;
  915. drive->mult_count = 0;
  916. if (!drive->keep_settings && !drive->using_dma)
  917. drive->mult_req = 0;
  918. if (drive->mult_req != drive->mult_count)
  919. drive->special.b.set_multmode = 1;
  920. }
  921. static void pre_reset(ide_drive_t *drive)
  922. {
  923. const struct ide_port_ops *port_ops = drive->hwif->port_ops;
  924. if (drive->media == ide_disk)
  925. ide_disk_pre_reset(drive);
  926. else
  927. drive->post_reset = 1;
  928. if (drive->using_dma) {
  929. if (drive->crc_count)
  930. ide_check_dma_crc(drive);
  931. else
  932. ide_dma_off(drive);
  933. }
  934. if (!drive->keep_settings) {
  935. if (!drive->using_dma) {
  936. drive->unmask = 0;
  937. drive->io_32bit = 0;
  938. }
  939. return;
  940. }
  941. if (port_ops && port_ops->pre_reset)
  942. port_ops->pre_reset(drive);
  943. if (drive->current_speed != 0xff)
  944. drive->desired_speed = drive->current_speed;
  945. drive->current_speed = 0xff;
  946. }
  947. /*
  948. * do_reset1() attempts to recover a confused drive by resetting it.
  949. * Unfortunately, resetting a disk drive actually resets all devices on
  950. * the same interface, so it can really be thought of as resetting the
  951. * interface rather than resetting the drive.
  952. *
  953. * ATAPI devices have their own reset mechanism which allows them to be
  954. * individually reset without clobbering other devices on the same interface.
  955. *
  956. * Unfortunately, the IDE interface does not generate an interrupt to let
  957. * us know when the reset operation has finished, so we must poll for this.
  958. * Equally poor, though, is the fact that this may a very long time to complete,
  959. * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
  960. * we set a timer to poll at 50ms intervals.
  961. */
  962. static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
  963. {
  964. unsigned int unit;
  965. unsigned long flags;
  966. ide_hwif_t *hwif;
  967. ide_hwgroup_t *hwgroup;
  968. struct ide_io_ports *io_ports;
  969. const struct ide_port_ops *port_ops;
  970. u8 ctl;
  971. spin_lock_irqsave(&ide_lock, flags);
  972. hwif = HWIF(drive);
  973. hwgroup = HWGROUP(drive);
  974. io_ports = &hwif->io_ports;
  975. /* We must not reset with running handlers */
  976. BUG_ON(hwgroup->handler != NULL);
  977. /* For an ATAPI device, first try an ATAPI SRST. */
  978. if (drive->media != ide_disk && !do_not_try_atapi) {
  979. pre_reset(drive);
  980. SELECT_DRIVE(drive);
  981. udelay (20);
  982. hwif->exec_command(hwif, WIN_SRST);
  983. ndelay(400);
  984. hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
  985. hwgroup->polling = 1;
  986. __ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
  987. spin_unlock_irqrestore(&ide_lock, flags);
  988. return ide_started;
  989. }
  990. /*
  991. * First, reset any device state data we were maintaining
  992. * for any of the drives on this interface.
  993. */
  994. for (unit = 0; unit < MAX_DRIVES; ++unit)
  995. pre_reset(&hwif->drives[unit]);
  996. if (io_ports->ctl_addr == 0) {
  997. spin_unlock_irqrestore(&ide_lock, flags);
  998. ide_complete_drive_reset(drive, -ENXIO);
  999. return ide_stopped;
  1000. }
  1001. /*
  1002. * Note that we also set nIEN while resetting the device,
  1003. * to mask unwanted interrupts from the interface during the reset.
  1004. * However, due to the design of PC hardware, this will cause an
  1005. * immediate interrupt due to the edge transition it produces.
  1006. * This single interrupt gives us a "fast poll" for drives that
  1007. * recover from reset very quickly, saving us the first 50ms wait time.
  1008. */
  1009. /* set SRST and nIEN */
  1010. hwif->OUTBSYNC(hwif, ATA_DEVCTL_OBS | 6, io_ports->ctl_addr);
  1011. /* more than enough time */
  1012. udelay(10);
  1013. if (drive->quirk_list == 2)
  1014. ctl = ATA_DEVCTL_OBS; /* clear SRST and nIEN */
  1015. else
  1016. ctl = ATA_DEVCTL_OBS | 2; /* clear SRST, leave nIEN */
  1017. hwif->OUTBSYNC(hwif, ctl, io_ports->ctl_addr);
  1018. /* more than enough time */
  1019. udelay(10);
  1020. hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
  1021. hwgroup->polling = 1;
  1022. __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
  1023. /*
  1024. * Some weird controller like resetting themselves to a strange
  1025. * state when the disks are reset this way. At least, the Winbond
  1026. * 553 documentation says that
  1027. */
  1028. port_ops = hwif->port_ops;
  1029. if (port_ops && port_ops->resetproc)
  1030. port_ops->resetproc(drive);
  1031. spin_unlock_irqrestore(&ide_lock, flags);
  1032. return ide_started;
  1033. }
  1034. /*
  1035. * ide_do_reset() is the entry point to the drive/interface reset code.
  1036. */
  1037. ide_startstop_t ide_do_reset (ide_drive_t *drive)
  1038. {
  1039. return do_reset1(drive, 0);
  1040. }
  1041. EXPORT_SYMBOL(ide_do_reset);
  1042. /*
  1043. * ide_wait_not_busy() waits for the currently selected device on the hwif
  1044. * to report a non-busy status, see comments in ide_probe_port().
  1045. */
  1046. int ide_wait_not_busy(ide_hwif_t *hwif, unsigned long timeout)
  1047. {
  1048. u8 stat = 0;
  1049. while(timeout--) {
  1050. /*
  1051. * Turn this into a schedule() sleep once I'm sure
  1052. * about locking issues (2.5 work ?).
  1053. */
  1054. mdelay(1);
  1055. stat = hwif->INB(hwif->io_ports.status_addr);
  1056. if ((stat & BUSY_STAT) == 0)
  1057. return 0;
  1058. /*
  1059. * Assume a value of 0xff means nothing is connected to
  1060. * the interface and it doesn't implement the pull-down
  1061. * resistor on D7.
  1062. */
  1063. if (stat == 0xff)
  1064. return -ENODEV;
  1065. touch_softlockup_watchdog();
  1066. touch_nmi_watchdog();
  1067. }
  1068. return -EBUSY;
  1069. }
  1070. EXPORT_SYMBOL_GPL(ide_wait_not_busy);