blob: ffd1390b155bf0eee4034c89a595d161b3033420 [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
bo yangf28cd7c2007-11-09 04:44:56 -050013 * Version : v00.00.03.16-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>
36#include <linux/uio.h>
37#include <asm/uaccess.h>
Al Viro43399232005-10-04 17:36:04 +010038#include <linux/fs.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040039#include <linux/compat.h>
Sumant Patrocf62a0a2007-02-14 12:41:55 -080040#include <linux/blkdev.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010041#include <linux/mutex.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040042
43#include <scsi/scsi.h>
44#include <scsi/scsi_cmnd.h>
45#include <scsi/scsi_device.h>
46#include <scsi/scsi_host.h>
47#include "megaraid_sas.h"
48
bo yangad84db22007-11-09 04:40:16 -050049/*
50 * poll_mode_io:1- schedule complete completion from q cmd
51 */
52static unsigned int poll_mode_io;
53module_param_named(poll_mode_io, poll_mode_io, int, 0);
54MODULE_PARM_DESC(poll_mode_io,
55 "Complete cmds from IO path, (default=0)");
56
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040057MODULE_LICENSE("GPL");
58MODULE_VERSION(MEGASAS_VERSION);
Sumant Patro3d6d1742006-12-29 08:13:54 -080059MODULE_AUTHOR("megaraidlinux@lsi.com");
bo yangf28cd7c2007-11-09 04:44:56 -050060MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040061
62/*
63 * PCI ID table for all supported controllers
64 */
65static struct pci_device_id megasas_pci_table[] = {
66
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +020067 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
68 /* xscale IOP */
69 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
70 /* ppc IOP */
71 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
72 /* xscale IOP, vega */
73 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
74 /* xscale IOP */
75 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040076};
77
78MODULE_DEVICE_TABLE(pci, megasas_pci_table);
79
80static int megasas_mgmt_majorno;
81static struct megasas_mgmt_info megasas_mgmt_info;
82static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +010083static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040084
Sumant Patro658dced2006-10-03 13:09:14 -070085static u32 megasas_dbg_lvl;
86
bo yang7343eb62007-11-09 04:35:44 -050087static void
88megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
89 u8 alt_status);
90
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040091/**
92 * megasas_get_cmd - Get a command from the free pool
93 * @instance: Adapter soft state
94 *
95 * Returns a free command from the pool
96 */
Arjan van de Ven858119e2006-01-14 13:20:43 -080097static struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040098 *instance)
99{
100 unsigned long flags;
101 struct megasas_cmd *cmd = NULL;
102
103 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
104
105 if (!list_empty(&instance->cmd_pool)) {
106 cmd = list_entry((&instance->cmd_pool)->next,
107 struct megasas_cmd, list);
108 list_del_init(&cmd->list);
109 } else {
110 printk(KERN_ERR "megasas: Command pool empty!\n");
111 }
112
113 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
114 return cmd;
115}
116
117/**
118 * megasas_return_cmd - Return a cmd to free command pool
119 * @instance: Adapter soft state
120 * @cmd: Command packet to be returned to free command pool
121 */
122static inline void
123megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
124{
125 unsigned long flags;
126
127 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
128
129 cmd->scmd = NULL;
130 list_add_tail(&cmd->list, &instance->cmd_pool);
131
132 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
133}
134
Sumant Patro1341c932006-01-25 12:02:40 -0800135
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400136/**
Sumant Patro1341c932006-01-25 12:02:40 -0800137* The following functions are defined for xscale
138* (deviceid : 1064R, PERC5) controllers
139*/
140
141/**
142 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400143 * @regs: MFI register set
144 */
145static inline void
Sumant Patro1341c932006-01-25 12:02:40 -0800146megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400147{
148 writel(1, &(regs)->outbound_intr_mask);
149
150 /* Dummy readl to force pci flush */
151 readl(&regs->outbound_intr_mask);
152}
153
154/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700155 * megasas_disable_intr_xscale -Disables interrupt
156 * @regs: MFI register set
157 */
158static inline void
159megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
160{
161 u32 mask = 0x1f;
162 writel(mask, &regs->outbound_intr_mask);
163 /* Dummy readl to force pci flush */
164 readl(&regs->outbound_intr_mask);
165}
166
167/**
Sumant Patro1341c932006-01-25 12:02:40 -0800168 * megasas_read_fw_status_reg_xscale - returns the current FW status value
169 * @regs: MFI register set
170 */
171static u32
172megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
173{
174 return readl(&(regs)->outbound_msg_0);
175}
176/**
177 * megasas_clear_interrupt_xscale - Check & clear interrupt
178 * @regs: MFI register set
179 */
180static int
181megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
182{
183 u32 status;
184 /*
185 * Check if it is our interrupt
186 */
187 status = readl(&regs->outbound_intr_status);
188
189 if (!(status & MFI_OB_INTR_STATUS_MASK)) {
190 return 1;
191 }
192
193 /*
194 * Clear the interrupt by writing back the same value
195 */
196 writel(status, &regs->outbound_intr_status);
197
198 return 0;
199}
200
201/**
202 * megasas_fire_cmd_xscale - Sends command to the FW
203 * @frame_phys_addr : Physical address of cmd
204 * @frame_count : Number of frames for the command
205 * @regs : MFI register set
206 */
207static inline void
208megasas_fire_cmd_xscale(dma_addr_t frame_phys_addr,u32 frame_count, struct megasas_register_set __iomem *regs)
209{
210 writel((frame_phys_addr >> 3)|(frame_count),
211 &(regs)->inbound_queue_port);
212}
213
214static struct megasas_instance_template megasas_instance_template_xscale = {
215
216 .fire_cmd = megasas_fire_cmd_xscale,
217 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700218 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800219 .clear_intr = megasas_clear_intr_xscale,
220 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
221};
222
223/**
224* This is the end of set of functions & definitions specific
225* to xscale (deviceid : 1064R, PERC5) controllers
226*/
227
228/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800229* The following functions are defined for ppc (deviceid : 0x60)
230* controllers
231*/
232
233/**
234 * megasas_enable_intr_ppc - Enables interrupts
235 * @regs: MFI register set
236 */
237static inline void
238megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
239{
240 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
241
242 writel(~0x80000004, &(regs)->outbound_intr_mask);
243
244 /* Dummy readl to force pci flush */
245 readl(&regs->outbound_intr_mask);
246}
247
248/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700249 * megasas_disable_intr_ppc - Disable interrupt
250 * @regs: MFI register set
251 */
252static inline void
253megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
254{
255 u32 mask = 0xFFFFFFFF;
256 writel(mask, &regs->outbound_intr_mask);
257 /* Dummy readl to force pci flush */
258 readl(&regs->outbound_intr_mask);
259}
260
261/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800262 * megasas_read_fw_status_reg_ppc - returns the current FW status value
263 * @regs: MFI register set
264 */
265static u32
266megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
267{
268 return readl(&(regs)->outbound_scratch_pad);
269}
270
271/**
272 * megasas_clear_interrupt_ppc - Check & clear interrupt
273 * @regs: MFI register set
274 */
275static int
276megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
277{
278 u32 status;
279 /*
280 * Check if it is our interrupt
281 */
282 status = readl(&regs->outbound_intr_status);
283
284 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
285 return 1;
286 }
287
288 /*
289 * Clear the interrupt by writing back the same value
290 */
291 writel(status, &regs->outbound_doorbell_clear);
292
293 return 0;
294}
295/**
296 * megasas_fire_cmd_ppc - Sends command to the FW
297 * @frame_phys_addr : Physical address of cmd
298 * @frame_count : Number of frames for the command
299 * @regs : MFI register set
300 */
301static inline void
302megasas_fire_cmd_ppc(dma_addr_t frame_phys_addr, u32 frame_count, struct megasas_register_set __iomem *regs)
303{
304 writel((frame_phys_addr | (frame_count<<1))|1,
305 &(regs)->inbound_queue_port);
306}
307
308static struct megasas_instance_template megasas_instance_template_ppc = {
309
310 .fire_cmd = megasas_fire_cmd_ppc,
311 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700312 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800313 .clear_intr = megasas_clear_intr_ppc,
314 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
315};
316
317/**
318* This is the end of set of functions & definitions
319* specific to ppc (deviceid : 0x60) controllers
320*/
321
322/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400323 * megasas_issue_polled - Issues a polling command
324 * @instance: Adapter soft state
325 * @cmd: Command packet to be issued
326 *
327 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
328 */
329static int
330megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
331{
332 int i;
333 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
334
335 struct megasas_header *frame_hdr = &cmd->frame->hdr;
336
337 frame_hdr->cmd_status = 0xFF;
338 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
339
340 /*
341 * Issue the frame using inbound queue port
342 */
Sumant Patro1341c932006-01-25 12:02:40 -0800343 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400344
345 /*
346 * Wait for cmd_status to change
347 */
348 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
349 rmb();
350 msleep(1);
351 }
352
353 if (frame_hdr->cmd_status == 0xff)
354 return -ETIME;
355
356 return 0;
357}
358
359/**
360 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
361 * @instance: Adapter soft state
362 * @cmd: Command to be issued
363 *
364 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -0700365 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400366 * Used to issue ioctl commands.
367 */
368static int
369megasas_issue_blocked_cmd(struct megasas_instance *instance,
370 struct megasas_cmd *cmd)
371{
372 cmd->cmd_status = ENODATA;
373
Sumant Patro1341c932006-01-25 12:02:40 -0800374 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400375
Sumant Patro2a3681e2006-10-03 13:19:21 -0700376 wait_event_timeout(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA),
377 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400378
379 return 0;
380}
381
382/**
383 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
384 * @instance: Adapter soft state
385 * @cmd_to_abort: Previously issued cmd to be aborted
386 *
387 * MFI firmware can abort previously issued AEN comamnd (automatic event
388 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -0700389 * cmd and waits for return status.
390 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400391 */
392static int
393megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
394 struct megasas_cmd *cmd_to_abort)
395{
396 struct megasas_cmd *cmd;
397 struct megasas_abort_frame *abort_fr;
398
399 cmd = megasas_get_cmd(instance);
400
401 if (!cmd)
402 return -1;
403
404 abort_fr = &cmd->frame->abort;
405
406 /*
407 * Prepare and issue the abort frame
408 */
409 abort_fr->cmd = MFI_CMD_ABORT;
410 abort_fr->cmd_status = 0xFF;
411 abort_fr->flags = 0;
412 abort_fr->abort_context = cmd_to_abort->index;
413 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
414 abort_fr->abort_mfi_phys_addr_hi = 0;
415
416 cmd->sync_cmd = 1;
417 cmd->cmd_status = 0xFF;
418
Sumant Patro1341c932006-01-25 12:02:40 -0800419 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400420
421 /*
422 * Wait for this cmd to complete
423 */
Sumant Patro2a3681e2006-10-03 13:19:21 -0700424 wait_event_timeout(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF),
425 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400426
427 megasas_return_cmd(instance, cmd);
428 return 0;
429}
430
431/**
432 * megasas_make_sgl32 - Prepares 32-bit SGL
433 * @instance: Adapter soft state
434 * @scp: SCSI command from the mid-layer
435 * @mfi_sgl: SGL to be filled in
436 *
437 * If successful, this function returns the number of SG elements. Otherwise,
438 * it returnes -1.
439 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800440static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400441megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
442 union megasas_sgl *mfi_sgl)
443{
444 int i;
445 int sge_count;
446 struct scatterlist *os_sgl;
447
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900448 sge_count = scsi_dma_map(scp);
449 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400450
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900451 if (sge_count) {
452 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
453 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
454 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
455 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400456 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400457 return sge_count;
458}
459
460/**
461 * megasas_make_sgl64 - Prepares 64-bit SGL
462 * @instance: Adapter soft state
463 * @scp: SCSI command from the mid-layer
464 * @mfi_sgl: SGL to be filled in
465 *
466 * If successful, this function returns the number of SG elements. Otherwise,
467 * it returnes -1.
468 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800469static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400470megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
471 union megasas_sgl *mfi_sgl)
472{
473 int i;
474 int sge_count;
475 struct scatterlist *os_sgl;
476
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900477 sge_count = scsi_dma_map(scp);
478 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400479
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900480 if (sge_count) {
481 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
482 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
483 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
484 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400485 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400486 return sge_count;
487}
488
Sumant Patrob1df99d2006-10-03 12:40:47 -0700489 /**
490 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -0400491 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -0700492 * @sge_count : number of sg elements
493 *
494 * Returns the number of frames required for numnber of sge's (sge_count)
495 */
496
bo yangd532dbe2008-03-17 03:36:43 -0400497static u32 megasas_get_frame_count(u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -0700498{
499 int num_cnt;
500 int sge_bytes;
501 u32 sge_sz;
502 u32 frame_count=0;
503
504 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
505 sizeof(struct megasas_sge32);
506
507 /*
bo yangd532dbe2008-03-17 03:36:43 -0400508 * Main frame can contain 2 SGEs for 64-bit SGLs and
509 * 3 SGEs for 32-bit SGLs for ldio &
510 * 1 SGEs for 64-bit SGLs and
511 * 2 SGEs for 32-bit SGLs for pthru frame
512 */
513 if (unlikely(frame_type == PTHRU_FRAME)) {
514 if (IS_DMA64)
515 num_cnt = sge_count - 1;
516 else
517 num_cnt = sge_count - 2;
518 } else {
519 if (IS_DMA64)
520 num_cnt = sge_count - 2;
521 else
522 num_cnt = sge_count - 3;
523 }
Sumant Patrob1df99d2006-10-03 12:40:47 -0700524
525 if(num_cnt>0){
526 sge_bytes = sge_sz * num_cnt;
527
528 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
529 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
530 }
531 /* Main frame */
532 frame_count +=1;
533
534 if (frame_count > 7)
535 frame_count = 8;
536 return frame_count;
537}
538
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400539/**
540 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
541 * @instance: Adapter soft state
542 * @scp: SCSI command
543 * @cmd: Command to be prepared in
544 *
545 * This function prepares CDB commands. These are typcially pass-through
546 * commands to the devices.
547 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800548static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400549megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
550 struct megasas_cmd *cmd)
551{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400552 u32 is_logical;
553 u32 device_id;
554 u16 flags = 0;
555 struct megasas_pthru_frame *pthru;
556
557 is_logical = MEGASAS_IS_LOGICAL(scp);
558 device_id = MEGASAS_DEV_INDEX(instance, scp);
559 pthru = (struct megasas_pthru_frame *)cmd->frame;
560
561 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
562 flags = MFI_FRAME_DIR_WRITE;
563 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
564 flags = MFI_FRAME_DIR_READ;
565 else if (scp->sc_data_direction == PCI_DMA_NONE)
566 flags = MFI_FRAME_DIR_NONE;
567
568 /*
569 * Prepare the DCDB frame
570 */
571 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
572 pthru->cmd_status = 0x0;
573 pthru->scsi_status = 0x0;
574 pthru->target_id = device_id;
575 pthru->lun = scp->device->lun;
576 pthru->cdb_len = scp->cmd_len;
577 pthru->timeout = 0;
578 pthru->flags = flags;
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900579 pthru->data_xfer_len = scsi_bufflen(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400580
581 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
582
583 /*
584 * Construct SGL
585 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400586 if (IS_DMA64) {
587 pthru->flags |= MFI_FRAME_SGL64;
588 pthru->sge_count = megasas_make_sgl64(instance, scp,
589 &pthru->sgl);
590 } else
591 pthru->sge_count = megasas_make_sgl32(instance, scp,
592 &pthru->sgl);
593
594 /*
595 * Sense info specific
596 */
597 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
598 pthru->sense_buf_phys_addr_hi = 0;
599 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
600
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400601 /*
602 * Compute the total number of frames this command consumes. FW uses
603 * this number to pull sufficient number of frames from host memory.
604 */
bo yangd532dbe2008-03-17 03:36:43 -0400605 cmd->frame_count = megasas_get_frame_count(pthru->sge_count,
606 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400607
608 return cmd->frame_count;
609}
610
611/**
612 * megasas_build_ldio - Prepares IOs to logical devices
613 * @instance: Adapter soft state
614 * @scp: SCSI command
615 * @cmd: Command to to be prepared
616 *
617 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
618 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800619static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400620megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
621 struct megasas_cmd *cmd)
622{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400623 u32 device_id;
624 u8 sc = scp->cmnd[0];
625 u16 flags = 0;
626 struct megasas_io_frame *ldio;
627
628 device_id = MEGASAS_DEV_INDEX(instance, scp);
629 ldio = (struct megasas_io_frame *)cmd->frame;
630
631 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
632 flags = MFI_FRAME_DIR_WRITE;
633 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
634 flags = MFI_FRAME_DIR_READ;
635
636 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -0700637 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400638 */
639 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
640 ldio->cmd_status = 0x0;
641 ldio->scsi_status = 0x0;
642 ldio->target_id = device_id;
643 ldio->timeout = 0;
644 ldio->reserved_0 = 0;
645 ldio->pad_0 = 0;
646 ldio->flags = flags;
647 ldio->start_lba_hi = 0;
648 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
649
650 /*
651 * 6-byte READ(0x08) or WRITE(0x0A) cdb
652 */
653 if (scp->cmd_len == 6) {
654 ldio->lba_count = (u32) scp->cmnd[4];
655 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
656 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
657
658 ldio->start_lba_lo &= 0x1FFFFF;
659 }
660
661 /*
662 * 10-byte READ(0x28) or WRITE(0x2A) cdb
663 */
664 else if (scp->cmd_len == 10) {
665 ldio->lba_count = (u32) scp->cmnd[8] |
666 ((u32) scp->cmnd[7] << 8);
667 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
668 ((u32) scp->cmnd[3] << 16) |
669 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
670 }
671
672 /*
673 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
674 */
675 else if (scp->cmd_len == 12) {
676 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
677 ((u32) scp->cmnd[7] << 16) |
678 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
679
680 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
681 ((u32) scp->cmnd[3] << 16) |
682 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
683 }
684
685 /*
686 * 16-byte READ(0x88) or WRITE(0x8A) cdb
687 */
688 else if (scp->cmd_len == 16) {
689 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
690 ((u32) scp->cmnd[11] << 16) |
691 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
692
693 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
694 ((u32) scp->cmnd[7] << 16) |
695 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
696
697 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
698 ((u32) scp->cmnd[3] << 16) |
699 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
700
701 }
702
703 /*
704 * Construct SGL
705 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400706 if (IS_DMA64) {
707 ldio->flags |= MFI_FRAME_SGL64;
708 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
709 } else
710 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
711
712 /*
713 * Sense info specific
714 */
715 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
716 ldio->sense_buf_phys_addr_hi = 0;
717 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
718
Sumant Patrob1df99d2006-10-03 12:40:47 -0700719 /*
720 * Compute the total number of frames this command consumes. FW uses
721 * this number to pull sufficient number of frames from host memory.
722 */
bo yangd532dbe2008-03-17 03:36:43 -0400723 cmd->frame_count = megasas_get_frame_count(ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400724
725 return cmd->frame_count;
726}
727
728/**
Sumant Patrocb59aa62006-01-25 11:53:25 -0800729 * megasas_is_ldio - Checks if the cmd is for logical drive
730 * @scmd: SCSI command
731 *
732 * Called by megasas_queue_command to find out if the command to be queued
733 * is a logical drive command
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400734 */
Sumant Patrocb59aa62006-01-25 11:53:25 -0800735static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400736{
Sumant Patrocb59aa62006-01-25 11:53:25 -0800737 if (!MEGASAS_IS_LOGICAL(cmd))
738 return 0;
739 switch (cmd->cmnd[0]) {
740 case READ_10:
741 case WRITE_10:
742 case READ_12:
743 case WRITE_12:
744 case READ_6:
745 case WRITE_6:
746 case READ_16:
747 case WRITE_16:
748 return 1;
749 default:
750 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400751 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400752}
753
Sumant Patro658dced2006-10-03 13:09:14 -0700754 /**
755 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
756 * in FW
757 * @instance: Adapter soft state
758 */
759static inline void
760megasas_dump_pending_frames(struct megasas_instance *instance)
761{
762 struct megasas_cmd *cmd;
763 int i,n;
764 union megasas_sgl *mfi_sgl;
765 struct megasas_io_frame *ldio;
766 struct megasas_pthru_frame *pthru;
767 u32 sgcount;
768 u32 max_cmd = instance->max_fw_cmds;
769
770 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
771 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
772 if (IS_DMA64)
773 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
774 else
775 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
776
777 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
778 for (i = 0; i < max_cmd; i++) {
779 cmd = instance->cmd_list[i];
780 if(!cmd->scmd)
781 continue;
782 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
783 if (megasas_is_ldio(cmd->scmd)){
784 ldio = (struct megasas_io_frame *)cmd->frame;
785 mfi_sgl = &ldio->sgl;
786 sgcount = ldio->sge_count;
787 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);
788 }
789 else {
790 pthru = (struct megasas_pthru_frame *) cmd->frame;
791 mfi_sgl = &pthru->sgl;
792 sgcount = pthru->sge_count;
793 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);
794 }
795 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
796 for (n = 0; n < sgcount; n++){
797 if (IS_DMA64)
798 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) ;
799 else
800 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
801 }
802 }
803 printk(KERN_ERR "\n");
804 } /*for max_cmd*/
805 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
806 for (i = 0; i < max_cmd; i++) {
807
808 cmd = instance->cmd_list[i];
809
810 if(cmd->sync_cmd == 1){
811 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
812 }
813 }
814 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
815}
816
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400817/**
818 * megasas_queue_command - Queue entry point
819 * @scmd: SCSI command to be queued
820 * @done: Callback entry point
821 */
822static int
823megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
824{
825 u32 frame_count;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400826 struct megasas_cmd *cmd;
827 struct megasas_instance *instance;
828
829 instance = (struct megasas_instance *)
830 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -0800831
832 /* Don't process if we have already declared adapter dead */
833 if (instance->hw_crit_error)
834 return SCSI_MLQUEUE_HOST_BUSY;
835
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400836 scmd->scsi_done = done;
837 scmd->result = 0;
838
Sumant Patrocb59aa62006-01-25 11:53:25 -0800839 if (MEGASAS_IS_LOGICAL(scmd) &&
840 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
841 scmd->result = DID_BAD_TARGET << 16;
842 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400843 }
844
Sumant Patro02b01e02007-02-14 13:00:55 -0800845 switch (scmd->cmnd[0]) {
846 case SYNCHRONIZE_CACHE:
847 /*
848 * FW takes care of flush cache on its own
849 * No need to send it down
850 */
851 scmd->result = DID_OK << 16;
852 goto out_done;
853 default:
854 break;
855 }
856
Sumant Patrocb59aa62006-01-25 11:53:25 -0800857 cmd = megasas_get_cmd(instance);
858 if (!cmd)
859 return SCSI_MLQUEUE_HOST_BUSY;
860
861 /*
862 * Logical drive command
863 */
864 if (megasas_is_ldio(scmd))
865 frame_count = megasas_build_ldio(instance, scmd, cmd);
866 else
867 frame_count = megasas_build_dcdb(instance, scmd, cmd);
868
869 if (!frame_count)
870 goto out_return_cmd;
871
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400872 cmd->scmd = scmd;
Sumant Patro05e9ebb2007-05-17 05:47:51 -0700873 scmd->SCp.ptr = (char *)cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400874
875 /*
876 * Issue the command to the FW
877 */
Sumant Patroe4a082c2006-05-30 12:03:37 -0700878 atomic_inc(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400879
Sumant Patro1341c932006-01-25 12:02:40 -0800880 instance->instancet->fire_cmd(cmd->frame_phys_addr ,cmd->frame_count-1,instance->reg_set);
bo yangad84db22007-11-09 04:40:16 -0500881 /*
882 * Check if we have pend cmds to be completed
883 */
884 if (poll_mode_io && atomic_read(&instance->fw_outstanding))
885 tasklet_schedule(&instance->isr_tasklet);
886
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400887
888 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -0800889
890 out_return_cmd:
891 megasas_return_cmd(instance, cmd);
892 out_done:
893 done(scmd);
894 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400895}
896
Christoph Hellwig147aab62006-02-17 12:13:48 +0100897static int megasas_slave_configure(struct scsi_device *sdev)
898{
899 /*
900 * Don't export physical disk devices to the disk driver.
901 *
902 * FIXME: Currently we don't export them to the midlayer at all.
903 * That will be fixed once LSI engineers have audited the
904 * firmware for possible issues.
905 */
906 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS && sdev->type == TYPE_DISK)
907 return -ENXIO;
Christoph Hellwige5b3a652006-03-10 17:08:57 +0100908
909 /*
910 * The RAID firmware may require extended timeouts.
911 */
912 if (sdev->channel >= MEGASAS_MAX_PD_CHANNELS)
Sumant Patro05e9ebb2007-05-17 05:47:51 -0700913 sdev->timeout = MEGASAS_DEFAULT_CMD_TIMEOUT * HZ;
Christoph Hellwig147aab62006-02-17 12:13:48 +0100914 return 0;
915}
916
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400917/**
bo yang7343eb62007-11-09 04:35:44 -0500918 * megasas_complete_cmd_dpc - Returns FW's controller structure
919 * @instance_addr: Address of adapter soft state
920 *
921 * Tasklet to complete cmds
922 */
923static void megasas_complete_cmd_dpc(unsigned long instance_addr)
924{
925 u32 producer;
926 u32 consumer;
927 u32 context;
928 struct megasas_cmd *cmd;
929 struct megasas_instance *instance =
930 (struct megasas_instance *)instance_addr;
931 unsigned long flags;
932
933 /* If we have already declared adapter dead, donot complete cmds */
934 if (instance->hw_crit_error)
935 return;
936
937 spin_lock_irqsave(&instance->completion_lock, flags);
938
939 producer = *instance->producer;
940 consumer = *instance->consumer;
941
942 while (consumer != producer) {
943 context = instance->reply_queue[consumer];
944
945 cmd = instance->cmd_list[context];
946
947 megasas_complete_cmd(instance, cmd, DID_OK);
948
949 consumer++;
950 if (consumer == (instance->max_fw_cmds + 1)) {
951 consumer = 0;
952 }
953 }
954
955 *instance->consumer = producer;
956
957 spin_unlock_irqrestore(&instance->completion_lock, flags);
958
959 /*
960 * Check if we can restore can_queue
961 */
962 if (instance->flag & MEGASAS_FW_BUSY
963 && time_after(jiffies, instance->last_time + 5 * HZ)
964 && atomic_read(&instance->fw_outstanding) < 17) {
965
966 spin_lock_irqsave(instance->host->host_lock, flags);
967 instance->flag &= ~MEGASAS_FW_BUSY;
968 instance->host->can_queue =
969 instance->max_fw_cmds - MEGASAS_INT_CMDS;
970
971 spin_unlock_irqrestore(instance->host->host_lock, flags);
972 }
973}
974
975/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400976 * megasas_wait_for_outstanding - Wait for all outstanding cmds
977 * @instance: Adapter soft state
978 *
979 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
980 * complete all its outstanding commands. Returns error if one or more IOs
981 * are pending after this time period. It also marks the controller dead.
982 */
983static int megasas_wait_for_outstanding(struct megasas_instance *instance)
984{
985 int i;
986 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
987
988 for (i = 0; i < wait_time; i++) {
989
Sumant Patroe4a082c2006-05-30 12:03:37 -0700990 int outstanding = atomic_read(&instance->fw_outstanding);
991
992 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400993 break;
994
995 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
996 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -0700997 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -0500998 /*
999 * Call cmd completion routine. Cmd to be
1000 * be completed directly without depending on isr.
1001 */
1002 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001003 }
1004
1005 msleep(1000);
1006 }
1007
Sumant Patroe4a082c2006-05-30 12:03:37 -07001008 if (atomic_read(&instance->fw_outstanding)) {
Sumant Patroe3bbff92006-10-03 12:28:49 -07001009 /*
1010 * Send signal to FW to stop processing any pending cmds.
1011 * The controller will be taken offline by the OS now.
1012 */
1013 writel(MFI_STOP_ADP,
1014 &instance->reg_set->inbound_doorbell);
Sumant Patro658dced2006-10-03 13:09:14 -07001015 megasas_dump_pending_frames(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001016 instance->hw_crit_error = 1;
1017 return FAILED;
1018 }
1019
1020 return SUCCESS;
1021}
1022
1023/**
1024 * megasas_generic_reset - Generic reset routine
1025 * @scmd: Mid-layer SCSI command
1026 *
1027 * This routine implements a generic reset handler for device, bus and host
1028 * reset requests. Device, bus and host specific reset handlers can use this
1029 * function after they do their specific tasks.
1030 */
1031static int megasas_generic_reset(struct scsi_cmnd *scmd)
1032{
1033 int ret_val;
1034 struct megasas_instance *instance;
1035
1036 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1037
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001038 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
1039 scmd->serial_number, scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001040
1041 if (instance->hw_crit_error) {
1042 printk(KERN_ERR "megasas: cannot recover from previous reset "
1043 "failures\n");
1044 return FAILED;
1045 }
1046
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001047 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001048 if (ret_val == SUCCESS)
1049 printk(KERN_NOTICE "megasas: reset successful \n");
1050 else
1051 printk(KERN_ERR "megasas: failed to do reset\n");
1052
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001053 return ret_val;
1054}
1055
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001056/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001057 * megasas_reset_timer - quiesce the adapter if required
1058 * @scmd: scsi cmnd
1059 *
1060 * Sets the FW busy flag and reduces the host->can_queue if the
1061 * cmd has not been completed within the timeout period.
1062 */
1063static enum
1064scsi_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
1065{
1066 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
1067 struct megasas_instance *instance;
1068 unsigned long flags;
1069
1070 if (time_after(jiffies, scmd->jiffies_at_alloc +
1071 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
1072 return EH_NOT_HANDLED;
1073 }
1074
1075 instance = cmd->instance;
1076 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1077 /* FW is busy, throttle IO */
1078 spin_lock_irqsave(instance->host->host_lock, flags);
1079
1080 instance->host->can_queue = 16;
1081 instance->last_time = jiffies;
1082 instance->flag |= MEGASAS_FW_BUSY;
1083
1084 spin_unlock_irqrestore(instance->host->host_lock, flags);
1085 }
1086 return EH_RESET_TIMER;
1087}
1088
1089/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001090 * megasas_reset_device - Device reset handler entry point
1091 */
1092static int megasas_reset_device(struct scsi_cmnd *scmd)
1093{
1094 int ret;
1095
1096 /*
1097 * First wait for all commands to complete
1098 */
1099 ret = megasas_generic_reset(scmd);
1100
1101 return ret;
1102}
1103
1104/**
1105 * megasas_reset_bus_host - Bus & host reset handler entry point
1106 */
1107static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1108{
1109 int ret;
1110
1111 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01001112 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001113 */
1114 ret = megasas_generic_reset(scmd);
1115
1116 return ret;
1117}
1118
1119/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001120 * megasas_bios_param - Returns disk geometry for a disk
1121 * @sdev: device handle
1122 * @bdev: block device
1123 * @capacity: drive capacity
1124 * @geom: geometry parameters
1125 */
1126static int
1127megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1128 sector_t capacity, int geom[])
1129{
1130 int heads;
1131 int sectors;
1132 sector_t cylinders;
1133 unsigned long tmp;
1134 /* Default heads (64) & sectors (32) */
1135 heads = 64;
1136 sectors = 32;
1137
1138 tmp = heads * sectors;
1139 cylinders = capacity;
1140
1141 sector_div(cylinders, tmp);
1142
1143 /*
1144 * Handle extended translation size for logical drives > 1Gb
1145 */
1146
1147 if (capacity >= 0x200000) {
1148 heads = 255;
1149 sectors = 63;
1150 tmp = heads*sectors;
1151 cylinders = capacity;
1152 sector_div(cylinders, tmp);
1153 }
1154
1155 geom[0] = heads;
1156 geom[1] = sectors;
1157 geom[2] = cylinders;
1158
1159 return 0;
1160}
1161
1162/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001163 * megasas_service_aen - Processes an event notification
1164 * @instance: Adapter soft state
1165 * @cmd: AEN command completed by the ISR
1166 *
1167 * For AEN, driver sends a command down to FW that is held by the FW till an
1168 * event occurs. When an event of interest occurs, FW completes the command
1169 * that it was previously holding.
1170 *
1171 * This routines sends SIGIO signal to processes that have registered with the
1172 * driver for AEN.
1173 */
1174static void
1175megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1176{
1177 /*
1178 * Don't signal app if it is just an aborted previously registered aen
1179 */
1180 if (!cmd->abort_aen)
1181 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
1182 else
1183 cmd->abort_aen = 0;
1184
1185 instance->aen_cmd = NULL;
1186 megasas_return_cmd(instance, cmd);
1187}
1188
1189/*
1190 * Scsi host template for megaraid_sas driver
1191 */
1192static struct scsi_host_template megasas_template = {
1193
1194 .module = THIS_MODULE,
bo yangf28cd7c2007-11-09 04:44:56 -05001195 .name = "LSI SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001196 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01001197 .slave_configure = megasas_slave_configure,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001198 .queuecommand = megasas_queue_command,
1199 .eh_device_reset_handler = megasas_reset_device,
1200 .eh_bus_reset_handler = megasas_reset_bus_host,
1201 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001202 .eh_timed_out = megasas_reset_timer,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001203 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001204 .use_clustering = ENABLE_CLUSTERING,
1205};
1206
1207/**
1208 * megasas_complete_int_cmd - Completes an internal command
1209 * @instance: Adapter soft state
1210 * @cmd: Command to be completed
1211 *
1212 * The megasas_issue_blocked_cmd() function waits for a command to complete
1213 * after it issues a command. This function wakes up that waiting routine by
1214 * calling wake_up() on the wait queue.
1215 */
1216static void
1217megasas_complete_int_cmd(struct megasas_instance *instance,
1218 struct megasas_cmd *cmd)
1219{
1220 cmd->cmd_status = cmd->frame->io.cmd_status;
1221
1222 if (cmd->cmd_status == ENODATA) {
1223 cmd->cmd_status = 0;
1224 }
1225 wake_up(&instance->int_cmd_wait_q);
1226}
1227
1228/**
1229 * megasas_complete_abort - Completes aborting a command
1230 * @instance: Adapter soft state
1231 * @cmd: Cmd that was issued to abort another cmd
1232 *
1233 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
1234 * after it issues an abort on a previously issued command. This function
1235 * wakes up all functions waiting on the same wait queue.
1236 */
1237static void
1238megasas_complete_abort(struct megasas_instance *instance,
1239 struct megasas_cmd *cmd)
1240{
1241 if (cmd->sync_cmd) {
1242 cmd->sync_cmd = 0;
1243 cmd->cmd_status = 0;
1244 wake_up(&instance->abort_cmd_wait_q);
1245 }
1246
1247 return;
1248}
1249
1250/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001251 * megasas_complete_cmd - Completes a command
1252 * @instance: Adapter soft state
1253 * @cmd: Command to be completed
1254 * @alt_status: If non-zero, use this value as status to
1255 * SCSI mid-layer instead of the value returned
1256 * by the FW. This should be used if caller wants
1257 * an alternate status (as in the case of aborted
1258 * commands)
1259 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001260static void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001261megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
1262 u8 alt_status)
1263{
1264 int exception = 0;
1265 struct megasas_header *hdr = &cmd->frame->hdr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001266
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001267 if (cmd->scmd)
1268 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001269
1270 switch (hdr->cmd) {
1271
1272 case MFI_CMD_PD_SCSI_IO:
1273 case MFI_CMD_LD_SCSI_IO:
1274
1275 /*
1276 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1277 * issued either through an IO path or an IOCTL path. If it
1278 * was via IOCTL, we will send it to internal completion.
1279 */
1280 if (cmd->sync_cmd) {
1281 cmd->sync_cmd = 0;
1282 megasas_complete_int_cmd(instance, cmd);
1283 break;
1284 }
1285
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001286 case MFI_CMD_LD_READ:
1287 case MFI_CMD_LD_WRITE:
1288
1289 if (alt_status) {
1290 cmd->scmd->result = alt_status << 16;
1291 exception = 1;
1292 }
1293
1294 if (exception) {
1295
Sumant Patroe4a082c2006-05-30 12:03:37 -07001296 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001297
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001298 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001299 cmd->scmd->scsi_done(cmd->scmd);
1300 megasas_return_cmd(instance, cmd);
1301
1302 break;
1303 }
1304
1305 switch (hdr->cmd_status) {
1306
1307 case MFI_STAT_OK:
1308 cmd->scmd->result = DID_OK << 16;
1309 break;
1310
1311 case MFI_STAT_SCSI_IO_FAILED:
1312 case MFI_STAT_LD_INIT_IN_PROGRESS:
1313 cmd->scmd->result =
1314 (DID_ERROR << 16) | hdr->scsi_status;
1315 break;
1316
1317 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1318
1319 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1320
1321 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1322 memset(cmd->scmd->sense_buffer, 0,
1323 SCSI_SENSE_BUFFERSIZE);
1324 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1325 hdr->sense_len);
1326
1327 cmd->scmd->result |= DRIVER_SENSE << 24;
1328 }
1329
1330 break;
1331
1332 case MFI_STAT_LD_OFFLINE:
1333 case MFI_STAT_DEVICE_NOT_FOUND:
1334 cmd->scmd->result = DID_BAD_TARGET << 16;
1335 break;
1336
1337 default:
1338 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1339 hdr->cmd_status);
1340 cmd->scmd->result = DID_ERROR << 16;
1341 break;
1342 }
1343
Sumant Patroe4a082c2006-05-30 12:03:37 -07001344 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001345
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001346 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001347 cmd->scmd->scsi_done(cmd->scmd);
1348 megasas_return_cmd(instance, cmd);
1349
1350 break;
1351
1352 case MFI_CMD_SMP:
1353 case MFI_CMD_STP:
1354 case MFI_CMD_DCMD:
1355
1356 /*
1357 * See if got an event notification
1358 */
1359 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1360 megasas_service_aen(instance, cmd);
1361 else
1362 megasas_complete_int_cmd(instance, cmd);
1363
1364 break;
1365
1366 case MFI_CMD_ABORT:
1367 /*
1368 * Cmd issued to abort another cmd returned
1369 */
1370 megasas_complete_abort(instance, cmd);
1371 break;
1372
1373 default:
1374 printk("megasas: Unknown command completed! [0x%X]\n",
1375 hdr->cmd);
1376 break;
1377 }
1378}
1379
1380/**
1381 * megasas_deplete_reply_queue - Processes all completed commands
1382 * @instance: Adapter soft state
1383 * @alt_status: Alternate status to be returned to
1384 * SCSI mid-layer instead of the status
1385 * returned by the FW
1386 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001387static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001388megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1389{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001390 /*
1391 * Check if it is our interrupt
Sumant Patro1341c932006-01-25 12:02:40 -08001392 * Clear the interrupt
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001393 */
Sumant Patro1341c932006-01-25 12:02:40 -08001394 if(instance->instancet->clear_intr(instance->reg_set))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001395 return IRQ_NONE;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001396
Sumant Patroaf37acf2007-02-14 12:34:46 -08001397 if (instance->hw_crit_error)
1398 goto out_done;
Sumant Patro5d018ad2006-10-03 13:13:18 -07001399 /*
1400 * Schedule the tasklet for cmd completion
1401 */
1402 tasklet_schedule(&instance->isr_tasklet);
Sumant Patroaf37acf2007-02-14 12:34:46 -08001403out_done:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001404 return IRQ_HANDLED;
1405}
1406
1407/**
1408 * megasas_isr - isr entry point
1409 */
David Howells7d12e782006-10-05 14:55:46 +01001410static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001411{
1412 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1413 DID_OK);
1414}
1415
1416/**
1417 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08001418 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001419 *
1420 * During the initialization, FW passes can potentially be in any one of
1421 * several possible states. If the FW in operational, waiting-for-handshake
1422 * states, driver must take steps to bring it to ready state. Otherwise, it
1423 * has to wait for the ready state.
1424 */
1425static int
Sumant Patro1341c932006-01-25 12:02:40 -08001426megasas_transition_to_ready(struct megasas_instance* instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001427{
1428 int i;
1429 u8 max_wait;
1430 u32 fw_state;
1431 u32 cur_state;
1432
Sumant Patro1341c932006-01-25 12:02:40 -08001433 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001434
Sumant Patroe3bbff92006-10-03 12:28:49 -07001435 if (fw_state != MFI_STATE_READY)
1436 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1437 " state\n");
1438
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001439 while (fw_state != MFI_STATE_READY) {
1440
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001441 switch (fw_state) {
1442
1443 case MFI_STATE_FAULT:
1444
1445 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1446 return -ENODEV;
1447
1448 case MFI_STATE_WAIT_HANDSHAKE:
1449 /*
1450 * Set the CLR bit in inbound doorbell
1451 */
Sumant Patroe3bbff92006-10-03 12:28:49 -07001452 writel(MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
Sumant Patro1341c932006-01-25 12:02:40 -08001453 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001454
1455 max_wait = 2;
1456 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1457 break;
1458
Sumant Patroe3bbff92006-10-03 12:28:49 -07001459 case MFI_STATE_BOOT_MESSAGE_PENDING:
1460 writel(MFI_INIT_HOTPLUG,
1461 &instance->reg_set->inbound_doorbell);
1462
1463 max_wait = 10;
1464 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
1465 break;
1466
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001467 case MFI_STATE_OPERATIONAL:
1468 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07001469 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001470 */
Sumant Patrob274cab2006-10-03 12:52:12 -07001471 instance->instancet->disable_intr(instance->reg_set);
Sumant Patroe3bbff92006-10-03 12:28:49 -07001472 writel(MFI_RESET_FLAGS, &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001473
1474 max_wait = 10;
1475 cur_state = MFI_STATE_OPERATIONAL;
1476 break;
1477
1478 case MFI_STATE_UNDEFINED:
1479 /*
1480 * This state should not last for more than 2 seconds
1481 */
1482 max_wait = 2;
1483 cur_state = MFI_STATE_UNDEFINED;
1484 break;
1485
1486 case MFI_STATE_BB_INIT:
1487 max_wait = 2;
1488 cur_state = MFI_STATE_BB_INIT;
1489 break;
1490
1491 case MFI_STATE_FW_INIT:
1492 max_wait = 20;
1493 cur_state = MFI_STATE_FW_INIT;
1494 break;
1495
1496 case MFI_STATE_FW_INIT_2:
1497 max_wait = 20;
1498 cur_state = MFI_STATE_FW_INIT_2;
1499 break;
1500
1501 case MFI_STATE_DEVICE_SCAN:
1502 max_wait = 20;
1503 cur_state = MFI_STATE_DEVICE_SCAN;
1504 break;
1505
1506 case MFI_STATE_FLUSH_CACHE:
1507 max_wait = 20;
1508 cur_state = MFI_STATE_FLUSH_CACHE;
1509 break;
1510
1511 default:
1512 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1513 fw_state);
1514 return -ENODEV;
1515 }
1516
1517 /*
1518 * The cur_state should not last for more than max_wait secs
1519 */
1520 for (i = 0; i < (max_wait * 1000); i++) {
Sumant Patro1341c932006-01-25 12:02:40 -08001521 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1522 MFI_STATE_MASK ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001523
1524 if (fw_state == cur_state) {
1525 msleep(1);
1526 } else
1527 break;
1528 }
1529
1530 /*
1531 * Return error if fw_state hasn't changed after max_wait
1532 */
1533 if (fw_state == cur_state) {
1534 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1535 "in %d secs\n", fw_state, max_wait);
1536 return -ENODEV;
1537 }
1538 };
Sumant Patroe3bbff92006-10-03 12:28:49 -07001539 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001540
1541 return 0;
1542}
1543
1544/**
1545 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1546 * @instance: Adapter soft state
1547 */
1548static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1549{
1550 int i;
1551 u32 max_cmd = instance->max_fw_cmds;
1552 struct megasas_cmd *cmd;
1553
1554 if (!instance->frame_dma_pool)
1555 return;
1556
1557 /*
1558 * Return all frames to pool
1559 */
1560 for (i = 0; i < max_cmd; i++) {
1561
1562 cmd = instance->cmd_list[i];
1563
1564 if (cmd->frame)
1565 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1566 cmd->frame_phys_addr);
1567
1568 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07001569 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001570 cmd->sense_phys_addr);
1571 }
1572
1573 /*
1574 * Now destroy the pool itself
1575 */
1576 pci_pool_destroy(instance->frame_dma_pool);
1577 pci_pool_destroy(instance->sense_dma_pool);
1578
1579 instance->frame_dma_pool = NULL;
1580 instance->sense_dma_pool = NULL;
1581}
1582
1583/**
1584 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1585 * @instance: Adapter soft state
1586 *
1587 * Each command packet has an embedded DMA memory buffer that is used for
1588 * filling MFI frame and the SG list that immediately follows the frame. This
1589 * function creates those DMA memory buffers for each command packet by using
1590 * PCI pool facility.
1591 */
1592static int megasas_create_frame_pool(struct megasas_instance *instance)
1593{
1594 int i;
1595 u32 max_cmd;
1596 u32 sge_sz;
1597 u32 sgl_sz;
1598 u32 total_sz;
1599 u32 frame_count;
1600 struct megasas_cmd *cmd;
1601
1602 max_cmd = instance->max_fw_cmds;
1603
1604 /*
1605 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1606 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1607 */
1608 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1609 sizeof(struct megasas_sge32);
1610
1611 /*
1612 * Calculated the number of 64byte frames required for SGL
1613 */
1614 sgl_sz = sge_sz * instance->max_num_sge;
1615 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1616
1617 /*
1618 * We need one extra frame for the MFI command
1619 */
1620 frame_count++;
1621
1622 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1623 /*
1624 * Use DMA pool facility provided by PCI layer
1625 */
1626 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1627 instance->pdev, total_sz, 64,
1628 0);
1629
1630 if (!instance->frame_dma_pool) {
1631 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1632 return -ENOMEM;
1633 }
1634
1635 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1636 instance->pdev, 128, 4, 0);
1637
1638 if (!instance->sense_dma_pool) {
1639 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1640
1641 pci_pool_destroy(instance->frame_dma_pool);
1642 instance->frame_dma_pool = NULL;
1643
1644 return -ENOMEM;
1645 }
1646
1647 /*
1648 * Allocate and attach a frame to each of the commands in cmd_list.
1649 * By making cmd->index as the context instead of the &cmd, we can
1650 * always use 32bit context regardless of the architecture
1651 */
1652 for (i = 0; i < max_cmd; i++) {
1653
1654 cmd = instance->cmd_list[i];
1655
1656 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1657 GFP_KERNEL, &cmd->frame_phys_addr);
1658
1659 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1660 GFP_KERNEL, &cmd->sense_phys_addr);
1661
1662 /*
1663 * megasas_teardown_frame_pool() takes care of freeing
1664 * whatever has been allocated
1665 */
1666 if (!cmd->frame || !cmd->sense) {
1667 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1668 megasas_teardown_frame_pool(instance);
1669 return -ENOMEM;
1670 }
1671
1672 cmd->frame->io.context = cmd->index;
1673 }
1674
1675 return 0;
1676}
1677
1678/**
1679 * megasas_free_cmds - Free all the cmds in the free cmd pool
1680 * @instance: Adapter soft state
1681 */
1682static void megasas_free_cmds(struct megasas_instance *instance)
1683{
1684 int i;
1685 /* First free the MFI frame pool */
1686 megasas_teardown_frame_pool(instance);
1687
1688 /* Free all the commands in the cmd_list */
1689 for (i = 0; i < instance->max_fw_cmds; i++)
1690 kfree(instance->cmd_list[i]);
1691
1692 /* Free the cmd_list buffer itself */
1693 kfree(instance->cmd_list);
1694 instance->cmd_list = NULL;
1695
1696 INIT_LIST_HEAD(&instance->cmd_pool);
1697}
1698
1699/**
1700 * megasas_alloc_cmds - Allocates the command packets
1701 * @instance: Adapter soft state
1702 *
1703 * Each command that is issued to the FW, whether IO commands from the OS or
1704 * internal commands like IOCTLs, are wrapped in local data structure called
1705 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1706 * the FW.
1707 *
1708 * Each frame has a 32-bit field called context (tag). This context is used
1709 * to get back the megasas_cmd from the frame when a frame gets completed in
1710 * the ISR. Typically the address of the megasas_cmd itself would be used as
1711 * the context. But we wanted to keep the differences between 32 and 64 bit
1712 * systems to the mininum. We always use 32 bit integers for the context. In
1713 * this driver, the 32 bit values are the indices into an array cmd_list.
1714 * This array is used only to look up the megasas_cmd given the context. The
1715 * free commands themselves are maintained in a linked list called cmd_pool.
1716 */
1717static int megasas_alloc_cmds(struct megasas_instance *instance)
1718{
1719 int i;
1720 int j;
1721 u32 max_cmd;
1722 struct megasas_cmd *cmd;
1723
1724 max_cmd = instance->max_fw_cmds;
1725
1726 /*
1727 * instance->cmd_list is an array of struct megasas_cmd pointers.
1728 * Allocate the dynamic array first and then allocate individual
1729 * commands.
1730 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001731 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001732
1733 if (!instance->cmd_list) {
1734 printk(KERN_DEBUG "megasas: out of memory\n");
1735 return -ENOMEM;
1736 }
1737
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001738
1739 for (i = 0; i < max_cmd; i++) {
1740 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
1741 GFP_KERNEL);
1742
1743 if (!instance->cmd_list[i]) {
1744
1745 for (j = 0; j < i; j++)
1746 kfree(instance->cmd_list[j]);
1747
1748 kfree(instance->cmd_list);
1749 instance->cmd_list = NULL;
1750
1751 return -ENOMEM;
1752 }
1753 }
1754
1755 /*
1756 * Add all the commands to command pool (instance->cmd_pool)
1757 */
1758 for (i = 0; i < max_cmd; i++) {
1759 cmd = instance->cmd_list[i];
1760 memset(cmd, 0, sizeof(struct megasas_cmd));
1761 cmd->index = i;
1762 cmd->instance = instance;
1763
1764 list_add_tail(&cmd->list, &instance->cmd_pool);
1765 }
1766
1767 /*
1768 * Create a frame pool and assign one frame to each cmd
1769 */
1770 if (megasas_create_frame_pool(instance)) {
1771 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
1772 megasas_free_cmds(instance);
1773 }
1774
1775 return 0;
1776}
1777
1778/**
1779 * megasas_get_controller_info - Returns FW's controller structure
1780 * @instance: Adapter soft state
1781 * @ctrl_info: Controller information structure
1782 *
1783 * Issues an internal command (DCMD) to get the FW's controller structure.
1784 * This information is mainly used to find out the maximum IO transfer per
1785 * command supported by the FW.
1786 */
1787static int
1788megasas_get_ctrl_info(struct megasas_instance *instance,
1789 struct megasas_ctrl_info *ctrl_info)
1790{
1791 int ret = 0;
1792 struct megasas_cmd *cmd;
1793 struct megasas_dcmd_frame *dcmd;
1794 struct megasas_ctrl_info *ci;
1795 dma_addr_t ci_h = 0;
1796
1797 cmd = megasas_get_cmd(instance);
1798
1799 if (!cmd) {
1800 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
1801 return -ENOMEM;
1802 }
1803
1804 dcmd = &cmd->frame->dcmd;
1805
1806 ci = pci_alloc_consistent(instance->pdev,
1807 sizeof(struct megasas_ctrl_info), &ci_h);
1808
1809 if (!ci) {
1810 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
1811 megasas_return_cmd(instance, cmd);
1812 return -ENOMEM;
1813 }
1814
1815 memset(ci, 0, sizeof(*ci));
1816 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1817
1818 dcmd->cmd = MFI_CMD_DCMD;
1819 dcmd->cmd_status = 0xFF;
1820 dcmd->sge_count = 1;
1821 dcmd->flags = MFI_FRAME_DIR_READ;
1822 dcmd->timeout = 0;
1823 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
1824 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
1825 dcmd->sgl.sge32[0].phys_addr = ci_h;
1826 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
1827
1828 if (!megasas_issue_polled(instance, cmd)) {
1829 ret = 0;
1830 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
1831 } else {
1832 ret = -1;
1833 }
1834
1835 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
1836 ci, ci_h);
1837
1838 megasas_return_cmd(instance, cmd);
1839 return ret;
1840}
1841
1842/**
bo yang31ea7082007-11-07 12:09:50 -05001843 * megasas_issue_init_mfi - Initializes the FW
1844 * @instance: Adapter soft state
1845 *
1846 * Issues the INIT MFI cmd
1847 */
1848static int
1849megasas_issue_init_mfi(struct megasas_instance *instance)
1850{
1851 u32 context;
1852
1853 struct megasas_cmd *cmd;
1854
1855 struct megasas_init_frame *init_frame;
1856 struct megasas_init_queue_info *initq_info;
1857 dma_addr_t init_frame_h;
1858 dma_addr_t initq_info_h;
1859
1860 /*
1861 * Prepare a init frame. Note the init frame points to queue info
1862 * structure. Each frame has SGL allocated after first 64 bytes. For
1863 * this frame - since we don't need any SGL - we use SGL's space as
1864 * queue info structure
1865 *
1866 * We will not get a NULL command below. We just created the pool.
1867 */
1868 cmd = megasas_get_cmd(instance);
1869
1870 init_frame = (struct megasas_init_frame *)cmd->frame;
1871 initq_info = (struct megasas_init_queue_info *)
1872 ((unsigned long)init_frame + 64);
1873
1874 init_frame_h = cmd->frame_phys_addr;
1875 initq_info_h = init_frame_h + 64;
1876
1877 context = init_frame->context;
1878 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
1879 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
1880 init_frame->context = context;
1881
1882 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
1883 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
1884
1885 initq_info->producer_index_phys_addr_lo = instance->producer_h;
1886 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
1887
1888 init_frame->cmd = MFI_CMD_INIT;
1889 init_frame->cmd_status = 0xFF;
1890 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
1891
1892 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
1893
1894 /*
1895 * disable the intr before firing the init frame to FW
1896 */
1897 instance->instancet->disable_intr(instance->reg_set);
1898
1899 /*
1900 * Issue the init frame in polled mode
1901 */
1902
1903 if (megasas_issue_polled(instance, cmd)) {
1904 printk(KERN_ERR "megasas: Failed to init firmware\n");
1905 megasas_return_cmd(instance, cmd);
1906 goto fail_fw_init;
1907 }
1908
1909 megasas_return_cmd(instance, cmd);
1910
1911 return 0;
1912
1913fail_fw_init:
1914 return -EINVAL;
1915}
1916
1917/**
bo yangad84db22007-11-09 04:40:16 -05001918 * megasas_start_timer - Initializes a timer object
1919 * @instance: Adapter soft state
1920 * @timer: timer object to be initialized
1921 * @fn: timer function
1922 * @interval: time interval between timer function call
1923 */
1924static inline void
1925megasas_start_timer(struct megasas_instance *instance,
1926 struct timer_list *timer,
1927 void *fn, unsigned long interval)
1928{
1929 init_timer(timer);
1930 timer->expires = jiffies + interval;
1931 timer->data = (unsigned long)instance;
1932 timer->function = fn;
1933 add_timer(timer);
1934}
1935
1936/**
1937 * megasas_io_completion_timer - Timer fn
1938 * @instance_addr: Address of adapter soft state
1939 *
1940 * Schedules tasklet for cmd completion
1941 * if poll_mode_io is set
1942 */
1943static void
1944megasas_io_completion_timer(unsigned long instance_addr)
1945{
1946 struct megasas_instance *instance =
1947 (struct megasas_instance *)instance_addr;
1948
1949 if (atomic_read(&instance->fw_outstanding))
1950 tasklet_schedule(&instance->isr_tasklet);
1951
1952 /* Restart timer */
1953 if (poll_mode_io)
1954 mod_timer(&instance->io_completion_timer,
1955 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
1956}
1957
1958/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001959 * megasas_init_mfi - Initializes the FW
1960 * @instance: Adapter soft state
1961 *
1962 * This is the main function for initializing MFI firmware.
1963 */
1964static int megasas_init_mfi(struct megasas_instance *instance)
1965{
1966 u32 context_sz;
1967 u32 reply_q_sz;
1968 u32 max_sectors_1;
1969 u32 max_sectors_2;
bo yang14faea92007-11-09 04:14:00 -05001970 u32 tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001971 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001972 struct megasas_ctrl_info *ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001973 /*
1974 * Map the message registers
1975 */
1976 instance->base_addr = pci_resource_start(instance->pdev, 0);
1977
bo yangf28cd7c2007-11-09 04:44:56 -05001978 if (pci_request_regions(instance->pdev, "megasas: LSI")) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001979 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
1980 return -EBUSY;
1981 }
1982
1983 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
1984
1985 if (!instance->reg_set) {
1986 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
1987 goto fail_ioremap;
1988 }
1989
1990 reg_set = instance->reg_set;
1991
Sumant Patrof9876f02006-02-03 15:34:35 -08001992 switch(instance->pdev->device)
1993 {
1994 case PCI_DEVICE_ID_LSI_SAS1078R:
1995 instance->instancet = &megasas_instance_template_ppc;
1996 break;
1997 case PCI_DEVICE_ID_LSI_SAS1064R:
1998 case PCI_DEVICE_ID_DELL_PERC5:
1999 default:
2000 instance->instancet = &megasas_instance_template_xscale;
2001 break;
2002 }
Sumant Patro1341c932006-01-25 12:02:40 -08002003
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002004 /*
2005 * We expect the FW state to be READY
2006 */
Sumant Patro1341c932006-01-25 12:02:40 -08002007 if (megasas_transition_to_ready(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002008 goto fail_ready_state;
2009
2010 /*
2011 * Get various operational parameters from status register
2012 */
Sumant Patro1341c932006-01-25 12:02:40 -08002013 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07002014 /*
2015 * Reduce the max supported cmds by 1. This is to ensure that the
2016 * reply_q_sz (1 more than the max cmd that driver may send)
2017 * does not exceed max cmds that the FW can support
2018 */
2019 instance->max_fw_cmds = instance->max_fw_cmds-1;
Sumant Patro1341c932006-01-25 12:02:40 -08002020 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
2021 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002022 /*
2023 * Create a pool of commands
2024 */
2025 if (megasas_alloc_cmds(instance))
2026 goto fail_alloc_cmds;
2027
2028 /*
2029 * Allocate memory for reply queue. Length of reply queue should
2030 * be _one_ more than the maximum commands handled by the firmware.
2031 *
2032 * Note: When FW completes commands, it places corresponding contex
2033 * values in this circular reply queue. This circular queue is a fairly
2034 * typical producer-consumer queue. FW is the producer (of completed
2035 * commands) and the driver is the consumer.
2036 */
2037 context_sz = sizeof(u32);
2038 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
2039
2040 instance->reply_queue = pci_alloc_consistent(instance->pdev,
2041 reply_q_sz,
2042 &instance->reply_queue_h);
2043
2044 if (!instance->reply_queue) {
2045 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
2046 goto fail_reply_queue;
2047 }
2048
bo yang31ea7082007-11-07 12:09:50 -05002049 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002050 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002051
2052 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
2053
2054 /*
2055 * Compute the max allowed sectors per IO: The controller info has two
2056 * limits on max sectors. Driver should use the minimum of these two.
2057 *
2058 * 1 << stripe_sz_ops.min = max sectors per strip
2059 *
2060 * Note that older firmwares ( < FW ver 30) didn't report information
2061 * to calculate max_sectors_1. So the number ended up as zero always.
2062 */
bo yang14faea92007-11-09 04:14:00 -05002063 tmp_sectors = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002064 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
2065
2066 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
2067 ctrl_info->max_strips_per_io;
2068 max_sectors_2 = ctrl_info->max_request_size;
2069
bo yang14faea92007-11-09 04:14:00 -05002070 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
2071 }
2072
2073 instance->max_sectors_per_req = instance->max_num_sge *
2074 PAGE_SIZE / 512;
2075 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
2076 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002077
2078 kfree(ctrl_info);
2079
Sumant Patro5d018ad2006-10-03 13:13:18 -07002080 /*
2081 * Setup tasklet for cmd completion
2082 */
2083
bo yangad84db22007-11-09 04:40:16 -05002084 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2085 (unsigned long)instance);
2086
2087 /* Initialize the cmd completion timer */
2088 if (poll_mode_io)
2089 megasas_start_timer(instance, &instance->io_completion_timer,
2090 megasas_io_completion_timer,
2091 MEGASAS_COMPLETION_TIMER_INTERVAL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002092 return 0;
2093
2094 fail_fw_init:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002095
2096 pci_free_consistent(instance->pdev, reply_q_sz,
2097 instance->reply_queue, instance->reply_queue_h);
2098 fail_reply_queue:
2099 megasas_free_cmds(instance);
2100
2101 fail_alloc_cmds:
2102 fail_ready_state:
2103 iounmap(instance->reg_set);
2104
2105 fail_ioremap:
2106 pci_release_regions(instance->pdev);
2107
2108 return -EINVAL;
2109}
2110
2111/**
2112 * megasas_release_mfi - Reverses the FW initialization
2113 * @intance: Adapter soft state
2114 */
2115static void megasas_release_mfi(struct megasas_instance *instance)
2116{
2117 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
2118
2119 pci_free_consistent(instance->pdev, reply_q_sz,
2120 instance->reply_queue, instance->reply_queue_h);
2121
2122 megasas_free_cmds(instance);
2123
2124 iounmap(instance->reg_set);
2125
2126 pci_release_regions(instance->pdev);
2127}
2128
2129/**
2130 * megasas_get_seq_num - Gets latest event sequence numbers
2131 * @instance: Adapter soft state
2132 * @eli: FW event log sequence numbers information
2133 *
2134 * FW maintains a log of all events in a non-volatile area. Upper layers would
2135 * usually find out the latest sequence number of the events, the seq number at
2136 * the boot etc. They would "read" all the events below the latest seq number
2137 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
2138 * number), they would subsribe to AEN (asynchronous event notification) and
2139 * wait for the events to happen.
2140 */
2141static int
2142megasas_get_seq_num(struct megasas_instance *instance,
2143 struct megasas_evt_log_info *eli)
2144{
2145 struct megasas_cmd *cmd;
2146 struct megasas_dcmd_frame *dcmd;
2147 struct megasas_evt_log_info *el_info;
2148 dma_addr_t el_info_h = 0;
2149
2150 cmd = megasas_get_cmd(instance);
2151
2152 if (!cmd) {
2153 return -ENOMEM;
2154 }
2155
2156 dcmd = &cmd->frame->dcmd;
2157 el_info = pci_alloc_consistent(instance->pdev,
2158 sizeof(struct megasas_evt_log_info),
2159 &el_info_h);
2160
2161 if (!el_info) {
2162 megasas_return_cmd(instance, cmd);
2163 return -ENOMEM;
2164 }
2165
2166 memset(el_info, 0, sizeof(*el_info));
2167 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2168
2169 dcmd->cmd = MFI_CMD_DCMD;
2170 dcmd->cmd_status = 0x0;
2171 dcmd->sge_count = 1;
2172 dcmd->flags = MFI_FRAME_DIR_READ;
2173 dcmd->timeout = 0;
2174 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
2175 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
2176 dcmd->sgl.sge32[0].phys_addr = el_info_h;
2177 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
2178
2179 megasas_issue_blocked_cmd(instance, cmd);
2180
2181 /*
2182 * Copy the data back into callers buffer
2183 */
2184 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
2185
2186 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
2187 el_info, el_info_h);
2188
2189 megasas_return_cmd(instance, cmd);
2190
2191 return 0;
2192}
2193
2194/**
2195 * megasas_register_aen - Registers for asynchronous event notification
2196 * @instance: Adapter soft state
2197 * @seq_num: The starting sequence number
2198 * @class_locale: Class of the event
2199 *
2200 * This function subscribes for AEN for events beyond the @seq_num. It requests
2201 * to be notified if and only if the event is of type @class_locale
2202 */
2203static int
2204megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
2205 u32 class_locale_word)
2206{
2207 int ret_val;
2208 struct megasas_cmd *cmd;
2209 struct megasas_dcmd_frame *dcmd;
2210 union megasas_evt_class_locale curr_aen;
2211 union megasas_evt_class_locale prev_aen;
2212
2213 /*
2214 * If there an AEN pending already (aen_cmd), check if the
2215 * class_locale of that pending AEN is inclusive of the new
2216 * AEN request we currently have. If it is, then we don't have
2217 * to do anything. In other words, whichever events the current
2218 * AEN request is subscribing to, have already been subscribed
2219 * to.
2220 *
2221 * If the old_cmd is _not_ inclusive, then we have to abort
2222 * that command, form a class_locale that is superset of both
2223 * old and current and re-issue to the FW
2224 */
2225
2226 curr_aen.word = class_locale_word;
2227
2228 if (instance->aen_cmd) {
2229
2230 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
2231
2232 /*
2233 * A class whose enum value is smaller is inclusive of all
2234 * higher values. If a PROGRESS (= -1) was previously
2235 * registered, then a new registration requests for higher
2236 * classes need not be sent to FW. They are automatically
2237 * included.
2238 *
2239 * Locale numbers don't have such hierarchy. They are bitmap
2240 * values
2241 */
2242 if ((prev_aen.members.class <= curr_aen.members.class) &&
2243 !((prev_aen.members.locale & curr_aen.members.locale) ^
2244 curr_aen.members.locale)) {
2245 /*
2246 * Previously issued event registration includes
2247 * current request. Nothing to do.
2248 */
2249 return 0;
2250 } else {
2251 curr_aen.members.locale |= prev_aen.members.locale;
2252
2253 if (prev_aen.members.class < curr_aen.members.class)
2254 curr_aen.members.class = prev_aen.members.class;
2255
2256 instance->aen_cmd->abort_aen = 1;
2257 ret_val = megasas_issue_blocked_abort_cmd(instance,
2258 instance->
2259 aen_cmd);
2260
2261 if (ret_val) {
2262 printk(KERN_DEBUG "megasas: Failed to abort "
2263 "previous AEN command\n");
2264 return ret_val;
2265 }
2266 }
2267 }
2268
2269 cmd = megasas_get_cmd(instance);
2270
2271 if (!cmd)
2272 return -ENOMEM;
2273
2274 dcmd = &cmd->frame->dcmd;
2275
2276 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
2277
2278 /*
2279 * Prepare DCMD for aen registration
2280 */
2281 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2282
2283 dcmd->cmd = MFI_CMD_DCMD;
2284 dcmd->cmd_status = 0x0;
2285 dcmd->sge_count = 1;
2286 dcmd->flags = MFI_FRAME_DIR_READ;
2287 dcmd->timeout = 0;
2288 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
2289 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
2290 dcmd->mbox.w[0] = seq_num;
2291 dcmd->mbox.w[1] = curr_aen.word;
2292 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
2293 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
2294
2295 /*
2296 * Store reference to the cmd used to register for AEN. When an
2297 * application wants us to register for AEN, we have to abort this
2298 * cmd and re-register with a new EVENT LOCALE supplied by that app
2299 */
2300 instance->aen_cmd = cmd;
2301
2302 /*
2303 * Issue the aen registration frame
2304 */
Sumant Patro1341c932006-01-25 12:02:40 -08002305 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002306
2307 return 0;
2308}
2309
2310/**
2311 * megasas_start_aen - Subscribes to AEN during driver load time
2312 * @instance: Adapter soft state
2313 */
2314static int megasas_start_aen(struct megasas_instance *instance)
2315{
2316 struct megasas_evt_log_info eli;
2317 union megasas_evt_class_locale class_locale;
2318
2319 /*
2320 * Get the latest sequence number from FW
2321 */
2322 memset(&eli, 0, sizeof(eli));
2323
2324 if (megasas_get_seq_num(instance, &eli))
2325 return -1;
2326
2327 /*
2328 * Register AEN with FW for latest sequence number plus 1
2329 */
2330 class_locale.members.reserved = 0;
2331 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2332 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2333
2334 return megasas_register_aen(instance, eli.newest_seq_num + 1,
2335 class_locale.word);
2336}
2337
2338/**
2339 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2340 * @instance: Adapter soft state
2341 */
2342static int megasas_io_attach(struct megasas_instance *instance)
2343{
2344 struct Scsi_Host *host = instance->host;
2345
2346 /*
2347 * Export parameters required by SCSI mid-layer
2348 */
2349 host->irq = instance->pdev->irq;
2350 host->unique_id = instance->unique_id;
2351 host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
2352 host->this_id = instance->init_id;
2353 host->sg_tablesize = instance->max_num_sge;
2354 host->max_sectors = instance->max_sectors_per_req;
2355 host->cmd_per_lun = 128;
2356 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
2357 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
2358 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08002359 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002360
2361 /*
2362 * Notify the mid-layer about the new controller
2363 */
2364 if (scsi_add_host(host, &instance->pdev->dev)) {
2365 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2366 return -ENODEV;
2367 }
2368
2369 /*
2370 * Trigger SCSI to scan our drives
2371 */
2372 scsi_scan_host(host);
2373 return 0;
2374}
2375
bo yang31ea7082007-11-07 12:09:50 -05002376static int
2377megasas_set_dma_mask(struct pci_dev *pdev)
2378{
2379 /*
2380 * All our contollers are capable of performing 64-bit DMA
2381 */
2382 if (IS_DMA64) {
2383 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK) != 0) {
2384
2385 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2386 goto fail_set_dma_mask;
2387 }
2388 } else {
2389 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2390 goto fail_set_dma_mask;
2391 }
2392 return 0;
2393
2394fail_set_dma_mask:
2395 return 1;
2396}
2397
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002398/**
2399 * megasas_probe_one - PCI hotplug entry point
2400 * @pdev: PCI device structure
2401 * @id: PCI ids of supported hotplugged adapter
2402 */
2403static int __devinit
2404megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2405{
2406 int rval;
2407 struct Scsi_Host *host;
2408 struct megasas_instance *instance;
2409
2410 /*
2411 * Announce PCI information
2412 */
2413 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2414 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2415 pdev->subsystem_device);
2416
2417 printk("bus %d:slot %d:func %d\n",
2418 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2419
2420 /*
2421 * PCI prepping: enable device set bus mastering and dma mask
2422 */
2423 rval = pci_enable_device(pdev);
2424
2425 if (rval) {
2426 return rval;
2427 }
2428
2429 pci_set_master(pdev);
2430
bo yang31ea7082007-11-07 12:09:50 -05002431 if (megasas_set_dma_mask(pdev))
2432 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002433
2434 host = scsi_host_alloc(&megasas_template,
2435 sizeof(struct megasas_instance));
2436
2437 if (!host) {
2438 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2439 goto fail_alloc_instance;
2440 }
2441
2442 instance = (struct megasas_instance *)host->hostdata;
2443 memset(instance, 0, sizeof(*instance));
2444
2445 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2446 &instance->producer_h);
2447 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2448 &instance->consumer_h);
2449
2450 if (!instance->producer || !instance->consumer) {
2451 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2452 "producer, consumer\n");
2453 goto fail_alloc_dma_buf;
2454 }
2455
2456 *instance->producer = 0;
2457 *instance->consumer = 0;
2458
2459 instance->evt_detail = pci_alloc_consistent(pdev,
2460 sizeof(struct
2461 megasas_evt_detail),
2462 &instance->evt_detail_h);
2463
2464 if (!instance->evt_detail) {
2465 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2466 "event detail structure\n");
2467 goto fail_alloc_dma_buf;
2468 }
2469
2470 /*
2471 * Initialize locks and queues
2472 */
2473 INIT_LIST_HEAD(&instance->cmd_pool);
2474
Sumant Patroe4a082c2006-05-30 12:03:37 -07002475 atomic_set(&instance->fw_outstanding,0);
2476
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002477 init_waitqueue_head(&instance->int_cmd_wait_q);
2478 init_waitqueue_head(&instance->abort_cmd_wait_q);
2479
2480 spin_lock_init(&instance->cmd_pool_lock);
bo yang7343eb62007-11-09 04:35:44 -05002481 spin_lock_init(&instance->completion_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002482
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02002483 mutex_init(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002484 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2485
2486 /*
2487 * Initialize PCI related and misc parameters
2488 */
2489 instance->pdev = pdev;
2490 instance->host = host;
2491 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2492 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2493
Sumant Patro658dced2006-10-03 13:09:14 -07002494 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002495 instance->flag = 0;
2496 instance->last_time = 0;
Sumant Patro658dced2006-10-03 13:09:14 -07002497
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002498 /*
2499 * Initialize MFI Firmware
2500 */
2501 if (megasas_init_mfi(instance))
2502 goto fail_init_mfi;
2503
2504 /*
2505 * Register IRQ
2506 */
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07002507 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002508 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2509 goto fail_irq;
2510 }
2511
Sumant Patro1341c932006-01-25 12:02:40 -08002512 instance->instancet->enable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002513
2514 /*
2515 * Store instance in PCI softstate
2516 */
2517 pci_set_drvdata(pdev, instance);
2518
2519 /*
2520 * Add this controller to megasas_mgmt_info structure so that it
2521 * can be exported to management applications
2522 */
2523 megasas_mgmt_info.count++;
2524 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2525 megasas_mgmt_info.max_index++;
2526
2527 /*
2528 * Initiate AEN (Asynchronous Event Notification)
2529 */
2530 if (megasas_start_aen(instance)) {
2531 printk(KERN_DEBUG "megasas: start aen failed\n");
2532 goto fail_start_aen;
2533 }
2534
2535 /*
2536 * Register with SCSI mid-layer
2537 */
2538 if (megasas_io_attach(instance))
2539 goto fail_io_attach;
2540
2541 return 0;
2542
2543 fail_start_aen:
2544 fail_io_attach:
2545 megasas_mgmt_info.count--;
2546 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2547 megasas_mgmt_info.max_index--;
2548
2549 pci_set_drvdata(pdev, NULL);
Sumant Patrob274cab2006-10-03 12:52:12 -07002550 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002551 free_irq(instance->pdev->irq, instance);
2552
2553 megasas_release_mfi(instance);
2554
2555 fail_irq:
2556 fail_init_mfi:
2557 fail_alloc_dma_buf:
2558 if (instance->evt_detail)
2559 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2560 instance->evt_detail,
2561 instance->evt_detail_h);
2562
2563 if (instance->producer)
2564 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2565 instance->producer_h);
2566 if (instance->consumer)
2567 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2568 instance->consumer_h);
2569 scsi_host_put(host);
2570
2571 fail_alloc_instance:
2572 fail_set_dma_mask:
2573 pci_disable_device(pdev);
2574
2575 return -ENODEV;
2576}
2577
2578/**
2579 * megasas_flush_cache - Requests FW to flush all its caches
2580 * @instance: Adapter soft state
2581 */
2582static void megasas_flush_cache(struct megasas_instance *instance)
2583{
2584 struct megasas_cmd *cmd;
2585 struct megasas_dcmd_frame *dcmd;
2586
2587 cmd = megasas_get_cmd(instance);
2588
2589 if (!cmd)
2590 return;
2591
2592 dcmd = &cmd->frame->dcmd;
2593
2594 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2595
2596 dcmd->cmd = MFI_CMD_DCMD;
2597 dcmd->cmd_status = 0x0;
2598 dcmd->sge_count = 0;
2599 dcmd->flags = MFI_FRAME_DIR_NONE;
2600 dcmd->timeout = 0;
2601 dcmd->data_xfer_len = 0;
2602 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
2603 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
2604
2605 megasas_issue_blocked_cmd(instance, cmd);
2606
2607 megasas_return_cmd(instance, cmd);
2608
2609 return;
2610}
2611
2612/**
2613 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2614 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05002615 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002616 */
bo yang31ea7082007-11-07 12:09:50 -05002617static void megasas_shutdown_controller(struct megasas_instance *instance,
2618 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002619{
2620 struct megasas_cmd *cmd;
2621 struct megasas_dcmd_frame *dcmd;
2622
2623 cmd = megasas_get_cmd(instance);
2624
2625 if (!cmd)
2626 return;
2627
2628 if (instance->aen_cmd)
2629 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
2630
2631 dcmd = &cmd->frame->dcmd;
2632
2633 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2634
2635 dcmd->cmd = MFI_CMD_DCMD;
2636 dcmd->cmd_status = 0x0;
2637 dcmd->sge_count = 0;
2638 dcmd->flags = MFI_FRAME_DIR_NONE;
2639 dcmd->timeout = 0;
2640 dcmd->data_xfer_len = 0;
bo yang31ea7082007-11-07 12:09:50 -05002641 dcmd->opcode = opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002642
2643 megasas_issue_blocked_cmd(instance, cmd);
2644
2645 megasas_return_cmd(instance, cmd);
2646
2647 return;
2648}
2649
2650/**
bo yangad84db22007-11-09 04:40:16 -05002651 * megasas_suspend - driver suspend entry point
2652 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05002653 * @state: PCI power state to suspend routine
2654 */
2655static int __devinit
2656megasas_suspend(struct pci_dev *pdev, pm_message_t state)
2657{
2658 struct Scsi_Host *host;
2659 struct megasas_instance *instance;
2660
2661 instance = pci_get_drvdata(pdev);
2662 host = instance->host;
2663
bo yangad84db22007-11-09 04:40:16 -05002664 if (poll_mode_io)
2665 del_timer_sync(&instance->io_completion_timer);
2666
bo yang31ea7082007-11-07 12:09:50 -05002667 megasas_flush_cache(instance);
2668 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
2669 tasklet_kill(&instance->isr_tasklet);
2670
2671 pci_set_drvdata(instance->pdev, instance);
2672 instance->instancet->disable_intr(instance->reg_set);
2673 free_irq(instance->pdev->irq, instance);
2674
2675 pci_save_state(pdev);
2676 pci_disable_device(pdev);
2677
2678 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2679
2680 return 0;
2681}
2682
2683/**
2684 * megasas_resume- driver resume entry point
2685 * @pdev: PCI device structure
2686 */
2687static int __devinit
2688megasas_resume(struct pci_dev *pdev)
2689{
2690 int rval;
2691 struct Scsi_Host *host;
2692 struct megasas_instance *instance;
2693
2694 instance = pci_get_drvdata(pdev);
2695 host = instance->host;
2696 pci_set_power_state(pdev, PCI_D0);
2697 pci_enable_wake(pdev, PCI_D0, 0);
2698 pci_restore_state(pdev);
2699
2700 /*
2701 * PCI prepping: enable device set bus mastering and dma mask
2702 */
2703 rval = pci_enable_device(pdev);
2704
2705 if (rval) {
2706 printk(KERN_ERR "megasas: Enable device failed\n");
2707 return rval;
2708 }
2709
2710 pci_set_master(pdev);
2711
2712 if (megasas_set_dma_mask(pdev))
2713 goto fail_set_dma_mask;
2714
2715 /*
2716 * Initialize MFI Firmware
2717 */
2718
2719 *instance->producer = 0;
2720 *instance->consumer = 0;
2721
2722 atomic_set(&instance->fw_outstanding, 0);
2723
2724 /*
2725 * We expect the FW state to be READY
2726 */
2727 if (megasas_transition_to_ready(instance))
2728 goto fail_ready_state;
2729
2730 if (megasas_issue_init_mfi(instance))
2731 goto fail_init_mfi;
2732
2733 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2734 (unsigned long)instance);
2735
2736 /*
2737 * Register IRQ
2738 */
2739 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED,
2740 "megasas", instance)) {
2741 printk(KERN_ERR "megasas: Failed to register IRQ\n");
2742 goto fail_irq;
2743 }
2744
2745 instance->instancet->enable_intr(instance->reg_set);
2746
2747 /*
2748 * Initiate AEN (Asynchronous Event Notification)
2749 */
2750 if (megasas_start_aen(instance))
2751 printk(KERN_ERR "megasas: Start AEN failed\n");
2752
bo yangad84db22007-11-09 04:40:16 -05002753 /* Initialize the cmd completion timer */
2754 if (poll_mode_io)
2755 megasas_start_timer(instance, &instance->io_completion_timer,
2756 megasas_io_completion_timer,
2757 MEGASAS_COMPLETION_TIMER_INTERVAL);
bo yang31ea7082007-11-07 12:09:50 -05002758 return 0;
2759
2760fail_irq:
2761fail_init_mfi:
2762 if (instance->evt_detail)
2763 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2764 instance->evt_detail,
2765 instance->evt_detail_h);
2766
2767 if (instance->producer)
2768 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2769 instance->producer_h);
2770 if (instance->consumer)
2771 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2772 instance->consumer_h);
2773 scsi_host_put(host);
2774
2775fail_set_dma_mask:
2776fail_ready_state:
2777
2778 pci_disable_device(pdev);
2779
2780 return -ENODEV;
2781}
2782
2783/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002784 * megasas_detach_one - PCI hot"un"plug entry point
2785 * @pdev: PCI device structure
2786 */
2787static void megasas_detach_one(struct pci_dev *pdev)
2788{
2789 int i;
2790 struct Scsi_Host *host;
2791 struct megasas_instance *instance;
2792
2793 instance = pci_get_drvdata(pdev);
2794 host = instance->host;
2795
bo yangad84db22007-11-09 04:40:16 -05002796 if (poll_mode_io)
2797 del_timer_sync(&instance->io_completion_timer);
2798
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002799 scsi_remove_host(instance->host);
2800 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05002801 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Sumant Patro5d018ad2006-10-03 13:13:18 -07002802 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002803
2804 /*
2805 * Take the instance off the instance array. Note that we will not
2806 * decrement the max_index. We let this array be sparse array
2807 */
2808 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2809 if (megasas_mgmt_info.instance[i] == instance) {
2810 megasas_mgmt_info.count--;
2811 megasas_mgmt_info.instance[i] = NULL;
2812
2813 break;
2814 }
2815 }
2816
2817 pci_set_drvdata(instance->pdev, NULL);
2818
Sumant Patrob274cab2006-10-03 12:52:12 -07002819 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002820
2821 free_irq(instance->pdev->irq, instance);
2822
2823 megasas_release_mfi(instance);
2824
2825 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2826 instance->evt_detail, instance->evt_detail_h);
2827
2828 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2829 instance->producer_h);
2830
2831 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2832 instance->consumer_h);
2833
2834 scsi_host_put(host);
2835
2836 pci_set_drvdata(pdev, NULL);
2837
2838 pci_disable_device(pdev);
2839
2840 return;
2841}
2842
2843/**
2844 * megasas_shutdown - Shutdown entry point
2845 * @device: Generic device structure
2846 */
2847static void megasas_shutdown(struct pci_dev *pdev)
2848{
2849 struct megasas_instance *instance = pci_get_drvdata(pdev);
2850 megasas_flush_cache(instance);
2851}
2852
2853/**
2854 * megasas_mgmt_open - char node "open" entry point
2855 */
2856static int megasas_mgmt_open(struct inode *inode, struct file *filep)
2857{
2858 /*
2859 * Allow only those users with admin rights
2860 */
2861 if (!capable(CAP_SYS_ADMIN))
2862 return -EACCES;
2863
2864 return 0;
2865}
2866
2867/**
2868 * megasas_mgmt_release - char node "release" entry point
2869 */
2870static int megasas_mgmt_release(struct inode *inode, struct file *filep)
2871{
2872 filep->private_data = NULL;
2873 fasync_helper(-1, filep, 0, &megasas_async_queue);
2874
2875 return 0;
2876}
2877
2878/**
2879 * megasas_mgmt_fasync - Async notifier registration from applications
2880 *
2881 * This function adds the calling process to a driver global queue. When an
2882 * event occurs, SIGIO will be sent to all processes in this queue.
2883 */
2884static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
2885{
2886 int rc;
2887
Arjan van de Ven0b950672006-01-11 13:16:10 +01002888 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002889
2890 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
2891
Arjan van de Ven0b950672006-01-11 13:16:10 +01002892 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002893
2894 if (rc >= 0) {
2895 /* For sanity check when we get ioctl */
2896 filep->private_data = filep;
2897 return 0;
2898 }
2899
2900 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
2901
2902 return rc;
2903}
2904
2905/**
2906 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
2907 * @instance: Adapter soft state
2908 * @argp: User's ioctl packet
2909 */
2910static int
2911megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
2912 struct megasas_iocpacket __user * user_ioc,
2913 struct megasas_iocpacket *ioc)
2914{
2915 struct megasas_sge32 *kern_sge32;
2916 struct megasas_cmd *cmd;
2917 void *kbuff_arr[MAX_IOCTL_SGE];
2918 dma_addr_t buf_handle = 0;
2919 int error = 0, i;
2920 void *sense = NULL;
2921 dma_addr_t sense_handle;
2922 u32 *sense_ptr;
2923
2924 memset(kbuff_arr, 0, sizeof(kbuff_arr));
2925
2926 if (ioc->sge_count > MAX_IOCTL_SGE) {
2927 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
2928 ioc->sge_count, MAX_IOCTL_SGE);
2929 return -EINVAL;
2930 }
2931
2932 cmd = megasas_get_cmd(instance);
2933 if (!cmd) {
2934 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
2935 return -ENOMEM;
2936 }
2937
2938 /*
2939 * User's IOCTL packet has 2 frames (maximum). Copy those two
2940 * frames into our cmd's frames. cmd->frame's context will get
2941 * overwritten when we copy from user's frames. So set that value
2942 * alone separately
2943 */
2944 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
2945 cmd->frame->hdr.context = cmd->index;
2946
2947 /*
2948 * The management interface between applications and the fw uses
2949 * MFI frames. E.g, RAID configuration changes, LD property changes
2950 * etc are accomplishes through different kinds of MFI frames. The
2951 * driver needs to care only about substituting user buffers with
2952 * kernel buffers in SGLs. The location of SGL is embedded in the
2953 * struct iocpacket itself.
2954 */
2955 kern_sge32 = (struct megasas_sge32 *)
2956 ((unsigned long)cmd->frame + ioc->sgl_off);
2957
2958 /*
2959 * For each user buffer, create a mirror buffer and copy in
2960 */
2961 for (i = 0; i < ioc->sge_count; i++) {
Sumant Patro9f35fa8a2007-02-14 12:55:45 -08002962 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002963 ioc->sgl[i].iov_len,
Sumant Patro9f35fa8a2007-02-14 12:55:45 -08002964 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002965 if (!kbuff_arr[i]) {
2966 printk(KERN_DEBUG "megasas: Failed to alloc "
2967 "kernel SGL buffer for IOCTL \n");
2968 error = -ENOMEM;
2969 goto out;
2970 }
2971
2972 /*
2973 * We don't change the dma_coherent_mask, so
2974 * pci_alloc_consistent only returns 32bit addresses
2975 */
2976 kern_sge32[i].phys_addr = (u32) buf_handle;
2977 kern_sge32[i].length = ioc->sgl[i].iov_len;
2978
2979 /*
2980 * We created a kernel buffer corresponding to the
2981 * user buffer. Now copy in from the user buffer
2982 */
2983 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
2984 (u32) (ioc->sgl[i].iov_len))) {
2985 error = -EFAULT;
2986 goto out;
2987 }
2988 }
2989
2990 if (ioc->sense_len) {
Sumant Patro9f35fa8a2007-02-14 12:55:45 -08002991 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
2992 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002993 if (!sense) {
2994 error = -ENOMEM;
2995 goto out;
2996 }
2997
2998 sense_ptr =
2999 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
3000 *sense_ptr = sense_handle;
3001 }
3002
3003 /*
3004 * Set the sync_cmd flag so that the ISR knows not to complete this
3005 * cmd to the SCSI mid-layer
3006 */
3007 cmd->sync_cmd = 1;
3008 megasas_issue_blocked_cmd(instance, cmd);
3009 cmd->sync_cmd = 0;
3010
3011 /*
3012 * copy out the kernel buffers to user buffers
3013 */
3014 for (i = 0; i < ioc->sge_count; i++) {
3015 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
3016 ioc->sgl[i].iov_len)) {
3017 error = -EFAULT;
3018 goto out;
3019 }
3020 }
3021
3022 /*
3023 * copy out the sense
3024 */
3025 if (ioc->sense_len) {
3026 /*
bo yangb70a41e2008-03-18 03:13:06 -04003027 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003028 * sense buffer address
3029 */
bo yangb70a41e2008-03-18 03:13:06 -04003030 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
3031 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003032
bo yangb70a41e2008-03-18 03:13:06 -04003033 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
3034 sense, ioc->sense_len)) {
bo yangb10c36a2007-11-09 04:28:47 -05003035 printk(KERN_ERR "megasas: Failed to copy out to user "
3036 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003037 error = -EFAULT;
3038 goto out;
3039 }
3040 }
3041
3042 /*
3043 * copy the status codes returned by the fw
3044 */
3045 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
3046 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
3047 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
3048 error = -EFAULT;
3049 }
3050
3051 out:
3052 if (sense) {
Sumant Patro9f35fa8a2007-02-14 12:55:45 -08003053 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003054 sense, sense_handle);
3055 }
3056
3057 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
Sumant Patro9f35fa8a2007-02-14 12:55:45 -08003058 dma_free_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003059 kern_sge32[i].length,
3060 kbuff_arr[i], kern_sge32[i].phys_addr);
3061 }
3062
3063 megasas_return_cmd(instance, cmd);
3064 return error;
3065}
3066
3067static struct megasas_instance *megasas_lookup_instance(u16 host_no)
3068{
3069 int i;
3070
3071 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3072
3073 if ((megasas_mgmt_info.instance[i]) &&
3074 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
3075 return megasas_mgmt_info.instance[i];
3076 }
3077
3078 return NULL;
3079}
3080
3081static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
3082{
3083 struct megasas_iocpacket __user *user_ioc =
3084 (struct megasas_iocpacket __user *)arg;
3085 struct megasas_iocpacket *ioc;
3086 struct megasas_instance *instance;
3087 int error;
3088
3089 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
3090 if (!ioc)
3091 return -ENOMEM;
3092
3093 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
3094 error = -EFAULT;
3095 goto out_kfree_ioc;
3096 }
3097
3098 instance = megasas_lookup_instance(ioc->host_no);
3099 if (!instance) {
3100 error = -ENODEV;
3101 goto out_kfree_ioc;
3102 }
3103
3104 /*
3105 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
3106 */
3107 if (down_interruptible(&instance->ioctl_sem)) {
3108 error = -ERESTARTSYS;
3109 goto out_kfree_ioc;
3110 }
3111 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
3112 up(&instance->ioctl_sem);
3113
3114 out_kfree_ioc:
3115 kfree(ioc);
3116 return error;
3117}
3118
3119static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
3120{
3121 struct megasas_instance *instance;
3122 struct megasas_aen aen;
3123 int error;
3124
3125 if (file->private_data != file) {
3126 printk(KERN_DEBUG "megasas: fasync_helper was not "
3127 "called first\n");
3128 return -EINVAL;
3129 }
3130
3131 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
3132 return -EFAULT;
3133
3134 instance = megasas_lookup_instance(aen.host_no);
3135
3136 if (!instance)
3137 return -ENODEV;
3138
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02003139 mutex_lock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003140 error = megasas_register_aen(instance, aen.seq_num,
3141 aen.class_locale_word);
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02003142 mutex_unlock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003143 return error;
3144}
3145
3146/**
3147 * megasas_mgmt_ioctl - char node ioctl entry point
3148 */
3149static long
3150megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3151{
3152 switch (cmd) {
3153 case MEGASAS_IOC_FIRMWARE:
3154 return megasas_mgmt_ioctl_fw(file, arg);
3155
3156 case MEGASAS_IOC_GET_AEN:
3157 return megasas_mgmt_ioctl_aen(file, arg);
3158 }
3159
3160 return -ENOTTY;
3161}
3162
3163#ifdef CONFIG_COMPAT
3164static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
3165{
3166 struct compat_megasas_iocpacket __user *cioc =
3167 (struct compat_megasas_iocpacket __user *)arg;
3168 struct megasas_iocpacket __user *ioc =
3169 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
3170 int i;
3171 int error = 0;
3172
Jeff Garzik83aabc12006-10-04 06:34:03 -04003173 if (clear_user(ioc, sizeof(*ioc)))
3174 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003175
3176 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
3177 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
3178 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
3179 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
3180 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
3181 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
3182 return -EFAULT;
3183
3184 for (i = 0; i < MAX_IOCTL_SGE; i++) {
3185 compat_uptr_t ptr;
3186
3187 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
3188 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
3189 copy_in_user(&ioc->sgl[i].iov_len,
3190 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
3191 return -EFAULT;
3192 }
3193
3194 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
3195
3196 if (copy_in_user(&cioc->frame.hdr.cmd_status,
3197 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
3198 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
3199 return -EFAULT;
3200 }
3201 return error;
3202}
3203
3204static long
3205megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
3206 unsigned long arg)
3207{
3208 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08003209 case MEGASAS_IOC_FIRMWARE32:
3210 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003211 case MEGASAS_IOC_GET_AEN:
3212 return megasas_mgmt_ioctl_aen(file, arg);
3213 }
3214
3215 return -ENOTTY;
3216}
3217#endif
3218
3219/*
3220 * File operations structure for management interface
3221 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08003222static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003223 .owner = THIS_MODULE,
3224 .open = megasas_mgmt_open,
3225 .release = megasas_mgmt_release,
3226 .fasync = megasas_mgmt_fasync,
3227 .unlocked_ioctl = megasas_mgmt_ioctl,
3228#ifdef CONFIG_COMPAT
3229 .compat_ioctl = megasas_mgmt_compat_ioctl,
3230#endif
3231};
3232
3233/*
3234 * PCI hotplug support registration structure
3235 */
3236static struct pci_driver megasas_pci_driver = {
3237
3238 .name = "megaraid_sas",
3239 .id_table = megasas_pci_table,
3240 .probe = megasas_probe_one,
3241 .remove = __devexit_p(megasas_detach_one),
bo yang31ea7082007-11-07 12:09:50 -05003242 .suspend = megasas_suspend,
3243 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003244 .shutdown = megasas_shutdown,
3245};
3246
3247/*
3248 * Sysfs driver attributes
3249 */
3250static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
3251{
3252 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
3253 MEGASAS_VERSION);
3254}
3255
3256static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
3257
3258static ssize_t
3259megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
3260{
3261 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
3262 MEGASAS_RELDATE);
3263}
3264
3265static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
3266 NULL);
3267
Sumant Patro658dced2006-10-03 13:09:14 -07003268static ssize_t
3269megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
3270{
bo yangad84db22007-11-09 04:40:16 -05003271 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07003272}
3273
3274static ssize_t
3275megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
3276{
3277 int retval = count;
3278 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
3279 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
3280 retval = -EINVAL;
3281 }
3282 return retval;
3283}
3284
3285static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUGO, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05003286 megasas_sysfs_set_dbg_lvl);
3287
3288static ssize_t
3289megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
3290{
3291 return sprintf(buf, "%u\n", poll_mode_io);
3292}
3293
3294static ssize_t
3295megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
3296 const char *buf, size_t count)
3297{
3298 int retval = count;
3299 int tmp = poll_mode_io;
3300 int i;
3301 struct megasas_instance *instance;
3302
3303 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
3304 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
3305 retval = -EINVAL;
3306 }
3307
3308 /*
3309 * Check if poll_mode_io is already set or is same as previous value
3310 */
3311 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
3312 goto out;
3313
3314 if (poll_mode_io) {
3315 /*
3316 * Start timers for all adapters
3317 */
3318 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3319 instance = megasas_mgmt_info.instance[i];
3320 if (instance) {
3321 megasas_start_timer(instance,
3322 &instance->io_completion_timer,
3323 megasas_io_completion_timer,
3324 MEGASAS_COMPLETION_TIMER_INTERVAL);
3325 }
3326 }
3327 } else {
3328 /*
3329 * Delete timers for all adapters
3330 */
3331 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3332 instance = megasas_mgmt_info.instance[i];
3333 if (instance)
3334 del_timer_sync(&instance->io_completion_timer);
3335 }
3336 }
3337
3338out:
3339 return retval;
3340}
3341
3342static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUGO,
3343 megasas_sysfs_show_poll_mode_io,
3344 megasas_sysfs_set_poll_mode_io);
Sumant Patro658dced2006-10-03 13:09:14 -07003345
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003346/**
3347 * megasas_init - Driver load entry point
3348 */
3349static int __init megasas_init(void)
3350{
3351 int rval;
3352
3353 /*
3354 * Announce driver version and other information
3355 */
3356 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
3357 MEGASAS_EXT_VERSION);
3358
3359 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
3360
3361 /*
3362 * Register character device node
3363 */
3364 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
3365
3366 if (rval < 0) {
3367 printk(KERN_DEBUG "megasas: failed to open device node\n");
3368 return rval;
3369 }
3370
3371 megasas_mgmt_majorno = rval;
3372
3373 /*
3374 * Register ourselves as PCI hotplug module
3375 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00003376 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003377
3378 if (rval) {
3379 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04003380 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003381 }
3382
Jeff Garzik83aabc12006-10-04 06:34:03 -04003383 rval = driver_create_file(&megasas_pci_driver.driver,
3384 &driver_attr_version);
3385 if (rval)
3386 goto err_dcf_attr_ver;
3387 rval = driver_create_file(&megasas_pci_driver.driver,
3388 &driver_attr_release_date);
3389 if (rval)
3390 goto err_dcf_rel_date;
3391 rval = driver_create_file(&megasas_pci_driver.driver,
3392 &driver_attr_dbg_lvl);
3393 if (rval)
3394 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05003395 rval = driver_create_file(&megasas_pci_driver.driver,
3396 &driver_attr_poll_mode_io);
3397 if (rval)
3398 goto err_dcf_poll_mode_io;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003399
3400 return rval;
bo yangad84db22007-11-09 04:40:16 -05003401
3402err_dcf_poll_mode_io:
3403 driver_remove_file(&megasas_pci_driver.driver,
3404 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04003405err_dcf_dbg_lvl:
3406 driver_remove_file(&megasas_pci_driver.driver,
3407 &driver_attr_release_date);
3408err_dcf_rel_date:
3409 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3410err_dcf_attr_ver:
3411 pci_unregister_driver(&megasas_pci_driver);
3412err_pcidrv:
3413 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3414 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003415}
3416
3417/**
3418 * megasas_exit - Driver unload entry point
3419 */
3420static void __exit megasas_exit(void)
3421{
Sumant Patro658dced2006-10-03 13:09:14 -07003422 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05003423 &driver_attr_poll_mode_io);
3424 driver_remove_file(&megasas_pci_driver.driver,
Sumant Patro658dced2006-10-03 13:09:14 -07003425 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04003426 driver_remove_file(&megasas_pci_driver.driver,
3427 &driver_attr_release_date);
3428 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003429
3430 pci_unregister_driver(&megasas_pci_driver);
3431 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3432}
3433
3434module_init(megasas_init);
3435module_exit(megasas_exit);