• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/workqueue.h>
27 
28 #include "rtsx.h"
29 #include "rtsx_chip.h"
30 #include "rtsx_transport.h"
31 #include "rtsx_scsi.h"
32 #include "rtsx_card.h"
33 #include "general.h"
34 
35 #include "ms.h"
36 #include "sd.h"
37 #include "xd.h"
38 
39 #define DRIVER_VERSION "v1.10"
40 
41 MODULE_DESCRIPTION("Realtek PCI-Express card reader driver");
42 MODULE_LICENSE("GPL");
43 MODULE_VERSION(DRIVER_VERSION);
44 
45 static unsigned int delay_use = 1;
46 module_param(delay_use, uint, S_IRUGO | S_IWUSR);
47 MODULE_PARM_DESC(delay_use, "seconds to delay before using a new device");
48 
49 static int ss_en;
50 module_param(ss_en, int, S_IRUGO | S_IWUSR);
51 MODULE_PARM_DESC(ss_en, "enable selective suspend");
52 
53 static int ss_interval = 50;
54 module_param(ss_interval, int, S_IRUGO | S_IWUSR);
55 MODULE_PARM_DESC(ss_interval, "Interval to enter ss state in seconds");
56 
57 static int auto_delink_en;
58 module_param(auto_delink_en, int, S_IRUGO | S_IWUSR);
59 MODULE_PARM_DESC(auto_delink_en, "enable auto delink");
60 
61 static unsigned char aspm_l0s_l1_en;
62 module_param(aspm_l0s_l1_en, byte, S_IRUGO | S_IWUSR);
63 MODULE_PARM_DESC(aspm_l0s_l1_en, "enable device aspm");
64 
65 static int msi_en;
66 module_param(msi_en, int, S_IRUGO | S_IWUSR);
67 MODULE_PARM_DESC(msi_en, "enable msi");
68 
69 static irqreturn_t rtsx_interrupt(int irq, void *dev_id);
70 
71 /***********************************************************************
72  * Host functions
73  ***********************************************************************/
74 
host_info(struct Scsi_Host * host)75 static const char *host_info(struct Scsi_Host *host)
76 {
77 	return "SCSI emulation for PCI-Express Mass Storage devices";
78 }
79 
slave_alloc(struct scsi_device * sdev)80 static int slave_alloc(struct scsi_device *sdev)
81 {
82 	/*
83 	 * Set the INQUIRY transfer length to 36.  We don't use any of
84 	 * the extra data and many devices choke if asked for more or
85 	 * less than 36 bytes.
86 	 */
87 	sdev->inquiry_len = 36;
88 	return 0;
89 }
90 
slave_configure(struct scsi_device * sdev)91 static int slave_configure(struct scsi_device *sdev)
92 {
93 	/* Scatter-gather buffers (all but the last) must have a length
94 	 * divisible by the bulk maxpacket size.  Otherwise a data packet
95 	 * would end up being short, causing a premature end to the data
96 	 * transfer.  Since high-speed bulk pipes have a maxpacket size
97 	 * of 512, we'll use that as the scsi device queue's DMA alignment
98 	 * mask.  Guaranteeing proper alignment of the first buffer will
99 	 * have the desired effect because, except at the beginning and
100 	 * the end, scatter-gather buffers follow page boundaries. */
101 	blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
102 
103 	/* Set the SCSI level to at least 2.  We'll leave it at 3 if that's
104 	 * what is originally reported.  We need this to avoid confusing
105 	 * the SCSI layer with devices that report 0 or 1, but need 10-byte
106 	 * commands (ala ATAPI devices behind certain bridges, or devices
107 	 * which simply have broken INQUIRY data).
108 	 *
109 	 * NOTE: This means /dev/sg programs (ala cdrecord) will get the
110 	 * actual information.  This seems to be the preference for
111 	 * programs like that.
112 	 *
113 	 * NOTE: This also means that /proc/scsi/scsi and sysfs may report
114 	 * the actual value or the modified one, depending on where the
115 	 * data comes from.
116 	 */
117 	if (sdev->scsi_level < SCSI_2)
118 		sdev->scsi_level = sdev->sdev_target->scsi_level = SCSI_2;
119 
120 	return 0;
121 }
122 
123 
124 /***********************************************************************
125  * /proc/scsi/ functions
126  ***********************************************************************/
127 
128 /* we use this macro to help us write into the buffer */
129 #undef SPRINTF
130 #define SPRINTF(args...) \
131 	do { if (pos < buffer+length) pos += sprintf(pos, ## args); } while (0)
132 
proc_info(struct Scsi_Host * host,char * buffer,char ** start,off_t offset,int length,int inout)133 static int proc_info(struct Scsi_Host *host, char *buffer,
134 		char **start, off_t offset, int length, int inout)
135 {
136 	char *pos = buffer;
137 
138 	/* if someone is sending us data, just throw it away */
139 	if (inout)
140 		return length;
141 
142 	/* print the controller name */
143 	SPRINTF("   Host scsi%d: %s\n", host->host_no, CR_DRIVER_NAME);
144 
145 	/* print product, vendor, and driver version strings */
146 	SPRINTF("       Vendor: Realtek Corp.\n");
147 	SPRINTF("      Product: PCIE Card Reader\n");
148 	SPRINTF("      Version: %s\n", DRIVER_VERSION);
149 
150 	/*
151 	 * Calculate start of next buffer, and return value.
152 	 */
153 	*start = buffer + offset;
154 
155 	if ((pos - buffer) < offset)
156 		return 0;
157 	else if ((pos - buffer - offset) < length)
158 		return pos - buffer - offset;
159 	else
160 		return length;
161 }
162 
163 /* queue a command */
164 /* This is always called with scsi_lock(host) held */
queuecommand_lck(struct scsi_cmnd * srb,void (* done)(struct scsi_cmnd *))165 static int queuecommand_lck(struct scsi_cmnd *srb,
166 			void (*done)(struct scsi_cmnd *))
167 {
168 	struct rtsx_dev *dev = host_to_rtsx(srb->device->host);
169 	struct rtsx_chip *chip = dev->chip;
170 
171 	/* check for state-transition errors */
172 	if (chip->srb != NULL) {
173 		printk(KERN_ERR "Error in %s: chip->srb = %p\n",
174 			__func__, chip->srb);
175 		return SCSI_MLQUEUE_HOST_BUSY;
176 	}
177 
178 	/* fail the command if we are disconnecting */
179 	if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
180 		printk(KERN_INFO "Fail command during disconnect\n");
181 		srb->result = DID_NO_CONNECT << 16;
182 		done(srb);
183 		return 0;
184 	}
185 
186 	/* enqueue the command and wake up the control thread */
187 	srb->scsi_done = done;
188 	chip->srb = srb;
189 	complete(&dev->cmnd_ready);
190 
191 	return 0;
192 }
193 
DEF_SCSI_QCMD(queuecommand)194 static DEF_SCSI_QCMD(queuecommand)
195 
196 /***********************************************************************
197  * Error handling functions
198  ***********************************************************************/
199 
200 /* Command timeout and abort */
201 static int command_abort(struct scsi_cmnd *srb)
202 {
203 	struct Scsi_Host *host = srb->device->host;
204 	struct rtsx_dev *dev = host_to_rtsx(host);
205 	struct rtsx_chip *chip = dev->chip;
206 
207 	printk(KERN_INFO "%s called\n", __func__);
208 
209 	scsi_lock(host);
210 
211 	/* Is this command still active? */
212 	if (chip->srb != srb) {
213 		scsi_unlock(host);
214 		printk(KERN_INFO "-- nothing to abort\n");
215 		return FAILED;
216 	}
217 
218 	rtsx_set_stat(chip, RTSX_STAT_ABORT);
219 
220 	scsi_unlock(host);
221 
222 	/* Wait for the aborted command to finish */
223 	wait_for_completion(&dev->notify);
224 
225 	return SUCCESS;
226 }
227 
228 /* This invokes the transport reset mechanism to reset the state of the
229  * device */
device_reset(struct scsi_cmnd * srb)230 static int device_reset(struct scsi_cmnd *srb)
231 {
232 	int result = 0;
233 
234 	printk(KERN_INFO "%s called\n", __func__);
235 
236 	return result < 0 ? FAILED : SUCCESS;
237 }
238 
239 /* Simulate a SCSI bus reset by resetting the device's USB port. */
bus_reset(struct scsi_cmnd * srb)240 static int bus_reset(struct scsi_cmnd *srb)
241 {
242 	int result = 0;
243 
244 	printk(KERN_INFO "%s called\n", __func__);
245 
246 	return result < 0 ? FAILED : SUCCESS;
247 }
248 
249 
250 /*
251  * this defines our host template, with which we'll allocate hosts
252  */
253 
254 static struct scsi_host_template rtsx_host_template = {
255 	/* basic userland interface stuff */
256 	.name =				CR_DRIVER_NAME,
257 	.proc_name =			CR_DRIVER_NAME,
258 	.proc_info =			proc_info,
259 	.info =				host_info,
260 
261 	/* command interface -- queued only */
262 	.queuecommand =			queuecommand,
263 
264 	/* error and abort handlers */
265 	.eh_abort_handler =		command_abort,
266 	.eh_device_reset_handler =	device_reset,
267 	.eh_bus_reset_handler =		bus_reset,
268 
269 	/* queue commands only, only one command per LUN */
270 	.can_queue =			1,
271 	.cmd_per_lun =			1,
272 
273 	/* unknown initiator id */
274 	.this_id =			-1,
275 
276 	.slave_alloc =			slave_alloc,
277 	.slave_configure =		slave_configure,
278 
279 	/* lots of sg segments can be handled */
280 	.sg_tablesize =			SG_ALL,
281 
282 	/* limit the total size of a transfer to 120 KB */
283 	.max_sectors =                  240,
284 
285 	/* merge commands... this seems to help performance, but
286 	 * periodically someone should test to see which setting is more
287 	 * optimal.
288 	 */
289 	.use_clustering =		1,
290 
291 	/* emulated HBA */
292 	.emulated =			1,
293 
294 	/* we do our own delay after a device or bus reset */
295 	.skip_settle_delay =		1,
296 
297 	/* module management */
298 	.module =			THIS_MODULE
299 };
300 
301 
rtsx_acquire_irq(struct rtsx_dev * dev)302 static int rtsx_acquire_irq(struct rtsx_dev *dev)
303 {
304 	struct rtsx_chip *chip = dev->chip;
305 
306 	printk(KERN_INFO "%s: chip->msi_en = %d, pci->irq = %d\n",
307 			__func__, chip->msi_en, dev->pci->irq);
308 
309 	if (request_irq(dev->pci->irq, rtsx_interrupt,
310 			chip->msi_en ? 0 : IRQF_SHARED,
311 			CR_DRIVER_NAME, dev)) {
312 		printk(KERN_ERR "rtsx: unable to grab IRQ %d, "
313 		       "disabling device\n", dev->pci->irq);
314 		return -1;
315 	}
316 
317 	dev->irq = dev->pci->irq;
318 	pci_intx(dev->pci, !chip->msi_en);
319 
320 	return 0;
321 }
322 
323 
rtsx_read_pci_cfg_byte(u8 bus,u8 dev,u8 func,u8 offset,u8 * val)324 int rtsx_read_pci_cfg_byte(u8 bus, u8 dev, u8 func, u8 offset, u8 *val)
325 {
326 	struct pci_dev *pdev;
327 	u8 data;
328 	u8 devfn = (dev << 3) | func;
329 
330 	pdev = pci_get_bus_and_slot(bus, devfn);
331 	if (!pdev)
332 		return -1;
333 
334 	pci_read_config_byte(pdev, offset, &data);
335 	if (val)
336 		*val = data;
337 
338 	return 0;
339 }
340 
341 #ifdef CONFIG_PM
342 /*
343  * power management
344  */
rtsx_suspend(struct pci_dev * pci,pm_message_t state)345 static int rtsx_suspend(struct pci_dev *pci, pm_message_t state)
346 {
347 	struct rtsx_dev *dev = (struct rtsx_dev *)pci_get_drvdata(pci);
348 	struct rtsx_chip *chip;
349 
350 	printk(KERN_INFO "Ready to suspend\n");
351 
352 	if (!dev) {
353 		printk(KERN_ERR "Invalid memory\n");
354 		return 0;
355 	}
356 
357 	/* lock the device pointers */
358 	mutex_lock(&(dev->dev_mutex));
359 
360 	chip = dev->chip;
361 
362 	rtsx_do_before_power_down(chip, PM_S3);
363 
364 	if (dev->irq >= 0) {
365 		synchronize_irq(dev->irq);
366 		free_irq(dev->irq, (void *)dev);
367 		dev->irq = -1;
368 	}
369 
370 	if (chip->msi_en)
371 		pci_disable_msi(pci);
372 
373 	pci_save_state(pci);
374 	pci_enable_wake(pci, pci_choose_state(pci, state), 1);
375 	pci_disable_device(pci);
376 	pci_set_power_state(pci, pci_choose_state(pci, state));
377 
378 	/* unlock the device pointers */
379 	mutex_unlock(&dev->dev_mutex);
380 
381 	return 0;
382 }
383 
rtsx_resume(struct pci_dev * pci)384 static int rtsx_resume(struct pci_dev *pci)
385 {
386 	struct rtsx_dev *dev = (struct rtsx_dev *)pci_get_drvdata(pci);
387 	struct rtsx_chip *chip;
388 
389 	printk(KERN_INFO "Ready to resume\n");
390 
391 	if (!dev) {
392 		printk(KERN_ERR "Invalid memory\n");
393 		return 0;
394 	}
395 
396 	chip = dev->chip;
397 
398 	/* lock the device pointers */
399 	mutex_lock(&(dev->dev_mutex));
400 
401 	pci_set_power_state(pci, PCI_D0);
402 	pci_restore_state(pci);
403 	if (pci_enable_device(pci) < 0) {
404 		printk(KERN_ERR "%s: pci_enable_device failed, "
405 		       "disabling device\n", CR_DRIVER_NAME);
406 		/* unlock the device pointers */
407 		mutex_unlock(&dev->dev_mutex);
408 		return -EIO;
409 	}
410 	pci_set_master(pci);
411 
412 	if (chip->msi_en) {
413 		if (pci_enable_msi(pci) < 0)
414 			chip->msi_en = 0;
415 	}
416 
417 	if (rtsx_acquire_irq(dev) < 0) {
418 		/* unlock the device pointers */
419 		mutex_unlock(&dev->dev_mutex);
420 		return -EIO;
421 	}
422 
423 	rtsx_write_register(chip, HOST_SLEEP_STATE, 0x03, 0x00);
424 	rtsx_init_chip(chip);
425 
426 	/* unlock the device pointers */
427 	mutex_unlock(&dev->dev_mutex);
428 
429 	return 0;
430 }
431 #endif /* CONFIG_PM */
432 
rtsx_shutdown(struct pci_dev * pci)433 static void rtsx_shutdown(struct pci_dev *pci)
434 {
435 	struct rtsx_dev *dev = (struct rtsx_dev *)pci_get_drvdata(pci);
436 	struct rtsx_chip *chip;
437 
438 	printk(KERN_INFO "Ready to shutdown\n");
439 
440 	if (!dev) {
441 		printk(KERN_ERR "Invalid memory\n");
442 		return;
443 	}
444 
445 	chip = dev->chip;
446 
447 	rtsx_do_before_power_down(chip, PM_S1);
448 
449 	if (dev->irq >= 0) {
450 		synchronize_irq(dev->irq);
451 		free_irq(dev->irq, (void *)dev);
452 		dev->irq = -1;
453 	}
454 
455 	if (chip->msi_en)
456 		pci_disable_msi(pci);
457 
458 	pci_disable_device(pci);
459 
460 	return;
461 }
462 
rtsx_control_thread(void * __dev)463 static int rtsx_control_thread(void *__dev)
464 {
465 	struct rtsx_dev *dev = (struct rtsx_dev *)__dev;
466 	struct rtsx_chip *chip = dev->chip;
467 	struct Scsi_Host *host = rtsx_to_host(dev);
468 
469 	for (;;) {
470 		if (wait_for_completion_interruptible(&dev->cmnd_ready))
471 			break;
472 
473 		/* lock the device pointers */
474 		mutex_lock(&(dev->dev_mutex));
475 
476 		/* if the device has disconnected, we are free to exit */
477 		if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
478 			printk(KERN_INFO "-- rtsx-control exiting\n");
479 			mutex_unlock(&dev->dev_mutex);
480 			break;
481 		}
482 
483 		/* lock access to the state */
484 		scsi_lock(host);
485 
486 		/* has the command aborted ? */
487 		if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
488 			chip->srb->result = DID_ABORT << 16;
489 			goto SkipForAbort;
490 		}
491 
492 		scsi_unlock(host);
493 
494 		/* reject the command if the direction indicator
495 		 * is UNKNOWN
496 		 */
497 		if (chip->srb->sc_data_direction == DMA_BIDIRECTIONAL) {
498 			printk(KERN_ERR "UNKNOWN data direction\n");
499 			chip->srb->result = DID_ERROR << 16;
500 		}
501 
502 		/* reject if target != 0 or if LUN is higher than
503 		 * the maximum known LUN
504 		 */
505 		else if (chip->srb->device->id) {
506 			printk(KERN_ERR "Bad target number (%d:%d)\n",
507 				chip->srb->device->id,
508 				chip->srb->device->lun);
509 			chip->srb->result = DID_BAD_TARGET << 16;
510 		}
511 
512 		else if (chip->srb->device->lun > chip->max_lun) {
513 			printk(KERN_ERR "Bad LUN (%d:%d)\n",
514 				chip->srb->device->id,
515 				chip->srb->device->lun);
516 			chip->srb->result = DID_BAD_TARGET << 16;
517 		}
518 
519 		/* we've got a command, let's do it! */
520 		else {
521 			RTSX_DEBUG(scsi_show_command(chip->srb));
522 			rtsx_invoke_transport(chip->srb, chip);
523 		}
524 
525 		/* lock access to the state */
526 		scsi_lock(host);
527 
528 		/* did the command already complete because of a disconnect? */
529 		if (!chip->srb)
530 			;		/* nothing to do */
531 
532 		/* indicate that the command is done */
533 		else if (chip->srb->result != DID_ABORT << 16) {
534 			chip->srb->scsi_done(chip->srb);
535 		} else {
536 SkipForAbort:
537 			printk(KERN_ERR "scsi command aborted\n");
538 		}
539 
540 		if (rtsx_chk_stat(chip, RTSX_STAT_ABORT)) {
541 			complete(&(dev->notify));
542 
543 			rtsx_set_stat(chip, RTSX_STAT_IDLE);
544 		}
545 
546 		/* finished working on this command */
547 		chip->srb = NULL;
548 		scsi_unlock(host);
549 
550 		/* unlock the device pointers */
551 		mutex_unlock(&dev->dev_mutex);
552 	} /* for (;;) */
553 
554 	/* notify the exit routine that we're actually exiting now
555 	 *
556 	 * complete()/wait_for_completion() is similar to up()/down(),
557 	 * except that complete() is safe in the case where the structure
558 	 * is getting deleted in a parallel mode of execution (i.e. just
559 	 * after the down() -- that's necessary for the thread-shutdown
560 	 * case.
561 	 *
562 	 * complete_and_exit() goes even further than this -- it is safe in
563 	 * the case that the thread of the caller is going away (not just
564 	 * the structure) -- this is necessary for the module-remove case.
565 	 * This is important in preemption kernels, which transfer the flow
566 	 * of execution immediately upon a complete().
567 	 */
568 	complete_and_exit(&dev->control_exit, 0);
569 }
570 
571 
rtsx_polling_thread(void * __dev)572 static int rtsx_polling_thread(void *__dev)
573 {
574 	struct rtsx_dev *dev = (struct rtsx_dev *)__dev;
575 	struct rtsx_chip *chip = dev->chip;
576 	struct sd_info *sd_card = &(chip->sd_card);
577 	struct xd_info *xd_card = &(chip->xd_card);
578 	struct ms_info *ms_card = &(chip->ms_card);
579 
580 	sd_card->cleanup_counter = 0;
581 	xd_card->cleanup_counter = 0;
582 	ms_card->cleanup_counter = 0;
583 
584 	/* Wait until SCSI scan finished */
585 	wait_timeout((delay_use + 5) * 1000);
586 
587 	for (;;) {
588 
589 		set_current_state(TASK_INTERRUPTIBLE);
590 		schedule_timeout(POLLING_INTERVAL);
591 
592 		/* lock the device pointers */
593 		mutex_lock(&(dev->dev_mutex));
594 
595 		/* if the device has disconnected, we are free to exit */
596 		if (rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
597 			printk(KERN_INFO "-- rtsx-polling exiting\n");
598 			mutex_unlock(&dev->dev_mutex);
599 			break;
600 		}
601 
602 		mutex_unlock(&dev->dev_mutex);
603 
604 		mspro_polling_format_status(chip);
605 
606 		/* lock the device pointers */
607 		mutex_lock(&(dev->dev_mutex));
608 
609 		rtsx_polling_func(chip);
610 
611 		/* unlock the device pointers */
612 		mutex_unlock(&dev->dev_mutex);
613 	}
614 
615 	complete_and_exit(&dev->polling_exit, 0);
616 }
617 
618 /*
619  * interrupt handler
620  */
rtsx_interrupt(int irq,void * dev_id)621 static irqreturn_t rtsx_interrupt(int irq, void *dev_id)
622 {
623 	struct rtsx_dev *dev = dev_id;
624 	struct rtsx_chip *chip;
625 	int retval;
626 	u32 status;
627 
628 	if (dev)
629 		chip = dev->chip;
630 	else
631 		return IRQ_NONE;
632 
633 	if (!chip)
634 		return IRQ_NONE;
635 
636 	spin_lock(&dev->reg_lock);
637 
638 	retval = rtsx_pre_handle_interrupt(chip);
639 	if (retval == STATUS_FAIL) {
640 		spin_unlock(&dev->reg_lock);
641 		if (chip->int_reg == 0xFFFFFFFF)
642 			return IRQ_HANDLED;
643 		else
644 			return IRQ_NONE;
645 	}
646 
647 	status = chip->int_reg;
648 
649 	if (dev->check_card_cd) {
650 		if (!(dev->check_card_cd & status)) {
651 			/* card not exist, return TRANS_RESULT_FAIL */
652 			dev->trans_result = TRANS_RESULT_FAIL;
653 			if (dev->done)
654 				complete(dev->done);
655 			goto Exit;
656 		}
657 	}
658 
659 	if (status & (NEED_COMPLETE_INT | DELINK_INT)) {
660 		if (status & (TRANS_FAIL_INT | DELINK_INT)) {
661 			if (status & DELINK_INT)
662 				RTSX_SET_DELINK(chip);
663 			dev->trans_result = TRANS_RESULT_FAIL;
664 			if (dev->done)
665 				complete(dev->done);
666 		} else if (status & TRANS_OK_INT) {
667 			dev->trans_result = TRANS_RESULT_OK;
668 			if (dev->done)
669 				complete(dev->done);
670 		} else if (status & DATA_DONE_INT) {
671 			dev->trans_result = TRANS_NOT_READY;
672 			if (dev->done && (dev->trans_state == STATE_TRANS_SG))
673 				complete(dev->done);
674 		}
675 	}
676 
677 Exit:
678 	spin_unlock(&dev->reg_lock);
679 	return IRQ_HANDLED;
680 }
681 
682 
683 /* Release all our dynamic resources */
rtsx_release_resources(struct rtsx_dev * dev)684 static void rtsx_release_resources(struct rtsx_dev *dev)
685 {
686 	printk(KERN_INFO "-- %s\n", __func__);
687 
688 	/* Tell the control thread to exit.  The SCSI host must
689 	 * already have been removed so it won't try to queue
690 	 * any more commands.
691 	 */
692 	printk(KERN_INFO "-- sending exit command to thread\n");
693 	complete(&dev->cmnd_ready);
694 	if (dev->ctl_thread)
695 		wait_for_completion(&dev->control_exit);
696 	if (dev->polling_thread)
697 		wait_for_completion(&dev->polling_exit);
698 
699 	wait_timeout(200);
700 
701 	if (dev->rtsx_resv_buf) {
702 		dma_free_coherent(&(dev->pci->dev), RTSX_RESV_BUF_LEN,
703 				dev->rtsx_resv_buf, dev->rtsx_resv_buf_addr);
704 		dev->chip->host_cmds_ptr = NULL;
705 		dev->chip->host_sg_tbl_ptr = NULL;
706 	}
707 
708 	if (dev->irq > 0)
709 		free_irq(dev->irq, (void *)dev);
710 	if (dev->chip->msi_en)
711 		pci_disable_msi(dev->pci);
712 	if (dev->remap_addr)
713 		iounmap(dev->remap_addr);
714 
715 	pci_disable_device(dev->pci);
716 	pci_release_regions(dev->pci);
717 
718 	rtsx_release_chip(dev->chip);
719 	kfree(dev->chip);
720 }
721 
722 /* First stage of disconnect processing: stop all commands and remove
723  * the host */
quiesce_and_remove_host(struct rtsx_dev * dev)724 static void quiesce_and_remove_host(struct rtsx_dev *dev)
725 {
726 	struct Scsi_Host *host = rtsx_to_host(dev);
727 	struct rtsx_chip *chip = dev->chip;
728 
729 	/* Prevent new transfers, stop the current command, and
730 	 * interrupt a SCSI-scan or device-reset delay */
731 	mutex_lock(&dev->dev_mutex);
732 	scsi_lock(host);
733 	rtsx_set_stat(chip, RTSX_STAT_DISCONNECT);
734 	scsi_unlock(host);
735 	mutex_unlock(&dev->dev_mutex);
736 	wake_up(&dev->delay_wait);
737 	wait_for_completion(&dev->scanning_done);
738 
739 	/* Wait some time to let other threads exist */
740 	wait_timeout(100);
741 
742 	/* queuecommand won't accept any new commands and the control
743 	 * thread won't execute a previously-queued command.  If there
744 	 * is such a command pending, complete it with an error. */
745 	mutex_lock(&dev->dev_mutex);
746 	if (chip->srb) {
747 		chip->srb->result = DID_NO_CONNECT << 16;
748 		scsi_lock(host);
749 		chip->srb->scsi_done(dev->chip->srb);
750 		chip->srb = NULL;
751 		scsi_unlock(host);
752 	}
753 	mutex_unlock(&dev->dev_mutex);
754 
755 	/* Now we own no commands so it's safe to remove the SCSI host */
756 	scsi_remove_host(host);
757 }
758 
759 /* Second stage of disconnect processing: deallocate all resources */
release_everything(struct rtsx_dev * dev)760 static void release_everything(struct rtsx_dev *dev)
761 {
762 	rtsx_release_resources(dev);
763 
764 	/* Drop our reference to the host; the SCSI core will free it
765 	 * when the refcount becomes 0. */
766 	scsi_host_put(rtsx_to_host(dev));
767 }
768 
769 /* Thread to carry out delayed SCSI-device scanning */
rtsx_scan_thread(void * __dev)770 static int rtsx_scan_thread(void *__dev)
771 {
772 	struct rtsx_dev *dev = (struct rtsx_dev *)__dev;
773 	struct rtsx_chip *chip = dev->chip;
774 
775 	/* Wait for the timeout to expire or for a disconnect */
776 	if (delay_use > 0) {
777 		printk(KERN_INFO "%s: waiting for device "
778 				"to settle before scanning\n", CR_DRIVER_NAME);
779 		wait_event_interruptible_timeout(dev->delay_wait,
780 				rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT),
781 				delay_use * HZ);
782 	}
783 
784 	/* If the device is still connected, perform the scanning */
785 	if (!rtsx_chk_stat(chip, RTSX_STAT_DISCONNECT)) {
786 		scsi_scan_host(rtsx_to_host(dev));
787 		printk(KERN_INFO "%s: device scan complete\n", CR_DRIVER_NAME);
788 
789 		/* Should we unbind if no devices were detected? */
790 	}
791 
792 	complete_and_exit(&dev->scanning_done, 0);
793 }
794 
rtsx_init_options(struct rtsx_chip * chip)795 static void rtsx_init_options(struct rtsx_chip *chip)
796 {
797 	chip->vendor_id = chip->rtsx->pci->vendor;
798 	chip->product_id = chip->rtsx->pci->device;
799 	chip->adma_mode = 1;
800 	chip->lun_mc = 0;
801 	chip->driver_first_load = 1;
802 #ifdef HW_AUTO_SWITCH_SD_BUS
803 	chip->sdio_in_charge = 0;
804 #endif
805 
806 	chip->mspro_formatter_enable = 1;
807 	chip->ignore_sd = 0;
808 	chip->use_hw_setting = 0;
809 	chip->lun_mode = DEFAULT_SINGLE;
810 	chip->auto_delink_en = auto_delink_en;
811 	chip->ss_en = ss_en;
812 	chip->ss_idle_period = ss_interval * 1000;
813 	chip->remote_wakeup_en = 0;
814 	chip->aspm_l0s_l1_en = aspm_l0s_l1_en;
815 	chip->dynamic_aspm = 1;
816 	chip->fpga_sd_sdr104_clk = CLK_200;
817 	chip->fpga_sd_ddr50_clk = CLK_100;
818 	chip->fpga_sd_sdr50_clk = CLK_100;
819 	chip->fpga_sd_hs_clk = CLK_100;
820 	chip->fpga_mmc_52m_clk = CLK_80;
821 	chip->fpga_ms_hg_clk = CLK_80;
822 	chip->fpga_ms_4bit_clk = CLK_80;
823 	chip->fpga_ms_1bit_clk = CLK_40;
824 	chip->asic_sd_sdr104_clk = 203;
825 	chip->asic_sd_sdr50_clk = 98;
826 	chip->asic_sd_ddr50_clk = 98;
827 	chip->asic_sd_hs_clk = 98;
828 	chip->asic_mmc_52m_clk = 98;
829 	chip->asic_ms_hg_clk = 117;
830 	chip->asic_ms_4bit_clk = 78;
831 	chip->asic_ms_1bit_clk = 39;
832 	chip->ssc_depth_sd_sdr104 = SSC_DEPTH_2M;
833 	chip->ssc_depth_sd_sdr50 = SSC_DEPTH_2M;
834 	chip->ssc_depth_sd_ddr50 = SSC_DEPTH_1M;
835 	chip->ssc_depth_sd_hs = SSC_DEPTH_1M;
836 	chip->ssc_depth_mmc_52m = SSC_DEPTH_1M;
837 	chip->ssc_depth_ms_hg = SSC_DEPTH_1M;
838 	chip->ssc_depth_ms_4bit = SSC_DEPTH_512K;
839 	chip->ssc_depth_low_speed = SSC_DEPTH_512K;
840 	chip->ssc_en = 1;
841 	chip->sd_speed_prior = 0x01040203;
842 	chip->sd_current_prior = 0x00010203;
843 	chip->sd_ctl = SD_PUSH_POINT_AUTO |
844 		       SD_SAMPLE_POINT_AUTO |
845 		       SUPPORT_MMC_DDR_MODE;
846 	chip->sd_ddr_tx_phase = 0;
847 	chip->mmc_ddr_tx_phase = 1;
848 	chip->sd_default_tx_phase = 15;
849 	chip->sd_default_rx_phase = 15;
850 	chip->pmos_pwr_on_interval = 200;
851 	chip->sd_voltage_switch_delay = 1000;
852 	chip->ms_power_class_en = 3;
853 
854 	chip->sd_400mA_ocp_thd = 1;
855 	chip->sd_800mA_ocp_thd = 5;
856 	chip->ms_ocp_thd = 2;
857 
858 	chip->card_drive_sel = 0x55;
859 	chip->sd30_drive_sel_1v8 = 0x03;
860 	chip->sd30_drive_sel_3v3 = 0x01;
861 
862 	chip->do_delink_before_power_down = 1;
863 	chip->auto_power_down = 1;
864 	chip->polling_config = 0;
865 
866 	chip->force_clkreq_0 = 1;
867 	chip->ft2_fast_mode = 0;
868 
869 	chip->sdio_retry_cnt = 1;
870 
871 	chip->xd_timeout = 2000;
872 	chip->sd_timeout = 10000;
873 	chip->ms_timeout = 2000;
874 	chip->mspro_timeout = 15000;
875 
876 	chip->power_down_in_ss = 1;
877 
878 	chip->sdr104_en = 1;
879 	chip->sdr50_en = 1;
880 	chip->ddr50_en = 1;
881 
882 	chip->delink_stage1_step = 100;
883 	chip->delink_stage2_step = 40;
884 	chip->delink_stage3_step = 20;
885 
886 	chip->auto_delink_in_L1 = 1;
887 	chip->blink_led = 1;
888 	chip->msi_en = msi_en;
889 	chip->hp_watch_bios_hotplug = 0;
890 	chip->max_payload = 0;
891 	chip->phy_voltage = 0;
892 
893 	chip->support_ms_8bit = 1;
894 	chip->s3_pwr_off_delay = 1000;
895 }
896 
rtsx_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)897 static int __devinit rtsx_probe(struct pci_dev *pci,
898 				const struct pci_device_id *pci_id)
899 {
900 	struct Scsi_Host *host;
901 	struct rtsx_dev *dev;
902 	int err = 0;
903 	struct task_struct *th;
904 
905 	RTSX_DEBUGP("Realtek PCI-E card reader detected\n");
906 
907 	err = pci_enable_device(pci);
908 	if (err < 0) {
909 		printk(KERN_ERR "PCI enable device failed!\n");
910 		return err;
911 	}
912 
913 	err = pci_request_regions(pci, CR_DRIVER_NAME);
914 	if (err < 0) {
915 		printk(KERN_ERR "PCI request regions for %s failed!\n",
916 		       CR_DRIVER_NAME);
917 		pci_disable_device(pci);
918 		return err;
919 	}
920 
921 	/*
922 	 * Ask the SCSI layer to allocate a host structure, with extra
923 	 * space at the end for our private rtsx_dev structure.
924 	 */
925 	host = scsi_host_alloc(&rtsx_host_template, sizeof(*dev));
926 	if (!host) {
927 		printk(KERN_ERR "Unable to allocate the scsi host\n");
928 		pci_release_regions(pci);
929 		pci_disable_device(pci);
930 		return -ENOMEM;
931 	}
932 
933 	dev = host_to_rtsx(host);
934 	memset(dev, 0, sizeof(struct rtsx_dev));
935 
936 	dev->chip = kzalloc(sizeof(struct rtsx_chip), GFP_KERNEL);
937 	if (dev->chip == NULL)
938 		goto errout;
939 
940 	spin_lock_init(&dev->reg_lock);
941 	mutex_init(&(dev->dev_mutex));
942 	init_completion(&dev->cmnd_ready);
943 	init_completion(&dev->control_exit);
944 	init_completion(&dev->polling_exit);
945 	init_completion(&(dev->notify));
946 	init_completion(&dev->scanning_done);
947 	init_waitqueue_head(&dev->delay_wait);
948 
949 	dev->pci = pci;
950 	dev->irq = -1;
951 
952 	printk(KERN_INFO "Resource length: 0x%x\n",
953 	       (unsigned int)pci_resource_len(pci, 0));
954 	dev->addr = pci_resource_start(pci, 0);
955 	dev->remap_addr = ioremap_nocache(dev->addr, pci_resource_len(pci, 0));
956 	if (dev->remap_addr == NULL) {
957 		printk(KERN_ERR "ioremap error\n");
958 		err = -ENXIO;
959 		goto errout;
960 	}
961 
962 	/*
963 	 * Using "unsigned long" cast here to eliminate gcc warning in
964 	 * 64-bit system
965 	 */
966 	printk(KERN_INFO "Original address: 0x%lx, remapped address: 0x%lx\n",
967 	       (unsigned long)(dev->addr), (unsigned long)(dev->remap_addr));
968 
969 	dev->rtsx_resv_buf = dma_alloc_coherent(&(pci->dev), RTSX_RESV_BUF_LEN,
970 			&(dev->rtsx_resv_buf_addr), GFP_KERNEL);
971 	if (dev->rtsx_resv_buf == NULL) {
972 		printk(KERN_ERR "alloc dma buffer fail\n");
973 		err = -ENXIO;
974 		goto errout;
975 	}
976 	dev->chip->host_cmds_ptr = dev->rtsx_resv_buf;
977 	dev->chip->host_cmds_addr = dev->rtsx_resv_buf_addr;
978 	dev->chip->host_sg_tbl_ptr = dev->rtsx_resv_buf + HOST_CMDS_BUF_LEN;
979 	dev->chip->host_sg_tbl_addr = dev->rtsx_resv_buf_addr +
980 				      HOST_CMDS_BUF_LEN;
981 
982 	dev->chip->rtsx = dev;
983 
984 	rtsx_init_options(dev->chip);
985 
986 	printk(KERN_INFO "pci->irq = %d\n", pci->irq);
987 
988 	if (dev->chip->msi_en) {
989 		if (pci_enable_msi(pci) < 0)
990 			dev->chip->msi_en = 0;
991 	}
992 
993 	if (rtsx_acquire_irq(dev) < 0) {
994 		err = -EBUSY;
995 		goto errout;
996 	}
997 
998 	pci_set_master(pci);
999 	synchronize_irq(dev->irq);
1000 
1001 	rtsx_init_chip(dev->chip);
1002 
1003 	/* set the supported max_lun and max_id for the scsi host
1004 	 * NOTE: the minimal value of max_id is 1 */
1005 	host->max_id = 1;
1006 	host->max_lun = dev->chip->max_lun;
1007 
1008 	/* Start up our control thread */
1009 	th = kthread_run(rtsx_control_thread, dev, CR_DRIVER_NAME);
1010 	if (IS_ERR(th)) {
1011 		printk(KERN_ERR "Unable to start control thread\n");
1012 		err = PTR_ERR(th);
1013 		goto errout;
1014 	}
1015 	dev->ctl_thread = th;
1016 
1017 	err = scsi_add_host(host, &pci->dev);
1018 	if (err) {
1019 		printk(KERN_ERR "Unable to add the scsi host\n");
1020 		goto errout;
1021 	}
1022 
1023 	/* Start up the thread for delayed SCSI-device scanning */
1024 	th = kthread_create(rtsx_scan_thread, dev, "rtsx-scan");
1025 	if (IS_ERR(th)) {
1026 		printk(KERN_ERR "Unable to start the device-scanning thread\n");
1027 		complete(&dev->scanning_done);
1028 		quiesce_and_remove_host(dev);
1029 		err = PTR_ERR(th);
1030 		goto errout;
1031 	}
1032 
1033 	wake_up_process(th);
1034 
1035 	/* Start up the thread for polling thread */
1036 	th = kthread_run(rtsx_polling_thread, dev, "rtsx-polling");
1037 	if (IS_ERR(th)) {
1038 		printk(KERN_ERR "Unable to start the device-polling thread\n");
1039 		quiesce_and_remove_host(dev);
1040 		err = PTR_ERR(th);
1041 		goto errout;
1042 	}
1043 	dev->polling_thread = th;
1044 
1045 	pci_set_drvdata(pci, dev);
1046 
1047 	return 0;
1048 
1049 	/* We come here if there are any problems */
1050 errout:
1051 	printk(KERN_ERR "rtsx_probe() failed\n");
1052 	release_everything(dev);
1053 
1054 	return err;
1055 }
1056 
1057 
rtsx_remove(struct pci_dev * pci)1058 static void __devexit rtsx_remove(struct pci_dev *pci)
1059 {
1060 	struct rtsx_dev *dev = (struct rtsx_dev *)pci_get_drvdata(pci);
1061 
1062 	printk(KERN_INFO "rtsx_remove() called\n");
1063 
1064 	quiesce_and_remove_host(dev);
1065 	release_everything(dev);
1066 
1067 	pci_set_drvdata(pci, NULL);
1068 }
1069 
1070 /* PCI IDs */
1071 static DEFINE_PCI_DEVICE_TABLE(rtsx_ids) = {
1072 	{ PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5208), PCI_CLASS_OTHERS << 16, 0xFF0000 },
1073 	{ PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5209), PCI_CLASS_OTHERS << 16, 0xFF0000 },
1074 	{ PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5288), PCI_CLASS_OTHERS << 16, 0xFF0000 },
1075 	{ 0, },
1076 };
1077 
1078 MODULE_DEVICE_TABLE(pci, rtsx_ids);
1079 
1080 /* pci_driver definition */
1081 static struct pci_driver driver = {
1082 	.name = CR_DRIVER_NAME,
1083 	.id_table = rtsx_ids,
1084 	.probe = rtsx_probe,
1085 	.remove = __devexit_p(rtsx_remove),
1086 #ifdef CONFIG_PM
1087 	.suspend = rtsx_suspend,
1088 	.resume = rtsx_resume,
1089 #endif
1090 	.shutdown = rtsx_shutdown,
1091 };
1092 
rtsx_init(void)1093 static int __init rtsx_init(void)
1094 {
1095 	printk(KERN_INFO "Initializing Realtek PCIE storage driver...\n");
1096 
1097 	return pci_register_driver(&driver);
1098 }
1099 
rtsx_exit(void)1100 static void __exit rtsx_exit(void)
1101 {
1102 	printk(KERN_INFO "rtsx_exit() called\n");
1103 
1104 	pci_unregister_driver(&driver);
1105 
1106 	printk(KERN_INFO "%s module exit\n", CR_DRIVER_NAME);
1107 }
1108 
1109 module_init(rtsx_init)
1110 module_exit(rtsx_exit)
1111 
1112