AHCI - Add serial number support, strip whitespace.
[dragonfly.git] / sys / dev / disk / ahci / ahci_cam.c
blobcb51f38b5bc612318b4bda9b6492848c22800077
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 "ahci.h"
63 const char *ScsiTypeArray[32] = {
64 "DIRECT",
65 "SEQUENTIAL",
66 "PRINTER",
67 "PROCESSOR",
68 "WORM",
69 "CDROM",
70 "SCANNER",
71 "OPTICAL",
72 "CHANGER",
73 "COMM",
74 "ASC0",
75 "ASC1",
76 "STORARRAY",
77 "ENCLOSURE",
78 "RBC",
79 "OCRW",
80 "0x10",
81 "OSD",
82 "ADC",
83 "0x13",
84 "0x14",
85 "0x15",
86 "0x16",
87 "0x17",
88 "0x18",
89 "0x19",
90 "0x1A",
91 "0x1B",
92 "0x1C",
93 "0x1D",
94 "0x1E",
95 "NODEVICE"
98 static void ahci_xpt_action(struct cam_sim *sim, union ccb *ccb);
99 static void ahci_xpt_poll(struct cam_sim *sim);
100 static void ahci_xpt_scsi_disk_io(struct ahci_port *ap,
101 struct ata_port *at, union ccb *ccb);
102 static void ahci_xpt_scsi_atapi_io(struct ahci_port *ap,
103 struct ata_port *at, union ccb *ccb);
104 static void ahci_xpt_page_inquiry(struct ahci_port *ap,
105 struct ata_port *at, union ccb *ccb);
107 static void ahci_ata_complete_disk_rw(struct ata_xfer *xa);
108 static void ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa);
109 static void ahci_atapi_complete_cmd(struct ata_xfer *xa);
110 static void ahci_ata_dummy_sense(struct scsi_sense_data *sense_data);
111 static void ahci_ata_atapi_sense(struct ata_fis_d2h *rfis,
112 struct scsi_sense_data *sense_data);
114 static int ahci_cam_probe_disk(struct ahci_port *ap, struct ata_port *at);
115 static int ahci_cam_probe_atapi(struct ahci_port *ap, struct ata_port *at);
116 static void ahci_ata_dummy_done(struct ata_xfer *xa);
117 static void ata_fix_identify(struct ata_identify *id);
118 static void ahci_cam_rescan(struct ahci_port *ap);
119 static void ahci_strip_string(const char **basep, int *lenp);
122 ahci_cam_attach(struct ahci_port *ap)
124 struct cam_devq *devq;
125 struct cam_sim *sim;
126 int error;
127 int unit;
130 * We want at least one ccb to be available for error processing
131 * so don't let CAM use more then ncmds - 1.
133 unit = device_get_unit(ap->ap_sc->sc_dev);
134 if (ap->ap_sc->sc_ncmds > 1)
135 devq = cam_simq_alloc(ap->ap_sc->sc_ncmds - 1);
136 else
137 devq = cam_simq_alloc(ap->ap_sc->sc_ncmds);
138 if (devq == NULL) {
139 return (ENOMEM);
141 sim = cam_sim_alloc(ahci_xpt_action, ahci_xpt_poll, "ahci",
142 (void *)ap, unit, &sim_mplock, 1, 1, devq);
143 cam_simq_release(devq);
144 if (sim == NULL) {
145 return (ENOMEM);
147 ap->ap_sim = sim;
148 ahci_os_unlock_port(ap);
149 error = xpt_bus_register(ap->ap_sim, ap->ap_num);
150 ahci_os_lock_port(ap);
151 if (error != CAM_SUCCESS) {
152 ahci_cam_detach(ap);
153 return (EINVAL);
155 ap->ap_flags |= AP_F_BUS_REGISTERED;
157 if (ap->ap_probe == ATA_PROBE_NEED_IDENT)
158 error = ahci_cam_probe(ap, NULL);
159 else
160 error = 0;
161 if (error) {
162 ahci_cam_detach(ap);
163 return (EIO);
165 ap->ap_flags |= AP_F_CAM_ATTACHED;
167 return(0);
171 * The state of the port has changed.
173 * If at is NULL the physical port has changed state.
174 * If at is non-NULL a particular target behind a PM has changed state.
176 * If found is -1 the target state must be queued to a non-interrupt context.
177 * (only works with at == NULL).
179 * If found is 0 the target was removed.
180 * If found is 1 the target was inserted.
182 void
183 ahci_cam_changed(struct ahci_port *ap, struct ata_port *atx, int found)
185 struct cam_path *tmppath;
186 int status;
187 int target;
189 target = atx ? atx->at_target : CAM_TARGET_WILDCARD;
191 if (ap->ap_sim == NULL)
192 return;
193 if (found == CAM_TARGET_WILDCARD) {
194 status = xpt_create_path(&tmppath, NULL,
195 cam_sim_path(ap->ap_sim),
196 target, CAM_LUN_WILDCARD);
197 if (status != CAM_REQ_CMP)
198 return;
199 ahci_cam_rescan(ap);
200 } else {
201 status = xpt_create_path(&tmppath, NULL,
202 cam_sim_path(ap->ap_sim),
203 target,
204 CAM_LUN_WILDCARD);
205 if (status != CAM_REQ_CMP)
206 return;
207 #if 0
209 * This confuses CAM
211 if (found)
212 xpt_async(AC_FOUND_DEVICE, tmppath, NULL);
213 else
214 xpt_async(AC_LOST_DEVICE, tmppath, NULL);
215 #endif
217 xpt_free_path(tmppath);
220 void
221 ahci_cam_detach(struct ahci_port *ap)
223 int error;
225 if ((ap->ap_flags & AP_F_CAM_ATTACHED) == 0)
226 return;
227 get_mplock();
228 if (ap->ap_sim) {
229 xpt_freeze_simq(ap->ap_sim, 1);
231 if (ap->ap_flags & AP_F_BUS_REGISTERED) {
232 error = xpt_bus_deregister(cam_sim_path(ap->ap_sim));
233 KKASSERT(error == CAM_REQ_CMP);
234 ap->ap_flags &= ~AP_F_BUS_REGISTERED;
236 if (ap->ap_sim) {
237 cam_sim_free(ap->ap_sim);
238 ap->ap_sim = NULL;
240 rel_mplock();
241 ap->ap_flags &= ~AP_F_CAM_ATTACHED;
245 * Once the AHCI port has been attached we need to probe for a device or
246 * devices on the port and setup various options.
248 * If at is NULL we are probing the direct-attached device on the port,
249 * which may or may not be a port multiplier.
252 ahci_cam_probe(struct ahci_port *ap, struct ata_port *atx)
254 struct ata_port *at;
255 struct ata_xfer *xa;
256 u_int64_t capacity;
257 u_int64_t capacity_bytes;
258 int model_len;
259 int firmware_len;
260 int serial_len;
261 int error;
262 int devncqdepth;
263 int i;
264 const char *model_id;
265 const char *firmware_id;
266 const char *serial_id;
267 const char *wcstr;
268 const char *rastr;
269 const char *scstr;
270 const char *type;
272 error = EIO;
275 * Delayed CAM attachment for initial probe, sim may be NULL
277 if (ap->ap_sim == NULL)
278 return(0);
281 * A NULL atx indicates a probe of the directly connected device.
282 * A non-NULL atx indicates a device connected via a port multiplier.
283 * We need to preserve atx for calls to ahci_ata_get_xfer().
285 * at is always non-NULL. For directly connected devices we supply
286 * an (at) pointing to target 0.
288 if (atx == NULL) {
289 at = ap->ap_ata; /* direct attached - device 0 */
290 if (ap->ap_type == ATA_PORT_T_PM) {
291 kprintf("%s: Found Port Multiplier\n",
292 ATANAME(ap, atx));
293 return (0);
295 at->at_type = ap->ap_type;
296 } else {
297 at = atx;
298 if (atx->at_type == ATA_PORT_T_PM) {
299 kprintf("%s: Bogus device, reducing port count to %d\n",
300 ATANAME(ap, atx), atx->at_target);
301 if (ap->ap_pmcount > atx->at_target)
302 ap->ap_pmcount = atx->at_target;
303 goto err;
306 if (ap->ap_type == ATA_PORT_T_NONE)
307 goto err;
308 if (at->at_type == ATA_PORT_T_NONE)
309 goto err;
312 * Issue identify, saving the result
314 xa = ahci_ata_get_xfer(ap, atx);
315 xa->complete = ahci_ata_dummy_done;
316 xa->data = &at->at_identify;
317 xa->datalen = sizeof(at->at_identify);
318 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
319 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
321 switch(at->at_type) {
322 case ATA_PORT_T_DISK:
323 xa->fis->command = ATA_C_IDENTIFY;
324 type = "DISK";
325 break;
326 case ATA_PORT_T_ATAPI:
327 xa->fis->command = ATA_C_ATAPI_IDENTIFY;
328 xa->flags |= ATA_F_AUTOSENSE;
329 type = "ATAPI";
330 break;
331 default:
332 xa->fis->command = ATA_C_ATAPI_IDENTIFY;
333 type = "UNKNOWN(ATAPI?)";
334 break;
336 xa->fis->features = 0;
337 xa->fis->device = 0;
338 xa->timeout = 1000;
340 if (ahci_ata_cmd(xa) != ATA_S_COMPLETE) {
341 kprintf("%s: Detected %s device but unable to IDENTIFY\n",
342 ATANAME(ap, atx), type);
343 ahci_ata_put_xfer(xa);
344 goto err;
346 ahci_ata_put_xfer(xa);
348 ata_fix_identify(&at->at_identify);
351 * Read capacity using SATA probe info.
353 if (le16toh(at->at_identify.cmdset83) & 0x0400) {
354 /* LBA48 feature set supported */
355 capacity = 0;
356 for (i = 3; i >= 0; --i) {
357 capacity <<= 16;
358 capacity +=
359 le16toh(at->at_identify.addrsecxt[i]);
361 } else {
362 capacity = le16toh(at->at_identify.addrsec[1]);
363 capacity <<= 16;
364 capacity += le16toh(at->at_identify.addrsec[0]);
366 if (capacity == 0)
367 capacity = 1024 * 1024 / 512;
368 at->at_capacity = capacity;
369 if (atx == NULL)
370 ap->ap_probe = ATA_PROBE_GOOD;
372 capacity_bytes = capacity * 512;
375 * Negotiate NCQ, throw away any ata_xfer's beyond the negotiated
376 * number of slots and limit the number of CAM ccb's to one less
377 * so we always have a slot available for recovery.
379 * NCQ is not used if ap_ncqdepth is 1 or the host controller does
380 * not support it, and in that case the driver can handle extra
381 * ccb's.
383 * NCQ is currently used only with direct-attached disks. It is
384 * not used with port multipliers or direct-attached ATAPI devices.
386 * Remember at least one extra CCB needs to be reserved for the
387 * error ccb.
389 if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) &&
390 ap->ap_type == ATA_PORT_T_DISK &&
391 (le16toh(at->at_identify.satacap) & (1 << 8))) {
392 at->at_ncqdepth = (le16toh(at->at_identify.qdepth) & 0x1F) + 1;
393 devncqdepth = at->at_ncqdepth;
394 if (at->at_ncqdepth > ap->ap_sc->sc_ncmds)
395 at->at_ncqdepth = ap->ap_sc->sc_ncmds;
396 if (at->at_ncqdepth > 1) {
397 for (i = 0; i < ap->ap_sc->sc_ncmds; ++i) {
398 xa = ahci_ata_get_xfer(ap, atx);
399 if (xa->tag < at->at_ncqdepth) {
400 xa->state = ATA_S_COMPLETE;
401 ahci_ata_put_xfer(xa);
404 if (at->at_ncqdepth >= ap->ap_sc->sc_ncmds) {
405 cam_devq_resize(ap->ap_sim->devq,
406 at->at_ncqdepth - 1);
409 } else {
410 devncqdepth = 0;
413 model_len = sizeof(at->at_identify.model);
414 model_id = at->at_identify.model;
415 ahci_strip_string(&model_id, &model_len);
417 firmware_len = sizeof(at->at_identify.firmware);
418 firmware_id = at->at_identify.firmware;
419 ahci_strip_string(&firmware_id, &firmware_len);
421 serial_len = sizeof(at->at_identify.serial);
422 serial_id = at->at_identify.serial;
423 ahci_strip_string(&serial_id, &serial_len);
426 * Generate informatiive strings.
428 * NOTE: We do not automatically set write caching, lookahead,
429 * or the security state for ATAPI devices.
431 if (at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) {
432 if (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE)
433 wcstr = "enabled";
434 else if (at->at_type == ATA_PORT_T_ATAPI)
435 wcstr = "disabled";
436 else
437 wcstr = "enabling";
438 } else {
439 wcstr = "notsupp";
442 if (at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) {
443 if (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD)
444 rastr = "enabled";
445 else if (at->at_type == ATA_PORT_T_ATAPI)
446 rastr = "disabled";
447 else
448 rastr = "enabling";
449 } else {
450 rastr = "notsupp";
453 if (at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) {
454 if (at->at_identify.securestatus & ATA_SECURE_FROZEN)
455 scstr = "frozen";
456 else if (at->at_type == ATA_PORT_T_ATAPI)
457 scstr = "unfrozen";
458 else if (AhciNoFeatures & (1 << ap->ap_num))
459 scstr = "<disabled>";
460 else
461 scstr = "freezing";
462 } else {
463 scstr = "notsupp";
466 kprintf("%s: Found %s \"%*.*s %*.*s\" serial=\"%*.*s\"\n"
467 "%s: tags=%d/%d satacap=%04x satafea=%04x NCQ=%s "
468 "capacity=%lld.%02dMB\n",
470 ATANAME(ap, atx),
471 type,
472 model_len, model_len, model_id,
473 firmware_len, firmware_len, firmware_id,
474 serial_len, serial_len, serial_id,
476 ATANAME(ap, atx),
477 devncqdepth, ap->ap_sc->sc_ncmds,
478 at->at_identify.satacap,
479 at->at_identify.satafsup,
480 (at->at_ncqdepth > 1 ? "YES" : "NO"),
481 (long long)capacity_bytes / (1024 * 1024),
482 (int)(capacity_bytes % (1024 * 1024)) * 100 / (1024 * 1024)
484 kprintf("%s: f85=%04x f86=%04x f87=%04x WC=%s RA=%s SEC=%s\n",
485 ATANAME(ap, atx),
486 at->at_identify.features85,
487 at->at_identify.features86,
488 at->at_identify.features87,
489 wcstr,
490 rastr,
491 scstr
495 * Additional type-specific probing
497 switch(at->at_type) {
498 case ATA_PORT_T_DISK:
499 error = ahci_cam_probe_disk(ap, atx);
500 break;
501 case ATA_PORT_T_ATAPI:
502 error = ahci_cam_probe_atapi(ap, atx);
503 break;
504 default:
505 error = EIO;
506 break;
508 err:
509 if (error) {
510 at->at_probe = ATA_PROBE_FAILED;
511 if (atx == NULL)
512 ap->ap_probe = at->at_probe;
513 } else {
514 at->at_probe = ATA_PROBE_GOOD;
515 if (atx == NULL)
516 ap->ap_probe = at->at_probe;
518 return (error);
522 * DISK-specific probe after initial ident
524 static int
525 ahci_cam_probe_disk(struct ahci_port *ap, struct ata_port *atx)
527 struct ata_port *at;
528 struct ata_xfer *xa;
530 at = atx ? atx : ap->ap_ata;
533 * Enable write cache if supported
535 * NOTE: "WD My Book" external disk devices have a very poor
536 * daughter board between the the ESATA and the HD. Sending
537 * any ATA_C_SET_FEATURES commands will break the hardware port
538 * with a fatal protocol error. However, this device also
539 * indicates that WRITECACHE is already on and READAHEAD is
540 * not supported so we avoid the issue.
542 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) &&
543 (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE) == 0) {
544 xa = ahci_ata_get_xfer(ap, atx);
545 xa->complete = ahci_ata_dummy_done;
546 xa->fis->command = ATA_C_SET_FEATURES;
547 /*xa->fis->features = ATA_SF_WRITECACHE_EN;*/
548 xa->fis->features = ATA_SF_LOOKAHEAD_EN;
549 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
550 xa->fis->device = 0;
551 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
552 xa->timeout = 1000;
553 xa->datalen = 0;
554 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
555 at->at_features |= ATA_PORT_F_WCACHE;
556 else
557 kprintf("%s: Unable to enable write-caching\n",
558 ATANAME(ap, atx));
559 ahci_ata_put_xfer(xa);
563 * Enable readahead if supported
565 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) &&
566 (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD) == 0) {
567 xa = ahci_ata_get_xfer(ap, atx);
568 xa->complete = ahci_ata_dummy_done;
569 xa->fis->command = ATA_C_SET_FEATURES;
570 xa->fis->features = ATA_SF_LOOKAHEAD_EN;
571 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
572 xa->fis->device = 0;
573 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
574 xa->timeout = 1000;
575 xa->datalen = 0;
576 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
577 at->at_features |= ATA_PORT_F_RAHEAD;
578 else
579 kprintf("%s: Unable to enable read-ahead\n",
580 ATANAME(ap, atx));
581 ahci_ata_put_xfer(xa);
585 * FREEZE LOCK the device so malicious users can't lock it on us.
586 * As there is no harm in issuing this to devices that don't
587 * support the security feature set we just send it, and don't bother
588 * checking if the device sends a command abort to tell us it doesn't
589 * support it
591 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) &&
592 (at->at_identify.securestatus & ATA_SECURE_FROZEN) == 0 &&
593 (AhciNoFeatures & (1 << ap->ap_num)) == 0) {
594 xa = ahci_ata_get_xfer(ap, atx);
595 xa->complete = ahci_ata_dummy_done;
596 xa->fis->command = ATA_C_SEC_FREEZE_LOCK;
597 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
598 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
599 xa->timeout = 1000;
600 xa->datalen = 0;
601 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
602 at->at_features |= ATA_PORT_F_FRZLCK;
603 else
604 kprintf("%s: Unable to set security freeze\n",
605 ATANAME(ap, atx));
606 ahci_ata_put_xfer(xa);
609 return (0);
613 * ATAPI-specific probe after initial ident
615 static int
616 ahci_cam_probe_atapi(struct ahci_port *ap, struct ata_port *atx)
618 return(0);
622 * Fix byte ordering so buffers can be accessed as
623 * strings.
625 static void
626 ata_fix_identify(struct ata_identify *id)
628 u_int16_t *swap;
629 int i;
631 swap = (u_int16_t *)id->serial;
632 for (i = 0; i < sizeof(id->serial) / sizeof(u_int16_t); i++)
633 swap[i] = bswap16(swap[i]);
635 swap = (u_int16_t *)id->firmware;
636 for (i = 0; i < sizeof(id->firmware) / sizeof(u_int16_t); i++)
637 swap[i] = bswap16(swap[i]);
639 swap = (u_int16_t *)id->model;
640 for (i = 0; i < sizeof(id->model) / sizeof(u_int16_t); i++)
641 swap[i] = bswap16(swap[i]);
645 * Dummy done callback for xa.
647 static void
648 ahci_ata_dummy_done(struct ata_xfer *xa)
653 * Use an engineering request to initiate a target scan for devices
654 * behind a port multiplier.
656 * An asynchronous bus scan is used to avoid reentrancy issues.
658 static void
659 ahci_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
661 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
663 if (ccb->ccb_h.func_code == XPT_SCAN_BUS) {
664 ap->ap_flags &= ~AP_F_SCAN_RUNNING;
665 if (ap->ap_flags & AP_F_SCAN_REQUESTED) {
666 ap->ap_flags &= ~AP_F_SCAN_REQUESTED;
667 ahci_cam_rescan(ap);
669 ap->ap_flags |= AP_F_SCAN_COMPLETED;
670 wakeup(&ap->ap_flags);
672 xpt_free_ccb(ccb);
675 static void
676 ahci_cam_rescan(struct ahci_port *ap)
678 struct cam_path *path;
679 union ccb *ccb;
680 int status;
681 int i;
683 if (ap->ap_flags & AP_F_SCAN_RUNNING) {
684 ap->ap_flags |= AP_F_SCAN_REQUESTED;
685 return;
687 ap->ap_flags |= AP_F_SCAN_RUNNING;
688 for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
689 ap->ap_ata[i].at_features |= ATA_PORT_F_RESCAN;
692 status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim),
693 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
694 if (status != CAM_REQ_CMP)
695 return;
697 ccb = xpt_alloc_ccb();
698 xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */
699 ccb->ccb_h.func_code = XPT_ENG_EXEC;
700 ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback;
701 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
702 ccb->crcn.flags = CAM_FLAG_NONE;
703 xpt_action_async(ccb);
706 static void
707 ahci_xpt_rescan(struct ahci_port *ap)
709 struct cam_path *path;
710 union ccb *ccb;
711 int status;
713 status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim),
714 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
715 if (status != CAM_REQ_CMP)
716 return;
718 ccb = xpt_alloc_ccb();
719 xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */
720 ccb->ccb_h.func_code = XPT_SCAN_BUS;
721 ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback;
722 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
723 ccb->crcn.flags = CAM_FLAG_NONE;
724 xpt_action_async(ccb);
728 * Action function - dispatch command
730 static
731 void
732 ahci_xpt_action(struct cam_sim *sim, union ccb *ccb)
734 struct ahci_port *ap;
735 struct ata_port *at, *atx;
736 struct ccb_hdr *ccbh;
737 int unit;
739 /* XXX lock */
740 ap = cam_sim_softc(sim);
741 at = ap->ap_ata;
742 atx = NULL;
743 KKASSERT(ap != NULL);
744 ccbh = &ccb->ccb_h;
745 unit = cam_sim_unit(sim);
748 * Early failure checks. These checks do not apply to XPT_PATH_INQ,
749 * otherwise the bus rescan will not remove the dead devices when
750 * unplugging a PM.
752 * For non-wildcards we have one target (0) and one lun (0),
753 * unless we have a port multiplier.
755 * A wildcard target indicates only the general bus is being
756 * probed.
758 * Calculate at and atx. at is always non-NULL. atx is only
759 * non-NULL for direct-attached devices. It will be NULL for
760 * devices behind a port multiplier.
762 * XXX What do we do with a LUN wildcard?
764 if (ccbh->target_id != CAM_TARGET_WILDCARD &&
765 ccbh->func_code != XPT_PATH_INQ) {
766 if (ap->ap_type == ATA_PORT_T_NONE) {
767 ccbh->status = CAM_DEV_NOT_THERE;
768 xpt_done(ccb);
769 return;
771 if (ccbh->target_id < 0 || ccbh->target_id >= ap->ap_pmcount) {
772 ccbh->status = CAM_DEV_NOT_THERE;
773 xpt_done(ccb);
774 return;
776 at += ccbh->target_id;
777 if (ap->ap_type == ATA_PORT_T_PM)
778 atx = at;
780 if (ccbh->target_lun != CAM_LUN_WILDCARD && ccbh->target_lun) {
781 ccbh->status = CAM_DEV_NOT_THERE;
782 xpt_done(ccb);
783 return;
788 * Switch on the meta XPT command
790 switch(ccbh->func_code) {
791 case XPT_ENG_EXEC:
793 * This routine is called after a port multiplier has been
794 * probed.
796 ccbh->status = CAM_REQ_CMP;
797 ahci_os_lock_port(ap);
798 ahci_port_state_machine(ap, 0);
799 ahci_os_unlock_port(ap);
800 xpt_done(ccb);
801 ahci_xpt_rescan(ap);
802 break;
803 case XPT_PATH_INQ:
805 * This command always succeeds, otherwise the bus scan
806 * will not detach dead devices.
808 ccb->cpi.version_num = 1;
809 ccb->cpi.hba_inquiry = 0;
810 ccb->cpi.target_sprt = 0;
811 ccb->cpi.hba_misc = PIM_SEQSCAN;
812 ccb->cpi.hba_eng_cnt = 0;
813 bzero(ccb->cpi.vuhba_flags, sizeof(ccb->cpi.vuhba_flags));
814 ccb->cpi.max_target = AHCI_MAX_PMPORTS - 1;
815 ccb->cpi.max_lun = 0;
816 ccb->cpi.async_flags = 0;
817 ccb->cpi.hpath_id = 0;
818 ccb->cpi.initiator_id = AHCI_MAX_PMPORTS - 1;
819 ccb->cpi.unit_number = cam_sim_unit(sim);
820 ccb->cpi.bus_id = cam_sim_bus(sim);
821 ccb->cpi.base_transfer_speed = 150000;
822 ccb->cpi.transport = XPORT_SATA;
823 ccb->cpi.transport_version = 1;
824 ccb->cpi.protocol = PROTO_SCSI;
825 ccb->cpi.protocol_version = SCSI_REV_2;
827 ccbh->status = CAM_REQ_CMP;
828 if (ccbh->target_id == CAM_TARGET_WILDCARD) {
829 ahci_os_lock_port(ap);
830 ahci_port_state_machine(ap, 0);
831 ahci_os_unlock_port(ap);
832 } else {
833 switch(ahci_pread(ap, AHCI_PREG_SSTS) &
834 AHCI_PREG_SSTS_SPD) {
835 case AHCI_PREG_SSTS_SPD_GEN1:
836 ccb->cpi.base_transfer_speed = 150000;
837 break;
838 case AHCI_PREG_SSTS_SPD_GEN2:
839 ccb->cpi.base_transfer_speed = 300000;
840 break;
841 default:
842 /* unknown */
843 ccb->cpi.base_transfer_speed = 1000;
844 break;
846 #if 0
847 if (ap->ap_type == ATA_PORT_T_NONE)
848 ccbh->status = CAM_DEV_NOT_THERE;
849 #endif
851 xpt_done(ccb);
852 break;
853 case XPT_RESET_DEV:
854 ahci_os_lock_port(ap);
855 if (ap->ap_type == ATA_PORT_T_NONE) {
856 ccbh->status = CAM_DEV_NOT_THERE;
857 } else {
858 ahci_port_reset(ap, atx, 0);
859 ccbh->status = CAM_REQ_CMP;
861 ahci_os_unlock_port(ap);
862 xpt_done(ccb);
863 break;
864 case XPT_RESET_BUS:
865 ahci_os_lock_port(ap);
866 ahci_port_reset(ap, NULL, 1);
867 ahci_os_unlock_port(ap);
868 ccbh->status = CAM_REQ_CMP;
869 xpt_done(ccb);
870 break;
871 case XPT_SET_TRAN_SETTINGS:
872 ccbh->status = CAM_FUNC_NOTAVAIL;
873 xpt_done(ccb);
874 break;
875 case XPT_GET_TRAN_SETTINGS:
876 ccb->cts.protocol = PROTO_SCSI;
877 ccb->cts.protocol_version = SCSI_REV_2;
878 ccb->cts.transport = XPORT_SATA;
879 ccb->cts.transport_version = XPORT_VERSION_UNSPECIFIED;
880 ccb->cts.proto_specific.valid = 0;
881 ccb->cts.xport_specific.valid = 0;
882 ccbh->status = CAM_REQ_CMP;
883 xpt_done(ccb);
884 break;
885 case XPT_CALC_GEOMETRY:
886 cam_calc_geometry(&ccb->ccg, 1);
887 xpt_done(ccb);
888 break;
889 case XPT_SCSI_IO:
891 * Our parallel startup code might have only probed through
892 * to the IDENT, so do the last step if necessary.
894 if (at->at_probe == ATA_PROBE_NEED_IDENT)
895 ahci_cam_probe(ap, atx);
896 if (at->at_probe != ATA_PROBE_GOOD) {
897 ccbh->status = CAM_DEV_NOT_THERE;
898 xpt_done(ccb);
899 break;
901 switch(at->at_type) {
902 case ATA_PORT_T_DISK:
903 ahci_xpt_scsi_disk_io(ap, atx, ccb);
904 break;
905 case ATA_PORT_T_ATAPI:
906 ahci_xpt_scsi_atapi_io(ap, atx, ccb);
907 break;
908 default:
909 ccbh->status = CAM_REQ_INVALID;
910 xpt_done(ccb);
911 break;
913 break;
914 default:
915 ccbh->status = CAM_REQ_INVALID;
916 xpt_done(ccb);
917 break;
922 * Poll function.
924 * Generally this function gets called heavily when interrupts might be
925 * non-operational, during a halt/reboot or panic.
927 static
928 void
929 ahci_xpt_poll(struct cam_sim *sim)
931 struct ahci_port *ap;
933 ap = cam_sim_softc(sim);
934 crit_enter();
935 ahci_os_lock_port(ap);
936 ahci_port_intr(ap, 1);
937 ahci_os_unlock_port(ap);
938 crit_exit();
942 * Convert the SCSI command in ccb to an ata_xfer command in xa
943 * for ATA_PORT_T_DISK operations. Set the completion function
944 * to convert the response back, then dispatch to the OpenBSD AHCI
945 * layer.
947 * AHCI DISK commands only support a limited command set, and we
948 * fake additional commands to make it play nice with the CAM subsystem.
950 static
951 void
952 ahci_xpt_scsi_disk_io(struct ahci_port *ap, struct ata_port *atx,
953 union ccb *ccb)
955 struct ccb_hdr *ccbh;
956 struct ccb_scsiio *csio;
957 struct ata_xfer *xa;
958 struct ata_port *at;
959 struct ata_fis_h2d *fis;
960 scsi_cdb_t cdb;
961 union scsi_data *rdata;
962 int rdata_len;
963 u_int64_t capacity;
964 u_int64_t lba;
965 u_int32_t count;
967 ccbh = &ccb->csio.ccb_h;
968 csio = &ccb->csio;
969 at = atx ? atx : &ap->ap_ata[0];
972 * XXX not passing NULL at for direct attach!
974 xa = ahci_ata_get_xfer(ap, atx);
975 rdata = (void *)csio->data_ptr;
976 rdata_len = csio->dxfer_len;
979 * Build the FIS or process the csio to completion.
981 cdb = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
982 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
984 switch(cdb->generic.opcode) {
985 case REQUEST_SENSE:
987 * Auto-sense everything, so explicit sense requests
988 * return no-sense.
990 ccbh->status = CAM_SCSI_STATUS_ERROR;
991 break;
992 case INQUIRY:
994 * Inquiry supported features
996 * [opcode, byte2, page_code, length, control]
998 if (cdb->inquiry.byte2 & SI_EVPD) {
999 ahci_xpt_page_inquiry(ap, at, ccb);
1000 } else {
1001 bzero(rdata, rdata_len);
1002 if (rdata_len < SHORT_INQUIRY_LENGTH) {
1003 ccbh->status = CAM_CCB_LEN_ERR;
1004 break;
1006 if (rdata_len > sizeof(rdata->inquiry_data))
1007 rdata_len = sizeof(rdata->inquiry_data);
1008 rdata->inquiry_data.device = T_DIRECT;
1009 rdata->inquiry_data.version = SCSI_REV_SPC2;
1010 rdata->inquiry_data.response_format = 2;
1011 rdata->inquiry_data.additional_length = 32;
1012 bcopy("SATA ", rdata->inquiry_data.vendor, 8);
1013 bcopy(at->at_identify.model,
1014 rdata->inquiry_data.product,
1015 sizeof(rdata->inquiry_data.product));
1016 bcopy(at->at_identify.firmware,
1017 rdata->inquiry_data.revision,
1018 sizeof(rdata->inquiry_data.revision));
1019 ccbh->status = CAM_REQ_CMP;
1021 break;
1022 case READ_CAPACITY_16:
1023 if (cdb->read_capacity_16.service_action != SRC16_SERVICE_ACTION) {
1024 ccbh->status = CAM_REQ_INVALID;
1025 break;
1027 if (rdata_len < sizeof(rdata->read_capacity_data_16)) {
1028 ccbh->status = CAM_CCB_LEN_ERR;
1029 break;
1031 /* fall through */
1032 case READ_CAPACITY:
1033 if (rdata_len < sizeof(rdata->read_capacity_data)) {
1034 ccbh->status = CAM_CCB_LEN_ERR;
1035 break;
1038 capacity = at->at_capacity;
1040 bzero(rdata, rdata_len);
1041 if (cdb->generic.opcode == READ_CAPACITY) {
1042 rdata_len = sizeof(rdata->read_capacity_data);
1043 if (capacity > 0xFFFFFFFFU)
1044 capacity = 0xFFFFFFFFU;
1045 bzero(&rdata->read_capacity_data, rdata_len);
1046 scsi_ulto4b((u_int32_t)capacity - 1,
1047 rdata->read_capacity_data.addr);
1048 scsi_ulto4b(512, rdata->read_capacity_data.length);
1049 } else {
1050 rdata_len = sizeof(rdata->read_capacity_data_16);
1051 bzero(&rdata->read_capacity_data_16, rdata_len);
1052 scsi_u64to8b(capacity - 1,
1053 rdata->read_capacity_data_16.addr);
1054 scsi_ulto4b(512, rdata->read_capacity_data_16.length);
1056 ccbh->status = CAM_REQ_CMP;
1057 break;
1058 case SYNCHRONIZE_CACHE:
1060 * Synchronize cache. Specification says this can take
1061 * greater then 30 seconds so give it at least 45.
1063 fis = xa->fis;
1064 fis->flags = ATA_H2D_FLAGS_CMD;
1065 fis->command = ATA_C_FLUSH_CACHE;
1066 fis->device = 0;
1067 if (xa->timeout < 45000)
1068 xa->timeout = 45000;
1069 xa->datalen = 0;
1070 xa->flags = ATA_F_READ;
1071 xa->complete = ahci_ata_complete_disk_synchronize_cache;
1072 break;
1073 case TEST_UNIT_READY:
1074 case START_STOP_UNIT:
1075 case PREVENT_ALLOW:
1077 * Just silently return success
1079 ccbh->status = CAM_REQ_CMP;
1080 rdata_len = 0;
1081 break;
1082 case ATA_PASS_12:
1083 case ATA_PASS_16:
1085 * XXX implement pass-through
1087 ccbh->status = CAM_FUNC_NOTAVAIL;
1088 break;
1089 default:
1090 switch(cdb->generic.opcode) {
1091 case READ_6:
1092 lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1093 count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1094 xa->flags = ATA_F_READ;
1095 break;
1096 case READ_10:
1097 lba = scsi_4btoul(cdb->rw_10.addr);
1098 count = scsi_2btoul(cdb->rw_10.length);
1099 xa->flags = ATA_F_READ;
1100 break;
1101 case READ_12:
1102 lba = scsi_4btoul(cdb->rw_12.addr);
1103 count = scsi_4btoul(cdb->rw_12.length);
1104 xa->flags = ATA_F_READ;
1105 break;
1106 case READ_16:
1107 lba = scsi_8btou64(cdb->rw_16.addr);
1108 count = scsi_4btoul(cdb->rw_16.length);
1109 xa->flags = ATA_F_READ;
1110 break;
1111 case WRITE_6:
1112 lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1113 count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1114 xa->flags = ATA_F_WRITE;
1115 break;
1116 case WRITE_10:
1117 lba = scsi_4btoul(cdb->rw_10.addr);
1118 count = scsi_2btoul(cdb->rw_10.length);
1119 xa->flags = ATA_F_WRITE;
1120 break;
1121 case WRITE_12:
1122 lba = scsi_4btoul(cdb->rw_12.addr);
1123 count = scsi_4btoul(cdb->rw_12.length);
1124 xa->flags = ATA_F_WRITE;
1125 break;
1126 case WRITE_16:
1127 lba = scsi_8btou64(cdb->rw_16.addr);
1128 count = scsi_4btoul(cdb->rw_16.length);
1129 xa->flags = ATA_F_WRITE;
1130 break;
1131 default:
1132 ccbh->status = CAM_REQ_INVALID;
1133 break;
1135 if (ccbh->status != CAM_REQ_INPROG)
1136 break;
1138 fis = xa->fis;
1139 fis->flags = ATA_H2D_FLAGS_CMD;
1140 fis->lba_low = (u_int8_t)lba;
1141 fis->lba_mid = (u_int8_t)(lba >> 8);
1142 fis->lba_high = (u_int8_t)(lba >> 16);
1143 fis->device = ATA_H2D_DEVICE_LBA;
1146 * NCQ only for direct-attached disks, do not currently
1147 * try to use NCQ with port multipliers.
1149 if (at->at_ncqdepth > 1 &&
1150 ap->ap_type == ATA_PORT_T_DISK &&
1151 (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) &&
1152 (ccbh->flags & CAM_POLLED) == 0) {
1154 * Use NCQ - always uses 48 bit addressing
1156 xa->flags |= ATA_F_NCQ;
1157 fis->command = (xa->flags & ATA_F_WRITE) ?
1158 ATA_C_WRITE_FPDMA : ATA_C_READ_FPDMA;
1159 fis->lba_low_exp = (u_int8_t)(lba >> 24);
1160 fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1161 fis->lba_high_exp = (u_int8_t)(lba >> 40);
1162 fis->sector_count = xa->tag << 3;
1163 fis->features = (u_int8_t)count;
1164 fis->features_exp = (u_int8_t)(count >> 8);
1165 } else if (count > 0x100 || lba > 0xFFFFFFFFU) {
1167 * Use LBA48
1169 fis->command = (xa->flags & ATA_F_WRITE) ?
1170 ATA_C_WRITEDMA_EXT : ATA_C_READDMA_EXT;
1171 fis->lba_low_exp = (u_int8_t)(lba >> 24);
1172 fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1173 fis->lba_high_exp = (u_int8_t)(lba >> 40);
1174 fis->sector_count = (u_int8_t)count;
1175 fis->sector_count_exp = (u_int8_t)(count >> 8);
1176 } else {
1178 * Use LBA
1180 * NOTE: 256 sectors is supported, stored as 0.
1182 fis->command = (xa->flags & ATA_F_WRITE) ?
1183 ATA_C_WRITEDMA : ATA_C_READDMA;
1184 fis->device |= (u_int8_t)(lba >> 24) & 0x0F;
1185 fis->sector_count = (u_int8_t)count;
1188 xa->data = csio->data_ptr;
1189 xa->datalen = csio->dxfer_len;
1190 xa->complete = ahci_ata_complete_disk_rw;
1191 xa->timeout = ccbh->timeout; /* milliseconds */
1192 #if 0
1193 if (xa->timeout > 10000) /* XXX - debug */
1194 xa->timeout = 10000;
1195 #endif
1196 if (ccbh->flags & CAM_POLLED)
1197 xa->flags |= ATA_F_POLL;
1198 break;
1202 * If the request is still in progress the xa and FIS have
1203 * been set up (except for the PM target), and must be dispatched.
1204 * Otherwise the request was completed.
1206 if (ccbh->status == CAM_REQ_INPROG) {
1207 KKASSERT(xa->complete != NULL);
1208 xa->atascsi_private = ccb;
1209 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
1210 ahci_os_lock_port(ap);
1211 xa->fis->flags |= at->at_target;
1212 ahci_ata_cmd(xa);
1213 ahci_os_unlock_port(ap);
1214 } else {
1215 ahci_ata_put_xfer(xa);
1216 xpt_done(ccb);
1221 * Convert the SCSI command in ccb to an ata_xfer command in xa
1222 * for ATA_PORT_T_ATAPI operations. Set the completion function
1223 * to convert the response back, then dispatch to the OpenBSD AHCI
1224 * layer.
1226 static
1227 void
1228 ahci_xpt_scsi_atapi_io(struct ahci_port *ap, struct ata_port *atx,
1229 union ccb *ccb)
1231 struct ccb_hdr *ccbh;
1232 struct ccb_scsiio *csio;
1233 struct ata_xfer *xa;
1234 struct ata_fis_h2d *fis;
1235 scsi_cdb_t cdbs;
1236 scsi_cdb_t cdbd;
1237 int flags;
1238 struct ata_port *at;
1240 ccbh = &ccb->csio.ccb_h;
1241 csio = &ccb->csio;
1242 at = atx ? atx : &ap->ap_ata[0];
1244 switch (ccbh->flags & CAM_DIR_MASK) {
1245 case CAM_DIR_IN:
1246 flags = ATA_F_PACKET | ATA_F_READ;
1247 break;
1248 case CAM_DIR_OUT:
1249 flags = ATA_F_PACKET | ATA_F_WRITE;
1250 break;
1251 case CAM_DIR_NONE:
1252 flags = ATA_F_PACKET;
1253 break;
1254 default:
1255 ccbh->status = CAM_REQ_INVALID;
1256 xpt_done(ccb);
1257 return;
1258 /* NOT REACHED */
1262 * Special handling to get the rfis back into host memory while
1263 * still allowing the Sili chip to run commands in parallel to
1264 * ATAPI devices behind a PM.
1266 flags |= ATA_F_AUTOSENSE;
1269 * The command has to fit in the packet command buffer.
1271 if (csio->cdb_len < 6 || csio->cdb_len > 16) {
1272 ccbh->status = CAM_CCB_LEN_ERR;
1273 xpt_done(ccb);
1274 return;
1278 * Initialize the XA and FIS.
1280 * XXX not passing NULL at for direct attach!
1282 xa = ahci_ata_get_xfer(ap, atx);
1283 fis = xa->fis;
1285 fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
1286 fis->command = ATA_C_PACKET;
1287 fis->device = 0;
1288 fis->sector_count = xa->tag << 3;
1289 fis->features = ATA_H2D_FEATURES_DMA |
1290 ((flags & ATA_F_WRITE) ?
1291 ATA_H2D_FEATURES_DIR_WRITE : ATA_H2D_FEATURES_DIR_READ);
1292 fis->lba_mid = 0x00;
1293 fis->lba_high = 0x20;
1295 xa->flags = flags;
1296 xa->data = csio->data_ptr;
1297 xa->datalen = csio->dxfer_len;
1298 xa->timeout = ccbh->timeout; /* milliseconds */
1300 if (ccbh->flags & CAM_POLLED)
1301 xa->flags |= ATA_F_POLL;
1304 * Copy the cdb to the packetcmd buffer in the FIS using a
1305 * convenient pointer in the xa.
1307 cdbs = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
1308 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
1309 bcopy(cdbs, xa->packetcmd, csio->cdb_len);
1311 #if 0
1312 kprintf("opcode %d cdb_len %d dxfer_len %d\n",
1313 cdbs->generic.opcode,
1314 csio->cdb_len, csio->dxfer_len);
1315 #endif
1318 * Some ATAPI commands do not actually follow the SCSI standard.
1320 cdbd = (void *)xa->packetcmd;
1322 switch(cdbd->generic.opcode) {
1323 case INQUIRY:
1325 * Some ATAPI devices can't handle long inquiry lengths,
1326 * don't ask me why. Truncate the inquiry length.
1328 if (cdbd->inquiry.page_code == 0 &&
1329 cdbd->inquiry.length > SHORT_INQUIRY_LENGTH) {
1330 cdbd->inquiry.length = SHORT_INQUIRY_LENGTH;
1332 break;
1333 case READ_6:
1334 case WRITE_6:
1336 * Convert *_6 to *_10 commands. Most ATAPI devices
1337 * cannot handle the SCSI READ_6 and WRITE_6 commands.
1339 cdbd->rw_10.opcode |= 0x20;
1340 cdbd->rw_10.byte2 = 0;
1341 cdbd->rw_10.addr[0] = cdbs->rw_6.addr[0] & 0x1F;
1342 cdbd->rw_10.addr[1] = cdbs->rw_6.addr[1];
1343 cdbd->rw_10.addr[2] = cdbs->rw_6.addr[2];
1344 cdbd->rw_10.addr[3] = 0;
1345 cdbd->rw_10.reserved = 0;
1346 cdbd->rw_10.length[0] = 0;
1347 cdbd->rw_10.length[1] = cdbs->rw_6.length;
1348 cdbd->rw_10.control = cdbs->rw_6.control;
1349 break;
1350 default:
1351 break;
1355 * And dispatch
1357 xa->complete = ahci_atapi_complete_cmd;
1358 xa->atascsi_private = ccb;
1359 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
1360 ahci_os_lock_port(ap);
1361 ahci_ata_cmd(xa);
1362 ahci_os_unlock_port(ap);
1366 * Simulate page inquiries for disk attachments.
1368 static
1369 void
1370 ahci_xpt_page_inquiry(struct ahci_port *ap, struct ata_port *at, union ccb *ccb)
1372 union {
1373 struct scsi_vpd_supported_page_list list;
1374 struct scsi_vpd_unit_serial_number serno;
1375 struct scsi_vpd_unit_devid devid;
1376 char buf[256];
1377 } *page;
1378 scsi_cdb_t cdb;
1379 int i;
1380 int j;
1381 int len;
1383 page = kmalloc(sizeof(*page), M_DEVBUF, M_WAITOK | M_ZERO);
1385 cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1386 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes);
1388 switch(cdb->inquiry.page_code) {
1389 case SVPD_SUPPORTED_PAGE_LIST:
1390 i = 0;
1391 page->list.device = T_DIRECT;
1392 page->list.page_code = SVPD_SUPPORTED_PAGE_LIST;
1393 page->list.list[i++] = SVPD_SUPPORTED_PAGE_LIST;
1394 page->list.list[i++] = SVPD_UNIT_SERIAL_NUMBER;
1395 page->list.list[i++] = SVPD_UNIT_DEVID;
1396 page->list.length = i;
1397 len = offsetof(struct scsi_vpd_supported_page_list, list[3]);
1398 break;
1399 case SVPD_UNIT_SERIAL_NUMBER:
1400 i = 0;
1401 j = sizeof(at->at_identify.serial);
1402 for (i = 0; i < j && at->at_identify.serial[i] == ' '; ++i)
1404 while (j > i && at->at_identify.serial[j-1] == ' ')
1405 --j;
1406 page->serno.device = T_DIRECT;
1407 page->serno.page_code = SVPD_UNIT_SERIAL_NUMBER;
1408 page->serno.length = j - i;
1409 bcopy(at->at_identify.serial + i,
1410 page->serno.serial_num, j - i);
1411 len = offsetof(struct scsi_vpd_unit_serial_number,
1412 serial_num[j-i]);
1413 break;
1414 case SVPD_UNIT_DEVID:
1415 /* fall through for now */
1416 default:
1417 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
1418 len = 0;
1419 break;
1421 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
1422 if (len <= ccb->csio.dxfer_len) {
1423 ccb->ccb_h.status = CAM_REQ_CMP;
1424 bzero(ccb->csio.data_ptr, ccb->csio.dxfer_len);
1425 bcopy(page, ccb->csio.data_ptr, len);
1426 ccb->csio.resid = ccb->csio.dxfer_len - len;
1427 } else {
1428 ccb->ccb_h.status = CAM_CCB_LEN_ERR;
1431 kfree(page, M_DEVBUF);
1435 * Completion function for ATA_PORT_T_DISK cache synchronization.
1437 static
1438 void
1439 ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa)
1441 union ccb *ccb = xa->atascsi_private;
1442 struct ccb_hdr *ccbh = &ccb->ccb_h;
1443 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1445 switch(xa->state) {
1446 case ATA_S_COMPLETE:
1447 ccbh->status = CAM_REQ_CMP;
1448 ccb->csio.scsi_status = SCSI_STATUS_OK;
1449 break;
1450 case ATA_S_ERROR:
1451 kprintf("%s: synchronize_cache: error\n",
1452 ATANAME(ap, xa->at));
1453 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1454 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1455 ahci_ata_dummy_sense(&ccb->csio.sense_data);
1456 break;
1457 case ATA_S_TIMEOUT:
1458 kprintf("%s: synchronize_cache: timeout\n",
1459 ATANAME(ap, xa->at));
1460 ccbh->status = CAM_CMD_TIMEOUT;
1461 break;
1462 default:
1463 kprintf("%s: synchronize_cache: unknown state %d\n",
1464 ATANAME(ap, xa->at), xa->state);
1465 ccbh->status = CAM_REQ_CMP_ERR;
1466 break;
1468 ahci_ata_put_xfer(xa);
1469 ahci_os_unlock_port(ap);
1470 xpt_done(ccb);
1471 ahci_os_lock_port(ap);
1475 * Completion function for ATA_PORT_T_DISK I/O
1477 static
1478 void
1479 ahci_ata_complete_disk_rw(struct ata_xfer *xa)
1481 union ccb *ccb = xa->atascsi_private;
1482 struct ccb_hdr *ccbh = &ccb->ccb_h;
1483 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1485 switch(xa->state) {
1486 case ATA_S_COMPLETE:
1487 ccbh->status = CAM_REQ_CMP;
1488 ccb->csio.scsi_status = SCSI_STATUS_OK;
1489 break;
1490 case ATA_S_ERROR:
1491 kprintf("%s: disk_rw: error\n", ATANAME(ap, xa->at));
1492 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1493 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1494 ahci_ata_dummy_sense(&ccb->csio.sense_data);
1495 break;
1496 case ATA_S_TIMEOUT:
1497 kprintf("%s: disk_rw: timeout\n", ATANAME(ap, xa->at));
1498 ccbh->status = CAM_CMD_TIMEOUT;
1499 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1500 ahci_ata_dummy_sense(&ccb->csio.sense_data);
1501 break;
1502 default:
1503 kprintf("%s: disk_rw: unknown state %d\n",
1504 ATANAME(ap, xa->at), xa->state);
1505 ccbh->status = CAM_REQ_CMP_ERR;
1506 break;
1508 ccb->csio.resid = xa->resid;
1509 ahci_ata_put_xfer(xa);
1510 ahci_os_unlock_port(ap);
1511 xpt_done(ccb);
1512 ahci_os_lock_port(ap);
1516 * Completion function for ATA_PORT_T_ATAPI I/O
1518 * Sense data is returned in the rfis.
1520 static
1521 void
1522 ahci_atapi_complete_cmd(struct ata_xfer *xa)
1524 union ccb *ccb = xa->atascsi_private;
1525 struct ccb_hdr *ccbh = &ccb->ccb_h;
1526 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1527 scsi_cdb_t cdb;
1529 cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1530 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes);
1532 switch(xa->state) {
1533 case ATA_S_COMPLETE:
1534 ccbh->status = CAM_REQ_CMP;
1535 ccb->csio.scsi_status = SCSI_STATUS_OK;
1536 break;
1537 case ATA_S_ERROR:
1538 ccbh->status = CAM_SCSI_STATUS_ERROR;
1539 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1540 ahci_ata_atapi_sense(&xa->rfis, &ccb->csio.sense_data);
1541 break;
1542 case ATA_S_TIMEOUT:
1543 kprintf("%s: cmd %d: timeout\n",
1544 PORTNAME(ap), cdb->generic.opcode);
1545 ccbh->status = CAM_CMD_TIMEOUT;
1546 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1547 ahci_ata_dummy_sense(&ccb->csio.sense_data);
1548 break;
1549 default:
1550 kprintf("%s: cmd %d: unknown state %d\n",
1551 PORTNAME(ap), cdb->generic.opcode, xa->state);
1552 ccbh->status = CAM_REQ_CMP_ERR;
1553 break;
1555 ccb->csio.resid = xa->resid;
1556 ahci_ata_put_xfer(xa);
1557 ahci_os_unlock_port(ap);
1558 xpt_done(ccb);
1559 ahci_os_lock_port(ap);
1563 * Construct dummy sense data for errors on DISKs
1565 static
1566 void
1567 ahci_ata_dummy_sense(struct scsi_sense_data *sense_data)
1569 sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
1570 sense_data->segment = 0;
1571 sense_data->flags = SSD_KEY_MEDIUM_ERROR;
1572 sense_data->info[0] = 0;
1573 sense_data->info[1] = 0;
1574 sense_data->info[2] = 0;
1575 sense_data->info[3] = 0;
1576 sense_data->extra_len = 0;
1580 * Construct atapi sense data for errors on ATAPI
1582 * The ATAPI sense data is stored in the passed rfis and must be converted
1583 * to SCSI sense data.
1585 static
1586 void
1587 ahci_ata_atapi_sense(struct ata_fis_d2h *rfis,
1588 struct scsi_sense_data *sense_data)
1590 sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
1591 sense_data->segment = 0;
1592 sense_data->flags = (rfis->error & 0xF0) >> 4;
1593 if (rfis->error & 0x04)
1594 sense_data->flags |= SSD_KEY_ILLEGAL_REQUEST;
1595 if (rfis->error & 0x02)
1596 sense_data->flags |= SSD_EOM;
1597 if (rfis->error & 0x01)
1598 sense_data->flags |= SSD_ILI;
1599 sense_data->info[0] = 0;
1600 sense_data->info[1] = 0;
1601 sense_data->info[2] = 0;
1602 sense_data->info[3] = 0;
1603 sense_data->extra_len = 0;
1606 static
1607 void
1608 ahci_strip_string(const char **basep, int *lenp)
1610 const char *base = *basep;
1611 int len = *lenp;
1613 while (len && (*base == 0 || *base == ' ')) {
1614 --len;
1615 ++base;
1617 while (len && (base[len-1] == 0 || base[len-1] == ' '))
1618 --len;
1619 *basep = base;
1620 *lenp = len;