amr_cam.c revision 105419
1/*-
2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
4 * 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 AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * 3. The party using or redistributing the source code and binary forms
28 *    agrees to the above disclaimer and the terms and conditions set forth
29 *    herein.
30 *
31 * Additional Copyright (c) 2002 by Eric Moore under same license.
32 * Additional Copyright (c) 2002 LSI Logic Corporation
33 *
34 *	$FreeBSD: head/sys/dev/amr/amr_cam.c 105419 2002-10-18 21:29:14Z emoore $
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/malloc.h>
40#include <sys/kernel.h>
41
42#include <dev/amr/amr_compat.h>
43#include <sys/bus.h>
44#include <sys/conf.h>
45#include <sys/devicestat.h>
46#include <sys/disk.h>
47#include <sys/stat.h>
48
49#include <cam/cam.h>
50#include <cam/cam_ccb.h>
51#include <cam/cam_sim.h>
52#include <cam/cam_xpt.h>
53#include <cam/cam_xpt_sim.h>
54#include <cam/cam_debug.h>
55#include <cam/scsi/scsi_all.h>
56#include <cam/scsi/scsi_message.h>
57
58#include <machine/resource.h>
59#include <machine/bus.h>
60
61#include <dev/amr/amrreg.h>
62#include <dev/amr/amrvar.h>
63
64static void		amr_cam_action(struct cam_sim *sim, union ccb *ccb);
65static void		amr_cam_poll(struct cam_sim *sim);
66static void		amr_cam_complete(struct amr_command *ac);
67
68
69/********************************************************************************
70 * Enqueue/dequeue functions
71 */
72static __inline void
73amr_enqueue_ccb(struct amr_softc *sc, union ccb *ccb)
74{
75    int		s;
76
77    s = splbio();
78    TAILQ_INSERT_TAIL(&sc->amr_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
79    splx(s);
80}
81
82static __inline void
83amr_requeue_ccb(struct amr_softc *sc, union ccb *ccb)
84{
85    int		s;
86
87    s = splbio();
88    TAILQ_INSERT_HEAD(&sc->amr_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
89    splx(s);
90}
91
92static __inline union ccb *
93amr_dequeue_ccb(struct amr_softc *sc)
94{
95    union ccb	*ccb;
96    int		s;
97
98    s = splbio();
99    if ((ccb = (union ccb *)TAILQ_FIRST(&sc->amr_cam_ccbq)) != NULL)
100	TAILQ_REMOVE(&sc->amr_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
101    splx(s);
102    return(ccb);
103}
104
105/********************************************************************************
106 * Attach our 'real' SCSI channels to CAM
107 */
108int
109amr_cam_attach(struct amr_softc *sc)
110{
111    struct cam_devq	*devq;
112    int			chn;
113
114    /* initialise the ccb queue */
115    TAILQ_INIT(&sc->amr_cam_ccbq);
116
117    /*
118     * Allocate a devq for all our channels combined.  This should
119     * allow for the maximum number of SCSI commands we will accept
120     * at one time.
121     */
122    if ((devq = cam_simq_alloc(AMR_MAX_SCSI_CMDS)) == NULL)
123	return(ENOMEM);
124
125    /*
126     * Iterate over our channels, registering them with CAM
127     */
128    for (chn = 0; chn < sc->amr_maxchan; chn++) {
129
130	/* allocate a sim */
131	if ((sc->amr_cam_sim[chn] = cam_sim_alloc(amr_cam_action,
132						  amr_cam_poll,
133						  "amr",
134						  sc,
135						  device_get_unit(sc->amr_dev),
136						  1,
137						  AMR_MAX_SCSI_CMDS,
138						  devq)) == NULL) {
139	    cam_simq_free(devq);
140	    device_printf(sc->amr_dev, "CAM SIM attach failed\n");
141	    return(ENOMEM);
142	}
143
144	/* register the bus ID so we can get it later */
145	if (xpt_bus_register(sc->amr_cam_sim[chn], chn)) {
146	    device_printf(sc->amr_dev, "CAM XPT bus registration failed\n");
147	    return(ENXIO);
148	}
149    }
150    /*
151     * XXX we should scan the config and work out which devices are actually
152     * protected.
153     */
154    return(0);
155}
156
157/********************************************************************************
158 * Disconnect ourselves from CAM
159 */
160void
161amr_cam_detach(struct amr_softc *sc)
162{
163    int		chn, first;
164
165    for (chn = 0, first = 1; chn < sc->amr_maxchan; chn++) {
166
167	/*
168	 * If a sim was allocated for this channel, free it
169	 */
170	if (sc->amr_cam_sim[chn] != NULL) {
171	    xpt_bus_deregister(cam_sim_path(sc->amr_cam_sim[chn]));
172	    cam_sim_free(sc->amr_cam_sim[chn], first ? TRUE : FALSE);
173	    first = 0;
174	}
175    }
176}
177
178/********************************************************************************
179 ********************************************************************************
180                                                        CAM passthrough interface
181 ********************************************************************************
182 ********************************************************************************/
183
184/********************************************************************************
185 * Handle a request for action from CAM
186 */
187static void
188amr_cam_action(struct cam_sim *sim, union ccb *ccb)
189{
190    struct amr_softc	*sc = cam_sim_softc(sim);
191
192    switch(ccb->ccb_h.func_code) {
193
194    /*
195     * Perform SCSI I/O to a physical device.
196     */
197    case XPT_SCSI_IO:
198    {
199	struct ccb_hdr		*ccbh = &ccb->ccb_h;
200	struct ccb_scsiio	*csio = &ccb->csio;
201
202	/* Validate the CCB */
203	ccbh->status = CAM_REQ_INPROG;
204
205	/* check the CDB length */
206	if (csio->cdb_len > AMR_MAX_CDB_LEN)
207	    ccbh->status = CAM_REQ_CMP_ERR;
208
209	/* check that the CDB pointer is not to a physical address */
210	if ((ccbh->flags & CAM_CDB_POINTER) && (ccbh->flags & CAM_CDB_PHYS))
211	    ccbh->status = CAM_REQ_CMP_ERR;
212
213	/* if there is data transfer, it must be to/from a virtual address */
214	if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
215	    if (ccbh->flags & CAM_DATA_PHYS)		/* we can't map it */
216		ccbh->status = CAM_REQ_CMP_ERR;
217	    if (ccbh->flags & CAM_SCATTER_VALID)	/* we want to do the s/g setup */
218		ccbh->status = CAM_REQ_CMP_ERR;
219	}
220
221	/*
222	 * If the command is to a LUN other than 0, fail it.
223	 * This is probably incorrect, but during testing the firmware did not
224	 * seem to respect the LUN field, and thus devices appear echoed.
225	 */
226	if (csio->ccb_h.target_lun != 0)
227	    ccbh->status = CAM_REQ_CMP_ERR;
228
229	/* if we're happy with the request, queue it for attention */
230	if (ccbh->status == CAM_REQ_INPROG) {
231
232	    /* save the channel number in the ccb */
233	    csio->ccb_h.sim_priv.entries[0].field = cam_sim_bus(sim);
234
235	    amr_enqueue_ccb(sc, ccb);
236	    amr_startio(sc);
237	    return;
238	}
239	break;
240    }
241
242    case XPT_CALC_GEOMETRY:
243    {
244	struct    ccb_calc_geometry *ccg = &ccb->ccg;
245	u_int32_t size_in_mb;
246	u_int32_t secs_per_cylinder;
247
248	size_in_mb = ccg->volume_size / ((1024L * 1024L) / ccg->block_size);
249
250	if (size_in_mb > 1024) {
251	    ccg->heads = 255;
252	    ccg->secs_per_track = 63;
253	} else {
254	    ccg->heads = 64;
255	    ccg->secs_per_track = 32;
256	}
257	secs_per_cylinder = ccg->heads * ccg->secs_per_track;
258	ccg->cylinders = ccg->volume_size / secs_per_cylinder;
259	ccb->ccb_h.status = CAM_REQ_CMP;
260	break;
261    }
262
263    /*
264     * Return path stats.  Some of these should probably be
265     * amended.
266     */
267    case XPT_PATH_INQ:
268    {
269	struct ccb_pathinq      *cpi = & ccb->cpi;
270
271	debug(3, "XPT_PATH_INQ");
272	cpi->version_num = 1;           /* XXX??? */
273	cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
274	cpi->target_sprt = 0;
275	cpi->hba_misc = PIM_NOBUSRESET;
276	cpi->hba_eng_cnt = 0;
277	cpi->max_target = AMR_MAX_TARGETS;
278	cpi->max_lun = 0 /* AMR_MAX_LUNS*/;
279	cpi->initiator_id = 7;          /* XXX variable? */
280	strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
281	strncpy(cpi->hba_vid, "LSI", HBA_IDLEN);
282	strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
283	cpi->unit_number = cam_sim_unit(sim);
284	cpi->bus_id = cam_sim_bus(sim);
285	cpi->base_transfer_speed = 132 * 1024;  /* XXX get from controller? */
286	cpi->ccb_h.status = CAM_REQ_CMP;
287
288	break;
289    }
290
291    case XPT_RESET_BUS:
292    {
293	struct ccb_pathinq	*cpi = & ccb->cpi;
294
295	debug(1, "XPT_RESET_BUS");
296	cpi->ccb_h.status = CAM_REQ_CMP;
297	break;
298    }
299
300    case XPT_RESET_DEV:
301    {
302	debug(1, "XPT_RESET_DEV");
303	ccb->ccb_h.status = CAM_REQ_CMP;
304	break;
305    }
306
307    case XPT_GET_TRAN_SETTINGS:
308    {
309	struct ccb_trans_settings	*cts;
310
311	debug(3, "XPT_GET_TRAN_SETTINGS");
312
313	cts = &(ccb->cts);
314
315	if ((cts->flags & CCB_TRANS_USER_SETTINGS) == 0) {
316		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
317		break;
318        }
319
320	cts->flags = CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB;
321	cts->bus_width = MSG_EXT_WDTR_BUS_32_BIT;
322	cts->sync_period = 6;   /* 40MHz how wide is this bus? */
323	cts->sync_offset = 31;  /* How to extract this from board? */
324
325	cts->valid = CCB_TRANS_SYNC_RATE_VALID
326	    | CCB_TRANS_SYNC_OFFSET_VALID
327	    | CCB_TRANS_BUS_WIDTH_VALID
328	    | CCB_TRANS_DISC_VALID
329	    | CCB_TRANS_TQ_VALID;
330	ccb->ccb_h.status = CAM_REQ_CMP;
331	break;
332    }
333
334    case XPT_SET_TRAN_SETTINGS:
335	debug(3, "XPT_SET_TRAN_SETTINGS");
336	ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
337	break;
338
339
340    /*
341     * Reject anything else as unsupported.
342     */
343    default:
344	/* we can't do this */
345	ccb->ccb_h.status = CAM_REQ_INVALID;
346	break;
347    }
348    xpt_done(ccb);
349}
350
351/********************************************************************************
352 * Convert a CAM CCB off the top of the CCB queue to a passthrough SCSI command.
353 */
354int
355amr_cam_command(struct amr_softc *sc, struct amr_command **acp)
356{
357    struct amr_command		*ac;
358    struct amr_passthrough	*ap;
359    struct ccb_scsiio		*csio;
360    int				bus, target, error;
361
362    error = 0;
363    ac = NULL;
364    ap = NULL;
365
366    /* check to see if there is a ccb for us to work with */
367    if ((csio = (struct ccb_scsiio *)amr_dequeue_ccb(sc)) == NULL)
368	goto out;
369
370    /* get bus/target, XXX validate against protected devices? */
371    bus = csio->ccb_h.sim_priv.entries[0].field;
372    target = csio->ccb_h.target_id;
373
374    /*
375     * Build a passthrough command.
376     */
377
378    /* construct passthrough */
379    if ((ap = malloc(sizeof(*ap), M_DEVBUF, M_NOWAIT | M_ZERO)) == NULL) {
380	error = ENOMEM;
381	goto out;
382    }
383    ap->ap_timeout = 0;
384    ap->ap_ars = 1;
385    ap->ap_request_sense_length = 14;
386    ap->ap_islogical = 0;
387    ap->ap_channel = bus;
388    ap->ap_scsi_id = target;
389    ap->ap_logical_drive_no = csio->ccb_h.target_lun;
390    ap->ap_cdb_length = csio->cdb_len;
391    ap->ap_data_transfer_length = csio->dxfer_len;
392    if (csio->ccb_h.flags & CAM_CDB_POINTER) {
393	bcopy(csio->cdb_io.cdb_ptr, ap->ap_cdb, csio->cdb_len);
394    } else {
395	bcopy(csio->cdb_io.cdb_bytes, ap->ap_cdb, csio->cdb_len);
396    }
397    /* we leave the data s/g list and s/g count to the map routine later */
398
399    debug(2, " COMMAND %x/%d+%d to %d:%d:%d", ap->ap_cdb[0], ap->ap_cdb_length, csio->dxfer_len,
400	  ap->ap_channel, ap->ap_scsi_id, ap->ap_logical_drive_no);
401
402    /* construct command */
403    if ((ac = amr_alloccmd(sc)) == NULL) {
404	error = ENOMEM;
405	goto out;
406    }
407
408    ac->ac_data = ap;
409    ac->ac_length = sizeof(*ap);
410    ac->ac_flags |= AMR_CMD_DATAOUT;
411
412    ac->ac_ccb_data = csio->data_ptr;
413    ac->ac_ccb_length = csio->dxfer_len;
414    if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
415	ac->ac_flags |= AMR_CMD_CCB_DATAIN;
416    if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
417	ac->ac_flags |= AMR_CMD_CCB_DATAOUT;
418
419    ac->ac_complete = amr_cam_complete;
420    ac->ac_private = csio;
421    ac->ac_mailbox.mb_command = AMR_CMD_PASS;
422
423out:
424    if (error != 0) {
425	if (ac != NULL)
426	    amr_releasecmd(ac);
427	if (ap != NULL)
428	    free(ap, M_DEVBUF);
429	if (csio != NULL)			/* put it back and try again later */
430	    amr_requeue_ccb(sc, (union ccb *)csio);
431    }
432    *acp = ac;
433    return(error);
434}
435
436/********************************************************************************
437 * Check for interrupt status
438 */
439static void
440amr_cam_poll(struct cam_sim *sim)
441{
442    amr_done(cam_sim_softc(sim));
443}
444
445/********************************************************************************
446 * Handle completion of a command submitted via CAM.
447 */
448static void
449amr_cam_complete(struct amr_command *ac)
450{
451    struct amr_passthrough      *ap = (struct amr_passthrough *)ac->ac_data;
452    struct ccb_scsiio           *csio = (struct ccb_scsiio *)ac->ac_private;
453    struct scsi_inquiry_data    *inq = (struct scsi_inquiry_data *)csio->data_ptr;
454
455    /* XXX note that we're ignoring ac->ac_status - good idea? */
456
457    debug(1, "status 0x%x  scsi_status 0x%x", ac->ac_status, ap->ap_scsi_status);
458
459    /*
460     * Hide disks from CAM so that they're not picked up and treated as 'normal' disks.
461     *
462     * If the configuration provides a mechanism to mark a disk a "not managed", we
463     * could add handling for that to allow disks to be selectively visible.
464     */
465
466    if ((ap->ap_cdb[0] == INQUIRY) && (SID_TYPE(inq) == T_DIRECT)) {
467	bzero(csio->data_ptr, csio->dxfer_len);
468	if (ap->ap_scsi_status == 0xf0) {
469	    csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
470	} else {
471	    csio->ccb_h.status = CAM_DEV_NOT_THERE;
472	}
473    } else {
474
475	/* handle passthrough SCSI status */
476	switch(ap->ap_scsi_status) {
477	case 0:				/* completed OK */
478	    csio->ccb_h.status = CAM_REQ_CMP;
479	    break;
480
481	case 0x02:
482	    csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
483	    csio->scsi_status = SCSI_STATUS_CHECK_COND;
484	    bcopy(ap->ap_request_sense_area, &csio->sense_data, AMR_MAX_REQ_SENSE_LEN);
485	    csio->sense_len = AMR_MAX_REQ_SENSE_LEN;
486	    csio->ccb_h.status |= CAM_AUTOSNS_VALID;
487	    break;
488
489	case 0x08:
490	    csio->ccb_h.status = CAM_SCSI_BUSY;
491	    break;
492
493	case 0xf0:
494	case 0xf4:
495	default:
496	    csio->ccb_h.status = CAM_REQ_CMP_ERR;
497	    break;
498	}
499    }
500    free(ap, M_DEVBUF);
501    if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)
502	debug(2, "%*D\n", imin(csio->dxfer_len, 16), csio->data_ptr, " ");
503    xpt_done((union ccb *)csio);
504    amr_releasecmd(ac);
505}
506
507