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