Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Zaitcev | ef45cb6 | 2008-04-08 17:41:51 -0700 | [diff] [blame] | 11 | * -- Return sense now that rq allows it (we always auto-sense anyway). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * -- 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * -- verify the 13 conditions and do bulk resets |
Pete Zaitcev | ba6abf1 | 2005-07-30 22:38:30 -0700 | [diff] [blame] | 15 | * -- highmem |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | * -- move top_sense and work_bcs into separate allocations (if they survive) |
| 17 | * for cache purists and esoteric architectures. |
Pete Zaitcev | ba6abf1 | 2005-07-30 22:38:30 -0700 | [diff] [blame] | 18 | * -- Allocate structure for LUN 0 before the first ub_sync_tur, avoid NULL. ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * -- prune comments, they are too volumnous |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | * -- Resove XXX's |
Pete Zaitcev | 1872bce | 2005-07-27 11:43:51 -0700 | [diff] [blame] | 21 | * -- CLEAR, CLR2STS, CLRRS seem to be ripe for refactoring. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | */ |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/usb.h> |
Pete Zaitcev | a00828e | 2005-10-22 20:15:09 -0700 | [diff] [blame] | 26 | #include <linux/usb_usual.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/timer.h> |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 29 | #include <linux/scatterlist.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 31 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <scsi/scsi.h> |
| 33 | |
| 34 | #define DRV_NAME "ub" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | #define UB_MAJOR 180 |
| 37 | |
| 38 | /* |
Pete Zaitcev | 1872bce | 2005-07-27 11:43:51 -0700 | [diff] [blame] | 39 | * The command state machine is the key model for understanding of this driver. |
| 40 | * |
| 41 | * The general rule is that all transitions are done towards the bottom |
| 42 | * of the diagram, thus preventing any loops. |
| 43 | * |
| 44 | * An exception to that is how the STAT state is handled. A counter allows it |
| 45 | * to be re-entered along the path marked with [C]. |
| 46 | * |
| 47 | * +--------+ |
| 48 | * ! INIT ! |
| 49 | * +--------+ |
| 50 | * ! |
| 51 | * ub_scsi_cmd_start fails ->--------------------------------------\ |
| 52 | * ! ! |
| 53 | * V ! |
| 54 | * +--------+ ! |
| 55 | * ! CMD ! ! |
| 56 | * +--------+ ! |
| 57 | * ! +--------+ ! |
| 58 | * was -EPIPE -->-------------------------------->! CLEAR ! ! |
| 59 | * ! +--------+ ! |
| 60 | * ! ! ! |
| 61 | * was error -->------------------------------------- ! --------->\ |
| 62 | * ! ! ! |
| 63 | * /--<-- cmd->dir == NONE ? ! ! |
| 64 | * ! ! ! ! |
| 65 | * ! V ! ! |
| 66 | * ! +--------+ ! ! |
| 67 | * ! ! DATA ! ! ! |
| 68 | * ! +--------+ ! ! |
| 69 | * ! ! +---------+ ! ! |
| 70 | * ! was -EPIPE -->--------------->! CLR2STS ! ! ! |
| 71 | * ! ! +---------+ ! ! |
| 72 | * ! ! ! ! ! |
| 73 | * ! ! was error -->---- ! --------->\ |
| 74 | * ! was error -->--------------------- ! ------------- ! --------->\ |
| 75 | * ! ! ! ! ! |
| 76 | * ! V ! ! ! |
| 77 | * \--->+--------+ ! ! ! |
| 78 | * ! STAT !<--------------------------/ ! ! |
| 79 | * /--->+--------+ ! ! |
| 80 | * ! ! ! ! |
| 81 | * [C] was -EPIPE -->-----------\ ! ! |
| 82 | * ! ! ! ! ! |
| 83 | * +<---- len == 0 ! ! ! |
| 84 | * ! ! ! ! ! |
| 85 | * ! was error -->--------------------------------------!---------->\ |
| 86 | * ! ! ! ! ! |
| 87 | * +<---- bad CSW ! ! ! |
| 88 | * +<---- bad tag ! ! ! |
| 89 | * ! ! V ! ! |
| 90 | * ! ! +--------+ ! ! |
| 91 | * ! ! ! CLRRS ! ! ! |
| 92 | * ! ! +--------+ ! ! |
| 93 | * ! ! ! ! ! |
| 94 | * \------- ! --------------------[C]--------\ ! ! |
| 95 | * ! ! ! ! |
| 96 | * cmd->error---\ +--------+ ! ! |
| 97 | * ! +--------------->! SENSE !<----------/ ! |
| 98 | * STAT_FAIL----/ +--------+ ! |
| 99 | * ! ! V |
| 100 | * ! V +--------+ |
| 101 | * \--------------------------------\--------------------->! DONE ! |
| 102 | * +--------+ |
| 103 | */ |
| 104 | |
| 105 | /* |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 106 | * This many LUNs per USB device. |
| 107 | * Every one of them takes a host, see UB_MAX_HOSTS. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | */ |
Pete Zaitcev | 9f793d2 | 2005-06-06 13:54:59 -0700 | [diff] [blame] | 109 | #define UB_MAX_LUNS 9 |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 110 | |
| 111 | /* |
| 112 | */ |
| 113 | |
Pete Zaitcev | 4fb729f | 2005-12-17 02:34:12 -0800 | [diff] [blame] | 114 | #define UB_PARTS_PER_LUN 8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | #define UB_MAX_CDB_SIZE 16 /* Corresponds to Bulk */ |
| 117 | |
| 118 | #define UB_SENSE_SIZE 18 |
| 119 | |
| 120 | /* |
| 121 | */ |
| 122 | |
| 123 | /* command block wrapper */ |
| 124 | struct bulk_cb_wrap { |
| 125 | __le32 Signature; /* contains 'USBC' */ |
| 126 | u32 Tag; /* unique per command id */ |
| 127 | __le32 DataTransferLength; /* size of data */ |
| 128 | u8 Flags; /* direction in bit 0 */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 129 | u8 Lun; /* LUN */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | u8 Length; /* of of the CDB */ |
| 131 | u8 CDB[UB_MAX_CDB_SIZE]; /* max command */ |
| 132 | }; |
| 133 | |
| 134 | #define US_BULK_CB_WRAP_LEN 31 |
| 135 | #define US_BULK_CB_SIGN 0x43425355 /*spells out USBC */ |
| 136 | #define US_BULK_FLAG_IN 1 |
| 137 | #define US_BULK_FLAG_OUT 0 |
| 138 | |
| 139 | /* command status wrapper */ |
| 140 | struct bulk_cs_wrap { |
| 141 | __le32 Signature; /* should = 'USBS' */ |
| 142 | u32 Tag; /* same as original command */ |
| 143 | __le32 Residue; /* amount not transferred */ |
| 144 | u8 Status; /* see below */ |
| 145 | }; |
| 146 | |
| 147 | #define US_BULK_CS_WRAP_LEN 13 |
| 148 | #define US_BULK_CS_SIGN 0x53425355 /* spells out 'USBS' */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | #define US_BULK_STAT_OK 0 |
| 150 | #define US_BULK_STAT_FAIL 1 |
| 151 | #define US_BULK_STAT_PHASE 2 |
| 152 | |
| 153 | /* bulk-only class specific requests */ |
| 154 | #define US_BULK_RESET_REQUEST 0xff |
| 155 | #define US_BULK_GET_MAX_LUN 0xfe |
| 156 | |
| 157 | /* |
| 158 | */ |
| 159 | struct ub_dev; |
| 160 | |
Pete Zaitcev | 64bd845 | 2005-09-22 00:48:29 -0700 | [diff] [blame] | 161 | #define UB_MAX_REQ_SG 9 /* cdrecord requires 32KB and maybe a header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | #define UB_MAX_SECTORS 64 |
| 163 | |
| 164 | /* |
| 165 | * A second is more than enough for a 32K transfer (UB_MAX_SECTORS) |
| 166 | * even if a webcam hogs the bus, but some devices need time to spin up. |
| 167 | */ |
| 168 | #define UB_URB_TIMEOUT (HZ*2) |
| 169 | #define UB_DATA_TIMEOUT (HZ*5) /* ZIP does spin-ups in the data phase */ |
| 170 | #define UB_STAT_TIMEOUT (HZ*5) /* Same spinups and eject for a dataless cmd. */ |
| 171 | #define UB_CTRL_TIMEOUT (HZ/2) /* 500ms ought to be enough to clear a stall */ |
| 172 | |
| 173 | /* |
| 174 | * An instance of a SCSI command in transit. |
| 175 | */ |
| 176 | #define UB_DIR_NONE 0 |
| 177 | #define UB_DIR_READ 1 |
| 178 | #define UB_DIR_ILLEGAL2 2 |
| 179 | #define UB_DIR_WRITE 3 |
| 180 | |
| 181 | #define UB_DIR_CHAR(c) (((c)==UB_DIR_WRITE)? 'w': \ |
| 182 | (((c)==UB_DIR_READ)? 'r': 'n')) |
| 183 | |
| 184 | enum ub_scsi_cmd_state { |
| 185 | UB_CMDST_INIT, /* Initial state */ |
| 186 | UB_CMDST_CMD, /* Command submitted */ |
| 187 | UB_CMDST_DATA, /* Data phase */ |
| 188 | UB_CMDST_CLR2STS, /* Clearing before requesting status */ |
| 189 | UB_CMDST_STAT, /* Status phase */ |
| 190 | UB_CMDST_CLEAR, /* Clearing a stall (halt, actually) */ |
Pete Zaitcev | 1872bce | 2005-07-27 11:43:51 -0700 | [diff] [blame] | 191 | UB_CMDST_CLRRS, /* Clearing before retrying status */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | UB_CMDST_SENSE, /* Sending Request Sense */ |
| 193 | UB_CMDST_DONE /* Final state */ |
| 194 | }; |
| 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | struct ub_scsi_cmd { |
| 197 | unsigned char cdb[UB_MAX_CDB_SIZE]; |
| 198 | unsigned char cdb_len; |
| 199 | |
| 200 | unsigned char dir; /* 0 - none, 1 - read, 3 - write. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | enum ub_scsi_cmd_state state; |
| 202 | unsigned int tag; |
| 203 | struct ub_scsi_cmd *next; |
| 204 | |
| 205 | int error; /* Return code - valid upon done */ |
| 206 | unsigned int act_len; /* Return size */ |
| 207 | unsigned char key, asc, ascq; /* May be valid if error==-EIO */ |
| 208 | |
| 209 | int stat_count; /* Retries getting status. */ |
Pete Zaitcev | 2c51ae7 | 2008-04-19 14:32:18 -0700 | [diff] [blame] | 210 | unsigned int timeo; /* jiffies until rq->timeout changes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | unsigned int len; /* Requested length */ |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 213 | unsigned int current_sg; |
| 214 | unsigned int nsg; /* sgv[nsg] */ |
| 215 | struct scatterlist sgv[UB_MAX_REQ_SG]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 217 | struct ub_lun *lun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | void (*done)(struct ub_dev *, struct ub_scsi_cmd *); |
| 219 | void *back; |
| 220 | }; |
| 221 | |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 222 | struct ub_request { |
| 223 | struct request *rq; |
| 224 | unsigned int current_try; |
| 225 | unsigned int nsg; /* sgv[nsg] */ |
| 226 | struct scatterlist sgv[UB_MAX_REQ_SG]; |
| 227 | }; |
| 228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | /* |
| 230 | */ |
| 231 | struct ub_capacity { |
| 232 | unsigned long nsec; /* Linux size - 512 byte sectors */ |
| 233 | unsigned int bsize; /* Linux hardsect_size */ |
| 234 | unsigned int bshift; /* Shift between 512 and hard sects */ |
| 235 | }; |
| 236 | |
| 237 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | * This is a direct take-off from linux/include/completion.h |
| 239 | * The difference is that I do not wait on this thing, just poll. |
| 240 | * When I want to wait (ub_probe), I just use the stock completion. |
| 241 | * |
| 242 | * Note that INIT_COMPLETION takes no lock. It is correct. But why |
| 243 | * in the bloody hell that thing takes struct instead of pointer to struct |
| 244 | * is quite beyond me. I just copied it from the stock completion. |
| 245 | */ |
| 246 | struct ub_completion { |
| 247 | unsigned int done; |
| 248 | spinlock_t lock; |
| 249 | }; |
| 250 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 251 | static DEFINE_MUTEX(ub_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | static inline void ub_init_completion(struct ub_completion *x) |
| 253 | { |
| 254 | x->done = 0; |
| 255 | spin_lock_init(&x->lock); |
| 256 | } |
| 257 | |
| 258 | #define UB_INIT_COMPLETION(x) ((x).done = 0) |
| 259 | |
| 260 | static void ub_complete(struct ub_completion *x) |
| 261 | { |
| 262 | unsigned long flags; |
| 263 | |
| 264 | spin_lock_irqsave(&x->lock, flags); |
| 265 | x->done++; |
| 266 | spin_unlock_irqrestore(&x->lock, flags); |
| 267 | } |
| 268 | |
| 269 | static int ub_is_completed(struct ub_completion *x) |
| 270 | { |
| 271 | unsigned long flags; |
| 272 | int ret; |
| 273 | |
| 274 | spin_lock_irqsave(&x->lock, flags); |
| 275 | ret = x->done; |
| 276 | spin_unlock_irqrestore(&x->lock, flags); |
| 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | */ |
| 282 | struct ub_scsi_cmd_queue { |
| 283 | int qlen, qmax; |
| 284 | struct ub_scsi_cmd *head, *tail; |
| 285 | }; |
| 286 | |
| 287 | /* |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 288 | * The block device instance (one per LUN). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 290 | struct ub_lun { |
| 291 | struct ub_dev *udev; |
| 292 | struct list_head link; |
| 293 | struct gendisk *disk; |
| 294 | int id; /* Host index */ |
| 295 | int num; /* LUN number */ |
| 296 | char name[16]; |
| 297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | int changed; /* Media was changed */ |
| 299 | int removable; |
| 300 | int readonly; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 301 | |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 302 | struct ub_request urq; |
| 303 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 304 | /* Use Ingo's mempool if or when we have more than one command. */ |
| 305 | /* |
| 306 | * Currently we never need more than one command for the whole device. |
| 307 | * However, giving every LUN a command is a cheap and automatic way |
| 308 | * to enforce fairness between them. |
| 309 | */ |
| 310 | int cmda[1]; |
| 311 | struct ub_scsi_cmd cmdv[1]; |
| 312 | |
| 313 | struct ub_capacity capacity; |
| 314 | }; |
| 315 | |
| 316 | /* |
| 317 | * The USB device instance. |
| 318 | */ |
| 319 | struct ub_dev { |
Pete Zaitcev | 65b4fe5 | 2005-12-28 14:22:17 -0800 | [diff] [blame] | 320 | spinlock_t *lock; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 321 | atomic_t poison; /* The USB device is disconnected */ |
| 322 | int openc; /* protected by ub_lock! */ |
| 323 | /* kref is too implicit for our taste */ |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 324 | int reset; /* Reset is running */ |
Pete Zaitcev | 0da13c8 | 2008-04-19 14:42:49 -0700 | [diff] [blame] | 325 | int bad_resid; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 326 | unsigned int tagcnt; |
| 327 | char name[12]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | struct usb_device *dev; |
| 329 | struct usb_interface *intf; |
| 330 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 331 | struct list_head luns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
| 333 | unsigned int send_bulk_pipe; /* cached pipe values */ |
| 334 | unsigned int recv_bulk_pipe; |
| 335 | unsigned int send_ctrl_pipe; |
| 336 | unsigned int recv_ctrl_pipe; |
| 337 | |
| 338 | struct tasklet_struct tasklet; |
| 339 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | struct ub_scsi_cmd_queue cmd_queue; |
| 341 | struct ub_scsi_cmd top_rqs_cmd; /* REQUEST SENSE */ |
| 342 | unsigned char top_sense[UB_SENSE_SIZE]; |
| 343 | |
| 344 | struct ub_completion work_done; |
| 345 | struct urb work_urb; |
| 346 | struct timer_list work_timer; |
| 347 | int last_pipe; /* What might need clearing */ |
Pete Zaitcev | 1872bce | 2005-07-27 11:43:51 -0700 | [diff] [blame] | 348 | __le32 signature; /* Learned signature */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | struct bulk_cb_wrap work_bcb; |
| 350 | struct bulk_cs_wrap work_bcs; |
| 351 | struct usb_ctrlrequest work_cr; |
| 352 | |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 353 | struct work_struct reset_work; |
| 354 | wait_queue_head_t reset_wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | }; |
| 356 | |
| 357 | /* |
| 358 | */ |
| 359 | static void ub_cleanup(struct ub_dev *sc); |
Pete Zaitcev | 6c1eb8c | 2005-07-30 22:51:52 -0700 | [diff] [blame] | 360 | static int ub_request_fn_1(struct ub_lun *lun, struct request *rq); |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 361 | static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun, |
| 362 | struct ub_scsi_cmd *cmd, struct ub_request *urq); |
| 363 | static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun, |
| 364 | struct ub_scsi_cmd *cmd, struct ub_request *urq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd); |
Tejun Heo | 3755100 | 2009-05-19 18:33:04 +0900 | [diff] [blame] | 366 | static void ub_end_rq(struct request *rq, unsigned int status); |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 367 | static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun, |
| 368 | struct ub_request *urq, struct ub_scsi_cmd *cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 370 | static void ub_urb_complete(struct urb *urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | static void ub_scsi_action(unsigned long _dev); |
| 372 | static void ub_scsi_dispatch(struct ub_dev *sc); |
| 373 | static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd); |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 374 | static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc); |
Pete Zaitcev | 1872bce | 2005-07-27 11:43:51 -0700 | [diff] [blame] | 376 | static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd); |
Pete Zaitcev | 1872bce | 2005-07-27 11:43:51 -0700 | [diff] [blame] | 378 | static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd); |
| 380 | static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd, |
| 381 | int stalled_pipe); |
| 382 | static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd); |
Pete Zaitcev | 2c2e4a2 | 2006-01-05 00:26:30 -0800 | [diff] [blame] | 383 | static void ub_reset_enter(struct ub_dev *sc, int try); |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 384 | static void ub_reset_task(struct work_struct *work); |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 385 | static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun); |
| 386 | static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun, |
| 387 | struct ub_capacity *ret); |
Pete Zaitcev | 2c2e4a2 | 2006-01-05 00:26:30 -0800 | [diff] [blame] | 388 | static int ub_sync_reset(struct ub_dev *sc); |
| 389 | static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe); |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 390 | static int ub_probe_lun(struct ub_dev *sc, int lnum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
| 392 | /* |
| 393 | */ |
Pete Zaitcev | a00828e | 2005-10-22 20:15:09 -0700 | [diff] [blame] | 394 | #ifdef CONFIG_USB_LIBUSUAL |
| 395 | |
Alan Stern | e6e244b | 2009-02-12 14:47:44 -0500 | [diff] [blame] | 396 | #define ub_usb_ids usb_storage_usb_ids |
Pete Zaitcev | a00828e | 2005-10-22 20:15:09 -0700 | [diff] [blame] | 397 | #else |
| 398 | |
Márton Németh | 577cdf0 | 2010-01-10 13:39:39 +0100 | [diff] [blame] | 399 | static const struct usb_device_id ub_usb_ids[] = { |
Michal Nazarewicz | 8fa7fd7 | 2010-10-07 13:05:21 +0200 | [diff] [blame] | 400 | { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, USB_SC_SCSI, USB_PR_BULK) }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | { } |
| 402 | }; |
| 403 | |
| 404 | MODULE_DEVICE_TABLE(usb, ub_usb_ids); |
Pete Zaitcev | a00828e | 2005-10-22 20:15:09 -0700 | [diff] [blame] | 405 | #endif /* CONFIG_USB_LIBUSUAL */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
| 407 | /* |
| 408 | * Find me a way to identify "next free minor" for add_disk(), |
| 409 | * and the array disappears the next day. However, the number of |
| 410 | * hosts has something to do with the naming and /proc/partitions. |
| 411 | * This has to be thought out in detail before changing. |
| 412 | * If UB_MAX_HOST was 1000, we'd use a bitmap. Or a better data structure. |
| 413 | */ |
| 414 | #define UB_MAX_HOSTS 26 |
| 415 | static char ub_hostv[UB_MAX_HOSTS]; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 416 | |
Pete Zaitcev | 65b4fe5 | 2005-12-28 14:22:17 -0800 | [diff] [blame] | 417 | #define UB_QLOCK_NUM 5 |
| 418 | static spinlock_t ub_qlockv[UB_QLOCK_NUM]; |
| 419 | static int ub_qlock_next = 0; |
| 420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | static DEFINE_SPINLOCK(ub_lock); /* Locks globals and ->openc */ |
| 422 | |
| 423 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | * The id allocator. |
| 425 | * |
| 426 | * This also stores the host for indexing by minor, which is somewhat dirty. |
| 427 | */ |
| 428 | static int ub_id_get(void) |
| 429 | { |
| 430 | unsigned long flags; |
| 431 | int i; |
| 432 | |
| 433 | spin_lock_irqsave(&ub_lock, flags); |
| 434 | for (i = 0; i < UB_MAX_HOSTS; i++) { |
| 435 | if (ub_hostv[i] == 0) { |
| 436 | ub_hostv[i] = 1; |
| 437 | spin_unlock_irqrestore(&ub_lock, flags); |
| 438 | return i; |
| 439 | } |
| 440 | } |
| 441 | spin_unlock_irqrestore(&ub_lock, flags); |
| 442 | return -1; |
| 443 | } |
| 444 | |
| 445 | static void ub_id_put(int id) |
| 446 | { |
| 447 | unsigned long flags; |
| 448 | |
| 449 | if (id < 0 || id >= UB_MAX_HOSTS) { |
| 450 | printk(KERN_ERR DRV_NAME ": bad host ID %d\n", id); |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | spin_lock_irqsave(&ub_lock, flags); |
| 455 | if (ub_hostv[id] == 0) { |
| 456 | spin_unlock_irqrestore(&ub_lock, flags); |
| 457 | printk(KERN_ERR DRV_NAME ": freeing free host ID %d\n", id); |
| 458 | return; |
| 459 | } |
| 460 | ub_hostv[id] = 0; |
| 461 | spin_unlock_irqrestore(&ub_lock, flags); |
| 462 | } |
| 463 | |
| 464 | /* |
Pete Zaitcev | 65b4fe5 | 2005-12-28 14:22:17 -0800 | [diff] [blame] | 465 | * This is necessitated by the fact that blk_cleanup_queue does not |
| 466 | * necesserily destroy the queue. Instead, it may merely decrease q->refcnt. |
| 467 | * Since our blk_init_queue() passes a spinlock common with ub_dev, |
| 468 | * we have life time issues when ub_cleanup frees ub_dev. |
| 469 | */ |
| 470 | static spinlock_t *ub_next_lock(void) |
| 471 | { |
| 472 | unsigned long flags; |
| 473 | spinlock_t *ret; |
| 474 | |
| 475 | spin_lock_irqsave(&ub_lock, flags); |
| 476 | ret = &ub_qlockv[ub_qlock_next]; |
| 477 | ub_qlock_next = (ub_qlock_next + 1) % UB_QLOCK_NUM; |
| 478 | spin_unlock_irqrestore(&ub_lock, flags); |
| 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | * Downcount for deallocation. This rides on two assumptions: |
| 484 | * - once something is poisoned, its refcount cannot grow |
| 485 | * - opens cannot happen at this time (del_gendisk was done) |
| 486 | * If the above is true, we can drop the lock, which we need for |
| 487 | * blk_cleanup_queue(): the silly thing may attempt to sleep. |
| 488 | * [Actually, it never needs to sleep for us, but it calls might_sleep()] |
| 489 | */ |
| 490 | static void ub_put(struct ub_dev *sc) |
| 491 | { |
| 492 | unsigned long flags; |
| 493 | |
| 494 | spin_lock_irqsave(&ub_lock, flags); |
| 495 | --sc->openc; |
| 496 | if (sc->openc == 0 && atomic_read(&sc->poison)) { |
| 497 | spin_unlock_irqrestore(&ub_lock, flags); |
| 498 | ub_cleanup(sc); |
| 499 | } else { |
| 500 | spin_unlock_irqrestore(&ub_lock, flags); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | /* |
| 505 | * Final cleanup and deallocation. |
| 506 | */ |
| 507 | static void ub_cleanup(struct ub_dev *sc) |
| 508 | { |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 509 | struct list_head *p; |
| 510 | struct ub_lun *lun; |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 511 | struct request_queue *q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 513 | while (!list_empty(&sc->luns)) { |
| 514 | p = sc->luns.next; |
| 515 | lun = list_entry(p, struct ub_lun, link); |
| 516 | list_del(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 518 | /* I don't think queue can be NULL. But... Stolen from sx8.c */ |
| 519 | if ((q = lun->disk->queue) != NULL) |
| 520 | blk_cleanup_queue(q); |
| 521 | /* |
| 522 | * If we zero disk->private_data BEFORE put_disk, we have |
| 523 | * to check for NULL all over the place in open, release, |
| 524 | * check_media and revalidate, because the block level |
| 525 | * semaphore is well inside the put_disk. |
| 526 | * But we cannot zero after the call, because *disk is gone. |
| 527 | * The sd.c is blatantly racy in this area. |
| 528 | */ |
| 529 | /* disk->private_data = NULL; */ |
| 530 | put_disk(lun->disk); |
| 531 | lun->disk = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 533 | ub_id_put(lun->id); |
| 534 | kfree(lun); |
| 535 | } |
| 536 | |
Pete Zaitcev | 77ef6c4 | 2006-05-03 00:16:00 -0700 | [diff] [blame] | 537 | usb_set_intfdata(sc->intf, NULL); |
| 538 | usb_put_intf(sc->intf); |
| 539 | usb_put_dev(sc->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | kfree(sc); |
| 541 | } |
| 542 | |
| 543 | /* |
| 544 | * The "command allocator". |
| 545 | */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 546 | static struct ub_scsi_cmd *ub_get_cmd(struct ub_lun *lun) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | { |
| 548 | struct ub_scsi_cmd *ret; |
| 549 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 550 | if (lun->cmda[0]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | return NULL; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 552 | ret = &lun->cmdv[0]; |
| 553 | lun->cmda[0] = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | return ret; |
| 555 | } |
| 556 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 557 | static void ub_put_cmd(struct ub_lun *lun, struct ub_scsi_cmd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | { |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 559 | if (cmd != &lun->cmdv[0]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | printk(KERN_WARNING "%s: releasing a foreign cmd %p\n", |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 561 | lun->name, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | return; |
| 563 | } |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 564 | if (!lun->cmda[0]) { |
| 565 | printk(KERN_WARNING "%s: releasing a free cmd\n", lun->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | return; |
| 567 | } |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 568 | lun->cmda[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /* |
| 572 | * The command queue. |
| 573 | */ |
| 574 | static void ub_cmdq_add(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 575 | { |
| 576 | struct ub_scsi_cmd_queue *t = &sc->cmd_queue; |
| 577 | |
| 578 | if (t->qlen++ == 0) { |
| 579 | t->head = cmd; |
| 580 | t->tail = cmd; |
| 581 | } else { |
| 582 | t->tail->next = cmd; |
| 583 | t->tail = cmd; |
| 584 | } |
| 585 | |
| 586 | if (t->qlen > t->qmax) |
| 587 | t->qmax = t->qlen; |
| 588 | } |
| 589 | |
| 590 | static void ub_cmdq_insert(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 591 | { |
| 592 | struct ub_scsi_cmd_queue *t = &sc->cmd_queue; |
| 593 | |
| 594 | if (t->qlen++ == 0) { |
| 595 | t->head = cmd; |
| 596 | t->tail = cmd; |
| 597 | } else { |
| 598 | cmd->next = t->head; |
| 599 | t->head = cmd; |
| 600 | } |
| 601 | |
| 602 | if (t->qlen > t->qmax) |
| 603 | t->qmax = t->qlen; |
| 604 | } |
| 605 | |
| 606 | static struct ub_scsi_cmd *ub_cmdq_pop(struct ub_dev *sc) |
| 607 | { |
| 608 | struct ub_scsi_cmd_queue *t = &sc->cmd_queue; |
| 609 | struct ub_scsi_cmd *cmd; |
| 610 | |
| 611 | if (t->qlen == 0) |
| 612 | return NULL; |
| 613 | if (--t->qlen == 0) |
| 614 | t->tail = NULL; |
| 615 | cmd = t->head; |
| 616 | t->head = cmd->next; |
| 617 | cmd->next = NULL; |
| 618 | return cmd; |
| 619 | } |
| 620 | |
| 621 | #define ub_cmdq_peek(sc) ((sc)->cmd_queue.head) |
| 622 | |
| 623 | /* |
| 624 | * The request function is our main entry point |
| 625 | */ |
| 626 | |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 627 | static void ub_request_fn(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | { |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 629 | struct ub_lun *lun = q->queuedata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | struct request *rq; |
| 631 | |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 632 | while ((rq = blk_peek_request(q)) != NULL) { |
Pete Zaitcev | 6c1eb8c | 2005-07-30 22:51:52 -0700 | [diff] [blame] | 633 | if (ub_request_fn_1(lun, rq) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | blk_stop_queue(q); |
| 635 | break; |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | |
Pete Zaitcev | 6c1eb8c | 2005-07-30 22:51:52 -0700 | [diff] [blame] | 640 | static int ub_request_fn_1(struct ub_lun *lun, struct request *rq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | { |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 642 | struct ub_dev *sc = lun->udev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | struct ub_scsi_cmd *cmd; |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 644 | struct ub_request *urq; |
| 645 | int n_elem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | |
Pete Zaitcev | d1ad4ea | 2006-09-07 16:54:22 -0700 | [diff] [blame] | 647 | if (atomic_read(&sc->poison)) { |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 648 | blk_start_request(rq); |
Tejun Heo | 3755100 | 2009-05-19 18:33:04 +0900 | [diff] [blame] | 649 | ub_end_rq(rq, DID_NO_CONNECT << 16); |
Pete Zaitcev | d1ad4ea | 2006-09-07 16:54:22 -0700 | [diff] [blame] | 650 | return 0; |
| 651 | } |
| 652 | |
Christoph Hellwig | 4c4762d | 2010-06-19 17:26:47 +0200 | [diff] [blame] | 653 | if (lun->changed && rq->cmd_type != REQ_TYPE_BLOCK_PC) { |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 654 | blk_start_request(rq); |
Tejun Heo | 3755100 | 2009-05-19 18:33:04 +0900 | [diff] [blame] | 655 | ub_end_rq(rq, SAM_STAT_CHECK_CONDITION); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | return 0; |
| 657 | } |
| 658 | |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 659 | if (lun->urq.rq != NULL) |
| 660 | return -1; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 661 | if ((cmd = ub_get_cmd(lun)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | return -1; |
| 663 | memset(cmd, 0, sizeof(struct ub_scsi_cmd)); |
| 664 | |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 665 | blk_start_request(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 667 | urq = &lun->urq; |
| 668 | memset(urq, 0, sizeof(struct ub_request)); |
| 669 | urq->rq = rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | |
| 671 | /* |
| 672 | * get scatterlist from block layer |
| 673 | */ |
Pete Zaitcev | 541645b | 2008-02-09 00:10:17 -0800 | [diff] [blame] | 674 | sg_init_table(&urq->sgv[0], UB_MAX_REQ_SG); |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 675 | n_elem = blk_rq_map_sg(lun->disk->queue, rq, &urq->sgv[0]); |
| 676 | if (n_elem < 0) { |
Pete Zaitcev | b560033 | 2006-05-25 20:08:50 -0700 | [diff] [blame] | 677 | /* Impossible, because blk_rq_map_sg should not hit ENOMEM. */ |
Pete Zaitcev | ba6abf1 | 2005-07-30 22:38:30 -0700 | [diff] [blame] | 678 | printk(KERN_INFO "%s: failed request map (%d)\n", |
Pete Zaitcev | b560033 | 2006-05-25 20:08:50 -0700 | [diff] [blame] | 679 | lun->name, n_elem); |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 680 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | } |
Pete Zaitcev | ba6abf1 | 2005-07-30 22:38:30 -0700 | [diff] [blame] | 682 | if (n_elem > UB_MAX_REQ_SG) { /* Paranoia */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | printk(KERN_WARNING "%s: request with %d segments\n", |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 684 | lun->name, n_elem); |
| 685 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | } |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 687 | urq->nsg = n_elem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 689 | if (rq->cmd_type == REQ_TYPE_BLOCK_PC) { |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 690 | ub_cmd_build_packet(sc, lun, cmd, urq); |
| 691 | } else { |
| 692 | ub_cmd_build_block(sc, lun, cmd, urq); |
| 693 | } |
| 694 | cmd->state = UB_CMDST_INIT; |
| 695 | cmd->lun = lun; |
| 696 | cmd->done = ub_rw_cmd_done; |
| 697 | cmd->back = urq; |
| 698 | |
| 699 | cmd->tag = sc->tagcnt++; |
| 700 | if (ub_submit_scsi(sc, cmd) != 0) |
| 701 | goto drop; |
| 702 | |
| 703 | return 0; |
| 704 | |
| 705 | drop: |
| 706 | ub_put_cmd(lun, cmd); |
Tejun Heo | 3755100 | 2009-05-19 18:33:04 +0900 | [diff] [blame] | 707 | ub_end_rq(rq, DID_ERROR << 16); |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun, |
| 712 | struct ub_scsi_cmd *cmd, struct ub_request *urq) |
| 713 | { |
| 714 | struct request *rq = urq->rq; |
| 715 | unsigned int block, nblks; |
| 716 | |
| 717 | if (rq_data_dir(rq) == WRITE) |
| 718 | cmd->dir = UB_DIR_WRITE; |
| 719 | else |
| 720 | cmd->dir = UB_DIR_READ; |
| 721 | |
| 722 | cmd->nsg = urq->nsg; |
| 723 | memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg); |
| 724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | /* |
| 726 | * build the command |
| 727 | * |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 728 | * The call to blk_queue_logical_block_size() guarantees that request |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | * is aligned, but it is given in terms of 512 byte units, always. |
| 730 | */ |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 731 | block = blk_rq_pos(rq) >> lun->capacity.bshift; |
| 732 | nblks = blk_rq_sectors(rq) >> lun->capacity.bshift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 734 | cmd->cdb[0] = (cmd->dir == UB_DIR_READ)? READ_10: WRITE_10; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | /* 10-byte uses 4 bytes of LBA: 2147483648KB, 2097152MB, 2048GB */ |
| 736 | cmd->cdb[2] = block >> 24; |
| 737 | cmd->cdb[3] = block >> 16; |
| 738 | cmd->cdb[4] = block >> 8; |
| 739 | cmd->cdb[5] = block; |
| 740 | cmd->cdb[7] = nblks >> 8; |
| 741 | cmd->cdb[8] = nblks; |
| 742 | cmd->cdb_len = 10; |
| 743 | |
Tejun Heo | 1011c1b | 2009-05-07 22:24:45 +0900 | [diff] [blame] | 744 | cmd->len = blk_rq_bytes(rq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 747 | static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun, |
| 748 | struct ub_scsi_cmd *cmd, struct ub_request *urq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | { |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 750 | struct request *rq = urq->rq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | |
Tejun Heo | b079041 | 2009-05-07 22:24:42 +0900 | [diff] [blame] | 752 | if (blk_rq_bytes(rq) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | cmd->dir = UB_DIR_NONE; |
| 754 | } else { |
| 755 | if (rq_data_dir(rq) == WRITE) |
| 756 | cmd->dir = UB_DIR_WRITE; |
| 757 | else |
| 758 | cmd->dir = UB_DIR_READ; |
| 759 | } |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 760 | |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 761 | cmd->nsg = urq->nsg; |
| 762 | memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg); |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 763 | |
| 764 | memcpy(&cmd->cdb, rq->cmd, rq->cmd_len); |
| 765 | cmd->cdb_len = rq->cmd_len; |
| 766 | |
Tejun Heo | b079041 | 2009-05-07 22:24:42 +0900 | [diff] [blame] | 767 | cmd->len = blk_rq_bytes(rq); |
Pete Zaitcev | 2c51ae7 | 2008-04-19 14:32:18 -0700 | [diff] [blame] | 768 | |
| 769 | /* |
| 770 | * To reapply this to every URB is not as incorrect as it looks. |
| 771 | * In return, we avoid any complicated tracking calculations. |
| 772 | */ |
| 773 | cmd->timeo = rq->timeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 777 | { |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 778 | struct ub_lun *lun = cmd->lun; |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 779 | struct ub_request *urq = cmd->back; |
| 780 | struct request *rq; |
Pete Zaitcev | d1ad4ea | 2006-09-07 16:54:22 -0700 | [diff] [blame] | 781 | unsigned int scsi_status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 783 | rq = urq->rq; |
| 784 | |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 785 | if (cmd->error == 0) { |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 786 | if (rq->cmd_type == REQ_TYPE_BLOCK_PC) { |
Tejun Heo | 5f49f63 | 2009-05-19 18:33:05 +0900 | [diff] [blame] | 787 | if (cmd->act_len >= rq->resid_len) |
| 788 | rq->resid_len = 0; |
| 789 | else |
| 790 | rq->resid_len -= cmd->act_len; |
Pete Zaitcev | ef45cb6 | 2008-04-08 17:41:51 -0700 | [diff] [blame] | 791 | scsi_status = 0; |
| 792 | } else { |
| 793 | if (cmd->act_len != cmd->len) { |
Pete Zaitcev | ef45cb6 | 2008-04-08 17:41:51 -0700 | [diff] [blame] | 794 | scsi_status = SAM_STAT_CHECK_CONDITION; |
| 795 | } else { |
| 796 | scsi_status = 0; |
| 797 | } |
Pete Zaitcev | ba6abf1 | 2005-07-30 22:38:30 -0700 | [diff] [blame] | 798 | } |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 799 | } else { |
Christoph Hellwig | 33659eb | 2010-08-07 18:17:56 +0200 | [diff] [blame] | 800 | if (rq->cmd_type == REQ_TYPE_BLOCK_PC) { |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 801 | /* UB_SENSE_SIZE is smaller than SCSI_SENSE_BUFFERSIZE */ |
| 802 | memcpy(rq->sense, sc->top_sense, UB_SENSE_SIZE); |
| 803 | rq->sense_len = UB_SENSE_SIZE; |
| 804 | if (sc->top_sense[0] != 0) |
Pete Zaitcev | d1ad4ea | 2006-09-07 16:54:22 -0700 | [diff] [blame] | 805 | scsi_status = SAM_STAT_CHECK_CONDITION; |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 806 | else |
Pete Zaitcev | d1ad4ea | 2006-09-07 16:54:22 -0700 | [diff] [blame] | 807 | scsi_status = DID_ERROR << 16; |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 808 | } else { |
Pete Zaitcev | 82fe26b | 2008-04-19 14:35:30 -0700 | [diff] [blame] | 809 | if (cmd->error == -EIO && |
| 810 | (cmd->key == 0 || |
| 811 | cmd->key == MEDIUM_ERROR || |
| 812 | cmd->key == UNIT_ATTENTION)) { |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 813 | if (ub_rw_cmd_retry(sc, lun, urq, cmd) == 0) |
| 814 | return; |
| 815 | } |
Pete Zaitcev | d1ad4ea | 2006-09-07 16:54:22 -0700 | [diff] [blame] | 816 | scsi_status = SAM_STAT_CHECK_CONDITION; |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 817 | } |
| 818 | } |
Pete Zaitcev | ba6abf1 | 2005-07-30 22:38:30 -0700 | [diff] [blame] | 819 | |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 820 | urq->rq = NULL; |
| 821 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 822 | ub_put_cmd(lun, cmd); |
Tejun Heo | 3755100 | 2009-05-19 18:33:04 +0900 | [diff] [blame] | 823 | ub_end_rq(rq, scsi_status); |
Pete Zaitcev | ba6abf1 | 2005-07-30 22:38:30 -0700 | [diff] [blame] | 824 | blk_start_queue(lun->disk->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Tejun Heo | 3755100 | 2009-05-19 18:33:04 +0900 | [diff] [blame] | 827 | static void ub_end_rq(struct request *rq, unsigned int scsi_status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | { |
Kiyoshi Ueda | 7d699ba | 2007-12-11 17:46:47 -0500 | [diff] [blame] | 829 | int error; |
Pete Zaitcev | d1ad4ea | 2006-09-07 16:54:22 -0700 | [diff] [blame] | 830 | |
| 831 | if (scsi_status == 0) { |
Kiyoshi Ueda | 7d699ba | 2007-12-11 17:46:47 -0500 | [diff] [blame] | 832 | error = 0; |
Pete Zaitcev | d1ad4ea | 2006-09-07 16:54:22 -0700 | [diff] [blame] | 833 | } else { |
Kiyoshi Ueda | 7d699ba | 2007-12-11 17:46:47 -0500 | [diff] [blame] | 834 | error = -EIO; |
Pete Zaitcev | d1ad4ea | 2006-09-07 16:54:22 -0700 | [diff] [blame] | 835 | rq->errors = scsi_status; |
| 836 | } |
Tejun Heo | 3755100 | 2009-05-19 18:33:04 +0900 | [diff] [blame] | 837 | __blk_end_request_all(rq, error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | } |
| 839 | |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 840 | static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun, |
| 841 | struct ub_request *urq, struct ub_scsi_cmd *cmd) |
| 842 | { |
| 843 | |
| 844 | if (atomic_read(&sc->poison)) |
| 845 | return -ENXIO; |
| 846 | |
Pete Zaitcev | 2c2e4a2 | 2006-01-05 00:26:30 -0800 | [diff] [blame] | 847 | ub_reset_enter(sc, urq->current_try); |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 848 | |
| 849 | if (urq->current_try >= 3) |
| 850 | return -EIO; |
| 851 | urq->current_try++; |
Pete Zaitcev | b560033 | 2006-05-25 20:08:50 -0700 | [diff] [blame] | 852 | |
| 853 | /* Remove this if anyone complains of flooding. */ |
| 854 | printk(KERN_DEBUG "%s: dir %c len/act %d/%d " |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 855 | "[sense %x %02x %02x] retry %d\n", |
| 856 | sc->name, UB_DIR_CHAR(cmd->dir), cmd->len, cmd->act_len, |
| 857 | cmd->key, cmd->asc, cmd->ascq, urq->current_try); |
| 858 | |
| 859 | memset(cmd, 0, sizeof(struct ub_scsi_cmd)); |
| 860 | ub_cmd_build_block(sc, lun, cmd, urq); |
| 861 | |
| 862 | cmd->state = UB_CMDST_INIT; |
| 863 | cmd->lun = lun; |
| 864 | cmd->done = ub_rw_cmd_done; |
| 865 | cmd->back = urq; |
| 866 | |
| 867 | cmd->tag = sc->tagcnt++; |
| 868 | |
| 869 | #if 0 /* Wasteful */ |
| 870 | return ub_submit_scsi(sc, cmd); |
| 871 | #else |
| 872 | ub_cmdq_add(sc, cmd); |
| 873 | return 0; |
| 874 | #endif |
| 875 | } |
| 876 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | /* |
| 878 | * Submit a regular SCSI operation (not an auto-sense). |
| 879 | * |
| 880 | * The Iron Law of Good Submit Routine is: |
| 881 | * Zero return - callback is done, Nonzero return - callback is not done. |
| 882 | * No exceptions. |
| 883 | * |
| 884 | * Host is assumed locked. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | */ |
| 886 | static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 887 | { |
| 888 | |
| 889 | if (cmd->state != UB_CMDST_INIT || |
| 890 | (cmd->dir != UB_DIR_NONE && cmd->len == 0)) { |
| 891 | return -EINVAL; |
| 892 | } |
| 893 | |
| 894 | ub_cmdq_add(sc, cmd); |
| 895 | /* |
| 896 | * We can call ub_scsi_dispatch(sc) right away here, but it's a little |
| 897 | * safer to jump to a tasklet, in case upper layers do something silly. |
| 898 | */ |
| 899 | tasklet_schedule(&sc->tasklet); |
| 900 | return 0; |
| 901 | } |
| 902 | |
| 903 | /* |
| 904 | * Submit the first URB for the queued command. |
| 905 | * This function does not deal with queueing in any way. |
| 906 | */ |
| 907 | static int ub_scsi_cmd_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 908 | { |
| 909 | struct bulk_cb_wrap *bcb; |
| 910 | int rc; |
| 911 | |
| 912 | bcb = &sc->work_bcb; |
| 913 | |
| 914 | /* |
| 915 | * ``If the allocation length is eighteen or greater, and a device |
| 916 | * server returns less than eithteen bytes of data, the application |
| 917 | * client should assume that the bytes not transferred would have been |
| 918 | * zeroes had the device server returned those bytes.'' |
| 919 | * |
| 920 | * We zero sense for all commands so that when a packet request |
| 921 | * fails it does not return a stale sense. |
| 922 | */ |
| 923 | memset(&sc->top_sense, 0, UB_SENSE_SIZE); |
| 924 | |
| 925 | /* set up the command wrapper */ |
| 926 | bcb->Signature = cpu_to_le32(US_BULK_CB_SIGN); |
| 927 | bcb->Tag = cmd->tag; /* Endianness is not important */ |
| 928 | bcb->DataTransferLength = cpu_to_le32(cmd->len); |
| 929 | bcb->Flags = (cmd->dir == UB_DIR_READ) ? 0x80 : 0; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 930 | bcb->Lun = (cmd->lun != NULL) ? cmd->lun->num : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | bcb->Length = cmd->cdb_len; |
| 932 | |
| 933 | /* copy the command payload */ |
| 934 | memcpy(bcb->CDB, cmd->cdb, UB_MAX_CDB_SIZE); |
| 935 | |
| 936 | UB_INIT_COMPLETION(sc->work_done); |
| 937 | |
| 938 | sc->last_pipe = sc->send_bulk_pipe; |
| 939 | usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->send_bulk_pipe, |
| 940 | bcb, US_BULK_CB_WRAP_LEN, ub_urb_complete, sc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) { |
| 943 | /* XXX Clear stalls */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | ub_complete(&sc->work_done); |
| 945 | return rc; |
| 946 | } |
| 947 | |
| 948 | sc->work_timer.expires = jiffies + UB_URB_TIMEOUT; |
| 949 | add_timer(&sc->work_timer); |
| 950 | |
| 951 | cmd->state = UB_CMDST_CMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | return 0; |
| 953 | } |
| 954 | |
| 955 | /* |
| 956 | * Timeout handler. |
| 957 | */ |
| 958 | static void ub_urb_timeout(unsigned long arg) |
| 959 | { |
| 960 | struct ub_dev *sc = (struct ub_dev *) arg; |
| 961 | unsigned long flags; |
| 962 | |
Pete Zaitcev | 65b4fe5 | 2005-12-28 14:22:17 -0800 | [diff] [blame] | 963 | spin_lock_irqsave(sc->lock, flags); |
Pete Zaitcev | b31f821 | 2006-01-05 00:14:02 -0800 | [diff] [blame] | 964 | if (!ub_is_completed(&sc->work_done)) |
| 965 | usb_unlink_urb(&sc->work_urb); |
Pete Zaitcev | 65b4fe5 | 2005-12-28 14:22:17 -0800 | [diff] [blame] | 966 | spin_unlock_irqrestore(sc->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | /* |
| 970 | * Completion routine for the work URB. |
| 971 | * |
| 972 | * This can be called directly from usb_submit_urb (while we have |
| 973 | * the sc->lock taken) and from an interrupt (while we do NOT have |
| 974 | * the sc->lock taken). Therefore, bounce this off to a tasklet. |
| 975 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 976 | static void ub_urb_complete(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | { |
| 978 | struct ub_dev *sc = urb->context; |
| 979 | |
| 980 | ub_complete(&sc->work_done); |
| 981 | tasklet_schedule(&sc->tasklet); |
| 982 | } |
| 983 | |
| 984 | static void ub_scsi_action(unsigned long _dev) |
| 985 | { |
| 986 | struct ub_dev *sc = (struct ub_dev *) _dev; |
| 987 | unsigned long flags; |
| 988 | |
Pete Zaitcev | 65b4fe5 | 2005-12-28 14:22:17 -0800 | [diff] [blame] | 989 | spin_lock_irqsave(sc->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | ub_scsi_dispatch(sc); |
Pete Zaitcev | 65b4fe5 | 2005-12-28 14:22:17 -0800 | [diff] [blame] | 991 | spin_unlock_irqrestore(sc->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | static void ub_scsi_dispatch(struct ub_dev *sc) |
| 995 | { |
| 996 | struct ub_scsi_cmd *cmd; |
| 997 | int rc; |
| 998 | |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 999 | while (!sc->reset && (cmd = ub_cmdq_peek(sc)) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | if (cmd->state == UB_CMDST_DONE) { |
| 1001 | ub_cmdq_pop(sc); |
| 1002 | (*cmd->done)(sc, cmd); |
| 1003 | } else if (cmd->state == UB_CMDST_INIT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | if ((rc = ub_scsi_cmd_start(sc, cmd)) == 0) |
| 1005 | break; |
| 1006 | cmd->error = rc; |
| 1007 | cmd->state = UB_CMDST_DONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | } else { |
| 1009 | if (!ub_is_completed(&sc->work_done)) |
| 1010 | break; |
Pete Zaitcev | b31f821 | 2006-01-05 00:14:02 -0800 | [diff] [blame] | 1011 | del_timer(&sc->work_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | ub_scsi_urb_compl(sc, cmd); |
| 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 1018 | { |
| 1019 | struct urb *urb = &sc->work_urb; |
| 1020 | struct bulk_cs_wrap *bcs; |
David Vrabel | 3444b26 | 2009-04-08 17:36:28 +0000 | [diff] [blame] | 1021 | int endp; |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1022 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | int rc; |
| 1024 | |
| 1025 | if (atomic_read(&sc->poison)) { |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1026 | ub_state_done(sc, cmd, -ENODEV); |
| 1027 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
David Vrabel | 3444b26 | 2009-04-08 17:36:28 +0000 | [diff] [blame] | 1030 | endp = usb_pipeendpoint(sc->last_pipe); |
| 1031 | if (usb_pipein(sc->last_pipe)) |
| 1032 | endp |= USB_DIR_IN; |
| 1033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | if (cmd->state == UB_CMDST_CLEAR) { |
| 1035 | if (urb->status == -EPIPE) { |
| 1036 | /* |
| 1037 | * STALL while clearning STALL. |
| 1038 | * The control pipe clears itself - nothing to do. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1040 | printk(KERN_NOTICE "%s: stall on control pipe\n", |
| 1041 | sc->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | goto Bad_End; |
| 1043 | } |
| 1044 | |
| 1045 | /* |
| 1046 | * We ignore the result for the halt clear. |
| 1047 | */ |
| 1048 | |
David Vrabel | 3444b26 | 2009-04-08 17:36:28 +0000 | [diff] [blame] | 1049 | usb_reset_endpoint(sc->dev, endp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | |
| 1051 | ub_state_sense(sc, cmd); |
| 1052 | |
| 1053 | } else if (cmd->state == UB_CMDST_CLR2STS) { |
| 1054 | if (urb->status == -EPIPE) { |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1055 | printk(KERN_NOTICE "%s: stall on control pipe\n", |
| 1056 | sc->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | goto Bad_End; |
| 1058 | } |
| 1059 | |
| 1060 | /* |
| 1061 | * We ignore the result for the halt clear. |
| 1062 | */ |
| 1063 | |
David Vrabel | 3444b26 | 2009-04-08 17:36:28 +0000 | [diff] [blame] | 1064 | usb_reset_endpoint(sc->dev, endp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | |
| 1066 | ub_state_stat(sc, cmd); |
| 1067 | |
Pete Zaitcev | 1872bce | 2005-07-27 11:43:51 -0700 | [diff] [blame] | 1068 | } else if (cmd->state == UB_CMDST_CLRRS) { |
| 1069 | if (urb->status == -EPIPE) { |
Pete Zaitcev | 1872bce | 2005-07-27 11:43:51 -0700 | [diff] [blame] | 1070 | printk(KERN_NOTICE "%s: stall on control pipe\n", |
| 1071 | sc->name); |
| 1072 | goto Bad_End; |
| 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * We ignore the result for the halt clear. |
| 1077 | */ |
| 1078 | |
David Vrabel | 3444b26 | 2009-04-08 17:36:28 +0000 | [diff] [blame] | 1079 | usb_reset_endpoint(sc->dev, endp); |
Pete Zaitcev | 1872bce | 2005-07-27 11:43:51 -0700 | [diff] [blame] | 1080 | |
| 1081 | ub_state_stat_counted(sc, cmd); |
| 1082 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | } else if (cmd->state == UB_CMDST_CMD) { |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1084 | switch (urb->status) { |
| 1085 | case 0: |
| 1086 | break; |
| 1087 | case -EOVERFLOW: |
| 1088 | goto Bad_End; |
| 1089 | case -EPIPE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe); |
| 1091 | if (rc != 0) { |
| 1092 | printk(KERN_NOTICE "%s: " |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1093 | "unable to submit clear (%d)\n", |
| 1094 | sc->name, rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | /* |
| 1096 | * This is typically ENOMEM or some other such shit. |
| 1097 | * Retrying is pointless. Just do Bad End on it... |
| 1098 | */ |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1099 | ub_state_done(sc, cmd, rc); |
| 1100 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | } |
| 1102 | cmd->state = UB_CMDST_CLEAR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | return; |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1104 | case -ESHUTDOWN: /* unplug */ |
| 1105 | case -EILSEQ: /* unplug timeout on uhci */ |
| 1106 | ub_state_done(sc, cmd, -ENODEV); |
| 1107 | return; |
| 1108 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | goto Bad_End; |
| 1110 | } |
| 1111 | if (urb->actual_length != US_BULK_CB_WRAP_LEN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | goto Bad_End; |
| 1113 | } |
| 1114 | |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 1115 | if (cmd->dir == UB_DIR_NONE || cmd->nsg < 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | ub_state_stat(sc, cmd); |
| 1117 | return; |
| 1118 | } |
| 1119 | |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 1120 | // udelay(125); // usb-storage has this |
| 1121 | ub_data_start(sc, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | |
| 1123 | } else if (cmd->state == UB_CMDST_DATA) { |
| 1124 | if (urb->status == -EPIPE) { |
| 1125 | rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe); |
| 1126 | if (rc != 0) { |
| 1127 | printk(KERN_NOTICE "%s: " |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1128 | "unable to submit clear (%d)\n", |
| 1129 | sc->name, rc); |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1130 | ub_state_done(sc, cmd, rc); |
| 1131 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | } |
| 1133 | cmd->state = UB_CMDST_CLR2STS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | return; |
| 1135 | } |
| 1136 | if (urb->status == -EOVERFLOW) { |
| 1137 | /* |
| 1138 | * A babble? Failure, but we must transfer CSW now. |
| 1139 | */ |
| 1140 | cmd->error = -EOVERFLOW; /* A cheap trick... */ |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 1141 | ub_state_stat(sc, cmd); |
| 1142 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | } |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1144 | |
| 1145 | if (cmd->dir == UB_DIR_WRITE) { |
| 1146 | /* |
| 1147 | * Do not continue writes in case of a failure. |
| 1148 | * Doing so would cause sectors to be mixed up, |
| 1149 | * which is worse than sectors lost. |
| 1150 | * |
| 1151 | * We must try to read the CSW, or many devices |
| 1152 | * get confused. |
| 1153 | */ |
| 1154 | len = urb->actual_length; |
| 1155 | if (urb->status != 0 || |
| 1156 | len != cmd->sgv[cmd->current_sg].length) { |
| 1157 | cmd->act_len += len; |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1158 | |
| 1159 | cmd->error = -EIO; |
| 1160 | ub_state_stat(sc, cmd); |
| 1161 | return; |
| 1162 | } |
| 1163 | |
| 1164 | } else { |
| 1165 | /* |
| 1166 | * If an error occurs on read, we record it, and |
| 1167 | * continue to fetch data in order to avoid bubble. |
| 1168 | * |
| 1169 | * As a small shortcut, we stop if we detect that |
| 1170 | * a CSW mixed into data. |
| 1171 | */ |
| 1172 | if (urb->status != 0) |
| 1173 | cmd->error = -EIO; |
| 1174 | |
| 1175 | len = urb->actual_length; |
| 1176 | if (urb->status != 0 || |
| 1177 | len != cmd->sgv[cmd->current_sg].length) { |
| 1178 | if ((len & 0x1FF) == US_BULK_CS_WRAP_LEN) |
| 1179 | goto Bad_End; |
| 1180 | } |
| 1181 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 1183 | cmd->act_len += urb->actual_length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 1185 | if (++cmd->current_sg < cmd->nsg) { |
| 1186 | ub_data_start(sc, cmd); |
| 1187 | return; |
| 1188 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | ub_state_stat(sc, cmd); |
| 1190 | |
| 1191 | } else if (cmd->state == UB_CMDST_STAT) { |
| 1192 | if (urb->status == -EPIPE) { |
| 1193 | rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe); |
| 1194 | if (rc != 0) { |
| 1195 | printk(KERN_NOTICE "%s: " |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1196 | "unable to submit clear (%d)\n", |
| 1197 | sc->name, rc); |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1198 | ub_state_done(sc, cmd, rc); |
| 1199 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | } |
Pete Zaitcev | 1872bce | 2005-07-27 11:43:51 -0700 | [diff] [blame] | 1201 | |
| 1202 | /* |
| 1203 | * Having a stall when getting CSW is an error, so |
| 1204 | * make sure uppper levels are not oblivious to it. |
| 1205 | */ |
| 1206 | cmd->error = -EIO; /* A cheap trick... */ |
| 1207 | |
| 1208 | cmd->state = UB_CMDST_CLRRS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | return; |
| 1210 | } |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1211 | |
| 1212 | /* Catch everything, including -EOVERFLOW and other nasties. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | if (urb->status != 0) |
| 1214 | goto Bad_End; |
| 1215 | |
| 1216 | if (urb->actual_length == 0) { |
Pete Zaitcev | 1872bce | 2005-07-27 11:43:51 -0700 | [diff] [blame] | 1217 | ub_state_stat_counted(sc, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | return; |
| 1219 | } |
| 1220 | |
| 1221 | /* |
| 1222 | * Check the returned Bulk protocol status. |
Pete Zaitcev | 1872bce | 2005-07-27 11:43:51 -0700 | [diff] [blame] | 1223 | * The status block has to be validated first. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | */ |
| 1225 | |
| 1226 | bcs = &sc->work_bcs; |
Pete Zaitcev | 1872bce | 2005-07-27 11:43:51 -0700 | [diff] [blame] | 1227 | |
| 1228 | if (sc->signature == cpu_to_le32(0)) { |
| 1229 | /* |
| 1230 | * This is the first reply, so do not perform the check. |
| 1231 | * Instead, remember the signature the device uses |
| 1232 | * for future checks. But do not allow a nul. |
| 1233 | */ |
| 1234 | sc->signature = bcs->Signature; |
| 1235 | if (sc->signature == cpu_to_le32(0)) { |
| 1236 | ub_state_stat_counted(sc, cmd); |
| 1237 | return; |
| 1238 | } |
| 1239 | } else { |
| 1240 | if (bcs->Signature != sc->signature) { |
| 1241 | ub_state_stat_counted(sc, cmd); |
| 1242 | return; |
| 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | if (bcs->Tag != cmd->tag) { |
| 1247 | /* |
| 1248 | * This usually happens when we disagree with the |
| 1249 | * device's microcode about something. For instance, |
| 1250 | * a few of them throw this after timeouts. They buffer |
| 1251 | * commands and reply at commands we timed out before. |
| 1252 | * Without flushing these replies we loop forever. |
| 1253 | */ |
| 1254 | ub_state_stat_counted(sc, cmd); |
| 1255 | return; |
| 1256 | } |
| 1257 | |
Pete Zaitcev | 0da13c8 | 2008-04-19 14:42:49 -0700 | [diff] [blame] | 1258 | if (!sc->bad_resid) { |
| 1259 | len = le32_to_cpu(bcs->Residue); |
| 1260 | if (len != cmd->len - cmd->act_len) { |
| 1261 | /* |
| 1262 | * Only start ignoring if this cmd ended well. |
| 1263 | */ |
| 1264 | if (cmd->len == cmd->act_len) { |
| 1265 | printk(KERN_NOTICE "%s: " |
| 1266 | "bad residual %d of %d, ignoring\n", |
| 1267 | sc->name, len, cmd->len); |
| 1268 | sc->bad_resid = 1; |
| 1269 | } |
| 1270 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | switch (bcs->Status) { |
| 1274 | case US_BULK_STAT_OK: |
| 1275 | break; |
| 1276 | case US_BULK_STAT_FAIL: |
| 1277 | ub_state_sense(sc, cmd); |
| 1278 | return; |
| 1279 | case US_BULK_STAT_PHASE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | goto Bad_End; |
| 1281 | default: |
| 1282 | printk(KERN_INFO "%s: unknown CSW status 0x%x\n", |
| 1283 | sc->name, bcs->Status); |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1284 | ub_state_done(sc, cmd, -EINVAL); |
| 1285 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | /* Not zeroing error to preserve a babble indicator */ |
Pete Zaitcev | 1872bce | 2005-07-27 11:43:51 -0700 | [diff] [blame] | 1289 | if (cmd->error != 0) { |
| 1290 | ub_state_sense(sc, cmd); |
| 1291 | return; |
| 1292 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | cmd->state = UB_CMDST_DONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | ub_cmdq_pop(sc); |
| 1295 | (*cmd->done)(sc, cmd); |
| 1296 | |
| 1297 | } else if (cmd->state == UB_CMDST_SENSE) { |
| 1298 | ub_state_done(sc, cmd, -EIO); |
| 1299 | |
| 1300 | } else { |
Pete Zaitcev | 9029b17 | 2008-04-19 14:45:24 -0700 | [diff] [blame] | 1301 | printk(KERN_WARNING "%s: wrong command state %d\n", |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1302 | sc->name, cmd->state); |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1303 | ub_state_done(sc, cmd, -EINVAL); |
| 1304 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | } |
| 1306 | return; |
| 1307 | |
| 1308 | Bad_End: /* Little Excel is dead */ |
| 1309 | ub_state_done(sc, cmd, -EIO); |
| 1310 | } |
| 1311 | |
| 1312 | /* |
| 1313 | * Factorization helper for the command state machine: |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 1314 | * Initiate a data segment transfer. |
| 1315 | */ |
| 1316 | static void ub_data_start(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 1317 | { |
| 1318 | struct scatterlist *sg = &cmd->sgv[cmd->current_sg]; |
| 1319 | int pipe; |
| 1320 | int rc; |
| 1321 | |
| 1322 | UB_INIT_COMPLETION(sc->work_done); |
| 1323 | |
| 1324 | if (cmd->dir == UB_DIR_READ) |
| 1325 | pipe = sc->recv_bulk_pipe; |
| 1326 | else |
| 1327 | pipe = sc->send_bulk_pipe; |
| 1328 | sc->last_pipe = pipe; |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 1329 | usb_fill_bulk_urb(&sc->work_urb, sc->dev, pipe, sg_virt(sg), |
| 1330 | sg->length, ub_urb_complete, sc); |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 1331 | |
| 1332 | if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) { |
| 1333 | /* XXX Clear stalls */ |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 1334 | ub_complete(&sc->work_done); |
| 1335 | ub_state_done(sc, cmd, rc); |
| 1336 | return; |
| 1337 | } |
| 1338 | |
Pete Zaitcev | 2c51ae7 | 2008-04-19 14:32:18 -0700 | [diff] [blame] | 1339 | if (cmd->timeo) |
| 1340 | sc->work_timer.expires = jiffies + cmd->timeo; |
| 1341 | else |
| 1342 | sc->work_timer.expires = jiffies + UB_DATA_TIMEOUT; |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 1343 | add_timer(&sc->work_timer); |
| 1344 | |
| 1345 | cmd->state = UB_CMDST_DATA; |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | /* |
| 1349 | * Factorization helper for the command state machine: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | * Finish the command. |
| 1351 | */ |
| 1352 | static void ub_state_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd, int rc) |
| 1353 | { |
| 1354 | |
| 1355 | cmd->error = rc; |
| 1356 | cmd->state = UB_CMDST_DONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | ub_cmdq_pop(sc); |
| 1358 | (*cmd->done)(sc, cmd); |
| 1359 | } |
| 1360 | |
| 1361 | /* |
| 1362 | * Factorization helper for the command state machine: |
| 1363 | * Submit a CSW read. |
| 1364 | */ |
Pete Zaitcev | 1872bce | 2005-07-27 11:43:51 -0700 | [diff] [blame] | 1365 | static int __ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | { |
| 1367 | int rc; |
| 1368 | |
| 1369 | UB_INIT_COMPLETION(sc->work_done); |
| 1370 | |
| 1371 | sc->last_pipe = sc->recv_bulk_pipe; |
| 1372 | usb_fill_bulk_urb(&sc->work_urb, sc->dev, sc->recv_bulk_pipe, |
| 1373 | &sc->work_bcs, US_BULK_CS_WRAP_LEN, ub_urb_complete, sc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | |
| 1375 | if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) { |
| 1376 | /* XXX Clear stalls */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | ub_complete(&sc->work_done); |
| 1378 | ub_state_done(sc, cmd, rc); |
Pete Zaitcev | 1872bce | 2005-07-27 11:43:51 -0700 | [diff] [blame] | 1379 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | } |
| 1381 | |
Pete Zaitcev | 2c51ae7 | 2008-04-19 14:32:18 -0700 | [diff] [blame] | 1382 | if (cmd->timeo) |
| 1383 | sc->work_timer.expires = jiffies + cmd->timeo; |
| 1384 | else |
| 1385 | sc->work_timer.expires = jiffies + UB_STAT_TIMEOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | add_timer(&sc->work_timer); |
Pete Zaitcev | 1872bce | 2005-07-27 11:43:51 -0700 | [diff] [blame] | 1387 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | /* |
| 1391 | * Factorization helper for the command state machine: |
| 1392 | * Submit a CSW read and go to STAT state. |
| 1393 | */ |
| 1394 | static void ub_state_stat(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 1395 | { |
Pete Zaitcev | 1872bce | 2005-07-27 11:43:51 -0700 | [diff] [blame] | 1396 | |
| 1397 | if (__ub_state_stat(sc, cmd) != 0) |
| 1398 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | |
| 1400 | cmd->stat_count = 0; |
| 1401 | cmd->state = UB_CMDST_STAT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | /* |
| 1405 | * Factorization helper for the command state machine: |
Pete Zaitcev | 1872bce | 2005-07-27 11:43:51 -0700 | [diff] [blame] | 1406 | * Submit a CSW read and go to STAT state with counter (along [C] path). |
| 1407 | */ |
| 1408 | static void ub_state_stat_counted(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 1409 | { |
| 1410 | |
| 1411 | if (++cmd->stat_count >= 4) { |
| 1412 | ub_state_sense(sc, cmd); |
| 1413 | return; |
| 1414 | } |
| 1415 | |
| 1416 | if (__ub_state_stat(sc, cmd) != 0) |
| 1417 | return; |
| 1418 | |
| 1419 | cmd->state = UB_CMDST_STAT; |
Pete Zaitcev | 1872bce | 2005-07-27 11:43:51 -0700 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | /* |
| 1423 | * Factorization helper for the command state machine: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | * Submit a REQUEST SENSE and go to SENSE state. |
| 1425 | */ |
| 1426 | static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 1427 | { |
| 1428 | struct ub_scsi_cmd *scmd; |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 1429 | struct scatterlist *sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | int rc; |
| 1431 | |
| 1432 | if (cmd->cdb[0] == REQUEST_SENSE) { |
| 1433 | rc = -EPIPE; |
| 1434 | goto error; |
| 1435 | } |
| 1436 | |
| 1437 | scmd = &sc->top_rqs_cmd; |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 1438 | memset(scmd, 0, sizeof(struct ub_scsi_cmd)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | scmd->cdb[0] = REQUEST_SENSE; |
| 1440 | scmd->cdb[4] = UB_SENSE_SIZE; |
| 1441 | scmd->cdb_len = 6; |
| 1442 | scmd->dir = UB_DIR_READ; |
| 1443 | scmd->state = UB_CMDST_INIT; |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 1444 | scmd->nsg = 1; |
| 1445 | sg = &scmd->sgv[0]; |
FUJITA Tomonori | 4f33a9d | 2007-10-25 09:17:03 +0200 | [diff] [blame] | 1446 | sg_init_table(sg, UB_MAX_REQ_SG); |
Jens Axboe | 642f149 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 1447 | sg_set_page(sg, virt_to_page(sc->top_sense), UB_SENSE_SIZE, |
| 1448 | (unsigned long)sc->top_sense & (PAGE_SIZE-1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | scmd->len = UB_SENSE_SIZE; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1450 | scmd->lun = cmd->lun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | scmd->done = ub_top_sense_done; |
| 1452 | scmd->back = cmd; |
| 1453 | |
| 1454 | scmd->tag = sc->tagcnt++; |
| 1455 | |
| 1456 | cmd->state = UB_CMDST_SENSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | |
| 1458 | ub_cmdq_insert(sc, scmd); |
| 1459 | return; |
| 1460 | |
| 1461 | error: |
| 1462 | ub_state_done(sc, cmd, rc); |
| 1463 | } |
| 1464 | |
| 1465 | /* |
| 1466 | * A helper for the command's state machine: |
| 1467 | * Submit a stall clear. |
| 1468 | */ |
| 1469 | static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd, |
| 1470 | int stalled_pipe) |
| 1471 | { |
| 1472 | int endp; |
| 1473 | struct usb_ctrlrequest *cr; |
| 1474 | int rc; |
| 1475 | |
| 1476 | endp = usb_pipeendpoint(stalled_pipe); |
| 1477 | if (usb_pipein (stalled_pipe)) |
| 1478 | endp |= USB_DIR_IN; |
| 1479 | |
| 1480 | cr = &sc->work_cr; |
| 1481 | cr->bRequestType = USB_RECIP_ENDPOINT; |
| 1482 | cr->bRequest = USB_REQ_CLEAR_FEATURE; |
| 1483 | cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT); |
| 1484 | cr->wIndex = cpu_to_le16(endp); |
| 1485 | cr->wLength = cpu_to_le16(0); |
| 1486 | |
| 1487 | UB_INIT_COMPLETION(sc->work_done); |
| 1488 | |
| 1489 | usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe, |
| 1490 | (unsigned char*) cr, NULL, 0, ub_urb_complete, sc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | |
| 1492 | if ((rc = usb_submit_urb(&sc->work_urb, GFP_ATOMIC)) != 0) { |
| 1493 | ub_complete(&sc->work_done); |
| 1494 | return rc; |
| 1495 | } |
| 1496 | |
| 1497 | sc->work_timer.expires = jiffies + UB_CTRL_TIMEOUT; |
| 1498 | add_timer(&sc->work_timer); |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
| 1502 | /* |
| 1503 | */ |
| 1504 | static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd) |
| 1505 | { |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 1506 | unsigned char *sense = sc->top_sense; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | struct ub_scsi_cmd *cmd; |
| 1508 | |
| 1509 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | * Find the command which triggered the unit attention or a check, |
| 1511 | * save the sense into it, and advance its state machine. |
| 1512 | */ |
| 1513 | if ((cmd = ub_cmdq_peek(sc)) == NULL) { |
| 1514 | printk(KERN_WARNING "%s: sense done while idle\n", sc->name); |
| 1515 | return; |
| 1516 | } |
| 1517 | if (cmd != scmd->back) { |
| 1518 | printk(KERN_WARNING "%s: " |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1519 | "sense done for wrong command 0x%x\n", |
| 1520 | sc->name, cmd->tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | return; |
| 1522 | } |
| 1523 | if (cmd->state != UB_CMDST_SENSE) { |
Pete Zaitcev | 9029b17 | 2008-04-19 14:45:24 -0700 | [diff] [blame] | 1524 | printk(KERN_WARNING "%s: sense done with bad cmd state %d\n", |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1525 | sc->name, cmd->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | return; |
| 1527 | } |
| 1528 | |
Pete Zaitcev | 952ba22 | 2006-03-02 16:42:59 -0800 | [diff] [blame] | 1529 | /* |
| 1530 | * Ignoring scmd->act_len, because the buffer was pre-zeroed. |
| 1531 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | cmd->key = sense[2] & 0x0F; |
| 1533 | cmd->asc = sense[12]; |
| 1534 | cmd->ascq = sense[13]; |
| 1535 | |
| 1536 | ub_scsi_urb_compl(sc, cmd); |
| 1537 | } |
| 1538 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | /* |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1540 | * Reset management |
| 1541 | */ |
| 1542 | |
Pete Zaitcev | 2c2e4a2 | 2006-01-05 00:26:30 -0800 | [diff] [blame] | 1543 | static void ub_reset_enter(struct ub_dev *sc, int try) |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1544 | { |
| 1545 | |
| 1546 | if (sc->reset) { |
| 1547 | /* This happens often on multi-LUN devices. */ |
| 1548 | return; |
| 1549 | } |
Pete Zaitcev | 2c2e4a2 | 2006-01-05 00:26:30 -0800 | [diff] [blame] | 1550 | sc->reset = try + 1; |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1551 | |
| 1552 | #if 0 /* Not needed because the disconnect waits for us. */ |
| 1553 | unsigned long flags; |
| 1554 | spin_lock_irqsave(&ub_lock, flags); |
| 1555 | sc->openc++; |
| 1556 | spin_unlock_irqrestore(&ub_lock, flags); |
| 1557 | #endif |
| 1558 | |
| 1559 | #if 0 /* We let them stop themselves. */ |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1560 | struct ub_lun *lun; |
Matthias Kaehlcke | a69228d | 2007-07-09 12:03:07 -0700 | [diff] [blame] | 1561 | list_for_each_entry(lun, &sc->luns, link) { |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1562 | blk_stop_queue(lun->disk->queue); |
| 1563 | } |
| 1564 | #endif |
| 1565 | |
| 1566 | schedule_work(&sc->reset_work); |
| 1567 | } |
| 1568 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1569 | static void ub_reset_task(struct work_struct *work) |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1570 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1571 | struct ub_dev *sc = container_of(work, struct ub_dev, reset_work); |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1572 | unsigned long flags; |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1573 | struct ub_lun *lun; |
Alan Stern | 011b15d | 2008-11-04 11:29:27 -0500 | [diff] [blame] | 1574 | int rc; |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1575 | |
| 1576 | if (!sc->reset) { |
| 1577 | printk(KERN_WARNING "%s: Running reset unrequested\n", |
| 1578 | sc->name); |
| 1579 | return; |
| 1580 | } |
| 1581 | |
| 1582 | if (atomic_read(&sc->poison)) { |
Pete Zaitcev | b560033 | 2006-05-25 20:08:50 -0700 | [diff] [blame] | 1583 | ; |
Pete Zaitcev | 2c2e4a2 | 2006-01-05 00:26:30 -0800 | [diff] [blame] | 1584 | } else if ((sc->reset & 1) == 0) { |
| 1585 | ub_sync_reset(sc); |
| 1586 | msleep(700); /* usb-storage sleeps 6s (!) */ |
| 1587 | ub_probe_clear_stall(sc, sc->recv_bulk_pipe); |
| 1588 | ub_probe_clear_stall(sc, sc->send_bulk_pipe); |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1589 | } else if (sc->dev->actconfig->desc.bNumInterfaces != 1) { |
Pete Zaitcev | b560033 | 2006-05-25 20:08:50 -0700 | [diff] [blame] | 1590 | ; |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1591 | } else { |
Alan Stern | 011b15d | 2008-11-04 11:29:27 -0500 | [diff] [blame] | 1592 | rc = usb_lock_device_for_reset(sc->dev, sc->intf); |
| 1593 | if (rc < 0) { |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1594 | printk(KERN_NOTICE |
| 1595 | "%s: usb_lock_device_for_reset failed (%d)\n", |
Alan Stern | 011b15d | 2008-11-04 11:29:27 -0500 | [diff] [blame] | 1596 | sc->name, rc); |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1597 | } else { |
| 1598 | rc = usb_reset_device(sc->dev); |
| 1599 | if (rc < 0) { |
| 1600 | printk(KERN_NOTICE "%s: " |
| 1601 | "usb_lock_device_for_reset failed (%d)\n", |
| 1602 | sc->name, rc); |
| 1603 | } |
Alan Stern | 011b15d | 2008-11-04 11:29:27 -0500 | [diff] [blame] | 1604 | usb_unlock_device(sc->dev); |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | /* |
| 1609 | * In theory, no commands can be running while reset is active, |
| 1610 | * so nobody can ask for another reset, and so we do not need any |
| 1611 | * queues of resets or anything. We do need a spinlock though, |
| 1612 | * to interact with block layer. |
| 1613 | */ |
Pete Zaitcev | 65b4fe5 | 2005-12-28 14:22:17 -0800 | [diff] [blame] | 1614 | spin_lock_irqsave(sc->lock, flags); |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1615 | sc->reset = 0; |
| 1616 | tasklet_schedule(&sc->tasklet); |
Matthias Kaehlcke | a69228d | 2007-07-09 12:03:07 -0700 | [diff] [blame] | 1617 | list_for_each_entry(lun, &sc->luns, link) { |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1618 | blk_start_queue(lun->disk->queue); |
| 1619 | } |
| 1620 | wake_up(&sc->reset_wait); |
Pete Zaitcev | 65b4fe5 | 2005-12-28 14:22:17 -0800 | [diff] [blame] | 1621 | spin_unlock_irqrestore(sc->lock, flags); |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | /* |
Pete Zaitcev | d73b7aff | 2008-11-10 21:11:11 -0700 | [diff] [blame] | 1625 | * XXX Reset brackets are too much hassle to implement, so just stub them |
| 1626 | * in order to prevent forced unbinding (which deadlocks solid when our |
| 1627 | * ->disconnect method waits for the reset to complete and this kills keventd). |
| 1628 | * |
| 1629 | * XXX Tell Alan to move usb_unlock_device inside of usb_reset_device, |
| 1630 | * or else the post_reset is invoked, and restats I/O on a locked device. |
| 1631 | */ |
| 1632 | static int ub_pre_reset(struct usb_interface *iface) { |
| 1633 | return 0; |
| 1634 | } |
| 1635 | |
| 1636 | static int ub_post_reset(struct usb_interface *iface) { |
| 1637 | return 0; |
| 1638 | } |
| 1639 | |
| 1640 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | * This is called from a process context. |
| 1642 | */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1643 | static void ub_revalidate(struct ub_dev *sc, struct ub_lun *lun) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | { |
| 1645 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1646 | lun->readonly = 0; /* XXX Query this from the device */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1648 | lun->capacity.nsec = 0; |
| 1649 | lun->capacity.bsize = 512; |
| 1650 | lun->capacity.bshift = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1652 | if (ub_sync_tur(sc, lun) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | return; /* Not ready */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1654 | lun->changed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1656 | if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | /* |
| 1658 | * The retry here means something is wrong, either with the |
| 1659 | * device, with the transport, or with our code. |
| 1660 | * We keep this because sd.c has retries for capacity. |
| 1661 | */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1662 | if (ub_sync_read_cap(sc, lun, &lun->capacity) != 0) { |
| 1663 | lun->capacity.nsec = 0; |
| 1664 | lun->capacity.bsize = 512; |
| 1665 | lun->capacity.bshift = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | } |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | /* |
| 1671 | * The open funcion. |
| 1672 | * This is mostly needed to keep refcounting, but also to support |
| 1673 | * media checks on removable media drives. |
| 1674 | */ |
Al Viro | 4099a96 | 2008-03-02 10:21:43 -0500 | [diff] [blame] | 1675 | static int ub_bd_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | { |
Al Viro | 4099a96 | 2008-03-02 10:21:43 -0500 | [diff] [blame] | 1677 | struct ub_lun *lun = bdev->bd_disk->private_data; |
Pete Zaitcev | 41fea55e | 2006-04-28 20:45:49 -0700 | [diff] [blame] | 1678 | struct ub_dev *sc = lun->udev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | unsigned long flags; |
| 1680 | int rc; |
| 1681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | spin_lock_irqsave(&ub_lock, flags); |
| 1683 | if (atomic_read(&sc->poison)) { |
| 1684 | spin_unlock_irqrestore(&ub_lock, flags); |
| 1685 | return -ENXIO; |
| 1686 | } |
| 1687 | sc->openc++; |
| 1688 | spin_unlock_irqrestore(&ub_lock, flags); |
| 1689 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1690 | if (lun->removable || lun->readonly) |
Al Viro | 4099a96 | 2008-03-02 10:21:43 -0500 | [diff] [blame] | 1691 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | |
| 1693 | /* |
| 1694 | * The sd.c considers ->media_present and ->changed not equivalent, |
| 1695 | * under some pretty murky conditions (a failure of READ CAPACITY). |
| 1696 | * We may need it one day. |
| 1697 | */ |
Al Viro | 4099a96 | 2008-03-02 10:21:43 -0500 | [diff] [blame] | 1698 | if (lun->removable && lun->changed && !(mode & FMODE_NDELAY)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | rc = -ENOMEDIUM; |
| 1700 | goto err_open; |
| 1701 | } |
| 1702 | |
Al Viro | 4099a96 | 2008-03-02 10:21:43 -0500 | [diff] [blame] | 1703 | if (lun->readonly && (mode & FMODE_WRITE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | rc = -EROFS; |
| 1705 | goto err_open; |
| 1706 | } |
| 1707 | |
| 1708 | return 0; |
| 1709 | |
| 1710 | err_open: |
| 1711 | ub_put(sc); |
| 1712 | return rc; |
| 1713 | } |
| 1714 | |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1715 | static int ub_bd_unlocked_open(struct block_device *bdev, fmode_t mode) |
| 1716 | { |
| 1717 | int ret; |
| 1718 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1719 | mutex_lock(&ub_mutex); |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1720 | ret = ub_bd_open(bdev, mode); |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1721 | mutex_unlock(&ub_mutex); |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1722 | |
| 1723 | return ret; |
| 1724 | } |
| 1725 | |
| 1726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | /* |
| 1728 | */ |
Al Viro | 4099a96 | 2008-03-02 10:21:43 -0500 | [diff] [blame] | 1729 | static int ub_bd_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | { |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1731 | struct ub_lun *lun = disk->private_data; |
| 1732 | struct ub_dev *sc = lun->udev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1734 | mutex_lock(&ub_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | ub_put(sc); |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1736 | mutex_unlock(&ub_mutex); |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1737 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | return 0; |
| 1739 | } |
| 1740 | |
| 1741 | /* |
| 1742 | * The ioctl interface. |
| 1743 | */ |
Al Viro | 4099a96 | 2008-03-02 10:21:43 -0500 | [diff] [blame] | 1744 | static int ub_bd_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | unsigned int cmd, unsigned long arg) |
| 1746 | { |
Al Viro | 4099a96 | 2008-03-02 10:21:43 -0500 | [diff] [blame] | 1747 | struct gendisk *disk = bdev->bd_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | void __user *usermem = (void __user *) arg; |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 1749 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1751 | mutex_lock(&ub_mutex); |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 1752 | ret = scsi_cmd_ioctl(disk->queue, disk, mode, cmd, usermem); |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1753 | mutex_unlock(&ub_mutex); |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 1754 | |
| 1755 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | /* |
Pete Zaitcev | 9029b17 | 2008-04-19 14:45:24 -0700 | [diff] [blame] | 1759 | * This is called by check_disk_change if we reported a media change. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | * The main onjective here is to discover the features of the media such as |
| 1761 | * the capacity, read-only status, etc. USB storage generally does not |
| 1762 | * need to be spun up, but if we needed it, this would be the place. |
| 1763 | * |
| 1764 | * This call can sleep. |
| 1765 | * |
| 1766 | * The return code is not used. |
| 1767 | */ |
| 1768 | static int ub_bd_revalidate(struct gendisk *disk) |
| 1769 | { |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1770 | struct ub_lun *lun = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1772 | ub_revalidate(lun->udev, lun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | |
| 1774 | /* XXX Support sector size switching like in sr.c */ |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1775 | blk_queue_logical_block_size(disk->queue, lun->capacity.bsize); |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1776 | set_capacity(disk, lun->capacity.nsec); |
| 1777 | // set_disk_ro(sdkp->disk, lun->readonly); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | |
| 1779 | return 0; |
| 1780 | } |
| 1781 | |
| 1782 | /* |
| 1783 | * The check is called by the block layer to verify if the media |
| 1784 | * is still available. It is supposed to be harmless, lightweight and |
| 1785 | * non-intrusive in case the media was not changed. |
| 1786 | * |
| 1787 | * This call can sleep. |
| 1788 | * |
| 1789 | * The return code is bool! |
| 1790 | */ |
| 1791 | static int ub_bd_media_changed(struct gendisk *disk) |
| 1792 | { |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1793 | struct ub_lun *lun = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1795 | if (!lun->removable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | return 0; |
| 1797 | |
| 1798 | /* |
| 1799 | * We clean checks always after every command, so this is not |
| 1800 | * as dangerous as it looks. If the TEST_UNIT_READY fails here, |
| 1801 | * the device is actually not ready with operator or software |
| 1802 | * intervention required. One dangerous item might be a drive which |
| 1803 | * spins itself down, and come the time to write dirty pages, this |
| 1804 | * will fail, then block layer discards the data. Since we never |
| 1805 | * spin drives up, such devices simply cannot be used with ub anyway. |
| 1806 | */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1807 | if (ub_sync_tur(lun->udev, lun) != 0) { |
| 1808 | lun->changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | return 1; |
| 1810 | } |
| 1811 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1812 | return lun->changed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | } |
| 1814 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 1815 | static const struct block_device_operations ub_bd_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | .owner = THIS_MODULE, |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1817 | .open = ub_bd_unlocked_open, |
Al Viro | 4099a96 | 2008-03-02 10:21:43 -0500 | [diff] [blame] | 1818 | .release = ub_bd_release, |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 1819 | .ioctl = ub_bd_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | .media_changed = ub_bd_media_changed, |
| 1821 | .revalidate_disk = ub_bd_revalidate, |
| 1822 | }; |
| 1823 | |
| 1824 | /* |
| 1825 | * Common ->done routine for commands executed synchronously. |
| 1826 | */ |
| 1827 | static void ub_probe_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd) |
| 1828 | { |
| 1829 | struct completion *cop = cmd->back; |
| 1830 | complete(cop); |
| 1831 | } |
| 1832 | |
| 1833 | /* |
| 1834 | * Test if the device has a check condition on it, synchronously. |
| 1835 | */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1836 | static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | { |
| 1838 | struct ub_scsi_cmd *cmd; |
| 1839 | enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) }; |
| 1840 | unsigned long flags; |
| 1841 | struct completion compl; |
| 1842 | int rc; |
| 1843 | |
| 1844 | init_completion(&compl); |
| 1845 | |
| 1846 | rc = -ENOMEM; |
Pete Zaitcev | 29da793 | 2006-02-13 20:35:57 -0800 | [diff] [blame] | 1847 | if ((cmd = kzalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | goto err_alloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | |
| 1850 | cmd->cdb[0] = TEST_UNIT_READY; |
| 1851 | cmd->cdb_len = 6; |
| 1852 | cmd->dir = UB_DIR_NONE; |
| 1853 | cmd->state = UB_CMDST_INIT; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1854 | cmd->lun = lun; /* This may be NULL, but that's ok */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | cmd->done = ub_probe_done; |
| 1856 | cmd->back = &compl; |
| 1857 | |
Pete Zaitcev | 65b4fe5 | 2005-12-28 14:22:17 -0800 | [diff] [blame] | 1858 | spin_lock_irqsave(sc->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | cmd->tag = sc->tagcnt++; |
| 1860 | |
| 1861 | rc = ub_submit_scsi(sc, cmd); |
Pete Zaitcev | 65b4fe5 | 2005-12-28 14:22:17 -0800 | [diff] [blame] | 1862 | spin_unlock_irqrestore(sc->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | |
Pete Zaitcev | b560033 | 2006-05-25 20:08:50 -0700 | [diff] [blame] | 1864 | if (rc != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | goto err_submit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | |
| 1867 | wait_for_completion(&compl); |
| 1868 | |
| 1869 | rc = cmd->error; |
| 1870 | |
| 1871 | if (rc == -EIO && cmd->key != 0) /* Retries for benh's key */ |
| 1872 | rc = cmd->key; |
| 1873 | |
| 1874 | err_submit: |
| 1875 | kfree(cmd); |
| 1876 | err_alloc: |
| 1877 | return rc; |
| 1878 | } |
| 1879 | |
| 1880 | /* |
| 1881 | * Read the SCSI capacity synchronously (for probing). |
| 1882 | */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1883 | static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun, |
| 1884 | struct ub_capacity *ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | { |
| 1886 | struct ub_scsi_cmd *cmd; |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 1887 | struct scatterlist *sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | char *p; |
| 1889 | enum { ALLOC_SIZE = sizeof(struct ub_scsi_cmd) + 8 }; |
| 1890 | unsigned long flags; |
| 1891 | unsigned int bsize, shift; |
| 1892 | unsigned long nsec; |
| 1893 | struct completion compl; |
| 1894 | int rc; |
| 1895 | |
| 1896 | init_completion(&compl); |
| 1897 | |
| 1898 | rc = -ENOMEM; |
Pete Zaitcev | 29da793 | 2006-02-13 20:35:57 -0800 | [diff] [blame] | 1899 | if ((cmd = kzalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | goto err_alloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | p = (char *)cmd + sizeof(struct ub_scsi_cmd); |
| 1902 | |
| 1903 | cmd->cdb[0] = 0x25; |
| 1904 | cmd->cdb_len = 10; |
| 1905 | cmd->dir = UB_DIR_READ; |
| 1906 | cmd->state = UB_CMDST_INIT; |
Pete Zaitcev | a1cf96e | 2005-08-14 21:16:03 -0700 | [diff] [blame] | 1907 | cmd->nsg = 1; |
| 1908 | sg = &cmd->sgv[0]; |
FUJITA Tomonori | 4f33a9d | 2007-10-25 09:17:03 +0200 | [diff] [blame] | 1909 | sg_init_table(sg, UB_MAX_REQ_SG); |
Jens Axboe | 642f149 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 1910 | sg_set_page(sg, virt_to_page(p), 8, (unsigned long)p & (PAGE_SIZE-1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | cmd->len = 8; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 1912 | cmd->lun = lun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | cmd->done = ub_probe_done; |
| 1914 | cmd->back = &compl; |
| 1915 | |
Pete Zaitcev | 65b4fe5 | 2005-12-28 14:22:17 -0800 | [diff] [blame] | 1916 | spin_lock_irqsave(sc->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | cmd->tag = sc->tagcnt++; |
| 1918 | |
| 1919 | rc = ub_submit_scsi(sc, cmd); |
Pete Zaitcev | 65b4fe5 | 2005-12-28 14:22:17 -0800 | [diff] [blame] | 1920 | spin_unlock_irqrestore(sc->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | |
Pete Zaitcev | b560033 | 2006-05-25 20:08:50 -0700 | [diff] [blame] | 1922 | if (rc != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | goto err_submit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | |
| 1925 | wait_for_completion(&compl); |
| 1926 | |
| 1927 | if (cmd->error != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | rc = -EIO; |
| 1929 | goto err_read; |
| 1930 | } |
| 1931 | if (cmd->act_len != 8) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | rc = -EIO; |
| 1933 | goto err_read; |
| 1934 | } |
| 1935 | |
| 1936 | /* sd.c special-cases sector size of 0 to mean 512. Needed? Safe? */ |
| 1937 | nsec = be32_to_cpu(*(__be32 *)p) + 1; |
| 1938 | bsize = be32_to_cpu(*(__be32 *)(p + 4)); |
| 1939 | switch (bsize) { |
| 1940 | case 512: shift = 0; break; |
| 1941 | case 1024: shift = 1; break; |
| 1942 | case 2048: shift = 2; break; |
| 1943 | case 4096: shift = 3; break; |
| 1944 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | rc = -EDOM; |
| 1946 | goto err_inv_bsize; |
| 1947 | } |
| 1948 | |
| 1949 | ret->bsize = bsize; |
| 1950 | ret->bshift = shift; |
| 1951 | ret->nsec = nsec << shift; |
| 1952 | rc = 0; |
| 1953 | |
| 1954 | err_inv_bsize: |
| 1955 | err_read: |
| 1956 | err_submit: |
| 1957 | kfree(cmd); |
| 1958 | err_alloc: |
| 1959 | return rc; |
| 1960 | } |
| 1961 | |
| 1962 | /* |
| 1963 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1964 | static void ub_probe_urb_complete(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | { |
| 1966 | struct completion *cop = urb->context; |
| 1967 | complete(cop); |
| 1968 | } |
| 1969 | |
| 1970 | static void ub_probe_timeout(unsigned long arg) |
| 1971 | { |
| 1972 | struct completion *cop = (struct completion *) arg; |
| 1973 | complete(cop); |
| 1974 | } |
| 1975 | |
| 1976 | /* |
Pete Zaitcev | 2c2e4a2 | 2006-01-05 00:26:30 -0800 | [diff] [blame] | 1977 | * Reset with a Bulk reset. |
| 1978 | */ |
| 1979 | static int ub_sync_reset(struct ub_dev *sc) |
| 1980 | { |
| 1981 | int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber; |
| 1982 | struct usb_ctrlrequest *cr; |
| 1983 | struct completion compl; |
| 1984 | struct timer_list timer; |
| 1985 | int rc; |
| 1986 | |
| 1987 | init_completion(&compl); |
| 1988 | |
| 1989 | cr = &sc->work_cr; |
| 1990 | cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE; |
| 1991 | cr->bRequest = US_BULK_RESET_REQUEST; |
| 1992 | cr->wValue = cpu_to_le16(0); |
| 1993 | cr->wIndex = cpu_to_le16(ifnum); |
| 1994 | cr->wLength = cpu_to_le16(0); |
| 1995 | |
| 1996 | usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe, |
| 1997 | (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl); |
Pete Zaitcev | 2c2e4a2 | 2006-01-05 00:26:30 -0800 | [diff] [blame] | 1998 | |
| 1999 | if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) { |
| 2000 | printk(KERN_WARNING |
| 2001 | "%s: Unable to submit a bulk reset (%d)\n", sc->name, rc); |
| 2002 | return rc; |
| 2003 | } |
| 2004 | |
| 2005 | init_timer(&timer); |
| 2006 | timer.function = ub_probe_timeout; |
| 2007 | timer.data = (unsigned long) &compl; |
| 2008 | timer.expires = jiffies + UB_CTRL_TIMEOUT; |
| 2009 | add_timer(&timer); |
| 2010 | |
| 2011 | wait_for_completion(&compl); |
| 2012 | |
| 2013 | del_timer_sync(&timer); |
| 2014 | usb_kill_urb(&sc->work_urb); |
| 2015 | |
| 2016 | return sc->work_urb.status; |
| 2017 | } |
| 2018 | |
| 2019 | /* |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2020 | * Get number of LUNs by the way of Bulk GetMaxLUN command. |
| 2021 | */ |
| 2022 | static int ub_sync_getmaxlun(struct ub_dev *sc) |
| 2023 | { |
| 2024 | int ifnum = sc->intf->cur_altsetting->desc.bInterfaceNumber; |
| 2025 | unsigned char *p; |
| 2026 | enum { ALLOC_SIZE = 1 }; |
| 2027 | struct usb_ctrlrequest *cr; |
| 2028 | struct completion compl; |
| 2029 | struct timer_list timer; |
| 2030 | int nluns; |
| 2031 | int rc; |
| 2032 | |
| 2033 | init_completion(&compl); |
| 2034 | |
| 2035 | rc = -ENOMEM; |
| 2036 | if ((p = kmalloc(ALLOC_SIZE, GFP_KERNEL)) == NULL) |
| 2037 | goto err_alloc; |
| 2038 | *p = 55; |
| 2039 | |
| 2040 | cr = &sc->work_cr; |
| 2041 | cr->bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE; |
| 2042 | cr->bRequest = US_BULK_GET_MAX_LUN; |
| 2043 | cr->wValue = cpu_to_le16(0); |
| 2044 | cr->wIndex = cpu_to_le16(ifnum); |
| 2045 | cr->wLength = cpu_to_le16(1); |
| 2046 | |
| 2047 | usb_fill_control_urb(&sc->work_urb, sc->dev, sc->recv_ctrl_pipe, |
| 2048 | (unsigned char*) cr, p, 1, ub_probe_urb_complete, &compl); |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2049 | |
Pete Zaitcev | b560033 | 2006-05-25 20:08:50 -0700 | [diff] [blame] | 2050 | if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2051 | goto err_submit; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2052 | |
| 2053 | init_timer(&timer); |
| 2054 | timer.function = ub_probe_timeout; |
| 2055 | timer.data = (unsigned long) &compl; |
| 2056 | timer.expires = jiffies + UB_CTRL_TIMEOUT; |
| 2057 | add_timer(&timer); |
| 2058 | |
| 2059 | wait_for_completion(&compl); |
| 2060 | |
| 2061 | del_timer_sync(&timer); |
| 2062 | usb_kill_urb(&sc->work_urb); |
| 2063 | |
Pete Zaitcev | b560033 | 2006-05-25 20:08:50 -0700 | [diff] [blame] | 2064 | if ((rc = sc->work_urb.status) < 0) |
Pete Zaitcev | 64bd845 | 2005-09-22 00:48:29 -0700 | [diff] [blame] | 2065 | goto err_io; |
Pete Zaitcev | 64bd845 | 2005-09-22 00:48:29 -0700 | [diff] [blame] | 2066 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2067 | if (sc->work_urb.actual_length != 1) { |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2068 | nluns = 0; |
| 2069 | } else { |
| 2070 | if ((nluns = *p) == 55) { |
| 2071 | nluns = 0; |
| 2072 | } else { |
| 2073 | /* GetMaxLUN returns the maximum LUN number */ |
| 2074 | nluns += 1; |
| 2075 | if (nluns > UB_MAX_LUNS) |
| 2076 | nluns = UB_MAX_LUNS; |
| 2077 | } |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | kfree(p); |
| 2081 | return nluns; |
| 2082 | |
Pete Zaitcev | 64bd845 | 2005-09-22 00:48:29 -0700 | [diff] [blame] | 2083 | err_io: |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2084 | err_submit: |
| 2085 | kfree(p); |
| 2086 | err_alloc: |
| 2087 | return rc; |
| 2088 | } |
| 2089 | |
| 2090 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | * Clear initial stalls. |
| 2092 | */ |
| 2093 | static int ub_probe_clear_stall(struct ub_dev *sc, int stalled_pipe) |
| 2094 | { |
| 2095 | int endp; |
| 2096 | struct usb_ctrlrequest *cr; |
| 2097 | struct completion compl; |
| 2098 | struct timer_list timer; |
| 2099 | int rc; |
| 2100 | |
| 2101 | init_completion(&compl); |
| 2102 | |
| 2103 | endp = usb_pipeendpoint(stalled_pipe); |
| 2104 | if (usb_pipein (stalled_pipe)) |
| 2105 | endp |= USB_DIR_IN; |
| 2106 | |
| 2107 | cr = &sc->work_cr; |
| 2108 | cr->bRequestType = USB_RECIP_ENDPOINT; |
| 2109 | cr->bRequest = USB_REQ_CLEAR_FEATURE; |
| 2110 | cr->wValue = cpu_to_le16(USB_ENDPOINT_HALT); |
| 2111 | cr->wIndex = cpu_to_le16(endp); |
| 2112 | cr->wLength = cpu_to_le16(0); |
| 2113 | |
| 2114 | usb_fill_control_urb(&sc->work_urb, sc->dev, sc->send_ctrl_pipe, |
| 2115 | (unsigned char*) cr, NULL, 0, ub_probe_urb_complete, &compl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | |
| 2117 | if ((rc = usb_submit_urb(&sc->work_urb, GFP_KERNEL)) != 0) { |
| 2118 | printk(KERN_WARNING |
| 2119 | "%s: Unable to submit a probe clear (%d)\n", sc->name, rc); |
| 2120 | return rc; |
| 2121 | } |
| 2122 | |
| 2123 | init_timer(&timer); |
| 2124 | timer.function = ub_probe_timeout; |
| 2125 | timer.data = (unsigned long) &compl; |
| 2126 | timer.expires = jiffies + UB_CTRL_TIMEOUT; |
| 2127 | add_timer(&timer); |
| 2128 | |
| 2129 | wait_for_completion(&compl); |
| 2130 | |
| 2131 | del_timer_sync(&timer); |
| 2132 | usb_kill_urb(&sc->work_urb); |
| 2133 | |
David Vrabel | 3444b26 | 2009-04-08 17:36:28 +0000 | [diff] [blame] | 2134 | usb_reset_endpoint(sc->dev, endp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | |
| 2136 | return 0; |
| 2137 | } |
| 2138 | |
| 2139 | /* |
| 2140 | * Get the pipe settings. |
| 2141 | */ |
| 2142 | static int ub_get_pipes(struct ub_dev *sc, struct usb_device *dev, |
| 2143 | struct usb_interface *intf) |
| 2144 | { |
| 2145 | struct usb_host_interface *altsetting = intf->cur_altsetting; |
| 2146 | struct usb_endpoint_descriptor *ep_in = NULL; |
| 2147 | struct usb_endpoint_descriptor *ep_out = NULL; |
| 2148 | struct usb_endpoint_descriptor *ep; |
| 2149 | int i; |
| 2150 | |
| 2151 | /* |
| 2152 | * Find the endpoints we need. |
| 2153 | * We are expecting a minimum of 2 endpoints - in and out (bulk). |
| 2154 | * We will ignore any others. |
| 2155 | */ |
| 2156 | for (i = 0; i < altsetting->desc.bNumEndpoints; i++) { |
| 2157 | ep = &altsetting->endpoint[i].desc; |
| 2158 | |
| 2159 | /* Is it a BULK endpoint? */ |
Julia Lawall | db5e6df | 2008-12-29 11:19:10 +0100 | [diff] [blame] | 2160 | if (usb_endpoint_xfer_bulk(ep)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | /* BULK in or out? */ |
Julia Lawall | db5e6df | 2008-12-29 11:19:10 +0100 | [diff] [blame] | 2162 | if (usb_endpoint_dir_in(ep)) { |
Pete Zaitcev | 643616e | 2007-03-08 19:56:23 -0800 | [diff] [blame] | 2163 | if (ep_in == NULL) |
| 2164 | ep_in = ep; |
| 2165 | } else { |
| 2166 | if (ep_out == NULL) |
| 2167 | ep_out = ep; |
| 2168 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | } |
| 2170 | } |
| 2171 | |
| 2172 | if (ep_in == NULL || ep_out == NULL) { |
Pete Zaitcev | 9029b17 | 2008-04-19 14:45:24 -0700 | [diff] [blame] | 2173 | printk(KERN_NOTICE "%s: failed endpoint check\n", sc->name); |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 2174 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | } |
| 2176 | |
| 2177 | /* Calculate and store the pipe values */ |
| 2178 | sc->send_ctrl_pipe = usb_sndctrlpipe(dev, 0); |
| 2179 | sc->recv_ctrl_pipe = usb_rcvctrlpipe(dev, 0); |
| 2180 | sc->send_bulk_pipe = usb_sndbulkpipe(dev, |
Julia Lawall | db5e6df | 2008-12-29 11:19:10 +0100 | [diff] [blame] | 2181 | usb_endpoint_num(ep_out)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | sc->recv_bulk_pipe = usb_rcvbulkpipe(dev, |
Julia Lawall | db5e6df | 2008-12-29 11:19:10 +0100 | [diff] [blame] | 2183 | usb_endpoint_num(ep_in)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | |
| 2185 | return 0; |
| 2186 | } |
| 2187 | |
| 2188 | /* |
| 2189 | * Probing is done in the process context, which allows us to cheat |
| 2190 | * and not to build a state machine for the discovery. |
| 2191 | */ |
| 2192 | static int ub_probe(struct usb_interface *intf, |
| 2193 | const struct usb_device_id *dev_id) |
| 2194 | { |
| 2195 | struct ub_dev *sc; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2196 | int nluns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | int rc; |
| 2198 | int i; |
| 2199 | |
Pete Zaitcev | a00828e | 2005-10-22 20:15:09 -0700 | [diff] [blame] | 2200 | if (usb_usual_check_type(dev_id, USB_US_TYPE_UB)) |
| 2201 | return -ENXIO; |
| 2202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | rc = -ENOMEM; |
Pete Zaitcev | 29da793 | 2006-02-13 20:35:57 -0800 | [diff] [blame] | 2204 | if ((sc = kzalloc(sizeof(struct ub_dev), GFP_KERNEL)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | goto err_core; |
Pete Zaitcev | 65b4fe5 | 2005-12-28 14:22:17 -0800 | [diff] [blame] | 2206 | sc->lock = ub_next_lock(); |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2207 | INIT_LIST_HEAD(&sc->luns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | usb_init_urb(&sc->work_urb); |
| 2209 | tasklet_init(&sc->tasklet, ub_scsi_action, (unsigned long)sc); |
| 2210 | atomic_set(&sc->poison, 0); |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 2211 | INIT_WORK(&sc->reset_work, ub_reset_task); |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 2212 | init_waitqueue_head(&sc->reset_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | |
| 2214 | init_timer(&sc->work_timer); |
| 2215 | sc->work_timer.data = (unsigned long) sc; |
| 2216 | sc->work_timer.function = ub_urb_timeout; |
| 2217 | |
| 2218 | ub_init_completion(&sc->work_done); |
| 2219 | sc->work_done.done = 1; /* A little yuk, but oh well... */ |
| 2220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | sc->dev = interface_to_usbdev(intf); |
| 2222 | sc->intf = intf; |
| 2223 | // sc->ifnum = intf->cur_altsetting->desc.bInterfaceNumber; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | usb_set_intfdata(intf, sc); |
| 2225 | usb_get_dev(sc->dev); |
Pete Zaitcev | 77ef6c4 | 2006-05-03 00:16:00 -0700 | [diff] [blame] | 2226 | /* |
| 2227 | * Since we give the interface struct to the block level through |
| 2228 | * disk->driverfs_dev, we have to pin it. Otherwise, block_uevent |
| 2229 | * oopses on close after a disconnect (kernels 2.6.16 and up). |
| 2230 | */ |
| 2231 | usb_get_intf(sc->intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2233 | snprintf(sc->name, 12, DRV_NAME "(%d.%d)", |
| 2234 | sc->dev->bus->busnum, sc->dev->devnum); |
| 2235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | /* XXX Verify that we can handle the device (from descriptors) */ |
| 2237 | |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 2238 | if (ub_get_pipes(sc, sc->dev, intf) != 0) |
| 2239 | goto err_dev_desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | /* |
| 2242 | * At this point, all USB initialization is done, do upper layer. |
| 2243 | * We really hate halfway initialized structures, so from the |
| 2244 | * invariants perspective, this ub_dev is fully constructed at |
| 2245 | * this point. |
| 2246 | */ |
| 2247 | |
| 2248 | /* |
| 2249 | * This is needed to clear toggles. It is a problem only if we do |
| 2250 | * `rmmod ub && modprobe ub` without disconnects, but we like that. |
| 2251 | */ |
Pete Zaitcev | c6c8883 | 2005-09-22 00:49:45 -0700 | [diff] [blame] | 2252 | #if 0 /* iPod Mini fails if we do this (big white iPod works) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | ub_probe_clear_stall(sc, sc->recv_bulk_pipe); |
| 2254 | ub_probe_clear_stall(sc, sc->send_bulk_pipe); |
Pete Zaitcev | c6c8883 | 2005-09-22 00:49:45 -0700 | [diff] [blame] | 2255 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | |
| 2257 | /* |
| 2258 | * The way this is used by the startup code is a little specific. |
| 2259 | * A SCSI check causes a USB stall. Our common case code sees it |
| 2260 | * and clears the check, after which the device is ready for use. |
| 2261 | * But if a check was not present, any command other than |
| 2262 | * TEST_UNIT_READY ends with a lockup (including REQUEST_SENSE). |
| 2263 | * |
| 2264 | * If we neglect to clear the SCSI check, the first real command fails |
| 2265 | * (which is the capacity readout). We clear that and retry, but why |
| 2266 | * causing spurious retries for no reason. |
| 2267 | * |
| 2268 | * Revalidation may start with its own TEST_UNIT_READY, but that one |
| 2269 | * has to succeed, so we clear checks with an additional one here. |
| 2270 | * In any case it's not our business how revaliadation is implemented. |
| 2271 | */ |
Pete Zaitcev | b560033 | 2006-05-25 20:08:50 -0700 | [diff] [blame] | 2272 | for (i = 0; i < 3; i++) { /* Retries for the schwag key from KS'04 */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2273 | if ((rc = ub_sync_tur(sc, NULL)) <= 0) break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | if (rc != 0x6) break; |
| 2275 | msleep(10); |
| 2276 | } |
| 2277 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2278 | nluns = 1; |
| 2279 | for (i = 0; i < 3; i++) { |
Pete Zaitcev | 11a223a | 2006-03-02 16:53:00 -0800 | [diff] [blame] | 2280 | if ((rc = ub_sync_getmaxlun(sc)) < 0) |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2281 | break; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2282 | if (rc != 0) { |
| 2283 | nluns = rc; |
| 2284 | break; |
| 2285 | } |
Pete Zaitcev | 9f793d2 | 2005-06-06 13:54:59 -0700 | [diff] [blame] | 2286 | msleep(100); |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2287 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2289 | for (i = 0; i < nluns; i++) { |
| 2290 | ub_probe_lun(sc, i); |
| 2291 | } |
| 2292 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 2294 | err_dev_desc: |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2295 | usb_set_intfdata(intf, NULL); |
Pete Zaitcev | 77ef6c4 | 2006-05-03 00:16:00 -0700 | [diff] [blame] | 2296 | usb_put_intf(sc->intf); |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2297 | usb_put_dev(sc->dev); |
| 2298 | kfree(sc); |
| 2299 | err_core: |
| 2300 | return rc; |
| 2301 | } |
| 2302 | |
| 2303 | static int ub_probe_lun(struct ub_dev *sc, int lnum) |
| 2304 | { |
| 2305 | struct ub_lun *lun; |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 2306 | struct request_queue *q; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2307 | struct gendisk *disk; |
| 2308 | int rc; |
| 2309 | |
| 2310 | rc = -ENOMEM; |
Pete Zaitcev | 29da793 | 2006-02-13 20:35:57 -0800 | [diff] [blame] | 2311 | if ((lun = kzalloc(sizeof(struct ub_lun), GFP_KERNEL)) == NULL) |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2312 | goto err_alloc; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2313 | lun->num = lnum; |
| 2314 | |
| 2315 | rc = -ENOSR; |
| 2316 | if ((lun->id = ub_id_get()) == -1) |
| 2317 | goto err_id; |
| 2318 | |
| 2319 | lun->udev = sc; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2320 | |
| 2321 | snprintf(lun->name, 16, DRV_NAME "%c(%d.%d.%d)", |
| 2322 | lun->id + 'a', sc->dev->bus->busnum, sc->dev->devnum, lun->num); |
| 2323 | |
| 2324 | lun->removable = 1; /* XXX Query this from the device */ |
| 2325 | lun->changed = 1; /* ub_revalidate clears only */ |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2326 | ub_revalidate(sc, lun); |
| 2327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | rc = -ENOMEM; |
Pete Zaitcev | 4fb729f | 2005-12-17 02:34:12 -0800 | [diff] [blame] | 2329 | if ((disk = alloc_disk(UB_PARTS_PER_LUN)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2330 | goto err_diskalloc; |
| 2331 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2332 | sprintf(disk->disk_name, DRV_NAME "%c", lun->id + 'a'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | disk->major = UB_MAJOR; |
Pete Zaitcev | 4fb729f | 2005-12-17 02:34:12 -0800 | [diff] [blame] | 2334 | disk->first_minor = lun->id * UB_PARTS_PER_LUN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | disk->fops = &ub_bd_fops; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2336 | disk->private_data = lun; |
Pete Zaitcev | 64bd845 | 2005-09-22 00:48:29 -0700 | [diff] [blame] | 2337 | disk->driverfs_dev = &sc->intf->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2338 | |
| 2339 | rc = -ENOMEM; |
Pete Zaitcev | 65b4fe5 | 2005-12-28 14:22:17 -0800 | [diff] [blame] | 2340 | if ((q = blk_init_queue(ub_request_fn, sc->lock)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2341 | goto err_blkqinit; |
| 2342 | |
| 2343 | disk->queue = q; |
| 2344 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2345 | blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH); |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 2346 | blk_queue_max_segments(q, UB_MAX_REQ_SG); |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2347 | blk_queue_segment_boundary(q, 0xffffffff); /* Dubious. */ |
Martin K. Petersen | 086fa5f | 2010-02-26 00:20:38 -0500 | [diff] [blame] | 2348 | blk_queue_max_hw_sectors(q, UB_MAX_SECTORS); |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 2349 | blk_queue_logical_block_size(q, lun->capacity.bsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | |
Pete Zaitcev | 688e9fb | 2006-05-25 20:04:54 -0700 | [diff] [blame] | 2351 | lun->disk = disk; |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2352 | q->queuedata = lun; |
Pete Zaitcev | 688e9fb | 2006-05-25 20:04:54 -0700 | [diff] [blame] | 2353 | list_add(&lun->link, &sc->luns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2354 | |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2355 | set_capacity(disk, lun->capacity.nsec); |
| 2356 | if (lun->removable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | disk->flags |= GENHD_FL_REMOVABLE; |
| 2358 | |
| 2359 | add_disk(disk); |
| 2360 | |
| 2361 | return 0; |
| 2362 | |
| 2363 | err_blkqinit: |
| 2364 | put_disk(disk); |
| 2365 | err_diskalloc: |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2366 | ub_id_put(lun->id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | err_id: |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2368 | kfree(lun); |
| 2369 | err_alloc: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | return rc; |
| 2371 | } |
| 2372 | |
| 2373 | static void ub_disconnect(struct usb_interface *intf) |
| 2374 | { |
| 2375 | struct ub_dev *sc = usb_get_intfdata(intf); |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2376 | struct ub_lun *lun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | unsigned long flags; |
| 2378 | |
| 2379 | /* |
| 2380 | * Prevent ub_bd_release from pulling the rug from under us. |
| 2381 | * XXX This is starting to look like a kref. |
| 2382 | * XXX Why not to take this ref at probe time? |
| 2383 | */ |
| 2384 | spin_lock_irqsave(&ub_lock, flags); |
| 2385 | sc->openc++; |
| 2386 | spin_unlock_irqrestore(&ub_lock, flags); |
| 2387 | |
| 2388 | /* |
Pete Zaitcev | 9029b17 | 2008-04-19 14:45:24 -0700 | [diff] [blame] | 2389 | * Fence stall clearings, operations triggered by unlinkings and so on. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | * We do not attempt to unlink any URBs, because we do not trust the |
| 2391 | * unlink paths in HC drivers. Also, we get -84 upon disconnect anyway. |
| 2392 | */ |
| 2393 | atomic_set(&sc->poison, 1); |
| 2394 | |
| 2395 | /* |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 2396 | * Wait for reset to end, if any. |
| 2397 | */ |
| 2398 | wait_event(sc->reset_wait, !sc->reset); |
| 2399 | |
| 2400 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2401 | * Blow away queued commands. |
| 2402 | * |
| 2403 | * Actually, this never works, because before we get here |
| 2404 | * the HCD terminates outstanding URB(s). It causes our |
| 2405 | * SCSI command queue to advance, commands fail to submit, |
| 2406 | * and the whole queue drains. So, we just use this code to |
| 2407 | * print warnings. |
| 2408 | */ |
Pete Zaitcev | 65b4fe5 | 2005-12-28 14:22:17 -0800 | [diff] [blame] | 2409 | spin_lock_irqsave(sc->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | { |
| 2411 | struct ub_scsi_cmd *cmd; |
| 2412 | int cnt = 0; |
Pete Zaitcev | 2c26c9e | 2005-12-17 02:16:43 -0800 | [diff] [blame] | 2413 | while ((cmd = ub_cmdq_peek(sc)) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | cmd->error = -ENOTCONN; |
| 2415 | cmd->state = UB_CMDST_DONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2416 | ub_cmdq_pop(sc); |
| 2417 | (*cmd->done)(sc, cmd); |
| 2418 | cnt++; |
| 2419 | } |
| 2420 | if (cnt != 0) { |
| 2421 | printk(KERN_WARNING "%s: " |
| 2422 | "%d was queued after shutdown\n", sc->name, cnt); |
| 2423 | } |
| 2424 | } |
Pete Zaitcev | 65b4fe5 | 2005-12-28 14:22:17 -0800 | [diff] [blame] | 2425 | spin_unlock_irqrestore(sc->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | |
| 2427 | /* |
| 2428 | * Unregister the upper layer. |
| 2429 | */ |
Matthias Kaehlcke | a69228d | 2007-07-09 12:03:07 -0700 | [diff] [blame] | 2430 | list_for_each_entry(lun, &sc->luns, link) { |
Pete Zaitcev | 688e9fb | 2006-05-25 20:04:54 -0700 | [diff] [blame] | 2431 | del_gendisk(lun->disk); |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2432 | /* |
| 2433 | * I wish I could do: |
Nick Piggin | 75ad23b | 2008-04-29 14:48:33 +0200 | [diff] [blame] | 2434 | * queue_flag_set(QUEUE_FLAG_DEAD, q); |
Pete Zaitcev | f480007 | 2005-05-01 16:05:40 -0700 | [diff] [blame] | 2435 | * As it is, we rely on our internal poisoning and let |
| 2436 | * the upper levels to spin furiously failing all the I/O. |
| 2437 | */ |
| 2438 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | |
| 2440 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | * Testing for -EINPROGRESS is always a bug, so we are bending |
| 2442 | * the rules a little. |
| 2443 | */ |
Pete Zaitcev | 65b4fe5 | 2005-12-28 14:22:17 -0800 | [diff] [blame] | 2444 | spin_lock_irqsave(sc->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | if (sc->work_urb.status == -EINPROGRESS) { /* janitors: ignore */ |
| 2446 | printk(KERN_WARNING "%s: " |
| 2447 | "URB is active after disconnect\n", sc->name); |
| 2448 | } |
Pete Zaitcev | 65b4fe5 | 2005-12-28 14:22:17 -0800 | [diff] [blame] | 2449 | spin_unlock_irqrestore(sc->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | |
| 2451 | /* |
Pete Zaitcev | 9029b17 | 2008-04-19 14:45:24 -0700 | [diff] [blame] | 2452 | * There is virtually no chance that other CPU runs a timeout so long |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2453 | * after ub_urb_complete should have called del_timer, but only if HCD |
| 2454 | * didn't forget to deliver a callback on unlink. |
| 2455 | */ |
| 2456 | del_timer_sync(&sc->work_timer); |
| 2457 | |
| 2458 | /* |
| 2459 | * At this point there must be no commands coming from anyone |
| 2460 | * and no URBs left in transit. |
| 2461 | */ |
| 2462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | ub_put(sc); |
| 2464 | } |
| 2465 | |
| 2466 | static struct usb_driver ub_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2467 | .name = "ub", |
| 2468 | .probe = ub_probe, |
| 2469 | .disconnect = ub_disconnect, |
| 2470 | .id_table = ub_usb_ids, |
Pete Zaitcev | d73b7aff | 2008-11-10 21:11:11 -0700 | [diff] [blame] | 2471 | .pre_reset = ub_pre_reset, |
| 2472 | .post_reset = ub_post_reset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | }; |
| 2474 | |
| 2475 | static int __init ub_init(void) |
| 2476 | { |
| 2477 | int rc; |
Pete Zaitcev | 65b4fe5 | 2005-12-28 14:22:17 -0800 | [diff] [blame] | 2478 | int i; |
| 2479 | |
| 2480 | for (i = 0; i < UB_QLOCK_NUM; i++) |
| 2481 | spin_lock_init(&ub_qlockv[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | if ((rc = register_blkdev(UB_MAJOR, DRV_NAME)) != 0) |
| 2484 | goto err_regblkdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | |
| 2486 | if ((rc = usb_register(&ub_driver)) != 0) |
| 2487 | goto err_register; |
| 2488 | |
Pete Zaitcev | a00828e | 2005-10-22 20:15:09 -0700 | [diff] [blame] | 2489 | usb_usual_set_present(USB_US_TYPE_UB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2490 | return 0; |
| 2491 | |
| 2492 | err_register: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | unregister_blkdev(UB_MAJOR, DRV_NAME); |
| 2494 | err_regblkdev: |
| 2495 | return rc; |
| 2496 | } |
| 2497 | |
| 2498 | static void __exit ub_exit(void) |
| 2499 | { |
| 2500 | usb_deregister(&ub_driver); |
| 2501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2502 | unregister_blkdev(UB_MAJOR, DRV_NAME); |
Pete Zaitcev | a00828e | 2005-10-22 20:15:09 -0700 | [diff] [blame] | 2503 | usb_usual_clear_present(USB_US_TYPE_UB); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2504 | } |
| 2505 | |
| 2506 | module_init(ub_init); |
| 2507 | module_exit(ub_exit); |
| 2508 | |
| 2509 | MODULE_LICENSE("GPL"); |