1/* $NetBSD: atapiconf.c,v 1.89 2016/11/20 15:37:19 mlelstv Exp $ */
2
3/*
4 * Copyright (c) 1996, 2001 Manuel Bouyer. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__KERNEL_RCSID(0, "$NetBSD: atapiconf.c,v 1.89 2016/11/20 15:37:19 mlelstv Exp $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/malloc.h>
33#include <sys/device.h>
34#include <sys/buf.h>
35#include <sys/proc.h>
36#include <sys/kthread.h>
37
38#include <dev/scsipi/scsipi_all.h>
39#include <dev/scsipi/scsipiconf.h>
40#include <dev/scsipi/atapiconf.h>
41
42#include "locators.h"
43
44#define SILENT_PRINTF(flags,string) if (!(flags & A_SILENT)) printf string
45#define MAX_TARGET 1
46
47const struct scsipi_periphsw atapi_probe_periphsw = {
48 NULL,
49 NULL,
50 NULL,
51 NULL,
52};
53
54static int atapibusmatch(device_t, cfdata_t, void *);
55static void atapibusattach(device_t, device_t, void *);
56static int atapibusdetach(device_t, int flags);
57static void atapibuschilddet(device_t, device_t);
58
59static int atapibussubmatch(device_t, cfdata_t, const int *, void *);
60
61static int atapi_probe_bus(struct atapibus_softc *, int);
62
63static int atapibusprint(void *, const char *);
64
65CFATTACH_DECL3_NEW(atapibus, sizeof(struct atapibus_softc),
66 atapibusmatch, atapibusattach, atapibusdetach, NULL, NULL,
67 atapibuschilddet, DVF_DETACH_SHUTDOWN);
68
69extern struct cfdriver atapibus_cd;
70
71static const struct scsi_quirk_inquiry_pattern atapi_quirk_patterns[] = {
72 {{T_CDROM, T_REMOV,
73 "ALPS ELECTRIC CO.,LTD. DC544C", "", "SW03D"}, PQUIRK_NOTUR},
74 {{T_CDROM, T_REMOV,
75 "CR-2801TE", "", "1.07"}, PQUIRK_NOSENSE},
76 {{T_CDROM, T_REMOV,
77 "CREATIVECD3630E", "", "AC101"}, PQUIRK_NOSENSE},
78 {{T_CDROM, T_REMOV,
79 "FX320S", "", "q01"}, PQUIRK_NOSENSE},
80 {{T_CDROM, T_REMOV,
81 "GCD-R580B", "", "1.00"}, PQUIRK_LITTLETOC},
82 {{T_CDROM, T_REMOV,
83 "HITACHI CDR-7730", "", "0008a"}, PQUIRK_NOSENSE},
84 {{T_CDROM, T_REMOV,
85 "MATSHITA CR-574", "", "1.02"}, PQUIRK_NOCAPACITY},
86 {{T_CDROM, T_REMOV,
87 "MATSHITA CR-574", "", "1.06"}, PQUIRK_NOCAPACITY},
88 {{T_CDROM, T_REMOV,
89 "Memorex CRW-2642", "", "1.0g"}, PQUIRK_NOSENSE},
90 {{T_CDROM, T_REMOV,
91 "NEC CD-ROM DRIVE:273", "", "4.21"}, PQUIRK_NOTUR},
92 {{T_CDROM, T_REMOV,
93 "SANYO CRD-256P", "", "1.02"}, PQUIRK_NOCAPACITY},
94 {{T_CDROM, T_REMOV,
95 "SANYO CRD-254P", "", "1.02"}, PQUIRK_NOCAPACITY},
96 {{T_CDROM, T_REMOV,
97 "SANYO CRD-S54P", "", "1.08"}, PQUIRK_NOCAPACITY},
98 {{T_CDROM, T_REMOV,
99 "CD-ROM CDR-S1", "", "1.70"}, PQUIRK_NOCAPACITY}, /* Sanyo */
100 {{T_CDROM, T_REMOV,
101 "CD-ROM CDR-N16", "", "1.25"}, PQUIRK_NOCAPACITY}, /* Sanyo */
102};
103
104int
105atapiprint(void *aux, const char *pnp)
106{
107 if (pnp)
108 aprint_normal("atapibus at %s", pnp);
109 return (UNCONF);
110}
111
112static int
113atapibusmatch(device_t parent, cfdata_t cf, void *aux)
114{
115 struct scsipi_channel *chan = aux;
116
117 if (chan == NULL)
118 return (0);
119
120 if (SCSIPI_BUSTYPE_TYPE(chan->chan_bustype->bustype_type) !=
121 SCSIPI_BUSTYPE_ATAPI)
122 return (0);
123
124 return (1);
125}
126
127static int
128atapibussubmatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
129{
130 struct scsipibus_attach_args *sa = aux;
131 struct scsipi_periph *periph = sa->sa_periph;
132
133 if (cf->cf_loc[ATAPIBUSCF_DRIVE] != ATAPIBUSCF_DRIVE_DEFAULT &&
134 cf->cf_loc[ATAPIBUSCF_DRIVE] != periph->periph_target)
135 return (0);
136 return (config_match(parent, cf, aux));
137}
138
139static void
140atapibusattach(device_t parent, device_t self, void *aux)
141{
142 struct atapibus_softc *sc = device_private(self);
143 struct scsipi_channel *chan = aux;
144
145 sc->sc_channel = chan;
146 sc->sc_dev = self;
147
148 chan->chan_name = device_xname(sc->sc_dev);
149
150 /* ATAPI has no LUNs. */
151 chan->chan_nluns = 1;
152 aprint_naive("\n");
153 aprint_normal(": %d targets\n", chan->chan_ntargets);
154
155 mutex_init(&chan->chan_adapter->adapt_mtx, MUTEX_DEFAULT, IPL_BIO);
156 cv_init(&chan->chan_cv_thr, "scshut");
157 cv_init(&chan->chan_cv_comp, "sccomp");
158 cv_init(&chan->chan_cv_xs, "xscmd");
159
160 /* Initialize the channel. */
161 chan->chan_init_cb = NULL;
162 chan->chan_init_cb_arg = NULL;
163 scsipi_channel_init(chan);
164
165 if (!pmf_device_register(self, NULL, NULL))
166 aprint_error_dev(self, "couldn't establish power handler\n");
167
168 /* Probe the bus for devices. */
169 atapi_probe_bus(sc, -1);
170}
171
172static void
173atapibuschilddet(device_t self, device_t child)
174{
175 struct atapibus_softc *sc = device_private(self);
176 struct scsipi_channel *chan = sc->sc_channel;
177 struct scsipi_periph *periph;
178 int target;
179
180 mutex_enter(chan_mtx(chan));
181 for (target = 0; target < chan->chan_ntargets; target++) {
182 periph = scsipi_lookup_periph_locked(chan, target, 0);
183 if (periph == NULL || periph->periph_dev != child)
184 continue;
185 scsipi_remove_periph(chan, periph);
186 scsipi_free_periph(periph);
187 break;
188 }
189 mutex_exit(chan_mtx(chan));
190}
191
192static int
193atapibusdetach(device_t self, int flags)
194{
195 struct atapibus_softc *sc = device_private(self);
196 struct scsipi_channel *chan = sc->sc_channel;
197 struct scsipi_periph *periph;
198 int target, error = 0;
199
200 /*
201 * Shut down the channel.
202 */
203 scsipi_channel_shutdown(chan);
204
205 /* for config_detach() */
206 KERNEL_LOCK(1, curlwp);
207
208 /*
209 * Now detach all of the periphs.
210 */
211 mutex_enter(chan_mtx(chan));
212 for (target = 0; target < chan->chan_ntargets; target++) {
213 periph = scsipi_lookup_periph_locked(chan, target, 0);
214 if (periph == NULL)
215 continue;
216 error = config_detach(periph->periph_dev, flags);
217 if (error) {
218 mutex_exit(chan_mtx(chan));
219 goto out;
220 }
221 KASSERT(scsipi_lookup_periph(chan, target, 0) == NULL);
222 }
223 mutex_exit(chan_mtx(chan));
224
225out:
226 cv_destroy(&chan->chan_cv_xs);
227 cv_destroy(&chan->chan_cv_comp);
228 cv_destroy(&chan->chan_cv_thr);
229 mutex_destroy(chan_mtx(chan));
230
231 /* XXXSMP scsipi */
232 KERNEL_UNLOCK_ONE(curlwp);
233 return error;
234}
235
236static int
237atapi_probe_bus(struct atapibus_softc *sc, int target)
238{
239 struct scsipi_channel *chan = sc->sc_channel;
240 int maxtarget, mintarget;
241 int error;
242 struct atapi_adapter *atapi_adapter;
243
244 KASSERT(chan->chan_ntargets >= 1);
245
246 if (target == -1) {
247 maxtarget = chan->chan_ntargets - 1;
248 mintarget = 0;
249 } else {
250 if (target < 0 || target >= chan->chan_ntargets)
251 return (ENXIO);
252 maxtarget = mintarget = target;
253 }
254
255 if ((error = scsipi_adapter_addref(chan->chan_adapter)) != 0)
256 return (error);
257 atapi_adapter = (struct atapi_adapter*)chan->chan_adapter;
258 for (target = mintarget; target <= maxtarget; target++)
259 atapi_adapter->atapi_probe_device(sc, target);
260 scsipi_adapter_delref(chan->chan_adapter);
261 return (0);
262}
263
264void *
265atapi_probe_device(struct atapibus_softc *sc, int target,
266 struct scsipi_periph *periph, struct scsipibus_attach_args *sa)
267{
268 struct scsipi_channel *chan = sc->sc_channel;
269 const struct scsi_quirk_inquiry_pattern *finger;
270 cfdata_t cf;
271 int priority, quirks;
272
273 finger = scsipi_inqmatch(
274 &sa->sa_inqbuf, (const void *)atapi_quirk_patterns,
275 sizeof(atapi_quirk_patterns) /
276 sizeof(atapi_quirk_patterns[0]),
277 sizeof(atapi_quirk_patterns[0]), &priority);
278
279 if (finger != NULL)
280 quirks = finger->quirks;
281 else
282 quirks = 0;
283
284 /*
285 * Now apply any quirks from the table.
286 */
287 periph->periph_quirks |= quirks;
288
289 if ((cf = config_search_ia(atapibussubmatch, sc->sc_dev,
290 "atapibus", sa)) != 0) {
291 scsipi_insert_periph(chan, periph);
292 /*
293 * XXX Can't assign periph_dev here, because we'll
294 * XXX need it before config_attach() returns. Must
295 * XXX assign it in periph driver.
296 */
297 return config_attach(sc->sc_dev, cf, sa,
298 atapibusprint);
299 } else {
300 atapibusprint(sa, device_xname(sc->sc_dev));
301 aprint_normal(" not configured\n");
302 scsipi_free_periph(periph);
303 return NULL;
304 }
305}
306
307static int
308atapibusprint(void *aux, const char *pnp)
309{
310 struct scsipibus_attach_args *sa = aux;
311 struct scsipi_inquiry_pattern *inqbuf;
312 const char *dtype;
313
314 if (pnp != NULL)
315 aprint_normal("%s", pnp);
316
317 inqbuf = &sa->sa_inqbuf;
318
319 dtype = scsipi_dtype(inqbuf->type & SID_TYPE);
320 aprint_normal(" drive %d: <%s, %s, %s> %s %s",
321 sa->sa_periph->periph_target, inqbuf->vendor,
322 inqbuf->product, inqbuf->revision, dtype,
323 inqbuf->removable ? "removable" : "fixed");
324 return (UNCONF);
325}
326