ide-iops.c 31 KB

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