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