Rewrite of the CAM error recovery code.
[dragonfly.git] / sys / bus / cam / scsi / scsi_pt.c
CommitLineData
984263bc
MD
1/*
2 * Implementation of SCSI Processor Target Peripheral driver for CAM.
3 *
4 * Copyright (c) 1998 Justin T. Gibbs.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification, 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.
15 *
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
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: src/sys/cam/scsi/scsi_pt.c,v 1.17 2000/01/17 06:27:37 mjacob Exp $
b05e84c9 29 * $DragonFly: src/sys/bus/cam/scsi/scsi_pt.c,v 1.20 2007/11/18 17:53:01 pavalos Exp $
984263bc
MD
30 */
31
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>
37#include <sys/buf.h>
38#include <sys/devicestat.h>
39#include <sys/malloc.h>
40#include <sys/conf.h>
41#include <sys/ptio.h>
f15db79e 42#include <sys/buf2.h>
4e01b467 43#include <sys/thread2.h>
984263bc 44
1f2de5d4
MD
45#include "../cam.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"
984263bc 51
1f2de5d4
MD
52#include "scsi_all.h"
53#include "scsi_message.h"
54#include "scsi_pt.h"
984263bc
MD
55
56#include "opt_pt.h"
57
58typedef enum {
59 PT_STATE_PROBE,
60 PT_STATE_NORMAL
61} pt_state;
62
63typedef enum {
64 PT_FLAG_NONE = 0x00,
65 PT_FLAG_OPEN = 0x01,
66 PT_FLAG_DEVICE_INVALID = 0x02,
67 PT_FLAG_RETRY_UA = 0x04
68} pt_flags;
69
70typedef enum {
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
75} pt_ccb_state;
76
77/* Offsets into our private area for storing information */
78#define ccb_state ppriv_field0
81b5c339 79#define ccb_bio ppriv_ptr1
984263bc
MD
80
81struct pt_softc {
81b5c339 82 struct bio_queue_head bio_queue;
984263bc
MD
83 struct devstat device_stats;
84 LIST_HEAD(, ccb_hdr) pending_ccbs;
85 pt_state state;
86 pt_flags flags;
87 union ccb saved_ccb;
88 int io_timeout;
b13267a5 89 cdev_t dev;
984263bc
MD
90};
91
92static d_open_t ptopen;
93static d_close_t ptclose;
94static d_strategy_t ptstrategy;
95static periph_init_t ptinit;
96static void ptasync(void *callback_arg, u_int32_t code,
97 struct cam_path *path, void *arg);
98static periph_ctor_t ptctor;
99static periph_oninv_t ptoninvalidate;
100static periph_dtor_t ptdtor;
101static periph_start_t ptstart;
102static void ptdone(struct cam_periph *periph,
103 union ccb *done_ccb);
104static d_ioctl_t ptioctl;
105static int pterror(union ccb *ccb, u_int32_t cam_flags,
106 u_int32_t sense_flags);
107
108void 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);
113
114static struct periph_driver ptdriver =
115{
116 ptinit, "pt",
117 TAILQ_HEAD_INITIALIZER(ptdriver.units), /* generation */ 0
118};
119
2ad14cb5 120PERIPHDRIVER_DECLARE(pt, ptdriver);
984263bc
MD
121
122#define PT_CDEV_MAJOR 61
123
fef8985e
MD
124static struct dev_ops pt_ops = {
125 { "pt", PT_CDEV_MAJOR, 0 },
126 .d_open = ptopen,
127 .d_close = ptclose,
128 .d_read = physread,
129 .d_write = physwrite,
130 .d_ioctl = ptioctl,
131 .d_strategy = ptstrategy,
984263bc
MD
132};
133
134static struct extend_array *ptperiphs;
135
136#ifndef SCSI_PT_DEFAULT_TIMEOUT
137#define SCSI_PT_DEFAULT_TIMEOUT 60
138#endif
139
140static int
fef8985e 141ptopen(struct dev_open_args *ap)
984263bc 142{
b13267a5 143 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
144 struct cam_periph *periph;
145 struct pt_softc *softc;
146 int unit;
147 int error;
984263bc
MD
148
149 unit = minor(dev);
150 periph = cam_extend_get(ptperiphs, unit);
151 if (periph == NULL)
152 return (ENXIO);
153
154 softc = (struct pt_softc *)periph->softc;
155
4e01b467 156 crit_enter();
984263bc 157 if (softc->flags & PT_FLAG_DEVICE_INVALID) {
4e01b467 158 crit_exit();
984263bc
MD
159 return(ENXIO);
160 }
161
162 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
163 ("ptopen: dev=%s (unit %d)\n", devtoname(dev), unit));
164
377d4740 165 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
4e01b467 166 crit_exit();
984263bc
MD
167 return (error); /* error code from tsleep */
168 }
169
4e01b467 170 crit_exit();
984263bc
MD
171
172 if ((softc->flags & PT_FLAG_OPEN) == 0) {
173 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
174 error = ENXIO;
175 else
176 softc->flags |= PT_FLAG_OPEN;
177 } else
178 error = EBUSY;
179
180 cam_periph_unlock(periph);
181 return (error);
182}
183
184static int
fef8985e 185ptclose(struct dev_close_args *ap)
984263bc 186{
b13267a5 187 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
188 struct cam_periph *periph;
189 struct pt_softc *softc;
190 int unit;
191 int error;
192
193 unit = minor(dev);
194 periph = cam_extend_get(ptperiphs, unit);
195 if (periph == NULL)
196 return (ENXIO);
197
198 softc = (struct pt_softc *)periph->softc;
199
377d4740 200 if ((error = cam_periph_lock(periph, 0)) != 0)
984263bc
MD
201 return (error); /* error code from tsleep */
202
203 softc->flags &= ~PT_FLAG_OPEN;
204 cam_periph_unlock(periph);
205 cam_periph_release(periph);
206 return (0);
207}
208
209/*
210 * Actually translate the requested transfer into one the physical driver
211 * can understand. The transfer is described by a buf and will include
212 * only one physical transfer.
213 */
fef8985e
MD
214static int
215ptstrategy(struct dev_strategy_args *ap)
984263bc 216{
b13267a5 217 cdev_t dev = ap->a_head.a_dev;
fef8985e 218 struct bio *bio = ap->a_bio;
81b5c339 219 struct buf *bp = bio->bio_buf;
984263bc
MD
220 struct cam_periph *periph;
221 struct pt_softc *softc;
222 u_int unit;
984263bc 223
81b5c339 224 unit = minor(dev);
984263bc
MD
225 periph = cam_extend_get(ptperiphs, unit);
226 if (periph == NULL) {
227 bp->b_error = ENXIO;
228 goto bad;
229 }
230 softc = (struct pt_softc *)periph->softc;
231
232 /*
233 * Mask interrupts so that the pack cannot be invalidated until
234 * after we are in the queue. Otherwise, we might not properly
235 * clean up one of the buffers.
236 */
4e01b467 237 crit_enter();
984263bc
MD
238
239 /*
240 * If the device has been made invalid, error out
241 */
242 if ((softc->flags & PT_FLAG_DEVICE_INVALID)) {
4e01b467 243 crit_exit();
984263bc
MD
244 bp->b_error = ENXIO;
245 goto bad;
246 }
247
248 /*
249 * Place it in the queue of disk activities for this disk
250 */
81b5c339 251 bioq_insert_tail(&softc->bio_queue, bio);
984263bc 252
4e01b467 253 crit_exit();
984263bc
MD
254
255 /*
256 * Schedule ourselves for performing the work.
257 */
258 xpt_schedule(periph, /* XXX priority */1);
259
fef8985e 260 return(0);
984263bc
MD
261bad:
262 bp->b_flags |= B_ERROR;
263
264 /*
265 * Correctly set the buf to indicate a completed xfer
266 */
267 bp->b_resid = bp->b_bcount;
81b5c339 268 biodone(bio);
fef8985e 269 return(0);
984263bc
MD
270}
271
272static void
273ptinit(void)
274{
275 cam_status status;
276 struct cam_path *path;
277
278 /*
279 * Create our extend array for storing the devices we attach to.
280 */
281 ptperiphs = cam_extend_new();
282 if (ptperiphs == NULL) {
85f8e2ea 283 kprintf("pt: Failed to alloc extend array!\n");
984263bc
MD
284 return;
285 }
286
287 /*
288 * Install a global async callback. This callback will
289 * receive async callbacks like "new device found".
290 */
291 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
292 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
293
294 if (status == CAM_REQ_CMP) {
295 struct ccb_setasync csa;
296
297 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
298 csa.ccb_h.func_code = XPT_SASYNC_CB;
299 csa.event_enable = AC_FOUND_DEVICE;
300 csa.callback = ptasync;
301 csa.callback_arg = NULL;
302 xpt_action((union ccb *)&csa);
303 status = csa.ccb_h.status;
304 xpt_free_path(path);
305 }
306
307 if (status != CAM_REQ_CMP) {
85f8e2ea 308 kprintf("pt: Failed to attach master async callback "
984263bc
MD
309 "due to status 0x%x!\n", status);
310 }
311}
312
313static cam_status
314ptctor(struct cam_periph *periph, void *arg)
315{
316 struct pt_softc *softc;
317 struct ccb_setasync csa;
318 struct ccb_getdev *cgd;
319
320 cgd = (struct ccb_getdev *)arg;
321 if (periph == NULL) {
85f8e2ea 322 kprintf("ptregister: periph was NULL!!\n");
984263bc
MD
323 return(CAM_REQ_CMP_ERR);
324 }
325
326 if (cgd == NULL) {
85f8e2ea 327 kprintf("ptregister: no getdev CCB, can't register device\n");
984263bc
MD
328 return(CAM_REQ_CMP_ERR);
329 }
330
efda3bd0 331 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
984263bc
MD
332 LIST_INIT(&softc->pending_ccbs);
333 softc->state = PT_STATE_NORMAL;
81b5c339 334 bioq_init(&softc->bio_queue);
984263bc
MD
335
336 softc->io_timeout = SCSI_PT_DEFAULT_TIMEOUT * 1000;
337
338 periph->softc = softc;
339
340 cam_extend_set(ptperiphs, periph->unit_number, periph);
341
342 devstat_add_entry(&softc->device_stats, "pt",
343 periph->unit_number, 0,
344 DEVSTAT_NO_BLOCKSIZE,
345 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
346 DEVSTAT_PRIORITY_OTHER);
347
fef8985e
MD
348 dev_ops_add(&pt_ops, -1, periph->unit_number);
349 make_dev(&pt_ops, periph->unit_number, UID_ROOT,
e4c9c0c8
MD
350 GID_OPERATOR, 0600, "%s%d", periph->periph_name,
351 periph->unit_number);
984263bc
MD
352 /*
353 * Add async callbacks for bus reset and
354 * bus device reset calls. I don't bother
355 * checking if this fails as, in most cases,
356 * the system will function just fine without
357 * them and the only alternative would be to
358 * not attach the device on failure.
359 */
360 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
361 csa.ccb_h.func_code = XPT_SASYNC_CB;
362 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
363 csa.callback = ptasync;
364 csa.callback_arg = periph;
365 xpt_action((union ccb *)&csa);
366
367 /* Tell the user we've attached to the device */
368 xpt_announce_periph(periph, NULL);
369
370 return(CAM_REQ_CMP);
371}
372
373static void
374ptoninvalidate(struct cam_periph *periph)
375{
984263bc 376 struct pt_softc *softc;
81b5c339 377 struct bio *q_bio;
984263bc
MD
378 struct buf *q_bp;
379 struct ccb_setasync csa;
380
381 softc = (struct pt_softc *)periph->softc;
382
383 /*
384 * De-register any async callbacks.
385 */
386 xpt_setup_ccb(&csa.ccb_h, periph->path,
387 /* priority */ 5);
388 csa.ccb_h.func_code = XPT_SASYNC_CB;
389 csa.event_enable = 0;
390 csa.callback = ptasync;
391 csa.callback_arg = periph;
392 xpt_action((union ccb *)&csa);
393
394 softc->flags |= PT_FLAG_DEVICE_INVALID;
395
396 /*
4e01b467 397 * We need to be in a critical section here to keep the buffer
984263bc
MD
398 * queue from being modified while we traverse it.
399 */
4e01b467 400 crit_enter();
984263bc
MD
401
402 /*
403 * Return all queued I/O with ENXIO.
404 * XXX Handle any transactions queued to the card
405 * with XPT_ABORT_CCB.
406 */
81b5c339
MD
407 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){
408 bioq_remove(&softc->bio_queue, q_bio);
409 q_bp = q_bio->bio_buf;
984263bc
MD
410 q_bp->b_resid = q_bp->b_bcount;
411 q_bp->b_error = ENXIO;
412 q_bp->b_flags |= B_ERROR;
81b5c339 413 biodone(q_bio);
984263bc
MD
414 }
415
4e01b467 416 crit_exit();
984263bc
MD
417
418 xpt_print_path(periph->path);
85f8e2ea 419 kprintf("lost device\n");
984263bc
MD
420}
421
422static void
423ptdtor(struct cam_periph *periph)
424{
425 struct pt_softc *softc;
426
427 softc = (struct pt_softc *)periph->softc;
428
429 devstat_remove_entry(&softc->device_stats);
430
984263bc
MD
431 cam_extend_release(ptperiphs, periph->unit_number);
432 xpt_print_path(periph->path);
85f8e2ea 433 kprintf("removing device entry\n");
fef8985e 434 dev_ops_remove(&pt_ops, -1, periph->unit_number);
efda3bd0 435 kfree(softc, M_DEVBUF);
984263bc
MD
436}
437
438static void
439ptasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
440{
441 struct cam_periph *periph;
442
443 periph = (struct cam_periph *)callback_arg;
444 switch (code) {
445 case AC_FOUND_DEVICE:
446 {
447 struct ccb_getdev *cgd;
448 cam_status status;
449
450 cgd = (struct ccb_getdev *)arg;
451
452 if (SID_TYPE(&cgd->inq_data) != T_PROCESSOR)
453 break;
454
455 /*
456 * Allocate a peripheral instance for
457 * this device and start the probe
458 * process.
459 */
460 status = cam_periph_alloc(ptctor, ptoninvalidate, ptdtor,
461 ptstart, "pt", CAM_PERIPH_BIO,
462 cgd->ccb_h.path, ptasync,
463 AC_FOUND_DEVICE, cgd);
464
465 if (status != CAM_REQ_CMP
466 && status != CAM_REQ_INPROG)
85f8e2ea 467 kprintf("ptasync: Unable to attach to new device "
984263bc
MD
468 "due to status 0x%x\n", status);
469 break;
470 }
471 case AC_SENT_BDR:
472 case AC_BUS_RESET:
473 {
474 struct pt_softc *softc;
475 struct ccb_hdr *ccbh;
984263bc
MD
476
477 softc = (struct pt_softc *)periph->softc;
4e01b467 478 crit_enter();
984263bc
MD
479 /*
480 * Don't fail on the expected unit attention
481 * that will occur.
482 */
483 softc->flags |= PT_FLAG_RETRY_UA;
484 for (ccbh = LIST_FIRST(&softc->pending_ccbs);
485 ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
486 ccbh->ccb_state |= PT_CCB_RETRY_UA;
4e01b467 487 crit_exit();
984263bc
MD
488 /* FALLTHROUGH */
489 }
490 default:
491 cam_periph_async(periph, code, path, arg);
492 break;
493 }
494}
495
496static void
497ptstart(struct cam_periph *periph, union ccb *start_ccb)
498{
499 struct pt_softc *softc;
500 struct buf *bp;
81b5c339 501 struct bio *bio;
984263bc
MD
502
503 softc = (struct pt_softc *)periph->softc;
504
505 /*
506 * See if there is a buf with work for us to do..
507 */
4e01b467 508 crit_enter();
81b5c339 509 bio = bioq_first(&softc->bio_queue);
984263bc
MD
510 if (periph->immediate_priority <= periph->pinfo.priority) {
511 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
512 ("queuing for immediate ccb\n"));
513 start_ccb->ccb_h.ccb_state = PT_CCB_WAITING;
514 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
515 periph_links.sle);
516 periph->immediate_priority = CAM_PRIORITY_NONE;
4e01b467 517 crit_exit();
984263bc 518 wakeup(&periph->ccb_list);
81b5c339 519 } else if (bio == NULL) {
4e01b467 520 crit_exit();
984263bc
MD
521 xpt_release_ccb(start_ccb);
522 } else {
81b5c339
MD
523 bioq_remove(&softc->bio_queue, bio);
524 bp = bio->bio_buf;
984263bc
MD
525
526 devstat_start_transaction(&softc->device_stats);
527
528 scsi_send_receive(&start_ccb->csio,
529 /*retries*/4,
530 ptdone,
531 MSG_SIMPLE_Q_TAG,
10f3fee5 532 (bp->b_cmd == BUF_CMD_READ),
984263bc
MD
533 /*byte2*/0,
534 bp->b_bcount,
535 bp->b_data,
536 /*sense_len*/SSD_FULL_SIZE,
537 /*timeout*/softc->io_timeout);
538
539 start_ccb->ccb_h.ccb_state = PT_CCB_BUFFER_IO;
540
541 /*
542 * Block out any asyncronous callbacks
543 * while we touch the pending ccb list.
544 */
984263bc
MD
545 LIST_INSERT_HEAD(&softc->pending_ccbs, &start_ccb->ccb_h,
546 periph_links.le);
984263bc 547
81b5c339
MD
548 start_ccb->ccb_h.ccb_bio = bio;
549 bio = bioq_first(&softc->bio_queue);
4e01b467 550 crit_exit();
984263bc
MD
551
552 xpt_action(start_ccb);
553
81b5c339 554 if (bio != NULL) {
984263bc
MD
555 /* Have more work to do, so ensure we stay scheduled */
556 xpt_schedule(periph, /* XXX priority */1);
557 }
558 }
559}
560
561static void
562ptdone(struct cam_periph *periph, union ccb *done_ccb)
563{
564 struct pt_softc *softc;
565 struct ccb_scsiio *csio;
566
567 softc = (struct pt_softc *)periph->softc;
568 csio = &done_ccb->csio;
569 switch (csio->ccb_h.ccb_state) {
570 case PT_CCB_BUFFER_IO:
571 case PT_CCB_BUFFER_IO_UA:
572 {
573 struct buf *bp;
81b5c339
MD
574 struct bio *bio;
575
576 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
577 bp = bio->bio_buf;
984263bc 578
984263bc
MD
579 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
580 int error;
984263bc
MD
581 int sf;
582
583 if ((csio->ccb_h.ccb_state & PT_CCB_RETRY_UA) != 0)
584 sf = SF_RETRY_UA;
585 else
586 sf = 0;
587
b05e84c9
PA
588 error = pterror(done_ccb, CAM_RETRY_SELTO, sf);
589 if (error == ERESTART) {
984263bc
MD
590 /*
591 * A retry was scheuled, so
592 * just return.
593 */
594 return;
595 }
596 if (error != 0) {
597 struct buf *q_bp;
81b5c339 598 struct bio *q_bio;
984263bc 599
4e01b467 600 crit_enter();
984263bc
MD
601
602 if (error == ENXIO) {
603 /*
604 * Catastrophic error. Mark our device
605 * as invalid.
606 */
607 xpt_print_path(periph->path);
85f8e2ea 608 kprintf("Invalidating device\n");
984263bc
MD
609 softc->flags |= PT_FLAG_DEVICE_INVALID;
610 }
611
612 /*
613 * return all queued I/O with EIO, so that
614 * the client can retry these I/Os in the
615 * proper order should it attempt to recover.
616 */
81b5c339 617 while ((q_bio = bioq_first(&softc->bio_queue))
984263bc 618 != NULL) {
81b5c339
MD
619 bioq_remove(&softc->bio_queue, q_bio);
620 q_bp = q_bio->bio_buf;
984263bc
MD
621 q_bp->b_resid = q_bp->b_bcount;
622 q_bp->b_error = EIO;
623 q_bp->b_flags |= B_ERROR;
81b5c339 624 biodone(q_bio);
984263bc 625 }
4e01b467 626 crit_exit();
984263bc
MD
627 bp->b_error = error;
628 bp->b_resid = bp->b_bcount;
629 bp->b_flags |= B_ERROR;
630 } else {
631 bp->b_resid = csio->resid;
632 bp->b_error = 0;
633 if (bp->b_resid != 0) {
634 /* Short transfer ??? */
635 bp->b_flags |= B_ERROR;
636 }
637 }
638 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
639 cam_release_devq(done_ccb->ccb_h.path,
640 /*relsim_flags*/0,
641 /*reduction*/0,
642 /*timeout*/0,
643 /*getcount_only*/0);
644 } else {
645 bp->b_resid = csio->resid;
646 if (bp->b_resid != 0)
647 bp->b_flags |= B_ERROR;
648 }
649
650 /*
651 * Block out any asyncronous callbacks
652 * while we touch the pending ccb list.
653 */
4e01b467 654 crit_enter();
984263bc 655 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
4e01b467 656 crit_exit();
984263bc
MD
657
658 devstat_end_transaction_buf(&softc->device_stats, bp);
81b5c339 659 biodone(bio);
984263bc
MD
660 break;
661 }
662 case PT_CCB_WAITING:
663 /* Caller will release the CCB */
664 wakeup(&done_ccb->ccb_h.cbfcnp);
665 return;
666 }
667 xpt_release_ccb(done_ccb);
668}
669
670static int
671pterror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
672{
673 struct pt_softc *softc;
674 struct cam_periph *periph;
675
676 periph = xpt_path_periph(ccb->ccb_h.path);
677 softc = (struct pt_softc *)periph->softc;
678
679 return(cam_periph_error(ccb, cam_flags, sense_flags,
680 &softc->saved_ccb));
681}
682
683static int
fef8985e 684ptioctl(struct dev_ioctl_args *ap)
984263bc 685{
b13267a5 686 cdev_t dev = ap->a_head.a_dev;
fef8985e 687 caddr_t addr = ap->a_data;
984263bc
MD
688 struct cam_periph *periph;
689 struct pt_softc *softc;
690 int unit;
691 int error;
692
693 unit = minor(dev);
694 periph = cam_extend_get(ptperiphs, unit);
695
696 if (periph == NULL)
697 return(ENXIO);
698
699 softc = (struct pt_softc *)periph->softc;
700
377d4740 701 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
984263bc
MD
702 return (error); /* error code from tsleep */
703 }
704
fef8985e 705 switch(ap->a_cmd) {
984263bc
MD
706 case PTIOCGETTIMEOUT:
707 if (softc->io_timeout >= 1000)
708 *(int *)addr = softc->io_timeout / 1000;
709 else
710 *(int *)addr = 0;
711 break;
712 case PTIOCSETTIMEOUT:
713 {
984263bc
MD
714 if (*(int *)addr < 1) {
715 error = EINVAL;
716 break;
717 }
718
4e01b467 719 crit_enter();
984263bc 720 softc->io_timeout = *(int *)addr * 1000;
4e01b467 721 crit_exit();
984263bc
MD
722
723 break;
724 }
725 default:
fef8985e 726 error = cam_periph_ioctl(periph, ap->a_cmd, addr, pterror);
984263bc
MD
727 break;
728 }
729
730 cam_periph_unlock(periph);
731
732 return(error);
733}
734
735void
736scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
737 void (*cbfcnp)(struct cam_periph *, union ccb *),
738 u_int tag_action, int readop, u_int byte2,
739 u_int32_t xfer_len, u_int8_t *data_ptr, u_int8_t sense_len,
740 u_int32_t timeout)
741{
742 struct scsi_send_receive *scsi_cmd;
743
744 scsi_cmd = (struct scsi_send_receive *)&csio->cdb_io.cdb_bytes;
745 scsi_cmd->opcode = readop ? RECEIVE : SEND;
746 scsi_cmd->byte2 = byte2;
747 scsi_ulto3b(xfer_len, scsi_cmd->xfer_len);
748 scsi_cmd->control = 0;
749
750 cam_fill_csio(csio,
751 retries,
752 cbfcnp,
753 /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT,
754 tag_action,
755 data_ptr,
756 xfer_len,
757 sense_len,
758 sizeof(*scsi_cmd),
759 timeout);
760}