1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stddef.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <byteswap.h>
30 #include <errno.h>
31 #include <ipxe/list.h>
32 #include <ipxe/process.h>
33 #include <ipxe/xfer.h>
34 #include <ipxe/blockdev.h>
35 #include <ipxe/scsi.h>
36 
37 /** @file
38  *
39  * SCSI block device
40  *
41  */
42 
43 /** Maximum number of TEST UNIT READY retries */
44 #define SCSI_READY_MAX_RETRIES 10
45 
46 /* Error numbers generated by SCSI sense data */
47 #define EIO_NO_SENSE __einfo_error ( EINFO_EIO_NO_SENSE )
48 #define EINFO_EIO_NO_SENSE \
49 	__einfo_uniqify ( EINFO_EIO, 0x00, "No sense" )
50 #define EIO_RECOVERED_ERROR __einfo_error ( EINFO_EIO_RECOVERED_ERROR )
51 #define EINFO_EIO_RECOVERED_ERROR \
52 	__einfo_uniqify ( EINFO_EIO, 0x01, "Recovered error" )
53 #define EIO_NOT_READY __einfo_error ( EINFO_EIO_NOT_READY )
54 #define EINFO_EIO_NOT_READY \
55 	__einfo_uniqify ( EINFO_EIO, 0x02, "Not ready" )
56 #define EIO_MEDIUM_ERROR __einfo_error ( EINFO_EIO_MEDIUM_ERROR )
57 #define EINFO_EIO_MEDIUM_ERROR \
58 	__einfo_uniqify ( EINFO_EIO, 0x03, "Medium error" )
59 #define EIO_HARDWARE_ERROR __einfo_error ( EINFO_EIO_HARDWARE_ERROR )
60 #define EINFO_EIO_HARDWARE_ERROR \
61 	__einfo_uniqify ( EINFO_EIO, 0x04, "Hardware error" )
62 #define EIO_ILLEGAL_REQUEST __einfo_error ( EINFO_EIO_ILLEGAL_REQUEST )
63 #define EINFO_EIO_ILLEGAL_REQUEST \
64 	__einfo_uniqify ( EINFO_EIO, 0x05, "Illegal request" )
65 #define EIO_UNIT_ATTENTION __einfo_error ( EINFO_EIO_UNIT_ATTENTION )
66 #define EINFO_EIO_UNIT_ATTENTION \
67 	__einfo_uniqify ( EINFO_EIO, 0x06, "Unit attention" )
68 #define EIO_DATA_PROTECT __einfo_error ( EINFO_EIO_DATA_PROTECT )
69 #define EINFO_EIO_DATA_PROTECT \
70 	__einfo_uniqify ( EINFO_EIO, 0x07, "Data protect" )
71 #define EIO_BLANK_CHECK __einfo_error ( EINFO_EIO_BLANK_CHECK )
72 #define EINFO_EIO_BLANK_CHECK \
73 	__einfo_uniqify ( EINFO_EIO, 0x08, "Blank check" )
74 #define EIO_VENDOR_SPECIFIC __einfo_error ( EINFO_EIO_VENDOR_SPECIFIC )
75 #define EINFO_EIO_VENDOR_SPECIFIC \
76 	__einfo_uniqify ( EINFO_EIO, 0x09, "Vendor specific" )
77 #define EIO_COPY_ABORTED __einfo_error ( EINFO_EIO_COPY_ABORTED )
78 #define EINFO_EIO_COPY_ABORTED \
79 	__einfo_uniqify ( EINFO_EIO, 0x0a, "Copy aborted" )
80 #define EIO_ABORTED_COMMAND __einfo_error ( EINFO_EIO_ABORTED_COMMAND )
81 #define EINFO_EIO_ABORTED_COMMAND \
82 	__einfo_uniqify ( EINFO_EIO, 0x0b, "Aborted command" )
83 #define EIO_RESERVED __einfo_error ( EINFO_EIO_RESERVED )
84 #define EINFO_EIO_RESERVED \
85 	__einfo_uniqify ( EINFO_EIO, 0x0c, "Reserved" )
86 #define EIO_VOLUME_OVERFLOW __einfo_error ( EINFO_EIO_VOLUME_OVERFLOW )
87 #define EINFO_EIO_VOLUME_OVERFLOW \
88 	__einfo_uniqify ( EINFO_EIO, 0x0d, "Volume overflow" )
89 #define EIO_MISCOMPARE __einfo_error ( EINFO_EIO_MISCOMPARE )
90 #define EINFO_EIO_MISCOMPARE \
91 	__einfo_uniqify ( EINFO_EIO, 0x0e, "Miscompare" )
92 #define EIO_COMPLETED __einfo_error ( EINFO_EIO_COMPLETED )
93 #define EINFO_EIO_COMPLETED \
94 	__einfo_uniqify ( EINFO_EIO, 0x0f, "Completed" )
95 #define EIO_SENSE( key )						\
96 	EUNIQ ( EINFO_EIO, (key), EIO_NO_SENSE, EIO_RECOVERED_ERROR,	\
97 		EIO_NOT_READY, EIO_MEDIUM_ERROR, EIO_HARDWARE_ERROR,	\
98 		EIO_ILLEGAL_REQUEST, EIO_UNIT_ATTENTION,		\
99 		EIO_DATA_PROTECT, EIO_BLANK_CHECK, EIO_VENDOR_SPECIFIC,	\
100 		EIO_COPY_ABORTED, EIO_ABORTED_COMMAND, EIO_RESERVED,	\
101 		EIO_VOLUME_OVERFLOW, EIO_MISCOMPARE, EIO_COMPLETED )
102 
103 /******************************************************************************
104  *
105  * Utility functions
106  *
107  ******************************************************************************
108  */
109 
110 /**
111  * Parse SCSI LUN
112  *
113  * @v lun_string	LUN string representation
114  * @v lun		LUN to fill in
115  * @ret rc		Return status code
116  */
scsi_parse_lun(const char * lun_string,struct scsi_lun * lun)117 int scsi_parse_lun ( const char *lun_string, struct scsi_lun *lun ) {
118 	char *p;
119 	int i;
120 
121 	memset ( lun, 0, sizeof ( *lun ) );
122 	if ( lun_string ) {
123 		p = ( char * ) lun_string;
124 		for ( i = 0 ; i < 4 ; i++ ) {
125 			lun->u16[i] = htons ( strtoul ( p, &p, 16 ) );
126 			if ( *p == '\0' )
127 				break;
128 			if ( *p != '-' )
129 				return -EINVAL;
130 			p++;
131 		}
132 		if ( *p )
133 			return -EINVAL;
134 	}
135 
136 	return 0;
137 }
138 
139 /**
140  * Parse SCSI sense data
141  *
142  * @v data		Raw sense data
143  * @v len		Length of raw sense data
144  * @v sense		Descriptor-format sense data to fill in
145  */
scsi_parse_sense(const void * data,size_t len,struct scsi_sns_descriptor * sense)146 void scsi_parse_sense ( const void *data, size_t len,
147 			struct scsi_sns_descriptor *sense ) {
148 	const union scsi_sns *sns = data;
149 
150 	/* Avoid returning uninitialised data */
151 	memset ( sense, 0, sizeof ( *sense ) );
152 
153 	/* Copy, assuming descriptor-format data */
154 	if ( len < sizeof ( sns->desc ) )
155 		return;
156 	memcpy ( sense, &sns->desc, sizeof ( *sense ) );
157 
158 	/* Convert fixed-format to descriptor-format, if applicable */
159 	if ( len < sizeof ( sns->fixed ) )
160 		return;
161 	if ( ! SCSI_SENSE_FIXED ( sns->code ) )
162 		return;
163 	sense->additional = sns->fixed.additional;
164 }
165 
166 /******************************************************************************
167  *
168  * Interface methods
169  *
170  ******************************************************************************
171  */
172 
173 /**
174  * Issue SCSI command
175  *
176  * @v control		SCSI control interface
177  * @v data		SCSI data interface
178  * @v command		SCSI command
179  * @ret tag		Command tag, or negative error
180  */
scsi_command(struct interface * control,struct interface * data,struct scsi_cmd * command)181 int scsi_command ( struct interface *control, struct interface *data,
182 		   struct scsi_cmd *command ) {
183 	struct interface *dest;
184 	scsi_command_TYPE ( void * ) *op =
185 		intf_get_dest_op ( control, scsi_command, &dest );
186 	void *object = intf_object ( dest );
187 	int tap;
188 
189 	if ( op ) {
190 		tap = op ( object, data, command );
191 	} else {
192 		/* Default is to fail to issue the command */
193 		tap = -EOPNOTSUPP;
194 	}
195 
196 	intf_put ( dest );
197 	return tap;
198 }
199 
200 /**
201  * Report SCSI response
202  *
203  * @v interface		SCSI command interface
204  * @v response		SCSI response
205  */
scsi_response(struct interface * intf,struct scsi_rsp * response)206 void scsi_response ( struct interface *intf, struct scsi_rsp *response ) {
207 	struct interface *dest;
208 	scsi_response_TYPE ( void * ) *op =
209 		intf_get_dest_op ( intf, scsi_response, &dest );
210 	void *object = intf_object ( dest );
211 
212 	if ( op ) {
213 		op ( object, response );
214 	} else {
215 		/* Default is to ignore the response */
216 	}
217 
218 	intf_put ( dest );
219 }
220 
221 /******************************************************************************
222  *
223  * SCSI devices and commands
224  *
225  ******************************************************************************
226  */
227 
228 /** A SCSI device */
229 struct scsi_device {
230 	/** Reference count */
231 	struct refcnt refcnt;
232 	/** Block control interface */
233 	struct interface block;
234 	/** SCSI control interface */
235 	struct interface scsi;
236 
237 	/** SCSI LUN */
238 	struct scsi_lun lun;
239 	/** Flags */
240 	unsigned int flags;
241 
242 	/** TEST UNIT READY interface */
243 	struct interface ready;
244 	/** TEST UNIT READY process */
245 	struct process process;
246 	/** TEST UNIT READY retry count */
247 	unsigned int retries;
248 
249 	/** List of commands */
250 	struct list_head cmds;
251 };
252 
253 /** SCSI device flags */
254 enum scsi_device_flags {
255 	/** TEST UNIT READY has been issued */
256 	SCSIDEV_UNIT_TESTED = 0x0001,
257 	/** TEST UNIT READY has completed successfully */
258 	SCSIDEV_UNIT_READY = 0x0002,
259 };
260 
261 /** A SCSI command */
262 struct scsi_command {
263 	/** Reference count */
264 	struct refcnt refcnt;
265 	/** SCSI device */
266 	struct scsi_device *scsidev;
267 	/** List of SCSI commands */
268 	struct list_head list;
269 
270 	/** Block data interface */
271 	struct interface block;
272 	/** SCSI data interface */
273 	struct interface scsi;
274 
275 	/** Command type */
276 	struct scsi_command_type *type;
277 	/** Starting logical block address */
278 	uint64_t lba;
279 	/** Number of blocks */
280 	unsigned int count;
281 	/** Data buffer */
282 	userptr_t buffer;
283 	/** Length of data buffer */
284 	size_t len;
285 	/** Command tag */
286 	uint32_t tag;
287 
288 	/** Private data */
289 	uint8_t priv[0];
290 };
291 
292 /** A SCSI command type */
293 struct scsi_command_type {
294 	/** Name */
295 	const char *name;
296 	/** Additional working space */
297 	size_t priv_len;
298 	/**
299 	 * Construct SCSI command IU
300 	 *
301 	 * @v scsicmd		SCSI command
302 	 * @v command		SCSI command IU
303 	 */
304 	void ( * cmd ) ( struct scsi_command *scsicmd,
305 			 struct scsi_cmd *command );
306 	/**
307 	 * Handle SCSI command completion
308 	 *
309 	 * @v scsicmd		SCSI command
310 	 * @v rc		Reason for completion
311 	 */
312 	void ( * done ) ( struct scsi_command *scsicmd, int rc );
313 };
314 
315 /**
316  * Get reference to SCSI device
317  *
318  * @v scsidev		SCSI device
319  * @ret scsidev		SCSI device
320  */
321 static inline __attribute__ (( always_inline )) struct scsi_device *
scsidev_get(struct scsi_device * scsidev)322 scsidev_get ( struct scsi_device *scsidev ) {
323 	ref_get ( &scsidev->refcnt );
324 	return scsidev;
325 }
326 
327 /**
328  * Drop reference to SCSI device
329  *
330  * @v scsidev		SCSI device
331  */
332 static inline __attribute__ (( always_inline )) void
scsidev_put(struct scsi_device * scsidev)333 scsidev_put ( struct scsi_device *scsidev ) {
334 	ref_put ( &scsidev->refcnt );
335 }
336 
337 /**
338  * Get reference to SCSI command
339  *
340  * @v scsicmd		SCSI command
341  * @ret scsicmd		SCSI command
342  */
343 static inline __attribute__ (( always_inline )) struct scsi_command *
scsicmd_get(struct scsi_command * scsicmd)344 scsicmd_get ( struct scsi_command *scsicmd ) {
345 	ref_get ( &scsicmd->refcnt );
346 	return scsicmd;
347 }
348 
349 /**
350  * Drop reference to SCSI command
351  *
352  * @v scsicmd		SCSI command
353  */
354 static inline __attribute__ (( always_inline )) void
scsicmd_put(struct scsi_command * scsicmd)355 scsicmd_put ( struct scsi_command *scsicmd ) {
356 	ref_put ( &scsicmd->refcnt );
357 }
358 
359 /**
360  * Get SCSI command private data
361  *
362  * @v scsicmd		SCSI command
363  * @ret priv		Private data
364  */
365 static inline __attribute__ (( always_inline )) void *
scsicmd_priv(struct scsi_command * scsicmd)366 scsicmd_priv ( struct scsi_command *scsicmd ) {
367 	return scsicmd->priv;
368 }
369 
370 /**
371  * Free SCSI command
372  *
373  * @v refcnt		Reference count
374  */
scsicmd_free(struct refcnt * refcnt)375 static void scsicmd_free ( struct refcnt *refcnt ) {
376 	struct scsi_command *scsicmd =
377 		container_of ( refcnt, struct scsi_command, refcnt );
378 
379 	/* Drop reference to SCSI device */
380 	scsidev_put ( scsicmd->scsidev );
381 
382 	/* Free command */
383 	free ( scsicmd );
384 }
385 
386 /**
387  * Close SCSI command
388  *
389  * @v scsicmd		SCSI command
390  * @v rc		Reason for close
391  */
scsicmd_close(struct scsi_command * scsicmd,int rc)392 static void scsicmd_close ( struct scsi_command *scsicmd, int rc ) {
393 	struct scsi_device *scsidev = scsicmd->scsidev;
394 
395 	if ( rc != 0 ) {
396 		DBGC ( scsidev, "SCSI %p tag %08x closed: %s\n",
397 		       scsidev, scsicmd->tag, strerror ( rc ) );
398 	}
399 
400 	/* Remove from list of commands */
401 	list_del ( &scsicmd->list );
402 
403 	/* Shut down interfaces */
404 	intfs_shutdown ( rc, &scsicmd->scsi, &scsicmd->block, NULL );
405 
406 	/* Drop list's reference */
407 	scsicmd_put ( scsicmd );
408 }
409 
410 /**
411  * Construct and issue SCSI command
412  *
413  * @ret rc		Return status code
414  */
scsicmd_command(struct scsi_command * scsicmd)415 static int scsicmd_command ( struct scsi_command *scsicmd ) {
416 	struct scsi_device *scsidev = scsicmd->scsidev;
417 	struct scsi_cmd command;
418 	int tag;
419 	int rc;
420 
421 	/* Construct command */
422 	memset ( &command, 0, sizeof ( command ) );
423 	memcpy ( &command.lun, &scsidev->lun, sizeof ( command.lun ) );
424 	scsicmd->type->cmd ( scsicmd, &command );
425 
426 	/* Issue command */
427 	if ( ( tag = scsi_command ( &scsidev->scsi, &scsicmd->scsi,
428 				    &command ) ) < 0 ) {
429 		rc = tag;
430 		DBGC ( scsidev, "SCSI %p could not issue command: %s\n",
431 		       scsidev, strerror ( rc ) );
432 		return rc;
433 	}
434 
435 	/* Record tag */
436 	if ( scsicmd->tag ) {
437 		DBGC ( scsidev, "SCSI %p tag %08x is now tag %08x\n",
438 		       scsidev, scsicmd->tag, tag );
439 	}
440 	scsicmd->tag = tag;
441 	DBGC2 ( scsidev, "SCSI %p tag %08x %s " SCSI_CDB_FORMAT "\n",
442 		scsidev, scsicmd->tag, scsicmd->type->name,
443 		SCSI_CDB_DATA ( command.cdb ) );
444 
445 	return 0;
446 }
447 
448 /**
449  * Handle SCSI command completion
450  *
451  * @v scsicmd		SCSI command
452  * @v rc		Reason for close
453  */
scsicmd_done(struct scsi_command * scsicmd,int rc)454 static void scsicmd_done ( struct scsi_command *scsicmd, int rc ) {
455 
456 	/* Restart SCSI interface */
457 	intf_restart ( &scsicmd->scsi, rc );
458 
459 	/* Hand over to the command completion handler */
460 	scsicmd->type->done ( scsicmd, rc );
461 }
462 
463 /**
464  * Handle SCSI response
465  *
466  * @v scsicmd		SCSI command
467  * @v response		SCSI response
468  */
scsicmd_response(struct scsi_command * scsicmd,struct scsi_rsp * response)469 static void scsicmd_response ( struct scsi_command *scsicmd,
470 			       struct scsi_rsp *response ) {
471 	struct scsi_device *scsidev = scsicmd->scsidev;
472 	size_t overrun;
473 	size_t underrun;
474 	int rc;
475 
476 	if ( response->status == 0 ) {
477 		scsicmd_done ( scsicmd, 0 );
478 	} else {
479 		DBGC ( scsidev, "SCSI %p tag %08x status %02x",
480 		       scsidev, scsicmd->tag, response->status );
481 		if ( response->overrun > 0 ) {
482 			overrun = response->overrun;
483 			DBGC ( scsidev, " overrun +%zd", overrun );
484 		} else if ( response->overrun < 0 ) {
485 			underrun = -(response->overrun);
486 			DBGC ( scsidev, " underrun -%zd", underrun );
487 		}
488 		DBGC ( scsidev, " sense %02x key %02x additional %04x\n",
489 		       ( response->sense.code & SCSI_SENSE_CODE_MASK ),
490 		       ( response->sense.key & SCSI_SENSE_KEY_MASK ),
491 		       ntohs ( response->sense.additional ) );
492 
493 		/* Construct error number from sense data */
494 		rc = -EIO_SENSE ( response->sense.key & SCSI_SENSE_KEY_MASK );
495 		scsicmd_done ( scsicmd, rc );
496 	}
497 }
498 
499 /**
500  * Construct SCSI READ command
501  *
502  * @v scsicmd		SCSI command
503  * @v command		SCSI command IU
504  */
scsicmd_read_cmd(struct scsi_command * scsicmd,struct scsi_cmd * command)505 static void scsicmd_read_cmd ( struct scsi_command *scsicmd,
506 			       struct scsi_cmd *command ) {
507 
508 	if ( ( scsicmd->lba + scsicmd->count ) > SCSI_MAX_BLOCK_10 ) {
509 		/* Use READ (16) */
510 		command->cdb.read16.opcode = SCSI_OPCODE_READ_16;
511 		command->cdb.read16.lba = cpu_to_be64 ( scsicmd->lba );
512 		command->cdb.read16.len = cpu_to_be32 ( scsicmd->count );
513 	} else {
514 		/* Use READ (10) */
515 		command->cdb.read10.opcode = SCSI_OPCODE_READ_10;
516 		command->cdb.read10.lba = cpu_to_be32 ( scsicmd->lba );
517 		command->cdb.read10.len = cpu_to_be16 ( scsicmd->count );
518 	}
519 	command->data_in = scsicmd->buffer;
520 	command->data_in_len = scsicmd->len;
521 }
522 
523 /** SCSI READ command type */
524 static struct scsi_command_type scsicmd_read = {
525 	.name = "READ",
526 	.cmd = scsicmd_read_cmd,
527 	.done = scsicmd_close,
528 };
529 
530 /**
531  * Construct SCSI WRITE command
532  *
533  * @v scsicmd		SCSI command
534  * @v command		SCSI command IU
535  */
scsicmd_write_cmd(struct scsi_command * scsicmd,struct scsi_cmd * command)536 static void scsicmd_write_cmd ( struct scsi_command *scsicmd,
537 				struct scsi_cmd *command ) {
538 
539 	if ( ( scsicmd->lba + scsicmd->count ) > SCSI_MAX_BLOCK_10 ) {
540 		/* Use WRITE (16) */
541 		command->cdb.write16.opcode = SCSI_OPCODE_WRITE_16;
542 		command->cdb.write16.lba = cpu_to_be64 ( scsicmd->lba );
543 		command->cdb.write16.len = cpu_to_be32 ( scsicmd->count );
544 	} else {
545 		/* Use WRITE (10) */
546 		command->cdb.write10.opcode = SCSI_OPCODE_WRITE_10;
547 		command->cdb.write10.lba = cpu_to_be32 ( scsicmd->lba );
548 		command->cdb.write10.len = cpu_to_be16 ( scsicmd->count );
549 	}
550 	command->data_out = scsicmd->buffer;
551 	command->data_out_len = scsicmd->len;
552 }
553 
554 /** SCSI WRITE command type */
555 static struct scsi_command_type scsicmd_write = {
556 	.name = "WRITE",
557 	.cmd = scsicmd_write_cmd,
558 	.done = scsicmd_close,
559 };
560 
561 /** SCSI READ CAPACITY private data */
562 struct scsi_read_capacity_private {
563 	/** Use READ CAPACITY (16) */
564 	int use16;
565 	/** Data buffer for READ CAPACITY commands */
566 	union {
567 		/** Data buffer for READ CAPACITY (10) */
568 		struct scsi_capacity_10 capacity10;
569 		/** Data buffer for READ CAPACITY (16) */
570 		struct scsi_capacity_16 capacity16;
571 	} capacity;
572 };
573 
574 /**
575  * Construct SCSI READ CAPACITY command
576  *
577  * @v scsicmd		SCSI command
578  * @v command		SCSI command IU
579  */
scsicmd_read_capacity_cmd(struct scsi_command * scsicmd,struct scsi_cmd * command)580 static void scsicmd_read_capacity_cmd ( struct scsi_command *scsicmd,
581 					struct scsi_cmd *command ) {
582 	struct scsi_read_capacity_private *priv = scsicmd_priv ( scsicmd );
583 	struct scsi_cdb_read_capacity_16 *readcap16 = &command->cdb.readcap16;
584 	struct scsi_cdb_read_capacity_10 *readcap10 = &command->cdb.readcap10;
585 	struct scsi_capacity_16 *capacity16 = &priv->capacity.capacity16;
586 	struct scsi_capacity_10 *capacity10 = &priv->capacity.capacity10;
587 
588 	if ( priv->use16 ) {
589 		/* Use READ CAPACITY (16) */
590 		readcap16->opcode = SCSI_OPCODE_SERVICE_ACTION_IN;
591 		readcap16->service_action =
592 			SCSI_SERVICE_ACTION_READ_CAPACITY_16;
593 		readcap16->len = cpu_to_be32 ( sizeof ( *capacity16 ) );
594 		command->data_in = virt_to_user ( capacity16 );
595 		command->data_in_len = sizeof ( *capacity16 );
596 	} else {
597 		/* Use READ CAPACITY (10) */
598 		readcap10->opcode = SCSI_OPCODE_READ_CAPACITY_10;
599 		command->data_in = virt_to_user ( capacity10 );
600 		command->data_in_len = sizeof ( *capacity10 );
601 	}
602 }
603 
604 /**
605  * Handle SCSI READ CAPACITY command completion
606  *
607  * @v scsicmd		SCSI command
608  * @v rc		Reason for completion
609  */
scsicmd_read_capacity_done(struct scsi_command * scsicmd,int rc)610 static void scsicmd_read_capacity_done ( struct scsi_command *scsicmd,
611 					 int rc ) {
612 	struct scsi_read_capacity_private *priv = scsicmd_priv ( scsicmd );
613 	struct scsi_capacity_16 *capacity16 = &priv->capacity.capacity16;
614 	struct scsi_capacity_10 *capacity10 = &priv->capacity.capacity10;
615 	struct block_device_capacity capacity;
616 
617 	/* Close if command failed */
618 	if ( rc != 0 ) {
619 		scsicmd_close ( scsicmd, rc );
620 		return;
621 	}
622 
623 	/* Extract capacity */
624 	if ( priv->use16 ) {
625 		capacity.blocks = ( be64_to_cpu ( capacity16->lba ) + 1 );
626 		capacity.blksize = be32_to_cpu ( capacity16->blksize );
627 	} else {
628 		capacity.blocks = ( be32_to_cpu ( capacity10->lba ) + 1 );
629 		capacity.blksize = be32_to_cpu ( capacity10->blksize );
630 
631 		/* If capacity range was exceeded (i.e. capacity.lba
632 		 * was 0xffffffff, meaning that blockdev->blocks is
633 		 * now zero), use READ CAPACITY (16) instead.  READ
634 		 * CAPACITY (16) is not mandatory, so we can't just
635 		 * use it straight off.
636 		 */
637 		if ( capacity.blocks == 0 ) {
638 			priv->use16 = 1;
639 			if ( ( rc = scsicmd_command ( scsicmd ) ) != 0 ) {
640 				scsicmd_close ( scsicmd, rc );
641 				return;
642 			}
643 			return;
644 		}
645 	}
646 	capacity.max_count = -1U;
647 
648 	/* Return capacity to caller */
649 	block_capacity ( &scsicmd->block, &capacity );
650 
651 	/* Close command */
652 	scsicmd_close ( scsicmd, 0 );
653 }
654 
655 /** SCSI READ CAPACITY command type */
656 static struct scsi_command_type scsicmd_read_capacity = {
657 	.name = "READ CAPACITY",
658 	.priv_len = sizeof ( struct scsi_read_capacity_private ),
659 	.cmd = scsicmd_read_capacity_cmd,
660 	.done = scsicmd_read_capacity_done,
661 };
662 
663 /**
664  * Construct SCSI TEST UNIT READY command
665  *
666  * @v scsicmd		SCSI command
667  * @v command		SCSI command IU
668  */
scsicmd_test_unit_ready_cmd(struct scsi_command * scsicmd __unused,struct scsi_cmd * command)669 static void scsicmd_test_unit_ready_cmd ( struct scsi_command *scsicmd __unused,
670 					  struct scsi_cmd *command ) {
671 	struct scsi_cdb_test_unit_ready *testready = &command->cdb.testready;
672 
673 	testready->opcode = SCSI_OPCODE_TEST_UNIT_READY;
674 }
675 
676 /** SCSI TEST UNIT READY command type */
677 static struct scsi_command_type scsicmd_test_unit_ready = {
678 	.name = "TEST UNIT READY",
679 	.cmd = scsicmd_test_unit_ready_cmd,
680 	.done = scsicmd_close,
681 };
682 
683 /** SCSI command block interface operations */
684 static struct interface_operation scsicmd_block_op[] = {
685 	INTF_OP ( intf_close, struct scsi_command *, scsicmd_close ),
686 };
687 
688 /** SCSI command block interface descriptor */
689 static struct interface_descriptor scsicmd_block_desc =
690 	INTF_DESC_PASSTHRU ( struct scsi_command, block,
691 			     scsicmd_block_op, scsi );
692 
693 /** SCSI command SCSI interface operations */
694 static struct interface_operation scsicmd_scsi_op[] = {
695 	INTF_OP ( intf_close, struct scsi_command *, scsicmd_done ),
696 	INTF_OP ( scsi_response, struct scsi_command *, scsicmd_response ),
697 };
698 
699 /** SCSI command SCSI interface descriptor */
700 static struct interface_descriptor scsicmd_scsi_desc =
701 	INTF_DESC_PASSTHRU ( struct scsi_command, scsi,
702 			     scsicmd_scsi_op, block );
703 
704 /**
705  * Create SCSI command
706  *
707  * @v scsidev		SCSI device
708  * @v block		Block data interface
709  * @v type		SCSI command type
710  * @v lba		Starting logical block address
711  * @v count		Number of blocks to transfer
712  * @v buffer		Data buffer
713  * @v len		Length of data buffer
714  * @ret rc		Return status code
715  */
scsidev_command(struct scsi_device * scsidev,struct interface * block,struct scsi_command_type * type,uint64_t lba,unsigned int count,userptr_t buffer,size_t len)716 static int scsidev_command ( struct scsi_device *scsidev,
717 			     struct interface *block,
718 			     struct scsi_command_type *type,
719 			     uint64_t lba, unsigned int count,
720 			     userptr_t buffer, size_t len ) {
721 	struct scsi_command *scsicmd;
722 	int rc;
723 
724 	/* Allocate and initialise structure */
725 	scsicmd = zalloc ( sizeof ( *scsicmd ) + type->priv_len );
726 	if ( ! scsicmd ) {
727 		rc = -ENOMEM;
728 		goto err_zalloc;
729 	}
730 	ref_init ( &scsicmd->refcnt, scsicmd_free );
731 	intf_init ( &scsicmd->block, &scsicmd_block_desc, &scsicmd->refcnt );
732 	intf_init ( &scsicmd->scsi, &scsicmd_scsi_desc,
733 		    &scsicmd->refcnt );
734 	scsicmd->scsidev = scsidev_get ( scsidev );
735 	list_add ( &scsicmd->list, &scsidev->cmds );
736 	scsicmd->type = type;
737 	scsicmd->lba = lba;
738 	scsicmd->count = count;
739 	scsicmd->buffer = buffer;
740 	scsicmd->len = len;
741 
742 	/* Issue SCSI command */
743 	if ( ( rc = scsicmd_command ( scsicmd ) ) != 0 )
744 		goto err_command;
745 
746 	/* Attach to parent interface, transfer reference to list, and return */
747 	intf_plug_plug ( &scsicmd->block, block );
748 	return 0;
749 
750  err_command:
751 	scsicmd_close ( scsicmd, rc );
752 	ref_put ( &scsicmd->refcnt );
753  err_zalloc:
754 	return rc;
755 }
756 
757 /**
758  * Issue SCSI block read
759  *
760  * @v scsidev		SCSI device
761  * @v block		Block data interface
762  * @v lba		Starting logical block address
763  * @v count		Number of blocks to transfer
764  * @v buffer		Data buffer
765  * @v len		Length of data buffer
766  * @ret rc		Return status code
767 
768  */
scsidev_read(struct scsi_device * scsidev,struct interface * block,uint64_t lba,unsigned int count,userptr_t buffer,size_t len)769 static int scsidev_read ( struct scsi_device *scsidev,
770 			  struct interface *block,
771 			  uint64_t lba, unsigned int count,
772 			  userptr_t buffer, size_t len ) {
773 	return scsidev_command ( scsidev, block, &scsicmd_read,
774 				 lba, count, buffer, len );
775 }
776 
777 /**
778  * Issue SCSI block write
779  *
780  * @v scsidev		SCSI device
781  * @v block		Block data interface
782  * @v lba		Starting logical block address
783  * @v count		Number of blocks to transfer
784  * @v buffer		Data buffer
785  * @v len		Length of data buffer
786  * @ret rc		Return status code
787  */
scsidev_write(struct scsi_device * scsidev,struct interface * block,uint64_t lba,unsigned int count,userptr_t buffer,size_t len)788 static int scsidev_write ( struct scsi_device *scsidev,
789 			   struct interface *block,
790 			   uint64_t lba, unsigned int count,
791 			   userptr_t buffer, size_t len ) {
792 	return scsidev_command ( scsidev, block, &scsicmd_write,
793 				 lba, count, buffer, len );
794 }
795 
796 /**
797  * Read SCSI device capacity
798  *
799  * @v scsidev		SCSI device
800  * @v block		Block data interface
801  * @ret rc		Return status code
802  */
scsidev_read_capacity(struct scsi_device * scsidev,struct interface * block)803 static int scsidev_read_capacity ( struct scsi_device *scsidev,
804 				   struct interface *block ) {
805 	return scsidev_command ( scsidev, block, &scsicmd_read_capacity,
806 				 0, 0, UNULL, 0 );
807 }
808 
809 /**
810  * Test to see if SCSI device is ready
811  *
812  * @v scsidev		SCSI device
813  * @v block		Block data interface
814  * @ret rc		Return status code
815  */
scsidev_test_unit_ready(struct scsi_device * scsidev,struct interface * block)816 static int scsidev_test_unit_ready ( struct scsi_device *scsidev,
817 				     struct interface *block ) {
818 	return scsidev_command ( scsidev, block, &scsicmd_test_unit_ready,
819 				 0, 0, UNULL, 0 );
820 }
821 
822 /**
823  * Check SCSI device flow-control window
824  *
825  * @v scsidev		SCSI device
826  * @ret len		Length of window
827  */
scsidev_window(struct scsi_device * scsidev)828 static size_t scsidev_window ( struct scsi_device *scsidev ) {
829 
830 	/* Refuse commands until unit is confirmed ready */
831 	if ( ! ( scsidev->flags & SCSIDEV_UNIT_READY ) )
832 		return 0;
833 
834 	return xfer_window ( &scsidev->scsi );
835 }
836 
837 /**
838  * Close SCSI device
839  *
840  * @v scsidev		SCSI device
841  * @v rc		Reason for close
842  */
scsidev_close(struct scsi_device * scsidev,int rc)843 static void scsidev_close ( struct scsi_device *scsidev, int rc ) {
844 	struct scsi_command *scsicmd;
845 	struct scsi_command *tmp;
846 
847 	/* Stop process */
848 	process_del ( &scsidev->process );
849 
850 	/* Shut down interfaces */
851 	intfs_shutdown ( rc, &scsidev->block, &scsidev->scsi, &scsidev->ready,
852 			 NULL );
853 
854 	/* Shut down any remaining commands */
855 	list_for_each_entry_safe ( scsicmd, tmp, &scsidev->cmds, list )
856 		scsicmd_close ( scsicmd, rc );
857 }
858 
859 /** SCSI device block interface operations */
860 static struct interface_operation scsidev_block_op[] = {
861 	INTF_OP ( xfer_window, struct scsi_device *, scsidev_window ),
862 	INTF_OP ( block_read, struct scsi_device *, scsidev_read ),
863 	INTF_OP ( block_write, struct scsi_device *, scsidev_write ),
864 	INTF_OP ( block_read_capacity, struct scsi_device *,
865 		  scsidev_read_capacity ),
866 	INTF_OP ( intf_close, struct scsi_device *, scsidev_close ),
867 };
868 
869 /** SCSI device block interface descriptor */
870 static struct interface_descriptor scsidev_block_desc =
871 	INTF_DESC_PASSTHRU ( struct scsi_device, block,
872 			     scsidev_block_op, scsi );
873 
874 /**
875  * Handle SCSI TEST UNIT READY response
876  *
877  * @v scsidev		SCSI device
878  * @v rc		Reason for close
879  */
scsidev_ready(struct scsi_device * scsidev,int rc)880 static void scsidev_ready ( struct scsi_device *scsidev, int rc ) {
881 
882 	/* Shut down interface */
883 	intf_restart ( &scsidev->ready, rc );
884 
885 	/* Mark device as ready, if applicable */
886 	if ( rc == 0 ) {
887 		DBGC ( scsidev, "SCSI %p unit is ready\n", scsidev );
888 		scsidev->flags |= SCSIDEV_UNIT_READY;
889 		xfer_window_changed ( &scsidev->block );
890 		return;
891 	}
892 	DBGC ( scsidev, "SCSI %p not ready: %s\n", scsidev, strerror ( rc ) );
893 
894 	/* SCSI targets have an annoying habit of returning occasional
895 	 * pointless "error" messages such as "power-on occurred", so
896 	 * we have to be prepared to retry commands.
897 	 *
898 	 * For most commands, we rely on the caller (e.g. the generic
899 	 * SAN device layer) to retry commands as needed.  However, a
900 	 * TEST UNIT READY failure is used as an indication that the
901 	 * whole SCSI device is unavailable and should be closed.  We
902 	 * must therefore perform this retry loop within the SCSI
903 	 * layer.
904 	 */
905 	if ( scsidev->retries++ < SCSI_READY_MAX_RETRIES ) {
906 		DBGC ( scsidev, "SCSI %p retrying (retry %d)\n",
907 		       scsidev, scsidev->retries );
908 		scsidev->flags &= ~SCSIDEV_UNIT_TESTED;
909 		process_add ( &scsidev->process );
910 		return;
911 	}
912 
913 	/* Close device */
914 	DBGC ( scsidev, "SCSI %p never became ready: %s\n",
915 	       scsidev, strerror ( rc ) );
916 	scsidev_close ( scsidev, rc );
917 }
918 
919 /** SCSI device TEST UNIT READY interface operations */
920 static struct interface_operation scsidev_ready_op[] = {
921 	INTF_OP ( intf_close, struct scsi_device *, scsidev_ready ),
922 };
923 
924 /** SCSI device TEST UNIT READY interface descriptor */
925 static struct interface_descriptor scsidev_ready_desc =
926 	INTF_DESC ( struct scsi_device, ready, scsidev_ready_op );
927 
928 /**
929  * SCSI TEST UNIT READY process
930  *
931  * @v scsidev		SCSI device
932  */
scsidev_step(struct scsi_device * scsidev)933 static void scsidev_step ( struct scsi_device *scsidev ) {
934 	int rc;
935 
936 	/* Do nothing if we have already issued TEST UNIT READY */
937 	if ( scsidev->flags & SCSIDEV_UNIT_TESTED )
938 		return;
939 
940 	/* Wait until underlying SCSI device is ready */
941 	if ( xfer_window ( &scsidev->scsi ) == 0 )
942 		return;
943 
944 	DBGC ( scsidev, "SCSI %p waiting for unit to become ready\n",
945 	       scsidev );
946 
947 	/* Mark TEST UNIT READY as sent */
948 	scsidev->flags |= SCSIDEV_UNIT_TESTED;
949 
950 	/* Issue TEST UNIT READY command */
951 	if ( ( rc = scsidev_test_unit_ready ( scsidev, &scsidev->ready )) !=0){
952 		scsidev_close ( scsidev, rc );
953 		return;
954 	}
955 }
956 
957 /** SCSI device SCSI interface operations */
958 static struct interface_operation scsidev_scsi_op[] = {
959 	INTF_OP ( xfer_window_changed, struct scsi_device *, scsidev_step ),
960 	INTF_OP ( intf_close, struct scsi_device *, scsidev_close ),
961 };
962 
963 /** SCSI device SCSI interface descriptor */
964 static struct interface_descriptor scsidev_scsi_desc =
965 	INTF_DESC_PASSTHRU ( struct scsi_device, scsi,
966 			     scsidev_scsi_op, block );
967 
968 /** SCSI device process descriptor */
969 static struct process_descriptor scsidev_process_desc =
970 	PROC_DESC_ONCE ( struct scsi_device, process, scsidev_step );
971 
972 /**
973  * Open SCSI device
974  *
975  * @v block		Block control interface
976  * @v scsi		SCSI control interface
977  * @v lun		SCSI LUN
978  * @ret rc		Return status code
979  */
scsi_open(struct interface * block,struct interface * scsi,struct scsi_lun * lun)980 int scsi_open ( struct interface *block, struct interface *scsi,
981 		struct scsi_lun *lun ) {
982 	struct scsi_device *scsidev;
983 
984 	/* Allocate and initialise structure */
985 	scsidev = zalloc ( sizeof ( *scsidev ) );
986 	if ( ! scsidev )
987 		return -ENOMEM;
988 	ref_init ( &scsidev->refcnt, NULL );
989 	intf_init ( &scsidev->block, &scsidev_block_desc, &scsidev->refcnt );
990 	intf_init ( &scsidev->scsi, &scsidev_scsi_desc, &scsidev->refcnt );
991 	intf_init ( &scsidev->ready, &scsidev_ready_desc, &scsidev->refcnt );
992 	process_init ( &scsidev->process, &scsidev_process_desc,
993 		       &scsidev->refcnt );
994 	INIT_LIST_HEAD ( &scsidev->cmds );
995 	memcpy ( &scsidev->lun, lun, sizeof ( scsidev->lun ) );
996 	DBGC ( scsidev, "SCSI %p created for LUN " SCSI_LUN_FORMAT "\n",
997 	       scsidev, SCSI_LUN_DATA ( scsidev->lun ) );
998 
999 	/* Attach to SCSI and parent interfaces, mortalise self, and return */
1000 	intf_plug_plug ( &scsidev->scsi, scsi );
1001 	intf_plug_plug ( &scsidev->block, block );
1002 	ref_put ( &scsidev->refcnt );
1003 	return 0;
1004 }
1005