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