ide-iops.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  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 void ide_outb (u8 val, unsigned long port)
  36. {
  37. outb(val, port);
  38. }
  39. /*
  40. * MMIO operations, typically used for SATA controllers
  41. */
  42. static u8 ide_mm_inb (unsigned long port)
  43. {
  44. return (u8) readb((void __iomem *) port);
  45. }
  46. static void ide_mm_outb (u8 value, unsigned long port)
  47. {
  48. writeb(value, (void __iomem *) port);
  49. }
  50. void SELECT_DRIVE (ide_drive_t *drive)
  51. {
  52. ide_hwif_t *hwif = drive->hwif;
  53. const struct ide_port_ops *port_ops = hwif->port_ops;
  54. ide_task_t task;
  55. if (port_ops && port_ops->selectproc)
  56. port_ops->selectproc(drive);
  57. memset(&task, 0, sizeof(task));
  58. task.tf_flags = IDE_TFLAG_OUT_DEVICE;
  59. drive->hwif->tp_ops->tf_load(drive, &task);
  60. }
  61. void SELECT_MASK(ide_drive_t *drive, int mask)
  62. {
  63. const struct ide_port_ops *port_ops = drive->hwif->port_ops;
  64. if (port_ops && port_ops->maskproc)
  65. port_ops->maskproc(drive, mask);
  66. }
  67. void ide_exec_command(ide_hwif_t *hwif, u8 cmd)
  68. {
  69. if (hwif->host_flags & IDE_HFLAG_MMIO)
  70. writeb(cmd, (void __iomem *)hwif->io_ports.command_addr);
  71. else
  72. outb(cmd, hwif->io_ports.command_addr);
  73. }
  74. EXPORT_SYMBOL_GPL(ide_exec_command);
  75. u8 ide_read_status(ide_hwif_t *hwif)
  76. {
  77. if (hwif->host_flags & IDE_HFLAG_MMIO)
  78. return readb((void __iomem *)hwif->io_ports.status_addr);
  79. else
  80. return inb(hwif->io_ports.status_addr);
  81. }
  82. EXPORT_SYMBOL_GPL(ide_read_status);
  83. u8 ide_read_altstatus(ide_hwif_t *hwif)
  84. {
  85. if (hwif->host_flags & IDE_HFLAG_MMIO)
  86. return readb((void __iomem *)hwif->io_ports.ctl_addr);
  87. else
  88. return inb(hwif->io_ports.ctl_addr);
  89. }
  90. EXPORT_SYMBOL_GPL(ide_read_altstatus);
  91. u8 ide_read_sff_dma_status(ide_hwif_t *hwif)
  92. {
  93. if (hwif->host_flags & IDE_HFLAG_MMIO)
  94. return readb((void __iomem *)(hwif->dma_base + ATA_DMA_STATUS));
  95. else
  96. return inb(hwif->dma_base + ATA_DMA_STATUS);
  97. }
  98. EXPORT_SYMBOL_GPL(ide_read_sff_dma_status);
  99. void ide_set_irq(ide_hwif_t *hwif, int on)
  100. {
  101. u8 ctl = ATA_DEVCTL_OBS;
  102. if (on == 4) { /* hack for SRST */
  103. ctl |= 4;
  104. on &= ~4;
  105. }
  106. ctl |= on ? 0 : 2;
  107. if (hwif->host_flags & IDE_HFLAG_MMIO)
  108. writeb(ctl, (void __iomem *)hwif->io_ports.ctl_addr);
  109. else
  110. outb(ctl, hwif->io_ports.ctl_addr);
  111. }
  112. EXPORT_SYMBOL_GPL(ide_set_irq);
  113. void ide_tf_load(ide_drive_t *drive, ide_task_t *task)
  114. {
  115. ide_hwif_t *hwif = drive->hwif;
  116. struct ide_io_ports *io_ports = &hwif->io_ports;
  117. struct ide_taskfile *tf = &task->tf;
  118. void (*tf_outb)(u8 addr, unsigned long port);
  119. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  120. u8 HIHI = (task->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
  121. if (mmio)
  122. tf_outb = ide_mm_outb;
  123. else
  124. tf_outb = ide_outb;
  125. if (task->tf_flags & IDE_TFLAG_FLAGGED)
  126. HIHI = 0xFF;
  127. if (task->tf_flags & IDE_TFLAG_OUT_DATA) {
  128. u16 data = (tf->hob_data << 8) | tf->data;
  129. if (mmio)
  130. writew(data, (void __iomem *)io_ports->data_addr);
  131. else
  132. outw(data, io_ports->data_addr);
  133. }
  134. if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
  135. tf_outb(tf->hob_feature, io_ports->feature_addr);
  136. if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
  137. tf_outb(tf->hob_nsect, io_ports->nsect_addr);
  138. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
  139. tf_outb(tf->hob_lbal, io_ports->lbal_addr);
  140. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
  141. tf_outb(tf->hob_lbam, io_ports->lbam_addr);
  142. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
  143. tf_outb(tf->hob_lbah, io_ports->lbah_addr);
  144. if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
  145. tf_outb(tf->feature, io_ports->feature_addr);
  146. if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
  147. tf_outb(tf->nsect, io_ports->nsect_addr);
  148. if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
  149. tf_outb(tf->lbal, io_ports->lbal_addr);
  150. if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
  151. tf_outb(tf->lbam, io_ports->lbam_addr);
  152. if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
  153. tf_outb(tf->lbah, io_ports->lbah_addr);
  154. if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
  155. tf_outb((tf->device & HIHI) | drive->select.all,
  156. io_ports->device_addr);
  157. }
  158. EXPORT_SYMBOL_GPL(ide_tf_load);
  159. void ide_tf_read(ide_drive_t *drive, ide_task_t *task)
  160. {
  161. ide_hwif_t *hwif = drive->hwif;
  162. struct ide_io_ports *io_ports = &hwif->io_ports;
  163. struct ide_taskfile *tf = &task->tf;
  164. void (*tf_outb)(u8 addr, unsigned long port);
  165. u8 (*tf_inb)(unsigned long port);
  166. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  167. if (mmio) {
  168. tf_outb = ide_mm_outb;
  169. tf_inb = ide_mm_inb;
  170. } else {
  171. tf_outb = ide_outb;
  172. tf_inb = ide_inb;
  173. }
  174. if (task->tf_flags & IDE_TFLAG_IN_DATA) {
  175. u16 data;
  176. if (mmio)
  177. data = readw((void __iomem *)io_ports->data_addr);
  178. else
  179. data = inw(io_ports->data_addr);
  180. tf->data = data & 0xff;
  181. tf->hob_data = (data >> 8) & 0xff;
  182. }
  183. /* be sure we're looking at the low order bits */
  184. tf_outb(ATA_DEVCTL_OBS & ~0x80, io_ports->ctl_addr);
  185. if (task->tf_flags & IDE_TFLAG_IN_FEATURE)
  186. tf->feature = tf_inb(io_ports->feature_addr);
  187. if (task->tf_flags & IDE_TFLAG_IN_NSECT)
  188. tf->nsect = tf_inb(io_ports->nsect_addr);
  189. if (task->tf_flags & IDE_TFLAG_IN_LBAL)
  190. tf->lbal = tf_inb(io_ports->lbal_addr);
  191. if (task->tf_flags & IDE_TFLAG_IN_LBAM)
  192. tf->lbam = tf_inb(io_ports->lbam_addr);
  193. if (task->tf_flags & IDE_TFLAG_IN_LBAH)
  194. tf->lbah = tf_inb(io_ports->lbah_addr);
  195. if (task->tf_flags & IDE_TFLAG_IN_DEVICE)
  196. tf->device = tf_inb(io_ports->device_addr);
  197. if (task->tf_flags & IDE_TFLAG_LBA48) {
  198. tf_outb(ATA_DEVCTL_OBS | 0x80, io_ports->ctl_addr);
  199. if (task->tf_flags & IDE_TFLAG_IN_HOB_FEATURE)
  200. tf->hob_feature = tf_inb(io_ports->feature_addr);
  201. if (task->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
  202. tf->hob_nsect = tf_inb(io_ports->nsect_addr);
  203. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
  204. tf->hob_lbal = tf_inb(io_ports->lbal_addr);
  205. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
  206. tf->hob_lbam = tf_inb(io_ports->lbam_addr);
  207. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
  208. tf->hob_lbah = tf_inb(io_ports->lbah_addr);
  209. }
  210. }
  211. EXPORT_SYMBOL_GPL(ide_tf_read);
  212. /*
  213. * Some localbus EIDE interfaces require a special access sequence
  214. * when using 32-bit I/O instructions to transfer data. We call this
  215. * the "vlb_sync" sequence, which consists of three successive reads
  216. * of the sector count register location, with interrupts disabled
  217. * to ensure that the reads all happen together.
  218. */
  219. static void ata_vlb_sync(unsigned long port)
  220. {
  221. (void)inb(port);
  222. (void)inb(port);
  223. (void)inb(port);
  224. }
  225. /*
  226. * This is used for most PIO data transfers *from* the IDE interface
  227. *
  228. * These routines will round up any request for an odd number of bytes,
  229. * so if an odd len is specified, be sure that there's at least one
  230. * extra byte allocated for the buffer.
  231. */
  232. void ide_input_data(ide_drive_t *drive, struct request *rq, void *buf,
  233. unsigned int len)
  234. {
  235. ide_hwif_t *hwif = drive->hwif;
  236. struct ide_io_ports *io_ports = &hwif->io_ports;
  237. unsigned long data_addr = io_ports->data_addr;
  238. u8 io_32bit = drive->io_32bit;
  239. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  240. len++;
  241. if (io_32bit) {
  242. unsigned long uninitialized_var(flags);
  243. if ((io_32bit & 2) && !mmio) {
  244. local_irq_save(flags);
  245. ata_vlb_sync(io_ports->nsect_addr);
  246. }
  247. if (mmio)
  248. __ide_mm_insl((void __iomem *)data_addr, buf, len / 4);
  249. else
  250. insl(data_addr, buf, len / 4);
  251. if ((io_32bit & 2) && !mmio)
  252. local_irq_restore(flags);
  253. if ((len & 3) >= 2) {
  254. if (mmio)
  255. __ide_mm_insw((void __iomem *)data_addr,
  256. (u8 *)buf + (len & ~3), 1);
  257. else
  258. insw(data_addr, (u8 *)buf + (len & ~3), 1);
  259. }
  260. } else {
  261. if (mmio)
  262. __ide_mm_insw((void __iomem *)data_addr, buf, len / 2);
  263. else
  264. insw(data_addr, buf, len / 2);
  265. }
  266. }
  267. EXPORT_SYMBOL_GPL(ide_input_data);
  268. /*
  269. * This is used for most PIO data transfers *to* the IDE interface
  270. */
  271. void ide_output_data(ide_drive_t *drive, struct request *rq, void *buf,
  272. unsigned int len)
  273. {
  274. ide_hwif_t *hwif = drive->hwif;
  275. struct ide_io_ports *io_ports = &hwif->io_ports;
  276. unsigned long data_addr = io_ports->data_addr;
  277. u8 io_32bit = drive->io_32bit;
  278. u8 mmio = (hwif->host_flags & IDE_HFLAG_MMIO) ? 1 : 0;
  279. if (io_32bit) {
  280. unsigned long uninitialized_var(flags);
  281. if ((io_32bit & 2) && !mmio) {
  282. local_irq_save(flags);
  283. ata_vlb_sync(io_ports->nsect_addr);
  284. }
  285. if (mmio)
  286. __ide_mm_outsl((void __iomem *)data_addr, buf, len / 4);
  287. else
  288. outsl(data_addr, buf, len / 4);
  289. if ((io_32bit & 2) && !mmio)
  290. local_irq_restore(flags);
  291. if ((len & 3) >= 2) {
  292. if (mmio)
  293. __ide_mm_outsw((void __iomem *)data_addr,
  294. (u8 *)buf + (len & ~3), 1);
  295. else
  296. outsw(data_addr, (u8 *)buf + (len & ~3), 1);
  297. }
  298. } else {
  299. if (mmio)
  300. __ide_mm_outsw((void __iomem *)data_addr, buf, len / 2);
  301. else
  302. outsw(data_addr, buf, len / 2);
  303. }
  304. }
  305. EXPORT_SYMBOL_GPL(ide_output_data);
  306. u8 ide_read_error(ide_drive_t *drive)
  307. {
  308. ide_task_t task;
  309. memset(&task, 0, sizeof(task));
  310. task.tf_flags = IDE_TFLAG_IN_FEATURE;
  311. drive->hwif->tp_ops->tf_read(drive, &task);
  312. return task.tf.error;
  313. }
  314. EXPORT_SYMBOL_GPL(ide_read_error);
  315. void ide_read_bcount_and_ireason(ide_drive_t *drive, u16 *bcount, u8 *ireason)
  316. {
  317. ide_task_t task;
  318. memset(&task, 0, sizeof(task));
  319. task.tf_flags = IDE_TFLAG_IN_LBAH | IDE_TFLAG_IN_LBAM |
  320. IDE_TFLAG_IN_NSECT;
  321. drive->hwif->tp_ops->tf_read(drive, &task);
  322. *bcount = (task.tf.lbah << 8) | task.tf.lbam;
  323. *ireason = task.tf.nsect & 3;
  324. }
  325. EXPORT_SYMBOL_GPL(ide_read_bcount_and_ireason);
  326. const struct ide_tp_ops default_tp_ops = {
  327. .exec_command = ide_exec_command,
  328. .read_status = ide_read_status,
  329. .read_altstatus = ide_read_altstatus,
  330. .read_sff_dma_status = ide_read_sff_dma_status,
  331. .set_irq = ide_set_irq,
  332. .tf_load = ide_tf_load,
  333. .tf_read = ide_tf_read,
  334. .input_data = ide_input_data,
  335. .output_data = ide_output_data,
  336. };
  337. void ide_fix_driveid(u16 *id)
  338. {
  339. #ifndef __LITTLE_ENDIAN
  340. # ifdef __BIG_ENDIAN
  341. int i;
  342. for (i = 0; i < 256; i++)
  343. id[i] = __le16_to_cpu(id[i]);
  344. # else
  345. # error "Please fix <asm/byteorder.h>"
  346. # endif
  347. #endif
  348. }
  349. /*
  350. * ide_fixstring() cleans up and (optionally) byte-swaps a text string,
  351. * removing leading/trailing blanks and compressing internal blanks.
  352. * It is primarily used to tidy up the model name/number fields as
  353. * returned by the ATA_CMD_ID_ATA[PI] commands.
  354. */
  355. void ide_fixstring (u8 *s, const int bytecount, const int byteswap)
  356. {
  357. u8 *p, *end = &s[bytecount & ~1]; /* bytecount must be even */
  358. if (byteswap) {
  359. /* convert from big-endian to host byte order */
  360. for (p = s ; p != end ; p += 2)
  361. be16_to_cpus((u16 *) p);
  362. }
  363. /* strip leading blanks */
  364. p = s;
  365. while (s != end && *s == ' ')
  366. ++s;
  367. /* compress internal blanks and strip trailing blanks */
  368. while (s != end && *s) {
  369. if (*s++ != ' ' || (s != end && *s && *s != ' '))
  370. *p++ = *(s-1);
  371. }
  372. /* wipe out trailing garbage */
  373. while (p != end)
  374. *p++ = '\0';
  375. }
  376. EXPORT_SYMBOL(ide_fixstring);
  377. /*
  378. * Needed for PCI irq sharing
  379. */
  380. int drive_is_ready (ide_drive_t *drive)
  381. {
  382. ide_hwif_t *hwif = HWIF(drive);
  383. u8 stat = 0;
  384. if (drive->waiting_for_dma)
  385. return hwif->dma_ops->dma_test_irq(drive);
  386. #if 0
  387. /* need to guarantee 400ns since last command was issued */
  388. udelay(1);
  389. #endif
  390. /*
  391. * We do a passive status test under shared PCI interrupts on
  392. * cards that truly share the ATA side interrupt, but may also share
  393. * an interrupt with another pci card/device. We make no assumptions
  394. * about possible isa-pnp and pci-pnp issues yet.
  395. */
  396. if (hwif->io_ports.ctl_addr)
  397. stat = hwif->tp_ops->read_altstatus(hwif);
  398. else
  399. /* Note: this may clear a pending IRQ!! */
  400. stat = hwif->tp_ops->read_status(hwif);
  401. if (stat & ATA_BUSY)
  402. /* drive busy: definitely not interrupting */
  403. return 0;
  404. /* drive ready: *might* be interrupting */
  405. return 1;
  406. }
  407. EXPORT_SYMBOL(drive_is_ready);
  408. /*
  409. * This routine busy-waits for the drive status to be not "busy".
  410. * It then checks the status for all of the "good" bits and none
  411. * of the "bad" bits, and if all is okay it returns 0. All other
  412. * cases return error -- caller may then invoke ide_error().
  413. *
  414. * This routine should get fixed to not hog the cpu during extra long waits..
  415. * That could be done by busy-waiting for the first jiffy or two, and then
  416. * setting a timer to wake up at half second intervals thereafter,
  417. * until timeout is achieved, before timing out.
  418. */
  419. static int __ide_wait_stat(ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout, u8 *rstat)
  420. {
  421. ide_hwif_t *hwif = drive->hwif;
  422. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  423. unsigned long flags;
  424. int i;
  425. u8 stat;
  426. udelay(1); /* spec allows drive 400ns to assert "BUSY" */
  427. stat = tp_ops->read_status(hwif);
  428. if (stat & ATA_BUSY) {
  429. local_irq_set(flags);
  430. timeout += jiffies;
  431. while ((stat = tp_ops->read_status(hwif)) & ATA_BUSY) {
  432. if (time_after(jiffies, timeout)) {
  433. /*
  434. * One last read after the timeout in case
  435. * heavy interrupt load made us not make any
  436. * progress during the timeout..
  437. */
  438. stat = tp_ops->read_status(hwif);
  439. if ((stat & ATA_BUSY) == 0)
  440. break;
  441. local_irq_restore(flags);
  442. *rstat = stat;
  443. return -EBUSY;
  444. }
  445. }
  446. local_irq_restore(flags);
  447. }
  448. /*
  449. * Allow status to settle, then read it again.
  450. * A few rare drives vastly violate the 400ns spec here,
  451. * so we'll wait up to 10usec for a "good" status
  452. * rather than expensively fail things immediately.
  453. * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
  454. */
  455. for (i = 0; i < 10; i++) {
  456. udelay(1);
  457. stat = tp_ops->read_status(hwif);
  458. if (OK_STAT(stat, good, bad)) {
  459. *rstat = stat;
  460. return 0;
  461. }
  462. }
  463. *rstat = stat;
  464. return -EFAULT;
  465. }
  466. /*
  467. * In case of error returns error value after doing "*startstop = ide_error()".
  468. * The caller should return the updated value of "startstop" in this case,
  469. * "startstop" is unchanged when the function returns 0.
  470. */
  471. int ide_wait_stat(ide_startstop_t *startstop, ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout)
  472. {
  473. int err;
  474. u8 stat;
  475. /* bail early if we've exceeded max_failures */
  476. if (drive->max_failures && (drive->failures > drive->max_failures)) {
  477. *startstop = ide_stopped;
  478. return 1;
  479. }
  480. err = __ide_wait_stat(drive, good, bad, timeout, &stat);
  481. if (err) {
  482. char *s = (err == -EBUSY) ? "status timeout" : "status error";
  483. *startstop = ide_error(drive, s, stat);
  484. }
  485. return err;
  486. }
  487. EXPORT_SYMBOL(ide_wait_stat);
  488. /**
  489. * ide_in_drive_list - look for drive in black/white list
  490. * @id: drive identifier
  491. * @table: list to inspect
  492. *
  493. * Look for a drive in the blacklist and the whitelist tables
  494. * Returns 1 if the drive is found in the table.
  495. */
  496. int ide_in_drive_list(u16 *id, const struct drive_list_entry *table)
  497. {
  498. for ( ; table->id_model; table++)
  499. if ((!strcmp(table->id_model, (char *)&id[ATA_ID_PROD])) &&
  500. (!table->id_firmware ||
  501. strstr((char *)&id[ATA_ID_FW_REV], table->id_firmware)))
  502. return 1;
  503. return 0;
  504. }
  505. EXPORT_SYMBOL_GPL(ide_in_drive_list);
  506. /*
  507. * Early UDMA66 devices don't set bit14 to 1, only bit13 is valid.
  508. * We list them here and depend on the device side cable detection for them.
  509. *
  510. * Some optical devices with the buggy firmwares have the same problem.
  511. */
  512. static const struct drive_list_entry ivb_list[] = {
  513. { "QUANTUM FIREBALLlct10 05" , "A03.0900" },
  514. { "TSSTcorp CDDVDW SH-S202J" , "SB00" },
  515. { "TSSTcorp CDDVDW SH-S202J" , "SB01" },
  516. { "TSSTcorp CDDVDW SH-S202N" , "SB00" },
  517. { "TSSTcorp CDDVDW SH-S202N" , "SB01" },
  518. { "TSSTcorp CDDVDW SH-S202H" , "SB00" },
  519. { "TSSTcorp CDDVDW SH-S202H" , "SB01" },
  520. { NULL , NULL }
  521. };
  522. /*
  523. * All hosts that use the 80c ribbon must use!
  524. * The name is derived from upper byte of word 93 and the 80c ribbon.
  525. */
  526. u8 eighty_ninty_three (ide_drive_t *drive)
  527. {
  528. ide_hwif_t *hwif = drive->hwif;
  529. u16 *id = drive->id;
  530. int ivb = ide_in_drive_list(id, ivb_list);
  531. if (hwif->cbl == ATA_CBL_PATA40_SHORT)
  532. return 1;
  533. if (ivb)
  534. printk(KERN_DEBUG "%s: skipping word 93 validity check\n",
  535. drive->name);
  536. if (ide_dev_is_sata(id) && !ivb)
  537. return 1;
  538. if (hwif->cbl != ATA_CBL_PATA80 && !ivb)
  539. goto no_80w;
  540. /*
  541. * FIXME:
  542. * - change master/slave IDENTIFY order
  543. * - force bit13 (80c cable present) check also for !ivb devices
  544. * (unless the slave device is pre-ATA3)
  545. */
  546. if ((id[ATA_ID_HW_CONFIG] & 0x4000) ||
  547. (ivb && (id[ATA_ID_HW_CONFIG] & 0x2000)))
  548. return 1;
  549. no_80w:
  550. if (drive->udma33_warned == 1)
  551. return 0;
  552. printk(KERN_WARNING "%s: %s side 80-wire cable detection failed, "
  553. "limiting max speed to UDMA33\n",
  554. drive->name,
  555. hwif->cbl == ATA_CBL_PATA80 ? "drive" : "host");
  556. drive->udma33_warned = 1;
  557. return 0;
  558. }
  559. int ide_driveid_update(ide_drive_t *drive)
  560. {
  561. ide_hwif_t *hwif = drive->hwif;
  562. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  563. u16 *id;
  564. unsigned long timeout, flags;
  565. u8 stat;
  566. /*
  567. * Re-read drive->id for possible DMA mode
  568. * change (copied from ide-probe.c)
  569. */
  570. SELECT_MASK(drive, 1);
  571. tp_ops->set_irq(hwif, 0);
  572. msleep(50);
  573. tp_ops->exec_command(hwif, ATA_CMD_ID_ATA);
  574. timeout = jiffies + WAIT_WORSTCASE;
  575. do {
  576. if (time_after(jiffies, timeout)) {
  577. SELECT_MASK(drive, 0);
  578. return 0; /* drive timed-out */
  579. }
  580. msleep(50); /* give drive a breather */
  581. stat = tp_ops->read_altstatus(hwif);
  582. } while (stat & ATA_BUSY);
  583. msleep(50); /* wait for IRQ and ATA_DRQ */
  584. stat = tp_ops->read_status(hwif);
  585. if (!OK_STAT(stat, ATA_DRQ, BAD_R_STAT)) {
  586. SELECT_MASK(drive, 0);
  587. printk("%s: CHECK for good STATUS\n", drive->name);
  588. return 0;
  589. }
  590. local_irq_save(flags);
  591. SELECT_MASK(drive, 0);
  592. id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
  593. if (!id) {
  594. local_irq_restore(flags);
  595. return 0;
  596. }
  597. tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
  598. (void)tp_ops->read_status(hwif); /* clear drive IRQ */
  599. local_irq_enable();
  600. local_irq_restore(flags);
  601. ide_fix_driveid(id);
  602. drive->id[ATA_ID_UDMA_MODES] = id[ATA_ID_UDMA_MODES];
  603. drive->id[ATA_ID_MWDMA_MODES] = id[ATA_ID_MWDMA_MODES];
  604. drive->id[ATA_ID_SWDMA_MODES] = id[ATA_ID_SWDMA_MODES];
  605. /* anything more ? */
  606. kfree(id);
  607. if (drive->using_dma && ide_id_dma_bug(drive))
  608. ide_dma_off(drive);
  609. return 1;
  610. }
  611. int ide_config_drive_speed(ide_drive_t *drive, u8 speed)
  612. {
  613. ide_hwif_t *hwif = drive->hwif;
  614. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  615. u16 *id = drive->id, i;
  616. int error = 0;
  617. u8 stat;
  618. ide_task_t task;
  619. #ifdef CONFIG_BLK_DEV_IDEDMA
  620. if (hwif->dma_ops) /* check if host supports DMA */
  621. hwif->dma_ops->dma_host_set(drive, 0);
  622. #endif
  623. /* Skip setting PIO flow-control modes on pre-EIDE drives */
  624. if ((speed & 0xf8) == XFER_PIO_0 && ata_id_has_iordy(drive->id) == 0)
  625. goto skip;
  626. /*
  627. * Don't use ide_wait_cmd here - it will
  628. * attempt to set_geometry and recalibrate,
  629. * but for some reason these don't work at
  630. * this point (lost interrupt).
  631. */
  632. /*
  633. * Select the drive, and issue the SETFEATURES command
  634. */
  635. disable_irq_nosync(hwif->irq);
  636. /*
  637. * FIXME: we race against the running IRQ here if
  638. * this is called from non IRQ context. If we use
  639. * disable_irq() we hang on the error path. Work
  640. * is needed.
  641. */
  642. udelay(1);
  643. SELECT_DRIVE(drive);
  644. SELECT_MASK(drive, 0);
  645. udelay(1);
  646. tp_ops->set_irq(hwif, 0);
  647. memset(&task, 0, sizeof(task));
  648. task.tf_flags = IDE_TFLAG_OUT_FEATURE | IDE_TFLAG_OUT_NSECT;
  649. task.tf.feature = SETFEATURES_XFER;
  650. task.tf.nsect = speed;
  651. tp_ops->tf_load(drive, &task);
  652. tp_ops->exec_command(hwif, ATA_CMD_SET_FEATURES);
  653. if (drive->quirk_list == 2)
  654. tp_ops->set_irq(hwif, 1);
  655. error = __ide_wait_stat(drive, drive->ready_stat,
  656. ATA_BUSY | ATA_DRQ | ATA_ERR,
  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. id[ATA_ID_UDMA_MODES] &= ~0xFF00;
  665. id[ATA_ID_MWDMA_MODES] &= ~0x0F00;
  666. id[ATA_ID_SWDMA_MODES] &= ~0x0F00;
  667. skip:
  668. #ifdef CONFIG_BLK_DEV_IDEDMA
  669. if (speed >= XFER_SW_DMA_0 && drive->using_dma)
  670. hwif->dma_ops->dma_host_set(drive, 1);
  671. else if (hwif->dma_ops) /* check if host supports DMA */
  672. ide_dma_off_quietly(drive);
  673. #endif
  674. if (speed >= XFER_UDMA_0) {
  675. i = 1 << (speed - XFER_UDMA_0);
  676. id[ATA_ID_UDMA_MODES] |= (i << 8 | i);
  677. } else if (speed >= XFER_MW_DMA_0) {
  678. i = 1 << (speed - XFER_MW_DMA_0);
  679. id[ATA_ID_MWDMA_MODES] |= (i << 8 | i);
  680. } else if (speed >= XFER_SW_DMA_0) {
  681. i = 1 << (speed - XFER_SW_DMA_0);
  682. id[ATA_ID_SWDMA_MODES] |= (i << 8 | i);
  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->tp_ops->exec_command(hwif, cmd);
  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. void ide_execute_pkt_cmd(ide_drive_t *drive)
  750. {
  751. ide_hwif_t *hwif = drive->hwif;
  752. unsigned long flags;
  753. spin_lock_irqsave(&ide_lock, flags);
  754. hwif->tp_ops->exec_command(hwif, ATA_CMD_PACKET);
  755. ndelay(400);
  756. spin_unlock_irqrestore(&ide_lock, flags);
  757. }
  758. EXPORT_SYMBOL_GPL(ide_execute_pkt_cmd);
  759. static inline void ide_complete_drive_reset(ide_drive_t *drive, int err)
  760. {
  761. struct request *rq = drive->hwif->hwgroup->rq;
  762. if (rq && blk_special_request(rq) && rq->cmd[0] == REQ_DRIVE_RESET)
  763. ide_end_request(drive, err ? err : 1, 0);
  764. }
  765. /* needed below */
  766. static ide_startstop_t do_reset1 (ide_drive_t *, int);
  767. /*
  768. * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms
  769. * during an atapi drive reset operation. If the drive has not yet responded,
  770. * and we have not yet hit our maximum waiting time, then the timer is restarted
  771. * for another 50ms.
  772. */
  773. static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive)
  774. {
  775. ide_hwif_t *hwif = drive->hwif;
  776. ide_hwgroup_t *hwgroup = hwif->hwgroup;
  777. u8 stat;
  778. SELECT_DRIVE(drive);
  779. udelay (10);
  780. stat = hwif->tp_ops->read_status(hwif);
  781. if (OK_STAT(stat, 0, ATA_BUSY))
  782. printk("%s: ATAPI reset complete\n", drive->name);
  783. else {
  784. if (time_before(jiffies, hwgroup->poll_timeout)) {
  785. ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
  786. /* continue polling */
  787. return ide_started;
  788. }
  789. /* end of polling */
  790. hwgroup->polling = 0;
  791. printk("%s: ATAPI reset timed-out, status=0x%02x\n",
  792. drive->name, stat);
  793. /* do it the old fashioned way */
  794. return do_reset1(drive, 1);
  795. }
  796. /* done polling */
  797. hwgroup->polling = 0;
  798. ide_complete_drive_reset(drive, 0);
  799. return ide_stopped;
  800. }
  801. /*
  802. * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
  803. * during an ide reset operation. If the drives have not yet responded,
  804. * and we have not yet hit our maximum waiting time, then the timer is restarted
  805. * for another 50ms.
  806. */
  807. static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
  808. {
  809. ide_hwgroup_t *hwgroup = HWGROUP(drive);
  810. ide_hwif_t *hwif = HWIF(drive);
  811. const struct ide_port_ops *port_ops = hwif->port_ops;
  812. u8 tmp;
  813. int err = 0;
  814. if (port_ops && port_ops->reset_poll) {
  815. err = port_ops->reset_poll(drive);
  816. if (err) {
  817. printk(KERN_ERR "%s: host reset_poll failure for %s.\n",
  818. hwif->name, drive->name);
  819. goto out;
  820. }
  821. }
  822. tmp = hwif->tp_ops->read_status(hwif);
  823. if (!OK_STAT(tmp, 0, ATA_BUSY)) {
  824. if (time_before(jiffies, hwgroup->poll_timeout)) {
  825. ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
  826. /* continue polling */
  827. return ide_started;
  828. }
  829. printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
  830. drive->failures++;
  831. err = -EIO;
  832. } else {
  833. printk("%s: reset: ", hwif->name);
  834. tmp = ide_read_error(drive);
  835. if (tmp == 1) {
  836. printk("success\n");
  837. drive->failures = 0;
  838. } else {
  839. drive->failures++;
  840. printk("master: ");
  841. switch (tmp & 0x7f) {
  842. case 1: printk("passed");
  843. break;
  844. case 2: printk("formatter device error");
  845. break;
  846. case 3: printk("sector buffer error");
  847. break;
  848. case 4: printk("ECC circuitry error");
  849. break;
  850. case 5: printk("controlling MPU error");
  851. break;
  852. default:printk("error (0x%02x?)", tmp);
  853. }
  854. if (tmp & 0x80)
  855. printk("; slave: failed");
  856. printk("\n");
  857. err = -EIO;
  858. }
  859. }
  860. out:
  861. hwgroup->polling = 0; /* done polling */
  862. ide_complete_drive_reset(drive, err);
  863. return ide_stopped;
  864. }
  865. static void ide_disk_pre_reset(ide_drive_t *drive)
  866. {
  867. int legacy = (drive->id[ATA_ID_CFS_ENABLE_2] & 0x0400) ? 0 : 1;
  868. drive->special.all = 0;
  869. drive->special.b.set_geometry = legacy;
  870. drive->special.b.recalibrate = legacy;
  871. drive->mult_count = 0;
  872. if (!drive->keep_settings && !drive->using_dma)
  873. drive->mult_req = 0;
  874. if (drive->mult_req != drive->mult_count)
  875. drive->special.b.set_multmode = 1;
  876. }
  877. static void pre_reset(ide_drive_t *drive)
  878. {
  879. const struct ide_port_ops *port_ops = drive->hwif->port_ops;
  880. if (drive->media == ide_disk)
  881. ide_disk_pre_reset(drive);
  882. else
  883. drive->post_reset = 1;
  884. if (drive->using_dma) {
  885. if (drive->crc_count)
  886. ide_check_dma_crc(drive);
  887. else
  888. ide_dma_off(drive);
  889. }
  890. if (!drive->keep_settings) {
  891. if (!drive->using_dma) {
  892. drive->unmask = 0;
  893. drive->io_32bit = 0;
  894. }
  895. return;
  896. }
  897. if (port_ops && port_ops->pre_reset)
  898. port_ops->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. struct ide_io_ports *io_ports;
  925. const struct ide_tp_ops *tp_ops;
  926. const struct ide_port_ops *port_ops;
  927. spin_lock_irqsave(&ide_lock, flags);
  928. hwif = HWIF(drive);
  929. hwgroup = HWGROUP(drive);
  930. io_ports = &hwif->io_ports;
  931. tp_ops = hwif->tp_ops;
  932. /* We must not reset with running handlers */
  933. BUG_ON(hwgroup->handler != NULL);
  934. /* For an ATAPI device, first try an ATAPI SRST. */
  935. if (drive->media != ide_disk && !do_not_try_atapi) {
  936. pre_reset(drive);
  937. SELECT_DRIVE(drive);
  938. udelay (20);
  939. tp_ops->exec_command(hwif, ATA_CMD_DEV_RESET);
  940. ndelay(400);
  941. hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
  942. hwgroup->polling = 1;
  943. __ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
  944. spin_unlock_irqrestore(&ide_lock, flags);
  945. return ide_started;
  946. }
  947. /*
  948. * First, reset any device state data we were maintaining
  949. * for any of the drives on this interface.
  950. */
  951. for (unit = 0; unit < MAX_DRIVES; ++unit)
  952. pre_reset(&hwif->drives[unit]);
  953. if (io_ports->ctl_addr == 0) {
  954. spin_unlock_irqrestore(&ide_lock, flags);
  955. ide_complete_drive_reset(drive, -ENXIO);
  956. return ide_stopped;
  957. }
  958. /*
  959. * Note that we also set nIEN while resetting the device,
  960. * to mask unwanted interrupts from the interface during the reset.
  961. * However, due to the design of PC hardware, this will cause an
  962. * immediate interrupt due to the edge transition it produces.
  963. * This single interrupt gives us a "fast poll" for drives that
  964. * recover from reset very quickly, saving us the first 50ms wait time.
  965. *
  966. * TODO: add ->softreset method and stop abusing ->set_irq
  967. */
  968. /* set SRST and nIEN */
  969. tp_ops->set_irq(hwif, 4);
  970. /* more than enough time */
  971. udelay(10);
  972. /* clear SRST, leave nIEN (unless device is on the quirk list) */
  973. tp_ops->set_irq(hwif, drive->quirk_list == 2);
  974. /* more than enough time */
  975. udelay(10);
  976. hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
  977. hwgroup->polling = 1;
  978. __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
  979. /*
  980. * Some weird controller like resetting themselves to a strange
  981. * state when the disks are reset this way. At least, the Winbond
  982. * 553 documentation says that
  983. */
  984. port_ops = hwif->port_ops;
  985. if (port_ops && port_ops->resetproc)
  986. port_ops->resetproc(drive);
  987. spin_unlock_irqrestore(&ide_lock, flags);
  988. return ide_started;
  989. }
  990. /*
  991. * ide_do_reset() is the entry point to the drive/interface reset code.
  992. */
  993. ide_startstop_t ide_do_reset (ide_drive_t *drive)
  994. {
  995. return do_reset1(drive, 0);
  996. }
  997. EXPORT_SYMBOL(ide_do_reset);
  998. /*
  999. * ide_wait_not_busy() waits for the currently selected device on the hwif
  1000. * to report a non-busy status, see comments in ide_probe_port().
  1001. */
  1002. int ide_wait_not_busy(ide_hwif_t *hwif, unsigned long timeout)
  1003. {
  1004. u8 stat = 0;
  1005. while(timeout--) {
  1006. /*
  1007. * Turn this into a schedule() sleep once I'm sure
  1008. * about locking issues (2.5 work ?).
  1009. */
  1010. mdelay(1);
  1011. stat = hwif->tp_ops->read_status(hwif);
  1012. if ((stat & ATA_BUSY) == 0)
  1013. return 0;
  1014. /*
  1015. * Assume a value of 0xff means nothing is connected to
  1016. * the interface and it doesn't implement the pull-down
  1017. * resistor on D7.
  1018. */
  1019. if (stat == 0xff)
  1020. return -ENODEV;
  1021. touch_softlockup_watchdog();
  1022. touch_nmi_watchdog();
  1023. }
  1024. return -EBUSY;
  1025. }
  1026. EXPORT_SYMBOL_GPL(ide_wait_not_busy);