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