| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  *  linux/drivers/acorn/scsi/scsi.h | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  Copyright (C) 2002 Russell King | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU General Public License version 2 as | 
					
						
							|  |  |  |  * published by the Free Software Foundation. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  Commonly used scsi driver functions. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-09-17 17:55:31 +10:00
										 |  |  | #include <linux/scatterlist.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | #define BELT_AND_BRACES
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * The scatter-gather list handling.  This contains all | 
					
						
							|  |  |  |  * the yucky stuff that needs to be fixed properly. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2007-09-09 21:31:21 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * copy_SCp_to_sg() Assumes contiguous allocation at @sg of at-most @max | 
					
						
							|  |  |  |  * entries of uninitialized memory. SCp is from scsi-ml and has a valid | 
					
						
							|  |  |  |  * (possibly chained) sg-list | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2005-10-31 18:31:56 +01:00
										 |  |  | static inline int copy_SCp_to_sg(struct scatterlist *sg, struct scsi_pointer *SCp, int max) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	int bufs = SCp->buffers_residual; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-09 21:31:21 +03:00
										 |  |  | 	/* FIXME: It should be easy for drivers to loop on copy_SCp_to_sg().
 | 
					
						
							|  |  |  | 	 * and to remove this BUG_ON. Use min() in-its-place | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	BUG_ON(bufs + 1 > max); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-09-17 17:55:31 +10:00
										 |  |  | 	sg_set_buf(sg, SCp->ptr, SCp->this_residual); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-09 21:31:21 +03:00
										 |  |  | 	if (bufs) { | 
					
						
							|  |  |  | 		struct scatterlist *src_sg; | 
					
						
							|  |  |  | 		unsigned i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for_each_sg(sg_next(SCp->buffer), src_sg, bufs, i) | 
					
						
							|  |  |  | 			*(++sg) = *src_sg; | 
					
						
							|  |  |  | 		sg_mark_end(sg); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	return bufs + 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-31 18:31:56 +01:00
										 |  |  | static inline int next_SCp(struct scsi_pointer *SCp) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	int ret = SCp->buffers_residual; | 
					
						
							|  |  |  | 	if (ret) { | 
					
						
							| 
									
										
										
										
											2007-09-09 21:31:21 +03:00
										 |  |  | 		SCp->buffer = sg_next(SCp->buffer); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		SCp->buffers_residual--; | 
					
						
							| 
									
										
										
										
											2007-10-26 17:47:34 +01:00
										 |  |  | 		SCp->ptr = sg_virt(SCp->buffer); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		SCp->this_residual = SCp->buffer->length; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		SCp->ptr = NULL; | 
					
						
							|  |  |  | 		SCp->this_residual = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-31 18:31:56 +01:00
										 |  |  | static inline unsigned char get_next_SCp_byte(struct scsi_pointer *SCp) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	char c = *SCp->ptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	SCp->ptr += 1; | 
					
						
							|  |  |  | 	SCp->this_residual -= 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return c; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-31 18:31:56 +01:00
										 |  |  | static inline void put_next_SCp_byte(struct scsi_pointer *SCp, unsigned char c) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	*SCp->ptr = c; | 
					
						
							|  |  |  | 	SCp->ptr += 1; | 
					
						
							|  |  |  | 	SCp->this_residual -= 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-10-01 13:18:37 +02:00
										 |  |  | static inline void init_SCp(struct scsi_cmnd *SCpnt) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	memset(&SCpnt->SCp, 0, sizeof(struct scsi_pointer)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-09 21:31:21 +03:00
										 |  |  | 	if (scsi_bufflen(SCpnt)) { | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		unsigned long len = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-09 21:31:21 +03:00
										 |  |  | 		SCpnt->SCp.buffer = scsi_sglist(SCpnt); | 
					
						
							|  |  |  | 		SCpnt->SCp.buffers_residual = scsi_sg_count(SCpnt) - 1; | 
					
						
							| 
									
										
										
										
											2007-10-26 17:47:34 +01:00
										 |  |  | 		SCpnt->SCp.ptr = sg_virt(SCpnt->SCp.buffer); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length; | 
					
						
							| 
									
										
										
										
											2007-09-09 21:31:21 +03:00
										 |  |  | 		SCpnt->SCp.phase = scsi_bufflen(SCpnt); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef BELT_AND_BRACES
 | 
					
						
							| 
									
										
										
										
											2007-09-09 21:31:21 +03:00
										 |  |  | 		{	/*
 | 
					
						
							|  |  |  | 			 * Calculate correct buffer length.  Some commands | 
					
						
							|  |  |  | 			 * come in with the wrong scsi_bufflen. | 
					
						
							|  |  |  | 			 */ | 
					
						
							|  |  |  | 			struct scatterlist *sg; | 
					
						
							|  |  |  | 			unsigned i, sg_count = scsi_sg_count(SCpnt); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			scsi_for_each_sg(SCpnt, sg, sg_count, i) | 
					
						
							|  |  |  | 				len += sg->length; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (scsi_bufflen(SCpnt) != len) { | 
					
						
							|  |  |  | 				printk(KERN_WARNING | 
					
						
							|  |  |  | 				       "scsi%d.%c: bad request buffer " | 
					
						
							|  |  |  | 				       "length %d, should be %ld\n", | 
					
						
							|  |  |  | 					SCpnt->device->host->host_no, | 
					
						
							|  |  |  | 					'0' + SCpnt->device->id, | 
					
						
							|  |  |  | 					scsi_bufflen(SCpnt), len); | 
					
						
							|  |  |  | 				/*
 | 
					
						
							|  |  |  | 				 * FIXME: Totaly naive fixup. We should abort | 
					
						
							|  |  |  | 				 * with error | 
					
						
							|  |  |  | 				 */ | 
					
						
							|  |  |  | 				SCpnt->SCp.phase = | 
					
						
							|  |  |  | 					min_t(unsigned long, len, | 
					
						
							|  |  |  | 					      scsi_bufflen(SCpnt)); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		SCpnt->SCp.ptr = NULL; | 
					
						
							| 
									
										
										
										
											2007-09-09 21:31:21 +03:00
										 |  |  | 		SCpnt->SCp.this_residual = 0; | 
					
						
							|  |  |  | 		SCpnt->SCp.phase = 0; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |