1195534Sscottl/*-
2195534Sscottl * Copyright 2009 Scott Long
3195534Sscottl * All rights reserved.
4195534Sscottl *
5195534Sscottl * Redistribution and use in source and binary forms, with or without
6195534Sscottl * modification, are permitted provided that the following conditions
7195534Sscottl * are met:
8195534Sscottl * 1. Redistributions of source code must retain the above copyright
9195534Sscottl *    notice, this list of conditions, and the following disclaimer,
10195534Sscottl *    without modification, immediately at the beginning of the file.
11195534Sscottl * 2. The name of the author may not be used to endorse or promote products
12195534Sscottl *    derived from this software without specific prior written permission.
13195534Sscottl *
14195534Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15195534Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16195534Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17195534Sscottl * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18195534Sscottl * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19195534Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20195534Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21195534Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22195534Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23195534Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24195534Sscottl * SUCH DAMAGE.
25195534Sscottl *
26195534Sscottl * $FreeBSD$
27195534Sscottl */
28195534Sscottl
29195534Sscottl#ifndef _CAM_CAM_XPT_INTERNAL_H
30195534Sscottl#define _CAM_CAM_XPT_INTERNAL_H 1
31195534Sscottl
32260387Sscottl#include <sys/taskqueue.h>
33260387Sscottl
34195534Sscottl/* Forward Declarations */
35195534Sscottlstruct cam_eb;
36195534Sscottlstruct cam_et;
37195534Sscottlstruct cam_ed;
38195534Sscottl
39195534Sscottltypedef struct cam_ed * (*xpt_alloc_device_func)(struct cam_eb *bus,
40195534Sscottl					         struct cam_et *target,
41195534Sscottl					         lun_id_t lun_id);
42198748Smavtypedef void (*xpt_release_device_func)(struct cam_ed *device);
43195534Sscottltypedef void (*xpt_action_func)(union ccb *start_ccb);
44195534Sscottltypedef void (*xpt_dev_async_func)(u_int32_t async_code,
45195534Sscottl				   struct cam_eb *bus,
46195534Sscottl				   struct cam_et *target,
47195534Sscottl				   struct cam_ed *device,
48195534Sscottl				   void *async_arg);
49204220Smavtypedef void (*xpt_announce_periph_func)(struct cam_periph *periph);
50195534Sscottl
51195534Sscottlstruct xpt_xport {
52195534Sscottl	xpt_alloc_device_func	alloc_device;
53195534Sscottl	xpt_release_device_func	reldev;
54195534Sscottl	xpt_action_func		action;
55195534Sscottl	xpt_dev_async_func	async;
56195534Sscottl	xpt_announce_periph_func announce;
57195534Sscottl};
58195534Sscottl
59195534Sscottl/*
60195534Sscottl * The CAM EDT (Existing Device Table) contains the device information for
61195534Sscottl * all devices for all busses in the system.  The table contains a
62195534Sscottl * cam_ed structure for each device on the bus.
63195534Sscottl */
64195534Sscottlstruct cam_ed {
65260387Sscottl	cam_pinfo	 devq_entry;
66195534Sscottl	TAILQ_ENTRY(cam_ed) links;
67195534Sscottl	struct	cam_et	 *target;
68195534Sscottl	struct	cam_sim  *sim;
69195534Sscottl	lun_id_t	 lun_id;
70195534Sscottl	struct	cam_ccbq ccbq;		/* Queue of pending ccbs */
71195534Sscottl	struct	async_list asyncs;	/* Async callback info for this B/T/L */
72195534Sscottl	struct	periph_list periphs;	/* All attached devices */
73195534Sscottl	u_int	generation;		/* Generation number */
74195534Sscottl	void		 *quirk;	/* Oddities about this device */
75195534Sscottl	u_int		 maxtags;
76195534Sscottl	u_int		 mintags;
77195534Sscottl	cam_proto	 protocol;
78195534Sscottl	u_int		 protocol_version;
79195534Sscottl	cam_xport	 transport;
80195534Sscottl	u_int		 transport_version;
81195534Sscottl	struct		 scsi_inquiry_data inq_data;
82216088Sken	uint8_t		 *supported_vpds;
83216088Sken	uint8_t		 supported_vpds_len;
84216088Sken	uint32_t	 device_id_len;
85216088Sken	uint8_t		 *device_id;
86223081Sgibbs	uint8_t		 physpath_len;
87223081Sgibbs	uint8_t		 *physpath;	/* physical path string form */
88230590Sken	uint32_t	 rcap_len;
89230590Sken	uint8_t		 *rcap_buf;
90195534Sscottl	struct		 ata_params ident_data;
91195534Sscottl	u_int8_t	 inq_flags;	/*
92195534Sscottl					 * Current settings for inquiry flags.
93195534Sscottl					 * This allows us to override settings
94195534Sscottl					 * like disconnection and tagged
95195534Sscottl					 * queuing for a device.
96195534Sscottl					 */
97195534Sscottl	u_int8_t	 queue_flags;	/* Queue flags from the control page */
98195534Sscottl	u_int8_t	 serial_num_len;
99195534Sscottl	u_int8_t	*serial_num;
100195534Sscottl	u_int32_t	 flags;
101195534Sscottl#define CAM_DEV_UNCONFIGURED	 	0x01
102195534Sscottl#define CAM_DEV_REL_TIMEOUT_PENDING	0x02
103195534Sscottl#define CAM_DEV_REL_ON_COMPLETE		0x04
104195534Sscottl#define CAM_DEV_REL_ON_QUEUE_EMPTY	0x08
105195534Sscottl#define CAM_DEV_TAG_AFTER_COUNT		0x20
106195534Sscottl#define CAM_DEV_INQUIRY_DATA_VALID	0x40
107195534Sscottl#define	CAM_DEV_IN_DV			0x80
108195534Sscottl#define	CAM_DEV_DV_HIT_BOTTOM		0x100
109198331Smav#define CAM_DEV_IDENTIFY_DATA_VALID	0x200
110195534Sscottl	u_int32_t	 tag_delay_count;
111195534Sscottl#define	CAM_TAG_DELAY_COUNT		5
112195534Sscottl	u_int32_t	 tag_saved_openings;
113195534Sscottl	u_int32_t	 refcount;
114195534Sscottl	struct callout	 callout;
115253958Smav	STAILQ_ENTRY(cam_ed) highpowerq_entry;
116260387Sscottl	struct mtx	 device_mtx;
117260387Sscottl	struct task	 device_destroy_task;
118195534Sscottl};
119195534Sscottl
120195534Sscottl/*
121195534Sscottl * Each target is represented by an ET (Existing Target).  These
122195534Sscottl * entries are created when a target is successfully probed with an
123195534Sscottl * identify, and removed when a device fails to respond after a number
124195534Sscottl * of retries, or a bus rescan finds the device missing.
125195534Sscottl */
126195534Sscottlstruct cam_et {
127195534Sscottl	TAILQ_HEAD(, cam_ed) ed_entries;
128195534Sscottl	TAILQ_ENTRY(cam_et) links;
129195534Sscottl	struct	cam_eb	*bus;
130195534Sscottl	target_id_t	target_id;
131195534Sscottl	u_int32_t	refcount;
132195534Sscottl	u_int		generation;
133195534Sscottl	struct		timeval last_reset;
134208911Smjacob	u_int		rpl_size;
135208911Smjacob	struct scsi_report_luns_data *luns;
136260387Sscottl	struct mtx	luns_mtx;	/* Protection for luns field. */
137195534Sscottl};
138195534Sscottl
139195534Sscottl/*
140195534Sscottl * Each bus is represented by an EB (Existing Bus).  These entries
141195534Sscottl * are created by calls to xpt_bus_register and deleted by calls to
142195534Sscottl * xpt_bus_deregister.
143195534Sscottl */
144195534Sscottlstruct cam_eb {
145195534Sscottl	TAILQ_HEAD(, cam_et) et_entries;
146195534Sscottl	TAILQ_ENTRY(cam_eb)  links;
147195534Sscottl	path_id_t	     path_id;
148195534Sscottl	struct cam_sim	     *sim;
149195534Sscottl	struct timeval	     last_reset;
150195534Sscottl	u_int32_t	     flags;
151195534Sscottl#define	CAM_EB_RUNQ_SCHEDULED	0x01
152195534Sscottl	u_int32_t	     refcount;
153195534Sscottl	u_int		     generation;
154195534Sscottl	device_t	     parent_dev;
155195534Sscottl	struct xpt_xport     *xport;
156260387Sscottl	struct mtx	     eb_mtx;	/* Bus topology mutex. */
157195534Sscottl};
158195534Sscottl
159195534Sscottlstruct cam_path {
160195534Sscottl	struct cam_periph *periph;
161195534Sscottl	struct cam_eb	  *bus;
162195534Sscottl	struct cam_et	  *target;
163195534Sscottl	struct cam_ed	  *device;
164195534Sscottl};
165195534Sscottl
166195534Sscottlstruct xpt_xport *	scsi_get_xport(void);
167195534Sscottlstruct xpt_xport *	ata_get_xport(void);
168195534Sscottl
169195534Sscottlstruct cam_ed *		xpt_alloc_device(struct cam_eb *bus,
170195534Sscottl					 struct cam_et *target,
171195534Sscottl					 lun_id_t lun_id);
172198748Smavvoid			xpt_acquire_device(struct cam_ed *device);
173198748Smavvoid			xpt_release_device(struct cam_ed *device);
174195534Sscottlu_int32_t		xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
175199178Smavvoid			xpt_start_tags(struct cam_path *path);
176199178Smavvoid			xpt_stop_tags(struct cam_path *path);
177195534Sscottl
178195534SscottlMALLOC_DECLARE(M_CAMXPT);
179195534Sscottl
180195534Sscottl#endif
181