]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
1da177e4 LT |
35 | #include <linux/module.h> |
36 | #include <linux/fs.h> | |
37 | #include <linux/kernel.h> | |
1da177e4 LT |
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> | |
1da177e4 | 48 | #include <linux/delay.h> |
0b950672 | 49 | #include <linux/mutex.h> |
1da177e4 LT |
50 | #include <asm/uaccess.h> |
51 | ||
52 | #include <scsi/scsi.h> | |
53 | #include <scsi/scsi_cmnd.h> | |
54 | #include <scsi/scsi_dbg.h> | |
55 | #include <scsi/scsi_device.h> | |
56 | #include <scsi/scsi_driver.h> | |
57 | #include <scsi/scsi_eh.h> | |
58 | #include <scsi/scsi_host.h> | |
59 | #include <scsi/scsi_ioctl.h> | |
1da177e4 | 60 | #include <scsi/scsicam.h> |
e73aec82 | 61 | #include <scsi/sd.h> |
1da177e4 LT |
62 | |
63 | #include "scsi_logging.h" | |
64 | ||
f018fa55 RH |
65 | MODULE_AUTHOR("Eric Youngdale"); |
66 | MODULE_DESCRIPTION("SCSI disk (sd) driver"); | |
67 | MODULE_LICENSE("GPL"); | |
68 | ||
69 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK0_MAJOR); | |
70 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK1_MAJOR); | |
71 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK2_MAJOR); | |
72 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK3_MAJOR); | |
73 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK4_MAJOR); | |
74 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK5_MAJOR); | |
75 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK6_MAJOR); | |
76 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK7_MAJOR); | |
77 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK8_MAJOR); | |
78 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK9_MAJOR); | |
79 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK10_MAJOR); | |
80 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK11_MAJOR); | |
81 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK12_MAJOR); | |
82 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK13_MAJOR); | |
83 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK14_MAJOR); | |
84 | MODULE_ALIAS_BLOCKDEV_MAJOR(SCSI_DISK15_MAJOR); | |
85 | ||
1da177e4 LT |
86 | static DEFINE_IDR(sd_index_idr); |
87 | static DEFINE_SPINLOCK(sd_index_lock); | |
88 | ||
89 | /* This semaphore is used to mediate the 0->1 reference get in the | |
90 | * face of object destruction (i.e. we can't allow a get on an | |
91 | * object after last put) */ | |
0b950672 | 92 | static DEFINE_MUTEX(sd_ref_mutex); |
1da177e4 | 93 | |
6bdaa1f1 JB |
94 | static const char *sd_cache_types[] = { |
95 | "write through", "none", "write back", | |
96 | "write back, no read (daft)" | |
97 | }; | |
98 | ||
99 | static ssize_t sd_store_cache_type(struct class_device *cdev, const char *buf, | |
100 | size_t count) | |
101 | { | |
102 | int i, ct = -1, rcd, wce, sp; | |
103 | struct scsi_disk *sdkp = to_scsi_disk(cdev); | |
104 | struct scsi_device *sdp = sdkp->device; | |
105 | char buffer[64]; | |
106 | char *buffer_data; | |
107 | struct scsi_mode_data data; | |
108 | struct scsi_sense_hdr sshdr; | |
109 | int len; | |
110 | ||
111 | if (sdp->type != TYPE_DISK) | |
112 | /* no cache control on RBC devices; theoretically they | |
113 | * can do it, but there's probably so many exceptions | |
114 | * it's not worth the risk */ | |
115 | return -EINVAL; | |
116 | ||
6391a113 | 117 | for (i = 0; i < ARRAY_SIZE(sd_cache_types); i++) { |
6bdaa1f1 JB |
118 | const int len = strlen(sd_cache_types[i]); |
119 | if (strncmp(sd_cache_types[i], buf, len) == 0 && | |
120 | buf[len] == '\n') { | |
121 | ct = i; | |
122 | break; | |
123 | } | |
124 | } | |
125 | if (ct < 0) | |
126 | return -EINVAL; | |
127 | rcd = ct & 0x01 ? 1 : 0; | |
128 | wce = ct & 0x02 ? 1 : 0; | |
129 | if (scsi_mode_sense(sdp, 0x08, 8, buffer, sizeof(buffer), SD_TIMEOUT, | |
130 | SD_MAX_RETRIES, &data, NULL)) | |
131 | return -EINVAL; | |
a9312fb8 | 132 | len = min_t(size_t, sizeof(buffer), data.length - data.header_length - |
6bdaa1f1 JB |
133 | data.block_descriptor_length); |
134 | buffer_data = buffer + data.header_length + | |
135 | data.block_descriptor_length; | |
136 | buffer_data[2] &= ~0x05; | |
137 | buffer_data[2] |= wce << 2 | rcd; | |
138 | sp = buffer_data[0] & 0x80 ? 1 : 0; | |
139 | ||
140 | if (scsi_mode_select(sdp, 1, sp, 8, buffer_data, len, SD_TIMEOUT, | |
141 | SD_MAX_RETRIES, &data, &sshdr)) { | |
142 | if (scsi_sense_valid(&sshdr)) | |
e73aec82 | 143 | sd_print_sense_hdr(sdkp, &sshdr); |
6bdaa1f1 JB |
144 | return -EINVAL; |
145 | } | |
146 | sd_revalidate_disk(sdkp->disk); | |
147 | return count; | |
148 | } | |
149 | ||
c3c94c5a TH |
150 | static ssize_t sd_store_manage_start_stop(struct class_device *cdev, |
151 | const char *buf, size_t count) | |
152 | { | |
153 | struct scsi_disk *sdkp = to_scsi_disk(cdev); | |
154 | struct scsi_device *sdp = sdkp->device; | |
155 | ||
156 | if (!capable(CAP_SYS_ADMIN)) | |
157 | return -EACCES; | |
158 | ||
159 | sdp->manage_start_stop = simple_strtoul(buf, NULL, 10); | |
160 | ||
161 | return count; | |
162 | } | |
163 | ||
a144c5ae BK |
164 | static ssize_t sd_store_allow_restart(struct class_device *cdev, const char *buf, |
165 | size_t count) | |
166 | { | |
167 | struct scsi_disk *sdkp = to_scsi_disk(cdev); | |
168 | struct scsi_device *sdp = sdkp->device; | |
169 | ||
170 | if (!capable(CAP_SYS_ADMIN)) | |
171 | return -EACCES; | |
172 | ||
173 | if (sdp->type != TYPE_DISK) | |
174 | return -EINVAL; | |
175 | ||
176 | sdp->allow_restart = simple_strtoul(buf, NULL, 10); | |
177 | ||
178 | return count; | |
179 | } | |
180 | ||
6bdaa1f1 JB |
181 | static ssize_t sd_show_cache_type(struct class_device *cdev, char *buf) |
182 | { | |
183 | struct scsi_disk *sdkp = to_scsi_disk(cdev); | |
184 | int ct = sdkp->RCD + 2*sdkp->WCE; | |
185 | ||
186 | return snprintf(buf, 40, "%s\n", sd_cache_types[ct]); | |
187 | } | |
188 | ||
189 | static ssize_t sd_show_fua(struct class_device *cdev, char *buf) | |
190 | { | |
191 | struct scsi_disk *sdkp = to_scsi_disk(cdev); | |
192 | ||
193 | return snprintf(buf, 20, "%u\n", sdkp->DPOFUA); | |
194 | } | |
195 | ||
c3c94c5a TH |
196 | static ssize_t sd_show_manage_start_stop(struct class_device *cdev, char *buf) |
197 | { | |
198 | struct scsi_disk *sdkp = to_scsi_disk(cdev); | |
199 | struct scsi_device *sdp = sdkp->device; | |
200 | ||
201 | return snprintf(buf, 20, "%u\n", sdp->manage_start_stop); | |
202 | } | |
203 | ||
a144c5ae BK |
204 | static ssize_t sd_show_allow_restart(struct class_device *cdev, char *buf) |
205 | { | |
206 | struct scsi_disk *sdkp = to_scsi_disk(cdev); | |
207 | ||
208 | return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart); | |
209 | } | |
210 | ||
6bdaa1f1 JB |
211 | static struct class_device_attribute sd_disk_attrs[] = { |
212 | __ATTR(cache_type, S_IRUGO|S_IWUSR, sd_show_cache_type, | |
213 | sd_store_cache_type), | |
214 | __ATTR(FUA, S_IRUGO, sd_show_fua, NULL), | |
a144c5ae BK |
215 | __ATTR(allow_restart, S_IRUGO|S_IWUSR, sd_show_allow_restart, |
216 | sd_store_allow_restart), | |
c3c94c5a TH |
217 | __ATTR(manage_start_stop, S_IRUGO|S_IWUSR, sd_show_manage_start_stop, |
218 | sd_store_manage_start_stop), | |
6bdaa1f1 JB |
219 | __ATTR_NULL, |
220 | }; | |
221 | ||
222 | static struct class sd_disk_class = { | |
223 | .name = "scsi_disk", | |
224 | .owner = THIS_MODULE, | |
225 | .release = scsi_disk_release, | |
226 | .class_dev_attrs = sd_disk_attrs, | |
227 | }; | |
1da177e4 LT |
228 | |
229 | static struct scsi_driver sd_template = { | |
230 | .owner = THIS_MODULE, | |
231 | .gendrv = { | |
232 | .name = "sd", | |
233 | .probe = sd_probe, | |
234 | .remove = sd_remove, | |
c3c94c5a TH |
235 | .suspend = sd_suspend, |
236 | .resume = sd_resume, | |
1da177e4 LT |
237 | .shutdown = sd_shutdown, |
238 | }, | |
239 | .rescan = sd_rescan, | |
240 | .init_command = sd_init_command, | |
241 | .issue_flush = sd_issue_flush, | |
1da177e4 LT |
242 | }; |
243 | ||
244 | /* | |
245 | * Device no to disk mapping: | |
246 | * | |
247 | * major disc2 disc p1 | |
248 | * |............|.............|....|....| <- dev_t | |
249 | * 31 20 19 8 7 4 3 0 | |
250 | * | |
251 | * Inside a major, we have 16k disks, however mapped non- | |
252 | * contiguously. The first 16 disks are for major0, the next | |
253 | * ones with major1, ... Disk 256 is for major0 again, disk 272 | |
254 | * for major1, ... | |
255 | * As we stay compatible with our numbering scheme, we can reuse | |
256 | * the well-know SCSI majors 8, 65--71, 136--143. | |
257 | */ | |
258 | static int sd_major(int major_idx) | |
259 | { | |
260 | switch (major_idx) { | |
261 | case 0: | |
262 | return SCSI_DISK0_MAJOR; | |
263 | case 1 ... 7: | |
264 | return SCSI_DISK1_MAJOR + major_idx - 1; | |
265 | case 8 ... 15: | |
266 | return SCSI_DISK8_MAJOR + major_idx - 8; | |
267 | default: | |
268 | BUG(); | |
269 | return 0; /* shut up gcc */ | |
270 | } | |
271 | } | |
272 | ||
1da177e4 LT |
273 | static inline struct scsi_disk *scsi_disk(struct gendisk *disk) |
274 | { | |
275 | return container_of(disk->private_data, struct scsi_disk, driver); | |
276 | } | |
277 | ||
39b7f1e2 | 278 | static struct scsi_disk *__scsi_disk_get(struct gendisk *disk) |
1da177e4 LT |
279 | { |
280 | struct scsi_disk *sdkp = NULL; | |
281 | ||
39b7f1e2 AS |
282 | if (disk->private_data) { |
283 | sdkp = scsi_disk(disk); | |
284 | if (scsi_device_get(sdkp->device) == 0) | |
6bdaa1f1 | 285 | class_device_get(&sdkp->cdev); |
39b7f1e2 AS |
286 | else |
287 | sdkp = NULL; | |
288 | } | |
289 | return sdkp; | |
290 | } | |
291 | ||
292 | static struct scsi_disk *scsi_disk_get(struct gendisk *disk) | |
293 | { | |
294 | struct scsi_disk *sdkp; | |
295 | ||
0b950672 | 296 | mutex_lock(&sd_ref_mutex); |
39b7f1e2 | 297 | sdkp = __scsi_disk_get(disk); |
0b950672 | 298 | mutex_unlock(&sd_ref_mutex); |
1da177e4 | 299 | return sdkp; |
39b7f1e2 | 300 | } |
1da177e4 | 301 | |
39b7f1e2 AS |
302 | static struct scsi_disk *scsi_disk_get_from_dev(struct device *dev) |
303 | { | |
304 | struct scsi_disk *sdkp; | |
305 | ||
0b950672 | 306 | mutex_lock(&sd_ref_mutex); |
39b7f1e2 AS |
307 | sdkp = dev_get_drvdata(dev); |
308 | if (sdkp) | |
309 | sdkp = __scsi_disk_get(sdkp->disk); | |
0b950672 | 310 | mutex_unlock(&sd_ref_mutex); |
1da177e4 LT |
311 | return sdkp; |
312 | } | |
313 | ||
314 | static void scsi_disk_put(struct scsi_disk *sdkp) | |
315 | { | |
316 | struct scsi_device *sdev = sdkp->device; | |
317 | ||
0b950672 | 318 | mutex_lock(&sd_ref_mutex); |
6bdaa1f1 | 319 | class_device_put(&sdkp->cdev); |
1da177e4 | 320 | scsi_device_put(sdev); |
0b950672 | 321 | mutex_unlock(&sd_ref_mutex); |
1da177e4 LT |
322 | } |
323 | ||
324 | /** | |
325 | * sd_init_command - build a scsi (read or write) command from | |
326 | * information in the request structure. | |
327 | * @SCpnt: pointer to mid-level's per scsi command structure that | |
328 | * contains request and into which the scsi command is written | |
329 | * | |
330 | * Returns 1 if successful and 0 if error (or cannot be done now). | |
331 | **/ | |
332 | static int sd_init_command(struct scsi_cmnd * SCpnt) | |
333 | { | |
1da177e4 LT |
334 | struct scsi_device *sdp = SCpnt->device; |
335 | struct request *rq = SCpnt->request; | |
776b23a0 CH |
336 | struct gendisk *disk = rq->rq_disk; |
337 | sector_t block = rq->sector; | |
338 | unsigned int this_count = SCpnt->request_bufflen >> 9; | |
339 | unsigned int timeout = sdp->timeout; | |
1da177e4 | 340 | |
fa0d34be MP |
341 | SCSI_LOG_HLQUEUE(1, scmd_printk(KERN_INFO, SCpnt, |
342 | "sd_init_command: block=%llu, " | |
343 | "count=%d\n", | |
344 | (unsigned long long)block, | |
345 | this_count)); | |
1da177e4 LT |
346 | |
347 | if (!sdp || !scsi_device_online(sdp) || | |
348 | block + rq->nr_sectors > get_capacity(disk)) { | |
fa0d34be MP |
349 | SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, |
350 | "Finishing %ld sectors\n", | |
351 | rq->nr_sectors)); | |
352 | SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, | |
353 | "Retry with 0x%p\n", SCpnt)); | |
1da177e4 LT |
354 | return 0; |
355 | } | |
356 | ||
357 | if (sdp->changed) { | |
358 | /* | |
359 | * quietly refuse to do anything to a changed disc until | |
360 | * the changed bit has been reset | |
361 | */ | |
362 | /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */ | |
363 | return 0; | |
364 | } | |
fa0d34be MP |
365 | SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, "block=%llu\n", |
366 | (unsigned long long)block)); | |
1da177e4 LT |
367 | |
368 | /* | |
369 | * If we have a 1K hardware sectorsize, prevent access to single | |
370 | * 512 byte sectors. In theory we could handle this - in fact | |
371 | * the scsi cdrom driver must be able to handle this because | |
372 | * we typically use 1K blocksizes, and cdroms typically have | |
373 | * 2K hardware sectorsizes. Of course, things are simpler | |
374 | * with the cdrom, since it is read-only. For performance | |
375 | * reasons, the filesystems should be able to handle this | |
376 | * and not force the scsi disk driver to use bounce buffers | |
377 | * for this. | |
378 | */ | |
379 | if (sdp->sector_size == 1024) { | |
380 | if ((block & 1) || (rq->nr_sectors & 1)) { | |
e73aec82 MP |
381 | scmd_printk(KERN_ERR, SCpnt, |
382 | "Bad block number requested\n"); | |
1da177e4 LT |
383 | return 0; |
384 | } else { | |
385 | block = block >> 1; | |
386 | this_count = this_count >> 1; | |
387 | } | |
388 | } | |
389 | if (sdp->sector_size == 2048) { | |
390 | if ((block & 3) || (rq->nr_sectors & 3)) { | |
e73aec82 MP |
391 | scmd_printk(KERN_ERR, SCpnt, |
392 | "Bad block number requested\n"); | |
1da177e4 LT |
393 | return 0; |
394 | } else { | |
395 | block = block >> 2; | |
396 | this_count = this_count >> 2; | |
397 | } | |
398 | } | |
399 | if (sdp->sector_size == 4096) { | |
400 | if ((block & 7) || (rq->nr_sectors & 7)) { | |
e73aec82 MP |
401 | scmd_printk(KERN_ERR, SCpnt, |
402 | "Bad block number requested\n"); | |
1da177e4 LT |
403 | return 0; |
404 | } else { | |
405 | block = block >> 3; | |
406 | this_count = this_count >> 3; | |
407 | } | |
408 | } | |
409 | if (rq_data_dir(rq) == WRITE) { | |
410 | if (!sdp->writeable) { | |
411 | return 0; | |
412 | } | |
413 | SCpnt->cmnd[0] = WRITE_6; | |
414 | SCpnt->sc_data_direction = DMA_TO_DEVICE; | |
415 | } else if (rq_data_dir(rq) == READ) { | |
416 | SCpnt->cmnd[0] = READ_6; | |
417 | SCpnt->sc_data_direction = DMA_FROM_DEVICE; | |
418 | } else { | |
e73aec82 | 419 | scmd_printk(KERN_ERR, SCpnt, "Unknown command %x\n", rq->cmd_flags); |
1da177e4 LT |
420 | return 0; |
421 | } | |
422 | ||
fa0d34be MP |
423 | SCSI_LOG_HLQUEUE(2, scmd_printk(KERN_INFO, SCpnt, |
424 | "%s %d/%ld 512 byte blocks.\n", | |
425 | (rq_data_dir(rq) == WRITE) ? | |
426 | "writing" : "reading", this_count, | |
427 | rq->nr_sectors)); | |
1da177e4 LT |
428 | |
429 | SCpnt->cmnd[1] = 0; | |
430 | ||
431 | if (block > 0xffffffff) { | |
432 | SCpnt->cmnd[0] += READ_16 - READ_6; | |
007365ad | 433 | SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0; |
1da177e4 LT |
434 | SCpnt->cmnd[2] = sizeof(block) > 4 ? (unsigned char) (block >> 56) & 0xff : 0; |
435 | SCpnt->cmnd[3] = sizeof(block) > 4 ? (unsigned char) (block >> 48) & 0xff : 0; | |
436 | SCpnt->cmnd[4] = sizeof(block) > 4 ? (unsigned char) (block >> 40) & 0xff : 0; | |
437 | SCpnt->cmnd[5] = sizeof(block) > 4 ? (unsigned char) (block >> 32) & 0xff : 0; | |
438 | SCpnt->cmnd[6] = (unsigned char) (block >> 24) & 0xff; | |
439 | SCpnt->cmnd[7] = (unsigned char) (block >> 16) & 0xff; | |
440 | SCpnt->cmnd[8] = (unsigned char) (block >> 8) & 0xff; | |
441 | SCpnt->cmnd[9] = (unsigned char) block & 0xff; | |
442 | SCpnt->cmnd[10] = (unsigned char) (this_count >> 24) & 0xff; | |
443 | SCpnt->cmnd[11] = (unsigned char) (this_count >> 16) & 0xff; | |
444 | SCpnt->cmnd[12] = (unsigned char) (this_count >> 8) & 0xff; | |
445 | SCpnt->cmnd[13] = (unsigned char) this_count & 0xff; | |
446 | SCpnt->cmnd[14] = SCpnt->cmnd[15] = 0; | |
447 | } else if ((this_count > 0xff) || (block > 0x1fffff) || | |
448 | SCpnt->device->use_10_for_rw) { | |
449 | if (this_count > 0xffff) | |
450 | this_count = 0xffff; | |
451 | ||
452 | SCpnt->cmnd[0] += READ_10 - READ_6; | |
007365ad | 453 | SCpnt->cmnd[1] |= blk_fua_rq(rq) ? 0x8 : 0; |
1da177e4 LT |
454 | SCpnt->cmnd[2] = (unsigned char) (block >> 24) & 0xff; |
455 | SCpnt->cmnd[3] = (unsigned char) (block >> 16) & 0xff; | |
456 | SCpnt->cmnd[4] = (unsigned char) (block >> 8) & 0xff; | |
457 | SCpnt->cmnd[5] = (unsigned char) block & 0xff; | |
458 | SCpnt->cmnd[6] = SCpnt->cmnd[9] = 0; | |
459 | SCpnt->cmnd[7] = (unsigned char) (this_count >> 8) & 0xff; | |
460 | SCpnt->cmnd[8] = (unsigned char) this_count & 0xff; | |
461 | } else { | |
007365ad TH |
462 | if (unlikely(blk_fua_rq(rq))) { |
463 | /* | |
464 | * This happens only if this drive failed | |
465 | * 10byte rw command with ILLEGAL_REQUEST | |
466 | * during operation and thus turned off | |
467 | * use_10_for_rw. | |
468 | */ | |
e73aec82 MP |
469 | scmd_printk(KERN_ERR, SCpnt, |
470 | "FUA write on READ/WRITE(6) drive\n"); | |
007365ad TH |
471 | return 0; |
472 | } | |
473 | ||
1da177e4 LT |
474 | SCpnt->cmnd[1] |= (unsigned char) ((block >> 16) & 0x1f); |
475 | SCpnt->cmnd[2] = (unsigned char) ((block >> 8) & 0xff); | |
476 | SCpnt->cmnd[3] = (unsigned char) block & 0xff; | |
477 | SCpnt->cmnd[4] = (unsigned char) this_count; | |
478 | SCpnt->cmnd[5] = 0; | |
479 | } | |
631c228c | 480 | SCpnt->request_bufflen = this_count * sdp->sector_size; |
1da177e4 LT |
481 | |
482 | /* | |
483 | * We shouldn't disconnect in the middle of a sector, so with a dumb | |
484 | * host adapter, it's safe to assume that we can at least transfer | |
485 | * this many bytes between each connect / disconnect. | |
486 | */ | |
487 | SCpnt->transfersize = sdp->sector_size; | |
488 | SCpnt->underflow = this_count << 9; | |
489 | SCpnt->allowed = SD_MAX_RETRIES; | |
1da177e4 LT |
490 | SCpnt->timeout_per_command = timeout; |
491 | ||
492 | /* | |
493 | * This is the completion routine we use. This is matched in terms | |
494 | * of capability to this function. | |
495 | */ | |
496 | SCpnt->done = sd_rw_intr; | |
497 | ||
498 | /* | |
499 | * This indicates that the command is ready from our end to be | |
500 | * queued. | |
501 | */ | |
502 | return 1; | |
503 | } | |
504 | ||
505 | /** | |
506 | * sd_open - open a scsi disk device | |
507 | * @inode: only i_rdev member may be used | |
508 | * @filp: only f_mode and f_flags may be used | |
509 | * | |
510 | * Returns 0 if successful. Returns a negated errno value in case | |
511 | * of error. | |
512 | * | |
513 | * Note: This can be called from a user context (e.g. fsck(1) ) | |
514 | * or from within the kernel (e.g. as a result of a mount(1) ). | |
515 | * In the latter case @inode and @filp carry an abridged amount | |
516 | * of information as noted above. | |
517 | **/ | |
518 | static int sd_open(struct inode *inode, struct file *filp) | |
519 | { | |
520 | struct gendisk *disk = inode->i_bdev->bd_disk; | |
521 | struct scsi_disk *sdkp; | |
522 | struct scsi_device *sdev; | |
523 | int retval; | |
524 | ||
525 | if (!(sdkp = scsi_disk_get(disk))) | |
526 | return -ENXIO; | |
527 | ||
528 | ||
fa0d34be | 529 | SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n")); |
1da177e4 LT |
530 | |
531 | sdev = sdkp->device; | |
532 | ||
533 | /* | |
534 | * If the device is in error recovery, wait until it is done. | |
535 | * If the device is offline, then disallow any access to it. | |
536 | */ | |
537 | retval = -ENXIO; | |
538 | if (!scsi_block_when_processing_errors(sdev)) | |
539 | goto error_out; | |
540 | ||
541 | if (sdev->removable || sdkp->write_prot) | |
542 | check_disk_change(inode->i_bdev); | |
543 | ||
544 | /* | |
545 | * If the drive is empty, just let the open fail. | |
546 | */ | |
547 | retval = -ENOMEDIUM; | |
548 | if (sdev->removable && !sdkp->media_present && | |
549 | !(filp->f_flags & O_NDELAY)) | |
550 | goto error_out; | |
551 | ||
552 | /* | |
553 | * If the device has the write protect tab set, have the open fail | |
554 | * if the user expects to be able to write to the thing. | |
555 | */ | |
556 | retval = -EROFS; | |
557 | if (sdkp->write_prot && (filp->f_mode & FMODE_WRITE)) | |
558 | goto error_out; | |
559 | ||
560 | /* | |
561 | * It is possible that the disk changing stuff resulted in | |
562 | * the device being taken offline. If this is the case, | |
563 | * report this to the user, and don't pretend that the | |
564 | * open actually succeeded. | |
565 | */ | |
566 | retval = -ENXIO; | |
567 | if (!scsi_device_online(sdev)) | |
568 | goto error_out; | |
569 | ||
570 | if (!sdkp->openers++ && sdev->removable) { | |
571 | if (scsi_block_when_processing_errors(sdev)) | |
572 | scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT); | |
573 | } | |
574 | ||
575 | return 0; | |
576 | ||
577 | error_out: | |
578 | scsi_disk_put(sdkp); | |
579 | return retval; | |
580 | } | |
581 | ||
582 | /** | |
583 | * sd_release - invoked when the (last) close(2) is called on this | |
584 | * scsi disk. | |
585 | * @inode: only i_rdev member may be used | |
586 | * @filp: only f_mode and f_flags may be used | |
587 | * | |
588 | * Returns 0. | |
589 | * | |
590 | * Note: may block (uninterruptible) if error recovery is underway | |
591 | * on this disk. | |
592 | **/ | |
593 | static int sd_release(struct inode *inode, struct file *filp) | |
594 | { | |
595 | struct gendisk *disk = inode->i_bdev->bd_disk; | |
596 | struct scsi_disk *sdkp = scsi_disk(disk); | |
597 | struct scsi_device *sdev = sdkp->device; | |
598 | ||
56937f7b | 599 | SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n")); |
1da177e4 LT |
600 | |
601 | if (!--sdkp->openers && sdev->removable) { | |
602 | if (scsi_block_when_processing_errors(sdev)) | |
603 | scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW); | |
604 | } | |
605 | ||
606 | /* | |
607 | * XXX and what if there are packets in flight and this close() | |
608 | * XXX is followed by a "rmmod sd_mod"? | |
609 | */ | |
610 | scsi_disk_put(sdkp); | |
611 | return 0; | |
612 | } | |
613 | ||
a885c8c4 | 614 | static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
1da177e4 LT |
615 | { |
616 | struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk); | |
617 | struct scsi_device *sdp = sdkp->device; | |
618 | struct Scsi_Host *host = sdp->host; | |
619 | int diskinfo[4]; | |
620 | ||
621 | /* default to most commonly used values */ | |
622 | diskinfo[0] = 0x40; /* 1 << 6 */ | |
623 | diskinfo[1] = 0x20; /* 1 << 5 */ | |
624 | diskinfo[2] = sdkp->capacity >> 11; | |
625 | ||
626 | /* override with calculated, extended default, or driver values */ | |
627 | if (host->hostt->bios_param) | |
628 | host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo); | |
629 | else | |
630 | scsicam_bios_param(bdev, sdkp->capacity, diskinfo); | |
631 | ||
a885c8c4 CH |
632 | geo->heads = diskinfo[0]; |
633 | geo->sectors = diskinfo[1]; | |
634 | geo->cylinders = diskinfo[2]; | |
1da177e4 LT |
635 | return 0; |
636 | } | |
637 | ||
638 | /** | |
639 | * sd_ioctl - process an ioctl | |
640 | * @inode: only i_rdev/i_bdev members may be used | |
641 | * @filp: only f_mode and f_flags may be used | |
642 | * @cmd: ioctl command number | |
643 | * @arg: this is third argument given to ioctl(2) system call. | |
644 | * Often contains a pointer. | |
645 | * | |
646 | * Returns 0 if successful (some ioctls return postive numbers on | |
647 | * success as well). Returns a negated errno value in case of error. | |
648 | * | |
649 | * Note: most ioctls are forward onto the block subsystem or further | |
650 | * down in the scsi subsytem. | |
651 | **/ | |
652 | static int sd_ioctl(struct inode * inode, struct file * filp, | |
653 | unsigned int cmd, unsigned long arg) | |
654 | { | |
655 | struct block_device *bdev = inode->i_bdev; | |
656 | struct gendisk *disk = bdev->bd_disk; | |
657 | struct scsi_device *sdp = scsi_disk(disk)->device; | |
658 | void __user *p = (void __user *)arg; | |
659 | int error; | |
660 | ||
661 | SCSI_LOG_IOCTL(1, printk("sd_ioctl: disk=%s, cmd=0x%x\n", | |
662 | disk->disk_name, cmd)); | |
663 | ||
664 | /* | |
665 | * If we are in the middle of error recovery, don't let anyone | |
666 | * else try and use this device. Also, if error recovery fails, it | |
667 | * may try and take the device offline, in which case all further | |
668 | * access to the device is prohibited. | |
669 | */ | |
670 | error = scsi_nonblockable_ioctl(sdp, cmd, p, filp); | |
671 | if (!scsi_block_when_processing_errors(sdp) || !error) | |
672 | return error; | |
673 | ||
1da177e4 LT |
674 | /* |
675 | * Send SCSI addressing ioctls directly to mid level, send other | |
676 | * ioctls to block level and then onto mid level if they can't be | |
677 | * resolved. | |
678 | */ | |
679 | switch (cmd) { | |
680 | case SCSI_IOCTL_GET_IDLUN: | |
681 | case SCSI_IOCTL_GET_BUS_NUMBER: | |
682 | return scsi_ioctl(sdp, cmd, p); | |
683 | default: | |
684 | error = scsi_cmd_ioctl(filp, disk, cmd, p); | |
685 | if (error != -ENOTTY) | |
686 | return error; | |
687 | } | |
688 | return scsi_ioctl(sdp, cmd, p); | |
689 | } | |
690 | ||
691 | static void set_media_not_present(struct scsi_disk *sdkp) | |
692 | { | |
693 | sdkp->media_present = 0; | |
694 | sdkp->capacity = 0; | |
695 | sdkp->device->changed = 1; | |
696 | } | |
697 | ||
698 | /** | |
699 | * sd_media_changed - check if our medium changed | |
700 | * @disk: kernel device descriptor | |
701 | * | |
702 | * Returns 0 if not applicable or no change; 1 if change | |
703 | * | |
704 | * Note: this function is invoked from the block subsystem. | |
705 | **/ | |
706 | static int sd_media_changed(struct gendisk *disk) | |
707 | { | |
708 | struct scsi_disk *sdkp = scsi_disk(disk); | |
709 | struct scsi_device *sdp = sdkp->device; | |
710 | int retval; | |
711 | ||
fa0d34be | 712 | SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_media_changed\n")); |
1da177e4 LT |
713 | |
714 | if (!sdp->removable) | |
715 | return 0; | |
716 | ||
717 | /* | |
718 | * If the device is offline, don't send any commands - just pretend as | |
719 | * if the command failed. If the device ever comes back online, we | |
720 | * can deal with it then. It is only because of unrecoverable errors | |
721 | * that we would ever take a device offline in the first place. | |
722 | */ | |
723 | if (!scsi_device_online(sdp)) | |
724 | goto not_present; | |
725 | ||
726 | /* | |
727 | * Using TEST_UNIT_READY enables differentiation between drive with | |
728 | * no cartridge loaded - NOT READY, drive with changed cartridge - | |
729 | * UNIT ATTENTION, or with same cartridge - GOOD STATUS. | |
730 | * | |
731 | * Drives that auto spin down. eg iomega jaz 1G, will be started | |
732 | * by sd_spinup_disk() from sd_revalidate_disk(), which happens whenever | |
733 | * sd_revalidate() is called. | |
734 | */ | |
735 | retval = -ENODEV; | |
736 | if (scsi_block_when_processing_errors(sdp)) | |
737 | retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, SD_MAX_RETRIES); | |
738 | ||
739 | /* | |
740 | * Unable to test, unit probably not ready. This usually | |
741 | * means there is no disc in the drive. Mark as changed, | |
742 | * and we will figure it out later once the drive is | |
743 | * available again. | |
744 | */ | |
745 | if (retval) | |
746 | goto not_present; | |
747 | ||
748 | /* | |
749 | * For removable scsi disk we have to recognise the presence | |
750 | * of a disk in the drive. This is kept in the struct scsi_disk | |
751 | * struct and tested at open ! Daniel Roche (dan@lectra.fr) | |
752 | */ | |
753 | sdkp->media_present = 1; | |
754 | ||
755 | retval = sdp->changed; | |
756 | sdp->changed = 0; | |
757 | ||
758 | return retval; | |
759 | ||
760 | not_present: | |
761 | set_media_not_present(sdkp); | |
762 | return 1; | |
763 | } | |
764 | ||
e73aec82 | 765 | static int sd_sync_cache(struct scsi_disk *sdkp) |
1da177e4 | 766 | { |
1da177e4 | 767 | int retries, res; |
e73aec82 | 768 | struct scsi_device *sdp = sdkp->device; |
ea73a9f2 | 769 | struct scsi_sense_hdr sshdr; |
1da177e4 LT |
770 | |
771 | if (!scsi_device_online(sdp)) | |
772 | return -ENODEV; | |
773 | ||
1da177e4 | 774 | |
1da177e4 LT |
775 | for (retries = 3; retries > 0; --retries) { |
776 | unsigned char cmd[10] = { 0 }; | |
777 | ||
778 | cmd[0] = SYNCHRONIZE_CACHE; | |
779 | /* | |
780 | * Leave the rest of the command zero to indicate | |
781 | * flush everything. | |
782 | */ | |
ea73a9f2 JB |
783 | res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr, |
784 | SD_TIMEOUT, SD_MAX_RETRIES); | |
785 | if (res == 0) | |
1da177e4 LT |
786 | break; |
787 | } | |
788 | ||
e73aec82 MP |
789 | if (res) { |
790 | sd_print_result(sdkp, res); | |
791 | if (driver_byte(res) & DRIVER_SENSE) | |
792 | sd_print_sense_hdr(sdkp, &sshdr); | |
1da177e4 LT |
793 | } |
794 | ||
3721050a TH |
795 | if (res) |
796 | return -EIO; | |
797 | return 0; | |
1da177e4 LT |
798 | } |
799 | ||
800 | static int sd_issue_flush(struct device *dev, sector_t *error_sector) | |
801 | { | |
39b7f1e2 | 802 | int ret = 0; |
39b7f1e2 | 803 | struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev); |
1da177e4 LT |
804 | |
805 | if (!sdkp) | |
806 | return -ENODEV; | |
807 | ||
39b7f1e2 | 808 | if (sdkp->WCE) |
e73aec82 | 809 | ret = sd_sync_cache(sdkp); |
39b7f1e2 AS |
810 | scsi_disk_put(sdkp); |
811 | return ret; | |
1da177e4 LT |
812 | } |
813 | ||
461d4e90 | 814 | static void sd_prepare_flush(request_queue_t *q, struct request *rq) |
1da177e4 | 815 | { |
c0ed79a3 | 816 | memset(rq->cmd, 0, sizeof(rq->cmd)); |
4aff5e23 | 817 | rq->cmd_type = REQ_TYPE_BLOCK_PC; |
c0ed79a3 JB |
818 | rq->timeout = SD_TIMEOUT; |
819 | rq->cmd[0] = SYNCHRONIZE_CACHE; | |
461d4e90 | 820 | rq->cmd_len = 10; |
1da177e4 LT |
821 | } |
822 | ||
823 | static void sd_rescan(struct device *dev) | |
824 | { | |
39b7f1e2 AS |
825 | struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev); |
826 | ||
827 | if (sdkp) { | |
828 | sd_revalidate_disk(sdkp->disk); | |
829 | scsi_disk_put(sdkp); | |
830 | } | |
1da177e4 LT |
831 | } |
832 | ||
833 | ||
834 | #ifdef CONFIG_COMPAT | |
835 | /* | |
836 | * This gets directly called from VFS. When the ioctl | |
837 | * is not recognized we go back to the other translation paths. | |
838 | */ | |
839 | static long sd_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
840 | { | |
7ac6207b | 841 | struct block_device *bdev = file->f_path.dentry->d_inode->i_bdev; |
1da177e4 LT |
842 | struct gendisk *disk = bdev->bd_disk; |
843 | struct scsi_device *sdev = scsi_disk(disk)->device; | |
844 | ||
845 | /* | |
846 | * If we are in the middle of error recovery, don't let anyone | |
847 | * else try and use this device. Also, if error recovery fails, it | |
848 | * may try and take the device offline, in which case all further | |
849 | * access to the device is prohibited. | |
850 | */ | |
851 | if (!scsi_block_when_processing_errors(sdev)) | |
852 | return -ENODEV; | |
853 | ||
854 | if (sdev->host->hostt->compat_ioctl) { | |
855 | int ret; | |
856 | ||
857 | ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg); | |
858 | ||
859 | return ret; | |
860 | } | |
861 | ||
862 | /* | |
863 | * Let the static ioctl translation table take care of it. | |
864 | */ | |
865 | return -ENOIOCTLCMD; | |
866 | } | |
867 | #endif | |
868 | ||
869 | static struct block_device_operations sd_fops = { | |
870 | .owner = THIS_MODULE, | |
871 | .open = sd_open, | |
872 | .release = sd_release, | |
873 | .ioctl = sd_ioctl, | |
a885c8c4 | 874 | .getgeo = sd_getgeo, |
1da177e4 LT |
875 | #ifdef CONFIG_COMPAT |
876 | .compat_ioctl = sd_compat_ioctl, | |
877 | #endif | |
878 | .media_changed = sd_media_changed, | |
879 | .revalidate_disk = sd_revalidate_disk, | |
880 | }; | |
881 | ||
882 | /** | |
883 | * sd_rw_intr - bottom half handler: called when the lower level | |
884 | * driver has completed (successfully or otherwise) a scsi command. | |
885 | * @SCpnt: mid-level's per command structure. | |
886 | * | |
887 | * Note: potentially run from within an ISR. Must not block. | |
888 | **/ | |
889 | static void sd_rw_intr(struct scsi_cmnd * SCpnt) | |
890 | { | |
891 | int result = SCpnt->result; | |
03aba2f7 LT |
892 | unsigned int xfer_size = SCpnt->request_bufflen; |
893 | unsigned int good_bytes = result ? 0 : xfer_size; | |
894 | u64 start_lba = SCpnt->request->sector; | |
895 | u64 bad_lba; | |
1da177e4 LT |
896 | struct scsi_sense_hdr sshdr; |
897 | int sense_valid = 0; | |
898 | int sense_deferred = 0; | |
899 | int info_valid; | |
900 | ||
901 | if (result) { | |
902 | sense_valid = scsi_command_normalize_sense(SCpnt, &sshdr); | |
903 | if (sense_valid) | |
904 | sense_deferred = scsi_sense_is_deferred(&sshdr); | |
905 | } | |
1da177e4 | 906 | #ifdef CONFIG_SCSI_LOGGING |
fa0d34be | 907 | SCSI_LOG_HLCOMPLETE(1, scsi_print_result(SCpnt)); |
1da177e4 | 908 | if (sense_valid) { |
fa0d34be MP |
909 | SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, SCpnt, |
910 | "sd_rw_intr: sb[respc,sk,asc," | |
911 | "ascq]=%x,%x,%x,%x\n", | |
912 | sshdr.response_code, | |
913 | sshdr.sense_key, sshdr.asc, | |
914 | sshdr.ascq)); | |
1da177e4 LT |
915 | } |
916 | #endif | |
03aba2f7 LT |
917 | if (driver_byte(result) != DRIVER_SENSE && |
918 | (!sense_valid || sense_deferred)) | |
919 | goto out; | |
920 | ||
921 | switch (sshdr.sense_key) { | |
922 | case HARDWARE_ERROR: | |
923 | case MEDIUM_ERROR: | |
924 | if (!blk_fs_request(SCpnt->request)) | |
925 | goto out; | |
926 | info_valid = scsi_get_sense_info_fld(SCpnt->sense_buffer, | |
927 | SCSI_SENSE_BUFFERSIZE, | |
928 | &bad_lba); | |
929 | if (!info_valid) | |
930 | goto out; | |
931 | if (xfer_size <= SCpnt->device->sector_size) | |
932 | goto out; | |
933 | switch (SCpnt->device->sector_size) { | |
934 | case 256: | |
935 | start_lba <<= 1; | |
1da177e4 | 936 | break; |
03aba2f7 | 937 | case 512: |
1da177e4 | 938 | break; |
03aba2f7 LT |
939 | case 1024: |
940 | start_lba >>= 1; | |
941 | break; | |
942 | case 2048: | |
943 | start_lba >>= 2; | |
944 | break; | |
945 | case 4096: | |
946 | start_lba >>= 3; | |
1da177e4 | 947 | break; |
1da177e4 | 948 | default: |
03aba2f7 LT |
949 | /* Print something here with limiting frequency. */ |
950 | goto out; | |
1da177e4 LT |
951 | break; |
952 | } | |
03aba2f7 LT |
953 | /* This computation should always be done in terms of |
954 | * the resolution of the device's medium. | |
955 | */ | |
956 | good_bytes = (bad_lba - start_lba)*SCpnt->device->sector_size; | |
957 | break; | |
958 | case RECOVERED_ERROR: | |
959 | case NO_SENSE: | |
960 | /* Inform the user, but make sure that it's not treated | |
961 | * as a hard error. | |
962 | */ | |
963 | scsi_print_sense("sd", SCpnt); | |
964 | SCpnt->result = 0; | |
965 | memset(SCpnt->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE); | |
966 | good_bytes = xfer_size; | |
967 | break; | |
968 | case ILLEGAL_REQUEST: | |
969 | if (SCpnt->device->use_10_for_rw && | |
970 | (SCpnt->cmnd[0] == READ_10 || | |
971 | SCpnt->cmnd[0] == WRITE_10)) | |
972 | SCpnt->device->use_10_for_rw = 0; | |
973 | if (SCpnt->device->use_10_for_ms && | |
974 | (SCpnt->cmnd[0] == MODE_SENSE_10 || | |
975 | SCpnt->cmnd[0] == MODE_SELECT_10)) | |
976 | SCpnt->device->use_10_for_ms = 0; | |
977 | break; | |
978 | default: | |
979 | break; | |
1da177e4 | 980 | } |
03aba2f7 LT |
981 | out: |
982 | scsi_io_completion(SCpnt, good_bytes); | |
1da177e4 LT |
983 | } |
984 | ||
ea73a9f2 JB |
985 | static int media_not_present(struct scsi_disk *sdkp, |
986 | struct scsi_sense_hdr *sshdr) | |
1da177e4 | 987 | { |
1da177e4 | 988 | |
ea73a9f2 | 989 | if (!scsi_sense_valid(sshdr)) |
1da177e4 LT |
990 | return 0; |
991 | /* not invoked for commands that could return deferred errors */ | |
ea73a9f2 JB |
992 | if (sshdr->sense_key != NOT_READY && |
993 | sshdr->sense_key != UNIT_ATTENTION) | |
994 | return 0; | |
995 | if (sshdr->asc != 0x3A) /* medium not present */ | |
996 | return 0; | |
997 | ||
1da177e4 LT |
998 | set_media_not_present(sdkp); |
999 | return 1; | |
1000 | } | |
1001 | ||
1002 | /* | |
1003 | * spinup disk - called only in sd_revalidate_disk() | |
1004 | */ | |
1005 | static void | |
e73aec82 | 1006 | sd_spinup_disk(struct scsi_disk *sdkp) |
ea73a9f2 | 1007 | { |
1da177e4 | 1008 | unsigned char cmd[10]; |
4451e472 | 1009 | unsigned long spintime_expire = 0; |
1da177e4 LT |
1010 | int retries, spintime; |
1011 | unsigned int the_result; | |
1012 | struct scsi_sense_hdr sshdr; | |
1013 | int sense_valid = 0; | |
1014 | ||
1015 | spintime = 0; | |
1016 | ||
1017 | /* Spin up drives, as required. Only do this at boot time */ | |
1018 | /* Spinup needs to be done for module loads too. */ | |
1019 | do { | |
1020 | retries = 0; | |
1021 | ||
1022 | do { | |
1023 | cmd[0] = TEST_UNIT_READY; | |
1024 | memset((void *) &cmd[1], 0, 9); | |
1025 | ||
ea73a9f2 JB |
1026 | the_result = scsi_execute_req(sdkp->device, cmd, |
1027 | DMA_NONE, NULL, 0, | |
1028 | &sshdr, SD_TIMEOUT, | |
1029 | SD_MAX_RETRIES); | |
1da177e4 | 1030 | |
b4d38e38 AS |
1031 | /* |
1032 | * If the drive has indicated to us that it | |
1033 | * doesn't have any media in it, don't bother | |
1034 | * with any more polling. | |
1035 | */ | |
1036 | if (media_not_present(sdkp, &sshdr)) | |
1037 | return; | |
1038 | ||
1da177e4 | 1039 | if (the_result) |
ea73a9f2 | 1040 | sense_valid = scsi_sense_valid(&sshdr); |
1da177e4 LT |
1041 | retries++; |
1042 | } while (retries < 3 && | |
1043 | (!scsi_status_is_good(the_result) || | |
1044 | ((driver_byte(the_result) & DRIVER_SENSE) && | |
1045 | sense_valid && sshdr.sense_key == UNIT_ATTENTION))); | |
1046 | ||
1da177e4 LT |
1047 | if ((driver_byte(the_result) & DRIVER_SENSE) == 0) { |
1048 | /* no sense, TUR either succeeded or failed | |
1049 | * with a status error */ | |
e73aec82 MP |
1050 | if(!spintime && !scsi_status_is_good(the_result)) { |
1051 | sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n"); | |
1052 | sd_print_result(sdkp, the_result); | |
1053 | } | |
1da177e4 LT |
1054 | break; |
1055 | } | |
1056 | ||
1057 | /* | |
1058 | * The device does not want the automatic start to be issued. | |
1059 | */ | |
1060 | if (sdkp->device->no_start_on_add) { | |
1061 | break; | |
1062 | } | |
1063 | ||
1064 | /* | |
1065 | * If manual intervention is required, or this is an | |
1066 | * absent USB storage device, a spinup is meaningless. | |
1067 | */ | |
1068 | if (sense_valid && | |
1069 | sshdr.sense_key == NOT_READY && | |
1070 | sshdr.asc == 4 && sshdr.ascq == 3) { | |
1071 | break; /* manual intervention required */ | |
1072 | ||
1073 | /* | |
1074 | * Issue command to spin up drive when not ready | |
1075 | */ | |
1076 | } else if (sense_valid && sshdr.sense_key == NOT_READY) { | |
1077 | if (!spintime) { | |
e73aec82 | 1078 | sd_printk(KERN_NOTICE, sdkp, "Spinning up disk..."); |
1da177e4 LT |
1079 | cmd[0] = START_STOP; |
1080 | cmd[1] = 1; /* Return immediately */ | |
1081 | memset((void *) &cmd[2], 0, 8); | |
1082 | cmd[4] = 1; /* Start spin cycle */ | |
ea73a9f2 JB |
1083 | scsi_execute_req(sdkp->device, cmd, DMA_NONE, |
1084 | NULL, 0, &sshdr, | |
1085 | SD_TIMEOUT, SD_MAX_RETRIES); | |
4451e472 AS |
1086 | spintime_expire = jiffies + 100 * HZ; |
1087 | spintime = 1; | |
1da177e4 | 1088 | } |
1da177e4 LT |
1089 | /* Wait 1 second for next try */ |
1090 | msleep(1000); | |
1091 | printk("."); | |
4451e472 AS |
1092 | |
1093 | /* | |
1094 | * Wait for USB flash devices with slow firmware. | |
1095 | * Yes, this sense key/ASC combination shouldn't | |
1096 | * occur here. It's characteristic of these devices. | |
1097 | */ | |
1098 | } else if (sense_valid && | |
1099 | sshdr.sense_key == UNIT_ATTENTION && | |
1100 | sshdr.asc == 0x28) { | |
1101 | if (!spintime) { | |
1102 | spintime_expire = jiffies + 5 * HZ; | |
1103 | spintime = 1; | |
1104 | } | |
1105 | /* Wait 1 second for next try */ | |
1106 | msleep(1000); | |
1da177e4 LT |
1107 | } else { |
1108 | /* we don't understand the sense code, so it's | |
1109 | * probably pointless to loop */ | |
1110 | if(!spintime) { | |
e73aec82 MP |
1111 | sd_printk(KERN_NOTICE, sdkp, "Unit Not Ready\n"); |
1112 | sd_print_sense_hdr(sdkp, &sshdr); | |
1da177e4 LT |
1113 | } |
1114 | break; | |
1115 | } | |
1116 | ||
4451e472 | 1117 | } while (spintime && time_before_eq(jiffies, spintime_expire)); |
1da177e4 LT |
1118 | |
1119 | if (spintime) { | |
1120 | if (scsi_status_is_good(the_result)) | |
1121 | printk("ready\n"); | |
1122 | else | |
1123 | printk("not responding...\n"); | |
1124 | } | |
1125 | } | |
1126 | ||
1127 | /* | |
1128 | * read disk capacity | |
1129 | */ | |
1130 | static void | |
e73aec82 | 1131 | sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer) |
ea73a9f2 | 1132 | { |
1da177e4 | 1133 | unsigned char cmd[16]; |
1da177e4 LT |
1134 | int the_result, retries; |
1135 | int sector_size = 0; | |
1136 | int longrc = 0; | |
1137 | struct scsi_sense_hdr sshdr; | |
1138 | int sense_valid = 0; | |
ea73a9f2 | 1139 | struct scsi_device *sdp = sdkp->device; |
1da177e4 LT |
1140 | |
1141 | repeat: | |
1142 | retries = 3; | |
1143 | do { | |
1144 | if (longrc) { | |
1145 | memset((void *) cmd, 0, 16); | |
1146 | cmd[0] = SERVICE_ACTION_IN; | |
1147 | cmd[1] = SAI_READ_CAPACITY_16; | |
1148 | cmd[13] = 12; | |
1149 | memset((void *) buffer, 0, 12); | |
1150 | } else { | |
1151 | cmd[0] = READ_CAPACITY; | |
1152 | memset((void *) &cmd[1], 0, 9); | |
1153 | memset((void *) buffer, 0, 8); | |
1154 | } | |
1155 | ||
ea73a9f2 JB |
1156 | the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE, |
1157 | buffer, longrc ? 12 : 8, &sshdr, | |
1158 | SD_TIMEOUT, SD_MAX_RETRIES); | |
1da177e4 | 1159 | |
ea73a9f2 | 1160 | if (media_not_present(sdkp, &sshdr)) |
1da177e4 LT |
1161 | return; |
1162 | ||
1da177e4 | 1163 | if (the_result) |
ea73a9f2 | 1164 | sense_valid = scsi_sense_valid(&sshdr); |
1da177e4 LT |
1165 | retries--; |
1166 | ||
1167 | } while (the_result && retries); | |
1168 | ||
1169 | if (the_result && !longrc) { | |
e73aec82 MP |
1170 | sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY failed\n"); |
1171 | sd_print_result(sdkp, the_result); | |
1da177e4 | 1172 | if (driver_byte(the_result) & DRIVER_SENSE) |
e73aec82 | 1173 | sd_print_sense_hdr(sdkp, &sshdr); |
1da177e4 | 1174 | else |
e73aec82 | 1175 | sd_printk(KERN_NOTICE, sdkp, "Sense not available.\n"); |
1da177e4 LT |
1176 | |
1177 | /* Set dirty bit for removable devices if not ready - | |
1178 | * sometimes drives will not report this properly. */ | |
1179 | if (sdp->removable && | |
1180 | sense_valid && sshdr.sense_key == NOT_READY) | |
1181 | sdp->changed = 1; | |
1182 | ||
1183 | /* Either no media are present but the drive didn't tell us, | |
1184 | or they are present but the read capacity command fails */ | |
1185 | /* sdkp->media_present = 0; -- not always correct */ | |
69bdd88c | 1186 | sdkp->capacity = 0; /* unknown mapped to zero - as usual */ |
1da177e4 LT |
1187 | |
1188 | return; | |
1189 | } else if (the_result && longrc) { | |
1190 | /* READ CAPACITY(16) has been failed */ | |
e73aec82 MP |
1191 | sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY(16) failed\n"); |
1192 | sd_print_result(sdkp, the_result); | |
1193 | sd_printk(KERN_NOTICE, sdkp, "Use 0xffffffff as device size\n"); | |
1194 | ||
1da177e4 LT |
1195 | sdkp->capacity = 1 + (sector_t) 0xffffffff; |
1196 | goto got_data; | |
1197 | } | |
1198 | ||
1199 | if (!longrc) { | |
1200 | sector_size = (buffer[4] << 24) | | |
1201 | (buffer[5] << 16) | (buffer[6] << 8) | buffer[7]; | |
1202 | if (buffer[0] == 0xff && buffer[1] == 0xff && | |
1203 | buffer[2] == 0xff && buffer[3] == 0xff) { | |
1204 | if(sizeof(sdkp->capacity) > 4) { | |
e73aec82 MP |
1205 | sd_printk(KERN_NOTICE, sdkp, "Very big device. " |
1206 | "Trying to use READ CAPACITY(16).\n"); | |
1da177e4 LT |
1207 | longrc = 1; |
1208 | goto repeat; | |
1209 | } | |
e73aec82 MP |
1210 | sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use " |
1211 | "a kernel compiled with support for large " | |
1212 | "block devices.\n"); | |
1da177e4 LT |
1213 | sdkp->capacity = 0; |
1214 | goto got_data; | |
1215 | } | |
1216 | sdkp->capacity = 1 + (((sector_t)buffer[0] << 24) | | |
1217 | (buffer[1] << 16) | | |
1218 | (buffer[2] << 8) | | |
1219 | buffer[3]); | |
1220 | } else { | |
1221 | sdkp->capacity = 1 + (((u64)buffer[0] << 56) | | |
1222 | ((u64)buffer[1] << 48) | | |
1223 | ((u64)buffer[2] << 40) | | |
1224 | ((u64)buffer[3] << 32) | | |
1225 | ((sector_t)buffer[4] << 24) | | |
1226 | ((sector_t)buffer[5] << 16) | | |
1227 | ((sector_t)buffer[6] << 8) | | |
1228 | (sector_t)buffer[7]); | |
1229 | ||
1230 | sector_size = (buffer[8] << 24) | | |
1231 | (buffer[9] << 16) | (buffer[10] << 8) | buffer[11]; | |
1232 | } | |
1233 | ||
1234 | /* Some devices return the total number of sectors, not the | |
1235 | * highest sector number. Make the necessary adjustment. */ | |
61bf54b7 | 1236 | if (sdp->fix_capacity) { |
1da177e4 LT |
1237 | --sdkp->capacity; |
1238 | ||
61bf54b7 ON |
1239 | /* Some devices have version which report the correct sizes |
1240 | * and others which do not. We guess size according to a heuristic | |
1241 | * and err on the side of lowering the capacity. */ | |
1242 | } else { | |
1243 | if (sdp->guess_capacity) | |
1244 | if (sdkp->capacity & 0x01) /* odd sizes are odd */ | |
1245 | --sdkp->capacity; | |
1246 | } | |
1247 | ||
1da177e4 LT |
1248 | got_data: |
1249 | if (sector_size == 0) { | |
1250 | sector_size = 512; | |
e73aec82 MP |
1251 | sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, " |
1252 | "assuming 512.\n"); | |
1da177e4 LT |
1253 | } |
1254 | ||
1255 | if (sector_size != 512 && | |
1256 | sector_size != 1024 && | |
1257 | sector_size != 2048 && | |
1258 | sector_size != 4096 && | |
1259 | sector_size != 256) { | |
e73aec82 MP |
1260 | sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n", |
1261 | sector_size); | |
1da177e4 LT |
1262 | /* |
1263 | * The user might want to re-format the drive with | |
1264 | * a supported sectorsize. Once this happens, it | |
1265 | * would be relatively trivial to set the thing up. | |
1266 | * For this reason, we leave the thing in the table. | |
1267 | */ | |
1268 | sdkp->capacity = 0; | |
1269 | /* | |
1270 | * set a bogus sector size so the normal read/write | |
1271 | * logic in the block layer will eventually refuse any | |
1272 | * request on this device without tripping over power | |
1273 | * of two sector size assumptions | |
1274 | */ | |
1275 | sector_size = 512; | |
1276 | } | |
1277 | { | |
1278 | /* | |
1279 | * The msdos fs needs to know the hardware sector size | |
1280 | * So I have created this table. See ll_rw_blk.c | |
1281 | * Jacques Gelinas (Jacques@solucorp.qc.ca) | |
1282 | */ | |
1283 | int hard_sector = sector_size; | |
7a691bd3 | 1284 | sector_t sz = (sdkp->capacity/2) * (hard_sector/256); |
1da177e4 | 1285 | request_queue_t *queue = sdp->request_queue; |
7a691bd3 | 1286 | sector_t mb = sz; |
1da177e4 LT |
1287 | |
1288 | blk_queue_hardsect_size(queue, hard_sector); | |
1289 | /* avoid 64-bit division on 32-bit platforms */ | |
7a691bd3 | 1290 | sector_div(sz, 625); |
1da177e4 LT |
1291 | mb -= sz - 974; |
1292 | sector_div(mb, 1950); | |
1293 | ||
e73aec82 MP |
1294 | sd_printk(KERN_NOTICE, sdkp, |
1295 | "%llu %d-byte hardware sectors (%llu MB)\n", | |
1296 | (unsigned long long)sdkp->capacity, | |
1297 | hard_sector, (unsigned long long)mb); | |
1da177e4 LT |
1298 | } |
1299 | ||
1300 | /* Rescale capacity to 512-byte units */ | |
1301 | if (sector_size == 4096) | |
1302 | sdkp->capacity <<= 3; | |
1303 | else if (sector_size == 2048) | |
1304 | sdkp->capacity <<= 2; | |
1305 | else if (sector_size == 1024) | |
1306 | sdkp->capacity <<= 1; | |
1307 | else if (sector_size == 256) | |
1308 | sdkp->capacity >>= 1; | |
1309 | ||
1310 | sdkp->device->sector_size = sector_size; | |
1311 | } | |
1312 | ||
1313 | /* called with buffer of length 512 */ | |
1314 | static inline int | |
ea73a9f2 JB |
1315 | sd_do_mode_sense(struct scsi_device *sdp, int dbd, int modepage, |
1316 | unsigned char *buffer, int len, struct scsi_mode_data *data, | |
1317 | struct scsi_sense_hdr *sshdr) | |
1da177e4 | 1318 | { |
ea73a9f2 | 1319 | return scsi_mode_sense(sdp, dbd, modepage, buffer, len, |
1cf72699 | 1320 | SD_TIMEOUT, SD_MAX_RETRIES, data, |
ea73a9f2 | 1321 | sshdr); |
1da177e4 LT |
1322 | } |
1323 | ||
1324 | /* | |
1325 | * read write protect setting, if possible - called only in sd_revalidate_disk() | |
48970800 | 1326 | * called with buffer of length SD_BUF_SIZE |
1da177e4 LT |
1327 | */ |
1328 | static void | |
e73aec82 | 1329 | sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer) |
ea73a9f2 | 1330 | { |
1da177e4 | 1331 | int res; |
ea73a9f2 | 1332 | struct scsi_device *sdp = sdkp->device; |
1da177e4 LT |
1333 | struct scsi_mode_data data; |
1334 | ||
1335 | set_disk_ro(sdkp->disk, 0); | |
ea73a9f2 | 1336 | if (sdp->skip_ms_page_3f) { |
e73aec82 | 1337 | sd_printk(KERN_NOTICE, sdkp, "Assuming Write Enabled\n"); |
1da177e4 LT |
1338 | return; |
1339 | } | |
1340 | ||
ea73a9f2 JB |
1341 | if (sdp->use_192_bytes_for_3f) { |
1342 | res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 192, &data, NULL); | |
1da177e4 LT |
1343 | } else { |
1344 | /* | |
1345 | * First attempt: ask for all pages (0x3F), but only 4 bytes. | |
1346 | * We have to start carefully: some devices hang if we ask | |
1347 | * for more than is available. | |
1348 | */ | |
ea73a9f2 | 1349 | res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 4, &data, NULL); |
1da177e4 LT |
1350 | |
1351 | /* | |
1352 | * Second attempt: ask for page 0 When only page 0 is | |
1353 | * implemented, a request for page 3F may return Sense Key | |
1354 | * 5: Illegal Request, Sense Code 24: Invalid field in | |
1355 | * CDB. | |
1356 | */ | |
1357 | if (!scsi_status_is_good(res)) | |
ea73a9f2 | 1358 | res = sd_do_mode_sense(sdp, 0, 0, buffer, 4, &data, NULL); |
1da177e4 LT |
1359 | |
1360 | /* | |
1361 | * Third attempt: ask 255 bytes, as we did earlier. | |
1362 | */ | |
1363 | if (!scsi_status_is_good(res)) | |
ea73a9f2 JB |
1364 | res = sd_do_mode_sense(sdp, 0, 0x3F, buffer, 255, |
1365 | &data, NULL); | |
1da177e4 LT |
1366 | } |
1367 | ||
1368 | if (!scsi_status_is_good(res)) { | |
e73aec82 MP |
1369 | sd_printk(KERN_WARNING, sdkp, |
1370 | "Test WP failed, assume Write Enabled\n"); | |
1da177e4 LT |
1371 | } else { |
1372 | sdkp->write_prot = ((data.device_specific & 0x80) != 0); | |
1373 | set_disk_ro(sdkp->disk, sdkp->write_prot); | |
e73aec82 MP |
1374 | sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n", |
1375 | sdkp->write_prot ? "on" : "off"); | |
1376 | sd_printk(KERN_DEBUG, sdkp, | |
1377 | "Mode Sense: %02x %02x %02x %02x\n", | |
1378 | buffer[0], buffer[1], buffer[2], buffer[3]); | |
1da177e4 LT |
1379 | } |
1380 | } | |
1381 | ||
1382 | /* | |
1383 | * sd_read_cache_type - called only from sd_revalidate_disk() | |
48970800 | 1384 | * called with buffer of length SD_BUF_SIZE |
1da177e4 LT |
1385 | */ |
1386 | static void | |
e73aec82 | 1387 | sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer) |
631e8a13 | 1388 | { |
1da177e4 | 1389 | int len = 0, res; |
ea73a9f2 | 1390 | struct scsi_device *sdp = sdkp->device; |
1da177e4 | 1391 | |
631e8a13 AV |
1392 | int dbd; |
1393 | int modepage; | |
1da177e4 LT |
1394 | struct scsi_mode_data data; |
1395 | struct scsi_sense_hdr sshdr; | |
1396 | ||
ea73a9f2 | 1397 | if (sdp->skip_ms_page_8) |
1da177e4 LT |
1398 | goto defaults; |
1399 | ||
ea73a9f2 | 1400 | if (sdp->type == TYPE_RBC) { |
631e8a13 AV |
1401 | modepage = 6; |
1402 | dbd = 8; | |
1403 | } else { | |
1404 | modepage = 8; | |
1405 | dbd = 0; | |
1406 | } | |
1407 | ||
1da177e4 | 1408 | /* cautiously ask */ |
ea73a9f2 | 1409 | res = sd_do_mode_sense(sdp, dbd, modepage, buffer, 4, &data, &sshdr); |
1da177e4 LT |
1410 | |
1411 | if (!scsi_status_is_good(res)) | |
1412 | goto bad_sense; | |
1413 | ||
6d73c851 AV |
1414 | if (!data.header_length) { |
1415 | modepage = 6; | |
e73aec82 | 1416 | sd_printk(KERN_ERR, sdkp, "Missing header in MODE_SENSE response\n"); |
6d73c851 AV |
1417 | } |
1418 | ||
1da177e4 LT |
1419 | /* that went OK, now ask for the proper length */ |
1420 | len = data.length; | |
1421 | ||
1422 | /* | |
1423 | * We're only interested in the first three bytes, actually. | |
1424 | * But the data cache page is defined for the first 20. | |
1425 | */ | |
1426 | if (len < 3) | |
1427 | goto bad_sense; | |
1428 | if (len > 20) | |
1429 | len = 20; | |
1430 | ||
1431 | /* Take headers and block descriptors into account */ | |
1432 | len += data.header_length + data.block_descriptor_length; | |
48970800 AV |
1433 | if (len > SD_BUF_SIZE) |
1434 | goto bad_sense; | |
1da177e4 LT |
1435 | |
1436 | /* Get the data */ | |
ea73a9f2 | 1437 | res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr); |
1da177e4 LT |
1438 | |
1439 | if (scsi_status_is_good(res)) { | |
631e8a13 | 1440 | int offset = data.header_length + data.block_descriptor_length; |
1da177e4 | 1441 | |
48970800 | 1442 | if (offset >= SD_BUF_SIZE - 2) { |
e73aec82 | 1443 | sd_printk(KERN_ERR, sdkp, "Malformed MODE SENSE response\n"); |
48970800 AV |
1444 | goto defaults; |
1445 | } | |
1446 | ||
631e8a13 | 1447 | if ((buffer[offset] & 0x3f) != modepage) { |
e73aec82 | 1448 | sd_printk(KERN_ERR, sdkp, "Got wrong page\n"); |
631e8a13 AV |
1449 | goto defaults; |
1450 | } | |
1451 | ||
1452 | if (modepage == 8) { | |
1453 | sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0); | |
1454 | sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0); | |
1455 | } else { | |
1456 | sdkp->WCE = ((buffer[offset + 2] & 0x01) == 0); | |
1457 | sdkp->RCD = 0; | |
1458 | } | |
1da177e4 | 1459 | |
007365ad TH |
1460 | sdkp->DPOFUA = (data.device_specific & 0x10) != 0; |
1461 | if (sdkp->DPOFUA && !sdkp->device->use_10_for_rw) { | |
e73aec82 MP |
1462 | sd_printk(KERN_NOTICE, sdkp, |
1463 | "Uses READ/WRITE(6), disabling FUA\n"); | |
007365ad TH |
1464 | sdkp->DPOFUA = 0; |
1465 | } | |
1466 | ||
e73aec82 MP |
1467 | sd_printk(KERN_NOTICE, sdkp, |
1468 | "Write cache: %s, read cache: %s, %s\n", | |
fd44bab5 LT |
1469 | sdkp->WCE ? "enabled" : "disabled", |
1470 | sdkp->RCD ? "disabled" : "enabled", | |
1471 | sdkp->DPOFUA ? "supports DPO and FUA" | |
1472 | : "doesn't support DPO or FUA"); | |
1da177e4 LT |
1473 | |
1474 | return; | |
1475 | } | |
1476 | ||
1477 | bad_sense: | |
ea73a9f2 | 1478 | if (scsi_sense_valid(&sshdr) && |
1da177e4 LT |
1479 | sshdr.sense_key == ILLEGAL_REQUEST && |
1480 | sshdr.asc == 0x24 && sshdr.ascq == 0x0) | |
e73aec82 MP |
1481 | /* Invalid field in CDB */ |
1482 | sd_printk(KERN_NOTICE, sdkp, "Cache data unavailable\n"); | |
1da177e4 | 1483 | else |
e73aec82 | 1484 | sd_printk(KERN_ERR, sdkp, "Asking for cache data failed\n"); |
1da177e4 LT |
1485 | |
1486 | defaults: | |
e73aec82 | 1487 | sd_printk(KERN_ERR, sdkp, "Assuming drive cache: write through\n"); |
1da177e4 LT |
1488 | sdkp->WCE = 0; |
1489 | sdkp->RCD = 0; | |
48970800 | 1490 | sdkp->DPOFUA = 0; |
1da177e4 LT |
1491 | } |
1492 | ||
1493 | /** | |
1494 | * sd_revalidate_disk - called the first time a new disk is seen, | |
1495 | * performs disk spin up, read_capacity, etc. | |
1496 | * @disk: struct gendisk we care about | |
1497 | **/ | |
1498 | static int sd_revalidate_disk(struct gendisk *disk) | |
1499 | { | |
1500 | struct scsi_disk *sdkp = scsi_disk(disk); | |
1501 | struct scsi_device *sdp = sdkp->device; | |
1da177e4 | 1502 | unsigned char *buffer; |
461d4e90 | 1503 | unsigned ordered; |
1da177e4 | 1504 | |
fa0d34be MP |
1505 | SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, |
1506 | "sd_revalidate_disk\n")); | |
1da177e4 LT |
1507 | |
1508 | /* | |
1509 | * If the device is offline, don't try and read capacity or any | |
1510 | * of the other niceties. | |
1511 | */ | |
1512 | if (!scsi_device_online(sdp)) | |
1513 | goto out; | |
1514 | ||
48970800 | 1515 | buffer = kmalloc(SD_BUF_SIZE, GFP_KERNEL | __GFP_DMA); |
1da177e4 | 1516 | if (!buffer) { |
e73aec82 MP |
1517 | sd_printk(KERN_WARNING, sdkp, "sd_revalidate_disk: Memory " |
1518 | "allocation failure.\n"); | |
ea73a9f2 | 1519 | goto out; |
1da177e4 LT |
1520 | } |
1521 | ||
1522 | /* defaults, until the device tells us otherwise */ | |
1523 | sdp->sector_size = 512; | |
1524 | sdkp->capacity = 0; | |
1525 | sdkp->media_present = 1; | |
1526 | sdkp->write_prot = 0; | |
1527 | sdkp->WCE = 0; | |
1528 | sdkp->RCD = 0; | |
1529 | ||
e73aec82 | 1530 | sd_spinup_disk(sdkp); |
1da177e4 LT |
1531 | |
1532 | /* | |
1533 | * Without media there is no reason to ask; moreover, some devices | |
1534 | * react badly if we do. | |
1535 | */ | |
1536 | if (sdkp->media_present) { | |
e73aec82 MP |
1537 | sd_read_capacity(sdkp, buffer); |
1538 | sd_read_write_protect_flag(sdkp, buffer); | |
1539 | sd_read_cache_type(sdkp, buffer); | |
1da177e4 | 1540 | } |
461d4e90 TH |
1541 | |
1542 | /* | |
1543 | * We now have all cache related info, determine how we deal | |
1544 | * with ordered requests. Note that as the current SCSI | |
1545 | * dispatch function can alter request order, we cannot use | |
1546 | * QUEUE_ORDERED_TAG_* even when ordered tag is supported. | |
1547 | */ | |
1548 | if (sdkp->WCE) | |
007365ad TH |
1549 | ordered = sdkp->DPOFUA |
1550 | ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH; | |
461d4e90 TH |
1551 | else |
1552 | ordered = QUEUE_ORDERED_DRAIN; | |
1553 | ||
1554 | blk_queue_ordered(sdkp->disk->queue, ordered, sd_prepare_flush); | |
1555 | ||
1da177e4 LT |
1556 | set_capacity(disk, sdkp->capacity); |
1557 | kfree(buffer); | |
1558 | ||
1da177e4 LT |
1559 | out: |
1560 | return 0; | |
1561 | } | |
1562 | ||
1563 | /** | |
1564 | * sd_probe - called during driver initialization and whenever a | |
1565 | * new scsi device is attached to the system. It is called once | |
1566 | * for each scsi device (not just disks) present. | |
1567 | * @dev: pointer to device object | |
1568 | * | |
1569 | * Returns 0 if successful (or not interested in this scsi device | |
1570 | * (e.g. scanner)); 1 when there is an error. | |
1571 | * | |
1572 | * Note: this function is invoked from the scsi mid-level. | |
1573 | * This function sets up the mapping between a given | |
1574 | * <host,channel,id,lun> (found in sdp) and new device name | |
1575 | * (e.g. /dev/sda). More precisely it is the block device major | |
1576 | * and minor number that is chosen here. | |
1577 | * | |
1578 | * Assume sd_attach is not re-entrant (for time being) | |
1579 | * Also think about sd_attach() and sd_remove() running coincidentally. | |
1580 | **/ | |
1581 | static int sd_probe(struct device *dev) | |
1582 | { | |
1583 | struct scsi_device *sdp = to_scsi_device(dev); | |
1584 | struct scsi_disk *sdkp; | |
1585 | struct gendisk *gd; | |
1586 | u32 index; | |
1587 | int error; | |
1588 | ||
1589 | error = -ENODEV; | |
631e8a13 | 1590 | if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC) |
1da177e4 LT |
1591 | goto out; |
1592 | ||
9ccfc756 JB |
1593 | SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp, |
1594 | "sd_attach\n")); | |
1da177e4 LT |
1595 | |
1596 | error = -ENOMEM; | |
24669f75 | 1597 | sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL); |
1da177e4 LT |
1598 | if (!sdkp) |
1599 | goto out; | |
1600 | ||
1da177e4 LT |
1601 | gd = alloc_disk(16); |
1602 | if (!gd) | |
1603 | goto out_free; | |
1604 | ||
1605 | if (!idr_pre_get(&sd_index_idr, GFP_KERNEL)) | |
1606 | goto out_put; | |
1607 | ||
1608 | spin_lock(&sd_index_lock); | |
1609 | error = idr_get_new(&sd_index_idr, NULL, &index); | |
1610 | spin_unlock(&sd_index_lock); | |
1611 | ||
1612 | if (index >= SD_MAX_DISKS) | |
1613 | error = -EBUSY; | |
1614 | if (error) | |
1615 | goto out_put; | |
1616 | ||
1617 | sdkp->device = sdp; | |
1618 | sdkp->driver = &sd_template; | |
1619 | sdkp->disk = gd; | |
1620 | sdkp->index = index; | |
1621 | sdkp->openers = 0; | |
1622 | ||
1623 | if (!sdp->timeout) { | |
631e8a13 | 1624 | if (sdp->type != TYPE_MOD) |
1da177e4 LT |
1625 | sdp->timeout = SD_TIMEOUT; |
1626 | else | |
1627 | sdp->timeout = SD_MOD_TIMEOUT; | |
1628 | } | |
1629 | ||
017f2e37 NST |
1630 | class_device_initialize(&sdkp->cdev); |
1631 | sdkp->cdev.dev = &sdp->sdev_gendev; | |
1632 | sdkp->cdev.class = &sd_disk_class; | |
1633 | strncpy(sdkp->cdev.class_id, sdp->sdev_gendev.bus_id, BUS_ID_SIZE); | |
1634 | ||
1635 | if (class_device_add(&sdkp->cdev)) | |
1636 | goto out_put; | |
1637 | ||
1638 | get_device(&sdp->sdev_gendev); | |
1639 | ||
1da177e4 LT |
1640 | gd->major = sd_major((index & 0xf0) >> 4); |
1641 | gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00); | |
1642 | gd->minors = 16; | |
1643 | gd->fops = &sd_fops; | |
1644 | ||
1645 | if (index < 26) { | |
1646 | sprintf(gd->disk_name, "sd%c", 'a' + index % 26); | |
1647 | } else if (index < (26 + 1) * 26) { | |
1648 | sprintf(gd->disk_name, "sd%c%c", | |
1649 | 'a' + index / 26 - 1,'a' + index % 26); | |
1650 | } else { | |
1651 | const unsigned int m1 = (index / 26 - 1) / 26 - 1; | |
1652 | const unsigned int m2 = (index / 26 - 1) % 26; | |
1653 | const unsigned int m3 = index % 26; | |
1654 | sprintf(gd->disk_name, "sd%c%c%c", | |
1655 | 'a' + m1, 'a' + m2, 'a' + m3); | |
1656 | } | |
1657 | ||
1da177e4 | 1658 | gd->private_data = &sdkp->driver; |
461d4e90 | 1659 | gd->queue = sdkp->device->request_queue; |
1da177e4 LT |
1660 | |
1661 | sd_revalidate_disk(gd); | |
1662 | ||
1663 | gd->driverfs_dev = &sdp->sdev_gendev; | |
1664 | gd->flags = GENHD_FL_DRIVERFS; | |
1665 | if (sdp->removable) | |
1666 | gd->flags |= GENHD_FL_REMOVABLE; | |
1da177e4 LT |
1667 | |
1668 | dev_set_drvdata(dev, sdkp); | |
1669 | add_disk(gd); | |
1670 | ||
e73aec82 MP |
1671 | sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n", |
1672 | sdp->removable ? "removable " : ""); | |
1da177e4 LT |
1673 | |
1674 | return 0; | |
1675 | ||
6bdaa1f1 | 1676 | out_put: |
1da177e4 | 1677 | put_disk(gd); |
6bdaa1f1 | 1678 | out_free: |
1da177e4 | 1679 | kfree(sdkp); |
6bdaa1f1 | 1680 | out: |
1da177e4 LT |
1681 | return error; |
1682 | } | |
1683 | ||
1684 | /** | |
1685 | * sd_remove - called whenever a scsi disk (previously recognized by | |
1686 | * sd_probe) is detached from the system. It is called (potentially | |
1687 | * multiple times) during sd module unload. | |
1688 | * @sdp: pointer to mid level scsi device object | |
1689 | * | |
1690 | * Note: this function is invoked from the scsi mid-level. | |
1691 | * This function potentially frees up a device name (e.g. /dev/sdc) | |
1692 | * that could be re-used by a subsequent sd_probe(). | |
1693 | * This function is not called when the built-in sd driver is "exit-ed". | |
1694 | **/ | |
1695 | static int sd_remove(struct device *dev) | |
1696 | { | |
1697 | struct scsi_disk *sdkp = dev_get_drvdata(dev); | |
1698 | ||
6bdaa1f1 | 1699 | class_device_del(&sdkp->cdev); |
1da177e4 LT |
1700 | del_gendisk(sdkp->disk); |
1701 | sd_shutdown(dev); | |
39b7f1e2 | 1702 | |
0b950672 | 1703 | mutex_lock(&sd_ref_mutex); |
39b7f1e2 | 1704 | dev_set_drvdata(dev, NULL); |
6bdaa1f1 | 1705 | class_device_put(&sdkp->cdev); |
0b950672 | 1706 | mutex_unlock(&sd_ref_mutex); |
1da177e4 LT |
1707 | |
1708 | return 0; | |
1709 | } | |
1710 | ||
1711 | /** | |
1712 | * scsi_disk_release - Called to free the scsi_disk structure | |
6bdaa1f1 | 1713 | * @cdev: pointer to embedded class device |
1da177e4 | 1714 | * |
0b950672 | 1715 | * sd_ref_mutex must be held entering this routine. Because it is |
1da177e4 LT |
1716 | * called on last put, you should always use the scsi_disk_get() |
1717 | * scsi_disk_put() helpers which manipulate the semaphore directly | |
6bdaa1f1 | 1718 | * and never do a direct class_device_put(). |
1da177e4 | 1719 | **/ |
6bdaa1f1 | 1720 | static void scsi_disk_release(struct class_device *cdev) |
1da177e4 | 1721 | { |
6bdaa1f1 | 1722 | struct scsi_disk *sdkp = to_scsi_disk(cdev); |
1da177e4 LT |
1723 | struct gendisk *disk = sdkp->disk; |
1724 | ||
1725 | spin_lock(&sd_index_lock); | |
1726 | idr_remove(&sd_index_idr, sdkp->index); | |
1727 | spin_unlock(&sd_index_lock); | |
1728 | ||
1729 | disk->private_data = NULL; | |
1da177e4 | 1730 | put_disk(disk); |
39b7f1e2 | 1731 | put_device(&sdkp->device->sdev_gendev); |
1da177e4 LT |
1732 | |
1733 | kfree(sdkp); | |
1734 | } | |
1735 | ||
c3c94c5a TH |
1736 | static int sd_start_stop_device(struct scsi_device *sdp, int start) |
1737 | { | |
1738 | unsigned char cmd[6] = { START_STOP }; /* START_VALID */ | |
1739 | struct scsi_sense_hdr sshdr; | |
1740 | int res; | |
1741 | ||
1742 | if (start) | |
1743 | cmd[4] |= 1; /* START */ | |
1744 | ||
1745 | if (!scsi_device_online(sdp)) | |
1746 | return -ENODEV; | |
1747 | ||
1748 | res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr, | |
1749 | SD_TIMEOUT, SD_MAX_RETRIES); | |
1750 | if (res) { | |
1751 | printk(KERN_WARNING "FAILED\n status = %x, message = %02x, " | |
1752 | "host = %d, driver = %02x\n ", | |
1753 | status_byte(res), msg_byte(res), | |
1754 | host_byte(res), driver_byte(res)); | |
1755 | if (driver_byte(res) & DRIVER_SENSE) | |
1756 | scsi_print_sense_hdr("sd", &sshdr); | |
1757 | } | |
1758 | ||
1759 | return res; | |
1760 | } | |
1761 | ||
1da177e4 LT |
1762 | /* |
1763 | * Send a SYNCHRONIZE CACHE instruction down to the device through | |
1764 | * the normal SCSI command structure. Wait for the command to | |
1765 | * complete. | |
1766 | */ | |
1767 | static void sd_shutdown(struct device *dev) | |
1768 | { | |
c3c94c5a | 1769 | struct scsi_device *sdp = to_scsi_device(dev); |
39b7f1e2 | 1770 | struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev); |
1da177e4 LT |
1771 | |
1772 | if (!sdkp) | |
1773 | return; /* this can happen */ | |
1774 | ||
39b7f1e2 | 1775 | if (sdkp->WCE) { |
e73aec82 MP |
1776 | sd_printk(KERN_NOTICE, sdkp, "Synchronizing SCSI cache\n"); |
1777 | sd_sync_cache(sdkp); | |
39b7f1e2 | 1778 | } |
c3c94c5a TH |
1779 | |
1780 | if (system_state != SYSTEM_RESTART && sdp->manage_start_stop) { | |
1781 | printk(KERN_NOTICE "Stopping disk %s: \n", | |
1782 | sdkp->disk->disk_name); | |
1783 | sd_start_stop_device(sdp, 0); | |
1784 | } | |
1785 | ||
39b7f1e2 AS |
1786 | scsi_disk_put(sdkp); |
1787 | } | |
1da177e4 | 1788 | |
c3c94c5a TH |
1789 | static int sd_suspend(struct device *dev, pm_message_t mesg) |
1790 | { | |
1791 | struct scsi_device *sdp = to_scsi_device(dev); | |
1792 | struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev); | |
1793 | int ret; | |
1794 | ||
1795 | if (!sdkp) | |
1796 | return 0; /* this can happen */ | |
1797 | ||
1798 | if (sdkp->WCE) { | |
1799 | printk(KERN_NOTICE "Synchronizing SCSI cache for disk %s: \n", | |
1800 | sdkp->disk->disk_name); | |
1801 | ret = sd_sync_cache(sdkp); | |
1802 | if (ret) | |
1803 | return ret; | |
1804 | } | |
1805 | ||
1806 | if (mesg.event == PM_EVENT_SUSPEND && sdp->manage_start_stop) { | |
1807 | printk(KERN_NOTICE "Stopping disk %s: \n", | |
1808 | sdkp->disk->disk_name); | |
1809 | ret = sd_start_stop_device(sdp, 0); | |
1810 | if (ret) | |
1811 | return ret; | |
1812 | } | |
1813 | ||
1814 | return 0; | |
1815 | } | |
1816 | ||
1817 | static int sd_resume(struct device *dev) | |
1818 | { | |
1819 | struct scsi_device *sdp = to_scsi_device(dev); | |
1820 | struct scsi_disk *sdkp = scsi_disk_get_from_dev(dev); | |
1821 | ||
1822 | if (!sdp->manage_start_stop) | |
1823 | return 0; | |
1824 | ||
1825 | printk(KERN_NOTICE "Starting disk %s: \n", sdkp->disk->disk_name); | |
1826 | ||
1827 | return sd_start_stop_device(sdp, 1); | |
1828 | } | |
1829 | ||
1da177e4 LT |
1830 | /** |
1831 | * init_sd - entry point for this driver (both when built in or when | |
1832 | * a module). | |
1833 | * | |
1834 | * Note: this function registers this driver with the scsi mid-level. | |
1835 | **/ | |
1836 | static int __init init_sd(void) | |
1837 | { | |
5e4009ba | 1838 | int majors = 0, i, err; |
1da177e4 LT |
1839 | |
1840 | SCSI_LOG_HLQUEUE(3, printk("init_sd: sd driver entry point\n")); | |
1841 | ||
1842 | for (i = 0; i < SD_MAJORS; i++) | |
1843 | if (register_blkdev(sd_major(i), "sd") == 0) | |
1844 | majors++; | |
1845 | ||
1846 | if (!majors) | |
1847 | return -ENODEV; | |
1848 | ||
5e4009ba JG |
1849 | err = class_register(&sd_disk_class); |
1850 | if (err) | |
1851 | goto err_out; | |
6bdaa1f1 | 1852 | |
5e4009ba JG |
1853 | err = scsi_register_driver(&sd_template.gendrv); |
1854 | if (err) | |
1855 | goto err_out_class; | |
1856 | ||
1857 | return 0; | |
1858 | ||
1859 | err_out_class: | |
1860 | class_unregister(&sd_disk_class); | |
1861 | err_out: | |
1862 | for (i = 0; i < SD_MAJORS; i++) | |
1863 | unregister_blkdev(sd_major(i), "sd"); | |
1864 | return err; | |
1da177e4 LT |
1865 | } |
1866 | ||
1867 | /** | |
1868 | * exit_sd - exit point for this driver (when it is a module). | |
1869 | * | |
1870 | * Note: this function unregisters this driver from the scsi mid-level. | |
1871 | **/ | |
1872 | static void __exit exit_sd(void) | |
1873 | { | |
1874 | int i; | |
1875 | ||
1876 | SCSI_LOG_HLQUEUE(3, printk("exit_sd: exiting sd driver\n")); | |
1877 | ||
1878 | scsi_unregister_driver(&sd_template.gendrv); | |
5e4009ba JG |
1879 | class_unregister(&sd_disk_class); |
1880 | ||
1da177e4 LT |
1881 | for (i = 0; i < SD_MAJORS; i++) |
1882 | unregister_blkdev(sd_major(i), "sd"); | |
1883 | } | |
1884 | ||
1da177e4 LT |
1885 | module_init(init_sd); |
1886 | module_exit(exit_sd); | |
e73aec82 MP |
1887 | |
1888 | static void sd_print_sense_hdr(struct scsi_disk *sdkp, | |
1889 | struct scsi_sense_hdr *sshdr) | |
1890 | { | |
1891 | sd_printk(KERN_INFO, sdkp, ""); | |
1892 | scsi_show_sense_hdr(sshdr); | |
1893 | sd_printk(KERN_INFO, sdkp, ""); | |
1894 | scsi_show_extd_sense(sshdr->asc, sshdr->ascq); | |
1895 | } | |
1896 | ||
1897 | static void sd_print_result(struct scsi_disk *sdkp, int result) | |
1898 | { | |
1899 | sd_printk(KERN_INFO, sdkp, ""); | |
1900 | scsi_show_result(result); | |
1901 | } | |
1902 |