ide-iops.c 31 KB

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