xref: /freebsd/sys/cam/cam_xpt_internal.h (revision 13ea0450a9c8742119d36f3bf8f47accdce46e54)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright 2009 Scott Long
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification, immediately at the beginning of the file.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #ifndef _CAM_CAM_XPT_INTERNAL_H
32 #define _CAM_CAM_XPT_INTERNAL_H 1
33 
34 #include <sys/taskqueue.h>
35 
36 /* Forward Declarations */
37 struct cam_eb;
38 struct cam_et;
39 struct cam_ed;
40 
41 typedef struct cam_ed * (*xpt_alloc_device_func)(struct cam_eb *bus,
42 					         struct cam_et *target,
43 					         lun_id_t lun_id);
44 typedef void (*xpt_release_device_func)(struct cam_ed *device);
45 typedef void (*xpt_action_func)(union ccb *start_ccb);
46 typedef void (*xpt_dev_async_func)(u_int32_t async_code,
47 				   struct cam_eb *bus,
48 				   struct cam_et *target,
49 				   struct cam_ed *device,
50 				   void *async_arg);
51 typedef void (*xpt_announce_periph_func)(struct cam_periph *periph);
52 typedef void (*xpt_announce_periph_sbuf_func)(struct cam_periph *periph, struct sbuf *sbuf);
53 
54 struct xpt_xport_ops {
55 	xpt_alloc_device_func	alloc_device;
56 	xpt_release_device_func	reldev;
57 	xpt_action_func		action;
58 	xpt_dev_async_func	async;
59 	xpt_announce_periph_func announce;
60 	xpt_announce_periph_sbuf_func announce_sbuf;
61 };
62 
63 struct xpt_xport {
64 	cam_xport		xport;
65 	const char		*name;
66 	struct xpt_xport_ops	*ops;
67 };
68 
69 SET_DECLARE(cam_xpt_xport_set, struct xpt_xport);
70 #define CAM_XPT_XPORT(data) 				\
71 	DATA_SET(cam_xpt_xport_set, data)
72 
73 typedef void (*xpt_proto_announce_func)(struct cam_ed *);
74 typedef void (*xpt_proto_announce_sbuf_func)(struct cam_ed *, struct sbuf *);
75 typedef void (*xpt_proto_debug_out_func)(union ccb *);
76 
77 struct xpt_proto_ops {
78 	xpt_proto_announce_func	announce;
79 	xpt_proto_announce_sbuf_func	announce_sbuf;
80 	xpt_proto_announce_func	denounce;
81 	xpt_proto_announce_sbuf_func	denounce_sbuf;
82 	xpt_proto_debug_out_func debug_out;
83 };
84 
85 struct xpt_proto {
86 	cam_proto		proto;
87 	const char		*name;
88 	struct xpt_proto_ops	*ops;
89 };
90 
91 SET_DECLARE(cam_xpt_proto_set, struct xpt_proto);
92 #define CAM_XPT_PROTO(data) 				\
93 	DATA_SET(cam_xpt_proto_set, data)
94 
95 
96 /*
97  * The CAM EDT (Existing Device Table) contains the device information for
98  * all devices for all buses in the system.  The table contains a
99  * cam_ed structure for each device on the bus.
100  */
101 struct cam_ed {
102 	cam_pinfo	 devq_entry;
103 	TAILQ_ENTRY(cam_ed) links;
104 	struct	cam_et	 *target;
105 	struct	cam_sim  *sim;
106 	lun_id_t	 lun_id;
107 	struct	cam_ccbq ccbq;		/* Queue of pending ccbs */
108 	struct	async_list asyncs;	/* Async callback info for this B/T/L */
109 	struct	periph_list periphs;	/* All attached devices */
110 	u_int	generation;		/* Generation number */
111 	void		 *quirk;	/* Oddities about this device */
112 	u_int		 maxtags;
113 	u_int		 mintags;
114 	cam_proto	 protocol;
115 	u_int		 protocol_version;
116 	cam_xport	 transport;
117 	u_int		 transport_version;
118 	struct		 scsi_inquiry_data inq_data;
119 	uint8_t		 *supported_vpds;
120 	uint8_t		 supported_vpds_len;
121 	uint32_t	 device_id_len;
122 	uint8_t		 *device_id;
123 	uint32_t	 ext_inq_len;
124 	uint8_t		 *ext_inq;
125 	uint8_t		 physpath_len;
126 	uint8_t		 *physpath;	/* physical path string form */
127 	uint32_t	 rcap_len;
128 	uint8_t		 *rcap_buf;
129 	struct		 ata_params ident_data;
130         struct		 mmc_params mmc_ident_data;
131 	u_int8_t	 inq_flags;	/*
132 					 * Current settings for inquiry flags.
133 					 * This allows us to override settings
134 					 * like disconnection and tagged
135 					 * queuing for a device.
136 					 */
137 	u_int8_t	 queue_flags;	/* Queue flags from the control page */
138 	u_int8_t	 serial_num_len;
139 	u_int8_t	*serial_num;
140 	u_int32_t	 flags;
141 #define CAM_DEV_UNCONFIGURED	 	0x01
142 #define CAM_DEV_REL_TIMEOUT_PENDING	0x02
143 #define CAM_DEV_REL_ON_COMPLETE		0x04
144 #define CAM_DEV_REL_ON_QUEUE_EMPTY	0x08
145 #define CAM_DEV_TAG_AFTER_COUNT		0x20
146 #define CAM_DEV_INQUIRY_DATA_VALID	0x40
147 #define	CAM_DEV_IN_DV			0x80
148 #define	CAM_DEV_DV_HIT_BOTTOM		0x100
149 #define CAM_DEV_IDENTIFY_DATA_VALID	0x200
150 	u_int32_t	 tag_delay_count;
151 #define	CAM_TAG_DELAY_COUNT		5
152 	u_int32_t	 tag_saved_openings;
153 	u_int32_t	 refcount;
154 	struct callout	 callout;
155 	STAILQ_ENTRY(cam_ed) highpowerq_entry;
156 	struct mtx	 device_mtx;
157 	struct task	 device_destroy_task;
158 	struct nvme_controller_data *nvme_cdata;
159 	struct nvme_namespace_data *nvme_data;
160 };
161 
162 /*
163  * Each target is represented by an ET (Existing Target).  These
164  * entries are created when a target is successfully probed with an
165  * identify, and removed when a device fails to respond after a number
166  * of retries, or a bus rescan finds the device missing.
167  */
168 struct cam_et {
169 	TAILQ_HEAD(, cam_ed) ed_entries;
170 	TAILQ_ENTRY(cam_et) links;
171 	struct	cam_eb	*bus;
172 	target_id_t	target_id;
173 	u_int32_t	refcount;
174 	u_int		generation;
175 	struct		timeval last_reset;
176 	u_int		rpl_size;
177 	struct scsi_report_luns_data *luns;
178 	struct mtx	luns_mtx;	/* Protection for luns field. */
179 };
180 
181 /*
182  * Each bus is represented by an EB (Existing Bus).  These entries
183  * are created by calls to xpt_bus_register and deleted by calls to
184  * xpt_bus_deregister.
185  */
186 struct cam_eb {
187 	TAILQ_HEAD(, cam_et) et_entries;
188 	TAILQ_ENTRY(cam_eb)  links;
189 	path_id_t	     path_id;
190 	struct cam_sim	     *sim;
191 	struct timeval	     last_reset;
192 	u_int32_t	     flags;
193 #define	CAM_EB_RUNQ_SCHEDULED	0x01
194 	u_int32_t	     refcount;
195 	u_int		     generation;
196 	device_t	     parent_dev;
197 	struct xpt_xport     *xport;
198 	struct mtx	     eb_mtx;	/* Bus topology mutex. */
199 };
200 
201 struct cam_path {
202 	struct cam_periph *periph;
203 	struct cam_eb	  *bus;
204 	struct cam_et	  *target;
205 	struct cam_ed	  *device;
206 };
207 
208 struct cam_ed *		xpt_alloc_device(struct cam_eb *bus,
209 					 struct cam_et *target,
210 					 lun_id_t lun_id);
211 void			xpt_acquire_device(struct cam_ed *device);
212 void			xpt_release_device(struct cam_ed *device);
213 u_int32_t		xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
214 void			xpt_start_tags(struct cam_path *path);
215 void			xpt_stop_tags(struct cam_path *path);
216 
217 MALLOC_DECLARE(M_CAMXPT);
218 
219 #endif
220