xref: /netbsd-src/sys/dev/ic/icpsp.c (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /*	$NetBSD: icpsp.c,v 1.14 2006/03/25 23:10:12 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: icpsp.c,v 1.14 2006/03/25 23:10:12 thorpej Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/device.h>
46 #include <sys/queue.h>
47 #include <sys/proc.h>
48 #include <sys/buf.h>
49 #include <sys/endian.h>
50 #include <sys/malloc.h>
51 #include <sys/scsiio.h>
52 #include <sys/lock.h>
53 
54 #include <sys/bswap.h>
55 #include <machine/bus.h>
56 
57 #include <uvm/uvm_extern.h>
58 
59 #include <dev/scsipi/scsi_all.h>
60 #include <dev/scsipi/scsi_disk.h>
61 #include <dev/scsipi/scsipi_all.h>
62 #include <dev/scsipi/scsiconf.h>
63 #include <dev/scsipi/scsi_message.h>
64 
65 #include <dev/ic/icpreg.h>
66 #include <dev/ic/icpvar.h>
67 
68 struct icpsp_softc {
69 	struct	device sc_dv;
70 	struct	scsipi_adapter sc_adapter;
71 	struct	scsipi_channel sc_channel;
72 	int	sc_busno;
73 	int	sc_openings;
74 };
75 
76 void	icpsp_attach(struct device *, struct device *, void *);
77 void	icpsp_intr(struct icp_ccb *);
78 int	icpsp_match(struct device *, struct cfdata *, void *);
79 void	icpsp_scsipi_request(struct scsipi_channel *, scsipi_adapter_req_t,
80 			     void *);
81 
82 void	icpsp_adjqparam(struct device *, int);
83 
84 CFATTACH_DECL(icpsp, sizeof(struct icpsp_softc),
85     icpsp_match, icpsp_attach, NULL, NULL);
86 
87 static const struct icp_servicecb icpsp_servicecb = {
88 	icpsp_adjqparam,
89 };
90 
91 int
92 icpsp_match(struct device *parent, struct cfdata *match, void *aux)
93 {
94 	struct icp_attach_args *icpa;
95 
96 	icpa = aux;
97 
98 	return (icpa->icpa_unit >= ICPA_UNIT_SCSI);
99 }
100 
101 void
102 icpsp_attach(struct device *parent, struct device *self, void *aux)
103 {
104 	struct icp_attach_args *icpa;
105 	struct icpsp_softc *sc;
106 	struct icp_softc *icp;
107 
108 	icpa = (struct icp_attach_args *)aux;
109 	sc = (struct icpsp_softc *)self;
110 	icp = (struct icp_softc *)parent;
111 
112 	sc->sc_busno = icpa->icpa_unit - ICPA_UNIT_SCSI;
113 	sc->sc_openings = icp->icp_openings;
114 	printf(": physical SCSI channel %d\n", sc->sc_busno);
115 
116 	icp_register_servicecb(icp, icpa->icpa_unit, &icpsp_servicecb);
117 
118 	sc->sc_adapter.adapt_dev = &sc->sc_dv;
119 	sc->sc_adapter.adapt_nchannels = 1;
120 	sc->sc_adapter.adapt_openings = icp->icp_openings;
121 	sc->sc_adapter.adapt_max_periph = icp->icp_openings;
122 	sc->sc_adapter.adapt_minphys = minphys;
123 	sc->sc_adapter.adapt_request = icpsp_scsipi_request;
124 
125 	sc->sc_channel.chan_adapter = &sc->sc_adapter;
126 	sc->sc_channel.chan_bustype = &scsi_bustype;
127 	sc->sc_channel.chan_channel = 0;
128 	sc->sc_channel.chan_ntargets = ((icp->icp_class & ICP_FC) != 0 ?
129 	    127 : 16); /* XXX bogus check */
130 	sc->sc_channel.chan_nluns = 8;
131 	sc->sc_channel.chan_id = icp->icp_bus_id[sc->sc_busno];
132 	sc->sc_channel.chan_flags = SCSIPI_CHAN_NOSETTLE;
133 
134 	config_found(self, &sc->sc_channel, scsiprint);
135 }
136 
137 void
138 icpsp_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
139 		     void *arg)
140 {
141 	struct scsipi_xfer *xs;
142 	struct scsipi_periph *periph;
143 	struct icpsp_softc *sc;
144 	struct icp_rawcmd *rc;
145 	struct icp_softc *icp;
146 	struct icp_ccb *ic;
147 	int rv, flags, s, soff;
148 
149 	sc = (void *)chan->chan_adapter->adapt_dev;
150 	icp = (struct icp_softc *)device_parent(&sc->sc_dv);
151 
152 	switch (req) {
153 	case ADAPTER_REQ_RUN_XFER:
154 		xs = arg;
155 		periph = xs->xs_periph;
156 		flags = xs->xs_control;
157 
158 		SC_DEBUG(periph, SCSIPI_DB2, ("icpsp_scsi_request run_xfer\n"));
159 
160 		if ((flags & XS_CTL_RESET) != 0) {
161 			/* XXX Unimplemented. */
162 			xs->error = XS_DRIVER_STUFFUP;
163 			scsipi_done(xs);
164 			return;
165 		}
166 
167 #if defined(ICP_DEBUG) || defined(SCSIDEBUG)
168 		if (xs->cmdlen > sizeof(rc->rc_cdb))
169 			panic("%s: CDB too large", sc->sc_dv.dv_xname);
170 #endif
171 
172 		/*
173 		 * Allocate a CCB.
174 		 */
175 		if (__predict_false((ic = icp_ccb_alloc(icp)) == NULL)) {
176 			xs->error = XS_RESOURCE_SHORTAGE;
177 			scsipi_done(xs);
178 			return;
179 		}
180 		rc = &ic->ic_cmd.cmd_packet.rc;
181 		ic->ic_sg = rc->rc_sg;
182 		ic->ic_service = ICP_SCSIRAWSERVICE;
183 		soff = ICP_SCRATCH_SENSE + ic->ic_ident *
184 		    sizeof(struct scsi_sense_data);
185 
186 		/*
187 		 * Build the command.  We don't need to actively prevent
188 		 * access to array components, since the controller kindly
189 		 * takes care of that for us.
190 		 */
191 		ic->ic_cmd.cmd_opcode = htole16(ICP_WRITE);
192 		memcpy(rc->rc_cdb, xs->cmd, xs->cmdlen);
193 
194 		rc->rc_padding0 = 0;
195 		rc->rc_direction = htole32((flags & XS_CTL_DATA_IN) != 0 ?
196 		    ICP_DATA_IN : ICP_DATA_OUT);
197 		rc->rc_mdisc_time = 0;
198 		rc->rc_mcon_time = 0;
199 		rc->rc_clen = htole32(xs->cmdlen);
200 		rc->rc_target = periph->periph_target;
201 		rc->rc_lun = periph->periph_lun;
202 		rc->rc_bus = sc->sc_busno;
203 		rc->rc_priority = 0;
204 		rc->rc_sense_len = htole32(sizeof(xs->sense.scsi_sense));
205 		rc->rc_sense_addr =
206 		    htole32(soff + icp->icp_scr_seg[0].ds_addr);
207 		rc->rc_padding1 = 0;
208 
209 		if (xs->datalen != 0) {
210 			rv = icp_ccb_map(icp, ic, xs->data, xs->datalen,
211 			   (flags & XS_CTL_DATA_IN) != 0 ? IC_XFER_IN :
212 			   IC_XFER_OUT);
213 			if (rv != 0) {
214 				icp_ccb_free(icp, ic);
215 				xs->error = XS_DRIVER_STUFFUP;
216 				scsipi_done(xs);
217 				return;
218 			}
219 
220 			rc->rc_nsgent = htole32(ic->ic_nsgent);
221 			rc->rc_sdata = ~0;
222 			rc->rc_sdlen = htole32(xs->datalen);
223 		} else {
224 			rc->rc_nsgent = 0;
225 			rc->rc_sdata = 0;
226 			rc->rc_sdlen = 0;
227 		}
228 
229 		ic->ic_cmdlen = (u_long)ic->ic_sg - (u_long)&ic->ic_cmd +
230 		    ic->ic_nsgent * sizeof(*ic->ic_sg);
231 
232 		bus_dmamap_sync(icp->icp_dmat, icp->icp_scr_dmamap, soff,
233 		    sizeof(xs->sense.scsi_sense), BUS_DMASYNC_PREREAD);
234 
235 		/*
236 		 * Fire it off to the controller.
237 		 */
238  		ic->ic_intr = icpsp_intr;
239 		ic->ic_context = xs;
240 		ic->ic_dv = &sc->sc_dv;
241 
242 		if ((flags & XS_CTL_POLL) != 0) {
243 			s = splbio();
244 			rv = icp_ccb_poll(icp, ic, xs->timeout);
245 			if (rv != 0) {
246 				if (xs->datalen != 0)
247 					icp_ccb_unmap(icp, ic);
248 				icp_ccb_free(icp, ic);
249 				xs->error = XS_TIMEOUT;
250 				scsipi_done(xs);
251 
252 				/*
253 				 * XXX We're now in a bad way, because we
254 				 * don't know how to abort the command.
255 				 * That shouldn't matter too much, since
256 				 * polled commands won't be used while the
257 				 * system is running.
258 				 */
259 			}
260 			splx(s);
261 		} else
262 			icp_ccb_enqueue(icp, ic);
263 
264 		break;
265 
266 	case ADAPTER_REQ_GROW_RESOURCES:
267 	case ADAPTER_REQ_SET_XFER_MODE:
268 		/*
269 		 * Neither of these cases are supported, and neither of them
270 		 * is particulatly relevant, since we have an abstract view
271 		 * of the bus; the controller takes care of all the nitty
272 		 * gritty.
273 		 */
274 		break;
275 	}
276 }
277 
278 void
279 icpsp_intr(struct icp_ccb *ic)
280 {
281 	struct scsipi_xfer *xs;
282 	struct icpsp_softc *sc;
283  	struct icp_softc *icp;
284  	int soff;
285 
286 	sc = (struct icpsp_softc *)ic->ic_dv;
287 	xs = (struct scsipi_xfer *)ic->ic_context;
288 	icp = (struct icp_softc *)device_parent(ic->ic_dv);
289 	soff = ICP_SCRATCH_SENSE + ic->ic_ident *
290 	    sizeof(struct scsi_sense_data);
291 
292 	SC_DEBUG(xs->xs_periph, SCSIPI_DB2, ("icpsp_intr\n"));
293 
294 	bus_dmamap_sync(icp->icp_dmat, icp->icp_scr_dmamap, soff,
295 	    sizeof(xs->sense.scsi_sense), BUS_DMASYNC_POSTREAD);
296 
297 	if (ic->ic_status == ICP_S_OK) {
298 		xs->status = SCSI_OK;
299 		xs->resid = 0;
300 	} else if (ic->ic_status != ICP_S_RAW_SCSI || icp->icp_info >= 0x100) {
301 		xs->error = XS_SELTIMEOUT;
302 		xs->resid = xs->datalen;
303 	} else {
304 		xs->status = icp->icp_info;
305 
306 		switch (xs->status) {
307 		case SCSI_OK:
308 #ifdef DIAGNOSTIC
309 			printf("%s: error return (%d), but SCSI_OK?\n",
310 			    sc->sc_dv.dv_xname, icp->icp_info);
311 #endif
312 			xs->resid = 0;
313 			break;
314 		case SCSI_CHECK:
315 			memcpy(&xs->sense.scsi_sense, icp->icp_scr + soff,
316 			    sizeof(xs->sense.scsi_sense));
317 			xs->error = XS_SENSE;
318 			/* FALLTHROUGH */
319 		default:
320 			/*
321 			 * XXX Don't know how to get residual count.
322 			 */
323 			xs->resid = xs->datalen;
324 			break;
325 		}
326 	}
327 
328 	if (xs->datalen != 0)
329 		icp_ccb_unmap(icp, ic);
330 	icp_ccb_free(icp, ic);
331 	scsipi_done(xs);
332 }
333 
334 void
335 icpsp_adjqparam(struct device *dv, int openings)
336 {
337 	struct icpsp_softc *sc = (struct icpsp_softc *) dv;
338 	int s;
339 
340 	s = splbio();
341 	sc->sc_adapter.adapt_openings += openings - sc->sc_openings;
342 	sc->sc_openings = openings;
343 	splx(s);
344 }
345