ide-iops.c 31 KB

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