blob: 6fd1e1796029729c0583e141d155fa04acb389d0 [file] [log] [blame]
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001/*
2 *
3 * Linux MegaRAID driver for SAS based RAID controllers
4 *
bo yangf28cd7c2007-11-09 04:44:56 -05005 * Copyright (c) 2003-2005 LSI Corporation.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006 *
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
Yang, Bo6610a6b2008-08-10 12:42:38 -070013 * Version : v00.00.04.01-rc1
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040014 *
15 * Authors:
Sumant Patrocc5968c2007-02-14 13:05:42 -080016 * (email-id : megaraidlinux@lsi.com)
17 * Sreenivas Bagalkote
18 * Sumant Patro
19 * Bo Yang
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040020 *
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>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040031#include <linux/moduleparam.h>
32#include <linux/module.h>
33#include <linux/spinlock.h>
34#include <linux/interrupt.h>
35#include <linux/delay.h>
Jonathan Corbetf2b98572008-05-18 15:32:43 -060036#include <linux/smp_lock.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040037#include <linux/uio.h>
38#include <asm/uaccess.h>
Al Viro43399232005-10-04 17:36:04 +010039#include <linux/fs.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040040#include <linux/compat.h>
Sumant Patrocf62a0a2007-02-14 12:41:55 -080041#include <linux/blkdev.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010042#include <linux/mutex.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040043
44#include <scsi/scsi.h>
45#include <scsi/scsi_cmnd.h>
46#include <scsi/scsi_device.h>
47#include <scsi/scsi_host.h>
48#include "megaraid_sas.h"
49
bo yangad84db22007-11-09 04:40:16 -050050/*
51 * poll_mode_io:1- schedule complete completion from q cmd
52 */
53static unsigned int poll_mode_io;
54module_param_named(poll_mode_io, poll_mode_io, int, 0);
55MODULE_PARM_DESC(poll_mode_io,
56 "Complete cmds from IO path, (default=0)");
57
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040058MODULE_LICENSE("GPL");
59MODULE_VERSION(MEGASAS_VERSION);
Sumant Patro3d6d1742006-12-29 08:13:54 -080060MODULE_AUTHOR("megaraidlinux@lsi.com");
bo yangf28cd7c2007-11-09 04:44:56 -050061MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040062
63/*
64 * PCI ID table for all supported controllers
65 */
66static struct pci_device_id megasas_pci_table[] = {
67
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +020068 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
69 /* xscale IOP */
70 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
71 /* ppc IOP */
bo yangaf7a5642008-03-17 04:13:07 -040072 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
73 /* ppc IOP */
Yang, Bo6610a6b2008-08-10 12:42:38 -070074 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
75 /* gen2*/
76 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
77 /* gen2*/
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +020078 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
79 /* xscale IOP, vega */
80 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
81 /* xscale IOP */
82 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040083};
84
85MODULE_DEVICE_TABLE(pci, megasas_pci_table);
86
87static int megasas_mgmt_majorno;
88static struct megasas_mgmt_info megasas_mgmt_info;
89static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +010090static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040091
Sumant Patro658dced2006-10-03 13:09:14 -070092static u32 megasas_dbg_lvl;
93
bo yang7343eb62007-11-09 04:35:44 -050094static void
95megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
96 u8 alt_status);
97
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040098/**
99 * megasas_get_cmd - Get a command from the free pool
100 * @instance: Adapter soft state
101 *
102 * Returns a free command from the pool
103 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800104static struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400105 *instance)
106{
107 unsigned long flags;
108 struct megasas_cmd *cmd = NULL;
109
110 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
111
112 if (!list_empty(&instance->cmd_pool)) {
113 cmd = list_entry((&instance->cmd_pool)->next,
114 struct megasas_cmd, list);
115 list_del_init(&cmd->list);
116 } else {
117 printk(KERN_ERR "megasas: Command pool empty!\n");
118 }
119
120 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
121 return cmd;
122}
123
124/**
125 * megasas_return_cmd - Return a cmd to free command pool
126 * @instance: Adapter soft state
127 * @cmd: Command packet to be returned to free command pool
128 */
129static inline void
130megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
131{
132 unsigned long flags;
133
134 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
135
136 cmd->scmd = NULL;
137 list_add_tail(&cmd->list, &instance->cmd_pool);
138
139 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
140}
141
Sumant Patro1341c932006-01-25 12:02:40 -0800142
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400143/**
Sumant Patro1341c932006-01-25 12:02:40 -0800144* The following functions are defined for xscale
145* (deviceid : 1064R, PERC5) controllers
146*/
147
148/**
149 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400150 * @regs: MFI register set
151 */
152static inline void
Sumant Patro1341c932006-01-25 12:02:40 -0800153megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400154{
155 writel(1, &(regs)->outbound_intr_mask);
156
157 /* Dummy readl to force pci flush */
158 readl(&regs->outbound_intr_mask);
159}
160
161/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700162 * megasas_disable_intr_xscale -Disables interrupt
163 * @regs: MFI register set
164 */
165static inline void
166megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
167{
168 u32 mask = 0x1f;
169 writel(mask, &regs->outbound_intr_mask);
170 /* Dummy readl to force pci flush */
171 readl(&regs->outbound_intr_mask);
172}
173
174/**
Sumant Patro1341c932006-01-25 12:02:40 -0800175 * megasas_read_fw_status_reg_xscale - returns the current FW status value
176 * @regs: MFI register set
177 */
178static u32
179megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
180{
181 return readl(&(regs)->outbound_msg_0);
182}
183/**
184 * megasas_clear_interrupt_xscale - Check & clear interrupt
185 * @regs: MFI register set
186 */
187static int
188megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
189{
190 u32 status;
191 /*
192 * Check if it is our interrupt
193 */
194 status = readl(&regs->outbound_intr_status);
195
196 if (!(status & MFI_OB_INTR_STATUS_MASK)) {
197 return 1;
198 }
199
200 /*
201 * Clear the interrupt by writing back the same value
202 */
203 writel(status, &regs->outbound_intr_status);
204
Yang, Bo06f579d2008-08-10 12:42:37 -0700205 /* Dummy readl to force pci flush */
206 readl(&regs->outbound_intr_status);
207
Sumant Patro1341c932006-01-25 12:02:40 -0800208 return 0;
209}
210
211/**
212 * megasas_fire_cmd_xscale - Sends command to the FW
213 * @frame_phys_addr : Physical address of cmd
214 * @frame_count : Number of frames for the command
215 * @regs : MFI register set
216 */
217static inline void
218megasas_fire_cmd_xscale(dma_addr_t frame_phys_addr,u32 frame_count, struct megasas_register_set __iomem *regs)
219{
220 writel((frame_phys_addr >> 3)|(frame_count),
221 &(regs)->inbound_queue_port);
222}
223
224static struct megasas_instance_template megasas_instance_template_xscale = {
225
226 .fire_cmd = megasas_fire_cmd_xscale,
227 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700228 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800229 .clear_intr = megasas_clear_intr_xscale,
230 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
231};
232
233/**
234* This is the end of set of functions & definitions specific
235* to xscale (deviceid : 1064R, PERC5) controllers
236*/
237
238/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800239* The following functions are defined for ppc (deviceid : 0x60)
240* controllers
241*/
242
243/**
244 * megasas_enable_intr_ppc - Enables interrupts
245 * @regs: MFI register set
246 */
247static inline void
248megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
249{
250 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
251
252 writel(~0x80000004, &(regs)->outbound_intr_mask);
253
254 /* Dummy readl to force pci flush */
255 readl(&regs->outbound_intr_mask);
256}
257
258/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700259 * megasas_disable_intr_ppc - Disable interrupt
260 * @regs: MFI register set
261 */
262static inline void
263megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
264{
265 u32 mask = 0xFFFFFFFF;
266 writel(mask, &regs->outbound_intr_mask);
267 /* Dummy readl to force pci flush */
268 readl(&regs->outbound_intr_mask);
269}
270
271/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800272 * megasas_read_fw_status_reg_ppc - returns the current FW status value
273 * @regs: MFI register set
274 */
275static u32
276megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
277{
278 return readl(&(regs)->outbound_scratch_pad);
279}
280
281/**
282 * megasas_clear_interrupt_ppc - Check & clear interrupt
283 * @regs: MFI register set
284 */
285static int
286megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
287{
288 u32 status;
289 /*
290 * Check if it is our interrupt
291 */
292 status = readl(&regs->outbound_intr_status);
293
294 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
295 return 1;
296 }
297
298 /*
299 * Clear the interrupt by writing back the same value
300 */
301 writel(status, &regs->outbound_doorbell_clear);
302
Yang, Bo06f579d2008-08-10 12:42:37 -0700303 /* Dummy readl to force pci flush */
304 readl(&regs->outbound_doorbell_clear);
305
Sumant Patrof9876f02006-02-03 15:34:35 -0800306 return 0;
307}
308/**
309 * megasas_fire_cmd_ppc - Sends command to the FW
310 * @frame_phys_addr : Physical address of cmd
311 * @frame_count : Number of frames for the command
312 * @regs : MFI register set
313 */
314static inline void
315megasas_fire_cmd_ppc(dma_addr_t frame_phys_addr, u32 frame_count, struct megasas_register_set __iomem *regs)
316{
317 writel((frame_phys_addr | (frame_count<<1))|1,
318 &(regs)->inbound_queue_port);
319}
320
321static struct megasas_instance_template megasas_instance_template_ppc = {
322
323 .fire_cmd = megasas_fire_cmd_ppc,
324 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700325 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800326 .clear_intr = megasas_clear_intr_ppc,
327 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
328};
329
330/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700331* The following functions are defined for gen2 (deviceid : 0x78 0x79)
332* controllers
333*/
334
335/**
336 * megasas_enable_intr_gen2 - Enables interrupts
337 * @regs: MFI register set
338 */
339static inline void
340megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
341{
342 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
343
344 /* write ~0x00000005 (4 & 1) to the intr mask*/
345 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
346
347 /* Dummy readl to force pci flush */
348 readl(&regs->outbound_intr_mask);
349}
350
351/**
352 * megasas_disable_intr_gen2 - Disables interrupt
353 * @regs: MFI register set
354 */
355static inline void
356megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
357{
358 u32 mask = 0xFFFFFFFF;
359 writel(mask, &regs->outbound_intr_mask);
360 /* Dummy readl to force pci flush */
361 readl(&regs->outbound_intr_mask);
362}
363
364/**
365 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
366 * @regs: MFI register set
367 */
368static u32
369megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
370{
371 return readl(&(regs)->outbound_scratch_pad);
372}
373
374/**
375 * megasas_clear_interrupt_gen2 - Check & clear interrupt
376 * @regs: MFI register set
377 */
378static int
379megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
380{
381 u32 status;
382 /*
383 * Check if it is our interrupt
384 */
385 status = readl(&regs->outbound_intr_status);
386
387 if (!(status & MFI_GEN2_ENABLE_INTERRUPT_MASK))
388 return 1;
389
390 /*
391 * Clear the interrupt by writing back the same value
392 */
393 writel(status, &regs->outbound_doorbell_clear);
394
395 /* Dummy readl to force pci flush */
396 readl(&regs->outbound_intr_status);
397
398 return 0;
399}
400/**
401 * megasas_fire_cmd_gen2 - Sends command to the FW
402 * @frame_phys_addr : Physical address of cmd
403 * @frame_count : Number of frames for the command
404 * @regs : MFI register set
405 */
406static inline void
407megasas_fire_cmd_gen2(dma_addr_t frame_phys_addr, u32 frame_count,
408 struct megasas_register_set __iomem *regs)
409{
410 writel((frame_phys_addr | (frame_count<<1))|1,
411 &(regs)->inbound_queue_port);
412}
413
414static struct megasas_instance_template megasas_instance_template_gen2 = {
415
416 .fire_cmd = megasas_fire_cmd_gen2,
417 .enable_intr = megasas_enable_intr_gen2,
418 .disable_intr = megasas_disable_intr_gen2,
419 .clear_intr = megasas_clear_intr_gen2,
420 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
421};
422
423/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800424* This is the end of set of functions & definitions
425* specific to ppc (deviceid : 0x60) controllers
426*/
427
428/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400429 * megasas_issue_polled - Issues a polling command
430 * @instance: Adapter soft state
431 * @cmd: Command packet to be issued
432 *
433 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
434 */
435static int
436megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
437{
438 int i;
439 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
440
441 struct megasas_header *frame_hdr = &cmd->frame->hdr;
442
443 frame_hdr->cmd_status = 0xFF;
444 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
445
446 /*
447 * Issue the frame using inbound queue port
448 */
Sumant Patro1341c932006-01-25 12:02:40 -0800449 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400450
451 /*
452 * Wait for cmd_status to change
453 */
454 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
455 rmb();
456 msleep(1);
457 }
458
459 if (frame_hdr->cmd_status == 0xff)
460 return -ETIME;
461
462 return 0;
463}
464
465/**
466 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
467 * @instance: Adapter soft state
468 * @cmd: Command to be issued
469 *
470 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -0700471 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400472 * Used to issue ioctl commands.
473 */
474static int
475megasas_issue_blocked_cmd(struct megasas_instance *instance,
476 struct megasas_cmd *cmd)
477{
478 cmd->cmd_status = ENODATA;
479
Sumant Patro1341c932006-01-25 12:02:40 -0800480 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400481
Sumant Patro2a3681e2006-10-03 13:19:21 -0700482 wait_event_timeout(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA),
483 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400484
485 return 0;
486}
487
488/**
489 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
490 * @instance: Adapter soft state
491 * @cmd_to_abort: Previously issued cmd to be aborted
492 *
493 * MFI firmware can abort previously issued AEN comamnd (automatic event
494 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -0700495 * cmd and waits for return status.
496 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400497 */
498static int
499megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
500 struct megasas_cmd *cmd_to_abort)
501{
502 struct megasas_cmd *cmd;
503 struct megasas_abort_frame *abort_fr;
504
505 cmd = megasas_get_cmd(instance);
506
507 if (!cmd)
508 return -1;
509
510 abort_fr = &cmd->frame->abort;
511
512 /*
513 * Prepare and issue the abort frame
514 */
515 abort_fr->cmd = MFI_CMD_ABORT;
516 abort_fr->cmd_status = 0xFF;
517 abort_fr->flags = 0;
518 abort_fr->abort_context = cmd_to_abort->index;
519 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
520 abort_fr->abort_mfi_phys_addr_hi = 0;
521
522 cmd->sync_cmd = 1;
523 cmd->cmd_status = 0xFF;
524
Sumant Patro1341c932006-01-25 12:02:40 -0800525 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400526
527 /*
528 * Wait for this cmd to complete
529 */
Sumant Patro2a3681e2006-10-03 13:19:21 -0700530 wait_event_timeout(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF),
531 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400532
533 megasas_return_cmd(instance, cmd);
534 return 0;
535}
536
537/**
538 * megasas_make_sgl32 - Prepares 32-bit SGL
539 * @instance: Adapter soft state
540 * @scp: SCSI command from the mid-layer
541 * @mfi_sgl: SGL to be filled in
542 *
543 * If successful, this function returns the number of SG elements. Otherwise,
544 * it returnes -1.
545 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800546static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400547megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
548 union megasas_sgl *mfi_sgl)
549{
550 int i;
551 int sge_count;
552 struct scatterlist *os_sgl;
553
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900554 sge_count = scsi_dma_map(scp);
555 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400556
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900557 if (sge_count) {
558 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
559 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
560 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
561 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400562 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400563 return sge_count;
564}
565
566/**
567 * megasas_make_sgl64 - Prepares 64-bit SGL
568 * @instance: Adapter soft state
569 * @scp: SCSI command from the mid-layer
570 * @mfi_sgl: SGL to be filled in
571 *
572 * If successful, this function returns the number of SG elements. Otherwise,
573 * it returnes -1.
574 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800575static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400576megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
577 union megasas_sgl *mfi_sgl)
578{
579 int i;
580 int sge_count;
581 struct scatterlist *os_sgl;
582
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900583 sge_count = scsi_dma_map(scp);
584 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400585
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900586 if (sge_count) {
587 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
588 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
589 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
590 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400591 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400592 return sge_count;
593}
594
Sumant Patrob1df99d2006-10-03 12:40:47 -0700595 /**
596 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -0400597 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -0700598 * @sge_count : number of sg elements
599 *
600 * Returns the number of frames required for numnber of sge's (sge_count)
601 */
602
bo yangd532dbe2008-03-17 03:36:43 -0400603static u32 megasas_get_frame_count(u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -0700604{
605 int num_cnt;
606 int sge_bytes;
607 u32 sge_sz;
608 u32 frame_count=0;
609
610 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
611 sizeof(struct megasas_sge32);
612
613 /*
bo yangd532dbe2008-03-17 03:36:43 -0400614 * Main frame can contain 2 SGEs for 64-bit SGLs and
615 * 3 SGEs for 32-bit SGLs for ldio &
616 * 1 SGEs for 64-bit SGLs and
617 * 2 SGEs for 32-bit SGLs for pthru frame
618 */
619 if (unlikely(frame_type == PTHRU_FRAME)) {
620 if (IS_DMA64)
621 num_cnt = sge_count - 1;
622 else
623 num_cnt = sge_count - 2;
624 } else {
625 if (IS_DMA64)
626 num_cnt = sge_count - 2;
627 else
628 num_cnt = sge_count - 3;
629 }
Sumant Patrob1df99d2006-10-03 12:40:47 -0700630
631 if(num_cnt>0){
632 sge_bytes = sge_sz * num_cnt;
633
634 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
635 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
636 }
637 /* Main frame */
638 frame_count +=1;
639
640 if (frame_count > 7)
641 frame_count = 8;
642 return frame_count;
643}
644
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400645/**
646 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
647 * @instance: Adapter soft state
648 * @scp: SCSI command
649 * @cmd: Command to be prepared in
650 *
651 * This function prepares CDB commands. These are typcially pass-through
652 * commands to the devices.
653 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800654static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400655megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
656 struct megasas_cmd *cmd)
657{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400658 u32 is_logical;
659 u32 device_id;
660 u16 flags = 0;
661 struct megasas_pthru_frame *pthru;
662
663 is_logical = MEGASAS_IS_LOGICAL(scp);
664 device_id = MEGASAS_DEV_INDEX(instance, scp);
665 pthru = (struct megasas_pthru_frame *)cmd->frame;
666
667 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
668 flags = MFI_FRAME_DIR_WRITE;
669 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
670 flags = MFI_FRAME_DIR_READ;
671 else if (scp->sc_data_direction == PCI_DMA_NONE)
672 flags = MFI_FRAME_DIR_NONE;
673
674 /*
675 * Prepare the DCDB frame
676 */
677 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
678 pthru->cmd_status = 0x0;
679 pthru->scsi_status = 0x0;
680 pthru->target_id = device_id;
681 pthru->lun = scp->device->lun;
682 pthru->cdb_len = scp->cmd_len;
683 pthru->timeout = 0;
684 pthru->flags = flags;
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900685 pthru->data_xfer_len = scsi_bufflen(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400686
687 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
688
689 /*
Yang, Bo8d568252009-10-06 14:12:21 -0600690 * If the command is for the tape device, set the
691 * pthru timeout to the os layer timeout value.
692 */
693 if (scp->device->type == TYPE_TAPE) {
694 if ((scp->request->timeout / HZ) > 0xFFFF)
695 pthru->timeout = 0xFFFF;
696 else
697 pthru->timeout = scp->request->timeout / HZ;
698 }
699
700 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400701 * Construct SGL
702 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400703 if (IS_DMA64) {
704 pthru->flags |= MFI_FRAME_SGL64;
705 pthru->sge_count = megasas_make_sgl64(instance, scp,
706 &pthru->sgl);
707 } else
708 pthru->sge_count = megasas_make_sgl32(instance, scp,
709 &pthru->sgl);
710
711 /*
712 * Sense info specific
713 */
714 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
715 pthru->sense_buf_phys_addr_hi = 0;
716 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
717
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400718 /*
719 * Compute the total number of frames this command consumes. FW uses
720 * this number to pull sufficient number of frames from host memory.
721 */
bo yangd532dbe2008-03-17 03:36:43 -0400722 cmd->frame_count = megasas_get_frame_count(pthru->sge_count,
723 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400724
725 return cmd->frame_count;
726}
727
728/**
729 * megasas_build_ldio - Prepares IOs to logical devices
730 * @instance: Adapter soft state
731 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +0200732 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400733 *
734 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
735 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800736static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400737megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
738 struct megasas_cmd *cmd)
739{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400740 u32 device_id;
741 u8 sc = scp->cmnd[0];
742 u16 flags = 0;
743 struct megasas_io_frame *ldio;
744
745 device_id = MEGASAS_DEV_INDEX(instance, scp);
746 ldio = (struct megasas_io_frame *)cmd->frame;
747
748 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
749 flags = MFI_FRAME_DIR_WRITE;
750 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
751 flags = MFI_FRAME_DIR_READ;
752
753 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -0700754 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400755 */
756 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
757 ldio->cmd_status = 0x0;
758 ldio->scsi_status = 0x0;
759 ldio->target_id = device_id;
760 ldio->timeout = 0;
761 ldio->reserved_0 = 0;
762 ldio->pad_0 = 0;
763 ldio->flags = flags;
764 ldio->start_lba_hi = 0;
765 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
766
767 /*
768 * 6-byte READ(0x08) or WRITE(0x0A) cdb
769 */
770 if (scp->cmd_len == 6) {
771 ldio->lba_count = (u32) scp->cmnd[4];
772 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
773 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
774
775 ldio->start_lba_lo &= 0x1FFFFF;
776 }
777
778 /*
779 * 10-byte READ(0x28) or WRITE(0x2A) cdb
780 */
781 else if (scp->cmd_len == 10) {
782 ldio->lba_count = (u32) scp->cmnd[8] |
783 ((u32) scp->cmnd[7] << 8);
784 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
785 ((u32) scp->cmnd[3] << 16) |
786 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
787 }
788
789 /*
790 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
791 */
792 else if (scp->cmd_len == 12) {
793 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
794 ((u32) scp->cmnd[7] << 16) |
795 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
796
797 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
798 ((u32) scp->cmnd[3] << 16) |
799 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
800 }
801
802 /*
803 * 16-byte READ(0x88) or WRITE(0x8A) cdb
804 */
805 else if (scp->cmd_len == 16) {
806 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
807 ((u32) scp->cmnd[11] << 16) |
808 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
809
810 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
811 ((u32) scp->cmnd[7] << 16) |
812 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
813
814 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
815 ((u32) scp->cmnd[3] << 16) |
816 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
817
818 }
819
820 /*
821 * Construct SGL
822 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400823 if (IS_DMA64) {
824 ldio->flags |= MFI_FRAME_SGL64;
825 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
826 } else
827 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
828
829 /*
830 * Sense info specific
831 */
832 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
833 ldio->sense_buf_phys_addr_hi = 0;
834 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
835
Sumant Patrob1df99d2006-10-03 12:40:47 -0700836 /*
837 * Compute the total number of frames this command consumes. FW uses
838 * this number to pull sufficient number of frames from host memory.
839 */
bo yangd532dbe2008-03-17 03:36:43 -0400840 cmd->frame_count = megasas_get_frame_count(ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400841
842 return cmd->frame_count;
843}
844
845/**
Sumant Patrocb59aa62006-01-25 11:53:25 -0800846 * megasas_is_ldio - Checks if the cmd is for logical drive
847 * @scmd: SCSI command
848 *
849 * Called by megasas_queue_command to find out if the command to be queued
850 * is a logical drive command
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400851 */
Sumant Patrocb59aa62006-01-25 11:53:25 -0800852static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400853{
Sumant Patrocb59aa62006-01-25 11:53:25 -0800854 if (!MEGASAS_IS_LOGICAL(cmd))
855 return 0;
856 switch (cmd->cmnd[0]) {
857 case READ_10:
858 case WRITE_10:
859 case READ_12:
860 case WRITE_12:
861 case READ_6:
862 case WRITE_6:
863 case READ_16:
864 case WRITE_16:
865 return 1;
866 default:
867 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400868 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400869}
870
Sumant Patro658dced2006-10-03 13:09:14 -0700871 /**
872 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
873 * in FW
874 * @instance: Adapter soft state
875 */
876static inline void
877megasas_dump_pending_frames(struct megasas_instance *instance)
878{
879 struct megasas_cmd *cmd;
880 int i,n;
881 union megasas_sgl *mfi_sgl;
882 struct megasas_io_frame *ldio;
883 struct megasas_pthru_frame *pthru;
884 u32 sgcount;
885 u32 max_cmd = instance->max_fw_cmds;
886
887 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
888 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
889 if (IS_DMA64)
890 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
891 else
892 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
893
894 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
895 for (i = 0; i < max_cmd; i++) {
896 cmd = instance->cmd_list[i];
897 if(!cmd->scmd)
898 continue;
899 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
900 if (megasas_is_ldio(cmd->scmd)){
901 ldio = (struct megasas_io_frame *)cmd->frame;
902 mfi_sgl = &ldio->sgl;
903 sgcount = ldio->sge_count;
904 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);
905 }
906 else {
907 pthru = (struct megasas_pthru_frame *) cmd->frame;
908 mfi_sgl = &pthru->sgl;
909 sgcount = pthru->sge_count;
910 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);
911 }
912 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
913 for (n = 0; n < sgcount; n++){
914 if (IS_DMA64)
915 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) ;
916 else
917 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
918 }
919 }
920 printk(KERN_ERR "\n");
921 } /*for max_cmd*/
922 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
923 for (i = 0; i < max_cmd; i++) {
924
925 cmd = instance->cmd_list[i];
926
927 if(cmd->sync_cmd == 1){
928 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
929 }
930 }
931 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
932}
933
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400934/**
935 * megasas_queue_command - Queue entry point
936 * @scmd: SCSI command to be queued
937 * @done: Callback entry point
938 */
939static int
940megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
941{
942 u32 frame_count;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400943 struct megasas_cmd *cmd;
944 struct megasas_instance *instance;
945
946 instance = (struct megasas_instance *)
947 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -0800948
949 /* Don't process if we have already declared adapter dead */
950 if (instance->hw_crit_error)
951 return SCSI_MLQUEUE_HOST_BUSY;
952
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400953 scmd->scsi_done = done;
954 scmd->result = 0;
955
Sumant Patrocb59aa62006-01-25 11:53:25 -0800956 if (MEGASAS_IS_LOGICAL(scmd) &&
957 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
958 scmd->result = DID_BAD_TARGET << 16;
959 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400960 }
961
Sumant Patro02b01e02007-02-14 13:00:55 -0800962 switch (scmd->cmnd[0]) {
963 case SYNCHRONIZE_CACHE:
964 /*
965 * FW takes care of flush cache on its own
966 * No need to send it down
967 */
968 scmd->result = DID_OK << 16;
969 goto out_done;
970 default:
971 break;
972 }
973
Sumant Patrocb59aa62006-01-25 11:53:25 -0800974 cmd = megasas_get_cmd(instance);
975 if (!cmd)
976 return SCSI_MLQUEUE_HOST_BUSY;
977
978 /*
979 * Logical drive command
980 */
981 if (megasas_is_ldio(scmd))
982 frame_count = megasas_build_ldio(instance, scmd, cmd);
983 else
984 frame_count = megasas_build_dcdb(instance, scmd, cmd);
985
986 if (!frame_count)
987 goto out_return_cmd;
988
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400989 cmd->scmd = scmd;
Sumant Patro05e9ebb2007-05-17 05:47:51 -0700990 scmd->SCp.ptr = (char *)cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400991
992 /*
993 * Issue the command to the FW
994 */
Sumant Patroe4a082c2006-05-30 12:03:37 -0700995 atomic_inc(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400996
Sumant Patro1341c932006-01-25 12:02:40 -0800997 instance->instancet->fire_cmd(cmd->frame_phys_addr ,cmd->frame_count-1,instance->reg_set);
bo yangad84db22007-11-09 04:40:16 -0500998 /*
999 * Check if we have pend cmds to be completed
1000 */
1001 if (poll_mode_io && atomic_read(&instance->fw_outstanding))
1002 tasklet_schedule(&instance->isr_tasklet);
1003
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001004
1005 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001006
1007 out_return_cmd:
1008 megasas_return_cmd(instance, cmd);
1009 out_done:
1010 done(scmd);
1011 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001012}
1013
Christoph Hellwig147aab62006-02-17 12:13:48 +01001014static int megasas_slave_configure(struct scsi_device *sdev)
1015{
1016 /*
1017 * Don't export physical disk devices to the disk driver.
1018 *
1019 * FIXME: Currently we don't export them to the midlayer at all.
1020 * That will be fixed once LSI engineers have audited the
1021 * firmware for possible issues.
1022 */
1023 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS && sdev->type == TYPE_DISK)
1024 return -ENXIO;
Christoph Hellwige5b3a652006-03-10 17:08:57 +01001025
1026 /*
1027 * The RAID firmware may require extended timeouts.
1028 */
1029 if (sdev->channel >= MEGASAS_MAX_PD_CHANNELS)
James Bottomleyee1ab9e2008-11-30 10:27:51 -06001030 blk_queue_rq_timeout(sdev->request_queue,
1031 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
Christoph Hellwig147aab62006-02-17 12:13:48 +01001032 return 0;
1033}
1034
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001035/**
bo yang7343eb62007-11-09 04:35:44 -05001036 * megasas_complete_cmd_dpc - Returns FW's controller structure
1037 * @instance_addr: Address of adapter soft state
1038 *
1039 * Tasklet to complete cmds
1040 */
1041static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1042{
1043 u32 producer;
1044 u32 consumer;
1045 u32 context;
1046 struct megasas_cmd *cmd;
1047 struct megasas_instance *instance =
1048 (struct megasas_instance *)instance_addr;
1049 unsigned long flags;
1050
1051 /* If we have already declared adapter dead, donot complete cmds */
1052 if (instance->hw_crit_error)
1053 return;
1054
1055 spin_lock_irqsave(&instance->completion_lock, flags);
1056
1057 producer = *instance->producer;
1058 consumer = *instance->consumer;
1059
1060 while (consumer != producer) {
1061 context = instance->reply_queue[consumer];
1062
1063 cmd = instance->cmd_list[context];
1064
1065 megasas_complete_cmd(instance, cmd, DID_OK);
1066
1067 consumer++;
1068 if (consumer == (instance->max_fw_cmds + 1)) {
1069 consumer = 0;
1070 }
1071 }
1072
1073 *instance->consumer = producer;
1074
1075 spin_unlock_irqrestore(&instance->completion_lock, flags);
1076
1077 /*
1078 * Check if we can restore can_queue
1079 */
1080 if (instance->flag & MEGASAS_FW_BUSY
1081 && time_after(jiffies, instance->last_time + 5 * HZ)
1082 && atomic_read(&instance->fw_outstanding) < 17) {
1083
1084 spin_lock_irqsave(instance->host->host_lock, flags);
1085 instance->flag &= ~MEGASAS_FW_BUSY;
1086 instance->host->can_queue =
1087 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1088
1089 spin_unlock_irqrestore(instance->host->host_lock, flags);
1090 }
1091}
1092
1093/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001094 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1095 * @instance: Adapter soft state
1096 *
1097 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
1098 * complete all its outstanding commands. Returns error if one or more IOs
1099 * are pending after this time period. It also marks the controller dead.
1100 */
1101static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1102{
1103 int i;
1104 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
1105
1106 for (i = 0; i < wait_time; i++) {
1107
Sumant Patroe4a082c2006-05-30 12:03:37 -07001108 int outstanding = atomic_read(&instance->fw_outstanding);
1109
1110 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001111 break;
1112
1113 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1114 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07001115 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05001116 /*
1117 * Call cmd completion routine. Cmd to be
1118 * be completed directly without depending on isr.
1119 */
1120 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001121 }
1122
1123 msleep(1000);
1124 }
1125
Sumant Patroe4a082c2006-05-30 12:03:37 -07001126 if (atomic_read(&instance->fw_outstanding)) {
Sumant Patroe3bbff92006-10-03 12:28:49 -07001127 /*
1128 * Send signal to FW to stop processing any pending cmds.
1129 * The controller will be taken offline by the OS now.
1130 */
1131 writel(MFI_STOP_ADP,
1132 &instance->reg_set->inbound_doorbell);
Sumant Patro658dced2006-10-03 13:09:14 -07001133 megasas_dump_pending_frames(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001134 instance->hw_crit_error = 1;
1135 return FAILED;
1136 }
1137
1138 return SUCCESS;
1139}
1140
1141/**
1142 * megasas_generic_reset - Generic reset routine
1143 * @scmd: Mid-layer SCSI command
1144 *
1145 * This routine implements a generic reset handler for device, bus and host
1146 * reset requests. Device, bus and host specific reset handlers can use this
1147 * function after they do their specific tasks.
1148 */
1149static int megasas_generic_reset(struct scsi_cmnd *scmd)
1150{
1151 int ret_val;
1152 struct megasas_instance *instance;
1153
1154 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1155
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001156 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
1157 scmd->serial_number, scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001158
1159 if (instance->hw_crit_error) {
1160 printk(KERN_ERR "megasas: cannot recover from previous reset "
1161 "failures\n");
1162 return FAILED;
1163 }
1164
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001165 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001166 if (ret_val == SUCCESS)
1167 printk(KERN_NOTICE "megasas: reset successful \n");
1168 else
1169 printk(KERN_ERR "megasas: failed to do reset\n");
1170
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001171 return ret_val;
1172}
1173
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001174/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001175 * megasas_reset_timer - quiesce the adapter if required
1176 * @scmd: scsi cmnd
1177 *
1178 * Sets the FW busy flag and reduces the host->can_queue if the
1179 * cmd has not been completed within the timeout period.
1180 */
1181static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07001182blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001183{
1184 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
1185 struct megasas_instance *instance;
1186 unsigned long flags;
1187
1188 if (time_after(jiffies, scmd->jiffies_at_alloc +
1189 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001190 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001191 }
1192
1193 instance = cmd->instance;
1194 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1195 /* FW is busy, throttle IO */
1196 spin_lock_irqsave(instance->host->host_lock, flags);
1197
1198 instance->host->can_queue = 16;
1199 instance->last_time = jiffies;
1200 instance->flag |= MEGASAS_FW_BUSY;
1201
1202 spin_unlock_irqrestore(instance->host->host_lock, flags);
1203 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07001204 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001205}
1206
1207/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001208 * megasas_reset_device - Device reset handler entry point
1209 */
1210static int megasas_reset_device(struct scsi_cmnd *scmd)
1211{
1212 int ret;
1213
1214 /*
1215 * First wait for all commands to complete
1216 */
1217 ret = megasas_generic_reset(scmd);
1218
1219 return ret;
1220}
1221
1222/**
1223 * megasas_reset_bus_host - Bus & host reset handler entry point
1224 */
1225static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1226{
1227 int ret;
1228
1229 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01001230 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001231 */
1232 ret = megasas_generic_reset(scmd);
1233
1234 return ret;
1235}
1236
1237/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001238 * megasas_bios_param - Returns disk geometry for a disk
1239 * @sdev: device handle
1240 * @bdev: block device
1241 * @capacity: drive capacity
1242 * @geom: geometry parameters
1243 */
1244static int
1245megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1246 sector_t capacity, int geom[])
1247{
1248 int heads;
1249 int sectors;
1250 sector_t cylinders;
1251 unsigned long tmp;
1252 /* Default heads (64) & sectors (32) */
1253 heads = 64;
1254 sectors = 32;
1255
1256 tmp = heads * sectors;
1257 cylinders = capacity;
1258
1259 sector_div(cylinders, tmp);
1260
1261 /*
1262 * Handle extended translation size for logical drives > 1Gb
1263 */
1264
1265 if (capacity >= 0x200000) {
1266 heads = 255;
1267 sectors = 63;
1268 tmp = heads*sectors;
1269 cylinders = capacity;
1270 sector_div(cylinders, tmp);
1271 }
1272
1273 geom[0] = heads;
1274 geom[1] = sectors;
1275 geom[2] = cylinders;
1276
1277 return 0;
1278}
1279
1280/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001281 * megasas_service_aen - Processes an event notification
1282 * @instance: Adapter soft state
1283 * @cmd: AEN command completed by the ISR
1284 *
1285 * For AEN, driver sends a command down to FW that is held by the FW till an
1286 * event occurs. When an event of interest occurs, FW completes the command
1287 * that it was previously holding.
1288 *
1289 * This routines sends SIGIO signal to processes that have registered with the
1290 * driver for AEN.
1291 */
1292static void
1293megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1294{
1295 /*
1296 * Don't signal app if it is just an aborted previously registered aen
1297 */
1298 if (!cmd->abort_aen)
1299 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
1300 else
1301 cmd->abort_aen = 0;
1302
1303 instance->aen_cmd = NULL;
1304 megasas_return_cmd(instance, cmd);
1305}
1306
1307/*
1308 * Scsi host template for megaraid_sas driver
1309 */
1310static struct scsi_host_template megasas_template = {
1311
1312 .module = THIS_MODULE,
bo yangf28cd7c2007-11-09 04:44:56 -05001313 .name = "LSI SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001314 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01001315 .slave_configure = megasas_slave_configure,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001316 .queuecommand = megasas_queue_command,
1317 .eh_device_reset_handler = megasas_reset_device,
1318 .eh_bus_reset_handler = megasas_reset_bus_host,
1319 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001320 .eh_timed_out = megasas_reset_timer,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001321 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001322 .use_clustering = ENABLE_CLUSTERING,
1323};
1324
1325/**
1326 * megasas_complete_int_cmd - Completes an internal command
1327 * @instance: Adapter soft state
1328 * @cmd: Command to be completed
1329 *
1330 * The megasas_issue_blocked_cmd() function waits for a command to complete
1331 * after it issues a command. This function wakes up that waiting routine by
1332 * calling wake_up() on the wait queue.
1333 */
1334static void
1335megasas_complete_int_cmd(struct megasas_instance *instance,
1336 struct megasas_cmd *cmd)
1337{
1338 cmd->cmd_status = cmd->frame->io.cmd_status;
1339
1340 if (cmd->cmd_status == ENODATA) {
1341 cmd->cmd_status = 0;
1342 }
1343 wake_up(&instance->int_cmd_wait_q);
1344}
1345
1346/**
1347 * megasas_complete_abort - Completes aborting a command
1348 * @instance: Adapter soft state
1349 * @cmd: Cmd that was issued to abort another cmd
1350 *
1351 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
1352 * after it issues an abort on a previously issued command. This function
1353 * wakes up all functions waiting on the same wait queue.
1354 */
1355static void
1356megasas_complete_abort(struct megasas_instance *instance,
1357 struct megasas_cmd *cmd)
1358{
1359 if (cmd->sync_cmd) {
1360 cmd->sync_cmd = 0;
1361 cmd->cmd_status = 0;
1362 wake_up(&instance->abort_cmd_wait_q);
1363 }
1364
1365 return;
1366}
1367
1368/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001369 * megasas_complete_cmd - Completes a command
1370 * @instance: Adapter soft state
1371 * @cmd: Command to be completed
1372 * @alt_status: If non-zero, use this value as status to
1373 * SCSI mid-layer instead of the value returned
1374 * by the FW. This should be used if caller wants
1375 * an alternate status (as in the case of aborted
1376 * commands)
1377 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001378static void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001379megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
1380 u8 alt_status)
1381{
1382 int exception = 0;
1383 struct megasas_header *hdr = &cmd->frame->hdr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001384
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001385 if (cmd->scmd)
1386 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001387
1388 switch (hdr->cmd) {
1389
1390 case MFI_CMD_PD_SCSI_IO:
1391 case MFI_CMD_LD_SCSI_IO:
1392
1393 /*
1394 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1395 * issued either through an IO path or an IOCTL path. If it
1396 * was via IOCTL, we will send it to internal completion.
1397 */
1398 if (cmd->sync_cmd) {
1399 cmd->sync_cmd = 0;
1400 megasas_complete_int_cmd(instance, cmd);
1401 break;
1402 }
1403
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001404 case MFI_CMD_LD_READ:
1405 case MFI_CMD_LD_WRITE:
1406
1407 if (alt_status) {
1408 cmd->scmd->result = alt_status << 16;
1409 exception = 1;
1410 }
1411
1412 if (exception) {
1413
Sumant Patroe4a082c2006-05-30 12:03:37 -07001414 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001415
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001416 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001417 cmd->scmd->scsi_done(cmd->scmd);
1418 megasas_return_cmd(instance, cmd);
1419
1420 break;
1421 }
1422
1423 switch (hdr->cmd_status) {
1424
1425 case MFI_STAT_OK:
1426 cmd->scmd->result = DID_OK << 16;
1427 break;
1428
1429 case MFI_STAT_SCSI_IO_FAILED:
1430 case MFI_STAT_LD_INIT_IN_PROGRESS:
1431 cmd->scmd->result =
1432 (DID_ERROR << 16) | hdr->scsi_status;
1433 break;
1434
1435 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1436
1437 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1438
1439 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1440 memset(cmd->scmd->sense_buffer, 0,
1441 SCSI_SENSE_BUFFERSIZE);
1442 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1443 hdr->sense_len);
1444
1445 cmd->scmd->result |= DRIVER_SENSE << 24;
1446 }
1447
1448 break;
1449
1450 case MFI_STAT_LD_OFFLINE:
1451 case MFI_STAT_DEVICE_NOT_FOUND:
1452 cmd->scmd->result = DID_BAD_TARGET << 16;
1453 break;
1454
1455 default:
1456 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1457 hdr->cmd_status);
1458 cmd->scmd->result = DID_ERROR << 16;
1459 break;
1460 }
1461
Sumant Patroe4a082c2006-05-30 12:03:37 -07001462 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001463
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001464 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001465 cmd->scmd->scsi_done(cmd->scmd);
1466 megasas_return_cmd(instance, cmd);
1467
1468 break;
1469
1470 case MFI_CMD_SMP:
1471 case MFI_CMD_STP:
1472 case MFI_CMD_DCMD:
1473
1474 /*
1475 * See if got an event notification
1476 */
1477 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1478 megasas_service_aen(instance, cmd);
1479 else
1480 megasas_complete_int_cmd(instance, cmd);
1481
1482 break;
1483
1484 case MFI_CMD_ABORT:
1485 /*
1486 * Cmd issued to abort another cmd returned
1487 */
1488 megasas_complete_abort(instance, cmd);
1489 break;
1490
1491 default:
1492 printk("megasas: Unknown command completed! [0x%X]\n",
1493 hdr->cmd);
1494 break;
1495 }
1496}
1497
1498/**
1499 * megasas_deplete_reply_queue - Processes all completed commands
1500 * @instance: Adapter soft state
1501 * @alt_status: Alternate status to be returned to
1502 * SCSI mid-layer instead of the status
1503 * returned by the FW
1504 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001505static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001506megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1507{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001508 /*
1509 * Check if it is our interrupt
Sumant Patro1341c932006-01-25 12:02:40 -08001510 * Clear the interrupt
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001511 */
Sumant Patro1341c932006-01-25 12:02:40 -08001512 if(instance->instancet->clear_intr(instance->reg_set))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001513 return IRQ_NONE;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001514
Sumant Patroaf37acf2007-02-14 12:34:46 -08001515 if (instance->hw_crit_error)
1516 goto out_done;
Sumant Patro5d018ad2006-10-03 13:13:18 -07001517 /*
1518 * Schedule the tasklet for cmd completion
1519 */
1520 tasklet_schedule(&instance->isr_tasklet);
Sumant Patroaf37acf2007-02-14 12:34:46 -08001521out_done:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001522 return IRQ_HANDLED;
1523}
1524
1525/**
1526 * megasas_isr - isr entry point
1527 */
David Howells7d12e782006-10-05 14:55:46 +01001528static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001529{
1530 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1531 DID_OK);
1532}
1533
1534/**
1535 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08001536 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001537 *
1538 * During the initialization, FW passes can potentially be in any one of
1539 * several possible states. If the FW in operational, waiting-for-handshake
1540 * states, driver must take steps to bring it to ready state. Otherwise, it
1541 * has to wait for the ready state.
1542 */
1543static int
Sumant Patro1341c932006-01-25 12:02:40 -08001544megasas_transition_to_ready(struct megasas_instance* instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001545{
1546 int i;
1547 u8 max_wait;
1548 u32 fw_state;
1549 u32 cur_state;
1550
Sumant Patro1341c932006-01-25 12:02:40 -08001551 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001552
Sumant Patroe3bbff92006-10-03 12:28:49 -07001553 if (fw_state != MFI_STATE_READY)
1554 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1555 " state\n");
1556
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001557 while (fw_state != MFI_STATE_READY) {
1558
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001559 switch (fw_state) {
1560
1561 case MFI_STATE_FAULT:
1562
1563 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1564 return -ENODEV;
1565
1566 case MFI_STATE_WAIT_HANDSHAKE:
1567 /*
1568 * Set the CLR bit in inbound doorbell
1569 */
Sumant Patroe3bbff92006-10-03 12:28:49 -07001570 writel(MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
Sumant Patro1341c932006-01-25 12:02:40 -08001571 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001572
1573 max_wait = 2;
1574 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1575 break;
1576
Sumant Patroe3bbff92006-10-03 12:28:49 -07001577 case MFI_STATE_BOOT_MESSAGE_PENDING:
1578 writel(MFI_INIT_HOTPLUG,
1579 &instance->reg_set->inbound_doorbell);
1580
1581 max_wait = 10;
1582 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
1583 break;
1584
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001585 case MFI_STATE_OPERATIONAL:
1586 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07001587 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001588 */
Sumant Patrob274cab2006-10-03 12:52:12 -07001589 instance->instancet->disable_intr(instance->reg_set);
Sumant Patroe3bbff92006-10-03 12:28:49 -07001590 writel(MFI_RESET_FLAGS, &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001591
bo yangaf7a5642008-03-17 04:13:07 -04001592 max_wait = 60;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001593 cur_state = MFI_STATE_OPERATIONAL;
1594 break;
1595
1596 case MFI_STATE_UNDEFINED:
1597 /*
1598 * This state should not last for more than 2 seconds
1599 */
1600 max_wait = 2;
1601 cur_state = MFI_STATE_UNDEFINED;
1602 break;
1603
1604 case MFI_STATE_BB_INIT:
1605 max_wait = 2;
1606 cur_state = MFI_STATE_BB_INIT;
1607 break;
1608
1609 case MFI_STATE_FW_INIT:
1610 max_wait = 20;
1611 cur_state = MFI_STATE_FW_INIT;
1612 break;
1613
1614 case MFI_STATE_FW_INIT_2:
1615 max_wait = 20;
1616 cur_state = MFI_STATE_FW_INIT_2;
1617 break;
1618
1619 case MFI_STATE_DEVICE_SCAN:
1620 max_wait = 20;
1621 cur_state = MFI_STATE_DEVICE_SCAN;
1622 break;
1623
1624 case MFI_STATE_FLUSH_CACHE:
1625 max_wait = 20;
1626 cur_state = MFI_STATE_FLUSH_CACHE;
1627 break;
1628
1629 default:
1630 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1631 fw_state);
1632 return -ENODEV;
1633 }
1634
1635 /*
1636 * The cur_state should not last for more than max_wait secs
1637 */
1638 for (i = 0; i < (max_wait * 1000); i++) {
Sumant Patro1341c932006-01-25 12:02:40 -08001639 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1640 MFI_STATE_MASK ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001641
1642 if (fw_state == cur_state) {
1643 msleep(1);
1644 } else
1645 break;
1646 }
1647
1648 /*
1649 * Return error if fw_state hasn't changed after max_wait
1650 */
1651 if (fw_state == cur_state) {
1652 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1653 "in %d secs\n", fw_state, max_wait);
1654 return -ENODEV;
1655 }
1656 };
Sumant Patroe3bbff92006-10-03 12:28:49 -07001657 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001658
1659 return 0;
1660}
1661
1662/**
1663 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1664 * @instance: Adapter soft state
1665 */
1666static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1667{
1668 int i;
1669 u32 max_cmd = instance->max_fw_cmds;
1670 struct megasas_cmd *cmd;
1671
1672 if (!instance->frame_dma_pool)
1673 return;
1674
1675 /*
1676 * Return all frames to pool
1677 */
1678 for (i = 0; i < max_cmd; i++) {
1679
1680 cmd = instance->cmd_list[i];
1681
1682 if (cmd->frame)
1683 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1684 cmd->frame_phys_addr);
1685
1686 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07001687 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001688 cmd->sense_phys_addr);
1689 }
1690
1691 /*
1692 * Now destroy the pool itself
1693 */
1694 pci_pool_destroy(instance->frame_dma_pool);
1695 pci_pool_destroy(instance->sense_dma_pool);
1696
1697 instance->frame_dma_pool = NULL;
1698 instance->sense_dma_pool = NULL;
1699}
1700
1701/**
1702 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1703 * @instance: Adapter soft state
1704 *
1705 * Each command packet has an embedded DMA memory buffer that is used for
1706 * filling MFI frame and the SG list that immediately follows the frame. This
1707 * function creates those DMA memory buffers for each command packet by using
1708 * PCI pool facility.
1709 */
1710static int megasas_create_frame_pool(struct megasas_instance *instance)
1711{
1712 int i;
1713 u32 max_cmd;
1714 u32 sge_sz;
1715 u32 sgl_sz;
1716 u32 total_sz;
1717 u32 frame_count;
1718 struct megasas_cmd *cmd;
1719
1720 max_cmd = instance->max_fw_cmds;
1721
1722 /*
1723 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1724 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1725 */
1726 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1727 sizeof(struct megasas_sge32);
1728
1729 /*
1730 * Calculated the number of 64byte frames required for SGL
1731 */
1732 sgl_sz = sge_sz * instance->max_num_sge;
1733 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1734
1735 /*
1736 * We need one extra frame for the MFI command
1737 */
1738 frame_count++;
1739
1740 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1741 /*
1742 * Use DMA pool facility provided by PCI layer
1743 */
1744 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1745 instance->pdev, total_sz, 64,
1746 0);
1747
1748 if (!instance->frame_dma_pool) {
1749 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1750 return -ENOMEM;
1751 }
1752
1753 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1754 instance->pdev, 128, 4, 0);
1755
1756 if (!instance->sense_dma_pool) {
1757 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1758
1759 pci_pool_destroy(instance->frame_dma_pool);
1760 instance->frame_dma_pool = NULL;
1761
1762 return -ENOMEM;
1763 }
1764
1765 /*
1766 * Allocate and attach a frame to each of the commands in cmd_list.
1767 * By making cmd->index as the context instead of the &cmd, we can
1768 * always use 32bit context regardless of the architecture
1769 */
1770 for (i = 0; i < max_cmd; i++) {
1771
1772 cmd = instance->cmd_list[i];
1773
1774 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1775 GFP_KERNEL, &cmd->frame_phys_addr);
1776
1777 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1778 GFP_KERNEL, &cmd->sense_phys_addr);
1779
1780 /*
1781 * megasas_teardown_frame_pool() takes care of freeing
1782 * whatever has been allocated
1783 */
1784 if (!cmd->frame || !cmd->sense) {
1785 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1786 megasas_teardown_frame_pool(instance);
1787 return -ENOMEM;
1788 }
1789
1790 cmd->frame->io.context = cmd->index;
1791 }
1792
1793 return 0;
1794}
1795
1796/**
1797 * megasas_free_cmds - Free all the cmds in the free cmd pool
1798 * @instance: Adapter soft state
1799 */
1800static void megasas_free_cmds(struct megasas_instance *instance)
1801{
1802 int i;
1803 /* First free the MFI frame pool */
1804 megasas_teardown_frame_pool(instance);
1805
1806 /* Free all the commands in the cmd_list */
1807 for (i = 0; i < instance->max_fw_cmds; i++)
1808 kfree(instance->cmd_list[i]);
1809
1810 /* Free the cmd_list buffer itself */
1811 kfree(instance->cmd_list);
1812 instance->cmd_list = NULL;
1813
1814 INIT_LIST_HEAD(&instance->cmd_pool);
1815}
1816
1817/**
1818 * megasas_alloc_cmds - Allocates the command packets
1819 * @instance: Adapter soft state
1820 *
1821 * Each command that is issued to the FW, whether IO commands from the OS or
1822 * internal commands like IOCTLs, are wrapped in local data structure called
1823 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1824 * the FW.
1825 *
1826 * Each frame has a 32-bit field called context (tag). This context is used
1827 * to get back the megasas_cmd from the frame when a frame gets completed in
1828 * the ISR. Typically the address of the megasas_cmd itself would be used as
1829 * the context. But we wanted to keep the differences between 32 and 64 bit
1830 * systems to the mininum. We always use 32 bit integers for the context. In
1831 * this driver, the 32 bit values are the indices into an array cmd_list.
1832 * This array is used only to look up the megasas_cmd given the context. The
1833 * free commands themselves are maintained in a linked list called cmd_pool.
1834 */
1835static int megasas_alloc_cmds(struct megasas_instance *instance)
1836{
1837 int i;
1838 int j;
1839 u32 max_cmd;
1840 struct megasas_cmd *cmd;
1841
1842 max_cmd = instance->max_fw_cmds;
1843
1844 /*
1845 * instance->cmd_list is an array of struct megasas_cmd pointers.
1846 * Allocate the dynamic array first and then allocate individual
1847 * commands.
1848 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001849 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001850
1851 if (!instance->cmd_list) {
1852 printk(KERN_DEBUG "megasas: out of memory\n");
1853 return -ENOMEM;
1854 }
1855
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001856
1857 for (i = 0; i < max_cmd; i++) {
1858 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
1859 GFP_KERNEL);
1860
1861 if (!instance->cmd_list[i]) {
1862
1863 for (j = 0; j < i; j++)
1864 kfree(instance->cmd_list[j]);
1865
1866 kfree(instance->cmd_list);
1867 instance->cmd_list = NULL;
1868
1869 return -ENOMEM;
1870 }
1871 }
1872
1873 /*
1874 * Add all the commands to command pool (instance->cmd_pool)
1875 */
1876 for (i = 0; i < max_cmd; i++) {
1877 cmd = instance->cmd_list[i];
1878 memset(cmd, 0, sizeof(struct megasas_cmd));
1879 cmd->index = i;
1880 cmd->instance = instance;
1881
1882 list_add_tail(&cmd->list, &instance->cmd_pool);
1883 }
1884
1885 /*
1886 * Create a frame pool and assign one frame to each cmd
1887 */
1888 if (megasas_create_frame_pool(instance)) {
1889 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
1890 megasas_free_cmds(instance);
1891 }
1892
1893 return 0;
1894}
1895
1896/**
1897 * megasas_get_controller_info - Returns FW's controller structure
1898 * @instance: Adapter soft state
1899 * @ctrl_info: Controller information structure
1900 *
1901 * Issues an internal command (DCMD) to get the FW's controller structure.
1902 * This information is mainly used to find out the maximum IO transfer per
1903 * command supported by the FW.
1904 */
1905static int
1906megasas_get_ctrl_info(struct megasas_instance *instance,
1907 struct megasas_ctrl_info *ctrl_info)
1908{
1909 int ret = 0;
1910 struct megasas_cmd *cmd;
1911 struct megasas_dcmd_frame *dcmd;
1912 struct megasas_ctrl_info *ci;
1913 dma_addr_t ci_h = 0;
1914
1915 cmd = megasas_get_cmd(instance);
1916
1917 if (!cmd) {
1918 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
1919 return -ENOMEM;
1920 }
1921
1922 dcmd = &cmd->frame->dcmd;
1923
1924 ci = pci_alloc_consistent(instance->pdev,
1925 sizeof(struct megasas_ctrl_info), &ci_h);
1926
1927 if (!ci) {
1928 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
1929 megasas_return_cmd(instance, cmd);
1930 return -ENOMEM;
1931 }
1932
1933 memset(ci, 0, sizeof(*ci));
1934 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1935
1936 dcmd->cmd = MFI_CMD_DCMD;
1937 dcmd->cmd_status = 0xFF;
1938 dcmd->sge_count = 1;
1939 dcmd->flags = MFI_FRAME_DIR_READ;
1940 dcmd->timeout = 0;
1941 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
1942 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
1943 dcmd->sgl.sge32[0].phys_addr = ci_h;
1944 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
1945
1946 if (!megasas_issue_polled(instance, cmd)) {
1947 ret = 0;
1948 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
1949 } else {
1950 ret = -1;
1951 }
1952
1953 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
1954 ci, ci_h);
1955
1956 megasas_return_cmd(instance, cmd);
1957 return ret;
1958}
1959
1960/**
bo yang31ea7082007-11-07 12:09:50 -05001961 * megasas_issue_init_mfi - Initializes the FW
1962 * @instance: Adapter soft state
1963 *
1964 * Issues the INIT MFI cmd
1965 */
1966static int
1967megasas_issue_init_mfi(struct megasas_instance *instance)
1968{
1969 u32 context;
1970
1971 struct megasas_cmd *cmd;
1972
1973 struct megasas_init_frame *init_frame;
1974 struct megasas_init_queue_info *initq_info;
1975 dma_addr_t init_frame_h;
1976 dma_addr_t initq_info_h;
1977
1978 /*
1979 * Prepare a init frame. Note the init frame points to queue info
1980 * structure. Each frame has SGL allocated after first 64 bytes. For
1981 * this frame - since we don't need any SGL - we use SGL's space as
1982 * queue info structure
1983 *
1984 * We will not get a NULL command below. We just created the pool.
1985 */
1986 cmd = megasas_get_cmd(instance);
1987
1988 init_frame = (struct megasas_init_frame *)cmd->frame;
1989 initq_info = (struct megasas_init_queue_info *)
1990 ((unsigned long)init_frame + 64);
1991
1992 init_frame_h = cmd->frame_phys_addr;
1993 initq_info_h = init_frame_h + 64;
1994
1995 context = init_frame->context;
1996 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
1997 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
1998 init_frame->context = context;
1999
2000 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
2001 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
2002
2003 initq_info->producer_index_phys_addr_lo = instance->producer_h;
2004 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
2005
2006 init_frame->cmd = MFI_CMD_INIT;
2007 init_frame->cmd_status = 0xFF;
2008 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
2009
2010 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
2011
2012 /*
2013 * disable the intr before firing the init frame to FW
2014 */
2015 instance->instancet->disable_intr(instance->reg_set);
2016
2017 /*
2018 * Issue the init frame in polled mode
2019 */
2020
2021 if (megasas_issue_polled(instance, cmd)) {
2022 printk(KERN_ERR "megasas: Failed to init firmware\n");
2023 megasas_return_cmd(instance, cmd);
2024 goto fail_fw_init;
2025 }
2026
2027 megasas_return_cmd(instance, cmd);
2028
2029 return 0;
2030
2031fail_fw_init:
2032 return -EINVAL;
2033}
2034
2035/**
bo yangad84db22007-11-09 04:40:16 -05002036 * megasas_start_timer - Initializes a timer object
2037 * @instance: Adapter soft state
2038 * @timer: timer object to be initialized
2039 * @fn: timer function
2040 * @interval: time interval between timer function call
2041 */
2042static inline void
2043megasas_start_timer(struct megasas_instance *instance,
2044 struct timer_list *timer,
2045 void *fn, unsigned long interval)
2046{
2047 init_timer(timer);
2048 timer->expires = jiffies + interval;
2049 timer->data = (unsigned long)instance;
2050 timer->function = fn;
2051 add_timer(timer);
2052}
2053
2054/**
2055 * megasas_io_completion_timer - Timer fn
2056 * @instance_addr: Address of adapter soft state
2057 *
2058 * Schedules tasklet for cmd completion
2059 * if poll_mode_io is set
2060 */
2061static void
2062megasas_io_completion_timer(unsigned long instance_addr)
2063{
2064 struct megasas_instance *instance =
2065 (struct megasas_instance *)instance_addr;
2066
2067 if (atomic_read(&instance->fw_outstanding))
2068 tasklet_schedule(&instance->isr_tasklet);
2069
2070 /* Restart timer */
2071 if (poll_mode_io)
2072 mod_timer(&instance->io_completion_timer,
2073 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
2074}
2075
2076/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002077 * megasas_init_mfi - Initializes the FW
2078 * @instance: Adapter soft state
2079 *
2080 * This is the main function for initializing MFI firmware.
2081 */
2082static int megasas_init_mfi(struct megasas_instance *instance)
2083{
2084 u32 context_sz;
2085 u32 reply_q_sz;
2086 u32 max_sectors_1;
2087 u32 max_sectors_2;
bo yang14faea92007-11-09 04:14:00 -05002088 u32 tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002089 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002090 struct megasas_ctrl_info *ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002091 /*
2092 * Map the message registers
2093 */
Yang, Bo6610a6b2008-08-10 12:42:38 -07002094 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
2095 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0079GEN2)) {
2096 instance->base_addr = pci_resource_start(instance->pdev, 1);
2097 } else {
2098 instance->base_addr = pci_resource_start(instance->pdev, 0);
2099 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002100
bo yangf28cd7c2007-11-09 04:44:56 -05002101 if (pci_request_regions(instance->pdev, "megasas: LSI")) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002102 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
2103 return -EBUSY;
2104 }
2105
2106 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
2107
2108 if (!instance->reg_set) {
2109 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
2110 goto fail_ioremap;
2111 }
2112
2113 reg_set = instance->reg_set;
2114
Sumant Patrof9876f02006-02-03 15:34:35 -08002115 switch(instance->pdev->device)
2116 {
bo yangaf7a5642008-03-17 04:13:07 -04002117 case PCI_DEVICE_ID_LSI_SAS1078R:
2118 case PCI_DEVICE_ID_LSI_SAS1078DE:
Sumant Patrof9876f02006-02-03 15:34:35 -08002119 instance->instancet = &megasas_instance_template_ppc;
2120 break;
Yang, Bo6610a6b2008-08-10 12:42:38 -07002121 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
2122 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
2123 instance->instancet = &megasas_instance_template_gen2;
2124 break;
Sumant Patrof9876f02006-02-03 15:34:35 -08002125 case PCI_DEVICE_ID_LSI_SAS1064R:
2126 case PCI_DEVICE_ID_DELL_PERC5:
2127 default:
2128 instance->instancet = &megasas_instance_template_xscale;
2129 break;
2130 }
Sumant Patro1341c932006-01-25 12:02:40 -08002131
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002132 /*
2133 * We expect the FW state to be READY
2134 */
Sumant Patro1341c932006-01-25 12:02:40 -08002135 if (megasas_transition_to_ready(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002136 goto fail_ready_state;
2137
2138 /*
2139 * Get various operational parameters from status register
2140 */
Sumant Patro1341c932006-01-25 12:02:40 -08002141 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07002142 /*
2143 * Reduce the max supported cmds by 1. This is to ensure that the
2144 * reply_q_sz (1 more than the max cmd that driver may send)
2145 * does not exceed max cmds that the FW can support
2146 */
2147 instance->max_fw_cmds = instance->max_fw_cmds-1;
Sumant Patro1341c932006-01-25 12:02:40 -08002148 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
2149 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002150 /*
2151 * Create a pool of commands
2152 */
2153 if (megasas_alloc_cmds(instance))
2154 goto fail_alloc_cmds;
2155
2156 /*
2157 * Allocate memory for reply queue. Length of reply queue should
2158 * be _one_ more than the maximum commands handled by the firmware.
2159 *
2160 * Note: When FW completes commands, it places corresponding contex
2161 * values in this circular reply queue. This circular queue is a fairly
2162 * typical producer-consumer queue. FW is the producer (of completed
2163 * commands) and the driver is the consumer.
2164 */
2165 context_sz = sizeof(u32);
2166 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
2167
2168 instance->reply_queue = pci_alloc_consistent(instance->pdev,
2169 reply_q_sz,
2170 &instance->reply_queue_h);
2171
2172 if (!instance->reply_queue) {
2173 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
2174 goto fail_reply_queue;
2175 }
2176
bo yang31ea7082007-11-07 12:09:50 -05002177 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002178 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002179
2180 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
2181
2182 /*
2183 * Compute the max allowed sectors per IO: The controller info has two
2184 * limits on max sectors. Driver should use the minimum of these two.
2185 *
2186 * 1 << stripe_sz_ops.min = max sectors per strip
2187 *
2188 * Note that older firmwares ( < FW ver 30) didn't report information
2189 * to calculate max_sectors_1. So the number ended up as zero always.
2190 */
bo yang14faea92007-11-09 04:14:00 -05002191 tmp_sectors = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002192 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
2193
2194 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
2195 ctrl_info->max_strips_per_io;
2196 max_sectors_2 = ctrl_info->max_request_size;
2197
bo yang14faea92007-11-09 04:14:00 -05002198 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
2199 }
2200
2201 instance->max_sectors_per_req = instance->max_num_sge *
2202 PAGE_SIZE / 512;
2203 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
2204 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002205
2206 kfree(ctrl_info);
2207
Sumant Patro5d018ad2006-10-03 13:13:18 -07002208 /*
2209 * Setup tasklet for cmd completion
2210 */
2211
bo yangad84db22007-11-09 04:40:16 -05002212 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2213 (unsigned long)instance);
2214
2215 /* Initialize the cmd completion timer */
2216 if (poll_mode_io)
2217 megasas_start_timer(instance, &instance->io_completion_timer,
2218 megasas_io_completion_timer,
2219 MEGASAS_COMPLETION_TIMER_INTERVAL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002220 return 0;
2221
2222 fail_fw_init:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002223
2224 pci_free_consistent(instance->pdev, reply_q_sz,
2225 instance->reply_queue, instance->reply_queue_h);
2226 fail_reply_queue:
2227 megasas_free_cmds(instance);
2228
2229 fail_alloc_cmds:
2230 fail_ready_state:
2231 iounmap(instance->reg_set);
2232
2233 fail_ioremap:
2234 pci_release_regions(instance->pdev);
2235
2236 return -EINVAL;
2237}
2238
2239/**
2240 * megasas_release_mfi - Reverses the FW initialization
2241 * @intance: Adapter soft state
2242 */
2243static void megasas_release_mfi(struct megasas_instance *instance)
2244{
2245 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
2246
2247 pci_free_consistent(instance->pdev, reply_q_sz,
2248 instance->reply_queue, instance->reply_queue_h);
2249
2250 megasas_free_cmds(instance);
2251
2252 iounmap(instance->reg_set);
2253
2254 pci_release_regions(instance->pdev);
2255}
2256
2257/**
2258 * megasas_get_seq_num - Gets latest event sequence numbers
2259 * @instance: Adapter soft state
2260 * @eli: FW event log sequence numbers information
2261 *
2262 * FW maintains a log of all events in a non-volatile area. Upper layers would
2263 * usually find out the latest sequence number of the events, the seq number at
2264 * the boot etc. They would "read" all the events below the latest seq number
2265 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
2266 * number), they would subsribe to AEN (asynchronous event notification) and
2267 * wait for the events to happen.
2268 */
2269static int
2270megasas_get_seq_num(struct megasas_instance *instance,
2271 struct megasas_evt_log_info *eli)
2272{
2273 struct megasas_cmd *cmd;
2274 struct megasas_dcmd_frame *dcmd;
2275 struct megasas_evt_log_info *el_info;
2276 dma_addr_t el_info_h = 0;
2277
2278 cmd = megasas_get_cmd(instance);
2279
2280 if (!cmd) {
2281 return -ENOMEM;
2282 }
2283
2284 dcmd = &cmd->frame->dcmd;
2285 el_info = pci_alloc_consistent(instance->pdev,
2286 sizeof(struct megasas_evt_log_info),
2287 &el_info_h);
2288
2289 if (!el_info) {
2290 megasas_return_cmd(instance, cmd);
2291 return -ENOMEM;
2292 }
2293
2294 memset(el_info, 0, sizeof(*el_info));
2295 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2296
2297 dcmd->cmd = MFI_CMD_DCMD;
2298 dcmd->cmd_status = 0x0;
2299 dcmd->sge_count = 1;
2300 dcmd->flags = MFI_FRAME_DIR_READ;
2301 dcmd->timeout = 0;
2302 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
2303 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
2304 dcmd->sgl.sge32[0].phys_addr = el_info_h;
2305 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
2306
2307 megasas_issue_blocked_cmd(instance, cmd);
2308
2309 /*
2310 * Copy the data back into callers buffer
2311 */
2312 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
2313
2314 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
2315 el_info, el_info_h);
2316
2317 megasas_return_cmd(instance, cmd);
2318
2319 return 0;
2320}
2321
2322/**
2323 * megasas_register_aen - Registers for asynchronous event notification
2324 * @instance: Adapter soft state
2325 * @seq_num: The starting sequence number
2326 * @class_locale: Class of the event
2327 *
2328 * This function subscribes for AEN for events beyond the @seq_num. It requests
2329 * to be notified if and only if the event is of type @class_locale
2330 */
2331static int
2332megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
2333 u32 class_locale_word)
2334{
2335 int ret_val;
2336 struct megasas_cmd *cmd;
2337 struct megasas_dcmd_frame *dcmd;
2338 union megasas_evt_class_locale curr_aen;
2339 union megasas_evt_class_locale prev_aen;
2340
2341 /*
2342 * If there an AEN pending already (aen_cmd), check if the
2343 * class_locale of that pending AEN is inclusive of the new
2344 * AEN request we currently have. If it is, then we don't have
2345 * to do anything. In other words, whichever events the current
2346 * AEN request is subscribing to, have already been subscribed
2347 * to.
2348 *
2349 * If the old_cmd is _not_ inclusive, then we have to abort
2350 * that command, form a class_locale that is superset of both
2351 * old and current and re-issue to the FW
2352 */
2353
2354 curr_aen.word = class_locale_word;
2355
2356 if (instance->aen_cmd) {
2357
2358 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
2359
2360 /*
2361 * A class whose enum value is smaller is inclusive of all
2362 * higher values. If a PROGRESS (= -1) was previously
2363 * registered, then a new registration requests for higher
2364 * classes need not be sent to FW. They are automatically
2365 * included.
2366 *
2367 * Locale numbers don't have such hierarchy. They are bitmap
2368 * values
2369 */
2370 if ((prev_aen.members.class <= curr_aen.members.class) &&
2371 !((prev_aen.members.locale & curr_aen.members.locale) ^
2372 curr_aen.members.locale)) {
2373 /*
2374 * Previously issued event registration includes
2375 * current request. Nothing to do.
2376 */
2377 return 0;
2378 } else {
2379 curr_aen.members.locale |= prev_aen.members.locale;
2380
2381 if (prev_aen.members.class < curr_aen.members.class)
2382 curr_aen.members.class = prev_aen.members.class;
2383
2384 instance->aen_cmd->abort_aen = 1;
2385 ret_val = megasas_issue_blocked_abort_cmd(instance,
2386 instance->
2387 aen_cmd);
2388
2389 if (ret_val) {
2390 printk(KERN_DEBUG "megasas: Failed to abort "
2391 "previous AEN command\n");
2392 return ret_val;
2393 }
2394 }
2395 }
2396
2397 cmd = megasas_get_cmd(instance);
2398
2399 if (!cmd)
2400 return -ENOMEM;
2401
2402 dcmd = &cmd->frame->dcmd;
2403
2404 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
2405
2406 /*
2407 * Prepare DCMD for aen registration
2408 */
2409 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2410
2411 dcmd->cmd = MFI_CMD_DCMD;
2412 dcmd->cmd_status = 0x0;
2413 dcmd->sge_count = 1;
2414 dcmd->flags = MFI_FRAME_DIR_READ;
2415 dcmd->timeout = 0;
2416 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
2417 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
2418 dcmd->mbox.w[0] = seq_num;
2419 dcmd->mbox.w[1] = curr_aen.word;
2420 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
2421 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
2422
2423 /*
2424 * Store reference to the cmd used to register for AEN. When an
2425 * application wants us to register for AEN, we have to abort this
2426 * cmd and re-register with a new EVENT LOCALE supplied by that app
2427 */
2428 instance->aen_cmd = cmd;
2429
2430 /*
2431 * Issue the aen registration frame
2432 */
Sumant Patro1341c932006-01-25 12:02:40 -08002433 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002434
2435 return 0;
2436}
2437
2438/**
2439 * megasas_start_aen - Subscribes to AEN during driver load time
2440 * @instance: Adapter soft state
2441 */
2442static int megasas_start_aen(struct megasas_instance *instance)
2443{
2444 struct megasas_evt_log_info eli;
2445 union megasas_evt_class_locale class_locale;
2446
2447 /*
2448 * Get the latest sequence number from FW
2449 */
2450 memset(&eli, 0, sizeof(eli));
2451
2452 if (megasas_get_seq_num(instance, &eli))
2453 return -1;
2454
2455 /*
2456 * Register AEN with FW for latest sequence number plus 1
2457 */
2458 class_locale.members.reserved = 0;
2459 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2460 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2461
2462 return megasas_register_aen(instance, eli.newest_seq_num + 1,
2463 class_locale.word);
2464}
2465
2466/**
2467 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2468 * @instance: Adapter soft state
2469 */
2470static int megasas_io_attach(struct megasas_instance *instance)
2471{
2472 struct Scsi_Host *host = instance->host;
2473
2474 /*
2475 * Export parameters required by SCSI mid-layer
2476 */
2477 host->irq = instance->pdev->irq;
2478 host->unique_id = instance->unique_id;
2479 host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
2480 host->this_id = instance->init_id;
2481 host->sg_tablesize = instance->max_num_sge;
2482 host->max_sectors = instance->max_sectors_per_req;
2483 host->cmd_per_lun = 128;
2484 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
2485 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
2486 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08002487 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002488
2489 /*
2490 * Notify the mid-layer about the new controller
2491 */
2492 if (scsi_add_host(host, &instance->pdev->dev)) {
2493 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2494 return -ENODEV;
2495 }
2496
2497 /*
2498 * Trigger SCSI to scan our drives
2499 */
2500 scsi_scan_host(host);
2501 return 0;
2502}
2503
bo yang31ea7082007-11-07 12:09:50 -05002504static int
2505megasas_set_dma_mask(struct pci_dev *pdev)
2506{
2507 /*
2508 * All our contollers are capable of performing 64-bit DMA
2509 */
2510 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07002511 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05002512
Yang Hongyang284901a2009-04-06 19:01:15 -07002513 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05002514 goto fail_set_dma_mask;
2515 }
2516 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07002517 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05002518 goto fail_set_dma_mask;
2519 }
2520 return 0;
2521
2522fail_set_dma_mask:
2523 return 1;
2524}
2525
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002526/**
2527 * megasas_probe_one - PCI hotplug entry point
2528 * @pdev: PCI device structure
2529 * @id: PCI ids of supported hotplugged adapter
2530 */
2531static int __devinit
2532megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2533{
2534 int rval;
2535 struct Scsi_Host *host;
2536 struct megasas_instance *instance;
2537
2538 /*
2539 * Announce PCI information
2540 */
2541 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2542 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2543 pdev->subsystem_device);
2544
2545 printk("bus %d:slot %d:func %d\n",
2546 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2547
2548 /*
2549 * PCI prepping: enable device set bus mastering and dma mask
2550 */
2551 rval = pci_enable_device(pdev);
2552
2553 if (rval) {
2554 return rval;
2555 }
2556
2557 pci_set_master(pdev);
2558
bo yang31ea7082007-11-07 12:09:50 -05002559 if (megasas_set_dma_mask(pdev))
2560 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002561
2562 host = scsi_host_alloc(&megasas_template,
2563 sizeof(struct megasas_instance));
2564
2565 if (!host) {
2566 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2567 goto fail_alloc_instance;
2568 }
2569
2570 instance = (struct megasas_instance *)host->hostdata;
2571 memset(instance, 0, sizeof(*instance));
2572
2573 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2574 &instance->producer_h);
2575 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2576 &instance->consumer_h);
2577
2578 if (!instance->producer || !instance->consumer) {
2579 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2580 "producer, consumer\n");
2581 goto fail_alloc_dma_buf;
2582 }
2583
2584 *instance->producer = 0;
2585 *instance->consumer = 0;
2586
2587 instance->evt_detail = pci_alloc_consistent(pdev,
2588 sizeof(struct
2589 megasas_evt_detail),
2590 &instance->evt_detail_h);
2591
2592 if (!instance->evt_detail) {
2593 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2594 "event detail structure\n");
2595 goto fail_alloc_dma_buf;
2596 }
2597
2598 /*
2599 * Initialize locks and queues
2600 */
2601 INIT_LIST_HEAD(&instance->cmd_pool);
2602
Sumant Patroe4a082c2006-05-30 12:03:37 -07002603 atomic_set(&instance->fw_outstanding,0);
2604
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002605 init_waitqueue_head(&instance->int_cmd_wait_q);
2606 init_waitqueue_head(&instance->abort_cmd_wait_q);
2607
2608 spin_lock_init(&instance->cmd_pool_lock);
bo yang7343eb62007-11-09 04:35:44 -05002609 spin_lock_init(&instance->completion_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002610
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02002611 mutex_init(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002612 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2613
2614 /*
2615 * Initialize PCI related and misc parameters
2616 */
2617 instance->pdev = pdev;
2618 instance->host = host;
2619 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2620 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2621
Sumant Patro658dced2006-10-03 13:09:14 -07002622 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002623 instance->flag = 0;
2624 instance->last_time = 0;
Sumant Patro658dced2006-10-03 13:09:14 -07002625
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002626 /*
2627 * Initialize MFI Firmware
2628 */
2629 if (megasas_init_mfi(instance))
2630 goto fail_init_mfi;
2631
2632 /*
2633 * Register IRQ
2634 */
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07002635 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002636 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2637 goto fail_irq;
2638 }
2639
Sumant Patro1341c932006-01-25 12:02:40 -08002640 instance->instancet->enable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002641
2642 /*
2643 * Store instance in PCI softstate
2644 */
2645 pci_set_drvdata(pdev, instance);
2646
2647 /*
2648 * Add this controller to megasas_mgmt_info structure so that it
2649 * can be exported to management applications
2650 */
2651 megasas_mgmt_info.count++;
2652 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2653 megasas_mgmt_info.max_index++;
2654
2655 /*
2656 * Initiate AEN (Asynchronous Event Notification)
2657 */
2658 if (megasas_start_aen(instance)) {
2659 printk(KERN_DEBUG "megasas: start aen failed\n");
2660 goto fail_start_aen;
2661 }
2662
2663 /*
2664 * Register with SCSI mid-layer
2665 */
2666 if (megasas_io_attach(instance))
2667 goto fail_io_attach;
2668
2669 return 0;
2670
2671 fail_start_aen:
2672 fail_io_attach:
2673 megasas_mgmt_info.count--;
2674 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2675 megasas_mgmt_info.max_index--;
2676
2677 pci_set_drvdata(pdev, NULL);
Sumant Patrob274cab2006-10-03 12:52:12 -07002678 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002679 free_irq(instance->pdev->irq, instance);
2680
2681 megasas_release_mfi(instance);
2682
2683 fail_irq:
2684 fail_init_mfi:
2685 fail_alloc_dma_buf:
2686 if (instance->evt_detail)
2687 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2688 instance->evt_detail,
2689 instance->evt_detail_h);
2690
2691 if (instance->producer)
2692 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2693 instance->producer_h);
2694 if (instance->consumer)
2695 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2696 instance->consumer_h);
2697 scsi_host_put(host);
2698
2699 fail_alloc_instance:
2700 fail_set_dma_mask:
2701 pci_disable_device(pdev);
2702
2703 return -ENODEV;
2704}
2705
2706/**
2707 * megasas_flush_cache - Requests FW to flush all its caches
2708 * @instance: Adapter soft state
2709 */
2710static void megasas_flush_cache(struct megasas_instance *instance)
2711{
2712 struct megasas_cmd *cmd;
2713 struct megasas_dcmd_frame *dcmd;
2714
2715 cmd = megasas_get_cmd(instance);
2716
2717 if (!cmd)
2718 return;
2719
2720 dcmd = &cmd->frame->dcmd;
2721
2722 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2723
2724 dcmd->cmd = MFI_CMD_DCMD;
2725 dcmd->cmd_status = 0x0;
2726 dcmd->sge_count = 0;
2727 dcmd->flags = MFI_FRAME_DIR_NONE;
2728 dcmd->timeout = 0;
2729 dcmd->data_xfer_len = 0;
2730 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
2731 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
2732
2733 megasas_issue_blocked_cmd(instance, cmd);
2734
2735 megasas_return_cmd(instance, cmd);
2736
2737 return;
2738}
2739
2740/**
2741 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2742 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05002743 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002744 */
bo yang31ea7082007-11-07 12:09:50 -05002745static void megasas_shutdown_controller(struct megasas_instance *instance,
2746 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002747{
2748 struct megasas_cmd *cmd;
2749 struct megasas_dcmd_frame *dcmd;
2750
2751 cmd = megasas_get_cmd(instance);
2752
2753 if (!cmd)
2754 return;
2755
2756 if (instance->aen_cmd)
2757 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
2758
2759 dcmd = &cmd->frame->dcmd;
2760
2761 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2762
2763 dcmd->cmd = MFI_CMD_DCMD;
2764 dcmd->cmd_status = 0x0;
2765 dcmd->sge_count = 0;
2766 dcmd->flags = MFI_FRAME_DIR_NONE;
2767 dcmd->timeout = 0;
2768 dcmd->data_xfer_len = 0;
bo yang31ea7082007-11-07 12:09:50 -05002769 dcmd->opcode = opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002770
2771 megasas_issue_blocked_cmd(instance, cmd);
2772
2773 megasas_return_cmd(instance, cmd);
2774
2775 return;
2776}
2777
Jiri Slaby33139b22008-05-01 17:56:02 +02002778#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002779/**
bo yangad84db22007-11-09 04:40:16 -05002780 * megasas_suspend - driver suspend entry point
2781 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05002782 * @state: PCI power state to suspend routine
2783 */
Jiri Slaby33139b22008-05-01 17:56:02 +02002784static int
bo yang31ea7082007-11-07 12:09:50 -05002785megasas_suspend(struct pci_dev *pdev, pm_message_t state)
2786{
2787 struct Scsi_Host *host;
2788 struct megasas_instance *instance;
2789
2790 instance = pci_get_drvdata(pdev);
2791 host = instance->host;
2792
bo yangad84db22007-11-09 04:40:16 -05002793 if (poll_mode_io)
2794 del_timer_sync(&instance->io_completion_timer);
2795
bo yang31ea7082007-11-07 12:09:50 -05002796 megasas_flush_cache(instance);
2797 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
2798 tasklet_kill(&instance->isr_tasklet);
2799
2800 pci_set_drvdata(instance->pdev, instance);
2801 instance->instancet->disable_intr(instance->reg_set);
2802 free_irq(instance->pdev->irq, instance);
2803
2804 pci_save_state(pdev);
2805 pci_disable_device(pdev);
2806
2807 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2808
2809 return 0;
2810}
2811
2812/**
2813 * megasas_resume- driver resume entry point
2814 * @pdev: PCI device structure
2815 */
Jiri Slaby33139b22008-05-01 17:56:02 +02002816static int
bo yang31ea7082007-11-07 12:09:50 -05002817megasas_resume(struct pci_dev *pdev)
2818{
2819 int rval;
2820 struct Scsi_Host *host;
2821 struct megasas_instance *instance;
2822
2823 instance = pci_get_drvdata(pdev);
2824 host = instance->host;
2825 pci_set_power_state(pdev, PCI_D0);
2826 pci_enable_wake(pdev, PCI_D0, 0);
2827 pci_restore_state(pdev);
2828
2829 /*
2830 * PCI prepping: enable device set bus mastering and dma mask
2831 */
2832 rval = pci_enable_device(pdev);
2833
2834 if (rval) {
2835 printk(KERN_ERR "megasas: Enable device failed\n");
2836 return rval;
2837 }
2838
2839 pci_set_master(pdev);
2840
2841 if (megasas_set_dma_mask(pdev))
2842 goto fail_set_dma_mask;
2843
2844 /*
2845 * Initialize MFI Firmware
2846 */
2847
2848 *instance->producer = 0;
2849 *instance->consumer = 0;
2850
2851 atomic_set(&instance->fw_outstanding, 0);
2852
2853 /*
2854 * We expect the FW state to be READY
2855 */
2856 if (megasas_transition_to_ready(instance))
2857 goto fail_ready_state;
2858
2859 if (megasas_issue_init_mfi(instance))
2860 goto fail_init_mfi;
2861
2862 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2863 (unsigned long)instance);
2864
2865 /*
2866 * Register IRQ
2867 */
2868 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED,
2869 "megasas", instance)) {
2870 printk(KERN_ERR "megasas: Failed to register IRQ\n");
2871 goto fail_irq;
2872 }
2873
2874 instance->instancet->enable_intr(instance->reg_set);
2875
2876 /*
2877 * Initiate AEN (Asynchronous Event Notification)
2878 */
2879 if (megasas_start_aen(instance))
2880 printk(KERN_ERR "megasas: Start AEN failed\n");
2881
bo yangad84db22007-11-09 04:40:16 -05002882 /* Initialize the cmd completion timer */
2883 if (poll_mode_io)
2884 megasas_start_timer(instance, &instance->io_completion_timer,
2885 megasas_io_completion_timer,
2886 MEGASAS_COMPLETION_TIMER_INTERVAL);
bo yang31ea7082007-11-07 12:09:50 -05002887 return 0;
2888
2889fail_irq:
2890fail_init_mfi:
2891 if (instance->evt_detail)
2892 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2893 instance->evt_detail,
2894 instance->evt_detail_h);
2895
2896 if (instance->producer)
2897 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2898 instance->producer_h);
2899 if (instance->consumer)
2900 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2901 instance->consumer_h);
2902 scsi_host_put(host);
2903
2904fail_set_dma_mask:
2905fail_ready_state:
2906
2907 pci_disable_device(pdev);
2908
2909 return -ENODEV;
2910}
Jiri Slaby33139b22008-05-01 17:56:02 +02002911#else
2912#define megasas_suspend NULL
2913#define megasas_resume NULL
2914#endif
bo yang31ea7082007-11-07 12:09:50 -05002915
2916/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002917 * megasas_detach_one - PCI hot"un"plug entry point
2918 * @pdev: PCI device structure
2919 */
Jiri Slaby33139b22008-05-01 17:56:02 +02002920static void __devexit megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002921{
2922 int i;
2923 struct Scsi_Host *host;
2924 struct megasas_instance *instance;
2925
2926 instance = pci_get_drvdata(pdev);
2927 host = instance->host;
2928
bo yangad84db22007-11-09 04:40:16 -05002929 if (poll_mode_io)
2930 del_timer_sync(&instance->io_completion_timer);
2931
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002932 scsi_remove_host(instance->host);
2933 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05002934 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Sumant Patro5d018ad2006-10-03 13:13:18 -07002935 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002936
2937 /*
2938 * Take the instance off the instance array. Note that we will not
2939 * decrement the max_index. We let this array be sparse array
2940 */
2941 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2942 if (megasas_mgmt_info.instance[i] == instance) {
2943 megasas_mgmt_info.count--;
2944 megasas_mgmt_info.instance[i] = NULL;
2945
2946 break;
2947 }
2948 }
2949
2950 pci_set_drvdata(instance->pdev, NULL);
2951
Sumant Patrob274cab2006-10-03 12:52:12 -07002952 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002953
2954 free_irq(instance->pdev->irq, instance);
2955
2956 megasas_release_mfi(instance);
2957
2958 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2959 instance->evt_detail, instance->evt_detail_h);
2960
2961 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2962 instance->producer_h);
2963
2964 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2965 instance->consumer_h);
2966
2967 scsi_host_put(host);
2968
2969 pci_set_drvdata(pdev, NULL);
2970
2971 pci_disable_device(pdev);
2972
2973 return;
2974}
2975
2976/**
2977 * megasas_shutdown - Shutdown entry point
2978 * @device: Generic device structure
2979 */
2980static void megasas_shutdown(struct pci_dev *pdev)
2981{
2982 struct megasas_instance *instance = pci_get_drvdata(pdev);
2983 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07002984 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002985}
2986
2987/**
2988 * megasas_mgmt_open - char node "open" entry point
2989 */
2990static int megasas_mgmt_open(struct inode *inode, struct file *filep)
2991{
Jonathan Corbetf2b98572008-05-18 15:32:43 -06002992 cycle_kernel_lock();
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002993 /*
2994 * Allow only those users with admin rights
2995 */
2996 if (!capable(CAP_SYS_ADMIN))
2997 return -EACCES;
2998
2999 return 0;
3000}
3001
3002/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003003 * megasas_mgmt_fasync - Async notifier registration from applications
3004 *
3005 * This function adds the calling process to a driver global queue. When an
3006 * event occurs, SIGIO will be sent to all processes in this queue.
3007 */
3008static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
3009{
3010 int rc;
3011
Arjan van de Ven0b950672006-01-11 13:16:10 +01003012 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003013
3014 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
3015
Arjan van de Ven0b950672006-01-11 13:16:10 +01003016 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003017
3018 if (rc >= 0) {
3019 /* For sanity check when we get ioctl */
3020 filep->private_data = filep;
3021 return 0;
3022 }
3023
3024 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
3025
3026 return rc;
3027}
3028
3029/**
3030 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
3031 * @instance: Adapter soft state
3032 * @argp: User's ioctl packet
3033 */
3034static int
3035megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
3036 struct megasas_iocpacket __user * user_ioc,
3037 struct megasas_iocpacket *ioc)
3038{
3039 struct megasas_sge32 *kern_sge32;
3040 struct megasas_cmd *cmd;
3041 void *kbuff_arr[MAX_IOCTL_SGE];
3042 dma_addr_t buf_handle = 0;
3043 int error = 0, i;
3044 void *sense = NULL;
3045 dma_addr_t sense_handle;
3046 u32 *sense_ptr;
3047
3048 memset(kbuff_arr, 0, sizeof(kbuff_arr));
3049
3050 if (ioc->sge_count > MAX_IOCTL_SGE) {
3051 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
3052 ioc->sge_count, MAX_IOCTL_SGE);
3053 return -EINVAL;
3054 }
3055
3056 cmd = megasas_get_cmd(instance);
3057 if (!cmd) {
3058 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
3059 return -ENOMEM;
3060 }
3061
3062 /*
3063 * User's IOCTL packet has 2 frames (maximum). Copy those two
3064 * frames into our cmd's frames. cmd->frame's context will get
3065 * overwritten when we copy from user's frames. So set that value
3066 * alone separately
3067 */
3068 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
3069 cmd->frame->hdr.context = cmd->index;
3070
3071 /*
3072 * The management interface between applications and the fw uses
3073 * MFI frames. E.g, RAID configuration changes, LD property changes
3074 * etc are accomplishes through different kinds of MFI frames. The
3075 * driver needs to care only about substituting user buffers with
3076 * kernel buffers in SGLs. The location of SGL is embedded in the
3077 * struct iocpacket itself.
3078 */
3079 kern_sge32 = (struct megasas_sge32 *)
3080 ((unsigned long)cmd->frame + ioc->sgl_off);
3081
3082 /*
3083 * For each user buffer, create a mirror buffer and copy in
3084 */
3085 for (i = 0; i < ioc->sge_count; i++) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08003086 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003087 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08003088 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003089 if (!kbuff_arr[i]) {
3090 printk(KERN_DEBUG "megasas: Failed to alloc "
3091 "kernel SGL buffer for IOCTL \n");
3092 error = -ENOMEM;
3093 goto out;
3094 }
3095
3096 /*
3097 * We don't change the dma_coherent_mask, so
3098 * pci_alloc_consistent only returns 32bit addresses
3099 */
3100 kern_sge32[i].phys_addr = (u32) buf_handle;
3101 kern_sge32[i].length = ioc->sgl[i].iov_len;
3102
3103 /*
3104 * We created a kernel buffer corresponding to the
3105 * user buffer. Now copy in from the user buffer
3106 */
3107 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
3108 (u32) (ioc->sgl[i].iov_len))) {
3109 error = -EFAULT;
3110 goto out;
3111 }
3112 }
3113
3114 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08003115 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
3116 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003117 if (!sense) {
3118 error = -ENOMEM;
3119 goto out;
3120 }
3121
3122 sense_ptr =
3123 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
3124 *sense_ptr = sense_handle;
3125 }
3126
3127 /*
3128 * Set the sync_cmd flag so that the ISR knows not to complete this
3129 * cmd to the SCSI mid-layer
3130 */
3131 cmd->sync_cmd = 1;
3132 megasas_issue_blocked_cmd(instance, cmd);
3133 cmd->sync_cmd = 0;
3134
3135 /*
3136 * copy out the kernel buffers to user buffers
3137 */
3138 for (i = 0; i < ioc->sge_count; i++) {
3139 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
3140 ioc->sgl[i].iov_len)) {
3141 error = -EFAULT;
3142 goto out;
3143 }
3144 }
3145
3146 /*
3147 * copy out the sense
3148 */
3149 if (ioc->sense_len) {
3150 /*
bo yangb70a41e2008-03-18 03:13:06 -04003151 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003152 * sense buffer address
3153 */
bo yangb70a41e2008-03-18 03:13:06 -04003154 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
3155 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003156
bo yangb70a41e2008-03-18 03:13:06 -04003157 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
3158 sense, ioc->sense_len)) {
bo yangb10c36a2007-11-09 04:28:47 -05003159 printk(KERN_ERR "megasas: Failed to copy out to user "
3160 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003161 error = -EFAULT;
3162 goto out;
3163 }
3164 }
3165
3166 /*
3167 * copy the status codes returned by the fw
3168 */
3169 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
3170 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
3171 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
3172 error = -EFAULT;
3173 }
3174
3175 out:
3176 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08003177 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003178 sense, sense_handle);
3179 }
3180
3181 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08003182 dma_free_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003183 kern_sge32[i].length,
3184 kbuff_arr[i], kern_sge32[i].phys_addr);
3185 }
3186
3187 megasas_return_cmd(instance, cmd);
3188 return error;
3189}
3190
3191static struct megasas_instance *megasas_lookup_instance(u16 host_no)
3192{
3193 int i;
3194
3195 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3196
3197 if ((megasas_mgmt_info.instance[i]) &&
3198 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
3199 return megasas_mgmt_info.instance[i];
3200 }
3201
3202 return NULL;
3203}
3204
3205static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
3206{
3207 struct megasas_iocpacket __user *user_ioc =
3208 (struct megasas_iocpacket __user *)arg;
3209 struct megasas_iocpacket *ioc;
3210 struct megasas_instance *instance;
3211 int error;
3212
3213 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
3214 if (!ioc)
3215 return -ENOMEM;
3216
3217 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
3218 error = -EFAULT;
3219 goto out_kfree_ioc;
3220 }
3221
3222 instance = megasas_lookup_instance(ioc->host_no);
3223 if (!instance) {
3224 error = -ENODEV;
3225 goto out_kfree_ioc;
3226 }
3227
3228 /*
3229 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
3230 */
3231 if (down_interruptible(&instance->ioctl_sem)) {
3232 error = -ERESTARTSYS;
3233 goto out_kfree_ioc;
3234 }
3235 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
3236 up(&instance->ioctl_sem);
3237
3238 out_kfree_ioc:
3239 kfree(ioc);
3240 return error;
3241}
3242
3243static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
3244{
3245 struct megasas_instance *instance;
3246 struct megasas_aen aen;
3247 int error;
3248
3249 if (file->private_data != file) {
3250 printk(KERN_DEBUG "megasas: fasync_helper was not "
3251 "called first\n");
3252 return -EINVAL;
3253 }
3254
3255 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
3256 return -EFAULT;
3257
3258 instance = megasas_lookup_instance(aen.host_no);
3259
3260 if (!instance)
3261 return -ENODEV;
3262
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02003263 mutex_lock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003264 error = megasas_register_aen(instance, aen.seq_num,
3265 aen.class_locale_word);
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02003266 mutex_unlock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003267 return error;
3268}
3269
3270/**
3271 * megasas_mgmt_ioctl - char node ioctl entry point
3272 */
3273static long
3274megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3275{
3276 switch (cmd) {
3277 case MEGASAS_IOC_FIRMWARE:
3278 return megasas_mgmt_ioctl_fw(file, arg);
3279
3280 case MEGASAS_IOC_GET_AEN:
3281 return megasas_mgmt_ioctl_aen(file, arg);
3282 }
3283
3284 return -ENOTTY;
3285}
3286
3287#ifdef CONFIG_COMPAT
3288static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
3289{
3290 struct compat_megasas_iocpacket __user *cioc =
3291 (struct compat_megasas_iocpacket __user *)arg;
3292 struct megasas_iocpacket __user *ioc =
3293 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
3294 int i;
3295 int error = 0;
3296
Jeff Garzik83aabc12006-10-04 06:34:03 -04003297 if (clear_user(ioc, sizeof(*ioc)))
3298 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003299
3300 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
3301 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
3302 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
3303 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
3304 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
3305 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
3306 return -EFAULT;
3307
3308 for (i = 0; i < MAX_IOCTL_SGE; i++) {
3309 compat_uptr_t ptr;
3310
3311 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
3312 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
3313 copy_in_user(&ioc->sgl[i].iov_len,
3314 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
3315 return -EFAULT;
3316 }
3317
3318 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
3319
3320 if (copy_in_user(&cioc->frame.hdr.cmd_status,
3321 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
3322 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
3323 return -EFAULT;
3324 }
3325 return error;
3326}
3327
3328static long
3329megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
3330 unsigned long arg)
3331{
3332 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08003333 case MEGASAS_IOC_FIRMWARE32:
3334 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003335 case MEGASAS_IOC_GET_AEN:
3336 return megasas_mgmt_ioctl_aen(file, arg);
3337 }
3338
3339 return -ENOTTY;
3340}
3341#endif
3342
3343/*
3344 * File operations structure for management interface
3345 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08003346static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003347 .owner = THIS_MODULE,
3348 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003349 .fasync = megasas_mgmt_fasync,
3350 .unlocked_ioctl = megasas_mgmt_ioctl,
3351#ifdef CONFIG_COMPAT
3352 .compat_ioctl = megasas_mgmt_compat_ioctl,
3353#endif
3354};
3355
3356/*
3357 * PCI hotplug support registration structure
3358 */
3359static struct pci_driver megasas_pci_driver = {
3360
3361 .name = "megaraid_sas",
3362 .id_table = megasas_pci_table,
3363 .probe = megasas_probe_one,
3364 .remove = __devexit_p(megasas_detach_one),
bo yang31ea7082007-11-07 12:09:50 -05003365 .suspend = megasas_suspend,
3366 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003367 .shutdown = megasas_shutdown,
3368};
3369
3370/*
3371 * Sysfs driver attributes
3372 */
3373static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
3374{
3375 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
3376 MEGASAS_VERSION);
3377}
3378
3379static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
3380
3381static ssize_t
3382megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
3383{
3384 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
3385 MEGASAS_RELDATE);
3386}
3387
3388static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
3389 NULL);
3390
Sumant Patro658dced2006-10-03 13:09:14 -07003391static ssize_t
3392megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
3393{
bo yangad84db22007-11-09 04:40:16 -05003394 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07003395}
3396
3397static ssize_t
3398megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
3399{
3400 int retval = count;
3401 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
3402 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
3403 retval = -EINVAL;
3404 }
3405 return retval;
3406}
3407
Joe Malicki66dca9b2008-08-14 17:14:48 -04003408static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05003409 megasas_sysfs_set_dbg_lvl);
3410
3411static ssize_t
3412megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
3413{
3414 return sprintf(buf, "%u\n", poll_mode_io);
3415}
3416
3417static ssize_t
3418megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
3419 const char *buf, size_t count)
3420{
3421 int retval = count;
3422 int tmp = poll_mode_io;
3423 int i;
3424 struct megasas_instance *instance;
3425
3426 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
3427 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
3428 retval = -EINVAL;
3429 }
3430
3431 /*
3432 * Check if poll_mode_io is already set or is same as previous value
3433 */
3434 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
3435 goto out;
3436
3437 if (poll_mode_io) {
3438 /*
3439 * Start timers for all adapters
3440 */
3441 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3442 instance = megasas_mgmt_info.instance[i];
3443 if (instance) {
3444 megasas_start_timer(instance,
3445 &instance->io_completion_timer,
3446 megasas_io_completion_timer,
3447 MEGASAS_COMPLETION_TIMER_INTERVAL);
3448 }
3449 }
3450 } else {
3451 /*
3452 * Delete timers for all adapters
3453 */
3454 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3455 instance = megasas_mgmt_info.instance[i];
3456 if (instance)
3457 del_timer_sync(&instance->io_completion_timer);
3458 }
3459 }
3460
3461out:
3462 return retval;
3463}
3464
3465static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUGO,
3466 megasas_sysfs_show_poll_mode_io,
3467 megasas_sysfs_set_poll_mode_io);
Sumant Patro658dced2006-10-03 13:09:14 -07003468
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003469/**
3470 * megasas_init - Driver load entry point
3471 */
3472static int __init megasas_init(void)
3473{
3474 int rval;
3475
3476 /*
3477 * Announce driver version and other information
3478 */
3479 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
3480 MEGASAS_EXT_VERSION);
3481
3482 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
3483
3484 /*
3485 * Register character device node
3486 */
3487 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
3488
3489 if (rval < 0) {
3490 printk(KERN_DEBUG "megasas: failed to open device node\n");
3491 return rval;
3492 }
3493
3494 megasas_mgmt_majorno = rval;
3495
3496 /*
3497 * Register ourselves as PCI hotplug module
3498 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00003499 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003500
3501 if (rval) {
3502 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04003503 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003504 }
3505
Jeff Garzik83aabc12006-10-04 06:34:03 -04003506 rval = driver_create_file(&megasas_pci_driver.driver,
3507 &driver_attr_version);
3508 if (rval)
3509 goto err_dcf_attr_ver;
3510 rval = driver_create_file(&megasas_pci_driver.driver,
3511 &driver_attr_release_date);
3512 if (rval)
3513 goto err_dcf_rel_date;
3514 rval = driver_create_file(&megasas_pci_driver.driver,
3515 &driver_attr_dbg_lvl);
3516 if (rval)
3517 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05003518 rval = driver_create_file(&megasas_pci_driver.driver,
3519 &driver_attr_poll_mode_io);
3520 if (rval)
3521 goto err_dcf_poll_mode_io;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003522
3523 return rval;
bo yangad84db22007-11-09 04:40:16 -05003524
3525err_dcf_poll_mode_io:
3526 driver_remove_file(&megasas_pci_driver.driver,
3527 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04003528err_dcf_dbg_lvl:
3529 driver_remove_file(&megasas_pci_driver.driver,
3530 &driver_attr_release_date);
3531err_dcf_rel_date:
3532 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3533err_dcf_attr_ver:
3534 pci_unregister_driver(&megasas_pci_driver);
3535err_pcidrv:
3536 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3537 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003538}
3539
3540/**
3541 * megasas_exit - Driver unload entry point
3542 */
3543static void __exit megasas_exit(void)
3544{
Sumant Patro658dced2006-10-03 13:09:14 -07003545 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05003546 &driver_attr_poll_mode_io);
3547 driver_remove_file(&megasas_pci_driver.driver,
Sumant Patro658dced2006-10-03 13:09:14 -07003548 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04003549 driver_remove_file(&megasas_pci_driver.driver,
3550 &driver_attr_release_date);
3551 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003552
3553 pci_unregister_driver(&megasas_pci_driver);
3554 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3555}
3556
3557module_init(megasas_init);
3558module_exit(megasas_exit);