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