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