ide-iops.c 33 KB

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