ide-iops.c 35 KB

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