iPXE
scsi.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stddef.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <byteswap.h>
00030 #include <errno.h>
00031 #include <ipxe/list.h>
00032 #include <ipxe/process.h>
00033 #include <ipxe/xfer.h>
00034 #include <ipxe/blockdev.h>
00035 #include <ipxe/scsi.h>
00036 
00037 /** @file
00038  *
00039  * SCSI block device
00040  *
00041  */
00042 
00043 /** Maximum number of TEST UNIT READY retries */
00044 #define SCSI_READY_MAX_RETRIES 10
00045 
00046 /* Error numbers generated by SCSI sense data */
00047 #define EIO_NO_SENSE __einfo_error ( EINFO_EIO_NO_SENSE )
00048 #define EINFO_EIO_NO_SENSE \
00049         __einfo_uniqify ( EINFO_EIO, 0x00, "No sense" )
00050 #define EIO_RECOVERED_ERROR __einfo_error ( EINFO_EIO_RECOVERED_ERROR )
00051 #define EINFO_EIO_RECOVERED_ERROR \
00052         __einfo_uniqify ( EINFO_EIO, 0x01, "Recovered error" )
00053 #define EIO_NOT_READY __einfo_error ( EINFO_EIO_NOT_READY )
00054 #define EINFO_EIO_NOT_READY \
00055         __einfo_uniqify ( EINFO_EIO, 0x02, "Not ready" )
00056 #define EIO_MEDIUM_ERROR __einfo_error ( EINFO_EIO_MEDIUM_ERROR )
00057 #define EINFO_EIO_MEDIUM_ERROR \
00058         __einfo_uniqify ( EINFO_EIO, 0x03, "Medium error" )
00059 #define EIO_HARDWARE_ERROR __einfo_error ( EINFO_EIO_HARDWARE_ERROR )
00060 #define EINFO_EIO_HARDWARE_ERROR \
00061         __einfo_uniqify ( EINFO_EIO, 0x04, "Hardware error" )
00062 #define EIO_ILLEGAL_REQUEST __einfo_error ( EINFO_EIO_ILLEGAL_REQUEST )
00063 #define EINFO_EIO_ILLEGAL_REQUEST \
00064         __einfo_uniqify ( EINFO_EIO, 0x05, "Illegal request" )
00065 #define EIO_UNIT_ATTENTION __einfo_error ( EINFO_EIO_UNIT_ATTENTION )
00066 #define EINFO_EIO_UNIT_ATTENTION \
00067         __einfo_uniqify ( EINFO_EIO, 0x06, "Unit attention" )
00068 #define EIO_DATA_PROTECT __einfo_error ( EINFO_EIO_DATA_PROTECT )
00069 #define EINFO_EIO_DATA_PROTECT \
00070         __einfo_uniqify ( EINFO_EIO, 0x07, "Data protect" )
00071 #define EIO_BLANK_CHECK __einfo_error ( EINFO_EIO_BLANK_CHECK )
00072 #define EINFO_EIO_BLANK_CHECK \
00073         __einfo_uniqify ( EINFO_EIO, 0x08, "Blank check" )
00074 #define EIO_VENDOR_SPECIFIC __einfo_error ( EINFO_EIO_VENDOR_SPECIFIC )
00075 #define EINFO_EIO_VENDOR_SPECIFIC \
00076         __einfo_uniqify ( EINFO_EIO, 0x09, "Vendor specific" )
00077 #define EIO_COPY_ABORTED __einfo_error ( EINFO_EIO_COPY_ABORTED )
00078 #define EINFO_EIO_COPY_ABORTED \
00079         __einfo_uniqify ( EINFO_EIO, 0x0a, "Copy aborted" )
00080 #define EIO_ABORTED_COMMAND __einfo_error ( EINFO_EIO_ABORTED_COMMAND )
00081 #define EINFO_EIO_ABORTED_COMMAND \
00082         __einfo_uniqify ( EINFO_EIO, 0x0b, "Aborted command" )
00083 #define EIO_RESERVED __einfo_error ( EINFO_EIO_RESERVED )
00084 #define EINFO_EIO_RESERVED \
00085         __einfo_uniqify ( EINFO_EIO, 0x0c, "Reserved" )
00086 #define EIO_VOLUME_OVERFLOW __einfo_error ( EINFO_EIO_VOLUME_OVERFLOW )
00087 #define EINFO_EIO_VOLUME_OVERFLOW \
00088         __einfo_uniqify ( EINFO_EIO, 0x0d, "Volume overflow" )
00089 #define EIO_MISCOMPARE __einfo_error ( EINFO_EIO_MISCOMPARE )
00090 #define EINFO_EIO_MISCOMPARE \
00091         __einfo_uniqify ( EINFO_EIO, 0x0e, "Miscompare" )
00092 #define EIO_COMPLETED __einfo_error ( EINFO_EIO_COMPLETED )
00093 #define EINFO_EIO_COMPLETED \
00094         __einfo_uniqify ( EINFO_EIO, 0x0f, "Completed" )
00095 #define EIO_SENSE( key )                                                \
00096         EUNIQ ( EINFO_EIO, (key), EIO_NO_SENSE, EIO_RECOVERED_ERROR,    \
00097                 EIO_NOT_READY, EIO_MEDIUM_ERROR, EIO_HARDWARE_ERROR,    \
00098                 EIO_ILLEGAL_REQUEST, EIO_UNIT_ATTENTION,                \
00099                 EIO_DATA_PROTECT, EIO_BLANK_CHECK, EIO_VENDOR_SPECIFIC, \
00100                 EIO_COPY_ABORTED, EIO_ABORTED_COMMAND, EIO_RESERVED,    \
00101                 EIO_VOLUME_OVERFLOW, EIO_MISCOMPARE, EIO_COMPLETED )
00102 
00103 /******************************************************************************
00104  *
00105  * Utility functions
00106  *
00107  ******************************************************************************
00108  */
00109 
00110 /**
00111  * Parse SCSI LUN
00112  *
00113  * @v lun_string        LUN string representation
00114  * @v lun               LUN to fill in
00115  * @ret rc              Return status code
00116  */
00117 int scsi_parse_lun ( const char *lun_string, struct scsi_lun *lun ) {
00118         char *p;
00119         int i;
00120 
00121         memset ( lun, 0, sizeof ( *lun ) );
00122         if ( lun_string ) {
00123                 p = ( char * ) lun_string;
00124                 for ( i = 0 ; i < 4 ; i++ ) {
00125                         lun->u16[i] = htons ( strtoul ( p, &p, 16 ) );
00126                         if ( *p == '\0' )
00127                                 break;
00128                         if ( *p != '-' )
00129                                 return -EINVAL;
00130                         p++;
00131                 }
00132                 if ( *p )
00133                         return -EINVAL;
00134         }
00135 
00136         return 0;
00137 }
00138 
00139 /**
00140  * Parse SCSI sense data
00141  *
00142  * @v data              Raw sense data
00143  * @v len               Length of raw sense data
00144  * @v sense             Descriptor-format sense data to fill in
00145  */
00146 void scsi_parse_sense ( const void *data, size_t len,
00147                         struct scsi_sns_descriptor *sense ) {
00148         const union scsi_sns *sns = data;
00149 
00150         /* Avoid returning uninitialised data */
00151         memset ( sense, 0, sizeof ( *sense ) );
00152 
00153         /* Copy, assuming descriptor-format data */
00154         if ( len < sizeof ( sns->desc ) )
00155                 return;
00156         memcpy ( sense, &sns->desc, sizeof ( *sense ) );
00157 
00158         /* Convert fixed-format to descriptor-format, if applicable */
00159         if ( len < sizeof ( sns->fixed ) )
00160                 return;
00161         if ( ! SCSI_SENSE_FIXED ( sns->code ) )
00162                 return;
00163         sense->additional = sns->fixed.additional;
00164 }
00165 
00166 /******************************************************************************
00167  *
00168  * Interface methods
00169  *
00170  ******************************************************************************
00171  */
00172 
00173 /**
00174  * Issue SCSI command
00175  *
00176  * @v control           SCSI control interface
00177  * @v data              SCSI data interface
00178  * @v command           SCSI command
00179  * @ret tag             Command tag, or negative error
00180  */
00181 int scsi_command ( struct interface *control, struct interface *data,
00182                    struct scsi_cmd *command ) {
00183         struct interface *dest;
00184         scsi_command_TYPE ( void * ) *op =
00185                 intf_get_dest_op ( control, scsi_command, &dest );
00186         void *object = intf_object ( dest );
00187         int tap;
00188 
00189         if ( op ) {
00190                 tap = op ( object, data, command );
00191         } else {
00192                 /* Default is to fail to issue the command */
00193                 tap = -EOPNOTSUPP;
00194         }
00195 
00196         intf_put ( dest );
00197         return tap;
00198 }
00199 
00200 /**
00201  * Report SCSI response
00202  *
00203  * @v interface         SCSI command interface
00204  * @v response          SCSI response
00205  */
00206 void scsi_response ( struct interface *intf, struct scsi_rsp *response ) {
00207         struct interface *dest;
00208         scsi_response_TYPE ( void * ) *op =
00209                 intf_get_dest_op ( intf, scsi_response, &dest );
00210         void *object = intf_object ( dest );
00211 
00212         if ( op ) {
00213                 op ( object, response );
00214         } else {
00215                 /* Default is to ignore the response */
00216         }
00217 
00218         intf_put ( dest );
00219 }
00220 
00221 /******************************************************************************
00222  *
00223  * SCSI devices and commands
00224  *
00225  ******************************************************************************
00226  */
00227 
00228 /** A SCSI device */
00229 struct scsi_device {
00230         /** Reference count */
00231         struct refcnt refcnt;
00232         /** Block control interface */
00233         struct interface block;
00234         /** SCSI control interface */
00235         struct interface scsi;
00236 
00237         /** SCSI LUN */
00238         struct scsi_lun lun;
00239         /** Flags */
00240         unsigned int flags;
00241 
00242         /** TEST UNIT READY interface */
00243         struct interface ready;
00244         /** TEST UNIT READY process */
00245         struct process process;
00246         /** TEST UNIT READY retry count */
00247         unsigned int retries;
00248 
00249         /** List of commands */
00250         struct list_head cmds;
00251 };
00252 
00253 /** SCSI device flags */
00254 enum scsi_device_flags {
00255         /** TEST UNIT READY has been issued */
00256         SCSIDEV_UNIT_TESTED = 0x0001,
00257         /** TEST UNIT READY has completed successfully */
00258         SCSIDEV_UNIT_READY = 0x0002,
00259 };
00260 
00261 /** A SCSI command */
00262 struct scsi_command {
00263         /** Reference count */
00264         struct refcnt refcnt;
00265         /** SCSI device */
00266         struct scsi_device *scsidev;
00267         /** List of SCSI commands */
00268         struct list_head list;
00269 
00270         /** Block data interface */
00271         struct interface block;
00272         /** SCSI data interface */
00273         struct interface scsi;
00274 
00275         /** Command type */
00276         struct scsi_command_type *type;
00277         /** Starting logical block address */
00278         uint64_t lba;
00279         /** Number of blocks */
00280         unsigned int count;
00281         /** Data buffer */
00282         userptr_t buffer;
00283         /** Length of data buffer */
00284         size_t len;
00285         /** Command tag */
00286         uint32_t tag;
00287 
00288         /** Private data */
00289         uint8_t priv[0];
00290 };
00291 
00292 /** A SCSI command type */
00293 struct scsi_command_type {
00294         /** Name */
00295         const char *name;
00296         /** Additional working space */
00297         size_t priv_len;
00298         /**
00299          * Construct SCSI command IU
00300          *
00301          * @v scsicmd           SCSI command
00302          * @v command           SCSI command IU
00303          */
00304         void ( * cmd ) ( struct scsi_command *scsicmd,
00305                          struct scsi_cmd *command );
00306         /**
00307          * Handle SCSI command completion
00308          *
00309          * @v scsicmd           SCSI command
00310          * @v rc                Reason for completion
00311          */
00312         void ( * done ) ( struct scsi_command *scsicmd, int rc );
00313 };
00314 
00315 /**
00316  * Get reference to SCSI device
00317  *
00318  * @v scsidev           SCSI device
00319  * @ret scsidev         SCSI device
00320  */
00321 static inline __attribute__ (( always_inline )) struct scsi_device *
00322 scsidev_get ( struct scsi_device *scsidev ) {
00323         ref_get ( &scsidev->refcnt );
00324         return scsidev;
00325 }
00326 
00327 /**
00328  * Drop reference to SCSI device
00329  *
00330  * @v scsidev           SCSI device
00331  */
00332 static inline __attribute__ (( always_inline )) void
00333 scsidev_put ( struct scsi_device *scsidev ) {
00334         ref_put ( &scsidev->refcnt );
00335 }
00336 
00337 /**
00338  * Get reference to SCSI command
00339  *
00340  * @v scsicmd           SCSI command
00341  * @ret scsicmd         SCSI command
00342  */
00343 static inline __attribute__ (( always_inline )) struct scsi_command *
00344 scsicmd_get ( struct scsi_command *scsicmd ) {
00345         ref_get ( &scsicmd->refcnt );
00346         return scsicmd;
00347 }
00348 
00349 /**
00350  * Drop reference to SCSI command
00351  *
00352  * @v scsicmd           SCSI command
00353  */
00354 static inline __attribute__ (( always_inline )) void
00355 scsicmd_put ( struct scsi_command *scsicmd ) {
00356         ref_put ( &scsicmd->refcnt );
00357 }
00358 
00359 /**
00360  * Get SCSI command private data
00361  *
00362  * @v scsicmd           SCSI command
00363  * @ret priv            Private data
00364  */
00365 static inline __attribute__ (( always_inline )) void *
00366 scsicmd_priv ( struct scsi_command *scsicmd ) {
00367         return scsicmd->priv;
00368 }
00369 
00370 /**
00371  * Free SCSI command
00372  *
00373  * @v refcnt            Reference count
00374  */
00375 static void scsicmd_free ( struct refcnt *refcnt ) {
00376         struct scsi_command *scsicmd =
00377                 container_of ( refcnt, struct scsi_command, refcnt );
00378 
00379         /* Drop reference to SCSI device */
00380         scsidev_put ( scsicmd->scsidev );
00381 
00382         /* Free command */
00383         free ( scsicmd );
00384 }
00385 
00386 /**
00387  * Close SCSI command
00388  *
00389  * @v scsicmd           SCSI command
00390  * @v rc                Reason for close
00391  */
00392 static void scsicmd_close ( struct scsi_command *scsicmd, int rc ) {
00393         struct scsi_device *scsidev = scsicmd->scsidev;
00394 
00395         if ( rc != 0 ) {
00396                 DBGC ( scsidev, "SCSI %p tag %08x closed: %s\n",
00397                        scsidev, scsicmd->tag, strerror ( rc ) );
00398         }
00399 
00400         /* Remove from list of commands */
00401         list_del ( &scsicmd->list );
00402 
00403         /* Shut down interfaces */
00404         intfs_shutdown ( rc, &scsicmd->scsi, &scsicmd->block, NULL );
00405 
00406         /* Drop list's reference */
00407         scsicmd_put ( scsicmd );
00408 }
00409 
00410 /**
00411  * Construct and issue SCSI command
00412  *
00413  * @ret rc              Return status code
00414  */
00415 static int scsicmd_command ( struct scsi_command *scsicmd ) {
00416         struct scsi_device *scsidev = scsicmd->scsidev;
00417         struct scsi_cmd command;
00418         int tag;
00419         int rc;
00420 
00421         /* Construct command */
00422         memset ( &command, 0, sizeof ( command ) );
00423         memcpy ( &command.lun, &scsidev->lun, sizeof ( command.lun ) );
00424         scsicmd->type->cmd ( scsicmd, &command );
00425 
00426         /* Issue command */
00427         if ( ( tag = scsi_command ( &scsidev->scsi, &scsicmd->scsi,
00428                                     &command ) ) < 0 ) {
00429                 rc = tag;
00430                 DBGC ( scsidev, "SCSI %p could not issue command: %s\n",
00431                        scsidev, strerror ( rc ) );
00432                 return rc;
00433         }
00434 
00435         /* Record tag */
00436         if ( scsicmd->tag ) {
00437                 DBGC ( scsidev, "SCSI %p tag %08x is now tag %08x\n",
00438                        scsidev, scsicmd->tag, tag );
00439         }
00440         scsicmd->tag = tag;
00441         DBGC2 ( scsidev, "SCSI %p tag %08x %s " SCSI_CDB_FORMAT "\n",
00442                 scsidev, scsicmd->tag, scsicmd->type->name,
00443                 SCSI_CDB_DATA ( command.cdb ) );
00444 
00445         return 0;
00446 }
00447 
00448 /**
00449  * Handle SCSI command completion
00450  *
00451  * @v scsicmd           SCSI command
00452  * @v rc                Reason for close
00453  */
00454 static void scsicmd_done ( struct scsi_command *scsicmd, int rc ) {
00455 
00456         /* Restart SCSI interface */
00457         intf_restart ( &scsicmd->scsi, rc );
00458 
00459         /* Hand over to the command completion handler */
00460         scsicmd->type->done ( scsicmd, rc );
00461 }
00462 
00463 /**
00464  * Handle SCSI response
00465  *
00466  * @v scsicmd           SCSI command
00467  * @v response          SCSI response
00468  */
00469 static void scsicmd_response ( struct scsi_command *scsicmd,
00470                                struct scsi_rsp *response ) {
00471         struct scsi_device *scsidev = scsicmd->scsidev;
00472         size_t overrun;
00473         size_t underrun;
00474         int rc;
00475 
00476         if ( response->status == 0 ) {
00477                 scsicmd_done ( scsicmd, 0 );
00478         } else {
00479                 DBGC ( scsidev, "SCSI %p tag %08x status %02x",
00480                        scsidev, scsicmd->tag, response->status );
00481                 if ( response->overrun > 0 ) {
00482                         overrun = response->overrun;
00483                         DBGC ( scsidev, " overrun +%zd", overrun );
00484                 } else if ( response->overrun < 0 ) {
00485                         underrun = -(response->overrun);
00486                         DBGC ( scsidev, " underrun -%zd", underrun );
00487                 }
00488                 DBGC ( scsidev, " sense %02x key %02x additional %04x\n",
00489                        ( response->sense.code & SCSI_SENSE_CODE_MASK ),
00490                        ( response->sense.key & SCSI_SENSE_KEY_MASK ),
00491                        ntohs ( response->sense.additional ) );
00492 
00493                 /* Construct error number from sense data */
00494                 rc = -EIO_SENSE ( response->sense.key & SCSI_SENSE_KEY_MASK );
00495                 scsicmd_done ( scsicmd, rc );
00496         }
00497 }
00498 
00499 /**
00500  * Construct SCSI READ command
00501  *
00502  * @v scsicmd           SCSI command
00503  * @v command           SCSI command IU
00504  */
00505 static void scsicmd_read_cmd ( struct scsi_command *scsicmd,
00506                                struct scsi_cmd *command ) {
00507 
00508         if ( ( scsicmd->lba + scsicmd->count ) > SCSI_MAX_BLOCK_10 ) {
00509                 /* Use READ (16) */
00510                 command->cdb.read16.opcode = SCSI_OPCODE_READ_16;
00511                 command->cdb.read16.lba = cpu_to_be64 ( scsicmd->lba );
00512                 command->cdb.read16.len = cpu_to_be32 ( scsicmd->count );
00513         } else {
00514                 /* Use READ (10) */
00515                 command->cdb.read10.opcode = SCSI_OPCODE_READ_10;
00516                 command->cdb.read10.lba = cpu_to_be32 ( scsicmd->lba );
00517                 command->cdb.read10.len = cpu_to_be16 ( scsicmd->count );
00518         }
00519         command->data_in = scsicmd->buffer;
00520         command->data_in_len = scsicmd->len;
00521 }
00522 
00523 /** SCSI READ command type */
00524 static struct scsi_command_type scsicmd_read = {
00525         .name = "READ",
00526         .cmd = scsicmd_read_cmd,
00527         .done = scsicmd_close,
00528 };
00529 
00530 /**
00531  * Construct SCSI WRITE command
00532  *
00533  * @v scsicmd           SCSI command
00534  * @v command           SCSI command IU
00535  */
00536 static void scsicmd_write_cmd ( struct scsi_command *scsicmd,
00537                                 struct scsi_cmd *command ) {
00538 
00539         if ( ( scsicmd->lba + scsicmd->count ) > SCSI_MAX_BLOCK_10 ) {
00540                 /* Use WRITE (16) */
00541                 command->cdb.write16.opcode = SCSI_OPCODE_WRITE_16;
00542                 command->cdb.write16.lba = cpu_to_be64 ( scsicmd->lba );
00543                 command->cdb.write16.len = cpu_to_be32 ( scsicmd->count );
00544         } else {
00545                 /* Use WRITE (10) */
00546                 command->cdb.write10.opcode = SCSI_OPCODE_WRITE_10;
00547                 command->cdb.write10.lba = cpu_to_be32 ( scsicmd->lba );
00548                 command->cdb.write10.len = cpu_to_be16 ( scsicmd->count );
00549         }
00550         command->data_out = scsicmd->buffer;
00551         command->data_out_len = scsicmd->len;
00552 }
00553 
00554 /** SCSI WRITE command type */
00555 static struct scsi_command_type scsicmd_write = {
00556         .name = "WRITE",
00557         .cmd = scsicmd_write_cmd,
00558         .done = scsicmd_close,
00559 };
00560 
00561 /** SCSI READ CAPACITY private data */
00562 struct scsi_read_capacity_private {
00563         /** Use READ CAPACITY (16) */
00564         int use16;
00565         /** Data buffer for READ CAPACITY commands */
00566         union {
00567                 /** Data buffer for READ CAPACITY (10) */
00568                 struct scsi_capacity_10 capacity10;
00569                 /** Data buffer for READ CAPACITY (16) */
00570                 struct scsi_capacity_16 capacity16;
00571         } capacity;
00572 };
00573 
00574 /**
00575  * Construct SCSI READ CAPACITY command
00576  *
00577  * @v scsicmd           SCSI command
00578  * @v command           SCSI command IU
00579  */
00580 static void scsicmd_read_capacity_cmd ( struct scsi_command *scsicmd,
00581                                         struct scsi_cmd *command ) {
00582         struct scsi_read_capacity_private *priv = scsicmd_priv ( scsicmd );
00583         struct scsi_cdb_read_capacity_16 *readcap16 = &command->cdb.readcap16;
00584         struct scsi_cdb_read_capacity_10 *readcap10 = &command->cdb.readcap10;
00585         struct scsi_capacity_16 *capacity16 = &priv->capacity.capacity16;
00586         struct scsi_capacity_10 *capacity10 = &priv->capacity.capacity10;
00587 
00588         if ( priv->use16 ) {
00589                 /* Use READ CAPACITY (16) */
00590                 readcap16->opcode = SCSI_OPCODE_SERVICE_ACTION_IN;
00591                 readcap16->service_action =
00592                         SCSI_SERVICE_ACTION_READ_CAPACITY_16;
00593                 readcap16->len = cpu_to_be32 ( sizeof ( *capacity16 ) );
00594                 command->data_in = virt_to_user ( capacity16 );
00595                 command->data_in_len = sizeof ( *capacity16 );
00596         } else {
00597                 /* Use READ CAPACITY (10) */
00598                 readcap10->opcode = SCSI_OPCODE_READ_CAPACITY_10;
00599                 command->data_in = virt_to_user ( capacity10 );
00600                 command->data_in_len = sizeof ( *capacity10 );
00601         }
00602 }
00603 
00604 /**
00605  * Handle SCSI READ CAPACITY command completion
00606  *
00607  * @v scsicmd           SCSI command
00608  * @v rc                Reason for completion
00609  */
00610 static void scsicmd_read_capacity_done ( struct scsi_command *scsicmd,
00611                                          int rc ) {
00612         struct scsi_read_capacity_private *priv = scsicmd_priv ( scsicmd );
00613         struct scsi_capacity_16 *capacity16 = &priv->capacity.capacity16;
00614         struct scsi_capacity_10 *capacity10 = &priv->capacity.capacity10;
00615         struct block_device_capacity capacity;
00616 
00617         /* Close if command failed */
00618         if ( rc != 0 ) {
00619                 scsicmd_close ( scsicmd, rc );
00620                 return;
00621         }
00622 
00623         /* Extract capacity */
00624         if ( priv->use16 ) {
00625                 capacity.blocks = ( be64_to_cpu ( capacity16->lba ) + 1 );
00626                 capacity.blksize = be32_to_cpu ( capacity16->blksize );
00627         } else {
00628                 capacity.blocks = ( be32_to_cpu ( capacity10->lba ) + 1 );
00629                 capacity.blksize = be32_to_cpu ( capacity10->blksize );
00630 
00631                 /* If capacity range was exceeded (i.e. capacity.lba
00632                  * was 0xffffffff, meaning that blockdev->blocks is
00633                  * now zero), use READ CAPACITY (16) instead.  READ
00634                  * CAPACITY (16) is not mandatory, so we can't just
00635                  * use it straight off.
00636                  */
00637                 if ( capacity.blocks == 0 ) {
00638                         priv->use16 = 1;
00639                         if ( ( rc = scsicmd_command ( scsicmd ) ) != 0 ) {
00640                                 scsicmd_close ( scsicmd, rc );
00641                                 return;
00642                         }
00643                         return;
00644                 }
00645         }
00646         capacity.max_count = -1U;
00647 
00648         /* Return capacity to caller */
00649         block_capacity ( &scsicmd->block, &capacity );
00650 
00651         /* Close command */
00652         scsicmd_close ( scsicmd, 0 );
00653 }
00654 
00655 /** SCSI READ CAPACITY command type */
00656 static struct scsi_command_type scsicmd_read_capacity = {
00657         .name = "READ CAPACITY",
00658         .priv_len = sizeof ( struct scsi_read_capacity_private ),
00659         .cmd = scsicmd_read_capacity_cmd,
00660         .done = scsicmd_read_capacity_done,
00661 };
00662 
00663 /**
00664  * Construct SCSI TEST UNIT READY command
00665  *
00666  * @v scsicmd           SCSI command
00667  * @v command           SCSI command IU
00668  */
00669 static void scsicmd_test_unit_ready_cmd ( struct scsi_command *scsicmd __unused,
00670                                           struct scsi_cmd *command ) {
00671         struct scsi_cdb_test_unit_ready *testready = &command->cdb.testready;
00672 
00673         testready->opcode = SCSI_OPCODE_TEST_UNIT_READY;
00674 }
00675 
00676 /** SCSI TEST UNIT READY command type */
00677 static struct scsi_command_type scsicmd_test_unit_ready = {
00678         .name = "TEST UNIT READY",
00679         .cmd = scsicmd_test_unit_ready_cmd,
00680         .done = scsicmd_close,
00681 };
00682 
00683 /** SCSI command block interface operations */
00684 static struct interface_operation scsicmd_block_op[] = {
00685         INTF_OP ( intf_close, struct scsi_command *, scsicmd_close ),
00686 };
00687 
00688 /** SCSI command block interface descriptor */
00689 static struct interface_descriptor scsicmd_block_desc =
00690         INTF_DESC_PASSTHRU ( struct scsi_command, block,
00691                              scsicmd_block_op, scsi );
00692 
00693 /** SCSI command SCSI interface operations */
00694 static struct interface_operation scsicmd_scsi_op[] = {
00695         INTF_OP ( intf_close, struct scsi_command *, scsicmd_done ),
00696         INTF_OP ( scsi_response, struct scsi_command *, scsicmd_response ),
00697 };
00698 
00699 /** SCSI command SCSI interface descriptor */
00700 static struct interface_descriptor scsicmd_scsi_desc =
00701         INTF_DESC_PASSTHRU ( struct scsi_command, scsi,
00702                              scsicmd_scsi_op, block );
00703 
00704 /**
00705  * Create SCSI command
00706  *
00707  * @v scsidev           SCSI device
00708  * @v block             Block data interface
00709  * @v type              SCSI command type
00710  * @v lba               Starting logical block address
00711  * @v count             Number of blocks to transfer
00712  * @v buffer            Data buffer
00713  * @v len               Length of data buffer
00714  * @ret rc              Return status code
00715  */
00716 static int scsidev_command ( struct scsi_device *scsidev,
00717                              struct interface *block,
00718                              struct scsi_command_type *type,
00719                              uint64_t lba, unsigned int count,
00720                              userptr_t buffer, size_t len ) {
00721         struct scsi_command *scsicmd;
00722         int rc;
00723 
00724         /* Allocate and initialise structure */
00725         scsicmd = zalloc ( sizeof ( *scsicmd ) + type->priv_len );
00726         if ( ! scsicmd ) {
00727                 rc = -ENOMEM;
00728                 goto err_zalloc;
00729         }
00730         ref_init ( &scsicmd->refcnt, scsicmd_free );
00731         intf_init ( &scsicmd->block, &scsicmd_block_desc, &scsicmd->refcnt );
00732         intf_init ( &scsicmd->scsi, &scsicmd_scsi_desc,
00733                     &scsicmd->refcnt );
00734         scsicmd->scsidev = scsidev_get ( scsidev );
00735         list_add ( &scsicmd->list, &scsidev->cmds );
00736         scsicmd->type = type;
00737         scsicmd->lba = lba;
00738         scsicmd->count = count;
00739         scsicmd->buffer = buffer;
00740         scsicmd->len = len;
00741 
00742         /* Issue SCSI command */
00743         if ( ( rc = scsicmd_command ( scsicmd ) ) != 0 )
00744                 goto err_command;
00745 
00746         /* Attach to parent interface, transfer reference to list, and return */
00747         intf_plug_plug ( &scsicmd->block, block );
00748         return 0;
00749 
00750  err_command:
00751         scsicmd_close ( scsicmd, rc );
00752         ref_put ( &scsicmd->refcnt );
00753  err_zalloc:
00754         return rc;
00755 }
00756 
00757 /**
00758  * Issue SCSI block read
00759  *
00760  * @v scsidev           SCSI device
00761  * @v block             Block data interface
00762  * @v lba               Starting logical block address
00763  * @v count             Number of blocks to transfer
00764  * @v buffer            Data buffer
00765  * @v len               Length of data buffer
00766  * @ret rc              Return status code
00767 
00768  */
00769 static int scsidev_read ( struct scsi_device *scsidev,
00770                           struct interface *block,
00771                           uint64_t lba, unsigned int count,
00772                           userptr_t buffer, size_t len ) {
00773         return scsidev_command ( scsidev, block, &scsicmd_read,
00774                                  lba, count, buffer, len );
00775 }
00776 
00777 /**
00778  * Issue SCSI block write
00779  *
00780  * @v scsidev           SCSI device
00781  * @v block             Block data interface
00782  * @v lba               Starting logical block address
00783  * @v count             Number of blocks to transfer
00784  * @v buffer            Data buffer
00785  * @v len               Length of data buffer
00786  * @ret rc              Return status code
00787  */
00788 static int scsidev_write ( struct scsi_device *scsidev,
00789                            struct interface *block,
00790                            uint64_t lba, unsigned int count,
00791                            userptr_t buffer, size_t len ) {
00792         return scsidev_command ( scsidev, block, &scsicmd_write,
00793                                  lba, count, buffer, len );
00794 }
00795 
00796 /**
00797  * Read SCSI device capacity
00798  *
00799  * @v scsidev           SCSI device
00800  * @v block             Block data interface
00801  * @ret rc              Return status code
00802  */
00803 static int scsidev_read_capacity ( struct scsi_device *scsidev,
00804                                    struct interface *block ) {
00805         return scsidev_command ( scsidev, block, &scsicmd_read_capacity,
00806                                  0, 0, UNULL, 0 );
00807 }
00808 
00809 /**
00810  * Test to see if SCSI device is ready
00811  *
00812  * @v scsidev           SCSI device
00813  * @v block             Block data interface
00814  * @ret rc              Return status code
00815  */
00816 static int scsidev_test_unit_ready ( struct scsi_device *scsidev,
00817                                      struct interface *block ) {
00818         return scsidev_command ( scsidev, block, &scsicmd_test_unit_ready,
00819                                  0, 0, UNULL, 0 );
00820 }
00821 
00822 /**
00823  * Check SCSI device flow-control window
00824  *
00825  * @v scsidev           SCSI device
00826  * @ret len             Length of window
00827  */
00828 static size_t scsidev_window ( struct scsi_device *scsidev ) {
00829 
00830         /* Refuse commands until unit is confirmed ready */
00831         if ( ! ( scsidev->flags & SCSIDEV_UNIT_READY ) )
00832                 return 0;
00833 
00834         return xfer_window ( &scsidev->scsi );
00835 }
00836 
00837 /**
00838  * Close SCSI device
00839  *
00840  * @v scsidev           SCSI device
00841  * @v rc                Reason for close
00842  */
00843 static void scsidev_close ( struct scsi_device *scsidev, int rc ) {
00844         struct scsi_command *scsicmd;
00845         struct scsi_command *tmp;
00846 
00847         /* Stop process */
00848         process_del ( &scsidev->process );
00849 
00850         /* Shut down interfaces */
00851         intfs_shutdown ( rc, &scsidev->block, &scsidev->scsi, &scsidev->ready,
00852                          NULL );
00853 
00854         /* Shut down any remaining commands */
00855         list_for_each_entry_safe ( scsicmd, tmp, &scsidev->cmds, list )
00856                 scsicmd_close ( scsicmd, rc );
00857 }
00858 
00859 /** SCSI device block interface operations */
00860 static struct interface_operation scsidev_block_op[] = {
00861         INTF_OP ( xfer_window, struct scsi_device *, scsidev_window ),
00862         INTF_OP ( block_read, struct scsi_device *, scsidev_read ),
00863         INTF_OP ( block_write, struct scsi_device *, scsidev_write ),
00864         INTF_OP ( block_read_capacity, struct scsi_device *,
00865                   scsidev_read_capacity ),
00866         INTF_OP ( intf_close, struct scsi_device *, scsidev_close ),
00867 };
00868 
00869 /** SCSI device block interface descriptor */
00870 static struct interface_descriptor scsidev_block_desc =
00871         INTF_DESC_PASSTHRU ( struct scsi_device, block,
00872                              scsidev_block_op, scsi );
00873 
00874 /**
00875  * Handle SCSI TEST UNIT READY response
00876  *
00877  * @v scsidev           SCSI device
00878  * @v rc                Reason for close
00879  */
00880 static void scsidev_ready ( struct scsi_device *scsidev, int rc ) {
00881 
00882         /* Shut down interface */
00883         intf_restart ( &scsidev->ready, rc );
00884 
00885         /* Mark device as ready, if applicable */
00886         if ( rc == 0 ) {
00887                 DBGC ( scsidev, "SCSI %p unit is ready\n", scsidev );
00888                 scsidev->flags |= SCSIDEV_UNIT_READY;
00889                 xfer_window_changed ( &scsidev->block );
00890                 return;
00891         }
00892         DBGC ( scsidev, "SCSI %p not ready: %s\n", scsidev, strerror ( rc ) );
00893 
00894         /* SCSI targets have an annoying habit of returning occasional
00895          * pointless "error" messages such as "power-on occurred", so
00896          * we have to be prepared to retry commands.
00897          *
00898          * For most commands, we rely on the caller (e.g. the generic
00899          * SAN device layer) to retry commands as needed.  However, a
00900          * TEST UNIT READY failure is used as an indication that the
00901          * whole SCSI device is unavailable and should be closed.  We
00902          * must therefore perform this retry loop within the SCSI
00903          * layer.
00904          */
00905         if ( scsidev->retries++ < SCSI_READY_MAX_RETRIES ) {
00906                 DBGC ( scsidev, "SCSI %p retrying (retry %d)\n",
00907                        scsidev, scsidev->retries );
00908                 scsidev->flags &= ~SCSIDEV_UNIT_TESTED;
00909                 process_add ( &scsidev->process );
00910                 return;
00911         }
00912 
00913         /* Close device */
00914         DBGC ( scsidev, "SCSI %p never became ready: %s\n",
00915                scsidev, strerror ( rc ) );
00916         scsidev_close ( scsidev, rc );
00917 }
00918 
00919 /** SCSI device TEST UNIT READY interface operations */
00920 static struct interface_operation scsidev_ready_op[] = {
00921         INTF_OP ( intf_close, struct scsi_device *, scsidev_ready ),
00922 };
00923 
00924 /** SCSI device TEST UNIT READY interface descriptor */
00925 static struct interface_descriptor scsidev_ready_desc =
00926         INTF_DESC ( struct scsi_device, ready, scsidev_ready_op );
00927 
00928 /**
00929  * SCSI TEST UNIT READY process
00930  *
00931  * @v scsidev           SCSI device
00932  */
00933 static void scsidev_step ( struct scsi_device *scsidev ) {
00934         int rc;
00935 
00936         /* Do nothing if we have already issued TEST UNIT READY */
00937         if ( scsidev->flags & SCSIDEV_UNIT_TESTED )
00938                 return;
00939 
00940         /* Wait until underlying SCSI device is ready */
00941         if ( xfer_window ( &scsidev->scsi ) == 0 )
00942                 return;
00943 
00944         DBGC ( scsidev, "SCSI %p waiting for unit to become ready\n",
00945                scsidev );
00946 
00947         /* Mark TEST UNIT READY as sent */
00948         scsidev->flags |= SCSIDEV_UNIT_TESTED;
00949 
00950         /* Issue TEST UNIT READY command */
00951         if ( ( rc = scsidev_test_unit_ready ( scsidev, &scsidev->ready )) !=0){
00952                 scsidev_close ( scsidev, rc );
00953                 return;
00954         }
00955 }
00956 
00957 /** SCSI device SCSI interface operations */
00958 static struct interface_operation scsidev_scsi_op[] = {
00959         INTF_OP ( xfer_window_changed, struct scsi_device *, scsidev_step ),
00960         INTF_OP ( intf_close, struct scsi_device *, scsidev_close ),
00961 };
00962 
00963 /** SCSI device SCSI interface descriptor */
00964 static struct interface_descriptor scsidev_scsi_desc =
00965         INTF_DESC_PASSTHRU ( struct scsi_device, scsi,
00966                              scsidev_scsi_op, block );
00967 
00968 /** SCSI device process descriptor */
00969 static struct process_descriptor scsidev_process_desc =
00970         PROC_DESC_ONCE ( struct scsi_device, process, scsidev_step );
00971 
00972 /**
00973  * Open SCSI device
00974  *
00975  * @v block             Block control interface
00976  * @v scsi              SCSI control interface
00977  * @v lun               SCSI LUN
00978  * @ret rc              Return status code
00979  */
00980 int scsi_open ( struct interface *block, struct interface *scsi,
00981                 struct scsi_lun *lun ) {
00982         struct scsi_device *scsidev;
00983 
00984         /* Allocate and initialise structure */
00985         scsidev = zalloc ( sizeof ( *scsidev ) );
00986         if ( ! scsidev )
00987                 return -ENOMEM;
00988         ref_init ( &scsidev->refcnt, NULL );
00989         intf_init ( &scsidev->block, &scsidev_block_desc, &scsidev->refcnt );
00990         intf_init ( &scsidev->scsi, &scsidev_scsi_desc, &scsidev->refcnt );
00991         intf_init ( &scsidev->ready, &scsidev_ready_desc, &scsidev->refcnt );
00992         process_init ( &scsidev->process, &scsidev_process_desc,
00993                        &scsidev->refcnt );
00994         INIT_LIST_HEAD ( &scsidev->cmds );
00995         memcpy ( &scsidev->lun, lun, sizeof ( scsidev->lun ) );
00996         DBGC ( scsidev, "SCSI %p created for LUN " SCSI_LUN_FORMAT "\n",
00997                scsidev, SCSI_LUN_DATA ( scsidev->lun ) );
00998 
00999         /* Attach to SCSI and parent interfaces, mortalise self, and return */
01000         intf_plug_plug ( &scsidev->scsi, scsi );
01001         intf_plug_plug ( &scsidev->block, block );
01002         ref_put ( &scsidev->refcnt );
01003         return 0;
01004 }