Line data Source code
1 : /*
2 : * sd.c Copyright (C) 1992 Drew Eckhardt
3 : * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
4 : *
5 : * Linux scsi disk driver
6 : * Initial versions: Drew Eckhardt
7 : * Subsequent revisions: Eric Youngdale
8 : * Modification history:
9 : * - Drew Eckhardt <drew@colorado.edu> original
10 : * - Eric Youngdale <eric@andante.org> add scatter-gather, multiple
11 : * outstanding request, and other enhancements.
12 : * Support loadable low-level scsi drivers.
13 : * - Jirka Hanika <geo@ff.cuni.cz> support more scsi disks using
14 : * eight major numbers.
15 : * - Richard Gooch <rgooch@atnf.csiro.au> support devfs.
16 : * - Torben Mathiasen <tmm@image.dk> Resource allocation fixes in
17 : * sd_init and cleanups.
18 : * - Alex Davis <letmein@erols.com> Fix problem where partition info
19 : * not being read in sd_open. Fix problem where removable media
20 : * could be ejected after sd_open.
21 : * - Douglas Gilbert <dgilbert@interlog.com> cleanup for lk 2.5.x
22 : * - Badari Pulavarty <pbadari@us.ibm.com>, Matthew Wilcox
23 : * <willy@debian.org>, Kurt Garloff <garloff@suse.de>:
24 : * Support 32k/1M disks.
25 : *
26 : * Logging policy (needs CONFIG_SCSI_LOGGING defined):
27 : * - setting up transfer: SCSI_LOG_HLQUEUE levels 1 and 2
28 : * - end of transfer (bh + scsi_lib): SCSI_LOG_HLCOMPLETE level 1
29 : * - entering sd_ioctl: SCSI_LOG_IOCTL level 1
30 : * - entering other commands: SCSI_LOG_HLQUEUE level 3
31 : * Note: when the logging level is set by the user, it must be greater
32 : * than the level indicated above to trigger output.
33 : */
34 :
35 : #include <linux/module.h>
36 : #include <linux/fs.h>
37 : #include <linux/kernel.h>
38 : #include <linux/mm.h>
39 : #include <linux/bio.h>
40 : #include <linux/genhd.h>
41 : #include <linux/hdreg.h>
42 : #include <linux/errno.h>
43 : #include <linux/idr.h>
44 : #include <linux/interrupt.h>
45 : #include <linux/init.h>
46 : #include <linux/blkdev.h>
47 : #include <linux/blkpg.h>
48 : #include <linux/delay.h>
49 : #include <linux/mutex.h>
50 : #include <linux/string_helpers.h>
51 : #include <linux/async.h>
52 : #include <asm/uaccess.h>
53 : #include <asm/unaligned.h>
54 :
55 : #include <scsi/scsi.h>
56 : #include <scsi/scsi_cmnd.h>
57 : #include <scsi/scsi_dbg.h>
58 : #include <scsi/scsi_device.h>
59 : #include <scsi/scsi_driver.h>
60 : #include <scsi/scsi_eh.h>
61 : #include <scsi/scsi_host.h>
62 : #include <scsi/scsi_ioctl.h>
63 : #include <scsi/scsicam.h>
64 :
65 : #include "sd.h"
66 : #include "scsi_logging.h"
67 :
68 : MODULE_AUTHOR("Eric Youngdale");
69 : MODULE_DESCRIPTION("SCSI disk (sd) driver");
70 : MODULE_LICENSE("GPL");
71 :
72 : MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR);
73 : MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR);
74 : MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR);
75 : MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR);
76 : MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR);
77 : MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR);
78 : MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR);
79 : MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR);
80 : MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR);
81 : MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR);
82 : MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR);
83 : MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR);
84 : MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR);
85 : MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR);
86 : MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR);
87 : MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR);
88 : MODULE_ALIAS_SCSI_DEVICE(TYPE_DISK);
89 : MODULE_ALIAS_SCSI_DEVICE(TYPE_MOD);
90 : MODULE_ALIAS_SCSI_DEVICE(TYPE_RBC);
91 :
92 : #if !defined(CONFIG_DEBUG_BLOCK_EXT_DEVT)
93 : #define SD_MINORS 16
94 : #else
95 : #define SD_MINORS 0
96 : #endif
97 :
98 : static int sd_revalidate_disk(struct gendisk *);
99 : static int sd_probe(struct device *);
100 : static int sd_remove(struct device *);
101 : static void sd_shutdown(struct device *);
102 : static int sd_suspend(struct device *, pm_message_t state);
103 : static int sd_resume(struct device *);
104 : static void sd_rescan(struct device *);
105 : static int sd_done(struct scsi_cmnd *);
106 : static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer);
107 : static void scsi_disk_release(struct device *cdev);
108 : static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *);
109 : static void sd_print_result(struct scsi_disk *, int);
110 :
111 1 : static DEFINE_SPINLOCK(sd_index_lock);
112 1 : static DEFINE_IDA(sd_index_ida);
113 :
114 : /* This semaphore is used to mediate the 0->1 reference get in the
115 : * face of object destruction (i.e. we can't allow a get on an
116 : * object after last put) */
117 1 : static DEFINE_MUTEX(sd_ref_mutex);
118 :
119 1 : struct kmem_cache *sd_cdb_cache;
120 1 : mempool_t *sd_cdb_pool;
121 :
122 1 : static const char *sd_cache_types[] = {
123 : "write through", "none", "write back",
124 : "write back, no read (daft)"
125 : };
126 :
127 : static ssize_t
128 : sd_store_cache_type(struct device *dev, struct device_attribute *attr,
129 : const char *buf, size_t count)
130 : {
131 2 : int i, ct = -1, rcd, wce, sp;
132 3 : struct scsi_disk *sdkp = to_scsi_disk(dev);
133 2 : struct scsi_device *sdp = sdkp->device;
134 1 : char buffer[64];
135 1 : char *buffer_data;
136 1 : struct scsi_mode_data data;
137 1 : struct scsi_sense_hdr sshdr;
138 1 : int len;
139 1 :
140 4 : if (sdp->type != TYPE_DISK)
141 1 : /* no cache control on RBC devices; theoretically they
142 1 : * can do it, but there's probably so many exceptions
143 1 : * it's not worth the risk */
144 2 : return -EINVAL;
145 1 :
146 6 : for (i = 0; i < ARRAY_SIZE(sd_cache_types); i++) {
147 5 : const int len = strlen(sd_cache_types[i]);
148 7 : if (strncmp(sd_cache_types[i], buf, len) == 0 &&
149 1 : buf[len] == '\n') {
150 2 : ct = i;
151 2 : break;
152 1 : }
153 : }
154 2 : if (ct < 0)
155 1 : return -EINVAL;
156 1 : rcd = ct & 0x01 ? 1 : 0;
157 1 : wce = ct & 0x02 ? 1 : 0;
158 3 : if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT,
159 : SD_MAX_RETRIES, &data, NULL))
160 1 : return -EINVAL;
161 8 : len = min_t(size_t, sizeof(buffer), data.length - data.header_length -
162 : data.block_descriptor_length);
163 1 : buffer_data = buffer + data.header_length +
164 : data.block_descriptor_length;
165 1 : buffer_data[2] &= ~0x05;
166 1 : buffer_data[2] |= wce << 2 | rcd;
167 1 : sp = buffer_data[0] & 0x80 ? 1 : 0;
168 :
169 3 : if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT,
170 : SD_MAX_RETRIES, &data, &sshdr)) {
171 4 : if (scsi_sense_valid(&sshdr))
172 3 : sd_print_sense_hdr(sdkp, &sshdr);
173 2 : return -EINVAL;
174 : }
175 1 : revalidate_disk(sdkp->disk);
176 1 : return count;
177 : }
178 :
179 : static ssize_t
180 : sd_store_manage_start_stop(struct device *dev, struct device_attribute *attr,
181 : const char *buf, size_t count)
182 : {
183 15 : struct scsi_disk *sdkp = to_scsi_disk(dev);
184 10 : struct scsi_device *sdp = sdkp->device;
185 5 :
186 20 : if (!capable(CAP_SYS_ADMIN))
187 10 : return -EACCES;
188 :
189 10 : sdp->manage_start_stop = simple_strtoul(buf, NULL, 10);
190 :
191 5 : return count;
192 : }
193 :
194 : static ssize_t
195 : sd_store_allow_restart(struct device *dev, struct device_attribute *attr,
196 : const char *buf, size_t count)
197 : {
198 3 : struct scsi_disk *sdkp = to_scsi_disk(dev);
199 2 : struct scsi_device *sdp = sdkp->device;
200 1 :
201 4 : if (!capable(CAP_SYS_ADMIN))
202 2 : return -EACCES;
203 :
204 3 : if (sdp->type != TYPE_DISK)
205 1 : return -EINVAL;
206 :
207 2 : sdp->allow_restart = simple_strtoul(buf, NULL, 10);
208 :
209 1 : return count;
210 : }
211 :
212 : static ssize_t
213 : sd_show_cache_type(struct device *dev, struct device_attribute *attr,
214 : char *buf)
215 : {
216 3 : struct scsi_disk *sdkp = to_scsi_disk(dev);
217 4 : int ct = sdkp->RCD + 2*sdkp->WCE;
218 1 :
219 3 : return snprintf(buf, 40, "%s\n", sd_cache_types[ct]);
220 : }
221 :
222 : static ssize_t
223 : sd_show_fua(struct device *dev, struct device_attribute *attr, char *buf)
224 : {
225 3 : struct scsi_disk *sdkp = to_scsi_disk(dev);
226 1 :
227 4 : return snprintf(buf, 20, "%u\n", sdkp->DPOFUA);
228 : }
229 :
230 : static ssize_t
231 : sd_show_manage_start_stop(struct device *dev, struct device_attribute *attr,
232 : char *buf)
233 : {
234 3 : struct scsi_disk *sdkp = to_scsi_disk(dev);
235 2 : struct scsi_device *sdp = sdkp->device;
236 1 :
237 4 : return snprintf(buf, 20, "%u\n", sdp->manage_start_stop);
238 : }
239 :
240 : static ssize_t
241 : sd_show_allow_restart(struct device *dev, struct device_attribute *attr,
242 : char *buf)
243 : {
244 3 : struct scsi_disk *sdkp = to_scsi_disk(dev);
245 1 :
246 4 : return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart);
247 : }
248 :
249 : static ssize_t
250 : sd_show_protection_type(struct device *dev, struct device_attribute *attr,
251 : char *buf)
252 : {
253 3 : struct scsi_disk *sdkp = to_scsi_disk(dev);
254 1 :
255 4 : return snprintf(buf, 20, "%u\n", sdkp->protection_type);
256 : }
257 :
258 : static ssize_t
259 : sd_show_app_tag_own(struct device *dev, struct device_attribute *attr,
260 : char *buf)
261 : {
262 3 : struct scsi_disk *sdkp = to_scsi_disk(dev);
263 1 :
264 4 : return snprintf(buf, 20, "%u\n", sdkp->ATO);
265 : }
266 :
267 : static ssize_t
268 : sd_show_thin_provisioning(struct device *dev, struct device_attribute *attr,
269 : char *buf)
270 : {
271 3 : struct scsi_disk *sdkp = to_scsi_disk(dev);
272 1 :
273 4 : return snprintf(buf, 20, "%u\n", sdkp->thin_provisioning);
274 : }
275 :
276 1 : static struct device_attribute sd_disk_attrs[] = {
277 : __ATTR(cache_type, S_IRUGO|S_IWUSR, sd_show_cache_type,
278 : sd_store_cache_type),
279 : __ATTR(FUA, S_IRUGO, sd_show_fua, NULL),
280 : __ATTR(allow_restart, S_IRUGO|S_IWUSR, sd_show_allow_restart,
281 : sd_store_allow_restart),
282 : __ATTR(manage_start_stop, S_IRUGO|S_IWUSR, sd_show_manage_start_stop,
283 : sd_store_manage_start_stop),
284 : __ATTR(protection_type, S_IRUGO, sd_show_protection_type, NULL),
285 : __ATTR(app_tag_own, S_IRUGO, sd_show_app_tag_own, NULL),
286 : __ATTR(thin_provisioning, S_IRUGO, sd_show_thin_provisioning, NULL),
287 : __ATTR_NULL,
288 : };
289 :
290 1 : static struct class sd_disk_class = {
291 : .name = "scsi_disk",
292 : .owner = THIS_MODULE,
293 : .dev_release = scsi_disk_release,
294 : .dev_attrs = sd_disk_attrs,
295 : };
296 :
297 1 : static struct scsi_driver sd_template = {
298 : .owner = THIS_MODULE,
299 : .gendrv = {
300 : .name = "sd",
301 : .probe = sd_probe,
302 : .remove = sd_remove,
303 : .suspend = sd_suspend,
304 : .resume = sd_resume,
305 : .shutdown = sd_shutdown,
306 : },
307 : .rescan = sd_rescan,
308 : .done = sd_done,
309 : };
310 :
311 : /*
312 : * Device no to disk mapping:
313 : *
314 : * major disc2 disc p1
315 : * |............|.............|....|....| <- dev_t
316 : * 31 20 19 8 7 4 3 0
317 : *
318 : * Inside a major, we have 16k disks, however mapped non-
319 : * contiguously. The first 16 disks are for major0, the next
320 : * ones with major1, ... Disk 256 is for major0 again, disk 272
321 : * for major1, ...
322 : * As we stay compatible with our numbering scheme, we can reuse
323 : * the well-know SCSI majors 8, 65--71, 136--143.
324 : */
325 : static int sd_major(int major_idx)
326 : {
327 : switch (major_idx) {
328 12 : case 0:
329 4 : return SCSI_DISK0_MAJOR;
330 64 : case 1 ... 7:
331 8 : return SCSI_DISK1_MAJOR + major_idx - 1;
332 76 : case 8 ... 15:
333 12 : return SCSI_DISK8_MAJOR + major_idx - 8;
334 16 : default:
335 20 : BUG();
336 12 : return 0; /* shut up gcc */
337 4 : }
338 4 : }
339 4 :
340 : static struct scsi_disk *__scsi_disk_get(struct gendisk *disk)
341 : {
342 90 : struct scsi_disk *sdkp = NULL;
343 45 :
344 135 : if (disk->private_data) {
345 90 : sdkp = scsi_disk(disk);
346 135 : if (scsi_device_get(sdkp->device) == 0)
347 45 : get_device(&sdkp->dev);
348 : else
349 45 : sdkp = NULL;
350 : }
351 90 : return sdkp;
352 : }
353 :
354 : static struct scsi_disk *scsi_disk_get(struct gendisk *disk)
355 : {
356 7 : struct scsi_disk *sdkp;
357 :
358 7 : mutex_lock(&sd_ref_mutex);
359 21 : sdkp = __scsi_disk_get(disk);
360 7 : mutex_unlock(&sd_ref_mutex);
361 7 : return sdkp;
362 : }
363 :
364 : static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev)
365 : {
366 38 : struct scsi_disk *sdkp;
367 38 :
368 38 : mutex_lock(&sd_ref_mutex);
369 114 : sdkp = dev_get_drvdata(dev);
370 76 : if (sdkp)
371 114 : sdkp = __scsi_disk_get(sdkp->disk);
372 76 : mutex_unlock(&sd_ref_mutex);
373 76 : return sdkp;
374 : }
375 :
376 : static void scsi_disk_put(struct scsi_disk *sdkp)
377 : {
378 222 : struct scsi_device *sdev = sdkp->device;
379 :
380 111 : mutex_lock(&sd_ref_mutex);
381 111 : put_device(&sdkp->dev);
382 111 : scsi_device_put(sdev);
383 111 : mutex_unlock(&sd_ref_mutex);
384 111 : }
385 :
386 : static void sd_prot_op(struct scsi_cmnd *scmd, unsigned int dif)
387 : {
388 0 : unsigned int prot_op = SCSI_PROT_NORMAL;
389 0 : unsigned int dix = scsi_prot_sg_count(scmd);
390 0 :
391 0 : if (scmd->sc_data_direction == DMA_FROM_DEVICE) {
392 0 : if (dif && dix)
393 0 : prot_op = SCSI_PROT_READ_PASS;
394 0 : else if (dif && !dix)
395 0 : prot_op = SCSI_PROT_READ_STRIP;
396 0 : else if (!dif && dix)
397 0 : prot_op = SCSI_PROT_READ_INSERT;
398 : } else {
399 0 : if (dif && dix)
400 0 : prot_op = SCSI_PROT_WRITE_PASS;
401 0 : else if (dif && !dix)
402 0 : prot_op = SCSI_PROT_WRITE_INSERT;
403 0 : else if (!dif && dix)
404 0 : prot_op = SCSI_PROT_WRITE_STRIP;
405 : }
406 :
407 0 : scsi_set_prot_op(scmd, prot_op);
408 0 : scsi_set_prot_type(scmd, dif);
409 0 : }
410 :
411 : /**
412 : * sd_prepare_discard - unmap blocks on thinly provisioned device
413 : * @rq: Request to prepare
414 : *
415 : * Will issue either UNMAP or WRITE SAME(16) depending on preference
416 : * indicated by target device.
417 : **/
418 : static int sd_prepare_discard(struct request *rq)
419 : {
420 0 : struct scsi_disk *sdkp = scsi_disk(rq->rq_disk);
421 0 : struct bio *bio = rq->bio;
422 0 : sector_t sector = bio->bi_sector;
423 0 : unsigned int num = bio_sectors(bio);
424 0 :
425 0 : if (sdkp->device->sector_size == 4096) {
426 0 : sector >>= 3;
427 0 : num >>= 3;
428 0 : }
429 :
430 0 : rq->cmd_type = REQ_TYPE_BLOCK_PC;
431 0 : rq->timeout = SD_TIMEOUT;
432 :
433 0 : memset(rq->cmd, 0, rq->cmd_len);
434 :
435 0 : if (sdkp->unmap) {
436 0 : char *buf = kmap_atomic(bio_page(bio), KM_USER0);
437 :
438 0 : rq->cmd[0] = UNMAP;
439 0 : rq->cmd[8] = 24;
440 0 : rq->cmd_len = 10;
441 :
442 : /* Ensure that data length matches payload */
443 0 : rq->__data_len = bio->bi_size = bio->bi_io_vec->bv_len = 24;
444 :
445 0 : put_unaligned_be16(6 + 16, &buf[0]);
446 0 : put_unaligned_be16(16, &buf[2]);
447 0 : put_unaligned_be64(sector, &buf[8]);
448 0 : put_unaligned_be32(num, &buf[16]);
449 :
450 0 : kunmap_atomic(buf, KM_USER0);
451 : } else {
452 0 : rq->cmd[0] = WRITE_SAME_16;
453 0 : rq->cmd[1] = 0x8; /* UNMAP */
454 0 : put_unaligned_be64(sector, &rq->cmd[2]);
455 0 : put_unaligned_be32(num, &rq->cmd[10]);
456 0 : rq->cmd_len = 16;
457 : }
458 :
459 0 : return BLKPREP_OK;
460 : }
461 :
462 : /**
463 : * sd_init_command - build a scsi (read or write) command from
464 : * information in the request structure.
465 : * @SCpnt: pointer to mid-level's per scsi command structure that
466 : * contains request and into which the scsi command is written
467 : *
468 : * Returns 1 if successful and 0 if error (or cannot be done now).
469 : **/
470 : static int sd_prep_fn(struct request_queue *q, struct request *rq)
471 : {
472 0 : struct scsi_cmnd *SCpnt;
473 0 : struct scsi_device *sdp = q->queuedata;
474 0 : struct gendisk *disk = rq->rq_disk;
475 0 : struct scsi_disk *sdkp;
476 0 : sector_t block = blk_rq_pos(rq);
477 0 : sector_t threshold;
478 0 : unsigned int this_count = blk_rq_sectors(rq);
479 0 : int ret, host_dif;
480 0 : unsigned char protect;
481 0 :
482 0 : /*
483 0 : * Discard request come in as REQ_TYPE_FS but we turn them into
484 0 : * block PC requests to make life easier.
485 0 : */
486 0 : if (blk_discard_rq(rq))
487 0 : ret = sd_prepare_discard(rq);
488 0 :
489 0 : if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
490 0 : ret = scsi_setup_blk_pc_cmnd(sdp, rq);
491 0 : goto out;
492 0 : } else if (rq->cmd_type != REQ_TYPE_FS) {
493 0 : ret = BLKPREP_KILL;
494 0 : goto out;
495 0 : }
496 0 : ret = scsi_setup_fs_cmnd(sdp, rq);
497 0 : if (ret != BLKPREP_OK)
498 0 : goto out;
499 0 : SCpnt = rq->special;
500 0 : sdkp = scsi_disk(disk);
501 0 :
502 0 : /* from here on until we're complete, any goto out
503 0 : * is used for a killable error condition */
504 0 : ret = BLKPREP_KILL;
505 0 :
506 0 : SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt,
507 0 : "sd_init_command: block=%llu, "
508 0 : "count=%d\n",
509 0 : (unsigned long long)block,
510 0 : this_count));
511 0 :
512 0 : if (!sdp || !scsi_device_online(sdp) ||
513 0 : block + blk_rq_sectors(rq) > get_capacity(disk)) {
514 0 : SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
515 0 : "Finishing %u sectors\n",
516 0 : blk_rq_sectors(rq)));
517 0 : SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
518 0 : "Retry with 0x%p\n", SCpnt));
519 0 : goto out;
520 0 : }
521 0 :
522 0 : if (sdp->changed) {
523 : /*
524 : * quietly refuse to do anything to a changed disc until
525 : * the changed bit has been reset
526 : */
527 : /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
528 0 : goto out;
529 : }
530 :
531 : /*
532 : * Some SD card readers can't handle multi-sector accesses which touch
533 : * the last one or two hardware sectors. Split accesses as needed.
534 : */
535 0 : threshold = get_capacity(disk) - SD_LAST_BUGGY_SECTORS *
536 : (sdp->sector_size / 512);
537 :
538 0 : if (unlikely(sdp->last_sector_bug && block + this_count > threshold)) {
539 0 : if (block < threshold) {
540 : /* Access up to the threshold but not beyond */
541 0 : this_count = threshold - block;
542 : } else {
543 : /* Access only a single hardware sector */
544 0 : this_count = sdp->sector_size / 512;
545 : }
546 : }
547 :
548 : SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n",
549 : (unsigned long long)block));
550 :
551 : /*
552 : * If we have a 1K hardware sectorsize, prevent access to single
553 : * 512 byte sectors. In theory we could handle this - in fact
554 : * the scsi cdrom driver must be able to handle this because
555 : * we typically use 1K blocksizes, and cdroms typically have
556 : * 2K hardware sectorsizes. Of course, things are simpler
557 : * with the cdrom, since it is read-only. For performance
558 : * reasons, the filesystems should be able to handle this
559 : * and not force the scsi disk driver to use bounce buffers
560 : * for this.
561 : */
562 0 : if (sdp->sector_size == 1024) {
563 0 : if ((block & 1) || (blk_rq_sectors(rq) & 1)) {
564 0 : scmd_printk(KERN_ERR, SCpnt,
565 : "Bad block number requested\n");
566 0 : goto out;
567 : } else {
568 0 : block = block >> 1;
569 0 : this_count = this_count >> 1;
570 : }
571 : }
572 0 : if (sdp->sector_size == 2048) {
573 0 : if ((block & 3) || (blk_rq_sectors(rq) & 3)) {
574 0 : scmd_printk(KERN_ERR, SCpnt,
575 : "Bad block number requested\n");
576 0 : goto out;
577 : } else {
578 0 : block = block >> 2;
579 0 : this_count = this_count >> 2;
580 : }
581 : }
582 0 : if (sdp->sector_size == 4096) {
583 0 : if ((block & 7) || (blk_rq_sectors(rq) & 7)) {
584 0 : scmd_printk(KERN_ERR, SCpnt,
585 : "Bad block number requested\n");
586 0 : goto out;
587 : } else {
588 0 : block = block >> 3;
589 0 : this_count = this_count >> 3;
590 : }
591 : }
592 0 : if (rq_data_dir(rq) == WRITE) {
593 0 : if (!sdp->writeable) {
594 0 : goto out;
595 : }
596 0 : SCpnt->cmnd[0] = WRITE_6;
597 0 : SCpnt->sc_data_direction = DMA_TO_DEVICE;
598 :
599 : if (blk_integrity_rq(rq) &&
600 : sd_dif_prepare(rq, block, sdp->sector_size) == -EIO)
601 : goto out;
602 :
603 0 : } else if (rq_data_dir(rq) == READ) {
604 0 : SCpnt->cmnd[0] = READ_6;
605 0 : SCpnt->sc_data_direction = DMA_FROM_DEVICE;
606 : } else {
607 0 : scmd_printk(KERN_ERR, SCpnt, "Unknown command %x\n", rq->cmd_flags);
608 0 : goto out;
609 : }
610 :
611 : SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt,
612 : "%s %d/%u 512 byte blocks.\n",
613 : (rq_data_dir(rq) == WRITE) ?
614 : "writing" : "reading", this_count,
615 : blk_rq_sectors(rq)));
616 :
617 : /* Set RDPROTECT/WRPROTECT if disk is formatted with DIF */
618 0 : host_dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type);
619 0 : if (host_dif)
620 0 : protect = 1 << 5;
621 : else
622 0 : protect = 0;
623 :
624 0 : if (host_dif == SD_DIF_TYPE2_PROTECTION) {
625 0 : SCpnt->cmnd = mempool_alloc(sd_cdb_pool, GFP_ATOMIC);
626 :
627 0 : if (unlikely(SCpnt->cmnd == NULL)) {
628 0 : ret = BLKPREP_DEFER;
629 0 : goto out;
630 : }
631 :
632 0 : SCpnt->cmd_len = SD_EXT_CDB_SIZE;
633 0 : memset(SCpnt->cmnd, 0, SCpnt->cmd_len);
634 0 : SCpnt->cmnd[0] = VARIABLE_LENGTH_CMD;
635 0 : SCpnt->cmnd[7] = 0x18;
636 0 : SCpnt->cmnd[9] = (rq_data_dir(rq) == READ) ? READ_32 : WRITE_32;
637 0 : SCpnt->cmnd[10] = protect | (blk_fua_rq(rq) ? 0x8 : 0);
638 :
639 : /* LBA */
640 0 : SCpnt->cmnd[12] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
641 0 : SCpnt->cmnd[13] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
642 0 : SCpnt->cmnd[14] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
643 0 : SCpnt->cmnd[15] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
644 0 : SCpnt->cmnd[16] = (unsigned char) (block >> 24) & 0xff;
645 0 : SCpnt->cmnd[17] = (unsigned char) (block >> 16) & 0xff;
646 0 : SCpnt->cmnd[18] = (unsigned char) (block >> 8) & 0xff;
647 0 : SCpnt->cmnd[19] = (unsigned char) block & 0xff;
648 :
649 : /* Expected Indirect LBA */
650 0 : SCpnt->cmnd[20] = (unsigned char) (block >> 24) & 0xff;
651 0 : SCpnt->cmnd[21] = (unsigned char) (block >> 16) & 0xff;
652 0 : SCpnt->cmnd[22] = (unsigned char) (block >> 8) & 0xff;
653 0 : SCpnt->cmnd[23] = (unsigned char) block & 0xff;
654 :
655 : /* Transfer length */
656 0 : SCpnt->cmnd[28] = (unsigned char) (this_count >> 24) & 0xff;
657 0 : SCpnt->cmnd[29] = (unsigned char) (this_count >> 16) & 0xff;
658 0 : SCpnt->cmnd[30] = (unsigned char) (this_count >> 8) & 0xff;
659 0 : SCpnt->cmnd[31] = (unsigned char) this_count & 0xff;
660 0 : } else if (block > 0xffffffff) {
661 0 : SCpnt->cmnd[0] += READ_16 - READ_6;
662 0 : SCpnt->cmnd[1] = protect | (blk_fua_rq(rq) ? 0x8 : 0);
663 0 : SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0;
664 0 : SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0;
665 0 : SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0;
666 0 : SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0;
667 0 : SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff;
668 0 : SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff;
669 0 : SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff;
670 0 : SCpnt->cmnd[9] = (unsigned char) block & 0xff;
671 0 : SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff;
672 0 : SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff;
673 0 : SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff;
674 0 : SCpnt->cmnd[13] = (unsigned char) this_count & 0xff;
675 0 : SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0;
676 0 : } else if ((this_count > 0xff) || (block > 0x1fffff) ||
677 0 : scsi_device_protection(SCpnt->device) ||
678 : SCpnt->device->use_10_for_rw) {
679 0 : if (this_count > 0xffff)
680 0 : this_count = 0xffff;
681 :
682 0 : SCpnt->cmnd[0] += READ_10 - READ_6;
683 0 : SCpnt->cmnd[1] = protect | (blk_fua_rq(rq) ? 0x8 : 0);
684 0 : SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff;
685 0 : SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff;
686 0 : SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff;
687 0 : SCpnt->cmnd[5] = (unsigned char) block & 0xff;
688 0 : SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0;
689 0 : SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff;
690 0 : SCpnt->cmnd[8] = (unsigned char) this_count & 0xff;
691 : } else {
692 0 : if (unlikely(blk_fua_rq(rq))) {
693 : /*
694 : * This happens only if this drive failed
695 : * 10byte rw command with ILLEGAL_REQUEST
696 : * during operation and thus turned off
697 : * use_10_for_rw.
698 : */
699 0 : scmd_printk(KERN_ERR, SCpnt,
700 : "FUA write on READ/WRITE(6) drive\n");
701 0 : goto out;
702 : }
703 :
704 0 : SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f);
705 0 : SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff);
706 0 : SCpnt->cmnd[3] = (unsigned char) block & 0xff;
707 0 : SCpnt->cmnd[4] = (unsigned char) this_count;
708 0 : SCpnt->cmnd[5] = 0;
709 : }
710 0 : SCpnt->sdb.length = this_count * sdp->sector_size;
711 :
712 : /* If DIF or DIX is enabled, tell HBA how to handle request */
713 0 : if (host_dif || scsi_prot_sg_count(SCpnt))
714 0 : sd_prot_op(SCpnt, host_dif);
715 :
716 : /*
717 : * We shouldn't disconnect in the middle of a sector, so with a dumb
718 : * host adapter, it's safe to assume that we can at least transfer
719 : * this many bytes between each connect / disconnect.
720 : */
721 0 : SCpnt->transfersize = sdp->sector_size;
722 0 : SCpnt->underflow = this_count << 9;
723 0 : SCpnt->allowed = SD_MAX_RETRIES;
724 :
725 : /*
726 : * This indicates that the command is ready from our end to be
727 : * queued.
728 : */
729 0 : ret = BLKPREP_OK;
730 0 : out:
731 0 : return scsi_prep_return(q, rq, ret);
732 : }
733 :
734 : /**
735 : * sd_open - open a scsi disk device
736 : * @inode: only i_rdev member may be used
737 : * @filp: only f_mode and f_flags may be used
738 : *
739 : * Returns 0 if successful. Returns a negated errno value in case
740 : * of error.
741 : *
742 : * Note: This can be called from a user context (e.g. fsck(1) )
743 : * or from within the kernel (e.g. as a result of a mount(1) ).
744 : * In the latter case @inode and @filp carry an abridged amount
745 : * of information as noted above.
746 : **/
747 : static int sd_open(struct block_device *bdev, fmode_t mode)
748 : {
749 28 : struct scsi_disk *sdkp = scsi_disk_get(bdev->bd_disk);
750 7 : struct scsi_device *sdev;
751 7 : int retval;
752 7 :
753 21 : if (!sdkp)
754 14 : return -ENXIO;
755 7 :
756 7 : SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
757 :
758 7 : sdev = sdkp->device;
759 :
760 : /*
761 : * If the device is in error recovery, wait until it is done.
762 : * If the device is offline, then disallow any access to it.
763 : */
764 7 : retval = -ENXIO;
765 21 : if (!scsi_block_when_processing_errors(sdev))
766 7 : goto error_out;
767 :
768 35 : if (sdev->removable || sdkp->write_prot)
769 7 : check_disk_change(bdev);
770 :
771 : /*
772 : * If the drive is empty, just let the open fail.
773 : */
774 7 : retval = -ENOMEDIUM;
775 49 : if (sdev->removable && !sdkp->media_present && !(mode & FMODE_NDELAY))
776 7 : goto error_out;
777 :
778 : /*
779 : * If the device has the write protect tab set, have the open fail
780 : * if the user expects to be able to write to the thing.
781 : */
782 7 : retval = -EROFS;
783 35 : if (sdkp->write_prot && (mode & FMODE_WRITE))
784 7 : goto error_out;
785 :
786 : /*
787 : * It is possible that the disk changing stuff resulted in
788 : * the device being taken offline. If this is the case,
789 : * report this to the user, and don't pretend that the
790 : * open actually succeeded.
791 : */
792 7 : retval = -ENXIO;
793 28 : if (!scsi_device_online(sdev))
794 7 : goto error_out;
795 :
796 42 : if (!sdkp->openers++ && sdev->removable) {
797 21 : if (scsi_block_when_processing_errors(sdev))
798 7 : scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
799 : }
800 :
801 7 : return 0;
802 14 :
803 : error_out:
804 28 : scsi_disk_put(sdkp);
805 7 : return retval;
806 : }
807 :
808 : /**
809 : * sd_release - invoked when the (last) close(2) is called on this
810 : * scsi disk.
811 : * @inode: only i_rdev member may be used
812 : * @filp: only f_mode and f_flags may be used
813 : *
814 : * Returns 0.
815 : *
816 : * Note: may block (uninterruptible) if error recovery is underway
817 : * on this disk.
818 : **/
819 : static int sd_release(struct gendisk *disk, fmode_t mode)
820 : {
821 28 : struct scsi_disk *sdkp = scsi_disk(disk);
822 14 : struct scsi_device *sdev = sdkp->device;
823 7 :
824 7 : SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
825 :
826 35 : if (!--sdkp->openers && sdev->removable) {
827 21 : if (scsi_block_when_processing_errors(sdev))
828 7 : scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
829 : }
830 :
831 : /*
832 : * XXX and what if there are packets in flight and this close()
833 : * XXX is followed by a "rmmod sd_mod"?
834 : */
835 14 : scsi_disk_put(sdkp);
836 7 : return 0;
837 : }
838 :
839 : static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
840 : {
841 28 : struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
842 14 : struct scsi_device *sdp = sdkp->device;
843 14 : struct Scsi_Host *host = sdp->host;
844 7 : int diskinfo[4];
845 7 :
846 : /* default to most commonly used values */
847 7 : diskinfo[0] = 0x40; /* 1 << 6 */
848 7 : diskinfo[1] = 0x20; /* 1 << 5 */
849 7 : diskinfo[2] = sdkp->capacity >> 11;
850 :
851 : /* override with calculated, extended default, or driver values */
852 21 : if (host->hostt->bios_param)
853 7 : host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo);
854 : else
855 7 : scsicam_bios_param(bdev, sdkp->capacity, diskinfo);
856 :
857 7 : geo->heads = diskinfo[0];
858 7 : geo->sectors = diskinfo[1];
859 7 : geo->cylinders = diskinfo[2];
860 7 : return 0;
861 : }
862 :
863 : /**
864 : * sd_ioctl - process an ioctl
865 : * @inode: only i_rdev/i_bdev members may be used
866 : * @filp: only f_mode and f_flags may be used
867 : * @cmd: ioctl command number
868 : * @arg: this is third argument given to ioctl(2) system call.
869 : * Often contains a pointer.
870 : *
871 : * Returns 0 if successful (some ioctls return postive numbers on
872 : * success as well). Returns a negated errno value in case of error.
873 : *
874 : * Note: most ioctls are forward onto the block subsystem or further
875 : * down in the scsi subsystem.
876 : **/
877 : static int sd_ioctl(struct block_device *bdev, fmode_t mode,
878 : unsigned int cmd, unsigned long arg)
879 : {
880 14 : struct gendisk *disk = bdev->bd_disk;
881 28 : struct scsi_device *sdp = scsi_disk(disk)->device;
882 14 : void __user *p = (void __user *)arg;
883 7 : int error;
884 7 :
885 7 : SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n",
886 7 : disk->disk_name, cmd));
887 7 :
888 : /*
889 : * If we are in the middle of error recovery, don't let anyone
890 : * else try and use this device. Also, if error recovery fails, it
891 : * may try and take the device offline, in which case all further
892 : * access to the device is prohibited.
893 : */
894 7 : error = scsi_nonblockable_ioctl(sdp, cmd, p,
895 : (mode & FMODE_NDELAY) != 0);
896 35 : if (!scsi_block_when_processing_errors(sdp) || !error)
897 7 : return error;
898 :
899 : /*
900 : * Send SCSI addressing ioctls directly to mid level, send other
901 : * ioctls to block level and then onto mid level if they can't be
902 : * resolved.
903 : */
904 : switch (cmd) {
905 21 : case SCSI_IOCTL_GET_IDLUN:
906 21 : case SCSI_IOCTL_GET_BUS_NUMBER:
907 14 : return scsi_ioctl(sdp, cmd, p);
908 14 : default:
909 14 : error = scsi_cmd_ioctl(disk->queue, disk, mode, cmd, p);
910 21 : if (error != -ENOTTY)
911 7 : return error;
912 : }
913 14 : return scsi_ioctl(sdp, cmd, p);
914 : }
915 7 :
916 : static void set_media_not_present(struct scsi_disk *sdkp)
917 : {
918 70 : sdkp->media_present = 0;
919 70 : sdkp->capacity = 0;
920 70 : sdkp->device->changed = 1;
921 70 : }
922 :
923 : /**
924 : * sd_media_changed - check if our medium changed
925 : * @disk: kernel device descriptor
926 : *
927 : * Returns 0 if not applicable or no change; 1 if change
928 : *
929 : * Note: this function is invoked from the block subsystem.
930 : **/
931 : static int sd_media_changed(struct gendisk *disk)
932 : {
933 28 : struct scsi_disk *sdkp = scsi_disk(disk);
934 14 : struct scsi_device *sdp = sdkp->device;
935 14 : struct scsi_sense_hdr *sshdr = NULL;
936 7 : int retval;
937 7 :
938 7 : SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_media_changed\n"));
939 7 :
940 21 : if (!sdp->removable)
941 14 : return 0;
942 :
943 : /*
944 : * If the device is offline, don't send any commands - just pretend as
945 : * if the command failed. If the device ever comes back online, we
946 : * can deal with it then. It is only because of unrecoverable errors
947 : * that we would ever take a device offline in the first place.
948 : */
949 28 : if (!scsi_device_online(sdp)) {
950 14 : set_media_not_present(sdkp);
951 7 : retval = 1;
952 7 : goto out;
953 : }
954 :
955 : /*
956 : * Using TEST_UNIT_READY enables differentiation between drive with
957 : * no cartridge loaded - NOT READY, drive with changed cartridge -
958 : * UNIT ATTENTION, or with same cartridge - GOOD STATUS.
959 : *
960 : * Drives that auto spin down. eg iomega jaz 1G, will be started
961 : * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever
962 : * sd_revalidate() is called.
963 : */
964 7 : retval = -ENODEV;
965 :
966 21 : if (scsi_block_when_processing_errors(sdp)) {
967 21 : sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
968 7 : retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES,
969 : sshdr);
970 : }
971 :
972 : /*
973 : * Unable to test, unit probably not ready. This usually
974 : * means there is no disc in the drive. Mark as changed,
975 : * and we will figure it out later once the drive is
976 : * available again.
977 : */
978 91 : if (retval || (scsi_sense_valid(sshdr) &&
979 : /* 0x3a is medium not present */
980 : sshdr->asc == 0x3a)) {
981 42 : set_media_not_present(sdkp);
982 14 : retval = 1;
983 14 : goto out;
984 : }
985 :
986 : /*
987 : * For removable scsi disk we have to recognise the presence
988 : * of a disk in the drive. This is kept in the struct scsi_disk
989 : * struct and tested at open ! Daniel Roche (dan@lectra.fr)
990 : */
991 7 : sdkp->media_present = 1;
992 :
993 14 : retval = sdp->changed;
994 7 : sdp->changed = 0;
995 7 : out:
996 84 : if (retval != sdkp->previous_state)
997 28 : sdev_evt_send_simple(sdp, SDEV_EVT_MEDIA_CHANGE, GFP_KERNEL);
998 28 : sdkp->previous_state = retval;
999 28 : kfree(sshdr);
1000 28 : return retval;
1001 : }
1002 :
1003 : static int sd_sync_cache(struct scsi_disk *sdkp)
1004 : {
1005 42 : int retries, res;
1006 84 : struct scsi_device *sdp = sdkp->device;
1007 42 : struct scsi_sense_hdr sshdr;
1008 42 :
1009 210 : if (!scsi_device_online(sdp))
1010 84 : return -ENODEV;
1011 42 :
1012 :
1013 210 : for (retries = 3; retries > 0; --retries) {
1014 462 : unsigned char cmd[10] = { 0 };
1015 84 :
1016 84 : cmd[0] = SYNCHRONIZE_CACHE;
1017 : /*
1018 : * Leave the rest of the command zero to indicate
1019 42 : * flush everything.
1020 : */
1021 42 : res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
1022 : SD_TIMEOUT, SD_MAX_RETRIES, NULL);
1023 84 : if (res == 0)
1024 42 : break;
1025 : }
1026 :
1027 168 : if (res) {
1028 252 : sd_print_result(sdkp, res);
1029 84 : if (driver_byte(res) & DRIVER_SENSE)
1030 126 : sd_print_sense_hdr(sdkp, &sshdr);
1031 : }
1032 :
1033 336 : if (res)
1034 168 : return -EIO;
1035 168 : return 0;
1036 : }
1037 :
1038 : static void sd_prepare_flush(struct request_queue *q, struct request *rq)
1039 : {
1040 0 : rq->cmd_type = REQ_TYPE_BLOCK_PC;
1041 0 : rq->timeout = SD_TIMEOUT;
1042 0 : rq->retries = SD_MAX_RETRIES;
1043 0 : rq->cmd[0] = SYNCHRONIZE_CACHE;
1044 0 : rq->cmd_len = 10;
1045 0 : }
1046 :
1047 : static void sd_rescan(struct device *dev)
1048 : {
1049 35 : struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
1050 7 :
1051 14 : if (sdkp) {
1052 7 : revalidate_disk(sdkp->disk);
1053 14 : scsi_disk_put(sdkp);
1054 : }
1055 14 : }
1056 :
1057 :
1058 : #ifdef CONFIG_COMPAT
1059 : /*
1060 : * This gets directly called from VFS. When the ioctl
1061 : * is not recognized we go back to the other translation paths.
1062 : */
1063 : static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode,
1064 : unsigned int cmd, unsigned long arg)
1065 : {
1066 28 : struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
1067 7 :
1068 7 : /*
1069 7 : * If we are in the middle of error recovery, don't let anyone
1070 : * else try and use this device. Also, if error recovery fails, it
1071 : * may try and take the device offline, in which case all further
1072 : * access to the device is prohibited.
1073 : */
1074 21 : if (!scsi_block_when_processing_errors(sdev))
1075 7 : return -ENODEV;
1076 :
1077 21 : if (sdev->host->hostt->compat_ioctl) {
1078 : int ret;
1079 :
1080 7 : ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
1081 :
1082 7 : return ret;
1083 : }
1084 :
1085 : /*
1086 : * Let the static ioctl translation table take care of it.
1087 : */
1088 7 : return -ENOIOCTLCMD;
1089 : }
1090 : #endif
1091 :
1092 1 : static const struct block_device_operations sd_fops = {
1093 : .owner = THIS_MODULE,
1094 : .open = sd_open,
1095 : .release = sd_release,
1096 : .locked_ioctl = sd_ioctl,
1097 : .getgeo = sd_getgeo,
1098 : #ifdef CONFIG_COMPAT
1099 : .compat_ioctl = sd_compat_ioctl,
1100 : #endif
1101 : .media_changed = sd_media_changed,
1102 : .revalidate_disk = sd_revalidate_disk,
1103 : };
1104 :
1105 : static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
1106 : {
1107 210 : u64 start_lba = blk_rq_pos(scmd->request);
1108 294 : u64 end_lba = blk_rq_pos(scmd->request) + (scsi_bufflen(scmd) / 512);
1109 42 : u64 bad_lba;
1110 42 : int info_valid;
1111 42 : /*
1112 42 : * resid is optional but mostly filled in. When it's unused,
1113 42 : * its value is zero, so we assume the whole buffer transferred
1114 42 : */
1115 252 : unsigned int transferred = scsi_bufflen(scmd) - scsi_get_resid(scmd);
1116 42 : unsigned int good_bytes;
1117 42 :
1118 168 : if (!blk_fs_request(scmd->request))
1119 84 : return 0;
1120 42 :
1121 126 : info_valid = scsi_get_sense_info_fld(scmd->sense_buffer,
1122 42 : SCSI_SENSE_BUFFERSIZE,
1123 42 : &bad_lba);
1124 126 : if (!info_valid)
1125 84 : return 0;
1126 :
1127 168 : if (scsi_bufflen(scmd) <= scmd->device->sector_size)
1128 42 : return 0;
1129 :
1130 84 : if (scmd->device->sector_size < 512) {
1131 : /* only legitimate sector_size here is 256 */
1132 42 : start_lba <<= 1;
1133 42 : end_lba <<= 1;
1134 : } else {
1135 : /* be careful ... don't want any overflows */
1136 42 : u64 factor = scmd->device->sector_size / 512;
1137 126 : do_div(start_lba, factor);
1138 126 : do_div(end_lba, factor);
1139 : }
1140 :
1141 : /* The bad lba was reported incorrectly, we have no idea where
1142 : * the error is.
1143 : */
1144 168 : if (bad_lba < start_lba || bad_lba >= end_lba)
1145 42 : return 0;
1146 :
1147 : /* This computation should always be done in terms of
1148 : * the resolution of the device's medium.
1149 : */
1150 42 : good_bytes = (bad_lba - start_lba) * scmd->device->sector_size;
1151 336 : return min(good_bytes, transferred);
1152 : }
1153 :
1154 : /**
1155 : * sd_done - bottom half handler: called when the lower level
1156 : * driver has completed (successfully or otherwise) a scsi command.
1157 : * @SCpnt: mid-level's per command structure.
1158 : *
1159 : * Note: potentially run from within an ISR. Must not block.
1160 : **/
1161 : static int sd_done(struct scsi_cmnd *SCpnt)
1162 : {
1163 14 : int result = SCpnt->result;
1164 63 : unsigned int good_bytes = result ? 0 : scsi_bufflen(SCpnt);
1165 7 : struct scsi_sense_hdr sshdr;
1166 42 : struct scsi_disk *sdkp = scsi_disk(SCpnt->request->rq_disk);
1167 14 : int sense_valid = 0;
1168 14 : int sense_deferred = 0;
1169 7 :
1170 21 : if (result) {
1171 14 : sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr);
1172 21 : if (sense_valid)
1173 21 : sense_deferred = scsi_sense_is_deferred(&sshdr);
1174 : }
1175 : #ifdef CONFIG_SCSI_LOGGING
1176 : SCSI_LOG_HLCOMPLETE(1, scsi_print_result(SCpnt));
1177 : if (sense_valid) {
1178 : SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt,
1179 : "sd_done: sb[respc,sk,asc,"
1180 : "ascq]=%x,%x,%x,%x\n",
1181 : sshdr.response_code,
1182 : sshdr.sense_key, sshdr.asc,
1183 : sshdr.ascq));
1184 : }
1185 : #endif
1186 84 : if (driver_byte(result) != DRIVER_SENSE &&
1187 : (!sense_valid || sense_deferred))
1188 14 : goto out;
1189 :
1190 : switch (sshdr.sense_key) {
1191 42 : case HARDWARE_ERROR:
1192 42 : case MEDIUM_ERROR:
1193 42 : good_bytes = sd_completed_bytes(SCpnt);
1194 7 : break;
1195 56 : case RECOVERED_ERROR:
1196 28 : good_bytes = scsi_bufflen(SCpnt);
1197 7 : break;
1198 56 : case NO_SENSE:
1199 : /* This indicates a false check condition, so ignore it. An
1200 : * unknown amount of data was transferred so treat it as an
1201 : * error.
1202 : */
1203 14 : scsi_print_sense("sd", SCpnt);
1204 14 : SCpnt->result = 0;
1205 28 : memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1206 14 : break;
1207 56 : case ABORTED_COMMAND:
1208 28 : if (sshdr.asc == 0x10) { /* DIF: Disk detected corruption */
1209 14 : scsi_print_result(SCpnt);
1210 14 : scsi_print_sense("sd", SCpnt);
1211 42 : good_bytes = sd_completed_bytes(SCpnt);
1212 : }
1213 21 : break;
1214 56 : case ILLEGAL_REQUEST:
1215 28 : if (sshdr.asc == 0x10) { /* DIX: HBA detected corruption */
1216 14 : scsi_print_result(SCpnt);
1217 14 : scsi_print_sense("sd", SCpnt);
1218 42 : good_bytes = sd_completed_bytes(SCpnt);
1219 : }
1220 21 : break;
1221 28 : default:
1222 28 : break;
1223 14 : }
1224 : out:
1225 182 : if (rq_data_dir(SCpnt->request) == READ && scsi_prot_sg_count(SCpnt))
1226 56 : sd_dif_complete(SCpnt, good_bytes);
1227 :
1228 210 : if (scsi_host_dif_capable(sdkp->device->host, sdkp->protection_type)
1229 : == SD_DIF_TYPE2_PROTECTION && SCpnt->cmnd != SCpnt->request->cmd)
1230 14 : mempool_free(SCpnt->cmnd, sd_cdb_pool);
1231 :
1232 7 : return good_bytes;
1233 : }
1234 :
1235 : static int media_not_present(struct scsi_disk *sdkp,
1236 : struct scsi_sense_hdr *sshdr)
1237 42 : {
1238 :
1239 168 : if (!scsi_sense_valid(sshdr))
1240 42 : return 0;
1241 : /* not invoked for commands that could return deferred errors */
1242 252 : if (sshdr->sense_key != NOT_READY &&
1243 : sshdr->sense_key != UNIT_ATTENTION)
1244 42 : return 0;
1245 126 : if (sshdr->asc != 0x3A) /* medium not present */
1246 42 : return 0;
1247 :
1248 84 : set_media_not_present(sdkp);
1249 42 : return 1;
1250 : }
1251 :
1252 : /*
1253 : * spinup disk - called only in sd_revalidate_disk()
1254 : */
1255 : static void
1256 : sd_spinup_disk(struct scsi_disk *sdkp)
1257 : {
1258 7 : unsigned char cmd[10];
1259 14 : unsigned long spintime_expire = 0;
1260 7 : int retries, spintime;
1261 7 : unsigned int the_result;
1262 7 : struct scsi_sense_hdr sshdr;
1263 14 : int sense_valid = 0;
1264 7 :
1265 14 : spintime = 0;
1266 14 :
1267 7 : /* Spin up drives, as required. Only do this at boot time */
1268 7 : /* Spinup needs to be done for module loads too. */
1269 7 : do {
1270 14 : retries = 0;
1271 14 :
1272 7 : do {
1273 14 : cmd[0] = TEST_UNIT_READY;
1274 14 : memset((void *) &cmd[1], 0, 9);
1275 7 :
1276 21 : the_result = scsi_execute_req(sdkp->device, cmd,
1277 7 : DMA_NONE, NULL, 0,
1278 7 : &sshdr, SD_TIMEOUT,
1279 7 : SD_MAX_RETRIES, NULL);
1280 7 :
1281 7 : /*
1282 : * If the drive has indicated to us that it
1283 : * doesn't have any media in it, don't bother
1284 : * with any more polling.
1285 : */
1286 35 : if (media_not_present(sdkp, &sshdr))
1287 7 : return;
1288 :
1289 14 : if (the_result)
1290 14 : sense_valid = scsi_sense_valid(&sshdr);
1291 14 : retries++;
1292 : } while (retries < 3 &&
1293 : (!scsi_status_is_good(the_result) ||
1294 : ((driver_byte(the_result) & DRIVER_SENSE) &&
1295 112 : sense_valid && sshdr.sense_key == UNIT_ATTENTION)));
1296 :
1297 49 : if ((driver_byte(the_result) & DRIVER_SENSE) == 0) {
1298 : /* no sense, TUR either succeeded or failed
1299 : * with a status error */
1300 105 : if(!spintime && !scsi_status_is_good(the_result)) {
1301 77 : sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1302 42 : sd_print_result(sdkp, the_result);
1303 : }
1304 35 : break;
1305 14 : }
1306 :
1307 : /*
1308 : * The device does not want the automatic start to be issued.
1309 : */
1310 63 : if (sdkp->device->no_start_on_add)
1311 21 : break;
1312 :
1313 84 : if (sense_valid && sshdr.sense_key == NOT_READY) {
1314 84 : if (sshdr.asc == 4 && sshdr.ascq == 3)
1315 21 : break; /* manual intervention required */
1316 84 : if (sshdr.asc == 4 && sshdr.ascq == 0xb)
1317 21 : break; /* standby */
1318 84 : if (sshdr.asc == 4 && sshdr.ascq == 0xc)
1319 21 : break; /* unavailable */
1320 : /*
1321 : * Issue command to spin up drive when not ready
1322 : */
1323 42 : if (!spintime) {
1324 175 : sd_printk(KERN_NOTICE, sdkp, "Spinning up disk...");
1325 14 : cmd[0] = START_STOP;
1326 14 : cmd[1] = 1; /* Return immediately */
1327 14 : memset((void *) &cmd[2], 0, 8);
1328 14 : cmd[4] = 1; /* Start spin cycle */
1329 42 : if (sdkp->device->start_stop_pwr_cond)
1330 14 : cmd[4] |= 1 << 4;
1331 14 : scsi_execute_req(sdkp->device, cmd, DMA_NONE,
1332 : NULL, 0, &sshdr,
1333 : SD_TIMEOUT, SD_MAX_RETRIES,
1334 : NULL);
1335 14 : spintime_expire = jiffies + 100 * HZ;
1336 14 : spintime = 1;
1337 : }
1338 : /* Wait 1 second for next try */
1339 35 : msleep(1000);
1340 35 : printk(".");
1341 :
1342 : /*
1343 : * Wait for USB flash devices with slow firmware.
1344 : * Yes, this sense key/ASC combination shouldn't
1345 : * occur here. It's characteristic of these devices.
1346 : */
1347 126 : } else if (sense_valid &&
1348 : sshdr.sense_key == UNIT_ATTENTION &&
1349 : sshdr.asc == 0x28) {
1350 42 : if (!spintime) {
1351 21 : spintime_expire = jiffies + 5 * HZ;
1352 21 : spintime = 1;
1353 : }
1354 : /* Wait 1 second for next try */
1355 21 : msleep(1000);
1356 : } else {
1357 : /* we don't understand the sense code, so it's
1358 : * probably pointless to loop */
1359 42 : if(!spintime) {
1360 175 : sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n");
1361 42 : sd_print_sense_hdr(sdkp, &sshdr);
1362 : }
1363 28 : break;
1364 : }
1365 :
1366 140 : } while (spintime && time_before_eq(jiffies, spintime_expire));
1367 :
1368 147 : if (spintime) {
1369 126 : if (scsi_status_is_good(the_result))
1370 7 : printk("ready\n");
1371 : else
1372 42 : printk("not responding...\n");
1373 63 : }
1374 : }
1375 :
1376 :
1377 : /*
1378 : * Determine whether disk supports Data Integrity Field.
1379 : */
1380 : void sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer)
1381 : {
1382 42 : struct scsi_device *sdp = sdkp->device;
1383 21 : u8 type;
1384 21 :
1385 147 : if (scsi_device_protection(sdp) == 0 || (buffer[12] & 1) == 0)
1386 42 : return;
1387 21 :
1388 42 : type = ((buffer[12] >> 1) & 7) + 1; /* P_TYPE 0 = Type 1 */
1389 21 :
1390 126 : if (type == sdkp->protection_type || !sdkp->first_scan)
1391 42 : return;
1392 21 :
1393 42 : sdkp->protection_type = type;
1394 21 :
1395 63 : if (type > SD_DIF_TYPE3_PROTECTION) {
1396 252 : sd_printk(KERN_ERR, sdkp, "formatted with unsupported " \
1397 21 : "protection type %u. Disabling disk!\n", type);
1398 42 : sdkp->capacity = 0;
1399 42 : return;
1400 : }
1401 :
1402 84 : if (scsi_host_dif_capable(sdp->host, type))
1403 231 : sd_printk(KERN_NOTICE, sdkp,
1404 : "Enabling DIF Type %u protection\n", type);
1405 : else
1406 231 : sd_printk(KERN_NOTICE, sdkp,
1407 84 : "Disabling DIF Type %u protection\n", type);
1408 : }
1409 :
1410 : static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp,
1411 : struct scsi_sense_hdr *sshdr, int sense_valid,
1412 : int the_result)
1413 70 : {
1414 280 : sd_print_result(sdkp, the_result);
1415 210 : if (driver_byte(the_result) & DRIVER_SENSE)
1416 280 : sd_print_sense_hdr(sdkp, sshdr);
1417 : else
1418 770 : sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n");
1419 :
1420 : /*
1421 : * Set dirty bit for removable devices if not ready -
1422 : * sometimes drives will not report this properly.
1423 : */
1424 1470 : if (sdp->removable &&
1425 : sense_valid && sshdr->sense_key == NOT_READY)
1426 210 : sdp->changed = 1;
1427 :
1428 : /*
1429 : * We used to set media_present to 0 here to indicate no media
1430 : * in the drive, but some drives fail read capacity even with
1431 : * media present, so we can't do that.
1432 : */
1433 210 : sdkp->capacity = 0; /* unknown mapped to zero - as usual */
1434 210 : }
1435 :
1436 : #define RC16_LEN 32
1437 : #if RC16_LEN > SD_BUF_SIZE
1438 : #error RC16_LEN must not be more than SD_BUF_SIZE
1439 : #endif
1440 :
1441 : static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
1442 : unsigned char *buffer)
1443 21 : {
1444 21 : unsigned char cmd[16];
1445 21 : struct scsi_sense_hdr sshdr;
1446 42 : int sense_valid = 0;
1447 21 : int the_result;
1448 42 : int retries = 3;
1449 42 : unsigned int alignment;
1450 21 : unsigned long long lba;
1451 21 : unsigned sector_size;
1452 21 :
1453 21 : do {
1454 42 : memset(cmd, 0, 16);
1455 42 : cmd[0] = SERVICE_ACTION_IN;
1456 42 : cmd[1] = SAI_READ_CAPACITY_16;
1457 42 : cmd[13] = RC16_LEN;
1458 42 : memset(buffer, 0, RC16_LEN);
1459 21 :
1460 42 : the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
1461 : buffer, RC16_LEN, &sshdr,
1462 : SD_TIMEOUT, SD_MAX_RETRIES, NULL);
1463 :
1464 105 : if (media_not_present(sdkp, &sshdr))
1465 21 : return -ENODEV;
1466 :
1467 42 : if (the_result) {
1468 42 : sense_valid = scsi_sense_valid(&sshdr);
1469 210 : if (sense_valid &&
1470 : sshdr.sense_key == ILLEGAL_REQUEST &&
1471 : (sshdr.asc == 0x20 || sshdr.asc == 0x24) &&
1472 : sshdr.ascq == 0x00)
1473 : /* Invalid Command Operation Code or
1474 : * Invalid Field in CDB, just retry
1475 : * silently with RC10 */
1476 21 : return -EINVAL;
1477 : }
1478 42 : retries--;
1479 :
1480 168 : } while (the_result && retries);
1481 :
1482 126 : if (the_result) {
1483 378 : sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY(16) failed\n");
1484 168 : read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
1485 21 : return -EINVAL;
1486 : }
1487 :
1488 84 : sector_size = get_unaligned_be32(&buffer[8]);
1489 42 : lba = get_unaligned_be64(&buffer[0]);
1490 :
1491 168 : sd_read_protection_type(sdkp, buffer);
1492 :
1493 : if ((sizeof(sdkp->capacity) == 4) && (lba >= 0xffffffffULL)) {
1494 : sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
1495 : "kernel compiled with support for large block "
1496 : "devices.\n");
1497 : sdkp->capacity = 0;
1498 : return -EOVERFLOW;
1499 : }
1500 :
1501 : /* Logical blocks per physical block exponent */
1502 21 : sdkp->hw_sector_size = (1 << (buffer[13] & 0xf)) * sector_size;
1503 :
1504 : /* Lowest aligned logical block */
1505 21 : alignment = ((buffer[14] & 0x3f) << 8 | buffer[15]) * sector_size;
1506 21 : blk_queue_alignment_offset(sdp->request_queue, alignment);
1507 84 : if (alignment && sdkp->first_scan)
1508 231 : sd_printk(KERN_NOTICE, sdkp,
1509 : "physical block alignment offset: %u\n", alignment);
1510 :
1511 126 : if (buffer[14] & 0x80) { /* TPE */
1512 63 : struct request_queue *q = sdp->request_queue;
1513 :
1514 63 : sdkp->thin_provisioning = 1;
1515 126 : q->limits.discard_granularity = sdkp->hw_sector_size;
1516 63 : q->limits.max_discard_sectors = 0xffffffff;
1517 :
1518 126 : if (buffer[14] & 0x40) /* TPRZ */
1519 63 : q->limits.discard_zeroes_data = 1;
1520 :
1521 126 : queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
1522 : }
1523 :
1524 84 : sdkp->capacity = lba + 1;
1525 84 : return sector_size;
1526 : }
1527 :
1528 : static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,
1529 : unsigned char *buffer)
1530 14 : {
1531 14 : unsigned char cmd[16];
1532 14 : struct scsi_sense_hdr sshdr;
1533 28 : int sense_valid = 0;
1534 14 : int the_result;
1535 28 : int retries = 3;
1536 28 : sector_t lba;
1537 14 : unsigned sector_size;
1538 14 :
1539 14 : do {
1540 28 : cmd[0] = READ_CAPACITY;
1541 28 : memset(&cmd[1], 0, 9);
1542 28 : memset(buffer, 0, 8);
1543 :
1544 14 : the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
1545 : buffer, 8, &sshdr,
1546 : SD_TIMEOUT, SD_MAX_RETRIES, NULL);
1547 :
1548 70 : if (media_not_present(sdkp, &sshdr))
1549 14 : return -ENODEV;
1550 :
1551 28 : if (the_result)
1552 28 : sense_valid = scsi_sense_valid(&sshdr);
1553 28 : retries--;
1554 :
1555 112 : } while (the_result && retries);
1556 :
1557 84 : if (the_result) {
1558 252 : sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY failed\n");
1559 112 : read_capacity_error(sdkp, sdp, &sshdr, sense_valid, the_result);
1560 14 : return -EINVAL;
1561 : }
1562 :
1563 56 : sector_size = get_unaligned_be32(&buffer[4]);
1564 42 : lba = get_unaligned_be32(&buffer[0]);
1565 :
1566 : if ((sizeof(sdkp->capacity) == 4) && (lba == 0xffffffff)) {
1567 : sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
1568 : "kernel compiled with support for large block "
1569 : "devices.\n");
1570 : sdkp->capacity = 0;
1571 : return -EOVERFLOW;
1572 : }
1573 :
1574 14 : sdkp->capacity = lba + 1;
1575 14 : sdkp->hw_sector_size = sector_size;
1576 14 : return sector_size;
1577 : }
1578 :
1579 : static int sd_try_rc16_first(struct scsi_device *sdp)
1580 : {
1581 28 : if (sdp->scsi_level > SCSI_SPC_2)
1582 7 : return 1;
1583 28 : if (scsi_device_protection(sdp))
1584 7 : return 1;
1585 7 : return 0;
1586 : }
1587 :
1588 : /*
1589 : * read disk capacity
1590 : */
1591 : static void
1592 : sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
1593 : {
1594 7 : int sector_size;
1595 14 : struct scsi_device *sdp = sdkp->device;
1596 14 : sector_t old_capacity = sdkp->capacity;
1597 7 :
1598 42 : if (sd_try_rc16_first(sdp)) {
1599 63 : sector_size = read_capacity_16(sdkp, sdp, buffer);
1600 21 : if (sector_size == -EOVERFLOW)
1601 14 : goto got_data;
1602 21 : if (sector_size == -ENODEV)
1603 14 : return;
1604 21 : if (sector_size < 0)
1605 35 : sector_size = read_capacity_10(sdkp, sdp, buffer);
1606 35 : if (sector_size < 0)
1607 21 : return;
1608 7 : } else {
1609 35 : sector_size = read_capacity_10(sdkp, sdp, buffer);
1610 21 : if (sector_size == -EOVERFLOW)
1611 14 : goto got_data;
1612 21 : if (sector_size < 0)
1613 14 : return;
1614 28 : if ((sizeof(sdkp->capacity) > 4) &&
1615 7 : (sdkp->capacity > 0xffffffffULL)) {
1616 14 : int old_sector_size = sector_size;
1617 84 : sd_printk(KERN_NOTICE, sdkp, "Very big device. "
1618 7 : "Trying to use READ CAPACITY(16).\n");
1619 119 : sector_size = read_capacity_16(sdkp, sdp, buffer);
1620 21 : if (sector_size < 0) {
1621 84 : sd_printk(KERN_NOTICE, sdkp,
1622 7 : "Using 0xffffffff as device size\n");
1623 21 : sdkp->capacity = 1 + (sector_t) 0xffffffff;
1624 21 : sector_size = old_sector_size;
1625 21 : goto got_data;
1626 7 : }
1627 7 : }
1628 7 : }
1629 7 :
1630 7 : /* Some devices are known to return the total number of blocks,
1631 : * not the highest block number. Some devices have versions
1632 : * which do this and others which do not. Some devices we might
1633 : * suspect of doing this but we don't know for certain.
1634 : *
1635 : * If we know the reported capacity is wrong, decrement it. If
1636 : * we can only guess, then assume the number of blocks is even
1637 : * (usually true but not always) and err on the side of lowering
1638 : * the capacity.
1639 : */
1640 196 : if (sdp->fix_capacity ||
1641 : (sdp->guess_capacity && (sdkp->capacity & 0x01))) {
1642 238 : sd_printk(KERN_INFO, sdkp, "Adjusting the sector count "
1643 : "from its reported value: %llu\n",
1644 : (unsigned long long) sdkp->capacity);
1645 14 : --sdkp->capacity;
1646 : }
1647 :
1648 : got_data:
1649 154 : if (sector_size == 0) {
1650 56 : sector_size = 512;
1651 420 : sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "
1652 : "assuming 512.\n");
1653 : }
1654 :
1655 700 : if (sector_size != 512 &&
1656 : sector_size != 1024 &&
1657 : sector_size != 2048 &&
1658 : sector_size != 4096 &&
1659 : sector_size != 256) {
1660 518 : sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
1661 : sector_size);
1662 : /*
1663 : * The user might want to re-format the drive with
1664 : * a supported sectorsize. Once this happens, it
1665 : * would be relatively trivial to set the thing up.
1666 : * For this reason, we leave the thing in the table.
1667 : */
1668 14 : sdkp->capacity = 0;
1669 : /*
1670 : * set a bogus sector size so the normal read/write
1671 : * logic in the block layer will eventually refuse any
1672 : * request on this device without tripping over power
1673 : * of two sector size assumptions
1674 : */
1675 14 : sector_size = 512;
1676 : }
1677 84 : blk_queue_logical_block_size(sdp->request_queue, sector_size);
1678 :
1679 : {
1680 : char cap_str_2[10], cap_str_10[10];
1681 182 : u64 sz = (u64)sdkp->capacity << ilog2(sector_size);
1682 :
1683 7 : string_get_size(sz, STRING_UNITS_2, cap_str_2,
1684 : sizeof(cap_str_2));
1685 7 : string_get_size(sz, STRING_UNITS_10, cap_str_10,
1686 : sizeof(cap_str_10));
1687 :
1688 28 : if (sdkp->first_scan || old_capacity != sdkp->capacity) {
1689 91 : sd_printk(KERN_NOTICE, sdkp,
1690 : "%llu %d-byte logical blocks: (%s/%s)\n",
1691 : (unsigned long long)sdkp->capacity,
1692 : sector_size, cap_str_10, cap_str_2);
1693 :
1694 42 : if (sdkp->hw_sector_size != sector_size)
1695 140 : sd_printk(KERN_NOTICE, sdkp,
1696 : "%u-byte physical blocks\n",
1697 : sdkp->hw_sector_size);
1698 : }
1699 : }
1700 :
1701 : /* Rescale capacity to 512-byte units */
1702 70 : if (sector_size == 4096)
1703 35 : sdkp->capacity <<= 3;
1704 70 : else if (sector_size == 2048)
1705 35 : sdkp->capacity <<= 2;
1706 70 : else if (sector_size == 1024)
1707 35 : sdkp->capacity <<= 1;
1708 70 : else if (sector_size == 256)
1709 35 : sdkp->capacity >>= 1;
1710 :
1711 70 : blk_queue_physical_block_size(sdp->request_queue, sdkp->hw_sector_size);
1712 35 : sdkp->device->sector_size = sector_size;
1713 35 : }
1714 :
1715 : /* called with buffer of length 512 */
1716 : static inline int
1717 : sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage,
1718 : unsigned char *buffer, int len, struct scsi_mode_data *data,
1719 : struct scsi_sense_hdr *sshdr)
1720 : {
1721 252 : return scsi_mode_sense(sdp, dbd, modepage, buffer, len,
1722 : SD_TIMEOUT, SD_MAX_RETRIES, data,
1723 : sshdr);
1724 : }
1725 :
1726 : /*
1727 : * read write protect setting, if possible - called only in sd_revalidate_disk()
1728 : * called with buffer of length SD_BUF_SIZE
1729 : */
1730 : static void
1731 : sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer)
1732 : {
1733 14 : int res;
1734 28 : struct scsi_device *sdp = sdkp->device;
1735 14 : struct scsi_mode_data data;
1736 42 : int old_wp = sdkp->write_prot;
1737 14 :
1738 28 : set_disk_ro(sdkp->disk, 0);
1739 42 : if (sdp->skip_ms_page_3f) {
1740 168 : sd_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n");
1741 42 : return;
1742 14 : }
1743 14 :
1744 42 : if (sdp->use_192_bytes_for_3f) {
1745 42 : res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL);
1746 14 : } else {
1747 14 : /*
1748 14 : * First attempt: ask for all pages (0x3F), but only 4 bytes.
1749 14 : * We have to start carefully: some devices hang if we ask
1750 14 : * for more than is available.
1751 14 : */
1752 42 : res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL);
1753 14 :
1754 14 : /*
1755 14 : * Second attempt: ask for page 0 When only page 0 is
1756 : * implemented, a request for page 3F may return Sense Key
1757 : * 5: Illegal Request, Sense Code 24: Invalid field in
1758 : * CDB.
1759 : */
1760 56 : if (!scsi_status_is_good(res))
1761 28 : res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL);
1762 :
1763 : /*
1764 : * Third attempt: ask 255 bytes, as we did earlier.
1765 : */
1766 84 : if (!scsi_status_is_good(res))
1767 28 : res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255,
1768 : &data, NULL);
1769 : }
1770 :
1771 112 : if (!scsi_status_is_good(res)) {
1772 154 : sd_printk(KERN_WARNING, sdkp,
1773 : "Test WP failed, assume Write Enabled\n");
1774 : } else {
1775 14 : sdkp->write_prot = ((data.device_specific & 0x80) != 0);
1776 28 : set_disk_ro(sdkp->disk, sdkp->write_prot);
1777 70 : if (sdkp->first_scan || old_wp != sdkp->write_prot) {
1778 252 : sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",
1779 70 : sdkp->write_prot ? "on" : "off");
1780 252 : sd_printk(KERN_DEBUG, sdkp,
1781 : "Mode Sense: %02x %02x %02x %02x\n",
1782 : buffer[0], buffer[1], buffer[2], buffer[3]);
1783 70 : }
1784 : }
1785 : }
1786 :
1787 : /*
1788 : * sd_read_cache_type - called only from sd_revalidate_disk()
1789 : * called with buffer of length SD_BUF_SIZE
1790 : */
1791 : static void
1792 : sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
1793 : {
1794 14 : int len = 0, res;
1795 14 : struct scsi_device *sdp = sdkp->device;
1796 7 :
1797 7 : int dbd;
1798 7 : int modepage;
1799 7 : struct scsi_mode_data data;
1800 7 : struct scsi_sense_hdr sshdr;
1801 21 : int old_wce = sdkp->WCE;
1802 21 : int old_rcd = sdkp->RCD;
1803 21 : int old_dpofua = sdkp->DPOFUA;
1804 7 :
1805 21 : if (sdp->skip_ms_page_8)
1806 14 : goto defaults;
1807 7 :
1808 28 : if (sdp->type == TYPE_RBC) {
1809 14 : modepage = 6;
1810 14 : dbd = 8;
1811 7 : } else {
1812 14 : modepage = 8;
1813 14 : dbd = 0;
1814 7 : }
1815 7 :
1816 7 : /* cautiously ask */
1817 21 : res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr);
1818 7 :
1819 35 : if (!scsi_status_is_good(res))
1820 14 : goto bad_sense;
1821 7 :
1822 21 : if (!data.header_length) {
1823 14 : modepage = 6;
1824 84 : sd_printk(KERN_ERR, sdkp, "Missing header in MODE_SENSE response\n");
1825 7 : }
1826 7 :
1827 7 : /* that went OK, now ask for the proper length */
1828 28 : len = data.length;
1829 7 :
1830 7 : /*
1831 7 : * We're only interested in the first three bytes, actually.
1832 7 : * But the data cache page is defined for the first 20.
1833 7 : */
1834 49 : if (len < 3)
1835 28 : goto bad_sense;
1836 49 : if (len > 20)
1837 28 : len = 20;
1838 7 :
1839 7 : /* Take headers and block descriptors into account */
1840 21 : len += data.header_length + data.block_descriptor_length;
1841 42 : if (len > SD_BUF_SIZE)
1842 21 : goto bad_sense;
1843 :
1844 : /* Get the data */
1845 42 : res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr);
1846 :
1847 28 : if (scsi_status_is_good(res)) {
1848 7 : int offset = data.header_length + data.block_descriptor_length;
1849 :
1850 14 : if (offset >= SD_BUF_SIZE - 2) {
1851 77 : sd_printk(KERN_ERR, sdkp, "Malformed MODE SENSE response\n");
1852 14 : goto defaults;
1853 : }
1854 :
1855 14 : if ((buffer[offset] & 0x3f) != modepage) {
1856 77 : sd_printk(KERN_ERR, sdkp, "Got wrong page\n");
1857 14 : goto defaults;
1858 : }
1859 :
1860 14 : if (modepage == 8) {
1861 7 : sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
1862 7 : sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
1863 : } else {
1864 7 : sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0);
1865 7 : sdkp->RCD = 0;
1866 : }
1867 :
1868 7 : sdkp->DPOFUA = (data.device_specific & 0x10) != 0;
1869 35 : if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) {
1870 77 : sd_printk(KERN_NOTICE, sdkp,
1871 : "Uses READ/WRITE(6), disabling FUA\n");
1872 14 : sdkp->DPOFUA = 0;
1873 : }
1874 :
1875 231 : if (sdkp->first_scan || old_wce != sdkp->WCE ||
1876 : old_rcd != sdkp->RCD || old_dpofua != sdkp->DPOFUA)
1877 217 : sd_printk(KERN_NOTICE, sdkp,
1878 56 : "Write cache: %s, read cache: %s, %s\n",
1879 56 : sdkp->WCE ? "enabled" : "disabled",
1880 56 : sdkp->RCD ? "disabled" : "enabled",
1881 : sdkp->DPOFUA ? "supports DPO and FUA"
1882 : : "doesn't support DPO or FUA");
1883 :
1884 35 : return;
1885 : }
1886 :
1887 : bad_sense:
1888 119 : if (scsi_sense_valid(&sshdr) &&
1889 : sshdr.sense_key == ILLEGAL_REQUEST &&
1890 : sshdr.asc == 0x24 && sshdr.ascq == 0x0)
1891 : /* Invalid field in CDB */
1892 77 : sd_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n");
1893 : else
1894 77 : sd_printk(KERN_ERR, sdkp, "Asking for cache data failed\n");
1895 :
1896 : defaults:
1897 497 : sd_printk(KERN_ERR, sdkp, "Assuming drive cache: write through\n");
1898 14 : sdkp->WCE = 0;
1899 14 : sdkp->RCD = 0;
1900 14 : sdkp->DPOFUA = 0;
1901 14 : }
1902 :
1903 : /*
1904 : * The ATO bit indicates whether the DIF application tag is available
1905 : * for use by the operating system.
1906 : */
1907 : void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
1908 : {
1909 7 : int res, offset;
1910 14 : struct scsi_device *sdp = sdkp->device;
1911 7 : struct scsi_mode_data data;
1912 7 : struct scsi_sense_hdr sshdr;
1913 7 :
1914 28 : if (sdp->type != TYPE_DISK)
1915 14 : return;
1916 7 :
1917 28 : if (sdkp->protection_type == 0)
1918 14 : return;
1919 7 :
1920 14 : res = scsi_mode_sense(sdp, 1, 0x0a, buffer, 36, SD_TIMEOUT,
1921 7 : SD_MAX_RETRIES, &data, &sshdr);
1922 7 :
1923 63 : if (!scsi_status_is_good(res) || !data.header_length ||
1924 : data.length < 6) {
1925 77 : sd_printk(KERN_WARNING, sdkp,
1926 : "getting Control mode page failed, assume no ATO\n");
1927 :
1928 42 : if (scsi_sense_valid(&sshdr))
1929 21 : sd_print_sense_hdr(sdkp, &sshdr);
1930 :
1931 14 : return;
1932 : }
1933 :
1934 7 : offset = data.header_length + data.block_descriptor_length;
1935 :
1936 14 : if ((buffer[offset] & 0x3f) != 0x0a) {
1937 77 : sd_printk(KERN_ERR, sdkp, "ATO Got wrong page\n");
1938 14 : return;
1939 : }
1940 :
1941 14 : if ((buffer[offset + 5] & 0x80) == 0)
1942 7 : return;
1943 :
1944 7 : sdkp->ATO = 1;
1945 :
1946 7 : return;
1947 : }
1948 :
1949 : /**
1950 : * sd_read_block_limits - Query disk device for preferred I/O sizes.
1951 : * @disk: disk to query
1952 : */
1953 : static void sd_read_block_limits(struct scsi_disk *sdkp)
1954 : {
1955 14 : struct request_queue *q = sdkp->disk->queue;
1956 14 : unsigned int sector_sz = sdkp->device->sector_size;
1957 7 : char *buffer;
1958 7 :
1959 7 : /* Block Limits VPD */
1960 21 : buffer = scsi_get_vpd_page(sdkp->device, 0xb0);
1961 7 :
1962 21 : if (buffer == NULL)
1963 14 : return;
1964 7 :
1965 21 : blk_queue_io_min(sdkp->disk->queue,
1966 : get_unaligned_be16(&buffer[6]) * sector_sz);
1967 21 : blk_queue_io_opt(sdkp->disk->queue,
1968 : get_unaligned_be32(&buffer[12]) * sector_sz);
1969 :
1970 : /* Thin provisioning enabled and page length indicates TP support */
1971 28 : if (sdkp->thin_provisioning && buffer[3] == 0x3c) {
1972 : unsigned int lba_count, desc_count, granularity;
1973 :
1974 14 : lba_count = get_unaligned_be32(&buffer[20]);
1975 14 : desc_count = get_unaligned_be32(&buffer[24]);
1976 :
1977 14 : if (lba_count) {
1978 7 : q->limits.max_discard_sectors =
1979 : lba_count * sector_sz >> 9;
1980 :
1981 14 : if (desc_count)
1982 7 : sdkp->unmap = 1;
1983 : }
1984 :
1985 14 : granularity = get_unaligned_be32(&buffer[28]);
1986 :
1987 14 : if (granularity)
1988 7 : q->limits.discard_granularity = granularity * sector_sz;
1989 :
1990 14 : if (buffer[32] & 0x80)
1991 21 : q->limits.discard_alignment =
1992 : get_unaligned_be32(&buffer[32]) & ~(1 << 31);
1993 : }
1994 :
1995 21 : kfree(buffer);
1996 21 : }
1997 :
1998 : /**
1999 : * sd_read_block_characteristics - Query block dev. characteristics
2000 : * @disk: disk to query
2001 : */
2002 : static void sd_read_block_characteristics(struct scsi_disk *sdkp)
2003 : {
2004 7 : char *buffer;
2005 7 : u16 rot;
2006 7 :
2007 : /* Block Device Characteristics VPD */
2008 14 : buffer = scsi_get_vpd_page(sdkp->device, 0xb1);
2009 :
2010 14 : if (buffer == NULL)
2011 7 : return;
2012 :
2013 14 : rot = get_unaligned_be16(&buffer[4]);
2014 :
2015 14 : if (rot == 1)
2016 14 : queue_flag_set_unlocked(QUEUE_FLAG_NONROT, sdkp->disk->queue);
2017 :
2018 14 : kfree(buffer);
2019 14 : }
2020 :
2021 : static int sd_try_extended_inquiry(struct scsi_device *sdp)
2022 : {
2023 : /*
2024 : * Although VPD inquiries can go to SCSI-2 type devices,
2025 : * some USB ones crash on receiving them, and the pages
2026 : * we currently ask for are for SPC-3 and beyond
2027 : */
2028 21 : if (sdp->scsi_level > SCSI_SPC_2)
2029 7 : return 1;
2030 7 : return 0;
2031 : }
2032 :
2033 : /**
2034 : * sd_revalidate_disk - called the first time a new disk is seen,
2035 : * performs disk spin up, read_capacity, etc.
2036 : * @disk: struct gendisk we care about
2037 : **/
2038 : static int sd_revalidate_disk(struct gendisk *disk)
2039 : {
2040 28 : struct scsi_disk *sdkp = scsi_disk(disk);
2041 14 : struct scsi_device *sdp = sdkp->device;
2042 7 : unsigned char *buffer;
2043 7 : unsigned ordered;
2044 7 :
2045 7 : SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
2046 7 : "sd_revalidate_disk\n"));
2047 7 :
2048 7 : /*
2049 7 : * If the device is offline, don't try and read capacity or any
2050 7 : * of the other niceties.
2051 7 : */
2052 28 : if (!scsi_device_online(sdp))
2053 7 : goto out;
2054 :
2055 21 : buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL);
2056 14 : if (!buffer) {
2057 77 : sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory "
2058 : "allocation failure.\n");
2059 14 : goto out;
2060 : }
2061 :
2062 70 : sd_spinup_disk(sdkp);
2063 :
2064 : /*
2065 : * Without media there is no reason to ask; moreover, some devices
2066 : * react badly if we do.
2067 : */
2068 21 : if (sdkp->media_present) {
2069 63 : sd_read_capacity(sdkp, buffer);
2070 :
2071 28 : if (sd_try_extended_inquiry(sdp)) {
2072 35 : sd_read_block_limits(sdkp);
2073 28 : sd_read_block_characteristics(sdkp);
2074 : }
2075 :
2076 112 : sd_read_write_protect_flag(sdkp, buffer);
2077 56 : sd_read_cache_type(sdkp, buffer);
2078 49 : sd_read_app_tag_own(sdkp, buffer);
2079 : }
2080 :
2081 14 : sdkp->first_scan = 0;
2082 :
2083 : /*
2084 : * We now have all cache related info, determine how we deal
2085 : * with ordered requests. Note that as the current SCSI
2086 : * dispatch function can alter request order, we cannot use
2087 : * QUEUE_ORDERED_TAG_* even when ordered tag is supported.
2088 : */
2089 28 : if (sdkp->WCE)
2090 84 : ordered = sdkp->DPOFUA
2091 : ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH;
2092 : else
2093 14 : ordered = QUEUE_ORDERED_DRAIN;
2094 :
2095 14 : blk_queue_ordered(sdkp->disk->queue, ordered, sd_prepare_flush);
2096 :
2097 28 : set_capacity(disk, sdkp->capacity);
2098 7 : kfree(buffer);
2099 :
2100 7 : out:
2101 28 : return 0;
2102 : }
2103 :
2104 : /**
2105 : * sd_format_disk_name - format disk name
2106 : * @prefix: name prefix - ie. "sd" for SCSI disks
2107 : * @index: index of the disk to format name for
2108 : * @buf: output buffer
2109 : * @buflen: length of the output buffer
2110 : *
2111 : * SCSI disk names starts at sda. The 26th device is sdz and the
2112 : * 27th is sdaa. The last one for two lettered suffix is sdzz
2113 : * which is followed by sdaaa.
2114 : *
2115 : * This is basically 26 base counting with one extra 'nil' entry
2116 : * at the beggining from the second digit on and can be
2117 : * determined using similar method as 26 base conversion with the
2118 : * index shifted -1 after each digit is computed.
2119 : *
2120 : * CONTEXT:
2121 : * Don't care.
2122 : *
2123 : * RETURNS:
2124 : * 0 on success, -errno on failure.
2125 : */
2126 : static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen)
2127 : {
2128 14 : const int base = 'z' - 'a' + 1;
2129 21 : char *begin = buf + strlen(prefix);
2130 14 : char *end = buf + buflen;
2131 7 : char *p;
2132 7 : int unit;
2133 7 :
2134 14 : p = end - 1;
2135 7 : *p = '\0';
2136 7 : unit = base;
2137 : do {
2138 21 : if (p == begin)
2139 7 : return -EINVAL;
2140 14 : *--p = 'a' + (index % unit);
2141 7 : index = (index / unit) - 1;
2142 14 : } while (index >= 0);
2143 :
2144 14 : memmove(begin, p, end - p);
2145 14 : memcpy(buf, prefix, strlen(prefix));
2146 :
2147 7 : return 0;
2148 : }
2149 :
2150 : /*
2151 : * The asynchronous part of sd_probe
2152 : */
2153 : static void sd_probe_async(void *data, async_cookie_t cookie)
2154 : {
2155 0 : struct scsi_disk *sdkp = data;
2156 0 : struct scsi_device *sdp;
2157 0 : struct gendisk *gd;
2158 0 : u32 index;
2159 0 : struct device *dev;
2160 0 :
2161 0 : sdp = sdkp->device;
2162 0 : gd = sdkp->disk;
2163 0 : index = sdkp->index;
2164 0 : dev = &sdp->sdev_gendev;
2165 :
2166 0 : gd->major = sd_major((index & 0xf0) >> 4);
2167 0 : gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
2168 0 : gd->minors = SD_MINORS;
2169 :
2170 0 : gd->fops = &sd_fops;
2171 0 : gd->private_data = &sdkp->driver;
2172 0 : gd->queue = sdkp->device->request_queue;
2173 :
2174 : /* defaults, until the device tells us otherwise */
2175 0 : sdp->sector_size = 512;
2176 0 : sdkp->capacity = 0;
2177 0 : sdkp->media_present = 1;
2178 0 : sdkp->write_prot = 0;
2179 0 : sdkp->WCE = 0;
2180 0 : sdkp->RCD = 0;
2181 0 : sdkp->ATO = 0;
2182 0 : sdkp->first_scan = 1;
2183 :
2184 0 : sd_revalidate_disk(gd);
2185 :
2186 0 : blk_queue_prep_rq(sdp->request_queue, sd_prep_fn);
2187 :
2188 0 : gd->driverfs_dev = &sdp->sdev_gendev;
2189 0 : gd->flags = GENHD_FL_EXT_DEVT | GENHD_FL_DRIVERFS;
2190 0 : if (sdp->removable)
2191 0 : gd->flags |= GENHD_FL_REMOVABLE;
2192 :
2193 0 : dev_set_drvdata(dev, sdkp);
2194 0 : add_disk(gd);
2195 0 : sd_dif_config_host(sdkp);
2196 :
2197 0 : sd_revalidate_disk(gd);
2198 :
2199 0 : sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
2200 0 : sdp->removable ? "removable " : "");
2201 0 : put_device(&sdkp->dev);
2202 0 : }
2203 :
2204 : /**
2205 : * sd_probe - called during driver initialization and whenever a
2206 : * new scsi device is attached to the system. It is called once
2207 : * for each scsi device (not just disks) present.
2208 : * @dev: pointer to device object
2209 : *
2210 : * Returns 0 if successful (or not interested in this scsi device
2211 : * (e.g. scanner)); 1 when there is an error.
2212 : *
2213 : * Note: this function is invoked from the scsi mid-level.
2214 : * This function sets up the mapping between a given
2215 : * <host,channel,id,lun> (found in sdp) and new device name
2216 : * (e.g. /dev/sda). More precisely it is the block device major
2217 : * and minor number that is chosen here.
2218 : *
2219 : * Assume sd_attach is not re-entrant (for time being)
2220 : * Also think about sd_attach() and sd_remove() running coincidentally.
2221 : **/
2222 : static int sd_probe(struct device *dev)
2223 : {
2224 21 : struct scsi_device *sdp = to_scsi_device(dev);
2225 7 : struct scsi_disk *sdkp;
2226 7 : struct gendisk *gd;
2227 7 : u32 index;
2228 7 : int error;
2229 7 :
2230 14 : error = -ENODEV;
2231 70 : if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
2232 14 : goto out;
2233 7 :
2234 7 : SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
2235 7 : "sd_attach\n"));
2236 :
2237 7 : error = -ENOMEM;
2238 21 : sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
2239 14 : if (!sdkp)
2240 7 : goto out;
2241 :
2242 7 : gd = alloc_disk(SD_MINORS);
2243 14 : if (!gd)
2244 7 : goto out_free;
2245 :
2246 : do {
2247 21 : if (!ida_pre_get(&sd_index_ida, GFP_KERNEL))
2248 14 : goto out_put;
2249 :
2250 14 : spin_lock(&sd_index_lock);
2251 7 : error = ida_get_new(&sd_index_ida, &index);
2252 14 : spin_unlock(&sd_index_lock);
2253 14 : } while (error == -EAGAIN);
2254 :
2255 21 : if (error)
2256 7 : goto out_put;
2257 :
2258 14 : if (index >= SD_MAX_DISKS) {
2259 7 : error = -ENODEV;
2260 28 : sdev_printk(KERN_WARNING, sdp, "SCSI disk (sd) name space exhausted.\n");
2261 7 : goto out_free_index;
2262 : }
2263 :
2264 14 : error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN);
2265 14 : if (error)
2266 7 : goto out_free_index;
2267 :
2268 7 : sdkp->device = sdp;
2269 7 : sdkp->driver = &sd_template;
2270 7 : sdkp->disk = gd;
2271 7 : sdkp->index = index;
2272 7 : sdkp->openers = 0;
2273 7 : sdkp->previous_state = 1;
2274 :
2275 14 : if (!sdp->request_queue->rq_timeout) {
2276 21 : if (sdp->type != TYPE_MOD)
2277 7 : blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT);
2278 : else
2279 7 : blk_queue_rq_timeout(sdp->request_queue,
2280 : SD_MOD_TIMEOUT);
2281 : }
2282 :
2283 7 : device_initialize(&sdkp->dev);
2284 7 : sdkp->dev.parent = &sdp->sdev_gendev;
2285 7 : sdkp->dev.class = &sd_disk_class;
2286 21 : dev_set_name(&sdkp->dev, dev_name(&sdp->sdev_gendev));
2287 :
2288 21 : if (device_add(&sdkp->dev))
2289 7 : goto out_free_index;
2290 :
2291 7 : get_device(&sdp->sdev_gendev);
2292 :
2293 7 : get_device(&sdkp->dev); /* prevent release before async_schedule */
2294 7 : async_schedule(sd_probe_async, sdkp);
2295 :
2296 7 : return 0;
2297 21 :
2298 : out_free_index:
2299 42 : spin_lock(&sd_index_lock);
2300 7 : ida_remove(&sd_index_ida, index);
2301 14 : spin_unlock(&sd_index_lock);
2302 : out_put:
2303 28 : put_disk(gd);
2304 : out_free:
2305 49 : kfree(sdkp);
2306 : out:
2307 63 : return error;
2308 : }
2309 :
2310 : /**
2311 : * sd_remove - called whenever a scsi disk (previously recognized by
2312 : * sd_probe) is detached from the system. It is called (potentially
2313 : * multiple times) during sd module unload.
2314 : * @sdp: pointer to mid level scsi device object
2315 : *
2316 : * Note: this function is invoked from the scsi mid-level.
2317 : * This function potentially frees up a device name (e.g. /dev/sdc)
2318 : * that could be re-used by a subsequent sd_probe().
2319 : * This function is not called when the built-in sd driver is "exit-ed".
2320 : **/
2321 : static int sd_remove(struct device *dev)
2322 : {
2323 7 : struct scsi_disk *sdkp;
2324 7 :
2325 7 : async_synchronize_full();
2326 21 : sdkp = dev_get_drvdata(dev);
2327 7 : blk_queue_prep_rq(sdkp->device->request_queue, scsi_prep_fn);
2328 7 : device_del(&sdkp->dev);
2329 7 : del_gendisk(sdkp->disk);
2330 21 : sd_shutdown(dev);
2331 :
2332 7 : mutex_lock(&sd_ref_mutex);
2333 7 : dev_set_drvdata(dev, NULL);
2334 7 : put_device(&sdkp->dev);
2335 7 : mutex_unlock(&sd_ref_mutex);
2336 :
2337 7 : return 0;
2338 : }
2339 :
2340 : /**
2341 : * scsi_disk_release - Called to free the scsi_disk structure
2342 : * @dev: pointer to embedded class device
2343 : *
2344 : * sd_ref_mutex must be held entering this routine. Because it is
2345 : * called on last put, you should always use the scsi_disk_get()
2346 : * scsi_disk_put() helpers which manipulate the semaphore directly
2347 : * and never do a direct put_device.
2348 : **/
2349 : static void scsi_disk_release(struct device *dev)
2350 : {
2351 21 : struct scsi_disk *sdkp = to_scsi_disk(dev);
2352 14 : struct gendisk *disk = sdkp->disk;
2353 7 :
2354 14 : spin_lock(&sd_index_lock);
2355 14 : ida_remove(&sd_index_ida, sdkp->index);
2356 14 : spin_unlock(&sd_index_lock);
2357 :
2358 7 : disk->private_data = NULL;
2359 7 : put_disk(disk);
2360 7 : put_device(&sdkp->device->sdev_gendev);
2361 :
2362 7 : kfree(sdkp);
2363 7 : }
2364 :
2365 : static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
2366 : {
2367 620 : unsigned char cmd[6] = { START_STOP }; /* START_VALID */
2368 124 : struct scsi_sense_hdr sshdr;
2369 186 : struct scsi_device *sdp = sdkp->device;
2370 62 : int res;
2371 62 :
2372 186 : if (start)
2373 124 : cmd[4] |= 1; /* START */
2374 62 :
2375 186 : if (sdp->start_stop_pwr_cond)
2376 434 : cmd[4] |= start ? 1 << 4 : 3 << 4; /* Active or Standby */
2377 :
2378 248 : if (!scsi_device_online(sdp))
2379 62 : return -ENODEV;
2380 :
2381 62 : res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
2382 : SD_TIMEOUT, SD_MAX_RETRIES, NULL);
2383 124 : if (res) {
2384 682 : sd_printk(KERN_WARNING, sdkp, "START_STOP FAILED\n");
2385 372 : sd_print_result(sdkp, res);
2386 124 : if (driver_byte(res) & DRIVER_SENSE)
2387 186 : sd_print_sense_hdr(sdkp, &sshdr);
2388 : }
2389 :
2390 186 : return res;
2391 : }
2392 :
2393 : /*
2394 : * Send a SYNCHRONIZE CACHE instruction down to the device through
2395 : * the normal SCSI command structure. Wait for the command to
2396 : * complete.
2397 : */
2398 : static void sd_shutdown(struct device *dev)
2399 : {
2400 70 : struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
2401 14 :
2402 42 : if (!sdkp)
2403 28 : return; /* this can happen */
2404 14 :
2405 42 : if (sdkp->WCE) {
2406 168 : sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
2407 182 : sd_sync_cache(sdkp);
2408 14 : }
2409 14 :
2410 140 : if (system_state != SYSTEM_RESTART && sdkp->device->manage_start_stop) {
2411 252 : sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
2412 112 : sd_start_stop_device(sdkp, 0);
2413 : }
2414 :
2415 84 : scsi_disk_put(sdkp);
2416 14 : }
2417 :
2418 : static int sd_suspend(struct device *dev, pm_message_t mesg)
2419 : {
2420 35 : struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
2421 14 : int ret = 0;
2422 7 :
2423 21 : if (!sdkp)
2424 14 : return 0; /* this can happen */
2425 7 :
2426 21 : if (sdkp->WCE) {
2427 84 : sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n");
2428 91 : ret = sd_sync_cache(sdkp);
2429 21 : if (ret)
2430 14 : goto done;
2431 : }
2432 :
2433 70 : if ((mesg.event & PM_EVENT_SLEEP) && sdkp->device->manage_start_stop) {
2434 126 : sd_printk(KERN_NOTICE, sdkp, "Stopping disk\n");
2435 56 : ret = sd_start_stop_device(sdkp, 0);
2436 : }
2437 :
2438 : done:
2439 42 : scsi_disk_put(sdkp);
2440 28 : return ret;
2441 : }
2442 :
2443 : static int sd_resume(struct device *dev)
2444 : {
2445 50 : struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev);
2446 20 : int ret = 0;
2447 10 :
2448 40 : if (!sdkp->device->manage_start_stop)
2449 20 : goto done;
2450 10 :
2451 120 : sd_printk(KERN_NOTICE, sdkp, "Starting disk\n");
2452 80 : ret = sd_start_stop_device(sdkp, 1);
2453 :
2454 10 : done:
2455 40 : scsi_disk_put(sdkp);
2456 10 : return ret;
2457 : }
2458 :
2459 : /**
2460 : * init_sd - entry point for this driver (both when built in or when
2461 : * a module).
2462 : *
2463 : * Note: this function registers this driver with the scsi mid-level.
2464 : **/
2465 : static int __init init_sd(void)
2466 : {
2467 2 : int majors = 0, i, err;
2468 1 :
2469 1 : SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n"));
2470 1 :
2471 6 : for (i = 0; i < SD_MAJORS; i++)
2472 8 : if (register_blkdev(sd_major(i), "sd") == 0)
2473 3 : majors++;
2474 1 :
2475 2 : if (!majors)
2476 1 : return -ENODEV;
2477 :
2478 2 : err = class_register(&sd_disk_class);
2479 2 : if (err)
2480 1 : goto err_out;
2481 :
2482 2 : err = scsi_register_driver(&sd_template.gendrv);
2483 2 : if (err)
2484 1 : goto err_out_class;
2485 :
2486 1 : sd_cdb_cache = kmem_cache_create("sd_ext_cdb", SD_EXT_CDB_SIZE,
2487 : 0, 0, NULL);
2488 2 : if (!sd_cdb_cache) {
2489 1 : printk(KERN_ERR "sd: can't init extended cdb cache\n");
2490 1 : goto err_out_class;
2491 : }
2492 :
2493 2 : sd_cdb_pool = mempool_create_slab_pool(SD_MEMPOOL_SIZE, sd_cdb_cache);
2494 2 : if (!sd_cdb_pool) {
2495 1 : printk(KERN_ERR "sd: can't init extended cdb pool\n");
2496 1 : goto err_out_cache;
2497 : }
2498 :
2499 1 : return 0;
2500 1 :
2501 : err_out_cache:
2502 1 : kmem_cache_destroy(sd_cdb_cache);
2503 :
2504 1 : err_out_class:
2505 2 : class_unregister(&sd_disk_class);
2506 : err_out:
2507 11 : for (i = 0; i < SD_MAJORS; i++)
2508 4 : unregister_blkdev(sd_major(i), "sd");
2509 3 : return err;
2510 : }
2511 :
2512 : /**
2513 : * exit_sd - exit point for this driver (when it is a module).
2514 : *
2515 : * Note: this function unregisters this driver from the scsi mid-level.
2516 : **/
2517 : static void __exit exit_sd(void)
2518 : {
2519 2 : int i;
2520 2 :
2521 : SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n"));
2522 :
2523 2 : mempool_destroy(sd_cdb_pool);
2524 2 : kmem_cache_destroy(sd_cdb_cache);
2525 :
2526 4 : scsi_unregister_driver(&sd_template.gendrv);
2527 2 : class_unregister(&sd_disk_class);
2528 :
2529 10 : for (i = 0; i < SD_MAJORS; i++)
2530 8 : unregister_blkdev(sd_major(i), "sd");
2531 4 : }
2532 :
2533 : module_init(init_sd);
2534 : module_exit(exit_sd);
2535 :
2536 2 : static void sd_print_sense_hdr(struct scsi_disk *sdkp,
2537 : struct scsi_sense_hdr *sshdr)
2538 196 : {
2539 2352 : sd_printk(KERN_INFO, sdkp, "");
2540 588 : scsi_show_sense_hdr(sshdr);
2541 3724 : sd_printk(KERN_INFO, sdkp, "");
2542 1372 : scsi_show_extd_sense(sshdr->asc, sshdr->ascq);
2543 588 : }
2544 196 :
2545 196 : static void sd_print_result(struct scsi_disk *sdkp, int result)
2546 : {
2547 3504 : sd_printk(KERN_INFO, sdkp, "");
2548 876 : scsi_show_result(result);
2549 876 : }
2550 293 :
|