Remove wrong getvfsbytype.3 MLINK. The manpage doesn't document it.
[dragonfly.git] / sys / bus / cam / scsi / scsi_da.c
CommitLineData
984263bc
MD
1/*
2 * Implementation of SCSI Direct Access Peripheral driver for CAM.
3 *
4 * Copyright (c) 1997 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 *
62ade751 28 * $FreeBSD: src/sys/cam/scsi/scsi_da.c,v 1.42.2.46 2003/10/21 22:18:19 thomas Exp $
984263bc
MD
29 */
30
984263bc
MD
31#include <sys/param.h>
32
33#ifdef _KERNEL
684a93c4 34
984263bc
MD
35#include <sys/systm.h>
36#include <sys/kernel.h>
37#include <sys/buf.h>
38#include <sys/sysctl.h>
62ade751 39#include <sys/taskqueue.h>
1c8b7a9a 40#include <sys/lock.h>
984263bc 41#include <sys/conf.h>
1c8b7a9a 42#include <sys/devicestat.h>
984263bc 43#include <sys/disk.h>
55a78310 44#include <sys/dtype.h>
984263bc
MD
45#include <sys/eventhandler.h>
46#include <sys/malloc.h>
47#include <sys/cons.h>
3020e3be 48#include <sys/proc.h>
684a93c4 49
3020e3be 50#include <sys/buf2.h>
684a93c4 51
1c8b7a9a 52#endif /* _KERNEL */
984263bc 53
05220613 54#ifdef _KERNEL
984263bc 55#include <vm/pmap.h>
05220613 56#endif
984263bc
MD
57
58#ifndef _KERNEL
59#include <stdio.h>
60#include <string.h>
61#endif /* _KERNEL */
62
55230951 63#include <sys/camlib.h>
1f2de5d4
MD
64#include "../cam.h"
65#include "../cam_ccb.h"
66#include "../cam_extend.h"
67#include "../cam_periph.h"
68#include "../cam_xpt_periph.h"
1c8b7a9a 69#include "../cam_sim.h"
984263bc 70
32506cfa 71#include "scsi_daio.h"
1f2de5d4 72#include "scsi_message.h"
984263bc
MD
73
74#ifndef _KERNEL
1f2de5d4 75#include "scsi_da.h"
984263bc
MD
76#endif /* !_KERNEL */
77
78#ifdef _KERNEL
79typedef enum {
80 DA_STATE_PROBE,
bdd58e03 81 DA_STATE_PROBE2,
984263bc
MD
82 DA_STATE_NORMAL
83} da_state;
84
85typedef enum {
86 DA_FLAG_PACK_INVALID = 0x001,
87 DA_FLAG_NEW_PACK = 0x002,
88 DA_FLAG_PACK_LOCKED = 0x004,
89 DA_FLAG_PACK_REMOVABLE = 0x008,
90 DA_FLAG_TAGGED_QUEUING = 0x010,
984263bc 91 DA_FLAG_RETRY_UA = 0x080,
62ade751 92 DA_FLAG_OPEN = 0x100,
af0aa0ac
MD
93 DA_FLAG_SCTX_INIT = 0x200,
94 DA_FLAG_RD_LIMIT = 0x400,
e0fb398b 95 DA_FLAG_WR_LIMIT = 0x800,
aec00d32
MD
96 DA_FLAG_CAN_TRIM = 0x1000,
97 DA_FLAG_CAP_MUTE = 0x2000
984263bc
MD
98} da_flags;
99
100typedef enum {
101 DA_Q_NONE = 0x00,
102 DA_Q_NO_SYNC_CACHE = 0x01,
62ade751
MD
103 DA_Q_NO_6_BYTE = 0x02,
104 DA_Q_NO_PREVENT = 0x04
984263bc
MD
105} da_quirks;
106
107typedef enum {
a9f09b75 108 DA_CCB_POLLED = 0x00,
984263bc 109 DA_CCB_PROBE = 0x01,
bdd58e03
MD
110 DA_CCB_PROBE2 = 0x02,
111 DA_CCB_BUFFER_IO = 0x03,
112 DA_CCB_WAITING = 0x04,
113 DA_CCB_DUMP = 0x05,
e0fb398b 114 DA_CCB_TRIM = 0x06,
984263bc
MD
115 DA_CCB_TYPE_MASK = 0x0F,
116 DA_CCB_RETRY_UA = 0x10
117} da_ccb_state;
118
119/* Offsets into our private area for storing information */
120#define ccb_state ppriv_field0
81b5c339 121#define ccb_bio ppriv_ptr1
984263bc
MD
122
123struct disk_params {
124 u_int8_t heads;
bdd58e03 125 u_int32_t cylinders;
984263bc
MD
126 u_int8_t secs_per_track;
127 u_int32_t secsize; /* Number of bytes/sector */
bdd58e03 128 u_int64_t sectors; /* total number sectors */
984263bc
MD
129};
130
e0fb398b
T
131#define TRIM_MAX_BLOCKS 8
132#define TRIM_MAX_RANGES TRIM_MAX_BLOCKS * 64
133struct trim_request {
134 uint8_t data[TRIM_MAX_RANGES * 8];
135 struct bio *bios[TRIM_MAX_RANGES];
136};
137
984263bc 138struct da_softc {
af0aa0ac
MD
139 struct bio_queue_head bio_queue_rd;
140 struct bio_queue_head bio_queue_wr;
e0fb398b 141 struct bio_queue_head bio_queue_trim;
984263bc
MD
142 struct devstat device_stats;
143 SLIST_ENTRY(da_softc) links;
144 LIST_HEAD(, ccb_hdr) pending_ccbs;
145 da_state state;
146 da_flags flags;
147 da_quirks quirks;
148 int minimum_cmd_size;
128500e6
MD
149 int outstanding_cmds_rd; /* outstanding read requests */
150 int outstanding_cmds_wr; /* outstanding write requests */
151 int tps_ticks;
152 long tps_rd; /* read bandwidth exponential/tick */
153 long tps_wr; /* write bandwidth exponential/tick */
e0fb398b
T
154 int trim_max_ranges;
155 int trim_running;
156 int trim_enabled;
984263bc
MD
157 struct disk_params params;
158 struct disk disk;
159 union ccb saved_ccb;
62ade751
MD
160 struct task sysctl_task;
161 struct sysctl_ctx_list sysctl_ctx;
162 struct sysctl_oid *sysctl_tree;
e0fb398b 163 struct trim_request trim_req;
984263bc
MD
164};
165
166struct da_quirk_entry {
167 struct scsi_inquiry_pattern inq_pat;
168 da_quirks quirks;
169};
170
171static const char quantum[] = "QUANTUM";
172static const char microp[] = "MICROP";
173
174static struct da_quirk_entry da_quirk_table[] =
175{
62ade751 176 /* SPI, FC devices */
984263bc
MD
177 {
178 /*
179 * Fujitsu M2513A MO drives.
180 * Tested devices: M2513A2 firmware versions 1200 & 1300.
181 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
182 * Reported by: W.Scholten <whs@xs4all.nl>
183 */
184 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
185 /*quirks*/ DA_Q_NO_SYNC_CACHE
186 },
187 {
188 /* See above. */
189 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
190 /*quirks*/ DA_Q_NO_SYNC_CACHE
191 },
192 {
193 /*
194 * This particular Fujitsu drive doesn't like the
195 * synchronize cache command.
196 * Reported by: Tom Jackson <toj@gorilla.net>
197 */
198 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
199 /*quirks*/ DA_Q_NO_SYNC_CACHE
984263bc
MD
200 },
201 {
202 /*
203 * This drive doesn't like the synchronize cache command
204 * either. Reported by: Matthew Jacob <mjacob@feral.com>
205 * in NetBSD PR kern/6027, August 24, 1998.
206 */
207 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
208 /*quirks*/ DA_Q_NO_SYNC_CACHE
209 },
210 {
211 /*
212 * This drive doesn't like the synchronize cache command
213 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
214 * (PR 8882).
215 */
216 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
217 /*quirks*/ DA_Q_NO_SYNC_CACHE
218 },
219 {
220 /*
221 * Doesn't like the synchronize cache command.
222 * Reported by: Blaz Zupan <blaz@gold.amis.net>
223 */
224 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
225 /*quirks*/ DA_Q_NO_SYNC_CACHE
226 },
227 {
228 /*
229 * Doesn't like the synchronize cache command.
d92d7552 230 * Reported by: Blaz Zupan <blaz@gold.amis.net>
984263bc
MD
231 */
232 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
233 /*quirks*/ DA_Q_NO_SYNC_CACHE
234 },
235 {
236 /*
237 * Doesn't like the synchronize cache command.
238 */
239 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
240 /*quirks*/ DA_Q_NO_SYNC_CACHE
241 },
d92d7552
PA
242 {
243 /*
244 * Doesn't like the synchronize cache command.
245 * Reported by: walter@pelissero.de
246 */
247 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
248 /*quirks*/ DA_Q_NO_SYNC_CACHE
249 },
984263bc
MD
250 {
251 /*
252 * Doesn't work correctly with 6 byte reads/writes.
253 * Returns illegal request, and points to byte 9 of the
254 * 6-byte CDB.
255 * Reported by: Adam McDougall <bsdx@spawnet.com>
256 */
257 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
258 /*quirks*/ DA_Q_NO_6_BYTE
259 },
260 {
62ade751 261 /* See above. */
984263bc
MD
262 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
263 /*quirks*/ DA_Q_NO_6_BYTE
264 },
984263bc
MD
265 {
266 /*
d92d7552
PA
267 * Doesn't like the synchronize cache command.
268 * Reported by: walter@pelissero.de
984263bc 269 */
d92d7552 270 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
62ade751 271 /*quirks*/ DA_Q_NO_SYNC_CACHE
984263bc
MD
272 },
273 {
d92d7552
PA
274 /*
275 * The CISS RAID controllers do not support SYNC_CACHE
276 */
277 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
62ade751
MD
278 /*quirks*/ DA_Q_NO_SYNC_CACHE
279 },
a4a9ba75
SW
280 {
281 /*
282 * The same goes for the mly(4) controllers
283 */
284 {T_DIRECT, SIP_MEDIA_FIXED, "MLY*", "*", "MYLX"},
285 /*quirks*/ DA_Q_NO_SYNC_CACHE
286 },
a9453758
MD
287 /*
288 * USB mass storage devices supported by umass(4)
289 *
290 * NOTE: USB attachments automatically set DA_Q_NO_SYNC_CACHE so
291 * it does not have to be specified here.
292 */
d92d7552
PA
293 {
294 /*
295 * Creative Nomad MUVO mp3 player (USB)
296 * PR: kern/53094
297 */
298 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
a9453758 299 /*quirks*/ DA_Q_NO_PREVENT
d92d7552 300 },
984263bc
MD
301 {
302 /*
d92d7552
PA
303 * Sigmatel USB Flash MP3 Player
304 * PR: kern/57046
984263bc 305 */
d92d7552 306 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
a9453758 307 /*quirks*/ DA_Q_NO_PREVENT
984263bc
MD
308 },
309 {
310 /*
d92d7552
PA
311 * SEAGRAND NP-900 MP3 Player
312 * PR: kern/64563
984263bc 313 */
d92d7552 314 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
a9453758 315 /*quirks*/ DA_Q_NO_PREVENT
984263bc
MD
316 },
317 {
318 /*
d92d7552
PA
319 * Creative MUVO Slim mp3 player (USB)
320 * PR: usb/86131
984263bc 321 */
d92d7552 322 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
a9453758 323 "*"}, /*quirks*/ DA_Q_NO_PREVENT
d92d7552
PA
324 },
325 {
326 /*
327 * Philips USB Key Audio KEY013
328 * PR: usb/68412
329 */
330 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
a9453758 331 /*quirks*/ DA_Q_NO_PREVENT
285d490c 332 },
984263bc
MD
333};
334
335static d_open_t daopen;
336static d_close_t daclose;
337static d_strategy_t dastrategy;
984263bc 338static d_dump_t dadump;
e0fb398b 339static d_ioctl_t daioctl;
984263bc
MD
340static periph_init_t dainit;
341static void daasync(void *callback_arg, u_int32_t code,
342 struct cam_path *path, void *arg);
62ade751 343static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
984263bc
MD
344static periph_ctor_t daregister;
345static periph_dtor_t dacleanup;
346static periph_start_t dastart;
347static periph_oninv_t daoninvalidate;
348static void dadone(struct cam_periph *periph,
349 union ccb *done_ccb);
350static int daerror(union ccb *ccb, u_int32_t cam_flags,
351 u_int32_t sense_flags);
352static void daprevent(struct cam_periph *periph, int action);
aec00d32 353static int dagetcapacity(struct cam_periph *periph, int ccbflags);
f7b26992 354static int dacheckmedia(struct cam_periph *periph);
bdd58e03
MD
355static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
356 uint64_t maxsector);
af0aa0ac 357static void daflushbioq(struct bio_queue_head *bioq, int error);
984263bc
MD
358static void dashutdown(void *arg, int howto);
359
360#ifndef DA_DEFAULT_TIMEOUT
361#define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
362#endif
363
364#ifndef DA_DEFAULT_RETRY
365#define DA_DEFAULT_RETRY 4
366#endif
367
128500e6
MD
368__read_mostly int da_retry_count = DA_DEFAULT_RETRY;
369__read_mostly int da_default_timeout = DA_DEFAULT_TIMEOUT;
370__read_mostly static int da_balance_enable = 1;
371__read_mostly static int da_balance_ratio = 100; /* read-to-write */
372__read_mostly static int da_balance_debug = 0;
984263bc 373
984263bc
MD
374SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
375 "CAM Direct Access Disk driver");
376SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
377 &da_retry_count, 0, "Normal I/O retry count");
62ade751 378TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
984263bc
MD
379SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
380 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
62ade751 381TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
984263bc 382
128500e6
MD
383SYSCTL_INT(_kern_cam_da, OID_AUTO, balance_enable, CTLFLAG_RW,
384 &da_balance_enable, 0, "Enable tps balancing");
385SYSCTL_INT(_kern_cam_da, OID_AUTO, balance_ratio, CTLFLAG_RW,
386 &da_balance_ratio, 0, "Set read-to-write ratio 100=1:1");
387SYSCTL_INT(_kern_cam_da, OID_AUTO, balance_debug, CTLFLAG_RW,
388 &da_balance_debug, 0, "Enable tps balance debugging");
389
984263bc
MD
390static struct periph_driver dadriver =
391{
392 dainit, "da",
393 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
394};
395
2ad14cb5 396PERIPHDRIVER_DECLARE(da, dadriver);
984263bc 397
fef8985e 398static struct dev_ops da_ops = {
b8e1d863 399 { "da", 0, D_DISK | D_MPSAFE },
fef8985e
MD
400 .d_open = daopen,
401 .d_close = daclose,
402 .d_read = physread,
403 .d_write = physwrite,
fef8985e 404 .d_strategy = dastrategy,
e0fb398b
T
405 .d_dump = dadump,
406 .d_ioctl = daioctl
984263bc
MD
407};
408
984263bc
MD
409static struct extend_array *daperiphs;
410
1c8b7a9a
PA
411MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
412
e0fb398b
T
413static int
414daioctl(struct dev_ioctl_args *ap)
415{
416 int unit;
417 int error = 0;
418 struct buf *bp;
419 struct cam_periph *periph;
420 int byte_count;
e0fb398b
T
421
422 off_t *del_num = (off_t*)ap->a_data;
423 off_t bytes_left;
424 off_t bytes_start;
425
426 cdev_t dev = ap->a_head.a_dev;
427
428
429 unit = dkunit(dev);
430 periph = cam_extend_get(daperiphs, unit);
431 if (periph == NULL)
432 return(ENXIO);
e0fb398b
T
433
434 switch (ap->a_cmd) {
32506cfa 435 case DAIOCTRIM:
e0fb398b
T
436 {
437
438 bytes_left = del_num[1];
439 bytes_start = del_num[0];
440
441 /* TRIM occurs on 512-byte sectors. */
442 KKASSERT((bytes_left % 512) == 0);
443 KKASSERT((bytes_start% 512) == 0);
444
445
446 /* Break TRIM up into int-sized commands because of b_bcount */
447 while(bytes_left) {
448
449 /*
450 * Rather than than squezing out more blocks in b_bcount
451 * and having to break up the TRIM request in da_start(),
452 * we ensure we can always TRIM this many bytes with one
453 * TRIM command (this happens if the device only
454 * supports one TRIM block).
455 *
456 * With min TRIM blksize of 1, TRIM command free
457 * 4194240 blks(64*65535): each LBA range can address
458 * 65535 blks and there 64 such ranges in a 512-byte
459 * block. And, 4194240 * 512 = 0x7FFF8000
460 *
461 */
462 byte_count = MIN(bytes_left,0x7FFF8000);
d2812084 463 bp = getnewbuf(0, 0, 0, 1);
e0fb398b
T
464
465 bp->b_cmd = BUF_CMD_FREEBLKS;
466 bp->b_bio1.bio_offset = bytes_start;
467 bp->b_bcount = byte_count;
468 bp->b_bio1.bio_flags |= BIO_SYNC;
469 bp->b_bio1.bio_done = biodone_sync;
470
471 dev_dstrategy(ap->a_head.a_dev, &bp->b_bio1);
472
473 if (biowait(&bp->b_bio1, "TRIM")) {
474 kprintf("Error:%d\n", bp->b_error);
53005b09 475 brelse(bp);
e0fb398b
T
476 return(bp->b_error ? bp->b_error : EIO);
477 }
478 brelse(bp);
479 bytes_left -= byte_count;
480 bytes_start += byte_count;
481 }
482 break;
483 }
484 default:
485 return(EINVAL);
486 }
487
488 return(error);
489}
490
984263bc 491static int
fef8985e 492daopen(struct dev_open_args *ap)
984263bc 493{
b13267a5 494 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
495 struct cam_periph *periph;
496 struct da_softc *softc;
a688b15c 497 struct disk_info info;
984263bc 498 int unit;
984263bc 499 int error;
984263bc
MD
500
501 unit = dkunit(dev);
984263bc 502 periph = cam_extend_get(daperiphs, unit);
b05e84c9 503 if (periph == NULL) {
984263bc 504 return (ENXIO);
b05e84c9 505 }
984263bc 506
1c8b7a9a
PA
507 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
508 return(ENXIO);
509 }
510
511 cam_periph_lock(periph);
512 if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
513 cam_periph_unlock(periph);
514 cam_periph_release(periph);
515 return (error);
516 }
517
518 unit = periph->unit_number;
984263bc
MD
519 softc = (struct da_softc *)periph->softc;
520
521 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
9ece9268
PA
522 ("daopen: dev=%s (unit %d)\n", devtoname(dev),
523 unit));
984263bc 524
984263bc
MD
525 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
526 /* Invalidate our pack information. */
527 disk_invalidate(&softc->disk);
528 softc->flags &= ~DA_FLAG_PACK_INVALID;
529 }
984263bc 530
f7b26992
MD
531 error = dacheckmedia(periph);
532 softc->flags |= DA_FLAG_OPEN;
984263bc
MD
533
534 if (error == 0) {
cec957e9 535 struct ccb_getdev *cgd;
984263bc 536
a688b15c
MD
537 /* Build disk information structure */
538 bzero(&info, sizeof(info));
539 info.d_type = DTYPE_SCSI;
984263bc
MD
540
541 /*
542 * Grab the inquiry data to get the vendor and product names.
543 * Put them in the typename and packname for the label.
544 */
cec957e9
MD
545 cgd = &xpt_alloc_ccb()->cgd;
546 xpt_setup_ccb(&cgd->ccb_h, periph->path, /*priority*/ 1);
547 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
548 xpt_action((union ccb *)cgd);
549 xpt_free_ccb(&cgd->ccb_h);
984263bc 550
984263bc
MD
551 /*
552 * Check to see whether or not the blocksize is set yet.
553 * If it isn't, set it and then clear the blocksize
554 * unavailable flag for the device statistics.
555 */
556 if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){
557 softc->device_stats.block_size = softc->params.secsize;
558 softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE;
559 }
560 }
561
b05e84c9 562 if (error == 0) {
aec00d32 563 softc->flags &= ~DA_FLAG_CAP_MUTE;
62ade751
MD
564 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
565 (softc->quirks & DA_Q_NO_PREVENT) == 0)
b05e84c9
PA
566 daprevent(periph, PR_PREVENT);
567 } else {
aec00d32 568 softc->flags |= DA_FLAG_CAP_MUTE;
984263bc
MD
569 softc->flags &= ~DA_FLAG_OPEN;
570 cam_periph_release(periph);
571 }
2d19cdd3 572 cam_periph_unhold(periph, 1);
984263bc
MD
573 return (error);
574}
575
576static int
fef8985e 577daclose(struct dev_close_args *ap)
984263bc 578{
b13267a5 579 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
580 struct cam_periph *periph;
581 struct da_softc *softc;
582 int unit;
583 int error;
584
585 unit = dkunit(dev);
586 periph = cam_extend_get(daperiphs, unit);
587 if (periph == NULL)
588 return (ENXIO);
589
1c8b7a9a
PA
590 cam_periph_lock(periph);
591 if ((error = cam_periph_hold(periph, 0)) != 0) {
592 cam_periph_unlock(periph);
593 cam_periph_release(periph);
594 return (error);
984263bc
MD
595 }
596
1c8b7a9a
PA
597 softc = (struct da_softc *)periph->softc;
598
984263bc
MD
599 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
600 union ccb *ccb;
601
602 ccb = cam_periph_getccb(periph, /*priority*/1);
a9f09b75 603 ccb->ccb_h.ccb_state = DA_CCB_POLLED;
984263bc
MD
604
605 scsi_synchronize_cache(&ccb->csio,
606 /*retries*/1,
607 /*cbfcnp*/dadone,
608 MSG_SIMPLE_Q_TAG,
609 /*begin_lba*/0,/* Cover the whole disk */
610 /*lb_count*/0,
611 SSD_FULL_SIZE,
612 5 * 60 * 1000);
613
614 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
615 /*sense_flags*/SF_RETRY_UA,
616 &softc->device_stats);
617
618 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
619 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
620 CAM_SCSI_STATUS_ERROR) {
621 int asc, ascq;
622 int sense_key, error_code;
623
624 scsi_extract_sense(&ccb->csio.sense_data,
625 &error_code,
626 &sense_key,
627 &asc, &ascq);
628 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
629 scsi_sense_print(&ccb->csio);
630 } else {
1c8b7a9a
PA
631 xpt_print(periph->path, "Synchronize cache "
632 "failed, status == 0x%x, scsi status == "
633 "0x%x\n", ccb->csio.ccb_h.status,
634 ccb->csio.scsi_status);
984263bc
MD
635 }
636 }
637
638 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
639 cam_release_devq(ccb->ccb_h.path,
640 /*relsim_flags*/0,
641 /*reduction*/0,
642 /*timeout*/0,
643 /*getcount_only*/0);
644
645 xpt_release_ccb(ccb);
646
647 }
648
649 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
62ade751
MD
650 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
651 daprevent(periph, PR_ALLOW);
984263bc
MD
652 /*
653 * If we've got removeable media, mark the blocksize as
654 * unavailable, since it could change when new media is
655 * inserted.
656 */
657 softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;
658 }
659
fca0fce6
MD
660 /*
661 * Don't compound any ref counting software bugs with more.
662 */
663 if (softc->flags & DA_FLAG_OPEN) {
664 softc->flags &= ~DA_FLAG_OPEN;
665 cam_periph_release(periph);
666 } else {
1c8b7a9a
PA
667 xpt_print(periph->path,
668 "daclose() called on an already closed device!\n");
fca0fce6 669 }
2d19cdd3 670 cam_periph_unhold(periph, 1);
984263bc
MD
671 return (0);
672}
673
674/*
675 * Actually translate the requested transfer into one the physical driver
676 * can understand. The transfer is described by a buf and will include
677 * only one physical transfer.
678 */
fef8985e
MD
679static int
680dastrategy(struct dev_strategy_args *ap)
984263bc 681{
b13267a5 682 cdev_t dev = ap->a_head.a_dev;
fef8985e 683 struct bio *bio = ap->a_bio;
81b5c339 684 struct buf *bp = bio->bio_buf;
984263bc
MD
685 struct cam_periph *periph;
686 struct da_softc *softc;
687 u_int unit;
984263bc 688
81b5c339 689 unit = dkunit(dev);
984263bc
MD
690 periph = cam_extend_get(daperiphs, unit);
691 if (periph == NULL) {
692 bp->b_error = ENXIO;
693 goto bad;
694 }
695 softc = (struct da_softc *)periph->softc;
1c8b7a9a
PA
696
697 cam_periph_lock(periph);
698
984263bc
MD
699#if 0
700 /*
701 * check it's not too big a transfer for our adapter
702 */
81b5c339 703 scsi_minphys(bp, &sd_switch);
984263bc
MD
704#endif
705
706 /*
707 * Mask interrupts so that the pack cannot be invalidated until
708 * after we are in the queue. Otherwise, we might not properly
709 * clean up one of the buffers.
710 */
984263bc
MD
711
712 /*
713 * If the device has been made invalid, error out
714 */
715 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1c8b7a9a 716 cam_periph_unlock(periph);
984263bc
MD
717 bp->b_error = ENXIO;
718 goto bad;
719 }
720
721 /*
722 * Place it in the queue of disk activities for this disk
723 */
af0aa0ac 724 if (bp->b_cmd == BUF_CMD_WRITE || bp->b_cmd == BUF_CMD_FLUSH)
c6cad506 725 bioqdisksort(&softc->bio_queue_wr, bio);
e0fb398b 726 else if (bp->b_cmd == BUF_CMD_FREEBLKS)
c6cad506 727 bioqdisksort(&softc->bio_queue_trim, bio);
af0aa0ac 728 else
c6cad506 729 bioqdisksort(&softc->bio_queue_rd, bio);
984263bc
MD
730
731 /*
732 * Schedule ourselves for performing the work.
733 */
734 xpt_schedule(periph, /* XXX priority */1);
1c8b7a9a 735 cam_periph_unlock(periph);
984263bc 736
fef8985e 737 return(0);
984263bc
MD
738bad:
739 bp->b_flags |= B_ERROR;
740
741 /*
742 * Correctly set the buf to indicate a completed xfer
743 */
744 bp->b_resid = bp->b_bcount;
81b5c339 745 biodone(bio);
fef8985e 746 return(0);
984263bc
MD
747}
748
984263bc 749static int
fef8985e 750dadump(struct dev_dump_args *ap)
984263bc 751{
b13267a5 752 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
753 struct cam_periph *periph;
754 struct da_softc *softc;
755 u_int unit;
b24cd69c 756 u_int32_t secsize;
cec957e9 757 struct ccb_scsiio *csio;
984263bc
MD
758
759 unit = dkunit(dev);
984263bc 760 periph = cam_extend_get(daperiphs, unit);
b24cd69c 761 if (periph == NULL)
984263bc 762 return (ENXIO);
b24cd69c 763
984263bc 764 softc = (struct da_softc *)periph->softc;
1c8b7a9a 765 cam_periph_lock(periph);
b24cd69c
AH
766 secsize = softc->params.secsize; /* XXX: or ap->a_secsize? */
767
1c8b7a9a
PA
768 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
769 cam_periph_unlock(periph);
984263bc 770 return (ENXIO);
1c8b7a9a 771 }
984263bc 772
cec957e9
MD
773 csio = &xpt_alloc_ccb()->csio;
774
b24cd69c
AH
775 /*
776 * because length == 0 means we are supposed to flush cache, we only
777 * try to write something if length > 0.
778 */
779 if (ap->a_length > 0) {
cec957e9
MD
780 xpt_setup_ccb(&csio->ccb_h, periph->path, /*priority*/1);
781 csio->ccb_h.flags |= CAM_POLLED;
782 csio->ccb_h.ccb_state = DA_CCB_DUMP;
783 scsi_read_write(csio,
984263bc
MD
784 /*retries*/1,
785 dadone,
786 MSG_ORDERED_Q_TAG,
787 /*read*/FALSE,
788 /*byte2*/0,
789 /*minimum_cmd_size*/ softc->minimum_cmd_size,
b24cd69c
AH
790 ap->a_offset / secsize,
791 ap->a_length / secsize,
792 /*data_ptr*/(u_int8_t *) ap->a_virtual,
793 /*dxfer_len*/ap->a_length,
984263bc
MD
794 /*sense_len*/SSD_FULL_SIZE,
795 DA_DEFAULT_TIMEOUT * 1000);
cec957e9 796 xpt_polled_action((union ccb *)csio);
984263bc 797
cec957e9 798 if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
85f8e2ea 799 kprintf("Aborting dump due to I/O error.\n");
cec957e9 800 if ((csio->ccb_h.status & CAM_STATUS_MASK) ==
984263bc 801 CAM_SCSI_STATUS_ERROR)
cec957e9 802 scsi_sense_print(csio);
984263bc 803 else
85f8e2ea 804 kprintf("status == 0x%x, scsi status == 0x%x\n",
cec957e9
MD
805 csio->ccb_h.status, csio->scsi_status);
806 cam_periph_unlock(periph);
807 xpt_free_ccb(&csio->ccb_h);
984263bc
MD
808 return(EIO);
809 }
cec957e9 810 goto done;
984263bc
MD
811 }
812
813 /*
814 * Sync the disk cache contents to the physical media.
815 */
816 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
817
cec957e9
MD
818 xpt_setup_ccb(&csio->ccb_h, periph->path, /*priority*/1);
819 csio->ccb_h.ccb_state = DA_CCB_DUMP;
820 scsi_synchronize_cache(csio,
984263bc
MD
821 /*retries*/1,
822 /*cbfcnp*/dadone,
823 MSG_SIMPLE_Q_TAG,
824 /*begin_lba*/0,/* Cover the whole disk */
825 /*lb_count*/0,
826 SSD_FULL_SIZE,
827 5 * 60 * 1000);
cec957e9 828 xpt_polled_action((union ccb *)csio);
984263bc 829
cec957e9
MD
830 if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
831 if ((csio->ccb_h.status & CAM_STATUS_MASK) ==
984263bc
MD
832 CAM_SCSI_STATUS_ERROR) {
833 int asc, ascq;
834 int sense_key, error_code;
835
cec957e9 836 scsi_extract_sense(&csio->sense_data,
984263bc
MD
837 &error_code,
838 &sense_key,
839 &asc, &ascq);
840 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
cec957e9 841 scsi_sense_print(csio);
984263bc 842 } else {
1c8b7a9a
PA
843 xpt_print(periph->path, "Synchronize cache "
844 "failed, status == 0x%x, scsi status == "
cec957e9
MD
845 "0x%x\n",
846 csio->ccb_h.status, csio->scsi_status);
984263bc
MD
847 }
848 }
849 }
cec957e9 850done:
1c8b7a9a 851 cam_periph_unlock(periph);
cec957e9
MD
852 xpt_free_ccb(&csio->ccb_h);
853
984263bc
MD
854 return (0);
855}
856
857static void
858dainit(void)
859{
860 cam_status status;
984263bc
MD
861
862 /*
863 * Create our extend array for storing the devices we attach to.
864 */
865 daperiphs = cam_extend_new();
984263bc 866 if (daperiphs == NULL) {
85f8e2ea 867 kprintf("da: Failed to alloc extend array!\n");
984263bc
MD
868 return;
869 }
3690a379 870
984263bc
MD
871 /*
872 * Install a global async callback. This callback will
873 * receive async callbacks like "new device found".
874 */
1c8b7a9a 875 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
984263bc
MD
876
877 if (status != CAM_REQ_CMP) {
85f8e2ea 878 kprintf("da: Failed to attach master async callback "
984263bc 879 "due to status 0x%x!\n", status);
0c4341b9 880 } else {
984263bc
MD
881 /* Register our shutdown event handler */
882 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
c022ffc9 883 NULL, SHUTDOWN_PRI_SECOND)) == NULL)
5ee727b6
SW
884 kprintf("%s: shutdown event registration failed!\n",
885 __func__);
984263bc
MD
886 }
887}
888
889static void
890daoninvalidate(struct cam_periph *periph)
891{
984263bc 892 struct da_softc *softc;
984263bc
MD
893
894 softc = (struct da_softc *)periph->softc;
895
896 /*
897 * De-register any async callbacks.
898 */
1c8b7a9a 899 xpt_register_async(0, daasync, periph, periph->path);
984263bc
MD
900
901 softc->flags |= DA_FLAG_PACK_INVALID;
902
984263bc
MD
903 /*
904 * Return all queued I/O with ENXIO.
905 * XXX Handle any transactions queued to the card
906 * with XPT_ABORT_CCB.
907 */
e0fb398b 908 daflushbioq(&softc->bio_queue_trim, ENXIO);
af0aa0ac
MD
909 daflushbioq(&softc->bio_queue_wr, ENXIO);
910 daflushbioq(&softc->bio_queue_rd, ENXIO);
911 xpt_print(periph->path, "lost device\n");
912}
913
914static void
915daflushbioq(struct bio_queue_head *bioq, int error)
916{
917 struct bio *q_bio;
918 struct buf *q_bp;
919
920 while ((q_bio = bioq_first(bioq)) != NULL){
921 bioq_remove(bioq, q_bio);
81b5c339 922 q_bp = q_bio->bio_buf;
984263bc 923 q_bp->b_resid = q_bp->b_bcount;
af0aa0ac 924 q_bp->b_error = error;
984263bc 925 q_bp->b_flags |= B_ERROR;
81b5c339 926 biodone(q_bio);
984263bc 927 }
984263bc
MD
928}
929
930static void
931dacleanup(struct cam_periph *periph)
932{
933 struct da_softc *softc;
934
935 softc = (struct da_softc *)periph->softc;
936
937 devstat_remove_entry(&softc->device_stats);
938 cam_extend_release(daperiphs, periph->unit_number);
1c8b7a9a 939 xpt_print(periph->path, "removing device entry\n");
62ade751
MD
940 /*
941 * If we can't free the sysctl tree, oh well...
942 */
943 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
944 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1c8b7a9a 945 xpt_print(periph->path, "can't remove sysctl context\n");
62ade751 946 }
2d19cdd3 947 periph->softc = NULL;
e4c9c0c8 948 if (softc->disk.d_rawdev) {
1c8b7a9a 949 cam_periph_unlock(periph);
335dda38 950 disk_destroy(&softc->disk);
1c8b7a9a 951 cam_periph_lock(periph);
984263bc 952 }
1c8b7a9a 953
efda3bd0 954 kfree(softc, M_DEVBUF);
984263bc
MD
955}
956
957static void
958daasync(void *callback_arg, u_int32_t code,
959 struct cam_path *path, void *arg)
960{
961 struct cam_periph *periph;
962
963 periph = (struct cam_periph *)callback_arg;
87993e5a 964
984263bc
MD
965 switch (code) {
966 case AC_FOUND_DEVICE:
967 {
968 struct ccb_getdev *cgd;
969 cam_status status;
970
971 cgd = (struct ccb_getdev *)arg;
e9936c96
PA
972 if (cgd == NULL)
973 break;
984263bc
MD
974
975 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
976 && SID_TYPE(&cgd->inq_data) != T_RBC
977 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
978 break;
979
87993e5a
MD
980 /*
981 * Don't complain if a valid peripheral is already attached.
982 */
983 periph = cam_periph_find(cgd->ccb_h.path, "da");
984 if (periph && (periph->flags & CAM_PERIPH_INVALID) == 0)
985 break;
986
984263bc
MD
987 /*
988 * Allocate a peripheral instance for
989 * this device and start the probe
990 * process.
991 */
992 status = cam_periph_alloc(daregister, daoninvalidate,
993 dacleanup, dastart,
994 "da", CAM_PERIPH_BIO,
995 cgd->ccb_h.path, daasync,
996 AC_FOUND_DEVICE, cgd);
997
87993e5a 998 if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) {
5ee727b6
SW
999 kprintf("%s: Unable to attach to new device "
1000 "due to status 0x%x\n", __func__, status);
87993e5a 1001 }
984263bc
MD
1002 break;
1003 }
1004 case AC_SENT_BDR:
1005 case AC_BUS_RESET:
1006 {
1007 struct da_softc *softc;
1008 struct ccb_hdr *ccbh;
984263bc
MD
1009
1010 softc = (struct da_softc *)periph->softc;
984263bc
MD
1011 /*
1012 * Don't fail on the expected unit attention
1013 * that will occur.
1014 */
1015 softc->flags |= DA_FLAG_RETRY_UA;
cbe8f7dc 1016 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
984263bc 1017 ccbh->ccb_state |= DA_CCB_RETRY_UA;
984263bc
MD
1018 /* FALLTHROUGH*/
1019 }
1020 default:
1021 cam_periph_async(periph, code, path, arg);
1022 break;
1023 }
1024}
1025
62ade751
MD
1026static void
1027dasysctlinit(void *context, int pending)
1028{
1029 struct cam_periph *periph;
1030 struct da_softc *softc;
1031 char tmpstr[80], tmpstr2[80];
1032
1033 periph = (struct cam_periph *)context;
227ce828 1034 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
1c8b7a9a 1035 return;
227ce828 1036 }
62ade751 1037
1c8b7a9a 1038 softc = (struct da_softc *)periph->softc;
da10ea93
MD
1039 ksnprintf(tmpstr, sizeof(tmpstr),
1040 "CAM DA unit %d", periph->unit_number);
1041 ksnprintf(tmpstr2, sizeof(tmpstr2),
1042 "%d", periph->unit_number);
62ade751 1043
da10ea93 1044 sysctl_ctx_free(&softc->sysctl_ctx);
62ade751
MD
1045 sysctl_ctx_init(&softc->sysctl_ctx);
1046 softc->flags |= DA_FLAG_SCTX_INIT;
1047 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1048 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1049 CTLFLAG_RD, 0, tmpstr);
1050 if (softc->sysctl_tree == NULL) {
5ee727b6 1051 kprintf("%s: unable to allocate sysctl tree\n", __func__);
1c8b7a9a 1052 cam_periph_release(periph);
62ade751
MD
1053 return;
1054 }
1055
1056 /*
1057 * Now register the sysctl handler, so the user can the value on
1058 * the fly.
1059 */
1060 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1061 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1062 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1063 "Minimum CDB size");
1c8b7a9a 1064
e0fb398b
T
1065 /* Only create the option if the device supports TRIM */
1066 if (softc->disk.d_info.d_trimflag) {
1067 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1068 SYSCTL_CHILDREN(softc->sysctl_tree),
1069 OID_AUTO,
1070 "trim_enabled",
1071 CTLFLAG_RW,
1072 &softc->trim_enabled,
1073 0,
1074 "Enable TRIM for this device (SSD))");
1075 }
1076
1c8b7a9a 1077 cam_periph_release(periph);
62ade751
MD
1078}
1079
1080static int
1081dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1082{
1083 int error, value;
1084
1085 value = *(int *)arg1;
1086
1087 error = sysctl_handle_int(oidp, &value, 0, req);
1088
1089 if ((error != 0)
1090 || (req->newptr == NULL))
1091 return (error);
1092
1093 /*
bdd58e03 1094 * Acceptable values here are 6, 10 or 12, or 16.
62ade751
MD
1095 */
1096 if (value < 6)
1097 value = 6;
1098 else if ((value > 6)
1099 && (value <= 10))
1100 value = 10;
bdd58e03
MD
1101 else if ((value > 10)
1102 && (value <= 12))
62ade751 1103 value = 12;
bdd58e03
MD
1104 else if (value > 12)
1105 value = 16;
62ade751
MD
1106
1107 *(int *)arg1 = value;
1108
1109 return (0);
1110}
1111
984263bc
MD
1112static cam_status
1113daregister(struct cam_periph *periph, void *arg)
1114{
984263bc 1115 struct da_softc *softc;
cec957e9 1116 struct ccb_pathinq *cpi;
984263bc 1117 struct ccb_getdev *cgd;
62ade751 1118 char tmpstr[80];
984263bc
MD
1119 caddr_t match;
1120
1121 cgd = (struct ccb_getdev *)arg;
1122 if (periph == NULL) {
5ee727b6 1123 kprintf("%s: periph was NULL!!\n", __func__);
984263bc
MD
1124 return(CAM_REQ_CMP_ERR);
1125 }
1126
1127 if (cgd == NULL) {
5ee727b6
SW
1128 kprintf("%s: no getdev CCB, can't register device\n",
1129 __func__);
984263bc
MD
1130 return(CAM_REQ_CMP_ERR);
1131 }
1132
efda3bd0 1133 softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
da10ea93 1134 sysctl_ctx_init(&softc->sysctl_ctx);
984263bc
MD
1135 LIST_INIT(&softc->pending_ccbs);
1136 softc->state = DA_STATE_PROBE;
e0fb398b 1137 bioq_init(&softc->bio_queue_trim);
af0aa0ac
MD
1138 bioq_init(&softc->bio_queue_rd);
1139 bioq_init(&softc->bio_queue_wr);
984263bc
MD
1140 if (SID_IS_REMOVABLE(&cgd->inq_data))
1141 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1142 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1143 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1144
e0fb398b
T
1145 /* Used to get TRIM status from AHCI driver */
1146 if (cgd->inq_data.vendor_specific1[0] == 1) {
1147 /*
1148 * max number of lba ranges an SSD can handle in a single
1149 * TRIM command. vendor_specific1[1] is the num of 512-byte
1150 * blocks the SSD reports that can be passed in a TRIM cmd.
1151 */
1152 softc->trim_max_ranges =
1153 min(cgd->inq_data.vendor_specific1[1] * 64, TRIM_MAX_RANGES);
1154 }
1155
984263bc
MD
1156 periph->softc = softc;
1157
1158 cam_extend_set(daperiphs, periph->unit_number, periph);
1159
1160 /*
1161 * See if this device has any quirks.
1162 */
1163 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1164 (caddr_t)da_quirk_table,
b370aff7 1165 NELEM(da_quirk_table),
984263bc
MD
1166 sizeof(*da_quirk_table), scsi_inquiry_match);
1167
1168 if (match != NULL)
1169 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1170 else
1171 softc->quirks = DA_Q_NONE;
1172
a9453758
MD
1173 /*
1174 * Unconditionally disable the synchronize cache command for
1175 * usb attachments. It's just impossible to determine if the
1176 * device supports it or not and if it doesn't the port can
1177 * brick.
1178 */
1179 if (strncmp(periph->sim->sim_name, "umass", 4) == 0) {
1180 softc->quirks |= DA_Q_NO_SYNC_CACHE;
1181 }
1182
62ade751
MD
1183 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1184
1185 /* Check if the SIM does not want 6 byte commands */
cec957e9
MD
1186 cpi = &xpt_alloc_ccb()->cpi;
1187 xpt_setup_ccb(&cpi->ccb_h, periph->path, /*priority*/1);
1188 cpi->ccb_h.func_code = XPT_PATH_INQ;
1189 xpt_action((union ccb *)cpi);
1190 if (cpi->ccb_h.status == CAM_REQ_CMP && (cpi->hba_misc & PIM_NO_6_BYTE))
62ade751
MD
1191 softc->quirks |= DA_Q_NO_6_BYTE;
1192
1193 /*
1194 * RBC devices don't have to support READ(6), only READ(10).
1195 */
984263bc
MD
1196 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1197 softc->minimum_cmd_size = 10;
1198 else
1199 softc->minimum_cmd_size = 6;
1200
62ade751
MD
1201 /*
1202 * Load the user's default, if any.
1203 */
f8c7a42d 1204 ksnprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
62ade751
MD
1205 periph->unit_number);
1206 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1207
1208 /*
bdd58e03 1209 * 6, 10, 12, and 16 are the currently permissible values.
62ade751
MD
1210 */
1211 if (softc->minimum_cmd_size < 6)
1212 softc->minimum_cmd_size = 6;
1213 else if ((softc->minimum_cmd_size > 6)
1214 && (softc->minimum_cmd_size <= 10))
1215 softc->minimum_cmd_size = 10;
bdd58e03
MD
1216 else if ((softc->minimum_cmd_size > 10)
1217 && (softc->minimum_cmd_size <= 12))
62ade751 1218 softc->minimum_cmd_size = 12;
bdd58e03
MD
1219 else if (softc->minimum_cmd_size > 12)
1220 softc->minimum_cmd_size = 16;
62ade751 1221
984263bc
MD
1222 /*
1223 * The DA driver supports a blocksize, but
1224 * we don't know the blocksize until we do
1225 * a read capacity. So, set a flag to
1226 * indicate that the blocksize is
1227 * unavailable right now. We'll clear the
1228 * flag as soon as we've done a read capacity.
1229 */
1230 devstat_add_entry(&softc->device_stats, "da",
1231 periph->unit_number, 0,
1232 DEVSTAT_BS_UNAVAILABLE,
1233 SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
1234 DEVSTAT_PRIORITY_DISK);
1235
1236 /*
1237 * Register this media as a disk
1238 */
1c8b7a9a 1239 CAM_SIM_UNLOCK(periph->sim);
a688b15c 1240 disk_create(periph->unit_number, &softc->disk, &da_ops);
cec957e9 1241 if (cpi->maxio == 0 || cpi->maxio > MAXPHYS)
eaae6702
SW
1242 softc->disk.d_rawdev->si_iosize_max = MAXPHYS;
1243 else
cec957e9 1244 softc->disk.d_rawdev->si_iosize_max = cpi->maxio;
0b41f48b
SW
1245 if (bootverbose) {
1246 kprintf("%s%d: si_iosize_max:%d\n",
1247 periph->periph_name,
1248 periph->unit_number,
1249 softc->disk.d_rawdev->si_iosize_max);
1250 }
1c8b7a9a 1251 CAM_SIM_LOCK(periph->sim);
984263bc
MD
1252
1253 /*
1254 * Add async callbacks for bus reset and
1255 * bus device reset calls. I don't bother
1256 * checking if this fails as, in most cases,
1257 * the system will function just fine without
1258 * them and the only alternative would be to
1259 * not attach the device on failure.
1260 */
1c8b7a9a
PA
1261 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
1262 daasync, periph, periph->path);
1263
984263bc 1264 /*
1c8b7a9a
PA
1265 * Take an exclusive refcount on the periph while dastart is called
1266 * to finish the probe. The reference will be dropped in dadone at
1267 * the end of probe.
984263bc 1268 */
cec957e9 1269 xpt_free_ccb(&cpi->ccb_h);
1c8b7a9a 1270 cam_periph_hold(periph, 0);
984263bc
MD
1271 xpt_schedule(periph, /*priority*/5);
1272
1273 return(CAM_REQ_CMP);
1274}
1275
1276static void
1277dastart(struct cam_periph *periph, union ccb *start_ccb)
1278{
1279 struct da_softc *softc;
1280
1281 softc = (struct da_softc *)periph->softc;
1282
984263bc
MD
1283 switch (softc->state) {
1284 case DA_STATE_NORMAL:
1285 {
1286 /* Pull a buffer from the queue and get going on it */
81b5c339 1287 struct bio *bio;
af0aa0ac
MD
1288 struct bio *bio_rd;
1289 struct bio *bio_wr;
984263bc 1290 struct buf *bp;
a9bf1b8c 1291 u_int8_t tag_code;
128500e6
MD
1292 int rd_limit;
1293 int wr_limit;
984263bc
MD
1294
1295 /*
1296 * See if there is a buf with work for us to do..
1297 */
af0aa0ac
MD
1298 bio_rd = bioq_first(&softc->bio_queue_rd);
1299 bio_wr = bioq_first(&softc->bio_queue_wr);
1300
984263bc
MD
1301 if (periph->immediate_priority <= periph->pinfo.priority) {
1302 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1303 ("queuing for immediate ccb\n"));
1304 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1305 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1306 periph_links.sle);
1307 periph->immediate_priority = CAM_PRIORITY_NONE;
984263bc 1308 wakeup(&periph->ccb_list);
af0aa0ac 1309 if (bio_rd || bio_wr) {
a9bf1b8c
MD
1310 /*
1311 * Have more work to do, so ensure we stay
1312 * scheduled
1313 */
1314 xpt_schedule(periph, /* XXX priority */1);
1315 }
1316 break;
1317 }
af0aa0ac 1318
e0fb398b
T
1319 /* Run the trim command if not already running */
1320 if (!softc->trim_running &&
4090d6ff 1321 (bio = bioq_first(&softc->bio_queue_trim)) != NULL) {
e0fb398b
T
1322 struct trim_request *req = &softc->trim_req;
1323 struct bio *bio1;
1324 int bps = 0, ranges = 0;
1325
1326 softc->trim_running = 1;
1327 bzero(req, sizeof(*req));
1328 bio1 = bio;
1329 while (1) {
1330 uint64_t lba;
1331 int count;
1332
1333 bp = bio1->bio_buf;
1334 count = bp->b_bcount / softc->params.secsize;
1335 lba = bio1->bio_offset/softc->params.secsize;
1336
e0fb398b
T
1337 bioq_remove(&softc->bio_queue_trim, bio1);
1338 while (count > 0) {
1339 int c = min(count, 0xffff);
1340 int off = ranges * 8;
1341
1342 req->data[off + 0] = lba & 0xff;
1343 req->data[off + 1] = (lba >> 8) & 0xff;
1344 req->data[off + 2] = (lba >> 16) & 0xff;
1345 req->data[off + 3] = (lba >> 24) & 0xff;
1346 req->data[off + 4] = (lba >> 32) & 0xff;
1347 req->data[off + 5] = (lba >> 40) & 0xff;
1348 req->data[off + 6] = c & 0xff;
1349 req->data[off + 7] = (c >> 8) & 0xff;
1350 lba += c;
1351 count -= c;
1352 ranges++;
1353 }
1354
1355 /* Try to merge multiple TRIM requests */
1356 req->bios[bps++] = bio1;
1357 bio1 = bioq_first(&softc->bio_queue_trim);
1358 if (bio1 == NULL ||
1359 bio1->bio_buf->b_bcount / softc->params.secsize >
1360 (softc->trim_max_ranges - ranges) * 0xffff)
1361 break;
1362 }
1363
1364
1365 cam_fill_csio(&start_ccb->csio,
1366 1/*retries*/,
1367 dadone,
1368 CAM_DIR_OUT,
1369 MSG_SIMPLE_Q_TAG,
1370 req->data,
1371 ((ranges +63)/64)*512,
1372 SSD_FULL_SIZE,
1373 sizeof(struct scsi_rw_6),
1374 da_default_timeout*2);
1375
1376 start_ccb->ccb_h.ccb_state = DA_CCB_TRIM;
1377 LIST_INSERT_HEAD(&softc->pending_ccbs,
1378 &start_ccb->ccb_h, periph_links.le);
1379 start_ccb->csio.ccb_h.func_code = XPT_TRIM;
1380 start_ccb->ccb_h.ccb_bio = bio;
1381 devstat_start_transaction(&softc->device_stats);
1382 xpt_action(start_ccb);
1383 xpt_schedule(periph, 1);
1384 break;
1385 }
1386
af0aa0ac
MD
1387 /*
1388 * Select a read or write buffer to queue. Limit the number
1389 * of tags dedicated to reading or writing, giving reads
1390 * precedence.
1391 *
1392 * Writes to modern hard drives go into the HDs cache and
1393 * return completion nearly instantly. That is until the
1394 * cache becomes full. When the HDs cache becomes full
1395 * write commands will begin to stall. If all available
1396 * tags are taken up by writes which saturate the drive
1397 * reads will become tag-starved.
1398 *
1399 * A similar situation can occur with reads. With many
1400 * parallel readers all tags can be taken up by reads
1401 * and prevent any writes from draining, even if the HD's
1402 * cache is not full.
1403 */
128500e6
MD
1404 rd_limit = periph->sim->max_tagged_dev_openings * 2 / 3 + 1;
1405 wr_limit = rd_limit;
1406
1407 /*
1408 * When TPS balancing is enabled we force wr_limit to 0
1409 * as necessary to balance the read TPS against the write
1410 * TPS. A lower or higher read:write ratio may be selected
1411 * via da_balance_ratio.
1412 *
1413 * wr_limit forcing stops queueing writes. This is generally
1414 * necessary because devices buffer writes and may starve
1415 * reads even when plenty of read tags are available.
1416 *
1417 * When no reads are being done, normalize tps_rd to avoid
1418 * instantly crowbaring the write tps.
1419 */
1420 if (da_balance_enable &&
1421 periph->sim->max_tagged_dev_openings >= 8 &&
1422 (bio_rd || softc->outstanding_cmds_rd) &&
1423 (bio_wr || softc->outstanding_cmds_wr) &&
1424 softc->tps_rd * 100 < softc->tps_wr * da_balance_ratio) {
1425 wr_limit = 0;
1426 } else if (bio_rd == NULL && softc->outstanding_cmds_rd == 0 &&
1427 softc->tps_rd < softc->tps_wr * 2) {
1428 softc->tps_rd += 100;
1429 }
1430 if (softc->tps_ticks != ticks) {
1431 softc->tps_ticks = ticks;
1432 softc->tps_rd = (softc->tps_rd * (hz - 1)) / hz;
1433 softc->tps_wr = (softc->tps_wr * (hz - 1)) / hz;
1434 }
1435
1436#if 1
af0aa0ac 1437 /* DEBUGGING */
128500e6
MD
1438 static time_t savets;
1439 if (da_balance_debug &&
1440 time_uptime != savets && (bio_rd || bio_wr ||
1441 softc->outstanding_cmds_rd ||
1442 softc->outstanding_cmds_wr)) {
1443 kprintf("softc=%p %d/%d %d/%d tps %ld/%ld\n",
1444 softc,
1445 softc->outstanding_cmds_rd, rd_limit,
1446 softc->outstanding_cmds_wr, wr_limit,
1447 softc->tps_rd / 100, softc->tps_wr / 100);
1448 savets = time_uptime;
af0aa0ac
MD
1449 }
1450#endif
128500e6 1451 if (bio_rd && softc->outstanding_cmds_rd < rd_limit) {
af0aa0ac
MD
1452 bio = bio_rd;
1453 bioq_remove(&softc->bio_queue_rd, bio);
128500e6
MD
1454 softc->tps_rd += 100;
1455 } else if (bio_wr && softc->outstanding_cmds_wr < wr_limit) {
af0aa0ac
MD
1456 bio = bio_wr;
1457 bioq_remove(&softc->bio_queue_wr, bio);
128500e6 1458 softc->tps_wr += 100;
af0aa0ac
MD
1459 } else {
1460 if (bio_rd)
1461 softc->flags |= DA_FLAG_RD_LIMIT;
1462 if (bio_wr)
1463 softc->flags |= DA_FLAG_WR_LIMIT;
984263bc 1464 xpt_release_ccb(start_ccb);
a9bf1b8c
MD
1465 break;
1466 }
984263bc 1467
a9bf1b8c
MD
1468 /*
1469 * We can queue new work.
1470 */
a9bf1b8c 1471 bp = bio->bio_buf;
54078292 1472
a9bf1b8c 1473 devstat_start_transaction(&softc->device_stats);
54078292 1474
0c4341b9 1475 tag_code = MSG_SIMPLE_Q_TAG;
984263bc 1476
a9bf1b8c
MD
1477 switch(bp->b_cmd) {
1478 case BUF_CMD_READ:
1479 case BUF_CMD_WRITE:
984263bc 1480 /*
a9bf1b8c 1481 * Block read/write op
984263bc 1482 */
a9bf1b8c 1483 KKASSERT(bio->bio_offset % softc->params.secsize == 0);
984263bc 1484
a9bf1b8c
MD
1485 scsi_read_write(
1486 &start_ccb->csio,
1487 da_retry_count, /* retries */
1488 dadone,
1489 tag_code,
1490 (bp->b_cmd == BUF_CMD_READ),
1491 0, /* byte2 */
1492 softc->minimum_cmd_size,
1493 bio->bio_offset / softc->params.secsize,
1494 bp->b_bcount / softc->params.secsize,
1495 bp->b_data,
1496 bp->b_bcount,
1497 SSD_FULL_SIZE, /* sense_len */
1498 da_default_timeout * 1000
1499 );
1500 break;
1501 case BUF_CMD_FLUSH:
a9453758
MD
1502 /*
1503 * Silently complete a flush request if the device
1504 * cannot handle it.
1505 */
1506 if (softc->quirks & DA_Q_NO_SYNC_CACHE) {
1507 xpt_release_ccb(start_ccb);
1508 start_ccb = NULL;
1509 devstat_end_transaction_buf(
1510 &softc->device_stats, bp);
1511 biodone(bio);
1512 } else {
1513 scsi_synchronize_cache(
1514 &start_ccb->csio,
1515 1, /* retries */
1516 dadone, /* cbfcnp */
1517 MSG_SIMPLE_Q_TAG,
1518 0, /* lba */
1519 0, /* count (whole disk) */
1520 SSD_FULL_SIZE,
1521 da_default_timeout*1000 /* timeout */
1522 );
1523 }
a9bf1b8c 1524 break;
e0fb398b
T
1525 case BUF_CMD_FREEBLKS:
1526 if (softc->disk.d_info.d_trimflag & DA_FLAG_CAN_TRIM){
1527 start_ccb->csio.ccb_h.func_code = XPT_TRIM;
1528 break;
1529 }
a9bf1b8c 1530 default:
a9453758
MD
1531 xpt_release_ccb(start_ccb);
1532 start_ccb = NULL;
a9bf1b8c
MD
1533 panic("dastart: unrecognized bio cmd %d", bp->b_cmd);
1534 break; /* NOT REACHED */
1535 }
984263bc 1536
a9bf1b8c
MD
1537 /*
1538 * Block out any asyncronous callbacks
1539 * while we touch the pending ccb list.
1540 */
a9453758
MD
1541 if (start_ccb) {
1542 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1543 LIST_INSERT_HEAD(&softc->pending_ccbs,
1544 &start_ccb->ccb_h, periph_links.le);
af0aa0ac
MD
1545 if (bp->b_cmd == BUF_CMD_WRITE ||
1546 bp->b_cmd == BUF_CMD_FLUSH) {
1547 ++softc->outstanding_cmds_wr;
1548 } else {
1549 ++softc->outstanding_cmds_rd;
1550 }
a9453758
MD
1551
1552 /* We expect a unit attention from this device */
1553 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1554 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1555 softc->flags &= ~DA_FLAG_RETRY_UA;
1556 }
a9bf1b8c 1557
a9453758
MD
1558 start_ccb->ccb_h.ccb_bio = bio;
1559 xpt_action(start_ccb);
1560 }
984263bc 1561
a9bf1b8c
MD
1562 /*
1563 * Be sure we stay scheduled if we have more work to do.
1564 */
af0aa0ac
MD
1565 if (bioq_first(&softc->bio_queue_rd) ||
1566 bioq_first(&softc->bio_queue_wr)) {
a9bf1b8c 1567 xpt_schedule(periph, 1);
af0aa0ac 1568 }
984263bc
MD
1569 break;
1570 }
1571 case DA_STATE_PROBE:
1572 {
1573 struct ccb_scsiio *csio;
1574 struct scsi_read_capacity_data *rcap;
1575
1c8b7a9a 1576 rcap = kmalloc(sizeof(*rcap), M_SCSIDA, M_INTWAIT | M_ZERO);
984263bc
MD
1577 csio = &start_ccb->csio;
1578 scsi_read_capacity(csio,
1579 /*retries*/4,
1580 dadone,
1581 MSG_SIMPLE_Q_TAG,
1582 rcap,
1583 SSD_FULL_SIZE,
1584 /*timeout*/5000);
81b5c339 1585 start_ccb->ccb_h.ccb_bio = NULL;
984263bc
MD
1586 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1587 xpt_action(start_ccb);
1588 break;
1589 }
bdd58e03
MD
1590 case DA_STATE_PROBE2:
1591 {
1592 struct ccb_scsiio *csio;
0b0362e1 1593 struct scsi_read_capacity_data_16 *rcaplong;
bdd58e03 1594
0b0362e1
MD
1595 rcaplong = kmalloc(sizeof(*rcaplong), M_SCSIDA,
1596 M_INTWAIT | M_ZERO);
bdd58e03
MD
1597 csio = &start_ccb->csio;
1598 scsi_read_capacity_16(csio,
1599 /*retries*/ 4,
1600 /*cbfcnp*/ dadone,
1601 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1602 /*lba*/ 0,
1603 /*reladr*/ 0,
1604 /*pmi*/ 0,
1605 rcaplong,
1606 /*sense_len*/ SSD_FULL_SIZE,
1607 /*timeout*/ 60000);
1608 start_ccb->ccb_h.ccb_bio = NULL;
1609 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1610 xpt_action(start_ccb);
1611 break;
1612 }
984263bc
MD
1613 }
1614}
1615
1616static int
1617cmd6workaround(union ccb *ccb)
1618{
1619 struct scsi_rw_6 cmd6;
1620 struct scsi_rw_10 *cmd10;
1621 struct da_softc *softc;
1622 u_int8_t *cdb;
1623 int frozen;
1624
1625 cdb = ccb->csio.cdb_io.cdb_bytes;
1626
1627 /* Translation only possible if CDB is an array and cmd is R/W6 */
1628 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1629 (*cdb != READ_6 && *cdb != WRITE_6))
1630 return 0;
1631
1c8b7a9a
PA
1632 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
1633 "increasing minimum_cmd_size to 10.\n");
984263bc
MD
1634 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1635 softc->minimum_cmd_size = 10;
1636
1637 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1638 cmd10 = (struct scsi_rw_10 *)cdb;
1639 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1640 cmd10->byte2 = 0;
1641 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1642 cmd10->reserved = 0;
1643 scsi_ulto2b(cmd6.length, cmd10->length);
1644 cmd10->control = cmd6.control;
1645 ccb->csio.cdb_len = sizeof(*cmd10);
1646
1647 /* Requeue request, unfreezing queue if necessary */
1648 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1649 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1650 xpt_action(ccb);
1651 if (frozen) {
1652 cam_release_devq(ccb->ccb_h.path,
1653 /*relsim_flags*/0,
1654 /*reduction*/0,
1655 /*timeout*/0,
1656 /*getcount_only*/0);
1657 }
1658 return (ERESTART);
1659}
1660
1661static void
1662dadone(struct cam_periph *periph, union ccb *done_ccb)
1663{
1664 struct da_softc *softc;
1665 struct ccb_scsiio *csio;
cd29885a 1666 struct disk_info info;
984263bc
MD
1667
1668 softc = (struct da_softc *)periph->softc;
1669 csio = &done_ccb->csio;
1670 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1671 case DA_CCB_BUFFER_IO:
e0fb398b 1672 case DA_CCB_TRIM:
984263bc
MD
1673 {
1674 struct buf *bp;
81b5c339 1675 struct bio *bio;
af0aa0ac 1676 int mustsched = 0;
984263bc 1677
81b5c339
MD
1678 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1679 bp = bio->bio_buf;
984263bc
MD
1680 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1681 int error;
984263bc
MD
1682 int sf;
1683
1684 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1685 sf = SF_RETRY_UA;
1686 else
1687 sf = 0;
1688
b05e84c9
PA
1689 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1690 if (error == ERESTART) {
984263bc
MD
1691 /*
1692 * A retry was scheuled, so
1693 * just return.
1694 */
1695 return;
1696 }
1697 if (error != 0) {
984263bc
MD
1698 if (error == ENXIO) {
1699 /*
1700 * Catastrophic error. Mark our pack as
1701 * invalid.
1702 */
1c8b7a9a
PA
1703 /*
1704 * XXX See if this is really a media
1705 * XXX change first?
984263bc 1706 */
1c8b7a9a
PA
1707 xpt_print(periph->path,
1708 "Invalidating pack\n");
984263bc
MD
1709 softc->flags |= DA_FLAG_PACK_INVALID;
1710 }
1711
1712 /*
af0aa0ac
MD
1713 * Return all queued write I/O's with EIO
1714 * so the client can retry these I/Os in the
984263bc 1715 * proper order should it attempt to recover.
af0aa0ac
MD
1716 *
1717 * Leave read I/O's alone.
984263bc 1718 */
af0aa0ac 1719 daflushbioq(&softc->bio_queue_wr, EIO);
984263bc
MD
1720 bp->b_error = error;
1721 bp->b_resid = bp->b_bcount;
1722 bp->b_flags |= B_ERROR;
1723 } else {
1724 bp->b_resid = csio->resid;
1725 bp->b_error = 0;
62ade751 1726 if (bp->b_resid != 0)
984263bc 1727 bp->b_flags |= B_ERROR;
984263bc
MD
1728 }
1729 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1730 cam_release_devq(done_ccb->ccb_h.path,
1731 /*relsim_flags*/0,
1732 /*reduction*/0,
1733 /*timeout*/0,
1734 /*getcount_only*/0);
1735 } else {
b05e84c9
PA
1736 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1737 panic("REQ_CMP with QFRZN");
984263bc 1738 bp->b_resid = csio->resid;
62ade751 1739 if (csio->resid > 0)
984263bc 1740 bp->b_flags |= B_ERROR;
984263bc
MD
1741 }
1742
1743 /*
128500e6
MD
1744 * Schedule the peripheral to pipeline further reads and
1745 * writes. A completed write wakes up more pending writes.
1746 * A completed read must wake up on either pending reads
1747 * or writes due to TPS balancing.
1748 *
1749 * Block out any asyncronous callbacks while we touch the
1750 * pending ccb list.
984263bc 1751 */
984263bc 1752 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
af0aa0ac
MD
1753 if (bp->b_cmd == BUF_CMD_WRITE || bp->b_cmd == BUF_CMD_FLUSH) {
1754 --softc->outstanding_cmds_wr;
1755 if (softc->flags & DA_FLAG_WR_LIMIT) {
1756 softc->flags &= ~DA_FLAG_WR_LIMIT;
1757 mustsched = 1;
1758 }
1759 } else {
1760 --softc->outstanding_cmds_rd;
128500e6
MD
1761 if (softc->flags &
1762 (DA_FLAG_RD_LIMIT | DA_FLAG_WR_LIMIT)) {
1763 softc->flags &=
1764 ~(DA_FLAG_RD_LIMIT | DA_FLAG_WR_LIMIT);
af0aa0ac
MD
1765 mustsched = 1;
1766 }
1767 }
984263bc
MD
1768
1769 devstat_end_transaction_buf(&softc->device_stats, bp);
e0fb398b
T
1770 if ((csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) ==
1771 DA_CCB_TRIM) {
1772 struct trim_request *req =
1773 (struct trim_request *) csio->data_ptr;
1774 int i;
1775
1776 for (i = 1; i < softc->trim_max_ranges &&
1777 req->bios[i]; i++) {
1778 struct bio *bp1 = req->bios[i];
1779
1780 bp1->bio_buf->b_resid = bp->b_resid;
1781 bp1->bio_buf->b_error = bp->b_error;
1782 if (bp->b_flags & B_ERROR)
1783 bp1->bio_buf->b_flags |= B_ERROR;
1784 biodone(bp1);
1785 }
1786 softc->trim_running = 0;
1787 biodone(bio);
1788 xpt_schedule(periph,1);
1789 } else
1790 biodone(bio);
1791
af0aa0ac
MD
1792
1793 if (mustsched)
1794 xpt_schedule(periph, /*priority*/1);
1795
984263bc
MD
1796 break;
1797 }
1798 case DA_CCB_PROBE:
bdd58e03 1799 case DA_CCB_PROBE2:
984263bc
MD
1800 {
1801 struct scsi_read_capacity_data *rdcap;
0b0362e1 1802 struct scsi_read_capacity_data_16 *rcaplong;
984263bc 1803 char announce_buf[80];
2ea825fb 1804 int doinfo = 0;
984263bc 1805
bdd58e03
MD
1806 rdcap = NULL;
1807 rcaplong = NULL;
1808 if (softc->state == DA_STATE_PROBE)
1809 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1810 else
0b0362e1 1811 rcaplong = (struct scsi_read_capacity_data_16 *)
bdd58e03 1812 csio->data_ptr;
f7b26992
MD
1813
1814 bzero(&info, sizeof(info));
1815 info.d_type = DTYPE_SCSI;
1816 info.d_serialno = xpt_path_serialno(periph->path);
984263bc
MD
1817
1818 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1819 struct disk_params *dp;
bdd58e03
MD
1820 uint32_t block_size;
1821 uint64_t maxsector;
1822
1823 if (softc->state == DA_STATE_PROBE) {
1824 block_size = scsi_4btoul(rdcap->length);
1825 maxsector = scsi_4btoul(rdcap->addr);
984263bc 1826
bdd58e03
MD
1827 /*
1828 * According to SBC-2, if the standard 10
1829 * byte READ CAPACITY command returns 2^32,
1830 * we should issue the 16 byte version of
1831 * the command, since the device in question
1832 * has more sectors than can be represented
1833 * with the short version of the command.
1834 */
1835 if (maxsector == 0xffffffff) {
1836 softc->state = DA_STATE_PROBE2;
1c8b7a9a 1837 kfree(rdcap, M_SCSIDA);
bdd58e03
MD
1838 xpt_release_ccb(done_ccb);
1839 xpt_schedule(periph, /*priority*/5);
1840 return;
1841 }
1842 } else {
1843 block_size = scsi_4btoul(rcaplong->length);
1844 maxsector = scsi_8btou64(rcaplong->addr);
1845 }
1846 dasetgeom(periph, block_size, maxsector);
984263bc 1847 dp = &softc->params;
f8c7a42d 1848 ksnprintf(announce_buf, sizeof(announce_buf),
bdd58e03
MD
1849 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1850 (uintmax_t) (((uintmax_t)dp->secsize *
1851 dp->sectors) / (1024*1024)),
1852 (uintmax_t)dp->sectors,
984263bc
MD
1853 dp->secsize, dp->heads, dp->secs_per_track,
1854 dp->cylinders);
e0fb398b 1855
cd29885a
MD
1856 info.d_media_blksize = softc->params.secsize;
1857 info.d_media_blocks = softc->params.sectors;
1858 info.d_media_size = 0;
1859 info.d_secpertrack = softc->params.secs_per_track;
1860 info.d_nheads = softc->params.heads;
1861 info.d_ncylinders = softc->params.cylinders;
1862 info.d_secpercyl = softc->params.heads *
1863 softc->params.secs_per_track;
55230951 1864 info.d_serialno = xpt_path_serialno(periph->path);
2ea825fb 1865 doinfo = 1;
984263bc
MD
1866 } else {
1867 int error;
1868
1869 announce_buf[0] = '\0';
1870
1871 /*
1872 * Retry any UNIT ATTENTION type errors. They
1873 * are expected at boot.
1874 */
b05e84c9
PA
1875 error = daerror(done_ccb, CAM_RETRY_SELTO,
1876 SF_RETRY_UA|SF_NO_PRINT);
984263bc
MD
1877 if (error == ERESTART) {
1878 /*
1879 * A retry was scheuled, so
1880 * just return.
1881 */
1882 return;
1883 } else if (error != 0) {
1884 struct scsi_sense_data *sense;
1885 int asc, ascq;
1886 int sense_key, error_code;
1887 int have_sense;
1888 cam_status status;
cec957e9 1889 struct ccb_getdev *cgd;
984263bc
MD
1890
1891 /* Don't wedge this device's queue */
984263bc 1892 status = done_ccb->ccb_h.status;
b05e84c9
PA
1893 if ((status & CAM_DEV_QFRZN) != 0)
1894 cam_release_devq(done_ccb->ccb_h.path,
1895 /*relsim_flags*/0,
1896 /*reduction*/0,
1897 /*timeout*/0,
1898 /*getcount_only*/0);
1899
cec957e9
MD
1900 cgd = &xpt_alloc_ccb()->cgd;
1901 xpt_setup_ccb(&cgd->ccb_h,
984263bc
MD
1902 done_ccb->ccb_h.path,
1903 /* priority */ 1);
cec957e9
MD
1904 cgd->ccb_h.func_code = XPT_GDEV_TYPE;
1905 xpt_action((union ccb *)cgd);
984263bc
MD
1906
1907 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1908 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1909 || ((status & CAM_AUTOSNS_VALID) == 0))
1910 have_sense = FALSE;
1911 else
1912 have_sense = TRUE;
1913
1914 if (have_sense) {
1915 sense = &csio->sense_data;
1916 scsi_extract_sense(sense, &error_code,
1917 &sense_key,
1918 &asc, &ascq);
1919 }
1920 /*
1921 * Attach to anything that claims to be a
1922 * direct access or optical disk device,
1923 * as long as it doesn't return a "Logical
1924 * unit not supported" (0x25) error.
1925 */
1926 if ((have_sense) && (asc != 0x25)
b05e84c9
PA
1927 && (error_code == SSD_CURRENT_ERROR)) {
1928 const char *sense_key_desc;
1929 const char *asc_desc;
1930
1931 scsi_sense_desc(sense_key, asc, ascq,
cec957e9 1932 &cgd->inq_data,
b05e84c9
PA
1933 &sense_key_desc,
1934 &asc_desc);
f8c7a42d 1935 ksnprintf(announce_buf,
984263bc
MD
1936 sizeof(announce_buf),
1937 "Attempt to query device "
1938 "size failed: %s, %s",
b05e84c9
PA
1939 sense_key_desc,
1940 asc_desc);
f7b26992 1941 info.d_media_blksize = 512;
2ea825fb 1942 doinfo = 1;
b05e84c9 1943 } else {
984263bc
MD
1944 if (have_sense)
1945 scsi_sense_print(
1946 &done_ccb->csio);
1947 else {
1c8b7a9a
PA
1948 xpt_print(periph->path,
1949 "got CAM status %#x\n",
1950 done_ccb->ccb_h.status);
984263bc
MD
1951 }
1952
1c8b7a9a
PA
1953 xpt_print(periph->path, "fatal error, "
1954 "failed to attach to device\n");
984263bc
MD
1955
1956 /*
1957 * Free up resources.
1958 */
1959 cam_periph_invalidate(periph);
1960 }
cec957e9 1961 xpt_free_ccb(&cgd->ccb_h);
984263bc
MD
1962 }
1963 }
1c8b7a9a 1964 kfree(csio->data_ptr, M_SCSIDA);
f2187f0a 1965 if (announce_buf[0] != '\0')
984263bc 1966 xpt_announce_periph(periph, announce_buf);
e0fb398b
T
1967
1968 if (softc->trim_max_ranges) {
f2187f0a 1969 info.d_trimflag |= DA_FLAG_CAN_TRIM;
e0fb398b
T
1970 kprintf("%s%d: supports TRIM\n",
1971 periph->periph_name,
1972 periph->unit_number);
1973 }
b05e84c9 1974 softc->state = DA_STATE_NORMAL;
f2187f0a 1975
984263bc
MD
1976 /*
1977 * Since our peripheral may be invalidated by an error
1978 * above or an external event, we must release our CCB
1979 * before releasing the probe lock on the peripheral.
1980 * The peripheral will only go away once the last lock
1981 * is removed, and we need it around for the CCB release
1982 * operation.
1983 */
1984 xpt_release_ccb(done_ccb);
2d19cdd3 1985 cam_periph_unhold(periph, 0);
2ea825fb
MD
1986 if (doinfo) {
1987 CAM_SIM_UNLOCK(periph->sim);
1988 disk_setdiskinfo(&softc->disk, &info);
1989 CAM_SIM_LOCK(periph->sim);
f2187f0a
MD
1990
1991 /*
1992 * Create our sysctl variables, now that we know
1993 * we have successfully attached.
1994 */
1995 taskqueue_enqueue(taskqueue_thread[mycpuid],
1996 &softc->sysctl_task);
2ea825fb 1997 }
984263bc
MD
1998 return;
1999 }
2000 case DA_CCB_WAITING:
2001 {
2002 /* Caller will release the CCB */
2003 wakeup(&done_ccb->ccb_h.cbfcnp);
2004 return;
2005 }
2006 case DA_CCB_DUMP:
2007 /* No-op. We're polling */
2008 return;
a9f09b75
MD
2009 case DA_CCB_POLLED:
2010 /* Caller releases ccb */
2011 wakeup(&done_ccb->ccb_h.cbfcnp);
2012 return;
984263bc
MD
2013 default:
2014 break;
2015 }
2016 xpt_release_ccb(done_ccb);
2017}
2018
2019static int
2020daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2021{
2022 struct da_softc *softc;
2023 struct cam_periph *periph;
62ade751 2024 int error;
984263bc
MD
2025
2026 periph = xpt_path_periph(ccb->ccb_h.path);
2027 softc = (struct da_softc *)periph->softc;
2028
2029 /*
2030 * Automatically detect devices that do not support
2031 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
2032 */
2033 error = 0;
62ade751
MD
2034 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
2035 error = cmd6workaround(ccb);
2036 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2037 CAM_SCSI_STATUS_ERROR)
2038 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
2039 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
2040 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
2041 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
2042 int sense_key, error_code, asc, ascq;
2043
984263bc
MD
2044 scsi_extract_sense(&ccb->csio.sense_data,
2045 &error_code, &sense_key, &asc, &ascq);
2046 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2047 error = cmd6workaround(ccb);
2048 }
2049 if (error == ERESTART)
2050 return (ERESTART);
2051
2052 /*
2053 * XXX
2054 * Until we have a better way of doing pack validation,
2055 * don't treat UAs as errors.
2056 */
2057 sense_flags |= SF_RETRY_UA;
2058 return(cam_periph_error(ccb, cam_flags, sense_flags,
2059 &softc->saved_ccb));
2060}
2061
2062static void
2063daprevent(struct cam_periph *periph, int action)
2064{
2065 struct da_softc *softc;
2066 union ccb *ccb;
2067 int error;
2068
2069 softc = (struct da_softc *)periph->softc;
2070
2071 if (((action == PR_ALLOW)
2072 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
2073 || ((action == PR_PREVENT)
2074 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
2075 return;
2076 }
2077
2078 ccb = cam_periph_getccb(periph, /*priority*/1);
a9f09b75 2079 ccb->ccb_h.ccb_state = DA_CCB_POLLED;
984263bc
MD
2080
2081 scsi_prevent(&ccb->csio,
2082 /*retries*/1,
2083 /*cbcfp*/dadone,
2084 MSG_SIMPLE_Q_TAG,
2085 action,
2086 SSD_FULL_SIZE,
2087 5000);
2088
3f499af5
PA
2089 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
2090 SF_RETRY_UA, &softc->device_stats);
984263bc
MD
2091
2092 if (error == 0) {
2093 if (action == PR_ALLOW)
2094 softc->flags &= ~DA_FLAG_PACK_LOCKED;
2095 else
2096 softc->flags |= DA_FLAG_PACK_LOCKED;
2097 }
2098
2099 xpt_release_ccb(ccb);
2100}
2101
f7b26992
MD
2102/*
2103 * Check media on open, e.g. card reader devices which had no initial media.
2104 */
2105static int
2106dacheckmedia(struct cam_periph *periph)
2107{
2108 struct disk_params *dp;
2109 struct da_softc *softc;
2110 struct disk_info info;
2111 int error;
aec00d32 2112 int mute;
f7b26992
MD
2113
2114 softc = (struct da_softc *)periph->softc;
2115 dp = &softc->params;
2116
aec00d32
MD
2117 if (softc->flags & DA_FLAG_CAP_MUTE) /* additional ccb flags */
2118 mute = CAM_QUIET;
2119 else
2120 mute = 0;
2121
2122 error = dagetcapacity(periph, mute);
f7b26992
MD
2123
2124 /*
2125 * Only reprobe on initial open and if the media is removable.
9670bdda
MD
2126 *
2127 * NOTE: If we setdiskinfo() it will take the device probe
2128 * a bit of time to probe the slices and partitions,
2129 * and mess up booting. So avoid if nothing has changed.
2130 * XXX
f7b26992
MD
2131 */
2132 if (softc->flags & DA_FLAG_OPEN)
2133 return (error);
2134 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) == 0)
2135 return (error);
2136
2137 bzero(&info, sizeof(info));
2138 info.d_type = DTYPE_SCSI;
2139 info.d_serialno = xpt_path_serialno(periph->path);
2140
2141 if (error == 0) {
f7b26992
MD
2142 CAM_SIM_UNLOCK(periph->sim);
2143 info.d_media_blksize = softc->params.secsize;
2144 info.d_media_blocks = softc->params.sectors;
2145 info.d_media_size = 0;
2146 info.d_secpertrack = softc->params.secs_per_track;
2147 info.d_nheads = softc->params.heads;
2148 info.d_ncylinders = softc->params.cylinders;
2149 info.d_secpercyl = softc->params.heads *
2150 softc->params.secs_per_track;
2151 info.d_serialno = xpt_path_serialno(periph->path);
9670bdda
MD
2152 if (info.d_media_blocks != softc->disk.d_info.d_media_blocks) {
2153 kprintf("%s%d: open removable media: "
2154 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)\n",
2155 periph->periph_name, periph->unit_number,
2156 (uintmax_t)(((uintmax_t)dp->secsize *
2157 dp->sectors) / (1024*1024)),
2158 (uintmax_t)dp->sectors, dp->secsize,
2159 dp->heads, dp->secs_per_track, dp->cylinders);
2160 disk_setdiskinfo(&softc->disk, &info);
2161 }
f7b26992
MD
2162 CAM_SIM_LOCK(periph->sim);
2163 } else {
aec00d32
MD
2164 if (!mute || bootverbose) {
2165 kprintf("%s%d: open removable media: "
2166 "no media present\n",
2167 periph->periph_name, periph->unit_number);
2168 }
f7b26992
MD
2169 info.d_media_blksize = 512;
2170 disk_setdiskinfo(&softc->disk, &info);
2171 }
2172 return (error);
2173}
2174
bdd58e03 2175static int
aec00d32 2176dagetcapacity(struct cam_periph *periph, int ccbflags)
bdd58e03
MD
2177{
2178 struct da_softc *softc;
2179 union ccb *ccb;
2180 struct scsi_read_capacity_data *rcap;
0b0362e1 2181 struct scsi_read_capacity_data_16 *rcaplong;
bdd58e03
MD
2182 uint32_t block_len;
2183 uint64_t maxsector;
2184 int error;
2185
2186 softc = (struct da_softc *)periph->softc;
2187 block_len = 0;
2188 maxsector = 0;
2189 error = 0;
2190
2191 /* Do a read capacity */
1c8b7a9a
PA
2192 rcap = (struct scsi_read_capacity_data *)kmalloc(sizeof(*rcaplong),
2193 M_SCSIDA, M_INTWAIT);
bdd58e03
MD
2194
2195 ccb = cam_periph_getccb(periph, /*priority*/1);
a9f09b75
MD
2196 ccb->ccb_h.ccb_state = DA_CCB_POLLED;
2197
bdd58e03
MD
2198 scsi_read_capacity(&ccb->csio,
2199 /*retries*/4,
2200 /*cbfncp*/dadone,
2201 MSG_SIMPLE_Q_TAG,
2202 rcap,
2203 SSD_FULL_SIZE,
2204 /*timeout*/60000);
2205 ccb->ccb_h.ccb_bio = NULL;
aec00d32 2206 ccb->ccb_h.flags |= ccbflags;
bdd58e03
MD
2207
2208 error = cam_periph_runccb(ccb, daerror,
b05e84c9 2209 /*cam_flags*/CAM_RETRY_SELTO,
bdd58e03
MD
2210 /*sense_flags*/SF_RETRY_UA,
2211 &softc->device_stats);
2212
2213 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2214 cam_release_devq(ccb->ccb_h.path,
2215 /*relsim_flags*/0,
2216 /*reduction*/0,
2217 /*timeout*/0,
2218 /*getcount_only*/0);
2219
2220 if (error == 0) {
2221 block_len = scsi_4btoul(rcap->length);
2222 maxsector = scsi_4btoul(rcap->addr);
2223
2224 if (maxsector != 0xffffffff)
2225 goto done;
2226 } else
2227 goto done;
2228
0b0362e1 2229 rcaplong = (struct scsi_read_capacity_data_16 *)rcap;
bdd58e03
MD
2230
2231 scsi_read_capacity_16(&ccb->csio,
2232 /*retries*/ 4,
2233 /*cbfcnp*/ dadone,
2234 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2235 /*lba*/ 0,
2236 /*reladr*/ 0,
2237 /*pmi*/ 0,
2238 rcaplong,
2239 /*sense_len*/ SSD_FULL_SIZE,
2240 /*timeout*/ 60000);
2241 ccb->ccb_h.ccb_bio = NULL;
2242
2243 error = cam_periph_runccb(ccb, daerror,
b05e84c9 2244 /*cam_flags*/CAM_RETRY_SELTO,
bdd58e03
MD
2245 /*sense_flags*/SF_RETRY_UA,
2246 &softc->device_stats);
2247
2248 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2249 cam_release_devq(ccb->ccb_h.path,
2250 /*relsim_flags*/0,
2251 /*reduction*/0,
2252 /*timeout*/0,
2253 /*getcount_only*/0);
2254
2255 if (error == 0) {
2256 block_len = scsi_4btoul(rcaplong->length);
2257 maxsector = scsi_8btou64(rcaplong->addr);
2258 }
2259
2260done:
2261
2262 if (error == 0)
2263 dasetgeom(periph, block_len, maxsector);
2264
2265 xpt_release_ccb(ccb);
2266
1c8b7a9a 2267 kfree(rcap, M_SCSIDA);
bdd58e03
MD
2268
2269 return (error);
2270}
2271
984263bc 2272static void
bdd58e03 2273dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
984263bc 2274{
cec957e9 2275 struct ccb_calc_geometry *ccg;
984263bc
MD
2276 struct da_softc *softc;
2277 struct disk_params *dp;
2278
2279 softc = (struct da_softc *)periph->softc;
2280
2281 dp = &softc->params;
bdd58e03
MD
2282 dp->secsize = block_len;
2283 dp->sectors = maxsector + 1;
984263bc
MD
2284 /*
2285 * Have the controller provide us with a geometry
2286 * for this disk. The only time the geometry
2287 * matters is when we boot and the controller
2288 * is the only one knowledgeable enough to come
2289 * up with something that will make this a bootable
2290 * device.
2291 */
cec957e9
MD
2292 ccg = &xpt_alloc_ccb()->ccg;
2293 xpt_setup_ccb(&ccg->ccb_h, periph->path, /*priority*/1);
2294 ccg->ccb_h.func_code = XPT_CALC_GEOMETRY;
2295 ccg->block_size = dp->secsize;
2296 ccg->volume_size = dp->sectors;
2297 ccg->heads = 0;
2298 ccg->secs_per_track = 0;
2299 ccg->cylinders = 0;
2300 xpt_action((union ccb*)ccg);
2301 if ((ccg->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
eac73adf
PA
2302 /*
2303 * We don't know what went wrong here- but just pick
2304 * a geometry so we don't have nasty things like divide
2305 * by zero.
2306 */
2307 dp->heads = 255;
2308 dp->secs_per_track = 255;
2309 dp->cylinders = dp->sectors / (255 * 255);
2310 if (dp->cylinders == 0) {
2311 dp->cylinders = 1;
2312 }
2313 } else {
cec957e9
MD
2314 dp->heads = ccg->heads;
2315 dp->secs_per_track = ccg->secs_per_track;
2316 dp->cylinders = ccg->cylinders;
eac73adf 2317 }
cec957e9 2318 xpt_free_ccb(&ccg->ccb_h);
984263bc
MD
2319}
2320
984263bc
MD
2321/*
2322 * Step through all DA peripheral drivers, and if the device is still open,
2323 * sync the disk cache to physical media.
2324 */
2325static void
2326dashutdown(void * arg, int howto)
2327{
2328 struct cam_periph *periph;
2329 struct da_softc *softc;
2330
234289a4 2331 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
cec957e9 2332 union ccb *ccb;
234289a4 2333
1c8b7a9a 2334 cam_periph_lock(periph);
984263bc
MD
2335 softc = (struct da_softc *)periph->softc;
2336
2337 /*
2338 * We only sync the cache if the drive is still open, and
2339 * if the drive is capable of it..
2340 */
2341 if (((softc->flags & DA_FLAG_OPEN) == 0)
1c8b7a9a
PA
2342 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
2343 cam_periph_unlock(periph);
984263bc 2344 continue;
1c8b7a9a 2345 }
984263bc 2346
cec957e9
MD
2347 ccb = xpt_alloc_ccb();
2348 xpt_setup_ccb(&ccb->ccb_h, periph->path, /*priority*/1);
984263bc 2349
cec957e9
MD
2350 ccb->ccb_h.ccb_state = DA_CCB_DUMP;
2351 scsi_synchronize_cache(&ccb->csio,
984263bc
MD
2352 /*retries*/1,
2353 /*cbfcnp*/dadone,
2354 MSG_SIMPLE_Q_TAG,
2355 /*begin_lba*/0, /* whole disk */
2356 /*lb_count*/0,
2357 SSD_FULL_SIZE,
19a136fb 2358 60 * 60 * 1000);
984263bc 2359
cec957e9 2360 xpt_polled_action(ccb);
984263bc 2361
cec957e9
MD
2362 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2363 if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
984263bc 2364 CAM_SCSI_STATUS_ERROR)
cec957e9 2365 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)){
984263bc
MD
2366 int error_code, sense_key, asc, ascq;
2367
cec957e9 2368 scsi_extract_sense(&ccb->csio.sense_data,
984263bc
MD
2369 &error_code, &sense_key,
2370 &asc, &ascq);
2371
2372 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
cec957e9 2373 scsi_sense_print(&ccb->csio);
984263bc 2374 } else {
1c8b7a9a
PA
2375 xpt_print(periph->path, "Synchronize "
2376 "cache failed, status == 0x%x, scsi status "
cec957e9
MD
2377 "== 0x%x\n", ccb->ccb_h.status,
2378 ccb->csio.scsi_status);
984263bc
MD
2379 }
2380 }
2381
cec957e9
MD
2382 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2383 cam_release_devq(ccb->ccb_h.path,
984263bc
MD
2384 /*relsim_flags*/0,
2385 /*reduction*/0,
2386 /*timeout*/0,
2387 /*getcount_only*/0);
2388
1c8b7a9a 2389 cam_periph_unlock(periph);
cec957e9 2390 xpt_free_ccb(&ccb->ccb_h);
984263bc
MD
2391 }
2392}
2393
2394#else /* !_KERNEL */
2395
2396/*
2397 * XXX This is only left out of the kernel build to silence warnings. If,
2398 * for some reason this function is used in the kernel, the ifdefs should
2399 * be moved so it is included both in the kernel and userland.
2400 */
2401void
2402scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2403 void (*cbfcnp)(struct cam_periph *, union ccb *),
2404 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2405 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2406 u_int32_t timeout)
2407{
2408 struct scsi_format_unit *scsi_cmd;
2409
2410 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2411 scsi_cmd->opcode = FORMAT_UNIT;
2412 scsi_cmd->byte2 = byte2;
2413 scsi_ulto2b(ileave, scsi_cmd->interleave);
2414
2415 cam_fill_csio(csio,
2416 retries,
2417 cbfcnp,
2418 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2419 tag_action,
2420 data_ptr,
2421 dxfer_len,
2422 sense_len,
2423 sizeof(*scsi_cmd),
2424 timeout);
2425}
2426
2427#endif /* _KERNEL */