xref: /spdk/lib/scsi/scsi_internal.h (revision 9889ab2dc80e40dae92dcef361d53dcba722043d)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
5  *   Copyright (c) Intel Corporation.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef SPDK_SCSI_INTERNAL_H
36 #define SPDK_SCSI_INTERNAL_H
37 
38 #include "spdk/stdinc.h"
39 
40 #include "spdk/bdev.h"
41 #include "spdk/scsi.h"
42 #include "spdk/scsi_spec.h"
43 #include "spdk/trace.h"
44 #include "spdk/dif.h"
45 
46 #include "spdk_internal/log.h"
47 
48 enum {
49 	SPDK_SCSI_TASK_UNKNOWN = -1,
50 	SPDK_SCSI_TASK_COMPLETE,
51 	SPDK_SCSI_TASK_PENDING,
52 };
53 
54 struct spdk_scsi_port {
55 	uint8_t			is_used;
56 	uint64_t		id;
57 	uint16_t		index;
58 	uint16_t		transport_id_len;
59 	char			transport_id[SPDK_SCSI_MAX_TRANSPORT_ID_LENGTH];
60 	char			name[SPDK_SCSI_PORT_MAX_NAME_LENGTH];
61 };
62 
63 /* Registrant with I_T nextus */
64 struct spdk_scsi_pr_registrant {
65 	uint64_t				rkey;
66 	uint16_t				relative_target_port_id;
67 	uint16_t				transport_id_len;
68 	char					transport_id[SPDK_SCSI_MAX_TRANSPORT_ID_LENGTH];
69 	char					initiator_port_name[SPDK_SCSI_PORT_MAX_NAME_LENGTH];
70 	char					target_port_name[SPDK_SCSI_PORT_MAX_NAME_LENGTH];
71 	struct spdk_scsi_port			*initiator_port;
72 	struct spdk_scsi_port			*target_port;
73 	TAILQ_ENTRY(spdk_scsi_pr_registrant)	link;
74 };
75 
76 /* Reservation with LU_SCOPE */
77 struct spdk_scsi_pr_reservation {
78 	struct spdk_scsi_pr_registrant		*holder;
79 	enum spdk_scsi_pr_type_code		rtype;
80 	uint64_t				crkey;
81 };
82 
83 struct spdk_scsi_dev {
84 	int					id;
85 	int					is_allocated;
86 	bool					removed;
87 	spdk_scsi_dev_destruct_cb_t		remove_cb;
88 	void					*remove_ctx;
89 
90 	char					name[SPDK_SCSI_DEV_MAX_NAME + 1];
91 
92 	struct spdk_scsi_lun			*lun[SPDK_SCSI_DEV_MAX_LUN];
93 
94 	int					num_ports;
95 	struct spdk_scsi_port			port[SPDK_SCSI_DEV_MAX_PORTS];
96 
97 	uint8_t					protocol_id;
98 };
99 
100 struct spdk_scsi_lun_desc {
101 	struct spdk_scsi_lun		*lun;
102 	spdk_scsi_lun_remove_cb_t	hotremove_cb;
103 	void				*hotremove_ctx;
104 	TAILQ_ENTRY(spdk_scsi_lun_desc)	link;
105 };
106 
107 struct spdk_scsi_lun {
108 	/** LUN id for this logical unit. */
109 	int id;
110 
111 	/** Pointer to the SCSI device containing this LUN. */
112 	struct spdk_scsi_dev *dev;
113 
114 	/** The bdev associated with this LUN. */
115 	struct spdk_bdev *bdev;
116 
117 	/** Descriptor for opened block device. */
118 	struct spdk_bdev_desc *bdev_desc;
119 
120 	/** I/O channel for the bdev associated with this LUN. */
121 	struct spdk_io_channel *io_channel;
122 
123 	/**  The reference number for this LUN, thus we can correctly free the io_channel */
124 	uint32_t ref;
125 
126 	/** Poller to release the resource of the lun when it is hot removed */
127 	struct spdk_poller *hotremove_poller;
128 
129 	/** The LUN is removed */
130 	bool removed;
131 
132 	/** Callback to be fired when LUN removal is first triggered. */
133 	void (*hotremove_cb)(const struct spdk_scsi_lun *lun, void *arg);
134 
135 	/** Argument for hotremove_cb */
136 	void *hotremove_ctx;
137 
138 	/** Registrant head for I_T nexus */
139 	TAILQ_HEAD(, spdk_scsi_pr_registrant) reg_head;
140 	/** Persistent Reservation Generation */
141 	uint32_t pr_generation;
142 	/** Reservation for the LUN */
143 	struct spdk_scsi_pr_reservation reservation;
144 
145 	/** List of open descriptors for this LUN. */
146 	TAILQ_HEAD(, spdk_scsi_lun_desc) open_descs;
147 
148 	/** submitted tasks */
149 	TAILQ_HEAD(tasks, spdk_scsi_task) tasks;
150 
151 	/** pending tasks */
152 	TAILQ_HEAD(pending_tasks, spdk_scsi_task) pending_tasks;
153 
154 	/** submitted management tasks */
155 	TAILQ_HEAD(mgmt_tasks, spdk_scsi_task) mgmt_tasks;
156 
157 	/** pending management tasks */
158 	TAILQ_HEAD(pending_mgmt_tasks, spdk_scsi_task) pending_mgmt_tasks;
159 
160 	/** poller to check completion of tasks prior to reset */
161 	struct spdk_poller *reset_poller;
162 };
163 
164 struct spdk_lun_db_entry {
165 	struct spdk_scsi_lun *lun;
166 	struct spdk_lun_db_entry *next;
167 };
168 
169 extern struct spdk_lun_db_entry *spdk_scsi_lun_list_head;
170 
171 /* This typedef exists to work around an astyle 2.05 bug.
172  * Remove it when astyle is fixed.
173  */
174 typedef struct spdk_scsi_lun _spdk_scsi_lun;
175 
176 _spdk_scsi_lun *spdk_scsi_lun_construct(struct spdk_bdev *bdev,
177 					void (*hotremove_cb)(const struct spdk_scsi_lun *, void *),
178 					void *hotremove_ctx);
179 void spdk_scsi_lun_destruct(struct spdk_scsi_lun *lun);
180 
181 void spdk_scsi_lun_append_task(struct spdk_scsi_lun *lun, struct spdk_scsi_task *task);
182 void spdk_scsi_lun_execute_tasks(struct spdk_scsi_lun *lun);
183 void spdk_scsi_lun_append_mgmt_task(struct spdk_scsi_lun *lun, struct spdk_scsi_task *task);
184 void spdk_scsi_lun_execute_mgmt_task(struct spdk_scsi_lun *lun);
185 bool spdk_scsi_lun_has_pending_mgmt_tasks(const struct spdk_scsi_lun *lun,
186 		const struct spdk_scsi_port *initiator_port);
187 void spdk_scsi_lun_complete_task(struct spdk_scsi_lun *lun, struct spdk_scsi_task *task);
188 void spdk_scsi_lun_complete_reset_task(struct spdk_scsi_lun *lun, struct spdk_scsi_task *task);
189 bool spdk_scsi_lun_has_pending_tasks(const struct spdk_scsi_lun *lun,
190 				     const struct spdk_scsi_port *initiator_port);
191 int _spdk_scsi_lun_allocate_io_channel(struct spdk_scsi_lun *lun);
192 void _spdk_scsi_lun_free_io_channel(struct spdk_scsi_lun *lun);
193 
194 struct spdk_scsi_dev *spdk_scsi_dev_get_list(void);
195 
196 int spdk_scsi_port_construct(struct spdk_scsi_port *port, uint64_t id,
197 			     uint16_t index, const char *name);
198 void spdk_scsi_port_destruct(struct spdk_scsi_port *port);
199 
200 int spdk_bdev_scsi_execute(struct spdk_scsi_task *task);
201 void spdk_bdev_scsi_reset(struct spdk_scsi_task *task);
202 
203 bool spdk_scsi_bdev_get_dif_ctx(struct spdk_bdev *bdev, uint8_t *cdb, uint32_t data_offset,
204 				struct spdk_dif_ctx *dif_ctx);
205 
206 int spdk_scsi_pr_out(struct spdk_scsi_task *task, uint8_t *cdb, uint8_t *data, uint16_t data_len);
207 int spdk_scsi_pr_in(struct spdk_scsi_task *task, uint8_t *cdb, uint8_t *data, uint16_t data_len);
208 int spdk_scsi_pr_check(struct spdk_scsi_task *task);
209 
210 struct spdk_scsi_globals {
211 	pthread_mutex_t mutex;
212 };
213 
214 extern struct spdk_scsi_globals g_spdk_scsi;
215 
216 #endif /* SPDK_SCSI_INTERNAL_H */
217