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