AHCI - Fix minor bug. Also AHCI/SILI - use ATA_F_EXCLUSIVE for pass-thru
[dragonfly.git] / sys / dev / disk / sili / sili_cam.c
blob1b9d619024fa40c38f0c267b0c2096402a00d55b
1 /*
2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * 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
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 * Copyright (c) 2007 David Gwynne <dlg@openbsd.org>
37 * Permission to use, copy, modify, and distribute this software for any
38 * purpose with or without fee is hereby granted, provided that the above
39 * copyright notice and this permission notice appear in all copies.
41 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
42 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
43 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
44 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
45 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
46 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
47 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
49 * $OpenBSD: atascsi.c,v 1.64 2009/02/16 21:19:06 miod Exp $
50 * $DragonFly$
53 * Implement each SATA port as its own SCSI bus on CAM. This way we can
54 * implement future port multiplier features as individual devices on the
55 * bus.
57 * Much of the cdb<->xa conversion code was taken from OpenBSD, the rest
58 * was written natively for DragonFly.
61 #include "sili.h"
63 static void sili_xpt_action(struct cam_sim *sim, union ccb *ccb);
64 static void sili_xpt_poll(struct cam_sim *sim);
65 static void sili_xpt_scsi_disk_io(struct sili_port *ap,
66 struct ata_port *at, union ccb *ccb);
67 static void sili_xpt_scsi_atapi_io(struct sili_port *ap,
68 struct ata_port *at, union ccb *ccb);
69 static void sili_xpt_page_inquiry(struct sili_port *ap,
70 struct ata_port *at, union ccb *ccb);
72 static void sili_ata_complete_disk_rw(struct ata_xfer *xa);
73 static void sili_ata_complete_disk_synchronize_cache(struct ata_xfer *xa);
74 static void sili_atapi_complete_cmd(struct ata_xfer *xa);
75 static void sili_ata_dummy_sense(struct scsi_sense_data *sense_data);
76 static void sili_ata_atapi_sense(struct ata_fis_d2h *rfis,
77 struct scsi_sense_data *sense_data);
79 static int sili_cam_probe_disk(struct sili_port *ap, struct ata_port *at);
80 static int sili_cam_probe_atapi(struct sili_port *ap, struct ata_port *at);
81 static void sili_ata_dummy_done(struct ata_xfer *xa);
82 static void ata_fix_identify(struct ata_identify *id);
83 static int sili_set_xfer(struct sili_port *ap, struct ata_port *atx);
84 static void sili_cam_rescan(struct sili_port *ap);
85 static void sili_strip_string(const char **basep, int *lenp);
87 int
88 sili_cam_attach(struct sili_port *ap)
90 struct cam_devq *devq;
91 struct cam_sim *sim;
92 int error;
93 int unit;
96 * We want at least one ccb to be available for error processing
97 * so don't let CAM use more then ncmds - 1.
99 unit = device_get_unit(ap->ap_sc->sc_dev);
100 if (ap->ap_sc->sc_ncmds > 1)
101 devq = cam_simq_alloc(ap->ap_sc->sc_ncmds - 1);
102 else
103 devq = cam_simq_alloc(ap->ap_sc->sc_ncmds);
104 if (devq == NULL) {
105 return (ENOMEM);
107 sim = cam_sim_alloc(sili_xpt_action, sili_xpt_poll, "sili",
108 (void *)ap, unit, &sim_mplock, 1, 1, devq);
109 cam_simq_release(devq);
110 if (sim == NULL) {
111 return (ENOMEM);
113 ap->ap_sim = sim;
114 sili_os_unlock_port(ap);
115 error = xpt_bus_register(ap->ap_sim, ap->ap_num);
116 sili_os_lock_port(ap);
117 if (error != CAM_SUCCESS) {
118 sili_cam_detach(ap);
119 return (EINVAL);
121 ap->ap_flags |= AP_F_BUS_REGISTERED;
123 if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
124 error = sili_cam_probe(ap, NULL);
125 else
126 error = 0;
127 if (error) {
128 sili_cam_detach(ap);
129 return (EIO);
131 ap->ap_flags |= AP_F_CAM_ATTACHED;
133 return(0);
137 * The state of the port has changed.
139 * If at is NULL the physical port has changed state.
140 * If at is non-NULL a particular target behind a PM has changed state.
142 * If found is -1 the target state must be queued to a non-interrupt context.
143 * (only works with at == NULL).
145 * If found is 0 the target was removed.
146 * If found is 1 the target was inserted.
148 void
149 sili_cam_changed(struct sili_port *ap, struct ata_port *atx, int found)
151 struct cam_path *tmppath;
152 int status;
153 int target;
155 target = atx ? atx->at_target : CAM_TARGET_WILDCARD;
157 if (ap->ap_sim == NULL)
158 return;
159 if (found == CAM_TARGET_WILDCARD) {
160 status = xpt_create_path(&tmppath, NULL,
161 cam_sim_path(ap->ap_sim),
162 target, CAM_LUN_WILDCARD);
163 if (status != CAM_REQ_CMP)
164 return;
165 sili_cam_rescan(ap);
166 } else {
167 status = xpt_create_path(&tmppath, NULL,
168 cam_sim_path(ap->ap_sim),
169 target,
170 CAM_LUN_WILDCARD);
171 if (status != CAM_REQ_CMP)
172 return;
173 #if 0
175 * This confuses CAM
177 if (found)
178 xpt_async(AC_FOUND_DEVICE, tmppath, NULL);
179 else
180 xpt_async(AC_LOST_DEVICE, tmppath, NULL);
181 #endif
183 xpt_free_path(tmppath);
186 void
187 sili_cam_detach(struct sili_port *ap)
189 int error;
191 if ((ap->ap_flags & AP_F_CAM_ATTACHED) == 0)
192 return;
193 get_mplock();
194 if (ap->ap_sim) {
195 xpt_freeze_simq(ap->ap_sim, 1);
197 if (ap->ap_flags & AP_F_BUS_REGISTERED) {
198 error = xpt_bus_deregister(cam_sim_path(ap->ap_sim));
199 KKASSERT(error == CAM_REQ_CMP);
200 ap->ap_flags &= ~AP_F_BUS_REGISTERED;
202 if (ap->ap_sim) {
203 cam_sim_free(ap->ap_sim);
204 ap->ap_sim = NULL;
206 rel_mplock();
207 ap->ap_flags &= ~AP_F_CAM_ATTACHED;
211 * Once the SILI port has been attached we need to probe for a device or
212 * devices on the port and setup various options.
214 * If at is NULL we are probing the direct-attached device on the port,
215 * which may or may not be a port multiplier.
218 sili_cam_probe(struct sili_port *ap, struct ata_port *atx)
220 struct ata_port *at;
221 struct ata_xfer *xa;
222 u_int64_t capacity;
223 u_int64_t capacity_bytes;
224 int model_len;
225 int firmware_len;
226 int serial_len;
227 int error;
228 int devncqdepth;
229 int i;
230 const char *model_id;
231 const char *firmware_id;
232 const char *serial_id;
233 const char *wcstr;
234 const char *rastr;
235 const char *scstr;
236 const char *type;
238 error = EIO;
241 * Delayed CAM attachment for initial probe, sim may be NULL
243 if (ap->ap_sim == NULL)
244 return(0);
247 * A NULL atx indicates a probe of the directly connected device.
248 * A non-NULL atx indicates a device connected via a port multiplier.
249 * We need to preserve atx for calls to sili_ata_get_xfer().
251 * at is always non-NULL. For directly connected devices we supply
252 * an (at) pointing to target 0.
254 if (atx == NULL) {
255 at = ap->ap_ata; /* direct attached - device 0 */
256 if (ap->ap_type == ATA_PORT_T_PM) {
257 kprintf("%s: Found Port Multiplier\n",
258 ATANAME(ap, atx));
259 return (0);
261 at->at_type = ap->ap_type;
262 } else {
263 at = atx;
264 if (atx->at_type == ATA_PORT_T_PM) {
265 kprintf("%s: Bogus device, reducing port count to %d\n",
266 ATANAME(ap, atx), atx->at_target);
267 if (ap->ap_pmcount > atx->at_target)
268 ap->ap_pmcount = atx->at_target;
269 goto err;
272 if (ap->ap_type == ATA_PORT_T_NONE)
273 goto err;
274 if (at->at_type == ATA_PORT_T_NONE)
275 goto err;
278 * Issue identify, saving the result
280 xa = sili_ata_get_xfer(ap, atx);
281 xa->complete = sili_ata_dummy_done;
282 xa->data = &at->at_identify;
283 xa->datalen = sizeof(at->at_identify);
284 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
285 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
287 switch(at->at_type) {
288 case ATA_PORT_T_DISK:
289 xa->fis->command = ATA_C_IDENTIFY;
290 type = "DISK";
291 break;
292 case ATA_PORT_T_ATAPI:
293 xa->fis->command = ATA_C_ATAPI_IDENTIFY;
294 xa->flags |= ATA_F_AUTOSENSE;
295 type = "ATAPI";
296 break;
297 default:
298 xa->fis->command = ATA_C_ATAPI_IDENTIFY;
299 type = "UNKNOWN(ATAPI?)";
300 break;
302 xa->fis->features = 0;
303 xa->fis->device = 0;
304 xa->timeout = 1000;
306 if (sili_ata_cmd(xa) != ATA_S_COMPLETE) {
307 kprintf("%s: Detected %s device but unable to IDENTIFY\n",
308 ATANAME(ap, atx), type);
309 sili_ata_put_xfer(xa);
310 goto err;
312 sili_ata_put_xfer(xa);
314 ata_fix_identify(&at->at_identify);
317 * Read capacity using SATA probe info.
319 if (le16toh(at->at_identify.cmdset83) & 0x0400) {
320 /* LBA48 feature set supported */
321 capacity = 0;
322 for (i = 3; i >= 0; --i) {
323 capacity <<= 16;
324 capacity +=
325 le16toh(at->at_identify.addrsecxt[i]);
327 } else {
328 capacity = le16toh(at->at_identify.addrsec[1]);
329 capacity <<= 16;
330 capacity += le16toh(at->at_identify.addrsec[0]);
332 at->at_capacity = capacity;
333 if (atx == NULL)
334 ap->ap_probe = ATA_PROBE_GOOD;
336 capacity_bytes = capacity * 512;
339 * Negotiate NCQ, throw away any ata_xfer's beyond the negotiated
340 * number of slots and limit the number of CAM ccb's to one less
341 * so we always have a slot available for recovery.
343 * NCQ is not used if ap_ncqdepth is 1 or the host controller does
344 * not support it, and in that case the driver can handle extra
345 * ccb's.
347 * NCQ is currently used only with direct-attached disks. It is
348 * not used with port multipliers or direct-attached ATAPI devices.
350 * Remember at least one extra CCB needs to be reserved for the
351 * error ccb.
353 if ((ap->ap_sc->sc_flags & SILI_F_NCQ) &&
354 at->at_type == ATA_PORT_T_DISK &&
355 (le16toh(at->at_identify.satacap) & (1 << 8))) {
356 at->at_ncqdepth = (le16toh(at->at_identify.qdepth) & 0x1F) + 1;
357 devncqdepth = at->at_ncqdepth;
358 if (at->at_ncqdepth > ap->ap_sc->sc_ncmds)
359 at->at_ncqdepth = ap->ap_sc->sc_ncmds;
360 if (at->at_ncqdepth > 1) {
361 for (i = 0; i < ap->ap_sc->sc_ncmds; ++i) {
362 xa = sili_ata_get_xfer(ap, atx);
363 if (xa->tag < at->at_ncqdepth) {
364 xa->state = ATA_S_COMPLETE;
365 sili_ata_put_xfer(xa);
368 if (at->at_ncqdepth >= ap->ap_sc->sc_ncmds) {
369 cam_devq_resize(ap->ap_sim->devq,
370 at->at_ncqdepth - 1);
373 } else {
374 devncqdepth = 0;
378 * Make the model string a bit more presentable
380 for (model_len = 40; model_len; --model_len) {
381 if (at->at_identify.model[model_len-1] == ' ')
382 continue;
383 if (at->at_identify.model[model_len-1] == 0)
384 continue;
385 break;
388 model_len = sizeof(at->at_identify.model);
389 model_id = at->at_identify.model;
390 sili_strip_string(&model_id, &model_len);
392 firmware_len = sizeof(at->at_identify.firmware);
393 firmware_id = at->at_identify.firmware;
394 sili_strip_string(&firmware_id, &firmware_len);
396 serial_len = sizeof(at->at_identify.serial);
397 serial_id = at->at_identify.serial;
398 sili_strip_string(&serial_id, &serial_len);
401 * Generate informatiive strings.
403 * NOTE: We do not automatically set write caching, lookahead,
404 * or the security state for ATAPI devices.
406 if (at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) {
407 if (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE)
408 wcstr = "enabled";
409 else if (at->at_type == ATA_PORT_T_ATAPI)
410 wcstr = "disabled";
411 else
412 wcstr = "enabling";
413 } else {
414 wcstr = "notsupp";
417 if (at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) {
418 if (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD)
419 rastr = "enabled";
420 else if (at->at_type == ATA_PORT_T_ATAPI)
421 rastr = "disabled";
422 else
423 rastr = "enabling";
424 } else {
425 rastr = "notsupp";
428 if (at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) {
429 if (at->at_identify.securestatus & ATA_SECURE_FROZEN)
430 scstr = "frozen";
431 else if (at->at_type == ATA_PORT_T_ATAPI)
432 scstr = "unfrozen";
433 else if (SiliNoFeatures & (1 << ap->ap_num))
434 scstr = "<disabled>";
435 else
436 scstr = "freezing";
437 } else {
438 scstr = "notsupp";
441 kprintf("%s: Found %s \"%*.*s %*.*s\" serial=\"%*.*s\"\n"
442 "%s: tags=%d/%d satacap=%04x satafea=%04x NCQ=%s "
443 "capacity=%lld.%02dMB\n",
445 ATANAME(ap, atx),
446 type,
447 model_len, model_len, model_id,
448 firmware_len, firmware_len, firmware_id,
449 serial_len, serial_len, serial_id,
451 ATANAME(ap, atx),
452 devncqdepth, ap->ap_sc->sc_ncmds,
453 at->at_identify.satacap,
454 at->at_identify.satafsup,
455 (at->at_ncqdepth > 1 ? "YES" : "NO"),
456 (long long)capacity_bytes / (1024 * 1024),
457 (int)(capacity_bytes % (1024 * 1024)) * 100 / (1024 * 1024)
459 kprintf("%s: f85=%04x f86=%04x f87=%04x WC=%s RA=%s SEC=%s\n",
460 ATANAME(ap, atx),
461 at->at_identify.features85,
462 at->at_identify.features86,
463 at->at_identify.features87,
464 wcstr,
465 rastr,
466 scstr
470 * Additional type-specific probing
472 switch(at->at_type) {
473 case ATA_PORT_T_DISK:
474 error = sili_cam_probe_disk(ap, atx);
475 break;
476 case ATA_PORT_T_ATAPI:
477 error = sili_cam_probe_atapi(ap, atx);
478 break;
479 default:
480 error = EIO;
481 break;
483 err:
484 if (error) {
485 at->at_probe = ATA_PROBE_FAILED;
486 if (atx == NULL)
487 ap->ap_probe = at->at_probe;
488 } else {
489 at->at_probe = ATA_PROBE_GOOD;
490 if (atx == NULL)
491 ap->ap_probe = at->at_probe;
493 return (error);
497 * DISK-specific probe after initial ident
499 static int
500 sili_cam_probe_disk(struct sili_port *ap, struct ata_port *atx)
502 struct ata_port *at;
503 struct ata_xfer *xa;
505 at = atx ? atx : ap->ap_ata;
508 * Set dummy xfer mode
510 sili_set_xfer(ap, atx);
513 * Enable write cache if supported
515 * NOTE: "WD My Book" external disk devices have a very poor
516 * daughter board between the the ESATA and the HD. Sending
517 * any ATA_C_SET_FEATURES commands will break the hardware port
518 * with a fatal protocol error. However, this device also
519 * indicates that WRITECACHE is already on and READAHEAD is
520 * not supported so we avoid the issue.
522 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) &&
523 (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE) == 0) {
524 xa = sili_ata_get_xfer(ap, atx);
525 xa->complete = sili_ata_dummy_done;
526 xa->fis->command = ATA_C_SET_FEATURES;
527 /*xa->fis->features = ATA_SF_WRITECACHE_EN;*/
528 xa->fis->features = ATA_SF_LOOKAHEAD_EN;
529 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
530 xa->fis->device = 0;
531 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
532 xa->timeout = 1000;
533 xa->datalen = 0;
534 if (sili_ata_cmd(xa) == ATA_S_COMPLETE)
535 at->at_features |= ATA_PORT_F_WCACHE;
536 else
537 kprintf("%s: Unable to enable write-caching\n",
538 ATANAME(ap, atx));
539 sili_ata_put_xfer(xa);
543 * Enable readahead if supported
545 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) &&
546 (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD) == 0) {
547 xa = sili_ata_get_xfer(ap, atx);
548 xa->complete = sili_ata_dummy_done;
549 xa->fis->command = ATA_C_SET_FEATURES;
550 xa->fis->features = ATA_SF_LOOKAHEAD_EN;
551 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
552 xa->fis->device = 0;
553 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
554 xa->timeout = 1000;
555 xa->datalen = 0;
556 if (sili_ata_cmd(xa) == ATA_S_COMPLETE)
557 at->at_features |= ATA_PORT_F_RAHEAD;
558 else
559 kprintf("%s: Unable to enable read-ahead\n",
560 ATANAME(ap, atx));
561 sili_ata_put_xfer(xa);
565 * FREEZE LOCK the device so malicious users can't lock it on us.
566 * As there is no harm in issuing this to devices that don't
567 * support the security feature set we just send it, and don't bother
568 * checking if the device sends a command abort to tell us it doesn't
569 * support it
571 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) &&
572 (at->at_identify.securestatus & ATA_SECURE_FROZEN) == 0 &&
573 (SiliNoFeatures & (1 << ap->ap_num)) == 0) {
574 xa = sili_ata_get_xfer(ap, atx);
575 xa->complete = sili_ata_dummy_done;
576 xa->fis->command = ATA_C_SEC_FREEZE_LOCK;
577 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
578 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
579 xa->timeout = 1000;
580 xa->datalen = 0;
581 if (sili_ata_cmd(xa) == ATA_S_COMPLETE)
582 at->at_features |= ATA_PORT_F_FRZLCK;
583 else
584 kprintf("%s: Unable to set security freeze\n",
585 ATANAME(ap, atx));
586 sili_ata_put_xfer(xa);
589 return (0);
593 * ATAPI-specific probe after initial ident
595 static int
596 sili_cam_probe_atapi(struct sili_port *ap, struct ata_port *atx)
598 sili_set_xfer(ap, atx);
599 return(0);
603 * Setting the transfer mode is irrelevant for the SATA transport
604 * but some (atapi) devices seem to need it anyway. In addition
605 * if we are running through a SATA->PATA converter for some reason
606 * beyond my comprehension we might have to set the mode.
608 * We only support DMA modes for SATA attached devices, so don't bother
609 * with legacy modes.
611 static int
612 sili_set_xfer(struct sili_port *ap, struct ata_port *atx)
614 struct ata_port *at;
615 struct ata_xfer *xa;
616 u_int16_t mode;
617 u_int16_t mask;
619 at = atx ? atx : ap->ap_ata;
622 * Figure out the supported UDMA mode. Ignore other legacy modes.
624 mask = le16toh(at->at_identify.ultradma);
625 if ((mask & 0xFF) == 0 || mask == 0xFFFF)
626 return(0);
627 mask &= 0xFF;
628 mode = 0x4F;
629 while ((mask & 0x8000) == 0) {
630 mask <<= 1;
631 --mode;
635 * SATA atapi devices often still report a dma mode, even though
636 * it is irrelevant for SATA transport. It is also possible that
637 * we are running through a SATA->PATA converter and seeing the
638 * PATA dma mode.
640 * In this case the device may require a (dummy) SETXFER to be
641 * sent before it will work properly.
643 xa = sili_ata_get_xfer(ap, atx);
644 xa->complete = sili_ata_dummy_done;
645 xa->fis->command = ATA_C_SET_FEATURES;
646 xa->fis->features = ATA_SF_SETXFER;
647 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
648 xa->fis->sector_count = mode;
649 xa->flags = ATA_F_PIO | ATA_F_POLL;
650 xa->timeout = 1000;
651 xa->datalen = 0;
652 if (sili_ata_cmd(xa) != ATA_S_COMPLETE) {
653 kprintf("%s: Unable to set dummy xfer mode \n",
654 ATANAME(ap, atx));
655 } else if (bootverbose) {
656 kprintf("%s: Set dummy xfer mode to %02x\n",
657 ATANAME(ap, atx), mode);
659 sili_ata_put_xfer(xa);
660 return(0);
664 * Fix byte ordering so buffers can be accessed as
665 * strings.
667 static void
668 ata_fix_identify(struct ata_identify *id)
670 u_int16_t *swap;
671 int i;
673 swap = (u_int16_t *)id->serial;
674 for (i = 0; i < sizeof(id->serial) / sizeof(u_int16_t); i++)
675 swap[i] = bswap16(swap[i]);
677 swap = (u_int16_t *)id->firmware;
678 for (i = 0; i < sizeof(id->firmware) / sizeof(u_int16_t); i++)
679 swap[i] = bswap16(swap[i]);
681 swap = (u_int16_t *)id->model;
682 for (i = 0; i < sizeof(id->model) / sizeof(u_int16_t); i++)
683 swap[i] = bswap16(swap[i]);
687 * Dummy done callback for xa.
689 static void
690 sili_ata_dummy_done(struct ata_xfer *xa)
695 * Use an engineering request to initiate a target scan for devices
696 * behind a port multiplier.
698 * An asynchronous bus scan is used to avoid reentrancy issues.
700 static void
701 sili_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
703 struct sili_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
705 if (ccb->ccb_h.func_code == XPT_SCAN_BUS) {
706 ap->ap_flags &= ~AP_F_SCAN_RUNNING;
707 if (ap->ap_flags & AP_F_SCAN_REQUESTED) {
708 ap->ap_flags &= ~AP_F_SCAN_REQUESTED;
709 sili_cam_rescan(ap);
711 ap->ap_flags |= AP_F_SCAN_COMPLETED;
712 wakeup(&ap->ap_flags);
714 xpt_free_ccb(ccb);
717 static void
718 sili_cam_rescan(struct sili_port *ap)
720 struct cam_path *path;
721 union ccb *ccb;
722 int status;
723 int i;
725 if (ap->ap_flags & AP_F_SCAN_RUNNING) {
726 ap->ap_flags |= AP_F_SCAN_REQUESTED;
727 return;
729 ap->ap_flags |= AP_F_SCAN_RUNNING;
730 for (i = 0; i < SILI_MAX_PMPORTS; ++i) {
731 ap->ap_ata[i].at_features |= ATA_PORT_F_RESCAN;
734 status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim),
735 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
736 if (status != CAM_REQ_CMP)
737 return;
739 ccb = xpt_alloc_ccb();
740 xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */
741 ccb->ccb_h.func_code = XPT_ENG_EXEC;
742 ccb->ccb_h.cbfcnp = sili_cam_rescan_callback;
743 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
744 ccb->crcn.flags = CAM_FLAG_NONE;
745 xpt_action_async(ccb);
748 static void
749 sili_xpt_rescan(struct sili_port *ap)
751 struct cam_path *path;
752 union ccb *ccb;
753 int status;
755 status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim),
756 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
757 if (status != CAM_REQ_CMP)
758 return;
760 ccb = xpt_alloc_ccb();
761 xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */
762 ccb->ccb_h.func_code = XPT_SCAN_BUS;
763 ccb->ccb_h.cbfcnp = sili_cam_rescan_callback;
764 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
765 ccb->crcn.flags = CAM_FLAG_NONE;
766 xpt_action_async(ccb);
770 * Action function - dispatch command
772 static
773 void
774 sili_xpt_action(struct cam_sim *sim, union ccb *ccb)
776 struct sili_port *ap;
777 struct ata_port *at, *atx;
778 struct ccb_hdr *ccbh;
779 int unit;
781 /* XXX lock */
782 ap = cam_sim_softc(sim);
783 at = ap->ap_ata;
784 atx = NULL;
785 KKASSERT(ap != NULL);
786 ccbh = &ccb->ccb_h;
787 unit = cam_sim_unit(sim);
790 * Early failure checks. These checks do not apply to XPT_PATH_INQ,
791 * otherwise the bus rescan will not remove the dead devices when
792 * unplugging a PM.
794 * For non-wildcards we have one target (0) and one lun (0),
795 * unless we have a port multiplier.
797 * A wildcard target indicates only the general bus is being
798 * probed.
800 * Calculate at and atx. at is always non-NULL. atx is only
801 * non-NULL for direct-attached devices. It will be NULL for
802 * devices behind a port multiplier.
804 * XXX What do we do with a LUN wildcard?
806 if (ccbh->target_id != CAM_TARGET_WILDCARD &&
807 ccbh->func_code != XPT_PATH_INQ) {
808 if (ap->ap_type == ATA_PORT_T_NONE) {
809 ccbh->status = CAM_DEV_NOT_THERE;
810 xpt_done(ccb);
811 return;
813 if (ccbh->target_id < 0 || ccbh->target_id >= ap->ap_pmcount) {
814 ccbh->status = CAM_DEV_NOT_THERE;
815 xpt_done(ccb);
816 return;
818 at += ccbh->target_id;
819 if (ap->ap_type == ATA_PORT_T_PM)
820 atx = at;
822 if (ccbh->target_lun != CAM_LUN_WILDCARD && ccbh->target_lun) {
823 ccbh->status = CAM_DEV_NOT_THERE;
824 xpt_done(ccb);
825 return;
830 * Switch on the meta XPT command
832 switch(ccbh->func_code) {
833 case XPT_ENG_EXEC:
835 * This routine is called after a port multiplier has been
836 * probed.
838 ccbh->status = CAM_REQ_CMP;
839 sili_os_lock_port(ap);
840 sili_port_state_machine(ap, 0);
841 sili_os_unlock_port(ap);
842 xpt_done(ccb);
843 sili_xpt_rescan(ap);
844 break;
845 case XPT_PATH_INQ:
847 * This command always succeeds, otherwise the bus scan
848 * will not detach dead devices.
850 ccb->cpi.version_num = 1;
851 ccb->cpi.hba_inquiry = 0;
852 ccb->cpi.target_sprt = 0;
853 ccb->cpi.hba_misc = PIM_SEQSCAN;
854 ccb->cpi.hba_eng_cnt = 0;
855 bzero(ccb->cpi.vuhba_flags, sizeof(ccb->cpi.vuhba_flags));
856 ccb->cpi.max_target = SILI_MAX_PMPORTS - 1;
857 ccb->cpi.max_lun = 0;
858 ccb->cpi.async_flags = 0;
859 ccb->cpi.hpath_id = 0;
860 ccb->cpi.initiator_id = SILI_MAX_PMPORTS - 1;
861 ccb->cpi.unit_number = cam_sim_unit(sim);
862 ccb->cpi.bus_id = cam_sim_bus(sim);
863 ccb->cpi.base_transfer_speed = 150000;
864 ccb->cpi.transport = XPORT_SATA;
865 ccb->cpi.transport_version = 1;
866 ccb->cpi.protocol = PROTO_SCSI;
867 ccb->cpi.protocol_version = SCSI_REV_2;
869 ccbh->status = CAM_REQ_CMP;
870 if (ccbh->target_id == CAM_TARGET_WILDCARD) {
871 sili_os_lock_port(ap);
872 sili_port_state_machine(ap, 0);
873 sili_os_unlock_port(ap);
874 } else {
875 switch(sili_pread(ap, SILI_PREG_SSTS) &
876 SILI_PREG_SSTS_SPD) {
877 case SILI_PREG_SSTS_SPD_GEN1:
878 ccb->cpi.base_transfer_speed = 150000;
879 break;
880 case SILI_PREG_SSTS_SPD_GEN2:
881 ccb->cpi.base_transfer_speed = 300000;
882 break;
883 default:
884 /* unknown */
885 ccb->cpi.base_transfer_speed = 1000;
886 break;
888 #if 0
889 if (ap->ap_type == ATA_PORT_T_NONE)
890 ccbh->status = CAM_DEV_NOT_THERE;
891 #endif
893 xpt_done(ccb);
894 break;
895 case XPT_RESET_DEV:
896 sili_os_lock_port(ap);
897 if (ap->ap_type == ATA_PORT_T_NONE) {
898 ccbh->status = CAM_DEV_NOT_THERE;
899 } else {
900 sili_port_reset(ap, atx, 0);
901 ccbh->status = CAM_REQ_CMP;
903 sili_os_unlock_port(ap);
904 xpt_done(ccb);
905 break;
906 case XPT_RESET_BUS:
907 sili_os_lock_port(ap);
908 sili_port_reset(ap, NULL, 1);
909 sili_os_unlock_port(ap);
910 ccbh->status = CAM_REQ_CMP;
911 xpt_done(ccb);
912 break;
913 case XPT_SET_TRAN_SETTINGS:
914 ccbh->status = CAM_FUNC_NOTAVAIL;
915 xpt_done(ccb);
916 break;
917 case XPT_GET_TRAN_SETTINGS:
918 ccb->cts.protocol = PROTO_SCSI;
919 ccb->cts.protocol_version = SCSI_REV_2;
920 ccb->cts.transport = XPORT_SATA;
921 ccb->cts.transport_version = XPORT_VERSION_UNSPECIFIED;
922 ccb->cts.proto_specific.valid = 0;
923 ccb->cts.xport_specific.valid = 0;
924 ccbh->status = CAM_REQ_CMP;
925 xpt_done(ccb);
926 break;
927 case XPT_CALC_GEOMETRY:
928 cam_calc_geometry(&ccb->ccg, 1);
929 xpt_done(ccb);
930 break;
931 case XPT_SCSI_IO:
933 * Our parallel startup code might have only probed through
934 * to the IDENT, so do the last step if necessary.
936 if (at->at_probe == ATA_PROBE_NEED_IDENT)
937 sili_cam_probe(ap, atx);
938 if (at->at_probe != ATA_PROBE_GOOD) {
939 ccbh->status = CAM_DEV_NOT_THERE;
940 xpt_done(ccb);
941 break;
943 switch(at->at_type) {
944 case ATA_PORT_T_DISK:
945 sili_xpt_scsi_disk_io(ap, atx, ccb);
946 break;
947 case ATA_PORT_T_ATAPI:
948 sili_xpt_scsi_atapi_io(ap, atx, ccb);
949 break;
950 default:
951 ccbh->status = CAM_REQ_INVALID;
952 xpt_done(ccb);
953 break;
955 break;
956 default:
957 ccbh->status = CAM_REQ_INVALID;
958 xpt_done(ccb);
959 break;
964 * Poll function.
966 * Generally this function gets called heavily when interrupts might be
967 * non-operational, during a halt/reboot or panic.
969 static
970 void
971 sili_xpt_poll(struct cam_sim *sim)
973 struct sili_port *ap;
975 ap = cam_sim_softc(sim);
976 crit_enter();
977 sili_os_lock_port(ap);
978 sili_port_intr(ap, 1);
979 sili_os_unlock_port(ap);
980 crit_exit();
984 * Convert the SCSI command in ccb to an ata_xfer command in xa
985 * for ATA_PORT_T_DISK operations. Set the completion function
986 * to convert the response back, then dispatch to the OpenBSD SILI
987 * layer.
989 * SILI DISK commands only support a limited command set, and we
990 * fake additional commands to make it play nice with the CAM subsystem.
992 static
993 void
994 sili_xpt_scsi_disk_io(struct sili_port *ap, struct ata_port *atx,
995 union ccb *ccb)
997 struct ccb_hdr *ccbh;
998 struct ccb_scsiio *csio;
999 struct ata_xfer *xa;
1000 struct ata_port *at;
1001 struct ata_fis_h2d *fis;
1002 struct ata_pass_12 *atp12;
1003 struct ata_pass_16 *atp16;
1004 scsi_cdb_t cdb;
1005 union scsi_data *rdata;
1006 int rdata_len;
1007 u_int64_t capacity;
1008 u_int64_t lba;
1009 u_int32_t count;
1011 ccbh = &ccb->csio.ccb_h;
1012 csio = &ccb->csio;
1013 at = atx ? atx : &ap->ap_ata[0];
1016 * XXX not passing NULL at for direct attach!
1018 xa = sili_ata_get_xfer(ap, atx);
1019 rdata = (void *)csio->data_ptr;
1020 rdata_len = csio->dxfer_len;
1023 * Build the FIS or process the csio to completion.
1025 cdb = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
1026 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
1028 switch(cdb->generic.opcode) {
1029 case REQUEST_SENSE:
1031 * Auto-sense everything, so explicit sense requests
1032 * return no-sense.
1034 ccbh->status = CAM_SCSI_STATUS_ERROR;
1035 break;
1036 case INQUIRY:
1038 * Inquiry supported features
1040 * [opcode, byte2, page_code, length, control]
1042 if (cdb->inquiry.byte2 & SI_EVPD) {
1043 sili_xpt_page_inquiry(ap, at, ccb);
1044 } else {
1045 bzero(rdata, rdata_len);
1046 if (rdata_len < SHORT_INQUIRY_LENGTH) {
1047 ccbh->status = CAM_CCB_LEN_ERR;
1048 break;
1050 if (rdata_len > sizeof(rdata->inquiry_data))
1051 rdata_len = sizeof(rdata->inquiry_data);
1052 rdata->inquiry_data.device = T_DIRECT;
1053 rdata->inquiry_data.version = SCSI_REV_SPC2;
1054 rdata->inquiry_data.response_format = 2;
1055 rdata->inquiry_data.additional_length = 32;
1056 bcopy("SATA ", rdata->inquiry_data.vendor, 8);
1057 bcopy(at->at_identify.model,
1058 rdata->inquiry_data.product,
1059 sizeof(rdata->inquiry_data.product));
1060 bcopy(at->at_identify.firmware,
1061 rdata->inquiry_data.revision,
1062 sizeof(rdata->inquiry_data.revision));
1063 ccbh->status = CAM_REQ_CMP;
1065 break;
1066 case READ_CAPACITY_16:
1067 if (cdb->read_capacity_16.service_action != SRC16_SERVICE_ACTION) {
1068 ccbh->status = CAM_REQ_INVALID;
1069 break;
1071 if (rdata_len < sizeof(rdata->read_capacity_data_16)) {
1072 ccbh->status = CAM_CCB_LEN_ERR;
1073 break;
1075 /* fall through */
1076 case READ_CAPACITY:
1077 if (rdata_len < sizeof(rdata->read_capacity_data)) {
1078 ccbh->status = CAM_CCB_LEN_ERR;
1079 break;
1082 capacity = at->at_capacity;
1084 bzero(rdata, rdata_len);
1085 if (cdb->generic.opcode == READ_CAPACITY) {
1086 rdata_len = sizeof(rdata->read_capacity_data);
1087 if (capacity > 0xFFFFFFFFU)
1088 capacity = 0xFFFFFFFFU;
1089 bzero(&rdata->read_capacity_data, rdata_len);
1090 scsi_ulto4b((u_int32_t)capacity - 1,
1091 rdata->read_capacity_data.addr);
1092 scsi_ulto4b(512, rdata->read_capacity_data.length);
1093 } else {
1094 rdata_len = sizeof(rdata->read_capacity_data_16);
1095 bzero(&rdata->read_capacity_data_16, rdata_len);
1096 scsi_u64to8b(capacity - 1,
1097 rdata->read_capacity_data_16.addr);
1098 scsi_ulto4b(512, rdata->read_capacity_data_16.length);
1100 ccbh->status = CAM_REQ_CMP;
1101 break;
1102 case SYNCHRONIZE_CACHE:
1104 * Synchronize cache. Specification says this can take
1105 * greater then 30 seconds so give it at least 45.
1107 fis = xa->fis;
1108 fis->flags = ATA_H2D_FLAGS_CMD;
1109 fis->command = ATA_C_FLUSH_CACHE;
1110 fis->device = 0;
1111 if (xa->timeout < 45000)
1112 xa->timeout = 45000;
1113 xa->datalen = 0;
1114 xa->flags = ATA_F_READ;
1115 xa->complete = sili_ata_complete_disk_synchronize_cache;
1116 break;
1117 case TEST_UNIT_READY:
1118 case START_STOP_UNIT:
1119 case PREVENT_ALLOW:
1121 * Just silently return success
1123 ccbh->status = CAM_REQ_CMP;
1124 rdata_len = 0;
1125 break;
1126 case ATA_PASS_12:
1127 atp12 = &cdb->ata_pass_12;
1128 fis = xa->fis;
1130 * Figure out the flags to be used, depending on the
1131 * direction of the CAM request.
1133 switch (ccbh->flags & CAM_DIR_MASK) {
1134 case CAM_DIR_IN:
1135 xa->flags = ATA_F_READ;
1136 break;
1137 case CAM_DIR_OUT:
1138 xa->flags = ATA_F_WRITE;
1139 break;
1140 default:
1141 xa->flags = 0;
1143 xa->flags |= ATA_F_POLL | ATA_F_EXCLUSIVE;
1144 xa->data = csio->data_ptr;
1145 xa->datalen = csio->dxfer_len;
1146 xa->complete = sili_ata_complete_disk_rw;
1147 xa->timeout = ccbh->timeout;
1150 * Populate the fis from the information we received through CAM
1151 * ATA passthrough.
1153 fis->flags = ATA_H2D_FLAGS_CMD; /* maybe also atp12->flags ? */
1154 fis->features = atp12->features;
1155 fis->sector_count = atp12->sector_count;
1156 fis->lba_low = atp12->lba_low;
1157 fis->lba_mid = atp12->lba_mid;
1158 fis->lba_high = atp12->lba_high;
1159 fis->device = atp12->device; /* maybe always 0? */
1160 fis->command = atp12->command;
1161 fis->control = atp12->control;
1164 * Mark as in progress so it is sent to the device.
1166 ccbh->status = CAM_REQ_INPROG;
1167 break;
1168 case ATA_PASS_16:
1169 atp16 = &cdb->ata_pass_16;
1170 fis = xa->fis;
1172 * Figure out the flags to be used, depending on the direction of the
1173 * CAM request.
1175 switch (ccbh->flags & CAM_DIR_MASK) {
1176 case CAM_DIR_IN:
1177 xa->flags = ATA_F_READ;
1178 break;
1179 case CAM_DIR_OUT:
1180 xa->flags = ATA_F_WRITE;
1181 break;
1182 default:
1183 xa->flags = 0;
1185 xa->flags |= ATA_F_POLL | ATA_F_EXCLUSIVE;
1186 xa->data = csio->data_ptr;
1187 xa->datalen = csio->dxfer_len;
1188 xa->complete = sili_ata_complete_disk_rw;
1189 xa->timeout = ccbh->timeout;
1192 * Populate the fis from the information we received through CAM
1193 * ATA passthrough.
1195 fis->flags = ATA_H2D_FLAGS_CMD; /* maybe also atp16->flags ? */
1196 fis->features = atp16->features;
1197 fis->features_exp = atp16->features_ext;
1198 fis->sector_count = atp16->sector_count;
1199 fis->sector_count_exp = atp16->sector_count_ext;
1200 fis->lba_low = atp16->lba_low;
1201 fis->lba_low_exp = atp16->lba_low_ext;
1202 fis->lba_mid = atp16->lba_mid;
1203 fis->lba_mid_exp = atp16->lba_mid_ext;
1204 fis->lba_high = atp16->lba_high;
1205 fis->lba_mid_exp = atp16->lba_mid_ext;
1206 fis->device = atp16->device; /* maybe always 0? */
1207 fis->command = atp16->command;
1210 * Mark as in progress so it is sent to the device.
1212 ccbh->status = CAM_REQ_INPROG;
1213 break;
1214 default:
1215 switch(cdb->generic.opcode) {
1216 case READ_6:
1217 lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1218 count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1219 xa->flags = ATA_F_READ;
1220 break;
1221 case READ_10:
1222 lba = scsi_4btoul(cdb->rw_10.addr);
1223 count = scsi_2btoul(cdb->rw_10.length);
1224 xa->flags = ATA_F_READ;
1225 break;
1226 case READ_12:
1227 lba = scsi_4btoul(cdb->rw_12.addr);
1228 count = scsi_4btoul(cdb->rw_12.length);
1229 xa->flags = ATA_F_READ;
1230 break;
1231 case READ_16:
1232 lba = scsi_8btou64(cdb->rw_16.addr);
1233 count = scsi_4btoul(cdb->rw_16.length);
1234 xa->flags = ATA_F_READ;
1235 break;
1236 case WRITE_6:
1237 lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1238 count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1239 xa->flags = ATA_F_WRITE;
1240 break;
1241 case WRITE_10:
1242 lba = scsi_4btoul(cdb->rw_10.addr);
1243 count = scsi_2btoul(cdb->rw_10.length);
1244 xa->flags = ATA_F_WRITE;
1245 break;
1246 case WRITE_12:
1247 lba = scsi_4btoul(cdb->rw_12.addr);
1248 count = scsi_4btoul(cdb->rw_12.length);
1249 xa->flags = ATA_F_WRITE;
1250 break;
1251 case WRITE_16:
1252 lba = scsi_8btou64(cdb->rw_16.addr);
1253 count = scsi_4btoul(cdb->rw_16.length);
1254 xa->flags = ATA_F_WRITE;
1255 break;
1256 default:
1257 ccbh->status = CAM_REQ_INVALID;
1258 break;
1260 if (ccbh->status != CAM_REQ_INPROG)
1261 break;
1263 fis = xa->fis;
1264 fis->flags = ATA_H2D_FLAGS_CMD;
1265 fis->lba_low = (u_int8_t)lba;
1266 fis->lba_mid = (u_int8_t)(lba >> 8);
1267 fis->lba_high = (u_int8_t)(lba >> 16);
1268 fis->device = ATA_H2D_DEVICE_LBA;
1271 * NCQ only for direct-attached disks, do not currently
1272 * try to use NCQ with port multipliers.
1274 * XXX fixme SII chip can do NCQ w/ port multipliers.
1276 if (at->at_ncqdepth > 1 &&
1277 at->at_type == ATA_PORT_T_DISK &&
1278 (ap->ap_sc->sc_flags & SILI_F_NCQ) &&
1279 (ccbh->flags & CAM_POLLED) == 0) {
1281 * Use NCQ - always uses 48 bit addressing
1283 xa->flags |= ATA_F_NCQ;
1284 fis->command = (xa->flags & ATA_F_WRITE) ?
1285 ATA_C_WRITE_FPDMA : ATA_C_READ_FPDMA;
1286 fis->lba_low_exp = (u_int8_t)(lba >> 24);
1287 fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1288 fis->lba_high_exp = (u_int8_t)(lba >> 40);
1289 fis->sector_count = xa->tag << 3;
1290 fis->features = (u_int8_t)count;
1291 fis->features_exp = (u_int8_t)(count >> 8);
1292 } else if (count > 0x100 || lba > 0x0FFFFFFFU) {
1294 * Use LBA48
1296 fis->command = (xa->flags & ATA_F_WRITE) ?
1297 ATA_C_WRITEDMA_EXT : ATA_C_READDMA_EXT;
1298 fis->lba_low_exp = (u_int8_t)(lba >> 24);
1299 fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1300 fis->lba_high_exp = (u_int8_t)(lba >> 40);
1301 fis->sector_count = (u_int8_t)count;
1302 fis->sector_count_exp = (u_int8_t)(count >> 8);
1303 } else {
1305 * Use LBA
1307 * NOTE: 256 sectors is supported, stored as 0.
1309 fis->command = (xa->flags & ATA_F_WRITE) ?
1310 ATA_C_WRITEDMA : ATA_C_READDMA;
1311 fis->device |= (u_int8_t)(lba >> 24) & 0x0F;
1312 fis->sector_count = (u_int8_t)count;
1315 xa->data = csio->data_ptr;
1316 xa->datalen = csio->dxfer_len;
1317 xa->complete = sili_ata_complete_disk_rw;
1318 xa->timeout = ccbh->timeout; /* milliseconds */
1319 if (ccbh->flags & CAM_POLLED)
1320 xa->flags |= ATA_F_POLL;
1321 break;
1325 * If the request is still in progress the xa and FIS have
1326 * been set up (except for the PM target), and must be dispatched.
1327 * Otherwise the request was completed.
1329 if (ccbh->status == CAM_REQ_INPROG) {
1330 KKASSERT(xa->complete != NULL);
1331 xa->atascsi_private = ccb;
1332 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
1333 sili_os_lock_port(ap);
1334 xa->fis->flags |= at->at_target;
1335 sili_ata_cmd(xa);
1336 sili_os_unlock_port(ap);
1337 } else {
1338 sili_ata_put_xfer(xa);
1339 xpt_done(ccb);
1344 * Convert the SCSI command in ccb to an ata_xfer command in xa
1345 * for ATA_PORT_T_ATAPI operations. Set the completion function
1346 * to convert the response back, then dispatch to the OpenBSD SILI
1347 * layer.
1349 static
1350 void
1351 sili_xpt_scsi_atapi_io(struct sili_port *ap, struct ata_port *atx,
1352 union ccb *ccb)
1354 struct ccb_hdr *ccbh;
1355 struct ccb_scsiio *csio;
1356 struct ata_xfer *xa;
1357 struct ata_fis_h2d *fis;
1358 scsi_cdb_t cdbs;
1359 scsi_cdb_t cdbd;
1360 int flags;
1361 struct ata_port *at;
1363 ccbh = &ccb->csio.ccb_h;
1364 csio = &ccb->csio;
1365 at = atx ? atx : &ap->ap_ata[0];
1367 switch (ccbh->flags & CAM_DIR_MASK) {
1368 case CAM_DIR_IN:
1369 flags = ATA_F_PACKET | ATA_F_READ;
1370 break;
1371 case CAM_DIR_OUT:
1372 flags = ATA_F_PACKET | ATA_F_WRITE;
1373 break;
1374 case CAM_DIR_NONE:
1375 flags = ATA_F_PACKET;
1376 break;
1377 default:
1378 ccbh->status = CAM_REQ_INVALID;
1379 xpt_done(ccb);
1380 return;
1381 /* NOT REACHED */
1385 * Special handling to get the rfis back into host memory while
1386 * still allowing the chip to run commands in parallel to
1387 * ATAPI devices behind a PM.
1389 flags |= ATA_F_AUTOSENSE;
1392 * The command has to fit in the packet command buffer.
1394 if (csio->cdb_len < 6 || csio->cdb_len > 16) {
1395 ccbh->status = CAM_CCB_LEN_ERR;
1396 xpt_done(ccb);
1397 return;
1401 * Initialize the XA and FIS. It is unclear how much of
1402 * this has to mimic the equivalent ATA command.
1404 * XXX not passing NULL at for direct attach!
1406 xa = sili_ata_get_xfer(ap, atx);
1407 fis = xa->fis;
1409 fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
1410 fis->command = ATA_C_PACKET;
1411 fis->device = ATA_H2D_DEVICE_LBA;
1412 fis->sector_count = xa->tag << 3;
1413 if (flags & (ATA_F_READ | ATA_F_WRITE)) {
1414 if (flags & ATA_F_WRITE) {
1415 fis->features = ATA_H2D_FEATURES_DMA |
1416 ATA_H2D_FEATURES_DIR_WRITE;
1417 } else {
1418 fis->features = ATA_H2D_FEATURES_DMA |
1419 ATA_H2D_FEATURES_DIR_READ;
1421 } else {
1422 fis->lba_mid = 0;
1423 fis->lba_high = 0;
1425 fis->control = ATA_FIS_CONTROL_4BIT;
1427 xa->flags = flags;
1428 xa->data = csio->data_ptr;
1429 xa->datalen = csio->dxfer_len;
1430 xa->timeout = ccbh->timeout; /* milliseconds */
1432 if (ccbh->flags & CAM_POLLED)
1433 xa->flags |= ATA_F_POLL;
1436 * Copy the cdb to the packetcmd buffer in the FIS using a
1437 * convenient pointer in the xa.
1439 cdbs = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
1440 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
1441 bcopy(cdbs, xa->packetcmd, csio->cdb_len);
1443 #if 0
1444 kprintf("opcode %d cdb_len %d dxfer_len %d\n",
1445 cdbs->generic.opcode,
1446 csio->cdb_len, csio->dxfer_len);
1447 #endif
1450 * Some ATAPI commands do not actually follow the SCSI standard.
1452 cdbd = (void *)xa->packetcmd;
1454 switch(cdbd->generic.opcode) {
1455 case REQUEST_SENSE:
1457 * Force SENSE requests to the ATAPI sense length.
1459 * It is unclear if this is needed or not.
1461 if (cdbd->sense.length == SSD_FULL_SIZE) {
1462 kprintf("%s: Shortening sense request\n",
1463 PORTNAME(ap));
1464 cdbd->sense.length = offsetof(struct scsi_sense_data,
1465 extra_bytes[0]);
1467 break;
1468 case INQUIRY:
1470 * Some ATAPI devices can't handle long inquiry lengths,
1471 * don't ask me why. Truncate the inquiry length.
1473 if (cdbd->inquiry.page_code == 0 &&
1474 cdbd->inquiry.length > SHORT_INQUIRY_LENGTH) {
1475 cdbd->inquiry.length = SHORT_INQUIRY_LENGTH;
1477 break;
1478 case READ_6:
1479 case WRITE_6:
1481 * Convert *_6 to *_10 commands. Most ATAPI devices
1482 * cannot handle the SCSI READ_6 and WRITE_6 commands.
1484 cdbd->rw_10.opcode |= 0x20;
1485 cdbd->rw_10.byte2 = 0;
1486 cdbd->rw_10.addr[0] = cdbs->rw_6.addr[0] & 0x1F;
1487 cdbd->rw_10.addr[1] = cdbs->rw_6.addr[1];
1488 cdbd->rw_10.addr[2] = cdbs->rw_6.addr[2];
1489 cdbd->rw_10.addr[3] = 0;
1490 cdbd->rw_10.reserved = 0;
1491 cdbd->rw_10.length[0] = 0;
1492 cdbd->rw_10.length[1] = cdbs->rw_6.length;
1493 cdbd->rw_10.control = cdbs->rw_6.control;
1494 break;
1495 default:
1496 break;
1500 * And dispatch
1502 xa->complete = sili_atapi_complete_cmd;
1503 xa->atascsi_private = ccb;
1504 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
1505 sili_os_lock_port(ap);
1506 sili_ata_cmd(xa);
1507 sili_os_unlock_port(ap);
1511 * Simulate page inquiries for disk attachments.
1513 static
1514 void
1515 sili_xpt_page_inquiry(struct sili_port *ap, struct ata_port *at, union ccb *ccb)
1517 union {
1518 struct scsi_vpd_supported_page_list list;
1519 struct scsi_vpd_unit_serial_number serno;
1520 struct scsi_vpd_unit_devid devid;
1521 char buf[256];
1522 } *page;
1523 scsi_cdb_t cdb;
1524 int i;
1525 int j;
1526 int len;
1528 page = kmalloc(sizeof(*page), M_DEVBUF, M_WAITOK | M_ZERO);
1530 cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1531 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes);
1533 switch(cdb->inquiry.page_code) {
1534 case SVPD_SUPPORTED_PAGE_LIST:
1535 i = 0;
1536 page->list.device = T_DIRECT;
1537 page->list.page_code = SVPD_SUPPORTED_PAGE_LIST;
1538 page->list.list[i++] = SVPD_SUPPORTED_PAGE_LIST;
1539 page->list.list[i++] = SVPD_UNIT_SERIAL_NUMBER;
1540 page->list.list[i++] = SVPD_UNIT_DEVID;
1541 page->list.length = i;
1542 len = offsetof(struct scsi_vpd_supported_page_list, list[3]);
1543 break;
1544 case SVPD_UNIT_SERIAL_NUMBER:
1545 i = 0;
1546 j = sizeof(at->at_identify.serial);
1547 for (i = 0; i < j && at->at_identify.serial[i] == ' '; ++i)
1549 while (j > i && at->at_identify.serial[j-1] == ' ')
1550 --j;
1551 page->serno.device = T_DIRECT;
1552 page->serno.page_code = SVPD_UNIT_SERIAL_NUMBER;
1553 page->serno.length = j - i;
1554 bcopy(at->at_identify.serial + i,
1555 page->serno.serial_num, j - i);
1556 len = offsetof(struct scsi_vpd_unit_serial_number,
1557 serial_num[j-i]);
1558 break;
1559 case SVPD_UNIT_DEVID:
1560 /* fall through for now */
1561 default:
1562 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1563 len = 0;
1564 break;
1566 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
1567 if (len <= ccb->csio.dxfer_len) {
1568 ccb->ccb_h.status = CAM_REQ_CMP;
1569 bzero(ccb->csio.data_ptr, ccb->csio.dxfer_len);
1570 bcopy(page, ccb->csio.data_ptr, len);
1571 ccb->csio.resid = ccb->csio.dxfer_len - len;
1572 } else {
1573 ccb->ccb_h.status = CAM_CCB_LEN_ERR;
1576 kfree(page, M_DEVBUF);
1580 * Completion function for ATA_PORT_T_DISK cache synchronization.
1582 static
1583 void
1584 sili_ata_complete_disk_synchronize_cache(struct ata_xfer *xa)
1586 union ccb *ccb = xa->atascsi_private;
1587 struct ccb_hdr *ccbh = &ccb->ccb_h;
1588 struct sili_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1590 switch(xa->state) {
1591 case ATA_S_COMPLETE:
1592 ccbh->status = CAM_REQ_CMP;
1593 ccb->csio.scsi_status = SCSI_STATUS_OK;
1594 break;
1595 case ATA_S_ERROR:
1596 kprintf("%s: synchronize_cache: error\n",
1597 ATANAME(ap, xa->at));
1598 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1599 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1600 sili_ata_dummy_sense(&ccb->csio.sense_data);
1601 break;
1602 case ATA_S_TIMEOUT:
1603 kprintf("%s: synchronize_cache: timeout\n",
1604 ATANAME(ap, xa->at));
1605 ccbh->status = CAM_CMD_TIMEOUT;
1606 break;
1607 default:
1608 kprintf("%s: synchronize_cache: unknown state %d\n",
1609 ATANAME(ap, xa->at), xa->state);
1610 ccbh->status = CAM_REQ_CMP_ERR;
1611 break;
1613 sili_ata_put_xfer(xa);
1614 sili_os_unlock_port(ap);
1615 xpt_done(ccb);
1616 sili_os_lock_port(ap);
1620 * Completion function for ATA_PORT_T_DISK I/O
1622 static
1623 void
1624 sili_ata_complete_disk_rw(struct ata_xfer *xa)
1626 union ccb *ccb = xa->atascsi_private;
1627 struct ccb_hdr *ccbh = &ccb->ccb_h;
1628 struct sili_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1630 switch(xa->state) {
1631 case ATA_S_COMPLETE:
1632 ccbh->status = CAM_REQ_CMP;
1633 ccb->csio.scsi_status = SCSI_STATUS_OK;
1634 break;
1635 case ATA_S_ERROR:
1636 kprintf("%s: disk_rw: error\n", ATANAME(ap, xa->at));
1637 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1638 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1639 sili_ata_dummy_sense(&ccb->csio.sense_data);
1640 break;
1641 case ATA_S_TIMEOUT:
1642 kprintf("%s: disk_rw: timeout\n", ATANAME(ap, xa->at));
1643 ccbh->status = CAM_CMD_TIMEOUT;
1644 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1645 sili_ata_dummy_sense(&ccb->csio.sense_data);
1646 break;
1647 default:
1648 kprintf("%s: disk_rw: unknown state %d\n",
1649 ATANAME(ap, xa->at), xa->state);
1650 ccbh->status = CAM_REQ_CMP_ERR;
1651 break;
1653 ccb->csio.resid = xa->resid;
1654 sili_ata_put_xfer(xa);
1655 sili_os_unlock_port(ap);
1656 xpt_done(ccb);
1657 sili_os_lock_port(ap);
1661 * Completion function for ATA_PORT_T_ATAPI I/O
1663 * Sense data is returned in the rfis.
1665 static
1666 void
1667 sili_atapi_complete_cmd(struct ata_xfer *xa)
1669 union ccb *ccb = xa->atascsi_private;
1670 struct ccb_hdr *ccbh = &ccb->ccb_h;
1671 struct sili_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1672 scsi_cdb_t cdb;
1674 cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1675 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes);
1677 switch(xa->state) {
1678 case ATA_S_COMPLETE:
1679 ccbh->status = CAM_REQ_CMP;
1680 ccb->csio.scsi_status = SCSI_STATUS_OK;
1681 break;
1682 case ATA_S_ERROR:
1683 ccbh->status = CAM_SCSI_STATUS_ERROR;
1684 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1685 sili_ata_atapi_sense(xa->rfis, &ccb->csio.sense_data);
1686 break;
1687 case ATA_S_TIMEOUT:
1688 kprintf("%s: cmd %d: timeout\n",
1689 PORTNAME(ap), cdb->generic.opcode);
1690 ccbh->status = CAM_CMD_TIMEOUT;
1691 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1692 sili_ata_dummy_sense(&ccb->csio.sense_data);
1693 break;
1694 default:
1695 kprintf("%s: cmd %d: unknown state %d\n",
1696 PORTNAME(ap), cdb->generic.opcode, xa->state);
1697 ccbh->status = CAM_REQ_CMP_ERR;
1698 break;
1700 ccb->csio.resid = xa->resid;
1701 sili_ata_put_xfer(xa);
1702 sili_os_unlock_port(ap);
1703 xpt_done(ccb);
1704 sili_os_lock_port(ap);
1708 * Construct dummy sense data for errors on DISKs
1710 static
1711 void
1712 sili_ata_dummy_sense(struct scsi_sense_data *sense_data)
1714 sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
1715 sense_data->segment = 0;
1716 sense_data->flags = SSD_KEY_MEDIUM_ERROR;
1717 sense_data->info[0] = 0;
1718 sense_data->info[1] = 0;
1719 sense_data->info[2] = 0;
1720 sense_data->info[3] = 0;
1721 sense_data->extra_len = 0;
1725 * Construct atapi sense data for errors on ATAPI
1727 * The ATAPI sense data is stored in the passed rfis and must be converted
1728 * to SCSI sense data.
1730 static
1731 void
1732 sili_ata_atapi_sense(struct ata_fis_d2h *rfis,
1733 struct scsi_sense_data *sense_data)
1735 sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
1736 sense_data->segment = 0;
1737 sense_data->flags = (rfis->error & 0xF0) >> 4;
1738 if (rfis->error & 0x04)
1739 sense_data->flags |= SSD_KEY_ILLEGAL_REQUEST;
1740 if (rfis->error & 0x02)
1741 sense_data->flags |= SSD_EOM;
1742 if (rfis->error & 0x01)
1743 sense_data->flags |= SSD_ILI;
1744 sense_data->info[0] = 0;
1745 sense_data->info[1] = 0;
1746 sense_data->info[2] = 0;
1747 sense_data->info[3] = 0;
1748 sense_data->extra_len = 0;
1751 static
1752 void
1753 sili_strip_string(const char **basep, int *lenp)
1755 const char *base = *basep;
1756 int len = *lenp;
1758 while (len && (*base == 0 || *base == ' ')) {
1759 --len;
1760 ++base;
1762 while (len && (base[len-1] == 0 || base[len-1] == ' '))
1763 --len;
1764 *basep = base;
1765 *lenp = len;