| 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 | * |
| 28 | * $FreeBSD: src/sys/cam/scsi/scsi_da.c,v 1.42.2.46 2003/10/21 22:18:19 thomas Exp $ |
| 29 | * $DragonFly: src/sys/bus/cam/scsi/scsi_da.c,v 1.32 2006/09/10 01:26:32 dillon Exp $ |
| 30 | */ |
| 31 | |
| 32 | #ifdef _KERNEL |
| 33 | #include "opt_hw_wdog.h" |
| 34 | #endif /* _KERNEL */ |
| 35 | |
| 36 | #include <sys/param.h> |
| 37 | #include <sys/bootmaj.h> |
| 38 | |
| 39 | #ifdef _KERNEL |
| 40 | #include <sys/systm.h> |
| 41 | #include <sys/kernel.h> |
| 42 | #include <sys/buf.h> |
| 43 | #include <sys/sysctl.h> |
| 44 | #include <sys/taskqueue.h> |
| 45 | #endif /* _KERNEL */ |
| 46 | |
| 47 | #include <sys/devicestat.h> |
| 48 | #include <sys/conf.h> |
| 49 | #ifdef _KERNEL |
| 50 | #include <sys/disk.h> |
| 51 | #include <sys/eventhandler.h> |
| 52 | #include <sys/malloc.h> |
| 53 | #include <sys/cons.h> |
| 54 | #include <sys/proc.h> |
| 55 | #endif |
| 56 | #include <sys/buf2.h> |
| 57 | #ifdef _KERNEL |
| 58 | #include <sys/thread2.h> |
| 59 | #endif |
| 60 | |
| 61 | #include <machine/md_var.h> |
| 62 | |
| 63 | #ifdef _KERNEL |
| 64 | #include <vm/vm.h> |
| 65 | #include <vm/pmap.h> |
| 66 | #endif |
| 67 | |
| 68 | #ifndef _KERNEL |
| 69 | #include <stdio.h> |
| 70 | #include <string.h> |
| 71 | #endif /* _KERNEL */ |
| 72 | |
| 73 | #include "../cam.h" |
| 74 | #include "../cam_ccb.h" |
| 75 | #include "../cam_extend.h" |
| 76 | #include "../cam_periph.h" |
| 77 | #include "../cam_xpt_periph.h" |
| 78 | |
| 79 | #include "scsi_message.h" |
| 80 | |
| 81 | #ifndef _KERNEL |
| 82 | #include "scsi_da.h" |
| 83 | #endif /* !_KERNEL */ |
| 84 | |
| 85 | #ifdef _KERNEL |
| 86 | typedef enum { |
| 87 | DA_STATE_PROBE, |
| 88 | DA_STATE_NORMAL |
| 89 | } da_state; |
| 90 | |
| 91 | typedef enum { |
| 92 | DA_FLAG_PACK_INVALID = 0x001, |
| 93 | DA_FLAG_NEW_PACK = 0x002, |
| 94 | DA_FLAG_PACK_LOCKED = 0x004, |
| 95 | DA_FLAG_PACK_REMOVABLE = 0x008, |
| 96 | DA_FLAG_TAGGED_QUEUING = 0x010, |
| 97 | DA_FLAG_NEED_OTAG = 0x020, |
| 98 | DA_FLAG_WENT_IDLE = 0x040, |
| 99 | DA_FLAG_RETRY_UA = 0x080, |
| 100 | DA_FLAG_OPEN = 0x100, |
| 101 | DA_FLAG_SCTX_INIT = 0x200 |
| 102 | } da_flags; |
| 103 | |
| 104 | typedef enum { |
| 105 | DA_Q_NONE = 0x00, |
| 106 | DA_Q_NO_SYNC_CACHE = 0x01, |
| 107 | DA_Q_NO_6_BYTE = 0x02, |
| 108 | DA_Q_NO_PREVENT = 0x04 |
| 109 | } da_quirks; |
| 110 | |
| 111 | typedef enum { |
| 112 | DA_CCB_PROBE = 0x01, |
| 113 | DA_CCB_BUFFER_IO = 0x02, |
| 114 | DA_CCB_WAITING = 0x03, |
| 115 | DA_CCB_DUMP = 0x04, |
| 116 | DA_CCB_TYPE_MASK = 0x0F, |
| 117 | DA_CCB_RETRY_UA = 0x10 |
| 118 | } da_ccb_state; |
| 119 | |
| 120 | /* Offsets into our private area for storing information */ |
| 121 | #define ccb_state ppriv_field0 |
| 122 | #define ccb_bio ppriv_ptr1 |
| 123 | |
| 124 | struct disk_params { |
| 125 | u_int8_t heads; |
| 126 | u_int16_t cylinders; |
| 127 | u_int8_t secs_per_track; |
| 128 | u_int32_t secsize; /* Number of bytes/sector */ |
| 129 | u_int32_t sectors; /* total number sectors */ |
| 130 | }; |
| 131 | |
| 132 | struct da_softc { |
| 133 | struct bio_queue_head bio_queue; |
| 134 | struct devstat device_stats; |
| 135 | SLIST_ENTRY(da_softc) links; |
| 136 | LIST_HEAD(, ccb_hdr) pending_ccbs; |
| 137 | da_state state; |
| 138 | da_flags flags; |
| 139 | da_quirks quirks; |
| 140 | int minimum_cmd_size; |
| 141 | int ordered_tag_count; |
| 142 | struct disk_params params; |
| 143 | struct disk disk; |
| 144 | union ccb saved_ccb; |
| 145 | struct task sysctl_task; |
| 146 | struct sysctl_ctx_list sysctl_ctx; |
| 147 | struct sysctl_oid *sysctl_tree; |
| 148 | }; |
| 149 | |
| 150 | struct da_quirk_entry { |
| 151 | struct scsi_inquiry_pattern inq_pat; |
| 152 | da_quirks quirks; |
| 153 | }; |
| 154 | |
| 155 | static const char quantum[] = "QUANTUM"; |
| 156 | static const char microp[] = "MICROP"; |
| 157 | |
| 158 | static struct da_quirk_entry da_quirk_table[] = |
| 159 | { |
| 160 | /* SPI, FC devices */ |
| 161 | { |
| 162 | /* |
| 163 | * Fujitsu M2513A MO drives. |
| 164 | * Tested devices: M2513A2 firmware versions 1200 & 1300. |
| 165 | * (dip switch selects whether T_DIRECT or T_OPTICAL device) |
| 166 | * Reported by: W.Scholten <whs@xs4all.nl> |
| 167 | */ |
| 168 | {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"}, |
| 169 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 170 | }, |
| 171 | { |
| 172 | /* See above. */ |
| 173 | {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"}, |
| 174 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 175 | }, |
| 176 | { |
| 177 | /* |
| 178 | * This particular Fujitsu drive doesn't like the |
| 179 | * synchronize cache command. |
| 180 | * Reported by: Tom Jackson <toj@gorilla.net> |
| 181 | */ |
| 182 | {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"}, |
| 183 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 184 | |
| 185 | }, |
| 186 | { |
| 187 | /* |
| 188 | * This drive doesn't like the synchronize cache command |
| 189 | * either. Reported by: Matthew Jacob <mjacob@feral.com> |
| 190 | * in NetBSD PR kern/6027, August 24, 1998. |
| 191 | */ |
| 192 | {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"}, |
| 193 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 194 | }, |
| 195 | { |
| 196 | /* |
| 197 | * This drive doesn't like the synchronize cache command |
| 198 | * either. Reported by: Hellmuth Michaelis (hm@kts.org) |
| 199 | * (PR 8882). |
| 200 | */ |
| 201 | {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"}, |
| 202 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 203 | }, |
| 204 | { |
| 205 | /* |
| 206 | * Doesn't like the synchronize cache command. |
| 207 | * Reported by: Blaz Zupan <blaz@gold.amis.net> |
| 208 | */ |
| 209 | {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"}, |
| 210 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 211 | }, |
| 212 | { |
| 213 | /* |
| 214 | * Doesn't like the synchronize cache command. |
| 215 | */ |
| 216 | {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"}, |
| 217 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 218 | }, |
| 219 | { |
| 220 | /* |
| 221 | * Doesn't like the synchronize cache command. |
| 222 | */ |
| 223 | {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"}, |
| 224 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 225 | }, |
| 226 | { |
| 227 | /* |
| 228 | * Doesn't work correctly with 6 byte reads/writes. |
| 229 | * Returns illegal request, and points to byte 9 of the |
| 230 | * 6-byte CDB. |
| 231 | * Reported by: Adam McDougall <bsdx@spawnet.com> |
| 232 | */ |
| 233 | {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"}, |
| 234 | /*quirks*/ DA_Q_NO_6_BYTE |
| 235 | }, |
| 236 | { |
| 237 | /* See above. */ |
| 238 | {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"}, |
| 239 | /*quirks*/ DA_Q_NO_6_BYTE |
| 240 | }, |
| 241 | /* XXX USB floppy quirks temporarily enabled for 4.9R */ |
| 242 | /* USB floppy devices supported by umass(4) */ |
| 243 | { |
| 244 | /* |
| 245 | * This USB floppy drive uses the UFI command set. This |
| 246 | * command set is a derivative of the ATAPI command set and |
| 247 | * does not support READ_6 commands only READ_10. It also does |
| 248 | * not support sync cache (0x35). |
| 249 | */ |
| 250 | {T_DIRECT, SIP_MEDIA_REMOVABLE, "Y-E DATA", "USB-FDU", "*"}, |
| 251 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 252 | }, |
| 253 | { |
| 254 | /* Another USB floppy */ |
| 255 | {T_DIRECT, SIP_MEDIA_REMOVABLE, "MATSHITA", "FDD CF-VFDU*","*"}, |
| 256 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 257 | }, |
| 258 | { |
| 259 | /* |
| 260 | * The vendor, product and version strings coming from the |
| 261 | * controller are null terminated instead of being padded with |
| 262 | * spaces. The trailing wildcard character '*' is required. |
| 263 | */ |
| 264 | {T_DIRECT, SIP_MEDIA_REMOVABLE, "SMSC*", "USB FDC*","*"}, |
| 265 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 266 | }, |
| 267 | { |
| 268 | /* |
| 269 | * SmartDisk (Mitsumi) USB floppy drive |
| 270 | * PR: kern/50226 |
| 271 | */ |
| 272 | {T_DIRECT, SIP_MEDIA_REMOVABLE, "MITSUMI", "USB FDD", "*"}, |
| 273 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 274 | }, |
| 275 | { |
| 276 | /* |
| 277 | * The CISS RAID controllers do not support SYNC_CACHE |
| 278 | */ |
| 279 | {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"}, |
| 280 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 281 | }, |
| 282 | #ifdef DA_OLD_QUIRKS |
| 283 | /* USB mass storage devices supported by umass(4) */ |
| 284 | { |
| 285 | /* |
| 286 | * Sony Memory Stick adapter MSAC-US1 and |
| 287 | * Sony PCG-C1VJ Internal Memory Stick Slot (MSC-U01). |
| 288 | * Make all sony MS* products use this quirk. |
| 289 | */ |
| 290 | {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "MS*", "*"}, |
| 291 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 292 | }, |
| 293 | { |
| 294 | /* |
| 295 | * Sony Memory Stick adapter for the CLIE series |
| 296 | * of PalmOS PDA's |
| 297 | */ |
| 298 | {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "CLIE*", "*"}, |
| 299 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 300 | }, |
| 301 | { |
| 302 | /* |
| 303 | * Intelligent Stick USB disk-on-key |
| 304 | * PR: kern/53005 |
| 305 | */ |
| 306 | {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB Card", |
| 307 | "IntelligentStick*", "*"}, |
| 308 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 309 | }, |
| 310 | { |
| 311 | /* |
| 312 | * Sony DSC cameras (DSC-S30, DSC-S50, DSC-S70) |
| 313 | */ |
| 314 | {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"}, |
| 315 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 316 | }, |
| 317 | { |
| 318 | /* |
| 319 | * Microtech USB CameraMate |
| 320 | */ |
| 321 | {T_DIRECT, SIP_MEDIA_REMOVABLE, "eUSB Compact*", |
| 322 | "Compact Flash*", "*"}, |
| 323 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 324 | }, |
| 325 | { |
| 326 | /* |
| 327 | * Olympus digital cameras (C-3040ZOOM, C-2040ZOOM, C-1) |
| 328 | */ |
| 329 | {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "C-*", "*"}, |
| 330 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 331 | }, |
| 332 | { |
| 333 | /* |
| 334 | * Olympus digital cameras (E-100RS, E-10). |
| 335 | */ |
| 336 | {T_DIRECT, SIP_MEDIA_REMOVABLE, "OLYMPUS", "E-*", "*"}, |
| 337 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 338 | }, |
| 339 | { |
| 340 | /* |
| 341 | * KingByte Pen Drives |
| 342 | */ |
| 343 | {T_DIRECT, SIP_MEDIA_REMOVABLE, "NO BRAND", "PEN DRIVE", "*"}, |
| 344 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 345 | }, |
| 346 | { |
| 347 | /* |
| 348 | * iRiver iFP MP3 player (with UMS Firmware) |
| 349 | * PR: kern/54881, i386/63941, kern/66124 |
| 350 | */ |
| 351 | {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"}, |
| 352 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 353 | }, |
| 354 | { |
| 355 | /* |
| 356 | * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01 |
| 357 | * PR: kern/70158 |
| 358 | */ |
| 359 | {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "NexIA+*", "*"}, |
| 360 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 361 | }, |
| 362 | { |
| 363 | /* |
| 364 | * FujiFilm Camera |
| 365 | */ |
| 366 | {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJIFILMUSB-DRIVEUNIT", |
| 367 | "USB-DRIVEUNIT", "*"}, |
| 368 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 369 | }, |
| 370 | { |
| 371 | /* |
| 372 | * Minolta Dimage E203 |
| 373 | */ |
| 374 | {T_DIRECT, SIP_MEDIA_REMOVABLE, "MINOLTA", "DiMAGE E203", "*"}, |
| 375 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 376 | }, |
| 377 | { |
| 378 | /* |
| 379 | * Apacer HandyDrive |
| 380 | * PR: kern/43627 |
| 381 | */ |
| 382 | {T_DIRECT, SIP_MEDIA_REMOVABLE, "Apacer", "HandyDrive", "*"}, |
| 383 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 384 | }, |
| 385 | { |
| 386 | /* |
| 387 | * Daisy Technology PhotoClip on Zoran chip |
| 388 | * PR: kern/43580 |
| 389 | */ |
| 390 | {T_DIRECT, SIP_MEDIA_REMOVABLE, "ZORAN", "COACH", "*"}, |
| 391 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 392 | }, |
| 393 | { |
| 394 | /* |
| 395 | * Sony USB Key-Storage |
| 396 | * PR: kern/46386 |
| 397 | */ |
| 398 | {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Storage Media", "*"}, |
| 399 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 400 | }, |
| 401 | #endif /* DA_OLD_QUIRKS */ |
| 402 | { |
| 403 | /* |
| 404 | * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player |
| 405 | * PR: kern/51675 |
| 406 | */ |
| 407 | {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"}, |
| 408 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 409 | }, |
| 410 | { |
| 411 | /* |
| 412 | * Jungsoft NEXDISK USB flash key |
| 413 | * PR: kern/54737 |
| 414 | */ |
| 415 | {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"}, |
| 416 | /*quirks*/ DA_Q_NO_SYNC_CACHE |
| 417 | }, |
| 418 | { |
| 419 | /* |
| 420 | * Creative Nomad MUVO mp3 player (USB) |
| 421 | * PR: kern/53094 |
| 422 | */ |
| 423 | {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"}, |
| 424 | /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT |
| 425 | }, |
| 426 | }; |
| 427 | |
| 428 | static d_open_t daopen; |
| 429 | static d_close_t daclose; |
| 430 | static d_strategy_t dastrategy; |
| 431 | static d_ioctl_t daioctl; |
| 432 | static d_dump_t dadump; |
| 433 | static periph_init_t dainit; |
| 434 | static void daasync(void *callback_arg, u_int32_t code, |
| 435 | struct cam_path *path, void *arg); |
| 436 | static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS); |
| 437 | static periph_ctor_t daregister; |
| 438 | static periph_dtor_t dacleanup; |
| 439 | static periph_start_t dastart; |
| 440 | static periph_oninv_t daoninvalidate; |
| 441 | static void dadone(struct cam_periph *periph, |
| 442 | union ccb *done_ccb); |
| 443 | static int daerror(union ccb *ccb, u_int32_t cam_flags, |
| 444 | u_int32_t sense_flags); |
| 445 | static void daprevent(struct cam_periph *periph, int action); |
| 446 | static void dasetgeom(struct cam_periph *periph, |
| 447 | struct scsi_read_capacity_data * rdcap); |
| 448 | static timeout_t dasendorderedtag; |
| 449 | static void dashutdown(void *arg, int howto); |
| 450 | |
| 451 | #ifndef DA_DEFAULT_TIMEOUT |
| 452 | #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */ |
| 453 | #endif |
| 454 | |
| 455 | #ifndef DA_DEFAULT_RETRY |
| 456 | #define DA_DEFAULT_RETRY 4 |
| 457 | #endif |
| 458 | |
| 459 | static int da_retry_count = DA_DEFAULT_RETRY; |
| 460 | static int da_default_timeout = DA_DEFAULT_TIMEOUT; |
| 461 | static struct callout dasendorderedtag_ch; |
| 462 | |
| 463 | SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0, |
| 464 | "CAM Direct Access Disk driver"); |
| 465 | SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW, |
| 466 | &da_retry_count, 0, "Normal I/O retry count"); |
| 467 | TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count); |
| 468 | SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW, |
| 469 | &da_default_timeout, 0, "Normal I/O timeout (in seconds)"); |
| 470 | TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout); |
| 471 | |
| 472 | /* |
| 473 | * DA_ORDEREDTAG_INTERVAL determines how often, relative |
| 474 | * to the default timeout, we check to see whether an ordered |
| 475 | * tagged transaction is appropriate to prevent simple tag |
| 476 | * starvation. Since we'd like to ensure that there is at least |
| 477 | * 1/2 of the timeout length left for a starved transaction to |
| 478 | * complete after we've sent an ordered tag, we must poll at least |
| 479 | * four times in every timeout period. This takes care of the worst |
| 480 | * case where a starved transaction starts during an interval that |
| 481 | * meets the requirement "don't send an ordered tag" test so it takes |
| 482 | * us two intervals to determine that a tag must be sent. |
| 483 | */ |
| 484 | #ifndef DA_ORDEREDTAG_INTERVAL |
| 485 | #define DA_ORDEREDTAG_INTERVAL 4 |
| 486 | #endif |
| 487 | |
| 488 | static struct periph_driver dadriver = |
| 489 | { |
| 490 | dainit, "da", |
| 491 | TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0 |
| 492 | }; |
| 493 | |
| 494 | DATA_SET(periphdriver_set, dadriver); |
| 495 | |
| 496 | /* For 2.2-stable support */ |
| 497 | #ifndef D_DISK |
| 498 | #define D_DISK 0 |
| 499 | #endif |
| 500 | |
| 501 | static struct dev_ops da_ops = { |
| 502 | { "da", DA_CDEV_MAJOR, D_DISK }, |
| 503 | .d_open = daopen, |
| 504 | .d_close = daclose, |
| 505 | .d_read = physread, |
| 506 | .d_write = physwrite, |
| 507 | .d_ioctl = daioctl, |
| 508 | .d_strategy = dastrategy, |
| 509 | .d_dump = dadump |
| 510 | }; |
| 511 | |
| 512 | static SLIST_HEAD(,da_softc) softc_list; |
| 513 | static struct extend_array *daperiphs; |
| 514 | |
| 515 | static int |
| 516 | daopen(struct dev_open_args *ap) |
| 517 | { |
| 518 | cdev_t dev = ap->a_head.a_dev; |
| 519 | struct cam_periph *periph; |
| 520 | struct da_softc *softc; |
| 521 | struct disklabel *label; |
| 522 | int unit; |
| 523 | int part; |
| 524 | int error; |
| 525 | |
| 526 | unit = dkunit(dev); |
| 527 | part = dkpart(dev); |
| 528 | periph = cam_extend_get(daperiphs, unit); |
| 529 | if (periph == NULL) |
| 530 | return (ENXIO); |
| 531 | |
| 532 | softc = (struct da_softc *)periph->softc; |
| 533 | |
| 534 | CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, |
| 535 | ("daopen: dev=%s (unit %d , partition %d)\n", devtoname(dev), |
| 536 | unit, part)); |
| 537 | |
| 538 | if ((error = cam_periph_lock(periph, PCATCH)) != 0) { |
| 539 | return (error); /* error code from tsleep */ |
| 540 | } |
| 541 | |
| 542 | if (cam_periph_acquire(periph) != CAM_REQ_CMP) |
| 543 | return(ENXIO); |
| 544 | softc->flags |= DA_FLAG_OPEN; |
| 545 | |
| 546 | crit_enter(); |
| 547 | if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) { |
| 548 | /* Invalidate our pack information. */ |
| 549 | disk_invalidate(&softc->disk); |
| 550 | softc->flags &= ~DA_FLAG_PACK_INVALID; |
| 551 | } |
| 552 | crit_exit(); |
| 553 | |
| 554 | /* Do a read capacity */ |
| 555 | { |
| 556 | struct scsi_read_capacity_data *rcap; |
| 557 | union ccb *ccb; |
| 558 | |
| 559 | rcap = kmalloc(sizeof(*rcap), M_TEMP, M_INTWAIT | M_ZERO); |
| 560 | |
| 561 | ccb = cam_periph_getccb(periph, /*priority*/1); |
| 562 | scsi_read_capacity(&ccb->csio, |
| 563 | /*retries*/1, |
| 564 | /*cbfncp*/dadone, |
| 565 | MSG_SIMPLE_Q_TAG, |
| 566 | rcap, |
| 567 | SSD_FULL_SIZE, |
| 568 | /*timeout*/60000); |
| 569 | ccb->ccb_h.ccb_bio = NULL; |
| 570 | |
| 571 | error = cam_periph_runccb(ccb, daerror, /*cam_flags*/0, |
| 572 | /*sense_flags*/SF_RETRY_UA | |
| 573 | SF_RETRY_SELTO, |
| 574 | &softc->device_stats); |
| 575 | |
| 576 | xpt_release_ccb(ccb); |
| 577 | |
| 578 | if (error == 0) { |
| 579 | dasetgeom(periph, rcap); |
| 580 | } |
| 581 | |
| 582 | kfree(rcap, M_TEMP); |
| 583 | } |
| 584 | |
| 585 | if (error == 0) { |
| 586 | struct ccb_getdev cgd; |
| 587 | |
| 588 | /* Build label for whole disk. */ |
| 589 | label = &softc->disk.d_label; |
| 590 | bzero(label, sizeof(*label)); |
| 591 | label->d_type = DTYPE_SCSI; |
| 592 | |
| 593 | /* |
| 594 | * Grab the inquiry data to get the vendor and product names. |
| 595 | * Put them in the typename and packname for the label. |
| 596 | */ |
| 597 | xpt_setup_ccb(&cgd.ccb_h, periph->path, /*priority*/ 1); |
| 598 | cgd.ccb_h.func_code = XPT_GDEV_TYPE; |
| 599 | xpt_action((union ccb *)&cgd); |
| 600 | |
| 601 | strncpy(label->d_typename, cgd.inq_data.vendor, |
| 602 | min(SID_VENDOR_SIZE, sizeof(label->d_typename))); |
| 603 | strncpy(label->d_packname, cgd.inq_data.product, |
| 604 | min(SID_PRODUCT_SIZE, sizeof(label->d_packname))); |
| 605 | |
| 606 | label->d_secsize = softc->params.secsize; |
| 607 | label->d_nsectors = softc->params.secs_per_track; |
| 608 | label->d_ntracks = softc->params.heads; |
| 609 | label->d_ncylinders = softc->params.cylinders; |
| 610 | label->d_secpercyl = softc->params.heads |
| 611 | * softc->params.secs_per_track; |
| 612 | label->d_secperunit = softc->params.sectors; |
| 613 | |
| 614 | if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 && |
| 615 | (softc->quirks & DA_Q_NO_PREVENT) == 0) |
| 616 | daprevent(periph, PR_PREVENT); |
| 617 | |
| 618 | /* |
| 619 | * Check to see whether or not the blocksize is set yet. |
| 620 | * If it isn't, set it and then clear the blocksize |
| 621 | * unavailable flag for the device statistics. |
| 622 | */ |
| 623 | if ((softc->device_stats.flags & DEVSTAT_BS_UNAVAILABLE) != 0){ |
| 624 | softc->device_stats.block_size = softc->params.secsize; |
| 625 | softc->device_stats.flags &= ~DEVSTAT_BS_UNAVAILABLE; |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | if (error != 0) { |
| 630 | if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 && |
| 631 | (softc->quirks & DA_Q_NO_PREVENT) == 0) |
| 632 | daprevent(periph, PR_ALLOW); |
| 633 | softc->flags &= ~DA_FLAG_OPEN; |
| 634 | cam_periph_release(periph); |
| 635 | } |
| 636 | cam_periph_unlock(periph); |
| 637 | return (error); |
| 638 | } |
| 639 | |
| 640 | static int |
| 641 | daclose(struct dev_close_args *ap) |
| 642 | { |
| 643 | cdev_t dev = ap->a_head.a_dev; |
| 644 | struct cam_periph *periph; |
| 645 | struct da_softc *softc; |
| 646 | int unit; |
| 647 | int error; |
| 648 | |
| 649 | unit = dkunit(dev); |
| 650 | periph = cam_extend_get(daperiphs, unit); |
| 651 | if (periph == NULL) |
| 652 | return (ENXIO); |
| 653 | |
| 654 | softc = (struct da_softc *)periph->softc; |
| 655 | |
| 656 | if ((error = cam_periph_lock(periph, 0)) != 0) { |
| 657 | return (error); /* error code from tsleep */ |
| 658 | } |
| 659 | |
| 660 | if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) { |
| 661 | union ccb *ccb; |
| 662 | |
| 663 | ccb = cam_periph_getccb(periph, /*priority*/1); |
| 664 | |
| 665 | scsi_synchronize_cache(&ccb->csio, |
| 666 | /*retries*/1, |
| 667 | /*cbfcnp*/dadone, |
| 668 | MSG_SIMPLE_Q_TAG, |
| 669 | /*begin_lba*/0,/* Cover the whole disk */ |
| 670 | /*lb_count*/0, |
| 671 | SSD_FULL_SIZE, |
| 672 | 5 * 60 * 1000); |
| 673 | |
| 674 | cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0, |
| 675 | /*sense_flags*/SF_RETRY_UA, |
| 676 | &softc->device_stats); |
| 677 | |
| 678 | if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { |
| 679 | if ((ccb->ccb_h.status & CAM_STATUS_MASK) == |
| 680 | CAM_SCSI_STATUS_ERROR) { |
| 681 | int asc, ascq; |
| 682 | int sense_key, error_code; |
| 683 | |
| 684 | scsi_extract_sense(&ccb->csio.sense_data, |
| 685 | &error_code, |
| 686 | &sense_key, |
| 687 | &asc, &ascq); |
| 688 | if (sense_key != SSD_KEY_ILLEGAL_REQUEST) |
| 689 | scsi_sense_print(&ccb->csio); |
| 690 | } else { |
| 691 | xpt_print_path(periph->path); |
| 692 | printf("Synchronize cache failed, status " |
| 693 | "== 0x%x, scsi status == 0x%x\n", |
| 694 | ccb->csio.ccb_h.status, |
| 695 | ccb->csio.scsi_status); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) |
| 700 | cam_release_devq(ccb->ccb_h.path, |
| 701 | /*relsim_flags*/0, |
| 702 | /*reduction*/0, |
| 703 | /*timeout*/0, |
| 704 | /*getcount_only*/0); |
| 705 | |
| 706 | xpt_release_ccb(ccb); |
| 707 | |
| 708 | } |
| 709 | |
| 710 | if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) { |
| 711 | if ((softc->quirks & DA_Q_NO_PREVENT) == 0) |
| 712 | daprevent(periph, PR_ALLOW); |
| 713 | /* |
| 714 | * If we've got removeable media, mark the blocksize as |
| 715 | * unavailable, since it could change when new media is |
| 716 | * inserted. |
| 717 | */ |
| 718 | softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE; |
| 719 | } |
| 720 | |
| 721 | softc->flags &= ~DA_FLAG_OPEN; |
| 722 | cam_periph_unlock(periph); |
| 723 | cam_periph_release(periph); |
| 724 | return (0); |
| 725 | } |
| 726 | |
| 727 | /* |
| 728 | * Actually translate the requested transfer into one the physical driver |
| 729 | * can understand. The transfer is described by a buf and will include |
| 730 | * only one physical transfer. |
| 731 | */ |
| 732 | static int |
| 733 | dastrategy(struct dev_strategy_args *ap) |
| 734 | { |
| 735 | cdev_t dev = ap->a_head.a_dev; |
| 736 | struct bio *bio = ap->a_bio; |
| 737 | struct buf *bp = bio->bio_buf; |
| 738 | struct cam_periph *periph; |
| 739 | struct da_softc *softc; |
| 740 | u_int unit; |
| 741 | u_int part; |
| 742 | |
| 743 | unit = dkunit(dev); |
| 744 | part = dkpart(dev); |
| 745 | periph = cam_extend_get(daperiphs, unit); |
| 746 | if (periph == NULL) { |
| 747 | bp->b_error = ENXIO; |
| 748 | goto bad; |
| 749 | } |
| 750 | softc = (struct da_softc *)periph->softc; |
| 751 | #if 0 |
| 752 | /* |
| 753 | * check it's not too big a transfer for our adapter |
| 754 | */ |
| 755 | scsi_minphys(bp, &sd_switch); |
| 756 | #endif |
| 757 | |
| 758 | /* |
| 759 | * Mask interrupts so that the pack cannot be invalidated until |
| 760 | * after we are in the queue. Otherwise, we might not properly |
| 761 | * clean up one of the buffers. |
| 762 | */ |
| 763 | crit_enter(); |
| 764 | |
| 765 | /* |
| 766 | * If the device has been made invalid, error out |
| 767 | */ |
| 768 | if ((softc->flags & DA_FLAG_PACK_INVALID)) { |
| 769 | crit_exit(); |
| 770 | bp->b_error = ENXIO; |
| 771 | goto bad; |
| 772 | } |
| 773 | |
| 774 | /* |
| 775 | * Place it in the queue of disk activities for this disk |
| 776 | */ |
| 777 | bioqdisksort(&softc->bio_queue, bio); |
| 778 | |
| 779 | crit_exit(); |
| 780 | |
| 781 | /* |
| 782 | * Schedule ourselves for performing the work. |
| 783 | */ |
| 784 | xpt_schedule(periph, /* XXX priority */1); |
| 785 | |
| 786 | return(0); |
| 787 | bad: |
| 788 | bp->b_flags |= B_ERROR; |
| 789 | |
| 790 | /* |
| 791 | * Correctly set the buf to indicate a completed xfer |
| 792 | */ |
| 793 | bp->b_resid = bp->b_bcount; |
| 794 | biodone(bio); |
| 795 | return(0); |
| 796 | } |
| 797 | |
| 798 | /* For 2.2-stable support */ |
| 799 | #ifndef ENOIOCTL |
| 800 | #define ENOIOCTL -1 |
| 801 | #endif |
| 802 | |
| 803 | static int |
| 804 | daioctl(struct dev_ioctl_args *ap) |
| 805 | { |
| 806 | cdev_t dev = ap->a_head.a_dev; |
| 807 | struct cam_periph *periph; |
| 808 | struct da_softc *softc; |
| 809 | int unit; |
| 810 | int error; |
| 811 | |
| 812 | unit = dkunit(dev); |
| 813 | periph = cam_extend_get(daperiphs, unit); |
| 814 | if (periph == NULL) |
| 815 | return (ENXIO); |
| 816 | |
| 817 | softc = (struct da_softc *)periph->softc; |
| 818 | |
| 819 | CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("daioctl\n")); |
| 820 | |
| 821 | if ((error = cam_periph_lock(periph, PCATCH)) != 0) { |
| 822 | return (error); /* error code from tsleep */ |
| 823 | } |
| 824 | |
| 825 | error = cam_periph_ioctl(periph, ap->a_cmd, ap->a_data, daerror); |
| 826 | |
| 827 | cam_periph_unlock(periph); |
| 828 | |
| 829 | return (error); |
| 830 | } |
| 831 | |
| 832 | static int |
| 833 | dadump(struct dev_dump_args *ap) |
| 834 | { |
| 835 | cdev_t dev = ap->a_head.a_dev; |
| 836 | struct cam_periph *periph; |
| 837 | struct da_softc *softc; |
| 838 | u_int unit; |
| 839 | long blkcnt; |
| 840 | vm_paddr_t addr; |
| 841 | struct ccb_scsiio csio; |
| 842 | int dumppages = MAXDUMPPGS; |
| 843 | int i; |
| 844 | |
| 845 | /* toss any characters present prior to dump */ |
| 846 | while (cncheckc() != -1) |
| 847 | ; |
| 848 | |
| 849 | unit = dkunit(dev); |
| 850 | periph = cam_extend_get(daperiphs, unit); |
| 851 | if (periph == NULL) { |
| 852 | return (ENXIO); |
| 853 | } |
| 854 | softc = (struct da_softc *)periph->softc; |
| 855 | |
| 856 | if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) |
| 857 | return (ENXIO); |
| 858 | |
| 859 | addr = 0; /* starting address */ |
| 860 | blkcnt = howmany(PAGE_SIZE, ap->a_secsize); |
| 861 | |
| 862 | while (ap->a_count > 0) { |
| 863 | caddr_t va = NULL; |
| 864 | |
| 865 | if ((ap->a_count / blkcnt) < dumppages) |
| 866 | dumppages = ap->a_count / blkcnt; |
| 867 | |
| 868 | for (i = 0; i < dumppages; ++i) { |
| 869 | vm_paddr_t a = addr + (i * PAGE_SIZE); |
| 870 | if (is_physical_memory(a)) |
| 871 | va = pmap_kenter_temporary(trunc_page(a), i); |
| 872 | else |
| 873 | va = pmap_kenter_temporary(trunc_page(0), i); |
| 874 | } |
| 875 | |
| 876 | xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1); |
| 877 | csio.ccb_h.ccb_state = DA_CCB_DUMP; |
| 878 | scsi_read_write(&csio, |
| 879 | /*retries*/1, |
| 880 | dadone, |
| 881 | MSG_ORDERED_Q_TAG, |
| 882 | /*read*/FALSE, |
| 883 | /*byte2*/0, |
| 884 | /*minimum_cmd_size*/ softc->minimum_cmd_size, |
| 885 | ap->a_blkno, |
| 886 | blkcnt * dumppages, |
| 887 | /*data_ptr*/(u_int8_t *) va, |
| 888 | /*dxfer_len*/blkcnt * ap->a_secsize * dumppages, |
| 889 | /*sense_len*/SSD_FULL_SIZE, |
| 890 | DA_DEFAULT_TIMEOUT * 1000); |
| 891 | xpt_polled_action((union ccb *)&csio); |
| 892 | |
| 893 | if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { |
| 894 | printf("Aborting dump due to I/O error.\n"); |
| 895 | if ((csio.ccb_h.status & CAM_STATUS_MASK) == |
| 896 | CAM_SCSI_STATUS_ERROR) |
| 897 | scsi_sense_print(&csio); |
| 898 | else |
| 899 | printf("status == 0x%x, scsi status == 0x%x\n", |
| 900 | csio.ccb_h.status, csio.scsi_status); |
| 901 | return(EIO); |
| 902 | } |
| 903 | |
| 904 | if (dumpstatus(addr, (off_t)ap->a_count * softc->params.secsize) < 0) |
| 905 | return (EINTR); |
| 906 | |
| 907 | /* update block count */ |
| 908 | ap->a_count -= blkcnt * dumppages; |
| 909 | ap->a_blkno += blkcnt * dumppages; |
| 910 | addr += PAGE_SIZE * dumppages; |
| 911 | } |
| 912 | |
| 913 | /* |
| 914 | * Sync the disk cache contents to the physical media. |
| 915 | */ |
| 916 | if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) { |
| 917 | |
| 918 | xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1); |
| 919 | csio.ccb_h.ccb_state = DA_CCB_DUMP; |
| 920 | scsi_synchronize_cache(&csio, |
| 921 | /*retries*/1, |
| 922 | /*cbfcnp*/dadone, |
| 923 | MSG_SIMPLE_Q_TAG, |
| 924 | /*begin_lba*/0,/* Cover the whole disk */ |
| 925 | /*lb_count*/0, |
| 926 | SSD_FULL_SIZE, |
| 927 | 5 * 60 * 1000); |
| 928 | xpt_polled_action((union ccb *)&csio); |
| 929 | |
| 930 | if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { |
| 931 | if ((csio.ccb_h.status & CAM_STATUS_MASK) == |
| 932 | CAM_SCSI_STATUS_ERROR) { |
| 933 | int asc, ascq; |
| 934 | int sense_key, error_code; |
| 935 | |
| 936 | scsi_extract_sense(&csio.sense_data, |
| 937 | &error_code, |
| 938 | &sense_key, |
| 939 | &asc, &ascq); |
| 940 | if (sense_key != SSD_KEY_ILLEGAL_REQUEST) |
| 941 | scsi_sense_print(&csio); |
| 942 | } else { |
| 943 | xpt_print_path(periph->path); |
| 944 | printf("Synchronize cache failed, status " |
| 945 | "== 0x%x, scsi status == 0x%x\n", |
| 946 | csio.ccb_h.status, csio.scsi_status); |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | return (0); |
| 951 | } |
| 952 | |
| 953 | static void |
| 954 | dainit(void) |
| 955 | { |
| 956 | cam_status status; |
| 957 | struct cam_path *path; |
| 958 | |
| 959 | /* |
| 960 | * Create our extend array for storing the devices we attach to. |
| 961 | */ |
| 962 | daperiphs = cam_extend_new(); |
| 963 | SLIST_INIT(&softc_list); |
| 964 | if (daperiphs == NULL) { |
| 965 | printf("da: Failed to alloc extend array!\n"); |
| 966 | return; |
| 967 | } |
| 968 | |
| 969 | callout_init(&dasendorderedtag_ch); |
| 970 | |
| 971 | /* |
| 972 | * Install a global async callback. This callback will |
| 973 | * receive async callbacks like "new device found". |
| 974 | */ |
| 975 | status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID, |
| 976 | CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); |
| 977 | |
| 978 | if (status == CAM_REQ_CMP) { |
| 979 | struct ccb_setasync csa; |
| 980 | |
| 981 | xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5); |
| 982 | csa.ccb_h.func_code = XPT_SASYNC_CB; |
| 983 | csa.event_enable = AC_FOUND_DEVICE; |
| 984 | csa.callback = daasync; |
| 985 | csa.callback_arg = NULL; |
| 986 | xpt_action((union ccb *)&csa); |
| 987 | status = csa.ccb_h.status; |
| 988 | xpt_free_path(path); |
| 989 | } |
| 990 | |
| 991 | if (status != CAM_REQ_CMP) { |
| 992 | printf("da: Failed to attach master async callback " |
| 993 | "due to status 0x%x!\n", status); |
| 994 | } else { |
| 995 | |
| 996 | /* |
| 997 | * Schedule a periodic event to occasionally send an |
| 998 | * ordered tag to a device. |
| 999 | */ |
| 1000 | callout_reset(&dasendorderedtag_ch, |
| 1001 | (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL, |
| 1002 | dasendorderedtag, NULL); |
| 1003 | |
| 1004 | /* Register our shutdown event handler */ |
| 1005 | if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown, |
| 1006 | NULL, SHUTDOWN_PRI_DEFAULT)) == NULL) |
| 1007 | printf("dainit: shutdown event registration failed!\n"); |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | static void |
| 1012 | daoninvalidate(struct cam_periph *periph) |
| 1013 | { |
| 1014 | struct da_softc *softc; |
| 1015 | struct bio *q_bio; |
| 1016 | struct buf *q_bp; |
| 1017 | struct ccb_setasync csa; |
| 1018 | |
| 1019 | softc = (struct da_softc *)periph->softc; |
| 1020 | |
| 1021 | /* |
| 1022 | * De-register any async callbacks. |
| 1023 | */ |
| 1024 | xpt_setup_ccb(&csa.ccb_h, periph->path, |
| 1025 | /* priority */ 5); |
| 1026 | csa.ccb_h.func_code = XPT_SASYNC_CB; |
| 1027 | csa.event_enable = 0; |
| 1028 | csa.callback = daasync; |
| 1029 | csa.callback_arg = periph; |
| 1030 | xpt_action((union ccb *)&csa); |
| 1031 | |
| 1032 | softc->flags |= DA_FLAG_PACK_INVALID; |
| 1033 | |
| 1034 | /* |
| 1035 | * Use a critical section to keep the buffer queue from being |
| 1036 | * modified while we traverse it. |
| 1037 | */ |
| 1038 | crit_enter(); |
| 1039 | |
| 1040 | /* |
| 1041 | * Return all queued I/O with ENXIO. |
| 1042 | * XXX Handle any transactions queued to the card |
| 1043 | * with XPT_ABORT_CCB. |
| 1044 | */ |
| 1045 | while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){ |
| 1046 | bioq_remove(&softc->bio_queue, q_bio); |
| 1047 | q_bp = q_bio->bio_buf; |
| 1048 | q_bp->b_resid = q_bp->b_bcount; |
| 1049 | q_bp->b_error = ENXIO; |
| 1050 | q_bp->b_flags |= B_ERROR; |
| 1051 | biodone(q_bio); |
| 1052 | } |
| 1053 | crit_exit(); |
| 1054 | |
| 1055 | SLIST_REMOVE(&softc_list, softc, da_softc, links); |
| 1056 | |
| 1057 | xpt_print_path(periph->path); |
| 1058 | printf("lost device\n"); |
| 1059 | } |
| 1060 | |
| 1061 | static void |
| 1062 | dacleanup(struct cam_periph *periph) |
| 1063 | { |
| 1064 | struct da_softc *softc; |
| 1065 | |
| 1066 | softc = (struct da_softc *)periph->softc; |
| 1067 | |
| 1068 | devstat_remove_entry(&softc->device_stats); |
| 1069 | cam_extend_release(daperiphs, periph->unit_number); |
| 1070 | xpt_print_path(periph->path); |
| 1071 | printf("removing device entry\n"); |
| 1072 | /* |
| 1073 | * If we can't free the sysctl tree, oh well... |
| 1074 | */ |
| 1075 | if ((softc->flags & DA_FLAG_SCTX_INIT) != 0 |
| 1076 | && sysctl_ctx_free(&softc->sysctl_ctx) != 0) { |
| 1077 | xpt_print_path(periph->path); |
| 1078 | printf("can't remove sysctl context\n"); |
| 1079 | } |
| 1080 | if (softc->disk.d_rawdev) { |
| 1081 | disk_destroy(&softc->disk); |
| 1082 | } |
| 1083 | kfree(softc, M_DEVBUF); |
| 1084 | } |
| 1085 | |
| 1086 | static void |
| 1087 | daasync(void *callback_arg, u_int32_t code, |
| 1088 | struct cam_path *path, void *arg) |
| 1089 | { |
| 1090 | struct cam_periph *periph; |
| 1091 | |
| 1092 | periph = (struct cam_periph *)callback_arg; |
| 1093 | switch (code) { |
| 1094 | case AC_FOUND_DEVICE: |
| 1095 | { |
| 1096 | struct ccb_getdev *cgd; |
| 1097 | cam_status status; |
| 1098 | |
| 1099 | cgd = (struct ccb_getdev *)arg; |
| 1100 | |
| 1101 | if (SID_TYPE(&cgd->inq_data) != T_DIRECT |
| 1102 | && SID_TYPE(&cgd->inq_data) != T_RBC |
| 1103 | && SID_TYPE(&cgd->inq_data) != T_OPTICAL) |
| 1104 | break; |
| 1105 | |
| 1106 | /* |
| 1107 | * Allocate a peripheral instance for |
| 1108 | * this device and start the probe |
| 1109 | * process. |
| 1110 | */ |
| 1111 | status = cam_periph_alloc(daregister, daoninvalidate, |
| 1112 | dacleanup, dastart, |
| 1113 | "da", CAM_PERIPH_BIO, |
| 1114 | cgd->ccb_h.path, daasync, |
| 1115 | AC_FOUND_DEVICE, cgd); |
| 1116 | |
| 1117 | if (status != CAM_REQ_CMP |
| 1118 | && status != CAM_REQ_INPROG) |
| 1119 | printf("daasync: Unable to attach to new device " |
| 1120 | "due to status 0x%x\n", status); |
| 1121 | break; |
| 1122 | } |
| 1123 | case AC_SENT_BDR: |
| 1124 | case AC_BUS_RESET: |
| 1125 | { |
| 1126 | struct da_softc *softc; |
| 1127 | struct ccb_hdr *ccbh; |
| 1128 | |
| 1129 | softc = (struct da_softc *)periph->softc; |
| 1130 | crit_enter(); |
| 1131 | /* |
| 1132 | * Don't fail on the expected unit attention |
| 1133 | * that will occur. |
| 1134 | */ |
| 1135 | softc->flags |= DA_FLAG_RETRY_UA; |
| 1136 | for (ccbh = LIST_FIRST(&softc->pending_ccbs); |
| 1137 | ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le)) |
| 1138 | ccbh->ccb_state |= DA_CCB_RETRY_UA; |
| 1139 | crit_exit(); |
| 1140 | /* FALLTHROUGH*/ |
| 1141 | } |
| 1142 | default: |
| 1143 | cam_periph_async(periph, code, path, arg); |
| 1144 | break; |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | static void |
| 1149 | dasysctlinit(void *context, int pending) |
| 1150 | { |
| 1151 | struct cam_periph *periph; |
| 1152 | struct da_softc *softc; |
| 1153 | char tmpstr[80], tmpstr2[80]; |
| 1154 | |
| 1155 | periph = (struct cam_periph *)context; |
| 1156 | softc = (struct da_softc *)periph->softc; |
| 1157 | |
| 1158 | snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number); |
| 1159 | snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number); |
| 1160 | |
| 1161 | sysctl_ctx_init(&softc->sysctl_ctx); |
| 1162 | softc->flags |= DA_FLAG_SCTX_INIT; |
| 1163 | softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, |
| 1164 | SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2, |
| 1165 | CTLFLAG_RD, 0, tmpstr); |
| 1166 | if (softc->sysctl_tree == NULL) { |
| 1167 | printf("dasysctlinit: unable to allocate sysctl tree\n"); |
| 1168 | return; |
| 1169 | } |
| 1170 | |
| 1171 | /* |
| 1172 | * Now register the sysctl handler, so the user can the value on |
| 1173 | * the fly. |
| 1174 | */ |
| 1175 | SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), |
| 1176 | OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW, |
| 1177 | &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I", |
| 1178 | "Minimum CDB size"); |
| 1179 | } |
| 1180 | |
| 1181 | static int |
| 1182 | dacmdsizesysctl(SYSCTL_HANDLER_ARGS) |
| 1183 | { |
| 1184 | int error, value; |
| 1185 | |
| 1186 | value = *(int *)arg1; |
| 1187 | |
| 1188 | error = sysctl_handle_int(oidp, &value, 0, req); |
| 1189 | |
| 1190 | if ((error != 0) |
| 1191 | || (req->newptr == NULL)) |
| 1192 | return (error); |
| 1193 | |
| 1194 | /* |
| 1195 | * Acceptable values here are 6, 10 or 12. It's possible we may |
| 1196 | * support a 16 byte minimum command size in the future, since |
| 1197 | * there are now READ(16) and WRITE(16) commands defined in the |
| 1198 | * SBC-2 spec. |
| 1199 | */ |
| 1200 | if (value < 6) |
| 1201 | value = 6; |
| 1202 | else if ((value > 6) |
| 1203 | && (value <= 10)) |
| 1204 | value = 10; |
| 1205 | else if (value > 10) |
| 1206 | value = 12; |
| 1207 | |
| 1208 | *(int *)arg1 = value; |
| 1209 | |
| 1210 | return (0); |
| 1211 | } |
| 1212 | |
| 1213 | static cam_status |
| 1214 | daregister(struct cam_periph *periph, void *arg) |
| 1215 | { |
| 1216 | struct da_softc *softc; |
| 1217 | struct ccb_setasync csa; |
| 1218 | struct ccb_pathinq cpi; |
| 1219 | struct ccb_getdev *cgd; |
| 1220 | char tmpstr[80]; |
| 1221 | caddr_t match; |
| 1222 | |
| 1223 | cgd = (struct ccb_getdev *)arg; |
| 1224 | if (periph == NULL) { |
| 1225 | printf("daregister: periph was NULL!!\n"); |
| 1226 | return(CAM_REQ_CMP_ERR); |
| 1227 | } |
| 1228 | |
| 1229 | if (cgd == NULL) { |
| 1230 | printf("daregister: no getdev CCB, can't register device\n"); |
| 1231 | return(CAM_REQ_CMP_ERR); |
| 1232 | } |
| 1233 | |
| 1234 | softc = kmalloc(sizeof(*softc), M_DEVBUF, M_INTWAIT | M_ZERO); |
| 1235 | LIST_INIT(&softc->pending_ccbs); |
| 1236 | softc->state = DA_STATE_PROBE; |
| 1237 | bioq_init(&softc->bio_queue); |
| 1238 | if (SID_IS_REMOVABLE(&cgd->inq_data)) |
| 1239 | softc->flags |= DA_FLAG_PACK_REMOVABLE; |
| 1240 | if ((cgd->inq_data.flags & SID_CmdQue) != 0) |
| 1241 | softc->flags |= DA_FLAG_TAGGED_QUEUING; |
| 1242 | |
| 1243 | periph->softc = softc; |
| 1244 | |
| 1245 | cam_extend_set(daperiphs, periph->unit_number, periph); |
| 1246 | |
| 1247 | /* |
| 1248 | * See if this device has any quirks. |
| 1249 | */ |
| 1250 | match = cam_quirkmatch((caddr_t)&cgd->inq_data, |
| 1251 | (caddr_t)da_quirk_table, |
| 1252 | sizeof(da_quirk_table)/sizeof(*da_quirk_table), |
| 1253 | sizeof(*da_quirk_table), scsi_inquiry_match); |
| 1254 | |
| 1255 | if (match != NULL) |
| 1256 | softc->quirks = ((struct da_quirk_entry *)match)->quirks; |
| 1257 | else |
| 1258 | softc->quirks = DA_Q_NONE; |
| 1259 | |
| 1260 | TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph); |
| 1261 | |
| 1262 | /* Check if the SIM does not want 6 byte commands */ |
| 1263 | xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1); |
| 1264 | cpi.ccb_h.func_code = XPT_PATH_INQ; |
| 1265 | xpt_action((union ccb *)&cpi); |
| 1266 | if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE)) |
| 1267 | softc->quirks |= DA_Q_NO_6_BYTE; |
| 1268 | |
| 1269 | /* |
| 1270 | * RBC devices don't have to support READ(6), only READ(10). |
| 1271 | */ |
| 1272 | if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC) |
| 1273 | softc->minimum_cmd_size = 10; |
| 1274 | else |
| 1275 | softc->minimum_cmd_size = 6; |
| 1276 | |
| 1277 | /* |
| 1278 | * Load the user's default, if any. |
| 1279 | */ |
| 1280 | snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size", |
| 1281 | periph->unit_number); |
| 1282 | TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size); |
| 1283 | |
| 1284 | /* |
| 1285 | * 6, 10 and 12 are the currently permissible values. |
| 1286 | */ |
| 1287 | if (softc->minimum_cmd_size < 6) |
| 1288 | softc->minimum_cmd_size = 6; |
| 1289 | else if ((softc->minimum_cmd_size > 6) |
| 1290 | && (softc->minimum_cmd_size <= 10)) |
| 1291 | softc->minimum_cmd_size = 10; |
| 1292 | else if (softc->minimum_cmd_size > 12) |
| 1293 | softc->minimum_cmd_size = 12; |
| 1294 | |
| 1295 | /* |
| 1296 | * Block our timeout handler while we |
| 1297 | * add this softc to the dev list. |
| 1298 | */ |
| 1299 | crit_enter(); |
| 1300 | SLIST_INSERT_HEAD(&softc_list, softc, links); |
| 1301 | crit_exit(); |
| 1302 | |
| 1303 | /* |
| 1304 | * The DA driver supports a blocksize, but |
| 1305 | * we don't know the blocksize until we do |
| 1306 | * a read capacity. So, set a flag to |
| 1307 | * indicate that the blocksize is |
| 1308 | * unavailable right now. We'll clear the |
| 1309 | * flag as soon as we've done a read capacity. |
| 1310 | */ |
| 1311 | devstat_add_entry(&softc->device_stats, "da", |
| 1312 | periph->unit_number, 0, |
| 1313 | DEVSTAT_BS_UNAVAILABLE, |
| 1314 | SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI, |
| 1315 | DEVSTAT_PRIORITY_DISK); |
| 1316 | |
| 1317 | /* |
| 1318 | * Register this media as a disk |
| 1319 | */ |
| 1320 | disk_create(periph->unit_number, &softc->disk, 0, &da_ops); |
| 1321 | |
| 1322 | /* |
| 1323 | * Add async callbacks for bus reset and |
| 1324 | * bus device reset calls. I don't bother |
| 1325 | * checking if this fails as, in most cases, |
| 1326 | * the system will function just fine without |
| 1327 | * them and the only alternative would be to |
| 1328 | * not attach the device on failure. |
| 1329 | */ |
| 1330 | xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5); |
| 1331 | csa.ccb_h.func_code = XPT_SASYNC_CB; |
| 1332 | csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE; |
| 1333 | csa.callback = daasync; |
| 1334 | csa.callback_arg = periph; |
| 1335 | xpt_action((union ccb *)&csa); |
| 1336 | /* |
| 1337 | * Lock this peripheral until we are setup. |
| 1338 | * This first call can't block |
| 1339 | */ |
| 1340 | cam_periph_lock(periph, 0); |
| 1341 | xpt_schedule(periph, /*priority*/5); |
| 1342 | |
| 1343 | return(CAM_REQ_CMP); |
| 1344 | } |
| 1345 | |
| 1346 | static void |
| 1347 | dastart(struct cam_periph *periph, union ccb *start_ccb) |
| 1348 | { |
| 1349 | struct da_softc *softc; |
| 1350 | |
| 1351 | softc = (struct da_softc *)periph->softc; |
| 1352 | |
| 1353 | |
| 1354 | switch (softc->state) { |
| 1355 | case DA_STATE_NORMAL: |
| 1356 | { |
| 1357 | /* Pull a buffer from the queue and get going on it */ |
| 1358 | struct bio *bio; |
| 1359 | struct buf *bp; |
| 1360 | |
| 1361 | /* |
| 1362 | * See if there is a buf with work for us to do.. |
| 1363 | */ |
| 1364 | crit_enter(); |
| 1365 | bio = bioq_first(&softc->bio_queue); |
| 1366 | if (periph->immediate_priority <= periph->pinfo.priority) { |
| 1367 | CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE, |
| 1368 | ("queuing for immediate ccb\n")); |
| 1369 | start_ccb->ccb_h.ccb_state = DA_CCB_WAITING; |
| 1370 | SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, |
| 1371 | periph_links.sle); |
| 1372 | periph->immediate_priority = CAM_PRIORITY_NONE; |
| 1373 | crit_exit(); |
| 1374 | wakeup(&periph->ccb_list); |
| 1375 | } else if (bio == NULL) { |
| 1376 | crit_exit(); |
| 1377 | xpt_release_ccb(start_ccb); |
| 1378 | } else { |
| 1379 | u_int8_t tag_code; |
| 1380 | |
| 1381 | bioq_remove(&softc->bio_queue, bio); |
| 1382 | bp = bio->bio_buf; |
| 1383 | |
| 1384 | devstat_start_transaction(&softc->device_stats); |
| 1385 | |
| 1386 | if ((bp->b_flags & B_ORDERED) != 0 |
| 1387 | || (softc->flags & DA_FLAG_NEED_OTAG) != 0) { |
| 1388 | softc->flags &= ~DA_FLAG_NEED_OTAG; |
| 1389 | softc->ordered_tag_count++; |
| 1390 | tag_code = MSG_ORDERED_Q_TAG; |
| 1391 | } else { |
| 1392 | tag_code = MSG_SIMPLE_Q_TAG; |
| 1393 | } |
| 1394 | |
| 1395 | KKASSERT(bio->bio_offset % softc->params.secsize == 0); |
| 1396 | |
| 1397 | scsi_read_write(&start_ccb->csio, |
| 1398 | /*retries*/da_retry_count, |
| 1399 | dadone, |
| 1400 | tag_code, |
| 1401 | (bp->b_cmd == BUF_CMD_READ), |
| 1402 | /*byte2*/0, |
| 1403 | softc->minimum_cmd_size, |
| 1404 | bio->bio_offset / softc->params.secsize, |
| 1405 | bp->b_bcount / softc->params.secsize, |
| 1406 | bp->b_data, |
| 1407 | bp->b_bcount, |
| 1408 | /*sense_len*/SSD_FULL_SIZE, |
| 1409 | da_default_timeout * 1000); |
| 1410 | start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO; |
| 1411 | |
| 1412 | /* |
| 1413 | * Block out any asyncronous callbacks |
| 1414 | * while we touch the pending ccb list. |
| 1415 | */ |
| 1416 | LIST_INSERT_HEAD(&softc->pending_ccbs, |
| 1417 | &start_ccb->ccb_h, periph_links.le); |
| 1418 | |
| 1419 | /* We expect a unit attention from this device */ |
| 1420 | if ((softc->flags & DA_FLAG_RETRY_UA) != 0) { |
| 1421 | start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA; |
| 1422 | softc->flags &= ~DA_FLAG_RETRY_UA; |
| 1423 | } |
| 1424 | |
| 1425 | start_ccb->ccb_h.ccb_bio = bio; |
| 1426 | bio = bioq_first(&softc->bio_queue); |
| 1427 | crit_exit(); |
| 1428 | |
| 1429 | xpt_action(start_ccb); |
| 1430 | } |
| 1431 | |
| 1432 | if (bio != NULL) { |
| 1433 | /* Have more work to do, so ensure we stay scheduled */ |
| 1434 | xpt_schedule(periph, /* XXX priority */1); |
| 1435 | } |
| 1436 | break; |
| 1437 | } |
| 1438 | case DA_STATE_PROBE: |
| 1439 | { |
| 1440 | struct ccb_scsiio *csio; |
| 1441 | struct scsi_read_capacity_data *rcap; |
| 1442 | |
| 1443 | rcap = kmalloc(sizeof(*rcap), M_TEMP, M_INTWAIT | M_ZERO); |
| 1444 | csio = &start_ccb->csio; |
| 1445 | scsi_read_capacity(csio, |
| 1446 | /*retries*/4, |
| 1447 | dadone, |
| 1448 | MSG_SIMPLE_Q_TAG, |
| 1449 | rcap, |
| 1450 | SSD_FULL_SIZE, |
| 1451 | /*timeout*/5000); |
| 1452 | start_ccb->ccb_h.ccb_bio = NULL; |
| 1453 | start_ccb->ccb_h.ccb_state = DA_CCB_PROBE; |
| 1454 | xpt_action(start_ccb); |
| 1455 | break; |
| 1456 | } |
| 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | static int |
| 1461 | cmd6workaround(union ccb *ccb) |
| 1462 | { |
| 1463 | struct scsi_rw_6 cmd6; |
| 1464 | struct scsi_rw_10 *cmd10; |
| 1465 | struct da_softc *softc; |
| 1466 | u_int8_t *cdb; |
| 1467 | int frozen; |
| 1468 | |
| 1469 | cdb = ccb->csio.cdb_io.cdb_bytes; |
| 1470 | |
| 1471 | /* Translation only possible if CDB is an array and cmd is R/W6 */ |
| 1472 | if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 || |
| 1473 | (*cdb != READ_6 && *cdb != WRITE_6)) |
| 1474 | return 0; |
| 1475 | |
| 1476 | xpt_print_path(ccb->ccb_h.path); |
| 1477 | printf("READ(6)/WRITE(6) not supported, " |
| 1478 | "increasing minimum_cmd_size to 10.\n"); |
| 1479 | softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc; |
| 1480 | softc->minimum_cmd_size = 10; |
| 1481 | |
| 1482 | bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6)); |
| 1483 | cmd10 = (struct scsi_rw_10 *)cdb; |
| 1484 | cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10; |
| 1485 | cmd10->byte2 = 0; |
| 1486 | scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr); |
| 1487 | cmd10->reserved = 0; |
| 1488 | scsi_ulto2b(cmd6.length, cmd10->length); |
| 1489 | cmd10->control = cmd6.control; |
| 1490 | ccb->csio.cdb_len = sizeof(*cmd10); |
| 1491 | |
| 1492 | /* Requeue request, unfreezing queue if necessary */ |
| 1493 | frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0; |
| 1494 | ccb->ccb_h.status = CAM_REQUEUE_REQ; |
| 1495 | xpt_action(ccb); |
| 1496 | if (frozen) { |
| 1497 | cam_release_devq(ccb->ccb_h.path, |
| 1498 | /*relsim_flags*/0, |
| 1499 | /*reduction*/0, |
| 1500 | /*timeout*/0, |
| 1501 | /*getcount_only*/0); |
| 1502 | } |
| 1503 | return (ERESTART); |
| 1504 | } |
| 1505 | |
| 1506 | static void |
| 1507 | dadone(struct cam_periph *periph, union ccb *done_ccb) |
| 1508 | { |
| 1509 | struct da_softc *softc; |
| 1510 | struct ccb_scsiio *csio; |
| 1511 | |
| 1512 | softc = (struct da_softc *)periph->softc; |
| 1513 | csio = &done_ccb->csio; |
| 1514 | switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) { |
| 1515 | case DA_CCB_BUFFER_IO: |
| 1516 | { |
| 1517 | struct buf *bp; |
| 1518 | struct bio *bio; |
| 1519 | |
| 1520 | bio = (struct bio *)done_ccb->ccb_h.ccb_bio; |
| 1521 | bp = bio->bio_buf; |
| 1522 | if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { |
| 1523 | int error; |
| 1524 | int sf; |
| 1525 | |
| 1526 | if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0) |
| 1527 | sf = SF_RETRY_UA; |
| 1528 | else |
| 1529 | sf = 0; |
| 1530 | |
| 1531 | /* Retry selection timeouts */ |
| 1532 | sf |= SF_RETRY_SELTO; |
| 1533 | |
| 1534 | if ((error = daerror(done_ccb, 0, sf)) == ERESTART) { |
| 1535 | /* |
| 1536 | * A retry was scheuled, so |
| 1537 | * just return. |
| 1538 | */ |
| 1539 | return; |
| 1540 | } |
| 1541 | if (error != 0) { |
| 1542 | struct bio *q_bio; |
| 1543 | struct buf *q_bp; |
| 1544 | |
| 1545 | crit_enter(); |
| 1546 | |
| 1547 | if (error == ENXIO) { |
| 1548 | /* |
| 1549 | * Catastrophic error. Mark our pack as |
| 1550 | * invalid. |
| 1551 | */ |
| 1552 | /* XXX See if this is really a media |
| 1553 | * change first. |
| 1554 | */ |
| 1555 | xpt_print_path(periph->path); |
| 1556 | printf("Invalidating pack\n"); |
| 1557 | softc->flags |= DA_FLAG_PACK_INVALID; |
| 1558 | } |
| 1559 | |
| 1560 | /* |
| 1561 | * return all queued I/O with EIO, so that |
| 1562 | * the client can retry these I/Os in the |
| 1563 | * proper order should it attempt to recover. |
| 1564 | */ |
| 1565 | while ((q_bio = bioq_first(&softc->bio_queue)) |
| 1566 | != NULL) { |
| 1567 | bioq_remove(&softc->bio_queue, q_bio); |
| 1568 | q_bp = q_bio->bio_buf; |
| 1569 | q_bp->b_resid = q_bp->b_bcount; |
| 1570 | q_bp->b_error = EIO; |
| 1571 | q_bp->b_flags |= B_ERROR; |
| 1572 | biodone(q_bio); |
| 1573 | } |
| 1574 | crit_exit(); |
| 1575 | bp->b_error = error; |
| 1576 | bp->b_resid = bp->b_bcount; |
| 1577 | bp->b_flags |= B_ERROR; |
| 1578 | } else { |
| 1579 | bp->b_resid = csio->resid; |
| 1580 | bp->b_error = 0; |
| 1581 | if (bp->b_resid != 0) |
| 1582 | bp->b_flags |= B_ERROR; |
| 1583 | } |
| 1584 | if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) |
| 1585 | cam_release_devq(done_ccb->ccb_h.path, |
| 1586 | /*relsim_flags*/0, |
| 1587 | /*reduction*/0, |
| 1588 | /*timeout*/0, |
| 1589 | /*getcount_only*/0); |
| 1590 | } else { |
| 1591 | bp->b_resid = csio->resid; |
| 1592 | if (csio->resid > 0) |
| 1593 | bp->b_flags |= B_ERROR; |
| 1594 | } |
| 1595 | |
| 1596 | /* |
| 1597 | * Block out any asyncronous callbacks |
| 1598 | * while we touch the pending ccb list. |
| 1599 | */ |
| 1600 | crit_enter(); |
| 1601 | LIST_REMOVE(&done_ccb->ccb_h, periph_links.le); |
| 1602 | crit_exit(); |
| 1603 | |
| 1604 | if (softc->device_stats.busy_count == 0) |
| 1605 | softc->flags |= DA_FLAG_WENT_IDLE; |
| 1606 | |
| 1607 | devstat_end_transaction_buf(&softc->device_stats, bp); |
| 1608 | biodone(bio); |
| 1609 | break; |
| 1610 | } |
| 1611 | case DA_CCB_PROBE: |
| 1612 | { |
| 1613 | struct scsi_read_capacity_data *rdcap; |
| 1614 | char announce_buf[80]; |
| 1615 | |
| 1616 | rdcap = (struct scsi_read_capacity_data *)csio->data_ptr; |
| 1617 | |
| 1618 | if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) { |
| 1619 | struct disk_params *dp; |
| 1620 | |
| 1621 | dasetgeom(periph, rdcap); |
| 1622 | dp = &softc->params; |
| 1623 | snprintf(announce_buf, sizeof(announce_buf), |
| 1624 | "%luMB (%u %u byte sectors: %dH %dS/T %dC)", |
| 1625 | (unsigned long) (((u_int64_t)dp->secsize * |
| 1626 | dp->sectors) / (1024*1024)), dp->sectors, |
| 1627 | dp->secsize, dp->heads, dp->secs_per_track, |
| 1628 | dp->cylinders); |
| 1629 | } else { |
| 1630 | int error; |
| 1631 | |
| 1632 | announce_buf[0] = '\0'; |
| 1633 | |
| 1634 | /* |
| 1635 | * Retry any UNIT ATTENTION type errors. They |
| 1636 | * are expected at boot. |
| 1637 | */ |
| 1638 | error = daerror(done_ccb, 0, SF_RETRY_UA | |
| 1639 | SF_RETRY_SELTO | SF_NO_PRINT); |
| 1640 | if (error == ERESTART) { |
| 1641 | /* |
| 1642 | * A retry was scheuled, so |
| 1643 | * just return. |
| 1644 | */ |
| 1645 | return; |
| 1646 | } else if (error != 0) { |
| 1647 | struct scsi_sense_data *sense; |
| 1648 | int asc, ascq; |
| 1649 | int sense_key, error_code; |
| 1650 | int have_sense; |
| 1651 | cam_status status; |
| 1652 | struct ccb_getdev cgd; |
| 1653 | |
| 1654 | /* Don't wedge this device's queue */ |
| 1655 | cam_release_devq(done_ccb->ccb_h.path, |
| 1656 | /*relsim_flags*/0, |
| 1657 | /*reduction*/0, |
| 1658 | /*timeout*/0, |
| 1659 | /*getcount_only*/0); |
| 1660 | |
| 1661 | status = done_ccb->ccb_h.status; |
| 1662 | |
| 1663 | xpt_setup_ccb(&cgd.ccb_h, |
| 1664 | done_ccb->ccb_h.path, |
| 1665 | /* priority */ 1); |
| 1666 | cgd.ccb_h.func_code = XPT_GDEV_TYPE; |
| 1667 | xpt_action((union ccb *)&cgd); |
| 1668 | |
| 1669 | if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0) |
| 1670 | || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0) |
| 1671 | || ((status & CAM_AUTOSNS_VALID) == 0)) |
| 1672 | have_sense = FALSE; |
| 1673 | else |
| 1674 | have_sense = TRUE; |
| 1675 | |
| 1676 | if (have_sense) { |
| 1677 | sense = &csio->sense_data; |
| 1678 | scsi_extract_sense(sense, &error_code, |
| 1679 | &sense_key, |
| 1680 | &asc, &ascq); |
| 1681 | } |
| 1682 | /* |
| 1683 | * Attach to anything that claims to be a |
| 1684 | * direct access or optical disk device, |
| 1685 | * as long as it doesn't return a "Logical |
| 1686 | * unit not supported" (0x25) error. |
| 1687 | */ |
| 1688 | if ((have_sense) && (asc != 0x25) |
| 1689 | && (error_code == SSD_CURRENT_ERROR)) |
| 1690 | snprintf(announce_buf, |
| 1691 | sizeof(announce_buf), |
| 1692 | "Attempt to query device " |
| 1693 | "size failed: %s, %s", |
| 1694 | scsi_sense_key_text[sense_key], |
| 1695 | scsi_sense_desc(asc,ascq, |
| 1696 | &cgd.inq_data)); |
| 1697 | else { |
| 1698 | if (have_sense) |
| 1699 | scsi_sense_print( |
| 1700 | &done_ccb->csio); |
| 1701 | else { |
| 1702 | xpt_print_path(periph->path); |
| 1703 | printf("got CAM status %#x\n", |
| 1704 | done_ccb->ccb_h.status); |
| 1705 | } |
| 1706 | |
| 1707 | xpt_print_path(periph->path); |
| 1708 | printf("fatal error, failed" |
| 1709 | " to attach to device\n"); |
| 1710 | |
| 1711 | /* |
| 1712 | * Free up resources. |
| 1713 | */ |
| 1714 | cam_periph_invalidate(periph); |
| 1715 | } |
| 1716 | } |
| 1717 | } |
| 1718 | kfree(rdcap, M_TEMP); |
| 1719 | if (announce_buf[0] != '\0') { |
| 1720 | xpt_announce_periph(periph, announce_buf); |
| 1721 | /* |
| 1722 | * Create our sysctl variables, now that we know |
| 1723 | * we have successfully attached. |
| 1724 | */ |
| 1725 | taskqueue_enqueue(taskqueue_thread[mycpuid], |
| 1726 | &softc->sysctl_task); |
| 1727 | } |
| 1728 | softc->state = DA_STATE_NORMAL; |
| 1729 | /* |
| 1730 | * Since our peripheral may be invalidated by an error |
| 1731 | * above or an external event, we must release our CCB |
| 1732 | * before releasing the probe lock on the peripheral. |
| 1733 | * The peripheral will only go away once the last lock |
| 1734 | * is removed, and we need it around for the CCB release |
| 1735 | * operation. |
| 1736 | */ |
| 1737 | xpt_release_ccb(done_ccb); |
| 1738 | cam_periph_unlock(periph); |
| 1739 | return; |
| 1740 | } |
| 1741 | case DA_CCB_WAITING: |
| 1742 | { |
| 1743 | /* Caller will release the CCB */ |
| 1744 | wakeup(&done_ccb->ccb_h.cbfcnp); |
| 1745 | return; |
| 1746 | } |
| 1747 | case DA_CCB_DUMP: |
| 1748 | /* No-op. We're polling */ |
| 1749 | return; |
| 1750 | default: |
| 1751 | break; |
| 1752 | } |
| 1753 | xpt_release_ccb(done_ccb); |
| 1754 | } |
| 1755 | |
| 1756 | static int |
| 1757 | daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) |
| 1758 | { |
| 1759 | struct da_softc *softc; |
| 1760 | struct cam_periph *periph; |
| 1761 | int error; |
| 1762 | |
| 1763 | periph = xpt_path_periph(ccb->ccb_h.path); |
| 1764 | softc = (struct da_softc *)periph->softc; |
| 1765 | |
| 1766 | /* |
| 1767 | * Automatically detect devices that do not support |
| 1768 | * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs. |
| 1769 | */ |
| 1770 | error = 0; |
| 1771 | if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) { |
| 1772 | error = cmd6workaround(ccb); |
| 1773 | } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) == |
| 1774 | CAM_SCSI_STATUS_ERROR) |
| 1775 | && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) |
| 1776 | && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) |
| 1777 | && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0) |
| 1778 | && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) { |
| 1779 | int sense_key, error_code, asc, ascq; |
| 1780 | |
| 1781 | scsi_extract_sense(&ccb->csio.sense_data, |
| 1782 | &error_code, &sense_key, &asc, &ascq); |
| 1783 | if (sense_key == SSD_KEY_ILLEGAL_REQUEST) |
| 1784 | error = cmd6workaround(ccb); |
| 1785 | } |
| 1786 | if (error == ERESTART) |
| 1787 | return (ERESTART); |
| 1788 | |
| 1789 | /* |
| 1790 | * XXX |
| 1791 | * Until we have a better way of doing pack validation, |
| 1792 | * don't treat UAs as errors. |
| 1793 | */ |
| 1794 | sense_flags |= SF_RETRY_UA; |
| 1795 | return(cam_periph_error(ccb, cam_flags, sense_flags, |
| 1796 | &softc->saved_ccb)); |
| 1797 | } |
| 1798 | |
| 1799 | static void |
| 1800 | daprevent(struct cam_periph *periph, int action) |
| 1801 | { |
| 1802 | struct da_softc *softc; |
| 1803 | union ccb *ccb; |
| 1804 | int error; |
| 1805 | |
| 1806 | softc = (struct da_softc *)periph->softc; |
| 1807 | |
| 1808 | if (((action == PR_ALLOW) |
| 1809 | && (softc->flags & DA_FLAG_PACK_LOCKED) == 0) |
| 1810 | || ((action == PR_PREVENT) |
| 1811 | && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) { |
| 1812 | return; |
| 1813 | } |
| 1814 | |
| 1815 | ccb = cam_periph_getccb(periph, /*priority*/1); |
| 1816 | |
| 1817 | scsi_prevent(&ccb->csio, |
| 1818 | /*retries*/1, |
| 1819 | /*cbcfp*/dadone, |
| 1820 | MSG_SIMPLE_Q_TAG, |
| 1821 | action, |
| 1822 | SSD_FULL_SIZE, |
| 1823 | 5000); |
| 1824 | |
| 1825 | error = cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0, |
| 1826 | /*sense_flags*/0, &softc->device_stats); |
| 1827 | |
| 1828 | if (error == 0) { |
| 1829 | if (action == PR_ALLOW) |
| 1830 | softc->flags &= ~DA_FLAG_PACK_LOCKED; |
| 1831 | else |
| 1832 | softc->flags |= DA_FLAG_PACK_LOCKED; |
| 1833 | } |
| 1834 | |
| 1835 | xpt_release_ccb(ccb); |
| 1836 | } |
| 1837 | |
| 1838 | static void |
| 1839 | dasetgeom(struct cam_periph *periph, struct scsi_read_capacity_data * rdcap) |
| 1840 | { |
| 1841 | struct ccb_calc_geometry ccg; |
| 1842 | struct da_softc *softc; |
| 1843 | struct disk_params *dp; |
| 1844 | |
| 1845 | softc = (struct da_softc *)periph->softc; |
| 1846 | |
| 1847 | dp = &softc->params; |
| 1848 | dp->secsize = scsi_4btoul(rdcap->length); |
| 1849 | dp->sectors = scsi_4btoul(rdcap->addr) + 1; |
| 1850 | /* |
| 1851 | * Have the controller provide us with a geometry |
| 1852 | * for this disk. The only time the geometry |
| 1853 | * matters is when we boot and the controller |
| 1854 | * is the only one knowledgeable enough to come |
| 1855 | * up with something that will make this a bootable |
| 1856 | * device. |
| 1857 | */ |
| 1858 | xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1); |
| 1859 | ccg.ccb_h.func_code = XPT_CALC_GEOMETRY; |
| 1860 | ccg.block_size = dp->secsize; |
| 1861 | ccg.volume_size = dp->sectors; |
| 1862 | ccg.heads = 0; |
| 1863 | ccg.secs_per_track = 0; |
| 1864 | ccg.cylinders = 0; |
| 1865 | xpt_action((union ccb*)&ccg); |
| 1866 | dp->heads = ccg.heads; |
| 1867 | dp->secs_per_track = ccg.secs_per_track; |
| 1868 | dp->cylinders = ccg.cylinders; |
| 1869 | } |
| 1870 | |
| 1871 | static void |
| 1872 | dasendorderedtag(void *arg) |
| 1873 | { |
| 1874 | struct da_softc *softc; |
| 1875 | |
| 1876 | for (softc = SLIST_FIRST(&softc_list); |
| 1877 | softc != NULL; |
| 1878 | softc = SLIST_NEXT(softc, links)) { |
| 1879 | crit_enter(); |
| 1880 | if ((softc->ordered_tag_count == 0) |
| 1881 | && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) { |
| 1882 | softc->flags |= DA_FLAG_NEED_OTAG; |
| 1883 | } |
| 1884 | if (softc->device_stats.busy_count > 0) |
| 1885 | softc->flags &= ~DA_FLAG_WENT_IDLE; |
| 1886 | |
| 1887 | softc->ordered_tag_count = 0; |
| 1888 | crit_exit(); |
| 1889 | } |
| 1890 | /* Queue us up again */ |
| 1891 | callout_reset(&dasendorderedtag_ch, |
| 1892 | (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL, |
| 1893 | dasendorderedtag, NULL); |
| 1894 | } |
| 1895 | |
| 1896 | /* |
| 1897 | * Step through all DA peripheral drivers, and if the device is still open, |
| 1898 | * sync the disk cache to physical media. |
| 1899 | */ |
| 1900 | static void |
| 1901 | dashutdown(void * arg, int howto) |
| 1902 | { |
| 1903 | struct cam_periph *periph; |
| 1904 | struct da_softc *softc; |
| 1905 | |
| 1906 | for (periph = TAILQ_FIRST(&dadriver.units); periph != NULL; |
| 1907 | periph = TAILQ_NEXT(periph, unit_links)) { |
| 1908 | union ccb ccb; |
| 1909 | softc = (struct da_softc *)periph->softc; |
| 1910 | |
| 1911 | /* |
| 1912 | * We only sync the cache if the drive is still open, and |
| 1913 | * if the drive is capable of it.. |
| 1914 | */ |
| 1915 | if (((softc->flags & DA_FLAG_OPEN) == 0) |
| 1916 | || (softc->quirks & DA_Q_NO_SYNC_CACHE)) |
| 1917 | continue; |
| 1918 | |
| 1919 | xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1); |
| 1920 | |
| 1921 | ccb.ccb_h.ccb_state = DA_CCB_DUMP; |
| 1922 | scsi_synchronize_cache(&ccb.csio, |
| 1923 | /*retries*/1, |
| 1924 | /*cbfcnp*/dadone, |
| 1925 | MSG_SIMPLE_Q_TAG, |
| 1926 | /*begin_lba*/0, /* whole disk */ |
| 1927 | /*lb_count*/0, |
| 1928 | SSD_FULL_SIZE, |
| 1929 | 5 * 60 * 1000); |
| 1930 | |
| 1931 | xpt_polled_action(&ccb); |
| 1932 | |
| 1933 | if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { |
| 1934 | if (((ccb.ccb_h.status & CAM_STATUS_MASK) == |
| 1935 | CAM_SCSI_STATUS_ERROR) |
| 1936 | && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){ |
| 1937 | int error_code, sense_key, asc, ascq; |
| 1938 | |
| 1939 | scsi_extract_sense(&ccb.csio.sense_data, |
| 1940 | &error_code, &sense_key, |
| 1941 | &asc, &ascq); |
| 1942 | |
| 1943 | if (sense_key != SSD_KEY_ILLEGAL_REQUEST) |
| 1944 | scsi_sense_print(&ccb.csio); |
| 1945 | } else { |
| 1946 | xpt_print_path(periph->path); |
| 1947 | printf("Synchronize cache failed, status " |
| 1948 | "== 0x%x, scsi status == 0x%x\n", |
| 1949 | ccb.ccb_h.status, ccb.csio.scsi_status); |
| 1950 | } |
| 1951 | } |
| 1952 | |
| 1953 | if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0) |
| 1954 | cam_release_devq(ccb.ccb_h.path, |
| 1955 | /*relsim_flags*/0, |
| 1956 | /*reduction*/0, |
| 1957 | /*timeout*/0, |
| 1958 | /*getcount_only*/0); |
| 1959 | |
| 1960 | } |
| 1961 | } |
| 1962 | |
| 1963 | #else /* !_KERNEL */ |
| 1964 | |
| 1965 | /* |
| 1966 | * XXX This is only left out of the kernel build to silence warnings. If, |
| 1967 | * for some reason this function is used in the kernel, the ifdefs should |
| 1968 | * be moved so it is included both in the kernel and userland. |
| 1969 | */ |
| 1970 | void |
| 1971 | scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries, |
| 1972 | void (*cbfcnp)(struct cam_periph *, union ccb *), |
| 1973 | u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave, |
| 1974 | u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len, |
| 1975 | u_int32_t timeout) |
| 1976 | { |
| 1977 | struct scsi_format_unit *scsi_cmd; |
| 1978 | |
| 1979 | scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes; |
| 1980 | scsi_cmd->opcode = FORMAT_UNIT; |
| 1981 | scsi_cmd->byte2 = byte2; |
| 1982 | scsi_ulto2b(ileave, scsi_cmd->interleave); |
| 1983 | |
| 1984 | cam_fill_csio(csio, |
| 1985 | retries, |
| 1986 | cbfcnp, |
| 1987 | /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE, |
| 1988 | tag_action, |
| 1989 | data_ptr, |
| 1990 | dxfer_len, |
| 1991 | sense_len, |
| 1992 | sizeof(*scsi_cmd), |
| 1993 | timeout); |
| 1994 | } |
| 1995 | |
| 1996 | #endif /* _KERNEL */ |