ub.c 62 KB

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