xref: /freebsd/sys/cam/ctl/ctl_tpc_local.c (revision d37eb51047221dc3322b34db1038ff3aa533883f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2014 Alexander Motin <mav@FreeBSD.org>
5  * Copyright (c) 2004, 2005 Silicon Graphics International Corp.
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  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer,
13  *    without modification, immediately at the beginning of the file.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/types.h>
34 #include <sys/lock.h>
35 #include <sys/module.h>
36 #include <sys/mutex.h>
37 #include <sys/condvar.h>
38 #include <sys/malloc.h>
39 #include <sys/conf.h>
40 #include <sys/queue.h>
41 #include <sys/sysctl.h>
42 
43 #include <cam/cam.h>
44 #include <cam/scsi/scsi_all.h>
45 #include <cam/scsi/scsi_da.h>
46 #include <cam/ctl/ctl_io.h>
47 #include <cam/ctl/ctl.h>
48 #include <cam/ctl/ctl_frontend.h>
49 #include <cam/ctl/ctl_util.h>
50 #include <cam/ctl/ctl_backend.h>
51 #include <cam/ctl/ctl_ioctl.h>
52 #include <cam/ctl/ctl_ha.h>
53 #include <cam/ctl/ctl_private.h>
54 #include <cam/ctl/ctl_debug.h>
55 #include <cam/ctl/ctl_scsi_all.h>
56 #include <cam/ctl/ctl_tpc.h>
57 #include <cam/ctl/ctl_error.h>
58 
59 struct tpcl_softc {
60 	struct ctl_port port;
61 	u_int cur_tag_num;
62 };
63 
64 static struct tpcl_softc tpcl_softc;
65 
66 static int tpcl_init(void);
67 static int tpcl_shutdown(void);
68 static void tpcl_datamove(union ctl_io *io);
69 static void tpcl_done(union ctl_io *io);
70 
71 static struct ctl_frontend tpcl_frontend =
72 {
73 	.name = "tpc",
74 	.init = tpcl_init,
75 	.shutdown = tpcl_shutdown,
76 };
77 CTL_FRONTEND_DECLARE(ctltpc, tpcl_frontend);
78 
79 static int
80 tpcl_init(void)
81 {
82 	struct tpcl_softc *tsoftc = &tpcl_softc;
83 	struct ctl_port *port;
84 	struct scsi_transportid_spi *tid;
85 	int error, len;
86 
87 	memset(tsoftc, 0, sizeof(*tsoftc));
88 
89 	port = &tsoftc->port;
90 	port->frontend = &tpcl_frontend;
91 	port->port_type = CTL_PORT_INTERNAL;
92 	port->num_requested_ctl_io = 100;
93 	port->port_name = "tpc";
94 	port->fe_datamove = tpcl_datamove;
95 	port->fe_done = tpcl_done;
96 	port->targ_port = -1;
97 	port->max_initiators = 1;
98 
99 	if ((error = ctl_port_register(port)) != 0) {
100 		printf("%s: tpc port registration failed\n", __func__);
101 		return (error);
102 	}
103 
104 	len = sizeof(struct scsi_transportid_spi);
105 	port->init_devid = malloc(sizeof(struct ctl_devid) + len,
106 	    M_CTL, M_WAITOK | M_ZERO);
107 	port->init_devid->len = len;
108 	tid = (struct scsi_transportid_spi *)port->init_devid->data;
109 	tid->format_protocol = SCSI_TRN_SPI_FORMAT_DEFAULT | SCSI_PROTO_SPI;
110 	scsi_ulto2b(0, tid->scsi_addr);
111 	scsi_ulto2b(port->targ_port, tid->rel_trgt_port_id);
112 
113 	ctl_port_online(port);
114 	return (0);
115 }
116 
117 static int
118 tpcl_shutdown(void)
119 {
120 	struct tpcl_softc *tsoftc = &tpcl_softc;
121 	struct ctl_port *port = &tsoftc->port;
122 	int error;
123 
124 	ctl_port_offline(port);
125 	if ((error = ctl_port_deregister(port)) != 0)
126 		printf("%s: tpc port deregistration failed\n", __func__);
127 	return (error);
128 }
129 
130 static void
131 tpcl_datamove(union ctl_io *io)
132 {
133 	struct ctl_sg_entry *ext_sglist, *kern_sglist;
134 	struct ctl_sg_entry ext_entry, kern_entry;
135 	int ext_sg_entries, kern_sg_entries;
136 	int ext_sg_start, ext_offset;
137 	int len_to_copy;
138 	int kern_watermark, ext_watermark;
139 	struct ctl_scsiio *ctsio;
140 	int i, j;
141 
142 	CTL_DEBUG_PRINT(("%s\n", __func__));
143 
144 	ctsio = &io->scsiio;
145 
146 	/*
147 	 * If this is the case, we're probably doing a BBR read and don't
148 	 * actually need to transfer the data.  This will effectively
149 	 * bit-bucket the data.
150 	 */
151 	if (ctsio->ext_data_ptr == NULL)
152 		goto bailout;
153 
154 	/*
155 	 * To simplify things here, if we have a single buffer, stick it in
156 	 * a S/G entry and just make it a single entry S/G list.
157 	 */
158 	if (ctsio->ext_sg_entries > 0) {
159 		int len_seen;
160 
161 		ext_sglist = (struct ctl_sg_entry *)ctsio->ext_data_ptr;
162 		ext_sg_entries = ctsio->ext_sg_entries;
163 		ext_sg_start = 0;
164 		ext_offset = 0;
165 		len_seen = 0;
166 		for (i = 0; i < ext_sg_entries; i++) {
167 			if ((len_seen + ext_sglist[i].len) >=
168 			     ctsio->ext_data_filled) {
169 				ext_sg_start = i;
170 				ext_offset = ctsio->ext_data_filled - len_seen;
171 				break;
172 			}
173 			len_seen += ext_sglist[i].len;
174 		}
175 	} else {
176 		ext_sglist = &ext_entry;
177 		ext_sglist->addr = ctsio->ext_data_ptr;
178 		ext_sglist->len = ctsio->ext_data_len;
179 		ext_sg_entries = 1;
180 		ext_sg_start = 0;
181 		ext_offset = ctsio->ext_data_filled;
182 	}
183 
184 	if (ctsio->kern_sg_entries > 0) {
185 		kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
186 		kern_sg_entries = ctsio->kern_sg_entries;
187 	} else {
188 		kern_sglist = &kern_entry;
189 		kern_sglist->addr = ctsio->kern_data_ptr;
190 		kern_sglist->len = ctsio->kern_data_len;
191 		kern_sg_entries = 1;
192 	}
193 
194 	kern_watermark = 0;
195 	ext_watermark = ext_offset;
196 	for (i = ext_sg_start, j = 0;
197 	     i < ext_sg_entries && j < kern_sg_entries;) {
198 		uint8_t *ext_ptr, *kern_ptr;
199 
200 		len_to_copy = min(ext_sglist[i].len - ext_watermark,
201 				  kern_sglist[j].len - kern_watermark);
202 
203 		ext_ptr = (uint8_t *)ext_sglist[i].addr;
204 		ext_ptr = ext_ptr + ext_watermark;
205 		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
206 			/*
207 			 * XXX KDM fix this!
208 			 */
209 			panic("need to implement bus address support");
210 #if 0
211 			kern_ptr = bus_to_virt(kern_sglist[j].addr);
212 #endif
213 		} else
214 			kern_ptr = (uint8_t *)kern_sglist[j].addr;
215 		kern_ptr = kern_ptr + kern_watermark;
216 
217 		if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
218 		     CTL_FLAG_DATA_IN) {
219 			CTL_DEBUG_PRINT(("%s: copying %d bytes to user\n",
220 					 __func__, len_to_copy));
221 			CTL_DEBUG_PRINT(("%s: from %p to %p\n", __func__,
222 					 kern_ptr, ext_ptr));
223 			memcpy(ext_ptr, kern_ptr, len_to_copy);
224 		} else {
225 			CTL_DEBUG_PRINT(("%s: copying %d bytes from user\n",
226 					 __func__, len_to_copy));
227 			CTL_DEBUG_PRINT(("%s: from %p to %p\n", __func__,
228 					 ext_ptr, kern_ptr));
229 			memcpy(kern_ptr, ext_ptr, len_to_copy);
230 		}
231 
232 		ctsio->ext_data_filled += len_to_copy;
233 		ctsio->kern_data_resid -= len_to_copy;
234 
235 		ext_watermark += len_to_copy;
236 		if (ext_sglist[i].len == ext_watermark) {
237 			i++;
238 			ext_watermark = 0;
239 		}
240 
241 		kern_watermark += len_to_copy;
242 		if (kern_sglist[j].len == kern_watermark) {
243 			j++;
244 			kern_watermark = 0;
245 		}
246 	}
247 
248 	CTL_DEBUG_PRINT(("%s: ext_sg_entries: %d, kern_sg_entries: %d\n",
249 			 __func__, ext_sg_entries, kern_sg_entries));
250 	CTL_DEBUG_PRINT(("%s: ext_data_len = %d, kern_data_len = %d\n",
251 			 __func__, ctsio->ext_data_len, ctsio->kern_data_len));
252 
253 bailout:
254 	ctl_datamove_done(io, true);
255 }
256 
257 static void
258 tpcl_done(union ctl_io *io)
259 {
260 
261 	tpc_done(io);
262 }
263 
264 uint64_t
265 tpcl_resolve(struct ctl_softc *softc, int init_port,
266     struct scsi_ec_cscd *cscd, uint32_t *ss, uint32_t *ps, uint32_t *pso)
267 {
268 	struct scsi_ec_cscd_id *cscdid;
269 	struct ctl_port *port;
270 	struct ctl_lun *lun;
271 	uint64_t lunid = UINT64_MAX;
272 
273 	if (cscd->type_code != EC_CSCD_ID ||
274 	    (cscd->luidt_pdt & EC_LUIDT_MASK) != EC_LUIDT_LUN ||
275 	    (cscd->luidt_pdt & EC_NUL) != 0)
276 		return (lunid);
277 
278 	cscdid = (struct scsi_ec_cscd_id *)cscd;
279 	mtx_lock(&softc->ctl_lock);
280 	if (init_port >= 0)
281 		port = softc->ctl_ports[init_port];
282 	else
283 		port = NULL;
284 	STAILQ_FOREACH(lun, &softc->lun_list, links) {
285 		if (port != NULL &&
286 		    ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
287 			continue;
288 		if (lun->lun_devid == NULL)
289 			continue;
290 		if (scsi_devid_match(lun->lun_devid->data,
291 		    lun->lun_devid->len, &cscdid->codeset,
292 		    cscdid->length + 4) == 0) {
293 			lunid = lun->lun;
294 			if (ss)
295 				*ss = lun->be_lun->blocksize;
296 			if (ps)
297 				*ps = lun->be_lun->blocksize <<
298 				    lun->be_lun->pblockexp;
299 			if (pso)
300 				*pso = lun->be_lun->blocksize *
301 				    lun->be_lun->pblockoff;
302 			break;
303 		}
304 	}
305 	mtx_unlock(&softc->ctl_lock);
306 	return (lunid);
307 };
308 
309 union ctl_io *
310 tpcl_alloc_io(void)
311 {
312 	struct tpcl_softc *tsoftc = &tpcl_softc;
313 
314 	return (ctl_alloc_io(tsoftc->port.ctl_pool_ref));
315 };
316 
317 int
318 tpcl_queue(union ctl_io *io, uint64_t lun)
319 {
320 	struct tpcl_softc *tsoftc = &tpcl_softc;
321 
322 	io->io_hdr.nexus.initid = 0;
323 	io->io_hdr.nexus.targ_port = tsoftc->port.targ_port;
324 	io->io_hdr.nexus.targ_lun = lun;
325 	io->scsiio.tag_num = atomic_fetchadd_int(&tsoftc->cur_tag_num, 1);
326 	io->scsiio.ext_data_filled = 0;
327 	return (ctl_queue(io));
328 }
329