ide-iops.c 32 KB

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