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