]>
Commit | Line | Data |
---|---|---|
1fdffbce JG |
1 | /* |
2 | * libata-bmdma.c - helper library for PCI IDE BMDMA | |
3 | * | |
4 | * Maintained by: Jeff Garzik <jgarzik@pobox.com> | |
5 | * Please ALWAYS copy linux-ide@vger.kernel.org | |
6 | * on emails. | |
7 | * | |
8 | * Copyright 2003-2006 Red Hat, Inc. All rights reserved. | |
9 | * Copyright 2003-2006 Jeff Garzik | |
10 | * | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2, or (at your option) | |
15 | * any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, | |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU General Public License | |
23 | * along with this program; see the file COPYING. If not, write to | |
24 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
25 | * | |
26 | * | |
27 | * libata documentation is available via 'make {ps|pdf}docs', | |
28 | * as Documentation/DocBook/libata.* | |
29 | * | |
30 | * Hardware documentation available from http://www.t13.org/ and | |
31 | * http://www.sata-io.org/ | |
32 | * | |
33 | */ | |
34 | ||
1fdffbce JG |
35 | #include <linux/kernel.h> |
36 | #include <linux/pci.h> | |
37 | #include <linux/libata.h> | |
38 | ||
39 | #include "libata.h" | |
40 | ||
90088bb4 TH |
41 | /** |
42 | * ata_irq_on - Enable interrupts on a port. | |
43 | * @ap: Port on which interrupts are enabled. | |
44 | * | |
45 | * Enable interrupts on a legacy IDE device using MMIO or PIO, | |
46 | * wait for idle, clear any pending interrupts. | |
47 | * | |
48 | * LOCKING: | |
49 | * Inherited from caller. | |
50 | */ | |
51 | u8 ata_irq_on(struct ata_port *ap) | |
52 | { | |
53 | struct ata_ioports *ioaddr = &ap->ioaddr; | |
54 | u8 tmp; | |
55 | ||
56 | ap->ctl &= ~ATA_NIEN; | |
57 | ap->last_ctl = ap->ctl; | |
58 | ||
0d5ff566 | 59 | iowrite8(ap->ctl, ioaddr->ctl_addr); |
90088bb4 TH |
60 | tmp = ata_wait_idle(ap); |
61 | ||
62 | ap->ops->irq_clear(ap); | |
63 | ||
64 | return tmp; | |
65 | } | |
66 | ||
83625006 AI |
67 | u8 ata_dummy_irq_on (struct ata_port *ap) { return 0; } |
68 | ||
69 | /** | |
70 | * ata_irq_ack - Acknowledge a device interrupt. | |
71 | * @ap: Port on which interrupts are enabled. | |
72 | * | |
73 | * Wait up to 10 ms for legacy IDE device to become idle (BUSY | |
74 | * or BUSY+DRQ clear). Obtain dma status and port status from | |
75 | * device. Clear the interrupt. Return port status. | |
76 | * | |
77 | * LOCKING: | |
78 | */ | |
79 | ||
80 | u8 ata_irq_ack(struct ata_port *ap, unsigned int chk_drq) | |
81 | { | |
82 | unsigned int bits = chk_drq ? ATA_BUSY | ATA_DRQ : ATA_BUSY; | |
83 | u8 host_stat, post_stat, status; | |
84 | ||
85 | status = ata_busy_wait(ap, bits, 1000); | |
86 | if (status & bits) | |
87 | if (ata_msg_err(ap)) | |
88 | printk(KERN_ERR "abnormal status 0x%X\n", status); | |
89 | ||
90 | /* get controller status; clear intr, err bits */ | |
91 | host_stat = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS); | |
92 | iowrite8(host_stat | ATA_DMA_INTR | ATA_DMA_ERR, | |
93 | ap->ioaddr.bmdma_addr + ATA_DMA_STATUS); | |
94 | ||
95 | post_stat = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS); | |
96 | ||
97 | if (ata_msg_intr(ap)) | |
98 | printk(KERN_INFO "%s: irq ack: host_stat 0x%X, new host_stat 0x%X, drv_stat 0x%X\n", | |
99 | __FUNCTION__, | |
100 | host_stat, post_stat, status); | |
101 | ||
102 | return status; | |
103 | } | |
104 | ||
105 | u8 ata_dummy_irq_ack(struct ata_port *ap, unsigned int chk_drq) { return 0; } | |
106 | ||
1fdffbce | 107 | /** |
0d5ff566 | 108 | * ata_tf_load - send taskfile registers to host controller |
1fdffbce JG |
109 | * @ap: Port to which output is sent |
110 | * @tf: ATA taskfile register set | |
111 | * | |
112 | * Outputs ATA taskfile to standard ATA host controller. | |
113 | * | |
114 | * LOCKING: | |
115 | * Inherited from caller. | |
116 | */ | |
117 | ||
0d5ff566 | 118 | void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf) |
1fdffbce JG |
119 | { |
120 | struct ata_ioports *ioaddr = &ap->ioaddr; | |
121 | unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR; | |
122 | ||
123 | if (tf->ctl != ap->last_ctl) { | |
0d5ff566 | 124 | iowrite8(tf->ctl, ioaddr->ctl_addr); |
1fdffbce JG |
125 | ap->last_ctl = tf->ctl; |
126 | ata_wait_idle(ap); | |
127 | } | |
128 | ||
129 | if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) { | |
0d5ff566 TH |
130 | iowrite8(tf->hob_feature, ioaddr->feature_addr); |
131 | iowrite8(tf->hob_nsect, ioaddr->nsect_addr); | |
132 | iowrite8(tf->hob_lbal, ioaddr->lbal_addr); | |
133 | iowrite8(tf->hob_lbam, ioaddr->lbam_addr); | |
134 | iowrite8(tf->hob_lbah, ioaddr->lbah_addr); | |
1fdffbce JG |
135 | VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n", |
136 | tf->hob_feature, | |
137 | tf->hob_nsect, | |
138 | tf->hob_lbal, | |
139 | tf->hob_lbam, | |
140 | tf->hob_lbah); | |
141 | } | |
142 | ||
143 | if (is_addr) { | |
0d5ff566 TH |
144 | iowrite8(tf->feature, ioaddr->feature_addr); |
145 | iowrite8(tf->nsect, ioaddr->nsect_addr); | |
146 | iowrite8(tf->lbal, ioaddr->lbal_addr); | |
147 | iowrite8(tf->lbam, ioaddr->lbam_addr); | |
148 | iowrite8(tf->lbah, ioaddr->lbah_addr); | |
1fdffbce JG |
149 | VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n", |
150 | tf->feature, | |
151 | tf->nsect, | |
152 | tf->lbal, | |
153 | tf->lbam, | |
154 | tf->lbah); | |
155 | } | |
156 | ||
157 | if (tf->flags & ATA_TFLAG_DEVICE) { | |
0d5ff566 | 158 | iowrite8(tf->device, ioaddr->device_addr); |
1fdffbce JG |
159 | VPRINTK("device 0x%X\n", tf->device); |
160 | } | |
161 | ||
162 | ata_wait_idle(ap); | |
163 | } | |
164 | ||
1fdffbce | 165 | /** |
0d5ff566 | 166 | * ata_exec_command - issue ATA command to host controller |
1fdffbce JG |
167 | * @ap: port to which command is being issued |
168 | * @tf: ATA taskfile register set | |
169 | * | |
0d5ff566 TH |
170 | * Issues ATA command, with proper synchronization with interrupt |
171 | * handler / other threads. | |
7c74ffd0 | 172 | * |
1fdffbce | 173 | * LOCKING: |
cca3974e | 174 | * spin_lock_irqsave(host lock) |
1fdffbce | 175 | */ |
0d5ff566 | 176 | void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf) |
1fdffbce | 177 | { |
44877b4e | 178 | DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command); |
1fdffbce | 179 | |
0d5ff566 | 180 | iowrite8(tf->command, ap->ioaddr.command_addr); |
1fdffbce JG |
181 | ata_pause(ap); |
182 | } | |
183 | ||
1fdffbce | 184 | /** |
0d5ff566 | 185 | * ata_tf_read - input device's ATA taskfile shadow registers |
1fdffbce JG |
186 | * @ap: Port from which input is read |
187 | * @tf: ATA taskfile register set for storing input | |
188 | * | |
189 | * Reads ATA taskfile registers for currently-selected device | |
190 | * into @tf. | |
191 | * | |
192 | * LOCKING: | |
193 | * Inherited from caller. | |
194 | */ | |
0d5ff566 | 195 | void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf) |
1fdffbce JG |
196 | { |
197 | struct ata_ioports *ioaddr = &ap->ioaddr; | |
198 | ||
199 | tf->command = ata_check_status(ap); | |
0d5ff566 TH |
200 | tf->feature = ioread8(ioaddr->error_addr); |
201 | tf->nsect = ioread8(ioaddr->nsect_addr); | |
202 | tf->lbal = ioread8(ioaddr->lbal_addr); | |
203 | tf->lbam = ioread8(ioaddr->lbam_addr); | |
204 | tf->lbah = ioread8(ioaddr->lbah_addr); | |
205 | tf->device = ioread8(ioaddr->device_addr); | |
1fdffbce JG |
206 | |
207 | if (tf->flags & ATA_TFLAG_LBA48) { | |
0d5ff566 TH |
208 | iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr); |
209 | tf->hob_feature = ioread8(ioaddr->error_addr); | |
210 | tf->hob_nsect = ioread8(ioaddr->nsect_addr); | |
211 | tf->hob_lbal = ioread8(ioaddr->lbal_addr); | |
212 | tf->hob_lbam = ioread8(ioaddr->lbam_addr); | |
213 | tf->hob_lbah = ioread8(ioaddr->lbah_addr); | |
1fdffbce JG |
214 | } |
215 | } | |
216 | ||
1fdffbce JG |
217 | /** |
218 | * ata_check_status - Read device status reg & clear interrupt | |
219 | * @ap: port where the device is | |
220 | * | |
221 | * Reads ATA taskfile status register for currently-selected device | |
222 | * and return its value. This also clears pending interrupts | |
223 | * from this device | |
224 | * | |
1fdffbce JG |
225 | * LOCKING: |
226 | * Inherited from caller. | |
227 | */ | |
228 | u8 ata_check_status(struct ata_port *ap) | |
229 | { | |
0d5ff566 | 230 | return ioread8(ap->ioaddr.status_addr); |
1fdffbce JG |
231 | } |
232 | ||
1fdffbce JG |
233 | /** |
234 | * ata_altstatus - Read device alternate status reg | |
235 | * @ap: port where the device is | |
236 | * | |
237 | * Reads ATA taskfile alternate status register for | |
238 | * currently-selected device and return its value. | |
239 | * | |
240 | * Note: may NOT be used as the check_altstatus() entry in | |
241 | * ata_port_operations. | |
242 | * | |
243 | * LOCKING: | |
244 | * Inherited from caller. | |
245 | */ | |
246 | u8 ata_altstatus(struct ata_port *ap) | |
247 | { | |
248 | if (ap->ops->check_altstatus) | |
249 | return ap->ops->check_altstatus(ap); | |
250 | ||
0d5ff566 | 251 | return ioread8(ap->ioaddr.altstatus_addr); |
1fdffbce JG |
252 | } |
253 | ||
2cc432ee | 254 | /** |
0d5ff566 | 255 | * ata_bmdma_setup - Set up PCI IDE BMDMA transaction |
2cc432ee JG |
256 | * @qc: Info associated with this ATA transaction. |
257 | * | |
258 | * LOCKING: | |
cca3974e | 259 | * spin_lock_irqsave(host lock) |
2cc432ee | 260 | */ |
0d5ff566 | 261 | void ata_bmdma_setup(struct ata_queued_cmd *qc) |
2cc432ee JG |
262 | { |
263 | struct ata_port *ap = qc->ap; | |
264 | unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE); | |
265 | u8 dmactl; | |
2cc432ee JG |
266 | |
267 | /* load PRD table addr. */ | |
268 | mb(); /* make sure PRD table writes are visible to controller */ | |
0d5ff566 | 269 | iowrite32(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS); |
2cc432ee JG |
270 | |
271 | /* specify data direction, triple-check start bit is clear */ | |
0d5ff566 | 272 | dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD); |
2cc432ee JG |
273 | dmactl &= ~(ATA_DMA_WR | ATA_DMA_START); |
274 | if (!rw) | |
275 | dmactl |= ATA_DMA_WR; | |
0d5ff566 | 276 | iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD); |
2cc432ee JG |
277 | |
278 | /* issue r/w command */ | |
279 | ap->ops->exec_command(ap, &qc->tf); | |
280 | } | |
281 | ||
282 | /** | |
0d5ff566 | 283 | * ata_bmdma_start - Start a PCI IDE BMDMA transaction |
2cc432ee JG |
284 | * @qc: Info associated with this ATA transaction. |
285 | * | |
286 | * LOCKING: | |
cca3974e | 287 | * spin_lock_irqsave(host lock) |
2cc432ee | 288 | */ |
0d5ff566 | 289 | void ata_bmdma_start (struct ata_queued_cmd *qc) |
2cc432ee JG |
290 | { |
291 | struct ata_port *ap = qc->ap; | |
2cc432ee JG |
292 | u8 dmactl; |
293 | ||
294 | /* start host DMA transaction */ | |
0d5ff566 TH |
295 | dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD); |
296 | iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD); | |
2cc432ee JG |
297 | |
298 | /* Strictly, one may wish to issue a readb() here, to | |
299 | * flush the mmio write. However, control also passes | |
300 | * to the hardware at this point, and it will interrupt | |
301 | * us when we are to resume control. So, in effect, | |
302 | * we don't care when the mmio write flushes. | |
303 | * Further, a read of the DMA status register _immediately_ | |
304 | * following the write may not be what certain flaky hardware | |
305 | * is expected, so I think it is best to not add a readb() | |
306 | * without first all the MMIO ATA cards/mobos. | |
307 | * Or maybe I'm just being paranoid. | |
308 | */ | |
309 | } | |
310 | ||
2cc432ee JG |
311 | /** |
312 | * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt. | |
313 | * @ap: Port associated with this ATA transaction. | |
314 | * | |
315 | * Clear interrupt and error flags in DMA status register. | |
316 | * | |
317 | * May be used as the irq_clear() entry in ata_port_operations. | |
318 | * | |
319 | * LOCKING: | |
cca3974e | 320 | * spin_lock_irqsave(host lock) |
2cc432ee | 321 | */ |
2cc432ee JG |
322 | void ata_bmdma_irq_clear(struct ata_port *ap) |
323 | { | |
0d5ff566 TH |
324 | void __iomem *mmio = ap->ioaddr.bmdma_addr; |
325 | ||
326 | if (!mmio) | |
2cc432ee JG |
327 | return; |
328 | ||
0d5ff566 | 329 | iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS); |
2cc432ee JG |
330 | } |
331 | ||
2cc432ee JG |
332 | /** |
333 | * ata_bmdma_status - Read PCI IDE BMDMA status | |
334 | * @ap: Port associated with this ATA transaction. | |
335 | * | |
336 | * Read and return BMDMA status register. | |
337 | * | |
338 | * May be used as the bmdma_status() entry in ata_port_operations. | |
339 | * | |
340 | * LOCKING: | |
cca3974e | 341 | * spin_lock_irqsave(host lock) |
2cc432ee | 342 | */ |
2cc432ee JG |
343 | u8 ata_bmdma_status(struct ata_port *ap) |
344 | { | |
0d5ff566 | 345 | return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS); |
2cc432ee JG |
346 | } |
347 | ||
2cc432ee JG |
348 | /** |
349 | * ata_bmdma_stop - Stop PCI IDE BMDMA transfer | |
350 | * @qc: Command we are ending DMA for | |
351 | * | |
352 | * Clears the ATA_DMA_START flag in the dma control register | |
353 | * | |
354 | * May be used as the bmdma_stop() entry in ata_port_operations. | |
355 | * | |
356 | * LOCKING: | |
cca3974e | 357 | * spin_lock_irqsave(host lock) |
2cc432ee | 358 | */ |
2cc432ee JG |
359 | void ata_bmdma_stop(struct ata_queued_cmd *qc) |
360 | { | |
361 | struct ata_port *ap = qc->ap; | |
0d5ff566 TH |
362 | void __iomem *mmio = ap->ioaddr.bmdma_addr; |
363 | ||
364 | /* clear start/stop bit */ | |
365 | iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START, | |
366 | mmio + ATA_DMA_CMD); | |
2cc432ee JG |
367 | |
368 | /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */ | |
369 | ata_altstatus(ap); /* dummy read */ | |
370 | } | |
371 | ||
6d97dbd7 TH |
372 | /** |
373 | * ata_bmdma_freeze - Freeze BMDMA controller port | |
374 | * @ap: port to freeze | |
375 | * | |
376 | * Freeze BMDMA controller port. | |
377 | * | |
378 | * LOCKING: | |
379 | * Inherited from caller. | |
380 | */ | |
381 | void ata_bmdma_freeze(struct ata_port *ap) | |
382 | { | |
383 | struct ata_ioports *ioaddr = &ap->ioaddr; | |
384 | ||
385 | ap->ctl |= ATA_NIEN; | |
386 | ap->last_ctl = ap->ctl; | |
387 | ||
0d5ff566 | 388 | iowrite8(ap->ctl, ioaddr->ctl_addr); |
0f0a3ad3 TH |
389 | |
390 | /* Under certain circumstances, some controllers raise IRQ on | |
391 | * ATA_NIEN manipulation. Also, many controllers fail to mask | |
392 | * previously pending IRQ on ATA_NIEN assertion. Clear it. | |
393 | */ | |
394 | ata_chk_status(ap); | |
395 | ||
396 | ap->ops->irq_clear(ap); | |
6d97dbd7 TH |
397 | } |
398 | ||
399 | /** | |
400 | * ata_bmdma_thaw - Thaw BMDMA controller port | |
401 | * @ap: port to thaw | |
402 | * | |
403 | * Thaw BMDMA controller port. | |
404 | * | |
405 | * LOCKING: | |
406 | * Inherited from caller. | |
407 | */ | |
408 | void ata_bmdma_thaw(struct ata_port *ap) | |
409 | { | |
410 | /* clear & re-enable interrupts */ | |
411 | ata_chk_status(ap); | |
412 | ap->ops->irq_clear(ap); | |
83625006 | 413 | ap->ops->irq_on(ap); |
6d97dbd7 TH |
414 | } |
415 | ||
416 | /** | |
417 | * ata_bmdma_drive_eh - Perform EH with given methods for BMDMA controller | |
418 | * @ap: port to handle error for | |
f5914a46 | 419 | * @prereset: prereset method (can be NULL) |
6d97dbd7 TH |
420 | * @softreset: softreset method (can be NULL) |
421 | * @hardreset: hardreset method (can be NULL) | |
422 | * @postreset: postreset method (can be NULL) | |
423 | * | |
424 | * Handle error for ATA BMDMA controller. It can handle both | |
425 | * PATA and SATA controllers. Many controllers should be able to | |
426 | * use this EH as-is or with some added handling before and | |
427 | * after. | |
428 | * | |
429 | * This function is intended to be used for constructing | |
430 | * ->error_handler callback by low level drivers. | |
431 | * | |
432 | * LOCKING: | |
433 | * Kernel thread context (may sleep) | |
434 | */ | |
f5914a46 TH |
435 | void ata_bmdma_drive_eh(struct ata_port *ap, ata_prereset_fn_t prereset, |
436 | ata_reset_fn_t softreset, ata_reset_fn_t hardreset, | |
437 | ata_postreset_fn_t postreset) | |
6d97dbd7 | 438 | { |
6d97dbd7 TH |
439 | struct ata_queued_cmd *qc; |
440 | unsigned long flags; | |
441 | int thaw = 0; | |
442 | ||
443 | qc = __ata_qc_from_tag(ap, ap->active_tag); | |
444 | if (qc && !(qc->flags & ATA_QCFLAG_FAILED)) | |
445 | qc = NULL; | |
446 | ||
447 | /* reset PIO HSM and stop DMA engine */ | |
ba6a1308 | 448 | spin_lock_irqsave(ap->lock, flags); |
6d97dbd7 | 449 | |
6d97dbd7 TH |
450 | ap->hsm_task_state = HSM_ST_IDLE; |
451 | ||
452 | if (qc && (qc->tf.protocol == ATA_PROT_DMA || | |
453 | qc->tf.protocol == ATA_PROT_ATAPI_DMA)) { | |
454 | u8 host_stat; | |
455 | ||
fbbb262d | 456 | host_stat = ap->ops->bmdma_status(ap); |
6d97dbd7 | 457 | |
6d97dbd7 TH |
458 | /* BMDMA controllers indicate host bus error by |
459 | * setting DMA_ERR bit and timing out. As it wasn't | |
460 | * really a timeout event, adjust error mask and | |
461 | * cancel frozen state. | |
462 | */ | |
18d90deb | 463 | if (qc->err_mask == AC_ERR_TIMEOUT && (host_stat & ATA_DMA_ERR)) { |
6d97dbd7 TH |
464 | qc->err_mask = AC_ERR_HOST_BUS; |
465 | thaw = 1; | |
466 | } | |
467 | ||
468 | ap->ops->bmdma_stop(qc); | |
469 | } | |
470 | ||
471 | ata_altstatus(ap); | |
472 | ata_chk_status(ap); | |
473 | ap->ops->irq_clear(ap); | |
474 | ||
ba6a1308 | 475 | spin_unlock_irqrestore(ap->lock, flags); |
6d97dbd7 TH |
476 | |
477 | if (thaw) | |
478 | ata_eh_thaw_port(ap); | |
479 | ||
480 | /* PIO and DMA engines have been stopped, perform recovery */ | |
f5914a46 | 481 | ata_do_eh(ap, prereset, softreset, hardreset, postreset); |
6d97dbd7 TH |
482 | } |
483 | ||
484 | /** | |
485 | * ata_bmdma_error_handler - Stock error handler for BMDMA controller | |
486 | * @ap: port to handle error for | |
487 | * | |
488 | * Stock error handler for BMDMA controller. | |
489 | * | |
490 | * LOCKING: | |
491 | * Kernel thread context (may sleep) | |
492 | */ | |
493 | void ata_bmdma_error_handler(struct ata_port *ap) | |
494 | { | |
495 | ata_reset_fn_t hardreset; | |
496 | ||
497 | hardreset = NULL; | |
498 | if (sata_scr_valid(ap)) | |
499 | hardreset = sata_std_hardreset; | |
500 | ||
f5914a46 TH |
501 | ata_bmdma_drive_eh(ap, ata_std_prereset, ata_std_softreset, hardreset, |
502 | ata_std_postreset); | |
6d97dbd7 TH |
503 | } |
504 | ||
505 | /** | |
506 | * ata_bmdma_post_internal_cmd - Stock post_internal_cmd for | |
507 | * BMDMA controller | |
508 | * @qc: internal command to clean up | |
509 | * | |
510 | * LOCKING: | |
511 | * Kernel thread context (may sleep) | |
512 | */ | |
513 | void ata_bmdma_post_internal_cmd(struct ata_queued_cmd *qc) | |
514 | { | |
61dd08c6 AC |
515 | if (qc->ap->ioaddr.bmdma_addr) |
516 | ata_bmdma_stop(qc); | |
6d97dbd7 TH |
517 | } |
518 | ||
1fdffbce | 519 | #ifdef CONFIG_PCI |
4112e16a AC |
520 | |
521 | static int ata_resources_present(struct pci_dev *pdev, int port) | |
522 | { | |
523 | int i; | |
a84471fe | 524 | |
4112e16a AC |
525 | /* Check the PCI resources for this channel are enabled */ |
526 | port = port * 2; | |
527 | for (i = 0; i < 2; i ++) { | |
528 | if (pci_resource_start(pdev, port + i) == 0 || | |
55a6adee TH |
529 | pci_resource_len(pdev, port + i) == 0) |
530 | return 0; | |
4112e16a AC |
531 | } |
532 | return 1; | |
533 | } | |
a84471fe | 534 | |
1fdffbce JG |
535 | /** |
536 | * ata_pci_init_native_mode - Initialize native-mode driver | |
537 | * @pdev: pci device to be initialized | |
538 | * @port: array[2] of pointers to port info structures. | |
539 | * @ports: bitmap of ports present | |
540 | * | |
541 | * Utility function which allocates and initializes an | |
542 | * ata_probe_ent structure for a standard dual-port | |
543 | * PIO-based IDE controller. The returned ata_probe_ent | |
544 | * structure can be passed to ata_device_add(). The returned | |
545 | * ata_probe_ent structure should then be freed with kfree(). | |
546 | * | |
547 | * The caller need only pass the address of the primary port, the | |
548 | * secondary will be deduced automatically. If the device has non | |
549 | * standard secondary port mappings this function can be called twice, | |
550 | * once for each interface. | |
551 | */ | |
552 | ||
553 | struct ata_probe_ent * | |
554 | ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports) | |
555 | { | |
0d5ff566 | 556 | struct ata_probe_ent *probe_ent; |
55a6adee | 557 | int i; |
0d5ff566 TH |
558 | void __iomem * const *iomap; |
559 | ||
55a6adee TH |
560 | /* Discard disabled ports. Some controllers show their unused |
561 | * channels this way. Disabled ports will be made dummy. | |
562 | */ | |
563 | if (ata_resources_present(pdev, 0) == 0) | |
564 | ports &= ~ATA_PORT_PRIMARY; | |
565 | if (ata_resources_present(pdev, 1) == 0) | |
566 | ports &= ~ATA_PORT_SECONDARY; | |
567 | ||
568 | if (!ports) { | |
569 | dev_printk(KERN_ERR, &pdev->dev, "no available port\n"); | |
570 | return NULL; | |
571 | } | |
572 | ||
0d5ff566 TH |
573 | /* iomap BARs */ |
574 | for (i = 0; i < 4; i++) { | |
55a6adee TH |
575 | if (!(ports & (1 << (i / 2)))) |
576 | continue; | |
0d5ff566 TH |
577 | if (pcim_iomap(pdev, i, 0) == NULL) { |
578 | dev_printk(KERN_ERR, &pdev->dev, | |
579 | "failed to iomap PCI BAR %d\n", i); | |
580 | return NULL; | |
581 | } | |
582 | } | |
1fdffbce | 583 | |
0d5ff566 TH |
584 | pcim_iomap(pdev, 4, 0); /* may fail */ |
585 | iomap = pcim_iomap_table(pdev); | |
586 | ||
587 | /* alloc and init probe_ent */ | |
588 | probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]); | |
1fdffbce JG |
589 | if (!probe_ent) |
590 | return NULL; | |
591 | ||
55a6adee | 592 | probe_ent->n_ports = 2; |
1fdffbce | 593 | probe_ent->irq = pdev->irq; |
1d6f359a | 594 | probe_ent->irq_flags = IRQF_SHARED; |
a84471fe | 595 | |
1fdffbce | 596 | if (ports & ATA_PORT_PRIMARY) { |
55a6adee TH |
597 | probe_ent->port[0].cmd_addr = iomap[0]; |
598 | probe_ent->port[0].altstatus_addr = | |
599 | probe_ent->port[0].ctl_addr = (void __iomem *) | |
0d5ff566 TH |
600 | ((unsigned long)iomap[1] | ATA_PCI_CTL_OFS); |
601 | if (iomap[4]) { | |
55a6adee | 602 | if ((!(port[0]->flags & ATA_FLAG_IGN_SIMPLEX)) && |
0d5ff566 | 603 | (ioread8(iomap[4] + 2) & 0x80)) |
cca3974e | 604 | probe_ent->_host_flags |= ATA_HOST_SIMPLEX; |
55a6adee | 605 | probe_ent->port[0].bmdma_addr = iomap[4]; |
4e5ec5db | 606 | } |
55a6adee TH |
607 | ata_std_ports(&probe_ent->port[0]); |
608 | } else | |
609 | probe_ent->dummy_port_mask |= ATA_PORT_PRIMARY; | |
1fdffbce JG |
610 | |
611 | if (ports & ATA_PORT_SECONDARY) { | |
55a6adee TH |
612 | probe_ent->port[1].cmd_addr = iomap[2]; |
613 | probe_ent->port[1].altstatus_addr = | |
614 | probe_ent->port[1].ctl_addr = (void __iomem *) | |
0d5ff566 TH |
615 | ((unsigned long)iomap[3] | ATA_PCI_CTL_OFS); |
616 | if (iomap[4]) { | |
55a6adee | 617 | if ((!(port[1]->flags & ATA_FLAG_IGN_SIMPLEX)) && |
0d5ff566 | 618 | (ioread8(iomap[4] + 10) & 0x80)) |
cca3974e | 619 | probe_ent->_host_flags |= ATA_HOST_SIMPLEX; |
55a6adee | 620 | probe_ent->port[1].bmdma_addr = iomap[4] + 8; |
4e5ec5db | 621 | } |
55a6adee | 622 | ata_std_ports(&probe_ent->port[1]); |
fea63e38 | 623 | probe_ent->pinfo2 = port[1]; |
55a6adee TH |
624 | } else |
625 | probe_ent->dummy_port_mask |= ATA_PORT_SECONDARY; | |
1fdffbce | 626 | |
1fdffbce JG |
627 | return probe_ent; |
628 | } | |
629 | ||
0f834de3 TH |
630 | /** |
631 | * ata_pci_init_bmdma - acquire PCI BMDMA resources and init ATA host | |
632 | * @host: target ATA host | |
633 | * | |
634 | * Acquire PCI BMDMA resources and initialize @host accordingly. | |
635 | * | |
636 | * LOCKING: | |
637 | * Inherited from calling layer (may sleep). | |
638 | * | |
639 | * RETURNS: | |
640 | * 0 on success, -errno otherwise. | |
641 | */ | |
642 | static int ata_pci_init_bmdma(struct ata_host *host) | |
1fdffbce | 643 | { |
0f834de3 TH |
644 | struct device *gdev = host->dev; |
645 | struct pci_dev *pdev = to_pci_dev(gdev); | |
646 | int i, rc; | |
0d5ff566 | 647 | |
0f834de3 TH |
648 | /* TODO: If we get no DMA mask we should fall back to PIO */ |
649 | rc = pci_set_dma_mask(pdev, ATA_DMA_MASK); | |
650 | if (rc) | |
651 | return rc; | |
652 | rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK); | |
653 | if (rc) | |
654 | return rc; | |
655 | ||
656 | /* request and iomap DMA region */ | |
657 | rc = pcim_iomap_regions(pdev, 1 << 4, DRV_NAME); | |
658 | if (rc) { | |
659 | dev_printk(KERN_ERR, gdev, "failed to request/iomap BAR4\n"); | |
660 | return -ENOMEM; | |
0d5ff566 | 661 | } |
0f834de3 | 662 | host->iomap = pcim_iomap_table(pdev); |
0d5ff566 | 663 | |
0f834de3 TH |
664 | for (i = 0; i < 2; i++) { |
665 | struct ata_port *ap = host->ports[i]; | |
0f834de3 TH |
666 | void __iomem *bmdma = host->iomap[4] + 8 * i; |
667 | ||
668 | if (ata_port_is_dummy(ap)) | |
669 | continue; | |
670 | ||
21b0ad4f | 671 | ap->ioaddr.bmdma_addr = bmdma; |
0f834de3 TH |
672 | if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) && |
673 | (ioread8(bmdma + 2) & 0x80)) | |
674 | host->flags |= ATA_HOST_SIMPLEX; | |
0d5ff566 TH |
675 | } |
676 | ||
0f834de3 TH |
677 | return 0; |
678 | } | |
2ec7df04 | 679 | |
d491b27b TH |
680 | /** |
681 | * ata_pci_init_native_host - acquire native ATA resources and init host | |
682 | * @host: target ATA host | |
683 | * @port_mask: ports to consider | |
684 | * | |
685 | * Acquire native PCI ATA resources for @host and initialize | |
686 | * @host accordoingly. | |
687 | * | |
688 | * LOCKING: | |
689 | * Inherited from calling layer (may sleep). | |
690 | * | |
691 | * RETURNS: | |
692 | * 0 on success, -errno otherwise. | |
693 | */ | |
694 | int ata_pci_init_native_host(struct ata_host *host, unsigned int port_mask) | |
695 | { | |
696 | struct device *gdev = host->dev; | |
697 | struct pci_dev *pdev = to_pci_dev(gdev); | |
698 | int i, rc; | |
699 | ||
700 | /* Discard disabled ports. Some controllers show their unused | |
701 | * channels this way. Disabled ports are made dummy. | |
702 | */ | |
703 | for (i = 0; i < 2; i++) { | |
704 | if ((port_mask & (1 << i)) && !ata_resources_present(pdev, i)) { | |
705 | host->ports[i]->ops = &ata_dummy_port_ops; | |
706 | port_mask &= ~(1 << i); | |
707 | } | |
708 | } | |
709 | ||
710 | if (!port_mask) { | |
711 | dev_printk(KERN_ERR, gdev, "no available port\n"); | |
712 | return -ENODEV; | |
713 | } | |
714 | ||
715 | /* request, iomap BARs and init port addresses accordingly */ | |
716 | for (i = 0; i < 2; i++) { | |
717 | struct ata_port *ap = host->ports[i]; | |
718 | int base = i * 2; | |
719 | void __iomem * const *iomap; | |
720 | ||
721 | if (!(port_mask & (1 << i))) | |
722 | continue; | |
723 | ||
724 | rc = pcim_iomap_regions(pdev, 0x3 << base, DRV_NAME); | |
725 | if (rc) { | |
726 | dev_printk(KERN_ERR, gdev, "failed to request/iomap " | |
727 | "BARs for port %d (errno=%d)\n", i, rc); | |
728 | if (rc == -EBUSY) | |
729 | pcim_pin_device(pdev); | |
730 | return rc; | |
731 | } | |
732 | host->iomap = iomap = pcim_iomap_table(pdev); | |
733 | ||
734 | ap->ioaddr.cmd_addr = iomap[base]; | |
735 | ap->ioaddr.altstatus_addr = | |
736 | ap->ioaddr.ctl_addr = (void __iomem *) | |
737 | ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS); | |
738 | ata_std_ports(&ap->ioaddr); | |
739 | } | |
740 | ||
741 | return 0; | |
742 | } | |
743 | ||
21b0ad4f TH |
744 | /** |
745 | * ata_pci_prepare_native_host - helper to prepare native PCI ATA host | |
746 | * @pdev: target PCI device | |
747 | * @ppi: array of port_info | |
748 | * @n_ports: number of ports to allocate | |
749 | * @r_host: out argument for the initialized ATA host | |
750 | * | |
751 | * Helper to allocate ATA host for @pdev, acquire all native PCI | |
752 | * resources and initialize it accordingly in one go. | |
753 | * | |
754 | * LOCKING: | |
755 | * Inherited from calling layer (may sleep). | |
756 | * | |
757 | * RETURNS: | |
758 | * 0 on success, -errno otherwise. | |
759 | */ | |
760 | int ata_pci_prepare_native_host(struct pci_dev *pdev, | |
761 | const struct ata_port_info * const * ppi, | |
762 | int n_ports, struct ata_host **r_host) | |
763 | { | |
764 | struct ata_host *host; | |
765 | unsigned int port_mask; | |
766 | int rc; | |
767 | ||
768 | if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) | |
769 | return -ENOMEM; | |
770 | ||
771 | host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2); | |
772 | if (!host) { | |
773 | dev_printk(KERN_ERR, &pdev->dev, | |
774 | "failed to allocate ATA host\n"); | |
775 | rc = -ENOMEM; | |
776 | goto err_out; | |
777 | } | |
778 | ||
779 | port_mask = ATA_PORT_PRIMARY; | |
780 | if (n_ports > 1) | |
781 | port_mask |= ATA_PORT_SECONDARY; | |
782 | ||
783 | rc = ata_pci_init_native_host(host, port_mask); | |
784 | if (rc) | |
785 | goto err_out; | |
786 | ||
787 | /* init DMA related stuff */ | |
788 | rc = ata_pci_init_bmdma(host); | |
789 | if (rc) | |
790 | goto err_bmdma; | |
791 | ||
792 | devres_remove_group(&pdev->dev, NULL); | |
793 | *r_host = host; | |
794 | return 0; | |
795 | ||
796 | err_bmdma: | |
797 | /* This is necessary because PCI and iomap resources are | |
798 | * merged and releasing the top group won't release the | |
799 | * acquired resources if some of those have been acquired | |
800 | * before entering this function. | |
801 | */ | |
802 | pcim_iounmap_regions(pdev, 0xf); | |
803 | err_out: | |
804 | devres_release_group(&pdev->dev, NULL); | |
805 | return rc; | |
806 | } | |
807 | ||
0f834de3 TH |
808 | struct ata_legacy_devres { |
809 | unsigned int mask; | |
810 | unsigned long cmd_port[2]; | |
811 | void __iomem * cmd_addr[2]; | |
812 | void __iomem * ctl_addr[2]; | |
813 | unsigned int irq[2]; | |
814 | void * irq_dev_id[2]; | |
815 | }; | |
1fdffbce | 816 | |
0f834de3 TH |
817 | static void ata_legacy_free_irqs(struct ata_legacy_devres *legacy_dr) |
818 | { | |
819 | int i; | |
2ec7df04 | 820 | |
0f834de3 TH |
821 | for (i = 0; i < 2; i++) { |
822 | if (!legacy_dr->irq[i]) | |
823 | continue; | |
824 | ||
825 | free_irq(legacy_dr->irq[i], legacy_dr->irq_dev_id[i]); | |
826 | legacy_dr->irq[i] = 0; | |
827 | legacy_dr->irq_dev_id[i] = NULL; | |
828 | } | |
829 | } | |
830 | ||
831 | static void ata_legacy_release(struct device *gdev, void *res) | |
832 | { | |
833 | struct ata_legacy_devres *this = res; | |
834 | int i; | |
835 | ||
836 | ata_legacy_free_irqs(this); | |
837 | ||
838 | for (i = 0; i < 2; i++) { | |
839 | if (this->cmd_addr[i]) | |
840 | ioport_unmap(this->cmd_addr[i]); | |
841 | if (this->ctl_addr[i]) | |
842 | ioport_unmap(this->ctl_addr[i]); | |
843 | if (this->cmd_port[i]) | |
844 | release_region(this->cmd_port[i], 8); | |
845 | } | |
846 | } | |
847 | ||
848 | static int ata_init_legacy_port(struct ata_port *ap, | |
849 | struct ata_legacy_devres *legacy_dr) | |
850 | { | |
851 | struct ata_host *host = ap->host; | |
852 | int port_no = ap->port_no; | |
853 | unsigned long cmd_port, ctl_port; | |
854 | ||
855 | if (port_no == 0) { | |
856 | cmd_port = ATA_PRIMARY_CMD; | |
857 | ctl_port = ATA_PRIMARY_CTL; | |
858 | } else { | |
859 | cmd_port = ATA_SECONDARY_CMD; | |
860 | ctl_port = ATA_SECONDARY_CTL; | |
861 | } | |
862 | ||
863 | /* request cmd_port */ | |
864 | if (request_region(cmd_port, 8, "libata")) | |
865 | legacy_dr->cmd_port[port_no] = cmd_port; | |
866 | else { | |
867 | dev_printk(KERN_WARNING, host->dev, | |
868 | "0x%0lX IDE port busy\n", cmd_port); | |
869 | return -EBUSY; | |
870 | } | |
871 | ||
872 | /* iomap cmd and ctl ports */ | |
873 | legacy_dr->cmd_addr[port_no] = ioport_map(cmd_port, 8); | |
874 | legacy_dr->ctl_addr[port_no] = ioport_map(ctl_port, 1); | |
875 | if (!legacy_dr->cmd_addr[port_no] || !legacy_dr->ctl_addr[port_no]) | |
876 | return -ENOMEM; | |
877 | ||
878 | /* init IO addresses */ | |
879 | ap->ioaddr.cmd_addr = legacy_dr->cmd_addr[port_no]; | |
880 | ap->ioaddr.altstatus_addr = legacy_dr->ctl_addr[port_no]; | |
881 | ap->ioaddr.ctl_addr = legacy_dr->ctl_addr[port_no]; | |
882 | ata_std_ports(&ap->ioaddr); | |
883 | ||
884 | return 0; | |
885 | } | |
886 | ||
887 | /** | |
888 | * ata_init_legacy_host - acquire legacy ATA resources and init ATA host | |
889 | * @host: target ATA host | |
890 | * @legacy_mask: out parameter, mask indicating ports is in legacy mode | |
891 | * @was_busy: out parameter, indicates whether any port was busy | |
892 | * | |
893 | * Acquire legacy ATA resources for ports. | |
894 | * | |
895 | * LOCKING: | |
896 | * Inherited from calling layer (may sleep). | |
897 | * | |
898 | * RETURNS: | |
899 | * 0 on success, -errno otherwise. | |
900 | */ | |
901 | static int ata_init_legacy_host(struct ata_host *host, | |
902 | unsigned int *legacy_mask, int *was_busy) | |
903 | { | |
904 | struct device *gdev = host->dev; | |
905 | struct ata_legacy_devres *legacy_dr; | |
906 | int i, rc; | |
907 | ||
908 | if (!devres_open_group(gdev, NULL, GFP_KERNEL)) | |
909 | return -ENOMEM; | |
910 | ||
911 | rc = -ENOMEM; | |
912 | legacy_dr = devres_alloc(ata_legacy_release, sizeof(*legacy_dr), | |
913 | GFP_KERNEL); | |
914 | if (!legacy_dr) | |
915 | goto err_out; | |
916 | devres_add(gdev, legacy_dr); | |
917 | ||
918 | for (i = 0; i < 2; i++) { | |
919 | *legacy_mask &= ~(1 << i); | |
920 | rc = ata_init_legacy_port(host->ports[i], legacy_dr); | |
921 | if (rc == 0) | |
922 | legacy_dr->mask |= 1 << i; | |
923 | else if (rc == -EBUSY) | |
924 | (*was_busy)++; | |
925 | } | |
926 | ||
927 | if (!legacy_dr->mask) | |
928 | return -EBUSY; | |
c4b01f1d | 929 | |
0f834de3 TH |
930 | for (i = 0; i < 2; i++) |
931 | if (!(legacy_dr->mask & (1 << i))) | |
932 | host->ports[i]->ops = &ata_dummy_port_ops; | |
933 | ||
934 | *legacy_mask |= legacy_dr->mask; | |
935 | ||
936 | devres_remove_group(gdev, NULL); | |
937 | return 0; | |
938 | ||
939 | err_out: | |
940 | devres_release_group(gdev, NULL); | |
941 | return rc; | |
942 | } | |
943 | ||
944 | /** | |
945 | * ata_request_legacy_irqs - request legacy ATA IRQs | |
946 | * @host: target ATA host | |
947 | * @handler: array of IRQ handlers | |
948 | * @irq_flags: array of IRQ flags | |
949 | * @dev_id: array of IRQ dev_ids | |
950 | * | |
951 | * Request legacy IRQs for non-dummy legacy ports in @host. All | |
952 | * IRQ parameters are passed as array to allow ports to have | |
953 | * separate IRQ handlers. | |
954 | * | |
955 | * LOCKING: | |
956 | * Inherited from calling layer (may sleep). | |
957 | * | |
958 | * RETURNS: | |
959 | * 0 on success, -errno otherwise. | |
960 | */ | |
961 | static int ata_request_legacy_irqs(struct ata_host *host, | |
962 | irq_handler_t const *handler, | |
963 | const unsigned int *irq_flags, | |
964 | void * const *dev_id) | |
965 | { | |
966 | struct device *gdev = host->dev; | |
967 | struct ata_legacy_devres *legacy_dr; | |
968 | int i, rc; | |
969 | ||
970 | legacy_dr = devres_find(host->dev, ata_legacy_release, NULL, NULL); | |
971 | BUG_ON(!legacy_dr); | |
972 | ||
973 | for (i = 0; i < 2; i++) { | |
974 | unsigned int irq; | |
975 | ||
976 | /* FIXME: ATA_*_IRQ() should take generic device not pci_dev */ | |
977 | if (i == 0) | |
978 | irq = ATA_PRIMARY_IRQ(to_pci_dev(gdev)); | |
c4b01f1d | 979 | else |
0f834de3 TH |
980 | irq = ATA_SECONDARY_IRQ(to_pci_dev(gdev)); |
981 | ||
982 | if (!(legacy_dr->mask & (1 << i))) | |
983 | continue; | |
984 | ||
985 | if (!handler[i]) { | |
986 | dev_printk(KERN_ERR, gdev, | |
987 | "NULL handler specified for port %d\n", i); | |
988 | rc = -EINVAL; | |
989 | goto err_out; | |
2ec7df04 | 990 | } |
d639ca94 | 991 | |
0f834de3 TH |
992 | rc = request_irq(irq, handler[i], irq_flags[i], DRV_NAME, |
993 | dev_id[i]); | |
994 | if (rc) { | |
995 | dev_printk(KERN_ERR, gdev, | |
996 | "irq %u request failed (errno=%d)\n", irq, rc); | |
997 | goto err_out; | |
998 | } | |
1fdffbce | 999 | |
0f834de3 TH |
1000 | /* record irq allocation in legacy_dr */ |
1001 | legacy_dr->irq[i] = irq; | |
1002 | legacy_dr->irq_dev_id[i] = dev_id[i]; | |
1fdffbce | 1003 | |
0f834de3 TH |
1004 | /* only used to print info */ |
1005 | if (i == 0) | |
1006 | host->irq = irq; | |
1007 | else | |
1008 | host->irq2 = irq; | |
1009 | } | |
1010 | ||
1011 | return 0; | |
1012 | ||
1013 | err_out: | |
1014 | ata_legacy_free_irqs(legacy_dr); | |
1015 | return rc; | |
1016 | } | |
1fdffbce JG |
1017 | |
1018 | /** | |
1019 | * ata_pci_init_one - Initialize/register PCI IDE host controller | |
1020 | * @pdev: Controller to be initialized | |
1021 | * @port_info: Information from low-level host driver | |
1022 | * @n_ports: Number of ports attached to host controller | |
1023 | * | |
1024 | * This is a helper function which can be called from a driver's | |
1025 | * xxx_init_one() probe function if the hardware uses traditional | |
1026 | * IDE taskfile registers. | |
1027 | * | |
1028 | * This function calls pci_enable_device(), reserves its register | |
1029 | * regions, sets the dma mask, enables bus master mode, and calls | |
1030 | * ata_device_add() | |
1031 | * | |
2ec7df04 AC |
1032 | * ASSUMPTION: |
1033 | * Nobody makes a single channel controller that appears solely as | |
1034 | * the secondary legacy port on PCI. | |
1035 | * | |
1fdffbce JG |
1036 | * LOCKING: |
1037 | * Inherited from PCI layer (may sleep). | |
1038 | * | |
1039 | * RETURNS: | |
1040 | * Zero on success, negative on errno-based value on error. | |
1041 | */ | |
1042 | ||
1043 | int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info, | |
1044 | unsigned int n_ports) | |
1045 | { | |
f0d36efd | 1046 | struct device *dev = &pdev->dev; |
0f834de3 TH |
1047 | struct ata_host *host = NULL; |
1048 | const struct ata_port_info *port[2]; | |
c791c306 | 1049 | u8 mask; |
1fdffbce | 1050 | unsigned int legacy_mode = 0; |
1fdffbce JG |
1051 | int rc; |
1052 | ||
1053 | DPRINTK("ENTER\n"); | |
1054 | ||
f0d36efd TH |
1055 | if (!devres_open_group(dev, NULL, GFP_KERNEL)) |
1056 | return -ENOMEM; | |
1057 | ||
c791c306 JG |
1058 | BUG_ON(n_ports < 1 || n_ports > 2); |
1059 | ||
1fdffbce JG |
1060 | port[0] = port_info[0]; |
1061 | if (n_ports > 1) | |
1062 | port[1] = port_info[1]; | |
1063 | else | |
1064 | port[1] = port[0]; | |
1065 | ||
1fdffbce JG |
1066 | /* FIXME: Really for ATA it isn't safe because the device may be |
1067 | multi-purpose and we want to leave it alone if it was already | |
1068 | enabled. Secondly for shared use as Arjan says we want refcounting | |
1069 | ||
1070 | Checking dev->is_enabled is insufficient as this is not set at | |
1071 | boot for the primary video which is BIOS enabled | |
d491b27b | 1072 | */ |
1fdffbce | 1073 | |
f0d36efd | 1074 | rc = pcim_enable_device(pdev); |
1fdffbce | 1075 | if (rc) |
f0d36efd | 1076 | goto err_out; |
1fdffbce | 1077 | |
c791c306 JG |
1078 | if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) { |
1079 | u8 tmp8; | |
1080 | ||
1081 | /* TODO: What if one channel is in native mode ... */ | |
1082 | pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8); | |
1083 | mask = (1 << 2) | (1 << 0); | |
1084 | if ((tmp8 & mask) != mask) | |
1085 | legacy_mode = (1 << 3); | |
8eb166bf AC |
1086 | #if defined(CONFIG_NO_ATA_LEGACY) |
1087 | /* Some platforms with PCI limits cannot address compat | |
1088 | port space. In that case we punt if their firmware has | |
1089 | left a device in compatibility mode */ | |
1090 | if (legacy_mode) { | |
1091 | printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n"); | |
f0d36efd TH |
1092 | rc = -EOPNOTSUPP; |
1093 | goto err_out; | |
8eb166bf AC |
1094 | } |
1095 | #endif | |
c791c306 JG |
1096 | } |
1097 | ||
d491b27b TH |
1098 | /* alloc and init host */ |
1099 | host = ata_host_alloc_pinfo(dev, port, 2); | |
1100 | if (!host) { | |
1101 | dev_printk(KERN_ERR, &pdev->dev, | |
1102 | "failed to allocate ATA host\n"); | |
1103 | rc = -ENOMEM; | |
1104 | goto err_out; | |
1105 | } | |
1106 | ||
dc3c3377 | 1107 | if (!legacy_mode) { |
d491b27b | 1108 | unsigned int port_mask; |
0f834de3 | 1109 | |
d491b27b TH |
1110 | port_mask = ATA_PORT_PRIMARY; |
1111 | if (n_ports > 1) | |
1112 | port_mask |= ATA_PORT_SECONDARY; | |
1113 | ||
1114 | rc = ata_pci_init_native_host(host, port_mask); | |
0f834de3 TH |
1115 | if (rc) |
1116 | goto err_out; | |
dc3c3377 | 1117 | } else { |
0f834de3 | 1118 | int was_busy = 0; |
1fdffbce | 1119 | |
0f834de3 TH |
1120 | rc = ata_init_legacy_host(host, &legacy_mode, &was_busy); |
1121 | if (was_busy) | |
8cdfb29c | 1122 | pcim_pin_device(pdev); |
0f834de3 TH |
1123 | if (rc) |
1124 | goto err_out; | |
1fdffbce | 1125 | |
0f834de3 TH |
1126 | /* request respective PCI regions, may fail */ |
1127 | rc = pci_request_region(pdev, 1, DRV_NAME); | |
1128 | rc = pci_request_region(pdev, 3, DRV_NAME); | |
0f834de3 | 1129 | } |
1fdffbce | 1130 | |
d491b27b TH |
1131 | /* init BMDMA, may fail */ |
1132 | ata_pci_init_bmdma(host); | |
1133 | pci_set_master(pdev); | |
1134 | ||
1135 | /* start host and request IRQ */ | |
1136 | rc = ata_host_start(host); | |
1137 | if (rc) | |
1138 | goto err_out; | |
1139 | ||
1140 | if (!legacy_mode) | |
1141 | rc = devm_request_irq(dev, pdev->irq, | |
1142 | port_info[0]->port_ops->irq_handler, | |
1143 | IRQF_SHARED, DRV_NAME, host); | |
1144 | else { | |
0f834de3 TH |
1145 | irq_handler_t handler[2] = { host->ops->irq_handler, |
1146 | host->ops->irq_handler }; | |
1147 | unsigned int irq_flags[2] = { IRQF_SHARED, IRQF_SHARED }; | |
1148 | void *dev_id[2] = { host, host }; | |
1149 | ||
0f834de3 | 1150 | rc = ata_request_legacy_irqs(host, handler, irq_flags, dev_id); |
d491b27b TH |
1151 | } |
1152 | if (rc) | |
1153 | goto err_out; | |
1fdffbce | 1154 | |
d491b27b TH |
1155 | /* register */ |
1156 | rc = ata_host_register(host, port_info[0]->sht); | |
1157 | if (rc) | |
1158 | goto err_out; | |
1fdffbce | 1159 | |
f0d36efd | 1160 | devres_remove_group(dev, NULL); |
1fdffbce JG |
1161 | return 0; |
1162 | ||
1fdffbce | 1163 | err_out: |
f0d36efd | 1164 | devres_release_group(dev, NULL); |
1fdffbce JG |
1165 | return rc; |
1166 | } | |
1167 | ||
d33d44fa AC |
1168 | /** |
1169 | * ata_pci_clear_simplex - attempt to kick device out of simplex | |
1170 | * @pdev: PCI device | |
1171 | * | |
1172 | * Some PCI ATA devices report simplex mode but in fact can be told to | |
2e9edbf8 | 1173 | * enter non simplex mode. This implements the neccessary logic to |
d33d44fa AC |
1174 | * perform the task on such devices. Calling it on other devices will |
1175 | * have -undefined- behaviour. | |
1176 | */ | |
1177 | ||
1178 | int ata_pci_clear_simplex(struct pci_dev *pdev) | |
1179 | { | |
1180 | unsigned long bmdma = pci_resource_start(pdev, 4); | |
1181 | u8 simplex; | |
1182 | ||
1183 | if (bmdma == 0) | |
1184 | return -ENOENT; | |
1185 | ||
1186 | simplex = inb(bmdma + 0x02); | |
1187 | outb(simplex & 0x60, bmdma + 0x02); | |
1188 | simplex = inb(bmdma + 0x02); | |
1189 | if (simplex & 0x80) | |
1190 | return -EOPNOTSUPP; | |
1191 | return 0; | |
1192 | } | |
1193 | ||
a76b62ca | 1194 | unsigned long ata_pci_default_filter(struct ata_device *adev, unsigned long xfer_mask) |
d33d44fa AC |
1195 | { |
1196 | /* Filter out DMA modes if the device has been configured by | |
1197 | the BIOS as PIO only */ | |
2e9edbf8 | 1198 | |
a76b62ca | 1199 | if (adev->ap->ioaddr.bmdma_addr == 0) |
d33d44fa AC |
1200 | xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA); |
1201 | return xfer_mask; | |
1202 | } | |
1203 | ||
1fdffbce JG |
1204 | #endif /* CONFIG_PCI */ |
1205 |