blob: 048d71d244d7335d3e719e916632ef1a71079465 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <scsi/scsi.h>
31
32#define DRV_NAME "ub"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#define UB_MAJOR 180
35
36/*
Pete Zaitcev1872bce2005-07-27 11:43:51 -070037 * The command state machine is the key model for understanding of this driver.
38 *
39 * The general rule is that all transitions are done towards the bottom
40 * of the diagram, thus preventing any loops.
41 *
42 * An exception to that is how the STAT state is handled. A counter allows it
43 * to be re-entered along the path marked with [C].
44 *
45 * +--------+
46 * ! INIT !
47 * +--------+
48 * !
49 * ub_scsi_cmd_start fails ->--------------------------------------\
50 * ! !
51 * V !
52 * +--------+ !
53 * ! CMD ! !
54 * +--------+ !
55 * ! +--------+ !
56 * was -EPIPE -->-------------------------------->! CLEAR ! !
57 * ! +--------+ !
58 * ! ! !
59 * was error -->------------------------------------- ! --------->\
60 * ! ! !
61 * /--<-- cmd->dir == NONE ? ! !
62 * ! ! ! !
63 * ! V ! !
64 * ! +--------+ ! !
65 * ! ! DATA ! ! !
66 * ! +--------+ ! !
67 * ! ! +---------+ ! !
68 * ! was -EPIPE -->--------------->! CLR2STS ! ! !
69 * ! ! +---------+ ! !
70 * ! ! ! ! !
71 * ! ! was error -->---- ! --------->\
72 * ! was error -->--------------------- ! ------------- ! --------->\
73 * ! ! ! ! !
74 * ! V ! ! !
75 * \--->+--------+ ! ! !
76 * ! STAT !<--------------------------/ ! !
77 * /--->+--------+ ! !
78 * ! ! ! !
79 * [C] was -EPIPE -->-----------\ ! !
80 * ! ! ! ! !
81 * +<---- len == 0 ! ! !
82 * ! ! ! ! !
83 * ! was error -->--------------------------------------!---------->\
84 * ! ! ! ! !
85 * +<---- bad CSW ! ! !
86 * +<---- bad tag ! ! !
87 * ! ! V ! !
88 * ! ! +--------+ ! !
89 * ! ! ! CLRRS ! ! !
90 * ! ! +--------+ ! !
91 * ! ! ! ! !
92 * \------- ! --------------------[C]--------\ ! !
93 * ! ! ! !
94 * cmd->error---\ +--------+ ! !
95 * ! +--------------->! SENSE !<----------/ !
96 * STAT_FAIL----/ +--------+ !
97 * ! ! V
98 * ! V +--------+
99 * \--------------------------------\--------------------->! DONE !
100 * +--------+
101 */
102
103/*
Pete Zaitcevf4800072005-05-01 16:05:40 -0700104 * This many LUNs per USB device.
105 * Every one of them takes a host, see UB_MAX_HOSTS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 */
Pete Zaitcev9f793d22005-06-06 13:54:59 -0700107#define UB_MAX_LUNS 9
Pete Zaitcevf4800072005-05-01 16:05:40 -0700108
109/*
110 */
111
Pete Zaitcev4fb729f2005-12-17 02:34:12 -0800112#define UB_PARTS_PER_LUN 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114#define UB_MAX_CDB_SIZE 16 /* Corresponds to Bulk */
115
116#define UB_SENSE_SIZE 18
117
118/*
119 */
120
121/* command block wrapper */
122struct bulk_cb_wrap {
123 __le32 Signature; /* contains 'USBC' */
124 u32 Tag; /* unique per command id */
125 __le32 DataTransferLength; /* size of data */
126 u8 Flags; /* direction in bit 0 */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700127 u8 Lun; /* LUN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 u8 Length; /* of of the CDB */
129 u8 CDB[UB_MAX_CDB_SIZE]; /* max command */
130};
131
132#define US_BULK_CB_WRAP_LEN 31
133#define US_BULK_CB_SIGN 0x43425355 /*spells out USBC */
134#define US_BULK_FLAG_IN 1
135#define US_BULK_FLAG_OUT 0
136
137/* command status wrapper */
138struct bulk_cs_wrap {
139 __le32 Signature; /* should = 'USBS' */
140 u32 Tag; /* same as original command */
141 __le32 Residue; /* amount not transferred */
142 u8 Status; /* see below */
143};
144
145#define US_BULK_CS_WRAP_LEN 13
146#define US_BULK_CS_SIGN 0x53425355 /* spells out 'USBS' */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#define US_BULK_STAT_OK 0
148#define US_BULK_STAT_FAIL 1
149#define US_BULK_STAT_PHASE 2
150
151/* bulk-only class specific requests */
152#define US_BULK_RESET_REQUEST 0xff
153#define US_BULK_GET_MAX_LUN 0xfe
154
155/*
156 */
157struct ub_dev;
158
Pete Zaitcev64bd8452005-09-22 00:48:29 -0700159#define UB_MAX_REQ_SG 9 /* cdrecord requires 32KB and maybe a header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#define UB_MAX_SECTORS 64
161
162/*
163 * A second is more than enough for a 32K transfer (UB_MAX_SECTORS)
164 * even if a webcam hogs the bus, but some devices need time to spin up.
165 */
166#define UB_URB_TIMEOUT (HZ*2)
167#define UB_DATA_TIMEOUT (HZ*5) /* ZIP does spin-ups in the data phase */
168#define UB_STAT_TIMEOUT (HZ*5) /* Same spinups and eject for a dataless cmd. */
169#define UB_CTRL_TIMEOUT (HZ/2) /* 500ms ought to be enough to clear a stall */
170
171/*
172 * An instance of a SCSI command in transit.
173 */
174#define UB_DIR_NONE 0
175#define UB_DIR_READ 1
176#define UB_DIR_ILLEGAL2 2
177#define UB_DIR_WRITE 3
178
179#define UB_DIR_CHAR(c) (((c)==UB_DIR_WRITE)? 'w': \
180 (((c)==UB_DIR_READ)? 'r': 'n'))
181
182enum ub_scsi_cmd_state {
183 UB_CMDST_INIT, /* Initial state */
184 UB_CMDST_CMD, /* Command submitted */
185 UB_CMDST_DATA, /* Data phase */
186 UB_CMDST_CLR2STS, /* Clearing before requesting status */
187 UB_CMDST_STAT, /* Status phase */
188 UB_CMDST_CLEAR, /* Clearing a stall (halt, actually) */
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700189 UB_CMDST_CLRRS, /* Clearing before retrying status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 UB_CMDST_SENSE, /* Sending Request Sense */
191 UB_CMDST_DONE /* Final state */
192};
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194struct ub_scsi_cmd {
195 unsigned char cdb[UB_MAX_CDB_SIZE];
196 unsigned char cdb_len;
197
198 unsigned char dir; /* 0 - none, 1 - read, 3 - write. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 enum ub_scsi_cmd_state state;
200 unsigned int tag;
201 struct ub_scsi_cmd *next;
202
203 int error; /* Return code - valid upon done */
204 unsigned int act_len; /* Return size */
205 unsigned char key, asc, ascq; /* May be valid if error==-EIO */
206
207 int stat_count; /* Retries getting status. */
Pete Zaitcev2c51ae72008-04-19 14:32:18 -0700208 unsigned int timeo; /* jiffies until rq->timeout changes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 unsigned int len; /* Requested length */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700211 unsigned int current_sg;
212 unsigned int nsg; /* sgv[nsg] */
213 struct scatterlist sgv[UB_MAX_REQ_SG];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Pete Zaitcevf4800072005-05-01 16:05:40 -0700215 struct ub_lun *lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 void (*done)(struct ub_dev *, struct ub_scsi_cmd *);
217 void *back;
218};
219
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800220struct ub_request {
221 struct request *rq;
222 unsigned int current_try;
223 unsigned int nsg; /* sgv[nsg] */
224 struct scatterlist sgv[UB_MAX_REQ_SG];
225};
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227/*
228 */
229struct ub_capacity {
230 unsigned long nsec; /* Linux size - 512 byte sectors */
231 unsigned int bsize; /* Linux hardsect_size */
232 unsigned int bshift; /* Shift between 512 and hard sects */
233};
234
235/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * This is a direct take-off from linux/include/completion.h
237 * The difference is that I do not wait on this thing, just poll.
238 * When I want to wait (ub_probe), I just use the stock completion.
239 *
240 * Note that INIT_COMPLETION takes no lock. It is correct. But why
241 * in the bloody hell that thing takes struct instead of pointer to struct
242 * is quite beyond me. I just copied it from the stock completion.
243 */
244struct ub_completion {
245 unsigned int done;
246 spinlock_t lock;
247};
248
249static inline void ub_init_completion(struct ub_completion *x)
250{
251 x->done = 0;
252 spin_lock_init(&x->lock);
253}
254
255#define UB_INIT_COMPLETION(x) ((x).done = 0)
256
257static void ub_complete(struct ub_completion *x)
258{
259 unsigned long flags;
260
261 spin_lock_irqsave(&x->lock, flags);
262 x->done++;
263 spin_unlock_irqrestore(&x->lock, flags);
264}
265
266static int ub_is_completed(struct ub_completion *x)
267{
268 unsigned long flags;
269 int ret;
270
271 spin_lock_irqsave(&x->lock, flags);
272 ret = x->done;
273 spin_unlock_irqrestore(&x->lock, flags);
274 return ret;
275}
276
277/*
278 */
279struct ub_scsi_cmd_queue {
280 int qlen, qmax;
281 struct ub_scsi_cmd *head, *tail;
282};
283
284/*
Pete Zaitcevf4800072005-05-01 16:05:40 -0700285 * The block device instance (one per LUN).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700287struct ub_lun {
288 struct ub_dev *udev;
289 struct list_head link;
290 struct gendisk *disk;
291 int id; /* Host index */
292 int num; /* LUN number */
293 char name[16];
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 int changed; /* Media was changed */
296 int removable;
297 int readonly;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700298
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800299 struct ub_request urq;
300
Pete Zaitcevf4800072005-05-01 16:05:40 -0700301 /* Use Ingo's mempool if or when we have more than one command. */
302 /*
303 * Currently we never need more than one command for the whole device.
304 * However, giving every LUN a command is a cheap and automatic way
305 * to enforce fairness between them.
306 */
307 int cmda[1];
308 struct ub_scsi_cmd cmdv[1];
309
310 struct ub_capacity capacity;
311};
312
313/*
314 * The USB device instance.
315 */
316struct ub_dev {
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800317 spinlock_t *lock;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700318 atomic_t poison; /* The USB device is disconnected */
319 int openc; /* protected by ub_lock! */
320 /* kref is too implicit for our taste */
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800321 int reset; /* Reset is running */
Pete Zaitcev0da13c82008-04-19 14:42:49 -0700322 int bad_resid;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700323 unsigned int tagcnt;
324 char name[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 struct usb_device *dev;
326 struct usb_interface *intf;
327
Pete Zaitcevf4800072005-05-01 16:05:40 -0700328 struct list_head luns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 unsigned int send_bulk_pipe; /* cached pipe values */
331 unsigned int recv_bulk_pipe;
332 unsigned int send_ctrl_pipe;
333 unsigned int recv_ctrl_pipe;
334
335 struct tasklet_struct tasklet;
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 struct ub_scsi_cmd_queue cmd_queue;
338 struct ub_scsi_cmd top_rqs_cmd; /* REQUEST SENSE */
339 unsigned char top_sense[UB_SENSE_SIZE];
340
341 struct ub_completion work_done;
342 struct urb work_urb;
343 struct timer_list work_timer;
344 int last_pipe; /* What might need clearing */
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700345 __le32 signature; /* Learned signature */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 struct bulk_cb_wrap work_bcb;
347 struct bulk_cs_wrap work_bcs;
348 struct usb_ctrlrequest work_cr;
349
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800350 struct work_struct reset_work;
351 wait_queue_head_t reset_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352};
353
354/*
355 */
356static void ub_cleanup(struct ub_dev *sc);
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700357static int ub_request_fn_1(struct ub_lun *lun, struct request *rq);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800358static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
359 struct ub_scsi_cmd *cmd, struct ub_request *urq);
360static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
361 struct ub_scsi_cmd *cmd, struct ub_request *urq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700363static void ub_end_rq(struct request *rq, unsigned int status,
364 unsigned int cmd_len);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800365static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
366 struct ub_request *urq, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
David Howells7d12e782006-10-05 14:55:46 +0100368static void ub_urb_complete(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369static void ub_scsi_action(unsigned long _dev);
370static void ub_scsi_dispatch(struct ub_dev *sc);
371static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700372static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc);
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700374static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700376static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
378static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
379 int stalled_pipe);
380static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd);
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -0800381static void ub_reset_enter(struct ub_dev *sc, int try);
David Howellsc4028952006-11-22 14:57:56 +0000382static void ub_reset_task(struct work_struct *work);
Pete Zaitcevf4800072005-05-01 16:05:40 -0700383static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun);
384static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
385 struct ub_capacity *ret);
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -0800386static int ub_sync_reset(struct ub_dev *sc);
387static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe);
Pete Zaitcevf4800072005-05-01 16:05:40 -0700388static int ub_probe_lun(struct ub_dev *sc, int lnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390/*
391 */
Pete Zaitceva00828e2005-10-22 20:15:09 -0700392#ifdef CONFIG_USB_LIBUSUAL
393
394#define ub_usb_ids storage_usb_ids
395#else
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397static struct usb_device_id ub_usb_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) },
399 { }
400};
401
402MODULE_DEVICE_TABLE(usb, ub_usb_ids);
Pete Zaitceva00828e2005-10-22 20:15:09 -0700403#endif /* CONFIG_USB_LIBUSUAL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405/*
406 * Find me a way to identify "next free minor" for add_disk(),
407 * and the array disappears the next day. However, the number of
408 * hosts has something to do with the naming and /proc/partitions.
409 * This has to be thought out in detail before changing.
410 * If UB_MAX_HOST was 1000, we'd use a bitmap. Or a better data structure.
411 */
412#define UB_MAX_HOSTS 26
413static char ub_hostv[UB_MAX_HOSTS];
Pete Zaitcevf4800072005-05-01 16:05:40 -0700414
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800415#define UB_QLOCK_NUM 5
416static spinlock_t ub_qlockv[UB_QLOCK_NUM];
417static int ub_qlock_next = 0;
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419static DEFINE_SPINLOCK(ub_lock); /* Locks globals and ->openc */
420
421/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 * The id allocator.
423 *
424 * This also stores the host for indexing by minor, which is somewhat dirty.
425 */
426static int ub_id_get(void)
427{
428 unsigned long flags;
429 int i;
430
431 spin_lock_irqsave(&ub_lock, flags);
432 for (i = 0; i < UB_MAX_HOSTS; i++) {
433 if (ub_hostv[i] == 0) {
434 ub_hostv[i] = 1;
435 spin_unlock_irqrestore(&ub_lock, flags);
436 return i;
437 }
438 }
439 spin_unlock_irqrestore(&ub_lock, flags);
440 return -1;
441}
442
443static void ub_id_put(int id)
444{
445 unsigned long flags;
446
447 if (id < 0 || id >= UB_MAX_HOSTS) {
448 printk(KERN_ERR DRV_NAME ": bad host ID %d\n", id);
449 return;
450 }
451
452 spin_lock_irqsave(&ub_lock, flags);
453 if (ub_hostv[id] == 0) {
454 spin_unlock_irqrestore(&ub_lock, flags);
455 printk(KERN_ERR DRV_NAME ": freeing free host ID %d\n", id);
456 return;
457 }
458 ub_hostv[id] = 0;
459 spin_unlock_irqrestore(&ub_lock, flags);
460}
461
462/*
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800463 * This is necessitated by the fact that blk_cleanup_queue does not
464 * necesserily destroy the queue. Instead, it may merely decrease q->refcnt.
465 * Since our blk_init_queue() passes a spinlock common with ub_dev,
466 * we have life time issues when ub_cleanup frees ub_dev.
467 */
468static spinlock_t *ub_next_lock(void)
469{
470 unsigned long flags;
471 spinlock_t *ret;
472
473 spin_lock_irqsave(&ub_lock, flags);
474 ret = &ub_qlockv[ub_qlock_next];
475 ub_qlock_next = (ub_qlock_next + 1) % UB_QLOCK_NUM;
476 spin_unlock_irqrestore(&ub_lock, flags);
477 return ret;
478}
479
480/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 * Downcount for deallocation. This rides on two assumptions:
482 * - once something is poisoned, its refcount cannot grow
483 * - opens cannot happen at this time (del_gendisk was done)
484 * If the above is true, we can drop the lock, which we need for
485 * blk_cleanup_queue(): the silly thing may attempt to sleep.
486 * [Actually, it never needs to sleep for us, but it calls might_sleep()]
487 */
488static void ub_put(struct ub_dev *sc)
489{
490 unsigned long flags;
491
492 spin_lock_irqsave(&ub_lock, flags);
493 --sc->openc;
494 if (sc->openc == 0 && atomic_read(&sc->poison)) {
495 spin_unlock_irqrestore(&ub_lock, flags);
496 ub_cleanup(sc);
497 } else {
498 spin_unlock_irqrestore(&ub_lock, flags);
499 }
500}
501
502/*
503 * Final cleanup and deallocation.
504 */
505static void ub_cleanup(struct ub_dev *sc)
506{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700507 struct list_head *p;
508 struct ub_lun *lun;
Jens Axboe165125e2007-07-24 09:28:11 +0200509 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Pete Zaitcevf4800072005-05-01 16:05:40 -0700511 while (!list_empty(&sc->luns)) {
512 p = sc->luns.next;
513 lun = list_entry(p, struct ub_lun, link);
514 list_del(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Pete Zaitcevf4800072005-05-01 16:05:40 -0700516 /* I don't think queue can be NULL. But... Stolen from sx8.c */
517 if ((q = lun->disk->queue) != NULL)
518 blk_cleanup_queue(q);
519 /*
520 * If we zero disk->private_data BEFORE put_disk, we have
521 * to check for NULL all over the place in open, release,
522 * check_media and revalidate, because the block level
523 * semaphore is well inside the put_disk.
524 * But we cannot zero after the call, because *disk is gone.
525 * The sd.c is blatantly racy in this area.
526 */
527 /* disk->private_data = NULL; */
528 put_disk(lun->disk);
529 lun->disk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Pete Zaitcevf4800072005-05-01 16:05:40 -0700531 ub_id_put(lun->id);
532 kfree(lun);
533 }
534
Pete Zaitcev77ef6c42006-05-03 00:16:00 -0700535 usb_set_intfdata(sc->intf, NULL);
536 usb_put_intf(sc->intf);
537 usb_put_dev(sc->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 kfree(sc);
539}
540
541/*
542 * The "command allocator".
543 */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700544static struct ub_scsi_cmd *ub_get_cmd(struct ub_lun *lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 struct ub_scsi_cmd *ret;
547
Pete Zaitcevf4800072005-05-01 16:05:40 -0700548 if (lun->cmda[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return NULL;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700550 ret = &lun->cmdv[0];
551 lun->cmda[0] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return ret;
553}
554
Pete Zaitcevf4800072005-05-01 16:05:40 -0700555static void ub_put_cmd(struct ub_lun *lun, struct ub_scsi_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700557 if (cmd != &lun->cmdv[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 printk(KERN_WARNING "%s: releasing a foreign cmd %p\n",
Pete Zaitcevf4800072005-05-01 16:05:40 -0700559 lun->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return;
561 }
Pete Zaitcevf4800072005-05-01 16:05:40 -0700562 if (!lun->cmda[0]) {
563 printk(KERN_WARNING "%s: releasing a free cmd\n", lun->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return;
565 }
Pete Zaitcevf4800072005-05-01 16:05:40 -0700566 lun->cmda[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
569/*
570 * The command queue.
571 */
572static void ub_cmdq_add(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
573{
574 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
575
576 if (t->qlen++ == 0) {
577 t->head = cmd;
578 t->tail = cmd;
579 } else {
580 t->tail->next = cmd;
581 t->tail = cmd;
582 }
583
584 if (t->qlen > t->qmax)
585 t->qmax = t->qlen;
586}
587
588static void ub_cmdq_insert(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
589{
590 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
591
592 if (t->qlen++ == 0) {
593 t->head = cmd;
594 t->tail = cmd;
595 } else {
596 cmd->next = t->head;
597 t->head = cmd;
598 }
599
600 if (t->qlen > t->qmax)
601 t->qmax = t->qlen;
602}
603
604static struct ub_scsi_cmd *ub_cmdq_pop(struct ub_dev *sc)
605{
606 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
607 struct ub_scsi_cmd *cmd;
608
609 if (t->qlen == 0)
610 return NULL;
611 if (--t->qlen == 0)
612 t->tail = NULL;
613 cmd = t->head;
614 t->head = cmd->next;
615 cmd->next = NULL;
616 return cmd;
617}
618
619#define ub_cmdq_peek(sc) ((sc)->cmd_queue.head)
620
621/*
622 * The request function is our main entry point
623 */
624
Jens Axboe165125e2007-07-24 09:28:11 +0200625static void ub_request_fn(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700627 struct ub_lun *lun = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 struct request *rq;
629
630 while ((rq = elv_next_request(q)) != NULL) {
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700631 if (ub_request_fn_1(lun, rq) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 blk_stop_queue(q);
633 break;
634 }
635 }
636}
637
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700638static int ub_request_fn_1(struct ub_lun *lun, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700640 struct ub_dev *sc = lun->udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct ub_scsi_cmd *cmd;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800642 struct ub_request *urq;
643 int n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700645 if (atomic_read(&sc->poison)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 blkdev_dequeue_request(rq);
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700647 ub_end_rq(rq, DID_NO_CONNECT << 16, blk_rq_bytes(rq));
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700648 return 0;
649 }
650
651 if (lun->changed && !blk_pc_request(rq)) {
652 blkdev_dequeue_request(rq);
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700653 ub_end_rq(rq, SAM_STAT_CHECK_CONDITION, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return 0;
655 }
656
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800657 if (lun->urq.rq != NULL)
658 return -1;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700659 if ((cmd = ub_get_cmd(lun)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return -1;
661 memset(cmd, 0, sizeof(struct ub_scsi_cmd));
662
663 blkdev_dequeue_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800665 urq = &lun->urq;
666 memset(urq, 0, sizeof(struct ub_request));
667 urq->rq = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 /*
670 * get scatterlist from block layer
671 */
Pete Zaitcev541645b2008-02-09 00:10:17 -0800672 sg_init_table(&urq->sgv[0], UB_MAX_REQ_SG);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800673 n_elem = blk_rq_map_sg(lun->disk->queue, rq, &urq->sgv[0]);
674 if (n_elem < 0) {
Pete Zaitcevb5600332006-05-25 20:08:50 -0700675 /* Impossible, because blk_rq_map_sg should not hit ENOMEM. */
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700676 printk(KERN_INFO "%s: failed request map (%d)\n",
Pete Zaitcevb5600332006-05-25 20:08:50 -0700677 lun->name, n_elem);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800678 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700680 if (n_elem > UB_MAX_REQ_SG) { /* Paranoia */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 printk(KERN_WARNING "%s: request with %d segments\n",
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800682 lun->name, n_elem);
683 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800685 urq->nsg = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800687 if (blk_pc_request(rq)) {
688 ub_cmd_build_packet(sc, lun, cmd, urq);
689 } else {
690 ub_cmd_build_block(sc, lun, cmd, urq);
691 }
692 cmd->state = UB_CMDST_INIT;
693 cmd->lun = lun;
694 cmd->done = ub_rw_cmd_done;
695 cmd->back = urq;
696
697 cmd->tag = sc->tagcnt++;
698 if (ub_submit_scsi(sc, cmd) != 0)
699 goto drop;
700
701 return 0;
702
703drop:
704 ub_put_cmd(lun, cmd);
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700705 ub_end_rq(rq, DID_ERROR << 16, blk_rq_bytes(rq));
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800706 return 0;
707}
708
709static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
710 struct ub_scsi_cmd *cmd, struct ub_request *urq)
711{
712 struct request *rq = urq->rq;
713 unsigned int block, nblks;
714
715 if (rq_data_dir(rq) == WRITE)
716 cmd->dir = UB_DIR_WRITE;
717 else
718 cmd->dir = UB_DIR_READ;
719
720 cmd->nsg = urq->nsg;
721 memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 /*
724 * build the command
725 *
726 * The call to blk_queue_hardsect_size() guarantees that request
727 * is aligned, but it is given in terms of 512 byte units, always.
728 */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700729 block = rq->sector >> lun->capacity.bshift;
730 nblks = rq->nr_sectors >> lun->capacity.bshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800732 cmd->cdb[0] = (cmd->dir == UB_DIR_READ)? READ_10: WRITE_10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /* 10-byte uses 4 bytes of LBA: 2147483648KB, 2097152MB, 2048GB */
734 cmd->cdb[2] = block >> 24;
735 cmd->cdb[3] = block >> 16;
736 cmd->cdb[4] = block >> 8;
737 cmd->cdb[5] = block;
738 cmd->cdb[7] = nblks >> 8;
739 cmd->cdb[8] = nblks;
740 cmd->cdb_len = 10;
741
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700742 cmd->len = rq->nr_sectors * 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800745static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
746 struct ub_scsi_cmd *cmd, struct ub_request *urq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800748 struct request *rq = urq->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 if (rq->data_len == 0) {
751 cmd->dir = UB_DIR_NONE;
752 } else {
753 if (rq_data_dir(rq) == WRITE)
754 cmd->dir = UB_DIR_WRITE;
755 else
756 cmd->dir = UB_DIR_READ;
757 }
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700758
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800759 cmd->nsg = urq->nsg;
760 memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700761
762 memcpy(&cmd->cdb, rq->cmd, rq->cmd_len);
763 cmd->cdb_len = rq->cmd_len;
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 cmd->len = rq->data_len;
Pete Zaitcev2c51ae72008-04-19 14:32:18 -0700766
767 /*
768 * To reapply this to every URB is not as incorrect as it looks.
769 * In return, we avoid any complicated tracking calculations.
770 */
771 cmd->timeo = rq->timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
775{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700776 struct ub_lun *lun = cmd->lun;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800777 struct ub_request *urq = cmd->back;
778 struct request *rq;
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700779 unsigned int scsi_status;
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700780 unsigned int cmd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800782 rq = urq->rq;
783
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700784 if (cmd->error == 0) {
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700785 if (blk_pc_request(rq)) {
786 if (cmd->act_len >= rq->data_len)
787 rq->data_len = 0;
788 else
789 rq->data_len -= cmd->act_len;
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700790 scsi_status = 0;
791 } else {
792 if (cmd->act_len != cmd->len) {
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700793 scsi_status = SAM_STAT_CHECK_CONDITION;
794 } else {
795 scsi_status = 0;
796 }
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700797 }
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700798 } else {
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700799 if (blk_pc_request(rq)) {
800 /* UB_SENSE_SIZE is smaller than SCSI_SENSE_BUFFERSIZE */
801 memcpy(rq->sense, sc->top_sense, UB_SENSE_SIZE);
802 rq->sense_len = UB_SENSE_SIZE;
803 if (sc->top_sense[0] != 0)
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700804 scsi_status = SAM_STAT_CHECK_CONDITION;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700805 else
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700806 scsi_status = DID_ERROR << 16;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800807 } else {
Pete Zaitcev82fe26b2008-04-19 14:35:30 -0700808 if (cmd->error == -EIO &&
809 (cmd->key == 0 ||
810 cmd->key == MEDIUM_ERROR ||
811 cmd->key == UNIT_ATTENTION)) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800812 if (ub_rw_cmd_retry(sc, lun, urq, cmd) == 0)
813 return;
814 }
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700815 scsi_status = SAM_STAT_CHECK_CONDITION;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700816 }
817 }
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700818
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800819 urq->rq = NULL;
820
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700821 cmd_len = cmd->len;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700822 ub_put_cmd(lun, cmd);
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700823 ub_end_rq(rq, scsi_status, cmd_len);
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700824 blk_start_queue(lun->disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700827static void ub_end_rq(struct request *rq, unsigned int scsi_status,
828 unsigned int cmd_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Kiyoshi Ueda7d699ba2007-12-11 17:46:47 -0500830 int error;
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700831 long rqlen;
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700832
833 if (scsi_status == 0) {
Kiyoshi Ueda7d699ba2007-12-11 17:46:47 -0500834 error = 0;
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700835 } else {
Kiyoshi Ueda7d699ba2007-12-11 17:46:47 -0500836 error = -EIO;
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700837 rq->errors = scsi_status;
838 }
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700839 rqlen = blk_rq_bytes(rq); /* Oddly enough, this is the residue. */
840 if (__blk_end_request(rq, error, cmd_len)) {
841 printk(KERN_WARNING DRV_NAME
842 ": __blk_end_request blew, %s-cmd total %u rqlen %ld\n",
843 blk_pc_request(rq)? "pc": "fs", cmd_len, rqlen);
844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800847static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
848 struct ub_request *urq, struct ub_scsi_cmd *cmd)
849{
850
851 if (atomic_read(&sc->poison))
852 return -ENXIO;
853
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -0800854 ub_reset_enter(sc, urq->current_try);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800855
856 if (urq->current_try >= 3)
857 return -EIO;
858 urq->current_try++;
Pete Zaitcevb5600332006-05-25 20:08:50 -0700859
860 /* Remove this if anyone complains of flooding. */
861 printk(KERN_DEBUG "%s: dir %c len/act %d/%d "
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800862 "[sense %x %02x %02x] retry %d\n",
863 sc->name, UB_DIR_CHAR(cmd->dir), cmd->len, cmd->act_len,
864 cmd->key, cmd->asc, cmd->ascq, urq->current_try);
865
866 memset(cmd, 0, sizeof(struct ub_scsi_cmd));
867 ub_cmd_build_block(sc, lun, cmd, urq);
868
869 cmd->state = UB_CMDST_INIT;
870 cmd->lun = lun;
871 cmd->done = ub_rw_cmd_done;
872 cmd->back = urq;
873
874 cmd->tag = sc->tagcnt++;
875
876#if 0 /* Wasteful */
877 return ub_submit_scsi(sc, cmd);
878#else
879 ub_cmdq_add(sc, cmd);
880 return 0;
881#endif
882}
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884/*
885 * Submit a regular SCSI operation (not an auto-sense).
886 *
887 * The Iron Law of Good Submit Routine is:
888 * Zero return - callback is done, Nonzero return - callback is not done.
889 * No exceptions.
890 *
891 * Host is assumed locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 */
893static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
894{
895
896 if (cmd->state != UB_CMDST_INIT ||
897 (cmd->dir != UB_DIR_NONE && cmd->len == 0)) {
898 return -EINVAL;
899 }
900
901 ub_cmdq_add(sc, cmd);
902 /*
903 * We can call ub_scsi_dispatch(sc) right away here, but it's a little
904 * safer to jump to a tasklet, in case upper layers do something silly.
905 */
906 tasklet_schedule(&sc->tasklet);
907 return 0;
908}
909
910/*
911 * Submit the first URB for the queued command.
912 * This function does not deal with queueing in any way.
913 */
914static int ub_scsi_cmd_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
915{
916 struct bulk_cb_wrap *bcb;
917 int rc;
918
919 bcb = &sc->work_bcb;
920
921 /*
922 * ``If the allocation length is eighteen or greater, and a device
923 * server returns less than eithteen bytes of data, the application
924 * client should assume that the bytes not transferred would have been
925 * zeroes had the device server returned those bytes.''
926 *
927 * We zero sense for all commands so that when a packet request
928 * fails it does not return a stale sense.
929 */
930 memset(&sc->top_sense, 0, UB_SENSE_SIZE);
931
932 /* set up the command wrapper */
933 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
934 bcb->Tag = cmd->tag; /* Endianness is not important */
935 bcb->DataTransferLength = cpu_to_le32(cmd->len);
936 bcb->Flags = (cmd->dir == UB_DIR_READ) ? 0x80 : 0;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700937 bcb->Lun = (cmd->lun != NULL) ? cmd->lun->num : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 bcb->Length = cmd->cdb_len;
939
940 /* copy the command payload */
941 memcpy(bcb->CDB, cmd->cdb, UB_MAX_CDB_SIZE);
942
943 UB_INIT_COMPLETION(sc->work_done);
944
945 sc->last_pipe = sc->send_bulk_pipe;
946 usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->send_bulk_pipe,
947 bcb, US_BULK_CB_WRAP_LEN, ub_urb_complete, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
950 /* XXX Clear stalls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 ub_complete(&sc->work_done);
952 return rc;
953 }
954
955 sc->work_timer.expires = jiffies + UB_URB_TIMEOUT;
956 add_timer(&sc->work_timer);
957
958 cmd->state = UB_CMDST_CMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return 0;
960}
961
962/*
963 * Timeout handler.
964 */
965static void ub_urb_timeout(unsigned long arg)
966{
967 struct ub_dev *sc = (struct ub_dev *) arg;
968 unsigned long flags;
969
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800970 spin_lock_irqsave(sc->lock, flags);
Pete Zaitcevb31f8212006-01-05 00:14:02 -0800971 if (!ub_is_completed(&sc->work_done))
972 usb_unlink_urb(&sc->work_urb);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800973 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
976/*
977 * Completion routine for the work URB.
978 *
979 * This can be called directly from usb_submit_urb (while we have
980 * the sc->lock taken) and from an interrupt (while we do NOT have
981 * the sc->lock taken). Therefore, bounce this off to a tasklet.
982 */
David Howells7d12e782006-10-05 14:55:46 +0100983static void ub_urb_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
985 struct ub_dev *sc = urb->context;
986
987 ub_complete(&sc->work_done);
988 tasklet_schedule(&sc->tasklet);
989}
990
991static void ub_scsi_action(unsigned long _dev)
992{
993 struct ub_dev *sc = (struct ub_dev *) _dev;
994 unsigned long flags;
995
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800996 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 ub_scsi_dispatch(sc);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800998 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
1001static void ub_scsi_dispatch(struct ub_dev *sc)
1002{
1003 struct ub_scsi_cmd *cmd;
1004 int rc;
1005
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001006 while (!sc->reset && (cmd = ub_cmdq_peek(sc)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 if (cmd->state == UB_CMDST_DONE) {
1008 ub_cmdq_pop(sc);
1009 (*cmd->done)(sc, cmd);
1010 } else if (cmd->state == UB_CMDST_INIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if ((rc = ub_scsi_cmd_start(sc, cmd)) == 0)
1012 break;
1013 cmd->error = rc;
1014 cmd->state = UB_CMDST_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 } else {
1016 if (!ub_is_completed(&sc->work_done))
1017 break;
Pete Zaitcevb31f8212006-01-05 00:14:02 -08001018 del_timer(&sc->work_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 ub_scsi_urb_compl(sc, cmd);
1020 }
1021 }
1022}
1023
1024static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1025{
1026 struct urb *urb = &sc->work_urb;
1027 struct bulk_cs_wrap *bcs;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001028 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 int rc;
1030
1031 if (atomic_read(&sc->poison)) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001032 ub_state_done(sc, cmd, -ENODEV);
1033 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035
1036 if (cmd->state == UB_CMDST_CLEAR) {
1037 if (urb->status == -EPIPE) {
1038 /*
1039 * STALL while clearning STALL.
1040 * The control pipe clears itself - nothing to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001042 printk(KERN_NOTICE "%s: stall on control pipe\n",
1043 sc->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 goto Bad_End;
1045 }
1046
1047 /*
1048 * We ignore the result for the halt clear.
1049 */
1050
1051 /* reset the endpoint toggle */
1052 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1053 usb_pipeout(sc->last_pipe), 0);
1054
1055 ub_state_sense(sc, cmd);
1056
1057 } else if (cmd->state == UB_CMDST_CLR2STS) {
1058 if (urb->status == -EPIPE) {
Pete Zaitcevf4800072005-05-01 16:05:40 -07001059 printk(KERN_NOTICE "%s: stall on control pipe\n",
1060 sc->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 goto Bad_End;
1062 }
1063
1064 /*
1065 * We ignore the result for the halt clear.
1066 */
1067
1068 /* reset the endpoint toggle */
1069 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1070 usb_pipeout(sc->last_pipe), 0);
1071
1072 ub_state_stat(sc, cmd);
1073
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001074 } else if (cmd->state == UB_CMDST_CLRRS) {
1075 if (urb->status == -EPIPE) {
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001076 printk(KERN_NOTICE "%s: stall on control pipe\n",
1077 sc->name);
1078 goto Bad_End;
1079 }
1080
1081 /*
1082 * We ignore the result for the halt clear.
1083 */
1084
1085 /* reset the endpoint toggle */
1086 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1087 usb_pipeout(sc->last_pipe), 0);
1088
1089 ub_state_stat_counted(sc, cmd);
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 } else if (cmd->state == UB_CMDST_CMD) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001092 switch (urb->status) {
1093 case 0:
1094 break;
1095 case -EOVERFLOW:
1096 goto Bad_End;
1097 case -EPIPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1099 if (rc != 0) {
1100 printk(KERN_NOTICE "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001101 "unable to submit clear (%d)\n",
1102 sc->name, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 /*
1104 * This is typically ENOMEM or some other such shit.
1105 * Retrying is pointless. Just do Bad End on it...
1106 */
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001107 ub_state_done(sc, cmd, rc);
1108 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110 cmd->state = UB_CMDST_CLEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001112 case -ESHUTDOWN: /* unplug */
1113 case -EILSEQ: /* unplug timeout on uhci */
1114 ub_state_done(sc, cmd, -ENODEV);
1115 return;
1116 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 goto Bad_End;
1118 }
1119 if (urb->actual_length != US_BULK_CB_WRAP_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 goto Bad_End;
1121 }
1122
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001123 if (cmd->dir == UB_DIR_NONE || cmd->nsg < 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 ub_state_stat(sc, cmd);
1125 return;
1126 }
1127
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001128 // udelay(125); // usb-storage has this
1129 ub_data_start(sc, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 } else if (cmd->state == UB_CMDST_DATA) {
1132 if (urb->status == -EPIPE) {
1133 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1134 if (rc != 0) {
1135 printk(KERN_NOTICE "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001136 "unable to submit clear (%d)\n",
1137 sc->name, rc);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001138 ub_state_done(sc, cmd, rc);
1139 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141 cmd->state = UB_CMDST_CLR2STS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return;
1143 }
1144 if (urb->status == -EOVERFLOW) {
1145 /*
1146 * A babble? Failure, but we must transfer CSW now.
1147 */
1148 cmd->error = -EOVERFLOW; /* A cheap trick... */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001149 ub_state_stat(sc, cmd);
1150 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001152
1153 if (cmd->dir == UB_DIR_WRITE) {
1154 /*
1155 * Do not continue writes in case of a failure.
1156 * Doing so would cause sectors to be mixed up,
1157 * which is worse than sectors lost.
1158 *
1159 * We must try to read the CSW, or many devices
1160 * get confused.
1161 */
1162 len = urb->actual_length;
1163 if (urb->status != 0 ||
1164 len != cmd->sgv[cmd->current_sg].length) {
1165 cmd->act_len += len;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001166
1167 cmd->error = -EIO;
1168 ub_state_stat(sc, cmd);
1169 return;
1170 }
1171
1172 } else {
1173 /*
1174 * If an error occurs on read, we record it, and
1175 * continue to fetch data in order to avoid bubble.
1176 *
1177 * As a small shortcut, we stop if we detect that
1178 * a CSW mixed into data.
1179 */
1180 if (urb->status != 0)
1181 cmd->error = -EIO;
1182
1183 len = urb->actual_length;
1184 if (urb->status != 0 ||
1185 len != cmd->sgv[cmd->current_sg].length) {
1186 if ((len & 0x1FF) == US_BULK_CS_WRAP_LEN)
1187 goto Bad_End;
1188 }
1189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001191 cmd->act_len += urb->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001193 if (++cmd->current_sg < cmd->nsg) {
1194 ub_data_start(sc, cmd);
1195 return;
1196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 ub_state_stat(sc, cmd);
1198
1199 } else if (cmd->state == UB_CMDST_STAT) {
1200 if (urb->status == -EPIPE) {
1201 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1202 if (rc != 0) {
1203 printk(KERN_NOTICE "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001204 "unable to submit clear (%d)\n",
1205 sc->name, rc);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001206 ub_state_done(sc, cmd, rc);
1207 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001209
1210 /*
1211 * Having a stall when getting CSW is an error, so
1212 * make sure uppper levels are not oblivious to it.
1213 */
1214 cmd->error = -EIO; /* A cheap trick... */
1215
1216 cmd->state = UB_CMDST_CLRRS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return;
1218 }
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001219
1220 /* Catch everything, including -EOVERFLOW and other nasties. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (urb->status != 0)
1222 goto Bad_End;
1223
1224 if (urb->actual_length == 0) {
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001225 ub_state_stat_counted(sc, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 return;
1227 }
1228
1229 /*
1230 * Check the returned Bulk protocol status.
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001231 * The status block has to be validated first.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 */
1233
1234 bcs = &sc->work_bcs;
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001235
1236 if (sc->signature == cpu_to_le32(0)) {
1237 /*
1238 * This is the first reply, so do not perform the check.
1239 * Instead, remember the signature the device uses
1240 * for future checks. But do not allow a nul.
1241 */
1242 sc->signature = bcs->Signature;
1243 if (sc->signature == cpu_to_le32(0)) {
1244 ub_state_stat_counted(sc, cmd);
1245 return;
1246 }
1247 } else {
1248 if (bcs->Signature != sc->signature) {
1249 ub_state_stat_counted(sc, cmd);
1250 return;
1251 }
1252 }
1253
1254 if (bcs->Tag != cmd->tag) {
1255 /*
1256 * This usually happens when we disagree with the
1257 * device's microcode about something. For instance,
1258 * a few of them throw this after timeouts. They buffer
1259 * commands and reply at commands we timed out before.
1260 * Without flushing these replies we loop forever.
1261 */
1262 ub_state_stat_counted(sc, cmd);
1263 return;
1264 }
1265
Pete Zaitcev0da13c82008-04-19 14:42:49 -07001266 if (!sc->bad_resid) {
1267 len = le32_to_cpu(bcs->Residue);
1268 if (len != cmd->len - cmd->act_len) {
1269 /*
1270 * Only start ignoring if this cmd ended well.
1271 */
1272 if (cmd->len == cmd->act_len) {
1273 printk(KERN_NOTICE "%s: "
1274 "bad residual %d of %d, ignoring\n",
1275 sc->name, len, cmd->len);
1276 sc->bad_resid = 1;
1277 }
1278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 switch (bcs->Status) {
1282 case US_BULK_STAT_OK:
1283 break;
1284 case US_BULK_STAT_FAIL:
1285 ub_state_sense(sc, cmd);
1286 return;
1287 case US_BULK_STAT_PHASE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 goto Bad_End;
1289 default:
1290 printk(KERN_INFO "%s: unknown CSW status 0x%x\n",
1291 sc->name, bcs->Status);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001292 ub_state_done(sc, cmd, -EINVAL);
1293 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
1295
1296 /* Not zeroing error to preserve a babble indicator */
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001297 if (cmd->error != 0) {
1298 ub_state_sense(sc, cmd);
1299 return;
1300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 cmd->state = UB_CMDST_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 ub_cmdq_pop(sc);
1303 (*cmd->done)(sc, cmd);
1304
1305 } else if (cmd->state == UB_CMDST_SENSE) {
1306 ub_state_done(sc, cmd, -EIO);
1307
1308 } else {
Pete Zaitcev9029b172008-04-19 14:45:24 -07001309 printk(KERN_WARNING "%s: wrong command state %d\n",
Pete Zaitcevf4800072005-05-01 16:05:40 -07001310 sc->name, cmd->state);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001311 ub_state_done(sc, cmd, -EINVAL);
1312 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
1314 return;
1315
1316Bad_End: /* Little Excel is dead */
1317 ub_state_done(sc, cmd, -EIO);
1318}
1319
1320/*
1321 * Factorization helper for the command state machine:
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001322 * Initiate a data segment transfer.
1323 */
1324static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1325{
1326 struct scatterlist *sg = &cmd->sgv[cmd->current_sg];
1327 int pipe;
1328 int rc;
1329
1330 UB_INIT_COMPLETION(sc->work_done);
1331
1332 if (cmd->dir == UB_DIR_READ)
1333 pipe = sc->recv_bulk_pipe;
1334 else
1335 pipe = sc->send_bulk_pipe;
1336 sc->last_pipe = pipe;
Jens Axboe45711f12007-10-22 21:19:53 +02001337 usb_fill_bulk_urb(&sc->work_urb, sc->dev, pipe, sg_virt(sg),
1338 sg->length, ub_urb_complete, sc);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001339
1340 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1341 /* XXX Clear stalls */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001342 ub_complete(&sc->work_done);
1343 ub_state_done(sc, cmd, rc);
1344 return;
1345 }
1346
Pete Zaitcev2c51ae72008-04-19 14:32:18 -07001347 if (cmd->timeo)
1348 sc->work_timer.expires = jiffies + cmd->timeo;
1349 else
1350 sc->work_timer.expires = jiffies + UB_DATA_TIMEOUT;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001351 add_timer(&sc->work_timer);
1352
1353 cmd->state = UB_CMDST_DATA;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001354}
1355
1356/*
1357 * Factorization helper for the command state machine:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 * Finish the command.
1359 */
1360static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc)
1361{
1362
1363 cmd->error = rc;
1364 cmd->state = UB_CMDST_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 ub_cmdq_pop(sc);
1366 (*cmd->done)(sc, cmd);
1367}
1368
1369/*
1370 * Factorization helper for the command state machine:
1371 * Submit a CSW read.
1372 */
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001373static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 int rc;
1376
1377 UB_INIT_COMPLETION(sc->work_done);
1378
1379 sc->last_pipe = sc->recv_bulk_pipe;
1380 usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->recv_bulk_pipe,
1381 &sc->work_bcs, US_BULK_CS_WRAP_LEN, ub_urb_complete, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1384 /* XXX Clear stalls */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 ub_complete(&sc->work_done);
1386 ub_state_done(sc, cmd, rc);
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001387 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389
Pete Zaitcev2c51ae72008-04-19 14:32:18 -07001390 if (cmd->timeo)
1391 sc->work_timer.expires = jiffies + cmd->timeo;
1392 else
1393 sc->work_timer.expires = jiffies + UB_STAT_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 add_timer(&sc->work_timer);
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396}
1397
1398/*
1399 * Factorization helper for the command state machine:
1400 * Submit a CSW read and go to STAT state.
1401 */
1402static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1403{
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001404
1405 if (__ub_state_stat(sc, cmd) != 0)
1406 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 cmd->stat_count = 0;
1409 cmd->state = UB_CMDST_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
1412/*
1413 * Factorization helper for the command state machine:
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001414 * Submit a CSW read and go to STAT state with counter (along [C] path).
1415 */
1416static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1417{
1418
1419 if (++cmd->stat_count >= 4) {
1420 ub_state_sense(sc, cmd);
1421 return;
1422 }
1423
1424 if (__ub_state_stat(sc, cmd) != 0)
1425 return;
1426
1427 cmd->state = UB_CMDST_STAT;
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001428}
1429
1430/*
1431 * Factorization helper for the command state machine:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 * Submit a REQUEST SENSE and go to SENSE state.
1433 */
1434static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1435{
1436 struct ub_scsi_cmd *scmd;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001437 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 int rc;
1439
1440 if (cmd->cdb[0] == REQUEST_SENSE) {
1441 rc = -EPIPE;
1442 goto error;
1443 }
1444
1445 scmd = &sc->top_rqs_cmd;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001446 memset(scmd, 0, sizeof(struct ub_scsi_cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 scmd->cdb[0] = REQUEST_SENSE;
1448 scmd->cdb[4] = UB_SENSE_SIZE;
1449 scmd->cdb_len = 6;
1450 scmd->dir = UB_DIR_READ;
1451 scmd->state = UB_CMDST_INIT;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001452 scmd->nsg = 1;
1453 sg = &scmd->sgv[0];
FUJITA Tomonori4f33a9d2007-10-25 09:17:03 +02001454 sg_init_table(sg, UB_MAX_REQ_SG);
Jens Axboe642f1492007-10-24 11:20:47 +02001455 sg_set_page(sg, virt_to_page(sc->top_sense), UB_SENSE_SIZE,
1456 (unsigned long)sc->top_sense & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 scmd->len = UB_SENSE_SIZE;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001458 scmd->lun = cmd->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 scmd->done = ub_top_sense_done;
1460 scmd->back = cmd;
1461
1462 scmd->tag = sc->tagcnt++;
1463
1464 cmd->state = UB_CMDST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 ub_cmdq_insert(sc, scmd);
1467 return;
1468
1469error:
1470 ub_state_done(sc, cmd, rc);
1471}
1472
1473/*
1474 * A helper for the command's state machine:
1475 * Submit a stall clear.
1476 */
1477static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
1478 int stalled_pipe)
1479{
1480 int endp;
1481 struct usb_ctrlrequest *cr;
1482 int rc;
1483
1484 endp = usb_pipeendpoint(stalled_pipe);
1485 if (usb_pipein (stalled_pipe))
1486 endp |= USB_DIR_IN;
1487
1488 cr = &sc->work_cr;
1489 cr->bRequestType = USB_RECIP_ENDPOINT;
1490 cr->bRequest = USB_REQ_CLEAR_FEATURE;
1491 cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
1492 cr->wIndex = cpu_to_le16(endp);
1493 cr->wLength = cpu_to_le16(0);
1494
1495 UB_INIT_COMPLETION(sc->work_done);
1496
1497 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
1498 (unsigned char*) cr, NULL, 0, ub_urb_complete, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1501 ub_complete(&sc->work_done);
1502 return rc;
1503 }
1504
1505 sc->work_timer.expires = jiffies + UB_CTRL_TIMEOUT;
1506 add_timer(&sc->work_timer);
1507 return 0;
1508}
1509
1510/*
1511 */
1512static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd)
1513{
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001514 unsigned char *sense = sc->top_sense;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 struct ub_scsi_cmd *cmd;
1516
1517 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 * Find the command which triggered the unit attention or a check,
1519 * save the sense into it, and advance its state machine.
1520 */
1521 if ((cmd = ub_cmdq_peek(sc)) == NULL) {
1522 printk(KERN_WARNING "%s: sense done while idle\n", sc->name);
1523 return;
1524 }
1525 if (cmd != scmd->back) {
1526 printk(KERN_WARNING "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001527 "sense done for wrong command 0x%x\n",
1528 sc->name, cmd->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return;
1530 }
1531 if (cmd->state != UB_CMDST_SENSE) {
Pete Zaitcev9029b172008-04-19 14:45:24 -07001532 printk(KERN_WARNING "%s: sense done with bad cmd state %d\n",
Pete Zaitcevf4800072005-05-01 16:05:40 -07001533 sc->name, cmd->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return;
1535 }
1536
Pete Zaitcev952ba222006-03-02 16:42:59 -08001537 /*
1538 * Ignoring scmd->act_len, because the buffer was pre-zeroed.
1539 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 cmd->key = sense[2] & 0x0F;
1541 cmd->asc = sense[12];
1542 cmd->ascq = sense[13];
1543
1544 ub_scsi_urb_compl(sc, cmd);
1545}
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547/*
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001548 * Reset management
1549 */
1550
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001551static void ub_reset_enter(struct ub_dev *sc, int try)
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001552{
1553
1554 if (sc->reset) {
1555 /* This happens often on multi-LUN devices. */
1556 return;
1557 }
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001558 sc->reset = try + 1;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001559
1560#if 0 /* Not needed because the disconnect waits for us. */
1561 unsigned long flags;
1562 spin_lock_irqsave(&ub_lock, flags);
1563 sc->openc++;
1564 spin_unlock_irqrestore(&ub_lock, flags);
1565#endif
1566
1567#if 0 /* We let them stop themselves. */
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001568 struct ub_lun *lun;
Matthias Kaehlckea69228d2007-07-09 12:03:07 -07001569 list_for_each_entry(lun, &sc->luns, link) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001570 blk_stop_queue(lun->disk->queue);
1571 }
1572#endif
1573
1574 schedule_work(&sc->reset_work);
1575}
1576
David Howellsc4028952006-11-22 14:57:56 +00001577static void ub_reset_task(struct work_struct *work)
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001578{
David Howellsc4028952006-11-22 14:57:56 +00001579 struct ub_dev *sc = container_of(work, struct ub_dev, reset_work);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001580 unsigned long flags;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001581 struct ub_lun *lun;
1582 int lkr, rc;
1583
1584 if (!sc->reset) {
1585 printk(KERN_WARNING "%s: Running reset unrequested\n",
1586 sc->name);
1587 return;
1588 }
1589
1590 if (atomic_read(&sc->poison)) {
Pete Zaitcevb5600332006-05-25 20:08:50 -07001591 ;
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001592 } else if ((sc->reset & 1) == 0) {
1593 ub_sync_reset(sc);
1594 msleep(700); /* usb-storage sleeps 6s (!) */
1595 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
1596 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001597 } else if (sc->dev->actconfig->desc.bNumInterfaces != 1) {
Pete Zaitcevb5600332006-05-25 20:08:50 -07001598 ;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001599 } else {
1600 if ((lkr = usb_lock_device_for_reset(sc->dev, sc->intf)) < 0) {
1601 printk(KERN_NOTICE
1602 "%s: usb_lock_device_for_reset failed (%d)\n",
1603 sc->name, lkr);
1604 } else {
1605 rc = usb_reset_device(sc->dev);
1606 if (rc < 0) {
1607 printk(KERN_NOTICE "%s: "
1608 "usb_lock_device_for_reset failed (%d)\n",
1609 sc->name, rc);
1610 }
1611
1612 if (lkr)
1613 usb_unlock_device(sc->dev);
1614 }
1615 }
1616
1617 /*
1618 * In theory, no commands can be running while reset is active,
1619 * so nobody can ask for another reset, and so we do not need any
1620 * queues of resets or anything. We do need a spinlock though,
1621 * to interact with block layer.
1622 */
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001623 spin_lock_irqsave(sc->lock, flags);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001624 sc->reset = 0;
1625 tasklet_schedule(&sc->tasklet);
Matthias Kaehlckea69228d2007-07-09 12:03:07 -07001626 list_for_each_entry(lun, &sc->luns, link) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001627 blk_start_queue(lun->disk->queue);
1628 }
1629 wake_up(&sc->reset_wait);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001630 spin_unlock_irqrestore(sc->lock, flags);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001631}
1632
1633/*
Pete Zaitcevd73b7aff2008-11-10 21:11:11 -07001634 * XXX Reset brackets are too much hassle to implement, so just stub them
1635 * in order to prevent forced unbinding (which deadlocks solid when our
1636 * ->disconnect method waits for the reset to complete and this kills keventd).
1637 *
1638 * XXX Tell Alan to move usb_unlock_device inside of usb_reset_device,
1639 * or else the post_reset is invoked, and restats I/O on a locked device.
1640 */
1641static int ub_pre_reset(struct usb_interface *iface) {
1642 return 0;
1643}
1644
1645static int ub_post_reset(struct usb_interface *iface) {
1646 return 0;
1647}
1648
1649/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 * This is called from a process context.
1651 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001652static void ub_revalidate(struct ub_dev *sc, struct ub_lun *lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
1654
Pete Zaitcevf4800072005-05-01 16:05:40 -07001655 lun->readonly = 0; /* XXX Query this from the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Pete Zaitcevf4800072005-05-01 16:05:40 -07001657 lun->capacity.nsec = 0;
1658 lun->capacity.bsize = 512;
1659 lun->capacity.bshift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Pete Zaitcevf4800072005-05-01 16:05:40 -07001661 if (ub_sync_tur(sc, lun) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 return; /* Not ready */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001663 lun->changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Pete Zaitcevf4800072005-05-01 16:05:40 -07001665 if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 /*
1667 * The retry here means something is wrong, either with the
1668 * device, with the transport, or with our code.
1669 * We keep this because sd.c has retries for capacity.
1670 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001671 if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
1672 lun->capacity.nsec = 0;
1673 lun->capacity.bsize = 512;
1674 lun->capacity.bshift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676 }
1677}
1678
1679/*
1680 * The open funcion.
1681 * This is mostly needed to keep refcounting, but also to support
1682 * media checks on removable media drives.
1683 */
Al Viro4099a962008-03-02 10:21:43 -05001684static int ub_bd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Al Viro4099a962008-03-02 10:21:43 -05001686 struct ub_lun *lun = bdev->bd_disk->private_data;
Pete Zaitcev41fea55e2006-04-28 20:45:49 -07001687 struct ub_dev *sc = lun->udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 unsigned long flags;
1689 int rc;
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 spin_lock_irqsave(&ub_lock, flags);
1692 if (atomic_read(&sc->poison)) {
1693 spin_unlock_irqrestore(&ub_lock, flags);
1694 return -ENXIO;
1695 }
1696 sc->openc++;
1697 spin_unlock_irqrestore(&ub_lock, flags);
1698
Pete Zaitcevf4800072005-05-01 16:05:40 -07001699 if (lun->removable || lun->readonly)
Al Viro4099a962008-03-02 10:21:43 -05001700 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 /*
1703 * The sd.c considers ->media_present and ->changed not equivalent,
1704 * under some pretty murky conditions (a failure of READ CAPACITY).
1705 * We may need it one day.
1706 */
Al Viro4099a962008-03-02 10:21:43 -05001707 if (lun->removable && lun->changed && !(mode & FMODE_NDELAY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 rc = -ENOMEDIUM;
1709 goto err_open;
1710 }
1711
Al Viro4099a962008-03-02 10:21:43 -05001712 if (lun->readonly && (mode & FMODE_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 rc = -EROFS;
1714 goto err_open;
1715 }
1716
1717 return 0;
1718
1719err_open:
1720 ub_put(sc);
1721 return rc;
1722}
1723
1724/*
1725 */
Al Viro4099a962008-03-02 10:21:43 -05001726static int ub_bd_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
Pete Zaitcevf4800072005-05-01 16:05:40 -07001728 struct ub_lun *lun = disk->private_data;
1729 struct ub_dev *sc = lun->udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 ub_put(sc);
1732 return 0;
1733}
1734
1735/*
1736 * The ioctl interface.
1737 */
Al Viro4099a962008-03-02 10:21:43 -05001738static int ub_bd_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 unsigned int cmd, unsigned long arg)
1740{
Al Viro4099a962008-03-02 10:21:43 -05001741 struct gendisk *disk = bdev->bd_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 void __user *usermem = (void __user *) arg;
1743
Al Viro4099a962008-03-02 10:21:43 -05001744 return scsi_cmd_ioctl(disk->queue, disk, mode, cmd, usermem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
1747/*
Pete Zaitcev9029b172008-04-19 14:45:24 -07001748 * This is called by check_disk_change if we reported a media change.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 * The main onjective here is to discover the features of the media such as
1750 * the capacity, read-only status, etc. USB storage generally does not
1751 * need to be spun up, but if we needed it, this would be the place.
1752 *
1753 * This call can sleep.
1754 *
1755 * The return code is not used.
1756 */
1757static int ub_bd_revalidate(struct gendisk *disk)
1758{
Pete Zaitcevf4800072005-05-01 16:05:40 -07001759 struct ub_lun *lun = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Pete Zaitcevf4800072005-05-01 16:05:40 -07001761 ub_revalidate(lun->udev, lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 /* XXX Support sector size switching like in sr.c */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001764 blk_queue_hardsect_size(disk->queue, lun->capacity.bsize);
1765 set_capacity(disk, lun->capacity.nsec);
1766 // set_disk_ro(sdkp->disk, lun->readonly);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768 return 0;
1769}
1770
1771/*
1772 * The check is called by the block layer to verify if the media
1773 * is still available. It is supposed to be harmless, lightweight and
1774 * non-intrusive in case the media was not changed.
1775 *
1776 * This call can sleep.
1777 *
1778 * The return code is bool!
1779 */
1780static int ub_bd_media_changed(struct gendisk *disk)
1781{
Pete Zaitcevf4800072005-05-01 16:05:40 -07001782 struct ub_lun *lun = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Pete Zaitcevf4800072005-05-01 16:05:40 -07001784 if (!lun->removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 return 0;
1786
1787 /*
1788 * We clean checks always after every command, so this is not
1789 * as dangerous as it looks. If the TEST_UNIT_READY fails here,
1790 * the device is actually not ready with operator or software
1791 * intervention required. One dangerous item might be a drive which
1792 * spins itself down, and come the time to write dirty pages, this
1793 * will fail, then block layer discards the data. Since we never
1794 * spin drives up, such devices simply cannot be used with ub anyway.
1795 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001796 if (ub_sync_tur(lun->udev, lun) != 0) {
1797 lun->changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return 1;
1799 }
1800
Pete Zaitcevf4800072005-05-01 16:05:40 -07001801 return lun->changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
1803
1804static struct block_device_operations ub_bd_fops = {
1805 .owner = THIS_MODULE,
Al Viro4099a962008-03-02 10:21:43 -05001806 .open = ub_bd_open,
1807 .release = ub_bd_release,
1808 .locked_ioctl = ub_bd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 .media_changed = ub_bd_media_changed,
1810 .revalidate_disk = ub_bd_revalidate,
1811};
1812
1813/*
1814 * Common ->done routine for commands executed synchronously.
1815 */
1816static void ub_probe_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1817{
1818 struct completion *cop = cmd->back;
1819 complete(cop);
1820}
1821
1822/*
1823 * Test if the device has a check condition on it, synchronously.
1824 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001825static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
1827 struct ub_scsi_cmd *cmd;
1828 enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) };
1829 unsigned long flags;
1830 struct completion compl;
1831 int rc;
1832
1833 init_completion(&compl);
1834
1835 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08001836 if ((cmd = kzalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 cmd->cdb[0] = TEST_UNIT_READY;
1840 cmd->cdb_len = 6;
1841 cmd->dir = UB_DIR_NONE;
1842 cmd->state = UB_CMDST_INIT;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001843 cmd->lun = lun; /* This may be NULL, but that's ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 cmd->done = ub_probe_done;
1845 cmd->back = &compl;
1846
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001847 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 cmd->tag = sc->tagcnt++;
1849
1850 rc = ub_submit_scsi(sc, cmd);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001851 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Pete Zaitcevb5600332006-05-25 20:08:50 -07001853 if (rc != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 goto err_submit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 wait_for_completion(&compl);
1857
1858 rc = cmd->error;
1859
1860 if (rc == -EIO && cmd->key != 0) /* Retries for benh's key */
1861 rc = cmd->key;
1862
1863err_submit:
1864 kfree(cmd);
1865err_alloc:
1866 return rc;
1867}
1868
1869/*
1870 * Read the SCSI capacity synchronously (for probing).
1871 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001872static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
1873 struct ub_capacity *ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
1875 struct ub_scsi_cmd *cmd;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001876 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 char *p;
1878 enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) + 8 };
1879 unsigned long flags;
1880 unsigned int bsize, shift;
1881 unsigned long nsec;
1882 struct completion compl;
1883 int rc;
1884
1885 init_completion(&compl);
1886
1887 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08001888 if ((cmd = kzalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 p = (char *)cmd + sizeof(struct ub_scsi_cmd);
1891
1892 cmd->cdb[0] = 0x25;
1893 cmd->cdb_len = 10;
1894 cmd->dir = UB_DIR_READ;
1895 cmd->state = UB_CMDST_INIT;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001896 cmd->nsg = 1;
1897 sg = &cmd->sgv[0];
FUJITA Tomonori4f33a9d2007-10-25 09:17:03 +02001898 sg_init_table(sg, UB_MAX_REQ_SG);
Jens Axboe642f1492007-10-24 11:20:47 +02001899 sg_set_page(sg, virt_to_page(p), 8, (unsigned long)p & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 cmd->len = 8;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001901 cmd->lun = lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 cmd->done = ub_probe_done;
1903 cmd->back = &compl;
1904
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001905 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 cmd->tag = sc->tagcnt++;
1907
1908 rc = ub_submit_scsi(sc, cmd);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001909 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Pete Zaitcevb5600332006-05-25 20:08:50 -07001911 if (rc != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 goto err_submit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 wait_for_completion(&compl);
1915
1916 if (cmd->error != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 rc = -EIO;
1918 goto err_read;
1919 }
1920 if (cmd->act_len != 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 rc = -EIO;
1922 goto err_read;
1923 }
1924
1925 /* sd.c special-cases sector size of 0 to mean 512. Needed? Safe? */
1926 nsec = be32_to_cpu(*(__be32 *)p) + 1;
1927 bsize = be32_to_cpu(*(__be32 *)(p + 4));
1928 switch (bsize) {
1929 case 512: shift = 0; break;
1930 case 1024: shift = 1; break;
1931 case 2048: shift = 2; break;
1932 case 4096: shift = 3; break;
1933 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 rc = -EDOM;
1935 goto err_inv_bsize;
1936 }
1937
1938 ret->bsize = bsize;
1939 ret->bshift = shift;
1940 ret->nsec = nsec << shift;
1941 rc = 0;
1942
1943err_inv_bsize:
1944err_read:
1945err_submit:
1946 kfree(cmd);
1947err_alloc:
1948 return rc;
1949}
1950
1951/*
1952 */
David Howells7d12e782006-10-05 14:55:46 +01001953static void ub_probe_urb_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
1955 struct completion *cop = urb->context;
1956 complete(cop);
1957}
1958
1959static void ub_probe_timeout(unsigned long arg)
1960{
1961 struct completion *cop = (struct completion *) arg;
1962 complete(cop);
1963}
1964
1965/*
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001966 * Reset with a Bulk reset.
1967 */
1968static int ub_sync_reset(struct ub_dev *sc)
1969{
1970 int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
1971 struct usb_ctrlrequest *cr;
1972 struct completion compl;
1973 struct timer_list timer;
1974 int rc;
1975
1976 init_completion(&compl);
1977
1978 cr = &sc->work_cr;
1979 cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1980 cr->bRequest = US_BULK_RESET_REQUEST;
1981 cr->wValue = cpu_to_le16(0);
1982 cr->wIndex = cpu_to_le16(ifnum);
1983 cr->wLength = cpu_to_le16(0);
1984
1985 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
1986 (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001987
1988 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
1989 printk(KERN_WARNING
1990 "%s: Unable to submit a bulk reset (%d)\n", sc->name, rc);
1991 return rc;
1992 }
1993
1994 init_timer(&timer);
1995 timer.function = ub_probe_timeout;
1996 timer.data = (unsigned long) &compl;
1997 timer.expires = jiffies + UB_CTRL_TIMEOUT;
1998 add_timer(&timer);
1999
2000 wait_for_completion(&compl);
2001
2002 del_timer_sync(&timer);
2003 usb_kill_urb(&sc->work_urb);
2004
2005 return sc->work_urb.status;
2006}
2007
2008/*
Pete Zaitcevf4800072005-05-01 16:05:40 -07002009 * Get number of LUNs by the way of Bulk GetMaxLUN command.
2010 */
2011static int ub_sync_getmaxlun(struct ub_dev *sc)
2012{
2013 int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
2014 unsigned char *p;
2015 enum { ALLOC_SIZE = 1 };
2016 struct usb_ctrlrequest *cr;
2017 struct completion compl;
2018 struct timer_list timer;
2019 int nluns;
2020 int rc;
2021
2022 init_completion(&compl);
2023
2024 rc = -ENOMEM;
2025 if ((p = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
2026 goto err_alloc;
2027 *p = 55;
2028
2029 cr = &sc->work_cr;
2030 cr->bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
2031 cr->bRequest = US_BULK_GET_MAX_LUN;
2032 cr->wValue = cpu_to_le16(0);
2033 cr->wIndex = cpu_to_le16(ifnum);
2034 cr->wLength = cpu_to_le16(1);
2035
2036 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->recv_ctrl_pipe,
2037 (unsigned char*) cr, p, 1, ub_probe_urb_complete, &compl);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002038
Pete Zaitcevb5600332006-05-25 20:08:50 -07002039 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0)
Pete Zaitcevf4800072005-05-01 16:05:40 -07002040 goto err_submit;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002041
2042 init_timer(&timer);
2043 timer.function = ub_probe_timeout;
2044 timer.data = (unsigned long) &compl;
2045 timer.expires = jiffies + UB_CTRL_TIMEOUT;
2046 add_timer(&timer);
2047
2048 wait_for_completion(&compl);
2049
2050 del_timer_sync(&timer);
2051 usb_kill_urb(&sc->work_urb);
2052
Pete Zaitcevb5600332006-05-25 20:08:50 -07002053 if ((rc = sc->work_urb.status) < 0)
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002054 goto err_io;
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002055
Pete Zaitcevf4800072005-05-01 16:05:40 -07002056 if (sc->work_urb.actual_length != 1) {
Pete Zaitcevf4800072005-05-01 16:05:40 -07002057 nluns = 0;
2058 } else {
2059 if ((nluns = *p) == 55) {
2060 nluns = 0;
2061 } else {
2062 /* GetMaxLUN returns the maximum LUN number */
2063 nluns += 1;
2064 if (nluns > UB_MAX_LUNS)
2065 nluns = UB_MAX_LUNS;
2066 }
Pete Zaitcevf4800072005-05-01 16:05:40 -07002067 }
2068
2069 kfree(p);
2070 return nluns;
2071
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002072err_io:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002073err_submit:
2074 kfree(p);
2075err_alloc:
2076 return rc;
2077}
2078
2079/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 * Clear initial stalls.
2081 */
2082static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe)
2083{
2084 int endp;
2085 struct usb_ctrlrequest *cr;
2086 struct completion compl;
2087 struct timer_list timer;
2088 int rc;
2089
2090 init_completion(&compl);
2091
2092 endp = usb_pipeendpoint(stalled_pipe);
2093 if (usb_pipein (stalled_pipe))
2094 endp |= USB_DIR_IN;
2095
2096 cr = &sc->work_cr;
2097 cr->bRequestType = USB_RECIP_ENDPOINT;
2098 cr->bRequest = USB_REQ_CLEAR_FEATURE;
2099 cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
2100 cr->wIndex = cpu_to_le16(endp);
2101 cr->wLength = cpu_to_le16(0);
2102
2103 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
2104 (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
2107 printk(KERN_WARNING
2108 "%s: Unable to submit a probe clear (%d)\n", sc->name, rc);
2109 return rc;
2110 }
2111
2112 init_timer(&timer);
2113 timer.function = ub_probe_timeout;
2114 timer.data = (unsigned long) &compl;
2115 timer.expires = jiffies + UB_CTRL_TIMEOUT;
2116 add_timer(&timer);
2117
2118 wait_for_completion(&compl);
2119
2120 del_timer_sync(&timer);
2121 usb_kill_urb(&sc->work_urb);
2122
2123 /* reset the endpoint toggle */
2124 usb_settoggle(sc->dev, endp, usb_pipeout(sc->last_pipe), 0);
2125
2126 return 0;
2127}
2128
2129/*
2130 * Get the pipe settings.
2131 */
2132static int ub_get_pipes(struct ub_dev *sc, struct usb_device *dev,
2133 struct usb_interface *intf)
2134{
2135 struct usb_host_interface *altsetting = intf->cur_altsetting;
2136 struct usb_endpoint_descriptor *ep_in = NULL;
2137 struct usb_endpoint_descriptor *ep_out = NULL;
2138 struct usb_endpoint_descriptor *ep;
2139 int i;
2140
2141 /*
2142 * Find the endpoints we need.
2143 * We are expecting a minimum of 2 endpoints - in and out (bulk).
2144 * We will ignore any others.
2145 */
2146 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
2147 ep = &altsetting->endpoint[i].desc;
2148
2149 /* Is it a BULK endpoint? */
2150 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
2151 == USB_ENDPOINT_XFER_BULK) {
2152 /* BULK in or out? */
Pete Zaitcev643616e2007-03-08 19:56:23 -08002153 if (ep->bEndpointAddress & USB_DIR_IN) {
2154 if (ep_in == NULL)
2155 ep_in = ep;
2156 } else {
2157 if (ep_out == NULL)
2158 ep_out = ep;
2159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 }
2161 }
2162
2163 if (ep_in == NULL || ep_out == NULL) {
Pete Zaitcev9029b172008-04-19 14:45:24 -07002164 printk(KERN_NOTICE "%s: failed endpoint check\n", sc->name);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002165 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 }
2167
2168 /* Calculate and store the pipe values */
2169 sc->send_ctrl_pipe = usb_sndctrlpipe(dev, 0);
2170 sc->recv_ctrl_pipe = usb_rcvctrlpipe(dev, 0);
2171 sc->send_bulk_pipe = usb_sndbulkpipe(dev,
2172 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2173 sc->recv_bulk_pipe = usb_rcvbulkpipe(dev,
2174 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2175
2176 return 0;
2177}
2178
2179/*
2180 * Probing is done in the process context, which allows us to cheat
2181 * and not to build a state machine for the discovery.
2182 */
2183static int ub_probe(struct usb_interface *intf,
2184 const struct usb_device_id *dev_id)
2185{
2186 struct ub_dev *sc;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002187 int nluns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 int rc;
2189 int i;
2190
Pete Zaitceva00828e2005-10-22 20:15:09 -07002191 if (usb_usual_check_type(dev_id, USB_US_TYPE_UB))
2192 return -ENXIO;
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08002195 if ((sc = kzalloc(sizeof(struct ub_dev), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 goto err_core;
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002197 sc->lock = ub_next_lock();
Pete Zaitcevf4800072005-05-01 16:05:40 -07002198 INIT_LIST_HEAD(&sc->luns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 usb_init_urb(&sc->work_urb);
2200 tasklet_init(&sc->tasklet, ub_scsi_action, (unsigned long)sc);
2201 atomic_set(&sc->poison, 0);
David Howellsc4028952006-11-22 14:57:56 +00002202 INIT_WORK(&sc->reset_work, ub_reset_task);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002203 init_waitqueue_head(&sc->reset_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 init_timer(&sc->work_timer);
2206 sc->work_timer.data = (unsigned long) sc;
2207 sc->work_timer.function = ub_urb_timeout;
2208
2209 ub_init_completion(&sc->work_done);
2210 sc->work_done.done = 1; /* A little yuk, but oh well... */
2211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 sc->dev = interface_to_usbdev(intf);
2213 sc->intf = intf;
2214 // sc->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 usb_set_intfdata(intf, sc);
2216 usb_get_dev(sc->dev);
Pete Zaitcev77ef6c42006-05-03 00:16:00 -07002217 /*
2218 * Since we give the interface struct to the block level through
2219 * disk->driverfs_dev, we have to pin it. Otherwise, block_uevent
2220 * oopses on close after a disconnect (kernels 2.6.16 and up).
2221 */
2222 usb_get_intf(sc->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Pete Zaitcevf4800072005-05-01 16:05:40 -07002224 snprintf(sc->name, 12, DRV_NAME "(%d.%d)",
2225 sc->dev->bus->busnum, sc->dev->devnum);
2226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 /* XXX Verify that we can handle the device (from descriptors) */
2228
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002229 if (ub_get_pipes(sc, sc->dev, intf) != 0)
2230 goto err_dev_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 /*
2233 * At this point, all USB initialization is done, do upper layer.
2234 * We really hate halfway initialized structures, so from the
2235 * invariants perspective, this ub_dev is fully constructed at
2236 * this point.
2237 */
2238
2239 /*
2240 * This is needed to clear toggles. It is a problem only if we do
2241 * `rmmod ub && modprobe ub` without disconnects, but we like that.
2242 */
Pete Zaitcevc6c88832005-09-22 00:49:45 -07002243#if 0 /* iPod Mini fails if we do this (big white iPod works) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
2245 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
Pete Zaitcevc6c88832005-09-22 00:49:45 -07002246#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
2248 /*
2249 * The way this is used by the startup code is a little specific.
2250 * A SCSI check causes a USB stall. Our common case code sees it
2251 * and clears the check, after which the device is ready for use.
2252 * But if a check was not present, any command other than
2253 * TEST_UNIT_READY ends with a lockup (including REQUEST_SENSE).
2254 *
2255 * If we neglect to clear the SCSI check, the first real command fails
2256 * (which is the capacity readout). We clear that and retry, but why
2257 * causing spurious retries for no reason.
2258 *
2259 * Revalidation may start with its own TEST_UNIT_READY, but that one
2260 * has to succeed, so we clear checks with an additional one here.
2261 * In any case it's not our business how revaliadation is implemented.
2262 */
Pete Zaitcevb5600332006-05-25 20:08:50 -07002263 for (i = 0; i < 3; i++) { /* Retries for the schwag key from KS'04 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07002264 if ((rc = ub_sync_tur(sc, NULL)) <= 0) break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 if (rc != 0x6) break;
2266 msleep(10);
2267 }
2268
Pete Zaitcevf4800072005-05-01 16:05:40 -07002269 nluns = 1;
2270 for (i = 0; i < 3; i++) {
Pete Zaitcev11a223a2006-03-02 16:53:00 -08002271 if ((rc = ub_sync_getmaxlun(sc)) < 0)
Pete Zaitcevf4800072005-05-01 16:05:40 -07002272 break;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002273 if (rc != 0) {
2274 nluns = rc;
2275 break;
2276 }
Pete Zaitcev9f793d22005-06-06 13:54:59 -07002277 msleep(100);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Pete Zaitcevf4800072005-05-01 16:05:40 -07002280 for (i = 0; i < nluns; i++) {
2281 ub_probe_lun(sc, i);
2282 }
2283 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002285err_dev_desc:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002286 usb_set_intfdata(intf, NULL);
Pete Zaitcev77ef6c42006-05-03 00:16:00 -07002287 usb_put_intf(sc->intf);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002288 usb_put_dev(sc->dev);
2289 kfree(sc);
2290err_core:
2291 return rc;
2292}
2293
2294static int ub_probe_lun(struct ub_dev *sc, int lnum)
2295{
2296 struct ub_lun *lun;
Jens Axboe165125e2007-07-24 09:28:11 +02002297 struct request_queue *q;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002298 struct gendisk *disk;
2299 int rc;
2300
2301 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08002302 if ((lun = kzalloc(sizeof(struct ub_lun), GFP_KERNEL)) == NULL)
Pete Zaitcevf4800072005-05-01 16:05:40 -07002303 goto err_alloc;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002304 lun->num = lnum;
2305
2306 rc = -ENOSR;
2307 if ((lun->id = ub_id_get()) == -1)
2308 goto err_id;
2309
2310 lun->udev = sc;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002311
2312 snprintf(lun->name, 16, DRV_NAME "%c(%d.%d.%d)",
2313 lun->id + 'a', sc->dev->bus->busnum, sc->dev->devnum, lun->num);
2314
2315 lun->removable = 1; /* XXX Query this from the device */
2316 lun->changed = 1; /* ub_revalidate clears only */
Pete Zaitcevf4800072005-05-01 16:05:40 -07002317 ub_revalidate(sc, lun);
2318
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 rc = -ENOMEM;
Pete Zaitcev4fb729f2005-12-17 02:34:12 -08002320 if ((disk = alloc_disk(UB_PARTS_PER_LUN)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 goto err_diskalloc;
2322
Pete Zaitcevf4800072005-05-01 16:05:40 -07002323 sprintf(disk->disk_name, DRV_NAME "%c", lun->id + 'a');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 disk->major = UB_MAJOR;
Pete Zaitcev4fb729f2005-12-17 02:34:12 -08002325 disk->first_minor = lun->id * UB_PARTS_PER_LUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 disk->fops = &ub_bd_fops;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002327 disk->private_data = lun;
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002328 disk->driverfs_dev = &sc->intf->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330 rc = -ENOMEM;
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002331 if ((q = blk_init_queue(ub_request_fn, sc->lock)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 goto err_blkqinit;
2333
2334 disk->queue = q;
2335
Pete Zaitcevf4800072005-05-01 16:05:40 -07002336 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 blk_queue_max_hw_segments(q, UB_MAX_REQ_SG);
2338 blk_queue_max_phys_segments(q, UB_MAX_REQ_SG);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002339 blk_queue_segment_boundary(q, 0xffffffff); /* Dubious. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 blk_queue_max_sectors(q, UB_MAX_SECTORS);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002341 blk_queue_hardsect_size(q, lun->capacity.bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Pete Zaitcev688e9fb2006-05-25 20:04:54 -07002343 lun->disk = disk;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002344 q->queuedata = lun;
Pete Zaitcev688e9fb2006-05-25 20:04:54 -07002345 list_add(&lun->link, &sc->luns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Pete Zaitcevf4800072005-05-01 16:05:40 -07002347 set_capacity(disk, lun->capacity.nsec);
2348 if (lun->removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 disk->flags |= GENHD_FL_REMOVABLE;
2350
2351 add_disk(disk);
2352
2353 return 0;
2354
2355err_blkqinit:
2356 put_disk(disk);
2357err_diskalloc:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002358 ub_id_put(lun->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359err_id:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002360 kfree(lun);
2361err_alloc:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 return rc;
2363}
2364
2365static void ub_disconnect(struct usb_interface *intf)
2366{
2367 struct ub_dev *sc = usb_get_intfdata(intf);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002368 struct ub_lun *lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 unsigned long flags;
2370
2371 /*
2372 * Prevent ub_bd_release from pulling the rug from under us.
2373 * XXX This is starting to look like a kref.
2374 * XXX Why not to take this ref at probe time?
2375 */
2376 spin_lock_irqsave(&ub_lock, flags);
2377 sc->openc++;
2378 spin_unlock_irqrestore(&ub_lock, flags);
2379
2380 /*
Pete Zaitcev9029b172008-04-19 14:45:24 -07002381 * Fence stall clearings, operations triggered by unlinkings and so on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 * We do not attempt to unlink any URBs, because we do not trust the
2383 * unlink paths in HC drivers. Also, we get -84 upon disconnect anyway.
2384 */
2385 atomic_set(&sc->poison, 1);
2386
2387 /*
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002388 * Wait for reset to end, if any.
2389 */
2390 wait_event(sc->reset_wait, !sc->reset);
2391
2392 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 * Blow away queued commands.
2394 *
2395 * Actually, this never works, because before we get here
2396 * the HCD terminates outstanding URB(s). It causes our
2397 * SCSI command queue to advance, commands fail to submit,
2398 * and the whole queue drains. So, we just use this code to
2399 * print warnings.
2400 */
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002401 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 {
2403 struct ub_scsi_cmd *cmd;
2404 int cnt = 0;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002405 while ((cmd = ub_cmdq_peek(sc)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 cmd->error = -ENOTCONN;
2407 cmd->state = UB_CMDST_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 ub_cmdq_pop(sc);
2409 (*cmd->done)(sc, cmd);
2410 cnt++;
2411 }
2412 if (cnt != 0) {
2413 printk(KERN_WARNING "%s: "
2414 "%d was queued after shutdown\n", sc->name, cnt);
2415 }
2416 }
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002417 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
2419 /*
2420 * Unregister the upper layer.
2421 */
Matthias Kaehlckea69228d2007-07-09 12:03:07 -07002422 list_for_each_entry(lun, &sc->luns, link) {
Pete Zaitcev688e9fb2006-05-25 20:04:54 -07002423 del_gendisk(lun->disk);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002424 /*
2425 * I wish I could do:
Nick Piggin75ad23b2008-04-29 14:48:33 +02002426 * queue_flag_set(QUEUE_FLAG_DEAD, q);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002427 * As it is, we rely on our internal poisoning and let
2428 * the upper levels to spin furiously failing all the I/O.
2429 */
2430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
2432 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 * Testing for -EINPROGRESS is always a bug, so we are bending
2434 * the rules a little.
2435 */
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002436 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 if (sc->work_urb.status == -EINPROGRESS) { /* janitors: ignore */
2438 printk(KERN_WARNING "%s: "
2439 "URB is active after disconnect\n", sc->name);
2440 }
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002441 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
2443 /*
Pete Zaitcev9029b172008-04-19 14:45:24 -07002444 * There is virtually no chance that other CPU runs a timeout so long
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 * after ub_urb_complete should have called del_timer, but only if HCD
2446 * didn't forget to deliver a callback on unlink.
2447 */
2448 del_timer_sync(&sc->work_timer);
2449
2450 /*
2451 * At this point there must be no commands coming from anyone
2452 * and no URBs left in transit.
2453 */
2454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 ub_put(sc);
2456}
2457
2458static struct usb_driver ub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 .name = "ub",
2460 .probe = ub_probe,
2461 .disconnect = ub_disconnect,
2462 .id_table = ub_usb_ids,
Pete Zaitcevd73b7aff2008-11-10 21:11:11 -07002463 .pre_reset = ub_pre_reset,
2464 .post_reset = ub_post_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465};
2466
2467static int __init ub_init(void)
2468{
2469 int rc;
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002470 int i;
2471
2472 for (i = 0; i < UB_QLOCK_NUM; i++)
2473 spin_lock_init(&ub_qlockv[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 if ((rc = register_blkdev(UB_MAJOR, DRV_NAME)) != 0)
2476 goto err_regblkdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
2478 if ((rc = usb_register(&ub_driver)) != 0)
2479 goto err_register;
2480
Pete Zaitceva00828e2005-10-22 20:15:09 -07002481 usb_usual_set_present(USB_US_TYPE_UB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 return 0;
2483
2484err_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 unregister_blkdev(UB_MAJOR, DRV_NAME);
2486err_regblkdev:
2487 return rc;
2488}
2489
2490static void __exit ub_exit(void)
2491{
2492 usb_deregister(&ub_driver);
2493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 unregister_blkdev(UB_MAJOR, DRV_NAME);
Pete Zaitceva00828e2005-10-22 20:15:09 -07002495 usb_usual_clear_present(USB_US_TYPE_UB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496}
2497
2498module_init(ub_init);
2499module_exit(ub_exit);
2500
2501MODULE_LICENSE("GPL");