Blame view

drivers/ide/ide-xfer-mode.c 6.66 KB
7eeaaaa52   Bartlomiej Zolnierkiewicz   ide: move xfer mo...
1
2
3
  #include <linux/types.h>
  #include <linux/string.h>
  #include <linux/kernel.h>
38789fda2   Paul Gortmaker   ide/ata: Add expo...
4
  #include <linux/export.h>
7eeaaaa52   Bartlomiej Zolnierkiewicz   ide: move xfer mo...
5
6
7
8
9
10
11
12
  #include <linux/interrupt.h>
  #include <linux/ide.h>
  #include <linux/bitops.h>
  
  static const char *udma_str[] =
  	 { "UDMA/16", "UDMA/25",  "UDMA/33",  "UDMA/44",
  	   "UDMA/66", "UDMA/100", "UDMA/133", "UDMA7" };
  static const char *mwdma_str[] =
74638c848   Sergei Shtylyov   ide: add support ...
13
  	{ "MWDMA0", "MWDMA1", "MWDMA2", "MWDMA3", "MWDMA4" };
7eeaaaa52   Bartlomiej Zolnierkiewicz   ide: move xfer mo...
14
15
16
  static const char *swdma_str[] =
  	{ "SWDMA0", "SWDMA1", "SWDMA2" };
  static const char *pio_str[] =
74638c848   Sergei Shtylyov   ide: add support ...
17
  	{ "PIO0", "PIO1", "PIO2", "PIO3", "PIO4", "PIO5", "PIO6" };
7eeaaaa52   Bartlomiej Zolnierkiewicz   ide: move xfer mo...
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  
  /**
   *	ide_xfer_verbose	-	return IDE mode names
   *	@mode: transfer mode
   *
   *	Returns a constant string giving the name of the mode
   *	requested.
   */
  
  const char *ide_xfer_verbose(u8 mode)
  {
  	const char *s;
  	u8 i = mode & 0xf;
  
  	if (mode >= XFER_UDMA_0 && mode <= XFER_UDMA_7)
  		s = udma_str[i];
74638c848   Sergei Shtylyov   ide: add support ...
34
  	else if (mode >= XFER_MW_DMA_0 && mode <= XFER_MW_DMA_4)
7eeaaaa52   Bartlomiej Zolnierkiewicz   ide: move xfer mo...
35
36
37
  		s = mwdma_str[i];
  	else if (mode >= XFER_SW_DMA_0 && mode <= XFER_SW_DMA_2)
  		s = swdma_str[i];
74638c848   Sergei Shtylyov   ide: add support ...
38
  	else if (mode >= XFER_PIO_0 && mode <= XFER_PIO_6)
7eeaaaa52   Bartlomiej Zolnierkiewicz   ide: move xfer mo...
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  		s = pio_str[i & 0x7];
  	else if (mode == XFER_PIO_SLOW)
  		s = "PIO SLOW";
  	else
  		s = "XFER ERROR";
  
  	return s;
  }
  EXPORT_SYMBOL(ide_xfer_verbose);
  
  /**
   *	ide_get_best_pio_mode	-	get PIO mode from drive
   *	@drive: drive to consider
   *	@mode_wanted: preferred mode
   *	@max_mode: highest allowed mode
   *
   *	This routine returns the recommended PIO settings for a given drive,
   *	based on the drive->id information and the ide_pio_blacklist[].
   *
   *	Drive PIO mode is auto-selected if 255 is passed as mode_wanted.
   *	This is used by most chipset support modules when "auto-tuning".
   */
220c58bc6   Bartlomiej Zolnierkiewicz   ide: make ide_get...
61
  static u8 ide_get_best_pio_mode(ide_drive_t *drive, u8 mode_wanted, u8 max_mode)
