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