blob: bb6550e31926d4a2752a1f7984d3d2978950fd42 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Hardeman378f0582005-09-17 17:55:31 +100013#include <linux/scatterlist.h>
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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 */
Christoph Hellwig0a041372005-10-31 18:31:56 +010021static inline int copy_SCp_to_sg(struct scatterlist *sg, struct scsi_pointer *SCp, int max)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
23 int bufs = SCp->buffers_residual;
24
25 BUG_ON(bufs + 1 > max);
26
David Hardeman378f0582005-09-17 17:55:31 +100027 sg_set_buf(sg, SCp->ptr, SCp->this_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29 if (bufs)
30 memcpy(sg + 1, SCp->buffer + 1,
31 sizeof(struct scatterlist) * bufs);
32 return bufs + 1;
33}
34
Christoph Hellwig0a041372005-10-31 18:31:56 +010035static inline int next_SCp(struct scsi_pointer *SCp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
37 int ret = SCp->buffers_residual;
38 if (ret) {
39 SCp->buffer++;
40 SCp->buffers_residual--;
Russell King01c0ad52007-10-26 17:47:34 +010041 SCp->ptr = sg_virt(SCp->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 SCp->this_residual = SCp->buffer->length;
43 } else {
44 SCp->ptr = NULL;
45 SCp->this_residual = 0;
46 }
47 return ret;
48}
49
Christoph Hellwig0a041372005-10-31 18:31:56 +010050static inline unsigned char get_next_SCp_byte(struct scsi_pointer *SCp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 char c = *SCp->ptr;
53
54 SCp->ptr += 1;
55 SCp->this_residual -= 1;
56
57 return c;
58}
59
Christoph Hellwig0a041372005-10-31 18:31:56 +010060static inline void put_next_SCp_byte(struct scsi_pointer *SCp, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 *SCp->ptr = c;
63 SCp->ptr += 1;
64 SCp->this_residual -= 1;
65}
66
Henneee0ca6b2006-10-01 13:18:37 +020067static inline void init_SCp(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 memset(&SCpnt->SCp, 0, sizeof(struct scsi_pointer));
70
71 if (SCpnt->use_sg) {
72 unsigned long len = 0;
73 int buf;
74
Russell King574dc0a2006-08-06 20:55:33 +010075 SCpnt->SCp.buffer = (struct scatterlist *) SCpnt->request_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 SCpnt->SCp.buffers_residual = SCpnt->use_sg - 1;
Russell King01c0ad52007-10-26 17:47:34 +010077 SCpnt->SCp.ptr = sg_virt(SCpnt->SCp.buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
Russell King6b4df7e2007-03-04 20:19:07 +000079 SCpnt->SCp.phase = SCpnt->request_bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81#ifdef BELT_AND_BRACES
82 /*
83 * Calculate correct buffer length. Some commands
84 * come in with the wrong request_bufflen.
85 */
86 for (buf = 0; buf <= SCpnt->SCp.buffers_residual; buf++)
87 len += SCpnt->SCp.buffer[buf].length;
88
89 if (SCpnt->request_bufflen != len)
90 printk(KERN_WARNING "scsi%d.%c: bad request buffer "
91 "length %d, should be %ld\n", SCpnt->device->host->host_no,
92 '0' + SCpnt->device->id, SCpnt->request_bufflen, len);
93 SCpnt->request_bufflen = len;
94#endif
95 } else {
96 SCpnt->SCp.ptr = (unsigned char *)SCpnt->request_buffer;
97 SCpnt->SCp.this_residual = SCpnt->request_bufflen;
Russell King6b4df7e2007-03-04 20:19:07 +000098 SCpnt->SCp.phase = SCpnt->request_bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100
101 /*
102 * If the upper SCSI layers pass a buffer, but zero length,
103 * we aren't interested in the buffer pointer.
104 */
105 if (SCpnt->SCp.this_residual == 0 && SCpnt->SCp.ptr) {
106#if 0 //def BELT_AND_BRACES
107 printk(KERN_WARNING "scsi%d.%c: zero length buffer passed for "
108 "command ", SCpnt->host->host_no, '0' + SCpnt->target);
db9dff32005-04-03 14:53:59 -0500109 __scsi_print_command(SCpnt->cmnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#endif
111 SCpnt->SCp.ptr = NULL;
112 }
113}