summaryrefslogtreecommitdiff
path: root/sys/dev/ic/icpsp.c
blob: 1bc4a4d282afb0e8e2c02ee7b90dc4cb5d28a663 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*	$NetBSD: icpsp.c,v 1.28 2021/08/07 16:19:12 thorpej Exp $	*/

/*-
 * Copyright (c) 2002 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Andrew Doran.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: icpsp.c,v 1.28 2021/08/07 16:19:12 thorpej Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/queue.h>
#include <sys/proc.h>
#include <sys/buf.h>
#include <sys/endian.h>
#include <sys/malloc.h>
#include <sys/scsiio.h>

#include <sys/bswap.h>
#include <sys/bus.h>

#include <dev/scsipi/scsi_all.h>
#include <dev/scsipi/scsi_disk.h>
#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsiconf.h>
#include <dev/scsipi/scsi_message.h>

#include <dev/ic/icpreg.h>
#include <dev/ic/icpvar.h>

struct icpsp_softc {
	device_t sc_dv;
	struct	scsipi_adapter sc_adapter;
	struct	scsipi_channel sc_channel;
	int	sc_busno;
	int	sc_openings;
};

void	icpsp_attach(device_t, device_t, void *);
void	icpsp_intr(struct icp_ccb *);
int	icpsp_match(device_t, cfdata_t, void *);
void	icpsp_scsipi_request(struct scsipi_channel *, scsipi_adapter_req_t,
			     void *);

void	icpsp_adjqparam(device_t, int);

CFATTACH_DECL_NEW(icpsp, sizeof(struct icpsp_softc),
    icpsp_match, icpsp_attach, NULL, NULL);

static const struct icp_servicecb icpsp_servicecb = {
	icpsp_adjqparam,
};

int
icpsp_match(device_t parent, cfdata_t match,
    void *aux)
{
	struct icp_attach_args *icpa;

	icpa = aux;

	return (icpa->icpa_unit >= ICPA_UNIT_SCSI);
}

void
icpsp_attach(device_t parent, device_t self, void *aux)
{
	struct icp_attach_args *icpa;
	struct icpsp_softc *sc;
	struct icp_softc *icp;

	icpa = (struct icp_attach_args *)aux;
	sc = device_private(self);
	icp = device_private(parent);

	sc->sc_dv = self;
	sc->sc_busno = icpa->icpa_unit - ICPA_UNIT_SCSI;
	sc->sc_openings = icp->icp_openings;
	printf(": physical SCSI channel %d\n", sc->sc_busno);

	icp_register_servicecb(icp, icpa->icpa_unit, &icpsp_servicecb);

	sc->sc_adapter.adapt_dev = sc->sc_dv;
	sc->sc_adapter.adapt_nchannels = 1;
	sc->sc_adapter.adapt_openings = icp->icp_openings;
	sc->sc_adapter.adapt_max_periph = icp->icp_openings;
	sc->sc_adapter.adapt_minphys = minphys;
	sc->sc_adapter.adapt_request = icpsp_scsipi_request;

	sc->sc_channel.chan_adapter = &sc->sc_adapter;
	sc->sc_channel.chan_bustype = &scsi_bustype;
	sc->sc_channel.chan_channel = 0;
	sc->sc_channel.chan_ntargets = ((icp->icp_class & ICP_FC) != 0 ?
	    127 : 16); /* XXX bogus check */
	sc->sc_channel.chan_nluns = 8;
	sc->sc_channel.chan_id = icp->icp_bus_id[sc->sc_busno];
	sc->sc_channel.chan_flags = SCSIPI_CHAN_NOSETTLE;

	config_found(self, &sc->sc_channel, scsiprint, CFARGS_NONE);
}

