ide-iops.c 35 KB

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