rtsx.c 26 KB

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