1#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2
3#include <linux/slab.h>
4#include <linux/module.h>
5#include <linux/mutex.h>
6
7#include <scsi/scsi.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>
12
13#include "usb.h"
14#include "scsiglue.h"
15#include "transport.h"
16
17
18
19
20
21static const char *host_info(struct Scsi_Host *host)
22{
23
24 return "SCSI emulation for USB Mass Storage devices";
25}
26
27
28
29
30static int slave_alloc(struct scsi_device *sdev)
31{
32 struct us_data *us = host_to_us(sdev->host);
33
34
35 sdev->inquiry_len = 36;
36
37 blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
38
39 if (us->subclass == USB_SC_UFI)
40 sdev->sdev_target->pdt_1f_for_no_lun = 1;
41
42 return 0;
43}
44
45
46
47
48static int slave_configure(struct scsi_device *sdev)
49{
50 struct us_data *us = host_to_us(sdev->host);
51
52
53 if (us->fflags & (US_FL_MAX_SECTORS_64 | US_FL_MAX_SECTORS_MIN)) {
54 unsigned int max_sectors = 64;
55
56 if (us->fflags & US_FL_MAX_SECTORS_MIN)
57 max_sectors = PAGE_CACHE_SIZE >> 9;
58 if (queue_max_sectors(sdev->request_queue) > max_sectors)
59 blk_queue_max_hw_sectors(sdev->request_queue,
60 max_sectors);
61 }
62
63 if (sdev->type == TYPE_DISK) {
64 if (us->subclass != USB_SC_SCSI &&
65 us->subclass != USB_SC_CYP_ATACB)
66 sdev->use_10_for_ms = 1;
67 sdev->use_192_bytes_for_3f = 1;
68 if (us->fflags & US_FL_NO_WP_DETECT)
69 sdev->skip_ms_page_3f = 1;
70 sdev->skip_ms_page_8 = 1;
71 if (us->fflags & US_FL_FIX_CAPACITY)
72 sdev->fix_capacity = 1;
73 if (us->fflags & US_FL_CAPACITY_HEURISTICS)
74 sdev->guess_capacity = 1;
75 if (sdev->scsi_level > SCSI_2)
76 sdev->sdev_target->scsi_level = sdev->scsi_level = SCSI_2;
77 sdev->retry_hwerror = 1;
78 sdev->allow_restart = 1;
79 sdev->last_sector_bug = 1;
80 } else {
81 sdev->use_10_for_ms = 1;
82 }
83
84 if ((us->protocol == USB_PR_CB || us->protocol == USB_PR_CBI) &&
85 sdev->scsi_level == SCSI_UNKNOWN)
86 us->max_lun = 0;
87
88 if (us->fflags & US_FL_NOT_LOCKABLE)
89 sdev->lockable = 0;
90
91 return 0;
92}
93
94
95
96
97
98static int queuecommand_lck(struct scsi_cmnd *srb,
99 void (*done)(struct scsi_cmnd *))
100{
101 struct us_data *us = host_to_us(srb->device->host);
102
103
104
105
106 if (us->srb != NULL) {
107
108
109 return SCSI_MLQUEUE_HOST_BUSY;
110 }
111
112
113 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
114 pr_info("Fail command during disconnect\n");
115 srb->result = DID_NO_CONNECT << 16;
116 done(srb);
117 return 0;
118 }
119
120
121 srb->scsi_done = done;
122 us->srb = srb;
123 complete(&us->cmnd_ready);
124
125 return 0;
126}
127
128static DEF_SCSI_QCMD(queuecommand)
129
130
131
132
133
134
135
136
137
138static int command_abort(struct scsi_cmnd *srb)
139{
140 struct us_data *us = host_to_us(srb->device->host);
141
142
143
144 scsi_lock(us_to_host(us));
145 if (us->srb != srb) {
146 scsi_unlock(us_to_host(us));
147 printk("-- nothing to abort\n");
148 return FAILED;
149 }
150
151 set_bit(US_FLIDX_TIMED_OUT, &us->dflags);
152 if (!test_bit(US_FLIDX_RESETTING, &us->dflags)) {
153 set_bit(US_FLIDX_ABORTING, &us->dflags);
154 usb_stor_stop_transport(us);
155 }
156 scsi_unlock(us_to_host(us));
157
158
159 wait_for_completion(&us->notify);
160 return SUCCESS;
161}
162
163
164
165
166
167
168
169static int device_reset(struct scsi_cmnd *srb)
170{
171 struct us_data *us = host_to_us(srb->device->host);
172 int result;
173
174
175
176
177 mutex_lock(&(us->dev_mutex));
178 result = us->transport_reset(us);
179 mutex_unlock(&us->dev_mutex);
180
181 return result < 0 ? FAILED : SUCCESS;
182}
183
184
185
186
187static int bus_reset(struct scsi_cmnd *srb)
188{
189 struct us_data *us = host_to_us(srb->device->host);
190 int result;
191
192
193 result = usb_stor_port_reset(us);
194 return result < 0 ? FAILED : SUCCESS;
195}
196
197
198
199
200void usb_stor_report_device_reset(struct us_data *us)
201{
202 int i;
203 struct Scsi_Host *host = us_to_host(us);
204
205
206 scsi_report_device_reset(host, 0, 0);
207 if (us->fflags & US_FL_SCM_MULT_TARG) {
208 for (i = 1; i < host->max_id; ++i)
209 scsi_report_device_reset(host, 0, i);
210 }
211}
212
213
214
215
216void usb_stor_report_bus_reset(struct us_data *us)
217{
218 struct Scsi_Host *host = us_to_host(us);
219
220
221 scsi_lock(host);
222 scsi_report_bus_reset(host, 0);
223 scsi_unlock(host);
224}
225
226
227
228
229
230
231#undef SPRINTF
232#define SPRINTF(args...) seq_printf(m, ##args)
233
234static int write_info(struct Scsi_Host *host, char *buffer, int length)
235{
236 return length;
237}
238
239static int show_info(struct seq_file *m, struct Scsi_Host *host)
240{
241 struct us_data *us = host_to_us(host);
242 const char *string;
243
244
245 SPRINTF(" Host scsi%d: usb-storage\n", host->host_no);
246
247
248 if (us->pusb_dev->manufacturer)
249 string = us->pusb_dev->manufacturer;
250 else if (us->unusual_dev->vendorName)
251 string = us->unusual_dev->vendorName;
252 else
253 string = "Unknown";
254 SPRINTF(" Vendor: %s\n", string);
255 if (us->pusb_dev->product)
256 string = us->pusb_dev->product;
257 else if (us->unusual_dev->productName)
258 string = us->unusual_dev->productName;
259 else
260 string = "Unknown";
261 SPRINTF(" Product: %s\n", string);
262 if (us->pusb_dev->serial)
263 string = us->pusb_dev->serial;
264 else
265 string = "None";
266 SPRINTF("Serial Number: %s\n", string);
267
268
269 SPRINTF(" Protocol: %s\n", us->protocol_name);
270 SPRINTF(" Transport: %s\n", us->transport_name);
271
272
273 SPRINTF(" Quirks:");
274
275#define US_FLAG(name, value) \
276 do { \
277 if (us->fflags & value) \
278 SPRINTF(" " #name); \
279 } while (0);
280US_DO_ALL_FLAGS
281#undef US_FLAG
282 seq_putc(m, '\n');
283 return 0;
284}
285
286
287
288
289
290
291
292
293
294static ssize_t show_max_sectors(struct device *dev,
295 struct device_attribute *attr, char *buf)
296{
297 struct scsi_device *sdev = to_scsi_device(dev);
298
299
300 return sprintf(buf, "%u\n", queue_max_sectors(sdev->request_queue));
301}
302
303
304
305
306
307static ssize_t store_max_sectors(struct device *dev,
308 struct device_attribute *attr,
309 const char *buf, size_t count)
310{
311 struct scsi_device *sdev = to_scsi_device(dev);
312 unsigned short ms;
313
314
315 if (sscanf(buf, "%hu", &ms) > 0 && ms <= SCSI_DEFAULT_MAX_SECTORS) {
316 blk_queue_max_hw_sectors(sdev->request_queue, ms);
317 return strlen(buf);
318 }
319 return -EINVAL;
320}
321
322static DEVICE_ATTR(max_sectors, S_IRUGO | S_IWUSR, show_max_sectors, store_max_sectors);
323static struct device_attribute *sysfs_device_attr_list[] = {&dev_attr_max_sectors, NULL, };
324
325
326
327
328
329
330struct scsi_host_template usb_stor_host_template = {
331
332 .name = "eucr-storage",
333 .proc_name = "eucr-storage",
334 .write_info = write_info,
335 .show_info = show_info,
336 .info = host_info,
337
338
339 .queuecommand = queuecommand,
340
341
342 .eh_abort_handler = command_abort,
343 .eh_device_reset_handler = device_reset,
344 .eh_bus_reset_handler = bus_reset,
345
346
347 .can_queue = 1,
348 .cmd_per_lun = 1,
349
350
351 .this_id = -1,
352
353 .slave_alloc = slave_alloc,
354 .slave_configure = slave_configure,
355
356
357 .sg_tablesize = SG_ALL,
358
359
360 .max_sectors = 240,
361
362
363
364
365
366 .use_clustering = 1,
367
368
369 .emulated = 1,
370
371
372 .skip_settle_delay = 1,
373
374
375 .sdev_attrs = sysfs_device_attr_list,
376
377
378 .module = THIS_MODULE
379};
380
381
382unsigned char usb_stor_sense_invalidCDB[18] = {
383 [0] = 0x70,
384 [2] = ILLEGAL_REQUEST,
385 [7] = 0x0a,
386 [12] = 0x24
387};
388
389
390
391
392
393
394
395
396unsigned int usb_stor_access_xfer_buf(struct us_data *us, unsigned char *buffer,
397 unsigned int buflen, struct scsi_cmnd *srb, struct scatterlist **sgptr,
398 unsigned int *offset, enum xfer_buf_dir dir)
399{
400 unsigned int cnt;
401
402
403 struct scatterlist *sg = *sgptr;
404
405 if (!sg)
406 sg = scsi_sglist(srb);
407
408 cnt = 0;
409 while (cnt < buflen && sg) {
410 struct page *page = sg_page(sg) +
411 ((sg->offset + *offset) >> PAGE_SHIFT);
412 unsigned int poff = (sg->offset + *offset) & (PAGE_SIZE-1);
413 unsigned int sglen = sg->length - *offset;
414
415 if (sglen > buflen - cnt) {
416
417 sglen = buflen - cnt;
418 *offset += sglen;
419 } else {
420
421 *offset = 0;
422 sg = sg_next(sg);
423 }
424
425 while (sglen > 0) {
426 unsigned int plen = min(sglen,
427 (unsigned int)PAGE_SIZE - poff);
428 unsigned char *ptr = kmap(page);
429
430 if (dir == TO_XFER_BUF)
431 memcpy(ptr + poff, buffer + cnt, plen);
432 else
433 memcpy(buffer + cnt, ptr + poff, plen);
434 kunmap(page);
435
436
437 poff = 0;
438 ++page;
439 cnt += plen;
440 sglen -= plen;
441 }
442 }
443 *sgptr = sg;
444
445
446 return cnt;
447}
448
449
450
451
452
453
454
455
456void usb_stor_set_xfer_buf(struct us_data *us, unsigned char *buffer,
457 unsigned int buflen, struct scsi_cmnd *srb, unsigned int dir)
458{
459 unsigned int offset = 0;
460 struct scatterlist *sg = NULL;
461
462
463
464 buflen = min(buflen, scsi_bufflen(srb));
465 buflen = usb_stor_access_xfer_buf(us, buffer, buflen, srb,
466 &sg, &offset, dir);
467 if (buflen < scsi_bufflen(srb))
468 scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
469}
470