1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3 #include <linux/slab.h>
4 #include <linux/module.h>
5 #include <linux/mutex.h>
8 #include <scsi/scsi_cmnd.h>
9 #include <scsi/scsi_devinfo.h>
10 #include <scsi/scsi_device.h>
11 #include <scsi/scsi_eh.h>
15 #include "transport.h"
18 //----- host_info() ---------------------
19 static const char* host_info(struct Scsi_Host
*host
)
21 /* pr_info("scsiglue --- host_info\n"); */
22 return "SCSI emulation for USB Mass Storage devices";
25 //----- slave_alloc() ---------------------
26 static int slave_alloc(struct scsi_device
*sdev
)
28 struct us_data
*us
= host_to_us(sdev
->host
);
30 /* pr_info("scsiglue --- slave_alloc\n"); */
31 sdev
->inquiry_len
= 36;
33 blk_queue_update_dma_alignment(sdev
->request_queue
, (512 - 1));
35 if (us
->subclass
== USB_SC_UFI
)
36 sdev
->sdev_target
->pdt_1f_for_no_lun
= 1;
41 //----- slave_configure() ---------------------
42 static int slave_configure(struct scsi_device
*sdev
)
44 struct us_data
*us
= host_to_us(sdev
->host
);
46 /* pr_info("scsiglue --- slave_configure\n"); */
47 if (us
->fflags
& (US_FL_MAX_SECTORS_64
| US_FL_MAX_SECTORS_MIN
))
49 unsigned int max_sectors
= 64;
51 if (us
->fflags
& US_FL_MAX_SECTORS_MIN
)
52 max_sectors
= PAGE_CACHE_SIZE
>> 9;
53 if (queue_max_sectors(sdev
->request_queue
) > max_sectors
)
54 blk_queue_max_hw_sectors(sdev
->request_queue
,
58 if (sdev
->type
== TYPE_DISK
)
60 if (us
->subclass
!= USB_SC_SCSI
&& us
->subclass
!= USB_SC_CYP_ATACB
)
61 sdev
->use_10_for_ms
= 1;
62 sdev
->use_192_bytes_for_3f
= 1;
63 if (us
->fflags
& US_FL_NO_WP_DETECT
)
64 sdev
->skip_ms_page_3f
= 1;
65 sdev
->skip_ms_page_8
= 1;
66 if (us
->fflags
& US_FL_FIX_CAPACITY
)
67 sdev
->fix_capacity
= 1;
68 if (us
->fflags
& US_FL_CAPACITY_HEURISTICS
)
69 sdev
->guess_capacity
= 1;
70 if (sdev
->scsi_level
> SCSI_2
)
71 sdev
->sdev_target
->scsi_level
= sdev
->scsi_level
= SCSI_2
;
72 sdev
->retry_hwerror
= 1;
73 sdev
->allow_restart
= 1;
74 sdev
->last_sector_bug
= 1;
78 sdev
->use_10_for_ms
= 1;
81 if ((us
->protocol
== USB_PR_CB
|| us
->protocol
== USB_PR_CBI
) && sdev
->scsi_level
== SCSI_UNKNOWN
)
84 if (us
->fflags
& US_FL_NOT_LOCKABLE
)
90 /* This is always called with scsi_lock(host) held */
91 //----- queuecommand() ---------------------
92 static int queuecommand_lck(struct scsi_cmnd
*srb
, void (*done
)(struct scsi_cmnd
*))
94 struct us_data
*us
= host_to_us(srb
->device
->host
);
96 /* pr_info("scsiglue --- queuecommand\n"); */
98 /* check for state-transition errors */
101 /* pr_info("Error in %s: us->srb = %p\n"
102 __FUNCTION__, us->srb); */
103 return SCSI_MLQUEUE_HOST_BUSY
;
106 /* fail the command if we are disconnecting */
107 if (test_bit(US_FLIDX_DISCONNECTING
, &us
->dflags
))
109 pr_info("Fail command during disconnect\n");
110 srb
->result
= DID_NO_CONNECT
<< 16;
115 /* enqueue the command and wake up the control thread */
116 srb
->scsi_done
= done
;
118 complete(&us
->cmnd_ready
);
123 static DEF_SCSI_QCMD(queuecommand
)
125 /***********************************************************************
126 * Error handling functions
127 ***********************************************************************/
129 /* Command timeout and abort */
130 //----- command_abort() ---------------------
131 static int command_abort(struct scsi_cmnd
*srb
)
133 struct us_data
*us
= host_to_us(srb
->device
->host
);
135 /* pr_info("scsiglue --- command_abort\n"); */
137 scsi_lock(us_to_host(us
));
140 scsi_unlock(us_to_host(us
));
141 printk ("-- nothing to abort\n");
145 set_bit(US_FLIDX_TIMED_OUT
, &us
->dflags
);
146 if (!test_bit(US_FLIDX_RESETTING
, &us
->dflags
))
148 set_bit(US_FLIDX_ABORTING
, &us
->dflags
);
149 usb_stor_stop_transport(us
);
151 scsi_unlock(us_to_host(us
));
153 /* Wait for the aborted command to finish */
154 wait_for_completion(&us
->notify
);
158 /* This invokes the transport reset mechanism to reset the state of the device */
159 //----- device_reset() ---------------------
160 static int device_reset(struct scsi_cmnd
*srb
)
162 struct us_data
*us
= host_to_us(srb
->device
->host
);
165 /* pr_info("scsiglue --- device_reset\n"); */
167 /* lock the device pointers and do the reset */
168 mutex_lock(&(us
->dev_mutex
));
169 result
= us
->transport_reset(us
);
170 mutex_unlock(&us
->dev_mutex
);
172 return result
< 0 ? FAILED
: SUCCESS
;
175 //----- bus_reset() ---------------------
176 static int bus_reset(struct scsi_cmnd
*srb
)
178 struct us_data
*us
= host_to_us(srb
->device
->host
);
181 /* pr_info("scsiglue --- bus_reset\n"); */
182 result
= usb_stor_port_reset(us
);
183 return result
< 0 ? FAILED
: SUCCESS
;
186 //----- usb_stor_report_device_reset() ---------------------
187 void usb_stor_report_device_reset(struct us_data
*us
)
190 struct Scsi_Host
*host
= us_to_host(us
);
192 /* pr_info("scsiglue --- usb_stor_report_device_reset\n"); */
193 scsi_report_device_reset(host
, 0, 0);
194 if (us
->fflags
& US_FL_SCM_MULT_TARG
)
196 for (i
= 1; i
< host
->max_id
; ++i
)
197 scsi_report_device_reset(host
, 0, i
);
201 //----- usb_stor_report_bus_reset() ---------------------
202 void usb_stor_report_bus_reset(struct us_data
*us
)
204 struct Scsi_Host
*host
= us_to_host(us
);
206 /* pr_info("scsiglue --- usb_stor_report_bus_reset\n"); */
208 scsi_report_bus_reset(host
, 0);
212 /***********************************************************************
213 * /proc/scsi/ functions
214 ***********************************************************************/
216 /* we use this macro to help us write into the buffer */
218 #define SPRINTF(args...) \
219 do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
221 //----- proc_info() ---------------------
222 static int proc_info (struct Scsi_Host
*host
, char *buffer
, char **start
, off_t offset
, int length
, int inout
)
224 struct us_data
*us
= host_to_us(host
);
228 /* pr_info("scsiglue --- proc_info\n"); */
232 /* print the controller name */
233 SPRINTF(" Host scsi%d: usb-storage\n", host
->host_no
);
235 /* print product, vendor, and serial number strings */
236 if (us
->pusb_dev
->manufacturer
)
237 string
= us
->pusb_dev
->manufacturer
;
238 else if (us
->unusual_dev
->vendorName
)
239 string
= us
->unusual_dev
->vendorName
;
242 SPRINTF(" Vendor: %s\n", string
);
243 if (us
->pusb_dev
->product
)
244 string
= us
->pusb_dev
->product
;
245 else if (us
->unusual_dev
->productName
)
246 string
= us
->unusual_dev
->productName
;
249 SPRINTF(" Product: %s\n", string
);
250 if (us
->pusb_dev
->serial
)
251 string
= us
->pusb_dev
->serial
;
254 SPRINTF("Serial Number: %s\n", string
);
256 /* show the protocol and transport */
257 SPRINTF(" Protocol: %s\n", us
->protocol_name
);
258 SPRINTF(" Transport: %s\n", us
->transport_name
);
260 /* show the device flags */
261 if (pos
< buffer
+ length
)
263 pos
+= sprintf(pos
, " Quirks:");
265 #define US_FLAG(name, value) \
266 if (us->fflags & value) pos += sprintf(pos, " " #name);
273 /* Calculate start of next buffer, and return value. */
274 *start
= buffer
+ offset
;
276 if ((pos
- buffer
) < offset
)
278 else if ((pos
- buffer
- offset
) < length
)
279 return (pos
- buffer
- offset
);
284 /***********************************************************************
286 ***********************************************************************/
288 /* Output routine for the sysfs max_sectors file */
289 //----- show_max_sectors() ---------------------
290 static ssize_t
show_max_sectors(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
292 struct scsi_device
*sdev
= to_scsi_device(dev
);
294 /* pr_info("scsiglue --- ssize_t show_max_sectors\n"); */
295 return sprintf(buf
, "%u\n", queue_max_sectors(sdev
->request_queue
));
298 /* Input routine for the sysfs max_sectors file */
299 //----- store_max_sectors() ---------------------
300 static ssize_t
store_max_sectors(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
302 struct scsi_device
*sdev
= to_scsi_device(dev
);
305 /* pr_info("scsiglue --- ssize_t store_max_sectors\n"); */
306 if (sscanf(buf
, "%hu", &ms
) > 0 && ms
<= SCSI_DEFAULT_MAX_SECTORS
)
308 blk_queue_max_hw_sectors(sdev
->request_queue
, ms
);
314 static DEVICE_ATTR(max_sectors
, S_IRUGO
| S_IWUSR
, show_max_sectors
, store_max_sectors
);
315 static struct device_attribute
*sysfs_device_attr_list
[] = {&dev_attr_max_sectors
, NULL
, };
317 /* this defines our host template, with which we'll allocate hosts */
319 //----- usb_stor_host_template() ---------------------
320 struct scsi_host_template usb_stor_host_template
= {
321 /* basic userland interface stuff */
322 .name
= "eucr-storage",
323 .proc_name
= "eucr-storage",
324 .proc_info
= proc_info
,
327 /* command interface -- queued only */
328 .queuecommand
= queuecommand
,
330 /* error and abort handlers */
331 .eh_abort_handler
= command_abort
,
332 .eh_device_reset_handler
= device_reset
,
333 .eh_bus_reset_handler
= bus_reset
,
335 /* queue commands only, only one command per LUN */
339 /* unknown initiator id */
342 .slave_alloc
= slave_alloc
,
343 .slave_configure
= slave_configure
,
345 /* lots of sg segments can be handled */
346 .sg_tablesize
= SG_ALL
,
348 /* limit the total size of a transfer to 120 KB */
351 /* merge commands... this seems to help performance, but
352 * periodically someone should test to see which setting is more
360 /* we do our own delay after a device or bus reset */
361 .skip_settle_delay
= 1,
363 /* sysfs device attributes */
364 .sdev_attrs
= sysfs_device_attr_list
,
366 /* module management */
367 .module
= THIS_MODULE
370 /* To Report "Illegal Request: Invalid Field in CDB */
371 unsigned char usb_stor_sense_invalidCDB
[18] = {
372 [0] = 0x70, /* current error */
373 [2] = ILLEGAL_REQUEST
, /* Illegal Request = 0x05 */
374 [7] = 0x0a, /* additional length */
375 [12] = 0x24 /* Invalid Field in CDB */
378 /***********************************************************************
379 * Scatter-gather transfer buffer access routines
380 ***********************************************************************/
382 //----- usb_stor_access_xfer_buf() ---------------------
383 unsigned int usb_stor_access_xfer_buf(struct us_data
*us
, unsigned char *buffer
,
384 unsigned int buflen
, struct scsi_cmnd
*srb
, struct scatterlist
**sgptr
,
385 unsigned int *offset
, enum xfer_buf_dir dir
)
389 /* pr_info("transport --- usb_stor_access_xfer_buf\n"); */
390 struct scatterlist
*sg
= *sgptr
;
393 sg
= scsi_sglist(srb
);
396 while (cnt
< buflen
&& sg
)
398 struct page
*page
= sg_page(sg
) + ((sg
->offset
+ *offset
) >> PAGE_SHIFT
);
399 unsigned int poff
= (sg
->offset
+ *offset
) & (PAGE_SIZE
-1);
400 unsigned int sglen
= sg
->length
- *offset
;
402 if (sglen
> buflen
- cnt
)
404 /* Transfer ends within this s-g entry */
405 sglen
= buflen
- cnt
;
410 /* Transfer continues to next s-g entry */
417 unsigned int plen
= min(sglen
, (unsigned int)PAGE_SIZE
- poff
);
418 unsigned char *ptr
= kmap(page
);
420 if (dir
== TO_XFER_BUF
)
421 memcpy(ptr
+ poff
, buffer
+ cnt
, plen
);
423 memcpy(buffer
+ cnt
, ptr
+ poff
, plen
);
426 /* Start at the beginning of the next page */
435 /* Return the amount actually transferred */
439 /* Store the contents of buffer into srb's transfer buffer and set the SCSI residue. */
440 //----- usb_stor_set_xfer_buf() ---------------------
441 void usb_stor_set_xfer_buf(struct us_data
*us
, unsigned char *buffer
, unsigned int buflen
, struct scsi_cmnd
*srb
,
444 unsigned int offset
= 0;
445 struct scatterlist
*sg
= NULL
;
447 /* pr_info("transport --- usb_stor_set_xfer_buf\n"); */
448 // TO_XFER_BUF = 0, FROM_XFER_BUF = 1
449 buflen
= min(buflen
, scsi_bufflen(srb
));
450 buflen
= usb_stor_access_xfer_buf(us
, buffer
, buflen
, srb
, &sg
, &offset
, dir
);
451 if (buflen
< scsi_bufflen(srb
))
452 scsi_set_resid(srb
, scsi_bufflen(srb
) - buflen
);