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