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