ide-iops.c 32 KB

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