ide-iops.c 31 KB

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