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 ------------------- */
71 static int rsxx_attr_pci_regs_show(struct seq_file
*m
, void *p
)
73 struct rsxx_cardinfo
*card
= m
->private;
75 seq_printf(m
, "HWID 0x%08x\n",
76 ioread32(card
->regmap
+ HWID
));
77 seq_printf(m
, "SCRATCH 0x%08x\n",
78 ioread32(card
->regmap
+ SCRATCH
));
79 seq_printf(m
, "IER 0x%08x\n",
80 ioread32(card
->regmap
+ IER
));
81 seq_printf(m
, "IPR 0x%08x\n",
82 ioread32(card
->regmap
+ IPR
));
83 seq_printf(m
, "CREG_CMD 0x%08x\n",
84 ioread32(card
->regmap
+ CREG_CMD
));
85 seq_printf(m
, "CREG_ADD 0x%08x\n",
86 ioread32(card
->regmap
+ CREG_ADD
));
87 seq_printf(m
, "CREG_CNT 0x%08x\n",
88 ioread32(card
->regmap
+ CREG_CNT
));
89 seq_printf(m
, "CREG_STAT 0x%08x\n",
90 ioread32(card
->regmap
+ CREG_STAT
));
91 seq_printf(m
, "CREG_DATA0 0x%08x\n",
92 ioread32(card
->regmap
+ CREG_DATA0
));
93 seq_printf(m
, "CREG_DATA1 0x%08x\n",
94 ioread32(card
->regmap
+ CREG_DATA1
));
95 seq_printf(m
, "CREG_DATA2 0x%08x\n",
96 ioread32(card
->regmap
+ CREG_DATA2
));
97 seq_printf(m
, "CREG_DATA3 0x%08x\n",
98 ioread32(card
->regmap
+ CREG_DATA3
));
99 seq_printf(m
, "CREG_DATA4 0x%08x\n",
100 ioread32(card
->regmap
+ CREG_DATA4
));
101 seq_printf(m
, "CREG_DATA5 0x%08x\n",
102 ioread32(card
->regmap
+ CREG_DATA5
));
103 seq_printf(m
, "CREG_DATA6 0x%08x\n",
104 ioread32(card
->regmap
+ CREG_DATA6
));
105 seq_printf(m
, "CREG_DATA7 0x%08x\n",
106 ioread32(card
->regmap
+ CREG_DATA7
));
107 seq_printf(m
, "INTR_COAL 0x%08x\n",
108 ioread32(card
->regmap
+ INTR_COAL
));
109 seq_printf(m
, "HW_ERROR 0x%08x\n",
110 ioread32(card
->regmap
+ HW_ERROR
));
111 seq_printf(m
, "DEBUG0 0x%08x\n",
112 ioread32(card
->regmap
+ PCI_DEBUG0
));
113 seq_printf(m
, "DEBUG1 0x%08x\n",
114 ioread32(card
->regmap
+ PCI_DEBUG1
));
115 seq_printf(m
, "DEBUG2 0x%08x\n",
116 ioread32(card
->regmap
+ PCI_DEBUG2
));
117 seq_printf(m
, "DEBUG3 0x%08x\n",
118 ioread32(card
->regmap
+ PCI_DEBUG3
));
119 seq_printf(m
, "DEBUG4 0x%08x\n",
120 ioread32(card
->regmap
+ PCI_DEBUG4
));
121 seq_printf(m
, "DEBUG5 0x%08x\n",
122 ioread32(card
->regmap
+ PCI_DEBUG5
));
123 seq_printf(m
, "DEBUG6 0x%08x\n",
124 ioread32(card
->regmap
+ PCI_DEBUG6
));
125 seq_printf(m
, "DEBUG7 0x%08x\n",
126 ioread32(card
->regmap
+ PCI_DEBUG7
));
127 seq_printf(m
, "RECONFIG 0x%08x\n",
128 ioread32(card
->regmap
+ PCI_RECONFIG
));
133 static int rsxx_attr_stats_show(struct seq_file
*m
, void *p
)
135 struct rsxx_cardinfo
*card
= m
->private;
138 for (i
= 0; i
< card
->n_targets
; i
++) {
139 seq_printf(m
, "Ctrl %d CRC Errors = %d\n",
140 i
, card
->ctrl
[i
].stats
.crc_errors
);
141 seq_printf(m
, "Ctrl %d Hard Errors = %d\n",
142 i
, card
->ctrl
[i
].stats
.hard_errors
);
143 seq_printf(m
, "Ctrl %d Soft Errors = %d\n",
144 i
, card
->ctrl
[i
].stats
.soft_errors
);
145 seq_printf(m
, "Ctrl %d Writes Issued = %d\n",
146 i
, card
->ctrl
[i
].stats
.writes_issued
);
147 seq_printf(m
, "Ctrl %d Writes Failed = %d\n",
148 i
, card
->ctrl
[i
].stats
.writes_failed
);
149 seq_printf(m
, "Ctrl %d Reads Issued = %d\n",
150 i
, card
->ctrl
[i
].stats
.reads_issued
);
151 seq_printf(m
, "Ctrl %d Reads Failed = %d\n",
152 i
, card
->ctrl
[i
].stats
.reads_failed
);
153 seq_printf(m
, "Ctrl %d Reads Retried = %d\n",
154 i
, card
->ctrl
[i
].stats
.reads_retried
);
155 seq_printf(m
, "Ctrl %d Discards Issued = %d\n",
156 i
, card
->ctrl
[i
].stats
.discards_issued
);
157 seq_printf(m
, "Ctrl %d Discards Failed = %d\n",
158 i
, card
->ctrl
[i
].stats
.discards_failed
);
159 seq_printf(m
, "Ctrl %d DMA SW Errors = %d\n",
160 i
, card
->ctrl
[i
].stats
.dma_sw_err
);
161 seq_printf(m
, "Ctrl %d DMA HW Faults = %d\n",
162 i
, card
->ctrl
[i
].stats
.dma_hw_fault
);
163 seq_printf(m
, "Ctrl %d DMAs Cancelled = %d\n",
164 i
, card
->ctrl
[i
].stats
.dma_cancelled
);
165 seq_printf(m
, "Ctrl %d SW Queue Depth = %d\n",
166 i
, card
->ctrl
[i
].stats
.sw_q_depth
);
167 seq_printf(m
, "Ctrl %d HW Queue Depth = %d\n",
168 i
, atomic_read(&card
->ctrl
[i
].stats
.hw_q_depth
));
174 static int rsxx_attr_stats_open(struct inode
*inode
, struct file
*file
)
176 return single_open(file
, rsxx_attr_stats_show
, inode
->i_private
);
179 static int rsxx_attr_pci_regs_open(struct inode
*inode
, struct file
*file
)
181 return single_open(file
, rsxx_attr_pci_regs_show
, inode
->i_private
);
184 static ssize_t
rsxx_cram_read(struct file
*fp
, char __user
*ubuf
,
185 size_t cnt
, loff_t
*ppos
)
187 struct rsxx_cram
*info
= fp
->private_data
;
188 struct rsxx_cardinfo
*card
= info
->i_private
;
192 buf
= kzalloc(sizeof(*buf
) * cnt
, GFP_KERNEL
);
196 info
->f_pos
= (u32
)*ppos
+ info
->offset
;
198 st
= rsxx_creg_read(card
, CREG_ADD_CRAM
+ info
->f_pos
, cnt
, buf
, 1);
202 st
= copy_to_user(ubuf
, buf
, cnt
);
213 static ssize_t
rsxx_cram_write(struct file
*fp
, const char __user
*ubuf
,
214 size_t cnt
, loff_t
*ppos
)
216 struct rsxx_cram
*info
= fp
->private_data
;
217 struct rsxx_cardinfo
*card
= info
->i_private
;
221 buf
= kzalloc(sizeof(*buf
) * cnt
, GFP_KERNEL
);
225 st
= copy_from_user(buf
, ubuf
, cnt
);
229 info
->f_pos
= (u32
)*ppos
+ info
->offset
;
231 st
= rsxx_creg_write(card
, CREG_ADD_CRAM
+ info
->f_pos
, cnt
, buf
, 1);
242 static int rsxx_cram_open(struct inode
*inode
, struct file
*file
)
244 struct rsxx_cram
*info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
248 info
->i_private
= inode
->i_private
;
249 info
->f_pos
= file
->f_pos
;
250 file
->private_data
= info
;
255 static int rsxx_cram_release(struct inode
*inode
, struct file
*file
)
257 struct rsxx_cram
*info
= file
->private_data
;
263 file
->private_data
= NULL
;
268 static const struct file_operations debugfs_cram_fops
= {
269 .owner
= THIS_MODULE
,
270 .open
= rsxx_cram_open
,
271 .read
= rsxx_cram_read
,
272 .write
= rsxx_cram_write
,
273 .release
= rsxx_cram_release
,
276 static const struct file_operations debugfs_stats_fops
= {
277 .owner
= THIS_MODULE
,
278 .open
= rsxx_attr_stats_open
,
281 .release
= single_release
,
284 static const struct file_operations debugfs_pci_regs_fops
= {
285 .owner
= THIS_MODULE
,
286 .open
= rsxx_attr_pci_regs_open
,
289 .release
= single_release
,
292 static void rsxx_debugfs_dev_new(struct rsxx_cardinfo
*card
)
294 struct dentry
*debugfs_stats
;
295 struct dentry
*debugfs_pci_regs
;
296 struct dentry
*debugfs_cram
;
298 card
->debugfs_dir
= debugfs_create_dir(card
->gendisk
->disk_name
, NULL
);
299 if (IS_ERR_OR_NULL(card
->debugfs_dir
))
300 goto failed_debugfs_dir
;
302 debugfs_stats
= debugfs_create_file("stats", S_IRUGO
,
303 card
->debugfs_dir
, card
,
304 &debugfs_stats_fops
);
305 if (IS_ERR_OR_NULL(debugfs_stats
))
306 goto failed_debugfs_stats
;
308 debugfs_pci_regs
= debugfs_create_file("pci_regs", S_IRUGO
,
309 card
->debugfs_dir
, card
,
310 &debugfs_pci_regs_fops
);
311 if (IS_ERR_OR_NULL(debugfs_pci_regs
))
312 goto failed_debugfs_pci_regs
;
314 debugfs_cram
= debugfs_create_file("cram", S_IRUGO
| S_IWUSR
,
315 card
->debugfs_dir
, card
,
317 if (IS_ERR_OR_NULL(debugfs_cram
))
318 goto failed_debugfs_cram
;
322 debugfs_remove(debugfs_pci_regs
);
323 failed_debugfs_pci_regs
:
324 debugfs_remove(debugfs_stats
);
325 failed_debugfs_stats
:
326 debugfs_remove(card
->debugfs_dir
);
328 card
->debugfs_dir
= NULL
;
331 /*----------------- Interrupt Control & Handling -------------------*/
333 static void rsxx_mask_interrupts(struct rsxx_cardinfo
*card
)
339 static void __enable_intr(unsigned int *mask
, unsigned int intr
)
344 static void __disable_intr(unsigned int *mask
, unsigned int intr
)
350 * NOTE: Disabling the IER will disable the hardware interrupt.
351 * Disabling the ISR will disable the software handling of the ISR bit.
353 * Enable/Disable interrupt functions assume the card->irq_lock
354 * is held by the caller.
356 void rsxx_enable_ier(struct rsxx_cardinfo
*card
, unsigned int intr
)
358 if (unlikely(card
->halt
) ||
359 unlikely(card
->eeh_state
))
362 __enable_intr(&card
->ier_mask
, intr
);
363 iowrite32(card
->ier_mask
, card
->regmap
+ IER
);
366 void rsxx_disable_ier(struct rsxx_cardinfo
*card
, unsigned int intr
)
368 if (unlikely(card
->eeh_state
))
371 __disable_intr(&card
->ier_mask
, intr
);
372 iowrite32(card
->ier_mask
, card
->regmap
+ IER
);
375 void rsxx_enable_ier_and_isr(struct rsxx_cardinfo
*card
,
378 if (unlikely(card
->halt
) ||
379 unlikely(card
->eeh_state
))
382 __enable_intr(&card
->isr_mask
, intr
);
383 __enable_intr(&card
->ier_mask
, intr
);
384 iowrite32(card
->ier_mask
, card
->regmap
+ IER
);
386 void rsxx_disable_ier_and_isr(struct rsxx_cardinfo
*card
,
389 if (unlikely(card
->eeh_state
))
392 __disable_intr(&card
->isr_mask
, intr
);
393 __disable_intr(&card
->ier_mask
, intr
);
394 iowrite32(card
->ier_mask
, card
->regmap
+ IER
);
397 static irqreturn_t
rsxx_isr(int irq
, void *pdata
)
399 struct rsxx_cardinfo
*card
= pdata
;
405 spin_lock(&card
->irq_lock
);
410 if (unlikely(card
->eeh_state
))
413 isr
= ioread32(card
->regmap
+ ISR
);
414 if (isr
== 0xffffffff) {
416 * A few systems seem to have an intermittent issue
417 * where PCI reads return all Fs, but retrying the read
418 * a little later will return as expected.
420 dev_info(CARD_TO_DEV(card
),
421 "ISR = 0xFFFFFFFF, retrying later\n");
425 isr
&= card
->isr_mask
;
429 for (i
= 0; i
< card
->n_targets
; i
++) {
430 if (isr
& CR_INTR_DMA(i
)) {
431 if (card
->ier_mask
& CR_INTR_DMA(i
)) {
432 rsxx_disable_ier(card
, CR_INTR_DMA(i
));
435 queue_work(card
->ctrl
[i
].done_wq
,
436 &card
->ctrl
[i
].dma_done_work
);
441 if (isr
& CR_INTR_CREG
) {
442 queue_work(card
->creg_ctrl
.creg_wq
,
443 &card
->creg_ctrl
.done_work
);
447 if (isr
& CR_INTR_EVENT
) {
448 queue_work(card
->event_wq
, &card
->event_work
);
449 rsxx_disable_ier_and_isr(card
, CR_INTR_EVENT
);
452 } while (reread_isr
);
454 spin_unlock(&card
->irq_lock
);
456 return handled
? IRQ_HANDLED
: IRQ_NONE
;
459 /*----------------- Card Event Handler -------------------*/
460 static const char * const rsxx_card_state_to_str(unsigned int state
)
462 static const char * const state_strings
[] = {
463 "Unknown", "Shutdown", "Starting", "Formatting",
464 "Uninitialized", "Good", "Shutting Down",
465 "Fault", "Read Only Fault", "dStroying"
468 return state_strings
[ffs(state
)];
471 static void card_state_change(struct rsxx_cardinfo
*card
,
472 unsigned int new_state
)
476 dev_info(CARD_TO_DEV(card
),
477 "card state change detected.(%s -> %s)\n",
478 rsxx_card_state_to_str(card
->state
),
479 rsxx_card_state_to_str(new_state
));
481 card
->state
= new_state
;
483 /* Don't attach DMA interfaces if the card has an invalid config */
484 if (!card
->config_valid
)
488 case CARD_STATE_RD_ONLY_FAULT
:
489 dev_crit(CARD_TO_DEV(card
),
490 "Hardware has entered read-only mode!\n");
492 * Fall through so the DMA devices can be attached and
493 * the user can attempt to pull off their data.
495 case CARD_STATE_GOOD
:
496 st
= rsxx_get_card_size8(card
, &card
->size8
);
498 dev_err(CARD_TO_DEV(card
),
499 "Failed attaching DMA devices\n");
501 if (card
->config_valid
)
502 set_capacity(card
->gendisk
, card
->size8
>> 9);
505 case CARD_STATE_FAULT
:
506 dev_crit(CARD_TO_DEV(card
),
507 "Hardware Fault reported!\n");
510 /* Everything else, detach DMA interface if it's attached. */
511 case CARD_STATE_SHUTDOWN
:
512 case CARD_STATE_STARTING
:
513 case CARD_STATE_FORMATTING
:
514 case CARD_STATE_UNINITIALIZED
:
515 case CARD_STATE_SHUTTING_DOWN
:
517 * dStroy is a term coined by marketing to represent the low level
520 case CARD_STATE_DSTROYING
:
521 set_capacity(card
->gendisk
, 0);
526 static void card_event_handler(struct work_struct
*work
)
528 struct rsxx_cardinfo
*card
;
533 card
= container_of(work
, struct rsxx_cardinfo
, event_work
);
535 if (unlikely(card
->halt
))
539 * Enable the interrupt now to avoid any weird race conditions where a
540 * state change might occur while rsxx_get_card_state() is
541 * processing a returned creg cmd.
543 spin_lock_irqsave(&card
->irq_lock
, flags
);
544 rsxx_enable_ier_and_isr(card
, CR_INTR_EVENT
);
545 spin_unlock_irqrestore(&card
->irq_lock
, flags
);
547 st
= rsxx_get_card_state(card
, &state
);
549 dev_info(CARD_TO_DEV(card
),
550 "Failed reading state after event.\n");
554 if (card
->state
!= state
)
555 card_state_change(card
, state
);
557 if (card
->creg_ctrl
.creg_stats
.stat
& CREG_STAT_LOG_PENDING
)
558 rsxx_read_hw_log(card
);
561 /*----------------- Card Operations -------------------*/
562 static int card_shutdown(struct rsxx_cardinfo
*card
)
566 const int timeout
= msecs_to_jiffies(120000);
569 /* We can't issue a shutdown if the card is in a transition state */
572 st
= rsxx_get_card_state(card
, &state
);
575 } while (state
== CARD_STATE_STARTING
&&
576 (jiffies
- start
< timeout
));
578 if (state
== CARD_STATE_STARTING
)
581 /* Only issue a shutdown if we need to */
582 if ((state
!= CARD_STATE_SHUTTING_DOWN
) &&
583 (state
!= CARD_STATE_SHUTDOWN
)) {
584 st
= rsxx_issue_card_cmd(card
, CARD_CMD_SHUTDOWN
);
591 st
= rsxx_get_card_state(card
, &state
);
594 } while (state
!= CARD_STATE_SHUTDOWN
&&
595 (jiffies
- start
< timeout
));
597 if (state
!= CARD_STATE_SHUTDOWN
)
603 static int rsxx_eeh_frozen(struct pci_dev
*dev
)
605 struct rsxx_cardinfo
*card
= pci_get_drvdata(dev
);
609 dev_warn(&dev
->dev
, "IBM Flash Adapter PCI: preparing for slot reset.\n");
612 rsxx_mask_interrupts(card
);
615 * We need to guarantee that the write for eeh_state and masking
616 * interrupts does not become reordered. This will prevent a possible
617 * race condition with the EEH code.
621 pci_disable_device(dev
);
623 st
= rsxx_eeh_save_issued_dmas(card
);
627 rsxx_eeh_save_issued_creg(card
);
629 for (i
= 0; i
< card
->n_targets
; i
++) {
630 if (card
->ctrl
[i
].status
.buf
)
631 pci_free_consistent(card
->dev
, STATUS_BUFFER_SIZE8
,
632 card
->ctrl
[i
].status
.buf
,
633 card
->ctrl
[i
].status
.dma_addr
);
634 if (card
->ctrl
[i
].cmd
.buf
)
635 pci_free_consistent(card
->dev
, COMMAND_BUFFER_SIZE8
,
636 card
->ctrl
[i
].cmd
.buf
,
637 card
->ctrl
[i
].cmd
.dma_addr
);
643 static void rsxx_eeh_failure(struct pci_dev
*dev
)
645 struct rsxx_cardinfo
*card
= pci_get_drvdata(dev
);
649 dev_err(&dev
->dev
, "IBM Flash Adapter PCI: disabling failed card.\n");
654 for (i
= 0; i
< card
->n_targets
; i
++) {
655 spin_lock_bh(&card
->ctrl
[i
].queue_lock
);
656 cnt
= rsxx_cleanup_dma_queue(&card
->ctrl
[i
],
657 &card
->ctrl
[i
].queue
);
658 spin_unlock_bh(&card
->ctrl
[i
].queue_lock
);
660 cnt
+= rsxx_dma_cancel(&card
->ctrl
[i
]);
663 dev_info(CARD_TO_DEV(card
),
664 "Freed %d queued DMAs on channel %d\n",
665 cnt
, card
->ctrl
[i
].id
);
669 static int rsxx_eeh_fifo_flush_poll(struct rsxx_cardinfo
*card
)
674 /* We need to wait for the hardware to reset */
675 while (iter
++ < 10) {
676 status
= ioread32(card
->regmap
+ PCI_RECONFIG
);
678 if (status
& RSXX_FLUSH_BUSY
) {
683 if (status
& RSXX_FLUSH_TIMEOUT
)
684 dev_warn(CARD_TO_DEV(card
), "HW: flash controller timeout\n");
688 /* Hardware failed resetting itself. */
692 static pci_ers_result_t
rsxx_error_detected(struct pci_dev
*dev
,
693 enum pci_channel_state error
)
697 if (dev
->revision
< RSXX_EEH_SUPPORT
)
698 return PCI_ERS_RESULT_NONE
;
700 if (error
== pci_channel_io_perm_failure
) {
701 rsxx_eeh_failure(dev
);
702 return PCI_ERS_RESULT_DISCONNECT
;
705 st
= rsxx_eeh_frozen(dev
);
707 dev_err(&dev
->dev
, "Slot reset setup failed\n");
708 rsxx_eeh_failure(dev
);
709 return PCI_ERS_RESULT_DISCONNECT
;
712 return PCI_ERS_RESULT_NEED_RESET
;
715 static pci_ers_result_t
rsxx_slot_reset(struct pci_dev
*dev
)
717 struct rsxx_cardinfo
*card
= pci_get_drvdata(dev
);
723 "IBM Flash Adapter PCI: recovering from slot reset.\n");
725 st
= pci_enable_device(dev
);
727 goto failed_hw_setup
;
731 st
= rsxx_eeh_fifo_flush_poll(card
);
733 goto failed_hw_setup
;
735 rsxx_dma_queue_reset(card
);
737 for (i
= 0; i
< card
->n_targets
; i
++) {
738 st
= rsxx_hw_buffers_init(dev
, &card
->ctrl
[i
]);
740 goto failed_hw_buffers_init
;
743 if (card
->config_valid
)
744 rsxx_dma_configure(card
);
746 /* Clears the ISR register from spurious interrupts */
747 st
= ioread32(card
->regmap
+ ISR
);
751 st
= rsxx_eeh_remap_dmas(card
);
753 goto failed_remap_dmas
;
755 spin_lock_irqsave(&card
->irq_lock
, flags
);
756 if (card
->n_targets
& RSXX_MAX_TARGETS
)
757 rsxx_enable_ier_and_isr(card
, CR_INTR_ALL_G
);
759 rsxx_enable_ier_and_isr(card
, CR_INTR_ALL_C
);
760 spin_unlock_irqrestore(&card
->irq_lock
, flags
);
762 rsxx_kick_creg_queue(card
);
764 for (i
= 0; i
< card
->n_targets
; i
++) {
765 spin_lock(&card
->ctrl
[i
].queue_lock
);
766 if (list_empty(&card
->ctrl
[i
].queue
)) {
767 spin_unlock(&card
->ctrl
[i
].queue_lock
);
770 spin_unlock(&card
->ctrl
[i
].queue_lock
);
772 queue_work(card
->ctrl
[i
].issue_wq
,
773 &card
->ctrl
[i
].issue_dma_work
);
776 dev_info(&dev
->dev
, "IBM Flash Adapter PCI: recovery complete.\n");
778 return PCI_ERS_RESULT_RECOVERED
;
780 failed_hw_buffers_init
:
782 for (i
= 0; i
< card
->n_targets
; i
++) {
783 if (card
->ctrl
[i
].status
.buf
)
784 pci_free_consistent(card
->dev
,
786 card
->ctrl
[i
].status
.buf
,
787 card
->ctrl
[i
].status
.dma_addr
);
788 if (card
->ctrl
[i
].cmd
.buf
)
789 pci_free_consistent(card
->dev
,
790 COMMAND_BUFFER_SIZE8
,
791 card
->ctrl
[i
].cmd
.buf
,
792 card
->ctrl
[i
].cmd
.dma_addr
);
795 rsxx_eeh_failure(dev
);
796 return PCI_ERS_RESULT_DISCONNECT
;
800 /*----------------- Driver Initialization & Setup -------------------*/
801 /* Returns: 0 if the driver is compatible with the device
802 -1 if the driver is NOT compatible with the device */
803 static int rsxx_compatibility_check(struct rsxx_cardinfo
*card
)
805 unsigned char pci_rev
;
807 pci_read_config_byte(card
->dev
, PCI_REVISION_ID
, &pci_rev
);
809 if (pci_rev
> RS70_PCI_REV_SUPPORTED
)
814 static int rsxx_pci_probe(struct pci_dev
*dev
,
815 const struct pci_device_id
*id
)
817 struct rsxx_cardinfo
*card
;
819 unsigned int sync_timeout
;
821 dev_info(&dev
->dev
, "PCI-Flash SSD discovered\n");
823 card
= kzalloc(sizeof(*card
), GFP_KERNEL
);
828 pci_set_drvdata(dev
, card
);
831 if (!ida_pre_get(&rsxx_disk_ida
, GFP_KERNEL
)) {
836 spin_lock(&rsxx_ida_lock
);
837 st
= ida_get_new(&rsxx_disk_ida
, &card
->disk_id
);
838 spin_unlock(&rsxx_ida_lock
);
839 } while (st
== -EAGAIN
);
844 st
= pci_enable_device(dev
);
849 pci_set_dma_max_seg_size(dev
, RSXX_HW_BLK_SIZE
);
851 st
= pci_set_dma_mask(dev
, DMA_BIT_MASK(64));
853 dev_err(CARD_TO_DEV(card
),
854 "No usable DMA configuration,aborting\n");
855 goto failed_dma_mask
;
858 st
= pci_request_regions(dev
, DRIVER_NAME
);
860 dev_err(CARD_TO_DEV(card
),
861 "Failed to request memory region\n");
862 goto failed_request_regions
;
865 if (pci_resource_len(dev
, 0) == 0) {
866 dev_err(CARD_TO_DEV(card
), "BAR0 has length 0!\n");
871 card
->regmap
= pci_iomap(dev
, 0, 0);
873 dev_err(CARD_TO_DEV(card
), "Failed to map BAR0\n");
878 spin_lock_init(&card
->irq_lock
);
882 spin_lock_irq(&card
->irq_lock
);
883 rsxx_disable_ier_and_isr(card
, CR_INTR_ALL
);
884 spin_unlock_irq(&card
->irq_lock
);
887 st
= pci_enable_msi(dev
);
889 dev_warn(CARD_TO_DEV(card
),
890 "Failed to enable MSI\n");
893 st
= request_irq(dev
->irq
, rsxx_isr
, IRQF_DISABLED
| IRQF_SHARED
,
896 dev_err(CARD_TO_DEV(card
),
897 "Failed requesting IRQ%d\n", dev
->irq
);
901 /************* Setup Processor Command Interface *************/
902 st
= rsxx_creg_setup(card
);
904 dev_err(CARD_TO_DEV(card
), "Failed to setup creg interface.\n");
905 goto failed_creg_setup
;
908 spin_lock_irq(&card
->irq_lock
);
909 rsxx_enable_ier_and_isr(card
, CR_INTR_CREG
);
910 spin_unlock_irq(&card
->irq_lock
);
912 st
= rsxx_compatibility_check(card
);
914 dev_warn(CARD_TO_DEV(card
),
915 "Incompatible driver detected. Please update the driver.\n");
917 goto failed_compatiblity_check
;
920 /************* Load Card Config *************/
921 st
= rsxx_load_config(card
);
923 dev_err(CARD_TO_DEV(card
),
924 "Failed loading card config\n");
926 /************* Setup DMA Engine *************/
927 st
= rsxx_get_num_targets(card
, &card
->n_targets
);
929 dev_info(CARD_TO_DEV(card
),
930 "Failed reading the number of DMA targets\n");
932 card
->ctrl
= kzalloc(card
->n_targets
* sizeof(*card
->ctrl
), GFP_KERNEL
);
935 goto failed_dma_setup
;
938 st
= rsxx_dma_setup(card
);
940 dev_info(CARD_TO_DEV(card
),
941 "Failed to setup DMA engine\n");
942 goto failed_dma_setup
;
945 /************* Setup Card Event Handler *************/
946 card
->event_wq
= create_singlethread_workqueue(DRIVER_NAME
"_event");
947 if (!card
->event_wq
) {
948 dev_err(CARD_TO_DEV(card
), "Failed card event setup.\n");
949 goto failed_event_handler
;
952 INIT_WORK(&card
->event_work
, card_event_handler
);
954 st
= rsxx_setup_dev(card
);
956 goto failed_create_dev
;
958 rsxx_get_card_state(card
, &card
->state
);
960 dev_info(CARD_TO_DEV(card
),
962 rsxx_card_state_to_str(card
->state
));
965 * Now that the DMA Engine and devices have been setup,
966 * we can enable the event interrupt(it kicks off actions in
967 * those layers so we couldn't enable it right away.)
969 spin_lock_irq(&card
->irq_lock
);
970 rsxx_enable_ier_and_isr(card
, CR_INTR_EVENT
);
971 spin_unlock_irq(&card
->irq_lock
);
973 if (card
->state
== CARD_STATE_SHUTDOWN
) {
974 st
= rsxx_issue_card_cmd(card
, CARD_CMD_STARTUP
);
976 dev_crit(CARD_TO_DEV(card
),
977 "Failed issuing card startup\n");
979 sync_timeout
= SYNC_START_TIMEOUT
;
981 dev_info(CARD_TO_DEV(card
),
982 "Waiting for card to startup\n");
988 rsxx_get_card_state(card
, &card
->state
);
989 } while (sync_timeout
&&
990 (card
->state
== CARD_STATE_STARTING
));
992 if (card
->state
== CARD_STATE_STARTING
) {
993 dev_warn(CARD_TO_DEV(card
),
994 "Card startup timed out\n");
997 dev_info(CARD_TO_DEV(card
),
999 rsxx_card_state_to_str(card
->state
));
1000 st
= rsxx_get_card_size8(card
, &card
->size8
);
1005 } else if (card
->state
== CARD_STATE_GOOD
||
1006 card
->state
== CARD_STATE_RD_ONLY_FAULT
) {
1007 st
= rsxx_get_card_size8(card
, &card
->size8
);
1012 rsxx_attach_dev(card
);
1014 /************* Setup Debugfs *************/
1015 rsxx_debugfs_dev_new(card
);
1020 destroy_workqueue(card
->event_wq
);
1021 card
->event_wq
= NULL
;
1022 failed_event_handler
:
1023 rsxx_dma_destroy(card
);
1025 failed_compatiblity_check
:
1026 destroy_workqueue(card
->creg_ctrl
.creg_wq
);
1027 card
->creg_ctrl
.creg_wq
= NULL
;
1029 spin_lock_irq(&card
->irq_lock
);
1030 rsxx_disable_ier_and_isr(card
, CR_INTR_ALL
);
1031 spin_unlock_irq(&card
->irq_lock
);
1032 free_irq(dev
->irq
, card
);
1034 pci_disable_msi(dev
);
1036 pci_iounmap(dev
, card
->regmap
);
1038 pci_release_regions(dev
);
1039 failed_request_regions
:
1041 pci_disable_device(dev
);
1043 spin_lock(&rsxx_ida_lock
);
1044 ida_remove(&rsxx_disk_ida
, card
->disk_id
);
1045 spin_unlock(&rsxx_ida_lock
);
1052 static void rsxx_pci_remove(struct pci_dev
*dev
)
1054 struct rsxx_cardinfo
*card
= pci_get_drvdata(dev
);
1055 unsigned long flags
;
1062 dev_info(CARD_TO_DEV(card
),
1063 "Removing PCI-Flash SSD.\n");
1065 rsxx_detach_dev(card
);
1067 for (i
= 0; i
< card
->n_targets
; i
++) {
1068 spin_lock_irqsave(&card
->irq_lock
, flags
);
1069 rsxx_disable_ier_and_isr(card
, CR_INTR_DMA(i
));
1070 spin_unlock_irqrestore(&card
->irq_lock
, flags
);
1073 st
= card_shutdown(card
);
1075 dev_crit(CARD_TO_DEV(card
), "Shutdown failed!\n");
1077 /* Sync outstanding event handlers. */
1078 spin_lock_irqsave(&card
->irq_lock
, flags
);
1079 rsxx_disable_ier_and_isr(card
, CR_INTR_EVENT
);
1080 spin_unlock_irqrestore(&card
->irq_lock
, flags
);
1082 cancel_work_sync(&card
->event_work
);
1084 rsxx_destroy_dev(card
);
1085 rsxx_dma_destroy(card
);
1087 spin_lock_irqsave(&card
->irq_lock
, flags
);
1088 rsxx_disable_ier_and_isr(card
, CR_INTR_ALL
);
1089 spin_unlock_irqrestore(&card
->irq_lock
, flags
);
1091 /* Prevent work_structs from re-queuing themselves. */
1094 debugfs_remove_recursive(card
->debugfs_dir
);
1096 free_irq(dev
->irq
, card
);
1099 pci_disable_msi(dev
);
1101 rsxx_creg_destroy(card
);
1103 pci_iounmap(dev
, card
->regmap
);
1105 pci_disable_device(dev
);
1106 pci_release_regions(dev
);
1111 static int rsxx_pci_suspend(struct pci_dev
*dev
, pm_message_t state
)
1113 /* We don't support suspend at this time. */
1117 static void rsxx_pci_shutdown(struct pci_dev
*dev
)
1119 struct rsxx_cardinfo
*card
= pci_get_drvdata(dev
);
1120 unsigned long flags
;
1126 dev_info(CARD_TO_DEV(card
), "Shutting down PCI-Flash SSD.\n");
1128 rsxx_detach_dev(card
);
1130 for (i
= 0; i
< card
->n_targets
; i
++) {
1131 spin_lock_irqsave(&card
->irq_lock
, flags
);
1132 rsxx_disable_ier_and_isr(card
, CR_INTR_DMA(i
));
1133 spin_unlock_irqrestore(&card
->irq_lock
, flags
);
1136 card_shutdown(card
);
1139 static const struct pci_error_handlers rsxx_err_handler
= {
1140 .error_detected
= rsxx_error_detected
,
1141 .slot_reset
= rsxx_slot_reset
,
1144 static DEFINE_PCI_DEVICE_TABLE(rsxx_pci_ids
) = {
1145 {PCI_DEVICE(PCI_VENDOR_ID_IBM
, PCI_DEVICE_ID_FS70_FLASH
)},
1146 {PCI_DEVICE(PCI_VENDOR_ID_IBM
, PCI_DEVICE_ID_FS80_FLASH
)},
1150 MODULE_DEVICE_TABLE(pci
, rsxx_pci_ids
);
1152 static struct pci_driver rsxx_pci_driver
= {
1153 .name
= DRIVER_NAME
,
1154 .id_table
= rsxx_pci_ids
,
1155 .probe
= rsxx_pci_probe
,
1156 .remove
= rsxx_pci_remove
,
1157 .suspend
= rsxx_pci_suspend
,
1158 .shutdown
= rsxx_pci_shutdown
,
1159 .err_handler
= &rsxx_err_handler
,
1162 static int __init
rsxx_core_init(void)
1166 st
= rsxx_dev_init();
1170 st
= rsxx_dma_init();
1172 goto dma_init_failed
;
1174 st
= rsxx_creg_init();
1176 goto creg_init_failed
;
1178 return pci_register_driver(&rsxx_pci_driver
);
1188 static void __exit
rsxx_core_cleanup(void)
1190 pci_unregister_driver(&rsxx_pci_driver
);
1191 rsxx_creg_cleanup();
1196 module_init(rsxx_core_init
);
1197 module_exit(rsxx_core_cleanup
);