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