2 * Copyright (c) 2009-2012 Alexander Motin <mav@FreeBSD.org>
3 * Copyright (c) 2017 Justin Hibbits <jhibbits@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/module.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
37 #include <sys/endian.h>
39 #include <sys/malloc.h>
40 #include <sys/mutex.h>
44 #include <cam/cam_ccb.h>
45 #include <cam/cam_sim.h>
46 #include <cam/cam_xpt_sim.h>
47 #include <cam/cam_debug.h>
49 #include <dev/ofw/ofw_bus_subr.h>
51 #include <machine/bus.h>
52 #include <machine/resource.h>
56 struct fsl_sata_channel;
58 enum fsl_sata_err_type;
59 struct fsl_sata_cmd_tab;
62 /* local prototypes */
63 static int fsl_sata_init(device_t dev);
64 static int fsl_sata_deinit(device_t dev);
65 static int fsl_sata_suspend(device_t dev);
66 static int fsl_sata_resume(device_t dev);
67 static void fsl_sata_pm(void *arg);
68 static void fsl_sata_intr(void *arg);
69 static void fsl_sata_intr_main(struct fsl_sata_channel *ch, uint32_t istatus);
70 static void fsl_sata_begin_transaction(struct fsl_sata_channel *ch, union ccb *ccb);
71 static void fsl_sata_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error);
72 static void fsl_sata_execute_transaction(struct fsl_sata_slot *slot);
73 static void fsl_sata_timeout(struct fsl_sata_slot *slot);
74 static void fsl_sata_end_transaction(struct fsl_sata_slot *slot, enum fsl_sata_err_type et);
75 static int fsl_sata_setup_fis(struct fsl_sata_channel *ch, struct fsl_sata_cmd_tab *ctp, union ccb *ccb, int tag);
76 static void fsl_sata_dmainit(device_t dev);
77 static void fsl_sata_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error);
78 static void fsl_sata_dmafini(device_t dev);
79 static void fsl_sata_slotsalloc(device_t dev);
80 static void fsl_sata_slotsfree(device_t dev);
81 static void fsl_sata_reset(struct fsl_sata_channel *ch);
82 static void fsl_sata_start(struct fsl_sata_channel *ch);
83 static void fsl_sata_stop(struct fsl_sata_channel *ch);
85 static void fsl_sata_issue_recovery(struct fsl_sata_channel *ch);
86 static void fsl_sata_process_read_log(struct fsl_sata_channel *ch, union ccb *ccb);
87 static void fsl_sata_process_request_sense(struct fsl_sata_channel *ch, union ccb *ccb);
89 static void fsl_sataaction(struct cam_sim *sim, union ccb *ccb);
90 static void fsl_satapoll(struct cam_sim *sim);
92 static MALLOC_DEFINE(M_FSL_SATA, "FSL SATA driver", "FSL SATA driver data buffers");
94 #define recovery_type spriv_field0
95 #define RECOVERY_NONE 0
96 #define RECOVERY_READ_LOG 1
97 #define RECOVERY_REQUEST_SENSE 2
98 #define recovery_slot spriv_field1
100 #define FSL_SATA_P_CQR 0x0
101 #define FSL_SATA_P_CAR 0x4
102 #define FSL_SATA_P_CCR 0x10
103 #define FSL_SATA_P_CER 0x18
104 #define FSL_SATA_P_DER 0x20
105 #define FSL_SATA_P_CHBA 0x24
106 #define FSL_SATA_P_HSTS 0x28
107 #define FSL_SATA_P_HSTS_HS_ON 0x80000000
108 #define FSL_SATA_P_HSTS_ME 0x00040000
109 #define FSL_SATA_P_HSTS_DLM 0x00001000
110 #define FSL_SATA_P_HSTS_FOT 0x00000200
111 #define FSL_SATA_P_HSTS_FOR 0x00000100
112 #define FSL_SATA_P_HSTS_FE 0x00000020
113 #define FSL_SATA_P_HSTS_PR 0x00000010
114 #define FSL_SATA_P_HSTS_SNTFU 0x00000004
115 #define FSL_SATA_P_HSTS_DE 0x00000002
116 #define FSL_SATA_P_HCTRL 0x2c
117 #define FSL_SATA_P_HCTRL_HC_ON 0x80000000
118 #define FSL_SATA_P_HCTRL_HC_FORCE_OFF 0x40000000
119 #define FSL_SATA_P_HCTRL_ENT 0x10000000
120 #define FSL_SATA_P_HCTRL_SNOOP 0x00000400
121 #define FSL_SATA_P_HCTRL_PM 0x00000200
122 #define FSL_SATA_P_HCTRL_FATAL 0x00000020
123 #define FSL_SATA_P_HCTRL_PHYRDY 0x00000010
124 #define FSL_SATA_P_HCTRL_SIG 0x00000008
125 #define FSL_SATA_P_HCTRL_SNTFY 0x00000004
126 #define FSL_SATA_P_HCTRL_DE 0x00000002
127 #define FSL_SATA_P_HCTRL_CC 0x00000001
128 #define FSL_SATA_P_HCTRL_INT_MASK 0x0000003f
129 #define FSL_SATA_P_CQPMP 0x30
130 #define FSL_SATA_P_SIG 0x34
131 #define FSL_SATA_P_ICC 0x38
132 #define FSL_SATA_P_ICC_ITC_M 0x1f000000
133 #define FSL_SATA_P_ICC_ITC_S 24
134 #define FSL_SATA_P_ICC_ITTCV_M 0x0007ffff
135 #define FSL_SATA_P_PCC 0x15c
136 #define FSL_SATA_P_PCC_SLUMBER 0x0000000c
137 #define FSL_SATA_P_PCC_PARTIAL 0x0000000a
138 #define FSL_SATA_PCC_LPB_EN 0x0000000e
140 #define FSL_SATA_MAX_SLOTS 16
141 /* FSL_SATA register defines */
143 #define FSL_SATA_P_SSTS 0x100
144 #define FSL_SATA_P_SERR 0x104
145 #define FSL_SATA_P_SCTL 0x108
146 #define FSL_SATA_P_SNTF 0x10c
148 /* Pessimistic prognosis on number of required S/G entries */
149 #define FSL_SATA_SG_ENTRIES 63
150 /* Command list. 16 commands. First, 1Kbyte aligned. */
151 #define FSL_SATA_CL_OFFSET 0
152 #define FSL_SATA_CL_SIZE 16
153 /* Command tables. Up to 32 commands, Each, 4-byte aligned. */
154 #define FSL_SATA_CT_OFFSET (FSL_SATA_CL_OFFSET + FSL_SATA_CL_SIZE * FSL_SATA_MAX_SLOTS)
155 #define FSL_SATA_CT_SIZE (96 + FSL_SATA_SG_ENTRIES * 16)
156 /* Total main work area. */
157 #define FSL_SATA_WORK_SIZE (FSL_SATA_CT_OFFSET + FSL_SATA_CT_SIZE * FSL_SATA_MAX_SLOTS)
158 #define FSL_SATA_MAX_XFER (64 * 1024 * 1024)
160 /* Some convenience macros for getting the CTP and CLP */
161 #define FSL_SATA_CTP_BUS(ch, slot) \
162 ((ch->dma.work_bus + FSL_SATA_CT_OFFSET + (FSL_SATA_CT_SIZE * slot->slot)))
163 #define FSL_SATA_PRD_OFFSET(prd) (96 + (prd) * 16)
164 #define FSL_SATA_CTP(ch, slot) \
165 ((struct fsl_sata_cmd_tab *)(ch->dma.work + FSL_SATA_CT_OFFSET + \
166 (FSL_SATA_CT_SIZE * slot->slot)))
167 #define FSL_SATA_CLP(ch, slot) \
168 ((struct fsl_sata_cmd_list *) (ch->dma.work + FSL_SATA_CL_OFFSET + \
169 (FSL_SATA_CL_SIZE * slot->slot)))
171 struct fsl_sata_dma_prd {
175 uint32_t dwc_flg; /* 0 based */
176 #define FSL_SATA_PRD_MASK 0x01fffffc /* max 32MB */
177 #define FSL_SATA_PRD_MAX (FSL_SATA_PRD_MASK + 4)
178 #define FSL_SATA_PRD_SNOOP 0x10000000
179 #define FSL_SATA_PRD_EXT 0x80000000
182 struct fsl_sata_cmd_tab {
186 uint8_t reserved[16];
187 struct fsl_sata_dma_prd prd_tab[FSL_SATA_SG_ENTRIES];
188 #define FSL_SATA_PRD_EXT_INDEX 15
189 #define FSL_SATA_PRD_MAX_DIRECT 16
192 struct fsl_sata_cmd_list {
193 uint32_t cda; /* word aligned */
194 uint16_t fis_length; /* length in bytes (aligned to words) */
195 uint16_t prd_length; /* PRD entries */
198 #define FSL_SATA_CMD_TAG_MASK 0x001f
199 #define FSL_SATA_CMD_ATAPI 0x0020
200 #define FSL_SATA_CMD_BIST 0x0040
201 #define FSL_SATA_CMD_RESET 0x0080
202 #define FSL_SATA_CMD_QUEUED 0x0100
203 #define FSL_SATA_CMD_SNOOP 0x0200
204 #define FSL_SATA_CMD_VBIST 0x0400
205 #define FSL_SATA_CMD_WRITE 0x0800
210 #define ATA_IRQ_RID 0
211 #define ATA_INTR_FLAGS (INTR_MPSAFE|INTR_TYPE_BIO|INTR_ENTROPY)
214 bus_dmamap_t data_map; /* data DMA map */
215 int nsegs; /* Number of segs loaded */
218 /* structure holding DMA related information */
220 bus_dma_tag_t work_tag; /* workspace DMA tag */
221 bus_dmamap_t work_map; /* workspace DMA map */
222 uint8_t *work; /* workspace */
223 bus_addr_t work_bus; /* bus address of work */
224 bus_dma_tag_t data_tag; /* data DMA tag */
227 enum fsl_sata_slot_states {
229 FSL_SATA_SLOT_LOADING,
230 FSL_SATA_SLOT_RUNNING,
231 FSL_SATA_SLOT_EXECUTING
234 struct fsl_sata_slot {
235 struct fsl_sata_channel *ch; /* Channel */
236 uint8_t slot; /* Number of this slot */
237 enum fsl_sata_slot_states state; /* Slot state */
238 union ccb *ccb; /* CCB occupying slot */
239 struct ata_dmaslot dma; /* DMA data of this slot */
240 struct callout timeout; /* Execution timeout */
244 struct fsl_sata_device {
253 /* structure describing an ATA channel */
254 struct fsl_sata_channel {
255 device_t dev; /* Device handle */
256 int unit; /* Physical channel */
257 struct resource *r_mem; /* Memory of this channel */
258 struct resource *r_irq; /* Interrupt of this channel */
259 void *ih; /* Interrupt handle */
260 struct ata_dma dma; /* DMA data */
262 struct cam_path *path;
263 uint32_t caps; /* Controller capabilities */
264 int pm_level; /* power management level */
265 int devices; /* What is present */
266 int pm_present; /* PM presence reported */
268 union ccb *hold[FSL_SATA_MAX_SLOTS];
269 struct fsl_sata_slot slot[FSL_SATA_MAX_SLOTS];
270 uint32_t oslots; /* Occupied slots */
271 uint32_t rslots; /* Running slots */
272 uint32_t aslots; /* Slots with atomic commands */
273 uint32_t eslots; /* Slots in error */
274 uint32_t toslots; /* Slots in timeout */
275 int lastslot; /* Last used slot */
276 int taggedtarget; /* Last tagged target */
277 int numrslots; /* Number of running slots */
278 int numrslotspd[16];/* Number of running slots per dev */
279 int numtslots; /* Number of tagged slots */
280 int numtslotspd[16];/* Number of tagged slots per dev */
281 int numhslots; /* Number of held slots */
282 int recoverycmd; /* Our READ LOG active */
283 int fatalerr; /* Fatal error happend */
284 int resetting; /* Hard-reset in progress. */
285 int resetpolldiv; /* Hard-reset poll divider. */
286 union ccb *frozen; /* Frozen command */
287 struct callout pm_timer; /* Power management events */
288 struct callout reset_timer; /* Hard-reset timeout */
290 struct fsl_sata_device user[16]; /* User-specified settings */
291 struct fsl_sata_device curr[16]; /* Current settings */
293 struct mtx_padalign mtx; /* state lock */
294 STAILQ_HEAD(, ccb_hdr) doneq; /* queue of completed CCBs */
295 int batch; /* doneq is in use */
298 enum fsl_sata_err_type {
299 FSL_SATA_ERR_NONE, /* No error */
300 FSL_SATA_ERR_INVALID, /* Error detected by us before submitting. */
301 FSL_SATA_ERR_INNOCENT, /* Innocent victim. */
302 FSL_SATA_ERR_TFE, /* Task File Error. */
303 FSL_SATA_ERR_SATA, /* SATA error. */
304 FSL_SATA_ERR_TIMEOUT, /* Command execution timeout. */
305 FSL_SATA_ERR_NCQ, /* NCQ command error. CCB should be put on hold
306 * until READ LOG executed to reveal error. */
309 /* macros to hide busspace uglyness */
310 #define ATA_INB(res, offset) \
311 bus_read_1((res), (offset))
312 #define ATA_INW(res, offset) \
313 bus_read_2((res), (offset))
314 #define ATA_INL(res, offset) \
315 bus_read_4((res), (offset))
316 #define ATA_INSW(res, offset, addr, count) \
317 bus_read_multi_2((res), (offset), (addr), (count))
318 #define ATA_INSW_STRM(res, offset, addr, count) \
319 bus_read_multi_stream_2((res), (offset), (addr), (count))
320 #define ATA_INSL(res, offset, addr, count) \
321 bus_read_multi_4((res), (offset), (addr), (count))
322 #define ATA_INSL_STRM(res, offset, addr, count) \
323 bus_read_multi_stream_4((res), (offset), (addr), (count))
324 #define ATA_OUTB(res, offset, value) \
325 bus_write_1((res), (offset), (value))
326 #define ATA_OUTW(res, offset, value) \
327 bus_write_2((res), (offset), (value))
328 #define ATA_OUTL(res, offset, value) \
329 bus_write_4((res), (offset), (value))
330 #define ATA_OUTSW(res, offset, addr, count) \
331 bus_write_multi_2((res), (offset), (addr), (count))
332 #define ATA_OUTSW_STRM(res, offset, addr, count) \
333 bus_write_multi_stream_2((res), (offset), (addr), (count))
334 #define ATA_OUTSL(res, offset, addr, count) \
335 bus_write_multi_4((res), (offset), (addr), (count))
336 #define ATA_OUTSL_STRM(res, offset, addr, count) \
337 bus_write_multi_stream_4((res), (offset), (addr), (count))
340 fsl_sata_probe(device_t dev)
343 if (!ofw_bus_is_compatible(dev, "fsl,pq-sata-v2") &&
344 !ofw_bus_is_compatible(dev, "fsl,pq-sata"))
347 device_set_desc_copy(dev, "Freescale Integrated SATA Controller");
348 return (BUS_PROBE_DEFAULT);
352 fsl_sata_attach(device_t dev)
354 struct fsl_sata_channel *ch = device_get_softc(dev);
355 struct cam_devq *devq;
356 int rid, error, i, sata_rev = 0;
359 ch->unit = (intptr_t)device_get_ivars(dev);
360 mtx_init(&ch->mtx, "FSL SATA channel lock", NULL, MTX_DEF);
362 resource_int_value(device_get_name(dev),
363 device_get_unit(dev), "pm_level", &ch->pm_level);
364 STAILQ_INIT(&ch->doneq);
365 if (ch->pm_level > 3)
366 callout_init_mtx(&ch->pm_timer, &ch->mtx, 0);
367 resource_int_value(device_get_name(dev),
368 device_get_unit(dev), "sata_rev", &sata_rev);
369 for (i = 0; i < 16; i++) {
370 ch->user[i].revision = sata_rev;
371 ch->user[i].mode = 0;
372 ch->user[i].bytecount = 8192;
373 ch->user[i].tags = FSL_SATA_MAX_SLOTS;
374 ch->user[i].caps = 0;
375 ch->curr[i] = ch->user[i];
377 ch->user[i].caps = CTS_SATA_CAPS_H_PMREQ |
378 CTS_SATA_CAPS_D_PMREQ;
380 ch->user[i].caps |= CTS_SATA_CAPS_H_AN;
383 if (!(ch->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
386 rman_set_bustag(ch->r_mem, &bs_le_tag);
387 fsl_sata_dmainit(dev);
388 fsl_sata_slotsalloc(dev);
391 if (!(ch->r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
392 &rid, RF_SHAREABLE | RF_ACTIVE))) {
393 device_printf(dev, "Unable to map interrupt\n");
397 if ((bus_setup_intr(dev, ch->r_irq, ATA_INTR_FLAGS, NULL,
398 fsl_sata_intr, ch, &ch->ih))) {
399 device_printf(dev, "Unable to setup interrupt\n");
404 /* Create the device queue for our SIM. */
405 devq = cam_simq_alloc(FSL_SATA_MAX_SLOTS);
407 device_printf(dev, "Unable to allocate simq\n");
411 /* Construct SIM entry */
412 ch->sim = cam_sim_alloc(fsl_sataaction, fsl_satapoll, "fslsata", ch,
413 device_get_unit(dev), (struct mtx *)&ch->mtx, 2, FSL_SATA_MAX_SLOTS,
415 if (ch->sim == NULL) {
417 device_printf(dev, "unable to allocate sim\n");
421 if (xpt_bus_register(ch->sim, dev, 0) != CAM_SUCCESS) {
422 device_printf(dev, "unable to register xpt bus\n");
426 if (xpt_create_path(&ch->path, /*periph*/NULL, cam_sim_path(ch->sim),
427 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
428 device_printf(dev, "unable to create path\n");
432 if (ch->pm_level > 3) {
433 callout_reset(&ch->pm_timer,
434 (ch->pm_level == 4) ? hz / 1000 : hz / 8,
437 mtx_unlock(&ch->mtx);
441 xpt_bus_deregister(cam_sim_path(ch->sim));
443 cam_sim_free(ch->sim, /*free_devq*/TRUE);
445 mtx_unlock(&ch->mtx);
446 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
448 bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
449 mtx_destroy(&ch->mtx);
454 fsl_sata_detach(device_t dev)
456 struct fsl_sata_channel *ch = device_get_softc(dev);
459 xpt_async(AC_LOST_DEVICE, ch->path, NULL);
461 xpt_free_path(ch->path);
462 xpt_bus_deregister(cam_sim_path(ch->sim));
463 cam_sim_free(ch->sim, /*free_devq*/TRUE);
464 mtx_unlock(&ch->mtx);
466 if (ch->pm_level > 3)
467 callout_drain(&ch->pm_timer);
468 bus_teardown_intr(dev, ch->r_irq, ch->ih);
469 bus_release_resource(dev, SYS_RES_IRQ, ATA_IRQ_RID, ch->r_irq);
471 fsl_sata_deinit(dev);
472 fsl_sata_slotsfree(dev);
473 fsl_sata_dmafini(dev);
475 bus_release_resource(dev, SYS_RES_MEMORY, ch->unit, ch->r_mem);
476 mtx_destroy(&ch->mtx);
481 fsl_sata_wait_register(struct fsl_sata_channel *ch, bus_size_t off,
482 unsigned int mask, unsigned int val, int t)
487 while (((rval = ATA_INL(ch->r_mem, off)) & mask) != val) {
498 fsl_sata_init(device_t dev)
500 struct fsl_sata_channel *ch = device_get_softc(dev);
504 /* Disable port interrupts */
505 r = ATA_INL(ch->r_mem, FSL_SATA_P_HCTRL);
506 r &= ~FSL_SATA_P_HCTRL_HC_ON;
507 r |= FSL_SATA_P_HCTRL_HC_FORCE_OFF;
508 ATA_OUTL(ch->r_mem, FSL_SATA_P_HCTRL, r & ~FSL_SATA_P_HCTRL_INT_MASK);
509 fsl_sata_wait_register(ch, FSL_SATA_P_HSTS,
510 FSL_SATA_P_HSTS_HS_ON, 0, 1000);
511 /* Setup work areas */
512 work = ch->dma.work_bus + FSL_SATA_CL_OFFSET;
513 ATA_OUTL(ch->r_mem, FSL_SATA_P_CHBA, work);
514 r &= ~FSL_SATA_P_HCTRL_ENT;
515 r &= ~FSL_SATA_P_HCTRL_PM;
516 ATA_OUTL(ch->r_mem, FSL_SATA_P_HCTRL, r);
517 r = ATA_INL(ch->r_mem, FSL_SATA_P_PCC);
518 ATA_OUTL(ch->r_mem, FSL_SATA_P_PCC, r & ~FSL_SATA_PCC_LPB_EN);
519 ATA_OUTL(ch->r_mem, FSL_SATA_P_ICC, (1 << FSL_SATA_P_ICC_ITC_S));
525 fsl_sata_deinit(device_t dev)
527 struct fsl_sata_channel *ch = device_get_softc(dev);
530 /* Disable port interrupts. */
531 r = ATA_INL(ch->r_mem, FSL_SATA_P_HCTRL);
532 ATA_OUTL(ch->r_mem, FSL_SATA_P_HCTRL, r & ~FSL_SATA_P_HCTRL_INT_MASK);
533 /* Reset command register. */
535 /* Allow everything, including partial and slumber modes. */
536 ATA_OUTL(ch->r_mem, FSL_SATA_P_SCTL, 0);
539 ATA_OUTL(ch->r_mem, FSL_SATA_P_SCTL, ATA_SC_DET_DISABLE);
540 r = ATA_INL(ch->r_mem, FSL_SATA_P_HCTRL);
541 /* Turn off the controller. */
542 ATA_OUTL(ch->r_mem, FSL_SATA_P_HCTRL, r & ~FSL_SATA_P_HCTRL_HC_ON);
547 fsl_sata_suspend(device_t dev)
549 struct fsl_sata_channel *ch = device_get_softc(dev);
552 xpt_freeze_simq(ch->sim, 1);
554 msleep(ch, &ch->mtx, PRIBIO, "fsl_satasusp", hz/100);
555 fsl_sata_deinit(dev);
556 mtx_unlock(&ch->mtx);
561 fsl_sata_resume(device_t dev)
563 struct fsl_sata_channel *ch = device_get_softc(dev);
568 xpt_release_simq(ch->sim, TRUE);
569 mtx_unlock(&ch->mtx);
573 devclass_t fsl_satach_devclass;
574 static device_method_t fsl_satach_methods[] = {
575 DEVMETHOD(device_probe, fsl_sata_probe),
576 DEVMETHOD(device_attach, fsl_sata_attach),
577 DEVMETHOD(device_detach, fsl_sata_detach),
578 DEVMETHOD(device_suspend, fsl_sata_suspend),
579 DEVMETHOD(device_resume, fsl_sata_resume),
582 static driver_t fsl_satach_driver = {
585 sizeof(struct fsl_sata_channel)
587 DRIVER_MODULE(fsl_satach, simplebus, fsl_satach_driver, fsl_satach_devclass, NULL, NULL);
589 struct fsl_sata_dc_cb_args {
595 fsl_sata_dmainit(device_t dev)
597 struct fsl_sata_channel *ch = device_get_softc(dev);
598 struct fsl_sata_dc_cb_args dcba;
601 if (bus_dma_tag_create(bus_get_dma_tag(dev), 1024, 0,
602 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
603 NULL, NULL, FSL_SATA_WORK_SIZE, 1, FSL_SATA_WORK_SIZE,
604 0, NULL, NULL, &ch->dma.work_tag))
606 if (bus_dmamem_alloc(ch->dma.work_tag, (void **)&ch->dma.work,
607 BUS_DMA_ZERO, &ch->dma.work_map))
609 if (bus_dmamap_load(ch->dma.work_tag, ch->dma.work_map, ch->dma.work,
610 FSL_SATA_WORK_SIZE, fsl_sata_dmasetupc_cb, &dcba, 0) || dcba.error) {
611 bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
614 ch->dma.work_bus = dcba.maddr;
616 if (bus_dma_tag_create(bus_get_dma_tag(dev), 4, 0,
617 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
618 NULL, NULL, FSL_SATA_MAX_XFER,
619 FSL_SATA_SG_ENTRIES - 1, FSL_SATA_PRD_MAX,
620 0, busdma_lock_mutex, &ch->mtx, &ch->dma.data_tag)) {
624 device_printf(dev, "work area: %p\n", ch->dma.work);
628 device_printf(dev, "WARNING - DMA initialization failed\n");
629 fsl_sata_dmafini(dev);
633 fsl_sata_dmasetupc_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
635 struct fsl_sata_dc_cb_args *dcba = (struct fsl_sata_dc_cb_args *)xsc;
637 if (!(dcba->error = error))
638 dcba->maddr = segs[0].ds_addr;
642 fsl_sata_dmafini(device_t dev)
644 struct fsl_sata_channel *ch = device_get_softc(dev);
646 if (ch->dma.data_tag) {
647 bus_dma_tag_destroy(ch->dma.data_tag);
648 ch->dma.data_tag = NULL;
650 if (ch->dma.work_bus) {
651 bus_dmamap_unload(ch->dma.work_tag, ch->dma.work_map);
652 bus_dmamem_free(ch->dma.work_tag, ch->dma.work, ch->dma.work_map);
653 ch->dma.work_bus = 0;
656 if (ch->dma.work_tag) {
657 bus_dma_tag_destroy(ch->dma.work_tag);
658 ch->dma.work_tag = NULL;
663 fsl_sata_slotsalloc(device_t dev)
665 struct fsl_sata_channel *ch = device_get_softc(dev);
668 /* Alloc and setup command/dma slots */
669 bzero(ch->slot, sizeof(ch->slot));
670 for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
671 struct fsl_sata_slot *slot = &ch->slot[i];
675 slot->state = FSL_SATA_SLOT_EMPTY;
677 callout_init_mtx(&slot->timeout, &ch->mtx, 0);
679 if (bus_dmamap_create(ch->dma.data_tag, 0, &slot->dma.data_map))
680 device_printf(ch->dev, "FAILURE - create data_map\n");
685 fsl_sata_slotsfree(device_t dev)
687 struct fsl_sata_channel *ch = device_get_softc(dev);
690 /* Free all dma slots */
691 for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
692 struct fsl_sata_slot *slot = &ch->slot[i];
694 callout_drain(&slot->timeout);
695 if (slot->dma.data_map) {
696 bus_dmamap_destroy(ch->dma.data_tag, slot->dma.data_map);
697 slot->dma.data_map = NULL;
703 fsl_sata_phy_check_events(struct fsl_sata_channel *ch, u_int32_t serr)
706 if (((ch->pm_level == 0) && (serr & ATA_SE_PHY_CHANGED)) ||
707 ((ch->pm_level != 0) && (serr & ATA_SE_EXCHANGED))) {
708 u_int32_t status = ATA_INL(ch->r_mem, FSL_SATA_P_SSTS);
712 if ((status & ATA_SS_DET_MASK) != ATA_SS_DET_NO_DEVICE)
713 device_printf(ch->dev, "CONNECT requested\n");
715 device_printf(ch->dev, "DISCONNECT requested\n");
717 /* Issue soft reset */
718 xpt_async(AC_BUS_RESET, ch->path, NULL);
719 if ((ccb = xpt_alloc_ccb_nowait()) == NULL)
721 if (xpt_create_path(&ccb->ccb_h.path, NULL,
722 cam_sim_path(ch->sim),
723 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
734 fsl_sata_notify_events(struct fsl_sata_channel *ch, u_int32_t status)
736 struct cam_path *dpath;
739 ATA_OUTL(ch->r_mem, FSL_SATA_P_SNTF, status);
741 device_printf(ch->dev, "SNTF 0x%04x\n", status);
742 for (i = 0; i < 16; i++) {
743 if ((status & (1 << i)) == 0)
745 if (xpt_create_path(&dpath, NULL,
746 xpt_path_path_id(ch->path), i, 0) == CAM_REQ_CMP) {
747 xpt_async(AC_SCSI_AEN, dpath, NULL);
748 xpt_free_path(dpath);
754 fsl_sata_done(struct fsl_sata_channel *ch, union ccb *ccb)
757 mtx_assert(&ch->mtx, MA_OWNED);
758 if ((ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0 ||
764 STAILQ_INSERT_TAIL(&ch->doneq, &ccb->ccb_h, sim_links.stqe);
768 fsl_sata_intr(void *arg)
770 struct fsl_sata_channel *ch = (struct fsl_sata_channel *)arg;
771 struct ccb_hdr *ccb_h;
773 STAILQ_HEAD(, ccb_hdr) tmp_doneq = STAILQ_HEAD_INITIALIZER(tmp_doneq);
775 /* Read interrupt statuses. */
776 istatus = ATA_INL(ch->r_mem, FSL_SATA_P_HSTS) & 0x7ffff;
777 if ((istatus & 0x3f) == 0)
782 fsl_sata_intr_main(ch, istatus);
785 * Prevent the possibility of issues caused by processing the queue
786 * while unlocked below by moving the contents to a local queue.
788 STAILQ_CONCAT(&tmp_doneq, &ch->doneq);
789 mtx_unlock(&ch->mtx);
790 while ((ccb_h = STAILQ_FIRST(&tmp_doneq)) != NULL) {
791 STAILQ_REMOVE_HEAD(&tmp_doneq, sim_links.stqe);
792 xpt_done_direct((union ccb *)ccb_h);
794 /* Clear interrupt statuses. */
795 ATA_OUTL(ch->r_mem, FSL_SATA_P_HSTS, istatus & 0x3f);
800 fsl_sata_pm(void *arg)
802 struct fsl_sata_channel *ch = (struct fsl_sata_channel *)arg;
805 if (ch->numrslots != 0)
807 work = ATA_INL(ch->r_mem, FSL_SATA_P_PCC) & ~FSL_SATA_PCC_LPB_EN;
808 if (ch->pm_level == 4)
809 work |= FSL_SATA_P_PCC_PARTIAL;
811 work |= FSL_SATA_P_PCC_SLUMBER;
812 ATA_OUTL(ch->r_mem, FSL_SATA_P_PCC, work);
815 /* XXX: interrupt todo */
817 fsl_sata_intr_main(struct fsl_sata_channel *ch, uint32_t istatus)
819 uint32_t cer, der, serr = 0, sntf = 0, ok, err;
820 enum fsl_sata_err_type et;
823 /* Complete all successful commands. */
824 ok = ATA_INL(ch->r_mem, FSL_SATA_P_CCR);
826 for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
827 if (((ok >> i) & 1) && ch->slot[i].ccb != NULL)
828 fsl_sata_end_transaction(&ch->slot[i], FSL_SATA_ERR_NONE);
830 /* Read command statuses. */
831 if (istatus & FSL_SATA_P_HSTS_SNTFU)
832 sntf = ATA_INL(ch->r_mem, FSL_SATA_P_SNTF);
833 /* XXX: Process PHY events */
834 serr = ATA_INL(ch->r_mem, FSL_SATA_P_SERR);
835 ATA_OUTL(ch->r_mem, FSL_SATA_P_SERR, serr);
836 if (istatus & (FSL_SATA_P_HSTS_PR)) {
838 fsl_sata_phy_check_events(ch, serr);
841 /* Process command errors */
842 err = (istatus & (FSL_SATA_P_HSTS_FE | FSL_SATA_P_HSTS_DE));
843 cer = ATA_INL(ch->r_mem, FSL_SATA_P_CER);
844 ATA_OUTL(ch->r_mem, FSL_SATA_P_CER, cer);
845 der = ATA_INL(ch->r_mem, FSL_SATA_P_DER);
846 ATA_OUTL(ch->r_mem, FSL_SATA_P_DER, der);
847 /* On error, complete the rest of commands with error statuses. */
850 union ccb *fccb = ch->frozen;
852 fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
853 if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
854 xpt_freeze_devq(fccb->ccb_h.path, 1);
855 fccb->ccb_h.status |= CAM_DEV_QFRZN;
857 fsl_sata_done(ch, fccb);
859 for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
860 if (ch->slot[i].ccb == NULL)
862 if ((cer & (1 << i)) != 0)
863 et = FSL_SATA_ERR_TFE;
864 else if ((der & (1 << ch->slot[i].ccb->ccb_h.target_id)) != 0)
865 et = FSL_SATA_ERR_SATA;
867 et = FSL_SATA_ERR_INVALID;
868 fsl_sata_end_transaction(&ch->slot[i], et);
871 /* Process NOTIFY events */
873 fsl_sata_notify_events(ch, sntf);
876 /* Must be called with channel locked. */
878 fsl_sata_check_collision(struct fsl_sata_channel *ch, union ccb *ccb)
880 int t = ccb->ccb_h.target_id;
882 if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
883 (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
884 /* Tagged command while we have no supported tag free. */
885 if (((~ch->oslots) & (0xffffffff >> (32 -
886 ch->curr[t].tags))) == 0)
888 /* Tagged command while untagged are active. */
889 if (ch->numrslotspd[t] != 0 && ch->numtslotspd[t] == 0)
892 /* Untagged command while tagged are active. */
893 if (ch->numrslotspd[t] != 0 && ch->numtslotspd[t] != 0)
896 if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
897 (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT))) {
898 /* Atomic command while anything active. */
899 if (ch->numrslots != 0)
902 /* We have some atomic command running. */
908 /* Must be called with channel locked. */
910 fsl_sata_begin_transaction(struct fsl_sata_channel *ch, union ccb *ccb)
912 struct fsl_sata_slot *slot;
915 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
916 ("fsl_sata_begin_transaction func_code=0x%x\n", ccb->ccb_h.func_code));
917 /* Choose empty slot. */
918 tags = FSL_SATA_MAX_SLOTS;
919 if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
920 (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA))
921 tags = ch->curr[ccb->ccb_h.target_id].tags;
922 if (ch->lastslot + 1 < tags)
923 tag = ffs(~(ch->oslots >> (ch->lastslot + 1)));
926 if (tag == 0 || tag + ch->lastslot >= tags)
927 tag = ffs(~ch->oslots) - 1;
931 /* Occupy chosen slot. */
932 slot = &ch->slot[tag];
936 if (ch->numrslots == 0 && ch->pm_level > 3)
937 callout_stop(&ch->pm_timer);
938 /* Update channel stats. */
939 ch->oslots |= (1 << tag);
941 ch->numrslotspd[ccb->ccb_h.target_id]++;
942 if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
943 (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
945 ch->numtslotspd[ccb->ccb_h.target_id]++;
946 ch->taggedtarget = ccb->ccb_h.target_id;
948 if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
949 (ccb->ataio.cmd.flags & (CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT)))
950 ch->aslots |= (1 << tag);
951 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
952 slot->state = FSL_SATA_SLOT_LOADING;
953 bus_dmamap_load_ccb(ch->dma.data_tag, slot->dma.data_map, ccb,
954 fsl_sata_dmasetprd, slot, 0);
957 fsl_sata_execute_transaction(slot);
960 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
961 ("fsl_sata_begin_transaction exit\n"));
964 /* Locked by busdma engine. */
966 fsl_sata_dmasetprd(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
968 struct fsl_sata_slot *slot = arg;
969 struct fsl_sata_channel *ch = slot->ch;
970 struct fsl_sata_cmd_tab *ctp;
971 struct fsl_sata_dma_prd *prd;
972 int i, j, len, extlen;
975 device_printf(ch->dev, "DMA load error %d\n", error);
976 fsl_sata_end_transaction(slot, FSL_SATA_ERR_INVALID);
979 KASSERT(nsegs <= FSL_SATA_SG_ENTRIES - 1,
980 ("too many DMA segment entries\n"));
981 /* Get a piece of the workspace for this request */
982 ctp = FSL_SATA_CTP(ch, slot);
984 prd = &ctp->prd_tab[0];
985 for (i = 0, j = 0; i < nsegs; i++, j++) {
986 if (j == FSL_SATA_PRD_EXT_INDEX &&
987 FSL_SATA_PRD_MAX_DIRECT < nsegs) {
988 prd[j].dba = FSL_SATA_CTP_BUS(ch, slot) +
989 FSL_SATA_PRD_OFFSET(j+1);
992 len = segs[i].ds_len;
993 len = roundup2(len, sizeof(uint32_t));
994 prd[i].dba = htole32((uint32_t)segs[i].ds_addr);
995 prd[i].dwc_flg = htole32(FSL_SATA_PRD_SNOOP | len);
997 if (j > FSL_SATA_PRD_MAX_DIRECT)
1000 slot->dma.nsegs = j;
1001 if (j > FSL_SATA_PRD_MAX_DIRECT)
1002 prd[FSL_SATA_PRD_EXT_INDEX].dwc_flg =
1003 htole32(FSL_SATA_PRD_SNOOP | FSL_SATA_PRD_EXT | extlen);
1004 bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
1005 ((slot->ccb->ccb_h.flags & CAM_DIR_IN) ?
1006 BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
1007 fsl_sata_execute_transaction(slot);
1010 /* Must be called with channel locked. */
1012 fsl_sata_execute_transaction(struct fsl_sata_slot *slot)
1014 struct fsl_sata_channel *ch = slot->ch;
1015 struct fsl_sata_cmd_tab *ctp;
1016 struct fsl_sata_cmd_list *clp;
1017 union ccb *ccb = slot->ccb;
1018 int port = ccb->ccb_h.target_id & 0x0f;
1019 int fis_size, i, softreset;
1021 uint32_t cmd_flags = FSL_SATA_CMD_WRITE | FSL_SATA_CMD_SNOOP;
1024 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
1025 ("fsl_sata_execute_transaction func_code=0x%x\n", ccb->ccb_h.func_code));
1026 /* Get a piece of the workspace for this request */
1027 ctp = FSL_SATA_CTP(ch, slot);
1028 /* Setup the FIS for this request */
1029 if (!(fis_size = fsl_sata_setup_fis(ch, ctp, ccb, slot->slot))) {
1030 device_printf(ch->dev, "Setting up SATA FIS failed\n");
1031 fsl_sata_end_transaction(slot, FSL_SATA_ERR_INVALID);
1034 /* Setup the command list entry */
1035 clp = FSL_SATA_CLP(ch, slot);
1036 clp->fis_length = htole16(fis_size);
1037 clp->prd_length = htole16(slot->dma.nsegs);
1038 /* Special handling for Soft Reset command. */
1039 if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1040 (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL)) {
1041 if (ccb->ataio.cmd.control & ATA_A_RESET) {
1043 cmd_flags |= FSL_SATA_CMD_RESET;
1045 /* Prepare FIS receive area for check. */
1046 for (i = 0; i < 32; i++)
1047 ctp->sfis[i] = 0xff;
1051 if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)
1052 cmd_flags |= FSL_SATA_CMD_QUEUED;
1053 clp->cmd_flags = htole32(cmd_flags |
1054 (ccb->ccb_h.func_code == XPT_SCSI_IO ? FSL_SATA_CMD_ATAPI : 0) |
1056 clp->ttl = htole32(slot->ttl);
1057 clp->cda = htole32(FSL_SATA_CTP_BUS(ch, slot));
1058 bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
1059 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1060 /* Issue command to the controller. */
1061 slot->state = FSL_SATA_SLOT_RUNNING;
1062 ch->rslots |= (1 << slot->slot);
1063 ATA_OUTL(ch->r_mem, FSL_SATA_P_CQPMP, port);
1064 ATA_OUTL(ch->r_mem, FSL_SATA_P_CQR, (1 << slot->slot));
1065 /* Device reset commands don't interrupt. Poll them. */
1066 if (ccb->ccb_h.func_code == XPT_ATA_IO &&
1067 (ccb->ataio.cmd.command == ATA_DEVICE_RESET || softreset)) {
1068 int count, timeout = ccb->ccb_h.timeout * 100;
1069 enum fsl_sata_err_type et = FSL_SATA_ERR_NONE;
1071 for (count = 0; count < timeout; count++) {
1074 if (softreset == 2) {
1075 tmp = ATA_INL(ch->r_mem, FSL_SATA_P_SIG);
1076 if (tmp != 0 && tmp != 0xffffffff)
1080 if ((ATA_INL(ch->r_mem, FSL_SATA_P_CCR) & (1 << slot->slot)) != 0)
1084 if (timeout && (count >= timeout)) {
1085 device_printf(ch->dev, "Poll timeout on slot %d port %d (round %d)\n",
1086 slot->slot, port, softreset);
1087 device_printf(ch->dev, "hsts %08x cqr %08x ccr %08x ss %08x "
1088 "rs %08x cer %08x der %08x serr %08x car %08x sig %08x\n",
1089 ATA_INL(ch->r_mem, FSL_SATA_P_HSTS),
1090 ATA_INL(ch->r_mem, FSL_SATA_P_CQR),
1091 ATA_INL(ch->r_mem, FSL_SATA_P_CCR),
1092 ATA_INL(ch->r_mem, FSL_SATA_P_SSTS), ch->rslots,
1093 ATA_INL(ch->r_mem, FSL_SATA_P_CER),
1094 ATA_INL(ch->r_mem, FSL_SATA_P_DER),
1095 ATA_INL(ch->r_mem, FSL_SATA_P_SERR),
1096 ATA_INL(ch->r_mem, FSL_SATA_P_CAR),
1097 ATA_INL(ch->r_mem, FSL_SATA_P_SIG));
1098 et = FSL_SATA_ERR_TIMEOUT;
1101 fsl_sata_end_transaction(slot, et);
1104 /* Start command execution timeout */
1105 callout_reset_sbt(&slot->timeout, SBT_1MS * ccb->ccb_h.timeout / 2,
1106 0, (timeout_t*)fsl_sata_timeout, slot, 0);
1110 /* Must be called with channel locked. */
1112 fsl_sata_process_timeout(struct fsl_sata_channel *ch)
1116 mtx_assert(&ch->mtx, MA_OWNED);
1117 /* Handle the rest of commands. */
1118 for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
1119 /* Do we have a running request on slot? */
1120 if (ch->slot[i].state < FSL_SATA_SLOT_RUNNING)
1122 fsl_sata_end_transaction(&ch->slot[i], FSL_SATA_ERR_TIMEOUT);
1126 /* Must be called with channel locked. */
1128 fsl_sata_rearm_timeout(struct fsl_sata_channel *ch)
1132 mtx_assert(&ch->mtx, MA_OWNED);
1133 for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
1134 struct fsl_sata_slot *slot = &ch->slot[i];
1136 /* Do we have a running request on slot? */
1137 if (slot->state < FSL_SATA_SLOT_RUNNING)
1139 if ((ch->toslots & (1 << i)) == 0)
1141 callout_reset_sbt(&slot->timeout,
1142 SBT_1MS * slot->ccb->ccb_h.timeout / 2, 0,
1143 (timeout_t*)fsl_sata_timeout, slot, 0);
1147 /* Locked by callout mechanism. */
1149 fsl_sata_timeout(struct fsl_sata_slot *slot)
1151 struct fsl_sata_channel *ch = slot->ch;
1152 device_t dev = ch->dev;
1155 /* Check for stale timeout. */
1156 if (slot->state < FSL_SATA_SLOT_RUNNING)
1159 /* Check if slot was not being executed last time we checked. */
1160 if (slot->state < FSL_SATA_SLOT_EXECUTING) {
1161 /* Check if slot started executing. */
1162 sstatus = ATA_INL(ch->r_mem, FSL_SATA_P_CAR);
1163 if ((sstatus & (1 << slot->slot)) != 0)
1164 slot->state = FSL_SATA_SLOT_EXECUTING;
1166 callout_reset_sbt(&slot->timeout,
1167 SBT_1MS * slot->ccb->ccb_h.timeout / 2, 0,
1168 (timeout_t*)fsl_sata_timeout, slot, 0);
1172 device_printf(dev, "Timeout on slot %d port %d\n",
1173 slot->slot, slot->ccb->ccb_h.target_id & 0x0f);
1175 /* Handle frozen command. */
1177 union ccb *fccb = ch->frozen;
1179 fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
1180 if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
1181 xpt_freeze_devq(fccb->ccb_h.path, 1);
1182 fccb->ccb_h.status |= CAM_DEV_QFRZN;
1184 fsl_sata_done(ch, fccb);
1186 if (ch->toslots == 0)
1187 xpt_freeze_simq(ch->sim, 1);
1188 ch->toslots |= (1 << slot->slot);
1189 if ((ch->rslots & ~ch->toslots) == 0)
1190 fsl_sata_process_timeout(ch);
1192 device_printf(dev, " ... waiting for slots %08x\n",
1193 ch->rslots & ~ch->toslots);
1196 /* Must be called with channel locked. */
1198 fsl_sata_end_transaction(struct fsl_sata_slot *slot, enum fsl_sata_err_type et)
1200 struct fsl_sata_channel *ch = slot->ch;
1201 union ccb *ccb = slot->ccb;
1202 struct fsl_sata_cmd_list *clp;
1206 bus_dmamap_sync(ch->dma.work_tag, ch->dma.work_map,
1207 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1208 clp = FSL_SATA_CLP(ch, slot);
1209 /* Read result registers to the result struct */
1210 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1211 struct ata_res *res = &ccb->ataio.res;
1213 if ((et == FSL_SATA_ERR_TFE) ||
1214 (ccb->ataio.cmd.flags & CAM_ATAIO_NEEDRESULT)) {
1215 struct fsl_sata_cmd_tab *ctp = FSL_SATA_CTP(ch, slot);
1216 uint8_t *fis = ctp->sfis;
1218 res->status = fis[2];
1219 res->error = fis[3];
1220 res->lba_low = fis[4];
1221 res->lba_mid = fis[5];
1222 res->lba_high = fis[6];
1223 res->device = fis[7];
1224 res->lba_low_exp = fis[8];
1225 res->lba_mid_exp = fis[9];
1226 res->lba_high_exp = fis[10];
1227 res->sector_count = fis[12];
1228 res->sector_count_exp = fis[13];
1230 if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) &&
1231 (ccb->ataio.cmd.control & ATA_A_RESET) == 0) {
1232 sig = ATA_INL(ch->r_mem, FSL_SATA_P_SIG);
1233 res->lba_high = sig >> 24;
1234 res->lba_mid = sig >> 16;
1235 res->lba_low = sig >> 8;
1236 res->sector_count = sig;
1239 bzero(res, sizeof(*res));
1240 if ((ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) == 0 &&
1241 (ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1243 ccb->ataio.dxfer_len - le32toh(clp->ttl);
1246 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1248 ccb->csio.dxfer_len - le32toh(clp->ttl);
1251 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1252 bus_dmamap_sync(ch->dma.data_tag, slot->dma.data_map,
1253 (ccb->ccb_h.flags & CAM_DIR_IN) ?
1254 BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
1255 bus_dmamap_unload(ch->dma.data_tag, slot->dma.data_map);
1257 if (et != FSL_SATA_ERR_NONE)
1258 ch->eslots |= (1 << slot->slot);
1259 /* In case of error, freeze device for proper recovery. */
1260 if ((et != FSL_SATA_ERR_NONE) && (!ch->recoverycmd) &&
1261 !(ccb->ccb_h.status & CAM_DEV_QFRZN)) {
1262 xpt_freeze_devq(ccb->ccb_h.path, 1);
1263 ccb->ccb_h.status |= CAM_DEV_QFRZN;
1265 /* Set proper result status. */
1266 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1268 case FSL_SATA_ERR_NONE:
1269 ccb->ccb_h.status |= CAM_REQ_CMP;
1270 if (ccb->ccb_h.func_code == XPT_SCSI_IO)
1271 ccb->csio.scsi_status = SCSI_STATUS_OK;
1273 case FSL_SATA_ERR_INVALID:
1275 ccb->ccb_h.status |= CAM_REQ_INVALID;
1277 case FSL_SATA_ERR_INNOCENT:
1278 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1280 case FSL_SATA_ERR_TFE:
1281 case FSL_SATA_ERR_NCQ:
1282 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1283 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1284 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1286 ccb->ccb_h.status |= CAM_ATA_STATUS_ERROR;
1289 case FSL_SATA_ERR_SATA:
1291 if (!ch->recoverycmd) {
1292 xpt_freeze_simq(ch->sim, 1);
1293 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1294 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1296 ccb->ccb_h.status |= CAM_UNCOR_PARITY;
1298 case FSL_SATA_ERR_TIMEOUT:
1299 if (!ch->recoverycmd) {
1300 xpt_freeze_simq(ch->sim, 1);
1301 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1302 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1304 ccb->ccb_h.status |= CAM_CMD_TIMEOUT;
1308 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
1311 ch->oslots &= ~(1 << slot->slot);
1312 ch->rslots &= ~(1 << slot->slot);
1313 ch->aslots &= ~(1 << slot->slot);
1314 slot->state = FSL_SATA_SLOT_EMPTY;
1316 /* Update channel stats. */
1318 ch->numrslotspd[ccb->ccb_h.target_id]--;
1319 ATA_OUTL(ch->r_mem, FSL_SATA_P_CCR, 1 << slot->slot);
1320 if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1321 (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA)) {
1323 ch->numtslotspd[ccb->ccb_h.target_id]--;
1325 /* Cancel timeout state if request completed normally. */
1326 if (et != FSL_SATA_ERR_TIMEOUT) {
1327 lastto = (ch->toslots == (1 << slot->slot));
1328 ch->toslots &= ~(1 << slot->slot);
1330 xpt_release_simq(ch->sim, TRUE);
1332 /* If it was first request of reset sequence and there is no error,
1333 * proceed to second request. */
1334 if ((ccb->ccb_h.func_code == XPT_ATA_IO) &&
1335 (ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) &&
1336 (ccb->ataio.cmd.control & ATA_A_RESET) &&
1337 et == FSL_SATA_ERR_NONE) {
1338 ccb->ataio.cmd.control &= ~ATA_A_RESET;
1339 fsl_sata_begin_transaction(ch, ccb);
1342 /* If it was our READ LOG command - process it. */
1343 if (ccb->ccb_h.recovery_type == RECOVERY_READ_LOG) {
1344 fsl_sata_process_read_log(ch, ccb);
1345 /* If it was our REQUEST SENSE command - process it. */
1346 } else if (ccb->ccb_h.recovery_type == RECOVERY_REQUEST_SENSE) {
1347 fsl_sata_process_request_sense(ch, ccb);
1348 /* If it was NCQ or ATAPI command error, put result on hold. */
1349 } else if (et == FSL_SATA_ERR_NCQ ||
1350 ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_SCSI_STATUS_ERROR &&
1351 (ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0)) {
1352 ch->hold[slot->slot] = ccb;
1355 fsl_sata_done(ch, ccb);
1356 /* If we have no other active commands, ... */
1357 if (ch->rslots == 0) {
1358 /* if there was fatal error - reset port. */
1359 if (ch->toslots != 0 || ch->fatalerr) {
1362 /* if we have slots in error, we can reinit port. */
1363 if (ch->eslots != 0) {
1367 /* if there commands on hold, we can do READ LOG. */
1368 if (!ch->recoverycmd && ch->numhslots)
1369 fsl_sata_issue_recovery(ch);
1371 /* If all the rest of commands are in timeout - give them chance. */
1372 } else if ((ch->rslots & ~ch->toslots) == 0 &&
1373 et != FSL_SATA_ERR_TIMEOUT)
1374 fsl_sata_rearm_timeout(ch);
1375 /* Unfreeze frozen command. */
1376 if (ch->frozen && !fsl_sata_check_collision(ch, ch->frozen)) {
1377 union ccb *fccb = ch->frozen;
1379 fsl_sata_begin_transaction(ch, fccb);
1380 xpt_release_simq(ch->sim, TRUE);
1382 /* Start PM timer. */
1383 if (ch->numrslots == 0 && ch->pm_level > 3 &&
1384 (ch->curr[ch->pm_present ? 15 : 0].caps & CTS_SATA_CAPS_D_PMREQ)) {
1385 callout_schedule(&ch->pm_timer,
1386 (ch->pm_level == 4) ? hz / 1000 : hz / 8);
1391 fsl_sata_issue_recovery(struct fsl_sata_channel *ch)
1394 struct ccb_ataio *ataio;
1395 struct ccb_scsiio *csio;
1398 /* Find some held command. */
1399 for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
1403 ccb = xpt_alloc_ccb_nowait();
1405 device_printf(ch->dev, "Unable to allocate recovery command\n");
1407 /* We can't do anything -- complete held commands. */
1408 for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
1409 if (ch->hold[i] == NULL)
1411 ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
1412 ch->hold[i]->ccb_h.status |= CAM_RESRC_UNAVAIL;
1413 fsl_sata_done(ch, ch->hold[i]);
1420 ccb->ccb_h = ch->hold[i]->ccb_h; /* Reuse old header. */
1421 if (ccb->ccb_h.func_code == XPT_ATA_IO) {
1423 ccb->ccb_h.recovery_type = RECOVERY_READ_LOG;
1424 ccb->ccb_h.func_code = XPT_ATA_IO;
1425 ccb->ccb_h.flags = CAM_DIR_IN;
1426 ccb->ccb_h.timeout = 1000; /* 1s should be enough. */
1427 ataio = &ccb->ataio;
1428 ataio->data_ptr = malloc(512, M_FSL_SATA, M_NOWAIT);
1429 if (ataio->data_ptr == NULL) {
1431 device_printf(ch->dev,
1432 "Unable to allocate memory for READ LOG command\n");
1435 ataio->dxfer_len = 512;
1436 bzero(&ataio->cmd, sizeof(ataio->cmd));
1437 ataio->cmd.flags = CAM_ATAIO_48BIT;
1438 ataio->cmd.command = 0x2F; /* READ LOG EXT */
1439 ataio->cmd.sector_count = 1;
1440 ataio->cmd.sector_count_exp = 0;
1441 ataio->cmd.lba_low = 0x10;
1442 ataio->cmd.lba_mid = 0;
1443 ataio->cmd.lba_mid_exp = 0;
1446 ccb->ccb_h.recovery_type = RECOVERY_REQUEST_SENSE;
1447 ccb->ccb_h.recovery_slot = i;
1448 ccb->ccb_h.func_code = XPT_SCSI_IO;
1449 ccb->ccb_h.flags = CAM_DIR_IN;
1450 ccb->ccb_h.status = 0;
1451 ccb->ccb_h.timeout = 1000; /* 1s should be enough. */
1453 csio->data_ptr = (void *)&ch->hold[i]->csio.sense_data;
1454 csio->dxfer_len = ch->hold[i]->csio.sense_len;
1456 bzero(&csio->cdb_io, sizeof(csio->cdb_io));
1457 csio->cdb_io.cdb_bytes[0] = 0x03;
1458 csio->cdb_io.cdb_bytes[4] = csio->dxfer_len;
1460 /* Freeze SIM while doing recovery. */
1461 ch->recoverycmd = 1;
1462 xpt_freeze_simq(ch->sim, 1);
1463 fsl_sata_begin_transaction(ch, ccb);
1467 fsl_sata_process_read_log(struct fsl_sata_channel *ch, union ccb *ccb)
1470 struct ata_res *res;
1473 ch->recoverycmd = 0;
1475 data = ccb->ataio.data_ptr;
1476 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
1477 (data[0] & 0x80) == 0) {
1478 for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
1481 if (ch->hold[i]->ccb_h.func_code != XPT_ATA_IO)
1483 if ((data[0] & 0x1F) == i) {
1484 res = &ch->hold[i]->ataio.res;
1485 res->status = data[2];
1486 res->error = data[3];
1487 res->lba_low = data[4];
1488 res->lba_mid = data[5];
1489 res->lba_high = data[6];
1490 res->device = data[7];
1491 res->lba_low_exp = data[8];
1492 res->lba_mid_exp = data[9];
1493 res->lba_high_exp = data[10];
1494 res->sector_count = data[12];
1495 res->sector_count_exp = data[13];
1497 ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
1498 ch->hold[i]->ccb_h.status |= CAM_REQUEUE_REQ;
1500 fsl_sata_done(ch, ch->hold[i]);
1505 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1506 device_printf(ch->dev, "Error while READ LOG EXT\n");
1507 else if ((data[0] & 0x80) == 0) {
1508 device_printf(ch->dev, "Non-queued command error in READ LOG EXT\n");
1510 for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
1513 if (ch->hold[i]->ccb_h.func_code != XPT_ATA_IO)
1515 fsl_sata_done(ch, ch->hold[i]);
1520 free(ccb->ataio.data_ptr, M_FSL_SATA);
1522 xpt_release_simq(ch->sim, TRUE);
1526 fsl_sata_process_request_sense(struct fsl_sata_channel *ch, union ccb *ccb)
1530 ch->recoverycmd = 0;
1532 i = ccb->ccb_h.recovery_slot;
1533 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1534 ch->hold[i]->ccb_h.status |= CAM_AUTOSNS_VALID;
1536 ch->hold[i]->ccb_h.status &= ~CAM_STATUS_MASK;
1537 ch->hold[i]->ccb_h.status |= CAM_AUTOSENSE_FAIL;
1539 fsl_sata_done(ch, ch->hold[i]);
1543 xpt_release_simq(ch->sim, TRUE);
1547 fsl_sata_start(struct fsl_sata_channel *ch)
1551 /* Clear SATA error register */
1552 ATA_OUTL(ch->r_mem, FSL_SATA_P_SERR, 0xFFFFFFFF);
1553 /* Clear any interrupts pending on this channel */
1554 ATA_OUTL(ch->r_mem, FSL_SATA_P_HSTS, 0x3F);
1555 ATA_OUTL(ch->r_mem, FSL_SATA_P_CER, 0xFFFF);
1556 ATA_OUTL(ch->r_mem, FSL_SATA_P_DER, 0xFFFF);
1557 /* Start operations on this channel */
1558 cmd = ATA_INL(ch->r_mem, FSL_SATA_P_HCTRL);
1559 cmd |= FSL_SATA_P_HCTRL_HC_ON | FSL_SATA_P_HCTRL_SNOOP;
1560 cmd &= ~FSL_SATA_P_HCTRL_HC_FORCE_OFF;
1561 ATA_OUTL(ch->r_mem, FSL_SATA_P_HCTRL, cmd |
1562 (ch->pm_present ? FSL_SATA_P_HCTRL_PM : 0));
1563 fsl_sata_wait_register(ch, FSL_SATA_P_HSTS,
1564 FSL_SATA_P_HSTS_PR, FSL_SATA_P_HSTS_PR, 500);
1565 ATA_OUTL(ch->r_mem, FSL_SATA_P_HSTS,
1566 ATA_INL(ch->r_mem, FSL_SATA_P_HSTS) & FSL_SATA_P_HSTS_PR);
1570 fsl_sata_stop(struct fsl_sata_channel *ch)
1575 /* Kill all activity on this channel */
1576 cmd = ATA_INL(ch->r_mem, FSL_SATA_P_HCTRL);
1577 cmd &= ~FSL_SATA_P_HCTRL_HC_ON;
1579 for (i = 0; i < 2; i++) {
1580 ATA_OUTL(ch->r_mem, FSL_SATA_P_HCTRL, cmd);
1581 if (fsl_sata_wait_register(ch, FSL_SATA_P_HSTS,
1582 FSL_SATA_P_HSTS_HS_ON, 0, 500)) {
1584 device_printf(ch->dev,
1585 "stopping FSL SATA engine failed\n");
1586 cmd |= FSL_SATA_P_HCTRL_HC_FORCE_OFF;
1594 fsl_sata_reset(struct fsl_sata_channel *ch)
1599 xpt_freeze_simq(ch->sim, 1);
1601 device_printf(ch->dev, "FSL SATA reset...\n");
1603 /* Requeue freezed command. */
1605 union ccb *fccb = ch->frozen;
1607 fccb->ccb_h.status = CAM_REQUEUE_REQ | CAM_RELEASE_SIMQ;
1608 if (!(fccb->ccb_h.status & CAM_DEV_QFRZN)) {
1609 xpt_freeze_devq(fccb->ccb_h.path, 1);
1610 fccb->ccb_h.status |= CAM_DEV_QFRZN;
1612 fsl_sata_done(ch, fccb);
1614 /* Kill the engine and requeue all running commands. */
1616 DELAY(1000); /* sleep for 1ms */
1617 for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
1618 /* Do we have a running request on slot? */
1619 if (ch->slot[i].state < FSL_SATA_SLOT_RUNNING)
1621 /* XXX; Commands in loading state. */
1622 fsl_sata_end_transaction(&ch->slot[i], FSL_SATA_ERR_INNOCENT);
1624 for (i = 0; i < FSL_SATA_MAX_SLOTS; i++) {
1627 fsl_sata_done(ch, ch->hold[i]);
1631 if (ch->toslots != 0)
1632 xpt_release_simq(ch->sim, TRUE);
1636 /* Tell the XPT about the event */
1637 xpt_async(AC_BUS_RESET, ch->path, NULL);
1638 /* Disable port interrupts */
1639 ATA_OUTL(ch->r_mem, FSL_SATA_P_HCTRL,
1640 ATA_INL(ch->r_mem, FSL_SATA_P_HCTRL) & ~0x3f);
1641 /* Reset and reconnect PHY, */
1643 if (fsl_sata_wait_register(ch, FSL_SATA_P_HSTS, 0x08, 0x08, 500)) {
1645 device_printf(ch->dev,
1646 "FSL SATA reset: device not found\n");
1648 /* Enable wanted port interrupts */
1649 ATA_OUTL(ch->r_mem, FSL_SATA_P_HCTRL,
1650 ATA_INL(ch->r_mem, FSL_SATA_P_HCTRL) | FSL_SATA_P_HCTRL_PHYRDY);
1651 xpt_release_simq(ch->sim, TRUE);
1655 device_printf(ch->dev, "FSL SATA reset: device found\n");
1657 /* Enable wanted port interrupts */
1658 ctrl = ATA_INL(ch->r_mem, FSL_SATA_P_HCTRL) & ~0x3f;
1659 ATA_OUTL(ch->r_mem, FSL_SATA_P_HCTRL,
1660 ctrl | FSL_SATA_P_HCTRL_FATAL | FSL_SATA_P_HCTRL_PHYRDY |
1661 FSL_SATA_P_HCTRL_SIG | FSL_SATA_P_HCTRL_SNTFY |
1662 FSL_SATA_P_HCTRL_DE | FSL_SATA_P_HCTRL_CC);
1663 xpt_release_simq(ch->sim, TRUE);
1667 fsl_sata_setup_fis(struct fsl_sata_channel *ch, struct fsl_sata_cmd_tab *ctp, union ccb *ccb, int tag)
1669 uint8_t *fis = &ctp->cfis[0];
1672 fis[0] = 0x27; /* host to device */
1673 fis[1] = (ccb->ccb_h.target_id & 0x0f);
1674 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
1676 fis[2] = ATA_PACKET_CMD;
1677 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE &&
1678 ch->curr[ccb->ccb_h.target_id].mode >= ATA_DMA)
1681 fis[5] = ccb->csio.dxfer_len;
1682 fis[6] = ccb->csio.dxfer_len >> 8;
1685 fis[15] = ATA_A_4BIT;
1686 bcopy((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1687 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes,
1688 ctp->acmd, ccb->csio.cdb_len);
1689 bzero(ctp->acmd + ccb->csio.cdb_len, 32 - ccb->csio.cdb_len);
1690 } else if ((ccb->ataio.cmd.flags & CAM_ATAIO_CONTROL) == 0) {
1692 fis[2] = ccb->ataio.cmd.command;
1693 fis[3] = ccb->ataio.cmd.features;
1694 fis[4] = ccb->ataio.cmd.lba_low;
1695 fis[5] = ccb->ataio.cmd.lba_mid;
1696 fis[6] = ccb->ataio.cmd.lba_high;
1697 fis[7] = ccb->ataio.cmd.device;
1698 fis[8] = ccb->ataio.cmd.lba_low_exp;
1699 fis[9] = ccb->ataio.cmd.lba_mid_exp;
1700 fis[10] = ccb->ataio.cmd.lba_high_exp;
1701 fis[11] = ccb->ataio.cmd.features_exp;
1702 if (ccb->ataio.cmd.flags & CAM_ATAIO_FPDMA) {
1706 fis[12] = ccb->ataio.cmd.sector_count;
1707 fis[13] = ccb->ataio.cmd.sector_count_exp;
1709 fis[15] = ATA_A_4BIT;
1711 fis[15] = ccb->ataio.cmd.control;
1717 fsl_sata_check_ids(struct fsl_sata_channel *ch, union ccb *ccb)
1720 if (ccb->ccb_h.target_id > 15) {
1721 ccb->ccb_h.status = CAM_TID_INVALID;
1722 fsl_sata_done(ch, ccb);
1725 if (ccb->ccb_h.target_lun != 0) {
1726 ccb->ccb_h.status = CAM_LUN_INVALID;
1727 fsl_sata_done(ch, ccb);
1734 fsl_sataaction(struct cam_sim *sim, union ccb *ccb)
1736 struct fsl_sata_channel *ch;
1738 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
1739 ("fsl_sataaction func_code=0x%x\n", ccb->ccb_h.func_code));
1741 ch = (struct fsl_sata_channel *)cam_sim_softc(sim);
1742 switch (ccb->ccb_h.func_code) {
1743 /* Common cases first */
1744 case XPT_ATA_IO: /* Execute the requested I/O operation */
1746 if (fsl_sata_check_ids(ch, ccb))
1748 if (ch->devices == 0 ||
1749 (ch->pm_present == 0 &&
1750 ccb->ccb_h.target_id > 0 && ccb->ccb_h.target_id < 15)) {
1751 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1754 ccb->ccb_h.recovery_type = RECOVERY_NONE;
1755 /* Check for command collision. */
1756 if (fsl_sata_check_collision(ch, ccb)) {
1757 /* Freeze command. */
1759 /* We have only one frozen slot, so freeze simq also. */
1760 xpt_freeze_simq(ch->sim, 1);
1763 fsl_sata_begin_transaction(ch, ccb);
1765 case XPT_ABORT: /* Abort the specified CCB */
1767 ccb->ccb_h.status = CAM_REQ_INVALID;
1769 case XPT_SET_TRAN_SETTINGS:
1771 struct ccb_trans_settings *cts = &ccb->cts;
1772 struct fsl_sata_device *d;
1774 if (fsl_sata_check_ids(ch, ccb))
1776 if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1777 d = &ch->curr[ccb->ccb_h.target_id];
1779 d = &ch->user[ccb->ccb_h.target_id];
1780 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_REVISION)
1781 d->revision = cts->xport_specific.sata.revision;
1782 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_MODE)
1783 d->mode = cts->xport_specific.sata.mode;
1784 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_BYTECOUNT)
1785 d->bytecount = min(8192, cts->xport_specific.sata.bytecount);
1786 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_TAGS)
1787 d->tags = min(FSL_SATA_MAX_SLOTS, cts->xport_specific.sata.tags);
1788 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_PM)
1789 ch->pm_present = cts->xport_specific.sata.pm_present;
1790 if (cts->xport_specific.sata.valid & CTS_SATA_VALID_ATAPI)
1791 d->atapi = cts->xport_specific.sata.atapi;
1792 ccb->ccb_h.status = CAM_REQ_CMP;
1795 case XPT_GET_TRAN_SETTINGS:
1796 /* Get default/user set transfer settings for the target */
1798 struct ccb_trans_settings *cts = &ccb->cts;
1799 struct fsl_sata_device *d;
1802 if (fsl_sata_check_ids(ch, ccb))
1804 if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
1805 d = &ch->curr[ccb->ccb_h.target_id];
1807 d = &ch->user[ccb->ccb_h.target_id];
1808 cts->protocol = PROTO_UNSPECIFIED;
1809 cts->protocol_version = PROTO_VERSION_UNSPECIFIED;
1810 cts->transport = XPORT_SATA;
1811 cts->transport_version = XPORT_VERSION_UNSPECIFIED;
1812 cts->proto_specific.valid = 0;
1813 cts->xport_specific.sata.valid = 0;
1814 if (cts->type == CTS_TYPE_CURRENT_SETTINGS &&
1815 (ccb->ccb_h.target_id == 15 ||
1816 (ccb->ccb_h.target_id == 0 && !ch->pm_present))) {
1817 status = ATA_INL(ch->r_mem, FSL_SATA_P_SSTS) & ATA_SS_SPD_MASK;
1818 if (status & 0x0f0) {
1819 cts->xport_specific.sata.revision =
1820 (status & 0x0f0) >> 4;
1821 cts->xport_specific.sata.valid |=
1822 CTS_SATA_VALID_REVISION;
1824 cts->xport_specific.sata.caps = d->caps & CTS_SATA_CAPS_D;
1826 cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_PMREQ;
1828 cts->xport_specific.sata.caps |= CTS_SATA_CAPS_H_AN;
1829 cts->xport_specific.sata.caps &=
1830 ch->user[ccb->ccb_h.target_id].caps;
1831 cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
1833 cts->xport_specific.sata.revision = d->revision;
1834 cts->xport_specific.sata.valid |= CTS_SATA_VALID_REVISION;
1835 cts->xport_specific.sata.caps = d->caps;
1836 cts->xport_specific.sata.valid |= CTS_SATA_VALID_CAPS;
1838 cts->xport_specific.sata.mode = d->mode;
1839 cts->xport_specific.sata.valid |= CTS_SATA_VALID_MODE;
1840 cts->xport_specific.sata.bytecount = d->bytecount;
1841 cts->xport_specific.sata.valid |= CTS_SATA_VALID_BYTECOUNT;
1842 cts->xport_specific.sata.pm_present = ch->pm_present;
1843 cts->xport_specific.sata.valid |= CTS_SATA_VALID_PM;
1844 cts->xport_specific.sata.tags = d->tags;
1845 cts->xport_specific.sata.valid |= CTS_SATA_VALID_TAGS;
1846 cts->xport_specific.sata.atapi = d->atapi;
1847 cts->xport_specific.sata.valid |= CTS_SATA_VALID_ATAPI;
1848 ccb->ccb_h.status = CAM_REQ_CMP;
1851 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
1852 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
1854 ccb->ccb_h.status = CAM_REQ_CMP;
1856 case XPT_TERM_IO: /* Terminate the I/O process */
1858 ccb->ccb_h.status = CAM_REQ_INVALID;
1860 case XPT_PATH_INQ: /* Path routing inquiry */
1862 struct ccb_pathinq *cpi = &ccb->cpi;
1864 cpi->version_num = 1; /* XXX??? */
1865 cpi->hba_inquiry = PI_SDTR_ABLE;
1866 cpi->hba_inquiry |= PI_TAG_ABLE;
1869 * XXX: CAM tries to reset port 15 if it sees port multiplier
1870 * support. Disable it for now.
1872 cpi->hba_inquiry |= PI_SATAPM;
1874 cpi->target_sprt = 0;
1875 cpi->hba_misc = PIM_SEQSCAN | PIM_UNMAPPED;
1876 cpi->hba_eng_cnt = 0;
1878 * XXX: This should be 15, since hardware *does* support a port
1879 * multiplier. See above.
1881 cpi->max_target = 0;
1883 cpi->initiator_id = 0;
1884 cpi->bus_id = cam_sim_bus(sim);
1885 cpi->base_transfer_speed = 150000;
1886 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1887 strncpy(cpi->hba_vid, "FSL SATA", HBA_IDLEN);
1888 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1889 cpi->unit_number = cam_sim_unit(sim);
1890 cpi->transport = XPORT_SATA;
1891 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
1892 cpi->protocol = PROTO_ATA;
1893 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
1894 cpi->maxio = MAXPHYS;
1895 cpi->ccb_h.status = CAM_REQ_CMP;
1899 ccb->ccb_h.status = CAM_REQ_INVALID;
1902 fsl_sata_done(ch, ccb);
1906 fsl_satapoll(struct cam_sim *sim)
1908 struct fsl_sata_channel *ch = (struct fsl_sata_channel *)cam_sim_softc(sim);
1911 /* Read interrupt statuses and process if any. */
1912 istatus = ATA_INL(ch->r_mem, FSL_SATA_P_HSTS);
1914 fsl_sata_intr_main(ch, istatus);
1916 MODULE_VERSION(fsl_sata, 1);
1917 MODULE_DEPEND(fsl_sata, cam, 1, 1, 1);