1 #include <linux/slab.h>
2 #include <linux/module.h>
3 #include <linux/mutex.h>
6 #include <scsi/scsi_cmnd.h>
7 #include <scsi/scsi_devinfo.h>
8 #include <scsi/scsi_device.h>
9 #include <scsi/scsi_eh.h>
13 #include "transport.h"
16 //----- host_info() ---------------------
17 static const char* host_info(struct Scsi_Host
*host
)
19 //printk("scsiglue --- host_info\n");
20 return "SCSI emulation for USB Mass Storage devices";
23 //----- slave_alloc() ---------------------
24 static int slave_alloc(struct scsi_device
*sdev
)
26 struct us_data
*us
= host_to_us(sdev
->host
);
28 //printk("scsiglue --- slave_alloc\n");
29 sdev
->inquiry_len
= 36;
31 blk_queue_update_dma_alignment(sdev
->request_queue
, (512 - 1));
33 if (us
->subclass
== USB_SC_UFI
)
34 sdev
->sdev_target
->pdt_1f_for_no_lun
= 1;
39 //----- slave_configure() ---------------------
40 static int slave_configure(struct scsi_device
*sdev
)
42 struct us_data
*us
= host_to_us(sdev
->host
);
44 //printk("scsiglue --- slave_configure\n");
45 if (us
->fflags
& (US_FL_MAX_SECTORS_64
| US_FL_MAX_SECTORS_MIN
))
47 unsigned int max_sectors
= 64;
49 if (us
->fflags
& US_FL_MAX_SECTORS_MIN
)
50 max_sectors
= PAGE_CACHE_SIZE
>> 9;
51 if (queue_max_sectors(sdev
->request_queue
) > max_sectors
)
52 blk_queue_max_hw_sectors(sdev
->request_queue
,
56 if (sdev
->type
== TYPE_DISK
)
58 if (us
->subclass
!= USB_SC_SCSI
&& us
->subclass
!= USB_SC_CYP_ATACB
)
59 sdev
->use_10_for_ms
= 1;
60 sdev
->use_192_bytes_for_3f
= 1;
61 if (us
->fflags
& US_FL_NO_WP_DETECT
)
62 sdev
->skip_ms_page_3f
= 1;
63 sdev
->skip_ms_page_8
= 1;
64 if (us
->fflags
& US_FL_FIX_CAPACITY
)
65 sdev
->fix_capacity
= 1;
66 if (us
->fflags
& US_FL_CAPACITY_HEURISTICS
)
67 sdev
->guess_capacity
= 1;
68 if (sdev
->scsi_level
> SCSI_2
)
69 sdev
->sdev_target
->scsi_level
= sdev
->scsi_level
= SCSI_2
;
70 sdev
->retry_hwerror
= 1;
71 sdev
->allow_restart
= 1;
72 sdev
->last_sector_bug
= 1;
76 sdev
->use_10_for_ms
= 1;
79 if ((us
->protocol
== USB_PR_CB
|| us
->protocol
== USB_PR_CBI
) && sdev
->scsi_level
== SCSI_UNKNOWN
)
82 if (us
->fflags
& US_FL_NOT_LOCKABLE
)
88 /* This is always called with scsi_lock(host) held */
89 //----- queuecommand() ---------------------
90 static int queuecommand(struct scsi_cmnd
*srb
, void (*done
)(struct scsi_cmnd
*))
92 struct us_data
*us
= host_to_us(srb
->device
->host
);
94 //printk("scsiglue --- queuecommand\n");
96 /* check for state-transition errors */
99 printk("Error in %s: us->srb = %p\n", __FUNCTION__
, us
->srb
);
100 return SCSI_MLQUEUE_HOST_BUSY
;
103 /* fail the command if we are disconnecting */
104 if (test_bit(US_FLIDX_DISCONNECTING
, &us
->dflags
))
106 printk("Fail command during disconnect\n");
107 srb
->result
= DID_NO_CONNECT
<< 16;
112 /* enqueue the command and wake up the control thread */
113 srb
->scsi_done
= done
;
115 complete(&us
->cmnd_ready
);
120 /***********************************************************************
121 * Error handling functions
122 ***********************************************************************/
124 /* Command timeout and abort */
125 //----- command_abort() ---------------------
126 static int command_abort(struct scsi_cmnd
*srb
)
128 struct us_data
*us
= host_to_us(srb
->device
->host
);
130 //printk("scsiglue --- command_abort\n");
132 scsi_lock(us_to_host(us
));
135 scsi_unlock(us_to_host(us
));
136 printk ("-- nothing to abort\n");
140 set_bit(US_FLIDX_TIMED_OUT
, &us
->dflags
);
141 if (!test_bit(US_FLIDX_RESETTING
, &us
->dflags
))
143 set_bit(US_FLIDX_ABORTING
, &us
->dflags
);
144 usb_stor_stop_transport(us
);
146 scsi_unlock(us_to_host(us
));
148 /* Wait for the aborted command to finish */
149 wait_for_completion(&us
->notify
);
153 /* This invokes the transport reset mechanism to reset the state of the device */
154 //----- device_reset() ---------------------
155 static int device_reset(struct scsi_cmnd
*srb
)
157 struct us_data
*us
= host_to_us(srb
->device
->host
);
160 //printk("scsiglue --- device_reset\n");
162 /* lock the device pointers and do the reset */
163 mutex_lock(&(us
->dev_mutex
));
164 result
= us
->transport_reset(us
);
165 mutex_unlock(&us
->dev_mutex
);
167 return result
< 0 ? FAILED
: SUCCESS
;
170 //----- bus_reset() ---------------------
171 static int bus_reset(struct scsi_cmnd
*srb
)
173 struct us_data
*us
= host_to_us(srb
->device
->host
);
176 //printk("scsiglue --- bus_reset\n");
177 result
= usb_stor_port_reset(us
);
178 return result
< 0 ? FAILED
: SUCCESS
;
181 //----- usb_stor_report_device_reset() ---------------------
182 void usb_stor_report_device_reset(struct us_data
*us
)
185 struct Scsi_Host
*host
= us_to_host(us
);
187 //printk("scsiglue --- usb_stor_report_device_reset\n");
188 scsi_report_device_reset(host
, 0, 0);
189 if (us
->fflags
& US_FL_SCM_MULT_TARG
)
191 for (i
= 1; i
< host
->max_id
; ++i
)
192 scsi_report_device_reset(host
, 0, i
);
196 //----- usb_stor_report_bus_reset() ---------------------
197 void usb_stor_report_bus_reset(struct us_data
*us
)
199 struct Scsi_Host
*host
= us_to_host(us
);
201 //printk("scsiglue --- usb_stor_report_bus_reset\n");
203 scsi_report_bus_reset(host
, 0);
207 /***********************************************************************
208 * /proc/scsi/ functions
209 ***********************************************************************/
211 /* we use this macro to help us write into the buffer */
213 #define SPRINTF(args...) \
214 do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
216 //----- proc_info() ---------------------
217 static int proc_info (struct Scsi_Host
*host
, char *buffer
, char **start
, off_t offset
, int length
, int inout
)
219 struct us_data
*us
= host_to_us(host
);
223 //printk("scsiglue --- proc_info\n");
227 /* print the controller name */
228 SPRINTF(" Host scsi%d: usb-storage\n", host
->host_no
);
230 /* print product, vendor, and serial number strings */
231 if (us
->pusb_dev
->manufacturer
)
232 string
= us
->pusb_dev
->manufacturer
;
233 else if (us
->unusual_dev
->vendorName
)
234 string
= us
->unusual_dev
->vendorName
;
237 SPRINTF(" Vendor: %s\n", string
);
238 if (us
->pusb_dev
->product
)
239 string
= us
->pusb_dev
->product
;
240 else if (us
->unusual_dev
->productName
)
241 string
= us
->unusual_dev
->productName
;
244 SPRINTF(" Product: %s\n", string
);
245 if (us
->pusb_dev
->serial
)
246 string
= us
->pusb_dev
->serial
;
249 SPRINTF("Serial Number: %s\n", string
);
251 /* show the protocol and transport */
252 SPRINTF(" Protocol: %s\n", us
->protocol_name
);
253 SPRINTF(" Transport: %s\n", us
->transport_name
);
255 /* show the device flags */
256 if (pos
< buffer
+ length
)
258 pos
+= sprintf(pos
, " Quirks:");
260 #define US_FLAG(name, value) \
261 if (us->fflags & value) pos += sprintf(pos, " " #name);
268 /* Calculate start of next buffer, and return value. */
269 *start
= buffer
+ offset
;
271 if ((pos
- buffer
) < offset
)
273 else if ((pos
- buffer
- offset
) < length
)
274 return (pos
- buffer
- offset
);
279 /***********************************************************************
281 ***********************************************************************/
283 /* Output routine for the sysfs max_sectors file */
284 //----- show_max_sectors() ---------------------
285 static ssize_t
show_max_sectors(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
287 struct scsi_device
*sdev
= to_scsi_device(dev
);
289 //printk("scsiglue --- ssize_t show_max_sectors\n");
290 return sprintf(buf
, "%u\n", queue_max_sectors(sdev
->request_queue
));
293 /* Input routine for the sysfs max_sectors file */
294 //----- store_max_sectors() ---------------------
295 static ssize_t
store_max_sectors(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
297 struct scsi_device
*sdev
= to_scsi_device(dev
);
300 //printk("scsiglue --- ssize_t store_max_sectors\n");
301 if (sscanf(buf
, "%hu", &ms
) > 0 && ms
<= SCSI_DEFAULT_MAX_SECTORS
)
303 blk_queue_max_hw_sectors(sdev
->request_queue
, ms
);
309 static DEVICE_ATTR(max_sectors
, S_IRUGO
| S_IWUSR
, show_max_sectors
, store_max_sectors
);
310 static struct device_attribute
*sysfs_device_attr_list
[] = {&dev_attr_max_sectors
, NULL
, };
312 /* this defines our host template, with which we'll allocate hosts */
314 //----- usb_stor_host_template() ---------------------
315 struct scsi_host_template usb_stor_host_template
= {
316 /* basic userland interface stuff */
317 .name
= "eucr-storage",
318 .proc_name
= "eucr-storage",
319 .proc_info
= proc_info
,
322 /* command interface -- queued only */
323 .queuecommand
= queuecommand
,
325 /* error and abort handlers */
326 .eh_abort_handler
= command_abort
,
327 .eh_device_reset_handler
= device_reset
,
328 .eh_bus_reset_handler
= bus_reset
,
330 /* queue commands only, only one command per LUN */
334 /* unknown initiator id */
337 .slave_alloc
= slave_alloc
,
338 .slave_configure
= slave_configure
,
340 /* lots of sg segments can be handled */
341 .sg_tablesize
= SG_ALL
,
343 /* limit the total size of a transfer to 120 KB */
346 /* merge commands... this seems to help performance, but
347 * periodically someone should test to see which setting is more
355 /* we do our own delay after a device or bus reset */
356 .skip_settle_delay
= 1,
358 /* sysfs device attributes */
359 .sdev_attrs
= sysfs_device_attr_list
,
361 /* module management */
362 .module
= THIS_MODULE
365 /* To Report "Illegal Request: Invalid Field in CDB */
366 unsigned char usb_stor_sense_invalidCDB
[18] = {
367 [0] = 0x70, /* current error */
368 [2] = ILLEGAL_REQUEST
, /* Illegal Request = 0x05 */
369 [7] = 0x0a, /* additional length */
370 [12] = 0x24 /* Invalid Field in CDB */
373 /***********************************************************************
374 * Scatter-gather transfer buffer access routines
375 ***********************************************************************/
377 //----- usb_stor_access_xfer_buf() ---------------------
378 unsigned int usb_stor_access_xfer_buf(struct us_data
*us
, unsigned char *buffer
,
379 unsigned int buflen
, struct scsi_cmnd
*srb
, struct scatterlist
**sgptr
,
380 unsigned int *offset
, enum xfer_buf_dir dir
)
384 //printk("transport --- usb_stor_access_xfer_buf\n");
385 struct scatterlist
*sg
= *sgptr
;
388 sg
= scsi_sglist(srb
);
391 while (cnt
< buflen
&& sg
)
393 struct page
*page
= sg_page(sg
) + ((sg
->offset
+ *offset
) >> PAGE_SHIFT
);
394 unsigned int poff
= (sg
->offset
+ *offset
) & (PAGE_SIZE
-1);
395 unsigned int sglen
= sg
->length
- *offset
;
397 if (sglen
> buflen
- cnt
)
399 /* Transfer ends within this s-g entry */
400 sglen
= buflen
- cnt
;
405 /* Transfer continues to next s-g entry */
412 unsigned int plen
= min(sglen
, (unsigned int)PAGE_SIZE
- poff
);
413 unsigned char *ptr
= kmap(page
);
415 if (dir
== TO_XFER_BUF
)
416 memcpy(ptr
+ poff
, buffer
+ cnt
, plen
);
418 memcpy(buffer
+ cnt
, ptr
+ poff
, plen
);
421 /* Start at the beginning of the next page */
430 /* Return the amount actually transferred */
434 /* Store the contents of buffer into srb's transfer buffer and set the SCSI residue. */
435 //----- usb_stor_set_xfer_buf() ---------------------
436 void usb_stor_set_xfer_buf(struct us_data
*us
, unsigned char *buffer
, unsigned int buflen
, struct scsi_cmnd
*srb
,
439 unsigned int offset
= 0;
440 struct scatterlist
*sg
= NULL
;
442 //printk("transport --- usb_stor_set_xfer_buf\n");
443 // TO_XFER_BUF = 0, FROM_XFER_BUF = 1
444 buflen
= min(buflen
, scsi_bufflen(srb
));
445 buflen
= usb_stor_access_xfer_buf(us
, buffer
, buflen
, srb
, &sg
, &offset
, dir
);
446 if (buflen
< scsi_bufflen(srb
))
447 scsi_set_resid(srb
, scsi_bufflen(srb
) - buflen
);