diff mbox series

[21/24] scsi: consolidate the START STOP UNIT handling

Message ID 20210712054816.4147559-22-hch@lst.de
State New
Headers show
Series [01/24] bsg: remove support for SCSI_IOCTL_SEND_COMMAND | expand

Commit Message

Christoph Hellwig July 12, 2021, 5:48 a.m. UTC
Factor out a helper for the various flavors of START STOP UNIT
command ioctls.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 drivers/scsi/scsi_ioctl.c | 48 ++++++++-------------------------------
 1 file changed, 10 insertions(+), 38 deletions(-)

Comments

Bart Van Assche July 22, 2021, 6:44 p.m. UTC | #1
On 7/11/21 10:48 PM, Christoph Hellwig wrote:
> +	char cdb[MAX_COMMAND_SIZE] = { };


How about using 'u8' instead of 'char'?

Additionally, MAX_COMMAND_SIZE equals 16. According to SBC-4 6 bytes is 
enough for the START STOP UNIT command.

> +	cdb[0] = START_STOP;

> +	cdb[4] = data;


Please combine the above two statements with the cdb[] declaration into 
a single line.

Additionally, please split data into two arguments to make calls of this 
function easier to read. This is what I found in SBC-4:
* bit 1 of byte 4 has the name LOEJ (load eject).
* bit 0 of byte 4 has the name START (start unit).

Thanks,

Bart.
Christoph Hellwig July 22, 2021, 7:20 p.m. UTC | #2
On Thu, Jul 22, 2021 at 11:44:33AM -0700, Bart Van Assche wrote:
> On 7/11/21 10:48 PM, Christoph Hellwig wrote:

>> +	char cdb[MAX_COMMAND_SIZE] = { };

>

> How about using 'u8' instead of 'char'?


Sure.

>

> Additionally, MAX_COMMAND_SIZE equals 16. According to SBC-4 6 bytes is 

> enough for the START STOP UNIT command.


This just keeps the existing logic.

>

>> +	cdb[0] = START_STOP;

>> +	cdb[4] = data;

>

> Please combine the above two statements with the cdb[] declaration into a 

> single line.


Just keeping the style of the existing code, which seems fine.  Even
when initializing at declaration time this would have to be multiple
lines to stay readable.

> Additionally, please split data into two arguments to make calls of this 

> function easier to read. This is what I found in SBC-4:

> * bit 1 of byte 4 has the name LOEJ (load eject).

> * bit 0 of byte 4 has the name START (start unit).


Not sure how that really helps anyone.  And again I'm not trying to
do a grand rewrite, just consoidating the existing code a bit.
diff mbox series

Patch

diff --git a/drivers/scsi/scsi_ioctl.c b/drivers/scsi/scsi_ioctl.c
index 3d4311a78383..f6f16edecc67 100644
--- a/drivers/scsi/scsi_ioctl.c
+++ b/drivers/scsi/scsi_ioctl.c
@@ -218,31 +218,14 @@  static int sg_emulated_host(struct request_queue *q, int __user *p)
 	return put_user(1, p);
 }
 
-/* Send basic block requests */
-static int __blk_send_generic(struct request_queue *q, struct gendisk *bd_disk,
-			      int cmd, int data)
+static int scsi_send_start_stop(struct scsi_device *sdev, int data)
 {
-	struct request *rq;
-	int err;
-
-	rq = blk_get_request(q, REQ_OP_DRV_OUT, 0);
-	if (IS_ERR(rq))
-		return PTR_ERR(rq);
-	rq->timeout = BLK_DEFAULT_SG_TIMEOUT;
-	scsi_req(rq)->cmd[0] = cmd;
-	scsi_req(rq)->cmd[4] = data;
-	scsi_req(rq)->cmd_len = 6;
-	blk_execute_rq(bd_disk, rq, 0);
-	err = scsi_req(rq)->result ? -EIO : 0;
-	blk_put_request(rq);
+	char cdb[MAX_COMMAND_SIZE] = { };
 
-	return err;
-}
-
-static inline int blk_send_start_stop(struct request_queue *q,
-				      struct gendisk *bd_disk, int data)
-{
-	return __blk_send_generic(q, bd_disk, GPCMD_START_STOP_UNIT, data);
+	cdb[0] = START_STOP;
+	cdb[4] = data;
+	return ioctl_internal_command(sdev, cdb, START_STOP_TIMEOUT,
+				      NORMAL_RETRIES);
 }
 
 bool scsi_cmd_allowed(unsigned char *cmd, fmode_t mode)
@@ -877,7 +860,6 @@  int scsi_ioctl(struct scsi_device *sdev, struct gendisk *disk, fmode_t mode,
 		int cmd, void __user *arg)
 {
 	struct request_queue *q = sdev->request_queue;
-	char scsi_cmd[MAX_COMMAND_SIZE];
 	struct scsi_sense_hdr sense_hdr;
 	int error;
 
@@ -930,9 +912,9 @@  int scsi_ioctl(struct scsi_device *sdev, struct gendisk *disk, fmode_t mode,
 	case CDROM_SEND_PACKET:
 		return scsi_cdrom_send_packet(q, disk, mode, arg);
 	case CDROMCLOSETRAY:
-		return blk_send_start_stop(q, disk, 0x03);
+		return scsi_send_start_stop(sdev, 3);
 	case CDROMEJECT:
-		return blk_send_start_stop(q, disk, 0x02);
+		return scsi_send_start_stop(sdev, 2);
 	case SCSI_IOCTL_GET_IDLUN: {
 		struct scsi_idlun v = {
 			.dev_id = (sdev->id & 0xff)
@@ -957,19 +939,9 @@  int scsi_ioctl(struct scsi_device *sdev, struct gendisk *disk, fmode_t mode,
 		return scsi_test_unit_ready(sdev, IOCTL_NORMAL_TIMEOUT,
 					    NORMAL_RETRIES, &sense_hdr);
 	case SCSI_IOCTL_START_UNIT:
-		scsi_cmd[0] = START_STOP;
-		scsi_cmd[1] = 0;
-		scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
-		scsi_cmd[4] = 1;
-		return ioctl_internal_command(sdev, scsi_cmd,
-				     START_STOP_TIMEOUT, NORMAL_RETRIES);
+		return scsi_send_start_stop(sdev, 1);
 	case SCSI_IOCTL_STOP_UNIT:
-		scsi_cmd[0] = START_STOP;
-		scsi_cmd[1] = 0;
-		scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
-		scsi_cmd[4] = 0;
-		return ioctl_internal_command(sdev, scsi_cmd,
-				     START_STOP_TIMEOUT, NORMAL_RETRIES);
+		return scsi_send_start_stop(sdev, 0);
         case SCSI_IOCTL_GET_PCI:
                 return scsi_ioctl_get_pci(sdev, arg);
 	case SG_SCSI_RESET: