5 * Authors: Joshua Morris <josh.h.morris@us.ibm.com>
6 * Philip Kelleher <pjk1939@linux.vnet.ibm.com>
8 * (C) Copyright 2013 IBM Corporation
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/interrupt.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
30 #include <linux/reboot.h>
31 #include <linux/slab.h>
32 #include <linux/bitops.h>
33 #include <linux/delay.h>
34 #include <linux/debugfs.h>
35 #include <linux/seq_file.h>
37 #include <linux/genhd.h>
38 #include <linux/idr.h>
40 #include "rsxx_priv.h"
44 #define SYNC_START_TIMEOUT (10 * 60) /* 10 minutes */
46 MODULE_DESCRIPTION("IBM Flash Adapter 900GB Full Height Device Driver");
47 MODULE_AUTHOR("Joshua Morris/Philip Kelleher, IBM");
48 MODULE_LICENSE("GPL");
49 MODULE_VERSION(DRIVER_VERSION
);
51 static unsigned int force_legacy
= NO_LEGACY
;
52 module_param(force_legacy
, uint
, 0444);
53 MODULE_PARM_DESC(force_legacy
, "Force the use of legacy type PCI interrupts");
55 static unsigned int sync_start
= 1;
56 module_param(sync_start
, uint
, 0444);
57 MODULE_PARM_DESC(sync_start
, "On by Default: Driver load will not complete "
58 "until the card startup has completed.");
60 static DEFINE_IDA(rsxx_disk_ida
);
61 static DEFINE_SPINLOCK(rsxx_ida_lock
);
63 /* --------------------Debugfs Setup ------------------- */
65 static int rsxx_attr_pci_regs_show(struct seq_file
*m
, void *p
)
67 struct rsxx_cardinfo
*card
= m
->private;
69 seq_printf(m
, "HWID 0x%08x\n",
70 ioread32(card
->regmap
+ HWID
));
71 seq_printf(m
, "SCRATCH 0x%08x\n",
72 ioread32(card
->regmap
+ SCRATCH
));
73 seq_printf(m
, "IER 0x%08x\n",
74 ioread32(card
->regmap
+ IER
));
75 seq_printf(m
, "IPR 0x%08x\n",
76 ioread32(card
->regmap
+ IPR
));
77 seq_printf(m
, "CREG_CMD 0x%08x\n",
78 ioread32(card
->regmap
+ CREG_CMD
));
79 seq_printf(m
, "CREG_ADD 0x%08x\n",
80 ioread32(card
->regmap
+ CREG_ADD
));
81 seq_printf(m
, "CREG_CNT 0x%08x\n",
82 ioread32(card
->regmap
+ CREG_CNT
));
83 seq_printf(m
, "CREG_STAT 0x%08x\n",
84 ioread32(card
->regmap
+ CREG_STAT
));
85 seq_printf(m
, "CREG_DATA0 0x%08x\n",
86 ioread32(card
->regmap
+ CREG_DATA0
));
87 seq_printf(m
, "CREG_DATA1 0x%08x\n",
88 ioread32(card
->regmap
+ CREG_DATA1
));
89 seq_printf(m
, "CREG_DATA2 0x%08x\n",
90 ioread32(card
->regmap
+ CREG_DATA2
));
91 seq_printf(m
, "CREG_DATA3 0x%08x\n",
92 ioread32(card
->regmap
+ CREG_DATA3
));
93 seq_printf(m
, "CREG_DATA4 0x%08x\n",
94 ioread32(card
->regmap
+ CREG_DATA4
));
95 seq_printf(m
, "CREG_DATA5 0x%08x\n",
96 ioread32(card
->regmap
+ CREG_DATA5
));
97 seq_printf(m
, "CREG_DATA6 0x%08x\n",
98 ioread32(card
->regmap
+ CREG_DATA6
));
99 seq_printf(m
, "CREG_DATA7 0x%08x\n",
100 ioread32(card
->regmap
+ CREG_DATA7
));
101 seq_printf(m
, "INTR_COAL 0x%08x\n",
102 ioread32(card
->regmap
+ INTR_COAL
));
103 seq_printf(m
, "HW_ERROR 0x%08x\n",
104 ioread32(card
->regmap
+ HW_ERROR
));
105 seq_printf(m
, "DEBUG0 0x%08x\n",
106 ioread32(card
->regmap
+ PCI_DEBUG0
));
107 seq_printf(m
, "DEBUG1 0x%08x\n",
108 ioread32(card
->regmap
+ PCI_DEBUG1
));
109 seq_printf(m
, "DEBUG2 0x%08x\n",
110 ioread32(card
->regmap
+ PCI_DEBUG2
));
111 seq_printf(m
, "DEBUG3 0x%08x\n",
112 ioread32(card
->regmap
+ PCI_DEBUG3
));
113 seq_printf(m
, "DEBUG4 0x%08x\n",
114 ioread32(card
->regmap
+ PCI_DEBUG4
));
115 seq_printf(m
, "DEBUG5 0x%08x\n",
116 ioread32(card
->regmap
+ PCI_DEBUG5
));
117 seq_printf(m
, "DEBUG6 0x%08x\n",
118 ioread32(card
->regmap
+ PCI_DEBUG6
));
119 seq_printf(m
, "DEBUG7 0x%08x\n",
120 ioread32(card
->regmap
+ PCI_DEBUG7
));
121 seq_printf(m
, "RECONFIG 0x%08x\n",
122 ioread32(card
->regmap
+ PCI_RECONFIG
));
127 static int rsxx_attr_stats_show(struct seq_file
*m
, void *p
)
129 struct rsxx_cardinfo
*card
= m
->private;
132 for (i
= 0; i
< card
->n_targets
; i
++) {
133 seq_printf(m
, "Ctrl %d CRC Errors = %d\n",
134 i
, card
->ctrl
[i
].stats
.crc_errors
);
135 seq_printf(m
, "Ctrl %d Hard Errors = %d\n",
136 i
, card
->ctrl
[i
].stats
.hard_errors
);
137 seq_printf(m
, "Ctrl %d Soft Errors = %d\n",
138 i
, card
->ctrl
[i
].stats
.soft_errors
);
139 seq_printf(m
, "Ctrl %d Writes Issued = %d\n",
140 i
, card
->ctrl
[i
].stats
.writes_issued
);
141 seq_printf(m
, "Ctrl %d Writes Failed = %d\n",
142 i
, card
->ctrl
[i
].stats
.writes_failed
);
143 seq_printf(m
, "Ctrl %d Reads Issued = %d\n",
144 i
, card
->ctrl
[i
].stats
.reads_issued
);
145 seq_printf(m
, "Ctrl %d Reads Failed = %d\n",
146 i
, card
->ctrl
[i
].stats
.reads_failed
);
147 seq_printf(m
, "Ctrl %d Reads Retried = %d\n",
148 i
, card
->ctrl
[i
].stats
.reads_retried
);
149 seq_printf(m
, "Ctrl %d Discards Issued = %d\n",
150 i
, card
->ctrl
[i
].stats
.discards_issued
);
151 seq_printf(m
, "Ctrl %d Discards Failed = %d\n",
152 i
, card
->ctrl
[i
].stats
.discards_failed
);
153 seq_printf(m
, "Ctrl %d DMA SW Errors = %d\n",
154 i
, card
->ctrl
[i
].stats
.dma_sw_err
);
155 seq_printf(m
, "Ctrl %d DMA HW Faults = %d\n",
156 i
, card
->ctrl
[i
].stats
.dma_hw_fault
);
157 seq_printf(m
, "Ctrl %d DMAs Cancelled = %d\n",
158 i
, card
->ctrl
[i
].stats
.dma_cancelled
);
159 seq_printf(m
, "Ctrl %d SW Queue Depth = %d\n",
160 i
, card
->ctrl
[i
].stats
.sw_q_depth
);
161 seq_printf(m
, "Ctrl %d HW Queue Depth = %d\n",
162 i
, atomic_read(&card
->ctrl
[i
].stats
.hw_q_depth
));
168 static int rsxx_attr_stats_open(struct inode
*inode
, struct file
*file
)
170 return single_open(file
, rsxx_attr_stats_show
, inode
->i_private
);
173 static int rsxx_attr_pci_regs_open(struct inode
*inode
, struct file
*file
)
175 return single_open(file
, rsxx_attr_pci_regs_show
, inode
->i_private
);
178 static ssize_t
rsxx_cram_read(struct file
*fp
, char __user
*ubuf
,
179 size_t cnt
, loff_t
*ppos
)
181 struct rsxx_cardinfo
*card
= file_inode(fp
)->i_private
;
185 buf
= kzalloc(cnt
, GFP_KERNEL
);
189 st
= rsxx_creg_read(card
, CREG_ADD_CRAM
+ (u32
)*ppos
, cnt
, buf
, 1);
191 st
= copy_to_user(ubuf
, buf
, cnt
);
199 static ssize_t
rsxx_cram_write(struct file
*fp
, const char __user
*ubuf
,
200 size_t cnt
, loff_t
*ppos
)
202 struct rsxx_cardinfo
*card
= file_inode(fp
)->i_private
;
206 buf
= memdup_user(ubuf
, cnt
);
210 st
= rsxx_creg_write(card
, CREG_ADD_CRAM
+ (u32
)*ppos
, cnt
, buf
, 1);
218 static const struct file_operations debugfs_cram_fops
= {
219 .owner
= THIS_MODULE
,
220 .read
= rsxx_cram_read
,
221 .write
= rsxx_cram_write
,
224 static const struct file_operations debugfs_stats_fops
= {
225 .owner
= THIS_MODULE
,
226 .open
= rsxx_attr_stats_open
,
229 .release
= single_release
,
232 static const struct file_operations debugfs_pci_regs_fops
= {
233 .owner
= THIS_MODULE
,
234 .open
= rsxx_attr_pci_regs_open
,
237 .release
= single_release
,
240 static void rsxx_debugfs_dev_new(struct rsxx_cardinfo
*card
)
242 struct dentry
*debugfs_stats
;
243 struct dentry
*debugfs_pci_regs
;
244 struct dentry
*debugfs_cram
;
246 card
->debugfs_dir
= debugfs_create_dir(card
->gendisk
->disk_name
, NULL
);
247 if (IS_ERR_OR_NULL(card
->debugfs_dir
))
248 goto failed_debugfs_dir
;
250 debugfs_stats
= debugfs_create_file("stats", S_IRUGO
,
251 card
->debugfs_dir
, card
,
252 &debugfs_stats_fops
);
253 if (IS_ERR_OR_NULL(debugfs_stats
))
254 goto failed_debugfs_stats
;
256 debugfs_pci_regs
= debugfs_create_file("pci_regs", S_IRUGO
,
257 card
->debugfs_dir
, card
,
258 &debugfs_pci_regs_fops
);
259 if (IS_ERR_OR_NULL(debugfs_pci_regs
))
260 goto failed_debugfs_pci_regs
;
262 debugfs_cram
= debugfs_create_file("cram", S_IRUGO
| S_IWUSR
,
263 card
->debugfs_dir
, card
,
265 if (IS_ERR_OR_NULL(debugfs_cram
))
266 goto failed_debugfs_cram
;
270 debugfs_remove(debugfs_pci_regs
);
271 failed_debugfs_pci_regs
:
272 debugfs_remove(debugfs_stats
);
273 failed_debugfs_stats
:
274 debugfs_remove(card
->debugfs_dir
);
276 card
->debugfs_dir
= NULL
;
279 /*----------------- Interrupt Control & Handling -------------------*/
281 static void rsxx_mask_interrupts(struct rsxx_cardinfo
*card
)
287 static void __enable_intr(unsigned int *mask
, unsigned int intr
)
292 static void __disable_intr(unsigned int *mask
, unsigned int intr
)
298 * NOTE: Disabling the IER will disable the hardware interrupt.
299 * Disabling the ISR will disable the software handling of the ISR bit.
301 * Enable/Disable interrupt functions assume the card->irq_lock
302 * is held by the caller.
304 void rsxx_enable_ier(struct rsxx_cardinfo
*card
, unsigned int intr
)
306 if (unlikely(card
->halt
) ||
307 unlikely(card
->eeh_state
))
310 __enable_intr(&card
->ier_mask
, intr
);
311 iowrite32(card
->ier_mask
, card
->regmap
+ IER
);
314 void rsxx_disable_ier(struct rsxx_cardinfo
*card
, unsigned int intr
)
316 if (unlikely(card
->eeh_state
))
319 __disable_intr(&card
->ier_mask
, intr
);
320 iowrite32(card
->ier_mask
, card
->regmap
+ IER
);
323 void rsxx_enable_ier_and_isr(struct rsxx_cardinfo
*card
,
326 if (unlikely(card
->halt
) ||
327 unlikely(card
->eeh_state
))
330 __enable_intr(&card
->isr_mask
, intr
);
331 __enable_intr(&card
->ier_mask
, intr
);
332 iowrite32(card
->ier_mask
, card
->regmap
+ IER
);
334 void rsxx_disable_ier_and_isr(struct rsxx_cardinfo
*card
,
337 if (unlikely(card
->eeh_state
))
340 __disable_intr(&card
->isr_mask
, intr
);
341 __disable_intr(&card
->ier_mask
, intr
);
342 iowrite32(card
->ier_mask
, card
->regmap
+ IER
);
345 static irqreturn_t
rsxx_isr(int irq
, void *pdata
)
347 struct rsxx_cardinfo
*card
= pdata
;
353 spin_lock(&card
->irq_lock
);
358 if (unlikely(card
->eeh_state
))
361 isr
= ioread32(card
->regmap
+ ISR
);
362 if (isr
== 0xffffffff) {
364 * A few systems seem to have an intermittent issue
365 * where PCI reads return all Fs, but retrying the read
366 * a little later will return as expected.
368 dev_info(CARD_TO_DEV(card
),
369 "ISR = 0xFFFFFFFF, retrying later\n");
373 isr
&= card
->isr_mask
;
377 for (i
= 0; i
< card
->n_targets
; i
++) {
378 if (isr
& CR_INTR_DMA(i
)) {
379 if (card
->ier_mask
& CR_INTR_DMA(i
)) {
380 rsxx_disable_ier(card
, CR_INTR_DMA(i
));
383 queue_work(card
->ctrl
[i
].done_wq
,
384 &card
->ctrl
[i
].dma_done_work
);
389 if (isr
& CR_INTR_CREG
) {
390 queue_work(card
->creg_ctrl
.creg_wq
,
391 &card
->creg_ctrl
.done_work
);
395 if (isr
& CR_INTR_EVENT
) {
396 queue_work(card
->event_wq
, &card
->event_work
);
397 rsxx_disable_ier_and_isr(card
, CR_INTR_EVENT
);
400 } while (reread_isr
);
402 spin_unlock(&card
->irq_lock
);
404 return handled
? IRQ_HANDLED
: IRQ_NONE
;
407 /*----------------- Card Event Handler -------------------*/
408 static const char * const rsxx_card_state_to_str(unsigned int state
)
410 static const char * const state_strings
[] = {
411 "Unknown", "Shutdown", "Starting", "Formatting",
412 "Uninitialized", "Good", "Shutting Down",
413 "Fault", "Read Only Fault", "dStroying"
416 return state_strings
[ffs(state
)];
419 static void card_state_change(struct rsxx_cardinfo
*card
,
420 unsigned int new_state
)
424 dev_info(CARD_TO_DEV(card
),
425 "card state change detected.(%s -> %s)\n",
426 rsxx_card_state_to_str(card
->state
),
427 rsxx_card_state_to_str(new_state
));
429 card
->state
= new_state
;
431 /* Don't attach DMA interfaces if the card has an invalid config */
432 if (!card
->config_valid
)
436 case CARD_STATE_RD_ONLY_FAULT
:
437 dev_crit(CARD_TO_DEV(card
),
438 "Hardware has entered read-only mode!\n");
440 * Fall through so the DMA devices can be attached and
441 * the user can attempt to pull off their data.
443 case CARD_STATE_GOOD
:
444 st
= rsxx_get_card_size8(card
, &card
->size8
);
446 dev_err(CARD_TO_DEV(card
),
447 "Failed attaching DMA devices\n");
449 if (card
->config_valid
)
450 set_capacity(card
->gendisk
, card
->size8
>> 9);
453 case CARD_STATE_FAULT
:
454 dev_crit(CARD_TO_DEV(card
),
455 "Hardware Fault reported!\n");
458 /* Everything else, detach DMA interface if it's attached. */
459 case CARD_STATE_SHUTDOWN
:
460 case CARD_STATE_STARTING
:
461 case CARD_STATE_FORMATTING
:
462 case CARD_STATE_UNINITIALIZED
:
463 case CARD_STATE_SHUTTING_DOWN
:
465 * dStroy is a term coined by marketing to represent the low level
468 case CARD_STATE_DSTROYING
:
469 set_capacity(card
->gendisk
, 0);
474 static void card_event_handler(struct work_struct
*work
)
476 struct rsxx_cardinfo
*card
;
481 card
= container_of(work
, struct rsxx_cardinfo
, event_work
);
483 if (unlikely(card
->halt
))
487 * Enable the interrupt now to avoid any weird race conditions where a
488 * state change might occur while rsxx_get_card_state() is
489 * processing a returned creg cmd.
491 spin_lock_irqsave(&card
->irq_lock
, flags
);
492 rsxx_enable_ier_and_isr(card
, CR_INTR_EVENT
);
493 spin_unlock_irqrestore(&card
->irq_lock
, flags
);
495 st
= rsxx_get_card_state(card
, &state
);
497 dev_info(CARD_TO_DEV(card
),
498 "Failed reading state after event.\n");
502 if (card
->state
!= state
)
503 card_state_change(card
, state
);
505 if (card
->creg_ctrl
.creg_stats
.stat
& CREG_STAT_LOG_PENDING
)
506 rsxx_read_hw_log(card
);
509 /*----------------- Card Operations -------------------*/
510 static int card_shutdown(struct rsxx_cardinfo
*card
)
514 const int timeout
= msecs_to_jiffies(120000);
517 /* We can't issue a shutdown if the card is in a transition state */
520 st
= rsxx_get_card_state(card
, &state
);
523 } while (state
== CARD_STATE_STARTING
&&
524 (jiffies
- start
< timeout
));
526 if (state
== CARD_STATE_STARTING
)
529 /* Only issue a shutdown if we need to */
530 if ((state
!= CARD_STATE_SHUTTING_DOWN
) &&
531 (state
!= CARD_STATE_SHUTDOWN
)) {
532 st
= rsxx_issue_card_cmd(card
, CARD_CMD_SHUTDOWN
);
539 st
= rsxx_get_card_state(card
, &state
);
542 } while (state
!= CARD_STATE_SHUTDOWN
&&
543 (jiffies
- start
< timeout
));
545 if (state
!= CARD_STATE_SHUTDOWN
)
551 static int rsxx_eeh_frozen(struct pci_dev
*dev
)
553 struct rsxx_cardinfo
*card
= pci_get_drvdata(dev
);
557 dev_warn(&dev
->dev
, "IBM Flash Adapter PCI: preparing for slot reset.\n");
560 rsxx_mask_interrupts(card
);
563 * We need to guarantee that the write for eeh_state and masking
564 * interrupts does not become reordered. This will prevent a possible
565 * race condition with the EEH code.
569 pci_disable_device(dev
);
571 st
= rsxx_eeh_save_issued_dmas(card
);
575 rsxx_eeh_save_issued_creg(card
);
577 for (i
= 0; i
< card
->n_targets
; i
++) {
578 if (card
->ctrl
[i
].status
.buf
)
579 pci_free_consistent(card
->dev
, STATUS_BUFFER_SIZE8
,
580 card
->ctrl
[i
].status
.buf
,
581 card
->ctrl
[i
].status
.dma_addr
);
582 if (card
->ctrl
[i
].cmd
.buf
)
583 pci_free_consistent(card
->dev
, COMMAND_BUFFER_SIZE8
,
584 card
->ctrl
[i
].cmd
.buf
,
585 card
->ctrl
[i
].cmd
.dma_addr
);
591 static void rsxx_eeh_failure(struct pci_dev
*dev
)
593 struct rsxx_cardinfo
*card
= pci_get_drvdata(dev
);
597 dev_err(&dev
->dev
, "IBM Flash Adapter PCI: disabling failed card.\n");
602 for (i
= 0; i
< card
->n_targets
; i
++) {
603 spin_lock_bh(&card
->ctrl
[i
].queue_lock
);
604 cnt
= rsxx_cleanup_dma_queue(&card
->ctrl
[i
],
605 &card
->ctrl
[i
].queue
,
607 spin_unlock_bh(&card
->ctrl
[i
].queue_lock
);
609 cnt
+= rsxx_dma_cancel(&card
->ctrl
[i
]);
612 dev_info(CARD_TO_DEV(card
),
613 "Freed %d queued DMAs on channel %d\n",
614 cnt
, card
->ctrl
[i
].id
);
618 static int rsxx_eeh_fifo_flush_poll(struct rsxx_cardinfo
*card
)
623 /* We need to wait for the hardware to reset */
624 while (iter
++ < 10) {
625 status
= ioread32(card
->regmap
+ PCI_RECONFIG
);
627 if (status
& RSXX_FLUSH_BUSY
) {
632 if (status
& RSXX_FLUSH_TIMEOUT
)
633 dev_warn(CARD_TO_DEV(card
), "HW: flash controller timeout\n");
637 /* Hardware failed resetting itself. */
641 static pci_ers_result_t
rsxx_error_detected(struct pci_dev
*dev
,
642 enum pci_channel_state error
)
646 if (dev
->revision
< RSXX_EEH_SUPPORT
)
647 return PCI_ERS_RESULT_NONE
;
649 if (error
== pci_channel_io_perm_failure
) {
650 rsxx_eeh_failure(dev
);
651 return PCI_ERS_RESULT_DISCONNECT
;
654 st
= rsxx_eeh_frozen(dev
);
656 dev_err(&dev
->dev
, "Slot reset setup failed\n");
657 rsxx_eeh_failure(dev
);
658 return PCI_ERS_RESULT_DISCONNECT
;
661 return PCI_ERS_RESULT_NEED_RESET
;
664 static pci_ers_result_t
rsxx_slot_reset(struct pci_dev
*dev
)
666 struct rsxx_cardinfo
*card
= pci_get_drvdata(dev
);
672 "IBM Flash Adapter PCI: recovering from slot reset.\n");
674 st
= pci_enable_device(dev
);
676 goto failed_hw_setup
;
680 st
= rsxx_eeh_fifo_flush_poll(card
);
682 goto failed_hw_setup
;
684 rsxx_dma_queue_reset(card
);
686 for (i
= 0; i
< card
->n_targets
; i
++) {
687 st
= rsxx_hw_buffers_init(dev
, &card
->ctrl
[i
]);
689 goto failed_hw_buffers_init
;
692 if (card
->config_valid
)
693 rsxx_dma_configure(card
);
695 /* Clears the ISR register from spurious interrupts */
696 st
= ioread32(card
->regmap
+ ISR
);
700 spin_lock_irqsave(&card
->irq_lock
, flags
);
701 if (card
->n_targets
& RSXX_MAX_TARGETS
)
702 rsxx_enable_ier_and_isr(card
, CR_INTR_ALL_G
);
704 rsxx_enable_ier_and_isr(card
, CR_INTR_ALL_C
);
705 spin_unlock_irqrestore(&card
->irq_lock
, flags
);
707 rsxx_kick_creg_queue(card
);
709 for (i
= 0; i
< card
->n_targets
; i
++) {
710 spin_lock(&card
->ctrl
[i
].queue_lock
);
711 if (list_empty(&card
->ctrl
[i
].queue
)) {
712 spin_unlock(&card
->ctrl
[i
].queue_lock
);
715 spin_unlock(&card
->ctrl
[i
].queue_lock
);
717 queue_work(card
->ctrl
[i
].issue_wq
,
718 &card
->ctrl
[i
].issue_dma_work
);
721 dev_info(&dev
->dev
, "IBM Flash Adapter PCI: recovery complete.\n");
723 return PCI_ERS_RESULT_RECOVERED
;
725 failed_hw_buffers_init
:
726 for (i
= 0; i
< card
->n_targets
; i
++) {
727 if (card
->ctrl
[i
].status
.buf
)
728 pci_free_consistent(card
->dev
,
730 card
->ctrl
[i
].status
.buf
,
731 card
->ctrl
[i
].status
.dma_addr
);
732 if (card
->ctrl
[i
].cmd
.buf
)
733 pci_free_consistent(card
->dev
,
734 COMMAND_BUFFER_SIZE8
,
735 card
->ctrl
[i
].cmd
.buf
,
736 card
->ctrl
[i
].cmd
.dma_addr
);
739 rsxx_eeh_failure(dev
);
740 return PCI_ERS_RESULT_DISCONNECT
;
744 /*----------------- Driver Initialization & Setup -------------------*/
745 /* Returns: 0 if the driver is compatible with the device
746 -1 if the driver is NOT compatible with the device */
747 static int rsxx_compatibility_check(struct rsxx_cardinfo
*card
)
749 unsigned char pci_rev
;
751 pci_read_config_byte(card
->dev
, PCI_REVISION_ID
, &pci_rev
);
753 if (pci_rev
> RS70_PCI_REV_SUPPORTED
)
758 static int rsxx_pci_probe(struct pci_dev
*dev
,
759 const struct pci_device_id
*id
)
761 struct rsxx_cardinfo
*card
;
763 unsigned int sync_timeout
;
765 dev_info(&dev
->dev
, "PCI-Flash SSD discovered\n");
767 card
= kzalloc(sizeof(*card
), GFP_KERNEL
);
772 pci_set_drvdata(dev
, card
);
775 if (!ida_pre_get(&rsxx_disk_ida
, GFP_KERNEL
)) {
780 spin_lock(&rsxx_ida_lock
);
781 st
= ida_get_new(&rsxx_disk_ida
, &card
->disk_id
);
782 spin_unlock(&rsxx_ida_lock
);
783 } while (st
== -EAGAIN
);
788 st
= pci_enable_device(dev
);
793 pci_set_dma_max_seg_size(dev
, RSXX_HW_BLK_SIZE
);
795 st
= pci_set_dma_mask(dev
, DMA_BIT_MASK(64));
797 dev_err(CARD_TO_DEV(card
),
798 "No usable DMA configuration,aborting\n");
799 goto failed_dma_mask
;
802 st
= pci_request_regions(dev
, DRIVER_NAME
);
804 dev_err(CARD_TO_DEV(card
),
805 "Failed to request memory region\n");
806 goto failed_request_regions
;
809 if (pci_resource_len(dev
, 0) == 0) {
810 dev_err(CARD_TO_DEV(card
), "BAR0 has length 0!\n");
815 card
->regmap
= pci_iomap(dev
, 0, 0);
817 dev_err(CARD_TO_DEV(card
), "Failed to map BAR0\n");
822 spin_lock_init(&card
->irq_lock
);
826 spin_lock_irq(&card
->irq_lock
);
827 rsxx_disable_ier_and_isr(card
, CR_INTR_ALL
);
828 spin_unlock_irq(&card
->irq_lock
);
831 st
= pci_enable_msi(dev
);
833 dev_warn(CARD_TO_DEV(card
),
834 "Failed to enable MSI\n");
837 st
= request_irq(dev
->irq
, rsxx_isr
, IRQF_SHARED
,
840 dev_err(CARD_TO_DEV(card
),
841 "Failed requesting IRQ%d\n", dev
->irq
);
845 /************* Setup Processor Command Interface *************/
846 st
= rsxx_creg_setup(card
);
848 dev_err(CARD_TO_DEV(card
), "Failed to setup creg interface.\n");
849 goto failed_creg_setup
;
852 spin_lock_irq(&card
->irq_lock
);
853 rsxx_enable_ier_and_isr(card
, CR_INTR_CREG
);
854 spin_unlock_irq(&card
->irq_lock
);
856 st
= rsxx_compatibility_check(card
);
858 dev_warn(CARD_TO_DEV(card
),
859 "Incompatible driver detected. Please update the driver.\n");
861 goto failed_compatiblity_check
;
864 /************* Load Card Config *************/
865 st
= rsxx_load_config(card
);
867 dev_err(CARD_TO_DEV(card
),
868 "Failed loading card config\n");
870 /************* Setup DMA Engine *************/
871 st
= rsxx_get_num_targets(card
, &card
->n_targets
);
873 dev_info(CARD_TO_DEV(card
),
874 "Failed reading the number of DMA targets\n");
876 card
->ctrl
= kzalloc(card
->n_targets
* sizeof(*card
->ctrl
), GFP_KERNEL
);
879 goto failed_dma_setup
;
882 st
= rsxx_dma_setup(card
);
884 dev_info(CARD_TO_DEV(card
),
885 "Failed to setup DMA engine\n");
886 goto failed_dma_setup
;
889 /************* Setup Card Event Handler *************/
890 card
->event_wq
= create_singlethread_workqueue(DRIVER_NAME
"_event");
891 if (!card
->event_wq
) {
892 dev_err(CARD_TO_DEV(card
), "Failed card event setup.\n");
893 goto failed_event_handler
;
896 INIT_WORK(&card
->event_work
, card_event_handler
);
898 st
= rsxx_setup_dev(card
);
900 goto failed_create_dev
;
902 rsxx_get_card_state(card
, &card
->state
);
904 dev_info(CARD_TO_DEV(card
),
906 rsxx_card_state_to_str(card
->state
));
909 * Now that the DMA Engine and devices have been setup,
910 * we can enable the event interrupt(it kicks off actions in
911 * those layers so we couldn't enable it right away.)
913 spin_lock_irq(&card
->irq_lock
);
914 rsxx_enable_ier_and_isr(card
, CR_INTR_EVENT
);
915 spin_unlock_irq(&card
->irq_lock
);
917 if (card
->state
== CARD_STATE_SHUTDOWN
) {
918 st
= rsxx_issue_card_cmd(card
, CARD_CMD_STARTUP
);
920 dev_crit(CARD_TO_DEV(card
),
921 "Failed issuing card startup\n");
923 sync_timeout
= SYNC_START_TIMEOUT
;
925 dev_info(CARD_TO_DEV(card
),
926 "Waiting for card to startup\n");
932 rsxx_get_card_state(card
, &card
->state
);
933 } while (sync_timeout
&&
934 (card
->state
== CARD_STATE_STARTING
));
936 if (card
->state
== CARD_STATE_STARTING
) {
937 dev_warn(CARD_TO_DEV(card
),
938 "Card startup timed out\n");
941 dev_info(CARD_TO_DEV(card
),
943 rsxx_card_state_to_str(card
->state
));
944 st
= rsxx_get_card_size8(card
, &card
->size8
);
949 } else if (card
->state
== CARD_STATE_GOOD
||
950 card
->state
== CARD_STATE_RD_ONLY_FAULT
) {
951 st
= rsxx_get_card_size8(card
, &card
->size8
);
956 rsxx_attach_dev(card
);
958 /************* Setup Debugfs *************/
959 rsxx_debugfs_dev_new(card
);
964 destroy_workqueue(card
->event_wq
);
965 card
->event_wq
= NULL
;
966 failed_event_handler
:
967 rsxx_dma_destroy(card
);
969 failed_compatiblity_check
:
970 destroy_workqueue(card
->creg_ctrl
.creg_wq
);
971 card
->creg_ctrl
.creg_wq
= NULL
;
973 spin_lock_irq(&card
->irq_lock
);
974 rsxx_disable_ier_and_isr(card
, CR_INTR_ALL
);
975 spin_unlock_irq(&card
->irq_lock
);
976 free_irq(dev
->irq
, card
);
978 pci_disable_msi(dev
);
980 pci_iounmap(dev
, card
->regmap
);
982 pci_release_regions(dev
);
983 failed_request_regions
:
985 pci_disable_device(dev
);
987 spin_lock(&rsxx_ida_lock
);
988 ida_remove(&rsxx_disk_ida
, card
->disk_id
);
989 spin_unlock(&rsxx_ida_lock
);
996 static void rsxx_pci_remove(struct pci_dev
*dev
)
998 struct rsxx_cardinfo
*card
= pci_get_drvdata(dev
);
1006 dev_info(CARD_TO_DEV(card
),
1007 "Removing PCI-Flash SSD.\n");
1009 rsxx_detach_dev(card
);
1011 for (i
= 0; i
< card
->n_targets
; i
++) {
1012 spin_lock_irqsave(&card
->irq_lock
, flags
);
1013 rsxx_disable_ier_and_isr(card
, CR_INTR_DMA(i
));
1014 spin_unlock_irqrestore(&card
->irq_lock
, flags
);
1017 st
= card_shutdown(card
);
1019 dev_crit(CARD_TO_DEV(card
), "Shutdown failed!\n");
1021 /* Sync outstanding event handlers. */
1022 spin_lock_irqsave(&card
->irq_lock
, flags
);
1023 rsxx_disable_ier_and_isr(card
, CR_INTR_EVENT
);
1024 spin_unlock_irqrestore(&card
->irq_lock
, flags
);
1026 cancel_work_sync(&card
->event_work
);
1028 rsxx_destroy_dev(card
);
1029 rsxx_dma_destroy(card
);
1031 spin_lock_irqsave(&card
->irq_lock
, flags
);
1032 rsxx_disable_ier_and_isr(card
, CR_INTR_ALL
);
1033 spin_unlock_irqrestore(&card
->irq_lock
, flags
);
1035 /* Prevent work_structs from re-queuing themselves. */
1038 debugfs_remove_recursive(card
->debugfs_dir
);
1040 free_irq(dev
->irq
, card
);
1043 pci_disable_msi(dev
);
1045 rsxx_creg_destroy(card
);
1047 pci_iounmap(dev
, card
->regmap
);
1049 pci_disable_device(dev
);
1050 pci_release_regions(dev
);
1055 static int rsxx_pci_suspend(struct pci_dev
*dev
, pm_message_t state
)
1057 /* We don't support suspend at this time. */
1061 static void rsxx_pci_shutdown(struct pci_dev
*dev
)
1063 struct rsxx_cardinfo
*card
= pci_get_drvdata(dev
);
1064 unsigned long flags
;
1070 dev_info(CARD_TO_DEV(card
), "Shutting down PCI-Flash SSD.\n");
1072 rsxx_detach_dev(card
);
1074 for (i
= 0; i
< card
->n_targets
; i
++) {
1075 spin_lock_irqsave(&card
->irq_lock
, flags
);
1076 rsxx_disable_ier_and_isr(card
, CR_INTR_DMA(i
));
1077 spin_unlock_irqrestore(&card
->irq_lock
, flags
);
1080 card_shutdown(card
);
1083 static const struct pci_error_handlers rsxx_err_handler
= {
1084 .error_detected
= rsxx_error_detected
,
1085 .slot_reset
= rsxx_slot_reset
,
1088 static const struct pci_device_id rsxx_pci_ids
[] = {
1089 {PCI_DEVICE(PCI_VENDOR_ID_IBM
, PCI_DEVICE_ID_FS70_FLASH
)},
1090 {PCI_DEVICE(PCI_VENDOR_ID_IBM
, PCI_DEVICE_ID_FS80_FLASH
)},
1094 MODULE_DEVICE_TABLE(pci
, rsxx_pci_ids
);
1096 static struct pci_driver rsxx_pci_driver
= {
1097 .name
= DRIVER_NAME
,
1098 .id_table
= rsxx_pci_ids
,
1099 .probe
= rsxx_pci_probe
,
1100 .remove
= rsxx_pci_remove
,
1101 .suspend
= rsxx_pci_suspend
,
1102 .shutdown
= rsxx_pci_shutdown
,
1103 .err_handler
= &rsxx_err_handler
,
1106 static int __init
rsxx_core_init(void)
1110 st
= rsxx_dev_init();
1114 st
= rsxx_dma_init();
1116 goto dma_init_failed
;
1118 st
= rsxx_creg_init();
1120 goto creg_init_failed
;
1122 return pci_register_driver(&rsxx_pci_driver
);
1132 static void __exit
rsxx_core_cleanup(void)
1134 pci_unregister_driver(&rsxx_pci_driver
);
1135 rsxx_creg_cleanup();
1140 module_init(rsxx_core_init
);
1141 module_exit(rsxx_core_cleanup
);