2 * Implementation of SCSI Processor Target Peripheral driver for CAM.
4 * Copyright (c) 1998 Justin T. Gibbs.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/sys/cam/scsi/scsi_pt.c,v 1.17 2000/01/17 06:27:37 mjacob Exp $
29 * $DragonFly: src/sys/bus/cam/scsi/scsi_pt.c,v 1.23 2008/05/18 20:30:20 pavalos Exp $
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/types.h>
38 #include <sys/devicestat.h>
39 #include <sys/malloc.h>
43 #include <sys/thread2.h>
46 #include "../cam_ccb.h"
47 #include "../cam_extend.h"
48 #include "../cam_periph.h"
49 #include "../cam_xpt_periph.h"
50 #include "../cam_debug.h"
53 #include "scsi_message.h"
66 PT_FLAG_DEVICE_INVALID = 0x02,
67 PT_FLAG_RETRY_UA = 0x04
71 PT_CCB_BUFFER_IO = 0x01,
72 PT_CCB_WAITING = 0x02,
73 PT_CCB_RETRY_UA = 0x04,
74 PT_CCB_BUFFER_IO_UA = PT_CCB_BUFFER_IO|PT_CCB_RETRY_UA
77 /* Offsets into our private area for storing information */
78 #define ccb_state ppriv_field0
79 #define ccb_bio ppriv_ptr1
82 struct bio_queue_head bio_queue;
83 struct devstat device_stats;
84 LIST_HEAD(, ccb_hdr) pending_ccbs;
92 static d_open_t ptopen;
93 static d_close_t ptclose;
94 static d_strategy_t ptstrategy;
95 static periph_init_t ptinit;
96 static void ptasync(void *callback_arg, u_int32_t code,
97 struct cam_path *path, void *arg);
98 static periph_ctor_t ptctor;
99 static periph_oninv_t ptoninvalidate;
100 static periph_dtor_t ptdtor;
101 static periph_start_t ptstart;
102 static void ptdone(struct cam_periph *periph,
103 union ccb *done_ccb);
104 static d_ioctl_t ptioctl;
105 static int pterror(union ccb *ccb, u_int32_t cam_flags,
106 u_int32_t sense_flags);
108 void scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
109 void (*cbfcnp)(struct cam_periph *, union ccb *),
110 u_int tag_action, int readop, u_int byte2,
111 u_int32_t xfer_len, u_int8_t *data_ptr,
112 u_int8_t sense_len, u_int32_t timeout);
114 static struct periph_driver ptdriver =
117 TAILQ_HEAD_INITIALIZER(ptdriver.units), /* generation */ 0
120 PERIPHDRIVER_DECLARE(pt, ptdriver);
122 #define PT_CDEV_MAJOR 61
124 static struct dev_ops pt_ops = {
125 { "pt", PT_CDEV_MAJOR, 0 },
129 .d_write = physwrite,
131 .d_strategy = ptstrategy,
134 static struct extend_array *ptperiphs;
136 #ifndef SCSI_PT_DEFAULT_TIMEOUT
137 #define SCSI_PT_DEFAULT_TIMEOUT 60
141 ptopen(struct dev_open_args *ap)
143 cdev_t dev = ap->a_head.a_dev;
144 struct cam_periph *periph;
145 struct pt_softc *softc;
150 periph = cam_extend_get(ptperiphs, unit);
151 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
154 softc = (struct pt_softc *)periph->softc;
156 cam_periph_lock(periph);
157 if (softc->flags & PT_FLAG_DEVICE_INVALID) {
158 cam_periph_unlock(periph);
159 cam_periph_release(periph);
163 if ((softc->flags & PT_FLAG_OPEN) == 0)
164 softc->flags |= PT_FLAG_OPEN;
167 cam_periph_release(periph);
170 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
171 ("ptopen: dev=%s\n", devtoname(dev)));
173 cam_periph_unlock(periph);
178 ptclose(struct dev_close_args *ap)
180 cdev_t dev = ap->a_head.a_dev;
181 struct cam_periph *periph;
182 struct pt_softc *softc;
186 periph = cam_extend_get(ptperiphs, unit);
190 softc = (struct pt_softc *)periph->softc;
192 cam_periph_lock(periph);
194 softc->flags &= ~PT_FLAG_OPEN;
195 cam_periph_unlock(periph);
196 cam_periph_release(periph);
201 * Actually translate the requested transfer into one the physical driver
202 * can understand. The transfer is described by a buf and will include
203 * only one physical transfer.
206 ptstrategy(struct dev_strategy_args *ap)
208 cdev_t dev = ap->a_head.a_dev;
209 struct bio *bio = ap->a_bio;
210 struct buf *bp = bio->bio_buf;
211 struct cam_periph *periph;
212 struct pt_softc *softc;
216 periph = cam_extend_get(ptperiphs, unit);
217 if (periph == NULL) {
221 cam_periph_lock(periph);
222 softc = (struct pt_softc *)periph->softc;
225 * If the device has been made invalid, error out
227 if ((softc->flags & PT_FLAG_DEVICE_INVALID)) {
228 cam_periph_unlock(periph);
234 * Place it in the queue of disk activities for this disk
236 bioq_insert_tail(&softc->bio_queue, bio);
239 * Schedule ourselves for performing the work.
241 xpt_schedule(periph, /* XXX priority */1);
242 cam_periph_unlock(periph);
246 bp->b_flags |= B_ERROR;
249 * Correctly set the buf to indicate a completed xfer
251 bp->b_resid = bp->b_bcount;
262 * Create our extend array for storing the devices we attach to.
264 ptperiphs = cam_extend_new();
265 if (ptperiphs == NULL) {
266 kprintf("pt: Failed to alloc extend array!\n");
271 * Install a global async callback. This callback will
272 * receive async callbacks like "new device found".
274 status = xpt_register_async(AC_FOUND_DEVICE, ptasync, NULL, NULL);
276 if (status != CAM_REQ_CMP) {
277 kprintf("pt: Failed to attach master async callback "
278 "due to status 0x%x!\n", status);
283 ptctor(struct cam_periph *periph, void *arg)
285 struct pt_softc *softc;
286 struct ccb_getdev *cgd;
288 cgd = (struct ccb_getdev *)arg;
289 if (periph == NULL) {
290 kprintf("ptregister: periph was NULL!!\n");
291 return(CAM_REQ_CMP_ERR);
295 kprintf("ptregister: no getdev CCB, can't register device\n");
296 return(CAM_REQ_CMP_ERR);
299 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
300 LIST_INIT(&softc->pending_ccbs);
301 softc->state = PT_STATE_NORMAL;
302 bioq_init(&softc->bio_queue);
304 softc->io_timeout = SCSI_PT_DEFAULT_TIMEOUT * 1000;
306 periph->softc = softc;
308 cam_periph_unlock(periph);
309 cam_extend_set(ptperiphs, periph->unit_number, periph);
311 devstat_add_entry(&softc->device_stats, "pt",
312 periph->unit_number, 0,
313 DEVSTAT_NO_BLOCKSIZE,
314 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
315 DEVSTAT_PRIORITY_OTHER);
317 make_dev(&pt_ops, periph->unit_number, UID_ROOT,
318 GID_OPERATOR, 0600, "%s%d", periph->periph_name,
319 periph->unit_number);
320 cam_periph_lock(periph);
322 * Add async callbacks for bus reset and
323 * bus device reset calls. I don't bother
324 * checking if this fails as, in most cases,
325 * the system will function just fine without
326 * them and the only alternative would be to
327 * not attach the device on failure.
329 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
330 ptasync, periph, periph->path);
332 /* Tell the user we've attached to the device */
333 xpt_announce_periph(periph, NULL);
339 ptoninvalidate(struct cam_periph *periph)
341 struct pt_softc *softc;
345 softc = (struct pt_softc *)periph->softc;
348 * De-register any async callbacks.
350 xpt_register_async(0, ptasync, periph, periph->path);
352 softc->flags |= PT_FLAG_DEVICE_INVALID;
355 * Return all queued I/O with ENXIO.
356 * XXX Handle any transactions queued to the card
357 * with XPT_ABORT_CCB.
359 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){
360 bioq_remove(&softc->bio_queue, q_bio);
361 q_bp = q_bio->bio_buf;
362 q_bp->b_resid = q_bp->b_bcount;
363 q_bp->b_error = ENXIO;
364 q_bp->b_flags |= B_ERROR;
368 xpt_print(periph->path, "lost device\n");
372 ptdtor(struct cam_periph *periph)
374 struct pt_softc *softc;
376 softc = (struct pt_softc *)periph->softc;
378 devstat_remove_entry(&softc->device_stats);
380 cam_extend_release(ptperiphs, periph->unit_number);
381 xpt_print(periph->path, "removing device entry\n");
382 dev_ops_remove_minor(&pt_ops, periph->unit_number);
383 kfree(softc, M_DEVBUF);
387 ptasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
389 struct cam_periph *periph;
391 periph = (struct cam_periph *)callback_arg;
393 case AC_FOUND_DEVICE:
395 struct ccb_getdev *cgd;
398 cgd = (struct ccb_getdev *)arg;
402 if (SID_TYPE(&cgd->inq_data) != T_PROCESSOR)
406 * Allocate a peripheral instance for
407 * this device and start the probe
410 status = cam_periph_alloc(ptctor, ptoninvalidate, ptdtor,
411 ptstart, "pt", CAM_PERIPH_BIO,
412 cgd->ccb_h.path, ptasync,
413 AC_FOUND_DEVICE, cgd);
415 if (status != CAM_REQ_CMP
416 && status != CAM_REQ_INPROG)
417 kprintf("ptasync: Unable to attach to new device "
418 "due to status 0x%x\n", status);
424 struct pt_softc *softc;
425 struct ccb_hdr *ccbh;
427 softc = (struct pt_softc *)periph->softc;
429 * Don't fail on the expected unit attention
432 softc->flags |= PT_FLAG_RETRY_UA;
433 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
434 ccbh->ccb_state |= PT_CCB_RETRY_UA;
438 cam_periph_async(periph, code, path, arg);
444 ptstart(struct cam_periph *periph, union ccb *start_ccb)
446 struct pt_softc *softc;
450 softc = (struct pt_softc *)periph->softc;
453 * See if there is a buf with work for us to do..
455 bio = bioq_first(&softc->bio_queue);
456 if (periph->immediate_priority <= periph->pinfo.priority) {
457 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
458 ("queuing for immediate ccb\n"));
459 start_ccb->ccb_h.ccb_state = PT_CCB_WAITING;
460 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
462 periph->immediate_priority = CAM_PRIORITY_NONE;
463 wakeup(&periph->ccb_list);
464 } else if (bio == NULL) {
465 xpt_release_ccb(start_ccb);
467 bioq_remove(&softc->bio_queue, bio);
470 devstat_start_transaction(&softc->device_stats);
472 scsi_send_receive(&start_ccb->csio,
476 (bp->b_cmd == BUF_CMD_READ),
480 /*sense_len*/SSD_FULL_SIZE,
481 /*timeout*/softc->io_timeout);
483 start_ccb->ccb_h.ccb_state = PT_CCB_BUFFER_IO_UA;
486 * Block out any asyncronous callbacks
487 * while we touch the pending ccb list.
489 LIST_INSERT_HEAD(&softc->pending_ccbs, &start_ccb->ccb_h,
492 start_ccb->ccb_h.ccb_bio = bio;
493 bio = bioq_first(&softc->bio_queue);
495 xpt_action(start_ccb);
498 /* Have more work to do, so ensure we stay scheduled */
499 xpt_schedule(periph, /* XXX priority */1);
505 ptdone(struct cam_periph *periph, union ccb *done_ccb)
507 struct pt_softc *softc;
508 struct ccb_scsiio *csio;
510 softc = (struct pt_softc *)periph->softc;
511 csio = &done_ccb->csio;
512 switch (csio->ccb_h.ccb_state) {
513 case PT_CCB_BUFFER_IO:
514 case PT_CCB_BUFFER_IO_UA:
519 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
522 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
526 if ((csio->ccb_h.ccb_state & PT_CCB_RETRY_UA) != 0)
531 error = pterror(done_ccb, CAM_RETRY_SELTO, sf);
532 if (error == ERESTART) {
534 * A retry was scheuled, so
543 if (error == ENXIO) {
545 * Catastrophic error. Mark our device
548 xpt_print(periph->path,
549 "Invalidating device\n");
550 softc->flags |= PT_FLAG_DEVICE_INVALID;
554 * return all queued I/O with EIO, so that
555 * the client can retry these I/Os in the
556 * proper order should it attempt to recover.
558 while ((q_bio = bioq_first(&softc->bio_queue))
560 bioq_remove(&softc->bio_queue, q_bio);
561 q_bp = q_bio->bio_buf;
562 q_bp->b_resid = q_bp->b_bcount;
564 q_bp->b_flags |= B_ERROR;
568 bp->b_resid = bp->b_bcount;
569 bp->b_flags |= B_ERROR;
571 bp->b_resid = csio->resid;
573 if (bp->b_resid != 0) {
574 /* Short transfer ??? */
575 bp->b_flags |= B_ERROR;
578 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
579 cam_release_devq(done_ccb->ccb_h.path,
585 bp->b_resid = csio->resid;
586 if (bp->b_resid != 0)
587 bp->b_flags |= B_ERROR;
591 * Block out any asyncronous callbacks
592 * while we touch the pending ccb list.
594 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
596 devstat_end_transaction_buf(&softc->device_stats, bp);
601 /* Caller will release the CCB */
602 wakeup(&done_ccb->ccb_h.cbfcnp);
605 xpt_release_ccb(done_ccb);
609 pterror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
611 struct pt_softc *softc;
612 struct cam_periph *periph;
614 periph = xpt_path_periph(ccb->ccb_h.path);
615 softc = (struct pt_softc *)periph->softc;
617 return(cam_periph_error(ccb, cam_flags, sense_flags,
622 ptioctl(struct dev_ioctl_args *ap)
624 cdev_t dev = ap->a_head.a_dev;
625 caddr_t addr = ap->a_data;
626 struct cam_periph *periph;
627 struct pt_softc *softc;
632 periph = cam_extend_get(ptperiphs, unit);
637 softc = (struct pt_softc *)periph->softc;
639 cam_periph_lock(periph);
642 case PTIOCGETTIMEOUT:
643 if (softc->io_timeout >= 1000)
644 *(int *)addr = softc->io_timeout / 1000;
648 case PTIOCSETTIMEOUT:
649 if (*(int *)addr < 1) {
654 softc->io_timeout = *(int *)addr * 1000;
658 error = cam_periph_ioctl(periph, ap->a_cmd, addr, pterror);
662 cam_periph_unlock(periph);
668 scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
669 void (*cbfcnp)(struct cam_periph *, union ccb *),
670 u_int tag_action, int readop, u_int byte2,
671 u_int32_t xfer_len, u_int8_t *data_ptr, u_int8_t sense_len,
674 struct scsi_send_receive *scsi_cmd;
676 scsi_cmd = (struct scsi_send_receive *)&csio->cdb_io.cdb_bytes;
677 scsi_cmd->opcode = readop ? RECEIVE : SEND;
678 scsi_cmd->byte2 = byte2;
679 scsi_ulto3b(xfer_len, scsi_cmd->xfer_len);
680 scsi_cmd->control = 0;
685 /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT,