ctl_tpc_local.c revision 269296
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: stable/10/sys/cam/ctl/ctl_tpc_local.c 269296 2014-07-30 07:18:32Z mav $");
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_frontend_internal.h>
51#include <cam/ctl/ctl_util.h>
52#include <cam/ctl/ctl_backend.h>
53#include <cam/ctl/ctl_ioctl.h>
54#include <cam/ctl/ctl_ha.h>
55#include <cam/ctl/ctl_private.h>
56#include <cam/ctl/ctl_debug.h>
57#include <cam/ctl/ctl_scsi_all.h>
58#include <cam/ctl/ctl_tpc.h>
59#include <cam/ctl/ctl_error.h>
60
61struct tpcl_softc {
62	struct ctl_port port;
63	int cur_tag_num;
64};
65
66extern struct ctl_softc *control_softc;
67static struct tpcl_softc tpcl_softc;
68
69static int tpcl_init(void);
70static void tpcl_shutdown(void);
71static void tpcl_online(void *arg);
72static void tpcl_offline(void *arg);
73static int tpcl_lun_enable(void *arg, struct ctl_id target_id, int lun_id);
74static int tpcl_lun_disable(void *arg, struct ctl_id target_id, int lun_id);
75static void tpcl_datamove(union ctl_io *io);
76static void tpcl_done(union ctl_io *io);
77
78
79static struct ctl_frontend tpcl_frontend =
80{
81	.name = "tpc",
82	.init = tpcl_init,
83	.shutdown = tpcl_shutdown,
84};
85CTL_FRONTEND_DECLARE(ctltpc, tpcl_frontend);
86
87static int
88tpcl_init(void)
89{
90	struct ctl_softc *softc = control_softc;
91	struct tpcl_softc *tsoftc = &tpcl_softc;
92	struct ctl_port *port;
93	struct scsi_transportid_spi *tid;
94	int len;
95
96	memset(tsoftc, 0, sizeof(*tsoftc));
97
98	port = &tsoftc->port;
99	port->frontend = &tpcl_frontend;
100	port->port_type = CTL_PORT_INTERNAL;
101	port->num_requested_ctl_io = 100;
102	port->port_name = "tpc";
103	port->port_online = tpcl_online;
104	port->port_offline = tpcl_offline;
105	port->onoff_arg = tsoftc;
106	port->lun_enable = tpcl_lun_enable;
107	port->lun_disable = tpcl_lun_disable;
108	port->targ_lun_arg = tsoftc;
109	port->fe_datamove = tpcl_datamove;
110	port->fe_done = tpcl_done;
111	port->max_targets = 1;
112	port->max_target_id = 0;
113	port->max_initiators = 1;
114
115	if (ctl_port_register(port, (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0)
116	{
117		printf("%s: tpc frontend registration failed\n", __func__);
118		return (0);
119	}
120
121	len = sizeof(struct scsi_transportid_spi);
122	port->init_devid = malloc(sizeof(struct ctl_devid) + len,
123	    M_CTL, M_WAITOK | M_ZERO);
124	port->init_devid->len = len;
125	tid = (struct scsi_transportid_spi *)port->init_devid->data;
126	tid->format_protocol = SCSI_TRN_SPI_FORMAT_DEFAULT | SCSI_PROTO_SPI;
127	scsi_ulto2b(0, tid->scsi_addr);
128	scsi_ulto2b(port->targ_port, tid->rel_trgt_port_id);
129
130	ctl_port_online(port);
131	return (0);
132}
133
134void
135tpcl_shutdown(void)
136{
137	struct tpcl_softc *tsoftc = &tpcl_softc;
138	struct ctl_port *port;
139
140	port = &tsoftc->port;
141	ctl_port_offline(port);
142	if (ctl_port_deregister(&tsoftc->port) != 0)
143		printf("%s: ctl_frontend_deregister() failed\n", __func__);
144}
145
146static void
147tpcl_online(void *arg)
148{
149}
150
151static void
152tpcl_offline(void *arg)
153{
154}
155
156static int
157tpcl_lun_enable(void *arg, struct ctl_id target_id, int lun_id)
158{
159
160	return (0);
161}
162
163static int
164tpcl_lun_disable(void *arg, struct ctl_id target_id, int lun_id)
165{
166
167	return (0);
168}
169
170static void
171tpcl_datamove(union ctl_io *io)
172{
173	struct ctl_sg_entry *ext_sglist, *kern_sglist;
174	struct ctl_sg_entry ext_entry, kern_entry;
175	int ext_sg_entries, kern_sg_entries;
176	int ext_sg_start, ext_offset;
177	int len_to_copy, len_copied;
178	int kern_watermark, ext_watermark;
179	struct ctl_scsiio *ctsio;
180	int i, j;
181
182	ext_sg_start = 0;
183	ext_offset = 0;
184	ext_sglist = NULL;
185
186	CTL_DEBUG_PRINT(("%s\n", __func__));
187
188	ctsio = &io->scsiio;
189
190	/*
191	 * If this is the case, we're probably doing a BBR read and don't
192	 * actually need to transfer the data.  This will effectively
193	 * bit-bucket the data.
194	 */
195	if (ctsio->ext_data_ptr == NULL)
196		goto bailout;
197
198	/*
199	 * To simplify things here, if we have a single buffer, stick it in
200	 * a S/G entry and just make it a single entry S/G list.
201	 */
202	if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
203		int len_seen;
204
205		ext_sglist = (struct ctl_sg_entry *)ctsio->ext_data_ptr;
206		ext_sg_entries = ctsio->ext_sg_entries;
207		ext_sg_start = 0;
208		ext_offset = 0;
209		len_seen = 0;
210		for (i = 0; i < ext_sg_entries; i++) {
211			if ((len_seen + ext_sglist[i].len) >=
212			     ctsio->ext_data_filled) {
213				ext_sg_start = i;
214				ext_offset = ctsio->ext_data_filled - len_seen;
215				break;
216			}
217			len_seen += ext_sglist[i].len;
218		}
219	} else {
220		ext_sglist = &ext_entry;
221		ext_sglist->addr = ctsio->ext_data_ptr;
222		ext_sglist->len = ctsio->ext_data_len;
223		ext_sg_entries = 1;
224		ext_sg_start = 0;
225		ext_offset = ctsio->ext_data_filled;
226	}
227
228	if (ctsio->kern_sg_entries > 0) {
229		kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
230		kern_sg_entries = ctsio->kern_sg_entries;
231	} else {
232		kern_sglist = &kern_entry;
233		kern_sglist->addr = ctsio->kern_data_ptr;
234		kern_sglist->len = ctsio->kern_data_len;
235		kern_sg_entries = 1;
236	}
237
238	kern_watermark = 0;
239	ext_watermark = ext_offset;
240	len_copied = 0;
241	for (i = ext_sg_start, j = 0;
242	     i < ext_sg_entries && j < kern_sg_entries;) {
243		uint8_t *ext_ptr, *kern_ptr;
244
245		len_to_copy = min(ext_sglist[i].len - ext_watermark,
246				  kern_sglist[j].len - kern_watermark);
247
248		ext_ptr = (uint8_t *)ext_sglist[i].addr;
249		ext_ptr = ext_ptr + ext_watermark;
250		if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
251			/*
252			 * XXX KDM fix this!
253			 */
254			panic("need to implement bus address support");
255#if 0
256			kern_ptr = bus_to_virt(kern_sglist[j].addr);
257#endif
258		} else
259			kern_ptr = (uint8_t *)kern_sglist[j].addr;
260		kern_ptr = kern_ptr + kern_watermark;
261
262		kern_watermark += len_to_copy;
263		ext_watermark += len_to_copy;
264
265		if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
266		     CTL_FLAG_DATA_IN) {
267			CTL_DEBUG_PRINT(("%s: copying %d bytes to user\n",
268					 __func__, len_to_copy));
269			CTL_DEBUG_PRINT(("%s: from %p to %p\n", __func__,
270					 kern_ptr, ext_ptr));
271			memcpy(ext_ptr, kern_ptr, len_to_copy);
272		} else {
273			CTL_DEBUG_PRINT(("%s: copying %d bytes from user\n",
274					 __func__, len_to_copy));
275			CTL_DEBUG_PRINT(("%s: from %p to %p\n", __func__,
276					 ext_ptr, kern_ptr));
277			memcpy(kern_ptr, ext_ptr, len_to_copy);
278		}
279
280		len_copied += len_to_copy;
281
282		if (ext_sglist[i].len == ext_watermark) {
283			i++;
284			ext_watermark = 0;
285		}
286
287		if (kern_sglist[j].len == kern_watermark) {
288			j++;
289			kern_watermark = 0;
290		}
291	}
292
293	ctsio->ext_data_filled += len_copied;
294
295	CTL_DEBUG_PRINT(("%s: ext_sg_entries: %d, kern_sg_entries: %d\n",
296			 __func__, ext_sg_entries, kern_sg_entries));
297	CTL_DEBUG_PRINT(("%s: ext_data_len = %d, kern_data_len = %d\n",
298			 __func__, ctsio->ext_data_len, ctsio->kern_data_len));
299
300	/* XXX KDM set residual?? */
301bailout:
302	io->scsiio.be_move_done(io);
303}
304
305static void
306tpcl_done(union ctl_io *io)
307{
308
309	tpc_done(io);
310}
311
312uint64_t
313tpcl_resolve(int init_port, struct scsi_ec_cscd *cscd, uint32_t *ss)
314{
315	struct ctl_softc *softc = control_softc;
316	struct scsi_ec_cscd_id *cscdid;
317	struct ctl_port *port;
318	struct ctl_lun *lun;
319	uint64_t lunid = UINT64_MAX, l;
320	int i;
321
322	if (cscd->type_code != EC_CSCD_ID)
323		return (lunid);
324
325	cscdid = (struct scsi_ec_cscd_id *)cscd;
326	mtx_lock(&softc->ctl_lock);
327	if (init_port >= 0) {
328		port = softc->ctl_ports[ctl_port_idx(init_port)];
329		if (port == NULL || port->lun_map == NULL)
330			init_port = -1;
331	}
332	if (init_port < 0) {
333		STAILQ_FOREACH(lun, &softc->lun_list, links) {
334			if (lun->lun_devid == NULL)
335				continue;
336			if (scsi_devid_match(lun->lun_devid->data,
337			    lun->lun_devid->len, &cscdid->codeset,
338			    cscdid->length + 4) == 0) {
339				lunid = lun->lun;
340				if (ss && lun->be_lun)
341					*ss = lun->be_lun->blocksize;
342				break;
343			}
344		}
345	} else {
346		for (i = 0; i < CTL_MAX_LUNS; i++) {
347			l = port->lun_map(port->targ_lun_arg, i);
348			if (l >= CTL_MAX_LUNS)
349				continue;
350			lun = softc->ctl_luns[l];
351			if (lun == NULL || lun->lun_devid == NULL)
352				continue;
353			if (scsi_devid_match(lun->lun_devid->data,
354			    lun->lun_devid->len, &cscdid->codeset,
355			    cscdid->length + 4) == 0) {
356				lunid = lun->lun;
357				if (ss && lun->be_lun)
358					*ss = lun->be_lun->blocksize;
359				break;
360			}
361		}
362	}
363	mtx_unlock(&softc->ctl_lock);
364	return (lunid);
365};
366
367union ctl_io *
368tpcl_alloc_io(void)
369{
370	struct tpcl_softc *tsoftc = &tpcl_softc;
371
372	return (ctl_alloc_io(tsoftc->port.ctl_pool_ref));
373};
374
375int
376tpcl_queue(union ctl_io *io, uint64_t lun)
377{
378	struct tpcl_softc *tsoftc = &tpcl_softc;
379
380	io->io_hdr.nexus.initid.id = 0;
381	io->io_hdr.nexus.targ_port = tsoftc->port.targ_port;
382	io->io_hdr.nexus.targ_target.id = 0;
383	io->io_hdr.nexus.targ_lun = lun;
384	io->scsiio.tag_num = atomic_fetchadd_int(&tsoftc->cur_tag_num, 1);
385	io->scsiio.ext_data_filled = 0;
386	return (ctl_queue(io));
387}
388