blob: 3e7689494c66430feb5c97aa518024e05eb2b60e [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 Zaitcevf4800072005-05-01 16:05:40 -070011 * -- Kill first_open (Al Viro fixed the block layer now)
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 * -- special case some senses, e.g. 3a/0 -> no media present, reduce retries
15 * -- verify the 13 conditions and do bulk resets
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * -- kill last_pipe and simply do two-state clearing on both pipes
Pete Zaitcevba6abf12005-07-30 22:38:30 -070017 * -- highmem
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * -- move top_sense and work_bcs into separate allocations (if they survive)
19 * for cache purists and esoteric architectures.
Pete Zaitcevba6abf12005-07-30 22:38:30 -070020 * -- Allocate structure for LUN 0 before the first ub_sync_tur, avoid NULL. ?
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * -- prune comments, they are too volumnous
22 * -- Exterminate P3 printks
23 * -- Resove XXX's
24 * -- Redo "benh's retries", perhaps have spin-up code to handle them. V:D=?
Pete Zaitcev1872bce2005-07-27 11:43:51 -070025 * -- CLEAR, CLR2STS, CLRRS seem to be ripe for refactoring.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 */
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/usb.h>
Pete Zaitceva00828e2005-10-22 20:15:09 -070030#include <linux/usb_usual.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/blkdev.h>
32#include <linux/devfs_fs_kernel.h>
33#include <linux/timer.h>
34#include <scsi/scsi.h>
35
36#define DRV_NAME "ub"
37#define DEVFS_NAME DRV_NAME
38
39#define UB_MAJOR 180
40
41/*
Pete Zaitcev1872bce2005-07-27 11:43:51 -070042 * The command state machine is the key model for understanding of this driver.
43 *
44 * The general rule is that all transitions are done towards the bottom
45 * of the diagram, thus preventing any loops.
46 *
47 * An exception to that is how the STAT state is handled. A counter allows it
48 * to be re-entered along the path marked with [C].
49 *
50 * +--------+
51 * ! INIT !
52 * +--------+
53 * !
54 * ub_scsi_cmd_start fails ->--------------------------------------\
55 * ! !
56 * V !
57 * +--------+ !
58 * ! CMD ! !
59 * +--------+ !
60 * ! +--------+ !
61 * was -EPIPE -->-------------------------------->! CLEAR ! !
62 * ! +--------+ !
63 * ! ! !
64 * was error -->------------------------------------- ! --------->\
65 * ! ! !
66 * /--<-- cmd->dir == NONE ? ! !
67 * ! ! ! !
68 * ! V ! !
69 * ! +--------+ ! !
70 * ! ! DATA ! ! !
71 * ! +--------+ ! !
72 * ! ! +---------+ ! !
73 * ! was -EPIPE -->--------------->! CLR2STS ! ! !
74 * ! ! +---------+ ! !
75 * ! ! ! ! !
76 * ! ! was error -->---- ! --------->\
77 * ! was error -->--------------------- ! ------------- ! --------->\
78 * ! ! ! ! !
79 * ! V ! ! !
80 * \--->+--------+ ! ! !
81 * ! STAT !<--------------------------/ ! !
82 * /--->+--------+ ! !
83 * ! ! ! !
84 * [C] was -EPIPE -->-----------\ ! !
85 * ! ! ! ! !
86 * +<---- len == 0 ! ! !
87 * ! ! ! ! !
88 * ! was error -->--------------------------------------!---------->\
89 * ! ! ! ! !
90 * +<---- bad CSW ! ! !
91 * +<---- bad tag ! ! !
92 * ! ! V ! !
93 * ! ! +--------+ ! !
94 * ! ! ! CLRRS ! ! !
95 * ! ! +--------+ ! !
96 * ! ! ! ! !
97 * \------- ! --------------------[C]--------\ ! !
98 * ! ! ! !
99 * cmd->error---\ +--------+ ! !
100 * ! +--------------->! SENSE !<----------/ !
101 * STAT_FAIL----/ +--------+ !
102 * ! ! V
103 * ! V +--------+
104 * \--------------------------------\--------------------->! DONE !
105 * +--------+
106 */
107
108/*
Pete Zaitcevf4800072005-05-01 16:05:40 -0700109 * This many LUNs per USB device.
110 * Every one of them takes a host, see UB_MAX_HOSTS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 */
Pete Zaitcev9f793d22005-06-06 13:54:59 -0700112#define UB_MAX_LUNS 9
Pete Zaitcevf4800072005-05-01 16:05:40 -0700113
114/*
115 */
116
Pete Zaitcev4fb729f2005-12-17 02:34:12 -0800117#define UB_PARTS_PER_LUN 8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119#define UB_MAX_CDB_SIZE 16 /* Corresponds to Bulk */
120
121#define UB_SENSE_SIZE 18
122
123/*
124 */
125
126/* command block wrapper */
127struct bulk_cb_wrap {
128 __le32 Signature; /* contains 'USBC' */
129 u32 Tag; /* unique per command id */
130 __le32 DataTransferLength; /* size of data */
131 u8 Flags; /* direction in bit 0 */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700132 u8 Lun; /* LUN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 u8 Length; /* of of the CDB */
134 u8 CDB[UB_MAX_CDB_SIZE]; /* max command */
135};
136
137#define US_BULK_CB_WRAP_LEN 31
138#define US_BULK_CB_SIGN 0x43425355 /*spells out USBC */
139#define US_BULK_FLAG_IN 1
140#define US_BULK_FLAG_OUT 0
141
142/* command status wrapper */
143struct bulk_cs_wrap {
144 __le32 Signature; /* should = 'USBS' */
145 u32 Tag; /* same as original command */
146 __le32 Residue; /* amount not transferred */
147 u8 Status; /* see below */
148};
149
150#define US_BULK_CS_WRAP_LEN 13
151#define US_BULK_CS_SIGN 0x53425355 /* spells out 'USBS' */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#define US_BULK_STAT_OK 0
153#define US_BULK_STAT_FAIL 1
154#define US_BULK_STAT_PHASE 2
155
156/* bulk-only class specific requests */
157#define US_BULK_RESET_REQUEST 0xff
158#define US_BULK_GET_MAX_LUN 0xfe
159
160/*
161 */
162struct ub_dev;
163
Pete Zaitcev64bd8452005-09-22 00:48:29 -0700164#define UB_MAX_REQ_SG 9 /* cdrecord requires 32KB and maybe a header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#define UB_MAX_SECTORS 64
166
167/*
168 * A second is more than enough for a 32K transfer (UB_MAX_SECTORS)
169 * even if a webcam hogs the bus, but some devices need time to spin up.
170 */
171#define UB_URB_TIMEOUT (HZ*2)
172#define UB_DATA_TIMEOUT (HZ*5) /* ZIP does spin-ups in the data phase */
173#define UB_STAT_TIMEOUT (HZ*5) /* Same spinups and eject for a dataless cmd. */
174#define UB_CTRL_TIMEOUT (HZ/2) /* 500ms ought to be enough to clear a stall */
175
176/*
177 * An instance of a SCSI command in transit.
178 */
179#define UB_DIR_NONE 0
180#define UB_DIR_READ 1
181#define UB_DIR_ILLEGAL2 2
182#define UB_DIR_WRITE 3
183
184#define UB_DIR_CHAR(c) (((c)==UB_DIR_WRITE)? 'w': \
185 (((c)==UB_DIR_READ)? 'r': 'n'))
186
187enum ub_scsi_cmd_state {
188 UB_CMDST_INIT, /* Initial state */
189 UB_CMDST_CMD, /* Command submitted */
190 UB_CMDST_DATA, /* Data phase */
191 UB_CMDST_CLR2STS, /* Clearing before requesting status */
192 UB_CMDST_STAT, /* Status phase */
193 UB_CMDST_CLEAR, /* Clearing a stall (halt, actually) */
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700194 UB_CMDST_CLRRS, /* Clearing before retrying status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 UB_CMDST_SENSE, /* Sending Request Sense */
196 UB_CMDST_DONE /* Final state */
197};
198
199static char *ub_scsi_cmd_stname[] = {
200 ". ",
201 "Cmd",
202 "dat",
203 "c2s",
204 "sts",
205 "clr",
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700206 "crs",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 "Sen",
208 "fin"
209};
210
211struct ub_scsi_cmd {
212 unsigned char cdb[UB_MAX_CDB_SIZE];
213 unsigned char cdb_len;
214
215 unsigned char dir; /* 0 - none, 1 - read, 3 - write. */
216 unsigned char trace_index;
217 enum ub_scsi_cmd_state state;
218 unsigned int tag;
219 struct ub_scsi_cmd *next;
220
221 int error; /* Return code - valid upon done */
222 unsigned int act_len; /* Return size */
223 unsigned char key, asc, ascq; /* May be valid if error==-EIO */
224
225 int stat_count; /* Retries getting status. */
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 unsigned int len; /* Requested length */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700228 unsigned int current_sg;
229 unsigned int nsg; /* sgv[nsg] */
230 struct scatterlist sgv[UB_MAX_REQ_SG];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Pete Zaitcevf4800072005-05-01 16:05:40 -0700232 struct ub_lun *lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 void (*done)(struct ub_dev *, struct ub_scsi_cmd *);
234 void *back;
235};
236
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800237struct ub_request {
238 struct request *rq;
239 unsigned int current_try;
240 unsigned int nsg; /* sgv[nsg] */
241 struct scatterlist sgv[UB_MAX_REQ_SG];
242};
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244/*
245 */
246struct ub_capacity {
247 unsigned long nsec; /* Linux size - 512 byte sectors */
248 unsigned int bsize; /* Linux hardsect_size */
249 unsigned int bshift; /* Shift between 512 and hard sects */
250};
251
252/*
253 * The SCSI command tracing structure.
254 */
255
256#define SCMD_ST_HIST_SZ 8
257#define SCMD_TRACE_SZ 63 /* Less than 4KB of 61-byte lines */
258
259struct ub_scsi_cmd_trace {
260 int hcur;
261 unsigned int tag;
262 unsigned int req_size, act_size;
263 unsigned char op;
264 unsigned char dir;
265 unsigned char key, asc, ascq;
266 char st_hst[SCMD_ST_HIST_SZ];
267};
268
269struct ub_scsi_trace {
270 int cur;
271 struct ub_scsi_cmd_trace vec[SCMD_TRACE_SZ];
272};
273
274/*
275 * This is a direct take-off from linux/include/completion.h
276 * The difference is that I do not wait on this thing, just poll.
277 * When I want to wait (ub_probe), I just use the stock completion.
278 *
279 * Note that INIT_COMPLETION takes no lock. It is correct. But why
280 * in the bloody hell that thing takes struct instead of pointer to struct
281 * is quite beyond me. I just copied it from the stock completion.
282 */
283struct ub_completion {
284 unsigned int done;
285 spinlock_t lock;
286};
287
288static inline void ub_init_completion(struct ub_completion *x)
289{
290 x->done = 0;
291 spin_lock_init(&x->lock);
292}
293
294#define UB_INIT_COMPLETION(x) ((x).done = 0)
295
296static void ub_complete(struct ub_completion *x)
297{
298 unsigned long flags;
299
300 spin_lock_irqsave(&x->lock, flags);
301 x->done++;
302 spin_unlock_irqrestore(&x->lock, flags);
303}
304
305static int ub_is_completed(struct ub_completion *x)
306{
307 unsigned long flags;
308 int ret;
309
310 spin_lock_irqsave(&x->lock, flags);
311 ret = x->done;
312 spin_unlock_irqrestore(&x->lock, flags);
313 return ret;
314}
315
316/*
317 */
318struct ub_scsi_cmd_queue {
319 int qlen, qmax;
320 struct ub_scsi_cmd *head, *tail;
321};
322
323/*
Pete Zaitcevf4800072005-05-01 16:05:40 -0700324 * The block device instance (one per LUN).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700326struct ub_lun {
327 struct ub_dev *udev;
328 struct list_head link;
329 struct gendisk *disk;
330 int id; /* Host index */
331 int num; /* LUN number */
332 char name[16];
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 int changed; /* Media was changed */
335 int removable;
336 int readonly;
337 int first_open; /* Kludge. See ub_bd_open. */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700338
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800339 struct ub_request urq;
340
Pete Zaitcevf4800072005-05-01 16:05:40 -0700341 /* Use Ingo's mempool if or when we have more than one command. */
342 /*
343 * Currently we never need more than one command for the whole device.
344 * However, giving every LUN a command is a cheap and automatic way
345 * to enforce fairness between them.
346 */
347 int cmda[1];
348 struct ub_scsi_cmd cmdv[1];
349
350 struct ub_capacity capacity;
351};
352
353/*
354 * The USB device instance.
355 */
356struct ub_dev {
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800357 spinlock_t *lock;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700358 atomic_t poison; /* The USB device is disconnected */
359 int openc; /* protected by ub_lock! */
360 /* kref is too implicit for our taste */
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800361 int reset; /* Reset is running */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700362 unsigned int tagcnt;
363 char name[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 struct usb_device *dev;
365 struct usb_interface *intf;
366
Pete Zaitcevf4800072005-05-01 16:05:40 -0700367 struct list_head luns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 unsigned int send_bulk_pipe; /* cached pipe values */
370 unsigned int recv_bulk_pipe;
371 unsigned int send_ctrl_pipe;
372 unsigned int recv_ctrl_pipe;
373
374 struct tasklet_struct tasklet;
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 struct ub_scsi_cmd_queue cmd_queue;
377 struct ub_scsi_cmd top_rqs_cmd; /* REQUEST SENSE */
378 unsigned char top_sense[UB_SENSE_SIZE];
379
380 struct ub_completion work_done;
381 struct urb work_urb;
382 struct timer_list work_timer;
383 int last_pipe; /* What might need clearing */
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700384 __le32 signature; /* Learned signature */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 struct bulk_cb_wrap work_bcb;
386 struct bulk_cs_wrap work_bcs;
387 struct usb_ctrlrequest work_cr;
388
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800389 struct work_struct reset_work;
390 wait_queue_head_t reset_wait;
391
Pete Zaitcev64bd8452005-09-22 00:48:29 -0700392 int sg_stat[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 struct ub_scsi_trace tr;
394};
395
396/*
397 */
398static void ub_cleanup(struct ub_dev *sc);
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700399static int ub_request_fn_1(struct ub_lun *lun, struct request *rq);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800400static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
401 struct ub_scsi_cmd *cmd, struct ub_request *urq);
402static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
403 struct ub_scsi_cmd *cmd, struct ub_request *urq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
405static void ub_end_rq(struct request *rq, int uptodate);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800406static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
407 struct ub_request *urq, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
409static void ub_urb_complete(struct urb *urb, struct pt_regs *pt);
410static void ub_scsi_action(unsigned long _dev);
411static void ub_scsi_dispatch(struct ub_dev *sc);
412static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700413static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc);
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700415static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Pete Zaitcev1872bce2005-07-27 11:43:51 -0700417static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
419static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
420 int stalled_pipe);
421static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd);
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -0800422static void ub_reset_enter(struct ub_dev *sc, int try);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800423static void ub_reset_task(void *arg);
Pete Zaitcevf4800072005-05-01 16:05:40 -0700424static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun);
425static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
426 struct ub_capacity *ret);
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -0800427static int ub_sync_reset(struct ub_dev *sc);
428static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe);
Pete Zaitcevf4800072005-05-01 16:05:40 -0700429static int ub_probe_lun(struct ub_dev *sc, int lnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431/*
432 */
Pete Zaitceva00828e2005-10-22 20:15:09 -0700433#ifdef CONFIG_USB_LIBUSUAL
434
435#define ub_usb_ids storage_usb_ids
436#else
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438static struct usb_device_id ub_usb_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_SCSI, US_PR_BULK) },
440 { }
441};
442
443MODULE_DEVICE_TABLE(usb, ub_usb_ids);
Pete Zaitceva00828e2005-10-22 20:15:09 -0700444#endif /* CONFIG_USB_LIBUSUAL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446/*
447 * Find me a way to identify "next free minor" for add_disk(),
448 * and the array disappears the next day. However, the number of
449 * hosts has something to do with the naming and /proc/partitions.
450 * This has to be thought out in detail before changing.
451 * If UB_MAX_HOST was 1000, we'd use a bitmap. Or a better data structure.
452 */
453#define UB_MAX_HOSTS 26
454static char ub_hostv[UB_MAX_HOSTS];
Pete Zaitcevf4800072005-05-01 16:05:40 -0700455
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800456#define UB_QLOCK_NUM 5
457static spinlock_t ub_qlockv[UB_QLOCK_NUM];
458static int ub_qlock_next = 0;
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460static DEFINE_SPINLOCK(ub_lock); /* Locks globals and ->openc */
461
462/*
463 * The SCSI command tracing procedures.
464 */
465
466static void ub_cmdtr_new(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
467{
468 int n;
469 struct ub_scsi_cmd_trace *t;
470
471 if ((n = sc->tr.cur + 1) == SCMD_TRACE_SZ) n = 0;
472 t = &sc->tr.vec[n];
473
474 memset(t, 0, sizeof(struct ub_scsi_cmd_trace));
475 t->tag = cmd->tag;
476 t->op = cmd->cdb[0];
477 t->dir = cmd->dir;
478 t->req_size = cmd->len;
479 t->st_hst[0] = cmd->state;
480
481 sc->tr.cur = n;
482 cmd->trace_index = n;
483}
484
485static void ub_cmdtr_state(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
486{
487 int n;
488 struct ub_scsi_cmd_trace *t;
489
490 t = &sc->tr.vec[cmd->trace_index];
491 if (t->tag == cmd->tag) {
492 if ((n = t->hcur + 1) == SCMD_ST_HIST_SZ) n = 0;
493 t->st_hst[n] = cmd->state;
494 t->hcur = n;
495 }
496}
497
498static void ub_cmdtr_act_len(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
499{
500 struct ub_scsi_cmd_trace *t;
501
502 t = &sc->tr.vec[cmd->trace_index];
503 if (t->tag == cmd->tag)
504 t->act_size = cmd->act_len;
505}
506
507static void ub_cmdtr_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
508 unsigned char *sense)
509{
510 struct ub_scsi_cmd_trace *t;
511
512 t = &sc->tr.vec[cmd->trace_index];
513 if (t->tag == cmd->tag) {
514 t->key = sense[2] & 0x0F;
515 t->asc = sense[12];
516 t->ascq = sense[13];
517 }
518}
519
Pete Zaitcev07d4fd22005-07-30 22:51:45 -0700520static ssize_t ub_diag_show(struct device *dev, struct device_attribute *attr,
521 char *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 struct usb_interface *intf;
524 struct ub_dev *sc;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700525 struct list_head *p;
526 struct ub_lun *lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 int cnt;
528 unsigned long flags;
529 int nc, nh;
530 int i, j;
531 struct ub_scsi_cmd_trace *t;
532
533 intf = to_usb_interface(dev);
534 sc = usb_get_intfdata(intf);
535 if (sc == NULL)
536 return 0;
537
538 cnt = 0;
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800539 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 cnt += sprintf(page + cnt,
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800542 "poison %d reset %d\n",
543 atomic_read(&sc->poison), sc->reset);
544 cnt += sprintf(page + cnt,
Pete Zaitcevf4800072005-05-01 16:05:40 -0700545 "qlen %d qmax %d\n",
546 sc->cmd_queue.qlen, sc->cmd_queue.qmax);
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700547 cnt += sprintf(page + cnt,
Pete Zaitcev64bd8452005-09-22 00:48:29 -0700548 "sg %d %d %d %d %d .. %d\n",
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700549 sc->sg_stat[0],
550 sc->sg_stat[1],
551 sc->sg_stat[2],
552 sc->sg_stat[3],
Pete Zaitcev64bd8452005-09-22 00:48:29 -0700553 sc->sg_stat[4],
554 sc->sg_stat[5]);
Pete Zaitcevf4800072005-05-01 16:05:40 -0700555
556 list_for_each (p, &sc->luns) {
557 lun = list_entry(p, struct ub_lun, link);
558 cnt += sprintf(page + cnt,
559 "lun %u changed %d removable %d readonly %d\n",
560 lun->num, lun->changed, lun->removable, lun->readonly);
561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 if ((nc = sc->tr.cur + 1) == SCMD_TRACE_SZ) nc = 0;
564 for (j = 0; j < SCMD_TRACE_SZ; j++) {
565 t = &sc->tr.vec[nc];
566
567 cnt += sprintf(page + cnt, "%08x %02x", t->tag, t->op);
568 if (t->op == REQUEST_SENSE) {
569 cnt += sprintf(page + cnt, " [sense %x %02x %02x]",
570 t->key, t->asc, t->ascq);
571 } else {
572 cnt += sprintf(page + cnt, " %c", UB_DIR_CHAR(t->dir));
573 cnt += sprintf(page + cnt, " [%5d %5d]",
574 t->req_size, t->act_size);
575 }
576 if ((nh = t->hcur + 1) == SCMD_ST_HIST_SZ) nh = 0;
577 for (i = 0; i < SCMD_ST_HIST_SZ; i++) {
578 cnt += sprintf(page + cnt, " %s",
579 ub_scsi_cmd_stname[(int)t->st_hst[nh]]);
580 if (++nh == SCMD_ST_HIST_SZ) nh = 0;
581 }
582 cnt += sprintf(page + cnt, "\n");
583
584 if (++nc == SCMD_TRACE_SZ) nc = 0;
585 }
586
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800587 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return cnt;
589}
590
591static DEVICE_ATTR(diag, S_IRUGO, ub_diag_show, NULL); /* N.B. World readable */
592
593/*
594 * The id allocator.
595 *
596 * This also stores the host for indexing by minor, which is somewhat dirty.
597 */
598static int ub_id_get(void)
599{
600 unsigned long flags;
601 int i;
602
603 spin_lock_irqsave(&ub_lock, flags);
604 for (i = 0; i < UB_MAX_HOSTS; i++) {
605 if (ub_hostv[i] == 0) {
606 ub_hostv[i] = 1;
607 spin_unlock_irqrestore(&ub_lock, flags);
608 return i;
609 }
610 }
611 spin_unlock_irqrestore(&ub_lock, flags);
612 return -1;
613}
614
615static void ub_id_put(int id)
616{
617 unsigned long flags;
618
619 if (id < 0 || id >= UB_MAX_HOSTS) {
620 printk(KERN_ERR DRV_NAME ": bad host ID %d\n", id);
621 return;
622 }
623
624 spin_lock_irqsave(&ub_lock, flags);
625 if (ub_hostv[id] == 0) {
626 spin_unlock_irqrestore(&ub_lock, flags);
627 printk(KERN_ERR DRV_NAME ": freeing free host ID %d\n", id);
628 return;
629 }
630 ub_hostv[id] = 0;
631 spin_unlock_irqrestore(&ub_lock, flags);
632}
633
634/*
Pete Zaitcev65b4fe52005-12-28 14:22:17 -0800635 * This is necessitated by the fact that blk_cleanup_queue does not
636 * necesserily destroy the queue. Instead, it may merely decrease q->refcnt.
637 * Since our blk_init_queue() passes a spinlock common with ub_dev,
638 * we have life time issues when ub_cleanup frees ub_dev.
639 */
640static spinlock_t *ub_next_lock(void)
641{
642 unsigned long flags;
643 spinlock_t *ret;
644
645 spin_lock_irqsave(&ub_lock, flags);
646 ret = &ub_qlockv[ub_qlock_next];
647 ub_qlock_next = (ub_qlock_next + 1) % UB_QLOCK_NUM;
648 spin_unlock_irqrestore(&ub_lock, flags);
649 return ret;
650}
651
652/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 * Downcount for deallocation. This rides on two assumptions:
654 * - once something is poisoned, its refcount cannot grow
655 * - opens cannot happen at this time (del_gendisk was done)
656 * If the above is true, we can drop the lock, which we need for
657 * blk_cleanup_queue(): the silly thing may attempt to sleep.
658 * [Actually, it never needs to sleep for us, but it calls might_sleep()]
659 */
660static void ub_put(struct ub_dev *sc)
661{
662 unsigned long flags;
663
664 spin_lock_irqsave(&ub_lock, flags);
665 --sc->openc;
666 if (sc->openc == 0 && atomic_read(&sc->poison)) {
667 spin_unlock_irqrestore(&ub_lock, flags);
668 ub_cleanup(sc);
669 } else {
670 spin_unlock_irqrestore(&ub_lock, flags);
671 }
672}
673
674/*
675 * Final cleanup and deallocation.
676 */
677static void ub_cleanup(struct ub_dev *sc)
678{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700679 struct list_head *p;
680 struct ub_lun *lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 request_queue_t *q;
682
Pete Zaitcevf4800072005-05-01 16:05:40 -0700683 while (!list_empty(&sc->luns)) {
684 p = sc->luns.next;
685 lun = list_entry(p, struct ub_lun, link);
686 list_del(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Pete Zaitcevf4800072005-05-01 16:05:40 -0700688 /* I don't think queue can be NULL. But... Stolen from sx8.c */
689 if ((q = lun->disk->queue) != NULL)
690 blk_cleanup_queue(q);
691 /*
692 * If we zero disk->private_data BEFORE put_disk, we have
693 * to check for NULL all over the place in open, release,
694 * check_media and revalidate, because the block level
695 * semaphore is well inside the put_disk.
696 * But we cannot zero after the call, because *disk is gone.
697 * The sd.c is blatantly racy in this area.
698 */
699 /* disk->private_data = NULL; */
700 put_disk(lun->disk);
701 lun->disk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Pete Zaitcevf4800072005-05-01 16:05:40 -0700703 ub_id_put(lun->id);
704 kfree(lun);
705 }
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 kfree(sc);
708}
709
710/*
711 * The "command allocator".
712 */
Pete Zaitcevf4800072005-05-01 16:05:40 -0700713static struct ub_scsi_cmd *ub_get_cmd(struct ub_lun *lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 struct ub_scsi_cmd *ret;
716
Pete Zaitcevf4800072005-05-01 16:05:40 -0700717 if (lun->cmda[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return NULL;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700719 ret = &lun->cmdv[0];
720 lun->cmda[0] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return ret;
722}
723
Pete Zaitcevf4800072005-05-01 16:05:40 -0700724static void ub_put_cmd(struct ub_lun *lun, struct ub_scsi_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700726 if (cmd != &lun->cmdv[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 printk(KERN_WARNING "%s: releasing a foreign cmd %p\n",
Pete Zaitcevf4800072005-05-01 16:05:40 -0700728 lun->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return;
730 }
Pete Zaitcevf4800072005-05-01 16:05:40 -0700731 if (!lun->cmda[0]) {
732 printk(KERN_WARNING "%s: releasing a free cmd\n", lun->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return;
734 }
Pete Zaitcevf4800072005-05-01 16:05:40 -0700735 lun->cmda[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
738/*
739 * The command queue.
740 */
741static void ub_cmdq_add(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
742{
743 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
744
745 if (t->qlen++ == 0) {
746 t->head = cmd;
747 t->tail = cmd;
748 } else {
749 t->tail->next = cmd;
750 t->tail = cmd;
751 }
752
753 if (t->qlen > t->qmax)
754 t->qmax = t->qlen;
755}
756
757static void ub_cmdq_insert(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
758{
759 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
760
761 if (t->qlen++ == 0) {
762 t->head = cmd;
763 t->tail = cmd;
764 } else {
765 cmd->next = t->head;
766 t->head = cmd;
767 }
768
769 if (t->qlen > t->qmax)
770 t->qmax = t->qlen;
771}
772
773static struct ub_scsi_cmd *ub_cmdq_pop(struct ub_dev *sc)
774{
775 struct ub_scsi_cmd_queue *t = &sc->cmd_queue;
776 struct ub_scsi_cmd *cmd;
777
778 if (t->qlen == 0)
779 return NULL;
780 if (--t->qlen == 0)
781 t->tail = NULL;
782 cmd = t->head;
783 t->head = cmd->next;
784 cmd->next = NULL;
785 return cmd;
786}
787
788#define ub_cmdq_peek(sc) ((sc)->cmd_queue.head)
789
790/*
791 * The request function is our main entry point
792 */
793
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700794static void ub_request_fn(request_queue_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700796 struct ub_lun *lun = q->queuedata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 struct request *rq;
798
799 while ((rq = elv_next_request(q)) != NULL) {
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700800 if (ub_request_fn_1(lun, rq) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 blk_stop_queue(q);
802 break;
803 }
804 }
805}
806
Pete Zaitcev6c1eb8c2005-07-30 22:51:52 -0700807static int ub_request_fn_1(struct ub_lun *lun, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700809 struct ub_dev *sc = lun->udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 struct ub_scsi_cmd *cmd;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800811 struct ub_request *urq;
812 int n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Pete Zaitcevf4800072005-05-01 16:05:40 -0700814 if (atomic_read(&sc->poison) || lun->changed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 blkdev_dequeue_request(rq);
816 ub_end_rq(rq, 0);
817 return 0;
818 }
819
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800820 if (lun->urq.rq != NULL)
821 return -1;
Pete Zaitcevf4800072005-05-01 16:05:40 -0700822 if ((cmd = ub_get_cmd(lun)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return -1;
824 memset(cmd, 0, sizeof(struct ub_scsi_cmd));
825
826 blkdev_dequeue_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800828 urq = &lun->urq;
829 memset(urq, 0, sizeof(struct ub_request));
830 urq->rq = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 /*
833 * get scatterlist from block layer
834 */
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800835 n_elem = blk_rq_map_sg(lun->disk->queue, rq, &urq->sgv[0]);
836 if (n_elem < 0) {
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700837 printk(KERN_INFO "%s: failed request map (%d)\n",
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800838 lun->name, n_elem); /* P3 */
839 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700841 if (n_elem > UB_MAX_REQ_SG) { /* Paranoia */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 printk(KERN_WARNING "%s: request with %d segments\n",
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800843 lun->name, n_elem);
844 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800846 urq->nsg = n_elem;
Pete Zaitcev64bd8452005-09-22 00:48:29 -0700847 sc->sg_stat[n_elem < 5 ? n_elem : 5]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800849 if (blk_pc_request(rq)) {
850 ub_cmd_build_packet(sc, lun, cmd, urq);
851 } else {
852 ub_cmd_build_block(sc, lun, cmd, urq);
853 }
854 cmd->state = UB_CMDST_INIT;
855 cmd->lun = lun;
856 cmd->done = ub_rw_cmd_done;
857 cmd->back = urq;
858
859 cmd->tag = sc->tagcnt++;
860 if (ub_submit_scsi(sc, cmd) != 0)
861 goto drop;
862
863 return 0;
864
865drop:
866 ub_put_cmd(lun, cmd);
867 ub_end_rq(rq, 0);
868 return 0;
869}
870
871static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
872 struct ub_scsi_cmd *cmd, struct ub_request *urq)
873{
874 struct request *rq = urq->rq;
875 unsigned int block, nblks;
876
877 if (rq_data_dir(rq) == WRITE)
878 cmd->dir = UB_DIR_WRITE;
879 else
880 cmd->dir = UB_DIR_READ;
881
882 cmd->nsg = urq->nsg;
883 memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 /*
886 * build the command
887 *
888 * The call to blk_queue_hardsect_size() guarantees that request
889 * is aligned, but it is given in terms of 512 byte units, always.
890 */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700891 block = rq->sector >> lun->capacity.bshift;
892 nblks = rq->nr_sectors >> lun->capacity.bshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800894 cmd->cdb[0] = (cmd->dir == UB_DIR_READ)? READ_10: WRITE_10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 /* 10-byte uses 4 bytes of LBA: 2147483648KB, 2097152MB, 2048GB */
896 cmd->cdb[2] = block >> 24;
897 cmd->cdb[3] = block >> 16;
898 cmd->cdb[4] = block >> 8;
899 cmd->cdb[5] = block;
900 cmd->cdb[7] = nblks >> 8;
901 cmd->cdb[8] = nblks;
902 cmd->cdb_len = 10;
903
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700904 cmd->len = rq->nr_sectors * 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800907static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
908 struct ub_scsi_cmd *cmd, struct ub_request *urq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800910 struct request *rq = urq->rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 if (rq->data_len == 0) {
913 cmd->dir = UB_DIR_NONE;
914 } else {
915 if (rq_data_dir(rq) == WRITE)
916 cmd->dir = UB_DIR_WRITE;
917 else
918 cmd->dir = UB_DIR_READ;
919 }
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700920
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800921 cmd->nsg = urq->nsg;
922 memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700923
924 memcpy(&cmd->cdb, rq->cmd, rq->cmd_len);
925 cmd->cdb_len = rq->cmd_len;
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 cmd->len = rq->data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
930static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
931{
Pete Zaitcevf4800072005-05-01 16:05:40 -0700932 struct ub_lun *lun = cmd->lun;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800933 struct ub_request *urq = cmd->back;
934 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 int uptodate;
936
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800937 rq = urq->rq;
938
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700939 if (cmd->error == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 uptodate = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700942 if (blk_pc_request(rq)) {
943 if (cmd->act_len >= rq->data_len)
944 rq->data_len = 0;
945 else
946 rq->data_len -= cmd->act_len;
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700947 }
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700948 } else {
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700949 uptodate = 0;
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700950
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700951 if (blk_pc_request(rq)) {
952 /* UB_SENSE_SIZE is smaller than SCSI_SENSE_BUFFERSIZE */
953 memcpy(rq->sense, sc->top_sense, UB_SENSE_SIZE);
954 rq->sense_len = UB_SENSE_SIZE;
955 if (sc->top_sense[0] != 0)
956 rq->errors = SAM_STAT_CHECK_CONDITION;
957 else
958 rq->errors = DID_ERROR << 16;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800959 } else {
960 if (cmd->error == -EIO) {
961 if (ub_rw_cmd_retry(sc, lun, urq, cmd) == 0)
962 return;
963 }
Pete Zaitceva1cf96e2005-08-14 21:16:03 -0700964 }
965 }
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700966
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800967 urq->rq = NULL;
968
Pete Zaitcevf4800072005-05-01 16:05:40 -0700969 ub_put_cmd(lun, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 ub_end_rq(rq, uptodate);
Pete Zaitcevba6abf12005-07-30 22:38:30 -0700971 blk_start_queue(lun->disk->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
974static void ub_end_rq(struct request *rq, int uptodate)
975{
Daniel Marjamakiab930912005-12-17 02:38:46 -0800976 end_that_request_first(rq, uptodate, rq->hard_nr_sectors);
Tejun Heo8ffdc652006-01-06 09:49:03 +0100977 end_that_request_last(rq, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800980static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
981 struct ub_request *urq, struct ub_scsi_cmd *cmd)
982{
983
984 if (atomic_read(&sc->poison))
985 return -ENXIO;
986
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -0800987 ub_reset_enter(sc, urq->current_try);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -0800988
989 if (urq->current_try >= 3)
990 return -EIO;
991 urq->current_try++;
992 /* P3 */ printk("%s: dir %c len/act %d/%d "
993 "[sense %x %02x %02x] retry %d\n",
994 sc->name, UB_DIR_CHAR(cmd->dir), cmd->len, cmd->act_len,
995 cmd->key, cmd->asc, cmd->ascq, urq->current_try);
996
997 memset(cmd, 0, sizeof(struct ub_scsi_cmd));
998 ub_cmd_build_block(sc, lun, cmd, urq);
999
1000 cmd->state = UB_CMDST_INIT;
1001 cmd->lun = lun;
1002 cmd->done = ub_rw_cmd_done;
1003 cmd->back = urq;
1004
1005 cmd->tag = sc->tagcnt++;
1006
1007#if 0 /* Wasteful */
1008 return ub_submit_scsi(sc, cmd);
1009#else
1010 ub_cmdq_add(sc, cmd);
1011 return 0;
1012#endif
1013}
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015/*
1016 * Submit a regular SCSI operation (not an auto-sense).
1017 *
1018 * The Iron Law of Good Submit Routine is:
1019 * Zero return - callback is done, Nonzero return - callback is not done.
1020 * No exceptions.
1021 *
1022 * Host is assumed locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 */
1024static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1025{
1026
1027 if (cmd->state != UB_CMDST_INIT ||
1028 (cmd->dir != UB_DIR_NONE && cmd->len == 0)) {
1029 return -EINVAL;
1030 }
1031
1032 ub_cmdq_add(sc, cmd);
1033 /*
1034 * We can call ub_scsi_dispatch(sc) right away here, but it's a little
1035 * safer to jump to a tasklet, in case upper layers do something silly.
1036 */
1037 tasklet_schedule(&sc->tasklet);
1038 return 0;
1039}
1040
1041/*
1042 * Submit the first URB for the queued command.
1043 * This function does not deal with queueing in any way.
1044 */
1045static int ub_scsi_cmd_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1046{
1047 struct bulk_cb_wrap *bcb;
1048 int rc;
1049
1050 bcb = &sc->work_bcb;
1051
1052 /*
1053 * ``If the allocation length is eighteen or greater, and a device
1054 * server returns less than eithteen bytes of data, the application
1055 * client should assume that the bytes not transferred would have been
1056 * zeroes had the device server returned those bytes.''
1057 *
1058 * We zero sense for all commands so that when a packet request
1059 * fails it does not return a stale sense.
1060 */
1061 memset(&sc->top_sense, 0, UB_SENSE_SIZE);
1062
1063 /* set up the command wrapper */
1064 bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN);
1065 bcb->Tag = cmd->tag; /* Endianness is not important */
1066 bcb->DataTransferLength = cpu_to_le32(cmd->len);
1067 bcb->Flags = (cmd->dir == UB_DIR_READ) ? 0x80 : 0;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001068 bcb->Lun = (cmd->lun != NULL) ? cmd->lun->num : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 bcb->Length = cmd->cdb_len;
1070
1071 /* copy the command payload */
1072 memcpy(bcb->CDB, cmd->cdb, UB_MAX_CDB_SIZE);
1073
1074 UB_INIT_COMPLETION(sc->work_done);
1075
1076 sc->last_pipe = sc->send_bulk_pipe;
1077 usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->send_bulk_pipe,
1078 bcb, US_BULK_CB_WRAP_LEN, ub_urb_complete, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 /* Fill what we shouldn't be filling, because usb-storage did so. */
1081 sc->work_urb.actual_length = 0;
1082 sc->work_urb.error_count = 0;
1083 sc->work_urb.status = 0;
1084
1085 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1086 /* XXX Clear stalls */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 ub_complete(&sc->work_done);
1088 return rc;
1089 }
1090
1091 sc->work_timer.expires = jiffies + UB_URB_TIMEOUT;
1092 add_timer(&sc->work_timer);
1093
1094 cmd->state = UB_CMDST_CMD;
1095 ub_cmdtr_state(sc, cmd);
1096 return 0;
1097}
1098
1099/*
1100 * Timeout handler.
1101 */
1102static void ub_urb_timeout(unsigned long arg)
1103{
1104 struct ub_dev *sc = (struct ub_dev *) arg;
1105 unsigned long flags;
1106
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001107 spin_lock_irqsave(sc->lock, flags);
Pete Zaitcevb31f8212006-01-05 00:14:02 -08001108 if (!ub_is_completed(&sc->work_done))
1109 usb_unlink_urb(&sc->work_urb);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001110 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
1113/*
1114 * Completion routine for the work URB.
1115 *
1116 * This can be called directly from usb_submit_urb (while we have
1117 * the sc->lock taken) and from an interrupt (while we do NOT have
1118 * the sc->lock taken). Therefore, bounce this off to a tasklet.
1119 */
1120static void ub_urb_complete(struct urb *urb, struct pt_regs *pt)
1121{
1122 struct ub_dev *sc = urb->context;
1123
1124 ub_complete(&sc->work_done);
1125 tasklet_schedule(&sc->tasklet);
1126}
1127
1128static void ub_scsi_action(unsigned long _dev)
1129{
1130 struct ub_dev *sc = (struct ub_dev *) _dev;
1131 unsigned long flags;
1132
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001133 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 ub_scsi_dispatch(sc);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001135 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
1138static void ub_scsi_dispatch(struct ub_dev *sc)
1139{
1140 struct ub_scsi_cmd *cmd;
1141 int rc;
1142
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001143 while (!sc->reset && (cmd = ub_cmdq_peek(sc)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (cmd->state == UB_CMDST_DONE) {
1145 ub_cmdq_pop(sc);
1146 (*cmd->done)(sc, cmd);
1147 } else if (cmd->state == UB_CMDST_INIT) {
1148 ub_cmdtr_new(sc, cmd);
1149 if ((rc = ub_scsi_cmd_start(sc, cmd)) == 0)
1150 break;
1151 cmd->error = rc;
1152 cmd->state = UB_CMDST_DONE;
1153 ub_cmdtr_state(sc, cmd);
1154 } else {
1155 if (!ub_is_completed(&sc->work_done))
1156 break;
Pete Zaitcevb31f8212006-01-05 00:14:02 -08001157 del_timer(&sc->work_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 ub_scsi_urb_compl(sc, cmd);
1159 }
1160 }
1161}
1162
1163static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1164{
1165 struct urb *urb = &sc->work_urb;
1166 struct bulk_cs_wrap *bcs;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001167 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 int rc;
1169
1170 if (atomic_read(&sc->poison)) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001171 ub_state_done(sc, cmd, -ENODEV);
1172 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
1174
1175 if (cmd->state == UB_CMDST_CLEAR) {
1176 if (urb->status == -EPIPE) {
1177 /*
1178 * STALL while clearning STALL.
1179 * The control pipe clears itself - nothing to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001181 printk(KERN_NOTICE "%s: stall on control pipe\n",
1182 sc->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 goto Bad_End;
1184 }
1185
1186 /*
1187 * We ignore the result for the halt clear.
1188 */
1189
1190 /* reset the endpoint toggle */
1191 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1192 usb_pipeout(sc->last_pipe), 0);
1193
1194 ub_state_sense(sc, cmd);
1195
1196 } else if (cmd->state == UB_CMDST_CLR2STS) {
1197 if (urb->status == -EPIPE) {
Pete Zaitcevf4800072005-05-01 16:05:40 -07001198 printk(KERN_NOTICE "%s: stall on control pipe\n",
1199 sc->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 goto Bad_End;
1201 }
1202
1203 /*
1204 * We ignore the result for the halt clear.
1205 */
1206
1207 /* reset the endpoint toggle */
1208 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1209 usb_pipeout(sc->last_pipe), 0);
1210
1211 ub_state_stat(sc, cmd);
1212
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001213 } else if (cmd->state == UB_CMDST_CLRRS) {
1214 if (urb->status == -EPIPE) {
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001215 printk(KERN_NOTICE "%s: stall on control pipe\n",
1216 sc->name);
1217 goto Bad_End;
1218 }
1219
1220 /*
1221 * We ignore the result for the halt clear.
1222 */
1223
1224 /* reset the endpoint toggle */
1225 usb_settoggle(sc->dev, usb_pipeendpoint(sc->last_pipe),
1226 usb_pipeout(sc->last_pipe), 0);
1227
1228 ub_state_stat_counted(sc, cmd);
1229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 } else if (cmd->state == UB_CMDST_CMD) {
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001231 switch (urb->status) {
1232 case 0:
1233 break;
1234 case -EOVERFLOW:
1235 goto Bad_End;
1236 case -EPIPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1238 if (rc != 0) {
1239 printk(KERN_NOTICE "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001240 "unable to submit clear (%d)\n",
1241 sc->name, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 /*
1243 * This is typically ENOMEM or some other such shit.
1244 * Retrying is pointless. Just do Bad End on it...
1245 */
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001246 ub_state_done(sc, cmd, rc);
1247 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249 cmd->state = UB_CMDST_CLEAR;
1250 ub_cmdtr_state(sc, cmd);
1251 return;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001252 case -ESHUTDOWN: /* unplug */
1253 case -EILSEQ: /* unplug timeout on uhci */
1254 ub_state_done(sc, cmd, -ENODEV);
1255 return;
1256 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 goto Bad_End;
1258 }
1259 if (urb->actual_length != US_BULK_CB_WRAP_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 goto Bad_End;
1261 }
1262
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001263 if (cmd->dir == UB_DIR_NONE || cmd->nsg < 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 ub_state_stat(sc, cmd);
1265 return;
1266 }
1267
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001268 // udelay(125); // usb-storage has this
1269 ub_data_start(sc, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 } else if (cmd->state == UB_CMDST_DATA) {
1272 if (urb->status == -EPIPE) {
1273 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1274 if (rc != 0) {
1275 printk(KERN_NOTICE "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001276 "unable to submit clear (%d)\n",
1277 sc->name, rc);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001278 ub_state_done(sc, cmd, rc);
1279 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281 cmd->state = UB_CMDST_CLR2STS;
1282 ub_cmdtr_state(sc, cmd);
1283 return;
1284 }
1285 if (urb->status == -EOVERFLOW) {
1286 /*
1287 * A babble? Failure, but we must transfer CSW now.
1288 */
1289 cmd->error = -EOVERFLOW; /* A cheap trick... */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001290 ub_state_stat(sc, cmd);
1291 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001293
1294 if (cmd->dir == UB_DIR_WRITE) {
1295 /*
1296 * Do not continue writes in case of a failure.
1297 * Doing so would cause sectors to be mixed up,
1298 * which is worse than sectors lost.
1299 *
1300 * We must try to read the CSW, or many devices
1301 * get confused.
1302 */
1303 len = urb->actual_length;
1304 if (urb->status != 0 ||
1305 len != cmd->sgv[cmd->current_sg].length) {
1306 cmd->act_len += len;
1307 ub_cmdtr_act_len(sc, cmd);
1308
1309 cmd->error = -EIO;
1310 ub_state_stat(sc, cmd);
1311 return;
1312 }
1313
1314 } else {
1315 /*
1316 * If an error occurs on read, we record it, and
1317 * continue to fetch data in order to avoid bubble.
1318 *
1319 * As a small shortcut, we stop if we detect that
1320 * a CSW mixed into data.
1321 */
1322 if (urb->status != 0)
1323 cmd->error = -EIO;
1324
1325 len = urb->actual_length;
1326 if (urb->status != 0 ||
1327 len != cmd->sgv[cmd->current_sg].length) {
1328 if ((len & 0x1FF) == US_BULK_CS_WRAP_LEN)
1329 goto Bad_End;
1330 }
1331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001333 cmd->act_len += urb->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 ub_cmdtr_act_len(sc, cmd);
1335
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001336 if (++cmd->current_sg < cmd->nsg) {
1337 ub_data_start(sc, cmd);
1338 return;
1339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 ub_state_stat(sc, cmd);
1341
1342 } else if (cmd->state == UB_CMDST_STAT) {
1343 if (urb->status == -EPIPE) {
1344 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1345 if (rc != 0) {
1346 printk(KERN_NOTICE "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001347 "unable to submit clear (%d)\n",
1348 sc->name, rc);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001349 ub_state_done(sc, cmd, rc);
1350 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001352
1353 /*
1354 * Having a stall when getting CSW is an error, so
1355 * make sure uppper levels are not oblivious to it.
1356 */
1357 cmd->error = -EIO; /* A cheap trick... */
1358
1359 cmd->state = UB_CMDST_CLRRS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 ub_cmdtr_state(sc, cmd);
1361 return;
1362 }
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001363
1364 /* Catch everything, including -EOVERFLOW and other nasties. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (urb->status != 0)
1366 goto Bad_End;
1367
1368 if (urb->actual_length == 0) {
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001369 ub_state_stat_counted(sc, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return;
1371 }
1372
1373 /*
1374 * Check the returned Bulk protocol status.
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001375 * The status block has to be validated first.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 */
1377
1378 bcs = &sc->work_bcs;
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001379
1380 if (sc->signature == cpu_to_le32(0)) {
1381 /*
1382 * This is the first reply, so do not perform the check.
1383 * Instead, remember the signature the device uses
1384 * for future checks. But do not allow a nul.
1385 */
1386 sc->signature = bcs->Signature;
1387 if (sc->signature == cpu_to_le32(0)) {
1388 ub_state_stat_counted(sc, cmd);
1389 return;
1390 }
1391 } else {
1392 if (bcs->Signature != sc->signature) {
1393 ub_state_stat_counted(sc, cmd);
1394 return;
1395 }
1396 }
1397
1398 if (bcs->Tag != cmd->tag) {
1399 /*
1400 * This usually happens when we disagree with the
1401 * device's microcode about something. For instance,
1402 * a few of them throw this after timeouts. They buffer
1403 * commands and reply at commands we timed out before.
1404 * Without flushing these replies we loop forever.
1405 */
1406 ub_state_stat_counted(sc, cmd);
1407 return;
1408 }
1409
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001410 len = le32_to_cpu(bcs->Residue);
1411 if (len != cmd->len - cmd->act_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 /*
1413 * It is all right to transfer less, the caller has
1414 * to check. But it's not all right if the device
1415 * counts disagree with our counts.
1416 */
1417 /* P3 */ printk("%s: resid %d len %d act %d\n",
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001418 sc->name, len, cmd->len, cmd->act_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 goto Bad_End;
1420 }
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 switch (bcs->Status) {
1423 case US_BULK_STAT_OK:
1424 break;
1425 case US_BULK_STAT_FAIL:
1426 ub_state_sense(sc, cmd);
1427 return;
1428 case US_BULK_STAT_PHASE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 /* P3 */ printk("%s: status PHASE\n", sc->name);
1430 goto Bad_End;
1431 default:
1432 printk(KERN_INFO "%s: unknown CSW status 0x%x\n",
1433 sc->name, bcs->Status);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001434 ub_state_done(sc, cmd, -EINVAL);
1435 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
1437
1438 /* Not zeroing error to preserve a babble indicator */
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001439 if (cmd->error != 0) {
1440 ub_state_sense(sc, cmd);
1441 return;
1442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 cmd->state = UB_CMDST_DONE;
1444 ub_cmdtr_state(sc, cmd);
1445 ub_cmdq_pop(sc);
1446 (*cmd->done)(sc, cmd);
1447
1448 } else if (cmd->state == UB_CMDST_SENSE) {
1449 ub_state_done(sc, cmd, -EIO);
1450
1451 } else {
1452 printk(KERN_WARNING "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001453 "wrong command state %d\n",
1454 sc->name, cmd->state);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001455 ub_state_done(sc, cmd, -EINVAL);
1456 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
1458 return;
1459
1460Bad_End: /* Little Excel is dead */
1461 ub_state_done(sc, cmd, -EIO);
1462}
1463
1464/*
1465 * Factorization helper for the command state machine:
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001466 * Initiate a data segment transfer.
1467 */
1468static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1469{
1470 struct scatterlist *sg = &cmd->sgv[cmd->current_sg];
1471 int pipe;
1472 int rc;
1473
1474 UB_INIT_COMPLETION(sc->work_done);
1475
1476 if (cmd->dir == UB_DIR_READ)
1477 pipe = sc->recv_bulk_pipe;
1478 else
1479 pipe = sc->send_bulk_pipe;
1480 sc->last_pipe = pipe;
1481 usb_fill_bulk_urb(&sc->work_urb, sc->dev, pipe,
1482 page_address(sg->page) + sg->offset, sg->length,
1483 ub_urb_complete, sc);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001484 sc->work_urb.actual_length = 0;
1485 sc->work_urb.error_count = 0;
1486 sc->work_urb.status = 0;
1487
1488 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1489 /* XXX Clear stalls */
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001490 ub_complete(&sc->work_done);
1491 ub_state_done(sc, cmd, rc);
1492 return;
1493 }
1494
1495 sc->work_timer.expires = jiffies + UB_DATA_TIMEOUT;
1496 add_timer(&sc->work_timer);
1497
1498 cmd->state = UB_CMDST_DATA;
1499 ub_cmdtr_state(sc, cmd);
1500}
1501
1502/*
1503 * Factorization helper for the command state machine:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 * Finish the command.
1505 */
1506static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc)
1507{
1508
1509 cmd->error = rc;
1510 cmd->state = UB_CMDST_DONE;
1511 ub_cmdtr_state(sc, cmd);
1512 ub_cmdq_pop(sc);
1513 (*cmd->done)(sc, cmd);
1514}
1515
1516/*
1517 * Factorization helper for the command state machine:
1518 * Submit a CSW read.
1519 */
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001520static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 int rc;
1523
1524 UB_INIT_COMPLETION(sc->work_done);
1525
1526 sc->last_pipe = sc->recv_bulk_pipe;
1527 usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->recv_bulk_pipe,
1528 &sc->work_bcs, US_BULK_CS_WRAP_LEN, ub_urb_complete, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 sc->work_urb.actual_length = 0;
1530 sc->work_urb.error_count = 0;
1531 sc->work_urb.status = 0;
1532
1533 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1534 /* XXX Clear stalls */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 ub_complete(&sc->work_done);
1536 ub_state_done(sc, cmd, rc);
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001537 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539
1540 sc->work_timer.expires = jiffies + UB_STAT_TIMEOUT;
1541 add_timer(&sc->work_timer);
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543}
1544
1545/*
1546 * Factorization helper for the command state machine:
1547 * Submit a CSW read and go to STAT state.
1548 */
1549static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1550{
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001551
1552 if (__ub_state_stat(sc, cmd) != 0)
1553 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555 cmd->stat_count = 0;
1556 cmd->state = UB_CMDST_STAT;
1557 ub_cmdtr_state(sc, cmd);
1558}
1559
1560/*
1561 * Factorization helper for the command state machine:
Pete Zaitcev1872bce2005-07-27 11:43:51 -07001562 * Submit a CSW read and go to STAT state with counter (along [C] path).
1563 */
1564static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1565{
1566
1567 if (++cmd->stat_count >= 4) {
1568 ub_state_sense(sc, cmd);
1569 return;
1570 }
1571
1572 if (__ub_state_stat(sc, cmd) != 0)
1573 return;
1574
1575 cmd->state = UB_CMDST_STAT;
1576 ub_cmdtr_state(sc, cmd);
1577}
1578
1579/*
1580 * Factorization helper for the command state machine:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 * Submit a REQUEST SENSE and go to SENSE state.
1582 */
1583static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1584{
1585 struct ub_scsi_cmd *scmd;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001586 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 int rc;
1588
1589 if (cmd->cdb[0] == REQUEST_SENSE) {
1590 rc = -EPIPE;
1591 goto error;
1592 }
1593
1594 scmd = &sc->top_rqs_cmd;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001595 memset(scmd, 0, sizeof(struct ub_scsi_cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 scmd->cdb[0] = REQUEST_SENSE;
1597 scmd->cdb[4] = UB_SENSE_SIZE;
1598 scmd->cdb_len = 6;
1599 scmd->dir = UB_DIR_READ;
1600 scmd->state = UB_CMDST_INIT;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001601 scmd->nsg = 1;
1602 sg = &scmd->sgv[0];
1603 sg->page = virt_to_page(sc->top_sense);
Pete Zaitcev38ffdd62005-09-24 13:10:16 -07001604 sg->offset = (unsigned long)sc->top_sense & (PAGE_SIZE-1);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001605 sg->length = UB_SENSE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 scmd->len = UB_SENSE_SIZE;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001607 scmd->lun = cmd->lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 scmd->done = ub_top_sense_done;
1609 scmd->back = cmd;
1610
1611 scmd->tag = sc->tagcnt++;
1612
1613 cmd->state = UB_CMDST_SENSE;
1614 ub_cmdtr_state(sc, cmd);
1615
1616 ub_cmdq_insert(sc, scmd);
1617 return;
1618
1619error:
1620 ub_state_done(sc, cmd, rc);
1621}
1622
1623/*
1624 * A helper for the command's state machine:
1625 * Submit a stall clear.
1626 */
1627static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
1628 int stalled_pipe)
1629{
1630 int endp;
1631 struct usb_ctrlrequest *cr;
1632 int rc;
1633
1634 endp = usb_pipeendpoint(stalled_pipe);
1635 if (usb_pipein (stalled_pipe))
1636 endp |= USB_DIR_IN;
1637
1638 cr = &sc->work_cr;
1639 cr->bRequestType = USB_RECIP_ENDPOINT;
1640 cr->bRequest = USB_REQ_CLEAR_FEATURE;
1641 cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
1642 cr->wIndex = cpu_to_le16(endp);
1643 cr->wLength = cpu_to_le16(0);
1644
1645 UB_INIT_COMPLETION(sc->work_done);
1646
1647 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
1648 (unsigned char*) cr, NULL, 0, ub_urb_complete, sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 sc->work_urb.actual_length = 0;
1650 sc->work_urb.error_count = 0;
1651 sc->work_urb.status = 0;
1652
1653 if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) {
1654 ub_complete(&sc->work_done);
1655 return rc;
1656 }
1657
1658 sc->work_timer.expires = jiffies + UB_CTRL_TIMEOUT;
1659 add_timer(&sc->work_timer);
1660 return 0;
1661}
1662
1663/*
1664 */
1665static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd)
1666{
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07001667 unsigned char *sense = sc->top_sense;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 struct ub_scsi_cmd *cmd;
1669
1670 /*
1671 * Ignoring scmd->act_len, because the buffer was pre-zeroed.
1672 */
1673 ub_cmdtr_sense(sc, scmd, sense);
1674
1675 /*
1676 * Find the command which triggered the unit attention or a check,
1677 * save the sense into it, and advance its state machine.
1678 */
1679 if ((cmd = ub_cmdq_peek(sc)) == NULL) {
1680 printk(KERN_WARNING "%s: sense done while idle\n", sc->name);
1681 return;
1682 }
1683 if (cmd != scmd->back) {
1684 printk(KERN_WARNING "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001685 "sense done for wrong command 0x%x\n",
1686 sc->name, cmd->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return;
1688 }
1689 if (cmd->state != UB_CMDST_SENSE) {
1690 printk(KERN_WARNING "%s: "
Pete Zaitcevf4800072005-05-01 16:05:40 -07001691 "sense done with bad cmd state %d\n",
1692 sc->name, cmd->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return;
1694 }
1695
1696 cmd->key = sense[2] & 0x0F;
1697 cmd->asc = sense[12];
1698 cmd->ascq = sense[13];
1699
1700 ub_scsi_urb_compl(sc, cmd);
1701}
1702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703/*
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001704 * Reset management
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001705 * XXX Move usb_reset_device to khubd. Hogging kevent is not a good thing.
1706 * XXX Make usb_sync_reset asynchronous.
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001707 */
1708
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001709static void ub_reset_enter(struct ub_dev *sc, int try)
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001710{
1711
1712 if (sc->reset) {
1713 /* This happens often on multi-LUN devices. */
1714 return;
1715 }
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001716 sc->reset = try + 1;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001717
1718#if 0 /* Not needed because the disconnect waits for us. */
1719 unsigned long flags;
1720 spin_lock_irqsave(&ub_lock, flags);
1721 sc->openc++;
1722 spin_unlock_irqrestore(&ub_lock, flags);
1723#endif
1724
1725#if 0 /* We let them stop themselves. */
1726 struct list_head *p;
1727 struct ub_lun *lun;
1728 list_for_each(p, &sc->luns) {
1729 lun = list_entry(p, struct ub_lun, link);
1730 blk_stop_queue(lun->disk->queue);
1731 }
1732#endif
1733
1734 schedule_work(&sc->reset_work);
1735}
1736
1737static void ub_reset_task(void *arg)
1738{
1739 struct ub_dev *sc = arg;
1740 unsigned long flags;
1741 struct list_head *p;
1742 struct ub_lun *lun;
1743 int lkr, rc;
1744
1745 if (!sc->reset) {
1746 printk(KERN_WARNING "%s: Running reset unrequested\n",
1747 sc->name);
1748 return;
1749 }
1750
1751 if (atomic_read(&sc->poison)) {
1752 printk(KERN_NOTICE "%s: Not resetting disconnected device\n",
1753 sc->name); /* P3 This floods. Remove soon. XXX */
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08001754 } else if ((sc->reset & 1) == 0) {
1755 ub_sync_reset(sc);
1756 msleep(700); /* usb-storage sleeps 6s (!) */
1757 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
1758 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001759 } else if (sc->dev->actconfig->desc.bNumInterfaces != 1) {
1760 printk(KERN_NOTICE "%s: Not resetting multi-interface device\n",
1761 sc->name); /* P3 This floods. Remove soon. XXX */
1762 } else {
1763 if ((lkr = usb_lock_device_for_reset(sc->dev, sc->intf)) < 0) {
1764 printk(KERN_NOTICE
1765 "%s: usb_lock_device_for_reset failed (%d)\n",
1766 sc->name, lkr);
1767 } else {
1768 rc = usb_reset_device(sc->dev);
1769 if (rc < 0) {
1770 printk(KERN_NOTICE "%s: "
1771 "usb_lock_device_for_reset failed (%d)\n",
1772 sc->name, rc);
1773 }
1774
1775 if (lkr)
1776 usb_unlock_device(sc->dev);
1777 }
1778 }
1779
1780 /*
1781 * In theory, no commands can be running while reset is active,
1782 * so nobody can ask for another reset, and so we do not need any
1783 * queues of resets or anything. We do need a spinlock though,
1784 * to interact with block layer.
1785 */
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001786 spin_lock_irqsave(sc->lock, flags);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001787 sc->reset = 0;
1788 tasklet_schedule(&sc->tasklet);
1789 list_for_each(p, &sc->luns) {
1790 lun = list_entry(p, struct ub_lun, link);
1791 blk_start_queue(lun->disk->queue);
1792 }
1793 wake_up(&sc->reset_wait);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08001794 spin_unlock_irqrestore(sc->lock, flags);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08001795}
1796
1797/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 * This is called from a process context.
1799 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001800static void ub_revalidate(struct ub_dev *sc, struct ub_lun *lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
1802
Pete Zaitcevf4800072005-05-01 16:05:40 -07001803 lun->readonly = 0; /* XXX Query this from the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Pete Zaitcevf4800072005-05-01 16:05:40 -07001805 lun->capacity.nsec = 0;
1806 lun->capacity.bsize = 512;
1807 lun->capacity.bshift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Pete Zaitcevf4800072005-05-01 16:05:40 -07001809 if (ub_sync_tur(sc, lun) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 return; /* Not ready */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001811 lun->changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Pete Zaitcevf4800072005-05-01 16:05:40 -07001813 if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 /*
1815 * The retry here means something is wrong, either with the
1816 * device, with the transport, or with our code.
1817 * We keep this because sd.c has retries for capacity.
1818 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001819 if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) {
1820 lun->capacity.nsec = 0;
1821 lun->capacity.bsize = 512;
1822 lun->capacity.bshift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
1824 }
1825}
1826
1827/*
1828 * The open funcion.
1829 * This is mostly needed to keep refcounting, but also to support
1830 * media checks on removable media drives.
1831 */
1832static int ub_bd_open(struct inode *inode, struct file *filp)
1833{
1834 struct gendisk *disk = inode->i_bdev->bd_disk;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001835 struct ub_lun *lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 struct ub_dev *sc;
1837 unsigned long flags;
1838 int rc;
1839
Pete Zaitcevf4800072005-05-01 16:05:40 -07001840 if ((lun = disk->private_data) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 return -ENXIO;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001842 sc = lun->udev;
1843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 spin_lock_irqsave(&ub_lock, flags);
1845 if (atomic_read(&sc->poison)) {
1846 spin_unlock_irqrestore(&ub_lock, flags);
1847 return -ENXIO;
1848 }
1849 sc->openc++;
1850 spin_unlock_irqrestore(&ub_lock, flags);
1851
1852 /*
1853 * This is a workaround for a specific problem in our block layer.
1854 * In 2.6.9, register_disk duplicates the code from rescan_partitions.
1855 * However, if we do add_disk with a device which persistently reports
1856 * a changed media, add_disk calls register_disk, which does do_open,
1857 * which will call rescan_paritions for changed media. After that,
1858 * register_disk attempts to do it all again and causes double kobject
1859 * registration and a eventually an oops on module removal.
1860 *
1861 * The bottom line is, Al Viro says that we should not allow
1862 * bdev->bd_invalidated to be set when doing add_disk no matter what.
1863 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001864 if (lun->first_open) {
1865 lun->first_open = 0;
1866 if (lun->changed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 rc = -ENOMEDIUM;
1868 goto err_open;
1869 }
1870 }
1871
Pete Zaitcevf4800072005-05-01 16:05:40 -07001872 if (lun->removable || lun->readonly)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 check_disk_change(inode->i_bdev);
1874
1875 /*
1876 * The sd.c considers ->media_present and ->changed not equivalent,
1877 * under some pretty murky conditions (a failure of READ CAPACITY).
1878 * We may need it one day.
1879 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001880 if (lun->removable && lun->changed && !(filp->f_flags & O_NDELAY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 rc = -ENOMEDIUM;
1882 goto err_open;
1883 }
1884
Pete Zaitcevf4800072005-05-01 16:05:40 -07001885 if (lun->readonly && (filp->f_mode & FMODE_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 rc = -EROFS;
1887 goto err_open;
1888 }
1889
1890 return 0;
1891
1892err_open:
1893 ub_put(sc);
1894 return rc;
1895}
1896
1897/*
1898 */
1899static int ub_bd_release(struct inode *inode, struct file *filp)
1900{
1901 struct gendisk *disk = inode->i_bdev->bd_disk;
Pete Zaitcevf4800072005-05-01 16:05:40 -07001902 struct ub_lun *lun = disk->private_data;
1903 struct ub_dev *sc = lun->udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905 ub_put(sc);
1906 return 0;
1907}
1908
1909/*
1910 * The ioctl interface.
1911 */
1912static int ub_bd_ioctl(struct inode *inode, struct file *filp,
1913 unsigned int cmd, unsigned long arg)
1914{
1915 struct gendisk *disk = inode->i_bdev->bd_disk;
1916 void __user *usermem = (void __user *) arg;
1917
1918 return scsi_cmd_ioctl(filp, disk, cmd, usermem);
1919}
1920
1921/*
1922 * This is called once a new disk was seen by the block layer or by ub_probe().
1923 * The main onjective here is to discover the features of the media such as
1924 * the capacity, read-only status, etc. USB storage generally does not
1925 * need to be spun up, but if we needed it, this would be the place.
1926 *
1927 * This call can sleep.
1928 *
1929 * The return code is not used.
1930 */
1931static int ub_bd_revalidate(struct gendisk *disk)
1932{
Pete Zaitcevf4800072005-05-01 16:05:40 -07001933 struct ub_lun *lun = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
Pete Zaitcevf4800072005-05-01 16:05:40 -07001935 ub_revalidate(lun->udev, lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 /* XXX Support sector size switching like in sr.c */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001938 blk_queue_hardsect_size(disk->queue, lun->capacity.bsize);
1939 set_capacity(disk, lun->capacity.nsec);
1940 // set_disk_ro(sdkp->disk, lun->readonly);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942 return 0;
1943}
1944
1945/*
1946 * The check is called by the block layer to verify if the media
1947 * is still available. It is supposed to be harmless, lightweight and
1948 * non-intrusive in case the media was not changed.
1949 *
1950 * This call can sleep.
1951 *
1952 * The return code is bool!
1953 */
1954static int ub_bd_media_changed(struct gendisk *disk)
1955{
Pete Zaitcevf4800072005-05-01 16:05:40 -07001956 struct ub_lun *lun = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Pete Zaitcevf4800072005-05-01 16:05:40 -07001958 if (!lun->removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 return 0;
1960
1961 /*
1962 * We clean checks always after every command, so this is not
1963 * as dangerous as it looks. If the TEST_UNIT_READY fails here,
1964 * the device is actually not ready with operator or software
1965 * intervention required. One dangerous item might be a drive which
1966 * spins itself down, and come the time to write dirty pages, this
1967 * will fail, then block layer discards the data. Since we never
1968 * spin drives up, such devices simply cannot be used with ub anyway.
1969 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001970 if (ub_sync_tur(lun->udev, lun) != 0) {
1971 lun->changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 return 1;
1973 }
1974
Pete Zaitcevf4800072005-05-01 16:05:40 -07001975 return lun->changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
1977
1978static struct block_device_operations ub_bd_fops = {
1979 .owner = THIS_MODULE,
1980 .open = ub_bd_open,
1981 .release = ub_bd_release,
1982 .ioctl = ub_bd_ioctl,
1983 .media_changed = ub_bd_media_changed,
1984 .revalidate_disk = ub_bd_revalidate,
1985};
1986
1987/*
1988 * Common ->done routine for commands executed synchronously.
1989 */
1990static void ub_probe_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1991{
1992 struct completion *cop = cmd->back;
1993 complete(cop);
1994}
1995
1996/*
1997 * Test if the device has a check condition on it, synchronously.
1998 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07001999static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
2001 struct ub_scsi_cmd *cmd;
2002 enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) };
2003 unsigned long flags;
2004 struct completion compl;
2005 int rc;
2006
2007 init_completion(&compl);
2008
2009 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08002010 if ((cmd = kzalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 cmd->cdb[0] = TEST_UNIT_READY;
2014 cmd->cdb_len = 6;
2015 cmd->dir = UB_DIR_NONE;
2016 cmd->state = UB_CMDST_INIT;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002017 cmd->lun = lun; /* This may be NULL, but that's ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 cmd->done = ub_probe_done;
2019 cmd->back = &compl;
2020
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002021 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 cmd->tag = sc->tagcnt++;
2023
2024 rc = ub_submit_scsi(sc, cmd);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002025 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 if (rc != 0) {
2028 printk("ub: testing ready: submit error (%d)\n", rc); /* P3 */
2029 goto err_submit;
2030 }
2031
2032 wait_for_completion(&compl);
2033
2034 rc = cmd->error;
2035
2036 if (rc == -EIO && cmd->key != 0) /* Retries for benh's key */
2037 rc = cmd->key;
2038
2039err_submit:
2040 kfree(cmd);
2041err_alloc:
2042 return rc;
2043}
2044
2045/*
2046 * Read the SCSI capacity synchronously (for probing).
2047 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07002048static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
2049 struct ub_capacity *ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
2051 struct ub_scsi_cmd *cmd;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07002052 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 char *p;
2054 enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) + 8 };
2055 unsigned long flags;
2056 unsigned int bsize, shift;
2057 unsigned long nsec;
2058 struct completion compl;
2059 int rc;
2060
2061 init_completion(&compl);
2062
2063 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08002064 if ((cmd = kzalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 goto err_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 p = (char *)cmd + sizeof(struct ub_scsi_cmd);
2067
2068 cmd->cdb[0] = 0x25;
2069 cmd->cdb_len = 10;
2070 cmd->dir = UB_DIR_READ;
2071 cmd->state = UB_CMDST_INIT;
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07002072 cmd->nsg = 1;
2073 sg = &cmd->sgv[0];
2074 sg->page = virt_to_page(p);
Pete Zaitcev38ffdd62005-09-24 13:10:16 -07002075 sg->offset = (unsigned long)p & (PAGE_SIZE-1);
Pete Zaitceva1cf96e2005-08-14 21:16:03 -07002076 sg->length = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 cmd->len = 8;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002078 cmd->lun = lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 cmd->done = ub_probe_done;
2080 cmd->back = &compl;
2081
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002082 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 cmd->tag = sc->tagcnt++;
2084
2085 rc = ub_submit_scsi(sc, cmd);
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002086 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 if (rc != 0) {
2089 printk("ub: reading capacity: submit error (%d)\n", rc); /* P3 */
2090 goto err_submit;
2091 }
2092
2093 wait_for_completion(&compl);
2094
2095 if (cmd->error != 0) {
2096 printk("ub: reading capacity: error %d\n", cmd->error); /* P3 */
2097 rc = -EIO;
2098 goto err_read;
2099 }
2100 if (cmd->act_len != 8) {
2101 printk("ub: reading capacity: size %d\n", cmd->act_len); /* P3 */
2102 rc = -EIO;
2103 goto err_read;
2104 }
2105
2106 /* sd.c special-cases sector size of 0 to mean 512. Needed? Safe? */
2107 nsec = be32_to_cpu(*(__be32 *)p) + 1;
2108 bsize = be32_to_cpu(*(__be32 *)(p + 4));
2109 switch (bsize) {
2110 case 512: shift = 0; break;
2111 case 1024: shift = 1; break;
2112 case 2048: shift = 2; break;
2113 case 4096: shift = 3; break;
2114 default:
2115 printk("ub: Bad sector size %u\n", bsize); /* P3 */
2116 rc = -EDOM;
2117 goto err_inv_bsize;
2118 }
2119
2120 ret->bsize = bsize;
2121 ret->bshift = shift;
2122 ret->nsec = nsec << shift;
2123 rc = 0;
2124
2125err_inv_bsize:
2126err_read:
2127err_submit:
2128 kfree(cmd);
2129err_alloc:
2130 return rc;
2131}
2132
2133/*
2134 */
2135static void ub_probe_urb_complete(struct urb *urb, struct pt_regs *pt)
2136{
2137 struct completion *cop = urb->context;
2138 complete(cop);
2139}
2140
2141static void ub_probe_timeout(unsigned long arg)
2142{
2143 struct completion *cop = (struct completion *) arg;
2144 complete(cop);
2145}
2146
2147/*
Pete Zaitcev2c2e4a22006-01-05 00:26:30 -08002148 * Reset with a Bulk reset.
2149 */
2150static int ub_sync_reset(struct ub_dev *sc)
2151{
2152 int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
2153 struct usb_ctrlrequest *cr;
2154 struct completion compl;
2155 struct timer_list timer;
2156 int rc;
2157
2158 init_completion(&compl);
2159
2160 cr = &sc->work_cr;
2161 cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
2162 cr->bRequest = US_BULK_RESET_REQUEST;
2163 cr->wValue = cpu_to_le16(0);
2164 cr->wIndex = cpu_to_le16(ifnum);
2165 cr->wLength = cpu_to_le16(0);
2166
2167 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
2168 (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
2169 sc->work_urb.actual_length = 0;
2170 sc->work_urb.error_count = 0;
2171 sc->work_urb.status = 0;
2172
2173 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
2174 printk(KERN_WARNING
2175 "%s: Unable to submit a bulk reset (%d)\n", sc->name, rc);
2176 return rc;
2177 }
2178
2179 init_timer(&timer);
2180 timer.function = ub_probe_timeout;
2181 timer.data = (unsigned long) &compl;
2182 timer.expires = jiffies + UB_CTRL_TIMEOUT;
2183 add_timer(&timer);
2184
2185 wait_for_completion(&compl);
2186
2187 del_timer_sync(&timer);
2188 usb_kill_urb(&sc->work_urb);
2189
2190 return sc->work_urb.status;
2191}
2192
2193/*
Pete Zaitcevf4800072005-05-01 16:05:40 -07002194 * Get number of LUNs by the way of Bulk GetMaxLUN command.
2195 */
2196static int ub_sync_getmaxlun(struct ub_dev *sc)
2197{
2198 int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber;
2199 unsigned char *p;
2200 enum { ALLOC_SIZE = 1 };
2201 struct usb_ctrlrequest *cr;
2202 struct completion compl;
2203 struct timer_list timer;
2204 int nluns;
2205 int rc;
2206
2207 init_completion(&compl);
2208
2209 rc = -ENOMEM;
2210 if ((p = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL)
2211 goto err_alloc;
2212 *p = 55;
2213
2214 cr = &sc->work_cr;
2215 cr->bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
2216 cr->bRequest = US_BULK_GET_MAX_LUN;
2217 cr->wValue = cpu_to_le16(0);
2218 cr->wIndex = cpu_to_le16(ifnum);
2219 cr->wLength = cpu_to_le16(1);
2220
2221 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->recv_ctrl_pipe,
2222 (unsigned char*) cr, p, 1, ub_probe_urb_complete, &compl);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002223 sc->work_urb.actual_length = 0;
2224 sc->work_urb.error_count = 0;
2225 sc->work_urb.status = 0;
2226
2227 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
2228 if (rc == -EPIPE) {
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002229 printk("%s: Stall submitting GetMaxLUN, using 1 LUN\n",
Pete Zaitcevf4800072005-05-01 16:05:40 -07002230 sc->name); /* P3 */
2231 } else {
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002232 printk(KERN_NOTICE
Pete Zaitcevf4800072005-05-01 16:05:40 -07002233 "%s: Unable to submit GetMaxLUN (%d)\n",
2234 sc->name, rc);
2235 }
2236 goto err_submit;
2237 }
2238
2239 init_timer(&timer);
2240 timer.function = ub_probe_timeout;
2241 timer.data = (unsigned long) &compl;
2242 timer.expires = jiffies + UB_CTRL_TIMEOUT;
2243 add_timer(&timer);
2244
2245 wait_for_completion(&compl);
2246
2247 del_timer_sync(&timer);
2248 usb_kill_urb(&sc->work_urb);
2249
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002250 if ((rc = sc->work_urb.status) < 0) {
2251 if (rc == -EPIPE) {
2252 printk("%s: Stall at GetMaxLUN, using 1 LUN\n",
2253 sc->name); /* P3 */
2254 } else {
2255 printk(KERN_NOTICE
2256 "%s: Error at GetMaxLUN (%d)\n",
2257 sc->name, rc);
2258 }
2259 goto err_io;
2260 }
2261
Pete Zaitcevf4800072005-05-01 16:05:40 -07002262 if (sc->work_urb.actual_length != 1) {
2263 printk("%s: GetMaxLUN returned %d bytes\n", sc->name,
2264 sc->work_urb.actual_length); /* P3 */
2265 nluns = 0;
2266 } else {
2267 if ((nluns = *p) == 55) {
2268 nluns = 0;
2269 } else {
2270 /* GetMaxLUN returns the maximum LUN number */
2271 nluns += 1;
2272 if (nluns > UB_MAX_LUNS)
2273 nluns = UB_MAX_LUNS;
2274 }
2275 printk("%s: GetMaxLUN returned %d, using %d LUNs\n", sc->name,
2276 *p, nluns); /* P3 */
2277 }
2278
2279 kfree(p);
2280 return nluns;
2281
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002282err_io:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002283err_submit:
2284 kfree(p);
2285err_alloc:
2286 return rc;
2287}
2288
2289/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 * Clear initial stalls.
2291 */
2292static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe)
2293{
2294 int endp;
2295 struct usb_ctrlrequest *cr;
2296 struct completion compl;
2297 struct timer_list timer;
2298 int rc;
2299
2300 init_completion(&compl);
2301
2302 endp = usb_pipeendpoint(stalled_pipe);
2303 if (usb_pipein (stalled_pipe))
2304 endp |= USB_DIR_IN;
2305
2306 cr = &sc->work_cr;
2307 cr->bRequestType = USB_RECIP_ENDPOINT;
2308 cr->bRequest = USB_REQ_CLEAR_FEATURE;
2309 cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT);
2310 cr->wIndex = cpu_to_le16(endp);
2311 cr->wLength = cpu_to_le16(0);
2312
2313 usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe,
2314 (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 sc->work_urb.actual_length = 0;
2316 sc->work_urb.error_count = 0;
2317 sc->work_urb.status = 0;
2318
2319 if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) {
2320 printk(KERN_WARNING
2321 "%s: Unable to submit a probe clear (%d)\n", sc->name, rc);
2322 return rc;
2323 }
2324
2325 init_timer(&timer);
2326 timer.function = ub_probe_timeout;
2327 timer.data = (unsigned long) &compl;
2328 timer.expires = jiffies + UB_CTRL_TIMEOUT;
2329 add_timer(&timer);
2330
2331 wait_for_completion(&compl);
2332
2333 del_timer_sync(&timer);
2334 usb_kill_urb(&sc->work_urb);
2335
2336 /* reset the endpoint toggle */
2337 usb_settoggle(sc->dev, endp, usb_pipeout(sc->last_pipe), 0);
2338
2339 return 0;
2340}
2341
2342/*
2343 * Get the pipe settings.
2344 */
2345static int ub_get_pipes(struct ub_dev *sc, struct usb_device *dev,
2346 struct usb_interface *intf)
2347{
2348 struct usb_host_interface *altsetting = intf->cur_altsetting;
2349 struct usb_endpoint_descriptor *ep_in = NULL;
2350 struct usb_endpoint_descriptor *ep_out = NULL;
2351 struct usb_endpoint_descriptor *ep;
2352 int i;
2353
2354 /*
2355 * Find the endpoints we need.
2356 * We are expecting a minimum of 2 endpoints - in and out (bulk).
2357 * We will ignore any others.
2358 */
2359 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
2360 ep = &altsetting->endpoint[i].desc;
2361
2362 /* Is it a BULK endpoint? */
2363 if ((ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
2364 == USB_ENDPOINT_XFER_BULK) {
2365 /* BULK in or out? */
2366 if (ep->bEndpointAddress & USB_DIR_IN)
2367 ep_in = ep;
2368 else
2369 ep_out = ep;
2370 }
2371 }
2372
2373 if (ep_in == NULL || ep_out == NULL) {
Pete Zaitcevf4800072005-05-01 16:05:40 -07002374 printk(KERN_NOTICE "%s: failed endpoint check\n",
2375 sc->name);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002376 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 }
2378
2379 /* Calculate and store the pipe values */
2380 sc->send_ctrl_pipe = usb_sndctrlpipe(dev, 0);
2381 sc->recv_ctrl_pipe = usb_rcvctrlpipe(dev, 0);
2382 sc->send_bulk_pipe = usb_sndbulkpipe(dev,
2383 ep_out->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2384 sc->recv_bulk_pipe = usb_rcvbulkpipe(dev,
2385 ep_in->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
2386
2387 return 0;
2388}
2389
2390/*
2391 * Probing is done in the process context, which allows us to cheat
2392 * and not to build a state machine for the discovery.
2393 */
2394static int ub_probe(struct usb_interface *intf,
2395 const struct usb_device_id *dev_id)
2396{
2397 struct ub_dev *sc;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002398 int nluns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 int rc;
2400 int i;
2401
Pete Zaitceva00828e2005-10-22 20:15:09 -07002402 if (usb_usual_check_type(dev_id, USB_US_TYPE_UB))
2403 return -ENXIO;
2404
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08002406 if ((sc = kzalloc(sizeof(struct ub_dev), GFP_KERNEL)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 goto err_core;
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002408 sc->lock = ub_next_lock();
Pete Zaitcevf4800072005-05-01 16:05:40 -07002409 INIT_LIST_HEAD(&sc->luns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 usb_init_urb(&sc->work_urb);
2411 tasklet_init(&sc->tasklet, ub_scsi_action, (unsigned long)sc);
2412 atomic_set(&sc->poison, 0);
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002413 INIT_WORK(&sc->reset_work, ub_reset_task, sc);
2414 init_waitqueue_head(&sc->reset_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
2416 init_timer(&sc->work_timer);
2417 sc->work_timer.data = (unsigned long) sc;
2418 sc->work_timer.function = ub_urb_timeout;
2419
2420 ub_init_completion(&sc->work_done);
2421 sc->work_done.done = 1; /* A little yuk, but oh well... */
2422
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 sc->dev = interface_to_usbdev(intf);
2424 sc->intf = intf;
2425 // sc->ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 usb_set_intfdata(intf, sc);
2427 usb_get_dev(sc->dev);
2428 // usb_get_intf(sc->intf); /* Do we need this? */
2429
Pete Zaitcevf4800072005-05-01 16:05:40 -07002430 snprintf(sc->name, 12, DRV_NAME "(%d.%d)",
2431 sc->dev->bus->busnum, sc->dev->devnum);
2432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 /* XXX Verify that we can handle the device (from descriptors) */
2434
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002435 if (ub_get_pipes(sc, sc->dev, intf) != 0)
2436 goto err_dev_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
2438 if (device_create_file(&sc->intf->dev, &dev_attr_diag) != 0)
2439 goto err_diag;
2440
2441 /*
2442 * At this point, all USB initialization is done, do upper layer.
2443 * We really hate halfway initialized structures, so from the
2444 * invariants perspective, this ub_dev is fully constructed at
2445 * this point.
2446 */
2447
2448 /*
2449 * This is needed to clear toggles. It is a problem only if we do
2450 * `rmmod ub && modprobe ub` without disconnects, but we like that.
2451 */
Pete Zaitcevc6c88832005-09-22 00:49:45 -07002452#if 0 /* iPod Mini fails if we do this (big white iPod works) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
2454 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
Pete Zaitcevc6c88832005-09-22 00:49:45 -07002455#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
2457 /*
2458 * The way this is used by the startup code is a little specific.
2459 * A SCSI check causes a USB stall. Our common case code sees it
2460 * and clears the check, after which the device is ready for use.
2461 * But if a check was not present, any command other than
2462 * TEST_UNIT_READY ends with a lockup (including REQUEST_SENSE).
2463 *
2464 * If we neglect to clear the SCSI check, the first real command fails
2465 * (which is the capacity readout). We clear that and retry, but why
2466 * causing spurious retries for no reason.
2467 *
2468 * Revalidation may start with its own TEST_UNIT_READY, but that one
2469 * has to succeed, so we clear checks with an additional one here.
2470 * In any case it's not our business how revaliadation is implemented.
2471 */
2472 for (i = 0; i < 3; i++) { /* Retries for benh's key */
Pete Zaitcevf4800072005-05-01 16:05:40 -07002473 if ((rc = ub_sync_tur(sc, NULL)) <= 0) break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 if (rc != 0x6) break;
2475 msleep(10);
2476 }
2477
Pete Zaitcevf4800072005-05-01 16:05:40 -07002478 nluns = 1;
2479 for (i = 0; i < 3; i++) {
2480 if ((rc = ub_sync_getmaxlun(sc)) < 0) {
2481 /*
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002482 * This segment is taken from usb-storage. They say
2483 * that ZIP-100 needs this, but my own ZIP-100 works
2484 * fine without this.
2485 * Still, it does not seem to hurt anything.
Pete Zaitcevf4800072005-05-01 16:05:40 -07002486 */
2487 if (rc == -EPIPE) {
2488 ub_probe_clear_stall(sc, sc->recv_bulk_pipe);
2489 ub_probe_clear_stall(sc, sc->send_bulk_pipe);
2490 }
2491 break;
2492 }
2493 if (rc != 0) {
2494 nluns = rc;
2495 break;
2496 }
Pete Zaitcev9f793d22005-06-06 13:54:59 -07002497 msleep(100);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Pete Zaitcevf4800072005-05-01 16:05:40 -07002500 for (i = 0; i < nluns; i++) {
2501 ub_probe_lun(sc, i);
2502 }
2503 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
Pete Zaitcevf4800072005-05-01 16:05:40 -07002505 /* device_remove_file(&sc->intf->dev, &dev_attr_diag); */
2506err_diag:
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002507err_dev_desc:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002508 usb_set_intfdata(intf, NULL);
2509 // usb_put_intf(sc->intf);
2510 usb_put_dev(sc->dev);
2511 kfree(sc);
2512err_core:
2513 return rc;
2514}
2515
2516static int ub_probe_lun(struct ub_dev *sc, int lnum)
2517{
2518 struct ub_lun *lun;
2519 request_queue_t *q;
2520 struct gendisk *disk;
2521 int rc;
2522
2523 rc = -ENOMEM;
Pete Zaitcev29da7932006-02-13 20:35:57 -08002524 if ((lun = kzalloc(sizeof(struct ub_lun), GFP_KERNEL)) == NULL)
Pete Zaitcevf4800072005-05-01 16:05:40 -07002525 goto err_alloc;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002526 lun->num = lnum;
2527
2528 rc = -ENOSR;
2529 if ((lun->id = ub_id_get()) == -1)
2530 goto err_id;
2531
2532 lun->udev = sc;
2533 list_add(&lun->link, &sc->luns);
2534
2535 snprintf(lun->name, 16, DRV_NAME "%c(%d.%d.%d)",
2536 lun->id + 'a', sc->dev->bus->busnum, sc->dev->devnum, lun->num);
2537
2538 lun->removable = 1; /* XXX Query this from the device */
2539 lun->changed = 1; /* ub_revalidate clears only */
2540 lun->first_open = 1;
2541 ub_revalidate(sc, lun);
2542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 rc = -ENOMEM;
Pete Zaitcev4fb729f2005-12-17 02:34:12 -08002544 if ((disk = alloc_disk(UB_PARTS_PER_LUN)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 goto err_diskalloc;
2546
Pete Zaitcevf4800072005-05-01 16:05:40 -07002547 lun->disk = disk;
2548 sprintf(disk->disk_name, DRV_NAME "%c", lun->id + 'a');
2549 sprintf(disk->devfs_name, DEVFS_NAME "/%c", lun->id + 'a');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 disk->major = UB_MAJOR;
Pete Zaitcev4fb729f2005-12-17 02:34:12 -08002551 disk->first_minor = lun->id * UB_PARTS_PER_LUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 disk->fops = &ub_bd_fops;
Pete Zaitcevf4800072005-05-01 16:05:40 -07002553 disk->private_data = lun;
Pete Zaitcev64bd8452005-09-22 00:48:29 -07002554 disk->driverfs_dev = &sc->intf->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
2556 rc = -ENOMEM;
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002557 if ((q = blk_init_queue(ub_request_fn, sc->lock)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 goto err_blkqinit;
2559
2560 disk->queue = q;
2561
Pete Zaitcevf4800072005-05-01 16:05:40 -07002562 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 blk_queue_max_hw_segments(q, UB_MAX_REQ_SG);
2564 blk_queue_max_phys_segments(q, UB_MAX_REQ_SG);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002565 blk_queue_segment_boundary(q, 0xffffffff); /* Dubious. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 blk_queue_max_sectors(q, UB_MAX_SECTORS);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002567 blk_queue_hardsect_size(q, lun->capacity.bsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Pete Zaitcevf4800072005-05-01 16:05:40 -07002569 q->queuedata = lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Pete Zaitcevf4800072005-05-01 16:05:40 -07002571 set_capacity(disk, lun->capacity.nsec);
2572 if (lun->removable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 disk->flags |= GENHD_FL_REMOVABLE;
2574
2575 add_disk(disk);
2576
2577 return 0;
2578
2579err_blkqinit:
2580 put_disk(disk);
2581err_diskalloc:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002582 list_del(&lun->link);
2583 ub_id_put(lun->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584err_id:
Pete Zaitcevf4800072005-05-01 16:05:40 -07002585 kfree(lun);
2586err_alloc:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 return rc;
2588}
2589
2590static void ub_disconnect(struct usb_interface *intf)
2591{
2592 struct ub_dev *sc = usb_get_intfdata(intf);
Pete Zaitcevf4800072005-05-01 16:05:40 -07002593 struct list_head *p;
2594 struct ub_lun *lun;
2595 struct gendisk *disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 unsigned long flags;
2597
2598 /*
2599 * Prevent ub_bd_release from pulling the rug from under us.
2600 * XXX This is starting to look like a kref.
2601 * XXX Why not to take this ref at probe time?
2602 */
2603 spin_lock_irqsave(&ub_lock, flags);
2604 sc->openc++;
2605 spin_unlock_irqrestore(&ub_lock, flags);
2606
2607 /*
2608 * Fence stall clearnings, operations triggered by unlinkings and so on.
2609 * We do not attempt to unlink any URBs, because we do not trust the
2610 * unlink paths in HC drivers. Also, we get -84 upon disconnect anyway.
2611 */
2612 atomic_set(&sc->poison, 1);
2613
2614 /*
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002615 * Wait for reset to end, if any.
2616 */
2617 wait_event(sc->reset_wait, !sc->reset);
2618
2619 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 * Blow away queued commands.
2621 *
2622 * Actually, this never works, because before we get here
2623 * the HCD terminates outstanding URB(s). It causes our
2624 * SCSI command queue to advance, commands fail to submit,
2625 * and the whole queue drains. So, we just use this code to
2626 * print warnings.
2627 */
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002628 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 {
2630 struct ub_scsi_cmd *cmd;
2631 int cnt = 0;
Pete Zaitcev2c26c9e2005-12-17 02:16:43 -08002632 while ((cmd = ub_cmdq_peek(sc)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 cmd->error = -ENOTCONN;
2634 cmd->state = UB_CMDST_DONE;
2635 ub_cmdtr_state(sc, cmd);
2636 ub_cmdq_pop(sc);
2637 (*cmd->done)(sc, cmd);
2638 cnt++;
2639 }
2640 if (cnt != 0) {
2641 printk(KERN_WARNING "%s: "
2642 "%d was queued after shutdown\n", sc->name, cnt);
2643 }
2644 }
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002645 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
2647 /*
2648 * Unregister the upper layer.
2649 */
Pete Zaitcevf4800072005-05-01 16:05:40 -07002650 list_for_each (p, &sc->luns) {
2651 lun = list_entry(p, struct ub_lun, link);
2652 disk = lun->disk;
2653 if (disk->flags & GENHD_FL_UP)
2654 del_gendisk(disk);
2655 /*
2656 * I wish I could do:
2657 * set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
2658 * As it is, we rely on our internal poisoning and let
2659 * the upper levels to spin furiously failing all the I/O.
2660 */
2661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
2663 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 * Testing for -EINPROGRESS is always a bug, so we are bending
2665 * the rules a little.
2666 */
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002667 spin_lock_irqsave(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 if (sc->work_urb.status == -EINPROGRESS) { /* janitors: ignore */
2669 printk(KERN_WARNING "%s: "
2670 "URB is active after disconnect\n", sc->name);
2671 }
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002672 spin_unlock_irqrestore(sc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
2674 /*
2675 * There is virtually no chance that other CPU runs times so long
2676 * after ub_urb_complete should have called del_timer, but only if HCD
2677 * didn't forget to deliver a callback on unlink.
2678 */
2679 del_timer_sync(&sc->work_timer);
2680
2681 /*
2682 * At this point there must be no commands coming from anyone
2683 * and no URBs left in transit.
2684 */
2685
2686 device_remove_file(&sc->intf->dev, &dev_attr_diag);
2687 usb_set_intfdata(intf, NULL);
2688 // usb_put_intf(sc->intf);
2689 sc->intf = NULL;
2690 usb_put_dev(sc->dev);
2691 sc->dev = NULL;
2692
2693 ub_put(sc);
2694}
2695
2696static struct usb_driver ub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 .name = "ub",
2698 .probe = ub_probe,
2699 .disconnect = ub_disconnect,
2700 .id_table = ub_usb_ids,
2701};
2702
2703static int __init ub_init(void)
2704{
2705 int rc;
Pete Zaitcev65b4fe52005-12-28 14:22:17 -08002706 int i;
2707
2708 for (i = 0; i < UB_QLOCK_NUM; i++)
2709 spin_lock_init(&ub_qlockv[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 if ((rc = register_blkdev(UB_MAJOR, DRV_NAME)) != 0)
2712 goto err_regblkdev;
2713 devfs_mk_dir(DEVFS_NAME);
2714
2715 if ((rc = usb_register(&ub_driver)) != 0)
2716 goto err_register;
2717
Pete Zaitceva00828e2005-10-22 20:15:09 -07002718 usb_usual_set_present(USB_US_TYPE_UB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 return 0;
2720
2721err_register:
2722 devfs_remove(DEVFS_NAME);
2723 unregister_blkdev(UB_MAJOR, DRV_NAME);
2724err_regblkdev:
2725 return rc;
2726}
2727
2728static void __exit ub_exit(void)
2729{
2730 usb_deregister(&ub_driver);
2731
2732 devfs_remove(DEVFS_NAME);
2733 unregister_blkdev(UB_MAJOR, DRV_NAME);
Pete Zaitceva00828e2005-10-22 20:15:09 -07002734 usb_usual_clear_present(USB_US_TYPE_UB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735}
2736
2737module_init(ub_init);
2738module_exit(ub_exit);
2739
2740MODULE_LICENSE("GPL");