aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi/libata-core.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/scsi/libata-core.c')
-rw-r--r--drivers/scsi/libata-core.c2794
1 files changed, 1354 insertions, 1440 deletions
diff --git a/drivers/scsi/libata-core.c b/drivers/scsi/libata-core.c
index 4f91b0dc572b..714b42bad935 100644
--- a/drivers/scsi/libata-core.c
+++ b/drivers/scsi/libata-core.c
@@ -61,24 +61,17 @@
61 61
62#include "libata.h" 62#include "libata.h"
63 63
64static unsigned int ata_busy_sleep (struct ata_port *ap, 64static unsigned int ata_dev_init_params(struct ata_port *ap,
65 unsigned long tmout_pat, 65 struct ata_device *dev);
66 unsigned long tmout);
67static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
68static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
69static void ata_set_mode(struct ata_port *ap); 66static void ata_set_mode(struct ata_port *ap);
70static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev); 67static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
71static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift); 68static unsigned int ata_dev_xfermask(struct ata_port *ap,
72static int fgb(u32 bitmap); 69 struct ata_device *dev);
73static int ata_choose_xfer_mode(const struct ata_port *ap,
74 u8 *xfer_mode_out,
75 unsigned int *xfer_shift_out);
76static void __ata_qc_complete(struct ata_queued_cmd *qc);
77 70
78static unsigned int ata_unique_id = 1; 71static unsigned int ata_unique_id = 1;
79static struct workqueue_struct *ata_wq; 72static struct workqueue_struct *ata_wq;
80 73
81int atapi_enabled = 0; 74int atapi_enabled = 1;
82module_param(atapi_enabled, int, 0444); 75module_param(atapi_enabled, int, 0444);
83MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)"); 76MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
84 77
@@ -91,403 +84,6 @@ MODULE_DESCRIPTION("Library module for ATA devices");
91MODULE_LICENSE("GPL"); 84MODULE_LICENSE("GPL");
92MODULE_VERSION(DRV_VERSION); 85MODULE_VERSION(DRV_VERSION);
93 86
94/**
95 * ata_tf_load_pio - send taskfile registers to host controller
96 * @ap: Port to which output is sent
97 * @tf: ATA taskfile register set
98 *
99 * Outputs ATA taskfile to standard ATA host controller.
100 *
101 * LOCKING:
102 * Inherited from caller.
103 */
104
105static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
106{
107 struct ata_ioports *ioaddr = &ap->ioaddr;
108 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
109
110 if (tf->ctl != ap->last_ctl) {
111 outb(tf->ctl, ioaddr->ctl_addr);
112 ap->last_ctl = tf->ctl;
113 ata_wait_idle(ap);
114 }
115
116 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
117 outb(tf->hob_feature, ioaddr->feature_addr);
118 outb(tf->hob_nsect, ioaddr->nsect_addr);
119 outb(tf->hob_lbal, ioaddr->lbal_addr);
120 outb(tf->hob_lbam, ioaddr->lbam_addr);
121 outb(tf->hob_lbah, ioaddr->lbah_addr);
122 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
123 tf->hob_feature,
124 tf->hob_nsect,
125 tf->hob_lbal,
126 tf->hob_lbam,
127 tf->hob_lbah);
128 }
129
130 if (is_addr) {
131 outb(tf->feature, ioaddr->feature_addr);
132 outb(tf->nsect, ioaddr->nsect_addr);
133 outb(tf->lbal, ioaddr->lbal_addr);
134 outb(tf->lbam, ioaddr->lbam_addr);
135 outb(tf->lbah, ioaddr->lbah_addr);
136 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
137 tf->feature,
138 tf->nsect,
139 tf->lbal,
140 tf->lbam,
141 tf->lbah);
142 }
143
144 if (tf->flags & ATA_TFLAG_DEVICE) {
145 outb(tf->device, ioaddr->device_addr);
146 VPRINTK("device 0x%X\n", tf->device);
147 }
148
149 ata_wait_idle(ap);
150}
151
152/**
153 * ata_tf_load_mmio - send taskfile registers to host controller
154 * @ap: Port to which output is sent
155 * @tf: ATA taskfile register set
156 *
157 * Outputs ATA taskfile to standard ATA host controller using MMIO.
158 *
159 * LOCKING:
160 * Inherited from caller.
161 */
162
163static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
164{
165 struct ata_ioports *ioaddr = &ap->ioaddr;
166 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
167
168 if (tf->ctl != ap->last_ctl) {
169 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
170 ap->last_ctl = tf->ctl;
171 ata_wait_idle(ap);
172 }
173
174 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
175 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
176 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
177 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
178 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
179 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
180 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
181 tf->hob_feature,
182 tf->hob_nsect,
183 tf->hob_lbal,
184 tf->hob_lbam,
185 tf->hob_lbah);
186 }
187
188 if (is_addr) {
189 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
190 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
191 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
192 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
193 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
194 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
195 tf->feature,
196 tf->nsect,
197 tf->lbal,
198 tf->lbam,
199 tf->lbah);
200 }
201
202 if (tf->flags & ATA_TFLAG_DEVICE) {
203 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
204 VPRINTK("device 0x%X\n", tf->device);
205 }
206
207 ata_wait_idle(ap);
208}
209
210
211/**
212 * ata_tf_load - send taskfile registers to host controller
213 * @ap: Port to which output is sent
214 * @tf: ATA taskfile register set
215 *
216 * Outputs ATA taskfile to standard ATA host controller using MMIO
217 * or PIO as indicated by the ATA_FLAG_MMIO flag.
218 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
219 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
220 * hob_lbal, hob_lbam, and hob_lbah.
221 *
222 * This function waits for idle (!BUSY and !DRQ) after writing
223 * registers. If the control register has a new value, this
224 * function also waits for idle after writing control and before
225 * writing the remaining registers.
226 *
227 * May be used as the tf_load() entry in ata_port_operations.
228 *
229 * LOCKING:
230 * Inherited from caller.
231 */
232void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
233{
234 if (ap->flags & ATA_FLAG_MMIO)
235 ata_tf_load_mmio(ap, tf);
236 else
237 ata_tf_load_pio(ap, tf);
238}
239
240/**
241 * ata_exec_command_pio - issue ATA command to host controller
242 * @ap: port to which command is being issued
243 * @tf: ATA taskfile register set
244 *
245 * Issues PIO write to ATA command register, with proper
246 * synchronization with interrupt handler / other threads.
247 *
248 * LOCKING:
249 * spin_lock_irqsave(host_set lock)
250 */
251
252static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
253{
254 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
255
256 outb(tf->command, ap->ioaddr.command_addr);
257 ata_pause(ap);
258}
259
260
261/**
262 * ata_exec_command_mmio - issue ATA command to host controller
263 * @ap: port to which command is being issued
264 * @tf: ATA taskfile register set
265 *
266 * Issues MMIO write to ATA command register, with proper
267 * synchronization with interrupt handler / other threads.
268 *
269 * LOCKING:
270 * spin_lock_irqsave(host_set lock)
271 */
272
273static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
274{
275 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
276
277 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
278 ata_pause(ap);
279}
280
281
282/**
283 * ata_exec_command - issue ATA command to host controller
284 * @ap: port to which command is being issued
285 * @tf: ATA taskfile register set
286 *
287 * Issues PIO/MMIO write to ATA command register, with proper
288 * synchronization with interrupt handler / other threads.
289 *
290 * LOCKING:
291 * spin_lock_irqsave(host_set lock)
292 */
293void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
294{
295 if (ap->flags & ATA_FLAG_MMIO)
296 ata_exec_command_mmio(ap, tf);
297 else
298 ata_exec_command_pio(ap, tf);
299}
300
301/**
302 * ata_tf_to_host - issue ATA taskfile to host controller
303 * @ap: port to which command is being issued
304 * @tf: ATA taskfile register set
305 *
306 * Issues ATA taskfile register set to ATA host controller,
307 * with proper synchronization with interrupt handler and
308 * other threads.
309 *
310 * LOCKING:
311 * spin_lock_irqsave(host_set lock)
312 */
313
314static inline void ata_tf_to_host(struct ata_port *ap,
315 const struct ata_taskfile *tf)
316{
317 ap->ops->tf_load(ap, tf);
318 ap->ops->exec_command(ap, tf);
319}
320
321/**
322 * ata_tf_read_pio - input device's ATA taskfile shadow registers
323 * @ap: Port from which input is read
324 * @tf: ATA taskfile register set for storing input
325 *
326 * Reads ATA taskfile registers for currently-selected device
327 * into @tf.
328 *
329 * LOCKING:
330 * Inherited from caller.
331 */
332
333static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
334{
335 struct ata_ioports *ioaddr = &ap->ioaddr;
336
337 tf->command = ata_check_status(ap);
338 tf->feature = inb(ioaddr->error_addr);
339 tf->nsect = inb(ioaddr->nsect_addr);
340 tf->lbal = inb(ioaddr->lbal_addr);
341 tf->lbam = inb(ioaddr->lbam_addr);
342 tf->lbah = inb(ioaddr->lbah_addr);
343 tf->device = inb(ioaddr->device_addr);
344
345 if (tf->flags & ATA_TFLAG_LBA48) {
346 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
347 tf->hob_feature = inb(ioaddr->error_addr);
348 tf->hob_nsect = inb(ioaddr->nsect_addr);
349 tf->hob_lbal = inb(ioaddr->lbal_addr);
350 tf->hob_lbam = inb(ioaddr->lbam_addr);
351 tf->hob_lbah = inb(ioaddr->lbah_addr);
352 }
353}
354
355/**
356 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
357 * @ap: Port from which input is read
358 * @tf: ATA taskfile register set for storing input
359 *
360 * Reads ATA taskfile registers for currently-selected device
361 * into @tf via MMIO.
362 *
363 * LOCKING:
364 * Inherited from caller.
365 */
366
367static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
368{
369 struct ata_ioports *ioaddr = &ap->ioaddr;
370
371 tf->command = ata_check_status(ap);
372 tf->feature = readb((void __iomem *)ioaddr->error_addr);
373 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
374 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
375 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
376 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
377 tf->device = readb((void __iomem *)ioaddr->device_addr);
378
379 if (tf->flags & ATA_TFLAG_LBA48) {
380 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
381 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
382 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
383 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
384 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
385 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
386 }
387}
388
389
390/**
391 * ata_tf_read - input device's ATA taskfile shadow registers
392 * @ap: Port from which input is read
393 * @tf: ATA taskfile register set for storing input
394 *
395 * Reads ATA taskfile registers for currently-selected device
396 * into @tf.
397 *
398 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
399 * is set, also reads the hob registers.
400 *
401 * May be used as the tf_read() entry in ata_port_operations.
402 *
403 * LOCKING:
404 * Inherited from caller.
405 */
406void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
407{
408 if (ap->flags & ATA_FLAG_MMIO)
409 ata_tf_read_mmio(ap, tf);
410 else
411 ata_tf_read_pio(ap, tf);
412}
413
414/**
415 * ata_check_status_pio - Read device status reg & clear interrupt
416 * @ap: port where the device is
417 *
418 * Reads ATA taskfile status register for currently-selected device
419 * and return its value. This also clears pending interrupts
420 * from this device
421 *
422 * LOCKING:
423 * Inherited from caller.
424 */
425static u8 ata_check_status_pio(struct ata_port *ap)
426{
427 return inb(ap->ioaddr.status_addr);
428}
429
430/**
431 * ata_check_status_mmio - Read device status reg & clear interrupt
432 * @ap: port where the device is
433 *
434 * Reads ATA taskfile status register for currently-selected device
435 * via MMIO and return its value. This also clears pending interrupts
436 * from this device
437 *
438 * LOCKING:
439 * Inherited from caller.
440 */
441static u8 ata_check_status_mmio(struct ata_port *ap)
442{
443 return readb((void __iomem *) ap->ioaddr.status_addr);
444}
445
446
447/**
448 * ata_check_status - Read device status reg & clear interrupt
449 * @ap: port where the device is
450 *
451 * Reads ATA taskfile status register for currently-selected device
452 * and return its value. This also clears pending interrupts
453 * from this device
454 *
455 * May be used as the check_status() entry in ata_port_operations.
456 *
457 * LOCKING:
458 * Inherited from caller.
459 */
460u8 ata_check_status(struct ata_port *ap)
461{
462 if (ap->flags & ATA_FLAG_MMIO)
463 return ata_check_status_mmio(ap);
464 return ata_check_status_pio(ap);
465}
466
467
468/**
469 * ata_altstatus - Read device alternate status reg
470 * @ap: port where the device is
471 *
472 * Reads ATA taskfile alternate status register for
473 * currently-selected device and return its value.
474 *
475 * Note: may NOT be used as the check_altstatus() entry in
476 * ata_port_operations.
477 *
478 * LOCKING:
479 * Inherited from caller.
480 */
481u8 ata_altstatus(struct ata_port *ap)
482{
483 if (ap->ops->check_altstatus)
484 return ap->ops->check_altstatus(ap);
485
486 if (ap->flags & ATA_FLAG_MMIO)
487 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
488 return inb(ap->ioaddr.altstatus_addr);
489}
490
491 87
492/** 88/**
493 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure 89 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
@@ -632,58 +228,148 @@ int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
632 return -1; 228 return -1;
633} 229}
634 230
635static const char * const xfer_mode_str[] = { 231/**
636 "UDMA/16", 232 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
637 "UDMA/25", 233 * @pio_mask: pio_mask
638 "UDMA/33", 234 * @mwdma_mask: mwdma_mask
639 "UDMA/44", 235 * @udma_mask: udma_mask
640 "UDMA/66", 236 *
641 "UDMA/100", 237 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
642 "UDMA/133", 238 * unsigned int xfer_mask.
643 "UDMA7", 239 *
644 "MWDMA0", 240 * LOCKING:
645 "MWDMA1", 241 * None.
646 "MWDMA2", 242 *
647 "PIO0", 243 * RETURNS:
648 "PIO1", 244 * Packed xfer_mask.
649 "PIO2", 245 */
650 "PIO3", 246static unsigned int ata_pack_xfermask(unsigned int pio_mask,
651 "PIO4", 247 unsigned int mwdma_mask,
248 unsigned int udma_mask)
249{
250 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
251 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
252 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
253}
254
255static const struct ata_xfer_ent {
256 unsigned int shift, bits;
257 u8 base;
258} ata_xfer_tbl[] = {
259 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
260 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
261 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
262 { -1, },
652}; 263};
653 264
654/** 265/**
655 * ata_udma_string - convert UDMA bit offset to string 266 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
656 * @mask: mask of bits supported; only highest bit counts. 267 * @xfer_mask: xfer_mask of interest
657 * 268 *
658 * Determine string which represents the highest speed 269 * Return matching XFER_* value for @xfer_mask. Only the highest
659 * (highest bit in @udma_mask). 270 * bit of @xfer_mask is considered.
660 * 271 *
661 * LOCKING: 272 * LOCKING:
662 * None. 273 * None.
663 * 274 *
664 * RETURNS: 275 * RETURNS:
665 * Constant C string representing highest speed listed in 276 * Matching XFER_* value, 0 if no match found.
666 * @udma_mask, or the constant C string "<n/a>".
667 */ 277 */
278static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
279{
280 int highbit = fls(xfer_mask) - 1;
281 const struct ata_xfer_ent *ent;
668 282
669static const char *ata_mode_string(unsigned int mask) 283 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
284 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
285 return ent->base + highbit - ent->shift;
286 return 0;
287}
288
289/**
290 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
291 * @xfer_mode: XFER_* of interest
292 *
293 * Return matching xfer_mask for @xfer_mode.
294 *
295 * LOCKING:
296 * None.
297 *
298 * RETURNS:
299 * Matching xfer_mask, 0 if no match found.
300 */
301static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
670{ 302{
671 int i; 303 const struct ata_xfer_ent *ent;
672 304
673 for (i = 7; i >= 0; i--) 305 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
674 if (mask & (1 << i)) 306 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
675 goto out; 307 return 1 << (ent->shift + xfer_mode - ent->base);
676 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--) 308 return 0;
677 if (mask & (1 << i)) 309}
678 goto out;
679 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
680 if (mask & (1 << i))
681 goto out;
682 310
683 return "<n/a>"; 311/**
312 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
313 * @xfer_mode: XFER_* of interest
314 *
315 * Return matching xfer_shift for @xfer_mode.
316 *
317 * LOCKING:
318 * None.
319 *
320 * RETURNS:
321 * Matching xfer_shift, -1 if no match found.
322 */
323static int ata_xfer_mode2shift(unsigned int xfer_mode)
324{
325 const struct ata_xfer_ent *ent;
684 326
685out: 327 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
686 return xfer_mode_str[i]; 328 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
329 return ent->shift;
330 return -1;
331}
332
333/**
334 * ata_mode_string - convert xfer_mask to string
335 * @xfer_mask: mask of bits supported; only highest bit counts.
336 *
337 * Determine string which represents the highest speed
338 * (highest bit in @modemask).
339 *
340 * LOCKING:
341 * None.
342 *
343 * RETURNS:
344 * Constant C string representing highest speed listed in
345 * @mode_mask, or the constant C string "<n/a>".
346 */
347static const char *ata_mode_string(unsigned int xfer_mask)
348{
349 static const char * const xfer_mode_str[] = {
350 "PIO0",
351 "PIO1",
352 "PIO2",
353 "PIO3",
354 "PIO4",
355 "MWDMA0",
356 "MWDMA1",
357 "MWDMA2",
358 "UDMA/16",
359 "UDMA/25",
360 "UDMA/33",
361 "UDMA/44",
362 "UDMA/66",
363 "UDMA/100",
364 "UDMA/133",
365 "UDMA7",
366 };
367 int highbit;
368
369 highbit = fls(xfer_mask) - 1;
370 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
371 return xfer_mode_str[highbit];
372 return "<n/a>";
687} 373}
688 374
689/** 375/**
@@ -838,6 +524,7 @@ unsigned int ata_dev_classify(const struct ata_taskfile *tf)
838 * ata_dev_try_classify - Parse returned ATA device signature 524 * ata_dev_try_classify - Parse returned ATA device signature
839 * @ap: ATA channel to examine 525 * @ap: ATA channel to examine
840 * @device: Device to examine (starting at zero) 526 * @device: Device to examine (starting at zero)
527 * @r_err: Value of error register on completion
841 * 528 *
842 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs, 529 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
843 * an ATA/ATAPI-defined set of values is placed in the ATA 530 * an ATA/ATAPI-defined set of values is placed in the ATA
@@ -850,11 +537,14 @@ unsigned int ata_dev_classify(const struct ata_taskfile *tf)
850 * 537 *
851 * LOCKING: 538 * LOCKING:
852 * caller. 539 * caller.
540 *
541 * RETURNS:
542 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
853 */ 543 */
854 544
855static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device) 545static unsigned int
546ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
856{ 547{
857 struct ata_device *dev = &ap->device[device];
858 struct ata_taskfile tf; 548 struct ata_taskfile tf;
859 unsigned int class; 549 unsigned int class;
860 u8 err; 550 u8 err;
@@ -865,8 +555,8 @@ static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
865 555
866 ap->ops->tf_read(ap, &tf); 556 ap->ops->tf_read(ap, &tf);
867 err = tf.feature; 557 err = tf.feature;
868 558 if (r_err)
869 dev->class = ATA_DEV_NONE; 559 *r_err = err;
870 560
871 /* see if device passed diags */ 561 /* see if device passed diags */
872 if (err == 1) 562 if (err == 1)
@@ -874,22 +564,20 @@ static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
874 else if ((device == 0) && (err == 0x81)) 564 else if ((device == 0) && (err == 0x81))
875 /* do nothing */ ; 565 /* do nothing */ ;
876 else 566 else
877 return err; 567 return ATA_DEV_NONE;
878 568
879 /* determine if device if ATA or ATAPI */ 569 /* determine if device is ATA or ATAPI */
880 class = ata_dev_classify(&tf); 570 class = ata_dev_classify(&tf);
571
881 if (class == ATA_DEV_UNKNOWN) 572 if (class == ATA_DEV_UNKNOWN)
882 return err; 573 return ATA_DEV_NONE;
883 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0)) 574 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
884 return err; 575 return ATA_DEV_NONE;
885 576 return class;
886 dev->class = class;
887
888 return err;
889} 577}
890 578
891/** 579/**
892 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string 580 * ata_id_string - Convert IDENTIFY DEVICE page into string
893 * @id: IDENTIFY DEVICE results we will examine 581 * @id: IDENTIFY DEVICE results we will examine
894 * @s: string into which data is output 582 * @s: string into which data is output
895 * @ofs: offset into identify device page 583 * @ofs: offset into identify device page
@@ -903,8 +591,8 @@ static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
903 * caller. 591 * caller.
904 */ 592 */
905 593
906void ata_dev_id_string(const u16 *id, unsigned char *s, 594void ata_id_string(const u16 *id, unsigned char *s,
907 unsigned int ofs, unsigned int len) 595 unsigned int ofs, unsigned int len)
908{ 596{
909 unsigned int c; 597 unsigned int c;
910 598
@@ -922,6 +610,49 @@ void ata_dev_id_string(const u16 *id, unsigned char *s,
922 } 610 }
923} 611}
924 612
613/**
614 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
615 * @id: IDENTIFY DEVICE results we will examine
616 * @s: string into which data is output
617 * @ofs: offset into identify device page
618 * @len: length of string to return. must be an odd number.
619 *
620 * This function is identical to ata_id_string except that it
621 * trims trailing spaces and terminates the resulting string with
622 * null. @len must be actual maximum length (even number) + 1.
623 *
624 * LOCKING:
625 * caller.
626 */
627void ata_id_c_string(const u16 *id, unsigned char *s,
628 unsigned int ofs, unsigned int len)
629{
630 unsigned char *p;
631
632 WARN_ON(!(len & 1));
633
634 ata_id_string(id, s, ofs, len - 1);
635
636 p = s + strnlen(s, len - 1);
637 while (p > s && p[-1] == ' ')
638 p--;
639 *p = '\0';
640}
641
642static u64 ata_id_n_sectors(const u16 *id)
643{
644 if (ata_id_has_lba(id)) {
645 if (ata_id_has_lba48(id))
646 return ata_id_u64(id, 100);
647 else
648 return ata_id_u32(id, 60);
649 } else {
650 if (ata_id_current_chs_valid(id))
651 return ata_id_u32(id, 57);
652 else
653 return id[1] * id[3] * id[6];
654 }
655}
925 656
926/** 657/**
927 * ata_noop_dev_select - Select device 0/1 on ATA bus 658 * ata_noop_dev_select - Select device 0/1 on ATA bus
@@ -1011,90 +742,172 @@ void ata_dev_select(struct ata_port *ap, unsigned int device,
1011 742
1012/** 743/**
1013 * ata_dump_id - IDENTIFY DEVICE info debugging output 744 * ata_dump_id - IDENTIFY DEVICE info debugging output
1014 * @dev: Device whose IDENTIFY DEVICE page we will dump 745 * @id: IDENTIFY DEVICE page to dump
1015 * 746 *
1016 * Dump selected 16-bit words from a detected device's 747 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1017 * IDENTIFY PAGE page. 748 * page.
1018 * 749 *
1019 * LOCKING: 750 * LOCKING:
1020 * caller. 751 * caller.
1021 */ 752 */
1022 753
1023static inline void ata_dump_id(const struct ata_device *dev) 754static inline void ata_dump_id(const u16 *id)
1024{ 755{
1025 DPRINTK("49==0x%04x " 756 DPRINTK("49==0x%04x "
1026 "53==0x%04x " 757 "53==0x%04x "
1027 "63==0x%04x " 758 "63==0x%04x "
1028 "64==0x%04x " 759 "64==0x%04x "
1029 "75==0x%04x \n", 760 "75==0x%04x \n",
1030 dev->id[49], 761 id[49],
1031 dev->id[53], 762 id[53],
1032 dev->id[63], 763 id[63],
1033 dev->id[64], 764 id[64],
1034 dev->id[75]); 765 id[75]);
1035 DPRINTK("80==0x%04x " 766 DPRINTK("80==0x%04x "
1036 "81==0x%04x " 767 "81==0x%04x "
1037 "82==0x%04x " 768 "82==0x%04x "
1038 "83==0x%04x " 769 "83==0x%04x "
1039 "84==0x%04x \n", 770 "84==0x%04x \n",
1040 dev->id[80], 771 id[80],
1041 dev->id[81], 772 id[81],
1042 dev->id[82], 773 id[82],
1043 dev->id[83], 774 id[83],
1044 dev->id[84]); 775 id[84]);
1045 DPRINTK("88==0x%04x " 776 DPRINTK("88==0x%04x "
1046 "93==0x%04x\n", 777 "93==0x%04x\n",
1047 dev->id[88], 778 id[88],
1048 dev->id[93]); 779 id[93]);
1049} 780}
1050 781
1051/* 782/**
1052 * Compute the PIO modes available for this device. This is not as 783 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1053 * trivial as it seems if we must consider early devices correctly. 784 * @id: IDENTIFY data to compute xfer mask from
785 *
786 * Compute the xfermask for this device. This is not as trivial
787 * as it seems if we must consider early devices correctly.
788 *
789 * FIXME: pre IDE drive timing (do we care ?).
790 *
791 * LOCKING:
792 * None.
1054 * 793 *
1055 * FIXME: pre IDE drive timing (do we care ?). 794 * RETURNS:
795 * Computed xfermask
1056 */ 796 */
1057 797static unsigned int ata_id_xfermask(const u16 *id)
1058static unsigned int ata_pio_modes(const struct ata_device *adev)
1059{ 798{
1060 u16 modes; 799 unsigned int pio_mask, mwdma_mask, udma_mask;
1061 800
1062 /* Usual case. Word 53 indicates word 64 is valid */ 801 /* Usual case. Word 53 indicates word 64 is valid */
1063 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) { 802 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1064 modes = adev->id[ATA_ID_PIO_MODES] & 0x03; 803 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1065 modes <<= 3; 804 pio_mask <<= 3;
1066 modes |= 0x7; 805 pio_mask |= 0x7;
1067 return modes; 806 } else {
807 /* If word 64 isn't valid then Word 51 high byte holds
808 * the PIO timing number for the maximum. Turn it into
809 * a mask.
810 */
811 pio_mask = (2 << (id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
812
813 /* But wait.. there's more. Design your standards by
814 * committee and you too can get a free iordy field to
815 * process. However its the speeds not the modes that
816 * are supported... Note drivers using the timing API
817 * will get this right anyway
818 */
1068 } 819 }
1069 820
1070 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing 821 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1071 number for the maximum. Turn it into a mask and return it */ 822
1072 modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ; 823 udma_mask = 0;
1073 return modes; 824 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1074 /* But wait.. there's more. Design your standards by committee and 825 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1075 you too can get a free iordy field to process. However its the 826
1076 speeds not the modes that are supported... Note drivers using the 827 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1077 timing API will get this right anyway */
1078} 828}
1079 829
1080struct ata_exec_internal_arg { 830/**
1081 unsigned int err_mask; 831 * ata_port_queue_task - Queue port_task
1082 struct ata_taskfile *tf; 832 * @ap: The ata_port to queue port_task for
1083 struct completion *waiting; 833 *
1084}; 834 * Schedule @fn(@data) for execution after @delay jiffies using
835 * port_task. There is one port_task per port and it's the
836 * user(low level driver)'s responsibility to make sure that only
837 * one task is active at any given time.
838 *
839 * libata core layer takes care of synchronization between
840 * port_task and EH. ata_port_queue_task() may be ignored for EH
841 * synchronization.
842 *
843 * LOCKING:
844 * Inherited from caller.
845 */
846void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
847 unsigned long delay)
848{
849 int rc;
850
851 if (ap->flags & ATA_FLAG_FLUSH_PORT_TASK)
852 return;
853
854 PREPARE_WORK(&ap->port_task, fn, data);
855
856 if (!delay)
857 rc = queue_work(ata_wq, &ap->port_task);
858 else
859 rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
860
861 /* rc == 0 means that another user is using port task */
862 WARN_ON(rc == 0);
863}
1085 864
1086int ata_qc_complete_internal(struct ata_queued_cmd *qc) 865/**
866 * ata_port_flush_task - Flush port_task
867 * @ap: The ata_port to flush port_task for
868 *
869 * After this function completes, port_task is guranteed not to
870 * be running or scheduled.
871 *
872 * LOCKING:
873 * Kernel thread context (may sleep)
874 */
875void ata_port_flush_task(struct ata_port *ap)
1087{ 876{
1088 struct ata_exec_internal_arg *arg = qc->private_data; 877 unsigned long flags;
1089 struct completion *waiting = arg->waiting;
1090 878
1091 if (!(qc->err_mask & ~AC_ERR_DEV)) 879 DPRINTK("ENTER\n");
1092 qc->ap->ops->tf_read(qc->ap, arg->tf);
1093 arg->err_mask = qc->err_mask;
1094 arg->waiting = NULL;
1095 complete(waiting);
1096 880
1097 return 0; 881 spin_lock_irqsave(&ap->host_set->lock, flags);
882 ap->flags |= ATA_FLAG_FLUSH_PORT_TASK;
883 spin_unlock_irqrestore(&ap->host_set->lock, flags);
884
885 DPRINTK("flush #1\n");
886 flush_workqueue(ata_wq);
887
888 /*
889 * At this point, if a task is running, it's guaranteed to see
890 * the FLUSH flag; thus, it will never queue pio tasks again.
891 * Cancel and flush.
892 */
893 if (!cancel_delayed_work(&ap->port_task)) {
894 DPRINTK("flush #2\n");
895 flush_workqueue(ata_wq);
896 }
897
898 spin_lock_irqsave(&ap->host_set->lock, flags);
899 ap->flags &= ~ATA_FLAG_FLUSH_PORT_TASK;
900 spin_unlock_irqrestore(&ap->host_set->lock, flags);
901
902 DPRINTK("EXIT\n");
903}
904
905void ata_qc_complete_internal(struct ata_queued_cmd *qc)
906{
907 struct completion *waiting = qc->private_data;
908
909 qc->ap->ops->tf_read(qc->ap, &qc->tf);
910 complete(waiting);
1098} 911}
1099 912
1100/** 913/**
@@ -1125,7 +938,7 @@ ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
1125 struct ata_queued_cmd *qc; 938 struct ata_queued_cmd *qc;
1126 DECLARE_COMPLETION(wait); 939 DECLARE_COMPLETION(wait);
1127 unsigned long flags; 940 unsigned long flags;
1128 struct ata_exec_internal_arg arg; 941 unsigned int err_mask;
1129 942
1130 spin_lock_irqsave(&ap->host_set->lock, flags); 943 spin_lock_irqsave(&ap->host_set->lock, flags);
1131 944
@@ -1139,13 +952,12 @@ ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
1139 qc->nsect = buflen / ATA_SECT_SIZE; 952 qc->nsect = buflen / ATA_SECT_SIZE;
1140 } 953 }
1141 954
1142 arg.waiting = &wait; 955 qc->private_data = &wait;
1143 arg.tf = tf;
1144 qc->private_data = &arg;
1145 qc->complete_fn = ata_qc_complete_internal; 956 qc->complete_fn = ata_qc_complete_internal;
1146 957
1147 if (ata_qc_issue(qc)) 958 qc->err_mask = ata_qc_issue(qc);
1148 goto issue_fail; 959 if (qc->err_mask)
960 ata_qc_complete(qc);
1149 961
1150 spin_unlock_irqrestore(&ap->host_set->lock, flags); 962 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1151 963
@@ -1158,8 +970,8 @@ ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
1158 * before the caller cleans up, it will result in a 970 * before the caller cleans up, it will result in a
1159 * spurious interrupt. We can live with that. 971 * spurious interrupt. We can live with that.
1160 */ 972 */
1161 if (arg.waiting) { 973 if (qc->flags & ATA_QCFLAG_ACTIVE) {
1162 qc->err_mask = AC_ERR_OTHER; 974 qc->err_mask = AC_ERR_TIMEOUT;
1163 ata_qc_complete(qc); 975 ata_qc_complete(qc);
1164 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n", 976 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1165 ap->id, command); 977 ap->id, command);
@@ -1168,12 +980,12 @@ ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
1168 spin_unlock_irqrestore(&ap->host_set->lock, flags); 980 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1169 } 981 }
1170 982
1171 return arg.err_mask; 983 *tf = qc->tf;
984 err_mask = qc->err_mask;
1172 985
1173 issue_fail:
1174 ata_qc_free(qc); 986 ata_qc_free(qc);
1175 spin_unlock_irqrestore(&ap->host_set->lock, flags); 987
1176 return AC_ERR_OTHER; 988 return err_mask;
1177} 989}
1178 990
1179/** 991/**
@@ -1210,73 +1022,78 @@ unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1210} 1022}
1211 1023
1212/** 1024/**
1213 * ata_dev_identify - obtain IDENTIFY x DEVICE page 1025 * ata_dev_read_id - Read ID data from the specified device
1214 * @ap: port on which device we wish to probe resides 1026 * @ap: port on which target device resides
1215 * @device: device bus address, starting at zero 1027 * @dev: target device
1216 * 1028 * @p_class: pointer to class of the target device (may be changed)
1217 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE 1029 * @post_reset: is this read ID post-reset?
1218 * command, and read back the 512-byte device information page. 1030 * @p_id: read IDENTIFY page (newly allocated)
1219 * The device information page is fed to us via the standard 1031 *
1220 * PIO-IN protocol, but we hand-code it here. (TODO: investigate 1032 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1221 * using standard PIO-IN paths) 1033 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1222 * 1034 * devices. This function also takes care of EDD signature
1223 * After reading the device information page, we use several 1035 * misreporting (to be removed once EDD support is gone) and
1224 * bits of information from it to initialize data structures 1036 * issues ATA_CMD_INIT_DEV_PARAMS for pre-ATA4 drives.
1225 * that will be used during the lifetime of the ata_device.
1226 * Other data from the info page is used to disqualify certain
1227 * older ATA devices we do not wish to support.
1228 * 1037 *
1229 * LOCKING: 1038 * LOCKING:
1230 * Inherited from caller. Some functions called by this function 1039 * Kernel thread context (may sleep)
1231 * obtain the host_set lock. 1040 *
1041 * RETURNS:
1042 * 0 on success, -errno otherwise.
1232 */ 1043 */
1233 1044static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
1234static void ata_dev_identify(struct ata_port *ap, unsigned int device) 1045 unsigned int *p_class, int post_reset, u16 **p_id)
1235{ 1046{
1236 struct ata_device *dev = &ap->device[device]; 1047 unsigned int class = *p_class;
1237 unsigned int major_version;
1238 u16 tmp;
1239 unsigned long xfer_modes;
1240 unsigned int using_edd; 1048 unsigned int using_edd;
1241 struct ata_taskfile tf; 1049 struct ata_taskfile tf;
1242 unsigned int err_mask; 1050 unsigned int err_mask = 0;
1051 u16 *id;
1052 const char *reason;
1243 int rc; 1053 int rc;
1244 1054
1245 if (!ata_dev_present(dev)) { 1055 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
1246 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1247 ap->id, device);
1248 return;
1249 }
1250 1056
1251 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET)) 1057 if (ap->ops->probe_reset ||
1058 ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1252 using_edd = 0; 1059 using_edd = 0;
1253 else 1060 else
1254 using_edd = 1; 1061 using_edd = 1;
1255 1062
1256 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device); 1063 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
1257
1258 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1259 dev->class == ATA_DEV_NONE);
1260 1064
1261 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */ 1065 id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL);
1066 if (id == NULL) {
1067 rc = -ENOMEM;
1068 reason = "out of memory";
1069 goto err_out;
1070 }
1262 1071
1263retry: 1072 retry:
1264 ata_tf_init(ap, &tf, device); 1073 ata_tf_init(ap, &tf, dev->devno);
1265 1074
1266 if (dev->class == ATA_DEV_ATA) { 1075 switch (class) {
1076 case ATA_DEV_ATA:
1267 tf.command = ATA_CMD_ID_ATA; 1077 tf.command = ATA_CMD_ID_ATA;
1268 DPRINTK("do ATA identify\n"); 1078 break;
1269 } else { 1079 case ATA_DEV_ATAPI:
1270 tf.command = ATA_CMD_ID_ATAPI; 1080 tf.command = ATA_CMD_ID_ATAPI;
1271 DPRINTK("do ATAPI identify\n"); 1081 break;
1082 default:
1083 rc = -ENODEV;
1084 reason = "unsupported class";
1085 goto err_out;
1272 } 1086 }
1273 1087
1274 tf.protocol = ATA_PROT_PIO; 1088 tf.protocol = ATA_PROT_PIO;
1275 1089
1276 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE, 1090 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1277 dev->id, sizeof(dev->id)); 1091 id, sizeof(id[0]) * ATA_ID_WORDS);
1278 1092
1279 if (err_mask) { 1093 if (err_mask) {
1094 rc = -EIO;
1095 reason = "I/O error";
1096
1280 if (err_mask & ~AC_ERR_DEV) 1097 if (err_mask & ~AC_ERR_DEV)
1281 goto err_out; 1098 goto err_out;
1282 1099
@@ -1291,180 +1108,223 @@ retry:
1291 * ATA software reset (SRST, the default) does not appear 1108 * ATA software reset (SRST, the default) does not appear
1292 * to have this problem. 1109 * to have this problem.
1293 */ 1110 */
1294 if ((using_edd) && (dev->class == ATA_DEV_ATA)) { 1111 if ((using_edd) && (class == ATA_DEV_ATA)) {
1295 u8 err = tf.feature; 1112 u8 err = tf.feature;
1296 if (err & ATA_ABORTED) { 1113 if (err & ATA_ABORTED) {
1297 dev->class = ATA_DEV_ATAPI; 1114 class = ATA_DEV_ATAPI;
1298 goto retry; 1115 goto retry;
1299 } 1116 }
1300 } 1117 }
1301 goto err_out; 1118 goto err_out;
1302 } 1119 }
1303 1120
1304 swap_buf_le16(dev->id, ATA_ID_WORDS); 1121 swap_buf_le16(id, ATA_ID_WORDS);
1122
1123 /* sanity check */
1124 if ((class == ATA_DEV_ATA) != ata_id_is_ata(id)) {
1125 rc = -EINVAL;
1126 reason = "device reports illegal type";
1127 goto err_out;
1128 }
1129
1130 if (post_reset && class == ATA_DEV_ATA) {
1131 /*
1132 * The exact sequence expected by certain pre-ATA4 drives is:
1133 * SRST RESET
1134 * IDENTIFY
1135 * INITIALIZE DEVICE PARAMETERS
1136 * anything else..
1137 * Some drives were very specific about that exact sequence.
1138 */
1139 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1140 err_mask = ata_dev_init_params(ap, dev);
1141 if (err_mask) {
1142 rc = -EIO;
1143 reason = "INIT_DEV_PARAMS failed";
1144 goto err_out;
1145 }
1146
1147 /* current CHS translation info (id[53-58]) might be
1148 * changed. reread the identify device info.
1149 */
1150 post_reset = 0;
1151 goto retry;
1152 }
1153 }
1154
1155 *p_class = class;
1156 *p_id = id;
1157 return 0;
1158
1159 err_out:
1160 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1161 ap->id, dev->devno, reason);
1162 kfree(id);
1163 return rc;
1164}
1165
1166static inline u8 ata_dev_knobble(const struct ata_port *ap,
1167 struct ata_device *dev)
1168{
1169 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1170}
1171
1172/**
1173 * ata_dev_configure - Configure the specified ATA/ATAPI device
1174 * @ap: Port on which target device resides
1175 * @dev: Target device to configure
1176 * @print_info: Enable device info printout
1177 *
1178 * Configure @dev according to @dev->id. Generic and low-level
1179 * driver specific fixups are also applied.
1180 *
1181 * LOCKING:
1182 * Kernel thread context (may sleep)
1183 *
1184 * RETURNS:
1185 * 0 on success, -errno otherwise
1186 */
1187static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev,
1188 int print_info)
1189{
1190 const u16 *id = dev->id;
1191 unsigned int xfer_mask;
1192 int i, rc;
1193
1194 if (!ata_dev_present(dev)) {
1195 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1196 ap->id, dev->devno);
1197 return 0;
1198 }
1199
1200 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
1305 1201
1306 /* print device capabilities */ 1202 /* print device capabilities */
1307 printk(KERN_DEBUG "ata%u: dev %u cfg " 1203 if (print_info)
1308 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n", 1204 printk(KERN_DEBUG "ata%u: dev %u cfg 49:%04x 82:%04x 83:%04x "
1309 ap->id, device, dev->id[49], 1205 "84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1310 dev->id[82], dev->id[83], dev->id[84], 1206 ap->id, dev->devno, id[49], id[82], id[83],
1311 dev->id[85], dev->id[86], dev->id[87], 1207 id[84], id[85], id[86], id[87], id[88]);
1312 dev->id[88]); 1208
1209 /* initialize to-be-configured parameters */
1210 dev->flags = 0;
1211 dev->max_sectors = 0;
1212 dev->cdb_len = 0;
1213 dev->n_sectors = 0;
1214 dev->cylinders = 0;
1215 dev->heads = 0;
1216 dev->sectors = 0;
1313 1217
1314 /* 1218 /*
1315 * common ATA, ATAPI feature tests 1219 * common ATA, ATAPI feature tests
1316 */ 1220 */
1317 1221
1318 /* we require DMA support (bits 8 of word 49) */ 1222 /* we require DMA support (bits 8 of word 49) */
1319 if (!ata_id_has_dma(dev->id)) { 1223 if (!ata_id_has_dma(id)) {
1320 printk(KERN_DEBUG "ata%u: no dma\n", ap->id); 1224 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1225 rc = -EINVAL;
1321 goto err_out_nosup; 1226 goto err_out_nosup;
1322 } 1227 }
1323 1228
1324 /* quick-n-dirty find max transfer mode; for printk only */ 1229 /* find max transfer mode; for printk only */
1325 xfer_modes = dev->id[ATA_ID_UDMA_MODES]; 1230 xfer_mask = ata_id_xfermask(id);
1326 if (!xfer_modes)
1327 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
1328 if (!xfer_modes)
1329 xfer_modes = ata_pio_modes(dev);
1330 1231
1331 ata_dump_id(dev); 1232 ata_dump_id(id);
1332 1233
1333 /* ATA-specific feature tests */ 1234 /* ATA-specific feature tests */
1334 if (dev->class == ATA_DEV_ATA) { 1235 if (dev->class == ATA_DEV_ATA) {
1335 if (!ata_id_is_ata(dev->id)) /* sanity check */ 1236 dev->n_sectors = ata_id_n_sectors(id);
1336 goto err_out_nosup;
1337 1237
1338 /* get major version */ 1238 if (ata_id_has_lba(id)) {
1339 tmp = dev->id[ATA_ID_MAJOR_VER]; 1239 const char *lba_desc;
1340 for (major_version = 14; major_version >= 1; major_version--)
1341 if (tmp & (1 << major_version))
1342 break;
1343 1240
1344 /* 1241 lba_desc = "LBA";
1345 * The exact sequence expected by certain pre-ATA4 drives is:
1346 * SRST RESET
1347 * IDENTIFY
1348 * INITIALIZE DEVICE PARAMETERS
1349 * anything else..
1350 * Some drives were very specific about that exact sequence.
1351 */
1352 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
1353 ata_dev_init_params(ap, dev);
1354
1355 /* current CHS translation info (id[53-58]) might be
1356 * changed. reread the identify device info.
1357 */
1358 ata_dev_reread_id(ap, dev);
1359 }
1360
1361 if (ata_id_has_lba(dev->id)) {
1362 dev->flags |= ATA_DFLAG_LBA; 1242 dev->flags |= ATA_DFLAG_LBA;
1363 1243 if (ata_id_has_lba48(id)) {
1364 if (ata_id_has_lba48(dev->id)) {
1365 dev->flags |= ATA_DFLAG_LBA48; 1244 dev->flags |= ATA_DFLAG_LBA48;
1366 dev->n_sectors = ata_id_u64(dev->id, 100); 1245 lba_desc = "LBA48";
1367 } else {
1368 dev->n_sectors = ata_id_u32(dev->id, 60);
1369 } 1246 }
1370 1247
1371 /* print device info to dmesg */ 1248 /* print device info to dmesg */
1372 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n", 1249 if (print_info)
1373 ap->id, device, 1250 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1374 major_version, 1251 "max %s, %Lu sectors: %s\n",
1375 ata_mode_string(xfer_modes), 1252 ap->id, dev->devno,
1376 (unsigned long long)dev->n_sectors, 1253 ata_id_major_version(id),
1377 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA"); 1254 ata_mode_string(xfer_mask),
1378 } else { 1255 (unsigned long long)dev->n_sectors,
1256 lba_desc);
1257 } else {
1379 /* CHS */ 1258 /* CHS */
1380 1259
1381 /* Default translation */ 1260 /* Default translation */
1382 dev->cylinders = dev->id[1]; 1261 dev->cylinders = id[1];
1383 dev->heads = dev->id[3]; 1262 dev->heads = id[3];
1384 dev->sectors = dev->id[6]; 1263 dev->sectors = id[6];
1385 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1386 1264
1387 if (ata_id_current_chs_valid(dev->id)) { 1265 if (ata_id_current_chs_valid(id)) {
1388 /* Current CHS translation is valid. */ 1266 /* Current CHS translation is valid. */
1389 dev->cylinders = dev->id[54]; 1267 dev->cylinders = id[54];
1390 dev->heads = dev->id[55]; 1268 dev->heads = id[55];
1391 dev->sectors = dev->id[56]; 1269 dev->sectors = id[56];
1392
1393 dev->n_sectors = ata_id_u32(dev->id, 57);
1394 } 1270 }
1395 1271
1396 /* print device info to dmesg */ 1272 /* print device info to dmesg */
1397 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n", 1273 if (print_info)
1398 ap->id, device, 1274 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1399 major_version, 1275 "max %s, %Lu sectors: CHS %u/%u/%u\n",
1400 ata_mode_string(xfer_modes), 1276 ap->id, dev->devno,
1401 (unsigned long long)dev->n_sectors, 1277 ata_id_major_version(id),
1402 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors); 1278 ata_mode_string(xfer_mask),
1403 1279 (unsigned long long)dev->n_sectors,
1280 dev->cylinders, dev->heads, dev->sectors);
1404 } 1281 }
1405 1282
1406 ap->host->max_cmd_len = 16; 1283 dev->cdb_len = 16;
1407 } 1284 }
1408 1285
1409 /* ATAPI-specific feature tests */ 1286 /* ATAPI-specific feature tests */
1410 else if (dev->class == ATA_DEV_ATAPI) { 1287 else if (dev->class == ATA_DEV_ATAPI) {
1411 if (ata_id_is_ata(dev->id)) /* sanity check */ 1288 rc = atapi_cdb_len(id);
1412 goto err_out_nosup;
1413
1414 rc = atapi_cdb_len(dev->id);
1415 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) { 1289 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1416 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id); 1290 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1291 rc = -EINVAL;
1417 goto err_out_nosup; 1292 goto err_out_nosup;
1418 } 1293 }
1419 ap->cdb_len = (unsigned int) rc; 1294 dev->cdb_len = (unsigned int) rc;
1420 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1421 1295
1422 /* print device info to dmesg */ 1296 /* print device info to dmesg */
1423 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n", 1297 if (print_info)
1424 ap->id, device, 1298 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1425 ata_mode_string(xfer_modes)); 1299 ap->id, dev->devno, ata_mode_string(xfer_mask));
1426 } 1300 }
1427 1301
1428 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap)); 1302 ap->host->max_cmd_len = 0;
1429 return; 1303 for (i = 0; i < ATA_MAX_DEVICES; i++)
1430 1304 ap->host->max_cmd_len = max_t(unsigned int,
1431err_out_nosup: 1305 ap->host->max_cmd_len,
1432 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n", 1306 ap->device[i].cdb_len);
1433 ap->id, device);
1434err_out:
1435 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1436 DPRINTK("EXIT, err\n");
1437}
1438
1439
1440static inline u8 ata_dev_knobble(const struct ata_port *ap)
1441{
1442 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1443}
1444
1445/**
1446 * ata_dev_config - Run device specific handlers and check for
1447 * SATA->PATA bridges
1448 * @ap: Bus
1449 * @i: Device
1450 *
1451 * LOCKING:
1452 */
1453 1307
1454void ata_dev_config(struct ata_port *ap, unsigned int i)
1455{
1456 /* limit bridge transfers to udma5, 200 sectors */ 1308 /* limit bridge transfers to udma5, 200 sectors */
1457 if (ata_dev_knobble(ap)) { 1309 if (ata_dev_knobble(ap, dev)) {
1458 printk(KERN_INFO "ata%u(%u): applying bridge limits\n", 1310 if (print_info)
1459 ap->id, ap->device->devno); 1311 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1312 ap->id, dev->devno);
1460 ap->udma_mask &= ATA_UDMA5; 1313 ap->udma_mask &= ATA_UDMA5;
1461 ap->host->max_sectors = ATA_MAX_SECTORS; 1314 dev->max_sectors = ATA_MAX_SECTORS;
1462 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1463 ap->device[i].flags |= ATA_DFLAG_LOCK_SECTORS;
1464 } 1315 }
1465 1316
1466 if (ap->ops->dev_config) 1317 if (ap->ops->dev_config)
1467 ap->ops->dev_config(ap, &ap->device[i]); 1318 ap->ops->dev_config(ap, dev);
1319
1320 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1321 return 0;
1322
1323err_out_nosup:
1324 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1325 ap->id, dev->devno);
1326 DPRINTK("EXIT, err\n");
1327 return rc;
1468} 1328}
1469 1329
1470/** 1330/**
@@ -1484,21 +1344,59 @@ void ata_dev_config(struct ata_port *ap, unsigned int i)
1484 1344
1485static int ata_bus_probe(struct ata_port *ap) 1345static int ata_bus_probe(struct ata_port *ap)
1486{ 1346{
1487 unsigned int i, found = 0; 1347 unsigned int classes[ATA_MAX_DEVICES];
1348 unsigned int i, rc, found = 0;
1488 1349
1489 ap->ops->phy_reset(ap); 1350 ata_port_probe(ap);
1490 if (ap->flags & ATA_FLAG_PORT_DISABLED) 1351
1491 goto err_out; 1352 /* reset and determine device classes */
1353 for (i = 0; i < ATA_MAX_DEVICES; i++)
1354 classes[i] = ATA_DEV_UNKNOWN;
1355
1356 if (ap->ops->probe_reset) {
1357 rc = ap->ops->probe_reset(ap, classes);
1358 if (rc) {
1359 printk("ata%u: reset failed (errno=%d)\n", ap->id, rc);
1360 return rc;
1361 }
1362 } else {
1363 ap->ops->phy_reset(ap);
1364
1365 if (!(ap->flags & ATA_FLAG_PORT_DISABLED))
1366 for (i = 0; i < ATA_MAX_DEVICES; i++)
1367 classes[i] = ap->device[i].class;
1368
1369 ata_port_probe(ap);
1370 }
1492 1371
1372 for (i = 0; i < ATA_MAX_DEVICES; i++)
1373 if (classes[i] == ATA_DEV_UNKNOWN)
1374 classes[i] = ATA_DEV_NONE;
1375
1376 /* read IDENTIFY page and configure devices */
1493 for (i = 0; i < ATA_MAX_DEVICES; i++) { 1377 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1494 ata_dev_identify(ap, i); 1378 struct ata_device *dev = &ap->device[i];
1495 if (ata_dev_present(&ap->device[i])) { 1379
1496 found = 1; 1380 dev->class = classes[i];
1497 ata_dev_config(ap,i); 1381
1382 if (!ata_dev_present(dev))
1383 continue;
1384
1385 WARN_ON(dev->id != NULL);
1386 if (ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id)) {
1387 dev->class = ATA_DEV_NONE;
1388 continue;
1389 }
1390
1391 if (ata_dev_configure(ap, dev, 1)) {
1392 dev->class++; /* disable device */
1393 continue;
1498 } 1394 }
1395
1396 found = 1;
1499 } 1397 }
1500 1398
1501 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED)) 1399 if (!found)
1502 goto err_out_disable; 1400 goto err_out_disable;
1503 1401
1504 ata_set_mode(ap); 1402 ata_set_mode(ap);
@@ -1509,7 +1407,6 @@ static int ata_bus_probe(struct ata_port *ap)
1509 1407
1510err_out_disable: 1408err_out_disable:
1511 ap->ops->port_disable(ap); 1409 ap->ops->port_disable(ap);
1512err_out:
1513 return -1; 1410 return -1;
1514} 1411}
1515 1412
@@ -1530,6 +1427,41 @@ void ata_port_probe(struct ata_port *ap)
1530} 1427}
1531 1428
1532/** 1429/**
1430 * sata_print_link_status - Print SATA link status
1431 * @ap: SATA port to printk link status about
1432 *
1433 * This function prints link speed and status of a SATA link.
1434 *
1435 * LOCKING:
1436 * None.
1437 */
1438static void sata_print_link_status(struct ata_port *ap)
1439{
1440 u32 sstatus, tmp;
1441 const char *speed;
1442
1443 if (!ap->ops->scr_read)
1444 return;
1445
1446 sstatus = scr_read(ap, SCR_STATUS);
1447
1448 if (sata_dev_present(ap)) {
1449 tmp = (sstatus >> 4) & 0xf;
1450 if (tmp & (1 << 0))
1451 speed = "1.5";
1452 else if (tmp & (1 << 1))
1453 speed = "3.0";
1454 else
1455 speed = "<unknown>";
1456 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1457 ap->id, speed, sstatus);
1458 } else {
1459 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1460 ap->id, sstatus);
1461 }
1462}
1463
1464/**
1533 * __sata_phy_reset - Wake/reset a low-level SATA PHY 1465 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1534 * @ap: SATA port associated with target SATA PHY. 1466 * @ap: SATA port associated with target SATA PHY.
1535 * 1467 *
@@ -1563,27 +1495,14 @@ void __sata_phy_reset(struct ata_port *ap)
1563 break; 1495 break;
1564 } while (time_before(jiffies, timeout)); 1496 } while (time_before(jiffies, timeout));
1565 1497
1566 /* TODO: phy layer with polling, timeouts, etc. */ 1498 /* print link status */
1567 sstatus = scr_read(ap, SCR_STATUS); 1499 sata_print_link_status(ap);
1568 if (sata_dev_present(ap)) {
1569 const char *speed;
1570 u32 tmp;
1571 1500
1572 tmp = (sstatus >> 4) & 0xf; 1501 /* TODO: phy layer with polling, timeouts, etc. */
1573 if (tmp & (1 << 0)) 1502 if (sata_dev_present(ap))
1574 speed = "1.5";
1575 else if (tmp & (1 << 1))
1576 speed = "3.0";
1577 else
1578 speed = "<unknown>";
1579 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1580 ap->id, speed, sstatus);
1581 ata_port_probe(ap); 1503 ata_port_probe(ap);
1582 } else { 1504 else
1583 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1584 ap->id, sstatus);
1585 ata_port_disable(ap); 1505 ata_port_disable(ap);
1586 }
1587 1506
1588 if (ap->flags & ATA_FLAG_PORT_DISABLED) 1507 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1589 return; 1508 return;
@@ -1756,9 +1675,9 @@ int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1756 ata_timing_quantize(t, t, T, UT); 1675 ata_timing_quantize(t, t, T, UT);
1757 1676
1758 /* 1677 /*
1759 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T 1678 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1760 * and some other commands. We have to ensure that the DMA cycle timing is 1679 * S.M.A.R.T * and some other commands. We have to ensure that the
1761 * slower/equal than the fastest PIO timing. 1680 * DMA cycle timing is slower/equal than the fastest PIO timing.
1762 */ 1681 */
1763 1682
1764 if (speed > XFER_PIO_4) { 1683 if (speed > XFER_PIO_4) {
@@ -1767,7 +1686,7 @@ int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1767 } 1686 }
1768 1687
1769 /* 1688 /*
1770 * Lenghten active & recovery time so that cycle time is correct. 1689 * Lengthen active & recovery time so that cycle time is correct.
1771 */ 1690 */
1772 1691
1773 if (t->act8b + t->rec8b < t->cyc8b) { 1692 if (t->act8b + t->rec8b < t->cyc8b) {
@@ -1783,31 +1702,8 @@ int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1783 return 0; 1702 return 0;
1784} 1703}
1785 1704
1786static const struct {
1787 unsigned int shift;
1788 u8 base;
1789} xfer_mode_classes[] = {
1790 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1791 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1792 { ATA_SHIFT_PIO, XFER_PIO_0 },
1793};
1794
1795static u8 base_from_shift(unsigned int shift)
1796{
1797 int i;
1798
1799 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1800 if (xfer_mode_classes[i].shift == shift)
1801 return xfer_mode_classes[i].base;
1802
1803 return 0xff;
1804}
1805
1806static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev) 1705static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1807{ 1706{
1808 int ofs, idx;
1809 u8 base;
1810
1811 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED)) 1707 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1812 return; 1708 return;
1813 1709
@@ -1816,65 +1712,58 @@ static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1816 1712
1817 ata_dev_set_xfermode(ap, dev); 1713 ata_dev_set_xfermode(ap, dev);
1818 1714
1819 base = base_from_shift(dev->xfer_shift); 1715 if (ata_dev_revalidate(ap, dev, 0)) {
1820 ofs = dev->xfer_mode - base; 1716 printk(KERN_ERR "ata%u: failed to revalidate after set "
1821 idx = ofs + dev->xfer_shift; 1717 "xfermode, disabled\n", ap->id);
1822 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str)); 1718 ata_port_disable(ap);
1719 }
1823 1720
1824 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n", 1721 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
1825 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs); 1722 dev->xfer_shift, (int)dev->xfer_mode);
1826 1723
1827 printk(KERN_INFO "ata%u: dev %u configured for %s\n", 1724 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1828 ap->id, dev->devno, xfer_mode_str[idx]); 1725 ap->id, dev->devno,
1726 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
1829} 1727}
1830 1728
1831static int ata_host_set_pio(struct ata_port *ap) 1729static int ata_host_set_pio(struct ata_port *ap)
1832{ 1730{
1833 unsigned int mask; 1731 int i;
1834 int x, i;
1835 u8 base, xfer_mode;
1836
1837 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1838 x = fgb(mask);
1839 if (x < 0) {
1840 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1841 return -1;
1842 }
1843
1844 base = base_from_shift(ATA_SHIFT_PIO);
1845 xfer_mode = base + x;
1846
1847 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1848 (int)base, (int)xfer_mode, mask, x);
1849 1732
1850 for (i = 0; i < ATA_MAX_DEVICES; i++) { 1733 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1851 struct ata_device *dev = &ap->device[i]; 1734 struct ata_device *dev = &ap->device[i];
1852 if (ata_dev_present(dev)) { 1735
1853 dev->pio_mode = xfer_mode; 1736 if (!ata_dev_present(dev))
1854 dev->xfer_mode = xfer_mode; 1737 continue;
1855 dev->xfer_shift = ATA_SHIFT_PIO; 1738
1856 if (ap->ops->set_piomode) 1739 if (!dev->pio_mode) {
1857 ap->ops->set_piomode(ap, dev); 1740 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1741 return -1;
1858 } 1742 }
1743
1744 dev->xfer_mode = dev->pio_mode;
1745 dev->xfer_shift = ATA_SHIFT_PIO;
1746 if (ap->ops->set_piomode)
1747 ap->ops->set_piomode(ap, dev);
1859 } 1748 }
1860 1749
1861 return 0; 1750 return 0;
1862} 1751}
1863 1752
1864static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode, 1753static void ata_host_set_dma(struct ata_port *ap)
1865 unsigned int xfer_shift)
1866{ 1754{
1867 int i; 1755 int i;
1868 1756
1869 for (i = 0; i < ATA_MAX_DEVICES; i++) { 1757 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1870 struct ata_device *dev = &ap->device[i]; 1758 struct ata_device *dev = &ap->device[i];
1871 if (ata_dev_present(dev)) { 1759
1872 dev->dma_mode = xfer_mode; 1760 if (!ata_dev_present(dev) || !dev->dma_mode)
1873 dev->xfer_mode = xfer_mode; 1761 continue;
1874 dev->xfer_shift = xfer_shift; 1762
1875 if (ap->ops->set_dmamode) 1763 dev->xfer_mode = dev->dma_mode;
1876 ap->ops->set_dmamode(ap, dev); 1764 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
1877 } 1765 if (ap->ops->set_dmamode)
1766 ap->ops->set_dmamode(ap, dev);
1878 } 1767 }
1879} 1768}
1880 1769
@@ -1886,32 +1775,37 @@ static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1886 * 1775 *
1887 * LOCKING: 1776 * LOCKING:
1888 * PCI/etc. bus probe sem. 1777 * PCI/etc. bus probe sem.
1889 *
1890 */ 1778 */
1891static void ata_set_mode(struct ata_port *ap) 1779static void ata_set_mode(struct ata_port *ap)
1892{ 1780{
1893 unsigned int xfer_shift; 1781 int i, rc;
1894 u8 xfer_mode;
1895 int rc;
1896 1782
1897 /* step 1: always set host PIO timings */ 1783 /* step 1: calculate xfer_mask */
1898 rc = ata_host_set_pio(ap); 1784 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1899 if (rc) 1785 struct ata_device *dev = &ap->device[i];
1900 goto err_out; 1786 unsigned int xfer_mask;
1901 1787
1902 /* step 2: choose the best data xfer mode */ 1788 if (!ata_dev_present(dev))
1903 xfer_mode = xfer_shift = 0; 1789 continue;
1904 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift); 1790
1791 xfer_mask = ata_dev_xfermask(ap, dev);
1792
1793 dev->pio_mode = ata_xfer_mask2mode(xfer_mask & ATA_MASK_PIO);
1794 dev->dma_mode = ata_xfer_mask2mode(xfer_mask & (ATA_MASK_MWDMA |
1795 ATA_MASK_UDMA));
1796 }
1797
1798 /* step 2: always set host PIO timings */
1799 rc = ata_host_set_pio(ap);
1905 if (rc) 1800 if (rc)
1906 goto err_out; 1801 goto err_out;
1907 1802
1908 /* step 3: if that xfer mode isn't PIO, set host DMA timings */ 1803 /* step 3: set host DMA timings */
1909 if (xfer_shift != ATA_SHIFT_PIO) 1804 ata_host_set_dma(ap);
1910 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1911 1805
1912 /* step 4: update devices' xfer mode */ 1806 /* step 4: update devices' xfer mode */
1913 ata_dev_set_mode(ap, &ap->device[0]); 1807 for (i = 0; i < ATA_MAX_DEVICES; i++)
1914 ata_dev_set_mode(ap, &ap->device[1]); 1808 ata_dev_set_mode(ap, &ap->device[i]);
1915 1809
1916 if (ap->flags & ATA_FLAG_PORT_DISABLED) 1810 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1917 return; 1811 return;
@@ -1926,6 +1820,26 @@ err_out:
1926} 1820}
1927 1821
1928/** 1822/**
1823 * ata_tf_to_host - issue ATA taskfile to host controller
1824 * @ap: port to which command is being issued
1825 * @tf: ATA taskfile register set
1826 *
1827 * Issues ATA taskfile register set to ATA host controller,
1828 * with proper synchronization with interrupt handler and
1829 * other threads.
1830 *
1831 * LOCKING:
1832 * spin_lock_irqsave(host_set lock)
1833 */
1834
1835static inline void ata_tf_to_host(struct ata_port *ap,
1836 const struct ata_taskfile *tf)
1837{
1838 ap->ops->tf_load(ap, tf);
1839 ap->ops->exec_command(ap, tf);
1840}
1841
1842/**
1929 * ata_busy_sleep - sleep until BSY clears, or timeout 1843 * ata_busy_sleep - sleep until BSY clears, or timeout
1930 * @ap: port containing status register to be polled 1844 * @ap: port containing status register to be polled
1931 * @tmout_pat: impatience timeout 1845 * @tmout_pat: impatience timeout
@@ -1935,12 +1849,10 @@ err_out:
1935 * or a timeout occurs. 1849 * or a timeout occurs.
1936 * 1850 *
1937 * LOCKING: None. 1851 * LOCKING: None.
1938 *
1939 */ 1852 */
1940 1853
1941static unsigned int ata_busy_sleep (struct ata_port *ap, 1854unsigned int ata_busy_sleep (struct ata_port *ap,
1942 unsigned long tmout_pat, 1855 unsigned long tmout_pat, unsigned long tmout)
1943 unsigned long tmout)
1944{ 1856{
1945 unsigned long timer_start, timeout; 1857 unsigned long timer_start, timeout;
1946 u8 status; 1858 u8 status;
@@ -2159,9 +2071,9 @@ void ata_bus_reset(struct ata_port *ap)
2159 /* 2071 /*
2160 * determine by signature whether we have ATA or ATAPI devices 2072 * determine by signature whether we have ATA or ATAPI devices
2161 */ 2073 */
2162 err = ata_dev_try_classify(ap, 0); 2074 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
2163 if ((slave_possible) && (err != 0x81)) 2075 if ((slave_possible) && (err != 0x81))
2164 ata_dev_try_classify(ap, 1); 2076 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
2165 2077
2166 /* re-enable interrupts */ 2078 /* re-enable interrupts */
2167 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */ 2079 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
@@ -2196,11 +2108,446 @@ err_out:
2196 DPRINTK("EXIT\n"); 2108 DPRINTK("EXIT\n");
2197} 2109}
2198 2110
2199static void ata_pr_blacklisted(const struct ata_port *ap, 2111static int sata_phy_resume(struct ata_port *ap)
2200 const struct ata_device *dev)
2201{ 2112{
2202 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n", 2113 unsigned long timeout = jiffies + (HZ * 5);
2203 ap->id, dev->devno); 2114 u32 sstatus;
2115
2116 scr_write_flush(ap, SCR_CONTROL, 0x300);
2117
2118 /* Wait for phy to become ready, if necessary. */
2119 do {
2120 msleep(200);
2121 sstatus = scr_read(ap, SCR_STATUS);
2122 if ((sstatus & 0xf) != 1)
2123 return 0;
2124 } while (time_before(jiffies, timeout));
2125
2126 return -1;
2127}
2128
2129/**
2130 * ata_std_probeinit - initialize probing
2131 * @ap: port to be probed
2132 *
2133 * @ap is about to be probed. Initialize it. This function is
2134 * to be used as standard callback for ata_drive_probe_reset().
2135 *
2136 * NOTE!!! Do not use this function as probeinit if a low level
2137 * driver implements only hardreset. Just pass NULL as probeinit
2138 * in that case. Using this function is probably okay but doing
2139 * so makes reset sequence different from the original
2140 * ->phy_reset implementation and Jeff nervous. :-P
2141 */
2142extern void ata_std_probeinit(struct ata_port *ap)
2143{
2144 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
2145 sata_phy_resume(ap);
2146 if (sata_dev_present(ap))
2147 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2148 }
2149}
2150
2151/**
2152 * ata_std_softreset - reset host port via ATA SRST
2153 * @ap: port to reset
2154 * @verbose: fail verbosely
2155 * @classes: resulting classes of attached devices
2156 *
2157 * Reset host port using ATA SRST. This function is to be used
2158 * as standard callback for ata_drive_*_reset() functions.
2159 *
2160 * LOCKING:
2161 * Kernel thread context (may sleep)
2162 *
2163 * RETURNS:
2164 * 0 on success, -errno otherwise.
2165 */
2166int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2167{
2168 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2169 unsigned int devmask = 0, err_mask;
2170 u8 err;
2171
2172 DPRINTK("ENTER\n");
2173
2174 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2175 classes[0] = ATA_DEV_NONE;
2176 goto out;
2177 }
2178
2179 /* determine if device 0/1 are present */
2180 if (ata_devchk(ap, 0))
2181 devmask |= (1 << 0);
2182 if (slave_possible && ata_devchk(ap, 1))
2183 devmask |= (1 << 1);
2184
2185 /* select device 0 again */
2186 ap->ops->dev_select(ap, 0);
2187
2188 /* issue bus reset */
2189 DPRINTK("about to softreset, devmask=%x\n", devmask);
2190 err_mask = ata_bus_softreset(ap, devmask);
2191 if (err_mask) {
2192 if (verbose)
2193 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2194 ap->id, err_mask);
2195 else
2196 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2197 err_mask);
2198 return -EIO;
2199 }
2200
2201 /* determine by signature whether we have ATA or ATAPI devices */
2202 classes[0] = ata_dev_try_classify(ap, 0, &err);
2203 if (slave_possible && err != 0x81)
2204 classes[1] = ata_dev_try_classify(ap, 1, &err);
2205
2206 out:
2207 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2208 return 0;
2209}
2210
2211/**
2212 * sata_std_hardreset - reset host port via SATA phy reset
2213 * @ap: port to reset
2214 * @verbose: fail verbosely
2215 * @class: resulting class of attached device
2216 *
2217 * SATA phy-reset host port using DET bits of SControl register.
2218 * This function is to be used as standard callback for
2219 * ata_drive_*_reset().
2220 *
2221 * LOCKING:
2222 * Kernel thread context (may sleep)
2223 *
2224 * RETURNS:
2225 * 0 on success, -errno otherwise.
2226 */
2227int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2228{
2229 DPRINTK("ENTER\n");
2230
2231 /* Issue phy wake/reset */
2232 scr_write_flush(ap, SCR_CONTROL, 0x301);
2233
2234 /*
2235 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2236 * 10.4.2 says at least 1 ms.
2237 */
2238 msleep(1);
2239
2240 /* Bring phy back */
2241 sata_phy_resume(ap);
2242
2243 /* TODO: phy layer with polling, timeouts, etc. */
2244 if (!sata_dev_present(ap)) {
2245 *class = ATA_DEV_NONE;
2246 DPRINTK("EXIT, link offline\n");
2247 return 0;
2248 }
2249
2250 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2251 if (verbose)
2252 printk(KERN_ERR "ata%u: COMRESET failed "
2253 "(device not ready)\n", ap->id);
2254 else
2255 DPRINTK("EXIT, device not ready\n");
2256 return -EIO;
2257 }
2258
2259 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2260
2261 *class = ata_dev_try_classify(ap, 0, NULL);
2262
2263 DPRINTK("EXIT, class=%u\n", *class);
2264 return 0;
2265}
2266
2267/**
2268 * ata_std_postreset - standard postreset callback
2269 * @ap: the target ata_port
2270 * @classes: classes of attached devices
2271 *
2272 * This function is invoked after a successful reset. Note that
2273 * the device might have been reset more than once using
2274 * different reset methods before postreset is invoked.
2275 *
2276 * This function is to be used as standard callback for
2277 * ata_drive_*_reset().
2278 *
2279 * LOCKING:
2280 * Kernel thread context (may sleep)
2281 */
2282void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2283{
2284 DPRINTK("ENTER\n");
2285
2286 /* set cable type if it isn't already set */
2287 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2288 ap->cbl = ATA_CBL_SATA;
2289
2290 /* print link status */
2291 if (ap->cbl == ATA_CBL_SATA)
2292 sata_print_link_status(ap);
2293
2294 /* re-enable interrupts */
2295 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2296 ata_irq_on(ap);
2297
2298 /* is double-select really necessary? */
2299 if (classes[0] != ATA_DEV_NONE)
2300 ap->ops->dev_select(ap, 1);
2301 if (classes[1] != ATA_DEV_NONE)
2302 ap->ops->dev_select(ap, 0);
2303
2304 /* bail out if no device is present */
2305 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2306 DPRINTK("EXIT, no device\n");
2307 return;
2308 }
2309
2310 /* set up device control */
2311 if (ap->ioaddr.ctl_addr) {
2312 if (ap->flags & ATA_FLAG_MMIO)
2313 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2314 else
2315 outb(ap->ctl, ap->ioaddr.ctl_addr);
2316 }
2317
2318 DPRINTK("EXIT\n");
2319}
2320
2321/**
2322 * ata_std_probe_reset - standard probe reset method
2323 * @ap: prot to perform probe-reset
2324 * @classes: resulting classes of attached devices
2325 *
2326 * The stock off-the-shelf ->probe_reset method.
2327 *
2328 * LOCKING:
2329 * Kernel thread context (may sleep)
2330 *
2331 * RETURNS:
2332 * 0 on success, -errno otherwise.
2333 */
2334int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2335{
2336 ata_reset_fn_t hardreset;
2337
2338 hardreset = NULL;
2339 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
2340 hardreset = sata_std_hardreset;
2341
2342 return ata_drive_probe_reset(ap, ata_std_probeinit,
2343 ata_std_softreset, hardreset,
2344 ata_std_postreset, classes);
2345}
2346
2347static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2348 ata_postreset_fn_t postreset,
2349 unsigned int *classes)
2350{
2351 int i, rc;
2352
2353 for (i = 0; i < ATA_MAX_DEVICES; i++)
2354 classes[i] = ATA_DEV_UNKNOWN;
2355
2356 rc = reset(ap, 0, classes);
2357 if (rc)
2358 return rc;
2359
2360 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2361 * is complete and convert all ATA_DEV_UNKNOWN to
2362 * ATA_DEV_NONE.
2363 */
2364 for (i = 0; i < ATA_MAX_DEVICES; i++)
2365 if (classes[i] != ATA_DEV_UNKNOWN)
2366 break;
2367
2368 if (i < ATA_MAX_DEVICES)
2369 for (i = 0; i < ATA_MAX_DEVICES; i++)
2370 if (classes[i] == ATA_DEV_UNKNOWN)
2371 classes[i] = ATA_DEV_NONE;
2372
2373 if (postreset)
2374 postreset(ap, classes);
2375
2376 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2377}
2378
2379/**
2380 * ata_drive_probe_reset - Perform probe reset with given methods
2381 * @ap: port to reset
2382 * @probeinit: probeinit method (can be NULL)
2383 * @softreset: softreset method (can be NULL)
2384 * @hardreset: hardreset method (can be NULL)
2385 * @postreset: postreset method (can be NULL)
2386 * @classes: resulting classes of attached devices
2387 *
2388 * Reset the specified port and classify attached devices using
2389 * given methods. This function prefers softreset but tries all
2390 * possible reset sequences to reset and classify devices. This
2391 * function is intended to be used for constructing ->probe_reset
2392 * callback by low level drivers.
2393 *
2394 * Reset methods should follow the following rules.
2395 *
2396 * - Return 0 on sucess, -errno on failure.
2397 * - If classification is supported, fill classes[] with
2398 * recognized class codes.
2399 * - If classification is not supported, leave classes[] alone.
2400 * - If verbose is non-zero, print error message on failure;
2401 * otherwise, shut up.
2402 *
2403 * LOCKING:
2404 * Kernel thread context (may sleep)
2405 *
2406 * RETURNS:
2407 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2408 * if classification fails, and any error code from reset
2409 * methods.
2410 */
2411int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
2412 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2413 ata_postreset_fn_t postreset, unsigned int *classes)
2414{
2415 int rc = -EINVAL;
2416
2417 if (probeinit)
2418 probeinit(ap);
2419
2420 if (softreset) {
2421 rc = do_probe_reset(ap, softreset, postreset, classes);
2422 if (rc == 0)
2423 return 0;
2424 }
2425
2426 if (!hardreset)
2427 return rc;
2428
2429 rc = do_probe_reset(ap, hardreset, postreset, classes);
2430 if (rc == 0 || rc != -ENODEV)
2431 return rc;
2432
2433 if (softreset)
2434 rc = do_probe_reset(ap, softreset, postreset, classes);
2435
2436 return rc;
2437}
2438
2439/**
2440 * ata_dev_same_device - Determine whether new ID matches configured device
2441 * @ap: port on which the device to compare against resides
2442 * @dev: device to compare against
2443 * @new_class: class of the new device
2444 * @new_id: IDENTIFY page of the new device
2445 *
2446 * Compare @new_class and @new_id against @dev and determine
2447 * whether @dev is the device indicated by @new_class and
2448 * @new_id.
2449 *
2450 * LOCKING:
2451 * None.
2452 *
2453 * RETURNS:
2454 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
2455 */
2456static int ata_dev_same_device(struct ata_port *ap, struct ata_device *dev,
2457 unsigned int new_class, const u16 *new_id)
2458{
2459 const u16 *old_id = dev->id;
2460 unsigned char model[2][41], serial[2][21];
2461 u64 new_n_sectors;
2462
2463 if (dev->class != new_class) {
2464 printk(KERN_INFO
2465 "ata%u: dev %u class mismatch %d != %d\n",
2466 ap->id, dev->devno, dev->class, new_class);
2467 return 0;
2468 }
2469
2470 ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
2471 ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
2472 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
2473 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
2474 new_n_sectors = ata_id_n_sectors(new_id);
2475
2476 if (strcmp(model[0], model[1])) {
2477 printk(KERN_INFO
2478 "ata%u: dev %u model number mismatch '%s' != '%s'\n",
2479 ap->id, dev->devno, model[0], model[1]);
2480 return 0;
2481 }
2482
2483 if (strcmp(serial[0], serial[1])) {
2484 printk(KERN_INFO
2485 "ata%u: dev %u serial number mismatch '%s' != '%s'\n",
2486 ap->id, dev->devno, serial[0], serial[1]);
2487 return 0;
2488 }
2489
2490 if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
2491 printk(KERN_INFO
2492 "ata%u: dev %u n_sectors mismatch %llu != %llu\n",
2493 ap->id, dev->devno, (unsigned long long)dev->n_sectors,
2494 (unsigned long long)new_n_sectors);
2495 return 0;
2496 }
2497
2498 return 1;
2499}
2500
2501/**
2502 * ata_dev_revalidate - Revalidate ATA device
2503 * @ap: port on which the device to revalidate resides
2504 * @dev: device to revalidate
2505 * @post_reset: is this revalidation after reset?
2506 *
2507 * Re-read IDENTIFY page and make sure @dev is still attached to
2508 * the port.
2509 *
2510 * LOCKING:
2511 * Kernel thread context (may sleep)
2512 *
2513 * RETURNS:
2514 * 0 on success, negative errno otherwise
2515 */
2516int ata_dev_revalidate(struct ata_port *ap, struct ata_device *dev,
2517 int post_reset)
2518{
2519 unsigned int class;
2520 u16 *id;
2521 int rc;
2522
2523 if (!ata_dev_present(dev))
2524 return -ENODEV;
2525
2526 class = dev->class;
2527 id = NULL;
2528
2529 /* allocate & read ID data */
2530 rc = ata_dev_read_id(ap, dev, &class, post_reset, &id);
2531 if (rc)
2532 goto fail;
2533
2534 /* is the device still there? */
2535 if (!ata_dev_same_device(ap, dev, class, id)) {
2536 rc = -ENODEV;
2537 goto fail;
2538 }
2539
2540 kfree(dev->id);
2541 dev->id = id;
2542
2543 /* configure device according to the new ID */
2544 return ata_dev_configure(ap, dev, 0);
2545
2546 fail:
2547 printk(KERN_ERR "ata%u: dev %u revalidation failed (errno=%d)\n",
2548 ap->id, dev->devno, rc);
2549 kfree(id);
2550 return rc;
2204} 2551}
2205 2552
2206static const char * const ata_dma_blacklist [] = { 2553static const char * const ata_dma_blacklist [] = {
@@ -2237,151 +2584,57 @@ static const char * const ata_dma_blacklist [] = {
2237 2584
2238static int ata_dma_blacklisted(const struct ata_device *dev) 2585static int ata_dma_blacklisted(const struct ata_device *dev)
2239{ 2586{
2240 unsigned char model_num[40]; 2587 unsigned char model_num[41];
2241 char *s;
2242 unsigned int len;
2243 int i; 2588 int i;
2244 2589
2245 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS, 2590 ata_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS, sizeof(model_num));
2246 sizeof(model_num));
2247 s = &model_num[0];
2248 len = strnlen(s, sizeof(model_num));
2249
2250 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2251 while ((len > 0) && (s[len - 1] == ' ')) {
2252 len--;
2253 s[len] = 0;
2254 }
2255 2591
2256 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++) 2592 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2257 if (!strncmp(ata_dma_blacklist[i], s, len)) 2593 if (!strcmp(ata_dma_blacklist[i], model_num))
2258 return 1; 2594 return 1;
2259 2595
2260 return 0; 2596 return 0;
2261} 2597}
2262 2598
2263static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
2264{
2265 const struct ata_device *master, *slave;
2266 unsigned int mask;
2267
2268 master = &ap->device[0];
2269 slave = &ap->device[1];
2270
2271 assert (ata_dev_present(master) || ata_dev_present(slave));
2272
2273 if (shift == ATA_SHIFT_UDMA) {
2274 mask = ap->udma_mask;
2275 if (ata_dev_present(master)) {
2276 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
2277 if (ata_dma_blacklisted(master)) {
2278 mask = 0;
2279 ata_pr_blacklisted(ap, master);
2280 }
2281 }
2282 if (ata_dev_present(slave)) {
2283 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
2284 if (ata_dma_blacklisted(slave)) {
2285 mask = 0;
2286 ata_pr_blacklisted(ap, slave);
2287 }
2288 }
2289 }
2290 else if (shift == ATA_SHIFT_MWDMA) {
2291 mask = ap->mwdma_mask;
2292 if (ata_dev_present(master)) {
2293 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
2294 if (ata_dma_blacklisted(master)) {
2295 mask = 0;
2296 ata_pr_blacklisted(ap, master);
2297 }
2298 }
2299 if (ata_dev_present(slave)) {
2300 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
2301 if (ata_dma_blacklisted(slave)) {
2302 mask = 0;
2303 ata_pr_blacklisted(ap, slave);
2304 }
2305 }
2306 }
2307 else if (shift == ATA_SHIFT_PIO) {
2308 mask = ap->pio_mask;
2309 if (ata_dev_present(master)) {
2310 /* spec doesn't return explicit support for
2311 * PIO0-2, so we fake it
2312 */
2313 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2314 tmp_mode <<= 3;
2315 tmp_mode |= 0x7;
2316 mask &= tmp_mode;
2317 }
2318 if (ata_dev_present(slave)) {
2319 /* spec doesn't return explicit support for
2320 * PIO0-2, so we fake it
2321 */
2322 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2323 tmp_mode <<= 3;
2324 tmp_mode |= 0x7;
2325 mask &= tmp_mode;
2326 }
2327 }
2328 else {
2329 mask = 0xffffffff; /* shut up compiler warning */
2330 BUG();
2331 }
2332
2333 return mask;
2334}
2335
2336/* find greatest bit */
2337static int fgb(u32 bitmap)
2338{
2339 unsigned int i;
2340 int x = -1;
2341
2342 for (i = 0; i < 32; i++)
2343 if (bitmap & (1 << i))
2344 x = i;
2345
2346 return x;
2347}
2348
2349/** 2599/**
2350 * ata_choose_xfer_mode - attempt to find best transfer mode 2600 * ata_dev_xfermask - Compute supported xfermask of the given device
2351 * @ap: Port for which an xfer mode will be selected 2601 * @ap: Port on which the device to compute xfermask for resides
2352 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code 2602 * @dev: Device to compute xfermask for
2353 * @xfer_shift_out: (output) bit shift that selects this mode
2354 * 2603 *
2355 * Based on host and device capabilities, determine the 2604 * Compute supported xfermask of @dev. This function is
2356 * maximum transfer mode that is amenable to all. 2605 * responsible for applying all known limits including host
2606 * controller limits, device blacklist, etc...
2357 * 2607 *
2358 * LOCKING: 2608 * LOCKING:
2359 * PCI/etc. bus probe sem. 2609 * None.
2360 * 2610 *
2361 * RETURNS: 2611 * RETURNS:
2362 * Zero on success, negative on error. 2612 * Computed xfermask.
2363 */ 2613 */
2364 2614static unsigned int ata_dev_xfermask(struct ata_port *ap,
2365static int ata_choose_xfer_mode(const struct ata_port *ap, 2615 struct ata_device *dev)
2366 u8 *xfer_mode_out,
2367 unsigned int *xfer_shift_out)
2368{ 2616{
2369 unsigned int mask, shift; 2617 unsigned long xfer_mask;
2370 int x, i; 2618 int i;
2371 2619
2372 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) { 2620 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
2373 shift = xfer_mode_classes[i].shift; 2621 ap->udma_mask);
2374 mask = ata_get_mode_mask(ap, shift);
2375 2622
2376 x = fgb(mask); 2623 /* use port-wide xfermask for now */
2377 if (x >= 0) { 2624 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2378 *xfer_mode_out = xfer_mode_classes[i].base + x; 2625 struct ata_device *d = &ap->device[i];
2379 *xfer_shift_out = shift; 2626 if (!ata_dev_present(d))
2380 return 0; 2627 continue;
2381 } 2628 xfer_mask &= ata_id_xfermask(d->id);
2629 if (ata_dma_blacklisted(d))
2630 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
2382 } 2631 }
2383 2632
2384 return -1; 2633 if (ata_dma_blacklisted(dev))
2634 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, "
2635 "disabling DMA\n", ap->id, dev->devno);
2636
2637 return xfer_mask;
2385} 2638}
2386 2639
2387/** 2640/**
@@ -2420,63 +2673,28 @@ static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2420} 2673}
2421 2674
2422/** 2675/**
2423 * ata_dev_reread_id - Reread the device identify device info
2424 * @ap: port where the device is
2425 * @dev: device to reread the identify device info
2426 *
2427 * LOCKING:
2428 */
2429
2430static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2431{
2432 struct ata_taskfile tf;
2433
2434 ata_tf_init(ap, &tf, dev->devno);
2435
2436 if (dev->class == ATA_DEV_ATA) {
2437 tf.command = ATA_CMD_ID_ATA;
2438 DPRINTK("do ATA identify\n");
2439 } else {
2440 tf.command = ATA_CMD_ID_ATAPI;
2441 DPRINTK("do ATAPI identify\n");
2442 }
2443
2444 tf.flags |= ATA_TFLAG_DEVICE;
2445 tf.protocol = ATA_PROT_PIO;
2446
2447 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2448 dev->id, sizeof(dev->id)))
2449 goto err_out;
2450
2451 swap_buf_le16(dev->id, ATA_ID_WORDS);
2452
2453 ata_dump_id(dev);
2454
2455 DPRINTK("EXIT\n");
2456
2457 return;
2458err_out:
2459 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
2460 ata_port_disable(ap);
2461}
2462
2463/**
2464 * ata_dev_init_params - Issue INIT DEV PARAMS command 2676 * ata_dev_init_params - Issue INIT DEV PARAMS command
2465 * @ap: Port associated with device @dev 2677 * @ap: Port associated with device @dev
2466 * @dev: Device to which command will be sent 2678 * @dev: Device to which command will be sent
2467 * 2679 *
2468 * LOCKING: 2680 * LOCKING:
2681 * Kernel thread context (may sleep)
2682 *
2683 * RETURNS:
2684 * 0 on success, AC_ERR_* mask otherwise.
2469 */ 2685 */
2470 2686
2471static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev) 2687static unsigned int ata_dev_init_params(struct ata_port *ap,
2688 struct ata_device *dev)
2472{ 2689{
2473 struct ata_taskfile tf; 2690 struct ata_taskfile tf;
2691 unsigned int err_mask;
2474 u16 sectors = dev->id[6]; 2692 u16 sectors = dev->id[6];
2475 u16 heads = dev->id[3]; 2693 u16 heads = dev->id[3];
2476 2694
2477 /* Number of sectors per track 1-255. Number of heads 1-16 */ 2695 /* Number of sectors per track 1-255. Number of heads 1-16 */
2478 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16) 2696 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2479 return; 2697 return 0;
2480 2698
2481 /* set up init dev params taskfile */ 2699 /* set up init dev params taskfile */
2482 DPRINTK("init dev params \n"); 2700 DPRINTK("init dev params \n");
@@ -2488,13 +2706,10 @@ static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2488 tf.nsect = sectors; 2706 tf.nsect = sectors;
2489 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */ 2707 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2490 2708
2491 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) { 2709 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
2492 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2493 ap->id);
2494 ata_port_disable(ap);
2495 }
2496 2710
2497 DPRINTK("EXIT\n"); 2711 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2712 return err_mask;
2498} 2713}
2499 2714
2500/** 2715/**
@@ -2514,11 +2729,11 @@ static void ata_sg_clean(struct ata_queued_cmd *qc)
2514 int dir = qc->dma_dir; 2729 int dir = qc->dma_dir;
2515 void *pad_buf = NULL; 2730 void *pad_buf = NULL;
2516 2731
2517 assert(qc->flags & ATA_QCFLAG_DMAMAP); 2732 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2518 assert(sg != NULL); 2733 WARN_ON(sg == NULL);
2519 2734
2520 if (qc->flags & ATA_QCFLAG_SINGLE) 2735 if (qc->flags & ATA_QCFLAG_SINGLE)
2521 assert(qc->n_elem <= 1); 2736 WARN_ON(qc->n_elem > 1);
2522 2737
2523 VPRINTK("unmapping %u sg elements\n", qc->n_elem); 2738 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
2524 2739
@@ -2573,8 +2788,8 @@ static void ata_fill_sg(struct ata_queued_cmd *qc)
2573 struct scatterlist *sg; 2788 struct scatterlist *sg;
2574 unsigned int idx; 2789 unsigned int idx;
2575 2790
2576 assert(qc->__sg != NULL); 2791 WARN_ON(qc->__sg == NULL);
2577 assert(qc->n_elem > 0 || qc->pad_len > 0); 2792 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
2578 2793
2579 idx = 0; 2794 idx = 0;
2580 ata_for_each_sg(sg, qc) { 2795 ata_for_each_sg(sg, qc) {
@@ -2727,7 +2942,7 @@ static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2727 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ); 2942 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2728 struct scatterlist *psg = &qc->pad_sgent; 2943 struct scatterlist *psg = &qc->pad_sgent;
2729 2944
2730 assert(qc->dev->class == ATA_DEV_ATAPI); 2945 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
2731 2946
2732 memset(pad_buf, 0, ATA_DMA_PAD_SZ); 2947 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2733 2948
@@ -2791,7 +3006,7 @@ static int ata_sg_setup(struct ata_queued_cmd *qc)
2791 int n_elem, pre_n_elem, dir, trim_sg = 0; 3006 int n_elem, pre_n_elem, dir, trim_sg = 0;
2792 3007
2793 VPRINTK("ENTER, ata%u\n", ap->id); 3008 VPRINTK("ENTER, ata%u\n", ap->id);
2794 assert(qc->flags & ATA_QCFLAG_SG); 3009 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
2795 3010
2796 /* we must lengthen transfers to end on a 32-bit boundary */ 3011 /* we must lengthen transfers to end on a 32-bit boundary */
2797 qc->pad_len = lsg->length & 3; 3012 qc->pad_len = lsg->length & 3;
@@ -2800,7 +3015,7 @@ static int ata_sg_setup(struct ata_queued_cmd *qc)
2800 struct scatterlist *psg = &qc->pad_sgent; 3015 struct scatterlist *psg = &qc->pad_sgent;
2801 unsigned int offset; 3016 unsigned int offset;
2802 3017
2803 assert(qc->dev->class == ATA_DEV_ATAPI); 3018 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
2804 3019
2805 memset(pad_buf, 0, ATA_DMA_PAD_SZ); 3020 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2806 3021
@@ -2876,7 +3091,7 @@ void ata_poll_qc_complete(struct ata_queued_cmd *qc)
2876} 3091}
2877 3092
2878/** 3093/**
2879 * ata_pio_poll - 3094 * ata_pio_poll - poll using PIO, depending on current state
2880 * @ap: the target ata_port 3095 * @ap: the target ata_port
2881 * 3096 *
2882 * LOCKING: 3097 * LOCKING:
@@ -2894,7 +3109,7 @@ static unsigned long ata_pio_poll(struct ata_port *ap)
2894 unsigned int reg_state = HSM_ST_UNKNOWN; 3109 unsigned int reg_state = HSM_ST_UNKNOWN;
2895 3110
2896 qc = ata_qc_from_tag(ap, ap->active_tag); 3111 qc = ata_qc_from_tag(ap, ap->active_tag);
2897 assert(qc != NULL); 3112 WARN_ON(qc == NULL);
2898 3113
2899 switch (ap->hsm_task_state) { 3114 switch (ap->hsm_task_state) {
2900 case HSM_ST: 3115 case HSM_ST:
@@ -2915,7 +3130,7 @@ static unsigned long ata_pio_poll(struct ata_port *ap)
2915 status = ata_chk_status(ap); 3130 status = ata_chk_status(ap);
2916 if (status & ATA_BUSY) { 3131 if (status & ATA_BUSY) {
2917 if (time_after(jiffies, ap->pio_task_timeout)) { 3132 if (time_after(jiffies, ap->pio_task_timeout)) {
2918 qc->err_mask |= AC_ERR_ATA_BUS; 3133 qc->err_mask |= AC_ERR_TIMEOUT;
2919 ap->hsm_task_state = HSM_ST_TMOUT; 3134 ap->hsm_task_state = HSM_ST_TMOUT;
2920 return 0; 3135 return 0;
2921 } 3136 }
@@ -2962,7 +3177,7 @@ static int ata_pio_complete (struct ata_port *ap)
2962 } 3177 }
2963 3178
2964 qc = ata_qc_from_tag(ap, ap->active_tag); 3179 qc = ata_qc_from_tag(ap, ap->active_tag);
2965 assert(qc != NULL); 3180 WARN_ON(qc == NULL);
2966 3181
2967 drv_stat = ata_wait_idle(ap); 3182 drv_stat = ata_wait_idle(ap);
2968 if (!ata_ok(drv_stat)) { 3183 if (!ata_ok(drv_stat)) {
@@ -2973,7 +3188,7 @@ static int ata_pio_complete (struct ata_port *ap)
2973 3188
2974 ap->hsm_task_state = HSM_ST_IDLE; 3189 ap->hsm_task_state = HSM_ST_IDLE;
2975 3190
2976 assert(qc->err_mask == 0); 3191 WARN_ON(qc->err_mask);
2977 ata_poll_qc_complete(qc); 3192 ata_poll_qc_complete(qc);
2978 3193
2979 /* another command may start at this point */ 3194 /* another command may start at this point */
@@ -2983,7 +3198,7 @@ static int ata_pio_complete (struct ata_port *ap)
2983 3198
2984 3199
2985/** 3200/**
2986 * swap_buf_le16 - swap halves of 16-words in place 3201 * swap_buf_le16 - swap halves of 16-bit words in place
2987 * @buf: Buffer to swap 3202 * @buf: Buffer to swap
2988 * @buf_words: Number of 16-bit words in buffer. 3203 * @buf_words: Number of 16-bit words in buffer.
2989 * 3204 *
@@ -3293,7 +3508,7 @@ static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3293err_out: 3508err_out:
3294 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n", 3509 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3295 ap->id, dev->devno); 3510 ap->id, dev->devno);
3296 qc->err_mask |= AC_ERR_ATA_BUS; 3511 qc->err_mask |= AC_ERR_HSM;
3297 ap->hsm_task_state = HSM_ST_ERR; 3512 ap->hsm_task_state = HSM_ST_ERR;
3298} 3513}
3299 3514
@@ -3330,7 +3545,7 @@ static void ata_pio_block(struct ata_port *ap)
3330 } 3545 }
3331 3546
3332 qc = ata_qc_from_tag(ap, ap->active_tag); 3547 qc = ata_qc_from_tag(ap, ap->active_tag);
3333 assert(qc != NULL); 3548 WARN_ON(qc == NULL);
3334 3549
3335 /* check error */ 3550 /* check error */
3336 if (status & (ATA_ERR | ATA_DF)) { 3551 if (status & (ATA_ERR | ATA_DF)) {
@@ -3351,7 +3566,7 @@ static void ata_pio_block(struct ata_port *ap)
3351 } else { 3566 } else {
3352 /* handle BSY=0, DRQ=0 as error */ 3567 /* handle BSY=0, DRQ=0 as error */
3353 if ((status & ATA_DRQ) == 0) { 3568 if ((status & ATA_DRQ) == 0) {
3354 qc->err_mask |= AC_ERR_ATA_BUS; 3569 qc->err_mask |= AC_ERR_HSM;
3355 ap->hsm_task_state = HSM_ST_ERR; 3570 ap->hsm_task_state = HSM_ST_ERR;
3356 return; 3571 return;
3357 } 3572 }
@@ -3365,7 +3580,7 @@ static void ata_pio_error(struct ata_port *ap)
3365 struct ata_queued_cmd *qc; 3580 struct ata_queued_cmd *qc;
3366 3581
3367 qc = ata_qc_from_tag(ap, ap->active_tag); 3582 qc = ata_qc_from_tag(ap, ap->active_tag);
3368 assert(qc != NULL); 3583 WARN_ON(qc == NULL);
3369 3584
3370 if (qc->tf.command != ATA_CMD_PACKET) 3585 if (qc->tf.command != ATA_CMD_PACKET)
3371 printk(KERN_WARNING "ata%u: PIO error\n", ap->id); 3586 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
@@ -3373,7 +3588,7 @@ static void ata_pio_error(struct ata_port *ap)
3373 /* make sure qc->err_mask is available to 3588 /* make sure qc->err_mask is available to
3374 * know what's wrong and recover 3589 * know what's wrong and recover
3375 */ 3590 */
3376 assert(qc->err_mask); 3591 WARN_ON(qc->err_mask == 0);
3377 3592
3378 ap->hsm_task_state = HSM_ST_IDLE; 3593 ap->hsm_task_state = HSM_ST_IDLE;
3379 3594
@@ -3414,12 +3629,84 @@ fsm_start:
3414 } 3629 }
3415 3630
3416 if (timeout) 3631 if (timeout)
3417 queue_delayed_work(ata_wq, &ap->pio_task, timeout); 3632 ata_port_queue_task(ap, ata_pio_task, ap, timeout);
3418 else if (!qc_completed) 3633 else if (!qc_completed)
3419 goto fsm_start; 3634 goto fsm_start;
3420} 3635}
3421 3636
3422/** 3637/**
3638 * atapi_packet_task - Write CDB bytes to hardware
3639 * @_data: Port to which ATAPI device is attached.
3640 *
3641 * When device has indicated its readiness to accept
3642 * a CDB, this function is called. Send the CDB.
3643 * If DMA is to be performed, exit immediately.
3644 * Otherwise, we are in polling mode, so poll
3645 * status under operation succeeds or fails.
3646 *
3647 * LOCKING:
3648 * Kernel thread context (may sleep)
3649 */
3650
3651static void atapi_packet_task(void *_data)
3652{
3653 struct ata_port *ap = _data;
3654 struct ata_queued_cmd *qc;
3655 u8 status;
3656
3657 qc = ata_qc_from_tag(ap, ap->active_tag);
3658 WARN_ON(qc == NULL);
3659 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
3660
3661 /* sleep-wait for BSY to clear */
3662 DPRINTK("busy wait\n");
3663 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
3664 qc->err_mask |= AC_ERR_TIMEOUT;
3665 goto err_out;
3666 }
3667
3668 /* make sure DRQ is set */
3669 status = ata_chk_status(ap);
3670 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3671 qc->err_mask |= AC_ERR_HSM;
3672 goto err_out;
3673 }
3674
3675 /* send SCSI cdb */
3676 DPRINTK("send cdb\n");
3677 WARN_ON(qc->dev->cdb_len < 12);
3678
3679 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3680 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3681 unsigned long flags;
3682
3683 /* Once we're done issuing command and kicking bmdma,
3684 * irq handler takes over. To not lose irq, we need
3685 * to clear NOINTR flag before sending cdb, but
3686 * interrupt handler shouldn't be invoked before we're
3687 * finished. Hence, the following locking.
3688 */
3689 spin_lock_irqsave(&ap->host_set->lock, flags);
3690 ap->flags &= ~ATA_FLAG_NOINTR;
3691 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3692 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3693 ap->ops->bmdma_start(qc); /* initiate bmdma */
3694 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3695 } else {
3696 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3697
3698 /* PIO commands are handled by polling */
3699 ap->hsm_task_state = HSM_ST;
3700 ata_port_queue_task(ap, ata_pio_task, ap, 0);
3701 }
3702
3703 return;
3704
3705err_out:
3706 ata_poll_qc_complete(qc);
3707}
3708
3709/**
3423 * ata_qc_timeout - Handle timeout of queued command 3710 * ata_qc_timeout - Handle timeout of queued command
3424 * @qc: Command that timed out 3711 * @qc: Command that timed out
3425 * 3712 *
@@ -3447,15 +3734,9 @@ static void ata_qc_timeout(struct ata_queued_cmd *qc)
3447 3734
3448 DPRINTK("ENTER\n"); 3735 DPRINTK("ENTER\n");
3449 3736
3450 spin_lock_irqsave(&host_set->lock, flags); 3737 ap->hsm_task_state = HSM_ST_IDLE;
3451 3738
3452 /* hack alert! We cannot use the supplied completion 3739 spin_lock_irqsave(&host_set->lock, flags);
3453 * function from inside the ->eh_strategy_handler() thread.
3454 * libata is the only user of ->eh_strategy_handler() in
3455 * any kernel, so the default scsi_done() assumes it is
3456 * not being called from the SCSI EH.
3457 */
3458 qc->scsidone = scsi_finish_command;
3459 3740
3460 switch (qc->tf.protocol) { 3741 switch (qc->tf.protocol) {
3461 3742
@@ -3480,12 +3761,13 @@ static void ata_qc_timeout(struct ata_queued_cmd *qc)
3480 3761
3481 /* complete taskfile transaction */ 3762 /* complete taskfile transaction */
3482 qc->err_mask |= ac_err_mask(drv_stat); 3763 qc->err_mask |= ac_err_mask(drv_stat);
3483 ata_qc_complete(qc);
3484 break; 3764 break;
3485 } 3765 }
3486 3766
3487 spin_unlock_irqrestore(&host_set->lock, flags); 3767 spin_unlock_irqrestore(&host_set->lock, flags);
3488 3768
3769 ata_eh_qc_complete(qc);
3770
3489 DPRINTK("EXIT\n"); 3771 DPRINTK("EXIT\n");
3490} 3772}
3491 3773
@@ -3510,20 +3792,10 @@ static void ata_qc_timeout(struct ata_queued_cmd *qc)
3510 3792
3511void ata_eng_timeout(struct ata_port *ap) 3793void ata_eng_timeout(struct ata_port *ap)
3512{ 3794{
3513 struct ata_queued_cmd *qc;
3514
3515 DPRINTK("ENTER\n"); 3795 DPRINTK("ENTER\n");
3516 3796
3517 qc = ata_qc_from_tag(ap, ap->active_tag); 3797 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
3518 if (qc)
3519 ata_qc_timeout(qc);
3520 else {
3521 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3522 ap->id);
3523 goto out;
3524 }
3525 3798
3526out:
3527 DPRINTK("EXIT\n"); 3799 DPRINTK("EXIT\n");
3528} 3800}
3529 3801
@@ -3579,21 +3851,6 @@ struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3579 return qc; 3851 return qc;
3580} 3852}
3581 3853
3582static void __ata_qc_complete(struct ata_queued_cmd *qc)
3583{
3584 struct ata_port *ap = qc->ap;
3585 unsigned int tag;
3586
3587 qc->flags = 0;
3588 tag = qc->tag;
3589 if (likely(ata_tag_valid(tag))) {
3590 if (tag == ap->active_tag)
3591 ap->active_tag = ATA_TAG_POISON;
3592 qc->tag = ATA_TAG_POISON;
3593 clear_bit(tag, &ap->qactive);
3594 }
3595}
3596
3597/** 3854/**
3598 * ata_qc_free - free unused ata_queued_cmd 3855 * ata_qc_free - free unused ata_queued_cmd
3599 * @qc: Command to complete 3856 * @qc: Command to complete
@@ -3606,29 +3863,25 @@ static void __ata_qc_complete(struct ata_queued_cmd *qc)
3606 */ 3863 */
3607void ata_qc_free(struct ata_queued_cmd *qc) 3864void ata_qc_free(struct ata_queued_cmd *qc)
3608{ 3865{
3609 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */ 3866 struct ata_port *ap = qc->ap;
3867 unsigned int tag;
3610 3868
3611 __ata_qc_complete(qc); 3869 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3612}
3613 3870
3614/** 3871 qc->flags = 0;
3615 * ata_qc_complete - Complete an active ATA command 3872 tag = qc->tag;
3616 * @qc: Command to complete 3873 if (likely(ata_tag_valid(tag))) {
3617 * @err_mask: ATA Status register contents 3874 if (tag == ap->active_tag)
3618 * 3875 ap->active_tag = ATA_TAG_POISON;
3619 * Indicate to the mid and upper layers that an ATA 3876 qc->tag = ATA_TAG_POISON;
3620 * command has completed, with either an ok or not-ok status. 3877 clear_bit(tag, &ap->qactive);
3621 * 3878 }
3622 * LOCKING: 3879}
3623 * spin_lock_irqsave(host_set lock)
3624 */
3625 3880
3626void ata_qc_complete(struct ata_queued_cmd *qc) 3881void __ata_qc_complete(struct ata_queued_cmd *qc)
3627{ 3882{
3628 int rc; 3883 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3629 3884 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
3630 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3631 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3632 3885
3633 if (likely(qc->flags & ATA_QCFLAG_DMAMAP)) 3886 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3634 ata_sg_clean(qc); 3887 ata_sg_clean(qc);
@@ -3640,17 +3893,7 @@ void ata_qc_complete(struct ata_queued_cmd *qc)
3640 qc->flags &= ~ATA_QCFLAG_ACTIVE; 3893 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3641 3894
3642 /* call completion callback */ 3895 /* call completion callback */
3643 rc = qc->complete_fn(qc); 3896 qc->complete_fn(qc);
3644
3645 /* if callback indicates not to complete command (non-zero),
3646 * return immediately
3647 */
3648 if (rc != 0)
3649 return;
3650
3651 __ata_qc_complete(qc);
3652
3653 VPRINTK("EXIT\n");
3654} 3897}
3655 3898
3656static inline int ata_should_dma_map(struct ata_queued_cmd *qc) 3899static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
@@ -3690,20 +3933,20 @@ static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3690 * spin_lock_irqsave(host_set lock) 3933 * spin_lock_irqsave(host_set lock)
3691 * 3934 *
3692 * RETURNS: 3935 * RETURNS:
3693 * Zero on success, negative on error. 3936 * Zero on success, AC_ERR_* mask on failure
3694 */ 3937 */
3695 3938
3696int ata_qc_issue(struct ata_queued_cmd *qc) 3939unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
3697{ 3940{
3698 struct ata_port *ap = qc->ap; 3941 struct ata_port *ap = qc->ap;
3699 3942
3700 if (ata_should_dma_map(qc)) { 3943 if (ata_should_dma_map(qc)) {
3701 if (qc->flags & ATA_QCFLAG_SG) { 3944 if (qc->flags & ATA_QCFLAG_SG) {
3702 if (ata_sg_setup(qc)) 3945 if (ata_sg_setup(qc))
3703 goto err_out; 3946 goto sg_err;
3704 } else if (qc->flags & ATA_QCFLAG_SINGLE) { 3947 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3705 if (ata_sg_setup_one(qc)) 3948 if (ata_sg_setup_one(qc))
3706 goto err_out; 3949 goto sg_err;
3707 } 3950 }
3708 } else { 3951 } else {
3709 qc->flags &= ~ATA_QCFLAG_DMAMAP; 3952 qc->flags &= ~ATA_QCFLAG_DMAMAP;
@@ -3716,8 +3959,9 @@ int ata_qc_issue(struct ata_queued_cmd *qc)
3716 3959
3717 return ap->ops->qc_issue(qc); 3960 return ap->ops->qc_issue(qc);
3718 3961
3719err_out: 3962sg_err:
3720 return -1; 3963 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3964 return AC_ERR_SYSTEM;
3721} 3965}
3722 3966
3723 3967
@@ -3736,10 +3980,10 @@ err_out:
3736 * spin_lock_irqsave(host_set lock) 3980 * spin_lock_irqsave(host_set lock)
3737 * 3981 *
3738 * RETURNS: 3982 * RETURNS:
3739 * Zero on success, negative on error. 3983 * Zero on success, AC_ERR_* mask on failure
3740 */ 3984 */
3741 3985
3742int ata_qc_issue_prot(struct ata_queued_cmd *qc) 3986unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3743{ 3987{
3744 struct ata_port *ap = qc->ap; 3988 struct ata_port *ap = qc->ap;
3745 3989
@@ -3760,31 +4004,31 @@ int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3760 ata_qc_set_polling(qc); 4004 ata_qc_set_polling(qc);
3761 ata_tf_to_host(ap, &qc->tf); 4005 ata_tf_to_host(ap, &qc->tf);
3762 ap->hsm_task_state = HSM_ST; 4006 ap->hsm_task_state = HSM_ST;
3763 queue_work(ata_wq, &ap->pio_task); 4007 ata_port_queue_task(ap, ata_pio_task, ap, 0);
3764 break; 4008 break;
3765 4009
3766 case ATA_PROT_ATAPI: 4010 case ATA_PROT_ATAPI:
3767 ata_qc_set_polling(qc); 4011 ata_qc_set_polling(qc);
3768 ata_tf_to_host(ap, &qc->tf); 4012 ata_tf_to_host(ap, &qc->tf);
3769 queue_work(ata_wq, &ap->packet_task); 4013 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
3770 break; 4014 break;
3771 4015
3772 case ATA_PROT_ATAPI_NODATA: 4016 case ATA_PROT_ATAPI_NODATA:
3773 ap->flags |= ATA_FLAG_NOINTR; 4017 ap->flags |= ATA_FLAG_NOINTR;
3774 ata_tf_to_host(ap, &qc->tf); 4018 ata_tf_to_host(ap, &qc->tf);
3775 queue_work(ata_wq, &ap->packet_task); 4019 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
3776 break; 4020 break;
3777 4021
3778 case ATA_PROT_ATAPI_DMA: 4022 case ATA_PROT_ATAPI_DMA:
3779 ap->flags |= ATA_FLAG_NOINTR; 4023 ap->flags |= ATA_FLAG_NOINTR;
3780 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */ 4024 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3781 ap->ops->bmdma_setup(qc); /* set up bmdma */ 4025 ap->ops->bmdma_setup(qc); /* set up bmdma */
3782 queue_work(ata_wq, &ap->packet_task); 4026 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
3783 break; 4027 break;
3784 4028
3785 default: 4029 default:
3786 WARN_ON(1); 4030 WARN_ON(1);
3787 return -1; 4031 return AC_ERR_SYSTEM;
3788 } 4032 }
3789 4033
3790 return 0; 4034 return 0;
@@ -4147,91 +4391,6 @@ irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4147 return IRQ_RETVAL(handled); 4391 return IRQ_RETVAL(handled);
4148} 4392}
4149 4393
4150/**
4151 * atapi_packet_task - Write CDB bytes to hardware
4152 * @_data: Port to which ATAPI device is attached.
4153 *
4154 * When device has indicated its readiness to accept
4155 * a CDB, this function is called. Send the CDB.
4156 * If DMA is to be performed, exit immediately.
4157 * Otherwise, we are in polling mode, so poll
4158 * status under operation succeeds or fails.
4159 *
4160 * LOCKING:
4161 * Kernel thread context (may sleep)
4162 */
4163
4164static void atapi_packet_task(void *_data)
4165{
4166 struct ata_port *ap = _data;
4167 struct ata_queued_cmd *qc;
4168 u8 status;
4169
4170 qc = ata_qc_from_tag(ap, ap->active_tag);
4171 assert(qc != NULL);
4172 assert(qc->flags & ATA_QCFLAG_ACTIVE);
4173
4174 /* sleep-wait for BSY to clear */
4175 DPRINTK("busy wait\n");
4176 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
4177 qc->err_mask |= AC_ERR_ATA_BUS;
4178 goto err_out;
4179 }
4180
4181 /* make sure DRQ is set */
4182 status = ata_chk_status(ap);
4183 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
4184 qc->err_mask |= AC_ERR_ATA_BUS;
4185 goto err_out;
4186 }
4187
4188 /* send SCSI cdb */
4189 DPRINTK("send cdb\n");
4190 assert(ap->cdb_len >= 12);
4191
4192 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4193 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4194 unsigned long flags;
4195
4196 /* Once we're done issuing command and kicking bmdma,
4197 * irq handler takes over. To not lose irq, we need
4198 * to clear NOINTR flag before sending cdb, but
4199 * interrupt handler shouldn't be invoked before we're
4200 * finished. Hence, the following locking.
4201 */
4202 spin_lock_irqsave(&ap->host_set->lock, flags);
4203 ap->flags &= ~ATA_FLAG_NOINTR;
4204 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4205 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4206 ap->ops->bmdma_start(qc); /* initiate bmdma */
4207 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4208 } else {
4209 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4210
4211 /* PIO commands are handled by polling */
4212 ap->hsm_task_state = HSM_ST;
4213 queue_work(ata_wq, &ap->pio_task);
4214 }
4215
4216 return;
4217
4218err_out:
4219 ata_poll_qc_complete(qc);
4220}
4221
4222
4223/**
4224 * ata_port_start - Set port up for dma.
4225 * @ap: Port to initialize
4226 *
4227 * Called just after data structures for each port are
4228 * initialized. Allocates space for PRD table.
4229 *
4230 * May be used as the port_start() entry in ata_port_operations.
4231 *
4232 * LOCKING:
4233 * Inherited from caller.
4234 */
4235 4394
4236/* 4395/*
4237 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself, 4396 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
@@ -4284,6 +4443,8 @@ static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4284 4443
4285/** 4444/**
4286 * ata_device_resume - wakeup a previously suspended devices 4445 * ata_device_resume - wakeup a previously suspended devices
4446 * @ap: port the device is connected to
4447 * @dev: the device to resume
4287 * 4448 *
4288 * Kick the drive back into action, by sending it an idle immediate 4449 * Kick the drive back into action, by sending it an idle immediate
4289 * command and making sure its transfer mode matches between drive 4450 * command and making sure its transfer mode matches between drive
@@ -4306,10 +4467,11 @@ int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4306 4467
4307/** 4468/**
4308 * ata_device_suspend - prepare a device for suspend 4469 * ata_device_suspend - prepare a device for suspend
4470 * @ap: port the device is connected to
4471 * @dev: the device to suspend
4309 * 4472 *
4310 * Flush the cache on the drive, if appropriate, then issue a 4473 * Flush the cache on the drive, if appropriate, then issue a
4311 * standbynow command. 4474 * standbynow command.
4312 *
4313 */ 4475 */
4314int ata_device_suspend(struct ata_port *ap, struct ata_device *dev) 4476int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4315{ 4477{
@@ -4323,6 +4485,19 @@ int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4323 return 0; 4485 return 0;
4324} 4486}
4325 4487
4488/**
4489 * ata_port_start - Set port up for dma.
4490 * @ap: Port to initialize
4491 *
4492 * Called just after data structures for each port are
4493 * initialized. Allocates space for PRD table.
4494 *
4495 * May be used as the port_start() entry in ata_port_operations.
4496 *
4497 * LOCKING:
4498 * Inherited from caller.
4499 */
4500
4326int ata_port_start (struct ata_port *ap) 4501int ata_port_start (struct ata_port *ap)
4327{ 4502{
4328 struct device *dev = ap->host_set->dev; 4503 struct device *dev = ap->host_set->dev;
@@ -4436,8 +4611,8 @@ static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4436 ap->active_tag = ATA_TAG_POISON; 4611 ap->active_tag = ATA_TAG_POISON;
4437 ap->last_ctl = 0xFF; 4612 ap->last_ctl = 0xFF;
4438 4613
4439 INIT_WORK(&ap->packet_task, atapi_packet_task, ap); 4614 INIT_WORK(&ap->port_task, NULL, NULL);
4440 INIT_WORK(&ap->pio_task, ata_pio_task, ap); 4615 INIT_LIST_HEAD(&ap->eh_done_q);
4441 4616
4442 for (i = 0; i < ATA_MAX_DEVICES; i++) 4617 for (i = 0; i < ATA_MAX_DEVICES; i++)
4443 ap->device[i].devno = i; 4618 ap->device[i].devno = i;
@@ -4579,9 +4754,9 @@ int ata_device_add(const struct ata_probe_ent *ent)
4579 4754
4580 ap = host_set->ports[i]; 4755 ap = host_set->ports[i];
4581 4756
4582 DPRINTK("ata%u: probe begin\n", ap->id); 4757 DPRINTK("ata%u: bus probe begin\n", ap->id);
4583 rc = ata_bus_probe(ap); 4758 rc = ata_bus_probe(ap);
4584 DPRINTK("ata%u: probe end\n", ap->id); 4759 DPRINTK("ata%u: bus probe end\n", ap->id);
4585 4760
4586 if (rc) { 4761 if (rc) {
4587 /* FIXME: do something useful here? 4762 /* FIXME: do something useful here?
@@ -4605,7 +4780,7 @@ int ata_device_add(const struct ata_probe_ent *ent)
4605 } 4780 }
4606 4781
4607 /* probes are done, now scan each port's disk(s) */ 4782 /* probes are done, now scan each port's disk(s) */
4608 DPRINTK("probe begin\n"); 4783 DPRINTK("host probe begin\n");
4609 for (i = 0; i < count; i++) { 4784 for (i = 0; i < count; i++) {
4610 struct ata_port *ap = host_set->ports[i]; 4785 struct ata_port *ap = host_set->ports[i];
4611 4786
@@ -4691,11 +4866,14 @@ void ata_host_set_remove(struct ata_host_set *host_set)
4691int ata_scsi_release(struct Scsi_Host *host) 4866int ata_scsi_release(struct Scsi_Host *host)
4692{ 4867{
4693 struct ata_port *ap = (struct ata_port *) &host->hostdata[0]; 4868 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4869 int i;
4694 4870
4695 DPRINTK("ENTER\n"); 4871 DPRINTK("ENTER\n");
4696 4872
4697 ap->ops->port_disable(ap); 4873 ap->ops->port_disable(ap);
4698 ata_host_remove(ap, 0); 4874 ata_host_remove(ap, 0);
4875 for (i = 0; i < ATA_MAX_DEVICES; i++)
4876 kfree(ap->device[i].id);
4699 4877
4700 DPRINTK("EXIT\n"); 4878 DPRINTK("EXIT\n");
4701 return 1; 4879 return 1;
@@ -4727,32 +4905,6 @@ void ata_std_ports(struct ata_ioports *ioaddr)
4727 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD; 4905 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4728} 4906}
4729 4907
4730static struct ata_probe_ent *
4731ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
4732{
4733 struct ata_probe_ent *probe_ent;
4734
4735 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
4736 if (!probe_ent) {
4737 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4738 kobject_name(&(dev->kobj)));
4739 return NULL;
4740 }
4741
4742 INIT_LIST_HEAD(&probe_ent->node);
4743 probe_ent->dev = dev;
4744
4745 probe_ent->sht = port->sht;
4746 probe_ent->host_flags = port->host_flags;
4747 probe_ent->pio_mask = port->pio_mask;
4748 probe_ent->mwdma_mask = port->mwdma_mask;
4749 probe_ent->udma_mask = port->udma_mask;
4750 probe_ent->port_ops = port->port_ops;
4751
4752 return probe_ent;
4753}
4754
4755
4756 4908
4757#ifdef CONFIG_PCI 4909#ifdef CONFIG_PCI
4758 4910
@@ -4764,256 +4916,6 @@ void ata_pci_host_stop (struct ata_host_set *host_set)
4764} 4916}
4765 4917
4766/** 4918/**
4767 * ata_pci_init_native_mode - Initialize native-mode driver
4768 * @pdev: pci device to be initialized
4769 * @port: array[2] of pointers to port info structures.
4770 * @ports: bitmap of ports present
4771 *
4772 * Utility function which allocates and initializes an
4773 * ata_probe_ent structure for a standard dual-port
4774 * PIO-based IDE controller. The returned ata_probe_ent
4775 * structure can be passed to ata_device_add(). The returned
4776 * ata_probe_ent structure should then be freed with kfree().
4777 *
4778 * The caller need only pass the address of the primary port, the
4779 * secondary will be deduced automatically. If the device has non
4780 * standard secondary port mappings this function can be called twice,
4781 * once for each interface.
4782 */
4783
4784struct ata_probe_ent *
4785ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
4786{
4787 struct ata_probe_ent *probe_ent =
4788 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4789 int p = 0;
4790
4791 if (!probe_ent)
4792 return NULL;
4793
4794 probe_ent->irq = pdev->irq;
4795 probe_ent->irq_flags = SA_SHIRQ;
4796 probe_ent->private_data = port[0]->private_data;
4797
4798 if (ports & ATA_PORT_PRIMARY) {
4799 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4800 probe_ent->port[p].altstatus_addr =
4801 probe_ent->port[p].ctl_addr =
4802 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4803 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4804 ata_std_ports(&probe_ent->port[p]);
4805 p++;
4806 }
4807
4808 if (ports & ATA_PORT_SECONDARY) {
4809 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4810 probe_ent->port[p].altstatus_addr =
4811 probe_ent->port[p].ctl_addr =
4812 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4813 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4814 ata_std_ports(&probe_ent->port[p]);
4815 p++;
4816 }
4817
4818 probe_ent->n_ports = p;
4819 return probe_ent;
4820}
4821
4822static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev, struct ata_port_info *port, int port_num)
4823{
4824 struct ata_probe_ent *probe_ent;
4825
4826 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
4827 if (!probe_ent)
4828 return NULL;
4829
4830 probe_ent->legacy_mode = 1;
4831 probe_ent->n_ports = 1;
4832 probe_ent->hard_port_no = port_num;
4833 probe_ent->private_data = port->private_data;
4834
4835 switch(port_num)
4836 {
4837 case 0:
4838 probe_ent->irq = 14;
4839 probe_ent->port[0].cmd_addr = 0x1f0;
4840 probe_ent->port[0].altstatus_addr =
4841 probe_ent->port[0].ctl_addr = 0x3f6;
4842 break;
4843 case 1:
4844 probe_ent->irq = 15;
4845 probe_ent->port[0].cmd_addr = 0x170;
4846 probe_ent->port[0].altstatus_addr =
4847 probe_ent->port[0].ctl_addr = 0x376;
4848 break;
4849 }
4850 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
4851 ata_std_ports(&probe_ent->port[0]);
4852 return probe_ent;
4853}
4854
4855/**
4856 * ata_pci_init_one - Initialize/register PCI IDE host controller
4857 * @pdev: Controller to be initialized
4858 * @port_info: Information from low-level host driver
4859 * @n_ports: Number of ports attached to host controller
4860 *
4861 * This is a helper function which can be called from a driver's
4862 * xxx_init_one() probe function if the hardware uses traditional
4863 * IDE taskfile registers.
4864 *
4865 * This function calls pci_enable_device(), reserves its register
4866 * regions, sets the dma mask, enables bus master mode, and calls
4867 * ata_device_add()
4868 *
4869 * LOCKING:
4870 * Inherited from PCI layer (may sleep).
4871 *
4872 * RETURNS:
4873 * Zero on success, negative on errno-based value on error.
4874 */
4875
4876int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4877 unsigned int n_ports)
4878{
4879 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
4880 struct ata_port_info *port[2];
4881 u8 tmp8, mask;
4882 unsigned int legacy_mode = 0;
4883 int disable_dev_on_err = 1;
4884 int rc;
4885
4886 DPRINTK("ENTER\n");
4887
4888 port[0] = port_info[0];
4889 if (n_ports > 1)
4890 port[1] = port_info[1];
4891 else
4892 port[1] = port[0];
4893
4894 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4895 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
4896 /* TODO: What if one channel is in native mode ... */
4897 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4898 mask = (1 << 2) | (1 << 0);
4899 if ((tmp8 & mask) != mask)
4900 legacy_mode = (1 << 3);
4901 }
4902
4903 /* FIXME... */
4904 if ((!legacy_mode) && (n_ports > 2)) {
4905 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4906 n_ports = 2;
4907 /* For now */
4908 }
4909
4910 /* FIXME: Really for ATA it isn't safe because the device may be
4911 multi-purpose and we want to leave it alone if it was already
4912 enabled. Secondly for shared use as Arjan says we want refcounting
4913
4914 Checking dev->is_enabled is insufficient as this is not set at
4915 boot for the primary video which is BIOS enabled
4916 */
4917
4918 rc = pci_enable_device(pdev);
4919 if (rc)
4920 return rc;
4921
4922 rc = pci_request_regions(pdev, DRV_NAME);
4923 if (rc) {
4924 disable_dev_on_err = 0;
4925 goto err_out;
4926 }
4927
4928 /* FIXME: Should use platform specific mappers for legacy port ranges */
4929 if (legacy_mode) {
4930 if (!request_region(0x1f0, 8, "libata")) {
4931 struct resource *conflict, res;
4932 res.start = 0x1f0;
4933 res.end = 0x1f0 + 8 - 1;
4934 conflict = ____request_resource(&ioport_resource, &res);
4935 if (!strcmp(conflict->name, "libata"))
4936 legacy_mode |= (1 << 0);
4937 else {
4938 disable_dev_on_err = 0;
4939 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4940 }
4941 } else
4942 legacy_mode |= (1 << 0);
4943
4944 if (!request_region(0x170, 8, "libata")) {
4945 struct resource *conflict, res;
4946 res.start = 0x170;
4947 res.end = 0x170 + 8 - 1;
4948 conflict = ____request_resource(&ioport_resource, &res);
4949 if (!strcmp(conflict->name, "libata"))
4950 legacy_mode |= (1 << 1);
4951 else {
4952 disable_dev_on_err = 0;
4953 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4954 }
4955 } else
4956 legacy_mode |= (1 << 1);
4957 }
4958
4959 /* we have legacy mode, but all ports are unavailable */
4960 if (legacy_mode == (1 << 3)) {
4961 rc = -EBUSY;
4962 goto err_out_regions;
4963 }
4964
4965 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4966 if (rc)
4967 goto err_out_regions;
4968 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4969 if (rc)
4970 goto err_out_regions;
4971
4972 if (legacy_mode) {
4973 if (legacy_mode & (1 << 0))
4974 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
4975 if (legacy_mode & (1 << 1))
4976 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
4977 } else {
4978 if (n_ports == 2)
4979 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4980 else
4981 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4982 }
4983 if (!probe_ent && !probe_ent2) {
4984 rc = -ENOMEM;
4985 goto err_out_regions;
4986 }
4987
4988 pci_set_master(pdev);
4989
4990 /* FIXME: check ata_device_add return */
4991 if (legacy_mode) {
4992 if (legacy_mode & (1 << 0))
4993 ata_device_add(probe_ent);
4994 if (legacy_mode & (1 << 1))
4995 ata_device_add(probe_ent2);
4996 } else
4997 ata_device_add(probe_ent);
4998
4999 kfree(probe_ent);
5000 kfree(probe_ent2);
5001
5002 return 0;
5003
5004err_out_regions:
5005 if (legacy_mode & (1 << 0))
5006 release_region(0x1f0, 8);
5007 if (legacy_mode & (1 << 1))
5008 release_region(0x170, 8);
5009 pci_release_regions(pdev);
5010err_out:
5011 if (disable_dev_on_err)
5012 pci_disable_device(pdev);
5013 return rc;
5014}
5015
5016/**
5017 * ata_pci_remove_one - PCI layer callback for device removal 4919 * ata_pci_remove_one - PCI layer callback for device removal
5018 * @pdev: PCI device that was removed 4920 * @pdev: PCI device that was removed
5019 * 4921 *
@@ -5143,7 +5045,7 @@ EXPORT_SYMBOL_GPL(ata_device_add);
5143EXPORT_SYMBOL_GPL(ata_host_set_remove); 5045EXPORT_SYMBOL_GPL(ata_host_set_remove);
5144EXPORT_SYMBOL_GPL(ata_sg_init); 5046EXPORT_SYMBOL_GPL(ata_sg_init);
5145EXPORT_SYMBOL_GPL(ata_sg_init_one); 5047EXPORT_SYMBOL_GPL(ata_sg_init_one);
5146EXPORT_SYMBOL_GPL(ata_qc_complete); 5048EXPORT_SYMBOL_GPL(__ata_qc_complete);
5147EXPORT_SYMBOL_GPL(ata_qc_issue_prot); 5049EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5148EXPORT_SYMBOL_GPL(ata_eng_timeout); 5050EXPORT_SYMBOL_GPL(ata_eng_timeout);
5149EXPORT_SYMBOL_GPL(ata_tf_load); 5051EXPORT_SYMBOL_GPL(ata_tf_load);
@@ -5169,18 +5071,30 @@ EXPORT_SYMBOL_GPL(ata_port_probe);
5169EXPORT_SYMBOL_GPL(sata_phy_reset); 5071EXPORT_SYMBOL_GPL(sata_phy_reset);
5170EXPORT_SYMBOL_GPL(__sata_phy_reset); 5072EXPORT_SYMBOL_GPL(__sata_phy_reset);
5171EXPORT_SYMBOL_GPL(ata_bus_reset); 5073EXPORT_SYMBOL_GPL(ata_bus_reset);
5074EXPORT_SYMBOL_GPL(ata_std_probeinit);
5075EXPORT_SYMBOL_GPL(ata_std_softreset);
5076EXPORT_SYMBOL_GPL(sata_std_hardreset);
5077EXPORT_SYMBOL_GPL(ata_std_postreset);
5078EXPORT_SYMBOL_GPL(ata_std_probe_reset);
5079EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
5080EXPORT_SYMBOL_GPL(ata_dev_revalidate);
5172EXPORT_SYMBOL_GPL(ata_port_disable); 5081EXPORT_SYMBOL_GPL(ata_port_disable);
5173EXPORT_SYMBOL_GPL(ata_ratelimit); 5082EXPORT_SYMBOL_GPL(ata_ratelimit);
5083EXPORT_SYMBOL_GPL(ata_busy_sleep);
5084EXPORT_SYMBOL_GPL(ata_port_queue_task);
5174EXPORT_SYMBOL_GPL(ata_scsi_ioctl); 5085EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5175EXPORT_SYMBOL_GPL(ata_scsi_queuecmd); 5086EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5087EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
5176EXPORT_SYMBOL_GPL(ata_scsi_error); 5088EXPORT_SYMBOL_GPL(ata_scsi_error);
5177EXPORT_SYMBOL_GPL(ata_scsi_slave_config); 5089EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5178EXPORT_SYMBOL_GPL(ata_scsi_release); 5090EXPORT_SYMBOL_GPL(ata_scsi_release);
5179EXPORT_SYMBOL_GPL(ata_host_intr); 5091EXPORT_SYMBOL_GPL(ata_host_intr);
5180EXPORT_SYMBOL_GPL(ata_dev_classify); 5092EXPORT_SYMBOL_GPL(ata_dev_classify);
5181EXPORT_SYMBOL_GPL(ata_dev_id_string); 5093EXPORT_SYMBOL_GPL(ata_id_string);
5182EXPORT_SYMBOL_GPL(ata_dev_config); 5094EXPORT_SYMBOL_GPL(ata_id_c_string);
5183EXPORT_SYMBOL_GPL(ata_scsi_simulate); 5095EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5096EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5097EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
5184 5098
5185EXPORT_SYMBOL_GPL(ata_pio_need_iordy); 5099EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
5186EXPORT_SYMBOL_GPL(ata_timing_compute); 5100EXPORT_SYMBOL_GPL(ata_timing_compute);