ide-iops.c 30 KB

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