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