ub.c 62 KB

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