7eeaaaa52   Bartlomiej Zolnierkiewicz   ide: move xfer mo...
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  {
  	u16 *id = drive->id;
  	int pio_mode = -1, overridden = 0;
  
  	if (mode_wanted != 255)
  		return min_t(u8, mode_wanted, max_mode);
  
  	if ((drive->hwif->host_flags & IDE_HFLAG_PIO_NO_BLACKLIST) == 0)
  		pio_mode = ide_scan_pio_blacklist((char *)&id[ATA_ID_PROD]);
  
  	if (pio_mode != -1) {
  		printk(KERN_INFO "%s: is on PIO blacklist
  ", drive->name);
  	} else {
  		pio_mode = id[ATA_ID_OLD_PIO_MODES] >> 8;
  		if (pio_mode > 2) {	/* 2 is maximum allowed tPIO value */
  			pio_mode = 2;
  			overridden = 1;
  		}
  
  		if (id[ATA_ID_FIELD_VALID] & 2) {	      /* ATA2? */
74638c848   Sergei Shtylyov   ide: add support ...
83
84
85
86
  			if (ata_id_is_cfa(id) && (id[ATA_ID_CFA_MODES] & 7))
  				pio_mode = 4 + min_t(int, 2,
  						     id[ATA_ID_CFA_MODES] & 7);
  			else if (ata_id_has_iordy(id)) {
7eeaaaa52   Bartlomiej Zolnierkiewicz   ide: move xfer mo...
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  				if (id[ATA_ID_PIO_MODES] & 7) {
  					overridden = 0;
  					if (id[ATA_ID_PIO_MODES] & 4)
  						pio_mode = 5;
  					else if (id[ATA_ID_PIO_MODES] & 2)
  						pio_mode = 4;
  					else
  						pio_mode = 3;
  				}
  			}
  		}
  
  		if (overridden)
  			printk(KERN_INFO "%s: tPIO > 2, assuming tPIO = 2
  ",
  					 drive->name);
  	}
  
  	if (pio_mode > max_mode)
  		pio_mode = max_mode;
  
  	return pio_mode;
  }
7eeaaaa52   Bartlomiej Zolnierkiewicz   ide: move xfer mo...
110

c9ef59ff0   Bartlomiej Zolnierkiewicz   ide: IORDY handli...
111
112
  int ide_pio_need_iordy(ide_drive_t *drive, const u8 pio)
  {
5880b5de7   Bartlomiej Zolnierkiewicz   ide: don't enable...
113
114
115
116
117
118
  	/*
  	 * IORDY may lead to controller lock up on certain controllers
  	 * if the port is not occupied.
  	 */
  	if (pio == 0 && (drive->hwif->port_flags & IDE_PFLAG_PROBING))
  		return 0;
c9ef59ff0   Bartlomiej Zolnierkiewicz   ide: IORDY handli...
119
120
121
  	return ata_id_pio_need_iordy(drive->id, pio);
  }
  EXPORT_SYMBOL_GPL(ide_pio_need_iordy);
7eeaaaa52   Bartlomiej Zolnierkiewicz   ide: move xfer mo...
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  int ide_set_pio_mode(ide_drive_t *drive, const u8 mode)
  {
  	ide_hwif_t *hwif = drive->hwif;
  	const struct ide_port_ops *port_ops = hwif->port_ops;
  
  	if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
  		return 0;
  
  	if (port_ops == NULL || port_ops->set_pio_mode == NULL)
  		return -1;
  
  	/*
  	 * TODO: temporary hack for some legacy host drivers that didn't
  	 * set transfer mode on the device in ->set_pio_mode method...
  	 */
  	if (port_ops->set_dma_mode == NULL) {
d2d4e780a   Bartlomiej Zolnierkiewicz   ide: add drive->p...
138
  		drive->pio_mode = mode;
e085b3cae   Bartlomiej Zolnierkiewicz   ide: change ->set...
139
  		port_ops->set_pio_mode(hwif, drive);
7eeaaaa52   Bartlomiej Zolnierkiewicz   ide: move xfer mo...
140
141
142
143
144
145
  		return 0;
  	}
  
  	if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
  		if (ide_config_drive_speed(drive, mode))
  			return -1;
d2d4e780a   Bartlomiej Zolnierkiewicz   ide: add drive->p...
146
  		drive->pio_mode = mode;
e085b3cae   Bartlomiej Zolnierkiewicz   ide: change ->set...
147
  		port_ops->set_pio_mode(hwif, drive);
7eeaaaa52   Bartlomiej Zolnierkiewicz   ide: move xfer mo...
148
149
  		return 0;
  	} else {
d2d4e780a   Bartlomiej Zolnierkiewicz   ide: add drive->p...
150
  		drive->pio_mode = mode;
e085b3cae   Bartlomiej Zolnierkiewicz   ide: change ->set...
151
  		port_ops->set_pio_mode(hwif, drive);
7eeaaaa52   Bartlomiej Zolnierkiewicz   ide: move xfer mo...
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
  		return ide_config_drive_speed(drive, mode);
  	}
  }
  
  int ide_set_dma_mode(ide_drive_t *drive, const u8 mode)
  {
  	ide_hwif_t *hwif = drive->hwif;
  	const struct ide_port_ops *port_ops = hwif->port_ops;
  
  	if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)
  		return 0;
  
  	if (port_ops == NULL || port_ops->set_dma_mode == NULL)
  		return -1;
  
  	if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) {
  		if (ide_config_drive_speed(drive, mode))
  			return -1;
3fccaa192   Bartlomiej Zolnierkiewicz   ide: add drive->d...
170
  		drive->dma_mode = mode;
8776168ca   Bartlomiej Zolnierkiewicz   ide: change ->set...
171
  		port_ops->set_dma_mode(hwif, drive);
7eeaaaa52   Bartlomiej Zolnierkiewicz   ide: move xfer mo...
172
173
  		return 0;
  	} else {
3fccaa192   Bartlomiej Zolnierkiewicz   ide: add drive->d...
174
  		drive->dma_mode = mode;
8776168ca   Bartlomiej Zolnierkiewicz   ide: change ->set...
175
  		port_ops->set_dma_mode(hwif, drive);
7eeaaaa52   Bartlomiej Zolnierkiewicz   ide: move xfer mo...
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
  		return ide_config_drive_speed(drive, mode);
  	}
  }
  EXPORT_SYMBOL_GPL(ide_set_dma_mode);
  
  /* req_pio == "255" for auto-tune */
  void ide_set_pio(ide_drive_t *drive, u8 req_pio)
  {
  	ide_hwif_t *hwif = drive->hwif;
  	const struct ide_port_ops *port_ops = hwif->port_ops;
  	u8 host_pio, pio;
  
  	if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
  	    (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
  		return;
  
  	BUG_ON(hwif->pio_mask == 0x00);
  
  	host_pio = fls(hwif->pio_mask) - 1;
  
  	pio = ide_get_best_pio_mode(drive, req_pio, host_pio);
  
  	/*
  	 * TODO:
  	 * - report device max PIO mode
  	 * - check req_pio != 255 against device max PIO mode
  	 */
  	printk(KERN_DEBUG "%s: host max PIO%d wanted PIO%d%s selected PIO%d
  ",
  			  drive->name, host_pio, req_pio,
  			  req_pio == 255 ? "(auto-tune)" : "", pio);
  
  	(void)ide_set_pio_mode(drive, XFER_PIO_0 + pio);
  }
  EXPORT_SYMBOL_GPL(ide_set_pio);
  
  /**
   *	ide_rate_filter		-	filter transfer mode
   *	@drive: IDE device
   *	@speed: desired speed
   *
   *	Given the available transfer modes this function returns
   *	the best available speed at or below the speed requested.
   *
   *	TODO: check device PIO capabilities
   */
  
  static u8 ide_rate_filter(ide_drive_t *drive, u8 speed)
  {
  	ide_hwif_t *hwif = drive->hwif;
  	u8 mode = ide_find_dma_mode(drive, speed);
  
  	if (mode == 0) {
  		if (hwif->pio_mask)
  			mode = fls(hwif->pio_mask) - 1 + XFER_PIO_0;
  		else
  			mode = XFER_PIO_4;
  	}
  
  /*	printk("%s: mode 0x%02x, speed 0x%02x
  ", __func__, mode, speed); */
  
  	return min(speed, mode);
  }
  
  /**
   *	ide_set_xfer_rate	-	set transfer rate
   *	@drive: drive to set
   *	@rate: speed to attempt to set
   *
   *	General helper for setting the speed of an IDE device. This
   *	function knows about user enforced limits from the configuration
   *	which ->set_pio_mode/->set_dma_mode does not.
   */
  
  int ide_set_xfer_rate(ide_drive_t *drive, u8 rate)
  {
  	ide_hwif_t *hwif = drive->hwif;
  	const struct ide_port_ops *port_ops = hwif->port_ops;
  
  	if (port_ops == NULL || port_ops->set_dma_mode == NULL ||
  	    (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
  		return -1;
  
  	rate = ide_rate_filter(drive, rate);
  
  	BUG_ON(rate < XFER_PIO_0);
74638c848   Sergei Shtylyov   ide: add support ...
263
  	if (rate >= XFER_PIO_0 && rate <= XFER_PIO_6)
7eeaaaa52   Bartlomiej Zolnierkiewicz   ide: move xfer mo...
264
265
266
267
  		return ide_set_pio_mode(drive, rate);
  
  	return ide_set_dma_mode(drive, rate);
  }