2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Implementation of SCSI Processor Target Peripheral driver for CAM.
6 * Copyright (c) 1998 Justin T. Gibbs.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification, immediately at the beginning of the file.
15 * 2. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/queue.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/types.h>
40 #include <sys/devicestat.h>
41 #include <sys/malloc.h>
46 #include <cam/cam_ccb.h>
47 #include <cam/cam_periph.h>
48 #include <cam/cam_xpt_periph.h>
49 #include <cam/cam_debug.h>
51 #include <cam/scsi/scsi_all.h>
52 #include <cam/scsi/scsi_message.h>
53 #include <cam/scsi/scsi_pt.h>
65 PT_FLAG_DEVICE_INVALID = 0x02,
66 PT_FLAG_RETRY_UA = 0x04
70 PT_CCB_BUFFER_IO = 0x01,
71 PT_CCB_RETRY_UA = 0x04,
72 PT_CCB_BUFFER_IO_UA = PT_CCB_BUFFER_IO|PT_CCB_RETRY_UA
75 /* Offsets into our private area for storing information */
76 #define ccb_state ppriv_field0
77 #define ccb_bp ppriv_ptr1
80 struct bio_queue_head bio_queue;
81 struct devstat *device_stats;
82 LIST_HEAD(, ccb_hdr) pending_ccbs;
89 static d_open_t ptopen;
90 static d_close_t ptclose;
91 static d_strategy_t ptstrategy;
92 static periph_init_t ptinit;
93 static void ptasync(void *callback_arg, u_int32_t code,
94 struct cam_path *path, void *arg);
95 static periph_ctor_t ptctor;
96 static periph_oninv_t ptoninvalidate;
97 static periph_dtor_t ptdtor;
98 static periph_start_t ptstart;
99 static void ptdone(struct cam_periph *periph,
100 union ccb *done_ccb);
101 static d_ioctl_t ptioctl;
102 static int pterror(union ccb *ccb, u_int32_t cam_flags,
103 u_int32_t sense_flags);
105 void scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
106 void (*cbfcnp)(struct cam_periph *, union ccb *),
107 u_int tag_action, int readop, u_int byte2,
108 u_int32_t xfer_len, u_int8_t *data_ptr,
109 u_int8_t sense_len, u_int32_t timeout);
111 static struct periph_driver ptdriver =
114 TAILQ_HEAD_INITIALIZER(ptdriver.units), /* generation */ 0
117 PERIPHDRIVER_DECLARE(pt, ptdriver);
119 static struct cdevsw pt_cdevsw = {
120 .d_version = D_VERSION,
125 .d_write = physwrite,
127 .d_strategy = ptstrategy,
131 #ifndef SCSI_PT_DEFAULT_TIMEOUT
132 #define SCSI_PT_DEFAULT_TIMEOUT 60
136 ptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
138 struct cam_periph *periph;
139 struct pt_softc *softc;
142 periph = (struct cam_periph *)dev->si_drv1;
143 if (cam_periph_acquire(periph) != 0)
146 softc = (struct pt_softc *)periph->softc;
148 cam_periph_lock(periph);
149 if (softc->flags & PT_FLAG_DEVICE_INVALID) {
150 cam_periph_release_locked(periph);
151 cam_periph_unlock(periph);
155 if ((softc->flags & PT_FLAG_OPEN) == 0)
156 softc->flags |= PT_FLAG_OPEN;
159 cam_periph_release(periph);
162 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
163 ("ptopen: dev=%s\n", devtoname(dev)));
165 cam_periph_unlock(periph);
170 ptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
172 struct cam_periph *periph;
173 struct pt_softc *softc;
175 periph = (struct cam_periph *)dev->si_drv1;
176 softc = (struct pt_softc *)periph->softc;
178 cam_periph_lock(periph);
180 softc->flags &= ~PT_FLAG_OPEN;
181 cam_periph_release_locked(periph);
182 cam_periph_unlock(periph);
187 * Actually translate the requested transfer into one the physical driver
188 * can understand. The transfer is described by a buf and will include
189 * only one physical transfer.
192 ptstrategy(struct bio *bp)
194 struct cam_periph *periph;
195 struct pt_softc *softc;
197 periph = (struct cam_periph *)bp->bio_dev->si_drv1;
198 bp->bio_resid = bp->bio_bcount;
199 if (periph == NULL) {
200 biofinish(bp, NULL, ENXIO);
203 cam_periph_lock(periph);
204 softc = (struct pt_softc *)periph->softc;
207 * If the device has been made invalid, error out
209 if ((softc->flags & PT_FLAG_DEVICE_INVALID)) {
210 cam_periph_unlock(periph);
211 biofinish(bp, NULL, ENXIO);
216 * Place it in the queue of disk activities for this disk
218 bioq_insert_tail(&softc->bio_queue, bp);
221 * Schedule ourselves for performing the work.
223 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
224 cam_periph_unlock(periph);
235 * Install a global async callback. This callback will
236 * receive async callbacks like "new device found".
238 status = xpt_register_async(AC_FOUND_DEVICE, ptasync, NULL, NULL);
240 if (status != CAM_REQ_CMP) {
241 printf("pt: Failed to attach master async callback "
242 "due to status 0x%x!\n", status);
247 ptctor(struct cam_periph *periph, void *arg)
249 struct pt_softc *softc;
250 struct ccb_getdev *cgd;
251 struct ccb_pathinq cpi;
252 struct make_dev_args args;
255 cgd = (struct ccb_getdev *)arg;
257 printf("ptregister: no getdev CCB, can't register device\n");
258 return(CAM_REQ_CMP_ERR);
261 softc = (struct pt_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
264 printf("daregister: Unable to probe new device. "
265 "Unable to allocate softc\n");
266 return(CAM_REQ_CMP_ERR);
269 bzero(softc, sizeof(*softc));
270 LIST_INIT(&softc->pending_ccbs);
271 softc->state = PT_STATE_NORMAL;
272 bioq_init(&softc->bio_queue);
274 softc->io_timeout = SCSI_PT_DEFAULT_TIMEOUT * 1000;
276 periph->softc = softc;
278 xpt_path_inq(&cpi, periph->path);
280 cam_periph_unlock(periph);
282 make_dev_args_init(&args);
283 args.mda_devsw = &pt_cdevsw;
284 args.mda_unit = periph->unit_number;
285 args.mda_uid = UID_ROOT;
286 args.mda_gid = GID_OPERATOR;
287 args.mda_mode = 0600;
288 args.mda_si_drv1 = periph;
289 error = make_dev_s(&args, &softc->dev, "%s%d", periph->periph_name,
290 periph->unit_number);
292 cam_periph_lock(periph);
293 return (CAM_REQ_CMP_ERR);
296 softc->device_stats = devstat_new_entry("pt",
297 periph->unit_number, 0,
298 DEVSTAT_NO_BLOCKSIZE,
299 SID_TYPE(&cgd->inq_data) |
300 XPORT_DEVSTAT_TYPE(cpi.transport),
301 DEVSTAT_PRIORITY_OTHER);
303 cam_periph_lock(periph);
306 * Add async callbacks for bus reset and
307 * bus device reset calls. I don't bother
308 * checking if this fails as, in most cases,
309 * the system will function just fine without
310 * them and the only alternative would be to
311 * not attach the device on failure.
313 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
314 ptasync, periph, periph->path);
316 /* Tell the user we've attached to the device */
317 xpt_announce_periph(periph, NULL);
323 ptoninvalidate(struct cam_periph *periph)
325 struct pt_softc *softc;
327 softc = (struct pt_softc *)periph->softc;
330 * De-register any async callbacks.
332 xpt_register_async(0, ptasync, periph, periph->path);
334 softc->flags |= PT_FLAG_DEVICE_INVALID;
337 * Return all queued I/O with ENXIO.
338 * XXX Handle any transactions queued to the card
339 * with XPT_ABORT_CCB.
341 bioq_flush(&softc->bio_queue, NULL, ENXIO);
345 ptdtor(struct cam_periph *periph)
347 struct pt_softc *softc;
349 softc = (struct pt_softc *)periph->softc;
351 devstat_remove_entry(softc->device_stats);
352 cam_periph_unlock(periph);
353 destroy_dev(softc->dev);
354 cam_periph_lock(periph);
355 free(softc, M_DEVBUF);
359 ptasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
361 struct cam_periph *periph;
363 periph = (struct cam_periph *)callback_arg;
365 case AC_FOUND_DEVICE:
367 struct ccb_getdev *cgd;
370 cgd = (struct ccb_getdev *)arg;
374 if (cgd->protocol != PROTO_SCSI)
376 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
378 if (SID_TYPE(&cgd->inq_data) != T_PROCESSOR)
382 * Allocate a peripheral instance for
383 * this device and start the probe
386 status = cam_periph_alloc(ptctor, ptoninvalidate, ptdtor,
387 ptstart, "pt", CAM_PERIPH_BIO,
389 AC_FOUND_DEVICE, cgd);
391 if (status != CAM_REQ_CMP
392 && status != CAM_REQ_INPROG)
393 printf("ptasync: Unable to attach to new device "
394 "due to status 0x%x\n", status);
400 struct pt_softc *softc;
401 struct ccb_hdr *ccbh;
403 softc = (struct pt_softc *)periph->softc;
405 * Don't fail on the expected unit attention
408 softc->flags |= PT_FLAG_RETRY_UA;
409 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
410 ccbh->ccb_state |= PT_CCB_RETRY_UA;
414 cam_periph_async(periph, code, path, arg);
420 ptstart(struct cam_periph *periph, union ccb *start_ccb)
422 struct pt_softc *softc;
425 softc = (struct pt_softc *)periph->softc;
427 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ptstart\n"));
430 * See if there is a buf with work for us to do..
432 bp = bioq_first(&softc->bio_queue);
434 xpt_release_ccb(start_ccb);
436 bioq_remove(&softc->bio_queue, bp);
438 devstat_start_transaction_bio(softc->device_stats, bp);
440 scsi_send_receive(&start_ccb->csio,
444 bp->bio_cmd == BIO_READ,
448 /*sense_len*/SSD_FULL_SIZE,
449 /*timeout*/softc->io_timeout);
451 start_ccb->ccb_h.ccb_state = PT_CCB_BUFFER_IO_UA;
454 * Block out any asynchronous callbacks
455 * while we touch the pending ccb list.
457 LIST_INSERT_HEAD(&softc->pending_ccbs, &start_ccb->ccb_h,
460 start_ccb->ccb_h.ccb_bp = bp;
461 bp = bioq_first(&softc->bio_queue);
463 xpt_action(start_ccb);
466 /* Have more work to do, so ensure we stay scheduled */
467 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
473 ptdone(struct cam_periph *periph, union ccb *done_ccb)
475 struct pt_softc *softc;
476 struct ccb_scsiio *csio;
478 softc = (struct pt_softc *)periph->softc;
480 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ptdone\n"));
482 csio = &done_ccb->csio;
483 switch (csio->ccb_h.ccb_state) {
484 case PT_CCB_BUFFER_IO:
485 case PT_CCB_BUFFER_IO_UA:
489 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
490 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
494 if ((csio->ccb_h.ccb_state & PT_CCB_RETRY_UA) != 0)
499 error = pterror(done_ccb, CAM_RETRY_SELTO, sf);
500 if (error == ERESTART) {
502 * A retry was scheuled, so
508 if (error == ENXIO) {
510 * Catastrophic error. Mark our device
513 xpt_print(periph->path,
514 "Invalidating device\n");
515 softc->flags |= PT_FLAG_DEVICE_INVALID;
519 * return all queued I/O with EIO, so that
520 * the client can retry these I/Os in the
521 * proper order should it attempt to recover.
523 bioq_flush(&softc->bio_queue, NULL, EIO);
524 bp->bio_error = error;
525 bp->bio_resid = bp->bio_bcount;
526 bp->bio_flags |= BIO_ERROR;
528 bp->bio_resid = csio->resid;
530 if (bp->bio_resid != 0) {
531 /* Short transfer ??? */
532 bp->bio_flags |= BIO_ERROR;
535 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
536 cam_release_devq(done_ccb->ccb_h.path,
542 bp->bio_resid = csio->resid;
543 if (bp->bio_resid != 0)
544 bp->bio_flags |= BIO_ERROR;
548 * Block out any asynchronous callbacks
549 * while we touch the pending ccb list.
551 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
553 biofinish(bp, softc->device_stats, 0);
557 xpt_release_ccb(done_ccb);
561 pterror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
563 struct pt_softc *softc;
564 struct cam_periph *periph;
566 periph = xpt_path_periph(ccb->ccb_h.path);
567 softc = (struct pt_softc *)periph->softc;
569 return(cam_periph_error(ccb, cam_flags, sense_flags));
573 ptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
575 struct cam_periph *periph;
576 struct pt_softc *softc;
579 periph = (struct cam_periph *)dev->si_drv1;
580 softc = (struct pt_softc *)periph->softc;
582 cam_periph_lock(periph);
585 case PTIOCGETTIMEOUT:
586 if (softc->io_timeout >= 1000)
587 *(int *)addr = softc->io_timeout / 1000;
591 case PTIOCSETTIMEOUT:
592 if (*(int *)addr < 1) {
597 softc->io_timeout = *(int *)addr * 1000;
601 error = cam_periph_ioctl(periph, cmd, addr, pterror);
605 cam_periph_unlock(periph);
611 scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
612 void (*cbfcnp)(struct cam_periph *, union ccb *),
613 u_int tag_action, int readop, u_int byte2,
614 u_int32_t xfer_len, u_int8_t *data_ptr, u_int8_t sense_len,
617 struct scsi_send_receive *scsi_cmd;
619 scsi_cmd = (struct scsi_send_receive *)&csio->cdb_io.cdb_bytes;
620 scsi_cmd->opcode = readop ? RECEIVE : SEND;
621 scsi_cmd->byte2 = byte2;
622 scsi_ulto3b(xfer_len, scsi_cmd->xfer_len);
623 scsi_cmd->control = 0;
628 /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT,