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