Merge remote-tracking branch 'cleancache/linux-next'
[linux-2.6/next.git] / drivers / staging / keucr / scsiglue.c
blob5e35d94c6a1b3bb5ce980389fcd9266bba35a0ae
1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3 #include <linux/slab.h>
4 #include <linux/module.h>
5 #include <linux/mutex.h>
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>
13 #include "usb.h"
14 #include "scsiglue.h"
15 #include "transport.h"
17 /* Host functions */
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;
38 return 0;
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,
55 max_sectors);
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;
76 else
78 sdev->use_10_for_ms = 1;
81 if ((us->protocol == USB_PR_CB || us->protocol == USB_PR_CBI) && sdev->scsi_level == SCSI_UNKNOWN)
82 us->max_lun = 0;
84 if (us->fflags & US_FL_NOT_LOCKABLE)
85 sdev->lockable = 0;
87 return 0;
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 */
99 if (us->srb != NULL)
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;
111 done(srb);
112 return 0;
115 /* enqueue the command and wake up the control thread */
116 srb->scsi_done = done;
117 us->srb = srb;
118 complete(&us->cmnd_ready);
120 return 0;
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));
138 if (us->srb != srb)
140 scsi_unlock(us_to_host(us));
141 printk ("-- nothing to abort\n");
142 return FAILED;
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);
155 return SUCCESS;
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);
163 int result;
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);
179 int result;
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)
189 int i;
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"); */
207 scsi_lock(host);
208 scsi_report_bus_reset(host, 0);
209 scsi_unlock(host);
212 /***********************************************************************
213 * /proc/scsi/ functions
214 ***********************************************************************/
216 /* we use this macro to help us write into the buffer */
217 #undef SPRINTF
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);
225 char *pos = buffer;
226 const char *string;
228 /* pr_info("scsiglue --- proc_info\n"); */
229 if (inout)
230 return length;
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;
240 else
241 string = "Unknown";
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;
247 else
248 string = "Unknown";
249 SPRINTF(" Product: %s\n", string);
250 if (us->pusb_dev->serial)
251 string = us->pusb_dev->serial;
252 else
253 string = "None";
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);
267 US_DO_ALL_FLAGS
268 #undef US_FLAG
270 *(pos++) = '\n';
273 /* Calculate start of next buffer, and return value. */
274 *start = buffer + offset;
276 if ((pos - buffer) < offset)
277 return (0);
278 else if ((pos - buffer - offset) < length)
279 return (pos - buffer - offset);
280 else
281 return (length);
284 /***********************************************************************
285 * Sysfs interface
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);
303 unsigned short ms;
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);
309 return strlen(buf);
311 return -EINVAL;
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,
325 .info = host_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 */
336 .can_queue = 1,
337 .cmd_per_lun = 1,
339 /* unknown initiator id */
340 .this_id = -1,
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 */
349 .max_sectors = 240,
351 /* merge commands... this seems to help performance, but
352 * periodically someone should test to see which setting is more
353 * optimal.
355 .use_clustering = 1,
357 /* emulated HBA */
358 .emulated = 1,
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)
387 unsigned int cnt;
389 /* pr_info("transport --- usb_stor_access_xfer_buf\n"); */
390 struct scatterlist *sg = *sgptr;
392 if (!sg)
393 sg = scsi_sglist(srb);
395 cnt = 0;
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;
406 *offset += sglen;
408 else
410 /* Transfer continues to next s-g entry */
411 *offset = 0;
412 sg = sg_next(sg);
415 while (sglen > 0)
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);
422 else
423 memcpy(buffer + cnt, ptr + poff, plen);
424 kunmap(page);
426 /* Start at the beginning of the next page */
427 poff = 0;
428 ++page;
429 cnt += plen;
430 sglen -= plen;
433 *sgptr = sg;
435 /* Return the amount actually transferred */
436 return cnt;
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,
442 unsigned int dir)
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);