Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  *  Copyright (C) 2002 Russell King
0004  *
0005  *  Commonly used functions by the ARM SCSI-II drivers.
0006  */
0007 
0008 #include <linux/scatterlist.h>
0009 
0010 #define BELT_AND_BRACES
0011 
0012 struct arm_cmd_priv {
0013     struct scsi_pointer scsi_pointer;
0014 };
0015 
0016 static inline struct scsi_pointer *arm_scsi_pointer(struct scsi_cmnd *cmd)
0017 {
0018     struct arm_cmd_priv *acmd = scsi_cmd_priv(cmd);
0019 
0020     return &acmd->scsi_pointer;
0021 }
0022 
0023 /*
0024  * The scatter-gather list handling.  This contains all
0025  * the yucky stuff that needs to be fixed properly.
0026  */
0027 
0028 /*
0029  * copy_SCp_to_sg() Assumes contiguous allocation at @sg of at-most @max
0030  * entries of uninitialized memory. SCp is from scsi-ml and has a valid
0031  * (possibly chained) sg-list
0032  */
0033 static inline int copy_SCp_to_sg(struct scatterlist *sg, struct scsi_pointer *SCp, int max)
0034 {
0035     int bufs = SCp->buffers_residual;
0036 
0037     /* FIXME: It should be easy for drivers to loop on copy_SCp_to_sg().
0038      * and to remove this BUG_ON. Use min() in-its-place
0039      */
0040     BUG_ON(bufs + 1 > max);
0041 
0042     sg_set_buf(sg, SCp->ptr, SCp->this_residual);
0043 
0044     if (bufs) {
0045         struct scatterlist *src_sg;
0046         unsigned i;
0047 
0048         for_each_sg(sg_next(SCp->buffer), src_sg, bufs, i)
0049             *(++sg) = *src_sg;
0050         sg_mark_end(sg);
0051     }
0052 
0053     return bufs + 1;
0054 }
0055 
0056 static inline int next_SCp(struct scsi_pointer *SCp)
0057 {
0058     int ret = SCp->buffers_residual;
0059     if (ret) {
0060         SCp->buffer = sg_next(SCp->buffer);
0061         SCp->buffers_residual--;
0062         SCp->ptr = sg_virt(SCp->buffer);
0063         SCp->this_residual = SCp->buffer->length;
0064     } else {
0065         SCp->ptr = NULL;
0066         SCp->this_residual = 0;
0067     }
0068     return ret;
0069 }
0070 
0071 static inline unsigned char get_next_SCp_byte(struct scsi_pointer *SCp)
0072 {
0073     char c = *SCp->ptr;
0074 
0075     SCp->ptr += 1;
0076     SCp->this_residual -= 1;
0077 
0078     return c;
0079 }
0080 
0081 static inline void put_next_SCp_byte(struct scsi_pointer *SCp, unsigned char c)
0082 {
0083     *SCp->ptr = c;
0084     SCp->ptr += 1;
0085     SCp->this_residual -= 1;
0086 }
0087 
0088 static inline void init_SCp(struct scsi_cmnd *SCpnt)
0089 {
0090     struct scsi_pointer *scsi_pointer = arm_scsi_pointer(SCpnt);
0091 
0092     memset(scsi_pointer, 0, sizeof(struct scsi_pointer));
0093 
0094     if (scsi_bufflen(SCpnt)) {
0095         unsigned long len = 0;
0096 
0097         scsi_pointer->buffer = scsi_sglist(SCpnt);
0098         scsi_pointer->buffers_residual = scsi_sg_count(SCpnt) - 1;
0099         scsi_pointer->ptr = sg_virt(scsi_pointer->buffer);
0100         scsi_pointer->this_residual = scsi_pointer->buffer->length;
0101         scsi_pointer->phase = scsi_bufflen(SCpnt);
0102 
0103 #ifdef BELT_AND_BRACES
0104         {   /*
0105              * Calculate correct buffer length.  Some commands
0106              * come in with the wrong scsi_bufflen.
0107              */
0108             struct scatterlist *sg;
0109             unsigned i, sg_count = scsi_sg_count(SCpnt);
0110 
0111             scsi_for_each_sg(SCpnt, sg, sg_count, i)
0112                 len += sg->length;
0113 
0114             if (scsi_bufflen(SCpnt) != len) {
0115                 printk(KERN_WARNING
0116                        "scsi%d.%c: bad request buffer "
0117                        "length %d, should be %ld\n",
0118                     SCpnt->device->host->host_no,
0119                     '0' + SCpnt->device->id,
0120                     scsi_bufflen(SCpnt), len);
0121                 /*
0122                  * FIXME: Totaly naive fixup. We should abort
0123                  * with error
0124                  */
0125                 scsi_pointer->phase =
0126                     min_t(unsigned long, len,
0127                           scsi_bufflen(SCpnt));
0128             }
0129         }
0130 #endif
0131     } else {
0132         scsi_pointer->ptr = NULL;
0133         scsi_pointer->this_residual = 0;
0134         scsi_pointer->phase = 0;
0135     }
0136 }