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