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