siisata.c revision 1.16 1 /* $NetBSD: siisata.c,v 1.16 2012/04/20 20:23:20 bouyer Exp $ */
2
3 /* from ahcisata_core.c */
4
5 /*
6 * Copyright (c) 2006 Manuel Bouyer.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30 /* from atapi_wdc.c */
31
32 /*
33 * Copyright (c) 1998, 2001 Manuel Bouyer.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
45 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
48 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
49 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
53 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
55
56 /*
57 * Copyright (c) 2007, 2008, 2009, 2010 Jonathan A. Kollasch.
58 * All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 * 1. Redistributions of source code must retain the above copyright
64 * notice, this list of conditions and the following disclaimer.
65 * 2. Redistributions in binary form must reproduce the above copyright
66 * notice, this list of conditions and the following disclaimer in the
67 * documentation and/or other materials provided with the distribution.
68 *
69 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
70 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
71 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
72 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
73 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
74 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
75 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
76 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
77 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
78 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
79 */
80
81 #include <sys/cdefs.h>
82 __KERNEL_RCSID(0, "$NetBSD: siisata.c,v 1.16 2012/04/20 20:23:20 bouyer Exp $");
83
84 #include <sys/types.h>
85 #include <sys/malloc.h>
86 #include <sys/param.h>
87 #include <sys/kernel.h>
88 #include <sys/systm.h>
89 #include <sys/syslog.h>
90 #include <sys/disklabel.h>
91 #include <sys/buf.h>
92 #include <sys/proc.h>
93
94 #include <dev/ata/atareg.h>
95 #include <dev/ata/satavar.h>
96 #include <dev/ata/satareg.h>
97 #include <dev/ata/satafisvar.h>
98 #include <dev/ata/satafisreg.h>
99 #include <dev/ic/siisatavar.h>
100 #include <dev/ic/siisatareg.h>
101
102 #include <dev/scsipi/scsi_all.h> /* for SCSI status */
103
104 #include "atapibus.h"
105
106 #ifdef SIISATA_DEBUG
107 int siisata_debug_mask = 0;
108 #endif
109
110 #define ATA_DELAY 10000 /* 10s for a drive I/O */
111
112 static void siisata_attach_port(struct siisata_softc *, int);
113 static void siisata_intr_port(struct siisata_channel *);
114
115 void siisata_probe_drive(struct ata_channel *);
116 void siisata_setup_channel(struct ata_channel *);
117
118 int siisata_ata_bio(struct ata_drive_datas *, struct ata_bio *);
119 void siisata_reset_drive(struct ata_drive_datas *, int);
120 void siisata_reset_channel(struct ata_channel *, int);
121 int siisata_ata_addref(struct ata_drive_datas *);
122 void siisata_ata_delref(struct ata_drive_datas *);
123 void siisata_killpending(struct ata_drive_datas *);
124
125 void siisata_cmd_start(struct ata_channel *, struct ata_xfer *);
126 int siisata_cmd_complete(struct ata_channel *, struct ata_xfer *, int);
127 void siisata_cmd_done(struct ata_channel *, struct ata_xfer *, int);
128 void siisata_cmd_kill_xfer(struct ata_channel *, struct ata_xfer *, int);
129
130 void siisata_bio_start(struct ata_channel *, struct ata_xfer *);
131 int siisata_bio_complete(struct ata_channel *, struct ata_xfer *, int);
132 void siisata_bio_kill_xfer(struct ata_channel *, struct ata_xfer *, int);
133 int siisata_exec_command(struct ata_drive_datas *, struct ata_command *);
134
135 void siisata_timeout(void *);
136
137 static void siisata_reinit_port(struct ata_channel *);
138 static void siisata_device_reset(struct ata_channel *);
139 static void siisata_activate_prb(struct siisata_channel *, int);
140 static void siisata_deactivate_prb(struct siisata_channel *, int);
141 static int siisata_dma_setup(struct ata_channel *chp, int slot,
142 void *data, size_t, int);
143
144 #if NATAPIBUS > 0
145 void siisata_atapibus_attach(struct atabus_softc *);
146 void siisata_atapi_probe_device(struct atapibus_softc *, int);
147 void siisata_atapi_minphys(struct buf *);
148 void siisata_atapi_start(struct ata_channel *,struct ata_xfer *);
149 int siisata_atapi_complete(struct ata_channel *, struct ata_xfer *, int);
150 void siisata_atapi_kill_xfer(struct ata_channel *, struct ata_xfer *, int);
151 void siisata_atapi_done(struct ata_channel *, struct ata_xfer *, int);
152 void siisata_atapi_scsipi_request(struct scsipi_channel *,
153 scsipi_adapter_req_t, void *);
154 void siisata_atapi_kill_pending(struct scsipi_periph *);
155 #endif /* NATAPIBUS */
156
157 const struct ata_bustype siisata_ata_bustype = {
158 SCSIPI_BUSTYPE_ATA,
159 siisata_ata_bio,
160 siisata_reset_drive,
161 siisata_reset_channel,
162 siisata_exec_command,
163 ata_get_params,
164 siisata_ata_addref,
165 siisata_ata_delref,
166 siisata_killpending
167 };
168
169 #if NATAPIBUS > 0
170 static const struct scsipi_bustype siisata_atapi_bustype = {
171 SCSIPI_BUSTYPE_ATAPI,
172 atapi_scsipi_cmd,
173 atapi_interpret_sense,
174 atapi_print_addr,
175 siisata_atapi_kill_pending,
176 NULL,
177 };
178 #endif /* NATAPIBUS */
179
180
181 void
182 siisata_attach(struct siisata_softc *sc)
183 {
184 int i;
185
186 SIISATA_DEBUG_PRINT(("%s: %s: GR_GC: 0x%08x\n",
187 SIISATANAME(sc), __func__, GRREAD(sc, GR_GC)), DEBUG_FUNCS);
188
189 sc->sc_atac.atac_cap = ATAC_CAP_DMA | ATAC_CAP_UDMA;
190 sc->sc_atac.atac_pio_cap = 4;
191 sc->sc_atac.atac_dma_cap = 2;
192 sc->sc_atac.atac_udma_cap = 6;
193 sc->sc_atac.atac_channels = sc->sc_chanarray;
194 sc->sc_atac.atac_probe = siisata_probe_drive;
195 sc->sc_atac.atac_bustype_ata = &siisata_ata_bustype;
196 sc->sc_atac.atac_set_modes = siisata_setup_channel;
197 #if NATAPIBUS > 0
198 sc->sc_atac.atac_atapibus_attach = siisata_atapibus_attach;
199 #endif
200
201 /* come out of reset state */
202 GRWRITE(sc, GR_GC, 0);
203
204 for (i = 0; i < sc->sc_atac.atac_nchannels; i++) {
205 siisata_attach_port(sc, i);
206 }
207
208 SIISATA_DEBUG_PRINT(("%s: %s: GR_GC: 0x%08x\n",
209 SIISATANAME(sc), __func__, GRREAD(sc, GR_GC)),
210 DEBUG_FUNCS);
211 return;
212 }
213
214 static void
215 siisata_init_port(struct siisata_softc *sc, int port)
216 {
217 struct siisata_channel *schp;
218 struct ata_channel *chp;
219
220 schp = &sc->sc_channels[port];
221 chp = (struct ata_channel *)schp;
222
223 /* come out of reset, 64-bit activation */
224 PRWRITE(sc, PRX(chp->ch_channel, PRO_PCC),
225 PR_PC_32BA | PR_PC_PORT_RESET);
226 /* initialize port */
227 siisata_reinit_port(chp);
228 /* clear any interrupts */
229 PRWRITE(sc, PRX(chp->ch_channel, PRO_PIS), 0xffffffff);
230 /* enable CmdErrr+CmdCmpl interrupting */
231 PRWRITE(sc, PRX(chp->ch_channel, PRO_PIES),
232 PR_PIS_CMDERRR | PR_PIS_CMDCMPL);
233 /* enable port interrupt */
234 GRWRITE(sc, GR_GC, GRREAD(sc, GR_GC) | GR_GC_PXIE(chp->ch_channel));
235 }
236
237 static void
238 siisata_attach_port(struct siisata_softc *sc, int port)
239 {
240 int j;
241 int dmasize;
242 int error;
243 void *prbp;
244 struct siisata_channel *schp;
245 struct ata_channel *chp;
246
247 schp = &sc->sc_channels[port];
248 chp = (struct ata_channel *)schp;
249 sc->sc_chanarray[port] = chp;
250 chp->ch_channel = port;
251 chp->ch_atac = &sc->sc_atac;
252 chp->ch_queue = malloc(sizeof(struct ata_queue),
253 M_DEVBUF, M_NOWAIT);
254 if (chp->ch_queue == NULL) {
255 aprint_error_dev(sc->sc_atac.atac_dev,
256 "port %d: can't allocate memory "
257 "for command queue\n", chp->ch_channel);
258 return;
259 }
260
261 dmasize = SIISATA_CMD_SIZE * SIISATA_MAX_SLOTS;
262
263 SIISATA_DEBUG_PRINT(("%s: %s: dmasize: %d\n", SIISATANAME(sc),
264 __func__, dmasize), DEBUG_FUNCS);
265
266 error = bus_dmamem_alloc(sc->sc_dmat, dmasize, PAGE_SIZE, 0,
267 &schp->sch_prb_seg, 1, &schp->sch_prb_nseg, BUS_DMA_NOWAIT);
268 if (error) {
269 aprint_error_dev(sc->sc_atac.atac_dev,
270 "unable to allocate PRB table memory, "
271 "error=%d\n", error);
272 return;
273 }
274
275 error = bus_dmamem_map(sc->sc_dmat,
276 &schp->sch_prb_seg, schp->sch_prb_nseg,
277 dmasize, &prbp, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
278 if (error) {
279 aprint_error_dev(sc->sc_atac.atac_dev,
280 "unable to map PRB table memory, "
281 "error=%d\n", error);
282 bus_dmamem_free(sc->sc_dmat,
283 &schp->sch_prb_seg, schp->sch_prb_nseg);
284 return;
285 }
286
287 error = bus_dmamap_create(sc->sc_dmat, dmasize, 1, dmasize, 0,
288 BUS_DMA_NOWAIT, &schp->sch_prbd);
289 if (error) {
290 aprint_error_dev(sc->sc_atac.atac_dev,
291 "unable to create PRB table map, "
292 "error=%d\n", error);
293 bus_dmamem_unmap(sc->sc_dmat, prbp, dmasize);
294 bus_dmamem_free(sc->sc_dmat,
295 &schp->sch_prb_seg, schp->sch_prb_nseg);
296 return;
297 }
298
299 error = bus_dmamap_load(sc->sc_dmat, schp->sch_prbd,
300 prbp, dmasize, NULL, BUS_DMA_NOWAIT);
301 if (error) {
302 aprint_error_dev(sc->sc_atac.atac_dev,
303 "unable to load PRB table map, "
304 "error=%d\n", error);
305 bus_dmamap_destroy(sc->sc_dmat, schp->sch_prbd);
306 bus_dmamem_unmap(sc->sc_dmat, prbp, dmasize);
307 bus_dmamem_free(sc->sc_dmat,
308 &schp->sch_prb_seg, schp->sch_prb_nseg);
309 return;
310 }
311
312 for (j = 0; j < SIISATA_MAX_SLOTS; j++) {
313 schp->sch_prb[j] = (struct siisata_prb *)
314 ((char *)prbp + SIISATA_CMD_SIZE * j);
315 schp->sch_bus_prb[j] =
316 schp->sch_prbd->dm_segs[0].ds_addr +
317 SIISATA_CMD_SIZE * j;
318 error = bus_dmamap_create(sc->sc_dmat, MAXPHYS,
319 SIISATA_NSGE, MAXPHYS, 0,
320 BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
321 &schp->sch_datad[j]);
322 if (error) {
323 aprint_error_dev(sc->sc_atac.atac_dev,
324 "couldn't create xfer DMA map, error=%d\n",
325 error);
326 return;
327 }
328 }
329
330 chp->ch_ndrive = 1;
331 if (bus_space_subregion(sc->sc_prt, sc->sc_prh,
332 PRX(chp->ch_channel, PRO_SSTATUS), 4, &schp->sch_sstatus) != 0) {
333 aprint_error_dev(sc->sc_atac.atac_dev,
334 "couldn't map port %d SStatus regs\n",
335 chp->ch_channel);
336 return;
337 }
338 if (bus_space_subregion(sc->sc_prt, sc->sc_prh,
339 PRX(chp->ch_channel, PRO_SCONTROL), 4, &schp->sch_scontrol) != 0) {
340 aprint_error_dev(sc->sc_atac.atac_dev,
341 "couldn't map port %d SControl regs\n",
342 chp->ch_channel);
343 return;
344 }
345 if (bus_space_subregion(sc->sc_prt, sc->sc_prh,
346 PRX(chp->ch_channel, PRO_SERROR), 4, &schp->sch_serror) != 0) {
347 aprint_error_dev(sc->sc_atac.atac_dev,
348 "couldn't map port %d SError regs\n",
349 chp->ch_channel);
350 return;
351 }
352
353 siisata_init_port(sc, port);
354
355 ata_channel_attach(chp);
356
357 return;
358 }
359
360 int
361 siisata_detach(struct siisata_softc *sc, int flags)
362 {
363 struct atac_softc *atac = &sc->sc_atac;
364 struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic;
365 struct siisata_channel *schp;
366 struct ata_channel *chp;
367 int i, j, error;
368
369 for (i = 0; i < sc->sc_atac.atac_nchannels; i++) {
370 schp = &sc->sc_channels[i];
371 chp = sc->sc_chanarray[i];
372
373 if (chp->atabus == NULL)
374 continue;
375 if ((error = config_detach(chp->atabus, flags)) != 0)
376 return error;
377
378 for (j = 0; j < SIISATA_MAX_SLOTS; j++)
379 bus_dmamap_destroy(sc->sc_dmat, schp->sch_datad[j]);
380
381 bus_dmamap_unload(sc->sc_dmat, schp->sch_prbd);
382 bus_dmamap_destroy(sc->sc_dmat, schp->sch_prbd);
383 bus_dmamem_unmap(sc->sc_dmat, schp->sch_prb[0],
384 SIISATA_CMD_SIZE * SIISATA_MAX_SLOTS);
385 bus_dmamem_free(sc->sc_dmat,
386 &schp->sch_prb_seg, schp->sch_prb_nseg);
387
388 free(chp->ch_queue, M_DEVBUF);
389 chp->atabus = NULL;
390 }
391
392 if (adapt->adapt_refcnt != 0)
393 return EBUSY;
394
395 /* leave the chip in reset */
396 GRWRITE(sc, GR_GC, GR_GC_GLBLRST);
397
398 return 0;
399 }
400
401 void
402 siisata_resume(struct siisata_softc *sc)
403 {
404 int i;
405
406 /* come out of reset state */
407 GRWRITE(sc, GR_GC, 0);
408
409 for (i = 0; i < sc->sc_atac.atac_nchannels; i++) {
410 siisata_init_port(sc, i);
411 }
412
413 }
414
415 int
416 siisata_intr(void *v)
417 {
418 struct siisata_softc *sc = v;
419 uint32_t is;
420 int i, r = 0;
421 while ((is = GRREAD(sc, GR_GIS))) {
422 SIISATA_DEBUG_PRINT(("%s: %s: GR_GIS: 0x%08x\n",
423 SIISATANAME(sc), __func__, is), DEBUG_INTR);
424 r = 1;
425 for (i = 0; i < sc->sc_atac.atac_nchannels; i++)
426 if (is & GR_GIS_PXIS(i))
427 siisata_intr_port(&sc->sc_channels[i]);
428 }
429 return r;
430 }
431
432 static void
433 siisata_intr_port(struct siisata_channel *schp)
434 {
435 struct siisata_softc *sc;
436 struct ata_channel *chp;
437 struct ata_xfer *xfer;
438 int slot;
439 uint32_t pss, pis;
440 uint32_t prbfis;
441
442 sc = (struct siisata_softc *)schp->ata_channel.ch_atac;
443 chp = &schp->ata_channel;
444 xfer = chp->ch_queue->active_xfer;
445 slot = SIISATA_NON_NCQ_SLOT;
446
447 SIISATA_DEBUG_PRINT(("%s: %s port %d\n",
448 SIISATANAME(sc), __func__, chp->ch_channel), DEBUG_INTR);
449
450 pis = PRREAD(sc, PRX(chp->ch_channel, PRO_PIS));
451
452 if (pis & PR_PIS_CMDCMPL) {
453 /* get slot status, clearing completion interrupt */
454 pss = PRREAD(sc, PRX(chp->ch_channel, PRO_PSS));
455 /* is this expected? */
456 /* XXX improve */
457 if ((schp->sch_active_slots & __BIT(slot)) == 0) {
458 PRWRITE(sc, PRX(chp->ch_channel, PRO_PIS), 0xffffffff);
459 log(LOG_WARNING, "%s: unexpected command "
460 "completion on port %d\n",
461 SIISATANAME(sc), chp->ch_channel);
462 return;
463 }
464 } else if (pis & PR_PIS_CMDERRR) {
465 uint32_t ec;
466
467 /* emulate a CRC error by default */
468 chp->ch_status = WDCS_ERR;
469 chp->ch_error = WDCE_CRC;
470
471 ec = PRREAD(sc, PRX(chp->ch_channel, PRO_PCE));
472 if (ec <= PR_PCE_DATAFISERROR) {
473 if (ec == PR_PCE_DEVICEERROR) {
474 /* read in specific information about error */
475 prbfis = bus_space_read_stream_4(
476 sc->sc_prt, sc->sc_prh,
477 PRSX(chp->ch_channel, slot, PRSO_FIS));
478 /* set ch_status and ch_error */
479 satafis_rdh_parse(chp, (uint8_t *)&prbfis);
480 }
481 siisata_reinit_port(chp);
482 } else {
483 /* okay, we have a "Fatal Error" */
484 siisata_device_reset(chp);
485 }
486 }
487
488 /* clear some (ok, all) ints */
489 PRWRITE(sc, PRX(chp->ch_channel, PRO_PIS), 0xffffffff);
490
491 KASSERT(xfer != NULL);
492 KASSERT(xfer->c_intr != NULL);
493 xfer->c_intr(chp, xfer, slot);
494
495 return;
496 }
497
498 void
499 siisata_reset_drive(struct ata_drive_datas *drvp, int flags)
500 {
501 struct ata_channel *chp = drvp->chnl_softc;
502 struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
503 struct siisata_channel *schp = (struct siisata_channel *)chp;
504 struct siisata_prb *prb;
505 int slot = SIISATA_NON_NCQ_SLOT;
506 int i;
507
508 /* wait for ready */
509 while (!(PRREAD(sc, PRX(chp->ch_channel, PRO_PS)) & PR_PS_PORT_READY))
510 DELAY(10);
511
512 prb = schp->sch_prb[slot];
513 memset(prb, 0, sizeof(struct siisata_prb));
514 prb->prb_control =
515 htole16(PRB_CF_SOFT_RESET | PRB_CF_INTERRUPT_MASK);
516
517 siisata_activate_prb(schp, slot);
518
519 for(i = 0; i < 31000; i++) {
520 if (PRREAD(sc, PRX(chp->ch_channel, PRO_PSS)) &
521 PR_PXSS(slot))
522 DELAY(1000);
523 else
524 break;
525 }
526
527 siisata_deactivate_prb(schp, slot);
528
529 log(LOG_DEBUG, "%s: port %d: ch_status %x ch_error %x\n",
530 __func__, chp->ch_channel, chp->ch_status, chp->ch_error);
531
532 #if 1
533 /* attempt to downgrade signaling in event of CRC error */
534 /* XXX should be part of the MI (S)ATA subsystem */
535 if (chp->ch_status == 0x51 && chp->ch_error == 0x84) {
536 bus_space_write_4(sc->sc_prt, schp->sch_scontrol, 0,
537 SControl_IPM_NONE | SControl_SPD_G1 | SControl_DET_INIT);
538 DELAY(10);
539 bus_space_write_4(sc->sc_prt, schp->sch_scontrol, 0,
540 SControl_IPM_NONE | SControl_SPD_G1);
541 DELAY(10);
542 for (;;) {
543 if ((bus_space_read_4(sc->sc_prt, schp->sch_sstatus, 0)
544 & SStatus_DET_mask) == SStatus_DET_DEV)
545 break;
546 DELAY(10);
547 }
548 }
549 #endif
550
551 #if 1
552 chp->ch_status = 0;
553 chp->ch_error = 0;
554 #endif
555
556 return;
557 }
558
559 void
560 siisata_reset_channel(struct ata_channel *chp, int flags)
561 {
562 struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
563 struct siisata_channel *schp = (struct siisata_channel *)chp;
564
565 SIISATA_DEBUG_PRINT(("%s: %s\n", SIISATANAME(sc), __func__),
566 DEBUG_FUNCS);
567
568 if (sata_reset_interface(chp, sc->sc_prt, schp->sch_scontrol,
569 schp->sch_sstatus) != SStatus_DET_DEV) {
570 log(LOG_CRIT, "%s port %d: reset failed\n",
571 SIISATANAME(sc), chp->ch_channel);
572 /* XXX and then ? */
573 }
574 /* wait for ready */
575 while (!(PRREAD(sc, PRX(chp->ch_channel, PRO_PS)) & PR_PS_PORT_READY))
576 DELAY(10);
577 PRWRITE(sc, PRX(chp->ch_channel, PRO_SERROR),
578 PRREAD(sc, PRX(chp->ch_channel, PRO_SERROR)));
579 if (chp->ch_queue->active_xfer) {
580 chp->ch_queue->active_xfer->c_kill_xfer(chp,
581 chp->ch_queue->active_xfer, KILL_RESET);
582 }
583
584 return;
585 }
586
587 int
588 siisata_ata_addref(struct ata_drive_datas *drvp)
589 {
590 return 0;
591 }
592
593 void
594 siisata_ata_delref(struct ata_drive_datas *drvp)
595 {
596 return;
597 }
598
599 void
600 siisata_killpending(struct ata_drive_datas *drvp)
601 {
602 return;
603 }
604
605 void
606 siisata_probe_drive(struct ata_channel *chp)
607 {
608 struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
609 struct siisata_channel *schp = (struct siisata_channel *)chp;
610 int i;
611 int s;
612 uint32_t sig;
613 int slot = SIISATA_NON_NCQ_SLOT;
614 struct siisata_prb *prb;
615
616 SIISATA_DEBUG_PRINT(("%s: %s: port %d start\n", SIISATANAME(sc),
617 __func__, chp->ch_channel), DEBUG_FUNCS);
618
619 /* XXX This should be done by other code. */
620 for (i = 0; i < chp->ch_ndrive; i++) {
621 chp->ch_drive[i].chnl_softc = chp;
622 chp->ch_drive[i].drive = i;
623 }
624
625 switch (sata_reset_interface(chp, sc->sc_prt, schp->sch_scontrol,
626 schp->sch_sstatus)) {
627 case SStatus_DET_DEV:
628 /* wait for ready */
629 while (!(PRREAD(sc, PRX(chp->ch_channel, PRO_PS))
630 & PR_PS_PORT_READY))
631 DELAY(10);
632
633 prb = schp->sch_prb[slot];
634 memset(prb, 0, sizeof(struct siisata_prb));
635 prb->prb_control =
636 htole16(PRB_CF_SOFT_RESET | PRB_CF_INTERRUPT_MASK);
637
638 siisata_activate_prb(schp, slot);
639
640 for(i = 0; i < 31000; i++) {
641 if (PRREAD(sc, PRX(chp->ch_channel, PRO_PSS)) &
642 PR_PXSS(slot))
643 DELAY(1000);
644 else
645 break;
646 }
647
648 siisata_deactivate_prb(schp, slot);
649
650 /* read the signature out of the FIS */
651 sig = 0;
652 sig |= (PRREAD(sc, PRSX(chp->ch_channel, slot,
653 PRSO_FIS+0x4)) & 0x00ffffff) << 8;
654 sig |= PRREAD(sc, PRSX(chp->ch_channel, slot,
655 PRSO_FIS+0xc)) & 0xff;
656
657 SIISATA_DEBUG_PRINT(("%s: %s: sig=0x%08x\n", SIISATANAME(sc),
658 __func__, sig), DEBUG_PROBE);
659
660 /* some ATAPI devices have bogus lower two bytes, sigh */
661 if ((sig & 0xffff0000) == 0xeb140000) {
662 sig &= 0xffff0000;
663 sig |= 0x00000101;
664 }
665
666 s = splbio();
667 switch (sig) {
668 case 0xeb140101:
669 chp->ch_drive[0].drive_flags |= DRIVE_ATAPI;
670 break;
671 case 0x00000101:
672 chp->ch_drive[0].drive_flags |= DRIVE_ATA;
673 break;
674 default:
675 chp->ch_drive[0].drive_flags |= DRIVE_ATA;
676 aprint_verbose_dev(sc->sc_atac.atac_dev,
677 "Unrecognized signature 0x%08x on port %d. "
678 "Assuming it's a disk.\n", sig, chp->ch_channel);
679 break;
680 }
681 splx(s);
682 break;
683 default:
684 break;
685 }
686
687 SIISATA_DEBUG_PRINT(("%s: %s: port %d done\n", SIISATANAME(sc),
688 __func__, chp->ch_channel), DEBUG_PROBE);
689 return;
690 }
691
692 void
693 siisata_setup_channel(struct ata_channel *chp)
694 {
695 return;
696 }
697
698 int
699 siisata_exec_command(struct ata_drive_datas *drvp, struct ata_command *ata_c)
700 {
701 struct ata_channel *chp = drvp->chnl_softc;
702 struct ata_xfer *xfer;
703 int ret;
704 int s;
705
706 SIISATA_DEBUG_PRINT(("%s: %s begins\n",
707 SIISATANAME((struct siisata_softc *)chp->ch_atac), __func__),
708 DEBUG_FUNCS);
709
710 xfer = ata_get_xfer(ata_c->flags & AT_WAIT ?
711 ATAXF_CANSLEEP : ATAXF_NOSLEEP);
712 if (xfer == NULL)
713 return ATACMD_TRY_AGAIN;
714 if (ata_c->flags & AT_POLL)
715 xfer->c_flags |= C_POLL;
716 if (ata_c->flags & AT_WAIT)
717 xfer->c_flags |= C_WAIT;
718 xfer->c_drive = drvp->drive;
719 xfer->c_databuf = ata_c->data;
720 xfer->c_bcount = ata_c->bcount;
721 xfer->c_cmd = ata_c;
722 xfer->c_start = siisata_cmd_start;
723 xfer->c_intr = siisata_cmd_complete;
724 xfer->c_kill_xfer = siisata_cmd_kill_xfer;
725 s = splbio();
726 ata_exec_xfer(chp, xfer);
727 #ifdef DIAGNOSTIC
728 if ((ata_c->flags & AT_POLL) != 0 &&
729 (ata_c->flags & AT_DONE) == 0)
730 panic("%s: polled command not done", __func__);
731 #endif
732 if (ata_c->flags & AT_DONE) {
733 ret = ATACMD_COMPLETE;
734 } else {
735 if (ata_c->flags & AT_WAIT) {
736 while ((ata_c->flags & AT_DONE) == 0) {
737 SIISATA_DEBUG_PRINT(("%s: %s: sleeping\n",
738 SIISATANAME(
739 (struct siisata_softc *)chp->ch_atac),
740 __func__), DEBUG_FUNCS);
741 tsleep(ata_c, PRIBIO, "siicmd", 0);
742 }
743 ret = ATACMD_COMPLETE;
744 } else {
745 ret = ATACMD_QUEUED;
746 }
747 }
748 splx(s);
749 SIISATA_DEBUG_PRINT( ("%s: %s ends\n",
750 SIISATANAME((struct siisata_softc *)chp->ch_atac), __func__),
751 DEBUG_FUNCS);
752 return ret;
753 }
754
755 void
756 siisata_cmd_start(struct ata_channel *chp, struct ata_xfer *xfer)
757 {
758 struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
759 struct siisata_channel *schp = (struct siisata_channel *)chp;
760 struct ata_command *ata_c = xfer->c_cmd;
761 int slot = SIISATA_NON_NCQ_SLOT;
762 struct siisata_prb *prb;
763 int i;
764
765 SIISATA_DEBUG_PRINT(("%s: %s port %d, slot %d\n",
766 SIISATANAME(sc), __func__, chp->ch_channel, slot), DEBUG_FUNCS);
767
768 chp->ch_status = 0;
769 chp->ch_error = 0;
770
771 prb = schp->sch_prb[slot];
772 memset(prb, 0, sizeof(struct siisata_prb));
773
774 satafis_rhd_construct_cmd(ata_c, prb->prb_fis);
775
776 memset(prb->prb_atapi, 0, sizeof(prb->prb_atapi));
777
778 if (siisata_dma_setup(chp, slot,
779 (ata_c->flags & (AT_READ | AT_WRITE)) ? ata_c->data : NULL,
780 ata_c->bcount,
781 (ata_c->flags & AT_READ) ? BUS_DMA_READ : BUS_DMA_WRITE)) {
782 ata_c->flags |= AT_DF;
783 siisata_cmd_complete(chp, xfer, slot);
784 return;
785 }
786
787 if (xfer->c_flags & C_POLL) {
788 /* polled command, disable interrupts */
789 prb->prb_control = htole16(PRB_CF_INTERRUPT_MASK);
790 }
791
792 /* go for it */
793 siisata_activate_prb(schp, slot);
794
795 if ((ata_c->flags & AT_POLL) == 0) {
796 chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */
797 callout_reset(&chp->ch_callout, mstohz(ata_c->timeout),
798 siisata_timeout, chp);
799 goto out;
800 }
801
802 /*
803 * polled command
804 */
805 for (i = 0; i < ata_c->timeout / 10; i++) {
806 if (ata_c->flags & AT_DONE)
807 break;
808 siisata_intr_port(schp);
809 DELAY(1000);
810 }
811
812 if ((ata_c->flags & AT_DONE) == 0) {
813 ata_c->flags |= AT_TIMEOU;
814 siisata_cmd_complete(chp, xfer, slot);
815 }
816
817 /* reenable interrupts */
818 GRWRITE(sc, GR_GC, GRREAD(sc, GR_GC) | GR_GC_PXIE(chp->ch_channel));
819 out:
820 SIISATA_DEBUG_PRINT(
821 ("%s: %s: done\n", SIISATANAME(sc), __func__), DEBUG_FUNCS);
822 return;
823 }
824
825 void
826 siisata_cmd_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer,
827 int reason)
828 {
829 int slot = SIISATA_NON_NCQ_SLOT;
830
831 struct ata_command *ata_c = xfer->c_cmd;
832 switch (reason) {
833 case KILL_GONE:
834 ata_c->flags |= AT_GONE;
835 break;
836 case KILL_RESET:
837 ata_c->flags |= AT_RESET;
838 break;
839 default:
840 panic("%s: port %d: unknown reason %d",
841 __func__, chp->ch_channel, reason);
842 }
843 siisata_cmd_done(chp, xfer, slot);
844 }
845
846 int
847 siisata_cmd_complete(struct ata_channel *chp, struct ata_xfer *xfer, int slot)
848 {
849 struct ata_command *ata_c = xfer->c_cmd;
850 #ifdef SIISATA_DEBUG
851 struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
852 #endif
853
854 SIISATA_DEBUG_PRINT(
855 ("%s: %s\n", SIISATANAME(sc), __func__), DEBUG_FUNCS);
856
857 chp->ch_flags &= ~ATACH_IRQ_WAIT;
858 if (xfer->c_flags & C_TIMEOU)
859 ata_c->flags |= AT_TIMEOU;
860 else
861 callout_stop(&chp->ch_callout);
862
863 if (chp->ch_status & WDCS_BSY) {
864 ata_c->flags |= AT_TIMEOU;
865 } else if (chp->ch_status & WDCS_ERR) {
866 ata_c->r_error = chp->ch_error;
867 ata_c->flags |= AT_ERROR;
868 }
869
870 if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_WAITDRAIN) {
871 siisata_cmd_kill_xfer(chp, xfer, KILL_GONE);
872 chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_WAITDRAIN;
873 wakeup(&chp->ch_queue->active_xfer);
874 return 0;
875 } else
876 siisata_cmd_done(chp, xfer, slot);
877
878 return 0;
879 }
880
881 void
882 siisata_cmd_done(struct ata_channel *chp, struct ata_xfer *xfer, int slot)
883 {
884 uint32_t fis[howmany(RDH_FISLEN,sizeof(uint32_t))];
885 struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
886 struct siisata_channel *schp = (struct siisata_channel *)chp;
887 struct ata_command *ata_c = xfer->c_cmd;
888 uint16_t *idwordbuf;
889 int i;
890
891 SIISATA_DEBUG_PRINT(
892 ("%s: %s.\n", SIISATANAME(sc), __func__), DEBUG_FUNCS);
893
894 siisata_deactivate_prb(schp, slot);
895
896 if (ata_c->flags & (AT_READ | AT_WRITE)) {
897 bus_dmamap_sync(sc->sc_dmat, schp->sch_datad[slot], 0,
898 schp->sch_datad[slot]->dm_mapsize,
899 (ata_c->flags & AT_READ) ? BUS_DMASYNC_POSTREAD :
900 BUS_DMASYNC_POSTWRITE);
901 bus_dmamap_unload(sc->sc_dmat, schp->sch_datad[slot]);
902 }
903
904 if (ata_c->flags & AT_READREG) {
905 bus_space_read_region_stream_4(sc->sc_prt, sc->sc_prh,
906 PRSX(chp->ch_channel, slot, PRSO_FIS),
907 fis, __arraycount(fis));
908 satafis_rdh_cmd_readreg(ata_c, (uint8_t *)fis);
909 }
910
911 /* correct the endianess of IDENTIFY data */
912 if (ata_c->r_command == WDCC_IDENTIFY ||
913 ata_c->r_command == ATAPI_IDENTIFY_DEVICE) {
914 idwordbuf = xfer->c_databuf;
915 for (i = 0; i < (xfer->c_bcount / sizeof(*idwordbuf)); i++) {
916 idwordbuf[i] = le16toh(idwordbuf[i]);
917 }
918 }
919
920 ata_c->flags |= AT_DONE;
921 if (PRREAD(sc, PRSX(chp->ch_channel, slot, PRSO_RTC)))
922 ata_c->flags |= AT_XFDONE;
923
924 chp->ch_queue->active_xfer = NULL;
925 ata_free_xfer(chp, xfer);
926 if (ata_c->flags & AT_WAIT)
927 wakeup(ata_c);
928 else if (ata_c->callback)
929 ata_c->callback(ata_c->callback_arg);
930 atastart(chp);
931 return;
932 }
933
934 int
935 siisata_ata_bio(struct ata_drive_datas *drvp, struct ata_bio *ata_bio)
936 {
937 struct ata_channel *chp = drvp->chnl_softc;
938 struct ata_xfer *xfer;
939
940 SIISATA_DEBUG_PRINT( ("%s: %s.\n",
941 SIISATANAME((struct siisata_softc *)chp->ch_atac),
942 __func__), DEBUG_FUNCS);
943
944 xfer = ata_get_xfer(ATAXF_NOSLEEP);
945 if (xfer == NULL)
946 return ATACMD_TRY_AGAIN;
947 if (ata_bio->flags & ATA_POLL)
948 xfer->c_flags |= C_POLL;
949 xfer->c_drive = drvp->drive;
950 xfer->c_cmd = ata_bio;
951 xfer->c_databuf = ata_bio->databuf;
952 xfer->c_bcount = ata_bio->bcount;
953 xfer->c_start = siisata_bio_start;
954 xfer->c_intr = siisata_bio_complete;
955 xfer->c_kill_xfer = siisata_bio_kill_xfer;
956 ata_exec_xfer(chp, xfer);
957 return (ata_bio->flags & ATA_ITSDONE) ?
958 ATACMD_COMPLETE : ATACMD_QUEUED;
959 }
960
961 void
962 siisata_bio_start(struct ata_channel *chp, struct ata_xfer *xfer)
963 {
964 struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
965 struct siisata_channel *schp = (struct siisata_channel *)chp;
966 struct siisata_prb *prb;
967 struct ata_bio *ata_bio = xfer->c_cmd;
968 int slot = SIISATA_NON_NCQ_SLOT;
969 int i;
970
971 SIISATA_DEBUG_PRINT(
972 ("%s: %s port %d, slot %d\n",
973 SIISATANAME(sc), __func__, chp->ch_channel, slot),
974 DEBUG_FUNCS);
975
976 chp->ch_status = 0;
977 chp->ch_error = 0;
978
979 prb = schp->sch_prb[slot];
980 memset(prb, 0, sizeof(struct siisata_prb));
981
982 satafis_rhd_construct_bio(xfer, prb->prb_fis);
983
984 memset(prb->prb_atapi, 0, sizeof(prb->prb_atapi));
985
986 if (siisata_dma_setup(chp, slot, ata_bio->databuf, ata_bio->bcount,
987 (ata_bio->flags & ATA_READ) ? BUS_DMA_READ : BUS_DMA_WRITE)) {
988 ata_bio->error = ERR_DMA;
989 ata_bio->r_error = 0;
990 siisata_bio_complete(chp, xfer, slot);
991 return;
992 }
993
994 if (xfer->c_flags & C_POLL) {
995 /* polled command, disable interrupts */
996 prb->prb_control = htole16(PRB_CF_INTERRUPT_MASK);
997 }
998
999 siisata_activate_prb(schp, slot);
1000
1001 if ((ata_bio->flags & ATA_POLL) == 0) {
1002 chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */
1003 callout_reset(&chp->ch_callout, mstohz(ATA_DELAY),
1004 siisata_timeout, chp);
1005 goto out;
1006 }
1007
1008 /*
1009 * polled command
1010 */
1011 for (i = 0; i < ATA_DELAY / 10; i++) {
1012 if (ata_bio->flags & ATA_ITSDONE)
1013 break;
1014 siisata_intr_port(schp);
1015 DELAY(1000);
1016 }
1017
1018 GRWRITE(sc, GR_GC, GRREAD(sc, GR_GC) | GR_GC_PXIE(chp->ch_channel));
1019 out:
1020 SIISATA_DEBUG_PRINT(
1021 ("%s: %s: done\n", SIISATANAME(sc), __func__), DEBUG_FUNCS);
1022 return;
1023 }
1024
1025 void
1026 siisata_bio_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer,
1027 int reason)
1028 {
1029 struct siisata_channel *schp = (struct siisata_channel *)chp;
1030 struct ata_bio *ata_bio = xfer->c_cmd;
1031 int drive = xfer->c_drive;
1032 int slot = SIISATA_NON_NCQ_SLOT;
1033
1034 SIISATA_DEBUG_PRINT(("%s: %s: port %d\n",
1035 SIISATANAME((struct siisata_softc *)chp->ch_atac),
1036 __func__, chp->ch_channel), DEBUG_FUNCS);
1037
1038 siisata_deactivate_prb(schp, slot);
1039
1040 ata_free_xfer(chp, xfer);
1041 ata_bio->flags |= ATA_ITSDONE;
1042 switch (reason) {
1043 case KILL_GONE:
1044 ata_bio->error = ERR_NODEV;
1045 break;
1046 case KILL_RESET:
1047 ata_bio->error = ERR_RESET;
1048 break;
1049 default:
1050 panic("%s: port %d: unknown reason %d",
1051 __func__, chp->ch_channel, reason);
1052 }
1053 ata_bio->r_error = WDCE_ABRT;
1054 (*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc);
1055 }
1056
1057 int
1058 siisata_bio_complete(struct ata_channel *chp, struct ata_xfer *xfer, int slot)
1059 {
1060 struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
1061 struct siisata_channel *schp = (struct siisata_channel *)chp;
1062 struct ata_bio *ata_bio = xfer->c_cmd;
1063 int drive = xfer->c_drive;
1064
1065 schp->sch_active_slots &= ~__BIT(slot);
1066 chp->ch_flags &= ~ATACH_IRQ_WAIT;
1067 if (xfer->c_flags & C_TIMEOU) {
1068 ata_bio->error = TIMEOUT;
1069 } else {
1070 callout_stop(&chp->ch_callout);
1071 ata_bio->error = NOERROR;
1072 }
1073
1074 bus_dmamap_sync(sc->sc_dmat, schp->sch_datad[slot], 0,
1075 schp->sch_datad[slot]->dm_mapsize,
1076 (ata_bio->flags & ATA_READ) ? BUS_DMASYNC_POSTREAD :
1077 BUS_DMASYNC_POSTWRITE);
1078 bus_dmamap_unload(sc->sc_dmat, schp->sch_datad[slot]);
1079
1080 if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_WAITDRAIN) {
1081 siisata_bio_kill_xfer(chp, xfer, KILL_GONE);
1082 chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_WAITDRAIN;
1083 wakeup(&chp->ch_queue->active_xfer);
1084 return 0;
1085 }
1086
1087 chp->ch_queue->active_xfer = NULL;
1088 ata_free_xfer(chp, xfer);
1089 ata_bio->flags |= ATA_ITSDONE;
1090 if (chp->ch_status & WDCS_DWF) {
1091 ata_bio->error = ERR_DF;
1092 } else if (chp->ch_status & WDCS_ERR) {
1093 ata_bio->error = ERROR;
1094 ata_bio->r_error = chp->ch_error;
1095 } else if (chp->ch_status & WDCS_CORR)
1096 ata_bio->flags |= ATA_CORR;
1097
1098 SIISATA_DEBUG_PRINT(("%s: %s bcount: %ld", SIISATANAME(sc),
1099 __func__, ata_bio->bcount), DEBUG_XFERS);
1100 if (ata_bio->error == NOERROR) {
1101 if (ata_bio->flags & ATA_READ)
1102 ata_bio->bcount -=
1103 PRREAD(sc, PRSX(chp->ch_channel, slot, PRSO_RTC));
1104 else
1105 ata_bio->bcount = 0;
1106 }
1107 SIISATA_DEBUG_PRINT((" now %ld\n", ata_bio->bcount), DEBUG_XFERS);
1108 if (ata_bio->flags & ATA_POLL)
1109 return 1;
1110 (*chp->ch_drive[drive].drv_done)(chp->ch_drive[drive].drv_softc);
1111 atastart(chp);
1112 return 0;
1113 }
1114
1115 void
1116 siisata_timeout(void *v)
1117 {
1118 struct ata_channel *chp = (struct ata_channel *)v;
1119 struct ata_xfer *xfer = chp->ch_queue->active_xfer;
1120 int slot = SIISATA_NON_NCQ_SLOT;
1121 int s = splbio();
1122 SIISATA_DEBUG_PRINT(("%s: %p\n", __func__, xfer), DEBUG_INTR);
1123 if ((chp->ch_flags & ATACH_IRQ_WAIT) != 0) {
1124 xfer->c_flags |= C_TIMEOU;
1125 xfer->c_intr(chp, xfer, slot);
1126 }
1127 splx(s);
1128 }
1129
1130 static int
1131 siisata_dma_setup(struct ata_channel *chp, int slot, void *data,
1132 size_t count, int op)
1133 {
1134
1135 int error, seg;
1136 struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
1137 struct siisata_channel *schp = (struct siisata_channel *)chp;
1138
1139 struct siisata_prb *prbp;
1140
1141 prbp = schp->sch_prb[slot];
1142
1143 if (data == NULL) {
1144 goto end;
1145 }
1146
1147 error = bus_dmamap_load(sc->sc_dmat, schp->sch_datad[slot],
1148 data, count, NULL, BUS_DMA_NOWAIT | BUS_DMA_STREAMING | op);
1149 if (error) {
1150 log(LOG_ERR, "%s port %d: "
1151 "failed to load xfer in slot %d: error %d\n",
1152 SIISATANAME(sc), chp->ch_channel, slot, error);
1153 return error;
1154 }
1155
1156 bus_dmamap_sync(sc->sc_dmat, schp->sch_datad[slot], 0,
1157 schp->sch_datad[slot]->dm_mapsize,
1158 (op == BUS_DMA_READ) ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
1159
1160 /* make sure it's clean */
1161 memset(prbp->prb_sge, 0, SIISATA_NSGE * sizeof(struct siisata_prb));
1162
1163 SIISATA_DEBUG_PRINT(("%s: %d segs, %ld count\n", __func__,
1164 schp->sch_datad[slot]->dm_nsegs, (long unsigned int) count),
1165 DEBUG_FUNCS | DEBUG_DEBUG);
1166
1167 for (seg = 0; seg < schp->sch_datad[slot]->dm_nsegs; seg++) {
1168 prbp->prb_sge[seg].sge_da =
1169 htole64(schp->sch_datad[slot]->dm_segs[seg].ds_addr);
1170 prbp->prb_sge[seg].sge_dc =
1171 htole32(schp->sch_datad[slot]->dm_segs[seg].ds_len);
1172 prbp->prb_sge[seg].sge_flags = htole32(0);
1173 }
1174 prbp->prb_sge[seg - 1].sge_flags |= htole32(SGE_FLAG_TRM);
1175 end:
1176 return 0;
1177 }
1178
1179 static void
1180 siisata_activate_prb(struct siisata_channel *schp, int slot)
1181 {
1182 struct siisata_softc *sc;
1183 bus_size_t offset;
1184 uint64_t pprb;
1185
1186 sc = (struct siisata_softc *)schp->ata_channel.ch_atac;
1187
1188 KASSERTMSG((schp->sch_active_slots & __BIT(slot)) != __BIT(slot),
1189 "%s: trying to activate active slot %d", SIISATANAME(sc), slot);
1190
1191 SIISATA_PRB_SYNC(sc, schp, slot, BUS_DMASYNC_PREWRITE);
1192 /* keep track of what's going on */
1193 schp->sch_active_slots |= __BIT(slot);
1194
1195 offset = PRO_CARX(schp->ata_channel.ch_channel, slot);
1196
1197 pprb = schp->sch_bus_prb[slot];
1198
1199 PRWRITE(sc, offset + 0, pprb >> 0);
1200 PRWRITE(sc, offset + 4, pprb >> 32);
1201 }
1202
1203 static void
1204 siisata_deactivate_prb(struct siisata_channel *schp, int slot)
1205 {
1206 struct siisata_softc *sc;
1207
1208 sc = (struct siisata_softc *)schp->ata_channel.ch_atac;
1209
1210 KASSERTMSG((schp->sch_active_slots & __BIT(slot)) != 0,
1211 "%s: trying to deactivate inactive slot %d", SIISATANAME(sc),
1212 slot);
1213
1214 schp->sch_active_slots &= ~__BIT(slot); /* mark free */
1215 SIISATA_PRB_SYNC(sc, schp, slot, BUS_DMASYNC_POSTWRITE);
1216 }
1217
1218 static void
1219 siisata_reinit_port(struct ata_channel *chp)
1220 {
1221 struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
1222
1223 PRWRITE(sc, PRX(chp->ch_channel, PRO_PCS), PR_PC_PORT_INITIALIZE);
1224 while (!(PRREAD(sc, PRX(chp->ch_channel, PRO_PS)) & PR_PS_PORT_READY))
1225 DELAY(10);
1226 }
1227
1228 static void
1229 siisata_device_reset(struct ata_channel *chp)
1230 {
1231 struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
1232
1233 PRWRITE(sc, PRX(chp->ch_channel, PRO_PCS), PR_PC_DEVICE_RESET);
1234 while (!(PRREAD(sc, PRX(chp->ch_channel, PRO_PS)) & PR_PS_PORT_READY))
1235 DELAY(10);
1236 }
1237
1238
1239 #if NATAPIBUS > 0
1240 void
1241 siisata_atapibus_attach(struct atabus_softc *ata_sc)
1242 {
1243 struct ata_channel *chp = ata_sc->sc_chan;
1244 struct atac_softc *atac = chp->ch_atac;
1245 struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic;
1246 struct scsipi_channel *chan = &chp->ch_atapi_channel;
1247
1248 /*
1249 * Fill in the scsipi_adapter.
1250 */
1251 adapt->adapt_dev = atac->atac_dev;
1252 adapt->adapt_nchannels = atac->atac_nchannels;
1253 adapt->adapt_request = siisata_atapi_scsipi_request;
1254 adapt->adapt_minphys = siisata_atapi_minphys;
1255 atac->atac_atapi_adapter.atapi_probe_device =
1256 siisata_atapi_probe_device;
1257
1258 /*
1259 * Fill in the scsipi_channel.
1260 */
1261 memset(chan, 0, sizeof(*chan));
1262 chan->chan_adapter = adapt;
1263 chan->chan_bustype = &siisata_atapi_bustype;
1264 chan->chan_channel = chp->ch_channel;
1265 chan->chan_flags = SCSIPI_CHAN_OPENINGS;
1266 chan->chan_openings = 1;
1267 chan->chan_max_periph = 1;
1268 chan->chan_ntargets = 1;
1269 chan->chan_nluns = 1;
1270
1271 chp->atapibus = config_found_ia(ata_sc->sc_dev, "atapi", chan,
1272 atapiprint);
1273 }
1274
1275 void
1276 siisata_atapi_minphys(struct buf *bp)
1277 {
1278 if (bp->b_bcount > MAXPHYS)
1279 bp->b_bcount = MAXPHYS;
1280 minphys(bp);
1281 }
1282
1283 /*
1284 * Kill off all pending xfers for a periph.
1285 *
1286 * Must be called at splbio().
1287 */
1288 void
1289 siisata_atapi_kill_pending(struct scsipi_periph *periph)
1290 {
1291 struct atac_softc *atac =
1292 device_private(periph->periph_channel->chan_adapter->adapt_dev);
1293 struct ata_channel *chp =
1294 atac->atac_channels[periph->periph_channel->chan_channel];
1295
1296 ata_kill_pending(&chp->ch_drive[periph->periph_target]);
1297 }
1298
1299 void
1300 siisata_atapi_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer,
1301 int reason)
1302 {
1303 struct scsipi_xfer *sc_xfer = xfer->c_cmd;
1304
1305 /* remove this command from xfer queue */
1306 switch (reason) {
1307 case KILL_GONE:
1308 sc_xfer->error = XS_DRIVER_STUFFUP;
1309 break;
1310 case KILL_RESET:
1311 sc_xfer->error = XS_RESET;
1312 break;
1313 default:
1314 panic("%s: port %d: unknown reason %d",
1315 __func__, chp->ch_channel, reason);
1316 }
1317 ata_free_xfer(chp, xfer);
1318 scsipi_done(sc_xfer);
1319 }
1320
1321 void
1322 siisata_atapi_probe_device(struct atapibus_softc *sc, int target)
1323 {
1324 struct scsipi_channel *chan = sc->sc_channel;
1325 struct scsipi_periph *periph;
1326 struct ataparams ids;
1327 struct ataparams *id = &ids;
1328 struct siisata_softc *siic =
1329 device_private(chan->chan_adapter->adapt_dev);
1330 struct atac_softc *atac = &siic->sc_atac;
1331 struct ata_channel *chp = atac->atac_channels[chan->chan_channel];
1332 struct ata_drive_datas *drvp = &chp->ch_drive[target];
1333 struct scsipibus_attach_args sa;
1334 char serial_number[21], model[41], firmware_revision[9];
1335 int s;
1336
1337 /* skip if already attached */
1338 if (scsipi_lookup_periph(chan, target, 0) != NULL)
1339 return;
1340
1341 /* if no ATAPI device detected at attach time, skip */
1342 if ((drvp->drive_flags & DRIVE_ATAPI) == 0) {
1343 SIISATA_DEBUG_PRINT(("%s: drive %d "
1344 "not present\n", __func__, target), DEBUG_PROBE);
1345 return;
1346 }
1347
1348 /* Some ATAPI devices need a bit more time after software reset. */
1349 DELAY(5000);
1350 if (ata_get_params(drvp, AT_WAIT, id) == 0) {
1351 #ifdef ATAPI_DEBUG_PROBE
1352 log(LOG_DEBUG, "%s drive %d: cmdsz 0x%x drqtype 0x%x\n",
1353 device_xname(sc->sc_dev), target,
1354 id->atap_config & ATAPI_CFG_CMD_MASK,
1355 id->atap_config & ATAPI_CFG_DRQ_MASK);
1356 #endif
1357 periph = scsipi_alloc_periph(M_NOWAIT);
1358 if (periph == NULL) {
1359 aprint_error_dev(sc->sc_dev,
1360 "%s: unable to allocate periph for "
1361 "channel %d drive %d\n", __func__,
1362 chp->ch_channel, target);
1363 return;
1364 }
1365 periph->periph_dev = NULL;
1366 periph->periph_channel = chan;
1367 periph->periph_switch = &atapi_probe_periphsw;
1368 periph->periph_target = target;
1369 periph->periph_lun = 0;
1370 periph->periph_quirks = PQUIRK_ONLYBIG;
1371
1372 #ifdef SCSIPI_DEBUG
1373 if (SCSIPI_DEBUG_TYPE == SCSIPI_BUSTYPE_ATAPI &&
1374 SCSIPI_DEBUG_TARGET == target)
1375 periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS;
1376 #endif
1377 periph->periph_type = ATAPI_CFG_TYPE(id->atap_config);
1378 if (id->atap_config & ATAPI_CFG_REMOV)
1379 periph->periph_flags |= PERIPH_REMOVABLE;
1380 if (periph->periph_type == T_SEQUENTIAL) {
1381 s = splbio();
1382 drvp->drive_flags |= DRIVE_ATAPIST;
1383 splx(s);
1384 }
1385
1386 sa.sa_periph = periph;
1387 sa.sa_inqbuf.type = ATAPI_CFG_TYPE(id->atap_config);
1388 sa.sa_inqbuf.removable = id->atap_config & ATAPI_CFG_REMOV ?
1389 T_REMOV : T_FIXED;
1390 scsipi_strvis((u_char *)model, 40, id->atap_model, 40);
1391 scsipi_strvis((u_char *)serial_number, 20,
1392 id->atap_serial, 20);
1393 scsipi_strvis((u_char *)firmware_revision, 8,
1394 id->atap_revision, 8);
1395 sa.sa_inqbuf.vendor = model;
1396 sa.sa_inqbuf.product = serial_number;
1397 sa.sa_inqbuf.revision = firmware_revision;
1398
1399 /*
1400 * Determine the operating mode capabilities of the device.
1401 */
1402 if ((id->atap_config & ATAPI_CFG_CMD_MASK)
1403 == ATAPI_CFG_CMD_16) {
1404 periph->periph_cap |= PERIPH_CAP_CMD16;
1405
1406 /* configure port for packet length */
1407 PRWRITE(siic, PRX(chp->ch_channel, PRO_PCS),
1408 PR_PC_PACKET_LENGTH);
1409 } else {
1410 PRWRITE(siic, PRX(chp->ch_channel, PRO_PCC),
1411 PR_PC_PACKET_LENGTH);
1412 }
1413
1414 /* XXX This is gross. */
1415 periph->periph_cap |= (id->atap_config & ATAPI_CFG_DRQ_MASK);
1416
1417 drvp->drv_softc = atapi_probe_device(sc, target, periph, &sa);
1418
1419 if (drvp->drv_softc)
1420 ata_probe_caps(drvp);
1421 else {
1422 s = splbio();
1423 drvp->drive_flags &= ~DRIVE_ATAPI;
1424 splx(s);
1425 }
1426 } else {
1427 SIISATA_DEBUG_PRINT(("%s: ATAPI_IDENTIFY_DEVICE "
1428 "failed for drive %s:%d:%d: error 0x%x\n",
1429 __func__, SIISATANAME(siic), chp->ch_channel, target,
1430 chp->ch_error), DEBUG_PROBE);
1431 s = splbio();
1432 drvp->drive_flags &= ~DRIVE_ATAPI;
1433 splx(s);
1434 }
1435 }
1436
1437 void
1438 siisata_atapi_scsipi_request(struct scsipi_channel *chan,
1439 scsipi_adapter_req_t req, void *arg)
1440 {
1441 struct scsipi_adapter *adapt = chan->chan_adapter;
1442 struct scsipi_periph *periph;
1443 struct scsipi_xfer *sc_xfer;
1444 struct siisata_softc *sc = device_private(adapt->adapt_dev);
1445 struct atac_softc *atac = &sc->sc_atac;
1446 struct ata_xfer *xfer;
1447 int channel = chan->chan_channel;
1448 int drive, s;
1449
1450 switch (req) {
1451 case ADAPTER_REQ_RUN_XFER:
1452 sc_xfer = arg;
1453 periph = sc_xfer->xs_periph;
1454 drive = periph->periph_target;
1455
1456 SIISATA_DEBUG_PRINT(("%s: %s:%d:%d\n", __func__,
1457 device_xname(atac->atac_dev), channel, drive),
1458 DEBUG_XFERS);
1459
1460 if (!device_is_active(atac->atac_dev)) {
1461 sc_xfer->error = XS_DRIVER_STUFFUP;
1462 scsipi_done(sc_xfer);
1463 return;
1464 }
1465 xfer = ata_get_xfer(ATAXF_NOSLEEP);
1466 if (xfer == NULL) {
1467 sc_xfer->error = XS_RESOURCE_SHORTAGE;
1468 scsipi_done(sc_xfer);
1469 return;
1470 }
1471
1472 if (sc_xfer->xs_control & XS_CTL_POLL)
1473 xfer->c_flags |= C_POLL;
1474 xfer->c_drive = drive;
1475 xfer->c_flags |= C_ATAPI;
1476 xfer->c_cmd = sc_xfer;
1477 xfer->c_databuf = sc_xfer->data;
1478 xfer->c_bcount = sc_xfer->datalen;
1479 xfer->c_start = siisata_atapi_start;
1480 xfer->c_intr = siisata_atapi_complete;
1481 xfer->c_kill_xfer = siisata_atapi_kill_xfer;
1482 xfer->c_dscpoll = 0;
1483 s = splbio();
1484 ata_exec_xfer(atac->atac_channels[channel], xfer);
1485 #ifdef DIAGNOSTIC
1486 if ((sc_xfer->xs_control & XS_CTL_POLL) != 0 &&
1487 (sc_xfer->xs_status & XS_STS_DONE) == 0)
1488 panic("%s: polled command not done", __func__);
1489 #endif
1490 splx(s);
1491 return;
1492
1493 default:
1494 /* Not supported, nothing to do. */
1495 ;
1496 }
1497 }
1498
1499 void
1500 siisata_atapi_start(struct ata_channel *chp, struct ata_xfer *xfer)
1501 {
1502 struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
1503 struct siisata_channel *schp = (struct siisata_channel *)chp;
1504 struct siisata_prb *prbp;
1505
1506 struct scsipi_xfer *sc_xfer = xfer->c_cmd;
1507
1508 int slot = SIISATA_NON_NCQ_SLOT;
1509 int i;
1510
1511 SIISATA_DEBUG_PRINT( ("%s: %s:%d:%d, scsi flags 0x%x\n", __func__,
1512 SIISATANAME(sc), chp->ch_channel,
1513 chp->ch_drive[xfer->c_drive].drive, sc_xfer->xs_control),
1514 DEBUG_XFERS);
1515
1516 chp->ch_status = 0;
1517 chp->ch_error = 0;
1518
1519 prbp = schp->sch_prb[slot];
1520 memset(prbp, 0, sizeof(struct siisata_prb));
1521
1522
1523 /* fill in direction for ATAPI command */
1524 if ((sc_xfer->xs_control & XS_CTL_DATA_IN))
1525 prbp->prb_control |= htole16(PRB_CF_PACKET_READ);
1526 if ((sc_xfer->xs_control & XS_CTL_DATA_OUT))
1527 prbp->prb_control |= htole16(PRB_CF_PACKET_WRITE);
1528
1529 satafis_rhd_construct_atapi(xfer, prbp->prb_fis);
1530
1531 /* copy over ATAPI command */
1532 memcpy(prbp->prb_atapi, sc_xfer->cmd, sc_xfer->cmdlen);
1533
1534 if (siisata_dma_setup(chp, slot,
1535 (sc_xfer->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) ?
1536 xfer->c_databuf : NULL,
1537 xfer->c_bcount,
1538 (sc_xfer->xs_control & XS_CTL_DATA_IN) ?
1539 BUS_DMA_READ : BUS_DMA_WRITE)
1540 )
1541 panic("%s", __func__);
1542
1543 if (xfer->c_flags & C_POLL) {
1544 /* polled command, disable interrupts */
1545 prbp->prb_control = htole16(PRB_CF_INTERRUPT_MASK);
1546 }
1547
1548 siisata_activate_prb(schp, slot);
1549
1550 if ((xfer->c_flags & C_POLL) == 0) {
1551 chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */
1552 callout_reset(&chp->ch_callout, mstohz(sc_xfer->timeout),
1553 siisata_timeout, chp);
1554 goto out;
1555 }
1556
1557 /*
1558 * polled command
1559 */
1560 for (i = 0; i < ATA_DELAY / 10; i++) {
1561 if (sc_xfer->xs_status & XS_STS_DONE)
1562 break;
1563 siisata_intr_port(schp);
1564 DELAY(1000);
1565 }
1566 if ((sc_xfer->xs_status & XS_STS_DONE) == 0) {
1567 sc_xfer->error = XS_TIMEOUT;
1568 siisata_atapi_complete(chp, xfer, slot);
1569 }
1570 /* reenable interrupts */
1571 GRWRITE(sc, GR_GC, GRREAD(sc, GR_GC) | GR_GC_PXIE(chp->ch_channel));
1572 out:
1573 SIISATA_DEBUG_PRINT(
1574 ("%s: %s: done\n", SIISATANAME(sc), __func__), DEBUG_FUNCS);
1575 return;
1576 }
1577
1578 int
1579 siisata_atapi_complete(struct ata_channel *chp, struct ata_xfer *xfer,
1580 int slot)
1581 {
1582 struct siisata_softc *sc = (struct siisata_softc *)chp->ch_atac;
1583 struct siisata_channel *schp = (struct siisata_channel *)chp;
1584 struct scsipi_xfer *sc_xfer = xfer->c_cmd;
1585
1586 SIISATA_DEBUG_PRINT(
1587 ("%s: %s()\n", SIISATANAME(sc), __func__), DEBUG_INTR);
1588
1589 /* this comamnd is not active any more */
1590 schp->sch_active_slots &= ~__BIT(slot);
1591 chp->ch_flags &= ~ATACH_IRQ_WAIT;
1592 if (xfer->c_flags & C_TIMEOU) {
1593 sc_xfer->error = XS_TIMEOUT;
1594 } else {
1595 callout_stop(&chp->ch_callout);
1596 sc_xfer->error = XS_NOERROR;
1597 }
1598
1599 bus_dmamap_sync(sc->sc_dmat, schp->sch_datad[slot], 0,
1600 schp->sch_datad[slot]->dm_mapsize,
1601 (sc_xfer->xs_control & XS_CTL_DATA_IN) ?
1602 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1603 bus_dmamap_unload(sc->sc_dmat, schp->sch_datad[slot]);
1604
1605 if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_WAITDRAIN) {
1606 siisata_atapi_kill_xfer(chp, xfer, KILL_GONE);
1607 chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_WAITDRAIN;
1608 wakeup(&chp->ch_queue->active_xfer);
1609 return 0; /* XXX verify */
1610 }
1611
1612 chp->ch_queue->active_xfer = NULL;
1613 ata_free_xfer(chp, xfer);
1614 sc_xfer->resid = sc_xfer->datalen;
1615 sc_xfer->resid -= PRREAD(sc, PRSX(chp->ch_channel, slot, PRSO_RTC));
1616 SIISATA_DEBUG_PRINT(("%s: %s datalen %d resid %d\n", SIISATANAME(sc),
1617 __func__, sc_xfer->datalen, sc_xfer->resid), DEBUG_XFERS);
1618 if ((chp->ch_status & WDCS_ERR) &&
1619 ((sc_xfer->xs_control & XS_CTL_REQSENSE) == 0 ||
1620 sc_xfer->resid == sc_xfer->datalen)) {
1621 sc_xfer->error = XS_SHORTSENSE;
1622 sc_xfer->sense.atapi_sense = chp->ch_error;
1623 if ((sc_xfer->xs_periph->periph_quirks &
1624 PQUIRK_NOSENSE) == 0) {
1625 /* request sense */
1626 sc_xfer->error = XS_BUSY;
1627 sc_xfer->status = SCSI_CHECK;
1628 }
1629 }
1630 scsipi_done(sc_xfer);
1631 atastart(chp);
1632 return 0; /* XXX verify */
1633 }
1634
1635 #endif /* NATAPIBUS */
1636