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