scsi_pt.c revision 115562
1/*
2 * Implementation of SCSI Processor Target Peripheral driver for CAM.
3 *
4 * Copyright (c) 1998 Justin T. Gibbs.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions, and the following disclaimer,
12 *    without modification, immediately at the beginning of the file.
13 * 2. The name of the author may not be used to endorse or promote products
14 *    derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: head/sys/cam/scsi/scsi_pt.c 115562 2003-05-31 20:46:21Z phk $
29 */
30
31#include <sys/param.h>
32#include <sys/queue.h>
33#include <sys/systm.h>
34#include <sys/kernel.h>
35#include <sys/types.h>
36#include <sys/bio.h>
37#include <sys/devicestat.h>
38#include <sys/malloc.h>
39#include <sys/conf.h>
40#include <sys/ptio.h>
41
42#include <cam/cam.h>
43#include <cam/cam_ccb.h>
44#include <cam/cam_periph.h>
45#include <cam/cam_xpt_periph.h>
46#include <cam/cam_debug.h>
47
48#include <cam/scsi/scsi_all.h>
49#include <cam/scsi/scsi_message.h>
50#include <cam/scsi/scsi_pt.h>
51
52#include "opt_pt.h"
53
54typedef enum {
55	PT_STATE_PROBE,
56	PT_STATE_NORMAL
57} pt_state;
58
59typedef enum {
60	PT_FLAG_NONE		= 0x00,
61	PT_FLAG_OPEN		= 0x01,
62	PT_FLAG_DEVICE_INVALID	= 0x02,
63	PT_FLAG_RETRY_UA	= 0x04
64} pt_flags;
65
66typedef enum {
67	PT_CCB_BUFFER_IO	= 0x01,
68	PT_CCB_WAITING		= 0x02,
69	PT_CCB_RETRY_UA		= 0x04,
70	PT_CCB_BUFFER_IO_UA	= PT_CCB_BUFFER_IO|PT_CCB_RETRY_UA
71} pt_ccb_state;
72
73/* Offsets into our private area for storing information */
74#define ccb_state	ppriv_field0
75#define ccb_bp		ppriv_ptr1
76
77struct pt_softc {
78	struct	 bio_queue_head bio_queue;
79	struct	 devstat *device_stats;
80	LIST_HEAD(, ccb_hdr) pending_ccbs;
81	pt_state state;
82	pt_flags flags;
83	union	 ccb saved_ccb;
84	int	 io_timeout;
85	dev_t	 dev;
86};
87
88static	d_open_t	ptopen;
89static	d_close_t	ptclose;
90static	d_strategy_t	ptstrategy;
91static	periph_init_t	ptinit;
92static	void		ptasync(void *callback_arg, u_int32_t code,
93				struct cam_path *path, void *arg);
94static	periph_ctor_t	ptctor;
95static	periph_oninv_t	ptoninvalidate;
96static	periph_dtor_t	ptdtor;
97static	periph_start_t	ptstart;
98static	void		ptdone(struct cam_periph *periph,
99			       union ccb *done_ccb);
100static	d_ioctl_t	ptioctl;
101static  int		pterror(union ccb *ccb, u_int32_t cam_flags,
102				u_int32_t sense_flags);
103
104void	scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
105			  void (*cbfcnp)(struct cam_periph *, union ccb *),
106			  u_int tag_action, int readop, u_int byte2,
107			  u_int32_t xfer_len, u_int8_t *data_ptr,
108			  u_int8_t sense_len, u_int32_t timeout);
109
110static struct periph_driver ptdriver =
111{
112	ptinit, "pt",
113	TAILQ_HEAD_INITIALIZER(ptdriver.units), /* generation */ 0
114};
115
116PERIPHDRIVER_DECLARE(pt, ptdriver);
117
118#define PT_CDEV_MAJOR 61
119
120static struct cdevsw pt_cdevsw = {
121	.d_open =	ptopen,
122	.d_close =	ptclose,
123	.d_read =	physread,
124	.d_write =	physwrite,
125	.d_ioctl =	ptioctl,
126	.d_strategy =	ptstrategy,
127	.d_name =	"pt",
128	.d_maj =	PT_CDEV_MAJOR,
129};
130
131#ifndef SCSI_PT_DEFAULT_TIMEOUT
132#define SCSI_PT_DEFAULT_TIMEOUT		60
133#endif
134
135static int
136ptopen(dev_t dev, int flags, int fmt, struct thread *td)
137{
138	struct cam_periph *periph;
139	struct pt_softc *softc;
140	int unit;
141	int error;
142	int s;
143
144	unit = minor(dev);
145	periph = (struct cam_periph *)dev->si_drv1;
146	if (periph == NULL)
147		return (ENXIO);
148
149	softc = (struct pt_softc *)periph->softc;
150
151	s = splsoftcam();
152	if (softc->flags & PT_FLAG_DEVICE_INVALID) {
153		splx(s);
154		return(ENXIO);
155	}
156
157	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
158	    ("ptopen: dev=%s (unit %d)\n", devtoname(dev), unit));
159
160	if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
161		splx(s);
162		return (error); /* error code from tsleep */
163	}
164
165	splx(s);
166
167	if ((softc->flags & PT_FLAG_OPEN) == 0) {
168		if (cam_periph_acquire(periph) != CAM_REQ_CMP)
169			error = ENXIO;
170		else
171			softc->flags |= PT_FLAG_OPEN;
172	} else
173		error = EBUSY;
174
175	cam_periph_unlock(periph);
176	return (error);
177}
178
179static int
180ptclose(dev_t dev, int flag, int fmt, struct thread *td)
181{
182	struct	cam_periph *periph;
183	struct	pt_softc *softc;
184	int	error;
185
186	periph = (struct cam_periph *)dev->si_drv1;
187	if (periph == NULL)
188		return (ENXIO);
189
190	softc = (struct pt_softc *)periph->softc;
191
192	if ((error = cam_periph_lock(periph, PRIBIO)) != 0)
193		return (error); /* error code from tsleep */
194
195	softc->flags &= ~PT_FLAG_OPEN;
196	cam_periph_unlock(periph);
197	cam_periph_release(periph);
198	return (0);
199}
200
201/*
202 * Actually translate the requested transfer into one the physical driver
203 * can understand.  The transfer is described by a buf and will include
204 * only one physical transfer.
205 */
206static void
207ptstrategy(struct bio *bp)
208{
209	struct cam_periph *periph;
210	struct pt_softc *softc;
211	int    s;
212
213	periph = (struct cam_periph *)bp->bio_dev->si_drv1;
214	bp->bio_resid = bp->bio_bcount;
215	if (periph == NULL) {
216		biofinish(bp, NULL, ENXIO);
217		return;
218	}
219	softc = (struct pt_softc *)periph->softc;
220
221	/*
222	 * Mask interrupts so that the pack cannot be invalidated until
223	 * after we are in the queue.  Otherwise, we might not properly
224	 * clean up one of the buffers.
225	 */
226	s = splbio();
227
228	/*
229	 * If the device has been made invalid, error out
230	 */
231	if ((softc->flags & PT_FLAG_DEVICE_INVALID)) {
232		splx(s);
233		biofinish(bp, NULL, ENXIO);
234		return;
235	}
236
237	/*
238	 * Place it in the queue of disk activities for this disk
239	 */
240	bioq_insert_tail(&softc->bio_queue, bp);
241
242	splx(s);
243
244	/*
245	 * Schedule ourselves for performing the work.
246	 */
247	xpt_schedule(periph, /* XXX priority */1);
248
249	return;
250}
251
252static void
253ptinit(void)
254{
255	cam_status status;
256	struct cam_path *path;
257
258	/*
259	 * Install a global async callback.  This callback will
260	 * receive async callbacks like "new device found".
261	 */
262	status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
263				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
264
265	if (status == CAM_REQ_CMP) {
266		struct ccb_setasync csa;
267
268                xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
269                csa.ccb_h.func_code = XPT_SASYNC_CB;
270                csa.event_enable = AC_FOUND_DEVICE;
271                csa.callback = ptasync;
272                csa.callback_arg = NULL;
273                xpt_action((union ccb *)&csa);
274		status = csa.ccb_h.status;
275                xpt_free_path(path);
276        }
277
278	if (status != CAM_REQ_CMP) {
279		printf("pt: Failed to attach master async callback "
280		       "due to status 0x%x!\n", status);
281	}
282}
283
284static cam_status
285ptctor(struct cam_periph *periph, void *arg)
286{
287	struct pt_softc *softc;
288	struct ccb_setasync csa;
289	struct ccb_getdev *cgd;
290
291	cgd = (struct ccb_getdev *)arg;
292	if (periph == NULL) {
293		printf("ptregister: periph was NULL!!\n");
294		return(CAM_REQ_CMP_ERR);
295	}
296
297	if (cgd == NULL) {
298		printf("ptregister: no getdev CCB, can't register device\n");
299		return(CAM_REQ_CMP_ERR);
300	}
301
302	softc = (struct pt_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
303
304	if (softc == NULL) {
305		printf("daregister: Unable to probe new device. "
306		       "Unable to allocate softc\n");
307		return(CAM_REQ_CMP_ERR);
308	}
309
310	bzero(softc, sizeof(*softc));
311	LIST_INIT(&softc->pending_ccbs);
312	softc->state = PT_STATE_NORMAL;
313	bioq_init(&softc->bio_queue);
314
315	softc->io_timeout = SCSI_PT_DEFAULT_TIMEOUT * 1000;
316
317	periph->softc = softc;
318
319	softc->device_stats = devstat_new_entry("pt",
320			  periph->unit_number, 0,
321			  DEVSTAT_NO_BLOCKSIZE,
322			  SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
323			  DEVSTAT_PRIORITY_OTHER);
324
325	softc->dev = make_dev(&pt_cdevsw, periph->unit_number, UID_ROOT,
326			      GID_OPERATOR, 0600, "%s%d", periph->periph_name,
327			      periph->unit_number);
328	softc->dev->si_drv1 = periph;
329
330	/*
331	 * Add async callbacks for bus reset and
332	 * bus device reset calls.  I don't bother
333	 * checking if this fails as, in most cases,
334	 * the system will function just fine without
335	 * them and the only alternative would be to
336	 * not attach the device on failure.
337	 */
338	xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
339	csa.ccb_h.func_code = XPT_SASYNC_CB;
340	csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
341	csa.callback = ptasync;
342	csa.callback_arg = periph;
343	xpt_action((union ccb *)&csa);
344
345	/* Tell the user we've attached to the device */
346	xpt_announce_periph(periph, NULL);
347
348	return(CAM_REQ_CMP);
349}
350
351static void
352ptoninvalidate(struct cam_periph *periph)
353{
354	int s;
355	struct pt_softc *softc;
356	struct ccb_setasync csa;
357
358	softc = (struct pt_softc *)periph->softc;
359
360	/*
361	 * De-register any async callbacks.
362	 */
363	xpt_setup_ccb(&csa.ccb_h, periph->path,
364		      /* priority */ 5);
365	csa.ccb_h.func_code = XPT_SASYNC_CB;
366	csa.event_enable = 0;
367	csa.callback = ptasync;
368	csa.callback_arg = periph;
369	xpt_action((union ccb *)&csa);
370
371	softc->flags |= PT_FLAG_DEVICE_INVALID;
372
373	/*
374	 * Although the oninvalidate() routines are always called at
375	 * splsoftcam, we need to be at splbio() here to keep the buffer
376	 * queue from being modified while we traverse it.
377	 */
378	s = splbio();
379
380	/*
381	 * Return all queued I/O with ENXIO.
382	 * XXX Handle any transactions queued to the card
383	 *     with XPT_ABORT_CCB.
384	 */
385	bioq_flush(&softc->bio_queue, NULL, ENXIO);
386
387	splx(s);
388
389	xpt_print_path(periph->path);
390	printf("lost device\n");
391}
392
393static void
394ptdtor(struct cam_periph *periph)
395{
396	struct pt_softc *softc;
397
398	softc = (struct pt_softc *)periph->softc;
399
400	devstat_remove_entry(softc->device_stats);
401
402	destroy_dev(softc->dev);
403
404	xpt_print_path(periph->path);
405	printf("removing device entry\n");
406	free(softc, M_DEVBUF);
407}
408
409static void
410ptasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
411{
412	struct cam_periph *periph;
413
414	periph = (struct cam_periph *)callback_arg;
415	switch (code) {
416	case AC_FOUND_DEVICE:
417	{
418		struct ccb_getdev *cgd;
419		cam_status status;
420
421		cgd = (struct ccb_getdev *)arg;
422		if (cgd == NULL)
423			break;
424
425		if (SID_TYPE(&cgd->inq_data) != T_PROCESSOR)
426			break;
427
428		/*
429		 * Allocate a peripheral instance for
430		 * this device and start the probe
431		 * process.
432		 */
433		status = cam_periph_alloc(ptctor, ptoninvalidate, ptdtor,
434					  ptstart, "pt", CAM_PERIPH_BIO,
435					  cgd->ccb_h.path, ptasync,
436					  AC_FOUND_DEVICE, cgd);
437
438		if (status != CAM_REQ_CMP
439		 && status != CAM_REQ_INPROG)
440			printf("ptasync: Unable to attach to new device "
441				"due to status 0x%x\n", status);
442		break;
443	}
444	case AC_SENT_BDR:
445	case AC_BUS_RESET:
446	{
447		struct pt_softc *softc;
448		struct ccb_hdr *ccbh;
449		int s;
450
451		softc = (struct pt_softc *)periph->softc;
452		s = splsoftcam();
453		/*
454		 * Don't fail on the expected unit attention
455		 * that will occur.
456		 */
457		softc->flags |= PT_FLAG_RETRY_UA;
458		LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
459			ccbh->ccb_state |= PT_CCB_RETRY_UA;
460		splx(s);
461	}
462	/* FALLTHROUGH */
463	default:
464		cam_periph_async(periph, code, path, arg);
465		break;
466	}
467}
468
469static void
470ptstart(struct cam_periph *periph, union ccb *start_ccb)
471{
472	struct pt_softc *softc;
473	struct bio *bp;
474	int s;
475
476	softc = (struct pt_softc *)periph->softc;
477
478	/*
479	 * See if there is a buf with work for us to do..
480	 */
481	s = splbio();
482	bp = bioq_first(&softc->bio_queue);
483	if (periph->immediate_priority <= periph->pinfo.priority) {
484		CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
485				("queuing for immediate ccb\n"));
486		start_ccb->ccb_h.ccb_state = PT_CCB_WAITING;
487		SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
488				  periph_links.sle);
489		periph->immediate_priority = CAM_PRIORITY_NONE;
490		splx(s);
491		wakeup(&periph->ccb_list);
492	} else if (bp == NULL) {
493		splx(s);
494		xpt_release_ccb(start_ccb);
495	} else {
496		int oldspl;
497
498		bioq_remove(&softc->bio_queue, bp);
499
500		devstat_start_transaction_bio(softc->device_stats, bp);
501
502		scsi_send_receive(&start_ccb->csio,
503				  /*retries*/4,
504				  ptdone,
505				  MSG_SIMPLE_Q_TAG,
506				  bp->bio_cmd == BIO_READ,
507				  /*byte2*/0,
508				  bp->bio_bcount,
509				  bp->bio_data,
510				  /*sense_len*/SSD_FULL_SIZE,
511				  /*timeout*/softc->io_timeout);
512
513		start_ccb->ccb_h.ccb_state = PT_CCB_BUFFER_IO_UA;
514
515		/*
516		 * Block out any asyncronous callbacks
517		 * while we touch the pending ccb list.
518		 */
519		oldspl = splcam();
520		LIST_INSERT_HEAD(&softc->pending_ccbs, &start_ccb->ccb_h,
521				 periph_links.le);
522		splx(oldspl);
523
524		start_ccb->ccb_h.ccb_bp = bp;
525		bp = bioq_first(&softc->bio_queue);
526		splx(s);
527
528		xpt_action(start_ccb);
529
530		if (bp != NULL) {
531			/* Have more work to do, so ensure we stay scheduled */
532			xpt_schedule(periph, /* XXX priority */1);
533		}
534	}
535}
536
537static void
538ptdone(struct cam_periph *periph, union ccb *done_ccb)
539{
540	struct pt_softc *softc;
541	struct ccb_scsiio *csio;
542
543	softc = (struct pt_softc *)periph->softc;
544	csio = &done_ccb->csio;
545	switch (csio->ccb_h.ccb_state) {
546	case PT_CCB_BUFFER_IO:
547	case PT_CCB_BUFFER_IO_UA:
548	{
549		struct bio *bp;
550		int    oldspl;
551
552		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
553		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
554			int error;
555			int s;
556			int sf;
557
558			if ((csio->ccb_h.ccb_state & PT_CCB_RETRY_UA) != 0)
559				sf = SF_RETRY_UA;
560			else
561				sf = 0;
562
563			error = pterror(done_ccb, CAM_RETRY_SELTO, sf);
564			if (error == ERESTART) {
565				/*
566				 * A retry was scheuled, so
567				 * just return.
568				 */
569				return;
570			}
571			if (error != 0) {
572				s = splbio();
573
574				if (error == ENXIO) {
575					/*
576					 * Catastrophic error.  Mark our device
577					 * as invalid.
578					 */
579					xpt_print_path(periph->path);
580					printf("Invalidating device\n");
581					softc->flags |= PT_FLAG_DEVICE_INVALID;
582				}
583
584				/*
585				 * return all queued I/O with EIO, so that
586				 * the client can retry these I/Os in the
587				 * proper order should it attempt to recover.
588				 */
589				bioq_flush(&softc->bio_queue, NULL, EIO);
590				splx(s);
591				bp->bio_error = error;
592				bp->bio_resid = bp->bio_bcount;
593				bp->bio_flags |= BIO_ERROR;
594			} else {
595				bp->bio_resid = csio->resid;
596				bp->bio_error = 0;
597				if (bp->bio_resid != 0) {
598					/* Short transfer ??? */
599					bp->bio_flags |= BIO_ERROR;
600				}
601			}
602			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
603				cam_release_devq(done_ccb->ccb_h.path,
604						 /*relsim_flags*/0,
605						 /*reduction*/0,
606						 /*timeout*/0,
607						 /*getcount_only*/0);
608		} else {
609			bp->bio_resid = csio->resid;
610			if (bp->bio_resid != 0)
611				bp->bio_flags |= BIO_ERROR;
612		}
613
614		/*
615		 * Block out any asyncronous callbacks
616		 * while we touch the pending ccb list.
617		 */
618		oldspl = splcam();
619		LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
620		splx(oldspl);
621
622		biofinish(bp, softc->device_stats, 0);
623		break;
624	}
625	case PT_CCB_WAITING:
626		/* Caller will release the CCB */
627		wakeup(&done_ccb->ccb_h.cbfcnp);
628		return;
629	}
630	xpt_release_ccb(done_ccb);
631}
632
633static int
634pterror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
635{
636	struct pt_softc	  *softc;
637	struct cam_periph *periph;
638
639	periph = xpt_path_periph(ccb->ccb_h.path);
640	softc = (struct pt_softc *)periph->softc;
641
642	return(cam_periph_error(ccb, cam_flags, sense_flags,
643				&softc->saved_ccb));
644}
645
646static int
647ptioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
648{
649	struct cam_periph *periph;
650	struct pt_softc *softc;
651	int error;
652
653	periph = (struct cam_periph *)dev->si_drv1;
654	if (periph == NULL)
655		return(ENXIO);
656
657	softc = (struct pt_softc *)periph->softc;
658
659	if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
660		return (error); /* error code from tsleep */
661	}
662
663	switch(cmd) {
664	case PTIOCGETTIMEOUT:
665		if (softc->io_timeout >= 1000)
666			*(int *)addr = softc->io_timeout / 1000;
667		else
668			*(int *)addr = 0;
669		break;
670	case PTIOCSETTIMEOUT:
671	{
672		int s;
673
674		if (*(int *)addr < 1) {
675			error = EINVAL;
676			break;
677		}
678
679		s = splsoftcam();
680		softc->io_timeout = *(int *)addr * 1000;
681		splx(s);
682
683		break;
684	}
685	default:
686		error = cam_periph_ioctl(periph, cmd, addr, pterror);
687		break;
688	}
689
690	cam_periph_unlock(periph);
691
692	return(error);
693}
694
695void
696scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
697		  void (*cbfcnp)(struct cam_periph *, union ccb *),
698		  u_int tag_action, int readop, u_int byte2,
699		  u_int32_t xfer_len, u_int8_t *data_ptr, u_int8_t sense_len,
700		  u_int32_t timeout)
701{
702	struct scsi_send_receive *scsi_cmd;
703
704	scsi_cmd = (struct scsi_send_receive *)&csio->cdb_io.cdb_bytes;
705	scsi_cmd->opcode = readop ? RECEIVE : SEND;
706	scsi_cmd->byte2 = byte2;
707	scsi_ulto3b(xfer_len, scsi_cmd->xfer_len);
708	scsi_cmd->control = 0;
709
710	cam_fill_csio(csio,
711		      retries,
712		      cbfcnp,
713		      /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT,
714		      tag_action,
715		      data_ptr,
716		      xfer_len,
717		      sense_len,
718		      sizeof(*scsi_cmd),
719		      timeout);
720}
721