1 /* Driver for Realtek PCI-Express card reader
3 * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2, or (at your option) any
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, see <http://www.gnu.org/licenses/>.
19 * Wei WANG (wei_wang@realsil.com.cn)
20 * Micky Ching (micky_ching@realsil.com.cn)
23 #include <linux/blkdev.h>
24 #include <linux/kthread.h>
25 #include <linux/sched.h>
26 #include <linux/vmalloc.h>
29 #include "rtsx_transport.h"
31 #include "rtsx_card.h"
32 #include "rtsx_chip.h"
33 #include "rtsx_scsi.h"
38 void scsi_show_command(struct rtsx_chip *chip)
40 struct scsi_cmnd *srb = chip->srb;
42 int unknown_cmd = 0, len;
44 switch (srb->cmnd[0]) {
46 what = "TEST_UNIT_READY";
52 what = "REQUEST_SENSE";
57 case READ_BLOCK_LIMITS:
58 what = "READ_BLOCK_LIMITS";
61 what = "REASSIGN_BLOCKS";
73 what = "READ_REVERSE";
76 what = "WRITE_FILEMARKS";
84 case RECOVER_BUFFERED_DATA:
85 what = "RECOVER_BUFFERED_DATA";
108 case RECEIVE_DIAGNOSTIC:
109 what = "RECEIVE_DIAGNOSTIC";
111 case SEND_DIAGNOSTIC:
112 what = "SEND_DIAGNOSTIC";
114 case ALLOW_MEDIUM_REMOVAL:
115 what = "ALLOW_MEDIUM_REMOVAL";
121 what = "READ_CAPACITY";
133 what = "WRITE_VERIFY";
139 what = "SEARCH_HIGH";
142 what = "SEARCH_EQUAL";
151 what = "READ_POSITION";
153 case SYNCHRONIZE_CACHE:
154 what = "SYNCHRONIZE_CACHE";
156 case LOCK_UNLOCK_CACHE:
157 what = "LOCK_UNLOCK_CACHE";
159 case READ_DEFECT_DATA:
160 what = "READ_DEFECT_DATA";
163 what = "MEDIUM_SCAN";
169 what = "COPY_VERIFY";
172 what = "WRITE_BUFFER";
175 what = "READ_BUFFER";
178 what = "UPDATE_BLOCK";
186 case CHANGE_DEFINITION:
187 what = "CHANGE_DEFINITION";
192 case GPCMD_READ_SUBCHANNEL:
193 what = "READ SUBCHANNEL";
198 case GPCMD_READ_HEADER:
199 what = "READ HEADER";
201 case GPCMD_PLAY_AUDIO_10:
202 what = "PLAY AUDIO (10)";
204 case GPCMD_PLAY_AUDIO_MSF:
205 what = "PLAY AUDIO MSF";
207 case GPCMD_GET_EVENT_STATUS_NOTIFICATION:
208 what = "GET EVENT/STATUS NOTIFICATION";
210 case GPCMD_PAUSE_RESUME:
211 what = "PAUSE/RESUME";
219 case GPCMD_STOP_PLAY_SCAN:
220 what = "STOP PLAY/SCAN";
222 case GPCMD_READ_DISC_INFO:
223 what = "READ DISC INFORMATION";
225 case GPCMD_READ_TRACK_RZONE_INFO:
226 what = "READ TRACK INFORMATION";
228 case GPCMD_RESERVE_RZONE_TRACK:
229 what = "RESERVE TRACK";
235 what = "MODE_SELECT_10";
237 case GPCMD_REPAIR_RZONE_TRACK:
238 what = "REPAIR TRACK";
241 what = "READ MASTER CUE";
244 what = "MODE_SENSE_10";
246 case GPCMD_CLOSE_TRACK:
247 what = "CLOSE TRACK/SESSION";
250 what = "READ BUFFER CAPACITY";
253 what = "SEND CUE SHEET";
259 what = "REPORT LUNS";
262 what = "MOVE_MEDIUM or PLAY AUDIO (12)";
270 case WRITE_VERIFY_12:
271 what = "WRITE_VERIFY_12";
274 what = "SEARCH_HIGH_12";
276 case SEARCH_EQUAL_12:
277 what = "SEARCH_EQUAL_12";
280 what = "SEARCH_LOW_12";
282 case SEND_VOLUME_TAG:
283 what = "SEND_VOLUME_TAG";
285 case READ_ELEMENT_STATUS:
286 what = "READ_ELEMENT_STATUS";
288 case GPCMD_READ_CD_MSF:
289 what = "READ CD MSF";
294 case GPCMD_SET_SPEED:
295 what = "SET CD SPEED";
297 case GPCMD_MECHANISM_STATUS:
298 what = "MECHANISM STATUS";
304 what = "WRITE CONTINUE";
307 what = "WRITE_LONG_2";
310 what = "Realtek's vendor command";
313 what = "(unknown command)"; unknown_cmd = 1;
317 if (srb->cmnd[0] != TEST_UNIT_READY)
318 dev_dbg(rtsx_dev(chip), "Command %s (%d bytes)\n",
322 len = min_t(unsigned short, srb->cmd_len, 16);
323 dev_dbg(rtsx_dev(chip), "%*ph\n", len, srb->cmnd);
327 void set_sense_type(struct rtsx_chip *chip, unsigned int lun, int sense_type)
329 switch (sense_type) {
330 case SENSE_TYPE_MEDIA_CHANGE:
331 set_sense_data(chip, lun, CUR_ERR, 0x06, 0, 0x28, 0, 0, 0);
334 case SENSE_TYPE_MEDIA_NOT_PRESENT:
335 set_sense_data(chip, lun, CUR_ERR, 0x02, 0, 0x3A, 0, 0, 0);
338 case SENSE_TYPE_MEDIA_LBA_OVER_RANGE:
339 set_sense_data(chip, lun, CUR_ERR, 0x05, 0, 0x21, 0, 0, 0);
342 case SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT:
343 set_sense_data(chip, lun, CUR_ERR, 0x05, 0, 0x25, 0, 0, 0);
346 case SENSE_TYPE_MEDIA_WRITE_PROTECT:
347 set_sense_data(chip, lun, CUR_ERR, 0x07, 0, 0x27, 0, 0, 0);
350 case SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR:
351 set_sense_data(chip, lun, CUR_ERR, 0x03, 0, 0x11, 0, 0, 0);
354 case SENSE_TYPE_MEDIA_WRITE_ERR:
355 set_sense_data(chip, lun, CUR_ERR, 0x03, 0, 0x0C, 0x02, 0, 0);
358 case SENSE_TYPE_MEDIA_INVALID_CMD_FIELD:
359 set_sense_data(chip, lun, CUR_ERR, ILGAL_REQ, 0,
360 ASC_INVLD_CDB, ASCQ_INVLD_CDB, CDB_ILLEGAL, 1);
363 case SENSE_TYPE_FORMAT_IN_PROGRESS:
364 set_sense_data(chip, lun, CUR_ERR, 0x02, 0, 0x04, 0x04, 0, 0);
367 case SENSE_TYPE_FORMAT_CMD_FAILED:
368 set_sense_data(chip, lun, CUR_ERR, 0x03, 0, 0x31, 0x01, 0, 0);
371 #ifdef SUPPORT_MAGIC_GATE
372 case SENSE_TYPE_MG_KEY_FAIL_NOT_ESTAB:
373 set_sense_data(chip, lun, CUR_ERR, 0x05, 0, 0x6F, 0x02, 0, 0);
376 case SENSE_TYPE_MG_KEY_FAIL_NOT_AUTHEN:
377 set_sense_data(chip, lun, CUR_ERR, 0x05, 0, 0x6F, 0x00, 0, 0);
380 case SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM:
381 set_sense_data(chip, lun, CUR_ERR, 0x02, 0, 0x30, 0x00, 0, 0);
384 case SENSE_TYPE_MG_WRITE_ERR:
385 set_sense_data(chip, lun, CUR_ERR, 0x03, 0, 0x0C, 0x00, 0, 0);
389 #ifdef SUPPORT_SD_LOCK
390 case SENSE_TYPE_MEDIA_READ_FORBIDDEN:
391 set_sense_data(chip, lun, CUR_ERR, 0x07, 0, 0x11, 0x13, 0, 0);
395 case SENSE_TYPE_NO_SENSE:
397 set_sense_data(chip, lun, CUR_ERR, 0, 0, 0, 0, 0, 0);
402 void set_sense_data(struct rtsx_chip *chip, unsigned int lun, u8 err_code,
403 u8 sense_key, u32 info, u8 asc, u8 ascq, u8 sns_key_info0,
406 struct sense_data_t *sense = &(chip->sense_buffer[lun]);
408 sense->err_code = err_code;
409 sense->sense_key = sense_key;
410 sense->info[0] = (u8)(info >> 24);
411 sense->info[1] = (u8)(info >> 16);
412 sense->info[2] = (u8)(info >> 8);
413 sense->info[3] = (u8)info;
415 sense->ad_sense_len = sizeof(struct sense_data_t) - 8;
418 if (sns_key_info0 != 0) {
419 sense->sns_key_info[0] = SKSV | sns_key_info0;
420 sense->sns_key_info[1] = (sns_key_info1 & 0xf0) >> 8;
421 sense->sns_key_info[2] = sns_key_info1 & 0x0f;
425 static int test_unit_ready(struct scsi_cmnd *srb, struct rtsx_chip *chip)
427 unsigned int lun = SCSI_LUN(srb);
429 if (!check_card_ready(chip, lun)) {
430 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
431 return TRANSPORT_FAILED;
434 if (!(CHK_BIT(chip->lun_mc, lun))) {
435 SET_BIT(chip->lun_mc, lun);
436 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_CHANGE);
437 return TRANSPORT_FAILED;
440 #ifdef SUPPORT_SD_LOCK
441 if (get_lun_card(chip, SCSI_LUN(srb)) == SD_CARD) {
442 struct sd_info *sd_card = &(chip->sd_card);
444 if (sd_card->sd_lock_notify) {
445 sd_card->sd_lock_notify = 0;
446 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_CHANGE);
447 return TRANSPORT_FAILED;
448 } else if (sd_card->sd_lock_status & SD_LOCKED) {
449 set_sense_type(chip, lun,
450 SENSE_TYPE_MEDIA_READ_FORBIDDEN);
451 return TRANSPORT_FAILED;
456 return TRANSPORT_GOOD;
459 static unsigned char formatter_inquiry_str[20] = {
460 'M', 'E', 'M', 'O', 'R', 'Y', 'S', 'T', 'I', 'C', 'K',
461 #ifdef SUPPORT_MAGIC_GATE
462 '-', 'M', 'G', /* Byte[47:49] */
464 0x20, 0x20, 0x20, /* Byte[47:49] */
467 #ifdef SUPPORT_MAGIC_GATE
468 0x0B, /* Byte[50]: MG, MS, MSPro, MSXC */
470 0x09, /* Byte[50]: MS, MSPro, MSXC */
472 0x00, /* Byte[51]: Category Specific Commands */
473 0x00, /* Byte[52]: Access Control and feature */
474 0x20, 0x20, 0x20, /* Byte[53:55] */
477 static int inquiry(struct scsi_cmnd *srb, struct rtsx_chip *chip)
479 unsigned int lun = SCSI_LUN(srb);
480 char *inquiry_default = (char *)"Generic-xD/SD/M.S. 1.00 ";
481 char *inquiry_sdms = (char *)"Generic-SD/MemoryStick 1.00 ";
482 char *inquiry_sd = (char *)"Generic-SD/MMC 1.00 ";
483 char *inquiry_ms = (char *)"Generic-MemoryStick 1.00 ";
484 char *inquiry_string;
485 unsigned char sendbytes;
487 u8 card = get_lun_card(chip, lun);
488 int pro_formatter_flag = 0;
489 unsigned char inquiry_buf[] = {
490 QULIFIRE|DRCT_ACCESS_DEV,
497 REL_ADR|WBUS_32|WBUS_16|SYNC|LINKED|CMD_QUE|SFT_RE,
500 if (CHECK_LUN_MODE(chip, SD_MS_2LUN)) {
501 if (chip->lun2card[lun] == SD_CARD)
502 inquiry_string = inquiry_sd;
504 inquiry_string = inquiry_ms;
506 } else if (CHECK_LUN_MODE(chip, SD_MS_1LUN)) {
507 inquiry_string = inquiry_sdms;
509 inquiry_string = inquiry_default;
512 buf = vmalloc(scsi_bufflen(srb));
514 TRACE_RET(chip, TRANSPORT_ERROR);
516 #ifdef SUPPORT_MAGIC_GATE
517 if ((chip->mspro_formatter_enable) &&
518 (chip->lun2card[lun] & MS_CARD))
520 if (chip->mspro_formatter_enable)
523 if (!card || (card == MS_CARD))
524 pro_formatter_flag = 1;
527 if (pro_formatter_flag) {
528 if (scsi_bufflen(srb) < 56)
529 sendbytes = (unsigned char)(scsi_bufflen(srb));
534 if (scsi_bufflen(srb) < 36)
535 sendbytes = (unsigned char)(scsi_bufflen(srb));
541 memcpy(buf, inquiry_buf, 8);
542 memcpy(buf + 8, inquiry_string, sendbytes - 8);
543 if (pro_formatter_flag) {
544 /* Additional Length */
548 memcpy(buf, inquiry_buf, sendbytes);
551 if (pro_formatter_flag) {
553 memcpy(buf + 36, formatter_inquiry_str, sendbytes - 36);
556 scsi_set_resid(srb, 0);
558 rtsx_stor_set_xfer_buf(buf, scsi_bufflen(srb), srb);
561 return TRANSPORT_GOOD;
565 static int start_stop_unit(struct scsi_cmnd *srb, struct rtsx_chip *chip)
567 unsigned int lun = SCSI_LUN(srb);
569 scsi_set_resid(srb, scsi_bufflen(srb));
571 if (srb->cmnd[1] == 1)
572 return TRANSPORT_GOOD;
574 switch (srb->cmnd[0x4]) {
577 return TRANSPORT_GOOD;
580 /* Media shall be unload */
581 if (check_card_ready(chip, lun))
582 eject_card(chip, lun);
583 return TRANSPORT_GOOD;
585 case MAKE_MEDIUM_READY:
587 if (check_card_ready(chip, lun))
588 return TRANSPORT_GOOD;
589 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
590 TRACE_RET(chip, TRANSPORT_FAILED);
595 TRACE_RET(chip, TRANSPORT_ERROR);
599 static int allow_medium_removal(struct scsi_cmnd *srb, struct rtsx_chip *chip)
603 prevent = srb->cmnd[4] & 0x1;
605 scsi_set_resid(srb, 0);
608 set_sense_type(chip, SCSI_LUN(srb),
609 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
610 TRACE_RET(chip, TRANSPORT_FAILED);
613 return TRANSPORT_GOOD;
617 static int request_sense(struct scsi_cmnd *srb, struct rtsx_chip *chip)
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;
624 sense = &(chip->sense_buffer[lun]);
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 /* Logical Unit Not Ready Format in Progress */
634 set_sense_data(chip, lun, CUR_ERR, 0x02, 0, 0x04, 0x04,
635 0, (u16)(ms_card->progress));
637 /* Format Command Failed */
638 set_sense_type(chip, lun, SENSE_TYPE_FORMAT_CMD_FAILED);
639 ms_card->pro_under_formatting = 0;
640 ms_card->progress = 0;
643 rtsx_set_stat(chip, RTSX_STAT_RUN);
646 buf = vmalloc(scsi_bufflen(srb));
648 TRACE_RET(chip, TRANSPORT_ERROR);
650 tmp = (unsigned char *)sense;
651 memcpy(buf, tmp, scsi_bufflen(srb));
653 rtsx_stor_set_xfer_buf(buf, scsi_bufflen(srb), srb);
656 scsi_set_resid(srb, 0);
657 /* Reset Sense Data */
658 set_sense_type(chip, lun, SENSE_TYPE_NO_SENSE);
659 return TRANSPORT_GOOD;
662 static void ms_mode_sense(struct rtsx_chip *chip, u8 cmd,
663 int lun, u8 *buf, int buf_len)
665 struct ms_info *ms_card = &(chip->ms_card);
667 int data_size = buf_len;
668 int support_format = 0;
671 if (cmd == MODE_SENSE) {
673 if (data_size > 0x68)
676 buf[i++] = 0x67; /* Mode Data Length */
678 sys_info_offset = 12;
679 if (data_size > 0x6C)
682 buf[i++] = 0x00; /* Mode Data Length (MSB) */
683 buf[i++] = 0x6A; /* Mode Data Length (LSB) */
686 /* Medium Type Code */
687 if (check_card_ready(chip, lun)) {
688 if (CHK_MSXC(ms_card)) {
691 } else if (CHK_MSPRO(ms_card)) {
699 if (check_card_wp(chip, lun))
705 buf[i++] = 0x00; /* MediaType */
706 buf[i++] = 0x00; /* WP */
709 buf[i++] = 0x00; /* Reserved */
711 if (cmd == MODE_SENSE_10) {
712 buf[i++] = 0x00; /* Reserved */
713 buf[i++] = 0x00; /* Block descriptor length(MSB) */
714 buf[i++] = 0x00; /* Block descriptor length(LSB) */
716 /* The Following Data is the content of "Page 0x20" */
718 buf[i++] = 0x20; /* Page Code */
720 buf[i++] = 0x62; /* Page Length */
722 buf[i++] = 0x00; /* No Access Control */
723 if (data_size >= 12) {
725 buf[i++] = 0xC0; /* SF, SGM */
730 /* The Following Data is the content of "Page 0x20" */
732 buf[i++] = 0x20; /* Page Code */
734 buf[i++] = 0x62; /* Page Length */
736 buf[i++] = 0x00; /* No Access Control */
737 if (data_size >= 8) {
739 buf[i++] = 0xC0; /* SF, SGM */
745 if (data_size > sys_info_offset) {
746 /* 96 Bytes Attribute Data */
747 int len = data_size - sys_info_offset;
749 len = (len < 96) ? len : 96;
751 memcpy(buf + sys_info_offset, ms_card->raw_sys_info, len);
755 static int mode_sense(struct scsi_cmnd *srb, struct rtsx_chip *chip)
757 unsigned int lun = SCSI_LUN(srb);
758 unsigned int dataSize;
760 int pro_formatter_flag;
761 unsigned char pageCode, *buf;
762 u8 card = get_lun_card(chip, lun);
764 #ifndef SUPPORT_MAGIC_GATE
765 if (!check_card_ready(chip, lun)) {
766 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
767 scsi_set_resid(srb, scsi_bufflen(srb));
768 TRACE_RET(chip, TRANSPORT_FAILED);
772 pro_formatter_flag = 0;
774 #ifdef SUPPORT_MAGIC_GATE
775 if ((chip->lun2card[lun] & MS_CARD)) {
776 if (!card || (card == MS_CARD)) {
778 if (chip->mspro_formatter_enable)
779 pro_formatter_flag = 1;
783 if (card == MS_CARD) {
784 if (chip->mspro_formatter_enable) {
785 pro_formatter_flag = 1;
791 buf = kmalloc(dataSize, GFP_KERNEL);
793 TRACE_RET(chip, TRANSPORT_ERROR);
795 pageCode = srb->cmnd[2] & 0x3f;
797 if ((pageCode == 0x3F) || (pageCode == 0x1C) ||
798 (pageCode == 0x00) ||
799 (pro_formatter_flag && (pageCode == 0x20))) {
800 if (srb->cmnd[0] == MODE_SENSE) {
801 if ((pageCode == 0x3F) || (pageCode == 0x20)) {
802 ms_mode_sense(chip, srb->cmnd[0],
808 if (check_card_wp(chip, lun))
816 if ((pageCode == 0x3F) || (pageCode == 0x20)) {
817 ms_mode_sense(chip, srb->cmnd[0],
824 if (check_card_wp(chip, lun))
834 status = TRANSPORT_GOOD;
836 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
837 scsi_set_resid(srb, scsi_bufflen(srb));
838 status = TRANSPORT_FAILED;
841 if (status == TRANSPORT_GOOD) {
842 unsigned int len = min_t(unsigned int, scsi_bufflen(srb),
844 rtsx_stor_set_xfer_buf(buf, len, srb);
845 scsi_set_resid(srb, scsi_bufflen(srb) - len);
852 static int read_write(struct scsi_cmnd *srb, struct rtsx_chip *chip)
854 #ifdef SUPPORT_SD_LOCK
855 struct sd_info *sd_card = &(chip->sd_card);
857 unsigned int lun = SCSI_LUN(srb);
862 rtsx_disable_aspm(chip);
864 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
868 rtsx_set_stat(chip, RTSX_STAT_RUN);
870 if (!check_card_ready(chip, lun) || (get_card_size(chip, lun) == 0)) {
871 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
872 TRACE_RET(chip, TRANSPORT_FAILED);
875 if (!(CHK_BIT(chip->lun_mc, lun))) {
876 SET_BIT(chip->lun_mc, lun);
877 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_CHANGE);
878 return TRANSPORT_FAILED;
881 #ifdef SUPPORT_SD_LOCK
882 if (sd_card->sd_erase_status) {
883 /* Accessing to any card is forbidden
884 * until the erase procedure of SD is completed
886 dev_dbg(rtsx_dev(chip), "SD card being erased!\n");
887 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_READ_FORBIDDEN);
888 TRACE_RET(chip, TRANSPORT_FAILED);
891 if (get_lun_card(chip, lun) == SD_CARD) {
892 if (sd_card->sd_lock_status & SD_LOCKED) {
893 dev_dbg(rtsx_dev(chip), "SD card locked!\n");
894 set_sense_type(chip, lun,
895 SENSE_TYPE_MEDIA_READ_FORBIDDEN);
896 TRACE_RET(chip, TRANSPORT_FAILED);
901 if ((srb->cmnd[0] == READ_10) || (srb->cmnd[0] == WRITE_10)) {
902 start_sec = ((u32)srb->cmnd[2] << 24) |
903 ((u32)srb->cmnd[3] << 16) |
904 ((u32)srb->cmnd[4] << 8) | ((u32)srb->cmnd[5]);
905 sec_cnt = ((u16)(srb->cmnd[7]) << 8) | srb->cmnd[8];
906 } else if ((srb->cmnd[0] == READ_6) || (srb->cmnd[0] == WRITE_6)) {
907 start_sec = ((u32)(srb->cmnd[1] & 0x1F) << 16) |
908 ((u32)srb->cmnd[2] << 8) | ((u32)srb->cmnd[3]);
909 sec_cnt = srb->cmnd[4];
910 } else if ((srb->cmnd[0] == VENDOR_CMND) &&
911 (srb->cmnd[1] == SCSI_APP_CMD) &&
912 ((srb->cmnd[2] == PP_READ10) || (srb->cmnd[2] == PP_WRITE10))) {
913 start_sec = ((u32)srb->cmnd[4] << 24) |
914 ((u32)srb->cmnd[5] << 16) |
915 ((u32)srb->cmnd[6] << 8) | ((u32)srb->cmnd[7]);
916 sec_cnt = ((u16)(srb->cmnd[9]) << 8) | srb->cmnd[10];
918 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
919 TRACE_RET(chip, TRANSPORT_FAILED);
922 /* In some test, we will receive a start_sec like 0xFFFFFFFF.
923 * In this situation, start_sec + sec_cnt will overflow, so we
924 * need to judge start_sec at first
926 if ((start_sec > get_card_size(chip, lun)) ||
927 ((start_sec + sec_cnt) > get_card_size(chip, lun))) {
928 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_LBA_OVER_RANGE);
929 TRACE_RET(chip, TRANSPORT_FAILED);
933 scsi_set_resid(srb, 0);
934 return TRANSPORT_GOOD;
937 if (chip->rw_fail_cnt[lun] == 3) {
938 dev_dbg(rtsx_dev(chip), "read/write fail three times in succession\n");
939 if (srb->sc_data_direction == DMA_FROM_DEVICE)
940 set_sense_type(chip, lun,
941 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
943 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_WRITE_ERR);
945 TRACE_RET(chip, TRANSPORT_FAILED);
948 if (srb->sc_data_direction == DMA_TO_DEVICE) {
949 if (check_card_wp(chip, lun)) {
950 dev_dbg(rtsx_dev(chip), "Write protected card!\n");
951 set_sense_type(chip, lun,
952 SENSE_TYPE_MEDIA_WRITE_PROTECT);
953 TRACE_RET(chip, TRANSPORT_FAILED);
957 retval = card_rw(srb, chip, start_sec, sec_cnt);
958 if (retval != STATUS_SUCCESS) {
959 if (chip->need_release & chip->lun2card[lun]) {
960 chip->rw_fail_cnt[lun] = 0;
961 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
963 chip->rw_fail_cnt[lun]++;
964 if (srb->sc_data_direction == DMA_FROM_DEVICE)
965 set_sense_type(chip, lun,
966 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
968 set_sense_type(chip, lun,
969 SENSE_TYPE_MEDIA_WRITE_ERR);
971 retval = TRANSPORT_FAILED;
972 TRACE_GOTO(chip, Exit);
974 chip->rw_fail_cnt[lun] = 0;
975 retval = TRANSPORT_GOOD;
978 scsi_set_resid(srb, 0);
984 static int read_format_capacity(struct scsi_cmnd *srb, struct rtsx_chip *chip)
987 unsigned int lun = SCSI_LUN(srb);
988 unsigned int buf_len;
989 u8 card = get_lun_card(chip, lun);
994 if (!check_card_ready(chip, lun)) {
995 if (!chip->mspro_formatter_enable) {
996 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
997 TRACE_RET(chip, TRANSPORT_FAILED);
1001 buf_len = (scsi_bufflen(srb) > 12) ? 0x14 : 12;
1003 buf = kmalloc(buf_len, GFP_KERNEL);
1005 TRACE_RET(chip, TRANSPORT_ERROR);
1011 /* Capacity List Length */
1012 if ((buf_len > 12) && chip->mspro_formatter_enable &&
1013 (chip->lun2card[lun] & MS_CARD) &&
1014 (!card || (card == MS_CARD))) {
1023 if (check_card_ready(chip, lun)) {
1024 card_size = get_card_size(chip, lun);
1025 buf[i++] = (unsigned char)(card_size >> 24);
1026 buf[i++] = (unsigned char)(card_size >> 16);
1027 buf[i++] = (unsigned char)(card_size >> 8);
1028 buf[i++] = (unsigned char)card_size;
1053 buf_len = min_t(unsigned int, scsi_bufflen(srb), buf_len);
1054 rtsx_stor_set_xfer_buf(buf, buf_len, srb);
1057 scsi_set_resid(srb, scsi_bufflen(srb) - buf_len);
1059 return TRANSPORT_GOOD;
1062 static int read_capacity(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1065 unsigned int lun = SCSI_LUN(srb);
1068 if (!check_card_ready(chip, lun)) {
1069 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
1070 TRACE_RET(chip, TRANSPORT_FAILED);
1073 if (!(CHK_BIT(chip->lun_mc, lun))) {
1074 SET_BIT(chip->lun_mc, lun);
1075 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_CHANGE);
1076 return TRANSPORT_FAILED;
1079 buf = kmalloc(8, GFP_KERNEL);
1081 TRACE_RET(chip, TRANSPORT_ERROR);
1083 card_size = get_card_size(chip, lun);
1084 buf[0] = (unsigned char)((card_size - 1) >> 24);
1085 buf[1] = (unsigned char)((card_size - 1) >> 16);
1086 buf[2] = (unsigned char)((card_size - 1) >> 8);
1087 buf[3] = (unsigned char)(card_size - 1);
1094 rtsx_stor_set_xfer_buf(buf, scsi_bufflen(srb), srb);
1097 scsi_set_resid(srb, 0);
1099 return TRANSPORT_GOOD;
1102 static int read_eeprom(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1104 unsigned short len, i;
1108 rtsx_disable_aspm(chip);
1110 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
1114 rtsx_set_stat(chip, RTSX_STAT_RUN);
1116 len = ((u16)srb->cmnd[4] << 8) | srb->cmnd[5];
1120 TRACE_RET(chip, TRANSPORT_ERROR);
1122 retval = rtsx_force_power_on(chip, SSC_PDCTL);
1123 if (retval != STATUS_SUCCESS) {
1125 set_sense_type(chip, SCSI_LUN(srb),
1126 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
1127 TRACE_RET(chip, TRANSPORT_FAILED);
1130 for (i = 0; i < len; i++) {
1131 retval = spi_read_eeprom(chip, i, buf + i);
1132 if (retval != STATUS_SUCCESS) {
1134 set_sense_type(chip, SCSI_LUN(srb),
1135 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
1136 TRACE_RET(chip, TRANSPORT_FAILED);
1140 len = (unsigned short)min_t(unsigned int, scsi_bufflen(srb), len);
1141 rtsx_stor_set_xfer_buf(buf, len, srb);
1142 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1146 return TRANSPORT_GOOD;
1149 static int write_eeprom(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1151 unsigned short len, i;
1155 rtsx_disable_aspm(chip);
1157 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
1161 rtsx_set_stat(chip, RTSX_STAT_RUN);
1163 len = ((u16)srb->cmnd[4] << 8) | srb->cmnd[5];
1165 retval = rtsx_force_power_on(chip, SSC_PDCTL);
1166 if (retval != STATUS_SUCCESS) {
1167 set_sense_type(chip, SCSI_LUN(srb), SENSE_TYPE_MEDIA_WRITE_ERR);
1168 TRACE_RET(chip, TRANSPORT_FAILED);
1172 retval = spi_erase_eeprom_chip(chip);
1173 if (retval != STATUS_SUCCESS) {
1174 set_sense_type(chip, SCSI_LUN(srb),
1175 SENSE_TYPE_MEDIA_WRITE_ERR);
1176 TRACE_RET(chip, TRANSPORT_FAILED);
1179 len = (unsigned short)min_t(unsigned int, scsi_bufflen(srb),
1183 TRACE_RET(chip, TRANSPORT_ERROR);
1185 rtsx_stor_get_xfer_buf(buf, len, srb);
1186 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1188 for (i = 0; i < len; i++) {
1189 retval = spi_write_eeprom(chip, i, buf[i]);
1190 if (retval != STATUS_SUCCESS) {
1192 set_sense_type(chip, SCSI_LUN(srb),
1193 SENSE_TYPE_MEDIA_WRITE_ERR);
1194 TRACE_RET(chip, TRANSPORT_FAILED);
1201 return TRANSPORT_GOOD;
1204 static int read_mem(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1206 unsigned short addr, len, i;
1210 rtsx_disable_aspm(chip);
1212 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
1216 rtsx_set_stat(chip, RTSX_STAT_RUN);
1218 addr = ((u16)srb->cmnd[2] << 8) | srb->cmnd[3];
1219 len = ((u16)srb->cmnd[4] << 8) | srb->cmnd[5];
1221 if (addr < 0xFC00) {
1222 set_sense_type(chip, SCSI_LUN(srb),
1223 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1224 TRACE_RET(chip, TRANSPORT_FAILED);
1229 TRACE_RET(chip, TRANSPORT_ERROR);
1231 retval = rtsx_force_power_on(chip, SSC_PDCTL);
1232 if (retval != STATUS_SUCCESS) {
1234 set_sense_type(chip, SCSI_LUN(srb), SENSE_TYPE_MEDIA_WRITE_ERR);
1235 TRACE_RET(chip, TRANSPORT_FAILED);
1238 for (i = 0; i < len; i++) {
1239 retval = rtsx_read_register(chip, addr + i, buf + i);
1240 if (retval != STATUS_SUCCESS) {
1242 set_sense_type(chip, SCSI_LUN(srb),
1243 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
1244 TRACE_RET(chip, TRANSPORT_FAILED);
1248 len = (unsigned short)min_t(unsigned int, scsi_bufflen(srb), len);
1249 rtsx_stor_set_xfer_buf(buf, len, srb);
1250 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1254 return TRANSPORT_GOOD;
1257 static int write_mem(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1259 unsigned short addr, len, i;
1263 rtsx_disable_aspm(chip);
1265 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
1269 rtsx_set_stat(chip, RTSX_STAT_RUN);
1271 addr = ((u16)srb->cmnd[2] << 8) | srb->cmnd[3];
1272 len = ((u16)srb->cmnd[4] << 8) | srb->cmnd[5];
1274 if (addr < 0xFC00) {
1275 set_sense_type(chip, SCSI_LUN(srb),
1276 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1277 TRACE_RET(chip, TRANSPORT_FAILED);
1280 len = (unsigned short)min_t(unsigned int, scsi_bufflen(srb), len);
1283 TRACE_RET(chip, TRANSPORT_ERROR);
1285 rtsx_stor_get_xfer_buf(buf, len, srb);
1286 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1288 retval = rtsx_force_power_on(chip, SSC_PDCTL);
1289 if (retval != STATUS_SUCCESS) {
1291 set_sense_type(chip, SCSI_LUN(srb), SENSE_TYPE_MEDIA_WRITE_ERR);
1292 TRACE_RET(chip, TRANSPORT_FAILED);
1295 for (i = 0; i < len; i++) {
1296 retval = rtsx_write_register(chip, addr + i, 0xFF, buf[i]);
1297 if (retval != STATUS_SUCCESS) {
1299 set_sense_type(chip, SCSI_LUN(srb),
1300 SENSE_TYPE_MEDIA_WRITE_ERR);
1301 TRACE_RET(chip, TRANSPORT_FAILED);
1307 return TRANSPORT_GOOD;
1310 static int get_sd_csd(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1312 struct sd_info *sd_card = &(chip->sd_card);
1313 unsigned int lun = SCSI_LUN(srb);
1315 if (!check_card_ready(chip, lun)) {
1316 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
1317 TRACE_RET(chip, TRANSPORT_FAILED);
1320 if (get_lun_card(chip, lun) != SD_CARD) {
1321 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
1322 TRACE_RET(chip, TRANSPORT_FAILED);
1325 scsi_set_resid(srb, 0);
1326 rtsx_stor_set_xfer_buf(sd_card->raw_csd, scsi_bufflen(srb), srb);
1328 return TRANSPORT_GOOD;
1331 static int toggle_gpio_cmd(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1333 u8 gpio = srb->cmnd[2];
1335 rtsx_disable_aspm(chip);
1337 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
1341 rtsx_set_stat(chip, RTSX_STAT_RUN);
1345 toggle_gpio(chip, gpio);
1347 return TRANSPORT_GOOD;
1351 static int trace_msg_cmd(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1353 unsigned char *ptr, *buf = NULL;
1356 unsigned int buf_len;
1358 buf_len = 4 + ((2 + MSG_FUNC_LEN + MSG_FILE_LEN + TIME_VAL_LEN) *
1361 if ((scsi_bufflen(srb) < buf_len) || (scsi_sglist(srb) == NULL)) {
1362 set_sense_type(chip, SCSI_LUN(srb),
1363 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
1364 TRACE_RET(chip, TRANSPORT_FAILED);
1367 clear = srb->cmnd[2];
1369 buf = vmalloc(scsi_bufflen(srb));
1371 TRACE_RET(chip, TRANSPORT_ERROR);
1374 if (chip->trace_msg[chip->msg_idx].valid)
1375 msg_cnt = TRACE_ITEM_CNT;
1377 msg_cnt = chip->msg_idx;
1379 *(ptr++) = (u8)(msg_cnt >> 24);
1380 *(ptr++) = (u8)(msg_cnt >> 16);
1381 *(ptr++) = (u8)(msg_cnt >> 8);
1382 *(ptr++) = (u8)msg_cnt;
1383 dev_dbg(rtsx_dev(chip), "Trace message count is %d\n", msg_cnt);
1385 for (i = 1; i <= msg_cnt; i++) {
1388 idx = chip->msg_idx - i;
1390 idx += TRACE_ITEM_CNT;
1392 *(ptr++) = (u8)(chip->trace_msg[idx].line >> 8);
1393 *(ptr++) = (u8)(chip->trace_msg[idx].line);
1394 for (j = 0; j < MSG_FUNC_LEN; j++)
1395 *(ptr++) = chip->trace_msg[idx].func[j];
1397 for (j = 0; j < MSG_FILE_LEN; j++)
1398 *(ptr++) = chip->trace_msg[idx].file[j];
1400 for (j = 0; j < TIME_VAL_LEN; j++)
1401 *(ptr++) = chip->trace_msg[idx].timeval_buf[j];
1404 rtsx_stor_set_xfer_buf(buf, scsi_bufflen(srb), srb);
1409 for (i = 0; i < TRACE_ITEM_CNT; i++)
1410 chip->trace_msg[i].valid = 0;
1413 scsi_set_resid(srb, 0);
1414 return TRANSPORT_GOOD;
1418 static int read_host_reg(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1424 rtsx_disable_aspm(chip);
1426 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
1430 rtsx_set_stat(chip, RTSX_STAT_RUN);
1432 addr = srb->cmnd[4];
1434 val = rtsx_readl(chip, addr);
1435 dev_dbg(rtsx_dev(chip), "Host register (0x%x): 0x%x\n", addr, val);
1437 buf[0] = (u8)(val >> 24);
1438 buf[1] = (u8)(val >> 16);
1439 buf[2] = (u8)(val >> 8);
1442 len = min_t(unsigned int, scsi_bufflen(srb), 4);
1443 rtsx_stor_set_xfer_buf(buf, len, srb);
1444 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1446 return TRANSPORT_GOOD;
1449 static int write_host_reg(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1455 rtsx_disable_aspm(chip);
1457 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
1461 rtsx_set_stat(chip, RTSX_STAT_RUN);
1463 addr = srb->cmnd[4];
1465 len = min_t(unsigned int, scsi_bufflen(srb), 4);
1466 rtsx_stor_get_xfer_buf(buf, len, srb);
1467 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1469 val = ((u32)buf[0] << 24) | ((u32)buf[1] << 16) | ((u32)buf[2]
1472 rtsx_writel(chip, addr, val);
1474 return TRANSPORT_GOOD;
1477 static int set_variable(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1479 unsigned lun = SCSI_LUN(srb);
1481 if (srb->cmnd[3] == 1) {
1482 /* Variable Clock */
1483 struct xd_info *xd_card = &(chip->xd_card);
1484 struct sd_info *sd_card = &(chip->sd_card);
1485 struct ms_info *ms_card = &(chip->ms_card);
1487 switch (srb->cmnd[4]) {
1489 xd_card->xd_clock = srb->cmnd[5];
1493 sd_card->sd_clock = srb->cmnd[5];
1497 ms_card->ms_clock = srb->cmnd[5];
1501 set_sense_type(chip, lun,
1502 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1503 TRACE_RET(chip, TRANSPORT_FAILED);
1505 } else if (srb->cmnd[3] == 2) {
1507 chip->blink_led = 1;
1511 chip->blink_led = 0;
1513 rtsx_disable_aspm(chip);
1516 (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
1520 rtsx_set_stat(chip, RTSX_STAT_RUN);
1522 retval = rtsx_force_power_on(chip, SSC_PDCTL);
1523 if (retval != STATUS_SUCCESS) {
1524 set_sense_type(chip, SCSI_LUN(srb),
1525 SENSE_TYPE_MEDIA_WRITE_ERR);
1526 TRACE_RET(chip, TRANSPORT_FAILED);
1529 turn_off_led(chip, LED_GPIO);
1532 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1533 TRACE_RET(chip, TRANSPORT_FAILED);
1536 return TRANSPORT_GOOD;
1539 static int get_variable(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1541 unsigned int lun = SCSI_LUN(srb);
1543 if (srb->cmnd[3] == 1) {
1544 struct xd_info *xd_card = &(chip->xd_card);
1545 struct sd_info *sd_card = &(chip->sd_card);
1546 struct ms_info *ms_card = &(chip->ms_card);
1549 switch (srb->cmnd[4]) {
1551 tmp = (u8)(xd_card->xd_clock);
1555 tmp = (u8)(sd_card->sd_clock);
1559 tmp = (u8)(ms_card->ms_clock);
1563 set_sense_type(chip, lun,
1564 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1565 TRACE_RET(chip, TRANSPORT_FAILED);
1568 rtsx_stor_set_xfer_buf(&tmp, 1, srb);
1569 } else if (srb->cmnd[3] == 2) {
1570 u8 tmp = chip->blink_led;
1572 rtsx_stor_set_xfer_buf(&tmp, 1, srb);
1574 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1575 TRACE_RET(chip, TRANSPORT_FAILED);
1578 return TRANSPORT_GOOD;
1581 static int dma_access_ring_buffer(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1584 unsigned int lun = SCSI_LUN(srb);
1587 rtsx_disable_aspm(chip);
1589 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
1593 rtsx_set_stat(chip, RTSX_STAT_RUN);
1595 len = ((u16)(srb->cmnd[4]) << 8) | srb->cmnd[5];
1596 len = min_t(u16, len, scsi_bufflen(srb));
1598 if (srb->sc_data_direction == DMA_FROM_DEVICE)
1599 dev_dbg(rtsx_dev(chip), "Read from device\n");
1601 dev_dbg(rtsx_dev(chip), "Write to device\n");
1603 retval = rtsx_transfer_data(chip, 0, scsi_sglist(srb), len,
1604 scsi_sg_count(srb), srb->sc_data_direction, 1000);
1606 if (srb->sc_data_direction == DMA_FROM_DEVICE)
1607 set_sense_type(chip, lun,
1608 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
1610 set_sense_type(chip, lun,
1611 SENSE_TYPE_MEDIA_WRITE_ERR);
1613 TRACE_RET(chip, TRANSPORT_FAILED);
1615 scsi_set_resid(srb, 0);
1617 return TRANSPORT_GOOD;
1620 static int get_dev_status(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1622 struct sd_info *sd_card = &(chip->sd_card);
1623 struct ms_info *ms_card = &(chip->ms_card);
1625 unsigned int lun = SCSI_LUN(srb);
1626 u8 card = get_lun_card(chip, lun);
1629 u8 oc_now_mask = 0, oc_ever_mask = 0;
1632 memset(status, 0, 32);
1634 status[0] = (u8)(chip->product_id);
1635 status[1] = chip->ic_version;
1637 if (chip->auto_delink_en)
1654 if (CHECK_LUN_MODE(chip,
1655 SD_MS_2LUN) && (chip->lun2card[lun] == MS_CARD)) {
1656 oc_now_mask = MS_OC_NOW;
1657 oc_ever_mask = MS_OC_EVER;
1659 oc_now_mask = SD_OC_NOW;
1660 oc_ever_mask = SD_OC_EVER;
1663 if (chip->ocp_stat & oc_now_mask)
1666 if (chip->ocp_stat & oc_ever_mask)
1670 if (card == SD_CARD) {
1671 if (CHK_SD(sd_card)) {
1672 if (CHK_SD_HCXC(sd_card)) {
1673 if (sd_card->capacity > 0x4000000)
1674 status[0x0E] = 0x02;
1676 status[0x0E] = 0x01;
1678 status[0x0E] = 0x00;
1681 if (CHK_SD_SDR104(sd_card))
1682 status[0x0F] = 0x03;
1683 else if (CHK_SD_DDR50(sd_card))
1684 status[0x0F] = 0x04;
1685 else if (CHK_SD_SDR50(sd_card))
1686 status[0x0F] = 0x02;
1687 else if (CHK_SD_HS(sd_card))
1688 status[0x0F] = 0x01;
1690 status[0x0F] = 0x00;
1692 if (CHK_MMC_SECTOR_MODE(sd_card))
1693 status[0x0E] = 0x01;
1695 status[0x0E] = 0x00;
1697 if (CHK_MMC_DDR52(sd_card))
1698 status[0x0F] = 0x03;
1699 else if (CHK_MMC_52M(sd_card))
1700 status[0x0F] = 0x02;
1701 else if (CHK_MMC_26M(sd_card))
1702 status[0x0F] = 0x01;
1704 status[0x0F] = 0x00;
1706 } else if (card == MS_CARD) {
1707 if (CHK_MSPRO(ms_card)) {
1708 if (CHK_MSXC(ms_card))
1709 status[0x0E] = 0x01;
1711 status[0x0E] = 0x00;
1713 if (CHK_HG8BIT(ms_card))
1714 status[0x0F] = 0x01;
1716 status[0x0F] = 0x00;
1720 #ifdef SUPPORT_SD_LOCK
1721 if (card == SD_CARD) {
1722 status[0x17] = 0x80;
1723 if (sd_card->sd_erase_status)
1724 status[0x17] |= 0x01;
1725 if (sd_card->sd_lock_status & SD_LOCKED) {
1726 status[0x17] |= 0x02;
1727 status[0x07] |= 0x40;
1729 if (sd_card->sd_lock_status & SD_PWD_EXIST)
1730 status[0x17] |= 0x04;
1732 status[0x17] = 0x00;
1735 dev_dbg(rtsx_dev(chip), "status[0x17] = 0x%x\n", status[0x17]);
1738 status[0x18] = 0x8A;
1739 status[0x1A] = 0x28;
1740 #ifdef SUPPORT_SD_LOCK
1741 status[0x1F] = 0x01;
1744 buf_len = min_t(unsigned int, scsi_bufflen(srb), sizeof(status));
1745 rtsx_stor_set_xfer_buf(status, buf_len, srb);
1746 scsi_set_resid(srb, scsi_bufflen(srb) - buf_len);
1748 return TRANSPORT_GOOD;
1751 static int set_chip_mode(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1757 if (!CHECK_PID(chip, 0x5208)) {
1758 set_sense_type(chip, SCSI_LUN(srb),
1759 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1760 TRACE_RET(chip, TRANSPORT_FAILED);
1763 phy_debug_mode = (int)(srb->cmnd[3]);
1765 if (phy_debug_mode) {
1766 chip->phy_debug_mode = 1;
1767 retval = rtsx_write_register(chip, CDRESUMECTL, 0x77, 0);
1768 if (retval != STATUS_SUCCESS)
1769 TRACE_RET(chip, TRANSPORT_FAILED);
1771 rtsx_disable_bus_int(chip);
1773 retval = rtsx_read_phy_register(chip, 0x1C, ®);
1774 if (retval != STATUS_SUCCESS)
1775 TRACE_RET(chip, TRANSPORT_FAILED);
1778 retval = rtsx_write_phy_register(chip, 0x1C, reg);
1779 if (retval != STATUS_SUCCESS)
1780 TRACE_RET(chip, TRANSPORT_FAILED);
1782 chip->phy_debug_mode = 0;
1783 retval = rtsx_write_register(chip, CDRESUMECTL, 0x77, 0x77);
1784 if (retval != STATUS_SUCCESS)
1785 TRACE_RET(chip, TRANSPORT_FAILED);
1787 rtsx_enable_bus_int(chip);
1789 retval = rtsx_read_phy_register(chip, 0x1C, ®);
1790 if (retval != STATUS_SUCCESS)
1791 TRACE_RET(chip, TRANSPORT_FAILED);
1794 retval = rtsx_write_phy_register(chip, 0x1C, reg);
1795 if (retval != STATUS_SUCCESS)
1796 TRACE_RET(chip, TRANSPORT_FAILED);
1799 return TRANSPORT_GOOD;
1802 static int rw_mem_cmd_buf(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1804 int retval = STATUS_SUCCESS;
1805 unsigned int lun = SCSI_LUN(srb);
1806 u8 cmd_type, mask, value, idx;
1809 rtsx_disable_aspm(chip);
1811 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
1815 rtsx_set_stat(chip, RTSX_STAT_RUN);
1817 switch (srb->cmnd[3]) {
1819 rtsx_init_cmd(chip);
1823 cmd_type = srb->cmnd[4];
1825 set_sense_type(chip, lun,
1826 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1827 TRACE_RET(chip, TRANSPORT_FAILED);
1829 addr = (srb->cmnd[5] << 8) | srb->cmnd[6];
1830 mask = srb->cmnd[7];
1831 value = srb->cmnd[8];
1832 rtsx_add_cmd(chip, cmd_type, addr, mask, value);
1836 retval = rtsx_send_cmd(chip, 0, 1000);
1841 value = *(rtsx_get_cmd_data(chip) + idx);
1842 if (scsi_bufflen(srb) < 1) {
1843 set_sense_type(chip, lun,
1844 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1845 TRACE_RET(chip, TRANSPORT_FAILED);
1847 rtsx_stor_set_xfer_buf(&value, 1, srb);
1848 scsi_set_resid(srb, 0);
1852 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
1853 TRACE_RET(chip, TRANSPORT_FAILED);
1856 if (retval != STATUS_SUCCESS) {
1857 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_WRITE_ERR);
1858 TRACE_RET(chip, TRANSPORT_FAILED);
1861 return TRANSPORT_GOOD;
1864 static int suit_cmd(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1868 switch (srb->cmnd[3]) {
1873 result = rw_mem_cmd_buf(srb, chip);
1876 result = TRANSPORT_ERROR;
1882 static int read_phy_register(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1884 unsigned short addr, len, i;
1889 rtsx_disable_aspm(chip);
1891 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
1895 rtsx_set_stat(chip, RTSX_STAT_RUN);
1897 addr = ((u16)srb->cmnd[4] << 8) | srb->cmnd[5];
1898 len = ((u16)srb->cmnd[6] << 8) | srb->cmnd[7];
1906 TRACE_RET(chip, TRANSPORT_ERROR);
1908 retval = rtsx_force_power_on(chip, SSC_PDCTL);
1909 if (retval != STATUS_SUCCESS) {
1911 set_sense_type(chip, SCSI_LUN(srb),
1912 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
1913 TRACE_RET(chip, TRANSPORT_FAILED);
1916 for (i = 0; i < len / 2; i++) {
1917 retval = rtsx_read_phy_register(chip, addr + i, &val);
1918 if (retval != STATUS_SUCCESS) {
1920 set_sense_type(chip, SCSI_LUN(srb),
1921 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
1922 TRACE_RET(chip, TRANSPORT_FAILED);
1925 buf[2*i] = (u8)(val >> 8);
1926 buf[2*i+1] = (u8)val;
1929 len = (unsigned short)min_t(unsigned int, scsi_bufflen(srb),
1931 rtsx_stor_set_xfer_buf(buf, len, srb);
1932 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1937 return TRANSPORT_GOOD;
1940 static int write_phy_register(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1942 unsigned short addr, len, i;
1947 rtsx_disable_aspm(chip);
1949 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
1953 rtsx_set_stat(chip, RTSX_STAT_RUN);
1955 addr = ((u16)srb->cmnd[4] << 8) | srb->cmnd[5];
1956 len = ((u16)srb->cmnd[6] << 8) | srb->cmnd[7];
1962 len = (unsigned short)min_t(unsigned int, scsi_bufflen(srb),
1967 TRACE_RET(chip, TRANSPORT_ERROR);
1969 rtsx_stor_get_xfer_buf(buf, len, srb);
1970 scsi_set_resid(srb, scsi_bufflen(srb) - len);
1972 retval = rtsx_force_power_on(chip, SSC_PDCTL);
1973 if (retval != STATUS_SUCCESS) {
1975 set_sense_type(chip, SCSI_LUN(srb),
1976 SENSE_TYPE_MEDIA_WRITE_ERR);
1977 TRACE_RET(chip, TRANSPORT_FAILED);
1980 for (i = 0; i < len / 2; i++) {
1981 val = ((u16)buf[2*i] << 8) | buf[2*i+1];
1982 retval = rtsx_write_phy_register(chip, addr + i, val);
1983 if (retval != STATUS_SUCCESS) {
1985 set_sense_type(chip, SCSI_LUN(srb),
1986 SENSE_TYPE_MEDIA_WRITE_ERR);
1987 TRACE_RET(chip, TRANSPORT_FAILED);
1994 return TRANSPORT_GOOD;
1997 static int erase_eeprom2(struct scsi_cmnd *srb, struct rtsx_chip *chip)
1999 unsigned short addr;
2003 rtsx_disable_aspm(chip);
2005 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
2009 rtsx_set_stat(chip, RTSX_STAT_RUN);
2011 retval = rtsx_force_power_on(chip, SSC_PDCTL);
2012 if (retval != STATUS_SUCCESS) {
2013 set_sense_type(chip, SCSI_LUN(srb), SENSE_TYPE_MEDIA_WRITE_ERR);
2014 TRACE_RET(chip, TRANSPORT_FAILED);
2017 mode = srb->cmnd[3];
2018 addr = ((u16)srb->cmnd[4] << 8) | srb->cmnd[5];
2021 retval = spi_erase_eeprom_chip(chip);
2022 if (retval != STATUS_SUCCESS) {
2023 set_sense_type(chip, SCSI_LUN(srb),
2024 SENSE_TYPE_MEDIA_WRITE_ERR);
2025 TRACE_RET(chip, TRANSPORT_FAILED);
2027 } else if (mode == 1) {
2028 retval = spi_erase_eeprom_byte(chip, addr);
2029 if (retval != STATUS_SUCCESS) {
2030 set_sense_type(chip, SCSI_LUN(srb),
2031 SENSE_TYPE_MEDIA_WRITE_ERR);
2032 TRACE_RET(chip, TRANSPORT_FAILED);
2035 set_sense_type(chip, SCSI_LUN(srb),
2036 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
2037 TRACE_RET(chip, TRANSPORT_FAILED);
2040 return TRANSPORT_GOOD;
2043 static int read_eeprom2(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2045 unsigned short addr, len, i;
2049 rtsx_disable_aspm(chip);
2051 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
2055 rtsx_set_stat(chip, RTSX_STAT_RUN);
2057 addr = ((u16)srb->cmnd[4] << 8) | srb->cmnd[5];
2058 len = ((u16)srb->cmnd[6] << 8) | srb->cmnd[7];
2062 TRACE_RET(chip, TRANSPORT_ERROR);
2064 retval = rtsx_force_power_on(chip, SSC_PDCTL);
2065 if (retval != STATUS_SUCCESS) {
2067 set_sense_type(chip, SCSI_LUN(srb),
2068 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
2069 TRACE_RET(chip, TRANSPORT_FAILED);
2072 for (i = 0; i < len; i++) {
2073 retval = spi_read_eeprom(chip, addr + i, buf + i);
2074 if (retval != STATUS_SUCCESS) {
2076 set_sense_type(chip, SCSI_LUN(srb),
2077 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
2078 TRACE_RET(chip, TRANSPORT_FAILED);
2082 len = (unsigned short)min_t(unsigned int, scsi_bufflen(srb), len);
2083 rtsx_stor_set_xfer_buf(buf, len, srb);
2084 scsi_set_resid(srb, scsi_bufflen(srb) - len);
2088 return TRANSPORT_GOOD;
2091 static int write_eeprom2(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2093 unsigned short addr, len, i;
2097 rtsx_disable_aspm(chip);
2099 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
2103 rtsx_set_stat(chip, RTSX_STAT_RUN);
2105 addr = ((u16)srb->cmnd[4] << 8) | srb->cmnd[5];
2106 len = ((u16)srb->cmnd[6] << 8) | srb->cmnd[7];
2108 len = (unsigned short)min_t(unsigned int, scsi_bufflen(srb), len);
2111 TRACE_RET(chip, TRANSPORT_ERROR);
2113 rtsx_stor_get_xfer_buf(buf, len, srb);
2114 scsi_set_resid(srb, scsi_bufflen(srb) - len);
2116 retval = rtsx_force_power_on(chip, SSC_PDCTL);
2117 if (retval != STATUS_SUCCESS) {
2119 set_sense_type(chip, SCSI_LUN(srb), SENSE_TYPE_MEDIA_WRITE_ERR);
2120 TRACE_RET(chip, TRANSPORT_FAILED);
2123 for (i = 0; i < len; i++) {
2124 retval = spi_write_eeprom(chip, addr + i, buf[i]);
2125 if (retval != STATUS_SUCCESS) {
2127 set_sense_type(chip, SCSI_LUN(srb),
2128 SENSE_TYPE_MEDIA_WRITE_ERR);
2129 TRACE_RET(chip, TRANSPORT_FAILED);
2135 return TRANSPORT_GOOD;
2138 static int read_efuse(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2144 rtsx_disable_aspm(chip);
2146 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
2150 rtsx_set_stat(chip, RTSX_STAT_RUN);
2152 addr = srb->cmnd[4];
2157 TRACE_RET(chip, TRANSPORT_ERROR);
2159 retval = rtsx_force_power_on(chip, SSC_PDCTL);
2160 if (retval != STATUS_SUCCESS) {
2162 set_sense_type(chip, SCSI_LUN(srb),
2163 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
2164 TRACE_RET(chip, TRANSPORT_FAILED);
2167 for (i = 0; i < len; i++) {
2168 retval = rtsx_read_efuse(chip, addr + i, buf + i);
2169 if (retval != STATUS_SUCCESS) {
2171 set_sense_type(chip, SCSI_LUN(srb),
2172 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
2173 TRACE_RET(chip, TRANSPORT_FAILED);
2177 len = (u8)min_t(unsigned int, scsi_bufflen(srb), len);
2178 rtsx_stor_set_xfer_buf(buf, len, srb);
2179 scsi_set_resid(srb, scsi_bufflen(srb) - len);
2183 return TRANSPORT_GOOD;
2186 static int write_efuse(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2188 int retval, result = TRANSPORT_GOOD;
2193 rtsx_disable_aspm(chip);
2195 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
2199 rtsx_set_stat(chip, RTSX_STAT_RUN);
2201 addr = srb->cmnd[4];
2204 len = (u8)min_t(unsigned int, scsi_bufflen(srb), len);
2207 TRACE_RET(chip, TRANSPORT_ERROR);
2209 rtsx_stor_get_xfer_buf(buf, len, srb);
2210 scsi_set_resid(srb, scsi_bufflen(srb) - len);
2212 retval = rtsx_force_power_on(chip, SSC_PDCTL);
2213 if (retval != STATUS_SUCCESS) {
2215 TRACE_RET(chip, TRANSPORT_ERROR);
2218 if (chip->asic_code) {
2219 retval = rtsx_read_phy_register(chip, 0x08, &val);
2220 if (retval != STATUS_SUCCESS) {
2222 TRACE_RET(chip, TRANSPORT_ERROR);
2225 retval = rtsx_write_register(chip, PWR_GATE_CTRL,
2226 LDO3318_PWR_MASK, LDO_OFF);
2227 if (retval != STATUS_SUCCESS) {
2229 TRACE_RET(chip, TRANSPORT_ERROR);
2234 retval = rtsx_write_phy_register(chip, 0x08,
2235 0x4C00 | chip->phy_voltage);
2236 if (retval != STATUS_SUCCESS) {
2238 TRACE_RET(chip, TRANSPORT_ERROR);
2241 retval = rtsx_write_register(chip, PWR_GATE_CTRL,
2242 LDO3318_PWR_MASK, LDO_ON);
2243 if (retval != STATUS_SUCCESS) {
2245 TRACE_RET(chip, TRANSPORT_ERROR);
2251 retval = card_power_on(chip, SPI_CARD);
2252 if (retval != STATUS_SUCCESS) {
2254 TRACE_RET(chip, TRANSPORT_ERROR);
2259 for (i = 0; i < len; i++) {
2260 retval = rtsx_write_efuse(chip, addr + i, buf[i]);
2261 if (retval != STATUS_SUCCESS) {
2262 set_sense_type(chip, SCSI_LUN(srb),
2263 SENSE_TYPE_MEDIA_WRITE_ERR);
2264 result = TRANSPORT_FAILED;
2265 TRACE_GOTO(chip, Exit);
2272 retval = card_power_off(chip, SPI_CARD);
2273 if (retval != STATUS_SUCCESS)
2274 TRACE_RET(chip, TRANSPORT_ERROR);
2276 if (chip->asic_code) {
2277 retval = rtsx_write_register(chip, PWR_GATE_CTRL,
2278 LDO3318_PWR_MASK, LDO_OFF);
2279 if (retval != STATUS_SUCCESS)
2280 TRACE_RET(chip, TRANSPORT_ERROR);
2284 retval = rtsx_write_phy_register(chip, 0x08, val);
2285 if (retval != STATUS_SUCCESS)
2286 TRACE_RET(chip, TRANSPORT_ERROR);
2288 retval = rtsx_write_register(chip, PWR_GATE_CTRL,
2289 LDO3318_PWR_MASK, LDO_ON);
2290 if (retval != STATUS_SUCCESS)
2291 TRACE_RET(chip, TRANSPORT_ERROR);
2297 static int read_cfg_byte(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2304 rtsx_disable_aspm(chip);
2306 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
2310 rtsx_set_stat(chip, RTSX_STAT_RUN);
2312 func = srb->cmnd[3];
2313 addr = ((u16)(srb->cmnd[4]) << 8) | srb->cmnd[5];
2314 len = ((u16)(srb->cmnd[6]) << 8) | srb->cmnd[7];
2316 dev_dbg(rtsx_dev(chip), "%s: func = %d, addr = 0x%x, len = %d\n",
2317 __func__, func, addr, len);
2319 if (CHK_SDIO_EXIST(chip) && !CHK_SDIO_IGNORED(chip))
2324 if (func > func_max) {
2325 set_sense_type(chip, SCSI_LUN(srb),
2326 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
2327 TRACE_RET(chip, TRANSPORT_FAILED);
2332 TRACE_RET(chip, TRANSPORT_ERROR);
2334 retval = rtsx_read_cfg_seq(chip, func, addr, buf, len);
2335 if (retval != STATUS_SUCCESS) {
2336 set_sense_type(chip, SCSI_LUN(srb),
2337 SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
2339 TRACE_RET(chip, TRANSPORT_FAILED);
2342 len = (u16)min_t(unsigned int, scsi_bufflen(srb), len);
2343 rtsx_stor_set_xfer_buf(buf, len, srb);
2344 scsi_set_resid(srb, scsi_bufflen(srb) - len);
2348 return TRANSPORT_GOOD;
2351 static int write_cfg_byte(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2358 rtsx_disable_aspm(chip);
2360 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
2364 rtsx_set_stat(chip, RTSX_STAT_RUN);
2366 func = srb->cmnd[3];
2367 addr = ((u16)(srb->cmnd[4]) << 8) | srb->cmnd[5];
2368 len = ((u16)(srb->cmnd[6]) << 8) | srb->cmnd[7];
2370 dev_dbg(rtsx_dev(chip), "%s: func = %d, addr = 0x%x\n",
2371 __func__, func, addr);
2373 if (CHK_SDIO_EXIST(chip) && !CHK_SDIO_IGNORED(chip))
2378 if (func > func_max) {
2379 set_sense_type(chip, SCSI_LUN(srb),
2380 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
2381 TRACE_RET(chip, TRANSPORT_FAILED);
2384 len = (unsigned short)min_t(unsigned int, scsi_bufflen(srb), len);
2387 TRACE_RET(chip, TRANSPORT_ERROR);
2389 rtsx_stor_get_xfer_buf(buf, len, srb);
2390 scsi_set_resid(srb, scsi_bufflen(srb) - len);
2392 retval = rtsx_write_cfg_seq(chip, func, addr, buf, len);
2393 if (retval != STATUS_SUCCESS) {
2394 set_sense_type(chip, SCSI_LUN(srb), SENSE_TYPE_MEDIA_WRITE_ERR);
2396 TRACE_RET(chip, TRANSPORT_FAILED);
2401 return TRANSPORT_GOOD;
2404 static int app_cmd(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2408 switch (srb->cmnd[2]) {
2411 result = read_write(srb, chip);
2415 result = read_host_reg(srb, chip);
2418 case WRITE_HOST_REG:
2419 result = write_host_reg(srb, chip);
2423 result = get_variable(srb, chip);
2427 result = set_variable(srb, chip);
2432 result = dma_access_ring_buffer(srb, chip);
2436 result = read_phy_register(srb, chip);
2440 result = write_phy_register(srb, chip);
2444 result = erase_eeprom2(srb, chip);
2448 result = read_eeprom2(srb, chip);
2452 result = write_eeprom2(srb, chip);
2456 result = read_efuse(srb, chip);
2460 result = write_efuse(srb, chip);
2464 result = read_cfg_byte(srb, chip);
2468 result = write_cfg_byte(srb, chip);
2472 result = set_chip_mode(srb, chip);
2476 result = suit_cmd(srb, chip);
2479 case GET_DEV_STATUS:
2480 result = get_dev_status(srb, chip);
2484 set_sense_type(chip, SCSI_LUN(srb),
2485 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
2486 TRACE_RET(chip, TRANSPORT_FAILED);
2493 static int read_status(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2497 unsigned int lun = SCSI_LUN(srb);
2499 rtsx_status[0] = (u8)(chip->vendor_id >> 8);
2500 rtsx_status[1] = (u8)(chip->vendor_id);
2502 rtsx_status[2] = (u8)(chip->product_id >> 8);
2503 rtsx_status[3] = (u8)(chip->product_id);
2505 rtsx_status[4] = (u8)lun;
2507 if (CHECK_LUN_MODE(chip, SD_MS_2LUN)) {
2508 if (chip->lun2card[lun] == SD_CARD)
2513 if (chip->card_exist) {
2514 if (chip->card_exist & XD_CARD)
2516 else if (chip->card_exist & SD_CARD)
2518 else if (chip->card_exist & MS_CARD)
2527 if (CHECK_LUN_MODE(chip, SD_MS_2LUN))
2532 rtsx_status[7] = (u8)(chip->product_id);
2533 rtsx_status[8] = chip->ic_version;
2535 if (check_card_exist(chip, lun))
2540 if (CHECK_LUN_MODE(chip, SD_MS_2LUN))
2541 rtsx_status[10] = 0;
2543 rtsx_status[10] = 1;
2545 if (CHECK_LUN_MODE(chip, SD_MS_2LUN)) {
2546 if (chip->lun2card[lun] == SD_CARD)
2547 rtsx_status[11] = SD_CARD;
2549 rtsx_status[11] = MS_CARD;
2551 rtsx_status[11] = XD_CARD | SD_CARD | MS_CARD;
2554 if (check_card_ready(chip, lun))
2555 rtsx_status[12] = 1;
2557 rtsx_status[12] = 0;
2559 if (get_lun_card(chip, lun) == XD_CARD) {
2560 rtsx_status[13] = 0x40;
2561 } else if (get_lun_card(chip, lun) == SD_CARD) {
2562 struct sd_info *sd_card = &(chip->sd_card);
2564 rtsx_status[13] = 0x20;
2565 if (CHK_SD(sd_card)) {
2566 if (CHK_SD_HCXC(sd_card))
2567 rtsx_status[13] |= 0x04;
2568 if (CHK_SD_HS(sd_card))
2569 rtsx_status[13] |= 0x02;
2571 rtsx_status[13] |= 0x08;
2572 if (CHK_MMC_52M(sd_card))
2573 rtsx_status[13] |= 0x02;
2574 if (CHK_MMC_SECTOR_MODE(sd_card))
2575 rtsx_status[13] |= 0x04;
2577 } else if (get_lun_card(chip, lun) == MS_CARD) {
2578 struct ms_info *ms_card = &(chip->ms_card);
2580 if (CHK_MSPRO(ms_card)) {
2581 rtsx_status[13] = 0x38;
2582 if (CHK_HG8BIT(ms_card))
2583 rtsx_status[13] |= 0x04;
2585 if (CHK_MSXC(ms_card))
2586 rtsx_status[13] |= 0x01;
2589 rtsx_status[13] = 0x30;
2592 if (CHECK_LUN_MODE(chip, DEFAULT_SINGLE)) {
2594 if (chip->sd_io && chip->sd_int)
2595 rtsx_status[13] = 0x60;
2597 rtsx_status[13] = 0x70;
2599 rtsx_status[13] = 0x70;
2602 if (chip->lun2card[lun] == SD_CARD)
2603 rtsx_status[13] = 0x20;
2605 rtsx_status[13] = 0x30;
2609 rtsx_status[14] = 0x78;
2610 if (CHK_SDIO_EXIST(chip) && !CHK_SDIO_IGNORED(chip))
2611 rtsx_status[15] = 0x83;
2613 rtsx_status[15] = 0x82;
2615 buf_len = min_t(unsigned int, scsi_bufflen(srb), sizeof(rtsx_status));
2616 rtsx_stor_set_xfer_buf(rtsx_status, buf_len, srb);
2617 scsi_set_resid(srb, scsi_bufflen(srb) - buf_len);
2619 return TRANSPORT_GOOD;
2622 static int get_card_bus_width(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2624 unsigned int lun = SCSI_LUN(srb);
2627 if (!check_card_ready(chip, lun)) {
2628 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
2629 TRACE_RET(chip, TRANSPORT_FAILED);
2632 card = get_lun_card(chip, lun);
2633 if ((card == SD_CARD) || (card == MS_CARD)) {
2634 bus_width = chip->card_bus_width[lun];
2636 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_UNRECOVER_READ_ERR);
2637 TRACE_RET(chip, TRANSPORT_FAILED);
2640 scsi_set_resid(srb, 0);
2641 rtsx_stor_set_xfer_buf(&bus_width, scsi_bufflen(srb), srb);
2643 return TRANSPORT_GOOD;
2646 static int spi_vendor_cmd(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2649 unsigned int lun = SCSI_LUN(srb);
2652 if (CHECK_PID(chip, 0x5208) || CHECK_PID(chip, 0x5288)) {
2653 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
2654 TRACE_RET(chip, TRANSPORT_FAILED);
2657 rtsx_disable_aspm(chip);
2659 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
2663 rtsx_set_stat(chip, RTSX_STAT_RUN);
2665 rtsx_force_power_on(chip, SSC_PDCTL);
2667 rtsx_read_register(chip, CARD_GPIO_DIR, &gpio_dir);
2668 rtsx_write_register(chip, CARD_GPIO_DIR, 0x07, gpio_dir & 0x06);
2670 switch (srb->cmnd[2]) {
2671 case SCSI_SPI_GETSTATUS:
2672 result = spi_get_status(srb, chip);
2675 case SCSI_SPI_SETPARAMETER:
2676 result = spi_set_parameter(srb, chip);
2679 case SCSI_SPI_READFALSHID:
2680 result = spi_read_flash_id(srb, chip);
2683 case SCSI_SPI_READFLASH:
2684 result = spi_read_flash(srb, chip);
2687 case SCSI_SPI_WRITEFLASH:
2688 result = spi_write_flash(srb, chip);
2691 case SCSI_SPI_WRITEFLASHSTATUS:
2692 result = spi_write_flash_status(srb, chip);
2695 case SCSI_SPI_ERASEFLASH:
2696 result = spi_erase_flash(srb, chip);
2700 rtsx_write_register(chip, CARD_GPIO_DIR, 0x07, gpio_dir);
2702 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
2703 TRACE_RET(chip, TRANSPORT_FAILED);
2706 rtsx_write_register(chip, CARD_GPIO_DIR, 0x07, gpio_dir);
2708 if (result != STATUS_SUCCESS)
2709 TRACE_RET(chip, TRANSPORT_FAILED);
2711 return TRANSPORT_GOOD;
2714 static int vendor_cmnd(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2718 switch (srb->cmnd[1]) {
2720 result = read_status(srb, chip);
2724 result = read_mem(srb, chip);
2728 result = write_mem(srb, chip);
2732 result = read_eeprom(srb, chip);
2736 result = write_eeprom(srb, chip);
2740 result = toggle_gpio_cmd(srb, chip);
2744 result = get_sd_csd(srb, chip);
2748 result = get_card_bus_width(srb, chip);
2753 result = trace_msg_cmd(srb, chip);
2758 result = app_cmd(srb, chip);
2761 case SPI_VENDOR_COMMAND:
2762 result = spi_vendor_cmd(srb, chip);
2766 set_sense_type(chip, SCSI_LUN(srb),
2767 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
2768 TRACE_RET(chip, TRANSPORT_FAILED);
2774 #if !defined(LED_AUTO_BLINK) && !defined(REGULAR_BLINK)
2775 void led_shine(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2777 unsigned int lun = SCSI_LUN(srb);
2780 if ((srb->cmnd[0] == READ_10) || (srb->cmnd[0] == WRITE_10))
2781 sec_cnt = ((u16)(srb->cmnd[7]) << 8) | srb->cmnd[8];
2782 else if ((srb->cmnd[0] == READ_6) || (srb->cmnd[0] == WRITE_6))
2783 sec_cnt = srb->cmnd[4];
2787 if (chip->rw_cap[lun] >= GPIO_TOGGLE_THRESHOLD) {
2788 toggle_gpio(chip, LED_GPIO);
2789 chip->rw_cap[lun] = 0;
2791 chip->rw_cap[lun] += sec_cnt;
2796 static int ms_format_cmnd(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2798 struct ms_info *ms_card = &(chip->ms_card);
2799 unsigned int lun = SCSI_LUN(srb);
2800 int retval, quick_format;
2802 if (get_lun_card(chip, lun) != MS_CARD) {
2803 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT);
2804 TRACE_RET(chip, TRANSPORT_FAILED);
2807 if ((srb->cmnd[3] != 0x4D) || (srb->cmnd[4] != 0x47) ||
2808 (srb->cmnd[5] != 0x66) || (srb->cmnd[6] != 0x6D) ||
2809 (srb->cmnd[7] != 0x74)) {
2810 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
2811 TRACE_RET(chip, TRANSPORT_FAILED);
2814 rtsx_disable_aspm(chip);
2816 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
2820 if (!check_card_ready(chip, lun) ||
2821 (get_card_size(chip, lun) == 0)) {
2822 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
2823 TRACE_RET(chip, TRANSPORT_FAILED);
2826 rtsx_set_stat(chip, RTSX_STAT_RUN);
2828 if (srb->cmnd[8] & 0x01)
2833 if (!(chip->card_ready & MS_CARD)) {
2834 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
2835 TRACE_RET(chip, TRANSPORT_FAILED);
2838 if (chip->card_wp & MS_CARD) {
2839 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_WRITE_PROTECT);
2840 TRACE_RET(chip, TRANSPORT_FAILED);
2843 if (!CHK_MSPRO(ms_card)) {
2844 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT);
2845 TRACE_RET(chip, TRANSPORT_FAILED);
2848 retval = mspro_format(srb, chip, MS_SHORT_DATA_LEN, quick_format);
2849 if (retval != STATUS_SUCCESS) {
2850 set_sense_type(chip, lun, SENSE_TYPE_FORMAT_CMD_FAILED);
2851 TRACE_RET(chip, TRANSPORT_FAILED);
2854 scsi_set_resid(srb, 0);
2855 return TRANSPORT_GOOD;
2858 #ifdef SUPPORT_PCGL_1P18
2859 static int get_ms_information(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2861 struct ms_info *ms_card = &(chip->ms_card);
2862 unsigned int lun = SCSI_LUN(srb);
2863 u8 dev_info_id, data_len;
2865 unsigned int buf_len;
2868 if (!check_card_ready(chip, lun)) {
2869 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
2870 TRACE_RET(chip, TRANSPORT_FAILED);
2872 if (get_lun_card(chip, lun) != MS_CARD) {
2873 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT);
2874 TRACE_RET(chip, TRANSPORT_FAILED);
2877 if ((srb->cmnd[2] != 0xB0) || (srb->cmnd[4] != 0x4D) ||
2878 (srb->cmnd[5] != 0x53) || (srb->cmnd[6] != 0x49) ||
2879 (srb->cmnd[7] != 0x44)) {
2880 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
2881 TRACE_RET(chip, TRANSPORT_FAILED);
2884 dev_info_id = srb->cmnd[3];
2885 if ((CHK_MSXC(ms_card) && (dev_info_id == 0x10)) ||
2886 (!CHK_MSXC(ms_card) && (dev_info_id == 0x13)) ||
2887 !CHK_MSPRO(ms_card)) {
2888 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
2889 TRACE_RET(chip, TRANSPORT_FAILED);
2892 if (dev_info_id == 0x15)
2893 buf_len = data_len = 0x3A;
2895 buf_len = data_len = 0x6A;
2897 buf = kmalloc(buf_len, GFP_KERNEL);
2899 TRACE_RET(chip, TRANSPORT_ERROR);
2902 /* GET Memory Stick Media Information Response Header */
2903 buf[i++] = 0x00; /* Data length MSB */
2904 buf[i++] = data_len; /* Data length LSB */
2905 /* Device Information Type Code */
2906 if (CHK_MSXC(ms_card))
2917 /* Number of Device Information */
2920 /* Device Information Body */
2922 /* Device Information ID Number */
2923 buf[i++] = dev_info_id;
2924 /* Device Information Length */
2925 if (dev_info_id == 0x15)
2930 buf[i++] = 0x00; /* Data length MSB */
2931 buf[i++] = data_len; /* Data length LSB */
2934 if ((dev_info_id == 0x10) || (dev_info_id == 0x13)) {
2935 /* System Information */
2936 memcpy(buf+i, ms_card->raw_sys_info, 96);
2939 memcpy(buf+i, ms_card->raw_model_name, 48);
2942 rtsx_stor_set_xfer_buf(buf, buf_len, srb);
2944 if (dev_info_id == 0x15)
2945 scsi_set_resid(srb, scsi_bufflen(srb)-0x3C);
2947 scsi_set_resid(srb, scsi_bufflen(srb)-0x6C);
2950 return STATUS_SUCCESS;
2954 static int ms_sp_cmnd(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2956 int retval = TRANSPORT_ERROR;
2958 if (srb->cmnd[2] == MS_FORMAT)
2959 retval = ms_format_cmnd(srb, chip);
2960 #ifdef SUPPORT_PCGL_1P18
2961 else if (srb->cmnd[2] == GET_MS_INFORMATION)
2962 retval = get_ms_information(srb, chip);
2969 static int sd_extention_cmnd(struct scsi_cmnd *srb, struct rtsx_chip *chip)
2971 unsigned int lun = SCSI_LUN(srb);
2974 rtsx_disable_aspm(chip);
2976 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
2980 rtsx_set_stat(chip, RTSX_STAT_RUN);
2982 sd_cleanup_work(chip);
2984 if (!check_card_ready(chip, lun)) {
2985 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
2986 TRACE_RET(chip, TRANSPORT_FAILED);
2988 if (get_lun_card(chip, lun) != SD_CARD) {
2989 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT);
2990 TRACE_RET(chip, TRANSPORT_FAILED);
2993 switch (srb->cmnd[0]) {
2994 case SD_PASS_THRU_MODE:
2995 result = sd_pass_thru_mode(srb, chip);
2998 case SD_EXECUTE_NO_DATA:
2999 result = sd_execute_no_data(srb, chip);
3002 case SD_EXECUTE_READ:
3003 result = sd_execute_read_data(srb, chip);
3006 case SD_EXECUTE_WRITE:
3007 result = sd_execute_write_data(srb, chip);
3011 result = sd_get_cmd_rsp(srb, chip);
3015 result = sd_hw_rst(srb, chip);
3019 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3020 TRACE_RET(chip, TRANSPORT_FAILED);
3027 #ifdef SUPPORT_MAGIC_GATE
3028 static int mg_report_key(struct scsi_cmnd *srb, struct rtsx_chip *chip)
3030 struct ms_info *ms_card = &(chip->ms_card);
3031 unsigned int lun = SCSI_LUN(srb);
3035 rtsx_disable_aspm(chip);
3037 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
3041 rtsx_set_stat(chip, RTSX_STAT_RUN);
3043 ms_cleanup_work(chip);
3045 if (!check_card_ready(chip, lun)) {
3046 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
3047 TRACE_RET(chip, TRANSPORT_FAILED);
3049 if (get_lun_card(chip, lun) != MS_CARD) {
3050 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT);
3051 TRACE_RET(chip, TRANSPORT_FAILED);
3054 if (srb->cmnd[7] != KC_MG_R_PRO) {
3055 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3056 TRACE_RET(chip, TRANSPORT_FAILED);
3059 if (!CHK_MSPRO(ms_card)) {
3060 set_sense_type(chip, lun, SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM);
3061 TRACE_RET(chip, TRANSPORT_FAILED);
3064 key_format = srb->cmnd[10] & 0x3F;
3065 dev_dbg(rtsx_dev(chip), "key_format = 0x%x\n", key_format);
3067 switch (key_format) {
3068 case KF_GET_LOC_EKB:
3069 if ((scsi_bufflen(srb) == 0x41C) &&
3070 (srb->cmnd[8] == 0x04) &&
3071 (srb->cmnd[9] == 0x1C)) {
3072 retval = mg_get_local_EKB(srb, chip);
3073 if (retval != STATUS_SUCCESS)
3074 TRACE_RET(chip, TRANSPORT_FAILED);
3077 set_sense_type(chip, lun,
3078 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3079 TRACE_RET(chip, TRANSPORT_FAILED);
3084 if ((scsi_bufflen(srb) == 0x24) &&
3085 (srb->cmnd[8] == 0x00) &&
3086 (srb->cmnd[9] == 0x24)) {
3087 retval = mg_get_rsp_chg(srb, chip);
3088 if (retval != STATUS_SUCCESS)
3089 TRACE_RET(chip, TRANSPORT_FAILED);
3092 set_sense_type(chip, lun,
3093 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3094 TRACE_RET(chip, TRANSPORT_FAILED);
3099 ms_card->mg_entry_num = srb->cmnd[5];
3100 if ((scsi_bufflen(srb) == 0x404) &&
3101 (srb->cmnd[8] == 0x04) &&
3102 (srb->cmnd[9] == 0x04) &&
3103 (srb->cmnd[2] == 0x00) &&
3104 (srb->cmnd[3] == 0x00) &&
3105 (srb->cmnd[4] == 0x00) &&
3106 (srb->cmnd[5] < 32)) {
3107 retval = mg_get_ICV(srb, chip);
3108 if (retval != STATUS_SUCCESS)
3109 TRACE_RET(chip, TRANSPORT_FAILED);
3112 set_sense_type(chip, lun,
3113 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3114 TRACE_RET(chip, TRANSPORT_FAILED);
3119 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3120 TRACE_RET(chip, TRANSPORT_FAILED);
3123 scsi_set_resid(srb, 0);
3124 return TRANSPORT_GOOD;
3127 static int mg_send_key(struct scsi_cmnd *srb, struct rtsx_chip *chip)
3129 struct ms_info *ms_card = &(chip->ms_card);
3130 unsigned int lun = SCSI_LUN(srb);
3134 rtsx_disable_aspm(chip);
3136 if (chip->ss_en && (rtsx_get_stat(chip) == RTSX_STAT_SS)) {
3140 rtsx_set_stat(chip, RTSX_STAT_RUN);
3142 ms_cleanup_work(chip);
3144 if (!check_card_ready(chip, lun)) {
3145 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_NOT_PRESENT);
3146 TRACE_RET(chip, TRANSPORT_FAILED);
3148 if (check_card_wp(chip, lun)) {
3149 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_WRITE_PROTECT);
3150 TRACE_RET(chip, TRANSPORT_FAILED);
3152 if (get_lun_card(chip, lun) != MS_CARD) {
3153 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_LUN_NOT_SUPPORT);
3154 TRACE_RET(chip, TRANSPORT_FAILED);
3157 if (srb->cmnd[7] != KC_MG_R_PRO) {
3158 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3159 TRACE_RET(chip, TRANSPORT_FAILED);
3162 if (!CHK_MSPRO(ms_card)) {
3163 set_sense_type(chip, lun, SENSE_TYPE_MG_INCOMPATIBLE_MEDIUM);
3164 TRACE_RET(chip, TRANSPORT_FAILED);
3167 key_format = srb->cmnd[10] & 0x3F;
3168 dev_dbg(rtsx_dev(chip), "key_format = 0x%x\n", key_format);
3170 switch (key_format) {
3171 case KF_SET_LEAF_ID:
3172 if ((scsi_bufflen(srb) == 0x0C) &&
3173 (srb->cmnd[8] == 0x00) &&
3174 (srb->cmnd[9] == 0x0C)) {
3175 retval = mg_set_leaf_id(srb, chip);
3176 if (retval != STATUS_SUCCESS)
3177 TRACE_RET(chip, TRANSPORT_FAILED);
3180 set_sense_type(chip, lun,
3181 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3182 TRACE_RET(chip, TRANSPORT_FAILED);
3187 if ((scsi_bufflen(srb) == 0x0C) &&
3188 (srb->cmnd[8] == 0x00) &&
3189 (srb->cmnd[9] == 0x0C)) {
3190 retval = mg_chg(srb, chip);
3191 if (retval != STATUS_SUCCESS)
3192 TRACE_RET(chip, TRANSPORT_FAILED);
3195 set_sense_type(chip, lun,
3196 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3197 TRACE_RET(chip, TRANSPORT_FAILED);
3202 if ((scsi_bufflen(srb) == 0x0C) &&
3203 (srb->cmnd[8] == 0x00) &&
3204 (srb->cmnd[9] == 0x0C)) {
3205 retval = mg_rsp(srb, chip);
3206 if (retval != STATUS_SUCCESS)
3207 TRACE_RET(chip, TRANSPORT_FAILED);
3210 set_sense_type(chip, lun,
3211 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3212 TRACE_RET(chip, TRANSPORT_FAILED);
3217 ms_card->mg_entry_num = srb->cmnd[5];
3218 if ((scsi_bufflen(srb) == 0x404) &&
3219 (srb->cmnd[8] == 0x04) &&
3220 (srb->cmnd[9] == 0x04) &&
3221 (srb->cmnd[2] == 0x00) &&
3222 (srb->cmnd[3] == 0x00) &&
3223 (srb->cmnd[4] == 0x00) &&
3224 (srb->cmnd[5] < 32)) {
3225 retval = mg_set_ICV(srb, chip);
3226 if (retval != STATUS_SUCCESS)
3227 TRACE_RET(chip, TRANSPORT_FAILED);
3230 set_sense_type(chip, lun,
3231 SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3232 TRACE_RET(chip, TRANSPORT_FAILED);
3237 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3238 TRACE_RET(chip, TRANSPORT_FAILED);
3241 scsi_set_resid(srb, 0);
3242 return TRANSPORT_GOOD;
3246 int rtsx_scsi_handler(struct scsi_cmnd *srb, struct rtsx_chip *chip)
3248 #ifdef SUPPORT_SD_LOCK
3249 struct sd_info *sd_card = &(chip->sd_card);
3251 struct ms_info *ms_card = &(chip->ms_card);
3252 unsigned int lun = SCSI_LUN(srb);
3255 #ifdef SUPPORT_SD_LOCK
3256 if (sd_card->sd_erase_status) {
3257 /* Block all SCSI command except for
3258 * REQUEST_SENSE and rs_ppstatus
3260 if (!((srb->cmnd[0] == VENDOR_CMND) &&
3261 (srb->cmnd[1] == SCSI_APP_CMD) &&
3262 (srb->cmnd[2] == GET_DEV_STATUS)) &&
3263 (srb->cmnd[0] != REQUEST_SENSE)) {
3264 /* Logical Unit Not Ready Format in Progress */
3265 set_sense_data(chip, lun, CUR_ERR,
3266 0x02, 0, 0x04, 0x04, 0, 0);
3267 TRACE_RET(chip, TRANSPORT_FAILED);
3272 if ((get_lun_card(chip, lun) == MS_CARD) &&
3273 (ms_card->format_status == FORMAT_IN_PROGRESS)) {
3274 if ((srb->cmnd[0] != REQUEST_SENSE) &&
3275 (srb->cmnd[0] != INQUIRY)) {
3276 /* Logical Unit Not Ready Format in Progress */
3277 set_sense_data(chip, lun, CUR_ERR, 0x02, 0, 0x04, 0x04,
3278 0, (u16)(ms_card->progress));
3279 TRACE_RET(chip, TRANSPORT_FAILED);
3283 switch (srb->cmnd[0]) {
3288 result = read_write(srb, chip);
3289 #if !defined(LED_AUTO_BLINK) && !defined(REGULAR_BLINK)
3290 led_shine(srb, chip);
3294 case TEST_UNIT_READY:
3295 result = test_unit_ready(srb, chip);
3299 result = inquiry(srb, chip);
3303 result = read_capacity(srb, chip);
3307 result = start_stop_unit(srb, chip);
3310 case ALLOW_MEDIUM_REMOVAL:
3311 result = allow_medium_removal(srb, chip);
3315 result = request_sense(srb, chip);
3320 result = mode_sense(srb, chip);
3324 result = read_format_capacity(srb, chip);
3328 result = vendor_cmnd(srb, chip);
3332 result = ms_sp_cmnd(srb, chip);
3336 case SD_PASS_THRU_MODE:
3337 case SD_EXECUTE_NO_DATA:
3338 case SD_EXECUTE_READ:
3339 case SD_EXECUTE_WRITE:
3342 result = sd_extention_cmnd(srb, chip);
3346 #ifdef SUPPORT_MAGIC_GATE
3347 case CMD_MSPRO_MG_RKEY:
3348 result = mg_report_key(srb, chip);
3351 case CMD_MSPRO_MG_SKEY:
3352 result = mg_send_key(srb, chip);
3359 result = TRANSPORT_GOOD;
3363 set_sense_type(chip, lun, SENSE_TYPE_MEDIA_INVALID_CMD_FIELD);
3364 result = TRANSPORT_FAILED;