ub.c 62 KB

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