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