blob: ac3d34dcc4382936535d932e6a82fc4b9d15a47b [file] [log] [blame]
Cho, Yu-Chenbeddfe82011-04-01 14:38:41 +08001#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2
Al Cho126bb032010-09-08 00:42:32 -07003#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/* Host functions */
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +080018/*
19 * host_info()
20 */
21static const char *host_info(struct Scsi_Host *host)
Al Cho126bb032010-09-08 00:42:32 -070022{
Cho, Yu-Chenbeddfe82011-04-01 14:38:41 +080023 /* pr_info("scsiglue --- host_info\n"); */
Al Cho126bb032010-09-08 00:42:32 -070024 return "SCSI emulation for USB Mass Storage devices";
25}
26
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +080027/*
28 * slave_alloc()
29 */
Al Cho126bb032010-09-08 00:42:32 -070030static int slave_alloc(struct scsi_device *sdev)
31{
32 struct us_data *us = host_to_us(sdev->host);
33
Cho, Yu-Chenbeddfe82011-04-01 14:38:41 +080034 /* pr_info("scsiglue --- slave_alloc\n"); */
Al Cho126bb032010-09-08 00:42:32 -070035 sdev->inquiry_len = 36;
36
37 blk_queue_update_dma_alignment(sdev->request_queue, (512 - 1));
38
Greg Kroah-Hartmanbceaddd2010-10-28 09:53:38 -070039 if (us->subclass == USB_SC_UFI)
Al Cho126bb032010-09-08 00:42:32 -070040 sdev->sdev_target->pdt_1f_for_no_lun = 1;
41
42 return 0;
43}
44
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +080045/*
46 * slave_configure()
47 */
Al Cho126bb032010-09-08 00:42:32 -070048static int slave_configure(struct scsi_device *sdev)
49{
50 struct us_data *us = host_to_us(sdev->host);
51
Cho, Yu-Chenbeddfe82011-04-01 14:38:41 +080052 /* pr_info("scsiglue --- slave_configure\n"); */
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +080053 if (us->fflags & (US_FL_MAX_SECTORS_64 | US_FL_MAX_SECTORS_MIN)) {
Al Cho126bb032010-09-08 00:42:32 -070054 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
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +080063 if (sdev->type == TYPE_DISK) {
64 if (us->subclass != USB_SC_SCSI &&
65 us->subclass != USB_SC_CYP_ATACB)
Al Cho126bb032010-09-08 00:42:32 -070066 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)
Amarjargal Gundjalamb9594b82013-05-17 01:06:49 -070076 sdev->sdev_target->scsi_level = sdev->scsi_level
77 = SCSI_2;
Al Cho126bb032010-09-08 00:42:32 -070078 sdev->retry_hwerror = 1;
79 sdev->allow_restart = 1;
80 sdev->last_sector_bug = 1;
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +080081 } else {
Al Cho126bb032010-09-08 00:42:32 -070082 sdev->use_10_for_ms = 1;
83 }
84
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +080085 if ((us->protocol == USB_PR_CB || us->protocol == USB_PR_CBI) &&
86 sdev->scsi_level == SCSI_UNKNOWN)
Al Cho126bb032010-09-08 00:42:32 -070087 us->max_lun = 0;
88
89 if (us->fflags & US_FL_NOT_LOCKABLE)
90 sdev->lockable = 0;
91
92 return 0;
93}
94
95/* This is always called with scsi_lock(host) held */
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +080096/*
97 * queuecommand()
98 */
99static int queuecommand_lck(struct scsi_cmnd *srb,
100 void (*done)(struct scsi_cmnd *))
Al Cho126bb032010-09-08 00:42:32 -0700101{
102 struct us_data *us = host_to_us(srb->device->host);
103
Cho, Yu-Chenbeddfe82011-04-01 14:38:41 +0800104 /* pr_info("scsiglue --- queuecommand\n"); */
Al Cho126bb032010-09-08 00:42:32 -0700105
106 /* check for state-transition errors */
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +0800107 if (us->srb != NULL) {
Cho, Yu-Chenbeddfe82011-04-01 14:38:41 +0800108 /* pr_info("Error in %s: us->srb = %p\n"
109 __FUNCTION__, us->srb); */
Al Cho126bb032010-09-08 00:42:32 -0700110 return SCSI_MLQUEUE_HOST_BUSY;
111 }
112
113 /* fail the command if we are disconnecting */
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +0800114 if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) {
Cho, Yu-Chenbeddfe82011-04-01 14:38:41 +0800115 pr_info("Fail command during disconnect\n");
Al Cho126bb032010-09-08 00:42:32 -0700116 srb->result = DID_NO_CONNECT << 16;
117 done(srb);
118 return 0;
119 }
120
121 /* enqueue the command and wake up the control thread */
122 srb->scsi_done = done;
123 us->srb = srb;
124 complete(&us->cmnd_ready);
125
126 return 0;
127}
128
Jeff Garzikf2812332010-11-16 02:10:29 -0500129static DEF_SCSI_QCMD(queuecommand)
130
Al Cho126bb032010-09-08 00:42:32 -0700131/***********************************************************************
132 * Error handling functions
133 ***********************************************************************/
134
135/* Command timeout and abort */
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +0800136/*
137 * command_abort()
138 */
Al Cho126bb032010-09-08 00:42:32 -0700139static int command_abort(struct scsi_cmnd *srb)
140{
141 struct us_data *us = host_to_us(srb->device->host);
142
Cho, Yu-Chenbeddfe82011-04-01 14:38:41 +0800143 /* pr_info("scsiglue --- command_abort\n"); */
Al Cho126bb032010-09-08 00:42:32 -0700144
145 scsi_lock(us_to_host(us));
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +0800146 if (us->srb != srb) {
Al Cho126bb032010-09-08 00:42:32 -0700147 scsi_unlock(us_to_host(us));
Laura Lawniczak29b31422013-06-06 18:10:47 +0200148 dev_info(&us->pusb_dev->dev, "-- nothing to abort\n");
Al Cho126bb032010-09-08 00:42:32 -0700149 return FAILED;
150 }
151
152 set_bit(US_FLIDX_TIMED_OUT, &us->dflags);
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +0800153 if (!test_bit(US_FLIDX_RESETTING, &us->dflags)) {
Al Cho126bb032010-09-08 00:42:32 -0700154 set_bit(US_FLIDX_ABORTING, &us->dflags);
155 usb_stor_stop_transport(us);
156 }
157 scsi_unlock(us_to_host(us));
158
159 /* Wait for the aborted command to finish */
160 wait_for_completion(&us->notify);
161 return SUCCESS;
162}
163
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +0800164/* This invokes the transport reset mechanism to reset the state of the
165 * device.
166 */
167/*
168 * device_reset()
169 */
Al Cho126bb032010-09-08 00:42:32 -0700170static int device_reset(struct scsi_cmnd *srb)
171{
172 struct us_data *us = host_to_us(srb->device->host);
173 int result;
174
Cho, Yu-Chenbeddfe82011-04-01 14:38:41 +0800175 /* pr_info("scsiglue --- device_reset\n"); */
Al Cho126bb032010-09-08 00:42:32 -0700176
177 /* lock the device pointers and do the reset */
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
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +0800185/*
186 * bus_reset()
187 */
Al Cho126bb032010-09-08 00:42:32 -0700188static int bus_reset(struct scsi_cmnd *srb)
189{
190 struct us_data *us = host_to_us(srb->device->host);
191 int result;
192
Cho, Yu-Chenbeddfe82011-04-01 14:38:41 +0800193 /* pr_info("scsiglue --- bus_reset\n"); */
Al Cho126bb032010-09-08 00:42:32 -0700194 result = usb_stor_port_reset(us);
195 return result < 0 ? FAILED : SUCCESS;
196}
197
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +0800198/*
199 * usb_stor_report_device_reset()
200 */
Al Cho126bb032010-09-08 00:42:32 -0700201void usb_stor_report_device_reset(struct us_data *us)
202{
203 int i;
204 struct Scsi_Host *host = us_to_host(us);
205
Cho, Yu-Chenbeddfe82011-04-01 14:38:41 +0800206 /* pr_info("scsiglue --- usb_stor_report_device_reset\n"); */
Al Cho126bb032010-09-08 00:42:32 -0700207 scsi_report_device_reset(host, 0, 0);
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +0800208 if (us->fflags & US_FL_SCM_MULT_TARG) {
Al Cho126bb032010-09-08 00:42:32 -0700209 for (i = 1; i < host->max_id; ++i)
210 scsi_report_device_reset(host, 0, i);
211 }
212}
213
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +0800214/*
215 * usb_stor_report_bus_reset()
216 */
Al Cho126bb032010-09-08 00:42:32 -0700217void usb_stor_report_bus_reset(struct us_data *us)
218{
219 struct Scsi_Host *host = us_to_host(us);
220
Cho, Yu-Chenbeddfe82011-04-01 14:38:41 +0800221 /* pr_info("scsiglue --- usb_stor_report_bus_reset\n"); */
Al Cho126bb032010-09-08 00:42:32 -0700222 scsi_lock(host);
223 scsi_report_bus_reset(host, 0);
224 scsi_unlock(host);
225}
226
227/***********************************************************************
228 * /proc/scsi/ functions
229 ***********************************************************************/
230
231/* we use this macro to help us write into the buffer */
232#undef SPRINTF
Al Viro60e8b802013-03-31 02:08:31 -0400233#define SPRINTF(args...) seq_printf(m, ##args)
Al Cho126bb032010-09-08 00:42:32 -0700234
Al Viro60e8b802013-03-31 02:08:31 -0400235static 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)
Al Cho126bb032010-09-08 00:42:32 -0700241{
242 struct us_data *us = host_to_us(host);
Al Cho126bb032010-09-08 00:42:32 -0700243 const char *string;
244
Al Cho126bb032010-09-08 00:42:32 -0700245 /* print the controller name */
246 SPRINTF(" Host scsi%d: usb-storage\n", host->host_no);
247
248 /* print product, vendor, and serial number strings */
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 /* show the protocol and transport */
270 SPRINTF(" Protocol: %s\n", us->protocol_name);
271 SPRINTF(" Transport: %s\n", us->transport_name);
272
273 /* show the device flags */
Al Viro60e8b802013-03-31 02:08:31 -0400274 SPRINTF(" Quirks:");
Al Cho126bb032010-09-08 00:42:32 -0700275
276#define US_FLAG(name, value) \
William Blaira2c49a92012-06-13 01:16:49 -0400277 do { \
278 if (us->fflags & value) \
Al Viro60e8b802013-03-31 02:08:31 -0400279 SPRINTF(" " #name); \
William Blaira2c49a92012-06-13 01:16:49 -0400280 } while (0);
Al Cho126bb032010-09-08 00:42:32 -0700281US_DO_ALL_FLAGS
282#undef US_FLAG
Al Viro60e8b802013-03-31 02:08:31 -0400283 seq_putc(m, '\n');
284 return 0;
Al Cho126bb032010-09-08 00:42:32 -0700285}
286
287/***********************************************************************
288 * Sysfs interface
289 ***********************************************************************/
290
291/* Output routine for the sysfs max_sectors file */
Greg Kroah-Hartman00009612013-08-24 10:33:07 -0700292static ssize_t max_sectors_show(struct device *dev,
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +0800293 struct device_attribute *attr, char *buf)
Al Cho126bb032010-09-08 00:42:32 -0700294{
295 struct scsi_device *sdev = to_scsi_device(dev);
296
Cho, Yu-Chenbeddfe82011-04-01 14:38:41 +0800297 /* pr_info("scsiglue --- ssize_t show_max_sectors\n"); */
Al Cho126bb032010-09-08 00:42:32 -0700298 return sprintf(buf, "%u\n", queue_max_sectors(sdev->request_queue));
299}
300
301/* Input routine for the sysfs max_sectors file */
Greg Kroah-Hartman00009612013-08-24 10:33:07 -0700302static ssize_t max_sectors_store(struct device *dev,
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +0800303 struct device_attribute *attr,
304 const char *buf, size_t count)
Al Cho126bb032010-09-08 00:42:32 -0700305{
306 struct scsi_device *sdev = to_scsi_device(dev);
307 unsigned short ms;
308
Cho, Yu-Chenbeddfe82011-04-01 14:38:41 +0800309 /* pr_info("scsiglue --- ssize_t store_max_sectors\n"); */
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +0800310 if (sscanf(buf, "%hu", &ms) > 0 && ms <= SCSI_DEFAULT_MAX_SECTORS) {
Al Cho126bb032010-09-08 00:42:32 -0700311 blk_queue_max_hw_sectors(sdev->request_queue, ms);
312 return strlen(buf);
313 }
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +0800314 return -EINVAL;
Al Cho126bb032010-09-08 00:42:32 -0700315}
Greg Kroah-Hartman00009612013-08-24 10:33:07 -0700316static DEVICE_ATTR_RW(max_sectors);
Al Cho126bb032010-09-08 00:42:32 -0700317
Johannes Schilling0ea8a162013-06-06 18:10:50 +0200318static struct device_attribute *sysfs_device_attr_list[] = {
319 &dev_attr_max_sectors, NULL,
320};
Al Cho126bb032010-09-08 00:42:32 -0700321
322/* this defines our host template, with which we'll allocate hosts */
323
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +0800324/*
325 * usb_stor_host_template()
326 */
Al Cho126bb032010-09-08 00:42:32 -0700327struct scsi_host_template usb_stor_host_template = {
328 /* basic userland interface stuff */
329 .name = "eucr-storage",
330 .proc_name = "eucr-storage",
Al Viro60e8b802013-03-31 02:08:31 -0400331 .write_info = write_info,
332 .show_info = show_info,
Al Cho126bb032010-09-08 00:42:32 -0700333 .info = host_info,
334
335 /* command interface -- queued only */
336 .queuecommand = queuecommand,
337
338 /* error and abort handlers */
339 .eh_abort_handler = command_abort,
340 .eh_device_reset_handler = device_reset,
341 .eh_bus_reset_handler = bus_reset,
342
343 /* queue commands only, only one command per LUN */
344 .can_queue = 1,
345 .cmd_per_lun = 1,
346
347 /* unknown initiator id */
348 .this_id = -1,
349
350 .slave_alloc = slave_alloc,
351 .slave_configure = slave_configure,
352
353 /* lots of sg segments can be handled */
354 .sg_tablesize = SG_ALL,
355
356 /* limit the total size of a transfer to 120 KB */
357 .max_sectors = 240,
358
359 /* merge commands... this seems to help performance, but
360 * periodically someone should test to see which setting is more
361 * optimal.
362 */
363 .use_clustering = 1,
364
365 /* emulated HBA */
366 .emulated = 1,
367
368 /* we do our own delay after a device or bus reset */
369 .skip_settle_delay = 1,
370
371 /* sysfs device attributes */
372 .sdev_attrs = sysfs_device_attr_list,
373
374 /* module management */
375 .module = THIS_MODULE
376};
377
378/* To Report "Illegal Request: Invalid Field in CDB */
379unsigned char usb_stor_sense_invalidCDB[18] = {
380 [0] = 0x70, /* current error */
381 [2] = ILLEGAL_REQUEST, /* Illegal Request = 0x05 */
382 [7] = 0x0a, /* additional length */
383 [12] = 0x24 /* Invalid Field in CDB */
384};
385
386/***********************************************************************
387 * Scatter-gather transfer buffer access routines
388 ***********************************************************************/
389
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +0800390/*
391 * usb_stor_access_xfer_buf()
392 */
Amarjargal Gundjalamb9594b82013-05-17 01:06:49 -0700393unsigned int usb_stor_access_xfer_buf(struct us_data *us,
394 unsigned char *buffer, unsigned int buflen,
395 struct scsi_cmnd *srb, struct scatterlist **sgptr,
Al Cho126bb032010-09-08 00:42:32 -0700396 unsigned int *offset, enum xfer_buf_dir dir)
397{
398 unsigned int cnt;
399
Cho, Yu-Chenbeddfe82011-04-01 14:38:41 +0800400 /* pr_info("transport --- usb_stor_access_xfer_buf\n"); */
Al Cho126bb032010-09-08 00:42:32 -0700401 struct scatterlist *sg = *sgptr;
402
403 if (!sg)
404 sg = scsi_sglist(srb);
405
406 cnt = 0;
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +0800407 while (cnt < buflen && sg) {
408 struct page *page = sg_page(sg) +
409 ((sg->offset + *offset) >> PAGE_SHIFT);
Al Cho126bb032010-09-08 00:42:32 -0700410 unsigned int poff = (sg->offset + *offset) & (PAGE_SIZE-1);
411 unsigned int sglen = sg->length - *offset;
412
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +0800413 if (sglen > buflen - cnt) {
Al Cho126bb032010-09-08 00:42:32 -0700414 /* Transfer ends within this s-g entry */
415 sglen = buflen - cnt;
416 *offset += sglen;
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +0800417 } else {
Al Cho126bb032010-09-08 00:42:32 -0700418 /* Transfer continues to next s-g entry */
419 *offset = 0;
420 sg = sg_next(sg);
421 }
422
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +0800423 while (sglen > 0) {
424 unsigned int plen = min(sglen,
Amarjargal Gundjalamb9594b82013-05-17 01:06:49 -0700425 (unsigned int)PAGE_SIZE - poff);
Al Cho126bb032010-09-08 00:42:32 -0700426 unsigned char *ptr = kmap(page);
427
428 if (dir == TO_XFER_BUF)
429 memcpy(ptr + poff, buffer + cnt, plen);
430 else
431 memcpy(buffer + cnt, ptr + poff, plen);
432 kunmap(page);
433
434 /* Start at the beginning of the next page */
435 poff = 0;
436 ++page;
437 cnt += plen;
438 sglen -= plen;
439 }
440 }
441 *sgptr = sg;
442
443 /* Return the amount actually transferred */
444 return cnt;
445}
446
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +0800447/*
448 * Store the contents of buffer into srb's transfer
449 * buffer and set the SCSI residue.
450 */
451/*
452 * usb_stor_set_xfer_buf()
453 */
454void usb_stor_set_xfer_buf(struct us_data *us, unsigned char *buffer,
455 unsigned int buflen, struct scsi_cmnd *srb, unsigned int dir)
Al Cho126bb032010-09-08 00:42:32 -0700456{
457 unsigned int offset = 0;
458 struct scatterlist *sg = NULL;
459
Cho, Yu-Chenbeddfe82011-04-01 14:38:41 +0800460 /* pr_info("transport --- usb_stor_set_xfer_buf\n"); */
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +0800461 /* TO_XFER_BUF = 0, FROM_XFER_BUF = 1 */
Al Cho126bb032010-09-08 00:42:32 -0700462 buflen = min(buflen, scsi_bufflen(srb));
Cho, Yu-Chen8a25a2c2011-05-18 18:40:11 +0800463 buflen = usb_stor_access_xfer_buf(us, buffer, buflen, srb,
464 &sg, &offset, dir);
Al Cho126bb032010-09-08 00:42:32 -0700465 if (buflen < scsi_bufflen(srb))
466 scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
467}