xref: /spdk/lib/scsi/scsi_internal.h (revision 2fac05e919e1940137e4502f01beabb81ebbef9c)
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 	/**  The reference number for this LUN, thus we can correctly free the io_channel */
92 	uint32_t ref;
93 
94 	/** Poller to release the resource of the lun when it is hot removed */
95 	struct spdk_poller *hotremove_poller;
96 
97 	/** The LUN is removed */
98 	bool removed;
99 
100 	/** Callback to be fired when LUN removal is first triggered. */
101 	void (*hotremove_cb)(const struct spdk_scsi_lun *lun, void *arg);
102 
103 	/** Argument for hotremove_cb */
104 	void *hotremove_ctx;
105 
106 	TAILQ_HEAD(tasks, spdk_scsi_task) tasks;			/* pending tasks */
107 };
108 
109 struct spdk_lun_db_entry {
110 	struct spdk_scsi_lun *lun;
111 	struct spdk_lun_db_entry *next;
112 };
113 
114 extern struct spdk_lun_db_entry *spdk_scsi_lun_list_head;
115 
116 /* This typedef exists to work around an astyle 2.05 bug.
117  * Remove it when astyle is fixed.
118  */
119 typedef struct spdk_scsi_lun _spdk_scsi_lun;
120 
121 _spdk_scsi_lun *spdk_scsi_lun_construct(struct spdk_bdev *bdev,
122 					void (*hotremove_cb)(const struct spdk_scsi_lun *, void *),
123 					void *hotremove_ctx);
124 void spdk_scsi_lun_destruct(struct spdk_scsi_lun *lun);
125 
126 void spdk_scsi_lun_execute_task(struct spdk_scsi_lun *lun, struct spdk_scsi_task *task);
127 int spdk_scsi_lun_task_mgmt_execute(struct spdk_scsi_task *task, enum spdk_scsi_task_func func);
128 void spdk_scsi_lun_complete_task(struct spdk_scsi_lun *lun, struct spdk_scsi_task *task);
129 void spdk_scsi_lun_complete_mgmt_task(struct spdk_scsi_lun *lun, struct spdk_scsi_task *task);
130 int spdk_scsi_lun_allocate_io_channel(struct spdk_scsi_lun *lun);
131 void spdk_scsi_lun_free_io_channel(struct spdk_scsi_lun *lun);
132 bool spdk_scsi_lun_has_pending_tasks(const struct spdk_scsi_lun *lun);
133 
134 struct spdk_scsi_dev *spdk_scsi_dev_get_list(void);
135 
136 int spdk_scsi_port_construct(struct spdk_scsi_port *port, uint64_t id,
137 			     uint16_t index, const char *name);
138 void spdk_scsi_port_destruct(struct spdk_scsi_port *port);
139 
140 int spdk_bdev_scsi_execute(struct spdk_scsi_task *task);
141 int spdk_bdev_scsi_reset(struct spdk_scsi_task *task);
142 
143 struct spdk_scsi_globals {
144 	pthread_mutex_t mutex;
145 };
146 
147 extern struct spdk_scsi_globals g_spdk_scsi;
148 
149 #endif /* SPDK_SCSI_INTERNAL_H */
150