Commit | Line | Data |
---|---|---|
126bb03b AC |
1 | #include <linux/slab.h> |
2 | #include <linux/module.h> | |
3 | #include <linux/mutex.h> | |
4 | ||
5 | #include <scsi/scsi.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> | |
10 | ||
11 | #include "usb.h" | |
12 | #include "scsiglue.h" | |
13 | #include "transport.h" | |
14 | ||
15 | /* Host functions */ | |
16 | //----- host_info() --------------------- | |
17 | static const char* host_info(struct Scsi_Host *host) | |
18 | { | |
19 | //printk("scsiglue --- host_info\n"); | |
20 | return "SCSI emulation for USB Mass Storage devices"; | |
21 | } | |
22 | ||
23 | //----- slave_alloc() --------------------- | |
24 | static int slave_alloc(struct scsi_device *sdev) | |
25 | { | |
26 | struct us_data *us = host_to_us(sdev->host); | |
27 | ||
28 | //printk("scsiglue --- slave_alloc\n"); | |
29 | sdev->inquiry_len = 36; | |
30 | ||
31 | blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1)); | |
32 | ||
bceadddd | 33 | if (us->subclass == USB_SC_UFI) |
126bb03b AC |
34 | sdev->sdev_target->pdt_1f_for_no_lun = 1; |
35 | ||
36 | return 0; | |
37 | } | |
38 | ||
39 | //----- slave_configure() --------------------- | |
40 | static int slave_configure(struct scsi_device *sdev) | |
41 | { | |
42 | struct us_data *us = host_to_us(sdev->host); | |
43 | ||
44 | //printk("scsiglue --- slave_configure\n"); | |
45 | if (us->fflags & (US_FL_MAX_SECTORS_64 | US_FL_MAX_SECTORS_MIN)) | |
46 | { | |
47 | unsigned int max_sectors = 64; | |
48 | ||
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, | |
53 | max_sectors); | |
54 | } | |
55 | ||
56 | if (sdev->type == TYPE_DISK) | |
57 | { | |
bceadddd | 58 | if (us->subclass != USB_SC_SCSI && us->subclass != USB_SC_CYP_ATACB) |
126bb03b AC |
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; | |
73 | } | |
74 | else | |
75 | { | |
76 | sdev->use_10_for_ms = 1; | |
77 | } | |
78 | ||
bceadddd | 79 | if ((us->protocol == USB_PR_CB || us->protocol == USB_PR_CBI) && sdev->scsi_level == SCSI_UNKNOWN) |
126bb03b AC |
80 | us->max_lun = 0; |
81 | ||
82 | if (us->fflags & US_FL_NOT_LOCKABLE) | |
83 | sdev->lockable = 0; | |
84 | ||
85 | return 0; | |
86 | } | |
87 | ||
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 *)) | |
91 | { | |
92 | struct us_data *us = host_to_us(srb->device->host); | |
93 | ||
94 | //printk("scsiglue --- queuecommand\n"); | |
95 | ||
96 | /* check for state-transition errors */ | |
97 | if (us->srb != NULL) | |
98 | { | |
99 | printk("Error in %s: us->srb = %p\n", __FUNCTION__, us->srb); | |
100 | return SCSI_MLQUEUE_HOST_BUSY; | |
101 | } | |
102 | ||
103 | /* fail the command if we are disconnecting */ | |
104 | if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) | |
105 | { | |
106 | printk("Fail command during disconnect\n"); | |
107 | srb->result = DID_NO_CONNECT << 16; | |
108 | done(srb); | |
109 | return 0; | |
110 | } | |
111 | ||
112 | /* enqueue the command and wake up the control thread */ | |
113 | srb->scsi_done = done; | |
114 | us->srb = srb; | |
115 | complete(&us->cmnd_ready); | |
116 | ||
117 | return 0; | |
118 | } | |
119 | ||
120 | /*********************************************************************** | |
121 | * Error handling functions | |
122 | ***********************************************************************/ | |
123 | ||
124 | /* Command timeout and abort */ | |
125 | //----- command_abort() --------------------- | |
126 | static int command_abort(struct scsi_cmnd *srb) | |
127 | { | |
128 | struct us_data *us = host_to_us(srb->device->host); | |
129 | ||
130 | //printk("scsiglue --- command_abort\n"); | |
131 | ||
132 | scsi_lock(us_to_host(us)); | |
133 | if (us->srb != srb) | |
134 | { | |
135 | scsi_unlock(us_to_host(us)); | |
136 | printk ("-- nothing to abort\n"); | |
137 | return FAILED; | |
138 | } | |
139 | ||
140 | set_bit(US_FLIDX_TIMED_OUT, &us->dflags); | |
141 | if (!test_bit(US_FLIDX_RESETTING, &us->dflags)) | |
142 | { | |
143 | set_bit(US_FLIDX_ABORTING, &us->dflags); | |
144 | usb_stor_stop_transport(us); | |
145 | } | |
146 | scsi_unlock(us_to_host(us)); | |
147 | ||
148 | /* Wait for the aborted command to finish */ | |
149 | wait_for_completion(&us->notify); | |
150 | return SUCCESS; | |
151 | } | |
152 | ||
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) | |
156 | { | |
157 | struct us_data *us = host_to_us(srb->device->host); | |
158 | int result; | |
159 | ||
160 | //printk("scsiglue --- device_reset\n"); | |
161 | ||
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); | |
166 | ||
167 | return result < 0 ? FAILED : SUCCESS; | |
168 | } | |
169 | ||
170 | //----- bus_reset() --------------------- | |
171 | static int bus_reset(struct scsi_cmnd *srb) | |
172 | { | |
173 | struct us_data *us = host_to_us(srb->device->host); | |
174 | int result; | |
175 | ||
176 | //printk("scsiglue --- bus_reset\n"); | |
177 | result = usb_stor_port_reset(us); | |
178 | return result < 0 ? FAILED : SUCCESS; | |
179 | } | |
180 | ||
181 | //----- usb_stor_report_device_reset() --------------------- | |
182 | void usb_stor_report_device_reset(struct us_data *us) | |
183 | { | |
184 | int i; | |
185 | struct Scsi_Host *host = us_to_host(us); | |
186 | ||
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) | |
190 | { | |
191 | for (i = 1; i < host->max_id; ++i) | |
192 | scsi_report_device_reset(host, 0, i); | |
193 | } | |
194 | } | |
195 | ||
196 | //----- usb_stor_report_bus_reset() --------------------- | |
197 | void usb_stor_report_bus_reset(struct us_data *us) | |
198 | { | |
199 | struct Scsi_Host *host = us_to_host(us); | |
200 | ||
201 | //printk("scsiglue --- usb_stor_report_bus_reset\n"); | |
202 | scsi_lock(host); | |
203 | scsi_report_bus_reset(host, 0); | |
204 | scsi_unlock(host); | |
205 | } | |
206 | ||
207 | /*********************************************************************** | |
208 | * /proc/scsi/ functions | |
209 | ***********************************************************************/ | |
210 | ||
211 | /* we use this macro to help us write into the buffer */ | |
212 | #undef SPRINTF | |
213 | #define SPRINTF(args...) \ | |
214 | do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0) | |
215 | ||
216 | //----- proc_info() --------------------- | |
217 | static int proc_info (struct Scsi_Host *host, char *buffer, char **start, off_t offset, int length, int inout) | |
218 | { | |
219 | struct us_data *us = host_to_us(host); | |
220 | char *pos = buffer; | |
221 | const char *string; | |
222 | ||
223 | //printk("scsiglue --- proc_info\n"); | |
224 | if (inout) | |
225 | return length; | |
226 | ||
227 | /* print the controller name */ | |
228 | SPRINTF(" Host scsi%d: usb-storage\n", host->host_no); | |
229 | ||
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; | |
235 | else | |
236 | string = "Unknown"; | |
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; | |
242 | else | |
243 | string = "Unknown"; | |
244 | SPRINTF(" Product: %s\n", string); | |
245 | if (us->pusb_dev->serial) | |
246 | string = us->pusb_dev->serial; | |
247 | else | |
248 | string = "None"; | |
249 | SPRINTF("Serial Number: %s\n", string); | |
250 | ||
251 | /* show the protocol and transport */ | |
252 | SPRINTF(" Protocol: %s\n", us->protocol_name); | |
253 | SPRINTF(" Transport: %s\n", us->transport_name); | |
254 | ||
255 | /* show the device flags */ | |
256 | if (pos < buffer + length) | |
257 | { | |
258 | pos += sprintf(pos, " Quirks:"); | |
259 | ||
260 | #define US_FLAG(name, value) \ | |
261 | if (us->fflags & value) pos += sprintf(pos, " " #name); | |
262 | US_DO_ALL_FLAGS | |
263 | #undef US_FLAG | |
264 | ||
265 | *(pos++) = '\n'; | |
266 | } | |
267 | ||
268 | /* Calculate start of next buffer, and return value. */ | |
269 | *start = buffer + offset; | |
270 | ||
271 | if ((pos - buffer) < offset) | |
272 | return (0); | |
273 | else if ((pos - buffer - offset) < length) | |
274 | return (pos - buffer - offset); | |
275 | else | |
276 | return (length); | |
277 | } | |
278 | ||
279 | /*********************************************************************** | |
280 | * Sysfs interface | |
281 | ***********************************************************************/ | |
282 | ||
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) | |
286 | { | |
287 | struct scsi_device *sdev = to_scsi_device(dev); | |
288 | ||
289 | //printk("scsiglue --- ssize_t show_max_sectors\n"); | |
290 | return sprintf(buf, "%u\n", queue_max_sectors(sdev->request_queue)); | |
291 | } | |
292 | ||
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) | |
296 | { | |
297 | struct scsi_device *sdev = to_scsi_device(dev); | |
298 | unsigned short ms; | |
299 | ||
300 | //printk("scsiglue --- ssize_t store_max_sectors\n"); | |
301 | if (sscanf(buf, "%hu", &ms) > 0 && ms <= SCSI_DEFAULT_MAX_SECTORS) | |
302 | { | |
303 | blk_queue_max_hw_sectors(sdev->request_queue, ms); | |
304 | return strlen(buf); | |
305 | } | |
306 | return -EINVAL; | |
307 | } | |
308 | ||
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, }; | |
311 | ||
312 | /* this defines our host template, with which we'll allocate hosts */ | |
313 | ||
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, | |
320 | .info = host_info, | |
321 | ||
322 | /* command interface -- queued only */ | |
323 | .queuecommand = queuecommand, | |
324 | ||
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, | |
329 | ||
330 | /* queue commands only, only one command per LUN */ | |
331 | .can_queue = 1, | |
332 | .cmd_per_lun = 1, | |
333 | ||
334 | /* unknown initiator id */ | |
335 | .this_id = -1, | |
336 | ||
337 | .slave_alloc = slave_alloc, | |
338 | .slave_configure = slave_configure, | |
339 | ||
340 | /* lots of sg segments can be handled */ | |
341 | .sg_tablesize = SG_ALL, | |
342 | ||
343 | /* limit the total size of a transfer to 120 KB */ | |
344 | .max_sectors = 240, | |
345 | ||
346 | /* merge commands... this seems to help performance, but | |
347 | * periodically someone should test to see which setting is more | |
348 | * optimal. | |
349 | */ | |
350 | .use_clustering = 1, | |
351 | ||
352 | /* emulated HBA */ | |
353 | .emulated = 1, | |
354 | ||
355 | /* we do our own delay after a device or bus reset */ | |
356 | .skip_settle_delay = 1, | |
357 | ||
358 | /* sysfs device attributes */ | |
359 | .sdev_attrs = sysfs_device_attr_list, | |
360 | ||
361 | /* module management */ | |
362 | .module = THIS_MODULE | |
363 | }; | |
364 | ||
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 */ | |
371 | }; | |
372 | ||
373 | /*********************************************************************** | |
374 | * Scatter-gather transfer buffer access routines | |
375 | ***********************************************************************/ | |
376 | ||
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) | |
381 | { | |
382 | unsigned int cnt; | |
383 | ||
384 | //printk("transport --- usb_stor_access_xfer_buf\n"); | |
385 | struct scatterlist *sg = *sgptr; | |
386 | ||
387 | if (!sg) | |
388 | sg = scsi_sglist(srb); | |
389 | ||
390 | cnt = 0; | |
391 | while (cnt < buflen && sg) | |
392 | { | |
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; | |
396 | ||
397 | if (sglen > buflen - cnt) | |
398 | { | |
399 | /* Transfer ends within this s-g entry */ | |
400 | sglen = buflen - cnt; | |
401 | *offset += sglen; | |
402 | } | |
403 | else | |
404 | { | |
405 | /* Transfer continues to next s-g entry */ | |
406 | *offset = 0; | |
407 | sg = sg_next(sg); | |
408 | } | |
409 | ||
410 | while (sglen > 0) | |
411 | { | |
412 | unsigned int plen = min(sglen, (unsigned int)PAGE_SIZE - poff); | |
413 | unsigned char *ptr = kmap(page); | |
414 | ||
415 | if (dir == TO_XFER_BUF) | |
416 | memcpy(ptr + poff, buffer + cnt, plen); | |
417 | else | |
418 | memcpy(buffer + cnt, ptr + poff, plen); | |
419 | kunmap(page); | |
420 | ||
421 | /* Start at the beginning of the next page */ | |
422 | poff = 0; | |
423 | ++page; | |
424 | cnt += plen; | |
425 | sglen -= plen; | |
426 | } | |
427 | } | |
428 | *sgptr = sg; | |
429 | ||
430 | /* Return the amount actually transferred */ | |
431 | return cnt; | |
432 | } | |
433 | ||
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, | |
437 | unsigned int dir) | |
438 | { | |
439 | unsigned int offset = 0; | |
440 | struct scatterlist *sg = NULL; | |
441 | ||
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); | |
448 | } |