xref: /freebsd/sys/cam/scsi/scsi_pass.c (revision 6637b7460066314f9b3ce0073ec6563e15ece608)
1898b0535SWarner Losh /*-
2bec9534dSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 
30ee709e70SDavid E. O'Brien #include <sys/cdefs.h>
31ee709e70SDavid E. O'Brien __FBSDID("$FreeBSD$");
32ee709e70SDavid E. O'Brien 
3376babe50SJustin T. Gibbs #include <sys/param.h>
3476babe50SJustin T. Gibbs #include <sys/systm.h>
3576babe50SJustin T. Gibbs #include <sys/kernel.h>
36a9934668SKenneth D. Merry #include <sys/conf.h>
3776babe50SJustin T. Gibbs #include <sys/types.h>
389626b608SPoul-Henning Kamp #include <sys/bio.h>
39a9934668SKenneth D. Merry #include <sys/bus.h>
4076babe50SJustin T. Gibbs #include <sys/devicestat.h>
41a9934668SKenneth D. Merry #include <sys/errno.h>
42a9934668SKenneth D. Merry #include <sys/fcntl.h>
43a9934668SKenneth D. Merry #include <sys/malloc.h>
44f7312ca2SRobert Watson #include <sys/proc.h>
45a9934668SKenneth D. Merry #include <sys/poll.h>
46a9934668SKenneth D. Merry #include <sys/selinfo.h>
47a9934668SKenneth D. Merry #include <sys/sdt.h>
48871dc983SBrooks Davis #include <sys/sysent.h>
49416494d7SJustin T. Gibbs #include <sys/taskqueue.h>
50a9934668SKenneth D. Merry #include <vm/uma.h>
51a9934668SKenneth D. Merry #include <vm/vm.h>
52a9934668SKenneth D. Merry #include <vm/vm_extern.h>
53a9934668SKenneth D. Merry 
54a9934668SKenneth D. Merry #include <machine/bus.h>
5576babe50SJustin T. Gibbs 
5676babe50SJustin T. Gibbs #include <cam/cam.h>
5776babe50SJustin T. Gibbs #include <cam/cam_ccb.h>
5876babe50SJustin T. Gibbs #include <cam/cam_periph.h>
593393f8daSKenneth D. Merry #include <cam/cam_queue.h>
60a9934668SKenneth D. Merry #include <cam/cam_xpt.h>
6176babe50SJustin T. Gibbs #include <cam/cam_xpt_periph.h>
6276babe50SJustin T. Gibbs #include <cam/cam_debug.h>
6325a2902cSScott Long #include <cam/cam_compat.h>
64a9934668SKenneth D. Merry #include <cam/cam_xpt_periph.h>
6576babe50SJustin T. Gibbs 
6676babe50SJustin T. Gibbs #include <cam/scsi/scsi_all.h>
6776babe50SJustin T. Gibbs #include <cam/scsi/scsi_pass.h>
6876babe50SJustin T. Gibbs 
6976babe50SJustin T. Gibbs typedef enum {
7076babe50SJustin T. Gibbs 	PASS_FLAG_OPEN			= 0x01,
7176babe50SJustin T. Gibbs 	PASS_FLAG_LOCKED		= 0x02,
72ea37f519SKenneth D. Merry 	PASS_FLAG_INVALID		= 0x04,
73a9934668SKenneth D. Merry 	PASS_FLAG_INITIAL_PHYSPATH	= 0x08,
74a9934668SKenneth D. Merry 	PASS_FLAG_ZONE_INPROG		= 0x10,
75a9934668SKenneth D. Merry 	PASS_FLAG_ZONE_VALID		= 0x20,
76a9934668SKenneth D. Merry 	PASS_FLAG_UNMAPPED_CAPABLE	= 0x40,
77a9934668SKenneth D. Merry 	PASS_FLAG_ABANDONED_REF_SET	= 0x80
7876babe50SJustin T. Gibbs } pass_flags;
7976babe50SJustin T. Gibbs 
8076babe50SJustin T. Gibbs typedef enum {
8176babe50SJustin T. Gibbs 	PASS_STATE_NORMAL
8276babe50SJustin T. Gibbs } pass_state;
8376babe50SJustin T. Gibbs 
8476babe50SJustin T. Gibbs typedef enum {
85a9934668SKenneth D. Merry 	PASS_CCB_BUFFER_IO,
86a9934668SKenneth D. Merry 	PASS_CCB_QUEUED_IO
8776babe50SJustin T. Gibbs } pass_ccb_types;
8876babe50SJustin T. Gibbs 
8976babe50SJustin T. Gibbs #define ccb_type	ppriv_field0
90a9934668SKenneth D. Merry #define ccb_ioreq	ppriv_ptr1
91a9934668SKenneth D. Merry 
92a9934668SKenneth D. Merry /*
93a9934668SKenneth D. Merry  * The maximum number of memory segments we preallocate.
94a9934668SKenneth D. Merry  */
95a9934668SKenneth D. Merry #define	PASS_MAX_SEGS	16
96a9934668SKenneth D. Merry 
97a9934668SKenneth D. Merry typedef enum {
98a9934668SKenneth D. Merry 	PASS_IO_NONE		= 0x00,
99a9934668SKenneth D. Merry 	PASS_IO_USER_SEG_MALLOC	= 0x01,
100a9934668SKenneth D. Merry 	PASS_IO_KERN_SEG_MALLOC	= 0x02,
101a9934668SKenneth D. Merry 	PASS_IO_ABANDONED	= 0x04
102a9934668SKenneth D. Merry } pass_io_flags;
103a9934668SKenneth D. Merry 
104a9934668SKenneth D. Merry struct pass_io_req {
105a9934668SKenneth D. Merry 	union ccb			 ccb;
106a9934668SKenneth D. Merry 	union ccb			*alloced_ccb;
107a9934668SKenneth D. Merry 	union ccb			*user_ccb_ptr;
108a9934668SKenneth D. Merry 	camq_entry			 user_periph_links;
109a9934668SKenneth D. Merry 	ccb_ppriv_area			 user_periph_priv;
110a9934668SKenneth D. Merry 	struct cam_periph_map_info	 mapinfo;
111a9934668SKenneth D. Merry 	pass_io_flags			 flags;
112a9934668SKenneth D. Merry 	ccb_flags			 data_flags;
113a9934668SKenneth D. Merry 	int				 num_user_segs;
114a9934668SKenneth D. Merry 	bus_dma_segment_t		 user_segs[PASS_MAX_SEGS];
115a9934668SKenneth D. Merry 	int				 num_kern_segs;
116a9934668SKenneth D. Merry 	bus_dma_segment_t		 kern_segs[PASS_MAX_SEGS];
117a9934668SKenneth D. Merry 	bus_dma_segment_t		*user_segptr;
118a9934668SKenneth D. Merry 	bus_dma_segment_t		*kern_segptr;
119a9934668SKenneth D. Merry 	int				 num_bufs;
120a9934668SKenneth D. Merry 	uint32_t			 dirs[CAM_PERIPH_MAXMAPS];
121a9934668SKenneth D. Merry 	uint32_t			 lengths[CAM_PERIPH_MAXMAPS];
122a9934668SKenneth D. Merry 	uint8_t				*user_bufs[CAM_PERIPH_MAXMAPS];
123a9934668SKenneth D. Merry 	uint8_t				*kern_bufs[CAM_PERIPH_MAXMAPS];
124a9934668SKenneth D. Merry 	struct bintime			 start_time;
125a9934668SKenneth D. Merry 	TAILQ_ENTRY(pass_io_req)	 links;
126a9934668SKenneth D. Merry };
12776babe50SJustin T. Gibbs 
12876babe50SJustin T. Gibbs struct pass_softc {
12976babe50SJustin T. Gibbs 	pass_state		  state;
13076babe50SJustin T. Gibbs 	pass_flags		  flags;
13176babe50SJustin T. Gibbs 	u_int8_t		  pd_type;
13286d45c7fSKenneth D. Merry 	int			  open_count;
133de239312SAlexander Motin 	u_int		 	  maxio;
134a9d2245eSPoul-Henning Kamp 	struct devstat		 *device_stats;
13589c9c53dSPoul-Henning Kamp 	struct cdev		 *dev;
136416494d7SJustin T. Gibbs 	struct cdev		 *alias_dev;
137416494d7SJustin T. Gibbs 	struct task		  add_physpath_task;
138a9934668SKenneth D. Merry 	struct task		  shutdown_kqueue_task;
139a9934668SKenneth D. Merry 	struct selinfo		  read_select;
140a9934668SKenneth D. Merry 	TAILQ_HEAD(, pass_io_req) incoming_queue;
141a9934668SKenneth D. Merry 	TAILQ_HEAD(, pass_io_req) active_queue;
142a9934668SKenneth D. Merry 	TAILQ_HEAD(, pass_io_req) abandoned_queue;
143a9934668SKenneth D. Merry 	TAILQ_HEAD(, pass_io_req) done_queue;
144a9934668SKenneth D. Merry 	struct cam_periph	 *periph;
145a9934668SKenneth D. Merry 	char			  zone_name[12];
146a9934668SKenneth D. Merry 	char			  io_zone_name[12];
147a9934668SKenneth D. Merry 	uma_zone_t		  pass_zone;
148a9934668SKenneth D. Merry 	uma_zone_t		  pass_io_zone;
149a9934668SKenneth D. Merry 	size_t			  io_zone_size;
15076babe50SJustin T. Gibbs };
15176babe50SJustin T. Gibbs 
15276babe50SJustin T. Gibbs static	d_open_t	passopen;
15376babe50SJustin T. Gibbs static	d_close_t	passclose;
15476babe50SJustin T. Gibbs static	d_ioctl_t	passioctl;
15525a2902cSScott Long static	d_ioctl_t	passdoioctl;
156a9934668SKenneth D. Merry static	d_poll_t	passpoll;
157a9934668SKenneth D. Merry static	d_kqfilter_t	passkqfilter;
158a9934668SKenneth D. Merry static	void		passreadfiltdetach(struct knote *kn);
159a9934668SKenneth D. Merry static	int		passreadfilt(struct knote *kn, long hint);
16076babe50SJustin T. Gibbs 
16176babe50SJustin T. Gibbs static	periph_init_t	passinit;
16276babe50SJustin T. Gibbs static	periph_ctor_t	passregister;
163ee9c90c7SKenneth D. Merry static	periph_oninv_t	passoninvalidate;
16476babe50SJustin T. Gibbs static	periph_dtor_t	passcleanup;
165a9934668SKenneth D. Merry static	periph_start_t	passstart;
166a9934668SKenneth D. Merry static	void		pass_shutdown_kqueue(void *context, int pending);
167416494d7SJustin T. Gibbs static	void		pass_add_physpath(void *context, int pending);
16876babe50SJustin T. Gibbs static	void		passasync(void *callback_arg, u_int32_t code,
16976babe50SJustin T. Gibbs 				  struct cam_path *path, void *arg);
170a9934668SKenneth D. Merry static	void		passdone(struct cam_periph *periph,
171a9934668SKenneth D. Merry 				 union ccb *done_ccb);
172a9934668SKenneth D. Merry static	int		passcreatezone(struct cam_periph *periph);
173a9934668SKenneth D. Merry static	void		passiocleanup(struct pass_softc *softc,
174a9934668SKenneth D. Merry 				      struct pass_io_req *io_req);
175a9934668SKenneth D. Merry static	int		passcopysglist(struct cam_periph *periph,
176a9934668SKenneth D. Merry 				       struct pass_io_req *io_req,
177a9934668SKenneth D. Merry 				       ccb_flags direction);
178a9934668SKenneth D. Merry static	int		passmemsetup(struct cam_periph *periph,
179a9934668SKenneth D. Merry 				     struct pass_io_req *io_req);
180a9934668SKenneth D. Merry static	int		passmemdone(struct cam_periph *periph,
181a9934668SKenneth D. Merry 				    struct pass_io_req *io_req);
18276babe50SJustin T. Gibbs static	int		passerror(union ccb *ccb, u_int32_t cam_flags,
18376babe50SJustin T. Gibbs 				  u_int32_t sense_flags);
18476babe50SJustin T. Gibbs static 	int		passsendccb(struct cam_periph *periph, union ccb *ccb,
18576babe50SJustin T. Gibbs 				    union ccb *inccb);
18676babe50SJustin T. Gibbs 
18776babe50SJustin T. Gibbs static struct periph_driver passdriver =
18876babe50SJustin T. Gibbs {
18976babe50SJustin T. Gibbs 	passinit, "pass",
19076babe50SJustin T. Gibbs 	TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0
19176babe50SJustin T. Gibbs };
19276babe50SJustin T. Gibbs 
1930b7c27b9SPeter Wemm PERIPHDRIVER_DECLARE(pass, passdriver);
19476babe50SJustin T. Gibbs 
1954e2f199eSPoul-Henning Kamp static struct cdevsw pass_cdevsw = {
196dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
197c552ebe1SKenneth D. Merry 	.d_flags =	D_TRACKCLOSE,
1987ac40f5fSPoul-Henning Kamp 	.d_open =	passopen,
1997ac40f5fSPoul-Henning Kamp 	.d_close =	passclose,
2007ac40f5fSPoul-Henning Kamp 	.d_ioctl =	passioctl,
201a9934668SKenneth D. Merry 	.d_poll = 	passpoll,
202a9934668SKenneth D. Merry 	.d_kqfilter = 	passkqfilter,
2037ac40f5fSPoul-Henning Kamp 	.d_name =	"pass",
20476babe50SJustin T. Gibbs };
20576babe50SJustin T. Gibbs 
206a9934668SKenneth D. Merry static struct filterops passread_filtops = {
207a9934668SKenneth D. Merry 	.f_isfd	=	1,
208a9934668SKenneth D. Merry 	.f_detach =	passreadfiltdetach,
209a9934668SKenneth D. Merry 	.f_event =	passreadfilt
210a9934668SKenneth D. Merry };
211a9934668SKenneth D. Merry 
212a9934668SKenneth D. Merry static MALLOC_DEFINE(M_SCSIPASS, "scsi_pass", "scsi passthrough buffers");
213a9934668SKenneth D. Merry 
21476babe50SJustin T. Gibbs static void
21576babe50SJustin T. Gibbs passinit(void)
21676babe50SJustin T. Gibbs {
21776babe50SJustin T. Gibbs 	cam_status status;
21876babe50SJustin T. Gibbs 
21976babe50SJustin T. Gibbs 	/*
22076babe50SJustin T. Gibbs 	 * Install a global async callback.  This callback will
22176babe50SJustin T. Gibbs 	 * receive async callbacks like "new device found".
22276babe50SJustin T. Gibbs 	 */
22385d92640SScott Long 	status = xpt_register_async(AC_FOUND_DEVICE, passasync, NULL, NULL);
22476babe50SJustin T. Gibbs 
22576babe50SJustin T. Gibbs 	if (status != CAM_REQ_CMP) {
22676babe50SJustin T. Gibbs 		printf("pass: Failed to attach master async callback "
22776babe50SJustin T. Gibbs 		       "due to status 0x%x!\n", status);
22876babe50SJustin T. Gibbs 	}
22976babe50SJustin T. Gibbs 
23076babe50SJustin T. Gibbs }
23176babe50SJustin T. Gibbs 
23276babe50SJustin T. Gibbs static void
233a9934668SKenneth D. Merry passrejectios(struct cam_periph *periph)
234a9934668SKenneth D. Merry {
235a9934668SKenneth D. Merry 	struct pass_io_req *io_req, *io_req2;
236a9934668SKenneth D. Merry 	struct pass_softc *softc;
237a9934668SKenneth D. Merry 
238a9934668SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
239a9934668SKenneth D. Merry 
240a9934668SKenneth D. Merry 	/*
241a9934668SKenneth D. Merry 	 * The user can no longer get status for I/O on the done queue, so
242a9934668SKenneth D. Merry 	 * clean up all outstanding I/O on the done queue.
243a9934668SKenneth D. Merry 	 */
244a9934668SKenneth D. Merry 	TAILQ_FOREACH_SAFE(io_req, &softc->done_queue, links, io_req2) {
245a9934668SKenneth D. Merry 		TAILQ_REMOVE(&softc->done_queue, io_req, links);
246a9934668SKenneth D. Merry 		passiocleanup(softc, io_req);
247a9934668SKenneth D. Merry 		uma_zfree(softc->pass_zone, io_req);
248a9934668SKenneth D. Merry 	}
249a9934668SKenneth D. Merry 
250a9934668SKenneth D. Merry 	/*
251a9934668SKenneth D. Merry 	 * The underlying device is gone, so we can't issue these I/Os.
252a9934668SKenneth D. Merry 	 * The devfs node has been shut down, so we can't return status to
253a9934668SKenneth D. Merry 	 * the user.  Free any I/O left on the incoming queue.
254a9934668SKenneth D. Merry 	 */
255a9934668SKenneth D. Merry 	TAILQ_FOREACH_SAFE(io_req, &softc->incoming_queue, links, io_req2) {
256a9934668SKenneth D. Merry 		TAILQ_REMOVE(&softc->incoming_queue, io_req, links);
257a9934668SKenneth D. Merry 		passiocleanup(softc, io_req);
258a9934668SKenneth D. Merry 		uma_zfree(softc->pass_zone, io_req);
259a9934668SKenneth D. Merry 	}
260a9934668SKenneth D. Merry 
261a9934668SKenneth D. Merry 	/*
262a9934668SKenneth D. Merry 	 * Normally we would put I/Os on the abandoned queue and acquire a
263a9934668SKenneth D. Merry 	 * reference when we saw the final close.  But, the device went
264a9934668SKenneth D. Merry 	 * away and devfs may have moved everything off to deadfs by the
265a9934668SKenneth D. Merry 	 * time the I/O done callback is called; as a result, we won't see
266a9934668SKenneth D. Merry 	 * any more closes.  So, if we have any active I/Os, we need to put
267a9934668SKenneth D. Merry 	 * them on the abandoned queue.  When the abandoned queue is empty,
268a9934668SKenneth D. Merry 	 * we'll release the remaining reference (see below) to the peripheral.
269a9934668SKenneth D. Merry 	 */
270a9934668SKenneth D. Merry 	TAILQ_FOREACH_SAFE(io_req, &softc->active_queue, links, io_req2) {
271a9934668SKenneth D. Merry 		TAILQ_REMOVE(&softc->active_queue, io_req, links);
272a9934668SKenneth D. Merry 		io_req->flags |= PASS_IO_ABANDONED;
273a9934668SKenneth D. Merry 		TAILQ_INSERT_TAIL(&softc->abandoned_queue, io_req, links);
274a9934668SKenneth D. Merry 	}
275a9934668SKenneth D. Merry 
276a9934668SKenneth D. Merry 	/*
277a9934668SKenneth D. Merry 	 * If we put any I/O on the abandoned queue, acquire a reference.
278a9934668SKenneth D. Merry 	 */
279a9934668SKenneth D. Merry 	if ((!TAILQ_EMPTY(&softc->abandoned_queue))
280a9934668SKenneth D. Merry 	 && ((softc->flags & PASS_FLAG_ABANDONED_REF_SET) == 0)) {
281a9934668SKenneth D. Merry 		cam_periph_doacquire(periph);
282a9934668SKenneth D. Merry 		softc->flags |= PASS_FLAG_ABANDONED_REF_SET;
283a9934668SKenneth D. Merry 	}
284a9934668SKenneth D. Merry }
285a9934668SKenneth D. Merry 
286a9934668SKenneth D. Merry static void
287ea37f519SKenneth D. Merry passdevgonecb(void *arg)
288ea37f519SKenneth D. Merry {
289ea37f519SKenneth D. Merry 	struct cam_periph *periph;
290227d67aaSAlexander Motin 	struct mtx *mtx;
29186d45c7fSKenneth D. Merry 	struct pass_softc *softc;
29286d45c7fSKenneth D. Merry 	int i;
293ea37f519SKenneth D. Merry 
294ea37f519SKenneth D. Merry 	periph = (struct cam_periph *)arg;
295227d67aaSAlexander Motin 	mtx = cam_periph_mtx(periph);
296227d67aaSAlexander Motin 	mtx_lock(mtx);
297ea37f519SKenneth D. Merry 
298227d67aaSAlexander Motin 	softc = (struct pass_softc *)periph->softc;
29986d45c7fSKenneth D. Merry 	KASSERT(softc->open_count >= 0, ("Negative open count %d",
30086d45c7fSKenneth D. Merry 		softc->open_count));
30186d45c7fSKenneth D. Merry 
30286d45c7fSKenneth D. Merry 	/*
30386d45c7fSKenneth D. Merry 	 * When we get this callback, we will get no more close calls from
30486d45c7fSKenneth D. Merry 	 * devfs.  So if we have any dangling opens, we need to release the
30586d45c7fSKenneth D. Merry 	 * reference held for that particular context.
30686d45c7fSKenneth D. Merry 	 */
30786d45c7fSKenneth D. Merry 	for (i = 0; i < softc->open_count; i++)
30886d45c7fSKenneth D. Merry 		cam_periph_release_locked(periph);
30986d45c7fSKenneth D. Merry 
31086d45c7fSKenneth D. Merry 	softc->open_count = 0;
31186d45c7fSKenneth D. Merry 
31286d45c7fSKenneth D. Merry 	/*
31386d45c7fSKenneth D. Merry 	 * Release the reference held for the device node, it is gone now.
314a9934668SKenneth D. Merry 	 * Accordingly, inform all queued I/Os of their fate.
31586d45c7fSKenneth D. Merry 	 */
31686d45c7fSKenneth D. Merry 	cam_periph_release_locked(periph);
317a9934668SKenneth D. Merry 	passrejectios(periph);
31886d45c7fSKenneth D. Merry 
31986d45c7fSKenneth D. Merry 	/*
320a9934668SKenneth D. Merry 	 * We reference the SIM lock directly here, instead of using
32186d45c7fSKenneth D. Merry 	 * cam_periph_unlock().  The reason is that the final call to
32286d45c7fSKenneth D. Merry 	 * cam_periph_release_locked() above could result in the periph
32386d45c7fSKenneth D. Merry 	 * getting freed.  If that is the case, dereferencing the periph
32486d45c7fSKenneth D. Merry 	 * with a cam_periph_unlock() call would cause a page fault.
32586d45c7fSKenneth D. Merry 	 */
326227d67aaSAlexander Motin 	mtx_unlock(mtx);
327a9934668SKenneth D. Merry 
328a9934668SKenneth D. Merry 	/*
329a9934668SKenneth D. Merry 	 * We have to remove our kqueue context from a thread because it
330a9934668SKenneth D. Merry 	 * may sleep.  It would be nice if we could get a callback from
331a9934668SKenneth D. Merry 	 * kqueue when it is done cleaning up resources.
332a9934668SKenneth D. Merry 	 */
333a9934668SKenneth D. Merry 	taskqueue_enqueue(taskqueue_thread, &softc->shutdown_kqueue_task);
334ea37f519SKenneth D. Merry }
335ea37f519SKenneth D. Merry 
336ea37f519SKenneth D. Merry static void
337ee9c90c7SKenneth D. Merry passoninvalidate(struct cam_periph *periph)
338ee9c90c7SKenneth D. Merry {
339ee9c90c7SKenneth D. Merry 	struct pass_softc *softc;
340ee9c90c7SKenneth D. Merry 
341ee9c90c7SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
342ee9c90c7SKenneth D. Merry 
343ee9c90c7SKenneth D. Merry 	/*
344ee9c90c7SKenneth D. Merry 	 * De-register any async callbacks.
345ee9c90c7SKenneth D. Merry 	 */
34685d92640SScott Long 	xpt_register_async(0, passasync, periph, periph->path);
347ee9c90c7SKenneth D. Merry 
348ee9c90c7SKenneth D. Merry 	softc->flags |= PASS_FLAG_INVALID;
349ee9c90c7SKenneth D. Merry 
350ee9c90c7SKenneth D. Merry 	/*
351ea37f519SKenneth D. Merry 	 * Tell devfs this device has gone away, and ask for a callback
352ea37f519SKenneth D. Merry 	 * when it has cleaned up its state.
353ea37f519SKenneth D. Merry 	 */
354ea37f519SKenneth D. Merry 	destroy_dev_sched_cb(softc->dev, passdevgonecb, periph);
355ee9c90c7SKenneth D. Merry }
356ee9c90c7SKenneth D. Merry 
357ee9c90c7SKenneth D. Merry static void
35876babe50SJustin T. Gibbs passcleanup(struct cam_periph *periph)
35976babe50SJustin T. Gibbs {
360ee9c90c7SKenneth D. Merry 	struct pass_softc *softc;
361ee9c90c7SKenneth D. Merry 
362ee9c90c7SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
363ee9c90c7SKenneth D. Merry 
364a9934668SKenneth D. Merry 	cam_periph_assert(periph, MA_OWNED);
365a9934668SKenneth D. Merry 	KASSERT(TAILQ_EMPTY(&softc->active_queue),
366a9934668SKenneth D. Merry 		("%s called when there are commands on the active queue!\n",
367a9934668SKenneth D. Merry 		__func__));
368a9934668SKenneth D. Merry 	KASSERT(TAILQ_EMPTY(&softc->abandoned_queue),
369a9934668SKenneth D. Merry 		("%s called when there are commands on the abandoned queue!\n",
370a9934668SKenneth D. Merry 		__func__));
371a9934668SKenneth D. Merry 	KASSERT(TAILQ_EMPTY(&softc->incoming_queue),
372a9934668SKenneth D. Merry 		("%s called when there are commands on the incoming queue!\n",
373a9934668SKenneth D. Merry 		__func__));
374a9934668SKenneth D. Merry 	KASSERT(TAILQ_EMPTY(&softc->done_queue),
375a9934668SKenneth D. Merry 		("%s called when there are commands on the done queue!\n",
376a9934668SKenneth D. Merry 		__func__));
377a9934668SKenneth D. Merry 
3785f3fed85SEdward Tomasz Napierala 	devstat_remove_entry(softc->device_stats);
379416494d7SJustin T. Gibbs 
3805f3fed85SEdward Tomasz Napierala 	cam_periph_unlock(periph);
381a9934668SKenneth D. Merry 
382a9934668SKenneth D. Merry 	/*
383a9934668SKenneth D. Merry 	 * We call taskqueue_drain() for the physpath task to make sure it
384a9934668SKenneth D. Merry 	 * is complete.  We drop the lock because this can potentially
385a9934668SKenneth D. Merry 	 * sleep.  XXX KDM that is bad.  Need a way to get a callback when
386a9934668SKenneth D. Merry 	 * a taskqueue is drained.
387a9934668SKenneth D. Merry 	 *
388a9934668SKenneth D. Merry  	 * Note that we don't drain the kqueue shutdown task queue.  This
389a9934668SKenneth D. Merry 	 * is because we hold a reference on the periph for kqueue, and
390a9934668SKenneth D. Merry 	 * release that reference from the kqueue shutdown task queue.  So
391a9934668SKenneth D. Merry 	 * we cannot come into this routine unless we've released that
392a9934668SKenneth D. Merry 	 * reference.  Also, because that could be the last reference, we
393a9934668SKenneth D. Merry 	 * could be called from the cam_periph_release() call in
394a9934668SKenneth D. Merry 	 * pass_shutdown_kqueue().  In that case, the taskqueue_drain()
395a9934668SKenneth D. Merry 	 * would deadlock.  It would be preferable if we had a way to
396a9934668SKenneth D. Merry 	 * get a callback when a taskqueue is done.
397a9934668SKenneth D. Merry 	 */
398416494d7SJustin T. Gibbs 	taskqueue_drain(taskqueue_thread, &softc->add_physpath_task);
399416494d7SJustin T. Gibbs 
4005f3fed85SEdward Tomasz Napierala 	cam_periph_lock(periph);
401416494d7SJustin T. Gibbs 
402ee9c90c7SKenneth D. Merry 	free(softc, M_DEVBUF);
40376babe50SJustin T. Gibbs }
40476babe50SJustin T. Gibbs 
40576babe50SJustin T. Gibbs static void
406a9934668SKenneth D. Merry pass_shutdown_kqueue(void *context, int pending)
407a9934668SKenneth D. Merry {
408a9934668SKenneth D. Merry 	struct cam_periph *periph;
409a9934668SKenneth D. Merry 	struct pass_softc *softc;
410a9934668SKenneth D. Merry 
411a9934668SKenneth D. Merry 	periph = context;
412a9934668SKenneth D. Merry 	softc = periph->softc;
413a9934668SKenneth D. Merry 
414a9934668SKenneth D. Merry 	knlist_clear(&softc->read_select.si_note, /*is_locked*/ 0);
415a9934668SKenneth D. Merry 	knlist_destroy(&softc->read_select.si_note);
416a9934668SKenneth D. Merry 
417a9934668SKenneth D. Merry 	/*
418a9934668SKenneth D. Merry 	 * Release the reference we held for kqueue.
419a9934668SKenneth D. Merry 	 */
420a9934668SKenneth D. Merry 	cam_periph_release(periph);
421a9934668SKenneth D. Merry }
422a9934668SKenneth D. Merry 
423a9934668SKenneth D. Merry static void
424416494d7SJustin T. Gibbs pass_add_physpath(void *context, int pending)
425416494d7SJustin T. Gibbs {
426416494d7SJustin T. Gibbs 	struct cam_periph *periph;
427416494d7SJustin T. Gibbs 	struct pass_softc *softc;
428a9934668SKenneth D. Merry 	struct mtx *mtx;
429416494d7SJustin T. Gibbs 	char *physpath;
430416494d7SJustin T. Gibbs 
431416494d7SJustin T. Gibbs 	/*
432416494d7SJustin T. Gibbs 	 * If we have one, create a devfs alias for our
433416494d7SJustin T. Gibbs 	 * physical path.
434416494d7SJustin T. Gibbs 	 */
435416494d7SJustin T. Gibbs 	periph = context;
436416494d7SJustin T. Gibbs 	softc = periph->softc;
4376884b662SAlexander Motin 	physpath = malloc(MAXPATHLEN, M_DEVBUF, M_WAITOK);
438a9934668SKenneth D. Merry 	mtx = cam_periph_mtx(periph);
439a9934668SKenneth D. Merry 	mtx_lock(mtx);
440a9934668SKenneth D. Merry 
441a9934668SKenneth D. Merry 	if (periph->flags & CAM_PERIPH_INVALID)
4426884b662SAlexander Motin 		goto out;
443a9934668SKenneth D. Merry 
444416494d7SJustin T. Gibbs 	if (xpt_getattr(physpath, MAXPATHLEN,
445416494d7SJustin T. Gibbs 			"GEOM::physpath", periph->path) == 0
446416494d7SJustin T. Gibbs 	 && strlen(physpath) != 0) {
447a9934668SKenneth D. Merry 		mtx_unlock(mtx);
448416494d7SJustin T. Gibbs 		make_dev_physpath_alias(MAKEDEV_WAITOK, &softc->alias_dev,
449416494d7SJustin T. Gibbs 					softc->dev, softc->alias_dev, physpath);
450a9934668SKenneth D. Merry 		mtx_lock(mtx);
451416494d7SJustin T. Gibbs 	}
452ea37f519SKenneth D. Merry 
453a9934668SKenneth D. Merry out:
454ea37f519SKenneth D. Merry 	/*
455ea37f519SKenneth D. Merry 	 * Now that we've made our alias, we no longer have to have a
456ea37f519SKenneth D. Merry 	 * reference to the device.
457ea37f519SKenneth D. Merry 	 */
458a9934668SKenneth D. Merry 	if ((softc->flags & PASS_FLAG_INITIAL_PHYSPATH) == 0)
459ea37f519SKenneth D. Merry 		softc->flags |= PASS_FLAG_INITIAL_PHYSPATH;
4606884b662SAlexander Motin 
461a9934668SKenneth D. Merry 	/*
462a9934668SKenneth D. Merry 	 * We always acquire a reference to the periph before queueing this
463a9934668SKenneth D. Merry 	 * task queue function, so it won't go away before we run.
464a9934668SKenneth D. Merry 	 */
465a9934668SKenneth D. Merry 	while (pending-- > 0)
466a9934668SKenneth D. Merry 		cam_periph_release_locked(periph);
467a9934668SKenneth D. Merry 	mtx_unlock(mtx);
468a9934668SKenneth D. Merry 
4696884b662SAlexander Motin 	free(physpath, M_DEVBUF);
470416494d7SJustin T. Gibbs }
471416494d7SJustin T. Gibbs 
472416494d7SJustin T. Gibbs static void
47376babe50SJustin T. Gibbs passasync(void *callback_arg, u_int32_t code,
47476babe50SJustin T. Gibbs 	  struct cam_path *path, void *arg)
47576babe50SJustin T. Gibbs {
47676babe50SJustin T. Gibbs 	struct cam_periph *periph;
47776babe50SJustin T. Gibbs 
47876babe50SJustin T. Gibbs 	periph = (struct cam_periph *)callback_arg;
47976babe50SJustin T. Gibbs 
48076babe50SJustin T. Gibbs 	switch (code) {
48176babe50SJustin T. Gibbs 	case AC_FOUND_DEVICE:
48276babe50SJustin T. Gibbs 	{
48376babe50SJustin T. Gibbs 		struct ccb_getdev *cgd;
48476babe50SJustin T. Gibbs 		cam_status status;
48576babe50SJustin T. Gibbs 
48676babe50SJustin T. Gibbs 		cgd = (struct ccb_getdev *)arg;
487c5ff3b2fSMatt Jacob 		if (cgd == NULL)
488c5ff3b2fSMatt Jacob 			break;
48976babe50SJustin T. Gibbs 
49076babe50SJustin T. Gibbs 		/*
49176babe50SJustin T. Gibbs 		 * Allocate a peripheral instance for
49276babe50SJustin T. Gibbs 		 * this device and start the probe
49376babe50SJustin T. Gibbs 		 * process.
49476babe50SJustin T. Gibbs 		 */
495ee9c90c7SKenneth D. Merry 		status = cam_periph_alloc(passregister, passoninvalidate,
496a9934668SKenneth D. Merry 					  passcleanup, passstart, "pass",
497227d67aaSAlexander Motin 					  CAM_PERIPH_BIO, path,
498ee9c90c7SKenneth D. Merry 					  passasync, AC_FOUND_DEVICE, cgd);
49976babe50SJustin T. Gibbs 
50076babe50SJustin T. Gibbs 		if (status != CAM_REQ_CMP
5013393f8daSKenneth D. Merry 		 && status != CAM_REQ_INPROG) {
5023393f8daSKenneth D. Merry 			const struct cam_status_entry *entry;
5033393f8daSKenneth D. Merry 
5043393f8daSKenneth D. Merry 			entry = cam_fetch_status_entry(status);
5053393f8daSKenneth D. Merry 
50676babe50SJustin T. Gibbs 			printf("passasync: Unable to attach new device "
5073393f8daSKenneth D. Merry 			       "due to status %#x: %s\n", status, entry ?
5083393f8daSKenneth D. Merry 			       entry->status_text : "Unknown");
5093393f8daSKenneth D. Merry 		}
51076babe50SJustin T. Gibbs 
51176babe50SJustin T. Gibbs 		break;
51276babe50SJustin T. Gibbs 	}
513416494d7SJustin T. Gibbs 	case AC_ADVINFO_CHANGED:
514416494d7SJustin T. Gibbs 	{
515416494d7SJustin T. Gibbs 		uintptr_t buftype;
516416494d7SJustin T. Gibbs 
517416494d7SJustin T. Gibbs 		buftype = (uintptr_t)arg;
518416494d7SJustin T. Gibbs 		if (buftype == CDAI_TYPE_PHYS_PATH) {
519416494d7SJustin T. Gibbs 			struct pass_softc *softc;
520416494d7SJustin T. Gibbs 
521416494d7SJustin T. Gibbs 			softc = (struct pass_softc *)periph->softc;
522a9934668SKenneth D. Merry 			/*
523a9934668SKenneth D. Merry 			 * Acquire a reference to the periph before we
524a9934668SKenneth D. Merry 			 * start the taskqueue, so that we don't run into
525a9934668SKenneth D. Merry 			 * a situation where the periph goes away before
526a9934668SKenneth D. Merry 			 * the task queue has a chance to run.
527a9934668SKenneth D. Merry 			 */
52899e7a4adSScott Long 			if (cam_periph_acquire(periph) != 0)
529a9934668SKenneth D. Merry 				break;
530a9934668SKenneth D. Merry 
531416494d7SJustin T. Gibbs 			taskqueue_enqueue(taskqueue_thread,
532416494d7SJustin T. Gibbs 					  &softc->add_physpath_task);
533416494d7SJustin T. Gibbs 		}
534416494d7SJustin T. Gibbs 		break;
535416494d7SJustin T. Gibbs 	}
53676babe50SJustin T. Gibbs 	default:
537516871c6SJustin T. Gibbs 		cam_periph_async(periph, code, path, arg);
53876babe50SJustin T. Gibbs 		break;
53976babe50SJustin T. Gibbs 	}
54076babe50SJustin T. Gibbs }
54176babe50SJustin T. Gibbs 
54276babe50SJustin T. Gibbs static cam_status
54376babe50SJustin T. Gibbs passregister(struct cam_periph *periph, void *arg)
54476babe50SJustin T. Gibbs {
54576babe50SJustin T. Gibbs 	struct pass_softc *softc;
54676babe50SJustin T. Gibbs 	struct ccb_getdev *cgd;
547b8b6b5d3SAlexander Motin 	struct ccb_pathinq cpi;
548ee198893SKonstantin Belousov 	struct make_dev_args args;
549ee198893SKonstantin Belousov 	int error, no_tags;
55076babe50SJustin T. Gibbs 
55176babe50SJustin T. Gibbs 	cgd = (struct ccb_getdev *)arg;
55276babe50SJustin T. Gibbs 	if (cgd == NULL) {
553ea37f519SKenneth D. Merry 		printf("%s: no getdev CCB, can't register device\n", __func__);
55476babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
55576babe50SJustin T. Gibbs 	}
55676babe50SJustin T. Gibbs 
55776babe50SJustin T. Gibbs 	softc = (struct pass_softc *)malloc(sizeof(*softc),
55876babe50SJustin T. Gibbs 					    M_DEVBUF, M_NOWAIT);
55976babe50SJustin T. Gibbs 
56076babe50SJustin T. Gibbs 	if (softc == NULL) {
561ea37f519SKenneth D. Merry 		printf("%s: Unable to probe new device. "
562ea37f519SKenneth D. Merry 		       "Unable to allocate softc\n", __func__);
56376babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
56476babe50SJustin T. Gibbs 	}
56576babe50SJustin T. Gibbs 
56676babe50SJustin T. Gibbs 	bzero(softc, sizeof(*softc));
56776babe50SJustin T. Gibbs 	softc->state = PASS_STATE_NORMAL;
568b8b6b5d3SAlexander Motin 	if (cgd->protocol == PROTO_SCSI || cgd->protocol == PROTO_ATAPI)
56910b6172aSMatt Jacob 		softc->pd_type = SID_TYPE(&cgd->inq_data);
570b8b6b5d3SAlexander Motin 	else if (cgd->protocol == PROTO_SATAPM)
571b8b6b5d3SAlexander Motin 		softc->pd_type = T_ENCLOSURE;
572b8b6b5d3SAlexander Motin 	else
573b8b6b5d3SAlexander Motin 		softc->pd_type = T_DIRECT;
57476babe50SJustin T. Gibbs 
57576babe50SJustin T. Gibbs 	periph->softc = softc;
576a9934668SKenneth D. Merry 	softc->periph = periph;
577a9934668SKenneth D. Merry 	TAILQ_INIT(&softc->incoming_queue);
578a9934668SKenneth D. Merry 	TAILQ_INIT(&softc->active_queue);
579a9934668SKenneth D. Merry 	TAILQ_INIT(&softc->abandoned_queue);
580a9934668SKenneth D. Merry 	TAILQ_INIT(&softc->done_queue);
581a9934668SKenneth D. Merry 	snprintf(softc->zone_name, sizeof(softc->zone_name), "%s%d",
582a9934668SKenneth D. Merry 		 periph->periph_name, periph->unit_number);
583a9934668SKenneth D. Merry 	snprintf(softc->io_zone_name, sizeof(softc->io_zone_name), "%s%dIO",
584a9934668SKenneth D. Merry 		 periph->periph_name, periph->unit_number);
585cd853791SKonstantin Belousov 	softc->io_zone_size = maxphys;
586a9934668SKenneth D. Merry 	knlist_init_mtx(&softc->read_select.si_note, cam_periph_mtx(periph));
5873393f8daSKenneth D. Merry 
588762a7f4fSWarner Losh 	xpt_path_inq(&cpi, periph->path);
589b8b6b5d3SAlexander Motin 
590de239312SAlexander Motin 	if (cpi.maxio == 0)
591de239312SAlexander Motin 		softc->maxio = DFLTPHYS;	/* traditional default */
592cd853791SKonstantin Belousov 	else if (cpi.maxio > maxphys)
593cd853791SKonstantin Belousov 		softc->maxio = maxphys;		/* for safety */
594de239312SAlexander Motin 	else
595de239312SAlexander Motin 		softc->maxio = cpi.maxio;	/* real value */
596de239312SAlexander Motin 
597a9934668SKenneth D. Merry 	if (cpi.hba_misc & PIM_UNMAPPED)
598a9934668SKenneth D. Merry 		softc->flags |= PASS_FLAG_UNMAPPED_CAPABLE;
599a9934668SKenneth D. Merry 
60076babe50SJustin T. Gibbs 	/*
60176babe50SJustin T. Gibbs 	 * We pass in 0 for a blocksize, since we don't
60276babe50SJustin T. Gibbs 	 * know what the blocksize of this device is, if
60376babe50SJustin T. Gibbs 	 * it even has a blocksize.
60476babe50SJustin T. Gibbs 	 */
605edec59d9SAlexander Motin 	cam_periph_unlock(periph);
6063393f8daSKenneth D. Merry 	no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0;
607c81d2c74SMatt Jacob 	softc->device_stats = devstat_new_entry("pass",
608d3ce8327SEd Schouten 			  periph->unit_number, 0,
6093393f8daSKenneth D. Merry 			  DEVSTAT_NO_BLOCKSIZE
6103393f8daSKenneth D. Merry 			  | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0),
61110b6172aSMatt Jacob 			  softc->pd_type |
612b8b6b5d3SAlexander Motin 			  XPORT_DEVSTAT_TYPE(cpi.transport) |
6132a888f93SKenneth D. Merry 			  DEVSTAT_TYPE_PASS,
6142a888f93SKenneth D. Merry 			  DEVSTAT_PRIORITY_PASS);
61573d26919SKenneth D. Merry 
616ea37f519SKenneth D. Merry 	/*
617a9934668SKenneth D. Merry 	 * Initialize the taskqueue handler for shutting down kqueue.
618a9934668SKenneth D. Merry 	 */
619a9934668SKenneth D. Merry 	TASK_INIT(&softc->shutdown_kqueue_task, /*priority*/ 0,
620a9934668SKenneth D. Merry 		  pass_shutdown_kqueue, periph);
621a9934668SKenneth D. Merry 
622a9934668SKenneth D. Merry 	/*
623a9934668SKenneth D. Merry 	 * Acquire a reference to the periph that we can release once we've
624a9934668SKenneth D. Merry 	 * cleaned up the kqueue.
625a9934668SKenneth D. Merry 	 */
62699e7a4adSScott Long 	if (cam_periph_acquire(periph) != 0) {
627a9934668SKenneth D. Merry 		xpt_print(periph->path, "%s: lost periph during "
628a9934668SKenneth D. Merry 			  "registration!\n", __func__);
629a9934668SKenneth D. Merry 		cam_periph_lock(periph);
630a9934668SKenneth D. Merry 		return (CAM_REQ_CMP_ERR);
631a9934668SKenneth D. Merry 	}
632a9934668SKenneth D. Merry 
633a9934668SKenneth D. Merry 	/*
634ea37f519SKenneth D. Merry 	 * Acquire a reference to the periph before we create the devfs
635ea37f519SKenneth D. Merry 	 * instance for it.  We'll release this reference once the devfs
636ea37f519SKenneth D. Merry 	 * instance has been freed.
637ea37f519SKenneth D. Merry 	 */
63899e7a4adSScott Long 	if (cam_periph_acquire(periph) != 0) {
639ea37f519SKenneth D. Merry 		xpt_print(periph->path, "%s: lost periph during "
640ea37f519SKenneth D. Merry 			  "registration!\n", __func__);
64186d45c7fSKenneth D. Merry 		cam_periph_lock(periph);
642ea37f519SKenneth D. Merry 		return (CAM_REQ_CMP_ERR);
643ea37f519SKenneth D. Merry 	}
644ea37f519SKenneth D. Merry 
64573d26919SKenneth D. Merry 	/* Register the device */
646ee198893SKonstantin Belousov 	make_dev_args_init(&args);
647ee198893SKonstantin Belousov 	args.mda_devsw = &pass_cdevsw;
648ee198893SKonstantin Belousov 	args.mda_unit = periph->unit_number;
649ee198893SKonstantin Belousov 	args.mda_uid = UID_ROOT;
650ee198893SKonstantin Belousov 	args.mda_gid = GID_OPERATOR;
651ee198893SKonstantin Belousov 	args.mda_mode = 0600;
652ee198893SKonstantin Belousov 	args.mda_si_drv1 = periph;
653f1f3f7cfSMateusz Guzik 	args.mda_flags = MAKEDEV_NOWAIT;
654ee198893SKonstantin Belousov 	error = make_dev_s(&args, &softc->dev, "%s%d", periph->periph_name,
655ee198893SKonstantin Belousov 	    periph->unit_number);
656ee198893SKonstantin Belousov 	if (error != 0) {
657ee198893SKonstantin Belousov 		cam_periph_lock(periph);
658ee198893SKonstantin Belousov 		cam_periph_release_locked(periph);
659ee198893SKonstantin Belousov 		return (CAM_REQ_CMP_ERR);
660ee198893SKonstantin Belousov 	}
661ea37f519SKenneth D. Merry 
662ea37f519SKenneth D. Merry 	/*
663a9934668SKenneth D. Merry 	 * Hold a reference to the periph before we create the physical
664a9934668SKenneth D. Merry 	 * path alias so it can't go away.
665ea37f519SKenneth D. Merry 	 */
66699e7a4adSScott Long 	if (cam_periph_acquire(periph) != 0) {
667a9934668SKenneth D. Merry 		xpt_print(periph->path, "%s: lost periph during "
668a9934668SKenneth D. Merry 			  "registration!\n", __func__);
669a9934668SKenneth D. Merry 		cam_periph_lock(periph);
670a9934668SKenneth D. Merry 		return (CAM_REQ_CMP_ERR);
671a9934668SKenneth D. Merry 	}
672ea37f519SKenneth D. Merry 
673edec59d9SAlexander Motin 	cam_periph_lock(periph);
67473d26919SKenneth D. Merry 
675416494d7SJustin T. Gibbs 	TASK_INIT(&softc->add_physpath_task, /*priority*/0,
676416494d7SJustin T. Gibbs 		  pass_add_physpath, periph);
677416494d7SJustin T. Gibbs 
67876babe50SJustin T. Gibbs 	/*
679416494d7SJustin T. Gibbs 	 * See if physical path information is already available.
68076babe50SJustin T. Gibbs 	 */
681416494d7SJustin T. Gibbs 	taskqueue_enqueue(taskqueue_thread, &softc->add_physpath_task);
682416494d7SJustin T. Gibbs 
683416494d7SJustin T. Gibbs 	/*
684416494d7SJustin T. Gibbs 	 * Add an async callback so that we get notified if
685416494d7SJustin T. Gibbs 	 * this device goes away or its physical path
686416494d7SJustin T. Gibbs 	 * (stored in the advanced info data of the EDT) has
687416494d7SJustin T. Gibbs 	 * changed.
688416494d7SJustin T. Gibbs 	 */
689416494d7SJustin T. Gibbs 	xpt_register_async(AC_LOST_DEVICE | AC_ADVINFO_CHANGED,
690416494d7SJustin T. Gibbs 			   passasync, periph, periph->path);
69176babe50SJustin T. Gibbs 
69276babe50SJustin T. Gibbs 	if (bootverbose)
69376babe50SJustin T. Gibbs 		xpt_announce_periph(periph, NULL);
69476babe50SJustin T. Gibbs 
69576babe50SJustin T. Gibbs 	return(CAM_REQ_CMP);
69676babe50SJustin T. Gibbs }
69776babe50SJustin T. Gibbs 
69876babe50SJustin T. Gibbs static int
69989c9c53dSPoul-Henning Kamp passopen(struct cdev *dev, int flags, int fmt, struct thread *td)
70076babe50SJustin T. Gibbs {
70176babe50SJustin T. Gibbs 	struct cam_periph *periph;
70276babe50SJustin T. Gibbs 	struct pass_softc *softc;
703e2a5fdf9SNate Lawson 	int error;
70476babe50SJustin T. Gibbs 
705e2a5fdf9SNate Lawson 	periph = (struct cam_periph *)dev->si_drv1;
70699e7a4adSScott Long 	if (cam_periph_acquire(periph) != 0)
70776babe50SJustin T. Gibbs 		return (ENXIO);
70876babe50SJustin T. Gibbs 
7092b83592fSScott Long 	cam_periph_lock(periph);
7102b83592fSScott Long 
71176babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
71276babe50SJustin T. Gibbs 
713ee9c90c7SKenneth D. Merry 	if (softc->flags & PASS_FLAG_INVALID) {
714c552ebe1SKenneth D. Merry 		cam_periph_release_locked(periph);
7152b83592fSScott Long 		cam_periph_unlock(periph);
71676babe50SJustin T. Gibbs 		return(ENXIO);
717ee9c90c7SKenneth D. Merry 	}
71822b9c86cSKenneth D. Merry 
71922b9c86cSKenneth D. Merry 	/*
720f5ef42beSRobert Watson 	 * Don't allow access when we're running at a high securelevel.
72122b9c86cSKenneth D. Merry 	 */
722a854ed98SJohn Baldwin 	error = securelevel_gt(td->td_ucred, 1);
723f7312ca2SRobert Watson 	if (error) {
724c552ebe1SKenneth D. Merry 		cam_periph_release_locked(periph);
7252b83592fSScott Long 		cam_periph_unlock(periph);
726f7312ca2SRobert Watson 		return(error);
72722b9c86cSKenneth D. Merry 	}
72876babe50SJustin T. Gibbs 
72976babe50SJustin T. Gibbs 	/*
73066a0780eSKenneth D. Merry 	 * Only allow read-write access.
73166a0780eSKenneth D. Merry 	 */
73222b9c86cSKenneth D. Merry 	if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) {
733c552ebe1SKenneth D. Merry 		cam_periph_release_locked(periph);
7342b83592fSScott Long 		cam_periph_unlock(periph);
73566a0780eSKenneth D. Merry 		return(EPERM);
73622b9c86cSKenneth D. Merry 	}
73766a0780eSKenneth D. Merry 
73866a0780eSKenneth D. Merry 	/*
73976babe50SJustin T. Gibbs 	 * We don't allow nonblocking access.
74076babe50SJustin T. Gibbs 	 */
74176babe50SJustin T. Gibbs 	if ((flags & O_NONBLOCK) != 0) {
742f0d9af51SMatt Jacob 		xpt_print(periph->path, "can't do nonblocking access\n");
743c552ebe1SKenneth D. Merry 		cam_periph_release_locked(periph);
7442b83592fSScott Long 		cam_periph_unlock(periph);
74522b9c86cSKenneth D. Merry 		return(EINVAL);
74676babe50SJustin T. Gibbs 	}
74776babe50SJustin T. Gibbs 
74886d45c7fSKenneth D. Merry 	softc->open_count++;
74986d45c7fSKenneth D. Merry 
750835187bfSScott Long 	cam_periph_unlock(periph);
75176babe50SJustin T. Gibbs 
75276babe50SJustin T. Gibbs 	return (error);
75376babe50SJustin T. Gibbs }
75476babe50SJustin T. Gibbs 
75576babe50SJustin T. Gibbs static int
75689c9c53dSPoul-Henning Kamp passclose(struct cdev *dev, int flag, int fmt, struct thread *td)
75776babe50SJustin T. Gibbs {
75876babe50SJustin T. Gibbs 	struct 	cam_periph *periph;
75986d45c7fSKenneth D. Merry 	struct  pass_softc *softc;
760227d67aaSAlexander Motin 	struct mtx *mtx;
76176babe50SJustin T. Gibbs 
762e2a5fdf9SNate Lawson 	periph = (struct cam_periph *)dev->si_drv1;
763227d67aaSAlexander Motin 	mtx = cam_periph_mtx(periph);
764227d67aaSAlexander Motin 	mtx_lock(mtx);
76576babe50SJustin T. Gibbs 
76686d45c7fSKenneth D. Merry 	softc = periph->softc;
76786d45c7fSKenneth D. Merry 	softc->open_count--;
76886d45c7fSKenneth D. Merry 
769a9934668SKenneth D. Merry 	if (softc->open_count == 0) {
770a9934668SKenneth D. Merry 		struct pass_io_req *io_req, *io_req2;
771a9934668SKenneth D. Merry 
772a9934668SKenneth D. Merry 		TAILQ_FOREACH_SAFE(io_req, &softc->done_queue, links, io_req2) {
773a9934668SKenneth D. Merry 			TAILQ_REMOVE(&softc->done_queue, io_req, links);
774a9934668SKenneth D. Merry 			passiocleanup(softc, io_req);
775a9934668SKenneth D. Merry 			uma_zfree(softc->pass_zone, io_req);
776a9934668SKenneth D. Merry 		}
777a9934668SKenneth D. Merry 
778a9934668SKenneth D. Merry 		TAILQ_FOREACH_SAFE(io_req, &softc->incoming_queue, links,
779a9934668SKenneth D. Merry 				   io_req2) {
780a9934668SKenneth D. Merry 			TAILQ_REMOVE(&softc->incoming_queue, io_req, links);
781a9934668SKenneth D. Merry 			passiocleanup(softc, io_req);
782a9934668SKenneth D. Merry 			uma_zfree(softc->pass_zone, io_req);
783a9934668SKenneth D. Merry 		}
784a9934668SKenneth D. Merry 
785a9934668SKenneth D. Merry 		/*
786a9934668SKenneth D. Merry 		 * If there are any active I/Os, we need to forcibly acquire a
787a9934668SKenneth D. Merry 		 * reference to the peripheral so that we don't go away
788a9934668SKenneth D. Merry 		 * before they complete.  We'll release the reference when
789a9934668SKenneth D. Merry 		 * the abandoned queue is empty.
790a9934668SKenneth D. Merry 		 */
791a9934668SKenneth D. Merry 		io_req = TAILQ_FIRST(&softc->active_queue);
792a9934668SKenneth D. Merry 		if ((io_req != NULL)
793a9934668SKenneth D. Merry 		 && (softc->flags & PASS_FLAG_ABANDONED_REF_SET) == 0) {
794a9934668SKenneth D. Merry 			cam_periph_doacquire(periph);
795a9934668SKenneth D. Merry 			softc->flags |= PASS_FLAG_ABANDONED_REF_SET;
796a9934668SKenneth D. Merry 		}
797a9934668SKenneth D. Merry 
798a9934668SKenneth D. Merry 		/*
799a9934668SKenneth D. Merry 		 * Since the I/O in the active queue is not under our
800a9934668SKenneth D. Merry 		 * control, just set a flag so that we can clean it up when
801a9934668SKenneth D. Merry 		 * it completes and put it on the abandoned queue.  This
802a9934668SKenneth D. Merry 		 * will prevent our sending spurious completions in the
803a9934668SKenneth D. Merry 		 * event that the device is opened again before these I/Os
804a9934668SKenneth D. Merry 		 * complete.
805a9934668SKenneth D. Merry 		 */
806a9934668SKenneth D. Merry 		TAILQ_FOREACH_SAFE(io_req, &softc->active_queue, links,
807a9934668SKenneth D. Merry 				   io_req2) {
808a9934668SKenneth D. Merry 			TAILQ_REMOVE(&softc->active_queue, io_req, links);
809a9934668SKenneth D. Merry 			io_req->flags |= PASS_IO_ABANDONED;
810a9934668SKenneth D. Merry 			TAILQ_INSERT_TAIL(&softc->abandoned_queue, io_req,
811a9934668SKenneth D. Merry 					  links);
812a9934668SKenneth D. Merry 		}
813a9934668SKenneth D. Merry 	}
814a9934668SKenneth D. Merry 
81586d45c7fSKenneth D. Merry 	cam_periph_release_locked(periph);
81686d45c7fSKenneth D. Merry 
81786d45c7fSKenneth D. Merry 	/*
818227d67aaSAlexander Motin 	 * We reference the lock directly here, instead of using
81986d45c7fSKenneth D. Merry 	 * cam_periph_unlock().  The reason is that the call to
82086d45c7fSKenneth D. Merry 	 * cam_periph_release_locked() above could result in the periph
82186d45c7fSKenneth D. Merry 	 * getting freed.  If that is the case, dereferencing the periph
82286d45c7fSKenneth D. Merry 	 * with a cam_periph_unlock() call would cause a page fault.
82386d45c7fSKenneth D. Merry 	 *
82486d45c7fSKenneth D. Merry 	 * cam_periph_release() avoids this problem using the same method,
82586d45c7fSKenneth D. Merry 	 * but we're manually acquiring and dropping the lock here to
82686d45c7fSKenneth D. Merry 	 * protect the open count and avoid another lock acquisition and
82786d45c7fSKenneth D. Merry 	 * release.
82886d45c7fSKenneth D. Merry 	 */
829227d67aaSAlexander Motin 	mtx_unlock(mtx);
83076babe50SJustin T. Gibbs 
83176babe50SJustin T. Gibbs 	return (0);
83276babe50SJustin T. Gibbs }
83376babe50SJustin T. Gibbs 
834a9934668SKenneth D. Merry static void
835a9934668SKenneth D. Merry passstart(struct cam_periph *periph, union ccb *start_ccb)
836a9934668SKenneth D. Merry {
837a9934668SKenneth D. Merry 	struct pass_softc *softc;
838a9934668SKenneth D. Merry 
839a9934668SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
840a9934668SKenneth D. Merry 
841a9934668SKenneth D. Merry 	switch (softc->state) {
842a9934668SKenneth D. Merry 	case PASS_STATE_NORMAL: {
843a9934668SKenneth D. Merry 		struct pass_io_req *io_req;
844a9934668SKenneth D. Merry 
845a9934668SKenneth D. Merry 		/*
846a9934668SKenneth D. Merry 		 * Check for any queued I/O requests that require an
847a9934668SKenneth D. Merry 		 * allocated slot.
848a9934668SKenneth D. Merry 		 */
849a9934668SKenneth D. Merry 		io_req = TAILQ_FIRST(&softc->incoming_queue);
850a9934668SKenneth D. Merry 		if (io_req == NULL) {
851a9934668SKenneth D. Merry 			xpt_release_ccb(start_ccb);
852a9934668SKenneth D. Merry 			break;
853a9934668SKenneth D. Merry 		}
854a9934668SKenneth D. Merry 		TAILQ_REMOVE(&softc->incoming_queue, io_req, links);
855a9934668SKenneth D. Merry 		TAILQ_INSERT_TAIL(&softc->active_queue, io_req, links);
856a9934668SKenneth D. Merry 		/*
857a9934668SKenneth D. Merry 		 * Merge the user's CCB into the allocated CCB.
858a9934668SKenneth D. Merry 		 */
859a9934668SKenneth D. Merry 		xpt_merge_ccb(start_ccb, &io_req->ccb);
860a9934668SKenneth D. Merry 		start_ccb->ccb_h.ccb_type = PASS_CCB_QUEUED_IO;
861a9934668SKenneth D. Merry 		start_ccb->ccb_h.ccb_ioreq = io_req;
862a9934668SKenneth D. Merry 		start_ccb->ccb_h.cbfcnp = passdone;
863a9934668SKenneth D. Merry 		io_req->alloced_ccb = start_ccb;
864a9934668SKenneth D. Merry 		binuptime(&io_req->start_time);
865a9934668SKenneth D. Merry 		devstat_start_transaction(softc->device_stats,
866a9934668SKenneth D. Merry 					  &io_req->start_time);
867a9934668SKenneth D. Merry 
868a9934668SKenneth D. Merry 		xpt_action(start_ccb);
869a9934668SKenneth D. Merry 
870a9934668SKenneth D. Merry 		/*
871a9934668SKenneth D. Merry 		 * If we have any more I/O waiting, schedule ourselves again.
872a9934668SKenneth D. Merry 		 */
873a9934668SKenneth D. Merry 		if (!TAILQ_EMPTY(&softc->incoming_queue))
874a9934668SKenneth D. Merry 			xpt_schedule(periph, CAM_PRIORITY_NORMAL);
875a9934668SKenneth D. Merry 		break;
876a9934668SKenneth D. Merry 	}
877a9934668SKenneth D. Merry 	default:
878a9934668SKenneth D. Merry 		break;
879a9934668SKenneth D. Merry 	}
880a9934668SKenneth D. Merry }
881a9934668SKenneth D. Merry 
882a9934668SKenneth D. Merry static void
883a9934668SKenneth D. Merry passdone(struct cam_periph *periph, union ccb *done_ccb)
884a9934668SKenneth D. Merry {
885a9934668SKenneth D. Merry 	struct pass_softc *softc;
886a9934668SKenneth D. Merry 	struct ccb_scsiio *csio;
887a9934668SKenneth D. Merry 
888a9934668SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
889a9934668SKenneth D. Merry 
890a9934668SKenneth D. Merry 	cam_periph_assert(periph, MA_OWNED);
891a9934668SKenneth D. Merry 
892a9934668SKenneth D. Merry 	csio = &done_ccb->csio;
893a9934668SKenneth D. Merry 	switch (csio->ccb_h.ccb_type) {
894a9934668SKenneth D. Merry 	case PASS_CCB_QUEUED_IO: {
895a9934668SKenneth D. Merry 		struct pass_io_req *io_req;
896a9934668SKenneth D. Merry 
897a9934668SKenneth D. Merry 		io_req = done_ccb->ccb_h.ccb_ioreq;
898a9934668SKenneth D. Merry #if 0
899a9934668SKenneth D. Merry 		xpt_print(periph->path, "%s: called for user CCB %p\n",
900a9934668SKenneth D. Merry 			  __func__, io_req->user_ccb_ptr);
901a9934668SKenneth D. Merry #endif
902a9934668SKenneth D. Merry 		if (((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
903a9934668SKenneth D. Merry 		 && (done_ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER)
904a9934668SKenneth D. Merry 		 && ((io_req->flags & PASS_IO_ABANDONED) == 0)) {
905a9934668SKenneth D. Merry 			int error;
906a9934668SKenneth D. Merry 
907a9934668SKenneth D. Merry 			error = passerror(done_ccb, CAM_RETRY_SELTO,
908a9934668SKenneth D. Merry 					  SF_RETRY_UA | SF_NO_PRINT);
909a9934668SKenneth D. Merry 
910a9934668SKenneth D. Merry 			if (error == ERESTART) {
911a9934668SKenneth D. Merry 				/*
912a9934668SKenneth D. Merry 				 * A retry was scheduled, so
913a9934668SKenneth D. Merry  				 * just return.
914a9934668SKenneth D. Merry 				 */
915a9934668SKenneth D. Merry 				return;
916a9934668SKenneth D. Merry 			}
917a9934668SKenneth D. Merry 		}
918a9934668SKenneth D. Merry 
919a9934668SKenneth D. Merry 		/*
920a9934668SKenneth D. Merry 		 * Copy the allocated CCB contents back to the malloced CCB
921a9934668SKenneth D. Merry 		 * so we can give status back to the user when he requests it.
922a9934668SKenneth D. Merry 		 */
923a9934668SKenneth D. Merry 		bcopy(done_ccb, &io_req->ccb, sizeof(*done_ccb));
924a9934668SKenneth D. Merry 
925a9934668SKenneth D. Merry 		/*
926a9934668SKenneth D. Merry 		 * Log data/transaction completion with devstat(9).
927a9934668SKenneth D. Merry 		 */
928a9934668SKenneth D. Merry 		switch (done_ccb->ccb_h.func_code) {
929a9934668SKenneth D. Merry 		case XPT_SCSI_IO:
930a9934668SKenneth D. Merry 			devstat_end_transaction(softc->device_stats,
931a9934668SKenneth D. Merry 			    done_ccb->csio.dxfer_len - done_ccb->csio.resid,
932a9934668SKenneth D. Merry 			    done_ccb->csio.tag_action & 0x3,
933a9934668SKenneth D. Merry 			    ((done_ccb->ccb_h.flags & CAM_DIR_MASK) ==
934a9934668SKenneth D. Merry 			    CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
935a9934668SKenneth D. Merry 			    (done_ccb->ccb_h.flags & CAM_DIR_OUT) ?
936a9934668SKenneth D. Merry 			    DEVSTAT_WRITE : DEVSTAT_READ, NULL,
937a9934668SKenneth D. Merry 			    &io_req->start_time);
938a9934668SKenneth D. Merry 			break;
939a9934668SKenneth D. Merry 		case XPT_ATA_IO:
940a9934668SKenneth D. Merry 			devstat_end_transaction(softc->device_stats,
941a9934668SKenneth D. Merry 			    done_ccb->ataio.dxfer_len - done_ccb->ataio.resid,
942e4cc6558SWarner Losh 			    0, /* Not used in ATA */
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_SMP_IO:
950a9934668SKenneth D. Merry 			/*
951a9934668SKenneth D. Merry 			 * XXX KDM this isn't quite right, but there isn't
952a9934668SKenneth D. Merry 			 * currently an easy way to represent a bidirectional
953a9934668SKenneth D. Merry 			 * transfer in devstat.  The only way to do it
954a9934668SKenneth D. Merry 			 * and have the byte counts come out right would
955a9934668SKenneth D. Merry 			 * mean that we would have to record two
956a9934668SKenneth D. Merry 			 * transactions, one for the request and one for the
957a9934668SKenneth D. Merry 			 * response.  For now, so that we report something,
958a9934668SKenneth D. Merry 			 * just treat the entire thing as a read.
959a9934668SKenneth D. Merry 			 */
960a9934668SKenneth D. Merry 			devstat_end_transaction(softc->device_stats,
961a9934668SKenneth D. Merry 			    done_ccb->smpio.smp_request_len +
962a9934668SKenneth D. Merry 			    done_ccb->smpio.smp_response_len,
963a9934668SKenneth D. Merry 			    DEVSTAT_TAG_SIMPLE, DEVSTAT_READ, NULL,
964a9934668SKenneth D. Merry 			    &io_req->start_time);
965a9934668SKenneth D. Merry 			break;
966a9934668SKenneth D. Merry 		default:
967a9934668SKenneth D. Merry 			devstat_end_transaction(softc->device_stats, 0,
968a9934668SKenneth D. Merry 			    DEVSTAT_TAG_NONE, DEVSTAT_NO_DATA, NULL,
969a9934668SKenneth D. Merry 			    &io_req->start_time);
970a9934668SKenneth D. Merry 			break;
971a9934668SKenneth D. Merry 		}
972a9934668SKenneth D. Merry 
973a9934668SKenneth D. Merry 		/*
974a9934668SKenneth D. Merry 		 * In the normal case, take the completed I/O off of the
975a9934668SKenneth D. Merry 		 * active queue and put it on the done queue.  Notitfy the
976a9934668SKenneth D. Merry 		 * user that we have a completed I/O.
977a9934668SKenneth D. Merry 		 */
978a9934668SKenneth D. Merry 		if ((io_req->flags & PASS_IO_ABANDONED) == 0) {
979a9934668SKenneth D. Merry 			TAILQ_REMOVE(&softc->active_queue, io_req, links);
980a9934668SKenneth D. Merry 			TAILQ_INSERT_TAIL(&softc->done_queue, io_req, links);
981a9934668SKenneth D. Merry 			selwakeuppri(&softc->read_select, PRIBIO);
982a9934668SKenneth D. Merry 			KNOTE_LOCKED(&softc->read_select.si_note, 0);
983a9934668SKenneth D. Merry 		} else {
984a9934668SKenneth D. Merry 			/*
985a9934668SKenneth D. Merry 			 * In the case of an abandoned I/O (final close
986a9934668SKenneth D. Merry 			 * without fetching the I/O), take it off of the
987a9934668SKenneth D. Merry 			 * abandoned queue and free it.
988a9934668SKenneth D. Merry 			 */
989a9934668SKenneth D. Merry 			TAILQ_REMOVE(&softc->abandoned_queue, io_req, links);
990a9934668SKenneth D. Merry 			passiocleanup(softc, io_req);
991a9934668SKenneth D. Merry 			uma_zfree(softc->pass_zone, io_req);
992a9934668SKenneth D. Merry 
993a9934668SKenneth D. Merry 			/*
994a9934668SKenneth D. Merry 			 * Release the done_ccb here, since we may wind up
995a9934668SKenneth D. Merry 			 * freeing the peripheral when we decrement the
996a9934668SKenneth D. Merry 			 * reference count below.
997a9934668SKenneth D. Merry 			 */
998a9934668SKenneth D. Merry 			xpt_release_ccb(done_ccb);
999a9934668SKenneth D. Merry 
1000a9934668SKenneth D. Merry 			/*
1001a9934668SKenneth D. Merry 			 * If the abandoned queue is empty, we can release
1002a9934668SKenneth D. Merry 			 * our reference to the periph since we won't have
1003a9934668SKenneth D. Merry 			 * any more completions coming.
1004a9934668SKenneth D. Merry 			 */
1005a9934668SKenneth D. Merry 			if ((TAILQ_EMPTY(&softc->abandoned_queue))
1006a9934668SKenneth D. Merry 			 && (softc->flags & PASS_FLAG_ABANDONED_REF_SET)) {
1007a9934668SKenneth D. Merry 				softc->flags &= ~PASS_FLAG_ABANDONED_REF_SET;
1008a9934668SKenneth D. Merry 				cam_periph_release_locked(periph);
1009a9934668SKenneth D. Merry 			}
1010a9934668SKenneth D. Merry 
1011a9934668SKenneth D. Merry 			/*
1012a9934668SKenneth D. Merry 			 * We have already released the CCB, so we can
1013a9934668SKenneth D. Merry 			 * return.
1014a9934668SKenneth D. Merry 			 */
1015a9934668SKenneth D. Merry 			return;
1016a9934668SKenneth D. Merry 		}
1017a9934668SKenneth D. Merry 		break;
1018a9934668SKenneth D. Merry 	}
1019a9934668SKenneth D. Merry 	}
1020a9934668SKenneth D. Merry 	xpt_release_ccb(done_ccb);
1021a9934668SKenneth D. Merry }
1022a9934668SKenneth D. Merry 
1023a9934668SKenneth D. Merry static int
1024a9934668SKenneth D. Merry passcreatezone(struct cam_periph *periph)
1025a9934668SKenneth D. Merry {
1026a9934668SKenneth D. Merry 	struct pass_softc *softc;
1027a9934668SKenneth D. Merry 	int error;
1028a9934668SKenneth D. Merry 
1029a9934668SKenneth D. Merry 	error = 0;
1030a9934668SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
1031a9934668SKenneth D. Merry 
1032a9934668SKenneth D. Merry 	cam_periph_assert(periph, MA_OWNED);
1033a9934668SKenneth D. Merry 	KASSERT(((softc->flags & PASS_FLAG_ZONE_VALID) == 0),
1034a9934668SKenneth D. Merry 		("%s called when the pass(4) zone is valid!\n", __func__));
1035a9934668SKenneth D. Merry 	KASSERT((softc->pass_zone == NULL),
1036a9934668SKenneth D. Merry 		("%s called when the pass(4) zone is allocated!\n", __func__));
1037a9934668SKenneth D. Merry 
1038a9934668SKenneth D. Merry 	if ((softc->flags & PASS_FLAG_ZONE_INPROG) == 0) {
1039a9934668SKenneth D. Merry 		/*
1040a9934668SKenneth D. Merry 		 * We're the first context through, so we need to create
1041a9934668SKenneth D. Merry 		 * the pass(4) UMA zone for I/O requests.
1042a9934668SKenneth D. Merry 		 */
1043a9934668SKenneth D. Merry 		softc->flags |= PASS_FLAG_ZONE_INPROG;
1044a9934668SKenneth D. Merry 
1045a9934668SKenneth D. Merry 		/*
1046a9934668SKenneth D. Merry 		 * uma_zcreate() does a blocking (M_WAITOK) allocation,
1047a9934668SKenneth D. Merry 		 * so we cannot hold a mutex while we call it.
1048a9934668SKenneth D. Merry 		 */
1049a9934668SKenneth D. Merry 		cam_periph_unlock(periph);
1050a9934668SKenneth D. Merry 
1051a9934668SKenneth D. Merry 		softc->pass_zone = uma_zcreate(softc->zone_name,
1052a9934668SKenneth D. Merry 		    sizeof(struct pass_io_req), NULL, NULL, NULL, NULL,
1053a9934668SKenneth D. Merry 		    /*align*/ 0, /*flags*/ 0);
1054a9934668SKenneth D. Merry 
1055a9934668SKenneth D. Merry 		softc->pass_io_zone = uma_zcreate(softc->io_zone_name,
1056a9934668SKenneth D. Merry 		    softc->io_zone_size, NULL, NULL, NULL, NULL,
1057a9934668SKenneth D. Merry 		    /*align*/ 0, /*flags*/ 0);
1058a9934668SKenneth D. Merry 
1059a9934668SKenneth D. Merry 		cam_periph_lock(periph);
1060a9934668SKenneth D. Merry 
1061a9934668SKenneth D. Merry 		if ((softc->pass_zone == NULL)
1062a9934668SKenneth D. Merry 		 || (softc->pass_io_zone == NULL)) {
1063a9934668SKenneth D. Merry 			if (softc->pass_zone == NULL)
1064a9934668SKenneth D. Merry 				xpt_print(periph->path, "unable to allocate "
1065a9934668SKenneth D. Merry 				    "IO Req UMA zone\n");
1066a9934668SKenneth D. Merry 			else
1067a9934668SKenneth D. Merry 				xpt_print(periph->path, "unable to allocate "
1068a9934668SKenneth D. Merry 				    "IO UMA zone\n");
1069a9934668SKenneth D. Merry 			softc->flags &= ~PASS_FLAG_ZONE_INPROG;
1070a9934668SKenneth D. Merry 			goto bailout;
1071a9934668SKenneth D. Merry 		}
1072a9934668SKenneth D. Merry 
1073a9934668SKenneth D. Merry 		/*
1074a9934668SKenneth D. Merry 		 * Set the flags appropriately and notify any other waiters.
1075a9934668SKenneth D. Merry 		 */
1076a9934668SKenneth D. Merry 		softc->flags &= PASS_FLAG_ZONE_INPROG;
1077a9934668SKenneth D. Merry 		softc->flags |= PASS_FLAG_ZONE_VALID;
1078a9934668SKenneth D. Merry 		wakeup(&softc->pass_zone);
1079a9934668SKenneth D. Merry 	} else {
1080a9934668SKenneth D. Merry 		/*
1081a9934668SKenneth D. Merry 		 * In this case, the UMA zone has not yet been created, but
1082a9934668SKenneth D. Merry 		 * another context is in the process of creating it.  We
1083a9934668SKenneth D. Merry 		 * need to sleep until the creation is either done or has
1084a9934668SKenneth D. Merry 		 * failed.
1085a9934668SKenneth D. Merry 		 */
1086a9934668SKenneth D. Merry 		while ((softc->flags & PASS_FLAG_ZONE_INPROG)
1087a9934668SKenneth D. Merry 		    && ((softc->flags & PASS_FLAG_ZONE_VALID) == 0)) {
1088a9934668SKenneth D. Merry 			error = msleep(&softc->pass_zone,
1089a9934668SKenneth D. Merry 				       cam_periph_mtx(periph), PRIBIO,
1090a9934668SKenneth D. Merry 				       "paszon", 0);
1091a9934668SKenneth D. Merry 			if (error != 0)
1092a9934668SKenneth D. Merry 				goto bailout;
1093a9934668SKenneth D. Merry 		}
1094a9934668SKenneth D. Merry 		/*
1095a9934668SKenneth D. Merry 		 * If the zone creation failed, no luck for the user.
1096a9934668SKenneth D. Merry 		 */
1097a9934668SKenneth D. Merry 		if ((softc->flags & PASS_FLAG_ZONE_VALID) == 0){
1098a9934668SKenneth D. Merry 			error = ENOMEM;
1099a9934668SKenneth D. Merry 			goto bailout;
1100a9934668SKenneth D. Merry 		}
1101a9934668SKenneth D. Merry 	}
1102a9934668SKenneth D. Merry bailout:
1103a9934668SKenneth D. Merry 	return (error);
1104a9934668SKenneth D. Merry }
1105a9934668SKenneth D. Merry 
1106a9934668SKenneth D. Merry static void
1107a9934668SKenneth D. Merry passiocleanup(struct pass_softc *softc, struct pass_io_req *io_req)
1108a9934668SKenneth D. Merry {
1109a9934668SKenneth D. Merry 	union ccb *ccb;
1110a9934668SKenneth D. Merry 	u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
1111a9934668SKenneth D. Merry 	int i, numbufs;
1112a9934668SKenneth D. Merry 
1113a9934668SKenneth D. Merry 	ccb = &io_req->ccb;
1114a9934668SKenneth D. Merry 
1115a9934668SKenneth D. Merry 	switch (ccb->ccb_h.func_code) {
1116a9934668SKenneth D. Merry 	case XPT_DEV_MATCH:
1117a9934668SKenneth D. Merry 		numbufs = min(io_req->num_bufs, 2);
1118a9934668SKenneth D. Merry 
1119a9934668SKenneth D. Merry 		if (numbufs == 1) {
1120a9934668SKenneth D. Merry 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
1121a9934668SKenneth D. Merry 		} else {
1122a9934668SKenneth D. Merry 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
1123a9934668SKenneth D. Merry 			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
1124a9934668SKenneth D. Merry 		}
1125a9934668SKenneth D. Merry 		break;
1126a9934668SKenneth D. Merry 	case XPT_SCSI_IO:
1127a9934668SKenneth D. Merry 	case XPT_CONT_TARGET_IO:
1128a9934668SKenneth D. Merry 		data_ptrs[0] = &ccb->csio.data_ptr;
1129a9934668SKenneth D. Merry 		numbufs = min(io_req->num_bufs, 1);
1130a9934668SKenneth D. Merry 		break;
1131a9934668SKenneth D. Merry 	case XPT_ATA_IO:
1132a9934668SKenneth D. Merry 		data_ptrs[0] = &ccb->ataio.data_ptr;
1133a9934668SKenneth D. Merry 		numbufs = min(io_req->num_bufs, 1);
1134a9934668SKenneth D. Merry 		break;
1135a9934668SKenneth D. Merry 	case XPT_SMP_IO:
1136a9934668SKenneth D. Merry 		numbufs = min(io_req->num_bufs, 2);
1137a9934668SKenneth D. Merry 		data_ptrs[0] = &ccb->smpio.smp_request;
1138a9934668SKenneth D. Merry 		data_ptrs[1] = &ccb->smpio.smp_response;
1139a9934668SKenneth D. Merry 		break;
1140a9934668SKenneth D. Merry 	case XPT_DEV_ADVINFO:
1141a9934668SKenneth D. Merry 		numbufs = min(io_req->num_bufs, 1);
1142a9934668SKenneth D. Merry 		data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
1143a9934668SKenneth D. Merry 		break;
1144df424515SWarner Losh 	case XPT_NVME_IO:
1145df424515SWarner Losh 	case XPT_NVME_ADMIN:
1146df424515SWarner Losh 		data_ptrs[0] = &ccb->nvmeio.data_ptr;
1147df424515SWarner Losh 		numbufs = min(io_req->num_bufs, 1);
1148df424515SWarner Losh 		break;
1149a9934668SKenneth D. Merry 	default:
1150a9934668SKenneth D. Merry 		/* allow ourselves to be swapped once again */
1151a9934668SKenneth D. Merry 		return;
1152a9934668SKenneth D. Merry 		break; /* NOTREACHED */
1153a9934668SKenneth D. Merry 	}
1154a9934668SKenneth D. Merry 
1155a9934668SKenneth D. Merry 	if (io_req->flags & PASS_IO_USER_SEG_MALLOC) {
1156a9934668SKenneth D. Merry 		free(io_req->user_segptr, M_SCSIPASS);
1157a9934668SKenneth D. Merry 		io_req->user_segptr = NULL;
1158a9934668SKenneth D. Merry 	}
1159a9934668SKenneth D. Merry 
1160a9934668SKenneth D. Merry 	/*
1161a9934668SKenneth D. Merry 	 * We only want to free memory we malloced.
1162a9934668SKenneth D. Merry 	 */
1163a9934668SKenneth D. Merry 	if (io_req->data_flags == CAM_DATA_VADDR) {
1164a9934668SKenneth D. Merry 		for (i = 0; i < io_req->num_bufs; i++) {
1165a9934668SKenneth D. Merry 			if (io_req->kern_bufs[i] == NULL)
1166a9934668SKenneth D. Merry 				continue;
1167a9934668SKenneth D. Merry 
1168a9934668SKenneth D. Merry 			free(io_req->kern_bufs[i], M_SCSIPASS);
1169a9934668SKenneth D. Merry 			io_req->kern_bufs[i] = NULL;
1170a9934668SKenneth D. Merry 		}
1171a9934668SKenneth D. Merry 	} else if (io_req->data_flags == CAM_DATA_SG) {
1172a9934668SKenneth D. Merry 		for (i = 0; i < io_req->num_kern_segs; i++) {
1173a9934668SKenneth D. Merry 			if ((uint8_t *)(uintptr_t)
1174a9934668SKenneth D. Merry 			    io_req->kern_segptr[i].ds_addr == NULL)
1175a9934668SKenneth D. Merry 				continue;
1176a9934668SKenneth D. Merry 
1177a9934668SKenneth D. Merry 			uma_zfree(softc->pass_io_zone, (uint8_t *)(uintptr_t)
1178a9934668SKenneth D. Merry 			    io_req->kern_segptr[i].ds_addr);
1179a9934668SKenneth D. Merry 			io_req->kern_segptr[i].ds_addr = 0;
1180a9934668SKenneth D. Merry 		}
1181a9934668SKenneth D. Merry 	}
1182a9934668SKenneth D. Merry 
1183a9934668SKenneth D. Merry 	if (io_req->flags & PASS_IO_KERN_SEG_MALLOC) {
1184a9934668SKenneth D. Merry 		free(io_req->kern_segptr, M_SCSIPASS);
1185a9934668SKenneth D. Merry 		io_req->kern_segptr = NULL;
1186a9934668SKenneth D. Merry 	}
1187a9934668SKenneth D. Merry 
1188a9934668SKenneth D. Merry 	if (io_req->data_flags != CAM_DATA_PADDR) {
1189a9934668SKenneth D. Merry 		for (i = 0; i < numbufs; i++) {
1190a9934668SKenneth D. Merry 			/*
1191a9934668SKenneth D. Merry 			 * Restore the user's buffer pointers to their
1192a9934668SKenneth D. Merry 			 * previous values.
1193a9934668SKenneth D. Merry 			 */
1194a9934668SKenneth D. Merry 			if (io_req->user_bufs[i] != NULL)
1195a9934668SKenneth D. Merry 				*data_ptrs[i] = io_req->user_bufs[i];
1196a9934668SKenneth D. Merry 		}
1197a9934668SKenneth D. Merry 	}
1198a9934668SKenneth D. Merry 
1199a9934668SKenneth D. Merry }
1200a9934668SKenneth D. Merry 
1201a9934668SKenneth D. Merry static int
1202a9934668SKenneth D. Merry passcopysglist(struct cam_periph *periph, struct pass_io_req *io_req,
1203a9934668SKenneth D. Merry 	       ccb_flags direction)
1204a9934668SKenneth D. Merry {
1205*6637b746SWarner Losh 	bus_size_t kern_watermark, user_watermark, len_to_copy;
1206a9934668SKenneth D. Merry 	bus_dma_segment_t *user_sglist, *kern_sglist;
1207a9934668SKenneth D. Merry 	int i, j, error;
1208a9934668SKenneth D. Merry 
1209a9934668SKenneth D. Merry 	error = 0;
1210a9934668SKenneth D. Merry 	kern_watermark = 0;
1211a9934668SKenneth D. Merry 	user_watermark = 0;
1212a9934668SKenneth D. Merry 	len_to_copy = 0;
1213a9934668SKenneth D. Merry 	user_sglist = io_req->user_segptr;
1214a9934668SKenneth D. Merry 	kern_sglist = io_req->kern_segptr;
1215a9934668SKenneth D. Merry 
1216a9934668SKenneth D. Merry 	for (i = 0, j = 0; i < io_req->num_user_segs &&
1217a9934668SKenneth D. Merry 	     j < io_req->num_kern_segs;) {
1218a9934668SKenneth D. Merry 		uint8_t *user_ptr, *kern_ptr;
1219a9934668SKenneth D. Merry 
1220a9934668SKenneth D. Merry 		len_to_copy = min(user_sglist[i].ds_len -user_watermark,
1221a9934668SKenneth D. Merry 		    kern_sglist[j].ds_len - kern_watermark);
1222a9934668SKenneth D. Merry 
1223a9934668SKenneth D. Merry 		user_ptr = (uint8_t *)(uintptr_t)user_sglist[i].ds_addr;
1224a9934668SKenneth D. Merry 		user_ptr = user_ptr + user_watermark;
1225a9934668SKenneth D. Merry 		kern_ptr = (uint8_t *)(uintptr_t)kern_sglist[j].ds_addr;
1226a9934668SKenneth D. Merry 		kern_ptr = kern_ptr + kern_watermark;
1227a9934668SKenneth D. Merry 
1228a9934668SKenneth D. Merry 		user_watermark += len_to_copy;
1229a9934668SKenneth D. Merry 		kern_watermark += len_to_copy;
1230a9934668SKenneth D. Merry 
1231a9934668SKenneth D. Merry 		if (direction == CAM_DIR_IN) {
1232a9934668SKenneth D. Merry 			error = copyout(kern_ptr, user_ptr, len_to_copy);
1233a9934668SKenneth D. Merry 			if (error != 0) {
1234a9934668SKenneth D. Merry 				xpt_print(periph->path, "%s: copyout of %u "
1235a9934668SKenneth D. Merry 					  "bytes from %p to %p failed with "
1236a9934668SKenneth D. Merry 					  "error %d\n", __func__, len_to_copy,
1237a9934668SKenneth D. Merry 					  kern_ptr, user_ptr, error);
1238a9934668SKenneth D. Merry 				goto bailout;
1239a9934668SKenneth D. Merry 			}
1240a9934668SKenneth D. Merry 		} else {
1241a9934668SKenneth D. Merry 			error = copyin(user_ptr, kern_ptr, len_to_copy);
1242a9934668SKenneth D. Merry 			if (error != 0) {
1243a9934668SKenneth D. Merry 				xpt_print(periph->path, "%s: copyin of %u "
1244a9934668SKenneth D. Merry 					  "bytes from %p to %p failed with "
1245a9934668SKenneth D. Merry 					  "error %d\n", __func__, len_to_copy,
1246a9934668SKenneth D. Merry 					  user_ptr, kern_ptr, error);
1247a9934668SKenneth D. Merry 				goto bailout;
1248a9934668SKenneth D. Merry 			}
1249a9934668SKenneth D. Merry 		}
1250a9934668SKenneth D. Merry 
1251a9934668SKenneth D. Merry 		if (user_sglist[i].ds_len == user_watermark) {
1252a9934668SKenneth D. Merry 			i++;
1253a9934668SKenneth D. Merry 			user_watermark = 0;
1254a9934668SKenneth D. Merry 		}
1255a9934668SKenneth D. Merry 
1256a9934668SKenneth D. Merry 		if (kern_sglist[j].ds_len == kern_watermark) {
1257a9934668SKenneth D. Merry 			j++;
1258a9934668SKenneth D. Merry 			kern_watermark = 0;
1259a9934668SKenneth D. Merry 		}
1260a9934668SKenneth D. Merry 	}
1261a9934668SKenneth D. Merry 
1262a9934668SKenneth D. Merry bailout:
1263a9934668SKenneth D. Merry 
1264a9934668SKenneth D. Merry 	return (error);
1265a9934668SKenneth D. Merry }
1266a9934668SKenneth D. Merry 
1267a9934668SKenneth D. Merry static int
1268a9934668SKenneth D. Merry passmemsetup(struct cam_periph *periph, struct pass_io_req *io_req)
1269a9934668SKenneth D. Merry {
1270a9934668SKenneth D. Merry 	union ccb *ccb;
1271a9934668SKenneth D. Merry 	struct pass_softc *softc;
1272a9934668SKenneth D. Merry 	int numbufs, i;
1273a9934668SKenneth D. Merry 	uint8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
1274a9934668SKenneth D. Merry 	uint32_t lengths[CAM_PERIPH_MAXMAPS];
1275a9934668SKenneth D. Merry 	uint32_t dirs[CAM_PERIPH_MAXMAPS];
1276a9934668SKenneth D. Merry 	uint32_t num_segs;
1277a9934668SKenneth D. Merry 	uint16_t *seg_cnt_ptr;
1278a9934668SKenneth D. Merry 	size_t maxmap;
1279a9934668SKenneth D. Merry 	int error;
1280a9934668SKenneth D. Merry 
1281a9934668SKenneth D. Merry 	cam_periph_assert(periph, MA_NOTOWNED);
1282a9934668SKenneth D. Merry 
1283a9934668SKenneth D. Merry 	softc = periph->softc;
1284a9934668SKenneth D. Merry 
1285a9934668SKenneth D. Merry 	error = 0;
1286a9934668SKenneth D. Merry 	ccb = &io_req->ccb;
1287a9934668SKenneth D. Merry 	maxmap = 0;
1288a9934668SKenneth D. Merry 	num_segs = 0;
1289a9934668SKenneth D. Merry 	seg_cnt_ptr = NULL;
1290a9934668SKenneth D. Merry 
1291a9934668SKenneth D. Merry 	switch(ccb->ccb_h.func_code) {
1292a9934668SKenneth D. Merry 	case XPT_DEV_MATCH:
1293a9934668SKenneth D. Merry 		if (ccb->cdm.match_buf_len == 0) {
1294a9934668SKenneth D. Merry 			printf("%s: invalid match buffer length 0\n", __func__);
1295a9934668SKenneth D. Merry 			return(EINVAL);
1296a9934668SKenneth D. Merry 		}
1297a9934668SKenneth D. Merry 		if (ccb->cdm.pattern_buf_len > 0) {
1298a9934668SKenneth D. Merry 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
1299a9934668SKenneth D. Merry 			lengths[0] = ccb->cdm.pattern_buf_len;
1300a9934668SKenneth D. Merry 			dirs[0] = CAM_DIR_OUT;
1301a9934668SKenneth D. Merry 			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
1302a9934668SKenneth D. Merry 			lengths[1] = ccb->cdm.match_buf_len;
1303a9934668SKenneth D. Merry 			dirs[1] = CAM_DIR_IN;
1304a9934668SKenneth D. Merry 			numbufs = 2;
1305a9934668SKenneth D. Merry 		} else {
1306a9934668SKenneth D. Merry 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
1307a9934668SKenneth D. Merry 			lengths[0] = ccb->cdm.match_buf_len;
1308a9934668SKenneth D. Merry 			dirs[0] = CAM_DIR_IN;
1309a9934668SKenneth D. Merry 			numbufs = 1;
1310a9934668SKenneth D. Merry 		}
1311a9934668SKenneth D. Merry 		io_req->data_flags = CAM_DATA_VADDR;
1312a9934668SKenneth D. Merry 		break;
1313a9934668SKenneth D. Merry 	case XPT_SCSI_IO:
1314a9934668SKenneth D. Merry 	case XPT_CONT_TARGET_IO:
1315a9934668SKenneth D. Merry 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
1316a9934668SKenneth D. Merry 			return(0);
1317a9934668SKenneth D. Merry 
1318a9934668SKenneth D. Merry 		/*
1319a9934668SKenneth D. Merry 		 * The user shouldn't be able to supply a bio.
1320a9934668SKenneth D. Merry 		 */
1321a9934668SKenneth D. Merry 		if ((ccb->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_BIO)
1322a9934668SKenneth D. Merry 			return (EINVAL);
1323a9934668SKenneth D. Merry 
1324a9934668SKenneth D. Merry 		io_req->data_flags = ccb->ccb_h.flags & CAM_DATA_MASK;
1325a9934668SKenneth D. Merry 
1326a9934668SKenneth D. Merry 		data_ptrs[0] = &ccb->csio.data_ptr;
1327a9934668SKenneth D. Merry 		lengths[0] = ccb->csio.dxfer_len;
1328a9934668SKenneth D. Merry 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
1329a9934668SKenneth D. Merry 		num_segs = ccb->csio.sglist_cnt;
1330a9934668SKenneth D. Merry 		seg_cnt_ptr = &ccb->csio.sglist_cnt;
1331a9934668SKenneth D. Merry 		numbufs = 1;
1332a9934668SKenneth D. Merry 		maxmap = softc->maxio;
1333a9934668SKenneth D. Merry 		break;
1334a9934668SKenneth D. Merry 	case XPT_ATA_IO:
1335a9934668SKenneth D. Merry 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
1336a9934668SKenneth D. Merry 			return(0);
1337a9934668SKenneth D. Merry 
1338a9934668SKenneth D. Merry 		/*
1339a9934668SKenneth D. Merry 		 * We only support a single virtual address for ATA I/O.
1340a9934668SKenneth D. Merry 		 */
1341a9934668SKenneth D. Merry 		if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
1342a9934668SKenneth D. Merry 			return (EINVAL);
1343a9934668SKenneth D. Merry 
1344a9934668SKenneth D. Merry 		io_req->data_flags = CAM_DATA_VADDR;
1345a9934668SKenneth D. Merry 
1346a9934668SKenneth D. Merry 		data_ptrs[0] = &ccb->ataio.data_ptr;
1347a9934668SKenneth D. Merry 		lengths[0] = ccb->ataio.dxfer_len;
1348a9934668SKenneth D. Merry 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
1349a9934668SKenneth D. Merry 		numbufs = 1;
1350a9934668SKenneth D. Merry 		maxmap = softc->maxio;
1351a9934668SKenneth D. Merry 		break;
1352a9934668SKenneth D. Merry 	case XPT_SMP_IO:
1353a9934668SKenneth D. Merry 		io_req->data_flags = CAM_DATA_VADDR;
1354a9934668SKenneth D. Merry 
1355a9934668SKenneth D. Merry 		data_ptrs[0] = &ccb->smpio.smp_request;
1356a9934668SKenneth D. Merry 		lengths[0] = ccb->smpio.smp_request_len;
1357a9934668SKenneth D. Merry 		dirs[0] = CAM_DIR_OUT;
1358a9934668SKenneth D. Merry 		data_ptrs[1] = &ccb->smpio.smp_response;
1359a9934668SKenneth D. Merry 		lengths[1] = ccb->smpio.smp_response_len;
1360a9934668SKenneth D. Merry 		dirs[1] = CAM_DIR_IN;
1361a9934668SKenneth D. Merry 		numbufs = 2;
1362a9934668SKenneth D. Merry 		maxmap = softc->maxio;
1363a9934668SKenneth D. Merry 		break;
1364a9934668SKenneth D. Merry 	case XPT_DEV_ADVINFO:
1365a9934668SKenneth D. Merry 		if (ccb->cdai.bufsiz == 0)
1366a9934668SKenneth D. Merry 			return (0);
1367a9934668SKenneth D. Merry 
1368a9934668SKenneth D. Merry 		io_req->data_flags = CAM_DATA_VADDR;
1369a9934668SKenneth D. Merry 
1370a9934668SKenneth D. Merry 		data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
1371a9934668SKenneth D. Merry 		lengths[0] = ccb->cdai.bufsiz;
1372a9934668SKenneth D. Merry 		dirs[0] = CAM_DIR_IN;
1373a9934668SKenneth D. Merry 		numbufs = 1;
1374a9934668SKenneth D. Merry 		break;
1375df424515SWarner Losh 	case XPT_NVME_ADMIN:
1376df424515SWarner Losh 	case XPT_NVME_IO:
1377df424515SWarner Losh 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
1378df424515SWarner Losh 			return (0);
1379df424515SWarner Losh 
1380df424515SWarner Losh 		io_req->data_flags = ccb->ccb_h.flags & CAM_DATA_MASK;
1381df424515SWarner Losh 
1382df424515SWarner Losh 		data_ptrs[0] = &ccb->nvmeio.data_ptr;
1383df424515SWarner Losh 		lengths[0] = ccb->nvmeio.dxfer_len;
1384df424515SWarner Losh 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
138551977281SWarner Losh 		num_segs = ccb->nvmeio.sglist_cnt;
138651977281SWarner Losh 		seg_cnt_ptr = &ccb->nvmeio.sglist_cnt;
1387df424515SWarner Losh 		numbufs = 1;
1388df424515SWarner Losh 		maxmap = softc->maxio;
1389df424515SWarner Losh 		break;
1390a9934668SKenneth D. Merry 	default:
1391a9934668SKenneth D. Merry 		return(EINVAL);
1392a9934668SKenneth D. Merry 		break; /* NOTREACHED */
1393a9934668SKenneth D. Merry 	}
1394a9934668SKenneth D. Merry 
1395a9934668SKenneth D. Merry 	io_req->num_bufs = numbufs;
1396a9934668SKenneth D. Merry 
1397a9934668SKenneth D. Merry 	/*
1398a9934668SKenneth D. Merry 	 * If there is a maximum, check to make sure that the user's
1399a9934668SKenneth D. Merry 	 * request fits within the limit.  In general, we should only have
1400a9934668SKenneth D. Merry 	 * a maximum length for requests that go to hardware.  Otherwise it
1401a9934668SKenneth D. Merry 	 * is whatever we're able to malloc.
1402a9934668SKenneth D. Merry 	 */
1403a9934668SKenneth D. Merry 	for (i = 0; i < numbufs; i++) {
1404a9934668SKenneth D. Merry 		io_req->user_bufs[i] = *data_ptrs[i];
1405a9934668SKenneth D. Merry 		io_req->dirs[i] = dirs[i];
1406a9934668SKenneth D. Merry 		io_req->lengths[i] = lengths[i];
1407a9934668SKenneth D. Merry 
1408a9934668SKenneth D. Merry 		if (maxmap == 0)
1409a9934668SKenneth D. Merry 			continue;
1410a9934668SKenneth D. Merry 
1411a9934668SKenneth D. Merry 		if (lengths[i] <= maxmap)
1412a9934668SKenneth D. Merry 			continue;
1413a9934668SKenneth D. Merry 
1414a9934668SKenneth D. Merry 		xpt_print(periph->path, "%s: data length %u > max allowed %u "
1415a9934668SKenneth D. Merry 			  "bytes\n", __func__, lengths[i], maxmap);
1416a9934668SKenneth D. Merry 		error = EINVAL;
1417a9934668SKenneth D. Merry 		goto bailout;
1418a9934668SKenneth D. Merry 	}
1419a9934668SKenneth D. Merry 
1420a9934668SKenneth D. Merry 	switch (io_req->data_flags) {
1421a9934668SKenneth D. Merry 	case CAM_DATA_VADDR:
1422a9934668SKenneth D. Merry 		/* Map or copy the buffer into kernel address space */
1423a9934668SKenneth D. Merry 		for (i = 0; i < numbufs; i++) {
1424a9934668SKenneth D. Merry 			uint8_t *tmp_buf;
1425a9934668SKenneth D. Merry 
1426a9934668SKenneth D. Merry 			/*
1427a9934668SKenneth D. Merry 			 * If for some reason no length is specified, we
1428a9934668SKenneth D. Merry 			 * don't need to allocate anything.
1429a9934668SKenneth D. Merry 			 */
1430a9934668SKenneth D. Merry 			if (io_req->lengths[i] == 0)
1431a9934668SKenneth D. Merry 				continue;
1432a9934668SKenneth D. Merry 
1433a9934668SKenneth D. Merry 			tmp_buf = malloc(lengths[i], M_SCSIPASS,
1434a9934668SKenneth D. Merry 					 M_WAITOK | M_ZERO);
1435a9934668SKenneth D. Merry 			io_req->kern_bufs[i] = tmp_buf;
1436a9934668SKenneth D. Merry 			*data_ptrs[i] = tmp_buf;
1437a9934668SKenneth D. Merry 
1438a9934668SKenneth D. Merry #if 0
1439a9934668SKenneth D. Merry 			xpt_print(periph->path, "%s: malloced %p len %u, user "
1440a9934668SKenneth D. Merry 				  "buffer %p, operation: %s\n", __func__,
1441a9934668SKenneth D. Merry 				  tmp_buf, lengths[i], io_req->user_bufs[i],
1442a9934668SKenneth D. Merry 				  (dirs[i] == CAM_DIR_IN) ? "read" : "write");
1443a9934668SKenneth D. Merry #endif
1444a9934668SKenneth D. Merry 			/*
1445a9934668SKenneth D. Merry 			 * We only need to copy in if the user is writing.
1446a9934668SKenneth D. Merry 			 */
1447a9934668SKenneth D. Merry 			if (dirs[i] != CAM_DIR_OUT)
1448a9934668SKenneth D. Merry 				continue;
1449a9934668SKenneth D. Merry 
1450a9934668SKenneth D. Merry 			error = copyin(io_req->user_bufs[i],
1451a9934668SKenneth D. Merry 				       io_req->kern_bufs[i], lengths[i]);
1452a9934668SKenneth D. Merry 			if (error != 0) {
1453a9934668SKenneth D. Merry 				xpt_print(periph->path, "%s: copy of user "
1454a9934668SKenneth D. Merry 					  "buffer from %p to %p failed with "
1455a9934668SKenneth D. Merry 					  "error %d\n", __func__,
1456a9934668SKenneth D. Merry 					  io_req->user_bufs[i],
1457a9934668SKenneth D. Merry 					  io_req->kern_bufs[i], error);
1458a9934668SKenneth D. Merry 				goto bailout;
1459a9934668SKenneth D. Merry 			}
1460a9934668SKenneth D. Merry 		}
1461a9934668SKenneth D. Merry 		break;
1462a9934668SKenneth D. Merry 	case CAM_DATA_PADDR:
1463a9934668SKenneth D. Merry 		/* Pass down the pointer as-is */
1464a9934668SKenneth D. Merry 		break;
1465a9934668SKenneth D. Merry 	case CAM_DATA_SG: {
1466a9934668SKenneth D. Merry 		size_t sg_length, size_to_go, alloc_size;
1467a9934668SKenneth D. Merry 		uint32_t num_segs_needed;
1468a9934668SKenneth D. Merry 
1469a9934668SKenneth D. Merry 		/*
1470a9934668SKenneth D. Merry 		 * Copy the user S/G list in, and then copy in the
1471a9934668SKenneth D. Merry 		 * individual segments.
1472a9934668SKenneth D. Merry 		 */
1473a9934668SKenneth D. Merry 		/*
1474a9934668SKenneth D. Merry 		 * We shouldn't see this, but check just in case.
1475a9934668SKenneth D. Merry 		 */
1476a9934668SKenneth D. Merry 		if (numbufs != 1) {
1477a9934668SKenneth D. Merry 			xpt_print(periph->path, "%s: cannot currently handle "
1478a9934668SKenneth D. Merry 				  "more than one S/G list per CCB\n", __func__);
1479a9934668SKenneth D. Merry 			error = EINVAL;
1480a9934668SKenneth D. Merry 			goto bailout;
1481a9934668SKenneth D. Merry 		}
1482a9934668SKenneth D. Merry 
1483a9934668SKenneth D. Merry 		/*
1484a9934668SKenneth D. Merry 		 * We have to have at least one segment.
1485a9934668SKenneth D. Merry 		 */
1486a9934668SKenneth D. Merry 		if (num_segs == 0) {
1487a9934668SKenneth D. Merry 			xpt_print(periph->path, "%s: CAM_DATA_SG flag set, "
1488a9934668SKenneth D. Merry 				  "but sglist_cnt=0!\n", __func__);
1489a9934668SKenneth D. Merry 			error = EINVAL;
1490a9934668SKenneth D. Merry 			goto bailout;
1491a9934668SKenneth D. Merry 		}
1492a9934668SKenneth D. Merry 
1493a9934668SKenneth D. Merry 		/*
1494a9934668SKenneth D. Merry 		 * Make sure the user specified the total length and didn't
1495a9934668SKenneth D. Merry 		 * just leave it to us to decode the S/G list.
1496a9934668SKenneth D. Merry 		 */
1497a9934668SKenneth D. Merry 		if (lengths[0] == 0) {
1498a9934668SKenneth D. Merry 			xpt_print(periph->path, "%s: no dxfer_len specified, "
1499a9934668SKenneth D. Merry 				  "but CAM_DATA_SG flag is set!\n", __func__);
1500a9934668SKenneth D. Merry 			error = EINVAL;
1501a9934668SKenneth D. Merry 			goto bailout;
1502a9934668SKenneth D. Merry 		}
1503a9934668SKenneth D. Merry 
1504a9934668SKenneth D. Merry 		/*
1505a9934668SKenneth D. Merry 		 * We allocate buffers in io_zone_size increments for an
1506cd853791SKonstantin Belousov 		 * S/G list.  This will generally be maxphys.
1507a9934668SKenneth D. Merry 		 */
1508a9934668SKenneth D. Merry 		if (lengths[0] <= softc->io_zone_size)
1509a9934668SKenneth D. Merry 			num_segs_needed = 1;
1510a9934668SKenneth D. Merry 		else {
1511a9934668SKenneth D. Merry 			num_segs_needed = lengths[0] / softc->io_zone_size;
1512a9934668SKenneth D. Merry 			if ((lengths[0] % softc->io_zone_size) != 0)
1513a9934668SKenneth D. Merry 				num_segs_needed++;
1514a9934668SKenneth D. Merry 		}
1515a9934668SKenneth D. Merry 
1516a9934668SKenneth D. Merry 		/* Figure out the size of the S/G list */
1517a9934668SKenneth D. Merry 		sg_length = num_segs * sizeof(bus_dma_segment_t);
1518a9934668SKenneth D. Merry 		io_req->num_user_segs = num_segs;
1519a9934668SKenneth D. Merry 		io_req->num_kern_segs = num_segs_needed;
1520a9934668SKenneth D. Merry 
1521a9934668SKenneth D. Merry 		/* Save the user's S/G list pointer for later restoration */
1522a9934668SKenneth D. Merry 		io_req->user_bufs[0] = *data_ptrs[0];
1523a9934668SKenneth D. Merry 
1524a9934668SKenneth D. Merry 		/*
1525a9934668SKenneth D. Merry 		 * If we have enough segments allocated by default to handle
1526a9934668SKenneth D. Merry 		 * the length of the user's S/G list,
1527a9934668SKenneth D. Merry 		 */
1528a9934668SKenneth D. Merry 		if (num_segs > PASS_MAX_SEGS) {
1529a9934668SKenneth D. Merry 			io_req->user_segptr = malloc(sizeof(bus_dma_segment_t) *
1530a9934668SKenneth D. Merry 			    num_segs, M_SCSIPASS, M_WAITOK | M_ZERO);
1531a9934668SKenneth D. Merry 			io_req->flags |= PASS_IO_USER_SEG_MALLOC;
1532a9934668SKenneth D. Merry 		} else
1533a9934668SKenneth D. Merry 			io_req->user_segptr = io_req->user_segs;
1534a9934668SKenneth D. Merry 
1535a9934668SKenneth D. Merry 		error = copyin(*data_ptrs[0], io_req->user_segptr, sg_length);
1536a9934668SKenneth D. Merry 		if (error != 0) {
1537a9934668SKenneth D. Merry 			xpt_print(periph->path, "%s: copy of user S/G list "
1538a9934668SKenneth D. Merry 				  "from %p to %p failed with error %d\n",
1539a9934668SKenneth D. Merry 				  __func__, *data_ptrs[0], io_req->user_segptr,
1540a9934668SKenneth D. Merry 				  error);
1541a9934668SKenneth D. Merry 			goto bailout;
1542a9934668SKenneth D. Merry 		}
1543a9934668SKenneth D. Merry 
1544a9934668SKenneth D. Merry 		if (num_segs_needed > PASS_MAX_SEGS) {
1545a9934668SKenneth D. Merry 			io_req->kern_segptr = malloc(sizeof(bus_dma_segment_t) *
1546a9934668SKenneth D. Merry 			    num_segs_needed, M_SCSIPASS, M_WAITOK | M_ZERO);
1547a9934668SKenneth D. Merry 			io_req->flags |= PASS_IO_KERN_SEG_MALLOC;
1548a9934668SKenneth D. Merry 		} else {
1549a9934668SKenneth D. Merry 			io_req->kern_segptr = io_req->kern_segs;
1550a9934668SKenneth D. Merry 		}
1551a9934668SKenneth D. Merry 
1552a9934668SKenneth D. Merry 		/*
1553a9934668SKenneth D. Merry 		 * Allocate the kernel S/G list.
1554a9934668SKenneth D. Merry 		 */
1555a9934668SKenneth D. Merry 		for (size_to_go = lengths[0], i = 0;
1556a9934668SKenneth D. Merry 		     size_to_go > 0 && i < num_segs_needed;
1557a9934668SKenneth D. Merry 		     i++, size_to_go -= alloc_size) {
1558a9934668SKenneth D. Merry 			uint8_t *kern_ptr;
1559a9934668SKenneth D. Merry 
1560a9934668SKenneth D. Merry 			alloc_size = min(size_to_go, softc->io_zone_size);
1561a9934668SKenneth D. Merry 			kern_ptr = uma_zalloc(softc->pass_io_zone, M_WAITOK);
1562a9934668SKenneth D. Merry 			io_req->kern_segptr[i].ds_addr =
1563a9934668SKenneth D. Merry 			    (bus_addr_t)(uintptr_t)kern_ptr;
1564a9934668SKenneth D. Merry 			io_req->kern_segptr[i].ds_len = alloc_size;
1565a9934668SKenneth D. Merry 		}
1566a9934668SKenneth D. Merry 		if (size_to_go > 0) {
1567a9934668SKenneth D. Merry 			printf("%s: size_to_go = %zu, software error!\n",
1568a9934668SKenneth D. Merry 			       __func__, size_to_go);
1569a9934668SKenneth D. Merry 			error = EINVAL;
1570a9934668SKenneth D. Merry 			goto bailout;
1571a9934668SKenneth D. Merry 		}
1572a9934668SKenneth D. Merry 
1573a9934668SKenneth D. Merry 		*data_ptrs[0] = (uint8_t *)io_req->kern_segptr;
1574a9934668SKenneth D. Merry 		*seg_cnt_ptr = io_req->num_kern_segs;
1575a9934668SKenneth D. Merry 
1576a9934668SKenneth D. Merry 		/*
1577a9934668SKenneth D. Merry 		 * We only need to copy data here if the user is writing.
1578a9934668SKenneth D. Merry 		 */
1579a9934668SKenneth D. Merry 		if (dirs[0] == CAM_DIR_OUT)
1580a9934668SKenneth D. Merry 			error = passcopysglist(periph, io_req, dirs[0]);
1581a9934668SKenneth D. Merry 		break;
1582a9934668SKenneth D. Merry 	}
1583a9934668SKenneth D. Merry 	case CAM_DATA_SG_PADDR: {
1584a9934668SKenneth D. Merry 		size_t sg_length;
1585a9934668SKenneth D. Merry 
1586a9934668SKenneth D. Merry 		/*
1587a9934668SKenneth D. Merry 		 * We shouldn't see this, but check just in case.
1588a9934668SKenneth D. Merry 		 */
1589a9934668SKenneth D. Merry 		if (numbufs != 1) {
1590a9934668SKenneth D. Merry 			printf("%s: cannot currently handle more than one "
1591a9934668SKenneth D. Merry 			       "S/G list per CCB\n", __func__);
1592a9934668SKenneth D. Merry 			error = EINVAL;
1593a9934668SKenneth D. Merry 			goto bailout;
1594a9934668SKenneth D. Merry 		}
1595a9934668SKenneth D. Merry 
1596a9934668SKenneth D. Merry 		/*
1597a9934668SKenneth D. Merry 		 * We have to have at least one segment.
1598a9934668SKenneth D. Merry 		 */
1599a9934668SKenneth D. Merry 		if (num_segs == 0) {
1600a9934668SKenneth D. Merry 			xpt_print(periph->path, "%s: CAM_DATA_SG_PADDR flag "
1601a9934668SKenneth D. Merry 				  "set, but sglist_cnt=0!\n", __func__);
1602a9934668SKenneth D. Merry 			error = EINVAL;
1603a9934668SKenneth D. Merry 			goto bailout;
1604a9934668SKenneth D. Merry 		}
1605a9934668SKenneth D. Merry 
1606a9934668SKenneth D. Merry 		/*
1607a9934668SKenneth D. Merry 		 * Make sure the user specified the total length and didn't
1608a9934668SKenneth D. Merry 		 * just leave it to us to decode the S/G list.
1609a9934668SKenneth D. Merry 		 */
1610a9934668SKenneth D. Merry 		if (lengths[0] == 0) {
1611a9934668SKenneth D. Merry 			xpt_print(periph->path, "%s: no dxfer_len specified, "
1612a9934668SKenneth D. Merry 				  "but CAM_DATA_SG flag is set!\n", __func__);
1613a9934668SKenneth D. Merry 			error = EINVAL;
1614a9934668SKenneth D. Merry 			goto bailout;
1615a9934668SKenneth D. Merry 		}
1616a9934668SKenneth D. Merry 
1617a9934668SKenneth D. Merry 		/* Figure out the size of the S/G list */
1618a9934668SKenneth D. Merry 		sg_length = num_segs * sizeof(bus_dma_segment_t);
1619a9934668SKenneth D. Merry 		io_req->num_user_segs = num_segs;
1620a9934668SKenneth D. Merry 		io_req->num_kern_segs = io_req->num_user_segs;
1621a9934668SKenneth D. Merry 
1622a9934668SKenneth D. Merry 		/* Save the user's S/G list pointer for later restoration */
1623a9934668SKenneth D. Merry 		io_req->user_bufs[0] = *data_ptrs[0];
1624a9934668SKenneth D. Merry 
1625a9934668SKenneth D. Merry 		if (num_segs > PASS_MAX_SEGS) {
1626a9934668SKenneth D. Merry 			io_req->user_segptr = malloc(sizeof(bus_dma_segment_t) *
1627a9934668SKenneth D. Merry 			    num_segs, M_SCSIPASS, M_WAITOK | M_ZERO);
1628a9934668SKenneth D. Merry 			io_req->flags |= PASS_IO_USER_SEG_MALLOC;
1629a9934668SKenneth D. Merry 		} else
1630a9934668SKenneth D. Merry 			io_req->user_segptr = io_req->user_segs;
1631a9934668SKenneth D. Merry 
1632a9934668SKenneth D. Merry 		io_req->kern_segptr = io_req->user_segptr;
1633a9934668SKenneth D. Merry 
1634a9934668SKenneth D. Merry 		error = copyin(*data_ptrs[0], io_req->user_segptr, sg_length);
1635a9934668SKenneth D. Merry 		if (error != 0) {
1636a9934668SKenneth D. Merry 			xpt_print(periph->path, "%s: copy of user S/G list "
1637a9934668SKenneth D. Merry 				  "from %p to %p failed with error %d\n",
1638a9934668SKenneth D. Merry 				  __func__, *data_ptrs[0], io_req->user_segptr,
1639a9934668SKenneth D. Merry 				  error);
1640a9934668SKenneth D. Merry 			goto bailout;
1641a9934668SKenneth D. Merry 		}
1642a9934668SKenneth D. Merry 		break;
1643a9934668SKenneth D. Merry 	}
1644a9934668SKenneth D. Merry 	default:
1645a9934668SKenneth D. Merry 	case CAM_DATA_BIO:
1646a9934668SKenneth D. Merry 		/*
1647a9934668SKenneth D. Merry 		 * A user shouldn't be attaching a bio to the CCB.  It
1648a9934668SKenneth D. Merry 		 * isn't a user-accessible structure.
1649a9934668SKenneth D. Merry 		 */
1650a9934668SKenneth D. Merry 		error = EINVAL;
1651a9934668SKenneth D. Merry 		break;
1652a9934668SKenneth D. Merry 	}
1653a9934668SKenneth D. Merry 
1654a9934668SKenneth D. Merry bailout:
1655a9934668SKenneth D. Merry 	if (error != 0)
1656a9934668SKenneth D. Merry 		passiocleanup(softc, io_req);
1657a9934668SKenneth D. Merry 
1658a9934668SKenneth D. Merry 	return (error);
1659a9934668SKenneth D. Merry }
1660a9934668SKenneth D. Merry 
1661a9934668SKenneth D. Merry static int
1662a9934668SKenneth D. Merry passmemdone(struct cam_periph *periph, struct pass_io_req *io_req)
1663a9934668SKenneth D. Merry {
1664a9934668SKenneth D. Merry 	struct pass_softc *softc;
1665a9934668SKenneth D. Merry 	int error;
1666a9934668SKenneth D. Merry 	int i;
1667a9934668SKenneth D. Merry 
1668a9934668SKenneth D. Merry 	error = 0;
1669a9934668SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
1670a9934668SKenneth D. Merry 
1671a9934668SKenneth D. Merry 	switch (io_req->data_flags) {
1672a9934668SKenneth D. Merry 	case CAM_DATA_VADDR:
1673a9934668SKenneth D. Merry 		/*
1674a9934668SKenneth D. Merry 		 * Copy back to the user buffer if this was a read.
1675a9934668SKenneth D. Merry 		 */
1676a9934668SKenneth D. Merry 		for (i = 0; i < io_req->num_bufs; i++) {
1677a9934668SKenneth D. Merry 			if (io_req->dirs[i] != CAM_DIR_IN)
1678a9934668SKenneth D. Merry 				continue;
1679a9934668SKenneth D. Merry 
1680a9934668SKenneth D. Merry 			error = copyout(io_req->kern_bufs[i],
1681a9934668SKenneth D. Merry 			    io_req->user_bufs[i], io_req->lengths[i]);
1682a9934668SKenneth D. Merry 			if (error != 0) {
1683a9934668SKenneth D. Merry 				xpt_print(periph->path, "Unable to copy %u "
1684a9934668SKenneth D. Merry 					  "bytes from %p to user address %p\n",
1685a9934668SKenneth D. Merry 					  io_req->lengths[i],
1686a9934668SKenneth D. Merry 					  io_req->kern_bufs[i],
1687a9934668SKenneth D. Merry 					  io_req->user_bufs[i]);
1688a9934668SKenneth D. Merry 				goto bailout;
1689a9934668SKenneth D. Merry 			}
1690a9934668SKenneth D. Merry 		}
1691a9934668SKenneth D. Merry 		break;
1692a9934668SKenneth D. Merry 	case CAM_DATA_PADDR:
1693a9934668SKenneth D. Merry 		/* Do nothing.  The pointer is a physical address already */
1694a9934668SKenneth D. Merry 		break;
1695a9934668SKenneth D. Merry 	case CAM_DATA_SG:
1696a9934668SKenneth D. Merry 		/*
1697a9934668SKenneth D. Merry 		 * Copy back to the user buffer if this was a read.
1698a9934668SKenneth D. Merry 		 * Restore the user's S/G list buffer pointer.
1699a9934668SKenneth D. Merry 		 */
1700a9934668SKenneth D. Merry 		if (io_req->dirs[0] == CAM_DIR_IN)
1701a9934668SKenneth D. Merry 			error = passcopysglist(periph, io_req, io_req->dirs[0]);
1702a9934668SKenneth D. Merry 		break;
1703a9934668SKenneth D. Merry 	case CAM_DATA_SG_PADDR:
1704a9934668SKenneth D. Merry 		/*
1705a9934668SKenneth D. Merry 		 * Restore the user's S/G list buffer pointer.  No need to
1706a9934668SKenneth D. Merry 		 * copy.
1707a9934668SKenneth D. Merry 		 */
1708a9934668SKenneth D. Merry 		break;
1709a9934668SKenneth D. Merry 	default:
1710a9934668SKenneth D. Merry 	case CAM_DATA_BIO:
1711a9934668SKenneth D. Merry 		error = EINVAL;
1712a9934668SKenneth D. Merry 		break;
1713a9934668SKenneth D. Merry 	}
1714a9934668SKenneth D. Merry 
1715a9934668SKenneth D. Merry bailout:
1716a9934668SKenneth D. Merry 	/*
1717a9934668SKenneth D. Merry 	 * Reset the user's pointers to their original values and free
1718a9934668SKenneth D. Merry 	 * allocated memory.
1719a9934668SKenneth D. Merry 	 */
1720a9934668SKenneth D. Merry 	passiocleanup(softc, io_req);
1721a9934668SKenneth D. Merry 
1722a9934668SKenneth D. Merry 	return (error);
1723a9934668SKenneth D. Merry }
1724a9934668SKenneth D. Merry 
172576babe50SJustin T. Gibbs static int
172689c9c53dSPoul-Henning Kamp passioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
172776babe50SJustin T. Gibbs {
172825a2902cSScott Long 	int error;
172925a2902cSScott Long 
173025a2902cSScott Long 	if ((error = passdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
1731f564de00SScott Long 		error = cam_compat_ioctl(dev, cmd, addr, flag, td, passdoioctl);
173225a2902cSScott Long 	}
173325a2902cSScott Long 	return (error);
173425a2902cSScott Long }
173525a2902cSScott Long 
173625a2902cSScott Long static int
173725a2902cSScott Long passdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
173825a2902cSScott Long {
173976babe50SJustin T. Gibbs 	struct	cam_periph *periph;
1740a9934668SKenneth D. Merry 	struct	pass_softc *softc;
174176babe50SJustin T. Gibbs 	int	error;
17428cff7eb8SAlexander Motin 	uint32_t priority;
174376babe50SJustin T. Gibbs 
1744e2a5fdf9SNate Lawson 	periph = (struct cam_periph *)dev->si_drv1;
17452b83592fSScott Long 	cam_periph_lock(periph);
1746a9934668SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
174776babe50SJustin T. Gibbs 
174876babe50SJustin T. Gibbs 	error = 0;
174976babe50SJustin T. Gibbs 
175076babe50SJustin T. Gibbs 	switch (cmd) {
175176babe50SJustin T. Gibbs 	case CAMIOCOMMAND:
175276babe50SJustin T. Gibbs 	{
175376babe50SJustin T. Gibbs 		union ccb *inccb;
175476babe50SJustin T. Gibbs 		union ccb *ccb;
17559deea857SKenneth D. Merry 		int ccb_malloced;
175676babe50SJustin T. Gibbs 
175776babe50SJustin T. Gibbs 		inccb = (union ccb *)addr;
17588fc77fffSConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
17598fc77fffSConrad Meyer 		if (inccb->ccb_h.func_code == XPT_SCSI_IO)
17608fc77fffSConrad Meyer 			inccb->csio.bio = NULL;
17618fc77fffSConrad Meyer #endif
17629deea857SKenneth D. Merry 
1763a0bbf9e0SMark Johnston 		if (inccb->ccb_h.flags & CAM_UNLOCKED) {
1764a0bbf9e0SMark Johnston 			error = EINVAL;
1765a0bbf9e0SMark Johnston 			break;
1766a0bbf9e0SMark Johnston 		}
1767a0bbf9e0SMark Johnston 
17689deea857SKenneth D. Merry 		/*
17699deea857SKenneth D. Merry 		 * Some CCB types, like scan bus and scan lun can only go
17709deea857SKenneth D. Merry 		 * through the transport layer device.
17719deea857SKenneth D. Merry 		 */
17729deea857SKenneth D. Merry 		if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
1773f0d9af51SMatt Jacob 			xpt_print(periph->path, "CCB function code %#x is "
1774f0d9af51SMatt Jacob 			    "restricted to the XPT device\n",
1775f0d9af51SMatt Jacob 			    inccb->ccb_h.func_code);
17769deea857SKenneth D. Merry 			error = ENODEV;
17779deea857SKenneth D. Merry 			break;
17789deea857SKenneth D. Merry 		}
17799deea857SKenneth D. Merry 
17808cff7eb8SAlexander Motin 		/* Compatibility for RL/priority-unaware code. */
17818cff7eb8SAlexander Motin 		priority = inccb->ccb_h.pinfo.priority;
1782cccf4220SAlexander Motin 		if (priority <= CAM_PRIORITY_OOB)
1783cccf4220SAlexander Motin 		    priority += CAM_PRIORITY_OOB + 1;
17848cff7eb8SAlexander Motin 
17859deea857SKenneth D. Merry 		/*
17869deea857SKenneth D. Merry 		 * Non-immediate CCBs need a CCB from the per-device pool
17879deea857SKenneth D. Merry 		 * of CCBs, which is scheduled by the transport layer.
17889deea857SKenneth D. Merry 		 * Immediate CCBs and user-supplied CCBs should just be
17899deea857SKenneth D. Merry 		 * malloced.
17909deea857SKenneth D. Merry 		 */
17919deea857SKenneth D. Merry 		if ((inccb->ccb_h.func_code & XPT_FC_QUEUED)
17929deea857SKenneth D. Merry 		 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) {
17938cff7eb8SAlexander Motin 			ccb = cam_periph_getccb(periph, priority);
17949deea857SKenneth D. Merry 			ccb_malloced = 0;
17959deea857SKenneth D. Merry 		} else {
17968008a935SScott Long 			ccb = xpt_alloc_ccb_nowait();
17979deea857SKenneth D. Merry 
17989deea857SKenneth D. Merry 			if (ccb != NULL)
17999deea857SKenneth D. Merry 				xpt_setup_ccb(&ccb->ccb_h, periph->path,
18008cff7eb8SAlexander Motin 					      priority);
18019deea857SKenneth D. Merry 			ccb_malloced = 1;
18029deea857SKenneth D. Merry 		}
18039deea857SKenneth D. Merry 
18049deea857SKenneth D. Merry 		if (ccb == NULL) {
1805f0d9af51SMatt Jacob 			xpt_print(periph->path, "unable to allocate CCB\n");
18069deea857SKenneth D. Merry 			error = ENOMEM;
18079deea857SKenneth D. Merry 			break;
18089deea857SKenneth D. Merry 		}
180976babe50SJustin T. Gibbs 
181076babe50SJustin T. Gibbs 		error = passsendccb(periph, ccb, inccb);
181176babe50SJustin T. Gibbs 
18129deea857SKenneth D. Merry 		if (ccb_malloced)
18139deea857SKenneth D. Merry 			xpt_free_ccb(ccb);
18149deea857SKenneth D. Merry 		else
181576babe50SJustin T. Gibbs 			xpt_release_ccb(ccb);
181676babe50SJustin T. Gibbs 
181776babe50SJustin T. Gibbs 		break;
181876babe50SJustin T. Gibbs 	}
1819a9934668SKenneth D. Merry 	case CAMIOQUEUE:
1820a9934668SKenneth D. Merry 	{
1821a9934668SKenneth D. Merry 		struct pass_io_req *io_req;
1822a9934668SKenneth D. Merry 		union ccb **user_ccb, *ccb;
1823a9934668SKenneth D. Merry 		xpt_opcode fc;
1824a9934668SKenneth D. Merry 
1825871dc983SBrooks Davis #ifdef COMPAT_FREEBSD32
1826871dc983SBrooks Davis 		if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
1827871dc983SBrooks Davis 			error = ENOTTY;
1828871dc983SBrooks Davis 			goto bailout;
1829871dc983SBrooks Davis 		}
1830871dc983SBrooks Davis #endif
1831a9934668SKenneth D. Merry 		if ((softc->flags & PASS_FLAG_ZONE_VALID) == 0) {
1832a9934668SKenneth D. Merry 			error = passcreatezone(periph);
1833a9934668SKenneth D. Merry 			if (error != 0)
1834a9934668SKenneth D. Merry 				goto bailout;
1835a9934668SKenneth D. Merry 		}
1836a9934668SKenneth D. Merry 
1837a9934668SKenneth D. Merry 		/*
1838a9934668SKenneth D. Merry 		 * We're going to do a blocking allocation for this I/O
1839a9934668SKenneth D. Merry 		 * request, so we have to drop the lock.
1840a9934668SKenneth D. Merry 		 */
1841a9934668SKenneth D. Merry 		cam_periph_unlock(periph);
1842a9934668SKenneth D. Merry 
1843a9934668SKenneth D. Merry 		io_req = uma_zalloc(softc->pass_zone, M_WAITOK | M_ZERO);
1844a9934668SKenneth D. Merry 		ccb = &io_req->ccb;
1845a9934668SKenneth D. Merry 		user_ccb = (union ccb **)addr;
1846a9934668SKenneth D. Merry 
1847a9934668SKenneth D. Merry 		/*
1848a9934668SKenneth D. Merry 		 * Unlike the CAMIOCOMMAND ioctl above, we only have a
1849a9934668SKenneth D. Merry 		 * pointer to the user's CCB, so we have to copy the whole
1850a9934668SKenneth D. Merry 		 * thing in to a buffer we have allocated (above) instead
1851a9934668SKenneth D. Merry 		 * of allowing the ioctl code to malloc a buffer and copy
1852a9934668SKenneth D. Merry 		 * it in.
1853a9934668SKenneth D. Merry 		 *
1854a9934668SKenneth D. Merry 		 * This is an advantage for this asynchronous interface,
1855a9934668SKenneth D. Merry 		 * since we don't want the memory to get freed while the
1856a9934668SKenneth D. Merry 		 * CCB is outstanding.
1857a9934668SKenneth D. Merry 		 */
1858a9934668SKenneth D. Merry #if 0
1859a9934668SKenneth D. Merry 		xpt_print(periph->path, "Copying user CCB %p to "
1860a9934668SKenneth D. Merry 			  "kernel address %p\n", *user_ccb, ccb);
1861a9934668SKenneth D. Merry #endif
1862a9934668SKenneth D. Merry 		error = copyin(*user_ccb, ccb, sizeof(*ccb));
1863a9934668SKenneth D. Merry 		if (error != 0) {
1864a9934668SKenneth D. Merry 			xpt_print(periph->path, "Copy of user CCB %p to "
1865a9934668SKenneth D. Merry 				  "kernel address %p failed with error %d\n",
1866a9934668SKenneth D. Merry 				  *user_ccb, ccb, error);
1867a0bbf9e0SMark Johnston 			goto camioqueue_error;
1868a9934668SKenneth D. Merry 		}
18698fc77fffSConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
18708fc77fffSConrad Meyer 		if (ccb->ccb_h.func_code == XPT_SCSI_IO)
18718fc77fffSConrad Meyer 			ccb->csio.bio = NULL;
18728fc77fffSConrad Meyer #endif
1873a9934668SKenneth D. Merry 
1874a0bbf9e0SMark Johnston 		if (ccb->ccb_h.flags & CAM_UNLOCKED) {
1875a0bbf9e0SMark Johnston 			error = EINVAL;
1876a0bbf9e0SMark Johnston 			goto camioqueue_error;
1877a0bbf9e0SMark Johnston 		}
1878a0bbf9e0SMark Johnston 
1879a1a604caSAlexander Motin 		if (ccb->ccb_h.flags & CAM_CDB_POINTER) {
1880a1a604caSAlexander Motin 			if (ccb->csio.cdb_len > IOCDBLEN) {
1881a1a604caSAlexander Motin 				error = EINVAL;
1882a0bbf9e0SMark Johnston 				goto camioqueue_error;
1883a1a604caSAlexander Motin 			}
1884a1a604caSAlexander Motin 			error = copyin(ccb->csio.cdb_io.cdb_ptr,
1885a1a604caSAlexander Motin 			    ccb->csio.cdb_io.cdb_bytes, ccb->csio.cdb_len);
1886a0bbf9e0SMark Johnston 			if (error != 0)
1887a0bbf9e0SMark Johnston 				goto camioqueue_error;
1888a1a604caSAlexander Motin 			ccb->ccb_h.flags &= ~CAM_CDB_POINTER;
1889a1a604caSAlexander Motin 		}
1890a1a604caSAlexander Motin 
1891a9934668SKenneth D. Merry 		/*
1892a9934668SKenneth D. Merry 		 * Some CCB types, like scan bus and scan lun can only go
1893a9934668SKenneth D. Merry 		 * through the transport layer device.
1894a9934668SKenneth D. Merry 		 */
1895a9934668SKenneth D. Merry 		if (ccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
1896a9934668SKenneth D. Merry 			xpt_print(periph->path, "CCB function code %#x is "
1897a9934668SKenneth D. Merry 			    "restricted to the XPT device\n",
1898a9934668SKenneth D. Merry 			    ccb->ccb_h.func_code);
1899a9934668SKenneth D. Merry 			error = ENODEV;
1900a0bbf9e0SMark Johnston 			goto camioqueue_error;
1901a9934668SKenneth D. Merry 		}
1902a9934668SKenneth D. Merry 
1903a9934668SKenneth D. Merry 		/*
1904a9934668SKenneth D. Merry 		 * Save the user's CCB pointer as well as his linked list
1905a9934668SKenneth D. Merry 		 * pointers and peripheral private area so that we can
1906a9934668SKenneth D. Merry 		 * restore these later.
1907a9934668SKenneth D. Merry 		 */
1908a9934668SKenneth D. Merry 		io_req->user_ccb_ptr = *user_ccb;
1909a9934668SKenneth D. Merry 		io_req->user_periph_links = ccb->ccb_h.periph_links;
1910a9934668SKenneth D. Merry 		io_req->user_periph_priv = ccb->ccb_h.periph_priv;
1911a9934668SKenneth D. Merry 
1912a9934668SKenneth D. Merry 		/*
1913a9934668SKenneth D. Merry 		 * Now that we've saved the user's values, we can set our
1914a9934668SKenneth D. Merry 		 * own peripheral private entry.
1915a9934668SKenneth D. Merry 		 */
1916a9934668SKenneth D. Merry 		ccb->ccb_h.ccb_ioreq = io_req;
1917a9934668SKenneth D. Merry 
1918a9934668SKenneth D. Merry 		/* Compatibility for RL/priority-unaware code. */
1919a9934668SKenneth D. Merry 		priority = ccb->ccb_h.pinfo.priority;
1920a9934668SKenneth D. Merry 		if (priority <= CAM_PRIORITY_OOB)
1921a9934668SKenneth D. Merry 		    priority += CAM_PRIORITY_OOB + 1;
1922a9934668SKenneth D. Merry 
1923a9934668SKenneth D. Merry 		/*
1924a9934668SKenneth D. Merry 		 * Setup fields in the CCB like the path and the priority.
1925a9934668SKenneth D. Merry 		 * The path in particular cannot be done in userland, since
1926a9934668SKenneth D. Merry 		 * it is a pointer to a kernel data structure.
1927a9934668SKenneth D. Merry 		 */
1928a9934668SKenneth D. Merry 		xpt_setup_ccb_flags(&ccb->ccb_h, periph->path, priority,
1929a9934668SKenneth D. Merry 				    ccb->ccb_h.flags);
1930a9934668SKenneth D. Merry 
1931a9934668SKenneth D. Merry 		/*
1932a9934668SKenneth D. Merry 		 * Setup our done routine.  There is no way for the user to
1933a9934668SKenneth D. Merry 		 * have a valid pointer here.
1934a9934668SKenneth D. Merry 		 */
1935a9934668SKenneth D. Merry 		ccb->ccb_h.cbfcnp = passdone;
1936a9934668SKenneth D. Merry 
1937a9934668SKenneth D. Merry 		fc = ccb->ccb_h.func_code;
1938a9934668SKenneth D. Merry 		/*
1939a9934668SKenneth D. Merry 		 * If this function code has memory that can be mapped in
1940a9934668SKenneth D. Merry 		 * or out, we need to call passmemsetup().
1941a9934668SKenneth D. Merry 		 */
1942a9934668SKenneth D. Merry 		if ((fc == XPT_SCSI_IO) || (fc == XPT_ATA_IO)
1943a9934668SKenneth D. Merry 		 || (fc == XPT_SMP_IO) || (fc == XPT_DEV_MATCH)
1944df424515SWarner Losh 		 || (fc == XPT_DEV_ADVINFO)
1945df424515SWarner Losh 		 || (fc == XPT_NVME_ADMIN) || (fc == XPT_NVME_IO)) {
1946a9934668SKenneth D. Merry 			error = passmemsetup(periph, io_req);
1947a0bbf9e0SMark Johnston 			if (error != 0)
1948a0bbf9e0SMark Johnston 				goto camioqueue_error;
1949a9934668SKenneth D. Merry 		} else
1950a9934668SKenneth D. Merry 			io_req->mapinfo.num_bufs_used = 0;
1951a9934668SKenneth D. Merry 
1952a9934668SKenneth D. Merry 		cam_periph_lock(periph);
1953a9934668SKenneth D. Merry 
1954a9934668SKenneth D. Merry 		/*
1955a9934668SKenneth D. Merry 		 * Everything goes on the incoming queue initially.
1956a9934668SKenneth D. Merry 		 */
1957a9934668SKenneth D. Merry 		TAILQ_INSERT_TAIL(&softc->incoming_queue, io_req, links);
1958a9934668SKenneth D. Merry 
1959a9934668SKenneth D. Merry 		/*
1960a9934668SKenneth D. Merry 		 * If the CCB is queued, and is not a user CCB, then
1961a9934668SKenneth D. Merry 		 * we need to allocate a slot for it.  Call xpt_schedule()
1962a9934668SKenneth D. Merry 		 * so that our start routine will get called when a CCB is
1963a9934668SKenneth D. Merry 		 * available.
1964a9934668SKenneth D. Merry 		 */
1965a9934668SKenneth D. Merry 		if ((fc & XPT_FC_QUEUED)
1966a9934668SKenneth D. Merry 		 && ((fc & XPT_FC_USER_CCB) == 0)) {
1967a9934668SKenneth D. Merry 			xpt_schedule(periph, priority);
1968a9934668SKenneth D. Merry 			break;
1969a9934668SKenneth D. Merry 		}
1970a9934668SKenneth D. Merry 
1971a9934668SKenneth D. Merry 		/*
1972a9934668SKenneth D. Merry 		 * At this point, the CCB in question is either an
1973a9934668SKenneth D. Merry 		 * immediate CCB (like XPT_DEV_ADVINFO) or it is a user CCB
1974a9934668SKenneth D. Merry 		 * and therefore should be malloced, not allocated via a slot.
1975a9934668SKenneth D. Merry 		 * Remove the CCB from the incoming queue and add it to the
1976a9934668SKenneth D. Merry 		 * active queue.
1977a9934668SKenneth D. Merry 		 */
1978a9934668SKenneth D. Merry 		TAILQ_REMOVE(&softc->incoming_queue, io_req, links);
1979a9934668SKenneth D. Merry 		TAILQ_INSERT_TAIL(&softc->active_queue, io_req, links);
1980a9934668SKenneth D. Merry 
1981a9934668SKenneth D. Merry 		xpt_action(ccb);
1982a9934668SKenneth D. Merry 
1983a9934668SKenneth D. Merry 		/*
1984a9934668SKenneth D. Merry 		 * If this is not a queued CCB (i.e. it is an immediate CCB),
1985a9934668SKenneth D. Merry 		 * then it is already done.  We need to put it on the done
1986a9934668SKenneth D. Merry 		 * queue for the user to fetch.
1987a9934668SKenneth D. Merry 		 */
1988a9934668SKenneth D. Merry 		if ((fc & XPT_FC_QUEUED) == 0) {
1989a9934668SKenneth D. Merry 			TAILQ_REMOVE(&softc->active_queue, io_req, links);
1990a9934668SKenneth D. Merry 			TAILQ_INSERT_TAIL(&softc->done_queue, io_req, links);
1991a9934668SKenneth D. Merry 		}
1992a9934668SKenneth D. Merry 		break;
1993a0bbf9e0SMark Johnston 
1994a0bbf9e0SMark Johnston camioqueue_error:
1995a0bbf9e0SMark Johnston 		uma_zfree(softc->pass_zone, io_req);
1996a0bbf9e0SMark Johnston 		cam_periph_lock(periph);
1997a0bbf9e0SMark Johnston 		break;
1998a9934668SKenneth D. Merry 	}
1999a9934668SKenneth D. Merry 	case CAMIOGET:
2000a9934668SKenneth D. Merry 	{
2001a9934668SKenneth D. Merry 		union ccb **user_ccb;
2002a9934668SKenneth D. Merry 		struct pass_io_req *io_req;
2003a9934668SKenneth D. Merry 		int old_error;
2004a9934668SKenneth D. Merry 
2005871dc983SBrooks Davis #ifdef COMPAT_FREEBSD32
2006871dc983SBrooks Davis 		if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
2007871dc983SBrooks Davis 			error = ENOTTY;
2008871dc983SBrooks Davis 			goto bailout;
2009871dc983SBrooks Davis 		}
2010871dc983SBrooks Davis #endif
2011a9934668SKenneth D. Merry 		user_ccb = (union ccb **)addr;
2012a9934668SKenneth D. Merry 		old_error = 0;
2013a9934668SKenneth D. Merry 
2014a9934668SKenneth D. Merry 		io_req = TAILQ_FIRST(&softc->done_queue);
2015a9934668SKenneth D. Merry 		if (io_req == NULL) {
2016a9934668SKenneth D. Merry 			error = ENOENT;
2017a9934668SKenneth D. Merry 			break;
2018a9934668SKenneth D. Merry 		}
2019a9934668SKenneth D. Merry 
2020a9934668SKenneth D. Merry 		/*
2021a9934668SKenneth D. Merry 		 * Remove the I/O from the done queue.
2022a9934668SKenneth D. Merry 		 */
2023a9934668SKenneth D. Merry 		TAILQ_REMOVE(&softc->done_queue, io_req, links);
2024a9934668SKenneth D. Merry 
2025a9934668SKenneth D. Merry 		/*
2026a9934668SKenneth D. Merry 		 * We have to drop the lock during the copyout because the
2027a9934668SKenneth D. Merry 		 * copyout can result in VM faults that require sleeping.
2028a9934668SKenneth D. Merry 		 */
2029a9934668SKenneth D. Merry 		cam_periph_unlock(periph);
2030a9934668SKenneth D. Merry 
2031a9934668SKenneth D. Merry 		/*
2032a9934668SKenneth D. Merry 		 * Do any needed copies (e.g. for reads) and revert the
2033a9934668SKenneth D. Merry 		 * pointers in the CCB back to the user's pointers.
2034a9934668SKenneth D. Merry 		 */
2035a9934668SKenneth D. Merry 		error = passmemdone(periph, io_req);
2036a9934668SKenneth D. Merry 
2037a9934668SKenneth D. Merry 		old_error = error;
2038a9934668SKenneth D. Merry 
2039a9934668SKenneth D. Merry 		io_req->ccb.ccb_h.periph_links = io_req->user_periph_links;
2040a9934668SKenneth D. Merry 		io_req->ccb.ccb_h.periph_priv = io_req->user_periph_priv;
2041a9934668SKenneth D. Merry 
2042a9934668SKenneth D. Merry #if 0
2043a9934668SKenneth D. Merry 		xpt_print(periph->path, "Copying to user CCB %p from "
2044a9934668SKenneth D. Merry 			  "kernel address %p\n", *user_ccb, &io_req->ccb);
2045a9934668SKenneth D. Merry #endif
2046a9934668SKenneth D. Merry 
2047a9934668SKenneth D. Merry 		error = copyout(&io_req->ccb, *user_ccb, sizeof(union ccb));
2048a9934668SKenneth D. Merry 		if (error != 0) {
2049a9934668SKenneth D. Merry 			xpt_print(periph->path, "Copy to user CCB %p from "
2050a9934668SKenneth D. Merry 				  "kernel address %p failed with error %d\n",
2051a9934668SKenneth D. Merry 				  *user_ccb, &io_req->ccb, error);
2052a9934668SKenneth D. Merry 		}
2053a9934668SKenneth D. Merry 
2054a9934668SKenneth D. Merry 		/*
2055a9934668SKenneth D. Merry 		 * Prefer the first error we got back, and make sure we
2056a9934668SKenneth D. Merry 		 * don't overwrite bad status with good.
2057a9934668SKenneth D. Merry 		 */
2058a9934668SKenneth D. Merry 		if (old_error != 0)
2059a9934668SKenneth D. Merry 			error = old_error;
2060a9934668SKenneth D. Merry 
2061a9934668SKenneth D. Merry 		cam_periph_lock(periph);
2062a9934668SKenneth D. Merry 
2063a9934668SKenneth D. Merry 		/*
2064a9934668SKenneth D. Merry 		 * At this point, if there was an error, we could potentially
2065a9934668SKenneth D. Merry 		 * re-queue the I/O and try again.  But why?  The error
2066a9934668SKenneth D. Merry 		 * would almost certainly happen again.  We might as well
2067a9934668SKenneth D. Merry 		 * not leak memory.
2068a9934668SKenneth D. Merry 		 */
2069a9934668SKenneth D. Merry 		uma_zfree(softc->pass_zone, io_req);
2070a9934668SKenneth D. Merry 		break;
2071a9934668SKenneth D. Merry 	}
207276babe50SJustin T. Gibbs 	default:
207376babe50SJustin T. Gibbs 		error = cam_periph_ioctl(periph, cmd, addr, passerror);
207476babe50SJustin T. Gibbs 		break;
207576babe50SJustin T. Gibbs 	}
207676babe50SJustin T. Gibbs 
2077a9934668SKenneth D. Merry bailout:
20782b83592fSScott Long 	cam_periph_unlock(periph);
2079a9934668SKenneth D. Merry 
208076babe50SJustin T. Gibbs 	return(error);
208176babe50SJustin T. Gibbs }
208276babe50SJustin T. Gibbs 
2083a9934668SKenneth D. Merry static int
2084a9934668SKenneth D. Merry passpoll(struct cdev *dev, int poll_events, struct thread *td)
2085a9934668SKenneth D. Merry {
2086a9934668SKenneth D. Merry 	struct cam_periph *periph;
2087a9934668SKenneth D. Merry 	struct pass_softc *softc;
2088a9934668SKenneth D. Merry 	int revents;
2089a9934668SKenneth D. Merry 
2090a9934668SKenneth D. Merry 	periph = (struct cam_periph *)dev->si_drv1;
2091a9934668SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
2092a9934668SKenneth D. Merry 
2093a9934668SKenneth D. Merry 	revents = poll_events & (POLLOUT | POLLWRNORM);
2094a9934668SKenneth D. Merry 	if ((poll_events & (POLLIN | POLLRDNORM)) != 0) {
2095a9934668SKenneth D. Merry 		cam_periph_lock(periph);
2096a9934668SKenneth D. Merry 
2097a9934668SKenneth D. Merry 		if (!TAILQ_EMPTY(&softc->done_queue)) {
2098a9934668SKenneth D. Merry 			revents |= poll_events & (POLLIN | POLLRDNORM);
2099a9934668SKenneth D. Merry 		}
2100a9934668SKenneth D. Merry 		cam_periph_unlock(periph);
2101a9934668SKenneth D. Merry 		if (revents == 0)
2102a9934668SKenneth D. Merry 			selrecord(td, &softc->read_select);
2103a9934668SKenneth D. Merry 	}
2104a9934668SKenneth D. Merry 
2105a9934668SKenneth D. Merry 	return (revents);
2106a9934668SKenneth D. Merry }
2107a9934668SKenneth D. Merry 
2108a9934668SKenneth D. Merry static int
2109a9934668SKenneth D. Merry passkqfilter(struct cdev *dev, struct knote *kn)
2110a9934668SKenneth D. Merry {
2111a9934668SKenneth D. Merry 	struct cam_periph *periph;
2112a9934668SKenneth D. Merry 	struct pass_softc *softc;
2113a9934668SKenneth D. Merry 
2114a9934668SKenneth D. Merry 	periph = (struct cam_periph *)dev->si_drv1;
2115a9934668SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
2116a9934668SKenneth D. Merry 
2117a9934668SKenneth D. Merry 	kn->kn_hook = (caddr_t)periph;
2118a9934668SKenneth D. Merry 	kn->kn_fop = &passread_filtops;
2119a9934668SKenneth D. Merry 	knlist_add(&softc->read_select.si_note, kn, 0);
2120a9934668SKenneth D. Merry 
2121a9934668SKenneth D. Merry 	return (0);
2122a9934668SKenneth D. Merry }
2123a9934668SKenneth D. Merry 
2124a9934668SKenneth D. Merry static void
2125a9934668SKenneth D. Merry passreadfiltdetach(struct knote *kn)
2126a9934668SKenneth D. Merry {
2127a9934668SKenneth D. Merry 	struct cam_periph *periph;
2128a9934668SKenneth D. Merry 	struct pass_softc *softc;
2129a9934668SKenneth D. Merry 
2130a9934668SKenneth D. Merry 	periph = (struct cam_periph *)kn->kn_hook;
2131a9934668SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
2132a9934668SKenneth D. Merry 
2133a9934668SKenneth D. Merry 	knlist_remove(&softc->read_select.si_note, kn, 0);
2134a9934668SKenneth D. Merry }
2135a9934668SKenneth D. Merry 
2136a9934668SKenneth D. Merry static int
2137a9934668SKenneth D. Merry passreadfilt(struct knote *kn, long hint)
2138a9934668SKenneth D. Merry {
2139a9934668SKenneth D. Merry 	struct cam_periph *periph;
2140a9934668SKenneth D. Merry 	struct pass_softc *softc;
2141a9934668SKenneth D. Merry 	int retval;
2142a9934668SKenneth D. Merry 
2143a9934668SKenneth D. Merry 	periph = (struct cam_periph *)kn->kn_hook;
2144a9934668SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
2145a9934668SKenneth D. Merry 
2146a9934668SKenneth D. Merry 	cam_periph_assert(periph, MA_OWNED);
2147a9934668SKenneth D. Merry 
2148a9934668SKenneth D. Merry 	if (TAILQ_EMPTY(&softc->done_queue))
2149a9934668SKenneth D. Merry 		retval = 0;
2150a9934668SKenneth D. Merry 	else
2151a9934668SKenneth D. Merry 		retval = 1;
2152a9934668SKenneth D. Merry 
2153a9934668SKenneth D. Merry 	return (retval);
2154a9934668SKenneth D. Merry }
2155a9934668SKenneth D. Merry 
215676babe50SJustin T. Gibbs /*
215776babe50SJustin T. Gibbs  * Generally, "ccb" should be the CCB supplied by the kernel.  "inccb"
215876babe50SJustin T. Gibbs  * should be the CCB that is copied in from the user.
215976babe50SJustin T. Gibbs  */
216076babe50SJustin T. Gibbs static int
216176babe50SJustin T. Gibbs passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
216276babe50SJustin T. Gibbs {
216376babe50SJustin T. Gibbs 	struct pass_softc *softc;
216476babe50SJustin T. Gibbs 	struct cam_periph_map_info mapinfo;
2165a1a604caSAlexander Motin 	uint8_t *cmd;
216695fbded6SScott Long 	xpt_opcode fc;
216795fbded6SScott Long 	int error;
216876babe50SJustin T. Gibbs 
216976babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
217076babe50SJustin T. Gibbs 
217176babe50SJustin T. Gibbs 	/*
217276babe50SJustin T. Gibbs 	 * There are some fields in the CCB header that need to be
217376babe50SJustin T. Gibbs 	 * preserved, the rest we get from the user.
217476babe50SJustin T. Gibbs 	 */
217576babe50SJustin T. Gibbs 	xpt_merge_ccb(ccb, inccb);
217676babe50SJustin T. Gibbs 
2177a1a604caSAlexander Motin 	if (ccb->ccb_h.flags & CAM_CDB_POINTER) {
2178a1a604caSAlexander Motin 		cmd = __builtin_alloca(ccb->csio.cdb_len);
2179a1a604caSAlexander Motin 		error = copyin(ccb->csio.cdb_io.cdb_ptr, cmd, ccb->csio.cdb_len);
2180a1a604caSAlexander Motin 		if (error)
2181a1a604caSAlexander Motin 			return (error);
2182a1a604caSAlexander Motin 		ccb->csio.cdb_io.cdb_ptr = cmd;
2183a1a604caSAlexander Motin 	}
2184a1a604caSAlexander Motin 
218576babe50SJustin T. Gibbs 	/*
218695fbded6SScott Long 	 * Let cam_periph_mapmem do a sanity check on the data pointer format.
218795fbded6SScott Long 	 * Even if no data transfer is needed, it's a cheap check and it
218895fbded6SScott Long 	 * simplifies the code.
218976babe50SJustin T. Gibbs 	 */
219095fbded6SScott Long 	fc = ccb->ccb_h.func_code;
219195fbded6SScott Long 	if ((fc == XPT_SCSI_IO) || (fc == XPT_ATA_IO) || (fc == XPT_SMP_IO)
2192a3cf03a5SWarner Losh             || (fc == XPT_DEV_MATCH) || (fc == XPT_DEV_ADVINFO) || (fc == XPT_MMC_IO)
2193a3cf03a5SWarner Losh             || (fc == XPT_NVME_ADMIN) || (fc == XPT_NVME_IO)) {
219476babe50SJustin T. Gibbs 		bzero(&mapinfo, sizeof(mapinfo));
219576babe50SJustin T. Gibbs 
21962b83592fSScott Long 		/*
21972b83592fSScott Long 		 * cam_periph_mapmem calls into proc and vm functions that can
21982b83592fSScott Long 		 * sleep as well as trigger I/O, so we can't hold the lock.
21992b83592fSScott Long 		 * Dropping it here is reasonably safe.
22002b83592fSScott Long 		 */
22012b83592fSScott Long 		cam_periph_unlock(periph);
2202de239312SAlexander Motin 		error = cam_periph_mapmem(ccb, &mapinfo, softc->maxio);
22032b83592fSScott Long 		cam_periph_lock(periph);
220476babe50SJustin T. Gibbs 
220576babe50SJustin T. Gibbs 		/*
220676babe50SJustin T. Gibbs 		 * cam_periph_mapmem returned an error, we can't continue.
220776babe50SJustin T. Gibbs 		 * Return the error to the user.
220876babe50SJustin T. Gibbs 		 */
220976babe50SJustin T. Gibbs 		if (error)
221076babe50SJustin T. Gibbs 			return(error);
221195fbded6SScott Long 	} else
221295fbded6SScott Long 		/* Ensure that the unmap call later on is a no-op. */
221395fbded6SScott Long 		mapinfo.num_bufs_used = 0;
221476babe50SJustin T. Gibbs 
221576babe50SJustin T. Gibbs 	/*
221676babe50SJustin T. Gibbs 	 * If the user wants us to perform any error recovery, then honor
221776babe50SJustin T. Gibbs 	 * that request.  Otherwise, it's up to the user to perform any
221876babe50SJustin T. Gibbs 	 * error recovery.
221976babe50SJustin T. Gibbs 	 */
22206953d22bSKenneth D. Merry 	cam_periph_runccb(ccb, (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ?
22216953d22bSKenneth D. Merry 	    passerror : NULL, /* cam_flags */ CAM_RETRY_SELTO,
22226953d22bSKenneth D. Merry 	    /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT,
2223a9d2245eSPoul-Henning Kamp 	    softc->device_stats);
222476babe50SJustin T. Gibbs 
22258cb46437SAlexander Motin 	cam_periph_unlock(periph);
222676babe50SJustin T. Gibbs 	cam_periph_unmapmem(ccb, &mapinfo);
22278cb46437SAlexander Motin 	cam_periph_lock(periph);
222876babe50SJustin T. Gibbs 
222976babe50SJustin T. Gibbs 	ccb->ccb_h.cbfcnp = NULL;
223076babe50SJustin T. Gibbs 	ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
223176babe50SJustin T. Gibbs 	bcopy(ccb, inccb, sizeof(union ccb));
223276babe50SJustin T. Gibbs 
223383c5d981SAlexander Motin 	return(0);
223476babe50SJustin T. Gibbs }
223576babe50SJustin T. Gibbs 
223676babe50SJustin T. Gibbs static int
223776babe50SJustin T. Gibbs passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
223876babe50SJustin T. Gibbs {
223976babe50SJustin T. Gibbs 
2240553484aeSWarner Losh 	return(cam_periph_error(ccb, cam_flags, sense_flags));
224176babe50SJustin T. Gibbs }
2242