| 1 | // SPDX-License-Identifier: GPL-2.0-only | 
|---|
| 2 | /* | 
|---|
| 3 | *  scsi.c Copyright (C) 1992 Drew Eckhardt | 
|---|
| 4 | *         Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale | 
|---|
| 5 | *         Copyright (C) 2002, 2003 Christoph Hellwig | 
|---|
| 6 | * | 
|---|
| 7 | *  generic mid-level SCSI driver | 
|---|
| 8 | *      Initial versions: Drew Eckhardt | 
|---|
| 9 | *      Subsequent revisions: Eric Youngdale | 
|---|
| 10 | * | 
|---|
| 11 | *  <drew@colorado.edu> | 
|---|
| 12 | * | 
|---|
| 13 | *  Bug correction thanks go to : | 
|---|
| 14 | *      Rik Faith <faith@cs.unc.edu> | 
|---|
| 15 | *      Tommy Thorn <tthorn> | 
|---|
| 16 | *      Thomas Wuensche <tw@fgb1.fgb.mw.tu-muenchen.de> | 
|---|
| 17 | * | 
|---|
| 18 | *  Modified by Eric Youngdale eric@andante.org or ericy@gnu.ai.mit.edu to | 
|---|
| 19 | *  add scatter-gather, multiple outstanding request, and other | 
|---|
| 20 | *  enhancements. | 
|---|
| 21 | * | 
|---|
| 22 | *  Native multichannel, wide scsi, /proc/scsi and hot plugging | 
|---|
| 23 | *  support added by Michael Neuffer <mike@i-connect.net> | 
|---|
| 24 | * | 
|---|
| 25 | *  Added request_module("scsi_hostadapter") for kerneld: | 
|---|
| 26 | *  (Put an "alias scsi_hostadapter your_hostadapter" in /etc/modprobe.conf) | 
|---|
| 27 | *  Bjorn Ekwall  <bj0rn@blox.se> | 
|---|
| 28 | *  (changed to kmod) | 
|---|
| 29 | * | 
|---|
| 30 | *  Major improvements to the timeout, abort, and reset processing, | 
|---|
| 31 | *  as well as performance modifications for large queue depths by | 
|---|
| 32 | *  Leonard N. Zubkoff <lnz@dandelion.com> | 
|---|
| 33 | * | 
|---|
| 34 | *  Converted cli() code to spinlocks, Ingo Molnar | 
|---|
| 35 | * | 
|---|
| 36 | *  Jiffies wrap fixes (host->resetting), 3 Dec 1998 Andrea Arcangeli | 
|---|
| 37 | * | 
|---|
| 38 | *  out_of_space hacks, D. Gilbert (dpg) 990608 | 
|---|
| 39 | */ | 
|---|
| 40 |  | 
|---|
| 41 | #include <linux/module.h> | 
|---|
| 42 | #include <linux/moduleparam.h> | 
|---|
| 43 | #include <linux/kernel.h> | 
|---|
| 44 | #include <linux/timer.h> | 
|---|
| 45 | #include <linux/string.h> | 
|---|
| 46 | #include <linux/slab.h> | 
|---|
| 47 | #include <linux/blkdev.h> | 
|---|
| 48 | #include <linux/delay.h> | 
|---|
| 49 | #include <linux/init.h> | 
|---|
| 50 | #include <linux/completion.h> | 
|---|
| 51 | #include <linux/unistd.h> | 
|---|
| 52 | #include <linux/spinlock.h> | 
|---|
| 53 | #include <linux/kmod.h> | 
|---|
| 54 | #include <linux/interrupt.h> | 
|---|
| 55 | #include <linux/notifier.h> | 
|---|
| 56 | #include <linux/cpu.h> | 
|---|
| 57 | #include <linux/mutex.h> | 
|---|
| 58 | #include <linux/unaligned.h> | 
|---|
| 59 |  | 
|---|
| 60 | #include <scsi/scsi.h> | 
|---|
| 61 | #include <scsi/scsi_cmnd.h> | 
|---|
| 62 | #include <scsi/scsi_dbg.h> | 
|---|
| 63 | #include <scsi/scsi_device.h> | 
|---|
| 64 | #include <scsi/scsi_driver.h> | 
|---|
| 65 | #include <scsi/scsi_eh.h> | 
|---|
| 66 | #include <scsi/scsi_host.h> | 
|---|
| 67 | #include <scsi/scsi_tcq.h> | 
|---|
| 68 |  | 
|---|
| 69 | #include "scsi_priv.h" | 
|---|
| 70 | #include "scsi_logging.h" | 
|---|
| 71 |  | 
|---|
| 72 | #define CREATE_TRACE_POINTS | 
|---|
| 73 | #include <trace/events/scsi.h> | 
|---|
| 74 |  | 
|---|
| 75 | /* | 
|---|
| 76 | * Definitions and constants. | 
|---|
| 77 | */ | 
|---|
| 78 |  | 
|---|
| 79 | /* | 
|---|
| 80 | * Note - the initial logging level can be set here to log events at boot time. | 
|---|
| 81 | * After the system is up, you may enable logging via the /proc interface. | 
|---|
| 82 | */ | 
|---|
| 83 | unsigned int scsi_logging_level; | 
|---|
| 84 | #if defined(CONFIG_SCSI_LOGGING) | 
|---|
| 85 | EXPORT_SYMBOL(scsi_logging_level); | 
|---|
| 86 | #endif | 
|---|
| 87 |  | 
|---|
| 88 | #ifdef CONFIG_SCSI_LOGGING | 
|---|
| 89 | void scsi_log_send(struct scsi_cmnd *cmd) | 
|---|
| 90 | { | 
|---|
| 91 | unsigned int level; | 
|---|
| 92 |  | 
|---|
| 93 | /* | 
|---|
| 94 | * If ML QUEUE log level is greater than or equal to: | 
|---|
| 95 | * | 
|---|
| 96 | * 1: nothing (match completion) | 
|---|
| 97 | * | 
|---|
| 98 | * 2: log opcode + command of all commands + cmd address | 
|---|
| 99 | * | 
|---|
| 100 | * 3: same as 2 | 
|---|
| 101 | * | 
|---|
| 102 | * 4: same as 3 | 
|---|
| 103 | */ | 
|---|
| 104 | if (unlikely(scsi_logging_level)) { | 
|---|
| 105 | level = SCSI_LOG_LEVEL(SCSI_LOG_MLQUEUE_SHIFT, | 
|---|
| 106 | SCSI_LOG_MLQUEUE_BITS); | 
|---|
| 107 | if (level > 1) { | 
|---|
| 108 | scmd_printk(KERN_INFO, cmd, | 
|---|
| 109 | "Send: scmd 0x%p\n", cmd); | 
|---|
| 110 | scsi_print_command(cmd); | 
|---|
| 111 | } | 
|---|
| 112 | } | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | void scsi_log_completion(struct scsi_cmnd *cmd, int disposition) | 
|---|
| 116 | { | 
|---|
| 117 | unsigned int level; | 
|---|
| 118 |  | 
|---|
| 119 | /* | 
|---|
| 120 | * If ML COMPLETE log level is greater than or equal to: | 
|---|
| 121 | * | 
|---|
| 122 | * 1: log disposition, result, opcode + command, and conditionally | 
|---|
| 123 | * sense data for failures or non SUCCESS dispositions. | 
|---|
| 124 | * | 
|---|
| 125 | * 2: same as 1 but for all command completions. | 
|---|
| 126 | * | 
|---|
| 127 | * 3: same as 2 | 
|---|
| 128 | * | 
|---|
| 129 | * 4: same as 3 plus dump extra junk | 
|---|
| 130 | */ | 
|---|
| 131 | if (unlikely(scsi_logging_level)) { | 
|---|
| 132 | level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT, | 
|---|
| 133 | SCSI_LOG_MLCOMPLETE_BITS); | 
|---|
| 134 | if (((level > 0) && (cmd->result || disposition != SUCCESS)) || | 
|---|
| 135 | (level > 1)) { | 
|---|
| 136 | scsi_print_result(cmd, "Done", disposition); | 
|---|
| 137 | scsi_print_command(cmd); | 
|---|
| 138 | if (scsi_status_is_check_condition(cmd->result)) | 
|---|
| 139 | scsi_print_sense(cmd); | 
|---|
| 140 | if (level > 3) | 
|---|
| 141 | scmd_printk(KERN_INFO, cmd, | 
|---|
| 142 | "scsi host busy %d failed %d\n", | 
|---|
| 143 | scsi_host_busy(cmd->device->host), | 
|---|
| 144 | cmd->device->host->host_failed); | 
|---|
| 145 | } | 
|---|
| 146 | } | 
|---|
| 147 | } | 
|---|
| 148 | #endif | 
|---|
| 149 |  | 
|---|
| 150 | /** | 
|---|
| 151 | * scsi_finish_command - cleanup and pass command back to upper layer | 
|---|
| 152 | * @cmd: the command | 
|---|
| 153 | * | 
|---|
| 154 | * Description: Pass command off to upper layer for finishing of I/O | 
|---|
| 155 | *              request, waking processes that are waiting on results, | 
|---|
| 156 | *              etc. | 
|---|
| 157 | */ | 
|---|
| 158 | void scsi_finish_command(struct scsi_cmnd *cmd) | 
|---|
| 159 | { | 
|---|
| 160 | struct scsi_device *sdev = cmd->device; | 
|---|
| 161 | struct scsi_target *starget = scsi_target(sdev); | 
|---|
| 162 | struct Scsi_Host *shost = sdev->host; | 
|---|
| 163 | struct scsi_driver *drv; | 
|---|
| 164 | unsigned int good_bytes; | 
|---|
| 165 |  | 
|---|
| 166 | scsi_device_unbusy(sdev, cmd); | 
|---|
| 167 |  | 
|---|
| 168 | /* | 
|---|
| 169 | * Clear the flags that say that the device/target/host is no longer | 
|---|
| 170 | * capable of accepting new commands. | 
|---|
| 171 | */ | 
|---|
| 172 | if (atomic_read(v: &shost->host_blocked)) | 
|---|
| 173 | atomic_set(v: &shost->host_blocked, i: 0); | 
|---|
| 174 | if (atomic_read(v: &starget->target_blocked)) | 
|---|
| 175 | atomic_set(v: &starget->target_blocked, i: 0); | 
|---|
| 176 | if (atomic_read(v: &sdev->device_blocked)) | 
|---|
| 177 | atomic_set(v: &sdev->device_blocked, i: 0); | 
|---|
| 178 |  | 
|---|
| 179 | SCSI_LOG_MLCOMPLETE(4, sdev_printk(KERN_INFO, sdev, | 
|---|
| 180 | "Notifying upper driver of completion " | 
|---|
| 181 | "(result %x)\n", cmd->result)); | 
|---|
| 182 |  | 
|---|
| 183 | good_bytes = scsi_bufflen(cmd); | 
|---|
| 184 | if (!blk_rq_is_passthrough(rq: scsi_cmd_to_rq(scmd: cmd))) { | 
|---|
| 185 | int old_good_bytes = good_bytes; | 
|---|
| 186 | drv = scsi_cmd_to_driver(cmd); | 
|---|
| 187 | if (drv->done) | 
|---|
| 188 | good_bytes = drv->done(cmd); | 
|---|
| 189 | /* | 
|---|
| 190 | * USB may not give sense identifying bad sector and | 
|---|
| 191 | * simply return a residue instead, so subtract off the | 
|---|
| 192 | * residue if drv->done() error processing indicates no | 
|---|
| 193 | * change to the completion length. | 
|---|
| 194 | */ | 
|---|
| 195 | if (good_bytes == old_good_bytes) | 
|---|
| 196 | good_bytes -= scsi_get_resid(cmd); | 
|---|
| 197 | } | 
|---|
| 198 | scsi_io_completion(cmd, good_bytes); | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 |  | 
|---|
| 202 | /* | 
|---|
| 203 | * 4096 is big enough for saturating fast SCSI LUNs. | 
|---|
| 204 | */ | 
|---|
| 205 | int scsi_device_max_queue_depth(struct scsi_device *sdev) | 
|---|
| 206 | { | 
|---|
| 207 | return min_t(int, sdev->host->can_queue, 4096); | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 210 | /** | 
|---|
| 211 | * scsi_change_queue_depth - change a device's queue depth | 
|---|
| 212 | * @sdev: SCSI Device in question | 
|---|
| 213 | * @depth: number of commands allowed to be queued to the driver | 
|---|
| 214 | * | 
|---|
| 215 | * Sets the device queue depth and returns the new value. | 
|---|
| 216 | */ | 
|---|
| 217 | int scsi_change_queue_depth(struct scsi_device *sdev, int depth) | 
|---|
| 218 | { | 
|---|
| 219 | depth = min_t(int, depth, scsi_device_max_queue_depth(sdev)); | 
|---|
| 220 |  | 
|---|
| 221 | if (depth > 0) { | 
|---|
| 222 | sdev->queue_depth = depth; | 
|---|
| 223 | wmb(); | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | if (sdev->request_queue) | 
|---|
| 227 | blk_set_queue_depth(q: sdev->request_queue, depth); | 
|---|
| 228 |  | 
|---|
| 229 | sbitmap_resize(sb: &sdev->budget_map, depth: sdev->queue_depth); | 
|---|
| 230 |  | 
|---|
| 231 | return sdev->queue_depth; | 
|---|
| 232 | } | 
|---|
| 233 | EXPORT_SYMBOL(scsi_change_queue_depth); | 
|---|
| 234 |  | 
|---|
| 235 | /** | 
|---|
| 236 | * scsi_track_queue_full - track QUEUE_FULL events to adjust queue depth | 
|---|
| 237 | * @sdev: SCSI Device in question | 
|---|
| 238 | * @depth: Current number of outstanding SCSI commands on this device, | 
|---|
| 239 | *         not counting the one returned as QUEUE_FULL. | 
|---|
| 240 | * | 
|---|
| 241 | * Description:	This function will track successive QUEUE_FULL events on a | 
|---|
| 242 | * 		specific SCSI device to determine if and when there is a | 
|---|
| 243 | * 		need to adjust the queue depth on the device. | 
|---|
| 244 | * | 
|---|
| 245 | * Returns: | 
|---|
| 246 | * * 0 - No change needed | 
|---|
| 247 | * * >0 - Adjust queue depth to this new depth, | 
|---|
| 248 | * * -1 - Drop back to untagged operation using host->cmd_per_lun as the | 
|---|
| 249 | *   untagged command depth | 
|---|
| 250 | * | 
|---|
| 251 | * Lock Status:	None held on entry | 
|---|
| 252 | * | 
|---|
| 253 | * Notes:	Low level drivers may call this at any time and we will do | 
|---|
| 254 | * 		"The Right Thing."  We are interrupt context safe. | 
|---|
| 255 | */ | 
|---|
| 256 | int scsi_track_queue_full(struct scsi_device *sdev, int depth) | 
|---|
| 257 | { | 
|---|
| 258 |  | 
|---|
| 259 | /* | 
|---|
| 260 | * Don't let QUEUE_FULLs on the same | 
|---|
| 261 | * jiffies count, they could all be from | 
|---|
| 262 | * same event. | 
|---|
| 263 | */ | 
|---|
| 264 | if ((jiffies >> 4) == (sdev->last_queue_full_time >> 4)) | 
|---|
| 265 | return 0; | 
|---|
| 266 |  | 
|---|
| 267 | sdev->last_queue_full_time = jiffies; | 
|---|
| 268 | if (sdev->last_queue_full_depth != depth) { | 
|---|
| 269 | sdev->last_queue_full_count = 1; | 
|---|
| 270 | sdev->last_queue_full_depth = depth; | 
|---|
| 271 | } else { | 
|---|
| 272 | sdev->last_queue_full_count++; | 
|---|
| 273 | } | 
|---|
| 274 |  | 
|---|
| 275 | if (sdev->last_queue_full_count <= 10) | 
|---|
| 276 | return 0; | 
|---|
| 277 |  | 
|---|
| 278 | return scsi_change_queue_depth(sdev, depth); | 
|---|
| 279 | } | 
|---|
| 280 | EXPORT_SYMBOL(scsi_track_queue_full); | 
|---|
| 281 |  | 
|---|
| 282 | /** | 
|---|
| 283 | * scsi_vpd_inquiry - Request a device provide us with a VPD page | 
|---|
| 284 | * @sdev: The device to ask | 
|---|
| 285 | * @buffer: Where to put the result | 
|---|
| 286 | * @page: Which Vital Product Data to return | 
|---|
| 287 | * @len: The length of the buffer | 
|---|
| 288 | * | 
|---|
| 289 | * This is an internal helper function.  You probably want to use | 
|---|
| 290 | * scsi_get_vpd_page instead. | 
|---|
| 291 | * | 
|---|
| 292 | * Returns size of the vpd page on success or a negative error number. | 
|---|
| 293 | */ | 
|---|
| 294 | static int scsi_vpd_inquiry(struct scsi_device *sdev, unsigned char *buffer, | 
|---|
| 295 | u8 page, unsigned len) | 
|---|
| 296 | { | 
|---|
| 297 | int result; | 
|---|
| 298 | unsigned char cmd[16]; | 
|---|
| 299 |  | 
|---|
| 300 | if (len < 4) | 
|---|
| 301 | return -EINVAL; | 
|---|
| 302 |  | 
|---|
| 303 | cmd[0] = INQUIRY; | 
|---|
| 304 | cmd[1] = 1;		/* EVPD */ | 
|---|
| 305 | cmd[2] = page; | 
|---|
| 306 | cmd[3] = len >> 8; | 
|---|
| 307 | cmd[4] = len & 0xff; | 
|---|
| 308 | cmd[5] = 0;		/* Control byte */ | 
|---|
| 309 |  | 
|---|
| 310 | /* | 
|---|
| 311 | * I'm not convinced we need to try quite this hard to get VPD, but | 
|---|
| 312 | * all the existing users tried this hard. | 
|---|
| 313 | */ | 
|---|
| 314 | result = scsi_execute_cmd(sdev, cmd, opf: REQ_OP_DRV_IN, buffer, bufflen: len, | 
|---|
| 315 | timeout: 30 * HZ, retries: 3, NULL); | 
|---|
| 316 | if (result) | 
|---|
| 317 | return -EIO; | 
|---|
| 318 |  | 
|---|
| 319 | /* | 
|---|
| 320 | * Sanity check that we got the page back that we asked for and that | 
|---|
| 321 | * the page size is not 0. | 
|---|
| 322 | */ | 
|---|
| 323 | if (buffer[1] != page) | 
|---|
| 324 | return -EIO; | 
|---|
| 325 |  | 
|---|
| 326 | result = get_unaligned_be16(p: &buffer[2]); | 
|---|
| 327 | if (!result) | 
|---|
| 328 | return -EIO; | 
|---|
| 329 |  | 
|---|
| 330 | return result + 4; | 
|---|
| 331 | } | 
|---|
| 332 |  | 
|---|
| 333 | enum scsi_vpd_parameters { | 
|---|
| 334 | = 4, | 
|---|
| 335 | SCSI_VPD_LIST_SIZE = 36, | 
|---|
| 336 | }; | 
|---|
| 337 |  | 
|---|
| 338 | static int scsi_get_vpd_size(struct scsi_device *sdev, u8 page) | 
|---|
| 339 | { | 
|---|
| 340 | unsigned char vpd[SCSI_VPD_LIST_SIZE] __aligned(4); | 
|---|
| 341 | int result; | 
|---|
| 342 |  | 
|---|
| 343 | if (sdev->no_vpd_size) | 
|---|
| 344 | return SCSI_DEFAULT_VPD_LEN; | 
|---|
| 345 |  | 
|---|
| 346 | /* | 
|---|
| 347 | * Fetch the supported pages VPD and validate that the requested page | 
|---|
| 348 | * number is present. | 
|---|
| 349 | */ | 
|---|
| 350 | if (page != 0) { | 
|---|
| 351 | result = scsi_vpd_inquiry(sdev, buffer: vpd, page: 0, len: sizeof(vpd)); | 
|---|
| 352 | if (result < SCSI_VPD_HEADER_SIZE) | 
|---|
| 353 | return 0; | 
|---|
| 354 |  | 
|---|
| 355 | if (result > sizeof(vpd)) { | 
|---|
| 356 | dev_warn_once(&sdev->sdev_gendev, | 
|---|
| 357 | "%s: long VPD page 0 length: %d bytes\n", | 
|---|
| 358 | __func__, result); | 
|---|
| 359 | result = sizeof(vpd); | 
|---|
| 360 | } | 
|---|
| 361 |  | 
|---|
| 362 | result -= SCSI_VPD_HEADER_SIZE; | 
|---|
| 363 | if (!memchr(&vpd[SCSI_VPD_HEADER_SIZE], page, result)) | 
|---|
| 364 | return 0; | 
|---|
| 365 | } | 
|---|
| 366 | /* | 
|---|
| 367 | * Fetch the VPD page header to find out how big the page | 
|---|
| 368 | * is. This is done to prevent problems on legacy devices | 
|---|
| 369 | * which can not handle allocation lengths as large as | 
|---|
| 370 | * potentially requested by the caller. | 
|---|
| 371 | */ | 
|---|
| 372 | result = scsi_vpd_inquiry(sdev, buffer: vpd, page, len: SCSI_VPD_HEADER_SIZE); | 
|---|
| 373 | if (result < 0) | 
|---|
| 374 | return 0; | 
|---|
| 375 |  | 
|---|
| 376 | if (result < SCSI_VPD_HEADER_SIZE) { | 
|---|
| 377 | dev_warn_once(&sdev->sdev_gendev, | 
|---|
| 378 | "%s: short VPD page 0x%02x length: %d bytes\n", | 
|---|
| 379 | __func__, page, result); | 
|---|
| 380 | return 0; | 
|---|
| 381 | } | 
|---|
| 382 |  | 
|---|
| 383 | return result; | 
|---|
| 384 | } | 
|---|
| 385 |  | 
|---|
| 386 | /** | 
|---|
| 387 | * scsi_get_vpd_page - Get Vital Product Data from a SCSI device | 
|---|
| 388 | * @sdev: The device to ask | 
|---|
| 389 | * @page: Which Vital Product Data to return | 
|---|
| 390 | * @buf: where to store the VPD | 
|---|
| 391 | * @buf_len: number of bytes in the VPD buffer area | 
|---|
| 392 | * | 
|---|
| 393 | * SCSI devices may optionally supply Vital Product Data.  Each 'page' | 
|---|
| 394 | * of VPD is defined in the appropriate SCSI document (eg SPC, SBC). | 
|---|
| 395 | * If the device supports this VPD page, this routine fills @buf | 
|---|
| 396 | * with the data from that page and return 0. If the VPD page is not | 
|---|
| 397 | * supported or its content cannot be retrieved, -EINVAL is returned. | 
|---|
| 398 | */ | 
|---|
| 399 | int scsi_get_vpd_page(struct scsi_device *sdev, u8 page, unsigned char *buf, | 
|---|
| 400 | int buf_len) | 
|---|
| 401 | { | 
|---|
| 402 | int result, vpd_len; | 
|---|
| 403 |  | 
|---|
| 404 | if (!scsi_device_supports_vpd(sdev)) | 
|---|
| 405 | return -EINVAL; | 
|---|
| 406 |  | 
|---|
| 407 | vpd_len = scsi_get_vpd_size(sdev, page); | 
|---|
| 408 | if (vpd_len <= 0) | 
|---|
| 409 | return -EINVAL; | 
|---|
| 410 |  | 
|---|
| 411 | vpd_len = min(vpd_len, buf_len); | 
|---|
| 412 |  | 
|---|
| 413 | /* | 
|---|
| 414 | * Fetch the actual page. Since the appropriate size was reported | 
|---|
| 415 | * by the device it is now safe to ask for something bigger. | 
|---|
| 416 | */ | 
|---|
| 417 | memset(s: buf, c: 0, n: buf_len); | 
|---|
| 418 | result = scsi_vpd_inquiry(sdev, buffer: buf, page, len: vpd_len); | 
|---|
| 419 | if (result < 0) | 
|---|
| 420 | return -EINVAL; | 
|---|
| 421 | else if (result > vpd_len) | 
|---|
| 422 | dev_warn_once(&sdev->sdev_gendev, | 
|---|
| 423 | "%s: VPD page 0x%02x result %d > %d bytes\n", | 
|---|
| 424 | __func__, page, result, vpd_len); | 
|---|
| 425 |  | 
|---|
| 426 | return 0; | 
|---|
| 427 | } | 
|---|
| 428 | EXPORT_SYMBOL_GPL(scsi_get_vpd_page); | 
|---|
| 429 |  | 
|---|
| 430 | /** | 
|---|
| 431 | * scsi_get_vpd_buf - Get Vital Product Data from a SCSI device | 
|---|
| 432 | * @sdev: The device to ask | 
|---|
| 433 | * @page: Which Vital Product Data to return | 
|---|
| 434 | * | 
|---|
| 435 | * Returns %NULL upon failure. | 
|---|
| 436 | */ | 
|---|
| 437 | static struct scsi_vpd *scsi_get_vpd_buf(struct scsi_device *sdev, u8 page) | 
|---|
| 438 | { | 
|---|
| 439 | struct scsi_vpd *vpd_buf; | 
|---|
| 440 | int vpd_len, result; | 
|---|
| 441 |  | 
|---|
| 442 | vpd_len = scsi_get_vpd_size(sdev, page); | 
|---|
| 443 | if (vpd_len <= 0) | 
|---|
| 444 | return NULL; | 
|---|
| 445 |  | 
|---|
| 446 | retry_pg: | 
|---|
| 447 | /* | 
|---|
| 448 | * Fetch the actual page. Since the appropriate size was reported | 
|---|
| 449 | * by the device it is now safe to ask for something bigger. | 
|---|
| 450 | */ | 
|---|
| 451 | vpd_buf = kmalloc(sizeof(*vpd_buf) + vpd_len, GFP_KERNEL); | 
|---|
| 452 | if (!vpd_buf) | 
|---|
| 453 | return NULL; | 
|---|
| 454 |  | 
|---|
| 455 | result = scsi_vpd_inquiry(sdev, buffer: vpd_buf->data, page, len: vpd_len); | 
|---|
| 456 | if (result < 0) { | 
|---|
| 457 | kfree(objp: vpd_buf); | 
|---|
| 458 | return NULL; | 
|---|
| 459 | } | 
|---|
| 460 | if (result > vpd_len) { | 
|---|
| 461 | dev_warn_once(&sdev->sdev_gendev, | 
|---|
| 462 | "%s: VPD page 0x%02x result %d > %d bytes\n", | 
|---|
| 463 | __func__, page, result, vpd_len); | 
|---|
| 464 | vpd_len = result; | 
|---|
| 465 | kfree(objp: vpd_buf); | 
|---|
| 466 | goto retry_pg; | 
|---|
| 467 | } | 
|---|
| 468 |  | 
|---|
| 469 | vpd_buf->len = result; | 
|---|
| 470 |  | 
|---|
| 471 | return vpd_buf; | 
|---|
| 472 | } | 
|---|
| 473 |  | 
|---|
| 474 | static void scsi_update_vpd_page(struct scsi_device *sdev, u8 page, | 
|---|
| 475 | struct scsi_vpd __rcu **sdev_vpd_buf) | 
|---|
| 476 | { | 
|---|
| 477 | struct scsi_vpd *vpd_buf; | 
|---|
| 478 |  | 
|---|
| 479 | vpd_buf = scsi_get_vpd_buf(sdev, page); | 
|---|
| 480 | if (!vpd_buf) | 
|---|
| 481 | return; | 
|---|
| 482 |  | 
|---|
| 483 | mutex_lock(lock: &sdev->inquiry_mutex); | 
|---|
| 484 | vpd_buf = rcu_replace_pointer(*sdev_vpd_buf, vpd_buf, | 
|---|
| 485 | lockdep_is_held(&sdev->inquiry_mutex)); | 
|---|
| 486 | mutex_unlock(lock: &sdev->inquiry_mutex); | 
|---|
| 487 |  | 
|---|
| 488 | if (vpd_buf) | 
|---|
| 489 | kfree_rcu(vpd_buf, rcu); | 
|---|
| 490 | } | 
|---|
| 491 |  | 
|---|
| 492 | /** | 
|---|
| 493 | * scsi_attach_vpd - Attach Vital Product Data to a SCSI device structure | 
|---|
| 494 | * @sdev: The device to ask | 
|---|
| 495 | * | 
|---|
| 496 | * Attach the 'Device Identification' VPD page (0x83) and the | 
|---|
| 497 | * 'Unit Serial Number' VPD page (0x80) to a SCSI device | 
|---|
| 498 | * structure. This information can be used to identify the device | 
|---|
| 499 | * uniquely. | 
|---|
| 500 | */ | 
|---|
| 501 | void scsi_attach_vpd(struct scsi_device *sdev) | 
|---|
| 502 | { | 
|---|
| 503 | int i; | 
|---|
| 504 | struct scsi_vpd *vpd_buf; | 
|---|
| 505 |  | 
|---|
| 506 | if (!scsi_device_supports_vpd(sdev)) | 
|---|
| 507 | return; | 
|---|
| 508 |  | 
|---|
| 509 | /* Ask for all the pages supported by this device */ | 
|---|
| 510 | vpd_buf = scsi_get_vpd_buf(sdev, page: 0); | 
|---|
| 511 | if (!vpd_buf) | 
|---|
| 512 | return; | 
|---|
| 513 |  | 
|---|
| 514 | for (i = 4; i < vpd_buf->len; i++) { | 
|---|
| 515 | switch (vpd_buf->data[i]) { | 
|---|
| 516 | case 0x0: | 
|---|
| 517 | scsi_update_vpd_page(sdev, page: 0x0, sdev_vpd_buf: &sdev->vpd_pg0); | 
|---|
| 518 | break; | 
|---|
| 519 | case 0x80: | 
|---|
| 520 | scsi_update_vpd_page(sdev, page: 0x80, sdev_vpd_buf: &sdev->vpd_pg80); | 
|---|
| 521 | break; | 
|---|
| 522 | case 0x83: | 
|---|
| 523 | scsi_update_vpd_page(sdev, page: 0x83, sdev_vpd_buf: &sdev->vpd_pg83); | 
|---|
| 524 | break; | 
|---|
| 525 | case 0x89: | 
|---|
| 526 | scsi_update_vpd_page(sdev, page: 0x89, sdev_vpd_buf: &sdev->vpd_pg89); | 
|---|
| 527 | break; | 
|---|
| 528 | case 0xb0: | 
|---|
| 529 | scsi_update_vpd_page(sdev, page: 0xb0, sdev_vpd_buf: &sdev->vpd_pgb0); | 
|---|
| 530 | break; | 
|---|
| 531 | case 0xb1: | 
|---|
| 532 | scsi_update_vpd_page(sdev, page: 0xb1, sdev_vpd_buf: &sdev->vpd_pgb1); | 
|---|
| 533 | break; | 
|---|
| 534 | case 0xb2: | 
|---|
| 535 | scsi_update_vpd_page(sdev, page: 0xb2, sdev_vpd_buf: &sdev->vpd_pgb2); | 
|---|
| 536 | break; | 
|---|
| 537 | case 0xb7: | 
|---|
| 538 | scsi_update_vpd_page(sdev, page: 0xb7, sdev_vpd_buf: &sdev->vpd_pgb7); | 
|---|
| 539 | break; | 
|---|
| 540 | default: | 
|---|
| 541 | break; | 
|---|
| 542 | } | 
|---|
| 543 | } | 
|---|
| 544 | kfree(objp: vpd_buf); | 
|---|
| 545 | } | 
|---|
| 546 |  | 
|---|
| 547 | /** | 
|---|
| 548 | * scsi_report_opcode - Find out if a given command is supported | 
|---|
| 549 | * @sdev:	scsi device to query | 
|---|
| 550 | * @buffer:	scratch buffer (must be at least 20 bytes long) | 
|---|
| 551 | * @len:	length of buffer | 
|---|
| 552 | * @opcode:	opcode for the command to look up | 
|---|
| 553 | * @sa:		service action for the command to look up | 
|---|
| 554 | * | 
|---|
| 555 | * Uses the REPORT SUPPORTED OPERATION CODES to check support for the | 
|---|
| 556 | * command identified with @opcode and @sa. If the command does not | 
|---|
| 557 | * have a service action, @sa must be 0. Returns -EINVAL if RSOC fails, | 
|---|
| 558 | * 0 if the command is not supported and 1 if the device claims to | 
|---|
| 559 | * support the command. | 
|---|
| 560 | */ | 
|---|
| 561 | int scsi_report_opcode(struct scsi_device *sdev, unsigned char *buffer, | 
|---|
| 562 | unsigned int len, unsigned char opcode, | 
|---|
| 563 | unsigned short sa) | 
|---|
| 564 | { | 
|---|
| 565 | unsigned char cmd[16]; | 
|---|
| 566 | struct scsi_sense_hdr sshdr; | 
|---|
| 567 | int result, request_len; | 
|---|
| 568 | const struct scsi_exec_args exec_args = { | 
|---|
| 569 | .sshdr = &sshdr, | 
|---|
| 570 | }; | 
|---|
| 571 |  | 
|---|
| 572 | if (sdev->no_report_opcodes || sdev->scsi_level < SCSI_SPC_3) | 
|---|
| 573 | return -EINVAL; | 
|---|
| 574 |  | 
|---|
| 575 | /* RSOC header + size of command we are asking about */ | 
|---|
| 576 | request_len = 4 + COMMAND_SIZE(opcode); | 
|---|
| 577 | if (request_len > len) { | 
|---|
| 578 | dev_warn_once(&sdev->sdev_gendev, | 
|---|
| 579 | "%s: len %u bytes, opcode 0x%02x needs %u\n", | 
|---|
| 580 | __func__, len, opcode, request_len); | 
|---|
| 581 | return -EINVAL; | 
|---|
| 582 | } | 
|---|
| 583 |  | 
|---|
| 584 | memset(s: cmd, c: 0, n: 16); | 
|---|
| 585 | cmd[0] = MAINTENANCE_IN; | 
|---|
| 586 | cmd[1] = MI_REPORT_SUPPORTED_OPERATION_CODES; | 
|---|
| 587 | if (!sa) { | 
|---|
| 588 | cmd[2] = 1;	/* One command format */ | 
|---|
| 589 | cmd[3] = opcode; | 
|---|
| 590 | } else { | 
|---|
| 591 | cmd[2] = 3;	/* One command format with service action */ | 
|---|
| 592 | cmd[3] = opcode; | 
|---|
| 593 | put_unaligned_be16(val: sa, p: &cmd[4]); | 
|---|
| 594 | } | 
|---|
| 595 | put_unaligned_be32(val: request_len, p: &cmd[6]); | 
|---|
| 596 | memset(s: buffer, c: 0, n: len); | 
|---|
| 597 |  | 
|---|
| 598 | result = scsi_execute_cmd(sdev, cmd, opf: REQ_OP_DRV_IN, buffer, | 
|---|
| 599 | bufflen: request_len, timeout: 30 * HZ, retries: 3, args: &exec_args); | 
|---|
| 600 | if (result < 0) | 
|---|
| 601 | return result; | 
|---|
| 602 | if (result && scsi_sense_valid(sshdr: &sshdr) && | 
|---|
| 603 | sshdr.sense_key == ILLEGAL_REQUEST && | 
|---|
| 604 | (sshdr.asc == 0x20 || sshdr.asc == 0x24) && sshdr.ascq == 0x00) | 
|---|
| 605 | return -EINVAL; | 
|---|
| 606 |  | 
|---|
| 607 | if ((buffer[1] & 3) == 3) /* Command supported */ | 
|---|
| 608 | return 1; | 
|---|
| 609 |  | 
|---|
| 610 | return 0; | 
|---|
| 611 | } | 
|---|
| 612 | EXPORT_SYMBOL(scsi_report_opcode); | 
|---|
| 613 |  | 
|---|
| 614 | #define SCSI_CDL_CHECK_BUF_LEN	64 | 
|---|
| 615 |  | 
|---|
| 616 | static bool scsi_cdl_check_cmd(struct scsi_device *sdev, u8 opcode, u16 sa, | 
|---|
| 617 | unsigned char *buf) | 
|---|
| 618 | { | 
|---|
| 619 | int ret; | 
|---|
| 620 | u8 cdlp; | 
|---|
| 621 |  | 
|---|
| 622 | /* Check operation code */ | 
|---|
| 623 | ret = scsi_report_opcode(sdev, buf, SCSI_CDL_CHECK_BUF_LEN, opcode, sa); | 
|---|
| 624 | if (ret <= 0) | 
|---|
| 625 | return false; | 
|---|
| 626 |  | 
|---|
| 627 | if ((buf[1] & 0x03) != 0x03) | 
|---|
| 628 | return false; | 
|---|
| 629 |  | 
|---|
| 630 | /* | 
|---|
| 631 | * See SPC-6, One_command parameter data format for | 
|---|
| 632 | * REPORT SUPPORTED OPERATION CODES. We have the following cases | 
|---|
| 633 | * depending on rwcdlp (buf[0] & 0x01) value: | 
|---|
| 634 | *  - rwcdlp == 0: then cdlp indicates support for the A mode page when | 
|---|
| 635 | *		   it is equal to 1 and for the B mode page when it is | 
|---|
| 636 | *		   equal to 2. | 
|---|
| 637 | *  - rwcdlp == 1: then cdlp indicates support for the T2A mode page | 
|---|
| 638 | *		   when it is equal to 1 and for the T2B mode page when | 
|---|
| 639 | *		   it is equal to 2. | 
|---|
| 640 | * Overall, to detect support for command duration limits, we only need | 
|---|
| 641 | * to check that cdlp is 1 or 2. | 
|---|
| 642 | */ | 
|---|
| 643 | cdlp = (buf[1] & 0x18) >> 3; | 
|---|
| 644 |  | 
|---|
| 645 | return cdlp == 0x01 || cdlp == 0x02; | 
|---|
| 646 | } | 
|---|
| 647 |  | 
|---|
| 648 | /** | 
|---|
| 649 | * scsi_cdl_check - Check if a SCSI device supports Command Duration Limits | 
|---|
| 650 | * @sdev: The device to check | 
|---|
| 651 | */ | 
|---|
| 652 | void scsi_cdl_check(struct scsi_device *sdev) | 
|---|
| 653 | { | 
|---|
| 654 | bool cdl_supported; | 
|---|
| 655 | unsigned char *buf; | 
|---|
| 656 |  | 
|---|
| 657 | /* | 
|---|
| 658 | * Support for CDL was defined in SPC-5. Ignore devices reporting an | 
|---|
| 659 | * lower SPC version. This also avoids problems with old drives choking | 
|---|
| 660 | * on MAINTENANCE_IN / MI_REPORT_SUPPORTED_OPERATION_CODES with a | 
|---|
| 661 | * service action specified, as done in scsi_cdl_check_cmd(). | 
|---|
| 662 | */ | 
|---|
| 663 | if (sdev->scsi_level < SCSI_SPC_5) { | 
|---|
| 664 | sdev->cdl_supported = 0; | 
|---|
| 665 | return; | 
|---|
| 666 | } | 
|---|
| 667 |  | 
|---|
| 668 | buf = kmalloc(SCSI_CDL_CHECK_BUF_LEN, GFP_KERNEL); | 
|---|
| 669 | if (!buf) { | 
|---|
| 670 | sdev->cdl_supported = 0; | 
|---|
| 671 | return; | 
|---|
| 672 | } | 
|---|
| 673 |  | 
|---|
| 674 | /* Check support for READ_16, WRITE_16, READ_32 and WRITE_32 commands */ | 
|---|
| 675 | cdl_supported = | 
|---|
| 676 | scsi_cdl_check_cmd(sdev, READ_16, sa: 0, buf) || | 
|---|
| 677 | scsi_cdl_check_cmd(sdev, WRITE_16, sa: 0, buf) || | 
|---|
| 678 | scsi_cdl_check_cmd(sdev, VARIABLE_LENGTH_CMD, READ_32, buf) || | 
|---|
| 679 | scsi_cdl_check_cmd(sdev, VARIABLE_LENGTH_CMD, WRITE_32, buf); | 
|---|
| 680 | if (cdl_supported) { | 
|---|
| 681 | /* | 
|---|
| 682 | * We have CDL support: force the use of READ16/WRITE16. | 
|---|
| 683 | * READ32 and WRITE32 will be used for devices that support | 
|---|
| 684 | * the T10_PI_TYPE2_PROTECTION protection type. | 
|---|
| 685 | */ | 
|---|
| 686 | sdev->use_16_for_rw = 1; | 
|---|
| 687 | sdev->use_10_for_rw = 0; | 
|---|
| 688 |  | 
|---|
| 689 | sdev->cdl_supported = 1; | 
|---|
| 690 |  | 
|---|
| 691 | /* | 
|---|
| 692 | * If the device supports CDL, make sure that the current drive | 
|---|
| 693 | * feature status is consistent with the user controlled | 
|---|
| 694 | * cdl_enable state. | 
|---|
| 695 | */ | 
|---|
| 696 | scsi_cdl_enable(sdev, enable: sdev->cdl_enable); | 
|---|
| 697 | } else { | 
|---|
| 698 | sdev->cdl_supported = 0; | 
|---|
| 699 | } | 
|---|
| 700 |  | 
|---|
| 701 | kfree(objp: buf); | 
|---|
| 702 | } | 
|---|
| 703 |  | 
|---|
| 704 | /** | 
|---|
| 705 | * scsi_cdl_enable - Enable or disable a SCSI device supports for Command | 
|---|
| 706 | *                   Duration Limits | 
|---|
| 707 | * @sdev: The target device | 
|---|
| 708 | * @enable: the target state | 
|---|
| 709 | */ | 
|---|
| 710 | int scsi_cdl_enable(struct scsi_device *sdev, bool enable) | 
|---|
| 711 | { | 
|---|
| 712 | char buf[64]; | 
|---|
| 713 | int ret; | 
|---|
| 714 |  | 
|---|
| 715 | if (!sdev->cdl_supported) | 
|---|
| 716 | return -EOPNOTSUPP; | 
|---|
| 717 |  | 
|---|
| 718 | /* | 
|---|
| 719 | * For ATA devices, CDL needs to be enabled with a SET FEATURES command. | 
|---|
| 720 | */ | 
|---|
| 721 | if (sdev->is_ata) { | 
|---|
| 722 | struct scsi_mode_data data; | 
|---|
| 723 | struct scsi_sense_hdr sshdr; | 
|---|
| 724 | char *buf_data; | 
|---|
| 725 | int len; | 
|---|
| 726 |  | 
|---|
| 727 | ret = scsi_mode_sense(sdev, dbd: 0x08, modepage: 0x0a, subpage: 0xf2, buffer: buf, len: sizeof(buf), | 
|---|
| 728 | timeout: 5 * HZ, retries: 3, data: &data, NULL); | 
|---|
| 729 | if (ret) | 
|---|
| 730 | return -EINVAL; | 
|---|
| 731 |  | 
|---|
| 732 | /* Enable or disable CDL using the ATA feature page */ | 
|---|
| 733 | len = min_t(size_t, sizeof(buf), | 
|---|
| 734 | data.length - data.header_length - | 
|---|
| 735 | data.block_descriptor_length); | 
|---|
| 736 | buf_data = buf + data.header_length + | 
|---|
| 737 | data.block_descriptor_length; | 
|---|
| 738 |  | 
|---|
| 739 | /* | 
|---|
| 740 | * If we want to enable CDL and CDL is already enabled on the | 
|---|
| 741 | * device, do nothing. This avoids needlessly resetting the CDL | 
|---|
| 742 | * statistics on the device as that is implied by the CDL enable | 
|---|
| 743 | * action. Similar to this, there is no need to do anything if | 
|---|
| 744 | * we want to disable CDL and CDL is already disabled. | 
|---|
| 745 | */ | 
|---|
| 746 | if (enable) { | 
|---|
| 747 | if ((buf_data[4] & 0x03) == 0x02) | 
|---|
| 748 | goto out; | 
|---|
| 749 | buf_data[4] &= ~0x03; | 
|---|
| 750 | buf_data[4] |= 0x02; | 
|---|
| 751 | } else { | 
|---|
| 752 | if ((buf_data[4] & 0x03) == 0x00) | 
|---|
| 753 | goto out; | 
|---|
| 754 | buf_data[4] &= ~0x03; | 
|---|
| 755 | } | 
|---|
| 756 |  | 
|---|
| 757 | ret = scsi_mode_select(sdev, pf: 1, sp: 0, buffer: buf_data, len, timeout: 5 * HZ, retries: 3, | 
|---|
| 758 | data: &data, &sshdr); | 
|---|
| 759 | if (ret) { | 
|---|
| 760 | if (ret > 0 && scsi_sense_valid(sshdr: &sshdr)) | 
|---|
| 761 | scsi_print_sense_hdr(sdev, | 
|---|
| 762 | dev_name(dev: &sdev->sdev_gendev), &sshdr); | 
|---|
| 763 | return ret; | 
|---|
| 764 | } | 
|---|
| 765 | } | 
|---|
| 766 |  | 
|---|
| 767 | out: | 
|---|
| 768 | sdev->cdl_enable = enable; | 
|---|
| 769 |  | 
|---|
| 770 | return 0; | 
|---|
| 771 | } | 
|---|
| 772 |  | 
|---|
| 773 | /** | 
|---|
| 774 | * scsi_device_get  -  get an additional reference to a scsi_device | 
|---|
| 775 | * @sdev:	device to get a reference to | 
|---|
| 776 | * | 
|---|
| 777 | * Description: Gets a reference to the scsi_device and increments the use count | 
|---|
| 778 | * of the underlying LLDD module.  You must hold host_lock of the | 
|---|
| 779 | * parent Scsi_Host or already have a reference when calling this. | 
|---|
| 780 | * | 
|---|
| 781 | * This will fail if a device is deleted or cancelled, or when the LLD module | 
|---|
| 782 | * is in the process of being unloaded. | 
|---|
| 783 | */ | 
|---|
| 784 | int scsi_device_get(struct scsi_device *sdev) | 
|---|
| 785 | { | 
|---|
| 786 | if (sdev->sdev_state == SDEV_DEL || sdev->sdev_state == SDEV_CANCEL) | 
|---|
| 787 | goto fail; | 
|---|
| 788 | if (!try_module_get(module: sdev->host->hostt->module)) | 
|---|
| 789 | goto fail; | 
|---|
| 790 | if (!get_device(dev: &sdev->sdev_gendev)) | 
|---|
| 791 | goto fail_put_module; | 
|---|
| 792 | return 0; | 
|---|
| 793 |  | 
|---|
| 794 | fail_put_module: | 
|---|
| 795 | module_put(module: sdev->host->hostt->module); | 
|---|
| 796 | fail: | 
|---|
| 797 | return -ENXIO; | 
|---|
| 798 | } | 
|---|
| 799 | EXPORT_SYMBOL(scsi_device_get); | 
|---|
| 800 |  | 
|---|
| 801 | /** | 
|---|
| 802 | * scsi_device_put  -  release a reference to a scsi_device | 
|---|
| 803 | * @sdev:	device to release a reference on. | 
|---|
| 804 | * | 
|---|
| 805 | * Description: Release a reference to the scsi_device and decrements the use | 
|---|
| 806 | * count of the underlying LLDD module.  The device is freed once the last | 
|---|
| 807 | * user vanishes. | 
|---|
| 808 | */ | 
|---|
| 809 | void scsi_device_put(struct scsi_device *sdev) | 
|---|
| 810 | { | 
|---|
| 811 | struct module *mod = sdev->host->hostt->module; | 
|---|
| 812 |  | 
|---|
| 813 | put_device(dev: &sdev->sdev_gendev); | 
|---|
| 814 | module_put(module: mod); | 
|---|
| 815 | } | 
|---|
| 816 | EXPORT_SYMBOL(scsi_device_put); | 
|---|
| 817 |  | 
|---|
| 818 | /* helper for shost_for_each_device, see that for documentation */ | 
|---|
| 819 | struct scsi_device *__scsi_iterate_devices(struct Scsi_Host *shost, | 
|---|
| 820 | struct scsi_device *prev) | 
|---|
| 821 | { | 
|---|
| 822 | struct list_head *list = (prev ? &prev->siblings : &shost->__devices); | 
|---|
| 823 | struct scsi_device *next = NULL; | 
|---|
| 824 | unsigned long flags; | 
|---|
| 825 |  | 
|---|
| 826 | spin_lock_irqsave(shost->host_lock, flags); | 
|---|
| 827 | while (list->next != &shost->__devices) { | 
|---|
| 828 | next = list_entry(list->next, struct scsi_device, siblings); | 
|---|
| 829 | /* skip devices that we can't get a reference to */ | 
|---|
| 830 | if (!scsi_device_get(next)) | 
|---|
| 831 | break; | 
|---|
| 832 | next = NULL; | 
|---|
| 833 | list = list->next; | 
|---|
| 834 | } | 
|---|
| 835 | spin_unlock_irqrestore(lock: shost->host_lock, flags); | 
|---|
| 836 |  | 
|---|
| 837 | if (prev) | 
|---|
| 838 | scsi_device_put(prev); | 
|---|
| 839 | return next; | 
|---|
| 840 | } | 
|---|
| 841 | EXPORT_SYMBOL(__scsi_iterate_devices); | 
|---|
| 842 |  | 
|---|
| 843 | /** | 
|---|
| 844 | * starget_for_each_device  -  helper to walk all devices of a target | 
|---|
| 845 | * @starget:	target whose devices we want to iterate over. | 
|---|
| 846 | * @data:	Opaque passed to each function call. | 
|---|
| 847 | * @fn:		Function to call on each device | 
|---|
| 848 | * | 
|---|
| 849 | * This traverses over each device of @starget.  The devices have | 
|---|
| 850 | * a reference that must be released by scsi_host_put when breaking | 
|---|
| 851 | * out of the loop. | 
|---|
| 852 | */ | 
|---|
| 853 | void starget_for_each_device(struct scsi_target *starget, void *data, | 
|---|
| 854 | void (*fn)(struct scsi_device *, void *)) | 
|---|
| 855 | { | 
|---|
| 856 | struct Scsi_Host *shost = dev_to_shost(dev: starget->dev.parent); | 
|---|
| 857 | struct scsi_device *sdev; | 
|---|
| 858 |  | 
|---|
| 859 | shost_for_each_device(sdev, shost) { | 
|---|
| 860 | if ((sdev->channel == starget->channel) && | 
|---|
| 861 | (sdev->id == starget->id)) | 
|---|
| 862 | fn(sdev, data); | 
|---|
| 863 | } | 
|---|
| 864 | } | 
|---|
| 865 | EXPORT_SYMBOL(starget_for_each_device); | 
|---|
| 866 |  | 
|---|
| 867 | /** | 
|---|
| 868 | * __starget_for_each_device - helper to walk all devices of a target (UNLOCKED) | 
|---|
| 869 | * @starget:	target whose devices we want to iterate over. | 
|---|
| 870 | * @data:	parameter for callback @fn() | 
|---|
| 871 | * @fn:		callback function that is invoked for each device | 
|---|
| 872 | * | 
|---|
| 873 | * This traverses over each device of @starget.  It does _not_ | 
|---|
| 874 | * take a reference on the scsi_device, so the whole loop must be | 
|---|
| 875 | * protected by shost->host_lock. | 
|---|
| 876 | * | 
|---|
| 877 | * Note:  The only reason why drivers would want to use this is because | 
|---|
| 878 | * they need to access the device list in irq context.  Otherwise you | 
|---|
| 879 | * really want to use starget_for_each_device instead. | 
|---|
| 880 | **/ | 
|---|
| 881 | void __starget_for_each_device(struct scsi_target *starget, void *data, | 
|---|
| 882 | void (*fn)(struct scsi_device *, void *)) | 
|---|
| 883 | { | 
|---|
| 884 | struct Scsi_Host *shost = dev_to_shost(dev: starget->dev.parent); | 
|---|
| 885 | struct scsi_device *sdev; | 
|---|
| 886 |  | 
|---|
| 887 | __shost_for_each_device(sdev, shost) { | 
|---|
| 888 | if ((sdev->channel == starget->channel) && | 
|---|
| 889 | (sdev->id == starget->id)) | 
|---|
| 890 | fn(sdev, data); | 
|---|
| 891 | } | 
|---|
| 892 | } | 
|---|
| 893 | EXPORT_SYMBOL(__starget_for_each_device); | 
|---|
| 894 |  | 
|---|
| 895 | /** | 
|---|
| 896 | * __scsi_device_lookup_by_target - find a device given the target (UNLOCKED) | 
|---|
| 897 | * @starget:	SCSI target pointer | 
|---|
| 898 | * @lun:	SCSI Logical Unit Number | 
|---|
| 899 | * | 
|---|
| 900 | * Description: Looks up the scsi_device with the specified @lun for a given | 
|---|
| 901 | * @starget.  The returned scsi_device does not have an additional | 
|---|
| 902 | * reference.  You must hold the host's host_lock over this call and | 
|---|
| 903 | * any access to the returned scsi_device. A scsi_device in state | 
|---|
| 904 | * SDEV_DEL is skipped. | 
|---|
| 905 | * | 
|---|
| 906 | * Note:  The only reason why drivers should use this is because | 
|---|
| 907 | * they need to access the device list in irq context.  Otherwise you | 
|---|
| 908 | * really want to use scsi_device_lookup_by_target instead. | 
|---|
| 909 | **/ | 
|---|
| 910 | struct scsi_device *__scsi_device_lookup_by_target(struct scsi_target *starget, | 
|---|
| 911 | u64 lun) | 
|---|
| 912 | { | 
|---|
| 913 | struct scsi_device *sdev; | 
|---|
| 914 |  | 
|---|
| 915 | list_for_each_entry(sdev, &starget->devices, same_target_siblings) { | 
|---|
| 916 | if (sdev->sdev_state == SDEV_DEL) | 
|---|
| 917 | continue; | 
|---|
| 918 | if (sdev->lun ==lun) | 
|---|
| 919 | return sdev; | 
|---|
| 920 | } | 
|---|
| 921 |  | 
|---|
| 922 | return NULL; | 
|---|
| 923 | } | 
|---|
| 924 | EXPORT_SYMBOL(__scsi_device_lookup_by_target); | 
|---|
| 925 |  | 
|---|
| 926 | /** | 
|---|
| 927 | * scsi_device_lookup_by_target - find a device given the target | 
|---|
| 928 | * @starget:	SCSI target pointer | 
|---|
| 929 | * @lun:	SCSI Logical Unit Number | 
|---|
| 930 | * | 
|---|
| 931 | * Description: Looks up the scsi_device with the specified @lun for a given | 
|---|
| 932 | * @starget.  The returned scsi_device has an additional reference that | 
|---|
| 933 | * needs to be released with scsi_device_put once you're done with it. | 
|---|
| 934 | **/ | 
|---|
| 935 | struct scsi_device *scsi_device_lookup_by_target(struct scsi_target *starget, | 
|---|
| 936 | u64 lun) | 
|---|
| 937 | { | 
|---|
| 938 | struct scsi_device *sdev; | 
|---|
| 939 | struct Scsi_Host *shost = dev_to_shost(dev: starget->dev.parent); | 
|---|
| 940 | unsigned long flags; | 
|---|
| 941 |  | 
|---|
| 942 | spin_lock_irqsave(shost->host_lock, flags); | 
|---|
| 943 | sdev = __scsi_device_lookup_by_target(starget, lun); | 
|---|
| 944 | if (sdev && scsi_device_get(sdev)) | 
|---|
| 945 | sdev = NULL; | 
|---|
| 946 | spin_unlock_irqrestore(lock: shost->host_lock, flags); | 
|---|
| 947 |  | 
|---|
| 948 | return sdev; | 
|---|
| 949 | } | 
|---|
| 950 | EXPORT_SYMBOL(scsi_device_lookup_by_target); | 
|---|
| 951 |  | 
|---|
| 952 | /** | 
|---|
| 953 | * __scsi_device_lookup - find a device given the host (UNLOCKED) | 
|---|
| 954 | * @shost:	SCSI host pointer | 
|---|
| 955 | * @channel:	SCSI channel (zero if only one channel) | 
|---|
| 956 | * @id:		SCSI target number (physical unit number) | 
|---|
| 957 | * @lun:	SCSI Logical Unit Number | 
|---|
| 958 | * | 
|---|
| 959 | * Description: Looks up the scsi_device with the specified @channel, @id, @lun | 
|---|
| 960 | * for a given host. The returned scsi_device does not have an additional | 
|---|
| 961 | * reference.  You must hold the host's host_lock over this call and any access | 
|---|
| 962 | * to the returned scsi_device. | 
|---|
| 963 | * | 
|---|
| 964 | * Note:  The only reason why drivers would want to use this is because | 
|---|
| 965 | * they need to access the device list in irq context.  Otherwise you | 
|---|
| 966 | * really want to use scsi_device_lookup instead. | 
|---|
| 967 | **/ | 
|---|
| 968 | struct scsi_device *__scsi_device_lookup(struct Scsi_Host *shost, | 
|---|
| 969 | uint channel, uint id, u64 lun) | 
|---|
| 970 | { | 
|---|
| 971 | struct scsi_device *sdev; | 
|---|
| 972 |  | 
|---|
| 973 | list_for_each_entry(sdev, &shost->__devices, siblings) { | 
|---|
| 974 | if (sdev->sdev_state == SDEV_DEL) | 
|---|
| 975 | continue; | 
|---|
| 976 | if (sdev->channel == channel && sdev->id == id && | 
|---|
| 977 | sdev->lun ==lun) | 
|---|
| 978 | return sdev; | 
|---|
| 979 | } | 
|---|
| 980 |  | 
|---|
| 981 | return NULL; | 
|---|
| 982 | } | 
|---|
| 983 | EXPORT_SYMBOL(__scsi_device_lookup); | 
|---|
| 984 |  | 
|---|
| 985 | /** | 
|---|
| 986 | * scsi_device_lookup - find a device given the host | 
|---|
| 987 | * @shost:	SCSI host pointer | 
|---|
| 988 | * @channel:	SCSI channel (zero if only one channel) | 
|---|
| 989 | * @id:		SCSI target number (physical unit number) | 
|---|
| 990 | * @lun:	SCSI Logical Unit Number | 
|---|
| 991 | * | 
|---|
| 992 | * Description: Looks up the scsi_device with the specified @channel, @id, @lun | 
|---|
| 993 | * for a given host.  The returned scsi_device has an additional reference that | 
|---|
| 994 | * needs to be released with scsi_device_put once you're done with it. | 
|---|
| 995 | **/ | 
|---|
| 996 | struct scsi_device *scsi_device_lookup(struct Scsi_Host *shost, | 
|---|
| 997 | uint channel, uint id, u64 lun) | 
|---|
| 998 | { | 
|---|
| 999 | struct scsi_device *sdev; | 
|---|
| 1000 | unsigned long flags; | 
|---|
| 1001 |  | 
|---|
| 1002 | spin_lock_irqsave(shost->host_lock, flags); | 
|---|
| 1003 | sdev = __scsi_device_lookup(shost, channel, id, lun); | 
|---|
| 1004 | if (sdev && scsi_device_get(sdev)) | 
|---|
| 1005 | sdev = NULL; | 
|---|
| 1006 | spin_unlock_irqrestore(lock: shost->host_lock, flags); | 
|---|
| 1007 |  | 
|---|
| 1008 | return sdev; | 
|---|
| 1009 | } | 
|---|
| 1010 | EXPORT_SYMBOL(scsi_device_lookup); | 
|---|
| 1011 |  | 
|---|
| 1012 | MODULE_DESCRIPTION( "SCSI core"); | 
|---|
| 1013 | MODULE_LICENSE( "GPL"); | 
|---|
| 1014 |  | 
|---|
| 1015 | module_param(scsi_logging_level, int, S_IRUGO|S_IWUSR); | 
|---|
| 1016 | MODULE_PARM_DESC(scsi_logging_level, "a bit mask of logging levels"); | 
|---|
| 1017 |  | 
|---|
| 1018 | static int __init init_scsi(void) | 
|---|
| 1019 | { | 
|---|
| 1020 | int error; | 
|---|
| 1021 |  | 
|---|
| 1022 | error = scsi_init_procfs(); | 
|---|
| 1023 | if (error) | 
|---|
| 1024 | goto cleanup_queue; | 
|---|
| 1025 | error = scsi_init_devinfo(); | 
|---|
| 1026 | if (error) | 
|---|
| 1027 | goto cleanup_procfs; | 
|---|
| 1028 | error = scsi_init_hosts(); | 
|---|
| 1029 | if (error) | 
|---|
| 1030 | goto cleanup_devlist; | 
|---|
| 1031 | error = scsi_init_sysctl(); | 
|---|
| 1032 | if (error) | 
|---|
| 1033 | goto cleanup_hosts; | 
|---|
| 1034 | error = scsi_sysfs_register(); | 
|---|
| 1035 | if (error) | 
|---|
| 1036 | goto cleanup_sysctl; | 
|---|
| 1037 |  | 
|---|
| 1038 | scsi_netlink_init(); | 
|---|
| 1039 |  | 
|---|
| 1040 | printk(KERN_NOTICE "SCSI subsystem initialized\n"); | 
|---|
| 1041 | return 0; | 
|---|
| 1042 |  | 
|---|
| 1043 | cleanup_sysctl: | 
|---|
| 1044 | scsi_exit_sysctl(); | 
|---|
| 1045 | cleanup_hosts: | 
|---|
| 1046 | scsi_exit_hosts(); | 
|---|
| 1047 | cleanup_devlist: | 
|---|
| 1048 | scsi_exit_devinfo(); | 
|---|
| 1049 | cleanup_procfs: | 
|---|
| 1050 | scsi_exit_procfs(); | 
|---|
| 1051 | cleanup_queue: | 
|---|
| 1052 | scsi_exit_queue(); | 
|---|
| 1053 | printk(KERN_ERR "SCSI subsystem failed to initialize, error = %d\n", | 
|---|
| 1054 | -error); | 
|---|
| 1055 | return error; | 
|---|
| 1056 | } | 
|---|
| 1057 |  | 
|---|
| 1058 | static void __exit exit_scsi(void) | 
|---|
| 1059 | { | 
|---|
| 1060 | scsi_netlink_exit(); | 
|---|
| 1061 | scsi_sysfs_unregister(); | 
|---|
| 1062 | scsi_exit_sysctl(); | 
|---|
| 1063 | scsi_exit_hosts(); | 
|---|
| 1064 | scsi_exit_devinfo(); | 
|---|
| 1065 | scsi_exit_procfs(); | 
|---|
| 1066 | scsi_exit_queue(); | 
|---|
| 1067 | } | 
|---|
| 1068 |  | 
|---|
| 1069 | subsys_initcall(init_scsi); | 
|---|
| 1070 | module_exit(exit_scsi); | 
|---|
| 1071 |  | 
|---|