ide-iops.c 32 KB

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