ide-iops.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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 (struct hd_driveid *id)
  338. {
  339. #ifndef __LITTLE_ENDIAN
  340. # ifdef __BIG_ENDIAN
  341. int i;
  342. u16 *stringcast;
  343. id->config = __le16_to_cpu(id->config);
  344. id->cyls = __le16_to_cpu(id->cyls);
  345. id->reserved2 = __le16_to_cpu(id->reserved2);
  346. id->heads = __le16_to_cpu(id->heads);
  347. id->track_bytes = __le16_to_cpu(id->track_bytes);
  348. id->sector_bytes = __le16_to_cpu(id->sector_bytes);
  349. id->sectors = __le16_to_cpu(id->sectors);
  350. id->vendor0 = __le16_to_cpu(id->vendor0);
  351. id->vendor1 = __le16_to_cpu(id->vendor1);
  352. id->vendor2 = __le16_to_cpu(id->vendor2);
  353. stringcast = (u16 *)&id->serial_no[0];
  354. for (i = 0; i < (20/2); i++)
  355. stringcast[i] = __le16_to_cpu(stringcast[i]);
  356. id->buf_type = __le16_to_cpu(id->buf_type);
  357. id->buf_size = __le16_to_cpu(id->buf_size);
  358. id->ecc_bytes = __le16_to_cpu(id->ecc_bytes);
  359. stringcast = (u16 *)&id->fw_rev[0];
  360. for (i = 0; i < (8/2); i++)
  361. stringcast[i] = __le16_to_cpu(stringcast[i]);
  362. stringcast = (u16 *)&id->model[0];
  363. for (i = 0; i < (40/2); i++)
  364. stringcast[i] = __le16_to_cpu(stringcast[i]);
  365. id->dword_io = __le16_to_cpu(id->dword_io);
  366. id->reserved50 = __le16_to_cpu(id->reserved50);
  367. id->field_valid = __le16_to_cpu(id->field_valid);
  368. id->cur_cyls = __le16_to_cpu(id->cur_cyls);
  369. id->cur_heads = __le16_to_cpu(id->cur_heads);
  370. id->cur_sectors = __le16_to_cpu(id->cur_sectors);
  371. id->cur_capacity0 = __le16_to_cpu(id->cur_capacity0);
  372. id->cur_capacity1 = __le16_to_cpu(id->cur_capacity1);
  373. id->lba_capacity = __le32_to_cpu(id->lba_capacity);
  374. id->dma_1word = __le16_to_cpu(id->dma_1word);
  375. id->dma_mword = __le16_to_cpu(id->dma_mword);
  376. id->eide_pio_modes = __le16_to_cpu(id->eide_pio_modes);
  377. id->eide_dma_min = __le16_to_cpu(id->eide_dma_min);
  378. id->eide_dma_time = __le16_to_cpu(id->eide_dma_time);
  379. id->eide_pio = __le16_to_cpu(id->eide_pio);
  380. id->eide_pio_iordy = __le16_to_cpu(id->eide_pio_iordy);
  381. for (i = 0; i < 2; ++i)
  382. id->words69_70[i] = __le16_to_cpu(id->words69_70[i]);
  383. for (i = 0; i < 4; ++i)
  384. id->words71_74[i] = __le16_to_cpu(id->words71_74[i]);
  385. id->queue_depth = __le16_to_cpu(id->queue_depth);
  386. for (i = 0; i < 4; ++i)
  387. id->words76_79[i] = __le16_to_cpu(id->words76_79[i]);
  388. id->major_rev_num = __le16_to_cpu(id->major_rev_num);
  389. id->minor_rev_num = __le16_to_cpu(id->minor_rev_num);
  390. id->command_set_1 = __le16_to_cpu(id->command_set_1);
  391. id->command_set_2 = __le16_to_cpu(id->command_set_2);
  392. id->cfsse = __le16_to_cpu(id->cfsse);
  393. id->cfs_enable_1 = __le16_to_cpu(id->cfs_enable_1);
  394. id->cfs_enable_2 = __le16_to_cpu(id->cfs_enable_2);
  395. id->csf_default = __le16_to_cpu(id->csf_default);
  396. id->dma_ultra = __le16_to_cpu(id->dma_ultra);
  397. id->trseuc = __le16_to_cpu(id->trseuc);
  398. id->trsEuc = __le16_to_cpu(id->trsEuc);
  399. id->CurAPMvalues = __le16_to_cpu(id->CurAPMvalues);
  400. id->mprc = __le16_to_cpu(id->mprc);
  401. id->hw_config = __le16_to_cpu(id->hw_config);
  402. id->acoustic = __le16_to_cpu(id->acoustic);
  403. id->msrqs = __le16_to_cpu(id->msrqs);
  404. id->sxfert = __le16_to_cpu(id->sxfert);
  405. id->sal = __le16_to_cpu(id->sal);
  406. id->spg = __le32_to_cpu(id->spg);
  407. id->lba_capacity_2 = __le64_to_cpu(id->lba_capacity_2);
  408. for (i = 0; i < 22; i++)
  409. id->words104_125[i] = __le16_to_cpu(id->words104_125[i]);
  410. id->last_lun = __le16_to_cpu(id->last_lun);
  411. id->word127 = __le16_to_cpu(id->word127);
  412. id->dlf = __le16_to_cpu(id->dlf);
  413. id->csfo = __le16_to_cpu(id->csfo);
  414. for (i = 0; i < 26; i++)
  415. id->words130_155[i] = __le16_to_cpu(id->words130_155[i]);
  416. id->word156 = __le16_to_cpu(id->word156);
  417. for (i = 0; i < 3; i++)
  418. id->words157_159[i] = __le16_to_cpu(id->words157_159[i]);
  419. id->cfa_power = __le16_to_cpu(id->cfa_power);
  420. for (i = 0; i < 14; i++)
  421. id->words161_175[i] = __le16_to_cpu(id->words161_175[i]);
  422. for (i = 0; i < 31; i++)
  423. id->words176_205[i] = __le16_to_cpu(id->words176_205[i]);
  424. for (i = 0; i < 48; i++)
  425. id->words206_254[i] = __le16_to_cpu(id->words206_254[i]);
  426. id->integrity_word = __le16_to_cpu(id->integrity_word);
  427. # else
  428. # error "Please fix <asm/byteorder.h>"
  429. # endif
  430. #endif
  431. }
  432. /*
  433. * ide_fixstring() cleans up and (optionally) byte-swaps a text string,
  434. * removing leading/trailing blanks and compressing internal blanks.
  435. * It is primarily used to tidy up the model name/number fields as
  436. * returned by the WIN_[P]IDENTIFY commands.
  437. */
  438. void ide_fixstring (u8 *s, const int bytecount, const int byteswap)
  439. {
  440. u8 *p = s, *end = &s[bytecount & ~1]; /* bytecount must be even */
  441. if (byteswap) {
  442. /* convert from big-endian to host byte order */
  443. for (p = end ; p != s;) {
  444. unsigned short *pp = (unsigned short *) (p -= 2);
  445. *pp = ntohs(*pp);
  446. }
  447. }
  448. /* strip leading blanks */
  449. while (s != end && *s == ' ')
  450. ++s;
  451. /* compress internal blanks and strip trailing blanks */
  452. while (s != end && *s) {
  453. if (*s++ != ' ' || (s != end && *s && *s != ' '))
  454. *p++ = *(s-1);
  455. }
  456. /* wipe out trailing garbage */
  457. while (p != end)
  458. *p++ = '\0';
  459. }
  460. EXPORT_SYMBOL(ide_fixstring);
  461. /*
  462. * Needed for PCI irq sharing
  463. */
  464. int drive_is_ready (ide_drive_t *drive)
  465. {
  466. ide_hwif_t *hwif = HWIF(drive);
  467. u8 stat = 0;
  468. if (drive->waiting_for_dma)
  469. return hwif->dma_ops->dma_test_irq(drive);
  470. #if 0
  471. /* need to guarantee 400ns since last command was issued */
  472. udelay(1);
  473. #endif
  474. /*
  475. * We do a passive status test under shared PCI interrupts on
  476. * cards that truly share the ATA side interrupt, but may also share
  477. * an interrupt with another pci card/device. We make no assumptions
  478. * about possible isa-pnp and pci-pnp issues yet.
  479. */
  480. if (hwif->io_ports.ctl_addr)
  481. stat = hwif->tp_ops->read_altstatus(hwif);
  482. else
  483. /* Note: this may clear a pending IRQ!! */
  484. stat = hwif->tp_ops->read_status(hwif);
  485. if (stat & BUSY_STAT)
  486. /* drive busy: definitely not interrupting */
  487. return 0;
  488. /* drive ready: *might* be interrupting */
  489. return 1;
  490. }
  491. EXPORT_SYMBOL(drive_is_ready);
  492. /*
  493. * This routine busy-waits for the drive status to be not "busy".
  494. * It then checks the status for all of the "good" bits and none
  495. * of the "bad" bits, and if all is okay it returns 0. All other
  496. * cases return error -- caller may then invoke ide_error().
  497. *
  498. * This routine should get fixed to not hog the cpu during extra long waits..
  499. * That could be done by busy-waiting for the first jiffy or two, and then
  500. * setting a timer to wake up at half second intervals thereafter,
  501. * until timeout is achieved, before timing out.
  502. */
  503. static int __ide_wait_stat(ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout, u8 *rstat)
  504. {
  505. ide_hwif_t *hwif = drive->hwif;
  506. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  507. unsigned long flags;
  508. int i;
  509. u8 stat;
  510. udelay(1); /* spec allows drive 400ns to assert "BUSY" */
  511. stat = tp_ops->read_status(hwif);
  512. if (stat & BUSY_STAT) {
  513. local_irq_set(flags);
  514. timeout += jiffies;
  515. while ((stat = tp_ops->read_status(hwif)) & BUSY_STAT) {
  516. if (time_after(jiffies, timeout)) {
  517. /*
  518. * One last read after the timeout in case
  519. * heavy interrupt load made us not make any
  520. * progress during the timeout..
  521. */
  522. stat = tp_ops->read_status(hwif);
  523. if (!(stat & BUSY_STAT))
  524. break;
  525. local_irq_restore(flags);
  526. *rstat = stat;
  527. return -EBUSY;
  528. }
  529. }
  530. local_irq_restore(flags);
  531. }
  532. /*
  533. * Allow status to settle, then read it again.
  534. * A few rare drives vastly violate the 400ns spec here,
  535. * so we'll wait up to 10usec for a "good" status
  536. * rather than expensively fail things immediately.
  537. * This fix courtesy of Matthew Faupel & Niccolo Rigacci.
  538. */
  539. for (i = 0; i < 10; i++) {
  540. udelay(1);
  541. stat = tp_ops->read_status(hwif);
  542. if (OK_STAT(stat, good, bad)) {
  543. *rstat = stat;
  544. return 0;
  545. }
  546. }
  547. *rstat = stat;
  548. return -EFAULT;
  549. }
  550. /*
  551. * In case of error returns error value after doing "*startstop = ide_error()".
  552. * The caller should return the updated value of "startstop" in this case,
  553. * "startstop" is unchanged when the function returns 0.
  554. */
  555. int ide_wait_stat(ide_startstop_t *startstop, ide_drive_t *drive, u8 good, u8 bad, unsigned long timeout)
  556. {
  557. int err;
  558. u8 stat;
  559. /* bail early if we've exceeded max_failures */
  560. if (drive->max_failures && (drive->failures > drive->max_failures)) {
  561. *startstop = ide_stopped;
  562. return 1;
  563. }
  564. err = __ide_wait_stat(drive, good, bad, timeout, &stat);
  565. if (err) {
  566. char *s = (err == -EBUSY) ? "status timeout" : "status error";
  567. *startstop = ide_error(drive, s, stat);
  568. }
  569. return err;
  570. }
  571. EXPORT_SYMBOL(ide_wait_stat);
  572. /**
  573. * ide_in_drive_list - look for drive in black/white list
  574. * @id: drive identifier
  575. * @drive_table: list to inspect
  576. *
  577. * Look for a drive in the blacklist and the whitelist tables
  578. * Returns 1 if the drive is found in the table.
  579. */
  580. int ide_in_drive_list(struct hd_driveid *id, const struct drive_list_entry *drive_table)
  581. {
  582. for ( ; drive_table->id_model; drive_table++)
  583. if ((!strcmp(drive_table->id_model, id->model)) &&
  584. (!drive_table->id_firmware ||
  585. strstr(id->fw_rev, drive_table->id_firmware)))
  586. return 1;
  587. return 0;
  588. }
  589. EXPORT_SYMBOL_GPL(ide_in_drive_list);
  590. /*
  591. * Early UDMA66 devices don't set bit14 to 1, only bit13 is valid.
  592. * We list them here and depend on the device side cable detection for them.
  593. *
  594. * Some optical devices with the buggy firmwares have the same problem.
  595. */
  596. static const struct drive_list_entry ivb_list[] = {
  597. { "QUANTUM FIREBALLlct10 05" , "A03.0900" },
  598. { "TSSTcorp CDDVDW SH-S202J" , "SB00" },
  599. { "TSSTcorp CDDVDW SH-S202J" , "SB01" },
  600. { "TSSTcorp CDDVDW SH-S202N" , "SB00" },
  601. { "TSSTcorp CDDVDW SH-S202N" , "SB01" },
  602. { "TSSTcorp CDDVDW SH-S202H" , "SB00" },
  603. { "TSSTcorp CDDVDW SH-S202H" , "SB01" },
  604. { NULL , NULL }
  605. };
  606. /*
  607. * All hosts that use the 80c ribbon must use!
  608. * The name is derived from upper byte of word 93 and the 80c ribbon.
  609. */
  610. u8 eighty_ninty_three (ide_drive_t *drive)
  611. {
  612. ide_hwif_t *hwif = drive->hwif;
  613. struct hd_driveid *id = drive->id;
  614. int ivb = ide_in_drive_list(id, ivb_list);
  615. if (hwif->cbl == ATA_CBL_PATA40_SHORT)
  616. return 1;
  617. if (ivb)
  618. printk(KERN_DEBUG "%s: skipping word 93 validity check\n",
  619. drive->name);
  620. if (ide_dev_is_sata(id) && !ivb)
  621. return 1;
  622. if (hwif->cbl != ATA_CBL_PATA80 && !ivb)
  623. goto no_80w;
  624. /*
  625. * FIXME:
  626. * - change master/slave IDENTIFY order
  627. * - force bit13 (80c cable present) check also for !ivb devices
  628. * (unless the slave device is pre-ATA3)
  629. */
  630. if ((id->hw_config & 0x4000) || (ivb && (id->hw_config & 0x2000)))
  631. return 1;
  632. no_80w:
  633. if (drive->udma33_warned == 1)
  634. return 0;
  635. printk(KERN_WARNING "%s: %s side 80-wire cable detection failed, "
  636. "limiting max speed to UDMA33\n",
  637. drive->name,
  638. hwif->cbl == ATA_CBL_PATA80 ? "drive" : "host");
  639. drive->udma33_warned = 1;
  640. return 0;
  641. }
  642. int ide_driveid_update(ide_drive_t *drive)
  643. {
  644. ide_hwif_t *hwif = drive->hwif;
  645. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  646. struct hd_driveid *id;
  647. unsigned long timeout, flags;
  648. u8 stat;
  649. /*
  650. * Re-read drive->id for possible DMA mode
  651. * change (copied from ide-probe.c)
  652. */
  653. SELECT_MASK(drive, 1);
  654. tp_ops->set_irq(hwif, 0);
  655. msleep(50);
  656. tp_ops->exec_command(hwif, WIN_IDENTIFY);
  657. timeout = jiffies + WAIT_WORSTCASE;
  658. do {
  659. if (time_after(jiffies, timeout)) {
  660. SELECT_MASK(drive, 0);
  661. return 0; /* drive timed-out */
  662. }
  663. msleep(50); /* give drive a breather */
  664. stat = tp_ops->read_altstatus(hwif);
  665. } while (stat & BUSY_STAT);
  666. msleep(50); /* wait for IRQ and DRQ_STAT */
  667. stat = tp_ops->read_status(hwif);
  668. if (!OK_STAT(stat, DRQ_STAT, BAD_R_STAT)) {
  669. SELECT_MASK(drive, 0);
  670. printk("%s: CHECK for good STATUS\n", drive->name);
  671. return 0;
  672. }
  673. local_irq_save(flags);
  674. SELECT_MASK(drive, 0);
  675. id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
  676. if (!id) {
  677. local_irq_restore(flags);
  678. return 0;
  679. }
  680. tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
  681. (void)tp_ops->read_status(hwif); /* clear drive IRQ */
  682. local_irq_enable();
  683. local_irq_restore(flags);
  684. ide_fix_driveid(id);
  685. if (id) {
  686. drive->id->dma_ultra = id->dma_ultra;
  687. drive->id->dma_mword = id->dma_mword;
  688. drive->id->dma_1word = id->dma_1word;
  689. /* anything more ? */
  690. kfree(id);
  691. if (drive->using_dma && ide_id_dma_bug(drive))
  692. ide_dma_off(drive);
  693. }
  694. return 1;
  695. }
  696. int ide_config_drive_speed(ide_drive_t *drive, u8 speed)
  697. {
  698. ide_hwif_t *hwif = drive->hwif;
  699. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  700. int error = 0;
  701. u8 stat;
  702. ide_task_t task;
  703. #ifdef CONFIG_BLK_DEV_IDEDMA
  704. if (hwif->dma_ops) /* check if host supports DMA */
  705. hwif->dma_ops->dma_host_set(drive, 0);
  706. #endif
  707. /* Skip setting PIO flow-control modes on pre-EIDE drives */
  708. if ((speed & 0xf8) == XFER_PIO_0 && !(drive->id->capability & 0x08))
  709. goto skip;
  710. /*
  711. * Don't use ide_wait_cmd here - it will
  712. * attempt to set_geometry and recalibrate,
  713. * but for some reason these don't work at
  714. * this point (lost interrupt).
  715. */
  716. /*
  717. * Select the drive, and issue the SETFEATURES command
  718. */
  719. disable_irq_nosync(hwif->irq);
  720. /*
  721. * FIXME: we race against the running IRQ here if
  722. * this is called from non IRQ context. If we use
  723. * disable_irq() we hang on the error path. Work
  724. * is needed.
  725. */
  726. udelay(1);
  727. SELECT_DRIVE(drive);
  728. SELECT_MASK(drive, 0);
  729. udelay(1);
  730. tp_ops->set_irq(hwif, 0);
  731. memset(&task, 0, sizeof(task));
  732. task.tf_flags = IDE_TFLAG_OUT_FEATURE | IDE_TFLAG_OUT_NSECT;
  733. task.tf.feature = SETFEATURES_XFER;
  734. task.tf.nsect = speed;
  735. tp_ops->tf_load(drive, &task);
  736. tp_ops->exec_command(hwif, WIN_SETFEATURES);
  737. if (drive->quirk_list == 2)
  738. tp_ops->set_irq(hwif, 1);
  739. error = __ide_wait_stat(drive, drive->ready_stat,
  740. BUSY_STAT|DRQ_STAT|ERR_STAT,
  741. WAIT_CMD, &stat);
  742. SELECT_MASK(drive, 0);
  743. enable_irq(hwif->irq);
  744. if (error) {
  745. (void) ide_dump_status(drive, "set_drive_speed_status", stat);
  746. return error;
  747. }
  748. drive->id->dma_ultra &= ~0xFF00;
  749. drive->id->dma_mword &= ~0x0F00;
  750. drive->id->dma_1word &= ~0x0F00;
  751. skip:
  752. #ifdef CONFIG_BLK_DEV_IDEDMA
  753. if (speed >= XFER_SW_DMA_0 && drive->using_dma)
  754. hwif->dma_ops->dma_host_set(drive, 1);
  755. else if (hwif->dma_ops) /* check if host supports DMA */
  756. ide_dma_off_quietly(drive);
  757. #endif
  758. switch(speed) {
  759. case XFER_UDMA_7: drive->id->dma_ultra |= 0x8080; break;
  760. case XFER_UDMA_6: drive->id->dma_ultra |= 0x4040; break;
  761. case XFER_UDMA_5: drive->id->dma_ultra |= 0x2020; break;
  762. case XFER_UDMA_4: drive->id->dma_ultra |= 0x1010; break;
  763. case XFER_UDMA_3: drive->id->dma_ultra |= 0x0808; break;
  764. case XFER_UDMA_2: drive->id->dma_ultra |= 0x0404; break;
  765. case XFER_UDMA_1: drive->id->dma_ultra |= 0x0202; break;
  766. case XFER_UDMA_0: drive->id->dma_ultra |= 0x0101; break;
  767. case XFER_MW_DMA_2: drive->id->dma_mword |= 0x0404; break;
  768. case XFER_MW_DMA_1: drive->id->dma_mword |= 0x0202; break;
  769. case XFER_MW_DMA_0: drive->id->dma_mword |= 0x0101; break;
  770. case XFER_SW_DMA_2: drive->id->dma_1word |= 0x0404; break;
  771. case XFER_SW_DMA_1: drive->id->dma_1word |= 0x0202; break;
  772. case XFER_SW_DMA_0: drive->id->dma_1word |= 0x0101; break;
  773. default: break;
  774. }
  775. if (!drive->init_speed)
  776. drive->init_speed = speed;
  777. drive->current_speed = speed;
  778. return error;
  779. }
  780. /*
  781. * This should get invoked any time we exit the driver to
  782. * wait for an interrupt response from a drive. handler() points
  783. * at the appropriate code to handle the next interrupt, and a
  784. * timer is started to prevent us from waiting forever in case
  785. * something goes wrong (see the ide_timer_expiry() handler later on).
  786. *
  787. * See also ide_execute_command
  788. */
  789. static void __ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
  790. unsigned int timeout, ide_expiry_t *expiry)
  791. {
  792. ide_hwgroup_t *hwgroup = HWGROUP(drive);
  793. BUG_ON(hwgroup->handler);
  794. hwgroup->handler = handler;
  795. hwgroup->expiry = expiry;
  796. hwgroup->timer.expires = jiffies + timeout;
  797. hwgroup->req_gen_timer = hwgroup->req_gen;
  798. add_timer(&hwgroup->timer);
  799. }
  800. void ide_set_handler (ide_drive_t *drive, ide_handler_t *handler,
  801. unsigned int timeout, ide_expiry_t *expiry)
  802. {
  803. unsigned long flags;
  804. spin_lock_irqsave(&ide_lock, flags);
  805. __ide_set_handler(drive, handler, timeout, expiry);
  806. spin_unlock_irqrestore(&ide_lock, flags);
  807. }
  808. EXPORT_SYMBOL(ide_set_handler);
  809. /**
  810. * ide_execute_command - execute an IDE command
  811. * @drive: IDE drive to issue the command against
  812. * @command: command byte to write
  813. * @handler: handler for next phase
  814. * @timeout: timeout for command
  815. * @expiry: handler to run on timeout
  816. *
  817. * Helper function to issue an IDE command. This handles the
  818. * atomicity requirements, command timing and ensures that the
  819. * handler and IRQ setup do not race. All IDE command kick off
  820. * should go via this function or do equivalent locking.
  821. */
  822. void ide_execute_command(ide_drive_t *drive, u8 cmd, ide_handler_t *handler,
  823. unsigned timeout, ide_expiry_t *expiry)
  824. {
  825. unsigned long flags;
  826. ide_hwif_t *hwif = HWIF(drive);
  827. spin_lock_irqsave(&ide_lock, flags);
  828. __ide_set_handler(drive, handler, timeout, expiry);
  829. hwif->tp_ops->exec_command(hwif, cmd);
  830. /*
  831. * Drive takes 400nS to respond, we must avoid the IRQ being
  832. * serviced before that.
  833. *
  834. * FIXME: we could skip this delay with care on non shared devices
  835. */
  836. ndelay(400);
  837. spin_unlock_irqrestore(&ide_lock, flags);
  838. }
  839. EXPORT_SYMBOL(ide_execute_command);
  840. void ide_execute_pkt_cmd(ide_drive_t *drive)
  841. {
  842. ide_hwif_t *hwif = drive->hwif;
  843. unsigned long flags;
  844. spin_lock_irqsave(&ide_lock, flags);
  845. hwif->tp_ops->exec_command(hwif, WIN_PACKETCMD);
  846. ndelay(400);
  847. spin_unlock_irqrestore(&ide_lock, flags);
  848. }
  849. EXPORT_SYMBOL_GPL(ide_execute_pkt_cmd);
  850. static inline void ide_complete_drive_reset(ide_drive_t *drive, int err)
  851. {
  852. struct request *rq = drive->hwif->hwgroup->rq;
  853. if (rq && blk_special_request(rq) && rq->cmd[0] == REQ_DRIVE_RESET)
  854. ide_end_request(drive, err ? err : 1, 0);
  855. }
  856. /* needed below */
  857. static ide_startstop_t do_reset1 (ide_drive_t *, int);
  858. /*
  859. * atapi_reset_pollfunc() gets invoked to poll the interface for completion every 50ms
  860. * during an atapi drive reset operation. If the drive has not yet responded,
  861. * and we have not yet hit our maximum waiting time, then the timer is restarted
  862. * for another 50ms.
  863. */
  864. static ide_startstop_t atapi_reset_pollfunc (ide_drive_t *drive)
  865. {
  866. ide_hwif_t *hwif = drive->hwif;
  867. ide_hwgroup_t *hwgroup = hwif->hwgroup;
  868. u8 stat;
  869. SELECT_DRIVE(drive);
  870. udelay (10);
  871. stat = hwif->tp_ops->read_status(hwif);
  872. if (OK_STAT(stat, 0, BUSY_STAT))
  873. printk("%s: ATAPI reset complete\n", drive->name);
  874. else {
  875. if (time_before(jiffies, hwgroup->poll_timeout)) {
  876. ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
  877. /* continue polling */
  878. return ide_started;
  879. }
  880. /* end of polling */
  881. hwgroup->polling = 0;
  882. printk("%s: ATAPI reset timed-out, status=0x%02x\n",
  883. drive->name, stat);
  884. /* do it the old fashioned way */
  885. return do_reset1(drive, 1);
  886. }
  887. /* done polling */
  888. hwgroup->polling = 0;
  889. ide_complete_drive_reset(drive, 0);
  890. return ide_stopped;
  891. }
  892. /*
  893. * reset_pollfunc() gets invoked to poll the interface for completion every 50ms
  894. * during an ide reset operation. If the drives have not yet responded,
  895. * and we have not yet hit our maximum waiting time, then the timer is restarted
  896. * for another 50ms.
  897. */
  898. static ide_startstop_t reset_pollfunc (ide_drive_t *drive)
  899. {
  900. ide_hwgroup_t *hwgroup = HWGROUP(drive);
  901. ide_hwif_t *hwif = HWIF(drive);
  902. const struct ide_port_ops *port_ops = hwif->port_ops;
  903. u8 tmp;
  904. int err = 0;
  905. if (port_ops && port_ops->reset_poll) {
  906. err = port_ops->reset_poll(drive);
  907. if (err) {
  908. printk(KERN_ERR "%s: host reset_poll failure for %s.\n",
  909. hwif->name, drive->name);
  910. goto out;
  911. }
  912. }
  913. tmp = hwif->tp_ops->read_status(hwif);
  914. if (!OK_STAT(tmp, 0, BUSY_STAT)) {
  915. if (time_before(jiffies, hwgroup->poll_timeout)) {
  916. ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
  917. /* continue polling */
  918. return ide_started;
  919. }
  920. printk("%s: reset timed-out, status=0x%02x\n", hwif->name, tmp);
  921. drive->failures++;
  922. err = -EIO;
  923. } else {
  924. printk("%s: reset: ", hwif->name);
  925. tmp = ide_read_error(drive);
  926. if (tmp == 1) {
  927. printk("success\n");
  928. drive->failures = 0;
  929. } else {
  930. drive->failures++;
  931. printk("master: ");
  932. switch (tmp & 0x7f) {
  933. case 1: printk("passed");
  934. break;
  935. case 2: printk("formatter device error");
  936. break;
  937. case 3: printk("sector buffer error");
  938. break;
  939. case 4: printk("ECC circuitry error");
  940. break;
  941. case 5: printk("controlling MPU error");
  942. break;
  943. default:printk("error (0x%02x?)", tmp);
  944. }
  945. if (tmp & 0x80)
  946. printk("; slave: failed");
  947. printk("\n");
  948. err = -EIO;
  949. }
  950. }
  951. out:
  952. hwgroup->polling = 0; /* done polling */
  953. ide_complete_drive_reset(drive, err);
  954. return ide_stopped;
  955. }
  956. static void ide_disk_pre_reset(ide_drive_t *drive)
  957. {
  958. int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
  959. drive->special.all = 0;
  960. drive->special.b.set_geometry = legacy;
  961. drive->special.b.recalibrate = legacy;
  962. drive->mult_count = 0;
  963. if (!drive->keep_settings && !drive->using_dma)
  964. drive->mult_req = 0;
  965. if (drive->mult_req != drive->mult_count)
  966. drive->special.b.set_multmode = 1;
  967. }
  968. static void pre_reset(ide_drive_t *drive)
  969. {
  970. const struct ide_port_ops *port_ops = drive->hwif->port_ops;
  971. if (drive->media == ide_disk)
  972. ide_disk_pre_reset(drive);
  973. else
  974. drive->post_reset = 1;
  975. if (drive->using_dma) {
  976. if (drive->crc_count)
  977. ide_check_dma_crc(drive);
  978. else
  979. ide_dma_off(drive);
  980. }
  981. if (!drive->keep_settings) {
  982. if (!drive->using_dma) {
  983. drive->unmask = 0;
  984. drive->io_32bit = 0;
  985. }
  986. return;
  987. }
  988. if (port_ops && port_ops->pre_reset)
  989. port_ops->pre_reset(drive);
  990. if (drive->current_speed != 0xff)
  991. drive->desired_speed = drive->current_speed;
  992. drive->current_speed = 0xff;
  993. }
  994. /*
  995. * do_reset1() attempts to recover a confused drive by resetting it.
  996. * Unfortunately, resetting a disk drive actually resets all devices on
  997. * the same interface, so it can really be thought of as resetting the
  998. * interface rather than resetting the drive.
  999. *
  1000. * ATAPI devices have their own reset mechanism which allows them to be
  1001. * individually reset without clobbering other devices on the same interface.
  1002. *
  1003. * Unfortunately, the IDE interface does not generate an interrupt to let
  1004. * us know when the reset operation has finished, so we must poll for this.
  1005. * Equally poor, though, is the fact that this may a very long time to complete,
  1006. * (up to 30 seconds worstcase). So, instead of busy-waiting here for it,
  1007. * we set a timer to poll at 50ms intervals.
  1008. */
  1009. static ide_startstop_t do_reset1 (ide_drive_t *drive, int do_not_try_atapi)
  1010. {
  1011. unsigned int unit;
  1012. unsigned long flags;
  1013. ide_hwif_t *hwif;
  1014. ide_hwgroup_t *hwgroup;
  1015. struct ide_io_ports *io_ports;
  1016. const struct ide_tp_ops *tp_ops;
  1017. const struct ide_port_ops *port_ops;
  1018. spin_lock_irqsave(&ide_lock, flags);
  1019. hwif = HWIF(drive);
  1020. hwgroup = HWGROUP(drive);
  1021. io_ports = &hwif->io_ports;
  1022. tp_ops = hwif->tp_ops;
  1023. /* We must not reset with running handlers */
  1024. BUG_ON(hwgroup->handler != NULL);
  1025. /* For an ATAPI device, first try an ATAPI SRST. */
  1026. if (drive->media != ide_disk && !do_not_try_atapi) {
  1027. pre_reset(drive);
  1028. SELECT_DRIVE(drive);
  1029. udelay (20);
  1030. tp_ops->exec_command(hwif, WIN_SRST);
  1031. ndelay(400);
  1032. hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
  1033. hwgroup->polling = 1;
  1034. __ide_set_handler(drive, &atapi_reset_pollfunc, HZ/20, NULL);
  1035. spin_unlock_irqrestore(&ide_lock, flags);
  1036. return ide_started;
  1037. }
  1038. /*
  1039. * First, reset any device state data we were maintaining
  1040. * for any of the drives on this interface.
  1041. */
  1042. for (unit = 0; unit < MAX_DRIVES; ++unit)
  1043. pre_reset(&hwif->drives[unit]);
  1044. if (io_ports->ctl_addr == 0) {
  1045. spin_unlock_irqrestore(&ide_lock, flags);
  1046. ide_complete_drive_reset(drive, -ENXIO);
  1047. return ide_stopped;
  1048. }
  1049. /*
  1050. * Note that we also set nIEN while resetting the device,
  1051. * to mask unwanted interrupts from the interface during the reset.
  1052. * However, due to the design of PC hardware, this will cause an
  1053. * immediate interrupt due to the edge transition it produces.
  1054. * This single interrupt gives us a "fast poll" for drives that
  1055. * recover from reset very quickly, saving us the first 50ms wait time.
  1056. *
  1057. * TODO: add ->softreset method and stop abusing ->set_irq
  1058. */
  1059. /* set SRST and nIEN */
  1060. tp_ops->set_irq(hwif, 4);
  1061. /* more than enough time */
  1062. udelay(10);
  1063. /* clear SRST, leave nIEN (unless device is on the quirk list) */
  1064. tp_ops->set_irq(hwif, drive->quirk_list == 2);
  1065. /* more than enough time */
  1066. udelay(10);
  1067. hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
  1068. hwgroup->polling = 1;
  1069. __ide_set_handler(drive, &reset_pollfunc, HZ/20, NULL);
  1070. /*
  1071. * Some weird controller like resetting themselves to a strange
  1072. * state when the disks are reset this way. At least, the Winbond
  1073. * 553 documentation says that
  1074. */
  1075. port_ops = hwif->port_ops;
  1076. if (port_ops && port_ops->resetproc)
  1077. port_ops->resetproc(drive);
  1078. spin_unlock_irqrestore(&ide_lock, flags);
  1079. return ide_started;
  1080. }
  1081. /*
  1082. * ide_do_reset() is the entry point to the drive/interface reset code.
  1083. */
  1084. ide_startstop_t ide_do_reset (ide_drive_t *drive)
  1085. {
  1086. return do_reset1(drive, 0);
  1087. }
  1088. EXPORT_SYMBOL(ide_do_reset);
  1089. /*
  1090. * ide_wait_not_busy() waits for the currently selected device on the hwif
  1091. * to report a non-busy status, see comments in ide_probe_port().
  1092. */
  1093. int ide_wait_not_busy(ide_hwif_t *hwif, unsigned long timeout)
  1094. {
  1095. u8 stat = 0;
  1096. while(timeout--) {
  1097. /*
  1098. * Turn this into a schedule() sleep once I'm sure
  1099. * about locking issues (2.5 work ?).
  1100. */
  1101. mdelay(1);
  1102. stat = hwif->tp_ops->read_status(hwif);
  1103. if ((stat & BUSY_STAT) == 0)
  1104. return 0;
  1105. /*
  1106. * Assume a value of 0xff means nothing is connected to
  1107. * the interface and it doesn't implement the pull-down
  1108. * resistor on D7.
  1109. */
  1110. if (stat == 0xff)
  1111. return -ENODEV;
  1112. touch_softlockup_watchdog();
  1113. touch_nmi_watchdog();
  1114. }
  1115. return -EBUSY;
  1116. }
  1117. EXPORT_SYMBOL_GPL(ide_wait_not_busy);