These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / 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  * 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 command retries */
44 #define SCSICMD_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  */
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  */
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  */
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  */
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
247         /** List of commands */
248         struct list_head cmds;
249 };
250
251 /** SCSI device flags */
252 enum scsi_device_flags {
253         /** TEST UNIT READY has been issued */
254         SCSIDEV_UNIT_TESTED = 0x0001,
255         /** TEST UNIT READY has completed successfully */
256         SCSIDEV_UNIT_READY = 0x0002,
257 };
258
259 /** A SCSI command */
260 struct scsi_command {
261         /** Reference count */
262         struct refcnt refcnt;
263         /** SCSI device */
264         struct scsi_device *scsidev;
265         /** List of SCSI commands */
266         struct list_head list;
267
268         /** Block data interface */
269         struct interface block;
270         /** SCSI data interface */
271         struct interface scsi;
272
273         /** Command type */
274         struct scsi_command_type *type;
275         /** Starting logical block address */
276         uint64_t lba;
277         /** Number of blocks */
278         unsigned int count;
279         /** Data buffer */
280         userptr_t buffer;
281         /** Length of data buffer */
282         size_t len;
283         /** Command tag */
284         uint32_t tag;
285
286         /** Retry count */
287         unsigned int retries;
288
289         /** Private data */
290         uint8_t priv[0];
291 };
292
293 /** A SCSI command type */
294 struct scsi_command_type {
295         /** Name */
296         const char *name;
297         /** Additional working space */
298         size_t priv_len;
299         /**
300          * Construct SCSI command IU
301          *
302          * @v scsicmd           SCSI command
303          * @v command           SCSI command IU
304          */
305         void ( * cmd ) ( struct scsi_command *scsicmd,
306                          struct scsi_cmd *command );
307         /**
308          * Handle SCSI command completion
309          *
310          * @v scsicmd           SCSI command
311          * @v rc                Reason for completion
312          */
313         void ( * done ) ( struct scsi_command *scsicmd, int rc );
314 };
315
316 /**
317  * Get reference to SCSI device
318  *
319  * @v scsidev           SCSI device
320  * @ret scsidev         SCSI device
321  */
322 static inline __attribute__ (( always_inline )) struct scsi_device *
323 scsidev_get ( struct scsi_device *scsidev ) {
324         ref_get ( &scsidev->refcnt );
325         return scsidev;
326 }
327
328 /**
329  * Drop reference to SCSI device
330  *
331  * @v scsidev           SCSI device
332  */
333 static inline __attribute__ (( always_inline )) void
334 scsidev_put ( struct scsi_device *scsidev ) {
335         ref_put ( &scsidev->refcnt );
336 }
337
338 /**
339  * Get reference to SCSI command
340  *
341  * @v scsicmd           SCSI command
342  * @ret scsicmd         SCSI command
343  */
344 static inline __attribute__ (( always_inline )) struct scsi_command *
345 scsicmd_get ( struct scsi_command *scsicmd ) {
346         ref_get ( &scsicmd->refcnt );
347         return scsicmd;
348 }
349
350 /**
351  * Drop reference to SCSI command
352  *
353  * @v scsicmd           SCSI command
354  */
355 static inline __attribute__ (( always_inline )) void
356 scsicmd_put ( struct scsi_command *scsicmd ) {
357         ref_put ( &scsicmd->refcnt );
358 }
359
360 /**
361  * Get SCSI command private data
362  *
363  * @v scsicmd           SCSI command
364  * @ret priv            Private data
365  */
366 static inline __attribute__ (( always_inline )) void *
367 scsicmd_priv ( struct scsi_command *scsicmd ) {
368         return scsicmd->priv;
369 }
370
371 /**
372  * Free SCSI command
373  *
374  * @v refcnt            Reference count
375  */
376 static void scsicmd_free ( struct refcnt *refcnt ) {
377         struct scsi_command *scsicmd =
378                 container_of ( refcnt, struct scsi_command, refcnt );
379
380         /* Remove from list of commands */
381         list_del ( &scsicmd->list );
382         scsidev_put ( scsicmd->scsidev );
383
384         /* Free command */
385         free ( scsicmd );
386 }
387
388 /**
389  * Close SCSI command
390  *
391  * @v scsicmd           SCSI command
392  * @v rc                Reason for close
393  */
394 static void scsicmd_close ( struct scsi_command *scsicmd, int rc ) {
395         struct scsi_device *scsidev = scsicmd->scsidev;
396
397         if ( rc != 0 ) {
398                 DBGC ( scsidev, "SCSI %p tag %08x closed: %s\n",
399                        scsidev, scsicmd->tag, strerror ( rc ) );
400         }
401
402         /* Shut down interfaces */
403         intf_shutdown ( &scsicmd->scsi, rc );
404         intf_shutdown ( &scsicmd->block, rc );
405 }
406
407 /**
408  * Construct and issue SCSI command
409  *
410  * @ret rc              Return status code
411  */
412 static int scsicmd_command ( struct scsi_command *scsicmd ) {
413         struct scsi_device *scsidev = scsicmd->scsidev;
414         struct scsi_cmd command;
415         int tag;
416         int rc;
417
418         /* Construct command */
419         memset ( &command, 0, sizeof ( command ) );
420         memcpy ( &command.lun, &scsidev->lun, sizeof ( command.lun ) );
421         scsicmd->type->cmd ( scsicmd, &command );
422
423         /* Issue command */
424         if ( ( tag = scsi_command ( &scsidev->scsi, &scsicmd->scsi,
425                                     &command ) ) < 0 ) {
426                 rc = tag;
427                 DBGC ( scsidev, "SCSI %p could not issue command: %s\n",
428                        scsidev, strerror ( rc ) );
429                 return rc;
430         }
431
432         /* Record tag */
433         if ( scsicmd->tag ) {
434                 DBGC ( scsidev, "SCSI %p tag %08x is now tag %08x\n",
435                        scsidev, scsicmd->tag, tag );
436         }
437         scsicmd->tag = tag;
438         DBGC2 ( scsidev, "SCSI %p tag %08x %s " SCSI_CDB_FORMAT "\n",
439                 scsidev, scsicmd->tag, scsicmd->type->name,
440                 SCSI_CDB_DATA ( command.cdb ) );
441
442         return 0;
443 }
444
445 /**
446  * Handle SCSI command completion
447  *
448  * @v scsicmd           SCSI command
449  * @v rc                Reason for close
450  */
451 static void scsicmd_done ( struct scsi_command *scsicmd, int rc ) {
452         struct scsi_device *scsidev = scsicmd->scsidev;
453
454         /* Restart SCSI interface */
455         intf_restart ( &scsicmd->scsi, rc );
456
457         /* SCSI targets have an annoying habit of returning occasional
458          * pointless "error" messages such as "power-on occurred", so
459          * we have to be prepared to retry commands.
460          */
461         if ( ( rc != 0 ) && ( scsicmd->retries++ < SCSICMD_MAX_RETRIES ) ) {
462                 /* Retry command */
463                 DBGC ( scsidev, "SCSI %p tag %08x failed: %s\n",
464                        scsidev, scsicmd->tag, strerror ( rc ) );
465                 DBGC ( scsidev, "SCSI %p tag %08x retrying (retry %d)\n",
466                        scsidev, scsicmd->tag, scsicmd->retries );
467                 if ( ( rc = scsicmd_command ( scsicmd ) ) == 0 )
468                         return;
469         }
470
471         /* If we didn't (successfully) reissue the command, hand over
472          * to the command completion handler.
473          */
474         scsicmd->type->done ( scsicmd, rc );
475 }
476
477 /**
478  * Handle SCSI response
479  *
480  * @v scsicmd           SCSI command
481  * @v response          SCSI response
482  */
483 static void scsicmd_response ( struct scsi_command *scsicmd,
484                                struct scsi_rsp *response ) {
485         struct scsi_device *scsidev = scsicmd->scsidev;
486         size_t overrun;
487         size_t underrun;
488         int rc;
489
490         if ( response->status == 0 ) {
491                 scsicmd_done ( scsicmd, 0 );
492         } else {
493                 DBGC ( scsidev, "SCSI %p tag %08x status %02x",
494                        scsidev, scsicmd->tag, response->status );
495                 if ( response->overrun > 0 ) {
496                         overrun = response->overrun;
497                         DBGC ( scsidev, " overrun +%zd", overrun );
498                 } else if ( response->overrun < 0 ) {
499                         underrun = -(response->overrun);
500                         DBGC ( scsidev, " underrun -%zd", underrun );
501                 }
502                 DBGC ( scsidev, " sense %02x key %02x additional %04x\n",
503                        ( response->sense.code & SCSI_SENSE_CODE_MASK ),
504                        ( response->sense.key & SCSI_SENSE_KEY_MASK ),
505                        ntohs ( response->sense.additional ) );
506
507                 /* Construct error number from sense data */
508                 rc = -EIO_SENSE ( response->sense.key & SCSI_SENSE_KEY_MASK );
509                 scsicmd_done ( scsicmd, rc );
510         }
511 }
512
513 /**
514  * Construct SCSI READ command
515  *
516  * @v scsicmd           SCSI command
517  * @v command           SCSI command IU
518  */
519 static void scsicmd_read_cmd ( struct scsi_command *scsicmd,
520                                struct scsi_cmd *command ) {
521
522         if ( ( scsicmd->lba + scsicmd->count ) > SCSI_MAX_BLOCK_10 ) {
523                 /* Use READ (16) */
524                 command->cdb.read16.opcode = SCSI_OPCODE_READ_16;
525                 command->cdb.read16.lba = cpu_to_be64 ( scsicmd->lba );
526                 command->cdb.read16.len = cpu_to_be32 ( scsicmd->count );
527         } else {
528                 /* Use READ (10) */
529                 command->cdb.read10.opcode = SCSI_OPCODE_READ_10;
530                 command->cdb.read10.lba = cpu_to_be32 ( scsicmd->lba );
531                 command->cdb.read10.len = cpu_to_be16 ( scsicmd->count );
532         }
533         command->data_in = scsicmd->buffer;
534         command->data_in_len = scsicmd->len;
535 }
536
537 /** SCSI READ command type */
538 static struct scsi_command_type scsicmd_read = {
539         .name = "READ",
540         .cmd = scsicmd_read_cmd,
541         .done = scsicmd_close,
542 };
543
544 /**
545  * Construct SCSI WRITE command
546  *
547  * @v scsicmd           SCSI command
548  * @v command           SCSI command IU
549  */
550 static void scsicmd_write_cmd ( struct scsi_command *scsicmd,
551                                 struct scsi_cmd *command ) {
552
553         if ( ( scsicmd->lba + scsicmd->count ) > SCSI_MAX_BLOCK_10 ) {
554                 /* Use WRITE (16) */
555                 command->cdb.write16.opcode = SCSI_OPCODE_WRITE_16;
556                 command->cdb.write16.lba = cpu_to_be64 ( scsicmd->lba );
557                 command->cdb.write16.len = cpu_to_be32 ( scsicmd->count );
558         } else {
559                 /* Use WRITE (10) */
560                 command->cdb.write10.opcode = SCSI_OPCODE_WRITE_10;
561                 command->cdb.write10.lba = cpu_to_be32 ( scsicmd->lba );
562                 command->cdb.write10.len = cpu_to_be16 ( scsicmd->count );
563         }
564         command->data_out = scsicmd->buffer;
565         command->data_out_len = scsicmd->len;
566 }
567
568 /** SCSI WRITE command type */
569 static struct scsi_command_type scsicmd_write = {
570         .name = "WRITE",
571         .cmd = scsicmd_write_cmd,
572         .done = scsicmd_close,
573 };
574
575 /** SCSI READ CAPACITY private data */
576 struct scsi_read_capacity_private {
577         /** Use READ CAPACITY (16) */
578         int use16;
579         /** Data buffer for READ CAPACITY commands */
580         union {
581                 /** Data buffer for READ CAPACITY (10) */
582                 struct scsi_capacity_10 capacity10;
583                 /** Data buffer for READ CAPACITY (16) */
584                 struct scsi_capacity_16 capacity16;
585         } capacity;
586 };
587
588 /**
589  * Construct SCSI READ CAPACITY command
590  *
591  * @v scsicmd           SCSI command
592  * @v command           SCSI command IU
593  */
594 static void scsicmd_read_capacity_cmd ( struct scsi_command *scsicmd,
595                                         struct scsi_cmd *command ) {
596         struct scsi_read_capacity_private *priv = scsicmd_priv ( scsicmd );
597         struct scsi_cdb_read_capacity_16 *readcap16 = &command->cdb.readcap16;
598         struct scsi_cdb_read_capacity_10 *readcap10 = &command->cdb.readcap10;
599         struct scsi_capacity_16 *capacity16 = &priv->capacity.capacity16;
600         struct scsi_capacity_10 *capacity10 = &priv->capacity.capacity10;
601
602         if ( priv->use16 ) {
603                 /* Use READ CAPACITY (16) */
604                 readcap16->opcode = SCSI_OPCODE_SERVICE_ACTION_IN;
605                 readcap16->service_action =
606                         SCSI_SERVICE_ACTION_READ_CAPACITY_16;
607                 readcap16->len = cpu_to_be32 ( sizeof ( *capacity16 ) );
608                 command->data_in = virt_to_user ( capacity16 );
609                 command->data_in_len = sizeof ( *capacity16 );
610         } else {
611                 /* Use READ CAPACITY (10) */
612                 readcap10->opcode = SCSI_OPCODE_READ_CAPACITY_10;
613                 command->data_in = virt_to_user ( capacity10 );
614                 command->data_in_len = sizeof ( *capacity10 );
615         }
616 }
617
618 /**
619  * Handle SCSI READ CAPACITY command completion
620  *
621  * @v scsicmd           SCSI command
622  * @v rc                Reason for completion
623  */
624 static void scsicmd_read_capacity_done ( struct scsi_command *scsicmd,
625                                          int rc ) {
626         struct scsi_read_capacity_private *priv = scsicmd_priv ( scsicmd );
627         struct scsi_capacity_16 *capacity16 = &priv->capacity.capacity16;
628         struct scsi_capacity_10 *capacity10 = &priv->capacity.capacity10;
629         struct block_device_capacity capacity;
630
631         /* Close if command failed */
632         if ( rc != 0 ) {
633                 scsicmd_close ( scsicmd, rc );
634                 return;
635         }
636
637         /* Extract capacity */
638         if ( priv->use16 ) {
639                 capacity.blocks = ( be64_to_cpu ( capacity16->lba ) + 1 );
640                 capacity.blksize = be32_to_cpu ( capacity16->blksize );
641         } else {
642                 capacity.blocks = ( be32_to_cpu ( capacity10->lba ) + 1 );
643                 capacity.blksize = be32_to_cpu ( capacity10->blksize );
644
645                 /* If capacity range was exceeded (i.e. capacity.lba
646                  * was 0xffffffff, meaning that blockdev->blocks is
647                  * now zero), use READ CAPACITY (16) instead.  READ
648                  * CAPACITY (16) is not mandatory, so we can't just
649                  * use it straight off.
650                  */
651                 if ( capacity.blocks == 0 ) {
652                         priv->use16 = 1;
653                         if ( ( rc = scsicmd_command ( scsicmd ) ) != 0 ) {
654                                 scsicmd_close ( scsicmd, rc );
655                                 return;
656                         }
657                         return;
658                 }
659         }
660         capacity.max_count = -1U;
661
662         /* Return capacity to caller */
663         block_capacity ( &scsicmd->block, &capacity );
664
665         /* Close command */
666         scsicmd_close ( scsicmd, 0 );
667 }
668
669 /** SCSI READ CAPACITY command type */
670 static struct scsi_command_type scsicmd_read_capacity = {
671         .name = "READ CAPACITY",
672         .priv_len = sizeof ( struct scsi_read_capacity_private ),
673         .cmd = scsicmd_read_capacity_cmd,
674         .done = scsicmd_read_capacity_done,
675 };
676
677 /**
678  * Construct SCSI TEST UNIT READY command
679  *
680  * @v scsicmd           SCSI command
681  * @v command           SCSI command IU
682  */
683 static void scsicmd_test_unit_ready_cmd ( struct scsi_command *scsicmd __unused,
684                                           struct scsi_cmd *command ) {
685         struct scsi_cdb_test_unit_ready *testready = &command->cdb.testready;
686
687         testready->opcode = SCSI_OPCODE_TEST_UNIT_READY;
688 }
689
690 /** SCSI TEST UNIT READY command type */
691 static struct scsi_command_type scsicmd_test_unit_ready = {
692         .name = "TEST UNIT READY",
693         .cmd = scsicmd_test_unit_ready_cmd,
694         .done = scsicmd_close,
695 };
696
697 /** SCSI command block interface operations */
698 static struct interface_operation scsicmd_block_op[] = {
699         INTF_OP ( intf_close, struct scsi_command *, scsicmd_close ),
700 };
701
702 /** SCSI command block interface descriptor */
703 static struct interface_descriptor scsicmd_block_desc =
704         INTF_DESC_PASSTHRU ( struct scsi_command, block,
705                              scsicmd_block_op, scsi );
706
707 /** SCSI command SCSI interface operations */
708 static struct interface_operation scsicmd_scsi_op[] = {
709         INTF_OP ( intf_close, struct scsi_command *, scsicmd_done ),
710         INTF_OP ( scsi_response, struct scsi_command *, scsicmd_response ),
711 };
712
713 /** SCSI command SCSI interface descriptor */
714 static struct interface_descriptor scsicmd_scsi_desc =
715         INTF_DESC_PASSTHRU ( struct scsi_command, scsi,
716                              scsicmd_scsi_op, block );
717
718 /**
719  * Create SCSI command
720  *
721  * @v scsidev           SCSI device
722  * @v block             Block data interface
723  * @v type              SCSI command type
724  * @v lba               Starting logical block address
725  * @v count             Number of blocks to transfer
726  * @v buffer            Data buffer
727  * @v len               Length of data buffer
728  * @ret rc              Return status code
729  */
730 static int scsidev_command ( struct scsi_device *scsidev,
731                              struct interface *block,
732                              struct scsi_command_type *type,
733                              uint64_t lba, unsigned int count,
734                              userptr_t buffer, size_t len ) {
735         struct scsi_command *scsicmd;
736         int rc;
737
738         /* Allocate and initialise structure */
739         scsicmd = zalloc ( sizeof ( *scsicmd ) + type->priv_len );
740         if ( ! scsicmd ) {
741                 rc = -ENOMEM;
742                 goto err_zalloc;
743         }
744         ref_init ( &scsicmd->refcnt, scsicmd_free );
745         intf_init ( &scsicmd->block, &scsicmd_block_desc, &scsicmd->refcnt );
746         intf_init ( &scsicmd->scsi, &scsicmd_scsi_desc,
747                     &scsicmd->refcnt );
748         scsicmd->scsidev = scsidev_get ( scsidev );
749         list_add ( &scsicmd->list, &scsidev->cmds );
750         scsicmd->type = type;
751         scsicmd->lba = lba;
752         scsicmd->count = count;
753         scsicmd->buffer = buffer;
754         scsicmd->len = len;
755
756         /* Issue SCSI command */
757         if ( ( rc = scsicmd_command ( scsicmd ) ) != 0 )
758                 goto err_command;
759
760         /* Attach to parent interface, mortalise self, and return */
761         intf_plug_plug ( &scsicmd->block, block );
762         ref_put ( &scsicmd->refcnt );
763         return 0;
764
765  err_command:
766         scsicmd_close ( scsicmd, rc );
767         ref_put ( &scsicmd->refcnt );
768  err_zalloc:
769         return rc;
770 }
771
772 /**
773  * Issue SCSI block read
774  *
775  * @v scsidev           SCSI device
776  * @v block             Block data interface
777  * @v lba               Starting logical block address
778  * @v count             Number of blocks to transfer
779  * @v buffer            Data buffer
780  * @v len               Length of data buffer
781  * @ret rc              Return status code
782
783  */
784 static int scsidev_read ( struct scsi_device *scsidev,
785                           struct interface *block,
786                           uint64_t lba, unsigned int count,
787                           userptr_t buffer, size_t len ) {
788         return scsidev_command ( scsidev, block, &scsicmd_read,
789                                  lba, count, buffer, len );
790 }
791
792 /**
793  * Issue SCSI block write
794  *
795  * @v scsidev           SCSI device
796  * @v block             Block data interface
797  * @v lba               Starting logical block address
798  * @v count             Number of blocks to transfer
799  * @v buffer            Data buffer
800  * @v len               Length of data buffer
801  * @ret rc              Return status code
802  */
803 static int scsidev_write ( struct scsi_device *scsidev,
804                            struct interface *block,
805                            uint64_t lba, unsigned int count,
806                            userptr_t buffer, size_t len ) {
807         return scsidev_command ( scsidev, block, &scsicmd_write,
808                                  lba, count, buffer, len );
809 }
810
811 /**
812  * Read SCSI device capacity
813  *
814  * @v scsidev           SCSI device
815  * @v block             Block data interface
816  * @ret rc              Return status code
817  */
818 static int scsidev_read_capacity ( struct scsi_device *scsidev,
819                                    struct interface *block ) {
820         return scsidev_command ( scsidev, block, &scsicmd_read_capacity,
821                                  0, 0, UNULL, 0 );
822 }
823
824 /**
825  * Test to see if SCSI device is ready
826  *
827  * @v scsidev           SCSI device
828  * @v block             Block data interface
829  * @ret rc              Return status code
830  */
831 static int scsidev_test_unit_ready ( struct scsi_device *scsidev,
832                                      struct interface *block ) {
833         return scsidev_command ( scsidev, block, &scsicmd_test_unit_ready,
834                                  0, 0, UNULL, 0 );
835 }
836
837 /**
838  * Check SCSI device flow-control window
839  *
840  * @v scsidev           SCSI device
841  * @ret len             Length of window
842  */
843 static size_t scsidev_window ( struct scsi_device *scsidev ) {
844
845         /* Refuse commands until unit is confirmed ready */
846         if ( ! ( scsidev->flags & SCSIDEV_UNIT_READY ) )
847                 return 0;
848
849         return xfer_window ( &scsidev->scsi );
850 }
851
852 /**
853  * Close SCSI device
854  *
855  * @v scsidev           SCSI device
856  * @v rc                Reason for close
857  */
858 static void scsidev_close ( struct scsi_device *scsidev, int rc ) {
859         struct scsi_command *scsicmd;
860         struct scsi_command *tmp;
861
862         /* Stop process */
863         process_del ( &scsidev->process );
864
865         /* Shut down interfaces */
866         intf_shutdown ( &scsidev->block, rc );
867         intf_shutdown ( &scsidev->scsi, rc );
868         intf_shutdown ( &scsidev->ready, rc );
869
870         /* Shut down any remaining commands */
871         list_for_each_entry_safe ( scsicmd, tmp, &scsidev->cmds, list ) {
872                 scsicmd_get ( scsicmd );
873                 scsicmd_close ( scsicmd, rc );
874                 scsicmd_put ( scsicmd );
875         }
876 }
877
878 /** SCSI device block interface operations */
879 static struct interface_operation scsidev_block_op[] = {
880         INTF_OP ( xfer_window, struct scsi_device *, scsidev_window ),
881         INTF_OP ( block_read, struct scsi_device *, scsidev_read ),
882         INTF_OP ( block_write, struct scsi_device *, scsidev_write ),
883         INTF_OP ( block_read_capacity, struct scsi_device *,
884                   scsidev_read_capacity ),
885         INTF_OP ( intf_close, struct scsi_device *, scsidev_close ),
886 };
887
888 /** SCSI device block interface descriptor */
889 static struct interface_descriptor scsidev_block_desc =
890         INTF_DESC_PASSTHRU ( struct scsi_device, block,
891                              scsidev_block_op, scsi );
892
893 /**
894  * Handle SCSI TEST UNIT READY response
895  *
896  * @v scsidev           SCSI device
897  * @v rc                Reason for close
898  */
899 static void scsidev_ready ( struct scsi_device *scsidev, int rc ) {
900
901         /* Shut down interface */
902         intf_shutdown ( &scsidev->ready, rc );
903
904         /* Close device on failure */
905         if ( rc != 0 ) {
906                 DBGC ( scsidev, "SCSI %p not ready: %s\n",
907                        scsidev, strerror ( rc ) );
908                 scsidev_close ( scsidev, rc );
909                 return;
910         }
911
912         /* Mark device as ready */
913         scsidev->flags |= SCSIDEV_UNIT_READY;
914         xfer_window_changed ( &scsidev->block );
915         DBGC ( scsidev, "SCSI %p unit is ready\n", scsidev );
916 }
917
918 /** SCSI device TEST UNIT READY interface operations */
919 static struct interface_operation scsidev_ready_op[] = {
920         INTF_OP ( intf_close, struct scsi_device *, scsidev_ready ),
921 };
922
923 /** SCSI device TEST UNIT READY interface descriptor */
924 static struct interface_descriptor scsidev_ready_desc =
925         INTF_DESC ( struct scsi_device, ready, scsidev_ready_op );
926
927 /**
928  * SCSI TEST UNIT READY process
929  *
930  * @v scsidev           SCSI device
931  */
932 static void scsidev_step ( struct scsi_device *scsidev ) {
933         int rc;
934
935         /* Do nothing if we have already issued TEST UNIT READY */
936         if ( scsidev->flags & SCSIDEV_UNIT_TESTED )
937                 return;
938
939         /* Wait until underlying SCSI device is ready */
940         if ( xfer_window ( &scsidev->scsi ) == 0 )
941                 return;
942
943         DBGC ( scsidev, "SCSI %p waiting for unit to become ready\n",
944                scsidev );
945
946         /* Mark TEST UNIT READY as sent */
947         scsidev->flags |= SCSIDEV_UNIT_TESTED;
948
949         /* Issue TEST UNIT READY command */
950         if ( ( rc = scsidev_test_unit_ready ( scsidev, &scsidev->ready )) !=0){
951                 scsidev_close ( scsidev, rc );
952                 return;
953         }
954 }
955
956 /** SCSI device SCSI interface operations */
957 static struct interface_operation scsidev_scsi_op[] = {
958         INTF_OP ( xfer_window_changed, struct scsi_device *, scsidev_step ),
959         INTF_OP ( intf_close, struct scsi_device *, scsidev_close ),
960 };
961
962 /** SCSI device SCSI interface descriptor */
963 static struct interface_descriptor scsidev_scsi_desc =
964         INTF_DESC_PASSTHRU ( struct scsi_device, scsi,
965                              scsidev_scsi_op, block );
966
967 /** SCSI device process descriptor */
968 static struct process_descriptor scsidev_process_desc =
969         PROC_DESC_ONCE ( struct scsi_device, process, scsidev_step );
970
971 /**
972  * Open SCSI device
973  *
974  * @v block             Block control interface
975  * @v scsi              SCSI control interface
976  * @v lun               SCSI LUN
977  * @ret rc              Return status code
978  */
979 int scsi_open ( struct interface *block, struct interface *scsi,
980                 struct scsi_lun *lun ) {
981         struct scsi_device *scsidev;
982
983         /* Allocate and initialise structure */
984         scsidev = zalloc ( sizeof ( *scsidev ) );
985         if ( ! scsidev )
986                 return -ENOMEM;
987         ref_init ( &scsidev->refcnt, NULL );
988         intf_init ( &scsidev->block, &scsidev_block_desc, &scsidev->refcnt );
989         intf_init ( &scsidev->scsi, &scsidev_scsi_desc, &scsidev->refcnt );
990         intf_init ( &scsidev->ready, &scsidev_ready_desc, &scsidev->refcnt );
991         process_init ( &scsidev->process, &scsidev_process_desc,
992                        &scsidev->refcnt );
993         INIT_LIST_HEAD ( &scsidev->cmds );
994         memcpy ( &scsidev->lun, lun, sizeof ( scsidev->lun ) );
995         DBGC ( scsidev, "SCSI %p created for LUN " SCSI_LUN_FORMAT "\n",
996                scsidev, SCSI_LUN_DATA ( scsidev->lun ) );
997
998         /* Attach to SCSI and parent interfaces, mortalise self, and return */
999         intf_plug_plug ( &scsidev->scsi, scsi );
1000         intf_plug_plug ( &scsidev->block, block );
1001         ref_put ( &scsidev->refcnt );
1002         return 0;
1003 }