ide-iops.c 33 KB

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