blob: e2c3ebd8db220e6911b1c7543bb280f15d80f119 [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;
352
Pete Zaitcev64bd8452005-09-22 00:48:29 -0700353 int sg_stat[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354};
355
356/*
357 */
358static void ub_cleanup(struct ub_dev *sc);
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700359static int ub_request_fn_1(struct ub_lun *lun, struct request *rq);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800360static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
361 struct ub_scsi_cmd *cmd, struct ub_request *urq);
362static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
363 struct ub_scsi_cmd *cmd, struct ub_request *urq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700365static void ub_end_rq(struct request *rq, unsigned int status,
366 unsigned int cmd_len);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800367static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
368 struct ub_request *urq, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
David Howells7d12e782006-10-05 14:55:46 +0100370static void ub_urb_complete(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371static void ub_scsi_action(unsigned long _dev);
372static void ub_scsi_dispatch(struct ub_dev *sc);
373static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700374static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc);
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700376static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700378static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
380static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
381 int stalled_pipe);
382static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd);
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -0800383static void ub_reset_enter(struct ub_dev *sc, int try);
David Howellsc4028952006-11-22 14:57:56 +0000384static void ub_reset_task(struct work_struct *work);
Pete Zaitcevf4800072005-05-01 16:05:40 -0700385static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun);
386static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
387 struct ub_capacity *ret);
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -0800388static int ub_sync_reset(struct ub_dev *sc);
389static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe);
Pete Zaitcevf4800072005-05-01 16:05:40 -0700390static int ub_probe_lun(struct ub_dev *sc, int lnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392/*
393 */
Pete Zaitceva00828e2005-10-22 20:15:09 -0700394#ifdef CONFIG_USB_LIBUSUAL
395
396#define ub_usb_ids storage_usb_ids
397#else
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399static struct usb_device_id ub_usb_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) },
401 { }
402};
403
404MODULE_DEVICE_TABLE(usb, ub_usb_ids);
Pete Zaitceva00828e2005-10-22 20:15:09 -0700405#endif /* CONFIG_USB_LIBUSUAL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407/*
408 * Find me a way to identify "next free minor" for add_disk(),
409 * and the array disappears the next day. However, the number of
410 * hosts has something to do with the naming and /proc/partitions.
411 * This has to be thought out in detail before changing.
412 * If UB_MAX_HOST was 1000, we'd use a bitmap. Or a better data structure.
413 */
414#define UB_MAX_HOSTS 26
415static char ub_hostv[UB_MAX_HOSTS];
Pete Zaitcevf4800072005-05-01 16:05:40 -0700416
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800417#define UB_QLOCK_NUM 5
418static spinlock_t ub_qlockv[UB_QLOCK_NUM];
419static int ub_qlock_next = 0;
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421static DEFINE_SPINLOCK(ub_lock); /* Locks globals and ->openc */
422
423/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 * The id allocator.
425 *
426 * This also stores the host for indexing by minor, which is somewhat dirty.
427 */
428static int ub_id_get(void)
429{
430 unsigned long flags;
431 int i;
432
433 spin_lock_irqsave(&ub_lock, flags);
434 for (i = 0; i < UB_MAX_HOSTS; i++) {
435 if (ub_hostv[i] == 0) {
436 ub_hostv[i] = 1;
437 spin_unlock_irqrestore(&ub_lock, flags);
438 return i;
439 }
440 }
441 spin_unlock_irqrestore(&ub_lock, flags);
442 return -1;
443}
444
445static void ub_id_put(int id)
446{
447 unsigned long flags;
448
449 if (id < 0 || id >= UB_MAX_HOSTS) {
450 printk(KERN_ERR DRV_NAME ": bad host ID %d\n", id);
451 return;
452 }
453
454 spin_lock_irqsave(&ub_lock, flags);
455 if (ub_hostv[id] == 0) {
456 spin_unlock_irqrestore(&ub_lock, flags);
457 printk(KERN_ERR DRV_NAME ": freeing free host ID %d\n", id);
458 return;
459 }
460 ub_hostv[id] = 0;
461 spin_unlock_irqrestore(&ub_lock, flags);
462}
463
464/*
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800465 * This is necessitated by the fact that blk_cleanup_queue does not
466 * necesserily destroy the queue. Instead, it may merely decrease q->refcnt.
467 * Since our blk_init_queue() passes a spinlock common with ub_dev,
468 * we have life time issues when ub_cleanup frees ub_dev.
469 */
470static spinlock_t *ub_next_lock(void)
471{
472 unsigned long flags;
473 spinlock_t *ret;
474
475 spin_lock_irqsave(&ub_lock, flags);
476 ret = &ub_qlockv[ub_qlock_next];
477 ub_qlock_next = (ub_qlock_next + 1) % UB_QLOCK_NUM;
478 spin_unlock_irqrestore(&ub_lock, flags);
479 return ret;
480}
481
482/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 * Downcount for deallocation. This rides on two assumptions:
484 * - once something is poisoned, its refcount cannot grow
485 * - opens cannot happen at this time (del_gendisk was done)
486 * If the above is true, we can drop the lock, which we need for
487 * blk_cleanup_queue(): the silly thing may attempt to sleep.
488 * [Actually, it never needs to sleep for us, but it calls might_sleep()]
489 */
490static void ub_put(struct ub_dev *sc)
491{
492 unsigned long flags;
493
494 spin_lock_irqsave(&ub_lock, flags);
495 --sc->openc;
496 if (sc->openc == 0 && atomic_read(&sc->poison)) {
497 spin_unlock_irqrestore(&ub_lock, flags);
498 ub_cleanup(sc);
499 } else {
500 spin_unlock_irqrestore(&ub_lock, flags);
501 }
502}
503
504/*
505 * Final cleanup and deallocation.
506 */
507static void ub_cleanup(struct ub_dev *sc)
508{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700509 struct list_head *p;
510 struct ub_lun *lun;
Jens Axboe165125e2007-07-24 09:28:11 +0200511 struct request_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Pete Zaitcevf4800072005-05-01 16:05:40 -0700513 while (!list_empty(&sc->luns)) {
514 p = sc->luns.next;
515 lun = list_entry(p, struct ub_lun, link);
516 list_del(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Pete Zaitcevf4800072005-05-01 16:05:40 -0700518 /* I don't think queue can be NULL. But... Stolen from sx8.c */
519 if ((q = lun->disk->queue) != NULL)
520 blk_cleanup_queue(q);
521 /*
522 * If we zero disk->private_data BEFORE put_disk, we have
523 * to check for NULL all over the place in open, release,
524 * check_media and revalidate, because the block level
525 * semaphore is well inside the put_disk.
526 * But we cannot zero after the call, because *disk is gone.
527 * The sd.c is blatantly racy in this area.
528 */
529 /* disk->private_data = NULL; */
530 put_disk(lun->disk);
531 lun->disk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Pete Zaitcevf4800072005-05-01 16:05:40 -0700533 ub_id_put(lun->id);
534 kfree(lun);
535 }
536
Pete Zaitcev77ef6c42006-05-03 00:16:00 -0700537 usb_set_intfdata(sc->intf, NULL);
538 usb_put_intf(sc->intf);
539 usb_put_dev(sc->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 kfree(sc);
541}
542
543/*
544 * The "command allocator".
545 */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700546static struct ub_scsi_cmd *ub_get_cmd(struct ub_lun *lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 struct ub_scsi_cmd *ret;
549
Pete Zaitcevf4800072005-05-01 16:05:40 -0700550 if (lun->cmda[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return NULL;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700552 ret = &lun->cmdv[0];
553 lun->cmda[0] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return ret;
555}
556
Pete Zaitcevf4800072005-05-01 16:05:40 -0700557static void ub_put_cmd(struct ub_lun *lun, struct ub_scsi_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700559 if (cmd != &lun->cmdv[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 printk(KERN_WARNING "%s: releasing a foreign cmd %p\n",
Pete Zaitcevf4800072005-05-01 16:05:40 -0700561 lun->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return;
563 }
Pete Zaitcevf4800072005-05-01 16:05:40 -0700564 if (!lun->cmda[0]) {
565 printk(KERN_WARNING "%s: releasing a free cmd\n", lun->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return;
567 }
Pete Zaitcevf4800072005-05-01 16:05:40 -0700568 lun->cmda[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
571/*
572 * The command queue.
573 */
574static void ub_cmdq_add(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
575{
576 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
577
578 if (t->qlen++ == 0) {
579 t->head = cmd;
580 t->tail = cmd;
581 } else {
582 t->tail->next = cmd;
583 t->tail = cmd;
584 }
585
586 if (t->qlen > t->qmax)
587 t->qmax = t->qlen;
588}
589
590static void ub_cmdq_insert(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
591{
592 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
593
594 if (t->qlen++ == 0) {
595 t->head = cmd;
596 t->tail = cmd;
597 } else {
598 cmd->next = t->head;
599 t->head = cmd;
600 }
601
602 if (t->qlen > t->qmax)
603 t->qmax = t->qlen;
604}
605
606static struct ub_scsi_cmd *ub_cmdq_pop(struct ub_dev *sc)
607{
608 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
609 struct ub_scsi_cmd *cmd;
610
611 if (t->qlen == 0)
612 return NULL;
613 if (--t->qlen == 0)
614 t->tail = NULL;
615 cmd = t->head;
616 t->head = cmd->next;
617 cmd->next = NULL;
618 return cmd;
619}
620
621#define ub_cmdq_peek(sc) ((sc)->cmd_queue.head)
622
623/*
624 * The request function is our main entry point
625 */
626
Jens Axboe165125e2007-07-24 09:28:11 +0200627static void ub_request_fn(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700629 struct ub_lun *lun = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 struct request *rq;
631
632 while ((rq = elv_next_request(q)) != NULL) {
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700633 if (ub_request_fn_1(lun, rq) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 blk_stop_queue(q);
635 break;
636 }
637 }
638}
639
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700640static int ub_request_fn_1(struct ub_lun *lun, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700642 struct ub_dev *sc = lun->udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 struct ub_scsi_cmd *cmd;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800644 struct ub_request *urq;
645 int n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700647 if (atomic_read(&sc->poison)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 blkdev_dequeue_request(rq);
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700649 ub_end_rq(rq, DID_NO_CONNECT << 16, blk_rq_bytes(rq));
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700650 return 0;
651 }
652
653 if (lun->changed && !blk_pc_request(rq)) {
654 blkdev_dequeue_request(rq);
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700655 ub_end_rq(rq, SAM_STAT_CHECK_CONDITION, blk_rq_bytes(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return 0;
657 }
658
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800659 if (lun->urq.rq != NULL)
660 return -1;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700661 if ((cmd = ub_get_cmd(lun)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return -1;
663 memset(cmd, 0, sizeof(struct ub_scsi_cmd));
664
665 blkdev_dequeue_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800667 urq = &lun->urq;
668 memset(urq, 0, sizeof(struct ub_request));
669 urq->rq = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 /*
672 * get scatterlist from block layer
673 */
Pete Zaitcev541645b2008-02-09 00:10:17 -0800674 sg_init_table(&urq->sgv[0], UB_MAX_REQ_SG);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800675 n_elem = blk_rq_map_sg(lun->disk->queue, rq, &urq->sgv[0]);
676 if (n_elem < 0) {
Pete Zaitcevb5600332006-05-25 20:08:50 -0700677 /* Impossible, because blk_rq_map_sg should not hit ENOMEM. */
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700678 printk(KERN_INFO "%s: failed request map (%d)\n",
Pete Zaitcevb5600332006-05-25 20:08:50 -0700679 lun->name, n_elem);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800680 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700682 if (n_elem > UB_MAX_REQ_SG) { /* Paranoia */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 printk(KERN_WARNING "%s: request with %d segments\n",
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800684 lun->name, n_elem);
685 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800687 urq->nsg = n_elem;
Pete Zaitcev64bd8452005-09-22 00:48:29 -0700688 sc->sg_stat[n_elem < 5 ? n_elem : 5]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800690 if (blk_pc_request(rq)) {
691 ub_cmd_build_packet(sc, lun, cmd, urq);
692 } else {
693 ub_cmd_build_block(sc, lun, cmd, urq);
694 }
695 cmd->state = UB_CMDST_INIT;
696 cmd->lun = lun;
697 cmd->done = ub_rw_cmd_done;
698 cmd->back = urq;
699
700 cmd->tag = sc->tagcnt++;
701 if (ub_submit_scsi(sc, cmd) != 0)
702 goto drop;
703
704 return 0;
705
706drop:
707 ub_put_cmd(lun, cmd);
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700708 ub_end_rq(rq, DID_ERROR << 16, blk_rq_bytes(rq));
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800709 return 0;
710}
711
712static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
713 struct ub_scsi_cmd *cmd, struct ub_request *urq)
714{
715 struct request *rq = urq->rq;
716 unsigned int block, nblks;
717
718 if (rq_data_dir(rq) == WRITE)
719 cmd->dir = UB_DIR_WRITE;
720 else
721 cmd->dir = UB_DIR_READ;
722
723 cmd->nsg = urq->nsg;
724 memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /*
727 * build the command
728 *
729 * The call to blk_queue_hardsect_size() guarantees that request
730 * is aligned, but it is given in terms of 512 byte units, always.
731 */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700732 block = rq->sector >> lun->capacity.bshift;
733 nblks = rq->nr_sectors >> lun->capacity.bshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800735 cmd->cdb[0] = (cmd->dir == UB_DIR_READ)? READ_10: WRITE_10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /* 10-byte uses 4 bytes of LBA: 2147483648KB, 2097152MB, 2048GB */
737 cmd->cdb[2] = block >> 24;
738 cmd->cdb[3] = block >> 16;
739 cmd->cdb[4] = block >> 8;
740 cmd->cdb[5] = block;
741 cmd->cdb[7] = nblks >> 8;
742 cmd->cdb[8] = nblks;
743 cmd->cdb_len = 10;
744
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700745 cmd->len = rq->nr_sectors * 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800748static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
749 struct ub_scsi_cmd *cmd, struct ub_request *urq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800751 struct request *rq = urq->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 if (rq->data_len == 0) {
754 cmd->dir = UB_DIR_NONE;
755 } else {
756 if (rq_data_dir(rq) == WRITE)
757 cmd->dir = UB_DIR_WRITE;
758 else
759 cmd->dir = UB_DIR_READ;
760 }
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700761
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800762 cmd->nsg = urq->nsg;
763 memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700764
765 memcpy(&cmd->cdb, rq->cmd, rq->cmd_len);
766 cmd->cdb_len = rq->cmd_len;
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 cmd->len = rq->data_len;
Pete Zaitcev2c51ae72008-04-19 14:32:18 -0700769
770 /*
771 * To reapply this to every URB is not as incorrect as it looks.
772 * In return, we avoid any complicated tracking calculations.
773 */
774 cmd->timeo = rq->timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
777static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
778{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700779 struct ub_lun *lun = cmd->lun;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800780 struct ub_request *urq = cmd->back;
781 struct request *rq;
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700782 unsigned int scsi_status;
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700783 unsigned int cmd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800785 rq = urq->rq;
786
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700787 if (cmd->error == 0) {
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700788 if (blk_pc_request(rq)) {
789 if (cmd->act_len >= rq->data_len)
790 rq->data_len = 0;
791 else
792 rq->data_len -= cmd->act_len;
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700793 scsi_status = 0;
794 } else {
795 if (cmd->act_len != cmd->len) {
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700796 scsi_status = SAM_STAT_CHECK_CONDITION;
797 } else {
798 scsi_status = 0;
799 }
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700800 }
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700801 } else {
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700802 if (blk_pc_request(rq)) {
803 /* UB_SENSE_SIZE is smaller than SCSI_SENSE_BUFFERSIZE */
804 memcpy(rq->sense, sc->top_sense, UB_SENSE_SIZE);
805 rq->sense_len = UB_SENSE_SIZE;
806 if (sc->top_sense[0] != 0)
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700807 scsi_status = SAM_STAT_CHECK_CONDITION;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700808 else
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700809 scsi_status = DID_ERROR << 16;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800810 } else {
Pete Zaitcev82fe26b2008-04-19 14:35:30 -0700811 if (cmd->error == -EIO &&
812 (cmd->key == 0 ||
813 cmd->key == MEDIUM_ERROR ||
814 cmd->key == UNIT_ATTENTION)) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800815 if (ub_rw_cmd_retry(sc, lun, urq, cmd) == 0)
816 return;
817 }
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700818 scsi_status = SAM_STAT_CHECK_CONDITION;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700819 }
820 }
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700821
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800822 urq->rq = NULL;
823
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700824 cmd_len = cmd->len;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700825 ub_put_cmd(lun, cmd);
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700826 ub_end_rq(rq, scsi_status, cmd_len);
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700827 blk_start_queue(lun->disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700830static void ub_end_rq(struct request *rq, unsigned int scsi_status,
831 unsigned int cmd_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Kiyoshi Ueda7d699ba2007-12-11 17:46:47 -0500833 int error;
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700834 long rqlen;
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700835
836 if (scsi_status == 0) {
Kiyoshi Ueda7d699ba2007-12-11 17:46:47 -0500837 error = 0;
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700838 } else {
Kiyoshi Ueda7d699ba2007-12-11 17:46:47 -0500839 error = -EIO;
Pete Zaitcevd1ad4ea2006-09-07 16:54:22 -0700840 rq->errors = scsi_status;
841 }
Pete Zaitcevef45cb62008-04-08 17:41:51 -0700842 rqlen = blk_rq_bytes(rq); /* Oddly enough, this is the residue. */
843 if (__blk_end_request(rq, error, cmd_len)) {
844 printk(KERN_WARNING DRV_NAME
845 ": __blk_end_request blew, %s-cmd total %u rqlen %ld\n",
846 blk_pc_request(rq)? "pc": "fs", cmd_len, rqlen);
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800850static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
851 struct ub_request *urq, struct ub_scsi_cmd *cmd)
852{
853
854 if (atomic_read(&sc->poison))
855 return -ENXIO;
856
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -0800857 ub_reset_enter(sc, urq->current_try);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800858
859 if (urq->current_try >= 3)
860 return -EIO;
861 urq->current_try++;
Pete Zaitcevb5600332006-05-25 20:08:50 -0700862
863 /* Remove this if anyone complains of flooding. */
864 printk(KERN_DEBUG "%s: dir %c len/act %d/%d "
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800865 "[sense %x %02x %02x] retry %d\n",
866 sc->name, UB_DIR_CHAR(cmd->dir), cmd->len, cmd->act_len,
867 cmd->key, cmd->asc, cmd->ascq, urq->current_try);
868
869 memset(cmd, 0, sizeof(struct ub_scsi_cmd));
870 ub_cmd_build_block(sc, lun, cmd, urq);
871
872 cmd->state = UB_CMDST_INIT;
873 cmd->lun = lun;
874 cmd->done = ub_rw_cmd_done;
875 cmd->back = urq;
876
877 cmd->tag = sc->tagcnt++;
878
879#if 0 /* Wasteful */
880 return ub_submit_scsi(sc, cmd);
881#else
882 ub_cmdq_add(sc, cmd);
883 return 0;
884#endif
885}
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887/*
888 * Submit a regular SCSI operation (not an auto-sense).
889 *
890 * The Iron Law of Good Submit Routine is:
891 * Zero return - callback is done, Nonzero return - callback is not done.
892 * No exceptions.
893 *
894 * Host is assumed locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 */
896static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
897{
898
899 if (cmd->state != UB_CMDST_INIT ||
900 (cmd->dir != UB_DIR_NONE && cmd->len == 0)) {
901 return -EINVAL;
902 }
903
904 ub_cmdq_add(sc, cmd);
905 /*
906 * We can call ub_scsi_dispatch(sc) right away here, but it's a little
907 * safer to jump to a tasklet, in case upper layers do something silly.
908 */
909 tasklet_schedule(&sc->tasklet);
910 return 0;
911}
912
913/*
914 * Submit the first URB for the queued command.
915 * This function does not deal with queueing in any way.
916 */
917static int ub_scsi_cmd_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
918{
919 struct bulk_cb_wrap *bcb;
920 int rc;
921
922 bcb = &sc->work_bcb;
923
924 /*
925 * ``If the allocation length is eighteen or greater, and a device
926 * server returns less than eithteen bytes of data, the application
927 * client should assume that the bytes not transferred would have been
928 * zeroes had the device server returned those bytes.''
929 *
930 * We zero sense for all commands so that when a packet request
931 * fails it does not return a stale sense.
932 */
933 memset(&sc->top_sense, 0, UB_SENSE_SIZE);
934
935 /* set up the command wrapper */
936 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
937 bcb->Tag = cmd->tag; /* Endianness is not important */
938 bcb->DataTransferLength = cpu_to_le32(cmd->len);
939 bcb->Flags = (cmd->dir == UB_DIR_READ) ? 0x80 : 0;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700940 bcb->Lun = (cmd->lun != NULL) ? cmd->lun->num : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 bcb->Length = cmd->cdb_len;
942
943 /* copy the command payload */
944 memcpy(bcb->CDB, cmd->cdb, UB_MAX_CDB_SIZE);
945
946 UB_INIT_COMPLETION(sc->work_done);
947
948 sc->last_pipe = sc->send_bulk_pipe;
949 usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->send_bulk_pipe,
950 bcb, US_BULK_CB_WRAP_LEN, ub_urb_complete, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
953 /* XXX Clear stalls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 ub_complete(&sc->work_done);
955 return rc;
956 }
957
958 sc->work_timer.expires = jiffies + UB_URB_TIMEOUT;
959 add_timer(&sc->work_timer);
960
961 cmd->state = UB_CMDST_CMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return 0;
963}
964
965/*
966 * Timeout handler.
967 */
968static void ub_urb_timeout(unsigned long arg)
969{
970 struct ub_dev *sc = (struct ub_dev *) arg;
971 unsigned long flags;
972
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800973 spin_lock_irqsave(sc->lock, flags);
Pete Zaitcevb31f8212006-01-05 00:14:02 -0800974 if (!ub_is_completed(&sc->work_done))
975 usb_unlink_urb(&sc->work_urb);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800976 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
979/*
980 * Completion routine for the work URB.
981 *
982 * This can be called directly from usb_submit_urb (while we have
983 * the sc->lock taken) and from an interrupt (while we do NOT have
984 * the sc->lock taken). Therefore, bounce this off to a tasklet.
985 */
David Howells7d12e782006-10-05 14:55:46 +0100986static void ub_urb_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
988 struct ub_dev *sc = urb->context;
989
990 ub_complete(&sc->work_done);
991 tasklet_schedule(&sc->tasklet);
992}
993
994static void ub_scsi_action(unsigned long _dev)
995{
996 struct ub_dev *sc = (struct ub_dev *) _dev;
997 unsigned long flags;
998
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800999 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 ub_scsi_dispatch(sc);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001001 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
1004static void ub_scsi_dispatch(struct ub_dev *sc)
1005{
1006 struct ub_scsi_cmd *cmd;
1007 int rc;
1008
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001009 while (!sc->reset && (cmd = ub_cmdq_peek(sc)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (cmd->state == UB_CMDST_DONE) {
1011 ub_cmdq_pop(sc);
1012 (*cmd->done)(sc, cmd);
1013 } else if (cmd->state == UB_CMDST_INIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if ((rc = ub_scsi_cmd_start(sc, cmd)) == 0)
1015 break;
1016 cmd->error = rc;
1017 cmd->state = UB_CMDST_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 } else {
1019 if (!ub_is_completed(&sc->work_done))
1020 break;
Pete Zaitcevb31f8212006-01-05 00:14:02 -08001021 del_timer(&sc->work_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 ub_scsi_urb_compl(sc, cmd);
1023 }
1024 }
1025}
1026
1027static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1028{
1029 struct urb *urb = &sc->work_urb;
1030 struct bulk_cs_wrap *bcs;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001031 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 int rc;
1033
1034 if (atomic_read(&sc->poison)) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001035 ub_state_done(sc, cmd, -ENODEV);
1036 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038
1039 if (cmd->state == UB_CMDST_CLEAR) {
1040 if (urb->status == -EPIPE) {
1041 /*
1042 * STALL while clearning STALL.
1043 * The control pipe clears itself - nothing to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001045 printk(KERN_NOTICE "%s: stall on control pipe\n",
1046 sc->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 goto Bad_End;
1048 }
1049
1050 /*
1051 * We ignore the result for the halt clear.
1052 */
1053
1054 /* reset the endpoint toggle */
1055 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1056 usb_pipeout(sc->last_pipe), 0);
1057
1058 ub_state_sense(sc, cmd);
1059
1060 } else if (cmd->state == UB_CMDST_CLR2STS) {
1061 if (urb->status == -EPIPE) {
Pete Zaitcevf4800072005-05-01 16:05:40 -07001062 printk(KERN_NOTICE "%s: stall on control pipe\n",
1063 sc->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 goto Bad_End;
1065 }
1066
1067 /*
1068 * We ignore the result for the halt clear.
1069 */
1070
1071 /* reset the endpoint toggle */
1072 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1073 usb_pipeout(sc->last_pipe), 0);
1074
1075 ub_state_stat(sc, cmd);
1076
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001077 } else if (cmd->state == UB_CMDST_CLRRS) {
1078 if (urb->status == -EPIPE) {
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001079 printk(KERN_NOTICE "%s: stall on control pipe\n",
1080 sc->name);
1081 goto Bad_End;
1082 }
1083
1084 /*
1085 * We ignore the result for the halt clear.
1086 */
1087
1088 /* reset the endpoint toggle */
1089 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1090 usb_pipeout(sc->last_pipe), 0);
1091
1092 ub_state_stat_counted(sc, cmd);
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 } else if (cmd->state == UB_CMDST_CMD) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001095 switch (urb->status) {
1096 case 0:
1097 break;
1098 case -EOVERFLOW:
1099 goto Bad_End;
1100 case -EPIPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1102 if (rc != 0) {
1103 printk(KERN_NOTICE "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001104 "unable to submit clear (%d)\n",
1105 sc->name, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 /*
1107 * This is typically ENOMEM or some other such shit.
1108 * Retrying is pointless. Just do Bad End on it...
1109 */
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001110 ub_state_done(sc, cmd, rc);
1111 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
1113 cmd->state = UB_CMDST_CLEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001115 case -ESHUTDOWN: /* unplug */
1116 case -EILSEQ: /* unplug timeout on uhci */
1117 ub_state_done(sc, cmd, -ENODEV);
1118 return;
1119 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 goto Bad_End;
1121 }
1122 if (urb->actual_length != US_BULK_CB_WRAP_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 goto Bad_End;
1124 }
1125
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001126 if (cmd->dir == UB_DIR_NONE || cmd->nsg < 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 ub_state_stat(sc, cmd);
1128 return;
1129 }
1130
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001131 // udelay(125); // usb-storage has this
1132 ub_data_start(sc, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 } else if (cmd->state == UB_CMDST_DATA) {
1135 if (urb->status == -EPIPE) {
1136 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1137 if (rc != 0) {
1138 printk(KERN_NOTICE "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001139 "unable to submit clear (%d)\n",
1140 sc->name, rc);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001141 ub_state_done(sc, cmd, rc);
1142 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144 cmd->state = UB_CMDST_CLR2STS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return;
1146 }
1147 if (urb->status == -EOVERFLOW) {
1148 /*
1149 * A babble? Failure, but we must transfer CSW now.
1150 */
1151 cmd->error = -EOVERFLOW; /* A cheap trick... */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001152 ub_state_stat(sc, cmd);
1153 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001155
1156 if (cmd->dir == UB_DIR_WRITE) {
1157 /*
1158 * Do not continue writes in case of a failure.
1159 * Doing so would cause sectors to be mixed up,
1160 * which is worse than sectors lost.
1161 *
1162 * We must try to read the CSW, or many devices
1163 * get confused.
1164 */
1165 len = urb->actual_length;
1166 if (urb->status != 0 ||
1167 len != cmd->sgv[cmd->current_sg].length) {
1168 cmd->act_len += len;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001169
1170 cmd->error = -EIO;
1171 ub_state_stat(sc, cmd);
1172 return;
1173 }
1174
1175 } else {
1176 /*
1177 * If an error occurs on read, we record it, and
1178 * continue to fetch data in order to avoid bubble.
1179 *
1180 * As a small shortcut, we stop if we detect that
1181 * a CSW mixed into data.
1182 */
1183 if (urb->status != 0)
1184 cmd->error = -EIO;
1185
1186 len = urb->actual_length;
1187 if (urb->status != 0 ||
1188 len != cmd->sgv[cmd->current_sg].length) {
1189 if ((len & 0x1FF) == US_BULK_CS_WRAP_LEN)
1190 goto Bad_End;
1191 }
1192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001194 cmd->act_len += urb->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001196 if (++cmd->current_sg < cmd->nsg) {
1197 ub_data_start(sc, cmd);
1198 return;
1199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 ub_state_stat(sc, cmd);
1201
1202 } else if (cmd->state == UB_CMDST_STAT) {
1203 if (urb->status == -EPIPE) {
1204 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1205 if (rc != 0) {
1206 printk(KERN_NOTICE "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001207 "unable to submit clear (%d)\n",
1208 sc->name, rc);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001209 ub_state_done(sc, cmd, rc);
1210 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001212
1213 /*
1214 * Having a stall when getting CSW is an error, so
1215 * make sure uppper levels are not oblivious to it.
1216 */
1217 cmd->error = -EIO; /* A cheap trick... */
1218
1219 cmd->state = UB_CMDST_CLRRS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return;
1221 }
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001222
1223 /* Catch everything, including -EOVERFLOW and other nasties. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (urb->status != 0)
1225 goto Bad_End;
1226
1227 if (urb->actual_length == 0) {
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001228 ub_state_stat_counted(sc, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return;
1230 }
1231
1232 /*
1233 * Check the returned Bulk protocol status.
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001234 * The status block has to be validated first.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 */
1236
1237 bcs = &sc->work_bcs;
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001238
1239 if (sc->signature == cpu_to_le32(0)) {
1240 /*
1241 * This is the first reply, so do not perform the check.
1242 * Instead, remember the signature the device uses
1243 * for future checks. But do not allow a nul.
1244 */
1245 sc->signature = bcs->Signature;
1246 if (sc->signature == cpu_to_le32(0)) {
1247 ub_state_stat_counted(sc, cmd);
1248 return;
1249 }
1250 } else {
1251 if (bcs->Signature != sc->signature) {
1252 ub_state_stat_counted(sc, cmd);
1253 return;
1254 }
1255 }
1256
1257 if (bcs->Tag != cmd->tag) {
1258 /*
1259 * This usually happens when we disagree with the
1260 * device's microcode about something. For instance,
1261 * a few of them throw this after timeouts. They buffer
1262 * commands and reply at commands we timed out before.
1263 * Without flushing these replies we loop forever.
1264 */
1265 ub_state_stat_counted(sc, cmd);
1266 return;
1267 }
1268
Pete Zaitcev0da13c82008-04-19 14:42:49 -07001269 if (!sc->bad_resid) {
1270 len = le32_to_cpu(bcs->Residue);
1271 if (len != cmd->len - cmd->act_len) {
1272 /*
1273 * Only start ignoring if this cmd ended well.
1274 */
1275 if (cmd->len == cmd->act_len) {
1276 printk(KERN_NOTICE "%s: "
1277 "bad residual %d of %d, ignoring\n",
1278 sc->name, len, cmd->len);
1279 sc->bad_resid = 1;
1280 }
1281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 switch (bcs->Status) {
1285 case US_BULK_STAT_OK:
1286 break;
1287 case US_BULK_STAT_FAIL:
1288 ub_state_sense(sc, cmd);
1289 return;
1290 case US_BULK_STAT_PHASE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 goto Bad_End;
1292 default:
1293 printk(KERN_INFO "%s: unknown CSW status 0x%x\n",
1294 sc->name, bcs->Status);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001295 ub_state_done(sc, cmd, -EINVAL);
1296 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298
1299 /* Not zeroing error to preserve a babble indicator */
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001300 if (cmd->error != 0) {
1301 ub_state_sense(sc, cmd);
1302 return;
1303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 cmd->state = UB_CMDST_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 ub_cmdq_pop(sc);
1306 (*cmd->done)(sc, cmd);
1307
1308 } else if (cmd->state == UB_CMDST_SENSE) {
1309 ub_state_done(sc, cmd, -EIO);
1310
1311 } else {
1312 printk(KERN_WARNING "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001313 "wrong command state %d\n",
1314 sc->name, cmd->state);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001315 ub_state_done(sc, cmd, -EINVAL);
1316 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318 return;
1319
1320Bad_End: /* Little Excel is dead */
1321 ub_state_done(sc, cmd, -EIO);
1322}
1323
1324/*
1325 * Factorization helper for the command state machine:
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001326 * Initiate a data segment transfer.
1327 */
1328static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1329{
1330 struct scatterlist *sg = &cmd->sgv[cmd->current_sg];
1331 int pipe;
1332 int rc;
1333
1334 UB_INIT_COMPLETION(sc->work_done);
1335
1336 if (cmd->dir == UB_DIR_READ)
1337 pipe = sc->recv_bulk_pipe;
1338 else
1339 pipe = sc->send_bulk_pipe;
1340 sc->last_pipe = pipe;
Jens Axboe45711f12007-10-22 21:19:53 +02001341 usb_fill_bulk_urb(&sc->work_urb, sc->dev, pipe, sg_virt(sg),
1342 sg->length, ub_urb_complete, sc);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001343
1344 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1345 /* XXX Clear stalls */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001346 ub_complete(&sc->work_done);
1347 ub_state_done(sc, cmd, rc);
1348 return;
1349 }
1350
Pete Zaitcev2c51ae72008-04-19 14:32:18 -07001351 if (cmd->timeo)
1352 sc->work_timer.expires = jiffies + cmd->timeo;
1353 else
1354 sc->work_timer.expires = jiffies + UB_DATA_TIMEOUT;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001355 add_timer(&sc->work_timer);
1356
1357 cmd->state = UB_CMDST_DATA;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001358}
1359
1360/*
1361 * Factorization helper for the command state machine:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 * Finish the command.
1363 */
1364static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc)
1365{
1366
1367 cmd->error = rc;
1368 cmd->state = UB_CMDST_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 ub_cmdq_pop(sc);
1370 (*cmd->done)(sc, cmd);
1371}
1372
1373/*
1374 * Factorization helper for the command state machine:
1375 * Submit a CSW read.
1376 */
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001377static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 int rc;
1380
1381 UB_INIT_COMPLETION(sc->work_done);
1382
1383 sc->last_pipe = sc->recv_bulk_pipe;
1384 usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->recv_bulk_pipe,
1385 &sc->work_bcs, US_BULK_CS_WRAP_LEN, ub_urb_complete, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1388 /* XXX Clear stalls */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 ub_complete(&sc->work_done);
1390 ub_state_done(sc, cmd, rc);
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001391 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
1393
Pete Zaitcev2c51ae72008-04-19 14:32:18 -07001394 if (cmd->timeo)
1395 sc->work_timer.expires = jiffies + cmd->timeo;
1396 else
1397 sc->work_timer.expires = jiffies + UB_STAT_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 add_timer(&sc->work_timer);
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001399 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
1402/*
1403 * Factorization helper for the command state machine:
1404 * Submit a CSW read and go to STAT state.
1405 */
1406static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1407{
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001408
1409 if (__ub_state_stat(sc, cmd) != 0)
1410 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 cmd->stat_count = 0;
1413 cmd->state = UB_CMDST_STAT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
1416/*
1417 * Factorization helper for the command state machine:
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001418 * Submit a CSW read and go to STAT state with counter (along [C] path).
1419 */
1420static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1421{
1422
1423 if (++cmd->stat_count >= 4) {
1424 ub_state_sense(sc, cmd);
1425 return;
1426 }
1427
1428 if (__ub_state_stat(sc, cmd) != 0)
1429 return;
1430
1431 cmd->state = UB_CMDST_STAT;
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001432}
1433
1434/*
1435 * Factorization helper for the command state machine:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 * Submit a REQUEST SENSE and go to SENSE state.
1437 */
1438static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1439{
1440 struct ub_scsi_cmd *scmd;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001441 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 int rc;
1443
1444 if (cmd->cdb[0] == REQUEST_SENSE) {
1445 rc = -EPIPE;
1446 goto error;
1447 }
1448
1449 scmd = &sc->top_rqs_cmd;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001450 memset(scmd, 0, sizeof(struct ub_scsi_cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 scmd->cdb[0] = REQUEST_SENSE;
1452 scmd->cdb[4] = UB_SENSE_SIZE;
1453 scmd->cdb_len = 6;
1454 scmd->dir = UB_DIR_READ;
1455 scmd->state = UB_CMDST_INIT;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001456 scmd->nsg = 1;
1457 sg = &scmd->sgv[0];
FUJITA Tomonori4f33a9d2007-10-25 09:17:03 +02001458 sg_init_table(sg, UB_MAX_REQ_SG);
Jens Axboe642f1492007-10-24 11:20:47 +02001459 sg_set_page(sg, virt_to_page(sc->top_sense), UB_SENSE_SIZE,
1460 (unsigned long)sc->top_sense & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 scmd->len = UB_SENSE_SIZE;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001462 scmd->lun = cmd->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 scmd->done = ub_top_sense_done;
1464 scmd->back = cmd;
1465
1466 scmd->tag = sc->tagcnt++;
1467
1468 cmd->state = UB_CMDST_SENSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 ub_cmdq_insert(sc, scmd);
1471 return;
1472
1473error:
1474 ub_state_done(sc, cmd, rc);
1475}
1476
1477/*
1478 * A helper for the command's state machine:
1479 * Submit a stall clear.
1480 */
1481static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
1482 int stalled_pipe)
1483{
1484 int endp;
1485 struct usb_ctrlrequest *cr;
1486 int rc;
1487
1488 endp = usb_pipeendpoint(stalled_pipe);
1489 if (usb_pipein (stalled_pipe))
1490 endp |= USB_DIR_IN;
1491
1492 cr = &sc->work_cr;
1493 cr->bRequestType = USB_RECIP_ENDPOINT;
1494 cr->bRequest = USB_REQ_CLEAR_FEATURE;
1495 cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
1496 cr->wIndex = cpu_to_le16(endp);
1497 cr->wLength = cpu_to_le16(0);
1498
1499 UB_INIT_COMPLETION(sc->work_done);
1500
1501 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
1502 (unsigned char*) cr, NULL, 0, ub_urb_complete, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1505 ub_complete(&sc->work_done);
1506 return rc;
1507 }
1508
1509 sc->work_timer.expires = jiffies + UB_CTRL_TIMEOUT;
1510 add_timer(&sc->work_timer);
1511 return 0;
1512}
1513
1514/*
1515 */
1516static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd)
1517{
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001518 unsigned char *sense = sc->top_sense;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 struct ub_scsi_cmd *cmd;
1520
1521 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 * Find the command which triggered the unit attention or a check,
1523 * save the sense into it, and advance its state machine.
1524 */
1525 if ((cmd = ub_cmdq_peek(sc)) == NULL) {
1526 printk(KERN_WARNING "%s: sense done while idle\n", sc->name);
1527 return;
1528 }
1529 if (cmd != scmd->back) {
1530 printk(KERN_WARNING "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001531 "sense done for wrong command 0x%x\n",
1532 sc->name, cmd->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return;
1534 }
1535 if (cmd->state != UB_CMDST_SENSE) {
1536 printk(KERN_WARNING "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001537 "sense done with bad cmd state %d\n",
1538 sc->name, cmd->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return;
1540 }
1541
Pete Zaitcev952ba222006-03-02 16:42:59 -08001542 /*
1543 * Ignoring scmd->act_len, because the buffer was pre-zeroed.
1544 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 cmd->key = sense[2] & 0x0F;
1546 cmd->asc = sense[12];
1547 cmd->ascq = sense[13];
1548
1549 ub_scsi_urb_compl(sc, cmd);
1550}
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552/*
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001553 * Reset management
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001554 * XXX Move usb_reset_device to khubd. Hogging kevent is not a good thing.
1555 * XXX Make usb_sync_reset asynchronous.
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001556 */
1557
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001558static void ub_reset_enter(struct ub_dev *sc, int try)
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001559{
1560
1561 if (sc->reset) {
1562 /* This happens often on multi-LUN devices. */
1563 return;
1564 }
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001565 sc->reset = try + 1;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001566
1567#if 0 /* Not needed because the disconnect waits for us. */
1568 unsigned long flags;
1569 spin_lock_irqsave(&ub_lock, flags);
1570 sc->openc++;
1571 spin_unlock_irqrestore(&ub_lock, flags);
1572#endif
1573
1574#if 0 /* We let them stop themselves. */
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001575 struct ub_lun *lun;
Matthias Kaehlckea69228d2007-07-09 12:03:07 -07001576 list_for_each_entry(lun, &sc->luns, link) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001577 blk_stop_queue(lun->disk->queue);
1578 }
1579#endif
1580
1581 schedule_work(&sc->reset_work);
1582}
1583
David Howellsc4028952006-11-22 14:57:56 +00001584static void ub_reset_task(struct work_struct *work)
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001585{
David Howellsc4028952006-11-22 14:57:56 +00001586 struct ub_dev *sc = container_of(work, struct ub_dev, reset_work);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001587 unsigned long flags;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001588 struct ub_lun *lun;
1589 int lkr, rc;
1590
1591 if (!sc->reset) {
1592 printk(KERN_WARNING "%s: Running reset unrequested\n",
1593 sc->name);
1594 return;
1595 }
1596
1597 if (atomic_read(&sc->poison)) {
Pete Zaitcevb5600332006-05-25 20:08:50 -07001598 ;
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001599 } else if ((sc->reset & 1) == 0) {
1600 ub_sync_reset(sc);
1601 msleep(700); /* usb-storage sleeps 6s (!) */
1602 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
1603 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001604 } else if (sc->dev->actconfig->desc.bNumInterfaces != 1) {
Pete Zaitcevb5600332006-05-25 20:08:50 -07001605 ;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001606 } else {
1607 if ((lkr = usb_lock_device_for_reset(sc->dev, sc->intf)) < 0) {
1608 printk(KERN_NOTICE
1609 "%s: usb_lock_device_for_reset failed (%d)\n",
1610 sc->name, lkr);
1611 } else {
1612 rc = usb_reset_device(sc->dev);
1613 if (rc < 0) {
1614 printk(KERN_NOTICE "%s: "
1615 "usb_lock_device_for_reset failed (%d)\n",
1616 sc->name, rc);
1617 }
1618
1619 if (lkr)
1620 usb_unlock_device(sc->dev);
1621 }
1622 }
1623
1624 /*
1625 * In theory, no commands can be running while reset is active,
1626 * so nobody can ask for another reset, and so we do not need any
1627 * queues of resets or anything. We do need a spinlock though,
1628 * to interact with block layer.
1629 */
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001630 spin_lock_irqsave(sc->lock, flags);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001631 sc->reset = 0;
1632 tasklet_schedule(&sc->tasklet);
Matthias Kaehlckea69228d2007-07-09 12:03:07 -07001633 list_for_each_entry(lun, &sc->luns, link) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001634 blk_start_queue(lun->disk->queue);
1635 }
1636 wake_up(&sc->reset_wait);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001637 spin_unlock_irqrestore(sc->lock, flags);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001638}
1639
1640/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 * This is called from a process context.
1642 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001643static void ub_revalidate(struct ub_dev *sc, struct ub_lun *lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
1645
Pete Zaitcevf4800072005-05-01 16:05:40 -07001646 lun->readonly = 0; /* XXX Query this from the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Pete Zaitcevf4800072005-05-01 16:05:40 -07001648 lun->capacity.nsec = 0;
1649 lun->capacity.bsize = 512;
1650 lun->capacity.bshift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Pete Zaitcevf4800072005-05-01 16:05:40 -07001652 if (ub_sync_tur(sc, lun) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 return; /* Not ready */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001654 lun->changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Pete Zaitcevf4800072005-05-01 16:05:40 -07001656 if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 /*
1658 * The retry here means something is wrong, either with the
1659 * device, with the transport, or with our code.
1660 * We keep this because sd.c has retries for capacity.
1661 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001662 if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
1663 lun->capacity.nsec = 0;
1664 lun->capacity.bsize = 512;
1665 lun->capacity.bshift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
1667 }
1668}
1669
1670/*
1671 * The open funcion.
1672 * This is mostly needed to keep refcounting, but also to support
1673 * media checks on removable media drives.
1674 */
1675static int ub_bd_open(struct inode *inode, struct file *filp)
1676{
1677 struct gendisk *disk = inode->i_bdev->bd_disk;
Pete Zaitcev41fea55e2006-04-28 20:45:49 -07001678 struct ub_lun *lun = disk->private_data;
1679 struct ub_dev *sc = lun->udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 unsigned long flags;
1681 int rc;
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 spin_lock_irqsave(&ub_lock, flags);
1684 if (atomic_read(&sc->poison)) {
1685 spin_unlock_irqrestore(&ub_lock, flags);
1686 return -ENXIO;
1687 }
1688 sc->openc++;
1689 spin_unlock_irqrestore(&ub_lock, flags);
1690
Pete Zaitcevf4800072005-05-01 16:05:40 -07001691 if (lun->removable || lun->readonly)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 check_disk_change(inode->i_bdev);
1693
1694 /*
1695 * The sd.c considers ->media_present and ->changed not equivalent,
1696 * under some pretty murky conditions (a failure of READ CAPACITY).
1697 * We may need it one day.
1698 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001699 if (lun->removable && lun->changed && !(filp->f_flags & O_NDELAY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 rc = -ENOMEDIUM;
1701 goto err_open;
1702 }
1703
Pete Zaitcevf4800072005-05-01 16:05:40 -07001704 if (lun->readonly && (filp->f_mode & FMODE_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 rc = -EROFS;
1706 goto err_open;
1707 }
1708
1709 return 0;
1710
1711err_open:
1712 ub_put(sc);
1713 return rc;
1714}
1715
1716/*
1717 */
1718static int ub_bd_release(struct inode *inode, struct file *filp)
1719{
1720 struct gendisk *disk = inode->i_bdev->bd_disk;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001721 struct ub_lun *lun = disk->private_data;
1722 struct ub_dev *sc = lun->udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 ub_put(sc);
1725 return 0;
1726}
1727
1728/*
1729 * The ioctl interface.
1730 */
1731static int ub_bd_ioctl(struct inode *inode, struct file *filp,
1732 unsigned int cmd, unsigned long arg)
1733{
1734 struct gendisk *disk = inode->i_bdev->bd_disk;
1735 void __user *usermem = (void __user *) arg;
1736
FUJITA Tomonori45e79a32007-07-09 12:39:20 +02001737 return scsi_cmd_ioctl(filp, disk->queue, disk, cmd, usermem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738}
1739
1740/*
1741 * This is called once a new disk was seen by the block layer or by ub_probe().
1742 * The main onjective here is to discover the features of the media such as
1743 * the capacity, read-only status, etc. USB storage generally does not
1744 * need to be spun up, but if we needed it, this would be the place.
1745 *
1746 * This call can sleep.
1747 *
1748 * The return code is not used.
1749 */
1750static int ub_bd_revalidate(struct gendisk *disk)
1751{
Pete Zaitcevf4800072005-05-01 16:05:40 -07001752 struct ub_lun *lun = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Pete Zaitcevf4800072005-05-01 16:05:40 -07001754 ub_revalidate(lun->udev, lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 /* XXX Support sector size switching like in sr.c */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001757 blk_queue_hardsect_size(disk->queue, lun->capacity.bsize);
1758 set_capacity(disk, lun->capacity.nsec);
1759 // set_disk_ro(sdkp->disk, lun->readonly);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 return 0;
1762}
1763
1764/*
1765 * The check is called by the block layer to verify if the media
1766 * is still available. It is supposed to be harmless, lightweight and
1767 * non-intrusive in case the media was not changed.
1768 *
1769 * This call can sleep.
1770 *
1771 * The return code is bool!
1772 */
1773static int ub_bd_media_changed(struct gendisk *disk)
1774{
Pete Zaitcevf4800072005-05-01 16:05:40 -07001775 struct ub_lun *lun = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Pete Zaitcevf4800072005-05-01 16:05:40 -07001777 if (!lun->removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return 0;
1779
1780 /*
1781 * We clean checks always after every command, so this is not
1782 * as dangerous as it looks. If the TEST_UNIT_READY fails here,
1783 * the device is actually not ready with operator or software
1784 * intervention required. One dangerous item might be a drive which
1785 * spins itself down, and come the time to write dirty pages, this
1786 * will fail, then block layer discards the data. Since we never
1787 * spin drives up, such devices simply cannot be used with ub anyway.
1788 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001789 if (ub_sync_tur(lun->udev, lun) != 0) {
1790 lun->changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 return 1;
1792 }
1793
Pete Zaitcevf4800072005-05-01 16:05:40 -07001794 return lun->changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795}
1796
1797static struct block_device_operations ub_bd_fops = {
1798 .owner = THIS_MODULE,
1799 .open = ub_bd_open,
1800 .release = ub_bd_release,
1801 .ioctl = ub_bd_ioctl,
1802 .media_changed = ub_bd_media_changed,
1803 .revalidate_disk = ub_bd_revalidate,
1804};
1805
1806/*
1807 * Common ->done routine for commands executed synchronously.
1808 */
1809static void ub_probe_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1810{
1811 struct completion *cop = cmd->back;
1812 complete(cop);
1813}
1814
1815/*
1816 * Test if the device has a check condition on it, synchronously.
1817 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001818static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
1820 struct ub_scsi_cmd *cmd;
1821 enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) };
1822 unsigned long flags;
1823 struct completion compl;
1824 int rc;
1825
1826 init_completion(&compl);
1827
1828 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08001829 if ((cmd = kzalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832 cmd->cdb[0] = TEST_UNIT_READY;
1833 cmd->cdb_len = 6;
1834 cmd->dir = UB_DIR_NONE;
1835 cmd->state = UB_CMDST_INIT;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001836 cmd->lun = lun; /* This may be NULL, but that's ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 cmd->done = ub_probe_done;
1838 cmd->back = &compl;
1839
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001840 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 cmd->tag = sc->tagcnt++;
1842
1843 rc = ub_submit_scsi(sc, cmd);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001844 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Pete Zaitcevb5600332006-05-25 20:08:50 -07001846 if (rc != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 goto err_submit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 wait_for_completion(&compl);
1850
1851 rc = cmd->error;
1852
1853 if (rc == -EIO && cmd->key != 0) /* Retries for benh's key */
1854 rc = cmd->key;
1855
1856err_submit:
1857 kfree(cmd);
1858err_alloc:
1859 return rc;
1860}
1861
1862/*
1863 * Read the SCSI capacity synchronously (for probing).
1864 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001865static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
1866 struct ub_capacity *ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
1868 struct ub_scsi_cmd *cmd;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001869 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 char *p;
1871 enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) + 8 };
1872 unsigned long flags;
1873 unsigned int bsize, shift;
1874 unsigned long nsec;
1875 struct completion compl;
1876 int rc;
1877
1878 init_completion(&compl);
1879
1880 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08001881 if ((cmd = kzalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 p = (char *)cmd + sizeof(struct ub_scsi_cmd);
1884
1885 cmd->cdb[0] = 0x25;
1886 cmd->cdb_len = 10;
1887 cmd->dir = UB_DIR_READ;
1888 cmd->state = UB_CMDST_INIT;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001889 cmd->nsg = 1;
1890 sg = &cmd->sgv[0];
FUJITA Tomonori4f33a9d2007-10-25 09:17:03 +02001891 sg_init_table(sg, UB_MAX_REQ_SG);
Jens Axboe642f1492007-10-24 11:20:47 +02001892 sg_set_page(sg, virt_to_page(p), 8, (unsigned long)p & (PAGE_SIZE-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 cmd->len = 8;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001894 cmd->lun = lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 cmd->done = ub_probe_done;
1896 cmd->back = &compl;
1897
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001898 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 cmd->tag = sc->tagcnt++;
1900
1901 rc = ub_submit_scsi(sc, cmd);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001902 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Pete Zaitcevb5600332006-05-25 20:08:50 -07001904 if (rc != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 goto err_submit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907 wait_for_completion(&compl);
1908
1909 if (cmd->error != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 rc = -EIO;
1911 goto err_read;
1912 }
1913 if (cmd->act_len != 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 rc = -EIO;
1915 goto err_read;
1916 }
1917
1918 /* sd.c special-cases sector size of 0 to mean 512. Needed? Safe? */
1919 nsec = be32_to_cpu(*(__be32 *)p) + 1;
1920 bsize = be32_to_cpu(*(__be32 *)(p + 4));
1921 switch (bsize) {
1922 case 512: shift = 0; break;
1923 case 1024: shift = 1; break;
1924 case 2048: shift = 2; break;
1925 case 4096: shift = 3; break;
1926 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 rc = -EDOM;
1928 goto err_inv_bsize;
1929 }
1930
1931 ret->bsize = bsize;
1932 ret->bshift = shift;
1933 ret->nsec = nsec << shift;
1934 rc = 0;
1935
1936err_inv_bsize:
1937err_read:
1938err_submit:
1939 kfree(cmd);
1940err_alloc:
1941 return rc;
1942}
1943
1944/*
1945 */
David Howells7d12e782006-10-05 14:55:46 +01001946static void ub_probe_urb_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
1948 struct completion *cop = urb->context;
1949 complete(cop);
1950}
1951
1952static void ub_probe_timeout(unsigned long arg)
1953{
1954 struct completion *cop = (struct completion *) arg;
1955 complete(cop);
1956}
1957
1958/*
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001959 * Reset with a Bulk reset.
1960 */
1961static int ub_sync_reset(struct ub_dev *sc)
1962{
1963 int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
1964 struct usb_ctrlrequest *cr;
1965 struct completion compl;
1966 struct timer_list timer;
1967 int rc;
1968
1969 init_completion(&compl);
1970
1971 cr = &sc->work_cr;
1972 cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
1973 cr->bRequest = US_BULK_RESET_REQUEST;
1974 cr->wValue = cpu_to_le16(0);
1975 cr->wIndex = cpu_to_le16(ifnum);
1976 cr->wLength = cpu_to_le16(0);
1977
1978 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
1979 (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001980
1981 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
1982 printk(KERN_WARNING
1983 "%s: Unable to submit a bulk reset (%d)\n", sc->name, rc);
1984 return rc;
1985 }
1986
1987 init_timer(&timer);
1988 timer.function = ub_probe_timeout;
1989 timer.data = (unsigned long) &compl;
1990 timer.expires = jiffies + UB_CTRL_TIMEOUT;
1991 add_timer(&timer);
1992
1993 wait_for_completion(&compl);
1994
1995 del_timer_sync(&timer);
1996 usb_kill_urb(&sc->work_urb);
1997
1998 return sc->work_urb.status;
1999}
2000
2001/*
Pete Zaitcevf4800072005-05-01 16:05:40 -07002002 * Get number of LUNs by the way of Bulk GetMaxLUN command.
2003 */
2004static int ub_sync_getmaxlun(struct ub_dev *sc)
2005{
2006 int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
2007 unsigned char *p;
2008 enum { ALLOC_SIZE = 1 };
2009 struct usb_ctrlrequest *cr;
2010 struct completion compl;
2011 struct timer_list timer;
2012 int nluns;
2013 int rc;
2014
2015 init_completion(&compl);
2016
2017 rc = -ENOMEM;
2018 if ((p = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
2019 goto err_alloc;
2020 *p = 55;
2021
2022 cr = &sc->work_cr;
2023 cr->bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
2024 cr->bRequest = US_BULK_GET_MAX_LUN;
2025 cr->wValue = cpu_to_le16(0);
2026 cr->wIndex = cpu_to_le16(ifnum);
2027 cr->wLength = cpu_to_le16(1);
2028
2029 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->recv_ctrl_pipe,
2030 (unsigned char*) cr, p, 1, ub_probe_urb_complete, &compl);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002031
Pete Zaitcevb5600332006-05-25 20:08:50 -07002032 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0)
Pete Zaitcevf4800072005-05-01 16:05:40 -07002033 goto err_submit;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002034
2035 init_timer(&timer);
2036 timer.function = ub_probe_timeout;
2037 timer.data = (unsigned long) &compl;
2038 timer.expires = jiffies + UB_CTRL_TIMEOUT;
2039 add_timer(&timer);
2040
2041 wait_for_completion(&compl);
2042
2043 del_timer_sync(&timer);
2044 usb_kill_urb(&sc->work_urb);
2045
Pete Zaitcevb5600332006-05-25 20:08:50 -07002046 if ((rc = sc->work_urb.status) < 0)
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002047 goto err_io;
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002048
Pete Zaitcevf4800072005-05-01 16:05:40 -07002049 if (sc->work_urb.actual_length != 1) {
Pete Zaitcevf4800072005-05-01 16:05:40 -07002050 nluns = 0;
2051 } else {
2052 if ((nluns = *p) == 55) {
2053 nluns = 0;
2054 } else {
2055 /* GetMaxLUN returns the maximum LUN number */
2056 nluns += 1;
2057 if (nluns > UB_MAX_LUNS)
2058 nluns = UB_MAX_LUNS;
2059 }
Pete Zaitcevf4800072005-05-01 16:05:40 -07002060 }
2061
2062 kfree(p);
2063 return nluns;
2064
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002065err_io:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002066err_submit:
2067 kfree(p);
2068err_alloc:
2069 return rc;
2070}
2071
2072/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 * Clear initial stalls.
2074 */
2075static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe)
2076{
2077 int endp;
2078 struct usb_ctrlrequest *cr;
2079 struct completion compl;
2080 struct timer_list timer;
2081 int rc;
2082
2083 init_completion(&compl);
2084
2085 endp = usb_pipeendpoint(stalled_pipe);
2086 if (usb_pipein (stalled_pipe))
2087 endp |= USB_DIR_IN;
2088
2089 cr = &sc->work_cr;
2090 cr->bRequestType = USB_RECIP_ENDPOINT;
2091 cr->bRequest = USB_REQ_CLEAR_FEATURE;
2092 cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
2093 cr->wIndex = cpu_to_le16(endp);
2094 cr->wLength = cpu_to_le16(0);
2095
2096 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
2097 (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
2100 printk(KERN_WARNING
2101 "%s: Unable to submit a probe clear (%d)\n", sc->name, rc);
2102 return rc;
2103 }
2104
2105 init_timer(&timer);
2106 timer.function = ub_probe_timeout;
2107 timer.data = (unsigned long) &compl;
2108 timer.expires = jiffies + UB_CTRL_TIMEOUT;
2109 add_timer(&timer);
2110
2111 wait_for_completion(&compl);
2112
2113 del_timer_sync(&timer);
2114 usb_kill_urb(&sc->work_urb);
2115
2116 /* reset the endpoint toggle */
2117 usb_settoggle(sc->dev, endp, usb_pipeout(sc->last_pipe), 0);
2118
2119 return 0;
2120}
2121
2122/*
2123 * Get the pipe settings.
2124 */
2125static int ub_get_pipes(struct ub_dev *sc, struct usb_device *dev,
2126 struct usb_interface *intf)
2127{
2128 struct usb_host_interface *altsetting = intf->cur_altsetting;
2129 struct usb_endpoint_descriptor *ep_in = NULL;
2130 struct usb_endpoint_descriptor *ep_out = NULL;
2131 struct usb_endpoint_descriptor *ep;
2132 int i;
2133
2134 /*
2135 * Find the endpoints we need.
2136 * We are expecting a minimum of 2 endpoints - in and out (bulk).
2137 * We will ignore any others.
2138 */
2139 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
2140 ep = &altsetting->endpoint[i].desc;
2141
2142 /* Is it a BULK endpoint? */
2143 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
2144 == USB_ENDPOINT_XFER_BULK) {
2145 /* BULK in or out? */
Pete Zaitcev643616e2007-03-08 19:56:23 -08002146 if (ep->bEndpointAddress & USB_DIR_IN) {
2147 if (ep_in == NULL)
2148 ep_in = ep;
2149 } else {
2150 if (ep_out == NULL)
2151 ep_out = ep;
2152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 }
2154 }
2155
2156 if (ep_in == NULL || ep_out == NULL) {
Pete Zaitcevf4800072005-05-01 16:05:40 -07002157 printk(KERN_NOTICE "%s: failed endpoint check\n",
2158 sc->name);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002159 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 }
2161
2162 /* Calculate and store the pipe values */
2163 sc->send_ctrl_pipe = usb_sndctrlpipe(dev, 0);
2164 sc->recv_ctrl_pipe = usb_rcvctrlpipe(dev, 0);
2165 sc->send_bulk_pipe = usb_sndbulkpipe(dev,
2166 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2167 sc->recv_bulk_pipe = usb_rcvbulkpipe(dev,
2168 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2169
2170 return 0;
2171}
2172
2173/*
2174 * Probing is done in the process context, which allows us to cheat
2175 * and not to build a state machine for the discovery.
2176 */
2177static int ub_probe(struct usb_interface *intf,
2178 const struct usb_device_id *dev_id)
2179{
2180 struct ub_dev *sc;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002181 int nluns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 int rc;
2183 int i;
2184
Pete Zaitceva00828e2005-10-22 20:15:09 -07002185 if (usb_usual_check_type(dev_id, USB_US_TYPE_UB))
2186 return -ENXIO;
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08002189 if ((sc = kzalloc(sizeof(struct ub_dev), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 goto err_core;
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002191 sc->lock = ub_next_lock();
Pete Zaitcevf4800072005-05-01 16:05:40 -07002192 INIT_LIST_HEAD(&sc->luns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 usb_init_urb(&sc->work_urb);
2194 tasklet_init(&sc->tasklet, ub_scsi_action, (unsigned long)sc);
2195 atomic_set(&sc->poison, 0);
David Howellsc4028952006-11-22 14:57:56 +00002196 INIT_WORK(&sc->reset_work, ub_reset_task);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002197 init_waitqueue_head(&sc->reset_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
2199 init_timer(&sc->work_timer);
2200 sc->work_timer.data = (unsigned long) sc;
2201 sc->work_timer.function = ub_urb_timeout;
2202
2203 ub_init_completion(&sc->work_done);
2204 sc->work_done.done = 1; /* A little yuk, but oh well... */
2205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 sc->dev = interface_to_usbdev(intf);
2207 sc->intf = intf;
2208 // sc->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 usb_set_intfdata(intf, sc);
2210 usb_get_dev(sc->dev);
Pete Zaitcev77ef6c42006-05-03 00:16:00 -07002211 /*
2212 * Since we give the interface struct to the block level through
2213 * disk->driverfs_dev, we have to pin it. Otherwise, block_uevent
2214 * oopses on close after a disconnect (kernels 2.6.16 and up).
2215 */
2216 usb_get_intf(sc->intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Pete Zaitcevf4800072005-05-01 16:05:40 -07002218 snprintf(sc->name, 12, DRV_NAME "(%d.%d)",
2219 sc->dev->bus->busnum, sc->dev->devnum);
2220
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 /* XXX Verify that we can handle the device (from descriptors) */
2222
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002223 if (ub_get_pipes(sc, sc->dev, intf) != 0)
2224 goto err_dev_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 /*
2227 * At this point, all USB initialization is done, do upper layer.
2228 * We really hate halfway initialized structures, so from the
2229 * invariants perspective, this ub_dev is fully constructed at
2230 * this point.
2231 */
2232
2233 /*
2234 * This is needed to clear toggles. It is a problem only if we do
2235 * `rmmod ub && modprobe ub` without disconnects, but we like that.
2236 */
Pete Zaitcevc6c88832005-09-22 00:49:45 -07002237#if 0 /* iPod Mini fails if we do this (big white iPod works) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
2239 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
Pete Zaitcevc6c88832005-09-22 00:49:45 -07002240#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
2242 /*
2243 * The way this is used by the startup code is a little specific.
2244 * A SCSI check causes a USB stall. Our common case code sees it
2245 * and clears the check, after which the device is ready for use.
2246 * But if a check was not present, any command other than
2247 * TEST_UNIT_READY ends with a lockup (including REQUEST_SENSE).
2248 *
2249 * If we neglect to clear the SCSI check, the first real command fails
2250 * (which is the capacity readout). We clear that and retry, but why
2251 * causing spurious retries for no reason.
2252 *
2253 * Revalidation may start with its own TEST_UNIT_READY, but that one
2254 * has to succeed, so we clear checks with an additional one here.
2255 * In any case it's not our business how revaliadation is implemented.
2256 */
Pete Zaitcevb5600332006-05-25 20:08:50 -07002257 for (i = 0; i < 3; i++) { /* Retries for the schwag key from KS'04 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07002258 if ((rc = ub_sync_tur(sc, NULL)) <= 0) break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (rc != 0x6) break;
2260 msleep(10);
2261 }
2262
Pete Zaitcevf4800072005-05-01 16:05:40 -07002263 nluns = 1;
2264 for (i = 0; i < 3; i++) {
Pete Zaitcev11a223a2006-03-02 16:53:00 -08002265 if ((rc = ub_sync_getmaxlun(sc)) < 0)
Pete Zaitcevf4800072005-05-01 16:05:40 -07002266 break;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002267 if (rc != 0) {
2268 nluns = rc;
2269 break;
2270 }
Pete Zaitcev9f793d22005-06-06 13:54:59 -07002271 msleep(100);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Pete Zaitcevf4800072005-05-01 16:05:40 -07002274 for (i = 0; i < nluns; i++) {
2275 ub_probe_lun(sc, i);
2276 }
2277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002279err_dev_desc:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002280 usb_set_intfdata(intf, NULL);
Pete Zaitcev77ef6c42006-05-03 00:16:00 -07002281 usb_put_intf(sc->intf);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002282 usb_put_dev(sc->dev);
2283 kfree(sc);
2284err_core:
2285 return rc;
2286}
2287
2288static int ub_probe_lun(struct ub_dev *sc, int lnum)
2289{
2290 struct ub_lun *lun;
Jens Axboe165125e2007-07-24 09:28:11 +02002291 struct request_queue *q;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002292 struct gendisk *disk;
2293 int rc;
2294
2295 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08002296 if ((lun = kzalloc(sizeof(struct ub_lun), GFP_KERNEL)) == NULL)
Pete Zaitcevf4800072005-05-01 16:05:40 -07002297 goto err_alloc;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002298 lun->num = lnum;
2299
2300 rc = -ENOSR;
2301 if ((lun->id = ub_id_get()) == -1)
2302 goto err_id;
2303
2304 lun->udev = sc;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002305
2306 snprintf(lun->name, 16, DRV_NAME "%c(%d.%d.%d)",
2307 lun->id + 'a', sc->dev->bus->busnum, sc->dev->devnum, lun->num);
2308
2309 lun->removable = 1; /* XXX Query this from the device */
2310 lun->changed = 1; /* ub_revalidate clears only */
Pete Zaitcevf4800072005-05-01 16:05:40 -07002311 ub_revalidate(sc, lun);
2312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 rc = -ENOMEM;
Pete Zaitcev4fb729f2005-12-17 02:34:12 -08002314 if ((disk = alloc_disk(UB_PARTS_PER_LUN)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 goto err_diskalloc;
2316
Pete Zaitcevf4800072005-05-01 16:05:40 -07002317 sprintf(disk->disk_name, DRV_NAME "%c", lun->id + 'a');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 disk->major = UB_MAJOR;
Pete Zaitcev4fb729f2005-12-17 02:34:12 -08002319 disk->first_minor = lun->id * UB_PARTS_PER_LUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 disk->fops = &ub_bd_fops;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002321 disk->private_data = lun;
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002322 disk->driverfs_dev = &sc->intf->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
2324 rc = -ENOMEM;
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002325 if ((q = blk_init_queue(ub_request_fn, sc->lock)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 goto err_blkqinit;
2327
2328 disk->queue = q;
2329
Pete Zaitcevf4800072005-05-01 16:05:40 -07002330 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 blk_queue_max_hw_segments(q, UB_MAX_REQ_SG);
2332 blk_queue_max_phys_segments(q, UB_MAX_REQ_SG);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002333 blk_queue_segment_boundary(q, 0xffffffff); /* Dubious. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 blk_queue_max_sectors(q, UB_MAX_SECTORS);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002335 blk_queue_hardsect_size(q, lun->capacity.bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Pete Zaitcev688e9fb2006-05-25 20:04:54 -07002337 lun->disk = disk;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002338 q->queuedata = lun;
Pete Zaitcev688e9fb2006-05-25 20:04:54 -07002339 list_add(&lun->link, &sc->luns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Pete Zaitcevf4800072005-05-01 16:05:40 -07002341 set_capacity(disk, lun->capacity.nsec);
2342 if (lun->removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 disk->flags |= GENHD_FL_REMOVABLE;
2344
2345 add_disk(disk);
2346
2347 return 0;
2348
2349err_blkqinit:
2350 put_disk(disk);
2351err_diskalloc:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002352 ub_id_put(lun->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353err_id:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002354 kfree(lun);
2355err_alloc:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 return rc;
2357}
2358
2359static void ub_disconnect(struct usb_interface *intf)
2360{
2361 struct ub_dev *sc = usb_get_intfdata(intf);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002362 struct ub_lun *lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 unsigned long flags;
2364
2365 /*
2366 * Prevent ub_bd_release from pulling the rug from under us.
2367 * XXX This is starting to look like a kref.
2368 * XXX Why not to take this ref at probe time?
2369 */
2370 spin_lock_irqsave(&ub_lock, flags);
2371 sc->openc++;
2372 spin_unlock_irqrestore(&ub_lock, flags);
2373
2374 /*
2375 * Fence stall clearnings, operations triggered by unlinkings and so on.
2376 * We do not attempt to unlink any URBs, because we do not trust the
2377 * unlink paths in HC drivers. Also, we get -84 upon disconnect anyway.
2378 */
2379 atomic_set(&sc->poison, 1);
2380
2381 /*
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002382 * Wait for reset to end, if any.
2383 */
2384 wait_event(sc->reset_wait, !sc->reset);
2385
2386 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 * Blow away queued commands.
2388 *
2389 * Actually, this never works, because before we get here
2390 * the HCD terminates outstanding URB(s). It causes our
2391 * SCSI command queue to advance, commands fail to submit,
2392 * and the whole queue drains. So, we just use this code to
2393 * print warnings.
2394 */
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002395 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 {
2397 struct ub_scsi_cmd *cmd;
2398 int cnt = 0;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002399 while ((cmd = ub_cmdq_peek(sc)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 cmd->error = -ENOTCONN;
2401 cmd->state = UB_CMDST_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 ub_cmdq_pop(sc);
2403 (*cmd->done)(sc, cmd);
2404 cnt++;
2405 }
2406 if (cnt != 0) {
2407 printk(KERN_WARNING "%s: "
2408 "%d was queued after shutdown\n", sc->name, cnt);
2409 }
2410 }
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002411 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
2413 /*
2414 * Unregister the upper layer.
2415 */
Matthias Kaehlckea69228d2007-07-09 12:03:07 -07002416 list_for_each_entry(lun, &sc->luns, link) {
Pete Zaitcev688e9fb2006-05-25 20:04:54 -07002417 del_gendisk(lun->disk);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002418 /*
2419 * I wish I could do:
Nick Piggin75ad23b2008-04-29 14:48:33 +02002420 * queue_flag_set(QUEUE_FLAG_DEAD, q);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002421 * As it is, we rely on our internal poisoning and let
2422 * the upper levels to spin furiously failing all the I/O.
2423 */
2424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 * Testing for -EINPROGRESS is always a bug, so we are bending
2428 * the rules a little.
2429 */
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002430 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 if (sc->work_urb.status == -EINPROGRESS) { /* janitors: ignore */
2432 printk(KERN_WARNING "%s: "
2433 "URB is active after disconnect\n", sc->name);
2434 }
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002435 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437 /*
2438 * There is virtually no chance that other CPU runs times so long
2439 * after ub_urb_complete should have called del_timer, but only if HCD
2440 * didn't forget to deliver a callback on unlink.
2441 */
2442 del_timer_sync(&sc->work_timer);
2443
2444 /*
2445 * At this point there must be no commands coming from anyone
2446 * and no URBs left in transit.
2447 */
2448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 ub_put(sc);
2450}
2451
2452static struct usb_driver ub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 .name = "ub",
2454 .probe = ub_probe,
2455 .disconnect = ub_disconnect,
2456 .id_table = ub_usb_ids,
2457};
2458
2459static int __init ub_init(void)
2460{
2461 int rc;
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002462 int i;
2463
2464 for (i = 0; i < UB_QLOCK_NUM; i++)
2465 spin_lock_init(&ub_qlockv[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 if ((rc = register_blkdev(UB_MAJOR, DRV_NAME)) != 0)
2468 goto err_regblkdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
2470 if ((rc = usb_register(&ub_driver)) != 0)
2471 goto err_register;
2472
Pete Zaitceva00828e2005-10-22 20:15:09 -07002473 usb_usual_set_present(USB_US_TYPE_UB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 return 0;
2475
2476err_register:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 unregister_blkdev(UB_MAJOR, DRV_NAME);
2478err_regblkdev:
2479 return rc;
2480}
2481
2482static void __exit ub_exit(void)
2483{
2484 usb_deregister(&ub_driver);
2485
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 unregister_blkdev(UB_MAJOR, DRV_NAME);
Pete Zaitceva00828e2005-10-22 20:15:09 -07002487 usb_usual_clear_present(USB_US_TYPE_UB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488}
2489
2490module_init(ub_init);
2491module_exit(ub_exit);
2492
2493MODULE_LICENSE("GPL");