aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/block/ub.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/block/ub.c')
-rw-r--r--drivers/block/ub.c404
1 files changed, 287 insertions, 117 deletions
diff --git a/drivers/block/ub.c b/drivers/block/ub.c
index c7a28f5be42f..ba05e31709d5 100644
--- a/drivers/block/ub.c
+++ b/drivers/block/ub.c
@@ -9,7 +9,6 @@
9 * 9 *
10 * TODO (sorted by decreasing priority) 10 * TODO (sorted by decreasing priority)
11 * -- Kill first_open (Al Viro fixed the block layer now) 11 * -- Kill first_open (Al Viro fixed the block layer now)
12 * -- Do resets with usb_device_reset (needs a thread context, use khubd)
13 * -- set readonly flag for CDs, set removable flag for CF readers 12 * -- set readonly flag for CDs, set removable flag for CF readers
14 * -- do inquiry and verify we got a disk and not a tape (for LUN mismatch) 13 * -- do inquiry and verify we got a disk and not a tape (for LUN mismatch)
15 * -- special case some senses, e.g. 3a/0 -> no media present, reduce retries 14 * -- special case some senses, e.g. 3a/0 -> no media present, reduce retries
@@ -236,6 +235,13 @@ struct ub_scsi_cmd {
236 void *back; 235 void *back;
237}; 236};
238 237
238struct ub_request {
239 struct request *rq;
240 unsigned int current_try;
241 unsigned int nsg; /* sgv[nsg] */
242 struct scatterlist sgv[UB_MAX_REQ_SG];
243};
244
239/* 245/*
240 */ 246 */
241struct ub_capacity { 247struct ub_capacity {
@@ -331,6 +337,8 @@ struct ub_lun {
331 int readonly; 337 int readonly;
332 int first_open; /* Kludge. See ub_bd_open. */ 338 int first_open; /* Kludge. See ub_bd_open. */
333 339
340 struct ub_request urq;
341
334 /* Use Ingo's mempool if or when we have more than one command. */ 342 /* Use Ingo's mempool if or when we have more than one command. */
335 /* 343 /*
336 * Currently we never need more than one command for the whole device. 344 * Currently we never need more than one command for the whole device.
@@ -351,6 +359,7 @@ struct ub_dev {
351 atomic_t poison; /* The USB device is disconnected */ 359 atomic_t poison; /* The USB device is disconnected */
352 int openc; /* protected by ub_lock! */ 360 int openc; /* protected by ub_lock! */
353 /* kref is too implicit for our taste */ 361 /* kref is too implicit for our taste */
362 int reset; /* Reset is running */
354 unsigned int tagcnt; 363 unsigned int tagcnt;
355 char name[12]; 364 char name[12];
356 struct usb_device *dev; 365 struct usb_device *dev;
@@ -378,6 +387,9 @@ struct ub_dev {
378 struct bulk_cs_wrap work_bcs; 387 struct bulk_cs_wrap work_bcs;
379 struct usb_ctrlrequest work_cr; 388 struct usb_ctrlrequest work_cr;
380 389
390 struct work_struct reset_work;
391 wait_queue_head_t reset_wait;
392
381 int sg_stat[6]; 393 int sg_stat[6];
382 struct ub_scsi_trace tr; 394 struct ub_scsi_trace tr;
383}; 395};
@@ -386,12 +398,14 @@ struct ub_dev {
386 */ 398 */
387static void ub_cleanup(struct ub_dev *sc); 399static void ub_cleanup(struct ub_dev *sc);
388static int ub_request_fn_1(struct ub_lun *lun, struct request *rq); 400static int ub_request_fn_1(struct ub_lun *lun, struct request *rq);
389static int ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun, 401static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
390 struct ub_scsi_cmd *cmd, struct request *rq); 402 struct ub_scsi_cmd *cmd, struct ub_request *urq);
391static int ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun, 403static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
392 struct ub_scsi_cmd *cmd, struct request *rq); 404 struct ub_scsi_cmd *cmd, struct ub_request *urq);
393static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd); 405static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
394static void ub_end_rq(struct request *rq, int uptodate); 406static void ub_end_rq(struct request *rq, int uptodate);
407static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
408 struct ub_request *urq, struct ub_scsi_cmd *cmd);
395static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd); 409static int ub_submit_scsi(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
396static void ub_urb_complete(struct urb *urb, struct pt_regs *pt); 410static void ub_urb_complete(struct urb *urb, struct pt_regs *pt);
397static void ub_scsi_action(unsigned long _dev); 411static void ub_scsi_action(unsigned long _dev);
@@ -406,6 +420,8 @@ static void ub_state_sense(struct ub_dev *sc, struct ub_scsi_cmd *cmd);
406static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd, 420static int ub_submit_clear_stall(struct ub_dev *sc, struct ub_scsi_cmd *cmd,
407 int stalled_pipe); 421 int stalled_pipe);
408static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd); 422static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd);
423static void ub_reset_enter(struct ub_dev *sc);
424static void ub_reset_task(void *arg);
409static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun); 425static int ub_sync_tur(struct ub_dev *sc, struct ub_lun *lun);
410static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun, 426static int ub_sync_read_cap(struct ub_dev *sc, struct ub_lun *lun,
411 struct ub_capacity *ret); 427 struct ub_capacity *ret);
@@ -518,6 +534,9 @@ static ssize_t ub_diag_show(struct device *dev, struct device_attribute *attr,
518 spin_lock_irqsave(&sc->lock, flags); 534 spin_lock_irqsave(&sc->lock, flags);
519 535
520 cnt += sprintf(page + cnt, 536 cnt += sprintf(page + cnt,
537 "poison %d reset %d\n",
538 atomic_read(&sc->poison), sc->reset);
539 cnt += sprintf(page + cnt,
521 "qlen %d qmax %d\n", 540 "qlen %d qmax %d\n",
522 sc->cmd_queue.qlen, sc->cmd_queue.qmax); 541 sc->cmd_queue.qlen, sc->cmd_queue.qmax);
523 cnt += sprintf(page + cnt, 542 cnt += sprintf(page + cnt,
@@ -766,7 +785,8 @@ static int ub_request_fn_1(struct ub_lun *lun, struct request *rq)
766{ 785{
767 struct ub_dev *sc = lun->udev; 786 struct ub_dev *sc = lun->udev;
768 struct ub_scsi_cmd *cmd; 787 struct ub_scsi_cmd *cmd;
769 int rc; 788 struct ub_request *urq;
789 int n_elem;
770 790
771 if (atomic_read(&sc->poison) || lun->changed) { 791 if (atomic_read(&sc->poison) || lun->changed) {
772 blkdev_dequeue_request(rq); 792 blkdev_dequeue_request(rq);
@@ -774,65 +794,70 @@ static int ub_request_fn_1(struct ub_lun *lun, struct request *rq)
774 return 0; 794 return 0;
775 } 795 }
776 796
797 if (lun->urq.rq != NULL)
798 return -1;
777 if ((cmd = ub_get_cmd(lun)) == NULL) 799 if ((cmd = ub_get_cmd(lun)) == NULL)
778 return -1; 800 return -1;
779 memset(cmd, 0, sizeof(struct ub_scsi_cmd)); 801 memset(cmd, 0, sizeof(struct ub_scsi_cmd));
780 802
781 blkdev_dequeue_request(rq); 803 blkdev_dequeue_request(rq);
804
805 urq = &lun->urq;
806 memset(urq, 0, sizeof(struct ub_request));
807 urq->rq = rq;
808
809 /*
810 * get scatterlist from block layer
811 */
812 n_elem = blk_rq_map_sg(lun->disk->queue, rq, &urq->sgv[0]);
813 if (n_elem < 0) {
814 printk(KERN_INFO "%s: failed request map (%d)\n",
815 lun->name, n_elem); /* P3 */
816 goto drop;
817 }
818 if (n_elem > UB_MAX_REQ_SG) { /* Paranoia */
819 printk(KERN_WARNING "%s: request with %d segments\n",
820 lun->name, n_elem);
821 goto drop;
822 }
823 urq->nsg = n_elem;
824 sc->sg_stat[n_elem < 5 ? n_elem : 5]++;
825
782 if (blk_pc_request(rq)) { 826 if (blk_pc_request(rq)) {
783 rc = ub_cmd_build_packet(sc, lun, cmd, rq); 827 ub_cmd_build_packet(sc, lun, cmd, urq);
784 } else { 828 } else {
785 rc = ub_cmd_build_block(sc, lun, cmd, rq); 829 ub_cmd_build_block(sc, lun, cmd, urq);
786 }
787 if (rc != 0) {
788 ub_put_cmd(lun, cmd);
789 ub_end_rq(rq, 0);
790 return 0;
791 } 830 }
792 cmd->state = UB_CMDST_INIT; 831 cmd->state = UB_CMDST_INIT;
793 cmd->lun = lun; 832 cmd->lun = lun;
794 cmd->done = ub_rw_cmd_done; 833 cmd->done = ub_rw_cmd_done;
795 cmd->back = rq; 834 cmd->back = urq;
796 835
797 cmd->tag = sc->tagcnt++; 836 cmd->tag = sc->tagcnt++;
798 if (ub_submit_scsi(sc, cmd) != 0) { 837 if (ub_submit_scsi(sc, cmd) != 0)
799 ub_put_cmd(lun, cmd); 838 goto drop;
800 ub_end_rq(rq, 0); 839
801 return 0; 840 return 0;
802 }
803 841
842drop:
843 ub_put_cmd(lun, cmd);
844 ub_end_rq(rq, 0);
804 return 0; 845 return 0;
805} 846}
806 847
807static int ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun, 848static void ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
808 struct ub_scsi_cmd *cmd, struct request *rq) 849 struct ub_scsi_cmd *cmd, struct ub_request *urq)
809{ 850{
810 int ub_dir; 851 struct request *rq = urq->rq;
811 int n_elem;
812 unsigned int block, nblks; 852 unsigned int block, nblks;
813 853
814 if (rq_data_dir(rq) == WRITE) 854 if (rq_data_dir(rq) == WRITE)
815 ub_dir = UB_DIR_WRITE; 855 cmd->dir = UB_DIR_WRITE;
816 else 856 else
817 ub_dir = UB_DIR_READ; 857 cmd->dir = UB_DIR_READ;
818 cmd->dir = ub_dir;
819 858
820 /* 859 cmd->nsg = urq->nsg;
821 * get scatterlist from block layer 860 memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
822 */
823 n_elem = blk_rq_map_sg(lun->disk->queue, rq, &cmd->sgv[0]);
824 if (n_elem <= 0) {
825 printk(KERN_INFO "%s: failed request map (%d)\n",
826 sc->name, n_elem); /* P3 */
827 return -1; /* request with no s/g entries? */
828 }
829 if (n_elem > UB_MAX_REQ_SG) { /* Paranoia */
830 printk(KERN_WARNING "%s: request with %d segments\n",
831 sc->name, n_elem);
832 return -1;
833 }
834 cmd->nsg = n_elem;
835 sc->sg_stat[n_elem < 5 ? n_elem : 5]++;
836 861
837 /* 862 /*
838 * build the command 863 * build the command
@@ -843,7 +868,7 @@ static int ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
843 block = rq->sector >> lun->capacity.bshift; 868 block = rq->sector >> lun->capacity.bshift;
844 nblks = rq->nr_sectors >> lun->capacity.bshift; 869 nblks = rq->nr_sectors >> lun->capacity.bshift;
845 870
846 cmd->cdb[0] = (ub_dir == UB_DIR_READ)? READ_10: WRITE_10; 871 cmd->cdb[0] = (cmd->dir == UB_DIR_READ)? READ_10: WRITE_10;
847 /* 10-byte uses 4 bytes of LBA: 2147483648KB, 2097152MB, 2048GB */ 872 /* 10-byte uses 4 bytes of LBA: 2147483648KB, 2097152MB, 2048GB */
848 cmd->cdb[2] = block >> 24; 873 cmd->cdb[2] = block >> 24;
849 cmd->cdb[3] = block >> 16; 874 cmd->cdb[3] = block >> 16;
@@ -854,14 +879,12 @@ static int ub_cmd_build_block(struct ub_dev *sc, struct ub_lun *lun,
854 cmd->cdb_len = 10; 879 cmd->cdb_len = 10;
855 880
856 cmd->len = rq->nr_sectors * 512; 881 cmd->len = rq->nr_sectors * 512;
857
858 return 0;
859} 882}
860 883
861static int ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun, 884static void ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
862 struct ub_scsi_cmd *cmd, struct request *rq) 885 struct ub_scsi_cmd *cmd, struct ub_request *urq)
863{ 886{
864 int n_elem; 887 struct request *rq = urq->rq;
865 888
866 if (rq->data_len == 0) { 889 if (rq->data_len == 0) {
867 cmd->dir = UB_DIR_NONE; 890 cmd->dir = UB_DIR_NONE;
@@ -870,40 +893,26 @@ static int ub_cmd_build_packet(struct ub_dev *sc, struct ub_lun *lun,
870 cmd->dir = UB_DIR_WRITE; 893 cmd->dir = UB_DIR_WRITE;
871 else 894 else
872 cmd->dir = UB_DIR_READ; 895 cmd->dir = UB_DIR_READ;
873
874 } 896 }
875 897
876 /* 898 cmd->nsg = urq->nsg;
877 * get scatterlist from block layer 899 memcpy(cmd->sgv, urq->sgv, sizeof(struct scatterlist) * cmd->nsg);
878 */
879 n_elem = blk_rq_map_sg(lun->disk->queue, rq, &cmd->sgv[0]);
880 if (n_elem < 0) {
881 printk(KERN_INFO "%s: failed request map (%d)\n",
882 sc->name, n_elem); /* P3 */
883 return -1;
884 }
885 if (n_elem > UB_MAX_REQ_SG) { /* Paranoia */
886 printk(KERN_WARNING "%s: request with %d segments\n",
887 sc->name, n_elem);
888 return -1;
889 }
890 cmd->nsg = n_elem;
891 sc->sg_stat[n_elem < 5 ? n_elem : 5]++;
892 900
893 memcpy(&cmd->cdb, rq->cmd, rq->cmd_len); 901 memcpy(&cmd->cdb, rq->cmd, rq->cmd_len);
894 cmd->cdb_len = rq->cmd_len; 902 cmd->cdb_len = rq->cmd_len;
895 903
896 cmd->len = rq->data_len; 904 cmd->len = rq->data_len;
897
898 return 0;
899} 905}
900 906
901static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd) 907static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
902{ 908{
903 struct request *rq = cmd->back;
904 struct ub_lun *lun = cmd->lun; 909 struct ub_lun *lun = cmd->lun;
910 struct ub_request *urq = cmd->back;
911 struct request *rq;
905 int uptodate; 912 int uptodate;
906 913
914 rq = urq->rq;
915
907 if (cmd->error == 0) { 916 if (cmd->error == 0) {
908 uptodate = 1; 917 uptodate = 1;
909 918
@@ -924,9 +933,16 @@ static void ub_rw_cmd_done(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
924 rq->errors = SAM_STAT_CHECK_CONDITION; 933 rq->errors = SAM_STAT_CHECK_CONDITION;
925 else 934 else
926 rq->errors = DID_ERROR << 16; 935 rq->errors = DID_ERROR << 16;
936 } else {
937 if (cmd->error == -EIO) {
938 if (ub_rw_cmd_retry(sc, lun, urq, cmd) == 0)
939 return;
940 }
927 } 941 }
928 } 942 }
929 943
944 urq->rq = NULL;
945
930 ub_put_cmd(lun, cmd); 946 ub_put_cmd(lun, cmd);
931 ub_end_rq(rq, uptodate); 947 ub_end_rq(rq, uptodate);
932 blk_start_queue(lun->disk->queue); 948 blk_start_queue(lun->disk->queue);
@@ -941,6 +957,41 @@ static void ub_end_rq(struct request *rq, int uptodate)
941 end_that_request_last(rq); 957 end_that_request_last(rq);
942} 958}
943 959
960static int ub_rw_cmd_retry(struct ub_dev *sc, struct ub_lun *lun,
961 struct ub_request *urq, struct ub_scsi_cmd *cmd)
962{
963
964 if (atomic_read(&sc->poison))
965 return -ENXIO;
966
967 ub_reset_enter(sc);
968
969 if (urq->current_try >= 3)
970 return -EIO;
971 urq->current_try++;
972 /* P3 */ printk("%s: dir %c len/act %d/%d "
973 "[sense %x %02x %02x] retry %d\n",
974 sc->name, UB_DIR_CHAR(cmd->dir), cmd->len, cmd->act_len,
975 cmd->key, cmd->asc, cmd->ascq, urq->current_try);
976
977 memset(cmd, 0, sizeof(struct ub_scsi_cmd));
978 ub_cmd_build_block(sc, lun, cmd, urq);
979
980 cmd->state = UB_CMDST_INIT;
981 cmd->lun = lun;
982 cmd->done = ub_rw_cmd_done;
983 cmd->back = urq;
984
985 cmd->tag = sc->tagcnt++;
986
987#if 0 /* Wasteful */
988 return ub_submit_scsi(sc, cmd);
989#else
990 ub_cmdq_add(sc, cmd);
991 return 0;
992#endif
993}
994
944/* 995/*
945 * Submit a regular SCSI operation (not an auto-sense). 996 * Submit a regular SCSI operation (not an auto-sense).
946 * 997 *
@@ -1071,7 +1122,7 @@ static void ub_scsi_dispatch(struct ub_dev *sc)
1071 struct ub_scsi_cmd *cmd; 1122 struct ub_scsi_cmd *cmd;
1072 int rc; 1123 int rc;
1073 1124
1074 while ((cmd = ub_cmdq_peek(sc)) != NULL) { 1125 while (!sc->reset && (cmd = ub_cmdq_peek(sc)) != NULL) {
1075 if (cmd->state == UB_CMDST_DONE) { 1126 if (cmd->state == UB_CMDST_DONE) {
1076 ub_cmdq_pop(sc); 1127 ub_cmdq_pop(sc);
1077 (*cmd->done)(sc, cmd); 1128 (*cmd->done)(sc, cmd);
@@ -1094,11 +1145,12 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1094{ 1145{
1095 struct urb *urb = &sc->work_urb; 1146 struct urb *urb = &sc->work_urb;
1096 struct bulk_cs_wrap *bcs; 1147 struct bulk_cs_wrap *bcs;
1148 int len;
1097 int rc; 1149 int rc;
1098 1150
1099 if (atomic_read(&sc->poison)) { 1151 if (atomic_read(&sc->poison)) {
1100 /* A little too simplistic, I feel... */ 1152 ub_state_done(sc, cmd, -ENODEV);
1101 goto Bad_End; 1153 return;
1102 } 1154 }
1103 1155
1104 if (cmd->state == UB_CMDST_CLEAR) { 1156 if (cmd->state == UB_CMDST_CLEAR) {
@@ -1106,7 +1158,6 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1106 /* 1158 /*
1107 * STALL while clearning STALL. 1159 * STALL while clearning STALL.
1108 * The control pipe clears itself - nothing to do. 1160 * The control pipe clears itself - nothing to do.
1109 * XXX Might try to reset the device here and retry.
1110 */ 1161 */
1111 printk(KERN_NOTICE "%s: stall on control pipe\n", 1162 printk(KERN_NOTICE "%s: stall on control pipe\n",
1112 sc->name); 1163 sc->name);
@@ -1125,11 +1176,6 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1125 1176
1126 } else if (cmd->state == UB_CMDST_CLR2STS) { 1177 } else if (cmd->state == UB_CMDST_CLR2STS) {
1127 if (urb->status == -EPIPE) { 1178 if (urb->status == -EPIPE) {
1128 /*
1129 * STALL while clearning STALL.
1130 * The control pipe clears itself - nothing to do.
1131 * XXX Might try to reset the device here and retry.
1132 */
1133 printk(KERN_NOTICE "%s: stall on control pipe\n", 1179 printk(KERN_NOTICE "%s: stall on control pipe\n",
1134 sc->name); 1180 sc->name);
1135 goto Bad_End; 1181 goto Bad_End;
@@ -1147,11 +1193,6 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1147 1193
1148 } else if (cmd->state == UB_CMDST_CLRRS) { 1194 } else if (cmd->state == UB_CMDST_CLRRS) {
1149 if (urb->status == -EPIPE) { 1195 if (urb->status == -EPIPE) {
1150 /*
1151 * STALL while clearning STALL.
1152 * The control pipe clears itself - nothing to do.
1153 * XXX Might try to reset the device here and retry.
1154 */
1155 printk(KERN_NOTICE "%s: stall on control pipe\n", 1196 printk(KERN_NOTICE "%s: stall on control pipe\n",
1156 sc->name); 1197 sc->name);
1157 goto Bad_End; 1198 goto Bad_End;
@@ -1168,7 +1209,12 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1168 ub_state_stat_counted(sc, cmd); 1209 ub_state_stat_counted(sc, cmd);
1169 1210
1170 } else if (cmd->state == UB_CMDST_CMD) { 1211 } else if (cmd->state == UB_CMDST_CMD) {
1171 if (urb->status == -EPIPE) { 1212 switch (urb->status) {
1213 case 0:
1214 break;
1215 case -EOVERFLOW:
1216 goto Bad_End;
1217 case -EPIPE:
1172 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe); 1218 rc = ub_submit_clear_stall(sc, cmd, sc->last_pipe);
1173 if (rc != 0) { 1219 if (rc != 0) {
1174 printk(KERN_NOTICE "%s: " 1220 printk(KERN_NOTICE "%s: "
@@ -1178,17 +1224,20 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1178 * This is typically ENOMEM or some other such shit. 1224 * This is typically ENOMEM or some other such shit.
1179 * Retrying is pointless. Just do Bad End on it... 1225 * Retrying is pointless. Just do Bad End on it...
1180 */ 1226 */
1181 goto Bad_End; 1227 ub_state_done(sc, cmd, rc);
1228 return;
1182 } 1229 }
1183 cmd->state = UB_CMDST_CLEAR; 1230 cmd->state = UB_CMDST_CLEAR;
1184 ub_cmdtr_state(sc, cmd); 1231 ub_cmdtr_state(sc, cmd);
1185 return; 1232 return;
1186 } 1233 case -ESHUTDOWN: /* unplug */
1187 if (urb->status != 0) { 1234 case -EILSEQ: /* unplug timeout on uhci */
1235 ub_state_done(sc, cmd, -ENODEV);
1236 return;
1237 default:
1188 goto Bad_End; 1238 goto Bad_End;
1189 } 1239 }
1190 if (urb->actual_length != US_BULK_CB_WRAP_LEN) { 1240 if (urb->actual_length != US_BULK_CB_WRAP_LEN) {
1191 /* XXX Must do reset here to unconfuse the device */
1192 goto Bad_End; 1241 goto Bad_End;
1193 } 1242 }
1194 1243
@@ -1207,11 +1256,8 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1207 printk(KERN_NOTICE "%s: " 1256 printk(KERN_NOTICE "%s: "
1208 "unable to submit clear (%d)\n", 1257 "unable to submit clear (%d)\n",
1209 sc->name, rc); 1258 sc->name, rc);
1210 /* 1259 ub_state_done(sc, cmd, rc);
1211 * This is typically ENOMEM or some other such shit. 1260 return;
1212 * Retrying is pointless. Just do Bad End on it...
1213 */
1214 goto Bad_End;
1215 } 1261 }
1216 cmd->state = UB_CMDST_CLR2STS; 1262 cmd->state = UB_CMDST_CLR2STS;
1217 ub_cmdtr_state(sc, cmd); 1263 ub_cmdtr_state(sc, cmd);
@@ -1220,14 +1266,50 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1220 if (urb->status == -EOVERFLOW) { 1266 if (urb->status == -EOVERFLOW) {
1221 /* 1267 /*
1222 * A babble? Failure, but we must transfer CSW now. 1268 * A babble? Failure, but we must transfer CSW now.
1223 * XXX This is going to end in perpetual babble. Reset.
1224 */ 1269 */
1225 cmd->error = -EOVERFLOW; /* A cheap trick... */ 1270 cmd->error = -EOVERFLOW; /* A cheap trick... */
1226 ub_state_stat(sc, cmd); 1271 ub_state_stat(sc, cmd);
1227 return; 1272 return;
1228 } 1273 }
1229 if (urb->status != 0) 1274
1230 goto Bad_End; 1275 if (cmd->dir == UB_DIR_WRITE) {
1276 /*
1277 * Do not continue writes in case of a failure.
1278 * Doing so would cause sectors to be mixed up,
1279 * which is worse than sectors lost.
1280 *
1281 * We must try to read the CSW, or many devices
1282 * get confused.
1283 */
1284 len = urb->actual_length;
1285 if (urb->status != 0 ||
1286 len != cmd->sgv[cmd->current_sg].length) {
1287 cmd->act_len += len;
1288 ub_cmdtr_act_len(sc, cmd);
1289
1290 cmd->error = -EIO;
1291 ub_state_stat(sc, cmd);
1292 return;
1293 }
1294
1295 } else {
1296 /*
1297 * If an error occurs on read, we record it, and
1298 * continue to fetch data in order to avoid bubble.
1299 *
1300 * As a small shortcut, we stop if we detect that
1301 * a CSW mixed into data.
1302 */
1303 if (urb->status != 0)
1304 cmd->error = -EIO;
1305
1306 len = urb->actual_length;
1307 if (urb->status != 0 ||
1308 len != cmd->sgv[cmd->current_sg].length) {
1309 if ((len & 0x1FF) == US_BULK_CS_WRAP_LEN)
1310 goto Bad_End;
1311 }
1312 }
1231 1313
1232 cmd->act_len += urb->actual_length; 1314 cmd->act_len += urb->actual_length;
1233 ub_cmdtr_act_len(sc, cmd); 1315 ub_cmdtr_act_len(sc, cmd);
@@ -1245,11 +1327,8 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1245 printk(KERN_NOTICE "%s: " 1327 printk(KERN_NOTICE "%s: "
1246 "unable to submit clear (%d)\n", 1328 "unable to submit clear (%d)\n",
1247 sc->name, rc); 1329 sc->name, rc);
1248 /* 1330 ub_state_done(sc, cmd, rc);
1249 * This is typically ENOMEM or some other such shit. 1331 return;
1250 * Retrying is pointless. Just do Bad End on it...
1251 */
1252 goto Bad_End;
1253 } 1332 }
1254 1333
1255 /* 1334 /*
@@ -1262,14 +1341,8 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1262 ub_cmdtr_state(sc, cmd); 1341 ub_cmdtr_state(sc, cmd);
1263 return; 1342 return;
1264 } 1343 }
1265 if (urb->status == -EOVERFLOW) { 1344
1266 /* 1345 /* Catch everything, including -EOVERFLOW and other nasties. */
1267 * XXX We are screwed here. Retrying is pointless,
1268 * because the pipelined data will not get in until
1269 * we read with a big enough buffer. We must reset XXX.
1270 */
1271 goto Bad_End;
1272 }
1273 if (urb->status != 0) 1346 if (urb->status != 0)
1274 goto Bad_End; 1347 goto Bad_End;
1275 1348
@@ -1315,15 +1388,15 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1315 return; 1388 return;
1316 } 1389 }
1317 1390
1318 rc = le32_to_cpu(bcs->Residue); 1391 len = le32_to_cpu(bcs->Residue);
1319 if (rc != cmd->len - cmd->act_len) { 1392 if (len != cmd->len - cmd->act_len) {
1320 /* 1393 /*
1321 * It is all right to transfer less, the caller has 1394 * It is all right to transfer less, the caller has
1322 * to check. But it's not all right if the device 1395 * to check. But it's not all right if the device
1323 * counts disagree with our counts. 1396 * counts disagree with our counts.
1324 */ 1397 */
1325 /* P3 */ printk("%s: resid %d len %d act %d\n", 1398 /* P3 */ printk("%s: resid %d len %d act %d\n",
1326 sc->name, rc, cmd->len, cmd->act_len); 1399 sc->name, len, cmd->len, cmd->act_len);
1327 goto Bad_End; 1400 goto Bad_End;
1328 } 1401 }
1329 1402
@@ -1334,13 +1407,13 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1334 ub_state_sense(sc, cmd); 1407 ub_state_sense(sc, cmd);
1335 return; 1408 return;
1336 case US_BULK_STAT_PHASE: 1409 case US_BULK_STAT_PHASE:
1337 /* XXX We must reset the transport here */
1338 /* P3 */ printk("%s: status PHASE\n", sc->name); 1410 /* P3 */ printk("%s: status PHASE\n", sc->name);
1339 goto Bad_End; 1411 goto Bad_End;
1340 default: 1412 default:
1341 printk(KERN_INFO "%s: unknown CSW status 0x%x\n", 1413 printk(KERN_INFO "%s: unknown CSW status 0x%x\n",
1342 sc->name, bcs->Status); 1414 sc->name, bcs->Status);
1343 goto Bad_End; 1415 ub_state_done(sc, cmd, -EINVAL);
1416 return;
1344 } 1417 }
1345 1418
1346 /* Not zeroing error to preserve a babble indicator */ 1419 /* Not zeroing error to preserve a babble indicator */
@@ -1360,7 +1433,8 @@ static void ub_scsi_urb_compl(struct ub_dev *sc, struct ub_scsi_cmd *cmd)
1360 printk(KERN_WARNING "%s: " 1433 printk(KERN_WARNING "%s: "
1361 "wrong command state %d\n", 1434 "wrong command state %d\n",
1362 sc->name, cmd->state); 1435 sc->name, cmd->state);
1363 goto Bad_End; 1436 ub_state_done(sc, cmd, -EINVAL);
1437 return;
1364 } 1438 }
1365 return; 1439 return;
1366 1440
@@ -1608,6 +1682,93 @@ static void ub_top_sense_done(struct ub_dev *sc, struct ub_scsi_cmd *scmd)
1608} 1682}
1609 1683
1610/* 1684/*
1685 * Reset management
1686 */
1687
1688static void ub_reset_enter(struct ub_dev *sc)
1689{
1690
1691 if (sc->reset) {
1692 /* This happens often on multi-LUN devices. */
1693 return;
1694 }
1695 sc->reset = 1;
1696
1697#if 0 /* Not needed because the disconnect waits for us. */
1698 unsigned long flags;
1699 spin_lock_irqsave(&ub_lock, flags);
1700 sc->openc++;
1701 spin_unlock_irqrestore(&ub_lock, flags);
1702#endif
1703
1704#if 0 /* We let them stop themselves. */
1705 struct list_head *p;
1706 struct ub_lun *lun;
1707 list_for_each(p, &sc->luns) {
1708 lun = list_entry(p, struct ub_lun, link);
1709 blk_stop_queue(lun->disk->queue);
1710 }
1711#endif
1712
1713 schedule_work(&sc->reset_work);
1714}
1715
1716static void ub_reset_task(void *arg)
1717{
1718 struct ub_dev *sc = arg;
1719 unsigned long flags;
1720 struct list_head *p;
1721 struct ub_lun *lun;
1722 int lkr, rc;
1723
1724 if (!sc->reset) {
1725 printk(KERN_WARNING "%s: Running reset unrequested\n",
1726 sc->name);
1727 return;
1728 }
1729
1730 if (atomic_read(&sc->poison)) {
1731 printk(KERN_NOTICE "%s: Not resetting disconnected device\n",
1732 sc->name); /* P3 This floods. Remove soon. XXX */
1733 } else if (sc->dev->actconfig->desc.bNumInterfaces != 1) {
1734 printk(KERN_NOTICE "%s: Not resetting multi-interface device\n",
1735 sc->name); /* P3 This floods. Remove soon. XXX */
1736 } else {
1737 if ((lkr = usb_lock_device_for_reset(sc->dev, sc->intf)) < 0) {
1738 printk(KERN_NOTICE
1739 "%s: usb_lock_device_for_reset failed (%d)\n",
1740 sc->name, lkr);
1741 } else {
1742 rc = usb_reset_device(sc->dev);
1743 if (rc < 0) {
1744 printk(KERN_NOTICE "%s: "
1745 "usb_lock_device_for_reset failed (%d)\n",
1746 sc->name, rc);
1747 }
1748
1749 if (lkr)
1750 usb_unlock_device(sc->dev);
1751 }
1752 }
1753
1754 /*
1755 * In theory, no commands can be running while reset is active,
1756 * so nobody can ask for another reset, and so we do not need any
1757 * queues of resets or anything. We do need a spinlock though,
1758 * to interact with block layer.
1759 */
1760 spin_lock_irqsave(&sc->lock, flags);
1761 sc->reset = 0;
1762 tasklet_schedule(&sc->tasklet);
1763 list_for_each(p, &sc->luns) {
1764 lun = list_entry(p, struct ub_lun, link);
1765 blk_start_queue(lun->disk->queue);
1766 }
1767 wake_up(&sc->reset_wait);
1768 spin_unlock_irqrestore(&sc->lock, flags);
1769}
1770
1771/*
1611 * This is called from a process context. 1772 * This is called from a process context.
1612 */ 1773 */
1613static void ub_revalidate(struct ub_dev *sc, struct ub_lun *lun) 1774static void ub_revalidate(struct ub_dev *sc, struct ub_lun *lun)
@@ -2142,7 +2303,7 @@ static int ub_get_pipes(struct ub_dev *sc, struct usb_device *dev,
2142 if (ep_in == NULL || ep_out == NULL) { 2303 if (ep_in == NULL || ep_out == NULL) {
2143 printk(KERN_NOTICE "%s: failed endpoint check\n", 2304 printk(KERN_NOTICE "%s: failed endpoint check\n",
2144 sc->name); 2305 sc->name);
2145 return -EIO; 2306 return -ENODEV;
2146 } 2307 }
2147 2308
2148 /* Calculate and store the pipe values */ 2309 /* Calculate and store the pipe values */
@@ -2180,6 +2341,8 @@ static int ub_probe(struct usb_interface *intf,
2180 usb_init_urb(&sc->work_urb); 2341 usb_init_urb(&sc->work_urb);
2181 tasklet_init(&sc->tasklet, ub_scsi_action, (unsigned long)sc); 2342 tasklet_init(&sc->tasklet, ub_scsi_action, (unsigned long)sc);
2182 atomic_set(&sc->poison, 0); 2343 atomic_set(&sc->poison, 0);
2344 INIT_WORK(&sc->reset_work, ub_reset_task, sc);
2345 init_waitqueue_head(&sc->reset_wait);
2183 2346
2184 init_timer(&sc->work_timer); 2347 init_timer(&sc->work_timer);
2185 sc->work_timer.data = (unsigned long) sc; 2348 sc->work_timer.data = (unsigned long) sc;
@@ -2200,7 +2363,8 @@ static int ub_probe(struct usb_interface *intf,
2200 2363
2201 /* XXX Verify that we can handle the device (from descriptors) */ 2364 /* XXX Verify that we can handle the device (from descriptors) */
2202 2365
2203 ub_get_pipes(sc, sc->dev, intf); 2366 if (ub_get_pipes(sc, sc->dev, intf) != 0)
2367 goto err_dev_desc;
2204 2368
2205 if (device_create_file(&sc->intf->dev, &dev_attr_diag) != 0) 2369 if (device_create_file(&sc->intf->dev, &dev_attr_diag) != 0)
2206 goto err_diag; 2370 goto err_diag;
@@ -2271,6 +2435,7 @@ static int ub_probe(struct usb_interface *intf,
2271 2435
2272 /* device_remove_file(&sc->intf->dev, &dev_attr_diag); */ 2436 /* device_remove_file(&sc->intf->dev, &dev_attr_diag); */
2273err_diag: 2437err_diag:
2438err_dev_desc:
2274 usb_set_intfdata(intf, NULL); 2439 usb_set_intfdata(intf, NULL);
2275 // usb_put_intf(sc->intf); 2440 // usb_put_intf(sc->intf);
2276 usb_put_dev(sc->dev); 2441 usb_put_dev(sc->dev);
@@ -2379,6 +2544,11 @@ static void ub_disconnect(struct usb_interface *intf)
2379 atomic_set(&sc->poison, 1); 2544 atomic_set(&sc->poison, 1);
2380 2545
2381 /* 2546 /*
2547 * Wait for reset to end, if any.
2548 */
2549 wait_event(sc->reset_wait, !sc->reset);
2550
2551 /*
2382 * Blow away queued commands. 2552 * Blow away queued commands.
2383 * 2553 *
2384 * Actually, this never works, because before we get here 2554 * Actually, this never works, because before we get here
@@ -2391,7 +2561,7 @@ static void ub_disconnect(struct usb_interface *intf)
2391 { 2561 {
2392 struct ub_scsi_cmd *cmd; 2562 struct ub_scsi_cmd *cmd;
2393 int cnt = 0; 2563 int cnt = 0;
2394 while ((cmd = ub_cmdq_pop(sc)) != NULL) { 2564 while ((cmd = ub_cmdq_peek(sc)) != NULL) {
2395 cmd->error = -ENOTCONN; 2565 cmd->error = -ENOTCONN;
2396 cmd->state = UB_CMDST_DONE; 2566 cmd->state = UB_CMDST_DONE;
2397 ub_cmdtr_state(sc, cmd); 2567 ub_cmdtr_state(sc, cmd);