ide-iops.c 30 KB

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