ide-iops.c 34 KB

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