xref: /spdk/lib/scsi/scsi_internal.h (revision 1fc4165fe9bf8512483356ad8e6d27f793f2e3db)
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 struct spdk_scsi_dev {
64 	int			id;
65 	int			is_allocated;
66 	bool			removed;
67 
68 	char			name[SPDK_SCSI_DEV_MAX_NAME + 1];
69 
70 	struct spdk_scsi_lun	*lun[SPDK_SCSI_DEV_MAX_LUN];
71 
72 	int			num_ports;
73 	struct spdk_scsi_port	port[SPDK_SCSI_DEV_MAX_PORTS];
74 
75 	uint8_t			protocol_id;
76 };
77 
78 struct spdk_scsi_desc {
79 	struct spdk_scsi_lun		*lun;
80 	spdk_scsi_remove_cb_t		hotremove_cb;
81 	void				*hotremove_ctx;
82 	TAILQ_ENTRY(spdk_scsi_desc)	link;
83 };
84 
85 struct spdk_scsi_lun {
86 	/** LUN id for this logical unit. */
87 	int id;
88 
89 	/** Pointer to the SCSI device containing this LUN. */
90 	struct spdk_scsi_dev *dev;
91 
92 	/** The bdev associated with this LUN. */
93 	struct spdk_bdev *bdev;
94 
95 	/** Descriptor for opened block device. */
96 	struct spdk_bdev_desc *bdev_desc;
97 
98 	/** I/O channel for the bdev associated with this LUN. */
99 	struct spdk_io_channel *io_channel;
100 
101 	/**  The reference number for this LUN, thus we can correctly free the io_channel */
102 	uint32_t ref;
103 
104 	/** Poller to release the resource of the lun when it is hot removed */
105 	struct spdk_poller *hotremove_poller;
106 
107 	/** The LUN is removed */
108 	bool removed;
109 
110 	/** Callback to be fired when LUN removal is first triggered. */
111 	void (*hotremove_cb)(const struct spdk_scsi_lun *lun, void *arg);
112 
113 	/** Argument for hotremove_cb */
114 	void *hotremove_ctx;
115 
116 	/** List of open descriptors for this LUN. */
117 	TAILQ_HEAD(, spdk_scsi_desc) open_descs;
118 
119 	/** submitted tasks */
120 	TAILQ_HEAD(tasks, spdk_scsi_task) tasks;
121 
122 	/** pending tasks */
123 	TAILQ_HEAD(pending_tasks, spdk_scsi_task) pending_tasks;
124 
125 	/** submitted management tasks */
126 	TAILQ_HEAD(mgmt_tasks, spdk_scsi_task) mgmt_tasks;
127 
128 	/** pending management tasks */
129 	TAILQ_HEAD(pending_mgmt_tasks, spdk_scsi_task) pending_mgmt_tasks;
130 
131 	/** poller to check completion of tasks prior to reset */
132 	struct spdk_poller *reset_poller;
133 };
134 
135 struct spdk_lun_db_entry {
136 	struct spdk_scsi_lun *lun;
137 	struct spdk_lun_db_entry *next;
138 };
139 
140 extern struct spdk_lun_db_entry *spdk_scsi_lun_list_head;
141 
142 /* This typedef exists to work around an astyle 2.05 bug.
143  * Remove it when astyle is fixed.
144  */
145 typedef struct spdk_scsi_lun _spdk_scsi_lun;
146 
147 _spdk_scsi_lun *spdk_scsi_lun_construct(struct spdk_bdev *bdev,
148 					void (*hotremove_cb)(const struct spdk_scsi_lun *, void *),
149 					void *hotremove_ctx);
150 void spdk_scsi_lun_destruct(struct spdk_scsi_lun *lun);
151 
152 void spdk_scsi_lun_append_task(struct spdk_scsi_lun *lun, struct spdk_scsi_task *task);
153 void spdk_scsi_lun_execute_tasks(struct spdk_scsi_lun *lun);
154 void spdk_scsi_lun_append_mgmt_task(struct spdk_scsi_lun *lun, struct spdk_scsi_task *task);
155 void spdk_scsi_lun_execute_mgmt_task(struct spdk_scsi_lun *lun);
156 bool spdk_scsi_lun_has_pending_mgmt_tasks(const struct spdk_scsi_lun *lun);
157 void spdk_scsi_lun_complete_task(struct spdk_scsi_lun *lun, struct spdk_scsi_task *task);
158 void spdk_scsi_lun_complete_reset_task(struct spdk_scsi_lun *lun, struct spdk_scsi_task *task);
159 bool spdk_scsi_lun_has_pending_tasks(const struct spdk_scsi_lun *lun);
160 int _spdk_scsi_lun_allocate_io_channel(struct spdk_scsi_lun *lun);
161 void _spdk_scsi_lun_free_io_channel(struct spdk_scsi_lun *lun);
162 
163 struct spdk_scsi_dev *spdk_scsi_dev_get_list(void);
164 
165 int spdk_scsi_port_construct(struct spdk_scsi_port *port, uint64_t id,
166 			     uint16_t index, const char *name);
167 void spdk_scsi_port_destruct(struct spdk_scsi_port *port);
168 
169 int spdk_bdev_scsi_execute(struct spdk_scsi_task *task);
170 void spdk_bdev_scsi_reset(struct spdk_scsi_task *task);
171 
172 bool spdk_scsi_bdev_get_dif_ctx(struct spdk_bdev *bdev, uint8_t *cdb, uint32_t offset,
173 				struct spdk_dif_ctx *dif_ctx);
174 
175 struct spdk_scsi_globals {
176 	pthread_mutex_t mutex;
177 };
178 
179 extern struct spdk_scsi_globals g_spdk_scsi;
180 
181 #endif /* SPDK_SCSI_INTERNAL_H */
182