xref: /freebsd/sys/cam/scsi/scsi_pass.c (revision ef9ffb8594eee294334ced627755bf5b46b48f9f)
1898b0535SWarner Losh /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3bec9534dSPedro F. Giffuni  *
43393f8daSKenneth D. Merry  * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs.
52a888f93SKenneth D. Merry  * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
676babe50SJustin T. Gibbs  * All rights reserved.
776babe50SJustin T. Gibbs  *
876babe50SJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
976babe50SJustin T. Gibbs  * modification, are permitted provided that the following conditions
1076babe50SJustin T. Gibbs  * are met:
1176babe50SJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
1276babe50SJustin T. Gibbs  *    notice, this list of conditions, and the following disclaimer,
1376babe50SJustin T. Gibbs  *    without modification, immediately at the beginning of the file.
1476babe50SJustin T. Gibbs  * 2. The name of the author may not be used to endorse or promote products
1576babe50SJustin T. Gibbs  *    derived from this software without specific prior written permission.
1676babe50SJustin T. Gibbs  *
1776babe50SJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1876babe50SJustin T. Gibbs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1976babe50SJustin T. Gibbs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2076babe50SJustin T. Gibbs  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
2176babe50SJustin T. Gibbs  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2276babe50SJustin T. Gibbs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2376babe50SJustin T. Gibbs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2476babe50SJustin T. Gibbs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2576babe50SJustin T. Gibbs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2676babe50SJustin T. Gibbs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2776babe50SJustin T. Gibbs  * SUCH DAMAGE.
2876babe50SJustin T. Gibbs  */
2976babe50SJustin T. Gibbs 
3076babe50SJustin T. Gibbs #include <sys/param.h>
3176babe50SJustin T. Gibbs #include <sys/systm.h>
3276babe50SJustin T. Gibbs #include <sys/kernel.h>
33a9934668SKenneth D. Merry #include <sys/conf.h>
3476babe50SJustin T. Gibbs #include <sys/types.h>
359626b608SPoul-Henning Kamp #include <sys/bio.h>
36a9934668SKenneth D. Merry #include <sys/bus.h>
3776babe50SJustin T. Gibbs #include <sys/devicestat.h>
38a9934668SKenneth D. Merry #include <sys/errno.h>
39a9934668SKenneth D. Merry #include <sys/fcntl.h>
40a9934668SKenneth D. Merry #include <sys/malloc.h>
41f7312ca2SRobert Watson #include <sys/proc.h>
42a9934668SKenneth D. Merry #include <sys/poll.h>
43a9934668SKenneth D. Merry #include <sys/selinfo.h>
44a9934668SKenneth D. Merry #include <sys/sdt.h>
45871dc983SBrooks Davis #include <sys/sysent.h>
46416494d7SJustin T. Gibbs #include <sys/taskqueue.h>
47a9934668SKenneth D. Merry #include <vm/uma.h>
48a9934668SKenneth D. Merry #include <vm/vm.h>
49a9934668SKenneth D. Merry #include <vm/vm_extern.h>
50a9934668SKenneth D. Merry 
51a9934668SKenneth D. Merry #include <machine/bus.h>
5276babe50SJustin T. Gibbs 
5376babe50SJustin T. Gibbs #include <cam/cam.h>
5476babe50SJustin T. Gibbs #include <cam/cam_ccb.h>
5576babe50SJustin T. Gibbs #include <cam/cam_periph.h>
563393f8daSKenneth D. Merry #include <cam/cam_queue.h>
57a9934668SKenneth D. Merry #include <cam/cam_xpt.h>
5876babe50SJustin T. Gibbs #include <cam/cam_xpt_periph.h>
5976babe50SJustin T. Gibbs #include <cam/cam_debug.h>
6025a2902cSScott Long #include <cam/cam_compat.h>
61a9934668SKenneth D. Merry #include <cam/cam_xpt_periph.h>
6276babe50SJustin T. Gibbs 
6376babe50SJustin T. Gibbs #include <cam/scsi/scsi_all.h>
6476babe50SJustin T. Gibbs #include <cam/scsi/scsi_pass.h>
6576babe50SJustin T. Gibbs 
6676babe50SJustin T. Gibbs typedef enum {
6776babe50SJustin T. Gibbs 	PASS_FLAG_OPEN			= 0x01,
6876babe50SJustin T. Gibbs 	PASS_FLAG_LOCKED		= 0x02,
69ea37f519SKenneth D. Merry 	PASS_FLAG_INVALID		= 0x04,
70a9934668SKenneth D. Merry 	PASS_FLAG_INITIAL_PHYSPATH	= 0x08,
71a9934668SKenneth D. Merry 	PASS_FLAG_ZONE_INPROG		= 0x10,
72a9934668SKenneth D. Merry 	PASS_FLAG_ZONE_VALID		= 0x20,
73a9934668SKenneth D. Merry 	PASS_FLAG_UNMAPPED_CAPABLE	= 0x40,
74a9934668SKenneth D. Merry 	PASS_FLAG_ABANDONED_REF_SET	= 0x80
7576babe50SJustin T. Gibbs } pass_flags;
7676babe50SJustin T. Gibbs 
7776babe50SJustin T. Gibbs typedef enum {
7876babe50SJustin T. Gibbs 	PASS_STATE_NORMAL
7976babe50SJustin T. Gibbs } pass_state;
8076babe50SJustin T. Gibbs 
8176babe50SJustin T. Gibbs typedef enum {
82a9934668SKenneth D. Merry 	PASS_CCB_BUFFER_IO,
83a9934668SKenneth D. Merry 	PASS_CCB_QUEUED_IO
8476babe50SJustin T. Gibbs } pass_ccb_types;
8576babe50SJustin T. Gibbs 
8676babe50SJustin T. Gibbs #define ccb_type	ppriv_field0
87a9934668SKenneth D. Merry #define ccb_ioreq	ppriv_ptr1
88a9934668SKenneth D. Merry 
89a9934668SKenneth D. Merry /*
90a9934668SKenneth D. Merry  * The maximum number of memory segments we preallocate.
91a9934668SKenneth D. Merry  */
92a9934668SKenneth D. Merry #define	PASS_MAX_SEGS	16
93a9934668SKenneth D. Merry 
94a9934668SKenneth D. Merry typedef enum {
95a9934668SKenneth D. Merry 	PASS_IO_NONE		= 0x00,
96a9934668SKenneth D. Merry 	PASS_IO_USER_SEG_MALLOC	= 0x01,
97a9934668SKenneth D. Merry 	PASS_IO_KERN_SEG_MALLOC	= 0x02,
98a9934668SKenneth D. Merry 	PASS_IO_ABANDONED	= 0x04
99a9934668SKenneth D. Merry } pass_io_flags;
100a9934668SKenneth D. Merry 
101a9934668SKenneth D. Merry struct pass_io_req {
102a9934668SKenneth D. Merry 	union ccb			 ccb;
103a9934668SKenneth D. Merry 	union ccb			*alloced_ccb;
104a9934668SKenneth D. Merry 	union ccb			*user_ccb_ptr;
105a9934668SKenneth D. Merry 	camq_entry			 user_periph_links;
106a9934668SKenneth D. Merry 	ccb_ppriv_area			 user_periph_priv;
107a9934668SKenneth D. Merry 	struct cam_periph_map_info	 mapinfo;
108a9934668SKenneth D. Merry 	pass_io_flags			 flags;
109a9934668SKenneth D. Merry 	ccb_flags			 data_flags;
110a9934668SKenneth D. Merry 	int				 num_user_segs;
111a9934668SKenneth D. Merry 	bus_dma_segment_t		 user_segs[PASS_MAX_SEGS];
112a9934668SKenneth D. Merry 	int				 num_kern_segs;
113a9934668SKenneth D. Merry 	bus_dma_segment_t		 kern_segs[PASS_MAX_SEGS];
114a9934668SKenneth D. Merry 	bus_dma_segment_t		*user_segptr;
115a9934668SKenneth D. Merry 	bus_dma_segment_t		*kern_segptr;
116a9934668SKenneth D. Merry 	int				 num_bufs;
117a9934668SKenneth D. Merry 	uint32_t			 dirs[CAM_PERIPH_MAXMAPS];
118a9934668SKenneth D. Merry 	uint32_t			 lengths[CAM_PERIPH_MAXMAPS];
119a9934668SKenneth D. Merry 	uint8_t				*user_bufs[CAM_PERIPH_MAXMAPS];
120a9934668SKenneth D. Merry 	uint8_t				*kern_bufs[CAM_PERIPH_MAXMAPS];
121a9934668SKenneth D. Merry 	struct bintime			 start_time;
122a9934668SKenneth D. Merry 	TAILQ_ENTRY(pass_io_req)	 links;
123a9934668SKenneth D. Merry };
12476babe50SJustin T. Gibbs 
12576babe50SJustin T. Gibbs struct pass_softc {
12676babe50SJustin T. Gibbs 	pass_state		  state;
12776babe50SJustin T. Gibbs 	pass_flags		  flags;
1287c5d20a6SWarner Losh 	uint8_t		  pd_type;
12986d45c7fSKenneth D. Merry 	int			  open_count;
130de239312SAlexander Motin 	u_int		 	  maxio;
131a9d2245eSPoul-Henning Kamp 	struct devstat		 *device_stats;
13289c9c53dSPoul-Henning Kamp 	struct cdev		 *dev;
133416494d7SJustin T. Gibbs 	struct cdev		 *alias_dev;
134416494d7SJustin T. Gibbs 	struct task		  add_physpath_task;
135a9934668SKenneth D. Merry 	struct task		  shutdown_kqueue_task;
136a9934668SKenneth D. Merry 	struct selinfo		  read_select;
137a9934668SKenneth D. Merry 	TAILQ_HEAD(, pass_io_req) incoming_queue;
138a9934668SKenneth D. Merry 	TAILQ_HEAD(, pass_io_req) active_queue;
139a9934668SKenneth D. Merry 	TAILQ_HEAD(, pass_io_req) abandoned_queue;
140a9934668SKenneth D. Merry 	TAILQ_HEAD(, pass_io_req) done_queue;
141a9934668SKenneth D. Merry 	struct cam_periph	 *periph;
142a9934668SKenneth D. Merry 	char			  zone_name[12];
143a9934668SKenneth D. Merry 	char			  io_zone_name[12];
144a9934668SKenneth D. Merry 	uma_zone_t		  pass_zone;
145a9934668SKenneth D. Merry 	uma_zone_t		  pass_io_zone;
146a9934668SKenneth D. Merry 	size_t			  io_zone_size;
14776babe50SJustin T. Gibbs };
14876babe50SJustin T. Gibbs 
14976babe50SJustin T. Gibbs static	d_open_t	passopen;
15076babe50SJustin T. Gibbs static	d_close_t	passclose;
15176babe50SJustin T. Gibbs static	d_ioctl_t	passioctl;
15225a2902cSScott Long static	d_ioctl_t	passdoioctl;
153a9934668SKenneth D. Merry static	d_poll_t	passpoll;
154a9934668SKenneth D. Merry static	d_kqfilter_t	passkqfilter;
155a9934668SKenneth D. Merry static	void		passreadfiltdetach(struct knote *kn);
156a9934668SKenneth D. Merry static	int		passreadfilt(struct knote *kn, long hint);
15776babe50SJustin T. Gibbs 
15876babe50SJustin T. Gibbs static	periph_init_t	passinit;
15976babe50SJustin T. Gibbs static	periph_ctor_t	passregister;
160ee9c90c7SKenneth D. Merry static	periph_oninv_t	passoninvalidate;
16176babe50SJustin T. Gibbs static	periph_dtor_t	passcleanup;
162a9934668SKenneth D. Merry static	periph_start_t	passstart;
163a9934668SKenneth D. Merry static	void		pass_shutdown_kqueue(void *context, int pending);
164416494d7SJustin T. Gibbs static	void		pass_add_physpath(void *context, int pending);
1657c5d20a6SWarner Losh static	void		passasync(void *callback_arg, uint32_t code,
16676babe50SJustin T. Gibbs 				  struct cam_path *path, void *arg);
167a9934668SKenneth D. Merry static	void		passdone(struct cam_periph *periph,
168a9934668SKenneth D. Merry 				 union ccb *done_ccb);
169a9934668SKenneth D. Merry static	int		passcreatezone(struct cam_periph *periph);
170a9934668SKenneth D. Merry static	void		passiocleanup(struct pass_softc *softc,
171a9934668SKenneth D. Merry 				      struct pass_io_req *io_req);
172a9934668SKenneth D. Merry static	int		passcopysglist(struct cam_periph *periph,
173a9934668SKenneth D. Merry 				       struct pass_io_req *io_req,
174a9934668SKenneth D. Merry 				       ccb_flags direction);
175a9934668SKenneth D. Merry static	int		passmemsetup(struct cam_periph *periph,
176a9934668SKenneth D. Merry 				     struct pass_io_req *io_req);
177a9934668SKenneth D. Merry static	int		passmemdone(struct cam_periph *periph,
178a9934668SKenneth D. Merry 				    struct pass_io_req *io_req);
1797c5d20a6SWarner Losh static	int		passerror(union ccb *ccb, uint32_t cam_flags,
1807c5d20a6SWarner Losh 				  uint32_t sense_flags);
18176babe50SJustin T. Gibbs static 	int		passsendccb(struct cam_periph *periph, union ccb *ccb,
18276babe50SJustin T. Gibbs 				    union ccb *inccb);
183e474a8e2SWarner Losh static	void		passflags(union ccb *ccb, uint32_t *cam_flags,
184e474a8e2SWarner Losh 				  uint32_t *sense_flags);
18576babe50SJustin T. Gibbs 
18676babe50SJustin T. Gibbs static struct periph_driver passdriver =
18776babe50SJustin T. Gibbs {
18876babe50SJustin T. Gibbs 	passinit, "pass",
18976babe50SJustin T. Gibbs 	TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0
19076babe50SJustin T. Gibbs };
19176babe50SJustin T. Gibbs 
1920b7c27b9SPeter Wemm PERIPHDRIVER_DECLARE(pass, passdriver);
19376babe50SJustin T. Gibbs 
1944e2f199eSPoul-Henning Kamp static struct cdevsw pass_cdevsw = {
195dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
196c552ebe1SKenneth D. Merry 	.d_flags =	D_TRACKCLOSE,
1977ac40f5fSPoul-Henning Kamp 	.d_open =	passopen,
1987ac40f5fSPoul-Henning Kamp 	.d_close =	passclose,
1997ac40f5fSPoul-Henning Kamp 	.d_ioctl =	passioctl,
200a9934668SKenneth D. Merry 	.d_poll = 	passpoll,
201a9934668SKenneth D. Merry 	.d_kqfilter = 	passkqfilter,
2027ac40f5fSPoul-Henning Kamp 	.d_name =	"pass",
20376babe50SJustin T. Gibbs };
20476babe50SJustin T. Gibbs 
205*ef9ffb85SMark Johnston static const struct filterops passread_filtops = {
206a9934668SKenneth D. Merry 	.f_isfd	=	1,
207a9934668SKenneth D. Merry 	.f_detach =	passreadfiltdetach,
208a9934668SKenneth D. Merry 	.f_event =	passreadfilt
209a9934668SKenneth D. Merry };
210a9934668SKenneth D. Merry 
211a9934668SKenneth D. Merry static MALLOC_DEFINE(M_SCSIPASS, "scsi_pass", "scsi passthrough buffers");
212a9934668SKenneth D. Merry 
21376babe50SJustin T. Gibbs static void
passinit(void)21476babe50SJustin T. Gibbs passinit(void)
21576babe50SJustin T. Gibbs {
21676babe50SJustin T. Gibbs 	cam_status status;
21776babe50SJustin T. Gibbs 
21876babe50SJustin T. Gibbs 	/*
21976babe50SJustin T. Gibbs 	 * Install a global async callback.  This callback will
22076babe50SJustin T. Gibbs 	 * receive async callbacks like "new device found".
22176babe50SJustin T. Gibbs 	 */
22285d92640SScott Long 	status = xpt_register_async(AC_FOUND_DEVICE, passasync, NULL, NULL);
22376babe50SJustin T. Gibbs 
22476babe50SJustin T. Gibbs 	if (status != CAM_REQ_CMP) {
22576babe50SJustin T. Gibbs 		printf("pass: Failed to attach master async callback "
22676babe50SJustin T. Gibbs 		       "due to status 0x%x!\n", status);
22776babe50SJustin T. Gibbs 	}
22876babe50SJustin T. Gibbs 
22976babe50SJustin T. Gibbs }
23076babe50SJustin T. Gibbs 
23176babe50SJustin T. Gibbs static void
passrejectios(struct cam_periph * periph)232a9934668SKenneth D. Merry passrejectios(struct cam_periph *periph)
233a9934668SKenneth D. Merry {
234a9934668SKenneth D. Merry 	struct pass_io_req *io_req, *io_req2;
235a9934668SKenneth D. Merry 	struct pass_softc *softc;
236a9934668SKenneth D. Merry 
237a9934668SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
238a9934668SKenneth D. Merry 
239a9934668SKenneth D. Merry 	/*
240a9934668SKenneth D. Merry 	 * The user can no longer get status for I/O on the done queue, so
241a9934668SKenneth D. Merry 	 * clean up all outstanding I/O on the done queue.
242a9934668SKenneth D. Merry 	 */
243a9934668SKenneth D. Merry 	TAILQ_FOREACH_SAFE(io_req, &softc->done_queue, links, io_req2) {
244a9934668SKenneth D. Merry 		TAILQ_REMOVE(&softc->done_queue, io_req, links);
245a9934668SKenneth D. Merry 		passiocleanup(softc, io_req);
246a9934668SKenneth D. Merry 		uma_zfree(softc->pass_zone, io_req);
247a9934668SKenneth D. Merry 	}
248a9934668SKenneth D. Merry 
249a9934668SKenneth D. Merry 	/*
250a9934668SKenneth D. Merry 	 * The underlying device is gone, so we can't issue these I/Os.
251a9934668SKenneth D. Merry 	 * The devfs node has been shut down, so we can't return status to
252a9934668SKenneth D. Merry 	 * the user.  Free any I/O left on the incoming queue.
253a9934668SKenneth D. Merry 	 */
254a9934668SKenneth D. Merry 	TAILQ_FOREACH_SAFE(io_req, &softc->incoming_queue, links, io_req2) {
255a9934668SKenneth D. Merry 		TAILQ_REMOVE(&softc->incoming_queue, io_req, links);
256a9934668SKenneth D. Merry 		passiocleanup(softc, io_req);
257a9934668SKenneth D. Merry 		uma_zfree(softc->pass_zone, io_req);
258a9934668SKenneth D. Merry 	}
259a9934668SKenneth D. Merry 
260a9934668SKenneth D. Merry 	/*
261a9934668SKenneth D. Merry 	 * Normally we would put I/Os on the abandoned queue and acquire a
262a9934668SKenneth D. Merry 	 * reference when we saw the final close.  But, the device went
263a9934668SKenneth D. Merry 	 * away and devfs may have moved everything off to deadfs by the
264a9934668SKenneth D. Merry 	 * time the I/O done callback is called; as a result, we won't see
265a9934668SKenneth D. Merry 	 * any more closes.  So, if we have any active I/Os, we need to put
266a9934668SKenneth D. Merry 	 * them on the abandoned queue.  When the abandoned queue is empty,
267a9934668SKenneth D. Merry 	 * we'll release the remaining reference (see below) to the peripheral.
268a9934668SKenneth D. Merry 	 */
269a9934668SKenneth D. Merry 	TAILQ_FOREACH_SAFE(io_req, &softc->active_queue, links, io_req2) {
270a9934668SKenneth D. Merry 		TAILQ_REMOVE(&softc->active_queue, io_req, links);
271a9934668SKenneth D. Merry 		io_req->flags |= PASS_IO_ABANDONED;
272a9934668SKenneth D. Merry 		TAILQ_INSERT_TAIL(&softc->abandoned_queue, io_req, links);
273a9934668SKenneth D. Merry 	}
274a9934668SKenneth D. Merry 
275a9934668SKenneth D. Merry 	/*
276a9934668SKenneth D. Merry 	 * If we put any I/O on the abandoned queue, acquire a reference.
277a9934668SKenneth D. Merry 	 */
278a9934668SKenneth D. Merry 	if ((!TAILQ_EMPTY(&softc->abandoned_queue))
279a9934668SKenneth D. Merry 	 && ((softc->flags & PASS_FLAG_ABANDONED_REF_SET) == 0)) {
280a9934668SKenneth D. Merry 		cam_periph_doacquire(periph);
281a9934668SKenneth D. Merry 		softc->flags |= PASS_FLAG_ABANDONED_REF_SET;
282a9934668SKenneth D. Merry 	}
283a9934668SKenneth D. Merry }
284a9934668SKenneth D. Merry 
285a9934668SKenneth D. Merry static void
passdevgonecb(void * arg)286ea37f519SKenneth D. Merry passdevgonecb(void *arg)
287ea37f519SKenneth D. Merry {
288ea37f519SKenneth D. Merry 	struct cam_periph *periph;
289227d67aaSAlexander Motin 	struct mtx *mtx;
29086d45c7fSKenneth D. Merry 	struct pass_softc *softc;
29186d45c7fSKenneth D. Merry 	int i;
292ea37f519SKenneth D. Merry 
293ea37f519SKenneth D. Merry 	periph = (struct cam_periph *)arg;
294227d67aaSAlexander Motin 	mtx = cam_periph_mtx(periph);
295227d67aaSAlexander Motin 	mtx_lock(mtx);
296ea37f519SKenneth D. Merry 
297227d67aaSAlexander Motin 	softc = (struct pass_softc *)periph->softc;
29886d45c7fSKenneth D. Merry 	KASSERT(softc->open_count >= 0, ("Negative open count %d",
29986d45c7fSKenneth D. Merry 		softc->open_count));
30086d45c7fSKenneth D. Merry 
30186d45c7fSKenneth D. Merry 	/*
30286d45c7fSKenneth D. Merry 	 * When we get this callback, we will get no more close calls from
30386d45c7fSKenneth D. Merry 	 * devfs.  So if we have any dangling opens, we need to release the
30486d45c7fSKenneth D. Merry 	 * reference held for that particular context.
30586d45c7fSKenneth D. Merry 	 */
30686d45c7fSKenneth D. Merry 	for (i = 0; i < softc->open_count; i++)
30786d45c7fSKenneth D. Merry 		cam_periph_release_locked(periph);
30886d45c7fSKenneth D. Merry 
30986d45c7fSKenneth D. Merry 	softc->open_count = 0;
31086d45c7fSKenneth D. Merry 
31186d45c7fSKenneth D. Merry 	/*
31286d45c7fSKenneth D. Merry 	 * Release the reference held for the device node, it is gone now.
313a9934668SKenneth D. Merry 	 * Accordingly, inform all queued I/Os of their fate.
31486d45c7fSKenneth D. Merry 	 */
31586d45c7fSKenneth D. Merry 	cam_periph_release_locked(periph);
316a9934668SKenneth D. Merry 	passrejectios(periph);
31786d45c7fSKenneth D. Merry 
31886d45c7fSKenneth D. Merry 	/*
319a9934668SKenneth D. Merry 	 * We reference the SIM lock directly here, instead of using
32086d45c7fSKenneth D. Merry 	 * cam_periph_unlock().  The reason is that the final call to
32186d45c7fSKenneth D. Merry 	 * cam_periph_release_locked() above could result in the periph
32286d45c7fSKenneth D. Merry 	 * getting freed.  If that is the case, dereferencing the periph
32386d45c7fSKenneth D. Merry 	 * with a cam_periph_unlock() call would cause a page fault.
32486d45c7fSKenneth D. Merry 	 */
325227d67aaSAlexander Motin 	mtx_unlock(mtx);
326a9934668SKenneth D. Merry 
327a9934668SKenneth D. Merry 	/*
328a9934668SKenneth D. Merry 	 * We have to remove our kqueue context from a thread because it
329a9934668SKenneth D. Merry 	 * may sleep.  It would be nice if we could get a callback from
330a9934668SKenneth D. Merry 	 * kqueue when it is done cleaning up resources.
331a9934668SKenneth D. Merry 	 */
332a9934668SKenneth D. Merry 	taskqueue_enqueue(taskqueue_thread, &softc->shutdown_kqueue_task);
333ea37f519SKenneth D. Merry }
334ea37f519SKenneth D. Merry 
335ea37f519SKenneth D. Merry static void
passoninvalidate(struct cam_periph * periph)336ee9c90c7SKenneth D. Merry passoninvalidate(struct cam_periph *periph)
337ee9c90c7SKenneth D. Merry {
338ee9c90c7SKenneth D. Merry 	struct pass_softc *softc;
339ee9c90c7SKenneth D. Merry 
340ee9c90c7SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
341ee9c90c7SKenneth D. Merry 
342ee9c90c7SKenneth D. Merry 	/*
343ee9c90c7SKenneth D. Merry 	 * De-register any async callbacks.
344ee9c90c7SKenneth D. Merry 	 */
34585d92640SScott Long 	xpt_register_async(0, passasync, periph, periph->path);
346ee9c90c7SKenneth D. Merry 
347ee9c90c7SKenneth D. Merry 	softc->flags |= PASS_FLAG_INVALID;
348ee9c90c7SKenneth D. Merry 
349ee9c90c7SKenneth D. Merry 	/*
350ea37f519SKenneth D. Merry 	 * Tell devfs this device has gone away, and ask for a callback
351ea37f519SKenneth D. Merry 	 * when it has cleaned up its state.
352ea37f519SKenneth D. Merry 	 */
353ea37f519SKenneth D. Merry 	destroy_dev_sched_cb(softc->dev, passdevgonecb, periph);
354ee9c90c7SKenneth D. Merry }
355ee9c90c7SKenneth D. Merry 
356ee9c90c7SKenneth D. Merry static void
passcleanup(struct cam_periph * periph)35776babe50SJustin T. Gibbs passcleanup(struct cam_periph *periph)
35876babe50SJustin T. Gibbs {
359ee9c90c7SKenneth D. Merry 	struct pass_softc *softc;
360ee9c90c7SKenneth D. Merry 
361ee9c90c7SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
362ee9c90c7SKenneth D. Merry 
363a9934668SKenneth D. Merry 	cam_periph_assert(periph, MA_OWNED);
364a9934668SKenneth D. Merry 	KASSERT(TAILQ_EMPTY(&softc->active_queue),
365a9934668SKenneth D. Merry 		("%s called when there are commands on the active queue!\n",
366a9934668SKenneth D. Merry 		__func__));
367a9934668SKenneth D. Merry 	KASSERT(TAILQ_EMPTY(&softc->abandoned_queue),
368a9934668SKenneth D. Merry 		("%s called when there are commands on the abandoned queue!\n",
369a9934668SKenneth D. Merry 		__func__));
370a9934668SKenneth D. Merry 	KASSERT(TAILQ_EMPTY(&softc->incoming_queue),
371a9934668SKenneth D. Merry 		("%s called when there are commands on the incoming queue!\n",
372a9934668SKenneth D. Merry 		__func__));
373a9934668SKenneth D. Merry 	KASSERT(TAILQ_EMPTY(&softc->done_queue),
374a9934668SKenneth D. Merry 		("%s called when there are commands on the done queue!\n",
375a9934668SKenneth D. Merry 		__func__));
376a9934668SKenneth D. Merry 
3775f3fed85SEdward Tomasz Napierala 	devstat_remove_entry(softc->device_stats);
378416494d7SJustin T. Gibbs 
3795f3fed85SEdward Tomasz Napierala 	cam_periph_unlock(periph);
380a9934668SKenneth D. Merry 
381a9934668SKenneth D. Merry 	/*
382a9934668SKenneth D. Merry 	 * We call taskqueue_drain() for the physpath task to make sure it
383a9934668SKenneth D. Merry 	 * is complete.  We drop the lock because this can potentially
384a9934668SKenneth D. Merry 	 * sleep.  XXX KDM that is bad.  Need a way to get a callback when
385a9934668SKenneth D. Merry 	 * a taskqueue is drained.
386a9934668SKenneth D. Merry 	 *
387a9934668SKenneth D. Merry  	 * Note that we don't drain the kqueue shutdown task queue.  This
388a9934668SKenneth D. Merry 	 * is because we hold a reference on the periph for kqueue, and
389a9934668SKenneth D. Merry 	 * release that reference from the kqueue shutdown task queue.  So
390a9934668SKenneth D. Merry 	 * we cannot come into this routine unless we've released that
391a9934668SKenneth D. Merry 	 * reference.  Also, because that could be the last reference, we
392a9934668SKenneth D. Merry 	 * could be called from the cam_periph_release() call in
393a9934668SKenneth D. Merry 	 * pass_shutdown_kqueue().  In that case, the taskqueue_drain()
394a9934668SKenneth D. Merry 	 * would deadlock.  It would be preferable if we had a way to
395a9934668SKenneth D. Merry 	 * get a callback when a taskqueue is done.
396a9934668SKenneth D. Merry 	 */
397416494d7SJustin T. Gibbs 	taskqueue_drain(taskqueue_thread, &softc->add_physpath_task);
398416494d7SJustin T. Gibbs 
399ca2a7262SKenneth D. Merry 	/*
400ca2a7262SKenneth D. Merry 	 * It should be safe to destroy the zones from here, because all
401ca2a7262SKenneth D. Merry 	 * of the references to this peripheral have been freed, and all
402ca2a7262SKenneth D. Merry 	 * I/O has been terminated and freed.  We check the zones for NULL
403ca2a7262SKenneth D. Merry 	 * because they may not have been allocated yet if the device went
404ca2a7262SKenneth D. Merry 	 * away before any asynchronous I/O has been issued.
405ca2a7262SKenneth D. Merry 	 */
406ca2a7262SKenneth D. Merry 	if (softc->pass_zone != NULL)
407ca2a7262SKenneth D. Merry 		uma_zdestroy(softc->pass_zone);
408ca2a7262SKenneth D. Merry 	if (softc->pass_io_zone != NULL)
409ca2a7262SKenneth D. Merry 		uma_zdestroy(softc->pass_io_zone);
410ca2a7262SKenneth D. Merry 
4115f3fed85SEdward Tomasz Napierala 	cam_periph_lock(periph);
412416494d7SJustin T. Gibbs 
413ee9c90c7SKenneth D. Merry 	free(softc, M_DEVBUF);
41476babe50SJustin T. Gibbs }
41576babe50SJustin T. Gibbs 
41676babe50SJustin T. Gibbs static void
pass_shutdown_kqueue(void * context,int pending)417a9934668SKenneth D. Merry pass_shutdown_kqueue(void *context, int pending)
418a9934668SKenneth D. Merry {
419a9934668SKenneth D. Merry 	struct cam_periph *periph;
420a9934668SKenneth D. Merry 	struct pass_softc *softc;
421a9934668SKenneth D. Merry 
422a9934668SKenneth D. Merry 	periph = context;
423a9934668SKenneth D. Merry 	softc = periph->softc;
424a9934668SKenneth D. Merry 
425a9934668SKenneth D. Merry 	knlist_clear(&softc->read_select.si_note, /*is_locked*/ 0);
426a9934668SKenneth D. Merry 	knlist_destroy(&softc->read_select.si_note);
427a9934668SKenneth D. Merry 
428a9934668SKenneth D. Merry 	/*
429a9934668SKenneth D. Merry 	 * Release the reference we held for kqueue.
430a9934668SKenneth D. Merry 	 */
431a9934668SKenneth D. Merry 	cam_periph_release(periph);
432a9934668SKenneth D. Merry }
433a9934668SKenneth D. Merry 
434a9934668SKenneth D. Merry static void
pass_add_physpath(void * context,int pending)435416494d7SJustin T. Gibbs pass_add_physpath(void *context, int pending)
436416494d7SJustin T. Gibbs {
437416494d7SJustin T. Gibbs 	struct cam_periph *periph;
438416494d7SJustin T. Gibbs 	struct pass_softc *softc;
439a9934668SKenneth D. Merry 	struct mtx *mtx;
440416494d7SJustin T. Gibbs 	char *physpath;
441416494d7SJustin T. Gibbs 
442416494d7SJustin T. Gibbs 	/*
443416494d7SJustin T. Gibbs 	 * If we have one, create a devfs alias for our
444416494d7SJustin T. Gibbs 	 * physical path.
445416494d7SJustin T. Gibbs 	 */
446416494d7SJustin T. Gibbs 	periph = context;
447416494d7SJustin T. Gibbs 	softc = periph->softc;
4486884b662SAlexander Motin 	physpath = malloc(MAXPATHLEN, M_DEVBUF, M_WAITOK);
449a9934668SKenneth D. Merry 	mtx = cam_periph_mtx(periph);
450a9934668SKenneth D. Merry 	mtx_lock(mtx);
451a9934668SKenneth D. Merry 
452a9934668SKenneth D. Merry 	if (periph->flags & CAM_PERIPH_INVALID)
4536884b662SAlexander Motin 		goto out;
454a9934668SKenneth D. Merry 
455416494d7SJustin T. Gibbs 	if (xpt_getattr(physpath, MAXPATHLEN,
456416494d7SJustin T. Gibbs 			"GEOM::physpath", periph->path) == 0
457416494d7SJustin T. Gibbs 	 && strlen(physpath) != 0) {
458a9934668SKenneth D. Merry 		mtx_unlock(mtx);
4595f438dd3SAlan Somers 		make_dev_physpath_alias(MAKEDEV_WAITOK | MAKEDEV_CHECKNAME,
4605f438dd3SAlan Somers 				&softc->alias_dev, softc->dev,
4615f438dd3SAlan Somers 				softc->alias_dev, physpath);
462a9934668SKenneth D. Merry 		mtx_lock(mtx);
463416494d7SJustin T. Gibbs 	}
464ea37f519SKenneth D. Merry 
465a9934668SKenneth D. Merry out:
466ea37f519SKenneth D. Merry 	/*
467ea37f519SKenneth D. Merry 	 * Now that we've made our alias, we no longer have to have a
468ea37f519SKenneth D. Merry 	 * reference to the device.
469ea37f519SKenneth D. Merry 	 */
470a9934668SKenneth D. Merry 	if ((softc->flags & PASS_FLAG_INITIAL_PHYSPATH) == 0)
471ea37f519SKenneth D. Merry 		softc->flags |= PASS_FLAG_INITIAL_PHYSPATH;
4726884b662SAlexander Motin 
473a9934668SKenneth D. Merry 	/*
474a9934668SKenneth D. Merry 	 * We always acquire a reference to the periph before queueing this
475a9934668SKenneth D. Merry 	 * task queue function, so it won't go away before we run.
476a9934668SKenneth D. Merry 	 */
477a9934668SKenneth D. Merry 	while (pending-- > 0)
478a9934668SKenneth D. Merry 		cam_periph_release_locked(periph);
479a9934668SKenneth D. Merry 	mtx_unlock(mtx);
480a9934668SKenneth D. Merry 
4816884b662SAlexander Motin 	free(physpath, M_DEVBUF);
482416494d7SJustin T. Gibbs }
483416494d7SJustin T. Gibbs 
484416494d7SJustin T. Gibbs static void
passasync(void * callback_arg,uint32_t code,struct cam_path * path,void * arg)4857c5d20a6SWarner Losh passasync(void *callback_arg, uint32_t code,
48676babe50SJustin T. Gibbs 	  struct cam_path *path, void *arg)
48776babe50SJustin T. Gibbs {
48876babe50SJustin T. Gibbs 	struct cam_periph *periph;
48976babe50SJustin T. Gibbs 
49076babe50SJustin T. Gibbs 	periph = (struct cam_periph *)callback_arg;
49176babe50SJustin T. Gibbs 
49276babe50SJustin T. Gibbs 	switch (code) {
49376babe50SJustin T. Gibbs 	case AC_FOUND_DEVICE:
49476babe50SJustin T. Gibbs 	{
49576babe50SJustin T. Gibbs 		struct ccb_getdev *cgd;
49676babe50SJustin T. Gibbs 		cam_status status;
49776babe50SJustin T. Gibbs 
49876babe50SJustin T. Gibbs 		cgd = (struct ccb_getdev *)arg;
499c5ff3b2fSMatt Jacob 		if (cgd == NULL)
500c5ff3b2fSMatt Jacob 			break;
50176babe50SJustin T. Gibbs 
50276babe50SJustin T. Gibbs 		/*
50376babe50SJustin T. Gibbs 		 * Allocate a peripheral instance for
50476babe50SJustin T. Gibbs 		 * this device and start the probe
50576babe50SJustin T. Gibbs 		 * process.
50676babe50SJustin T. Gibbs 		 */
507ee9c90c7SKenneth D. Merry 		status = cam_periph_alloc(passregister, passoninvalidate,
508a9934668SKenneth D. Merry 					  passcleanup, passstart, "pass",
509227d67aaSAlexander Motin 					  CAM_PERIPH_BIO, path,
510ee9c90c7SKenneth D. Merry 					  passasync, AC_FOUND_DEVICE, cgd);
51176babe50SJustin T. Gibbs 
51276babe50SJustin T. Gibbs 		if (status != CAM_REQ_CMP
5133393f8daSKenneth D. Merry 		 && status != CAM_REQ_INPROG) {
5143393f8daSKenneth D. Merry 			const struct cam_status_entry *entry;
5153393f8daSKenneth D. Merry 
5163393f8daSKenneth D. Merry 			entry = cam_fetch_status_entry(status);
5173393f8daSKenneth D. Merry 
51876babe50SJustin T. Gibbs 			printf("passasync: Unable to attach new device "
5193393f8daSKenneth D. Merry 			       "due to status %#x: %s\n", status, entry ?
5203393f8daSKenneth D. Merry 			       entry->status_text : "Unknown");
5213393f8daSKenneth D. Merry 		}
52276babe50SJustin T. Gibbs 
52376babe50SJustin T. Gibbs 		break;
52476babe50SJustin T. Gibbs 	}
525416494d7SJustin T. Gibbs 	case AC_ADVINFO_CHANGED:
526416494d7SJustin T. Gibbs 	{
527416494d7SJustin T. Gibbs 		uintptr_t buftype;
528416494d7SJustin T. Gibbs 
529416494d7SJustin T. Gibbs 		buftype = (uintptr_t)arg;
530416494d7SJustin T. Gibbs 		if (buftype == CDAI_TYPE_PHYS_PATH) {
531416494d7SJustin T. Gibbs 			struct pass_softc *softc;
532416494d7SJustin T. Gibbs 
533416494d7SJustin T. Gibbs 			softc = (struct pass_softc *)periph->softc;
534a9934668SKenneth D. Merry 			/*
535a9934668SKenneth D. Merry 			 * Acquire a reference to the periph before we
536a9934668SKenneth D. Merry 			 * start the taskqueue, so that we don't run into
537a9934668SKenneth D. Merry 			 * a situation where the periph goes away before
538a9934668SKenneth D. Merry 			 * the task queue has a chance to run.
539a9934668SKenneth D. Merry 			 */
54099e7a4adSScott Long 			if (cam_periph_acquire(periph) != 0)
541a9934668SKenneth D. Merry 				break;
542a9934668SKenneth D. Merry 
543416494d7SJustin T. Gibbs 			taskqueue_enqueue(taskqueue_thread,
544416494d7SJustin T. Gibbs 					  &softc->add_physpath_task);
545416494d7SJustin T. Gibbs 		}
546416494d7SJustin T. Gibbs 		break;
547416494d7SJustin T. Gibbs 	}
54876babe50SJustin T. Gibbs 	default:
549516871c6SJustin T. Gibbs 		cam_periph_async(periph, code, path, arg);
55076babe50SJustin T. Gibbs 		break;
55176babe50SJustin T. Gibbs 	}
55276babe50SJustin T. Gibbs }
55376babe50SJustin T. Gibbs 
55476babe50SJustin T. Gibbs static cam_status
passregister(struct cam_periph * periph,void * arg)55576babe50SJustin T. Gibbs passregister(struct cam_periph *periph, void *arg)
55676babe50SJustin T. Gibbs {
55776babe50SJustin T. Gibbs 	struct pass_softc *softc;
55876babe50SJustin T. Gibbs 	struct ccb_getdev *cgd;
559b8b6b5d3SAlexander Motin 	struct ccb_pathinq cpi;
560ee198893SKonstantin Belousov 	struct make_dev_args args;
561ee198893SKonstantin Belousov 	int error, no_tags;
56276babe50SJustin T. Gibbs 
56376babe50SJustin T. Gibbs 	cgd = (struct ccb_getdev *)arg;
56476babe50SJustin T. Gibbs 	if (cgd == NULL) {
565ea37f519SKenneth D. Merry 		printf("%s: no getdev CCB, can't register device\n", __func__);
56676babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
56776babe50SJustin T. Gibbs 	}
56876babe50SJustin T. Gibbs 
56976babe50SJustin T. Gibbs 	softc = (struct pass_softc *)malloc(sizeof(*softc),
57076babe50SJustin T. Gibbs 					    M_DEVBUF, M_NOWAIT);
57176babe50SJustin T. Gibbs 
57276babe50SJustin T. Gibbs 	if (softc == NULL) {
573ea37f519SKenneth D. Merry 		printf("%s: Unable to probe new device. "
574ea37f519SKenneth D. Merry 		       "Unable to allocate softc\n", __func__);
57576babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
57676babe50SJustin T. Gibbs 	}
57776babe50SJustin T. Gibbs 
57876babe50SJustin T. Gibbs 	bzero(softc, sizeof(*softc));
57976babe50SJustin T. Gibbs 	softc->state = PASS_STATE_NORMAL;
580b8b6b5d3SAlexander Motin 	if (cgd->protocol == PROTO_SCSI || cgd->protocol == PROTO_ATAPI)
58110b6172aSMatt Jacob 		softc->pd_type = SID_TYPE(&cgd->inq_data);
582b8b6b5d3SAlexander Motin 	else if (cgd->protocol == PROTO_SATAPM)
583b8b6b5d3SAlexander Motin 		softc->pd_type = T_ENCLOSURE;
584b8b6b5d3SAlexander Motin 	else
585b8b6b5d3SAlexander Motin 		softc->pd_type = T_DIRECT;
58676babe50SJustin T. Gibbs 
58776babe50SJustin T. Gibbs 	periph->softc = softc;
588a9934668SKenneth D. Merry 	softc->periph = periph;
589a9934668SKenneth D. Merry 	TAILQ_INIT(&softc->incoming_queue);
590a9934668SKenneth D. Merry 	TAILQ_INIT(&softc->active_queue);
591a9934668SKenneth D. Merry 	TAILQ_INIT(&softc->abandoned_queue);
592a9934668SKenneth D. Merry 	TAILQ_INIT(&softc->done_queue);
593a9934668SKenneth D. Merry 	snprintf(softc->zone_name, sizeof(softc->zone_name), "%s%d",
594a9934668SKenneth D. Merry 		 periph->periph_name, periph->unit_number);
595a9934668SKenneth D. Merry 	snprintf(softc->io_zone_name, sizeof(softc->io_zone_name), "%s%dIO",
596a9934668SKenneth D. Merry 		 periph->periph_name, periph->unit_number);
597cd853791SKonstantin Belousov 	softc->io_zone_size = maxphys;
598a9934668SKenneth D. Merry 	knlist_init_mtx(&softc->read_select.si_note, cam_periph_mtx(periph));
5993393f8daSKenneth D. Merry 
600762a7f4fSWarner Losh 	xpt_path_inq(&cpi, periph->path);
601b8b6b5d3SAlexander Motin 
602de239312SAlexander Motin 	if (cpi.maxio == 0)
603de239312SAlexander Motin 		softc->maxio = DFLTPHYS;	/* traditional default */
604cd853791SKonstantin Belousov 	else if (cpi.maxio > maxphys)
605cd853791SKonstantin Belousov 		softc->maxio = maxphys;		/* for safety */
606de239312SAlexander Motin 	else
607de239312SAlexander Motin 		softc->maxio = cpi.maxio;	/* real value */
608de239312SAlexander Motin 
609a9934668SKenneth D. Merry 	if (cpi.hba_misc & PIM_UNMAPPED)
610a9934668SKenneth D. Merry 		softc->flags |= PASS_FLAG_UNMAPPED_CAPABLE;
611a9934668SKenneth D. Merry 
61276babe50SJustin T. Gibbs 	/*
61376babe50SJustin T. Gibbs 	 * We pass in 0 for a blocksize, since we don't
61476babe50SJustin T. Gibbs 	 * know what the blocksize of this device is, if
61576babe50SJustin T. Gibbs 	 * it even has a blocksize.
61676babe50SJustin T. Gibbs 	 */
617edec59d9SAlexander Motin 	cam_periph_unlock(periph);
6183393f8daSKenneth D. Merry 	no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0;
619c81d2c74SMatt Jacob 	softc->device_stats = devstat_new_entry("pass",
620d3ce8327SEd Schouten 			  periph->unit_number, 0,
6213393f8daSKenneth D. Merry 			  DEVSTAT_NO_BLOCKSIZE
6223393f8daSKenneth D. Merry 			  | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0),
62310b6172aSMatt Jacob 			  softc->pd_type |
624b8b6b5d3SAlexander Motin 			  XPORT_DEVSTAT_TYPE(cpi.transport) |
6252a888f93SKenneth D. Merry 			  DEVSTAT_TYPE_PASS,
6262a888f93SKenneth D. Merry 			  DEVSTAT_PRIORITY_PASS);
62773d26919SKenneth D. Merry 
628ea37f519SKenneth D. Merry 	/*
629a9934668SKenneth D. Merry 	 * Initialize the taskqueue handler for shutting down kqueue.
630a9934668SKenneth D. Merry 	 */
631a9934668SKenneth D. Merry 	TASK_INIT(&softc->shutdown_kqueue_task, /*priority*/ 0,
632a9934668SKenneth D. Merry 		  pass_shutdown_kqueue, periph);
633a9934668SKenneth D. Merry 
634a9934668SKenneth D. Merry 	/*
635a9934668SKenneth D. Merry 	 * Acquire a reference to the periph that we can release once we've
636a9934668SKenneth D. Merry 	 * cleaned up the kqueue.
637a9934668SKenneth D. Merry 	 */
63899e7a4adSScott Long 	if (cam_periph_acquire(periph) != 0) {
639a9934668SKenneth D. Merry 		xpt_print(periph->path, "%s: lost periph during "
640a9934668SKenneth D. Merry 			  "registration!\n", __func__);
641a9934668SKenneth D. Merry 		cam_periph_lock(periph);
642a9934668SKenneth D. Merry 		return (CAM_REQ_CMP_ERR);
643a9934668SKenneth D. Merry 	}
644a9934668SKenneth D. Merry 
645a9934668SKenneth D. Merry 	/*
646ea37f519SKenneth D. Merry 	 * Acquire a reference to the periph before we create the devfs
647ea37f519SKenneth D. Merry 	 * instance for it.  We'll release this reference once the devfs
648ea37f519SKenneth D. Merry 	 * instance has been freed.
649ea37f519SKenneth D. Merry 	 */
65099e7a4adSScott Long 	if (cam_periph_acquire(periph) != 0) {
651ea37f519SKenneth D. Merry 		xpt_print(periph->path, "%s: lost periph during "
652ea37f519SKenneth D. Merry 			  "registration!\n", __func__);
65386d45c7fSKenneth D. Merry 		cam_periph_lock(periph);
654ea37f519SKenneth D. Merry 		return (CAM_REQ_CMP_ERR);
655ea37f519SKenneth D. Merry 	}
656ea37f519SKenneth D. Merry 
65773d26919SKenneth D. Merry 	/* Register the device */
658ee198893SKonstantin Belousov 	make_dev_args_init(&args);
659ee198893SKonstantin Belousov 	args.mda_devsw = &pass_cdevsw;
660ee198893SKonstantin Belousov 	args.mda_unit = periph->unit_number;
661ee198893SKonstantin Belousov 	args.mda_uid = UID_ROOT;
662ee198893SKonstantin Belousov 	args.mda_gid = GID_OPERATOR;
663ee198893SKonstantin Belousov 	args.mda_mode = 0600;
664ee198893SKonstantin Belousov 	args.mda_si_drv1 = periph;
665f1f3f7cfSMateusz Guzik 	args.mda_flags = MAKEDEV_NOWAIT;
666ee198893SKonstantin Belousov 	error = make_dev_s(&args, &softc->dev, "%s%d", periph->periph_name,
667ee198893SKonstantin Belousov 	    periph->unit_number);
668ee198893SKonstantin Belousov 	if (error != 0) {
669ee198893SKonstantin Belousov 		cam_periph_lock(periph);
670ee198893SKonstantin Belousov 		cam_periph_release_locked(periph);
671ee198893SKonstantin Belousov 		return (CAM_REQ_CMP_ERR);
672ee198893SKonstantin Belousov 	}
673ea37f519SKenneth D. Merry 
674ea37f519SKenneth D. Merry 	/*
675a9934668SKenneth D. Merry 	 * Hold a reference to the periph before we create the physical
676a9934668SKenneth D. Merry 	 * path alias so it can't go away.
677ea37f519SKenneth D. Merry 	 */
67899e7a4adSScott Long 	if (cam_periph_acquire(periph) != 0) {
679a9934668SKenneth D. Merry 		xpt_print(periph->path, "%s: lost periph during "
680a9934668SKenneth D. Merry 			  "registration!\n", __func__);
681a9934668SKenneth D. Merry 		cam_periph_lock(periph);
682a9934668SKenneth D. Merry 		return (CAM_REQ_CMP_ERR);
683a9934668SKenneth D. Merry 	}
684ea37f519SKenneth D. Merry 
685edec59d9SAlexander Motin 	cam_periph_lock(periph);
68673d26919SKenneth D. Merry 
687416494d7SJustin T. Gibbs 	TASK_INIT(&softc->add_physpath_task, /*priority*/0,
688416494d7SJustin T. Gibbs 		  pass_add_physpath, periph);
689416494d7SJustin T. Gibbs 
69076babe50SJustin T. Gibbs 	/*
691416494d7SJustin T. Gibbs 	 * See if physical path information is already available.
69276babe50SJustin T. Gibbs 	 */
693416494d7SJustin T. Gibbs 	taskqueue_enqueue(taskqueue_thread, &softc->add_physpath_task);
694416494d7SJustin T. Gibbs 
695416494d7SJustin T. Gibbs 	/*
696416494d7SJustin T. Gibbs 	 * Add an async callback so that we get notified if
697416494d7SJustin T. Gibbs 	 * this device goes away or its physical path
698416494d7SJustin T. Gibbs 	 * (stored in the advanced info data of the EDT) has
699416494d7SJustin T. Gibbs 	 * changed.
700416494d7SJustin T. Gibbs 	 */
701416494d7SJustin T. Gibbs 	xpt_register_async(AC_LOST_DEVICE | AC_ADVINFO_CHANGED,
702416494d7SJustin T. Gibbs 			   passasync, periph, periph->path);
70376babe50SJustin T. Gibbs 
70476babe50SJustin T. Gibbs 	if (bootverbose)
70576babe50SJustin T. Gibbs 		xpt_announce_periph(periph, NULL);
70676babe50SJustin T. Gibbs 
70776babe50SJustin T. Gibbs 	return(CAM_REQ_CMP);
70876babe50SJustin T. Gibbs }
70976babe50SJustin T. Gibbs 
71076babe50SJustin T. Gibbs static int
passopen(struct cdev * dev,int flags,int fmt,struct thread * td)71189c9c53dSPoul-Henning Kamp passopen(struct cdev *dev, int flags, int fmt, struct thread *td)
71276babe50SJustin T. Gibbs {
71376babe50SJustin T. Gibbs 	struct cam_periph *periph;
71476babe50SJustin T. Gibbs 	struct pass_softc *softc;
715e2a5fdf9SNate Lawson 	int error;
71676babe50SJustin T. Gibbs 
717e2a5fdf9SNate Lawson 	periph = (struct cam_periph *)dev->si_drv1;
71899e7a4adSScott Long 	if (cam_periph_acquire(periph) != 0)
71976babe50SJustin T. Gibbs 		return (ENXIO);
72076babe50SJustin T. Gibbs 
7212b83592fSScott Long 	cam_periph_lock(periph);
7222b83592fSScott Long 
72376babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
72476babe50SJustin T. Gibbs 
725ee9c90c7SKenneth D. Merry 	if (softc->flags & PASS_FLAG_INVALID) {
726c552ebe1SKenneth D. Merry 		cam_periph_release_locked(periph);
7272b83592fSScott Long 		cam_periph_unlock(periph);
72876babe50SJustin T. Gibbs 		return(ENXIO);
729ee9c90c7SKenneth D. Merry 	}
73022b9c86cSKenneth D. Merry 
73122b9c86cSKenneth D. Merry 	/*
732f5ef42beSRobert Watson 	 * Don't allow access when we're running at a high securelevel.
73322b9c86cSKenneth D. Merry 	 */
734a854ed98SJohn Baldwin 	error = securelevel_gt(td->td_ucred, 1);
735f7312ca2SRobert Watson 	if (error) {
736c552ebe1SKenneth D. Merry 		cam_periph_release_locked(periph);
7372b83592fSScott Long 		cam_periph_unlock(periph);
738f7312ca2SRobert Watson 		return(error);
73922b9c86cSKenneth D. Merry 	}
74076babe50SJustin T. Gibbs 
74176babe50SJustin T. Gibbs 	/*
74266a0780eSKenneth D. Merry 	 * Only allow read-write access.
74366a0780eSKenneth D. Merry 	 */
74422b9c86cSKenneth D. Merry 	if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) {
745c552ebe1SKenneth D. Merry 		cam_periph_release_locked(periph);
7462b83592fSScott Long 		cam_periph_unlock(periph);
74766a0780eSKenneth D. Merry 		return(EPERM);
74822b9c86cSKenneth D. Merry 	}
74966a0780eSKenneth D. Merry 
75066a0780eSKenneth D. Merry 	/*
75176babe50SJustin T. Gibbs 	 * We don't allow nonblocking access.
75276babe50SJustin T. Gibbs 	 */
75376babe50SJustin T. Gibbs 	if ((flags & O_NONBLOCK) != 0) {
754f0d9af51SMatt Jacob 		xpt_print(periph->path, "can't do nonblocking access\n");
755c552ebe1SKenneth D. Merry 		cam_periph_release_locked(periph);
7562b83592fSScott Long 		cam_periph_unlock(periph);
75722b9c86cSKenneth D. Merry 		return(EINVAL);
75876babe50SJustin T. Gibbs 	}
75976babe50SJustin T. Gibbs 
76086d45c7fSKenneth D. Merry 	softc->open_count++;
76186d45c7fSKenneth D. Merry 
762835187bfSScott Long 	cam_periph_unlock(periph);
76376babe50SJustin T. Gibbs 
76476babe50SJustin T. Gibbs 	return (error);
76576babe50SJustin T. Gibbs }
76676babe50SJustin T. Gibbs 
76776babe50SJustin T. Gibbs static int
passclose(struct cdev * dev,int flag,int fmt,struct thread * td)76889c9c53dSPoul-Henning Kamp passclose(struct cdev *dev, int flag, int fmt, struct thread *td)
76976babe50SJustin T. Gibbs {
77076babe50SJustin T. Gibbs 	struct 	cam_periph *periph;
77186d45c7fSKenneth D. Merry 	struct  pass_softc *softc;
772227d67aaSAlexander Motin 	struct mtx *mtx;
77376babe50SJustin T. Gibbs 
774e2a5fdf9SNate Lawson 	periph = (struct cam_periph *)dev->si_drv1;
775227d67aaSAlexander Motin 	mtx = cam_periph_mtx(periph);
776227d67aaSAlexander Motin 	mtx_lock(mtx);
77776babe50SJustin T. Gibbs 
77886d45c7fSKenneth D. Merry 	softc = periph->softc;
77986d45c7fSKenneth D. Merry 	softc->open_count--;
78086d45c7fSKenneth D. Merry 
781a9934668SKenneth D. Merry 	if (softc->open_count == 0) {
782a9934668SKenneth D. Merry 		struct pass_io_req *io_req, *io_req2;
783a9934668SKenneth D. Merry 
784a9934668SKenneth D. Merry 		TAILQ_FOREACH_SAFE(io_req, &softc->done_queue, links, io_req2) {
785a9934668SKenneth D. Merry 			TAILQ_REMOVE(&softc->done_queue, io_req, links);
786a9934668SKenneth D. Merry 			passiocleanup(softc, io_req);
787a9934668SKenneth D. Merry 			uma_zfree(softc->pass_zone, io_req);
788a9934668SKenneth D. Merry 		}
789a9934668SKenneth D. Merry 
790a9934668SKenneth D. Merry 		TAILQ_FOREACH_SAFE(io_req, &softc->incoming_queue, links,
791a9934668SKenneth D. Merry 				   io_req2) {
792a9934668SKenneth D. Merry 			TAILQ_REMOVE(&softc->incoming_queue, io_req, links);
793a9934668SKenneth D. Merry 			passiocleanup(softc, io_req);
794a9934668SKenneth D. Merry 			uma_zfree(softc->pass_zone, io_req);
795a9934668SKenneth D. Merry 		}
796a9934668SKenneth D. Merry 
797a9934668SKenneth D. Merry 		/*
798a9934668SKenneth D. Merry 		 * If there are any active I/Os, we need to forcibly acquire a
799a9934668SKenneth D. Merry 		 * reference to the peripheral so that we don't go away
800a9934668SKenneth D. Merry 		 * before they complete.  We'll release the reference when
801a9934668SKenneth D. Merry 		 * the abandoned queue is empty.
802a9934668SKenneth D. Merry 		 */
803a9934668SKenneth D. Merry 		io_req = TAILQ_FIRST(&softc->active_queue);
804a9934668SKenneth D. Merry 		if ((io_req != NULL)
805a9934668SKenneth D. Merry 		 && (softc->flags & PASS_FLAG_ABANDONED_REF_SET) == 0) {
806a9934668SKenneth D. Merry 			cam_periph_doacquire(periph);
807a9934668SKenneth D. Merry 			softc->flags |= PASS_FLAG_ABANDONED_REF_SET;
808a9934668SKenneth D. Merry 		}
809a9934668SKenneth D. Merry 
810a9934668SKenneth D. Merry 		/*
811a9934668SKenneth D. Merry 		 * Since the I/O in the active queue is not under our
812a9934668SKenneth D. Merry 		 * control, just set a flag so that we can clean it up when
813a9934668SKenneth D. Merry 		 * it completes and put it on the abandoned queue.  This
814a9934668SKenneth D. Merry 		 * will prevent our sending spurious completions in the
815a9934668SKenneth D. Merry 		 * event that the device is opened again before these I/Os
816a9934668SKenneth D. Merry 		 * complete.
817a9934668SKenneth D. Merry 		 */
818a9934668SKenneth D. Merry 		TAILQ_FOREACH_SAFE(io_req, &softc->active_queue, links,
819a9934668SKenneth D. Merry 				   io_req2) {
820a9934668SKenneth D. Merry 			TAILQ_REMOVE(&softc->active_queue, io_req, links);
821a9934668SKenneth D. Merry 			io_req->flags |= PASS_IO_ABANDONED;
822a9934668SKenneth D. Merry 			TAILQ_INSERT_TAIL(&softc->abandoned_queue, io_req,
823a9934668SKenneth D. Merry 					  links);
824a9934668SKenneth D. Merry 		}
825a9934668SKenneth D. Merry 	}
826a9934668SKenneth D. Merry 
82786d45c7fSKenneth D. Merry 	cam_periph_release_locked(periph);
82886d45c7fSKenneth D. Merry 
82986d45c7fSKenneth D. Merry 	/*
830227d67aaSAlexander Motin 	 * We reference the lock directly here, instead of using
83186d45c7fSKenneth D. Merry 	 * cam_periph_unlock().  The reason is that the call to
83286d45c7fSKenneth D. Merry 	 * cam_periph_release_locked() above could result in the periph
83386d45c7fSKenneth D. Merry 	 * getting freed.  If that is the case, dereferencing the periph
83486d45c7fSKenneth D. Merry 	 * with a cam_periph_unlock() call would cause a page fault.
83586d45c7fSKenneth D. Merry 	 *
83686d45c7fSKenneth D. Merry 	 * cam_periph_release() avoids this problem using the same method,
83786d45c7fSKenneth D. Merry 	 * but we're manually acquiring and dropping the lock here to
83886d45c7fSKenneth D. Merry 	 * protect the open count and avoid another lock acquisition and
83986d45c7fSKenneth D. Merry 	 * release.
84086d45c7fSKenneth D. Merry 	 */
841227d67aaSAlexander Motin 	mtx_unlock(mtx);
84276babe50SJustin T. Gibbs 
84376babe50SJustin T. Gibbs 	return (0);
84476babe50SJustin T. Gibbs }
84576babe50SJustin T. Gibbs 
846a9934668SKenneth D. Merry static void
passstart(struct cam_periph * periph,union ccb * start_ccb)847a9934668SKenneth D. Merry passstart(struct cam_periph *periph, union ccb *start_ccb)
848a9934668SKenneth D. Merry {
849a9934668SKenneth D. Merry 	struct pass_softc *softc;
850a9934668SKenneth D. Merry 
851a9934668SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
852a9934668SKenneth D. Merry 
853a9934668SKenneth D. Merry 	switch (softc->state) {
854a9934668SKenneth D. Merry 	case PASS_STATE_NORMAL: {
855a9934668SKenneth D. Merry 		struct pass_io_req *io_req;
856a9934668SKenneth D. Merry 
857a9934668SKenneth D. Merry 		/*
858a9934668SKenneth D. Merry 		 * Check for any queued I/O requests that require an
859a9934668SKenneth D. Merry 		 * allocated slot.
860a9934668SKenneth D. Merry 		 */
861a9934668SKenneth D. Merry 		io_req = TAILQ_FIRST(&softc->incoming_queue);
862a9934668SKenneth D. Merry 		if (io_req == NULL) {
863a9934668SKenneth D. Merry 			xpt_release_ccb(start_ccb);
864a9934668SKenneth D. Merry 			break;
865a9934668SKenneth D. Merry 		}
866a9934668SKenneth D. Merry 		TAILQ_REMOVE(&softc->incoming_queue, io_req, links);
867a9934668SKenneth D. Merry 		TAILQ_INSERT_TAIL(&softc->active_queue, io_req, links);
868a9934668SKenneth D. Merry 		/*
869a9934668SKenneth D. Merry 		 * Merge the user's CCB into the allocated CCB.
870a9934668SKenneth D. Merry 		 */
871a9934668SKenneth D. Merry 		xpt_merge_ccb(start_ccb, &io_req->ccb);
872a9934668SKenneth D. Merry 		start_ccb->ccb_h.ccb_type = PASS_CCB_QUEUED_IO;
873a9934668SKenneth D. Merry 		start_ccb->ccb_h.ccb_ioreq = io_req;
874a9934668SKenneth D. Merry 		start_ccb->ccb_h.cbfcnp = passdone;
875a9934668SKenneth D. Merry 		io_req->alloced_ccb = start_ccb;
876a9934668SKenneth D. Merry 		binuptime(&io_req->start_time);
877a9934668SKenneth D. Merry 		devstat_start_transaction(softc->device_stats,
878a9934668SKenneth D. Merry 					  &io_req->start_time);
879a9934668SKenneth D. Merry 
880a9934668SKenneth D. Merry 		xpt_action(start_ccb);
881a9934668SKenneth D. Merry 
882a9934668SKenneth D. Merry 		/*
883a9934668SKenneth D. Merry 		 * If we have any more I/O waiting, schedule ourselves again.
884a9934668SKenneth D. Merry 		 */
885a9934668SKenneth D. Merry 		if (!TAILQ_EMPTY(&softc->incoming_queue))
886a9934668SKenneth D. Merry 			xpt_schedule(periph, CAM_PRIORITY_NORMAL);
887a9934668SKenneth D. Merry 		break;
888a9934668SKenneth D. Merry 	}
889a9934668SKenneth D. Merry 	default:
890a9934668SKenneth D. Merry 		break;
891a9934668SKenneth D. Merry 	}
892a9934668SKenneth D. Merry }
893a9934668SKenneth D. Merry 
894a9934668SKenneth D. Merry static void
passdone(struct cam_periph * periph,union ccb * done_ccb)895a9934668SKenneth D. Merry passdone(struct cam_periph *periph, union ccb *done_ccb)
896a9934668SKenneth D. Merry {
897a9934668SKenneth D. Merry 	struct pass_softc *softc;
898a9934668SKenneth D. Merry 	struct ccb_scsiio *csio;
899a9934668SKenneth D. Merry 
900a9934668SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
901a9934668SKenneth D. Merry 
902a9934668SKenneth D. Merry 	cam_periph_assert(periph, MA_OWNED);
903a9934668SKenneth D. Merry 
904a9934668SKenneth D. Merry 	csio = &done_ccb->csio;
905a9934668SKenneth D. Merry 	switch (csio->ccb_h.ccb_type) {
906a9934668SKenneth D. Merry 	case PASS_CCB_QUEUED_IO: {
907a9934668SKenneth D. Merry 		struct pass_io_req *io_req;
908a9934668SKenneth D. Merry 
909a9934668SKenneth D. Merry 		io_req = done_ccb->ccb_h.ccb_ioreq;
910a9934668SKenneth D. Merry #if 0
911a9934668SKenneth D. Merry 		xpt_print(periph->path, "%s: called for user CCB %p\n",
912a9934668SKenneth D. Merry 			  __func__, io_req->user_ccb_ptr);
913a9934668SKenneth D. Merry #endif
914e474a8e2SWarner Losh 		if (((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) &&
915e474a8e2SWarner Losh 		    ((io_req->flags & PASS_IO_ABANDONED) == 0)) {
916a9934668SKenneth D. Merry 			int error;
917e474a8e2SWarner Losh 			uint32_t cam_flags, sense_flags;
918a9934668SKenneth D. Merry 
919e474a8e2SWarner Losh 			passflags(done_ccb, &cam_flags, &sense_flags);
920e474a8e2SWarner Losh 			error = passerror(done_ccb, cam_flags, sense_flags);
921a9934668SKenneth D. Merry 
922a9934668SKenneth D. Merry 			if (error == ERESTART) {
923e474a8e2SWarner Losh 				KASSERT(((sense_flags & SF_NO_RETRY) == 0),
924e474a8e2SWarner Losh 				    ("passerror returned ERESTART with no retry requested\n"));
925a9934668SKenneth D. Merry 				return;
926a9934668SKenneth D. Merry 			}
927a9934668SKenneth D. Merry 		}
928a9934668SKenneth D. Merry 
929a9934668SKenneth D. Merry 		/*
930a9934668SKenneth D. Merry 		 * Copy the allocated CCB contents back to the malloced CCB
931a9934668SKenneth D. Merry 		 * so we can give status back to the user when he requests it.
932a9934668SKenneth D. Merry 		 */
933a9934668SKenneth D. Merry 		bcopy(done_ccb, &io_req->ccb, sizeof(*done_ccb));
934a9934668SKenneth D. Merry 
935a9934668SKenneth D. Merry 		/*
936a9934668SKenneth D. Merry 		 * Log data/transaction completion with devstat(9).
937a9934668SKenneth D. Merry 		 */
938a9934668SKenneth D. Merry 		switch (done_ccb->ccb_h.func_code) {
939a9934668SKenneth D. Merry 		case XPT_SCSI_IO:
940a9934668SKenneth D. Merry 			devstat_end_transaction(softc->device_stats,
941a9934668SKenneth D. Merry 			    done_ccb->csio.dxfer_len - done_ccb->csio.resid,
942a9934668SKenneth D. Merry 			    done_ccb->csio.tag_action & 0x3,
943a9934668SKenneth D. Merry 			    ((done_ccb->ccb_h.flags & CAM_DIR_MASK) ==
944a9934668SKenneth D. Merry 			    CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
945a9934668SKenneth D. Merry 			    (done_ccb->ccb_h.flags & CAM_DIR_OUT) ?
946a9934668SKenneth D. Merry 			    DEVSTAT_WRITE : DEVSTAT_READ, NULL,
947a9934668SKenneth D. Merry 			    &io_req->start_time);
948a9934668SKenneth D. Merry 			break;
949a9934668SKenneth D. Merry 		case XPT_ATA_IO:
950a9934668SKenneth D. Merry 			devstat_end_transaction(softc->device_stats,
951a9934668SKenneth D. Merry 			    done_ccb->ataio.dxfer_len - done_ccb->ataio.resid,
952e4cc6558SWarner Losh 			    0, /* Not used in ATA */
953a9934668SKenneth D. Merry 			    ((done_ccb->ccb_h.flags & CAM_DIR_MASK) ==
954a9934668SKenneth D. Merry 			    CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
955a9934668SKenneth D. Merry 			    (done_ccb->ccb_h.flags & CAM_DIR_OUT) ?
956a9934668SKenneth D. Merry 			    DEVSTAT_WRITE : DEVSTAT_READ, NULL,
957a9934668SKenneth D. Merry 			    &io_req->start_time);
958a9934668SKenneth D. Merry 			break;
959a9934668SKenneth D. Merry 		case XPT_SMP_IO:
960a9934668SKenneth D. Merry 			/*
961a9934668SKenneth D. Merry 			 * XXX KDM this isn't quite right, but there isn't
962a9934668SKenneth D. Merry 			 * currently an easy way to represent a bidirectional
963a9934668SKenneth D. Merry 			 * transfer in devstat.  The only way to do it
964a9934668SKenneth D. Merry 			 * and have the byte counts come out right would
965a9934668SKenneth D. Merry 			 * mean that we would have to record two
966a9934668SKenneth D. Merry 			 * transactions, one for the request and one for the
967a9934668SKenneth D. Merry 			 * response.  For now, so that we report something,
968a9934668SKenneth D. Merry 			 * just treat the entire thing as a read.
969a9934668SKenneth D. Merry 			 */
970a9934668SKenneth D. Merry 			devstat_end_transaction(softc->device_stats,
971a9934668SKenneth D. Merry 			    done_ccb->smpio.smp_request_len +
972a9934668SKenneth D. Merry 			    done_ccb->smpio.smp_response_len,
973a9934668SKenneth D. Merry 			    DEVSTAT_TAG_SIMPLE, DEVSTAT_READ, NULL,
974a9934668SKenneth D. Merry 			    &io_req->start_time);
975a9934668SKenneth D. Merry 			break;
976a9934668SKenneth D. Merry 		default:
977a9934668SKenneth D. Merry 			devstat_end_transaction(softc->device_stats, 0,
978a9934668SKenneth D. Merry 			    DEVSTAT_TAG_NONE, DEVSTAT_NO_DATA, NULL,
979a9934668SKenneth D. Merry 			    &io_req->start_time);
980a9934668SKenneth D. Merry 			break;
981a9934668SKenneth D. Merry 		}
982a9934668SKenneth D. Merry 
983a9934668SKenneth D. Merry 		/*
984a9934668SKenneth D. Merry 		 * In the normal case, take the completed I/O off of the
985a9934668SKenneth D. Merry 		 * active queue and put it on the done queue.  Notitfy the
986a9934668SKenneth D. Merry 		 * user that we have a completed I/O.
987a9934668SKenneth D. Merry 		 */
988a9934668SKenneth D. Merry 		if ((io_req->flags & PASS_IO_ABANDONED) == 0) {
989a9934668SKenneth D. Merry 			TAILQ_REMOVE(&softc->active_queue, io_req, links);
990a9934668SKenneth D. Merry 			TAILQ_INSERT_TAIL(&softc->done_queue, io_req, links);
991a9934668SKenneth D. Merry 			selwakeuppri(&softc->read_select, PRIBIO);
992a9934668SKenneth D. Merry 			KNOTE_LOCKED(&softc->read_select.si_note, 0);
993a9934668SKenneth D. Merry 		} else {
994a9934668SKenneth D. Merry 			/*
995a9934668SKenneth D. Merry 			 * In the case of an abandoned I/O (final close
996a9934668SKenneth D. Merry 			 * without fetching the I/O), take it off of the
997a9934668SKenneth D. Merry 			 * abandoned queue and free it.
998a9934668SKenneth D. Merry 			 */
999a9934668SKenneth D. Merry 			TAILQ_REMOVE(&softc->abandoned_queue, io_req, links);
1000a9934668SKenneth D. Merry 			passiocleanup(softc, io_req);
1001a9934668SKenneth D. Merry 			uma_zfree(softc->pass_zone, io_req);
1002a9934668SKenneth D. Merry 
1003a9934668SKenneth D. Merry 			/*
1004a9934668SKenneth D. Merry 			 * Release the done_ccb here, since we may wind up
1005a9934668SKenneth D. Merry 			 * freeing the peripheral when we decrement the
1006a9934668SKenneth D. Merry 			 * reference count below.
1007a9934668SKenneth D. Merry 			 */
1008a9934668SKenneth D. Merry 			xpt_release_ccb(done_ccb);
1009a9934668SKenneth D. Merry 
1010a9934668SKenneth D. Merry 			/*
1011a9934668SKenneth D. Merry 			 * If the abandoned queue is empty, we can release
1012a9934668SKenneth D. Merry 			 * our reference to the periph since we won't have
1013a9934668SKenneth D. Merry 			 * any more completions coming.
1014a9934668SKenneth D. Merry 			 */
1015a9934668SKenneth D. Merry 			if ((TAILQ_EMPTY(&softc->abandoned_queue))
1016a9934668SKenneth D. Merry 			 && (softc->flags & PASS_FLAG_ABANDONED_REF_SET)) {
1017a9934668SKenneth D. Merry 				softc->flags &= ~PASS_FLAG_ABANDONED_REF_SET;
1018a9934668SKenneth D. Merry 				cam_periph_release_locked(periph);
1019a9934668SKenneth D. Merry 			}
1020a9934668SKenneth D. Merry 
1021a9934668SKenneth D. Merry 			/*
1022a9934668SKenneth D. Merry 			 * We have already released the CCB, so we can
1023a9934668SKenneth D. Merry 			 * return.
1024a9934668SKenneth D. Merry 			 */
1025a9934668SKenneth D. Merry 			return;
1026a9934668SKenneth D. Merry 		}
1027a9934668SKenneth D. Merry 		break;
1028a9934668SKenneth D. Merry 	}
1029a9934668SKenneth D. Merry 	}
1030a9934668SKenneth D. Merry 	xpt_release_ccb(done_ccb);
1031a9934668SKenneth D. Merry }
1032a9934668SKenneth D. Merry 
1033a9934668SKenneth D. Merry static int
passcreatezone(struct cam_periph * periph)1034a9934668SKenneth D. Merry passcreatezone(struct cam_periph *periph)
1035a9934668SKenneth D. Merry {
1036a9934668SKenneth D. Merry 	struct pass_softc *softc;
1037a9934668SKenneth D. Merry 	int error;
1038a9934668SKenneth D. Merry 
1039a9934668SKenneth D. Merry 	error = 0;
1040a9934668SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
1041a9934668SKenneth D. Merry 
1042a9934668SKenneth D. Merry 	cam_periph_assert(periph, MA_OWNED);
1043a9934668SKenneth D. Merry 	KASSERT(((softc->flags & PASS_FLAG_ZONE_VALID) == 0),
1044a9934668SKenneth D. Merry 		("%s called when the pass(4) zone is valid!\n", __func__));
1045a9934668SKenneth D. Merry 	KASSERT((softc->pass_zone == NULL),
1046a9934668SKenneth D. Merry 		("%s called when the pass(4) zone is allocated!\n", __func__));
1047a9934668SKenneth D. Merry 
1048a9934668SKenneth D. Merry 	if ((softc->flags & PASS_FLAG_ZONE_INPROG) == 0) {
1049a9934668SKenneth D. Merry 		/*
1050a9934668SKenneth D. Merry 		 * We're the first context through, so we need to create
1051a9934668SKenneth D. Merry 		 * the pass(4) UMA zone for I/O requests.
1052a9934668SKenneth D. Merry 		 */
1053a9934668SKenneth D. Merry 		softc->flags |= PASS_FLAG_ZONE_INPROG;
1054a9934668SKenneth D. Merry 
1055a9934668SKenneth D. Merry 		/*
1056a9934668SKenneth D. Merry 		 * uma_zcreate() does a blocking (M_WAITOK) allocation,
1057a9934668SKenneth D. Merry 		 * so we cannot hold a mutex while we call it.
1058a9934668SKenneth D. Merry 		 */
1059a9934668SKenneth D. Merry 		cam_periph_unlock(periph);
1060a9934668SKenneth D. Merry 
1061a9934668SKenneth D. Merry 		softc->pass_zone = uma_zcreate(softc->zone_name,
1062a9934668SKenneth D. Merry 		    sizeof(struct pass_io_req), NULL, NULL, NULL, NULL,
1063a9934668SKenneth D. Merry 		    /*align*/ 0, /*flags*/ 0);
1064a9934668SKenneth D. Merry 
1065a9934668SKenneth D. Merry 		softc->pass_io_zone = uma_zcreate(softc->io_zone_name,
1066a9934668SKenneth D. Merry 		    softc->io_zone_size, NULL, NULL, NULL, NULL,
1067a9934668SKenneth D. Merry 		    /*align*/ 0, /*flags*/ 0);
1068a9934668SKenneth D. Merry 
1069a9934668SKenneth D. Merry 		cam_periph_lock(periph);
1070a9934668SKenneth D. Merry 
1071a9934668SKenneth D. Merry 		if ((softc->pass_zone == NULL)
1072a9934668SKenneth D. Merry 		 || (softc->pass_io_zone == NULL)) {
1073a9934668SKenneth D. Merry 			if (softc->pass_zone == NULL)
1074a9934668SKenneth D. Merry 				xpt_print(periph->path, "unable to allocate "
1075a9934668SKenneth D. Merry 				    "IO Req UMA zone\n");
1076a9934668SKenneth D. Merry 			else
1077a9934668SKenneth D. Merry 				xpt_print(periph->path, "unable to allocate "
1078a9934668SKenneth D. Merry 				    "IO UMA zone\n");
1079a9934668SKenneth D. Merry 			softc->flags &= ~PASS_FLAG_ZONE_INPROG;
1080a9934668SKenneth D. Merry 			goto bailout;
1081a9934668SKenneth D. Merry 		}
1082a9934668SKenneth D. Merry 
1083a9934668SKenneth D. Merry 		/*
1084a9934668SKenneth D. Merry 		 * Set the flags appropriately and notify any other waiters.
1085a9934668SKenneth D. Merry 		 */
1086ca2a7262SKenneth D. Merry 		softc->flags &= ~PASS_FLAG_ZONE_INPROG;
1087a9934668SKenneth D. Merry 		softc->flags |= PASS_FLAG_ZONE_VALID;
1088a9934668SKenneth D. Merry 		wakeup(&softc->pass_zone);
1089a9934668SKenneth D. Merry 	} else {
1090a9934668SKenneth D. Merry 		/*
1091a9934668SKenneth D. Merry 		 * In this case, the UMA zone has not yet been created, but
1092a9934668SKenneth D. Merry 		 * another context is in the process of creating it.  We
1093a9934668SKenneth D. Merry 		 * need to sleep until the creation is either done or has
1094a9934668SKenneth D. Merry 		 * failed.
1095a9934668SKenneth D. Merry 		 */
1096a9934668SKenneth D. Merry 		while ((softc->flags & PASS_FLAG_ZONE_INPROG)
1097a9934668SKenneth D. Merry 		    && ((softc->flags & PASS_FLAG_ZONE_VALID) == 0)) {
1098a9934668SKenneth D. Merry 			error = msleep(&softc->pass_zone,
1099a9934668SKenneth D. Merry 				       cam_periph_mtx(periph), PRIBIO,
1100a9934668SKenneth D. Merry 				       "paszon", 0);
1101a9934668SKenneth D. Merry 			if (error != 0)
1102a9934668SKenneth D. Merry 				goto bailout;
1103a9934668SKenneth D. Merry 		}
1104a9934668SKenneth D. Merry 		/*
1105a9934668SKenneth D. Merry 		 * If the zone creation failed, no luck for the user.
1106a9934668SKenneth D. Merry 		 */
1107a9934668SKenneth D. Merry 		if ((softc->flags & PASS_FLAG_ZONE_VALID) == 0){
1108a9934668SKenneth D. Merry 			error = ENOMEM;
1109a9934668SKenneth D. Merry 			goto bailout;
1110a9934668SKenneth D. Merry 		}
1111a9934668SKenneth D. Merry 	}
1112a9934668SKenneth D. Merry bailout:
1113a9934668SKenneth D. Merry 	return (error);
1114a9934668SKenneth D. Merry }
1115a9934668SKenneth D. Merry 
1116a9934668SKenneth D. Merry static void
passiocleanup(struct pass_softc * softc,struct pass_io_req * io_req)1117a9934668SKenneth D. Merry passiocleanup(struct pass_softc *softc, struct pass_io_req *io_req)
1118a9934668SKenneth D. Merry {
1119a9934668SKenneth D. Merry 	union ccb *ccb;
11207c5d20a6SWarner Losh 	uint8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
1121a9934668SKenneth D. Merry 	int i, numbufs;
1122a9934668SKenneth D. Merry 
1123a9934668SKenneth D. Merry 	ccb = &io_req->ccb;
1124a9934668SKenneth D. Merry 
1125a9934668SKenneth D. Merry 	switch (ccb->ccb_h.func_code) {
1126a9934668SKenneth D. Merry 	case XPT_DEV_MATCH:
1127a9934668SKenneth D. Merry 		numbufs = min(io_req->num_bufs, 2);
1128a9934668SKenneth D. Merry 
1129a9934668SKenneth D. Merry 		if (numbufs == 1) {
11307c5d20a6SWarner Losh 			data_ptrs[0] = (uint8_t **)&ccb->cdm.matches;
1131a9934668SKenneth D. Merry 		} else {
11327c5d20a6SWarner Losh 			data_ptrs[0] = (uint8_t **)&ccb->cdm.patterns;
11337c5d20a6SWarner Losh 			data_ptrs[1] = (uint8_t **)&ccb->cdm.matches;
1134a9934668SKenneth D. Merry 		}
1135a9934668SKenneth D. Merry 		break;
1136a9934668SKenneth D. Merry 	case XPT_SCSI_IO:
1137a9934668SKenneth D. Merry 	case XPT_CONT_TARGET_IO:
1138a9934668SKenneth D. Merry 		data_ptrs[0] = &ccb->csio.data_ptr;
1139a9934668SKenneth D. Merry 		numbufs = min(io_req->num_bufs, 1);
1140a9934668SKenneth D. Merry 		break;
1141a9934668SKenneth D. Merry 	case XPT_ATA_IO:
1142a9934668SKenneth D. Merry 		data_ptrs[0] = &ccb->ataio.data_ptr;
1143a9934668SKenneth D. Merry 		numbufs = min(io_req->num_bufs, 1);
1144a9934668SKenneth D. Merry 		break;
1145a9934668SKenneth D. Merry 	case XPT_SMP_IO:
1146a9934668SKenneth D. Merry 		numbufs = min(io_req->num_bufs, 2);
1147a9934668SKenneth D. Merry 		data_ptrs[0] = &ccb->smpio.smp_request;
1148a9934668SKenneth D. Merry 		data_ptrs[1] = &ccb->smpio.smp_response;
1149a9934668SKenneth D. Merry 		break;
1150a9934668SKenneth D. Merry 	case XPT_DEV_ADVINFO:
1151a9934668SKenneth D. Merry 		numbufs = min(io_req->num_bufs, 1);
1152a9934668SKenneth D. Merry 		data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
1153a9934668SKenneth D. Merry 		break;
1154df424515SWarner Losh 	case XPT_NVME_IO:
1155df424515SWarner Losh 	case XPT_NVME_ADMIN:
1156df424515SWarner Losh 		data_ptrs[0] = &ccb->nvmeio.data_ptr;
1157df424515SWarner Losh 		numbufs = min(io_req->num_bufs, 1);
1158df424515SWarner Losh 		break;
1159a9934668SKenneth D. Merry 	default:
1160a9934668SKenneth D. Merry 		/* allow ourselves to be swapped once again */
1161a9934668SKenneth D. Merry 		return;
1162a9934668SKenneth D. Merry 		break; /* NOTREACHED */
1163a9934668SKenneth D. Merry 	}
1164a9934668SKenneth D. Merry 
1165a9934668SKenneth D. Merry 	if (io_req->flags & PASS_IO_USER_SEG_MALLOC) {
1166a9934668SKenneth D. Merry 		free(io_req->user_segptr, M_SCSIPASS);
1167a9934668SKenneth D. Merry 		io_req->user_segptr = NULL;
1168a9934668SKenneth D. Merry 	}
1169a9934668SKenneth D. Merry 
1170a9934668SKenneth D. Merry 	/*
1171a9934668SKenneth D. Merry 	 * We only want to free memory we malloced.
1172a9934668SKenneth D. Merry 	 */
1173a9934668SKenneth D. Merry 	if (io_req->data_flags == CAM_DATA_VADDR) {
1174a9934668SKenneth D. Merry 		for (i = 0; i < io_req->num_bufs; i++) {
1175a9934668SKenneth D. Merry 			if (io_req->kern_bufs[i] == NULL)
1176a9934668SKenneth D. Merry 				continue;
1177a9934668SKenneth D. Merry 
1178a9934668SKenneth D. Merry 			free(io_req->kern_bufs[i], M_SCSIPASS);
1179a9934668SKenneth D. Merry 			io_req->kern_bufs[i] = NULL;
1180a9934668SKenneth D. Merry 		}
1181a9934668SKenneth D. Merry 	} else if (io_req->data_flags == CAM_DATA_SG) {
1182a9934668SKenneth D. Merry 		for (i = 0; i < io_req->num_kern_segs; i++) {
1183a9934668SKenneth D. Merry 			if ((uint8_t *)(uintptr_t)
1184a9934668SKenneth D. Merry 			    io_req->kern_segptr[i].ds_addr == NULL)
1185a9934668SKenneth D. Merry 				continue;
1186a9934668SKenneth D. Merry 
1187a9934668SKenneth D. Merry 			uma_zfree(softc->pass_io_zone, (uint8_t *)(uintptr_t)
1188a9934668SKenneth D. Merry 			    io_req->kern_segptr[i].ds_addr);
1189a9934668SKenneth D. Merry 			io_req->kern_segptr[i].ds_addr = 0;
1190a9934668SKenneth D. Merry 		}
1191a9934668SKenneth D. Merry 	}
1192a9934668SKenneth D. Merry 
1193a9934668SKenneth D. Merry 	if (io_req->flags & PASS_IO_KERN_SEG_MALLOC) {
1194a9934668SKenneth D. Merry 		free(io_req->kern_segptr, M_SCSIPASS);
1195a9934668SKenneth D. Merry 		io_req->kern_segptr = NULL;
1196a9934668SKenneth D. Merry 	}
1197a9934668SKenneth D. Merry 
1198a9934668SKenneth D. Merry 	if (io_req->data_flags != CAM_DATA_PADDR) {
1199a9934668SKenneth D. Merry 		for (i = 0; i < numbufs; i++) {
1200a9934668SKenneth D. Merry 			/*
1201a9934668SKenneth D. Merry 			 * Restore the user's buffer pointers to their
1202a9934668SKenneth D. Merry 			 * previous values.
1203a9934668SKenneth D. Merry 			 */
1204a9934668SKenneth D. Merry 			if (io_req->user_bufs[i] != NULL)
1205a9934668SKenneth D. Merry 				*data_ptrs[i] = io_req->user_bufs[i];
1206a9934668SKenneth D. Merry 		}
1207a9934668SKenneth D. Merry 	}
1208a9934668SKenneth D. Merry 
1209a9934668SKenneth D. Merry }
1210a9934668SKenneth D. Merry 
1211a9934668SKenneth D. Merry static int
passcopysglist(struct cam_periph * periph,struct pass_io_req * io_req,ccb_flags direction)1212a9934668SKenneth D. Merry passcopysglist(struct cam_periph *periph, struct pass_io_req *io_req,
1213a9934668SKenneth D. Merry 	       ccb_flags direction)
1214a9934668SKenneth D. Merry {
12156637b746SWarner Losh 	bus_size_t kern_watermark, user_watermark, len_to_copy;
1216a9934668SKenneth D. Merry 	bus_dma_segment_t *user_sglist, *kern_sglist;
1217a9934668SKenneth D. Merry 	int i, j, error;
1218a9934668SKenneth D. Merry 
1219a9934668SKenneth D. Merry 	error = 0;
1220a9934668SKenneth D. Merry 	kern_watermark = 0;
1221a9934668SKenneth D. Merry 	user_watermark = 0;
1222a9934668SKenneth D. Merry 	len_to_copy = 0;
1223a9934668SKenneth D. Merry 	user_sglist = io_req->user_segptr;
1224a9934668SKenneth D. Merry 	kern_sglist = io_req->kern_segptr;
1225a9934668SKenneth D. Merry 
1226a9934668SKenneth D. Merry 	for (i = 0, j = 0; i < io_req->num_user_segs &&
1227a9934668SKenneth D. Merry 	     j < io_req->num_kern_segs;) {
1228a9934668SKenneth D. Merry 		uint8_t *user_ptr, *kern_ptr;
1229a9934668SKenneth D. Merry 
1230a9934668SKenneth D. Merry 		len_to_copy = min(user_sglist[i].ds_len -user_watermark,
1231a9934668SKenneth D. Merry 		    kern_sglist[j].ds_len - kern_watermark);
1232a9934668SKenneth D. Merry 
1233a9934668SKenneth D. Merry 		user_ptr = (uint8_t *)(uintptr_t)user_sglist[i].ds_addr;
1234a9934668SKenneth D. Merry 		user_ptr = user_ptr + user_watermark;
1235a9934668SKenneth D. Merry 		kern_ptr = (uint8_t *)(uintptr_t)kern_sglist[j].ds_addr;
1236a9934668SKenneth D. Merry 		kern_ptr = kern_ptr + kern_watermark;
1237a9934668SKenneth D. Merry 
1238a9934668SKenneth D. Merry 		user_watermark += len_to_copy;
1239a9934668SKenneth D. Merry 		kern_watermark += len_to_copy;
1240a9934668SKenneth D. Merry 
1241a9934668SKenneth D. Merry 		if (direction == CAM_DIR_IN) {
1242a9934668SKenneth D. Merry 			error = copyout(kern_ptr, user_ptr, len_to_copy);
1243a9934668SKenneth D. Merry 			if (error != 0) {
1244a9934668SKenneth D. Merry 				xpt_print(periph->path, "%s: copyout of %u "
1245a9934668SKenneth D. Merry 					  "bytes from %p to %p failed with "
1246a9934668SKenneth D. Merry 					  "error %d\n", __func__, len_to_copy,
1247a9934668SKenneth D. Merry 					  kern_ptr, user_ptr, error);
1248a9934668SKenneth D. Merry 				goto bailout;
1249a9934668SKenneth D. Merry 			}
1250a9934668SKenneth D. Merry 		} else {
1251a9934668SKenneth D. Merry 			error = copyin(user_ptr, kern_ptr, len_to_copy);
1252a9934668SKenneth D. Merry 			if (error != 0) {
1253a9934668SKenneth D. Merry 				xpt_print(periph->path, "%s: copyin of %u "
1254a9934668SKenneth D. Merry 					  "bytes from %p to %p failed with "
1255a9934668SKenneth D. Merry 					  "error %d\n", __func__, len_to_copy,
1256a9934668SKenneth D. Merry 					  user_ptr, kern_ptr, error);
1257a9934668SKenneth D. Merry 				goto bailout;
1258a9934668SKenneth D. Merry 			}
1259a9934668SKenneth D. Merry 		}
1260a9934668SKenneth D. Merry 
1261a9934668SKenneth D. Merry 		if (user_sglist[i].ds_len == user_watermark) {
1262a9934668SKenneth D. Merry 			i++;
1263a9934668SKenneth D. Merry 			user_watermark = 0;
1264a9934668SKenneth D. Merry 		}
1265a9934668SKenneth D. Merry 
1266a9934668SKenneth D. Merry 		if (kern_sglist[j].ds_len == kern_watermark) {
1267a9934668SKenneth D. Merry 			j++;
1268a9934668SKenneth D. Merry 			kern_watermark = 0;
1269a9934668SKenneth D. Merry 		}
1270a9934668SKenneth D. Merry 	}
1271a9934668SKenneth D. Merry 
1272a9934668SKenneth D. Merry bailout:
1273a9934668SKenneth D. Merry 
1274a9934668SKenneth D. Merry 	return (error);
1275a9934668SKenneth D. Merry }
1276a9934668SKenneth D. Merry 
1277a9934668SKenneth D. Merry static int
passmemsetup(struct cam_periph * periph,struct pass_io_req * io_req)1278a9934668SKenneth D. Merry passmemsetup(struct cam_periph *periph, struct pass_io_req *io_req)
1279a9934668SKenneth D. Merry {
1280a9934668SKenneth D. Merry 	union ccb *ccb;
1281a9934668SKenneth D. Merry 	struct pass_softc *softc;
1282a9934668SKenneth D. Merry 	int numbufs, i;
1283a9934668SKenneth D. Merry 	uint8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
1284a9934668SKenneth D. Merry 	uint32_t lengths[CAM_PERIPH_MAXMAPS];
1285a9934668SKenneth D. Merry 	uint32_t dirs[CAM_PERIPH_MAXMAPS];
1286a9934668SKenneth D. Merry 	uint32_t num_segs;
1287a9934668SKenneth D. Merry 	uint16_t *seg_cnt_ptr;
1288a9934668SKenneth D. Merry 	size_t maxmap;
1289a9934668SKenneth D. Merry 	int error;
1290a9934668SKenneth D. Merry 
1291a9934668SKenneth D. Merry 	cam_periph_assert(periph, MA_NOTOWNED);
1292a9934668SKenneth D. Merry 
1293a9934668SKenneth D. Merry 	softc = periph->softc;
1294a9934668SKenneth D. Merry 
1295a9934668SKenneth D. Merry 	error = 0;
1296a9934668SKenneth D. Merry 	ccb = &io_req->ccb;
1297a9934668SKenneth D. Merry 	maxmap = 0;
1298a9934668SKenneth D. Merry 	num_segs = 0;
1299a9934668SKenneth D. Merry 	seg_cnt_ptr = NULL;
1300a9934668SKenneth D. Merry 
1301a9934668SKenneth D. Merry 	switch(ccb->ccb_h.func_code) {
1302a9934668SKenneth D. Merry 	case XPT_DEV_MATCH:
1303a9934668SKenneth D. Merry 		if (ccb->cdm.match_buf_len == 0) {
1304a9934668SKenneth D. Merry 			printf("%s: invalid match buffer length 0\n", __func__);
1305a9934668SKenneth D. Merry 			return(EINVAL);
1306a9934668SKenneth D. Merry 		}
1307a9934668SKenneth D. Merry 		if (ccb->cdm.pattern_buf_len > 0) {
13087c5d20a6SWarner Losh 			data_ptrs[0] = (uint8_t **)&ccb->cdm.patterns;
1309a9934668SKenneth D. Merry 			lengths[0] = ccb->cdm.pattern_buf_len;
1310a9934668SKenneth D. Merry 			dirs[0] = CAM_DIR_OUT;
13117c5d20a6SWarner Losh 			data_ptrs[1] = (uint8_t **)&ccb->cdm.matches;
1312a9934668SKenneth D. Merry 			lengths[1] = ccb->cdm.match_buf_len;
1313a9934668SKenneth D. Merry 			dirs[1] = CAM_DIR_IN;
1314a9934668SKenneth D. Merry 			numbufs = 2;
1315a9934668SKenneth D. Merry 		} else {
13167c5d20a6SWarner Losh 			data_ptrs[0] = (uint8_t **)&ccb->cdm.matches;
1317a9934668SKenneth D. Merry 			lengths[0] = ccb->cdm.match_buf_len;
1318a9934668SKenneth D. Merry 			dirs[0] = CAM_DIR_IN;
1319a9934668SKenneth D. Merry 			numbufs = 1;
1320a9934668SKenneth D. Merry 		}
1321a9934668SKenneth D. Merry 		io_req->data_flags = CAM_DATA_VADDR;
1322a9934668SKenneth D. Merry 		break;
1323a9934668SKenneth D. Merry 	case XPT_SCSI_IO:
1324a9934668SKenneth D. Merry 	case XPT_CONT_TARGET_IO:
1325a9934668SKenneth D. Merry 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
1326a9934668SKenneth D. Merry 			return(0);
1327a9934668SKenneth D. Merry 
1328a9934668SKenneth D. Merry 		/*
1329a9934668SKenneth D. Merry 		 * The user shouldn't be able to supply a bio.
1330a9934668SKenneth D. Merry 		 */
1331a9934668SKenneth D. Merry 		if ((ccb->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_BIO)
1332a9934668SKenneth D. Merry 			return (EINVAL);
1333a9934668SKenneth D. Merry 
1334a9934668SKenneth D. Merry 		io_req->data_flags = ccb->ccb_h.flags & CAM_DATA_MASK;
1335a9934668SKenneth D. Merry 
1336a9934668SKenneth D. Merry 		data_ptrs[0] = &ccb->csio.data_ptr;
1337a9934668SKenneth D. Merry 		lengths[0] = ccb->csio.dxfer_len;
1338a9934668SKenneth D. Merry 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
1339a9934668SKenneth D. Merry 		num_segs = ccb->csio.sglist_cnt;
1340a9934668SKenneth D. Merry 		seg_cnt_ptr = &ccb->csio.sglist_cnt;
1341a9934668SKenneth D. Merry 		numbufs = 1;
1342a9934668SKenneth D. Merry 		maxmap = softc->maxio;
1343a9934668SKenneth D. Merry 		break;
1344a9934668SKenneth D. Merry 	case XPT_ATA_IO:
1345a9934668SKenneth D. Merry 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
1346a9934668SKenneth D. Merry 			return(0);
1347a9934668SKenneth D. Merry 
1348a9934668SKenneth D. Merry 		/*
1349a9934668SKenneth D. Merry 		 * We only support a single virtual address for ATA I/O.
1350a9934668SKenneth D. Merry 		 */
1351a9934668SKenneth D. Merry 		if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
1352a9934668SKenneth D. Merry 			return (EINVAL);
1353a9934668SKenneth D. Merry 
1354a9934668SKenneth D. Merry 		io_req->data_flags = CAM_DATA_VADDR;
1355a9934668SKenneth D. Merry 
1356a9934668SKenneth D. Merry 		data_ptrs[0] = &ccb->ataio.data_ptr;
1357a9934668SKenneth D. Merry 		lengths[0] = ccb->ataio.dxfer_len;
1358a9934668SKenneth D. Merry 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
1359a9934668SKenneth D. Merry 		numbufs = 1;
1360a9934668SKenneth D. Merry 		maxmap = softc->maxio;
1361a9934668SKenneth D. Merry 		break;
1362a9934668SKenneth D. Merry 	case XPT_SMP_IO:
1363a9934668SKenneth D. Merry 		io_req->data_flags = CAM_DATA_VADDR;
1364a9934668SKenneth D. Merry 
1365a9934668SKenneth D. Merry 		data_ptrs[0] = &ccb->smpio.smp_request;
1366a9934668SKenneth D. Merry 		lengths[0] = ccb->smpio.smp_request_len;
1367a9934668SKenneth D. Merry 		dirs[0] = CAM_DIR_OUT;
1368a9934668SKenneth D. Merry 		data_ptrs[1] = &ccb->smpio.smp_response;
1369a9934668SKenneth D. Merry 		lengths[1] = ccb->smpio.smp_response_len;
1370a9934668SKenneth D. Merry 		dirs[1] = CAM_DIR_IN;
1371a9934668SKenneth D. Merry 		numbufs = 2;
1372a9934668SKenneth D. Merry 		maxmap = softc->maxio;
1373a9934668SKenneth D. Merry 		break;
1374a9934668SKenneth D. Merry 	case XPT_DEV_ADVINFO:
1375a9934668SKenneth D. Merry 		if (ccb->cdai.bufsiz == 0)
1376a9934668SKenneth D. Merry 			return (0);
1377a9934668SKenneth D. Merry 
1378a9934668SKenneth D. Merry 		io_req->data_flags = CAM_DATA_VADDR;
1379a9934668SKenneth D. Merry 
1380a9934668SKenneth D. Merry 		data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
1381a9934668SKenneth D. Merry 		lengths[0] = ccb->cdai.bufsiz;
1382a9934668SKenneth D. Merry 		dirs[0] = CAM_DIR_IN;
1383a9934668SKenneth D. Merry 		numbufs = 1;
1384a9934668SKenneth D. Merry 		break;
1385df424515SWarner Losh 	case XPT_NVME_ADMIN:
1386df424515SWarner Losh 	case XPT_NVME_IO:
1387df424515SWarner Losh 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
1388df424515SWarner Losh 			return (0);
1389df424515SWarner Losh 
1390df424515SWarner Losh 		io_req->data_flags = ccb->ccb_h.flags & CAM_DATA_MASK;
1391df424515SWarner Losh 
1392df424515SWarner Losh 		data_ptrs[0] = &ccb->nvmeio.data_ptr;
1393df424515SWarner Losh 		lengths[0] = ccb->nvmeio.dxfer_len;
1394df424515SWarner Losh 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
139551977281SWarner Losh 		num_segs = ccb->nvmeio.sglist_cnt;
139651977281SWarner Losh 		seg_cnt_ptr = &ccb->nvmeio.sglist_cnt;
1397df424515SWarner Losh 		numbufs = 1;
1398df424515SWarner Losh 		maxmap = softc->maxio;
1399df424515SWarner Losh 		break;
1400a9934668SKenneth D. Merry 	default:
1401a9934668SKenneth D. Merry 		return(EINVAL);
1402a9934668SKenneth D. Merry 		break; /* NOTREACHED */
1403a9934668SKenneth D. Merry 	}
1404a9934668SKenneth D. Merry 
1405a9934668SKenneth D. Merry 	io_req->num_bufs = numbufs;
1406a9934668SKenneth D. Merry 
1407a9934668SKenneth D. Merry 	/*
1408a9934668SKenneth D. Merry 	 * If there is a maximum, check to make sure that the user's
1409a9934668SKenneth D. Merry 	 * request fits within the limit.  In general, we should only have
1410a9934668SKenneth D. Merry 	 * a maximum length for requests that go to hardware.  Otherwise it
1411a9934668SKenneth D. Merry 	 * is whatever we're able to malloc.
1412a9934668SKenneth D. Merry 	 */
1413a9934668SKenneth D. Merry 	for (i = 0; i < numbufs; i++) {
1414a9934668SKenneth D. Merry 		io_req->user_bufs[i] = *data_ptrs[i];
1415a9934668SKenneth D. Merry 		io_req->dirs[i] = dirs[i];
1416a9934668SKenneth D. Merry 		io_req->lengths[i] = lengths[i];
1417a9934668SKenneth D. Merry 
1418a9934668SKenneth D. Merry 		if (maxmap == 0)
1419a9934668SKenneth D. Merry 			continue;
1420a9934668SKenneth D. Merry 
1421a9934668SKenneth D. Merry 		if (lengths[i] <= maxmap)
1422a9934668SKenneth D. Merry 			continue;
1423a9934668SKenneth D. Merry 
1424a9934668SKenneth D. Merry 		xpt_print(periph->path, "%s: data length %u > max allowed %u "
1425a9934668SKenneth D. Merry 			  "bytes\n", __func__, lengths[i], maxmap);
1426a9934668SKenneth D. Merry 		error = EINVAL;
1427a9934668SKenneth D. Merry 		goto bailout;
1428a9934668SKenneth D. Merry 	}
1429a9934668SKenneth D. Merry 
1430a9934668SKenneth D. Merry 	switch (io_req->data_flags) {
1431a9934668SKenneth D. Merry 	case CAM_DATA_VADDR:
1432a9934668SKenneth D. Merry 		/* Map or copy the buffer into kernel address space */
1433a9934668SKenneth D. Merry 		for (i = 0; i < numbufs; i++) {
1434a9934668SKenneth D. Merry 			uint8_t *tmp_buf;
1435a9934668SKenneth D. Merry 
1436a9934668SKenneth D. Merry 			/*
1437a9934668SKenneth D. Merry 			 * If for some reason no length is specified, we
1438a9934668SKenneth D. Merry 			 * don't need to allocate anything.
1439a9934668SKenneth D. Merry 			 */
1440a9934668SKenneth D. Merry 			if (io_req->lengths[i] == 0)
1441a9934668SKenneth D. Merry 				continue;
1442a9934668SKenneth D. Merry 
1443a9934668SKenneth D. Merry 			tmp_buf = malloc(lengths[i], M_SCSIPASS,
1444a9934668SKenneth D. Merry 					 M_WAITOK | M_ZERO);
1445a9934668SKenneth D. Merry 			io_req->kern_bufs[i] = tmp_buf;
1446a9934668SKenneth D. Merry 			*data_ptrs[i] = tmp_buf;
1447a9934668SKenneth D. Merry 
1448a9934668SKenneth D. Merry #if 0
1449a9934668SKenneth D. Merry 			xpt_print(periph->path, "%s: malloced %p len %u, user "
1450a9934668SKenneth D. Merry 				  "buffer %p, operation: %s\n", __func__,
1451a9934668SKenneth D. Merry 				  tmp_buf, lengths[i], io_req->user_bufs[i],
1452a9934668SKenneth D. Merry 				  (dirs[i] == CAM_DIR_IN) ? "read" : "write");
1453a9934668SKenneth D. Merry #endif
1454a9934668SKenneth D. Merry 			/*
1455a9934668SKenneth D. Merry 			 * We only need to copy in if the user is writing.
1456a9934668SKenneth D. Merry 			 */
1457a9934668SKenneth D. Merry 			if (dirs[i] != CAM_DIR_OUT)
1458a9934668SKenneth D. Merry 				continue;
1459a9934668SKenneth D. Merry 
1460a9934668SKenneth D. Merry 			error = copyin(io_req->user_bufs[i],
1461a9934668SKenneth D. Merry 				       io_req->kern_bufs[i], lengths[i]);
1462a9934668SKenneth D. Merry 			if (error != 0) {
1463a9934668SKenneth D. Merry 				xpt_print(periph->path, "%s: copy of user "
1464a9934668SKenneth D. Merry 					  "buffer from %p to %p failed with "
1465a9934668SKenneth D. Merry 					  "error %d\n", __func__,
1466a9934668SKenneth D. Merry 					  io_req->user_bufs[i],
1467a9934668SKenneth D. Merry 					  io_req->kern_bufs[i], error);
1468a9934668SKenneth D. Merry 				goto bailout;
1469a9934668SKenneth D. Merry 			}
1470a9934668SKenneth D. Merry 		}
1471a9934668SKenneth D. Merry 		break;
1472a9934668SKenneth D. Merry 	case CAM_DATA_PADDR:
1473a9934668SKenneth D. Merry 		/* Pass down the pointer as-is */
1474a9934668SKenneth D. Merry 		break;
1475a9934668SKenneth D. Merry 	case CAM_DATA_SG: {
1476a9934668SKenneth D. Merry 		size_t sg_length, size_to_go, alloc_size;
1477a9934668SKenneth D. Merry 		uint32_t num_segs_needed;
1478a9934668SKenneth D. Merry 
1479a9934668SKenneth D. Merry 		/*
1480a9934668SKenneth D. Merry 		 * Copy the user S/G list in, and then copy in the
1481a9934668SKenneth D. Merry 		 * individual segments.
1482a9934668SKenneth D. Merry 		 */
1483a9934668SKenneth D. Merry 		/*
1484a9934668SKenneth D. Merry 		 * We shouldn't see this, but check just in case.
1485a9934668SKenneth D. Merry 		 */
1486a9934668SKenneth D. Merry 		if (numbufs != 1) {
1487a9934668SKenneth D. Merry 			xpt_print(periph->path, "%s: cannot currently handle "
1488a9934668SKenneth D. Merry 				  "more than one S/G list per CCB\n", __func__);
1489a9934668SKenneth D. Merry 			error = EINVAL;
1490a9934668SKenneth D. Merry 			goto bailout;
1491a9934668SKenneth D. Merry 		}
1492a9934668SKenneth D. Merry 
1493a9934668SKenneth D. Merry 		/*
1494a9934668SKenneth D. Merry 		 * We have to have at least one segment.
1495a9934668SKenneth D. Merry 		 */
1496a9934668SKenneth D. Merry 		if (num_segs == 0) {
1497a9934668SKenneth D. Merry 			xpt_print(periph->path, "%s: CAM_DATA_SG flag set, "
1498a9934668SKenneth D. Merry 				  "but sglist_cnt=0!\n", __func__);
1499a9934668SKenneth D. Merry 			error = EINVAL;
1500a9934668SKenneth D. Merry 			goto bailout;
1501a9934668SKenneth D. Merry 		}
1502a9934668SKenneth D. Merry 
1503a9934668SKenneth D. Merry 		/*
1504a9934668SKenneth D. Merry 		 * Make sure the user specified the total length and didn't
1505a9934668SKenneth D. Merry 		 * just leave it to us to decode the S/G list.
1506a9934668SKenneth D. Merry 		 */
1507a9934668SKenneth D. Merry 		if (lengths[0] == 0) {
1508a9934668SKenneth D. Merry 			xpt_print(periph->path, "%s: no dxfer_len specified, "
1509a9934668SKenneth D. Merry 				  "but CAM_DATA_SG flag is set!\n", __func__);
1510a9934668SKenneth D. Merry 			error = EINVAL;
1511a9934668SKenneth D. Merry 			goto bailout;
1512a9934668SKenneth D. Merry 		}
1513a9934668SKenneth D. Merry 
1514a9934668SKenneth D. Merry 		/*
1515a9934668SKenneth D. Merry 		 * We allocate buffers in io_zone_size increments for an
1516cd853791SKonstantin Belousov 		 * S/G list.  This will generally be maxphys.
1517a9934668SKenneth D. Merry 		 */
1518a9934668SKenneth D. Merry 		if (lengths[0] <= softc->io_zone_size)
1519a9934668SKenneth D. Merry 			num_segs_needed = 1;
1520a9934668SKenneth D. Merry 		else {
1521a9934668SKenneth D. Merry 			num_segs_needed = lengths[0] / softc->io_zone_size;
1522a9934668SKenneth D. Merry 			if ((lengths[0] % softc->io_zone_size) != 0)
1523a9934668SKenneth D. Merry 				num_segs_needed++;
1524a9934668SKenneth D. Merry 		}
1525a9934668SKenneth D. Merry 
1526a9934668SKenneth D. Merry 		/* Figure out the size of the S/G list */
1527a9934668SKenneth D. Merry 		sg_length = num_segs * sizeof(bus_dma_segment_t);
1528a9934668SKenneth D. Merry 		io_req->num_user_segs = num_segs;
1529a9934668SKenneth D. Merry 		io_req->num_kern_segs = num_segs_needed;
1530a9934668SKenneth D. Merry 
1531a9934668SKenneth D. Merry 		/* Save the user's S/G list pointer for later restoration */
1532a9934668SKenneth D. Merry 		io_req->user_bufs[0] = *data_ptrs[0];
1533a9934668SKenneth D. Merry 
1534a9934668SKenneth D. Merry 		/*
1535a9934668SKenneth D. Merry 		 * If we have enough segments allocated by default to handle
1536a9934668SKenneth D. Merry 		 * the length of the user's S/G list,
1537a9934668SKenneth D. Merry 		 */
1538a9934668SKenneth D. Merry 		if (num_segs > PASS_MAX_SEGS) {
1539a9934668SKenneth D. Merry 			io_req->user_segptr = malloc(sizeof(bus_dma_segment_t) *
1540a9934668SKenneth D. Merry 			    num_segs, M_SCSIPASS, M_WAITOK | M_ZERO);
1541a9934668SKenneth D. Merry 			io_req->flags |= PASS_IO_USER_SEG_MALLOC;
1542a9934668SKenneth D. Merry 		} else
1543a9934668SKenneth D. Merry 			io_req->user_segptr = io_req->user_segs;
1544a9934668SKenneth D. Merry 
1545a9934668SKenneth D. Merry 		error = copyin(*data_ptrs[0], io_req->user_segptr, sg_length);
1546a9934668SKenneth D. Merry 		if (error != 0) {
1547a9934668SKenneth D. Merry 			xpt_print(periph->path, "%s: copy of user S/G list "
1548a9934668SKenneth D. Merry 				  "from %p to %p failed with error %d\n",
1549a9934668SKenneth D. Merry 				  __func__, *data_ptrs[0], io_req->user_segptr,
1550a9934668SKenneth D. Merry 				  error);
1551a9934668SKenneth D. Merry 			goto bailout;
1552a9934668SKenneth D. Merry 		}
1553a9934668SKenneth D. Merry 
1554a9934668SKenneth D. Merry 		if (num_segs_needed > PASS_MAX_SEGS) {
1555a9934668SKenneth D. Merry 			io_req->kern_segptr = malloc(sizeof(bus_dma_segment_t) *
1556a9934668SKenneth D. Merry 			    num_segs_needed, M_SCSIPASS, M_WAITOK | M_ZERO);
1557a9934668SKenneth D. Merry 			io_req->flags |= PASS_IO_KERN_SEG_MALLOC;
1558a9934668SKenneth D. Merry 		} else {
1559a9934668SKenneth D. Merry 			io_req->kern_segptr = io_req->kern_segs;
1560a9934668SKenneth D. Merry 		}
1561a9934668SKenneth D. Merry 
1562a9934668SKenneth D. Merry 		/*
1563a9934668SKenneth D. Merry 		 * Allocate the kernel S/G list.
1564a9934668SKenneth D. Merry 		 */
1565a9934668SKenneth D. Merry 		for (size_to_go = lengths[0], i = 0;
1566a9934668SKenneth D. Merry 		     size_to_go > 0 && i < num_segs_needed;
1567a9934668SKenneth D. Merry 		     i++, size_to_go -= alloc_size) {
1568a9934668SKenneth D. Merry 			uint8_t *kern_ptr;
1569a9934668SKenneth D. Merry 
1570a9934668SKenneth D. Merry 			alloc_size = min(size_to_go, softc->io_zone_size);
1571a9934668SKenneth D. Merry 			kern_ptr = uma_zalloc(softc->pass_io_zone, M_WAITOK);
1572a9934668SKenneth D. Merry 			io_req->kern_segptr[i].ds_addr =
1573a9934668SKenneth D. Merry 			    (bus_addr_t)(uintptr_t)kern_ptr;
1574a9934668SKenneth D. Merry 			io_req->kern_segptr[i].ds_len = alloc_size;
1575a9934668SKenneth D. Merry 		}
1576a9934668SKenneth D. Merry 		if (size_to_go > 0) {
1577a9934668SKenneth D. Merry 			printf("%s: size_to_go = %zu, software error!\n",
1578a9934668SKenneth D. Merry 			       __func__, size_to_go);
1579a9934668SKenneth D. Merry 			error = EINVAL;
1580a9934668SKenneth D. Merry 			goto bailout;
1581a9934668SKenneth D. Merry 		}
1582a9934668SKenneth D. Merry 
1583a9934668SKenneth D. Merry 		*data_ptrs[0] = (uint8_t *)io_req->kern_segptr;
1584a9934668SKenneth D. Merry 		*seg_cnt_ptr = io_req->num_kern_segs;
1585a9934668SKenneth D. Merry 
1586a9934668SKenneth D. Merry 		/*
1587a9934668SKenneth D. Merry 		 * We only need to copy data here if the user is writing.
1588a9934668SKenneth D. Merry 		 */
1589a9934668SKenneth D. Merry 		if (dirs[0] == CAM_DIR_OUT)
1590a9934668SKenneth D. Merry 			error = passcopysglist(periph, io_req, dirs[0]);
1591a9934668SKenneth D. Merry 		break;
1592a9934668SKenneth D. Merry 	}
1593a9934668SKenneth D. Merry 	case CAM_DATA_SG_PADDR: {
1594a9934668SKenneth D. Merry 		size_t sg_length;
1595a9934668SKenneth D. Merry 
1596a9934668SKenneth D. Merry 		/*
1597a9934668SKenneth D. Merry 		 * We shouldn't see this, but check just in case.
1598a9934668SKenneth D. Merry 		 */
1599a9934668SKenneth D. Merry 		if (numbufs != 1) {
1600a9934668SKenneth D. Merry 			printf("%s: cannot currently handle more than one "
1601a9934668SKenneth D. Merry 			       "S/G list per CCB\n", __func__);
1602a9934668SKenneth D. Merry 			error = EINVAL;
1603a9934668SKenneth D. Merry 			goto bailout;
1604a9934668SKenneth D. Merry 		}
1605a9934668SKenneth D. Merry 
1606a9934668SKenneth D. Merry 		/*
1607a9934668SKenneth D. Merry 		 * We have to have at least one segment.
1608a9934668SKenneth D. Merry 		 */
1609a9934668SKenneth D. Merry 		if (num_segs == 0) {
1610a9934668SKenneth D. Merry 			xpt_print(periph->path, "%s: CAM_DATA_SG_PADDR flag "
1611a9934668SKenneth D. Merry 				  "set, but sglist_cnt=0!\n", __func__);
1612a9934668SKenneth D. Merry 			error = EINVAL;
1613a9934668SKenneth D. Merry 			goto bailout;
1614a9934668SKenneth D. Merry 		}
1615a9934668SKenneth D. Merry 
1616a9934668SKenneth D. Merry 		/*
1617a9934668SKenneth D. Merry 		 * Make sure the user specified the total length and didn't
1618a9934668SKenneth D. Merry 		 * just leave it to us to decode the S/G list.
1619a9934668SKenneth D. Merry 		 */
1620a9934668SKenneth D. Merry 		if (lengths[0] == 0) {
1621a9934668SKenneth D. Merry 			xpt_print(periph->path, "%s: no dxfer_len specified, "
1622a9934668SKenneth D. Merry 				  "but CAM_DATA_SG flag is set!\n", __func__);
1623a9934668SKenneth D. Merry 			error = EINVAL;
1624a9934668SKenneth D. Merry 			goto bailout;
1625a9934668SKenneth D. Merry 		}
1626a9934668SKenneth D. Merry 
1627a9934668SKenneth D. Merry 		/* Figure out the size of the S/G list */
1628a9934668SKenneth D. Merry 		sg_length = num_segs * sizeof(bus_dma_segment_t);
1629a9934668SKenneth D. Merry 		io_req->num_user_segs = num_segs;
1630a9934668SKenneth D. Merry 		io_req->num_kern_segs = io_req->num_user_segs;
1631a9934668SKenneth D. Merry 
1632a9934668SKenneth D. Merry 		/* Save the user's S/G list pointer for later restoration */
1633a9934668SKenneth D. Merry 		io_req->user_bufs[0] = *data_ptrs[0];
1634a9934668SKenneth D. Merry 
1635a9934668SKenneth D. Merry 		if (num_segs > PASS_MAX_SEGS) {
1636a9934668SKenneth D. Merry 			io_req->user_segptr = malloc(sizeof(bus_dma_segment_t) *
1637a9934668SKenneth D. Merry 			    num_segs, M_SCSIPASS, M_WAITOK | M_ZERO);
1638a9934668SKenneth D. Merry 			io_req->flags |= PASS_IO_USER_SEG_MALLOC;
1639a9934668SKenneth D. Merry 		} else
1640a9934668SKenneth D. Merry 			io_req->user_segptr = io_req->user_segs;
1641a9934668SKenneth D. Merry 
1642a9934668SKenneth D. Merry 		io_req->kern_segptr = io_req->user_segptr;
1643a9934668SKenneth D. Merry 
1644a9934668SKenneth D. Merry 		error = copyin(*data_ptrs[0], io_req->user_segptr, sg_length);
1645a9934668SKenneth D. Merry 		if (error != 0) {
1646a9934668SKenneth D. Merry 			xpt_print(periph->path, "%s: copy of user S/G list "
1647a9934668SKenneth D. Merry 				  "from %p to %p failed with error %d\n",
1648a9934668SKenneth D. Merry 				  __func__, *data_ptrs[0], io_req->user_segptr,
1649a9934668SKenneth D. Merry 				  error);
1650a9934668SKenneth D. Merry 			goto bailout;
1651a9934668SKenneth D. Merry 		}
1652a9934668SKenneth D. Merry 		break;
1653a9934668SKenneth D. Merry 	}
1654a9934668SKenneth D. Merry 	default:
1655a9934668SKenneth D. Merry 	case CAM_DATA_BIO:
1656a9934668SKenneth D. Merry 		/*
1657a9934668SKenneth D. Merry 		 * A user shouldn't be attaching a bio to the CCB.  It
1658a9934668SKenneth D. Merry 		 * isn't a user-accessible structure.
1659a9934668SKenneth D. Merry 		 */
1660a9934668SKenneth D. Merry 		error = EINVAL;
1661a9934668SKenneth D. Merry 		break;
1662a9934668SKenneth D. Merry 	}
1663a9934668SKenneth D. Merry 
1664a9934668SKenneth D. Merry bailout:
1665a9934668SKenneth D. Merry 	if (error != 0)
1666a9934668SKenneth D. Merry 		passiocleanup(softc, io_req);
1667a9934668SKenneth D. Merry 
1668a9934668SKenneth D. Merry 	return (error);
1669a9934668SKenneth D. Merry }
1670a9934668SKenneth D. Merry 
1671a9934668SKenneth D. Merry static int
passmemdone(struct cam_periph * periph,struct pass_io_req * io_req)1672a9934668SKenneth D. Merry passmemdone(struct cam_periph *periph, struct pass_io_req *io_req)
1673a9934668SKenneth D. Merry {
1674a9934668SKenneth D. Merry 	struct pass_softc *softc;
1675a9934668SKenneth D. Merry 	int error;
1676a9934668SKenneth D. Merry 	int i;
1677a9934668SKenneth D. Merry 
1678a9934668SKenneth D. Merry 	error = 0;
1679a9934668SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
1680a9934668SKenneth D. Merry 
1681a9934668SKenneth D. Merry 	switch (io_req->data_flags) {
1682a9934668SKenneth D. Merry 	case CAM_DATA_VADDR:
1683a9934668SKenneth D. Merry 		/*
1684a9934668SKenneth D. Merry 		 * Copy back to the user buffer if this was a read.
1685a9934668SKenneth D. Merry 		 */
1686a9934668SKenneth D. Merry 		for (i = 0; i < io_req->num_bufs; i++) {
1687a9934668SKenneth D. Merry 			if (io_req->dirs[i] != CAM_DIR_IN)
1688a9934668SKenneth D. Merry 				continue;
1689a9934668SKenneth D. Merry 
1690a9934668SKenneth D. Merry 			error = copyout(io_req->kern_bufs[i],
1691a9934668SKenneth D. Merry 			    io_req->user_bufs[i], io_req->lengths[i]);
1692a9934668SKenneth D. Merry 			if (error != 0) {
1693a9934668SKenneth D. Merry 				xpt_print(periph->path, "Unable to copy %u "
1694a9934668SKenneth D. Merry 					  "bytes from %p to user address %p\n",
1695a9934668SKenneth D. Merry 					  io_req->lengths[i],
1696a9934668SKenneth D. Merry 					  io_req->kern_bufs[i],
1697a9934668SKenneth D. Merry 					  io_req->user_bufs[i]);
1698a9934668SKenneth D. Merry 				goto bailout;
1699a9934668SKenneth D. Merry 			}
1700a9934668SKenneth D. Merry 		}
1701a9934668SKenneth D. Merry 		break;
1702a9934668SKenneth D. Merry 	case CAM_DATA_PADDR:
1703a9934668SKenneth D. Merry 		/* Do nothing.  The pointer is a physical address already */
1704a9934668SKenneth D. Merry 		break;
1705a9934668SKenneth D. Merry 	case CAM_DATA_SG:
1706a9934668SKenneth D. Merry 		/*
1707a9934668SKenneth D. Merry 		 * Copy back to the user buffer if this was a read.
1708a9934668SKenneth D. Merry 		 * Restore the user's S/G list buffer pointer.
1709a9934668SKenneth D. Merry 		 */
1710a9934668SKenneth D. Merry 		if (io_req->dirs[0] == CAM_DIR_IN)
1711a9934668SKenneth D. Merry 			error = passcopysglist(periph, io_req, io_req->dirs[0]);
1712a9934668SKenneth D. Merry 		break;
1713a9934668SKenneth D. Merry 	case CAM_DATA_SG_PADDR:
1714a9934668SKenneth D. Merry 		/*
1715a9934668SKenneth D. Merry 		 * Restore the user's S/G list buffer pointer.  No need to
1716a9934668SKenneth D. Merry 		 * copy.
1717a9934668SKenneth D. Merry 		 */
1718a9934668SKenneth D. Merry 		break;
1719a9934668SKenneth D. Merry 	default:
1720a9934668SKenneth D. Merry 	case CAM_DATA_BIO:
1721a9934668SKenneth D. Merry 		error = EINVAL;
1722a9934668SKenneth D. Merry 		break;
1723a9934668SKenneth D. Merry 	}
1724a9934668SKenneth D. Merry 
1725a9934668SKenneth D. Merry bailout:
1726a9934668SKenneth D. Merry 	/*
1727a9934668SKenneth D. Merry 	 * Reset the user's pointers to their original values and free
1728a9934668SKenneth D. Merry 	 * allocated memory.
1729a9934668SKenneth D. Merry 	 */
1730a9934668SKenneth D. Merry 	passiocleanup(softc, io_req);
1731a9934668SKenneth D. Merry 
1732a9934668SKenneth D. Merry 	return (error);
1733a9934668SKenneth D. Merry }
1734a9934668SKenneth D. Merry 
173576babe50SJustin T. Gibbs static int
passioctl(struct cdev * dev,u_long cmd,caddr_t addr,int flag,struct thread * td)173689c9c53dSPoul-Henning Kamp passioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
173776babe50SJustin T. Gibbs {
173825a2902cSScott Long 	int error;
173925a2902cSScott Long 
174025a2902cSScott Long 	if ((error = passdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
1741f564de00SScott Long 		error = cam_compat_ioctl(dev, cmd, addr, flag, td, passdoioctl);
174225a2902cSScott Long 	}
174325a2902cSScott Long 	return (error);
174425a2902cSScott Long }
174525a2902cSScott Long 
174625a2902cSScott Long static int
passdoioctl(struct cdev * dev,u_long cmd,caddr_t addr,int flag,struct thread * td)174725a2902cSScott Long passdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
174825a2902cSScott Long {
174976babe50SJustin T. Gibbs 	struct	cam_periph *periph;
1750a9934668SKenneth D. Merry 	struct	pass_softc *softc;
175176babe50SJustin T. Gibbs 	int	error;
17528cff7eb8SAlexander Motin 	uint32_t priority;
175376babe50SJustin T. Gibbs 
1754e2a5fdf9SNate Lawson 	periph = (struct cam_periph *)dev->si_drv1;
17552b83592fSScott Long 	cam_periph_lock(periph);
1756a9934668SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
175776babe50SJustin T. Gibbs 
175876babe50SJustin T. Gibbs 	error = 0;
175976babe50SJustin T. Gibbs 
176076babe50SJustin T. Gibbs 	switch (cmd) {
176176babe50SJustin T. Gibbs 	case CAMIOCOMMAND:
176276babe50SJustin T. Gibbs 	{
176376babe50SJustin T. Gibbs 		union ccb *inccb;
176476babe50SJustin T. Gibbs 		union ccb *ccb;
17659deea857SKenneth D. Merry 		int ccb_malloced;
176676babe50SJustin T. Gibbs 
176776babe50SJustin T. Gibbs 		inccb = (union ccb *)addr;
17688fc77fffSConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
17698fc77fffSConrad Meyer 		if (inccb->ccb_h.func_code == XPT_SCSI_IO)
17708fc77fffSConrad Meyer 			inccb->csio.bio = NULL;
17718fc77fffSConrad Meyer #endif
17729deea857SKenneth D. Merry 
1773a0bbf9e0SMark Johnston 		if (inccb->ccb_h.flags & CAM_UNLOCKED) {
1774a0bbf9e0SMark Johnston 			error = EINVAL;
1775a0bbf9e0SMark Johnston 			break;
1776a0bbf9e0SMark Johnston 		}
1777a0bbf9e0SMark Johnston 
17789deea857SKenneth D. Merry 		/*
17799deea857SKenneth D. Merry 		 * Some CCB types, like scan bus and scan lun can only go
17809deea857SKenneth D. Merry 		 * through the transport layer device.
17819deea857SKenneth D. Merry 		 */
17829deea857SKenneth D. Merry 		if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
1783f0d9af51SMatt Jacob 			xpt_print(periph->path, "CCB function code %#x is "
1784f0d9af51SMatt Jacob 			    "restricted to the XPT device\n",
1785f0d9af51SMatt Jacob 			    inccb->ccb_h.func_code);
17869deea857SKenneth D. Merry 			error = ENODEV;
17879deea857SKenneth D. Merry 			break;
17889deea857SKenneth D. Merry 		}
17899deea857SKenneth D. Merry 
17908cff7eb8SAlexander Motin 		/* Compatibility for RL/priority-unaware code. */
17918cff7eb8SAlexander Motin 		priority = inccb->ccb_h.pinfo.priority;
1792cccf4220SAlexander Motin 		if (priority <= CAM_PRIORITY_OOB)
1793cccf4220SAlexander Motin 		    priority += CAM_PRIORITY_OOB + 1;
17948cff7eb8SAlexander Motin 
17959deea857SKenneth D. Merry 		/*
17969deea857SKenneth D. Merry 		 * Non-immediate CCBs need a CCB from the per-device pool
17979deea857SKenneth D. Merry 		 * of CCBs, which is scheduled by the transport layer.
17989deea857SKenneth D. Merry 		 * Immediate CCBs and user-supplied CCBs should just be
17999deea857SKenneth D. Merry 		 * malloced.
18009deea857SKenneth D. Merry 		 */
18019deea857SKenneth D. Merry 		if ((inccb->ccb_h.func_code & XPT_FC_QUEUED)
18029deea857SKenneth D. Merry 		 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) {
18038cff7eb8SAlexander Motin 			ccb = cam_periph_getccb(periph, priority);
18049deea857SKenneth D. Merry 			ccb_malloced = 0;
18059deea857SKenneth D. Merry 		} else {
18068008a935SScott Long 			ccb = xpt_alloc_ccb_nowait();
18079deea857SKenneth D. Merry 
18089deea857SKenneth D. Merry 			if (ccb != NULL)
18099deea857SKenneth D. Merry 				xpt_setup_ccb(&ccb->ccb_h, periph->path,
18108cff7eb8SAlexander Motin 					      priority);
18119deea857SKenneth D. Merry 			ccb_malloced = 1;
18129deea857SKenneth D. Merry 		}
18139deea857SKenneth D. Merry 
18149deea857SKenneth D. Merry 		if (ccb == NULL) {
1815f0d9af51SMatt Jacob 			xpt_print(periph->path, "unable to allocate CCB\n");
18169deea857SKenneth D. Merry 			error = ENOMEM;
18179deea857SKenneth D. Merry 			break;
18189deea857SKenneth D. Merry 		}
181976babe50SJustin T. Gibbs 
182076babe50SJustin T. Gibbs 		error = passsendccb(periph, ccb, inccb);
182176babe50SJustin T. Gibbs 
18229deea857SKenneth D. Merry 		if (ccb_malloced)
18239deea857SKenneth D. Merry 			xpt_free_ccb(ccb);
18249deea857SKenneth D. Merry 		else
182576babe50SJustin T. Gibbs 			xpt_release_ccb(ccb);
182676babe50SJustin T. Gibbs 
182776babe50SJustin T. Gibbs 		break;
182876babe50SJustin T. Gibbs 	}
1829a9934668SKenneth D. Merry 	case CAMIOQUEUE:
1830a9934668SKenneth D. Merry 	{
1831a9934668SKenneth D. Merry 		struct pass_io_req *io_req;
1832a9934668SKenneth D. Merry 		union ccb **user_ccb, *ccb;
1833a9934668SKenneth D. Merry 		xpt_opcode fc;
1834a9934668SKenneth D. Merry 
1835871dc983SBrooks Davis #ifdef COMPAT_FREEBSD32
1836871dc983SBrooks Davis 		if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
1837871dc983SBrooks Davis 			error = ENOTTY;
1838871dc983SBrooks Davis 			goto bailout;
1839871dc983SBrooks Davis 		}
1840871dc983SBrooks Davis #endif
1841a9934668SKenneth D. Merry 		if ((softc->flags & PASS_FLAG_ZONE_VALID) == 0) {
1842a9934668SKenneth D. Merry 			error = passcreatezone(periph);
1843a9934668SKenneth D. Merry 			if (error != 0)
1844a9934668SKenneth D. Merry 				goto bailout;
1845a9934668SKenneth D. Merry 		}
1846a9934668SKenneth D. Merry 
1847a9934668SKenneth D. Merry 		/*
1848a9934668SKenneth D. Merry 		 * We're going to do a blocking allocation for this I/O
1849a9934668SKenneth D. Merry 		 * request, so we have to drop the lock.
1850a9934668SKenneth D. Merry 		 */
1851a9934668SKenneth D. Merry 		cam_periph_unlock(periph);
1852a9934668SKenneth D. Merry 
1853a9934668SKenneth D. Merry 		io_req = uma_zalloc(softc->pass_zone, M_WAITOK | M_ZERO);
1854a9934668SKenneth D. Merry 		ccb = &io_req->ccb;
1855a9934668SKenneth D. Merry 		user_ccb = (union ccb **)addr;
1856a9934668SKenneth D. Merry 
1857a9934668SKenneth D. Merry 		/*
1858a9934668SKenneth D. Merry 		 * Unlike the CAMIOCOMMAND ioctl above, we only have a
1859a9934668SKenneth D. Merry 		 * pointer to the user's CCB, so we have to copy the whole
1860a9934668SKenneth D. Merry 		 * thing in to a buffer we have allocated (above) instead
1861a9934668SKenneth D. Merry 		 * of allowing the ioctl code to malloc a buffer and copy
1862a9934668SKenneth D. Merry 		 * it in.
1863a9934668SKenneth D. Merry 		 *
1864a9934668SKenneth D. Merry 		 * This is an advantage for this asynchronous interface,
1865a9934668SKenneth D. Merry 		 * since we don't want the memory to get freed while the
1866a9934668SKenneth D. Merry 		 * CCB is outstanding.
1867a9934668SKenneth D. Merry 		 */
1868a9934668SKenneth D. Merry #if 0
1869a9934668SKenneth D. Merry 		xpt_print(periph->path, "Copying user CCB %p to "
1870a9934668SKenneth D. Merry 			  "kernel address %p\n", *user_ccb, ccb);
1871a9934668SKenneth D. Merry #endif
1872a9934668SKenneth D. Merry 		error = copyin(*user_ccb, ccb, sizeof(*ccb));
1873a9934668SKenneth D. Merry 		if (error != 0) {
1874a9934668SKenneth D. Merry 			xpt_print(periph->path, "Copy of user CCB %p to "
1875a9934668SKenneth D. Merry 				  "kernel address %p failed with error %d\n",
1876a9934668SKenneth D. Merry 				  *user_ccb, ccb, error);
1877a0bbf9e0SMark Johnston 			goto camioqueue_error;
1878a9934668SKenneth D. Merry 		}
18798fc77fffSConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
18808fc77fffSConrad Meyer 		if (ccb->ccb_h.func_code == XPT_SCSI_IO)
18818fc77fffSConrad Meyer 			ccb->csio.bio = NULL;
18828fc77fffSConrad Meyer #endif
1883a9934668SKenneth D. Merry 
1884a0bbf9e0SMark Johnston 		if (ccb->ccb_h.flags & CAM_UNLOCKED) {
1885a0bbf9e0SMark Johnston 			error = EINVAL;
1886a0bbf9e0SMark Johnston 			goto camioqueue_error;
1887a0bbf9e0SMark Johnston 		}
1888a0bbf9e0SMark Johnston 
1889a1a604caSAlexander Motin 		if (ccb->ccb_h.flags & CAM_CDB_POINTER) {
1890a1a604caSAlexander Motin 			if (ccb->csio.cdb_len > IOCDBLEN) {
1891a1a604caSAlexander Motin 				error = EINVAL;
1892a0bbf9e0SMark Johnston 				goto camioqueue_error;
1893a1a604caSAlexander Motin 			}
1894a1a604caSAlexander Motin 			error = copyin(ccb->csio.cdb_io.cdb_ptr,
1895a1a604caSAlexander Motin 			    ccb->csio.cdb_io.cdb_bytes, ccb->csio.cdb_len);
1896a0bbf9e0SMark Johnston 			if (error != 0)
1897a0bbf9e0SMark Johnston 				goto camioqueue_error;
1898a1a604caSAlexander Motin 			ccb->ccb_h.flags &= ~CAM_CDB_POINTER;
1899a1a604caSAlexander Motin 		}
1900a1a604caSAlexander Motin 
1901a9934668SKenneth D. Merry 		/*
1902a9934668SKenneth D. Merry 		 * Some CCB types, like scan bus and scan lun can only go
1903a9934668SKenneth D. Merry 		 * through the transport layer device.
1904a9934668SKenneth D. Merry 		 */
1905a9934668SKenneth D. Merry 		if (ccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
1906a9934668SKenneth D. Merry 			xpt_print(periph->path, "CCB function code %#x is "
1907a9934668SKenneth D. Merry 			    "restricted to the XPT device\n",
1908a9934668SKenneth D. Merry 			    ccb->ccb_h.func_code);
1909a9934668SKenneth D. Merry 			error = ENODEV;
1910a0bbf9e0SMark Johnston 			goto camioqueue_error;
1911a9934668SKenneth D. Merry 		}
1912a9934668SKenneth D. Merry 
1913a9934668SKenneth D. Merry 		/*
1914a9934668SKenneth D. Merry 		 * Save the user's CCB pointer as well as his linked list
1915a9934668SKenneth D. Merry 		 * pointers and peripheral private area so that we can
1916a9934668SKenneth D. Merry 		 * restore these later.
1917a9934668SKenneth D. Merry 		 */
1918a9934668SKenneth D. Merry 		io_req->user_ccb_ptr = *user_ccb;
1919a9934668SKenneth D. Merry 		io_req->user_periph_links = ccb->ccb_h.periph_links;
1920a9934668SKenneth D. Merry 		io_req->user_periph_priv = ccb->ccb_h.periph_priv;
1921a9934668SKenneth D. Merry 
1922a9934668SKenneth D. Merry 		/*
1923a9934668SKenneth D. Merry 		 * Now that we've saved the user's values, we can set our
1924a9934668SKenneth D. Merry 		 * own peripheral private entry.
1925a9934668SKenneth D. Merry 		 */
1926a9934668SKenneth D. Merry 		ccb->ccb_h.ccb_ioreq = io_req;
1927a9934668SKenneth D. Merry 
1928a9934668SKenneth D. Merry 		/* Compatibility for RL/priority-unaware code. */
1929a9934668SKenneth D. Merry 		priority = ccb->ccb_h.pinfo.priority;
1930a9934668SKenneth D. Merry 		if (priority <= CAM_PRIORITY_OOB)
1931a9934668SKenneth D. Merry 		    priority += CAM_PRIORITY_OOB + 1;
1932a9934668SKenneth D. Merry 
1933a9934668SKenneth D. Merry 		/*
1934a9934668SKenneth D. Merry 		 * Setup fields in the CCB like the path and the priority.
1935a9934668SKenneth D. Merry 		 * The path in particular cannot be done in userland, since
1936a9934668SKenneth D. Merry 		 * it is a pointer to a kernel data structure.
1937a9934668SKenneth D. Merry 		 */
1938a9934668SKenneth D. Merry 		xpt_setup_ccb_flags(&ccb->ccb_h, periph->path, priority,
1939a9934668SKenneth D. Merry 				    ccb->ccb_h.flags);
1940a9934668SKenneth D. Merry 
1941a9934668SKenneth D. Merry 		/*
1942a9934668SKenneth D. Merry 		 * Setup our done routine.  There is no way for the user to
1943a9934668SKenneth D. Merry 		 * have a valid pointer here.
1944a9934668SKenneth D. Merry 		 */
1945a9934668SKenneth D. Merry 		ccb->ccb_h.cbfcnp = passdone;
1946a9934668SKenneth D. Merry 
1947a9934668SKenneth D. Merry 		fc = ccb->ccb_h.func_code;
1948a9934668SKenneth D. Merry 		/*
1949a9934668SKenneth D. Merry 		 * If this function code has memory that can be mapped in
1950a9934668SKenneth D. Merry 		 * or out, we need to call passmemsetup().
1951a9934668SKenneth D. Merry 		 */
1952a9934668SKenneth D. Merry 		if ((fc == XPT_SCSI_IO) || (fc == XPT_ATA_IO)
1953a9934668SKenneth D. Merry 		 || (fc == XPT_SMP_IO) || (fc == XPT_DEV_MATCH)
1954df424515SWarner Losh 		 || (fc == XPT_DEV_ADVINFO)
1955df424515SWarner Losh 		 || (fc == XPT_NVME_ADMIN) || (fc == XPT_NVME_IO)) {
1956a9934668SKenneth D. Merry 			error = passmemsetup(periph, io_req);
1957a0bbf9e0SMark Johnston 			if (error != 0)
1958a0bbf9e0SMark Johnston 				goto camioqueue_error;
1959a9934668SKenneth D. Merry 		} else
1960a9934668SKenneth D. Merry 			io_req->mapinfo.num_bufs_used = 0;
1961a9934668SKenneth D. Merry 
1962a9934668SKenneth D. Merry 		cam_periph_lock(periph);
1963a9934668SKenneth D. Merry 
1964a9934668SKenneth D. Merry 		/*
1965a9934668SKenneth D. Merry 		 * Everything goes on the incoming queue initially.
1966a9934668SKenneth D. Merry 		 */
1967a9934668SKenneth D. Merry 		TAILQ_INSERT_TAIL(&softc->incoming_queue, io_req, links);
1968a9934668SKenneth D. Merry 
1969a9934668SKenneth D. Merry 		/*
1970a9934668SKenneth D. Merry 		 * If the CCB is queued, and is not a user CCB, then
1971a9934668SKenneth D. Merry 		 * we need to allocate a slot for it.  Call xpt_schedule()
1972a9934668SKenneth D. Merry 		 * so that our start routine will get called when a CCB is
1973a9934668SKenneth D. Merry 		 * available.
1974a9934668SKenneth D. Merry 		 */
1975a9934668SKenneth D. Merry 		if ((fc & XPT_FC_QUEUED)
1976a9934668SKenneth D. Merry 		 && ((fc & XPT_FC_USER_CCB) == 0)) {
1977a9934668SKenneth D. Merry 			xpt_schedule(periph, priority);
1978a9934668SKenneth D. Merry 			break;
1979a9934668SKenneth D. Merry 		}
1980a9934668SKenneth D. Merry 
1981a9934668SKenneth D. Merry 		/*
1982a9934668SKenneth D. Merry 		 * At this point, the CCB in question is either an
1983a9934668SKenneth D. Merry 		 * immediate CCB (like XPT_DEV_ADVINFO) or it is a user CCB
1984a9934668SKenneth D. Merry 		 * and therefore should be malloced, not allocated via a slot.
1985a9934668SKenneth D. Merry 		 * Remove the CCB from the incoming queue and add it to the
1986a9934668SKenneth D. Merry 		 * active queue.
1987a9934668SKenneth D. Merry 		 */
1988a9934668SKenneth D. Merry 		TAILQ_REMOVE(&softc->incoming_queue, io_req, links);
1989a9934668SKenneth D. Merry 		TAILQ_INSERT_TAIL(&softc->active_queue, io_req, links);
1990a9934668SKenneth D. Merry 
1991a9934668SKenneth D. Merry 		xpt_action(ccb);
1992a9934668SKenneth D. Merry 
1993a9934668SKenneth D. Merry 		/*
1994a9934668SKenneth D. Merry 		 * If this is not a queued CCB (i.e. it is an immediate CCB),
1995a9934668SKenneth D. Merry 		 * then it is already done.  We need to put it on the done
1996a9934668SKenneth D. Merry 		 * queue for the user to fetch.
1997a9934668SKenneth D. Merry 		 */
1998a9934668SKenneth D. Merry 		if ((fc & XPT_FC_QUEUED) == 0) {
1999a9934668SKenneth D. Merry 			TAILQ_REMOVE(&softc->active_queue, io_req, links);
2000a9934668SKenneth D. Merry 			TAILQ_INSERT_TAIL(&softc->done_queue, io_req, links);
2001a9934668SKenneth D. Merry 		}
2002a9934668SKenneth D. Merry 		break;
2003a0bbf9e0SMark Johnston 
2004a0bbf9e0SMark Johnston camioqueue_error:
2005a0bbf9e0SMark Johnston 		uma_zfree(softc->pass_zone, io_req);
2006a0bbf9e0SMark Johnston 		cam_periph_lock(periph);
2007a0bbf9e0SMark Johnston 		break;
2008a9934668SKenneth D. Merry 	}
2009a9934668SKenneth D. Merry 	case CAMIOGET:
2010a9934668SKenneth D. Merry 	{
2011a9934668SKenneth D. Merry 		union ccb **user_ccb;
2012a9934668SKenneth D. Merry 		struct pass_io_req *io_req;
2013a9934668SKenneth D. Merry 		int old_error;
2014a9934668SKenneth D. Merry 
2015871dc983SBrooks Davis #ifdef COMPAT_FREEBSD32
2016871dc983SBrooks Davis 		if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
2017871dc983SBrooks Davis 			error = ENOTTY;
2018871dc983SBrooks Davis 			goto bailout;
2019871dc983SBrooks Davis 		}
2020871dc983SBrooks Davis #endif
2021a9934668SKenneth D. Merry 		user_ccb = (union ccb **)addr;
2022a9934668SKenneth D. Merry 		old_error = 0;
2023a9934668SKenneth D. Merry 
2024a9934668SKenneth D. Merry 		io_req = TAILQ_FIRST(&softc->done_queue);
2025a9934668SKenneth D. Merry 		if (io_req == NULL) {
2026a9934668SKenneth D. Merry 			error = ENOENT;
2027a9934668SKenneth D. Merry 			break;
2028a9934668SKenneth D. Merry 		}
2029a9934668SKenneth D. Merry 
2030a9934668SKenneth D. Merry 		/*
2031a9934668SKenneth D. Merry 		 * Remove the I/O from the done queue.
2032a9934668SKenneth D. Merry 		 */
2033a9934668SKenneth D. Merry 		TAILQ_REMOVE(&softc->done_queue, io_req, links);
2034a9934668SKenneth D. Merry 
2035a9934668SKenneth D. Merry 		/*
2036a9934668SKenneth D. Merry 		 * We have to drop the lock during the copyout because the
2037a9934668SKenneth D. Merry 		 * copyout can result in VM faults that require sleeping.
2038a9934668SKenneth D. Merry 		 */
2039a9934668SKenneth D. Merry 		cam_periph_unlock(periph);
2040a9934668SKenneth D. Merry 
2041a9934668SKenneth D. Merry 		/*
2042a9934668SKenneth D. Merry 		 * Do any needed copies (e.g. for reads) and revert the
2043a9934668SKenneth D. Merry 		 * pointers in the CCB back to the user's pointers.
2044a9934668SKenneth D. Merry 		 */
2045a9934668SKenneth D. Merry 		error = passmemdone(periph, io_req);
2046a9934668SKenneth D. Merry 
2047a9934668SKenneth D. Merry 		old_error = error;
2048a9934668SKenneth D. Merry 
2049a9934668SKenneth D. Merry 		io_req->ccb.ccb_h.periph_links = io_req->user_periph_links;
2050a9934668SKenneth D. Merry 		io_req->ccb.ccb_h.periph_priv = io_req->user_periph_priv;
2051a9934668SKenneth D. Merry 
2052a9934668SKenneth D. Merry #if 0
2053a9934668SKenneth D. Merry 		xpt_print(periph->path, "Copying to user CCB %p from "
2054a9934668SKenneth D. Merry 			  "kernel address %p\n", *user_ccb, &io_req->ccb);
2055a9934668SKenneth D. Merry #endif
2056a9934668SKenneth D. Merry 
2057a9934668SKenneth D. Merry 		error = copyout(&io_req->ccb, *user_ccb, sizeof(union ccb));
2058a9934668SKenneth D. Merry 		if (error != 0) {
2059a9934668SKenneth D. Merry 			xpt_print(periph->path, "Copy to user CCB %p from "
2060a9934668SKenneth D. Merry 				  "kernel address %p failed with error %d\n",
2061a9934668SKenneth D. Merry 				  *user_ccb, &io_req->ccb, error);
2062a9934668SKenneth D. Merry 		}
2063a9934668SKenneth D. Merry 
2064a9934668SKenneth D. Merry 		/*
2065a9934668SKenneth D. Merry 		 * Prefer the first error we got back, and make sure we
2066a9934668SKenneth D. Merry 		 * don't overwrite bad status with good.
2067a9934668SKenneth D. Merry 		 */
2068a9934668SKenneth D. Merry 		if (old_error != 0)
2069a9934668SKenneth D. Merry 			error = old_error;
2070a9934668SKenneth D. Merry 
2071a9934668SKenneth D. Merry 		cam_periph_lock(periph);
2072a9934668SKenneth D. Merry 
2073a9934668SKenneth D. Merry 		/*
2074a9934668SKenneth D. Merry 		 * At this point, if there was an error, we could potentially
2075a9934668SKenneth D. Merry 		 * re-queue the I/O and try again.  But why?  The error
2076a9934668SKenneth D. Merry 		 * would almost certainly happen again.  We might as well
2077a9934668SKenneth D. Merry 		 * not leak memory.
2078a9934668SKenneth D. Merry 		 */
2079a9934668SKenneth D. Merry 		uma_zfree(softc->pass_zone, io_req);
2080a9934668SKenneth D. Merry 		break;
2081a9934668SKenneth D. Merry 	}
208276babe50SJustin T. Gibbs 	default:
208376babe50SJustin T. Gibbs 		error = cam_periph_ioctl(periph, cmd, addr, passerror);
208476babe50SJustin T. Gibbs 		break;
208576babe50SJustin T. Gibbs 	}
208676babe50SJustin T. Gibbs 
2087a9934668SKenneth D. Merry bailout:
20882b83592fSScott Long 	cam_periph_unlock(periph);
2089a9934668SKenneth D. Merry 
209076babe50SJustin T. Gibbs 	return(error);
209176babe50SJustin T. Gibbs }
209276babe50SJustin T. Gibbs 
2093a9934668SKenneth D. Merry static int
passpoll(struct cdev * dev,int poll_events,struct thread * td)2094a9934668SKenneth D. Merry passpoll(struct cdev *dev, int poll_events, struct thread *td)
2095a9934668SKenneth D. Merry {
2096a9934668SKenneth D. Merry 	struct cam_periph *periph;
2097a9934668SKenneth D. Merry 	struct pass_softc *softc;
2098a9934668SKenneth D. Merry 	int revents;
2099a9934668SKenneth D. Merry 
2100a9934668SKenneth D. Merry 	periph = (struct cam_periph *)dev->si_drv1;
2101a9934668SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
2102a9934668SKenneth D. Merry 
2103a9934668SKenneth D. Merry 	revents = poll_events & (POLLOUT | POLLWRNORM);
2104a9934668SKenneth D. Merry 	if ((poll_events & (POLLIN | POLLRDNORM)) != 0) {
2105a9934668SKenneth D. Merry 		cam_periph_lock(periph);
2106a9934668SKenneth D. Merry 
2107a9934668SKenneth D. Merry 		if (!TAILQ_EMPTY(&softc->done_queue)) {
2108a9934668SKenneth D. Merry 			revents |= poll_events & (POLLIN | POLLRDNORM);
2109a9934668SKenneth D. Merry 		}
2110a9934668SKenneth D. Merry 		cam_periph_unlock(periph);
2111a9934668SKenneth D. Merry 		if (revents == 0)
2112a9934668SKenneth D. Merry 			selrecord(td, &softc->read_select);
2113a9934668SKenneth D. Merry 	}
2114a9934668SKenneth D. Merry 
2115a9934668SKenneth D. Merry 	return (revents);
2116a9934668SKenneth D. Merry }
2117a9934668SKenneth D. Merry 
2118a9934668SKenneth D. Merry static int
passkqfilter(struct cdev * dev,struct knote * kn)2119a9934668SKenneth D. Merry passkqfilter(struct cdev *dev, struct knote *kn)
2120a9934668SKenneth D. Merry {
2121a9934668SKenneth D. Merry 	struct cam_periph *periph;
2122a9934668SKenneth D. Merry 	struct pass_softc *softc;
2123a9934668SKenneth D. Merry 
2124a9934668SKenneth D. Merry 	periph = (struct cam_periph *)dev->si_drv1;
2125a9934668SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
2126a9934668SKenneth D. Merry 
2127a9934668SKenneth D. Merry 	kn->kn_hook = (caddr_t)periph;
2128a9934668SKenneth D. Merry 	kn->kn_fop = &passread_filtops;
2129a9934668SKenneth D. Merry 	knlist_add(&softc->read_select.si_note, kn, 0);
2130a9934668SKenneth D. Merry 
2131a9934668SKenneth D. Merry 	return (0);
2132a9934668SKenneth D. Merry }
2133a9934668SKenneth D. Merry 
2134a9934668SKenneth D. Merry static void
passreadfiltdetach(struct knote * kn)2135a9934668SKenneth D. Merry passreadfiltdetach(struct knote *kn)
2136a9934668SKenneth D. Merry {
2137a9934668SKenneth D. Merry 	struct cam_periph *periph;
2138a9934668SKenneth D. Merry 	struct pass_softc *softc;
2139a9934668SKenneth D. Merry 
2140a9934668SKenneth D. Merry 	periph = (struct cam_periph *)kn->kn_hook;
2141a9934668SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
2142a9934668SKenneth D. Merry 
2143a9934668SKenneth D. Merry 	knlist_remove(&softc->read_select.si_note, kn, 0);
2144a9934668SKenneth D. Merry }
2145a9934668SKenneth D. Merry 
2146a9934668SKenneth D. Merry static int
passreadfilt(struct knote * kn,long hint)2147a9934668SKenneth D. Merry passreadfilt(struct knote *kn, long hint)
2148a9934668SKenneth D. Merry {
2149a9934668SKenneth D. Merry 	struct cam_periph *periph;
2150a9934668SKenneth D. Merry 	struct pass_softc *softc;
2151a9934668SKenneth D. Merry 	int retval;
2152a9934668SKenneth D. Merry 
2153a9934668SKenneth D. Merry 	periph = (struct cam_periph *)kn->kn_hook;
2154a9934668SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
2155a9934668SKenneth D. Merry 
2156a9934668SKenneth D. Merry 	cam_periph_assert(periph, MA_OWNED);
2157a9934668SKenneth D. Merry 
2158a9934668SKenneth D. Merry 	if (TAILQ_EMPTY(&softc->done_queue))
2159a9934668SKenneth D. Merry 		retval = 0;
2160a9934668SKenneth D. Merry 	else
2161a9934668SKenneth D. Merry 		retval = 1;
2162a9934668SKenneth D. Merry 
2163a9934668SKenneth D. Merry 	return (retval);
2164a9934668SKenneth D. Merry }
2165a9934668SKenneth D. Merry 
216676babe50SJustin T. Gibbs /*
216776babe50SJustin T. Gibbs  * Generally, "ccb" should be the CCB supplied by the kernel.  "inccb"
216876babe50SJustin T. Gibbs  * should be the CCB that is copied in from the user.
216976babe50SJustin T. Gibbs  */
217076babe50SJustin T. Gibbs static int
passsendccb(struct cam_periph * periph,union ccb * ccb,union ccb * inccb)217176babe50SJustin T. Gibbs passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
217276babe50SJustin T. Gibbs {
217376babe50SJustin T. Gibbs 	struct pass_softc *softc;
217476babe50SJustin T. Gibbs 	struct cam_periph_map_info mapinfo;
2175a1a604caSAlexander Motin 	uint8_t *cmd;
217695fbded6SScott Long 	xpt_opcode fc;
217795fbded6SScott Long 	int error;
217876babe50SJustin T. Gibbs 
217976babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
218076babe50SJustin T. Gibbs 
218176babe50SJustin T. Gibbs 	/*
218276babe50SJustin T. Gibbs 	 * There are some fields in the CCB header that need to be
218376babe50SJustin T. Gibbs 	 * preserved, the rest we get from the user.
218476babe50SJustin T. Gibbs 	 */
218576babe50SJustin T. Gibbs 	xpt_merge_ccb(ccb, inccb);
218676babe50SJustin T. Gibbs 
2187a1a604caSAlexander Motin 	if (ccb->ccb_h.flags & CAM_CDB_POINTER) {
2188a1a604caSAlexander Motin 		cmd = __builtin_alloca(ccb->csio.cdb_len);
2189a1a604caSAlexander Motin 		error = copyin(ccb->csio.cdb_io.cdb_ptr, cmd, ccb->csio.cdb_len);
2190a1a604caSAlexander Motin 		if (error)
2191a1a604caSAlexander Motin 			return (error);
2192a1a604caSAlexander Motin 		ccb->csio.cdb_io.cdb_ptr = cmd;
2193a1a604caSAlexander Motin 	}
2194a1a604caSAlexander Motin 
219576babe50SJustin T. Gibbs 	/*
219695fbded6SScott Long 	 * Let cam_periph_mapmem do a sanity check on the data pointer format.
219795fbded6SScott Long 	 * Even if no data transfer is needed, it's a cheap check and it
219895fbded6SScott Long 	 * simplifies the code.
219976babe50SJustin T. Gibbs 	 */
220095fbded6SScott Long 	fc = ccb->ccb_h.func_code;
220195fbded6SScott Long 	if ((fc == XPT_SCSI_IO) || (fc == XPT_ATA_IO) || (fc == XPT_SMP_IO)
2202a3cf03a5SWarner Losh             || (fc == XPT_DEV_MATCH) || (fc == XPT_DEV_ADVINFO) || (fc == XPT_MMC_IO)
2203a3cf03a5SWarner Losh             || (fc == XPT_NVME_ADMIN) || (fc == XPT_NVME_IO)) {
220476babe50SJustin T. Gibbs 		bzero(&mapinfo, sizeof(mapinfo));
220576babe50SJustin T. Gibbs 
22062b83592fSScott Long 		/*
22072b83592fSScott Long 		 * cam_periph_mapmem calls into proc and vm functions that can
22082b83592fSScott Long 		 * sleep as well as trigger I/O, so we can't hold the lock.
22092b83592fSScott Long 		 * Dropping it here is reasonably safe.
22102b83592fSScott Long 		 */
22112b83592fSScott Long 		cam_periph_unlock(periph);
2212de239312SAlexander Motin 		error = cam_periph_mapmem(ccb, &mapinfo, softc->maxio);
22132b83592fSScott Long 		cam_periph_lock(periph);
221476babe50SJustin T. Gibbs 
221576babe50SJustin T. Gibbs 		/*
221676babe50SJustin T. Gibbs 		 * cam_periph_mapmem returned an error, we can't continue.
221776babe50SJustin T. Gibbs 		 * Return the error to the user.
221876babe50SJustin T. Gibbs 		 */
221976babe50SJustin T. Gibbs 		if (error)
222076babe50SJustin T. Gibbs 			return(error);
222195fbded6SScott Long 	} else
222295fbded6SScott Long 		/* Ensure that the unmap call later on is a no-op. */
222395fbded6SScott Long 		mapinfo.num_bufs_used = 0;
222476babe50SJustin T. Gibbs 
222576babe50SJustin T. Gibbs 	/*
222676babe50SJustin T. Gibbs 	 * If the user wants us to perform any error recovery, then honor
222776babe50SJustin T. Gibbs 	 * that request.  Otherwise, it's up to the user to perform any
222876babe50SJustin T. Gibbs 	 * error recovery.
222976babe50SJustin T. Gibbs 	 */
2230e474a8e2SWarner Losh 	{
2231e474a8e2SWarner Losh 		uint32_t cam_flags, sense_flags;
2232e474a8e2SWarner Losh 
2233e474a8e2SWarner Losh 		passflags(ccb, &cam_flags, &sense_flags);
2234e474a8e2SWarner Losh 		cam_periph_runccb(ccb,  passerror, cam_flags,
2235e474a8e2SWarner Losh 		    sense_flags, softc->device_stats);
2236e474a8e2SWarner Losh 	}
223776babe50SJustin T. Gibbs 
22388cb46437SAlexander Motin 	cam_periph_unlock(periph);
2239d068ea16SMark Johnston 	error = cam_periph_unmapmem(ccb, &mapinfo);
22408cb46437SAlexander Motin 	cam_periph_lock(periph);
224176babe50SJustin T. Gibbs 
224276babe50SJustin T. Gibbs 	ccb->ccb_h.cbfcnp = NULL;
224376babe50SJustin T. Gibbs 	ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
224476babe50SJustin T. Gibbs 	bcopy(ccb, inccb, sizeof(union ccb));
224576babe50SJustin T. Gibbs 
2246d068ea16SMark Johnston 	return (error);
224776babe50SJustin T. Gibbs }
224876babe50SJustin T. Gibbs 
2249e474a8e2SWarner Losh /*
2250e474a8e2SWarner Losh  * Set the cam_flags and sense_flags based on whether or not the request wants
2251e474a8e2SWarner Losh  * error recovery. In order to log errors via devctl, we need to do at least
2252e474a8e2SWarner Losh  * minimal recovery. We do this by not retrying unit attention (we let the
2253e474a8e2SWarner Losh  * requester do it, or not, if appropriate) and specifically asking for no
2254e474a8e2SWarner Losh  * recovery, like we do during device probing.
2255e474a8e2SWarner Losh  */
2256e474a8e2SWarner Losh static void
passflags(union ccb * ccb,uint32_t * cam_flags,uint32_t * sense_flags)2257e474a8e2SWarner Losh passflags(union ccb *ccb, uint32_t *cam_flags, uint32_t *sense_flags)
2258e474a8e2SWarner Losh {
2259e474a8e2SWarner Losh 	if ((ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) != 0) {
2260e474a8e2SWarner Losh 		*cam_flags = CAM_RETRY_SELTO;
2261e474a8e2SWarner Losh 		*sense_flags = SF_RETRY_UA | SF_NO_PRINT;
2262e474a8e2SWarner Losh 	} else {
2263e474a8e2SWarner Losh 		*cam_flags = 0;
2264e474a8e2SWarner Losh 		*sense_flags = SF_NO_RETRY | SF_NO_RECOVERY | SF_NO_PRINT;
2265e474a8e2SWarner Losh 	}
2266e474a8e2SWarner Losh }
2267e474a8e2SWarner Losh 
226876babe50SJustin T. Gibbs static int
passerror(union ccb * ccb,uint32_t cam_flags,uint32_t sense_flags)22697c5d20a6SWarner Losh passerror(union ccb *ccb, uint32_t cam_flags, uint32_t sense_flags)
227076babe50SJustin T. Gibbs {
227176babe50SJustin T. Gibbs 
2272553484aeSWarner Losh 	return(cam_periph_error(ccb, cam_flags, sense_flags));
227376babe50SJustin T. Gibbs }
2274