1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2009 Silicon Graphics International Corp.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 * substantially similar to the "NO WARRANTY" disclaimer below
15 * ("Disclaimer") and any redistribution must be conditioned upon
16 * including a substantially similar Disclaimer requirement for further
17 * binary redistribution.
18 *
19 * NO WARRANTY
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGES.
31 *
32 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_frontend_cam_sim.c#4 $
33 */
34 /*
35 * CTL frontend to CAM SIM interface. This allows access to CTL LUNs via
36 * the da(4) and pass(4) drivers from inside the system.
37 *
38 * Author: Ken Merry <ken@FreeBSD.org>
39 */
40
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD: stable/12/sys/cam/ctl/ctl_frontend_cam_sim.c 369451 2021-03-15 03:04:14Z mav $");
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/types.h>
48 #include <sys/malloc.h>
49 #include <sys/bus.h>
50 #include <sys/sysctl.h>
51 #include <machine/atomic.h>
52 #include <machine/bus.h>
53 #include <sys/sbuf.h>
54
55 #include <cam/cam.h>
56 #include <cam/cam_ccb.h>
57 #include <cam/cam_sim.h>
58 #include <cam/cam_xpt_sim.h>
59 #include <cam/cam_xpt.h>
60 #include <cam/cam_periph.h>
61 #include <cam/scsi/scsi_all.h>
62 #include <cam/scsi/scsi_message.h>
63 #include <cam/ctl/ctl_io.h>
64 #include <cam/ctl/ctl.h>
65 #include <cam/ctl/ctl_frontend.h>
66 #include <cam/ctl/ctl_debug.h>
67
68 #define io_ptr spriv_ptr1
69
70 struct cfcs_io {
71 union ccb *ccb;
72 };
73
74 struct cfcs_softc {
75 struct ctl_port port;
76 char port_name[32];
77 struct cam_sim *sim;
78 struct cam_devq *devq;
79 struct cam_path *path;
80 uint64_t wwnn;
81 uint64_t wwpn;
82 uint32_t cur_tag_num;
83 int online;
84 };
85
86 /*
87 * We can't handle CCBs with these flags. For the most part, we just don't
88 * handle physical addresses yet. That would require mapping things in
89 * order to do the copy.
90 */
91 #define CFCS_BAD_CCB_FLAGS (CAM_DATA_ISPHYS | CAM_MSG_BUF_PHYS | \
92 CAM_SNS_BUF_PHYS | CAM_CDB_PHYS | CAM_SENSE_PTR | \
93 CAM_SENSE_PHYS)
94
95 static int cfcs_init(void);
96 static int cfcs_shutdown(void);
97 static void cfcs_poll(struct cam_sim *sim);
98 static void cfcs_online(void *arg);
99 static void cfcs_offline(void *arg);
100 static void cfcs_datamove(union ctl_io *io);
101 static void cfcs_done(union ctl_io *io);
102 void cfcs_action(struct cam_sim *sim, union ccb *ccb);
103
104 struct cfcs_softc cfcs_softc;
105 /*
106 * This is primarily intended to allow for error injection to test the CAM
107 * sense data and sense residual handling code. This sets the maximum
108 * amount of SCSI sense data that we will report to CAM.
109 */
110 static int cfcs_max_sense = sizeof(struct scsi_sense_data);
111
112 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl2cam, CTLFLAG_RD, 0,
113 "CAM Target Layer SIM frontend");
114 SYSCTL_INT(_kern_cam_ctl2cam, OID_AUTO, max_sense, CTLFLAG_RW,
115 &cfcs_max_sense, 0, "Maximum sense data size");
116
117 static struct ctl_frontend cfcs_frontend =
118 {
119 .name = "camsim",
120 .init = cfcs_init,
121 .shutdown = cfcs_shutdown,
122 };
123 CTL_FRONTEND_DECLARE(ctlcfcs, cfcs_frontend);
124
125 static int
cfcs_init(void)126 cfcs_init(void)
127 {
128 struct cfcs_softc *softc;
129 struct ctl_port *port;
130 int retval;
131
132 softc = &cfcs_softc;
133 bzero(softc, sizeof(*softc));
134 port = &softc->port;
135
136 port->frontend = &cfcs_frontend;
137 port->port_type = CTL_PORT_INTERNAL;
138 /* XXX KDM what should the real number be here? */
139 port->num_requested_ctl_io = 4096;
140 snprintf(softc->port_name, sizeof(softc->port_name), "camsim");
141 port->port_name = softc->port_name;
142 port->port_online = cfcs_online;
143 port->port_offline = cfcs_offline;
144 port->onoff_arg = softc;
145 port->fe_datamove = cfcs_datamove;
146 port->fe_done = cfcs_done;
147 port->targ_port = -1;
148
149 retval = ctl_port_register(port);
150 if (retval != 0) {
151 printf("%s: ctl_port_register() failed with error %d!\n",
152 __func__, retval);
153 return (retval);
154 }
155
156 /*
157 * If the CTL frontend didn't tell us what our WWNN/WWPN is, go
158 * ahead and set something random.
159 */
160 if (port->wwnn == 0) {
161 uint64_t random_bits;
162
163 arc4rand(&random_bits, sizeof(random_bits), 0);
164 softc->wwnn = (random_bits & 0x0000000fffffff00ULL) |
165 /* Company ID */ 0x5000000000000000ULL |
166 /* NL-Port */ 0x0300;
167 softc->wwpn = softc->wwnn + port->targ_port + 1;
168 ctl_port_set_wwns(port, true, softc->wwnn, true, softc->wwpn);
169 } else {
170 softc->wwnn = port->wwnn;
171 softc->wwpn = port->wwpn;
172 }
173
174 softc->devq = cam_simq_alloc(port->num_requested_ctl_io);
175 if (softc->devq == NULL) {
176 printf("%s: error allocating devq\n", __func__);
177 retval = ENOMEM;
178 goto bailout;
179 }
180
181 softc->sim = cam_sim_alloc(cfcs_action, cfcs_poll, softc->port_name,
182 softc, /*unit*/ 0, NULL, 1,
183 port->num_requested_ctl_io, softc->devq);
184 if (softc->sim == NULL) {
185 printf("%s: error allocating SIM\n", __func__);
186 retval = ENOMEM;
187 goto bailout;
188 }
189
190 if (xpt_bus_register(softc->sim, NULL, 0) != CAM_SUCCESS) {
191 printf("%s: error registering SIM\n", __func__);
192 retval = ENOMEM;
193 goto bailout;
194 }
195
196 if (xpt_create_path(&softc->path, /*periph*/NULL,
197 cam_sim_path(softc->sim),
198 CAM_TARGET_WILDCARD,
199 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
200 printf("%s: error creating path\n", __func__);
201 xpt_bus_deregister(cam_sim_path(softc->sim));
202 retval = EINVAL;
203 goto bailout;
204 }
205
206 return (retval);
207
208 bailout:
209 if (softc->sim)
210 cam_sim_free(softc->sim, /*free_devq*/ TRUE);
211 else if (softc->devq)
212 cam_simq_free(softc->devq);
213 return (retval);
214 }
215
216 static int
cfcs_shutdown(void)217 cfcs_shutdown(void)
218 {
219 struct cfcs_softc *softc = &cfcs_softc;
220 struct ctl_port *port = &softc->port;
221 int error;
222
223 ctl_port_offline(port);
224
225 xpt_free_path(softc->path);
226 xpt_bus_deregister(cam_sim_path(softc->sim));
227 cam_sim_free(softc->sim, /*free_devq*/ TRUE);
228
229 if ((error = ctl_port_deregister(port)) != 0)
230 printf("%s: cam_sim port deregistration failed\n", __func__);
231 return (error);
232 }
233
234 static void
cfcs_poll(struct cam_sim * sim)235 cfcs_poll(struct cam_sim *sim)
236 {
237
238 }
239
240 static void
cfcs_onoffline(void * arg,int online)241 cfcs_onoffline(void *arg, int online)
242 {
243 struct cfcs_softc *softc = (struct cfcs_softc *)arg;
244 union ccb *ccb;
245
246 softc->online = online;
247
248 ccb = xpt_alloc_ccb_nowait();
249 if (ccb == NULL) {
250 printf("%s: unable to allocate CCB for rescan\n", __func__);
251 return;
252 }
253
254 if (xpt_create_path(&ccb->ccb_h.path, NULL,
255 cam_sim_path(softc->sim), CAM_TARGET_WILDCARD,
256 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
257 printf("%s: can't allocate path for rescan\n", __func__);
258 xpt_free_ccb(ccb);
259 return;
260 }
261 xpt_rescan(ccb);
262 }
263
264 static void
cfcs_online(void * arg)265 cfcs_online(void *arg)
266 {
267 cfcs_onoffline(arg, /*online*/ 1);
268 }
269
270 static void
cfcs_offline(void * arg)271 cfcs_offline(void *arg)
272 {
273 cfcs_onoffline(arg, /*online*/ 0);
274 }
275
276 /*
277 * This function is very similar to ctl_ioctl_do_datamove(). Is there a
278 * way to combine the functionality?
279 *
280 * XXX KDM may need to move this into a thread. We're doing a bcopy in the
281 * caller's context, which will usually be the backend. That may not be a
282 * good thing.
283 */
284 static void
cfcs_datamove(union ctl_io * io)285 cfcs_datamove(union ctl_io *io)
286 {
287 union ccb *ccb;
288 bus_dma_segment_t cam_sg_entry, *cam_sglist;
289 struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
290 int cam_sg_count, ctl_sg_count, cam_sg_start;
291 int cam_sg_offset;
292 int len_to_copy;
293 int ctl_watermark, cam_watermark;
294 int i, j;
295
296 ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
297
298 /*
299 * Note that we have a check in cfcs_action() to make sure that any
300 * CCBs with "bad" flags are returned with CAM_REQ_INVALID. This
301 * is just to make sure no one removes that check without updating
302 * this code to provide the additional functionality necessary to
303 * support those modes of operation.
304 */
305 KASSERT(((ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS) == 0), ("invalid "
306 "CAM flags %#x", (ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS)));
307
308 /*
309 * Simplify things on both sides by putting single buffers into a
310 * single entry S/G list.
311 */
312 switch ((ccb->ccb_h.flags & CAM_DATA_MASK)) {
313 case CAM_DATA_SG: {
314 int len_seen;
315
316 cam_sglist = (bus_dma_segment_t *)ccb->csio.data_ptr;
317 cam_sg_count = ccb->csio.sglist_cnt;
318 cam_sg_start = cam_sg_count;
319 cam_sg_offset = 0;
320
321 for (i = 0, len_seen = 0; i < cam_sg_count; i++) {
322 if ((len_seen + cam_sglist[i].ds_len) >=
323 io->scsiio.kern_rel_offset) {
324 cam_sg_start = i;
325 cam_sg_offset = io->scsiio.kern_rel_offset -
326 len_seen;
327 break;
328 }
329 len_seen += cam_sglist[i].ds_len;
330 }
331 break;
332 }
333 case CAM_DATA_VADDR:
334 cam_sglist = &cam_sg_entry;
335 cam_sglist[0].ds_len = ccb->csio.dxfer_len;
336 cam_sglist[0].ds_addr = (bus_addr_t)(uintptr_t)ccb->csio.data_ptr;
337 cam_sg_count = 1;
338 cam_sg_start = 0;
339 cam_sg_offset = io->scsiio.kern_rel_offset;
340 break;
341 default:
342 panic("Invalid CAM flags %#x", ccb->ccb_h.flags);
343 }
344
345 if (io->scsiio.kern_sg_entries > 0) {
346 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
347 ctl_sg_count = io->scsiio.kern_sg_entries;
348 } else {
349 ctl_sglist = &ctl_sg_entry;
350 ctl_sglist->addr = io->scsiio.kern_data_ptr;
351 ctl_sglist->len = io->scsiio.kern_data_len;
352 ctl_sg_count = 1;
353 }
354
355 ctl_watermark = 0;
356 cam_watermark = cam_sg_offset;
357 for (i = cam_sg_start, j = 0;
358 i < cam_sg_count && j < ctl_sg_count;) {
359 uint8_t *cam_ptr, *ctl_ptr;
360
361 len_to_copy = MIN(cam_sglist[i].ds_len - cam_watermark,
362 ctl_sglist[j].len - ctl_watermark);
363
364 cam_ptr = (uint8_t *)(uintptr_t)cam_sglist[i].ds_addr;
365 cam_ptr = cam_ptr + cam_watermark;
366 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
367 /*
368 * XXX KDM fix this!
369 */
370 panic("need to implement bus address support");
371 #if 0
372 kern_ptr = bus_to_virt(kern_sglist[j].addr);
373 #endif
374 } else
375 ctl_ptr = (uint8_t *)ctl_sglist[j].addr;
376 ctl_ptr = ctl_ptr + ctl_watermark;
377
378 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
379 CTL_FLAG_DATA_IN) {
380 CTL_DEBUG_PRINT(("%s: copying %d bytes to CAM\n",
381 __func__, len_to_copy));
382 CTL_DEBUG_PRINT(("%s: from %p to %p\n", ctl_ptr,
383 __func__, cam_ptr));
384 bcopy(ctl_ptr, cam_ptr, len_to_copy);
385 } else {
386 CTL_DEBUG_PRINT(("%s: copying %d bytes from CAM\n",
387 __func__, len_to_copy));
388 CTL_DEBUG_PRINT(("%s: from %p to %p\n", cam_ptr,
389 __func__, ctl_ptr));
390 bcopy(cam_ptr, ctl_ptr, len_to_copy);
391 }
392
393 io->scsiio.ext_data_filled += len_to_copy;
394 io->scsiio.kern_data_resid -= len_to_copy;
395
396 cam_watermark += len_to_copy;
397 if (cam_sglist[i].ds_len == cam_watermark) {
398 i++;
399 cam_watermark = 0;
400 }
401
402 ctl_watermark += len_to_copy;
403 if (ctl_sglist[j].len == ctl_watermark) {
404 j++;
405 ctl_watermark = 0;
406 }
407 }
408
409 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
410 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = NULL;
411 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
412 ccb->csio.resid = ccb->csio.dxfer_len -
413 io->scsiio.ext_data_filled;
414 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
415 ccb->ccb_h.status |= CAM_REQ_CMP;
416 xpt_done(ccb);
417 }
418
419 ctl_datamove_done(io, true);
420 }
421
422 static void
cfcs_done(union ctl_io * io)423 cfcs_done(union ctl_io *io)
424 {
425 union ccb *ccb;
426
427 ccb = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
428 if (ccb == NULL) {
429 ctl_free_io(io);
430 return;
431 }
432
433 /*
434 * At this point we should have status. If we don't, that's a bug.
435 */
436 KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
437 ("invalid CTL status %#x", io->io_hdr.status));
438
439 /*
440 * Translate CTL status to CAM status.
441 */
442 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
443 ccb->csio.resid = ccb->csio.dxfer_len -
444 io->scsiio.ext_data_filled;
445 }
446 ccb->ccb_h.status &= ~CAM_STATUS_MASK;
447 switch (io->io_hdr.status & CTL_STATUS_MASK) {
448 case CTL_SUCCESS:
449 ccb->ccb_h.status |= CAM_REQ_CMP;
450 break;
451 case CTL_SCSI_ERROR:
452 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
453 ccb->csio.scsi_status = io->scsiio.scsi_status;
454 bcopy(&io->scsiio.sense_data, &ccb->csio.sense_data,
455 min(io->scsiio.sense_len, ccb->csio.sense_len));
456 if (ccb->csio.sense_len > io->scsiio.sense_len)
457 ccb->csio.sense_resid = ccb->csio.sense_len -
458 io->scsiio.sense_len;
459 else
460 ccb->csio.sense_resid = 0;
461 if ((ccb->csio.sense_len - ccb->csio.sense_resid) >
462 cfcs_max_sense) {
463 ccb->csio.sense_resid = ccb->csio.sense_len -
464 cfcs_max_sense;
465 }
466 break;
467 case CTL_CMD_ABORTED:
468 ccb->ccb_h.status |= CAM_REQ_ABORTED;
469 break;
470 case CTL_ERROR:
471 default:
472 ccb->ccb_h.status |= CAM_REQ_CMP_ERR;
473 break;
474 }
475 ctl_free_io(io);
476 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP &&
477 (ccb->ccb_h.status & CAM_DEV_QFRZN) == 0) {
478 xpt_freeze_devq(ccb->ccb_h.path, 1);
479 ccb->ccb_h.status |= CAM_DEV_QFRZN;
480 }
481 xpt_done(ccb);
482 }
483
484 void
cfcs_action(struct cam_sim * sim,union ccb * ccb)485 cfcs_action(struct cam_sim *sim, union ccb *ccb)
486 {
487 struct cfcs_softc *softc;
488 int err;
489
490 softc = (struct cfcs_softc *)cam_sim_softc(sim);
491
492 switch (ccb->ccb_h.func_code) {
493 case XPT_SCSI_IO: {
494 union ctl_io *io;
495 struct ccb_scsiio *csio;
496
497 csio = &ccb->csio;
498
499 /*
500 * Catch CCB flags, like physical address flags, that
501 * indicate situations we currently can't handle.
502 */
503 if (ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS) {
504 ccb->ccb_h.status = CAM_REQ_INVALID;
505 printf("%s: bad CCB flags %#x (all flags %#x)\n",
506 __func__, ccb->ccb_h.flags & CFCS_BAD_CCB_FLAGS,
507 ccb->ccb_h.flags);
508 xpt_done(ccb);
509 return;
510 }
511
512 /*
513 * If we aren't online, there are no devices to see.
514 */
515 if (softc->online == 0) {
516 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
517 xpt_done(ccb);
518 return;
519 }
520
521 io = ctl_alloc_io_nowait(softc->port.ctl_pool_ref);
522 if (io == NULL) {
523 printf("%s: can't allocate ctl_io\n", __func__);
524 ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
525 xpt_freeze_devq(ccb->ccb_h.path, 1);
526 xpt_done(ccb);
527 return;
528 }
529 ctl_zero_io(io);
530 /* Save pointers on both sides */
531 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ccb;
532 ccb->ccb_h.io_ptr = io;
533
534 /*
535 * Only SCSI I/O comes down this path, resets, etc. come
536 * down via the XPT_RESET_BUS/LUN CCBs below.
537 */
538 io->io_hdr.io_type = CTL_IO_SCSI;
539 io->io_hdr.nexus.initid = 1;
540 io->io_hdr.nexus.targ_port = softc->port.targ_port;
541 io->io_hdr.nexus.targ_lun = ctl_decode_lun(
542 CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun));
543 io->scsiio.priority = csio->priority;
544 /*
545 * This tag scheme isn't the best, since we could in theory
546 * have a very long-lived I/O and tag collision, especially
547 * in a high I/O environment. But it should work well
548 * enough for now. Since we're using unsigned ints,
549 * they'll just wrap around.
550 */
551 io->scsiio.tag_num = atomic_fetchadd_32(&softc->cur_tag_num, 1);
552 csio->tag_id = io->scsiio.tag_num;
553 switch (csio->tag_action) {
554 case CAM_TAG_ACTION_NONE:
555 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
556 break;
557 case MSG_SIMPLE_TASK:
558 io->scsiio.tag_type = CTL_TAG_SIMPLE;
559 break;
560 case MSG_HEAD_OF_QUEUE_TASK:
561 io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
562 break;
563 case MSG_ORDERED_TASK:
564 io->scsiio.tag_type = CTL_TAG_ORDERED;
565 break;
566 case MSG_ACA_TASK:
567 io->scsiio.tag_type = CTL_TAG_ACA;
568 break;
569 default:
570 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
571 printf("%s: unhandled tag type %#x!!\n", __func__,
572 csio->tag_action);
573 break;
574 }
575 if (csio->cdb_len > sizeof(io->scsiio.cdb)) {
576 printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
577 __func__, csio->cdb_len, sizeof(io->scsiio.cdb));
578 }
579 io->scsiio.cdb_len = min(csio->cdb_len, sizeof(io->scsiio.cdb));
580 bcopy(scsiio_cdb_ptr(csio), io->scsiio.cdb, io->scsiio.cdb_len);
581
582 ccb->ccb_h.status |= CAM_SIM_QUEUED;
583 err = ctl_queue(io);
584 if (err != CTL_RETVAL_COMPLETE) {
585 printf("%s: func %d: error %d returned by "
586 "ctl_queue()!\n", __func__,
587 ccb->ccb_h.func_code, err);
588 ctl_free_io(io);
589 ccb->ccb_h.status = CAM_REQ_INVALID;
590 xpt_done(ccb);
591 return;
592 }
593 break;
594 }
595 case XPT_ABORT: {
596 union ctl_io *io;
597 union ccb *abort_ccb;
598
599 abort_ccb = ccb->cab.abort_ccb;
600
601 if (abort_ccb->ccb_h.func_code != XPT_SCSI_IO) {
602 ccb->ccb_h.status = CAM_REQ_INVALID;
603 xpt_done(ccb);
604 }
605
606 /*
607 * If we aren't online, there are no devices to talk to.
608 */
609 if (softc->online == 0) {
610 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
611 xpt_done(ccb);
612 return;
613 }
614
615 io = ctl_alloc_io_nowait(softc->port.ctl_pool_ref);
616 if (io == NULL) {
617 ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
618 xpt_freeze_devq(ccb->ccb_h.path, 1);
619 xpt_done(ccb);
620 return;
621 }
622
623 ctl_zero_io(io);
624 /* Save pointers on both sides */
625 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ccb;
626 ccb->ccb_h.io_ptr = io;
627
628 io->io_hdr.io_type = CTL_IO_TASK;
629 io->io_hdr.nexus.initid = 1;
630 io->io_hdr.nexus.targ_port = softc->port.targ_port;
631 io->io_hdr.nexus.targ_lun = ctl_decode_lun(
632 CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun));
633 io->taskio.task_action = CTL_TASK_ABORT_TASK;
634 io->taskio.tag_num = abort_ccb->csio.tag_id;
635 switch (abort_ccb->csio.tag_action) {
636 case CAM_TAG_ACTION_NONE:
637 io->taskio.tag_type = CTL_TAG_UNTAGGED;
638 break;
639 case MSG_SIMPLE_TASK:
640 io->taskio.tag_type = CTL_TAG_SIMPLE;
641 break;
642 case MSG_HEAD_OF_QUEUE_TASK:
643 io->taskio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
644 break;
645 case MSG_ORDERED_TASK:
646 io->taskio.tag_type = CTL_TAG_ORDERED;
647 break;
648 case MSG_ACA_TASK:
649 io->taskio.tag_type = CTL_TAG_ACA;
650 break;
651 default:
652 io->taskio.tag_type = CTL_TAG_UNTAGGED;
653 printf("%s: unhandled tag type %#x!!\n", __func__,
654 abort_ccb->csio.tag_action);
655 break;
656 }
657 err = ctl_queue(io);
658 if (err != CTL_RETVAL_COMPLETE) {
659 printf("%s func %d: error %d returned by "
660 "ctl_queue()!\n", __func__,
661 ccb->ccb_h.func_code, err);
662 ctl_free_io(io);
663 }
664 break;
665 }
666 case XPT_GET_TRAN_SETTINGS: {
667 struct ccb_trans_settings *cts;
668 struct ccb_trans_settings_scsi *scsi;
669 struct ccb_trans_settings_fc *fc;
670
671 cts = &ccb->cts;
672 scsi = &cts->proto_specific.scsi;
673 fc = &cts->xport_specific.fc;
674
675
676 cts->protocol = PROTO_SCSI;
677 cts->protocol_version = SCSI_REV_SPC2;
678 cts->transport = XPORT_FC;
679 cts->transport_version = 0;
680
681 scsi->valid = CTS_SCSI_VALID_TQ;
682 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
683 fc->valid = CTS_FC_VALID_SPEED;
684 fc->bitrate = 800000;
685 fc->wwnn = softc->wwnn;
686 fc->wwpn = softc->wwpn;
687 fc->port = softc->port.targ_port;
688 fc->valid |= CTS_FC_VALID_WWNN | CTS_FC_VALID_WWPN |
689 CTS_FC_VALID_PORT;
690 ccb->ccb_h.status = CAM_REQ_CMP;
691 break;
692 }
693 case XPT_SET_TRAN_SETTINGS:
694 /* XXX KDM should we actually do something here? */
695 ccb->ccb_h.status = CAM_REQ_CMP;
696 break;
697 case XPT_RESET_BUS:
698 case XPT_RESET_DEV: {
699 union ctl_io *io;
700
701 /*
702 * If we aren't online, there are no devices to talk to.
703 */
704 if (softc->online == 0) {
705 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
706 xpt_done(ccb);
707 return;
708 }
709
710 io = ctl_alloc_io_nowait(softc->port.ctl_pool_ref);
711 if (io == NULL) {
712 ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
713 xpt_freeze_devq(ccb->ccb_h.path, 1);
714 xpt_done(ccb);
715 return;
716 }
717
718 ctl_zero_io(io);
719 /* Save pointers on both sides */
720 if (ccb->ccb_h.func_code == XPT_RESET_DEV)
721 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ccb;
722 ccb->ccb_h.io_ptr = io;
723
724 io->io_hdr.io_type = CTL_IO_TASK;
725 io->io_hdr.nexus.initid = 1;
726 io->io_hdr.nexus.targ_port = softc->port.targ_port;
727 io->io_hdr.nexus.targ_lun = ctl_decode_lun(
728 CAM_EXTLUN_BYTE_SWIZZLE(ccb->ccb_h.target_lun));
729 if (ccb->ccb_h.func_code == XPT_RESET_BUS)
730 io->taskio.task_action = CTL_TASK_BUS_RESET;
731 else
732 io->taskio.task_action = CTL_TASK_LUN_RESET;
733
734 err = ctl_queue(io);
735 if (err != CTL_RETVAL_COMPLETE) {
736 printf("%s func %d: error %d returned by "
737 "ctl_queue()!\n", __func__,
738 ccb->ccb_h.func_code, err);
739 ctl_free_io(io);
740 }
741 break;
742 }
743 case XPT_CALC_GEOMETRY:
744 cam_calc_geometry(&ccb->ccg, 1);
745 xpt_done(ccb);
746 break;
747 case XPT_PATH_INQ: {
748 struct ccb_pathinq *cpi;
749
750 cpi = &ccb->cpi;
751
752 cpi->version_num = 0;
753 cpi->hba_inquiry = PI_TAG_ABLE;
754 cpi->target_sprt = 0;
755 cpi->hba_misc = PIM_EXTLUNS;
756 cpi->hba_eng_cnt = 0;
757 cpi->max_target = 0;
758 cpi->max_lun = 1024;
759 /* Do we really have a limit? */
760 cpi->maxio = 1024 * 1024;
761 cpi->async_flags = 0;
762 cpi->hpath_id = 0;
763 cpi->initiator_id = 1;
764
765 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
766 strlcpy(cpi->hba_vid, "FreeBSD", HBA_IDLEN);
767 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
768 cpi->unit_number = 0;
769 cpi->bus_id = 0;
770 cpi->base_transfer_speed = 800000;
771 cpi->protocol = PROTO_SCSI;
772 cpi->protocol_version = SCSI_REV_SPC2;
773 /*
774 * Pretend to be Fibre Channel.
775 */
776 cpi->transport = XPORT_FC;
777 cpi->transport_version = 0;
778 cpi->xport_specific.fc.wwnn = softc->wwnn;
779 cpi->xport_specific.fc.wwpn = softc->wwpn;
780 cpi->xport_specific.fc.port = softc->port.targ_port;
781 cpi->xport_specific.fc.bitrate = 8 * 1000 * 1000;
782 cpi->ccb_h.status = CAM_REQ_CMP;
783 break;
784 }
785 default:
786 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
787 printf("%s: unsupported CCB type %#x\n", __func__,
788 ccb->ccb_h.func_code);
789 xpt_done(ccb);
790 break;
791 }
792 }
793