ide-iops.c 34 KB

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