ide-iops.c 34 KB

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