1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2008, 2009 Silicon Graphics International Corp.
5 * Copyright (c) 2014-2015 Alexander Motin <mav@FreeBSD.org>
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.
14 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
15 * substantially similar to the "NO WARRANTY" disclaimer below
16 * ("Disclaimer") and any redistribution must be conditioned upon
17 * including a substantially similar Disclaimer requirement for further
18 * binary redistribution.
19 *
20 * NO WARRANTY
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGES.
32 *
33 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/scsi_ctl.c#4 $
34 */
35 /*
36 * Peripheral driver interface between CAM and CTL (CAM Target Layer).
37 *
38 * Author: Ken Merry <ken@FreeBSD.org>
39 */
40
41 #include <sys/param.h>
42 #include <sys/queue.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/condvar.h>
48 #include <sys/malloc.h>
49 #include <sys/bus.h>
50 #include <sys/endian.h>
51 #include <sys/sbuf.h>
52 #include <sys/sysctl.h>
53 #include <sys/types.h>
54 #include <sys/systm.h>
55 #include <sys/taskqueue.h>
56 #include <machine/bus.h>
57
58 #include <cam/cam.h>
59 #include <cam/cam_ccb.h>
60 #include <cam/cam_periph.h>
61 #include <cam/cam_queue.h>
62 #include <cam/cam_xpt_periph.h>
63 #include <cam/cam_debug.h>
64 #include <cam/cam_sim.h>
65 #include <cam/cam_xpt.h>
66
67 #include <cam/scsi/scsi_all.h>
68 #include <cam/scsi/scsi_message.h>
69
70 #include <cam/ctl/ctl_io.h>
71 #include <cam/ctl/ctl.h>
72 #include <cam/ctl/ctl_frontend.h>
73 #include <cam/ctl/ctl_util.h>
74 #include <cam/ctl/ctl_error.h>
75
76 struct ctlfe_softc {
77 struct ctl_port port;
78 path_id_t path_id;
79 target_id_t target_id;
80 uint32_t hba_misc;
81 u_int maxio;
82 struct cam_sim *sim;
83 char port_name[DEV_IDLEN];
84 struct mtx lun_softc_mtx;
85 STAILQ_HEAD(, ctlfe_lun_softc) lun_softc_list;
86 STAILQ_ENTRY(ctlfe_softc) links;
87 };
88
89 STAILQ_HEAD(, ctlfe_softc) ctlfe_softc_list;
90 struct mtx ctlfe_list_mtx;
91 static char ctlfe_mtx_desc[] = "ctlfelist";
92
93 typedef enum {
94 CTLFE_LUN_NONE = 0x00,
95 CTLFE_LUN_WILDCARD = 0x01
96 } ctlfe_lun_flags;
97
98 struct ctlfe_lun_softc {
99 struct ctlfe_softc *parent_softc;
100 struct cam_periph *periph;
101 ctlfe_lun_flags flags;
102 int ctios_sent; /* Number of active CTIOs */
103 int refcount; /* Number of active xpt_action() */
104 int atios_alloced; /* Number of ATIOs not freed */
105 int inots_alloced; /* Number of INOTs not freed */
106 struct task refdrain_task;
107 STAILQ_HEAD(, ccb_hdr) work_queue;
108 LIST_HEAD(, ccb_hdr) atio_list; /* List of ATIOs queued to SIM. */
109 LIST_HEAD(, ccb_hdr) inot_list; /* List of INOTs queued to SIM. */
110 STAILQ_ENTRY(ctlfe_lun_softc) links;
111 };
112
113 typedef enum {
114 CTLFE_CMD_NONE = 0x00,
115 CTLFE_CMD_PIECEWISE = 0x01
116 } ctlfe_cmd_flags;
117
118 struct ctlfe_cmd_info {
119 int cur_transfer_index;
120 size_t cur_transfer_off;
121 ctlfe_cmd_flags flags;
122 /*
123 * XXX KDM struct bus_dma_segment is 8 bytes on i386, and 16
124 * bytes on amd64. So with 32 elements, this is 256 bytes on
125 * i386 and 512 bytes on amd64.
126 */
127 #define CTLFE_MAX_SEGS 32
128 bus_dma_segment_t cam_sglist[CTLFE_MAX_SEGS];
129 };
130
131 /*
132 * When we register the adapter/bus, request that this many ctl_ios be
133 * allocated. This should be the maximum supported by the adapter, but we
134 * currently don't have a way to get that back from the path inquiry.
135 * XXX KDM add that to the path inquiry.
136 */
137 #define CTLFE_REQ_CTL_IO 4096
138 /*
139 * Number of Accept Target I/O CCBs to allocate and queue down to the
140 * adapter per LUN.
141 * XXX KDM should this be controlled by CTL?
142 */
143 #define CTLFE_ATIO_PER_LUN 1024
144 /*
145 * Number of Immediate Notify CCBs (used for aborts, resets, etc.) to
146 * allocate and queue down to the adapter per LUN.
147 * XXX KDM should this be controlled by CTL?
148 */
149 #define CTLFE_IN_PER_LUN 1024
150
151 /*
152 * Timeout (in seconds) on CTIO CCB doing DMA or sending status
153 */
154 #define CTLFE_TIMEOUT 5
155
156 /*
157 * Turn this on to enable extra debugging prints.
158 */
159 #if 0
160 #define CTLFE_DEBUG
161 #endif
162
163 MALLOC_DEFINE(M_CTLFE, "CAM CTL FE", "CAM CTL FE interface");
164
165 #define io_ptr ppriv_ptr0
166
167 /* This is only used in the CTIO */
168 #define ccb_atio ppriv_ptr1
169
170 #define PRIV_CCB(io) ((io)->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptrs[0])
171 #define PRIV_INFO(io) ((io)->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptrs[1])
172
173 static int ctlfeinitialize(void);
174 static int ctlfeshutdown(void);
175 static periph_init_t ctlfeperiphinit;
176 static periph_deinit_t ctlfeperiphdeinit;
177 static void ctlfeasync(void *callback_arg, uint32_t code,
178 struct cam_path *path, void *arg);
179 static periph_ctor_t ctlferegister;
180 static periph_oninv_t ctlfeoninvalidate;
181 static periph_dtor_t ctlfecleanup;
182 static periph_start_t ctlfestart;
183 static void ctlfedone(struct cam_periph *periph,
184 union ccb *done_ccb);
185
186 static void ctlfe_onoffline(void *arg, int online);
187 static void ctlfe_online(void *arg);
188 static void ctlfe_offline(void *arg);
189 static int ctlfe_lun_enable(void *arg, int lun_id);
190 static int ctlfe_lun_disable(void *arg, int lun_id);
191 static void ctlfe_dump_sim(struct cam_sim *sim);
192 static void ctlfe_dump_queue(struct ctlfe_lun_softc *softc);
193 static void ctlfe_datamove(union ctl_io *io);
194 static void ctlfe_done(union ctl_io *io);
195 static void ctlfe_dump(void);
196 static void ctlfe_free_ccb(struct cam_periph *periph,
197 union ccb *ccb);
198 static void ctlfe_requeue_ccb(struct cam_periph *periph,
199 union ccb *ccb, int unlock);
200
201 static struct periph_driver ctlfe_driver =
202 {
203 ctlfeperiphinit, "ctl",
204 TAILQ_HEAD_INITIALIZER(ctlfe_driver.units), /*generation*/ 0,
205 CAM_PERIPH_DRV_EARLY,
206 ctlfeperiphdeinit
207 };
208
209 static struct ctl_frontend ctlfe_frontend =
210 {
211 .name = "camtgt",
212 .init = ctlfeinitialize,
213 .fe_dump = ctlfe_dump,
214 .shutdown = ctlfeshutdown,
215 };
216 CTL_FRONTEND_DECLARE(ctlfe, ctlfe_frontend);
217
218 static int
ctlfeinitialize(void)219 ctlfeinitialize(void)
220 {
221
222 STAILQ_INIT(&ctlfe_softc_list);
223 mtx_init(&ctlfe_list_mtx, ctlfe_mtx_desc, NULL, MTX_DEF);
224 periphdriver_register(&ctlfe_driver);
225 return (0);
226 }
227
228 static int
ctlfeshutdown(void)229 ctlfeshutdown(void)
230 {
231 int error;
232
233 error = periphdriver_unregister(&ctlfe_driver);
234 if (error != 0)
235 return (error);
236 mtx_destroy(&ctlfe_list_mtx);
237 return (0);
238 }
239
240 static void
ctlfeperiphinit(void)241 ctlfeperiphinit(void)
242 {
243 cam_status status;
244
245 status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED |
246 AC_CONTRACT, ctlfeasync, NULL, NULL);
247 if (status != CAM_REQ_CMP) {
248 printf("ctl: Failed to attach async callback due to CAM "
249 "status 0x%x!\n", status);
250 }
251 }
252
253 static int
ctlfeperiphdeinit(void)254 ctlfeperiphdeinit(void)
255 {
256
257 /* XXX: It would be good to tear down active ports here. */
258 if (!TAILQ_EMPTY(&ctlfe_driver.units))
259 return (EBUSY);
260 xpt_register_async(0, ctlfeasync, NULL, NULL);
261 return (0);
262 }
263
264 static void
ctlfeasync(void * callback_arg,uint32_t code,struct cam_path * path,void * arg)265 ctlfeasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
266 {
267 struct ctlfe_softc *softc;
268
269 #ifdef CTLFEDEBUG
270 printf("%s: entered\n", __func__);
271 #endif
272
273 mtx_lock(&ctlfe_list_mtx);
274 STAILQ_FOREACH(softc, &ctlfe_softc_list, links) {
275 if (softc->path_id == xpt_path_path_id(path))
276 break;
277 }
278 mtx_unlock(&ctlfe_list_mtx);
279
280 /*
281 * When a new path gets registered, and it is capable of target
282 * mode, go ahead and attach. Later on, we may need to be more
283 * selective, but for now this will be sufficient.
284 */
285 switch (code) {
286 case AC_PATH_REGISTERED: {
287 struct ctl_port *port;
288 struct ccb_pathinq *cpi;
289 int retval;
290
291 cpi = (struct ccb_pathinq *)arg;
292
293 /* Don't attach if it doesn't support target mode */
294 if ((cpi->target_sprt & PIT_PROCESSOR) == 0) {
295 #ifdef CTLFEDEBUG
296 printf("%s: SIM %s%d doesn't support target mode\n",
297 __func__, cpi->dev_name, cpi->unit_number);
298 #endif
299 break;
300 }
301
302 if (softc != NULL) {
303 #ifdef CTLFEDEBUG
304 printf("%s: CTL port for CAM path %u already exists\n",
305 __func__, xpt_path_path_id(path));
306 #endif
307 break;
308 }
309
310 /*
311 * We're in an interrupt context here, so we have to
312 * use M_NOWAIT. Of course this means trouble if we
313 * can't allocate memory.
314 */
315 softc = malloc(sizeof(*softc), M_CTLFE, M_NOWAIT | M_ZERO);
316 if (softc == NULL) {
317 printf("%s: unable to malloc %zd bytes for softc\n",
318 __func__, sizeof(*softc));
319 return;
320 }
321
322 softc->path_id = cpi->ccb_h.path_id;
323 softc->target_id = cpi->initiator_id;
324 softc->sim = xpt_path_sim(path);
325 softc->hba_misc = cpi->hba_misc;
326 if (cpi->maxio != 0)
327 softc->maxio = cpi->maxio;
328 else
329 softc->maxio = DFLTPHYS;
330 mtx_init(&softc->lun_softc_mtx, "LUN softc mtx", NULL, MTX_DEF);
331 STAILQ_INIT(&softc->lun_softc_list);
332
333 port = &softc->port;
334 port->frontend = &ctlfe_frontend;
335
336 /*
337 * XXX KDM should we be more accurate here ?
338 */
339 if (cpi->transport == XPORT_FC)
340 port->port_type = CTL_PORT_FC;
341 else if (cpi->transport == XPORT_SAS)
342 port->port_type = CTL_PORT_SAS;
343 else
344 port->port_type = CTL_PORT_SCSI;
345
346 /* XXX KDM what should the real number be here? */
347 port->num_requested_ctl_io = CTLFE_REQ_CTL_IO;
348 snprintf(softc->port_name, sizeof(softc->port_name),
349 "%s%d", cpi->dev_name, cpi->unit_number);
350 /*
351 * XXX KDM it would be nice to allocate storage in the
352 * frontend structure itself.
353 */
354 port->port_name = softc->port_name;
355 port->physical_port = cpi->bus_id;
356 port->virtual_port = 0;
357 port->port_online = ctlfe_online;
358 port->port_offline = ctlfe_offline;
359 port->onoff_arg = softc;
360 port->lun_enable = ctlfe_lun_enable;
361 port->lun_disable = ctlfe_lun_disable;
362 port->targ_lun_arg = softc;
363 port->fe_datamove = ctlfe_datamove;
364 port->fe_done = ctlfe_done;
365 port->targ_port = -1;
366
367 retval = ctl_port_register(port);
368 if (retval != 0) {
369 printf("%s: ctl_port_register() failed with "
370 "error %d!\n", __func__, retval);
371 mtx_destroy(&softc->lun_softc_mtx);
372 free(softc, M_CTLFE);
373 break;
374 } else {
375 mtx_lock(&ctlfe_list_mtx);
376 STAILQ_INSERT_TAIL(&ctlfe_softc_list, softc, links);
377 mtx_unlock(&ctlfe_list_mtx);
378 }
379
380 break;
381 }
382 case AC_PATH_DEREGISTERED: {
383 if (softc != NULL) {
384 /*
385 * XXX KDM are we certain at this point that there
386 * are no outstanding commands for this frontend?
387 */
388 mtx_lock(&ctlfe_list_mtx);
389 STAILQ_REMOVE(&ctlfe_softc_list, softc, ctlfe_softc,
390 links);
391 mtx_unlock(&ctlfe_list_mtx);
392 ctl_port_deregister(&softc->port);
393 mtx_destroy(&softc->lun_softc_mtx);
394 free(softc, M_CTLFE);
395 }
396 break;
397 }
398 case AC_CONTRACT: {
399 struct ac_contract *ac;
400
401 ac = (struct ac_contract *)arg;
402
403 switch (ac->contract_number) {
404 case AC_CONTRACT_DEV_CHG: {
405 struct ac_device_changed *dev_chg;
406 int retval;
407
408 dev_chg = (struct ac_device_changed *)ac->contract_data;
409
410 printf("%s: WWPN %#jx port 0x%06x path %u target %u %s\n",
411 __func__, dev_chg->wwpn, dev_chg->port,
412 xpt_path_path_id(path), dev_chg->target,
413 (dev_chg->arrived == 0) ? "left" : "arrived");
414
415 if (softc == NULL) {
416 printf("%s: CTL port for CAM path %u not "
417 "found!\n", __func__,
418 xpt_path_path_id(path));
419 break;
420 }
421 if (dev_chg->arrived != 0) {
422 retval = ctl_add_initiator(&softc->port,
423 dev_chg->target, dev_chg->wwpn, NULL);
424 } else {
425 retval = ctl_remove_initiator(&softc->port,
426 dev_chg->target);
427 }
428
429 if (retval < 0) {
430 printf("%s: could not %s port %d iid %u "
431 "WWPN %#jx!\n", __func__,
432 (dev_chg->arrived != 0) ? "add" :
433 "remove", softc->port.targ_port,
434 dev_chg->target,
435 (uintmax_t)dev_chg->wwpn);
436 }
437 break;
438 }
439 default:
440 printf("%s: unsupported contract number %ju\n",
441 __func__, (uintmax_t)ac->contract_number);
442 break;
443 }
444 break;
445 }
446 default:
447 break;
448 }
449 }
450
451 static cam_status
ctlferegister(struct cam_periph * periph,void * arg)452 ctlferegister(struct cam_periph *periph, void *arg)
453 {
454 struct ctlfe_softc *bus_softc;
455 struct ctlfe_lun_softc *softc;
456 union ccb ccb;
457 cam_status status;
458 int i, acstatus;
459
460 softc = (struct ctlfe_lun_softc *)arg;
461 bus_softc = softc->parent_softc;
462
463 STAILQ_INIT(&softc->work_queue);
464 LIST_INIT(&softc->atio_list);
465 LIST_INIT(&softc->inot_list);
466 softc->periph = periph;
467 periph->softc = softc;
468
469 /* Increase device openings to maximum for the SIM. */
470 if (bus_softc->sim->max_tagged_dev_openings >
471 bus_softc->sim->max_dev_openings) {
472 cam_release_devq(periph->path,
473 /*relsim_flags*/RELSIM_ADJUST_OPENINGS,
474 /*openings*/bus_softc->sim->max_tagged_dev_openings,
475 /*timeout*/0,
476 /*getcount_only*/1);
477 }
478
479 memset(&ccb, 0, sizeof(ccb));
480 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
481 ccb.ccb_h.func_code = XPT_EN_LUN;
482 ccb.cel.grp6_len = 0;
483 ccb.cel.grp7_len = 0;
484 ccb.cel.enable = 1;
485 xpt_action(&ccb);
486 status = (ccb.ccb_h.status & CAM_STATUS_MASK);
487 if (status != CAM_REQ_CMP) {
488 xpt_print(periph->path, "%s: Enable LUN failed, status 0x%x\n",
489 __func__, ccb.ccb_h.status);
490 return (status);
491 }
492
493 status = CAM_REQ_CMP;
494
495 for (i = 0; i < CTLFE_ATIO_PER_LUN; i++) {
496 union ccb *new_ccb;
497 union ctl_io *new_io;
498 struct ctlfe_cmd_info *cmd_info;
499
500 new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
501 M_ZERO|M_NOWAIT);
502 if (new_ccb == NULL) {
503 status = CAM_RESRC_UNAVAIL;
504 break;
505 }
506 new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref);
507 if (new_io == NULL) {
508 free(new_ccb, M_CTLFE);
509 status = CAM_RESRC_UNAVAIL;
510 break;
511 }
512 cmd_info = malloc(sizeof(*cmd_info), M_CTLFE,
513 M_ZERO | M_NOWAIT);
514 if (cmd_info == NULL) {
515 ctl_free_io(new_io);
516 free(new_ccb, M_CTLFE);
517 status = CAM_RESRC_UNAVAIL;
518 break;
519 }
520 PRIV_INFO(new_io) = cmd_info;
521 softc->atios_alloced++;
522 new_ccb->ccb_h.io_ptr = new_io;
523 LIST_INSERT_HEAD(&softc->atio_list, &new_ccb->ccb_h, periph_links.le);
524
525 xpt_setup_ccb(&new_ccb->ccb_h, periph->path,
526 CAM_PRIORITY_NORMAL);
527 new_ccb->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
528 new_ccb->ccb_h.cbfcnp = ctlfedone;
529 new_ccb->ccb_h.flags |= CAM_UNLOCKED;
530 xpt_action(new_ccb);
531 status = new_ccb->ccb_h.status;
532 if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
533 free(cmd_info, M_CTLFE);
534 ctl_free_io(new_io);
535 free(new_ccb, M_CTLFE);
536 break;
537 }
538 }
539
540 acstatus = cam_periph_acquire(periph);
541 if (acstatus != 0) {
542 xpt_print(periph->path, "%s: could not acquire reference "
543 "count, status = %#x\n", __func__, acstatus);
544 return (CAM_REQ_CMP_ERR);
545 }
546
547 if (i == 0) {
548 xpt_print(periph->path, "%s: could not allocate ATIO CCBs, "
549 "status 0x%x\n", __func__, status);
550 return (CAM_REQ_CMP_ERR);
551 }
552
553 for (i = 0; i < CTLFE_IN_PER_LUN; i++) {
554 union ccb *new_ccb;
555 union ctl_io *new_io;
556
557 new_ccb = (union ccb *)malloc(sizeof(*new_ccb), M_CTLFE,
558 M_ZERO|M_NOWAIT);
559 if (new_ccb == NULL) {
560 status = CAM_RESRC_UNAVAIL;
561 break;
562 }
563 new_io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref);
564 if (new_io == NULL) {
565 free(new_ccb, M_CTLFE);
566 status = CAM_RESRC_UNAVAIL;
567 break;
568 }
569 softc->inots_alloced++;
570 new_ccb->ccb_h.io_ptr = new_io;
571 LIST_INSERT_HEAD(&softc->inot_list, &new_ccb->ccb_h, periph_links.le);
572
573 xpt_setup_ccb(&new_ccb->ccb_h, periph->path,
574 CAM_PRIORITY_NORMAL);
575 new_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
576 new_ccb->ccb_h.cbfcnp = ctlfedone;
577 new_ccb->ccb_h.flags |= CAM_UNLOCKED;
578 xpt_action(new_ccb);
579 status = new_ccb->ccb_h.status;
580 if ((status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
581 /*
582 * Note that we don't free the CCB here. If the
583 * status is not CAM_REQ_INPROG, then we're
584 * probably talking to a SIM that says it is
585 * target-capable but doesn't support the
586 * XPT_IMMEDIATE_NOTIFY CCB. i.e. it supports the
587 * older API. In that case, it'll call xpt_done()
588 * on the CCB, and we need to free it in our done
589 * routine as a result.
590 */
591 break;
592 }
593 }
594 if ((i == 0)
595 || (status != CAM_REQ_INPROG)) {
596 xpt_print(periph->path, "%s: could not allocate immediate "
597 "notify CCBs, status 0x%x\n", __func__, status);
598 return (CAM_REQ_CMP_ERR);
599 }
600 mtx_lock(&bus_softc->lun_softc_mtx);
601 STAILQ_INSERT_TAIL(&bus_softc->lun_softc_list, softc, links);
602 mtx_unlock(&bus_softc->lun_softc_mtx);
603 return (CAM_REQ_CMP);
604 }
605
606 static void
ctlfeoninvalidate(struct cam_periph * periph)607 ctlfeoninvalidate(struct cam_periph *periph)
608 {
609 struct ctlfe_lun_softc *softc = (struct ctlfe_lun_softc *)periph->softc;
610 struct ctlfe_softc *bus_softc;
611 union ccb ccb;
612 struct ccb_hdr *hdr;
613 cam_status status;
614
615 /* Abort all ATIOs and INOTs queued to SIM. */
616 memset(&ccb, 0, sizeof(ccb));
617 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NONE);
618 ccb.ccb_h.func_code = XPT_ABORT;
619 LIST_FOREACH(hdr, &softc->atio_list, periph_links.le) {
620 ccb.cab.abort_ccb = (union ccb *)hdr;
621 xpt_action(&ccb);
622 }
623 LIST_FOREACH(hdr, &softc->inot_list, periph_links.le) {
624 ccb.cab.abort_ccb = (union ccb *)hdr;
625 xpt_action(&ccb);
626 }
627
628 /* Disable the LUN in SIM. */
629 ccb.ccb_h.func_code = XPT_EN_LUN;
630 ccb.cel.grp6_len = 0;
631 ccb.cel.grp7_len = 0;
632 ccb.cel.enable = 0;
633 xpt_action(&ccb);
634 status = (ccb.ccb_h.status & CAM_STATUS_MASK);
635 if (status != CAM_REQ_CMP) {
636 xpt_print(periph->path, "%s: Disable LUN failed, status 0x%x\n",
637 __func__, ccb.ccb_h.status);
638 /*
639 * XXX KDM what do we do now?
640 */
641 }
642
643 bus_softc = softc->parent_softc;
644 mtx_lock(&bus_softc->lun_softc_mtx);
645 STAILQ_REMOVE(&bus_softc->lun_softc_list, softc, ctlfe_lun_softc, links);
646 mtx_unlock(&bus_softc->lun_softc_mtx);
647 }
648
649 static void
ctlfecleanup(struct cam_periph * periph)650 ctlfecleanup(struct cam_periph *periph)
651 {
652 struct ctlfe_lun_softc *softc;
653
654 softc = (struct ctlfe_lun_softc *)periph->softc;
655
656 KASSERT(softc->ctios_sent == 0, ("%s: ctios_sent %d != 0",
657 __func__, softc->ctios_sent));
658 KASSERT(softc->refcount == 0, ("%s: refcount %d != 0",
659 __func__, softc->refcount));
660 KASSERT(softc->atios_alloced == 0, ("%s: atios_alloced %d != 0",
661 __func__, softc->atios_alloced));
662 KASSERT(softc->inots_alloced == 0, ("%s: inots_alloced %d != 0",
663 __func__, softc->inots_alloced));
664
665 free(softc, M_CTLFE);
666 }
667
668 static void
ctlfedata(struct ctlfe_lun_softc * softc,union ctl_io * io,ccb_flags * flags,uint8_t ** data_ptr,uint32_t * dxfer_len,uint16_t * sglist_cnt)669 ctlfedata(struct ctlfe_lun_softc *softc, union ctl_io *io,
670 ccb_flags *flags, uint8_t **data_ptr, uint32_t *dxfer_len,
671 uint16_t *sglist_cnt)
672 {
673 struct ctlfe_softc *bus_softc;
674 struct ctlfe_cmd_info *cmd_info;
675 struct ctl_sg_entry *ctl_sglist;
676 bus_dma_segment_t *cam_sglist;
677 size_t off;
678 int i, idx;
679
680 cmd_info = PRIV_INFO(io);
681 bus_softc = softc->parent_softc;
682
683 /*
684 * Set the direction, relative to the initiator.
685 */
686 *flags &= ~CAM_DIR_MASK;
687 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
688 *flags |= CAM_DIR_IN;
689 else
690 *flags |= CAM_DIR_OUT;
691
692 *flags &= ~CAM_DATA_MASK;
693 idx = cmd_info->cur_transfer_index;
694 off = cmd_info->cur_transfer_off;
695 cmd_info->flags &= ~CTLFE_CMD_PIECEWISE;
696 if (io->scsiio.kern_sg_entries == 0) { /* No S/G list. */
697
698 /* One time shift for SRR offset. */
699 off += io->scsiio.ext_data_filled;
700 io->scsiio.ext_data_filled = 0;
701
702 *data_ptr = io->scsiio.kern_data_ptr + off;
703 if (io->scsiio.kern_data_len - off <= bus_softc->maxio) {
704 *dxfer_len = io->scsiio.kern_data_len - off;
705 } else {
706 *dxfer_len = bus_softc->maxio;
707 cmd_info->cur_transfer_off += bus_softc->maxio;
708 cmd_info->flags |= CTLFE_CMD_PIECEWISE;
709 }
710 *sglist_cnt = 0;
711
712 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
713 *flags |= CAM_DATA_PADDR;
714 else
715 *flags |= CAM_DATA_VADDR;
716 } else { /* S/G list with physical or virtual pointers. */
717 ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
718
719 /* One time shift for SRR offset. */
720 while (io->scsiio.ext_data_filled >= ctl_sglist[idx].len - off) {
721 io->scsiio.ext_data_filled -= ctl_sglist[idx].len - off;
722 idx++;
723 off = 0;
724 }
725 off += io->scsiio.ext_data_filled;
726 io->scsiio.ext_data_filled = 0;
727
728 cam_sglist = cmd_info->cam_sglist;
729 *dxfer_len = 0;
730 for (i = 0; i < io->scsiio.kern_sg_entries - idx; i++) {
731 cam_sglist[i].ds_addr = (bus_addr_t)(uintptr_t)ctl_sglist[i + idx].addr + off;
732 if (ctl_sglist[i + idx].len - off <= bus_softc->maxio - *dxfer_len) {
733 cam_sglist[i].ds_len = ctl_sglist[idx + i].len - off;
734 *dxfer_len += cam_sglist[i].ds_len;
735 } else {
736 cam_sglist[i].ds_len = bus_softc->maxio - *dxfer_len;
737 cmd_info->cur_transfer_index = idx + i;
738 cmd_info->cur_transfer_off = cam_sglist[i].ds_len + off;
739 cmd_info->flags |= CTLFE_CMD_PIECEWISE;
740 *dxfer_len += cam_sglist[i].ds_len;
741 if (ctl_sglist[i].len != 0)
742 i++;
743 break;
744 }
745 if (i == (CTLFE_MAX_SEGS - 1) &&
746 idx + i < (io->scsiio.kern_sg_entries - 1)) {
747 cmd_info->cur_transfer_index = idx + i + 1;
748 cmd_info->cur_transfer_off = 0;
749 cmd_info->flags |= CTLFE_CMD_PIECEWISE;
750 i++;
751 break;
752 }
753 off = 0;
754 }
755 *sglist_cnt = i;
756 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR)
757 *flags |= CAM_DATA_SG_PADDR;
758 else
759 *flags |= CAM_DATA_SG;
760 *data_ptr = (uint8_t *)cam_sglist;
761 }
762 }
763
764 static void
ctlfestart(struct cam_periph * periph,union ccb * start_ccb)765 ctlfestart(struct cam_periph *periph, union ccb *start_ccb)
766 {
767 struct ctlfe_lun_softc *softc;
768 struct ctlfe_cmd_info *cmd_info;
769 struct ccb_hdr *ccb_h;
770 struct ccb_accept_tio *atio;
771 struct ccb_scsiio *csio;
772 uint8_t *data_ptr;
773 uint32_t dxfer_len;
774 ccb_flags flags;
775 union ctl_io *io;
776 uint8_t scsi_status;
777
778 softc = (struct ctlfe_lun_softc *)periph->softc;
779
780 next:
781 /* Take the ATIO off the work queue */
782 ccb_h = STAILQ_FIRST(&softc->work_queue);
783 if (ccb_h == NULL) {
784 xpt_release_ccb(start_ccb);
785 return;
786 }
787 STAILQ_REMOVE_HEAD(&softc->work_queue, periph_links.stqe);
788 atio = (struct ccb_accept_tio *)ccb_h;
789 io = (union ctl_io *)ccb_h->io_ptr;
790 csio = &start_ccb->csio;
791
792 flags = atio->ccb_h.flags &
793 (CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
794 cmd_info = PRIV_INFO(io);
795 cmd_info->cur_transfer_index = 0;
796 cmd_info->cur_transfer_off = 0;
797 cmd_info->flags = 0;
798
799 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) {
800 /*
801 * Datamove call, we need to setup the S/G list.
802 */
803 ctlfedata(softc, io, &flags, &data_ptr, &dxfer_len,
804 &csio->sglist_cnt);
805 } else {
806 /*
807 * We're done, send status back.
808 */
809 if ((io->io_hdr.flags & CTL_FLAG_ABORT) &&
810 (io->io_hdr.flags & CTL_FLAG_ABORT_STATUS) == 0) {
811 io->io_hdr.flags &= ~CTL_FLAG_STATUS_QUEUED;
812
813 /* Tell the SIM that we've aborted this ATIO */
814 #ifdef CTLFEDEBUG
815 printf("%s: tag %04x abort\n", __func__, atio->tag_id);
816 #endif
817 KASSERT(atio->ccb_h.func_code == XPT_ACCEPT_TARGET_IO,
818 ("func_code %#x is not ATIO", atio->ccb_h.func_code));
819 start_ccb->ccb_h.func_code = XPT_ABORT;
820 start_ccb->cab.abort_ccb = (union ccb *)atio;
821 xpt_action(start_ccb);
822
823 ctlfe_requeue_ccb(periph, (union ccb *)atio,
824 /* unlock */0);
825
826 /* XPT_ABORT is not queued, so we can take next I/O. */
827 goto next;
828 }
829 data_ptr = NULL;
830 dxfer_len = 0;
831 csio->sglist_cnt = 0;
832 }
833 scsi_status = 0;
834 if ((io->io_hdr.flags & CTL_FLAG_STATUS_QUEUED) &&
835 (cmd_info->flags & CTLFE_CMD_PIECEWISE) == 0 &&
836 ((io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) == 0 ||
837 io->io_hdr.status == CTL_SUCCESS)) {
838 flags |= CAM_SEND_STATUS;
839 scsi_status = io->scsiio.scsi_status;
840 csio->sense_len = io->scsiio.sense_len;
841 #ifdef CTLFEDEBUG
842 printf("%s: tag %04x status %x\n", __func__,
843 atio->tag_id, io->io_hdr.status);
844 #endif
845 if (csio->sense_len != 0) {
846 csio->sense_data = io->scsiio.sense_data;
847 flags |= CAM_SEND_SENSE;
848 }
849 }
850
851 #ifdef CTLFEDEBUG
852 printf("%s: %s: tag %04x flags %x ptr %p len %u\n", __func__,
853 (flags & CAM_SEND_STATUS) ? "done" : "datamove",
854 atio->tag_id, flags, data_ptr, dxfer_len);
855 #endif
856
857 /*
858 * Valid combinations:
859 * - CAM_SEND_STATUS, CAM_DATA_SG = 0, dxfer_len = 0,
860 * sglist_cnt = 0
861 * - CAM_SEND_STATUS = 0, CAM_DATA_SG = 0, dxfer_len != 0,
862 * sglist_cnt = 0
863 * - CAM_SEND_STATUS = 0, CAM_DATA_SG, dxfer_len != 0,
864 * sglist_cnt != 0
865 */
866 #ifdef CTLFEDEBUG
867 if (((flags & CAM_SEND_STATUS)
868 && (((flags & CAM_DATA_SG) != 0)
869 || (dxfer_len != 0)
870 || (csio->sglist_cnt != 0)))
871 || (((flags & CAM_SEND_STATUS) == 0)
872 && (dxfer_len == 0))
873 || ((flags & CAM_DATA_SG)
874 && (csio->sglist_cnt == 0))
875 || (((flags & CAM_DATA_SG) == 0)
876 && (csio->sglist_cnt != 0))) {
877 printf("%s: tag %04x cdb %02x flags %#x dxfer_len "
878 "%d sg %u\n", __func__, atio->tag_id,
879 atio_cdb_ptr(atio)[0], flags, dxfer_len,
880 csio->sglist_cnt);
881 printf("%s: tag %04x io status %#x\n", __func__,
882 atio->tag_id, io->io_hdr.status);
883 }
884 #endif
885 cam_fill_ctio(csio,
886 /*retries*/ 2,
887 ctlfedone,
888 flags,
889 (flags & CAM_TAG_ACTION_VALID) ? MSG_SIMPLE_Q_TAG : 0,
890 atio->tag_id,
891 atio->init_id,
892 scsi_status,
893 /*data_ptr*/ data_ptr,
894 /*dxfer_len*/ dxfer_len,
895 /*timeout*/ CTLFE_TIMEOUT * 1000);
896 start_ccb->ccb_h.flags |= CAM_UNLOCKED;
897 start_ccb->ccb_h.ccb_atio = atio;
898 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
899 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
900 io->io_hdr.flags &= ~(CTL_FLAG_DMA_QUEUED | CTL_FLAG_STATUS_QUEUED);
901
902 softc->ctios_sent++;
903 softc->refcount++;
904 cam_periph_unlock(periph);
905 xpt_action(start_ccb);
906 cam_periph_lock(periph);
907 softc->refcount--;
908
909 /*
910 * If we still have work to do, ask for another CCB.
911 */
912 if (!STAILQ_EMPTY(&softc->work_queue))
913 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
914 }
915
916 static void
ctlfe_drain(void * context,int pending)917 ctlfe_drain(void *context, int pending)
918 {
919 struct cam_periph *periph = context;
920 struct ctlfe_lun_softc *softc = periph->softc;
921
922 cam_periph_lock(periph);
923 while (softc->refcount != 0) {
924 cam_periph_sleep(periph, &softc->refcount, PRIBIO,
925 "ctlfe_drain", 1);
926 }
927 cam_periph_unlock(periph);
928 cam_periph_release(periph);
929 }
930
931 static void
ctlfe_free_ccb(struct cam_periph * periph,union ccb * ccb)932 ctlfe_free_ccb(struct cam_periph *periph, union ccb *ccb)
933 {
934 struct ctlfe_lun_softc *softc;
935 union ctl_io *io;
936 struct ctlfe_cmd_info *cmd_info;
937
938 softc = (struct ctlfe_lun_softc *)periph->softc;
939 io = ccb->ccb_h.io_ptr;
940
941 switch (ccb->ccb_h.func_code) {
942 case XPT_ACCEPT_TARGET_IO:
943 softc->atios_alloced--;
944 cmd_info = PRIV_INFO(io);
945 free(cmd_info, M_CTLFE);
946 break;
947 case XPT_IMMEDIATE_NOTIFY:
948 case XPT_NOTIFY_ACKNOWLEDGE:
949 softc->inots_alloced--;
950 break;
951 default:
952 break;
953 }
954
955 ctl_free_io(io);
956 free(ccb, M_CTLFE);
957
958 KASSERT(softc->atios_alloced >= 0, ("%s: atios_alloced %d < 0",
959 __func__, softc->atios_alloced));
960 KASSERT(softc->inots_alloced >= 0, ("%s: inots_alloced %d < 0",
961 __func__, softc->inots_alloced));
962
963 /*
964 * If we have received all of our CCBs, we can release our
965 * reference on the peripheral driver. It will probably go away
966 * now.
967 */
968 if (softc->atios_alloced == 0 && softc->inots_alloced == 0) {
969 if (softc->refcount == 0) {
970 cam_periph_release_locked(periph);
971 } else {
972 TASK_INIT(&softc->refdrain_task, 0, ctlfe_drain, periph);
973 taskqueue_enqueue(taskqueue_thread,
974 &softc->refdrain_task);
975 }
976 }
977 }
978
979 /*
980 * Send the ATIO/INOT back to the SIM, or free it if periph was invalidated.
981 */
982 static void
ctlfe_requeue_ccb(struct cam_periph * periph,union ccb * ccb,int unlock)983 ctlfe_requeue_ccb(struct cam_periph *periph, union ccb *ccb, int unlock)
984 {
985 struct ctlfe_lun_softc *softc;
986 struct mtx *mtx;
987
988 if (periph->flags & CAM_PERIPH_INVALID) {
989 mtx = cam_periph_mtx(periph);
990 ctlfe_free_ccb(periph, ccb);
991 if (unlock)
992 mtx_unlock(mtx);
993 return;
994 }
995 softc = (struct ctlfe_lun_softc *)periph->softc;
996 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
997 LIST_INSERT_HEAD(&softc->atio_list, &ccb->ccb_h, periph_links.le);
998 else
999 LIST_INSERT_HEAD(&softc->inot_list, &ccb->ccb_h, periph_links.le);
1000 if (unlock)
1001 cam_periph_unlock(periph);
1002
1003 /*
1004 * For a wildcard attachment, commands can come in with a specific
1005 * target/lun. Reset the target and LUN fields back to the wildcard
1006 * values before we send them back down to the SIM.
1007 */
1008 xpt_setup_ccb_flags(&ccb->ccb_h, periph->path, CAM_PRIORITY_NORMAL,
1009 ccb->ccb_h.flags);
1010
1011 xpt_action(ccb);
1012 }
1013
1014 static int
ctlfe_adjust_cdb(struct ccb_accept_tio * atio,uint32_t offset)1015 ctlfe_adjust_cdb(struct ccb_accept_tio *atio, uint32_t offset)
1016 {
1017 uint64_t lba;
1018 uint32_t num_blocks, nbc;
1019 uint8_t *cmdbyt = atio_cdb_ptr(atio);
1020
1021 nbc = offset >> 9; /* ASSUMING 512 BYTE BLOCKS */
1022
1023 switch (cmdbyt[0]) {
1024 case READ_6:
1025 case WRITE_6:
1026 {
1027 struct scsi_rw_6 *cdb = (struct scsi_rw_6 *)cmdbyt;
1028 lba = scsi_3btoul(cdb->addr);
1029 lba &= 0x1fffff;
1030 num_blocks = cdb->length;
1031 if (num_blocks == 0)
1032 num_blocks = 256;
1033 lba += nbc;
1034 num_blocks -= nbc;
1035 scsi_ulto3b(lba, cdb->addr);
1036 cdb->length = num_blocks;
1037 break;
1038 }
1039 case READ_10:
1040 case WRITE_10:
1041 {
1042 struct scsi_rw_10 *cdb = (struct scsi_rw_10 *)cmdbyt;
1043 lba = scsi_4btoul(cdb->addr);
1044 num_blocks = scsi_2btoul(cdb->length);
1045 lba += nbc;
1046 num_blocks -= nbc;
1047 scsi_ulto4b(lba, cdb->addr);
1048 scsi_ulto2b(num_blocks, cdb->length);
1049 break;
1050 }
1051 case READ_12:
1052 case WRITE_12:
1053 {
1054 struct scsi_rw_12 *cdb = (struct scsi_rw_12 *)cmdbyt;
1055 lba = scsi_4btoul(cdb->addr);
1056 num_blocks = scsi_4btoul(cdb->length);
1057 lba += nbc;
1058 num_blocks -= nbc;
1059 scsi_ulto4b(lba, cdb->addr);
1060 scsi_ulto4b(num_blocks, cdb->length);
1061 break;
1062 }
1063 case READ_16:
1064 case WRITE_16:
1065 {
1066 struct scsi_rw_16 *cdb = (struct scsi_rw_16 *)cmdbyt;
1067 lba = scsi_8btou64(cdb->addr);
1068 num_blocks = scsi_4btoul(cdb->length);
1069 lba += nbc;
1070 num_blocks -= nbc;
1071 scsi_u64to8b(lba, cdb->addr);
1072 scsi_ulto4b(num_blocks, cdb->length);
1073 break;
1074 }
1075 default:
1076 return -1;
1077 }
1078 return (0);
1079 }
1080
1081 static void
ctlfedone(struct cam_periph * periph,union ccb * done_ccb)1082 ctlfedone(struct cam_periph *periph, union ccb *done_ccb)
1083 {
1084 struct ctlfe_lun_softc *softc;
1085 struct ctlfe_softc *bus_softc;
1086 struct ctlfe_cmd_info *cmd_info;
1087 struct ccb_accept_tio *atio = NULL;
1088 union ctl_io *io = NULL;
1089 struct mtx *mtx;
1090 cam_status status;
1091
1092 KASSERT((done_ccb->ccb_h.flags & CAM_UNLOCKED) != 0,
1093 ("CCB in ctlfedone() without CAM_UNLOCKED flag"));
1094 #ifdef CTLFE_DEBUG
1095 printf("%s: entered, func_code = %#x\n", __func__,
1096 done_ccb->ccb_h.func_code);
1097 #endif
1098
1099 /*
1100 * At this point CTL has no known use case for device queue freezes.
1101 * In case some SIM think different -- drop its freeze right here.
1102 */
1103 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1104 cam_release_devq(periph->path,
1105 /*relsim_flags*/0,
1106 /*reduction*/0,
1107 /*timeout*/0,
1108 /*getcount_only*/0);
1109 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
1110 }
1111
1112 softc = (struct ctlfe_lun_softc *)periph->softc;
1113 bus_softc = softc->parent_softc;
1114 mtx = cam_periph_mtx(periph);
1115 mtx_lock(mtx);
1116
1117 switch (done_ccb->ccb_h.func_code) {
1118 case XPT_ACCEPT_TARGET_IO: {
1119 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1120 atio = &done_ccb->atio;
1121 status = atio->ccb_h.status & CAM_STATUS_MASK;
1122 if (status != CAM_CDB_RECVD) {
1123 ctlfe_free_ccb(periph, done_ccb);
1124 goto out;
1125 }
1126
1127 resubmit:
1128 /*
1129 * Allocate a ctl_io, pass it to CTL, and wait for the
1130 * datamove or done.
1131 */
1132 mtx_unlock(mtx);
1133 io = done_ccb->ccb_h.io_ptr;
1134 cmd_info = PRIV_INFO(io);
1135 ctl_zero_io(io);
1136
1137 /* Save pointers on both sides */
1138 PRIV_CCB(io) = done_ccb;
1139 PRIV_INFO(io) = cmd_info;
1140 done_ccb->ccb_h.io_ptr = io;
1141
1142 /*
1143 * Only SCSI I/O comes down this path, resets, etc. come
1144 * down the immediate notify path below.
1145 */
1146 io->io_hdr.io_type = CTL_IO_SCSI;
1147 io->io_hdr.nexus.initid = atio->init_id;
1148 io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1149 if (bus_softc->hba_misc & PIM_EXTLUNS) {
1150 io->io_hdr.nexus.targ_lun = ctl_decode_lun(
1151 CAM_EXTLUN_BYTE_SWIZZLE(atio->ccb_h.target_lun));
1152 } else {
1153 io->io_hdr.nexus.targ_lun = atio->ccb_h.target_lun;
1154 }
1155 io->scsiio.priority = atio->priority;
1156 io->scsiio.tag_num = atio->tag_id;
1157 switch (atio->tag_action) {
1158 case CAM_TAG_ACTION_NONE:
1159 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1160 break;
1161 case MSG_SIMPLE_TASK:
1162 io->scsiio.tag_type = CTL_TAG_SIMPLE;
1163 break;
1164 case MSG_HEAD_OF_QUEUE_TASK:
1165 io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
1166 break;
1167 case MSG_ORDERED_TASK:
1168 io->scsiio.tag_type = CTL_TAG_ORDERED;
1169 break;
1170 case MSG_ACA_TASK:
1171 io->scsiio.tag_type = CTL_TAG_ACA;
1172 break;
1173 default:
1174 io->scsiio.tag_type = CTL_TAG_UNTAGGED;
1175 printf("%s: unhandled tag type %#x!!\n", __func__,
1176 atio->tag_action);
1177 break;
1178 }
1179 if (atio->cdb_len > sizeof(io->scsiio.cdb)) {
1180 printf("%s: WARNING: CDB len %d > ctl_io space %zd\n",
1181 __func__, atio->cdb_len, sizeof(io->scsiio.cdb));
1182 }
1183 io->scsiio.cdb_len = min(atio->cdb_len, sizeof(io->scsiio.cdb));
1184 bcopy(atio_cdb_ptr(atio), io->scsiio.cdb, io->scsiio.cdb_len);
1185
1186 #ifdef CTLFEDEBUG
1187 printf("%s: %u:%u:%u: tag %jx CDB %02x\n", __func__,
1188 io->io_hdr.nexus.initid,
1189 io->io_hdr.nexus.targ_port,
1190 io->io_hdr.nexus.targ_lun,
1191 io->scsiio.tag_num, io->scsiio.cdb[0]);
1192 #endif
1193
1194 ctl_queue(io);
1195 return;
1196 }
1197 case XPT_CONT_TARGET_IO: {
1198 int srr = 0;
1199 uint32_t srr_off = 0;
1200
1201 atio = (struct ccb_accept_tio *)done_ccb->ccb_h.ccb_atio;
1202 io = (union ctl_io *)atio->ccb_h.io_ptr;
1203
1204 softc->ctios_sent--;
1205 #ifdef CTLFEDEBUG
1206 printf("%s: got XPT_CONT_TARGET_IO tag %#x flags %#x\n",
1207 __func__, atio->tag_id, done_ccb->ccb_h.flags);
1208 #endif
1209 /*
1210 * Handle SRR case were the data pointer is pushed back hack
1211 */
1212 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_MESSAGE_RECV
1213 && done_ccb->csio.msg_ptr != NULL
1214 && done_ccb->csio.msg_ptr[0] == MSG_EXTENDED
1215 && done_ccb->csio.msg_ptr[1] == 5
1216 && done_ccb->csio.msg_ptr[2] == 0) {
1217 srr = 1;
1218 srr_off =
1219 (done_ccb->csio.msg_ptr[3] << 24)
1220 | (done_ccb->csio.msg_ptr[4] << 16)
1221 | (done_ccb->csio.msg_ptr[5] << 8)
1222 | (done_ccb->csio.msg_ptr[6]);
1223 }
1224
1225 /*
1226 * If we have an SRR and we're still sending data, we
1227 * should be able to adjust offsets and cycle again.
1228 * It is possible only if offset is from this datamove.
1229 */
1230 if (srr && (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) &&
1231 srr_off >= io->scsiio.kern_rel_offset &&
1232 srr_off < io->scsiio.kern_rel_offset +
1233 io->scsiio.kern_data_len) {
1234 io->scsiio.kern_data_resid =
1235 io->scsiio.kern_rel_offset +
1236 io->scsiio.kern_data_len - srr_off;
1237 io->scsiio.ext_data_filled = srr_off;
1238 io->scsiio.io_hdr.status = CTL_STATUS_NONE;
1239 io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
1240 xpt_release_ccb(done_ccb);
1241 STAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h,
1242 periph_links.stqe);
1243 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1244 break;
1245 }
1246
1247 /*
1248 * If status was being sent, the back end data is now history.
1249 * Hack it up and resubmit a new command with the CDB adjusted.
1250 * If the SIM does the right thing, all of the resid math
1251 * should work.
1252 */
1253 if (srr && (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) {
1254 xpt_release_ccb(done_ccb);
1255 if (ctlfe_adjust_cdb(atio, srr_off) == 0) {
1256 done_ccb = (union ccb *)atio;
1257 goto resubmit;
1258 }
1259 /*
1260 * Fall through to doom....
1261 */
1262 }
1263
1264 if ((done_ccb->ccb_h.flags & CAM_SEND_STATUS) &&
1265 (done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1266 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1267
1268 /*
1269 * If we were sending status back to the initiator, free up
1270 * resources. If we were doing a datamove, call the
1271 * datamove done routine.
1272 */
1273 if ((io->io_hdr.flags & CTL_FLAG_DMA_INPROG) == 0) {
1274 /*
1275 * If we asked to send sense data but it wasn't sent,
1276 * queue the I/O back to CTL for later REQUEST SENSE.
1277 */
1278 if ((done_ccb->ccb_h.flags & CAM_SEND_SENSE) != 0 &&
1279 (done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP &&
1280 (done_ccb->ccb_h.status & CAM_SENT_SENSE) == 0 &&
1281 (io = ctl_alloc_io_nowait(bus_softc->port.ctl_pool_ref)) != NULL) {
1282 PRIV_INFO(io) = PRIV_INFO(
1283 (union ctl_io *)atio->ccb_h.io_ptr);
1284 ctl_queue_sense(atio->ccb_h.io_ptr);
1285 atio->ccb_h.io_ptr = io;
1286 }
1287
1288 /* Abort ATIO if CTIO sending status has failed. */
1289 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) !=
1290 CAM_REQ_CMP) {
1291 done_ccb->ccb_h.func_code = XPT_ABORT;
1292 done_ccb->cab.abort_ccb = (union ccb *)atio;
1293 xpt_action(done_ccb);
1294 }
1295
1296 xpt_release_ccb(done_ccb);
1297 ctlfe_requeue_ccb(periph, (union ccb *)atio,
1298 /* unlock */1);
1299 return;
1300 } else {
1301 struct ctlfe_cmd_info *cmd_info;
1302 struct ccb_scsiio *csio;
1303
1304 csio = &done_ccb->csio;
1305 cmd_info = PRIV_INFO(io);
1306
1307 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1308
1309 /*
1310 * Translate CAM status to CTL status. Success
1311 * does not change the overall, ctl_io status. In
1312 * that case we just set port_status to 0. If we
1313 * have a failure, though, set a data phase error
1314 * for the overall ctl_io.
1315 */
1316 switch (done_ccb->ccb_h.status & CAM_STATUS_MASK) {
1317 case CAM_REQ_CMP:
1318 io->scsiio.kern_data_resid -=
1319 csio->dxfer_len - csio->resid;
1320 io->io_hdr.port_status = 0;
1321 break;
1322 default:
1323 /*
1324 * XXX KDM we probably need to figure out a
1325 * standard set of errors that the SIM
1326 * drivers should return in the event of a
1327 * data transfer failure. A data phase
1328 * error will at least point the user to a
1329 * data transfer error of some sort.
1330 * Hopefully the SIM printed out some
1331 * additional information to give the user
1332 * a clue what happened.
1333 */
1334 io->io_hdr.port_status = 0xbad1;
1335 ctl_set_data_phase_error(&io->scsiio);
1336 /*
1337 * XXX KDM figure out residual.
1338 */
1339 break;
1340 }
1341 /*
1342 * If we had to break this S/G list into multiple
1343 * pieces, figure out where we are in the list, and
1344 * continue sending pieces if necessary.
1345 */
1346 if ((cmd_info->flags & CTLFE_CMD_PIECEWISE) &&
1347 io->io_hdr.port_status == 0 && csio->resid == 0) {
1348 ccb_flags flags;
1349 uint8_t *data_ptr;
1350 uint32_t dxfer_len;
1351
1352 flags = atio->ccb_h.flags &
1353 (CAM_DIS_DISCONNECT|
1354 CAM_TAG_ACTION_VALID);
1355
1356 ctlfedata(softc, io, &flags, &data_ptr,
1357 &dxfer_len, &csio->sglist_cnt);
1358
1359 if (((flags & CAM_SEND_STATUS) == 0)
1360 && (dxfer_len == 0)) {
1361 printf("%s: tag %04x no status or "
1362 "len cdb = %02x\n", __func__,
1363 atio->tag_id,
1364 atio_cdb_ptr(atio)[0]);
1365 printf("%s: tag %04x io status %#x\n",
1366 __func__, atio->tag_id,
1367 io->io_hdr.status);
1368 }
1369
1370 cam_fill_ctio(csio,
1371 /*retries*/ 2,
1372 ctlfedone,
1373 flags,
1374 (flags & CAM_TAG_ACTION_VALID) ?
1375 MSG_SIMPLE_Q_TAG : 0,
1376 atio->tag_id,
1377 atio->init_id,
1378 0,
1379 /*data_ptr*/ data_ptr,
1380 /*dxfer_len*/ dxfer_len,
1381 CTLFE_TIMEOUT * 1000);
1382
1383 csio->ccb_h.flags |= CAM_UNLOCKED;
1384 csio->resid = 0;
1385 csio->ccb_h.ccb_atio = atio;
1386 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
1387 softc->ctios_sent++;
1388 mtx_unlock(mtx);
1389 xpt_action((union ccb *)csio);
1390 } else {
1391 /*
1392 * Release the CTIO. The ATIO will be sent back
1393 * down to the SIM once we send status.
1394 */
1395 xpt_release_ccb(done_ccb);
1396 mtx_unlock(mtx);
1397
1398 ctl_datamove_done(io, false);
1399 }
1400 return;
1401 }
1402 break;
1403 }
1404 case XPT_IMMEDIATE_NOTIFY: {
1405 union ctl_io *io;
1406 struct ccb_immediate_notify *inot;
1407 int send_ctl_io;
1408
1409 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1410 inot = &done_ccb->cin1;
1411 io = done_ccb->ccb_h.io_ptr;
1412 ctl_zero_io(io);
1413
1414 send_ctl_io = 1;
1415
1416 io->io_hdr.io_type = CTL_IO_TASK;
1417 PRIV_CCB(io) = done_ccb;
1418 inot->ccb_h.io_ptr = io;
1419 io->io_hdr.nexus.initid = inot->initiator_id;
1420 io->io_hdr.nexus.targ_port = bus_softc->port.targ_port;
1421 if (bus_softc->hba_misc & PIM_EXTLUNS) {
1422 io->io_hdr.nexus.targ_lun = ctl_decode_lun(
1423 CAM_EXTLUN_BYTE_SWIZZLE(inot->ccb_h.target_lun));
1424 } else {
1425 io->io_hdr.nexus.targ_lun = inot->ccb_h.target_lun;
1426 }
1427 /* XXX KDM should this be the tag_id? */
1428 io->taskio.tag_num = inot->seq_id;
1429
1430 status = inot->ccb_h.status & CAM_STATUS_MASK;
1431 switch (status) {
1432 case CAM_SCSI_BUS_RESET:
1433 io->taskio.task_action = CTL_TASK_BUS_RESET;
1434 break;
1435 case CAM_BDR_SENT:
1436 io->taskio.task_action = CTL_TASK_TARGET_RESET;
1437 break;
1438 case CAM_MESSAGE_RECV:
1439 switch (inot->arg) {
1440 case MSG_ABORT_TASK_SET:
1441 io->taskio.task_action =
1442 CTL_TASK_ABORT_TASK_SET;
1443 break;
1444 case MSG_TARGET_RESET:
1445 io->taskio.task_action = CTL_TASK_TARGET_RESET;
1446 break;
1447 case MSG_ABORT_TASK:
1448 io->taskio.task_action = CTL_TASK_ABORT_TASK;
1449 break;
1450 case MSG_LOGICAL_UNIT_RESET:
1451 io->taskio.task_action = CTL_TASK_LUN_RESET;
1452 break;
1453 case MSG_CLEAR_TASK_SET:
1454 io->taskio.task_action =
1455 CTL_TASK_CLEAR_TASK_SET;
1456 break;
1457 case MSG_CLEAR_ACA:
1458 io->taskio.task_action = CTL_TASK_CLEAR_ACA;
1459 break;
1460 case MSG_QUERY_TASK:
1461 io->taskio.task_action = CTL_TASK_QUERY_TASK;
1462 break;
1463 case MSG_QUERY_TASK_SET:
1464 io->taskio.task_action =
1465 CTL_TASK_QUERY_TASK_SET;
1466 break;
1467 case MSG_QUERY_ASYNC_EVENT:
1468 io->taskio.task_action =
1469 CTL_TASK_QUERY_ASYNC_EVENT;
1470 break;
1471 case MSG_NOOP:
1472 send_ctl_io = 0;
1473 break;
1474 default:
1475 xpt_print(periph->path,
1476 "%s: unsupported INOT message 0x%x\n",
1477 __func__, inot->arg);
1478 send_ctl_io = 0;
1479 break;
1480 }
1481 break;
1482 default:
1483 xpt_print(periph->path,
1484 "%s: unsupported INOT status 0x%x\n",
1485 __func__, status);
1486 /* FALLTHROUGH */
1487 case CAM_REQ_ABORTED:
1488 case CAM_REQ_INVALID:
1489 case CAM_DEV_NOT_THERE:
1490 case CAM_PROVIDE_FAIL:
1491 ctlfe_free_ccb(periph, done_ccb);
1492 goto out;
1493 }
1494 mtx_unlock(mtx);
1495 if (send_ctl_io != 0) {
1496 ctl_queue(io);
1497 } else {
1498 done_ccb->ccb_h.status = CAM_REQ_INPROG;
1499 done_ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1500 xpt_action(done_ccb);
1501 }
1502 return;
1503 }
1504 case XPT_NOTIFY_ACKNOWLEDGE:
1505 /* Queue this back down to the SIM as an immediate notify. */
1506 done_ccb->ccb_h.status = CAM_REQ_INPROG;
1507 done_ccb->ccb_h.func_code = XPT_IMMEDIATE_NOTIFY;
1508 ctlfe_requeue_ccb(periph, done_ccb, /* unlock */1);
1509 return;
1510 case XPT_SET_SIM_KNOB:
1511 case XPT_GET_SIM_KNOB:
1512 case XPT_GET_SIM_KNOB_OLD:
1513 break;
1514 default:
1515 panic("%s: unexpected CCB type %#x", __func__,
1516 done_ccb->ccb_h.func_code);
1517 break;
1518 }
1519
1520 out:
1521 mtx_unlock(mtx);
1522 }
1523
1524 static void
ctlfe_onoffline(void * arg,int online)1525 ctlfe_onoffline(void *arg, int online)
1526 {
1527 struct ctlfe_softc *bus_softc = arg;
1528 union ccb *ccb;
1529 cam_status status;
1530 struct cam_path *path;
1531 int set_wwnn = 0;
1532
1533 status = xpt_create_path(&path, /*periph*/ NULL, bus_softc->path_id,
1534 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1535 if (status != CAM_REQ_CMP) {
1536 printf("%s: unable to create path!\n", __func__);
1537 return;
1538 }
1539 ccb = xpt_alloc_ccb();
1540 xpt_setup_ccb(&ccb->ccb_h, path, CAM_PRIORITY_NONE);
1541 ccb->ccb_h.func_code = XPT_GET_SIM_KNOB;
1542 xpt_action(ccb);
1543
1544 /* Check whether we should change WWNs. */
1545 if (online != 0) {
1546 if ((ccb->knob.xport_specific.valid & KNOB_VALID_ADDRESS) != 0){
1547 printf("%s: %s current WWNN %#jx\n", __func__,
1548 bus_softc->port_name,
1549 ccb->knob.xport_specific.fc.wwnn);
1550 printf("%s: %s current WWPN %#jx\n", __func__,
1551 bus_softc->port_name,
1552 ccb->knob.xport_specific.fc.wwpn);
1553
1554 /*
1555 * If the user has specified a WWNN/WWPN, send them
1556 * down to the SIM. Otherwise, record what the SIM
1557 * has reported.
1558 */
1559 if (bus_softc->port.wwnn != 0 && bus_softc->port.wwnn
1560 != ccb->knob.xport_specific.fc.wwnn) {
1561 ccb->knob.xport_specific.fc.wwnn =
1562 bus_softc->port.wwnn;
1563 set_wwnn = 1;
1564 } else {
1565 ctl_port_set_wwns(&bus_softc->port,
1566 true, ccb->knob.xport_specific.fc.wwnn,
1567 false, 0);
1568 }
1569 if (bus_softc->port.wwpn != 0 && bus_softc->port.wwpn
1570 != ccb->knob.xport_specific.fc.wwpn) {
1571 ccb->knob.xport_specific.fc.wwpn =
1572 bus_softc->port.wwpn;
1573 set_wwnn = 1;
1574 } else {
1575 ctl_port_set_wwns(&bus_softc->port,
1576 false, 0,
1577 true, ccb->knob.xport_specific.fc.wwpn);
1578 }
1579 } else {
1580 printf("%s: %s has no valid WWNN/WWPN\n", __func__,
1581 bus_softc->port_name);
1582 if (bus_softc->port.wwnn != 0) {
1583 ccb->knob.xport_specific.fc.wwnn =
1584 bus_softc->port.wwnn;
1585 set_wwnn = 1;
1586 }
1587 if (bus_softc->port.wwpn != 0) {
1588 ccb->knob.xport_specific.fc.wwpn =
1589 bus_softc->port.wwpn;
1590 set_wwnn = 1;
1591 }
1592 }
1593 }
1594 if (set_wwnn) {
1595 ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
1596 ccb->knob.xport_specific.valid = KNOB_VALID_ADDRESS;
1597 xpt_action(ccb);
1598 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1599 printf("%s: %s (path id %d) failed set WWNs: %#x\n",
1600 __func__, bus_softc->port_name, bus_softc->path_id,
1601 ccb->ccb_h.status);
1602 } else {
1603 printf("%s: %s new WWNN %#jx\n", __func__,
1604 bus_softc->port_name,
1605 ccb->knob.xport_specific.fc.wwnn);
1606 printf("%s: %s new WWPN %#jx\n", __func__,
1607 bus_softc->port_name,
1608 ccb->knob.xport_specific.fc.wwpn);
1609 }
1610 }
1611
1612 /* Check whether we should change role. */
1613 if ((ccb->knob.xport_specific.valid & KNOB_VALID_ROLE) == 0 ||
1614 ((online != 0) ^
1615 ((ccb->knob.xport_specific.fc.role & KNOB_ROLE_TARGET) != 0)) != 0) {
1616 ccb->ccb_h.func_code = XPT_SET_SIM_KNOB;
1617 ccb->knob.xport_specific.valid = KNOB_VALID_ROLE;
1618 if (online)
1619 ccb->knob.xport_specific.fc.role |= KNOB_ROLE_TARGET;
1620 else
1621 ccb->knob.xport_specific.fc.role &= ~KNOB_ROLE_TARGET;
1622 xpt_action(ccb);
1623 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1624 printf("%s: %s (path id %d) failed %s target role: %#x\n",
1625 __func__, bus_softc->port_name, bus_softc->path_id,
1626 online ? "enable" : "disable", ccb->ccb_h.status);
1627 } else {
1628 printf("%s: %s (path id %d) target role %s succeeded\n",
1629 __func__, bus_softc->port_name, bus_softc->path_id,
1630 online ? "enable" : "disable");
1631 }
1632 }
1633
1634 xpt_free_path(path);
1635 xpt_free_ccb(ccb);
1636 }
1637
1638 static void
ctlfe_online(void * arg)1639 ctlfe_online(void *arg)
1640 {
1641 struct ctlfe_softc *bus_softc;
1642 struct cam_path *path;
1643 cam_status status;
1644 struct ctlfe_lun_softc *lun_softc;
1645 struct cam_periph *periph;
1646
1647 bus_softc = (struct ctlfe_softc *)arg;
1648
1649 /*
1650 * Create the wildcard LUN before bringing the port online.
1651 */
1652 status = xpt_create_path(&path, /*periph*/ NULL,
1653 bus_softc->path_id, CAM_TARGET_WILDCARD,
1654 CAM_LUN_WILDCARD);
1655 if (status != CAM_REQ_CMP) {
1656 printf("%s: unable to create path for wildcard periph\n",
1657 __func__);
1658 return;
1659 }
1660
1661 lun_softc = malloc(sizeof(*lun_softc), M_CTLFE, M_WAITOK | M_ZERO);
1662
1663 xpt_path_lock(path);
1664 periph = cam_periph_find(path, "ctl");
1665 if (periph != NULL) {
1666 /* We've already got a periph, no need to alloc a new one. */
1667 xpt_path_unlock(path);
1668 xpt_free_path(path);
1669 free(lun_softc, M_CTLFE);
1670 return;
1671 }
1672 lun_softc->parent_softc = bus_softc;
1673 lun_softc->flags |= CTLFE_LUN_WILDCARD;
1674
1675 status = cam_periph_alloc(ctlferegister,
1676 ctlfeoninvalidate,
1677 ctlfecleanup,
1678 ctlfestart,
1679 "ctl",
1680 CAM_PERIPH_BIO,
1681 path,
1682 ctlfeasync,
1683 0,
1684 lun_softc);
1685
1686 if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1687 const struct cam_status_entry *entry;
1688
1689 entry = cam_fetch_status_entry(status);
1690 printf("%s: CAM error %s (%#x) returned from "
1691 "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1692 entry->status_text : "Unknown", status);
1693 free(lun_softc, M_CTLFE);
1694 }
1695
1696 xpt_path_unlock(path);
1697 ctlfe_onoffline(arg, /*online*/ 1);
1698 xpt_free_path(path);
1699 }
1700
1701 static void
ctlfe_offline(void * arg)1702 ctlfe_offline(void *arg)
1703 {
1704 struct ctlfe_softc *bus_softc;
1705 struct cam_path *path;
1706 cam_status status;
1707 struct cam_periph *periph;
1708
1709 bus_softc = (struct ctlfe_softc *)arg;
1710
1711 ctlfe_onoffline(arg, /*online*/ 0);
1712
1713 /*
1714 * Disable the wildcard LUN for this port now that we have taken
1715 * the port offline.
1716 */
1717 status = xpt_create_path(&path, /*periph*/ NULL,
1718 bus_softc->path_id, CAM_TARGET_WILDCARD,
1719 CAM_LUN_WILDCARD);
1720 if (status != CAM_REQ_CMP) {
1721 printf("%s: unable to create path for wildcard periph\n",
1722 __func__);
1723 return;
1724 }
1725 xpt_path_lock(path);
1726 if ((periph = cam_periph_find(path, "ctl")) != NULL)
1727 cam_periph_invalidate(periph);
1728 xpt_path_unlock(path);
1729 xpt_free_path(path);
1730 }
1731
1732 /*
1733 * This will get called to enable a LUN on every bus that is attached to
1734 * CTL. So we only need to create a path/periph for this particular bus.
1735 */
1736 static int
ctlfe_lun_enable(void * arg,int lun_id)1737 ctlfe_lun_enable(void *arg, int lun_id)
1738 {
1739 struct ctlfe_softc *bus_softc;
1740 struct ctlfe_lun_softc *softc;
1741 struct cam_path *path;
1742 struct cam_periph *periph;
1743 cam_status status;
1744
1745 bus_softc = (struct ctlfe_softc *)arg;
1746 if (bus_softc->hba_misc & PIM_EXTLUNS)
1747 lun_id = CAM_EXTLUN_BYTE_SWIZZLE(ctl_encode_lun(lun_id));
1748
1749 status = xpt_create_path(&path, /*periph*/ NULL,
1750 bus_softc->path_id, bus_softc->target_id, lun_id);
1751 /* XXX KDM need some way to return status to CTL here? */
1752 if (status != CAM_REQ_CMP) {
1753 printf("%s: could not create path, status %#x\n", __func__,
1754 status);
1755 return (1);
1756 }
1757
1758 softc = malloc(sizeof(*softc), M_CTLFE, M_WAITOK | M_ZERO);
1759 xpt_path_lock(path);
1760 periph = cam_periph_find(path, "ctl");
1761 if (periph != NULL) {
1762 /* We've already got a periph, no need to alloc a new one. */
1763 xpt_path_unlock(path);
1764 xpt_free_path(path);
1765 free(softc, M_CTLFE);
1766 return (0);
1767 }
1768 softc->parent_softc = bus_softc;
1769
1770 status = cam_periph_alloc(ctlferegister,
1771 ctlfeoninvalidate,
1772 ctlfecleanup,
1773 ctlfestart,
1774 "ctl",
1775 CAM_PERIPH_BIO,
1776 path,
1777 ctlfeasync,
1778 0,
1779 softc);
1780
1781 if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1782 const struct cam_status_entry *entry;
1783
1784 entry = cam_fetch_status_entry(status);
1785 printf("%s: CAM error %s (%#x) returned from "
1786 "cam_periph_alloc()\n", __func__, (entry != NULL) ?
1787 entry->status_text : "Unknown", status);
1788 free(softc, M_CTLFE);
1789 }
1790
1791 xpt_path_unlock(path);
1792 xpt_free_path(path);
1793 return (0);
1794 }
1795
1796 /*
1797 * This will get called when the user removes a LUN to disable that LUN
1798 * on every bus that is attached to CTL.
1799 */
1800 static int
ctlfe_lun_disable(void * arg,int lun_id)1801 ctlfe_lun_disable(void *arg, int lun_id)
1802 {
1803 struct ctlfe_softc *softc;
1804 struct ctlfe_lun_softc *lun_softc;
1805
1806 softc = (struct ctlfe_softc *)arg;
1807 if (softc->hba_misc & PIM_EXTLUNS)
1808 lun_id = CAM_EXTLUN_BYTE_SWIZZLE(ctl_encode_lun(lun_id));
1809
1810 mtx_lock(&softc->lun_softc_mtx);
1811 STAILQ_FOREACH(lun_softc, &softc->lun_softc_list, links) {
1812 struct cam_path *path;
1813
1814 path = lun_softc->periph->path;
1815
1816 if ((xpt_path_target_id(path) == softc->target_id)
1817 && (xpt_path_lun_id(path) == lun_id)) {
1818 break;
1819 }
1820 }
1821 if (lun_softc == NULL) {
1822 mtx_unlock(&softc->lun_softc_mtx);
1823 printf("%s: can't find lun %d\n", __func__, lun_id);
1824 return (1);
1825 }
1826 cam_periph_acquire(lun_softc->periph);
1827 mtx_unlock(&softc->lun_softc_mtx);
1828
1829 cam_periph_lock(lun_softc->periph);
1830 cam_periph_invalidate(lun_softc->periph);
1831 cam_periph_unlock(lun_softc->periph);
1832 cam_periph_release(lun_softc->periph);
1833 return (0);
1834 }
1835
1836 static void
ctlfe_dump_sim(struct cam_sim * sim)1837 ctlfe_dump_sim(struct cam_sim *sim)
1838 {
1839
1840 printf("%s%d: max dev openings: %d, max tagged dev openings: %d\n",
1841 sim->sim_name, sim->unit_number, sim->max_dev_openings,
1842 sim->max_tagged_dev_openings);
1843 }
1844
1845 /*
1846 * Assumes that the SIM lock is held.
1847 */
1848 static void
ctlfe_dump_queue(struct ctlfe_lun_softc * softc)1849 ctlfe_dump_queue(struct ctlfe_lun_softc *softc)
1850 {
1851 struct cam_periph *periph = softc->periph;
1852 struct ccb_hdr *hdr;
1853 struct ccb_getdevstats cgds;
1854 int num_items;
1855
1856 memset(&cgds, 0, sizeof(cgds));
1857 xpt_setup_ccb(&cgds.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1858 cgds.ccb_h.func_code = XPT_GDEV_STATS;
1859 xpt_action((union ccb *)&cgds);
1860 if ((cgds.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1861 xpt_print(periph->path, "devq: openings %d, active %d, "
1862 "allocated %d, queued %d, held %d\n",
1863 cgds.dev_openings, cgds.dev_active, cgds.allocated,
1864 cgds.queued, cgds.held);
1865 }
1866
1867 num_items = 0;
1868
1869 STAILQ_FOREACH(hdr, &softc->work_queue, periph_links.stqe) {
1870 union ctl_io *io = hdr->io_ptr;
1871
1872 num_items++;
1873
1874 /*
1875 * Only regular SCSI I/O is put on the work
1876 * queue, so we can print sense here. There may be no
1877 * sense if it's no the queue for a DMA, but this serves to
1878 * print out the CCB as well.
1879 *
1880 * XXX KDM switch this over to scsi_sense_print() when
1881 * CTL is merged in with CAM.
1882 */
1883 ctl_io_error_print(io, NULL);
1884
1885 /*
1886 * Print DMA status if we are DMA_QUEUED.
1887 */
1888 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) {
1889 xpt_print(periph->path,
1890 "Total %u, Current %u, Resid %u\n",
1891 io->scsiio.kern_total_len,
1892 io->scsiio.kern_data_len,
1893 io->scsiio.kern_data_resid);
1894 }
1895 }
1896
1897 xpt_print(periph->path, "%d requests waiting for CCBs\n", num_items);
1898 xpt_print(periph->path, "%d CTIOs outstanding\n", softc->ctios_sent);
1899 }
1900
1901 /*
1902 * Datamove/done routine called by CTL. Put ourselves on the queue to
1903 * receive a CCB from CAM so we can queue the continue I/O request down
1904 * to the adapter.
1905 */
1906 static void
ctlfe_datamove(union ctl_io * io)1907 ctlfe_datamove(union ctl_io *io)
1908 {
1909 union ccb *ccb;
1910 struct cam_periph *periph;
1911 struct ctlfe_lun_softc *softc;
1912
1913 CTL_IO_ASSERT(io, SCSI);
1914
1915 io->scsiio.ext_data_filled = 0;
1916 ccb = PRIV_CCB(io);
1917 periph = xpt_path_periph(ccb->ccb_h.path);
1918 cam_periph_lock(periph);
1919 softc = (struct ctlfe_lun_softc *)periph->softc;
1920 io->io_hdr.flags |= CTL_FLAG_DMA_QUEUED;
1921 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)
1922 io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
1923 STAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
1924 periph_links.stqe);
1925 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1926 cam_periph_unlock(periph);
1927 }
1928
1929 static void
ctlfe_done(union ctl_io * io)1930 ctlfe_done(union ctl_io *io)
1931 {
1932 union ccb *ccb;
1933 struct cam_periph *periph;
1934 struct ctlfe_lun_softc *softc;
1935
1936 ccb = PRIV_CCB(io);
1937 periph = xpt_path_periph(ccb->ccb_h.path);
1938 cam_periph_lock(periph);
1939 softc = (struct ctlfe_lun_softc *)periph->softc;
1940
1941 if (io->io_hdr.io_type == CTL_IO_TASK) {
1942 /*
1943 * Send the notify acknowledge down to the SIM, to let it
1944 * know we processed the task management command.
1945 */
1946 ccb->ccb_h.status = CAM_REQ_INPROG;
1947 ccb->ccb_h.func_code = XPT_NOTIFY_ACKNOWLEDGE;
1948 switch (io->taskio.task_status) {
1949 case CTL_TASK_FUNCTION_COMPLETE:
1950 ccb->cna2.arg = CAM_RSP_TMF_COMPLETE;
1951 break;
1952 case CTL_TASK_FUNCTION_SUCCEEDED:
1953 ccb->cna2.arg = CAM_RSP_TMF_SUCCEEDED;
1954 ccb->ccb_h.flags |= CAM_SEND_STATUS;
1955 break;
1956 case CTL_TASK_FUNCTION_REJECTED:
1957 ccb->cna2.arg = CAM_RSP_TMF_REJECTED;
1958 ccb->ccb_h.flags |= CAM_SEND_STATUS;
1959 break;
1960 case CTL_TASK_LUN_DOES_NOT_EXIST:
1961 ccb->cna2.arg = CAM_RSP_TMF_INCORRECT_LUN;
1962 ccb->ccb_h.flags |= CAM_SEND_STATUS;
1963 break;
1964 case CTL_TASK_FUNCTION_NOT_SUPPORTED:
1965 ccb->cna2.arg = CAM_RSP_TMF_FAILED;
1966 ccb->ccb_h.flags |= CAM_SEND_STATUS;
1967 break;
1968 }
1969 ccb->cna2.arg |= scsi_3btoul(io->taskio.task_resp) << 8;
1970 xpt_action(ccb);
1971 } else if (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) {
1972 ctlfe_requeue_ccb(periph, ccb, /* unlock */1);
1973 return;
1974 } else {
1975 io->io_hdr.flags |= CTL_FLAG_STATUS_QUEUED;
1976 STAILQ_INSERT_TAIL(&softc->work_queue, &ccb->ccb_h,
1977 periph_links.stqe);
1978 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1979 }
1980
1981 cam_periph_unlock(periph);
1982 }
1983
1984 static void
ctlfe_dump(void)1985 ctlfe_dump(void)
1986 {
1987 struct ctlfe_softc *bus_softc;
1988 struct ctlfe_lun_softc *lun_softc;
1989
1990 STAILQ_FOREACH(bus_softc, &ctlfe_softc_list, links) {
1991 ctlfe_dump_sim(bus_softc->sim);
1992 STAILQ_FOREACH(lun_softc, &bus_softc->lun_softc_list, links)
1993 ctlfe_dump_queue(lun_softc);
1994 }
1995 }
1996