1/* $NetBSD: scsiconf.c,v 1.276 2016/11/20 15:37:19 mlelstv Exp $ */
2
3/*-
4 * Copyright (c) 1998, 1999, 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum; Jason R. Thorpe of the Numerical Aerospace
9 * Simulation Facility, NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * Originally written by Julian Elischer (julian@tfs.com)
35 * for TRW Financial Systems for use under the MACH(2.5) operating system.
36 *
37 * TRW Financial Systems, in accordance with their agreement with Carnegie
38 * Mellon University, makes this software available to CMU to distribute
39 * or use in any manner that they see fit as long as this message is kept with
40 * the software. For this reason TFS also grants any other persons or
41 * organisations permission to use or modify this software.
42 *
43 * TFS supplies this software to be publicly redistributed
44 * on the understanding that TFS is not responsible for the correct
45 * functioning of this software in any circumstances.
46 *
47 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
48 */
49
50#include <sys/cdefs.h>
51__KERNEL_RCSID(0, "$NetBSD: scsiconf.c,v 1.276 2016/11/20 15:37:19 mlelstv Exp $");
52
53#include <sys/param.h>
54#include <sys/systm.h>
55#include <sys/kernel.h>
56#include <sys/proc.h>
57#include <sys/kthread.h>
58#include <sys/malloc.h>
59#include <sys/mutex.h>
60#include <sys/once.h>
61#include <sys/device.h>
62#include <sys/conf.h>
63#include <sys/fcntl.h>
64#include <sys/scsiio.h>
65#include <sys/queue.h>
66
67#include <dev/scsipi/scsi_all.h>
68#include <dev/scsipi/scsipi_all.h>
69#include <dev/scsipi/scsiconf.h>
70
71#include "locators.h"
72
73static const struct scsipi_periphsw scsi_probe_dev = {
74 NULL,
75 NULL,
76 NULL,
77 NULL,
78};
79
80struct scsi_initq {
81 struct scsipi_channel *sc_channel;
82 TAILQ_ENTRY(scsi_initq) scsi_initq;
83};
84
85static ONCE_DECL(scsi_conf_ctrl);
86static TAILQ_HEAD(, scsi_initq) scsi_initq_head;
87static kmutex_t scsibus_qlock;
88static kcondvar_t scsibus_qcv;
89
90static int scsi_probe_device(struct scsibus_softc *, int, int);
91
92static int scsibusmatch(device_t, cfdata_t, void *);
93static void scsibusattach(device_t, device_t, void *);
94static int scsibusdetach(device_t, int flags);
95static int scsibusrescan(device_t, const char *, const int *);
96static void scsidevdetached(device_t, device_t);
97
98CFATTACH_DECL3_NEW(scsibus, sizeof(struct scsibus_softc),
99 scsibusmatch, scsibusattach, scsibusdetach, NULL,
100 scsibusrescan, scsidevdetached, DVF_DETACH_SHUTDOWN);
101
102extern struct cfdriver scsibus_cd;
103
104static dev_type_open(scsibusopen);
105static dev_type_close(scsibusclose);
106static dev_type_ioctl(scsibusioctl);
107
108const struct cdevsw scsibus_cdevsw = {
109 .d_open = scsibusopen,
110 .d_close = scsibusclose,
111 .d_read = noread,
112 .d_write = nowrite,
113 .d_ioctl = scsibusioctl,
114 .d_stop = nostop,
115 .d_tty = notty,
116 .d_poll = nopoll,
117 .d_mmap = nommap,
118 .d_kqfilter = nokqfilter,
119 .d_discard = nodiscard,
120 .d_flag = D_OTHER | D_MPSAFE
121};
122
123static int scsibusprint(void *, const char *);
124static void scsibus_discover_thread(void *);
125static void scsibus_config(struct scsibus_softc *);
126
127const struct scsipi_bustype scsi_bustype = {
128 SCSIPI_BUSTYPE_BUSTYPE(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_PSCSI),
129 scsi_scsipi_cmd,
130 scsipi_interpret_sense,
131 scsi_print_addr,
132 scsi_kill_pending,
133 scsi_async_event_xfer_mode,
134};
135
136const struct scsipi_bustype scsi_fc_bustype = {
137 SCSIPI_BUSTYPE_BUSTYPE(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_FC),
138 scsi_scsipi_cmd,
139 scsipi_interpret_sense,
140 scsi_print_addr,
141 scsi_kill_pending,
142 scsi_fc_sas_async_event_xfer_mode,
143};
144
145const struct scsipi_bustype scsi_sas_bustype = {
146 SCSIPI_BUSTYPE_BUSTYPE(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_SAS),
147 scsi_scsipi_cmd,
148 scsipi_interpret_sense,
149 scsi_print_addr,
150 scsi_kill_pending,
151 scsi_fc_sas_async_event_xfer_mode,
152};
153
154const struct scsipi_bustype scsi_usb_bustype = {
155 SCSIPI_BUSTYPE_BUSTYPE(SCSIPI_BUSTYPE_SCSI, SCSIPI_BUSTYPE_SCSI_USB),
156 scsi_scsipi_cmd,
157 scsipi_interpret_sense,
158 scsi_print_addr,
159 scsi_kill_pending,
160 NULL,
161};
162
163static int
164scsibus_init(void)
165{
166
167 TAILQ_INIT(&scsi_initq_head);
168 mutex_init(&scsibus_qlock, MUTEX_DEFAULT, IPL_NONE);
169 cv_init(&scsibus_qcv, "scsinitq");
170 return 0;
171}
172
173int
174scsiprint(void *aux, const char *pnp)
175{
176 struct scsipi_channel *chan = aux;
177 struct scsipi_adapter *adapt = chan->chan_adapter;
178
179 /* only "scsibus"es can attach to "scsi"s; easy. */
180 if (pnp)
181 aprint_normal("scsibus at %s", pnp);
182
183 /* don't print channel if the controller says there can be only one. */
184 if (adapt->adapt_nchannels != 1)
185 aprint_normal(" channel %d", chan->chan_channel);
186
187 return (UNCONF);
188}
189
190static int
191scsibusmatch(device_t parent, cfdata_t cf, void *aux)
192{
193 struct scsipi_channel *chan = aux;
194
195 if (SCSIPI_BUSTYPE_TYPE(chan->chan_bustype->bustype_type) !=
196 SCSIPI_BUSTYPE_SCSI)
197 return 0;
198
199 if (cf->cf_loc[SCSICF_CHANNEL] != chan->chan_channel &&
200 cf->cf_loc[SCSICF_CHANNEL] != SCSICF_CHANNEL_DEFAULT)
201 return (0);
202
203 return (1);
204}
205
206static void
207scsibusattach(device_t parent, device_t self, void *aux)
208{
209 struct scsibus_softc *sc = device_private(self);
210 struct scsipi_channel *chan = aux;
211 struct scsi_initq *scsi_initq;
212
213 if (!pmf_device_register(self, NULL, NULL))
214 aprint_error_dev(self, "couldn't establish power handler\n");
215
216 sc->sc_dev = self;
217 sc->sc_channel = chan;
218 chan->chan_name = device_xname(sc->sc_dev);
219
220 aprint_naive(": SCSI bus\n");
221 aprint_normal(": %d target%s, %d lun%s per target\n",
222 chan->chan_ntargets,
223 chan->chan_ntargets == 1 ? "" : "s",
224 chan->chan_nluns,
225 chan->chan_nluns == 1 ? "" : "s");
226
227 /*
228 * XXX
229 * newer adapters support more than 256 outstanding commands
230 * per periph and don't use the tag (they eventually allocate one
231 * internally). Right now scsipi always allocate a tag and
232 * is limited to 256 tags, per scsi specs.
233 * this should be revisited
234 */
235 if (chan->chan_flags & SCSIPI_CHAN_OPENINGS) {
236 if (chan->chan_max_periph > 256)
237 chan->chan_max_periph = 256;
238 } else {
239 if (chan->chan_adapter->adapt_max_periph > 256)
240 chan->chan_adapter->adapt_max_periph = 256;
241 }
242
243 mutex_init(chan_mtx(chan), MUTEX_DEFAULT, IPL_BIO);
244 cv_init(&chan->chan_cv_thr, "scshut");
245 cv_init(&chan->chan_cv_comp, "sccomp");
246 cv_init(&chan->chan_cv_xs, "xscmd");
247 chan_running(chan) = true;
248
249 if (scsipi_adapter_addref(chan->chan_adapter))
250 return;
251
252 RUN_ONCE(&scsi_conf_ctrl, scsibus_init);
253
254 /* Initialize the channel structure first */
255 chan->chan_init_cb = NULL;
256 chan->chan_init_cb_arg = NULL;
257
258 scsi_initq = malloc(sizeof(struct scsi_initq), M_DEVBUF, M_WAITOK);
259 scsi_initq->sc_channel = chan;
260 TAILQ_INSERT_TAIL(&scsi_initq_head, scsi_initq, scsi_initq);
261 config_pending_incr(sc->sc_dev);
262 if (scsipi_channel_init(chan)) {
263 aprint_error_dev(sc->sc_dev, "failed to init channel\n");
264 return;
265 }
266
267 /*
268 * Create the discover thread
269 */
270 if (kthread_create(PRI_NONE, 0, NULL, scsibus_discover_thread, sc,
271 NULL, "%s-d", chan->chan_name)) {
272 aprint_error_dev(sc->sc_dev, "unable to create discovery "
273 "thread for channel %d\n", chan->chan_channel);
274 return;
275 }
276}
277
278static void
279scsibus_discover_thread(void *arg)
280{
281 struct scsibus_softc *sc = arg;
282
283 scsibus_config(sc);
284 kthread_exit(0);
285}
286
287static void
288scsibus_config(struct scsibus_softc *sc)
289{
290 struct scsipi_channel *chan = sc->sc_channel;
291 struct scsi_initq *scsi_initq;
292
293#ifndef SCSI_DELAY
294#define SCSI_DELAY 2
295#endif
296 if ((chan->chan_flags & SCSIPI_CHAN_NOSETTLE) == 0 &&
297 SCSI_DELAY > 0) {
298 aprint_normal_dev(sc->sc_dev,
299 "waiting %d seconds for devices to settle...\n",
300 SCSI_DELAY);
301 /* ...an identifier we know no one will use... */
302 kpause("scsidly", false, SCSI_DELAY * hz, NULL);
303 }
304
305 /* Make sure the devices probe in scsibus order to avoid jitter. */
306 mutex_enter(&scsibus_qlock);
307 for (;;) {
308 scsi_initq = TAILQ_FIRST(&scsi_initq_head);
309 if (scsi_initq->sc_channel == chan)
310 break;
311 cv_wait(&scsibus_qcv, &scsibus_qlock);
312 }
313 mutex_exit(&scsibus_qlock);
314
315 scsi_probe_bus(sc, -1, -1);
316
317 mutex_enter(&scsibus_qlock);
318 TAILQ_REMOVE(&scsi_initq_head, scsi_initq, scsi_initq);
319 cv_broadcast(&scsibus_qcv);
320 mutex_exit(&scsibus_qlock);
321
322 free(scsi_initq, M_DEVBUF);
323
324 scsipi_adapter_delref(chan->chan_adapter);
325
326 config_pending_decr(sc->sc_dev);
327}
328
329static int
330scsibusdetach(device_t self, int flags)
331{
332 struct scsibus_softc *sc = device_private(self);
333 struct scsipi_channel *chan = sc->sc_channel;
334 struct scsipi_periph *periph;
335 int ctarget, clun;
336 struct scsipi_xfer *xs;
337 int error;
338
339 /*
340 * Detach all of the periphs.
341 */
342 if ((error = scsipi_target_detach(chan, -1, -1, flags)) != 0)
343 return error;
344
345 pmf_device_deregister(self);
346
347 /*
348 * Process outstanding commands (which will never complete as the
349 * controller is gone).
350 *
351 * XXX Surely this is redundant? If we get this far, the
352 * XXX peripherals have all been detached.
353 */
354 for (ctarget = 0; ctarget < chan->chan_ntargets; ctarget++) {
355 if (ctarget == chan->chan_id)
356 continue;
357 for (clun = 0; clun < chan->chan_nluns; clun++) {
358 periph = scsipi_lookup_periph(chan, ctarget, clun);
359 if (periph == NULL)
360 continue;
361 TAILQ_FOREACH(xs, &periph->periph_xferq, device_q) {
362 callout_stop(&xs->xs_callout);
363 xs->error = XS_DRIVER_STUFFUP;
364 scsipi_done(xs);
365 }
366 }
367 }
368
369 /*
370 * Now shut down the channel.
371 */
372 scsipi_channel_shutdown(chan);
373
374 chan_running(chan) = false;
375 cv_destroy(&chan->chan_cv_xs);
376 cv_destroy(&chan->chan_cv_comp);
377 cv_destroy(&chan->chan_cv_thr);
378 mutex_destroy(chan_mtx(chan));
379
380 return 0;
381}
382
383/*
384 * Probe the requested scsi bus. It must be already set up.
385 * target and lun optionally narrow the search if not -1
386 */
387int
388scsi_probe_bus(struct scsibus_softc *sc, int target, int lun)
389{
390 struct scsipi_channel *chan = sc->sc_channel;
391 int maxtarget, mintarget, maxlun, minlun;
392 int error;
393
394 if (target == -1) {
395 maxtarget = chan->chan_ntargets - 1;
396 mintarget = 0;
397 } else {
398 if (target < 0 || target >= chan->chan_ntargets)
399 return (EINVAL);
400 maxtarget = mintarget = target;
401 }
402
403 if (lun == -1) {
404 maxlun = chan->chan_nluns - 1;
405 minlun = 0;
406 } else {
407 if (lun < 0 || lun >= chan->chan_nluns)
408 return (EINVAL);
409 maxlun = minlun = lun;
410 }
411
412 /*
413 * Some HBAs provide an abstracted view of the bus; give them an
414 * oppertunity to re-scan it before we do.
415 */
416 scsipi_adapter_ioctl(chan, SCBUSIOLLSCAN, NULL, 0, curproc);
417
418 if ((error = scsipi_adapter_addref(chan->chan_adapter)) != 0)
419 goto ret;
420 for (target = mintarget; target <= maxtarget; target++) {
421 if (target == chan->chan_id)
422 continue;
423 for (lun = minlun; lun <= maxlun; lun++) {
424 /*
425 * See if there's a device present, and configure it.
426 */
427 if (scsi_probe_device(sc, target, lun) == 0)
428 break;
429 /* otherwise something says we should look further */
430 }
431
432 /*
433 * Now that we've discovered all of the LUNs on this
434 * I_T Nexus, update the xfer mode for all of them
435 * that we know about.
436 */
437 scsipi_set_xfer_mode(chan, target, 1);
438 }
439 scsipi_adapter_delref(chan->chan_adapter);
440ret:
441 return (error);
442}
443
444static int
445scsibusrescan(device_t sc, const char *ifattr,
446 const int *locators)
447{
448
449 KASSERT(ifattr && !strcmp(ifattr, "scsibus"));
450 KASSERT(locators);
451
452 return (scsi_probe_bus(device_private(sc),
453 locators[SCSIBUSCF_TARGET], locators[SCSIBUSCF_LUN]));
454}
455
456static void
457scsidevdetached(device_t self, device_t child)
458{
459 struct scsibus_softc *sc = device_private(self);
460 struct scsipi_channel *chan = sc->sc_channel;
461 struct scsipi_periph *periph;
462 int target, lun;
463
464 target = device_locator(child, SCSIBUSCF_TARGET);
465 lun = device_locator(child, SCSIBUSCF_LUN);
466
467 mutex_enter(chan_mtx(chan));
468
469 periph = scsipi_lookup_periph_locked(chan, target, lun);
470 KASSERT(periph->periph_dev == child);
471
472 scsipi_remove_periph(chan, periph);
473 scsipi_free_periph(periph);
474
475 mutex_exit(chan_mtx(chan));
476}
477
478/*
479 * Print out autoconfiguration information for a subdevice.
480 *
481 * This is a slight abuse of 'standard' autoconfiguration semantics,
482 * because 'print' functions don't normally print the colon and
483 * device information. However, in this case that's better than
484 * either printing redundant information before the attach message,
485 * or having the device driver call a special function to print out
486 * the standard device information.
487 */
488static int
489scsibusprint(void *aux, const char *pnp)
490{
491 struct scsipibus_attach_args *sa = aux;
492 struct scsipi_inquiry_pattern *inqbuf;
493 u_int8_t type;
494 const char *dtype;
495 char vendor[33], product[65], revision[17];
496 int target, lun;
497
498 if (pnp != NULL)
499 aprint_normal("%s", pnp);
500
501 inqbuf = &sa->sa_inqbuf;
502
503 target = sa->sa_periph->periph_target;
504 lun = sa->sa_periph->periph_lun;
505 type = inqbuf->type & SID_TYPE;
506
507 dtype = scsipi_dtype(type);
508
509 strnvisx(vendor, sizeof(vendor), inqbuf->vendor, 8,
510 VIS_TRIM|VIS_SAFE|VIS_OCTAL);
511 strnvisx(product, sizeof(product), inqbuf->product, 16,
512 VIS_TRIM|VIS_SAFE|VIS_OCTAL);
513 strnvisx(revision, sizeof(revision), inqbuf->revision, 4,
514 VIS_TRIM|VIS_SAFE|VIS_OCTAL);
515
516 aprint_normal(" target %d lun %d: <%s, %s, %s> %s %s",
517 target, lun, vendor, product, revision, dtype,
518 inqbuf->removable ? "removable" : "fixed");
519
520 return (UNCONF);
521}
522
523static const struct scsi_quirk_inquiry_pattern scsi_quirk_patterns[] = {
524 {{T_DIRECT, T_REMOV,
525 "Apple ", "iPod ", ""}, PQUIRK_START},
526 {{T_CDROM, T_REMOV,
527 "CHINON ", "CD-ROM CDS-431 ", ""}, PQUIRK_NOLUNS},
528 {{T_CDROM, T_REMOV,
529 "CHINON ", "CD-ROM CDS-435 ", ""}, PQUIRK_NOLUNS},
530 {{T_CDROM, T_REMOV,
531 "Chinon ", "CD-ROM CDS-525 ", ""}, PQUIRK_NOLUNS},
532 {{T_CDROM, T_REMOV,
533 "CHINON ", "CD-ROM CDS-535 ", ""}, PQUIRK_NOLUNS},
534 {{T_CDROM, T_REMOV,
535 "DEC ", "RRD42 (C) DEC ", ""}, PQUIRK_NOLUNS},
536 {{T_CDROM, T_REMOV,
537 "DENON ", "DRD-25X ", "V"}, PQUIRK_NOLUNS},
538 {{T_CDROM, T_REMOV,
539 "GENERIC ", "CRD-BP2 ", ""}, PQUIRK_NOLUNS},
540 {{T_CDROM, T_REMOV,
541 "HP ", "C4324/C4325 ", ""}, PQUIRK_NOLUNS},
542 {{T_CDROM, T_REMOV,
543 "IMS ", "CDD521/10 ", "2.06"}, PQUIRK_NOLUNS},
544 {{T_CDROM, T_REMOV,
545 "MATSHITA", "CD-ROM CR-5XX ", "1.0b"}, PQUIRK_NOLUNS},
546 {{T_CDROM, T_REMOV,
547 "MEDAVIS ", "RENO CD-ROMX2A ", ""}, PQUIRK_NOLUNS},
548 {{T_CDROM, T_REMOV,
549 "MEDIAVIS", "CDR-H93MV ", "1.3"}, PQUIRK_NOLUNS},
550 {{T_CDROM, T_REMOV,
551 "NEC ", "CD-ROM DRIVE:502", ""}, PQUIRK_NOLUNS},
552 {{T_CDROM, T_REMOV,
553 "NEC ", "CD-ROM DRIVE:55 ", ""}, PQUIRK_NOLUNS},
554 {{T_CDROM, T_REMOV,
555 "NEC ", "CD-ROM DRIVE:83 ", ""}, PQUIRK_NOLUNS},
556 {{T_CDROM, T_REMOV,
557 "NEC ", "CD-ROM DRIVE:84 ", ""}, PQUIRK_NOLUNS},
558 {{T_CDROM, T_REMOV,
559 "NEC ", "CD-ROM DRIVE:841", ""}, PQUIRK_NOLUNS},
560 {{T_CDROM, T_REMOV,
561 "OLYMPUS ", "CDS620E ", "1.1d"},
562 PQUIRK_NOLUNS|PQUIRK_NOSYNC|PQUIRK_NOCAPACITY},
563 {{T_CDROM, T_REMOV,
564 "PIONEER ", "CD-ROM DR-124X ", "1.01"}, PQUIRK_NOLUNS},
565 {{T_CDROM, T_REMOV,
566 "PLEXTOR ", "CD-ROM PX-4XCS ", "1.01"},
567 PQUIRK_NOLUNS|PQUIRK_NOSYNC},
568 {{T_CDROM, T_REMOV,
569 "SONY ", "CD-ROM CDU-541 ", ""}, PQUIRK_NOLUNS},
570 {{T_CDROM, T_REMOV,
571 "SONY ", "CD-ROM CDU-55S ", ""}, PQUIRK_NOLUNS},
572 {{T_CDROM, T_REMOV,
573 "SONY ", "CD-ROM CDU-561 ", ""}, PQUIRK_NOLUNS},
574 {{T_CDROM, T_REMOV,
575 "SONY ", "CD-ROM CDU-76S", ""},
576 PQUIRK_NOLUNS|PQUIRK_NOSYNC|PQUIRK_NOWIDE},
577 {{T_CDROM, T_REMOV,
578 "SONY ", "CD-ROM CDU-8003A", ""}, PQUIRK_NOLUNS},
579 {{T_CDROM, T_REMOV,
580 "SONY ", "CD-ROM CDU-8012 ", ""}, PQUIRK_NOLUNS},
581 {{T_CDROM, T_REMOV,
582 "TEAC ", "CD-ROM ", "1.06"}, PQUIRK_NOLUNS},
583 {{T_CDROM, T_REMOV,
584 "TEAC ", "CD-ROM CD-56S ", "1.0B"}, PQUIRK_NOLUNS},
585 {{T_CDROM, T_REMOV,
586 "TEXEL ", "CD-ROM ", "1.06"}, PQUIRK_NOLUNS},
587 {{T_CDROM, T_REMOV,
588 "TEXEL ", "CD-ROM DM-XX24 K", "1.09"}, PQUIRK_NOLUNS},
589 {{T_CDROM, T_REMOV,
590 "TEXEL ", "CD-ROM DM-XX24 K", "1.10"}, PQUIRK_NOLUNS},
591 {{T_CDROM, T_REMOV,
592 "TOSHIBA ", "XM-4101TASUNSLCD", ""}, PQUIRK_NOLUNS|PQUIRK_NOSYNC},
593 /* "IBM CDRM00201 !F" 0724 is an IBM OEM Toshiba XM-4101BME */
594 {{T_CDROM, T_REMOV,
595 "IBM ", "CDRM00201 !F", "0724"}, PQUIRK_NOLUNS|PQUIRK_NOSYNC},
596 {{T_CDROM, T_REMOV,
597 "ShinaKen", "CD-ROM DM-3x1S", "1.04"}, PQUIRK_NOLUNS},
598 {{T_CDROM, T_REMOV,
599 "JVC ", "R2626", ""}, PQUIRK_NOLUNS},
600 {{T_CDROM, T_REMOV,
601 "YAMAHA", "CRW8424S", ""}, PQUIRK_NOLUNS},
602 {{T_CDROM, T_REMOV,
603 "NEC ", "CD-ROM DRIVE:222", ""}, PQUIRK_NOLUNS|PQUIRK_NOSYNC},
604
605 {{T_DIRECT, T_FIXED,
606 "MICROP ", "1588-15MBSUN0669", ""}, PQUIRK_AUTOSAVE},
607 {{T_DIRECT, T_FIXED,
608 "MICROP ", "2217-15MQ1091501", ""}, PQUIRK_NOSYNCCACHE},
609 {{T_OPTICAL, T_REMOV,
610 "EPSON ", "OMD-5010 ", "3.08"}, PQUIRK_NOLUNS},
611 {{T_DIRECT, T_FIXED,
612 "ADAPTEC ", "AEC-4412BD", "1.2A"}, PQUIRK_NOMODESENSE},
613 {{T_DIRECT, T_FIXED,
614 "ADAPTEC ", "ACB-4000", ""}, PQUIRK_FORCELUNS|PQUIRK_AUTOSAVE|PQUIRK_NOMODESENSE},
615 {{T_DIRECT, T_FIXED,
616 "DEC ", "RZ55 (C) DEC", ""}, PQUIRK_AUTOSAVE},
617 {{T_DIRECT, T_FIXED,
618 "EMULEX ", "MD21/S2 ESDI", "A00"},
619 PQUIRK_FORCELUNS|PQUIRK_AUTOSAVE},
620 {{T_DIRECT, T_FIXED,
621 "MICROP", "1548-15MZ1077801", "HZ2P"}, PQUIRK_NOTAG},
622 {{T_DIRECT, T_FIXED,
623 "HP ", "C372", ""}, PQUIRK_NOTAG},
624 {{T_DIRECT, T_FIXED,
625 "IBMRAID ", "0662S", ""}, PQUIRK_AUTOSAVE},
626 {{T_DIRECT, T_FIXED,
627 "IBM ", "0663H", ""}, PQUIRK_AUTOSAVE},
628 {{T_DIRECT, T_FIXED,
629 "IBM", "0664", ""}, PQUIRK_AUTOSAVE},
630 {{T_DIRECT, T_FIXED,
631 /* improperly report DT-only sync mode */
632 "IBM ", "DXHS36D", ""},
633 PQUIRK_CAP_SYNC|PQUIRK_CAP_WIDE16},
634 {{T_DIRECT, T_FIXED,
635 "IBM ", "DXHS18Y", ""},
636 PQUIRK_CAP_SYNC|PQUIRK_CAP_WIDE16},
637 {{T_DIRECT, T_FIXED,
638 "IBM ", "H3171-S2", ""},
639 PQUIRK_NOLUNS|PQUIRK_AUTOSAVE},
640 {{T_DIRECT, T_FIXED,
641 "IBM ", "KZ-C", ""}, PQUIRK_AUTOSAVE},
642 /* Broken IBM disk */
643 {{T_DIRECT, T_FIXED,
644 "" , "DFRSS2F", ""}, PQUIRK_AUTOSAVE},
645 {{T_DIRECT, T_FIXED,
646 "Initio ", "", ""}, PQUIRK_NOBIGMODESENSE},
647 {{T_DIRECT, T_REMOV,
648 "MPL ", "MC-DISK- ", ""}, PQUIRK_NOLUNS},
649 {{T_DIRECT, T_FIXED,
650 "MAXTOR ", "XT-3280 ", ""}, PQUIRK_NOLUNS},
651 {{T_DIRECT, T_FIXED,
652 "MAXTOR ", "XT-4380S ", ""}, PQUIRK_NOLUNS},
653 {{T_DIRECT, T_FIXED,
654 "MAXTOR ", "MXT-1240S ", ""}, PQUIRK_NOLUNS},
655 {{T_DIRECT, T_FIXED,
656 "MAXTOR ", "XT-4170S ", ""}, PQUIRK_NOLUNS},
657 {{T_DIRECT, T_FIXED,
658 "MAXTOR ", "XT-8760S", ""}, PQUIRK_NOLUNS},
659 {{T_DIRECT, T_FIXED,
660 "MAXTOR ", "LXT-213S ", ""}, PQUIRK_NOLUNS},
661 {{T_DIRECT, T_FIXED,
662 "MAXTOR ", "LXT-213S SUN0207", ""}, PQUIRK_NOLUNS},
663 {{T_DIRECT, T_FIXED,
664 "MAXTOR ", "LXT-200S ", ""}, PQUIRK_NOLUNS},
665 {{T_DIRECT, T_FIXED,
666 "MEGADRV ", "EV1000", ""}, PQUIRK_NOMODESENSE},
667 {{T_DIRECT, T_FIXED,
668 "MICROP", "1991-27MZ", ""}, PQUIRK_NOTAG},
669 {{T_DIRECT, T_FIXED,
670 "MST ", "SnapLink ", ""}, PQUIRK_NOLUNS},
671 {{T_DIRECT, T_FIXED,
672 "NEC ", "D3847 ", "0307"}, PQUIRK_NOLUNS},
673 {{T_DIRECT, T_FIXED,
674 "QUANTUM ", "ELS85S ", ""}, PQUIRK_AUTOSAVE},
675 {{T_DIRECT, T_FIXED,
676 "QUANTUM ", "LPS525S ", ""}, PQUIRK_NOLUNS},
677 {{T_DIRECT, T_FIXED,
678 "QUANTUM ", "P105S 910-10-94x", ""}, PQUIRK_NOLUNS},
679 {{T_DIRECT, T_FIXED,
680 "QUANTUM ", "PD1225S ", ""}, PQUIRK_NOLUNS},
681 {{T_DIRECT, T_FIXED,
682 "QUANTUM ", "PD210S SUN0207", ""}, PQUIRK_NOLUNS},
683 {{T_DIRECT, T_FIXED,
684 "QUANTUM ", "ATLAS IV 9 WLS", "0A0A"}, PQUIRK_CAP_NODT},
685 {{T_DIRECT, T_FIXED,
686 "RODIME ", "RO3000S ", ""}, PQUIRK_NOLUNS},
687 {{T_DIRECT, T_FIXED,
688 "SEAGATE ", "ST125N ", ""}, PQUIRK_NOLUNS},
689 {{T_DIRECT, T_FIXED,
690 "SEAGATE ", "ST157N ", ""}, PQUIRK_NOLUNS},
691 {{T_DIRECT, T_FIXED,
692 "SEAGATE ", "ST296 ", ""}, PQUIRK_NOLUNS},
693 {{T_DIRECT, T_FIXED,
694 "SEAGATE ", "ST296N ", ""}, PQUIRK_NOLUNS},
695 {{T_DIRECT, T_FIXED,
696 "SEAGATE ", "ST318404LC ", ""}, PQUIRK_NOLUNS},
697 {{T_DIRECT, T_FIXED,
698 "SEAGATE ", "ST15150N ", ""}, PQUIRK_NOTAG},
699 {{T_DIRECT, T_FIXED,
700 "SEAGATE ", "ST19171", ""}, PQUIRK_NOMODESENSE},
701 {{T_DIRECT, T_FIXED,
702 "SEAGATE ", "ST32430N", ""}, PQUIRK_CAP_SYNC},
703 {{T_DIRECT, T_FIXED,
704 "SEAGATE ", "ST34501FC ", ""}, PQUIRK_NOMODESENSE},
705 {{T_DIRECT, T_FIXED,
706 "SEAGATE ", "SX910800N", ""}, PQUIRK_NOTAG},
707 {{T_DIRECT, T_FIXED,
708 "TOSHIBA ", "MK538FB ", "6027"}, PQUIRK_NOLUNS},
709 {{T_DIRECT, T_FIXED,
710 "MICROP ", "1924", ""}, PQUIRK_CAP_SYNC},
711 {{T_DIRECT, T_FIXED,
712 "FUJITSU ", "M2266", ""}, PQUIRK_CAP_SYNC},
713 {{T_DIRECT, T_FIXED,
714 "FUJITSU ", "M2624S-512 ", ""}, PQUIRK_CAP_SYNC},
715 {{T_DIRECT, T_FIXED,
716 "SEAGATE ", "SX336704LC" , ""}, PQUIRK_CAP_SYNC | PQUIRK_CAP_WIDE16},
717 {{T_DIRECT, T_FIXED,
718 "SEAGATE ", "SX173404LC", ""}, PQUIRK_CAP_SYNC | PQUIRK_CAP_WIDE16},
719
720 {{T_DIRECT, T_REMOV,
721 "IOMEGA", "ZIP 100", "J.03"}, PQUIRK_NOLUNS|PQUIRK_NOSYNC},
722 {{T_DIRECT, T_REMOV,
723 "INSITE", "I325VM", ""}, PQUIRK_NOLUNS},
724
725 /* XXX: QIC-36 tape behind Emulex adapter. Very broken. */
726 {{T_SEQUENTIAL, T_REMOV,
727 " ", " ", " "}, PQUIRK_NOLUNS},
728 {{T_SEQUENTIAL, T_REMOV,
729 "EMULEX ", "MT-02 QIC ", ""}, PQUIRK_NOLUNS},
730 {{T_SEQUENTIAL, T_REMOV,
731 "CALIPER ", "CP150 ", ""}, PQUIRK_NOLUNS},
732 {{T_SEQUENTIAL, T_REMOV,
733 "EXABYTE ", "EXB-8200 ", ""}, PQUIRK_NOLUNS},
734 {{T_SEQUENTIAL, T_REMOV,
735 "SONY ", "GY-10C ", ""}, PQUIRK_NOLUNS},
736 {{T_SEQUENTIAL, T_REMOV,
737 "SONY ", "SDT-2000 ", "2.09"}, PQUIRK_NOLUNS},
738 {{T_SEQUENTIAL, T_REMOV,
739 "SONY ", "SDT-5000 ", "3."}, PQUIRK_NOSYNC|PQUIRK_NOWIDE},
740 {{T_SEQUENTIAL, T_REMOV,
741 "SONY ", "SDT-5200 ", "3."}, PQUIRK_NOLUNS},
742 {{T_SEQUENTIAL, T_REMOV,
743 "TANDBERG", " TDC 3600 ", ""}, PQUIRK_NOLUNS},
744 /* Following entry reported as a Tandberg 3600; ref. PR1933 */
745 {{T_SEQUENTIAL, T_REMOV,
746 "ARCHIVE ", "VIPER 150 21247", ""}, PQUIRK_NOLUNS},
747 /* Following entry for a Cipher ST150S; ref. PR4171 */
748 {{T_SEQUENTIAL, T_REMOV,
749 "ARCHIVE ", "VIPER 1500 21247", "2.2G"}, PQUIRK_NOLUNS},
750 {{T_SEQUENTIAL, T_REMOV,
751 "ARCHIVE ", "Python 28454-XXX", ""}, PQUIRK_NOLUNS},
752 {{T_SEQUENTIAL, T_REMOV,
753 "WANGTEK ", "5099ES SCSI", ""}, PQUIRK_NOLUNS},
754 {{T_SEQUENTIAL, T_REMOV,
755 "WANGTEK ", "5150ES SCSI", ""}, PQUIRK_NOLUNS},
756 {{T_SEQUENTIAL, T_REMOV,
757 "WANGTEK ", "SCSI-36", ""}, PQUIRK_NOLUNS},
758 {{T_SEQUENTIAL, T_REMOV,
759 "WangDAT ", "Model 1300 ", "02.4"}, PQUIRK_NOSYNC|PQUIRK_NOWIDE},
760 {{T_SEQUENTIAL, T_REMOV,
761 "WangDAT ", "Model 2600 ", "01.7"}, PQUIRK_NOSYNC|PQUIRK_NOWIDE},
762 {{T_SEQUENTIAL, T_REMOV,
763 "WangDAT ", "Model 3200 ", "02.2"}, PQUIRK_NOSYNC|PQUIRK_NOWIDE},
764 {{T_SEQUENTIAL, T_REMOV,
765 "TEAC ", "MT-2ST/N50 ", ""}, PQUIRK_NOLUNS},
766
767 {{T_SCANNER, T_FIXED,
768 "RICOH ", "IS60 ", "1R08"}, PQUIRK_NOLUNS},
769 {{T_SCANNER, T_FIXED,
770 "UMAX ", "Astra 1200S ", "V2.9"}, PQUIRK_NOLUNS},
771 {{T_SCANNER, T_FIXED,
772 "UMAX ", "Astra 1220S ", ""}, PQUIRK_NOLUNS},
773 {{T_SCANNER, T_FIXED,
774 "UMAX ", "UMAX S-6E ", "V2.0"}, PQUIRK_NOLUNS},
775 {{T_SCANNER, T_FIXED,
776 "UMAX ", "UMAX S-12 ", "V2.1"}, PQUIRK_NOLUNS},
777 {{T_SCANNER, T_FIXED,
778 "ULTIMA ", "A6000C ", ""}, PQUIRK_NOLUNS},
779 {{T_PROCESSOR, T_FIXED,
780 "ESG-SHV", "SCA HSBP M15", ""}, PQUIRK_NOLUNS},
781 {{T_PROCESSOR, T_FIXED,
782 "SYMBIOS", "", ""}, PQUIRK_NOLUNS},
783 {{T_PROCESSOR, T_FIXED,
784 "LITRONIC", "PCMCIA ", ""}, PQUIRK_NOLUNS},
785 {{T_CHANGER, T_REMOV,
786 "SONY ", "CDL1100 ", ""}, PQUIRK_NOLUNS},
787 {{T_ENCLOSURE, T_FIXED,
788 "SUN ", "SENA ", ""}, PQUIRK_NOLUNS},
789};
790
791/*
792 * given a target and lun, ask the device what
793 * it is, and find the correct driver table
794 * entry.
795 */
796static int
797scsi_probe_device(struct scsibus_softc *sc, int target, int lun)
798{
799 struct scsipi_channel *chan = sc->sc_channel;
800 struct scsipi_periph *periph;
801 struct scsipi_inquiry_data inqbuf;
802 const struct scsi_quirk_inquiry_pattern *finger;
803 int checkdtype, priority, docontinue, quirks;
804 struct scsipibus_attach_args sa;
805 cfdata_t cf;
806 int locs[SCSIBUSCF_NLOCS];
807
808 /*
809 * Assume no more luns to search after this one.
810 * If we successfully get Inquiry data and after
811 * merging quirks we find we can probe for more
812 * luns, we will.
813 */
814 docontinue = 0;
815
816 /* Skip this slot if it is already attached. */
817 if (scsipi_lookup_periph(chan, target, lun) != NULL)
818 return (docontinue);
819
820 periph = scsipi_alloc_periph(M_NOWAIT);
821 if (periph == NULL) {
822#ifdef DIAGNOSTIC
823 aprint_error_dev(sc->sc_dev,
824 "cannot allocate periph for target %d lun %d\n",
825 target, lun);
826#endif
827 return (ENOMEM);
828 }
829 periph->periph_channel = chan;
830 periph->periph_switch = &scsi_probe_dev;
831
832 periph->periph_target = target;
833 periph->periph_lun = lun;
834 periph->periph_quirks = chan->chan_defquirks;
835
836#ifdef SCSIPI_DEBUG
837 if (SCSIPI_DEBUG_TYPE == SCSIPI_BUSTYPE_SCSI &&
838 SCSIPI_DEBUG_TARGET == target &&
839 SCSIPI_DEBUG_LUN == lun)
840 periph->periph_dbflags |= SCSIPI_DEBUG_FLAGS;
841#endif
842
843 /*
844 * Ask the device what it is
845 */
846
847#ifdef SCSI_2_DEF
848 /* some devices need to be told to go to SCSI2 */
849 /* However some just explode if you tell them this.. leave it out */
850 scsi_change_def(periph, XS_CTL_DISCOVERY | XS_CTL_SILENT);
851#endif /* SCSI_2_DEF */
852
853 /* Now go ask the device all about itself. */
854 memset(&inqbuf, 0, sizeof(inqbuf));
855 {
856 u_int8_t *extension = &inqbuf.flags1;
857 int len = 0;
858 while (len < 3)
859 extension[len++] = '\0';
860 while (len < 3 + 28)
861 extension[len++] = ' ';
862 while (len < 3 + 28 + 20)
863 extension[len++] = '\0';
864 while (len < 3 + 28 + 20 + 1)
865 extension[len++] = '\0';
866 while (len < 3 + 28 + 20 + 1 + 1)
867 extension[len++] = '\0';
868 while (len < 3 + 28 + 20 + 1 + 1 + (8*2))
869 extension[len++] = ' ';
870 }
871 if (scsipi_inquire(periph, &inqbuf, XS_CTL_DISCOVERY | XS_CTL_SILENT))
872 goto bad;
873
874 periph->periph_type = inqbuf.device & SID_TYPE;
875 if (inqbuf.dev_qual2 & SID_REMOVABLE)
876 periph->periph_flags |= PERIPH_REMOVABLE;
877 periph->periph_version = inqbuf.version & SID_ANSII;
878
879 /*
880 * Any device qualifier that has the top bit set (qualifier&4 != 0)
881 * is vendor specific and won't match in this switch.
882 * All we do here is throw out bad/negative responses.
883 */
884 checkdtype = 0;
885 switch (inqbuf.device & SID_QUAL) {
886 case SID_QUAL_LU_PRESENT:
887 checkdtype = 1;
888 break;
889
890 case SID_QUAL_LU_NOTPRESENT:
891 case SID_QUAL_reserved:
892 case SID_QUAL_LU_NOT_SUPP:
893 goto bad;
894
895 default:
896 break;
897 }
898
899 /* Let the adapter driver handle the device separatley if it wants. */
900 if (chan->chan_adapter->adapt_accesschk != NULL &&
901 (*chan->chan_adapter->adapt_accesschk)(periph, &sa.sa_inqbuf))
902 goto bad;
903
904 if (checkdtype) {
905 switch (periph->periph_type) {
906 case T_DIRECT:
907 case T_SEQUENTIAL:
908 case T_PRINTER:
909 case T_PROCESSOR:
910 case T_WORM:
911 case T_CDROM:
912 case T_SCANNER:
913 case T_OPTICAL:
914 case T_CHANGER:
915 case T_COMM:
916 case T_IT8_1:
917 case T_IT8_2:
918 case T_STORARRAY:
919 case T_ENCLOSURE:
920 case T_SIMPLE_DIRECT:
921 case T_OPTIC_CARD_RW:
922 case T_OBJECT_STORED:
923 default:
924 break;
925 case T_NODEVICE:
926 goto bad;
927 }
928 }
929
930 sa.sa_periph = periph;
931 sa.sa_inqbuf.type = inqbuf.device;
932 sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
933 T_REMOV : T_FIXED;
934 sa.sa_inqbuf.vendor = inqbuf.vendor;
935 sa.sa_inqbuf.product = inqbuf.product;
936 sa.sa_inqbuf.revision = inqbuf.revision;
937 sa.scsipi_info.scsi_version = inqbuf.version;
938 sa.sa_inqptr = &inqbuf;
939
940 finger = scsipi_inqmatch(
941 &sa.sa_inqbuf, scsi_quirk_patterns,
942 sizeof(scsi_quirk_patterns)/sizeof(scsi_quirk_patterns[0]),
943 sizeof(scsi_quirk_patterns[0]), &priority);
944
945 if (finger != NULL)
946 quirks = finger->quirks;
947 else
948 quirks = 0;
949
950 /*
951 * Determine the operating mode capabilities of the device.
952 */
953 if (periph->periph_version >= 2) {
954 if ((inqbuf.flags3 & SID_CmdQue) != 0 &&
955 (quirks & PQUIRK_NOTAG) == 0)
956 periph->periph_cap |= PERIPH_CAP_TQING;
957 if ((inqbuf.flags3 & SID_Linked) != 0)
958 periph->periph_cap |= PERIPH_CAP_LINKCMDS;
959 if ((inqbuf.flags3 & SID_Sync) != 0 &&
960 (quirks & PQUIRK_NOSYNC) == 0)
961 periph->periph_cap |= PERIPH_CAP_SYNC;
962 if ((inqbuf.flags3 & SID_WBus16) != 0 &&
963 (quirks & PQUIRK_NOWIDE) == 0)
964 periph->periph_cap |= PERIPH_CAP_WIDE16;
965 if ((inqbuf.flags3 & SID_WBus32) != 0 &&
966 (quirks & PQUIRK_NOWIDE) == 0)
967 periph->periph_cap |= PERIPH_CAP_WIDE32;
968 if ((inqbuf.flags3 & SID_SftRe) != 0)
969 periph->periph_cap |= PERIPH_CAP_SFTRESET;
970 if ((inqbuf.flags3 & SID_RelAdr) != 0)
971 periph->periph_cap |= PERIPH_CAP_RELADR;
972 /* SPC-2 */
973 if (periph->periph_version >= 3 &&
974 !(quirks & PQUIRK_CAP_NODT)){
975 /*
976 * Report ST clocking though CAP_WIDExx/CAP_SYNC.
977 * If the device only supports DT, clear these
978 * flags (DT implies SYNC and WIDE)
979 */
980 switch (inqbuf.flags4 & SID_Clocking) {
981 case SID_CLOCKING_DT_ONLY:
982 periph->periph_cap &=
983 ~(PERIPH_CAP_SYNC |
984 PERIPH_CAP_WIDE16 |
985 PERIPH_CAP_WIDE32);
986 /* FALLTHROUGH */
987 case SID_CLOCKING_SD_DT:
988 periph->periph_cap |= PERIPH_CAP_DT;
989 break;
990 default: /* ST only or invalid */
991 /* nothing to do */
992 break;
993 }
994 }
995 if (periph->periph_version >= 3) {
996 if (inqbuf.flags4 & SID_IUS)
997 periph->periph_cap |= PERIPH_CAP_IUS;
998 if (inqbuf.flags4 & SID_QAS)
999 periph->periph_cap |= PERIPH_CAP_QAS;
1000 }
1001 }
1002 if (quirks & PQUIRK_CAP_SYNC)
1003 periph->periph_cap |= PERIPH_CAP_SYNC;
1004 if (quirks & PQUIRK_CAP_WIDE16)
1005 periph->periph_cap |= PERIPH_CAP_WIDE16;
1006
1007 /*
1008 * Now apply any quirks from the table.
1009 */
1010 periph->periph_quirks |= quirks;
1011 if (periph->periph_version == 0 &&
1012 (periph->periph_quirks & PQUIRK_FORCELUNS) == 0)
1013 periph->periph_quirks |= PQUIRK_NOLUNS;
1014
1015 if ((periph->periph_quirks & PQUIRK_NOLUNS) == 0)
1016 docontinue = 1;
1017
1018 locs[SCSIBUSCF_TARGET] = target;
1019 locs[SCSIBUSCF_LUN] = lun;
1020
1021 if ((cf = config_search_loc(config_stdsubmatch, sc->sc_dev,
1022 "scsibus", locs, &sa)) != NULL) {
1023 scsipi_insert_periph(chan, periph);
1024 /*
1025 * XXX Can't assign periph_dev here, because we'll
1026 * XXX need it before config_attach() returns. Must
1027 * XXX assign it in periph driver.
1028 */
1029 config_attach_loc(sc->sc_dev, cf, locs, &sa,
1030 scsibusprint);
1031 } else {
1032 scsibusprint(&sa, device_xname(sc->sc_dev));
1033 aprint_normal(" not configured\n");
1034 goto bad;
1035 }
1036
1037 return (docontinue);
1038
1039bad:
1040 scsipi_free_periph(periph);
1041 return (docontinue);
1042}
1043
1044/****** Entry points for user control of the SCSI bus. ******/
1045
1046static int
1047scsibusopen(dev_t dev, int flag, int fmt,
1048 struct lwp *l)
1049{
1050 struct scsibus_softc *sc;
1051 int error, unit = minor(dev);
1052
1053 sc = device_lookup_private(&scsibus_cd, unit);
1054 if (sc == NULL)
1055 return (ENXIO);
1056
1057 if (sc->sc_flags & SCSIBUSF_OPEN)
1058 return (EBUSY);
1059
1060 if ((error = scsipi_adapter_addref(sc->sc_channel->chan_adapter)) != 0)
1061 return (error);
1062
1063 sc->sc_flags |= SCSIBUSF_OPEN;
1064
1065 return (0);
1066}
1067
1068static int
1069scsibusclose(dev_t dev, int flag, int fmt,
1070 struct lwp *l)
1071{
1072 struct scsibus_softc *sc;
1073
1074 sc = device_lookup_private(&scsibus_cd, minor(dev));
1075 scsipi_adapter_delref(sc->sc_channel->chan_adapter);
1076
1077 sc->sc_flags &= ~SCSIBUSF_OPEN;
1078
1079 return (0);
1080}
1081
1082static int
1083scsibusioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
1084{
1085 struct scsibus_softc *sc;
1086 struct scsipi_channel *chan;
1087 int error;
1088
1089 sc = device_lookup_private(&scsibus_cd, minor(dev));
1090 chan = sc->sc_channel;
1091
1092 /*
1093 * Enforce write permission for ioctls that change the
1094 * state of the bus. Host adapter specific ioctls must
1095 * be checked by the adapter driver.
1096 */
1097 switch (cmd) {
1098 case SCBUSIOSCAN:
1099 case SCBUSIODETACH:
1100 case SCBUSIORESET:
1101 if ((flag & FWRITE) == 0)
1102 return (EBADF);
1103 }
1104
1105 switch (cmd) {
1106 case SCBUSIOSCAN:
1107 {
1108 struct scbusioscan_args *a =
1109 (struct scbusioscan_args *)addr;
1110
1111 error = scsi_probe_bus(sc, a->sa_target, a->sa_lun);
1112 break;
1113 }
1114
1115 case SCBUSIODETACH:
1116 {
1117 struct scbusiodetach_args *a =
1118 (struct scbusiodetach_args *)addr;
1119
1120 error = scsipi_target_detach(chan, a->sa_target, a->sa_lun, 0);
1121 break;
1122 }
1123
1124
1125 case SCBUSIORESET:
1126 /* FALLTHROUGH */
1127 default:
1128 error = scsipi_adapter_ioctl(chan, cmd, addr, flag, l->l_proc);
1129 break;
1130 }
1131
1132 return (error);
1133}
1134