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;
90 static d_open_t ptopen;
91 static d_close_t ptclose;
92 static d_strategy_t ptstrategy;
93 static periph_init_t ptinit;
94 static void ptasync(void *callback_arg, u_int32_t code,
95 struct cam_path *path, void *arg);
96 static periph_ctor_t ptctor;
97 static periph_oninv_t ptoninvalidate;
98 static periph_dtor_t ptdtor;
99 static periph_start_t ptstart;
100 static void ptdone(struct cam_periph *periph,
101 union ccb *done_ccb);
102 static d_ioctl_t ptioctl;
103 static int pterror(union ccb *ccb, u_int32_t cam_flags,
104 u_int32_t sense_flags);
106 void scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
107 void (*cbfcnp)(struct cam_periph *, union ccb *),
108 u_int tag_action, int readop, u_int byte2,
109 u_int32_t xfer_len, u_int8_t *data_ptr,
110 u_int8_t sense_len, u_int32_t timeout);
112 static struct periph_driver ptdriver =
115 TAILQ_HEAD_INITIALIZER(ptdriver.units), /* generation */ 0
118 PERIPHDRIVER_DECLARE(pt, ptdriver);
121 static struct cdevsw pt_cdevsw = {
122 .d_version = D_VERSION,
127 .d_write = physwrite,
129 .d_strategy = ptstrategy,
133 #ifndef SCSI_PT_DEFAULT_TIMEOUT
134 #define SCSI_PT_DEFAULT_TIMEOUT 60
138 ptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
140 struct cam_periph *periph;
141 struct pt_softc *softc;
144 periph = (struct cam_periph *)dev->si_drv1;
145 if (cam_periph_acquire(periph) != 0)
148 softc = (struct pt_softc *)periph->softc;
150 cam_periph_lock(periph);
151 if (softc->flags & PT_FLAG_DEVICE_INVALID) {
152 cam_periph_release_locked(periph);
153 cam_periph_unlock(periph);
157 if ((softc->flags & PT_FLAG_OPEN) == 0)
158 softc->flags |= PT_FLAG_OPEN;
161 cam_periph_release(periph);
164 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
165 ("ptopen: dev=%s\n", devtoname(dev)));
167 cam_periph_unlock(periph);
172 ptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
174 struct cam_periph *periph;
175 struct pt_softc *softc;
177 periph = (struct cam_periph *)dev->si_drv1;
178 softc = (struct pt_softc *)periph->softc;
180 cam_periph_lock(periph);
182 softc->flags &= ~PT_FLAG_OPEN;
183 cam_periph_release_locked(periph);
184 cam_periph_unlock(periph);
189 * Actually translate the requested transfer into one the physical driver
190 * can understand. The transfer is described by a buf and will include
191 * only one physical transfer.
194 ptstrategy(struct bio *bp)
196 struct cam_periph *periph;
197 struct pt_softc *softc;
199 periph = (struct cam_periph *)bp->bio_dev->si_drv1;
200 bp->bio_resid = bp->bio_bcount;
201 if (periph == NULL) {
202 biofinish(bp, NULL, ENXIO);
205 cam_periph_lock(periph);
206 softc = (struct pt_softc *)periph->softc;
209 * If the device has been made invalid, error out
211 if ((softc->flags & PT_FLAG_DEVICE_INVALID)) {
212 cam_periph_unlock(periph);
213 biofinish(bp, NULL, ENXIO);
218 * Place it in the queue of disk activities for this disk
220 bioq_insert_tail(&softc->bio_queue, bp);
223 * Schedule ourselves for performing the work.
225 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
226 cam_periph_unlock(periph);
237 * Install a global async callback. This callback will
238 * receive async callbacks like "new device found".
240 status = xpt_register_async(AC_FOUND_DEVICE, ptasync, NULL, NULL);
242 if (status != CAM_REQ_CMP) {
243 printf("pt: Failed to attach master async callback "
244 "due to status 0x%x!\n", status);
249 ptctor(struct cam_periph *periph, void *arg)
251 struct pt_softc *softc;
252 struct ccb_getdev *cgd;
253 struct ccb_pathinq cpi;
254 struct make_dev_args args;
257 cgd = (struct ccb_getdev *)arg;
259 printf("ptregister: no getdev CCB, can't register device\n");
260 return(CAM_REQ_CMP_ERR);
263 softc = (struct pt_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
266 printf("daregister: Unable to probe new device. "
267 "Unable to allocate softc\n");
268 return(CAM_REQ_CMP_ERR);
271 bzero(softc, sizeof(*softc));
272 LIST_INIT(&softc->pending_ccbs);
273 softc->state = PT_STATE_NORMAL;
274 bioq_init(&softc->bio_queue);
276 softc->io_timeout = SCSI_PT_DEFAULT_TIMEOUT * 1000;
278 periph->softc = softc;
280 xpt_path_inq(&cpi, periph->path);
282 cam_periph_unlock(periph);
284 make_dev_args_init(&args);
285 args.mda_devsw = &pt_cdevsw;
286 args.mda_unit = periph->unit_number;
287 args.mda_uid = UID_ROOT;
288 args.mda_gid = GID_OPERATOR;
289 args.mda_mode = 0600;
290 args.mda_si_drv1 = periph;
291 error = make_dev_s(&args, &softc->dev, "%s%d", periph->periph_name,
292 periph->unit_number);
294 cam_periph_lock(periph);
295 return (CAM_REQ_CMP_ERR);
298 softc->device_stats = devstat_new_entry("pt",
299 periph->unit_number, 0,
300 DEVSTAT_NO_BLOCKSIZE,
301 SID_TYPE(&cgd->inq_data) |
302 XPORT_DEVSTAT_TYPE(cpi.transport),
303 DEVSTAT_PRIORITY_OTHER);
305 cam_periph_lock(periph);
308 * Add async callbacks for bus reset and
309 * bus device reset calls. I don't bother
310 * checking if this fails as, in most cases,
311 * the system will function just fine without
312 * them and the only alternative would be to
313 * not attach the device on failure.
315 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
316 ptasync, periph, periph->path);
318 /* Tell the user we've attached to the device */
319 xpt_announce_periph(periph, NULL);
325 ptoninvalidate(struct cam_periph *periph)
327 struct pt_softc *softc;
329 softc = (struct pt_softc *)periph->softc;
332 * De-register any async callbacks.
334 xpt_register_async(0, ptasync, periph, periph->path);
336 softc->flags |= PT_FLAG_DEVICE_INVALID;
339 * Return all queued I/O with ENXIO.
340 * XXX Handle any transactions queued to the card
341 * with XPT_ABORT_CCB.
343 bioq_flush(&softc->bio_queue, NULL, ENXIO);
347 ptdtor(struct cam_periph *periph)
349 struct pt_softc *softc;
351 softc = (struct pt_softc *)periph->softc;
353 devstat_remove_entry(softc->device_stats);
354 cam_periph_unlock(periph);
355 destroy_dev(softc->dev);
356 cam_periph_lock(periph);
357 free(softc, M_DEVBUF);
361 ptasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
363 struct cam_periph *periph;
365 periph = (struct cam_periph *)callback_arg;
367 case AC_FOUND_DEVICE:
369 struct ccb_getdev *cgd;
372 cgd = (struct ccb_getdev *)arg;
376 if (cgd->protocol != PROTO_SCSI)
378 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
380 if (SID_TYPE(&cgd->inq_data) != T_PROCESSOR)
384 * Allocate a peripheral instance for
385 * this device and start the probe
388 status = cam_periph_alloc(ptctor, ptoninvalidate, ptdtor,
389 ptstart, "pt", CAM_PERIPH_BIO,
391 AC_FOUND_DEVICE, cgd);
393 if (status != CAM_REQ_CMP
394 && status != CAM_REQ_INPROG)
395 printf("ptasync: Unable to attach to new device "
396 "due to status 0x%x\n", status);
402 struct pt_softc *softc;
403 struct ccb_hdr *ccbh;
405 softc = (struct pt_softc *)periph->softc;
407 * Don't fail on the expected unit attention
410 softc->flags |= PT_FLAG_RETRY_UA;
411 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
412 ccbh->ccb_state |= PT_CCB_RETRY_UA;
416 cam_periph_async(periph, code, path, arg);
422 ptstart(struct cam_periph *periph, union ccb *start_ccb)
424 struct pt_softc *softc;
427 softc = (struct pt_softc *)periph->softc;
429 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ptstart\n"));
432 * See if there is a buf with work for us to do..
434 bp = bioq_first(&softc->bio_queue);
436 xpt_release_ccb(start_ccb);
438 bioq_remove(&softc->bio_queue, bp);
440 devstat_start_transaction_bio(softc->device_stats, bp);
442 scsi_send_receive(&start_ccb->csio,
446 bp->bio_cmd == BIO_READ,
450 /*sense_len*/SSD_FULL_SIZE,
451 /*timeout*/softc->io_timeout);
453 start_ccb->ccb_h.ccb_state = PT_CCB_BUFFER_IO_UA;
456 * Block out any asynchronous callbacks
457 * while we touch the pending ccb list.
459 LIST_INSERT_HEAD(&softc->pending_ccbs, &start_ccb->ccb_h,
462 start_ccb->ccb_h.ccb_bp = bp;
463 bp = bioq_first(&softc->bio_queue);
465 xpt_action(start_ccb);
468 /* Have more work to do, so ensure we stay scheduled */
469 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
475 ptdone(struct cam_periph *periph, union ccb *done_ccb)
477 struct pt_softc *softc;
478 struct ccb_scsiio *csio;
480 softc = (struct pt_softc *)periph->softc;
482 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("ptdone\n"));
484 csio = &done_ccb->csio;
485 switch (csio->ccb_h.ccb_state) {
486 case PT_CCB_BUFFER_IO:
487 case PT_CCB_BUFFER_IO_UA:
491 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
492 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
496 if ((csio->ccb_h.ccb_state & PT_CCB_RETRY_UA) != 0)
501 error = pterror(done_ccb, CAM_RETRY_SELTO, sf);
502 if (error == ERESTART) {
504 * A retry was scheuled, so
510 if (error == ENXIO) {
512 * Catastrophic error. Mark our device
515 xpt_print(periph->path,
516 "Invalidating device\n");
517 softc->flags |= PT_FLAG_DEVICE_INVALID;
521 * return all queued I/O with EIO, so that
522 * the client can retry these I/Os in the
523 * proper order should it attempt to recover.
525 bioq_flush(&softc->bio_queue, NULL, EIO);
526 bp->bio_error = error;
527 bp->bio_resid = bp->bio_bcount;
528 bp->bio_flags |= BIO_ERROR;
530 bp->bio_resid = csio->resid;
532 if (bp->bio_resid != 0) {
533 /* Short transfer ??? */
534 bp->bio_flags |= BIO_ERROR;
537 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
538 cam_release_devq(done_ccb->ccb_h.path,
544 bp->bio_resid = csio->resid;
545 if (bp->bio_resid != 0)
546 bp->bio_flags |= BIO_ERROR;
550 * Block out any asynchronous callbacks
551 * while we touch the pending ccb list.
553 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
555 biofinish(bp, softc->device_stats, 0);
559 xpt_release_ccb(done_ccb);
563 pterror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
565 struct pt_softc *softc;
566 struct cam_periph *periph;
568 periph = xpt_path_periph(ccb->ccb_h.path);
569 softc = (struct pt_softc *)periph->softc;
571 return(cam_periph_error(ccb, cam_flags, sense_flags));
575 ptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
577 struct cam_periph *periph;
578 struct pt_softc *softc;
581 periph = (struct cam_periph *)dev->si_drv1;
582 softc = (struct pt_softc *)periph->softc;
584 cam_periph_lock(periph);
587 case PTIOCGETTIMEOUT:
588 if (softc->io_timeout >= 1000)
589 *(int *)addr = softc->io_timeout / 1000;
593 case PTIOCSETTIMEOUT:
594 if (*(int *)addr < 1) {
599 softc->io_timeout = *(int *)addr * 1000;
603 error = cam_periph_ioctl(periph, cmd, addr, pterror);
607 cam_periph_unlock(periph);
613 scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
614 void (*cbfcnp)(struct cam_periph *, union ccb *),
615 u_int tag_action, int readop, u_int byte2,
616 u_int32_t xfer_len, u_int8_t *data_ptr, u_int8_t sense_len,
619 struct scsi_send_receive *scsi_cmd;
621 scsi_cmd = (struct scsi_send_receive *)&csio->cdb_io.cdb_bytes;
622 scsi_cmd->opcode = readop ? RECEIVE : SEND;
623 scsi_cmd->byte2 = byte2;
624 scsi_ulto3b(xfer_len, scsi_cmd->xfer_len);
625 scsi_cmd->control = 0;
630 /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT,