[SCSI] megaraid_sas: Add support for MegaRAID Fury (device ID-0x005f) 12Gb/s controllers
[deliverable/linux.git] / drivers / scsi / megaraid / megaraid_sas_base.c
1 /*
2 * Linux MegaRAID driver for SAS based RAID controllers
3 *
4 * Copyright (c) 2003-2012 LSI Corporation.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * FILE: megaraid_sas_base.c
21 * Version : v06.506.00.00-rc1
22 *
23 * Authors: LSI Corporation
24 * Sreenivas Bagalkote
25 * Sumant Patro
26 * Bo Yang
27 * Adam Radford <linuxraid@lsi.com>
28 *
29 * Send feedback to: <megaraidlinux@lsi.com>
30 *
31 * Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035
32 * ATTN: Linuxraid
33 */
34
35 #include <linux/kernel.h>
36 #include <linux/types.h>
37 #include <linux/pci.h>
38 #include <linux/list.h>
39 #include <linux/moduleparam.h>
40 #include <linux/module.h>
41 #include <linux/spinlock.h>
42 #include <linux/interrupt.h>
43 #include <linux/delay.h>
44 #include <linux/uio.h>
45 #include <linux/slab.h>
46 #include <asm/uaccess.h>
47 #include <linux/fs.h>
48 #include <linux/compat.h>
49 #include <linux/blkdev.h>
50 #include <linux/mutex.h>
51 #include <linux/poll.h>
52
53 #include <scsi/scsi.h>
54 #include <scsi/scsi_cmnd.h>
55 #include <scsi/scsi_device.h>
56 #include <scsi/scsi_host.h>
57 #include <scsi/scsi_tcq.h>
58 #include "megaraid_sas_fusion.h"
59 #include "megaraid_sas.h"
60
61 /*
62 * Number of sectors per IO command
63 * Will be set in megasas_init_mfi if user does not provide
64 */
65 static unsigned int max_sectors;
66 module_param_named(max_sectors, max_sectors, int, 0);
67 MODULE_PARM_DESC(max_sectors,
68 "Maximum number of sectors per IO command");
69
70 static int msix_disable;
71 module_param(msix_disable, int, S_IRUGO);
72 MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
73
74 static unsigned int msix_vectors;
75 module_param(msix_vectors, int, S_IRUGO);
76 MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
77
78 static int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
79 module_param(throttlequeuedepth, int, S_IRUGO);
80 MODULE_PARM_DESC(throttlequeuedepth,
81 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
82
83 int resetwaittime = MEGASAS_RESET_WAIT_TIME;
84 module_param(resetwaittime, int, S_IRUGO);
85 MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
86 "before resetting adapter. Default: 180");
87
88 MODULE_LICENSE("GPL");
89 MODULE_VERSION(MEGASAS_VERSION);
90 MODULE_AUTHOR("megaraidlinux@lsi.com");
91 MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
92
93 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
94 static int megasas_get_pd_list(struct megasas_instance *instance);
95 static int megasas_issue_init_mfi(struct megasas_instance *instance);
96 static int megasas_register_aen(struct megasas_instance *instance,
97 u32 seq_num, u32 class_locale_word);
98 /*
99 * PCI ID table for all supported controllers
100 */
101 static struct pci_device_id megasas_pci_table[] = {
102
103 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
104 /* xscale IOP */
105 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
106 /* ppc IOP */
107 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
108 /* ppc IOP */
109 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
110 /* gen2*/
111 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
112 /* gen2*/
113 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
114 /* skinny*/
115 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
116 /* skinny*/
117 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
118 /* xscale IOP, vega */
119 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
120 /* xscale IOP */
121 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
122 /* Fusion */
123 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
124 /* Invader */
125 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
126 /* Fury */
127 {}
128 };
129
130 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
131
132 static int megasas_mgmt_majorno;
133 static struct megasas_mgmt_info megasas_mgmt_info;
134 static struct fasync_struct *megasas_async_queue;
135 static DEFINE_MUTEX(megasas_async_queue_mutex);
136
137 static int megasas_poll_wait_aen;
138 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
139 static u32 support_poll_for_event;
140 u32 megasas_dbg_lvl;
141 static u32 support_device_change;
142
143 /* define lock for aen poll */
144 spinlock_t poll_aen_lock;
145
146 void
147 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
148 u8 alt_status);
149 static u32
150 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
151 static int
152 megasas_adp_reset_gen2(struct megasas_instance *instance,
153 struct megasas_register_set __iomem *reg_set);
154 static irqreturn_t megasas_isr(int irq, void *devp);
155 static u32
156 megasas_init_adapter_mfi(struct megasas_instance *instance);
157 u32
158 megasas_build_and_issue_cmd(struct megasas_instance *instance,
159 struct scsi_cmnd *scmd);
160 static void megasas_complete_cmd_dpc(unsigned long instance_addr);
161 void
162 megasas_release_fusion(struct megasas_instance *instance);
163 int
164 megasas_ioc_init_fusion(struct megasas_instance *instance);
165 void
166 megasas_free_cmds_fusion(struct megasas_instance *instance);
167 u8
168 megasas_get_map_info(struct megasas_instance *instance);
169 int
170 megasas_sync_map_info(struct megasas_instance *instance);
171 int
172 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
173 void megasas_reset_reply_desc(struct megasas_instance *instance);
174 u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
175 struct LD_LOAD_BALANCE_INFO *lbInfo);
176 int megasas_reset_fusion(struct Scsi_Host *shost);
177 void megasas_fusion_ocr_wq(struct work_struct *work);
178
179 void
180 megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
181 {
182 instance->instancet->fire_cmd(instance,
183 cmd->frame_phys_addr, 0, instance->reg_set);
184 }
185
186 /**
187 * megasas_get_cmd - Get a command from the free pool
188 * @instance: Adapter soft state
189 *
190 * Returns a free command from the pool
191 */
192 struct megasas_cmd *megasas_get_cmd(struct megasas_instance
193 *instance)
194 {
195 unsigned long flags;
196 struct megasas_cmd *cmd = NULL;
197
198 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
199
200 if (!list_empty(&instance->cmd_pool)) {
201 cmd = list_entry((&instance->cmd_pool)->next,
202 struct megasas_cmd, list);
203 list_del_init(&cmd->list);
204 } else {
205 printk(KERN_ERR "megasas: Command pool empty!\n");
206 }
207
208 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
209 return cmd;
210 }
211
212 /**
213 * megasas_return_cmd - Return a cmd to free command pool
214 * @instance: Adapter soft state
215 * @cmd: Command packet to be returned to free command pool
216 */
217 inline void
218 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
219 {
220 unsigned long flags;
221
222 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
223
224 cmd->scmd = NULL;
225 cmd->frame_count = 0;
226 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
227 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
228 (instance->pdev->device != PCI_DEVICE_ID_LSI_FURY) &&
229 (reset_devices))
230 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
231 list_add_tail(&cmd->list, &instance->cmd_pool);
232
233 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
234 }
235
236
237 /**
238 * The following functions are defined for xscale
239 * (deviceid : 1064R, PERC5) controllers
240 */
241
242 /**
243 * megasas_enable_intr_xscale - Enables interrupts
244 * @regs: MFI register set
245 */
246 static inline void
247 megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
248 {
249 writel(0, &(regs)->outbound_intr_mask);
250
251 /* Dummy readl to force pci flush */
252 readl(&regs->outbound_intr_mask);
253 }
254
255 /**
256 * megasas_disable_intr_xscale -Disables interrupt
257 * @regs: MFI register set
258 */
259 static inline void
260 megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
261 {
262 u32 mask = 0x1f;
263 writel(mask, &regs->outbound_intr_mask);
264 /* Dummy readl to force pci flush */
265 readl(&regs->outbound_intr_mask);
266 }
267
268 /**
269 * megasas_read_fw_status_reg_xscale - returns the current FW status value
270 * @regs: MFI register set
271 */
272 static u32
273 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
274 {
275 return readl(&(regs)->outbound_msg_0);
276 }
277 /**
278 * megasas_clear_interrupt_xscale - Check & clear interrupt
279 * @regs: MFI register set
280 */
281 static int
282 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
283 {
284 u32 status;
285 u32 mfiStatus = 0;
286 /*
287 * Check if it is our interrupt
288 */
289 status = readl(&regs->outbound_intr_status);
290
291 if (status & MFI_OB_INTR_STATUS_MASK)
292 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
293 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
294 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
295
296 /*
297 * Clear the interrupt by writing back the same value
298 */
299 if (mfiStatus)
300 writel(status, &regs->outbound_intr_status);
301
302 /* Dummy readl to force pci flush */
303 readl(&regs->outbound_intr_status);
304
305 return mfiStatus;
306 }
307
308 /**
309 * megasas_fire_cmd_xscale - Sends command to the FW
310 * @frame_phys_addr : Physical address of cmd
311 * @frame_count : Number of frames for the command
312 * @regs : MFI register set
313 */
314 static inline void
315 megasas_fire_cmd_xscale(struct megasas_instance *instance,
316 dma_addr_t frame_phys_addr,
317 u32 frame_count,
318 struct megasas_register_set __iomem *regs)
319 {
320 unsigned long flags;
321 spin_lock_irqsave(&instance->hba_lock, flags);
322 writel((frame_phys_addr >> 3)|(frame_count),
323 &(regs)->inbound_queue_port);
324 spin_unlock_irqrestore(&instance->hba_lock, flags);
325 }
326
327 /**
328 * megasas_adp_reset_xscale - For controller reset
329 * @regs: MFI register set
330 */
331 static int
332 megasas_adp_reset_xscale(struct megasas_instance *instance,
333 struct megasas_register_set __iomem *regs)
334 {
335 u32 i;
336 u32 pcidata;
337 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
338
339 for (i = 0; i < 3; i++)
340 msleep(1000); /* sleep for 3 secs */
341 pcidata = 0;
342 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
343 printk(KERN_NOTICE "pcidata = %x\n", pcidata);
344 if (pcidata & 0x2) {
345 printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
346 pcidata &= ~0x2;
347 pci_write_config_dword(instance->pdev,
348 MFI_1068_PCSR_OFFSET, pcidata);
349
350 for (i = 0; i < 2; i++)
351 msleep(1000); /* need to wait 2 secs again */
352
353 pcidata = 0;
354 pci_read_config_dword(instance->pdev,
355 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
356 printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
357 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
358 printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
359 pcidata = 0;
360 pci_write_config_dword(instance->pdev,
361 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
362 }
363 }
364 return 0;
365 }
366
367 /**
368 * megasas_check_reset_xscale - For controller reset check
369 * @regs: MFI register set
370 */
371 static int
372 megasas_check_reset_xscale(struct megasas_instance *instance,
373 struct megasas_register_set __iomem *regs)
374 {
375 u32 consumer;
376 consumer = *instance->consumer;
377
378 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
379 (*instance->consumer == MEGASAS_ADPRESET_INPROG_SIGN)) {
380 return 1;
381 }
382 return 0;
383 }
384
385 static struct megasas_instance_template megasas_instance_template_xscale = {
386
387 .fire_cmd = megasas_fire_cmd_xscale,
388 .enable_intr = megasas_enable_intr_xscale,
389 .disable_intr = megasas_disable_intr_xscale,
390 .clear_intr = megasas_clear_intr_xscale,
391 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
392 .adp_reset = megasas_adp_reset_xscale,
393 .check_reset = megasas_check_reset_xscale,
394 .service_isr = megasas_isr,
395 .tasklet = megasas_complete_cmd_dpc,
396 .init_adapter = megasas_init_adapter_mfi,
397 .build_and_issue_cmd = megasas_build_and_issue_cmd,
398 .issue_dcmd = megasas_issue_dcmd,
399 };
400
401 /**
402 * This is the end of set of functions & definitions specific
403 * to xscale (deviceid : 1064R, PERC5) controllers
404 */
405
406 /**
407 * The following functions are defined for ppc (deviceid : 0x60)
408 * controllers
409 */
410
411 /**
412 * megasas_enable_intr_ppc - Enables interrupts
413 * @regs: MFI register set
414 */
415 static inline void
416 megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
417 {
418 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
419
420 writel(~0x80000000, &(regs)->outbound_intr_mask);
421
422 /* Dummy readl to force pci flush */
423 readl(&regs->outbound_intr_mask);
424 }
425
426 /**
427 * megasas_disable_intr_ppc - Disable interrupt
428 * @regs: MFI register set
429 */
430 static inline void
431 megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
432 {
433 u32 mask = 0xFFFFFFFF;
434 writel(mask, &regs->outbound_intr_mask);
435 /* Dummy readl to force pci flush */
436 readl(&regs->outbound_intr_mask);
437 }
438
439 /**
440 * megasas_read_fw_status_reg_ppc - returns the current FW status value
441 * @regs: MFI register set
442 */
443 static u32
444 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
445 {
446 return readl(&(regs)->outbound_scratch_pad);
447 }
448
449 /**
450 * megasas_clear_interrupt_ppc - Check & clear interrupt
451 * @regs: MFI register set
452 */
453 static int
454 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
455 {
456 u32 status, mfiStatus = 0;
457
458 /*
459 * Check if it is our interrupt
460 */
461 status = readl(&regs->outbound_intr_status);
462
463 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
464 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
465
466 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
467 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
468
469 /*
470 * Clear the interrupt by writing back the same value
471 */
472 writel(status, &regs->outbound_doorbell_clear);
473
474 /* Dummy readl to force pci flush */
475 readl(&regs->outbound_doorbell_clear);
476
477 return mfiStatus;
478 }
479
480 /**
481 * megasas_fire_cmd_ppc - Sends command to the FW
482 * @frame_phys_addr : Physical address of cmd
483 * @frame_count : Number of frames for the command
484 * @regs : MFI register set
485 */
486 static inline void
487 megasas_fire_cmd_ppc(struct megasas_instance *instance,
488 dma_addr_t frame_phys_addr,
489 u32 frame_count,
490 struct megasas_register_set __iomem *regs)
491 {
492 unsigned long flags;
493 spin_lock_irqsave(&instance->hba_lock, flags);
494 writel((frame_phys_addr | (frame_count<<1))|1,
495 &(regs)->inbound_queue_port);
496 spin_unlock_irqrestore(&instance->hba_lock, flags);
497 }
498
499 /**
500 * megasas_check_reset_ppc - For controller reset check
501 * @regs: MFI register set
502 */
503 static int
504 megasas_check_reset_ppc(struct megasas_instance *instance,
505 struct megasas_register_set __iomem *regs)
506 {
507 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
508 return 1;
509
510 return 0;
511 }
512
513 static struct megasas_instance_template megasas_instance_template_ppc = {
514
515 .fire_cmd = megasas_fire_cmd_ppc,
516 .enable_intr = megasas_enable_intr_ppc,
517 .disable_intr = megasas_disable_intr_ppc,
518 .clear_intr = megasas_clear_intr_ppc,
519 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
520 .adp_reset = megasas_adp_reset_xscale,
521 .check_reset = megasas_check_reset_ppc,
522 .service_isr = megasas_isr,
523 .tasklet = megasas_complete_cmd_dpc,
524 .init_adapter = megasas_init_adapter_mfi,
525 .build_and_issue_cmd = megasas_build_and_issue_cmd,
526 .issue_dcmd = megasas_issue_dcmd,
527 };
528
529 /**
530 * megasas_enable_intr_skinny - Enables interrupts
531 * @regs: MFI register set
532 */
533 static inline void
534 megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
535 {
536 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
537
538 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
539
540 /* Dummy readl to force pci flush */
541 readl(&regs->outbound_intr_mask);
542 }
543
544 /**
545 * megasas_disable_intr_skinny - Disables interrupt
546 * @regs: MFI register set
547 */
548 static inline void
549 megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
550 {
551 u32 mask = 0xFFFFFFFF;
552 writel(mask, &regs->outbound_intr_mask);
553 /* Dummy readl to force pci flush */
554 readl(&regs->outbound_intr_mask);
555 }
556
557 /**
558 * megasas_read_fw_status_reg_skinny - returns the current FW status value
559 * @regs: MFI register set
560 */
561 static u32
562 megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
563 {
564 return readl(&(regs)->outbound_scratch_pad);
565 }
566
567 /**
568 * megasas_clear_interrupt_skinny - Check & clear interrupt
569 * @regs: MFI register set
570 */
571 static int
572 megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
573 {
574 u32 status;
575 u32 mfiStatus = 0;
576
577 /*
578 * Check if it is our interrupt
579 */
580 status = readl(&regs->outbound_intr_status);
581
582 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
583 return 0;
584 }
585
586 /*
587 * Check if it is our interrupt
588 */
589 if ((megasas_read_fw_status_reg_gen2(regs) & MFI_STATE_MASK) ==
590 MFI_STATE_FAULT) {
591 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
592 } else
593 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
594
595 /*
596 * Clear the interrupt by writing back the same value
597 */
598 writel(status, &regs->outbound_intr_status);
599
600 /*
601 * dummy read to flush PCI
602 */
603 readl(&regs->outbound_intr_status);
604
605 return mfiStatus;
606 }
607
608 /**
609 * megasas_fire_cmd_skinny - Sends command to the FW
610 * @frame_phys_addr : Physical address of cmd
611 * @frame_count : Number of frames for the command
612 * @regs : MFI register set
613 */
614 static inline void
615 megasas_fire_cmd_skinny(struct megasas_instance *instance,
616 dma_addr_t frame_phys_addr,
617 u32 frame_count,
618 struct megasas_register_set __iomem *regs)
619 {
620 unsigned long flags;
621 spin_lock_irqsave(&instance->hba_lock, flags);
622 writel(0, &(regs)->inbound_high_queue_port);
623 writel((frame_phys_addr | (frame_count<<1))|1,
624 &(regs)->inbound_low_queue_port);
625 spin_unlock_irqrestore(&instance->hba_lock, flags);
626 }
627
628 /**
629 * megasas_check_reset_skinny - For controller reset check
630 * @regs: MFI register set
631 */
632 static int
633 megasas_check_reset_skinny(struct megasas_instance *instance,
634 struct megasas_register_set __iomem *regs)
635 {
636 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
637 return 1;
638
639 return 0;
640 }
641
642 static struct megasas_instance_template megasas_instance_template_skinny = {
643
644 .fire_cmd = megasas_fire_cmd_skinny,
645 .enable_intr = megasas_enable_intr_skinny,
646 .disable_intr = megasas_disable_intr_skinny,
647 .clear_intr = megasas_clear_intr_skinny,
648 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
649 .adp_reset = megasas_adp_reset_gen2,
650 .check_reset = megasas_check_reset_skinny,
651 .service_isr = megasas_isr,
652 .tasklet = megasas_complete_cmd_dpc,
653 .init_adapter = megasas_init_adapter_mfi,
654 .build_and_issue_cmd = megasas_build_and_issue_cmd,
655 .issue_dcmd = megasas_issue_dcmd,
656 };
657
658
659 /**
660 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
661 * controllers
662 */
663
664 /**
665 * megasas_enable_intr_gen2 - Enables interrupts
666 * @regs: MFI register set
667 */
668 static inline void
669 megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
670 {
671 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
672
673 /* write ~0x00000005 (4 & 1) to the intr mask*/
674 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
675
676 /* Dummy readl to force pci flush */
677 readl(&regs->outbound_intr_mask);
678 }
679
680 /**
681 * megasas_disable_intr_gen2 - Disables interrupt
682 * @regs: MFI register set
683 */
684 static inline void
685 megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
686 {
687 u32 mask = 0xFFFFFFFF;
688 writel(mask, &regs->outbound_intr_mask);
689 /* Dummy readl to force pci flush */
690 readl(&regs->outbound_intr_mask);
691 }
692
693 /**
694 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
695 * @regs: MFI register set
696 */
697 static u32
698 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
699 {
700 return readl(&(regs)->outbound_scratch_pad);
701 }
702
703 /**
704 * megasas_clear_interrupt_gen2 - Check & clear interrupt
705 * @regs: MFI register set
706 */
707 static int
708 megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
709 {
710 u32 status;
711 u32 mfiStatus = 0;
712 /*
713 * Check if it is our interrupt
714 */
715 status = readl(&regs->outbound_intr_status);
716
717 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
718 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
719 }
720 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
721 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
722 }
723
724 /*
725 * Clear the interrupt by writing back the same value
726 */
727 if (mfiStatus)
728 writel(status, &regs->outbound_doorbell_clear);
729
730 /* Dummy readl to force pci flush */
731 readl(&regs->outbound_intr_status);
732
733 return mfiStatus;
734 }
735 /**
736 * megasas_fire_cmd_gen2 - Sends command to the FW
737 * @frame_phys_addr : Physical address of cmd
738 * @frame_count : Number of frames for the command
739 * @regs : MFI register set
740 */
741 static inline void
742 megasas_fire_cmd_gen2(struct megasas_instance *instance,
743 dma_addr_t frame_phys_addr,
744 u32 frame_count,
745 struct megasas_register_set __iomem *regs)
746 {
747 unsigned long flags;
748 spin_lock_irqsave(&instance->hba_lock, flags);
749 writel((frame_phys_addr | (frame_count<<1))|1,
750 &(regs)->inbound_queue_port);
751 spin_unlock_irqrestore(&instance->hba_lock, flags);
752 }
753
754 /**
755 * megasas_adp_reset_gen2 - For controller reset
756 * @regs: MFI register set
757 */
758 static int
759 megasas_adp_reset_gen2(struct megasas_instance *instance,
760 struct megasas_register_set __iomem *reg_set)
761 {
762 u32 retry = 0 ;
763 u32 HostDiag;
764 u32 *seq_offset = &reg_set->seq_offset;
765 u32 *hostdiag_offset = &reg_set->host_diag;
766
767 if (instance->instancet == &megasas_instance_template_skinny) {
768 seq_offset = &reg_set->fusion_seq_offset;
769 hostdiag_offset = &reg_set->fusion_host_diag;
770 }
771
772 writel(0, seq_offset);
773 writel(4, seq_offset);
774 writel(0xb, seq_offset);
775 writel(2, seq_offset);
776 writel(7, seq_offset);
777 writel(0xd, seq_offset);
778
779 msleep(1000);
780
781 HostDiag = (u32)readl(hostdiag_offset);
782
783 while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
784 msleep(100);
785 HostDiag = (u32)readl(hostdiag_offset);
786 printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
787 retry, HostDiag);
788
789 if (retry++ >= 100)
790 return 1;
791
792 }
793
794 printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
795
796 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
797
798 ssleep(10);
799
800 HostDiag = (u32)readl(hostdiag_offset);
801 while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
802 msleep(100);
803 HostDiag = (u32)readl(hostdiag_offset);
804 printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
805 retry, HostDiag);
806
807 if (retry++ >= 1000)
808 return 1;
809
810 }
811 return 0;
812 }
813
814 /**
815 * megasas_check_reset_gen2 - For controller reset check
816 * @regs: MFI register set
817 */
818 static int
819 megasas_check_reset_gen2(struct megasas_instance *instance,
820 struct megasas_register_set __iomem *regs)
821 {
822 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
823 return 1;
824 }
825
826 return 0;
827 }
828
829 static struct megasas_instance_template megasas_instance_template_gen2 = {
830
831 .fire_cmd = megasas_fire_cmd_gen2,
832 .enable_intr = megasas_enable_intr_gen2,
833 .disable_intr = megasas_disable_intr_gen2,
834 .clear_intr = megasas_clear_intr_gen2,
835 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
836 .adp_reset = megasas_adp_reset_gen2,
837 .check_reset = megasas_check_reset_gen2,
838 .service_isr = megasas_isr,
839 .tasklet = megasas_complete_cmd_dpc,
840 .init_adapter = megasas_init_adapter_mfi,
841 .build_and_issue_cmd = megasas_build_and_issue_cmd,
842 .issue_dcmd = megasas_issue_dcmd,
843 };
844
845 /**
846 * This is the end of set of functions & definitions
847 * specific to gen2 (deviceid : 0x78, 0x79) controllers
848 */
849
850 /*
851 * Template added for TB (Fusion)
852 */
853 extern struct megasas_instance_template megasas_instance_template_fusion;
854
855 /**
856 * megasas_issue_polled - Issues a polling command
857 * @instance: Adapter soft state
858 * @cmd: Command packet to be issued
859 *
860 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
861 */
862 int
863 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
864 {
865
866 struct megasas_header *frame_hdr = &cmd->frame->hdr;
867
868 frame_hdr->cmd_status = 0xFF;
869 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
870
871 /*
872 * Issue the frame using inbound queue port
873 */
874 instance->instancet->issue_dcmd(instance, cmd);
875
876 /*
877 * Wait for cmd_status to change
878 */
879 return wait_and_poll(instance, cmd);
880 }
881
882 /**
883 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
884 * @instance: Adapter soft state
885 * @cmd: Command to be issued
886 *
887 * This function waits on an event for the command to be returned from ISR.
888 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
889 * Used to issue ioctl commands.
890 */
891 static int
892 megasas_issue_blocked_cmd(struct megasas_instance *instance,
893 struct megasas_cmd *cmd)
894 {
895 cmd->cmd_status = ENODATA;
896
897 instance->instancet->issue_dcmd(instance, cmd);
898
899 wait_event(instance->int_cmd_wait_q, cmd->cmd_status != ENODATA);
900
901 return 0;
902 }
903
904 /**
905 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
906 * @instance: Adapter soft state
907 * @cmd_to_abort: Previously issued cmd to be aborted
908 *
909 * MFI firmware can abort previously issued AEN command (automatic event
910 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
911 * cmd and waits for return status.
912 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
913 */
914 static int
915 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
916 struct megasas_cmd *cmd_to_abort)
917 {
918 struct megasas_cmd *cmd;
919 struct megasas_abort_frame *abort_fr;
920
921 cmd = megasas_get_cmd(instance);
922
923 if (!cmd)
924 return -1;
925
926 abort_fr = &cmd->frame->abort;
927
928 /*
929 * Prepare and issue the abort frame
930 */
931 abort_fr->cmd = MFI_CMD_ABORT;
932 abort_fr->cmd_status = 0xFF;
933 abort_fr->flags = 0;
934 abort_fr->abort_context = cmd_to_abort->index;
935 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
936 abort_fr->abort_mfi_phys_addr_hi = 0;
937
938 cmd->sync_cmd = 1;
939 cmd->cmd_status = 0xFF;
940
941 instance->instancet->issue_dcmd(instance, cmd);
942
943 /*
944 * Wait for this cmd to complete
945 */
946 wait_event(instance->abort_cmd_wait_q, cmd->cmd_status != 0xFF);
947 cmd->sync_cmd = 0;
948
949 megasas_return_cmd(instance, cmd);
950 return 0;
951 }
952
953 /**
954 * megasas_make_sgl32 - Prepares 32-bit SGL
955 * @instance: Adapter soft state
956 * @scp: SCSI command from the mid-layer
957 * @mfi_sgl: SGL to be filled in
958 *
959 * If successful, this function returns the number of SG elements. Otherwise,
960 * it returnes -1.
961 */
962 static int
963 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
964 union megasas_sgl *mfi_sgl)
965 {
966 int i;
967 int sge_count;
968 struct scatterlist *os_sgl;
969
970 sge_count = scsi_dma_map(scp);
971 BUG_ON(sge_count < 0);
972
973 if (sge_count) {
974 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
975 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
976 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
977 }
978 }
979 return sge_count;
980 }
981
982 /**
983 * megasas_make_sgl64 - Prepares 64-bit SGL
984 * @instance: Adapter soft state
985 * @scp: SCSI command from the mid-layer
986 * @mfi_sgl: SGL to be filled in
987 *
988 * If successful, this function returns the number of SG elements. Otherwise,
989 * it returnes -1.
990 */
991 static int
992 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
993 union megasas_sgl *mfi_sgl)
994 {
995 int i;
996 int sge_count;
997 struct scatterlist *os_sgl;
998
999 sge_count = scsi_dma_map(scp);
1000 BUG_ON(sge_count < 0);
1001
1002 if (sge_count) {
1003 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1004 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
1005 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
1006 }
1007 }
1008 return sge_count;
1009 }
1010
1011 /**
1012 * megasas_make_sgl_skinny - Prepares IEEE SGL
1013 * @instance: Adapter soft state
1014 * @scp: SCSI command from the mid-layer
1015 * @mfi_sgl: SGL to be filled in
1016 *
1017 * If successful, this function returns the number of SG elements. Otherwise,
1018 * it returnes -1.
1019 */
1020 static int
1021 megasas_make_sgl_skinny(struct megasas_instance *instance,
1022 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1023 {
1024 int i;
1025 int sge_count;
1026 struct scatterlist *os_sgl;
1027
1028 sge_count = scsi_dma_map(scp);
1029
1030 if (sge_count) {
1031 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1032 mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
1033 mfi_sgl->sge_skinny[i].phys_addr =
1034 sg_dma_address(os_sgl);
1035 mfi_sgl->sge_skinny[i].flag = 0;
1036 }
1037 }
1038 return sge_count;
1039 }
1040
1041 /**
1042 * megasas_get_frame_count - Computes the number of frames
1043 * @frame_type : type of frame- io or pthru frame
1044 * @sge_count : number of sg elements
1045 *
1046 * Returns the number of frames required for numnber of sge's (sge_count)
1047 */
1048
1049 static u32 megasas_get_frame_count(struct megasas_instance *instance,
1050 u8 sge_count, u8 frame_type)
1051 {
1052 int num_cnt;
1053 int sge_bytes;
1054 u32 sge_sz;
1055 u32 frame_count=0;
1056
1057 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1058 sizeof(struct megasas_sge32);
1059
1060 if (instance->flag_ieee) {
1061 sge_sz = sizeof(struct megasas_sge_skinny);
1062 }
1063
1064 /*
1065 * Main frame can contain 2 SGEs for 64-bit SGLs and
1066 * 3 SGEs for 32-bit SGLs for ldio &
1067 * 1 SGEs for 64-bit SGLs and
1068 * 2 SGEs for 32-bit SGLs for pthru frame
1069 */
1070 if (unlikely(frame_type == PTHRU_FRAME)) {
1071 if (instance->flag_ieee == 1) {
1072 num_cnt = sge_count - 1;
1073 } else if (IS_DMA64)
1074 num_cnt = sge_count - 1;
1075 else
1076 num_cnt = sge_count - 2;
1077 } else {
1078 if (instance->flag_ieee == 1) {
1079 num_cnt = sge_count - 1;
1080 } else if (IS_DMA64)
1081 num_cnt = sge_count - 2;
1082 else
1083 num_cnt = sge_count - 3;
1084 }
1085
1086 if(num_cnt>0){
1087 sge_bytes = sge_sz * num_cnt;
1088
1089 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1090 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1091 }
1092 /* Main frame */
1093 frame_count +=1;
1094
1095 if (frame_count > 7)
1096 frame_count = 8;
1097 return frame_count;
1098 }
1099
1100 /**
1101 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1102 * @instance: Adapter soft state
1103 * @scp: SCSI command
1104 * @cmd: Command to be prepared in
1105 *
1106 * This function prepares CDB commands. These are typcially pass-through
1107 * commands to the devices.
1108 */
1109 static int
1110 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1111 struct megasas_cmd *cmd)
1112 {
1113 u32 is_logical;
1114 u32 device_id;
1115 u16 flags = 0;
1116 struct megasas_pthru_frame *pthru;
1117
1118 is_logical = MEGASAS_IS_LOGICAL(scp);
1119 device_id = MEGASAS_DEV_INDEX(instance, scp);
1120 pthru = (struct megasas_pthru_frame *)cmd->frame;
1121
1122 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1123 flags = MFI_FRAME_DIR_WRITE;
1124 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1125 flags = MFI_FRAME_DIR_READ;
1126 else if (scp->sc_data_direction == PCI_DMA_NONE)
1127 flags = MFI_FRAME_DIR_NONE;
1128
1129 if (instance->flag_ieee == 1) {
1130 flags |= MFI_FRAME_IEEE;
1131 }
1132
1133 /*
1134 * Prepare the DCDB frame
1135 */
1136 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1137 pthru->cmd_status = 0x0;
1138 pthru->scsi_status = 0x0;
1139 pthru->target_id = device_id;
1140 pthru->lun = scp->device->lun;
1141 pthru->cdb_len = scp->cmd_len;
1142 pthru->timeout = 0;
1143 pthru->pad_0 = 0;
1144 pthru->flags = flags;
1145 pthru->data_xfer_len = scsi_bufflen(scp);
1146
1147 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1148
1149 /*
1150 * If the command is for the tape device, set the
1151 * pthru timeout to the os layer timeout value.
1152 */
1153 if (scp->device->type == TYPE_TAPE) {
1154 if ((scp->request->timeout / HZ) > 0xFFFF)
1155 pthru->timeout = 0xFFFF;
1156 else
1157 pthru->timeout = scp->request->timeout / HZ;
1158 }
1159
1160 /*
1161 * Construct SGL
1162 */
1163 if (instance->flag_ieee == 1) {
1164 pthru->flags |= MFI_FRAME_SGL64;
1165 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1166 &pthru->sgl);
1167 } else if (IS_DMA64) {
1168 pthru->flags |= MFI_FRAME_SGL64;
1169 pthru->sge_count = megasas_make_sgl64(instance, scp,
1170 &pthru->sgl);
1171 } else
1172 pthru->sge_count = megasas_make_sgl32(instance, scp,
1173 &pthru->sgl);
1174
1175 if (pthru->sge_count > instance->max_num_sge) {
1176 printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
1177 pthru->sge_count);
1178 return 0;
1179 }
1180
1181 /*
1182 * Sense info specific
1183 */
1184 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1185 pthru->sense_buf_phys_addr_hi = 0;
1186 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1187
1188 /*
1189 * Compute the total number of frames this command consumes. FW uses
1190 * this number to pull sufficient number of frames from host memory.
1191 */
1192 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1193 PTHRU_FRAME);
1194
1195 return cmd->frame_count;
1196 }
1197
1198 /**
1199 * megasas_build_ldio - Prepares IOs to logical devices
1200 * @instance: Adapter soft state
1201 * @scp: SCSI command
1202 * @cmd: Command to be prepared
1203 *
1204 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1205 */
1206 static int
1207 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1208 struct megasas_cmd *cmd)
1209 {
1210 u32 device_id;
1211 u8 sc = scp->cmnd[0];
1212 u16 flags = 0;
1213 struct megasas_io_frame *ldio;
1214
1215 device_id = MEGASAS_DEV_INDEX(instance, scp);
1216 ldio = (struct megasas_io_frame *)cmd->frame;
1217
1218 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1219 flags = MFI_FRAME_DIR_WRITE;
1220 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1221 flags = MFI_FRAME_DIR_READ;
1222
1223 if (instance->flag_ieee == 1) {
1224 flags |= MFI_FRAME_IEEE;
1225 }
1226
1227 /*
1228 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1229 */
1230 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1231 ldio->cmd_status = 0x0;
1232 ldio->scsi_status = 0x0;
1233 ldio->target_id = device_id;
1234 ldio->timeout = 0;
1235 ldio->reserved_0 = 0;
1236 ldio->pad_0 = 0;
1237 ldio->flags = flags;
1238 ldio->start_lba_hi = 0;
1239 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1240
1241 /*
1242 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1243 */
1244 if (scp->cmd_len == 6) {
1245 ldio->lba_count = (u32) scp->cmnd[4];
1246 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1247 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1248
1249 ldio->start_lba_lo &= 0x1FFFFF;
1250 }
1251
1252 /*
1253 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1254 */
1255 else if (scp->cmd_len == 10) {
1256 ldio->lba_count = (u32) scp->cmnd[8] |
1257 ((u32) scp->cmnd[7] << 8);
1258 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1259 ((u32) scp->cmnd[3] << 16) |
1260 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1261 }
1262
1263 /*
1264 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1265 */
1266 else if (scp->cmd_len == 12) {
1267 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
1268 ((u32) scp->cmnd[7] << 16) |
1269 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1270
1271 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1272 ((u32) scp->cmnd[3] << 16) |
1273 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1274 }
1275
1276 /*
1277 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1278 */
1279 else if (scp->cmd_len == 16) {
1280 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
1281 ((u32) scp->cmnd[11] << 16) |
1282 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1283
1284 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1285 ((u32) scp->cmnd[7] << 16) |
1286 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1287
1288 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1289 ((u32) scp->cmnd[3] << 16) |
1290 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1291
1292 }
1293
1294 /*
1295 * Construct SGL
1296 */
1297 if (instance->flag_ieee) {
1298 ldio->flags |= MFI_FRAME_SGL64;
1299 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1300 &ldio->sgl);
1301 } else if (IS_DMA64) {
1302 ldio->flags |= MFI_FRAME_SGL64;
1303 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1304 } else
1305 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1306
1307 if (ldio->sge_count > instance->max_num_sge) {
1308 printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1309 ldio->sge_count);
1310 return 0;
1311 }
1312
1313 /*
1314 * Sense info specific
1315 */
1316 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1317 ldio->sense_buf_phys_addr_hi = 0;
1318 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1319
1320 /*
1321 * Compute the total number of frames this command consumes. FW uses
1322 * this number to pull sufficient number of frames from host memory.
1323 */
1324 cmd->frame_count = megasas_get_frame_count(instance,
1325 ldio->sge_count, IO_FRAME);
1326
1327 return cmd->frame_count;
1328 }
1329
1330 /**
1331 * megasas_is_ldio - Checks if the cmd is for logical drive
1332 * @scmd: SCSI command
1333 *
1334 * Called by megasas_queue_command to find out if the command to be queued
1335 * is a logical drive command
1336 */
1337 inline int megasas_is_ldio(struct scsi_cmnd *cmd)
1338 {
1339 if (!MEGASAS_IS_LOGICAL(cmd))
1340 return 0;
1341 switch (cmd->cmnd[0]) {
1342 case READ_10:
1343 case WRITE_10:
1344 case READ_12:
1345 case WRITE_12:
1346 case READ_6:
1347 case WRITE_6:
1348 case READ_16:
1349 case WRITE_16:
1350 return 1;
1351 default:
1352 return 0;
1353 }
1354 }
1355
1356 /**
1357 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1358 * in FW
1359 * @instance: Adapter soft state
1360 */
1361 static inline void
1362 megasas_dump_pending_frames(struct megasas_instance *instance)
1363 {
1364 struct megasas_cmd *cmd;
1365 int i,n;
1366 union megasas_sgl *mfi_sgl;
1367 struct megasas_io_frame *ldio;
1368 struct megasas_pthru_frame *pthru;
1369 u32 sgcount;
1370 u32 max_cmd = instance->max_fw_cmds;
1371
1372 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1373 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1374 if (IS_DMA64)
1375 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1376 else
1377 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1378
1379 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1380 for (i = 0; i < max_cmd; i++) {
1381 cmd = instance->cmd_list[i];
1382 if(!cmd->scmd)
1383 continue;
1384 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1385 if (megasas_is_ldio(cmd->scmd)){
1386 ldio = (struct megasas_io_frame *)cmd->frame;
1387 mfi_sgl = &ldio->sgl;
1388 sgcount = ldio->sge_count;
1389 printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",instance->host->host_no, cmd->frame_count,ldio->cmd,ldio->target_id, ldio->start_lba_lo,ldio->start_lba_hi,ldio->sense_buf_phys_addr_lo,sgcount);
1390 }
1391 else {
1392 pthru = (struct megasas_pthru_frame *) cmd->frame;
1393 mfi_sgl = &pthru->sgl;
1394 sgcount = pthru->sge_count;
1395 printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",instance->host->host_no,cmd->frame_count,pthru->cmd,pthru->target_id,pthru->lun,pthru->cdb_len , pthru->data_xfer_len,pthru->sense_buf_phys_addr_lo,sgcount);
1396 }
1397 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1398 for (n = 0; n < sgcount; n++){
1399 if (IS_DMA64)
1400 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%08lx ",mfi_sgl->sge64[n].length , (unsigned long)mfi_sgl->sge64[n].phys_addr) ;
1401 else
1402 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
1403 }
1404 }
1405 printk(KERN_ERR "\n");
1406 } /*for max_cmd*/
1407 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1408 for (i = 0; i < max_cmd; i++) {
1409
1410 cmd = instance->cmd_list[i];
1411
1412 if(cmd->sync_cmd == 1){
1413 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1414 }
1415 }
1416 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1417 }
1418
1419 u32
1420 megasas_build_and_issue_cmd(struct megasas_instance *instance,
1421 struct scsi_cmnd *scmd)
1422 {
1423 struct megasas_cmd *cmd;
1424 u32 frame_count;
1425
1426 cmd = megasas_get_cmd(instance);
1427 if (!cmd)
1428 return SCSI_MLQUEUE_HOST_BUSY;
1429
1430 /*
1431 * Logical drive command
1432 */
1433 if (megasas_is_ldio(scmd))
1434 frame_count = megasas_build_ldio(instance, scmd, cmd);
1435 else
1436 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1437
1438 if (!frame_count)
1439 goto out_return_cmd;
1440
1441 cmd->scmd = scmd;
1442 scmd->SCp.ptr = (char *)cmd;
1443
1444 /*
1445 * Issue the command to the FW
1446 */
1447 atomic_inc(&instance->fw_outstanding);
1448
1449 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1450 cmd->frame_count-1, instance->reg_set);
1451
1452 return 0;
1453 out_return_cmd:
1454 megasas_return_cmd(instance, cmd);
1455 return 1;
1456 }
1457
1458
1459 /**
1460 * megasas_queue_command - Queue entry point
1461 * @scmd: SCSI command to be queued
1462 * @done: Callback entry point
1463 */
1464 static int
1465 megasas_queue_command_lck(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
1466 {
1467 struct megasas_instance *instance;
1468 unsigned long flags;
1469
1470 instance = (struct megasas_instance *)
1471 scmd->device->host->hostdata;
1472
1473 if (instance->issuepend_done == 0)
1474 return SCSI_MLQUEUE_HOST_BUSY;
1475
1476 spin_lock_irqsave(&instance->hba_lock, flags);
1477
1478 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1479 spin_unlock_irqrestore(&instance->hba_lock, flags);
1480 scmd->result = DID_ERROR << 16;
1481 done(scmd);
1482 return 0;
1483 }
1484
1485 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1486 spin_unlock_irqrestore(&instance->hba_lock, flags);
1487 return SCSI_MLQUEUE_HOST_BUSY;
1488 }
1489
1490 spin_unlock_irqrestore(&instance->hba_lock, flags);
1491
1492 scmd->scsi_done = done;
1493 scmd->result = 0;
1494
1495 if (MEGASAS_IS_LOGICAL(scmd) &&
1496 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1497 scmd->result = DID_BAD_TARGET << 16;
1498 goto out_done;
1499 }
1500
1501 switch (scmd->cmnd[0]) {
1502 case SYNCHRONIZE_CACHE:
1503 /*
1504 * FW takes care of flush cache on its own
1505 * No need to send it down
1506 */
1507 scmd->result = DID_OK << 16;
1508 goto out_done;
1509 default:
1510 break;
1511 }
1512
1513 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1514 printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
1515 return SCSI_MLQUEUE_HOST_BUSY;
1516 }
1517
1518 return 0;
1519
1520 out_done:
1521 done(scmd);
1522 return 0;
1523 }
1524
1525 static DEF_SCSI_QCMD(megasas_queue_command)
1526
1527 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1528 {
1529 int i;
1530
1531 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1532
1533 if ((megasas_mgmt_info.instance[i]) &&
1534 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1535 return megasas_mgmt_info.instance[i];
1536 }
1537
1538 return NULL;
1539 }
1540
1541 static int megasas_slave_configure(struct scsi_device *sdev)
1542 {
1543 u16 pd_index = 0;
1544 struct megasas_instance *instance ;
1545
1546 instance = megasas_lookup_instance(sdev->host->host_no);
1547
1548 /*
1549 * Don't export physical disk devices to the disk driver.
1550 *
1551 * FIXME: Currently we don't export them to the midlayer at all.
1552 * That will be fixed once LSI engineers have audited the
1553 * firmware for possible issues.
1554 */
1555 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1556 sdev->type == TYPE_DISK) {
1557 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1558 sdev->id;
1559 if (instance->pd_list[pd_index].driveState ==
1560 MR_PD_STATE_SYSTEM) {
1561 blk_queue_rq_timeout(sdev->request_queue,
1562 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1563 return 0;
1564 }
1565 return -ENXIO;
1566 }
1567
1568 /*
1569 * The RAID firmware may require extended timeouts.
1570 */
1571 blk_queue_rq_timeout(sdev->request_queue,
1572 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1573 return 0;
1574 }
1575
1576 static int megasas_slave_alloc(struct scsi_device *sdev)
1577 {
1578 u16 pd_index = 0;
1579 struct megasas_instance *instance ;
1580 instance = megasas_lookup_instance(sdev->host->host_no);
1581 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1582 (sdev->type == TYPE_DISK)) {
1583 /*
1584 * Open the OS scan to the SYSTEM PD
1585 */
1586 pd_index =
1587 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1588 sdev->id;
1589 if ((instance->pd_list[pd_index].driveState ==
1590 MR_PD_STATE_SYSTEM) &&
1591 (instance->pd_list[pd_index].driveType ==
1592 TYPE_DISK)) {
1593 return 0;
1594 }
1595 return -ENXIO;
1596 }
1597 return 0;
1598 }
1599
1600 void megaraid_sas_kill_hba(struct megasas_instance *instance)
1601 {
1602 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1603 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
1604 (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1605 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1606 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1607 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
1608 } else {
1609 writel(MFI_STOP_ADP, &instance->reg_set->inbound_doorbell);
1610 }
1611 }
1612
1613 /**
1614 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1615 * restored to max value
1616 * @instance: Adapter soft state
1617 *
1618 */
1619 void
1620 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1621 {
1622 unsigned long flags;
1623 if (instance->flag & MEGASAS_FW_BUSY
1624 && time_after(jiffies, instance->last_time + 5 * HZ)
1625 && atomic_read(&instance->fw_outstanding) <
1626 instance->throttlequeuedepth + 1) {
1627
1628 spin_lock_irqsave(instance->host->host_lock, flags);
1629 instance->flag &= ~MEGASAS_FW_BUSY;
1630 if ((instance->pdev->device ==
1631 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1632 (instance->pdev->device ==
1633 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1634 instance->host->can_queue =
1635 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1636 } else
1637 instance->host->can_queue =
1638 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1639
1640 spin_unlock_irqrestore(instance->host->host_lock, flags);
1641 }
1642 }
1643
1644 /**
1645 * megasas_complete_cmd_dpc - Returns FW's controller structure
1646 * @instance_addr: Address of adapter soft state
1647 *
1648 * Tasklet to complete cmds
1649 */
1650 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1651 {
1652 u32 producer;
1653 u32 consumer;
1654 u32 context;
1655 struct megasas_cmd *cmd;
1656 struct megasas_instance *instance =
1657 (struct megasas_instance *)instance_addr;
1658 unsigned long flags;
1659
1660 /* If we have already declared adapter dead, donot complete cmds */
1661 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
1662 return;
1663
1664 spin_lock_irqsave(&instance->completion_lock, flags);
1665
1666 producer = *instance->producer;
1667 consumer = *instance->consumer;
1668
1669 while (consumer != producer) {
1670 context = instance->reply_queue[consumer];
1671 if (context >= instance->max_fw_cmds) {
1672 printk(KERN_ERR "Unexpected context value %x\n",
1673 context);
1674 BUG();
1675 }
1676
1677 cmd = instance->cmd_list[context];
1678
1679 megasas_complete_cmd(instance, cmd, DID_OK);
1680
1681 consumer++;
1682 if (consumer == (instance->max_fw_cmds + 1)) {
1683 consumer = 0;
1684 }
1685 }
1686
1687 *instance->consumer = producer;
1688
1689 spin_unlock_irqrestore(&instance->completion_lock, flags);
1690
1691 /*
1692 * Check if we can restore can_queue
1693 */
1694 megasas_check_and_restore_queue_depth(instance);
1695 }
1696
1697 static void
1698 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1699
1700 static void
1701 process_fw_state_change_wq(struct work_struct *work);
1702
1703 void megasas_do_ocr(struct megasas_instance *instance)
1704 {
1705 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1706 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1707 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1708 *instance->consumer = MEGASAS_ADPRESET_INPROG_SIGN;
1709 }
1710 instance->instancet->disable_intr(instance->reg_set);
1711 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1712 instance->issuepend_done = 0;
1713
1714 atomic_set(&instance->fw_outstanding, 0);
1715 megasas_internal_reset_defer_cmds(instance);
1716 process_fw_state_change_wq(&instance->work_init);
1717 }
1718
1719 /**
1720 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1721 * @instance: Adapter soft state
1722 *
1723 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
1724 * complete all its outstanding commands. Returns error if one or more IOs
1725 * are pending after this time period. It also marks the controller dead.
1726 */
1727 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1728 {
1729 int i;
1730 u32 reset_index;
1731 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
1732 u8 adprecovery;
1733 unsigned long flags;
1734 struct list_head clist_local;
1735 struct megasas_cmd *reset_cmd;
1736 u32 fw_state;
1737 u8 kill_adapter_flag;
1738
1739 spin_lock_irqsave(&instance->hba_lock, flags);
1740 adprecovery = instance->adprecovery;
1741 spin_unlock_irqrestore(&instance->hba_lock, flags);
1742
1743 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1744
1745 INIT_LIST_HEAD(&clist_local);
1746 spin_lock_irqsave(&instance->hba_lock, flags);
1747 list_splice_init(&instance->internal_reset_pending_q,
1748 &clist_local);
1749 spin_unlock_irqrestore(&instance->hba_lock, flags);
1750
1751 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
1752 for (i = 0; i < wait_time; i++) {
1753 msleep(1000);
1754 spin_lock_irqsave(&instance->hba_lock, flags);
1755 adprecovery = instance->adprecovery;
1756 spin_unlock_irqrestore(&instance->hba_lock, flags);
1757 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
1758 break;
1759 }
1760
1761 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1762 printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
1763 spin_lock_irqsave(&instance->hba_lock, flags);
1764 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1765 spin_unlock_irqrestore(&instance->hba_lock, flags);
1766 return FAILED;
1767 }
1768
1769 reset_index = 0;
1770 while (!list_empty(&clist_local)) {
1771 reset_cmd = list_entry((&clist_local)->next,
1772 struct megasas_cmd, list);
1773 list_del_init(&reset_cmd->list);
1774 if (reset_cmd->scmd) {
1775 reset_cmd->scmd->result = DID_RESET << 16;
1776 printk(KERN_NOTICE "%d:%p reset [%02x]\n",
1777 reset_index, reset_cmd,
1778 reset_cmd->scmd->cmnd[0]);
1779
1780 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
1781 megasas_return_cmd(instance, reset_cmd);
1782 } else if (reset_cmd->sync_cmd) {
1783 printk(KERN_NOTICE "megasas:%p synch cmds"
1784 "reset queue\n",
1785 reset_cmd);
1786
1787 reset_cmd->cmd_status = ENODATA;
1788 instance->instancet->fire_cmd(instance,
1789 reset_cmd->frame_phys_addr,
1790 0, instance->reg_set);
1791 } else {
1792 printk(KERN_NOTICE "megasas: %p unexpected"
1793 "cmds lst\n",
1794 reset_cmd);
1795 }
1796 reset_index++;
1797 }
1798
1799 return SUCCESS;
1800 }
1801
1802 for (i = 0; i < resetwaittime; i++) {
1803
1804 int outstanding = atomic_read(&instance->fw_outstanding);
1805
1806 if (!outstanding)
1807 break;
1808
1809 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1810 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
1811 "commands to complete\n",i,outstanding);
1812 /*
1813 * Call cmd completion routine. Cmd to be
1814 * be completed directly without depending on isr.
1815 */
1816 megasas_complete_cmd_dpc((unsigned long)instance);
1817 }
1818
1819 msleep(1000);
1820 }
1821
1822 i = 0;
1823 kill_adapter_flag = 0;
1824 do {
1825 fw_state = instance->instancet->read_fw_status_reg(
1826 instance->reg_set) & MFI_STATE_MASK;
1827 if ((fw_state == MFI_STATE_FAULT) &&
1828 (instance->disableOnlineCtrlReset == 0)) {
1829 if (i == 3) {
1830 kill_adapter_flag = 2;
1831 break;
1832 }
1833 megasas_do_ocr(instance);
1834 kill_adapter_flag = 1;
1835
1836 /* wait for 1 secs to let FW finish the pending cmds */
1837 msleep(1000);
1838 }
1839 i++;
1840 } while (i <= 3);
1841
1842 if (atomic_read(&instance->fw_outstanding) &&
1843 !kill_adapter_flag) {
1844 if (instance->disableOnlineCtrlReset == 0) {
1845
1846 megasas_do_ocr(instance);
1847
1848 /* wait for 5 secs to let FW finish the pending cmds */
1849 for (i = 0; i < wait_time; i++) {
1850 int outstanding =
1851 atomic_read(&instance->fw_outstanding);
1852 if (!outstanding)
1853 return SUCCESS;
1854 msleep(1000);
1855 }
1856 }
1857 }
1858
1859 if (atomic_read(&instance->fw_outstanding) ||
1860 (kill_adapter_flag == 2)) {
1861 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
1862 /*
1863 * Send signal to FW to stop processing any pending cmds.
1864 * The controller will be taken offline by the OS now.
1865 */
1866 if ((instance->pdev->device ==
1867 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1868 (instance->pdev->device ==
1869 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1870 writel(MFI_STOP_ADP,
1871 &instance->reg_set->doorbell);
1872 } else {
1873 writel(MFI_STOP_ADP,
1874 &instance->reg_set->inbound_doorbell);
1875 }
1876 megasas_dump_pending_frames(instance);
1877 spin_lock_irqsave(&instance->hba_lock, flags);
1878 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1879 spin_unlock_irqrestore(&instance->hba_lock, flags);
1880 return FAILED;
1881 }
1882
1883 printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
1884
1885 return SUCCESS;
1886 }
1887
1888 /**
1889 * megasas_generic_reset - Generic reset routine
1890 * @scmd: Mid-layer SCSI command
1891 *
1892 * This routine implements a generic reset handler for device, bus and host
1893 * reset requests. Device, bus and host specific reset handlers can use this
1894 * function after they do their specific tasks.
1895 */
1896 static int megasas_generic_reset(struct scsi_cmnd *scmd)
1897 {
1898 int ret_val;
1899 struct megasas_instance *instance;
1900
1901 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1902
1903 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
1904 scmd->cmnd[0], scmd->retries);
1905
1906 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1907 printk(KERN_ERR "megasas: cannot recover from previous reset "
1908 "failures\n");
1909 return FAILED;
1910 }
1911
1912 ret_val = megasas_wait_for_outstanding(instance);
1913 if (ret_val == SUCCESS)
1914 printk(KERN_NOTICE "megasas: reset successful \n");
1915 else
1916 printk(KERN_ERR "megasas: failed to do reset\n");
1917
1918 return ret_val;
1919 }
1920
1921 /**
1922 * megasas_reset_timer - quiesce the adapter if required
1923 * @scmd: scsi cmnd
1924 *
1925 * Sets the FW busy flag and reduces the host->can_queue if the
1926 * cmd has not been completed within the timeout period.
1927 */
1928 static enum
1929 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
1930 {
1931 struct megasas_instance *instance;
1932 unsigned long flags;
1933
1934 if (time_after(jiffies, scmd->jiffies_at_alloc +
1935 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
1936 return BLK_EH_NOT_HANDLED;
1937 }
1938
1939 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1940 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1941 /* FW is busy, throttle IO */
1942 spin_lock_irqsave(instance->host->host_lock, flags);
1943
1944 instance->host->can_queue = instance->throttlequeuedepth;
1945 instance->last_time = jiffies;
1946 instance->flag |= MEGASAS_FW_BUSY;
1947
1948 spin_unlock_irqrestore(instance->host->host_lock, flags);
1949 }
1950 return BLK_EH_RESET_TIMER;
1951 }
1952
1953 /**
1954 * megasas_reset_device - Device reset handler entry point
1955 */
1956 static int megasas_reset_device(struct scsi_cmnd *scmd)
1957 {
1958 int ret;
1959
1960 /*
1961 * First wait for all commands to complete
1962 */
1963 ret = megasas_generic_reset(scmd);
1964
1965 return ret;
1966 }
1967
1968 /**
1969 * megasas_reset_bus_host - Bus & host reset handler entry point
1970 */
1971 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1972 {
1973 int ret;
1974 struct megasas_instance *instance;
1975 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1976
1977 /*
1978 * First wait for all commands to complete
1979 */
1980 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1981 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1982 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
1983 ret = megasas_reset_fusion(scmd->device->host);
1984 else
1985 ret = megasas_generic_reset(scmd);
1986
1987 return ret;
1988 }
1989
1990 /**
1991 * megasas_bios_param - Returns disk geometry for a disk
1992 * @sdev: device handle
1993 * @bdev: block device
1994 * @capacity: drive capacity
1995 * @geom: geometry parameters
1996 */
1997 static int
1998 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1999 sector_t capacity, int geom[])
2000 {
2001 int heads;
2002 int sectors;
2003 sector_t cylinders;
2004 unsigned long tmp;
2005 /* Default heads (64) & sectors (32) */
2006 heads = 64;
2007 sectors = 32;
2008
2009 tmp = heads * sectors;
2010 cylinders = capacity;
2011
2012 sector_div(cylinders, tmp);
2013
2014 /*
2015 * Handle extended translation size for logical drives > 1Gb
2016 */
2017
2018 if (capacity >= 0x200000) {
2019 heads = 255;
2020 sectors = 63;
2021 tmp = heads*sectors;
2022 cylinders = capacity;
2023 sector_div(cylinders, tmp);
2024 }
2025
2026 geom[0] = heads;
2027 geom[1] = sectors;
2028 geom[2] = cylinders;
2029
2030 return 0;
2031 }
2032
2033 static void megasas_aen_polling(struct work_struct *work);
2034
2035 /**
2036 * megasas_service_aen - Processes an event notification
2037 * @instance: Adapter soft state
2038 * @cmd: AEN command completed by the ISR
2039 *
2040 * For AEN, driver sends a command down to FW that is held by the FW till an
2041 * event occurs. When an event of interest occurs, FW completes the command
2042 * that it was previously holding.
2043 *
2044 * This routines sends SIGIO signal to processes that have registered with the
2045 * driver for AEN.
2046 */
2047 static void
2048 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2049 {
2050 unsigned long flags;
2051 /*
2052 * Don't signal app if it is just an aborted previously registered aen
2053 */
2054 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2055 spin_lock_irqsave(&poll_aen_lock, flags);
2056 megasas_poll_wait_aen = 1;
2057 spin_unlock_irqrestore(&poll_aen_lock, flags);
2058 wake_up(&megasas_poll_wait);
2059 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
2060 }
2061 else
2062 cmd->abort_aen = 0;
2063
2064 instance->aen_cmd = NULL;
2065 megasas_return_cmd(instance, cmd);
2066
2067 if ((instance->unload == 0) &&
2068 ((instance->issuepend_done == 1))) {
2069 struct megasas_aen_event *ev;
2070 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2071 if (!ev) {
2072 printk(KERN_ERR "megasas_service_aen: out of memory\n");
2073 } else {
2074 ev->instance = instance;
2075 instance->ev = ev;
2076 INIT_DELAYED_WORK(&ev->hotplug_work,
2077 megasas_aen_polling);
2078 schedule_delayed_work(&ev->hotplug_work, 0);
2079 }
2080 }
2081 }
2082
2083 static int megasas_change_queue_depth(struct scsi_device *sdev,
2084 int queue_depth, int reason)
2085 {
2086 if (reason != SCSI_QDEPTH_DEFAULT)
2087 return -EOPNOTSUPP;
2088
2089 if (queue_depth > sdev->host->can_queue)
2090 queue_depth = sdev->host->can_queue;
2091 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
2092 queue_depth);
2093
2094 return queue_depth;
2095 }
2096
2097 /*
2098 * Scsi host template for megaraid_sas driver
2099 */
2100 static struct scsi_host_template megasas_template = {
2101
2102 .module = THIS_MODULE,
2103 .name = "LSI SAS based MegaRAID driver",
2104 .proc_name = "megaraid_sas",
2105 .slave_configure = megasas_slave_configure,
2106 .slave_alloc = megasas_slave_alloc,
2107 .queuecommand = megasas_queue_command,
2108 .eh_device_reset_handler = megasas_reset_device,
2109 .eh_bus_reset_handler = megasas_reset_bus_host,
2110 .eh_host_reset_handler = megasas_reset_bus_host,
2111 .eh_timed_out = megasas_reset_timer,
2112 .bios_param = megasas_bios_param,
2113 .use_clustering = ENABLE_CLUSTERING,
2114 .change_queue_depth = megasas_change_queue_depth,
2115 };
2116
2117 /**
2118 * megasas_complete_int_cmd - Completes an internal command
2119 * @instance: Adapter soft state
2120 * @cmd: Command to be completed
2121 *
2122 * The megasas_issue_blocked_cmd() function waits for a command to complete
2123 * after it issues a command. This function wakes up that waiting routine by
2124 * calling wake_up() on the wait queue.
2125 */
2126 static void
2127 megasas_complete_int_cmd(struct megasas_instance *instance,
2128 struct megasas_cmd *cmd)
2129 {
2130 cmd->cmd_status = cmd->frame->io.cmd_status;
2131
2132 if (cmd->cmd_status == ENODATA) {
2133 cmd->cmd_status = 0;
2134 }
2135 wake_up(&instance->int_cmd_wait_q);
2136 }
2137
2138 /**
2139 * megasas_complete_abort - Completes aborting a command
2140 * @instance: Adapter soft state
2141 * @cmd: Cmd that was issued to abort another cmd
2142 *
2143 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2144 * after it issues an abort on a previously issued command. This function
2145 * wakes up all functions waiting on the same wait queue.
2146 */
2147 static void
2148 megasas_complete_abort(struct megasas_instance *instance,
2149 struct megasas_cmd *cmd)
2150 {
2151 if (cmd->sync_cmd) {
2152 cmd->sync_cmd = 0;
2153 cmd->cmd_status = 0;
2154 wake_up(&instance->abort_cmd_wait_q);
2155 }
2156
2157 return;
2158 }
2159
2160 /**
2161 * megasas_complete_cmd - Completes a command
2162 * @instance: Adapter soft state
2163 * @cmd: Command to be completed
2164 * @alt_status: If non-zero, use this value as status to
2165 * SCSI mid-layer instead of the value returned
2166 * by the FW. This should be used if caller wants
2167 * an alternate status (as in the case of aborted
2168 * commands)
2169 */
2170 void
2171 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2172 u8 alt_status)
2173 {
2174 int exception = 0;
2175 struct megasas_header *hdr = &cmd->frame->hdr;
2176 unsigned long flags;
2177 struct fusion_context *fusion = instance->ctrl_context;
2178
2179 /* flag for the retry reset */
2180 cmd->retry_for_fw_reset = 0;
2181
2182 if (cmd->scmd)
2183 cmd->scmd->SCp.ptr = NULL;
2184
2185 switch (hdr->cmd) {
2186 case MFI_CMD_INVALID:
2187 /* Some older 1068 controller FW may keep a pended
2188 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2189 when booting the kdump kernel. Ignore this command to
2190 prevent a kernel panic on shutdown of the kdump kernel. */
2191 printk(KERN_WARNING "megaraid_sas: MFI_CMD_INVALID command "
2192 "completed.\n");
2193 printk(KERN_WARNING "megaraid_sas: If you have a controller "
2194 "other than PERC5, please upgrade your firmware.\n");
2195 break;
2196 case MFI_CMD_PD_SCSI_IO:
2197 case MFI_CMD_LD_SCSI_IO:
2198
2199 /*
2200 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2201 * issued either through an IO path or an IOCTL path. If it
2202 * was via IOCTL, we will send it to internal completion.
2203 */
2204 if (cmd->sync_cmd) {
2205 cmd->sync_cmd = 0;
2206 megasas_complete_int_cmd(instance, cmd);
2207 break;
2208 }
2209
2210 case MFI_CMD_LD_READ:
2211 case MFI_CMD_LD_WRITE:
2212
2213 if (alt_status) {
2214 cmd->scmd->result = alt_status << 16;
2215 exception = 1;
2216 }
2217
2218 if (exception) {
2219
2220 atomic_dec(&instance->fw_outstanding);
2221
2222 scsi_dma_unmap(cmd->scmd);
2223 cmd->scmd->scsi_done(cmd->scmd);
2224 megasas_return_cmd(instance, cmd);
2225
2226 break;
2227 }
2228
2229 switch (hdr->cmd_status) {
2230
2231 case MFI_STAT_OK:
2232 cmd->scmd->result = DID_OK << 16;
2233 break;
2234
2235 case MFI_STAT_SCSI_IO_FAILED:
2236 case MFI_STAT_LD_INIT_IN_PROGRESS:
2237 cmd->scmd->result =
2238 (DID_ERROR << 16) | hdr->scsi_status;
2239 break;
2240
2241 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2242
2243 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2244
2245 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2246 memset(cmd->scmd->sense_buffer, 0,
2247 SCSI_SENSE_BUFFERSIZE);
2248 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2249 hdr->sense_len);
2250
2251 cmd->scmd->result |= DRIVER_SENSE << 24;
2252 }
2253
2254 break;
2255
2256 case MFI_STAT_LD_OFFLINE:
2257 case MFI_STAT_DEVICE_NOT_FOUND:
2258 cmd->scmd->result = DID_BAD_TARGET << 16;
2259 break;
2260
2261 default:
2262 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2263 hdr->cmd_status);
2264 cmd->scmd->result = DID_ERROR << 16;
2265 break;
2266 }
2267
2268 atomic_dec(&instance->fw_outstanding);
2269
2270 scsi_dma_unmap(cmd->scmd);
2271 cmd->scmd->scsi_done(cmd->scmd);
2272 megasas_return_cmd(instance, cmd);
2273
2274 break;
2275
2276 case MFI_CMD_SMP:
2277 case MFI_CMD_STP:
2278 case MFI_CMD_DCMD:
2279 /* Check for LD map update */
2280 if ((cmd->frame->dcmd.opcode == MR_DCMD_LD_MAP_GET_INFO) &&
2281 (cmd->frame->dcmd.mbox.b[1] == 1)) {
2282 spin_lock_irqsave(instance->host->host_lock, flags);
2283 if (cmd->frame->hdr.cmd_status != 0) {
2284 if (cmd->frame->hdr.cmd_status !=
2285 MFI_STAT_NOT_FOUND)
2286 printk(KERN_WARNING "megasas: map sync"
2287 "failed, status = 0x%x.\n",
2288 cmd->frame->hdr.cmd_status);
2289 else {
2290 megasas_return_cmd(instance, cmd);
2291 spin_unlock_irqrestore(
2292 instance->host->host_lock,
2293 flags);
2294 break;
2295 }
2296 } else
2297 instance->map_id++;
2298 megasas_return_cmd(instance, cmd);
2299 if (MR_ValidateMapInfo(
2300 fusion->ld_map[(instance->map_id & 1)],
2301 fusion->load_balance_info))
2302 fusion->fast_path_io = 1;
2303 else
2304 fusion->fast_path_io = 0;
2305 megasas_sync_map_info(instance);
2306 spin_unlock_irqrestore(instance->host->host_lock,
2307 flags);
2308 break;
2309 }
2310 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2311 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
2312 spin_lock_irqsave(&poll_aen_lock, flags);
2313 megasas_poll_wait_aen = 0;
2314 spin_unlock_irqrestore(&poll_aen_lock, flags);
2315 }
2316
2317 /*
2318 * See if got an event notification
2319 */
2320 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
2321 megasas_service_aen(instance, cmd);
2322 else
2323 megasas_complete_int_cmd(instance, cmd);
2324
2325 break;
2326
2327 case MFI_CMD_ABORT:
2328 /*
2329 * Cmd issued to abort another cmd returned
2330 */
2331 megasas_complete_abort(instance, cmd);
2332 break;
2333
2334 default:
2335 printk("megasas: Unknown command completed! [0x%X]\n",
2336 hdr->cmd);
2337 break;
2338 }
2339 }
2340
2341 /**
2342 * megasas_issue_pending_cmds_again - issue all pending cmds
2343 * in FW again because of the fw reset
2344 * @instance: Adapter soft state
2345 */
2346 static inline void
2347 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
2348 {
2349 struct megasas_cmd *cmd;
2350 struct list_head clist_local;
2351 union megasas_evt_class_locale class_locale;
2352 unsigned long flags;
2353 u32 seq_num;
2354
2355 INIT_LIST_HEAD(&clist_local);
2356 spin_lock_irqsave(&instance->hba_lock, flags);
2357 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
2358 spin_unlock_irqrestore(&instance->hba_lock, flags);
2359
2360 while (!list_empty(&clist_local)) {
2361 cmd = list_entry((&clist_local)->next,
2362 struct megasas_cmd, list);
2363 list_del_init(&cmd->list);
2364
2365 if (cmd->sync_cmd || cmd->scmd) {
2366 printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
2367 "detected to be pending while HBA reset.\n",
2368 cmd, cmd->scmd, cmd->sync_cmd);
2369
2370 cmd->retry_for_fw_reset++;
2371
2372 if (cmd->retry_for_fw_reset == 3) {
2373 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
2374 "was tried multiple times during reset."
2375 "Shutting down the HBA\n",
2376 cmd, cmd->scmd, cmd->sync_cmd);
2377 megaraid_sas_kill_hba(instance);
2378
2379 instance->adprecovery =
2380 MEGASAS_HW_CRITICAL_ERROR;
2381 return;
2382 }
2383 }
2384
2385 if (cmd->sync_cmd == 1) {
2386 if (cmd->scmd) {
2387 printk(KERN_NOTICE "megaraid_sas: unexpected"
2388 "cmd attached to internal command!\n");
2389 }
2390 printk(KERN_NOTICE "megasas: %p synchronous cmd"
2391 "on the internal reset queue,"
2392 "issue it again.\n", cmd);
2393 cmd->cmd_status = ENODATA;
2394 instance->instancet->fire_cmd(instance,
2395 cmd->frame_phys_addr ,
2396 0, instance->reg_set);
2397 } else if (cmd->scmd) {
2398 printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
2399 "detected on the internal queue, issue again.\n",
2400 cmd, cmd->scmd->cmnd[0]);
2401
2402 atomic_inc(&instance->fw_outstanding);
2403 instance->instancet->fire_cmd(instance,
2404 cmd->frame_phys_addr,
2405 cmd->frame_count-1, instance->reg_set);
2406 } else {
2407 printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
2408 "internal reset defer list while re-issue!!\n",
2409 cmd);
2410 }
2411 }
2412
2413 if (instance->aen_cmd) {
2414 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
2415 megasas_return_cmd(instance, instance->aen_cmd);
2416
2417 instance->aen_cmd = NULL;
2418 }
2419
2420 /*
2421 * Initiate AEN (Asynchronous Event Notification)
2422 */
2423 seq_num = instance->last_seq_num;
2424 class_locale.members.reserved = 0;
2425 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2426 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2427
2428 megasas_register_aen(instance, seq_num, class_locale.word);
2429 }
2430
2431 /**
2432 * Move the internal reset pending commands to a deferred queue.
2433 *
2434 * We move the commands pending at internal reset time to a
2435 * pending queue. This queue would be flushed after successful
2436 * completion of the internal reset sequence. if the internal reset
2437 * did not complete in time, the kernel reset handler would flush
2438 * these commands.
2439 **/
2440 static void
2441 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
2442 {
2443 struct megasas_cmd *cmd;
2444 int i;
2445 u32 max_cmd = instance->max_fw_cmds;
2446 u32 defer_index;
2447 unsigned long flags;
2448
2449 defer_index = 0;
2450 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
2451 for (i = 0; i < max_cmd; i++) {
2452 cmd = instance->cmd_list[i];
2453 if (cmd->sync_cmd == 1 || cmd->scmd) {
2454 printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
2455 "on the defer queue as internal\n",
2456 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
2457
2458 if (!list_empty(&cmd->list)) {
2459 printk(KERN_NOTICE "megaraid_sas: ERROR while"
2460 " moving this cmd:%p, %d %p, it was"
2461 "discovered on some list?\n",
2462 cmd, cmd->sync_cmd, cmd->scmd);
2463
2464 list_del_init(&cmd->list);
2465 }
2466 defer_index++;
2467 list_add_tail(&cmd->list,
2468 &instance->internal_reset_pending_q);
2469 }
2470 }
2471 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
2472 }
2473
2474
2475 static void
2476 process_fw_state_change_wq(struct work_struct *work)
2477 {
2478 struct megasas_instance *instance =
2479 container_of(work, struct megasas_instance, work_init);
2480 u32 wait;
2481 unsigned long flags;
2482
2483 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
2484 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
2485 instance->adprecovery);
2486 return ;
2487 }
2488
2489 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
2490 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
2491 "state, restarting it...\n");
2492
2493 instance->instancet->disable_intr(instance->reg_set);
2494 atomic_set(&instance->fw_outstanding, 0);
2495
2496 atomic_set(&instance->fw_reset_no_pci_access, 1);
2497 instance->instancet->adp_reset(instance, instance->reg_set);
2498 atomic_set(&instance->fw_reset_no_pci_access, 0 );
2499
2500 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
2501 "initiating next stage...\n");
2502
2503 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
2504 "state 2 starting...\n");
2505
2506 /*waitting for about 20 second before start the second init*/
2507 for (wait = 0; wait < 30; wait++) {
2508 msleep(1000);
2509 }
2510
2511 if (megasas_transition_to_ready(instance, 1)) {
2512 printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
2513
2514 megaraid_sas_kill_hba(instance);
2515 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2516 return ;
2517 }
2518
2519 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2520 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2521 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
2522 ) {
2523 *instance->consumer = *instance->producer;
2524 } else {
2525 *instance->consumer = 0;
2526 *instance->producer = 0;
2527 }
2528
2529 megasas_issue_init_mfi(instance);
2530
2531 spin_lock_irqsave(&instance->hba_lock, flags);
2532 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2533 spin_unlock_irqrestore(&instance->hba_lock, flags);
2534 instance->instancet->enable_intr(instance->reg_set);
2535
2536 megasas_issue_pending_cmds_again(instance);
2537 instance->issuepend_done = 1;
2538 }
2539 return ;
2540 }
2541
2542 /**
2543 * megasas_deplete_reply_queue - Processes all completed commands
2544 * @instance: Adapter soft state
2545 * @alt_status: Alternate status to be returned to
2546 * SCSI mid-layer instead of the status
2547 * returned by the FW
2548 * Note: this must be called with hba lock held
2549 */
2550 static int
2551 megasas_deplete_reply_queue(struct megasas_instance *instance,
2552 u8 alt_status)
2553 {
2554 u32 mfiStatus;
2555 u32 fw_state;
2556
2557 if ((mfiStatus = instance->instancet->check_reset(instance,
2558 instance->reg_set)) == 1) {
2559 return IRQ_HANDLED;
2560 }
2561
2562 if ((mfiStatus = instance->instancet->clear_intr(
2563 instance->reg_set)
2564 ) == 0) {
2565 /* Hardware may not set outbound_intr_status in MSI-X mode */
2566 if (!instance->msix_vectors)
2567 return IRQ_NONE;
2568 }
2569
2570 instance->mfiStatus = mfiStatus;
2571
2572 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
2573 fw_state = instance->instancet->read_fw_status_reg(
2574 instance->reg_set) & MFI_STATE_MASK;
2575
2576 if (fw_state != MFI_STATE_FAULT) {
2577 printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
2578 fw_state);
2579 }
2580
2581 if ((fw_state == MFI_STATE_FAULT) &&
2582 (instance->disableOnlineCtrlReset == 0)) {
2583 printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
2584
2585 if ((instance->pdev->device ==
2586 PCI_DEVICE_ID_LSI_SAS1064R) ||
2587 (instance->pdev->device ==
2588 PCI_DEVICE_ID_DELL_PERC5) ||
2589 (instance->pdev->device ==
2590 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2591
2592 *instance->consumer =
2593 MEGASAS_ADPRESET_INPROG_SIGN;
2594 }
2595
2596
2597 instance->instancet->disable_intr(instance->reg_set);
2598 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2599 instance->issuepend_done = 0;
2600
2601 atomic_set(&instance->fw_outstanding, 0);
2602 megasas_internal_reset_defer_cmds(instance);
2603
2604 printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
2605 fw_state, instance->adprecovery);
2606
2607 schedule_work(&instance->work_init);
2608 return IRQ_HANDLED;
2609
2610 } else {
2611 printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
2612 fw_state, instance->disableOnlineCtrlReset);
2613 }
2614 }
2615
2616 tasklet_schedule(&instance->isr_tasklet);
2617 return IRQ_HANDLED;
2618 }
2619 /**
2620 * megasas_isr - isr entry point
2621 */
2622 static irqreturn_t megasas_isr(int irq, void *devp)
2623 {
2624 struct megasas_irq_context *irq_context = devp;
2625 struct megasas_instance *instance = irq_context->instance;
2626 unsigned long flags;
2627 irqreturn_t rc;
2628
2629 if (atomic_read(&instance->fw_reset_no_pci_access))
2630 return IRQ_HANDLED;
2631
2632 spin_lock_irqsave(&instance->hba_lock, flags);
2633 rc = megasas_deplete_reply_queue(instance, DID_OK);
2634 spin_unlock_irqrestore(&instance->hba_lock, flags);
2635
2636 return rc;
2637 }
2638
2639 /**
2640 * megasas_transition_to_ready - Move the FW to READY state
2641 * @instance: Adapter soft state
2642 *
2643 * During the initialization, FW passes can potentially be in any one of
2644 * several possible states. If the FW in operational, waiting-for-handshake
2645 * states, driver must take steps to bring it to ready state. Otherwise, it
2646 * has to wait for the ready state.
2647 */
2648 int
2649 megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
2650 {
2651 int i;
2652 u8 max_wait;
2653 u32 fw_state;
2654 u32 cur_state;
2655 u32 abs_state, curr_abs_state;
2656
2657 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2658
2659 if (fw_state != MFI_STATE_READY)
2660 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
2661 " state\n");
2662
2663 while (fw_state != MFI_STATE_READY) {
2664
2665 abs_state =
2666 instance->instancet->read_fw_status_reg(instance->reg_set);
2667
2668 switch (fw_state) {
2669
2670 case MFI_STATE_FAULT:
2671 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
2672 if (ocr) {
2673 max_wait = MEGASAS_RESET_WAIT_TIME;
2674 cur_state = MFI_STATE_FAULT;
2675 break;
2676 } else
2677 return -ENODEV;
2678
2679 case MFI_STATE_WAIT_HANDSHAKE:
2680 /*
2681 * Set the CLR bit in inbound doorbell
2682 */
2683 if ((instance->pdev->device ==
2684 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2685 (instance->pdev->device ==
2686 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2687 (instance->pdev->device ==
2688 PCI_DEVICE_ID_LSI_FUSION) ||
2689 (instance->pdev->device ==
2690 PCI_DEVICE_ID_LSI_INVADER) ||
2691 (instance->pdev->device ==
2692 PCI_DEVICE_ID_LSI_FURY)) {
2693 writel(
2694 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2695 &instance->reg_set->doorbell);
2696 } else {
2697 writel(
2698 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2699 &instance->reg_set->inbound_doorbell);
2700 }
2701
2702 max_wait = MEGASAS_RESET_WAIT_TIME;
2703 cur_state = MFI_STATE_WAIT_HANDSHAKE;
2704 break;
2705
2706 case MFI_STATE_BOOT_MESSAGE_PENDING:
2707 if ((instance->pdev->device ==
2708 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2709 (instance->pdev->device ==
2710 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2711 (instance->pdev->device ==
2712 PCI_DEVICE_ID_LSI_FUSION) ||
2713 (instance->pdev->device ==
2714 PCI_DEVICE_ID_LSI_INVADER) ||
2715 (instance->pdev->device ==
2716 PCI_DEVICE_ID_LSI_FURY)) {
2717 writel(MFI_INIT_HOTPLUG,
2718 &instance->reg_set->doorbell);
2719 } else
2720 writel(MFI_INIT_HOTPLUG,
2721 &instance->reg_set->inbound_doorbell);
2722
2723 max_wait = MEGASAS_RESET_WAIT_TIME;
2724 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
2725 break;
2726
2727 case MFI_STATE_OPERATIONAL:
2728 /*
2729 * Bring it to READY state; assuming max wait 10 secs
2730 */
2731 instance->instancet->disable_intr(instance->reg_set);
2732 if ((instance->pdev->device ==
2733 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2734 (instance->pdev->device ==
2735 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2736 (instance->pdev->device
2737 == PCI_DEVICE_ID_LSI_FUSION) ||
2738 (instance->pdev->device
2739 == PCI_DEVICE_ID_LSI_INVADER) ||
2740 (instance->pdev->device
2741 == PCI_DEVICE_ID_LSI_FURY)) {
2742 writel(MFI_RESET_FLAGS,
2743 &instance->reg_set->doorbell);
2744 if ((instance->pdev->device ==
2745 PCI_DEVICE_ID_LSI_FUSION) ||
2746 (instance->pdev->device ==
2747 PCI_DEVICE_ID_LSI_INVADER) ||
2748 (instance->pdev->device ==
2749 PCI_DEVICE_ID_LSI_FURY)) {
2750 for (i = 0; i < (10 * 1000); i += 20) {
2751 if (readl(
2752 &instance->
2753 reg_set->
2754 doorbell) & 1)
2755 msleep(20);
2756 else
2757 break;
2758 }
2759 }
2760 } else
2761 writel(MFI_RESET_FLAGS,
2762 &instance->reg_set->inbound_doorbell);
2763
2764 max_wait = MEGASAS_RESET_WAIT_TIME;
2765 cur_state = MFI_STATE_OPERATIONAL;
2766 break;
2767
2768 case MFI_STATE_UNDEFINED:
2769 /*
2770 * This state should not last for more than 2 seconds
2771 */
2772 max_wait = MEGASAS_RESET_WAIT_TIME;
2773 cur_state = MFI_STATE_UNDEFINED;
2774 break;
2775
2776 case MFI_STATE_BB_INIT:
2777 max_wait = MEGASAS_RESET_WAIT_TIME;
2778 cur_state = MFI_STATE_BB_INIT;
2779 break;
2780
2781 case MFI_STATE_FW_INIT:
2782 max_wait = MEGASAS_RESET_WAIT_TIME;
2783 cur_state = MFI_STATE_FW_INIT;
2784 break;
2785
2786 case MFI_STATE_FW_INIT_2:
2787 max_wait = MEGASAS_RESET_WAIT_TIME;
2788 cur_state = MFI_STATE_FW_INIT_2;
2789 break;
2790
2791 case MFI_STATE_DEVICE_SCAN:
2792 max_wait = MEGASAS_RESET_WAIT_TIME;
2793 cur_state = MFI_STATE_DEVICE_SCAN;
2794 break;
2795
2796 case MFI_STATE_FLUSH_CACHE:
2797 max_wait = MEGASAS_RESET_WAIT_TIME;
2798 cur_state = MFI_STATE_FLUSH_CACHE;
2799 break;
2800
2801 default:
2802 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
2803 fw_state);
2804 return -ENODEV;
2805 }
2806
2807 /*
2808 * The cur_state should not last for more than max_wait secs
2809 */
2810 for (i = 0; i < (max_wait * 1000); i++) {
2811 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
2812 MFI_STATE_MASK ;
2813 curr_abs_state =
2814 instance->instancet->read_fw_status_reg(instance->reg_set);
2815
2816 if (abs_state == curr_abs_state) {
2817 msleep(1);
2818 } else
2819 break;
2820 }
2821
2822 /*
2823 * Return error if fw_state hasn't changed after max_wait
2824 */
2825 if (curr_abs_state == abs_state) {
2826 printk(KERN_DEBUG "FW state [%d] hasn't changed "
2827 "in %d secs\n", fw_state, max_wait);
2828 return -ENODEV;
2829 }
2830 }
2831 printk(KERN_INFO "megasas: FW now in Ready state\n");
2832
2833 return 0;
2834 }
2835
2836 /**
2837 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
2838 * @instance: Adapter soft state
2839 */
2840 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
2841 {
2842 int i;
2843 u32 max_cmd = instance->max_mfi_cmds;
2844 struct megasas_cmd *cmd;
2845
2846 if (!instance->frame_dma_pool)
2847 return;
2848
2849 /*
2850 * Return all frames to pool
2851 */
2852 for (i = 0; i < max_cmd; i++) {
2853
2854 cmd = instance->cmd_list[i];
2855
2856 if (cmd->frame)
2857 pci_pool_free(instance->frame_dma_pool, cmd->frame,
2858 cmd->frame_phys_addr);
2859
2860 if (cmd->sense)
2861 pci_pool_free(instance->sense_dma_pool, cmd->sense,
2862 cmd->sense_phys_addr);
2863 }
2864
2865 /*
2866 * Now destroy the pool itself
2867 */
2868 pci_pool_destroy(instance->frame_dma_pool);
2869 pci_pool_destroy(instance->sense_dma_pool);
2870
2871 instance->frame_dma_pool = NULL;
2872 instance->sense_dma_pool = NULL;
2873 }
2874
2875 /**
2876 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2877 * @instance: Adapter soft state
2878 *
2879 * Each command packet has an embedded DMA memory buffer that is used for
2880 * filling MFI frame and the SG list that immediately follows the frame. This
2881 * function creates those DMA memory buffers for each command packet by using
2882 * PCI pool facility.
2883 */
2884 static int megasas_create_frame_pool(struct megasas_instance *instance)
2885 {
2886 int i;
2887 u32 max_cmd;
2888 u32 sge_sz;
2889 u32 sgl_sz;
2890 u32 total_sz;
2891 u32 frame_count;
2892 struct megasas_cmd *cmd;
2893
2894 max_cmd = instance->max_mfi_cmds;
2895
2896 /*
2897 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2898 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2899 */
2900 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2901 sizeof(struct megasas_sge32);
2902
2903 if (instance->flag_ieee) {
2904 sge_sz = sizeof(struct megasas_sge_skinny);
2905 }
2906
2907 /*
2908 * Calculated the number of 64byte frames required for SGL
2909 */
2910 sgl_sz = sge_sz * instance->max_num_sge;
2911 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
2912 frame_count = 15;
2913
2914 /*
2915 * We need one extra frame for the MFI command
2916 */
2917 frame_count++;
2918
2919 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2920 /*
2921 * Use DMA pool facility provided by PCI layer
2922 */
2923 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2924 instance->pdev, total_sz, 64,
2925 0);
2926
2927 if (!instance->frame_dma_pool) {
2928 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2929 return -ENOMEM;
2930 }
2931
2932 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2933 instance->pdev, 128, 4, 0);
2934
2935 if (!instance->sense_dma_pool) {
2936 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2937
2938 pci_pool_destroy(instance->frame_dma_pool);
2939 instance->frame_dma_pool = NULL;
2940
2941 return -ENOMEM;
2942 }
2943
2944 /*
2945 * Allocate and attach a frame to each of the commands in cmd_list.
2946 * By making cmd->index as the context instead of the &cmd, we can
2947 * always use 32bit context regardless of the architecture
2948 */
2949 for (i = 0; i < max_cmd; i++) {
2950
2951 cmd = instance->cmd_list[i];
2952
2953 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2954 GFP_KERNEL, &cmd->frame_phys_addr);
2955
2956 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2957 GFP_KERNEL, &cmd->sense_phys_addr);
2958
2959 /*
2960 * megasas_teardown_frame_pool() takes care of freeing
2961 * whatever has been allocated
2962 */
2963 if (!cmd->frame || !cmd->sense) {
2964 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2965 megasas_teardown_frame_pool(instance);
2966 return -ENOMEM;
2967 }
2968
2969 memset(cmd->frame, 0, total_sz);
2970 cmd->frame->io.context = cmd->index;
2971 cmd->frame->io.pad_0 = 0;
2972 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
2973 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
2974 (instance->pdev->device != PCI_DEVICE_ID_LSI_FURY) &&
2975 (reset_devices))
2976 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
2977 }
2978
2979 return 0;
2980 }
2981
2982 /**
2983 * megasas_free_cmds - Free all the cmds in the free cmd pool
2984 * @instance: Adapter soft state
2985 */
2986 void megasas_free_cmds(struct megasas_instance *instance)
2987 {
2988 int i;
2989 /* First free the MFI frame pool */
2990 megasas_teardown_frame_pool(instance);
2991
2992 /* Free all the commands in the cmd_list */
2993 for (i = 0; i < instance->max_mfi_cmds; i++)
2994
2995 kfree(instance->cmd_list[i]);
2996
2997 /* Free the cmd_list buffer itself */
2998 kfree(instance->cmd_list);
2999 instance->cmd_list = NULL;
3000
3001 INIT_LIST_HEAD(&instance->cmd_pool);
3002 }
3003
3004 /**
3005 * megasas_alloc_cmds - Allocates the command packets
3006 * @instance: Adapter soft state
3007 *
3008 * Each command that is issued to the FW, whether IO commands from the OS or
3009 * internal commands like IOCTLs, are wrapped in local data structure called
3010 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3011 * the FW.
3012 *
3013 * Each frame has a 32-bit field called context (tag). This context is used
3014 * to get back the megasas_cmd from the frame when a frame gets completed in
3015 * the ISR. Typically the address of the megasas_cmd itself would be used as
3016 * the context. But we wanted to keep the differences between 32 and 64 bit
3017 * systems to the mininum. We always use 32 bit integers for the context. In
3018 * this driver, the 32 bit values are the indices into an array cmd_list.
3019 * This array is used only to look up the megasas_cmd given the context. The
3020 * free commands themselves are maintained in a linked list called cmd_pool.
3021 */
3022 int megasas_alloc_cmds(struct megasas_instance *instance)
3023 {
3024 int i;
3025 int j;
3026 u32 max_cmd;
3027 struct megasas_cmd *cmd;
3028
3029 max_cmd = instance->max_mfi_cmds;
3030
3031 /*
3032 * instance->cmd_list is an array of struct megasas_cmd pointers.
3033 * Allocate the dynamic array first and then allocate individual
3034 * commands.
3035 */
3036 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
3037
3038 if (!instance->cmd_list) {
3039 printk(KERN_DEBUG "megasas: out of memory\n");
3040 return -ENOMEM;
3041 }
3042
3043 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
3044
3045 for (i = 0; i < max_cmd; i++) {
3046 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3047 GFP_KERNEL);
3048
3049 if (!instance->cmd_list[i]) {
3050
3051 for (j = 0; j < i; j++)
3052 kfree(instance->cmd_list[j]);
3053
3054 kfree(instance->cmd_list);
3055 instance->cmd_list = NULL;
3056
3057 return -ENOMEM;
3058 }
3059 }
3060
3061 /*
3062 * Add all the commands to command pool (instance->cmd_pool)
3063 */
3064 for (i = 0; i < max_cmd; i++) {
3065 cmd = instance->cmd_list[i];
3066 memset(cmd, 0, sizeof(struct megasas_cmd));
3067 cmd->index = i;
3068 cmd->scmd = NULL;
3069 cmd->instance = instance;
3070
3071 list_add_tail(&cmd->list, &instance->cmd_pool);
3072 }
3073
3074 /*
3075 * Create a frame pool and assign one frame to each cmd
3076 */
3077 if (megasas_create_frame_pool(instance)) {
3078 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
3079 megasas_free_cmds(instance);
3080 }
3081
3082 return 0;
3083 }
3084
3085 /*
3086 * megasas_get_pd_list_info - Returns FW's pd_list structure
3087 * @instance: Adapter soft state
3088 * @pd_list: pd_list structure
3089 *
3090 * Issues an internal command (DCMD) to get the FW's controller PD
3091 * list structure. This information is mainly used to find out SYSTEM
3092 * supported by the FW.
3093 */
3094 static int
3095 megasas_get_pd_list(struct megasas_instance *instance)
3096 {
3097 int ret = 0, pd_index = 0;
3098 struct megasas_cmd *cmd;
3099 struct megasas_dcmd_frame *dcmd;
3100 struct MR_PD_LIST *ci;
3101 struct MR_PD_ADDRESS *pd_addr;
3102 dma_addr_t ci_h = 0;
3103
3104 cmd = megasas_get_cmd(instance);
3105
3106 if (!cmd) {
3107 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
3108 return -ENOMEM;
3109 }
3110
3111 dcmd = &cmd->frame->dcmd;
3112
3113 ci = pci_alloc_consistent(instance->pdev,
3114 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3115
3116 if (!ci) {
3117 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
3118 megasas_return_cmd(instance, cmd);
3119 return -ENOMEM;
3120 }
3121
3122 memset(ci, 0, sizeof(*ci));
3123 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3124
3125 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3126 dcmd->mbox.b[1] = 0;
3127 dcmd->cmd = MFI_CMD_DCMD;
3128 dcmd->cmd_status = 0xFF;
3129 dcmd->sge_count = 1;
3130 dcmd->flags = MFI_FRAME_DIR_READ;
3131 dcmd->timeout = 0;
3132 dcmd->pad_0 = 0;
3133 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3134 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
3135 dcmd->sgl.sge32[0].phys_addr = ci_h;
3136 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3137
3138 if (!megasas_issue_polled(instance, cmd)) {
3139 ret = 0;
3140 } else {
3141 ret = -1;
3142 }
3143
3144 /*
3145 * the following function will get the instance PD LIST.
3146 */
3147
3148 pd_addr = ci->addr;
3149
3150 if ( ret == 0 &&
3151 (ci->count <
3152 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3153
3154 memset(instance->pd_list, 0,
3155 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3156
3157 for (pd_index = 0; pd_index < ci->count; pd_index++) {
3158
3159 instance->pd_list[pd_addr->deviceId].tid =
3160 pd_addr->deviceId;
3161 instance->pd_list[pd_addr->deviceId].driveType =
3162 pd_addr->scsiDevType;
3163 instance->pd_list[pd_addr->deviceId].driveState =
3164 MR_PD_STATE_SYSTEM;
3165 pd_addr++;
3166 }
3167 }
3168
3169 pci_free_consistent(instance->pdev,
3170 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3171 ci, ci_h);
3172 megasas_return_cmd(instance, cmd);
3173
3174 return ret;
3175 }
3176
3177 /*
3178 * megasas_get_ld_list_info - Returns FW's ld_list structure
3179 * @instance: Adapter soft state
3180 * @ld_list: ld_list structure
3181 *
3182 * Issues an internal command (DCMD) to get the FW's controller PD
3183 * list structure. This information is mainly used to find out SYSTEM
3184 * supported by the FW.
3185 */
3186 static int
3187 megasas_get_ld_list(struct megasas_instance *instance)
3188 {
3189 int ret = 0, ld_index = 0, ids = 0;
3190 struct megasas_cmd *cmd;
3191 struct megasas_dcmd_frame *dcmd;
3192 struct MR_LD_LIST *ci;
3193 dma_addr_t ci_h = 0;
3194
3195 cmd = megasas_get_cmd(instance);
3196
3197 if (!cmd) {
3198 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
3199 return -ENOMEM;
3200 }
3201
3202 dcmd = &cmd->frame->dcmd;
3203
3204 ci = pci_alloc_consistent(instance->pdev,
3205 sizeof(struct MR_LD_LIST),
3206 &ci_h);
3207
3208 if (!ci) {
3209 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
3210 megasas_return_cmd(instance, cmd);
3211 return -ENOMEM;
3212 }
3213
3214 memset(ci, 0, sizeof(*ci));
3215 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3216
3217 dcmd->cmd = MFI_CMD_DCMD;
3218 dcmd->cmd_status = 0xFF;
3219 dcmd->sge_count = 1;
3220 dcmd->flags = MFI_FRAME_DIR_READ;
3221 dcmd->timeout = 0;
3222 dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
3223 dcmd->opcode = MR_DCMD_LD_GET_LIST;
3224 dcmd->sgl.sge32[0].phys_addr = ci_h;
3225 dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
3226 dcmd->pad_0 = 0;
3227
3228 if (!megasas_issue_polled(instance, cmd)) {
3229 ret = 0;
3230 } else {
3231 ret = -1;
3232 }
3233
3234 /* the following function will get the instance PD LIST */
3235
3236 if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
3237 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3238
3239 for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
3240 if (ci->ldList[ld_index].state != 0) {
3241 ids = ci->ldList[ld_index].ref.targetId;
3242 instance->ld_ids[ids] =
3243 ci->ldList[ld_index].ref.targetId;
3244 }
3245 }
3246 }
3247
3248 pci_free_consistent(instance->pdev,
3249 sizeof(struct MR_LD_LIST),
3250 ci,
3251 ci_h);
3252
3253 megasas_return_cmd(instance, cmd);
3254 return ret;
3255 }
3256
3257 /**
3258 * megasas_get_controller_info - Returns FW's controller structure
3259 * @instance: Adapter soft state
3260 * @ctrl_info: Controller information structure
3261 *
3262 * Issues an internal command (DCMD) to get the FW's controller structure.
3263 * This information is mainly used to find out the maximum IO transfer per
3264 * command supported by the FW.
3265 */
3266 static int
3267 megasas_get_ctrl_info(struct megasas_instance *instance,
3268 struct megasas_ctrl_info *ctrl_info)
3269 {
3270 int ret = 0;
3271 struct megasas_cmd *cmd;
3272 struct megasas_dcmd_frame *dcmd;
3273 struct megasas_ctrl_info *ci;
3274 dma_addr_t ci_h = 0;
3275
3276 cmd = megasas_get_cmd(instance);
3277
3278 if (!cmd) {
3279 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
3280 return -ENOMEM;
3281 }
3282
3283 dcmd = &cmd->frame->dcmd;
3284
3285 ci = pci_alloc_consistent(instance->pdev,
3286 sizeof(struct megasas_ctrl_info), &ci_h);
3287
3288 if (!ci) {
3289 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
3290 megasas_return_cmd(instance, cmd);
3291 return -ENOMEM;
3292 }
3293
3294 memset(ci, 0, sizeof(*ci));
3295 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3296
3297 dcmd->cmd = MFI_CMD_DCMD;
3298 dcmd->cmd_status = 0xFF;
3299 dcmd->sge_count = 1;
3300 dcmd->flags = MFI_FRAME_DIR_READ;
3301 dcmd->timeout = 0;
3302 dcmd->pad_0 = 0;
3303 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
3304 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
3305 dcmd->sgl.sge32[0].phys_addr = ci_h;
3306 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
3307
3308 if (!megasas_issue_polled(instance, cmd)) {
3309 ret = 0;
3310 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
3311 } else {
3312 ret = -1;
3313 }
3314
3315 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
3316 ci, ci_h);
3317
3318 megasas_return_cmd(instance, cmd);
3319 return ret;
3320 }
3321
3322 /**
3323 * megasas_issue_init_mfi - Initializes the FW
3324 * @instance: Adapter soft state
3325 *
3326 * Issues the INIT MFI cmd
3327 */
3328 static int
3329 megasas_issue_init_mfi(struct megasas_instance *instance)
3330 {
3331 u32 context;
3332
3333 struct megasas_cmd *cmd;
3334
3335 struct megasas_init_frame *init_frame;
3336 struct megasas_init_queue_info *initq_info;
3337 dma_addr_t init_frame_h;
3338 dma_addr_t initq_info_h;
3339
3340 /*
3341 * Prepare a init frame. Note the init frame points to queue info
3342 * structure. Each frame has SGL allocated after first 64 bytes. For
3343 * this frame - since we don't need any SGL - we use SGL's space as
3344 * queue info structure
3345 *
3346 * We will not get a NULL command below. We just created the pool.
3347 */
3348 cmd = megasas_get_cmd(instance);
3349
3350 init_frame = (struct megasas_init_frame *)cmd->frame;
3351 initq_info = (struct megasas_init_queue_info *)
3352 ((unsigned long)init_frame + 64);
3353
3354 init_frame_h = cmd->frame_phys_addr;
3355 initq_info_h = init_frame_h + 64;
3356
3357 context = init_frame->context;
3358 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
3359 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
3360 init_frame->context = context;
3361
3362 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
3363 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
3364
3365 initq_info->producer_index_phys_addr_lo = instance->producer_h;
3366 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
3367
3368 init_frame->cmd = MFI_CMD_INIT;
3369 init_frame->cmd_status = 0xFF;
3370 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
3371
3372 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
3373
3374 /*
3375 * disable the intr before firing the init frame to FW
3376 */
3377 instance->instancet->disable_intr(instance->reg_set);
3378
3379 /*
3380 * Issue the init frame in polled mode
3381 */
3382
3383 if (megasas_issue_polled(instance, cmd)) {
3384 printk(KERN_ERR "megasas: Failed to init firmware\n");
3385 megasas_return_cmd(instance, cmd);
3386 goto fail_fw_init;
3387 }
3388
3389 megasas_return_cmd(instance, cmd);
3390
3391 return 0;
3392
3393 fail_fw_init:
3394 return -EINVAL;
3395 }
3396
3397 static u32
3398 megasas_init_adapter_mfi(struct megasas_instance *instance)
3399 {
3400 struct megasas_register_set __iomem *reg_set;
3401 u32 context_sz;
3402 u32 reply_q_sz;
3403
3404 reg_set = instance->reg_set;
3405
3406 /*
3407 * Get various operational parameters from status register
3408 */
3409 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
3410 /*
3411 * Reduce the max supported cmds by 1. This is to ensure that the
3412 * reply_q_sz (1 more than the max cmd that driver may send)
3413 * does not exceed max cmds that the FW can support
3414 */
3415 instance->max_fw_cmds = instance->max_fw_cmds-1;
3416 instance->max_mfi_cmds = instance->max_fw_cmds;
3417 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
3418 0x10;
3419 /*
3420 * Create a pool of commands
3421 */
3422 if (megasas_alloc_cmds(instance))
3423 goto fail_alloc_cmds;
3424
3425 /*
3426 * Allocate memory for reply queue. Length of reply queue should
3427 * be _one_ more than the maximum commands handled by the firmware.
3428 *
3429 * Note: When FW completes commands, it places corresponding contex
3430 * values in this circular reply queue. This circular queue is a fairly
3431 * typical producer-consumer queue. FW is the producer (of completed
3432 * commands) and the driver is the consumer.
3433 */
3434 context_sz = sizeof(u32);
3435 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
3436
3437 instance->reply_queue = pci_alloc_consistent(instance->pdev,
3438 reply_q_sz,
3439 &instance->reply_queue_h);
3440
3441 if (!instance->reply_queue) {
3442 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
3443 goto fail_reply_queue;
3444 }
3445
3446 if (megasas_issue_init_mfi(instance))
3447 goto fail_fw_init;
3448
3449 instance->fw_support_ieee = 0;
3450 instance->fw_support_ieee =
3451 (instance->instancet->read_fw_status_reg(reg_set) &
3452 0x04000000);
3453
3454 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
3455 instance->fw_support_ieee);
3456
3457 if (instance->fw_support_ieee)
3458 instance->flag_ieee = 1;
3459
3460 return 0;
3461
3462 fail_fw_init:
3463
3464 pci_free_consistent(instance->pdev, reply_q_sz,
3465 instance->reply_queue, instance->reply_queue_h);
3466 fail_reply_queue:
3467 megasas_free_cmds(instance);
3468
3469 fail_alloc_cmds:
3470 return 1;
3471 }
3472
3473 /**
3474 * megasas_init_fw - Initializes the FW
3475 * @instance: Adapter soft state
3476 *
3477 * This is the main function for initializing firmware
3478 */
3479
3480 static int megasas_init_fw(struct megasas_instance *instance)
3481 {
3482 u32 max_sectors_1;
3483 u32 max_sectors_2;
3484 u32 tmp_sectors, msix_enable;
3485 struct megasas_register_set __iomem *reg_set;
3486 struct megasas_ctrl_info *ctrl_info;
3487 unsigned long bar_list;
3488 int i;
3489
3490 /* Find first memory bar */
3491 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
3492 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
3493 instance->base_addr = pci_resource_start(instance->pdev, instance->bar);
3494 if (pci_request_selected_regions(instance->pdev, instance->bar,
3495 "megasas: LSI")) {
3496 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
3497 return -EBUSY;
3498 }
3499
3500 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
3501
3502 if (!instance->reg_set) {
3503 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
3504 goto fail_ioremap;
3505 }
3506
3507 reg_set = instance->reg_set;
3508
3509 switch (instance->pdev->device) {
3510 case PCI_DEVICE_ID_LSI_FUSION:
3511 case PCI_DEVICE_ID_LSI_INVADER:
3512 case PCI_DEVICE_ID_LSI_FURY:
3513 instance->instancet = &megasas_instance_template_fusion;
3514 break;
3515 case PCI_DEVICE_ID_LSI_SAS1078R:
3516 case PCI_DEVICE_ID_LSI_SAS1078DE:
3517 instance->instancet = &megasas_instance_template_ppc;
3518 break;
3519 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
3520 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
3521 instance->instancet = &megasas_instance_template_gen2;
3522 break;
3523 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
3524 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
3525 instance->instancet = &megasas_instance_template_skinny;
3526 break;
3527 case PCI_DEVICE_ID_LSI_SAS1064R:
3528 case PCI_DEVICE_ID_DELL_PERC5:
3529 default:
3530 instance->instancet = &megasas_instance_template_xscale;
3531 break;
3532 }
3533
3534 /*
3535 * We expect the FW state to be READY
3536 */
3537 if (megasas_transition_to_ready(instance, 0))
3538 goto fail_ready_state;
3539
3540 /* Check if MSI-X is supported while in ready state */
3541 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
3542 0x4000000) >> 0x1a;
3543 if (msix_enable && !msix_disable) {
3544 /* Check max MSI-X vectors */
3545 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
3546 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
3547 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
3548 instance->msix_vectors = (readl(&instance->reg_set->
3549 outbound_scratch_pad_2
3550 ) & 0x1F) + 1;
3551 if (msix_vectors)
3552 instance->msix_vectors =
3553 min(msix_vectors,
3554 instance->msix_vectors);
3555 } else
3556 instance->msix_vectors = 1;
3557 /* Don't bother allocating more MSI-X vectors than cpus */
3558 instance->msix_vectors = min(instance->msix_vectors,
3559 (unsigned int)num_online_cpus());
3560 for (i = 0; i < instance->msix_vectors; i++)
3561 instance->msixentry[i].entry = i;
3562 i = pci_enable_msix(instance->pdev, instance->msixentry,
3563 instance->msix_vectors);
3564 if (i >= 0) {
3565 if (i) {
3566 if (!pci_enable_msix(instance->pdev,
3567 instance->msixentry, i))
3568 instance->msix_vectors = i;
3569 else
3570 instance->msix_vectors = 0;
3571 }
3572 } else
3573 instance->msix_vectors = 0;
3574 }
3575
3576 /* Get operational params, sge flags, send init cmd to controller */
3577 if (instance->instancet->init_adapter(instance))
3578 goto fail_init_adapter;
3579
3580 printk(KERN_ERR "megasas: INIT adapter done\n");
3581
3582 /** for passthrough
3583 * the following function will get the PD LIST.
3584 */
3585
3586 memset(instance->pd_list, 0 ,
3587 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
3588 megasas_get_pd_list(instance);
3589
3590 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3591 megasas_get_ld_list(instance);
3592
3593 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
3594
3595 /*
3596 * Compute the max allowed sectors per IO: The controller info has two
3597 * limits on max sectors. Driver should use the minimum of these two.
3598 *
3599 * 1 << stripe_sz_ops.min = max sectors per strip
3600 *
3601 * Note that older firmwares ( < FW ver 30) didn't report information
3602 * to calculate max_sectors_1. So the number ended up as zero always.
3603 */
3604 tmp_sectors = 0;
3605 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
3606
3607 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
3608 ctrl_info->max_strips_per_io;
3609 max_sectors_2 = ctrl_info->max_request_size;
3610
3611 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
3612 instance->disableOnlineCtrlReset =
3613 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
3614 }
3615
3616 instance->max_sectors_per_req = instance->max_num_sge *
3617 PAGE_SIZE / 512;
3618 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
3619 instance->max_sectors_per_req = tmp_sectors;
3620
3621 kfree(ctrl_info);
3622
3623 /* Check for valid throttlequeuedepth module parameter */
3624 if (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY ||
3625 instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) {
3626 if (throttlequeuedepth > (instance->max_fw_cmds -
3627 MEGASAS_SKINNY_INT_CMDS))
3628 instance->throttlequeuedepth =
3629 MEGASAS_THROTTLE_QUEUE_DEPTH;
3630 else
3631 instance->throttlequeuedepth = throttlequeuedepth;
3632 } else {
3633 if (throttlequeuedepth > (instance->max_fw_cmds -
3634 MEGASAS_INT_CMDS))
3635 instance->throttlequeuedepth =
3636 MEGASAS_THROTTLE_QUEUE_DEPTH;
3637 else
3638 instance->throttlequeuedepth = throttlequeuedepth;
3639 }
3640
3641 /*
3642 * Setup tasklet for cmd completion
3643 */
3644
3645 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
3646 (unsigned long)instance);
3647
3648 return 0;
3649
3650 fail_init_adapter:
3651 fail_ready_state:
3652 iounmap(instance->reg_set);
3653
3654 fail_ioremap:
3655 pci_release_selected_regions(instance->pdev, instance->bar);
3656
3657 return -EINVAL;
3658 }
3659
3660 /**
3661 * megasas_release_mfi - Reverses the FW initialization
3662 * @intance: Adapter soft state
3663 */
3664 static void megasas_release_mfi(struct megasas_instance *instance)
3665 {
3666 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
3667
3668 if (instance->reply_queue)
3669 pci_free_consistent(instance->pdev, reply_q_sz,
3670 instance->reply_queue, instance->reply_queue_h);
3671
3672 megasas_free_cmds(instance);
3673
3674 iounmap(instance->reg_set);
3675
3676 pci_release_selected_regions(instance->pdev, instance->bar);
3677 }
3678
3679 /**
3680 * megasas_get_seq_num - Gets latest event sequence numbers
3681 * @instance: Adapter soft state
3682 * @eli: FW event log sequence numbers information
3683 *
3684 * FW maintains a log of all events in a non-volatile area. Upper layers would
3685 * usually find out the latest sequence number of the events, the seq number at
3686 * the boot etc. They would "read" all the events below the latest seq number
3687 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3688 * number), they would subsribe to AEN (asynchronous event notification) and
3689 * wait for the events to happen.
3690 */
3691 static int
3692 megasas_get_seq_num(struct megasas_instance *instance,
3693 struct megasas_evt_log_info *eli)
3694 {
3695 struct megasas_cmd *cmd;
3696 struct megasas_dcmd_frame *dcmd;
3697 struct megasas_evt_log_info *el_info;
3698 dma_addr_t el_info_h = 0;
3699
3700 cmd = megasas_get_cmd(instance);
3701
3702 if (!cmd) {
3703 return -ENOMEM;
3704 }
3705
3706 dcmd = &cmd->frame->dcmd;
3707 el_info = pci_alloc_consistent(instance->pdev,
3708 sizeof(struct megasas_evt_log_info),
3709 &el_info_h);
3710
3711 if (!el_info) {
3712 megasas_return_cmd(instance, cmd);
3713 return -ENOMEM;
3714 }
3715
3716 memset(el_info, 0, sizeof(*el_info));
3717 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3718
3719 dcmd->cmd = MFI_CMD_DCMD;
3720 dcmd->cmd_status = 0x0;
3721 dcmd->sge_count = 1;
3722 dcmd->flags = MFI_FRAME_DIR_READ;
3723 dcmd->timeout = 0;
3724 dcmd->pad_0 = 0;
3725 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
3726 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
3727 dcmd->sgl.sge32[0].phys_addr = el_info_h;
3728 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
3729
3730 megasas_issue_blocked_cmd(instance, cmd);
3731
3732 /*
3733 * Copy the data back into callers buffer
3734 */
3735 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
3736
3737 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
3738 el_info, el_info_h);
3739
3740 megasas_return_cmd(instance, cmd);
3741
3742 return 0;
3743 }
3744
3745 /**
3746 * megasas_register_aen - Registers for asynchronous event notification
3747 * @instance: Adapter soft state
3748 * @seq_num: The starting sequence number
3749 * @class_locale: Class of the event
3750 *
3751 * This function subscribes for AEN for events beyond the @seq_num. It requests
3752 * to be notified if and only if the event is of type @class_locale
3753 */
3754 static int
3755 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
3756 u32 class_locale_word)
3757 {
3758 int ret_val;
3759 struct megasas_cmd *cmd;
3760 struct megasas_dcmd_frame *dcmd;
3761 union megasas_evt_class_locale curr_aen;
3762 union megasas_evt_class_locale prev_aen;
3763
3764 /*
3765 * If there an AEN pending already (aen_cmd), check if the
3766 * class_locale of that pending AEN is inclusive of the new
3767 * AEN request we currently have. If it is, then we don't have
3768 * to do anything. In other words, whichever events the current
3769 * AEN request is subscribing to, have already been subscribed
3770 * to.
3771 *
3772 * If the old_cmd is _not_ inclusive, then we have to abort
3773 * that command, form a class_locale that is superset of both
3774 * old and current and re-issue to the FW
3775 */
3776
3777 curr_aen.word = class_locale_word;
3778
3779 if (instance->aen_cmd) {
3780
3781 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
3782
3783 /*
3784 * A class whose enum value is smaller is inclusive of all
3785 * higher values. If a PROGRESS (= -1) was previously
3786 * registered, then a new registration requests for higher
3787 * classes need not be sent to FW. They are automatically
3788 * included.
3789 *
3790 * Locale numbers don't have such hierarchy. They are bitmap
3791 * values
3792 */
3793 if ((prev_aen.members.class <= curr_aen.members.class) &&
3794 !((prev_aen.members.locale & curr_aen.members.locale) ^
3795 curr_aen.members.locale)) {
3796 /*
3797 * Previously issued event registration includes
3798 * current request. Nothing to do.
3799 */
3800 return 0;
3801 } else {
3802 curr_aen.members.locale |= prev_aen.members.locale;
3803
3804 if (prev_aen.members.class < curr_aen.members.class)
3805 curr_aen.members.class = prev_aen.members.class;
3806
3807 instance->aen_cmd->abort_aen = 1;
3808 ret_val = megasas_issue_blocked_abort_cmd(instance,
3809 instance->
3810 aen_cmd);
3811
3812 if (ret_val) {
3813 printk(KERN_DEBUG "megasas: Failed to abort "
3814 "previous AEN command\n");
3815 return ret_val;
3816 }
3817 }
3818 }
3819
3820 cmd = megasas_get_cmd(instance);
3821
3822 if (!cmd)
3823 return -ENOMEM;
3824
3825 dcmd = &cmd->frame->dcmd;
3826
3827 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
3828
3829 /*
3830 * Prepare DCMD for aen registration
3831 */
3832 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3833
3834 dcmd->cmd = MFI_CMD_DCMD;
3835 dcmd->cmd_status = 0x0;
3836 dcmd->sge_count = 1;
3837 dcmd->flags = MFI_FRAME_DIR_READ;
3838 dcmd->timeout = 0;
3839 dcmd->pad_0 = 0;
3840 instance->last_seq_num = seq_num;
3841 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
3842 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
3843 dcmd->mbox.w[0] = seq_num;
3844 dcmd->mbox.w[1] = curr_aen.word;
3845 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
3846 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
3847
3848 if (instance->aen_cmd != NULL) {
3849 megasas_return_cmd(instance, cmd);
3850 return 0;
3851 }
3852
3853 /*
3854 * Store reference to the cmd used to register for AEN. When an
3855 * application wants us to register for AEN, we have to abort this
3856 * cmd and re-register with a new EVENT LOCALE supplied by that app
3857 */
3858 instance->aen_cmd = cmd;
3859
3860 /*
3861 * Issue the aen registration frame
3862 */
3863 instance->instancet->issue_dcmd(instance, cmd);
3864
3865 return 0;
3866 }
3867
3868 /**
3869 * megasas_start_aen - Subscribes to AEN during driver load time
3870 * @instance: Adapter soft state
3871 */
3872 static int megasas_start_aen(struct megasas_instance *instance)
3873 {
3874 struct megasas_evt_log_info eli;
3875 union megasas_evt_class_locale class_locale;
3876
3877 /*
3878 * Get the latest sequence number from FW
3879 */
3880 memset(&eli, 0, sizeof(eli));
3881
3882 if (megasas_get_seq_num(instance, &eli))
3883 return -1;
3884
3885 /*
3886 * Register AEN with FW for latest sequence number plus 1
3887 */
3888 class_locale.members.reserved = 0;
3889 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3890 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3891
3892 return megasas_register_aen(instance, eli.newest_seq_num + 1,
3893 class_locale.word);
3894 }
3895
3896 /**
3897 * megasas_io_attach - Attaches this driver to SCSI mid-layer
3898 * @instance: Adapter soft state
3899 */
3900 static int megasas_io_attach(struct megasas_instance *instance)
3901 {
3902 struct Scsi_Host *host = instance->host;
3903
3904 /*
3905 * Export parameters required by SCSI mid-layer
3906 */
3907 host->irq = instance->pdev->irq;
3908 host->unique_id = instance->unique_id;
3909 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3910 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3911 host->can_queue =
3912 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
3913 } else
3914 host->can_queue =
3915 instance->max_fw_cmds - MEGASAS_INT_CMDS;
3916 host->this_id = instance->init_id;
3917 host->sg_tablesize = instance->max_num_sge;
3918
3919 if (instance->fw_support_ieee)
3920 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
3921
3922 /*
3923 * Check if the module parameter value for max_sectors can be used
3924 */
3925 if (max_sectors && max_sectors < instance->max_sectors_per_req)
3926 instance->max_sectors_per_req = max_sectors;
3927 else {
3928 if (max_sectors) {
3929 if (((instance->pdev->device ==
3930 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3931 (instance->pdev->device ==
3932 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
3933 (max_sectors <= MEGASAS_MAX_SECTORS)) {
3934 instance->max_sectors_per_req = max_sectors;
3935 } else {
3936 printk(KERN_INFO "megasas: max_sectors should be > 0"
3937 "and <= %d (or < 1MB for GEN2 controller)\n",
3938 instance->max_sectors_per_req);
3939 }
3940 }
3941 }
3942
3943 host->max_sectors = instance->max_sectors_per_req;
3944 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
3945 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
3946 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
3947 host->max_lun = MEGASAS_MAX_LUN;
3948 host->max_cmd_len = 16;
3949
3950 /* Fusion only supports host reset */
3951 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
3952 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
3953 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
3954 host->hostt->eh_device_reset_handler = NULL;
3955 host->hostt->eh_bus_reset_handler = NULL;
3956 }
3957
3958 /*
3959 * Notify the mid-layer about the new controller
3960 */
3961 if (scsi_add_host(host, &instance->pdev->dev)) {
3962 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
3963 return -ENODEV;
3964 }
3965
3966 /*
3967 * Trigger SCSI to scan our drives
3968 */
3969 scsi_scan_host(host);
3970 return 0;
3971 }
3972
3973 static int
3974 megasas_set_dma_mask(struct pci_dev *pdev)
3975 {
3976 /*
3977 * All our contollers are capable of performing 64-bit DMA
3978 */
3979 if (IS_DMA64) {
3980 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
3981
3982 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
3983 goto fail_set_dma_mask;
3984 }
3985 } else {
3986 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
3987 goto fail_set_dma_mask;
3988 }
3989 return 0;
3990
3991 fail_set_dma_mask:
3992 return 1;
3993 }
3994
3995 /**
3996 * megasas_probe_one - PCI hotplug entry point
3997 * @pdev: PCI device structure
3998 * @id: PCI ids of supported hotplugged adapter
3999 */
4000 static int megasas_probe_one(struct pci_dev *pdev,
4001 const struct pci_device_id *id)
4002 {
4003 int rval, pos, i, j;
4004 struct Scsi_Host *host;
4005 struct megasas_instance *instance;
4006 u16 control = 0;
4007
4008 /* Reset MSI-X in the kdump kernel */
4009 if (reset_devices) {
4010 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
4011 if (pos) {
4012 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
4013 &control);
4014 if (control & PCI_MSIX_FLAGS_ENABLE) {
4015 dev_info(&pdev->dev, "resetting MSI-X\n");
4016 pci_write_config_word(pdev,
4017 pos + PCI_MSIX_FLAGS,
4018 control &
4019 ~PCI_MSIX_FLAGS_ENABLE);
4020 }
4021 }
4022 }
4023
4024 /*
4025 * Announce PCI information
4026 */
4027 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
4028 pdev->vendor, pdev->device, pdev->subsystem_vendor,
4029 pdev->subsystem_device);
4030
4031 printk("bus %d:slot %d:func %d\n",
4032 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
4033
4034 /*
4035 * PCI prepping: enable device set bus mastering and dma mask
4036 */
4037 rval = pci_enable_device_mem(pdev);
4038
4039 if (rval) {
4040 return rval;
4041 }
4042
4043 pci_set_master(pdev);
4044
4045 if (megasas_set_dma_mask(pdev))
4046 goto fail_set_dma_mask;
4047
4048 host = scsi_host_alloc(&megasas_template,
4049 sizeof(struct megasas_instance));
4050
4051 if (!host) {
4052 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
4053 goto fail_alloc_instance;
4054 }
4055
4056 instance = (struct megasas_instance *)host->hostdata;
4057 memset(instance, 0, sizeof(*instance));
4058 atomic_set( &instance->fw_reset_no_pci_access, 0 );
4059 instance->pdev = pdev;
4060
4061 switch (instance->pdev->device) {
4062 case PCI_DEVICE_ID_LSI_FUSION:
4063 case PCI_DEVICE_ID_LSI_INVADER:
4064 case PCI_DEVICE_ID_LSI_FURY:
4065 {
4066 struct fusion_context *fusion;
4067
4068 instance->ctrl_context =
4069 kzalloc(sizeof(struct fusion_context), GFP_KERNEL);
4070 if (!instance->ctrl_context) {
4071 printk(KERN_DEBUG "megasas: Failed to allocate "
4072 "memory for Fusion context info\n");
4073 goto fail_alloc_dma_buf;
4074 }
4075 fusion = instance->ctrl_context;
4076 INIT_LIST_HEAD(&fusion->cmd_pool);
4077 spin_lock_init(&fusion->cmd_pool_lock);
4078 }
4079 break;
4080 default: /* For all other supported controllers */
4081
4082 instance->producer =
4083 pci_alloc_consistent(pdev, sizeof(u32),
4084 &instance->producer_h);
4085 instance->consumer =
4086 pci_alloc_consistent(pdev, sizeof(u32),
4087 &instance->consumer_h);
4088
4089 if (!instance->producer || !instance->consumer) {
4090 printk(KERN_DEBUG "megasas: Failed to allocate"
4091 "memory for producer, consumer\n");
4092 goto fail_alloc_dma_buf;
4093 }
4094
4095 *instance->producer = 0;
4096 *instance->consumer = 0;
4097 break;
4098 }
4099
4100 megasas_poll_wait_aen = 0;
4101 instance->flag_ieee = 0;
4102 instance->ev = NULL;
4103 instance->issuepend_done = 1;
4104 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
4105 megasas_poll_wait_aen = 0;
4106
4107 instance->evt_detail = pci_alloc_consistent(pdev,
4108 sizeof(struct
4109 megasas_evt_detail),
4110 &instance->evt_detail_h);
4111
4112 if (!instance->evt_detail) {
4113 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
4114 "event detail structure\n");
4115 goto fail_alloc_dma_buf;
4116 }
4117
4118 /*
4119 * Initialize locks and queues
4120 */
4121 INIT_LIST_HEAD(&instance->cmd_pool);
4122 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
4123
4124 atomic_set(&instance->fw_outstanding,0);
4125
4126 init_waitqueue_head(&instance->int_cmd_wait_q);
4127 init_waitqueue_head(&instance->abort_cmd_wait_q);
4128
4129 spin_lock_init(&instance->cmd_pool_lock);
4130 spin_lock_init(&instance->hba_lock);
4131 spin_lock_init(&instance->completion_lock);
4132
4133 mutex_init(&instance->aen_mutex);
4134 mutex_init(&instance->reset_mutex);
4135
4136 /*
4137 * Initialize PCI related and misc parameters
4138 */
4139 instance->host = host;
4140 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
4141 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
4142
4143 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4144 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4145 instance->flag_ieee = 1;
4146 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4147 } else
4148 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
4149
4150 megasas_dbg_lvl = 0;
4151 instance->flag = 0;
4152 instance->unload = 1;
4153 instance->last_time = 0;
4154 instance->disableOnlineCtrlReset = 1;
4155
4156 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4157 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
4158 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
4159 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
4160 else
4161 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
4162
4163 /*
4164 * Initialize MFI Firmware
4165 */
4166 if (megasas_init_fw(instance))
4167 goto fail_init_mfi;
4168
4169 /*
4170 * Register IRQ
4171 */
4172 if (instance->msix_vectors) {
4173 for (i = 0 ; i < instance->msix_vectors; i++) {
4174 instance->irq_context[i].instance = instance;
4175 instance->irq_context[i].MSIxIndex = i;
4176 if (request_irq(instance->msixentry[i].vector,
4177 instance->instancet->service_isr, 0,
4178 "megasas",
4179 &instance->irq_context[i])) {
4180 printk(KERN_DEBUG "megasas: Failed to "
4181 "register IRQ for vector %d.\n", i);
4182 for (j = 0 ; j < i ; j++)
4183 free_irq(
4184 instance->msixentry[j].vector,
4185 &instance->irq_context[j]);
4186 goto fail_irq;
4187 }
4188 }
4189 } else {
4190 instance->irq_context[0].instance = instance;
4191 instance->irq_context[0].MSIxIndex = 0;
4192 if (request_irq(pdev->irq, instance->instancet->service_isr,
4193 IRQF_SHARED, "megasas",
4194 &instance->irq_context[0])) {
4195 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4196 goto fail_irq;
4197 }
4198 }
4199
4200 instance->instancet->enable_intr(instance->reg_set);
4201
4202 /*
4203 * Store instance in PCI softstate
4204 */
4205 pci_set_drvdata(pdev, instance);
4206
4207 /*
4208 * Add this controller to megasas_mgmt_info structure so that it
4209 * can be exported to management applications
4210 */
4211 megasas_mgmt_info.count++;
4212 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
4213 megasas_mgmt_info.max_index++;
4214
4215 /*
4216 * Register with SCSI mid-layer
4217 */
4218 if (megasas_io_attach(instance))
4219 goto fail_io_attach;
4220
4221 instance->unload = 0;
4222
4223 /*
4224 * Initiate AEN (Asynchronous Event Notification)
4225 */
4226 if (megasas_start_aen(instance)) {
4227 printk(KERN_DEBUG "megasas: start aen failed\n");
4228 goto fail_start_aen;
4229 }
4230
4231 return 0;
4232
4233 fail_start_aen:
4234 fail_io_attach:
4235 megasas_mgmt_info.count--;
4236 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
4237 megasas_mgmt_info.max_index--;
4238
4239 pci_set_drvdata(pdev, NULL);
4240 instance->instancet->disable_intr(instance->reg_set);
4241 if (instance->msix_vectors)
4242 for (i = 0 ; i < instance->msix_vectors; i++)
4243 free_irq(instance->msixentry[i].vector,
4244 &instance->irq_context[i]);
4245 else
4246 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4247 fail_irq:
4248 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4249 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
4250 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
4251 megasas_release_fusion(instance);
4252 else
4253 megasas_release_mfi(instance);
4254 fail_init_mfi:
4255 if (instance->msix_vectors)
4256 pci_disable_msix(instance->pdev);
4257 fail_alloc_dma_buf:
4258 if (instance->evt_detail)
4259 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4260 instance->evt_detail,
4261 instance->evt_detail_h);
4262
4263 if (instance->producer)
4264 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4265 instance->producer_h);
4266 if (instance->consumer)
4267 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4268 instance->consumer_h);
4269 scsi_host_put(host);
4270
4271 fail_alloc_instance:
4272 fail_set_dma_mask:
4273 pci_disable_device(pdev);
4274
4275 return -ENODEV;
4276 }
4277
4278 /**
4279 * megasas_flush_cache - Requests FW to flush all its caches
4280 * @instance: Adapter soft state
4281 */
4282 static void megasas_flush_cache(struct megasas_instance *instance)
4283 {
4284 struct megasas_cmd *cmd;
4285 struct megasas_dcmd_frame *dcmd;
4286
4287 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4288 return;
4289
4290 cmd = megasas_get_cmd(instance);
4291
4292 if (!cmd)
4293 return;
4294
4295 dcmd = &cmd->frame->dcmd;
4296
4297 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4298
4299 dcmd->cmd = MFI_CMD_DCMD;
4300 dcmd->cmd_status = 0x0;
4301 dcmd->sge_count = 0;
4302 dcmd->flags = MFI_FRAME_DIR_NONE;
4303 dcmd->timeout = 0;
4304 dcmd->pad_0 = 0;
4305 dcmd->data_xfer_len = 0;
4306 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
4307 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
4308
4309 megasas_issue_blocked_cmd(instance, cmd);
4310
4311 megasas_return_cmd(instance, cmd);
4312
4313 return;
4314 }
4315
4316 /**
4317 * megasas_shutdown_controller - Instructs FW to shutdown the controller
4318 * @instance: Adapter soft state
4319 * @opcode: Shutdown/Hibernate
4320 */
4321 static void megasas_shutdown_controller(struct megasas_instance *instance,
4322 u32 opcode)
4323 {
4324 struct megasas_cmd *cmd;
4325 struct megasas_dcmd_frame *dcmd;
4326
4327 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4328 return;
4329
4330 cmd = megasas_get_cmd(instance);
4331
4332 if (!cmd)
4333 return;
4334
4335 if (instance->aen_cmd)
4336 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
4337 if (instance->map_update_cmd)
4338 megasas_issue_blocked_abort_cmd(instance,
4339 instance->map_update_cmd);
4340 dcmd = &cmd->frame->dcmd;
4341
4342 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4343
4344 dcmd->cmd = MFI_CMD_DCMD;
4345 dcmd->cmd_status = 0x0;
4346 dcmd->sge_count = 0;
4347 dcmd->flags = MFI_FRAME_DIR_NONE;
4348 dcmd->timeout = 0;
4349 dcmd->pad_0 = 0;
4350 dcmd->data_xfer_len = 0;
4351 dcmd->opcode = opcode;
4352
4353 megasas_issue_blocked_cmd(instance, cmd);
4354
4355 megasas_return_cmd(instance, cmd);
4356
4357 return;
4358 }
4359
4360 #ifdef CONFIG_PM
4361 /**
4362 * megasas_suspend - driver suspend entry point
4363 * @pdev: PCI device structure
4364 * @state: PCI power state to suspend routine
4365 */
4366 static int
4367 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
4368 {
4369 struct Scsi_Host *host;
4370 struct megasas_instance *instance;
4371 int i;
4372
4373 instance = pci_get_drvdata(pdev);
4374 host = instance->host;
4375 instance->unload = 1;
4376
4377 megasas_flush_cache(instance);
4378 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
4379
4380 /* cancel the delayed work if this work still in queue */
4381 if (instance->ev != NULL) {
4382 struct megasas_aen_event *ev = instance->ev;
4383 cancel_delayed_work_sync(&ev->hotplug_work);
4384 instance->ev = NULL;
4385 }
4386
4387 tasklet_kill(&instance->isr_tasklet);
4388
4389 pci_set_drvdata(instance->pdev, instance);
4390 instance->instancet->disable_intr(instance->reg_set);
4391
4392 if (instance->msix_vectors)
4393 for (i = 0 ; i < instance->msix_vectors; i++)
4394 free_irq(instance->msixentry[i].vector,
4395 &instance->irq_context[i]);
4396 else
4397 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4398 if (instance->msix_vectors)
4399 pci_disable_msix(instance->pdev);
4400
4401 pci_save_state(pdev);
4402 pci_disable_device(pdev);
4403
4404 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4405
4406 return 0;
4407 }
4408
4409 /**
4410 * megasas_resume- driver resume entry point
4411 * @pdev: PCI device structure
4412 */
4413 static int
4414 megasas_resume(struct pci_dev *pdev)
4415 {
4416 int rval, i, j;
4417 struct Scsi_Host *host;
4418 struct megasas_instance *instance;
4419
4420 instance = pci_get_drvdata(pdev);
4421 host = instance->host;
4422 pci_set_power_state(pdev, PCI_D0);
4423 pci_enable_wake(pdev, PCI_D0, 0);
4424 pci_restore_state(pdev);
4425
4426 /*
4427 * PCI prepping: enable device set bus mastering and dma mask
4428 */
4429 rval = pci_enable_device_mem(pdev);
4430
4431 if (rval) {
4432 printk(KERN_ERR "megasas: Enable device failed\n");
4433 return rval;
4434 }
4435
4436 pci_set_master(pdev);
4437
4438 if (megasas_set_dma_mask(pdev))
4439 goto fail_set_dma_mask;
4440
4441 /*
4442 * Initialize MFI Firmware
4443 */
4444
4445 atomic_set(&instance->fw_outstanding, 0);
4446
4447 /*
4448 * We expect the FW state to be READY
4449 */
4450 if (megasas_transition_to_ready(instance, 0))
4451 goto fail_ready_state;
4452
4453 /* Now re-enable MSI-X */
4454 if (instance->msix_vectors)
4455 pci_enable_msix(instance->pdev, instance->msixentry,
4456 instance->msix_vectors);
4457
4458 switch (instance->pdev->device) {
4459 case PCI_DEVICE_ID_LSI_FUSION:
4460 case PCI_DEVICE_ID_LSI_INVADER:
4461 case PCI_DEVICE_ID_LSI_FURY:
4462 {
4463 megasas_reset_reply_desc(instance);
4464 if (megasas_ioc_init_fusion(instance)) {
4465 megasas_free_cmds(instance);
4466 megasas_free_cmds_fusion(instance);
4467 goto fail_init_mfi;
4468 }
4469 if (!megasas_get_map_info(instance))
4470 megasas_sync_map_info(instance);
4471 }
4472 break;
4473 default:
4474 *instance->producer = 0;
4475 *instance->consumer = 0;
4476 if (megasas_issue_init_mfi(instance))
4477 goto fail_init_mfi;
4478 break;
4479 }
4480
4481 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4482 (unsigned long)instance);
4483
4484 /*
4485 * Register IRQ
4486 */
4487 if (instance->msix_vectors) {
4488 for (i = 0 ; i < instance->msix_vectors; i++) {
4489 instance->irq_context[i].instance = instance;
4490 instance->irq_context[i].MSIxIndex = i;
4491 if (request_irq(instance->msixentry[i].vector,
4492 instance->instancet->service_isr, 0,
4493 "megasas",
4494 &instance->irq_context[i])) {
4495 printk(KERN_DEBUG "megasas: Failed to "
4496 "register IRQ for vector %d.\n", i);
4497 for (j = 0 ; j < i ; j++)
4498 free_irq(
4499 instance->msixentry[j].vector,
4500 &instance->irq_context[j]);
4501 goto fail_irq;
4502 }
4503 }
4504 } else {
4505 instance->irq_context[0].instance = instance;
4506 instance->irq_context[0].MSIxIndex = 0;
4507 if (request_irq(pdev->irq, instance->instancet->service_isr,
4508 IRQF_SHARED, "megasas",
4509 &instance->irq_context[0])) {
4510 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4511 goto fail_irq;
4512 }
4513 }
4514
4515 instance->instancet->enable_intr(instance->reg_set);
4516 instance->unload = 0;
4517
4518 /*
4519 * Initiate AEN (Asynchronous Event Notification)
4520 */
4521 if (megasas_start_aen(instance))
4522 printk(KERN_ERR "megasas: Start AEN failed\n");
4523
4524 return 0;
4525
4526 fail_irq:
4527 fail_init_mfi:
4528 if (instance->evt_detail)
4529 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4530 instance->evt_detail,
4531 instance->evt_detail_h);
4532
4533 if (instance->producer)
4534 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4535 instance->producer_h);
4536 if (instance->consumer)
4537 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4538 instance->consumer_h);
4539 scsi_host_put(host);
4540
4541 fail_set_dma_mask:
4542 fail_ready_state:
4543
4544 pci_disable_device(pdev);
4545
4546 return -ENODEV;
4547 }
4548 #else
4549 #define megasas_suspend NULL
4550 #define megasas_resume NULL
4551 #endif
4552
4553 /**
4554 * megasas_detach_one - PCI hot"un"plug entry point
4555 * @pdev: PCI device structure
4556 */
4557 static void megasas_detach_one(struct pci_dev *pdev)
4558 {
4559 int i;
4560 struct Scsi_Host *host;
4561 struct megasas_instance *instance;
4562 struct fusion_context *fusion;
4563
4564 instance = pci_get_drvdata(pdev);
4565 instance->unload = 1;
4566 host = instance->host;
4567 fusion = instance->ctrl_context;
4568
4569 scsi_remove_host(instance->host);
4570 megasas_flush_cache(instance);
4571 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
4572
4573 /* cancel the delayed work if this work still in queue*/
4574 if (instance->ev != NULL) {
4575 struct megasas_aen_event *ev = instance->ev;
4576 cancel_delayed_work_sync(&ev->hotplug_work);
4577 instance->ev = NULL;
4578 }
4579
4580 tasklet_kill(&instance->isr_tasklet);
4581
4582 /*
4583 * Take the instance off the instance array. Note that we will not
4584 * decrement the max_index. We let this array be sparse array
4585 */
4586 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4587 if (megasas_mgmt_info.instance[i] == instance) {
4588 megasas_mgmt_info.count--;
4589 megasas_mgmt_info.instance[i] = NULL;
4590
4591 break;
4592 }
4593 }
4594
4595 pci_set_drvdata(instance->pdev, NULL);
4596
4597 instance->instancet->disable_intr(instance->reg_set);
4598
4599 if (instance->msix_vectors)
4600 for (i = 0 ; i < instance->msix_vectors; i++)
4601 free_irq(instance->msixentry[i].vector,
4602 &instance->irq_context[i]);
4603 else
4604 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4605 if (instance->msix_vectors)
4606 pci_disable_msix(instance->pdev);
4607
4608 switch (instance->pdev->device) {
4609 case PCI_DEVICE_ID_LSI_FUSION:
4610 case PCI_DEVICE_ID_LSI_INVADER:
4611 case PCI_DEVICE_ID_LSI_FURY:
4612 megasas_release_fusion(instance);
4613 for (i = 0; i < 2 ; i++)
4614 if (fusion->ld_map[i])
4615 dma_free_coherent(&instance->pdev->dev,
4616 fusion->map_sz,
4617 fusion->ld_map[i],
4618 fusion->
4619 ld_map_phys[i]);
4620 kfree(instance->ctrl_context);
4621 break;
4622 default:
4623 megasas_release_mfi(instance);
4624 pci_free_consistent(pdev, sizeof(u32),
4625 instance->producer,
4626 instance->producer_h);
4627 pci_free_consistent(pdev, sizeof(u32),
4628 instance->consumer,
4629 instance->consumer_h);
4630 break;
4631 }
4632
4633 if (instance->evt_detail)
4634 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4635 instance->evt_detail, instance->evt_detail_h);
4636 scsi_host_put(host);
4637
4638 pci_set_drvdata(pdev, NULL);
4639
4640 pci_disable_device(pdev);
4641
4642 return;
4643 }
4644
4645 /**
4646 * megasas_shutdown - Shutdown entry point
4647 * @device: Generic device structure
4648 */
4649 static void megasas_shutdown(struct pci_dev *pdev)
4650 {
4651 int i;
4652 struct megasas_instance *instance = pci_get_drvdata(pdev);
4653
4654 instance->unload = 1;
4655 megasas_flush_cache(instance);
4656 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
4657 instance->instancet->disable_intr(instance->reg_set);
4658 if (instance->msix_vectors)
4659 for (i = 0 ; i < instance->msix_vectors; i++)
4660 free_irq(instance->msixentry[i].vector,
4661 &instance->irq_context[i]);
4662 else
4663 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4664 if (instance->msix_vectors)
4665 pci_disable_msix(instance->pdev);
4666 }
4667
4668 /**
4669 * megasas_mgmt_open - char node "open" entry point
4670 */
4671 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
4672 {
4673 /*
4674 * Allow only those users with admin rights
4675 */
4676 if (!capable(CAP_SYS_ADMIN))
4677 return -EACCES;
4678
4679 return 0;
4680 }
4681
4682 /**
4683 * megasas_mgmt_fasync - Async notifier registration from applications
4684 *
4685 * This function adds the calling process to a driver global queue. When an
4686 * event occurs, SIGIO will be sent to all processes in this queue.
4687 */
4688 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
4689 {
4690 int rc;
4691
4692 mutex_lock(&megasas_async_queue_mutex);
4693
4694 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
4695
4696 mutex_unlock(&megasas_async_queue_mutex);
4697
4698 if (rc >= 0) {
4699 /* For sanity check when we get ioctl */
4700 filep->private_data = filep;
4701 return 0;
4702 }
4703
4704 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
4705
4706 return rc;
4707 }
4708
4709 /**
4710 * megasas_mgmt_poll - char node "poll" entry point
4711 * */
4712 static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
4713 {
4714 unsigned int mask;
4715 unsigned long flags;
4716 poll_wait(file, &megasas_poll_wait, wait);
4717 spin_lock_irqsave(&poll_aen_lock, flags);
4718 if (megasas_poll_wait_aen)
4719 mask = (POLLIN | POLLRDNORM);
4720 else
4721 mask = 0;
4722 spin_unlock_irqrestore(&poll_aen_lock, flags);
4723 return mask;
4724 }
4725
4726 /**
4727 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
4728 * @instance: Adapter soft state
4729 * @argp: User's ioctl packet
4730 */
4731 static int
4732 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
4733 struct megasas_iocpacket __user * user_ioc,
4734 struct megasas_iocpacket *ioc)
4735 {
4736 struct megasas_sge32 *kern_sge32;
4737 struct megasas_cmd *cmd;
4738 void *kbuff_arr[MAX_IOCTL_SGE];
4739 dma_addr_t buf_handle = 0;
4740 int error = 0, i;
4741 void *sense = NULL;
4742 dma_addr_t sense_handle;
4743 unsigned long *sense_ptr;
4744
4745 memset(kbuff_arr, 0, sizeof(kbuff_arr));
4746
4747 if (ioc->sge_count > MAX_IOCTL_SGE) {
4748 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
4749 ioc->sge_count, MAX_IOCTL_SGE);
4750 return -EINVAL;
4751 }
4752
4753 cmd = megasas_get_cmd(instance);
4754 if (!cmd) {
4755 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
4756 return -ENOMEM;
4757 }
4758
4759 /*
4760 * User's IOCTL packet has 2 frames (maximum). Copy those two
4761 * frames into our cmd's frames. cmd->frame's context will get
4762 * overwritten when we copy from user's frames. So set that value
4763 * alone separately
4764 */
4765 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
4766 cmd->frame->hdr.context = cmd->index;
4767 cmd->frame->hdr.pad_0 = 0;
4768 cmd->frame->hdr.flags &= ~(MFI_FRAME_IEEE | MFI_FRAME_SGL64 |
4769 MFI_FRAME_SENSE64);
4770
4771 /*
4772 * The management interface between applications and the fw uses
4773 * MFI frames. E.g, RAID configuration changes, LD property changes
4774 * etc are accomplishes through different kinds of MFI frames. The
4775 * driver needs to care only about substituting user buffers with
4776 * kernel buffers in SGLs. The location of SGL is embedded in the
4777 * struct iocpacket itself.
4778 */
4779 kern_sge32 = (struct megasas_sge32 *)
4780 ((unsigned long)cmd->frame + ioc->sgl_off);
4781
4782 /*
4783 * For each user buffer, create a mirror buffer and copy in
4784 */
4785 for (i = 0; i < ioc->sge_count; i++) {
4786 if (!ioc->sgl[i].iov_len)
4787 continue;
4788
4789 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
4790 ioc->sgl[i].iov_len,
4791 &buf_handle, GFP_KERNEL);
4792 if (!kbuff_arr[i]) {
4793 printk(KERN_DEBUG "megasas: Failed to alloc "
4794 "kernel SGL buffer for IOCTL \n");
4795 error = -ENOMEM;
4796 goto out;
4797 }
4798
4799 /*
4800 * We don't change the dma_coherent_mask, so
4801 * pci_alloc_consistent only returns 32bit addresses
4802 */
4803 kern_sge32[i].phys_addr = (u32) buf_handle;
4804 kern_sge32[i].length = ioc->sgl[i].iov_len;
4805
4806 /*
4807 * We created a kernel buffer corresponding to the
4808 * user buffer. Now copy in from the user buffer
4809 */
4810 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
4811 (u32) (ioc->sgl[i].iov_len))) {
4812 error = -EFAULT;
4813 goto out;
4814 }
4815 }
4816
4817 if (ioc->sense_len) {
4818 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
4819 &sense_handle, GFP_KERNEL);
4820 if (!sense) {
4821 error = -ENOMEM;
4822 goto out;
4823 }
4824
4825 sense_ptr =
4826 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
4827 *sense_ptr = sense_handle;
4828 }
4829
4830 /*
4831 * Set the sync_cmd flag so that the ISR knows not to complete this
4832 * cmd to the SCSI mid-layer
4833 */
4834 cmd->sync_cmd = 1;
4835 megasas_issue_blocked_cmd(instance, cmd);
4836 cmd->sync_cmd = 0;
4837
4838 /*
4839 * copy out the kernel buffers to user buffers
4840 */
4841 for (i = 0; i < ioc->sge_count; i++) {
4842 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
4843 ioc->sgl[i].iov_len)) {
4844 error = -EFAULT;
4845 goto out;
4846 }
4847 }
4848
4849 /*
4850 * copy out the sense
4851 */
4852 if (ioc->sense_len) {
4853 /*
4854 * sense_ptr points to the location that has the user
4855 * sense buffer address
4856 */
4857 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
4858 ioc->sense_off);
4859
4860 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
4861 sense, ioc->sense_len)) {
4862 printk(KERN_ERR "megasas: Failed to copy out to user "
4863 "sense data\n");
4864 error = -EFAULT;
4865 goto out;
4866 }
4867 }
4868
4869 /*
4870 * copy the status codes returned by the fw
4871 */
4872 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
4873 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
4874 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
4875 error = -EFAULT;
4876 }
4877
4878 out:
4879 if (sense) {
4880 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
4881 sense, sense_handle);
4882 }
4883
4884 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
4885 dma_free_coherent(&instance->pdev->dev,
4886 kern_sge32[i].length,
4887 kbuff_arr[i], kern_sge32[i].phys_addr);
4888 }
4889
4890 megasas_return_cmd(instance, cmd);
4891 return error;
4892 }
4893
4894 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
4895 {
4896 struct megasas_iocpacket __user *user_ioc =
4897 (struct megasas_iocpacket __user *)arg;
4898 struct megasas_iocpacket *ioc;
4899 struct megasas_instance *instance;
4900 int error;
4901 int i;
4902 unsigned long flags;
4903 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
4904
4905 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
4906 if (!ioc)
4907 return -ENOMEM;
4908
4909 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
4910 error = -EFAULT;
4911 goto out_kfree_ioc;
4912 }
4913
4914 instance = megasas_lookup_instance(ioc->host_no);
4915 if (!instance) {
4916 error = -ENODEV;
4917 goto out_kfree_ioc;
4918 }
4919
4920 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4921 printk(KERN_ERR "Controller in crit error\n");
4922 error = -ENODEV;
4923 goto out_kfree_ioc;
4924 }
4925
4926 if (instance->unload == 1) {
4927 error = -ENODEV;
4928 goto out_kfree_ioc;
4929 }
4930
4931 /*
4932 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
4933 */
4934 if (down_interruptible(&instance->ioctl_sem)) {
4935 error = -ERESTARTSYS;
4936 goto out_kfree_ioc;
4937 }
4938
4939 for (i = 0; i < wait_time; i++) {
4940
4941 spin_lock_irqsave(&instance->hba_lock, flags);
4942 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4943 spin_unlock_irqrestore(&instance->hba_lock, flags);
4944 break;
4945 }
4946 spin_unlock_irqrestore(&instance->hba_lock, flags);
4947
4948 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4949 printk(KERN_NOTICE "megasas: waiting"
4950 "for controller reset to finish\n");
4951 }
4952
4953 msleep(1000);
4954 }
4955
4956 spin_lock_irqsave(&instance->hba_lock, flags);
4957 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4958 spin_unlock_irqrestore(&instance->hba_lock, flags);
4959
4960 printk(KERN_ERR "megaraid_sas: timed out while"
4961 "waiting for HBA to recover\n");
4962 error = -ENODEV;
4963 goto out_up;
4964 }
4965 spin_unlock_irqrestore(&instance->hba_lock, flags);
4966
4967 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
4968 out_up:
4969 up(&instance->ioctl_sem);
4970
4971 out_kfree_ioc:
4972 kfree(ioc);
4973 return error;
4974 }
4975
4976 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
4977 {
4978 struct megasas_instance *instance;
4979 struct megasas_aen aen;
4980 int error;
4981 int i;
4982 unsigned long flags;
4983 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
4984
4985 if (file->private_data != file) {
4986 printk(KERN_DEBUG "megasas: fasync_helper was not "
4987 "called first\n");
4988 return -EINVAL;
4989 }
4990
4991 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
4992 return -EFAULT;
4993
4994 instance = megasas_lookup_instance(aen.host_no);
4995
4996 if (!instance)
4997 return -ENODEV;
4998
4999 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
5000 return -ENODEV;
5001 }
5002
5003 if (instance->unload == 1) {
5004 return -ENODEV;
5005 }
5006
5007 for (i = 0; i < wait_time; i++) {
5008
5009 spin_lock_irqsave(&instance->hba_lock, flags);
5010 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
5011 spin_unlock_irqrestore(&instance->hba_lock,
5012 flags);
5013 break;
5014 }
5015
5016 spin_unlock_irqrestore(&instance->hba_lock, flags);
5017
5018 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
5019 printk(KERN_NOTICE "megasas: waiting for"
5020 "controller reset to finish\n");
5021 }
5022
5023 msleep(1000);
5024 }
5025
5026 spin_lock_irqsave(&instance->hba_lock, flags);
5027 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
5028 spin_unlock_irqrestore(&instance->hba_lock, flags);
5029 printk(KERN_ERR "megaraid_sas: timed out while waiting"
5030 "for HBA to recover.\n");
5031 return -ENODEV;
5032 }
5033 spin_unlock_irqrestore(&instance->hba_lock, flags);
5034
5035 mutex_lock(&instance->aen_mutex);
5036 error = megasas_register_aen(instance, aen.seq_num,
5037 aen.class_locale_word);
5038 mutex_unlock(&instance->aen_mutex);
5039 return error;
5040 }
5041
5042 /**
5043 * megasas_mgmt_ioctl - char node ioctl entry point
5044 */
5045 static long
5046 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5047 {
5048 switch (cmd) {
5049 case MEGASAS_IOC_FIRMWARE:
5050 return megasas_mgmt_ioctl_fw(file, arg);
5051
5052 case MEGASAS_IOC_GET_AEN:
5053 return megasas_mgmt_ioctl_aen(file, arg);
5054 }
5055
5056 return -ENOTTY;
5057 }
5058
5059 #ifdef CONFIG_COMPAT
5060 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
5061 {
5062 struct compat_megasas_iocpacket __user *cioc =
5063 (struct compat_megasas_iocpacket __user *)arg;
5064 struct megasas_iocpacket __user *ioc =
5065 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
5066 int i;
5067 int error = 0;
5068 compat_uptr_t ptr;
5069
5070 if (clear_user(ioc, sizeof(*ioc)))
5071 return -EFAULT;
5072
5073 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
5074 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
5075 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
5076 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
5077 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
5078 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
5079 return -EFAULT;
5080
5081 /*
5082 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
5083 * sense_len is not null, so prepare the 64bit value under
5084 * the same condition.
5085 */
5086 if (ioc->sense_len) {
5087 void __user **sense_ioc_ptr =
5088 (void __user **)(ioc->frame.raw + ioc->sense_off);
5089 compat_uptr_t *sense_cioc_ptr =
5090 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
5091 if (get_user(ptr, sense_cioc_ptr) ||
5092 put_user(compat_ptr(ptr), sense_ioc_ptr))
5093 return -EFAULT;
5094 }
5095
5096 for (i = 0; i < MAX_IOCTL_SGE; i++) {
5097 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
5098 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
5099 copy_in_user(&ioc->sgl[i].iov_len,
5100 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
5101 return -EFAULT;
5102 }
5103
5104 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
5105
5106 if (copy_in_user(&cioc->frame.hdr.cmd_status,
5107 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
5108 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
5109 return -EFAULT;
5110 }
5111 return error;
5112 }
5113
5114 static long
5115 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
5116 unsigned long arg)
5117 {
5118 switch (cmd) {
5119 case MEGASAS_IOC_FIRMWARE32:
5120 return megasas_mgmt_compat_ioctl_fw(file, arg);
5121 case MEGASAS_IOC_GET_AEN:
5122 return megasas_mgmt_ioctl_aen(file, arg);
5123 }
5124
5125 return -ENOTTY;
5126 }
5127 #endif
5128
5129 /*
5130 * File operations structure for management interface
5131 */
5132 static const struct file_operations megasas_mgmt_fops = {
5133 .owner = THIS_MODULE,
5134 .open = megasas_mgmt_open,
5135 .fasync = megasas_mgmt_fasync,
5136 .unlocked_ioctl = megasas_mgmt_ioctl,
5137 .poll = megasas_mgmt_poll,
5138 #ifdef CONFIG_COMPAT
5139 .compat_ioctl = megasas_mgmt_compat_ioctl,
5140 #endif
5141 .llseek = noop_llseek,
5142 };
5143
5144 /*
5145 * PCI hotplug support registration structure
5146 */
5147 static struct pci_driver megasas_pci_driver = {
5148
5149 .name = "megaraid_sas",
5150 .id_table = megasas_pci_table,
5151 .probe = megasas_probe_one,
5152 .remove = megasas_detach_one,
5153 .suspend = megasas_suspend,
5154 .resume = megasas_resume,
5155 .shutdown = megasas_shutdown,
5156 };
5157
5158 /*
5159 * Sysfs driver attributes
5160 */
5161 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
5162 {
5163 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
5164 MEGASAS_VERSION);
5165 }
5166
5167 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
5168
5169 static ssize_t
5170 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
5171 {
5172 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
5173 MEGASAS_RELDATE);
5174 }
5175
5176 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
5177 NULL);
5178
5179 static ssize_t
5180 megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
5181 {
5182 return sprintf(buf, "%u\n", support_poll_for_event);
5183 }
5184
5185 static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
5186 megasas_sysfs_show_support_poll_for_event, NULL);
5187
5188 static ssize_t
5189 megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
5190 {
5191 return sprintf(buf, "%u\n", support_device_change);
5192 }
5193
5194 static DRIVER_ATTR(support_device_change, S_IRUGO,
5195 megasas_sysfs_show_support_device_change, NULL);
5196
5197 static ssize_t
5198 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
5199 {
5200 return sprintf(buf, "%u\n", megasas_dbg_lvl);
5201 }
5202
5203 static ssize_t
5204 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
5205 {
5206 int retval = count;
5207 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
5208 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
5209 retval = -EINVAL;
5210 }
5211 return retval;
5212 }
5213
5214 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
5215 megasas_sysfs_set_dbg_lvl);
5216
5217 static void
5218 megasas_aen_polling(struct work_struct *work)
5219 {
5220 struct megasas_aen_event *ev =
5221 container_of(work, struct megasas_aen_event, hotplug_work.work);
5222 struct megasas_instance *instance = ev->instance;
5223 union megasas_evt_class_locale class_locale;
5224 struct Scsi_Host *host;
5225 struct scsi_device *sdev1;
5226 u16 pd_index = 0;
5227 u16 ld_index = 0;
5228 int i, j, doscan = 0;
5229 u32 seq_num;
5230 int error;
5231
5232 if (!instance) {
5233 printk(KERN_ERR "invalid instance!\n");
5234 kfree(ev);
5235 return;
5236 }
5237 instance->ev = NULL;
5238 host = instance->host;
5239 if (instance->evt_detail) {
5240
5241 switch (instance->evt_detail->code) {
5242 case MR_EVT_PD_INSERTED:
5243 if (megasas_get_pd_list(instance) == 0) {
5244 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5245 for (j = 0;
5246 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5247 j++) {
5248
5249 pd_index =
5250 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5251
5252 sdev1 =
5253 scsi_device_lookup(host, i, j, 0);
5254
5255 if (instance->pd_list[pd_index].driveState
5256 == MR_PD_STATE_SYSTEM) {
5257 if (!sdev1) {
5258 scsi_add_device(host, i, j, 0);
5259 }
5260
5261 if (sdev1)
5262 scsi_device_put(sdev1);
5263 }
5264 }
5265 }
5266 }
5267 doscan = 0;
5268 break;
5269
5270 case MR_EVT_PD_REMOVED:
5271 if (megasas_get_pd_list(instance) == 0) {
5272 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5273 for (j = 0;
5274 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5275 j++) {
5276
5277 pd_index =
5278 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5279
5280 sdev1 =
5281 scsi_device_lookup(host, i, j, 0);
5282
5283 if (instance->pd_list[pd_index].driveState
5284 == MR_PD_STATE_SYSTEM) {
5285 if (sdev1) {
5286 scsi_device_put(sdev1);
5287 }
5288 } else {
5289 if (sdev1) {
5290 scsi_remove_device(sdev1);
5291 scsi_device_put(sdev1);
5292 }
5293 }
5294 }
5295 }
5296 }
5297 doscan = 0;
5298 break;
5299
5300 case MR_EVT_LD_OFFLINE:
5301 case MR_EVT_CFG_CLEARED:
5302 case MR_EVT_LD_DELETED:
5303 megasas_get_ld_list(instance);
5304 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5305 for (j = 0;
5306 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5307 j++) {
5308
5309 ld_index =
5310 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5311
5312 sdev1 = scsi_device_lookup(host,
5313 i + MEGASAS_MAX_LD_CHANNELS,
5314 j,
5315 0);
5316
5317 if (instance->ld_ids[ld_index] != 0xff) {
5318 if (sdev1) {
5319 scsi_device_put(sdev1);
5320 }
5321 } else {
5322 if (sdev1) {
5323 scsi_remove_device(sdev1);
5324 scsi_device_put(sdev1);
5325 }
5326 }
5327 }
5328 }
5329 doscan = 0;
5330 break;
5331 case MR_EVT_LD_CREATED:
5332 megasas_get_ld_list(instance);
5333 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5334 for (j = 0;
5335 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5336 j++) {
5337 ld_index =
5338 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5339
5340 sdev1 = scsi_device_lookup(host,
5341 i+MEGASAS_MAX_LD_CHANNELS,
5342 j, 0);
5343
5344 if (instance->ld_ids[ld_index] !=
5345 0xff) {
5346 if (!sdev1) {
5347 scsi_add_device(host,
5348 i + 2,
5349 j, 0);
5350 }
5351 }
5352 if (sdev1) {
5353 scsi_device_put(sdev1);
5354 }
5355 }
5356 }
5357 doscan = 0;
5358 break;
5359 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
5360 case MR_EVT_FOREIGN_CFG_IMPORTED:
5361 case MR_EVT_LD_STATE_CHANGE:
5362 doscan = 1;
5363 break;
5364 default:
5365 doscan = 0;
5366 break;
5367 }
5368 } else {
5369 printk(KERN_ERR "invalid evt_detail!\n");
5370 kfree(ev);
5371 return;
5372 }
5373
5374 if (doscan) {
5375 printk(KERN_INFO "scanning ...\n");
5376 megasas_get_pd_list(instance);
5377 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5378 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5379 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
5380 sdev1 = scsi_device_lookup(host, i, j, 0);
5381 if (instance->pd_list[pd_index].driveState ==
5382 MR_PD_STATE_SYSTEM) {
5383 if (!sdev1) {
5384 scsi_add_device(host, i, j, 0);
5385 }
5386 if (sdev1)
5387 scsi_device_put(sdev1);
5388 } else {
5389 if (sdev1) {
5390 scsi_remove_device(sdev1);
5391 scsi_device_put(sdev1);
5392 }
5393 }
5394 }
5395 }
5396
5397 megasas_get_ld_list(instance);
5398 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5399 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5400 ld_index =
5401 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5402
5403 sdev1 = scsi_device_lookup(host,
5404 i+MEGASAS_MAX_LD_CHANNELS, j, 0);
5405 if (instance->ld_ids[ld_index] != 0xff) {
5406 if (!sdev1) {
5407 scsi_add_device(host,
5408 i+2,
5409 j, 0);
5410 } else {
5411 scsi_device_put(sdev1);
5412 }
5413 } else {
5414 if (sdev1) {
5415 scsi_remove_device(sdev1);
5416 scsi_device_put(sdev1);
5417 }
5418 }
5419 }
5420 }
5421 }
5422
5423 if ( instance->aen_cmd != NULL ) {
5424 kfree(ev);
5425 return ;
5426 }
5427
5428 seq_num = instance->evt_detail->seq_num + 1;
5429
5430 /* Register AEN with FW for latest sequence number plus 1 */
5431 class_locale.members.reserved = 0;
5432 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5433 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5434 mutex_lock(&instance->aen_mutex);
5435 error = megasas_register_aen(instance, seq_num,
5436 class_locale.word);
5437 mutex_unlock(&instance->aen_mutex);
5438
5439 if (error)
5440 printk(KERN_ERR "register aen failed error %x\n", error);
5441
5442 kfree(ev);
5443 }
5444
5445 /**
5446 * megasas_init - Driver load entry point
5447 */
5448 static int __init megasas_init(void)
5449 {
5450 int rval;
5451
5452 /*
5453 * Announce driver version and other information
5454 */
5455 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
5456 MEGASAS_EXT_VERSION);
5457
5458 spin_lock_init(&poll_aen_lock);
5459
5460 support_poll_for_event = 2;
5461 support_device_change = 1;
5462
5463 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
5464
5465 /*
5466 * Register character device node
5467 */
5468 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
5469
5470 if (rval < 0) {
5471 printk(KERN_DEBUG "megasas: failed to open device node\n");
5472 return rval;
5473 }
5474
5475 megasas_mgmt_majorno = rval;
5476
5477 /*
5478 * Register ourselves as PCI hotplug module
5479 */
5480 rval = pci_register_driver(&megasas_pci_driver);
5481
5482 if (rval) {
5483 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
5484 goto err_pcidrv;
5485 }
5486
5487 rval = driver_create_file(&megasas_pci_driver.driver,
5488 &driver_attr_version);
5489 if (rval)
5490 goto err_dcf_attr_ver;
5491 rval = driver_create_file(&megasas_pci_driver.driver,
5492 &driver_attr_release_date);
5493 if (rval)
5494 goto err_dcf_rel_date;
5495
5496 rval = driver_create_file(&megasas_pci_driver.driver,
5497 &driver_attr_support_poll_for_event);
5498 if (rval)
5499 goto err_dcf_support_poll_for_event;
5500
5501 rval = driver_create_file(&megasas_pci_driver.driver,
5502 &driver_attr_dbg_lvl);
5503 if (rval)
5504 goto err_dcf_dbg_lvl;
5505 rval = driver_create_file(&megasas_pci_driver.driver,
5506 &driver_attr_support_device_change);
5507 if (rval)
5508 goto err_dcf_support_device_change;
5509
5510 return rval;
5511
5512 err_dcf_support_device_change:
5513 driver_remove_file(&megasas_pci_driver.driver,
5514 &driver_attr_dbg_lvl);
5515 err_dcf_dbg_lvl:
5516 driver_remove_file(&megasas_pci_driver.driver,
5517 &driver_attr_support_poll_for_event);
5518
5519 err_dcf_support_poll_for_event:
5520 driver_remove_file(&megasas_pci_driver.driver,
5521 &driver_attr_release_date);
5522
5523 err_dcf_rel_date:
5524 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5525 err_dcf_attr_ver:
5526 pci_unregister_driver(&megasas_pci_driver);
5527 err_pcidrv:
5528 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5529 return rval;
5530 }
5531
5532 /**
5533 * megasas_exit - Driver unload entry point
5534 */
5535 static void __exit megasas_exit(void)
5536 {
5537 driver_remove_file(&megasas_pci_driver.driver,
5538 &driver_attr_dbg_lvl);
5539 driver_remove_file(&megasas_pci_driver.driver,
5540 &driver_attr_support_poll_for_event);
5541 driver_remove_file(&megasas_pci_driver.driver,
5542 &driver_attr_support_device_change);
5543 driver_remove_file(&megasas_pci_driver.driver,
5544 &driver_attr_release_date);
5545 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5546
5547 pci_unregister_driver(&megasas_pci_driver);
5548 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5549 }
5550
5551 module_init(megasas_init);
5552 module_exit(megasas_exit);
This page took 0.312334 seconds and 5 git commands to generate.