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