ide-iops.c 34 KB

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