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