blob: fcec0225ac764abf81c6a5038df3d4e319ca6fa8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * The low performance USB storage driver (ub).
3 *
4 * Copyright (c) 1999, 2000 Matthew Dharm (mdharm-usb@one-eyed-alien.net)
5 * Copyright (C) 2004 Pete Zaitcev (zaitcev@yahoo.com)
6 *
7 * This work is a part of Linux kernel, is derived from it,
8 * and is not licensed separately. See file COPYING for details.
9 *
10 * TODO (sorted by decreasing priority)
Pete Zaitcevef45cb62008-04-08 17:41:51 -070011 * -- Return sense now that rq allows it (we always auto-sense anyway).
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * -- set readonly flag for CDs, set removable flag for CF readers
13 * -- do inquiry and verify we got a disk and not a tape (for LUN mismatch)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * -- verify the 13 conditions and do bulk resets
Pete Zaitcevba6abf12005-07-30 22:38:30 -070015 * -- highmem
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * -- move top_sense and work_bcs into separate allocations (if they survive)
17 * for cache purists and esoteric architectures.
Pete Zaitcevba6abf12005-07-30 22:38:30 -070018 * -- Allocate structure for LUN 0 before the first ub_sync_tur, avoid NULL. ?
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * -- prune comments, they are too volumnous
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * -- Resove XXX's
Pete Zaitcev1872bce2005-07-27 11:43:51 -070021 * -- CLEAR, CLR2STS, CLRRS seem to be ripe for refactoring.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/usb.h>
Pete Zaitceva00828e2005-10-22 20:15:09 -070026#include <linux/usb_usual.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/timer.h>
Jens Axboe45711f12007-10-22 21:19:53 +020029#include <linux/scatterlist.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020031#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <scsi/scsi.h>
33
34#define DRV_NAME "ub"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#define UB_MAJOR 180
37
38/*
Pete Zaitcev1872bce2005-07-27 11:43:51 -070039 * The command state machine is the key model for understanding of this driver.
40 *
41 * The general rule is that all transitions are done towards the bottom
42 * of the diagram, thus preventing any loops.
43 *
44 * An exception to that is how the STAT state is handled. A counter allows it
45 * to be re-entered along the path marked with [C].
46 *
47 * +--------+
48 * ! INIT !
49 * +--------+
50 * !
51 * ub_scsi_cmd_start fails ->--------------------------------------\
52 * ! !
53 * V !
54 * +--------+ !
55 * ! CMD ! !
56 * +--------+ !
57 * ! +--------+ !
58 * was -EPIPE -->-------------------------------->! CLEAR ! !
59 * ! +--------+ !
60 * ! ! !
61 * was error -->------------------------------------- ! --------->\
62 * ! ! !
63 * /--<-- cmd->dir == NONE ? ! !
64 * ! ! ! !
65 * ! V ! !
66 * ! +--------+ ! !
67 * ! ! DATA ! ! !
68 * ! +--------+ ! !
69 * ! ! +---------+ ! !
70 * ! was -EPIPE -->--------------->! CLR2STS ! ! !
71 * ! ! +---------+ ! !
72 * ! ! ! ! !
73 * ! ! was error -->---- ! --------->\
74 * ! was error -->--------------------- ! ------------- ! --------->\
75 * ! ! ! ! !
76 * ! V ! ! !
77 * \--->+--------+ ! ! !
78 * ! STAT !<--------------------------/ ! !
79 * /--->+--------+ ! !
80 * ! ! ! !
81 * [C] was -EPIPE -->-----------\ ! !
82 * ! ! ! ! !
83 * +<---- len == 0 ! ! !
84 * ! ! ! ! !
85 * ! was error -->--------------------------------------!---------->\
86 * ! ! ! ! !
87 * +<---- bad CSW ! ! !
88 * +<---- bad tag ! ! !
89 * ! ! V ! !
90 * ! ! +--------+ ! !
91 * ! ! ! CLRRS ! ! !
92 * ! ! +--------+ ! !
93 * ! ! ! ! !
94 * \------- ! --------------------[C]--------\ ! !
95 * ! ! ! !
96 * cmd->error---\ +--------+ ! !
97 * ! +--------------->! SENSE !<----------/ !
98 * STAT_FAIL----/ +--------+ !
99 * ! ! V
100 * ! V +--------+
101 * \--------------------------------\--------------------->! DONE !
102 * +--------+
103 */
104
105/*
Pete Zaitcevf4800072005-05-01 16:05:40 -0700106 * This many LUNs per USB device.
107 * Every one of them takes a host, see UB_MAX_HOSTS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 */
Pete Zaitcev9f793d22005-06-06 13:54:59 -0700109#define UB_MAX_LUNS 9
Pete Zaitcevf4800072005-05-01 16:05:40 -0700110
111/*
112 */
113
Pete Zaitcev4fb729f2005-12-17 02:34:12 -0800114#define UB_PARTS_PER_LUN 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116#define UB_MAX_CDB_SIZE 16 /* Corresponds to Bulk */
117
118#define UB_SENSE_SIZE 18
119
120/*
121 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122struct ub_dev;
123
Pete Zaitcev64bd8452005-09-22 00:48:29 -0700124#define UB_MAX_REQ_SG 9 /* cdrecord requires 32KB and maybe a header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#define UB_MAX_SECTORS 64
126
127/*
128 * A second is more than enough for a 32K transfer (UB_MAX_SECTORS)
129 * even if a webcam hogs the bus, but some devices need time to spin up.
130 */
131#define UB_URB_TIMEOUT (HZ*2)
132#define UB_DATA_TIMEOUT (HZ*5) /* ZIP does spin-ups in the data phase */
133#define UB_STAT_TIMEOUT (HZ*5) /* Same spinups and eject for a dataless cmd. */
134#define UB_CTRL_TIMEOUT (HZ/2) /* 500ms ought to be enough to clear a stall */
135
136/*
137 * An instance of a SCSI command in transit.
138 */
139#define UB_DIR_NONE 0
140#define UB_DIR_READ 1
141#define UB_DIR_ILLEGAL2 2
142#define UB_DIR_WRITE 3
143
144#define UB_DIR_CHAR(c) (((c)==UB_DIR_WRITE)? 'w': \
145 (((c)==UB_DIR_READ)? 'r': 'n'))
146
147enum ub_scsi_cmd_state {
148 UB_CMDST_INIT, /* Initial state */
149 UB_CMDST_CMD, /* Command submitted */
150 UB_CMDST_DATA, /* Data phase */
151 UB_CMDST_CLR2STS, /* Clearing before requesting status */
152 UB_CMDST_STAT, /* Status phase */
153 UB_CMDST_CLEAR, /* Clearing a stall (halt, actually) */
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700154 UB_CMDST_CLRRS, /* Clearing before retrying status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 UB_CMDST_SENSE, /* Sending Request Sense */
156 UB_CMDST_DONE /* Final state */
157};
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159struct ub_scsi_cmd {
160 unsigned char cdb[UB_MAX_CDB_SIZE];
161 unsigned char cdb_len;
162
163 unsigned char dir; /* 0 - none, 1 - read, 3 - write. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 enum ub_scsi_cmd_state state;
165 unsigned int tag;
166 struct ub_scsi_cmd *next;
167
168 int error; /* Return code - valid upon done */
169 unsigned int act_len; /* Return size */
170 unsigned char key, asc, ascq; /* May be valid if error==-EIO */
171
172 int stat_count; /* Retries getting status. */
Pete Zaitcev2c51ae72008-04-19 14:32:18 -0700173 unsigned int timeo; /* jiffies until rq->timeout changes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 unsigned int len; /* Requested length */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700176 unsigned int current_sg;
177 unsigned int nsg; /* sgv[nsg] */
178 struct scatterlist sgv[UB_MAX_REQ_SG];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Pete Zaitcevf4800072005-05-01 16:05:40 -0700180 struct ub_lun *lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 void (*done)(struct ub_dev *, struct ub_scsi_cmd *);
182 void *back;
183};
184
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800185struct ub_request {
186 struct request *rq;
187 unsigned int current_try;
188 unsigned int nsg; /* sgv[nsg] */
189 struct scatterlist sgv[UB_MAX_REQ_SG];
190};
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/*
193 */
194struct ub_capacity {
195 unsigned long nsec; /* Linux size - 512 byte sectors */
196 unsigned int bsize; /* Linux hardsect_size */
197 unsigned int bshift; /* Shift between 512 and hard sects */
198};
199
200/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * This is a direct take-off from linux/include/completion.h
202 * The difference is that I do not wait on this thing, just poll.
203 * When I want to wait (ub_probe), I just use the stock completion.
204 *
205 * Note that INIT_COMPLETION takes no lock. It is correct. But why
206 * in the bloody hell that thing takes struct instead of pointer to struct
207 * is quite beyond me. I just copied it from the stock completion.
208 */
209struct ub_completion {
210 unsigned int done;
211 spinlock_t lock;
212};
213
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200214static DEFINE_MUTEX(ub_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215static inline void ub_init_completion(struct ub_completion *x)
216{
217 x->done = 0;
218 spin_lock_init(&x->lock);
219}
220
221#define UB_INIT_COMPLETION(x) ((x).done = 0)
222
223static void ub_complete(struct ub_completion *x)
224{
225 unsigned long flags;
226
227 spin_lock_irqsave(&x->lock, flags);
228 x->done++;
229 spin_unlock_irqrestore(&x->lock, flags);
230}
231
232static int ub_is_completed(struct ub_completion *x)
233{
234 unsigned long flags;
235 int ret;
236
237 spin_lock_irqsave(&x->lock, flags);
238 ret = x->done;
239 spin_unlock_irqrestore(&x->lock, flags);
240 return ret;
241}
242
243/*
244 */
245struct ub_scsi_cmd_queue {
246 int qlen, qmax;
247 struct ub_scsi_cmd *head, *tail;
248};
249
250/*
Pete Zaitcevf4800072005-05-01 16:05:40 -0700251 * The block device instance (one per LUN).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700253struct ub_lun {
254 struct ub_dev *udev;
255 struct list_head link;
256 struct gendisk *disk;
257 int id; /* Host index */
258 int num; /* LUN number */
259 char name[16];
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 int changed; /* Media was changed */
262 int removable;
263 int readonly;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700264
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800265 struct ub_request urq;
266
Pete Zaitcevf4800072005-05-01 16:05:40 -0700267 /* Use Ingo's mempool if or when we have more than one command. */
268 /*
269 * Currently we never need more than one command for the whole device.
270 * However, giving every LUN a command is a cheap and automatic way
271 * to enforce fairness between them.
272 */
273 int cmda[1];
274 struct ub_scsi_cmd cmdv[1];
275
276 struct ub_capacity capacity;
277};
278
279/*
280 * The USB device instance.
281 */
282struct ub_dev {
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800283 spinlock_t *lock;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700284 atomic_t poison; /* The USB device is disconnected */
285 int openc; /* protected by ub_lock! */
286 /* kref is too implicit for our taste */
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800287 int reset; /* Reset is running */
Pete Zaitcev0da13c82008-04-19 14:42:49 -0700288 int bad_resid;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700289 unsigned int tagcnt;
290 char name[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 struct usb_device *dev;
292 struct usb_interface *intf;
293
Pete Zaitcevf4800072005-05-01 16:05:40 -0700294 struct list_head luns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 unsigned int send_bulk_pipe; /* cached pipe values */
297 unsigned int recv_bulk_pipe;
298 unsigned int send_ctrl_pipe;
299 unsigned int recv_ctrl_pipe;
300
301 struct tasklet_struct tasklet;
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 struct ub_scsi_cmd_queue cmd_queue;
304 struct ub_scsi_cmd top_rqs_cmd; /* REQUEST SENSE */
305 unsigned char top_sense[UB_SENSE_SIZE];
306
307 struct ub_completion work_done;
308 struct urb work_urb;
309 struct timer_list work_timer;
310 int last_pipe; /* What might need clearing */
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700311 __le32 signature; /* Learned signature */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 struct bulk_cb_wrap work_bcb;
313 struct bulk_cs_wrap work_bcs;
314 struct usb_ctrlrequest work_cr;
315
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800316 struct work_struct reset_work;
317 wait_queue_head_t reset_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318};
319
320/*
321 */
322static void ub_cleanup(struct ub_dev *sc);
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700323static int ub_request_fn_1(struct ub_lun *lun, struct request *rq);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800324static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
325 struct ub_scsi_cmd *cmd, struct ub_request *urq);
326static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
327 struct ub_scsi_cmd *cmd, struct ub_request *urq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Tejun Heo37551002009-05-19 18:33:04 +0900329static void ub_end_rq(struct request *rq, unsigned int status);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800330static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
331 struct ub_request *urq, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
David Howells7d12e782006-10-05 14:55:46 +0100333static void ub_urb_complete(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334static void ub_scsi_action(unsigned long _dev);
335static void ub_scsi_dispatch(struct ub_dev *sc);
336static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700337static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc);
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700339static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700341static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
343static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
344 int stalled_pipe);
345static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd);
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -0800346static void ub_reset_enter(struct ub_dev *sc, int try);
David Howellsc4028952006-11-22 14:57:56 +0000347static void ub_reset_task(struct work_struct *work);
Pete Zaitcevf4800072005-05-01 16:05:40 -0700348static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun);
349static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
350 struct ub_capacity *ret);
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -0800351static int ub_sync_reset(struct ub_dev *sc);
352static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe);
Pete Zaitcevf4800072005-05-01 16:05:40 -0700353static int ub_probe_lun(struct ub_dev *sc, int lnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355/*
356 */
Pete Zaitceva00828e2005-10-22 20:15:09 -0700357#ifdef CONFIG_USB_LIBUSUAL
358
Alan Sterne6e244b2009-02-12 14:47:44 -0500359#define ub_usb_ids usb_storage_usb_ids
Pete Zaitceva00828e2005-10-22 20:15:09 -0700360#else
361
Márton Németh577cdf02010-01-10 13:39:39 +0100362static const struct usb_device_id ub_usb_ids[] = {
Michal Nazarewicz8fa7fd72010-10-07 13:05:21 +0200363 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 { }
365};
366
367MODULE_DEVICE_TABLE(usb, ub_usb_ids);
Pete Zaitceva00828e2005-10-22 20:15:09 -0700368#endif /* CONFIG_USB_LIBUSUAL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370/*
371 * Find me a way to identify "next free minor" for add_disk(),
372 * and the array disappears the next day. However, the number of
373 * hosts has something to do with the naming and /proc/partitions.
374 * This has to be thought out in detail before changing.
375 * If UB_MAX_HOST was 1000, we'd use a bitmap. Or a better data structure.
376 */
377#define UB_MAX_HOSTS 26
378static char ub_hostv[UB_MAX_HOSTS];
Pete Zaitcevf4800072005-05-01 16:05:40 -0700379
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800380#define UB_QLOCK_NUM 5
381static spinlock_t ub_qlockv[UB_QLOCK_NUM];
382static int ub_qlock_next = 0;
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384static DEFINE_SPINLOCK(ub_lock); /* Locks globals and ->openc */
385
386/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 * The id allocator.
388 *
389 * This also stores the host for indexing by minor, which is somewhat dirty.
390 */
391static int ub_id_get(void)
392{
393 unsigned long flags;
394 int i;
395
396 spin_lock_irqsave(&ub_lock, flags);
397 for (i = 0; i < UB_MAX_HOSTS; i++) {
398 if (ub_hostv[i] == 0) {
399 ub_hostv[i] = 1;
400 spin_unlock_irqrestore(&ub_lock, flags);
401 return i;
402 }
403 }
404 spin_unlock_irqrestore(&ub_lock, flags);
405 return -1;
406}
407
408static void ub_id_put(int id)
409{
410 unsigned long flags;
411
412 if (id < 0 || id >= UB_MAX_HOSTS) {
413 printk(KERN_ERR DRV_NAME ": bad host ID %d\n", id);
414 return;
415 }
416
417 spin_lock_irqsave(&ub_lock, flags);
418 if (ub_hostv[id] == 0) {
419 spin_unlock_irqrestore(&ub_lock, flags);
420 printk(KERN_ERR DRV_NAME ": freeing free host ID %d\n", id);
421 return;
422 }
423 ub_hostv[id] = 0;
424 spin_unlock_irqrestore(&ub_lock, flags);
425}
426
427/*
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800428 * This is necessitated by the fact that blk_cleanup_queue does not
429 * necesserily destroy the queue. Instead, it may merely decrease q->refcnt.
430 * Since our blk_init_queue() passes a spinlock common with ub_dev,
431 * we have life time issues when ub_cleanup frees ub_dev.
432 */
433static spinlock_t *ub_next_lock(void)
434{
435 unsigned long flags;
436 spinlock_t *ret;
437
438 spin_lock_irqsave(&ub_lock, flags);
439 ret = &ub_qlockv[ub_qlock_next];
440 ub_qlock_next = (ub_qlock_next + 1) % UB_QLOCK_NUM;
441 spin_unlock_irqrestore(&ub_lock, flags);
442 return ret;
443}
444
445/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * Downcount for deallocation. This rides on two assumptions:
447 * - once something is poisoned, its refcount cannot grow
448 * - opens cannot happen at this time (del_gendisk was done)
449 * If the above is true, we can drop the lock, which we need for
450 * blk_cleanup_queue(): the silly thing may attempt to sleep.
451 * [Actually, it never needs to sleep for us, but it calls might_sleep()]
452 */
453static void ub_put(struct ub_dev *sc)
454{
455 unsigned long flags;
456
457 spin_lock_irqsave(&ub_lock, flags);
458 --sc->openc;
459 if (sc->openc == 0 && atomic_read(&sc->poison)) {
460 spin_unlock_irqrestore(&ub_lock, flags);
461 ub_cleanup(sc);
462 } else {
463 spin_unlock_irqrestore(&ub_lock, flags);
464 }
465}
466
467/*
468 * Final cleanup and deallocation.
469 */
470static void ub_cleanup(struct ub_dev *sc)
471{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700472 struct list_head *p;
473 struct ub_lun *lun;
Jens Axboe165125e2007-07-24 09:28:11 +0200474 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Pete Zaitcevf4800072005-05-01 16:05:40 -0700476 while (!list_empty(&sc->luns)) {
477 p = sc->luns.next;
478 lun = list_entry(p, struct ub_lun, link);
479 list_del(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Pete Zaitcevf4800072005-05-01 16:05:40 -0700481 /* I don't think queue can be NULL. But... Stolen from sx8.c */
482 if ((q = lun->disk->queue) != NULL)
483 blk_cleanup_queue(q);
484 /*
485 * If we zero disk->private_data BEFORE put_disk, we have
486 * to check for NULL all over the place in open, release,
487 * check_media and revalidate, because the block level
488 * semaphore is well inside the put_disk.
489 * But we cannot zero after the call, because *disk is gone.
490 * The sd.c is blatantly racy in this area.
491 */
492 /* disk->private_data = NULL; */
493 put_disk(lun->disk);
494 lun->disk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Pete Zaitcevf4800072005-05-01 16:05:40 -0700496 ub_id_put(lun->id);
497 kfree(lun);
498 }
499
Pete Zaitcev77ef6c42006-05-03 00:16:00 -0700500 usb_set_intfdata(sc->intf, NULL);
501 usb_put_intf(sc->intf);
502 usb_put_dev(sc->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 kfree(sc);
504}
505
506/*
507 * The "command allocator".
508 */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700509static struct ub_scsi_cmd *ub_get_cmd(struct ub_lun *lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 struct ub_scsi_cmd *ret;
512
Pete Zaitcevf4800072005-05-01 16:05:40 -0700513 if (lun->cmda[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return NULL;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700515 ret = &lun->cmdv[0];
516 lun->cmda[0] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return ret;
518}
519
Pete Zaitcevf4800072005-05-01 16:05:40 -0700520static void ub_put_cmd(struct ub_lun *lun, struct ub_scsi_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700522 if (cmd != &lun->cmdv[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 printk(KERN_WARNING "%s: releasing a foreign cmd %p\n",
Pete Zaitcevf4800072005-05-01 16:05:40 -0700524 lun->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return;
526 }
Pete Zaitcevf4800072005-05-01 16:05:40 -0700527 if (!lun->cmda[0]) {
528 printk(KERN_WARNING "%s: releasing a free cmd\n", lun->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return;
530 }
Pete Zaitcevf4800072005-05-01 16:05:40 -0700531 lun->cmda[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
534/*
535 * The command queue.
536 */
537static void ub_cmdq_add(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
538{
539 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
540
541 if (t->qlen++ == 0) {
542 t->head = cmd;
543 t->tail = cmd;
544 } else {
545 t->tail->next = cmd;
546 t->tail = cmd;
547 }
548
549 if (t->qlen > t->qmax)
550 t->qmax = t->qlen;
551}
552
553static void ub_cmdq_insert(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
554{
555 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
556
557 if (t->qlen++ == 0) {
558 t->head = cmd;
559 t->tail = cmd;
560 } else {
561 cmd->next = t->head;
562 t->head = cmd;
563 }
564
565 if (t->qlen > t->qmax)
566 t->qmax = t->qlen;
567}
568
569static struct ub_scsi_cmd *ub_cmdq_pop(struct ub_dev *sc)
570{
571 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
572 struct ub_scsi_cmd *cmd;
573
574 if (t->qlen == 0)
575 return NULL;
576 if (--t->qlen == 0)
577 t->tail = NULL;
578 cmd = t->head;
579 t->head = cmd->next;
580 cmd->next = NULL;
581 return cmd;
582}
583
584#define ub_cmdq_peek(sc) ((sc)->cmd_queue.head)
585
586/*
587 * The request function is our main entry point
588 */
589
Jens Axboe165125e2007-07-24 09:28:11 +0200590static void ub_request_fn(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700592 struct ub_lun *lun = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 struct request *rq;
594
Tejun Heo9934c8c2009-05-08 11:54:16 +0900595 while ((rq = blk_peek_request(q)) != NULL) {
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700596 if (ub_request_fn_1(lun, rq) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 blk_stop_queue(q);
598 break;
599 }
600 }
601}
602
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700603static int ub_request_fn_1(struct ub_lun *lun, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700605 struct ub_dev *sc = lun->udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 struct ub_scsi_cmd *cmd;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800607 struct ub_request *urq;
608 int n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700610 if (atomic_read(&sc->poison)) {
Tejun Heo9934c8c2009-05-08 11:54:16 +0900611 blk_start_request(rq);
Tejun Heo37551002009-05-19 18:33:04 +0900612 ub_end_rq(rq, DID_NO_CONNECT << 16);
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700613 return 0;
614 }
615
Christoph Hellwig4c4762d2010-06-19 17:26:47 +0200616 if (lun->changed && rq->cmd_type != REQ_TYPE_BLOCK_PC) {
Tejun Heo9934c8c2009-05-08 11:54:16 +0900617 blk_start_request(rq);
Tejun Heo37551002009-05-19 18:33:04 +0900618 ub_end_rq(rq, SAM_STAT_CHECK_CONDITION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return 0;
620 }
621
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800622 if (lun->urq.rq != NULL)
623 return -1;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700624 if ((cmd = ub_get_cmd(lun)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return -1;
626 memset(cmd, 0, sizeof(struct ub_scsi_cmd));
627
Tejun Heo9934c8c2009-05-08 11:54:16 +0900628 blk_start_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800630 urq = &lun->urq;
631 memset(urq, 0, sizeof(struct ub_request));
632 urq->rq = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 /*
635 * get scatterlist from block layer
636 */
Pete Zaitcev541645b2008-02-09 00:10:17 -0800637 sg_init_table(&urq->sgv[0], UB_MAX_REQ_SG);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800638 n_elem = blk_rq_map_sg(lun->disk->queue, rq, &urq->sgv[0]);
639 if (n_elem < 0) {
Pete Zaitcevb5600332006-05-25 20:08:50 -0700640 /* Impossible, because blk_rq_map_sg should not hit ENOMEM. */
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700641 printk(KERN_INFO "%s: failed request map (%d)\n",
Pete Zaitcevb5600332006-05-25 20:08:50 -0700642 lun->name, n_elem);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800643 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700645 if (n_elem > UB_MAX_REQ_SG) { /* Paranoia */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 printk(KERN_WARNING "%s: request with %d segments\n",
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800647 lun->name, n_elem);
648 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800650 urq->nsg = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200652 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800653 ub_cmd_build_packet(sc, lun, cmd, urq);
654 } else {
655 ub_cmd_build_block(sc, lun, cmd, urq);
656 }
657 cmd->state = UB_CMDST_INIT;
658 cmd->lun = lun;
659 cmd->done = ub_rw_cmd_done;
660 cmd->back = urq;
661
662 cmd->tag = sc->tagcnt++;
663 if (ub_submit_scsi(sc, cmd) != 0)
664 goto drop;
665
666 return 0;
667
668drop:
669 ub_put_cmd(lun, cmd);
Tejun Heo37551002009-05-19 18:33:04 +0900670 ub_end_rq(rq, DID_ERROR << 16);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800671 return 0;
672}
673
674static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
675 struct ub_scsi_cmd *cmd, struct ub_request *urq)
676{
677 struct request *rq = urq->rq;
678 unsigned int block, nblks;
679
680 if (rq_data_dir(rq) == WRITE)
681 cmd->dir = UB_DIR_WRITE;
682 else
683 cmd->dir = UB_DIR_READ;
684
685 cmd->nsg = urq->nsg;
686 memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /*
689 * build the command
690 *
Martin K. Petersene1defc42009-05-22 17:17:49 -0400691 * The call to blk_queue_logical_block_size() guarantees that request
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 * is aligned, but it is given in terms of 512 byte units, always.
693 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900694 block = blk_rq_pos(rq) >> lun->capacity.bshift;
695 nblks = blk_rq_sectors(rq) >> lun->capacity.bshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800697 cmd->cdb[0] = (cmd->dir == UB_DIR_READ)? READ_10: WRITE_10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 /* 10-byte uses 4 bytes of LBA: 2147483648KB, 2097152MB, 2048GB */
699 cmd->cdb[2] = block >> 24;
700 cmd->cdb[3] = block >> 16;
701 cmd->cdb[4] = block >> 8;
702 cmd->cdb[5] = block;
703 cmd->cdb[7] = nblks >> 8;
704 cmd->cdb[8] = nblks;
705 cmd->cdb_len = 10;
706
Tejun Heo1011c1b2009-05-07 22:24:45 +0900707 cmd->len = blk_rq_bytes(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800710static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
711 struct ub_scsi_cmd *cmd, struct ub_request *urq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800713 struct request *rq = urq->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Tejun Heob07904102009-05-07 22:24:42 +0900715 if (blk_rq_bytes(rq) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 cmd->dir = UB_DIR_NONE;
717 } else {
718 if (rq_data_dir(rq) == WRITE)
719 cmd->dir = UB_DIR_WRITE;
720 else
721 cmd->dir = UB_DIR_READ;
722 }
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700723
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800724 cmd->nsg = urq->nsg;
725 memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700726
727 memcpy(&cmd->cdb, rq->cmd, rq->cmd_len);
728 cmd->cdb_len = rq->cmd_len;
729
Tejun Heob07904102009-05-07 22:24:42 +0900730 cmd->len = blk_rq_bytes(rq);
Pete Zaitcev2c51ae72008-04-19 14:32:18 -0700731
732 /*
733 * To reapply this to every URB is not as incorrect as it looks.
734 * In return, we avoid any complicated tracking calculations.
735 */
736 cmd->timeo = rq->timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
739static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
740{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700741 struct ub_lun *lun = cmd->lun;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800742 struct ub_request *urq = cmd->back;
743 struct request *rq;
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700744 unsigned int scsi_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800746 rq = urq->rq;
747
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700748 if (cmd->error == 0) {
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200749 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Tejun Heo5f49f632009-05-19 18:33:05 +0900750 if (cmd->act_len >= rq->resid_len)
751 rq->resid_len = 0;
752 else
753 rq->resid_len -= cmd->act_len;
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700754 scsi_status = 0;
755 } else {
756 if (cmd->act_len != cmd->len) {
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700757 scsi_status = SAM_STAT_CHECK_CONDITION;
758 } else {
759 scsi_status = 0;
760 }
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700761 }
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700762 } else {
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200763 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700764 /* UB_SENSE_SIZE is smaller than SCSI_SENSE_BUFFERSIZE */
765 memcpy(rq->sense, sc->top_sense, UB_SENSE_SIZE);
766 rq->sense_len = UB_SENSE_SIZE;
767 if (sc->top_sense[0] != 0)
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700768 scsi_status = SAM_STAT_CHECK_CONDITION;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700769 else
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700770 scsi_status = DID_ERROR << 16;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800771 } else {
Pete Zaitcev82fe26b2008-04-19 14:35:30 -0700772 if (cmd->error == -EIO &&
773 (cmd->key == 0 ||
774 cmd->key == MEDIUM_ERROR ||
775 cmd->key == UNIT_ATTENTION)) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800776 if (ub_rw_cmd_retry(sc, lun, urq, cmd) == 0)
777 return;
778 }
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700779 scsi_status = SAM_STAT_CHECK_CONDITION;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700780 }
781 }
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700782
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800783 urq->rq = NULL;
784
Pete Zaitcevf4800072005-05-01 16:05:40 -0700785 ub_put_cmd(lun, cmd);
Tejun Heo37551002009-05-19 18:33:04 +0900786 ub_end_rq(rq, scsi_status);
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700787 blk_start_queue(lun->disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
Tejun Heo37551002009-05-19 18:33:04 +0900790static void ub_end_rq(struct request *rq, unsigned int scsi_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Kiyoshi Ueda7d699ba2007-12-11 17:46:47 -0500792 int error;
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700793
794 if (scsi_status == 0) {
Kiyoshi Ueda7d699ba2007-12-11 17:46:47 -0500795 error = 0;
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700796 } else {
Kiyoshi Ueda7d699ba2007-12-11 17:46:47 -0500797 error = -EIO;
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700798 rq->errors = scsi_status;
799 }
Tejun Heo37551002009-05-19 18:33:04 +0900800 __blk_end_request_all(rq, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800803static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
804 struct ub_request *urq, struct ub_scsi_cmd *cmd)
805{
806
807 if (atomic_read(&sc->poison))
808 return -ENXIO;
809
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -0800810 ub_reset_enter(sc, urq->current_try);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800811
812 if (urq->current_try >= 3)
813 return -EIO;
814 urq->current_try++;
Pete Zaitcevb5600332006-05-25 20:08:50 -0700815
816 /* Remove this if anyone complains of flooding. */
817 printk(KERN_DEBUG "%s: dir %c len/act %d/%d "
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800818 "[sense %x %02x %02x] retry %d\n",
819 sc->name, UB_DIR_CHAR(cmd->dir), cmd->len, cmd->act_len,
820 cmd->key, cmd->asc, cmd->ascq, urq->current_try);
821
822 memset(cmd, 0, sizeof(struct ub_scsi_cmd));
823 ub_cmd_build_block(sc, lun, cmd, urq);
824
825 cmd->state = UB_CMDST_INIT;
826 cmd->lun = lun;
827 cmd->done = ub_rw_cmd_done;
828 cmd->back = urq;
829
830 cmd->tag = sc->tagcnt++;
831
832#if 0 /* Wasteful */
833 return ub_submit_scsi(sc, cmd);
834#else
835 ub_cmdq_add(sc, cmd);
836 return 0;
837#endif
838}
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840/*
841 * Submit a regular SCSI operation (not an auto-sense).
842 *
843 * The Iron Law of Good Submit Routine is:
844 * Zero return - callback is done, Nonzero return - callback is not done.
845 * No exceptions.
846 *
847 * Host is assumed locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 */
849static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
850{
851
852 if (cmd->state != UB_CMDST_INIT ||
853 (cmd->dir != UB_DIR_NONE && cmd->len == 0)) {
854 return -EINVAL;
855 }
856
857 ub_cmdq_add(sc, cmd);
858 /*
859 * We can call ub_scsi_dispatch(sc) right away here, but it's a little
860 * safer to jump to a tasklet, in case upper layers do something silly.
861 */
862 tasklet_schedule(&sc->tasklet);
863 return 0;
864}
865
866/*
867 * Submit the first URB for the queued command.
868 * This function does not deal with queueing in any way.
869 */
870static int ub_scsi_cmd_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
871{
872 struct bulk_cb_wrap *bcb;
873 int rc;
874
875 bcb = &sc->work_bcb;
876
877 /*
878 * ``If the allocation length is eighteen or greater, and a device
879 * server returns less than eithteen bytes of data, the application
880 * client should assume that the bytes not transferred would have been
881 * zeroes had the device server returned those bytes.''
882 *
883 * We zero sense for all commands so that when a packet request
884 * fails it does not return a stale sense.
885 */
886 memset(&sc->top_sense, 0, UB_SENSE_SIZE);
887
888 /* set up the command wrapper */
889 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
890 bcb->Tag = cmd->tag; /* Endianness is not important */
891 bcb->DataTransferLength = cpu_to_le32(cmd->len);
892 bcb->Flags = (cmd->dir == UB_DIR_READ) ? 0x80 : 0;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700893 bcb->Lun = (cmd->lun != NULL) ? cmd->lun->num : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 bcb->Length = cmd->cdb_len;
895
896 /* copy the command payload */
897 memcpy(bcb->CDB, cmd->cdb, UB_MAX_CDB_SIZE);
898
899 UB_INIT_COMPLETION(sc->work_done);
900
901 sc->last_pipe = sc->send_bulk_pipe;
902 usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->send_bulk_pipe,
903 bcb, US_BULK_CB_WRAP_LEN, ub_urb_complete, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
906 /* XXX Clear stalls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 ub_complete(&sc->work_done);
908 return rc;
909 }
910
911 sc->work_timer.expires = jiffies + UB_URB_TIMEOUT;
912 add_timer(&sc->work_timer);
913
914 cmd->state = UB_CMDST_CMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return 0;
916}
917
918/*
919 * Timeout handler.
920 */
921static void ub_urb_timeout(unsigned long arg)
922{
923 struct ub_dev *sc = (struct ub_dev *) arg;
924 unsigned long flags;
925
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800926 spin_lock_irqsave(sc->lock, flags);
Pete Zaitcevb31f8212006-01-05 00:14:02 -0800927 if (!ub_is_completed(&sc->work_done))
928 usb_unlink_urb(&sc->work_urb);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800929 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
932/*
933 * Completion routine for the work URB.
934 *
935 * This can be called directly from usb_submit_urb (while we have
936 * the sc->lock taken) and from an interrupt (while we do NOT have
937 * the sc->lock taken). Therefore, bounce this off to a tasklet.
938 */
David Howells7d12e782006-10-05 14:55:46 +0100939static void ub_urb_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
941 struct ub_dev *sc = urb->context;
942
943 ub_complete(&sc->work_done);
944 tasklet_schedule(&sc->tasklet);
945}
946
947static void ub_scsi_action(unsigned long _dev)
948{
949 struct ub_dev *sc = (struct ub_dev *) _dev;
950 unsigned long flags;
951
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800952 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 ub_scsi_dispatch(sc);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800954 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
957static void ub_scsi_dispatch(struct ub_dev *sc)
958{
959 struct ub_scsi_cmd *cmd;
960 int rc;
961
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800962 while (!sc->reset && (cmd = ub_cmdq_peek(sc)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (cmd->state == UB_CMDST_DONE) {
964 ub_cmdq_pop(sc);
965 (*cmd->done)(sc, cmd);
966 } else if (cmd->state == UB_CMDST_INIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if ((rc = ub_scsi_cmd_start(sc, cmd)) == 0)
968 break;
969 cmd->error = rc;
970 cmd->state = UB_CMDST_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 } else {
972 if (!ub_is_completed(&sc->work_done))
973 break;
Pete Zaitcevb31f8212006-01-05 00:14:02 -0800974 del_timer(&sc->work_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 ub_scsi_urb_compl(sc, cmd);
976 }
977 }
978}
979
980static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
981{
982 struct urb *urb = &sc->work_urb;
983 struct bulk_cs_wrap *bcs;
David Vrabel3444b262009-04-08 17:36:28 +0000984 int endp;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800985 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 int rc;
987
988 if (atomic_read(&sc->poison)) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800989 ub_state_done(sc, cmd, -ENODEV);
990 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992
David Vrabel3444b262009-04-08 17:36:28 +0000993 endp = usb_pipeendpoint(sc->last_pipe);
994 if (usb_pipein(sc->last_pipe))
995 endp |= USB_DIR_IN;
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (cmd->state == UB_CMDST_CLEAR) {
998 if (urb->status == -EPIPE) {
999 /*
1000 * STALL while clearning STALL.
1001 * The control pipe clears itself - nothing to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001003 printk(KERN_NOTICE "%s: stall on control pipe\n",
1004 sc->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 goto Bad_End;
1006 }
1007
1008 /*
1009 * We ignore the result for the halt clear.
1010 */
1011
David Vrabel3444b262009-04-08 17:36:28 +00001012 usb_reset_endpoint(sc->dev, endp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 ub_state_sense(sc, cmd);
1015
1016 } else if (cmd->state == UB_CMDST_CLR2STS) {
1017 if (urb->status == -EPIPE) {
Pete Zaitcevf4800072005-05-01 16:05:40 -07001018 printk(KERN_NOTICE "%s: stall on control pipe\n",
1019 sc->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 goto Bad_End;
1021 }
1022
1023 /*
1024 * We ignore the result for the halt clear.
1025 */
1026
David Vrabel3444b262009-04-08 17:36:28 +00001027 usb_reset_endpoint(sc->dev, endp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 ub_state_stat(sc, cmd);
1030
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001031 } else if (cmd->state == UB_CMDST_CLRRS) {
1032 if (urb->status == -EPIPE) {
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001033 printk(KERN_NOTICE "%s: stall on control pipe\n",
1034 sc->name);
1035 goto Bad_End;
1036 }
1037
1038 /*
1039 * We ignore the result for the halt clear.
1040 */
1041
David Vrabel3444b262009-04-08 17:36:28 +00001042 usb_reset_endpoint(sc->dev, endp);
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001043
1044 ub_state_stat_counted(sc, cmd);
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 } else if (cmd->state == UB_CMDST_CMD) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001047 switch (urb->status) {
1048 case 0:
1049 break;
1050 case -EOVERFLOW:
1051 goto Bad_End;
1052 case -EPIPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1054 if (rc != 0) {
1055 printk(KERN_NOTICE "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001056 "unable to submit clear (%d)\n",
1057 sc->name, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 /*
1059 * This is typically ENOMEM or some other such shit.
1060 * Retrying is pointless. Just do Bad End on it...
1061 */
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001062 ub_state_done(sc, cmd, rc);
1063 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065 cmd->state = UB_CMDST_CLEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001067 case -ESHUTDOWN: /* unplug */
1068 case -EILSEQ: /* unplug timeout on uhci */
1069 ub_state_done(sc, cmd, -ENODEV);
1070 return;
1071 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 goto Bad_End;
1073 }
1074 if (urb->actual_length != US_BULK_CB_WRAP_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 goto Bad_End;
1076 }
1077
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001078 if (cmd->dir == UB_DIR_NONE || cmd->nsg < 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 ub_state_stat(sc, cmd);
1080 return;
1081 }
1082
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001083 // udelay(125); // usb-storage has this
1084 ub_data_start(sc, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 } else if (cmd->state == UB_CMDST_DATA) {
1087 if (urb->status == -EPIPE) {
1088 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1089 if (rc != 0) {
1090 printk(KERN_NOTICE "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001091 "unable to submit clear (%d)\n",
1092 sc->name, rc);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001093 ub_state_done(sc, cmd, rc);
1094 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096 cmd->state = UB_CMDST_CLR2STS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return;
1098 }
1099 if (urb->status == -EOVERFLOW) {
1100 /*
1101 * A babble? Failure, but we must transfer CSW now.
1102 */
1103 cmd->error = -EOVERFLOW; /* A cheap trick... */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001104 ub_state_stat(sc, cmd);
1105 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001107
1108 if (cmd->dir == UB_DIR_WRITE) {
1109 /*
1110 * Do not continue writes in case of a failure.
1111 * Doing so would cause sectors to be mixed up,
1112 * which is worse than sectors lost.
1113 *
1114 * We must try to read the CSW, or many devices
1115 * get confused.
1116 */
1117 len = urb->actual_length;
1118 if (urb->status != 0 ||
1119 len != cmd->sgv[cmd->current_sg].length) {
1120 cmd->act_len += len;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001121
1122 cmd->error = -EIO;
1123 ub_state_stat(sc, cmd);
1124 return;
1125 }
1126
1127 } else {
1128 /*
1129 * If an error occurs on read, we record it, and
1130 * continue to fetch data in order to avoid bubble.
1131 *
1132 * As a small shortcut, we stop if we detect that
1133 * a CSW mixed into data.
1134 */
1135 if (urb->status != 0)
1136 cmd->error = -EIO;
1137
1138 len = urb->actual_length;
1139 if (urb->status != 0 ||
1140 len != cmd->sgv[cmd->current_sg].length) {
1141 if ((len & 0x1FF) == US_BULK_CS_WRAP_LEN)
1142 goto Bad_End;
1143 }
1144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001146 cmd->act_len += urb->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001148 if (++cmd->current_sg < cmd->nsg) {
1149 ub_data_start(sc, cmd);
1150 return;
1151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 ub_state_stat(sc, cmd);
1153
1154 } else if (cmd->state == UB_CMDST_STAT) {
1155 if (urb->status == -EPIPE) {
1156 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1157 if (rc != 0) {
1158 printk(KERN_NOTICE "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001159 "unable to submit clear (%d)\n",
1160 sc->name, rc);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001161 ub_state_done(sc, cmd, rc);
1162 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001164
1165 /*
1166 * Having a stall when getting CSW is an error, so
1167 * make sure uppper levels are not oblivious to it.
1168 */
1169 cmd->error = -EIO; /* A cheap trick... */
1170
1171 cmd->state = UB_CMDST_CLRRS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return;
1173 }
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001174
1175 /* Catch everything, including -EOVERFLOW and other nasties. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (urb->status != 0)
1177 goto Bad_End;
1178
1179 if (urb->actual_length == 0) {
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001180 ub_state_stat_counted(sc, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return;
1182 }
1183
1184 /*
1185 * Check the returned Bulk protocol status.
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001186 * The status block has to be validated first.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 */
1188
1189 bcs = &sc->work_bcs;
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001190
1191 if (sc->signature == cpu_to_le32(0)) {
1192 /*
1193 * This is the first reply, so do not perform the check.
1194 * Instead, remember the signature the device uses
1195 * for future checks. But do not allow a nul.
1196 */
1197 sc->signature = bcs->Signature;
1198 if (sc->signature == cpu_to_le32(0)) {
1199 ub_state_stat_counted(sc, cmd);
1200 return;
1201 }
1202 } else {
1203 if (bcs->Signature != sc->signature) {
1204 ub_state_stat_counted(sc, cmd);
1205 return;
1206 }
1207 }
1208
1209 if (bcs->Tag != cmd->tag) {
1210 /*
1211 * This usually happens when we disagree with the
1212 * device's microcode about something. For instance,
1213 * a few of them throw this after timeouts. They buffer
1214 * commands and reply at commands we timed out before.
1215 * Without flushing these replies we loop forever.
1216 */
1217 ub_state_stat_counted(sc, cmd);
1218 return;
1219 }
1220
Pete Zaitcev0da13c82008-04-19 14:42:49 -07001221 if (!sc->bad_resid) {
1222 len = le32_to_cpu(bcs->Residue);
1223 if (len != cmd->len - cmd->act_len) {
1224 /*
1225 * Only start ignoring if this cmd ended well.
1226 */
1227 if (cmd->len == cmd->act_len) {
1228 printk(KERN_NOTICE "%s: "
1229 "bad residual %d of %d, ignoring\n",
1230 sc->name, len, cmd->len);
1231 sc->bad_resid = 1;
1232 }
1233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 switch (bcs->Status) {
1237 case US_BULK_STAT_OK:
1238 break;
1239 case US_BULK_STAT_FAIL:
1240 ub_state_sense(sc, cmd);
1241 return;
1242 case US_BULK_STAT_PHASE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 goto Bad_End;
1244 default:
1245 printk(KERN_INFO "%s: unknown CSW status 0x%x\n",
1246 sc->name, bcs->Status);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001247 ub_state_done(sc, cmd, -EINVAL);
1248 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
1250
1251 /* Not zeroing error to preserve a babble indicator */
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001252 if (cmd->error != 0) {
1253 ub_state_sense(sc, cmd);
1254 return;
1255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 cmd->state = UB_CMDST_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 ub_cmdq_pop(sc);
1258 (*cmd->done)(sc, cmd);
1259
1260 } else if (cmd->state == UB_CMDST_SENSE) {
1261 ub_state_done(sc, cmd, -EIO);
1262
1263 } else {
Pete Zaitcev9029b172008-04-19 14:45:24 -07001264 printk(KERN_WARNING "%s: wrong command state %d\n",
Pete Zaitcevf4800072005-05-01 16:05:40 -07001265 sc->name, cmd->state);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001266 ub_state_done(sc, cmd, -EINVAL);
1267 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269 return;
1270
1271Bad_End: /* Little Excel is dead */
1272 ub_state_done(sc, cmd, -EIO);
1273}
1274
1275/*
1276 * Factorization helper for the command state machine:
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001277 * Initiate a data segment transfer.
1278 */
1279static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1280{
1281 struct scatterlist *sg = &cmd->sgv[cmd->current_sg];
1282 int pipe;
1283 int rc;
1284
1285 UB_INIT_COMPLETION(sc->work_done);
1286
1287 if (cmd->dir == UB_DIR_READ)
1288 pipe = sc->recv_bulk_pipe;
1289 else
1290 pipe = sc->send_bulk_pipe;
1291 sc->last_pipe = pipe;
Jens Axboe45711f12007-10-22 21:19:53 +02001292 usb_fill_bulk_urb(&sc->work_urb, sc->dev, pipe, sg_virt(sg),
1293 sg->length, ub_urb_complete, sc);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001294
1295 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1296 /* XXX Clear stalls */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001297 ub_complete(&sc->work_done);
1298 ub_state_done(sc, cmd, rc);
1299 return;
1300 }
1301
Pete Zaitcev2c51ae72008-04-19 14:32:18 -07001302 if (cmd->timeo)
1303 sc->work_timer.expires = jiffies + cmd->timeo;
1304 else
1305 sc->work_timer.expires = jiffies + UB_DATA_TIMEOUT;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001306 add_timer(&sc->work_timer);
1307
1308 cmd->state = UB_CMDST_DATA;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001309}
1310
1311/*
1312 * Factorization helper for the command state machine:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 * Finish the command.
1314 */
1315static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc)
1316{
1317
1318 cmd->error = rc;
1319 cmd->state = UB_CMDST_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 ub_cmdq_pop(sc);
1321 (*cmd->done)(sc, cmd);
1322}
1323
1324/*
1325 * Factorization helper for the command state machine:
1326 * Submit a CSW read.
1327 */
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001328static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
1330 int rc;
1331
1332 UB_INIT_COMPLETION(sc->work_done);
1333
1334 sc->last_pipe = sc->recv_bulk_pipe;
1335 usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->recv_bulk_pipe,
1336 &sc->work_bcs, US_BULK_CS_WRAP_LEN, ub_urb_complete, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1339 /* XXX Clear stalls */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 ub_complete(&sc->work_done);
1341 ub_state_done(sc, cmd, rc);
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001342 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344
Pete Zaitcev2c51ae72008-04-19 14:32:18 -07001345 if (cmd->timeo)
1346 sc->work_timer.expires = jiffies + cmd->timeo;
1347 else
1348 sc->work_timer.expires = jiffies + UB_STAT_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 add_timer(&sc->work_timer);
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001350 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
1353/*
1354 * Factorization helper for the command state machine:
1355 * Submit a CSW read and go to STAT state.
1356 */
1357static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1358{
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001359
1360 if (__ub_state_stat(sc, cmd) != 0)
1361 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 cmd->stat_count = 0;
1364 cmd->state = UB_CMDST_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
1366
1367/*
1368 * Factorization helper for the command state machine:
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001369 * Submit a CSW read and go to STAT state with counter (along [C] path).
1370 */
1371static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1372{
1373
1374 if (++cmd->stat_count >= 4) {
1375 ub_state_sense(sc, cmd);
1376 return;
1377 }
1378
1379 if (__ub_state_stat(sc, cmd) != 0)
1380 return;
1381
1382 cmd->state = UB_CMDST_STAT;
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001383}
1384
1385/*
1386 * Factorization helper for the command state machine:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 * Submit a REQUEST SENSE and go to SENSE state.
1388 */
1389static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1390{
1391 struct ub_scsi_cmd *scmd;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001392 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 int rc;
1394
1395 if (cmd->cdb[0] == REQUEST_SENSE) {
1396 rc = -EPIPE;
1397 goto error;
1398 }
1399
1400 scmd = &sc->top_rqs_cmd;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001401 memset(scmd, 0, sizeof(struct ub_scsi_cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 scmd->cdb[0] = REQUEST_SENSE;
1403 scmd->cdb[4] = UB_SENSE_SIZE;
1404 scmd->cdb_len = 6;
1405 scmd->dir = UB_DIR_READ;
1406 scmd->state = UB_CMDST_INIT;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001407 scmd->nsg = 1;
1408 sg = &scmd->sgv[0];
FUJITA Tomonori4f33a9d2007-10-25 09:17:03 +02001409 sg_init_table(sg, UB_MAX_REQ_SG);
Jens Axboe642f149032007-10-24 11:20:47 +02001410 sg_set_page(sg, virt_to_page(sc->top_sense), UB_SENSE_SIZE,
1411 (unsigned long)sc->top_sense & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 scmd->len = UB_SENSE_SIZE;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001413 scmd->lun = cmd->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 scmd->done = ub_top_sense_done;
1415 scmd->back = cmd;
1416
1417 scmd->tag = sc->tagcnt++;
1418
1419 cmd->state = UB_CMDST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 ub_cmdq_insert(sc, scmd);
1422 return;
1423
1424error:
1425 ub_state_done(sc, cmd, rc);
1426}
1427
1428/*
1429 * A helper for the command's state machine:
1430 * Submit a stall clear.
1431 */
1432static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
1433 int stalled_pipe)
1434{
1435 int endp;
1436 struct usb_ctrlrequest *cr;
1437 int rc;
1438
1439 endp = usb_pipeendpoint(stalled_pipe);
1440 if (usb_pipein (stalled_pipe))
1441 endp |= USB_DIR_IN;
1442
1443 cr = &sc->work_cr;
1444 cr->bRequestType = USB_RECIP_ENDPOINT;
1445 cr->bRequest = USB_REQ_CLEAR_FEATURE;
1446 cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
1447 cr->wIndex = cpu_to_le16(endp);
1448 cr->wLength = cpu_to_le16(0);
1449
1450 UB_INIT_COMPLETION(sc->work_done);
1451
1452 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
1453 (unsigned char*) cr, NULL, 0, ub_urb_complete, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1456 ub_complete(&sc->work_done);
1457 return rc;
1458 }
1459
1460 sc->work_timer.expires = jiffies + UB_CTRL_TIMEOUT;
1461 add_timer(&sc->work_timer);
1462 return 0;
1463}
1464
1465/*
1466 */
1467static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd)
1468{
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001469 unsigned char *sense = sc->top_sense;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 struct ub_scsi_cmd *cmd;
1471
1472 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 * Find the command which triggered the unit attention or a check,
1474 * save the sense into it, and advance its state machine.
1475 */
1476 if ((cmd = ub_cmdq_peek(sc)) == NULL) {
1477 printk(KERN_WARNING "%s: sense done while idle\n", sc->name);
1478 return;
1479 }
1480 if (cmd != scmd->back) {
1481 printk(KERN_WARNING "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001482 "sense done for wrong command 0x%x\n",
1483 sc->name, cmd->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 return;
1485 }
1486 if (cmd->state != UB_CMDST_SENSE) {
Pete Zaitcev9029b172008-04-19 14:45:24 -07001487 printk(KERN_WARNING "%s: sense done with bad cmd state %d\n",
Pete Zaitcevf4800072005-05-01 16:05:40 -07001488 sc->name, cmd->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return;
1490 }
1491
Pete Zaitcev952ba222006-03-02 16:42:59 -08001492 /*
1493 * Ignoring scmd->act_len, because the buffer was pre-zeroed.
1494 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 cmd->key = sense[2] & 0x0F;
1496 cmd->asc = sense[12];
1497 cmd->ascq = sense[13];
1498
1499 ub_scsi_urb_compl(sc, cmd);
1500}
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502/*
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001503 * Reset management
1504 */
1505
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001506static void ub_reset_enter(struct ub_dev *sc, int try)
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001507{
1508
1509 if (sc->reset) {
1510 /* This happens often on multi-LUN devices. */
1511 return;
1512 }
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001513 sc->reset = try + 1;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001514
1515#if 0 /* Not needed because the disconnect waits for us. */
1516 unsigned long flags;
1517 spin_lock_irqsave(&ub_lock, flags);
1518 sc->openc++;
1519 spin_unlock_irqrestore(&ub_lock, flags);
1520#endif
1521
1522#if 0 /* We let them stop themselves. */
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001523 struct ub_lun *lun;
Matthias Kaehlckea69228d2007-07-09 12:03:07 -07001524 list_for_each_entry(lun, &sc->luns, link) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001525 blk_stop_queue(lun->disk->queue);
1526 }
1527#endif
1528
1529 schedule_work(&sc->reset_work);
1530}
1531
David Howellsc4028952006-11-22 14:57:56 +00001532static void ub_reset_task(struct work_struct *work)
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001533{
David Howellsc4028952006-11-22 14:57:56 +00001534 struct ub_dev *sc = container_of(work, struct ub_dev, reset_work);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001535 unsigned long flags;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001536 struct ub_lun *lun;
Alan Stern011b15d2008-11-04 11:29:27 -05001537 int rc;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001538
1539 if (!sc->reset) {
1540 printk(KERN_WARNING "%s: Running reset unrequested\n",
1541 sc->name);
1542 return;
1543 }
1544
1545 if (atomic_read(&sc->poison)) {
Pete Zaitcevb5600332006-05-25 20:08:50 -07001546 ;
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001547 } else if ((sc->reset & 1) == 0) {
1548 ub_sync_reset(sc);
1549 msleep(700); /* usb-storage sleeps 6s (!) */
1550 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
1551 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001552 } else if (sc->dev->actconfig->desc.bNumInterfaces != 1) {
Pete Zaitcevb5600332006-05-25 20:08:50 -07001553 ;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001554 } else {
Alan Stern011b15d2008-11-04 11:29:27 -05001555 rc = usb_lock_device_for_reset(sc->dev, sc->intf);
1556 if (rc < 0) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001557 printk(KERN_NOTICE
1558 "%s: usb_lock_device_for_reset failed (%d)\n",
Alan Stern011b15d2008-11-04 11:29:27 -05001559 sc->name, rc);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001560 } else {
1561 rc = usb_reset_device(sc->dev);
1562 if (rc < 0) {
1563 printk(KERN_NOTICE "%s: "
1564 "usb_lock_device_for_reset failed (%d)\n",
1565 sc->name, rc);
1566 }
Alan Stern011b15d2008-11-04 11:29:27 -05001567 usb_unlock_device(sc->dev);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001568 }
1569 }
1570
1571 /*
1572 * In theory, no commands can be running while reset is active,
1573 * so nobody can ask for another reset, and so we do not need any
1574 * queues of resets or anything. We do need a spinlock though,
1575 * to interact with block layer.
1576 */
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001577 spin_lock_irqsave(sc->lock, flags);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001578 sc->reset = 0;
1579 tasklet_schedule(&sc->tasklet);
Matthias Kaehlckea69228d2007-07-09 12:03:07 -07001580 list_for_each_entry(lun, &sc->luns, link) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001581 blk_start_queue(lun->disk->queue);
1582 }
1583 wake_up(&sc->reset_wait);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001584 spin_unlock_irqrestore(sc->lock, flags);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001585}
1586
1587/*
Pete Zaitcevd73b7aff2008-11-10 21:11:11 -07001588 * XXX Reset brackets are too much hassle to implement, so just stub them
1589 * in order to prevent forced unbinding (which deadlocks solid when our
1590 * ->disconnect method waits for the reset to complete and this kills keventd).
1591 *
1592 * XXX Tell Alan to move usb_unlock_device inside of usb_reset_device,
1593 * or else the post_reset is invoked, and restats I/O on a locked device.
1594 */
1595static int ub_pre_reset(struct usb_interface *iface) {
1596 return 0;
1597}
1598
1599static int ub_post_reset(struct usb_interface *iface) {
1600 return 0;
1601}
1602
1603/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 * This is called from a process context.
1605 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001606static void ub_revalidate(struct ub_dev *sc, struct ub_lun *lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
1608
Pete Zaitcevf4800072005-05-01 16:05:40 -07001609 lun->readonly = 0; /* XXX Query this from the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Pete Zaitcevf4800072005-05-01 16:05:40 -07001611 lun->capacity.nsec = 0;
1612 lun->capacity.bsize = 512;
1613 lun->capacity.bshift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Pete Zaitcevf4800072005-05-01 16:05:40 -07001615 if (ub_sync_tur(sc, lun) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return; /* Not ready */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001617 lun->changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Pete Zaitcevf4800072005-05-01 16:05:40 -07001619 if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 /*
1621 * The retry here means something is wrong, either with the
1622 * device, with the transport, or with our code.
1623 * We keep this because sd.c has retries for capacity.
1624 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001625 if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
1626 lun->capacity.nsec = 0;
1627 lun->capacity.bsize = 512;
1628 lun->capacity.bshift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 }
1630 }
1631}
1632
1633/*
1634 * The open funcion.
1635 * This is mostly needed to keep refcounting, but also to support
1636 * media checks on removable media drives.
1637 */
Al Viro4099a962008-03-02 10:21:43 -05001638static int ub_bd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
Al Viro4099a962008-03-02 10:21:43 -05001640 struct ub_lun *lun = bdev->bd_disk->private_data;
Pete Zaitcev41fea55e2006-04-28 20:45:49 -07001641 struct ub_dev *sc = lun->udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 unsigned long flags;
1643 int rc;
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 spin_lock_irqsave(&ub_lock, flags);
1646 if (atomic_read(&sc->poison)) {
1647 spin_unlock_irqrestore(&ub_lock, flags);
1648 return -ENXIO;
1649 }
1650 sc->openc++;
1651 spin_unlock_irqrestore(&ub_lock, flags);
1652
Pete Zaitcevf4800072005-05-01 16:05:40 -07001653 if (lun->removable || lun->readonly)
Al Viro4099a962008-03-02 10:21:43 -05001654 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 /*
1657 * The sd.c considers ->media_present and ->changed not equivalent,
1658 * under some pretty murky conditions (a failure of READ CAPACITY).
1659 * We may need it one day.
1660 */
Al Viro4099a962008-03-02 10:21:43 -05001661 if (lun->removable && lun->changed && !(mode & FMODE_NDELAY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 rc = -ENOMEDIUM;
1663 goto err_open;
1664 }
1665
Al Viro4099a962008-03-02 10:21:43 -05001666 if (lun->readonly && (mode & FMODE_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 rc = -EROFS;
1668 goto err_open;
1669 }
1670
1671 return 0;
1672
1673err_open:
1674 ub_put(sc);
1675 return rc;
1676}
1677
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001678static int ub_bd_unlocked_open(struct block_device *bdev, fmode_t mode)
1679{
1680 int ret;
1681
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001682 mutex_lock(&ub_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001683 ret = ub_bd_open(bdev, mode);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001684 mutex_unlock(&ub_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001685
1686 return ret;
1687}
1688
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690/*
1691 */
Al Viro4099a962008-03-02 10:21:43 -05001692static int ub_bd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
Pete Zaitcevf4800072005-05-01 16:05:40 -07001694 struct ub_lun *lun = disk->private_data;
1695 struct ub_dev *sc = lun->udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001697 mutex_lock(&ub_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 ub_put(sc);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001699 mutex_unlock(&ub_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return 0;
1702}
1703
1704/*
1705 * The ioctl interface.
1706 */
Al Viro4099a962008-03-02 10:21:43 -05001707static int ub_bd_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 unsigned int cmd, unsigned long arg)
1709{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 void __user *usermem = (void __user *) arg;
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001711 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001713 mutex_lock(&ub_mutex);
Paolo Bonzini577ebb32012-01-12 16:01:27 +01001714 ret = scsi_cmd_blk_ioctl(bdev, mode, cmd, usermem);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001715 mutex_unlock(&ub_mutex);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001716
1717 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718}
1719
1720/*
Pete Zaitcev9029b172008-04-19 14:45:24 -07001721 * This is called by check_disk_change if we reported a media change.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 * The main onjective here is to discover the features of the media such as
1723 * the capacity, read-only status, etc. USB storage generally does not
1724 * need to be spun up, but if we needed it, this would be the place.
1725 *
1726 * This call can sleep.
1727 *
1728 * The return code is not used.
1729 */
1730static int ub_bd_revalidate(struct gendisk *disk)
1731{
Pete Zaitcevf4800072005-05-01 16:05:40 -07001732 struct ub_lun *lun = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Pete Zaitcevf4800072005-05-01 16:05:40 -07001734 ub_revalidate(lun->udev, lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 /* XXX Support sector size switching like in sr.c */
Martin K. Petersene1defc42009-05-22 17:17:49 -04001737 blk_queue_logical_block_size(disk->queue, lun->capacity.bsize);
Pete Zaitcevf4800072005-05-01 16:05:40 -07001738 set_capacity(disk, lun->capacity.nsec);
1739 // set_disk_ro(sdkp->disk, lun->readonly);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 return 0;
1742}
1743
1744/*
1745 * The check is called by the block layer to verify if the media
1746 * is still available. It is supposed to be harmless, lightweight and
1747 * non-intrusive in case the media was not changed.
1748 *
1749 * This call can sleep.
1750 *
1751 * The return code is bool!
1752 */
Tejun Heoaaa7c012011-03-09 19:54:28 +01001753static unsigned int ub_bd_check_events(struct gendisk *disk,
1754 unsigned int clearing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Pete Zaitcevf4800072005-05-01 16:05:40 -07001756 struct ub_lun *lun = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Pete Zaitcevf4800072005-05-01 16:05:40 -07001758 if (!lun->removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 return 0;
1760
1761 /*
1762 * We clean checks always after every command, so this is not
1763 * as dangerous as it looks. If the TEST_UNIT_READY fails here,
1764 * the device is actually not ready with operator or software
1765 * intervention required. One dangerous item might be a drive which
1766 * spins itself down, and come the time to write dirty pages, this
1767 * will fail, then block layer discards the data. Since we never
1768 * spin drives up, such devices simply cannot be used with ub anyway.
1769 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001770 if (ub_sync_tur(lun->udev, lun) != 0) {
1771 lun->changed = 1;
Tejun Heoaaa7c012011-03-09 19:54:28 +01001772 return DISK_EVENT_MEDIA_CHANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774
Tejun Heoaaa7c012011-03-09 19:54:28 +01001775 return lun->changed ? DISK_EVENT_MEDIA_CHANGE : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001778static const struct block_device_operations ub_bd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 .owner = THIS_MODULE,
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001780 .open = ub_bd_unlocked_open,
Al Viro4099a962008-03-02 10:21:43 -05001781 .release = ub_bd_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001782 .ioctl = ub_bd_ioctl,
Tejun Heoaaa7c012011-03-09 19:54:28 +01001783 .check_events = ub_bd_check_events,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 .revalidate_disk = ub_bd_revalidate,
1785};
1786
1787/*
1788 * Common ->done routine for commands executed synchronously.
1789 */
1790static void ub_probe_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1791{
1792 struct completion *cop = cmd->back;
1793 complete(cop);
1794}
1795
1796/*
1797 * Test if the device has a check condition on it, synchronously.
1798 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001799static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
1801 struct ub_scsi_cmd *cmd;
1802 enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) };
1803 unsigned long flags;
1804 struct completion compl;
1805 int rc;
1806
1807 init_completion(&compl);
1808
1809 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08001810 if ((cmd = kzalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
1813 cmd->cdb[0] = TEST_UNIT_READY;
1814 cmd->cdb_len = 6;
1815 cmd->dir = UB_DIR_NONE;
1816 cmd->state = UB_CMDST_INIT;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001817 cmd->lun = lun; /* This may be NULL, but that's ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 cmd->done = ub_probe_done;
1819 cmd->back = &compl;
1820
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001821 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 cmd->tag = sc->tagcnt++;
1823
1824 rc = ub_submit_scsi(sc, cmd);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001825 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Pete Zaitcevb5600332006-05-25 20:08:50 -07001827 if (rc != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 goto err_submit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 wait_for_completion(&compl);
1831
1832 rc = cmd->error;
1833
1834 if (rc == -EIO && cmd->key != 0) /* Retries for benh's key */
1835 rc = cmd->key;
1836
1837err_submit:
1838 kfree(cmd);
1839err_alloc:
1840 return rc;
1841}
1842
1843/*
1844 * Read the SCSI capacity synchronously (for probing).
1845 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001846static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
1847 struct ub_capacity *ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
1849 struct ub_scsi_cmd *cmd;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001850 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 char *p;
1852 enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) + 8 };
1853 unsigned long flags;
1854 unsigned int bsize, shift;
1855 unsigned long nsec;
1856 struct completion compl;
1857 int rc;
1858
1859 init_completion(&compl);
1860
1861 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08001862 if ((cmd = kzalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 p = (char *)cmd + sizeof(struct ub_scsi_cmd);
1865
1866 cmd->cdb[0] = 0x25;
1867 cmd->cdb_len = 10;
1868 cmd->dir = UB_DIR_READ;
1869 cmd->state = UB_CMDST_INIT;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001870 cmd->nsg = 1;
1871 sg = &cmd->sgv[0];
FUJITA Tomonori4f33a9d2007-10-25 09:17:03 +02001872 sg_init_table(sg, UB_MAX_REQ_SG);
Jens Axboe642f149032007-10-24 11:20:47 +02001873 sg_set_page(sg, virt_to_page(p), 8, (unsigned long)p & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 cmd->len = 8;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001875 cmd->lun = lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 cmd->done = ub_probe_done;
1877 cmd->back = &compl;
1878
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001879 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 cmd->tag = sc->tagcnt++;
1881
1882 rc = ub_submit_scsi(sc, cmd);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001883 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Pete Zaitcevb5600332006-05-25 20:08:50 -07001885 if (rc != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 goto err_submit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 wait_for_completion(&compl);
1889
1890 if (cmd->error != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 rc = -EIO;
1892 goto err_read;
1893 }
1894 if (cmd->act_len != 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 rc = -EIO;
1896 goto err_read;
1897 }
1898
1899 /* sd.c special-cases sector size of 0 to mean 512. Needed? Safe? */
1900 nsec = be32_to_cpu(*(__be32 *)p) + 1;
1901 bsize = be32_to_cpu(*(__be32 *)(p + 4));
1902 switch (bsize) {
1903 case 512: shift = 0; break;
1904 case 1024: shift = 1; break;
1905 case 2048: shift = 2; break;
1906 case 4096: shift = 3; break;
1907 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 rc = -EDOM;
1909 goto err_inv_bsize;
1910 }
1911
1912 ret->bsize = bsize;
1913 ret->bshift = shift;
1914 ret->nsec = nsec << shift;
1915 rc = 0;
1916
1917err_inv_bsize:
1918err_read:
1919err_submit:
1920 kfree(cmd);
1921err_alloc:
1922 return rc;
1923}
1924
1925/*
1926 */
David Howells7d12e782006-10-05 14:55:46 +01001927static void ub_probe_urb_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
1929 struct completion *cop = urb->context;
1930 complete(cop);
1931}
1932
1933static void ub_probe_timeout(unsigned long arg)
1934{
1935 struct completion *cop = (struct completion *) arg;
1936 complete(cop);
1937}
1938
1939/*
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001940 * Reset with a Bulk reset.
1941 */
1942static int ub_sync_reset(struct ub_dev *sc)
1943{
1944 int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
1945 struct usb_ctrlrequest *cr;
1946 struct completion compl;
1947 struct timer_list timer;
1948 int rc;
1949
1950 init_completion(&compl);
1951
1952 cr = &sc->work_cr;
1953 cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1954 cr->bRequest = US_BULK_RESET_REQUEST;
1955 cr->wValue = cpu_to_le16(0);
1956 cr->wIndex = cpu_to_le16(ifnum);
1957 cr->wLength = cpu_to_le16(0);
1958
1959 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
1960 (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001961
1962 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
1963 printk(KERN_WARNING
1964 "%s: Unable to submit a bulk reset (%d)\n", sc->name, rc);
1965 return rc;
1966 }
1967
1968 init_timer(&timer);
1969 timer.function = ub_probe_timeout;
1970 timer.data = (unsigned long) &compl;
1971 timer.expires = jiffies + UB_CTRL_TIMEOUT;
1972 add_timer(&timer);
1973
1974 wait_for_completion(&compl);
1975
1976 del_timer_sync(&timer);
1977 usb_kill_urb(&sc->work_urb);
1978
1979 return sc->work_urb.status;
1980}
1981
1982/*
Pete Zaitcevf4800072005-05-01 16:05:40 -07001983 * Get number of LUNs by the way of Bulk GetMaxLUN command.
1984 */
1985static int ub_sync_getmaxlun(struct ub_dev *sc)
1986{
1987 int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
1988 unsigned char *p;
1989 enum { ALLOC_SIZE = 1 };
1990 struct usb_ctrlrequest *cr;
1991 struct completion compl;
1992 struct timer_list timer;
1993 int nluns;
1994 int rc;
1995
1996 init_completion(&compl);
1997
1998 rc = -ENOMEM;
1999 if ((p = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
2000 goto err_alloc;
2001 *p = 55;
2002
2003 cr = &sc->work_cr;
2004 cr->bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
2005 cr->bRequest = US_BULK_GET_MAX_LUN;
2006 cr->wValue = cpu_to_le16(0);
2007 cr->wIndex = cpu_to_le16(ifnum);
2008 cr->wLength = cpu_to_le16(1);
2009
2010 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->recv_ctrl_pipe,
2011 (unsigned char*) cr, p, 1, ub_probe_urb_complete, &compl);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002012
Pete Zaitcevb5600332006-05-25 20:08:50 -07002013 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0)
Pete Zaitcevf4800072005-05-01 16:05:40 -07002014 goto err_submit;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002015
2016 init_timer(&timer);
2017 timer.function = ub_probe_timeout;
2018 timer.data = (unsigned long) &compl;
2019 timer.expires = jiffies + UB_CTRL_TIMEOUT;
2020 add_timer(&timer);
2021
2022 wait_for_completion(&compl);
2023
2024 del_timer_sync(&timer);
2025 usb_kill_urb(&sc->work_urb);
2026
Pete Zaitcevb5600332006-05-25 20:08:50 -07002027 if ((rc = sc->work_urb.status) < 0)
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002028 goto err_io;
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002029
Pete Zaitcevf4800072005-05-01 16:05:40 -07002030 if (sc->work_urb.actual_length != 1) {
Pete Zaitcevf4800072005-05-01 16:05:40 -07002031 nluns = 0;
2032 } else {
2033 if ((nluns = *p) == 55) {
2034 nluns = 0;
2035 } else {
2036 /* GetMaxLUN returns the maximum LUN number */
2037 nluns += 1;
2038 if (nluns > UB_MAX_LUNS)
2039 nluns = UB_MAX_LUNS;
2040 }
Pete Zaitcevf4800072005-05-01 16:05:40 -07002041 }
2042
2043 kfree(p);
2044 return nluns;
2045
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002046err_io:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002047err_submit:
2048 kfree(p);
2049err_alloc:
2050 return rc;
2051}
2052
2053/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 * Clear initial stalls.
2055 */
2056static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe)
2057{
2058 int endp;
2059 struct usb_ctrlrequest *cr;
2060 struct completion compl;
2061 struct timer_list timer;
2062 int rc;
2063
2064 init_completion(&compl);
2065
2066 endp = usb_pipeendpoint(stalled_pipe);
2067 if (usb_pipein (stalled_pipe))
2068 endp |= USB_DIR_IN;
2069
2070 cr = &sc->work_cr;
2071 cr->bRequestType = USB_RECIP_ENDPOINT;
2072 cr->bRequest = USB_REQ_CLEAR_FEATURE;
2073 cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
2074 cr->wIndex = cpu_to_le16(endp);
2075 cr->wLength = cpu_to_le16(0);
2076
2077 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
2078 (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
2081 printk(KERN_WARNING
2082 "%s: Unable to submit a probe clear (%d)\n", sc->name, rc);
2083 return rc;
2084 }
2085
2086 init_timer(&timer);
2087 timer.function = ub_probe_timeout;
2088 timer.data = (unsigned long) &compl;
2089 timer.expires = jiffies + UB_CTRL_TIMEOUT;
2090 add_timer(&timer);
2091
2092 wait_for_completion(&compl);
2093
2094 del_timer_sync(&timer);
2095 usb_kill_urb(&sc->work_urb);
2096
David Vrabel3444b262009-04-08 17:36:28 +00002097 usb_reset_endpoint(sc->dev, endp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099 return 0;
2100}
2101
2102/*
2103 * Get the pipe settings.
2104 */
2105static int ub_get_pipes(struct ub_dev *sc, struct usb_device *dev,
2106 struct usb_interface *intf)
2107{
2108 struct usb_host_interface *altsetting = intf->cur_altsetting;
2109 struct usb_endpoint_descriptor *ep_in = NULL;
2110 struct usb_endpoint_descriptor *ep_out = NULL;
2111 struct usb_endpoint_descriptor *ep;
2112 int i;
2113
2114 /*
2115 * Find the endpoints we need.
2116 * We are expecting a minimum of 2 endpoints - in and out (bulk).
2117 * We will ignore any others.
2118 */
2119 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
2120 ep = &altsetting->endpoint[i].desc;
2121
2122 /* Is it a BULK endpoint? */
Julia Lawalldb5e6df2008-12-29 11:19:10 +01002123 if (usb_endpoint_xfer_bulk(ep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 /* BULK in or out? */
Julia Lawalldb5e6df2008-12-29 11:19:10 +01002125 if (usb_endpoint_dir_in(ep)) {
Pete Zaitcev643616e2007-03-08 19:56:23 -08002126 if (ep_in == NULL)
2127 ep_in = ep;
2128 } else {
2129 if (ep_out == NULL)
2130 ep_out = ep;
2131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
2133 }
2134
2135 if (ep_in == NULL || ep_out == NULL) {
Pete Zaitcev9029b172008-04-19 14:45:24 -07002136 printk(KERN_NOTICE "%s: failed endpoint check\n", sc->name);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002137 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
2139
2140 /* Calculate and store the pipe values */
2141 sc->send_ctrl_pipe = usb_sndctrlpipe(dev, 0);
2142 sc->recv_ctrl_pipe = usb_rcvctrlpipe(dev, 0);
2143 sc->send_bulk_pipe = usb_sndbulkpipe(dev,
Julia Lawalldb5e6df2008-12-29 11:19:10 +01002144 usb_endpoint_num(ep_out));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 sc->recv_bulk_pipe = usb_rcvbulkpipe(dev,
Julia Lawalldb5e6df2008-12-29 11:19:10 +01002146 usb_endpoint_num(ep_in));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 return 0;
2149}
2150
2151/*
2152 * Probing is done in the process context, which allows us to cheat
2153 * and not to build a state machine for the discovery.
2154 */
2155static int ub_probe(struct usb_interface *intf,
2156 const struct usb_device_id *dev_id)
2157{
2158 struct ub_dev *sc;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002159 int nluns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 int rc;
2161 int i;
2162
Pete Zaitceva00828e2005-10-22 20:15:09 -07002163 if (usb_usual_check_type(dev_id, USB_US_TYPE_UB))
2164 return -ENXIO;
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08002167 if ((sc = kzalloc(sizeof(struct ub_dev), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 goto err_core;
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002169 sc->lock = ub_next_lock();
Pete Zaitcevf4800072005-05-01 16:05:40 -07002170 INIT_LIST_HEAD(&sc->luns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 usb_init_urb(&sc->work_urb);
2172 tasklet_init(&sc->tasklet, ub_scsi_action, (unsigned long)sc);
2173 atomic_set(&sc->poison, 0);
David Howellsc4028952006-11-22 14:57:56 +00002174 INIT_WORK(&sc->reset_work, ub_reset_task);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002175 init_waitqueue_head(&sc->reset_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
2177 init_timer(&sc->work_timer);
2178 sc->work_timer.data = (unsigned long) sc;
2179 sc->work_timer.function = ub_urb_timeout;
2180
2181 ub_init_completion(&sc->work_done);
2182 sc->work_done.done = 1; /* A little yuk, but oh well... */
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 sc->dev = interface_to_usbdev(intf);
2185 sc->intf = intf;
2186 // sc->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 usb_set_intfdata(intf, sc);
2188 usb_get_dev(sc->dev);
Pete Zaitcev77ef6c42006-05-03 00:16:00 -07002189 /*
2190 * Since we give the interface struct to the block level through
2191 * disk->driverfs_dev, we have to pin it. Otherwise, block_uevent
2192 * oopses on close after a disconnect (kernels 2.6.16 and up).
2193 */
2194 usb_get_intf(sc->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Pete Zaitcevf4800072005-05-01 16:05:40 -07002196 snprintf(sc->name, 12, DRV_NAME "(%d.%d)",
2197 sc->dev->bus->busnum, sc->dev->devnum);
2198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 /* XXX Verify that we can handle the device (from descriptors) */
2200
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002201 if (ub_get_pipes(sc, sc->dev, intf) != 0)
2202 goto err_dev_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 /*
2205 * At this point, all USB initialization is done, do upper layer.
2206 * We really hate halfway initialized structures, so from the
2207 * invariants perspective, this ub_dev is fully constructed at
2208 * this point.
2209 */
2210
2211 /*
2212 * This is needed to clear toggles. It is a problem only if we do
2213 * `rmmod ub && modprobe ub` without disconnects, but we like that.
2214 */
Pete Zaitcevc6c88832005-09-22 00:49:45 -07002215#if 0 /* iPod Mini fails if we do this (big white iPod works) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
2217 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
Pete Zaitcevc6c88832005-09-22 00:49:45 -07002218#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
2220 /*
2221 * The way this is used by the startup code is a little specific.
2222 * A SCSI check causes a USB stall. Our common case code sees it
2223 * and clears the check, after which the device is ready for use.
2224 * But if a check was not present, any command other than
2225 * TEST_UNIT_READY ends with a lockup (including REQUEST_SENSE).
2226 *
2227 * If we neglect to clear the SCSI check, the first real command fails
2228 * (which is the capacity readout). We clear that and retry, but why
2229 * causing spurious retries for no reason.
2230 *
2231 * Revalidation may start with its own TEST_UNIT_READY, but that one
2232 * has to succeed, so we clear checks with an additional one here.
2233 * In any case it's not our business how revaliadation is implemented.
2234 */
Pete Zaitcevb5600332006-05-25 20:08:50 -07002235 for (i = 0; i < 3; i++) { /* Retries for the schwag key from KS'04 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07002236 if ((rc = ub_sync_tur(sc, NULL)) <= 0) break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 if (rc != 0x6) break;
2238 msleep(10);
2239 }
2240
Pete Zaitcevf4800072005-05-01 16:05:40 -07002241 nluns = 1;
2242 for (i = 0; i < 3; i++) {
Pete Zaitcev11a223a2006-03-02 16:53:00 -08002243 if ((rc = ub_sync_getmaxlun(sc)) < 0)
Pete Zaitcevf4800072005-05-01 16:05:40 -07002244 break;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002245 if (rc != 0) {
2246 nluns = rc;
2247 break;
2248 }
Pete Zaitcev9f793d22005-06-06 13:54:59 -07002249 msleep(100);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Pete Zaitcevf4800072005-05-01 16:05:40 -07002252 for (i = 0; i < nluns; i++) {
2253 ub_probe_lun(sc, i);
2254 }
2255 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002257err_dev_desc:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002258 usb_set_intfdata(intf, NULL);
Pete Zaitcev77ef6c42006-05-03 00:16:00 -07002259 usb_put_intf(sc->intf);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002260 usb_put_dev(sc->dev);
2261 kfree(sc);
2262err_core:
2263 return rc;
2264}
2265
2266static int ub_probe_lun(struct ub_dev *sc, int lnum)
2267{
2268 struct ub_lun *lun;
Jens Axboe165125e2007-07-24 09:28:11 +02002269 struct request_queue *q;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002270 struct gendisk *disk;
2271 int rc;
2272
2273 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08002274 if ((lun = kzalloc(sizeof(struct ub_lun), GFP_KERNEL)) == NULL)
Pete Zaitcevf4800072005-05-01 16:05:40 -07002275 goto err_alloc;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002276 lun->num = lnum;
2277
2278 rc = -ENOSR;
2279 if ((lun->id = ub_id_get()) == -1)
2280 goto err_id;
2281
2282 lun->udev = sc;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002283
2284 snprintf(lun->name, 16, DRV_NAME "%c(%d.%d.%d)",
2285 lun->id + 'a', sc->dev->bus->busnum, sc->dev->devnum, lun->num);
2286
2287 lun->removable = 1; /* XXX Query this from the device */
2288 lun->changed = 1; /* ub_revalidate clears only */
Pete Zaitcevf4800072005-05-01 16:05:40 -07002289 ub_revalidate(sc, lun);
2290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 rc = -ENOMEM;
Pete Zaitcev4fb729f2005-12-17 02:34:12 -08002292 if ((disk = alloc_disk(UB_PARTS_PER_LUN)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 goto err_diskalloc;
2294
Pete Zaitcevf4800072005-05-01 16:05:40 -07002295 sprintf(disk->disk_name, DRV_NAME "%c", lun->id + 'a');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 disk->major = UB_MAJOR;
Pete Zaitcev4fb729f2005-12-17 02:34:12 -08002297 disk->first_minor = lun->id * UB_PARTS_PER_LUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 disk->fops = &ub_bd_fops;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002299 disk->private_data = lun;
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002300 disk->driverfs_dev = &sc->intf->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
2302 rc = -ENOMEM;
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002303 if ((q = blk_init_queue(ub_request_fn, sc->lock)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 goto err_blkqinit;
2305
2306 disk->queue = q;
2307
Pete Zaitcevf4800072005-05-01 16:05:40 -07002308 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
Martin K. Petersen8a783622010-02-26 00:20:39 -05002309 blk_queue_max_segments(q, UB_MAX_REQ_SG);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002310 blk_queue_segment_boundary(q, 0xffffffff); /* Dubious. */
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05002311 blk_queue_max_hw_sectors(q, UB_MAX_SECTORS);
Martin K. Petersene1defc42009-05-22 17:17:49 -04002312 blk_queue_logical_block_size(q, lun->capacity.bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Pete Zaitcev688e9fb2006-05-25 20:04:54 -07002314 lun->disk = disk;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002315 q->queuedata = lun;
Pete Zaitcev688e9fb2006-05-25 20:04:54 -07002316 list_add(&lun->link, &sc->luns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Pete Zaitcevf4800072005-05-01 16:05:40 -07002318 set_capacity(disk, lun->capacity.nsec);
2319 if (lun->removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 disk->flags |= GENHD_FL_REMOVABLE;
2321
2322 add_disk(disk);
2323
2324 return 0;
2325
2326err_blkqinit:
2327 put_disk(disk);
2328err_diskalloc:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002329 ub_id_put(lun->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330err_id:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002331 kfree(lun);
2332err_alloc:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 return rc;
2334}
2335
2336static void ub_disconnect(struct usb_interface *intf)
2337{
2338 struct ub_dev *sc = usb_get_intfdata(intf);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002339 struct ub_lun *lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 unsigned long flags;
2341
2342 /*
2343 * Prevent ub_bd_release from pulling the rug from under us.
2344 * XXX This is starting to look like a kref.
2345 * XXX Why not to take this ref at probe time?
2346 */
2347 spin_lock_irqsave(&ub_lock, flags);
2348 sc->openc++;
2349 spin_unlock_irqrestore(&ub_lock, flags);
2350
2351 /*
Pete Zaitcev9029b172008-04-19 14:45:24 -07002352 * Fence stall clearings, operations triggered by unlinkings and so on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 * We do not attempt to unlink any URBs, because we do not trust the
2354 * unlink paths in HC drivers. Also, we get -84 upon disconnect anyway.
2355 */
2356 atomic_set(&sc->poison, 1);
2357
2358 /*
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002359 * Wait for reset to end, if any.
2360 */
2361 wait_event(sc->reset_wait, !sc->reset);
2362
2363 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 * Blow away queued commands.
2365 *
2366 * Actually, this never works, because before we get here
2367 * the HCD terminates outstanding URB(s). It causes our
2368 * SCSI command queue to advance, commands fail to submit,
2369 * and the whole queue drains. So, we just use this code to
2370 * print warnings.
2371 */
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002372 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 {
2374 struct ub_scsi_cmd *cmd;
2375 int cnt = 0;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002376 while ((cmd = ub_cmdq_peek(sc)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 cmd->error = -ENOTCONN;
2378 cmd->state = UB_CMDST_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 ub_cmdq_pop(sc);
2380 (*cmd->done)(sc, cmd);
2381 cnt++;
2382 }
2383 if (cnt != 0) {
2384 printk(KERN_WARNING "%s: "
2385 "%d was queued after shutdown\n", sc->name, cnt);
2386 }
2387 }
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002388 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390 /*
2391 * Unregister the upper layer.
2392 */
Matthias Kaehlckea69228d2007-07-09 12:03:07 -07002393 list_for_each_entry(lun, &sc->luns, link) {
Pete Zaitcev688e9fb2006-05-25 20:04:54 -07002394 del_gendisk(lun->disk);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002395 /*
2396 * I wish I could do:
Nick Piggin75ad23b2008-04-29 14:48:33 +02002397 * queue_flag_set(QUEUE_FLAG_DEAD, q);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002398 * As it is, we rely on our internal poisoning and let
2399 * the upper levels to spin furiously failing all the I/O.
2400 */
2401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
2403 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 * Testing for -EINPROGRESS is always a bug, so we are bending
2405 * the rules a little.
2406 */
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002407 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 if (sc->work_urb.status == -EINPROGRESS) { /* janitors: ignore */
2409 printk(KERN_WARNING "%s: "
2410 "URB is active after disconnect\n", sc->name);
2411 }
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002412 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
2414 /*
Pete Zaitcev9029b172008-04-19 14:45:24 -07002415 * There is virtually no chance that other CPU runs a timeout so long
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 * after ub_urb_complete should have called del_timer, but only if HCD
2417 * didn't forget to deliver a callback on unlink.
2418 */
2419 del_timer_sync(&sc->work_timer);
2420
2421 /*
2422 * At this point there must be no commands coming from anyone
2423 * and no URBs left in transit.
2424 */
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 ub_put(sc);
2427}
2428
2429static struct usb_driver ub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 .name = "ub",
2431 .probe = ub_probe,
2432 .disconnect = ub_disconnect,
2433 .id_table = ub_usb_ids,
Pete Zaitcevd73b7aff2008-11-10 21:11:11 -07002434 .pre_reset = ub_pre_reset,
2435 .post_reset = ub_post_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436};
2437
2438static int __init ub_init(void)
2439{
2440 int rc;
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002441 int i;
2442
Sebastian Andrzej Siewior7396bd92012-03-15 21:59:36 +01002443 pr_info("'Low Performance USB Block' driver is deprecated. "
2444 "Please switch to usb-storage\n");
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002445 for (i = 0; i < UB_QLOCK_NUM; i++)
2446 spin_lock_init(&ub_qlockv[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if ((rc = register_blkdev(UB_MAJOR, DRV_NAME)) != 0)
2449 goto err_regblkdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
2451 if ((rc = usb_register(&ub_driver)) != 0)
2452 goto err_register;
2453
Pete Zaitceva00828e2005-10-22 20:15:09 -07002454 usb_usual_set_present(USB_US_TYPE_UB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 return 0;
2456
2457err_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 unregister_blkdev(UB_MAJOR, DRV_NAME);
2459err_regblkdev:
2460 return rc;
2461}
2462
2463static void __exit ub_exit(void)
2464{
2465 usb_deregister(&ub_driver);
2466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 unregister_blkdev(UB_MAJOR, DRV_NAME);
Pete Zaitceva00828e2005-10-22 20:15:09 -07002468 usb_usual_clear_present(USB_US_TYPE_UB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469}
2470
2471module_init(ub_init);
2472module_exit(ub_exit);
2473
2474MODULE_LICENSE("GPL");