xref: /spdk/lib/scsi/scsi_internal.h (revision 8a0a98d35e21f282088edf28b9e8da66ec390e3a)
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 
45 #include "spdk_internal/log.h"
46 
47 enum {
48 	SPDK_SCSI_TASK_UNKNOWN = -1,
49 	SPDK_SCSI_TASK_COMPLETE,
50 	SPDK_SCSI_TASK_PENDING,
51 };
52 
53 struct spdk_scsi_port {
54 	uint8_t			is_used;
55 	uint64_t		id;
56 	uint16_t		index;
57 	char			name[SPDK_SCSI_PORT_MAX_NAME_LENGTH];
58 };
59 
60 struct spdk_scsi_dev {
61 	int			id;
62 	int			is_allocated;
63 	bool			removed;
64 
65 	char			name[SPDK_SCSI_DEV_MAX_NAME + 1];
66 
67 	struct spdk_scsi_lun	*lun[SPDK_SCSI_DEV_MAX_LUN];
68 
69 	int			num_ports;
70 	struct spdk_scsi_port	port[SPDK_SCSI_DEV_MAX_PORTS];
71 
72 	uint8_t			protocol_id;
73 };
74 
75 struct spdk_scsi_lun {
76 	/** LUN id for this logical unit. */
77 	int id;
78 
79 	/** Pointer to the SCSI device containing this LUN. */
80 	struct spdk_scsi_dev *dev;
81 
82 	/** The bdev associated with this LUN. */
83 	struct spdk_bdev *bdev;
84 
85 	/** Descriptor for opened block device. */
86 	struct spdk_bdev_desc *bdev_desc;
87 
88 	/** I/O channel for the bdev associated with this LUN. */
89 	struct spdk_io_channel *io_channel;
90 
91 	/** Thread ID for the thread that allocated the I/O channel for this
92 	 *   LUN.  All I/O to this LUN must be performed from this thread.
93 	 */
94 	pthread_t thread_id;
95 
96 	/**  The reference number for this LUN, thus we can correctly free the io_channel */
97 	uint32_t ref;
98 
99 	/** Poller to release the resource of the lun when it is hot removed */
100 	struct spdk_poller *hotremove_poller;
101 
102 	/** The LUN is removed */
103 	bool removed;
104 
105 	/** Callback to be fired when LUN removal is first triggered. */
106 	void (*hotremove_cb)(const struct spdk_scsi_lun *lun, void *arg);
107 
108 	/** Argument for hotremove_cb */
109 	void *hotremove_ctx;
110 
111 	TAILQ_HEAD(tasks, spdk_scsi_task) tasks;			/* pending tasks */
112 };
113 
114 struct spdk_lun_db_entry {
115 	struct spdk_scsi_lun *lun;
116 	struct spdk_lun_db_entry *next;
117 };
118 
119 extern struct spdk_lun_db_entry *spdk_scsi_lun_list_head;
120 
121 /* This typedef exists to work around an astyle 2.05 bug.
122  * Remove it when astyle is fixed.
123  */
124 typedef struct spdk_scsi_lun _spdk_scsi_lun;
125 
126 _spdk_scsi_lun *spdk_scsi_lun_construct(struct spdk_bdev *bdev,
127 					void (*hotremove_cb)(const struct spdk_scsi_lun *, void *),
128 					void *hotremove_ctx);
129 void spdk_scsi_lun_destruct(struct spdk_scsi_lun *lun);
130 
131 void spdk_scsi_lun_execute_task(struct spdk_scsi_lun *lun, struct spdk_scsi_task *task);
132 int spdk_scsi_lun_task_mgmt_execute(struct spdk_scsi_task *task, enum spdk_scsi_task_func func);
133 void spdk_scsi_lun_complete_task(struct spdk_scsi_lun *lun, struct spdk_scsi_task *task);
134 void spdk_scsi_lun_complete_mgmt_task(struct spdk_scsi_lun *lun, struct spdk_scsi_task *task);
135 int spdk_scsi_lun_allocate_io_channel(struct spdk_scsi_lun *lun);
136 void spdk_scsi_lun_free_io_channel(struct spdk_scsi_lun *lun);
137 bool spdk_scsi_lun_has_pending_tasks(const struct spdk_scsi_lun *lun);
138 
139 struct spdk_scsi_dev *spdk_scsi_dev_get_list(void);
140 
141 int spdk_scsi_port_construct(struct spdk_scsi_port *port, uint64_t id,
142 			     uint16_t index, const char *name);
143 void spdk_scsi_port_destruct(struct spdk_scsi_port *port);
144 
145 int spdk_bdev_scsi_execute(struct spdk_scsi_task *task);
146 int spdk_bdev_scsi_reset(struct spdk_scsi_task *task);
147 
148 struct spdk_scsi_globals {
149 	pthread_mutex_t mutex;
150 };
151 
152 extern struct spdk_scsi_globals g_spdk_scsi;
153 
154 #endif /* SPDK_SCSI_INTERNAL_H */
155