Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/acorn/scsi/scsi.h |
| 3 | * |
| 4 | * Copyright (C) 2002 Russell King |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * Commonly used scsi driver functions. |
| 11 | */ |
| 12 | |
David Hardeman | 378f058 | 2005-09-17 17:55:31 +1000 | [diff] [blame] | 13 | #include <linux/scatterlist.h> |
| 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #define BELT_AND_BRACES |
| 16 | |
| 17 | /* |
| 18 | * The scatter-gather list handling. This contains all |
| 19 | * the yucky stuff that needs to be fixed properly. |
| 20 | */ |
Boaz Harrosh | 84ac86c | 2007-09-09 21:31:21 +0300 | [diff] [blame] | 21 | |
| 22 | /* |
| 23 | * copy_SCp_to_sg() Assumes contiguous allocation at @sg of at-most @max |
| 24 | * entries of uninitialized memory. SCp is from scsi-ml and has a valid |
| 25 | * (possibly chained) sg-list |
| 26 | */ |
Christoph Hellwig | 0a04137 | 2005-10-31 18:31:56 +0100 | [diff] [blame] | 27 | static inline int copy_SCp_to_sg(struct scatterlist *sg, struct scsi_pointer *SCp, int max) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | { |
| 29 | int bufs = SCp->buffers_residual; |
| 30 | |
Boaz Harrosh | 84ac86c | 2007-09-09 21:31:21 +0300 | [diff] [blame] | 31 | /* FIXME: It should be easy for drivers to loop on copy_SCp_to_sg(). |
| 32 | * and to remove this BUG_ON. Use min() in-its-place |
| 33 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | BUG_ON(bufs + 1 > max); |
| 35 | |
David Hardeman | 378f058 | 2005-09-17 17:55:31 +1000 | [diff] [blame] | 36 | sg_set_buf(sg, SCp->ptr, SCp->this_residual); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Boaz Harrosh | 84ac86c | 2007-09-09 21:31:21 +0300 | [diff] [blame] | 38 | if (bufs) { |
| 39 | struct scatterlist *src_sg; |
| 40 | unsigned i; |
| 41 | |
| 42 | for_each_sg(sg_next(SCp->buffer), src_sg, bufs, i) |
| 43 | *(++sg) = *src_sg; |
| 44 | sg_mark_end(sg); |
| 45 | } |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | return bufs + 1; |
| 48 | } |
| 49 | |
Christoph Hellwig | 0a04137 | 2005-10-31 18:31:56 +0100 | [diff] [blame] | 50 | static inline int next_SCp(struct scsi_pointer *SCp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { |
| 52 | int ret = SCp->buffers_residual; |
| 53 | if (ret) { |
Boaz Harrosh | 84ac86c | 2007-09-09 21:31:21 +0300 | [diff] [blame] | 54 | SCp->buffer = sg_next(SCp->buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | SCp->buffers_residual--; |
Russell King | 01c0ad5 | 2007-10-26 17:47:34 +0100 | [diff] [blame] | 56 | SCp->ptr = sg_virt(SCp->buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | SCp->this_residual = SCp->buffer->length; |
| 58 | } else { |
| 59 | SCp->ptr = NULL; |
| 60 | SCp->this_residual = 0; |
| 61 | } |
| 62 | return ret; |
| 63 | } |
| 64 | |
Christoph Hellwig | 0a04137 | 2005-10-31 18:31:56 +0100 | [diff] [blame] | 65 | static inline unsigned char get_next_SCp_byte(struct scsi_pointer *SCp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
| 67 | char c = *SCp->ptr; |
| 68 | |
| 69 | SCp->ptr += 1; |
| 70 | SCp->this_residual -= 1; |
| 71 | |
| 72 | return c; |
| 73 | } |
| 74 | |
Christoph Hellwig | 0a04137 | 2005-10-31 18:31:56 +0100 | [diff] [blame] | 75 | static inline void put_next_SCp_byte(struct scsi_pointer *SCp, unsigned char c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
| 77 | *SCp->ptr = c; |
| 78 | SCp->ptr += 1; |
| 79 | SCp->this_residual -= 1; |
| 80 | } |
| 81 | |
Henne | ee0ca6b | 2006-10-01 13:18:37 +0200 | [diff] [blame] | 82 | static inline void init_SCp(struct scsi_cmnd *SCpnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | { |
| 84 | memset(&SCpnt->SCp, 0, sizeof(struct scsi_pointer)); |
| 85 | |
Boaz Harrosh | 84ac86c | 2007-09-09 21:31:21 +0300 | [diff] [blame] | 86 | if (scsi_bufflen(SCpnt)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | unsigned long len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Boaz Harrosh | 84ac86c | 2007-09-09 21:31:21 +0300 | [diff] [blame] | 89 | SCpnt->SCp.buffer = scsi_sglist(SCpnt); |
| 90 | SCpnt->SCp.buffers_residual = scsi_sg_count(SCpnt) - 1; |
Russell King | 01c0ad5 | 2007-10-26 17:47:34 +0100 | [diff] [blame] | 91 | SCpnt->SCp.ptr = sg_virt(SCpnt->SCp.buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length; |
Boaz Harrosh | 84ac86c | 2007-09-09 21:31:21 +0300 | [diff] [blame] | 93 | SCpnt->SCp.phase = scsi_bufflen(SCpnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
| 95 | #ifdef BELT_AND_BRACES |
Boaz Harrosh | 84ac86c | 2007-09-09 21:31:21 +0300 | [diff] [blame] | 96 | { /* |
| 97 | * Calculate correct buffer length. Some commands |
| 98 | * come in with the wrong scsi_bufflen. |
| 99 | */ |
| 100 | struct scatterlist *sg; |
| 101 | unsigned i, sg_count = scsi_sg_count(SCpnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Boaz Harrosh | 84ac86c | 2007-09-09 21:31:21 +0300 | [diff] [blame] | 103 | scsi_for_each_sg(SCpnt, sg, sg_count, i) |
| 104 | len += sg->length; |
| 105 | |
| 106 | if (scsi_bufflen(SCpnt) != len) { |
| 107 | printk(KERN_WARNING |
| 108 | "scsi%d.%c: bad request buffer " |
| 109 | "length %d, should be %ld\n", |
| 110 | SCpnt->device->host->host_no, |
| 111 | '0' + SCpnt->device->id, |
| 112 | scsi_bufflen(SCpnt), len); |
| 113 | /* |
| 114 | * FIXME: Totaly naive fixup. We should abort |
| 115 | * with error |
| 116 | */ |
| 117 | SCpnt->SCp.phase = |
| 118 | min_t(unsigned long, len, |
| 119 | scsi_bufflen(SCpnt)); |
| 120 | } |
| 121 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | #endif |
| 123 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | SCpnt->SCp.ptr = NULL; |
Boaz Harrosh | 84ac86c | 2007-09-09 21:31:21 +0300 | [diff] [blame] | 125 | SCpnt->SCp.this_residual = 0; |
| 126 | SCpnt->SCp.phase = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
| 128 | } |