blob: 6d998e050338412e686e403129f4f264cf1d9432 [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>
Yang, Boc3518832009-10-06 14:18:02 -060043#include <linux/poll.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040044
45#include <scsi/scsi.h>
46#include <scsi/scsi_cmnd.h>
47#include <scsi/scsi_device.h>
48#include <scsi/scsi_host.h>
49#include "megaraid_sas.h"
50
bo yangad84db22007-11-09 04:40:16 -050051/*
52 * poll_mode_io:1- schedule complete completion from q cmd
53 */
54static unsigned int poll_mode_io;
55module_param_named(poll_mode_io, poll_mode_io, int, 0);
56MODULE_PARM_DESC(poll_mode_io,
57 "Complete cmds from IO path, (default=0)");
58
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040059MODULE_LICENSE("GPL");
60MODULE_VERSION(MEGASAS_VERSION);
Sumant Patro3d6d1742006-12-29 08:13:54 -080061MODULE_AUTHOR("megaraidlinux@lsi.com");
bo yangf28cd7c2007-11-09 04:44:56 -050062MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040063
64/*
65 * PCI ID table for all supported controllers
66 */
67static struct pci_device_id megasas_pci_table[] = {
68
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +020069 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
70 /* xscale IOP */
71 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
72 /* ppc IOP */
bo yangaf7a5642008-03-17 04:13:07 -040073 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
74 /* ppc IOP */
Yang, Bo6610a6b2008-08-10 12:42:38 -070075 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
76 /* gen2*/
77 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
78 /* gen2*/
Yang, Bo87911122009-10-06 14:31:54 -060079 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
80 /* skinny*/
81 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
82 /* skinny*/
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +020083 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
84 /* xscale IOP, vega */
85 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
86 /* xscale IOP */
87 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040088};
89
90MODULE_DEVICE_TABLE(pci, megasas_pci_table);
91
92static int megasas_mgmt_majorno;
93static struct megasas_mgmt_info megasas_mgmt_info;
94static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +010095static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040096
Yang, Boc3518832009-10-06 14:18:02 -060097static int megasas_poll_wait_aen;
98static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -060099static u32 support_poll_for_event;
Sumant Patro658dced2006-10-03 13:09:14 -0700100static u32 megasas_dbg_lvl;
101
Yang, Boc3518832009-10-06 14:18:02 -0600102/* define lock for aen poll */
103spinlock_t poll_aen_lock;
104
bo yang7343eb62007-11-09 04:35:44 -0500105static void
106megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
107 u8 alt_status);
108
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400109/**
110 * megasas_get_cmd - Get a command from the free pool
111 * @instance: Adapter soft state
112 *
113 * Returns a free command from the pool
114 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800115static struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400116 *instance)
117{
118 unsigned long flags;
119 struct megasas_cmd *cmd = NULL;
120
121 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
122
123 if (!list_empty(&instance->cmd_pool)) {
124 cmd = list_entry((&instance->cmd_pool)->next,
125 struct megasas_cmd, list);
126 list_del_init(&cmd->list);
127 } else {
128 printk(KERN_ERR "megasas: Command pool empty!\n");
129 }
130
131 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
132 return cmd;
133}
134
135/**
136 * megasas_return_cmd - Return a cmd to free command pool
137 * @instance: Adapter soft state
138 * @cmd: Command packet to be returned to free command pool
139 */
140static inline void
141megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
142{
143 unsigned long flags;
144
145 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
146
147 cmd->scmd = NULL;
148 list_add_tail(&cmd->list, &instance->cmd_pool);
149
150 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
151}
152
Sumant Patro1341c932006-01-25 12:02:40 -0800153
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400154/**
Sumant Patro1341c932006-01-25 12:02:40 -0800155* The following functions are defined for xscale
156* (deviceid : 1064R, PERC5) controllers
157*/
158
159/**
160 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400161 * @regs: MFI register set
162 */
163static inline void
Sumant Patro1341c932006-01-25 12:02:40 -0800164megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400165{
166 writel(1, &(regs)->outbound_intr_mask);
167
168 /* Dummy readl to force pci flush */
169 readl(&regs->outbound_intr_mask);
170}
171
172/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700173 * megasas_disable_intr_xscale -Disables interrupt
174 * @regs: MFI register set
175 */
176static inline void
177megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
178{
179 u32 mask = 0x1f;
180 writel(mask, &regs->outbound_intr_mask);
181 /* Dummy readl to force pci flush */
182 readl(&regs->outbound_intr_mask);
183}
184
185/**
Sumant Patro1341c932006-01-25 12:02:40 -0800186 * megasas_read_fw_status_reg_xscale - returns the current FW status value
187 * @regs: MFI register set
188 */
189static u32
190megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
191{
192 return readl(&(regs)->outbound_msg_0);
193}
194/**
195 * megasas_clear_interrupt_xscale - Check & clear interrupt
196 * @regs: MFI register set
197 */
198static int
199megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
200{
201 u32 status;
202 /*
203 * Check if it is our interrupt
204 */
205 status = readl(&regs->outbound_intr_status);
206
207 if (!(status & MFI_OB_INTR_STATUS_MASK)) {
208 return 1;
209 }
210
211 /*
212 * Clear the interrupt by writing back the same value
213 */
214 writel(status, &regs->outbound_intr_status);
215
Yang, Bo06f579d2008-08-10 12:42:37 -0700216 /* Dummy readl to force pci flush */
217 readl(&regs->outbound_intr_status);
218
Sumant Patro1341c932006-01-25 12:02:40 -0800219 return 0;
220}
221
222/**
223 * megasas_fire_cmd_xscale - Sends command to the FW
224 * @frame_phys_addr : Physical address of cmd
225 * @frame_count : Number of frames for the command
226 * @regs : MFI register set
227 */
228static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600229megasas_fire_cmd_xscale(struct megasas_instance *instance,
230 dma_addr_t frame_phys_addr,
231 u32 frame_count,
232 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800233{
234 writel((frame_phys_addr >> 3)|(frame_count),
235 &(regs)->inbound_queue_port);
236}
237
238static struct megasas_instance_template megasas_instance_template_xscale = {
239
240 .fire_cmd = megasas_fire_cmd_xscale,
241 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700242 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800243 .clear_intr = megasas_clear_intr_xscale,
244 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
245};
246
247/**
248* This is the end of set of functions & definitions specific
249* to xscale (deviceid : 1064R, PERC5) controllers
250*/
251
252/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800253* The following functions are defined for ppc (deviceid : 0x60)
254* controllers
255*/
256
257/**
258 * megasas_enable_intr_ppc - Enables interrupts
259 * @regs: MFI register set
260 */
261static inline void
262megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
263{
264 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
265
266 writel(~0x80000004, &(regs)->outbound_intr_mask);
267
268 /* Dummy readl to force pci flush */
269 readl(&regs->outbound_intr_mask);
270}
271
272/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700273 * megasas_disable_intr_ppc - Disable interrupt
274 * @regs: MFI register set
275 */
276static inline void
277megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
278{
279 u32 mask = 0xFFFFFFFF;
280 writel(mask, &regs->outbound_intr_mask);
281 /* Dummy readl to force pci flush */
282 readl(&regs->outbound_intr_mask);
283}
284
285/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800286 * megasas_read_fw_status_reg_ppc - returns the current FW status value
287 * @regs: MFI register set
288 */
289static u32
290megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
291{
292 return readl(&(regs)->outbound_scratch_pad);
293}
294
295/**
296 * megasas_clear_interrupt_ppc - Check & clear interrupt
297 * @regs: MFI register set
298 */
299static int
300megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
301{
302 u32 status;
303 /*
304 * Check if it is our interrupt
305 */
306 status = readl(&regs->outbound_intr_status);
307
308 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
309 return 1;
310 }
311
312 /*
313 * Clear the interrupt by writing back the same value
314 */
315 writel(status, &regs->outbound_doorbell_clear);
316
Yang, Bo06f579d2008-08-10 12:42:37 -0700317 /* Dummy readl to force pci flush */
318 readl(&regs->outbound_doorbell_clear);
319
Sumant Patrof9876f02006-02-03 15:34:35 -0800320 return 0;
321}
322/**
323 * megasas_fire_cmd_ppc - Sends command to the FW
324 * @frame_phys_addr : Physical address of cmd
325 * @frame_count : Number of frames for the command
326 * @regs : MFI register set
327 */
328static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600329megasas_fire_cmd_ppc(struct megasas_instance *instance,
330 dma_addr_t frame_phys_addr,
331 u32 frame_count,
332 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800333{
334 writel((frame_phys_addr | (frame_count<<1))|1,
335 &(regs)->inbound_queue_port);
336}
337
338static struct megasas_instance_template megasas_instance_template_ppc = {
339
340 .fire_cmd = megasas_fire_cmd_ppc,
341 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700342 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800343 .clear_intr = megasas_clear_intr_ppc,
344 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
345};
346
347/**
Yang, Bo87911122009-10-06 14:31:54 -0600348 * megasas_enable_intr_skinny - Enables interrupts
349 * @regs: MFI register set
350 */
351static inline void
352megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
353{
354 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
355
356 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
357
358 /* Dummy readl to force pci flush */
359 readl(&regs->outbound_intr_mask);
360}
361
362/**
363 * megasas_disable_intr_skinny - Disables interrupt
364 * @regs: MFI register set
365 */
366static inline void
367megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
368{
369 u32 mask = 0xFFFFFFFF;
370 writel(mask, &regs->outbound_intr_mask);
371 /* Dummy readl to force pci flush */
372 readl(&regs->outbound_intr_mask);
373}
374
375/**
376 * megasas_read_fw_status_reg_skinny - returns the current FW status value
377 * @regs: MFI register set
378 */
379static u32
380megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
381{
382 return readl(&(regs)->outbound_scratch_pad);
383}
384
385/**
386 * megasas_clear_interrupt_skinny - Check & clear interrupt
387 * @regs: MFI register set
388 */
389static int
390megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
391{
392 u32 status;
393 /*
394 * Check if it is our interrupt
395 */
396 status = readl(&regs->outbound_intr_status);
397
398 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
399 return 1;
400 }
401
402 /*
403 * Clear the interrupt by writing back the same value
404 */
405 writel(status, &regs->outbound_intr_status);
406
407 /*
408 * dummy read to flush PCI
409 */
410 readl(&regs->outbound_intr_status);
411
412 return 0;
413}
414
415/**
416 * megasas_fire_cmd_skinny - Sends command to the FW
417 * @frame_phys_addr : Physical address of cmd
418 * @frame_count : Number of frames for the command
419 * @regs : MFI register set
420 */
421static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600422megasas_fire_cmd_skinny(struct megasas_instance *instance,
423 dma_addr_t frame_phys_addr,
424 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600425 struct megasas_register_set __iomem *regs)
426{
Yang, Bo0c79e682009-10-06 14:47:35 -0600427 unsigned long flags;
428 spin_lock_irqsave(&instance->fire_lock, flags);
Yang, Bo87911122009-10-06 14:31:54 -0600429 writel(0, &(regs)->inbound_high_queue_port);
430 writel((frame_phys_addr | (frame_count<<1))|1,
431 &(regs)->inbound_low_queue_port);
Yang, Bo0c79e682009-10-06 14:47:35 -0600432 spin_unlock_irqrestore(&instance->fire_lock, flags);
Yang, Bo87911122009-10-06 14:31:54 -0600433}
434
435static struct megasas_instance_template megasas_instance_template_skinny = {
436
437 .fire_cmd = megasas_fire_cmd_skinny,
438 .enable_intr = megasas_enable_intr_skinny,
439 .disable_intr = megasas_disable_intr_skinny,
440 .clear_intr = megasas_clear_intr_skinny,
441 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
442};
443
444
445/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700446* The following functions are defined for gen2 (deviceid : 0x78 0x79)
447* controllers
448*/
449
450/**
451 * megasas_enable_intr_gen2 - Enables interrupts
452 * @regs: MFI register set
453 */
454static inline void
455megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
456{
457 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
458
459 /* write ~0x00000005 (4 & 1) to the intr mask*/
460 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
461
462 /* Dummy readl to force pci flush */
463 readl(&regs->outbound_intr_mask);
464}
465
466/**
467 * megasas_disable_intr_gen2 - Disables interrupt
468 * @regs: MFI register set
469 */
470static inline void
471megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
472{
473 u32 mask = 0xFFFFFFFF;
474 writel(mask, &regs->outbound_intr_mask);
475 /* Dummy readl to force pci flush */
476 readl(&regs->outbound_intr_mask);
477}
478
479/**
480 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
481 * @regs: MFI register set
482 */
483static u32
484megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
485{
486 return readl(&(regs)->outbound_scratch_pad);
487}
488
489/**
490 * megasas_clear_interrupt_gen2 - Check & clear interrupt
491 * @regs: MFI register set
492 */
493static int
494megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
495{
496 u32 status;
497 /*
498 * Check if it is our interrupt
499 */
500 status = readl(&regs->outbound_intr_status);
501
502 if (!(status & MFI_GEN2_ENABLE_INTERRUPT_MASK))
503 return 1;
504
505 /*
506 * Clear the interrupt by writing back the same value
507 */
508 writel(status, &regs->outbound_doorbell_clear);
509
510 /* Dummy readl to force pci flush */
511 readl(&regs->outbound_intr_status);
512
513 return 0;
514}
515/**
516 * megasas_fire_cmd_gen2 - Sends command to the FW
517 * @frame_phys_addr : Physical address of cmd
518 * @frame_count : Number of frames for the command
519 * @regs : MFI register set
520 */
521static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600522megasas_fire_cmd_gen2(struct megasas_instance *instance,
523 dma_addr_t frame_phys_addr,
524 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700525 struct megasas_register_set __iomem *regs)
526{
527 writel((frame_phys_addr | (frame_count<<1))|1,
528 &(regs)->inbound_queue_port);
529}
530
531static struct megasas_instance_template megasas_instance_template_gen2 = {
532
533 .fire_cmd = megasas_fire_cmd_gen2,
534 .enable_intr = megasas_enable_intr_gen2,
535 .disable_intr = megasas_disable_intr_gen2,
536 .clear_intr = megasas_clear_intr_gen2,
537 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
538};
539
540/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800541* This is the end of set of functions & definitions
542* specific to ppc (deviceid : 0x60) controllers
543*/
544
545/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400546 * megasas_issue_polled - Issues a polling command
547 * @instance: Adapter soft state
548 * @cmd: Command packet to be issued
549 *
550 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
551 */
552static int
553megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
554{
555 int i;
556 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
557
558 struct megasas_header *frame_hdr = &cmd->frame->hdr;
559
560 frame_hdr->cmd_status = 0xFF;
561 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
562
563 /*
564 * Issue the frame using inbound queue port
565 */
Yang, Bo0c79e682009-10-06 14:47:35 -0600566 instance->instancet->fire_cmd(instance,
567 cmd->frame_phys_addr, 0, instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400568
569 /*
570 * Wait for cmd_status to change
571 */
572 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
573 rmb();
574 msleep(1);
575 }
576
577 if (frame_hdr->cmd_status == 0xff)
578 return -ETIME;
579
580 return 0;
581}
582
583/**
584 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
585 * @instance: Adapter soft state
586 * @cmd: Command to be issued
587 *
588 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -0700589 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400590 * Used to issue ioctl commands.
591 */
592static int
593megasas_issue_blocked_cmd(struct megasas_instance *instance,
594 struct megasas_cmd *cmd)
595{
596 cmd->cmd_status = ENODATA;
597
Yang, Bo0c79e682009-10-06 14:47:35 -0600598 instance->instancet->fire_cmd(instance,
599 cmd->frame_phys_addr, 0, instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400600
Sumant Patro2a3681e2006-10-03 13:19:21 -0700601 wait_event_timeout(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA),
602 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400603
604 return 0;
605}
606
607/**
608 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
609 * @instance: Adapter soft state
610 * @cmd_to_abort: Previously issued cmd to be aborted
611 *
612 * MFI firmware can abort previously issued AEN comamnd (automatic event
613 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -0700614 * cmd and waits for return status.
615 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400616 */
617static int
618megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
619 struct megasas_cmd *cmd_to_abort)
620{
621 struct megasas_cmd *cmd;
622 struct megasas_abort_frame *abort_fr;
623
624 cmd = megasas_get_cmd(instance);
625
626 if (!cmd)
627 return -1;
628
629 abort_fr = &cmd->frame->abort;
630
631 /*
632 * Prepare and issue the abort frame
633 */
634 abort_fr->cmd = MFI_CMD_ABORT;
635 abort_fr->cmd_status = 0xFF;
636 abort_fr->flags = 0;
637 abort_fr->abort_context = cmd_to_abort->index;
638 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
639 abort_fr->abort_mfi_phys_addr_hi = 0;
640
641 cmd->sync_cmd = 1;
642 cmd->cmd_status = 0xFF;
643
Yang, Bo0c79e682009-10-06 14:47:35 -0600644 instance->instancet->fire_cmd(instance,
645 cmd->frame_phys_addr, 0, instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400646
647 /*
648 * Wait for this cmd to complete
649 */
Sumant Patro2a3681e2006-10-03 13:19:21 -0700650 wait_event_timeout(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF),
651 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400652
653 megasas_return_cmd(instance, cmd);
654 return 0;
655}
656
657/**
658 * megasas_make_sgl32 - Prepares 32-bit SGL
659 * @instance: Adapter soft state
660 * @scp: SCSI command from the mid-layer
661 * @mfi_sgl: SGL to be filled in
662 *
663 * If successful, this function returns the number of SG elements. Otherwise,
664 * it returnes -1.
665 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800666static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400667megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
668 union megasas_sgl *mfi_sgl)
669{
670 int i;
671 int sge_count;
672 struct scatterlist *os_sgl;
673
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900674 sge_count = scsi_dma_map(scp);
675 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400676
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900677 if (sge_count) {
678 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
679 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
680 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
681 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400682 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400683 return sge_count;
684}
685
686/**
687 * megasas_make_sgl64 - Prepares 64-bit SGL
688 * @instance: Adapter soft state
689 * @scp: SCSI command from the mid-layer
690 * @mfi_sgl: SGL to be filled in
691 *
692 * If successful, this function returns the number of SG elements. Otherwise,
693 * it returnes -1.
694 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800695static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400696megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
697 union megasas_sgl *mfi_sgl)
698{
699 int i;
700 int sge_count;
701 struct scatterlist *os_sgl;
702
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900703 sge_count = scsi_dma_map(scp);
704 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400705
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900706 if (sge_count) {
707 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
708 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
709 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
710 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400711 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400712 return sge_count;
713}
714
Yang, Bof4c9a132009-10-06 14:43:28 -0600715/**
716 * megasas_make_sgl_skinny - Prepares IEEE SGL
717 * @instance: Adapter soft state
718 * @scp: SCSI command from the mid-layer
719 * @mfi_sgl: SGL to be filled in
720 *
721 * If successful, this function returns the number of SG elements. Otherwise,
722 * it returnes -1.
723 */
724static int
725megasas_make_sgl_skinny(struct megasas_instance *instance,
726 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
727{
728 int i;
729 int sge_count;
730 struct scatterlist *os_sgl;
731
732 sge_count = scsi_dma_map(scp);
733
734 if (sge_count) {
735 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
736 mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
737 mfi_sgl->sge_skinny[i].phys_addr =
738 sg_dma_address(os_sgl);
739 }
740 }
741 return sge_count;
742}
743
Sumant Patrob1df99d2006-10-03 12:40:47 -0700744 /**
745 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -0400746 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -0700747 * @sge_count : number of sg elements
748 *
749 * Returns the number of frames required for numnber of sge's (sge_count)
750 */
751
Yang, Bof4c9a132009-10-06 14:43:28 -0600752static u32 megasas_get_frame_count(struct megasas_instance *instance,
753 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -0700754{
755 int num_cnt;
756 int sge_bytes;
757 u32 sge_sz;
758 u32 frame_count=0;
759
760 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
761 sizeof(struct megasas_sge32);
762
Yang, Bof4c9a132009-10-06 14:43:28 -0600763 if (instance->flag_ieee) {
764 sge_sz = sizeof(struct megasas_sge_skinny);
765 }
766
Sumant Patrob1df99d2006-10-03 12:40:47 -0700767 /*
bo yangd532dbe2008-03-17 03:36:43 -0400768 * Main frame can contain 2 SGEs for 64-bit SGLs and
769 * 3 SGEs for 32-bit SGLs for ldio &
770 * 1 SGEs for 64-bit SGLs and
771 * 2 SGEs for 32-bit SGLs for pthru frame
772 */
773 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -0600774 if (instance->flag_ieee == 1) {
775 num_cnt = sge_count - 1;
776 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -0400777 num_cnt = sge_count - 1;
778 else
779 num_cnt = sge_count - 2;
780 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -0600781 if (instance->flag_ieee == 1) {
782 num_cnt = sge_count - 1;
783 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -0400784 num_cnt = sge_count - 2;
785 else
786 num_cnt = sge_count - 3;
787 }
Sumant Patrob1df99d2006-10-03 12:40:47 -0700788
789 if(num_cnt>0){
790 sge_bytes = sge_sz * num_cnt;
791
792 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
793 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
794 }
795 /* Main frame */
796 frame_count +=1;
797
798 if (frame_count > 7)
799 frame_count = 8;
800 return frame_count;
801}
802
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400803/**
804 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
805 * @instance: Adapter soft state
806 * @scp: SCSI command
807 * @cmd: Command to be prepared in
808 *
809 * This function prepares CDB commands. These are typcially pass-through
810 * commands to the devices.
811 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800812static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400813megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
814 struct megasas_cmd *cmd)
815{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400816 u32 is_logical;
817 u32 device_id;
818 u16 flags = 0;
819 struct megasas_pthru_frame *pthru;
820
821 is_logical = MEGASAS_IS_LOGICAL(scp);
822 device_id = MEGASAS_DEV_INDEX(instance, scp);
823 pthru = (struct megasas_pthru_frame *)cmd->frame;
824
825 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
826 flags = MFI_FRAME_DIR_WRITE;
827 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
828 flags = MFI_FRAME_DIR_READ;
829 else if (scp->sc_data_direction == PCI_DMA_NONE)
830 flags = MFI_FRAME_DIR_NONE;
831
Yang, Bof4c9a132009-10-06 14:43:28 -0600832 if (instance->flag_ieee == 1) {
833 flags |= MFI_FRAME_IEEE;
834 }
835
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400836 /*
837 * Prepare the DCDB frame
838 */
839 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
840 pthru->cmd_status = 0x0;
841 pthru->scsi_status = 0x0;
842 pthru->target_id = device_id;
843 pthru->lun = scp->device->lun;
844 pthru->cdb_len = scp->cmd_len;
845 pthru->timeout = 0;
846 pthru->flags = flags;
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900847 pthru->data_xfer_len = scsi_bufflen(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400848
849 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
850
851 /*
Yang, Bo8d568252009-10-06 14:12:21 -0600852 * If the command is for the tape device, set the
853 * pthru timeout to the os layer timeout value.
854 */
855 if (scp->device->type == TYPE_TAPE) {
856 if ((scp->request->timeout / HZ) > 0xFFFF)
857 pthru->timeout = 0xFFFF;
858 else
859 pthru->timeout = scp->request->timeout / HZ;
860 }
861
862 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400863 * Construct SGL
864 */
Yang, Bof4c9a132009-10-06 14:43:28 -0600865 if (instance->flag_ieee == 1) {
866 pthru->flags |= MFI_FRAME_SGL64;
867 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
868 &pthru->sgl);
869 } else if (IS_DMA64) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400870 pthru->flags |= MFI_FRAME_SGL64;
871 pthru->sge_count = megasas_make_sgl64(instance, scp,
872 &pthru->sgl);
873 } else
874 pthru->sge_count = megasas_make_sgl32(instance, scp,
875 &pthru->sgl);
876
877 /*
878 * Sense info specific
879 */
880 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
881 pthru->sense_buf_phys_addr_hi = 0;
882 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
883
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400884 /*
885 * Compute the total number of frames this command consumes. FW uses
886 * this number to pull sufficient number of frames from host memory.
887 */
Yang, Bof4c9a132009-10-06 14:43:28 -0600888 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -0400889 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400890
891 return cmd->frame_count;
892}
893
894/**
895 * megasas_build_ldio - Prepares IOs to logical devices
896 * @instance: Adapter soft state
897 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +0200898 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400899 *
900 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
901 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800902static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400903megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
904 struct megasas_cmd *cmd)
905{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400906 u32 device_id;
907 u8 sc = scp->cmnd[0];
908 u16 flags = 0;
909 struct megasas_io_frame *ldio;
910
911 device_id = MEGASAS_DEV_INDEX(instance, scp);
912 ldio = (struct megasas_io_frame *)cmd->frame;
913
914 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
915 flags = MFI_FRAME_DIR_WRITE;
916 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
917 flags = MFI_FRAME_DIR_READ;
918
Yang, Bof4c9a132009-10-06 14:43:28 -0600919 if (instance->flag_ieee == 1) {
920 flags |= MFI_FRAME_IEEE;
921 }
922
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400923 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -0700924 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400925 */
926 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
927 ldio->cmd_status = 0x0;
928 ldio->scsi_status = 0x0;
929 ldio->target_id = device_id;
930 ldio->timeout = 0;
931 ldio->reserved_0 = 0;
932 ldio->pad_0 = 0;
933 ldio->flags = flags;
934 ldio->start_lba_hi = 0;
935 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
936
937 /*
938 * 6-byte READ(0x08) or WRITE(0x0A) cdb
939 */
940 if (scp->cmd_len == 6) {
941 ldio->lba_count = (u32) scp->cmnd[4];
942 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
943 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
944
945 ldio->start_lba_lo &= 0x1FFFFF;
946 }
947
948 /*
949 * 10-byte READ(0x28) or WRITE(0x2A) cdb
950 */
951 else if (scp->cmd_len == 10) {
952 ldio->lba_count = (u32) scp->cmnd[8] |
953 ((u32) scp->cmnd[7] << 8);
954 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
955 ((u32) scp->cmnd[3] << 16) |
956 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
957 }
958
959 /*
960 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
961 */
962 else if (scp->cmd_len == 12) {
963 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
964 ((u32) scp->cmnd[7] << 16) |
965 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
966
967 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
968 ((u32) scp->cmnd[3] << 16) |
969 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
970 }
971
972 /*
973 * 16-byte READ(0x88) or WRITE(0x8A) cdb
974 */
975 else if (scp->cmd_len == 16) {
976 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
977 ((u32) scp->cmnd[11] << 16) |
978 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
979
980 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
981 ((u32) scp->cmnd[7] << 16) |
982 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
983
984 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
985 ((u32) scp->cmnd[3] << 16) |
986 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
987
988 }
989
990 /*
991 * Construct SGL
992 */
Yang, Bof4c9a132009-10-06 14:43:28 -0600993 if (instance->flag_ieee) {
994 ldio->flags |= MFI_FRAME_SGL64;
995 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
996 &ldio->sgl);
997 } else if (IS_DMA64) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400998 ldio->flags |= MFI_FRAME_SGL64;
999 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1000 } else
1001 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1002
1003 /*
1004 * Sense info specific
1005 */
1006 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1007 ldio->sense_buf_phys_addr_hi = 0;
1008 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1009
Sumant Patrob1df99d2006-10-03 12:40:47 -07001010 /*
1011 * Compute the total number of frames this command consumes. FW uses
1012 * this number to pull sufficient number of frames from host memory.
1013 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001014 cmd->frame_count = megasas_get_frame_count(instance,
1015 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001016
1017 return cmd->frame_count;
1018}
1019
1020/**
Sumant Patrocb59aa62006-01-25 11:53:25 -08001021 * megasas_is_ldio - Checks if the cmd is for logical drive
1022 * @scmd: SCSI command
1023 *
1024 * Called by megasas_queue_command to find out if the command to be queued
1025 * is a logical drive command
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001026 */
Sumant Patrocb59aa62006-01-25 11:53:25 -08001027static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001028{
Sumant Patrocb59aa62006-01-25 11:53:25 -08001029 if (!MEGASAS_IS_LOGICAL(cmd))
1030 return 0;
1031 switch (cmd->cmnd[0]) {
1032 case READ_10:
1033 case WRITE_10:
1034 case READ_12:
1035 case WRITE_12:
1036 case READ_6:
1037 case WRITE_6:
1038 case READ_16:
1039 case WRITE_16:
1040 return 1;
1041 default:
1042 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001043 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001044}
1045
Sumant Patro658dced2006-10-03 13:09:14 -07001046 /**
1047 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1048 * in FW
1049 * @instance: Adapter soft state
1050 */
1051static inline void
1052megasas_dump_pending_frames(struct megasas_instance *instance)
1053{
1054 struct megasas_cmd *cmd;
1055 int i,n;
1056 union megasas_sgl *mfi_sgl;
1057 struct megasas_io_frame *ldio;
1058 struct megasas_pthru_frame *pthru;
1059 u32 sgcount;
1060 u32 max_cmd = instance->max_fw_cmds;
1061
1062 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1063 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1064 if (IS_DMA64)
1065 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1066 else
1067 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1068
1069 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1070 for (i = 0; i < max_cmd; i++) {
1071 cmd = instance->cmd_list[i];
1072 if(!cmd->scmd)
1073 continue;
1074 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1075 if (megasas_is_ldio(cmd->scmd)){
1076 ldio = (struct megasas_io_frame *)cmd->frame;
1077 mfi_sgl = &ldio->sgl;
1078 sgcount = ldio->sge_count;
1079 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);
1080 }
1081 else {
1082 pthru = (struct megasas_pthru_frame *) cmd->frame;
1083 mfi_sgl = &pthru->sgl;
1084 sgcount = pthru->sge_count;
1085 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);
1086 }
1087 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1088 for (n = 0; n < sgcount; n++){
1089 if (IS_DMA64)
1090 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) ;
1091 else
1092 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
1093 }
1094 }
1095 printk(KERN_ERR "\n");
1096 } /*for max_cmd*/
1097 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1098 for (i = 0; i < max_cmd; i++) {
1099
1100 cmd = instance->cmd_list[i];
1101
1102 if(cmd->sync_cmd == 1){
1103 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1104 }
1105 }
1106 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1107}
1108
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001109/**
1110 * megasas_queue_command - Queue entry point
1111 * @scmd: SCSI command to be queued
1112 * @done: Callback entry point
1113 */
1114static int
1115megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
1116{
1117 u32 frame_count;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001118 struct megasas_cmd *cmd;
1119 struct megasas_instance *instance;
1120
1121 instance = (struct megasas_instance *)
1122 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001123
1124 /* Don't process if we have already declared adapter dead */
1125 if (instance->hw_crit_error)
1126 return SCSI_MLQUEUE_HOST_BUSY;
1127
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001128 scmd->scsi_done = done;
1129 scmd->result = 0;
1130
Sumant Patrocb59aa62006-01-25 11:53:25 -08001131 if (MEGASAS_IS_LOGICAL(scmd) &&
1132 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1133 scmd->result = DID_BAD_TARGET << 16;
1134 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001135 }
1136
Sumant Patro02b01e02007-02-14 13:00:55 -08001137 switch (scmd->cmnd[0]) {
1138 case SYNCHRONIZE_CACHE:
1139 /*
1140 * FW takes care of flush cache on its own
1141 * No need to send it down
1142 */
1143 scmd->result = DID_OK << 16;
1144 goto out_done;
1145 default:
1146 break;
1147 }
1148
Sumant Patrocb59aa62006-01-25 11:53:25 -08001149 cmd = megasas_get_cmd(instance);
1150 if (!cmd)
1151 return SCSI_MLQUEUE_HOST_BUSY;
1152
1153 /*
1154 * Logical drive command
1155 */
1156 if (megasas_is_ldio(scmd))
1157 frame_count = megasas_build_ldio(instance, scmd, cmd);
1158 else
1159 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1160
1161 if (!frame_count)
1162 goto out_return_cmd;
1163
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001164 cmd->scmd = scmd;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001165 scmd->SCp.ptr = (char *)cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001166
1167 /*
1168 * Issue the command to the FW
1169 */
Sumant Patroe4a082c2006-05-30 12:03:37 -07001170 atomic_inc(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001171
Yang, Bo0c79e682009-10-06 14:47:35 -06001172 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1173 cmd->frame_count-1, instance->reg_set);
bo yangad84db22007-11-09 04:40:16 -05001174 /*
1175 * Check if we have pend cmds to be completed
1176 */
1177 if (poll_mode_io && atomic_read(&instance->fw_outstanding))
1178 tasklet_schedule(&instance->isr_tasklet);
1179
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001180
1181 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001182
1183 out_return_cmd:
1184 megasas_return_cmd(instance, cmd);
1185 out_done:
1186 done(scmd);
1187 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001188}
1189
Yang, Bo044833b2009-10-06 14:33:06 -06001190static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1191{
1192 int i;
1193
1194 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1195
1196 if ((megasas_mgmt_info.instance[i]) &&
1197 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1198 return megasas_mgmt_info.instance[i];
1199 }
1200
1201 return NULL;
1202}
1203
Christoph Hellwig147aab62006-02-17 12:13:48 +01001204static int megasas_slave_configure(struct scsi_device *sdev)
1205{
Yang, Bo044833b2009-10-06 14:33:06 -06001206 u16 pd_index = 0;
1207 struct megasas_instance *instance ;
1208
1209 instance = megasas_lookup_instance(sdev->host->host_no);
Christoph Hellwige5b3a652006-03-10 17:08:57 +01001210
1211 /*
Yang, Bo044833b2009-10-06 14:33:06 -06001212 * Don't export physical disk devices to the disk driver.
1213 *
1214 * FIXME: Currently we don't export them to the midlayer at all.
1215 * That will be fixed once LSI engineers have audited the
1216 * firmware for possible issues.
1217 */
1218 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1219 sdev->type == TYPE_DISK) {
1220 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1221 sdev->id;
1222 if (instance->pd_list[pd_index].driveState ==
1223 MR_PD_STATE_SYSTEM) {
1224 blk_queue_rq_timeout(sdev->request_queue,
1225 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1226 return 0;
1227 }
1228 return -ENXIO;
1229 }
1230
1231 /*
1232 * The RAID firmware may require extended timeouts.
1233 */
1234 blk_queue_rq_timeout(sdev->request_queue,
1235 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1236 return 0;
1237}
1238
1239static int megasas_slave_alloc(struct scsi_device *sdev)
1240{
1241 u16 pd_index = 0;
1242 struct megasas_instance *instance ;
1243 instance = megasas_lookup_instance(sdev->host->host_no);
1244 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1245 (sdev->type == TYPE_DISK)) {
1246 /*
1247 * Open the OS scan to the SYSTEM PD
1248 */
1249 pd_index =
1250 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1251 sdev->id;
1252 if ((instance->pd_list[pd_index].driveState ==
1253 MR_PD_STATE_SYSTEM) &&
1254 (instance->pd_list[pd_index].driveType ==
1255 TYPE_DISK)) {
1256 return 0;
1257 }
1258 return -ENXIO;
1259 }
Christoph Hellwig147aab62006-02-17 12:13:48 +01001260 return 0;
1261}
1262
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001263/**
bo yang7343eb62007-11-09 04:35:44 -05001264 * megasas_complete_cmd_dpc - Returns FW's controller structure
1265 * @instance_addr: Address of adapter soft state
1266 *
1267 * Tasklet to complete cmds
1268 */
1269static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1270{
1271 u32 producer;
1272 u32 consumer;
1273 u32 context;
1274 struct megasas_cmd *cmd;
1275 struct megasas_instance *instance =
1276 (struct megasas_instance *)instance_addr;
1277 unsigned long flags;
1278
1279 /* If we have already declared adapter dead, donot complete cmds */
1280 if (instance->hw_crit_error)
1281 return;
1282
1283 spin_lock_irqsave(&instance->completion_lock, flags);
1284
1285 producer = *instance->producer;
1286 consumer = *instance->consumer;
1287
1288 while (consumer != producer) {
1289 context = instance->reply_queue[consumer];
1290
1291 cmd = instance->cmd_list[context];
1292
1293 megasas_complete_cmd(instance, cmd, DID_OK);
1294
1295 consumer++;
1296 if (consumer == (instance->max_fw_cmds + 1)) {
1297 consumer = 0;
1298 }
1299 }
1300
1301 *instance->consumer = producer;
1302
1303 spin_unlock_irqrestore(&instance->completion_lock, flags);
1304
1305 /*
1306 * Check if we can restore can_queue
1307 */
1308 if (instance->flag & MEGASAS_FW_BUSY
1309 && time_after(jiffies, instance->last_time + 5 * HZ)
1310 && atomic_read(&instance->fw_outstanding) < 17) {
1311
1312 spin_lock_irqsave(instance->host->host_lock, flags);
1313 instance->flag &= ~MEGASAS_FW_BUSY;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06001314 if ((instance->pdev->device ==
1315 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1316 (instance->pdev->device ==
1317 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1318 instance->host->can_queue =
1319 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1320 } else
1321 instance->host->can_queue =
bo yang7343eb62007-11-09 04:35:44 -05001322 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1323
1324 spin_unlock_irqrestore(instance->host->host_lock, flags);
1325 }
1326}
1327
1328/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001329 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1330 * @instance: Adapter soft state
1331 *
1332 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
1333 * complete all its outstanding commands. Returns error if one or more IOs
1334 * are pending after this time period. It also marks the controller dead.
1335 */
1336static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1337{
1338 int i;
1339 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
1340
1341 for (i = 0; i < wait_time; i++) {
1342
Sumant Patroe4a082c2006-05-30 12:03:37 -07001343 int outstanding = atomic_read(&instance->fw_outstanding);
1344
1345 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001346 break;
1347
1348 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1349 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07001350 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05001351 /*
1352 * Call cmd completion routine. Cmd to be
1353 * be completed directly without depending on isr.
1354 */
1355 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001356 }
1357
1358 msleep(1000);
1359 }
1360
Sumant Patroe4a082c2006-05-30 12:03:37 -07001361 if (atomic_read(&instance->fw_outstanding)) {
Sumant Patroe3bbff92006-10-03 12:28:49 -07001362 /*
1363 * Send signal to FW to stop processing any pending cmds.
1364 * The controller will be taken offline by the OS now.
1365 */
Yang, Bo0c79e682009-10-06 14:47:35 -06001366 if ((instance->pdev->device ==
1367 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1368 (instance->pdev->device ==
1369 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1370 writel(MFI_STOP_ADP,
1371 &instance->reg_set->reserved_0[0]);
1372 } else {
1373 writel(MFI_STOP_ADP,
Sumant Patroe3bbff92006-10-03 12:28:49 -07001374 &instance->reg_set->inbound_doorbell);
Yang, Bo0c79e682009-10-06 14:47:35 -06001375 }
Sumant Patro658dced2006-10-03 13:09:14 -07001376 megasas_dump_pending_frames(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001377 instance->hw_crit_error = 1;
1378 return FAILED;
1379 }
1380
1381 return SUCCESS;
1382}
1383
1384/**
1385 * megasas_generic_reset - Generic reset routine
1386 * @scmd: Mid-layer SCSI command
1387 *
1388 * This routine implements a generic reset handler for device, bus and host
1389 * reset requests. Device, bus and host specific reset handlers can use this
1390 * function after they do their specific tasks.
1391 */
1392static int megasas_generic_reset(struct scsi_cmnd *scmd)
1393{
1394 int ret_val;
1395 struct megasas_instance *instance;
1396
1397 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1398
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001399 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
1400 scmd->serial_number, scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001401
1402 if (instance->hw_crit_error) {
1403 printk(KERN_ERR "megasas: cannot recover from previous reset "
1404 "failures\n");
1405 return FAILED;
1406 }
1407
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001408 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001409 if (ret_val == SUCCESS)
1410 printk(KERN_NOTICE "megasas: reset successful \n");
1411 else
1412 printk(KERN_ERR "megasas: failed to do reset\n");
1413
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001414 return ret_val;
1415}
1416
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001417/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001418 * megasas_reset_timer - quiesce the adapter if required
1419 * @scmd: scsi cmnd
1420 *
1421 * Sets the FW busy flag and reduces the host->can_queue if the
1422 * cmd has not been completed within the timeout period.
1423 */
1424static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07001425blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001426{
1427 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
1428 struct megasas_instance *instance;
1429 unsigned long flags;
1430
1431 if (time_after(jiffies, scmd->jiffies_at_alloc +
1432 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001433 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001434 }
1435
1436 instance = cmd->instance;
1437 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1438 /* FW is busy, throttle IO */
1439 spin_lock_irqsave(instance->host->host_lock, flags);
1440
1441 instance->host->can_queue = 16;
1442 instance->last_time = jiffies;
1443 instance->flag |= MEGASAS_FW_BUSY;
1444
1445 spin_unlock_irqrestore(instance->host->host_lock, flags);
1446 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07001447 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001448}
1449
1450/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001451 * megasas_reset_device - Device reset handler entry point
1452 */
1453static int megasas_reset_device(struct scsi_cmnd *scmd)
1454{
1455 int ret;
1456
1457 /*
1458 * First wait for all commands to complete
1459 */
1460 ret = megasas_generic_reset(scmd);
1461
1462 return ret;
1463}
1464
1465/**
1466 * megasas_reset_bus_host - Bus & host reset handler entry point
1467 */
1468static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1469{
1470 int ret;
1471
1472 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01001473 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001474 */
1475 ret = megasas_generic_reset(scmd);
1476
1477 return ret;
1478}
1479
1480/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001481 * megasas_bios_param - Returns disk geometry for a disk
1482 * @sdev: device handle
1483 * @bdev: block device
1484 * @capacity: drive capacity
1485 * @geom: geometry parameters
1486 */
1487static int
1488megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1489 sector_t capacity, int geom[])
1490{
1491 int heads;
1492 int sectors;
1493 sector_t cylinders;
1494 unsigned long tmp;
1495 /* Default heads (64) & sectors (32) */
1496 heads = 64;
1497 sectors = 32;
1498
1499 tmp = heads * sectors;
1500 cylinders = capacity;
1501
1502 sector_div(cylinders, tmp);
1503
1504 /*
1505 * Handle extended translation size for logical drives > 1Gb
1506 */
1507
1508 if (capacity >= 0x200000) {
1509 heads = 255;
1510 sectors = 63;
1511 tmp = heads*sectors;
1512 cylinders = capacity;
1513 sector_div(cylinders, tmp);
1514 }
1515
1516 geom[0] = heads;
1517 geom[1] = sectors;
1518 geom[2] = cylinders;
1519
1520 return 0;
1521}
1522
1523/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001524 * megasas_service_aen - Processes an event notification
1525 * @instance: Adapter soft state
1526 * @cmd: AEN command completed by the ISR
1527 *
1528 * For AEN, driver sends a command down to FW that is held by the FW till an
1529 * event occurs. When an event of interest occurs, FW completes the command
1530 * that it was previously holding.
1531 *
1532 * This routines sends SIGIO signal to processes that have registered with the
1533 * driver for AEN.
1534 */
1535static void
1536megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1537{
Yang, Boc3518832009-10-06 14:18:02 -06001538 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001539 /*
1540 * Don't signal app if it is just an aborted previously registered aen
1541 */
Yang, Boc3518832009-10-06 14:18:02 -06001542 if ((!cmd->abort_aen) && (instance->unload == 0)) {
1543 spin_lock_irqsave(&poll_aen_lock, flags);
1544 megasas_poll_wait_aen = 1;
1545 spin_unlock_irqrestore(&poll_aen_lock, flags);
1546 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001547 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06001548 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001549 else
1550 cmd->abort_aen = 0;
1551
1552 instance->aen_cmd = NULL;
1553 megasas_return_cmd(instance, cmd);
1554}
1555
1556/*
1557 * Scsi host template for megaraid_sas driver
1558 */
1559static struct scsi_host_template megasas_template = {
1560
1561 .module = THIS_MODULE,
bo yangf28cd7c2007-11-09 04:44:56 -05001562 .name = "LSI SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001563 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01001564 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06001565 .slave_alloc = megasas_slave_alloc,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001566 .queuecommand = megasas_queue_command,
1567 .eh_device_reset_handler = megasas_reset_device,
1568 .eh_bus_reset_handler = megasas_reset_bus_host,
1569 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001570 .eh_timed_out = megasas_reset_timer,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001571 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001572 .use_clustering = ENABLE_CLUSTERING,
1573};
1574
1575/**
1576 * megasas_complete_int_cmd - Completes an internal command
1577 * @instance: Adapter soft state
1578 * @cmd: Command to be completed
1579 *
1580 * The megasas_issue_blocked_cmd() function waits for a command to complete
1581 * after it issues a command. This function wakes up that waiting routine by
1582 * calling wake_up() on the wait queue.
1583 */
1584static void
1585megasas_complete_int_cmd(struct megasas_instance *instance,
1586 struct megasas_cmd *cmd)
1587{
1588 cmd->cmd_status = cmd->frame->io.cmd_status;
1589
1590 if (cmd->cmd_status == ENODATA) {
1591 cmd->cmd_status = 0;
1592 }
1593 wake_up(&instance->int_cmd_wait_q);
1594}
1595
1596/**
1597 * megasas_complete_abort - Completes aborting a command
1598 * @instance: Adapter soft state
1599 * @cmd: Cmd that was issued to abort another cmd
1600 *
1601 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
1602 * after it issues an abort on a previously issued command. This function
1603 * wakes up all functions waiting on the same wait queue.
1604 */
1605static void
1606megasas_complete_abort(struct megasas_instance *instance,
1607 struct megasas_cmd *cmd)
1608{
1609 if (cmd->sync_cmd) {
1610 cmd->sync_cmd = 0;
1611 cmd->cmd_status = 0;
1612 wake_up(&instance->abort_cmd_wait_q);
1613 }
1614
1615 return;
1616}
1617
1618/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001619 * megasas_complete_cmd - Completes a command
1620 * @instance: Adapter soft state
1621 * @cmd: Command to be completed
1622 * @alt_status: If non-zero, use this value as status to
1623 * SCSI mid-layer instead of the value returned
1624 * by the FW. This should be used if caller wants
1625 * an alternate status (as in the case of aborted
1626 * commands)
1627 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001628static void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001629megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
1630 u8 alt_status)
1631{
1632 int exception = 0;
1633 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06001634 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001635
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001636 if (cmd->scmd)
1637 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001638
1639 switch (hdr->cmd) {
1640
1641 case MFI_CMD_PD_SCSI_IO:
1642 case MFI_CMD_LD_SCSI_IO:
1643
1644 /*
1645 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1646 * issued either through an IO path or an IOCTL path. If it
1647 * was via IOCTL, we will send it to internal completion.
1648 */
1649 if (cmd->sync_cmd) {
1650 cmd->sync_cmd = 0;
1651 megasas_complete_int_cmd(instance, cmd);
1652 break;
1653 }
1654
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001655 case MFI_CMD_LD_READ:
1656 case MFI_CMD_LD_WRITE:
1657
1658 if (alt_status) {
1659 cmd->scmd->result = alt_status << 16;
1660 exception = 1;
1661 }
1662
1663 if (exception) {
1664
Sumant Patroe4a082c2006-05-30 12:03:37 -07001665 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001666
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001667 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001668 cmd->scmd->scsi_done(cmd->scmd);
1669 megasas_return_cmd(instance, cmd);
1670
1671 break;
1672 }
1673
1674 switch (hdr->cmd_status) {
1675
1676 case MFI_STAT_OK:
1677 cmd->scmd->result = DID_OK << 16;
1678 break;
1679
1680 case MFI_STAT_SCSI_IO_FAILED:
1681 case MFI_STAT_LD_INIT_IN_PROGRESS:
1682 cmd->scmd->result =
1683 (DID_ERROR << 16) | hdr->scsi_status;
1684 break;
1685
1686 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1687
1688 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1689
1690 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1691 memset(cmd->scmd->sense_buffer, 0,
1692 SCSI_SENSE_BUFFERSIZE);
1693 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1694 hdr->sense_len);
1695
1696 cmd->scmd->result |= DRIVER_SENSE << 24;
1697 }
1698
1699 break;
1700
1701 case MFI_STAT_LD_OFFLINE:
1702 case MFI_STAT_DEVICE_NOT_FOUND:
1703 cmd->scmd->result = DID_BAD_TARGET << 16;
1704 break;
1705
1706 default:
1707 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1708 hdr->cmd_status);
1709 cmd->scmd->result = DID_ERROR << 16;
1710 break;
1711 }
1712
Sumant Patroe4a082c2006-05-30 12:03:37 -07001713 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001714
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001715 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001716 cmd->scmd->scsi_done(cmd->scmd);
1717 megasas_return_cmd(instance, cmd);
1718
1719 break;
1720
1721 case MFI_CMD_SMP:
1722 case MFI_CMD_STP:
1723 case MFI_CMD_DCMD:
Yang, Boc3518832009-10-06 14:18:02 -06001724 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
1725 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
1726 spin_lock_irqsave(&poll_aen_lock, flags);
1727 megasas_poll_wait_aen = 0;
1728 spin_unlock_irqrestore(&poll_aen_lock, flags);
1729 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001730
1731 /*
1732 * See if got an event notification
1733 */
1734 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1735 megasas_service_aen(instance, cmd);
1736 else
1737 megasas_complete_int_cmd(instance, cmd);
1738
1739 break;
1740
1741 case MFI_CMD_ABORT:
1742 /*
1743 * Cmd issued to abort another cmd returned
1744 */
1745 megasas_complete_abort(instance, cmd);
1746 break;
1747
1748 default:
1749 printk("megasas: Unknown command completed! [0x%X]\n",
1750 hdr->cmd);
1751 break;
1752 }
1753}
1754
1755/**
1756 * megasas_deplete_reply_queue - Processes all completed commands
1757 * @instance: Adapter soft state
1758 * @alt_status: Alternate status to be returned to
1759 * SCSI mid-layer instead of the status
1760 * returned by the FW
1761 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001762static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001763megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1764{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001765 /*
1766 * Check if it is our interrupt
Sumant Patro1341c932006-01-25 12:02:40 -08001767 * Clear the interrupt
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001768 */
Sumant Patro1341c932006-01-25 12:02:40 -08001769 if(instance->instancet->clear_intr(instance->reg_set))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001770 return IRQ_NONE;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001771
Sumant Patroaf37acf2007-02-14 12:34:46 -08001772 if (instance->hw_crit_error)
1773 goto out_done;
Sumant Patro5d018ad2006-10-03 13:13:18 -07001774 /*
1775 * Schedule the tasklet for cmd completion
1776 */
1777 tasklet_schedule(&instance->isr_tasklet);
Sumant Patroaf37acf2007-02-14 12:34:46 -08001778out_done:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001779 return IRQ_HANDLED;
1780}
1781
1782/**
1783 * megasas_isr - isr entry point
1784 */
David Howells7d12e782006-10-05 14:55:46 +01001785static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001786{
1787 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1788 DID_OK);
1789}
1790
1791/**
1792 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08001793 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001794 *
1795 * During the initialization, FW passes can potentially be in any one of
1796 * several possible states. If the FW in operational, waiting-for-handshake
1797 * states, driver must take steps to bring it to ready state. Otherwise, it
1798 * has to wait for the ready state.
1799 */
1800static int
Sumant Patro1341c932006-01-25 12:02:40 -08001801megasas_transition_to_ready(struct megasas_instance* instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001802{
1803 int i;
1804 u8 max_wait;
1805 u32 fw_state;
1806 u32 cur_state;
1807
Sumant Patro1341c932006-01-25 12:02:40 -08001808 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001809
Sumant Patroe3bbff92006-10-03 12:28:49 -07001810 if (fw_state != MFI_STATE_READY)
1811 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1812 " state\n");
1813
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001814 while (fw_state != MFI_STATE_READY) {
1815
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001816 switch (fw_state) {
1817
1818 case MFI_STATE_FAULT:
1819
1820 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1821 return -ENODEV;
1822
1823 case MFI_STATE_WAIT_HANDSHAKE:
1824 /*
1825 * Set the CLR bit in inbound doorbell
1826 */
Yang, Bo0c79e682009-10-06 14:47:35 -06001827 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06001828 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1829 (instance->pdev->device ==
1830 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1831
1832 writel(
1833 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
1834 &instance->reg_set->reserved_0[0]);
1835 } else {
1836 writel(
1837 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
1838 &instance->reg_set->inbound_doorbell);
1839 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001840
1841 max_wait = 2;
1842 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1843 break;
1844
Sumant Patroe3bbff92006-10-03 12:28:49 -07001845 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06001846 if ((instance->pdev->device ==
1847 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1848 (instance->pdev->device ==
1849 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1850 writel(MFI_INIT_HOTPLUG,
1851 &instance->reg_set->reserved_0[0]);
1852 } else
1853 writel(MFI_INIT_HOTPLUG,
1854 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07001855
1856 max_wait = 10;
1857 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
1858 break;
1859
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001860 case MFI_STATE_OPERATIONAL:
1861 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07001862 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001863 */
Sumant Patrob274cab2006-10-03 12:52:12 -07001864 instance->instancet->disable_intr(instance->reg_set);
Yang, Bo87911122009-10-06 14:31:54 -06001865 if ((instance->pdev->device ==
1866 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1867 (instance->pdev->device ==
1868 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1869 writel(MFI_RESET_FLAGS,
1870 &instance->reg_set->reserved_0[0]);
1871 } else
1872 writel(MFI_RESET_FLAGS,
1873 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001874
bo yangaf7a5642008-03-17 04:13:07 -04001875 max_wait = 60;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001876 cur_state = MFI_STATE_OPERATIONAL;
1877 break;
1878
1879 case MFI_STATE_UNDEFINED:
1880 /*
1881 * This state should not last for more than 2 seconds
1882 */
1883 max_wait = 2;
1884 cur_state = MFI_STATE_UNDEFINED;
1885 break;
1886
1887 case MFI_STATE_BB_INIT:
1888 max_wait = 2;
1889 cur_state = MFI_STATE_BB_INIT;
1890 break;
1891
1892 case MFI_STATE_FW_INIT:
1893 max_wait = 20;
1894 cur_state = MFI_STATE_FW_INIT;
1895 break;
1896
1897 case MFI_STATE_FW_INIT_2:
1898 max_wait = 20;
1899 cur_state = MFI_STATE_FW_INIT_2;
1900 break;
1901
1902 case MFI_STATE_DEVICE_SCAN:
1903 max_wait = 20;
1904 cur_state = MFI_STATE_DEVICE_SCAN;
1905 break;
1906
1907 case MFI_STATE_FLUSH_CACHE:
1908 max_wait = 20;
1909 cur_state = MFI_STATE_FLUSH_CACHE;
1910 break;
1911
1912 default:
1913 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1914 fw_state);
1915 return -ENODEV;
1916 }
1917
1918 /*
1919 * The cur_state should not last for more than max_wait secs
1920 */
1921 for (i = 0; i < (max_wait * 1000); i++) {
Sumant Patro1341c932006-01-25 12:02:40 -08001922 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1923 MFI_STATE_MASK ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001924
1925 if (fw_state == cur_state) {
1926 msleep(1);
1927 } else
1928 break;
1929 }
1930
1931 /*
1932 * Return error if fw_state hasn't changed after max_wait
1933 */
1934 if (fw_state == cur_state) {
1935 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1936 "in %d secs\n", fw_state, max_wait);
1937 return -ENODEV;
1938 }
1939 };
Sumant Patroe3bbff92006-10-03 12:28:49 -07001940 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001941
1942 return 0;
1943}
1944
1945/**
1946 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1947 * @instance: Adapter soft state
1948 */
1949static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1950{
1951 int i;
1952 u32 max_cmd = instance->max_fw_cmds;
1953 struct megasas_cmd *cmd;
1954
1955 if (!instance->frame_dma_pool)
1956 return;
1957
1958 /*
1959 * Return all frames to pool
1960 */
1961 for (i = 0; i < max_cmd; i++) {
1962
1963 cmd = instance->cmd_list[i];
1964
1965 if (cmd->frame)
1966 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1967 cmd->frame_phys_addr);
1968
1969 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07001970 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001971 cmd->sense_phys_addr);
1972 }
1973
1974 /*
1975 * Now destroy the pool itself
1976 */
1977 pci_pool_destroy(instance->frame_dma_pool);
1978 pci_pool_destroy(instance->sense_dma_pool);
1979
1980 instance->frame_dma_pool = NULL;
1981 instance->sense_dma_pool = NULL;
1982}
1983
1984/**
1985 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1986 * @instance: Adapter soft state
1987 *
1988 * Each command packet has an embedded DMA memory buffer that is used for
1989 * filling MFI frame and the SG list that immediately follows the frame. This
1990 * function creates those DMA memory buffers for each command packet by using
1991 * PCI pool facility.
1992 */
1993static int megasas_create_frame_pool(struct megasas_instance *instance)
1994{
1995 int i;
1996 u32 max_cmd;
1997 u32 sge_sz;
1998 u32 sgl_sz;
1999 u32 total_sz;
2000 u32 frame_count;
2001 struct megasas_cmd *cmd;
2002
2003 max_cmd = instance->max_fw_cmds;
2004
2005 /*
2006 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2007 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2008 */
2009 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2010 sizeof(struct megasas_sge32);
2011
Yang, Bof4c9a132009-10-06 14:43:28 -06002012 if (instance->flag_ieee) {
2013 sge_sz = sizeof(struct megasas_sge_skinny);
2014 }
2015
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002016 /*
2017 * Calculated the number of 64byte frames required for SGL
2018 */
2019 sgl_sz = sge_sz * instance->max_num_sge;
2020 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
2021
2022 /*
2023 * We need one extra frame for the MFI command
2024 */
2025 frame_count++;
2026
2027 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2028 /*
2029 * Use DMA pool facility provided by PCI layer
2030 */
2031 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2032 instance->pdev, total_sz, 64,
2033 0);
2034
2035 if (!instance->frame_dma_pool) {
2036 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2037 return -ENOMEM;
2038 }
2039
2040 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2041 instance->pdev, 128, 4, 0);
2042
2043 if (!instance->sense_dma_pool) {
2044 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2045
2046 pci_pool_destroy(instance->frame_dma_pool);
2047 instance->frame_dma_pool = NULL;
2048
2049 return -ENOMEM;
2050 }
2051
2052 /*
2053 * Allocate and attach a frame to each of the commands in cmd_list.
2054 * By making cmd->index as the context instead of the &cmd, we can
2055 * always use 32bit context regardless of the architecture
2056 */
2057 for (i = 0; i < max_cmd; i++) {
2058
2059 cmd = instance->cmd_list[i];
2060
2061 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2062 GFP_KERNEL, &cmd->frame_phys_addr);
2063
2064 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2065 GFP_KERNEL, &cmd->sense_phys_addr);
2066
2067 /*
2068 * megasas_teardown_frame_pool() takes care of freeing
2069 * whatever has been allocated
2070 */
2071 if (!cmd->frame || !cmd->sense) {
2072 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2073 megasas_teardown_frame_pool(instance);
2074 return -ENOMEM;
2075 }
2076
2077 cmd->frame->io.context = cmd->index;
2078 }
2079
2080 return 0;
2081}
2082
2083/**
2084 * megasas_free_cmds - Free all the cmds in the free cmd pool
2085 * @instance: Adapter soft state
2086 */
2087static void megasas_free_cmds(struct megasas_instance *instance)
2088{
2089 int i;
2090 /* First free the MFI frame pool */
2091 megasas_teardown_frame_pool(instance);
2092
2093 /* Free all the commands in the cmd_list */
2094 for (i = 0; i < instance->max_fw_cmds; i++)
2095 kfree(instance->cmd_list[i]);
2096
2097 /* Free the cmd_list buffer itself */
2098 kfree(instance->cmd_list);
2099 instance->cmd_list = NULL;
2100
2101 INIT_LIST_HEAD(&instance->cmd_pool);
2102}
2103
2104/**
2105 * megasas_alloc_cmds - Allocates the command packets
2106 * @instance: Adapter soft state
2107 *
2108 * Each command that is issued to the FW, whether IO commands from the OS or
2109 * internal commands like IOCTLs, are wrapped in local data structure called
2110 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2111 * the FW.
2112 *
2113 * Each frame has a 32-bit field called context (tag). This context is used
2114 * to get back the megasas_cmd from the frame when a frame gets completed in
2115 * the ISR. Typically the address of the megasas_cmd itself would be used as
2116 * the context. But we wanted to keep the differences between 32 and 64 bit
2117 * systems to the mininum. We always use 32 bit integers for the context. In
2118 * this driver, the 32 bit values are the indices into an array cmd_list.
2119 * This array is used only to look up the megasas_cmd given the context. The
2120 * free commands themselves are maintained in a linked list called cmd_pool.
2121 */
2122static int megasas_alloc_cmds(struct megasas_instance *instance)
2123{
2124 int i;
2125 int j;
2126 u32 max_cmd;
2127 struct megasas_cmd *cmd;
2128
2129 max_cmd = instance->max_fw_cmds;
2130
2131 /*
2132 * instance->cmd_list is an array of struct megasas_cmd pointers.
2133 * Allocate the dynamic array first and then allocate individual
2134 * commands.
2135 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002136 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002137
2138 if (!instance->cmd_list) {
2139 printk(KERN_DEBUG "megasas: out of memory\n");
2140 return -ENOMEM;
2141 }
2142
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002143
2144 for (i = 0; i < max_cmd; i++) {
2145 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
2146 GFP_KERNEL);
2147
2148 if (!instance->cmd_list[i]) {
2149
2150 for (j = 0; j < i; j++)
2151 kfree(instance->cmd_list[j]);
2152
2153 kfree(instance->cmd_list);
2154 instance->cmd_list = NULL;
2155
2156 return -ENOMEM;
2157 }
2158 }
2159
2160 /*
2161 * Add all the commands to command pool (instance->cmd_pool)
2162 */
2163 for (i = 0; i < max_cmd; i++) {
2164 cmd = instance->cmd_list[i];
2165 memset(cmd, 0, sizeof(struct megasas_cmd));
2166 cmd->index = i;
2167 cmd->instance = instance;
2168
2169 list_add_tail(&cmd->list, &instance->cmd_pool);
2170 }
2171
2172 /*
2173 * Create a frame pool and assign one frame to each cmd
2174 */
2175 if (megasas_create_frame_pool(instance)) {
2176 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
2177 megasas_free_cmds(instance);
2178 }
2179
2180 return 0;
2181}
2182
Yang, Bo81e403c2009-10-06 14:27:54 -06002183/*
2184 * megasas_get_pd_list_info - Returns FW's pd_list structure
2185 * @instance: Adapter soft state
2186 * @pd_list: pd_list structure
2187 *
2188 * Issues an internal command (DCMD) to get the FW's controller PD
2189 * list structure. This information is mainly used to find out SYSTEM
2190 * supported by the FW.
2191 */
2192static int
2193megasas_get_pd_list(struct megasas_instance *instance)
2194{
2195 int ret = 0, pd_index = 0;
2196 struct megasas_cmd *cmd;
2197 struct megasas_dcmd_frame *dcmd;
2198 struct MR_PD_LIST *ci;
2199 struct MR_PD_ADDRESS *pd_addr;
2200 dma_addr_t ci_h = 0;
2201
2202 cmd = megasas_get_cmd(instance);
2203
2204 if (!cmd) {
2205 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
2206 return -ENOMEM;
2207 }
2208
2209 dcmd = &cmd->frame->dcmd;
2210
2211 ci = pci_alloc_consistent(instance->pdev,
2212 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
2213
2214 if (!ci) {
2215 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
2216 megasas_return_cmd(instance, cmd);
2217 return -ENOMEM;
2218 }
2219
2220 memset(ci, 0, sizeof(*ci));
2221 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2222
2223 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
2224 dcmd->mbox.b[1] = 0;
2225 dcmd->cmd = MFI_CMD_DCMD;
2226 dcmd->cmd_status = 0xFF;
2227 dcmd->sge_count = 1;
2228 dcmd->flags = MFI_FRAME_DIR_READ;
2229 dcmd->timeout = 0;
2230 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
2231 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
2232 dcmd->sgl.sge32[0].phys_addr = ci_h;
2233 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
2234
2235 if (!megasas_issue_polled(instance, cmd)) {
2236 ret = 0;
2237 } else {
2238 ret = -1;
2239 }
2240
2241 /*
2242 * the following function will get the instance PD LIST.
2243 */
2244
2245 pd_addr = ci->addr;
2246
2247 if ( ret == 0 &&
2248 (ci->count <
2249 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
2250
2251 memset(instance->pd_list, 0,
2252 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
2253
2254 for (pd_index = 0; pd_index < ci->count; pd_index++) {
2255
2256 instance->pd_list[pd_addr->deviceId].tid =
2257 pd_addr->deviceId;
2258 instance->pd_list[pd_addr->deviceId].driveType =
2259 pd_addr->scsiDevType;
2260 instance->pd_list[pd_addr->deviceId].driveState =
2261 MR_PD_STATE_SYSTEM;
2262 pd_addr++;
2263 }
2264 }
2265
2266 pci_free_consistent(instance->pdev,
2267 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
2268 ci, ci_h);
2269 megasas_return_cmd(instance, cmd);
2270
2271 return ret;
2272}
2273
2274
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002275/**
2276 * megasas_get_controller_info - Returns FW's controller structure
2277 * @instance: Adapter soft state
2278 * @ctrl_info: Controller information structure
2279 *
2280 * Issues an internal command (DCMD) to get the FW's controller structure.
2281 * This information is mainly used to find out the maximum IO transfer per
2282 * command supported by the FW.
2283 */
2284static int
2285megasas_get_ctrl_info(struct megasas_instance *instance,
2286 struct megasas_ctrl_info *ctrl_info)
2287{
2288 int ret = 0;
2289 struct megasas_cmd *cmd;
2290 struct megasas_dcmd_frame *dcmd;
2291 struct megasas_ctrl_info *ci;
2292 dma_addr_t ci_h = 0;
2293
2294 cmd = megasas_get_cmd(instance);
2295
2296 if (!cmd) {
2297 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
2298 return -ENOMEM;
2299 }
2300
2301 dcmd = &cmd->frame->dcmd;
2302
2303 ci = pci_alloc_consistent(instance->pdev,
2304 sizeof(struct megasas_ctrl_info), &ci_h);
2305
2306 if (!ci) {
2307 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
2308 megasas_return_cmd(instance, cmd);
2309 return -ENOMEM;
2310 }
2311
2312 memset(ci, 0, sizeof(*ci));
2313 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2314
2315 dcmd->cmd = MFI_CMD_DCMD;
2316 dcmd->cmd_status = 0xFF;
2317 dcmd->sge_count = 1;
2318 dcmd->flags = MFI_FRAME_DIR_READ;
2319 dcmd->timeout = 0;
2320 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
2321 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
2322 dcmd->sgl.sge32[0].phys_addr = ci_h;
2323 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
2324
2325 if (!megasas_issue_polled(instance, cmd)) {
2326 ret = 0;
2327 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
2328 } else {
2329 ret = -1;
2330 }
2331
2332 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
2333 ci, ci_h);
2334
2335 megasas_return_cmd(instance, cmd);
2336 return ret;
2337}
2338
2339/**
bo yang31ea7082007-11-07 12:09:50 -05002340 * megasas_issue_init_mfi - Initializes the FW
2341 * @instance: Adapter soft state
2342 *
2343 * Issues the INIT MFI cmd
2344 */
2345static int
2346megasas_issue_init_mfi(struct megasas_instance *instance)
2347{
2348 u32 context;
2349
2350 struct megasas_cmd *cmd;
2351
2352 struct megasas_init_frame *init_frame;
2353 struct megasas_init_queue_info *initq_info;
2354 dma_addr_t init_frame_h;
2355 dma_addr_t initq_info_h;
2356
2357 /*
2358 * Prepare a init frame. Note the init frame points to queue info
2359 * structure. Each frame has SGL allocated after first 64 bytes. For
2360 * this frame - since we don't need any SGL - we use SGL's space as
2361 * queue info structure
2362 *
2363 * We will not get a NULL command below. We just created the pool.
2364 */
2365 cmd = megasas_get_cmd(instance);
2366
2367 init_frame = (struct megasas_init_frame *)cmd->frame;
2368 initq_info = (struct megasas_init_queue_info *)
2369 ((unsigned long)init_frame + 64);
2370
2371 init_frame_h = cmd->frame_phys_addr;
2372 initq_info_h = init_frame_h + 64;
2373
2374 context = init_frame->context;
2375 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
2376 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
2377 init_frame->context = context;
2378
2379 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
2380 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
2381
2382 initq_info->producer_index_phys_addr_lo = instance->producer_h;
2383 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
2384
2385 init_frame->cmd = MFI_CMD_INIT;
2386 init_frame->cmd_status = 0xFF;
2387 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
2388
2389 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
2390
2391 /*
2392 * disable the intr before firing the init frame to FW
2393 */
2394 instance->instancet->disable_intr(instance->reg_set);
2395
2396 /*
2397 * Issue the init frame in polled mode
2398 */
2399
2400 if (megasas_issue_polled(instance, cmd)) {
2401 printk(KERN_ERR "megasas: Failed to init firmware\n");
2402 megasas_return_cmd(instance, cmd);
2403 goto fail_fw_init;
2404 }
2405
2406 megasas_return_cmd(instance, cmd);
2407
2408 return 0;
2409
2410fail_fw_init:
2411 return -EINVAL;
2412}
2413
2414/**
bo yangad84db22007-11-09 04:40:16 -05002415 * megasas_start_timer - Initializes a timer object
2416 * @instance: Adapter soft state
2417 * @timer: timer object to be initialized
2418 * @fn: timer function
2419 * @interval: time interval between timer function call
2420 */
2421static inline void
2422megasas_start_timer(struct megasas_instance *instance,
2423 struct timer_list *timer,
2424 void *fn, unsigned long interval)
2425{
2426 init_timer(timer);
2427 timer->expires = jiffies + interval;
2428 timer->data = (unsigned long)instance;
2429 timer->function = fn;
2430 add_timer(timer);
2431}
2432
2433/**
2434 * megasas_io_completion_timer - Timer fn
2435 * @instance_addr: Address of adapter soft state
2436 *
2437 * Schedules tasklet for cmd completion
2438 * if poll_mode_io is set
2439 */
2440static void
2441megasas_io_completion_timer(unsigned long instance_addr)
2442{
2443 struct megasas_instance *instance =
2444 (struct megasas_instance *)instance_addr;
2445
2446 if (atomic_read(&instance->fw_outstanding))
2447 tasklet_schedule(&instance->isr_tasklet);
2448
2449 /* Restart timer */
2450 if (poll_mode_io)
2451 mod_timer(&instance->io_completion_timer,
2452 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
2453}
2454
2455/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002456 * megasas_init_mfi - Initializes the FW
2457 * @instance: Adapter soft state
2458 *
2459 * This is the main function for initializing MFI firmware.
2460 */
2461static int megasas_init_mfi(struct megasas_instance *instance)
2462{
2463 u32 context_sz;
2464 u32 reply_q_sz;
2465 u32 max_sectors_1;
2466 u32 max_sectors_2;
bo yang14faea92007-11-09 04:14:00 -05002467 u32 tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002468 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002469 struct megasas_ctrl_info *ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002470 /*
2471 * Map the message registers
2472 */
Yang, Bo6610a6b2008-08-10 12:42:38 -07002473 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
Yang, Bo87911122009-10-06 14:31:54 -06002474 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2475 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Yang, Bo6610a6b2008-08-10 12:42:38 -07002476 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0079GEN2)) {
2477 instance->base_addr = pci_resource_start(instance->pdev, 1);
2478 } else {
2479 instance->base_addr = pci_resource_start(instance->pdev, 0);
2480 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002481
bo yangf28cd7c2007-11-09 04:44:56 -05002482 if (pci_request_regions(instance->pdev, "megasas: LSI")) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002483 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
2484 return -EBUSY;
2485 }
2486
2487 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
2488
2489 if (!instance->reg_set) {
2490 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
2491 goto fail_ioremap;
2492 }
2493
2494 reg_set = instance->reg_set;
2495
Sumant Patrof9876f02006-02-03 15:34:35 -08002496 switch(instance->pdev->device)
2497 {
bo yangaf7a5642008-03-17 04:13:07 -04002498 case PCI_DEVICE_ID_LSI_SAS1078R:
2499 case PCI_DEVICE_ID_LSI_SAS1078DE:
Sumant Patrof9876f02006-02-03 15:34:35 -08002500 instance->instancet = &megasas_instance_template_ppc;
2501 break;
Yang, Bo6610a6b2008-08-10 12:42:38 -07002502 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
2503 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
2504 instance->instancet = &megasas_instance_template_gen2;
2505 break;
Yang, Bo87911122009-10-06 14:31:54 -06002506 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
2507 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
2508 instance->instancet = &megasas_instance_template_skinny;
2509 break;
Sumant Patrof9876f02006-02-03 15:34:35 -08002510 case PCI_DEVICE_ID_LSI_SAS1064R:
2511 case PCI_DEVICE_ID_DELL_PERC5:
2512 default:
2513 instance->instancet = &megasas_instance_template_xscale;
2514 break;
2515 }
Sumant Patro1341c932006-01-25 12:02:40 -08002516
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002517 /*
2518 * We expect the FW state to be READY
2519 */
Sumant Patro1341c932006-01-25 12:02:40 -08002520 if (megasas_transition_to_ready(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002521 goto fail_ready_state;
2522
2523 /*
2524 * Get various operational parameters from status register
2525 */
Sumant Patro1341c932006-01-25 12:02:40 -08002526 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07002527 /*
2528 * Reduce the max supported cmds by 1. This is to ensure that the
2529 * reply_q_sz (1 more than the max cmd that driver may send)
2530 * does not exceed max cmds that the FW can support
2531 */
2532 instance->max_fw_cmds = instance->max_fw_cmds-1;
Sumant Patro1341c932006-01-25 12:02:40 -08002533 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
2534 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002535 /*
2536 * Create a pool of commands
2537 */
2538 if (megasas_alloc_cmds(instance))
2539 goto fail_alloc_cmds;
2540
2541 /*
2542 * Allocate memory for reply queue. Length of reply queue should
2543 * be _one_ more than the maximum commands handled by the firmware.
2544 *
2545 * Note: When FW completes commands, it places corresponding contex
2546 * values in this circular reply queue. This circular queue is a fairly
2547 * typical producer-consumer queue. FW is the producer (of completed
2548 * commands) and the driver is the consumer.
2549 */
2550 context_sz = sizeof(u32);
2551 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
2552
2553 instance->reply_queue = pci_alloc_consistent(instance->pdev,
2554 reply_q_sz,
2555 &instance->reply_queue_h);
2556
2557 if (!instance->reply_queue) {
2558 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
2559 goto fail_reply_queue;
2560 }
2561
bo yang31ea7082007-11-07 12:09:50 -05002562 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002563 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002564
Yang, Bo81e403c2009-10-06 14:27:54 -06002565 memset(instance->pd_list, 0 ,
2566 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
2567 megasas_get_pd_list(instance);
2568
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002569 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
2570
2571 /*
2572 * Compute the max allowed sectors per IO: The controller info has two
2573 * limits on max sectors. Driver should use the minimum of these two.
2574 *
2575 * 1 << stripe_sz_ops.min = max sectors per strip
2576 *
2577 * Note that older firmwares ( < FW ver 30) didn't report information
2578 * to calculate max_sectors_1. So the number ended up as zero always.
2579 */
bo yang14faea92007-11-09 04:14:00 -05002580 tmp_sectors = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002581 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
2582
2583 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
2584 ctrl_info->max_strips_per_io;
2585 max_sectors_2 = ctrl_info->max_request_size;
2586
bo yang14faea92007-11-09 04:14:00 -05002587 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
2588 }
2589
2590 instance->max_sectors_per_req = instance->max_num_sge *
2591 PAGE_SIZE / 512;
2592 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
2593 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002594
2595 kfree(ctrl_info);
2596
Sumant Patro5d018ad2006-10-03 13:13:18 -07002597 /*
2598 * Setup tasklet for cmd completion
2599 */
2600
bo yangad84db22007-11-09 04:40:16 -05002601 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2602 (unsigned long)instance);
2603
2604 /* Initialize the cmd completion timer */
2605 if (poll_mode_io)
2606 megasas_start_timer(instance, &instance->io_completion_timer,
2607 megasas_io_completion_timer,
2608 MEGASAS_COMPLETION_TIMER_INTERVAL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002609 return 0;
2610
2611 fail_fw_init:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002612
2613 pci_free_consistent(instance->pdev, reply_q_sz,
2614 instance->reply_queue, instance->reply_queue_h);
2615 fail_reply_queue:
2616 megasas_free_cmds(instance);
2617
2618 fail_alloc_cmds:
2619 fail_ready_state:
2620 iounmap(instance->reg_set);
2621
2622 fail_ioremap:
2623 pci_release_regions(instance->pdev);
2624
2625 return -EINVAL;
2626}
2627
2628/**
2629 * megasas_release_mfi - Reverses the FW initialization
2630 * @intance: Adapter soft state
2631 */
2632static void megasas_release_mfi(struct megasas_instance *instance)
2633{
2634 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
2635
2636 pci_free_consistent(instance->pdev, reply_q_sz,
2637 instance->reply_queue, instance->reply_queue_h);
2638
2639 megasas_free_cmds(instance);
2640
2641 iounmap(instance->reg_set);
2642
2643 pci_release_regions(instance->pdev);
2644}
2645
2646/**
2647 * megasas_get_seq_num - Gets latest event sequence numbers
2648 * @instance: Adapter soft state
2649 * @eli: FW event log sequence numbers information
2650 *
2651 * FW maintains a log of all events in a non-volatile area. Upper layers would
2652 * usually find out the latest sequence number of the events, the seq number at
2653 * the boot etc. They would "read" all the events below the latest seq number
2654 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
2655 * number), they would subsribe to AEN (asynchronous event notification) and
2656 * wait for the events to happen.
2657 */
2658static int
2659megasas_get_seq_num(struct megasas_instance *instance,
2660 struct megasas_evt_log_info *eli)
2661{
2662 struct megasas_cmd *cmd;
2663 struct megasas_dcmd_frame *dcmd;
2664 struct megasas_evt_log_info *el_info;
2665 dma_addr_t el_info_h = 0;
2666
2667 cmd = megasas_get_cmd(instance);
2668
2669 if (!cmd) {
2670 return -ENOMEM;
2671 }
2672
2673 dcmd = &cmd->frame->dcmd;
2674 el_info = pci_alloc_consistent(instance->pdev,
2675 sizeof(struct megasas_evt_log_info),
2676 &el_info_h);
2677
2678 if (!el_info) {
2679 megasas_return_cmd(instance, cmd);
2680 return -ENOMEM;
2681 }
2682
2683 memset(el_info, 0, sizeof(*el_info));
2684 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2685
2686 dcmd->cmd = MFI_CMD_DCMD;
2687 dcmd->cmd_status = 0x0;
2688 dcmd->sge_count = 1;
2689 dcmd->flags = MFI_FRAME_DIR_READ;
2690 dcmd->timeout = 0;
2691 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
2692 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
2693 dcmd->sgl.sge32[0].phys_addr = el_info_h;
2694 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
2695
2696 megasas_issue_blocked_cmd(instance, cmd);
2697
2698 /*
2699 * Copy the data back into callers buffer
2700 */
2701 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
2702
2703 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
2704 el_info, el_info_h);
2705
2706 megasas_return_cmd(instance, cmd);
2707
2708 return 0;
2709}
2710
2711/**
2712 * megasas_register_aen - Registers for asynchronous event notification
2713 * @instance: Adapter soft state
2714 * @seq_num: The starting sequence number
2715 * @class_locale: Class of the event
2716 *
2717 * This function subscribes for AEN for events beyond the @seq_num. It requests
2718 * to be notified if and only if the event is of type @class_locale
2719 */
2720static int
2721megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
2722 u32 class_locale_word)
2723{
2724 int ret_val;
2725 struct megasas_cmd *cmd;
2726 struct megasas_dcmd_frame *dcmd;
2727 union megasas_evt_class_locale curr_aen;
2728 union megasas_evt_class_locale prev_aen;
2729
2730 /*
2731 * If there an AEN pending already (aen_cmd), check if the
2732 * class_locale of that pending AEN is inclusive of the new
2733 * AEN request we currently have. If it is, then we don't have
2734 * to do anything. In other words, whichever events the current
2735 * AEN request is subscribing to, have already been subscribed
2736 * to.
2737 *
2738 * If the old_cmd is _not_ inclusive, then we have to abort
2739 * that command, form a class_locale that is superset of both
2740 * old and current and re-issue to the FW
2741 */
2742
2743 curr_aen.word = class_locale_word;
2744
2745 if (instance->aen_cmd) {
2746
2747 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
2748
2749 /*
2750 * A class whose enum value is smaller is inclusive of all
2751 * higher values. If a PROGRESS (= -1) was previously
2752 * registered, then a new registration requests for higher
2753 * classes need not be sent to FW. They are automatically
2754 * included.
2755 *
2756 * Locale numbers don't have such hierarchy. They are bitmap
2757 * values
2758 */
2759 if ((prev_aen.members.class <= curr_aen.members.class) &&
2760 !((prev_aen.members.locale & curr_aen.members.locale) ^
2761 curr_aen.members.locale)) {
2762 /*
2763 * Previously issued event registration includes
2764 * current request. Nothing to do.
2765 */
2766 return 0;
2767 } else {
2768 curr_aen.members.locale |= prev_aen.members.locale;
2769
2770 if (prev_aen.members.class < curr_aen.members.class)
2771 curr_aen.members.class = prev_aen.members.class;
2772
2773 instance->aen_cmd->abort_aen = 1;
2774 ret_val = megasas_issue_blocked_abort_cmd(instance,
2775 instance->
2776 aen_cmd);
2777
2778 if (ret_val) {
2779 printk(KERN_DEBUG "megasas: Failed to abort "
2780 "previous AEN command\n");
2781 return ret_val;
2782 }
2783 }
2784 }
2785
2786 cmd = megasas_get_cmd(instance);
2787
2788 if (!cmd)
2789 return -ENOMEM;
2790
2791 dcmd = &cmd->frame->dcmd;
2792
2793 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
2794
2795 /*
2796 * Prepare DCMD for aen registration
2797 */
2798 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2799
2800 dcmd->cmd = MFI_CMD_DCMD;
2801 dcmd->cmd_status = 0x0;
2802 dcmd->sge_count = 1;
2803 dcmd->flags = MFI_FRAME_DIR_READ;
2804 dcmd->timeout = 0;
2805 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
2806 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
2807 dcmd->mbox.w[0] = seq_num;
2808 dcmd->mbox.w[1] = curr_aen.word;
2809 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
2810 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
2811
Yang, Bof4c9a132009-10-06 14:43:28 -06002812 if (instance->aen_cmd != NULL) {
2813 megasas_return_cmd(instance, cmd);
2814 return 0;
2815 }
2816
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002817 /*
2818 * Store reference to the cmd used to register for AEN. When an
2819 * application wants us to register for AEN, we have to abort this
2820 * cmd and re-register with a new EVENT LOCALE supplied by that app
2821 */
2822 instance->aen_cmd = cmd;
2823
2824 /*
2825 * Issue the aen registration frame
2826 */
Yang, Bo0c79e682009-10-06 14:47:35 -06002827 instance->instancet->fire_cmd(instance,
2828 cmd->frame_phys_addr, 0, instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002829
2830 return 0;
2831}
2832
2833/**
2834 * megasas_start_aen - Subscribes to AEN during driver load time
2835 * @instance: Adapter soft state
2836 */
2837static int megasas_start_aen(struct megasas_instance *instance)
2838{
2839 struct megasas_evt_log_info eli;
2840 union megasas_evt_class_locale class_locale;
2841
2842 /*
2843 * Get the latest sequence number from FW
2844 */
2845 memset(&eli, 0, sizeof(eli));
2846
2847 if (megasas_get_seq_num(instance, &eli))
2848 return -1;
2849
2850 /*
2851 * Register AEN with FW for latest sequence number plus 1
2852 */
2853 class_locale.members.reserved = 0;
2854 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2855 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2856
2857 return megasas_register_aen(instance, eli.newest_seq_num + 1,
2858 class_locale.word);
2859}
2860
2861/**
2862 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2863 * @instance: Adapter soft state
2864 */
2865static int megasas_io_attach(struct megasas_instance *instance)
2866{
2867 struct Scsi_Host *host = instance->host;
2868
2869 /*
2870 * Export parameters required by SCSI mid-layer
2871 */
2872 host->irq = instance->pdev->irq;
2873 host->unique_id = instance->unique_id;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06002874 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2875 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2876 host->can_queue =
2877 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
2878 } else
2879 host->can_queue =
2880 instance->max_fw_cmds - MEGASAS_INT_CMDS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002881 host->this_id = instance->init_id;
2882 host->sg_tablesize = instance->max_num_sge;
2883 host->max_sectors = instance->max_sectors_per_req;
2884 host->cmd_per_lun = 128;
2885 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
2886 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
2887 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08002888 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002889
2890 /*
2891 * Notify the mid-layer about the new controller
2892 */
2893 if (scsi_add_host(host, &instance->pdev->dev)) {
2894 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2895 return -ENODEV;
2896 }
2897
2898 /*
2899 * Trigger SCSI to scan our drives
2900 */
2901 scsi_scan_host(host);
2902 return 0;
2903}
2904
bo yang31ea7082007-11-07 12:09:50 -05002905static int
2906megasas_set_dma_mask(struct pci_dev *pdev)
2907{
2908 /*
2909 * All our contollers are capable of performing 64-bit DMA
2910 */
2911 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07002912 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05002913
Yang Hongyang284901a2009-04-06 19:01:15 -07002914 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05002915 goto fail_set_dma_mask;
2916 }
2917 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07002918 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05002919 goto fail_set_dma_mask;
2920 }
2921 return 0;
2922
2923fail_set_dma_mask:
2924 return 1;
2925}
2926
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002927/**
2928 * megasas_probe_one - PCI hotplug entry point
2929 * @pdev: PCI device structure
2930 * @id: PCI ids of supported hotplugged adapter
2931 */
2932static int __devinit
2933megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2934{
2935 int rval;
2936 struct Scsi_Host *host;
2937 struct megasas_instance *instance;
2938
2939 /*
2940 * Announce PCI information
2941 */
2942 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2943 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2944 pdev->subsystem_device);
2945
2946 printk("bus %d:slot %d:func %d\n",
2947 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2948
2949 /*
2950 * PCI prepping: enable device set bus mastering and dma mask
2951 */
2952 rval = pci_enable_device(pdev);
2953
2954 if (rval) {
2955 return rval;
2956 }
2957
2958 pci_set_master(pdev);
2959
bo yang31ea7082007-11-07 12:09:50 -05002960 if (megasas_set_dma_mask(pdev))
2961 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002962
2963 host = scsi_host_alloc(&megasas_template,
2964 sizeof(struct megasas_instance));
2965
2966 if (!host) {
2967 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2968 goto fail_alloc_instance;
2969 }
2970
2971 instance = (struct megasas_instance *)host->hostdata;
2972 memset(instance, 0, sizeof(*instance));
2973
2974 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2975 &instance->producer_h);
2976 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2977 &instance->consumer_h);
2978
2979 if (!instance->producer || !instance->consumer) {
2980 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2981 "producer, consumer\n");
2982 goto fail_alloc_dma_buf;
2983 }
2984
2985 *instance->producer = 0;
2986 *instance->consumer = 0;
Yang, Boc3518832009-10-06 14:18:02 -06002987 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06002988 instance->flag_ieee = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002989
2990 instance->evt_detail = pci_alloc_consistent(pdev,
2991 sizeof(struct
2992 megasas_evt_detail),
2993 &instance->evt_detail_h);
2994
2995 if (!instance->evt_detail) {
2996 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2997 "event detail structure\n");
2998 goto fail_alloc_dma_buf;
2999 }
3000
3001 /*
3002 * Initialize locks and queues
3003 */
3004 INIT_LIST_HEAD(&instance->cmd_pool);
3005
Sumant Patroe4a082c2006-05-30 12:03:37 -07003006 atomic_set(&instance->fw_outstanding,0);
3007
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003008 init_waitqueue_head(&instance->int_cmd_wait_q);
3009 init_waitqueue_head(&instance->abort_cmd_wait_q);
3010
3011 spin_lock_init(&instance->cmd_pool_lock);
Yang, Bo0c79e682009-10-06 14:47:35 -06003012 spin_lock_init(&instance->fire_lock);
bo yang7343eb62007-11-09 04:35:44 -05003013 spin_lock_init(&instance->completion_lock);
Yang, Boc3518832009-10-06 14:18:02 -06003014 spin_lock_init(&poll_aen_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003015
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02003016 mutex_init(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003017
3018 /*
3019 * Initialize PCI related and misc parameters
3020 */
3021 instance->pdev = pdev;
3022 instance->host = host;
3023 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
3024 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
3025
Yang, Bo7bebf5c2009-10-06 14:40:58 -06003026 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3027 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06003028 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06003029 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
3030 } else
3031 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
3032
Sumant Patro658dced2006-10-03 13:09:14 -07003033 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003034 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06003035 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003036 instance->last_time = 0;
Sumant Patro658dced2006-10-03 13:09:14 -07003037
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003038 /*
3039 * Initialize MFI Firmware
3040 */
3041 if (megasas_init_mfi(instance))
3042 goto fail_init_mfi;
3043
3044 /*
3045 * Register IRQ
3046 */
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07003047 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003048 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
3049 goto fail_irq;
3050 }
3051
Sumant Patro1341c932006-01-25 12:02:40 -08003052 instance->instancet->enable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003053
3054 /*
3055 * Store instance in PCI softstate
3056 */
3057 pci_set_drvdata(pdev, instance);
3058
3059 /*
3060 * Add this controller to megasas_mgmt_info structure so that it
3061 * can be exported to management applications
3062 */
3063 megasas_mgmt_info.count++;
3064 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
3065 megasas_mgmt_info.max_index++;
3066
3067 /*
3068 * Initiate AEN (Asynchronous Event Notification)
3069 */
3070 if (megasas_start_aen(instance)) {
3071 printk(KERN_DEBUG "megasas: start aen failed\n");
3072 goto fail_start_aen;
3073 }
3074
3075 /*
3076 * Register with SCSI mid-layer
3077 */
3078 if (megasas_io_attach(instance))
3079 goto fail_io_attach;
3080
Yang, Bo0c79e682009-10-06 14:47:35 -06003081 instance->unload = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003082 return 0;
3083
3084 fail_start_aen:
3085 fail_io_attach:
3086 megasas_mgmt_info.count--;
3087 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
3088 megasas_mgmt_info.max_index--;
3089
3090 pci_set_drvdata(pdev, NULL);
Sumant Patrob274cab2006-10-03 12:52:12 -07003091 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003092 free_irq(instance->pdev->irq, instance);
3093
3094 megasas_release_mfi(instance);
3095
3096 fail_irq:
3097 fail_init_mfi:
3098 fail_alloc_dma_buf:
3099 if (instance->evt_detail)
3100 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3101 instance->evt_detail,
3102 instance->evt_detail_h);
3103
3104 if (instance->producer)
3105 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3106 instance->producer_h);
3107 if (instance->consumer)
3108 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3109 instance->consumer_h);
3110 scsi_host_put(host);
3111
3112 fail_alloc_instance:
3113 fail_set_dma_mask:
3114 pci_disable_device(pdev);
3115
3116 return -ENODEV;
3117}
3118
3119/**
3120 * megasas_flush_cache - Requests FW to flush all its caches
3121 * @instance: Adapter soft state
3122 */
3123static void megasas_flush_cache(struct megasas_instance *instance)
3124{
3125 struct megasas_cmd *cmd;
3126 struct megasas_dcmd_frame *dcmd;
3127
3128 cmd = megasas_get_cmd(instance);
3129
3130 if (!cmd)
3131 return;
3132
3133 dcmd = &cmd->frame->dcmd;
3134
3135 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3136
3137 dcmd->cmd = MFI_CMD_DCMD;
3138 dcmd->cmd_status = 0x0;
3139 dcmd->sge_count = 0;
3140 dcmd->flags = MFI_FRAME_DIR_NONE;
3141 dcmd->timeout = 0;
3142 dcmd->data_xfer_len = 0;
3143 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
3144 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
3145
3146 megasas_issue_blocked_cmd(instance, cmd);
3147
3148 megasas_return_cmd(instance, cmd);
3149
3150 return;
3151}
3152
3153/**
3154 * megasas_shutdown_controller - Instructs FW to shutdown the controller
3155 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05003156 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003157 */
bo yang31ea7082007-11-07 12:09:50 -05003158static void megasas_shutdown_controller(struct megasas_instance *instance,
3159 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003160{
3161 struct megasas_cmd *cmd;
3162 struct megasas_dcmd_frame *dcmd;
3163
3164 cmd = megasas_get_cmd(instance);
3165
3166 if (!cmd)
3167 return;
3168
3169 if (instance->aen_cmd)
3170 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
3171
3172 dcmd = &cmd->frame->dcmd;
3173
3174 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3175
3176 dcmd->cmd = MFI_CMD_DCMD;
3177 dcmd->cmd_status = 0x0;
3178 dcmd->sge_count = 0;
3179 dcmd->flags = MFI_FRAME_DIR_NONE;
3180 dcmd->timeout = 0;
3181 dcmd->data_xfer_len = 0;
bo yang31ea7082007-11-07 12:09:50 -05003182 dcmd->opcode = opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003183
3184 megasas_issue_blocked_cmd(instance, cmd);
3185
3186 megasas_return_cmd(instance, cmd);
3187
3188 return;
3189}
3190
Jiri Slaby33139b22008-05-01 17:56:02 +02003191#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003192/**
bo yangad84db22007-11-09 04:40:16 -05003193 * megasas_suspend - driver suspend entry point
3194 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05003195 * @state: PCI power state to suspend routine
3196 */
Jiri Slaby33139b22008-05-01 17:56:02 +02003197static int
bo yang31ea7082007-11-07 12:09:50 -05003198megasas_suspend(struct pci_dev *pdev, pm_message_t state)
3199{
3200 struct Scsi_Host *host;
3201 struct megasas_instance *instance;
3202
3203 instance = pci_get_drvdata(pdev);
3204 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06003205 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05003206
bo yangad84db22007-11-09 04:40:16 -05003207 if (poll_mode_io)
3208 del_timer_sync(&instance->io_completion_timer);
3209
bo yang31ea7082007-11-07 12:09:50 -05003210 megasas_flush_cache(instance);
3211 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
3212 tasklet_kill(&instance->isr_tasklet);
3213
3214 pci_set_drvdata(instance->pdev, instance);
3215 instance->instancet->disable_intr(instance->reg_set);
3216 free_irq(instance->pdev->irq, instance);
3217
3218 pci_save_state(pdev);
3219 pci_disable_device(pdev);
3220
3221 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3222
3223 return 0;
3224}
3225
3226/**
3227 * megasas_resume- driver resume entry point
3228 * @pdev: PCI device structure
3229 */
Jiri Slaby33139b22008-05-01 17:56:02 +02003230static int
bo yang31ea7082007-11-07 12:09:50 -05003231megasas_resume(struct pci_dev *pdev)
3232{
3233 int rval;
3234 struct Scsi_Host *host;
3235 struct megasas_instance *instance;
3236
3237 instance = pci_get_drvdata(pdev);
3238 host = instance->host;
3239 pci_set_power_state(pdev, PCI_D0);
3240 pci_enable_wake(pdev, PCI_D0, 0);
3241 pci_restore_state(pdev);
3242
3243 /*
3244 * PCI prepping: enable device set bus mastering and dma mask
3245 */
3246 rval = pci_enable_device(pdev);
3247
3248 if (rval) {
3249 printk(KERN_ERR "megasas: Enable device failed\n");
3250 return rval;
3251 }
3252
3253 pci_set_master(pdev);
3254
3255 if (megasas_set_dma_mask(pdev))
3256 goto fail_set_dma_mask;
3257
3258 /*
3259 * Initialize MFI Firmware
3260 */
3261
3262 *instance->producer = 0;
3263 *instance->consumer = 0;
3264
3265 atomic_set(&instance->fw_outstanding, 0);
3266
3267 /*
3268 * We expect the FW state to be READY
3269 */
3270 if (megasas_transition_to_ready(instance))
3271 goto fail_ready_state;
3272
3273 if (megasas_issue_init_mfi(instance))
3274 goto fail_init_mfi;
3275
3276 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
3277 (unsigned long)instance);
3278
3279 /*
3280 * Register IRQ
3281 */
3282 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED,
3283 "megasas", instance)) {
3284 printk(KERN_ERR "megasas: Failed to register IRQ\n");
3285 goto fail_irq;
3286 }
3287
3288 instance->instancet->enable_intr(instance->reg_set);
3289
3290 /*
3291 * Initiate AEN (Asynchronous Event Notification)
3292 */
3293 if (megasas_start_aen(instance))
3294 printk(KERN_ERR "megasas: Start AEN failed\n");
3295
bo yangad84db22007-11-09 04:40:16 -05003296 /* Initialize the cmd completion timer */
3297 if (poll_mode_io)
3298 megasas_start_timer(instance, &instance->io_completion_timer,
3299 megasas_io_completion_timer,
3300 MEGASAS_COMPLETION_TIMER_INTERVAL);
Yang, Bo0c79e682009-10-06 14:47:35 -06003301 instance->unload = 0;
3302
bo yang31ea7082007-11-07 12:09:50 -05003303 return 0;
3304
3305fail_irq:
3306fail_init_mfi:
3307 if (instance->evt_detail)
3308 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3309 instance->evt_detail,
3310 instance->evt_detail_h);
3311
3312 if (instance->producer)
3313 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3314 instance->producer_h);
3315 if (instance->consumer)
3316 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3317 instance->consumer_h);
3318 scsi_host_put(host);
3319
3320fail_set_dma_mask:
3321fail_ready_state:
3322
3323 pci_disable_device(pdev);
3324
3325 return -ENODEV;
3326}
Jiri Slaby33139b22008-05-01 17:56:02 +02003327#else
3328#define megasas_suspend NULL
3329#define megasas_resume NULL
3330#endif
bo yang31ea7082007-11-07 12:09:50 -05003331
3332/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003333 * megasas_detach_one - PCI hot"un"plug entry point
3334 * @pdev: PCI device structure
3335 */
Jiri Slaby33139b22008-05-01 17:56:02 +02003336static void __devexit megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003337{
3338 int i;
3339 struct Scsi_Host *host;
3340 struct megasas_instance *instance;
3341
3342 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06003343 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003344 host = instance->host;
3345
bo yangad84db22007-11-09 04:40:16 -05003346 if (poll_mode_io)
3347 del_timer_sync(&instance->io_completion_timer);
3348
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003349 scsi_remove_host(instance->host);
3350 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05003351 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Sumant Patro5d018ad2006-10-03 13:13:18 -07003352 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003353
3354 /*
3355 * Take the instance off the instance array. Note that we will not
3356 * decrement the max_index. We let this array be sparse array
3357 */
3358 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3359 if (megasas_mgmt_info.instance[i] == instance) {
3360 megasas_mgmt_info.count--;
3361 megasas_mgmt_info.instance[i] = NULL;
3362
3363 break;
3364 }
3365 }
3366
3367 pci_set_drvdata(instance->pdev, NULL);
3368
Sumant Patrob274cab2006-10-03 12:52:12 -07003369 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003370
3371 free_irq(instance->pdev->irq, instance);
3372
3373 megasas_release_mfi(instance);
3374
3375 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3376 instance->evt_detail, instance->evt_detail_h);
3377
3378 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3379 instance->producer_h);
3380
3381 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3382 instance->consumer_h);
3383
3384 scsi_host_put(host);
3385
3386 pci_set_drvdata(pdev, NULL);
3387
3388 pci_disable_device(pdev);
3389
3390 return;
3391}
3392
3393/**
3394 * megasas_shutdown - Shutdown entry point
3395 * @device: Generic device structure
3396 */
3397static void megasas_shutdown(struct pci_dev *pdev)
3398{
3399 struct megasas_instance *instance = pci_get_drvdata(pdev);
Yang, Bo0c79e682009-10-06 14:47:35 -06003400 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003401 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07003402 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003403}
3404
3405/**
3406 * megasas_mgmt_open - char node "open" entry point
3407 */
3408static int megasas_mgmt_open(struct inode *inode, struct file *filep)
3409{
Jonathan Corbetf2b98572008-05-18 15:32:43 -06003410 cycle_kernel_lock();
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003411 /*
3412 * Allow only those users with admin rights
3413 */
3414 if (!capable(CAP_SYS_ADMIN))
3415 return -EACCES;
3416
3417 return 0;
3418}
3419
3420/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003421 * megasas_mgmt_fasync - Async notifier registration from applications
3422 *
3423 * This function adds the calling process to a driver global queue. When an
3424 * event occurs, SIGIO will be sent to all processes in this queue.
3425 */
3426static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
3427{
3428 int rc;
3429
Arjan van de Ven0b950672006-01-11 13:16:10 +01003430 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003431
3432 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
3433
Arjan van de Ven0b950672006-01-11 13:16:10 +01003434 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003435
3436 if (rc >= 0) {
3437 /* For sanity check when we get ioctl */
3438 filep->private_data = filep;
3439 return 0;
3440 }
3441
3442 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
3443
3444 return rc;
3445}
3446
3447/**
Yang, Boc3518832009-10-06 14:18:02 -06003448 * megasas_mgmt_poll - char node "poll" entry point
3449 * */
3450static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
3451{
3452 unsigned int mask;
3453 unsigned long flags;
3454 poll_wait(file, &megasas_poll_wait, wait);
3455 spin_lock_irqsave(&poll_aen_lock, flags);
3456 if (megasas_poll_wait_aen)
3457 mask = (POLLIN | POLLRDNORM);
3458 else
3459 mask = 0;
3460 spin_unlock_irqrestore(&poll_aen_lock, flags);
3461 return mask;
3462}
3463
3464/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003465 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
3466 * @instance: Adapter soft state
3467 * @argp: User's ioctl packet
3468 */
3469static int
3470megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
3471 struct megasas_iocpacket __user * user_ioc,
3472 struct megasas_iocpacket *ioc)
3473{
3474 struct megasas_sge32 *kern_sge32;
3475 struct megasas_cmd *cmd;
3476 void *kbuff_arr[MAX_IOCTL_SGE];
3477 dma_addr_t buf_handle = 0;
3478 int error = 0, i;
3479 void *sense = NULL;
3480 dma_addr_t sense_handle;
3481 u32 *sense_ptr;
3482
3483 memset(kbuff_arr, 0, sizeof(kbuff_arr));
3484
3485 if (ioc->sge_count > MAX_IOCTL_SGE) {
3486 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
3487 ioc->sge_count, MAX_IOCTL_SGE);
3488 return -EINVAL;
3489 }
3490
3491 cmd = megasas_get_cmd(instance);
3492 if (!cmd) {
3493 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
3494 return -ENOMEM;
3495 }
3496
3497 /*
3498 * User's IOCTL packet has 2 frames (maximum). Copy those two
3499 * frames into our cmd's frames. cmd->frame's context will get
3500 * overwritten when we copy from user's frames. So set that value
3501 * alone separately
3502 */
3503 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
3504 cmd->frame->hdr.context = cmd->index;
Yang, Boc3518832009-10-06 14:18:02 -06003505 cmd->frame->hdr.pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003506
3507 /*
3508 * The management interface between applications and the fw uses
3509 * MFI frames. E.g, RAID configuration changes, LD property changes
3510 * etc are accomplishes through different kinds of MFI frames. The
3511 * driver needs to care only about substituting user buffers with
3512 * kernel buffers in SGLs. The location of SGL is embedded in the
3513 * struct iocpacket itself.
3514 */
3515 kern_sge32 = (struct megasas_sge32 *)
3516 ((unsigned long)cmd->frame + ioc->sgl_off);
3517
3518 /*
3519 * For each user buffer, create a mirror buffer and copy in
3520 */
3521 for (i = 0; i < ioc->sge_count; i++) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08003522 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003523 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08003524 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003525 if (!kbuff_arr[i]) {
3526 printk(KERN_DEBUG "megasas: Failed to alloc "
3527 "kernel SGL buffer for IOCTL \n");
3528 error = -ENOMEM;
3529 goto out;
3530 }
3531
3532 /*
3533 * We don't change the dma_coherent_mask, so
3534 * pci_alloc_consistent only returns 32bit addresses
3535 */
3536 kern_sge32[i].phys_addr = (u32) buf_handle;
3537 kern_sge32[i].length = ioc->sgl[i].iov_len;
3538
3539 /*
3540 * We created a kernel buffer corresponding to the
3541 * user buffer. Now copy in from the user buffer
3542 */
3543 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
3544 (u32) (ioc->sgl[i].iov_len))) {
3545 error = -EFAULT;
3546 goto out;
3547 }
3548 }
3549
3550 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08003551 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
3552 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003553 if (!sense) {
3554 error = -ENOMEM;
3555 goto out;
3556 }
3557
3558 sense_ptr =
3559 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
3560 *sense_ptr = sense_handle;
3561 }
3562
3563 /*
3564 * Set the sync_cmd flag so that the ISR knows not to complete this
3565 * cmd to the SCSI mid-layer
3566 */
3567 cmd->sync_cmd = 1;
3568 megasas_issue_blocked_cmd(instance, cmd);
3569 cmd->sync_cmd = 0;
3570
3571 /*
3572 * copy out the kernel buffers to user buffers
3573 */
3574 for (i = 0; i < ioc->sge_count; i++) {
3575 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
3576 ioc->sgl[i].iov_len)) {
3577 error = -EFAULT;
3578 goto out;
3579 }
3580 }
3581
3582 /*
3583 * copy out the sense
3584 */
3585 if (ioc->sense_len) {
3586 /*
bo yangb70a41e2008-03-18 03:13:06 -04003587 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003588 * sense buffer address
3589 */
bo yangb70a41e2008-03-18 03:13:06 -04003590 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
3591 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003592
bo yangb70a41e2008-03-18 03:13:06 -04003593 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
3594 sense, ioc->sense_len)) {
bo yangb10c36a2007-11-09 04:28:47 -05003595 printk(KERN_ERR "megasas: Failed to copy out to user "
3596 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003597 error = -EFAULT;
3598 goto out;
3599 }
3600 }
3601
3602 /*
3603 * copy the status codes returned by the fw
3604 */
3605 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
3606 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
3607 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
3608 error = -EFAULT;
3609 }
3610
3611 out:
3612 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08003613 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003614 sense, sense_handle);
3615 }
3616
3617 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08003618 dma_free_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003619 kern_sge32[i].length,
3620 kbuff_arr[i], kern_sge32[i].phys_addr);
3621 }
3622
3623 megasas_return_cmd(instance, cmd);
3624 return error;
3625}
3626
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003627static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
3628{
3629 struct megasas_iocpacket __user *user_ioc =
3630 (struct megasas_iocpacket __user *)arg;
3631 struct megasas_iocpacket *ioc;
3632 struct megasas_instance *instance;
3633 int error;
3634
3635 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
3636 if (!ioc)
3637 return -ENOMEM;
3638
3639 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
3640 error = -EFAULT;
3641 goto out_kfree_ioc;
3642 }
3643
3644 instance = megasas_lookup_instance(ioc->host_no);
3645 if (!instance) {
3646 error = -ENODEV;
3647 goto out_kfree_ioc;
3648 }
3649
Yang, Bo0c79e682009-10-06 14:47:35 -06003650 if (instance->hw_crit_error == 1) {
3651 printk(KERN_DEBUG "Controller in Crit ERROR\n");
3652 error = -ENODEV;
3653 goto out_kfree_ioc;
3654 }
3655
3656 if (instance->unload == 1) {
3657 error = -ENODEV;
3658 goto out_kfree_ioc;
3659 }
3660
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003661 /*
3662 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
3663 */
3664 if (down_interruptible(&instance->ioctl_sem)) {
3665 error = -ERESTARTSYS;
3666 goto out_kfree_ioc;
3667 }
3668 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
3669 up(&instance->ioctl_sem);
3670
3671 out_kfree_ioc:
3672 kfree(ioc);
3673 return error;
3674}
3675
3676static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
3677{
3678 struct megasas_instance *instance;
3679 struct megasas_aen aen;
3680 int error;
3681
3682 if (file->private_data != file) {
3683 printk(KERN_DEBUG "megasas: fasync_helper was not "
3684 "called first\n");
3685 return -EINVAL;
3686 }
3687
3688 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
3689 return -EFAULT;
3690
3691 instance = megasas_lookup_instance(aen.host_no);
3692
3693 if (!instance)
3694 return -ENODEV;
3695
Yang, Bo0c79e682009-10-06 14:47:35 -06003696 if (instance->hw_crit_error == 1) {
3697 error = -ENODEV;
3698 }
3699
3700 if (instance->unload == 1) {
3701 return -ENODEV;
3702 }
3703
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02003704 mutex_lock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003705 error = megasas_register_aen(instance, aen.seq_num,
3706 aen.class_locale_word);
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02003707 mutex_unlock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003708 return error;
3709}
3710
3711/**
3712 * megasas_mgmt_ioctl - char node ioctl entry point
3713 */
3714static long
3715megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3716{
3717 switch (cmd) {
3718 case MEGASAS_IOC_FIRMWARE:
3719 return megasas_mgmt_ioctl_fw(file, arg);
3720
3721 case MEGASAS_IOC_GET_AEN:
3722 return megasas_mgmt_ioctl_aen(file, arg);
3723 }
3724
3725 return -ENOTTY;
3726}
3727
3728#ifdef CONFIG_COMPAT
3729static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
3730{
3731 struct compat_megasas_iocpacket __user *cioc =
3732 (struct compat_megasas_iocpacket __user *)arg;
3733 struct megasas_iocpacket __user *ioc =
3734 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
3735 int i;
3736 int error = 0;
3737
Jeff Garzik83aabc12006-10-04 06:34:03 -04003738 if (clear_user(ioc, sizeof(*ioc)))
3739 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003740
3741 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
3742 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
3743 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
3744 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
3745 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
3746 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
3747 return -EFAULT;
3748
3749 for (i = 0; i < MAX_IOCTL_SGE; i++) {
3750 compat_uptr_t ptr;
3751
3752 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
3753 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
3754 copy_in_user(&ioc->sgl[i].iov_len,
3755 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
3756 return -EFAULT;
3757 }
3758
3759 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
3760
3761 if (copy_in_user(&cioc->frame.hdr.cmd_status,
3762 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
3763 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
3764 return -EFAULT;
3765 }
3766 return error;
3767}
3768
3769static long
3770megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
3771 unsigned long arg)
3772{
3773 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08003774 case MEGASAS_IOC_FIRMWARE32:
3775 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003776 case MEGASAS_IOC_GET_AEN:
3777 return megasas_mgmt_ioctl_aen(file, arg);
3778 }
3779
3780 return -ENOTTY;
3781}
3782#endif
3783
3784/*
3785 * File operations structure for management interface
3786 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08003787static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003788 .owner = THIS_MODULE,
3789 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003790 .fasync = megasas_mgmt_fasync,
3791 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06003792 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003793#ifdef CONFIG_COMPAT
3794 .compat_ioctl = megasas_mgmt_compat_ioctl,
3795#endif
3796};
3797
3798/*
3799 * PCI hotplug support registration structure
3800 */
3801static struct pci_driver megasas_pci_driver = {
3802
3803 .name = "megaraid_sas",
3804 .id_table = megasas_pci_table,
3805 .probe = megasas_probe_one,
3806 .remove = __devexit_p(megasas_detach_one),
bo yang31ea7082007-11-07 12:09:50 -05003807 .suspend = megasas_suspend,
3808 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003809 .shutdown = megasas_shutdown,
3810};
3811
3812/*
3813 * Sysfs driver attributes
3814 */
3815static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
3816{
3817 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
3818 MEGASAS_VERSION);
3819}
3820
3821static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
3822
3823static ssize_t
3824megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
3825{
3826 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
3827 MEGASAS_RELDATE);
3828}
3829
3830static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
3831 NULL);
3832
Sumant Patro658dced2006-10-03 13:09:14 -07003833static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06003834megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
3835{
3836 return sprintf(buf, "%u\n", support_poll_for_event);
3837}
3838
3839static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
3840 megasas_sysfs_show_support_poll_for_event, NULL);
3841
3842static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07003843megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
3844{
bo yangad84db22007-11-09 04:40:16 -05003845 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07003846}
3847
3848static ssize_t
3849megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
3850{
3851 int retval = count;
3852 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
3853 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
3854 retval = -EINVAL;
3855 }
3856 return retval;
3857}
3858
Joe Malicki66dca9b2008-08-14 17:14:48 -04003859static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05003860 megasas_sysfs_set_dbg_lvl);
3861
3862static ssize_t
3863megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
3864{
3865 return sprintf(buf, "%u\n", poll_mode_io);
3866}
3867
3868static ssize_t
3869megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
3870 const char *buf, size_t count)
3871{
3872 int retval = count;
3873 int tmp = poll_mode_io;
3874 int i;
3875 struct megasas_instance *instance;
3876
3877 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
3878 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
3879 retval = -EINVAL;
3880 }
3881
3882 /*
3883 * Check if poll_mode_io is already set or is same as previous value
3884 */
3885 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
3886 goto out;
3887
3888 if (poll_mode_io) {
3889 /*
3890 * Start timers for all adapters
3891 */
3892 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3893 instance = megasas_mgmt_info.instance[i];
3894 if (instance) {
3895 megasas_start_timer(instance,
3896 &instance->io_completion_timer,
3897 megasas_io_completion_timer,
3898 MEGASAS_COMPLETION_TIMER_INTERVAL);
3899 }
3900 }
3901 } else {
3902 /*
3903 * Delete timers for all adapters
3904 */
3905 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3906 instance = megasas_mgmt_info.instance[i];
3907 if (instance)
3908 del_timer_sync(&instance->io_completion_timer);
3909 }
3910 }
3911
3912out:
3913 return retval;
3914}
3915
3916static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUGO,
3917 megasas_sysfs_show_poll_mode_io,
3918 megasas_sysfs_set_poll_mode_io);
Sumant Patro658dced2006-10-03 13:09:14 -07003919
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003920/**
3921 * megasas_init - Driver load entry point
3922 */
3923static int __init megasas_init(void)
3924{
3925 int rval;
3926
3927 /*
3928 * Announce driver version and other information
3929 */
3930 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
3931 MEGASAS_EXT_VERSION);
3932
Yang, Bo72c4fd32009-10-06 14:20:59 -06003933 support_poll_for_event = 2;
3934
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003935 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
3936
3937 /*
3938 * Register character device node
3939 */
3940 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
3941
3942 if (rval < 0) {
3943 printk(KERN_DEBUG "megasas: failed to open device node\n");
3944 return rval;
3945 }
3946
3947 megasas_mgmt_majorno = rval;
3948
3949 /*
3950 * Register ourselves as PCI hotplug module
3951 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00003952 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003953
3954 if (rval) {
3955 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04003956 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003957 }
3958
Jeff Garzik83aabc12006-10-04 06:34:03 -04003959 rval = driver_create_file(&megasas_pci_driver.driver,
3960 &driver_attr_version);
3961 if (rval)
3962 goto err_dcf_attr_ver;
3963 rval = driver_create_file(&megasas_pci_driver.driver,
3964 &driver_attr_release_date);
3965 if (rval)
3966 goto err_dcf_rel_date;
Yang, Bo72c4fd32009-10-06 14:20:59 -06003967
3968 rval = driver_create_file(&megasas_pci_driver.driver,
3969 &driver_attr_support_poll_for_event);
3970 if (rval)
3971 goto err_dcf_support_poll_for_event;
3972
Jeff Garzik83aabc12006-10-04 06:34:03 -04003973 rval = driver_create_file(&megasas_pci_driver.driver,
3974 &driver_attr_dbg_lvl);
3975 if (rval)
3976 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05003977 rval = driver_create_file(&megasas_pci_driver.driver,
3978 &driver_attr_poll_mode_io);
3979 if (rval)
3980 goto err_dcf_poll_mode_io;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003981
3982 return rval;
bo yangad84db22007-11-09 04:40:16 -05003983
3984err_dcf_poll_mode_io:
3985 driver_remove_file(&megasas_pci_driver.driver,
3986 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04003987err_dcf_dbg_lvl:
3988 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06003989 &driver_attr_support_poll_for_event);
3990
3991err_dcf_support_poll_for_event:
3992 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04003993 &driver_attr_release_date);
Yang, Bo72c4fd32009-10-06 14:20:59 -06003994
Jeff Garzik83aabc12006-10-04 06:34:03 -04003995err_dcf_rel_date:
3996 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3997err_dcf_attr_ver:
3998 pci_unregister_driver(&megasas_pci_driver);
3999err_pcidrv:
4000 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
4001 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004002}
4003
4004/**
4005 * megasas_exit - Driver unload entry point
4006 */
4007static void __exit megasas_exit(void)
4008{
Sumant Patro658dced2006-10-03 13:09:14 -07004009 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05004010 &driver_attr_poll_mode_io);
4011 driver_remove_file(&megasas_pci_driver.driver,
Sumant Patro658dced2006-10-03 13:09:14 -07004012 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04004013 driver_remove_file(&megasas_pci_driver.driver,
4014 &driver_attr_release_date);
4015 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004016
4017 pci_unregister_driver(&megasas_pci_driver);
4018 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
4019}
4020
4021module_init(megasas_init);
4022module_exit(megasas_exit);