ub.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486
  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 const 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_segments(q, UB_MAX_REQ_SG);
  1973. blk_queue_segment_boundary(q, 0xffffffff); /* Dubious. */
  1974. blk_queue_max_hw_sectors(q, UB_MAX_SECTORS);
  1975. blk_queue_logical_block_size(q, lun->capacity.bsize);
  1976. lun->disk = disk;
  1977. q->queuedata = lun;
  1978. list_add(&lun->link, &sc->luns);
  1979. set_capacity(disk, lun->capacity.nsec);
  1980. if (lun->removable)
  1981. disk->flags |= GENHD_FL_REMOVABLE;
  1982. add_disk(disk);
  1983. return 0;
  1984. err_blkqinit:
  1985. put_disk(disk);
  1986. err_diskalloc:
  1987. ub_id_put(lun->id);
  1988. err_id:
  1989. kfree(lun);
  1990. err_alloc:
  1991. return rc;
  1992. }
  1993. static void ub_disconnect(struct usb_interface *intf)
  1994. {
  1995. struct ub_dev *sc = usb_get_intfdata(intf);
  1996. struct ub_lun *lun;
  1997. unsigned long flags;
  1998. /*
  1999. * Prevent ub_bd_release from pulling the rug from under us.
  2000. * XXX This is starting to look like a kref.
  2001. * XXX Why not to take this ref at probe time?
  2002. */
  2003. spin_lock_irqsave(&ub_lock, flags);
  2004. sc->openc++;
  2005. spin_unlock_irqrestore(&ub_lock, flags);
  2006. /*
  2007. * Fence stall clearings, operations triggered by unlinkings and so on.
  2008. * We do not attempt to unlink any URBs, because we do not trust the
  2009. * unlink paths in HC drivers. Also, we get -84 upon disconnect anyway.
  2010. */
  2011. atomic_set(&sc->poison, 1);
  2012. /*
  2013. * Wait for reset to end, if any.
  2014. */
  2015. wait_event(sc->reset_wait, !sc->reset);
  2016. /*
  2017. * Blow away queued commands.
  2018. *
  2019. * Actually, this never works, because before we get here
  2020. * the HCD terminates outstanding URB(s). It causes our
  2021. * SCSI command queue to advance, commands fail to submit,
  2022. * and the whole queue drains. So, we just use this code to
  2023. * print warnings.
  2024. */
  2025. spin_lock_irqsave(sc->lock, flags);
  2026. {
  2027. struct ub_scsi_cmd *cmd;
  2028. int cnt = 0;
  2029. while ((cmd = ub_cmdq_peek(sc)) != NULL) {
  2030. cmd->error = -ENOTCONN;
  2031. cmd->state = UB_CMDST_DONE;
  2032. ub_cmdq_pop(sc);
  2033. (*cmd->done)(sc, cmd);
  2034. cnt++;
  2035. }
  2036. if (cnt != 0) {
  2037. printk(KERN_WARNING "%s: "
  2038. "%d was queued after shutdown\n", sc->name, cnt);
  2039. }
  2040. }
  2041. spin_unlock_irqrestore(sc->lock, flags);
  2042. /*
  2043. * Unregister the upper layer.
  2044. */
  2045. list_for_each_entry(lun, &sc->luns, link) {
  2046. del_gendisk(lun->disk);
  2047. /*
  2048. * I wish I could do:
  2049. * queue_flag_set(QUEUE_FLAG_DEAD, q);
  2050. * As it is, we rely on our internal poisoning and let
  2051. * the upper levels to spin furiously failing all the I/O.
  2052. */
  2053. }
  2054. /*
  2055. * Testing for -EINPROGRESS is always a bug, so we are bending
  2056. * the rules a little.
  2057. */
  2058. spin_lock_irqsave(sc->lock, flags);
  2059. if (sc->work_urb.status == -EINPROGRESS) { /* janitors: ignore */
  2060. printk(KERN_WARNING "%s: "
  2061. "URB is active after disconnect\n", sc->name);
  2062. }
  2063. spin_unlock_irqrestore(sc->lock, flags);
  2064. /*
  2065. * There is virtually no chance that other CPU runs a timeout so long
  2066. * after ub_urb_complete should have called del_timer, but only if HCD
  2067. * didn't forget to deliver a callback on unlink.
  2068. */
  2069. del_timer_sync(&sc->work_timer);
  2070. /*
  2071. * At this point there must be no commands coming from anyone
  2072. * and no URBs left in transit.
  2073. */
  2074. ub_put(sc);
  2075. }
  2076. static struct usb_driver ub_driver = {
  2077. .name = "ub",
  2078. .probe = ub_probe,
  2079. .disconnect = ub_disconnect,
  2080. .id_table = ub_usb_ids,
  2081. .pre_reset = ub_pre_reset,
  2082. .post_reset = ub_post_reset,
  2083. };
  2084. static int __init ub_init(void)
  2085. {
  2086. int rc;
  2087. int i;
  2088. for (i = 0; i < UB_QLOCK_NUM; i++)
  2089. spin_lock_init(&ub_qlockv[i]);
  2090. if ((rc = register_blkdev(UB_MAJOR, DRV_NAME)) != 0)
  2091. goto err_regblkdev;
  2092. if ((rc = usb_register(&ub_driver)) != 0)
  2093. goto err_register;
  2094. usb_usual_set_present(USB_US_TYPE_UB);
  2095. return 0;
  2096. err_register:
  2097. unregister_blkdev(UB_MAJOR, DRV_NAME);
  2098. err_regblkdev:
  2099. return rc;
  2100. }
  2101. static void __exit ub_exit(void)
  2102. {
  2103. usb_deregister(&ub_driver);
  2104. unregister_blkdev(UB_MAJOR, DRV_NAME);
  2105. usb_usual_clear_present(USB_US_TYPE_UB);
  2106. }
  2107. module_init(ub_init);
  2108. module_exit(ub_exit);
  2109. MODULE_LICENSE("GPL");