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