ide-iops.c 33 KB

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