ub.c 61 KB

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