1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <linux/blkdev.h>
27#include <linux/kthread.h>
28#include <linux/sched.h>
29#include <linux/slab.h>
30
31#include <scsi/scsi.h>
32#include <scsi/scsi_eh.h>
33#include <scsi/scsi_device.h>
34
35#include "debug.h"
36#include "rts51x.h"
37#include "rts51x_chip.h"
38#include "rts51x_card.h"
39#include "rts51x_scsi.h"
40#include "rts51x_transport.h"
41#include "trace.h"
42
43
44
45
46
47
48
49
50
51
52unsigned int rts51x_access_sglist(unsigned char *buffer,
53 unsigned int buflen, void *sglist,
54 void **sgptr, unsigned int *offset,
55 enum xfer_buf_dir dir)
56{
57 unsigned int cnt;
58 struct scatterlist *sg = (struct scatterlist *)*sgptr;
59
60
61
62
63
64
65
66
67
68
69 if (!sg)
70 sg = (struct scatterlist *)sglist;
71
72
73
74
75
76
77 cnt = 0;
78 while (cnt < buflen && sg) {
79 struct page *page = sg_page(sg) +
80 ((sg->offset + *offset) >> PAGE_SHIFT);
81 unsigned int poff = (sg->offset + *offset) & (PAGE_SIZE - 1);
82 unsigned int sglen = sg->length - *offset;
83
84 if (sglen > buflen - cnt) {
85
86
87 sglen = buflen - cnt;
88 *offset += sglen;
89 } else {
90
91
92 *offset = 0;
93 sg = sg_next(sg);
94 }
95
96
97
98
99 while (sglen > 0) {
100 unsigned int plen = min(sglen, (unsigned int)
101 PAGE_SIZE - poff);
102 unsigned char *ptr = kmap(page);
103
104 if (dir == TO_XFER_BUF)
105 memcpy(ptr + poff, buffer + cnt, plen);
106 else
107 memcpy(buffer + cnt, ptr + poff, plen);
108 kunmap(page);
109
110
111 poff = 0;
112 ++page;
113 cnt += plen;
114 sglen -= plen;
115 }
116 }
117 *sgptr = sg;
118
119
120 return cnt;
121}
122
123static unsigned int rts51x_access_xfer_buf(unsigned char *buffer,
124 unsigned int buflen, struct scsi_cmnd *srb,
125 struct scatterlist **sgptr,
126 unsigned int *offset, enum xfer_buf_dir dir)
127{
128 return rts51x_access_sglist(buffer, buflen, (void *)scsi_sglist(srb),
129 (void **)sgptr, offset, dir);
130}
131
132
133
134
135void rts51x_set_xfer_buf(unsigned char *buffer,
136 unsigned int buflen, struct scsi_cmnd *srb)
137{
138 unsigned int offset = 0;
139 struct scatterlist *sg = NULL;
140
141 buflen = min(buflen, scsi_bufflen(srb));
142 buflen = rts51x_access_xfer_buf(buffer, buflen, srb, &sg, &offset,
143 TO_XFER_BUF);
144 if (buflen < scsi_bufflen(srb))
145 scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
146}
147
148void rts51x_get_xfer_buf(unsigned char *buffer,
149 unsigned int buflen, struct scsi_cmnd *srb)
150{
151 unsigned int offset = 0;
152 struct scatterlist *sg = NULL;
153
154 buflen = min(buflen, scsi_bufflen(srb));
155 buflen = rts51x_access_xfer_buf(buffer, buflen, srb, &sg, &offset,
156 FROM_XFER_BUF);
157 if (buflen < scsi_bufflen(srb))
158 scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
159}
160
161
162
163
164static void urb_done_completion(struct urb *urb)
165{
166 struct completion *urb_done_ptr = urb->context;
167
168 if (urb_done_ptr)
169 complete(urb_done_ptr);
170}
171
172
173
174
175
176
177
178static int rts51x_msg_common(struct rts51x_chip *chip, struct urb *urb,
179 int timeout)
180{
181 struct rts51x_usb *rts51x = chip->usb;
182 struct completion urb_done;
183 long timeleft;
184 int status;
185
186
187 if (test_bit(FLIDX_ABORTING, &rts51x->dflags))
188 TRACE_RET(chip, -EIO);
189
190
191 init_completion(&urb_done);
192
193
194 urb->context = &urb_done;
195 urb->actual_length = 0;
196 urb->error_count = 0;
197 urb->status = 0;
198
199
200
201
202
203 urb->transfer_flags = URB_NO_SETUP_DMA_MAP;
204 if (urb->transfer_buffer == rts51x->iobuf) {
205 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
206 urb->transfer_dma = rts51x->iobuf_dma;
207 }
208 urb->setup_dma = rts51x->cr_dma;
209
210
211 status = usb_submit_urb(urb, GFP_NOIO);
212 if (status) {
213
214 TRACE_RET(chip, status);
215 }
216
217
218
219 set_bit(FLIDX_URB_ACTIVE, &rts51x->dflags);
220
221
222 if (test_bit(FLIDX_ABORTING, &rts51x->dflags)) {
223
224
225 if (test_and_clear_bit(FLIDX_URB_ACTIVE, &rts51x->dflags)) {
226 RTS51X_DEBUGP("-- cancelling URB\n");
227 usb_unlink_urb(urb);
228 }
229 }
230
231
232 timeleft =
233 wait_for_completion_interruptible_timeout(&urb_done,
234 (timeout * HZ /
235 1000) ? :
236 MAX_SCHEDULE_TIMEOUT);
237
238 clear_bit(FLIDX_URB_ACTIVE, &rts51x->dflags);
239
240 if (timeleft <= 0) {
241 RTS51X_DEBUGP("%s -- cancelling URB\n",
242 timeleft == 0 ? "Timeout" : "Signal");
243 usb_kill_urb(urb);
244 if (timeleft == 0)
245 status = -ETIMEDOUT;
246 else
247 status = -EINTR;
248 } else {
249 status = urb->status;
250 }
251
252 return status;
253}
254
255static int rts51x_clear_halt(struct rts51x_chip *chip, unsigned int pipe);
256
257
258
259
260static int interpret_urb_result(struct rts51x_chip *chip, unsigned int pipe,
261 unsigned int length, int result,
262 unsigned int partial)
263{
264 int retval = STATUS_SUCCESS;
265
266
267
268 switch (result) {
269
270 case 0:
271 if (partial != length) {
272 RTS51X_DEBUGP("-- short transfer\n");
273 TRACE_RET(chip, STATUS_TRANS_SHORT);
274 }
275
276 return STATUS_SUCCESS;
277
278 case -EPIPE:
279
280
281 if (usb_pipecontrol(pipe)) {
282 RTS51X_DEBUGP("-- stall on control pipe\n");
283 TRACE_RET(chip, STATUS_STALLED);
284 }
285
286 RTS51X_DEBUGP("clearing endpoint halt for pipe 0x%x\n", pipe);
287 if (rts51x_clear_halt(chip, pipe) < 0)
288 TRACE_RET(chip, STATUS_ERROR);
289 retval = STATUS_STALLED;
290 TRACE_GOTO(chip, Exit);
291
292
293
294 case -EOVERFLOW:
295 RTS51X_DEBUGP("-- babble\n");
296 retval = STATUS_TRANS_LONG;
297 TRACE_GOTO(chip, Exit);
298
299
300
301 case -ECONNRESET:
302 RTS51X_DEBUGP("-- transfer cancelled\n");
303 retval = STATUS_ERROR;
304 TRACE_GOTO(chip, Exit);
305
306
307 case -EREMOTEIO:
308 RTS51X_DEBUGP("-- short read transfer\n");
309 retval = STATUS_TRANS_SHORT;
310 TRACE_GOTO(chip, Exit);
311
312
313 case -EIO:
314 RTS51X_DEBUGP("-- abort or disconnect in progress\n");
315 retval = STATUS_ERROR;
316 TRACE_GOTO(chip, Exit);
317
318 case -ETIMEDOUT:
319 RTS51X_DEBUGP("-- time out\n");
320 retval = STATUS_TIMEDOUT;
321 TRACE_GOTO(chip, Exit);
322
323
324 default:
325 RTS51X_DEBUGP("-- unknown error\n");
326 retval = STATUS_ERROR;
327 TRACE_GOTO(chip, Exit);
328 }
329
330Exit:
331 if ((retval != STATUS_SUCCESS) && !usb_pipecontrol(pipe))
332 rts51x_clear_hw_error(chip);
333
334 return retval;
335}
336
337int rts51x_ctrl_transfer(struct rts51x_chip *chip, unsigned int pipe,
338 u8 request, u8 requesttype, u16 value, u16 index,
339 void *data, u16 size, int timeout)
340{
341 struct rts51x_usb *rts51x = chip->usb;
342 int result;
343
344 RTS51X_DEBUGP("%s: rq=%02x rqtype=%02x value=%04x index=%02x len=%u\n",
345 __func__, request, requesttype, value, index, size);
346
347
348 rts51x->cr->bRequestType = requesttype;
349 rts51x->cr->bRequest = request;
350 rts51x->cr->wValue = cpu_to_le16(value);
351 rts51x->cr->wIndex = cpu_to_le16(index);
352 rts51x->cr->wLength = cpu_to_le16(size);
353
354
355 usb_fill_control_urb(rts51x->current_urb, rts51x->pusb_dev, pipe,
356 (unsigned char *)rts51x->cr, data, size,
357 urb_done_completion, NULL);
358 result = rts51x_msg_common(chip, rts51x->current_urb, timeout);
359
360 return interpret_urb_result(chip, pipe, size, result,
361 rts51x->current_urb->actual_length);
362}
363
364static int rts51x_clear_halt(struct rts51x_chip *chip, unsigned int pipe)
365{
366 int result;
367 int endp = usb_pipeendpoint(pipe);
368
369 if (usb_pipein(pipe))
370 endp |= USB_DIR_IN;
371
372 result = rts51x_ctrl_transfer(chip, SND_CTRL_PIPE(chip),
373 USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT,
374 USB_ENDPOINT_HALT, endp, NULL, 0, 3000);
375 if (result != STATUS_SUCCESS)
376 TRACE_RET(chip, STATUS_FAIL);
377
378 usb_reset_endpoint(chip->usb->pusb_dev, endp);
379
380 return STATUS_SUCCESS;
381}
382
383static void rts51x_sg_clean(struct usb_sg_request *io)
384{
385 if (io->urbs) {
386 while (io->entries--)
387 usb_free_urb(io->urbs[io->entries]);
388 kfree(io->urbs);
389 io->urbs = NULL;
390 }
391 io->dev = NULL;
392}
393
394static int rts51x_sg_init(struct usb_sg_request *io, struct usb_device *dev,
395 unsigned pipe, unsigned period, struct scatterlist *sg,
396 int nents, size_t length, gfp_t mem_flags)
397{
398 return usb_sg_init(io, dev, pipe, period, sg, nents, length, mem_flags);
399}
400
401static int rts51x_sg_wait(struct usb_sg_request *io, int timeout)
402{
403 long timeleft;
404 int i;
405 int entries = io->entries;
406
407
408 spin_lock_irq(&io->lock);
409 i = 0;
410 while (i < entries && !io->status) {
411 int retval;
412
413 io->urbs[i]->dev = io->dev;
414 retval = usb_submit_urb(io->urbs[i], GFP_ATOMIC);
415
416
417
418
419 spin_unlock_irq(&io->lock);
420 switch (retval) {
421
422 case -ENXIO:
423 case -EAGAIN:
424 case -ENOMEM:
425 io->urbs[i]->dev = NULL;
426 retval = 0;
427 yield();
428 break;
429
430
431
432
433
434
435
436 case 0:
437 ++i;
438 cpu_relax();
439 break;
440
441
442 default:
443 io->urbs[i]->dev = NULL;
444 io->urbs[i]->status = retval;
445 dev_dbg(&io->dev->dev, "%s, submit --> %d\n",
446 __func__, retval);
447 usb_sg_cancel(io);
448 }
449 spin_lock_irq(&io->lock);
450 if (retval && (io->status == 0 || io->status == -ECONNRESET))
451 io->status = retval;
452 }
453 io->count -= entries - i;
454 if (io->count == 0)
455 complete(&io->complete);
456 spin_unlock_irq(&io->lock);
457
458 timeleft =
459 wait_for_completion_interruptible_timeout(&io->complete,
460 (timeout * HZ /
461 1000) ? :
462 MAX_SCHEDULE_TIMEOUT);
463 if (timeleft <= 0) {
464 RTS51X_DEBUGP("%s -- cancelling SG request\n",
465 timeleft == 0 ? "Timeout" : "Signal");
466 usb_sg_cancel(io);
467 if (timeleft == 0)
468 io->status = -ETIMEDOUT;
469 else
470 io->status = -EINTR;
471 }
472
473 rts51x_sg_clean(io);
474 return io->status;
475}
476
477
478
479
480
481
482
483static int rts51x_bulk_transfer_sglist(struct rts51x_chip *chip,
484 unsigned int pipe,
485 struct scatterlist *sg, int num_sg,
486 unsigned int length,
487 unsigned int *act_len, int timeout)
488{
489 int result;
490
491
492 if (test_bit(FLIDX_ABORTING, &chip->usb->dflags))
493 TRACE_RET(chip, STATUS_ERROR);
494
495
496 RTS51X_DEBUGP("%s: xfer %u bytes, %d entries\n", __func__,
497 length, num_sg);
498 result =
499 rts51x_sg_init(&chip->usb->current_sg, chip->usb->pusb_dev, pipe, 0,
500 sg, num_sg, length, GFP_NOIO);
501 if (result) {
502 RTS51X_DEBUGP("rts51x_sg_init returned %d\n", result);
503 TRACE_RET(chip, STATUS_ERROR);
504 }
505
506
507
508 set_bit(FLIDX_SG_ACTIVE, &chip->usb->dflags);
509
510
511 if (test_bit(FLIDX_ABORTING, &chip->usb->dflags)) {
512
513
514 if (test_and_clear_bit(FLIDX_SG_ACTIVE, &chip->usb->dflags)) {
515 RTS51X_DEBUGP("-- cancelling sg request\n");
516 usb_sg_cancel(&chip->usb->current_sg);
517 }
518 }
519
520
521 result = rts51x_sg_wait(&chip->usb->current_sg, timeout);
522
523 clear_bit(FLIDX_SG_ACTIVE, &chip->usb->dflags);
524
525
526 if (act_len)
527 *act_len = chip->usb->current_sg.bytes;
528 return interpret_urb_result(chip, pipe, length, result,
529 chip->usb->current_sg.bytes);
530}
531
532static int rts51x_bulk_transfer_buf(struct rts51x_chip *chip,
533 unsigned int pipe,
534 void *buf, unsigned int length,
535 unsigned int *act_len, int timeout)
536{
537 int result;
538
539
540 usb_fill_bulk_urb(chip->usb->current_urb, chip->usb->pusb_dev, pipe,
541 buf, length, urb_done_completion, NULL);
542 result = rts51x_msg_common(chip, chip->usb->current_urb, timeout);
543
544
545 if (act_len)
546 *act_len = chip->usb->current_urb->actual_length;
547 return interpret_urb_result(chip, pipe, length, result,
548 chip->usb->current_urb->actual_length);
549}
550
551int rts51x_transfer_data(struct rts51x_chip *chip, unsigned int pipe,
552 void *buf, unsigned int len, int use_sg,
553 unsigned int *act_len, int timeout)
554{
555 int result;
556
557 if (timeout < 600)
558 timeout = 600;
559
560 if (use_sg) {
561 result =
562 rts51x_bulk_transfer_sglist(chip, pipe,
563 (struct scatterlist *)buf,
564 use_sg, len, act_len, timeout);
565 } else {
566 result =
567 rts51x_bulk_transfer_buf(chip, pipe, buf, len, act_len,
568 timeout);
569 }
570
571 return result;
572}
573
574int rts51x_transfer_data_partial(struct rts51x_chip *chip, unsigned int pipe,
575 void *buf, void **ptr, unsigned int *offset,
576 unsigned int len, int use_sg,
577 unsigned int *act_len, int timeout)
578{
579 int result;
580
581 if (timeout < 600)
582 timeout = 600;
583
584 if (use_sg) {
585 void *tmp_buf = kmalloc(len, GFP_KERNEL);
586 if (!tmp_buf)
587 TRACE_RET(chip, STATUS_NOMEM);
588
589 if (usb_pipeout(pipe)) {
590 rts51x_access_sglist(tmp_buf, len, buf, ptr, offset,
591 FROM_XFER_BUF);
592 }
593 result =
594 rts51x_bulk_transfer_buf(chip, pipe, tmp_buf, len, act_len,
595 timeout);
596 if (result == STATUS_SUCCESS) {
597 if (usb_pipein(pipe)) {
598 rts51x_access_sglist(tmp_buf, len, buf, ptr,
599 offset, TO_XFER_BUF);
600 }
601 }
602
603 kfree(tmp_buf);
604 } else {
605 unsigned int step = 0;
606 if (offset)
607 step = *offset;
608 result =
609 rts51x_bulk_transfer_buf(chip, pipe, buf + step, len,
610 act_len, timeout);
611 if (act_len)
612 step += *act_len;
613 else
614 step += len;
615 if (offset)
616 *offset = step;
617 }
618
619 return result;
620}
621
622int rts51x_get_epc_status(struct rts51x_chip *chip, u16 *status)
623{
624 unsigned int pipe = RCV_INTR_PIPE(chip);
625 struct usb_host_endpoint *ep;
626 struct completion urb_done;
627 int result;
628
629 if (!status)
630 TRACE_RET(chip, STATUS_ERROR);
631
632
633 init_completion(&urb_done);
634
635 ep = chip->usb->pusb_dev->ep_in[usb_pipeendpoint(pipe)];
636
637
638
639
640 usb_fill_int_urb(chip->usb->intr_urb, chip->usb->pusb_dev, pipe,
641 status, 2, urb_done_completion, &urb_done, 1);
642
643 result = rts51x_msg_common(chip, chip->usb->intr_urb, 50);
644
645 return interpret_urb_result(chip, pipe, 2, result,
646 chip->usb->intr_urb->actual_length);
647}
648
649u8 media_not_present[] = {
650 0x70, 0, 0x02, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0x3A, 0, 0, 0, 0, 0 };
651u8 invalid_cmd_field[] = {
652 0x70, 0, 0x05, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0x24, 0, 0, 0, 0, 0 };
653
654void rts51x_invoke_transport(struct scsi_cmnd *srb, struct rts51x_chip *chip)
655{
656 int result;
657
658#ifdef CONFIG_PM
659 if (chip->option.ss_en) {
660 if (srb->cmnd[0] == TEST_UNIT_READY) {
661 if (RTS51X_CHK_STAT(chip, STAT_SS)) {
662 if (check_fake_card_ready(chip,
663 SCSI_LUN(srb))) {
664 srb->result = SAM_STAT_GOOD;
665 } else {
666 srb->result = SAM_STAT_CHECK_CONDITION;
667 memcpy(srb->sense_buffer,
668 media_not_present, SENSE_SIZE);
669 }
670 return;
671 }
672 } else if (srb->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
673 if (RTS51X_CHK_STAT(chip, STAT_SS)) {
674 int prevent = srb->cmnd[4] & 0x1;
675
676 if (prevent) {
677 srb->result = SAM_STAT_CHECK_CONDITION;
678 memcpy(srb->sense_buffer,
679 invalid_cmd_field, SENSE_SIZE);
680 } else {
681 srb->result = SAM_STAT_GOOD;
682 }
683 return;
684 }
685 } else {
686 if (RTS51X_CHK_STAT(chip, STAT_SS)
687 || RTS51X_CHK_STAT(chip, STAT_SS_PRE)) {
688
689 RTS51X_DEBUGP("Try to wake up device\n");
690 chip->resume_from_scsi = 1;
691
692 rts51x_try_to_exit_ss(chip);
693
694 if (RTS51X_CHK_STAT(chip, STAT_SS)) {
695 wait_timeout(3000);
696
697 rts51x_init_chip(chip);
698 rts51x_init_cards(chip);
699 }
700 }
701 }
702 }
703#endif
704
705 result = rts51x_scsi_handler(srb, chip);
706
707
708 if (result == TRANSPORT_ERROR) {
709 RTS51X_DEBUGP("-- transport indicates error, resetting\n");
710 srb->result = DID_ERROR << 16;
711 goto Handle_Errors;
712 }
713
714 srb->result = SAM_STAT_GOOD;
715
716
717
718
719
720
721 if (result == TRANSPORT_FAILED) {
722
723 srb->result = SAM_STAT_CHECK_CONDITION;
724 memcpy(srb->sense_buffer,
725 (unsigned char *)&(chip->sense_buffer[SCSI_LUN(srb)]),
726 sizeof(struct sense_data_t));
727 }
728
729 return;
730
731
732
733
734Handle_Errors:
735 return;
736}
737