GNU Linux-libre 4.9.309-gnu1
[releases.git] / drivers / block / rsxx / core.c
1 /*
2 * Filename: core.c
3 *
4 *
5 * Authors: Joshua Morris <josh.h.morris@us.ibm.com>
6 *       Philip Kelleher <pjk1939@linux.vnet.ibm.com>
7 *
8 * (C) Copyright 2013 IBM Corporation
9 *
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.
14 *
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.
19 *
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
23 */
24
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>
36
37 #include <linux/genhd.h>
38 #include <linux/idr.h>
39
40 #include "rsxx_priv.h"
41 #include "rsxx_cfg.h"
42
43 #define NO_LEGACY 0
44 #define SYNC_START_TIMEOUT (10 * 60) /* 10 minutes */
45
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);
50
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");
54
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.");
59
60 static DEFINE_IDA(rsxx_disk_ida);
61 static DEFINE_SPINLOCK(rsxx_ida_lock);
62
63 /* --------------------Debugfs Setup ------------------- */
64
65 static int rsxx_attr_pci_regs_show(struct seq_file *m, void *p)
66 {
67         struct rsxx_cardinfo *card = m->private;
68
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));
123
124         return 0;
125 }
126
127 static int rsxx_attr_stats_show(struct seq_file *m, void *p)
128 {
129         struct rsxx_cardinfo *card = m->private;
130         int i;
131
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));
163         }
164
165         return 0;
166 }
167
168 static int rsxx_attr_stats_open(struct inode *inode, struct file *file)
169 {
170         return single_open(file, rsxx_attr_stats_show, inode->i_private);
171 }
172
173 static int rsxx_attr_pci_regs_open(struct inode *inode, struct file *file)
174 {
175         return single_open(file, rsxx_attr_pci_regs_show, inode->i_private);
176 }
177
178 static ssize_t rsxx_cram_read(struct file *fp, char __user *ubuf,
179                               size_t cnt, loff_t *ppos)
180 {
181         struct rsxx_cardinfo *card = file_inode(fp)->i_private;
182         char *buf;
183         int st;
184
185         buf = kzalloc(cnt, GFP_KERNEL);
186         if (!buf)
187                 return -ENOMEM;
188
189         st = rsxx_creg_read(card, CREG_ADD_CRAM + (u32)*ppos, cnt, buf, 1);
190         if (!st) {
191                 if (copy_to_user(ubuf, buf, cnt))
192                         st = -EFAULT;
193         }
194         kfree(buf);
195         if (st)
196                 return st;
197         *ppos += cnt;
198         return cnt;
199 }
200
201 static ssize_t rsxx_cram_write(struct file *fp, const char __user *ubuf,
202                                size_t cnt, loff_t *ppos)
203 {
204         struct rsxx_cardinfo *card = file_inode(fp)->i_private;
205         char *buf;
206         ssize_t st;
207
208         buf = memdup_user(ubuf, cnt);
209         if (IS_ERR(buf))
210                 return PTR_ERR(buf);
211
212         st = rsxx_creg_write(card, CREG_ADD_CRAM + (u32)*ppos, cnt, buf, 1);
213         kfree(buf);
214         if (st)
215                 return st;
216         *ppos += cnt;
217         return cnt;
218 }
219
220 static const struct file_operations debugfs_cram_fops = {
221         .owner          = THIS_MODULE,
222         .read           = rsxx_cram_read,
223         .write          = rsxx_cram_write,
224 };
225
226 static const struct file_operations debugfs_stats_fops = {
227         .owner          = THIS_MODULE,
228         .open           = rsxx_attr_stats_open,
229         .read           = seq_read,
230         .llseek         = seq_lseek,
231         .release        = single_release,
232 };
233
234 static const struct file_operations debugfs_pci_regs_fops = {
235         .owner          = THIS_MODULE,
236         .open           = rsxx_attr_pci_regs_open,
237         .read           = seq_read,
238         .llseek         = seq_lseek,
239         .release        = single_release,
240 };
241
242 static void rsxx_debugfs_dev_new(struct rsxx_cardinfo *card)
243 {
244         struct dentry *debugfs_stats;
245         struct dentry *debugfs_pci_regs;
246         struct dentry *debugfs_cram;
247
248         card->debugfs_dir = debugfs_create_dir(card->gendisk->disk_name, NULL);
249         if (IS_ERR_OR_NULL(card->debugfs_dir))
250                 goto failed_debugfs_dir;
251
252         debugfs_stats = debugfs_create_file("stats", S_IRUGO,
253                                             card->debugfs_dir, card,
254                                             &debugfs_stats_fops);
255         if (IS_ERR_OR_NULL(debugfs_stats))
256                 goto failed_debugfs_stats;
257
258         debugfs_pci_regs = debugfs_create_file("pci_regs", S_IRUGO,
259                                                card->debugfs_dir, card,
260                                                &debugfs_pci_regs_fops);
261         if (IS_ERR_OR_NULL(debugfs_pci_regs))
262                 goto failed_debugfs_pci_regs;
263
264         debugfs_cram = debugfs_create_file("cram", S_IRUGO | S_IWUSR,
265                                            card->debugfs_dir, card,
266                                            &debugfs_cram_fops);
267         if (IS_ERR_OR_NULL(debugfs_cram))
268                 goto failed_debugfs_cram;
269
270         return;
271 failed_debugfs_cram:
272         debugfs_remove(debugfs_pci_regs);
273 failed_debugfs_pci_regs:
274         debugfs_remove(debugfs_stats);
275 failed_debugfs_stats:
276         debugfs_remove(card->debugfs_dir);
277 failed_debugfs_dir:
278         card->debugfs_dir = NULL;
279 }
280
281 /*----------------- Interrupt Control & Handling -------------------*/
282
283 static void rsxx_mask_interrupts(struct rsxx_cardinfo *card)
284 {
285         card->isr_mask = 0;
286         card->ier_mask = 0;
287 }
288
289 static void __enable_intr(unsigned int *mask, unsigned int intr)
290 {
291         *mask |= intr;
292 }
293
294 static void __disable_intr(unsigned int *mask, unsigned int intr)
295 {
296         *mask &= ~intr;
297 }
298
299 /*
300  * NOTE: Disabling the IER will disable the hardware interrupt.
301  * Disabling the ISR will disable the software handling of the ISR bit.
302  *
303  * Enable/Disable interrupt functions assume the card->irq_lock
304  * is held by the caller.
305  */
306 void rsxx_enable_ier(struct rsxx_cardinfo *card, unsigned int intr)
307 {
308         if (unlikely(card->halt) ||
309             unlikely(card->eeh_state))
310                 return;
311
312         __enable_intr(&card->ier_mask, intr);
313         iowrite32(card->ier_mask, card->regmap + IER);
314 }
315
316 void rsxx_disable_ier(struct rsxx_cardinfo *card, unsigned int intr)
317 {
318         if (unlikely(card->eeh_state))
319                 return;
320
321         __disable_intr(&card->ier_mask, intr);
322         iowrite32(card->ier_mask, card->regmap + IER);
323 }
324
325 void rsxx_enable_ier_and_isr(struct rsxx_cardinfo *card,
326                                  unsigned int intr)
327 {
328         if (unlikely(card->halt) ||
329             unlikely(card->eeh_state))
330                 return;
331
332         __enable_intr(&card->isr_mask, intr);
333         __enable_intr(&card->ier_mask, intr);
334         iowrite32(card->ier_mask, card->regmap + IER);
335 }
336 void rsxx_disable_ier_and_isr(struct rsxx_cardinfo *card,
337                                   unsigned int intr)
338 {
339         if (unlikely(card->eeh_state))
340                 return;
341
342         __disable_intr(&card->isr_mask, intr);
343         __disable_intr(&card->ier_mask, intr);
344         iowrite32(card->ier_mask, card->regmap + IER);
345 }
346
347 static irqreturn_t rsxx_isr(int irq, void *pdata)
348 {
349         struct rsxx_cardinfo *card = pdata;
350         unsigned int isr;
351         int handled = 0;
352         int reread_isr;
353         int i;
354
355         spin_lock(&card->irq_lock);
356
357         do {
358                 reread_isr = 0;
359
360                 if (unlikely(card->eeh_state))
361                         break;
362
363                 isr = ioread32(card->regmap + ISR);
364                 if (isr == 0xffffffff) {
365                         /*
366                          * A few systems seem to have an intermittent issue
367                          * where PCI reads return all Fs, but retrying the read
368                          * a little later will return as expected.
369                          */
370                         dev_info(CARD_TO_DEV(card),
371                                 "ISR = 0xFFFFFFFF, retrying later\n");
372                         break;
373                 }
374
375                 isr &= card->isr_mask;
376                 if (!isr)
377                         break;
378
379                 for (i = 0; i < card->n_targets; i++) {
380                         if (isr & CR_INTR_DMA(i)) {
381                                 if (card->ier_mask & CR_INTR_DMA(i)) {
382                                         rsxx_disable_ier(card, CR_INTR_DMA(i));
383                                         reread_isr = 1;
384                                 }
385                                 queue_work(card->ctrl[i].done_wq,
386                                            &card->ctrl[i].dma_done_work);
387                                 handled++;
388                         }
389                 }
390
391                 if (isr & CR_INTR_CREG) {
392                         queue_work(card->creg_ctrl.creg_wq,
393                                    &card->creg_ctrl.done_work);
394                         handled++;
395                 }
396
397                 if (isr & CR_INTR_EVENT) {
398                         queue_work(card->event_wq, &card->event_work);
399                         rsxx_disable_ier_and_isr(card, CR_INTR_EVENT);
400                         handled++;
401                 }
402         } while (reread_isr);
403
404         spin_unlock(&card->irq_lock);
405
406         return handled ? IRQ_HANDLED : IRQ_NONE;
407 }
408
409 /*----------------- Card Event Handler -------------------*/
410 static const char * const rsxx_card_state_to_str(unsigned int state)
411 {
412         static const char * const state_strings[] = {
413                 "Unknown", "Shutdown", "Starting", "Formatting",
414                 "Uninitialized", "Good", "Shutting Down",
415                 "Fault", "Read Only Fault", "dStroying"
416         };
417
418         return state_strings[ffs(state)];
419 }
420
421 static void card_state_change(struct rsxx_cardinfo *card,
422                               unsigned int new_state)
423 {
424         int st;
425
426         dev_info(CARD_TO_DEV(card),
427                 "card state change detected.(%s -> %s)\n",
428                 rsxx_card_state_to_str(card->state),
429                 rsxx_card_state_to_str(new_state));
430
431         card->state = new_state;
432
433         /* Don't attach DMA interfaces if the card has an invalid config */
434         if (!card->config_valid)
435                 return;
436
437         switch (new_state) {
438         case CARD_STATE_RD_ONLY_FAULT:
439                 dev_crit(CARD_TO_DEV(card),
440                         "Hardware has entered read-only mode!\n");
441                 /*
442                  * Fall through so the DMA devices can be attached and
443                  * the user can attempt to pull off their data.
444                  */
445         case CARD_STATE_GOOD:
446                 st = rsxx_get_card_size8(card, &card->size8);
447                 if (st)
448                         dev_err(CARD_TO_DEV(card),
449                                 "Failed attaching DMA devices\n");
450
451                 if (card->config_valid)
452                         set_capacity(card->gendisk, card->size8 >> 9);
453                 break;
454
455         case CARD_STATE_FAULT:
456                 dev_crit(CARD_TO_DEV(card),
457                         "Hardware Fault reported!\n");
458                 /* Fall through. */
459
460         /* Everything else, detach DMA interface if it's attached. */
461         case CARD_STATE_SHUTDOWN:
462         case CARD_STATE_STARTING:
463         case CARD_STATE_FORMATTING:
464         case CARD_STATE_UNINITIALIZED:
465         case CARD_STATE_SHUTTING_DOWN:
466         /*
467          * dStroy is a term coined by marketing to represent the low level
468          * secure erase.
469          */
470         case CARD_STATE_DSTROYING:
471                 set_capacity(card->gendisk, 0);
472                 break;
473         }
474 }
475
476 static void card_event_handler(struct work_struct *work)
477 {
478         struct rsxx_cardinfo *card;
479         unsigned int state;
480         unsigned long flags;
481         int st;
482
483         card = container_of(work, struct rsxx_cardinfo, event_work);
484
485         if (unlikely(card->halt))
486                 return;
487
488         /*
489          * Enable the interrupt now to avoid any weird race conditions where a
490          * state change might occur while rsxx_get_card_state() is
491          * processing a returned creg cmd.
492          */
493         spin_lock_irqsave(&card->irq_lock, flags);
494         rsxx_enable_ier_and_isr(card, CR_INTR_EVENT);
495         spin_unlock_irqrestore(&card->irq_lock, flags);
496
497         st = rsxx_get_card_state(card, &state);
498         if (st) {
499                 dev_info(CARD_TO_DEV(card),
500                         "Failed reading state after event.\n");
501                 return;
502         }
503
504         if (card->state != state)
505                 card_state_change(card, state);
506
507         if (card->creg_ctrl.creg_stats.stat & CREG_STAT_LOG_PENDING)
508                 rsxx_read_hw_log(card);
509 }
510
511 /*----------------- Card Operations -------------------*/
512 static int card_shutdown(struct rsxx_cardinfo *card)
513 {
514         unsigned int state;
515         signed long start;
516         const int timeout = msecs_to_jiffies(120000);
517         int st;
518
519         /* We can't issue a shutdown if the card is in a transition state */
520         start = jiffies;
521         do {
522                 st = rsxx_get_card_state(card, &state);
523                 if (st)
524                         return st;
525         } while (state == CARD_STATE_STARTING &&
526                  (jiffies - start < timeout));
527
528         if (state == CARD_STATE_STARTING)
529                 return -ETIMEDOUT;
530
531         /* Only issue a shutdown if we need to */
532         if ((state != CARD_STATE_SHUTTING_DOWN) &&
533             (state != CARD_STATE_SHUTDOWN)) {
534                 st = rsxx_issue_card_cmd(card, CARD_CMD_SHUTDOWN);
535                 if (st)
536                         return st;
537         }
538
539         start = jiffies;
540         do {
541                 st = rsxx_get_card_state(card, &state);
542                 if (st)
543                         return st;
544         } while (state != CARD_STATE_SHUTDOWN &&
545                  (jiffies - start < timeout));
546
547         if (state != CARD_STATE_SHUTDOWN)
548                 return -ETIMEDOUT;
549
550         return 0;
551 }
552
553 static int rsxx_eeh_frozen(struct pci_dev *dev)
554 {
555         struct rsxx_cardinfo *card = pci_get_drvdata(dev);
556         int i;
557         int st;
558
559         dev_warn(&dev->dev, "IBM Flash Adapter PCI: preparing for slot reset.\n");
560
561         card->eeh_state = 1;
562         rsxx_mask_interrupts(card);
563
564         /*
565          * We need to guarantee that the write for eeh_state and masking
566          * interrupts does not become reordered. This will prevent a possible
567          * race condition with the EEH code.
568          */
569         wmb();
570
571         pci_disable_device(dev);
572
573         st = rsxx_eeh_save_issued_dmas(card);
574         if (st)
575                 return st;
576
577         rsxx_eeh_save_issued_creg(card);
578
579         for (i = 0; i < card->n_targets; i++) {
580                 if (card->ctrl[i].status.buf)
581                         pci_free_consistent(card->dev, STATUS_BUFFER_SIZE8,
582                                             card->ctrl[i].status.buf,
583                                             card->ctrl[i].status.dma_addr);
584                 if (card->ctrl[i].cmd.buf)
585                         pci_free_consistent(card->dev, COMMAND_BUFFER_SIZE8,
586                                             card->ctrl[i].cmd.buf,
587                                             card->ctrl[i].cmd.dma_addr);
588         }
589
590         return 0;
591 }
592
593 static void rsxx_eeh_failure(struct pci_dev *dev)
594 {
595         struct rsxx_cardinfo *card = pci_get_drvdata(dev);
596         int i;
597         int cnt = 0;
598
599         dev_err(&dev->dev, "IBM Flash Adapter PCI: disabling failed card.\n");
600
601         card->eeh_state = 1;
602         card->halt = 1;
603
604         for (i = 0; i < card->n_targets; i++) {
605                 spin_lock_bh(&card->ctrl[i].queue_lock);
606                 cnt = rsxx_cleanup_dma_queue(&card->ctrl[i],
607                                              &card->ctrl[i].queue,
608                                              COMPLETE_DMA);
609                 spin_unlock_bh(&card->ctrl[i].queue_lock);
610
611                 cnt += rsxx_dma_cancel(&card->ctrl[i]);
612
613                 if (cnt)
614                         dev_info(CARD_TO_DEV(card),
615                                 "Freed %d queued DMAs on channel %d\n",
616                                 cnt, card->ctrl[i].id);
617         }
618 }
619
620 static int rsxx_eeh_fifo_flush_poll(struct rsxx_cardinfo *card)
621 {
622         unsigned int status;
623         int iter = 0;
624
625         /* We need to wait for the hardware to reset */
626         while (iter++ < 10) {
627                 status = ioread32(card->regmap + PCI_RECONFIG);
628
629                 if (status & RSXX_FLUSH_BUSY) {
630                         ssleep(1);
631                         continue;
632                 }
633
634                 if (status & RSXX_FLUSH_TIMEOUT)
635                         dev_warn(CARD_TO_DEV(card), "HW: flash controller timeout\n");
636                 return 0;
637         }
638
639         /* Hardware failed resetting itself. */
640         return -1;
641 }
642
643 static pci_ers_result_t rsxx_error_detected(struct pci_dev *dev,
644                                             enum pci_channel_state error)
645 {
646         int st;
647
648         if (dev->revision < RSXX_EEH_SUPPORT)
649                 return PCI_ERS_RESULT_NONE;
650
651         if (error == pci_channel_io_perm_failure) {
652                 rsxx_eeh_failure(dev);
653                 return PCI_ERS_RESULT_DISCONNECT;
654         }
655
656         st = rsxx_eeh_frozen(dev);
657         if (st) {
658                 dev_err(&dev->dev, "Slot reset setup failed\n");
659                 rsxx_eeh_failure(dev);
660                 return PCI_ERS_RESULT_DISCONNECT;
661         }
662
663         return PCI_ERS_RESULT_NEED_RESET;
664 }
665
666 static pci_ers_result_t rsxx_slot_reset(struct pci_dev *dev)
667 {
668         struct rsxx_cardinfo *card = pci_get_drvdata(dev);
669         unsigned long flags;
670         int i;
671         int st;
672
673         dev_warn(&dev->dev,
674                 "IBM Flash Adapter PCI: recovering from slot reset.\n");
675
676         st = pci_enable_device(dev);
677         if (st)
678                 goto failed_hw_setup;
679
680         pci_set_master(dev);
681
682         st = rsxx_eeh_fifo_flush_poll(card);
683         if (st)
684                 goto failed_hw_setup;
685
686         rsxx_dma_queue_reset(card);
687
688         for (i = 0; i < card->n_targets; i++) {
689                 st = rsxx_hw_buffers_init(dev, &card->ctrl[i]);
690                 if (st)
691                         goto failed_hw_buffers_init;
692         }
693
694         if (card->config_valid)
695                 rsxx_dma_configure(card);
696
697         /* Clears the ISR register from spurious interrupts */
698         st = ioread32(card->regmap + ISR);
699
700         card->eeh_state = 0;
701
702         spin_lock_irqsave(&card->irq_lock, flags);
703         if (card->n_targets & RSXX_MAX_TARGETS)
704                 rsxx_enable_ier_and_isr(card, CR_INTR_ALL_G);
705         else
706                 rsxx_enable_ier_and_isr(card, CR_INTR_ALL_C);
707         spin_unlock_irqrestore(&card->irq_lock, flags);
708
709         rsxx_kick_creg_queue(card);
710
711         for (i = 0; i < card->n_targets; i++) {
712                 spin_lock(&card->ctrl[i].queue_lock);
713                 if (list_empty(&card->ctrl[i].queue)) {
714                         spin_unlock(&card->ctrl[i].queue_lock);
715                         continue;
716                 }
717                 spin_unlock(&card->ctrl[i].queue_lock);
718
719                 queue_work(card->ctrl[i].issue_wq,
720                                 &card->ctrl[i].issue_dma_work);
721         }
722
723         dev_info(&dev->dev, "IBM Flash Adapter PCI: recovery complete.\n");
724
725         return PCI_ERS_RESULT_RECOVERED;
726
727 failed_hw_buffers_init:
728         for (i = 0; i < card->n_targets; i++) {
729                 if (card->ctrl[i].status.buf)
730                         pci_free_consistent(card->dev,
731                                         STATUS_BUFFER_SIZE8,
732                                         card->ctrl[i].status.buf,
733                                         card->ctrl[i].status.dma_addr);
734                 if (card->ctrl[i].cmd.buf)
735                         pci_free_consistent(card->dev,
736                                         COMMAND_BUFFER_SIZE8,
737                                         card->ctrl[i].cmd.buf,
738                                         card->ctrl[i].cmd.dma_addr);
739         }
740 failed_hw_setup:
741         rsxx_eeh_failure(dev);
742         return PCI_ERS_RESULT_DISCONNECT;
743
744 }
745
746 /*----------------- Driver Initialization & Setup -------------------*/
747 /* Returns:   0 if the driver is compatible with the device
748              -1 if the driver is NOT compatible with the device */
749 static int rsxx_compatibility_check(struct rsxx_cardinfo *card)
750 {
751         unsigned char pci_rev;
752
753         pci_read_config_byte(card->dev, PCI_REVISION_ID, &pci_rev);
754
755         if (pci_rev > RS70_PCI_REV_SUPPORTED)
756                 return -1;
757         return 0;
758 }
759
760 static int rsxx_pci_probe(struct pci_dev *dev,
761                                         const struct pci_device_id *id)
762 {
763         struct rsxx_cardinfo *card;
764         int st;
765         unsigned int sync_timeout;
766
767         dev_info(&dev->dev, "PCI-Flash SSD discovered\n");
768
769         card = kzalloc(sizeof(*card), GFP_KERNEL);
770         if (!card)
771                 return -ENOMEM;
772
773         card->dev = dev;
774         pci_set_drvdata(dev, card);
775
776         do {
777                 if (!ida_pre_get(&rsxx_disk_ida, GFP_KERNEL)) {
778                         st = -ENOMEM;
779                         goto failed_ida_get;
780                 }
781
782                 spin_lock(&rsxx_ida_lock);
783                 st = ida_get_new(&rsxx_disk_ida, &card->disk_id);
784                 spin_unlock(&rsxx_ida_lock);
785         } while (st == -EAGAIN);
786
787         if (st)
788                 goto failed_ida_get;
789
790         st = pci_enable_device(dev);
791         if (st)
792                 goto failed_enable;
793
794         pci_set_master(dev);
795         pci_set_dma_max_seg_size(dev, RSXX_HW_BLK_SIZE);
796
797         st = pci_set_dma_mask(dev, DMA_BIT_MASK(64));
798         if (st) {
799                 dev_err(CARD_TO_DEV(card),
800                         "No usable DMA configuration,aborting\n");
801                 goto failed_dma_mask;
802         }
803
804         st = pci_request_regions(dev, DRIVER_NAME);
805         if (st) {
806                 dev_err(CARD_TO_DEV(card),
807                         "Failed to request memory region\n");
808                 goto failed_request_regions;
809         }
810
811         if (pci_resource_len(dev, 0) == 0) {
812                 dev_err(CARD_TO_DEV(card), "BAR0 has length 0!\n");
813                 st = -ENOMEM;
814                 goto failed_iomap;
815         }
816
817         card->regmap = pci_iomap(dev, 0, 0);
818         if (!card->regmap) {
819                 dev_err(CARD_TO_DEV(card), "Failed to map BAR0\n");
820                 st = -ENOMEM;
821                 goto failed_iomap;
822         }
823
824         spin_lock_init(&card->irq_lock);
825         card->halt = 0;
826         card->eeh_state = 0;
827
828         spin_lock_irq(&card->irq_lock);
829         rsxx_disable_ier_and_isr(card, CR_INTR_ALL);
830         spin_unlock_irq(&card->irq_lock);
831
832         if (!force_legacy) {
833                 st = pci_enable_msi(dev);
834                 if (st)
835                         dev_warn(CARD_TO_DEV(card),
836                                 "Failed to enable MSI\n");
837         }
838
839         st = request_irq(dev->irq, rsxx_isr, IRQF_SHARED,
840                          DRIVER_NAME, card);
841         if (st) {
842                 dev_err(CARD_TO_DEV(card),
843                         "Failed requesting IRQ%d\n", dev->irq);
844                 goto failed_irq;
845         }
846
847         /************* Setup Processor Command Interface *************/
848         st = rsxx_creg_setup(card);
849         if (st) {
850                 dev_err(CARD_TO_DEV(card), "Failed to setup creg interface.\n");
851                 goto failed_creg_setup;
852         }
853
854         spin_lock_irq(&card->irq_lock);
855         rsxx_enable_ier_and_isr(card, CR_INTR_CREG);
856         spin_unlock_irq(&card->irq_lock);
857
858         st = rsxx_compatibility_check(card);
859         if (st) {
860                 dev_warn(CARD_TO_DEV(card),
861                         "Incompatible driver detected. Please update the driver.\n");
862                 st = -EINVAL;
863                 goto failed_compatiblity_check;
864         }
865
866         /************* Load Card Config *************/
867         st = rsxx_load_config(card);
868         if (st)
869                 dev_err(CARD_TO_DEV(card),
870                         "Failed loading card config\n");
871
872         /************* Setup DMA Engine *************/
873         st = rsxx_get_num_targets(card, &card->n_targets);
874         if (st)
875                 dev_info(CARD_TO_DEV(card),
876                         "Failed reading the number of DMA targets\n");
877
878         card->ctrl = kzalloc(card->n_targets * sizeof(*card->ctrl), GFP_KERNEL);
879         if (!card->ctrl) {
880                 st = -ENOMEM;
881                 goto failed_dma_setup;
882         }
883
884         st = rsxx_dma_setup(card);
885         if (st) {
886                 dev_info(CARD_TO_DEV(card),
887                         "Failed to setup DMA engine\n");
888                 goto failed_dma_setup;
889         }
890
891         /************* Setup Card Event Handler *************/
892         card->event_wq = create_singlethread_workqueue(DRIVER_NAME"_event");
893         if (!card->event_wq) {
894                 dev_err(CARD_TO_DEV(card), "Failed card event setup.\n");
895                 st = -ENOMEM;
896                 goto failed_event_handler;
897         }
898
899         INIT_WORK(&card->event_work, card_event_handler);
900
901         st = rsxx_setup_dev(card);
902         if (st)
903                 goto failed_create_dev;
904
905         rsxx_get_card_state(card, &card->state);
906
907         dev_info(CARD_TO_DEV(card),
908                 "card state: %s\n",
909                 rsxx_card_state_to_str(card->state));
910
911         /*
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.)
915          */
916         spin_lock_irq(&card->irq_lock);
917         rsxx_enable_ier_and_isr(card, CR_INTR_EVENT);
918         spin_unlock_irq(&card->irq_lock);
919
920         if (card->state == CARD_STATE_SHUTDOWN) {
921                 st = rsxx_issue_card_cmd(card, CARD_CMD_STARTUP);
922                 if (st)
923                         dev_crit(CARD_TO_DEV(card),
924                                 "Failed issuing card startup\n");
925                 if (sync_start) {
926                         sync_timeout = SYNC_START_TIMEOUT;
927
928                         dev_info(CARD_TO_DEV(card),
929                                  "Waiting for card to startup\n");
930
931                         do {
932                                 ssleep(1);
933                                 sync_timeout--;
934
935                                 rsxx_get_card_state(card, &card->state);
936                         } while (sync_timeout &&
937                                 (card->state == CARD_STATE_STARTING));
938
939                         if (card->state == CARD_STATE_STARTING) {
940                                 dev_warn(CARD_TO_DEV(card),
941                                          "Card startup timed out\n");
942                                 card->size8 = 0;
943                         } else {
944                                 dev_info(CARD_TO_DEV(card),
945                                         "card state: %s\n",
946                                         rsxx_card_state_to_str(card->state));
947                                 st = rsxx_get_card_size8(card, &card->size8);
948                                 if (st)
949                                         card->size8 = 0;
950                         }
951                 }
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);
955                 if (st)
956                         card->size8 = 0;
957         }
958
959         rsxx_attach_dev(card);
960
961         /************* Setup Debugfs *************/
962         rsxx_debugfs_dev_new(card);
963
964         return 0;
965
966 failed_create_dev:
967         destroy_workqueue(card->event_wq);
968         card->event_wq = NULL;
969 failed_event_handler:
970         rsxx_dma_destroy(card);
971 failed_dma_setup:
972 failed_compatiblity_check:
973         destroy_workqueue(card->creg_ctrl.creg_wq);
974         card->creg_ctrl.creg_wq = NULL;
975 failed_creg_setup:
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);
980         if (!force_legacy)
981                 pci_disable_msi(dev);
982 failed_irq:
983         pci_iounmap(dev, card->regmap);
984 failed_iomap:
985         pci_release_regions(dev);
986 failed_request_regions:
987 failed_dma_mask:
988         pci_disable_device(dev);
989 failed_enable:
990         spin_lock(&rsxx_ida_lock);
991         ida_remove(&rsxx_disk_ida, card->disk_id);
992         spin_unlock(&rsxx_ida_lock);
993 failed_ida_get:
994         kfree(card);
995
996         return st;
997 }
998
999 static void rsxx_pci_remove(struct pci_dev *dev)
1000 {
1001         struct rsxx_cardinfo *card = pci_get_drvdata(dev);
1002         unsigned long flags;
1003         int st;
1004         int i;
1005
1006         if (!card)
1007                 return;
1008
1009         dev_info(CARD_TO_DEV(card),
1010                 "Removing PCI-Flash SSD.\n");
1011
1012         rsxx_detach_dev(card);
1013
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);
1018         }
1019
1020         st = card_shutdown(card);
1021         if (st)
1022                 dev_crit(CARD_TO_DEV(card), "Shutdown failed!\n");
1023
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);
1028
1029         cancel_work_sync(&card->event_work);
1030
1031         destroy_workqueue(card->event_wq);
1032         rsxx_destroy_dev(card);
1033         rsxx_dma_destroy(card);
1034         destroy_workqueue(card->creg_ctrl.creg_wq);
1035
1036         spin_lock_irqsave(&card->irq_lock, flags);
1037         rsxx_disable_ier_and_isr(card, CR_INTR_ALL);
1038         spin_unlock_irqrestore(&card->irq_lock, flags);
1039
1040         /* Prevent work_structs from re-queuing themselves. */
1041         card->halt = 1;
1042
1043         debugfs_remove_recursive(card->debugfs_dir);
1044
1045         free_irq(dev->irq, card);
1046
1047         if (!force_legacy)
1048                 pci_disable_msi(dev);
1049
1050         rsxx_creg_destroy(card);
1051
1052         pci_iounmap(dev, card->regmap);
1053
1054         pci_disable_device(dev);
1055         pci_release_regions(dev);
1056
1057         kfree(card);
1058 }
1059
1060 static int rsxx_pci_suspend(struct pci_dev *dev, pm_message_t state)
1061 {
1062         /* We don't support suspend at this time. */
1063         return -ENOSYS;
1064 }
1065
1066 static void rsxx_pci_shutdown(struct pci_dev *dev)
1067 {
1068         struct rsxx_cardinfo *card = pci_get_drvdata(dev);
1069         unsigned long flags;
1070         int i;
1071
1072         if (!card)
1073                 return;
1074
1075         dev_info(CARD_TO_DEV(card), "Shutting down PCI-Flash SSD.\n");
1076
1077         rsxx_detach_dev(card);
1078
1079         for (i = 0; i < card->n_targets; i++) {
1080                 spin_lock_irqsave(&card->irq_lock, flags);
1081                 rsxx_disable_ier_and_isr(card, CR_INTR_DMA(i));
1082                 spin_unlock_irqrestore(&card->irq_lock, flags);
1083         }
1084
1085         card_shutdown(card);
1086 }
1087
1088 static const struct pci_error_handlers rsxx_err_handler = {
1089         .error_detected = rsxx_error_detected,
1090         .slot_reset     = rsxx_slot_reset,
1091 };
1092
1093 static const struct pci_device_id rsxx_pci_ids[] = {
1094         {PCI_DEVICE(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_FS70_FLASH)},
1095         {PCI_DEVICE(PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_FS80_FLASH)},
1096         {0,},
1097 };
1098
1099 MODULE_DEVICE_TABLE(pci, rsxx_pci_ids);
1100
1101 static struct pci_driver rsxx_pci_driver = {
1102         .name           = DRIVER_NAME,
1103         .id_table       = rsxx_pci_ids,
1104         .probe          = rsxx_pci_probe,
1105         .remove         = rsxx_pci_remove,
1106         .suspend        = rsxx_pci_suspend,
1107         .shutdown       = rsxx_pci_shutdown,
1108         .err_handler    = &rsxx_err_handler,
1109 };
1110
1111 static int __init rsxx_core_init(void)
1112 {
1113         int st;
1114
1115         st = rsxx_dev_init();
1116         if (st)
1117                 return st;
1118
1119         st = rsxx_dma_init();
1120         if (st)
1121                 goto dma_init_failed;
1122
1123         st = rsxx_creg_init();
1124         if (st)
1125                 goto creg_init_failed;
1126
1127         return pci_register_driver(&rsxx_pci_driver);
1128
1129 creg_init_failed:
1130         rsxx_dma_cleanup();
1131 dma_init_failed:
1132         rsxx_dev_cleanup();
1133
1134         return st;
1135 }
1136
1137 static void __exit rsxx_core_cleanup(void)
1138 {
1139         pci_unregister_driver(&rsxx_pci_driver);
1140         rsxx_creg_cleanup();
1141         rsxx_dma_cleanup();
1142         rsxx_dev_cleanup();
1143 }
1144
1145 module_init(rsxx_core_init);
1146 module_exit(rsxx_core_cleanup);