AHCI - Port multiplier plug/unplug work, tuning, cleanup, bug fixes.
[dragonfly.git] / sys / dev / disk / ahci / ahci_cam.c
CommitLineData
258223a3
MD
1/*
2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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.
20 *
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.
33 *
34 *
35 * Copyright (c) 2007 David Gwynne <dlg@openbsd.org>
36 *
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.
40 *
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.
48 *
49 * $OpenBSD: atascsi.c,v 1.64 2009/02/16 21:19:06 miod Exp $
50 * $DragonFly$
51 */
52/*
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.
56 *
57 * Much of the cdb<->xa conversion code was taken from OpenBSD, the rest
58 * was written natively for DragonFly.
59 */
60
61#include "ahci.h"
62
b4189e5e
MD
63const 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"
96};
97
258223a3
MD
98static void ahci_xpt_action(struct cam_sim *sim, union ccb *ccb);
99static void ahci_xpt_poll(struct cam_sim *sim);
1980eff3
MD
100static void ahci_xpt_scsi_disk_io(struct ahci_port *ap,
101 struct ata_port *at, union ccb *ccb);
102static void ahci_xpt_scsi_atapi_io(struct ahci_port *ap,
103 struct ata_port *at, union ccb *ccb);
258223a3
MD
104
105static void ahci_ata_complete_disk_rw(struct ata_xfer *xa);
106static void ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa);
b4189e5e
MD
107static void ahci_atapi_complete_cmd(struct ata_xfer *xa);
108static void ahci_ata_dummy_sense(struct scsi_sense_data *sense_data);
7d4fcf34
MD
109static void ahci_ata_atapi_sense(struct ata_fis_d2h *rfis,
110 struct scsi_sense_data *sense_data);
258223a3 111
1980eff3
MD
112static int ahci_cam_probe_disk(struct ahci_port *ap, struct ata_port *at);
113static int ahci_cam_probe_atapi(struct ahci_port *ap, struct ata_port *at);
b4189e5e 114static void ahci_ata_dummy_done(struct ata_xfer *xa);
258223a3
MD
115static void ata_fix_identify(struct ata_identify *id);
116static void ahci_cam_rescan(struct ahci_port *ap);
117
118int
119ahci_cam_attach(struct ahci_port *ap)
120{
121 struct cam_devq *devq;
122 struct cam_sim *sim;
123 int error;
124 int unit;
125
cec85a37
MD
126 /*
127 * We want at least one ccb to be available for error processing
128 * so don't let CAM use more then ncmds - 1.
129 */
258223a3 130 unit = device_get_unit(ap->ap_sc->sc_dev);
cec85a37
MD
131 if (ap->ap_sc->sc_ncmds > 1)
132 devq = cam_simq_alloc(ap->ap_sc->sc_ncmds - 1);
133 else
134 devq = cam_simq_alloc(ap->ap_sc->sc_ncmds);
258223a3
MD
135 if (devq == NULL) {
136 return (ENOMEM);
137 }
138 sim = cam_sim_alloc(ahci_xpt_action, ahci_xpt_poll, "ahci",
139 (void *)ap, unit, &sim_mplock, 1, 1, devq);
140 cam_simq_release(devq);
141 if (sim == NULL) {
142 return (ENOMEM);
143 }
144 ap->ap_sim = sim;
831bc9e3 145 ahci_os_unlock_port(ap);
258223a3 146 error = xpt_bus_register(ap->ap_sim, ap->ap_num);
831bc9e3 147 ahci_os_lock_port(ap);
258223a3
MD
148 if (error != CAM_SUCCESS) {
149 ahci_cam_detach(ap);
150 return (EINVAL);
151 }
152 ap->ap_flags |= AP_F_BUS_REGISTERED;
258223a3 153
1980eff3 154 error = ahci_cam_probe(ap, NULL);
258223a3
MD
155 if (error) {
156 ahci_cam_detach(ap);
157 return (EIO);
158 }
159 ap->ap_flags |= AP_F_CAM_ATTACHED;
160
258223a3
MD
161 return(0);
162}
163
1980eff3 164/*
3209f581
MD
165 * The state of the port has changed.
166 *
167 * If at is NULL the physical port has changed state.
168 * If at is non-NULL a particular target behind a PM has changed state.
169 *
170 * If found is -1 the target state must be queued to a non-interrupt context.
171 * (only works with at == NULL).
172 *
173 * If found is 0 the target was removed.
174 * If found is 1 the target was inserted.
1980eff3 175 */
258223a3 176void
3209f581 177ahci_cam_changed(struct ahci_port *ap, struct ata_port *atx, int found)
258223a3 178{
fd8bd957 179 struct cam_path *tmppath;
3209f581
MD
180 int status;
181 int target;
182
183 target = atx ? atx->at_target : CAM_TARGET_WILDCARD;
fd8bd957 184
258223a3
MD
185 if (ap->ap_sim == NULL)
186 return;
3209f581
MD
187 if (found == CAM_TARGET_WILDCARD) {
188 status = xpt_create_path(&tmppath, NULL,
189 cam_sim_path(ap->ap_sim),
190 target, CAM_LUN_WILDCARD);
191 if (status != CAM_REQ_CMP)
192 return;
fd8bd957
MD
193 ahci_cam_rescan(ap);
194 } else {
3209f581
MD
195 status = xpt_create_path(&tmppath, NULL,
196 cam_sim_path(ap->ap_sim),
197 target,
198 CAM_LUN_WILDCARD);
199 if (status != CAM_REQ_CMP)
200 return;
201#if 0
202 /*
203 * This confuses CAM
204 */
205 if (found)
206 xpt_async(AC_FOUND_DEVICE, tmppath, NULL);
207 else
208 xpt_async(AC_LOST_DEVICE, tmppath, NULL);
209#endif
fd8bd957
MD
210 }
211 xpt_free_path(tmppath);
258223a3
MD
212}
213
214void
215ahci_cam_detach(struct ahci_port *ap)
216{
217 int error;
218
219 if ((ap->ap_flags & AP_F_CAM_ATTACHED) == 0)
220 return;
221 get_mplock();
222 if (ap->ap_sim) {
223 xpt_freeze_simq(ap->ap_sim, 1);
224 }
258223a3
MD
225 if (ap->ap_flags & AP_F_BUS_REGISTERED) {
226 error = xpt_bus_deregister(cam_sim_path(ap->ap_sim));
227 KKASSERT(error == CAM_REQ_CMP);
228 ap->ap_flags &= ~AP_F_BUS_REGISTERED;
229 }
230 if (ap->ap_sim) {
231 cam_sim_free(ap->ap_sim);
232 ap->ap_sim = NULL;
233 }
234 rel_mplock();
235 ap->ap_flags &= ~AP_F_CAM_ATTACHED;
236}
237
238/*
1980eff3 239 * Once the AHCI port has been attached we need to probe for a device or
258223a3 240 * devices on the port and setup various options.
1980eff3
MD
241 *
242 * If at is NULL we are probing the direct-attached device on the port,
243 * which may or may not be a port multiplier.
258223a3 244 */
3209f581 245int
1980eff3 246ahci_cam_probe(struct ahci_port *ap, struct ata_port *atx)
258223a3 247{
1980eff3 248 struct ata_port *at;
258223a3 249 struct ata_xfer *xa;
258223a3
MD
250 u_int64_t capacity;
251 u_int64_t capacity_bytes;
252 int model_len;
fd8bd957 253 int error;
258223a3
MD
254 int devncqdepth;
255 int i;
669fbbf7
MD
256 const char *wcstr;
257 const char *rastr;
fd8bd957
MD
258 const char *scstr;
259 const char *type;
260
3209f581 261 error = EIO;
1980eff3
MD
262
263 /*
f4553de1
MD
264 * Delayed CAM attachment for initial probe, sim may be NULL
265 */
266 if (ap->ap_sim == NULL)
267 return(0);
268
269 /*
1980eff3
MD
270 * A NULL atx indicates a probe of the directly connected device.
271 * A non-NULL atx indicates a device connected via a port multiplier.
272 * We need to preserve atx for calls to ahci_ata_get_xfer().
273 *
274 * at is always non-NULL. For directly connected devices we supply
275 * an (at) pointing to target 0.
276 */
277 if (atx == NULL) {
3209f581 278 at = ap->ap_ata; /* direct attached - device 0 */
1980eff3 279 if (ap->ap_type == ATA_PORT_T_PM) {
831bc9e3
MD
280 kprintf("%s: Found Port Multiplier\n",
281 ATANAME(ap, atx));
1980eff3
MD
282 return (0);
283 }
1980eff3
MD
284 at->at_type = ap->ap_type;
285 } else {
3209f581 286 at = atx;
1980eff3
MD
287 if (atx->at_type == ATA_PORT_T_PM) {
288 kprintf("%s: Bogus device, reducing port count to %d\n",
289 ATANAME(ap, atx), atx->at_target);
290 if (ap->ap_pmcount > atx->at_target)
291 ap->ap_pmcount = atx->at_target;
3209f581 292 goto err;
1980eff3 293 }
1980eff3 294 }
3209f581
MD
295 if (ap->ap_type == ATA_PORT_T_NONE)
296 goto err;
1980eff3 297 if (at->at_type == ATA_PORT_T_NONE)
3209f581 298 goto err;
258223a3 299
258223a3
MD
300 /*
301 * Issue identify, saving the result
302 */
1980eff3 303 xa = ahci_ata_get_xfer(ap, atx);
258223a3 304 xa->complete = ahci_ata_dummy_done;
1980eff3
MD
305 xa->data = &at->at_identify;
306 xa->datalen = sizeof(at->at_identify);
307 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
308
309 switch(at->at_type) {
310 case ATA_PORT_T_DISK:
fd8bd957
MD
311 xa->fis->command = ATA_C_IDENTIFY;
312 type = "DISK";
1980eff3
MD
313 break;
314 case ATA_PORT_T_ATAPI:
315 xa->fis->command = ATA_C_ATAPI_IDENTIFY;
316 type = "ATAPI";
317 break;
318 default:
319 xa->fis->command = ATA_C_ATAPI_IDENTIFY;
320 type = "UNKNOWN(ATAPI?)";
321 break;
fd8bd957 322 }
258223a3
MD
323 xa->fis->features = 0;
324 xa->fis->device = 0;
325 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
3209f581 326 xa->timeout = 1000;
258223a3 327
831bc9e3 328 if (ahci_ata_cmd(xa) != ATA_S_COMPLETE) {
fd8bd957 329 kprintf("%s: Detected %s device but unable to IDENTIFY\n",
1980eff3 330 ATANAME(ap, atx), type);
258223a3 331 ahci_ata_put_xfer(xa);
3209f581 332 goto err;
258223a3 333 }
258223a3
MD
334 ahci_ata_put_xfer(xa);
335
1980eff3 336 ata_fix_identify(&at->at_identify);
258223a3
MD
337
338 /*
339 * Read capacity using SATA probe info.
340 */
1980eff3 341 if (le16toh(at->at_identify.cmdset83) & 0x0400) {
258223a3
MD
342 /* LBA48 feature set supported */
343 capacity = 0;
344 for (i = 3; i >= 0; --i) {
345 capacity <<= 16;
346 capacity +=
1980eff3 347 le16toh(at->at_identify.addrsecxt[i]);
258223a3
MD
348 }
349 } else {
1980eff3 350 capacity = le16toh(at->at_identify.addrsec[1]);
258223a3 351 capacity <<= 16;
1980eff3 352 capacity += le16toh(at->at_identify.addrsec[0]);
258223a3 353 }
1980eff3
MD
354 at->at_capacity = capacity;
355 if (atx == NULL)
356 ap->ap_probe = ATA_PROBE_GOOD;
258223a3
MD
357
358 capacity_bytes = capacity * 512;
359
360 /*
361 * Negotiate NCQ, throw away any ata_xfer's beyond the negotiated
362 * number of slots and limit the number of CAM ccb's to one less
363 * so we always have a slot available for recovery.
364 *
365 * NCQ is not used if ap_ncqdepth is 1 or the host controller does
366 * not support it, and in that case the driver can handle extra
367 * ccb's.
cec85a37 368 *
1980eff3
MD
369 * NCQ is currently used only with direct-attached disks. It is
370 * not used with port multipliers or direct-attached ATAPI devices.
371 *
cec85a37
MD
372 * Remember at least one extra CCB needs to be reserved for the
373 * error ccb.
258223a3
MD
374 */
375 if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) &&
1980eff3
MD
376 ap->ap_type == ATA_PORT_T_DISK &&
377 (le16toh(at->at_identify.satacap) & (1 << 8))) {
378 at->at_ncqdepth = (le16toh(at->at_identify.qdepth) & 0x1F) + 1;
379 devncqdepth = at->at_ncqdepth;
380 if (at->at_ncqdepth > ap->ap_sc->sc_ncmds)
381 at->at_ncqdepth = ap->ap_sc->sc_ncmds;
382 if (at->at_ncqdepth > 1) {
cec85a37 383 for (i = 0; i < ap->ap_sc->sc_ncmds; ++i) {
1980eff3
MD
384 xa = ahci_ata_get_xfer(ap, atx);
385 if (xa->tag < at->at_ncqdepth) {
cec85a37
MD
386 xa->state = ATA_S_COMPLETE;
387 ahci_ata_put_xfer(xa);
388 }
389 }
1980eff3 390 if (at->at_ncqdepth >= ap->ap_sc->sc_ncmds) {
cec85a37 391 cam_devq_resize(ap->ap_sim->devq,
1980eff3 392 at->at_ncqdepth - 1);
258223a3 393 }
258223a3
MD
394 }
395 } else {
396 devncqdepth = 0;
397 }
398
fd8bd957
MD
399 /*
400 * Make the model string a bit more presentable
401 */
258223a3 402 for (model_len = 40; model_len; --model_len) {
1980eff3 403 if (at->at_identify.model[model_len-1] == ' ')
258223a3 404 continue;
1980eff3 405 if (at->at_identify.model[model_len-1] == 0)
258223a3
MD
406 continue;
407 break;
408 }
669fbbf7 409
fd8bd957
MD
410 /*
411 * Generate informatiive strings.
412 *
413 * NOTE: We do not automatically set write caching, lookahead,
414 * or the security state for ATAPI devices.
415 */
1980eff3
MD
416 if (at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) {
417 if (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE)
669fbbf7 418 wcstr = "enabled";
1980eff3 419 else if (at->at_type == ATA_PORT_T_ATAPI)
fd8bd957 420 wcstr = "disabled";
669fbbf7
MD
421 else
422 wcstr = "enabling";
423 } else {
424 wcstr = "notsupp";
425 }
426
1980eff3
MD
427 if (at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) {
428 if (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD)
669fbbf7 429 rastr = "enabled";
1980eff3 430 else if (at->at_type == ATA_PORT_T_ATAPI)
fd8bd957 431 rastr = "disabled";
669fbbf7
MD
432 else
433 rastr = "enabling";
434 } else {
435 rastr = "notsupp";
436 }
437
1980eff3
MD
438 if (at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) {
439 if (at->at_identify.securestatus & ATA_SECURE_FROZEN)
fd8bd957 440 scstr = "frozen";
1980eff3 441 else if (at->at_type == ATA_PORT_T_ATAPI)
fd8bd957
MD
442 scstr = "unfrozen";
443 else
444 scstr = "freezing";
445 } else {
446 scstr = "notsupp";
447 }
448
449 kprintf("%s: Found %s \"%*.*s %8.8s\" serial=\"%20.20s\"\n"
258223a3
MD
450 "%s: tags=%d/%d satacaps=%04x satafeat=%04x "
451 "capacity=%lld.%02dMB\n"
fd8bd957 452 "%s: f85=%04x f86=%04x f87=%04x WC=%s RA=%s SEC=%s\n",
1980eff3 453 ATANAME(ap, atx),
fd8bd957 454 type,
258223a3 455 model_len, model_len,
1980eff3
MD
456 at->at_identify.model,
457 at->at_identify.firmware,
458 at->at_identify.serial,
258223a3 459
1980eff3 460 ATANAME(ap, atx),
258223a3 461 devncqdepth, ap->ap_sc->sc_ncmds,
1980eff3
MD
462 at->at_identify.satacap,
463 at->at_identify.satafsup,
258223a3
MD
464 (long long)capacity_bytes / (1024 * 1024),
465 (int)(capacity_bytes % (1024 * 1024)) * 100 / (1024 * 1024),
466
1980eff3
MD
467 ATANAME(ap, atx),
468 at->at_identify.features85,
469 at->at_identify.features86,
470 at->at_identify.features87,
669fbbf7 471 wcstr,
fd8bd957
MD
472 rastr,
473 scstr
258223a3
MD
474 );
475
476 /*
fd8bd957
MD
477 * Additional type-specific probing
478 */
1980eff3 479 switch(at->at_type) {
fd8bd957 480 case ATA_PORT_T_DISK:
1980eff3
MD
481 error = ahci_cam_probe_disk(ap, atx);
482 break;
483 case ATA_PORT_T_ATAPI:
484 error = ahci_cam_probe_atapi(ap, atx);
fd8bd957
MD
485 break;
486 default:
1980eff3 487 error = EIO;
fd8bd957
MD
488 break;
489 }
3209f581
MD
490err:
491 if (error) {
492 at->at_probe = ATA_PROBE_FAILED;
493 if (atx == NULL)
494 ap->ap_probe = at->at_probe;
495 } else {
496 at->at_probe = ATA_PROBE_GOOD;
497 if (atx == NULL)
498 ap->ap_probe = at->at_probe;
499 }
500 return (error);
fd8bd957
MD
501}
502
503/*
504 * DISK-specific probe after initial ident
505 */
506static int
1980eff3 507ahci_cam_probe_disk(struct ahci_port *ap, struct ata_port *atx)
fd8bd957 508{
1980eff3 509 struct ata_port *at;
fd8bd957 510 struct ata_xfer *xa;
fd8bd957 511
1980eff3
MD
512 at = atx ? atx : ap->ap_ata;
513
fd8bd957 514 /*
258223a3 515 * Enable write cache if supported
fd8bd957
MD
516 *
517 * NOTE: "WD My Book" external disk devices have a very poor
518 * daughter board between the the ESATA and the HD. Sending
519 * any ATA_C_SET_FEATURES commands will break the hardware port
520 * with a fatal protocol error. However, this device also
521 * indicates that WRITECACHE is already on and READAHEAD is
522 * not supported so we avoid the issue.
258223a3 523 */
1980eff3
MD
524 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_WRITECACHE) &&
525 (at->at_identify.features85 & ATA_IDENTIFY_WRITECACHE) == 0) {
526 xa = ahci_ata_get_xfer(ap, atx);
258223a3
MD
527 xa->complete = ahci_ata_dummy_done;
528 xa->fis->command = ATA_C_SET_FEATURES;
669fbbf7
MD
529 /*xa->fis->features = ATA_SF_WRITECACHE_EN;*/
530 xa->fis->features = ATA_SF_LOOKAHEAD_EN;
1980eff3 531 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
669fbbf7 532 xa->fis->device = 0;
258223a3 533 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
3209f581 534 xa->timeout = 1000;
669fbbf7 535 xa->datalen = 0;
831bc9e3 536 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
1980eff3 537 at->at_features |= ATA_PORT_F_WCACHE;
258223a3
MD
538 ahci_ata_put_xfer(xa);
539 }
540
541 /*
542 * Enable readahead if supported
543 */
1980eff3
MD
544 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_LOOKAHEAD) &&
545 (at->at_identify.features85 & ATA_IDENTIFY_LOOKAHEAD) == 0) {
546 xa = ahci_ata_get_xfer(ap, atx);
258223a3
MD
547 xa->complete = ahci_ata_dummy_done;
548 xa->fis->command = ATA_C_SET_FEATURES;
549 xa->fis->features = ATA_SF_LOOKAHEAD_EN;
1980eff3 550 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
669fbbf7 551 xa->fis->device = 0;
258223a3 552 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
3209f581 553 xa->timeout = 1000;
669fbbf7 554 xa->datalen = 0;
831bc9e3 555 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
1980eff3 556 at->at_features |= ATA_PORT_F_RAHEAD;
258223a3
MD
557 ahci_ata_put_xfer(xa);
558 }
559
560 /*
561 * FREEZE LOCK the device so malicious users can't lock it on us.
562 * As there is no harm in issuing this to devices that don't
563 * support the security feature set we just send it, and don't bother
564 * checking if the device sends a command abort to tell us it doesn't
565 * support it
566 */
1980eff3
MD
567 if ((at->at_identify.cmdset82 & ATA_IDENTIFY_SECURITY) &&
568 (at->at_identify.securestatus & ATA_SECURE_FROZEN) == 0) {
569 xa = ahci_ata_get_xfer(ap, atx);
669fbbf7
MD
570 xa->complete = ahci_ata_dummy_done;
571 xa->fis->command = ATA_C_SEC_FREEZE_LOCK;
1980eff3 572 xa->fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
669fbbf7 573 xa->flags = ATA_F_READ | ATA_F_PIO | ATA_F_POLL;
3209f581 574 xa->timeout = 1000;
669fbbf7 575 xa->datalen = 0;
831bc9e3 576 if (ahci_ata_cmd(xa) == ATA_S_COMPLETE)
1980eff3 577 at->at_features |= ATA_PORT_F_FRZLCK;
669fbbf7
MD
578 ahci_ata_put_xfer(xa);
579 }
258223a3 580
b4189e5e
MD
581 return (0);
582}
583
fd8bd957
MD
584/*
585 * ATAPI-specific probe after initial ident
586 */
b4189e5e 587static int
1980eff3 588ahci_cam_probe_atapi(struct ahci_port *ap, struct ata_port *atx)
b4189e5e 589{
fd8bd957
MD
590 return(0);
591}
592
b4189e5e
MD
593/*
594 * Fix byte ordering so buffers can be accessed as
595 * strings.
596 */
258223a3
MD
597static void
598ata_fix_identify(struct ata_identify *id)
599{
600 u_int16_t *swap;
601 int i;
602
603 swap = (u_int16_t *)id->serial;
604 for (i = 0; i < sizeof(id->serial) / sizeof(u_int16_t); i++)
605 swap[i] = bswap16(swap[i]);
606
607 swap = (u_int16_t *)id->firmware;
608 for (i = 0; i < sizeof(id->firmware) / sizeof(u_int16_t); i++)
609 swap[i] = bswap16(swap[i]);
610
611 swap = (u_int16_t *)id->model;
612 for (i = 0; i < sizeof(id->model) / sizeof(u_int16_t); i++)
613 swap[i] = bswap16(swap[i]);
614}
615
616/*
b4189e5e
MD
617 * Dummy done callback for xa.
618 */
619static void
620ahci_ata_dummy_done(struct ata_xfer *xa)
621{
622}
623
624/*
3209f581
MD
625 * Use an engineering request to initiate a target scan for devices
626 * behind a port multiplier.
fd8bd957 627 *
3209f581 628 * An asynchronous bus scan is used to avoid reentrancy issues.
258223a3
MD
629 */
630static void
631ahci_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
632{
3209f581
MD
633 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
634
f4553de1
MD
635 if (ccb->ccb_h.func_code == XPT_SCAN_BUS) {
636 ap->ap_flags &= ~AP_F_SCAN_RUNNING;
637 if (ap->ap_flags & AP_F_SCAN_REQUESTED) {
638 ap->ap_flags &= ~AP_F_SCAN_REQUESTED;
639 ahci_cam_rescan(ap);
640 }
641 ap->ap_flags |= AP_F_SCAN_COMPLETED;
642 wakeup(&ap->ap_flags);
3209f581 643 }
f4553de1 644 xpt_free_ccb(ccb);
258223a3
MD
645}
646
647static void
648ahci_cam_rescan(struct ahci_port *ap)
649{
650 struct cam_path *path;
651 union ccb *ccb;
652 int status;
3209f581
MD
653 int i;
654
655 if (ap->ap_flags & AP_F_SCAN_RUNNING) {
656 ap->ap_flags |= AP_F_SCAN_REQUESTED;
657 return;
658 }
659 ap->ap_flags |= AP_F_SCAN_RUNNING;
660 for (i = 0; i < AHCI_MAX_PMPORTS; ++i) {
661 ap->ap_ata[i].at_features |= ATA_PORT_F_RESCAN;
662 }
258223a3 663
258223a3
MD
664 status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim),
665 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
666 if (status != CAM_REQ_CMP)
667 return;
668
f4553de1 669 ccb = xpt_alloc_ccb();
258223a3 670 xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */
2de5e9ba 671 ccb->ccb_h.func_code = XPT_ENG_EXEC;
258223a3 672 ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback;
3209f581 673 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
258223a3 674 ccb->crcn.flags = CAM_FLAG_NONE;
2de5e9ba 675 xpt_action_async(ccb);
258223a3
MD
676}
677
3209f581
MD
678static void
679ahci_xpt_rescan(struct ahci_port *ap)
680{
681 struct cam_path *path;
682 union ccb *ccb;
683 int status;
684
685 status = xpt_create_path(&path, xpt_periph, cam_sim_path(ap->ap_sim),
686 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
687 if (status != CAM_REQ_CMP)
688 return;
f4553de1
MD
689
690 ccb = xpt_alloc_ccb();
3209f581 691 xpt_setup_ccb(&ccb->ccb_h, path, 5); /* 5 = low priority */
2de5e9ba 692 ccb->ccb_h.func_code = XPT_SCAN_BUS;
3209f581
MD
693 ccb->ccb_h.cbfcnp = ahci_cam_rescan_callback;
694 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
695 ccb->crcn.flags = CAM_FLAG_NONE;
f4553de1 696 xpt_action(ccb);
3209f581
MD
697}
698
258223a3
MD
699/*
700 * Action function - dispatch command
701 */
702static
703void
704ahci_xpt_action(struct cam_sim *sim, union ccb *ccb)
705{
706 struct ahci_port *ap;
1980eff3 707 struct ata_port *at, *atx;
258223a3
MD
708 struct ccb_hdr *ccbh;
709 int unit;
710
711 /* XXX lock */
712 ap = cam_sim_softc(sim);
1980eff3
MD
713 at = ap->ap_ata;
714 atx = NULL;
258223a3
MD
715 KKASSERT(ap != NULL);
716 ccbh = &ccb->ccb_h;
717 unit = cam_sim_unit(sim);
718
719 /*
3209f581
MD
720 * Early failure checks. These checks do not apply to XPT_PATH_INQ,
721 * otherwise the bus rescan will not remove the dead devices when
722 * unplugging a PM.
723 *
1980eff3
MD
724 * For non-wildcards we have one target (0) and one lun (0),
725 * unless we have a port multiplier.
726 *
727 * A wildcard target indicates only the general bus is being
728 * probed.
729 *
730 * Calculate at and atx. at is always non-NULL. atx is only
731 * non-NULL for direct-attached devices. It will be NULL for
732 * devices behind a port multiplier.
258223a3
MD
733 *
734 * XXX What do we do with a LUN wildcard?
735 */
3209f581
MD
736 if (ccbh->target_id != CAM_TARGET_WILDCARD &&
737 ccbh->func_code != XPT_PATH_INQ) {
1980eff3 738 if (ap->ap_type == ATA_PORT_T_NONE) {
3209f581 739 ccbh->status = CAM_DEV_NOT_THERE;
258223a3
MD
740 xpt_done(ccb);
741 return;
742 }
1980eff3 743 if (ccbh->target_id < 0 || ccbh->target_id >= ap->ap_pmcount) {
258223a3
MD
744 ccbh->status = CAM_DEV_NOT_THERE;
745 xpt_done(ccb);
746 return;
747 }
1980eff3
MD
748 at += ccbh->target_id;
749 if (ap->ap_type == ATA_PORT_T_PM)
750 atx = at;
751
258223a3
MD
752 if (ccbh->target_lun != CAM_LUN_WILDCARD && ccbh->target_lun) {
753 ccbh->status = CAM_DEV_NOT_THERE;
754 xpt_done(ccb);
755 return;
756 }
757 }
758
759 /*
760 * Switch on the meta XPT command
761 */
762 switch(ccbh->func_code) {
3209f581
MD
763 case XPT_ENG_EXEC:
764 /*
765 * This routine is called after a port multiplier has been
766 * probed.
767 */
768 ccbh->status = CAM_REQ_CMP;
f4553de1 769 ahci_os_lock_port(ap);
831bc9e3 770 ahci_port_state_machine(ap, 0);
f4553de1 771 ahci_os_unlock_port(ap);
3209f581 772 xpt_done(ccb);
3209f581
MD
773 ahci_xpt_rescan(ap);
774 break;
258223a3 775 case XPT_PATH_INQ:
3209f581
MD
776 /*
777 * This command always succeeds, otherwise the bus scan
778 * will not detach dead devices.
779 */
258223a3
MD
780 ccb->cpi.version_num = 1;
781 ccb->cpi.hba_inquiry = 0;
782 ccb->cpi.target_sprt = 0;
1980eff3 783 ccb->cpi.hba_misc = PIM_SEQSCAN;
258223a3
MD
784 ccb->cpi.hba_eng_cnt = 0;
785 bzero(ccb->cpi.vuhba_flags, sizeof(ccb->cpi.vuhba_flags));
1980eff3 786 ccb->cpi.max_target = AHCI_MAX_PMPORTS;
258223a3
MD
787 ccb->cpi.max_lun = 0;
788 ccb->cpi.async_flags = 0;
789 ccb->cpi.hpath_id = 0;
1980eff3 790 ccb->cpi.initiator_id = AHCI_MAX_PMPORTS - 1;
258223a3
MD
791 ccb->cpi.unit_number = cam_sim_unit(sim);
792 ccb->cpi.bus_id = cam_sim_bus(sim);
793 ccb->cpi.base_transfer_speed = 150000;
794 ccb->cpi.transport = XPORT_AHCI;
795 ccb->cpi.transport_version = 1;
796 ccb->cpi.protocol = PROTO_SCSI;
797 ccb->cpi.protocol_version = SCSI_REV_2;
798
3209f581 799 ccbh->status = CAM_REQ_CMP;
831bc9e3
MD
800 if (ccbh->target_id == CAM_TARGET_WILDCARD) {
801 ahci_os_lock_port(ap);
802 ahci_port_state_machine(ap, 0);
803 ahci_os_unlock_port(ap);
804 } else {
258223a3
MD
805 switch(ahci_pread(ap, AHCI_PREG_SSTS) &
806 AHCI_PREG_SSTS_SPD) {
807 case AHCI_PREG_SSTS_SPD_GEN1:
808 ccb->cpi.base_transfer_speed = 150000;
809 break;
810 case AHCI_PREG_SSTS_SPD_GEN2:
811 ccb->cpi.base_transfer_speed = 300000;
812 break;
813 default:
814 /* unknown */
815 ccb->cpi.base_transfer_speed = 1000;
816 break;
817 }
3209f581
MD
818#if 0
819 if (ap->ap_type == ATA_PORT_T_NONE)
820 ccbh->status = CAM_DEV_NOT_THERE;
821#endif
258223a3 822 }
258223a3
MD
823 xpt_done(ccb);
824 break;
825 case XPT_RESET_DEV:
f4553de1 826 ahci_os_lock_port(ap);
1980eff3 827 if (ap->ap_type == ATA_PORT_T_NONE) {
3209f581 828 ccbh->status = CAM_DEV_NOT_THERE;
1980eff3
MD
829 } else {
830 ahci_port_reset(ap, atx, 0);
831 ccbh->status = CAM_REQ_CMP;
832 }
f4553de1 833 ahci_os_unlock_port(ap);
258223a3
MD
834 xpt_done(ccb);
835 break;
836 case XPT_RESET_BUS:
f4553de1 837 ahci_os_lock_port(ap);
1980eff3 838 ahci_port_reset(ap, NULL, 1);
f4553de1 839 ahci_os_unlock_port(ap);
fd8bd957 840 ccbh->status = CAM_REQ_CMP;
258223a3
MD
841 xpt_done(ccb);
842 break;
843 case XPT_SET_TRAN_SETTINGS:
844 ccbh->status = CAM_FUNC_NOTAVAIL;
845 xpt_done(ccb);
846 break;
847 case XPT_GET_TRAN_SETTINGS:
848 ccb->cts.protocol = PROTO_SCSI;
849 ccb->cts.protocol_version = SCSI_REV_2;
850 ccb->cts.transport = XPORT_AHCI;
851 ccb->cts.transport_version = XPORT_VERSION_UNSPECIFIED;
852 ccb->cts.proto_specific.valid = 0;
853 ccb->cts.xport_specific.valid = 0;
854 ccbh->status = CAM_REQ_CMP;
855 xpt_done(ccb);
856 break;
857 case XPT_CALC_GEOMETRY:
858 cam_calc_geometry(&ccb->ccg, 1);
859 xpt_done(ccb);
860 break;
861 case XPT_SCSI_IO:
f4553de1
MD
862 /*
863 * Our parallel startup code might have only probed through
864 * to the IDENT, so do the last step if necessary.
865 */
866 if (at->at_probe == ATA_PROBE_NEED_IDENT)
867 ahci_cam_probe(ap, atx);
868 if (at->at_probe != ATA_PROBE_GOOD) {
869 ccbh->status = CAM_DEV_NOT_THERE;
870 xpt_done(ccb);
871 break;
872 }
1980eff3 873 switch(at->at_type) {
258223a3 874 case ATA_PORT_T_DISK:
1980eff3 875 ahci_xpt_scsi_disk_io(ap, atx, ccb);
258223a3
MD
876 break;
877 case ATA_PORT_T_ATAPI:
1980eff3 878 ahci_xpt_scsi_atapi_io(ap, atx, ccb);
258223a3
MD
879 break;
880 default:
881 ccbh->status = CAM_REQ_INVALID;
882 xpt_done(ccb);
883 break;
884 }
885 break;
886 default:
258223a3
MD
887 ccbh->status = CAM_REQ_INVALID;
888 xpt_done(ccb);
889 break;
890 }
891}
892
893/*
de68d532
MD
894 * Poll function.
895 *
896 * Generally this function gets called heavily when interrupts might be
897 * non-operational, during a halt/reboot or panic.
258223a3
MD
898 */
899static
900void
901ahci_xpt_poll(struct cam_sim *sim)
902{
de68d532 903 struct ahci_port *ap;
258223a3 904
de68d532
MD
905 ap = cam_sim_softc(sim);
906 crit_enter();
f4553de1
MD
907 ahci_os_lock_port(ap);
908 ahci_port_intr(ap, 1);
909 ahci_os_unlock_port(ap);
de68d532 910 crit_exit();
258223a3
MD
911}
912
913/*
b4189e5e
MD
914 * Convert the SCSI command in ccb to an ata_xfer command in xa
915 * for ATA_PORT_T_DISK operations. Set the completion function
916 * to convert the response back, then dispatch to the OpenBSD AHCI
917 * layer.
258223a3 918 *
b4189e5e
MD
919 * AHCI DISK commands only support a limited command set, and we
920 * fake additional commands to make it play nice with the CAM subsystem.
258223a3
MD
921 */
922static
923void
1980eff3
MD
924ahci_xpt_scsi_disk_io(struct ahci_port *ap, struct ata_port *atx,
925 union ccb *ccb)
258223a3 926{
258223a3
MD
927 struct ccb_hdr *ccbh;
928 struct ccb_scsiio *csio;
929 struct ata_xfer *xa;
1980eff3 930 struct ata_port *at;
258223a3
MD
931 struct ata_fis_h2d *fis;
932 scsi_cdb_t cdb;
933 union scsi_data *rdata;
934 int rdata_len;
935 u_int64_t capacity;
936 u_int64_t lba;
937 u_int32_t count;
938
258223a3
MD
939 ccbh = &ccb->csio.ccb_h;
940 csio = &ccb->csio;
1980eff3
MD
941 at = atx ? atx : &ap->ap_ata[0];
942
943 /*
944 * XXX not passing NULL at for direct attach!
945 */
946 xa = ahci_ata_get_xfer(ap, atx);
258223a3
MD
947 rdata = (void *)csio->data_ptr;
948 rdata_len = csio->dxfer_len;
949
950 /*
951 * Build the FIS or process the csio to completion.
952 */
953 cdb = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
954 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
955
956 switch(cdb->generic.opcode) {
957 case REQUEST_SENSE:
958 /*
959 * Auto-sense everything, so explicit sense requests
960 * return no-sense.
961 */
962 ccbh->status = CAM_SCSI_STATUS_ERROR;
963 break;
964 case INQUIRY:
965 /*
966 * Inquiry supported features
967 *
968 * [opcode, byte2, page_code, length, control]
969 */
970 if (cdb->inquiry.byte2 & SI_EVPD) {
971 switch(cdb->inquiry.page_code) {
972 case SVPD_SUPPORTED_PAGE_LIST:
973 /* XXX atascsi_disk_vpd_supported */
974 case SVPD_UNIT_SERIAL_NUMBER:
975 /* XXX atascsi_disk_vpd_serial */
976 case SVPD_UNIT_DEVID:
977 /* XXX atascsi_disk_vpd_ident */
978 default:
979 ccbh->status = CAM_FUNC_NOTAVAIL;
980 break;
981 }
982 } else {
983 bzero(rdata, rdata_len);
984 if (rdata_len < SHORT_INQUIRY_LENGTH) {
985 ccbh->status = CAM_CCB_LEN_ERR;
986 break;
987 }
988 if (rdata_len > sizeof(rdata->inquiry_data))
989 rdata_len = sizeof(rdata->inquiry_data);
990 rdata->inquiry_data.device = T_DIRECT;
991 rdata->inquiry_data.version = SCSI_REV_SPC2;
992 rdata->inquiry_data.response_format = 2;
993 rdata->inquiry_data.additional_length = 32;
994 bcopy("SATA ", rdata->inquiry_data.vendor, 8);
1980eff3 995 bcopy(at->at_identify.model,
258223a3
MD
996 rdata->inquiry_data.product,
997 sizeof(rdata->inquiry_data.product));
1980eff3 998 bcopy(at->at_identify.firmware,
258223a3
MD
999 rdata->inquiry_data.revision,
1000 sizeof(rdata->inquiry_data.revision));
1001 ccbh->status = CAM_REQ_CMP;
1002 }
1003 break;
1004 case READ_CAPACITY_16:
1005 if (cdb->read_capacity_16.service_action != SRC16_SERVICE_ACTION) {
1006 ccbh->status = CAM_REQ_INVALID;
1007 break;
1008 }
1009 if (rdata_len < sizeof(rdata->read_capacity_data_16)) {
1010 ccbh->status = CAM_CCB_LEN_ERR;
1011 break;
1012 }
1013 /* fall through */
1014 case READ_CAPACITY:
1015 if (rdata_len < sizeof(rdata->read_capacity_data)) {
1016 ccbh->status = CAM_CCB_LEN_ERR;
1017 break;
1018 }
1019
1980eff3 1020 capacity = at->at_capacity;
258223a3
MD
1021
1022 bzero(rdata, rdata_len);
1023 if (cdb->generic.opcode == READ_CAPACITY) {
1024 rdata_len = sizeof(rdata->read_capacity_data);
1025 if (capacity > 0xFFFFFFFFU)
1026 capacity = 0xFFFFFFFFU;
1027 bzero(&rdata->read_capacity_data, rdata_len);
1028 scsi_ulto4b((u_int32_t)capacity - 1,
1029 rdata->read_capacity_data.addr);
1030 scsi_ulto4b(512, rdata->read_capacity_data.length);
1031 } else {
1032 rdata_len = sizeof(rdata->read_capacity_data_16);
1033 bzero(&rdata->read_capacity_data_16, rdata_len);
1034 scsi_u64to8b(capacity - 1,
1035 rdata->read_capacity_data_16.addr);
1036 scsi_ulto4b(512, rdata->read_capacity_data_16.length);
1037 }
1038 ccbh->status = CAM_REQ_CMP;
1039 break;
1040 case SYNCHRONIZE_CACHE:
1041 /*
1042 * Synchronize cache. Specification says this can take
1043 * greater then 30 seconds so give it at least 45.
1044 */
1045 fis = xa->fis;
258223a3
MD
1046 fis->flags = ATA_H2D_FLAGS_CMD;
1047 fis->command = ATA_C_FLUSH_CACHE;
1048 fis->device = 0;
3209f581
MD
1049 if (xa->timeout < 45000)
1050 xa->timeout = 45000;
1980eff3
MD
1051 xa->datalen = 0;
1052 xa->flags = ATA_F_READ;
1053 xa->complete = ahci_ata_complete_disk_synchronize_cache;
258223a3
MD
1054 break;
1055 case TEST_UNIT_READY:
1056 case START_STOP_UNIT:
1057 case PREVENT_ALLOW:
1058 /*
1059 * Just silently return success
1060 */
1061 ccbh->status = CAM_REQ_CMP;
1062 rdata_len = 0;
1063 break;
1064 case ATA_PASS_12:
1065 case ATA_PASS_16:
1066 /*
1067 * XXX implement pass-through
1068 */
1069 ccbh->status = CAM_FUNC_NOTAVAIL;
1070 break;
1071 default:
1072 switch(cdb->generic.opcode) {
1073 case READ_6:
1074 lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1075 count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1076 xa->flags = ATA_F_READ;
1077 break;
1078 case READ_10:
1079 lba = scsi_4btoul(cdb->rw_10.addr);
1080 count = scsi_2btoul(cdb->rw_10.length);
1081 xa->flags = ATA_F_READ;
1082 break;
1083 case READ_12:
1084 lba = scsi_4btoul(cdb->rw_12.addr);
1085 count = scsi_4btoul(cdb->rw_12.length);
1086 xa->flags = ATA_F_READ;
1087 break;
1088 case READ_16:
1089 lba = scsi_8btou64(cdb->rw_16.addr);
1090 count = scsi_4btoul(cdb->rw_16.length);
1091 xa->flags = ATA_F_READ;
1092 break;
1093 case WRITE_6:
1094 lba = scsi_3btoul(cdb->rw_6.addr) & 0x1FFFFF;
1095 count = cdb->rw_6.length ? cdb->rw_6.length : 0x100;
1096 xa->flags = ATA_F_WRITE;
1097 break;
1098 case WRITE_10:
1099 lba = scsi_4btoul(cdb->rw_10.addr);
1100 count = scsi_2btoul(cdb->rw_10.length);
1101 xa->flags = ATA_F_WRITE;
1102 break;
1103 case WRITE_12:
1104 lba = scsi_4btoul(cdb->rw_12.addr);
1105 count = scsi_4btoul(cdb->rw_12.length);
1106 xa->flags = ATA_F_WRITE;
1107 break;
1108 case WRITE_16:
1109 lba = scsi_8btou64(cdb->rw_16.addr);
1110 count = scsi_4btoul(cdb->rw_16.length);
1111 xa->flags = ATA_F_WRITE;
1112 break;
1113 default:
1114 ccbh->status = CAM_REQ_INVALID;
1115 break;
1116 }
1117 if (ccbh->status != CAM_REQ_INPROG)
1118 break;
1119
1120 fis = xa->fis;
1121 fis->flags = ATA_H2D_FLAGS_CMD;
1122 fis->lba_low = (u_int8_t)lba;
1123 fis->lba_mid = (u_int8_t)(lba >> 8);
1124 fis->lba_high = (u_int8_t)(lba >> 16);
1125 fis->device = ATA_H2D_DEVICE_LBA;
1126
1980eff3
MD
1127 /*
1128 * NCQ only for direct-attached disks, do not currently
1129 * try to use NCQ with port multipliers.
1130 */
1131 if (at->at_ncqdepth > 1 &&
1132 ap->ap_type == ATA_PORT_T_DISK &&
258223a3
MD
1133 (ap->ap_sc->sc_cap & AHCI_REG_CAP_SNCQ) &&
1134 (ccbh->flags & CAM_POLLED) == 0) {
1135 /*
1136 * Use NCQ - always uses 48 bit addressing
1137 */
1138 xa->flags |= ATA_F_NCQ;
1139 fis->command = (xa->flags & ATA_F_WRITE) ?
1140 ATA_C_WRITE_FPDMA : ATA_C_READ_FPDMA;
1141 fis->lba_low_exp = (u_int8_t)(lba >> 24);
1142 fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1143 fis->lba_high_exp = (u_int8_t)(lba >> 40);
1144 fis->sector_count = xa->tag << 3;
1145 fis->features = (u_int8_t)count;
1146 fis->features_exp = (u_int8_t)(count >> 8);
1147 } else if (count > 0x100 || lba > 0xFFFFFFFFU) {
1148 /*
1149 * Use LBA48
1150 */
1151 fis->command = (xa->flags & ATA_F_WRITE) ?
1152 ATA_C_WRITEDMA_EXT : ATA_C_READDMA_EXT;
1153 fis->lba_low_exp = (u_int8_t)(lba >> 24);
1154 fis->lba_mid_exp = (u_int8_t)(lba >> 32);
1155 fis->lba_high_exp = (u_int8_t)(lba >> 40);
1156 fis->sector_count = (u_int8_t)count;
1157 fis->sector_count_exp = (u_int8_t)(count >> 8);
1158 } else {
1159 /*
1160 * Use LBA
1161 *
1162 * NOTE: 256 sectors is supported, stored as 0.
1163 */
1164 fis->command = (xa->flags & ATA_F_WRITE) ?
1165 ATA_C_WRITEDMA : ATA_C_READDMA;
1166 fis->device |= (u_int8_t)(lba >> 24) & 0x0F;
1167 fis->sector_count = (u_int8_t)count;
1168 }
1169
1170 xa->data = csio->data_ptr;
1171 xa->datalen = csio->dxfer_len;
1172 xa->complete = ahci_ata_complete_disk_rw;
3209f581 1173 xa->timeout = ccbh->timeout; /* milliseconds */
258223a3
MD
1174 if (ccbh->flags & CAM_POLLED)
1175 xa->flags |= ATA_F_POLL;
1176 break;
1177 }
1178
1179 /*
1180 * If the request is still in progress the xa and FIS have
1181 * been set up and must be dispatched. Otherwise the request
1182 * is complete.
1183 */
1184 if (ccbh->status == CAM_REQ_INPROG) {
1185 KKASSERT(xa->complete != NULL);
1186 xa->atascsi_private = ccb;
1187 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
f4553de1 1188 ahci_os_lock_port(ap);
1980eff3 1189 fis->flags |= at->at_target;
258223a3 1190 ahci_ata_cmd(xa);
f4553de1 1191 ahci_os_unlock_port(ap);
258223a3
MD
1192 } else {
1193 ahci_ata_put_xfer(xa);
1194 xpt_done(ccb);
1195 }
1196}
1197
b4189e5e
MD
1198/*
1199 * Convert the SCSI command in ccb to an ata_xfer command in xa
1200 * for ATA_PORT_T_ATAPI operations. Set the completion function
1201 * to convert the response back, then dispatch to the OpenBSD AHCI
1202 * layer.
1203 */
258223a3
MD
1204static
1205void
1980eff3
MD
1206ahci_xpt_scsi_atapi_io(struct ahci_port *ap, struct ata_port *atx,
1207 union ccb *ccb)
258223a3 1208{
258223a3
MD
1209 struct ccb_hdr *ccbh;
1210 struct ccb_scsiio *csio;
1211 struct ata_xfer *xa;
1212 struct ata_fis_h2d *fis;
b4189e5e
MD
1213 scsi_cdb_t cdbs;
1214 scsi_cdb_t cdbd;
1215 int flags;
1980eff3 1216 struct ata_port *at;
258223a3 1217
258223a3
MD
1218 ccbh = &ccb->csio.ccb_h;
1219 csio = &ccb->csio;
1980eff3 1220 at = atx ? atx : &ap->ap_ata[0];
b4189e5e
MD
1221
1222 switch (ccbh->flags & CAM_DIR_MASK) {
1223 case CAM_DIR_IN:
1224 flags = ATA_F_PACKET | ATA_F_READ;
1225 break;
1226 case CAM_DIR_OUT:
1227 flags = ATA_F_PACKET | ATA_F_WRITE;
1228 break;
1229 case CAM_DIR_NONE:
1230 flags = ATA_F_PACKET;
1231 break;
1232 default:
1233 ccbh->status = CAM_REQ_INVALID;
1234 xpt_done(ccb);
1235 return;
1236 /* NOT REACHED */
1237 }
1238
1239 /*
1240 * The command has to fit in the packet command buffer.
1241 */
1242 if (csio->cdb_len < 6 || csio->cdb_len > 16) {
1243 ccbh->status = CAM_CCB_LEN_ERR;
1244 xpt_done(ccb);
1245 return;
1246 }
1247
1248 /*
1249 * Initialize the XA and FIS.
1980eff3
MD
1250 *
1251 * XXX not passing NULL at for direct attach!
b4189e5e 1252 */
1980eff3 1253 xa = ahci_ata_get_xfer(ap, atx);
258223a3
MD
1254 fis = xa->fis;
1255
1980eff3 1256 fis->flags = ATA_H2D_FLAGS_CMD | at->at_target;
b4189e5e
MD
1257 fis->command = ATA_C_PACKET;
1258 fis->device = 0;
1259 fis->sector_count = xa->tag << 3;
1260 fis->features = ATA_H2D_FEATURES_DMA |
1980eff3 1261 ((flags & ATA_F_WRITE) ?
b4189e5e
MD
1262 ATA_H2D_FEATURES_DIR_WRITE : ATA_H2D_FEATURES_DIR_READ);
1263 fis->lba_mid = 0x00;
1264 fis->lba_high = 0x20;
1265
1980eff3
MD
1266 xa->flags = flags;
1267 xa->data = csio->data_ptr;
1268 xa->datalen = csio->dxfer_len;
3209f581 1269 xa->timeout = ccbh->timeout; /* milliseconds */
1980eff3
MD
1270
1271 if (ccbh->flags & CAM_POLLED)
1272 xa->flags |= ATA_F_POLL;
1273
b4189e5e
MD
1274 /*
1275 * Copy the cdb to the packetcmd buffer in the FIS using a
1276 * convenient pointer in the xa.
1277 */
1278 cdbs = (void *)((ccbh->flags & CAM_CDB_POINTER) ?
258223a3 1279 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
b4189e5e 1280 bcopy(cdbs, xa->packetcmd, csio->cdb_len);
258223a3 1281
669fbbf7 1282#if 0
b4189e5e
MD
1283 kprintf("opcode %d cdb_len %d dxfer_len %d\n",
1284 cdbs->generic.opcode,
1285 csio->cdb_len, csio->dxfer_len);
669fbbf7 1286#endif
b4189e5e
MD
1287
1288 /*
1289 * Some ATAPI commands do not actually follow the SCSI standard.
1290 */
1291 cdbd = (void *)xa->packetcmd;
1292
1293 switch(cdbd->generic.opcode) {
1294 case INQUIRY:
258223a3 1295 /*
b4189e5e
MD
1296 * Some ATAPI devices can't handle SI_EVPD being set
1297 * for a basic inquiry (page_code == 0).
1298 *
1299 * Some ATAPI devices can't handle long inquiry lengths,
1300 * don't ask me why. Truncate the inquiry length.
258223a3 1301 */
b4189e5e
MD
1302 if ((cdbd->inquiry.byte2 & SI_EVPD) &&
1303 cdbd->inquiry.page_code == 0) {
1304 cdbd->inquiry.byte2 &= ~SI_EVPD;
1305 }
1306 if (cdbd->inquiry.page_code == 0 &&
1307 cdbd->inquiry.length > SHORT_INQUIRY_LENGTH) {
1308 cdbd->inquiry.length = SHORT_INQUIRY_LENGTH;
1309 }
258223a3 1310 break;
258223a3 1311 case READ_6:
258223a3 1312 case WRITE_6:
b4189e5e
MD
1313 /*
1314 * Convert *_6 to *_10 commands. Most ATAPI devices
1315 * cannot handle the SCSI READ_6 and WRITE_6 commands.
1316 */
1317 cdbd->rw_10.opcode |= 0x20;
1318 cdbd->rw_10.byte2 = 0;
1319 cdbd->rw_10.addr[0] = cdbs->rw_6.addr[0] & 0x1F;
1320 cdbd->rw_10.addr[1] = cdbs->rw_6.addr[1];
1321 cdbd->rw_10.addr[2] = cdbs->rw_6.addr[2];
1322 cdbd->rw_10.addr[3] = 0;
1323 cdbd->rw_10.reserved = 0;
1324 cdbd->rw_10.length[0] = 0;
1325 cdbd->rw_10.length[1] = cdbs->rw_6.length;
1326 cdbd->rw_10.control = cdbs->rw_6.control;
1327 break;
258223a3 1328 default:
258223a3
MD
1329 break;
1330 }
1331
b4189e5e
MD
1332 /*
1333 * And dispatch
1334 */
1335 xa->complete = ahci_atapi_complete_cmd;
1336 xa->atascsi_private = ccb;
1337 ccb->ccb_h.sim_priv.entries[0].ptr = ap;
1338 ahci_ata_cmd(xa);
258223a3
MD
1339}
1340
b4189e5e
MD
1341/*
1342 * Completion function for ATA_PORT_T_DISK cache synchronization.
1343 */
258223a3
MD
1344static
1345void
1346ahci_ata_complete_disk_synchronize_cache(struct ata_xfer *xa)
1347{
1348 union ccb *ccb = xa->atascsi_private;
1349 struct ccb_hdr *ccbh = &ccb->ccb_h;
1350 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1351
1352 switch(xa->state) {
1353 case ATA_S_COMPLETE:
1354 ccbh->status = CAM_REQ_CMP;
b4189e5e 1355 ccb->csio.scsi_status = SCSI_STATUS_OK;
258223a3
MD
1356 break;
1357 case ATA_S_ERROR:
1980eff3
MD
1358 kprintf("%s: synchronize_cache: error\n",
1359 ATANAME(ap, xa->at));
b4189e5e
MD
1360 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1361 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1362 ahci_ata_dummy_sense(&ccb->csio.sense_data);
258223a3
MD
1363 break;
1364 case ATA_S_TIMEOUT:
1980eff3
MD
1365 kprintf("%s: synchronize_cache: timeout\n",
1366 ATANAME(ap, xa->at));
258223a3
MD
1367 ccbh->status = CAM_CMD_TIMEOUT;
1368 break;
1369 default:
1370 kprintf("%s: synchronize_cache: unknown state %d\n",
1980eff3 1371 ATANAME(ap, xa->at), xa->state);
258223a3
MD
1372 ccbh->status = CAM_REQ_CMP_ERR;
1373 break;
1374 }
1375 ahci_ata_put_xfer(xa);
f4553de1 1376 ahci_os_unlock_port(ap);
258223a3 1377 xpt_done(ccb);
f4553de1 1378 ahci_os_lock_port(ap);
258223a3
MD
1379}
1380
b4189e5e
MD
1381/*
1382 * Completion function for ATA_PORT_T_DISK I/O
1383 */
258223a3
MD
1384static
1385void
1386ahci_ata_complete_disk_rw(struct ata_xfer *xa)
1387{
1388 union ccb *ccb = xa->atascsi_private;
1389 struct ccb_hdr *ccbh = &ccb->ccb_h;
1390 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1391
1392 switch(xa->state) {
1393 case ATA_S_COMPLETE:
1394 ccbh->status = CAM_REQ_CMP;
b4189e5e 1395 ccb->csio.scsi_status = SCSI_STATUS_OK;
258223a3
MD
1396 break;
1397 case ATA_S_ERROR:
1980eff3 1398 kprintf("%s: disk_rw: error\n", ATANAME(ap, xa->at));
b4189e5e
MD
1399 ccbh->status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
1400 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
1401 ahci_ata_dummy_sense(&ccb->csio.sense_data);
258223a3
MD
1402 break;
1403 case ATA_S_TIMEOUT:
1980eff3 1404 kprintf("%s: disk_rw: timeout\n", ATANAME(ap, xa->at));
258223a3
MD
1405 ccbh->status = CAM_CMD_TIMEOUT;
1406 break;
1407 default:
1408 kprintf("%s: disk_rw: unknown state %d\n",
1980eff3 1409 ATANAME(ap, xa->at), xa->state);
258223a3
MD
1410 ccbh->status = CAM_REQ_CMP_ERR;
1411 break;
1412 }
1413 ccb->csio.resid = xa->resid;
1414 ahci_ata_put_xfer(xa);
f4553de1 1415 ahci_os_unlock_port(ap);
258223a3 1416 xpt_done(ccb);
f4553de1 1417 ahci_os_lock_port(ap);
258223a3 1418}
b4189e5e 1419
7d4fcf34
MD
1420/*
1421 * Completion function for ATA_PORT_T_ATAPI I/O
1422 *
1423 * Sense data is returned in the rfis.
1424 */
b4189e5e
MD
1425static
1426void
1427ahci_atapi_complete_cmd(struct ata_xfer *xa)
1428{
1429 union ccb *ccb = xa->atascsi_private;
1430 struct ccb_hdr *ccbh = &ccb->ccb_h;
1431 struct ahci_port *ap = ccb->ccb_h.sim_priv.entries[0].ptr;
1432 scsi_cdb_t cdb;
1433
1434 cdb = (void *)((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1435 ccb->csio.cdb_io.cdb_ptr : ccb->csio.cdb_io.cdb_bytes);
1436
1437 switch(xa->state) {
1438 case ATA_S_COMPLETE:
1439 ccbh->status = CAM_REQ_CMP;
1440 ccb->csio.scsi_status = SCSI_STATUS_OK;
1441 break;
1442 case ATA_S_ERROR:
1443 kprintf("%s: cmd %d: error\n",
1444 PORTNAME(ap), cdb->generic.opcode);
1445 ccbh->status = CAM_SCSI_STATUS_ERROR;
1446 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
7d4fcf34 1447 ahci_ata_atapi_sense(&xa->rfis, &ccb->csio.sense_data);
b4189e5e
MD
1448 break;
1449 case ATA_S_TIMEOUT:
1450 kprintf("%s: cmd %d: timeout\n",
1451 PORTNAME(ap), cdb->generic.opcode);
1452 ccbh->status = CAM_CMD_TIMEOUT;
1453 break;
1454 default:
1455 kprintf("%s: cmd %d: unknown state %d\n",
1456 PORTNAME(ap), cdb->generic.opcode, xa->state);
1457 ccbh->status = CAM_REQ_CMP_ERR;
1458 break;
1459 }
1460 ccb->csio.resid = xa->resid;
1461 ahci_ata_put_xfer(xa);
f4553de1 1462 ahci_os_unlock_port(ap);
b4189e5e 1463 xpt_done(ccb);
f4553de1 1464 ahci_os_lock_port(ap);
b4189e5e
MD
1465}
1466
7d4fcf34
MD
1467/*
1468 * Construct dummy sense data for errors on DISKs
1469 */
b4189e5e
MD
1470static
1471void
1472ahci_ata_dummy_sense(struct scsi_sense_data *sense_data)
1473{
1474 sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
1475 sense_data->segment = 0;
1476 sense_data->flags = SSD_KEY_MEDIUM_ERROR;
1477 sense_data->info[0] = 0;
1478 sense_data->info[1] = 0;
1479 sense_data->info[2] = 0;
1480 sense_data->info[3] = 0;
1481 sense_data->extra_len = 0;
1482}
7d4fcf34
MD
1483
1484/*
1485 * Construct atapi sense data for errors on ATAPI
1486 *
1487 * The ATAPI sense data is stored in the passed rfis and must be converted
1488 * to SCSI sense data.
1489 */
1490static
1491void
1492ahci_ata_atapi_sense(struct ata_fis_d2h *rfis,
1493 struct scsi_sense_data *sense_data)
1494{
1495 sense_data->error_code = SSD_ERRCODE_VALID | SSD_CURRENT_ERROR;
1496 sense_data->segment = 0;
1497 sense_data->flags = (rfis->error & 0xF0) >> 4;
1498 if (rfis->error & 0x04)
1499 sense_data->flags |= SSD_KEY_ILLEGAL_REQUEST;
1500 if (rfis->error & 0x02)
1501 sense_data->flags |= SSD_EOM;
1502 if (rfis->error & 0x01)
1503 sense_data->flags |= SSD_ILI;
1504 sense_data->info[0] = 0;
1505 sense_data->info[1] = 0;
1506 sense_data->info[2] = 0;
1507 sense_data->info[3] = 0;
1508 sense_data->extra_len = 0;
1509}