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
= kzalloc(cnt
, GFP_KERNEL
);
210 st
= copy_from_user(buf
, ubuf
, cnt
);
212 st
= rsxx_creg_write(card
, CREG_ADD_CRAM
+ (u32
)*ppos
, cnt
,
221 static const struct file_operations debugfs_cram_fops
= {
222 .owner
= THIS_MODULE
,
223 .read
= rsxx_cram_read
,
224 .write
= rsxx_cram_write
,
227 static const struct file_operations debugfs_stats_fops
= {
228 .owner
= THIS_MODULE
,
229 .open
= rsxx_attr_stats_open
,
232 .release
= single_release
,
235 static const struct file_operations debugfs_pci_regs_fops
= {
236 .owner
= THIS_MODULE
,
237 .open
= rsxx_attr_pci_regs_open
,
240 .release
= single_release
,
243 static void rsxx_debugfs_dev_new(struct rsxx_cardinfo
*card
)
245 struct dentry
*debugfs_stats
;
246 struct dentry
*debugfs_pci_regs
;
247 struct dentry
*debugfs_cram
;
249 card
->debugfs_dir
= debugfs_create_dir(card
->gendisk
->disk_name
, NULL
);
250 if (IS_ERR_OR_NULL(card
->debugfs_dir
))
251 goto failed_debugfs_dir
;
253 debugfs_stats
= debugfs_create_file("stats", S_IRUGO
,
254 card
->debugfs_dir
, card
,
255 &debugfs_stats_fops
);
256 if (IS_ERR_OR_NULL(debugfs_stats
))
257 goto failed_debugfs_stats
;
259 debugfs_pci_regs
= debugfs_create_file("pci_regs", S_IRUGO
,
260 card
->debugfs_dir
, card
,
261 &debugfs_pci_regs_fops
);
262 if (IS_ERR_OR_NULL(debugfs_pci_regs
))
263 goto failed_debugfs_pci_regs
;
265 debugfs_cram
= debugfs_create_file("cram", S_IRUGO
| S_IWUSR
,
266 card
->debugfs_dir
, card
,
268 if (IS_ERR_OR_NULL(debugfs_cram
))
269 goto failed_debugfs_cram
;
273 debugfs_remove(debugfs_pci_regs
);
274 failed_debugfs_pci_regs
:
275 debugfs_remove(debugfs_stats
);
276 failed_debugfs_stats
:
277 debugfs_remove(card
->debugfs_dir
);
279 card
->debugfs_dir
= NULL
;
282 /*----------------- Interrupt Control & Handling -------------------*/
284 static void rsxx_mask_interrupts(struct rsxx_cardinfo
*card
)
290 static void __enable_intr(unsigned int *mask
, unsigned int intr
)
295 static void __disable_intr(unsigned int *mask
, unsigned int intr
)
301 * NOTE: Disabling the IER will disable the hardware interrupt.
302 * Disabling the ISR will disable the software handling of the ISR bit.
304 * Enable/Disable interrupt functions assume the card->irq_lock
305 * is held by the caller.
307 void rsxx_enable_ier(struct rsxx_cardinfo
*card
, unsigned int intr
)
309 if (unlikely(card
->halt
) ||
310 unlikely(card
->eeh_state
))
313 __enable_intr(&card
->ier_mask
, intr
);
314 iowrite32(card
->ier_mask
, card
->regmap
+ IER
);
317 void rsxx_disable_ier(struct rsxx_cardinfo
*card
, unsigned int intr
)
319 if (unlikely(card
->eeh_state
))
322 __disable_intr(&card
->ier_mask
, intr
);
323 iowrite32(card
->ier_mask
, card
->regmap
+ IER
);
326 void rsxx_enable_ier_and_isr(struct rsxx_cardinfo
*card
,
329 if (unlikely(card
->halt
) ||
330 unlikely(card
->eeh_state
))
333 __enable_intr(&card
->isr_mask
, intr
);
334 __enable_intr(&card
->ier_mask
, intr
);
335 iowrite32(card
->ier_mask
, card
->regmap
+ IER
);
337 void rsxx_disable_ier_and_isr(struct rsxx_cardinfo
*card
,
340 if (unlikely(card
->eeh_state
))
343 __disable_intr(&card
->isr_mask
, intr
);
344 __disable_intr(&card
->ier_mask
, intr
);
345 iowrite32(card
->ier_mask
, card
->regmap
+ IER
);
348 static irqreturn_t
rsxx_isr(int irq
, void *pdata
)
350 struct rsxx_cardinfo
*card
= pdata
;
356 spin_lock(&card
->irq_lock
);
361 if (unlikely(card
->eeh_state
))
364 isr
= ioread32(card
->regmap
+ ISR
);
365 if (isr
== 0xffffffff) {
367 * A few systems seem to have an intermittent issue
368 * where PCI reads return all Fs, but retrying the read
369 * a little later will return as expected.
371 dev_info(CARD_TO_DEV(card
),
372 "ISR = 0xFFFFFFFF, retrying later\n");
376 isr
&= card
->isr_mask
;
380 for (i
= 0; i
< card
->n_targets
; i
++) {
381 if (isr
& CR_INTR_DMA(i
)) {
382 if (card
->ier_mask
& CR_INTR_DMA(i
)) {
383 rsxx_disable_ier(card
, CR_INTR_DMA(i
));
386 queue_work(card
->ctrl
[i
].done_wq
,
387 &card
->ctrl
[i
].dma_done_work
);
392 if (isr
& CR_INTR_CREG
) {
393 queue_work(card
->creg_ctrl
.creg_wq
,
394 &card
->creg_ctrl
.done_work
);
398 if (isr
& CR_INTR_EVENT
) {
399 queue_work(card
->event_wq
, &card
->event_work
);
400 rsxx_disable_ier_and_isr(card
, CR_INTR_EVENT
);
403 } while (reread_isr
);
405 spin_unlock(&card
->irq_lock
);
407 return handled
? IRQ_HANDLED
: IRQ_NONE
;
410 /*----------------- Card Event Handler -------------------*/
411 static const char * const rsxx_card_state_to_str(unsigned int state
)
413 static const char * const state_strings
[] = {
414 "Unknown", "Shutdown", "Starting", "Formatting",
415 "Uninitialized", "Good", "Shutting Down",
416 "Fault", "Read Only Fault", "dStroying"
419 return state_strings
[ffs(state
)];
422 static void card_state_change(struct rsxx_cardinfo
*card
,
423 unsigned int new_state
)
427 dev_info(CARD_TO_DEV(card
),
428 "card state change detected.(%s -> %s)\n",
429 rsxx_card_state_to_str(card
->state
),
430 rsxx_card_state_to_str(new_state
));
432 card
->state
= new_state
;
434 /* Don't attach DMA interfaces if the card has an invalid config */
435 if (!card
->config_valid
)
439 case CARD_STATE_RD_ONLY_FAULT
:
440 dev_crit(CARD_TO_DEV(card
),
441 "Hardware has entered read-only mode!\n");
443 * Fall through so the DMA devices can be attached and
444 * the user can attempt to pull off their data.
446 case CARD_STATE_GOOD
:
447 st
= rsxx_get_card_size8(card
, &card
->size8
);
449 dev_err(CARD_TO_DEV(card
),
450 "Failed attaching DMA devices\n");
452 if (card
->config_valid
)
453 set_capacity(card
->gendisk
, card
->size8
>> 9);
456 case CARD_STATE_FAULT
:
457 dev_crit(CARD_TO_DEV(card
),
458 "Hardware Fault reported!\n");
461 /* Everything else, detach DMA interface if it's attached. */
462 case CARD_STATE_SHUTDOWN
:
463 case CARD_STATE_STARTING
:
464 case CARD_STATE_FORMATTING
:
465 case CARD_STATE_UNINITIALIZED
:
466 case CARD_STATE_SHUTTING_DOWN
:
468 * dStroy is a term coined by marketing to represent the low level
471 case CARD_STATE_DSTROYING
:
472 set_capacity(card
->gendisk
, 0);
477 static void card_event_handler(struct work_struct
*work
)
479 struct rsxx_cardinfo
*card
;
484 card
= container_of(work
, struct rsxx_cardinfo
, event_work
);
486 if (unlikely(card
->halt
))
490 * Enable the interrupt now to avoid any weird race conditions where a
491 * state change might occur while rsxx_get_card_state() is
492 * processing a returned creg cmd.
494 spin_lock_irqsave(&card
->irq_lock
, flags
);
495 rsxx_enable_ier_and_isr(card
, CR_INTR_EVENT
);
496 spin_unlock_irqrestore(&card
->irq_lock
, flags
);
498 st
= rsxx_get_card_state(card
, &state
);
500 dev_info(CARD_TO_DEV(card
),
501 "Failed reading state after event.\n");
505 if (card
->state
!= state
)
506 card_state_change(card
, state
);
508 if (card
->creg_ctrl
.creg_stats
.stat
& CREG_STAT_LOG_PENDING
)
509 rsxx_read_hw_log(card
);
512 /*----------------- Card Operations -------------------*/
513 static int card_shutdown(struct rsxx_cardinfo
*card
)
517 const int timeout
= msecs_to_jiffies(120000);
520 /* We can't issue a shutdown if the card is in a transition state */
523 st
= rsxx_get_card_state(card
, &state
);
526 } while (state
== CARD_STATE_STARTING
&&
527 (jiffies
- start
< timeout
));
529 if (state
== CARD_STATE_STARTING
)
532 /* Only issue a shutdown if we need to */
533 if ((state
!= CARD_STATE_SHUTTING_DOWN
) &&
534 (state
!= CARD_STATE_SHUTDOWN
)) {
535 st
= rsxx_issue_card_cmd(card
, CARD_CMD_SHUTDOWN
);
542 st
= rsxx_get_card_state(card
, &state
);
545 } while (state
!= CARD_STATE_SHUTDOWN
&&
546 (jiffies
- start
< timeout
));
548 if (state
!= CARD_STATE_SHUTDOWN
)
554 static int rsxx_eeh_frozen(struct pci_dev
*dev
)
556 struct rsxx_cardinfo
*card
= pci_get_drvdata(dev
);
560 dev_warn(&dev
->dev
, "IBM Flash Adapter PCI: preparing for slot reset.\n");
563 rsxx_mask_interrupts(card
);
566 * We need to guarantee that the write for eeh_state and masking
567 * interrupts does not become reordered. This will prevent a possible
568 * race condition with the EEH code.
572 pci_disable_device(dev
);
574 st
= rsxx_eeh_save_issued_dmas(card
);
578 rsxx_eeh_save_issued_creg(card
);
580 for (i
= 0; i
< card
->n_targets
; i
++) {
581 if (card
->ctrl
[i
].status
.buf
)
582 pci_free_consistent(card
->dev
, STATUS_BUFFER_SIZE8
,
583 card
->ctrl
[i
].status
.buf
,
584 card
->ctrl
[i
].status
.dma_addr
);
585 if (card
->ctrl
[i
].cmd
.buf
)
586 pci_free_consistent(card
->dev
, COMMAND_BUFFER_SIZE8
,
587 card
->ctrl
[i
].cmd
.buf
,
588 card
->ctrl
[i
].cmd
.dma_addr
);
594 static void rsxx_eeh_failure(struct pci_dev
*dev
)
596 struct rsxx_cardinfo
*card
= pci_get_drvdata(dev
);
600 dev_err(&dev
->dev
, "IBM Flash Adapter PCI: disabling failed card.\n");
605 for (i
= 0; i
< card
->n_targets
; i
++) {
606 spin_lock_bh(&card
->ctrl
[i
].queue_lock
);
607 cnt
= rsxx_cleanup_dma_queue(&card
->ctrl
[i
],
608 &card
->ctrl
[i
].queue
,
610 spin_unlock_bh(&card
->ctrl
[i
].queue_lock
);
612 cnt
+= rsxx_dma_cancel(&card
->ctrl
[i
]);
615 dev_info(CARD_TO_DEV(card
),
616 "Freed %d queued DMAs on channel %d\n",
617 cnt
, card
->ctrl
[i
].id
);
621 static int rsxx_eeh_fifo_flush_poll(struct rsxx_cardinfo
*card
)
626 /* We need to wait for the hardware to reset */
627 while (iter
++ < 10) {
628 status
= ioread32(card
->regmap
+ PCI_RECONFIG
);
630 if (status
& RSXX_FLUSH_BUSY
) {
635 if (status
& RSXX_FLUSH_TIMEOUT
)
636 dev_warn(CARD_TO_DEV(card
), "HW: flash controller timeout\n");
640 /* Hardware failed resetting itself. */
644 static pci_ers_result_t
rsxx_error_detected(struct pci_dev
*dev
,
645 enum pci_channel_state error
)
649 if (dev
->revision
< RSXX_EEH_SUPPORT
)
650 return PCI_ERS_RESULT_NONE
;
652 if (error
== pci_channel_io_perm_failure
) {
653 rsxx_eeh_failure(dev
);
654 return PCI_ERS_RESULT_DISCONNECT
;
657 st
= rsxx_eeh_frozen(dev
);
659 dev_err(&dev
->dev
, "Slot reset setup failed\n");
660 rsxx_eeh_failure(dev
);
661 return PCI_ERS_RESULT_DISCONNECT
;
664 return PCI_ERS_RESULT_NEED_RESET
;
667 static pci_ers_result_t
rsxx_slot_reset(struct pci_dev
*dev
)
669 struct rsxx_cardinfo
*card
= pci_get_drvdata(dev
);
675 "IBM Flash Adapter PCI: recovering from slot reset.\n");
677 st
= pci_enable_device(dev
);
679 goto failed_hw_setup
;
683 st
= rsxx_eeh_fifo_flush_poll(card
);
685 goto failed_hw_setup
;
687 rsxx_dma_queue_reset(card
);
689 for (i
= 0; i
< card
->n_targets
; i
++) {
690 st
= rsxx_hw_buffers_init(dev
, &card
->ctrl
[i
]);
692 goto failed_hw_buffers_init
;
695 if (card
->config_valid
)
696 rsxx_dma_configure(card
);
698 /* Clears the ISR register from spurious interrupts */
699 st
= ioread32(card
->regmap
+ ISR
);
703 spin_lock_irqsave(&card
->irq_lock
, flags
);
704 if (card
->n_targets
& RSXX_MAX_TARGETS
)
705 rsxx_enable_ier_and_isr(card
, CR_INTR_ALL_G
);
707 rsxx_enable_ier_and_isr(card
, CR_INTR_ALL_C
);
708 spin_unlock_irqrestore(&card
->irq_lock
, flags
);
710 rsxx_kick_creg_queue(card
);
712 for (i
= 0; i
< card
->n_targets
; i
++) {
713 spin_lock(&card
->ctrl
[i
].queue_lock
);
714 if (list_empty(&card
->ctrl
[i
].queue
)) {
715 spin_unlock(&card
->ctrl
[i
].queue_lock
);
718 spin_unlock(&card
->ctrl
[i
].queue_lock
);
720 queue_work(card
->ctrl
[i
].issue_wq
,
721 &card
->ctrl
[i
].issue_dma_work
);
724 dev_info(&dev
->dev
, "IBM Flash Adapter PCI: recovery complete.\n");
726 return PCI_ERS_RESULT_RECOVERED
;
728 failed_hw_buffers_init
:
729 for (i
= 0; i
< card
->n_targets
; i
++) {
730 if (card
->ctrl
[i
].status
.buf
)
731 pci_free_consistent(card
->dev
,
733 card
->ctrl
[i
].status
.buf
,
734 card
->ctrl
[i
].status
.dma_addr
);
735 if (card
->ctrl
[i
].cmd
.buf
)
736 pci_free_consistent(card
->dev
,
737 COMMAND_BUFFER_SIZE8
,
738 card
->ctrl
[i
].cmd
.buf
,
739 card
->ctrl
[i
].cmd
.dma_addr
);
742 rsxx_eeh_failure(dev
);
743 return PCI_ERS_RESULT_DISCONNECT
;
747 /*----------------- Driver Initialization & Setup -------------------*/
748 /* Returns: 0 if the driver is compatible with the device
749 -1 if the driver is NOT compatible with the device */
750 static int rsxx_compatibility_check(struct rsxx_cardinfo
*card
)
752 unsigned char pci_rev
;
754 pci_read_config_byte(card
->dev
, PCI_REVISION_ID
, &pci_rev
);
756 if (pci_rev
> RS70_PCI_REV_SUPPORTED
)
761 static int rsxx_pci_probe(struct pci_dev
*dev
,
762 const struct pci_device_id
*id
)
764 struct rsxx_cardinfo
*card
;
766 unsigned int sync_timeout
;
768 dev_info(&dev
->dev
, "PCI-Flash SSD discovered\n");
770 card
= kzalloc(sizeof(*card
), GFP_KERNEL
);
775 pci_set_drvdata(dev
, card
);
778 if (!ida_pre_get(&rsxx_disk_ida
, GFP_KERNEL
)) {
783 spin_lock(&rsxx_ida_lock
);
784 st
= ida_get_new(&rsxx_disk_ida
, &card
->disk_id
);
785 spin_unlock(&rsxx_ida_lock
);
786 } while (st
== -EAGAIN
);
791 st
= pci_enable_device(dev
);
796 pci_set_dma_max_seg_size(dev
, RSXX_HW_BLK_SIZE
);
798 st
= pci_set_dma_mask(dev
, DMA_BIT_MASK(64));
800 dev_err(CARD_TO_DEV(card
),
801 "No usable DMA configuration,aborting\n");
802 goto failed_dma_mask
;
805 st
= pci_request_regions(dev
, DRIVER_NAME
);
807 dev_err(CARD_TO_DEV(card
),
808 "Failed to request memory region\n");
809 goto failed_request_regions
;
812 if (pci_resource_len(dev
, 0) == 0) {
813 dev_err(CARD_TO_DEV(card
), "BAR0 has length 0!\n");
818 card
->regmap
= pci_iomap(dev
, 0, 0);
820 dev_err(CARD_TO_DEV(card
), "Failed to map BAR0\n");
825 spin_lock_init(&card
->irq_lock
);
829 spin_lock_irq(&card
->irq_lock
);
830 rsxx_disable_ier_and_isr(card
, CR_INTR_ALL
);
831 spin_unlock_irq(&card
->irq_lock
);
834 st
= pci_enable_msi(dev
);
836 dev_warn(CARD_TO_DEV(card
),
837 "Failed to enable MSI\n");
840 st
= request_irq(dev
->irq
, rsxx_isr
, IRQF_SHARED
,
843 dev_err(CARD_TO_DEV(card
),
844 "Failed requesting IRQ%d\n", dev
->irq
);
848 /************* Setup Processor Command Interface *************/
849 st
= rsxx_creg_setup(card
);
851 dev_err(CARD_TO_DEV(card
), "Failed to setup creg interface.\n");
852 goto failed_creg_setup
;
855 spin_lock_irq(&card
->irq_lock
);
856 rsxx_enable_ier_and_isr(card
, CR_INTR_CREG
);
857 spin_unlock_irq(&card
->irq_lock
);
859 st
= rsxx_compatibility_check(card
);
861 dev_warn(CARD_TO_DEV(card
),
862 "Incompatible driver detected. Please update the driver.\n");
864 goto failed_compatiblity_check
;
867 /************* Load Card Config *************/
868 st
= rsxx_load_config(card
);
870 dev_err(CARD_TO_DEV(card
),
871 "Failed loading card config\n");
873 /************* Setup DMA Engine *************/
874 st
= rsxx_get_num_targets(card
, &card
->n_targets
);
876 dev_info(CARD_TO_DEV(card
),
877 "Failed reading the number of DMA targets\n");
879 card
->ctrl
= kzalloc(card
->n_targets
* sizeof(*card
->ctrl
), GFP_KERNEL
);
882 goto failed_dma_setup
;
885 st
= rsxx_dma_setup(card
);
887 dev_info(CARD_TO_DEV(card
),
888 "Failed to setup DMA engine\n");
889 goto failed_dma_setup
;
892 /************* Setup Card Event Handler *************/
893 card
->event_wq
= create_singlethread_workqueue(DRIVER_NAME
"_event");
894 if (!card
->event_wq
) {
895 dev_err(CARD_TO_DEV(card
), "Failed card event setup.\n");
896 goto failed_event_handler
;
899 INIT_WORK(&card
->event_work
, card_event_handler
);
901 st
= rsxx_setup_dev(card
);
903 goto failed_create_dev
;
905 rsxx_get_card_state(card
, &card
->state
);
907 dev_info(CARD_TO_DEV(card
),
909 rsxx_card_state_to_str(card
->state
));
912 * Now that the DMA Engine and devices have been setup,
913 * we can enable the event interrupt(it kicks off actions in
914 * those layers so we couldn't enable it right away.)
916 spin_lock_irq(&card
->irq_lock
);
917 rsxx_enable_ier_and_isr(card
, CR_INTR_EVENT
);
918 spin_unlock_irq(&card
->irq_lock
);
920 if (card
->state
== CARD_STATE_SHUTDOWN
) {
921 st
= rsxx_issue_card_cmd(card
, CARD_CMD_STARTUP
);
923 dev_crit(CARD_TO_DEV(card
),
924 "Failed issuing card startup\n");
926 sync_timeout
= SYNC_START_TIMEOUT
;
928 dev_info(CARD_TO_DEV(card
),
929 "Waiting for card to startup\n");
935 rsxx_get_card_state(card
, &card
->state
);
936 } while (sync_timeout
&&
937 (card
->state
== CARD_STATE_STARTING
));
939 if (card
->state
== CARD_STATE_STARTING
) {
940 dev_warn(CARD_TO_DEV(card
),
941 "Card startup timed out\n");
944 dev_info(CARD_TO_DEV(card
),
946 rsxx_card_state_to_str(card
->state
));
947 st
= rsxx_get_card_size8(card
, &card
->size8
);
952 } else if (card
->state
== CARD_STATE_GOOD
||
953 card
->state
== CARD_STATE_RD_ONLY_FAULT
) {
954 st
= rsxx_get_card_size8(card
, &card
->size8
);
959 rsxx_attach_dev(card
);
961 /************* Setup Debugfs *************/
962 rsxx_debugfs_dev_new(card
);
967 destroy_workqueue(card
->event_wq
);
968 card
->event_wq
= NULL
;
969 failed_event_handler
:
970 rsxx_dma_destroy(card
);
972 failed_compatiblity_check
:
973 destroy_workqueue(card
->creg_ctrl
.creg_wq
);
974 card
->creg_ctrl
.creg_wq
= NULL
;
976 spin_lock_irq(&card
->irq_lock
);
977 rsxx_disable_ier_and_isr(card
, CR_INTR_ALL
);
978 spin_unlock_irq(&card
->irq_lock
);
979 free_irq(dev
->irq
, card
);
981 pci_disable_msi(dev
);
983 pci_iounmap(dev
, card
->regmap
);
985 pci_release_regions(dev
);
986 failed_request_regions
:
988 pci_disable_device(dev
);
990 spin_lock(&rsxx_ida_lock
);
991 ida_remove(&rsxx_disk_ida
, card
->disk_id
);
992 spin_unlock(&rsxx_ida_lock
);
999 static void rsxx_pci_remove(struct pci_dev
*dev
)
1001 struct rsxx_cardinfo
*card
= pci_get_drvdata(dev
);
1002 unsigned long flags
;
1009 dev_info(CARD_TO_DEV(card
),
1010 "Removing PCI-Flash SSD.\n");
1012 rsxx_detach_dev(card
);
1014 for (i
= 0; i
< card
->n_targets
; i
++) {
1015 spin_lock_irqsave(&card
->irq_lock
, flags
);
1016 rsxx_disable_ier_and_isr(card
, CR_INTR_DMA(i
));
1017 spin_unlock_irqrestore(&card
->irq_lock
, flags
);
1020 st
= card_shutdown(card
);
1022 dev_crit(CARD_TO_DEV(card
), "Shutdown failed!\n");
1024 /* Sync outstanding event handlers. */
1025 spin_lock_irqsave(&card
->irq_lock
, flags
);
1026 rsxx_disable_ier_and_isr(card
, CR_INTR_EVENT
);
1027 spin_unlock_irqrestore(&card
->irq_lock
, flags
);
1029 cancel_work_sync(&card
->event_work
);
1031 rsxx_destroy_dev(card
);
1032 rsxx_dma_destroy(card
);
1034 spin_lock_irqsave(&card
->irq_lock
, flags
);
1035 rsxx_disable_ier_and_isr(card
, CR_INTR_ALL
);
1036 spin_unlock_irqrestore(&card
->irq_lock
, flags
);
1038 /* Prevent work_structs from re-queuing themselves. */
1041 debugfs_remove_recursive(card
->debugfs_dir
);
1043 free_irq(dev
->irq
, card
);
1046 pci_disable_msi(dev
);
1048 rsxx_creg_destroy(card
);
1050 pci_iounmap(dev
, card
->regmap
);
1052 pci_disable_device(dev
);
1053 pci_release_regions(dev
);
1058 static int rsxx_pci_suspend(struct pci_dev
*dev
, pm_message_t state
)
1060 /* We don't support suspend at this time. */
1064 static void rsxx_pci_shutdown(struct pci_dev
*dev
)
1066 struct rsxx_cardinfo
*card
= pci_get_drvdata(dev
);
1067 unsigned long flags
;
1073 dev_info(CARD_TO_DEV(card
), "Shutting down PCI-Flash SSD.\n");
1075 rsxx_detach_dev(card
);
1077 for (i
= 0; i
< card
->n_targets
; i
++) {
1078 spin_lock_irqsave(&card
->irq_lock
, flags
);
1079 rsxx_disable_ier_and_isr(card
, CR_INTR_DMA(i
));
1080 spin_unlock_irqrestore(&card
->irq_lock
, flags
);
1083 card_shutdown(card
);
1086 static const struct pci_error_handlers rsxx_err_handler
= {
1087 .error_detected
= rsxx_error_detected
,
1088 .slot_reset
= rsxx_slot_reset
,
1091 static const struct pci_device_id rsxx_pci_ids
[] = {
1092 {PCI_DEVICE(PCI_VENDOR_ID_IBM
, PCI_DEVICE_ID_FS70_FLASH
)},
1093 {PCI_DEVICE(PCI_VENDOR_ID_IBM
, PCI_DEVICE_ID_FS80_FLASH
)},
1097 MODULE_DEVICE_TABLE(pci
, rsxx_pci_ids
);
1099 static struct pci_driver rsxx_pci_driver
= {
1100 .name
= DRIVER_NAME
,
1101 .id_table
= rsxx_pci_ids
,
1102 .probe
= rsxx_pci_probe
,
1103 .remove
= rsxx_pci_remove
,
1104 .suspend
= rsxx_pci_suspend
,
1105 .shutdown
= rsxx_pci_shutdown
,
1106 .err_handler
= &rsxx_err_handler
,
1109 static int __init
rsxx_core_init(void)
1113 st
= rsxx_dev_init();
1117 st
= rsxx_dma_init();
1119 goto dma_init_failed
;
1121 st
= rsxx_creg_init();
1123 goto creg_init_failed
;
1125 return pci_register_driver(&rsxx_pci_driver
);
1135 static void __exit
rsxx_core_cleanup(void)
1137 pci_unregister_driver(&rsxx_pci_driver
);
1138 rsxx_creg_cleanup();
1143 module_init(rsxx_core_init
);
1144 module_exit(rsxx_core_cleanup
);