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