blob: 4c04a68bad6c3fb2486ef4476dd349f06680d7df [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
229megasas_fire_cmd_xscale(dma_addr_t frame_phys_addr,u32 frame_count, struct megasas_register_set __iomem *regs)
230{
231 writel((frame_phys_addr >> 3)|(frame_count),
232 &(regs)->inbound_queue_port);
233}
234
235static struct megasas_instance_template megasas_instance_template_xscale = {
236
237 .fire_cmd = megasas_fire_cmd_xscale,
238 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700239 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800240 .clear_intr = megasas_clear_intr_xscale,
241 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
242};
243
244/**
245* This is the end of set of functions & definitions specific
246* to xscale (deviceid : 1064R, PERC5) controllers
247*/
248
249/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800250* The following functions are defined for ppc (deviceid : 0x60)
251* controllers
252*/
253
254/**
255 * megasas_enable_intr_ppc - Enables interrupts
256 * @regs: MFI register set
257 */
258static inline void
259megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
260{
261 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
262
263 writel(~0x80000004, &(regs)->outbound_intr_mask);
264
265 /* Dummy readl to force pci flush */
266 readl(&regs->outbound_intr_mask);
267}
268
269/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700270 * megasas_disable_intr_ppc - Disable interrupt
271 * @regs: MFI register set
272 */
273static inline void
274megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
275{
276 u32 mask = 0xFFFFFFFF;
277 writel(mask, &regs->outbound_intr_mask);
278 /* Dummy readl to force pci flush */
279 readl(&regs->outbound_intr_mask);
280}
281
282/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800283 * megasas_read_fw_status_reg_ppc - returns the current FW status value
284 * @regs: MFI register set
285 */
286static u32
287megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
288{
289 return readl(&(regs)->outbound_scratch_pad);
290}
291
292/**
293 * megasas_clear_interrupt_ppc - Check & clear interrupt
294 * @regs: MFI register set
295 */
296static int
297megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
298{
299 u32 status;
300 /*
301 * Check if it is our interrupt
302 */
303 status = readl(&regs->outbound_intr_status);
304
305 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
306 return 1;
307 }
308
309 /*
310 * Clear the interrupt by writing back the same value
311 */
312 writel(status, &regs->outbound_doorbell_clear);
313
Yang, Bo06f579d2008-08-10 12:42:37 -0700314 /* Dummy readl to force pci flush */
315 readl(&regs->outbound_doorbell_clear);
316
Sumant Patrof9876f02006-02-03 15:34:35 -0800317 return 0;
318}
319/**
320 * megasas_fire_cmd_ppc - Sends command to the FW
321 * @frame_phys_addr : Physical address of cmd
322 * @frame_count : Number of frames for the command
323 * @regs : MFI register set
324 */
325static inline void
326megasas_fire_cmd_ppc(dma_addr_t frame_phys_addr, u32 frame_count, struct megasas_register_set __iomem *regs)
327{
328 writel((frame_phys_addr | (frame_count<<1))|1,
329 &(regs)->inbound_queue_port);
330}
331
332static struct megasas_instance_template megasas_instance_template_ppc = {
333
334 .fire_cmd = megasas_fire_cmd_ppc,
335 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700336 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800337 .clear_intr = megasas_clear_intr_ppc,
338 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
339};
340
341/**
Yang, Bo87911122009-10-06 14:31:54 -0600342 * megasas_enable_intr_skinny - Enables interrupts
343 * @regs: MFI register set
344 */
345static inline void
346megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
347{
348 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
349
350 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
351
352 /* Dummy readl to force pci flush */
353 readl(&regs->outbound_intr_mask);
354}
355
356/**
357 * megasas_disable_intr_skinny - Disables interrupt
358 * @regs: MFI register set
359 */
360static inline void
361megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
362{
363 u32 mask = 0xFFFFFFFF;
364 writel(mask, &regs->outbound_intr_mask);
365 /* Dummy readl to force pci flush */
366 readl(&regs->outbound_intr_mask);
367}
368
369/**
370 * megasas_read_fw_status_reg_skinny - returns the current FW status value
371 * @regs: MFI register set
372 */
373static u32
374megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
375{
376 return readl(&(regs)->outbound_scratch_pad);
377}
378
379/**
380 * megasas_clear_interrupt_skinny - Check & clear interrupt
381 * @regs: MFI register set
382 */
383static int
384megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
385{
386 u32 status;
387 /*
388 * Check if it is our interrupt
389 */
390 status = readl(&regs->outbound_intr_status);
391
392 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
393 return 1;
394 }
395
396 /*
397 * Clear the interrupt by writing back the same value
398 */
399 writel(status, &regs->outbound_intr_status);
400
401 /*
402 * dummy read to flush PCI
403 */
404 readl(&regs->outbound_intr_status);
405
406 return 0;
407}
408
409/**
410 * megasas_fire_cmd_skinny - Sends command to the FW
411 * @frame_phys_addr : Physical address of cmd
412 * @frame_count : Number of frames for the command
413 * @regs : MFI register set
414 */
415static inline void
416megasas_fire_cmd_skinny(dma_addr_t frame_phys_addr, u32 frame_count,
417 struct megasas_register_set __iomem *regs)
418{
419 writel(0, &(regs)->inbound_high_queue_port);
420 writel((frame_phys_addr | (frame_count<<1))|1,
421 &(regs)->inbound_low_queue_port);
422}
423
424static struct megasas_instance_template megasas_instance_template_skinny = {
425
426 .fire_cmd = megasas_fire_cmd_skinny,
427 .enable_intr = megasas_enable_intr_skinny,
428 .disable_intr = megasas_disable_intr_skinny,
429 .clear_intr = megasas_clear_intr_skinny,
430 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
431};
432
433
434/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700435* The following functions are defined for gen2 (deviceid : 0x78 0x79)
436* controllers
437*/
438
439/**
440 * megasas_enable_intr_gen2 - Enables interrupts
441 * @regs: MFI register set
442 */
443static inline void
444megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
445{
446 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
447
448 /* write ~0x00000005 (4 & 1) to the intr mask*/
449 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
450
451 /* Dummy readl to force pci flush */
452 readl(&regs->outbound_intr_mask);
453}
454
455/**
456 * megasas_disable_intr_gen2 - Disables interrupt
457 * @regs: MFI register set
458 */
459static inline void
460megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
461{
462 u32 mask = 0xFFFFFFFF;
463 writel(mask, &regs->outbound_intr_mask);
464 /* Dummy readl to force pci flush */
465 readl(&regs->outbound_intr_mask);
466}
467
468/**
469 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
470 * @regs: MFI register set
471 */
472static u32
473megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
474{
475 return readl(&(regs)->outbound_scratch_pad);
476}
477
478/**
479 * megasas_clear_interrupt_gen2 - Check & clear interrupt
480 * @regs: MFI register set
481 */
482static int
483megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
484{
485 u32 status;
486 /*
487 * Check if it is our interrupt
488 */
489 status = readl(&regs->outbound_intr_status);
490
491 if (!(status & MFI_GEN2_ENABLE_INTERRUPT_MASK))
492 return 1;
493
494 /*
495 * Clear the interrupt by writing back the same value
496 */
497 writel(status, &regs->outbound_doorbell_clear);
498
499 /* Dummy readl to force pci flush */
500 readl(&regs->outbound_intr_status);
501
502 return 0;
503}
504/**
505 * megasas_fire_cmd_gen2 - Sends command to the FW
506 * @frame_phys_addr : Physical address of cmd
507 * @frame_count : Number of frames for the command
508 * @regs : MFI register set
509 */
510static inline void
511megasas_fire_cmd_gen2(dma_addr_t frame_phys_addr, u32 frame_count,
512 struct megasas_register_set __iomem *regs)
513{
514 writel((frame_phys_addr | (frame_count<<1))|1,
515 &(regs)->inbound_queue_port);
516}
517
518static struct megasas_instance_template megasas_instance_template_gen2 = {
519
520 .fire_cmd = megasas_fire_cmd_gen2,
521 .enable_intr = megasas_enable_intr_gen2,
522 .disable_intr = megasas_disable_intr_gen2,
523 .clear_intr = megasas_clear_intr_gen2,
524 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
525};
526
527/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800528* This is the end of set of functions & definitions
529* specific to ppc (deviceid : 0x60) controllers
530*/
531
532/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400533 * megasas_issue_polled - Issues a polling command
534 * @instance: Adapter soft state
535 * @cmd: Command packet to be issued
536 *
537 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
538 */
539static int
540megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
541{
542 int i;
543 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
544
545 struct megasas_header *frame_hdr = &cmd->frame->hdr;
546
547 frame_hdr->cmd_status = 0xFF;
548 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
549
550 /*
551 * Issue the frame using inbound queue port
552 */
Sumant Patro1341c932006-01-25 12:02:40 -0800553 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400554
555 /*
556 * Wait for cmd_status to change
557 */
558 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
559 rmb();
560 msleep(1);
561 }
562
563 if (frame_hdr->cmd_status == 0xff)
564 return -ETIME;
565
566 return 0;
567}
568
569/**
570 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
571 * @instance: Adapter soft state
572 * @cmd: Command to be issued
573 *
574 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -0700575 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400576 * Used to issue ioctl commands.
577 */
578static int
579megasas_issue_blocked_cmd(struct megasas_instance *instance,
580 struct megasas_cmd *cmd)
581{
582 cmd->cmd_status = ENODATA;
583
Sumant Patro1341c932006-01-25 12:02:40 -0800584 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400585
Sumant Patro2a3681e2006-10-03 13:19:21 -0700586 wait_event_timeout(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA),
587 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400588
589 return 0;
590}
591
592/**
593 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
594 * @instance: Adapter soft state
595 * @cmd_to_abort: Previously issued cmd to be aborted
596 *
597 * MFI firmware can abort previously issued AEN comamnd (automatic event
598 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -0700599 * cmd and waits for return status.
600 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400601 */
602static int
603megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
604 struct megasas_cmd *cmd_to_abort)
605{
606 struct megasas_cmd *cmd;
607 struct megasas_abort_frame *abort_fr;
608
609 cmd = megasas_get_cmd(instance);
610
611 if (!cmd)
612 return -1;
613
614 abort_fr = &cmd->frame->abort;
615
616 /*
617 * Prepare and issue the abort frame
618 */
619 abort_fr->cmd = MFI_CMD_ABORT;
620 abort_fr->cmd_status = 0xFF;
621 abort_fr->flags = 0;
622 abort_fr->abort_context = cmd_to_abort->index;
623 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
624 abort_fr->abort_mfi_phys_addr_hi = 0;
625
626 cmd->sync_cmd = 1;
627 cmd->cmd_status = 0xFF;
628
Sumant Patro1341c932006-01-25 12:02:40 -0800629 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400630
631 /*
632 * Wait for this cmd to complete
633 */
Sumant Patro2a3681e2006-10-03 13:19:21 -0700634 wait_event_timeout(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF),
635 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400636
637 megasas_return_cmd(instance, cmd);
638 return 0;
639}
640
641/**
642 * megasas_make_sgl32 - Prepares 32-bit SGL
643 * @instance: Adapter soft state
644 * @scp: SCSI command from the mid-layer
645 * @mfi_sgl: SGL to be filled in
646 *
647 * If successful, this function returns the number of SG elements. Otherwise,
648 * it returnes -1.
649 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800650static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400651megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
652 union megasas_sgl *mfi_sgl)
653{
654 int i;
655 int sge_count;
656 struct scatterlist *os_sgl;
657
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900658 sge_count = scsi_dma_map(scp);
659 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400660
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900661 if (sge_count) {
662 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
663 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
664 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
665 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400666 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400667 return sge_count;
668}
669
670/**
671 * megasas_make_sgl64 - Prepares 64-bit SGL
672 * @instance: Adapter soft state
673 * @scp: SCSI command from the mid-layer
674 * @mfi_sgl: SGL to be filled in
675 *
676 * If successful, this function returns the number of SG elements. Otherwise,
677 * it returnes -1.
678 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800679static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400680megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
681 union megasas_sgl *mfi_sgl)
682{
683 int i;
684 int sge_count;
685 struct scatterlist *os_sgl;
686
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900687 sge_count = scsi_dma_map(scp);
688 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400689
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900690 if (sge_count) {
691 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
692 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
693 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
694 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400695 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400696 return sge_count;
697}
698
Yang, Bof4c9a132009-10-06 14:43:28 -0600699/**
700 * megasas_make_sgl_skinny - Prepares IEEE SGL
701 * @instance: Adapter soft state
702 * @scp: SCSI command from the mid-layer
703 * @mfi_sgl: SGL to be filled in
704 *
705 * If successful, this function returns the number of SG elements. Otherwise,
706 * it returnes -1.
707 */
708static int
709megasas_make_sgl_skinny(struct megasas_instance *instance,
710 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
711{
712 int i;
713 int sge_count;
714 struct scatterlist *os_sgl;
715
716 sge_count = scsi_dma_map(scp);
717
718 if (sge_count) {
719 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
720 mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
721 mfi_sgl->sge_skinny[i].phys_addr =
722 sg_dma_address(os_sgl);
723 }
724 }
725 return sge_count;
726}
727
Sumant Patrob1df99d2006-10-03 12:40:47 -0700728 /**
729 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -0400730 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -0700731 * @sge_count : number of sg elements
732 *
733 * Returns the number of frames required for numnber of sge's (sge_count)
734 */
735
Yang, Bof4c9a132009-10-06 14:43:28 -0600736static u32 megasas_get_frame_count(struct megasas_instance *instance,
737 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -0700738{
739 int num_cnt;
740 int sge_bytes;
741 u32 sge_sz;
742 u32 frame_count=0;
743
744 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
745 sizeof(struct megasas_sge32);
746
Yang, Bof4c9a132009-10-06 14:43:28 -0600747 if (instance->flag_ieee) {
748 sge_sz = sizeof(struct megasas_sge_skinny);
749 }
750
Sumant Patrob1df99d2006-10-03 12:40:47 -0700751 /*
bo yangd532dbe2008-03-17 03:36:43 -0400752 * Main frame can contain 2 SGEs for 64-bit SGLs and
753 * 3 SGEs for 32-bit SGLs for ldio &
754 * 1 SGEs for 64-bit SGLs and
755 * 2 SGEs for 32-bit SGLs for pthru frame
756 */
757 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -0600758 if (instance->flag_ieee == 1) {
759 num_cnt = sge_count - 1;
760 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -0400761 num_cnt = sge_count - 1;
762 else
763 num_cnt = sge_count - 2;
764 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -0600765 if (instance->flag_ieee == 1) {
766 num_cnt = sge_count - 1;
767 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -0400768 num_cnt = sge_count - 2;
769 else
770 num_cnt = sge_count - 3;
771 }
Sumant Patrob1df99d2006-10-03 12:40:47 -0700772
773 if(num_cnt>0){
774 sge_bytes = sge_sz * num_cnt;
775
776 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
777 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
778 }
779 /* Main frame */
780 frame_count +=1;
781
782 if (frame_count > 7)
783 frame_count = 8;
784 return frame_count;
785}
786
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400787/**
788 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
789 * @instance: Adapter soft state
790 * @scp: SCSI command
791 * @cmd: Command to be prepared in
792 *
793 * This function prepares CDB commands. These are typcially pass-through
794 * commands to the devices.
795 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800796static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400797megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
798 struct megasas_cmd *cmd)
799{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400800 u32 is_logical;
801 u32 device_id;
802 u16 flags = 0;
803 struct megasas_pthru_frame *pthru;
804
805 is_logical = MEGASAS_IS_LOGICAL(scp);
806 device_id = MEGASAS_DEV_INDEX(instance, scp);
807 pthru = (struct megasas_pthru_frame *)cmd->frame;
808
809 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
810 flags = MFI_FRAME_DIR_WRITE;
811 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
812 flags = MFI_FRAME_DIR_READ;
813 else if (scp->sc_data_direction == PCI_DMA_NONE)
814 flags = MFI_FRAME_DIR_NONE;
815
Yang, Bof4c9a132009-10-06 14:43:28 -0600816 if (instance->flag_ieee == 1) {
817 flags |= MFI_FRAME_IEEE;
818 }
819
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400820 /*
821 * Prepare the DCDB frame
822 */
823 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
824 pthru->cmd_status = 0x0;
825 pthru->scsi_status = 0x0;
826 pthru->target_id = device_id;
827 pthru->lun = scp->device->lun;
828 pthru->cdb_len = scp->cmd_len;
829 pthru->timeout = 0;
830 pthru->flags = flags;
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900831 pthru->data_xfer_len = scsi_bufflen(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400832
833 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
834
835 /*
Yang, Bo8d568252009-10-06 14:12:21 -0600836 * If the command is for the tape device, set the
837 * pthru timeout to the os layer timeout value.
838 */
839 if (scp->device->type == TYPE_TAPE) {
840 if ((scp->request->timeout / HZ) > 0xFFFF)
841 pthru->timeout = 0xFFFF;
842 else
843 pthru->timeout = scp->request->timeout / HZ;
844 }
845
846 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400847 * Construct SGL
848 */
Yang, Bof4c9a132009-10-06 14:43:28 -0600849 if (instance->flag_ieee == 1) {
850 pthru->flags |= MFI_FRAME_SGL64;
851 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
852 &pthru->sgl);
853 } else if (IS_DMA64) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400854 pthru->flags |= MFI_FRAME_SGL64;
855 pthru->sge_count = megasas_make_sgl64(instance, scp,
856 &pthru->sgl);
857 } else
858 pthru->sge_count = megasas_make_sgl32(instance, scp,
859 &pthru->sgl);
860
861 /*
862 * Sense info specific
863 */
864 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
865 pthru->sense_buf_phys_addr_hi = 0;
866 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
867
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400868 /*
869 * Compute the total number of frames this command consumes. FW uses
870 * this number to pull sufficient number of frames from host memory.
871 */
Yang, Bof4c9a132009-10-06 14:43:28 -0600872 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -0400873 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400874
875 return cmd->frame_count;
876}
877
878/**
879 * megasas_build_ldio - Prepares IOs to logical devices
880 * @instance: Adapter soft state
881 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +0200882 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400883 *
884 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
885 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800886static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400887megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
888 struct megasas_cmd *cmd)
889{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400890 u32 device_id;
891 u8 sc = scp->cmnd[0];
892 u16 flags = 0;
893 struct megasas_io_frame *ldio;
894
895 device_id = MEGASAS_DEV_INDEX(instance, scp);
896 ldio = (struct megasas_io_frame *)cmd->frame;
897
898 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
899 flags = MFI_FRAME_DIR_WRITE;
900 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
901 flags = MFI_FRAME_DIR_READ;
902
Yang, Bof4c9a132009-10-06 14:43:28 -0600903 if (instance->flag_ieee == 1) {
904 flags |= MFI_FRAME_IEEE;
905 }
906
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400907 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -0700908 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400909 */
910 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
911 ldio->cmd_status = 0x0;
912 ldio->scsi_status = 0x0;
913 ldio->target_id = device_id;
914 ldio->timeout = 0;
915 ldio->reserved_0 = 0;
916 ldio->pad_0 = 0;
917 ldio->flags = flags;
918 ldio->start_lba_hi = 0;
919 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
920
921 /*
922 * 6-byte READ(0x08) or WRITE(0x0A) cdb
923 */
924 if (scp->cmd_len == 6) {
925 ldio->lba_count = (u32) scp->cmnd[4];
926 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
927 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
928
929 ldio->start_lba_lo &= 0x1FFFFF;
930 }
931
932 /*
933 * 10-byte READ(0x28) or WRITE(0x2A) cdb
934 */
935 else if (scp->cmd_len == 10) {
936 ldio->lba_count = (u32) scp->cmnd[8] |
937 ((u32) scp->cmnd[7] << 8);
938 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
939 ((u32) scp->cmnd[3] << 16) |
940 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
941 }
942
943 /*
944 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
945 */
946 else if (scp->cmd_len == 12) {
947 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
948 ((u32) scp->cmnd[7] << 16) |
949 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
950
951 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
952 ((u32) scp->cmnd[3] << 16) |
953 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
954 }
955
956 /*
957 * 16-byte READ(0x88) or WRITE(0x8A) cdb
958 */
959 else if (scp->cmd_len == 16) {
960 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
961 ((u32) scp->cmnd[11] << 16) |
962 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
963
964 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
965 ((u32) scp->cmnd[7] << 16) |
966 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
967
968 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
969 ((u32) scp->cmnd[3] << 16) |
970 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
971
972 }
973
974 /*
975 * Construct SGL
976 */
Yang, Bof4c9a132009-10-06 14:43:28 -0600977 if (instance->flag_ieee) {
978 ldio->flags |= MFI_FRAME_SGL64;
979 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
980 &ldio->sgl);
981 } else if (IS_DMA64) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400982 ldio->flags |= MFI_FRAME_SGL64;
983 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
984 } else
985 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
986
987 /*
988 * Sense info specific
989 */
990 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
991 ldio->sense_buf_phys_addr_hi = 0;
992 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
993
Sumant Patrob1df99d2006-10-03 12:40:47 -0700994 /*
995 * Compute the total number of frames this command consumes. FW uses
996 * this number to pull sufficient number of frames from host memory.
997 */
Yang, Bof4c9a132009-10-06 14:43:28 -0600998 cmd->frame_count = megasas_get_frame_count(instance,
999 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001000
1001 return cmd->frame_count;
1002}
1003
1004/**
Sumant Patrocb59aa62006-01-25 11:53:25 -08001005 * megasas_is_ldio - Checks if the cmd is for logical drive
1006 * @scmd: SCSI command
1007 *
1008 * Called by megasas_queue_command to find out if the command to be queued
1009 * is a logical drive command
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001010 */
Sumant Patrocb59aa62006-01-25 11:53:25 -08001011static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001012{
Sumant Patrocb59aa62006-01-25 11:53:25 -08001013 if (!MEGASAS_IS_LOGICAL(cmd))
1014 return 0;
1015 switch (cmd->cmnd[0]) {
1016 case READ_10:
1017 case WRITE_10:
1018 case READ_12:
1019 case WRITE_12:
1020 case READ_6:
1021 case WRITE_6:
1022 case READ_16:
1023 case WRITE_16:
1024 return 1;
1025 default:
1026 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001027 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001028}
1029
Sumant Patro658dced2006-10-03 13:09:14 -07001030 /**
1031 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1032 * in FW
1033 * @instance: Adapter soft state
1034 */
1035static inline void
1036megasas_dump_pending_frames(struct megasas_instance *instance)
1037{
1038 struct megasas_cmd *cmd;
1039 int i,n;
1040 union megasas_sgl *mfi_sgl;
1041 struct megasas_io_frame *ldio;
1042 struct megasas_pthru_frame *pthru;
1043 u32 sgcount;
1044 u32 max_cmd = instance->max_fw_cmds;
1045
1046 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1047 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1048 if (IS_DMA64)
1049 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1050 else
1051 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1052
1053 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1054 for (i = 0; i < max_cmd; i++) {
1055 cmd = instance->cmd_list[i];
1056 if(!cmd->scmd)
1057 continue;
1058 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1059 if (megasas_is_ldio(cmd->scmd)){
1060 ldio = (struct megasas_io_frame *)cmd->frame;
1061 mfi_sgl = &ldio->sgl;
1062 sgcount = ldio->sge_count;
1063 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);
1064 }
1065 else {
1066 pthru = (struct megasas_pthru_frame *) cmd->frame;
1067 mfi_sgl = &pthru->sgl;
1068 sgcount = pthru->sge_count;
1069 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);
1070 }
1071 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1072 for (n = 0; n < sgcount; n++){
1073 if (IS_DMA64)
1074 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) ;
1075 else
1076 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
1077 }
1078 }
1079 printk(KERN_ERR "\n");
1080 } /*for max_cmd*/
1081 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1082 for (i = 0; i < max_cmd; i++) {
1083
1084 cmd = instance->cmd_list[i];
1085
1086 if(cmd->sync_cmd == 1){
1087 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1088 }
1089 }
1090 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1091}
1092
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001093/**
1094 * megasas_queue_command - Queue entry point
1095 * @scmd: SCSI command to be queued
1096 * @done: Callback entry point
1097 */
1098static int
1099megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
1100{
1101 u32 frame_count;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001102 struct megasas_cmd *cmd;
1103 struct megasas_instance *instance;
1104
1105 instance = (struct megasas_instance *)
1106 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001107
1108 /* Don't process if we have already declared adapter dead */
1109 if (instance->hw_crit_error)
1110 return SCSI_MLQUEUE_HOST_BUSY;
1111
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001112 scmd->scsi_done = done;
1113 scmd->result = 0;
1114
Sumant Patrocb59aa62006-01-25 11:53:25 -08001115 if (MEGASAS_IS_LOGICAL(scmd) &&
1116 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1117 scmd->result = DID_BAD_TARGET << 16;
1118 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001119 }
1120
Sumant Patro02b01e02007-02-14 13:00:55 -08001121 switch (scmd->cmnd[0]) {
1122 case SYNCHRONIZE_CACHE:
1123 /*
1124 * FW takes care of flush cache on its own
1125 * No need to send it down
1126 */
1127 scmd->result = DID_OK << 16;
1128 goto out_done;
1129 default:
1130 break;
1131 }
1132
Sumant Patrocb59aa62006-01-25 11:53:25 -08001133 cmd = megasas_get_cmd(instance);
1134 if (!cmd)
1135 return SCSI_MLQUEUE_HOST_BUSY;
1136
1137 /*
1138 * Logical drive command
1139 */
1140 if (megasas_is_ldio(scmd))
1141 frame_count = megasas_build_ldio(instance, scmd, cmd);
1142 else
1143 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1144
1145 if (!frame_count)
1146 goto out_return_cmd;
1147
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001148 cmd->scmd = scmd;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001149 scmd->SCp.ptr = (char *)cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001150
1151 /*
1152 * Issue the command to the FW
1153 */
Sumant Patroe4a082c2006-05-30 12:03:37 -07001154 atomic_inc(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001155
Sumant Patro1341c932006-01-25 12:02:40 -08001156 instance->instancet->fire_cmd(cmd->frame_phys_addr ,cmd->frame_count-1,instance->reg_set);
bo yangad84db22007-11-09 04:40:16 -05001157 /*
1158 * Check if we have pend cmds to be completed
1159 */
1160 if (poll_mode_io && atomic_read(&instance->fw_outstanding))
1161 tasklet_schedule(&instance->isr_tasklet);
1162
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001163
1164 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001165
1166 out_return_cmd:
1167 megasas_return_cmd(instance, cmd);
1168 out_done:
1169 done(scmd);
1170 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001171}
1172
Yang, Bo044833b2009-10-06 14:33:06 -06001173static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1174{
1175 int i;
1176
1177 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1178
1179 if ((megasas_mgmt_info.instance[i]) &&
1180 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1181 return megasas_mgmt_info.instance[i];
1182 }
1183
1184 return NULL;
1185}
1186
Christoph Hellwig147aab62006-02-17 12:13:48 +01001187static int megasas_slave_configure(struct scsi_device *sdev)
1188{
Yang, Bo044833b2009-10-06 14:33:06 -06001189 u16 pd_index = 0;
1190 struct megasas_instance *instance ;
1191
1192 instance = megasas_lookup_instance(sdev->host->host_no);
Christoph Hellwige5b3a652006-03-10 17:08:57 +01001193
1194 /*
Yang, Bo044833b2009-10-06 14:33:06 -06001195 * Don't export physical disk devices to the disk driver.
1196 *
1197 * FIXME: Currently we don't export them to the midlayer at all.
1198 * That will be fixed once LSI engineers have audited the
1199 * firmware for possible issues.
1200 */
1201 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1202 sdev->type == TYPE_DISK) {
1203 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1204 sdev->id;
1205 if (instance->pd_list[pd_index].driveState ==
1206 MR_PD_STATE_SYSTEM) {
1207 blk_queue_rq_timeout(sdev->request_queue,
1208 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1209 return 0;
1210 }
1211 return -ENXIO;
1212 }
1213
1214 /*
1215 * The RAID firmware may require extended timeouts.
1216 */
1217 blk_queue_rq_timeout(sdev->request_queue,
1218 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1219 return 0;
1220}
1221
1222static int megasas_slave_alloc(struct scsi_device *sdev)
1223{
1224 u16 pd_index = 0;
1225 struct megasas_instance *instance ;
1226 instance = megasas_lookup_instance(sdev->host->host_no);
1227 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1228 (sdev->type == TYPE_DISK)) {
1229 /*
1230 * Open the OS scan to the SYSTEM PD
1231 */
1232 pd_index =
1233 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1234 sdev->id;
1235 if ((instance->pd_list[pd_index].driveState ==
1236 MR_PD_STATE_SYSTEM) &&
1237 (instance->pd_list[pd_index].driveType ==
1238 TYPE_DISK)) {
1239 return 0;
1240 }
1241 return -ENXIO;
1242 }
Christoph Hellwig147aab62006-02-17 12:13:48 +01001243 return 0;
1244}
1245
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001246/**
bo yang7343eb62007-11-09 04:35:44 -05001247 * megasas_complete_cmd_dpc - Returns FW's controller structure
1248 * @instance_addr: Address of adapter soft state
1249 *
1250 * Tasklet to complete cmds
1251 */
1252static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1253{
1254 u32 producer;
1255 u32 consumer;
1256 u32 context;
1257 struct megasas_cmd *cmd;
1258 struct megasas_instance *instance =
1259 (struct megasas_instance *)instance_addr;
1260 unsigned long flags;
1261
1262 /* If we have already declared adapter dead, donot complete cmds */
1263 if (instance->hw_crit_error)
1264 return;
1265
1266 spin_lock_irqsave(&instance->completion_lock, flags);
1267
1268 producer = *instance->producer;
1269 consumer = *instance->consumer;
1270
1271 while (consumer != producer) {
1272 context = instance->reply_queue[consumer];
1273
1274 cmd = instance->cmd_list[context];
1275
1276 megasas_complete_cmd(instance, cmd, DID_OK);
1277
1278 consumer++;
1279 if (consumer == (instance->max_fw_cmds + 1)) {
1280 consumer = 0;
1281 }
1282 }
1283
1284 *instance->consumer = producer;
1285
1286 spin_unlock_irqrestore(&instance->completion_lock, flags);
1287
1288 /*
1289 * Check if we can restore can_queue
1290 */
1291 if (instance->flag & MEGASAS_FW_BUSY
1292 && time_after(jiffies, instance->last_time + 5 * HZ)
1293 && atomic_read(&instance->fw_outstanding) < 17) {
1294
1295 spin_lock_irqsave(instance->host->host_lock, flags);
1296 instance->flag &= ~MEGASAS_FW_BUSY;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06001297 if ((instance->pdev->device ==
1298 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1299 (instance->pdev->device ==
1300 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1301 instance->host->can_queue =
1302 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1303 } else
1304 instance->host->can_queue =
bo yang7343eb62007-11-09 04:35:44 -05001305 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1306
1307 spin_unlock_irqrestore(instance->host->host_lock, flags);
1308 }
1309}
1310
1311/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001312 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1313 * @instance: Adapter soft state
1314 *
1315 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
1316 * complete all its outstanding commands. Returns error if one or more IOs
1317 * are pending after this time period. It also marks the controller dead.
1318 */
1319static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1320{
1321 int i;
1322 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
1323
1324 for (i = 0; i < wait_time; i++) {
1325
Sumant Patroe4a082c2006-05-30 12:03:37 -07001326 int outstanding = atomic_read(&instance->fw_outstanding);
1327
1328 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001329 break;
1330
1331 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1332 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07001333 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05001334 /*
1335 * Call cmd completion routine. Cmd to be
1336 * be completed directly without depending on isr.
1337 */
1338 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001339 }
1340
1341 msleep(1000);
1342 }
1343
Sumant Patroe4a082c2006-05-30 12:03:37 -07001344 if (atomic_read(&instance->fw_outstanding)) {
Sumant Patroe3bbff92006-10-03 12:28:49 -07001345 /*
1346 * Send signal to FW to stop processing any pending cmds.
1347 * The controller will be taken offline by the OS now.
1348 */
1349 writel(MFI_STOP_ADP,
1350 &instance->reg_set->inbound_doorbell);
Sumant Patro658dced2006-10-03 13:09:14 -07001351 megasas_dump_pending_frames(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001352 instance->hw_crit_error = 1;
1353 return FAILED;
1354 }
1355
1356 return SUCCESS;
1357}
1358
1359/**
1360 * megasas_generic_reset - Generic reset routine
1361 * @scmd: Mid-layer SCSI command
1362 *
1363 * This routine implements a generic reset handler for device, bus and host
1364 * reset requests. Device, bus and host specific reset handlers can use this
1365 * function after they do their specific tasks.
1366 */
1367static int megasas_generic_reset(struct scsi_cmnd *scmd)
1368{
1369 int ret_val;
1370 struct megasas_instance *instance;
1371
1372 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1373
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001374 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
1375 scmd->serial_number, scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001376
1377 if (instance->hw_crit_error) {
1378 printk(KERN_ERR "megasas: cannot recover from previous reset "
1379 "failures\n");
1380 return FAILED;
1381 }
1382
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001383 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001384 if (ret_val == SUCCESS)
1385 printk(KERN_NOTICE "megasas: reset successful \n");
1386 else
1387 printk(KERN_ERR "megasas: failed to do reset\n");
1388
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001389 return ret_val;
1390}
1391
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001392/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001393 * megasas_reset_timer - quiesce the adapter if required
1394 * @scmd: scsi cmnd
1395 *
1396 * Sets the FW busy flag and reduces the host->can_queue if the
1397 * cmd has not been completed within the timeout period.
1398 */
1399static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07001400blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001401{
1402 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
1403 struct megasas_instance *instance;
1404 unsigned long flags;
1405
1406 if (time_after(jiffies, scmd->jiffies_at_alloc +
1407 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001408 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001409 }
1410
1411 instance = cmd->instance;
1412 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1413 /* FW is busy, throttle IO */
1414 spin_lock_irqsave(instance->host->host_lock, flags);
1415
1416 instance->host->can_queue = 16;
1417 instance->last_time = jiffies;
1418 instance->flag |= MEGASAS_FW_BUSY;
1419
1420 spin_unlock_irqrestore(instance->host->host_lock, flags);
1421 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07001422 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001423}
1424
1425/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001426 * megasas_reset_device - Device reset handler entry point
1427 */
1428static int megasas_reset_device(struct scsi_cmnd *scmd)
1429{
1430 int ret;
1431
1432 /*
1433 * First wait for all commands to complete
1434 */
1435 ret = megasas_generic_reset(scmd);
1436
1437 return ret;
1438}
1439
1440/**
1441 * megasas_reset_bus_host - Bus & host reset handler entry point
1442 */
1443static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1444{
1445 int ret;
1446
1447 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01001448 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001449 */
1450 ret = megasas_generic_reset(scmd);
1451
1452 return ret;
1453}
1454
1455/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001456 * megasas_bios_param - Returns disk geometry for a disk
1457 * @sdev: device handle
1458 * @bdev: block device
1459 * @capacity: drive capacity
1460 * @geom: geometry parameters
1461 */
1462static int
1463megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1464 sector_t capacity, int geom[])
1465{
1466 int heads;
1467 int sectors;
1468 sector_t cylinders;
1469 unsigned long tmp;
1470 /* Default heads (64) & sectors (32) */
1471 heads = 64;
1472 sectors = 32;
1473
1474 tmp = heads * sectors;
1475 cylinders = capacity;
1476
1477 sector_div(cylinders, tmp);
1478
1479 /*
1480 * Handle extended translation size for logical drives > 1Gb
1481 */
1482
1483 if (capacity >= 0x200000) {
1484 heads = 255;
1485 sectors = 63;
1486 tmp = heads*sectors;
1487 cylinders = capacity;
1488 sector_div(cylinders, tmp);
1489 }
1490
1491 geom[0] = heads;
1492 geom[1] = sectors;
1493 geom[2] = cylinders;
1494
1495 return 0;
1496}
1497
1498/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001499 * megasas_service_aen - Processes an event notification
1500 * @instance: Adapter soft state
1501 * @cmd: AEN command completed by the ISR
1502 *
1503 * For AEN, driver sends a command down to FW that is held by the FW till an
1504 * event occurs. When an event of interest occurs, FW completes the command
1505 * that it was previously holding.
1506 *
1507 * This routines sends SIGIO signal to processes that have registered with the
1508 * driver for AEN.
1509 */
1510static void
1511megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1512{
Yang, Boc3518832009-10-06 14:18:02 -06001513 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001514 /*
1515 * Don't signal app if it is just an aborted previously registered aen
1516 */
Yang, Boc3518832009-10-06 14:18:02 -06001517 if ((!cmd->abort_aen) && (instance->unload == 0)) {
1518 spin_lock_irqsave(&poll_aen_lock, flags);
1519 megasas_poll_wait_aen = 1;
1520 spin_unlock_irqrestore(&poll_aen_lock, flags);
1521 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001522 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06001523 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001524 else
1525 cmd->abort_aen = 0;
1526
1527 instance->aen_cmd = NULL;
1528 megasas_return_cmd(instance, cmd);
1529}
1530
1531/*
1532 * Scsi host template for megaraid_sas driver
1533 */
1534static struct scsi_host_template megasas_template = {
1535
1536 .module = THIS_MODULE,
bo yangf28cd7c2007-11-09 04:44:56 -05001537 .name = "LSI SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001538 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01001539 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06001540 .slave_alloc = megasas_slave_alloc,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001541 .queuecommand = megasas_queue_command,
1542 .eh_device_reset_handler = megasas_reset_device,
1543 .eh_bus_reset_handler = megasas_reset_bus_host,
1544 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001545 .eh_timed_out = megasas_reset_timer,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001546 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001547 .use_clustering = ENABLE_CLUSTERING,
1548};
1549
1550/**
1551 * megasas_complete_int_cmd - Completes an internal command
1552 * @instance: Adapter soft state
1553 * @cmd: Command to be completed
1554 *
1555 * The megasas_issue_blocked_cmd() function waits for a command to complete
1556 * after it issues a command. This function wakes up that waiting routine by
1557 * calling wake_up() on the wait queue.
1558 */
1559static void
1560megasas_complete_int_cmd(struct megasas_instance *instance,
1561 struct megasas_cmd *cmd)
1562{
1563 cmd->cmd_status = cmd->frame->io.cmd_status;
1564
1565 if (cmd->cmd_status == ENODATA) {
1566 cmd->cmd_status = 0;
1567 }
1568 wake_up(&instance->int_cmd_wait_q);
1569}
1570
1571/**
1572 * megasas_complete_abort - Completes aborting a command
1573 * @instance: Adapter soft state
1574 * @cmd: Cmd that was issued to abort another cmd
1575 *
1576 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
1577 * after it issues an abort on a previously issued command. This function
1578 * wakes up all functions waiting on the same wait queue.
1579 */
1580static void
1581megasas_complete_abort(struct megasas_instance *instance,
1582 struct megasas_cmd *cmd)
1583{
1584 if (cmd->sync_cmd) {
1585 cmd->sync_cmd = 0;
1586 cmd->cmd_status = 0;
1587 wake_up(&instance->abort_cmd_wait_q);
1588 }
1589
1590 return;
1591}
1592
1593/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001594 * megasas_complete_cmd - Completes a command
1595 * @instance: Adapter soft state
1596 * @cmd: Command to be completed
1597 * @alt_status: If non-zero, use this value as status to
1598 * SCSI mid-layer instead of the value returned
1599 * by the FW. This should be used if caller wants
1600 * an alternate status (as in the case of aborted
1601 * commands)
1602 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001603static void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001604megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
1605 u8 alt_status)
1606{
1607 int exception = 0;
1608 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06001609 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001610
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001611 if (cmd->scmd)
1612 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001613
1614 switch (hdr->cmd) {
1615
1616 case MFI_CMD_PD_SCSI_IO:
1617 case MFI_CMD_LD_SCSI_IO:
1618
1619 /*
1620 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1621 * issued either through an IO path or an IOCTL path. If it
1622 * was via IOCTL, we will send it to internal completion.
1623 */
1624 if (cmd->sync_cmd) {
1625 cmd->sync_cmd = 0;
1626 megasas_complete_int_cmd(instance, cmd);
1627 break;
1628 }
1629
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001630 case MFI_CMD_LD_READ:
1631 case MFI_CMD_LD_WRITE:
1632
1633 if (alt_status) {
1634 cmd->scmd->result = alt_status << 16;
1635 exception = 1;
1636 }
1637
1638 if (exception) {
1639
Sumant Patroe4a082c2006-05-30 12:03:37 -07001640 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001641
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001642 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001643 cmd->scmd->scsi_done(cmd->scmd);
1644 megasas_return_cmd(instance, cmd);
1645
1646 break;
1647 }
1648
1649 switch (hdr->cmd_status) {
1650
1651 case MFI_STAT_OK:
1652 cmd->scmd->result = DID_OK << 16;
1653 break;
1654
1655 case MFI_STAT_SCSI_IO_FAILED:
1656 case MFI_STAT_LD_INIT_IN_PROGRESS:
1657 cmd->scmd->result =
1658 (DID_ERROR << 16) | hdr->scsi_status;
1659 break;
1660
1661 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1662
1663 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1664
1665 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1666 memset(cmd->scmd->sense_buffer, 0,
1667 SCSI_SENSE_BUFFERSIZE);
1668 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1669 hdr->sense_len);
1670
1671 cmd->scmd->result |= DRIVER_SENSE << 24;
1672 }
1673
1674 break;
1675
1676 case MFI_STAT_LD_OFFLINE:
1677 case MFI_STAT_DEVICE_NOT_FOUND:
1678 cmd->scmd->result = DID_BAD_TARGET << 16;
1679 break;
1680
1681 default:
1682 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1683 hdr->cmd_status);
1684 cmd->scmd->result = DID_ERROR << 16;
1685 break;
1686 }
1687
Sumant Patroe4a082c2006-05-30 12:03:37 -07001688 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001689
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001690 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001691 cmd->scmd->scsi_done(cmd->scmd);
1692 megasas_return_cmd(instance, cmd);
1693
1694 break;
1695
1696 case MFI_CMD_SMP:
1697 case MFI_CMD_STP:
1698 case MFI_CMD_DCMD:
Yang, Boc3518832009-10-06 14:18:02 -06001699 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
1700 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
1701 spin_lock_irqsave(&poll_aen_lock, flags);
1702 megasas_poll_wait_aen = 0;
1703 spin_unlock_irqrestore(&poll_aen_lock, flags);
1704 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001705
1706 /*
1707 * See if got an event notification
1708 */
1709 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1710 megasas_service_aen(instance, cmd);
1711 else
1712 megasas_complete_int_cmd(instance, cmd);
1713
1714 break;
1715
1716 case MFI_CMD_ABORT:
1717 /*
1718 * Cmd issued to abort another cmd returned
1719 */
1720 megasas_complete_abort(instance, cmd);
1721 break;
1722
1723 default:
1724 printk("megasas: Unknown command completed! [0x%X]\n",
1725 hdr->cmd);
1726 break;
1727 }
1728}
1729
1730/**
1731 * megasas_deplete_reply_queue - Processes all completed commands
1732 * @instance: Adapter soft state
1733 * @alt_status: Alternate status to be returned to
1734 * SCSI mid-layer instead of the status
1735 * returned by the FW
1736 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001737static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001738megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1739{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001740 /*
1741 * Check if it is our interrupt
Sumant Patro1341c932006-01-25 12:02:40 -08001742 * Clear the interrupt
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001743 */
Sumant Patro1341c932006-01-25 12:02:40 -08001744 if(instance->instancet->clear_intr(instance->reg_set))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001745 return IRQ_NONE;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001746
Sumant Patroaf37acf2007-02-14 12:34:46 -08001747 if (instance->hw_crit_error)
1748 goto out_done;
Sumant Patro5d018ad2006-10-03 13:13:18 -07001749 /*
1750 * Schedule the tasklet for cmd completion
1751 */
1752 tasklet_schedule(&instance->isr_tasklet);
Sumant Patroaf37acf2007-02-14 12:34:46 -08001753out_done:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001754 return IRQ_HANDLED;
1755}
1756
1757/**
1758 * megasas_isr - isr entry point
1759 */
David Howells7d12e782006-10-05 14:55:46 +01001760static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001761{
1762 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1763 DID_OK);
1764}
1765
1766/**
1767 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08001768 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001769 *
1770 * During the initialization, FW passes can potentially be in any one of
1771 * several possible states. If the FW in operational, waiting-for-handshake
1772 * states, driver must take steps to bring it to ready state. Otherwise, it
1773 * has to wait for the ready state.
1774 */
1775static int
Sumant Patro1341c932006-01-25 12:02:40 -08001776megasas_transition_to_ready(struct megasas_instance* instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001777{
1778 int i;
1779 u8 max_wait;
1780 u32 fw_state;
1781 u32 cur_state;
1782
Sumant Patro1341c932006-01-25 12:02:40 -08001783 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001784
Sumant Patroe3bbff92006-10-03 12:28:49 -07001785 if (fw_state != MFI_STATE_READY)
1786 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1787 " state\n");
1788
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001789 while (fw_state != MFI_STATE_READY) {
1790
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001791 switch (fw_state) {
1792
1793 case MFI_STATE_FAULT:
1794
1795 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1796 return -ENODEV;
1797
1798 case MFI_STATE_WAIT_HANDSHAKE:
1799 /*
1800 * Set the CLR bit in inbound doorbell
1801 */
Yang, Bo87911122009-10-06 14:31:54 -06001802 if ((instance->pdev->device == \
1803 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1804 (instance->pdev->device ==
1805 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1806
1807 writel(
1808 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
1809 &instance->reg_set->reserved_0[0]);
1810 } else {
1811 writel(
1812 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
1813 &instance->reg_set->inbound_doorbell);
1814 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001815
1816 max_wait = 2;
1817 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1818 break;
1819
Sumant Patroe3bbff92006-10-03 12:28:49 -07001820 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06001821 if ((instance->pdev->device ==
1822 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1823 (instance->pdev->device ==
1824 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1825 writel(MFI_INIT_HOTPLUG,
1826 &instance->reg_set->reserved_0[0]);
1827 } else
1828 writel(MFI_INIT_HOTPLUG,
1829 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07001830
1831 max_wait = 10;
1832 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
1833 break;
1834
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001835 case MFI_STATE_OPERATIONAL:
1836 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07001837 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001838 */
Sumant Patrob274cab2006-10-03 12:52:12 -07001839 instance->instancet->disable_intr(instance->reg_set);
Yang, Bo87911122009-10-06 14:31:54 -06001840 if ((instance->pdev->device ==
1841 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1842 (instance->pdev->device ==
1843 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1844 writel(MFI_RESET_FLAGS,
1845 &instance->reg_set->reserved_0[0]);
1846 } else
1847 writel(MFI_RESET_FLAGS,
1848 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001849
bo yangaf7a5642008-03-17 04:13:07 -04001850 max_wait = 60;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001851 cur_state = MFI_STATE_OPERATIONAL;
1852 break;
1853
1854 case MFI_STATE_UNDEFINED:
1855 /*
1856 * This state should not last for more than 2 seconds
1857 */
1858 max_wait = 2;
1859 cur_state = MFI_STATE_UNDEFINED;
1860 break;
1861
1862 case MFI_STATE_BB_INIT:
1863 max_wait = 2;
1864 cur_state = MFI_STATE_BB_INIT;
1865 break;
1866
1867 case MFI_STATE_FW_INIT:
1868 max_wait = 20;
1869 cur_state = MFI_STATE_FW_INIT;
1870 break;
1871
1872 case MFI_STATE_FW_INIT_2:
1873 max_wait = 20;
1874 cur_state = MFI_STATE_FW_INIT_2;
1875 break;
1876
1877 case MFI_STATE_DEVICE_SCAN:
1878 max_wait = 20;
1879 cur_state = MFI_STATE_DEVICE_SCAN;
1880 break;
1881
1882 case MFI_STATE_FLUSH_CACHE:
1883 max_wait = 20;
1884 cur_state = MFI_STATE_FLUSH_CACHE;
1885 break;
1886
1887 default:
1888 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1889 fw_state);
1890 return -ENODEV;
1891 }
1892
1893 /*
1894 * The cur_state should not last for more than max_wait secs
1895 */
1896 for (i = 0; i < (max_wait * 1000); i++) {
Sumant Patro1341c932006-01-25 12:02:40 -08001897 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1898 MFI_STATE_MASK ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001899
1900 if (fw_state == cur_state) {
1901 msleep(1);
1902 } else
1903 break;
1904 }
1905
1906 /*
1907 * Return error if fw_state hasn't changed after max_wait
1908 */
1909 if (fw_state == cur_state) {
1910 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1911 "in %d secs\n", fw_state, max_wait);
1912 return -ENODEV;
1913 }
1914 };
Sumant Patroe3bbff92006-10-03 12:28:49 -07001915 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001916
1917 return 0;
1918}
1919
1920/**
1921 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1922 * @instance: Adapter soft state
1923 */
1924static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1925{
1926 int i;
1927 u32 max_cmd = instance->max_fw_cmds;
1928 struct megasas_cmd *cmd;
1929
1930 if (!instance->frame_dma_pool)
1931 return;
1932
1933 /*
1934 * Return all frames to pool
1935 */
1936 for (i = 0; i < max_cmd; i++) {
1937
1938 cmd = instance->cmd_list[i];
1939
1940 if (cmd->frame)
1941 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1942 cmd->frame_phys_addr);
1943
1944 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07001945 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001946 cmd->sense_phys_addr);
1947 }
1948
1949 /*
1950 * Now destroy the pool itself
1951 */
1952 pci_pool_destroy(instance->frame_dma_pool);
1953 pci_pool_destroy(instance->sense_dma_pool);
1954
1955 instance->frame_dma_pool = NULL;
1956 instance->sense_dma_pool = NULL;
1957}
1958
1959/**
1960 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1961 * @instance: Adapter soft state
1962 *
1963 * Each command packet has an embedded DMA memory buffer that is used for
1964 * filling MFI frame and the SG list that immediately follows the frame. This
1965 * function creates those DMA memory buffers for each command packet by using
1966 * PCI pool facility.
1967 */
1968static int megasas_create_frame_pool(struct megasas_instance *instance)
1969{
1970 int i;
1971 u32 max_cmd;
1972 u32 sge_sz;
1973 u32 sgl_sz;
1974 u32 total_sz;
1975 u32 frame_count;
1976 struct megasas_cmd *cmd;
1977
1978 max_cmd = instance->max_fw_cmds;
1979
1980 /*
1981 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1982 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1983 */
1984 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1985 sizeof(struct megasas_sge32);
1986
Yang, Bof4c9a132009-10-06 14:43:28 -06001987 if (instance->flag_ieee) {
1988 sge_sz = sizeof(struct megasas_sge_skinny);
1989 }
1990
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001991 /*
1992 * Calculated the number of 64byte frames required for SGL
1993 */
1994 sgl_sz = sge_sz * instance->max_num_sge;
1995 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1996
1997 /*
1998 * We need one extra frame for the MFI command
1999 */
2000 frame_count++;
2001
2002 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2003 /*
2004 * Use DMA pool facility provided by PCI layer
2005 */
2006 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2007 instance->pdev, total_sz, 64,
2008 0);
2009
2010 if (!instance->frame_dma_pool) {
2011 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2012 return -ENOMEM;
2013 }
2014
2015 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2016 instance->pdev, 128, 4, 0);
2017
2018 if (!instance->sense_dma_pool) {
2019 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2020
2021 pci_pool_destroy(instance->frame_dma_pool);
2022 instance->frame_dma_pool = NULL;
2023
2024 return -ENOMEM;
2025 }
2026
2027 /*
2028 * Allocate and attach a frame to each of the commands in cmd_list.
2029 * By making cmd->index as the context instead of the &cmd, we can
2030 * always use 32bit context regardless of the architecture
2031 */
2032 for (i = 0; i < max_cmd; i++) {
2033
2034 cmd = instance->cmd_list[i];
2035
2036 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2037 GFP_KERNEL, &cmd->frame_phys_addr);
2038
2039 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2040 GFP_KERNEL, &cmd->sense_phys_addr);
2041
2042 /*
2043 * megasas_teardown_frame_pool() takes care of freeing
2044 * whatever has been allocated
2045 */
2046 if (!cmd->frame || !cmd->sense) {
2047 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2048 megasas_teardown_frame_pool(instance);
2049 return -ENOMEM;
2050 }
2051
2052 cmd->frame->io.context = cmd->index;
2053 }
2054
2055 return 0;
2056}
2057
2058/**
2059 * megasas_free_cmds - Free all the cmds in the free cmd pool
2060 * @instance: Adapter soft state
2061 */
2062static void megasas_free_cmds(struct megasas_instance *instance)
2063{
2064 int i;
2065 /* First free the MFI frame pool */
2066 megasas_teardown_frame_pool(instance);
2067
2068 /* Free all the commands in the cmd_list */
2069 for (i = 0; i < instance->max_fw_cmds; i++)
2070 kfree(instance->cmd_list[i]);
2071
2072 /* Free the cmd_list buffer itself */
2073 kfree(instance->cmd_list);
2074 instance->cmd_list = NULL;
2075
2076 INIT_LIST_HEAD(&instance->cmd_pool);
2077}
2078
2079/**
2080 * megasas_alloc_cmds - Allocates the command packets
2081 * @instance: Adapter soft state
2082 *
2083 * Each command that is issued to the FW, whether IO commands from the OS or
2084 * internal commands like IOCTLs, are wrapped in local data structure called
2085 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2086 * the FW.
2087 *
2088 * Each frame has a 32-bit field called context (tag). This context is used
2089 * to get back the megasas_cmd from the frame when a frame gets completed in
2090 * the ISR. Typically the address of the megasas_cmd itself would be used as
2091 * the context. But we wanted to keep the differences between 32 and 64 bit
2092 * systems to the mininum. We always use 32 bit integers for the context. In
2093 * this driver, the 32 bit values are the indices into an array cmd_list.
2094 * This array is used only to look up the megasas_cmd given the context. The
2095 * free commands themselves are maintained in a linked list called cmd_pool.
2096 */
2097static int megasas_alloc_cmds(struct megasas_instance *instance)
2098{
2099 int i;
2100 int j;
2101 u32 max_cmd;
2102 struct megasas_cmd *cmd;
2103
2104 max_cmd = instance->max_fw_cmds;
2105
2106 /*
2107 * instance->cmd_list is an array of struct megasas_cmd pointers.
2108 * Allocate the dynamic array first and then allocate individual
2109 * commands.
2110 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07002111 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002112
2113 if (!instance->cmd_list) {
2114 printk(KERN_DEBUG "megasas: out of memory\n");
2115 return -ENOMEM;
2116 }
2117
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002118
2119 for (i = 0; i < max_cmd; i++) {
2120 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
2121 GFP_KERNEL);
2122
2123 if (!instance->cmd_list[i]) {
2124
2125 for (j = 0; j < i; j++)
2126 kfree(instance->cmd_list[j]);
2127
2128 kfree(instance->cmd_list);
2129 instance->cmd_list = NULL;
2130
2131 return -ENOMEM;
2132 }
2133 }
2134
2135 /*
2136 * Add all the commands to command pool (instance->cmd_pool)
2137 */
2138 for (i = 0; i < max_cmd; i++) {
2139 cmd = instance->cmd_list[i];
2140 memset(cmd, 0, sizeof(struct megasas_cmd));
2141 cmd->index = i;
2142 cmd->instance = instance;
2143
2144 list_add_tail(&cmd->list, &instance->cmd_pool);
2145 }
2146
2147 /*
2148 * Create a frame pool and assign one frame to each cmd
2149 */
2150 if (megasas_create_frame_pool(instance)) {
2151 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
2152 megasas_free_cmds(instance);
2153 }
2154
2155 return 0;
2156}
2157
Yang, Bo81e403c2009-10-06 14:27:54 -06002158/*
2159 * megasas_get_pd_list_info - Returns FW's pd_list structure
2160 * @instance: Adapter soft state
2161 * @pd_list: pd_list structure
2162 *
2163 * Issues an internal command (DCMD) to get the FW's controller PD
2164 * list structure. This information is mainly used to find out SYSTEM
2165 * supported by the FW.
2166 */
2167static int
2168megasas_get_pd_list(struct megasas_instance *instance)
2169{
2170 int ret = 0, pd_index = 0;
2171 struct megasas_cmd *cmd;
2172 struct megasas_dcmd_frame *dcmd;
2173 struct MR_PD_LIST *ci;
2174 struct MR_PD_ADDRESS *pd_addr;
2175 dma_addr_t ci_h = 0;
2176
2177 cmd = megasas_get_cmd(instance);
2178
2179 if (!cmd) {
2180 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
2181 return -ENOMEM;
2182 }
2183
2184 dcmd = &cmd->frame->dcmd;
2185
2186 ci = pci_alloc_consistent(instance->pdev,
2187 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
2188
2189 if (!ci) {
2190 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
2191 megasas_return_cmd(instance, cmd);
2192 return -ENOMEM;
2193 }
2194
2195 memset(ci, 0, sizeof(*ci));
2196 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2197
2198 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
2199 dcmd->mbox.b[1] = 0;
2200 dcmd->cmd = MFI_CMD_DCMD;
2201 dcmd->cmd_status = 0xFF;
2202 dcmd->sge_count = 1;
2203 dcmd->flags = MFI_FRAME_DIR_READ;
2204 dcmd->timeout = 0;
2205 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
2206 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
2207 dcmd->sgl.sge32[0].phys_addr = ci_h;
2208 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
2209
2210 if (!megasas_issue_polled(instance, cmd)) {
2211 ret = 0;
2212 } else {
2213 ret = -1;
2214 }
2215
2216 /*
2217 * the following function will get the instance PD LIST.
2218 */
2219
2220 pd_addr = ci->addr;
2221
2222 if ( ret == 0 &&
2223 (ci->count <
2224 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
2225
2226 memset(instance->pd_list, 0,
2227 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
2228
2229 for (pd_index = 0; pd_index < ci->count; pd_index++) {
2230
2231 instance->pd_list[pd_addr->deviceId].tid =
2232 pd_addr->deviceId;
2233 instance->pd_list[pd_addr->deviceId].driveType =
2234 pd_addr->scsiDevType;
2235 instance->pd_list[pd_addr->deviceId].driveState =
2236 MR_PD_STATE_SYSTEM;
2237 pd_addr++;
2238 }
2239 }
2240
2241 pci_free_consistent(instance->pdev,
2242 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
2243 ci, ci_h);
2244 megasas_return_cmd(instance, cmd);
2245
2246 return ret;
2247}
2248
2249
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002250/**
2251 * megasas_get_controller_info - Returns FW's controller structure
2252 * @instance: Adapter soft state
2253 * @ctrl_info: Controller information structure
2254 *
2255 * Issues an internal command (DCMD) to get the FW's controller structure.
2256 * This information is mainly used to find out the maximum IO transfer per
2257 * command supported by the FW.
2258 */
2259static int
2260megasas_get_ctrl_info(struct megasas_instance *instance,
2261 struct megasas_ctrl_info *ctrl_info)
2262{
2263 int ret = 0;
2264 struct megasas_cmd *cmd;
2265 struct megasas_dcmd_frame *dcmd;
2266 struct megasas_ctrl_info *ci;
2267 dma_addr_t ci_h = 0;
2268
2269 cmd = megasas_get_cmd(instance);
2270
2271 if (!cmd) {
2272 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
2273 return -ENOMEM;
2274 }
2275
2276 dcmd = &cmd->frame->dcmd;
2277
2278 ci = pci_alloc_consistent(instance->pdev,
2279 sizeof(struct megasas_ctrl_info), &ci_h);
2280
2281 if (!ci) {
2282 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
2283 megasas_return_cmd(instance, cmd);
2284 return -ENOMEM;
2285 }
2286
2287 memset(ci, 0, sizeof(*ci));
2288 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2289
2290 dcmd->cmd = MFI_CMD_DCMD;
2291 dcmd->cmd_status = 0xFF;
2292 dcmd->sge_count = 1;
2293 dcmd->flags = MFI_FRAME_DIR_READ;
2294 dcmd->timeout = 0;
2295 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
2296 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
2297 dcmd->sgl.sge32[0].phys_addr = ci_h;
2298 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
2299
2300 if (!megasas_issue_polled(instance, cmd)) {
2301 ret = 0;
2302 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
2303 } else {
2304 ret = -1;
2305 }
2306
2307 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
2308 ci, ci_h);
2309
2310 megasas_return_cmd(instance, cmd);
2311 return ret;
2312}
2313
2314/**
bo yang31ea7082007-11-07 12:09:50 -05002315 * megasas_issue_init_mfi - Initializes the FW
2316 * @instance: Adapter soft state
2317 *
2318 * Issues the INIT MFI cmd
2319 */
2320static int
2321megasas_issue_init_mfi(struct megasas_instance *instance)
2322{
2323 u32 context;
2324
2325 struct megasas_cmd *cmd;
2326
2327 struct megasas_init_frame *init_frame;
2328 struct megasas_init_queue_info *initq_info;
2329 dma_addr_t init_frame_h;
2330 dma_addr_t initq_info_h;
2331
2332 /*
2333 * Prepare a init frame. Note the init frame points to queue info
2334 * structure. Each frame has SGL allocated after first 64 bytes. For
2335 * this frame - since we don't need any SGL - we use SGL's space as
2336 * queue info structure
2337 *
2338 * We will not get a NULL command below. We just created the pool.
2339 */
2340 cmd = megasas_get_cmd(instance);
2341
2342 init_frame = (struct megasas_init_frame *)cmd->frame;
2343 initq_info = (struct megasas_init_queue_info *)
2344 ((unsigned long)init_frame + 64);
2345
2346 init_frame_h = cmd->frame_phys_addr;
2347 initq_info_h = init_frame_h + 64;
2348
2349 context = init_frame->context;
2350 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
2351 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
2352 init_frame->context = context;
2353
2354 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
2355 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
2356
2357 initq_info->producer_index_phys_addr_lo = instance->producer_h;
2358 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
2359
2360 init_frame->cmd = MFI_CMD_INIT;
2361 init_frame->cmd_status = 0xFF;
2362 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
2363
2364 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
2365
2366 /*
2367 * disable the intr before firing the init frame to FW
2368 */
2369 instance->instancet->disable_intr(instance->reg_set);
2370
2371 /*
2372 * Issue the init frame in polled mode
2373 */
2374
2375 if (megasas_issue_polled(instance, cmd)) {
2376 printk(KERN_ERR "megasas: Failed to init firmware\n");
2377 megasas_return_cmd(instance, cmd);
2378 goto fail_fw_init;
2379 }
2380
2381 megasas_return_cmd(instance, cmd);
2382
2383 return 0;
2384
2385fail_fw_init:
2386 return -EINVAL;
2387}
2388
2389/**
bo yangad84db22007-11-09 04:40:16 -05002390 * megasas_start_timer - Initializes a timer object
2391 * @instance: Adapter soft state
2392 * @timer: timer object to be initialized
2393 * @fn: timer function
2394 * @interval: time interval between timer function call
2395 */
2396static inline void
2397megasas_start_timer(struct megasas_instance *instance,
2398 struct timer_list *timer,
2399 void *fn, unsigned long interval)
2400{
2401 init_timer(timer);
2402 timer->expires = jiffies + interval;
2403 timer->data = (unsigned long)instance;
2404 timer->function = fn;
2405 add_timer(timer);
2406}
2407
2408/**
2409 * megasas_io_completion_timer - Timer fn
2410 * @instance_addr: Address of adapter soft state
2411 *
2412 * Schedules tasklet for cmd completion
2413 * if poll_mode_io is set
2414 */
2415static void
2416megasas_io_completion_timer(unsigned long instance_addr)
2417{
2418 struct megasas_instance *instance =
2419 (struct megasas_instance *)instance_addr;
2420
2421 if (atomic_read(&instance->fw_outstanding))
2422 tasklet_schedule(&instance->isr_tasklet);
2423
2424 /* Restart timer */
2425 if (poll_mode_io)
2426 mod_timer(&instance->io_completion_timer,
2427 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
2428}
2429
2430/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002431 * megasas_init_mfi - Initializes the FW
2432 * @instance: Adapter soft state
2433 *
2434 * This is the main function for initializing MFI firmware.
2435 */
2436static int megasas_init_mfi(struct megasas_instance *instance)
2437{
2438 u32 context_sz;
2439 u32 reply_q_sz;
2440 u32 max_sectors_1;
2441 u32 max_sectors_2;
bo yang14faea92007-11-09 04:14:00 -05002442 u32 tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002443 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002444 struct megasas_ctrl_info *ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002445 /*
2446 * Map the message registers
2447 */
Yang, Bo6610a6b2008-08-10 12:42:38 -07002448 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
Yang, Bo87911122009-10-06 14:31:54 -06002449 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2450 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Yang, Bo6610a6b2008-08-10 12:42:38 -07002451 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0079GEN2)) {
2452 instance->base_addr = pci_resource_start(instance->pdev, 1);
2453 } else {
2454 instance->base_addr = pci_resource_start(instance->pdev, 0);
2455 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002456
bo yangf28cd7c2007-11-09 04:44:56 -05002457 if (pci_request_regions(instance->pdev, "megasas: LSI")) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002458 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
2459 return -EBUSY;
2460 }
2461
2462 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
2463
2464 if (!instance->reg_set) {
2465 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
2466 goto fail_ioremap;
2467 }
2468
2469 reg_set = instance->reg_set;
2470
Sumant Patrof9876f02006-02-03 15:34:35 -08002471 switch(instance->pdev->device)
2472 {
bo yangaf7a5642008-03-17 04:13:07 -04002473 case PCI_DEVICE_ID_LSI_SAS1078R:
2474 case PCI_DEVICE_ID_LSI_SAS1078DE:
Sumant Patrof9876f02006-02-03 15:34:35 -08002475 instance->instancet = &megasas_instance_template_ppc;
2476 break;
Yang, Bo6610a6b2008-08-10 12:42:38 -07002477 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
2478 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
2479 instance->instancet = &megasas_instance_template_gen2;
2480 break;
Yang, Bo87911122009-10-06 14:31:54 -06002481 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
2482 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
2483 instance->instancet = &megasas_instance_template_skinny;
2484 break;
Sumant Patrof9876f02006-02-03 15:34:35 -08002485 case PCI_DEVICE_ID_LSI_SAS1064R:
2486 case PCI_DEVICE_ID_DELL_PERC5:
2487 default:
2488 instance->instancet = &megasas_instance_template_xscale;
2489 break;
2490 }
Sumant Patro1341c932006-01-25 12:02:40 -08002491
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002492 /*
2493 * We expect the FW state to be READY
2494 */
Sumant Patro1341c932006-01-25 12:02:40 -08002495 if (megasas_transition_to_ready(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002496 goto fail_ready_state;
2497
2498 /*
2499 * Get various operational parameters from status register
2500 */
Sumant Patro1341c932006-01-25 12:02:40 -08002501 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07002502 /*
2503 * Reduce the max supported cmds by 1. This is to ensure that the
2504 * reply_q_sz (1 more than the max cmd that driver may send)
2505 * does not exceed max cmds that the FW can support
2506 */
2507 instance->max_fw_cmds = instance->max_fw_cmds-1;
Sumant Patro1341c932006-01-25 12:02:40 -08002508 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
2509 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002510 /*
2511 * Create a pool of commands
2512 */
2513 if (megasas_alloc_cmds(instance))
2514 goto fail_alloc_cmds;
2515
2516 /*
2517 * Allocate memory for reply queue. Length of reply queue should
2518 * be _one_ more than the maximum commands handled by the firmware.
2519 *
2520 * Note: When FW completes commands, it places corresponding contex
2521 * values in this circular reply queue. This circular queue is a fairly
2522 * typical producer-consumer queue. FW is the producer (of completed
2523 * commands) and the driver is the consumer.
2524 */
2525 context_sz = sizeof(u32);
2526 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
2527
2528 instance->reply_queue = pci_alloc_consistent(instance->pdev,
2529 reply_q_sz,
2530 &instance->reply_queue_h);
2531
2532 if (!instance->reply_queue) {
2533 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
2534 goto fail_reply_queue;
2535 }
2536
bo yang31ea7082007-11-07 12:09:50 -05002537 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002538 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002539
Yang, Bo81e403c2009-10-06 14:27:54 -06002540 memset(instance->pd_list, 0 ,
2541 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
2542 megasas_get_pd_list(instance);
2543
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002544 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
2545
2546 /*
2547 * Compute the max allowed sectors per IO: The controller info has two
2548 * limits on max sectors. Driver should use the minimum of these two.
2549 *
2550 * 1 << stripe_sz_ops.min = max sectors per strip
2551 *
2552 * Note that older firmwares ( < FW ver 30) didn't report information
2553 * to calculate max_sectors_1. So the number ended up as zero always.
2554 */
bo yang14faea92007-11-09 04:14:00 -05002555 tmp_sectors = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002556 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
2557
2558 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
2559 ctrl_info->max_strips_per_io;
2560 max_sectors_2 = ctrl_info->max_request_size;
2561
bo yang14faea92007-11-09 04:14:00 -05002562 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
2563 }
2564
2565 instance->max_sectors_per_req = instance->max_num_sge *
2566 PAGE_SIZE / 512;
2567 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
2568 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002569
2570 kfree(ctrl_info);
2571
Sumant Patro5d018ad2006-10-03 13:13:18 -07002572 /*
2573 * Setup tasklet for cmd completion
2574 */
2575
bo yangad84db22007-11-09 04:40:16 -05002576 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2577 (unsigned long)instance);
2578
2579 /* Initialize the cmd completion timer */
2580 if (poll_mode_io)
2581 megasas_start_timer(instance, &instance->io_completion_timer,
2582 megasas_io_completion_timer,
2583 MEGASAS_COMPLETION_TIMER_INTERVAL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002584 return 0;
2585
2586 fail_fw_init:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002587
2588 pci_free_consistent(instance->pdev, reply_q_sz,
2589 instance->reply_queue, instance->reply_queue_h);
2590 fail_reply_queue:
2591 megasas_free_cmds(instance);
2592
2593 fail_alloc_cmds:
2594 fail_ready_state:
2595 iounmap(instance->reg_set);
2596
2597 fail_ioremap:
2598 pci_release_regions(instance->pdev);
2599
2600 return -EINVAL;
2601}
2602
2603/**
2604 * megasas_release_mfi - Reverses the FW initialization
2605 * @intance: Adapter soft state
2606 */
2607static void megasas_release_mfi(struct megasas_instance *instance)
2608{
2609 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
2610
2611 pci_free_consistent(instance->pdev, reply_q_sz,
2612 instance->reply_queue, instance->reply_queue_h);
2613
2614 megasas_free_cmds(instance);
2615
2616 iounmap(instance->reg_set);
2617
2618 pci_release_regions(instance->pdev);
2619}
2620
2621/**
2622 * megasas_get_seq_num - Gets latest event sequence numbers
2623 * @instance: Adapter soft state
2624 * @eli: FW event log sequence numbers information
2625 *
2626 * FW maintains a log of all events in a non-volatile area. Upper layers would
2627 * usually find out the latest sequence number of the events, the seq number at
2628 * the boot etc. They would "read" all the events below the latest seq number
2629 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
2630 * number), they would subsribe to AEN (asynchronous event notification) and
2631 * wait for the events to happen.
2632 */
2633static int
2634megasas_get_seq_num(struct megasas_instance *instance,
2635 struct megasas_evt_log_info *eli)
2636{
2637 struct megasas_cmd *cmd;
2638 struct megasas_dcmd_frame *dcmd;
2639 struct megasas_evt_log_info *el_info;
2640 dma_addr_t el_info_h = 0;
2641
2642 cmd = megasas_get_cmd(instance);
2643
2644 if (!cmd) {
2645 return -ENOMEM;
2646 }
2647
2648 dcmd = &cmd->frame->dcmd;
2649 el_info = pci_alloc_consistent(instance->pdev,
2650 sizeof(struct megasas_evt_log_info),
2651 &el_info_h);
2652
2653 if (!el_info) {
2654 megasas_return_cmd(instance, cmd);
2655 return -ENOMEM;
2656 }
2657
2658 memset(el_info, 0, sizeof(*el_info));
2659 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2660
2661 dcmd->cmd = MFI_CMD_DCMD;
2662 dcmd->cmd_status = 0x0;
2663 dcmd->sge_count = 1;
2664 dcmd->flags = MFI_FRAME_DIR_READ;
2665 dcmd->timeout = 0;
2666 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
2667 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
2668 dcmd->sgl.sge32[0].phys_addr = el_info_h;
2669 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
2670
2671 megasas_issue_blocked_cmd(instance, cmd);
2672
2673 /*
2674 * Copy the data back into callers buffer
2675 */
2676 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
2677
2678 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
2679 el_info, el_info_h);
2680
2681 megasas_return_cmd(instance, cmd);
2682
2683 return 0;
2684}
2685
2686/**
2687 * megasas_register_aen - Registers for asynchronous event notification
2688 * @instance: Adapter soft state
2689 * @seq_num: The starting sequence number
2690 * @class_locale: Class of the event
2691 *
2692 * This function subscribes for AEN for events beyond the @seq_num. It requests
2693 * to be notified if and only if the event is of type @class_locale
2694 */
2695static int
2696megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
2697 u32 class_locale_word)
2698{
2699 int ret_val;
2700 struct megasas_cmd *cmd;
2701 struct megasas_dcmd_frame *dcmd;
2702 union megasas_evt_class_locale curr_aen;
2703 union megasas_evt_class_locale prev_aen;
2704
2705 /*
2706 * If there an AEN pending already (aen_cmd), check if the
2707 * class_locale of that pending AEN is inclusive of the new
2708 * AEN request we currently have. If it is, then we don't have
2709 * to do anything. In other words, whichever events the current
2710 * AEN request is subscribing to, have already been subscribed
2711 * to.
2712 *
2713 * If the old_cmd is _not_ inclusive, then we have to abort
2714 * that command, form a class_locale that is superset of both
2715 * old and current and re-issue to the FW
2716 */
2717
2718 curr_aen.word = class_locale_word;
2719
2720 if (instance->aen_cmd) {
2721
2722 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
2723
2724 /*
2725 * A class whose enum value is smaller is inclusive of all
2726 * higher values. If a PROGRESS (= -1) was previously
2727 * registered, then a new registration requests for higher
2728 * classes need not be sent to FW. They are automatically
2729 * included.
2730 *
2731 * Locale numbers don't have such hierarchy. They are bitmap
2732 * values
2733 */
2734 if ((prev_aen.members.class <= curr_aen.members.class) &&
2735 !((prev_aen.members.locale & curr_aen.members.locale) ^
2736 curr_aen.members.locale)) {
2737 /*
2738 * Previously issued event registration includes
2739 * current request. Nothing to do.
2740 */
2741 return 0;
2742 } else {
2743 curr_aen.members.locale |= prev_aen.members.locale;
2744
2745 if (prev_aen.members.class < curr_aen.members.class)
2746 curr_aen.members.class = prev_aen.members.class;
2747
2748 instance->aen_cmd->abort_aen = 1;
2749 ret_val = megasas_issue_blocked_abort_cmd(instance,
2750 instance->
2751 aen_cmd);
2752
2753 if (ret_val) {
2754 printk(KERN_DEBUG "megasas: Failed to abort "
2755 "previous AEN command\n");
2756 return ret_val;
2757 }
2758 }
2759 }
2760
2761 cmd = megasas_get_cmd(instance);
2762
2763 if (!cmd)
2764 return -ENOMEM;
2765
2766 dcmd = &cmd->frame->dcmd;
2767
2768 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
2769
2770 /*
2771 * Prepare DCMD for aen registration
2772 */
2773 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2774
2775 dcmd->cmd = MFI_CMD_DCMD;
2776 dcmd->cmd_status = 0x0;
2777 dcmd->sge_count = 1;
2778 dcmd->flags = MFI_FRAME_DIR_READ;
2779 dcmd->timeout = 0;
2780 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
2781 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
2782 dcmd->mbox.w[0] = seq_num;
2783 dcmd->mbox.w[1] = curr_aen.word;
2784 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
2785 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
2786
Yang, Bof4c9a132009-10-06 14:43:28 -06002787 if (instance->aen_cmd != NULL) {
2788 megasas_return_cmd(instance, cmd);
2789 return 0;
2790 }
2791
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002792 /*
2793 * Store reference to the cmd used to register for AEN. When an
2794 * application wants us to register for AEN, we have to abort this
2795 * cmd and re-register with a new EVENT LOCALE supplied by that app
2796 */
2797 instance->aen_cmd = cmd;
2798
2799 /*
2800 * Issue the aen registration frame
2801 */
Sumant Patro1341c932006-01-25 12:02:40 -08002802 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002803
2804 return 0;
2805}
2806
2807/**
2808 * megasas_start_aen - Subscribes to AEN during driver load time
2809 * @instance: Adapter soft state
2810 */
2811static int megasas_start_aen(struct megasas_instance *instance)
2812{
2813 struct megasas_evt_log_info eli;
2814 union megasas_evt_class_locale class_locale;
2815
2816 /*
2817 * Get the latest sequence number from FW
2818 */
2819 memset(&eli, 0, sizeof(eli));
2820
2821 if (megasas_get_seq_num(instance, &eli))
2822 return -1;
2823
2824 /*
2825 * Register AEN with FW for latest sequence number plus 1
2826 */
2827 class_locale.members.reserved = 0;
2828 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2829 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2830
2831 return megasas_register_aen(instance, eli.newest_seq_num + 1,
2832 class_locale.word);
2833}
2834
2835/**
2836 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2837 * @instance: Adapter soft state
2838 */
2839static int megasas_io_attach(struct megasas_instance *instance)
2840{
2841 struct Scsi_Host *host = instance->host;
2842
2843 /*
2844 * Export parameters required by SCSI mid-layer
2845 */
2846 host->irq = instance->pdev->irq;
2847 host->unique_id = instance->unique_id;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06002848 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2849 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2850 host->can_queue =
2851 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
2852 } else
2853 host->can_queue =
2854 instance->max_fw_cmds - MEGASAS_INT_CMDS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002855 host->this_id = instance->init_id;
2856 host->sg_tablesize = instance->max_num_sge;
2857 host->max_sectors = instance->max_sectors_per_req;
2858 host->cmd_per_lun = 128;
2859 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
2860 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
2861 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08002862 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002863
2864 /*
2865 * Notify the mid-layer about the new controller
2866 */
2867 if (scsi_add_host(host, &instance->pdev->dev)) {
2868 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2869 return -ENODEV;
2870 }
2871
2872 /*
2873 * Trigger SCSI to scan our drives
2874 */
2875 scsi_scan_host(host);
2876 return 0;
2877}
2878
bo yang31ea7082007-11-07 12:09:50 -05002879static int
2880megasas_set_dma_mask(struct pci_dev *pdev)
2881{
2882 /*
2883 * All our contollers are capable of performing 64-bit DMA
2884 */
2885 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07002886 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05002887
Yang Hongyang284901a2009-04-06 19:01:15 -07002888 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05002889 goto fail_set_dma_mask;
2890 }
2891 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07002892 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05002893 goto fail_set_dma_mask;
2894 }
2895 return 0;
2896
2897fail_set_dma_mask:
2898 return 1;
2899}
2900
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002901/**
2902 * megasas_probe_one - PCI hotplug entry point
2903 * @pdev: PCI device structure
2904 * @id: PCI ids of supported hotplugged adapter
2905 */
2906static int __devinit
2907megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2908{
2909 int rval;
2910 struct Scsi_Host *host;
2911 struct megasas_instance *instance;
2912
2913 /*
2914 * Announce PCI information
2915 */
2916 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2917 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2918 pdev->subsystem_device);
2919
2920 printk("bus %d:slot %d:func %d\n",
2921 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2922
2923 /*
2924 * PCI prepping: enable device set bus mastering and dma mask
2925 */
2926 rval = pci_enable_device(pdev);
2927
2928 if (rval) {
2929 return rval;
2930 }
2931
2932 pci_set_master(pdev);
2933
bo yang31ea7082007-11-07 12:09:50 -05002934 if (megasas_set_dma_mask(pdev))
2935 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002936
2937 host = scsi_host_alloc(&megasas_template,
2938 sizeof(struct megasas_instance));
2939
2940 if (!host) {
2941 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2942 goto fail_alloc_instance;
2943 }
2944
2945 instance = (struct megasas_instance *)host->hostdata;
2946 memset(instance, 0, sizeof(*instance));
2947
2948 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2949 &instance->producer_h);
2950 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2951 &instance->consumer_h);
2952
2953 if (!instance->producer || !instance->consumer) {
2954 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2955 "producer, consumer\n");
2956 goto fail_alloc_dma_buf;
2957 }
2958
2959 *instance->producer = 0;
2960 *instance->consumer = 0;
Yang, Boc3518832009-10-06 14:18:02 -06002961 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06002962 instance->flag_ieee = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002963
2964 instance->evt_detail = pci_alloc_consistent(pdev,
2965 sizeof(struct
2966 megasas_evt_detail),
2967 &instance->evt_detail_h);
2968
2969 if (!instance->evt_detail) {
2970 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2971 "event detail structure\n");
2972 goto fail_alloc_dma_buf;
2973 }
2974
2975 /*
2976 * Initialize locks and queues
2977 */
2978 INIT_LIST_HEAD(&instance->cmd_pool);
2979
Sumant Patroe4a082c2006-05-30 12:03:37 -07002980 atomic_set(&instance->fw_outstanding,0);
2981
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002982 init_waitqueue_head(&instance->int_cmd_wait_q);
2983 init_waitqueue_head(&instance->abort_cmd_wait_q);
2984
2985 spin_lock_init(&instance->cmd_pool_lock);
bo yang7343eb62007-11-09 04:35:44 -05002986 spin_lock_init(&instance->completion_lock);
Yang, Boc3518832009-10-06 14:18:02 -06002987 spin_lock_init(&poll_aen_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002988
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02002989 mutex_init(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002990
2991 /*
2992 * Initialize PCI related and misc parameters
2993 */
2994 instance->pdev = pdev;
2995 instance->host = host;
2996 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2997 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2998
Yang, Bo7bebf5c2009-10-06 14:40:58 -06002999 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3000 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06003001 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06003002 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
3003 } else
3004 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
3005
Sumant Patro658dced2006-10-03 13:09:14 -07003006 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003007 instance->flag = 0;
Yang, Boc3518832009-10-06 14:18:02 -06003008 instance->unload = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003009 instance->last_time = 0;
Sumant Patro658dced2006-10-03 13:09:14 -07003010
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003011 /*
3012 * Initialize MFI Firmware
3013 */
3014 if (megasas_init_mfi(instance))
3015 goto fail_init_mfi;
3016
3017 /*
3018 * Register IRQ
3019 */
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07003020 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003021 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
3022 goto fail_irq;
3023 }
3024
Sumant Patro1341c932006-01-25 12:02:40 -08003025 instance->instancet->enable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003026
3027 /*
3028 * Store instance in PCI softstate
3029 */
3030 pci_set_drvdata(pdev, instance);
3031
3032 /*
3033 * Add this controller to megasas_mgmt_info structure so that it
3034 * can be exported to management applications
3035 */
3036 megasas_mgmt_info.count++;
3037 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
3038 megasas_mgmt_info.max_index++;
3039
3040 /*
3041 * Initiate AEN (Asynchronous Event Notification)
3042 */
3043 if (megasas_start_aen(instance)) {
3044 printk(KERN_DEBUG "megasas: start aen failed\n");
3045 goto fail_start_aen;
3046 }
3047
3048 /*
3049 * Register with SCSI mid-layer
3050 */
3051 if (megasas_io_attach(instance))
3052 goto fail_io_attach;
3053
3054 return 0;
3055
3056 fail_start_aen:
3057 fail_io_attach:
3058 megasas_mgmt_info.count--;
3059 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
3060 megasas_mgmt_info.max_index--;
3061
3062 pci_set_drvdata(pdev, NULL);
Sumant Patrob274cab2006-10-03 12:52:12 -07003063 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003064 free_irq(instance->pdev->irq, instance);
3065
3066 megasas_release_mfi(instance);
3067
3068 fail_irq:
3069 fail_init_mfi:
3070 fail_alloc_dma_buf:
3071 if (instance->evt_detail)
3072 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3073 instance->evt_detail,
3074 instance->evt_detail_h);
3075
3076 if (instance->producer)
3077 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3078 instance->producer_h);
3079 if (instance->consumer)
3080 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3081 instance->consumer_h);
3082 scsi_host_put(host);
3083
3084 fail_alloc_instance:
3085 fail_set_dma_mask:
3086 pci_disable_device(pdev);
3087
3088 return -ENODEV;
3089}
3090
3091/**
3092 * megasas_flush_cache - Requests FW to flush all its caches
3093 * @instance: Adapter soft state
3094 */
3095static void megasas_flush_cache(struct megasas_instance *instance)
3096{
3097 struct megasas_cmd *cmd;
3098 struct megasas_dcmd_frame *dcmd;
3099
3100 cmd = megasas_get_cmd(instance);
3101
3102 if (!cmd)
3103 return;
3104
3105 dcmd = &cmd->frame->dcmd;
3106
3107 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3108
3109 dcmd->cmd = MFI_CMD_DCMD;
3110 dcmd->cmd_status = 0x0;
3111 dcmd->sge_count = 0;
3112 dcmd->flags = MFI_FRAME_DIR_NONE;
3113 dcmd->timeout = 0;
3114 dcmd->data_xfer_len = 0;
3115 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
3116 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
3117
3118 megasas_issue_blocked_cmd(instance, cmd);
3119
3120 megasas_return_cmd(instance, cmd);
3121
3122 return;
3123}
3124
3125/**
3126 * megasas_shutdown_controller - Instructs FW to shutdown the controller
3127 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05003128 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003129 */
bo yang31ea7082007-11-07 12:09:50 -05003130static void megasas_shutdown_controller(struct megasas_instance *instance,
3131 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003132{
3133 struct megasas_cmd *cmd;
3134 struct megasas_dcmd_frame *dcmd;
3135
3136 cmd = megasas_get_cmd(instance);
3137
3138 if (!cmd)
3139 return;
3140
3141 if (instance->aen_cmd)
3142 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
3143
3144 dcmd = &cmd->frame->dcmd;
3145
3146 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3147
3148 dcmd->cmd = MFI_CMD_DCMD;
3149 dcmd->cmd_status = 0x0;
3150 dcmd->sge_count = 0;
3151 dcmd->flags = MFI_FRAME_DIR_NONE;
3152 dcmd->timeout = 0;
3153 dcmd->data_xfer_len = 0;
bo yang31ea7082007-11-07 12:09:50 -05003154 dcmd->opcode = opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003155
3156 megasas_issue_blocked_cmd(instance, cmd);
3157
3158 megasas_return_cmd(instance, cmd);
3159
3160 return;
3161}
3162
Jiri Slaby33139b22008-05-01 17:56:02 +02003163#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003164/**
bo yangad84db22007-11-09 04:40:16 -05003165 * megasas_suspend - driver suspend entry point
3166 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05003167 * @state: PCI power state to suspend routine
3168 */
Jiri Slaby33139b22008-05-01 17:56:02 +02003169static int
bo yang31ea7082007-11-07 12:09:50 -05003170megasas_suspend(struct pci_dev *pdev, pm_message_t state)
3171{
3172 struct Scsi_Host *host;
3173 struct megasas_instance *instance;
3174
3175 instance = pci_get_drvdata(pdev);
3176 host = instance->host;
3177
bo yangad84db22007-11-09 04:40:16 -05003178 if (poll_mode_io)
3179 del_timer_sync(&instance->io_completion_timer);
3180
bo yang31ea7082007-11-07 12:09:50 -05003181 megasas_flush_cache(instance);
3182 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
3183 tasklet_kill(&instance->isr_tasklet);
3184
3185 pci_set_drvdata(instance->pdev, instance);
3186 instance->instancet->disable_intr(instance->reg_set);
3187 free_irq(instance->pdev->irq, instance);
3188
3189 pci_save_state(pdev);
3190 pci_disable_device(pdev);
3191
3192 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3193
3194 return 0;
3195}
3196
3197/**
3198 * megasas_resume- driver resume entry point
3199 * @pdev: PCI device structure
3200 */
Jiri Slaby33139b22008-05-01 17:56:02 +02003201static int
bo yang31ea7082007-11-07 12:09:50 -05003202megasas_resume(struct pci_dev *pdev)
3203{
3204 int rval;
3205 struct Scsi_Host *host;
3206 struct megasas_instance *instance;
3207
3208 instance = pci_get_drvdata(pdev);
3209 host = instance->host;
3210 pci_set_power_state(pdev, PCI_D0);
3211 pci_enable_wake(pdev, PCI_D0, 0);
3212 pci_restore_state(pdev);
3213
3214 /*
3215 * PCI prepping: enable device set bus mastering and dma mask
3216 */
3217 rval = pci_enable_device(pdev);
3218
3219 if (rval) {
3220 printk(KERN_ERR "megasas: Enable device failed\n");
3221 return rval;
3222 }
3223
3224 pci_set_master(pdev);
3225
3226 if (megasas_set_dma_mask(pdev))
3227 goto fail_set_dma_mask;
3228
3229 /*
3230 * Initialize MFI Firmware
3231 */
3232
3233 *instance->producer = 0;
3234 *instance->consumer = 0;
3235
3236 atomic_set(&instance->fw_outstanding, 0);
3237
3238 /*
3239 * We expect the FW state to be READY
3240 */
3241 if (megasas_transition_to_ready(instance))
3242 goto fail_ready_state;
3243
3244 if (megasas_issue_init_mfi(instance))
3245 goto fail_init_mfi;
3246
3247 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
3248 (unsigned long)instance);
3249
3250 /*
3251 * Register IRQ
3252 */
3253 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED,
3254 "megasas", instance)) {
3255 printk(KERN_ERR "megasas: Failed to register IRQ\n");
3256 goto fail_irq;
3257 }
3258
3259 instance->instancet->enable_intr(instance->reg_set);
3260
3261 /*
3262 * Initiate AEN (Asynchronous Event Notification)
3263 */
3264 if (megasas_start_aen(instance))
3265 printk(KERN_ERR "megasas: Start AEN failed\n");
3266
bo yangad84db22007-11-09 04:40:16 -05003267 /* Initialize the cmd completion timer */
3268 if (poll_mode_io)
3269 megasas_start_timer(instance, &instance->io_completion_timer,
3270 megasas_io_completion_timer,
3271 MEGASAS_COMPLETION_TIMER_INTERVAL);
bo yang31ea7082007-11-07 12:09:50 -05003272 return 0;
3273
3274fail_irq:
3275fail_init_mfi:
3276 if (instance->evt_detail)
3277 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3278 instance->evt_detail,
3279 instance->evt_detail_h);
3280
3281 if (instance->producer)
3282 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3283 instance->producer_h);
3284 if (instance->consumer)
3285 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3286 instance->consumer_h);
3287 scsi_host_put(host);
3288
3289fail_set_dma_mask:
3290fail_ready_state:
3291
3292 pci_disable_device(pdev);
3293
3294 return -ENODEV;
3295}
Jiri Slaby33139b22008-05-01 17:56:02 +02003296#else
3297#define megasas_suspend NULL
3298#define megasas_resume NULL
3299#endif
bo yang31ea7082007-11-07 12:09:50 -05003300
3301/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003302 * megasas_detach_one - PCI hot"un"plug entry point
3303 * @pdev: PCI device structure
3304 */
Jiri Slaby33139b22008-05-01 17:56:02 +02003305static void __devexit megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003306{
3307 int i;
3308 struct Scsi_Host *host;
3309 struct megasas_instance *instance;
3310
3311 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06003312 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003313 host = instance->host;
3314
bo yangad84db22007-11-09 04:40:16 -05003315 if (poll_mode_io)
3316 del_timer_sync(&instance->io_completion_timer);
3317
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003318 scsi_remove_host(instance->host);
3319 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05003320 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Sumant Patro5d018ad2006-10-03 13:13:18 -07003321 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003322
3323 /*
3324 * Take the instance off the instance array. Note that we will not
3325 * decrement the max_index. We let this array be sparse array
3326 */
3327 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3328 if (megasas_mgmt_info.instance[i] == instance) {
3329 megasas_mgmt_info.count--;
3330 megasas_mgmt_info.instance[i] = NULL;
3331
3332 break;
3333 }
3334 }
3335
3336 pci_set_drvdata(instance->pdev, NULL);
3337
Sumant Patrob274cab2006-10-03 12:52:12 -07003338 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003339
3340 free_irq(instance->pdev->irq, instance);
3341
3342 megasas_release_mfi(instance);
3343
3344 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3345 instance->evt_detail, instance->evt_detail_h);
3346
3347 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3348 instance->producer_h);
3349
3350 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3351 instance->consumer_h);
3352
3353 scsi_host_put(host);
3354
3355 pci_set_drvdata(pdev, NULL);
3356
3357 pci_disable_device(pdev);
3358
3359 return;
3360}
3361
3362/**
3363 * megasas_shutdown - Shutdown entry point
3364 * @device: Generic device structure
3365 */
3366static void megasas_shutdown(struct pci_dev *pdev)
3367{
3368 struct megasas_instance *instance = pci_get_drvdata(pdev);
3369 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07003370 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003371}
3372
3373/**
3374 * megasas_mgmt_open - char node "open" entry point
3375 */
3376static int megasas_mgmt_open(struct inode *inode, struct file *filep)
3377{
Jonathan Corbetf2b98572008-05-18 15:32:43 -06003378 cycle_kernel_lock();
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003379 /*
3380 * Allow only those users with admin rights
3381 */
3382 if (!capable(CAP_SYS_ADMIN))
3383 return -EACCES;
3384
3385 return 0;
3386}
3387
3388/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003389 * megasas_mgmt_fasync - Async notifier registration from applications
3390 *
3391 * This function adds the calling process to a driver global queue. When an
3392 * event occurs, SIGIO will be sent to all processes in this queue.
3393 */
3394static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
3395{
3396 int rc;
3397
Arjan van de Ven0b950672006-01-11 13:16:10 +01003398 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003399
3400 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
3401
Arjan van de Ven0b950672006-01-11 13:16:10 +01003402 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003403
3404 if (rc >= 0) {
3405 /* For sanity check when we get ioctl */
3406 filep->private_data = filep;
3407 return 0;
3408 }
3409
3410 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
3411
3412 return rc;
3413}
3414
3415/**
Yang, Boc3518832009-10-06 14:18:02 -06003416 * megasas_mgmt_poll - char node "poll" entry point
3417 * */
3418static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
3419{
3420 unsigned int mask;
3421 unsigned long flags;
3422 poll_wait(file, &megasas_poll_wait, wait);
3423 spin_lock_irqsave(&poll_aen_lock, flags);
3424 if (megasas_poll_wait_aen)
3425 mask = (POLLIN | POLLRDNORM);
3426 else
3427 mask = 0;
3428 spin_unlock_irqrestore(&poll_aen_lock, flags);
3429 return mask;
3430}
3431
3432/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003433 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
3434 * @instance: Adapter soft state
3435 * @argp: User's ioctl packet
3436 */
3437static int
3438megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
3439 struct megasas_iocpacket __user * user_ioc,
3440 struct megasas_iocpacket *ioc)
3441{
3442 struct megasas_sge32 *kern_sge32;
3443 struct megasas_cmd *cmd;
3444 void *kbuff_arr[MAX_IOCTL_SGE];
3445 dma_addr_t buf_handle = 0;
3446 int error = 0, i;
3447 void *sense = NULL;
3448 dma_addr_t sense_handle;
3449 u32 *sense_ptr;
3450
3451 memset(kbuff_arr, 0, sizeof(kbuff_arr));
3452
3453 if (ioc->sge_count > MAX_IOCTL_SGE) {
3454 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
3455 ioc->sge_count, MAX_IOCTL_SGE);
3456 return -EINVAL;
3457 }
3458
3459 cmd = megasas_get_cmd(instance);
3460 if (!cmd) {
3461 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
3462 return -ENOMEM;
3463 }
3464
3465 /*
3466 * User's IOCTL packet has 2 frames (maximum). Copy those two
3467 * frames into our cmd's frames. cmd->frame's context will get
3468 * overwritten when we copy from user's frames. So set that value
3469 * alone separately
3470 */
3471 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
3472 cmd->frame->hdr.context = cmd->index;
Yang, Boc3518832009-10-06 14:18:02 -06003473 cmd->frame->hdr.pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003474
3475 /*
3476 * The management interface between applications and the fw uses
3477 * MFI frames. E.g, RAID configuration changes, LD property changes
3478 * etc are accomplishes through different kinds of MFI frames. The
3479 * driver needs to care only about substituting user buffers with
3480 * kernel buffers in SGLs. The location of SGL is embedded in the
3481 * struct iocpacket itself.
3482 */
3483 kern_sge32 = (struct megasas_sge32 *)
3484 ((unsigned long)cmd->frame + ioc->sgl_off);
3485
3486 /*
3487 * For each user buffer, create a mirror buffer and copy in
3488 */
3489 for (i = 0; i < ioc->sge_count; i++) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08003490 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003491 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08003492 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003493 if (!kbuff_arr[i]) {
3494 printk(KERN_DEBUG "megasas: Failed to alloc "
3495 "kernel SGL buffer for IOCTL \n");
3496 error = -ENOMEM;
3497 goto out;
3498 }
3499
3500 /*
3501 * We don't change the dma_coherent_mask, so
3502 * pci_alloc_consistent only returns 32bit addresses
3503 */
3504 kern_sge32[i].phys_addr = (u32) buf_handle;
3505 kern_sge32[i].length = ioc->sgl[i].iov_len;
3506
3507 /*
3508 * We created a kernel buffer corresponding to the
3509 * user buffer. Now copy in from the user buffer
3510 */
3511 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
3512 (u32) (ioc->sgl[i].iov_len))) {
3513 error = -EFAULT;
3514 goto out;
3515 }
3516 }
3517
3518 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08003519 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
3520 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003521 if (!sense) {
3522 error = -ENOMEM;
3523 goto out;
3524 }
3525
3526 sense_ptr =
3527 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
3528 *sense_ptr = sense_handle;
3529 }
3530
3531 /*
3532 * Set the sync_cmd flag so that the ISR knows not to complete this
3533 * cmd to the SCSI mid-layer
3534 */
3535 cmd->sync_cmd = 1;
3536 megasas_issue_blocked_cmd(instance, cmd);
3537 cmd->sync_cmd = 0;
3538
3539 /*
3540 * copy out the kernel buffers to user buffers
3541 */
3542 for (i = 0; i < ioc->sge_count; i++) {
3543 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
3544 ioc->sgl[i].iov_len)) {
3545 error = -EFAULT;
3546 goto out;
3547 }
3548 }
3549
3550 /*
3551 * copy out the sense
3552 */
3553 if (ioc->sense_len) {
3554 /*
bo yangb70a41e2008-03-18 03:13:06 -04003555 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003556 * sense buffer address
3557 */
bo yangb70a41e2008-03-18 03:13:06 -04003558 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
3559 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003560
bo yangb70a41e2008-03-18 03:13:06 -04003561 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
3562 sense, ioc->sense_len)) {
bo yangb10c36a2007-11-09 04:28:47 -05003563 printk(KERN_ERR "megasas: Failed to copy out to user "
3564 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003565 error = -EFAULT;
3566 goto out;
3567 }
3568 }
3569
3570 /*
3571 * copy the status codes returned by the fw
3572 */
3573 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
3574 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
3575 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
3576 error = -EFAULT;
3577 }
3578
3579 out:
3580 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08003581 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003582 sense, sense_handle);
3583 }
3584
3585 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08003586 dma_free_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003587 kern_sge32[i].length,
3588 kbuff_arr[i], kern_sge32[i].phys_addr);
3589 }
3590
3591 megasas_return_cmd(instance, cmd);
3592 return error;
3593}
3594
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003595static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
3596{
3597 struct megasas_iocpacket __user *user_ioc =
3598 (struct megasas_iocpacket __user *)arg;
3599 struct megasas_iocpacket *ioc;
3600 struct megasas_instance *instance;
3601 int error;
3602
3603 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
3604 if (!ioc)
3605 return -ENOMEM;
3606
3607 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
3608 error = -EFAULT;
3609 goto out_kfree_ioc;
3610 }
3611
3612 instance = megasas_lookup_instance(ioc->host_no);
3613 if (!instance) {
3614 error = -ENODEV;
3615 goto out_kfree_ioc;
3616 }
3617
3618 /*
3619 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
3620 */
3621 if (down_interruptible(&instance->ioctl_sem)) {
3622 error = -ERESTARTSYS;
3623 goto out_kfree_ioc;
3624 }
3625 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
3626 up(&instance->ioctl_sem);
3627
3628 out_kfree_ioc:
3629 kfree(ioc);
3630 return error;
3631}
3632
3633static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
3634{
3635 struct megasas_instance *instance;
3636 struct megasas_aen aen;
3637 int error;
3638
3639 if (file->private_data != file) {
3640 printk(KERN_DEBUG "megasas: fasync_helper was not "
3641 "called first\n");
3642 return -EINVAL;
3643 }
3644
3645 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
3646 return -EFAULT;
3647
3648 instance = megasas_lookup_instance(aen.host_no);
3649
3650 if (!instance)
3651 return -ENODEV;
3652
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02003653 mutex_lock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003654 error = megasas_register_aen(instance, aen.seq_num,
3655 aen.class_locale_word);
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02003656 mutex_unlock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003657 return error;
3658}
3659
3660/**
3661 * megasas_mgmt_ioctl - char node ioctl entry point
3662 */
3663static long
3664megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3665{
3666 switch (cmd) {
3667 case MEGASAS_IOC_FIRMWARE:
3668 return megasas_mgmt_ioctl_fw(file, arg);
3669
3670 case MEGASAS_IOC_GET_AEN:
3671 return megasas_mgmt_ioctl_aen(file, arg);
3672 }
3673
3674 return -ENOTTY;
3675}
3676
3677#ifdef CONFIG_COMPAT
3678static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
3679{
3680 struct compat_megasas_iocpacket __user *cioc =
3681 (struct compat_megasas_iocpacket __user *)arg;
3682 struct megasas_iocpacket __user *ioc =
3683 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
3684 int i;
3685 int error = 0;
3686
Jeff Garzik83aabc12006-10-04 06:34:03 -04003687 if (clear_user(ioc, sizeof(*ioc)))
3688 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003689
3690 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
3691 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
3692 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
3693 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
3694 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
3695 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
3696 return -EFAULT;
3697
3698 for (i = 0; i < MAX_IOCTL_SGE; i++) {
3699 compat_uptr_t ptr;
3700
3701 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
3702 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
3703 copy_in_user(&ioc->sgl[i].iov_len,
3704 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
3705 return -EFAULT;
3706 }
3707
3708 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
3709
3710 if (copy_in_user(&cioc->frame.hdr.cmd_status,
3711 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
3712 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
3713 return -EFAULT;
3714 }
3715 return error;
3716}
3717
3718static long
3719megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
3720 unsigned long arg)
3721{
3722 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08003723 case MEGASAS_IOC_FIRMWARE32:
3724 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003725 case MEGASAS_IOC_GET_AEN:
3726 return megasas_mgmt_ioctl_aen(file, arg);
3727 }
3728
3729 return -ENOTTY;
3730}
3731#endif
3732
3733/*
3734 * File operations structure for management interface
3735 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08003736static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003737 .owner = THIS_MODULE,
3738 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003739 .fasync = megasas_mgmt_fasync,
3740 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06003741 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003742#ifdef CONFIG_COMPAT
3743 .compat_ioctl = megasas_mgmt_compat_ioctl,
3744#endif
3745};
3746
3747/*
3748 * PCI hotplug support registration structure
3749 */
3750static struct pci_driver megasas_pci_driver = {
3751
3752 .name = "megaraid_sas",
3753 .id_table = megasas_pci_table,
3754 .probe = megasas_probe_one,
3755 .remove = __devexit_p(megasas_detach_one),
bo yang31ea7082007-11-07 12:09:50 -05003756 .suspend = megasas_suspend,
3757 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003758 .shutdown = megasas_shutdown,
3759};
3760
3761/*
3762 * Sysfs driver attributes
3763 */
3764static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
3765{
3766 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
3767 MEGASAS_VERSION);
3768}
3769
3770static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
3771
3772static ssize_t
3773megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
3774{
3775 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
3776 MEGASAS_RELDATE);
3777}
3778
3779static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
3780 NULL);
3781
Sumant Patro658dced2006-10-03 13:09:14 -07003782static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06003783megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
3784{
3785 return sprintf(buf, "%u\n", support_poll_for_event);
3786}
3787
3788static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
3789 megasas_sysfs_show_support_poll_for_event, NULL);
3790
3791static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07003792megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
3793{
bo yangad84db22007-11-09 04:40:16 -05003794 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07003795}
3796
3797static ssize_t
3798megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
3799{
3800 int retval = count;
3801 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
3802 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
3803 retval = -EINVAL;
3804 }
3805 return retval;
3806}
3807
Joe Malicki66dca9b2008-08-14 17:14:48 -04003808static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05003809 megasas_sysfs_set_dbg_lvl);
3810
3811static ssize_t
3812megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
3813{
3814 return sprintf(buf, "%u\n", poll_mode_io);
3815}
3816
3817static ssize_t
3818megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
3819 const char *buf, size_t count)
3820{
3821 int retval = count;
3822 int tmp = poll_mode_io;
3823 int i;
3824 struct megasas_instance *instance;
3825
3826 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
3827 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
3828 retval = -EINVAL;
3829 }
3830
3831 /*
3832 * Check if poll_mode_io is already set or is same as previous value
3833 */
3834 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
3835 goto out;
3836
3837 if (poll_mode_io) {
3838 /*
3839 * Start timers for all adapters
3840 */
3841 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3842 instance = megasas_mgmt_info.instance[i];
3843 if (instance) {
3844 megasas_start_timer(instance,
3845 &instance->io_completion_timer,
3846 megasas_io_completion_timer,
3847 MEGASAS_COMPLETION_TIMER_INTERVAL);
3848 }
3849 }
3850 } else {
3851 /*
3852 * Delete timers for all adapters
3853 */
3854 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3855 instance = megasas_mgmt_info.instance[i];
3856 if (instance)
3857 del_timer_sync(&instance->io_completion_timer);
3858 }
3859 }
3860
3861out:
3862 return retval;
3863}
3864
3865static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUGO,
3866 megasas_sysfs_show_poll_mode_io,
3867 megasas_sysfs_set_poll_mode_io);
Sumant Patro658dced2006-10-03 13:09:14 -07003868
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003869/**
3870 * megasas_init - Driver load entry point
3871 */
3872static int __init megasas_init(void)
3873{
3874 int rval;
3875
3876 /*
3877 * Announce driver version and other information
3878 */
3879 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
3880 MEGASAS_EXT_VERSION);
3881
Yang, Bo72c4fd32009-10-06 14:20:59 -06003882 support_poll_for_event = 2;
3883
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003884 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
3885
3886 /*
3887 * Register character device node
3888 */
3889 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
3890
3891 if (rval < 0) {
3892 printk(KERN_DEBUG "megasas: failed to open device node\n");
3893 return rval;
3894 }
3895
3896 megasas_mgmt_majorno = rval;
3897
3898 /*
3899 * Register ourselves as PCI hotplug module
3900 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00003901 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003902
3903 if (rval) {
3904 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04003905 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003906 }
3907
Jeff Garzik83aabc12006-10-04 06:34:03 -04003908 rval = driver_create_file(&megasas_pci_driver.driver,
3909 &driver_attr_version);
3910 if (rval)
3911 goto err_dcf_attr_ver;
3912 rval = driver_create_file(&megasas_pci_driver.driver,
3913 &driver_attr_release_date);
3914 if (rval)
3915 goto err_dcf_rel_date;
Yang, Bo72c4fd32009-10-06 14:20:59 -06003916
3917 rval = driver_create_file(&megasas_pci_driver.driver,
3918 &driver_attr_support_poll_for_event);
3919 if (rval)
3920 goto err_dcf_support_poll_for_event;
3921
Jeff Garzik83aabc12006-10-04 06:34:03 -04003922 rval = driver_create_file(&megasas_pci_driver.driver,
3923 &driver_attr_dbg_lvl);
3924 if (rval)
3925 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05003926 rval = driver_create_file(&megasas_pci_driver.driver,
3927 &driver_attr_poll_mode_io);
3928 if (rval)
3929 goto err_dcf_poll_mode_io;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003930
3931 return rval;
bo yangad84db22007-11-09 04:40:16 -05003932
3933err_dcf_poll_mode_io:
3934 driver_remove_file(&megasas_pci_driver.driver,
3935 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04003936err_dcf_dbg_lvl:
3937 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06003938 &driver_attr_support_poll_for_event);
3939
3940err_dcf_support_poll_for_event:
3941 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04003942 &driver_attr_release_date);
Yang, Bo72c4fd32009-10-06 14:20:59 -06003943
Jeff Garzik83aabc12006-10-04 06:34:03 -04003944err_dcf_rel_date:
3945 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3946err_dcf_attr_ver:
3947 pci_unregister_driver(&megasas_pci_driver);
3948err_pcidrv:
3949 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3950 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003951}
3952
3953/**
3954 * megasas_exit - Driver unload entry point
3955 */
3956static void __exit megasas_exit(void)
3957{
Sumant Patro658dced2006-10-03 13:09:14 -07003958 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05003959 &driver_attr_poll_mode_io);
3960 driver_remove_file(&megasas_pci_driver.driver,
Sumant Patro658dced2006-10-03 13:09:14 -07003961 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04003962 driver_remove_file(&megasas_pci_driver.driver,
3963 &driver_attr_release_date);
3964 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003965
3966 pci_unregister_driver(&megasas_pci_driver);
3967 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3968}
3969
3970module_init(megasas_init);
3971module_exit(megasas_exit);