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