1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/blkdev.h>
24#include <linux/kthread.h>
25#include <linux/sched.h>
26#include <linux/vmalloc.h>
27
28#include "rtsx.h"
29#include "sd.h"
30#include "ms.h"
31#include "spi.h"
32
33void scsi_show_command(struct rtsx_chip *chip)
34{
35 struct scsi_cmnd *srb = chip->srb;
36 char *what = NULL;
37 bool unknown_cmd = false;
38 int len;
39
40 switch (srb->cmnd[0]) {
41 case TEST_UNIT_READY:
42 what = "TEST_UNIT_READY";
43 break;
44 case REZERO_UNIT:
45 what = "REZERO_UNIT";
46 break;
47 case REQUEST_SENSE:
48 what = "REQUEST_SENSE";
49 break;
50 case FORMAT_UNIT:
51 what = "FORMAT_UNIT";
52 break;
53 case READ_BLOCK_LIMITS:
54 what = "READ_BLOCK_LIMITS";
55 break;
56 case REASSIGN_BLOCKS:
57 what = "REASSIGN_BLOCKS";
58 break;
59 case READ_6:
60 what = "READ_6";
61 break;
62 case WRITE_6:
63 what = "WRITE_6";
64 break;
65 case SEEK_6:
66 what = "SEEK_6";
67 break;
68 case READ_REVERSE:
69 what = "READ_REVERSE";
70 break;
71 case WRITE_FILEMARKS:
72 what = "WRITE_FILEMARKS";
73 break;
74 case SPACE:
75 what = "SPACE";
76 break;
77 case INQUIRY:
78 what = "INQUIRY";
79 break;
80 case RECOVER_BUFFERED_DATA:
81 what = "RECOVER_BUFFERED_DATA";
82 break;
83 case MODE_SELECT:
84 what = "MODE_SELECT";
85 break;
86 case RESERVE:
87 what = "RESERVE";
88 break;
89 case RELEASE:
90 what = "RELEASE";
91 break;
92 case COPY:
93 what = "COPY";
94 break;
95 case ERASE:
96 what = "ERASE";
97 break;
98 case MODE_SENSE:
99 what = "MODE_SENSE";
100 break;
101 case START_STOP:
102 what = "START_STOP";
103 break;
104 case RECEIVE_DIAGNOSTIC:
105 what = "RECEIVE_DIAGNOSTIC";
106 break;
107 case SEND_DIAGNOSTIC:
108 what = "SEND_DIAGNOSTIC";
109 break;
110 case ALLOW_MEDIUM_REMOVAL:
111 what = "ALLOW_MEDIUM_REMOVAL";
112 break;
113 case SET_WINDOW:
114 what = "SET_WINDOW";
115 break;
116 case READ_CAPACITY:
117 what = "READ_CAPACITY";
118 break;
119 case READ_10:
120 what = "READ_10";
121 break;
122 case WRITE_10:
123 what = "WRITE_10";
124 break;
125 case SEEK_10:
126 what = "SEEK_10";
127 break;
128 case WRITE_VERIFY:
129 what = "WRITE_VERIFY";
130 break;
131 case VERIFY:
132 what = "VERIFY";
133 break;
134 case SEARCH_HIGH:
135 what = "SEARCH_HIGH";
136 break;
137 case SEARCH_EQUAL:
138 what = "SEARCH_EQUAL";
139 break;
140 case SEARCH_LOW:
141 what = "SEARCH_LOW";
142 break;
143 case SET_LIMITS:
144 what = "SET_LIMITS";
145 break;
146 case READ_POSITION:
147 what = "READ_POSITION";
148 break;
149 case SYNCHRONIZE_CACHE:
150 what = "SYNCHRONIZE_CACHE";
151 break;
152 case LOCK_UNLOCK_CACHE:
153 what = "LOCK_UNLOCK_CACHE";
154 break;
155 case READ_DEFECT_DATA:
156 what = "READ_DEFECT_DATA";
157 break;
158 case MEDIUM_SCAN:
159 what = "MEDIUM_SCAN";
160 break;
161 case COMPARE:
162 what = "COMPARE";
163 break;
164 case COPY_VERIFY:
165 what = "COPY_VERIFY";
166 break;
167 case WRITE_BUFFER:
168 what = "WRITE_BUFFER";
169 break;
170 case READ_BUFFER:
171 what = "READ_BUFFER";
172 break;
173 case UPDATE_BLOCK:
174 what = "UPDATE_BLOCK";
175 break;
176 case READ_LONG:
177 what = "READ_LONG";
178 break;
179 case WRITE_LONG:
180 what = "WRITE_LONG";
181 break;
182 case CHANGE_DEFINITION:
183 what = "CHANGE_DEFINITION";
184 break;
185 case WRITE_SAME:
186 what = "WRITE_SAME";
187 break;
188 case GPCMD_READ_SUBCHANNEL:
189 what = "READ SUBCHANNEL";
190 break;
191 case READ_TOC:
192 what = "READ_TOC";
193 break;
194 case GPCMD_READ_HEADER:
195 what = "READ HEADER";
196 break;
197 case GPCMD_PLAY_AUDIO_10:
198 what = "PLAY AUDIO (10)";
199 break;
200 case GPCMD_PLAY_AUDIO_MSF:
201 what = "PLAY AUDIO MSF";
202 break;
203 case GPCMD_GET_EVENT_STATUS_NOTIFICATION:
204 what = "GET EVENT/STATUS NOTIFICATION";
205 break;
206 case GPCMD_PAUSE_RESUME:
207 what = "PAUSE/RESUME";
208 break;
209 case LOG_SELECT:
210 what = "LOG_SELECT";
211 break;
212 case LOG_SENSE:
213 what = "LOG_SENSE";
214 break;
215 case GPCMD_STOP_PLAY_SCAN:
216 what = "STOP PLAY/SCAN";
217 break;
218 case GPCMD_READ_DISC_INFO:
219 what = "READ DISC INFORMATION";
220 break;
221 case GPCMD_READ_TRACK_RZONE_INFO:
222 what = "READ TRACK INFORMATION";
223 break;
224 case GPCMD_RESERVE_RZONE_TRACK:
225 what = "RESERVE TRACK";
226 break;
227 case GPCMD_SEND_OPC:
228 what = "SEND OPC";
229 break;
230 case MODE_SELECT_10:
231 what = "MODE_SELECT_10";
232 break;
233 case GPCMD_REPAIR_RZONE_TRACK:
234 what = "REPAIR TRACK";
235 break;
236 case 0x59:
237 what = "READ MASTER CUE";
238 break;
239 case MODE_SENSE_10:
240 what = "MODE_SENSE_10";
241 break;
242 case GPCMD_CLOSE_TRACK:
243 what = "CLOSE TRACK/SESSION";
244 break;
245 case 0x5C:
246 what = "READ BUFFER CAPACITY";
247 break;
248 case 0x5D:
249 what = "SEND CUE SHEET";
250 break;
251 case GPCMD_BLANK:
252 what = "BLANK";
253 break;
254 case REPORT_LUNS:
255 what = "REPORT LUNS";
256 break;
257 case MOVE_MEDIUM:
258 what = "MOVE_MEDIUM or PLAY AUDIO (12)";
259 break;
260 case READ_12:
261 what = "READ_12";
262 break;
263 case WRITE_12:
264 what = "WRITE_12";
265 break;
266 case WRITE_VERIFY_12:
267 what = "WRITE_VERIFY_12";
268 break;
269 case SEARCH_HIGH_12:
270 what = "SEARCH_HIGH_12";
271 break;
272 case SEARCH_EQUAL_12:
273 what = "SEARCH_EQUAL_12";
274 break;
275 case SEARCH_LOW_12:
276 what = "SEARCH_LOW_12";
277 break;
278 case SEND_VOLUME_TAG:
279 what = "SEND_VOLUME_TAG";
280 break;
281 case READ_ELEMENT_STATUS:
282 what = "READ_ELEMENT_STATUS";
283 break;
284 case GPCMD_READ_CD_MSF:
285 what = "READ CD MSF";
286 break;
287 case GPCMD_SCAN:
288 what = "SCAN";
289 break;
290 case GPCMD_SET_SPEED:
291 what = "SET CD SPEED";
292 break;
293 case GPCMD_MECHANISM_STATUS:
294 what = "MECHANISM STATUS";
295 break;
296 case GPCMD_READ_CD:
297 what = "READ CD";
298 break;
299 case 0xE1:
300 what = "WRITE CONTINUE";
301 break;
302 case WRITE_LONG_2:
303 what = "WRITE_LONG_2";
304 break;
305 case VENDOR_CMND:
306 what = "Realtek's vendor command";
307 break;
308 default:
309 what = "(unknown command)";
310 unknown_cmd = true;
311 break;
312 }
313
314 if (srb->cmnd[0] != TEST_UNIT_READY)
315 dev_dbg(rtsx_dev(chip), "Command %s (%d bytes)\n",
316 what, srb->cmd_len);
317
318 if (unknown_cmd) {
319 len = min_t(unsigned short, srb->cmd_len, 16);
320 dev_dbg(rtsx_dev(chip), "%*ph\n", len, srb->cmnd);
321 }
322}
323
324void set_sense_type(struct rtsx_chip *chip, unsigned int lun, int sense_type)
325{
326 switch (sense_type) {
327 case SENSE_TYPE_MEDIA_CHANGE:
328 set_sense_data(chip, lun, CUR_ERR, 0x06, 0, 0x28, 0, 0, 0);
329 break;
330
331 case SENSE_TYPE_MEDIA_NOT_PRESENT:
332 set_sense_data(chip, lun, CUR_ERR, 0x02, 0, 0x3A, 0, 0, 0);
333 break;
334
335 case SENSE_TYPE_MEDIA_LBA_OVER_RANGE:
336 set_sense_data(chip, lun, CUR_ERR, 0x05, 0, 0x21, 0, 0, 0);
337 break;
338
339 case SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT:
340 set_sense_data(chip, lun, CUR_ERR, 0x05, 0, 0x25, 0, 0, 0);
341 break;
342
343 case SENSE_TYPE_MEDIA_WRITE_PROTECT:
344 set_sense_data(chip, lun, CUR_ERR, 0x07, 0, 0x27, 0, 0, 0);
345 break;
346
347 case SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR:
348 set_sense_data(chip, lun, CUR_ERR, 0x03, 0, 0x11, 0, 0, 0);
349 break;
350
351 case SENSE_TYPE_MEDIA_WRITE_ERR:
352 set_sense_data(chip, lun, CUR_ERR, 0x03, 0, 0x0C, 0x02, 0, 0);
353 break;
354
355 case SENSE_TYPE_MEDIA_INVALID_CMD_FIELD:
356 set_sense_data(chip, lun, CUR_ERR, ILGAL_REQ, 0,
357 ASC_INVLD_CDB, ASCQ_INVLD_CDB, CDB_ILLEGAL, 1);
358 break;
359
360 case SENSE_TYPE_FORMAT_IN_PROGRESS:
361 set_sense_data(chip, lun, CUR_ERR, 0x02, 0, 0x04, 0x04, 0, 0);
362 break;
363
364 case SENSE_TYPE_FORMAT_CMD_FAILED:
365 set_sense_data(chip, lun, CUR_ERR, 0x03, 0, 0x31, 0x01, 0, 0);
366 break;
367
368#ifdef SUPPORT_MAGIC_GATE
369 case SENSE_TYPE_MG_KEY_FAIL_NOT_ESTAB:
370 set_sense_data(chip, lun, CUR_ERR, 0x05, 0, 0x6F, 0x02, 0, 0);
371 break;
372
373 case SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN:
374 set_sense_data(chip, lun, CUR_ERR, 0x05, 0, 0x6F, 0x00, 0, 0);
375 break;
376
377 case SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM:
378 set_sense_data(chip, lun, CUR_ERR, 0x02, 0, 0x30, 0x00, 0, 0);
379 break;
380
381 case SENSE_TYPE_MG_WRITE_ERR:
382 set_sense_data(chip, lun, CUR_ERR, 0x03, 0, 0x0C, 0x00, 0, 0);
383 break;
384#endif
385
386#ifdef SUPPORT_SD_LOCK
387 case SENSE_TYPE_MEDIA_READ_FORBIDDEN:
388 set_sense_data(chip, lun, CUR_ERR, 0x07, 0, 0x11, 0x13, 0, 0);
389 break;
390#endif
391
392 case SENSE_TYPE_NO_SENSE:
393 default:
394 set_sense_data(chip, lun, CUR_ERR, 0, 0, 0, 0, 0, 0);
395 break;
396 }
397}
398
399void set_sense_data(struct rtsx_chip *chip, unsigned int lun, u8 err_code,
400 u8 sense_key, u32 info, u8 asc, u8 ascq, u8 sns_key_info0,
401 u16 sns_key_info1)
402{
403 struct sense_data_t *sense = &(chip->sense_buffer[lun]);
404
405 sense->err_code = err_code;
406 sense->sense_key = sense_key;
407 sense->info[0] = (u8)(info >> 24);
408 sense->info[1] = (u8)(info >> 16);
409 sense->info[2] = (u8)(info >> 8);
410 sense->info[3] = (u8)info;
411
412 sense->ad_sense_len = sizeof(struct sense_data_t) - 8;
413 sense->asc = asc;
414 sense->ascq = ascq;
415 if (sns_key_info0 != 0) {
416 sense->sns_key_info[0] = SKSV | sns_key_info0;
417 sense->sns_key_info[1] = (sns_key_info1 & 0xf0) >> 8;
418 sense->sns_key_info[2] = sns_key_info1 & 0x0f;
419 }
420}
421
422static int test_unit_ready(struct scsi_cmnd *srb, struct rtsx_chip *chip)
423{
424 unsigned int lun = SCSI_LUN(srb);
425
426 if (!check_card_ready(chip, lun)) {
427 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
428 return TRANSPORT_FAILED;
429 }
430
431 if (!(CHK_BIT(chip->lun_mc, lun))) {
432 SET_BIT(chip->lun_mc, lun);
433 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_CHANGE);
434 return TRANSPORT_FAILED;
435 }
436
437#ifdef SUPPORT_SD_LOCK
438 if (get_lun_card(chip, SCSI_LUN(srb)) == SD_CARD) {
439 struct sd_info *sd_card = &(chip->sd_card);
440
441 if (sd_card->sd_lock_notify) {
442 sd_card->sd_lock_notify = 0;
443 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_CHANGE);
444 return TRANSPORT_FAILED;
445 } else if (sd_card->sd_lock_status & SD_LOCKED) {
446 set_sense_type(chip, lun,
447 SENSE_TYPE_MEDIA_READ_FORBIDDEN);
448 return TRANSPORT_FAILED;
449 }
450 }
451#endif
452
453 return TRANSPORT_GOOD;
454}
455
456static unsigned char formatter_inquiry_str[20] = {
457 'M', 'E', 'M', 'O', 'R', 'Y', 'S', 'T', 'I', 'C', 'K',
458#ifdef SUPPORT_MAGIC_GATE
459 '-', 'M', 'G',
460#else
461 0x20, 0x20, 0x20,
462#endif
463
464#ifdef SUPPORT_MAGIC_GATE
465 0x0B,
466#else
467 0x09,
468#endif
469 0x00,
470 0x00,
471 0x20, 0x20, 0x20,
472};
473
474static int inquiry(struct scsi_cmnd *srb, struct rtsx_chip *chip)
475{
476 unsigned int lun = SCSI_LUN(srb);
477 char *inquiry_default = (char *)"Generic-xD/SD/M.S. 1.00 ";
478 char *inquiry_sdms = (char *)"Generic-SD/MemoryStick 1.00 ";
479 char *inquiry_sd = (char *)"Generic-SD/MMC 1.00 ";
480 char *inquiry_ms = (char *)"Generic-MemoryStick 1.00 ";
481 char *inquiry_string;
482 unsigned char sendbytes;
483 unsigned char *buf;
484 u8 card = get_lun_card(chip, lun);
485 bool pro_formatter_flag = false;
486 unsigned char inquiry_buf[] = {
487 QULIFIRE|DRCT_ACCESS_DEV,
488 RMB_DISC|0x0D,
489 0x00,
490 0x01,
491 0x1f,
492 0x02,
493 0,
494 REL_ADR|WBUS_32|WBUS_16|SYNC|LINKED|CMD_QUE|SFT_RE,
495 };
496
497 if (CHECK_LUN_MODE(chip, SD_MS_2LUN)) {
498 if (chip->lun2card[lun] == SD_CARD)
499 inquiry_string = inquiry_sd;
500 else
501 inquiry_string = inquiry_ms;
502
503 } else if (CHECK_LUN_MODE(chip, SD_MS_1LUN)) {
504 inquiry_string = inquiry_sdms;
505 } else {
506 inquiry_string = inquiry_default;
507 }
508
509 buf = vmalloc(scsi_bufflen(srb));
510 if (buf == NULL) {
511 rtsx_trace(chip);
512 return TRANSPORT_ERROR;
513 }
514
515#ifdef SUPPORT_MAGIC_GATE
516 if ((chip->mspro_formatter_enable) &&
517 (chip->lun2card[lun] & MS_CARD))
518#else
519 if (chip->mspro_formatter_enable)
520#endif
521 if (!card || (card == MS_CARD))
522 pro_formatter_flag = true;
523
524 if (pro_formatter_flag) {
525 if (scsi_bufflen(srb) < 56)
526 sendbytes = (unsigned char)(scsi_bufflen(srb));
527 else
528 sendbytes = 56;
529
530 } else {
531 if (scsi_bufflen(srb) < 36)
532 sendbytes = (unsigned char)(scsi_bufflen(srb));
533 else
534 sendbytes = 36;
535 }
536
537 if (sendbytes > 8) {
538 memcpy(buf, inquiry_buf, 8);
539 memcpy(buf + 8, inquiry_string, sendbytes - 8);
540 if (pro_formatter_flag) {
541
542 buf[4] = 0x33;
543 }
544 } else {
545 memcpy(buf, inquiry_buf, sendbytes);
546 }
547
548 if (pro_formatter_flag) {
549 if (sendbytes > 36)
550 memcpy(buf + 36, formatter_inquiry_str, sendbytes - 36);
551 }
552
553 scsi_set_resid(srb, 0);
554
555 rtsx_stor_set_xfer_buf(buf, scsi_bufflen(srb), srb);
556 vfree(buf);
557
558 return TRANSPORT_GOOD;
559}
560
561
562static int start_stop_unit(struct scsi_cmnd *srb, struct rtsx_chip *chip)
563{
564 unsigned int lun = SCSI_LUN(srb);
565
566 scsi_set_resid(srb, scsi_bufflen(srb));
567
568 if (srb->cmnd[1] == 1)
569 return TRANSPORT_GOOD;
570
571 switch (srb->cmnd[0x4]) {
572 case STOP_MEDIUM:
573
574 return TRANSPORT_GOOD;
575
576 case UNLOAD_MEDIUM:
577
578 if (check_card_ready(chip, lun))
579 eject_card(chip, lun);
580 return TRANSPORT_GOOD;
581
582 case MAKE_MEDIUM_READY:
583 case LOAD_MEDIUM:
584 if (check_card_ready(chip, lun))
585 return TRANSPORT_GOOD;
586 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
587 rtsx_trace(chip);
588 return TRANSPORT_FAILED;
589
590 break;
591 }
592
593 rtsx_trace(chip);
594 return TRANSPORT_ERROR;
595}
596
597
598static int allow_medium_removal(struct scsi_cmnd *srb, struct rtsx_chip *chip)
599{
600 int prevent;
601
602 prevent = srb->cmnd[4] & 0x1;
603
604 scsi_set_resid(srb, 0);
605
606 if (prevent) {
607 set_sense_type(chip, SCSI_LUN(srb),
608 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
609 rtsx_trace(chip);
610 return TRANSPORT_FAILED;
611 }
612
613 return TRANSPORT_GOOD;
614}
615
616
617static int request_sense(struct scsi_cmnd *srb, struct rtsx_chip *chip)
618{
619 struct sense_data_t *sense;
620 unsigned int lun = SCSI_LUN(srb);
621 struct ms_info *ms_card = &(chip->ms_card);
622 unsigned char *tmp, *buf;
623
624 sense = &(chip->sense_buffer[lun]);
625
626 if ((get_lun_card(chip, lun) == MS_CARD) &&
627 ms_card->pro_under_formatting) {
628 if (ms_card->format_status == FORMAT_SUCCESS) {
629 set_sense_type(chip, lun, SENSE_TYPE_NO_SENSE);
630 ms_card->pro_under_formatting = 0;
631 ms_card->progress = 0;
632 } else if (ms_card->format_status == FORMAT_IN_PROGRESS) {
633
634 set_sense_data(chip, lun, CUR_ERR, 0x02, 0, 0x04, 0x04,
635 0, (u16)(ms_card->progress));
636 } else {
637
638 set_sense_type(chip, lun, SENSE_TYPE_FORMAT_CMD_FAILED);
639 ms_card->pro_under_formatting = 0;
640 ms_card->progress = 0;
641 }
642
643 rtsx_set_stat(chip, RTSX_STAT_RUN);
644 }
645
646 buf = vmalloc(scsi_bufflen(srb));
647 if (buf == NULL) {
648 rtsx_trace(chip);
649 return TRANSPORT_ERROR;
650 }
651
652 tmp = (unsigned char *)sense;
653 memcpy(buf, tmp, scsi_bufflen(srb));
654
655 rtsx_stor_set_xfer_buf(buf, scsi_bufflen(srb), srb);
656 vfree(buf);
657
658 scsi_set_resid(srb, 0);
659
660 set_sense_type(chip, lun, SENSE_TYPE_NO_SENSE);
661 return TRANSPORT_GOOD;
662}
663
664static void ms_mode_sense(struct rtsx_chip *chip, u8 cmd,
665 int lun, u8 *buf, int buf_len)
666{
667 struct ms_info *ms_card = &(chip->ms_card);
668 int sys_info_offset;
669 int data_size = buf_len;
670 bool support_format = false;
671 int i = 0;
672
673 if (cmd == MODE_SENSE) {
674 sys_info_offset = 8;
675 if (data_size > 0x68)
676 data_size = 0x68;
677
678 buf[i++] = 0x67;
679 } else {
680 sys_info_offset = 12;
681 if (data_size > 0x6C)
682 data_size = 0x6C;
683
684 buf[i++] = 0x00;
685 buf[i++] = 0x6A;
686 }
687
688
689 if (check_card_ready(chip, lun)) {
690 if (CHK_MSXC(ms_card)) {
691 support_format = true;
692 buf[i++] = 0x40;
693 } else if (CHK_MSPRO(ms_card)) {
694 support_format = true;
695 buf[i++] = 0x20;
696 } else {
697 buf[i++] = 0x10;
698 }
699
700
701 if (check_card_wp(chip, lun))
702 buf[i++] = 0x80;
703 else
704 buf[i++] = 0x00;
705
706 } else {
707 buf[i++] = 0x00;
708 buf[i++] = 0x00;
709 }
710
711 buf[i++] = 0x00;
712
713 if (cmd == MODE_SENSE_10) {
714 buf[i++] = 0x00;
715 buf[i++] = 0x00;
716 buf[i++] = 0x00;
717
718
719 if (data_size >= 9)
720 buf[i++] = 0x20;
721 if (data_size >= 10)
722 buf[i++] = 0x62;
723 if (data_size >= 11)
724 buf[i++] = 0x00;
725 if (data_size >= 12) {
726 if (support_format)
727 buf[i++] = 0xC0;
728 else
729 buf[i++] = 0x00;
730 }
731 } else {
732
733 if (data_size >= 5)
734 buf[i++] = 0x20;
735 if (data_size >= 6)
736 buf[i++] = 0x62;
737 if (data_size >= 7)
738 buf[i++] = 0x00;
739 if (data_size >= 8) {
740 if (support_format)
741 buf[i++] = 0xC0;
742 else
743 buf[i++] = 0x00;
744 }
745 }
746
747 if (data_size > sys_info_offset) {
748
749 int len = data_size - sys_info_offset;
750
751 len = (len < 96) ? len : 96;
752
753 memcpy(buf + sys_info_offset, ms_card->raw_sys_info, len);
754 }
755}
756
757static int mode_sense(struct scsi_cmnd *srb, struct rtsx_chip *chip)
758{
759 unsigned int lun = SCSI_LUN(srb);
760 unsigned int dataSize;
761 int status;
762 bool pro_formatter_flag;
763 unsigned char pageCode, *buf;
764 u8 card = get_lun_card(chip, lun);
765
766#ifndef SUPPORT_MAGIC_GATE
767 if (!check_card_ready(chip, lun)) {
768 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
769 scsi_set_resid(srb, scsi_bufflen(srb));
770 rtsx_trace(chip);
771 return TRANSPORT_FAILED;
772 }
773#endif
774
775 pro_formatter_flag = false;
776 dataSize = 8;
777#ifdef SUPPORT_MAGIC_GATE
778 if ((chip->lun2card[lun] & MS_CARD)) {
779 if (!card || (card == MS_CARD)) {
780 dataSize = 108;
781 if (chip->mspro_formatter_enable)
782 pro_formatter_flag = true;
783 }
784 }
785#else
786 if (card == MS_CARD) {
787 if (chip->mspro_formatter_enable) {
788 pro_formatter_flag = true;
789 dataSize = 108;
790 }
791 }
792#endif
793
794 buf = kmalloc(dataSize, GFP_KERNEL);
795 if (buf == NULL) {
796 rtsx_trace(chip);
797 return TRANSPORT_ERROR;
798 }
799
800 pageCode = srb->cmnd[2] & 0x3f;
801
802 if ((pageCode == 0x3F) || (pageCode == 0x1C) ||
803 (pageCode == 0x00) ||
804 (pro_formatter_flag && (pageCode == 0x20))) {
805 if (srb->cmnd[0] == MODE_SENSE) {
806 if ((pageCode == 0x3F) || (pageCode == 0x20)) {
807 ms_mode_sense(chip, srb->cmnd[0],
808 lun, buf, dataSize);
809 } else {
810 dataSize = 4;
811 buf[0] = 0x03;
812 buf[1] = 0x00;
813 if (check_card_wp(chip, lun))
814 buf[2] = 0x80;
815 else
816 buf[2] = 0x00;
817
818 buf[3] = 0x00;
819 }
820 } else {
821 if ((pageCode == 0x3F) || (pageCode == 0x20)) {
822 ms_mode_sense(chip, srb->cmnd[0],
823 lun, buf, dataSize);
824 } else {
825 dataSize = 8;
826 buf[0] = 0x00;
827 buf[1] = 0x06;
828 buf[2] = 0x00;
829 if (check_card_wp(chip, lun))
830 buf[3] = 0x80;
831 else
832 buf[3] = 0x00;
833 buf[4] = 0x00;
834 buf[5] = 0x00;
835 buf[6] = 0x00;
836 buf[7] = 0x00;
837 }
838 }
839 status = TRANSPORT_GOOD;
840 } else {
841 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
842 scsi_set_resid(srb, scsi_bufflen(srb));
843 status = TRANSPORT_FAILED;
844 }
845
846 if (status == TRANSPORT_GOOD) {
847 unsigned int len = min_t(unsigned int, scsi_bufflen(srb),
848 dataSize);
849 rtsx_stor_set_xfer_buf(buf, len, srb);
850 scsi_set_resid(srb, scsi_bufflen(srb) - len);
851 }
852 kfree(buf);
853
854 return status;
855}
856
857static int read_write(struct scsi_cmnd *srb, struct rtsx_chip *chip)
858{
859#ifdef SUPPORT_SD_LOCK
860 struct sd_info *sd_card = &(chip->sd_card);
861#endif
862 unsigned int lun = SCSI_LUN(srb);
863 int retval;
864 u32 start_sec;
865 u16 sec_cnt;
866
867 rtsx_disable_aspm(chip);
868
869 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
870 rtsx_exit_ss(chip);
871 wait_timeout(100);
872 }
873 rtsx_set_stat(chip, RTSX_STAT_RUN);
874
875 if (!check_card_ready(chip, lun) || (get_card_size(chip, lun) == 0)) {
876 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
877 rtsx_trace(chip);
878 return TRANSPORT_FAILED;
879 }
880
881 if (!(CHK_BIT(chip->lun_mc, lun))) {
882 SET_BIT(chip->lun_mc, lun);
883 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_CHANGE);
884 return TRANSPORT_FAILED;
885 }
886
887#ifdef SUPPORT_SD_LOCK
888 if (sd_card->sd_erase_status) {
889
890
891
892 dev_dbg(rtsx_dev(chip), "SD card being erased!\n");
893 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_READ_FORBIDDEN);
894 rtsx_trace(chip);
895 return TRANSPORT_FAILED;
896 }
897
898 if (get_lun_card(chip, lun) == SD_CARD) {
899 if (sd_card->sd_lock_status & SD_LOCKED) {
900 dev_dbg(rtsx_dev(chip), "SD card locked!\n");
901 set_sense_type(chip, lun,
902 SENSE_TYPE_MEDIA_READ_FORBIDDEN);
903 rtsx_trace(chip);
904 return TRANSPORT_FAILED;
905 }
906 }
907#endif
908
909 if ((srb->cmnd[0] == READ_10) || (srb->cmnd[0] == WRITE_10)) {
910 start_sec = ((u32)srb->cmnd[2] << 24) |
911 ((u32)srb->cmnd[3] << 16) |
912 ((u32)srb->cmnd[4] << 8) | ((u32)srb->cmnd[5]);
913 sec_cnt = ((u16)(srb->cmnd[7]) << 8) | srb->cmnd[8];
914 } else if ((srb->cmnd[0] == READ_6) || (srb->cmnd[0] == WRITE_6)) {
915 start_sec = ((u32)(srb->cmnd[1] & 0x1F) << 16) |
916 ((u32)srb->cmnd[2] << 8) | ((u32)srb->cmnd[3]);
917 sec_cnt = srb->cmnd[4];
918 if (sec_cnt == 0)
919 sec_cnt = 256;
920 } else if ((srb->cmnd[0] == VENDOR_CMND) &&
921 (srb->cmnd[1] == SCSI_APP_CMD) &&
922 ((srb->cmnd[2] == PP_READ10) || (srb->cmnd[2] == PP_WRITE10))) {
923 start_sec = ((u32)srb->cmnd[4] << 24) |
924 ((u32)srb->cmnd[5] << 16) |
925 ((u32)srb->cmnd[6] << 8) | ((u32)srb->cmnd[7]);
926 sec_cnt = ((u16)(srb->cmnd[9]) << 8) | srb->cmnd[10];
927 } else {
928 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
929 rtsx_trace(chip);
930 return TRANSPORT_FAILED;
931 }
932
933
934
935
936
937 if ((start_sec > get_card_size(chip, lun)) ||
938 ((start_sec + sec_cnt) > get_card_size(chip, lun))) {
939 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_LBA_OVER_RANGE);
940 rtsx_trace(chip);
941 return TRANSPORT_FAILED;
942 }
943
944 if (sec_cnt == 0) {
945 scsi_set_resid(srb, 0);
946 return TRANSPORT_GOOD;
947 }
948
949 if (chip->rw_fail_cnt[lun] == 3) {
950 dev_dbg(rtsx_dev(chip), "read/write fail three times in succession\n");
951 if (srb->sc_data_direction == DMA_FROM_DEVICE)
952 set_sense_type(chip, lun,
953 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
954 else
955 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_WRITE_ERR);
956
957 rtsx_trace(chip);
958 return TRANSPORT_FAILED;
959 }
960
961 if (srb->sc_data_direction == DMA_TO_DEVICE) {
962 if (check_card_wp(chip, lun)) {
963 dev_dbg(rtsx_dev(chip), "Write protected card!\n");
964 set_sense_type(chip, lun,
965 SENSE_TYPE_MEDIA_WRITE_PROTECT);
966 rtsx_trace(chip);
967 return TRANSPORT_FAILED;
968 }
969 }
970
971 retval = card_rw(srb, chip, start_sec, sec_cnt);
972 if (retval != STATUS_SUCCESS) {
973 if (chip->need_release & chip->lun2card[lun]) {
974 chip->rw_fail_cnt[lun] = 0;
975 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
976 } else {
977 chip->rw_fail_cnt[lun]++;
978 if (srb->sc_data_direction == DMA_FROM_DEVICE)
979 set_sense_type(chip, lun,
980 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
981 else
982 set_sense_type(chip, lun,
983 SENSE_TYPE_MEDIA_WRITE_ERR);
984 }
985 retval = TRANSPORT_FAILED;
986 rtsx_trace(chip);
987 goto Exit;
988 } else {
989 chip->rw_fail_cnt[lun] = 0;
990 retval = TRANSPORT_GOOD;
991 }
992
993 scsi_set_resid(srb, 0);
994
995Exit:
996 return retval;
997}
998
999static int read_format_capacity(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1000{
1001 unsigned char *buf;
1002 unsigned int lun = SCSI_LUN(srb);
1003 unsigned int buf_len;
1004 u8 card = get_lun_card(chip, lun);
1005 u32 card_size;
1006 int desc_cnt;
1007 int i = 0;
1008
1009 if (!check_card_ready(chip, lun)) {
1010 if (!chip->mspro_formatter_enable) {
1011 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
1012 rtsx_trace(chip);
1013 return TRANSPORT_FAILED;
1014 }
1015 }
1016
1017 buf_len = (scsi_bufflen(srb) > 12) ? 0x14 : 12;
1018
1019 buf = kmalloc(buf_len, GFP_KERNEL);
1020 if (buf == NULL) {
1021 rtsx_trace(chip);
1022 return TRANSPORT_ERROR;
1023 }
1024
1025 buf[i++] = 0;
1026 buf[i++] = 0;
1027 buf[i++] = 0;
1028
1029
1030 if ((buf_len > 12) && chip->mspro_formatter_enable &&
1031 (chip->lun2card[lun] & MS_CARD) &&
1032 (!card || (card == MS_CARD))) {
1033 buf[i++] = 0x10;
1034 desc_cnt = 2;
1035 } else {
1036 buf[i++] = 0x08;
1037 desc_cnt = 1;
1038 }
1039
1040 while (desc_cnt) {
1041 if (check_card_ready(chip, lun)) {
1042 card_size = get_card_size(chip, lun);
1043 buf[i++] = (unsigned char)(card_size >> 24);
1044 buf[i++] = (unsigned char)(card_size >> 16);
1045 buf[i++] = (unsigned char)(card_size >> 8);
1046 buf[i++] = (unsigned char)card_size;
1047
1048 if (desc_cnt == 2)
1049 buf[i++] = 2;
1050 else
1051 buf[i++] = 0;
1052 } else {
1053 buf[i++] = 0xFF;
1054 buf[i++] = 0xFF;
1055 buf[i++] = 0xFF;
1056 buf[i++] = 0xFF;
1057
1058 if (desc_cnt == 2)
1059 buf[i++] = 3;
1060 else
1061 buf[i++] = 0;
1062 }
1063
1064 buf[i++] = 0x00;
1065 buf[i++] = 0x02;
1066 buf[i++] = 0x00;
1067
1068 desc_cnt--;
1069 }
1070
1071 buf_len = min_t(unsigned int, scsi_bufflen(srb), buf_len);
1072 rtsx_stor_set_xfer_buf(buf, buf_len, srb);
1073 kfree(buf);
1074
1075 scsi_set_resid(srb, scsi_bufflen(srb) - buf_len);
1076
1077 return TRANSPORT_GOOD;
1078}
1079
1080static int read_capacity(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1081{
1082 unsigned char *buf;
1083 unsigned int lun = SCSI_LUN(srb);
1084 u32 card_size;
1085
1086 if (!check_card_ready(chip, lun)) {
1087 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
1088 rtsx_trace(chip);
1089 return TRANSPORT_FAILED;
1090 }
1091
1092 if (!(CHK_BIT(chip->lun_mc, lun))) {
1093 SET_BIT(chip->lun_mc, lun);
1094 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_CHANGE);
1095 return TRANSPORT_FAILED;
1096 }
1097
1098 buf = kmalloc(8, GFP_KERNEL);
1099 if (buf == NULL) {
1100 rtsx_trace(chip);
1101 return TRANSPORT_ERROR;
1102 }
1103
1104 card_size = get_card_size(chip, lun);
1105 buf[0] = (unsigned char)((card_size - 1) >> 24);
1106 buf[1] = (unsigned char)((card_size - 1) >> 16);
1107 buf[2] = (unsigned char)((card_size - 1) >> 8);
1108 buf[3] = (unsigned char)(card_size - 1);
1109
1110 buf[4] = 0x00;
1111 buf[5] = 0x00;
1112 buf[6] = 0x02;
1113 buf[7] = 0x00;
1114
1115 rtsx_stor_set_xfer_buf(buf, scsi_bufflen(srb), srb);
1116 kfree(buf);
1117
1118 scsi_set_resid(srb, 0);
1119
1120 return TRANSPORT_GOOD;
1121}
1122
1123static int read_eeprom(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1124{
1125 unsigned short len, i;
1126 int retval;
1127 u8 *buf;
1128
1129 rtsx_disable_aspm(chip);
1130
1131 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
1132 rtsx_exit_ss(chip);
1133 wait_timeout(100);
1134 }
1135 rtsx_set_stat(chip, RTSX_STAT_RUN);
1136
1137 len = ((u16)srb->cmnd[4] << 8) | srb->cmnd[5];
1138
1139 buf = vmalloc(len);
1140 if (!buf) {
1141 rtsx_trace(chip);
1142 return TRANSPORT_ERROR;
1143 }
1144
1145 retval = rtsx_force_power_on(chip, SSC_PDCTL);
1146 if (retval != STATUS_SUCCESS) {
1147 vfree(buf);
1148 set_sense_type(chip, SCSI_LUN(srb),
1149 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
1150 rtsx_trace(chip);
1151 return TRANSPORT_FAILED;
1152 }
1153
1154 for (i = 0; i < len; i++) {
1155 retval = spi_read_eeprom(chip, i, buf + i);
1156 if (retval != STATUS_SUCCESS) {
1157 vfree(buf);
1158 set_sense_type(chip, SCSI_LUN(srb),
1159 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
1160 rtsx_trace(chip);
1161 return TRANSPORT_FAILED;
1162 }
1163 }
1164
1165 len = (unsigned short)min_t(unsigned int, scsi_bufflen(srb), len);
1166 rtsx_stor_set_xfer_buf(buf, len, srb);
1167 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1168
1169 vfree(buf);
1170
1171 return TRANSPORT_GOOD;
1172}
1173
1174static int write_eeprom(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1175{
1176 unsigned short len, i;
1177 int retval;
1178 u8 *buf;
1179
1180 rtsx_disable_aspm(chip);
1181
1182 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
1183 rtsx_exit_ss(chip);
1184 wait_timeout(100);
1185 }
1186 rtsx_set_stat(chip, RTSX_STAT_RUN);
1187
1188 len = ((u16)srb->cmnd[4] << 8) | srb->cmnd[5];
1189
1190 retval = rtsx_force_power_on(chip, SSC_PDCTL);
1191 if (retval != STATUS_SUCCESS) {
1192 set_sense_type(chip, SCSI_LUN(srb), SENSE_TYPE_MEDIA_WRITE_ERR);
1193 rtsx_trace(chip);
1194 return TRANSPORT_FAILED;
1195 }
1196
1197 if (len == 511) {
1198 retval = spi_erase_eeprom_chip(chip);
1199 if (retval != STATUS_SUCCESS) {
1200 set_sense_type(chip, SCSI_LUN(srb),
1201 SENSE_TYPE_MEDIA_WRITE_ERR);
1202 rtsx_trace(chip);
1203 return TRANSPORT_FAILED;
1204 }
1205 } else {
1206 len = (unsigned short)min_t(unsigned int, scsi_bufflen(srb),
1207 len);
1208 buf = vmalloc(len);
1209 if (buf == NULL) {
1210 rtsx_trace(chip);
1211 return TRANSPORT_ERROR;
1212 }
1213
1214 rtsx_stor_get_xfer_buf(buf, len, srb);
1215 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1216
1217 for (i = 0; i < len; i++) {
1218 retval = spi_write_eeprom(chip, i, buf[i]);
1219 if (retval != STATUS_SUCCESS) {
1220 vfree(buf);
1221 set_sense_type(chip, SCSI_LUN(srb),
1222 SENSE_TYPE_MEDIA_WRITE_ERR);
1223 rtsx_trace(chip);
1224 return TRANSPORT_FAILED;
1225 }
1226 }
1227
1228 vfree(buf);
1229 }
1230
1231 return TRANSPORT_GOOD;
1232}
1233
1234static int read_mem(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1235{
1236 unsigned short addr, len, i;
1237 int retval;
1238 u8 *buf;
1239
1240 rtsx_disable_aspm(chip);
1241
1242 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
1243 rtsx_exit_ss(chip);
1244 wait_timeout(100);
1245 }
1246 rtsx_set_stat(chip, RTSX_STAT_RUN);
1247
1248 addr = ((u16)srb->cmnd[2] << 8) | srb->cmnd[3];
1249 len = ((u16)srb->cmnd[4] << 8) | srb->cmnd[5];
1250
1251 if (addr < 0xFC00) {
1252 set_sense_type(chip, SCSI_LUN(srb),
1253 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1254 rtsx_trace(chip);
1255 return TRANSPORT_FAILED;
1256 }
1257
1258 buf = vmalloc(len);
1259 if (!buf) {
1260 rtsx_trace(chip);
1261 return TRANSPORT_ERROR;
1262 }
1263
1264 retval = rtsx_force_power_on(chip, SSC_PDCTL);
1265 if (retval != STATUS_SUCCESS) {
1266 vfree(buf);
1267 set_sense_type(chip, SCSI_LUN(srb), SENSE_TYPE_MEDIA_WRITE_ERR);
1268 rtsx_trace(chip);
1269 return TRANSPORT_FAILED;
1270 }
1271
1272 for (i = 0; i < len; i++) {
1273 retval = rtsx_read_register(chip, addr + i, buf + i);
1274 if (retval != STATUS_SUCCESS) {
1275 vfree(buf);
1276 set_sense_type(chip, SCSI_LUN(srb),
1277 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
1278 rtsx_trace(chip);
1279 return TRANSPORT_FAILED;
1280 }
1281 }
1282
1283 len = (unsigned short)min_t(unsigned int, scsi_bufflen(srb), len);
1284 rtsx_stor_set_xfer_buf(buf, len, srb);
1285 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1286
1287 vfree(buf);
1288
1289 return TRANSPORT_GOOD;
1290}
1291
1292static int write_mem(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1293{
1294 unsigned short addr, len, i;
1295 int retval;
1296 u8 *buf;
1297
1298 rtsx_disable_aspm(chip);
1299
1300 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
1301 rtsx_exit_ss(chip);
1302 wait_timeout(100);
1303 }
1304 rtsx_set_stat(chip, RTSX_STAT_RUN);
1305
1306 addr = ((u16)srb->cmnd[2] << 8) | srb->cmnd[3];
1307 len = ((u16)srb->cmnd[4] << 8) | srb->cmnd[5];
1308
1309 if (addr < 0xFC00) {
1310 set_sense_type(chip, SCSI_LUN(srb),
1311 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1312 rtsx_trace(chip);
1313 return TRANSPORT_FAILED;
1314 }
1315
1316 len = (unsigned short)min_t(unsigned int, scsi_bufflen(srb), len);
1317 buf = vmalloc(len);
1318 if (buf == NULL) {
1319 rtsx_trace(chip);
1320 return TRANSPORT_ERROR;
1321 }
1322
1323 rtsx_stor_get_xfer_buf(buf, len, srb);
1324 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1325
1326 retval = rtsx_force_power_on(chip, SSC_PDCTL);
1327 if (retval != STATUS_SUCCESS) {
1328 vfree(buf);
1329 set_sense_type(chip, SCSI_LUN(srb), SENSE_TYPE_MEDIA_WRITE_ERR);
1330 rtsx_trace(chip);
1331 return TRANSPORT_FAILED;
1332 }
1333
1334 for (i = 0; i < len; i++) {
1335 retval = rtsx_write_register(chip, addr + i, 0xFF, buf[i]);
1336 if (retval != STATUS_SUCCESS) {
1337 vfree(buf);
1338 set_sense_type(chip, SCSI_LUN(srb),
1339 SENSE_TYPE_MEDIA_WRITE_ERR);
1340 rtsx_trace(chip);
1341 return TRANSPORT_FAILED;
1342 }
1343 }
1344
1345 vfree(buf);
1346
1347 return TRANSPORT_GOOD;
1348}
1349
1350static int get_sd_csd(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1351{
1352 struct sd_info *sd_card = &(chip->sd_card);
1353 unsigned int lun = SCSI_LUN(srb);
1354
1355 if (!check_card_ready(chip, lun)) {
1356 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
1357 rtsx_trace(chip);
1358 return TRANSPORT_FAILED;
1359 }
1360
1361 if (get_lun_card(chip, lun) != SD_CARD) {
1362 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
1363 rtsx_trace(chip);
1364 return TRANSPORT_FAILED;
1365 }
1366
1367 scsi_set_resid(srb, 0);
1368 rtsx_stor_set_xfer_buf(sd_card->raw_csd, scsi_bufflen(srb), srb);
1369
1370 return TRANSPORT_GOOD;
1371}
1372
1373static int toggle_gpio_cmd(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1374{
1375 u8 gpio = srb->cmnd[2];
1376
1377 rtsx_disable_aspm(chip);
1378
1379 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
1380 rtsx_exit_ss(chip);
1381 wait_timeout(100);
1382 }
1383 rtsx_set_stat(chip, RTSX_STAT_RUN);
1384
1385 if (gpio > 3)
1386 gpio = 1;
1387 toggle_gpio(chip, gpio);
1388
1389 return TRANSPORT_GOOD;
1390}
1391
1392#ifdef _MSG_TRACE
1393static int trace_msg_cmd(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1394{
1395 unsigned char *ptr, *buf = NULL;
1396 int i, msg_cnt;
1397 u8 clear;
1398 unsigned int buf_len;
1399
1400 buf_len = 4 + ((2 + MSG_FUNC_LEN + MSG_FILE_LEN + TIME_VAL_LEN) *
1401 TRACE_ITEM_CNT);
1402
1403 if ((scsi_bufflen(srb) < buf_len) || (scsi_sglist(srb) == NULL)) {
1404 set_sense_type(chip, SCSI_LUN(srb),
1405 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
1406 rtsx_trace(chip);
1407 return TRANSPORT_FAILED;
1408 }
1409
1410 clear = srb->cmnd[2];
1411
1412 buf = vmalloc(scsi_bufflen(srb));
1413 if (buf == NULL) {
1414 rtsx_trace(chip);
1415 return TRANSPORT_ERROR;
1416 }
1417 ptr = buf;
1418
1419 if (chip->trace_msg[chip->msg_idx].valid)
1420 msg_cnt = TRACE_ITEM_CNT;
1421 else
1422 msg_cnt = chip->msg_idx;
1423
1424 *(ptr++) = (u8)(msg_cnt >> 24);
1425 *(ptr++) = (u8)(msg_cnt >> 16);
1426 *(ptr++) = (u8)(msg_cnt >> 8);
1427 *(ptr++) = (u8)msg_cnt;
1428 dev_dbg(rtsx_dev(chip), "Trace message count is %d\n", msg_cnt);
1429
1430 for (i = 1; i <= msg_cnt; i++) {
1431 int j, idx;
1432
1433 idx = chip->msg_idx - i;
1434 if (idx < 0)
1435 idx += TRACE_ITEM_CNT;
1436
1437 *(ptr++) = (u8)(chip->trace_msg[idx].line >> 8);
1438 *(ptr++) = (u8)(chip->trace_msg[idx].line);
1439 for (j = 0; j < MSG_FUNC_LEN; j++)
1440 *(ptr++) = chip->trace_msg[idx].func[j];
1441
1442 for (j = 0; j < MSG_FILE_LEN; j++)
1443 *(ptr++) = chip->trace_msg[idx].file[j];
1444
1445 for (j = 0; j < TIME_VAL_LEN; j++)
1446 *(ptr++) = chip->trace_msg[idx].timeval_buf[j];
1447 }
1448
1449 rtsx_stor_set_xfer_buf(buf, scsi_bufflen(srb), srb);
1450 vfree(buf);
1451
1452 if (clear) {
1453 chip->msg_idx = 0;
1454 for (i = 0; i < TRACE_ITEM_CNT; i++)
1455 chip->trace_msg[i].valid = 0;
1456 }
1457
1458 scsi_set_resid(srb, 0);
1459 return TRANSPORT_GOOD;
1460}
1461#endif
1462
1463static int read_host_reg(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1464{
1465 u8 addr, buf[4];
1466 u32 val;
1467 unsigned int len;
1468
1469 rtsx_disable_aspm(chip);
1470
1471 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
1472 rtsx_exit_ss(chip);
1473 wait_timeout(100);
1474 }
1475 rtsx_set_stat(chip, RTSX_STAT_RUN);
1476
1477 addr = srb->cmnd[4];
1478
1479 val = rtsx_readl(chip, addr);
1480 dev_dbg(rtsx_dev(chip), "Host register (0x%x): 0x%x\n", addr, val);
1481
1482 buf[0] = (u8)(val >> 24);
1483 buf[1] = (u8)(val >> 16);
1484 buf[2] = (u8)(val >> 8);
1485 buf[3] = (u8)val;
1486
1487 len = min_t(unsigned int, scsi_bufflen(srb), 4);
1488 rtsx_stor_set_xfer_buf(buf, len, srb);
1489 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1490
1491 return TRANSPORT_GOOD;
1492}
1493
1494static int write_host_reg(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1495{
1496 u8 addr, buf[4];
1497 u32 val;
1498 unsigned int len;
1499
1500 rtsx_disable_aspm(chip);
1501
1502 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
1503 rtsx_exit_ss(chip);
1504 wait_timeout(100);
1505 }
1506 rtsx_set_stat(chip, RTSX_STAT_RUN);
1507
1508 addr = srb->cmnd[4];
1509
1510 len = min_t(unsigned int, scsi_bufflen(srb), 4);
1511 rtsx_stor_get_xfer_buf(buf, len, srb);
1512 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1513
1514 val = ((u32)buf[0] << 24) | ((u32)buf[1] << 16) | ((u32)buf[2]
1515 << 8) | buf[3];
1516
1517 rtsx_writel(chip, addr, val);
1518
1519 return TRANSPORT_GOOD;
1520}
1521
1522static int set_variable(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1523{
1524 unsigned lun = SCSI_LUN(srb);
1525
1526 if (srb->cmnd[3] == 1) {
1527
1528 struct xd_info *xd_card = &(chip->xd_card);
1529 struct sd_info *sd_card = &(chip->sd_card);
1530 struct ms_info *ms_card = &(chip->ms_card);
1531
1532 switch (srb->cmnd[4]) {
1533 case XD_CARD:
1534 xd_card->xd_clock = srb->cmnd[5];
1535 break;
1536
1537 case SD_CARD:
1538 sd_card->sd_clock = srb->cmnd[5];
1539 break;
1540
1541 case MS_CARD:
1542 ms_card->ms_clock = srb->cmnd[5];
1543 break;
1544
1545 default:
1546 set_sense_type(chip, lun,
1547 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1548 rtsx_trace(chip);
1549 return TRANSPORT_FAILED;
1550 }
1551 } else if (srb->cmnd[3] == 2) {
1552 if (srb->cmnd[4]) {
1553 chip->blink_led = 1;
1554 } else {
1555 int retval;
1556
1557 chip->blink_led = 0;
1558
1559 rtsx_disable_aspm(chip);
1560
1561 if (chip->ss_en &&
1562 (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
1563 rtsx_exit_ss(chip);
1564 wait_timeout(100);
1565 }
1566 rtsx_set_stat(chip, RTSX_STAT_RUN);
1567
1568 retval = rtsx_force_power_on(chip, SSC_PDCTL);
1569 if (retval != STATUS_SUCCESS) {
1570 set_sense_type(chip, SCSI_LUN(srb),
1571 SENSE_TYPE_MEDIA_WRITE_ERR);
1572 rtsx_trace(chip);
1573 return TRANSPORT_FAILED;
1574 }
1575
1576 turn_off_led(chip, LED_GPIO);
1577 }
1578 } else {
1579 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1580 rtsx_trace(chip);
1581 return TRANSPORT_FAILED;
1582 }
1583
1584 return TRANSPORT_GOOD;
1585}
1586
1587static int get_variable(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1588{
1589 unsigned int lun = SCSI_LUN(srb);
1590
1591 if (srb->cmnd[3] == 1) {
1592 struct xd_info *xd_card = &(chip->xd_card);
1593 struct sd_info *sd_card = &(chip->sd_card);
1594 struct ms_info *ms_card = &(chip->ms_card);
1595 u8 tmp;
1596
1597 switch (srb->cmnd[4]) {
1598 case XD_CARD:
1599 tmp = (u8)(xd_card->xd_clock);
1600 break;
1601
1602 case SD_CARD:
1603 tmp = (u8)(sd_card->sd_clock);
1604 break;
1605
1606 case MS_CARD:
1607 tmp = (u8)(ms_card->ms_clock);
1608 break;
1609
1610 default:
1611 set_sense_type(chip, lun,
1612 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1613 rtsx_trace(chip);
1614 return TRANSPORT_FAILED;
1615 }
1616
1617 rtsx_stor_set_xfer_buf(&tmp, 1, srb);
1618 } else if (srb->cmnd[3] == 2) {
1619 u8 tmp = chip->blink_led;
1620
1621 rtsx_stor_set_xfer_buf(&tmp, 1, srb);
1622 } else {
1623 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1624 rtsx_trace(chip);
1625 return TRANSPORT_FAILED;
1626 }
1627
1628 return TRANSPORT_GOOD;
1629}
1630
1631static int dma_access_ring_buffer(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1632{
1633 int retval;
1634 unsigned int lun = SCSI_LUN(srb);
1635 u16 len;
1636
1637 rtsx_disable_aspm(chip);
1638
1639 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
1640 rtsx_exit_ss(chip);
1641 wait_timeout(100);
1642 }
1643 rtsx_set_stat(chip, RTSX_STAT_RUN);
1644
1645 len = ((u16)(srb->cmnd[4]) << 8) | srb->cmnd[5];
1646 len = min_t(u16, len, scsi_bufflen(srb));
1647
1648 if (srb->sc_data_direction == DMA_FROM_DEVICE)
1649 dev_dbg(rtsx_dev(chip), "Read from device\n");
1650 else
1651 dev_dbg(rtsx_dev(chip), "Write to device\n");
1652
1653 retval = rtsx_transfer_data(chip, 0, scsi_sglist(srb), len,
1654 scsi_sg_count(srb), srb->sc_data_direction, 1000);
1655 if (retval < 0) {
1656 if (srb->sc_data_direction == DMA_FROM_DEVICE)
1657 set_sense_type(chip, lun,
1658 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
1659 else
1660 set_sense_type(chip, lun,
1661 SENSE_TYPE_MEDIA_WRITE_ERR);
1662
1663 rtsx_trace(chip);
1664 return TRANSPORT_FAILED;
1665 }
1666 scsi_set_resid(srb, 0);
1667
1668 return TRANSPORT_GOOD;
1669}
1670
1671static int get_dev_status(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1672{
1673 struct sd_info *sd_card = &(chip->sd_card);
1674 struct ms_info *ms_card = &(chip->ms_card);
1675 int buf_len;
1676 unsigned int lun = SCSI_LUN(srb);
1677 u8 card = get_lun_card(chip, lun);
1678 u8 status[32];
1679#ifdef SUPPORT_OCP
1680 u8 oc_now_mask = 0, oc_ever_mask = 0;
1681#endif
1682
1683 memset(status, 0, 32);
1684
1685 status[0] = (u8)(chip->product_id);
1686 status[1] = chip->ic_version;
1687
1688 if (chip->auto_delink_en)
1689 status[2] = 0x10;
1690 else
1691 status[2] = 0x00;
1692
1693 status[3] = 20;
1694 status[4] = 10;
1695 status[5] = 05;
1696 status[6] = 21;
1697
1698 if (chip->card_wp)
1699 status[7] = 0x20;
1700 else
1701 status[7] = 0x00;
1702
1703#ifdef SUPPORT_OCP
1704 status[8] = 0;
1705 if (CHECK_LUN_MODE(chip,
1706 SD_MS_2LUN) && (chip->lun2card[lun] == MS_CARD)) {
1707 oc_now_mask = MS_OC_NOW;
1708 oc_ever_mask = MS_OC_EVER;
1709 } else {
1710 oc_now_mask = SD_OC_NOW;
1711 oc_ever_mask = SD_OC_EVER;
1712 }
1713
1714 if (chip->ocp_stat & oc_now_mask)
1715 status[8] |= 0x02;
1716
1717 if (chip->ocp_stat & oc_ever_mask)
1718 status[8] |= 0x01;
1719#endif
1720
1721 if (card == SD_CARD) {
1722 if (CHK_SD(sd_card)) {
1723 if (CHK_SD_HCXC(sd_card)) {
1724 if (sd_card->capacity > 0x4000000)
1725 status[0x0E] = 0x02;
1726 else
1727 status[0x0E] = 0x01;
1728 } else {
1729 status[0x0E] = 0x00;
1730 }
1731
1732 if (CHK_SD_SDR104(sd_card))
1733 status[0x0F] = 0x03;
1734 else if (CHK_SD_DDR50(sd_card))
1735 status[0x0F] = 0x04;
1736 else if (CHK_SD_SDR50(sd_card))
1737 status[0x0F] = 0x02;
1738 else if (CHK_SD_HS(sd_card))
1739 status[0x0F] = 0x01;
1740 else
1741 status[0x0F] = 0x00;
1742 } else {
1743 if (CHK_MMC_SECTOR_MODE(sd_card))
1744 status[0x0E] = 0x01;
1745 else
1746 status[0x0E] = 0x00;
1747
1748 if (CHK_MMC_DDR52(sd_card))
1749 status[0x0F] = 0x03;
1750 else if (CHK_MMC_52M(sd_card))
1751 status[0x0F] = 0x02;
1752 else if (CHK_MMC_26M(sd_card))
1753 status[0x0F] = 0x01;
1754 else
1755 status[0x0F] = 0x00;
1756 }
1757 } else if (card == MS_CARD) {
1758 if (CHK_MSPRO(ms_card)) {
1759 if (CHK_MSXC(ms_card))
1760 status[0x0E] = 0x01;
1761 else
1762 status[0x0E] = 0x00;
1763
1764 if (CHK_HG8BIT(ms_card))
1765 status[0x0F] = 0x01;
1766 else
1767 status[0x0F] = 0x00;
1768 }
1769 }
1770
1771#ifdef SUPPORT_SD_LOCK
1772 if (card == SD_CARD) {
1773 status[0x17] = 0x80;
1774 if (sd_card->sd_erase_status)
1775 status[0x17] |= 0x01;
1776 if (sd_card->sd_lock_status & SD_LOCKED) {
1777 status[0x17] |= 0x02;
1778 status[0x07] |= 0x40;
1779 }
1780 if (sd_card->sd_lock_status & SD_PWD_EXIST)
1781 status[0x17] |= 0x04;
1782 } else {
1783 status[0x17] = 0x00;
1784 }
1785
1786 dev_dbg(rtsx_dev(chip), "status[0x17] = 0x%x\n", status[0x17]);
1787#endif
1788
1789 status[0x18] = 0x8A;
1790 status[0x1A] = 0x28;
1791#ifdef SUPPORT_SD_LOCK
1792 status[0x1F] = 0x01;
1793#endif
1794
1795 buf_len = min_t(unsigned int, scsi_bufflen(srb), sizeof(status));
1796 rtsx_stor_set_xfer_buf(status, buf_len, srb);
1797 scsi_set_resid(srb, scsi_bufflen(srb) - buf_len);
1798
1799 return TRANSPORT_GOOD;
1800}
1801
1802static int set_chip_mode(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1803{
1804 int phy_debug_mode;
1805 int retval;
1806 u16 reg;
1807
1808 if (!CHECK_PID(chip, 0x5208)) {
1809 set_sense_type(chip, SCSI_LUN(srb),
1810 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1811 rtsx_trace(chip);
1812 return TRANSPORT_FAILED;
1813 }
1814
1815 phy_debug_mode = (int)(srb->cmnd[3]);
1816
1817 if (phy_debug_mode) {
1818 chip->phy_debug_mode = 1;
1819 retval = rtsx_write_register(chip, CDRESUMECTL, 0x77, 0);
1820 if (retval != STATUS_SUCCESS) {
1821 rtsx_trace(chip);
1822 return TRANSPORT_FAILED;
1823 }
1824
1825 rtsx_disable_bus_int(chip);
1826
1827 retval = rtsx_read_phy_register(chip, 0x1C, ®);
1828 if (retval != STATUS_SUCCESS) {
1829 rtsx_trace(chip);
1830 return TRANSPORT_FAILED;
1831 }
1832
1833 reg |= 0x0001;
1834 retval = rtsx_write_phy_register(chip, 0x1C, reg);
1835 if (retval != STATUS_SUCCESS) {
1836 rtsx_trace(chip);
1837 return TRANSPORT_FAILED;
1838 }
1839 } else {
1840 chip->phy_debug_mode = 0;
1841 retval = rtsx_write_register(chip, CDRESUMECTL, 0x77, 0x77);
1842 if (retval != STATUS_SUCCESS) {
1843 rtsx_trace(chip);
1844 return TRANSPORT_FAILED;
1845 }
1846
1847 rtsx_enable_bus_int(chip);
1848
1849 retval = rtsx_read_phy_register(chip, 0x1C, ®);
1850 if (retval != STATUS_SUCCESS) {
1851 rtsx_trace(chip);
1852 return TRANSPORT_FAILED;
1853 }
1854
1855 reg &= 0xFFFE;
1856 retval = rtsx_write_phy_register(chip, 0x1C, reg);
1857 if (retval != STATUS_SUCCESS) {
1858 rtsx_trace(chip);
1859 return TRANSPORT_FAILED;
1860 }
1861 }
1862
1863 return TRANSPORT_GOOD;
1864}
1865
1866static int rw_mem_cmd_buf(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1867{
1868 int retval = STATUS_SUCCESS;
1869 unsigned int lun = SCSI_LUN(srb);
1870 u8 cmd_type, mask, value, idx;
1871 u16 addr;
1872
1873 rtsx_disable_aspm(chip);
1874
1875 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
1876 rtsx_exit_ss(chip);
1877 wait_timeout(100);
1878 }
1879 rtsx_set_stat(chip, RTSX_STAT_RUN);
1880
1881 switch (srb->cmnd[3]) {
1882 case INIT_BATCHCMD:
1883 rtsx_init_cmd(chip);
1884 break;
1885
1886 case ADD_BATCHCMD:
1887 cmd_type = srb->cmnd[4];
1888 if (cmd_type > 2) {
1889 set_sense_type(chip, lun,
1890 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1891 rtsx_trace(chip);
1892 return TRANSPORT_FAILED;
1893 }
1894 addr = (srb->cmnd[5] << 8) | srb->cmnd[6];
1895 mask = srb->cmnd[7];
1896 value = srb->cmnd[8];
1897 rtsx_add_cmd(chip, cmd_type, addr, mask, value);
1898 break;
1899
1900 case SEND_BATCHCMD:
1901 retval = rtsx_send_cmd(chip, 0, 1000);
1902 break;
1903
1904 case GET_BATCHRSP:
1905 idx = srb->cmnd[4];
1906 value = *(rtsx_get_cmd_data(chip) + idx);
1907 if (scsi_bufflen(srb) < 1) {
1908 set_sense_type(chip, lun,
1909 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1910 rtsx_trace(chip);
1911 return TRANSPORT_FAILED;
1912 }
1913 rtsx_stor_set_xfer_buf(&value, 1, srb);
1914 scsi_set_resid(srb, 0);
1915 break;
1916
1917 default:
1918 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1919 rtsx_trace(chip);
1920 return TRANSPORT_FAILED;
1921 }
1922
1923 if (retval != STATUS_SUCCESS) {
1924 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_WRITE_ERR);
1925 rtsx_trace(chip);
1926 return TRANSPORT_FAILED;
1927 }
1928
1929 return TRANSPORT_GOOD;
1930}
1931
1932static int suit_cmd(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1933{
1934 int result;
1935
1936 switch (srb->cmnd[3]) {
1937 case INIT_BATCHCMD:
1938 case ADD_BATCHCMD:
1939 case SEND_BATCHCMD:
1940 case GET_BATCHRSP:
1941 result = rw_mem_cmd_buf(srb, chip);
1942 break;
1943 default:
1944 result = TRANSPORT_ERROR;
1945 }
1946
1947 return result;
1948}
1949
1950static int read_phy_register(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1951{
1952 unsigned short addr, len, i;
1953 int retval;
1954 u8 *buf;
1955 u16 val;
1956
1957 rtsx_disable_aspm(chip);
1958
1959 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
1960 rtsx_exit_ss(chip);
1961 wait_timeout(100);
1962 }
1963 rtsx_set_stat(chip, RTSX_STAT_RUN);
1964
1965 addr = ((u16)srb->cmnd[4] << 8) | srb->cmnd[5];
1966 len = ((u16)srb->cmnd[6] << 8) | srb->cmnd[7];
1967
1968 if (len % 2)
1969 len -= len % 2;
1970
1971 if (len) {
1972 buf = vmalloc(len);
1973 if (!buf) {
1974 rtsx_trace(chip);
1975 return TRANSPORT_ERROR;
1976 }
1977
1978 retval = rtsx_force_power_on(chip, SSC_PDCTL);
1979 if (retval != STATUS_SUCCESS) {
1980 vfree(buf);
1981 set_sense_type(chip, SCSI_LUN(srb),
1982 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
1983 rtsx_trace(chip);
1984 return TRANSPORT_FAILED;
1985 }
1986
1987 for (i = 0; i < len / 2; i++) {
1988 retval = rtsx_read_phy_register(chip, addr + i, &val);
1989 if (retval != STATUS_SUCCESS) {
1990 vfree(buf);
1991 set_sense_type(chip, SCSI_LUN(srb),
1992 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
1993 rtsx_trace(chip);
1994 return TRANSPORT_FAILED;
1995 }
1996
1997 buf[2*i] = (u8)(val >> 8);
1998 buf[2*i+1] = (u8)val;
1999 }
2000
2001 len = (unsigned short)min_t(unsigned int, scsi_bufflen(srb),
2002 len);
2003 rtsx_stor_set_xfer_buf(buf, len, srb);
2004 scsi_set_resid(srb, scsi_bufflen(srb) - len);
2005
2006 vfree(buf);
2007 }
2008
2009 return TRANSPORT_GOOD;
2010}
2011
2012static int write_phy_register(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2013{
2014 unsigned short addr, len, i;
2015 int retval;
2016 u8 *buf;
2017 u16 val;
2018
2019 rtsx_disable_aspm(chip);
2020
2021 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
2022 rtsx_exit_ss(chip);
2023 wait_timeout(100);
2024 }
2025 rtsx_set_stat(chip, RTSX_STAT_RUN);
2026
2027 addr = ((u16)srb->cmnd[4] << 8) | srb->cmnd[5];
2028 len = ((u16)srb->cmnd[6] << 8) | srb->cmnd[7];
2029
2030 if (len % 2)
2031 len -= len % 2;
2032
2033 if (len) {
2034 len = (unsigned short)min_t(unsigned int, scsi_bufflen(srb),
2035 len);
2036
2037 buf = vmalloc(len);
2038 if (buf == NULL) {
2039 rtsx_trace(chip);
2040 return TRANSPORT_ERROR;
2041 }
2042
2043 rtsx_stor_get_xfer_buf(buf, len, srb);
2044 scsi_set_resid(srb, scsi_bufflen(srb) - len);
2045
2046 retval = rtsx_force_power_on(chip, SSC_PDCTL);
2047 if (retval != STATUS_SUCCESS) {
2048 vfree(buf);
2049 set_sense_type(chip, SCSI_LUN(srb),
2050 SENSE_TYPE_MEDIA_WRITE_ERR);
2051 rtsx_trace(chip);
2052 return TRANSPORT_FAILED;
2053 }
2054
2055 for (i = 0; i < len / 2; i++) {
2056 val = ((u16)buf[2*i] << 8) | buf[2*i+1];
2057 retval = rtsx_write_phy_register(chip, addr + i, val);
2058 if (retval != STATUS_SUCCESS) {
2059 vfree(buf);
2060 set_sense_type(chip, SCSI_LUN(srb),
2061 SENSE_TYPE_MEDIA_WRITE_ERR);
2062 rtsx_trace(chip);
2063 return TRANSPORT_FAILED;
2064 }
2065 }
2066
2067 vfree(buf);
2068 }
2069
2070 return TRANSPORT_GOOD;
2071}
2072
2073static int erase_eeprom2(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2074{
2075 unsigned short addr;
2076 int retval;
2077 u8 mode;
2078
2079 rtsx_disable_aspm(chip);
2080
2081 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
2082 rtsx_exit_ss(chip);
2083 wait_timeout(100);
2084 }
2085 rtsx_set_stat(chip, RTSX_STAT_RUN);
2086
2087 retval = rtsx_force_power_on(chip, SSC_PDCTL);
2088 if (retval != STATUS_SUCCESS) {
2089 set_sense_type(chip, SCSI_LUN(srb), SENSE_TYPE_MEDIA_WRITE_ERR);
2090 rtsx_trace(chip);
2091 return TRANSPORT_FAILED;
2092 }
2093
2094 mode = srb->cmnd[3];
2095 addr = ((u16)srb->cmnd[4] << 8) | srb->cmnd[5];
2096
2097 if (mode == 0) {
2098 retval = spi_erase_eeprom_chip(chip);
2099 if (retval != STATUS_SUCCESS) {
2100 set_sense_type(chip, SCSI_LUN(srb),
2101 SENSE_TYPE_MEDIA_WRITE_ERR);
2102 rtsx_trace(chip);
2103 return TRANSPORT_FAILED;
2104 }
2105 } else if (mode == 1) {
2106 retval = spi_erase_eeprom_byte(chip, addr);
2107 if (retval != STATUS_SUCCESS) {
2108 set_sense_type(chip, SCSI_LUN(srb),
2109 SENSE_TYPE_MEDIA_WRITE_ERR);
2110 rtsx_trace(chip);
2111 return TRANSPORT_FAILED;
2112 }
2113 } else {
2114 set_sense_type(chip, SCSI_LUN(srb),
2115 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
2116 rtsx_trace(chip);
2117 return TRANSPORT_FAILED;
2118 }
2119
2120 return TRANSPORT_GOOD;
2121}
2122
2123static int read_eeprom2(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2124{
2125 unsigned short addr, len, i;
2126 int retval;
2127 u8 *buf;
2128
2129 rtsx_disable_aspm(chip);
2130
2131 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
2132 rtsx_exit_ss(chip);
2133 wait_timeout(100);
2134 }
2135 rtsx_set_stat(chip, RTSX_STAT_RUN);
2136
2137 addr = ((u16)srb->cmnd[4] << 8) | srb->cmnd[5];
2138 len = ((u16)srb->cmnd[6] << 8) | srb->cmnd[7];
2139
2140 buf = vmalloc(len);
2141 if (!buf) {
2142 rtsx_trace(chip);
2143 return TRANSPORT_ERROR;
2144 }
2145
2146 retval = rtsx_force_power_on(chip, SSC_PDCTL);
2147 if (retval != STATUS_SUCCESS) {
2148 vfree(buf);
2149 set_sense_type(chip, SCSI_LUN(srb),
2150 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
2151 rtsx_trace(chip);
2152 return TRANSPORT_FAILED;
2153 }
2154
2155 for (i = 0; i < len; i++) {
2156 retval = spi_read_eeprom(chip, addr + i, buf + i);
2157 if (retval != STATUS_SUCCESS) {
2158 vfree(buf);
2159 set_sense_type(chip, SCSI_LUN(srb),
2160 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
2161 rtsx_trace(chip);
2162 return TRANSPORT_FAILED;
2163 }
2164 }
2165
2166 len = (unsigned short)min_t(unsigned int, scsi_bufflen(srb), len);
2167 rtsx_stor_set_xfer_buf(buf, len, srb);
2168 scsi_set_resid(srb, scsi_bufflen(srb) - len);
2169
2170 vfree(buf);
2171
2172 return TRANSPORT_GOOD;
2173}
2174
2175static int write_eeprom2(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2176{
2177 unsigned short addr, len, i;
2178 int retval;
2179 u8 *buf;
2180
2181 rtsx_disable_aspm(chip);
2182
2183 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
2184 rtsx_exit_ss(chip);
2185 wait_timeout(100);
2186 }
2187 rtsx_set_stat(chip, RTSX_STAT_RUN);
2188
2189 addr = ((u16)srb->cmnd[4] << 8) | srb->cmnd[5];
2190 len = ((u16)srb->cmnd[6] << 8) | srb->cmnd[7];
2191
2192 len = (unsigned short)min_t(unsigned int, scsi_bufflen(srb), len);
2193 buf = vmalloc(len);
2194 if (buf == NULL) {
2195 rtsx_trace(chip);
2196 return TRANSPORT_ERROR;
2197 }
2198
2199 rtsx_stor_get_xfer_buf(buf, len, srb);
2200 scsi_set_resid(srb, scsi_bufflen(srb) - len);
2201
2202 retval = rtsx_force_power_on(chip, SSC_PDCTL);
2203 if (retval != STATUS_SUCCESS) {
2204 vfree(buf);
2205 set_sense_type(chip, SCSI_LUN(srb), SENSE_TYPE_MEDIA_WRITE_ERR);
2206 rtsx_trace(chip);
2207 return TRANSPORT_FAILED;
2208 }
2209
2210 for (i = 0; i < len; i++) {
2211 retval = spi_write_eeprom(chip, addr + i, buf[i]);
2212 if (retval != STATUS_SUCCESS) {
2213 vfree(buf);
2214 set_sense_type(chip, SCSI_LUN(srb),
2215 SENSE_TYPE_MEDIA_WRITE_ERR);
2216 rtsx_trace(chip);
2217 return TRANSPORT_FAILED;
2218 }
2219 }
2220
2221 vfree(buf);
2222
2223 return TRANSPORT_GOOD;
2224}
2225
2226static int read_efuse(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2227{
2228 int retval;
2229 u8 addr, len, i;
2230 u8 *buf;
2231
2232 rtsx_disable_aspm(chip);
2233
2234 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
2235 rtsx_exit_ss(chip);
2236 wait_timeout(100);
2237 }
2238 rtsx_set_stat(chip, RTSX_STAT_RUN);
2239
2240 addr = srb->cmnd[4];
2241 len = srb->cmnd[5];
2242
2243 buf = vmalloc(len);
2244 if (!buf) {
2245 rtsx_trace(chip);
2246 return TRANSPORT_ERROR;
2247 }
2248
2249 retval = rtsx_force_power_on(chip, SSC_PDCTL);
2250 if (retval != STATUS_SUCCESS) {
2251 vfree(buf);
2252 set_sense_type(chip, SCSI_LUN(srb),
2253 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
2254 rtsx_trace(chip);
2255 return TRANSPORT_FAILED;
2256 }
2257
2258 for (i = 0; i < len; i++) {
2259 retval = rtsx_read_efuse(chip, addr + i, buf + i);
2260 if (retval != STATUS_SUCCESS) {
2261 vfree(buf);
2262 set_sense_type(chip, SCSI_LUN(srb),
2263 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
2264 rtsx_trace(chip);
2265 return TRANSPORT_FAILED;
2266 }
2267 }
2268
2269 len = (u8)min_t(unsigned int, scsi_bufflen(srb), len);
2270 rtsx_stor_set_xfer_buf(buf, len, srb);
2271 scsi_set_resid(srb, scsi_bufflen(srb) - len);
2272
2273 vfree(buf);
2274
2275 return TRANSPORT_GOOD;
2276}
2277
2278static int write_efuse(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2279{
2280 int retval, result = TRANSPORT_GOOD;
2281 u16 val;
2282 u8 addr, len, i;
2283 u8 *buf;
2284
2285 rtsx_disable_aspm(chip);
2286
2287 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
2288 rtsx_exit_ss(chip);
2289 wait_timeout(100);
2290 }
2291 rtsx_set_stat(chip, RTSX_STAT_RUN);
2292
2293 addr = srb->cmnd[4];
2294 len = srb->cmnd[5];
2295
2296 len = (u8)min_t(unsigned int, scsi_bufflen(srb), len);
2297 buf = vmalloc(len);
2298 if (buf == NULL) {
2299 rtsx_trace(chip);
2300 return TRANSPORT_ERROR;
2301 }
2302
2303 rtsx_stor_get_xfer_buf(buf, len, srb);
2304 scsi_set_resid(srb, scsi_bufflen(srb) - len);
2305
2306 retval = rtsx_force_power_on(chip, SSC_PDCTL);
2307 if (retval != STATUS_SUCCESS) {
2308 vfree(buf);
2309 rtsx_trace(chip);
2310 return TRANSPORT_ERROR;
2311 }
2312
2313 if (chip->asic_code) {
2314 retval = rtsx_read_phy_register(chip, 0x08, &val);
2315 if (retval != STATUS_SUCCESS) {
2316 vfree(buf);
2317 rtsx_trace(chip);
2318 return TRANSPORT_ERROR;
2319 }
2320
2321 retval = rtsx_write_register(chip, PWR_GATE_CTRL,
2322 LDO3318_PWR_MASK, LDO_OFF);
2323 if (retval != STATUS_SUCCESS) {
2324 vfree(buf);
2325 rtsx_trace(chip);
2326 return TRANSPORT_ERROR;
2327 }
2328
2329 wait_timeout(600);
2330
2331 retval = rtsx_write_phy_register(chip, 0x08,
2332 0x4C00 | chip->phy_voltage);
2333 if (retval != STATUS_SUCCESS) {
2334 vfree(buf);
2335 rtsx_trace(chip);
2336 return TRANSPORT_ERROR;
2337 }
2338
2339 retval = rtsx_write_register(chip, PWR_GATE_CTRL,
2340 LDO3318_PWR_MASK, LDO_ON);
2341 if (retval != STATUS_SUCCESS) {
2342 vfree(buf);
2343 rtsx_trace(chip);
2344 return TRANSPORT_ERROR;
2345 }
2346
2347 wait_timeout(600);
2348 }
2349
2350 retval = card_power_on(chip, SPI_CARD);
2351 if (retval != STATUS_SUCCESS) {
2352 vfree(buf);
2353 rtsx_trace(chip);
2354 return TRANSPORT_ERROR;
2355 }
2356
2357 wait_timeout(50);
2358
2359 for (i = 0; i < len; i++) {
2360 retval = rtsx_write_efuse(chip, addr + i, buf[i]);
2361 if (retval != STATUS_SUCCESS) {
2362 set_sense_type(chip, SCSI_LUN(srb),
2363 SENSE_TYPE_MEDIA_WRITE_ERR);
2364 result = TRANSPORT_FAILED;
2365 rtsx_trace(chip);
2366 goto Exit;
2367 }
2368 }
2369
2370Exit:
2371 vfree(buf);
2372
2373 retval = card_power_off(chip, SPI_CARD);
2374 if (retval != STATUS_SUCCESS) {
2375 rtsx_trace(chip);
2376 return TRANSPORT_ERROR;
2377 }
2378
2379 if (chip->asic_code) {
2380 retval = rtsx_write_register(chip, PWR_GATE_CTRL,
2381 LDO3318_PWR_MASK, LDO_OFF);
2382 if (retval != STATUS_SUCCESS) {
2383 rtsx_trace(chip);
2384 return TRANSPORT_ERROR;
2385 }
2386
2387 wait_timeout(600);
2388
2389 retval = rtsx_write_phy_register(chip, 0x08, val);
2390 if (retval != STATUS_SUCCESS) {
2391 rtsx_trace(chip);
2392 return TRANSPORT_ERROR;
2393 }
2394
2395 retval = rtsx_write_register(chip, PWR_GATE_CTRL,
2396 LDO3318_PWR_MASK, LDO_ON);
2397 if (retval != STATUS_SUCCESS) {
2398 rtsx_trace(chip);
2399 return TRANSPORT_ERROR;
2400 }
2401 }
2402
2403 return result;
2404}
2405
2406static int read_cfg_byte(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2407{
2408 int retval;
2409 bool func_max;
2410 u8 func;
2411 u16 addr, len;
2412 u8 *buf;
2413
2414 rtsx_disable_aspm(chip);
2415
2416 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
2417 rtsx_exit_ss(chip);
2418 wait_timeout(100);
2419 }
2420 rtsx_set_stat(chip, RTSX_STAT_RUN);
2421
2422 func = srb->cmnd[3];
2423 addr = ((u16)(srb->cmnd[4]) << 8) | srb->cmnd[5];
2424 len = ((u16)(srb->cmnd[6]) << 8) | srb->cmnd[7];
2425
2426 dev_dbg(rtsx_dev(chip), "%s: func = %d, addr = 0x%x, len = %d\n",
2427 __func__, func, addr, len);
2428
2429 if (CHK_SDIO_EXIST(chip) && !CHK_SDIO_IGNORED(chip))
2430 func_max = true;
2431 else
2432 func_max = false;
2433
2434 if (func > func_max) {
2435 set_sense_type(chip, SCSI_LUN(srb),
2436 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
2437 rtsx_trace(chip);
2438 return TRANSPORT_FAILED;
2439 }
2440
2441 buf = vmalloc(len);
2442 if (!buf) {
2443 rtsx_trace(chip);
2444 return TRANSPORT_ERROR;
2445 }
2446
2447 retval = rtsx_read_cfg_seq(chip, func, addr, buf, len);
2448 if (retval != STATUS_SUCCESS) {
2449 set_sense_type(chip, SCSI_LUN(srb),
2450 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
2451 vfree(buf);
2452 rtsx_trace(chip);
2453 return TRANSPORT_FAILED;
2454 }
2455
2456 len = (u16)min_t(unsigned int, scsi_bufflen(srb), len);
2457 rtsx_stor_set_xfer_buf(buf, len, srb);
2458 scsi_set_resid(srb, scsi_bufflen(srb) - len);
2459
2460 vfree(buf);
2461
2462 return TRANSPORT_GOOD;
2463}
2464
2465static int write_cfg_byte(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2466{
2467 int retval;
2468 bool func_max;
2469 u8 func;
2470 u16 addr, len;
2471 u8 *buf;
2472
2473 rtsx_disable_aspm(chip);
2474
2475 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
2476 rtsx_exit_ss(chip);
2477 wait_timeout(100);
2478 }
2479 rtsx_set_stat(chip, RTSX_STAT_RUN);
2480
2481 func = srb->cmnd[3];
2482 addr = ((u16)(srb->cmnd[4]) << 8) | srb->cmnd[5];
2483 len = ((u16)(srb->cmnd[6]) << 8) | srb->cmnd[7];
2484
2485 dev_dbg(rtsx_dev(chip), "%s: func = %d, addr = 0x%x\n",
2486 __func__, func, addr);
2487
2488 if (CHK_SDIO_EXIST(chip) && !CHK_SDIO_IGNORED(chip))
2489 func_max = true;
2490 else
2491 func_max = false;
2492
2493 if (func > func_max) {
2494 set_sense_type(chip, SCSI_LUN(srb),
2495 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
2496 rtsx_trace(chip);
2497 return TRANSPORT_FAILED;
2498 }
2499
2500 len = (unsigned short)min_t(unsigned int, scsi_bufflen(srb), len);
2501 buf = vmalloc(len);
2502 if (!buf) {
2503 rtsx_trace(chip);
2504 return TRANSPORT_ERROR;
2505 }
2506
2507 rtsx_stor_get_xfer_buf(buf, len, srb);
2508 scsi_set_resid(srb, scsi_bufflen(srb) - len);
2509
2510 retval = rtsx_write_cfg_seq(chip, func, addr, buf, len);
2511 if (retval != STATUS_SUCCESS) {
2512 set_sense_type(chip, SCSI_LUN(srb), SENSE_TYPE_MEDIA_WRITE_ERR);
2513 vfree(buf);
2514 rtsx_trace(chip);
2515 return TRANSPORT_FAILED;
2516 }
2517
2518 vfree(buf);
2519
2520 return TRANSPORT_GOOD;
2521}
2522
2523static int app_cmd(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2524{
2525 int result;
2526
2527 switch (srb->cmnd[2]) {
2528 case PP_READ10:
2529 case PP_WRITE10:
2530 result = read_write(srb, chip);
2531 break;
2532
2533 case READ_HOST_REG:
2534 result = read_host_reg(srb, chip);
2535 break;
2536
2537 case WRITE_HOST_REG:
2538 result = write_host_reg(srb, chip);
2539 break;
2540
2541 case GET_VAR:
2542 result = get_variable(srb, chip);
2543 break;
2544
2545 case SET_VAR:
2546 result = set_variable(srb, chip);
2547 break;
2548
2549 case DMA_READ:
2550 case DMA_WRITE:
2551 result = dma_access_ring_buffer(srb, chip);
2552 break;
2553
2554 case READ_PHY:
2555 result = read_phy_register(srb, chip);
2556 break;
2557
2558 case WRITE_PHY:
2559 result = write_phy_register(srb, chip);
2560 break;
2561
2562 case ERASE_EEPROM2:
2563 result = erase_eeprom2(srb, chip);
2564 break;
2565
2566 case READ_EEPROM2:
2567 result = read_eeprom2(srb, chip);
2568 break;
2569
2570 case WRITE_EEPROM2:
2571 result = write_eeprom2(srb, chip);
2572 break;
2573
2574 case READ_EFUSE:
2575 result = read_efuse(srb, chip);
2576 break;
2577
2578 case WRITE_EFUSE:
2579 result = write_efuse(srb, chip);
2580 break;
2581
2582 case READ_CFG:
2583 result = read_cfg_byte(srb, chip);
2584 break;
2585
2586 case WRITE_CFG:
2587 result = write_cfg_byte(srb, chip);
2588 break;
2589
2590 case SET_CHIP_MODE:
2591 result = set_chip_mode(srb, chip);
2592 break;
2593
2594 case SUIT_CMD:
2595 result = suit_cmd(srb, chip);
2596 break;
2597
2598 case GET_DEV_STATUS:
2599 result = get_dev_status(srb, chip);
2600 break;
2601
2602 default:
2603 set_sense_type(chip, SCSI_LUN(srb),
2604 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
2605 rtsx_trace(chip);
2606 return TRANSPORT_FAILED;
2607 }
2608
2609 return result;
2610}
2611
2612
2613static int read_status(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2614{
2615 u8 rtsx_status[16];
2616 int buf_len;
2617 unsigned int lun = SCSI_LUN(srb);
2618
2619 rtsx_status[0] = (u8)(chip->vendor_id >> 8);
2620 rtsx_status[1] = (u8)(chip->vendor_id);
2621
2622 rtsx_status[2] = (u8)(chip->product_id >> 8);
2623 rtsx_status[3] = (u8)(chip->product_id);
2624
2625 rtsx_status[4] = (u8)lun;
2626
2627 if (CHECK_LUN_MODE(chip, SD_MS_2LUN)) {
2628 if (chip->lun2card[lun] == SD_CARD)
2629 rtsx_status[5] = 2;
2630 else
2631 rtsx_status[5] = 3;
2632 } else {
2633 if (chip->card_exist) {
2634 if (chip->card_exist & XD_CARD)
2635 rtsx_status[5] = 4;
2636 else if (chip->card_exist & SD_CARD)
2637 rtsx_status[5] = 2;
2638 else if (chip->card_exist & MS_CARD)
2639 rtsx_status[5] = 3;
2640 else
2641 rtsx_status[5] = 7;
2642 } else {
2643 rtsx_status[5] = 7;
2644 }
2645 }
2646
2647 if (CHECK_LUN_MODE(chip, SD_MS_2LUN))
2648 rtsx_status[6] = 2;
2649 else
2650 rtsx_status[6] = 1;
2651
2652 rtsx_status[7] = (u8)(chip->product_id);
2653 rtsx_status[8] = chip->ic_version;
2654
2655 if (check_card_exist(chip, lun))
2656 rtsx_status[9] = 1;
2657 else
2658 rtsx_status[9] = 0;
2659
2660 if (CHECK_LUN_MODE(chip, SD_MS_2LUN))
2661 rtsx_status[10] = 0;
2662 else
2663 rtsx_status[10] = 1;
2664
2665 if (CHECK_LUN_MODE(chip, SD_MS_2LUN)) {
2666 if (chip->lun2card[lun] == SD_CARD)
2667 rtsx_status[11] = SD_CARD;
2668 else
2669 rtsx_status[11] = MS_CARD;
2670 } else {
2671 rtsx_status[11] = XD_CARD | SD_CARD | MS_CARD;
2672 }
2673
2674 if (check_card_ready(chip, lun))
2675 rtsx_status[12] = 1;
2676 else
2677 rtsx_status[12] = 0;
2678
2679 if (get_lun_card(chip, lun) == XD_CARD) {
2680 rtsx_status[13] = 0x40;
2681 } else if (get_lun_card(chip, lun) == SD_CARD) {
2682 struct sd_info *sd_card = &(chip->sd_card);
2683
2684 rtsx_status[13] = 0x20;
2685 if (CHK_SD(sd_card)) {
2686 if (CHK_SD_HCXC(sd_card))
2687 rtsx_status[13] |= 0x04;
2688 if (CHK_SD_HS(sd_card))
2689 rtsx_status[13] |= 0x02;
2690 } else {
2691 rtsx_status[13] |= 0x08;
2692 if (CHK_MMC_52M(sd_card))
2693 rtsx_status[13] |= 0x02;
2694 if (CHK_MMC_SECTOR_MODE(sd_card))
2695 rtsx_status[13] |= 0x04;
2696 }
2697 } else if (get_lun_card(chip, lun) == MS_CARD) {
2698 struct ms_info *ms_card = &(chip->ms_card);
2699
2700 if (CHK_MSPRO(ms_card)) {
2701 rtsx_status[13] = 0x38;
2702 if (CHK_HG8BIT(ms_card))
2703 rtsx_status[13] |= 0x04;
2704#ifdef SUPPORT_MSXC
2705 if (CHK_MSXC(ms_card))
2706 rtsx_status[13] |= 0x01;
2707#endif
2708 } else {
2709 rtsx_status[13] = 0x30;
2710 }
2711 } else {
2712 if (CHECK_LUN_MODE(chip, DEFAULT_SINGLE)) {
2713#ifdef SUPPORT_SDIO
2714 if (chip->sd_io && chip->sd_int)
2715 rtsx_status[13] = 0x60;
2716 else
2717 rtsx_status[13] = 0x70;
2718#else
2719 rtsx_status[13] = 0x70;
2720#endif
2721 } else {
2722 if (chip->lun2card[lun] == SD_CARD)
2723 rtsx_status[13] = 0x20;
2724 else
2725 rtsx_status[13] = 0x30;
2726 }
2727 }
2728
2729 rtsx_status[14] = 0x78;
2730 if (CHK_SDIO_EXIST(chip) && !CHK_SDIO_IGNORED(chip))
2731 rtsx_status[15] = 0x83;
2732 else
2733 rtsx_status[15] = 0x82;
2734
2735 buf_len = min_t(unsigned int, scsi_bufflen(srb), sizeof(rtsx_status));
2736 rtsx_stor_set_xfer_buf(rtsx_status, buf_len, srb);
2737 scsi_set_resid(srb, scsi_bufflen(srb) - buf_len);
2738
2739 return TRANSPORT_GOOD;
2740}
2741
2742static int get_card_bus_width(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2743{
2744 unsigned int lun = SCSI_LUN(srb);
2745 u8 card, bus_width;
2746
2747 if (!check_card_ready(chip, lun)) {
2748 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
2749 rtsx_trace(chip);
2750 return TRANSPORT_FAILED;
2751 }
2752
2753 card = get_lun_card(chip, lun);
2754 if ((card == SD_CARD) || (card == MS_CARD)) {
2755 bus_width = chip->card_bus_width[lun];
2756 } else {
2757 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
2758 rtsx_trace(chip);
2759 return TRANSPORT_FAILED;
2760 }
2761
2762 scsi_set_resid(srb, 0);
2763 rtsx_stor_set_xfer_buf(&bus_width, scsi_bufflen(srb), srb);
2764
2765 return TRANSPORT_GOOD;
2766}
2767
2768static int spi_vendor_cmd(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2769{
2770 int result;
2771 unsigned int lun = SCSI_LUN(srb);
2772 u8 gpio_dir;
2773
2774 if (CHECK_PID(chip, 0x5208) || CHECK_PID(chip, 0x5288)) {
2775 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
2776 rtsx_trace(chip);
2777 return TRANSPORT_FAILED;
2778 }
2779
2780 rtsx_disable_aspm(chip);
2781
2782 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
2783 rtsx_exit_ss(chip);
2784 wait_timeout(100);
2785 }
2786 rtsx_set_stat(chip, RTSX_STAT_RUN);
2787
2788 rtsx_force_power_on(chip, SSC_PDCTL);
2789
2790 rtsx_read_register(chip, CARD_GPIO_DIR, &gpio_dir);
2791 rtsx_write_register(chip, CARD_GPIO_DIR, 0x07, gpio_dir & 0x06);
2792
2793 switch (srb->cmnd[2]) {
2794 case SCSI_SPI_GETSTATUS:
2795 result = spi_get_status(srb, chip);
2796 break;
2797
2798 case SCSI_SPI_SETPARAMETER:
2799 result = spi_set_parameter(srb, chip);
2800 break;
2801
2802 case SCSI_SPI_READFALSHID:
2803 result = spi_read_flash_id(srb, chip);
2804 break;
2805
2806 case SCSI_SPI_READFLASH:
2807 result = spi_read_flash(srb, chip);
2808 break;
2809
2810 case SCSI_SPI_WRITEFLASH:
2811 result = spi_write_flash(srb, chip);
2812 break;
2813
2814 case SCSI_SPI_WRITEFLASHSTATUS:
2815 result = spi_write_flash_status(srb, chip);
2816 break;
2817
2818 case SCSI_SPI_ERASEFLASH:
2819 result = spi_erase_flash(srb, chip);
2820 break;
2821
2822 default:
2823 rtsx_write_register(chip, CARD_GPIO_DIR, 0x07, gpio_dir);
2824
2825 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
2826 rtsx_trace(chip);
2827 return TRANSPORT_FAILED;
2828 }
2829
2830 rtsx_write_register(chip, CARD_GPIO_DIR, 0x07, gpio_dir);
2831
2832 if (result != STATUS_SUCCESS) {
2833 rtsx_trace(chip);
2834 return TRANSPORT_FAILED;
2835 }
2836
2837 return TRANSPORT_GOOD;
2838}
2839
2840static int vendor_cmnd(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2841{
2842 int result;
2843
2844 switch (srb->cmnd[1]) {
2845 case READ_STATUS:
2846 result = read_status(srb, chip);
2847 break;
2848
2849 case READ_MEM:
2850 result = read_mem(srb, chip);
2851 break;
2852
2853 case WRITE_MEM:
2854 result = write_mem(srb, chip);
2855 break;
2856
2857 case READ_EEPROM:
2858 result = read_eeprom(srb, chip);
2859 break;
2860
2861 case WRITE_EEPROM:
2862 result = write_eeprom(srb, chip);
2863 break;
2864
2865 case TOGGLE_GPIO:
2866 result = toggle_gpio_cmd(srb, chip);
2867 break;
2868
2869 case GET_SD_CSD:
2870 result = get_sd_csd(srb, chip);
2871 break;
2872
2873 case GET_BUS_WIDTH:
2874 result = get_card_bus_width(srb, chip);
2875 break;
2876
2877#ifdef _MSG_TRACE
2878 case TRACE_MSG:
2879 result = trace_msg_cmd(srb, chip);
2880 break;
2881#endif
2882
2883 case SCSI_APP_CMD:
2884 result = app_cmd(srb, chip);
2885 break;
2886
2887 case SPI_VENDOR_COMMAND:
2888 result = spi_vendor_cmd(srb, chip);
2889 break;
2890
2891 default:
2892 set_sense_type(chip, SCSI_LUN(srb),
2893 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
2894 rtsx_trace(chip);
2895 return TRANSPORT_FAILED;
2896 }
2897
2898 return result;
2899}
2900
2901#if !defined(LED_AUTO_BLINK) && !defined(REGULAR_BLINK)
2902void led_shine(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2903{
2904 unsigned int lun = SCSI_LUN(srb);
2905 u16 sec_cnt;
2906
2907 if ((srb->cmnd[0] == READ_10) || (srb->cmnd[0] == WRITE_10))
2908 sec_cnt = ((u16)(srb->cmnd[7]) << 8) | srb->cmnd[8];
2909 else if ((srb->cmnd[0] == READ_6) || (srb->cmnd[0] == WRITE_6)) {
2910 sec_cnt = srb->cmnd[4];
2911 if (sec_cnt == 0)
2912 sec_cnt = 256;
2913 } else
2914 return;
2915
2916 if (chip->rw_cap[lun] >= GPIO_TOGGLE_THRESHOLD) {
2917 toggle_gpio(chip, LED_GPIO);
2918 chip->rw_cap[lun] = 0;
2919 } else {
2920 chip->rw_cap[lun] += sec_cnt;
2921 }
2922}
2923#endif
2924
2925static int ms_format_cmnd(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2926{
2927 struct ms_info *ms_card = &(chip->ms_card);
2928 unsigned int lun = SCSI_LUN(srb);
2929 bool quick_format;
2930 int retval;
2931
2932 if (get_lun_card(chip, lun) != MS_CARD) {
2933 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT);
2934 rtsx_trace(chip);
2935 return TRANSPORT_FAILED;
2936 }
2937
2938 if ((srb->cmnd[3] != 0x4D) || (srb->cmnd[4] != 0x47) ||
2939 (srb->cmnd[5] != 0x66) || (srb->cmnd[6] != 0x6D) ||
2940 (srb->cmnd[7] != 0x74)) {
2941 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
2942 rtsx_trace(chip);
2943 return TRANSPORT_FAILED;
2944 }
2945
2946 rtsx_disable_aspm(chip);
2947
2948 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
2949 rtsx_exit_ss(chip);
2950 wait_timeout(100);
2951
2952 if (!check_card_ready(chip, lun) ||
2953 (get_card_size(chip, lun) == 0)) {
2954 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
2955 rtsx_trace(chip);
2956 return TRANSPORT_FAILED;
2957 }
2958 }
2959 rtsx_set_stat(chip, RTSX_STAT_RUN);
2960
2961 if (srb->cmnd[8] & 0x01)
2962 quick_format = false;
2963 else
2964 quick_format = true;
2965
2966 if (!(chip->card_ready & MS_CARD)) {
2967 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
2968 rtsx_trace(chip);
2969 return TRANSPORT_FAILED;
2970 }
2971
2972 if (chip->card_wp & MS_CARD) {
2973 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_WRITE_PROTECT);
2974 rtsx_trace(chip);
2975 return TRANSPORT_FAILED;
2976 }
2977
2978 if (!CHK_MSPRO(ms_card)) {
2979 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT);
2980 rtsx_trace(chip);
2981 return TRANSPORT_FAILED;
2982 }
2983
2984 retval = mspro_format(srb, chip, MS_SHORT_DATA_LEN, quick_format);
2985 if (retval != STATUS_SUCCESS) {
2986 set_sense_type(chip, lun, SENSE_TYPE_FORMAT_CMD_FAILED);
2987 rtsx_trace(chip);
2988 return TRANSPORT_FAILED;
2989 }
2990
2991 scsi_set_resid(srb, 0);
2992 return TRANSPORT_GOOD;
2993}
2994
2995#ifdef SUPPORT_PCGL_1P18
2996static int get_ms_information(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2997{
2998 struct ms_info *ms_card = &(chip->ms_card);
2999 unsigned int lun = SCSI_LUN(srb);
3000 u8 dev_info_id, data_len;
3001 u8 *buf;
3002 unsigned int buf_len;
3003 int i;
3004
3005 if (!check_card_ready(chip, lun)) {
3006 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
3007 rtsx_trace(chip);
3008 return TRANSPORT_FAILED;
3009 }
3010 if (get_lun_card(chip, lun) != MS_CARD) {
3011 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT);
3012 rtsx_trace(chip);
3013 return TRANSPORT_FAILED;
3014 }
3015
3016 if ((srb->cmnd[2] != 0xB0) || (srb->cmnd[4] != 0x4D) ||
3017 (srb->cmnd[5] != 0x53) || (srb->cmnd[6] != 0x49) ||
3018 (srb->cmnd[7] != 0x44)) {
3019 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3020 rtsx_trace(chip);
3021 return TRANSPORT_FAILED;
3022 }
3023
3024 dev_info_id = srb->cmnd[3];
3025 if ((CHK_MSXC(ms_card) && (dev_info_id == 0x10)) ||
3026 (!CHK_MSXC(ms_card) && (dev_info_id == 0x13)) ||
3027 !CHK_MSPRO(ms_card)) {
3028 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3029 rtsx_trace(chip);
3030 return TRANSPORT_FAILED;
3031 }
3032
3033 if (dev_info_id == 0x15)
3034 buf_len = data_len = 0x3A;
3035 else
3036 buf_len = data_len = 0x6A;
3037
3038 buf = kmalloc(buf_len, GFP_KERNEL);
3039 if (!buf) {
3040 rtsx_trace(chip);
3041 return TRANSPORT_ERROR;
3042 }
3043
3044 i = 0;
3045
3046 buf[i++] = 0x00;
3047 buf[i++] = data_len;
3048
3049 if (CHK_MSXC(ms_card))
3050 buf[i++] = 0x03;
3051 else
3052 buf[i++] = 0x02;
3053
3054
3055 buf[i++] = 0x01;
3056
3057 buf[i++] = 0x00;
3058 buf[i++] = 0x00;
3059 buf[i++] = 0x00;
3060
3061 buf[i++] = 0x01;
3062
3063
3064
3065
3066 buf[i++] = dev_info_id;
3067
3068 if (dev_info_id == 0x15)
3069 data_len = 0x31;
3070 else
3071 data_len = 0x61;
3072
3073 buf[i++] = 0x00;
3074 buf[i++] = data_len;
3075
3076 buf[i++] = 0x80;
3077 if ((dev_info_id == 0x10) || (dev_info_id == 0x13)) {
3078
3079 memcpy(buf+i, ms_card->raw_sys_info, 96);
3080 } else {
3081
3082 memcpy(buf+i, ms_card->raw_model_name, 48);
3083 }
3084
3085 rtsx_stor_set_xfer_buf(buf, buf_len, srb);
3086
3087 if (dev_info_id == 0x15)
3088 scsi_set_resid(srb, scsi_bufflen(srb)-0x3C);
3089 else
3090 scsi_set_resid(srb, scsi_bufflen(srb)-0x6C);
3091
3092 kfree(buf);
3093 return STATUS_SUCCESS;
3094}
3095#endif
3096
3097static int ms_sp_cmnd(struct scsi_cmnd *srb, struct rtsx_chip *chip)
3098{
3099 int retval = TRANSPORT_ERROR;
3100
3101 if (srb->cmnd[2] == MS_FORMAT)
3102 retval = ms_format_cmnd(srb, chip);
3103#ifdef SUPPORT_PCGL_1P18
3104 else if (srb->cmnd[2] == GET_MS_INFORMATION)
3105 retval = get_ms_information(srb, chip);
3106#endif
3107
3108 return retval;
3109}
3110
3111#ifdef SUPPORT_CPRM
3112static int sd_extention_cmnd(struct scsi_cmnd *srb, struct rtsx_chip *chip)
3113{
3114 unsigned int lun = SCSI_LUN(srb);
3115 int result;
3116
3117 rtsx_disable_aspm(chip);
3118
3119 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
3120 rtsx_exit_ss(chip);
3121 wait_timeout(100);
3122 }
3123 rtsx_set_stat(chip, RTSX_STAT_RUN);
3124
3125 sd_cleanup_work(chip);
3126
3127 if (!check_card_ready(chip, lun)) {
3128 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
3129 rtsx_trace(chip);
3130 return TRANSPORT_FAILED;
3131 }
3132 if (get_lun_card(chip, lun) != SD_CARD) {
3133 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT);
3134 rtsx_trace(chip);
3135 return TRANSPORT_FAILED;
3136 }
3137
3138 switch (srb->cmnd[0]) {
3139 case SD_PASS_THRU_MODE:
3140 result = sd_pass_thru_mode(srb, chip);
3141 break;
3142
3143 case SD_EXECUTE_NO_DATA:
3144 result = sd_execute_no_data(srb, chip);
3145 break;
3146
3147 case SD_EXECUTE_READ:
3148 result = sd_execute_read_data(srb, chip);
3149 break;
3150
3151 case SD_EXECUTE_WRITE:
3152 result = sd_execute_write_data(srb, chip);
3153 break;
3154
3155 case SD_GET_RSP:
3156 result = sd_get_cmd_rsp(srb, chip);
3157 break;
3158
3159 case SD_HW_RST:
3160 result = sd_hw_rst(srb, chip);
3161 break;
3162
3163 default:
3164 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3165 rtsx_trace(chip);
3166 return TRANSPORT_FAILED;
3167 }
3168
3169 return result;
3170}
3171#endif
3172
3173#ifdef SUPPORT_MAGIC_GATE
3174static int mg_report_key(struct scsi_cmnd *srb, struct rtsx_chip *chip)
3175{
3176 struct ms_info *ms_card = &(chip->ms_card);
3177 unsigned int lun = SCSI_LUN(srb);
3178 int retval;
3179 u8 key_format;
3180
3181 rtsx_disable_aspm(chip);
3182
3183 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
3184 rtsx_exit_ss(chip);
3185 wait_timeout(100);
3186 }
3187 rtsx_set_stat(chip, RTSX_STAT_RUN);
3188
3189 ms_cleanup_work(chip);
3190
3191 if (!check_card_ready(chip, lun)) {
3192 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
3193 rtsx_trace(chip);
3194 return TRANSPORT_FAILED;
3195 }
3196 if (get_lun_card(chip, lun) != MS_CARD) {
3197 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT);
3198 rtsx_trace(chip);
3199 return TRANSPORT_FAILED;
3200 }
3201
3202 if (srb->cmnd[7] != KC_MG_R_PRO) {
3203 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3204 rtsx_trace(chip);
3205 return TRANSPORT_FAILED;
3206 }
3207
3208 if (!CHK_MSPRO(ms_card)) {
3209 set_sense_type(chip, lun, SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM);
3210 rtsx_trace(chip);
3211 return TRANSPORT_FAILED;
3212 }
3213
3214 key_format = srb->cmnd[10] & 0x3F;
3215 dev_dbg(rtsx_dev(chip), "key_format = 0x%x\n", key_format);
3216
3217 switch (key_format) {
3218 case KF_GET_LOC_EKB:
3219 if ((scsi_bufflen(srb) == 0x41C) &&
3220 (srb->cmnd[8] == 0x04) &&
3221 (srb->cmnd[9] == 0x1C)) {
3222 retval = mg_get_local_EKB(srb, chip);
3223 if (retval != STATUS_SUCCESS) {
3224 rtsx_trace(chip);
3225 return TRANSPORT_FAILED;
3226 }
3227
3228 } else {
3229 set_sense_type(chip, lun,
3230 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3231 rtsx_trace(chip);
3232 return TRANSPORT_FAILED;
3233 }
3234 break;
3235
3236 case KF_RSP_CHG:
3237 if ((scsi_bufflen(srb) == 0x24) &&
3238 (srb->cmnd[8] == 0x00) &&
3239 (srb->cmnd[9] == 0x24)) {
3240 retval = mg_get_rsp_chg(srb, chip);
3241 if (retval != STATUS_SUCCESS) {
3242 rtsx_trace(chip);
3243 return TRANSPORT_FAILED;
3244 }
3245
3246 } else {
3247 set_sense_type(chip, lun,
3248 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3249 rtsx_trace(chip);
3250 return TRANSPORT_FAILED;
3251 }
3252 break;
3253
3254 case KF_GET_ICV:
3255 ms_card->mg_entry_num = srb->cmnd[5];
3256 if ((scsi_bufflen(srb) == 0x404) &&
3257 (srb->cmnd[8] == 0x04) &&
3258 (srb->cmnd[9] == 0x04) &&
3259 (srb->cmnd[2] == 0x00) &&
3260 (srb->cmnd[3] == 0x00) &&
3261 (srb->cmnd[4] == 0x00) &&
3262 (srb->cmnd[5] < 32)) {
3263 retval = mg_get_ICV(srb, chip);
3264 if (retval != STATUS_SUCCESS) {
3265 rtsx_trace(chip);
3266 return TRANSPORT_FAILED;
3267 }
3268
3269 } else {
3270 set_sense_type(chip, lun,
3271 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3272 rtsx_trace(chip);
3273 return TRANSPORT_FAILED;
3274 }
3275 break;
3276
3277 default:
3278 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3279 rtsx_trace(chip);
3280 return TRANSPORT_FAILED;
3281 }
3282
3283 scsi_set_resid(srb, 0);
3284 return TRANSPORT_GOOD;
3285}
3286
3287static int mg_send_key(struct scsi_cmnd *srb, struct rtsx_chip *chip)
3288{
3289 struct ms_info *ms_card = &(chip->ms_card);
3290 unsigned int lun = SCSI_LUN(srb);
3291 int retval;
3292 u8 key_format;
3293
3294 rtsx_disable_aspm(chip);
3295
3296 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
3297 rtsx_exit_ss(chip);
3298 wait_timeout(100);
3299 }
3300 rtsx_set_stat(chip, RTSX_STAT_RUN);
3301
3302 ms_cleanup_work(chip);
3303
3304 if (!check_card_ready(chip, lun)) {
3305 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
3306 rtsx_trace(chip);
3307 return TRANSPORT_FAILED;
3308 }
3309 if (check_card_wp(chip, lun)) {
3310 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_WRITE_PROTECT);
3311 rtsx_trace(chip);
3312 return TRANSPORT_FAILED;
3313 }
3314 if (get_lun_card(chip, lun) != MS_CARD) {
3315 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT);
3316 rtsx_trace(chip);
3317 return TRANSPORT_FAILED;
3318 }
3319
3320 if (srb->cmnd[7] != KC_MG_R_PRO) {
3321 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3322 rtsx_trace(chip);
3323 return TRANSPORT_FAILED;
3324 }
3325
3326 if (!CHK_MSPRO(ms_card)) {
3327 set_sense_type(chip, lun, SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM);
3328 rtsx_trace(chip);
3329 return TRANSPORT_FAILED;
3330 }
3331
3332 key_format = srb->cmnd[10] & 0x3F;
3333 dev_dbg(rtsx_dev(chip), "key_format = 0x%x\n", key_format);
3334
3335 switch (key_format) {
3336 case KF_SET_LEAF_ID:
3337 if ((scsi_bufflen(srb) == 0x0C) &&
3338 (srb->cmnd[8] == 0x00) &&
3339 (srb->cmnd[9] == 0x0C)) {
3340 retval = mg_set_leaf_id(srb, chip);
3341 if (retval != STATUS_SUCCESS) {
3342 rtsx_trace(chip);
3343 return TRANSPORT_FAILED;
3344 }
3345
3346 } else {
3347 set_sense_type(chip, lun,
3348 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3349 rtsx_trace(chip);
3350 return TRANSPORT_FAILED;
3351 }
3352 break;
3353
3354 case KF_CHG_HOST:
3355 if ((scsi_bufflen(srb) == 0x0C) &&
3356 (srb->cmnd[8] == 0x00) &&
3357 (srb->cmnd[9] == 0x0C)) {
3358 retval = mg_chg(srb, chip);
3359 if (retval != STATUS_SUCCESS) {
3360 rtsx_trace(chip);
3361 return TRANSPORT_FAILED;
3362 }
3363
3364 } else {
3365 set_sense_type(chip, lun,
3366 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3367 rtsx_trace(chip);
3368 return TRANSPORT_FAILED;
3369 }
3370 break;
3371
3372 case KF_RSP_HOST:
3373 if ((scsi_bufflen(srb) == 0x0C) &&
3374 (srb->cmnd[8] == 0x00) &&
3375 (srb->cmnd[9] == 0x0C)) {
3376 retval = mg_rsp(srb, chip);
3377 if (retval != STATUS_SUCCESS) {
3378 rtsx_trace(chip);
3379 return TRANSPORT_FAILED;
3380 }
3381
3382 } else {
3383 set_sense_type(chip, lun,
3384 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3385 rtsx_trace(chip);
3386 return TRANSPORT_FAILED;
3387 }
3388 break;
3389
3390 case KF_SET_ICV:
3391 ms_card->mg_entry_num = srb->cmnd[5];
3392 if ((scsi_bufflen(srb) == 0x404) &&
3393 (srb->cmnd[8] == 0x04) &&
3394 (srb->cmnd[9] == 0x04) &&
3395 (srb->cmnd[2] == 0x00) &&
3396 (srb->cmnd[3] == 0x00) &&
3397 (srb->cmnd[4] == 0x00) &&
3398 (srb->cmnd[5] < 32)) {
3399 retval = mg_set_ICV(srb, chip);
3400 if (retval != STATUS_SUCCESS) {
3401 rtsx_trace(chip);
3402 return TRANSPORT_FAILED;
3403 }
3404
3405 } else {
3406 set_sense_type(chip, lun,
3407 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3408 rtsx_trace(chip);
3409 return TRANSPORT_FAILED;
3410 }
3411 break;
3412
3413 default:
3414 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3415 rtsx_trace(chip);
3416 return TRANSPORT_FAILED;
3417 }
3418
3419 scsi_set_resid(srb, 0);
3420 return TRANSPORT_GOOD;
3421}
3422#endif
3423
3424int rtsx_scsi_handler(struct scsi_cmnd *srb, struct rtsx_chip *chip)
3425{
3426#ifdef SUPPORT_SD_LOCK
3427 struct sd_info *sd_card = &(chip->sd_card);
3428#endif
3429 struct ms_info *ms_card = &(chip->ms_card);
3430 unsigned int lun = SCSI_LUN(srb);
3431 int result;
3432
3433#ifdef SUPPORT_SD_LOCK
3434 if (sd_card->sd_erase_status) {
3435
3436
3437
3438 if (!((srb->cmnd[0] == VENDOR_CMND) &&
3439 (srb->cmnd[1] == SCSI_APP_CMD) &&
3440 (srb->cmnd[2] == GET_DEV_STATUS)) &&
3441 (srb->cmnd[0] != REQUEST_SENSE)) {
3442
3443 set_sense_data(chip, lun, CUR_ERR,
3444 0x02, 0, 0x04, 0x04, 0, 0);
3445 rtsx_trace(chip);
3446 return TRANSPORT_FAILED;
3447 }
3448 }
3449#endif
3450
3451 if ((get_lun_card(chip, lun) == MS_CARD) &&
3452 (ms_card->format_status == FORMAT_IN_PROGRESS)) {
3453 if ((srb->cmnd[0] != REQUEST_SENSE) &&
3454 (srb->cmnd[0] != INQUIRY)) {
3455
3456 set_sense_data(chip, lun, CUR_ERR, 0x02, 0, 0x04, 0x04,
3457 0, (u16)(ms_card->progress));
3458 rtsx_trace(chip);
3459 return TRANSPORT_FAILED;
3460 }
3461 }
3462
3463 switch (srb->cmnd[0]) {
3464 case READ_10:
3465 case WRITE_10:
3466 case READ_6:
3467 case WRITE_6:
3468 result = read_write(srb, chip);
3469#if !defined(LED_AUTO_BLINK) && !defined(REGULAR_BLINK)
3470 led_shine(srb, chip);
3471#endif
3472 break;
3473
3474 case TEST_UNIT_READY:
3475 result = test_unit_ready(srb, chip);
3476 break;
3477
3478 case INQUIRY:
3479 result = inquiry(srb, chip);
3480 break;
3481
3482 case READ_CAPACITY:
3483 result = read_capacity(srb, chip);
3484 break;
3485
3486 case START_STOP:
3487 result = start_stop_unit(srb, chip);
3488 break;
3489
3490 case ALLOW_MEDIUM_REMOVAL:
3491 result = allow_medium_removal(srb, chip);
3492 break;
3493
3494 case REQUEST_SENSE:
3495 result = request_sense(srb, chip);
3496 break;
3497
3498 case MODE_SENSE:
3499 case MODE_SENSE_10:
3500 result = mode_sense(srb, chip);
3501 break;
3502
3503 case 0x23:
3504 result = read_format_capacity(srb, chip);
3505 break;
3506
3507 case VENDOR_CMND:
3508 result = vendor_cmnd(srb, chip);
3509 break;
3510
3511 case MS_SP_CMND:
3512 result = ms_sp_cmnd(srb, chip);
3513 break;
3514
3515#ifdef SUPPORT_CPRM
3516 case SD_PASS_THRU_MODE:
3517 case SD_EXECUTE_NO_DATA:
3518 case SD_EXECUTE_READ:
3519 case SD_EXECUTE_WRITE:
3520 case SD_GET_RSP:
3521 case SD_HW_RST:
3522 result = sd_extention_cmnd(srb, chip);
3523 break;
3524#endif
3525
3526#ifdef SUPPORT_MAGIC_GATE
3527 case CMD_MSPRO_MG_RKEY:
3528 result = mg_report_key(srb, chip);
3529 break;
3530
3531 case CMD_MSPRO_MG_SKEY:
3532 result = mg_send_key(srb, chip);
3533 break;
3534#endif
3535
3536 case FORMAT_UNIT:
3537 case MODE_SELECT:
3538 case VERIFY:
3539 result = TRANSPORT_GOOD;
3540 break;
3541
3542 default:
3543 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3544 result = TRANSPORT_FAILED;
3545 }
3546
3547 return result;
3548}
3549