blob: b6e43271883c277f0523eff64555e459a6568ca7 [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
Sumant Patrob1df99d2006-10-03 12:40:47 -0700699 /**
700 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -0400701 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -0700702 * @sge_count : number of sg elements
703 *
704 * Returns the number of frames required for numnber of sge's (sge_count)
705 */
706
bo yangd532dbe2008-03-17 03:36:43 -0400707static u32 megasas_get_frame_count(u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -0700708{
709 int num_cnt;
710 int sge_bytes;
711 u32 sge_sz;
712 u32 frame_count=0;
713
714 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
715 sizeof(struct megasas_sge32);
716
717 /*
bo yangd532dbe2008-03-17 03:36:43 -0400718 * Main frame can contain 2 SGEs for 64-bit SGLs and
719 * 3 SGEs for 32-bit SGLs for ldio &
720 * 1 SGEs for 64-bit SGLs and
721 * 2 SGEs for 32-bit SGLs for pthru frame
722 */
723 if (unlikely(frame_type == PTHRU_FRAME)) {
724 if (IS_DMA64)
725 num_cnt = sge_count - 1;
726 else
727 num_cnt = sge_count - 2;
728 } else {
729 if (IS_DMA64)
730 num_cnt = sge_count - 2;
731 else
732 num_cnt = sge_count - 3;
733 }
Sumant Patrob1df99d2006-10-03 12:40:47 -0700734
735 if(num_cnt>0){
736 sge_bytes = sge_sz * num_cnt;
737
738 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
739 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
740 }
741 /* Main frame */
742 frame_count +=1;
743
744 if (frame_count > 7)
745 frame_count = 8;
746 return frame_count;
747}
748
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400749/**
750 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
751 * @instance: Adapter soft state
752 * @scp: SCSI command
753 * @cmd: Command to be prepared in
754 *
755 * This function prepares CDB commands. These are typcially pass-through
756 * commands to the devices.
757 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800758static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400759megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
760 struct megasas_cmd *cmd)
761{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400762 u32 is_logical;
763 u32 device_id;
764 u16 flags = 0;
765 struct megasas_pthru_frame *pthru;
766
767 is_logical = MEGASAS_IS_LOGICAL(scp);
768 device_id = MEGASAS_DEV_INDEX(instance, scp);
769 pthru = (struct megasas_pthru_frame *)cmd->frame;
770
771 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
772 flags = MFI_FRAME_DIR_WRITE;
773 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
774 flags = MFI_FRAME_DIR_READ;
775 else if (scp->sc_data_direction == PCI_DMA_NONE)
776 flags = MFI_FRAME_DIR_NONE;
777
778 /*
779 * Prepare the DCDB frame
780 */
781 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
782 pthru->cmd_status = 0x0;
783 pthru->scsi_status = 0x0;
784 pthru->target_id = device_id;
785 pthru->lun = scp->device->lun;
786 pthru->cdb_len = scp->cmd_len;
787 pthru->timeout = 0;
788 pthru->flags = flags;
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900789 pthru->data_xfer_len = scsi_bufflen(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400790
791 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
792
793 /*
Yang, Bo8d568252009-10-06 14:12:21 -0600794 * If the command is for the tape device, set the
795 * pthru timeout to the os layer timeout value.
796 */
797 if (scp->device->type == TYPE_TAPE) {
798 if ((scp->request->timeout / HZ) > 0xFFFF)
799 pthru->timeout = 0xFFFF;
800 else
801 pthru->timeout = scp->request->timeout / HZ;
802 }
803
804 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400805 * Construct SGL
806 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400807 if (IS_DMA64) {
808 pthru->flags |= MFI_FRAME_SGL64;
809 pthru->sge_count = megasas_make_sgl64(instance, scp,
810 &pthru->sgl);
811 } else
812 pthru->sge_count = megasas_make_sgl32(instance, scp,
813 &pthru->sgl);
814
815 /*
816 * Sense info specific
817 */
818 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
819 pthru->sense_buf_phys_addr_hi = 0;
820 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
821
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400822 /*
823 * Compute the total number of frames this command consumes. FW uses
824 * this number to pull sufficient number of frames from host memory.
825 */
bo yangd532dbe2008-03-17 03:36:43 -0400826 cmd->frame_count = megasas_get_frame_count(pthru->sge_count,
827 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400828
829 return cmd->frame_count;
830}
831
832/**
833 * megasas_build_ldio - Prepares IOs to logical devices
834 * @instance: Adapter soft state
835 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +0200836 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400837 *
838 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
839 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800840static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400841megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
842 struct megasas_cmd *cmd)
843{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400844 u32 device_id;
845 u8 sc = scp->cmnd[0];
846 u16 flags = 0;
847 struct megasas_io_frame *ldio;
848
849 device_id = MEGASAS_DEV_INDEX(instance, scp);
850 ldio = (struct megasas_io_frame *)cmd->frame;
851
852 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
853 flags = MFI_FRAME_DIR_WRITE;
854 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
855 flags = MFI_FRAME_DIR_READ;
856
857 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -0700858 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400859 */
860 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
861 ldio->cmd_status = 0x0;
862 ldio->scsi_status = 0x0;
863 ldio->target_id = device_id;
864 ldio->timeout = 0;
865 ldio->reserved_0 = 0;
866 ldio->pad_0 = 0;
867 ldio->flags = flags;
868 ldio->start_lba_hi = 0;
869 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
870
871 /*
872 * 6-byte READ(0x08) or WRITE(0x0A) cdb
873 */
874 if (scp->cmd_len == 6) {
875 ldio->lba_count = (u32) scp->cmnd[4];
876 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
877 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
878
879 ldio->start_lba_lo &= 0x1FFFFF;
880 }
881
882 /*
883 * 10-byte READ(0x28) or WRITE(0x2A) cdb
884 */
885 else if (scp->cmd_len == 10) {
886 ldio->lba_count = (u32) scp->cmnd[8] |
887 ((u32) scp->cmnd[7] << 8);
888 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
889 ((u32) scp->cmnd[3] << 16) |
890 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
891 }
892
893 /*
894 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
895 */
896 else if (scp->cmd_len == 12) {
897 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
898 ((u32) scp->cmnd[7] << 16) |
899 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
900
901 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
902 ((u32) scp->cmnd[3] << 16) |
903 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
904 }
905
906 /*
907 * 16-byte READ(0x88) or WRITE(0x8A) cdb
908 */
909 else if (scp->cmd_len == 16) {
910 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
911 ((u32) scp->cmnd[11] << 16) |
912 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
913
914 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
915 ((u32) scp->cmnd[7] << 16) |
916 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
917
918 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
919 ((u32) scp->cmnd[3] << 16) |
920 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
921
922 }
923
924 /*
925 * Construct SGL
926 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400927 if (IS_DMA64) {
928 ldio->flags |= MFI_FRAME_SGL64;
929 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
930 } else
931 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
932
933 /*
934 * Sense info specific
935 */
936 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
937 ldio->sense_buf_phys_addr_hi = 0;
938 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
939
Sumant Patrob1df99d2006-10-03 12:40:47 -0700940 /*
941 * Compute the total number of frames this command consumes. FW uses
942 * this number to pull sufficient number of frames from host memory.
943 */
bo yangd532dbe2008-03-17 03:36:43 -0400944 cmd->frame_count = megasas_get_frame_count(ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400945
946 return cmd->frame_count;
947}
948
949/**
Sumant Patrocb59aa62006-01-25 11:53:25 -0800950 * megasas_is_ldio - Checks if the cmd is for logical drive
951 * @scmd: SCSI command
952 *
953 * Called by megasas_queue_command to find out if the command to be queued
954 * is a logical drive command
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400955 */
Sumant Patrocb59aa62006-01-25 11:53:25 -0800956static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400957{
Sumant Patrocb59aa62006-01-25 11:53:25 -0800958 if (!MEGASAS_IS_LOGICAL(cmd))
959 return 0;
960 switch (cmd->cmnd[0]) {
961 case READ_10:
962 case WRITE_10:
963 case READ_12:
964 case WRITE_12:
965 case READ_6:
966 case WRITE_6:
967 case READ_16:
968 case WRITE_16:
969 return 1;
970 default:
971 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400972 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400973}
974
Sumant Patro658dced2006-10-03 13:09:14 -0700975 /**
976 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
977 * in FW
978 * @instance: Adapter soft state
979 */
980static inline void
981megasas_dump_pending_frames(struct megasas_instance *instance)
982{
983 struct megasas_cmd *cmd;
984 int i,n;
985 union megasas_sgl *mfi_sgl;
986 struct megasas_io_frame *ldio;
987 struct megasas_pthru_frame *pthru;
988 u32 sgcount;
989 u32 max_cmd = instance->max_fw_cmds;
990
991 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
992 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
993 if (IS_DMA64)
994 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
995 else
996 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
997
998 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
999 for (i = 0; i < max_cmd; i++) {
1000 cmd = instance->cmd_list[i];
1001 if(!cmd->scmd)
1002 continue;
1003 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1004 if (megasas_is_ldio(cmd->scmd)){
1005 ldio = (struct megasas_io_frame *)cmd->frame;
1006 mfi_sgl = &ldio->sgl;
1007 sgcount = ldio->sge_count;
1008 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);
1009 }
1010 else {
1011 pthru = (struct megasas_pthru_frame *) cmd->frame;
1012 mfi_sgl = &pthru->sgl;
1013 sgcount = pthru->sge_count;
1014 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);
1015 }
1016 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1017 for (n = 0; n < sgcount; n++){
1018 if (IS_DMA64)
1019 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) ;
1020 else
1021 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
1022 }
1023 }
1024 printk(KERN_ERR "\n");
1025 } /*for max_cmd*/
1026 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1027 for (i = 0; i < max_cmd; i++) {
1028
1029 cmd = instance->cmd_list[i];
1030
1031 if(cmd->sync_cmd == 1){
1032 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1033 }
1034 }
1035 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1036}
1037
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001038/**
1039 * megasas_queue_command - Queue entry point
1040 * @scmd: SCSI command to be queued
1041 * @done: Callback entry point
1042 */
1043static int
1044megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
1045{
1046 u32 frame_count;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001047 struct megasas_cmd *cmd;
1048 struct megasas_instance *instance;
1049
1050 instance = (struct megasas_instance *)
1051 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001052
1053 /* Don't process if we have already declared adapter dead */
1054 if (instance->hw_crit_error)
1055 return SCSI_MLQUEUE_HOST_BUSY;
1056
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001057 scmd->scsi_done = done;
1058 scmd->result = 0;
1059
Sumant Patrocb59aa62006-01-25 11:53:25 -08001060 if (MEGASAS_IS_LOGICAL(scmd) &&
1061 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1062 scmd->result = DID_BAD_TARGET << 16;
1063 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001064 }
1065
Sumant Patro02b01e02007-02-14 13:00:55 -08001066 switch (scmd->cmnd[0]) {
1067 case SYNCHRONIZE_CACHE:
1068 /*
1069 * FW takes care of flush cache on its own
1070 * No need to send it down
1071 */
1072 scmd->result = DID_OK << 16;
1073 goto out_done;
1074 default:
1075 break;
1076 }
1077
Sumant Patrocb59aa62006-01-25 11:53:25 -08001078 cmd = megasas_get_cmd(instance);
1079 if (!cmd)
1080 return SCSI_MLQUEUE_HOST_BUSY;
1081
1082 /*
1083 * Logical drive command
1084 */
1085 if (megasas_is_ldio(scmd))
1086 frame_count = megasas_build_ldio(instance, scmd, cmd);
1087 else
1088 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1089
1090 if (!frame_count)
1091 goto out_return_cmd;
1092
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001093 cmd->scmd = scmd;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001094 scmd->SCp.ptr = (char *)cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001095
1096 /*
1097 * Issue the command to the FW
1098 */
Sumant Patroe4a082c2006-05-30 12:03:37 -07001099 atomic_inc(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001100
Sumant Patro1341c932006-01-25 12:02:40 -08001101 instance->instancet->fire_cmd(cmd->frame_phys_addr ,cmd->frame_count-1,instance->reg_set);
bo yangad84db22007-11-09 04:40:16 -05001102 /*
1103 * Check if we have pend cmds to be completed
1104 */
1105 if (poll_mode_io && atomic_read(&instance->fw_outstanding))
1106 tasklet_schedule(&instance->isr_tasklet);
1107
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001108
1109 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001110
1111 out_return_cmd:
1112 megasas_return_cmd(instance, cmd);
1113 out_done:
1114 done(scmd);
1115 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001116}
1117
Christoph Hellwig147aab62006-02-17 12:13:48 +01001118static int megasas_slave_configure(struct scsi_device *sdev)
1119{
1120 /*
1121 * Don't export physical disk devices to the disk driver.
1122 *
1123 * FIXME: Currently we don't export them to the midlayer at all.
1124 * That will be fixed once LSI engineers have audited the
1125 * firmware for possible issues.
1126 */
1127 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS && sdev->type == TYPE_DISK)
1128 return -ENXIO;
Christoph Hellwige5b3a652006-03-10 17:08:57 +01001129
1130 /*
1131 * The RAID firmware may require extended timeouts.
1132 */
1133 if (sdev->channel >= MEGASAS_MAX_PD_CHANNELS)
James Bottomleyee1ab9e2008-11-30 10:27:51 -06001134 blk_queue_rq_timeout(sdev->request_queue,
1135 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
Christoph Hellwig147aab62006-02-17 12:13:48 +01001136 return 0;
1137}
1138
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001139/**
bo yang7343eb62007-11-09 04:35:44 -05001140 * megasas_complete_cmd_dpc - Returns FW's controller structure
1141 * @instance_addr: Address of adapter soft state
1142 *
1143 * Tasklet to complete cmds
1144 */
1145static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1146{
1147 u32 producer;
1148 u32 consumer;
1149 u32 context;
1150 struct megasas_cmd *cmd;
1151 struct megasas_instance *instance =
1152 (struct megasas_instance *)instance_addr;
1153 unsigned long flags;
1154
1155 /* If we have already declared adapter dead, donot complete cmds */
1156 if (instance->hw_crit_error)
1157 return;
1158
1159 spin_lock_irqsave(&instance->completion_lock, flags);
1160
1161 producer = *instance->producer;
1162 consumer = *instance->consumer;
1163
1164 while (consumer != producer) {
1165 context = instance->reply_queue[consumer];
1166
1167 cmd = instance->cmd_list[context];
1168
1169 megasas_complete_cmd(instance, cmd, DID_OK);
1170
1171 consumer++;
1172 if (consumer == (instance->max_fw_cmds + 1)) {
1173 consumer = 0;
1174 }
1175 }
1176
1177 *instance->consumer = producer;
1178
1179 spin_unlock_irqrestore(&instance->completion_lock, flags);
1180
1181 /*
1182 * Check if we can restore can_queue
1183 */
1184 if (instance->flag & MEGASAS_FW_BUSY
1185 && time_after(jiffies, instance->last_time + 5 * HZ)
1186 && atomic_read(&instance->fw_outstanding) < 17) {
1187
1188 spin_lock_irqsave(instance->host->host_lock, flags);
1189 instance->flag &= ~MEGASAS_FW_BUSY;
1190 instance->host->can_queue =
1191 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1192
1193 spin_unlock_irqrestore(instance->host->host_lock, flags);
1194 }
1195}
1196
1197/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001198 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1199 * @instance: Adapter soft state
1200 *
1201 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
1202 * complete all its outstanding commands. Returns error if one or more IOs
1203 * are pending after this time period. It also marks the controller dead.
1204 */
1205static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1206{
1207 int i;
1208 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
1209
1210 for (i = 0; i < wait_time; i++) {
1211
Sumant Patroe4a082c2006-05-30 12:03:37 -07001212 int outstanding = atomic_read(&instance->fw_outstanding);
1213
1214 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001215 break;
1216
1217 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1218 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07001219 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05001220 /*
1221 * Call cmd completion routine. Cmd to be
1222 * be completed directly without depending on isr.
1223 */
1224 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001225 }
1226
1227 msleep(1000);
1228 }
1229
Sumant Patroe4a082c2006-05-30 12:03:37 -07001230 if (atomic_read(&instance->fw_outstanding)) {
Sumant Patroe3bbff92006-10-03 12:28:49 -07001231 /*
1232 * Send signal to FW to stop processing any pending cmds.
1233 * The controller will be taken offline by the OS now.
1234 */
1235 writel(MFI_STOP_ADP,
1236 &instance->reg_set->inbound_doorbell);
Sumant Patro658dced2006-10-03 13:09:14 -07001237 megasas_dump_pending_frames(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001238 instance->hw_crit_error = 1;
1239 return FAILED;
1240 }
1241
1242 return SUCCESS;
1243}
1244
1245/**
1246 * megasas_generic_reset - Generic reset routine
1247 * @scmd: Mid-layer SCSI command
1248 *
1249 * This routine implements a generic reset handler for device, bus and host
1250 * reset requests. Device, bus and host specific reset handlers can use this
1251 * function after they do their specific tasks.
1252 */
1253static int megasas_generic_reset(struct scsi_cmnd *scmd)
1254{
1255 int ret_val;
1256 struct megasas_instance *instance;
1257
1258 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1259
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001260 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
1261 scmd->serial_number, scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001262
1263 if (instance->hw_crit_error) {
1264 printk(KERN_ERR "megasas: cannot recover from previous reset "
1265 "failures\n");
1266 return FAILED;
1267 }
1268
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001269 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001270 if (ret_val == SUCCESS)
1271 printk(KERN_NOTICE "megasas: reset successful \n");
1272 else
1273 printk(KERN_ERR "megasas: failed to do reset\n");
1274
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001275 return ret_val;
1276}
1277
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001278/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001279 * megasas_reset_timer - quiesce the adapter if required
1280 * @scmd: scsi cmnd
1281 *
1282 * Sets the FW busy flag and reduces the host->can_queue if the
1283 * cmd has not been completed within the timeout period.
1284 */
1285static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07001286blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001287{
1288 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
1289 struct megasas_instance *instance;
1290 unsigned long flags;
1291
1292 if (time_after(jiffies, scmd->jiffies_at_alloc +
1293 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001294 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001295 }
1296
1297 instance = cmd->instance;
1298 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1299 /* FW is busy, throttle IO */
1300 spin_lock_irqsave(instance->host->host_lock, flags);
1301
1302 instance->host->can_queue = 16;
1303 instance->last_time = jiffies;
1304 instance->flag |= MEGASAS_FW_BUSY;
1305
1306 spin_unlock_irqrestore(instance->host->host_lock, flags);
1307 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07001308 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001309}
1310
1311/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001312 * megasas_reset_device - Device reset handler entry point
1313 */
1314static int megasas_reset_device(struct scsi_cmnd *scmd)
1315{
1316 int ret;
1317
1318 /*
1319 * First wait for all commands to complete
1320 */
1321 ret = megasas_generic_reset(scmd);
1322
1323 return ret;
1324}
1325
1326/**
1327 * megasas_reset_bus_host - Bus & host reset handler entry point
1328 */
1329static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1330{
1331 int ret;
1332
1333 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01001334 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001335 */
1336 ret = megasas_generic_reset(scmd);
1337
1338 return ret;
1339}
1340
1341/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001342 * megasas_bios_param - Returns disk geometry for a disk
1343 * @sdev: device handle
1344 * @bdev: block device
1345 * @capacity: drive capacity
1346 * @geom: geometry parameters
1347 */
1348static int
1349megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1350 sector_t capacity, int geom[])
1351{
1352 int heads;
1353 int sectors;
1354 sector_t cylinders;
1355 unsigned long tmp;
1356 /* Default heads (64) & sectors (32) */
1357 heads = 64;
1358 sectors = 32;
1359
1360 tmp = heads * sectors;
1361 cylinders = capacity;
1362
1363 sector_div(cylinders, tmp);
1364
1365 /*
1366 * Handle extended translation size for logical drives > 1Gb
1367 */
1368
1369 if (capacity >= 0x200000) {
1370 heads = 255;
1371 sectors = 63;
1372 tmp = heads*sectors;
1373 cylinders = capacity;
1374 sector_div(cylinders, tmp);
1375 }
1376
1377 geom[0] = heads;
1378 geom[1] = sectors;
1379 geom[2] = cylinders;
1380
1381 return 0;
1382}
1383
1384/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001385 * megasas_service_aen - Processes an event notification
1386 * @instance: Adapter soft state
1387 * @cmd: AEN command completed by the ISR
1388 *
1389 * For AEN, driver sends a command down to FW that is held by the FW till an
1390 * event occurs. When an event of interest occurs, FW completes the command
1391 * that it was previously holding.
1392 *
1393 * This routines sends SIGIO signal to processes that have registered with the
1394 * driver for AEN.
1395 */
1396static void
1397megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1398{
Yang, Boc3518832009-10-06 14:18:02 -06001399 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001400 /*
1401 * Don't signal app if it is just an aborted previously registered aen
1402 */
Yang, Boc3518832009-10-06 14:18:02 -06001403 if ((!cmd->abort_aen) && (instance->unload == 0)) {
1404 spin_lock_irqsave(&poll_aen_lock, flags);
1405 megasas_poll_wait_aen = 1;
1406 spin_unlock_irqrestore(&poll_aen_lock, flags);
1407 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001408 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06001409 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001410 else
1411 cmd->abort_aen = 0;
1412
1413 instance->aen_cmd = NULL;
1414 megasas_return_cmd(instance, cmd);
1415}
1416
1417/*
1418 * Scsi host template for megaraid_sas driver
1419 */
1420static struct scsi_host_template megasas_template = {
1421
1422 .module = THIS_MODULE,
bo yangf28cd7c2007-11-09 04:44:56 -05001423 .name = "LSI SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001424 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01001425 .slave_configure = megasas_slave_configure,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001426 .queuecommand = megasas_queue_command,
1427 .eh_device_reset_handler = megasas_reset_device,
1428 .eh_bus_reset_handler = megasas_reset_bus_host,
1429 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001430 .eh_timed_out = megasas_reset_timer,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001431 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001432 .use_clustering = ENABLE_CLUSTERING,
1433};
1434
1435/**
1436 * megasas_complete_int_cmd - Completes an internal command
1437 * @instance: Adapter soft state
1438 * @cmd: Command to be completed
1439 *
1440 * The megasas_issue_blocked_cmd() function waits for a command to complete
1441 * after it issues a command. This function wakes up that waiting routine by
1442 * calling wake_up() on the wait queue.
1443 */
1444static void
1445megasas_complete_int_cmd(struct megasas_instance *instance,
1446 struct megasas_cmd *cmd)
1447{
1448 cmd->cmd_status = cmd->frame->io.cmd_status;
1449
1450 if (cmd->cmd_status == ENODATA) {
1451 cmd->cmd_status = 0;
1452 }
1453 wake_up(&instance->int_cmd_wait_q);
1454}
1455
1456/**
1457 * megasas_complete_abort - Completes aborting a command
1458 * @instance: Adapter soft state
1459 * @cmd: Cmd that was issued to abort another cmd
1460 *
1461 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
1462 * after it issues an abort on a previously issued command. This function
1463 * wakes up all functions waiting on the same wait queue.
1464 */
1465static void
1466megasas_complete_abort(struct megasas_instance *instance,
1467 struct megasas_cmd *cmd)
1468{
1469 if (cmd->sync_cmd) {
1470 cmd->sync_cmd = 0;
1471 cmd->cmd_status = 0;
1472 wake_up(&instance->abort_cmd_wait_q);
1473 }
1474
1475 return;
1476}
1477
1478/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001479 * megasas_complete_cmd - Completes a command
1480 * @instance: Adapter soft state
1481 * @cmd: Command to be completed
1482 * @alt_status: If non-zero, use this value as status to
1483 * SCSI mid-layer instead of the value returned
1484 * by the FW. This should be used if caller wants
1485 * an alternate status (as in the case of aborted
1486 * commands)
1487 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001488static void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001489megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
1490 u8 alt_status)
1491{
1492 int exception = 0;
1493 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06001494 unsigned long flags;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001495
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001496 if (cmd->scmd)
1497 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001498
1499 switch (hdr->cmd) {
1500
1501 case MFI_CMD_PD_SCSI_IO:
1502 case MFI_CMD_LD_SCSI_IO:
1503
1504 /*
1505 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1506 * issued either through an IO path or an IOCTL path. If it
1507 * was via IOCTL, we will send it to internal completion.
1508 */
1509 if (cmd->sync_cmd) {
1510 cmd->sync_cmd = 0;
1511 megasas_complete_int_cmd(instance, cmd);
1512 break;
1513 }
1514
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001515 case MFI_CMD_LD_READ:
1516 case MFI_CMD_LD_WRITE:
1517
1518 if (alt_status) {
1519 cmd->scmd->result = alt_status << 16;
1520 exception = 1;
1521 }
1522
1523 if (exception) {
1524
Sumant Patroe4a082c2006-05-30 12:03:37 -07001525 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001526
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001527 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001528 cmd->scmd->scsi_done(cmd->scmd);
1529 megasas_return_cmd(instance, cmd);
1530
1531 break;
1532 }
1533
1534 switch (hdr->cmd_status) {
1535
1536 case MFI_STAT_OK:
1537 cmd->scmd->result = DID_OK << 16;
1538 break;
1539
1540 case MFI_STAT_SCSI_IO_FAILED:
1541 case MFI_STAT_LD_INIT_IN_PROGRESS:
1542 cmd->scmd->result =
1543 (DID_ERROR << 16) | hdr->scsi_status;
1544 break;
1545
1546 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1547
1548 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1549
1550 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1551 memset(cmd->scmd->sense_buffer, 0,
1552 SCSI_SENSE_BUFFERSIZE);
1553 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1554 hdr->sense_len);
1555
1556 cmd->scmd->result |= DRIVER_SENSE << 24;
1557 }
1558
1559 break;
1560
1561 case MFI_STAT_LD_OFFLINE:
1562 case MFI_STAT_DEVICE_NOT_FOUND:
1563 cmd->scmd->result = DID_BAD_TARGET << 16;
1564 break;
1565
1566 default:
1567 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1568 hdr->cmd_status);
1569 cmd->scmd->result = DID_ERROR << 16;
1570 break;
1571 }
1572
Sumant Patroe4a082c2006-05-30 12:03:37 -07001573 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001574
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001575 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001576 cmd->scmd->scsi_done(cmd->scmd);
1577 megasas_return_cmd(instance, cmd);
1578
1579 break;
1580
1581 case MFI_CMD_SMP:
1582 case MFI_CMD_STP:
1583 case MFI_CMD_DCMD:
Yang, Boc3518832009-10-06 14:18:02 -06001584 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
1585 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
1586 spin_lock_irqsave(&poll_aen_lock, flags);
1587 megasas_poll_wait_aen = 0;
1588 spin_unlock_irqrestore(&poll_aen_lock, flags);
1589 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001590
1591 /*
1592 * See if got an event notification
1593 */
1594 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1595 megasas_service_aen(instance, cmd);
1596 else
1597 megasas_complete_int_cmd(instance, cmd);
1598
1599 break;
1600
1601 case MFI_CMD_ABORT:
1602 /*
1603 * Cmd issued to abort another cmd returned
1604 */
1605 megasas_complete_abort(instance, cmd);
1606 break;
1607
1608 default:
1609 printk("megasas: Unknown command completed! [0x%X]\n",
1610 hdr->cmd);
1611 break;
1612 }
1613}
1614
1615/**
1616 * megasas_deplete_reply_queue - Processes all completed commands
1617 * @instance: Adapter soft state
1618 * @alt_status: Alternate status to be returned to
1619 * SCSI mid-layer instead of the status
1620 * returned by the FW
1621 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001622static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001623megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1624{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001625 /*
1626 * Check if it is our interrupt
Sumant Patro1341c932006-01-25 12:02:40 -08001627 * Clear the interrupt
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001628 */
Sumant Patro1341c932006-01-25 12:02:40 -08001629 if(instance->instancet->clear_intr(instance->reg_set))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001630 return IRQ_NONE;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001631
Sumant Patroaf37acf2007-02-14 12:34:46 -08001632 if (instance->hw_crit_error)
1633 goto out_done;
Sumant Patro5d018ad2006-10-03 13:13:18 -07001634 /*
1635 * Schedule the tasklet for cmd completion
1636 */
1637 tasklet_schedule(&instance->isr_tasklet);
Sumant Patroaf37acf2007-02-14 12:34:46 -08001638out_done:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001639 return IRQ_HANDLED;
1640}
1641
1642/**
1643 * megasas_isr - isr entry point
1644 */
David Howells7d12e782006-10-05 14:55:46 +01001645static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001646{
1647 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1648 DID_OK);
1649}
1650
1651/**
1652 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08001653 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001654 *
1655 * During the initialization, FW passes can potentially be in any one of
1656 * several possible states. If the FW in operational, waiting-for-handshake
1657 * states, driver must take steps to bring it to ready state. Otherwise, it
1658 * has to wait for the ready state.
1659 */
1660static int
Sumant Patro1341c932006-01-25 12:02:40 -08001661megasas_transition_to_ready(struct megasas_instance* instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001662{
1663 int i;
1664 u8 max_wait;
1665 u32 fw_state;
1666 u32 cur_state;
1667
Sumant Patro1341c932006-01-25 12:02:40 -08001668 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001669
Sumant Patroe3bbff92006-10-03 12:28:49 -07001670 if (fw_state != MFI_STATE_READY)
1671 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1672 " state\n");
1673
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001674 while (fw_state != MFI_STATE_READY) {
1675
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001676 switch (fw_state) {
1677
1678 case MFI_STATE_FAULT:
1679
1680 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1681 return -ENODEV;
1682
1683 case MFI_STATE_WAIT_HANDSHAKE:
1684 /*
1685 * Set the CLR bit in inbound doorbell
1686 */
Yang, Bo87911122009-10-06 14:31:54 -06001687 if ((instance->pdev->device == \
1688 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1689 (instance->pdev->device ==
1690 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1691
1692 writel(
1693 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
1694 &instance->reg_set->reserved_0[0]);
1695 } else {
1696 writel(
1697 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
1698 &instance->reg_set->inbound_doorbell);
1699 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001700
1701 max_wait = 2;
1702 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1703 break;
1704
Sumant Patroe3bbff92006-10-03 12:28:49 -07001705 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06001706 if ((instance->pdev->device ==
1707 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1708 (instance->pdev->device ==
1709 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1710 writel(MFI_INIT_HOTPLUG,
1711 &instance->reg_set->reserved_0[0]);
1712 } else
1713 writel(MFI_INIT_HOTPLUG,
1714 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07001715
1716 max_wait = 10;
1717 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
1718 break;
1719
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001720 case MFI_STATE_OPERATIONAL:
1721 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07001722 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001723 */
Sumant Patrob274cab2006-10-03 12:52:12 -07001724 instance->instancet->disable_intr(instance->reg_set);
Yang, Bo87911122009-10-06 14:31:54 -06001725 if ((instance->pdev->device ==
1726 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1727 (instance->pdev->device ==
1728 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1729 writel(MFI_RESET_FLAGS,
1730 &instance->reg_set->reserved_0[0]);
1731 } else
1732 writel(MFI_RESET_FLAGS,
1733 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001734
bo yangaf7a5642008-03-17 04:13:07 -04001735 max_wait = 60;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001736 cur_state = MFI_STATE_OPERATIONAL;
1737 break;
1738
1739 case MFI_STATE_UNDEFINED:
1740 /*
1741 * This state should not last for more than 2 seconds
1742 */
1743 max_wait = 2;
1744 cur_state = MFI_STATE_UNDEFINED;
1745 break;
1746
1747 case MFI_STATE_BB_INIT:
1748 max_wait = 2;
1749 cur_state = MFI_STATE_BB_INIT;
1750 break;
1751
1752 case MFI_STATE_FW_INIT:
1753 max_wait = 20;
1754 cur_state = MFI_STATE_FW_INIT;
1755 break;
1756
1757 case MFI_STATE_FW_INIT_2:
1758 max_wait = 20;
1759 cur_state = MFI_STATE_FW_INIT_2;
1760 break;
1761
1762 case MFI_STATE_DEVICE_SCAN:
1763 max_wait = 20;
1764 cur_state = MFI_STATE_DEVICE_SCAN;
1765 break;
1766
1767 case MFI_STATE_FLUSH_CACHE:
1768 max_wait = 20;
1769 cur_state = MFI_STATE_FLUSH_CACHE;
1770 break;
1771
1772 default:
1773 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1774 fw_state);
1775 return -ENODEV;
1776 }
1777
1778 /*
1779 * The cur_state should not last for more than max_wait secs
1780 */
1781 for (i = 0; i < (max_wait * 1000); i++) {
Sumant Patro1341c932006-01-25 12:02:40 -08001782 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1783 MFI_STATE_MASK ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001784
1785 if (fw_state == cur_state) {
1786 msleep(1);
1787 } else
1788 break;
1789 }
1790
1791 /*
1792 * Return error if fw_state hasn't changed after max_wait
1793 */
1794 if (fw_state == cur_state) {
1795 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1796 "in %d secs\n", fw_state, max_wait);
1797 return -ENODEV;
1798 }
1799 };
Sumant Patroe3bbff92006-10-03 12:28:49 -07001800 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001801
1802 return 0;
1803}
1804
1805/**
1806 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1807 * @instance: Adapter soft state
1808 */
1809static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1810{
1811 int i;
1812 u32 max_cmd = instance->max_fw_cmds;
1813 struct megasas_cmd *cmd;
1814
1815 if (!instance->frame_dma_pool)
1816 return;
1817
1818 /*
1819 * Return all frames to pool
1820 */
1821 for (i = 0; i < max_cmd; i++) {
1822
1823 cmd = instance->cmd_list[i];
1824
1825 if (cmd->frame)
1826 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1827 cmd->frame_phys_addr);
1828
1829 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07001830 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001831 cmd->sense_phys_addr);
1832 }
1833
1834 /*
1835 * Now destroy the pool itself
1836 */
1837 pci_pool_destroy(instance->frame_dma_pool);
1838 pci_pool_destroy(instance->sense_dma_pool);
1839
1840 instance->frame_dma_pool = NULL;
1841 instance->sense_dma_pool = NULL;
1842}
1843
1844/**
1845 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1846 * @instance: Adapter soft state
1847 *
1848 * Each command packet has an embedded DMA memory buffer that is used for
1849 * filling MFI frame and the SG list that immediately follows the frame. This
1850 * function creates those DMA memory buffers for each command packet by using
1851 * PCI pool facility.
1852 */
1853static int megasas_create_frame_pool(struct megasas_instance *instance)
1854{
1855 int i;
1856 u32 max_cmd;
1857 u32 sge_sz;
1858 u32 sgl_sz;
1859 u32 total_sz;
1860 u32 frame_count;
1861 struct megasas_cmd *cmd;
1862
1863 max_cmd = instance->max_fw_cmds;
1864
1865 /*
1866 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1867 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1868 */
1869 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1870 sizeof(struct megasas_sge32);
1871
1872 /*
1873 * Calculated the number of 64byte frames required for SGL
1874 */
1875 sgl_sz = sge_sz * instance->max_num_sge;
1876 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1877
1878 /*
1879 * We need one extra frame for the MFI command
1880 */
1881 frame_count++;
1882
1883 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1884 /*
1885 * Use DMA pool facility provided by PCI layer
1886 */
1887 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1888 instance->pdev, total_sz, 64,
1889 0);
1890
1891 if (!instance->frame_dma_pool) {
1892 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1893 return -ENOMEM;
1894 }
1895
1896 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1897 instance->pdev, 128, 4, 0);
1898
1899 if (!instance->sense_dma_pool) {
1900 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1901
1902 pci_pool_destroy(instance->frame_dma_pool);
1903 instance->frame_dma_pool = NULL;
1904
1905 return -ENOMEM;
1906 }
1907
1908 /*
1909 * Allocate and attach a frame to each of the commands in cmd_list.
1910 * By making cmd->index as the context instead of the &cmd, we can
1911 * always use 32bit context regardless of the architecture
1912 */
1913 for (i = 0; i < max_cmd; i++) {
1914
1915 cmd = instance->cmd_list[i];
1916
1917 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1918 GFP_KERNEL, &cmd->frame_phys_addr);
1919
1920 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1921 GFP_KERNEL, &cmd->sense_phys_addr);
1922
1923 /*
1924 * megasas_teardown_frame_pool() takes care of freeing
1925 * whatever has been allocated
1926 */
1927 if (!cmd->frame || !cmd->sense) {
1928 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1929 megasas_teardown_frame_pool(instance);
1930 return -ENOMEM;
1931 }
1932
1933 cmd->frame->io.context = cmd->index;
1934 }
1935
1936 return 0;
1937}
1938
1939/**
1940 * megasas_free_cmds - Free all the cmds in the free cmd pool
1941 * @instance: Adapter soft state
1942 */
1943static void megasas_free_cmds(struct megasas_instance *instance)
1944{
1945 int i;
1946 /* First free the MFI frame pool */
1947 megasas_teardown_frame_pool(instance);
1948
1949 /* Free all the commands in the cmd_list */
1950 for (i = 0; i < instance->max_fw_cmds; i++)
1951 kfree(instance->cmd_list[i]);
1952
1953 /* Free the cmd_list buffer itself */
1954 kfree(instance->cmd_list);
1955 instance->cmd_list = NULL;
1956
1957 INIT_LIST_HEAD(&instance->cmd_pool);
1958}
1959
1960/**
1961 * megasas_alloc_cmds - Allocates the command packets
1962 * @instance: Adapter soft state
1963 *
1964 * Each command that is issued to the FW, whether IO commands from the OS or
1965 * internal commands like IOCTLs, are wrapped in local data structure called
1966 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1967 * the FW.
1968 *
1969 * Each frame has a 32-bit field called context (tag). This context is used
1970 * to get back the megasas_cmd from the frame when a frame gets completed in
1971 * the ISR. Typically the address of the megasas_cmd itself would be used as
1972 * the context. But we wanted to keep the differences between 32 and 64 bit
1973 * systems to the mininum. We always use 32 bit integers for the context. In
1974 * this driver, the 32 bit values are the indices into an array cmd_list.
1975 * This array is used only to look up the megasas_cmd given the context. The
1976 * free commands themselves are maintained in a linked list called cmd_pool.
1977 */
1978static int megasas_alloc_cmds(struct megasas_instance *instance)
1979{
1980 int i;
1981 int j;
1982 u32 max_cmd;
1983 struct megasas_cmd *cmd;
1984
1985 max_cmd = instance->max_fw_cmds;
1986
1987 /*
1988 * instance->cmd_list is an array of struct megasas_cmd pointers.
1989 * Allocate the dynamic array first and then allocate individual
1990 * commands.
1991 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001992 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001993
1994 if (!instance->cmd_list) {
1995 printk(KERN_DEBUG "megasas: out of memory\n");
1996 return -ENOMEM;
1997 }
1998
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001999
2000 for (i = 0; i < max_cmd; i++) {
2001 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
2002 GFP_KERNEL);
2003
2004 if (!instance->cmd_list[i]) {
2005
2006 for (j = 0; j < i; j++)
2007 kfree(instance->cmd_list[j]);
2008
2009 kfree(instance->cmd_list);
2010 instance->cmd_list = NULL;
2011
2012 return -ENOMEM;
2013 }
2014 }
2015
2016 /*
2017 * Add all the commands to command pool (instance->cmd_pool)
2018 */
2019 for (i = 0; i < max_cmd; i++) {
2020 cmd = instance->cmd_list[i];
2021 memset(cmd, 0, sizeof(struct megasas_cmd));
2022 cmd->index = i;
2023 cmd->instance = instance;
2024
2025 list_add_tail(&cmd->list, &instance->cmd_pool);
2026 }
2027
2028 /*
2029 * Create a frame pool and assign one frame to each cmd
2030 */
2031 if (megasas_create_frame_pool(instance)) {
2032 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
2033 megasas_free_cmds(instance);
2034 }
2035
2036 return 0;
2037}
2038
2039/**
2040 * megasas_get_controller_info - Returns FW's controller structure
2041 * @instance: Adapter soft state
2042 * @ctrl_info: Controller information structure
2043 *
2044 * Issues an internal command (DCMD) to get the FW's controller structure.
2045 * This information is mainly used to find out the maximum IO transfer per
2046 * command supported by the FW.
2047 */
2048static int
2049megasas_get_ctrl_info(struct megasas_instance *instance,
2050 struct megasas_ctrl_info *ctrl_info)
2051{
2052 int ret = 0;
2053 struct megasas_cmd *cmd;
2054 struct megasas_dcmd_frame *dcmd;
2055 struct megasas_ctrl_info *ci;
2056 dma_addr_t ci_h = 0;
2057
2058 cmd = megasas_get_cmd(instance);
2059
2060 if (!cmd) {
2061 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
2062 return -ENOMEM;
2063 }
2064
2065 dcmd = &cmd->frame->dcmd;
2066
2067 ci = pci_alloc_consistent(instance->pdev,
2068 sizeof(struct megasas_ctrl_info), &ci_h);
2069
2070 if (!ci) {
2071 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
2072 megasas_return_cmd(instance, cmd);
2073 return -ENOMEM;
2074 }
2075
2076 memset(ci, 0, sizeof(*ci));
2077 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2078
2079 dcmd->cmd = MFI_CMD_DCMD;
2080 dcmd->cmd_status = 0xFF;
2081 dcmd->sge_count = 1;
2082 dcmd->flags = MFI_FRAME_DIR_READ;
2083 dcmd->timeout = 0;
2084 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
2085 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
2086 dcmd->sgl.sge32[0].phys_addr = ci_h;
2087 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
2088
2089 if (!megasas_issue_polled(instance, cmd)) {
2090 ret = 0;
2091 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
2092 } else {
2093 ret = -1;
2094 }
2095
2096 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
2097 ci, ci_h);
2098
2099 megasas_return_cmd(instance, cmd);
2100 return ret;
2101}
2102
2103/**
bo yang31ea7082007-11-07 12:09:50 -05002104 * megasas_issue_init_mfi - Initializes the FW
2105 * @instance: Adapter soft state
2106 *
2107 * Issues the INIT MFI cmd
2108 */
2109static int
2110megasas_issue_init_mfi(struct megasas_instance *instance)
2111{
2112 u32 context;
2113
2114 struct megasas_cmd *cmd;
2115
2116 struct megasas_init_frame *init_frame;
2117 struct megasas_init_queue_info *initq_info;
2118 dma_addr_t init_frame_h;
2119 dma_addr_t initq_info_h;
2120
2121 /*
2122 * Prepare a init frame. Note the init frame points to queue info
2123 * structure. Each frame has SGL allocated after first 64 bytes. For
2124 * this frame - since we don't need any SGL - we use SGL's space as
2125 * queue info structure
2126 *
2127 * We will not get a NULL command below. We just created the pool.
2128 */
2129 cmd = megasas_get_cmd(instance);
2130
2131 init_frame = (struct megasas_init_frame *)cmd->frame;
2132 initq_info = (struct megasas_init_queue_info *)
2133 ((unsigned long)init_frame + 64);
2134
2135 init_frame_h = cmd->frame_phys_addr;
2136 initq_info_h = init_frame_h + 64;
2137
2138 context = init_frame->context;
2139 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
2140 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
2141 init_frame->context = context;
2142
2143 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
2144 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
2145
2146 initq_info->producer_index_phys_addr_lo = instance->producer_h;
2147 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
2148
2149 init_frame->cmd = MFI_CMD_INIT;
2150 init_frame->cmd_status = 0xFF;
2151 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
2152
2153 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
2154
2155 /*
2156 * disable the intr before firing the init frame to FW
2157 */
2158 instance->instancet->disable_intr(instance->reg_set);
2159
2160 /*
2161 * Issue the init frame in polled mode
2162 */
2163
2164 if (megasas_issue_polled(instance, cmd)) {
2165 printk(KERN_ERR "megasas: Failed to init firmware\n");
2166 megasas_return_cmd(instance, cmd);
2167 goto fail_fw_init;
2168 }
2169
2170 megasas_return_cmd(instance, cmd);
2171
2172 return 0;
2173
2174fail_fw_init:
2175 return -EINVAL;
2176}
2177
2178/**
bo yangad84db22007-11-09 04:40:16 -05002179 * megasas_start_timer - Initializes a timer object
2180 * @instance: Adapter soft state
2181 * @timer: timer object to be initialized
2182 * @fn: timer function
2183 * @interval: time interval between timer function call
2184 */
2185static inline void
2186megasas_start_timer(struct megasas_instance *instance,
2187 struct timer_list *timer,
2188 void *fn, unsigned long interval)
2189{
2190 init_timer(timer);
2191 timer->expires = jiffies + interval;
2192 timer->data = (unsigned long)instance;
2193 timer->function = fn;
2194 add_timer(timer);
2195}
2196
2197/**
2198 * megasas_io_completion_timer - Timer fn
2199 * @instance_addr: Address of adapter soft state
2200 *
2201 * Schedules tasklet for cmd completion
2202 * if poll_mode_io is set
2203 */
2204static void
2205megasas_io_completion_timer(unsigned long instance_addr)
2206{
2207 struct megasas_instance *instance =
2208 (struct megasas_instance *)instance_addr;
2209
2210 if (atomic_read(&instance->fw_outstanding))
2211 tasklet_schedule(&instance->isr_tasklet);
2212
2213 /* Restart timer */
2214 if (poll_mode_io)
2215 mod_timer(&instance->io_completion_timer,
2216 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
2217}
2218
2219/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002220 * megasas_init_mfi - Initializes the FW
2221 * @instance: Adapter soft state
2222 *
2223 * This is the main function for initializing MFI firmware.
2224 */
2225static int megasas_init_mfi(struct megasas_instance *instance)
2226{
2227 u32 context_sz;
2228 u32 reply_q_sz;
2229 u32 max_sectors_1;
2230 u32 max_sectors_2;
bo yang14faea92007-11-09 04:14:00 -05002231 u32 tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002232 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002233 struct megasas_ctrl_info *ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002234 /*
2235 * Map the message registers
2236 */
Yang, Bo6610a6b2008-08-10 12:42:38 -07002237 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
Yang, Bo87911122009-10-06 14:31:54 -06002238 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2239 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Yang, Bo6610a6b2008-08-10 12:42:38 -07002240 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0079GEN2)) {
2241 instance->base_addr = pci_resource_start(instance->pdev, 1);
2242 } else {
2243 instance->base_addr = pci_resource_start(instance->pdev, 0);
2244 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002245
bo yangf28cd7c2007-11-09 04:44:56 -05002246 if (pci_request_regions(instance->pdev, "megasas: LSI")) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002247 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
2248 return -EBUSY;
2249 }
2250
2251 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
2252
2253 if (!instance->reg_set) {
2254 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
2255 goto fail_ioremap;
2256 }
2257
2258 reg_set = instance->reg_set;
2259
Sumant Patrof9876f02006-02-03 15:34:35 -08002260 switch(instance->pdev->device)
2261 {
bo yangaf7a5642008-03-17 04:13:07 -04002262 case PCI_DEVICE_ID_LSI_SAS1078R:
2263 case PCI_DEVICE_ID_LSI_SAS1078DE:
Sumant Patrof9876f02006-02-03 15:34:35 -08002264 instance->instancet = &megasas_instance_template_ppc;
2265 break;
Yang, Bo6610a6b2008-08-10 12:42:38 -07002266 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
2267 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
2268 instance->instancet = &megasas_instance_template_gen2;
2269 break;
Yang, Bo87911122009-10-06 14:31:54 -06002270 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
2271 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
2272 instance->instancet = &megasas_instance_template_skinny;
2273 break;
Sumant Patrof9876f02006-02-03 15:34:35 -08002274 case PCI_DEVICE_ID_LSI_SAS1064R:
2275 case PCI_DEVICE_ID_DELL_PERC5:
2276 default:
2277 instance->instancet = &megasas_instance_template_xscale;
2278 break;
2279 }
Sumant Patro1341c932006-01-25 12:02:40 -08002280
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002281 /*
2282 * We expect the FW state to be READY
2283 */
Sumant Patro1341c932006-01-25 12:02:40 -08002284 if (megasas_transition_to_ready(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002285 goto fail_ready_state;
2286
2287 /*
2288 * Get various operational parameters from status register
2289 */
Sumant Patro1341c932006-01-25 12:02:40 -08002290 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07002291 /*
2292 * Reduce the max supported cmds by 1. This is to ensure that the
2293 * reply_q_sz (1 more than the max cmd that driver may send)
2294 * does not exceed max cmds that the FW can support
2295 */
2296 instance->max_fw_cmds = instance->max_fw_cmds-1;
Sumant Patro1341c932006-01-25 12:02:40 -08002297 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
2298 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002299 /*
2300 * Create a pool of commands
2301 */
2302 if (megasas_alloc_cmds(instance))
2303 goto fail_alloc_cmds;
2304
2305 /*
2306 * Allocate memory for reply queue. Length of reply queue should
2307 * be _one_ more than the maximum commands handled by the firmware.
2308 *
2309 * Note: When FW completes commands, it places corresponding contex
2310 * values in this circular reply queue. This circular queue is a fairly
2311 * typical producer-consumer queue. FW is the producer (of completed
2312 * commands) and the driver is the consumer.
2313 */
2314 context_sz = sizeof(u32);
2315 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
2316
2317 instance->reply_queue = pci_alloc_consistent(instance->pdev,
2318 reply_q_sz,
2319 &instance->reply_queue_h);
2320
2321 if (!instance->reply_queue) {
2322 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
2323 goto fail_reply_queue;
2324 }
2325
bo yang31ea7082007-11-07 12:09:50 -05002326 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002327 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002328
2329 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
2330
2331 /*
2332 * Compute the max allowed sectors per IO: The controller info has two
2333 * limits on max sectors. Driver should use the minimum of these two.
2334 *
2335 * 1 << stripe_sz_ops.min = max sectors per strip
2336 *
2337 * Note that older firmwares ( < FW ver 30) didn't report information
2338 * to calculate max_sectors_1. So the number ended up as zero always.
2339 */
bo yang14faea92007-11-09 04:14:00 -05002340 tmp_sectors = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002341 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
2342
2343 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
2344 ctrl_info->max_strips_per_io;
2345 max_sectors_2 = ctrl_info->max_request_size;
2346
bo yang14faea92007-11-09 04:14:00 -05002347 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
2348 }
2349
2350 instance->max_sectors_per_req = instance->max_num_sge *
2351 PAGE_SIZE / 512;
2352 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
2353 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002354
2355 kfree(ctrl_info);
2356
Sumant Patro5d018ad2006-10-03 13:13:18 -07002357 /*
2358 * Setup tasklet for cmd completion
2359 */
2360
bo yangad84db22007-11-09 04:40:16 -05002361 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2362 (unsigned long)instance);
2363
2364 /* Initialize the cmd completion timer */
2365 if (poll_mode_io)
2366 megasas_start_timer(instance, &instance->io_completion_timer,
2367 megasas_io_completion_timer,
2368 MEGASAS_COMPLETION_TIMER_INTERVAL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002369 return 0;
2370
2371 fail_fw_init:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002372
2373 pci_free_consistent(instance->pdev, reply_q_sz,
2374 instance->reply_queue, instance->reply_queue_h);
2375 fail_reply_queue:
2376 megasas_free_cmds(instance);
2377
2378 fail_alloc_cmds:
2379 fail_ready_state:
2380 iounmap(instance->reg_set);
2381
2382 fail_ioremap:
2383 pci_release_regions(instance->pdev);
2384
2385 return -EINVAL;
2386}
2387
2388/**
2389 * megasas_release_mfi - Reverses the FW initialization
2390 * @intance: Adapter soft state
2391 */
2392static void megasas_release_mfi(struct megasas_instance *instance)
2393{
2394 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
2395
2396 pci_free_consistent(instance->pdev, reply_q_sz,
2397 instance->reply_queue, instance->reply_queue_h);
2398
2399 megasas_free_cmds(instance);
2400
2401 iounmap(instance->reg_set);
2402
2403 pci_release_regions(instance->pdev);
2404}
2405
2406/**
2407 * megasas_get_seq_num - Gets latest event sequence numbers
2408 * @instance: Adapter soft state
2409 * @eli: FW event log sequence numbers information
2410 *
2411 * FW maintains a log of all events in a non-volatile area. Upper layers would
2412 * usually find out the latest sequence number of the events, the seq number at
2413 * the boot etc. They would "read" all the events below the latest seq number
2414 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
2415 * number), they would subsribe to AEN (asynchronous event notification) and
2416 * wait for the events to happen.
2417 */
2418static int
2419megasas_get_seq_num(struct megasas_instance *instance,
2420 struct megasas_evt_log_info *eli)
2421{
2422 struct megasas_cmd *cmd;
2423 struct megasas_dcmd_frame *dcmd;
2424 struct megasas_evt_log_info *el_info;
2425 dma_addr_t el_info_h = 0;
2426
2427 cmd = megasas_get_cmd(instance);
2428
2429 if (!cmd) {
2430 return -ENOMEM;
2431 }
2432
2433 dcmd = &cmd->frame->dcmd;
2434 el_info = pci_alloc_consistent(instance->pdev,
2435 sizeof(struct megasas_evt_log_info),
2436 &el_info_h);
2437
2438 if (!el_info) {
2439 megasas_return_cmd(instance, cmd);
2440 return -ENOMEM;
2441 }
2442
2443 memset(el_info, 0, sizeof(*el_info));
2444 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2445
2446 dcmd->cmd = MFI_CMD_DCMD;
2447 dcmd->cmd_status = 0x0;
2448 dcmd->sge_count = 1;
2449 dcmd->flags = MFI_FRAME_DIR_READ;
2450 dcmd->timeout = 0;
2451 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
2452 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
2453 dcmd->sgl.sge32[0].phys_addr = el_info_h;
2454 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
2455
2456 megasas_issue_blocked_cmd(instance, cmd);
2457
2458 /*
2459 * Copy the data back into callers buffer
2460 */
2461 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
2462
2463 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
2464 el_info, el_info_h);
2465
2466 megasas_return_cmd(instance, cmd);
2467
2468 return 0;
2469}
2470
2471/**
2472 * megasas_register_aen - Registers for asynchronous event notification
2473 * @instance: Adapter soft state
2474 * @seq_num: The starting sequence number
2475 * @class_locale: Class of the event
2476 *
2477 * This function subscribes for AEN for events beyond the @seq_num. It requests
2478 * to be notified if and only if the event is of type @class_locale
2479 */
2480static int
2481megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
2482 u32 class_locale_word)
2483{
2484 int ret_val;
2485 struct megasas_cmd *cmd;
2486 struct megasas_dcmd_frame *dcmd;
2487 union megasas_evt_class_locale curr_aen;
2488 union megasas_evt_class_locale prev_aen;
2489
2490 /*
2491 * If there an AEN pending already (aen_cmd), check if the
2492 * class_locale of that pending AEN is inclusive of the new
2493 * AEN request we currently have. If it is, then we don't have
2494 * to do anything. In other words, whichever events the current
2495 * AEN request is subscribing to, have already been subscribed
2496 * to.
2497 *
2498 * If the old_cmd is _not_ inclusive, then we have to abort
2499 * that command, form a class_locale that is superset of both
2500 * old and current and re-issue to the FW
2501 */
2502
2503 curr_aen.word = class_locale_word;
2504
2505 if (instance->aen_cmd) {
2506
2507 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
2508
2509 /*
2510 * A class whose enum value is smaller is inclusive of all
2511 * higher values. If a PROGRESS (= -1) was previously
2512 * registered, then a new registration requests for higher
2513 * classes need not be sent to FW. They are automatically
2514 * included.
2515 *
2516 * Locale numbers don't have such hierarchy. They are bitmap
2517 * values
2518 */
2519 if ((prev_aen.members.class <= curr_aen.members.class) &&
2520 !((prev_aen.members.locale & curr_aen.members.locale) ^
2521 curr_aen.members.locale)) {
2522 /*
2523 * Previously issued event registration includes
2524 * current request. Nothing to do.
2525 */
2526 return 0;
2527 } else {
2528 curr_aen.members.locale |= prev_aen.members.locale;
2529
2530 if (prev_aen.members.class < curr_aen.members.class)
2531 curr_aen.members.class = prev_aen.members.class;
2532
2533 instance->aen_cmd->abort_aen = 1;
2534 ret_val = megasas_issue_blocked_abort_cmd(instance,
2535 instance->
2536 aen_cmd);
2537
2538 if (ret_val) {
2539 printk(KERN_DEBUG "megasas: Failed to abort "
2540 "previous AEN command\n");
2541 return ret_val;
2542 }
2543 }
2544 }
2545
2546 cmd = megasas_get_cmd(instance);
2547
2548 if (!cmd)
2549 return -ENOMEM;
2550
2551 dcmd = &cmd->frame->dcmd;
2552
2553 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
2554
2555 /*
2556 * Prepare DCMD for aen registration
2557 */
2558 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2559
2560 dcmd->cmd = MFI_CMD_DCMD;
2561 dcmd->cmd_status = 0x0;
2562 dcmd->sge_count = 1;
2563 dcmd->flags = MFI_FRAME_DIR_READ;
2564 dcmd->timeout = 0;
2565 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
2566 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
2567 dcmd->mbox.w[0] = seq_num;
2568 dcmd->mbox.w[1] = curr_aen.word;
2569 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
2570 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
2571
2572 /*
2573 * Store reference to the cmd used to register for AEN. When an
2574 * application wants us to register for AEN, we have to abort this
2575 * cmd and re-register with a new EVENT LOCALE supplied by that app
2576 */
2577 instance->aen_cmd = cmd;
2578
2579 /*
2580 * Issue the aen registration frame
2581 */
Sumant Patro1341c932006-01-25 12:02:40 -08002582 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002583
2584 return 0;
2585}
2586
2587/**
2588 * megasas_start_aen - Subscribes to AEN during driver load time
2589 * @instance: Adapter soft state
2590 */
2591static int megasas_start_aen(struct megasas_instance *instance)
2592{
2593 struct megasas_evt_log_info eli;
2594 union megasas_evt_class_locale class_locale;
2595
2596 /*
2597 * Get the latest sequence number from FW
2598 */
2599 memset(&eli, 0, sizeof(eli));
2600
2601 if (megasas_get_seq_num(instance, &eli))
2602 return -1;
2603
2604 /*
2605 * Register AEN with FW for latest sequence number plus 1
2606 */
2607 class_locale.members.reserved = 0;
2608 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2609 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2610
2611 return megasas_register_aen(instance, eli.newest_seq_num + 1,
2612 class_locale.word);
2613}
2614
2615/**
2616 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2617 * @instance: Adapter soft state
2618 */
2619static int megasas_io_attach(struct megasas_instance *instance)
2620{
2621 struct Scsi_Host *host = instance->host;
2622
2623 /*
2624 * Export parameters required by SCSI mid-layer
2625 */
2626 host->irq = instance->pdev->irq;
2627 host->unique_id = instance->unique_id;
2628 host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
2629 host->this_id = instance->init_id;
2630 host->sg_tablesize = instance->max_num_sge;
2631 host->max_sectors = instance->max_sectors_per_req;
2632 host->cmd_per_lun = 128;
2633 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
2634 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
2635 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08002636 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002637
2638 /*
2639 * Notify the mid-layer about the new controller
2640 */
2641 if (scsi_add_host(host, &instance->pdev->dev)) {
2642 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2643 return -ENODEV;
2644 }
2645
2646 /*
2647 * Trigger SCSI to scan our drives
2648 */
2649 scsi_scan_host(host);
2650 return 0;
2651}
2652
bo yang31ea7082007-11-07 12:09:50 -05002653static int
2654megasas_set_dma_mask(struct pci_dev *pdev)
2655{
2656 /*
2657 * All our contollers are capable of performing 64-bit DMA
2658 */
2659 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07002660 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05002661
Yang Hongyang284901a2009-04-06 19:01:15 -07002662 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05002663 goto fail_set_dma_mask;
2664 }
2665 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07002666 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05002667 goto fail_set_dma_mask;
2668 }
2669 return 0;
2670
2671fail_set_dma_mask:
2672 return 1;
2673}
2674
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002675/**
2676 * megasas_probe_one - PCI hotplug entry point
2677 * @pdev: PCI device structure
2678 * @id: PCI ids of supported hotplugged adapter
2679 */
2680static int __devinit
2681megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2682{
2683 int rval;
2684 struct Scsi_Host *host;
2685 struct megasas_instance *instance;
2686
2687 /*
2688 * Announce PCI information
2689 */
2690 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2691 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2692 pdev->subsystem_device);
2693
2694 printk("bus %d:slot %d:func %d\n",
2695 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2696
2697 /*
2698 * PCI prepping: enable device set bus mastering and dma mask
2699 */
2700 rval = pci_enable_device(pdev);
2701
2702 if (rval) {
2703 return rval;
2704 }
2705
2706 pci_set_master(pdev);
2707
bo yang31ea7082007-11-07 12:09:50 -05002708 if (megasas_set_dma_mask(pdev))
2709 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002710
2711 host = scsi_host_alloc(&megasas_template,
2712 sizeof(struct megasas_instance));
2713
2714 if (!host) {
2715 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2716 goto fail_alloc_instance;
2717 }
2718
2719 instance = (struct megasas_instance *)host->hostdata;
2720 memset(instance, 0, sizeof(*instance));
2721
2722 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2723 &instance->producer_h);
2724 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2725 &instance->consumer_h);
2726
2727 if (!instance->producer || !instance->consumer) {
2728 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2729 "producer, consumer\n");
2730 goto fail_alloc_dma_buf;
2731 }
2732
2733 *instance->producer = 0;
2734 *instance->consumer = 0;
Yang, Boc3518832009-10-06 14:18:02 -06002735 megasas_poll_wait_aen = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002736
2737 instance->evt_detail = pci_alloc_consistent(pdev,
2738 sizeof(struct
2739 megasas_evt_detail),
2740 &instance->evt_detail_h);
2741
2742 if (!instance->evt_detail) {
2743 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2744 "event detail structure\n");
2745 goto fail_alloc_dma_buf;
2746 }
2747
2748 /*
2749 * Initialize locks and queues
2750 */
2751 INIT_LIST_HEAD(&instance->cmd_pool);
2752
Sumant Patroe4a082c2006-05-30 12:03:37 -07002753 atomic_set(&instance->fw_outstanding,0);
2754
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002755 init_waitqueue_head(&instance->int_cmd_wait_q);
2756 init_waitqueue_head(&instance->abort_cmd_wait_q);
2757
2758 spin_lock_init(&instance->cmd_pool_lock);
bo yang7343eb62007-11-09 04:35:44 -05002759 spin_lock_init(&instance->completion_lock);
Yang, Boc3518832009-10-06 14:18:02 -06002760 spin_lock_init(&poll_aen_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002761
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02002762 mutex_init(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002763 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2764
2765 /*
2766 * Initialize PCI related and misc parameters
2767 */
2768 instance->pdev = pdev;
2769 instance->host = host;
2770 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2771 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2772
Sumant Patro658dced2006-10-03 13:09:14 -07002773 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002774 instance->flag = 0;
Yang, Boc3518832009-10-06 14:18:02 -06002775 instance->unload = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002776 instance->last_time = 0;
Sumant Patro658dced2006-10-03 13:09:14 -07002777
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002778 /*
2779 * Initialize MFI Firmware
2780 */
2781 if (megasas_init_mfi(instance))
2782 goto fail_init_mfi;
2783
2784 /*
2785 * Register IRQ
2786 */
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07002787 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002788 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2789 goto fail_irq;
2790 }
2791
Sumant Patro1341c932006-01-25 12:02:40 -08002792 instance->instancet->enable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002793
2794 /*
2795 * Store instance in PCI softstate
2796 */
2797 pci_set_drvdata(pdev, instance);
2798
2799 /*
2800 * Add this controller to megasas_mgmt_info structure so that it
2801 * can be exported to management applications
2802 */
2803 megasas_mgmt_info.count++;
2804 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2805 megasas_mgmt_info.max_index++;
2806
2807 /*
2808 * Initiate AEN (Asynchronous Event Notification)
2809 */
2810 if (megasas_start_aen(instance)) {
2811 printk(KERN_DEBUG "megasas: start aen failed\n");
2812 goto fail_start_aen;
2813 }
2814
2815 /*
2816 * Register with SCSI mid-layer
2817 */
2818 if (megasas_io_attach(instance))
2819 goto fail_io_attach;
2820
2821 return 0;
2822
2823 fail_start_aen:
2824 fail_io_attach:
2825 megasas_mgmt_info.count--;
2826 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2827 megasas_mgmt_info.max_index--;
2828
2829 pci_set_drvdata(pdev, NULL);
Sumant Patrob274cab2006-10-03 12:52:12 -07002830 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002831 free_irq(instance->pdev->irq, instance);
2832
2833 megasas_release_mfi(instance);
2834
2835 fail_irq:
2836 fail_init_mfi:
2837 fail_alloc_dma_buf:
2838 if (instance->evt_detail)
2839 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2840 instance->evt_detail,
2841 instance->evt_detail_h);
2842
2843 if (instance->producer)
2844 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2845 instance->producer_h);
2846 if (instance->consumer)
2847 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2848 instance->consumer_h);
2849 scsi_host_put(host);
2850
2851 fail_alloc_instance:
2852 fail_set_dma_mask:
2853 pci_disable_device(pdev);
2854
2855 return -ENODEV;
2856}
2857
2858/**
2859 * megasas_flush_cache - Requests FW to flush all its caches
2860 * @instance: Adapter soft state
2861 */
2862static void megasas_flush_cache(struct megasas_instance *instance)
2863{
2864 struct megasas_cmd *cmd;
2865 struct megasas_dcmd_frame *dcmd;
2866
2867 cmd = megasas_get_cmd(instance);
2868
2869 if (!cmd)
2870 return;
2871
2872 dcmd = &cmd->frame->dcmd;
2873
2874 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2875
2876 dcmd->cmd = MFI_CMD_DCMD;
2877 dcmd->cmd_status = 0x0;
2878 dcmd->sge_count = 0;
2879 dcmd->flags = MFI_FRAME_DIR_NONE;
2880 dcmd->timeout = 0;
2881 dcmd->data_xfer_len = 0;
2882 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
2883 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
2884
2885 megasas_issue_blocked_cmd(instance, cmd);
2886
2887 megasas_return_cmd(instance, cmd);
2888
2889 return;
2890}
2891
2892/**
2893 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2894 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05002895 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002896 */
bo yang31ea7082007-11-07 12:09:50 -05002897static void megasas_shutdown_controller(struct megasas_instance *instance,
2898 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002899{
2900 struct megasas_cmd *cmd;
2901 struct megasas_dcmd_frame *dcmd;
2902
2903 cmd = megasas_get_cmd(instance);
2904
2905 if (!cmd)
2906 return;
2907
2908 if (instance->aen_cmd)
2909 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
2910
2911 dcmd = &cmd->frame->dcmd;
2912
2913 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2914
2915 dcmd->cmd = MFI_CMD_DCMD;
2916 dcmd->cmd_status = 0x0;
2917 dcmd->sge_count = 0;
2918 dcmd->flags = MFI_FRAME_DIR_NONE;
2919 dcmd->timeout = 0;
2920 dcmd->data_xfer_len = 0;
bo yang31ea7082007-11-07 12:09:50 -05002921 dcmd->opcode = opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002922
2923 megasas_issue_blocked_cmd(instance, cmd);
2924
2925 megasas_return_cmd(instance, cmd);
2926
2927 return;
2928}
2929
Jiri Slaby33139b22008-05-01 17:56:02 +02002930#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002931/**
bo yangad84db22007-11-09 04:40:16 -05002932 * megasas_suspend - driver suspend entry point
2933 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05002934 * @state: PCI power state to suspend routine
2935 */
Jiri Slaby33139b22008-05-01 17:56:02 +02002936static int
bo yang31ea7082007-11-07 12:09:50 -05002937megasas_suspend(struct pci_dev *pdev, pm_message_t state)
2938{
2939 struct Scsi_Host *host;
2940 struct megasas_instance *instance;
2941
2942 instance = pci_get_drvdata(pdev);
2943 host = instance->host;
2944
bo yangad84db22007-11-09 04:40:16 -05002945 if (poll_mode_io)
2946 del_timer_sync(&instance->io_completion_timer);
2947
bo yang31ea7082007-11-07 12:09:50 -05002948 megasas_flush_cache(instance);
2949 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
2950 tasklet_kill(&instance->isr_tasklet);
2951
2952 pci_set_drvdata(instance->pdev, instance);
2953 instance->instancet->disable_intr(instance->reg_set);
2954 free_irq(instance->pdev->irq, instance);
2955
2956 pci_save_state(pdev);
2957 pci_disable_device(pdev);
2958
2959 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2960
2961 return 0;
2962}
2963
2964/**
2965 * megasas_resume- driver resume entry point
2966 * @pdev: PCI device structure
2967 */
Jiri Slaby33139b22008-05-01 17:56:02 +02002968static int
bo yang31ea7082007-11-07 12:09:50 -05002969megasas_resume(struct pci_dev *pdev)
2970{
2971 int rval;
2972 struct Scsi_Host *host;
2973 struct megasas_instance *instance;
2974
2975 instance = pci_get_drvdata(pdev);
2976 host = instance->host;
2977 pci_set_power_state(pdev, PCI_D0);
2978 pci_enable_wake(pdev, PCI_D0, 0);
2979 pci_restore_state(pdev);
2980
2981 /*
2982 * PCI prepping: enable device set bus mastering and dma mask
2983 */
2984 rval = pci_enable_device(pdev);
2985
2986 if (rval) {
2987 printk(KERN_ERR "megasas: Enable device failed\n");
2988 return rval;
2989 }
2990
2991 pci_set_master(pdev);
2992
2993 if (megasas_set_dma_mask(pdev))
2994 goto fail_set_dma_mask;
2995
2996 /*
2997 * Initialize MFI Firmware
2998 */
2999
3000 *instance->producer = 0;
3001 *instance->consumer = 0;
3002
3003 atomic_set(&instance->fw_outstanding, 0);
3004
3005 /*
3006 * We expect the FW state to be READY
3007 */
3008 if (megasas_transition_to_ready(instance))
3009 goto fail_ready_state;
3010
3011 if (megasas_issue_init_mfi(instance))
3012 goto fail_init_mfi;
3013
3014 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
3015 (unsigned long)instance);
3016
3017 /*
3018 * Register IRQ
3019 */
3020 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED,
3021 "megasas", instance)) {
3022 printk(KERN_ERR "megasas: Failed to register IRQ\n");
3023 goto fail_irq;
3024 }
3025
3026 instance->instancet->enable_intr(instance->reg_set);
3027
3028 /*
3029 * Initiate AEN (Asynchronous Event Notification)
3030 */
3031 if (megasas_start_aen(instance))
3032 printk(KERN_ERR "megasas: Start AEN failed\n");
3033
bo yangad84db22007-11-09 04:40:16 -05003034 /* Initialize the cmd completion timer */
3035 if (poll_mode_io)
3036 megasas_start_timer(instance, &instance->io_completion_timer,
3037 megasas_io_completion_timer,
3038 MEGASAS_COMPLETION_TIMER_INTERVAL);
bo yang31ea7082007-11-07 12:09:50 -05003039 return 0;
3040
3041fail_irq:
3042fail_init_mfi:
3043 if (instance->evt_detail)
3044 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3045 instance->evt_detail,
3046 instance->evt_detail_h);
3047
3048 if (instance->producer)
3049 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3050 instance->producer_h);
3051 if (instance->consumer)
3052 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3053 instance->consumer_h);
3054 scsi_host_put(host);
3055
3056fail_set_dma_mask:
3057fail_ready_state:
3058
3059 pci_disable_device(pdev);
3060
3061 return -ENODEV;
3062}
Jiri Slaby33139b22008-05-01 17:56:02 +02003063#else
3064#define megasas_suspend NULL
3065#define megasas_resume NULL
3066#endif
bo yang31ea7082007-11-07 12:09:50 -05003067
3068/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003069 * megasas_detach_one - PCI hot"un"plug entry point
3070 * @pdev: PCI device structure
3071 */
Jiri Slaby33139b22008-05-01 17:56:02 +02003072static void __devexit megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003073{
3074 int i;
3075 struct Scsi_Host *host;
3076 struct megasas_instance *instance;
3077
3078 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06003079 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003080 host = instance->host;
3081
bo yangad84db22007-11-09 04:40:16 -05003082 if (poll_mode_io)
3083 del_timer_sync(&instance->io_completion_timer);
3084
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003085 scsi_remove_host(instance->host);
3086 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05003087 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Sumant Patro5d018ad2006-10-03 13:13:18 -07003088 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003089
3090 /*
3091 * Take the instance off the instance array. Note that we will not
3092 * decrement the max_index. We let this array be sparse array
3093 */
3094 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3095 if (megasas_mgmt_info.instance[i] == instance) {
3096 megasas_mgmt_info.count--;
3097 megasas_mgmt_info.instance[i] = NULL;
3098
3099 break;
3100 }
3101 }
3102
3103 pci_set_drvdata(instance->pdev, NULL);
3104
Sumant Patrob274cab2006-10-03 12:52:12 -07003105 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003106
3107 free_irq(instance->pdev->irq, instance);
3108
3109 megasas_release_mfi(instance);
3110
3111 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3112 instance->evt_detail, instance->evt_detail_h);
3113
3114 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3115 instance->producer_h);
3116
3117 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3118 instance->consumer_h);
3119
3120 scsi_host_put(host);
3121
3122 pci_set_drvdata(pdev, NULL);
3123
3124 pci_disable_device(pdev);
3125
3126 return;
3127}
3128
3129/**
3130 * megasas_shutdown - Shutdown entry point
3131 * @device: Generic device structure
3132 */
3133static void megasas_shutdown(struct pci_dev *pdev)
3134{
3135 struct megasas_instance *instance = pci_get_drvdata(pdev);
3136 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07003137 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003138}
3139
3140/**
3141 * megasas_mgmt_open - char node "open" entry point
3142 */
3143static int megasas_mgmt_open(struct inode *inode, struct file *filep)
3144{
Jonathan Corbetf2b98572008-05-18 15:32:43 -06003145 cycle_kernel_lock();
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003146 /*
3147 * Allow only those users with admin rights
3148 */
3149 if (!capable(CAP_SYS_ADMIN))
3150 return -EACCES;
3151
3152 return 0;
3153}
3154
3155/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003156 * megasas_mgmt_fasync - Async notifier registration from applications
3157 *
3158 * This function adds the calling process to a driver global queue. When an
3159 * event occurs, SIGIO will be sent to all processes in this queue.
3160 */
3161static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
3162{
3163 int rc;
3164
Arjan van de Ven0b950672006-01-11 13:16:10 +01003165 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003166
3167 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
3168
Arjan van de Ven0b950672006-01-11 13:16:10 +01003169 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003170
3171 if (rc >= 0) {
3172 /* For sanity check when we get ioctl */
3173 filep->private_data = filep;
3174 return 0;
3175 }
3176
3177 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
3178
3179 return rc;
3180}
3181
3182/**
Yang, Boc3518832009-10-06 14:18:02 -06003183 * megasas_mgmt_poll - char node "poll" entry point
3184 * */
3185static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
3186{
3187 unsigned int mask;
3188 unsigned long flags;
3189 poll_wait(file, &megasas_poll_wait, wait);
3190 spin_lock_irqsave(&poll_aen_lock, flags);
3191 if (megasas_poll_wait_aen)
3192 mask = (POLLIN | POLLRDNORM);
3193 else
3194 mask = 0;
3195 spin_unlock_irqrestore(&poll_aen_lock, flags);
3196 return mask;
3197}
3198
3199/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003200 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
3201 * @instance: Adapter soft state
3202 * @argp: User's ioctl packet
3203 */
3204static int
3205megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
3206 struct megasas_iocpacket __user * user_ioc,
3207 struct megasas_iocpacket *ioc)
3208{
3209 struct megasas_sge32 *kern_sge32;
3210 struct megasas_cmd *cmd;
3211 void *kbuff_arr[MAX_IOCTL_SGE];
3212 dma_addr_t buf_handle = 0;
3213 int error = 0, i;
3214 void *sense = NULL;
3215 dma_addr_t sense_handle;
3216 u32 *sense_ptr;
3217
3218 memset(kbuff_arr, 0, sizeof(kbuff_arr));
3219
3220 if (ioc->sge_count > MAX_IOCTL_SGE) {
3221 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
3222 ioc->sge_count, MAX_IOCTL_SGE);
3223 return -EINVAL;
3224 }
3225
3226 cmd = megasas_get_cmd(instance);
3227 if (!cmd) {
3228 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
3229 return -ENOMEM;
3230 }
3231
3232 /*
3233 * User's IOCTL packet has 2 frames (maximum). Copy those two
3234 * frames into our cmd's frames. cmd->frame's context will get
3235 * overwritten when we copy from user's frames. So set that value
3236 * alone separately
3237 */
3238 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
3239 cmd->frame->hdr.context = cmd->index;
Yang, Boc3518832009-10-06 14:18:02 -06003240 cmd->frame->hdr.pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003241
3242 /*
3243 * The management interface between applications and the fw uses
3244 * MFI frames. E.g, RAID configuration changes, LD property changes
3245 * etc are accomplishes through different kinds of MFI frames. The
3246 * driver needs to care only about substituting user buffers with
3247 * kernel buffers in SGLs. The location of SGL is embedded in the
3248 * struct iocpacket itself.
3249 */
3250 kern_sge32 = (struct megasas_sge32 *)
3251 ((unsigned long)cmd->frame + ioc->sgl_off);
3252
3253 /*
3254 * For each user buffer, create a mirror buffer and copy in
3255 */
3256 for (i = 0; i < ioc->sge_count; i++) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08003257 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003258 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08003259 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003260 if (!kbuff_arr[i]) {
3261 printk(KERN_DEBUG "megasas: Failed to alloc "
3262 "kernel SGL buffer for IOCTL \n");
3263 error = -ENOMEM;
3264 goto out;
3265 }
3266
3267 /*
3268 * We don't change the dma_coherent_mask, so
3269 * pci_alloc_consistent only returns 32bit addresses
3270 */
3271 kern_sge32[i].phys_addr = (u32) buf_handle;
3272 kern_sge32[i].length = ioc->sgl[i].iov_len;
3273
3274 /*
3275 * We created a kernel buffer corresponding to the
3276 * user buffer. Now copy in from the user buffer
3277 */
3278 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
3279 (u32) (ioc->sgl[i].iov_len))) {
3280 error = -EFAULT;
3281 goto out;
3282 }
3283 }
3284
3285 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08003286 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
3287 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003288 if (!sense) {
3289 error = -ENOMEM;
3290 goto out;
3291 }
3292
3293 sense_ptr =
3294 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
3295 *sense_ptr = sense_handle;
3296 }
3297
3298 /*
3299 * Set the sync_cmd flag so that the ISR knows not to complete this
3300 * cmd to the SCSI mid-layer
3301 */
3302 cmd->sync_cmd = 1;
3303 megasas_issue_blocked_cmd(instance, cmd);
3304 cmd->sync_cmd = 0;
3305
3306 /*
3307 * copy out the kernel buffers to user buffers
3308 */
3309 for (i = 0; i < ioc->sge_count; i++) {
3310 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
3311 ioc->sgl[i].iov_len)) {
3312 error = -EFAULT;
3313 goto out;
3314 }
3315 }
3316
3317 /*
3318 * copy out the sense
3319 */
3320 if (ioc->sense_len) {
3321 /*
bo yangb70a41e2008-03-18 03:13:06 -04003322 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003323 * sense buffer address
3324 */
bo yangb70a41e2008-03-18 03:13:06 -04003325 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
3326 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003327
bo yangb70a41e2008-03-18 03:13:06 -04003328 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
3329 sense, ioc->sense_len)) {
bo yangb10c36a2007-11-09 04:28:47 -05003330 printk(KERN_ERR "megasas: Failed to copy out to user "
3331 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003332 error = -EFAULT;
3333 goto out;
3334 }
3335 }
3336
3337 /*
3338 * copy the status codes returned by the fw
3339 */
3340 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
3341 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
3342 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
3343 error = -EFAULT;
3344 }
3345
3346 out:
3347 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08003348 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003349 sense, sense_handle);
3350 }
3351
3352 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08003353 dma_free_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003354 kern_sge32[i].length,
3355 kbuff_arr[i], kern_sge32[i].phys_addr);
3356 }
3357
3358 megasas_return_cmd(instance, cmd);
3359 return error;
3360}
3361
3362static struct megasas_instance *megasas_lookup_instance(u16 host_no)
3363{
3364 int i;
3365
3366 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3367
3368 if ((megasas_mgmt_info.instance[i]) &&
3369 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
3370 return megasas_mgmt_info.instance[i];
3371 }
3372
3373 return NULL;
3374}
3375
3376static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
3377{
3378 struct megasas_iocpacket __user *user_ioc =
3379 (struct megasas_iocpacket __user *)arg;
3380 struct megasas_iocpacket *ioc;
3381 struct megasas_instance *instance;
3382 int error;
3383
3384 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
3385 if (!ioc)
3386 return -ENOMEM;
3387
3388 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
3389 error = -EFAULT;
3390 goto out_kfree_ioc;
3391 }
3392
3393 instance = megasas_lookup_instance(ioc->host_no);
3394 if (!instance) {
3395 error = -ENODEV;
3396 goto out_kfree_ioc;
3397 }
3398
3399 /*
3400 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
3401 */
3402 if (down_interruptible(&instance->ioctl_sem)) {
3403 error = -ERESTARTSYS;
3404 goto out_kfree_ioc;
3405 }
3406 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
3407 up(&instance->ioctl_sem);
3408
3409 out_kfree_ioc:
3410 kfree(ioc);
3411 return error;
3412}
3413
3414static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
3415{
3416 struct megasas_instance *instance;
3417 struct megasas_aen aen;
3418 int error;
3419
3420 if (file->private_data != file) {
3421 printk(KERN_DEBUG "megasas: fasync_helper was not "
3422 "called first\n");
3423 return -EINVAL;
3424 }
3425
3426 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
3427 return -EFAULT;
3428
3429 instance = megasas_lookup_instance(aen.host_no);
3430
3431 if (!instance)
3432 return -ENODEV;
3433
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02003434 mutex_lock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003435 error = megasas_register_aen(instance, aen.seq_num,
3436 aen.class_locale_word);
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02003437 mutex_unlock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003438 return error;
3439}
3440
3441/**
3442 * megasas_mgmt_ioctl - char node ioctl entry point
3443 */
3444static long
3445megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3446{
3447 switch (cmd) {
3448 case MEGASAS_IOC_FIRMWARE:
3449 return megasas_mgmt_ioctl_fw(file, arg);
3450
3451 case MEGASAS_IOC_GET_AEN:
3452 return megasas_mgmt_ioctl_aen(file, arg);
3453 }
3454
3455 return -ENOTTY;
3456}
3457
3458#ifdef CONFIG_COMPAT
3459static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
3460{
3461 struct compat_megasas_iocpacket __user *cioc =
3462 (struct compat_megasas_iocpacket __user *)arg;
3463 struct megasas_iocpacket __user *ioc =
3464 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
3465 int i;
3466 int error = 0;
3467
Jeff Garzik83aabc12006-10-04 06:34:03 -04003468 if (clear_user(ioc, sizeof(*ioc)))
3469 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003470
3471 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
3472 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
3473 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
3474 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
3475 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
3476 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
3477 return -EFAULT;
3478
3479 for (i = 0; i < MAX_IOCTL_SGE; i++) {
3480 compat_uptr_t ptr;
3481
3482 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
3483 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
3484 copy_in_user(&ioc->sgl[i].iov_len,
3485 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
3486 return -EFAULT;
3487 }
3488
3489 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
3490
3491 if (copy_in_user(&cioc->frame.hdr.cmd_status,
3492 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
3493 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
3494 return -EFAULT;
3495 }
3496 return error;
3497}
3498
3499static long
3500megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
3501 unsigned long arg)
3502{
3503 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08003504 case MEGASAS_IOC_FIRMWARE32:
3505 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003506 case MEGASAS_IOC_GET_AEN:
3507 return megasas_mgmt_ioctl_aen(file, arg);
3508 }
3509
3510 return -ENOTTY;
3511}
3512#endif
3513
3514/*
3515 * File operations structure for management interface
3516 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08003517static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003518 .owner = THIS_MODULE,
3519 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003520 .fasync = megasas_mgmt_fasync,
3521 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06003522 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003523#ifdef CONFIG_COMPAT
3524 .compat_ioctl = megasas_mgmt_compat_ioctl,
3525#endif
3526};
3527
3528/*
3529 * PCI hotplug support registration structure
3530 */
3531static struct pci_driver megasas_pci_driver = {
3532
3533 .name = "megaraid_sas",
3534 .id_table = megasas_pci_table,
3535 .probe = megasas_probe_one,
3536 .remove = __devexit_p(megasas_detach_one),
bo yang31ea7082007-11-07 12:09:50 -05003537 .suspend = megasas_suspend,
3538 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003539 .shutdown = megasas_shutdown,
3540};
3541
3542/*
3543 * Sysfs driver attributes
3544 */
3545static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
3546{
3547 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
3548 MEGASAS_VERSION);
3549}
3550
3551static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
3552
3553static ssize_t
3554megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
3555{
3556 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
3557 MEGASAS_RELDATE);
3558}
3559
3560static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
3561 NULL);
3562
Sumant Patro658dced2006-10-03 13:09:14 -07003563static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06003564megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
3565{
3566 return sprintf(buf, "%u\n", support_poll_for_event);
3567}
3568
3569static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
3570 megasas_sysfs_show_support_poll_for_event, NULL);
3571
3572static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07003573megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
3574{
bo yangad84db22007-11-09 04:40:16 -05003575 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07003576}
3577
3578static ssize_t
3579megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
3580{
3581 int retval = count;
3582 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
3583 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
3584 retval = -EINVAL;
3585 }
3586 return retval;
3587}
3588
Joe Malicki66dca9b2008-08-14 17:14:48 -04003589static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05003590 megasas_sysfs_set_dbg_lvl);
3591
3592static ssize_t
3593megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
3594{
3595 return sprintf(buf, "%u\n", poll_mode_io);
3596}
3597
3598static ssize_t
3599megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
3600 const char *buf, size_t count)
3601{
3602 int retval = count;
3603 int tmp = poll_mode_io;
3604 int i;
3605 struct megasas_instance *instance;
3606
3607 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
3608 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
3609 retval = -EINVAL;
3610 }
3611
3612 /*
3613 * Check if poll_mode_io is already set or is same as previous value
3614 */
3615 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
3616 goto out;
3617
3618 if (poll_mode_io) {
3619 /*
3620 * Start timers for all adapters
3621 */
3622 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3623 instance = megasas_mgmt_info.instance[i];
3624 if (instance) {
3625 megasas_start_timer(instance,
3626 &instance->io_completion_timer,
3627 megasas_io_completion_timer,
3628 MEGASAS_COMPLETION_TIMER_INTERVAL);
3629 }
3630 }
3631 } else {
3632 /*
3633 * Delete timers for all adapters
3634 */
3635 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3636 instance = megasas_mgmt_info.instance[i];
3637 if (instance)
3638 del_timer_sync(&instance->io_completion_timer);
3639 }
3640 }
3641
3642out:
3643 return retval;
3644}
3645
3646static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUGO,
3647 megasas_sysfs_show_poll_mode_io,
3648 megasas_sysfs_set_poll_mode_io);
Sumant Patro658dced2006-10-03 13:09:14 -07003649
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003650/**
3651 * megasas_init - Driver load entry point
3652 */
3653static int __init megasas_init(void)
3654{
3655 int rval;
3656
3657 /*
3658 * Announce driver version and other information
3659 */
3660 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
3661 MEGASAS_EXT_VERSION);
3662
Yang, Bo72c4fd32009-10-06 14:20:59 -06003663 support_poll_for_event = 2;
3664
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003665 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
3666
3667 /*
3668 * Register character device node
3669 */
3670 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
3671
3672 if (rval < 0) {
3673 printk(KERN_DEBUG "megasas: failed to open device node\n");
3674 return rval;
3675 }
3676
3677 megasas_mgmt_majorno = rval;
3678
3679 /*
3680 * Register ourselves as PCI hotplug module
3681 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00003682 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003683
3684 if (rval) {
3685 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04003686 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003687 }
3688
Jeff Garzik83aabc12006-10-04 06:34:03 -04003689 rval = driver_create_file(&megasas_pci_driver.driver,
3690 &driver_attr_version);
3691 if (rval)
3692 goto err_dcf_attr_ver;
3693 rval = driver_create_file(&megasas_pci_driver.driver,
3694 &driver_attr_release_date);
3695 if (rval)
3696 goto err_dcf_rel_date;
Yang, Bo72c4fd32009-10-06 14:20:59 -06003697
3698 rval = driver_create_file(&megasas_pci_driver.driver,
3699 &driver_attr_support_poll_for_event);
3700 if (rval)
3701 goto err_dcf_support_poll_for_event;
3702
Jeff Garzik83aabc12006-10-04 06:34:03 -04003703 rval = driver_create_file(&megasas_pci_driver.driver,
3704 &driver_attr_dbg_lvl);
3705 if (rval)
3706 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05003707 rval = driver_create_file(&megasas_pci_driver.driver,
3708 &driver_attr_poll_mode_io);
3709 if (rval)
3710 goto err_dcf_poll_mode_io;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003711
3712 return rval;
bo yangad84db22007-11-09 04:40:16 -05003713
3714err_dcf_poll_mode_io:
3715 driver_remove_file(&megasas_pci_driver.driver,
3716 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04003717err_dcf_dbg_lvl:
3718 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06003719 &driver_attr_support_poll_for_event);
3720
3721err_dcf_support_poll_for_event:
3722 driver_remove_file(&megasas_pci_driver.driver,
Jeff Garzik83aabc12006-10-04 06:34:03 -04003723 &driver_attr_release_date);
Yang, Bo72c4fd32009-10-06 14:20:59 -06003724
Jeff Garzik83aabc12006-10-04 06:34:03 -04003725err_dcf_rel_date:
3726 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3727err_dcf_attr_ver:
3728 pci_unregister_driver(&megasas_pci_driver);
3729err_pcidrv:
3730 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3731 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003732}
3733
3734/**
3735 * megasas_exit - Driver unload entry point
3736 */
3737static void __exit megasas_exit(void)
3738{
Sumant Patro658dced2006-10-03 13:09:14 -07003739 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05003740 &driver_attr_poll_mode_io);
3741 driver_remove_file(&megasas_pci_driver.driver,
Sumant Patro658dced2006-10-03 13:09:14 -07003742 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04003743 driver_remove_file(&megasas_pci_driver.driver,
3744 &driver_attr_release_date);
3745 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003746
3747 pci_unregister_driver(&megasas_pci_driver);
3748 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3749}
3750
3751module_init(megasas_init);
3752module_exit(megasas_exit);