xref: /freebsd/sys/cam/cam_xpt_internal.h (revision 830940567b49bb0c08dfaed40418999e76616909)
1 /*-
2  * Copyright 2009 Scott Long
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions, and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
11  * 2. The name of the author may not be used to endorse or promote products
12  *    derived from this software without specific prior written permission.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #ifndef _CAM_CAM_XPT_INTERNAL_H
30 #define _CAM_CAM_XPT_INTERNAL_H 1
31 
32 /* Forward Declarations */
33 struct cam_eb;
34 struct cam_et;
35 struct cam_ed;
36 
37 typedef struct cam_ed * (*xpt_alloc_device_func)(struct cam_eb *bus,
38 					         struct cam_et *target,
39 					         lun_id_t lun_id);
40 typedef void (*xpt_release_device_func)(struct cam_eb *bus,
41 				        struct cam_et *target,
42 				        struct cam_ed *device);
43 typedef void (*xpt_action_func)(union ccb *start_ccb);
44 typedef void (*xpt_dev_async_func)(u_int32_t async_code,
45 				   struct cam_eb *bus,
46 				   struct cam_et *target,
47 				   struct cam_ed *device,
48 				   void *async_arg);
49 typedef void (*xpt_announce_periph_func)(struct cam_periph *periph,
50 					 char *announce_string);
51 
52 struct xpt_xport {
53 	xpt_alloc_device_func	alloc_device;
54 	xpt_release_device_func	reldev;
55 	xpt_action_func		action;
56 	xpt_dev_async_func	async;
57 	xpt_announce_periph_func announce;
58 };
59 
60 /*
61  * Structure for queueing a device in a run queue.
62  * There is one run queue for allocating new ccbs,
63  * and another for sending ccbs to the controller.
64  */
65 struct cam_ed_qinfo {
66 	cam_pinfo pinfo;
67 	struct	  cam_ed *device;
68 };
69 
70 /*
71  * The CAM EDT (Existing Device Table) contains the device information for
72  * all devices for all busses in the system.  The table contains a
73  * cam_ed structure for each device on the bus.
74  */
75 struct cam_ed {
76 	TAILQ_ENTRY(cam_ed) links;
77 	struct	cam_ed_qinfo alloc_ccb_entry;
78 	struct	cam_ed_qinfo send_ccb_entry;
79 	struct	cam_et	 *target;
80 	struct	cam_sim  *sim;
81 	lun_id_t	 lun_id;
82 	struct	camq drvq;		/*
83 					 * Queue of type drivers wanting to do
84 					 * work on this device.
85 					 */
86 	struct	cam_ccbq ccbq;		/* Queue of pending ccbs */
87 	struct	async_list asyncs;	/* Async callback info for this B/T/L */
88 	struct	periph_list periphs;	/* All attached devices */
89 	u_int	generation;		/* Generation number */
90 	struct	cam_periph *owner;	/* Peripheral driver's ownership tag */
91 	void		 *quirk;	/* Oddities about this device */
92 	u_int		 maxtags;
93 	u_int		 mintags;
94 	cam_proto	 protocol;
95 	u_int		 protocol_version;
96 	cam_xport	 transport;
97 	u_int		 transport_version;
98 	struct		 scsi_inquiry_data inq_data;
99 	struct		 ata_params ident_data;
100 	u_int8_t	 inq_flags;	/*
101 					 * Current settings for inquiry flags.
102 					 * This allows us to override settings
103 					 * like disconnection and tagged
104 					 * queuing for a device.
105 					 */
106 	u_int8_t	 queue_flags;	/* Queue flags from the control page */
107 	u_int8_t	 serial_num_len;
108 	u_int8_t	*serial_num;
109 	u_int32_t	 qfrozen_cnt;
110 	u_int32_t	 flags;
111 #define CAM_DEV_UNCONFIGURED	 	0x01
112 #define CAM_DEV_REL_TIMEOUT_PENDING	0x02
113 #define CAM_DEV_REL_ON_COMPLETE		0x04
114 #define CAM_DEV_REL_ON_QUEUE_EMPTY	0x08
115 #define CAM_DEV_RESIZE_QUEUE_NEEDED	0x10
116 #define CAM_DEV_TAG_AFTER_COUNT		0x20
117 #define CAM_DEV_INQUIRY_DATA_VALID	0x40
118 #define	CAM_DEV_IN_DV			0x80
119 #define	CAM_DEV_DV_HIT_BOTTOM		0x100
120 	u_int32_t	 tag_delay_count;
121 #define	CAM_TAG_DELAY_COUNT		5
122 	u_int32_t	 tag_saved_openings;
123 	u_int32_t	 refcount;
124 	struct callout	 callout;
125 };
126 
127 /*
128  * Each target is represented by an ET (Existing Target).  These
129  * entries are created when a target is successfully probed with an
130  * identify, and removed when a device fails to respond after a number
131  * of retries, or a bus rescan finds the device missing.
132  */
133 struct cam_et {
134 	TAILQ_HEAD(, cam_ed) ed_entries;
135 	TAILQ_ENTRY(cam_et) links;
136 	struct	cam_eb	*bus;
137 	target_id_t	target_id;
138 	u_int32_t	refcount;
139 	u_int		generation;
140 	struct		timeval last_reset;
141 };
142 
143 /*
144  * Each bus is represented by an EB (Existing Bus).  These entries
145  * are created by calls to xpt_bus_register and deleted by calls to
146  * xpt_bus_deregister.
147  */
148 struct cam_eb {
149 	TAILQ_HEAD(, cam_et) et_entries;
150 	TAILQ_ENTRY(cam_eb)  links;
151 	path_id_t	     path_id;
152 	struct cam_sim	     *sim;
153 	struct timeval	     last_reset;
154 	u_int32_t	     flags;
155 #define	CAM_EB_RUNQ_SCHEDULED	0x01
156 	u_int32_t	     refcount;
157 	u_int		     generation;
158 	device_t	     parent_dev;
159 	struct xpt_xport     *xport;
160 };
161 
162 struct cam_path {
163 	struct cam_periph *periph;
164 	struct cam_eb	  *bus;
165 	struct cam_et	  *target;
166 	struct cam_ed	  *device;
167 };
168 
169 struct xpt_xport *	scsi_get_xport(void);
170 struct xpt_xport *	ata_get_xport(void);
171 
172 struct cam_ed *		xpt_alloc_device(struct cam_eb *bus,
173 					 struct cam_et *target,
174 					 lun_id_t lun_id);
175 void			xpt_run_dev_sendq(struct cam_eb *bus);
176 int			xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
177 					 u_int32_t new_priority);
178 u_int32_t		xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
179 
180 
181 
182 static __inline int
183 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
184 {
185 	int	retval;
186 
187 	if (dev->ccbq.dev_openings > 0) {
188 		/*
189 		 * The priority of a device waiting for controller
190 		 * resources is that of the the highest priority CCB
191 		 * enqueued.
192 		 */
193 		retval =
194 		    xpt_schedule_dev(&bus->sim->devq->send_queue,
195 				     &dev->send_ccb_entry.pinfo,
196 				     CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
197 	} else {
198 		retval = 0;
199 	}
200 	return (retval);
201 }
202 
203 MALLOC_DECLARE(M_CAMXPT);
204 
205 #endif
206