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