2 * Driver for Realtek PCI-Express card reader
4 * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, see <http://www.gnu.org/licenses/>.
20 * Wei WANG (wei_wang@realsil.com.cn)
21 * Micky Ching (micky_ching@realsil.com.cn)
24 #include <linux/blkdev.h>
25 #include <linux/kthread.h>
26 #include <linux/sched.h>
30 /***********************************************************************
31 * Scatter-gather transfer buffer access routines
32 ***********************************************************************/
35 * Copy a buffer of length buflen to/from the srb's transfer buffer.
36 * (Note: for scatter-gather transfers (srb->use_sg > 0), srb->request_buffer
37 * points to a list of s-g entries and we ignore srb->request_bufflen.
38 * For non-scatter-gather transfers, srb->request_buffer points to the
39 * transfer buffer itself and srb->request_bufflen is the buffer's length.)
40 * Update the *index and *offset variables so that the next copy will
41 * pick up from where this one left off.
44 unsigned int rtsx_stor_access_xfer_buf(unsigned char *buffer,
46 struct scsi_cmnd *srb,
49 enum xfer_buf_dir dir)
53 /* If not using scatter-gather, just transfer the data directly. */
54 if (scsi_sg_count(srb) == 0) {
55 unsigned char *sgbuffer;
57 if (*offset >= scsi_bufflen(srb))
59 cnt = min(buflen, scsi_bufflen(srb) - *offset);
61 sgbuffer = (unsigned char *)scsi_sglist(srb) + *offset;
63 if (dir == TO_XFER_BUF)
64 memcpy(sgbuffer, buffer, cnt);
66 memcpy(buffer, sgbuffer, cnt);
70 * Using scatter-gather. We have to go through the list one entry
71 * at a time. Each s-g entry contains some number of pages, and
72 * each page has to be kmap()'ed separately.
75 struct scatterlist *sg =
76 (struct scatterlist *)scsi_sglist(srb)
80 * This loop handles a single s-g list entry, which may
81 * include multiple pages. Find the initial page structure
82 * and the starting offset within the page, and update
83 * the *offset and *index values for the next loop.
86 while (cnt < buflen && *index < scsi_sg_count(srb)) {
87 struct page *page = sg_page(sg) +
88 ((sg->offset + *offset) >> PAGE_SHIFT);
89 unsigned int poff = (sg->offset + *offset) &
91 unsigned int sglen = sg->length - *offset;
93 if (sglen > buflen - cnt) {
94 /* Transfer ends within this s-g entry */
98 /* Transfer continues to next s-g entry */
105 unsigned int plen = min(sglen, (unsigned int)
107 unsigned char *ptr = kmap(page);
109 if (dir == TO_XFER_BUF)
110 memcpy(ptr + poff, buffer + cnt, plen);
112 memcpy(buffer + cnt, ptr + poff, plen);
115 /* Start at the beginning of the next page */
124 /* Return the amount actually transferred */
129 * Store the contents of buffer into srb's transfer buffer and set the
132 void rtsx_stor_set_xfer_buf(unsigned char *buffer,
133 unsigned int buflen, struct scsi_cmnd *srb)
135 unsigned int index = 0, offset = 0;
137 rtsx_stor_access_xfer_buf(buffer, buflen, srb, &index, &offset,
139 if (buflen < scsi_bufflen(srb))
140 scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
143 void rtsx_stor_get_xfer_buf(unsigned char *buffer,
144 unsigned int buflen, struct scsi_cmnd *srb)
146 unsigned int index = 0, offset = 0;
148 rtsx_stor_access_xfer_buf(buffer, buflen, srb, &index, &offset,
150 if (buflen < scsi_bufflen(srb))
151 scsi_set_resid(srb, scsi_bufflen(srb) - buflen);
154 /***********************************************************************
156 ***********************************************************************/
159 * Invoke the transport and basic error-handling/recovery methods
161 * This is used to send the message to the device and receive the response.
163 void rtsx_invoke_transport(struct scsi_cmnd *srb, struct rtsx_chip *chip)
167 result = rtsx_scsi_handler(srb, chip);
170 * if the command gets aborted by the higher layers, we need to
171 * short-circuit all other processing.
173 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
174 dev_dbg(rtsx_dev(chip), "-- command was aborted\n");
175 srb->result = DID_ABORT << 16;
179 /* if there is a transport error, reset and don't auto-sense */
180 if (result == TRANSPORT_ERROR) {
181 dev_dbg(rtsx_dev(chip), "-- transport indicates error, resetting\n");
182 srb->result = DID_ERROR << 16;
186 srb->result = SAM_STAT_GOOD;
189 * If we have a failure, we're going to do a REQUEST_SENSE
190 * automatically. Note that we differentiate between a command
191 * "failure" and an "error" in the transport mechanism.
193 if (result == TRANSPORT_FAILED) {
194 /* set the result so the higher layers expect this data */
195 srb->result = SAM_STAT_CHECK_CONDITION;
196 memcpy(srb->sense_buffer,
197 (unsigned char *)&chip->sense_buffer[SCSI_LUN(srb)],
198 sizeof(struct sense_data_t));
207 void rtsx_add_cmd(struct rtsx_chip *chip,
208 u8 cmd_type, u16 reg_addr, u8 mask, u8 data)
210 __le32 *cb = (__le32 *)(chip->host_cmds_ptr);
213 val |= (u32)(cmd_type & 0x03) << 30;
214 val |= (u32)(reg_addr & 0x3FFF) << 16;
215 val |= (u32)mask << 8;
218 spin_lock_irq(&chip->rtsx->reg_lock);
219 if (chip->ci < (HOST_CMDS_BUF_LEN / 4))
220 cb[(chip->ci)++] = cpu_to_le32(val);
222 spin_unlock_irq(&chip->rtsx->reg_lock);
225 void rtsx_send_cmd_no_wait(struct rtsx_chip *chip)
229 rtsx_writel(chip, RTSX_HCBAR, chip->host_cmds_addr);
231 val |= (u32)(chip->ci * 4) & 0x00FFFFFF;
232 /* Hardware Auto Response */
234 rtsx_writel(chip, RTSX_HCBCTLR, val);
237 int rtsx_send_cmd(struct rtsx_chip *chip, u8 card, int timeout)
239 struct rtsx_dev *rtsx = chip->rtsx;
240 struct completion trans_done;
246 rtsx->check_card_cd = SD_EXIST;
247 else if (card == MS_CARD)
248 rtsx->check_card_cd = MS_EXIST;
249 else if (card == XD_CARD)
250 rtsx->check_card_cd = XD_EXIST;
252 rtsx->check_card_cd = 0;
254 spin_lock_irq(&rtsx->reg_lock);
256 /* set up data structures for the wakeup system */
257 rtsx->done = &trans_done;
258 rtsx->trans_result = TRANS_NOT_READY;
259 init_completion(&trans_done);
260 rtsx->trans_state = STATE_TRANS_CMD;
262 rtsx_writel(chip, RTSX_HCBAR, chip->host_cmds_addr);
264 val |= (u32)(chip->ci * 4) & 0x00FFFFFF;
265 /* Hardware Auto Response */
267 rtsx_writel(chip, RTSX_HCBCTLR, val);
269 spin_unlock_irq(&rtsx->reg_lock);
271 /* Wait for TRANS_OK_INT */
272 timeleft = wait_for_completion_interruptible_timeout(
273 &trans_done, msecs_to_jiffies(timeout));
275 dev_dbg(rtsx_dev(chip), "chip->int_reg = 0x%x\n",
278 goto finish_send_cmd;
281 spin_lock_irq(&rtsx->reg_lock);
282 if (rtsx->trans_result == TRANS_RESULT_FAIL)
284 else if (rtsx->trans_result == TRANS_RESULT_OK)
287 spin_unlock_irq(&rtsx->reg_lock);
291 rtsx->trans_state = STATE_TRANS_NONE;
294 rtsx_stop_cmd(chip, card);
299 static inline void rtsx_add_sg_tbl(
300 struct rtsx_chip *chip, u32 addr, u32 len, u8 option)
302 __le64 *sgb = (__le64 *)(chip->host_sg_tbl_ptr);
310 temp_opt = option & (~RTSX_SG_END);
315 val = ((u64)addr << 32) | ((u64)temp_len << 12) | temp_opt;
317 if (chip->sgi < (HOST_SG_TBL_BUF_LEN / 8))
318 sgb[(chip->sgi)++] = cpu_to_le64(val);
325 static int rtsx_transfer_sglist_adma_partial(struct rtsx_chip *chip, u8 card,
326 struct scatterlist *sg, int num_sg,
328 unsigned int *offset, int size,
329 enum dma_data_direction dma_dir,
332 struct rtsx_dev *rtsx = chip->rtsx;
333 struct completion trans_done;
335 int sg_cnt, i, resid;
338 struct scatterlist *sg_ptr;
341 if (!sg || (num_sg <= 0) || !offset || !index)
344 if (dma_dir == DMA_TO_DEVICE)
345 dir = HOST_TO_DEVICE;
346 else if (dma_dir == DMA_FROM_DEVICE)
347 dir = DEVICE_TO_HOST;
352 rtsx->check_card_cd = SD_EXIST;
353 else if (card == MS_CARD)
354 rtsx->check_card_cd = MS_EXIST;
355 else if (card == XD_CARD)
356 rtsx->check_card_cd = XD_EXIST;
358 rtsx->check_card_cd = 0;
360 spin_lock_irq(&rtsx->reg_lock);
362 /* set up data structures for the wakeup system */
363 rtsx->done = &trans_done;
365 rtsx->trans_state = STATE_TRANS_SG;
366 rtsx->trans_result = TRANS_NOT_READY;
368 spin_unlock_irq(&rtsx->reg_lock);
370 sg_cnt = dma_map_sg(&rtsx->pci->dev, sg, num_sg, dma_dir);
376 * Usually the next entry will be @sg@ + 1, but if this sg element
377 * is part of a chained scatterlist, it could jump to the start of
378 * a new scatterlist array. So here we use sg_next to move to
381 for (i = 0; i < *index; i++)
382 sg_ptr = sg_next(sg_ptr);
383 for (i = *index; i < sg_cnt; i++) {
388 addr = sg_dma_address(sg_ptr);
389 len = sg_dma_len(sg_ptr);
391 dev_dbg(rtsx_dev(chip), "DMA addr: 0x%x, Len: 0x%x\n",
392 (unsigned int)addr, len);
393 dev_dbg(rtsx_dev(chip), "*index = %d, *offset = %d\n",
398 if ((len - *offset) > resid) {
403 resid -= (len - *offset);
408 if ((i == (sg_cnt - 1)) || !resid)
409 option = RTSX_SG_VALID | RTSX_SG_END | RTSX_SG_TRANS_DATA;
411 option = RTSX_SG_VALID | RTSX_SG_TRANS_DATA;
413 rtsx_add_sg_tbl(chip, (u32)addr, (u32)len, option);
418 sg_ptr = sg_next(sg_ptr);
421 dev_dbg(rtsx_dev(chip), "SG table count = %d\n", chip->sgi);
423 val |= (u32)(dir & 0x01) << 29;
426 spin_lock_irq(&rtsx->reg_lock);
428 init_completion(&trans_done);
430 rtsx_writel(chip, RTSX_HDBAR, chip->host_sg_tbl_addr);
431 rtsx_writel(chip, RTSX_HDBCTLR, val);
433 spin_unlock_irq(&rtsx->reg_lock);
435 timeleft = wait_for_completion_interruptible_timeout(
436 &trans_done, msecs_to_jiffies(timeout));
438 dev_dbg(rtsx_dev(chip), "Timeout (%s %d)\n",
440 dev_dbg(rtsx_dev(chip), "chip->int_reg = 0x%x\n",
446 spin_lock_irq(&rtsx->reg_lock);
447 if (rtsx->trans_result == TRANS_RESULT_FAIL) {
449 spin_unlock_irq(&rtsx->reg_lock);
452 spin_unlock_irq(&rtsx->reg_lock);
454 /* Wait for TRANS_OK_INT */
455 spin_lock_irq(&rtsx->reg_lock);
456 if (rtsx->trans_result == TRANS_NOT_READY) {
457 init_completion(&trans_done);
458 spin_unlock_irq(&rtsx->reg_lock);
459 timeleft = wait_for_completion_interruptible_timeout(
460 &trans_done, msecs_to_jiffies(timeout));
462 dev_dbg(rtsx_dev(chip), "Timeout (%s %d)\n",
464 dev_dbg(rtsx_dev(chip), "chip->int_reg = 0x%x\n",
470 spin_unlock_irq(&rtsx->reg_lock);
473 spin_lock_irq(&rtsx->reg_lock);
474 if (rtsx->trans_result == TRANS_RESULT_FAIL)
476 else if (rtsx->trans_result == TRANS_RESULT_OK)
479 spin_unlock_irq(&rtsx->reg_lock);
483 rtsx->trans_state = STATE_TRANS_NONE;
484 dma_unmap_sg(&rtsx->pci->dev, sg, num_sg, dma_dir);
487 rtsx_stop_cmd(chip, card);
492 static int rtsx_transfer_sglist_adma(struct rtsx_chip *chip, u8 card,
493 struct scatterlist *sg, int num_sg,
494 enum dma_data_direction dma_dir,
497 struct rtsx_dev *rtsx = chip->rtsx;
498 struct completion trans_done;
503 struct scatterlist *sg_ptr;
505 if (!sg || (num_sg <= 0))
508 if (dma_dir == DMA_TO_DEVICE)
509 dir = HOST_TO_DEVICE;
510 else if (dma_dir == DMA_FROM_DEVICE)
511 dir = DEVICE_TO_HOST;
516 rtsx->check_card_cd = SD_EXIST;
517 else if (card == MS_CARD)
518 rtsx->check_card_cd = MS_EXIST;
519 else if (card == XD_CARD)
520 rtsx->check_card_cd = XD_EXIST;
522 rtsx->check_card_cd = 0;
524 spin_lock_irq(&rtsx->reg_lock);
526 /* set up data structures for the wakeup system */
527 rtsx->done = &trans_done;
529 rtsx->trans_state = STATE_TRANS_SG;
530 rtsx->trans_result = TRANS_NOT_READY;
532 spin_unlock_irq(&rtsx->reg_lock);
534 buf_cnt = dma_map_sg(&rtsx->pci->dev, sg, num_sg, dma_dir);
538 for (i = 0; i <= buf_cnt / (HOST_SG_TBL_BUF_LEN / 8); i++) {
542 if (i == buf_cnt / (HOST_SG_TBL_BUF_LEN / 8))
543 sg_cnt = buf_cnt % (HOST_SG_TBL_BUF_LEN / 8);
545 sg_cnt = HOST_SG_TBL_BUF_LEN / 8;
548 for (j = 0; j < sg_cnt; j++) {
549 dma_addr_t addr = sg_dma_address(sg_ptr);
550 unsigned int len = sg_dma_len(sg_ptr);
553 dev_dbg(rtsx_dev(chip), "DMA addr: 0x%x, Len: 0x%x\n",
554 (unsigned int)addr, len);
556 if (j == (sg_cnt - 1))
557 option = RTSX_SG_VALID | RTSX_SG_END | RTSX_SG_TRANS_DATA;
559 option = RTSX_SG_VALID | RTSX_SG_TRANS_DATA;
561 rtsx_add_sg_tbl(chip, (u32)addr, (u32)len, option);
563 sg_ptr = sg_next(sg_ptr);
566 dev_dbg(rtsx_dev(chip), "SG table count = %d\n", chip->sgi);
568 val |= (u32)(dir & 0x01) << 29;
571 spin_lock_irq(&rtsx->reg_lock);
573 init_completion(&trans_done);
575 rtsx_writel(chip, RTSX_HDBAR, chip->host_sg_tbl_addr);
576 rtsx_writel(chip, RTSX_HDBCTLR, val);
578 spin_unlock_irq(&rtsx->reg_lock);
580 timeleft = wait_for_completion_interruptible_timeout(
581 &trans_done, msecs_to_jiffies(timeout));
583 dev_dbg(rtsx_dev(chip), "Timeout (%s %d)\n",
585 dev_dbg(rtsx_dev(chip), "chip->int_reg = 0x%x\n",
591 spin_lock_irq(&rtsx->reg_lock);
592 if (rtsx->trans_result == TRANS_RESULT_FAIL) {
594 spin_unlock_irq(&rtsx->reg_lock);
597 spin_unlock_irq(&rtsx->reg_lock);
602 /* Wait for TRANS_OK_INT */
603 spin_lock_irq(&rtsx->reg_lock);
604 if (rtsx->trans_result == TRANS_NOT_READY) {
605 init_completion(&trans_done);
606 spin_unlock_irq(&rtsx->reg_lock);
607 timeleft = wait_for_completion_interruptible_timeout(
608 &trans_done, msecs_to_jiffies(timeout));
610 dev_dbg(rtsx_dev(chip), "Timeout (%s %d)\n",
612 dev_dbg(rtsx_dev(chip), "chip->int_reg = 0x%x\n",
618 spin_unlock_irq(&rtsx->reg_lock);
621 spin_lock_irq(&rtsx->reg_lock);
622 if (rtsx->trans_result == TRANS_RESULT_FAIL)
624 else if (rtsx->trans_result == TRANS_RESULT_OK)
627 spin_unlock_irq(&rtsx->reg_lock);
631 rtsx->trans_state = STATE_TRANS_NONE;
632 dma_unmap_sg(&rtsx->pci->dev, sg, num_sg, dma_dir);
635 rtsx_stop_cmd(chip, card);
640 static int rtsx_transfer_buf(struct rtsx_chip *chip, u8 card, void *buf,
641 size_t len, enum dma_data_direction dma_dir,
644 struct rtsx_dev *rtsx = chip->rtsx;
645 struct completion trans_done;
652 if (!buf || (len <= 0))
655 if (dma_dir == DMA_TO_DEVICE)
656 dir = HOST_TO_DEVICE;
657 else if (dma_dir == DMA_FROM_DEVICE)
658 dir = DEVICE_TO_HOST;
662 addr = dma_map_single(&rtsx->pci->dev, buf, len, dma_dir);
663 if (dma_mapping_error(&rtsx->pci->dev, addr))
667 rtsx->check_card_cd = SD_EXIST;
668 else if (card == MS_CARD)
669 rtsx->check_card_cd = MS_EXIST;
670 else if (card == XD_CARD)
671 rtsx->check_card_cd = XD_EXIST;
673 rtsx->check_card_cd = 0;
675 val |= (u32)(dir & 0x01) << 29;
676 val |= (u32)(len & 0x00FFFFFF);
678 spin_lock_irq(&rtsx->reg_lock);
680 /* set up data structures for the wakeup system */
681 rtsx->done = &trans_done;
683 init_completion(&trans_done);
685 rtsx->trans_state = STATE_TRANS_BUF;
686 rtsx->trans_result = TRANS_NOT_READY;
688 rtsx_writel(chip, RTSX_HDBAR, addr);
689 rtsx_writel(chip, RTSX_HDBCTLR, val);
691 spin_unlock_irq(&rtsx->reg_lock);
693 /* Wait for TRANS_OK_INT */
694 timeleft = wait_for_completion_interruptible_timeout(
695 &trans_done, msecs_to_jiffies(timeout));
697 dev_dbg(rtsx_dev(chip), "Timeout (%s %d)\n",
699 dev_dbg(rtsx_dev(chip), "chip->int_reg = 0x%x\n",
705 spin_lock_irq(&rtsx->reg_lock);
706 if (rtsx->trans_result == TRANS_RESULT_FAIL)
708 else if (rtsx->trans_result == TRANS_RESULT_OK)
711 spin_unlock_irq(&rtsx->reg_lock);
715 rtsx->trans_state = STATE_TRANS_NONE;
716 dma_unmap_single(&rtsx->pci->dev, addr, len, dma_dir);
719 rtsx_stop_cmd(chip, card);
724 int rtsx_transfer_data_partial(struct rtsx_chip *chip, u8 card,
725 void *buf, size_t len, int use_sg,
726 unsigned int *index, unsigned int *offset,
727 enum dma_data_direction dma_dir, int timeout)
731 /* don't transfer data during abort processing */
732 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT))
736 struct scatterlist *sg = buf;
738 err = rtsx_transfer_sglist_adma_partial(chip, card, sg, use_sg,
739 index, offset, (int)len,
742 err = rtsx_transfer_buf(chip, card,
743 buf, len, dma_dir, timeout);
746 if (RTSX_TST_DELINK(chip)) {
747 RTSX_CLR_DELINK(chip);
748 chip->need_reinit = SD_CARD | MS_CARD | XD_CARD;
749 rtsx_reinit_cards(chip, 1);
756 int rtsx_transfer_data(struct rtsx_chip *chip, u8 card, void *buf, size_t len,
757 int use_sg, enum dma_data_direction dma_dir, int timeout)
761 dev_dbg(rtsx_dev(chip), "use_sg = %d\n", use_sg);
763 /* don't transfer data during abort processing */
764 if (rtsx_chk_stat(chip, RTSX_STAT_ABORT))
768 err = rtsx_transfer_sglist_adma(chip, card, buf,
769 use_sg, dma_dir, timeout);
771 err = rtsx_transfer_buf(chip, card, buf, len, dma_dir, timeout);
775 if (RTSX_TST_DELINK(chip)) {
776 RTSX_CLR_DELINK(chip);
777 chip->need_reinit = SD_CARD | MS_CARD | XD_CARD;
778 rtsx_reinit_cards(chip, 1);