void
icpsp_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
		     void *arg)
{
	struct scsipi_xfer *xs;
	struct scsipi_periph *periph;
	struct icpsp_softc *sc;
	struct icp_rawcmd *rc;
	struct icp_softc *icp;
	struct icp_ccb *ic;
	int rv, flags, s, soff;

	sc = device_private(chan->chan_adapter->adapt_dev);
	icp = device_private(device_parent(sc->sc_dv));

	switch (req) {
	case ADAPTER_REQ_RUN_XFER:
		xs = arg;
		periph = xs->xs_periph;
		flags = xs->xs_control;

		SC_DEBUG(periph, SCSIPI_DB2, ("icpsp_scsi_request run_xfer\n"));

		if ((flags & XS_CTL_RESET) != 0) {
			/* XXX Unimplemented. */
			xs->error = XS_DRIVER_STUFFUP;
			scsipi_done(xs);
			return;
		}

#if defined(ICP_DEBUG) || defined(SCSIDEBUG)
		if (xs->cmdlen > sizeof(rc->rc_cdb))
			panic("%s: CDB too large", device_xname(sc->sc_dv));
#endif

		/*
		 * Allocate a CCB.
		 */
		if (__predict_false((ic = icp_ccb_alloc(icp)) == NULL)) {
			xs->error = XS_RESOURCE_SHORTAGE;
			scsipi_done(xs);
			return;
		}
		rc = &ic->ic_cmd.cmd_packet.rc;
		ic->ic_sg = rc->rc_sg;
		ic->ic_service = ICP_SCSIRAWSERVICE;
		soff = ICP_SCRATCH_SENSE + ic->ic_ident *
		    sizeof(struct scsi_sense_data);

		/*
		 * Build the command.  We don't need to actively prevent
		 * access to array components, since the controller kindly
		 * takes care of that for us.
		 */
		ic->ic_cmd.cmd_opcode = htole16(ICP_WRITE);
		memcpy(rc->rc_cdb, xs->cmd, xs->cmdlen);

		rc->rc_padding0 = 0;
		rc->rc_direction = htole32((flags & XS_CTL_DATA_IN) != 0 ?
		    ICP_DATA_IN : ICP_DATA_OUT);
		rc->rc_mdisc_time = 0;
		rc->rc_mcon_time = 0;
		rc->rc_clen = htole32(xs->cmdlen);
		rc->rc_target = periph->periph_target;
		rc->rc_lun = periph->periph_lun;
		rc->rc_bus = sc->sc_busno;
		rc->rc_priority = 0;
		rc->rc_sense_len = htole32(sizeof(xs->sense.scsi_sense));
		rc->rc_sense_addr =
		    htole32(soff + icp->icp_scr_seg[0].ds_addr);
		rc->rc_padding1 = 0;

		if (xs->datalen != 0) {
			rv = icp_ccb_map(icp, ic, xs->data, xs->datalen,
			   (flags & XS_CTL_DATA_IN) != 0 ? IC_XFER_IN :
			   IC_XFER_OUT);
			if (rv != 0) {
				icp_ccb_free(icp, ic);
				xs->error = XS_DRIVER_STUFFUP;
				scsipi_done(xs);
				return;
			}

			rc->rc_nsgent = htole32(ic->ic_nsgent);
			rc->rc_sdata = ~0;
			rc->rc_sdlen = htole32(xs->datalen);
		} else {
			rc->rc_nsgent = 0;
			rc->rc_sdata = 0;
			rc->rc_sdlen = 0;
		}

		ic->ic_cmdlen = (u_long)ic->ic_sg - (u_long)&ic->ic_cmd +
		    ic->ic_nsgent * sizeof(*ic->ic_sg);

		bus_dmamap_sync(icp->icp_dmat, icp->icp_scr_dmamap, soff,
		    sizeof(xs->sense.scsi_sense), BUS_DMASYNC_PREREAD);

		/*
		 * Fire it off to the controller.
		 */
 		ic->ic_intr = icpsp_intr;
		ic->ic_context = xs;
		ic->ic_dv = sc->sc_dv;

		if ((flags & XS_CTL_POLL) != 0) {
			s = splbio();
			rv = icp_ccb_poll(icp, ic, xs->timeout);
			if (rv != 0) {
				if (xs->datalen != 0)
					icp_ccb_unmap(icp, ic);
				icp_ccb_free(icp, ic);
				xs->error = XS_TIMEOUT;
				scsipi_done(xs);

				/*
				 * XXX We're now in a bad way, because we
				 * don't know how to abort the command.
				 * That shouldn't matter too much, since
				 * polled commands won't be used while the
				 * system is running.
				 */
			}
			splx(s);
		} else
			icp_ccb_enqueue(icp, ic);

		break;

	case ADAPTER_REQ_GROW_RESOURCES:
	case ADAPTER_REQ_SET_XFER_MODE:
		/*
		 * Neither of these cases are supported, and neither of them
		 * is particulatly relevant, since we have an abstract view
		 * of the bus; the controller takes care of all the nitty
		 * gritty.
		 */
		break;
	}
}

void
icpsp_intr(struct icp_ccb *ic)
{
	struct scsipi_xfer *xs;
 	struct icp_softc *icp;
 	int soff;

#ifdef DIAGNOSTIC
	struct icpsp_softc *sc = device_private(ic->ic_dv);
#endif
	xs = ic->ic_context;
	icp = device_private(device_parent(ic->ic_dv));
	soff = ICP_SCRATCH_SENSE + ic->ic_ident *
	    sizeof(struct scsi_sense_data);

	SC_DEBUG(xs->xs_periph, SCSIPI_DB2, ("icpsp_intr\n"));

	bus_dmamap_sync(icp->icp_dmat, icp->icp_scr_dmamap, soff,
	    sizeof(xs->sense.scsi_sense), BUS_DMASYNC_POSTREAD);

	if (ic->ic_status == ICP_S_OK) {
		xs->status = SCSI_OK;
		xs->resid = 0;
	} else if (ic->ic_status != ICP_S_RAW_SCSI || icp->icp_info >= 0x100) {
		xs->error = XS_SELTIMEOUT;
		xs->resid = xs->datalen;
	} else {
		xs->status = icp->icp_info;

		switch (xs->status) {
		case SCSI_OK:
#ifdef DIAGNOSTIC
			printf("%s: error return (%d), but SCSI_OK?\n",
			    device_xname(sc->sc_dv), icp->icp_info);
#endif
			xs->resid = 0;
			break;
		case SCSI_CHECK:
			memcpy(&xs->sense.scsi_sense,
			    (char *)icp->icp_scr + soff,
			    sizeof(xs->sense.scsi_sense));
			xs->error = XS_SENSE;
			/* FALLTHROUGH */
		default:
			/*
			 * XXX Don't know how to get residual count.
			 */
			xs->resid = xs->datalen;
			break;
		}
	}

	if (xs->datalen != 0)
		icp_ccb_unmap(icp, ic);
	icp_ccb_free(icp, ic);
	scsipi_done(xs);
}

void
icpsp_adjqparam(device_t self, int openings)
{
	struct icpsp_softc *sc = device_private(self);
	int s;

	s = splbio();
	sc->sc_adapter.adapt_openings += openings - sc->sc_openings;
	sc->sc_openings = openings;
	splx(s);
}