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