xref: /openbmc/linux/drivers/scsi/sr_ioctl.c (revision 5d0e4d78)
1 #include <linux/kernel.h>
2 #include <linux/mm.h>
3 #include <linux/fs.h>
4 #include <linux/errno.h>
5 #include <linux/string.h>
6 #include <linux/blkdev.h>
7 #include <linux/module.h>
8 #include <linux/blkpg.h>
9 #include <linux/cdrom.h>
10 #include <linux/delay.h>
11 #include <linux/slab.h>
12 #include <asm/io.h>
13 #include <linux/uaccess.h>
14 
15 #include <scsi/scsi.h>
16 #include <scsi/scsi_dbg.h>
17 #include <scsi/scsi_device.h>
18 #include <scsi/scsi_eh.h>
19 #include <scsi/scsi_host.h>
20 #include <scsi/scsi_ioctl.h>
21 #include <scsi/scsi_cmnd.h>
22 
23 #include "sr.h"
24 
25 #if 0
26 #define DEBUG
27 #endif
28 
29 /* The sr_is_xa() seems to trigger firmware bugs with some drives :-(
30  * It is off by default and can be turned on with this module parameter */
31 static int xa_test = 0;
32 
33 module_param(xa_test, int, S_IRUGO | S_IWUSR);
34 
35 /* primitive to determine whether we need to have GFP_DMA set based on
36  * the status of the unchecked_isa_dma flag in the host structure */
37 #define SR_GFP_DMA(cd) (((cd)->device->host->unchecked_isa_dma) ? GFP_DMA : 0)
38 
39 static int sr_read_tochdr(struct cdrom_device_info *cdi,
40 		struct cdrom_tochdr *tochdr)
41 {
42 	struct scsi_cd *cd = cdi->handle;
43 	struct packet_command cgc;
44 	int result;
45 	unsigned char *buffer;
46 
47 	buffer = kmalloc(32, GFP_KERNEL | SR_GFP_DMA(cd));
48 	if (!buffer)
49 		return -ENOMEM;
50 
51 	memset(&cgc, 0, sizeof(struct packet_command));
52 	cgc.timeout = IOCTL_TIMEOUT;
53 	cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
54 	cgc.cmd[8] = 12;		/* LSB of length */
55 	cgc.buffer = buffer;
56 	cgc.buflen = 12;
57 	cgc.quiet = 1;
58 	cgc.data_direction = DMA_FROM_DEVICE;
59 
60 	result = sr_do_ioctl(cd, &cgc);
61 
62 	tochdr->cdth_trk0 = buffer[2];
63 	tochdr->cdth_trk1 = buffer[3];
64 
65 	kfree(buffer);
66 	return result;
67 }
68 
69 static int sr_read_tocentry(struct cdrom_device_info *cdi,
70 		struct cdrom_tocentry *tocentry)
71 {
72 	struct scsi_cd *cd = cdi->handle;
73 	struct packet_command cgc;
74 	int result;
75 	unsigned char *buffer;
76 
77 	buffer = kmalloc(32, GFP_KERNEL | SR_GFP_DMA(cd));
78 	if (!buffer)
79 		return -ENOMEM;
80 
81 	memset(&cgc, 0, sizeof(struct packet_command));
82 	cgc.timeout = IOCTL_TIMEOUT;
83 	cgc.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
84 	cgc.cmd[1] |= (tocentry->cdte_format == CDROM_MSF) ? 0x02 : 0;
85 	cgc.cmd[6] = tocentry->cdte_track;
86 	cgc.cmd[8] = 12;		/* LSB of length */
87 	cgc.buffer = buffer;
88 	cgc.buflen = 12;
89 	cgc.data_direction = DMA_FROM_DEVICE;
90 
91 	result = sr_do_ioctl(cd, &cgc);
92 
93 	tocentry->cdte_ctrl = buffer[5] & 0xf;
94 	tocentry->cdte_adr = buffer[5] >> 4;
95 	tocentry->cdte_datamode = (tocentry->cdte_ctrl & 0x04) ? 1 : 0;
96 	if (tocentry->cdte_format == CDROM_MSF) {
97 		tocentry->cdte_addr.msf.minute = buffer[9];
98 		tocentry->cdte_addr.msf.second = buffer[10];
99 		tocentry->cdte_addr.msf.frame = buffer[11];
100 	} else
101 		tocentry->cdte_addr.lba = (((((buffer[8] << 8) + buffer[9]) << 8)
102 			+ buffer[10]) << 8) + buffer[11];
103 
104 	kfree(buffer);
105 	return result;
106 }
107 
108 #define IOCTL_RETRIES 3
109 
110 /* ATAPI drives don't have a SCMD_PLAYAUDIO_TI command.  When these drives
111    are emulating a SCSI device via the idescsi module, they need to have
112    CDROMPLAYTRKIND commands translated into CDROMPLAYMSF commands for them */
113 
114 static int sr_fake_playtrkind(struct cdrom_device_info *cdi, struct cdrom_ti *ti)
115 {
116 	struct cdrom_tocentry trk0_te, trk1_te;
117 	struct cdrom_tochdr tochdr;
118 	struct packet_command cgc;
119 	int ntracks, ret;
120 
121 	ret = sr_read_tochdr(cdi, &tochdr);
122 	if (ret)
123 		return ret;
124 
125 	ntracks = tochdr.cdth_trk1 - tochdr.cdth_trk0 + 1;
126 
127 	if (ti->cdti_trk1 == ntracks)
128 		ti->cdti_trk1 = CDROM_LEADOUT;
129 	else if (ti->cdti_trk1 != CDROM_LEADOUT)
130 		ti->cdti_trk1 ++;
131 
132 	trk0_te.cdte_track = ti->cdti_trk0;
133 	trk0_te.cdte_format = CDROM_MSF;
134 	trk1_te.cdte_track = ti->cdti_trk1;
135 	trk1_te.cdte_format = CDROM_MSF;
136 
137 	ret = sr_read_tocentry(cdi, &trk0_te);
138 	if (ret)
139 		return ret;
140 	ret = sr_read_tocentry(cdi, &trk1_te);
141 	if (ret)
142 		return ret;
143 
144 	memset(&cgc, 0, sizeof(struct packet_command));
145 	cgc.cmd[0] = GPCMD_PLAY_AUDIO_MSF;
146 	cgc.cmd[3] = trk0_te.cdte_addr.msf.minute;
147 	cgc.cmd[4] = trk0_te.cdte_addr.msf.second;
148 	cgc.cmd[5] = trk0_te.cdte_addr.msf.frame;
149 	cgc.cmd[6] = trk1_te.cdte_addr.msf.minute;
150 	cgc.cmd[7] = trk1_te.cdte_addr.msf.second;
151 	cgc.cmd[8] = trk1_te.cdte_addr.msf.frame;
152 	cgc.data_direction = DMA_NONE;
153 	cgc.timeout = IOCTL_TIMEOUT;
154 	return sr_do_ioctl(cdi->handle, &cgc);
155 }
156 
157 static int sr_play_trkind(struct cdrom_device_info *cdi,
158 		struct cdrom_ti *ti)
159 
160 {
161 	struct scsi_cd *cd = cdi->handle;
162 	struct packet_command cgc;
163 	int result;
164 
165 	memset(&cgc, 0, sizeof(struct packet_command));
166 	cgc.timeout = IOCTL_TIMEOUT;
167 	cgc.cmd[0] = GPCMD_PLAYAUDIO_TI;
168 	cgc.cmd[4] = ti->cdti_trk0;
169 	cgc.cmd[5] = ti->cdti_ind0;
170 	cgc.cmd[7] = ti->cdti_trk1;
171 	cgc.cmd[8] = ti->cdti_ind1;
172 	cgc.data_direction = DMA_NONE;
173 
174 	result = sr_do_ioctl(cd, &cgc);
175 	if (result == -EDRIVE_CANT_DO_THIS)
176 		result = sr_fake_playtrkind(cdi, ti);
177 
178 	return result;
179 }
180 
181 /* We do our own retries because we want to know what the specific
182    error code is.  Normally the UNIT_ATTENTION code will automatically
183    clear after one error */
184 
185 int sr_do_ioctl(Scsi_CD *cd, struct packet_command *cgc)
186 {
187 	struct scsi_device *SDev;
188 	struct scsi_sense_hdr sshdr;
189 	int result, err = 0, retries = 0;
190 
191 	SDev = cd->device;
192 
193       retry:
194 	if (!scsi_block_when_processing_errors(SDev)) {
195 		err = -ENODEV;
196 		goto out;
197 	}
198 
199 	result = scsi_execute(SDev, cgc->cmd, cgc->data_direction,
200 			      cgc->buffer, cgc->buflen,
201 			      (unsigned char *)cgc->sense, &sshdr,
202 			      cgc->timeout, IOCTL_RETRIES, 0, 0, NULL);
203 
204 	/* Minimal error checking.  Ignore cases we know about, and report the rest. */
205 	if (driver_byte(result) != 0) {
206 		switch (sshdr.sense_key) {
207 		case UNIT_ATTENTION:
208 			SDev->changed = 1;
209 			if (!cgc->quiet)
210 				sr_printk(KERN_INFO, cd,
211 					  "disc change detected.\n");
212 			if (retries++ < 10)
213 				goto retry;
214 			err = -ENOMEDIUM;
215 			break;
216 		case NOT_READY:	/* This happens if there is no disc in drive */
217 			if (sshdr.asc == 0x04 &&
218 			    sshdr.ascq == 0x01) {
219 				/* sense: Logical unit is in process of becoming ready */
220 				if (!cgc->quiet)
221 					sr_printk(KERN_INFO, cd,
222 						  "CDROM not ready yet.\n");
223 				if (retries++ < 10) {
224 					/* sleep 2 sec and try again */
225 					ssleep(2);
226 					goto retry;
227 				} else {
228 					/* 20 secs are enough? */
229 					err = -ENOMEDIUM;
230 					break;
231 				}
232 			}
233 			if (!cgc->quiet)
234 				sr_printk(KERN_INFO, cd,
235 					  "CDROM not ready.  Make sure there "
236 					  "is a disc in the drive.\n");
237 			err = -ENOMEDIUM;
238 			break;
239 		case ILLEGAL_REQUEST:
240 			err = -EIO;
241 			if (sshdr.asc == 0x20 &&
242 			    sshdr.ascq == 0x00)
243 				/* sense: Invalid command operation code */
244 				err = -EDRIVE_CANT_DO_THIS;
245 			break;
246 		default:
247 			err = -EIO;
248 		}
249 	}
250 
251 	/* Wake up a process waiting for device */
252       out:
253 	cgc->stat = err;
254 	return err;
255 }
256 
257 /* ---------------------------------------------------------------------- */
258 /* interface to cdrom.c                                                   */
259 
260 int sr_tray_move(struct cdrom_device_info *cdi, int pos)
261 {
262 	Scsi_CD *cd = cdi->handle;
263 	struct packet_command cgc;
264 
265 	memset(&cgc, 0, sizeof(struct packet_command));
266 	cgc.cmd[0] = GPCMD_START_STOP_UNIT;
267 	cgc.cmd[4] = (pos == 0) ? 0x03 /* close */ : 0x02 /* eject */ ;
268 	cgc.data_direction = DMA_NONE;
269 	cgc.timeout = IOCTL_TIMEOUT;
270 	return sr_do_ioctl(cd, &cgc);
271 }
272 
273 int sr_lock_door(struct cdrom_device_info *cdi, int lock)
274 {
275 	Scsi_CD *cd = cdi->handle;
276 
277 	return scsi_set_medium_removal(cd->device, lock ?
278 		       SCSI_REMOVAL_PREVENT : SCSI_REMOVAL_ALLOW);
279 }
280 
281 int sr_drive_status(struct cdrom_device_info *cdi, int slot)
282 {
283 	struct scsi_cd *cd = cdi->handle;
284 	struct scsi_sense_hdr sshdr;
285 	struct media_event_desc med;
286 
287 	if (CDSL_CURRENT != slot) {
288 		/* we have no changer support */
289 		return -EINVAL;
290 	}
291 	if (!scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr))
292 		return CDS_DISC_OK;
293 
294 	/* SK/ASC/ASCQ of 2/4/1 means "unit is becoming ready" */
295 	if (scsi_sense_valid(&sshdr) && sshdr.sense_key == NOT_READY
296 			&& sshdr.asc == 0x04 && sshdr.ascq == 0x01)
297 		return CDS_DRIVE_NOT_READY;
298 
299 	if (!cdrom_get_media_event(cdi, &med)) {
300 		if (med.media_present)
301 			return CDS_DISC_OK;
302 		else if (med.door_open)
303 			return CDS_TRAY_OPEN;
304 		else
305 			return CDS_NO_DISC;
306 	}
307 
308 	/*
309 	 * SK/ASC/ASCQ of 2/4/2 means "initialization required"
310 	 * Using CD_TRAY_OPEN results in an START_STOP_UNIT to close
311 	 * the tray, which resolves the initialization requirement.
312 	 */
313 	if (scsi_sense_valid(&sshdr) && sshdr.sense_key == NOT_READY
314 			&& sshdr.asc == 0x04 && sshdr.ascq == 0x02)
315 		return CDS_TRAY_OPEN;
316 
317 	/*
318 	 * 0x04 is format in progress .. but there must be a disc present!
319 	 */
320 	if (sshdr.sense_key == NOT_READY && sshdr.asc == 0x04)
321 		return CDS_DISC_OK;
322 
323 	/*
324 	 * If not using Mt Fuji extended media tray reports,
325 	 * just return TRAY_OPEN since ATAPI doesn't provide
326 	 * any other way to detect this...
327 	 */
328 	if (scsi_sense_valid(&sshdr) &&
329 	    /* 0x3a is medium not present */
330 	    sshdr.asc == 0x3a)
331 		return CDS_NO_DISC;
332 	else
333 		return CDS_TRAY_OPEN;
334 
335 	return CDS_DRIVE_NOT_READY;
336 }
337 
338 int sr_disk_status(struct cdrom_device_info *cdi)
339 {
340 	Scsi_CD *cd = cdi->handle;
341 	struct cdrom_tochdr toc_h;
342 	struct cdrom_tocentry toc_e;
343 	int i, rc, have_datatracks = 0;
344 
345 	/* look for data tracks */
346 	rc = sr_read_tochdr(cdi, &toc_h);
347 	if (rc)
348 		return (rc == -ENOMEDIUM) ? CDS_NO_DISC : CDS_NO_INFO;
349 
350 	for (i = toc_h.cdth_trk0; i <= toc_h.cdth_trk1; i++) {
351 		toc_e.cdte_track = i;
352 		toc_e.cdte_format = CDROM_LBA;
353 		if (sr_read_tocentry(cdi, &toc_e))
354 			return CDS_NO_INFO;
355 		if (toc_e.cdte_ctrl & CDROM_DATA_TRACK) {
356 			have_datatracks = 1;
357 			break;
358 		}
359 	}
360 	if (!have_datatracks)
361 		return CDS_AUDIO;
362 
363 	if (cd->xa_flag)
364 		return CDS_XA_2_1;
365 	else
366 		return CDS_DATA_1;
367 }
368 
369 int sr_get_last_session(struct cdrom_device_info *cdi,
370 			struct cdrom_multisession *ms_info)
371 {
372 	Scsi_CD *cd = cdi->handle;
373 
374 	ms_info->addr.lba = cd->ms_offset;
375 	ms_info->xa_flag = cd->xa_flag || cd->ms_offset > 0;
376 
377 	return 0;
378 }
379 
380 int sr_get_mcn(struct cdrom_device_info *cdi, struct cdrom_mcn *mcn)
381 {
382 	Scsi_CD *cd = cdi->handle;
383 	struct packet_command cgc;
384 	char *buffer = kmalloc(32, GFP_KERNEL | SR_GFP_DMA(cd));
385 	int result;
386 
387 	if (!buffer)
388 		return -ENOMEM;
389 
390 	memset(&cgc, 0, sizeof(struct packet_command));
391 	cgc.cmd[0] = GPCMD_READ_SUBCHANNEL;
392 	cgc.cmd[2] = 0x40;	/* I do want the subchannel info */
393 	cgc.cmd[3] = 0x02;	/* Give me medium catalog number info */
394 	cgc.cmd[8] = 24;
395 	cgc.buffer = buffer;
396 	cgc.buflen = 24;
397 	cgc.data_direction = DMA_FROM_DEVICE;
398 	cgc.timeout = IOCTL_TIMEOUT;
399 	result = sr_do_ioctl(cd, &cgc);
400 
401 	memcpy(mcn->medium_catalog_number, buffer + 9, 13);
402 	mcn->medium_catalog_number[13] = 0;
403 
404 	kfree(buffer);
405 	return result;
406 }
407 
408 int sr_reset(struct cdrom_device_info *cdi)
409 {
410 	return 0;
411 }
412 
413 int sr_select_speed(struct cdrom_device_info *cdi, int speed)
414 {
415 	Scsi_CD *cd = cdi->handle;
416 	struct packet_command cgc;
417 
418 	if (speed == 0)
419 		speed = 0xffff;	/* set to max */
420 	else
421 		speed *= 177;	/* Nx to kbyte/s */
422 
423 	memset(&cgc, 0, sizeof(struct packet_command));
424 	cgc.cmd[0] = GPCMD_SET_SPEED;	/* SET CD SPEED */
425 	cgc.cmd[2] = (speed >> 8) & 0xff;	/* MSB for speed (in kbytes/sec) */
426 	cgc.cmd[3] = speed & 0xff;	/* LSB */
427 	cgc.data_direction = DMA_NONE;
428 	cgc.timeout = IOCTL_TIMEOUT;
429 
430 	if (sr_do_ioctl(cd, &cgc))
431 		return -EIO;
432 	return 0;
433 }
434 
435 /* ----------------------------------------------------------------------- */
436 /* this is called by the generic cdrom driver. arg is a _kernel_ pointer,  */
437 /* because the generic cdrom driver does the user access stuff for us.     */
438 /* only cdromreadtochdr and cdromreadtocentry are left - for use with the  */
439 /* sr_disk_status interface for the generic cdrom driver.                  */
440 
441 int sr_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd, void *arg)
442 {
443 	switch (cmd) {
444 	case CDROMREADTOCHDR:
445 		return sr_read_tochdr(cdi, arg);
446 	case CDROMREADTOCENTRY:
447 		return sr_read_tocentry(cdi, arg);
448 	case CDROMPLAYTRKIND:
449 		return sr_play_trkind(cdi, arg);
450 	default:
451 		return -EINVAL;
452 	}
453 }
454 
455 /* -----------------------------------------------------------------------
456  * a function to read all sorts of funny cdrom sectors using the READ_CD
457  * scsi-3 mmc command
458  *
459  * lba:     linear block address
460  * format:  0 = data (anything)
461  *          1 = audio
462  *          2 = data (mode 1)
463  *          3 = data (mode 2)
464  *          4 = data (mode 2 form1)
465  *          5 = data (mode 2 form2)
466  * blksize: 2048 | 2336 | 2340 | 2352
467  */
468 
469 static int sr_read_cd(Scsi_CD *cd, unsigned char *dest, int lba, int format, int blksize)
470 {
471 	struct packet_command cgc;
472 
473 #ifdef DEBUG
474 	sr_printk(KERN_INFO, cd, "sr_read_cd lba=%d format=%d blksize=%d\n",
475 		  lba, format, blksize);
476 #endif
477 
478 	memset(&cgc, 0, sizeof(struct packet_command));
479 	cgc.cmd[0] = GPCMD_READ_CD;	/* READ_CD */
480 	cgc.cmd[1] = ((format & 7) << 2);
481 	cgc.cmd[2] = (unsigned char) (lba >> 24) & 0xff;
482 	cgc.cmd[3] = (unsigned char) (lba >> 16) & 0xff;
483 	cgc.cmd[4] = (unsigned char) (lba >> 8) & 0xff;
484 	cgc.cmd[5] = (unsigned char) lba & 0xff;
485 	cgc.cmd[8] = 1;
486 	switch (blksize) {
487 	case 2336:
488 		cgc.cmd[9] = 0x58;
489 		break;
490 	case 2340:
491 		cgc.cmd[9] = 0x78;
492 		break;
493 	case 2352:
494 		cgc.cmd[9] = 0xf8;
495 		break;
496 	default:
497 		cgc.cmd[9] = 0x10;
498 		break;
499 	}
500 	cgc.buffer = dest;
501 	cgc.buflen = blksize;
502 	cgc.data_direction = DMA_FROM_DEVICE;
503 	cgc.timeout = IOCTL_TIMEOUT;
504 	return sr_do_ioctl(cd, &cgc);
505 }
506 
507 /*
508  * read sectors with blocksizes other than 2048
509  */
510 
511 static int sr_read_sector(Scsi_CD *cd, int lba, int blksize, unsigned char *dest)
512 {
513 	struct packet_command cgc;
514 	int rc;
515 
516 	/* we try the READ CD command first... */
517 	if (cd->readcd_known) {
518 		rc = sr_read_cd(cd, dest, lba, 0, blksize);
519 		if (-EDRIVE_CANT_DO_THIS != rc)
520 			return rc;
521 		cd->readcd_known = 0;
522 		sr_printk(KERN_INFO, cd,
523 			  "CDROM does'nt support READ CD (0xbe) command\n");
524 		/* fall & retry the other way */
525 	}
526 	/* ... if this fails, we switch the blocksize using MODE SELECT */
527 	if (blksize != cd->device->sector_size) {
528 		if (0 != (rc = sr_set_blocklength(cd, blksize)))
529 			return rc;
530 	}
531 #ifdef DEBUG
532 	sr_printk(KERN_INFO, cd, "sr_read_sector lba=%d blksize=%d\n",
533 		  lba, blksize);
534 #endif
535 
536 	memset(&cgc, 0, sizeof(struct packet_command));
537 	cgc.cmd[0] = GPCMD_READ_10;
538 	cgc.cmd[2] = (unsigned char) (lba >> 24) & 0xff;
539 	cgc.cmd[3] = (unsigned char) (lba >> 16) & 0xff;
540 	cgc.cmd[4] = (unsigned char) (lba >> 8) & 0xff;
541 	cgc.cmd[5] = (unsigned char) lba & 0xff;
542 	cgc.cmd[8] = 1;
543 	cgc.buffer = dest;
544 	cgc.buflen = blksize;
545 	cgc.data_direction = DMA_FROM_DEVICE;
546 	cgc.timeout = IOCTL_TIMEOUT;
547 	rc = sr_do_ioctl(cd, &cgc);
548 
549 	return rc;
550 }
551 
552 /*
553  * read a sector in raw mode to check the sector format
554  * ret: 1 == mode2 (XA), 0 == mode1, <0 == error
555  */
556 
557 int sr_is_xa(Scsi_CD *cd)
558 {
559 	unsigned char *raw_sector;
560 	int is_xa;
561 
562 	if (!xa_test)
563 		return 0;
564 
565 	raw_sector = kmalloc(2048, GFP_KERNEL | SR_GFP_DMA(cd));
566 	if (!raw_sector)
567 		return -ENOMEM;
568 	if (0 == sr_read_sector(cd, cd->ms_offset + 16,
569 				CD_FRAMESIZE_RAW1, raw_sector)) {
570 		is_xa = (raw_sector[3] == 0x02) ? 1 : 0;
571 	} else {
572 		/* read a raw sector failed for some reason. */
573 		is_xa = -1;
574 	}
575 	kfree(raw_sector);
576 #ifdef DEBUG
577 	sr_printk(KERN_INFO, cd, "sr_is_xa: %d\n", is_xa);
578 #endif
579 	return is_xa;
580 }
581