blob: b0d6991cb6f3f298798f02fe00b26e53aa280bd5 [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
Yang, Bo7e8a75f2009-10-06 14:50:17 -06001523static void megasas_aen_polling(struct work_struct *work);
1524
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001525/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001526 * megasas_service_aen - Processes an event notification
1527 * @instance: Adapter soft state
1528 * @cmd: AEN command completed by the ISR
1529 *
1530 * For AEN, driver sends a command down to FW that is held by the FW till an
1531 * event occurs. When an event of interest occurs, FW completes the command
1532 * that it was previously holding.
1533 *
1534 * This routines sends SIGIO signal to processes that have registered with the
1535 * driver for AEN.
1536 */
1537static void
1538megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1539{
Yang, Boc3518832009-10-06 14:18:02 -06001540 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001541 /*
1542 * Don't signal app if it is just an aborted previously registered aen
1543 */
Yang, Boc3518832009-10-06 14:18:02 -06001544 if ((!cmd->abort_aen) && (instance->unload == 0)) {
1545 spin_lock_irqsave(&poll_aen_lock, flags);
1546 megasas_poll_wait_aen = 1;
1547 spin_unlock_irqrestore(&poll_aen_lock, flags);
1548 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001549 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06001550 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001551 else
1552 cmd->abort_aen = 0;
1553
1554 instance->aen_cmd = NULL;
1555 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06001556
1557 if (instance->unload == 0) {
1558 struct megasas_aen_event *ev;
1559 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
1560 if (!ev) {
1561 printk(KERN_ERR "megasas_service_aen: out of memory\n");
1562 } else {
1563 ev->instance = instance;
1564 instance->ev = ev;
1565 INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
1566 schedule_delayed_work(
1567 (struct delayed_work *)&ev->hotplug_work, 0);
1568 }
1569 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001570}
1571
1572/*
1573 * Scsi host template for megaraid_sas driver
1574 */
1575static struct scsi_host_template megasas_template = {
1576
1577 .module = THIS_MODULE,
bo yangf28cd7c2007-11-09 04:44:56 -05001578 .name = "LSI SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001579 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01001580 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06001581 .slave_alloc = megasas_slave_alloc,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001582 .queuecommand = megasas_queue_command,
1583 .eh_device_reset_handler = megasas_reset_device,
1584 .eh_bus_reset_handler = megasas_reset_bus_host,
1585 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001586 .eh_timed_out = megasas_reset_timer,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001587 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001588 .use_clustering = ENABLE_CLUSTERING,
1589};
1590
1591/**
1592 * megasas_complete_int_cmd - Completes an internal command
1593 * @instance: Adapter soft state
1594 * @cmd: Command to be completed
1595 *
1596 * The megasas_issue_blocked_cmd() function waits for a command to complete
1597 * after it issues a command. This function wakes up that waiting routine by
1598 * calling wake_up() on the wait queue.
1599 */
1600static void
1601megasas_complete_int_cmd(struct megasas_instance *instance,
1602 struct megasas_cmd *cmd)
1603{
1604 cmd->cmd_status = cmd->frame->io.cmd_status;
1605
1606 if (cmd->cmd_status == ENODATA) {
1607 cmd->cmd_status = 0;
1608 }
1609 wake_up(&instance->int_cmd_wait_q);
1610}
1611
1612/**
1613 * megasas_complete_abort - Completes aborting a command
1614 * @instance: Adapter soft state
1615 * @cmd: Cmd that was issued to abort another cmd
1616 *
1617 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
1618 * after it issues an abort on a previously issued command. This function
1619 * wakes up all functions waiting on the same wait queue.
1620 */
1621static void
1622megasas_complete_abort(struct megasas_instance *instance,
1623 struct megasas_cmd *cmd)
1624{
1625 if (cmd->sync_cmd) {
1626 cmd->sync_cmd = 0;
1627 cmd->cmd_status = 0;
1628 wake_up(&instance->abort_cmd_wait_q);
1629 }
1630
1631 return;
1632}
1633
1634/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001635 * megasas_complete_cmd - Completes a command
1636 * @instance: Adapter soft state
1637 * @cmd: Command to be completed
1638 * @alt_status: If non-zero, use this value as status to
1639 * SCSI mid-layer instead of the value returned
1640 * by the FW. This should be used if caller wants
1641 * an alternate status (as in the case of aborted
1642 * commands)
1643 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001644static void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001645megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
1646 u8 alt_status)
1647{
1648 int exception = 0;
1649 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06001650 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001651
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001652 if (cmd->scmd)
1653 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001654
1655 switch (hdr->cmd) {
1656
1657 case MFI_CMD_PD_SCSI_IO:
1658 case MFI_CMD_LD_SCSI_IO:
1659
1660 /*
1661 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1662 * issued either through an IO path or an IOCTL path. If it
1663 * was via IOCTL, we will send it to internal completion.
1664 */
1665 if (cmd->sync_cmd) {
1666 cmd->sync_cmd = 0;
1667 megasas_complete_int_cmd(instance, cmd);
1668 break;
1669 }
1670
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001671 case MFI_CMD_LD_READ:
1672 case MFI_CMD_LD_WRITE:
1673
1674 if (alt_status) {
1675 cmd->scmd->result = alt_status << 16;
1676 exception = 1;
1677 }
1678
1679 if (exception) {
1680
Sumant Patroe4a082c2006-05-30 12:03:37 -07001681 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001682
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001683 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001684 cmd->scmd->scsi_done(cmd->scmd);
1685 megasas_return_cmd(instance, cmd);
1686
1687 break;
1688 }
1689
1690 switch (hdr->cmd_status) {
1691
1692 case MFI_STAT_OK:
1693 cmd->scmd->result = DID_OK << 16;
1694 break;
1695
1696 case MFI_STAT_SCSI_IO_FAILED:
1697 case MFI_STAT_LD_INIT_IN_PROGRESS:
1698 cmd->scmd->result =
1699 (DID_ERROR << 16) | hdr->scsi_status;
1700 break;
1701
1702 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1703
1704 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1705
1706 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1707 memset(cmd->scmd->sense_buffer, 0,
1708 SCSI_SENSE_BUFFERSIZE);
1709 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1710 hdr->sense_len);
1711
1712 cmd->scmd->result |= DRIVER_SENSE << 24;
1713 }
1714
1715 break;
1716
1717 case MFI_STAT_LD_OFFLINE:
1718 case MFI_STAT_DEVICE_NOT_FOUND:
1719 cmd->scmd->result = DID_BAD_TARGET << 16;
1720 break;
1721
1722 default:
1723 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1724 hdr->cmd_status);
1725 cmd->scmd->result = DID_ERROR << 16;
1726 break;
1727 }
1728
Sumant Patroe4a082c2006-05-30 12:03:37 -07001729 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001730
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001731 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001732 cmd->scmd->scsi_done(cmd->scmd);
1733 megasas_return_cmd(instance, cmd);
1734
1735 break;
1736
1737 case MFI_CMD_SMP:
1738 case MFI_CMD_STP:
1739 case MFI_CMD_DCMD:
Yang, Boc3518832009-10-06 14:18:02 -06001740 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
1741 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
1742 spin_lock_irqsave(&poll_aen_lock, flags);
1743 megasas_poll_wait_aen = 0;
1744 spin_unlock_irqrestore(&poll_aen_lock, flags);
1745 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001746
1747 /*
1748 * See if got an event notification
1749 */
1750 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1751 megasas_service_aen(instance, cmd);
1752 else
1753 megasas_complete_int_cmd(instance, cmd);
1754
1755 break;
1756
1757 case MFI_CMD_ABORT:
1758 /*
1759 * Cmd issued to abort another cmd returned
1760 */
1761 megasas_complete_abort(instance, cmd);
1762 break;
1763
1764 default:
1765 printk("megasas: Unknown command completed! [0x%X]\n",
1766 hdr->cmd);
1767 break;
1768 }
1769}
1770
1771/**
1772 * megasas_deplete_reply_queue - Processes all completed commands
1773 * @instance: Adapter soft state
1774 * @alt_status: Alternate status to be returned to
1775 * SCSI mid-layer instead of the status
1776 * returned by the FW
1777 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001778static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001779megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1780{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001781 /*
1782 * Check if it is our interrupt
Sumant Patro1341c932006-01-25 12:02:40 -08001783 * Clear the interrupt
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001784 */
Sumant Patro1341c932006-01-25 12:02:40 -08001785 if(instance->instancet->clear_intr(instance->reg_set))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001786 return IRQ_NONE;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001787
Sumant Patroaf37acf2007-02-14 12:34:46 -08001788 if (instance->hw_crit_error)
1789 goto out_done;
Sumant Patro5d018ad2006-10-03 13:13:18 -07001790 /*
1791 * Schedule the tasklet for cmd completion
1792 */
1793 tasklet_schedule(&instance->isr_tasklet);
Sumant Patroaf37acf2007-02-14 12:34:46 -08001794out_done:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001795 return IRQ_HANDLED;
1796}
1797
1798/**
1799 * megasas_isr - isr entry point
1800 */
David Howells7d12e782006-10-05 14:55:46 +01001801static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001802{
1803 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1804 DID_OK);
1805}
1806
1807/**
1808 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08001809 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001810 *
1811 * During the initialization, FW passes can potentially be in any one of
1812 * several possible states. If the FW in operational, waiting-for-handshake
1813 * states, driver must take steps to bring it to ready state. Otherwise, it
1814 * has to wait for the ready state.
1815 */
1816static int
Sumant Patro1341c932006-01-25 12:02:40 -08001817megasas_transition_to_ready(struct megasas_instance* instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001818{
1819 int i;
1820 u8 max_wait;
1821 u32 fw_state;
1822 u32 cur_state;
1823
Sumant Patro1341c932006-01-25 12:02:40 -08001824 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001825
Sumant Patroe3bbff92006-10-03 12:28:49 -07001826 if (fw_state != MFI_STATE_READY)
1827 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1828 " state\n");
1829
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001830 while (fw_state != MFI_STATE_READY) {
1831
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001832 switch (fw_state) {
1833
1834 case MFI_STATE_FAULT:
1835
1836 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1837 return -ENODEV;
1838
1839 case MFI_STATE_WAIT_HANDSHAKE:
1840 /*
1841 * Set the CLR bit in inbound doorbell
1842 */
Yang, Bo0c79e682009-10-06 14:47:35 -06001843 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06001844 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1845 (instance->pdev->device ==
1846 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1847
1848 writel(
1849 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
1850 &instance->reg_set->reserved_0[0]);
1851 } else {
1852 writel(
1853 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
1854 &instance->reg_set->inbound_doorbell);
1855 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001856
1857 max_wait = 2;
1858 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1859 break;
1860
Sumant Patroe3bbff92006-10-03 12:28:49 -07001861 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06001862 if ((instance->pdev->device ==
1863 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1864 (instance->pdev->device ==
1865 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1866 writel(MFI_INIT_HOTPLUG,
1867 &instance->reg_set->reserved_0[0]);
1868 } else
1869 writel(MFI_INIT_HOTPLUG,
1870 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07001871
1872 max_wait = 10;
1873 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
1874 break;
1875
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001876 case MFI_STATE_OPERATIONAL:
1877 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07001878 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001879 */
Sumant Patrob274cab2006-10-03 12:52:12 -07001880 instance->instancet->disable_intr(instance->reg_set);
Yang, Bo87911122009-10-06 14:31:54 -06001881 if ((instance->pdev->device ==
1882 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1883 (instance->pdev->device ==
1884 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1885 writel(MFI_RESET_FLAGS,
1886 &instance->reg_set->reserved_0[0]);
1887 } else
1888 writel(MFI_RESET_FLAGS,
1889 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001890
bo yangaf7a5642008-03-17 04:13:07 -04001891 max_wait = 60;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001892 cur_state = MFI_STATE_OPERATIONAL;
1893 break;
1894
1895 case MFI_STATE_UNDEFINED:
1896 /*
1897 * This state should not last for more than 2 seconds
1898 */
1899 max_wait = 2;
1900 cur_state = MFI_STATE_UNDEFINED;
1901 break;
1902
1903 case MFI_STATE_BB_INIT:
1904 max_wait = 2;
1905 cur_state = MFI_STATE_BB_INIT;
1906 break;
1907
1908 case MFI_STATE_FW_INIT:
1909 max_wait = 20;
1910 cur_state = MFI_STATE_FW_INIT;
1911 break;
1912
1913 case MFI_STATE_FW_INIT_2:
1914 max_wait = 20;
1915 cur_state = MFI_STATE_FW_INIT_2;
1916 break;
1917
1918 case MFI_STATE_DEVICE_SCAN:
1919 max_wait = 20;
1920 cur_state = MFI_STATE_DEVICE_SCAN;
1921 break;
1922
1923 case MFI_STATE_FLUSH_CACHE:
1924 max_wait = 20;
1925 cur_state = MFI_STATE_FLUSH_CACHE;
1926 break;
1927
1928 default:
1929 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1930 fw_state);
1931 return -ENODEV;
1932 }
1933
1934 /*
1935 * The cur_state should not last for more than max_wait secs
1936 */
1937 for (i = 0; i < (max_wait * 1000); i++) {
Sumant Patro1341c932006-01-25 12:02:40 -08001938 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1939 MFI_STATE_MASK ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001940
1941 if (fw_state == cur_state) {
1942 msleep(1);
1943 } else
1944 break;
1945 }
1946
1947 /*
1948 * Return error if fw_state hasn't changed after max_wait
1949 */
1950 if (fw_state == cur_state) {
1951 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1952 "in %d secs\n", fw_state, max_wait);
1953 return -ENODEV;
1954 }
1955 };
Sumant Patroe3bbff92006-10-03 12:28:49 -07001956 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001957
1958 return 0;
1959}
1960
1961/**
1962 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1963 * @instance: Adapter soft state
1964 */
1965static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1966{
1967 int i;
1968 u32 max_cmd = instance->max_fw_cmds;
1969 struct megasas_cmd *cmd;
1970
1971 if (!instance->frame_dma_pool)
1972 return;
1973
1974 /*
1975 * Return all frames to pool
1976 */
1977 for (i = 0; i < max_cmd; i++) {
1978
1979 cmd = instance->cmd_list[i];
1980
1981 if (cmd->frame)
1982 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1983 cmd->frame_phys_addr);
1984
1985 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07001986 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001987 cmd->sense_phys_addr);
1988 }
1989
1990 /*
1991 * Now destroy the pool itself
1992 */
1993 pci_pool_destroy(instance->frame_dma_pool);
1994 pci_pool_destroy(instance->sense_dma_pool);
1995
1996 instance->frame_dma_pool = NULL;
1997 instance->sense_dma_pool = NULL;
1998}
1999
2000/**
2001 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2002 * @instance: Adapter soft state
2003 *
2004 * Each command packet has an embedded DMA memory buffer that is used for
2005 * filling MFI frame and the SG list that immediately follows the frame. This
2006 * function creates those DMA memory buffers for each command packet by using
2007 * PCI pool facility.
2008 */
2009static int megasas_create_frame_pool(struct megasas_instance *instance)
2010{
2011 int i;
2012 u32 max_cmd;
2013 u32 sge_sz;
2014 u32 sgl_sz;
2015 u32 total_sz;
2016 u32 frame_count;
2017 struct megasas_cmd *cmd;
2018
2019 max_cmd = instance->max_fw_cmds;
2020
2021 /*
2022 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2023 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2024 */
2025 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2026 sizeof(struct megasas_sge32);
2027
Yang, Bof4c9a132009-10-06 14:43:28 -06002028 if (instance->flag_ieee) {
2029 sge_sz = sizeof(struct megasas_sge_skinny);
2030 }
2031
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002032 /*
2033 * Calculated the number of 64byte frames required for SGL
2034 */
2035 sgl_sz = sge_sz * instance->max_num_sge;
2036 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
2037
2038 /*
2039 * We need one extra frame for the MFI command
2040 */
2041 frame_count++;
2042
2043 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2044 /*
2045 * Use DMA pool facility provided by PCI layer
2046 */
2047 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2048 instance->pdev, total_sz, 64,
2049 0);
2050
2051 if (!instance->frame_dma_pool) {
2052 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2053 return -ENOMEM;
2054 }
2055
2056 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2057 instance->pdev, 128, 4, 0);
2058
2059 if (!instance->sense_dma_pool) {
2060 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2061
2062 pci_pool_destroy(instance->frame_dma_pool);
2063 instance->frame_dma_pool = NULL;
2064
2065 return -ENOMEM;
2066 }
2067
2068 /*
2069 * Allocate and attach a frame to each of the commands in cmd_list.
2070 * By making cmd->index as the context instead of the &cmd, we can
2071 * always use 32bit context regardless of the architecture
2072 */
2073 for (i = 0; i < max_cmd; i++) {
2074
2075 cmd = instance->cmd_list[i];
2076
2077 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2078 GFP_KERNEL, &cmd->frame_phys_addr);
2079
2080 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2081 GFP_KERNEL, &cmd->sense_phys_addr);
2082
2083 /*
2084 * megasas_teardown_frame_pool() takes care of freeing
2085 * whatever has been allocated
2086 */
2087 if (!cmd->frame || !cmd->sense) {
2088 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2089 megasas_teardown_frame_pool(instance);
2090 return -ENOMEM;
2091 }
2092
2093 cmd->frame->io.context = cmd->index;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002094 cmd->frame->io.pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002095 }
2096
2097 return 0;
2098}
2099
2100/**
2101 * megasas_free_cmds - Free all the cmds in the free cmd pool
2102 * @instance: Adapter soft state
2103 */
2104static void megasas_free_cmds(struct megasas_instance *instance)
2105{
2106 int i;
2107 /* First free the MFI frame pool */
2108 megasas_teardown_frame_pool(instance);
2109
2110 /* Free all the commands in the cmd_list */
2111 for (i = 0; i < instance->max_fw_cmds; i++)
2112 kfree(instance->cmd_list[i]);
2113
2114 /* Free the cmd_list buffer itself */
2115 kfree(instance->cmd_list);
2116 instance->cmd_list = NULL;
2117
2118 INIT_LIST_HEAD(&instance->cmd_pool);
2119}
2120
2121/**
2122 * megasas_alloc_cmds - Allocates the command packets
2123 * @instance: Adapter soft state
2124 *
2125 * Each command that is issued to the FW, whether IO commands from the OS or
2126 * internal commands like IOCTLs, are wrapped in local data structure called
2127 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2128 * the FW.
2129 *
2130 * Each frame has a 32-bit field called context (tag). This context is used
2131 * to get back the megasas_cmd from the frame when a frame gets completed in
2132 * the ISR. Typically the address of the megasas_cmd itself would be used as
2133 * the context. But we wanted to keep the differences between 32 and 64 bit
2134 * systems to the mininum. We always use 32 bit integers for the context. In
2135 * this driver, the 32 bit values are the indices into an array cmd_list.
2136 * This array is used only to look up the megasas_cmd given the context. The
2137 * free commands themselves are maintained in a linked list called cmd_pool.
2138 */
2139static int megasas_alloc_cmds(struct megasas_instance *instance)
2140{
2141 int i;
2142 int j;
2143 u32 max_cmd;
2144 struct megasas_cmd *cmd;
2145
2146 max_cmd = instance->max_fw_cmds;
2147
2148 /*
2149 * instance->cmd_list is an array of struct megasas_cmd pointers.
2150 * Allocate the dynamic array first and then allocate individual
2151 * commands.
2152 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002153 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002154
2155 if (!instance->cmd_list) {
2156 printk(KERN_DEBUG "megasas: out of memory\n");
2157 return -ENOMEM;
2158 }
2159
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002160
2161 for (i = 0; i < max_cmd; i++) {
2162 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
2163 GFP_KERNEL);
2164
2165 if (!instance->cmd_list[i]) {
2166
2167 for (j = 0; j < i; j++)
2168 kfree(instance->cmd_list[j]);
2169
2170 kfree(instance->cmd_list);
2171 instance->cmd_list = NULL;
2172
2173 return -ENOMEM;
2174 }
2175 }
2176
2177 /*
2178 * Add all the commands to command pool (instance->cmd_pool)
2179 */
2180 for (i = 0; i < max_cmd; i++) {
2181 cmd = instance->cmd_list[i];
2182 memset(cmd, 0, sizeof(struct megasas_cmd));
2183 cmd->index = i;
2184 cmd->instance = instance;
2185
2186 list_add_tail(&cmd->list, &instance->cmd_pool);
2187 }
2188
2189 /*
2190 * Create a frame pool and assign one frame to each cmd
2191 */
2192 if (megasas_create_frame_pool(instance)) {
2193 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
2194 megasas_free_cmds(instance);
2195 }
2196
2197 return 0;
2198}
2199
Yang, Bo81e403c2009-10-06 14:27:54 -06002200/*
2201 * megasas_get_pd_list_info - Returns FW's pd_list structure
2202 * @instance: Adapter soft state
2203 * @pd_list: pd_list structure
2204 *
2205 * Issues an internal command (DCMD) to get the FW's controller PD
2206 * list structure. This information is mainly used to find out SYSTEM
2207 * supported by the FW.
2208 */
2209static int
2210megasas_get_pd_list(struct megasas_instance *instance)
2211{
2212 int ret = 0, pd_index = 0;
2213 struct megasas_cmd *cmd;
2214 struct megasas_dcmd_frame *dcmd;
2215 struct MR_PD_LIST *ci;
2216 struct MR_PD_ADDRESS *pd_addr;
2217 dma_addr_t ci_h = 0;
2218
2219 cmd = megasas_get_cmd(instance);
2220
2221 if (!cmd) {
2222 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
2223 return -ENOMEM;
2224 }
2225
2226 dcmd = &cmd->frame->dcmd;
2227
2228 ci = pci_alloc_consistent(instance->pdev,
2229 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
2230
2231 if (!ci) {
2232 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
2233 megasas_return_cmd(instance, cmd);
2234 return -ENOMEM;
2235 }
2236
2237 memset(ci, 0, sizeof(*ci));
2238 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2239
2240 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
2241 dcmd->mbox.b[1] = 0;
2242 dcmd->cmd = MFI_CMD_DCMD;
2243 dcmd->cmd_status = 0xFF;
2244 dcmd->sge_count = 1;
2245 dcmd->flags = MFI_FRAME_DIR_READ;
2246 dcmd->timeout = 0;
2247 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
2248 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
2249 dcmd->sgl.sge32[0].phys_addr = ci_h;
2250 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
2251
2252 if (!megasas_issue_polled(instance, cmd)) {
2253 ret = 0;
2254 } else {
2255 ret = -1;
2256 }
2257
2258 /*
2259 * the following function will get the instance PD LIST.
2260 */
2261
2262 pd_addr = ci->addr;
2263
2264 if ( ret == 0 &&
2265 (ci->count <
2266 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
2267
2268 memset(instance->pd_list, 0,
2269 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
2270
2271 for (pd_index = 0; pd_index < ci->count; pd_index++) {
2272
2273 instance->pd_list[pd_addr->deviceId].tid =
2274 pd_addr->deviceId;
2275 instance->pd_list[pd_addr->deviceId].driveType =
2276 pd_addr->scsiDevType;
2277 instance->pd_list[pd_addr->deviceId].driveState =
2278 MR_PD_STATE_SYSTEM;
2279 pd_addr++;
2280 }
2281 }
2282
2283 pci_free_consistent(instance->pdev,
2284 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
2285 ci, ci_h);
2286 megasas_return_cmd(instance, cmd);
2287
2288 return ret;
2289}
2290
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002291/**
2292 * megasas_get_controller_info - Returns FW's controller structure
2293 * @instance: Adapter soft state
2294 * @ctrl_info: Controller information structure
2295 *
2296 * Issues an internal command (DCMD) to get the FW's controller structure.
2297 * This information is mainly used to find out the maximum IO transfer per
2298 * command supported by the FW.
2299 */
2300static int
2301megasas_get_ctrl_info(struct megasas_instance *instance,
2302 struct megasas_ctrl_info *ctrl_info)
2303{
2304 int ret = 0;
2305 struct megasas_cmd *cmd;
2306 struct megasas_dcmd_frame *dcmd;
2307 struct megasas_ctrl_info *ci;
2308 dma_addr_t ci_h = 0;
2309
2310 cmd = megasas_get_cmd(instance);
2311
2312 if (!cmd) {
2313 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
2314 return -ENOMEM;
2315 }
2316
2317 dcmd = &cmd->frame->dcmd;
2318
2319 ci = pci_alloc_consistent(instance->pdev,
2320 sizeof(struct megasas_ctrl_info), &ci_h);
2321
2322 if (!ci) {
2323 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
2324 megasas_return_cmd(instance, cmd);
2325 return -ENOMEM;
2326 }
2327
2328 memset(ci, 0, sizeof(*ci));
2329 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2330
2331 dcmd->cmd = MFI_CMD_DCMD;
2332 dcmd->cmd_status = 0xFF;
2333 dcmd->sge_count = 1;
2334 dcmd->flags = MFI_FRAME_DIR_READ;
2335 dcmd->timeout = 0;
2336 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
2337 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
2338 dcmd->sgl.sge32[0].phys_addr = ci_h;
2339 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
2340
2341 if (!megasas_issue_polled(instance, cmd)) {
2342 ret = 0;
2343 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
2344 } else {
2345 ret = -1;
2346 }
2347
2348 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
2349 ci, ci_h);
2350
2351 megasas_return_cmd(instance, cmd);
2352 return ret;
2353}
2354
2355/**
bo yang31ea7082007-11-07 12:09:50 -05002356 * megasas_issue_init_mfi - Initializes the FW
2357 * @instance: Adapter soft state
2358 *
2359 * Issues the INIT MFI cmd
2360 */
2361static int
2362megasas_issue_init_mfi(struct megasas_instance *instance)
2363{
2364 u32 context;
2365
2366 struct megasas_cmd *cmd;
2367
2368 struct megasas_init_frame *init_frame;
2369 struct megasas_init_queue_info *initq_info;
2370 dma_addr_t init_frame_h;
2371 dma_addr_t initq_info_h;
2372
2373 /*
2374 * Prepare a init frame. Note the init frame points to queue info
2375 * structure. Each frame has SGL allocated after first 64 bytes. For
2376 * this frame - since we don't need any SGL - we use SGL's space as
2377 * queue info structure
2378 *
2379 * We will not get a NULL command below. We just created the pool.
2380 */
2381 cmd = megasas_get_cmd(instance);
2382
2383 init_frame = (struct megasas_init_frame *)cmd->frame;
2384 initq_info = (struct megasas_init_queue_info *)
2385 ((unsigned long)init_frame + 64);
2386
2387 init_frame_h = cmd->frame_phys_addr;
2388 initq_info_h = init_frame_h + 64;
2389
2390 context = init_frame->context;
2391 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
2392 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
2393 init_frame->context = context;
2394
2395 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
2396 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
2397
2398 initq_info->producer_index_phys_addr_lo = instance->producer_h;
2399 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
2400
2401 init_frame->cmd = MFI_CMD_INIT;
2402 init_frame->cmd_status = 0xFF;
2403 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
2404
2405 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
2406
2407 /*
2408 * disable the intr before firing the init frame to FW
2409 */
2410 instance->instancet->disable_intr(instance->reg_set);
2411
2412 /*
2413 * Issue the init frame in polled mode
2414 */
2415
2416 if (megasas_issue_polled(instance, cmd)) {
2417 printk(KERN_ERR "megasas: Failed to init firmware\n");
2418 megasas_return_cmd(instance, cmd);
2419 goto fail_fw_init;
2420 }
2421
2422 megasas_return_cmd(instance, cmd);
2423
2424 return 0;
2425
2426fail_fw_init:
2427 return -EINVAL;
2428}
2429
2430/**
bo yangad84db22007-11-09 04:40:16 -05002431 * megasas_start_timer - Initializes a timer object
2432 * @instance: Adapter soft state
2433 * @timer: timer object to be initialized
2434 * @fn: timer function
2435 * @interval: time interval between timer function call
2436 */
2437static inline void
2438megasas_start_timer(struct megasas_instance *instance,
2439 struct timer_list *timer,
2440 void *fn, unsigned long interval)
2441{
2442 init_timer(timer);
2443 timer->expires = jiffies + interval;
2444 timer->data = (unsigned long)instance;
2445 timer->function = fn;
2446 add_timer(timer);
2447}
2448
2449/**
2450 * megasas_io_completion_timer - Timer fn
2451 * @instance_addr: Address of adapter soft state
2452 *
2453 * Schedules tasklet for cmd completion
2454 * if poll_mode_io is set
2455 */
2456static void
2457megasas_io_completion_timer(unsigned long instance_addr)
2458{
2459 struct megasas_instance *instance =
2460 (struct megasas_instance *)instance_addr;
2461
2462 if (atomic_read(&instance->fw_outstanding))
2463 tasklet_schedule(&instance->isr_tasklet);
2464
2465 /* Restart timer */
2466 if (poll_mode_io)
2467 mod_timer(&instance->io_completion_timer,
2468 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
2469}
2470
2471/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002472 * megasas_init_mfi - Initializes the FW
2473 * @instance: Adapter soft state
2474 *
2475 * This is the main function for initializing MFI firmware.
2476 */
2477static int megasas_init_mfi(struct megasas_instance *instance)
2478{
2479 u32 context_sz;
2480 u32 reply_q_sz;
2481 u32 max_sectors_1;
2482 u32 max_sectors_2;
bo yang14faea92007-11-09 04:14:00 -05002483 u32 tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002484 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002485 struct megasas_ctrl_info *ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002486 /*
2487 * Map the message registers
2488 */
Yang, Bo6610a6b2008-08-10 12:42:38 -07002489 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
Yang, Bo87911122009-10-06 14:31:54 -06002490 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2491 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Yang, Bo6610a6b2008-08-10 12:42:38 -07002492 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0079GEN2)) {
2493 instance->base_addr = pci_resource_start(instance->pdev, 1);
2494 } else {
2495 instance->base_addr = pci_resource_start(instance->pdev, 0);
2496 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002497
bo yangf28cd7c2007-11-09 04:44:56 -05002498 if (pci_request_regions(instance->pdev, "megasas: LSI")) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002499 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
2500 return -EBUSY;
2501 }
2502
2503 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
2504
2505 if (!instance->reg_set) {
2506 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
2507 goto fail_ioremap;
2508 }
2509
2510 reg_set = instance->reg_set;
2511
Sumant Patrof9876f02006-02-03 15:34:35 -08002512 switch(instance->pdev->device)
2513 {
bo yangaf7a5642008-03-17 04:13:07 -04002514 case PCI_DEVICE_ID_LSI_SAS1078R:
2515 case PCI_DEVICE_ID_LSI_SAS1078DE:
Sumant Patrof9876f02006-02-03 15:34:35 -08002516 instance->instancet = &megasas_instance_template_ppc;
2517 break;
Yang, Bo6610a6b2008-08-10 12:42:38 -07002518 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
2519 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
2520 instance->instancet = &megasas_instance_template_gen2;
2521 break;
Yang, Bo87911122009-10-06 14:31:54 -06002522 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
2523 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
2524 instance->instancet = &megasas_instance_template_skinny;
2525 break;
Sumant Patrof9876f02006-02-03 15:34:35 -08002526 case PCI_DEVICE_ID_LSI_SAS1064R:
2527 case PCI_DEVICE_ID_DELL_PERC5:
2528 default:
2529 instance->instancet = &megasas_instance_template_xscale;
2530 break;
2531 }
Sumant Patro1341c932006-01-25 12:02:40 -08002532
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002533 /*
2534 * We expect the FW state to be READY
2535 */
Sumant Patro1341c932006-01-25 12:02:40 -08002536 if (megasas_transition_to_ready(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002537 goto fail_ready_state;
2538
2539 /*
2540 * Get various operational parameters from status register
2541 */
Sumant Patro1341c932006-01-25 12:02:40 -08002542 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07002543 /*
2544 * Reduce the max supported cmds by 1. This is to ensure that the
2545 * reply_q_sz (1 more than the max cmd that driver may send)
2546 * does not exceed max cmds that the FW can support
2547 */
2548 instance->max_fw_cmds = instance->max_fw_cmds-1;
Sumant Patro1341c932006-01-25 12:02:40 -08002549 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
2550 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002551 /*
2552 * Create a pool of commands
2553 */
2554 if (megasas_alloc_cmds(instance))
2555 goto fail_alloc_cmds;
2556
2557 /*
2558 * Allocate memory for reply queue. Length of reply queue should
2559 * be _one_ more than the maximum commands handled by the firmware.
2560 *
2561 * Note: When FW completes commands, it places corresponding contex
2562 * values in this circular reply queue. This circular queue is a fairly
2563 * typical producer-consumer queue. FW is the producer (of completed
2564 * commands) and the driver is the consumer.
2565 */
2566 context_sz = sizeof(u32);
2567 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
2568
2569 instance->reply_queue = pci_alloc_consistent(instance->pdev,
2570 reply_q_sz,
2571 &instance->reply_queue_h);
2572
2573 if (!instance->reply_queue) {
2574 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
2575 goto fail_reply_queue;
2576 }
2577
bo yang31ea7082007-11-07 12:09:50 -05002578 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002579 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002580
Yang, Bo81e403c2009-10-06 14:27:54 -06002581 memset(instance->pd_list, 0 ,
2582 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
2583 megasas_get_pd_list(instance);
2584
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002585 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
2586
2587 /*
2588 * Compute the max allowed sectors per IO: The controller info has two
2589 * limits on max sectors. Driver should use the minimum of these two.
2590 *
2591 * 1 << stripe_sz_ops.min = max sectors per strip
2592 *
2593 * Note that older firmwares ( < FW ver 30) didn't report information
2594 * to calculate max_sectors_1. So the number ended up as zero always.
2595 */
bo yang14faea92007-11-09 04:14:00 -05002596 tmp_sectors = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002597 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
2598
2599 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
2600 ctrl_info->max_strips_per_io;
2601 max_sectors_2 = ctrl_info->max_request_size;
2602
bo yang14faea92007-11-09 04:14:00 -05002603 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
2604 }
2605
2606 instance->max_sectors_per_req = instance->max_num_sge *
2607 PAGE_SIZE / 512;
2608 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
2609 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002610
2611 kfree(ctrl_info);
2612
Sumant Patro5d018ad2006-10-03 13:13:18 -07002613 /*
2614 * Setup tasklet for cmd completion
2615 */
2616
bo yangad84db22007-11-09 04:40:16 -05002617 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2618 (unsigned long)instance);
2619
2620 /* Initialize the cmd completion timer */
2621 if (poll_mode_io)
2622 megasas_start_timer(instance, &instance->io_completion_timer,
2623 megasas_io_completion_timer,
2624 MEGASAS_COMPLETION_TIMER_INTERVAL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002625 return 0;
2626
2627 fail_fw_init:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002628
2629 pci_free_consistent(instance->pdev, reply_q_sz,
2630 instance->reply_queue, instance->reply_queue_h);
2631 fail_reply_queue:
2632 megasas_free_cmds(instance);
2633
2634 fail_alloc_cmds:
2635 fail_ready_state:
2636 iounmap(instance->reg_set);
2637
2638 fail_ioremap:
2639 pci_release_regions(instance->pdev);
2640
2641 return -EINVAL;
2642}
2643
2644/**
2645 * megasas_release_mfi - Reverses the FW initialization
2646 * @intance: Adapter soft state
2647 */
2648static void megasas_release_mfi(struct megasas_instance *instance)
2649{
2650 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
2651
2652 pci_free_consistent(instance->pdev, reply_q_sz,
2653 instance->reply_queue, instance->reply_queue_h);
2654
2655 megasas_free_cmds(instance);
2656
2657 iounmap(instance->reg_set);
2658
2659 pci_release_regions(instance->pdev);
2660}
2661
2662/**
2663 * megasas_get_seq_num - Gets latest event sequence numbers
2664 * @instance: Adapter soft state
2665 * @eli: FW event log sequence numbers information
2666 *
2667 * FW maintains a log of all events in a non-volatile area. Upper layers would
2668 * usually find out the latest sequence number of the events, the seq number at
2669 * the boot etc. They would "read" all the events below the latest seq number
2670 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
2671 * number), they would subsribe to AEN (asynchronous event notification) and
2672 * wait for the events to happen.
2673 */
2674static int
2675megasas_get_seq_num(struct megasas_instance *instance,
2676 struct megasas_evt_log_info *eli)
2677{
2678 struct megasas_cmd *cmd;
2679 struct megasas_dcmd_frame *dcmd;
2680 struct megasas_evt_log_info *el_info;
2681 dma_addr_t el_info_h = 0;
2682
2683 cmd = megasas_get_cmd(instance);
2684
2685 if (!cmd) {
2686 return -ENOMEM;
2687 }
2688
2689 dcmd = &cmd->frame->dcmd;
2690 el_info = pci_alloc_consistent(instance->pdev,
2691 sizeof(struct megasas_evt_log_info),
2692 &el_info_h);
2693
2694 if (!el_info) {
2695 megasas_return_cmd(instance, cmd);
2696 return -ENOMEM;
2697 }
2698
2699 memset(el_info, 0, sizeof(*el_info));
2700 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2701
2702 dcmd->cmd = MFI_CMD_DCMD;
2703 dcmd->cmd_status = 0x0;
2704 dcmd->sge_count = 1;
2705 dcmd->flags = MFI_FRAME_DIR_READ;
2706 dcmd->timeout = 0;
2707 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
2708 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
2709 dcmd->sgl.sge32[0].phys_addr = el_info_h;
2710 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
2711
2712 megasas_issue_blocked_cmd(instance, cmd);
2713
2714 /*
2715 * Copy the data back into callers buffer
2716 */
2717 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
2718
2719 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
2720 el_info, el_info_h);
2721
2722 megasas_return_cmd(instance, cmd);
2723
2724 return 0;
2725}
2726
2727/**
2728 * megasas_register_aen - Registers for asynchronous event notification
2729 * @instance: Adapter soft state
2730 * @seq_num: The starting sequence number
2731 * @class_locale: Class of the event
2732 *
2733 * This function subscribes for AEN for events beyond the @seq_num. It requests
2734 * to be notified if and only if the event is of type @class_locale
2735 */
2736static int
2737megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
2738 u32 class_locale_word)
2739{
2740 int ret_val;
2741 struct megasas_cmd *cmd;
2742 struct megasas_dcmd_frame *dcmd;
2743 union megasas_evt_class_locale curr_aen;
2744 union megasas_evt_class_locale prev_aen;
2745
2746 /*
2747 * If there an AEN pending already (aen_cmd), check if the
2748 * class_locale of that pending AEN is inclusive of the new
2749 * AEN request we currently have. If it is, then we don't have
2750 * to do anything. In other words, whichever events the current
2751 * AEN request is subscribing to, have already been subscribed
2752 * to.
2753 *
2754 * If the old_cmd is _not_ inclusive, then we have to abort
2755 * that command, form a class_locale that is superset of both
2756 * old and current and re-issue to the FW
2757 */
2758
2759 curr_aen.word = class_locale_word;
2760
2761 if (instance->aen_cmd) {
2762
2763 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
2764
2765 /*
2766 * A class whose enum value is smaller is inclusive of all
2767 * higher values. If a PROGRESS (= -1) was previously
2768 * registered, then a new registration requests for higher
2769 * classes need not be sent to FW. They are automatically
2770 * included.
2771 *
2772 * Locale numbers don't have such hierarchy. They are bitmap
2773 * values
2774 */
2775 if ((prev_aen.members.class <= curr_aen.members.class) &&
2776 !((prev_aen.members.locale & curr_aen.members.locale) ^
2777 curr_aen.members.locale)) {
2778 /*
2779 * Previously issued event registration includes
2780 * current request. Nothing to do.
2781 */
2782 return 0;
2783 } else {
2784 curr_aen.members.locale |= prev_aen.members.locale;
2785
2786 if (prev_aen.members.class < curr_aen.members.class)
2787 curr_aen.members.class = prev_aen.members.class;
2788
2789 instance->aen_cmd->abort_aen = 1;
2790 ret_val = megasas_issue_blocked_abort_cmd(instance,
2791 instance->
2792 aen_cmd);
2793
2794 if (ret_val) {
2795 printk(KERN_DEBUG "megasas: Failed to abort "
2796 "previous AEN command\n");
2797 return ret_val;
2798 }
2799 }
2800 }
2801
2802 cmd = megasas_get_cmd(instance);
2803
2804 if (!cmd)
2805 return -ENOMEM;
2806
2807 dcmd = &cmd->frame->dcmd;
2808
2809 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
2810
2811 /*
2812 * Prepare DCMD for aen registration
2813 */
2814 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2815
2816 dcmd->cmd = MFI_CMD_DCMD;
2817 dcmd->cmd_status = 0x0;
2818 dcmd->sge_count = 1;
2819 dcmd->flags = MFI_FRAME_DIR_READ;
2820 dcmd->timeout = 0;
2821 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
2822 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
2823 dcmd->mbox.w[0] = seq_num;
2824 dcmd->mbox.w[1] = curr_aen.word;
2825 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
2826 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
2827
Yang, Bof4c9a132009-10-06 14:43:28 -06002828 if (instance->aen_cmd != NULL) {
2829 megasas_return_cmd(instance, cmd);
2830 return 0;
2831 }
2832
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002833 /*
2834 * Store reference to the cmd used to register for AEN. When an
2835 * application wants us to register for AEN, we have to abort this
2836 * cmd and re-register with a new EVENT LOCALE supplied by that app
2837 */
2838 instance->aen_cmd = cmd;
2839
2840 /*
2841 * Issue the aen registration frame
2842 */
Yang, Bo0c79e682009-10-06 14:47:35 -06002843 instance->instancet->fire_cmd(instance,
2844 cmd->frame_phys_addr, 0, instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002845
2846 return 0;
2847}
2848
2849/**
2850 * megasas_start_aen - Subscribes to AEN during driver load time
2851 * @instance: Adapter soft state
2852 */
2853static int megasas_start_aen(struct megasas_instance *instance)
2854{
2855 struct megasas_evt_log_info eli;
2856 union megasas_evt_class_locale class_locale;
2857
2858 /*
2859 * Get the latest sequence number from FW
2860 */
2861 memset(&eli, 0, sizeof(eli));
2862
2863 if (megasas_get_seq_num(instance, &eli))
2864 return -1;
2865
2866 /*
2867 * Register AEN with FW for latest sequence number plus 1
2868 */
2869 class_locale.members.reserved = 0;
2870 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2871 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2872
2873 return megasas_register_aen(instance, eli.newest_seq_num + 1,
2874 class_locale.word);
2875}
2876
2877/**
2878 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2879 * @instance: Adapter soft state
2880 */
2881static int megasas_io_attach(struct megasas_instance *instance)
2882{
2883 struct Scsi_Host *host = instance->host;
2884
2885 /*
2886 * Export parameters required by SCSI mid-layer
2887 */
2888 host->irq = instance->pdev->irq;
2889 host->unique_id = instance->unique_id;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06002890 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2891 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2892 host->can_queue =
2893 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
2894 } else
2895 host->can_queue =
2896 instance->max_fw_cmds - MEGASAS_INT_CMDS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002897 host->this_id = instance->init_id;
2898 host->sg_tablesize = instance->max_num_sge;
2899 host->max_sectors = instance->max_sectors_per_req;
2900 host->cmd_per_lun = 128;
2901 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
2902 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
2903 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08002904 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002905
2906 /*
2907 * Notify the mid-layer about the new controller
2908 */
2909 if (scsi_add_host(host, &instance->pdev->dev)) {
2910 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2911 return -ENODEV;
2912 }
2913
2914 /*
2915 * Trigger SCSI to scan our drives
2916 */
2917 scsi_scan_host(host);
2918 return 0;
2919}
2920
bo yang31ea7082007-11-07 12:09:50 -05002921static int
2922megasas_set_dma_mask(struct pci_dev *pdev)
2923{
2924 /*
2925 * All our contollers are capable of performing 64-bit DMA
2926 */
2927 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07002928 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05002929
Yang Hongyang284901a2009-04-06 19:01:15 -07002930 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05002931 goto fail_set_dma_mask;
2932 }
2933 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07002934 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05002935 goto fail_set_dma_mask;
2936 }
2937 return 0;
2938
2939fail_set_dma_mask:
2940 return 1;
2941}
2942
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002943/**
2944 * megasas_probe_one - PCI hotplug entry point
2945 * @pdev: PCI device structure
2946 * @id: PCI ids of supported hotplugged adapter
2947 */
2948static int __devinit
2949megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2950{
2951 int rval;
2952 struct Scsi_Host *host;
2953 struct megasas_instance *instance;
2954
2955 /*
2956 * Announce PCI information
2957 */
2958 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2959 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2960 pdev->subsystem_device);
2961
2962 printk("bus %d:slot %d:func %d\n",
2963 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2964
2965 /*
2966 * PCI prepping: enable device set bus mastering and dma mask
2967 */
2968 rval = pci_enable_device(pdev);
2969
2970 if (rval) {
2971 return rval;
2972 }
2973
2974 pci_set_master(pdev);
2975
bo yang31ea7082007-11-07 12:09:50 -05002976 if (megasas_set_dma_mask(pdev))
2977 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002978
2979 host = scsi_host_alloc(&megasas_template,
2980 sizeof(struct megasas_instance));
2981
2982 if (!host) {
2983 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2984 goto fail_alloc_instance;
2985 }
2986
2987 instance = (struct megasas_instance *)host->hostdata;
2988 memset(instance, 0, sizeof(*instance));
2989
2990 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2991 &instance->producer_h);
2992 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2993 &instance->consumer_h);
2994
2995 if (!instance->producer || !instance->consumer) {
2996 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2997 "producer, consumer\n");
2998 goto fail_alloc_dma_buf;
2999 }
3000
3001 *instance->producer = 0;
3002 *instance->consumer = 0;
Yang, Boc3518832009-10-06 14:18:02 -06003003 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06003004 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003005 instance->ev = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003006
3007 instance->evt_detail = pci_alloc_consistent(pdev,
3008 sizeof(struct
3009 megasas_evt_detail),
3010 &instance->evt_detail_h);
3011
3012 if (!instance->evt_detail) {
3013 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
3014 "event detail structure\n");
3015 goto fail_alloc_dma_buf;
3016 }
3017
3018 /*
3019 * Initialize locks and queues
3020 */
3021 INIT_LIST_HEAD(&instance->cmd_pool);
3022
Sumant Patroe4a082c2006-05-30 12:03:37 -07003023 atomic_set(&instance->fw_outstanding,0);
3024
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003025 init_waitqueue_head(&instance->int_cmd_wait_q);
3026 init_waitqueue_head(&instance->abort_cmd_wait_q);
3027
3028 spin_lock_init(&instance->cmd_pool_lock);
Yang, Bo0c79e682009-10-06 14:47:35 -06003029 spin_lock_init(&instance->fire_lock);
bo yang7343eb62007-11-09 04:35:44 -05003030 spin_lock_init(&instance->completion_lock);
Yang, Boc3518832009-10-06 14:18:02 -06003031 spin_lock_init(&poll_aen_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003032
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02003033 mutex_init(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003034
3035 /*
3036 * Initialize PCI related and misc parameters
3037 */
3038 instance->pdev = pdev;
3039 instance->host = host;
3040 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
3041 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
3042
Yang, Bo7bebf5c2009-10-06 14:40:58 -06003043 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3044 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06003045 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06003046 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
3047 } else
3048 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
3049
Sumant Patro658dced2006-10-03 13:09:14 -07003050 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003051 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06003052 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003053 instance->last_time = 0;
Sumant Patro658dced2006-10-03 13:09:14 -07003054
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003055 /*
3056 * Initialize MFI Firmware
3057 */
3058 if (megasas_init_mfi(instance))
3059 goto fail_init_mfi;
3060
3061 /*
3062 * Register IRQ
3063 */
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07003064 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003065 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
3066 goto fail_irq;
3067 }
3068
Sumant Patro1341c932006-01-25 12:02:40 -08003069 instance->instancet->enable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003070
3071 /*
3072 * Store instance in PCI softstate
3073 */
3074 pci_set_drvdata(pdev, instance);
3075
3076 /*
3077 * Add this controller to megasas_mgmt_info structure so that it
3078 * can be exported to management applications
3079 */
3080 megasas_mgmt_info.count++;
3081 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
3082 megasas_mgmt_info.max_index++;
3083
3084 /*
3085 * Initiate AEN (Asynchronous Event Notification)
3086 */
3087 if (megasas_start_aen(instance)) {
3088 printk(KERN_DEBUG "megasas: start aen failed\n");
3089 goto fail_start_aen;
3090 }
3091
3092 /*
3093 * Register with SCSI mid-layer
3094 */
3095 if (megasas_io_attach(instance))
3096 goto fail_io_attach;
3097
Yang, Bo0c79e682009-10-06 14:47:35 -06003098 instance->unload = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003099 return 0;
3100
3101 fail_start_aen:
3102 fail_io_attach:
3103 megasas_mgmt_info.count--;
3104 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
3105 megasas_mgmt_info.max_index--;
3106
3107 pci_set_drvdata(pdev, NULL);
Sumant Patrob274cab2006-10-03 12:52:12 -07003108 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003109 free_irq(instance->pdev->irq, instance);
3110
3111 megasas_release_mfi(instance);
3112
3113 fail_irq:
3114 fail_init_mfi:
3115 fail_alloc_dma_buf:
3116 if (instance->evt_detail)
3117 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3118 instance->evt_detail,
3119 instance->evt_detail_h);
3120
3121 if (instance->producer)
3122 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3123 instance->producer_h);
3124 if (instance->consumer)
3125 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3126 instance->consumer_h);
3127 scsi_host_put(host);
3128
3129 fail_alloc_instance:
3130 fail_set_dma_mask:
3131 pci_disable_device(pdev);
3132
3133 return -ENODEV;
3134}
3135
3136/**
3137 * megasas_flush_cache - Requests FW to flush all its caches
3138 * @instance: Adapter soft state
3139 */
3140static void megasas_flush_cache(struct megasas_instance *instance)
3141{
3142 struct megasas_cmd *cmd;
3143 struct megasas_dcmd_frame *dcmd;
3144
3145 cmd = megasas_get_cmd(instance);
3146
3147 if (!cmd)
3148 return;
3149
3150 dcmd = &cmd->frame->dcmd;
3151
3152 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3153
3154 dcmd->cmd = MFI_CMD_DCMD;
3155 dcmd->cmd_status = 0x0;
3156 dcmd->sge_count = 0;
3157 dcmd->flags = MFI_FRAME_DIR_NONE;
3158 dcmd->timeout = 0;
3159 dcmd->data_xfer_len = 0;
3160 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
3161 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
3162
3163 megasas_issue_blocked_cmd(instance, cmd);
3164
3165 megasas_return_cmd(instance, cmd);
3166
3167 return;
3168}
3169
3170/**
3171 * megasas_shutdown_controller - Instructs FW to shutdown the controller
3172 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05003173 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003174 */
bo yang31ea7082007-11-07 12:09:50 -05003175static void megasas_shutdown_controller(struct megasas_instance *instance,
3176 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003177{
3178 struct megasas_cmd *cmd;
3179 struct megasas_dcmd_frame *dcmd;
3180
3181 cmd = megasas_get_cmd(instance);
3182
3183 if (!cmd)
3184 return;
3185
3186 if (instance->aen_cmd)
3187 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
3188
3189 dcmd = &cmd->frame->dcmd;
3190
3191 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3192
3193 dcmd->cmd = MFI_CMD_DCMD;
3194 dcmd->cmd_status = 0x0;
3195 dcmd->sge_count = 0;
3196 dcmd->flags = MFI_FRAME_DIR_NONE;
3197 dcmd->timeout = 0;
3198 dcmd->data_xfer_len = 0;
bo yang31ea7082007-11-07 12:09:50 -05003199 dcmd->opcode = opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003200
3201 megasas_issue_blocked_cmd(instance, cmd);
3202
3203 megasas_return_cmd(instance, cmd);
3204
3205 return;
3206}
3207
Jiri Slaby33139b22008-05-01 17:56:02 +02003208#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003209/**
bo yangad84db22007-11-09 04:40:16 -05003210 * megasas_suspend - driver suspend entry point
3211 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05003212 * @state: PCI power state to suspend routine
3213 */
Jiri Slaby33139b22008-05-01 17:56:02 +02003214static int
bo yang31ea7082007-11-07 12:09:50 -05003215megasas_suspend(struct pci_dev *pdev, pm_message_t state)
3216{
3217 struct Scsi_Host *host;
3218 struct megasas_instance *instance;
3219
3220 instance = pci_get_drvdata(pdev);
3221 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06003222 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05003223
bo yangad84db22007-11-09 04:40:16 -05003224 if (poll_mode_io)
3225 del_timer_sync(&instance->io_completion_timer);
3226
bo yang31ea7082007-11-07 12:09:50 -05003227 megasas_flush_cache(instance);
3228 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003229
3230 /* cancel the delayed work if this work still in queue */
3231 if (instance->ev != NULL) {
3232 struct megasas_aen_event *ev = instance->ev;
3233 cancel_delayed_work(
3234 (struct delayed_work *)&ev->hotplug_work);
3235 flush_scheduled_work();
3236 instance->ev = NULL;
3237 }
3238
bo yang31ea7082007-11-07 12:09:50 -05003239 tasklet_kill(&instance->isr_tasklet);
3240
3241 pci_set_drvdata(instance->pdev, instance);
3242 instance->instancet->disable_intr(instance->reg_set);
3243 free_irq(instance->pdev->irq, instance);
3244
3245 pci_save_state(pdev);
3246 pci_disable_device(pdev);
3247
3248 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3249
3250 return 0;
3251}
3252
3253/**
3254 * megasas_resume- driver resume entry point
3255 * @pdev: PCI device structure
3256 */
Jiri Slaby33139b22008-05-01 17:56:02 +02003257static int
bo yang31ea7082007-11-07 12:09:50 -05003258megasas_resume(struct pci_dev *pdev)
3259{
3260 int rval;
3261 struct Scsi_Host *host;
3262 struct megasas_instance *instance;
3263
3264 instance = pci_get_drvdata(pdev);
3265 host = instance->host;
3266 pci_set_power_state(pdev, PCI_D0);
3267 pci_enable_wake(pdev, PCI_D0, 0);
3268 pci_restore_state(pdev);
3269
3270 /*
3271 * PCI prepping: enable device set bus mastering and dma mask
3272 */
3273 rval = pci_enable_device(pdev);
3274
3275 if (rval) {
3276 printk(KERN_ERR "megasas: Enable device failed\n");
3277 return rval;
3278 }
3279
3280 pci_set_master(pdev);
3281
3282 if (megasas_set_dma_mask(pdev))
3283 goto fail_set_dma_mask;
3284
3285 /*
3286 * Initialize MFI Firmware
3287 */
3288
3289 *instance->producer = 0;
3290 *instance->consumer = 0;
3291
3292 atomic_set(&instance->fw_outstanding, 0);
3293
3294 /*
3295 * We expect the FW state to be READY
3296 */
3297 if (megasas_transition_to_ready(instance))
3298 goto fail_ready_state;
3299
3300 if (megasas_issue_init_mfi(instance))
3301 goto fail_init_mfi;
3302
3303 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
3304 (unsigned long)instance);
3305
3306 /*
3307 * Register IRQ
3308 */
3309 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED,
3310 "megasas", instance)) {
3311 printk(KERN_ERR "megasas: Failed to register IRQ\n");
3312 goto fail_irq;
3313 }
3314
3315 instance->instancet->enable_intr(instance->reg_set);
3316
3317 /*
3318 * Initiate AEN (Asynchronous Event Notification)
3319 */
3320 if (megasas_start_aen(instance))
3321 printk(KERN_ERR "megasas: Start AEN failed\n");
3322
bo yangad84db22007-11-09 04:40:16 -05003323 /* Initialize the cmd completion timer */
3324 if (poll_mode_io)
3325 megasas_start_timer(instance, &instance->io_completion_timer,
3326 megasas_io_completion_timer,
3327 MEGASAS_COMPLETION_TIMER_INTERVAL);
Yang, Bo0c79e682009-10-06 14:47:35 -06003328 instance->unload = 0;
3329
bo yang31ea7082007-11-07 12:09:50 -05003330 return 0;
3331
3332fail_irq:
3333fail_init_mfi:
3334 if (instance->evt_detail)
3335 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3336 instance->evt_detail,
3337 instance->evt_detail_h);
3338
3339 if (instance->producer)
3340 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3341 instance->producer_h);
3342 if (instance->consumer)
3343 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3344 instance->consumer_h);
3345 scsi_host_put(host);
3346
3347fail_set_dma_mask:
3348fail_ready_state:
3349
3350 pci_disable_device(pdev);
3351
3352 return -ENODEV;
3353}
Jiri Slaby33139b22008-05-01 17:56:02 +02003354#else
3355#define megasas_suspend NULL
3356#define megasas_resume NULL
3357#endif
bo yang31ea7082007-11-07 12:09:50 -05003358
3359/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003360 * megasas_detach_one - PCI hot"un"plug entry point
3361 * @pdev: PCI device structure
3362 */
Jiri Slaby33139b22008-05-01 17:56:02 +02003363static void __devexit megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003364{
3365 int i;
3366 struct Scsi_Host *host;
3367 struct megasas_instance *instance;
3368
3369 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06003370 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003371 host = instance->host;
3372
bo yangad84db22007-11-09 04:40:16 -05003373 if (poll_mode_io)
3374 del_timer_sync(&instance->io_completion_timer);
3375
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003376 scsi_remove_host(instance->host);
3377 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05003378 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003379
3380 /* cancel the delayed work if this work still in queue*/
3381 if (instance->ev != NULL) {
3382 struct megasas_aen_event *ev = instance->ev;
3383 cancel_delayed_work(
3384 (struct delayed_work *)&ev->hotplug_work);
3385 flush_scheduled_work();
3386 instance->ev = NULL;
3387 }
3388
Sumant Patro5d018ad2006-10-03 13:13:18 -07003389 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003390
3391 /*
3392 * Take the instance off the instance array. Note that we will not
3393 * decrement the max_index. We let this array be sparse array
3394 */
3395 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3396 if (megasas_mgmt_info.instance[i] == instance) {
3397 megasas_mgmt_info.count--;
3398 megasas_mgmt_info.instance[i] = NULL;
3399
3400 break;
3401 }
3402 }
3403
3404 pci_set_drvdata(instance->pdev, NULL);
3405
Sumant Patrob274cab2006-10-03 12:52:12 -07003406 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003407
3408 free_irq(instance->pdev->irq, instance);
3409
3410 megasas_release_mfi(instance);
3411
3412 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3413 instance->evt_detail, instance->evt_detail_h);
3414
3415 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3416 instance->producer_h);
3417
3418 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3419 instance->consumer_h);
3420
3421 scsi_host_put(host);
3422
3423 pci_set_drvdata(pdev, NULL);
3424
3425 pci_disable_device(pdev);
3426
3427 return;
3428}
3429
3430/**
3431 * megasas_shutdown - Shutdown entry point
3432 * @device: Generic device structure
3433 */
3434static void megasas_shutdown(struct pci_dev *pdev)
3435{
3436 struct megasas_instance *instance = pci_get_drvdata(pdev);
Yang, Bo0c79e682009-10-06 14:47:35 -06003437 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003438 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07003439 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003440}
3441
3442/**
3443 * megasas_mgmt_open - char node "open" entry point
3444 */
3445static int megasas_mgmt_open(struct inode *inode, struct file *filep)
3446{
Jonathan Corbetf2b98572008-05-18 15:32:43 -06003447 cycle_kernel_lock();
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003448 /*
3449 * Allow only those users with admin rights
3450 */
3451 if (!capable(CAP_SYS_ADMIN))
3452 return -EACCES;
3453
3454 return 0;
3455}
3456
3457/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003458 * megasas_mgmt_fasync - Async notifier registration from applications
3459 *
3460 * This function adds the calling process to a driver global queue. When an
3461 * event occurs, SIGIO will be sent to all processes in this queue.
3462 */
3463static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
3464{
3465 int rc;
3466
Arjan van de Ven0b950672006-01-11 13:16:10 +01003467 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003468
3469 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
3470
Arjan van de Ven0b950672006-01-11 13:16:10 +01003471 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003472
3473 if (rc >= 0) {
3474 /* For sanity check when we get ioctl */
3475 filep->private_data = filep;
3476 return 0;
3477 }
3478
3479 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
3480
3481 return rc;
3482}
3483
3484/**
Yang, Boc3518832009-10-06 14:18:02 -06003485 * megasas_mgmt_poll - char node "poll" entry point
3486 * */
3487static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
3488{
3489 unsigned int mask;
3490 unsigned long flags;
3491 poll_wait(file, &megasas_poll_wait, wait);
3492 spin_lock_irqsave(&poll_aen_lock, flags);
3493 if (megasas_poll_wait_aen)
3494 mask = (POLLIN | POLLRDNORM);
3495 else
3496 mask = 0;
3497 spin_unlock_irqrestore(&poll_aen_lock, flags);
3498 return mask;
3499}
3500
3501/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003502 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
3503 * @instance: Adapter soft state
3504 * @argp: User's ioctl packet
3505 */
3506static int
3507megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
3508 struct megasas_iocpacket __user * user_ioc,
3509 struct megasas_iocpacket *ioc)
3510{
3511 struct megasas_sge32 *kern_sge32;
3512 struct megasas_cmd *cmd;
3513 void *kbuff_arr[MAX_IOCTL_SGE];
3514 dma_addr_t buf_handle = 0;
3515 int error = 0, i;
3516 void *sense = NULL;
3517 dma_addr_t sense_handle;
3518 u32 *sense_ptr;
3519
3520 memset(kbuff_arr, 0, sizeof(kbuff_arr));
3521
3522 if (ioc->sge_count > MAX_IOCTL_SGE) {
3523 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
3524 ioc->sge_count, MAX_IOCTL_SGE);
3525 return -EINVAL;
3526 }
3527
3528 cmd = megasas_get_cmd(instance);
3529 if (!cmd) {
3530 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
3531 return -ENOMEM;
3532 }
3533
3534 /*
3535 * User's IOCTL packet has 2 frames (maximum). Copy those two
3536 * frames into our cmd's frames. cmd->frame's context will get
3537 * overwritten when we copy from user's frames. So set that value
3538 * alone separately
3539 */
3540 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
3541 cmd->frame->hdr.context = cmd->index;
Yang, Boc3518832009-10-06 14:18:02 -06003542 cmd->frame->hdr.pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003543
3544 /*
3545 * The management interface between applications and the fw uses
3546 * MFI frames. E.g, RAID configuration changes, LD property changes
3547 * etc are accomplishes through different kinds of MFI frames. The
3548 * driver needs to care only about substituting user buffers with
3549 * kernel buffers in SGLs. The location of SGL is embedded in the
3550 * struct iocpacket itself.
3551 */
3552 kern_sge32 = (struct megasas_sge32 *)
3553 ((unsigned long)cmd->frame + ioc->sgl_off);
3554
3555 /*
3556 * For each user buffer, create a mirror buffer and copy in
3557 */
3558 for (i = 0; i < ioc->sge_count; i++) {
Sumant Patro9f35fa8a2007-02-14 12:55:45 -08003559 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003560 ioc->sgl[i].iov_len,
Sumant Patro9f35fa8a2007-02-14 12:55:45 -08003561 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003562 if (!kbuff_arr[i]) {
3563 printk(KERN_DEBUG "megasas: Failed to alloc "
3564 "kernel SGL buffer for IOCTL \n");
3565 error = -ENOMEM;
3566 goto out;
3567 }
3568
3569 /*
3570 * We don't change the dma_coherent_mask, so
3571 * pci_alloc_consistent only returns 32bit addresses
3572 */
3573 kern_sge32[i].phys_addr = (u32) buf_handle;
3574 kern_sge32[i].length = ioc->sgl[i].iov_len;
3575
3576 /*
3577 * We created a kernel buffer corresponding to the
3578 * user buffer. Now copy in from the user buffer
3579 */
3580 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
3581 (u32) (ioc->sgl[i].iov_len))) {
3582 error = -EFAULT;
3583 goto out;
3584 }
3585 }
3586
3587 if (ioc->sense_len) {
Sumant Patro9f35fa8a2007-02-14 12:55:45 -08003588 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
3589 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003590 if (!sense) {
3591 error = -ENOMEM;
3592 goto out;
3593 }
3594
3595 sense_ptr =
3596 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
3597 *sense_ptr = sense_handle;
3598 }
3599
3600 /*
3601 * Set the sync_cmd flag so that the ISR knows not to complete this
3602 * cmd to the SCSI mid-layer
3603 */
3604 cmd->sync_cmd = 1;
3605 megasas_issue_blocked_cmd(instance, cmd);
3606 cmd->sync_cmd = 0;
3607
3608 /*
3609 * copy out the kernel buffers to user buffers
3610 */
3611 for (i = 0; i < ioc->sge_count; i++) {
3612 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
3613 ioc->sgl[i].iov_len)) {
3614 error = -EFAULT;
3615 goto out;
3616 }
3617 }
3618
3619 /*
3620 * copy out the sense
3621 */
3622 if (ioc->sense_len) {
3623 /*
bo yangb70a41e2008-03-18 03:13:06 -04003624 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003625 * sense buffer address
3626 */
bo yangb70a41e2008-03-18 03:13:06 -04003627 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
3628 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003629
bo yangb70a41e2008-03-18 03:13:06 -04003630 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
3631 sense, ioc->sense_len)) {
bo yangb10c36a2007-11-09 04:28:47 -05003632 printk(KERN_ERR "megasas: Failed to copy out to user "
3633 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003634 error = -EFAULT;
3635 goto out;
3636 }
3637 }
3638
3639 /*
3640 * copy the status codes returned by the fw
3641 */
3642 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
3643 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
3644 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
3645 error = -EFAULT;
3646 }
3647
3648 out:
3649 if (sense) {
Sumant Patro9f35fa8a2007-02-14 12:55:45 -08003650 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003651 sense, sense_handle);
3652 }
3653
3654 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
Sumant Patro9f35fa8a2007-02-14 12:55:45 -08003655 dma_free_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003656 kern_sge32[i].length,
3657 kbuff_arr[i], kern_sge32[i].phys_addr);
3658 }
3659
3660 megasas_return_cmd(instance, cmd);
3661 return error;
3662}
3663
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003664static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
3665{
3666 struct megasas_iocpacket __user *user_ioc =
3667 (struct megasas_iocpacket __user *)arg;
3668 struct megasas_iocpacket *ioc;
3669 struct megasas_instance *instance;
3670 int error;
3671
3672 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
3673 if (!ioc)
3674 return -ENOMEM;
3675
3676 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
3677 error = -EFAULT;
3678 goto out_kfree_ioc;
3679 }
3680
3681 instance = megasas_lookup_instance(ioc->host_no);
3682 if (!instance) {
3683 error = -ENODEV;
3684 goto out_kfree_ioc;
3685 }
3686
Yang, Bo0c79e682009-10-06 14:47:35 -06003687 if (instance->hw_crit_error == 1) {
3688 printk(KERN_DEBUG "Controller in Crit ERROR\n");
3689 error = -ENODEV;
3690 goto out_kfree_ioc;
3691 }
3692
3693 if (instance->unload == 1) {
3694 error = -ENODEV;
3695 goto out_kfree_ioc;
3696 }
3697
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003698 /*
3699 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
3700 */
3701 if (down_interruptible(&instance->ioctl_sem)) {
3702 error = -ERESTARTSYS;
3703 goto out_kfree_ioc;
3704 }
3705 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
3706 up(&instance->ioctl_sem);
3707
3708 out_kfree_ioc:
3709 kfree(ioc);
3710 return error;
3711}
3712
3713static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
3714{
3715 struct megasas_instance *instance;
3716 struct megasas_aen aen;
3717 int error;
3718
3719 if (file->private_data != file) {
3720 printk(KERN_DEBUG "megasas: fasync_helper was not "
3721 "called first\n");
3722 return -EINVAL;
3723 }
3724
3725 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
3726 return -EFAULT;
3727
3728 instance = megasas_lookup_instance(aen.host_no);
3729
3730 if (!instance)
3731 return -ENODEV;
3732
Yang, Bo0c79e682009-10-06 14:47:35 -06003733 if (instance->hw_crit_error == 1) {
3734 error = -ENODEV;
3735 }
3736
3737 if (instance->unload == 1) {
3738 return -ENODEV;
3739 }
3740
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02003741 mutex_lock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003742 error = megasas_register_aen(instance, aen.seq_num,
3743 aen.class_locale_word);
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02003744 mutex_unlock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003745 return error;
3746}
3747
3748/**
3749 * megasas_mgmt_ioctl - char node ioctl entry point
3750 */
3751static long
3752megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3753{
3754 switch (cmd) {
3755 case MEGASAS_IOC_FIRMWARE:
3756 return megasas_mgmt_ioctl_fw(file, arg);
3757
3758 case MEGASAS_IOC_GET_AEN:
3759 return megasas_mgmt_ioctl_aen(file, arg);
3760 }
3761
3762 return -ENOTTY;
3763}
3764
3765#ifdef CONFIG_COMPAT
3766static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
3767{
3768 struct compat_megasas_iocpacket __user *cioc =
3769 (struct compat_megasas_iocpacket __user *)arg;
3770 struct megasas_iocpacket __user *ioc =
3771 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
3772 int i;
3773 int error = 0;
3774
Jeff Garzik83aabc12006-10-04 06:34:03 -04003775 if (clear_user(ioc, sizeof(*ioc)))
3776 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003777
3778 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
3779 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
3780 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
3781 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
3782 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
3783 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
3784 return -EFAULT;
3785
3786 for (i = 0; i < MAX_IOCTL_SGE; i++) {
3787 compat_uptr_t ptr;
3788
3789 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
3790 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
3791 copy_in_user(&ioc->sgl[i].iov_len,
3792 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
3793 return -EFAULT;
3794 }
3795
3796 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
3797
3798 if (copy_in_user(&cioc->frame.hdr.cmd_status,
3799 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
3800 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
3801 return -EFAULT;
3802 }
3803 return error;
3804}
3805
3806static long
3807megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
3808 unsigned long arg)
3809{
3810 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08003811 case MEGASAS_IOC_FIRMWARE32:
3812 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003813 case MEGASAS_IOC_GET_AEN:
3814 return megasas_mgmt_ioctl_aen(file, arg);
3815 }
3816
3817 return -ENOTTY;
3818}
3819#endif
3820
3821/*
3822 * File operations structure for management interface
3823 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08003824static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003825 .owner = THIS_MODULE,
3826 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003827 .fasync = megasas_mgmt_fasync,
3828 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06003829 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003830#ifdef CONFIG_COMPAT
3831 .compat_ioctl = megasas_mgmt_compat_ioctl,
3832#endif
3833};
3834
3835/*
3836 * PCI hotplug support registration structure
3837 */
3838static struct pci_driver megasas_pci_driver = {
3839
3840 .name = "megaraid_sas",
3841 .id_table = megasas_pci_table,
3842 .probe = megasas_probe_one,
3843 .remove = __devexit_p(megasas_detach_one),
bo yang31ea7082007-11-07 12:09:50 -05003844 .suspend = megasas_suspend,
3845 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003846 .shutdown = megasas_shutdown,
3847};
3848
3849/*
3850 * Sysfs driver attributes
3851 */
3852static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
3853{
3854 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
3855 MEGASAS_VERSION);
3856}
3857
3858static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
3859
3860static ssize_t
3861megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
3862{
3863 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
3864 MEGASAS_RELDATE);
3865}
3866
3867static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
3868 NULL);
3869
Sumant Patro658dced2006-10-03 13:09:14 -07003870static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06003871megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
3872{
3873 return sprintf(buf, "%u\n", support_poll_for_event);
3874}
3875
3876static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
3877 megasas_sysfs_show_support_poll_for_event, NULL);
3878
3879static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07003880megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
3881{
bo yangad84db22007-11-09 04:40:16 -05003882 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07003883}
3884
3885static ssize_t
3886megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
3887{
3888 int retval = count;
3889 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
3890 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
3891 retval = -EINVAL;
3892 }
3893 return retval;
3894}
3895
Joe Malicki66dca9b2008-08-14 17:14:48 -04003896static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05003897 megasas_sysfs_set_dbg_lvl);
3898
3899static ssize_t
3900megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
3901{
3902 return sprintf(buf, "%u\n", poll_mode_io);
3903}
3904
3905static ssize_t
3906megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
3907 const char *buf, size_t count)
3908{
3909 int retval = count;
3910 int tmp = poll_mode_io;
3911 int i;
3912 struct megasas_instance *instance;
3913
3914 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
3915 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
3916 retval = -EINVAL;
3917 }
3918
3919 /*
3920 * Check if poll_mode_io is already set or is same as previous value
3921 */
3922 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
3923 goto out;
3924
3925 if (poll_mode_io) {
3926 /*
3927 * Start timers for all adapters
3928 */
3929 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3930 instance = megasas_mgmt_info.instance[i];
3931 if (instance) {
3932 megasas_start_timer(instance,
3933 &instance->io_completion_timer,
3934 megasas_io_completion_timer,
3935 MEGASAS_COMPLETION_TIMER_INTERVAL);
3936 }
3937 }
3938 } else {
3939 /*
3940 * Delete timers for all adapters
3941 */
3942 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3943 instance = megasas_mgmt_info.instance[i];
3944 if (instance)
3945 del_timer_sync(&instance->io_completion_timer);
3946 }
3947 }
3948
3949out:
3950 return retval;
3951}
3952
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003953static void
3954megasas_aen_polling(struct work_struct *work)
3955{
3956 struct megasas_aen_event *ev =
3957 container_of(work, struct megasas_aen_event, hotplug_work);
3958 struct megasas_instance *instance = ev->instance;
3959 union megasas_evt_class_locale class_locale;
3960 struct Scsi_Host *host;
3961 struct scsi_device *sdev1;
3962 u16 pd_index = 0;
3963 int i, j, doscan = 0;
3964 u32 seq_num;
3965 int error;
3966
3967 if (!instance) {
3968 printk(KERN_ERR "invalid instance!\n");
3969 kfree(ev);
3970 return;
3971 }
3972 instance->ev = NULL;
3973 host = instance->host;
3974 if (instance->evt_detail) {
3975
3976 switch (instance->evt_detail->code) {
3977 case MR_EVT_PD_INSERTED:
3978 case MR_EVT_PD_REMOVED:
3979 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
3980 doscan = 1;
3981 break;
3982 default:
3983 doscan = 0;
3984 break;
3985 }
3986 } else {
3987 printk(KERN_ERR "invalid evt_detail!\n");
3988 kfree(ev);
3989 return;
3990 }
3991
3992 if (doscan) {
3993 printk(KERN_INFO "scanning ...\n");
3994 megasas_get_pd_list(instance);
3995 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
3996 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
3997 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
3998 sdev1 = scsi_device_lookup(host, i, j, 0);
3999 if (instance->pd_list[pd_index].driveState ==
4000 MR_PD_STATE_SYSTEM) {
4001 if (!sdev1) {
4002 scsi_add_device(host, i, j, 0);
4003 }
4004 if (sdev1)
4005 scsi_device_put(sdev1);
4006 } else {
4007 if (sdev1) {
4008 scsi_remove_device(sdev1);
4009 scsi_device_put(sdev1);
4010 }
4011 }
4012 }
4013 }
4014 }
4015
4016 if ( instance->aen_cmd != NULL ) {
4017 kfree(ev);
4018 return ;
4019 }
4020
4021 seq_num = instance->evt_detail->seq_num + 1;
4022
4023 /* Register AEN with FW for latest sequence number plus 1 */
4024 class_locale.members.reserved = 0;
4025 class_locale.members.locale = MR_EVT_LOCALE_ALL;
4026 class_locale.members.class = MR_EVT_CLASS_DEBUG;
4027 mutex_lock(&instance->aen_mutex);
4028 error = megasas_register_aen(instance, seq_num,
4029 class_locale.word);
4030 mutex_unlock(&instance->aen_mutex);
4031
4032 if (error)
4033 printk(KERN_ERR "register aen failed error %x\n", error);
4034
4035 kfree(ev);
4036}
4037
4038
bo yangad84db22007-11-09 04:40:16 -05004039static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUGO,
4040 megasas_sysfs_show_poll_mode_io,
4041 megasas_sysfs_set_poll_mode_io);
Sumant Patro658dced2006-10-03 13:09:14 -07004042
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004043/**
4044 * megasas_init - Driver load entry point
4045 */
4046static int __init megasas_init(void)
4047{
4048 int rval;
4049
4050 /*
4051 * Announce driver version and other information
4052 */
4053 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
4054 MEGASAS_EXT_VERSION);
4055
Yang, Bo72c4fd32009-10-06 14:20:59 -06004056 support_poll_for_event = 2;
4057
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004058 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
4059
4060 /*
4061 * Register character device node
4062 */
4063 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
4064
4065 if (rval < 0) {
4066 printk(KERN_DEBUG "megasas: failed to open device node\n");
4067 return rval;
4068 }
4069
4070 megasas_mgmt_majorno = rval;
4071
4072 /*
4073 * Register ourselves as PCI hotplug module
4074 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00004075 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004076
4077 if (rval) {
4078 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04004079 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004080 }
4081
Jeff Garzik83aabc12006-10-04 06:34:03 -04004082 rval = driver_create_file(&megasas_pci_driver.driver,
4083 &driver_attr_version);
4084 if (rval)
4085 goto err_dcf_attr_ver;
4086 rval = driver_create_file(&megasas_pci_driver.driver,
4087 &driver_attr_release_date);
4088 if (rval)
4089 goto err_dcf_rel_date;
Yang, Bo72c4fd32009-10-06 14:20:59 -06004090
4091 rval = driver_create_file(&megasas_pci_driver.driver,
4092 &driver_attr_support_poll_for_event);
4093 if (rval)
4094 goto err_dcf_support_poll_for_event;
4095
Jeff Garzik83aabc12006-10-04 06:34:03 -04004096 rval = driver_create_file(&megasas_pci_driver.driver,
4097 &driver_attr_dbg_lvl);
4098 if (rval)
4099 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05004100 rval = driver_create_file(&megasas_pci_driver.driver,
4101 &driver_attr_poll_mode_io);
4102 if (rval)
4103 goto err_dcf_poll_mode_io;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004104
4105 return rval;
bo yangad84db22007-11-09 04:40:16 -05004106
4107err_dcf_poll_mode_io:
4108 driver_remove_file(&megasas_pci_driver.driver,
4109 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04004110err_dcf_dbg_lvl:
4111 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06004112 &driver_attr_support_poll_for_event);
4113
4114err_dcf_support_poll_for_event:
4115 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04004116 &driver_attr_release_date);
Yang, Bo72c4fd32009-10-06 14:20:59 -06004117
Jeff Garzik83aabc12006-10-04 06:34:03 -04004118err_dcf_rel_date:
4119 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
4120err_dcf_attr_ver:
4121 pci_unregister_driver(&megasas_pci_driver);
4122err_pcidrv:
4123 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
4124 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004125}
4126
4127/**
4128 * megasas_exit - Driver unload entry point
4129 */
4130static void __exit megasas_exit(void)
4131{
Sumant Patro658dced2006-10-03 13:09:14 -07004132 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05004133 &driver_attr_poll_mode_io);
4134 driver_remove_file(&megasas_pci_driver.driver,
Sumant Patro658dced2006-10-03 13:09:14 -07004135 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04004136 driver_remove_file(&megasas_pci_driver.driver,
4137 &driver_attr_release_date);
4138 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004139
4140 pci_unregister_driver(&megasas_pci_driver);
4141 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
4142}
4143
4144module_init(megasas_init);
4145module_exit(megasas_exit);