blob: 77a62a1b12c3e52cf76df0c3f65f58e607d293b8 [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
491 * @sge_count : number of sg elements
492 *
493 * Returns the number of frames required for numnber of sge's (sge_count)
494 */
495
Adrian Bunkb448de42006-11-20 03:23:49 +0100496static u32 megasas_get_frame_count(u8 sge_count)
Sumant Patrob1df99d2006-10-03 12:40:47 -0700497{
498 int num_cnt;
499 int sge_bytes;
500 u32 sge_sz;
501 u32 frame_count=0;
502
503 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
504 sizeof(struct megasas_sge32);
505
506 /*
507 * Main frame can contain 2 SGEs for 64-bit SGLs and
508 * 3 SGEs for 32-bit SGLs
509 */
510 if (IS_DMA64)
511 num_cnt = sge_count - 2;
512 else
513 num_cnt = sge_count - 3;
514
515 if(num_cnt>0){
516 sge_bytes = sge_sz * num_cnt;
517
518 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
519 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
520 }
521 /* Main frame */
522 frame_count +=1;
523
524 if (frame_count > 7)
525 frame_count = 8;
526 return frame_count;
527}
528
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400529/**
530 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
531 * @instance: Adapter soft state
532 * @scp: SCSI command
533 * @cmd: Command to be prepared in
534 *
535 * This function prepares CDB commands. These are typcially pass-through
536 * commands to the devices.
537 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800538static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400539megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
540 struct megasas_cmd *cmd)
541{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400542 u32 is_logical;
543 u32 device_id;
544 u16 flags = 0;
545 struct megasas_pthru_frame *pthru;
546
547 is_logical = MEGASAS_IS_LOGICAL(scp);
548 device_id = MEGASAS_DEV_INDEX(instance, scp);
549 pthru = (struct megasas_pthru_frame *)cmd->frame;
550
551 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
552 flags = MFI_FRAME_DIR_WRITE;
553 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
554 flags = MFI_FRAME_DIR_READ;
555 else if (scp->sc_data_direction == PCI_DMA_NONE)
556 flags = MFI_FRAME_DIR_NONE;
557
558 /*
559 * Prepare the DCDB frame
560 */
561 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
562 pthru->cmd_status = 0x0;
563 pthru->scsi_status = 0x0;
564 pthru->target_id = device_id;
565 pthru->lun = scp->device->lun;
566 pthru->cdb_len = scp->cmd_len;
567 pthru->timeout = 0;
568 pthru->flags = flags;
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900569 pthru->data_xfer_len = scsi_bufflen(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400570
571 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
572
573 /*
574 * Construct SGL
575 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400576 if (IS_DMA64) {
577 pthru->flags |= MFI_FRAME_SGL64;
578 pthru->sge_count = megasas_make_sgl64(instance, scp,
579 &pthru->sgl);
580 } else
581 pthru->sge_count = megasas_make_sgl32(instance, scp,
582 &pthru->sgl);
583
584 /*
585 * Sense info specific
586 */
587 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
588 pthru->sense_buf_phys_addr_hi = 0;
589 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
590
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400591 /*
592 * Compute the total number of frames this command consumes. FW uses
593 * this number to pull sufficient number of frames from host memory.
594 */
Sumant Patrob1df99d2006-10-03 12:40:47 -0700595 cmd->frame_count = megasas_get_frame_count(pthru->sge_count);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400596
597 return cmd->frame_count;
598}
599
600/**
601 * megasas_build_ldio - Prepares IOs to logical devices
602 * @instance: Adapter soft state
603 * @scp: SCSI command
604 * @cmd: Command to to be prepared
605 *
606 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
607 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800608static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400609megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
610 struct megasas_cmd *cmd)
611{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400612 u32 device_id;
613 u8 sc = scp->cmnd[0];
614 u16 flags = 0;
615 struct megasas_io_frame *ldio;
616
617 device_id = MEGASAS_DEV_INDEX(instance, scp);
618 ldio = (struct megasas_io_frame *)cmd->frame;
619
620 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
621 flags = MFI_FRAME_DIR_WRITE;
622 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
623 flags = MFI_FRAME_DIR_READ;
624
625 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -0700626 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400627 */
628 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
629 ldio->cmd_status = 0x0;
630 ldio->scsi_status = 0x0;
631 ldio->target_id = device_id;
632 ldio->timeout = 0;
633 ldio->reserved_0 = 0;
634 ldio->pad_0 = 0;
635 ldio->flags = flags;
636 ldio->start_lba_hi = 0;
637 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
638
639 /*
640 * 6-byte READ(0x08) or WRITE(0x0A) cdb
641 */
642 if (scp->cmd_len == 6) {
643 ldio->lba_count = (u32) scp->cmnd[4];
644 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
645 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
646
647 ldio->start_lba_lo &= 0x1FFFFF;
648 }
649
650 /*
651 * 10-byte READ(0x28) or WRITE(0x2A) cdb
652 */
653 else if (scp->cmd_len == 10) {
654 ldio->lba_count = (u32) scp->cmnd[8] |
655 ((u32) scp->cmnd[7] << 8);
656 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
657 ((u32) scp->cmnd[3] << 16) |
658 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
659 }
660
661 /*
662 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
663 */
664 else if (scp->cmd_len == 12) {
665 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
666 ((u32) scp->cmnd[7] << 16) |
667 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
668
669 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
670 ((u32) scp->cmnd[3] << 16) |
671 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
672 }
673
674 /*
675 * 16-byte READ(0x88) or WRITE(0x8A) cdb
676 */
677 else if (scp->cmd_len == 16) {
678 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
679 ((u32) scp->cmnd[11] << 16) |
680 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
681
682 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
683 ((u32) scp->cmnd[7] << 16) |
684 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
685
686 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
687 ((u32) scp->cmnd[3] << 16) |
688 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
689
690 }
691
692 /*
693 * Construct SGL
694 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400695 if (IS_DMA64) {
696 ldio->flags |= MFI_FRAME_SGL64;
697 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
698 } else
699 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
700
701 /*
702 * Sense info specific
703 */
704 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
705 ldio->sense_buf_phys_addr_hi = 0;
706 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
707
Sumant Patrob1df99d2006-10-03 12:40:47 -0700708 /*
709 * Compute the total number of frames this command consumes. FW uses
710 * this number to pull sufficient number of frames from host memory.
711 */
712 cmd->frame_count = megasas_get_frame_count(ldio->sge_count);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400713
714 return cmd->frame_count;
715}
716
717/**
Sumant Patrocb59aa62006-01-25 11:53:25 -0800718 * megasas_is_ldio - Checks if the cmd is for logical drive
719 * @scmd: SCSI command
720 *
721 * Called by megasas_queue_command to find out if the command to be queued
722 * is a logical drive command
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400723 */
Sumant Patrocb59aa62006-01-25 11:53:25 -0800724static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400725{
Sumant Patrocb59aa62006-01-25 11:53:25 -0800726 if (!MEGASAS_IS_LOGICAL(cmd))
727 return 0;
728 switch (cmd->cmnd[0]) {
729 case READ_10:
730 case WRITE_10:
731 case READ_12:
732 case WRITE_12:
733 case READ_6:
734 case WRITE_6:
735 case READ_16:
736 case WRITE_16:
737 return 1;
738 default:
739 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400740 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400741}
742
Sumant Patro658dced2006-10-03 13:09:14 -0700743 /**
744 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
745 * in FW
746 * @instance: Adapter soft state
747 */
748static inline void
749megasas_dump_pending_frames(struct megasas_instance *instance)
750{
751 struct megasas_cmd *cmd;
752 int i,n;
753 union megasas_sgl *mfi_sgl;
754 struct megasas_io_frame *ldio;
755 struct megasas_pthru_frame *pthru;
756 u32 sgcount;
757 u32 max_cmd = instance->max_fw_cmds;
758
759 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
760 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
761 if (IS_DMA64)
762 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
763 else
764 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
765
766 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
767 for (i = 0; i < max_cmd; i++) {
768 cmd = instance->cmd_list[i];
769 if(!cmd->scmd)
770 continue;
771 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
772 if (megasas_is_ldio(cmd->scmd)){
773 ldio = (struct megasas_io_frame *)cmd->frame;
774 mfi_sgl = &ldio->sgl;
775 sgcount = ldio->sge_count;
776 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);
777 }
778 else {
779 pthru = (struct megasas_pthru_frame *) cmd->frame;
780 mfi_sgl = &pthru->sgl;
781 sgcount = pthru->sge_count;
782 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);
783 }
784 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
785 for (n = 0; n < sgcount; n++){
786 if (IS_DMA64)
787 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) ;
788 else
789 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
790 }
791 }
792 printk(KERN_ERR "\n");
793 } /*for max_cmd*/
794 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
795 for (i = 0; i < max_cmd; i++) {
796
797 cmd = instance->cmd_list[i];
798
799 if(cmd->sync_cmd == 1){
800 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
801 }
802 }
803 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
804}
805
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400806/**
807 * megasas_queue_command - Queue entry point
808 * @scmd: SCSI command to be queued
809 * @done: Callback entry point
810 */
811static int
812megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
813{
814 u32 frame_count;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400815 struct megasas_cmd *cmd;
816 struct megasas_instance *instance;
817
818 instance = (struct megasas_instance *)
819 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -0800820
821 /* Don't process if we have already declared adapter dead */
822 if (instance->hw_crit_error)
823 return SCSI_MLQUEUE_HOST_BUSY;
824
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400825 scmd->scsi_done = done;
826 scmd->result = 0;
827
Sumant Patrocb59aa62006-01-25 11:53:25 -0800828 if (MEGASAS_IS_LOGICAL(scmd) &&
829 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
830 scmd->result = DID_BAD_TARGET << 16;
831 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400832 }
833
Sumant Patro02b01e02007-02-14 13:00:55 -0800834 switch (scmd->cmnd[0]) {
835 case SYNCHRONIZE_CACHE:
836 /*
837 * FW takes care of flush cache on its own
838 * No need to send it down
839 */
840 scmd->result = DID_OK << 16;
841 goto out_done;
842 default:
843 break;
844 }
845
Sumant Patrocb59aa62006-01-25 11:53:25 -0800846 cmd = megasas_get_cmd(instance);
847 if (!cmd)
848 return SCSI_MLQUEUE_HOST_BUSY;
849
850 /*
851 * Logical drive command
852 */
853 if (megasas_is_ldio(scmd))
854 frame_count = megasas_build_ldio(instance, scmd, cmd);
855 else
856 frame_count = megasas_build_dcdb(instance, scmd, cmd);
857
858 if (!frame_count)
859 goto out_return_cmd;
860
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400861 cmd->scmd = scmd;
Sumant Patro05e9ebb2007-05-17 05:47:51 -0700862 scmd->SCp.ptr = (char *)cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400863
864 /*
865 * Issue the command to the FW
866 */
Sumant Patroe4a082c2006-05-30 12:03:37 -0700867 atomic_inc(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400868
Sumant Patro1341c932006-01-25 12:02:40 -0800869 instance->instancet->fire_cmd(cmd->frame_phys_addr ,cmd->frame_count-1,instance->reg_set);
bo yangad84db22007-11-09 04:40:16 -0500870 /*
871 * Check if we have pend cmds to be completed
872 */
873 if (poll_mode_io && atomic_read(&instance->fw_outstanding))
874 tasklet_schedule(&instance->isr_tasklet);
875
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400876
877 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -0800878
879 out_return_cmd:
880 megasas_return_cmd(instance, cmd);
881 out_done:
882 done(scmd);
883 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400884}
885
Christoph Hellwig147aab62006-02-17 12:13:48 +0100886static int megasas_slave_configure(struct scsi_device *sdev)
887{
888 /*
889 * Don't export physical disk devices to the disk driver.
890 *
891 * FIXME: Currently we don't export them to the midlayer at all.
892 * That will be fixed once LSI engineers have audited the
893 * firmware for possible issues.
894 */
895 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS && sdev->type == TYPE_DISK)
896 return -ENXIO;
Christoph Hellwige5b3a652006-03-10 17:08:57 +0100897
898 /*
899 * The RAID firmware may require extended timeouts.
900 */
901 if (sdev->channel >= MEGASAS_MAX_PD_CHANNELS)
Sumant Patro05e9ebb2007-05-17 05:47:51 -0700902 sdev->timeout = MEGASAS_DEFAULT_CMD_TIMEOUT * HZ;
Christoph Hellwig147aab62006-02-17 12:13:48 +0100903 return 0;
904}
905
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400906/**
bo yang7343eb62007-11-09 04:35:44 -0500907 * megasas_complete_cmd_dpc - Returns FW's controller structure
908 * @instance_addr: Address of adapter soft state
909 *
910 * Tasklet to complete cmds
911 */
912static void megasas_complete_cmd_dpc(unsigned long instance_addr)
913{
914 u32 producer;
915 u32 consumer;
916 u32 context;
917 struct megasas_cmd *cmd;
918 struct megasas_instance *instance =
919 (struct megasas_instance *)instance_addr;
920 unsigned long flags;
921
922 /* If we have already declared adapter dead, donot complete cmds */
923 if (instance->hw_crit_error)
924 return;
925
926 spin_lock_irqsave(&instance->completion_lock, flags);
927
928 producer = *instance->producer;
929 consumer = *instance->consumer;
930
931 while (consumer != producer) {
932 context = instance->reply_queue[consumer];
933
934 cmd = instance->cmd_list[context];
935
936 megasas_complete_cmd(instance, cmd, DID_OK);
937
938 consumer++;
939 if (consumer == (instance->max_fw_cmds + 1)) {
940 consumer = 0;
941 }
942 }
943
944 *instance->consumer = producer;
945
946 spin_unlock_irqrestore(&instance->completion_lock, flags);
947
948 /*
949 * Check if we can restore can_queue
950 */
951 if (instance->flag & MEGASAS_FW_BUSY
952 && time_after(jiffies, instance->last_time + 5 * HZ)
953 && atomic_read(&instance->fw_outstanding) < 17) {
954
955 spin_lock_irqsave(instance->host->host_lock, flags);
956 instance->flag &= ~MEGASAS_FW_BUSY;
957 instance->host->can_queue =
958 instance->max_fw_cmds - MEGASAS_INT_CMDS;
959
960 spin_unlock_irqrestore(instance->host->host_lock, flags);
961 }
962}
963
964/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400965 * megasas_wait_for_outstanding - Wait for all outstanding cmds
966 * @instance: Adapter soft state
967 *
968 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
969 * complete all its outstanding commands. Returns error if one or more IOs
970 * are pending after this time period. It also marks the controller dead.
971 */
972static int megasas_wait_for_outstanding(struct megasas_instance *instance)
973{
974 int i;
975 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
976
977 for (i = 0; i < wait_time; i++) {
978
Sumant Patroe4a082c2006-05-30 12:03:37 -0700979 int outstanding = atomic_read(&instance->fw_outstanding);
980
981 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400982 break;
983
984 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
985 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -0700986 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -0500987 /*
988 * Call cmd completion routine. Cmd to be
989 * be completed directly without depending on isr.
990 */
991 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400992 }
993
994 msleep(1000);
995 }
996
Sumant Patroe4a082c2006-05-30 12:03:37 -0700997 if (atomic_read(&instance->fw_outstanding)) {
Sumant Patroe3bbff92006-10-03 12:28:49 -0700998 /*
999 * Send signal to FW to stop processing any pending cmds.
1000 * The controller will be taken offline by the OS now.
1001 */
1002 writel(MFI_STOP_ADP,
1003 &instance->reg_set->inbound_doorbell);
Sumant Patro658dced2006-10-03 13:09:14 -07001004 megasas_dump_pending_frames(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001005 instance->hw_crit_error = 1;
1006 return FAILED;
1007 }
1008
1009 return SUCCESS;
1010}
1011
1012/**
1013 * megasas_generic_reset - Generic reset routine
1014 * @scmd: Mid-layer SCSI command
1015 *
1016 * This routine implements a generic reset handler for device, bus and host
1017 * reset requests. Device, bus and host specific reset handlers can use this
1018 * function after they do their specific tasks.
1019 */
1020static int megasas_generic_reset(struct scsi_cmnd *scmd)
1021{
1022 int ret_val;
1023 struct megasas_instance *instance;
1024
1025 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1026
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001027 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
1028 scmd->serial_number, scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001029
1030 if (instance->hw_crit_error) {
1031 printk(KERN_ERR "megasas: cannot recover from previous reset "
1032 "failures\n");
1033 return FAILED;
1034 }
1035
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001036 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001037 if (ret_val == SUCCESS)
1038 printk(KERN_NOTICE "megasas: reset successful \n");
1039 else
1040 printk(KERN_ERR "megasas: failed to do reset\n");
1041
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001042 return ret_val;
1043}
1044
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001045/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001046 * megasas_reset_timer - quiesce the adapter if required
1047 * @scmd: scsi cmnd
1048 *
1049 * Sets the FW busy flag and reduces the host->can_queue if the
1050 * cmd has not been completed within the timeout period.
1051 */
1052static enum
1053scsi_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
1054{
1055 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
1056 struct megasas_instance *instance;
1057 unsigned long flags;
1058
1059 if (time_after(jiffies, scmd->jiffies_at_alloc +
1060 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
1061 return EH_NOT_HANDLED;
1062 }
1063
1064 instance = cmd->instance;
1065 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1066 /* FW is busy, throttle IO */
1067 spin_lock_irqsave(instance->host->host_lock, flags);
1068
1069 instance->host->can_queue = 16;
1070 instance->last_time = jiffies;
1071 instance->flag |= MEGASAS_FW_BUSY;
1072
1073 spin_unlock_irqrestore(instance->host->host_lock, flags);
1074 }
1075 return EH_RESET_TIMER;
1076}
1077
1078/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001079 * megasas_reset_device - Device reset handler entry point
1080 */
1081static int megasas_reset_device(struct scsi_cmnd *scmd)
1082{
1083 int ret;
1084
1085 /*
1086 * First wait for all commands to complete
1087 */
1088 ret = megasas_generic_reset(scmd);
1089
1090 return ret;
1091}
1092
1093/**
1094 * megasas_reset_bus_host - Bus & host reset handler entry point
1095 */
1096static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1097{
1098 int ret;
1099
1100 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01001101 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001102 */
1103 ret = megasas_generic_reset(scmd);
1104
1105 return ret;
1106}
1107
1108/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001109 * megasas_bios_param - Returns disk geometry for a disk
1110 * @sdev: device handle
1111 * @bdev: block device
1112 * @capacity: drive capacity
1113 * @geom: geometry parameters
1114 */
1115static int
1116megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1117 sector_t capacity, int geom[])
1118{
1119 int heads;
1120 int sectors;
1121 sector_t cylinders;
1122 unsigned long tmp;
1123 /* Default heads (64) & sectors (32) */
1124 heads = 64;
1125 sectors = 32;
1126
1127 tmp = heads * sectors;
1128 cylinders = capacity;
1129
1130 sector_div(cylinders, tmp);
1131
1132 /*
1133 * Handle extended translation size for logical drives > 1Gb
1134 */
1135
1136 if (capacity >= 0x200000) {
1137 heads = 255;
1138 sectors = 63;
1139 tmp = heads*sectors;
1140 cylinders = capacity;
1141 sector_div(cylinders, tmp);
1142 }
1143
1144 geom[0] = heads;
1145 geom[1] = sectors;
1146 geom[2] = cylinders;
1147
1148 return 0;
1149}
1150
1151/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001152 * megasas_service_aen - Processes an event notification
1153 * @instance: Adapter soft state
1154 * @cmd: AEN command completed by the ISR
1155 *
1156 * For AEN, driver sends a command down to FW that is held by the FW till an
1157 * event occurs. When an event of interest occurs, FW completes the command
1158 * that it was previously holding.
1159 *
1160 * This routines sends SIGIO signal to processes that have registered with the
1161 * driver for AEN.
1162 */
1163static void
1164megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1165{
1166 /*
1167 * Don't signal app if it is just an aborted previously registered aen
1168 */
1169 if (!cmd->abort_aen)
1170 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
1171 else
1172 cmd->abort_aen = 0;
1173
1174 instance->aen_cmd = NULL;
1175 megasas_return_cmd(instance, cmd);
1176}
1177
1178/*
1179 * Scsi host template for megaraid_sas driver
1180 */
1181static struct scsi_host_template megasas_template = {
1182
1183 .module = THIS_MODULE,
bo yangf28cd7c2007-11-09 04:44:56 -05001184 .name = "LSI SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001185 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01001186 .slave_configure = megasas_slave_configure,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001187 .queuecommand = megasas_queue_command,
1188 .eh_device_reset_handler = megasas_reset_device,
1189 .eh_bus_reset_handler = megasas_reset_bus_host,
1190 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001191 .eh_timed_out = megasas_reset_timer,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001192 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001193 .use_clustering = ENABLE_CLUSTERING,
1194};
1195
1196/**
1197 * megasas_complete_int_cmd - Completes an internal command
1198 * @instance: Adapter soft state
1199 * @cmd: Command to be completed
1200 *
1201 * The megasas_issue_blocked_cmd() function waits for a command to complete
1202 * after it issues a command. This function wakes up that waiting routine by
1203 * calling wake_up() on the wait queue.
1204 */
1205static void
1206megasas_complete_int_cmd(struct megasas_instance *instance,
1207 struct megasas_cmd *cmd)
1208{
1209 cmd->cmd_status = cmd->frame->io.cmd_status;
1210
1211 if (cmd->cmd_status == ENODATA) {
1212 cmd->cmd_status = 0;
1213 }
1214 wake_up(&instance->int_cmd_wait_q);
1215}
1216
1217/**
1218 * megasas_complete_abort - Completes aborting a command
1219 * @instance: Adapter soft state
1220 * @cmd: Cmd that was issued to abort another cmd
1221 *
1222 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
1223 * after it issues an abort on a previously issued command. This function
1224 * wakes up all functions waiting on the same wait queue.
1225 */
1226static void
1227megasas_complete_abort(struct megasas_instance *instance,
1228 struct megasas_cmd *cmd)
1229{
1230 if (cmd->sync_cmd) {
1231 cmd->sync_cmd = 0;
1232 cmd->cmd_status = 0;
1233 wake_up(&instance->abort_cmd_wait_q);
1234 }
1235
1236 return;
1237}
1238
1239/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001240 * megasas_complete_cmd - Completes a command
1241 * @instance: Adapter soft state
1242 * @cmd: Command to be completed
1243 * @alt_status: If non-zero, use this value as status to
1244 * SCSI mid-layer instead of the value returned
1245 * by the FW. This should be used if caller wants
1246 * an alternate status (as in the case of aborted
1247 * commands)
1248 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001249static void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001250megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
1251 u8 alt_status)
1252{
1253 int exception = 0;
1254 struct megasas_header *hdr = &cmd->frame->hdr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001255
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001256 if (cmd->scmd)
1257 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001258
1259 switch (hdr->cmd) {
1260
1261 case MFI_CMD_PD_SCSI_IO:
1262 case MFI_CMD_LD_SCSI_IO:
1263
1264 /*
1265 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1266 * issued either through an IO path or an IOCTL path. If it
1267 * was via IOCTL, we will send it to internal completion.
1268 */
1269 if (cmd->sync_cmd) {
1270 cmd->sync_cmd = 0;
1271 megasas_complete_int_cmd(instance, cmd);
1272 break;
1273 }
1274
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001275 case MFI_CMD_LD_READ:
1276 case MFI_CMD_LD_WRITE:
1277
1278 if (alt_status) {
1279 cmd->scmd->result = alt_status << 16;
1280 exception = 1;
1281 }
1282
1283 if (exception) {
1284
Sumant Patroe4a082c2006-05-30 12:03:37 -07001285 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001286
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001287 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001288 cmd->scmd->scsi_done(cmd->scmd);
1289 megasas_return_cmd(instance, cmd);
1290
1291 break;
1292 }
1293
1294 switch (hdr->cmd_status) {
1295
1296 case MFI_STAT_OK:
1297 cmd->scmd->result = DID_OK << 16;
1298 break;
1299
1300 case MFI_STAT_SCSI_IO_FAILED:
1301 case MFI_STAT_LD_INIT_IN_PROGRESS:
1302 cmd->scmd->result =
1303 (DID_ERROR << 16) | hdr->scsi_status;
1304 break;
1305
1306 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1307
1308 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1309
1310 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1311 memset(cmd->scmd->sense_buffer, 0,
1312 SCSI_SENSE_BUFFERSIZE);
1313 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1314 hdr->sense_len);
1315
1316 cmd->scmd->result |= DRIVER_SENSE << 24;
1317 }
1318
1319 break;
1320
1321 case MFI_STAT_LD_OFFLINE:
1322 case MFI_STAT_DEVICE_NOT_FOUND:
1323 cmd->scmd->result = DID_BAD_TARGET << 16;
1324 break;
1325
1326 default:
1327 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1328 hdr->cmd_status);
1329 cmd->scmd->result = DID_ERROR << 16;
1330 break;
1331 }
1332
Sumant Patroe4a082c2006-05-30 12:03:37 -07001333 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001334
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001335 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001336 cmd->scmd->scsi_done(cmd->scmd);
1337 megasas_return_cmd(instance, cmd);
1338
1339 break;
1340
1341 case MFI_CMD_SMP:
1342 case MFI_CMD_STP:
1343 case MFI_CMD_DCMD:
1344
1345 /*
1346 * See if got an event notification
1347 */
1348 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1349 megasas_service_aen(instance, cmd);
1350 else
1351 megasas_complete_int_cmd(instance, cmd);
1352
1353 break;
1354
1355 case MFI_CMD_ABORT:
1356 /*
1357 * Cmd issued to abort another cmd returned
1358 */
1359 megasas_complete_abort(instance, cmd);
1360 break;
1361
1362 default:
1363 printk("megasas: Unknown command completed! [0x%X]\n",
1364 hdr->cmd);
1365 break;
1366 }
1367}
1368
1369/**
1370 * megasas_deplete_reply_queue - Processes all completed commands
1371 * @instance: Adapter soft state
1372 * @alt_status: Alternate status to be returned to
1373 * SCSI mid-layer instead of the status
1374 * returned by the FW
1375 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001376static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001377megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1378{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001379 /*
1380 * Check if it is our interrupt
Sumant Patro1341c932006-01-25 12:02:40 -08001381 * Clear the interrupt
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001382 */
Sumant Patro1341c932006-01-25 12:02:40 -08001383 if(instance->instancet->clear_intr(instance->reg_set))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001384 return IRQ_NONE;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001385
Sumant Patroaf37acf2007-02-14 12:34:46 -08001386 if (instance->hw_crit_error)
1387 goto out_done;
Sumant Patro5d018ad2006-10-03 13:13:18 -07001388 /*
1389 * Schedule the tasklet for cmd completion
1390 */
1391 tasklet_schedule(&instance->isr_tasklet);
Sumant Patroaf37acf2007-02-14 12:34:46 -08001392out_done:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001393 return IRQ_HANDLED;
1394}
1395
1396/**
1397 * megasas_isr - isr entry point
1398 */
David Howells7d12e782006-10-05 14:55:46 +01001399static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001400{
1401 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1402 DID_OK);
1403}
1404
1405/**
1406 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08001407 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001408 *
1409 * During the initialization, FW passes can potentially be in any one of
1410 * several possible states. If the FW in operational, waiting-for-handshake
1411 * states, driver must take steps to bring it to ready state. Otherwise, it
1412 * has to wait for the ready state.
1413 */
1414static int
Sumant Patro1341c932006-01-25 12:02:40 -08001415megasas_transition_to_ready(struct megasas_instance* instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001416{
1417 int i;
1418 u8 max_wait;
1419 u32 fw_state;
1420 u32 cur_state;
1421
Sumant Patro1341c932006-01-25 12:02:40 -08001422 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001423
Sumant Patroe3bbff92006-10-03 12:28:49 -07001424 if (fw_state != MFI_STATE_READY)
1425 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1426 " state\n");
1427
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001428 while (fw_state != MFI_STATE_READY) {
1429
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001430 switch (fw_state) {
1431
1432 case MFI_STATE_FAULT:
1433
1434 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1435 return -ENODEV;
1436
1437 case MFI_STATE_WAIT_HANDSHAKE:
1438 /*
1439 * Set the CLR bit in inbound doorbell
1440 */
Sumant Patroe3bbff92006-10-03 12:28:49 -07001441 writel(MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
Sumant Patro1341c932006-01-25 12:02:40 -08001442 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001443
1444 max_wait = 2;
1445 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1446 break;
1447
Sumant Patroe3bbff92006-10-03 12:28:49 -07001448 case MFI_STATE_BOOT_MESSAGE_PENDING:
1449 writel(MFI_INIT_HOTPLUG,
1450 &instance->reg_set->inbound_doorbell);
1451
1452 max_wait = 10;
1453 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
1454 break;
1455
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001456 case MFI_STATE_OPERATIONAL:
1457 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07001458 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001459 */
Sumant Patrob274cab2006-10-03 12:52:12 -07001460 instance->instancet->disable_intr(instance->reg_set);
Sumant Patroe3bbff92006-10-03 12:28:49 -07001461 writel(MFI_RESET_FLAGS, &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001462
1463 max_wait = 10;
1464 cur_state = MFI_STATE_OPERATIONAL;
1465 break;
1466
1467 case MFI_STATE_UNDEFINED:
1468 /*
1469 * This state should not last for more than 2 seconds
1470 */
1471 max_wait = 2;
1472 cur_state = MFI_STATE_UNDEFINED;
1473 break;
1474
1475 case MFI_STATE_BB_INIT:
1476 max_wait = 2;
1477 cur_state = MFI_STATE_BB_INIT;
1478 break;
1479
1480 case MFI_STATE_FW_INIT:
1481 max_wait = 20;
1482 cur_state = MFI_STATE_FW_INIT;
1483 break;
1484
1485 case MFI_STATE_FW_INIT_2:
1486 max_wait = 20;
1487 cur_state = MFI_STATE_FW_INIT_2;
1488 break;
1489
1490 case MFI_STATE_DEVICE_SCAN:
1491 max_wait = 20;
1492 cur_state = MFI_STATE_DEVICE_SCAN;
1493 break;
1494
1495 case MFI_STATE_FLUSH_CACHE:
1496 max_wait = 20;
1497 cur_state = MFI_STATE_FLUSH_CACHE;
1498 break;
1499
1500 default:
1501 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1502 fw_state);
1503 return -ENODEV;
1504 }
1505
1506 /*
1507 * The cur_state should not last for more than max_wait secs
1508 */
1509 for (i = 0; i < (max_wait * 1000); i++) {
Sumant Patro1341c932006-01-25 12:02:40 -08001510 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1511 MFI_STATE_MASK ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001512
1513 if (fw_state == cur_state) {
1514 msleep(1);
1515 } else
1516 break;
1517 }
1518
1519 /*
1520 * Return error if fw_state hasn't changed after max_wait
1521 */
1522 if (fw_state == cur_state) {
1523 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1524 "in %d secs\n", fw_state, max_wait);
1525 return -ENODEV;
1526 }
1527 };
Sumant Patroe3bbff92006-10-03 12:28:49 -07001528 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001529
1530 return 0;
1531}
1532
1533/**
1534 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1535 * @instance: Adapter soft state
1536 */
1537static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1538{
1539 int i;
1540 u32 max_cmd = instance->max_fw_cmds;
1541 struct megasas_cmd *cmd;
1542
1543 if (!instance->frame_dma_pool)
1544 return;
1545
1546 /*
1547 * Return all frames to pool
1548 */
1549 for (i = 0; i < max_cmd; i++) {
1550
1551 cmd = instance->cmd_list[i];
1552
1553 if (cmd->frame)
1554 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1555 cmd->frame_phys_addr);
1556
1557 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07001558 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001559 cmd->sense_phys_addr);
1560 }
1561
1562 /*
1563 * Now destroy the pool itself
1564 */
1565 pci_pool_destroy(instance->frame_dma_pool);
1566 pci_pool_destroy(instance->sense_dma_pool);
1567
1568 instance->frame_dma_pool = NULL;
1569 instance->sense_dma_pool = NULL;
1570}
1571
1572/**
1573 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1574 * @instance: Adapter soft state
1575 *
1576 * Each command packet has an embedded DMA memory buffer that is used for
1577 * filling MFI frame and the SG list that immediately follows the frame. This
1578 * function creates those DMA memory buffers for each command packet by using
1579 * PCI pool facility.
1580 */
1581static int megasas_create_frame_pool(struct megasas_instance *instance)
1582{
1583 int i;
1584 u32 max_cmd;
1585 u32 sge_sz;
1586 u32 sgl_sz;
1587 u32 total_sz;
1588 u32 frame_count;
1589 struct megasas_cmd *cmd;
1590
1591 max_cmd = instance->max_fw_cmds;
1592
1593 /*
1594 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1595 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1596 */
1597 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1598 sizeof(struct megasas_sge32);
1599
1600 /*
1601 * Calculated the number of 64byte frames required for SGL
1602 */
1603 sgl_sz = sge_sz * instance->max_num_sge;
1604 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1605
1606 /*
1607 * We need one extra frame for the MFI command
1608 */
1609 frame_count++;
1610
1611 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1612 /*
1613 * Use DMA pool facility provided by PCI layer
1614 */
1615 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1616 instance->pdev, total_sz, 64,
1617 0);
1618
1619 if (!instance->frame_dma_pool) {
1620 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1621 return -ENOMEM;
1622 }
1623
1624 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1625 instance->pdev, 128, 4, 0);
1626
1627 if (!instance->sense_dma_pool) {
1628 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1629
1630 pci_pool_destroy(instance->frame_dma_pool);
1631 instance->frame_dma_pool = NULL;
1632
1633 return -ENOMEM;
1634 }
1635
1636 /*
1637 * Allocate and attach a frame to each of the commands in cmd_list.
1638 * By making cmd->index as the context instead of the &cmd, we can
1639 * always use 32bit context regardless of the architecture
1640 */
1641 for (i = 0; i < max_cmd; i++) {
1642
1643 cmd = instance->cmd_list[i];
1644
1645 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1646 GFP_KERNEL, &cmd->frame_phys_addr);
1647
1648 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1649 GFP_KERNEL, &cmd->sense_phys_addr);
1650
1651 /*
1652 * megasas_teardown_frame_pool() takes care of freeing
1653 * whatever has been allocated
1654 */
1655 if (!cmd->frame || !cmd->sense) {
1656 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1657 megasas_teardown_frame_pool(instance);
1658 return -ENOMEM;
1659 }
1660
1661 cmd->frame->io.context = cmd->index;
1662 }
1663
1664 return 0;
1665}
1666
1667/**
1668 * megasas_free_cmds - Free all the cmds in the free cmd pool
1669 * @instance: Adapter soft state
1670 */
1671static void megasas_free_cmds(struct megasas_instance *instance)
1672{
1673 int i;
1674 /* First free the MFI frame pool */
1675 megasas_teardown_frame_pool(instance);
1676
1677 /* Free all the commands in the cmd_list */
1678 for (i = 0; i < instance->max_fw_cmds; i++)
1679 kfree(instance->cmd_list[i]);
1680
1681 /* Free the cmd_list buffer itself */
1682 kfree(instance->cmd_list);
1683 instance->cmd_list = NULL;
1684
1685 INIT_LIST_HEAD(&instance->cmd_pool);
1686}
1687
1688/**
1689 * megasas_alloc_cmds - Allocates the command packets
1690 * @instance: Adapter soft state
1691 *
1692 * Each command that is issued to the FW, whether IO commands from the OS or
1693 * internal commands like IOCTLs, are wrapped in local data structure called
1694 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1695 * the FW.
1696 *
1697 * Each frame has a 32-bit field called context (tag). This context is used
1698 * to get back the megasas_cmd from the frame when a frame gets completed in
1699 * the ISR. Typically the address of the megasas_cmd itself would be used as
1700 * the context. But we wanted to keep the differences between 32 and 64 bit
1701 * systems to the mininum. We always use 32 bit integers for the context. In
1702 * this driver, the 32 bit values are the indices into an array cmd_list.
1703 * This array is used only to look up the megasas_cmd given the context. The
1704 * free commands themselves are maintained in a linked list called cmd_pool.
1705 */
1706static int megasas_alloc_cmds(struct megasas_instance *instance)
1707{
1708 int i;
1709 int j;
1710 u32 max_cmd;
1711 struct megasas_cmd *cmd;
1712
1713 max_cmd = instance->max_fw_cmds;
1714
1715 /*
1716 * instance->cmd_list is an array of struct megasas_cmd pointers.
1717 * Allocate the dynamic array first and then allocate individual
1718 * commands.
1719 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001720 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001721
1722 if (!instance->cmd_list) {
1723 printk(KERN_DEBUG "megasas: out of memory\n");
1724 return -ENOMEM;
1725 }
1726
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001727
1728 for (i = 0; i < max_cmd; i++) {
1729 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
1730 GFP_KERNEL);
1731
1732 if (!instance->cmd_list[i]) {
1733
1734 for (j = 0; j < i; j++)
1735 kfree(instance->cmd_list[j]);
1736
1737 kfree(instance->cmd_list);
1738 instance->cmd_list = NULL;
1739
1740 return -ENOMEM;
1741 }
1742 }
1743
1744 /*
1745 * Add all the commands to command pool (instance->cmd_pool)
1746 */
1747 for (i = 0; i < max_cmd; i++) {
1748 cmd = instance->cmd_list[i];
1749 memset(cmd, 0, sizeof(struct megasas_cmd));
1750 cmd->index = i;
1751 cmd->instance = instance;
1752
1753 list_add_tail(&cmd->list, &instance->cmd_pool);
1754 }
1755
1756 /*
1757 * Create a frame pool and assign one frame to each cmd
1758 */
1759 if (megasas_create_frame_pool(instance)) {
1760 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
1761 megasas_free_cmds(instance);
1762 }
1763
1764 return 0;
1765}
1766
1767/**
1768 * megasas_get_controller_info - Returns FW's controller structure
1769 * @instance: Adapter soft state
1770 * @ctrl_info: Controller information structure
1771 *
1772 * Issues an internal command (DCMD) to get the FW's controller structure.
1773 * This information is mainly used to find out the maximum IO transfer per
1774 * command supported by the FW.
1775 */
1776static int
1777megasas_get_ctrl_info(struct megasas_instance *instance,
1778 struct megasas_ctrl_info *ctrl_info)
1779{
1780 int ret = 0;
1781 struct megasas_cmd *cmd;
1782 struct megasas_dcmd_frame *dcmd;
1783 struct megasas_ctrl_info *ci;
1784 dma_addr_t ci_h = 0;
1785
1786 cmd = megasas_get_cmd(instance);
1787
1788 if (!cmd) {
1789 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
1790 return -ENOMEM;
1791 }
1792
1793 dcmd = &cmd->frame->dcmd;
1794
1795 ci = pci_alloc_consistent(instance->pdev,
1796 sizeof(struct megasas_ctrl_info), &ci_h);
1797
1798 if (!ci) {
1799 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
1800 megasas_return_cmd(instance, cmd);
1801 return -ENOMEM;
1802 }
1803
1804 memset(ci, 0, sizeof(*ci));
1805 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1806
1807 dcmd->cmd = MFI_CMD_DCMD;
1808 dcmd->cmd_status = 0xFF;
1809 dcmd->sge_count = 1;
1810 dcmd->flags = MFI_FRAME_DIR_READ;
1811 dcmd->timeout = 0;
1812 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
1813 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
1814 dcmd->sgl.sge32[0].phys_addr = ci_h;
1815 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
1816
1817 if (!megasas_issue_polled(instance, cmd)) {
1818 ret = 0;
1819 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
1820 } else {
1821 ret = -1;
1822 }
1823
1824 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
1825 ci, ci_h);
1826
1827 megasas_return_cmd(instance, cmd);
1828 return ret;
1829}
1830
1831/**
bo yang31ea7082007-11-07 12:09:50 -05001832 * megasas_issue_init_mfi - Initializes the FW
1833 * @instance: Adapter soft state
1834 *
1835 * Issues the INIT MFI cmd
1836 */
1837static int
1838megasas_issue_init_mfi(struct megasas_instance *instance)
1839{
1840 u32 context;
1841
1842 struct megasas_cmd *cmd;
1843
1844 struct megasas_init_frame *init_frame;
1845 struct megasas_init_queue_info *initq_info;
1846 dma_addr_t init_frame_h;
1847 dma_addr_t initq_info_h;
1848
1849 /*
1850 * Prepare a init frame. Note the init frame points to queue info
1851 * structure. Each frame has SGL allocated after first 64 bytes. For
1852 * this frame - since we don't need any SGL - we use SGL's space as
1853 * queue info structure
1854 *
1855 * We will not get a NULL command below. We just created the pool.
1856 */
1857 cmd = megasas_get_cmd(instance);
1858
1859 init_frame = (struct megasas_init_frame *)cmd->frame;
1860 initq_info = (struct megasas_init_queue_info *)
1861 ((unsigned long)init_frame + 64);
1862
1863 init_frame_h = cmd->frame_phys_addr;
1864 initq_info_h = init_frame_h + 64;
1865
1866 context = init_frame->context;
1867 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
1868 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
1869 init_frame->context = context;
1870
1871 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
1872 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
1873
1874 initq_info->producer_index_phys_addr_lo = instance->producer_h;
1875 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
1876
1877 init_frame->cmd = MFI_CMD_INIT;
1878 init_frame->cmd_status = 0xFF;
1879 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
1880
1881 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
1882
1883 /*
1884 * disable the intr before firing the init frame to FW
1885 */
1886 instance->instancet->disable_intr(instance->reg_set);
1887
1888 /*
1889 * Issue the init frame in polled mode
1890 */
1891
1892 if (megasas_issue_polled(instance, cmd)) {
1893 printk(KERN_ERR "megasas: Failed to init firmware\n");
1894 megasas_return_cmd(instance, cmd);
1895 goto fail_fw_init;
1896 }
1897
1898 megasas_return_cmd(instance, cmd);
1899
1900 return 0;
1901
1902fail_fw_init:
1903 return -EINVAL;
1904}
1905
1906/**
bo yangad84db22007-11-09 04:40:16 -05001907 * megasas_start_timer - Initializes a timer object
1908 * @instance: Adapter soft state
1909 * @timer: timer object to be initialized
1910 * @fn: timer function
1911 * @interval: time interval between timer function call
1912 */
1913static inline void
1914megasas_start_timer(struct megasas_instance *instance,
1915 struct timer_list *timer,
1916 void *fn, unsigned long interval)
1917{
1918 init_timer(timer);
1919 timer->expires = jiffies + interval;
1920 timer->data = (unsigned long)instance;
1921 timer->function = fn;
1922 add_timer(timer);
1923}
1924
1925/**
1926 * megasas_io_completion_timer - Timer fn
1927 * @instance_addr: Address of adapter soft state
1928 *
1929 * Schedules tasklet for cmd completion
1930 * if poll_mode_io is set
1931 */
1932static void
1933megasas_io_completion_timer(unsigned long instance_addr)
1934{
1935 struct megasas_instance *instance =
1936 (struct megasas_instance *)instance_addr;
1937
1938 if (atomic_read(&instance->fw_outstanding))
1939 tasklet_schedule(&instance->isr_tasklet);
1940
1941 /* Restart timer */
1942 if (poll_mode_io)
1943 mod_timer(&instance->io_completion_timer,
1944 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
1945}
1946
1947/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001948 * megasas_init_mfi - Initializes the FW
1949 * @instance: Adapter soft state
1950 *
1951 * This is the main function for initializing MFI firmware.
1952 */
1953static int megasas_init_mfi(struct megasas_instance *instance)
1954{
1955 u32 context_sz;
1956 u32 reply_q_sz;
1957 u32 max_sectors_1;
1958 u32 max_sectors_2;
bo yang14faea92007-11-09 04:14:00 -05001959 u32 tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001960 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001961 struct megasas_ctrl_info *ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001962 /*
1963 * Map the message registers
1964 */
1965 instance->base_addr = pci_resource_start(instance->pdev, 0);
1966
bo yangf28cd7c2007-11-09 04:44:56 -05001967 if (pci_request_regions(instance->pdev, "megasas: LSI")) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001968 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
1969 return -EBUSY;
1970 }
1971
1972 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
1973
1974 if (!instance->reg_set) {
1975 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
1976 goto fail_ioremap;
1977 }
1978
1979 reg_set = instance->reg_set;
1980
Sumant Patrof9876f02006-02-03 15:34:35 -08001981 switch(instance->pdev->device)
1982 {
1983 case PCI_DEVICE_ID_LSI_SAS1078R:
1984 instance->instancet = &megasas_instance_template_ppc;
1985 break;
1986 case PCI_DEVICE_ID_LSI_SAS1064R:
1987 case PCI_DEVICE_ID_DELL_PERC5:
1988 default:
1989 instance->instancet = &megasas_instance_template_xscale;
1990 break;
1991 }
Sumant Patro1341c932006-01-25 12:02:40 -08001992
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001993 /*
1994 * We expect the FW state to be READY
1995 */
Sumant Patro1341c932006-01-25 12:02:40 -08001996 if (megasas_transition_to_ready(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001997 goto fail_ready_state;
1998
1999 /*
2000 * Get various operational parameters from status register
2001 */
Sumant Patro1341c932006-01-25 12:02:40 -08002002 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07002003 /*
2004 * Reduce the max supported cmds by 1. This is to ensure that the
2005 * reply_q_sz (1 more than the max cmd that driver may send)
2006 * does not exceed max cmds that the FW can support
2007 */
2008 instance->max_fw_cmds = instance->max_fw_cmds-1;
Sumant Patro1341c932006-01-25 12:02:40 -08002009 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
2010 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002011 /*
2012 * Create a pool of commands
2013 */
2014 if (megasas_alloc_cmds(instance))
2015 goto fail_alloc_cmds;
2016
2017 /*
2018 * Allocate memory for reply queue. Length of reply queue should
2019 * be _one_ more than the maximum commands handled by the firmware.
2020 *
2021 * Note: When FW completes commands, it places corresponding contex
2022 * values in this circular reply queue. This circular queue is a fairly
2023 * typical producer-consumer queue. FW is the producer (of completed
2024 * commands) and the driver is the consumer.
2025 */
2026 context_sz = sizeof(u32);
2027 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
2028
2029 instance->reply_queue = pci_alloc_consistent(instance->pdev,
2030 reply_q_sz,
2031 &instance->reply_queue_h);
2032
2033 if (!instance->reply_queue) {
2034 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
2035 goto fail_reply_queue;
2036 }
2037
bo yang31ea7082007-11-07 12:09:50 -05002038 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002039 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002040
2041 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
2042
2043 /*
2044 * Compute the max allowed sectors per IO: The controller info has two
2045 * limits on max sectors. Driver should use the minimum of these two.
2046 *
2047 * 1 << stripe_sz_ops.min = max sectors per strip
2048 *
2049 * Note that older firmwares ( < FW ver 30) didn't report information
2050 * to calculate max_sectors_1. So the number ended up as zero always.
2051 */
bo yang14faea92007-11-09 04:14:00 -05002052 tmp_sectors = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002053 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
2054
2055 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
2056 ctrl_info->max_strips_per_io;
2057 max_sectors_2 = ctrl_info->max_request_size;
2058
bo yang14faea92007-11-09 04:14:00 -05002059 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
2060 }
2061
2062 instance->max_sectors_per_req = instance->max_num_sge *
2063 PAGE_SIZE / 512;
2064 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
2065 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002066
2067 kfree(ctrl_info);
2068
Sumant Patro5d018ad2006-10-03 13:13:18 -07002069 /*
2070 * Setup tasklet for cmd completion
2071 */
2072
bo yangad84db22007-11-09 04:40:16 -05002073 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2074 (unsigned long)instance);
2075
2076 /* Initialize the cmd completion timer */
2077 if (poll_mode_io)
2078 megasas_start_timer(instance, &instance->io_completion_timer,
2079 megasas_io_completion_timer,
2080 MEGASAS_COMPLETION_TIMER_INTERVAL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002081 return 0;
2082
2083 fail_fw_init:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002084
2085 pci_free_consistent(instance->pdev, reply_q_sz,
2086 instance->reply_queue, instance->reply_queue_h);
2087 fail_reply_queue:
2088 megasas_free_cmds(instance);
2089
2090 fail_alloc_cmds:
2091 fail_ready_state:
2092 iounmap(instance->reg_set);
2093
2094 fail_ioremap:
2095 pci_release_regions(instance->pdev);
2096
2097 return -EINVAL;
2098}
2099
2100/**
2101 * megasas_release_mfi - Reverses the FW initialization
2102 * @intance: Adapter soft state
2103 */
2104static void megasas_release_mfi(struct megasas_instance *instance)
2105{
2106 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
2107
2108 pci_free_consistent(instance->pdev, reply_q_sz,
2109 instance->reply_queue, instance->reply_queue_h);
2110
2111 megasas_free_cmds(instance);
2112
2113 iounmap(instance->reg_set);
2114
2115 pci_release_regions(instance->pdev);
2116}
2117
2118/**
2119 * megasas_get_seq_num - Gets latest event sequence numbers
2120 * @instance: Adapter soft state
2121 * @eli: FW event log sequence numbers information
2122 *
2123 * FW maintains a log of all events in a non-volatile area. Upper layers would
2124 * usually find out the latest sequence number of the events, the seq number at
2125 * the boot etc. They would "read" all the events below the latest seq number
2126 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
2127 * number), they would subsribe to AEN (asynchronous event notification) and
2128 * wait for the events to happen.
2129 */
2130static int
2131megasas_get_seq_num(struct megasas_instance *instance,
2132 struct megasas_evt_log_info *eli)
2133{
2134 struct megasas_cmd *cmd;
2135 struct megasas_dcmd_frame *dcmd;
2136 struct megasas_evt_log_info *el_info;
2137 dma_addr_t el_info_h = 0;
2138
2139 cmd = megasas_get_cmd(instance);
2140
2141 if (!cmd) {
2142 return -ENOMEM;
2143 }
2144
2145 dcmd = &cmd->frame->dcmd;
2146 el_info = pci_alloc_consistent(instance->pdev,
2147 sizeof(struct megasas_evt_log_info),
2148 &el_info_h);
2149
2150 if (!el_info) {
2151 megasas_return_cmd(instance, cmd);
2152 return -ENOMEM;
2153 }
2154
2155 memset(el_info, 0, sizeof(*el_info));
2156 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2157
2158 dcmd->cmd = MFI_CMD_DCMD;
2159 dcmd->cmd_status = 0x0;
2160 dcmd->sge_count = 1;
2161 dcmd->flags = MFI_FRAME_DIR_READ;
2162 dcmd->timeout = 0;
2163 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
2164 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
2165 dcmd->sgl.sge32[0].phys_addr = el_info_h;
2166 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
2167
2168 megasas_issue_blocked_cmd(instance, cmd);
2169
2170 /*
2171 * Copy the data back into callers buffer
2172 */
2173 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
2174
2175 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
2176 el_info, el_info_h);
2177
2178 megasas_return_cmd(instance, cmd);
2179
2180 return 0;
2181}
2182
2183/**
2184 * megasas_register_aen - Registers for asynchronous event notification
2185 * @instance: Adapter soft state
2186 * @seq_num: The starting sequence number
2187 * @class_locale: Class of the event
2188 *
2189 * This function subscribes for AEN for events beyond the @seq_num. It requests
2190 * to be notified if and only if the event is of type @class_locale
2191 */
2192static int
2193megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
2194 u32 class_locale_word)
2195{
2196 int ret_val;
2197 struct megasas_cmd *cmd;
2198 struct megasas_dcmd_frame *dcmd;
2199 union megasas_evt_class_locale curr_aen;
2200 union megasas_evt_class_locale prev_aen;
2201
2202 /*
2203 * If there an AEN pending already (aen_cmd), check if the
2204 * class_locale of that pending AEN is inclusive of the new
2205 * AEN request we currently have. If it is, then we don't have
2206 * to do anything. In other words, whichever events the current
2207 * AEN request is subscribing to, have already been subscribed
2208 * to.
2209 *
2210 * If the old_cmd is _not_ inclusive, then we have to abort
2211 * that command, form a class_locale that is superset of both
2212 * old and current and re-issue to the FW
2213 */
2214
2215 curr_aen.word = class_locale_word;
2216
2217 if (instance->aen_cmd) {
2218
2219 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
2220
2221 /*
2222 * A class whose enum value is smaller is inclusive of all
2223 * higher values. If a PROGRESS (= -1) was previously
2224 * registered, then a new registration requests for higher
2225 * classes need not be sent to FW. They are automatically
2226 * included.
2227 *
2228 * Locale numbers don't have such hierarchy. They are bitmap
2229 * values
2230 */
2231 if ((prev_aen.members.class <= curr_aen.members.class) &&
2232 !((prev_aen.members.locale & curr_aen.members.locale) ^
2233 curr_aen.members.locale)) {
2234 /*
2235 * Previously issued event registration includes
2236 * current request. Nothing to do.
2237 */
2238 return 0;
2239 } else {
2240 curr_aen.members.locale |= prev_aen.members.locale;
2241
2242 if (prev_aen.members.class < curr_aen.members.class)
2243 curr_aen.members.class = prev_aen.members.class;
2244
2245 instance->aen_cmd->abort_aen = 1;
2246 ret_val = megasas_issue_blocked_abort_cmd(instance,
2247 instance->
2248 aen_cmd);
2249
2250 if (ret_val) {
2251 printk(KERN_DEBUG "megasas: Failed to abort "
2252 "previous AEN command\n");
2253 return ret_val;
2254 }
2255 }
2256 }
2257
2258 cmd = megasas_get_cmd(instance);
2259
2260 if (!cmd)
2261 return -ENOMEM;
2262
2263 dcmd = &cmd->frame->dcmd;
2264
2265 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
2266
2267 /*
2268 * Prepare DCMD for aen registration
2269 */
2270 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2271
2272 dcmd->cmd = MFI_CMD_DCMD;
2273 dcmd->cmd_status = 0x0;
2274 dcmd->sge_count = 1;
2275 dcmd->flags = MFI_FRAME_DIR_READ;
2276 dcmd->timeout = 0;
2277 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
2278 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
2279 dcmd->mbox.w[0] = seq_num;
2280 dcmd->mbox.w[1] = curr_aen.word;
2281 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
2282 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
2283
2284 /*
2285 * Store reference to the cmd used to register for AEN. When an
2286 * application wants us to register for AEN, we have to abort this
2287 * cmd and re-register with a new EVENT LOCALE supplied by that app
2288 */
2289 instance->aen_cmd = cmd;
2290
2291 /*
2292 * Issue the aen registration frame
2293 */
Sumant Patro1341c932006-01-25 12:02:40 -08002294 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002295
2296 return 0;
2297}
2298
2299/**
2300 * megasas_start_aen - Subscribes to AEN during driver load time
2301 * @instance: Adapter soft state
2302 */
2303static int megasas_start_aen(struct megasas_instance *instance)
2304{
2305 struct megasas_evt_log_info eli;
2306 union megasas_evt_class_locale class_locale;
2307
2308 /*
2309 * Get the latest sequence number from FW
2310 */
2311 memset(&eli, 0, sizeof(eli));
2312
2313 if (megasas_get_seq_num(instance, &eli))
2314 return -1;
2315
2316 /*
2317 * Register AEN with FW for latest sequence number plus 1
2318 */
2319 class_locale.members.reserved = 0;
2320 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2321 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2322
2323 return megasas_register_aen(instance, eli.newest_seq_num + 1,
2324 class_locale.word);
2325}
2326
2327/**
2328 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2329 * @instance: Adapter soft state
2330 */
2331static int megasas_io_attach(struct megasas_instance *instance)
2332{
2333 struct Scsi_Host *host = instance->host;
2334
2335 /*
2336 * Export parameters required by SCSI mid-layer
2337 */
2338 host->irq = instance->pdev->irq;
2339 host->unique_id = instance->unique_id;
2340 host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
2341 host->this_id = instance->init_id;
2342 host->sg_tablesize = instance->max_num_sge;
2343 host->max_sectors = instance->max_sectors_per_req;
2344 host->cmd_per_lun = 128;
2345 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
2346 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
2347 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08002348 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002349
2350 /*
2351 * Notify the mid-layer about the new controller
2352 */
2353 if (scsi_add_host(host, &instance->pdev->dev)) {
2354 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2355 return -ENODEV;
2356 }
2357
2358 /*
2359 * Trigger SCSI to scan our drives
2360 */
2361 scsi_scan_host(host);
2362 return 0;
2363}
2364
bo yang31ea7082007-11-07 12:09:50 -05002365static int
2366megasas_set_dma_mask(struct pci_dev *pdev)
2367{
2368 /*
2369 * All our contollers are capable of performing 64-bit DMA
2370 */
2371 if (IS_DMA64) {
2372 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK) != 0) {
2373
2374 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2375 goto fail_set_dma_mask;
2376 }
2377 } else {
2378 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2379 goto fail_set_dma_mask;
2380 }
2381 return 0;
2382
2383fail_set_dma_mask:
2384 return 1;
2385}
2386
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002387/**
2388 * megasas_probe_one - PCI hotplug entry point
2389 * @pdev: PCI device structure
2390 * @id: PCI ids of supported hotplugged adapter
2391 */
2392static int __devinit
2393megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2394{
2395 int rval;
2396 struct Scsi_Host *host;
2397 struct megasas_instance *instance;
2398
2399 /*
2400 * Announce PCI information
2401 */
2402 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2403 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2404 pdev->subsystem_device);
2405
2406 printk("bus %d:slot %d:func %d\n",
2407 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2408
2409 /*
2410 * PCI prepping: enable device set bus mastering and dma mask
2411 */
2412 rval = pci_enable_device(pdev);
2413
2414 if (rval) {
2415 return rval;
2416 }
2417
2418 pci_set_master(pdev);
2419
bo yang31ea7082007-11-07 12:09:50 -05002420 if (megasas_set_dma_mask(pdev))
2421 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002422
2423 host = scsi_host_alloc(&megasas_template,
2424 sizeof(struct megasas_instance));
2425
2426 if (!host) {
2427 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2428 goto fail_alloc_instance;
2429 }
2430
2431 instance = (struct megasas_instance *)host->hostdata;
2432 memset(instance, 0, sizeof(*instance));
2433
2434 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2435 &instance->producer_h);
2436 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2437 &instance->consumer_h);
2438
2439 if (!instance->producer || !instance->consumer) {
2440 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2441 "producer, consumer\n");
2442 goto fail_alloc_dma_buf;
2443 }
2444
2445 *instance->producer = 0;
2446 *instance->consumer = 0;
2447
2448 instance->evt_detail = pci_alloc_consistent(pdev,
2449 sizeof(struct
2450 megasas_evt_detail),
2451 &instance->evt_detail_h);
2452
2453 if (!instance->evt_detail) {
2454 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2455 "event detail structure\n");
2456 goto fail_alloc_dma_buf;
2457 }
2458
2459 /*
2460 * Initialize locks and queues
2461 */
2462 INIT_LIST_HEAD(&instance->cmd_pool);
2463
Sumant Patroe4a082c2006-05-30 12:03:37 -07002464 atomic_set(&instance->fw_outstanding,0);
2465
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002466 init_waitqueue_head(&instance->int_cmd_wait_q);
2467 init_waitqueue_head(&instance->abort_cmd_wait_q);
2468
2469 spin_lock_init(&instance->cmd_pool_lock);
bo yang7343eb62007-11-09 04:35:44 -05002470 spin_lock_init(&instance->completion_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002471
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02002472 mutex_init(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002473 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2474
2475 /*
2476 * Initialize PCI related and misc parameters
2477 */
2478 instance->pdev = pdev;
2479 instance->host = host;
2480 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2481 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2482
Sumant Patro658dced2006-10-03 13:09:14 -07002483 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002484 instance->flag = 0;
2485 instance->last_time = 0;
Sumant Patro658dced2006-10-03 13:09:14 -07002486
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002487 /*
2488 * Initialize MFI Firmware
2489 */
2490 if (megasas_init_mfi(instance))
2491 goto fail_init_mfi;
2492
2493 /*
2494 * Register IRQ
2495 */
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07002496 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002497 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2498 goto fail_irq;
2499 }
2500
Sumant Patro1341c932006-01-25 12:02:40 -08002501 instance->instancet->enable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002502
2503 /*
2504 * Store instance in PCI softstate
2505 */
2506 pci_set_drvdata(pdev, instance);
2507
2508 /*
2509 * Add this controller to megasas_mgmt_info structure so that it
2510 * can be exported to management applications
2511 */
2512 megasas_mgmt_info.count++;
2513 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2514 megasas_mgmt_info.max_index++;
2515
2516 /*
2517 * Initiate AEN (Asynchronous Event Notification)
2518 */
2519 if (megasas_start_aen(instance)) {
2520 printk(KERN_DEBUG "megasas: start aen failed\n");
2521 goto fail_start_aen;
2522 }
2523
2524 /*
2525 * Register with SCSI mid-layer
2526 */
2527 if (megasas_io_attach(instance))
2528 goto fail_io_attach;
2529
2530 return 0;
2531
2532 fail_start_aen:
2533 fail_io_attach:
2534 megasas_mgmt_info.count--;
2535 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2536 megasas_mgmt_info.max_index--;
2537
2538 pci_set_drvdata(pdev, NULL);
Sumant Patrob274cab2006-10-03 12:52:12 -07002539 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002540 free_irq(instance->pdev->irq, instance);
2541
2542 megasas_release_mfi(instance);
2543
2544 fail_irq:
2545 fail_init_mfi:
2546 fail_alloc_dma_buf:
2547 if (instance->evt_detail)
2548 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2549 instance->evt_detail,
2550 instance->evt_detail_h);
2551
2552 if (instance->producer)
2553 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2554 instance->producer_h);
2555 if (instance->consumer)
2556 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2557 instance->consumer_h);
2558 scsi_host_put(host);
2559
2560 fail_alloc_instance:
2561 fail_set_dma_mask:
2562 pci_disable_device(pdev);
2563
2564 return -ENODEV;
2565}
2566
2567/**
2568 * megasas_flush_cache - Requests FW to flush all its caches
2569 * @instance: Adapter soft state
2570 */
2571static void megasas_flush_cache(struct megasas_instance *instance)
2572{
2573 struct megasas_cmd *cmd;
2574 struct megasas_dcmd_frame *dcmd;
2575
2576 cmd = megasas_get_cmd(instance);
2577
2578 if (!cmd)
2579 return;
2580
2581 dcmd = &cmd->frame->dcmd;
2582
2583 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2584
2585 dcmd->cmd = MFI_CMD_DCMD;
2586 dcmd->cmd_status = 0x0;
2587 dcmd->sge_count = 0;
2588 dcmd->flags = MFI_FRAME_DIR_NONE;
2589 dcmd->timeout = 0;
2590 dcmd->data_xfer_len = 0;
2591 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
2592 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
2593
2594 megasas_issue_blocked_cmd(instance, cmd);
2595
2596 megasas_return_cmd(instance, cmd);
2597
2598 return;
2599}
2600
2601/**
2602 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2603 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05002604 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002605 */
bo yang31ea7082007-11-07 12:09:50 -05002606static void megasas_shutdown_controller(struct megasas_instance *instance,
2607 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002608{
2609 struct megasas_cmd *cmd;
2610 struct megasas_dcmd_frame *dcmd;
2611
2612 cmd = megasas_get_cmd(instance);
2613
2614 if (!cmd)
2615 return;
2616
2617 if (instance->aen_cmd)
2618 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
2619
2620 dcmd = &cmd->frame->dcmd;
2621
2622 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2623
2624 dcmd->cmd = MFI_CMD_DCMD;
2625 dcmd->cmd_status = 0x0;
2626 dcmd->sge_count = 0;
2627 dcmd->flags = MFI_FRAME_DIR_NONE;
2628 dcmd->timeout = 0;
2629 dcmd->data_xfer_len = 0;
bo yang31ea7082007-11-07 12:09:50 -05002630 dcmd->opcode = opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002631
2632 megasas_issue_blocked_cmd(instance, cmd);
2633
2634 megasas_return_cmd(instance, cmd);
2635
2636 return;
2637}
2638
2639/**
bo yangad84db22007-11-09 04:40:16 -05002640 * megasas_suspend - driver suspend entry point
2641 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05002642 * @state: PCI power state to suspend routine
2643 */
2644static int __devinit
2645megasas_suspend(struct pci_dev *pdev, pm_message_t state)
2646{
2647 struct Scsi_Host *host;
2648 struct megasas_instance *instance;
2649
2650 instance = pci_get_drvdata(pdev);
2651 host = instance->host;
2652
bo yangad84db22007-11-09 04:40:16 -05002653 if (poll_mode_io)
2654 del_timer_sync(&instance->io_completion_timer);
2655
bo yang31ea7082007-11-07 12:09:50 -05002656 megasas_flush_cache(instance);
2657 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
2658 tasklet_kill(&instance->isr_tasklet);
2659
2660 pci_set_drvdata(instance->pdev, instance);
2661 instance->instancet->disable_intr(instance->reg_set);
2662 free_irq(instance->pdev->irq, instance);
2663
2664 pci_save_state(pdev);
2665 pci_disable_device(pdev);
2666
2667 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2668
2669 return 0;
2670}
2671
2672/**
2673 * megasas_resume- driver resume entry point
2674 * @pdev: PCI device structure
2675 */
2676static int __devinit
2677megasas_resume(struct pci_dev *pdev)
2678{
2679 int rval;
2680 struct Scsi_Host *host;
2681 struct megasas_instance *instance;
2682
2683 instance = pci_get_drvdata(pdev);
2684 host = instance->host;
2685 pci_set_power_state(pdev, PCI_D0);
2686 pci_enable_wake(pdev, PCI_D0, 0);
2687 pci_restore_state(pdev);
2688
2689 /*
2690 * PCI prepping: enable device set bus mastering and dma mask
2691 */
2692 rval = pci_enable_device(pdev);
2693
2694 if (rval) {
2695 printk(KERN_ERR "megasas: Enable device failed\n");
2696 return rval;
2697 }
2698
2699 pci_set_master(pdev);
2700
2701 if (megasas_set_dma_mask(pdev))
2702 goto fail_set_dma_mask;
2703
2704 /*
2705 * Initialize MFI Firmware
2706 */
2707
2708 *instance->producer = 0;
2709 *instance->consumer = 0;
2710
2711 atomic_set(&instance->fw_outstanding, 0);
2712
2713 /*
2714 * We expect the FW state to be READY
2715 */
2716 if (megasas_transition_to_ready(instance))
2717 goto fail_ready_state;
2718
2719 if (megasas_issue_init_mfi(instance))
2720 goto fail_init_mfi;
2721
2722 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2723 (unsigned long)instance);
2724
2725 /*
2726 * Register IRQ
2727 */
2728 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED,
2729 "megasas", instance)) {
2730 printk(KERN_ERR "megasas: Failed to register IRQ\n");
2731 goto fail_irq;
2732 }
2733
2734 instance->instancet->enable_intr(instance->reg_set);
2735
2736 /*
2737 * Initiate AEN (Asynchronous Event Notification)
2738 */
2739 if (megasas_start_aen(instance))
2740 printk(KERN_ERR "megasas: Start AEN failed\n");
2741
bo yangad84db22007-11-09 04:40:16 -05002742 /* Initialize the cmd completion timer */
2743 if (poll_mode_io)
2744 megasas_start_timer(instance, &instance->io_completion_timer,
2745 megasas_io_completion_timer,
2746 MEGASAS_COMPLETION_TIMER_INTERVAL);
bo yang31ea7082007-11-07 12:09:50 -05002747 return 0;
2748
2749fail_irq:
2750fail_init_mfi:
2751 if (instance->evt_detail)
2752 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2753 instance->evt_detail,
2754 instance->evt_detail_h);
2755
2756 if (instance->producer)
2757 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2758 instance->producer_h);
2759 if (instance->consumer)
2760 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2761 instance->consumer_h);
2762 scsi_host_put(host);
2763
2764fail_set_dma_mask:
2765fail_ready_state:
2766
2767 pci_disable_device(pdev);
2768
2769 return -ENODEV;
2770}
2771
2772/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002773 * megasas_detach_one - PCI hot"un"plug entry point
2774 * @pdev: PCI device structure
2775 */
2776static void megasas_detach_one(struct pci_dev *pdev)
2777{
2778 int i;
2779 struct Scsi_Host *host;
2780 struct megasas_instance *instance;
2781
2782 instance = pci_get_drvdata(pdev);
2783 host = instance->host;
2784
bo yangad84db22007-11-09 04:40:16 -05002785 if (poll_mode_io)
2786 del_timer_sync(&instance->io_completion_timer);
2787
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002788 scsi_remove_host(instance->host);
2789 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05002790 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Sumant Patro5d018ad2006-10-03 13:13:18 -07002791 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002792
2793 /*
2794 * Take the instance off the instance array. Note that we will not
2795 * decrement the max_index. We let this array be sparse array
2796 */
2797 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2798 if (megasas_mgmt_info.instance[i] == instance) {
2799 megasas_mgmt_info.count--;
2800 megasas_mgmt_info.instance[i] = NULL;
2801
2802 break;
2803 }
2804 }
2805
2806 pci_set_drvdata(instance->pdev, NULL);
2807
Sumant Patrob274cab2006-10-03 12:52:12 -07002808 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002809
2810 free_irq(instance->pdev->irq, instance);
2811
2812 megasas_release_mfi(instance);
2813
2814 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2815 instance->evt_detail, instance->evt_detail_h);
2816
2817 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2818 instance->producer_h);
2819
2820 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2821 instance->consumer_h);
2822
2823 scsi_host_put(host);
2824
2825 pci_set_drvdata(pdev, NULL);
2826
2827 pci_disable_device(pdev);
2828
2829 return;
2830}
2831
2832/**
2833 * megasas_shutdown - Shutdown entry point
2834 * @device: Generic device structure
2835 */
2836static void megasas_shutdown(struct pci_dev *pdev)
2837{
2838 struct megasas_instance *instance = pci_get_drvdata(pdev);
2839 megasas_flush_cache(instance);
2840}
2841
2842/**
2843 * megasas_mgmt_open - char node "open" entry point
2844 */
2845static int megasas_mgmt_open(struct inode *inode, struct file *filep)
2846{
2847 /*
2848 * Allow only those users with admin rights
2849 */
2850 if (!capable(CAP_SYS_ADMIN))
2851 return -EACCES;
2852
2853 return 0;
2854}
2855
2856/**
2857 * megasas_mgmt_release - char node "release" entry point
2858 */
2859static int megasas_mgmt_release(struct inode *inode, struct file *filep)
2860{
2861 filep->private_data = NULL;
2862 fasync_helper(-1, filep, 0, &megasas_async_queue);
2863
2864 return 0;
2865}
2866
2867/**
2868 * megasas_mgmt_fasync - Async notifier registration from applications
2869 *
2870 * This function adds the calling process to a driver global queue. When an
2871 * event occurs, SIGIO will be sent to all processes in this queue.
2872 */
2873static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
2874{
2875 int rc;
2876
Arjan van de Ven0b950672006-01-11 13:16:10 +01002877 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002878
2879 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
2880
Arjan van de Ven0b950672006-01-11 13:16:10 +01002881 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002882
2883 if (rc >= 0) {
2884 /* For sanity check when we get ioctl */
2885 filep->private_data = filep;
2886 return 0;
2887 }
2888
2889 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
2890
2891 return rc;
2892}
2893
2894/**
2895 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
2896 * @instance: Adapter soft state
2897 * @argp: User's ioctl packet
2898 */
2899static int
2900megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
2901 struct megasas_iocpacket __user * user_ioc,
2902 struct megasas_iocpacket *ioc)
2903{
2904 struct megasas_sge32 *kern_sge32;
2905 struct megasas_cmd *cmd;
2906 void *kbuff_arr[MAX_IOCTL_SGE];
2907 dma_addr_t buf_handle = 0;
2908 int error = 0, i;
2909 void *sense = NULL;
2910 dma_addr_t sense_handle;
2911 u32 *sense_ptr;
bo yangb10c36a2007-11-09 04:28:47 -05002912 unsigned long *sense_buff;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002913
2914 memset(kbuff_arr, 0, sizeof(kbuff_arr));
2915
2916 if (ioc->sge_count > MAX_IOCTL_SGE) {
2917 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
2918 ioc->sge_count, MAX_IOCTL_SGE);
2919 return -EINVAL;
2920 }
2921
2922 cmd = megasas_get_cmd(instance);
2923 if (!cmd) {
2924 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
2925 return -ENOMEM;
2926 }
2927
2928 /*
2929 * User's IOCTL packet has 2 frames (maximum). Copy those two
2930 * frames into our cmd's frames. cmd->frame's context will get
2931 * overwritten when we copy from user's frames. So set that value
2932 * alone separately
2933 */
2934 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
2935 cmd->frame->hdr.context = cmd->index;
2936
2937 /*
2938 * The management interface between applications and the fw uses
2939 * MFI frames. E.g, RAID configuration changes, LD property changes
2940 * etc are accomplishes through different kinds of MFI frames. The
2941 * driver needs to care only about substituting user buffers with
2942 * kernel buffers in SGLs. The location of SGL is embedded in the
2943 * struct iocpacket itself.
2944 */
2945 kern_sge32 = (struct megasas_sge32 *)
2946 ((unsigned long)cmd->frame + ioc->sgl_off);
2947
2948 /*
2949 * For each user buffer, create a mirror buffer and copy in
2950 */
2951 for (i = 0; i < ioc->sge_count; i++) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08002952 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002953 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08002954 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002955 if (!kbuff_arr[i]) {
2956 printk(KERN_DEBUG "megasas: Failed to alloc "
2957 "kernel SGL buffer for IOCTL \n");
2958 error = -ENOMEM;
2959 goto out;
2960 }
2961
2962 /*
2963 * We don't change the dma_coherent_mask, so
2964 * pci_alloc_consistent only returns 32bit addresses
2965 */
2966 kern_sge32[i].phys_addr = (u32) buf_handle;
2967 kern_sge32[i].length = ioc->sgl[i].iov_len;
2968
2969 /*
2970 * We created a kernel buffer corresponding to the
2971 * user buffer. Now copy in from the user buffer
2972 */
2973 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
2974 (u32) (ioc->sgl[i].iov_len))) {
2975 error = -EFAULT;
2976 goto out;
2977 }
2978 }
2979
2980 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08002981 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
2982 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002983 if (!sense) {
2984 error = -ENOMEM;
2985 goto out;
2986 }
2987
2988 sense_ptr =
2989 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
2990 *sense_ptr = sense_handle;
2991 }
2992
2993 /*
2994 * Set the sync_cmd flag so that the ISR knows not to complete this
2995 * cmd to the SCSI mid-layer
2996 */
2997 cmd->sync_cmd = 1;
2998 megasas_issue_blocked_cmd(instance, cmd);
2999 cmd->sync_cmd = 0;
3000
3001 /*
3002 * copy out the kernel buffers to user buffers
3003 */
3004 for (i = 0; i < ioc->sge_count; i++) {
3005 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
3006 ioc->sgl[i].iov_len)) {
3007 error = -EFAULT;
3008 goto out;
3009 }
3010 }
3011
3012 /*
3013 * copy out the sense
3014 */
3015 if (ioc->sense_len) {
3016 /*
bo yangb10c36a2007-11-09 04:28:47 -05003017 * sense_buff points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003018 * sense buffer address
3019 */
bo yangb10c36a2007-11-09 04:28:47 -05003020 sense_buff = (unsigned long *) ((unsigned long)ioc->frame.raw +
3021 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003022
bo yangb10c36a2007-11-09 04:28:47 -05003023 if (copy_to_user((void __user *)(unsigned long)(*sense_buff),
3024 sense, ioc->sense_len)) {
3025 printk(KERN_ERR "megasas: Failed to copy out to user "
3026 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003027 error = -EFAULT;
3028 goto out;
3029 }
3030 }
3031
3032 /*
3033 * copy the status codes returned by the fw
3034 */
3035 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
3036 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
3037 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
3038 error = -EFAULT;
3039 }
3040
3041 out:
3042 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08003043 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003044 sense, sense_handle);
3045 }
3046
3047 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08003048 dma_free_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003049 kern_sge32[i].length,
3050 kbuff_arr[i], kern_sge32[i].phys_addr);
3051 }
3052
3053 megasas_return_cmd(instance, cmd);
3054 return error;
3055}
3056
3057static struct megasas_instance *megasas_lookup_instance(u16 host_no)
3058{
3059 int i;
3060
3061 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3062
3063 if ((megasas_mgmt_info.instance[i]) &&
3064 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
3065 return megasas_mgmt_info.instance[i];
3066 }
3067
3068 return NULL;
3069}
3070
3071static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
3072{
3073 struct megasas_iocpacket __user *user_ioc =
3074 (struct megasas_iocpacket __user *)arg;
3075 struct megasas_iocpacket *ioc;
3076 struct megasas_instance *instance;
3077 int error;
3078
3079 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
3080 if (!ioc)
3081 return -ENOMEM;
3082
3083 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
3084 error = -EFAULT;
3085 goto out_kfree_ioc;
3086 }
3087
3088 instance = megasas_lookup_instance(ioc->host_no);
3089 if (!instance) {
3090 error = -ENODEV;
3091 goto out_kfree_ioc;
3092 }
3093
3094 /*
3095 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
3096 */
3097 if (down_interruptible(&instance->ioctl_sem)) {
3098 error = -ERESTARTSYS;
3099 goto out_kfree_ioc;
3100 }
3101 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
3102 up(&instance->ioctl_sem);
3103
3104 out_kfree_ioc:
3105 kfree(ioc);
3106 return error;
3107}
3108
3109static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
3110{
3111 struct megasas_instance *instance;
3112 struct megasas_aen aen;
3113 int error;
3114
3115 if (file->private_data != file) {
3116 printk(KERN_DEBUG "megasas: fasync_helper was not "
3117 "called first\n");
3118 return -EINVAL;
3119 }
3120
3121 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
3122 return -EFAULT;
3123
3124 instance = megasas_lookup_instance(aen.host_no);
3125
3126 if (!instance)
3127 return -ENODEV;
3128
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02003129 mutex_lock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003130 error = megasas_register_aen(instance, aen.seq_num,
3131 aen.class_locale_word);
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02003132 mutex_unlock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003133 return error;
3134}
3135
3136/**
3137 * megasas_mgmt_ioctl - char node ioctl entry point
3138 */
3139static long
3140megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3141{
3142 switch (cmd) {
3143 case MEGASAS_IOC_FIRMWARE:
3144 return megasas_mgmt_ioctl_fw(file, arg);
3145
3146 case MEGASAS_IOC_GET_AEN:
3147 return megasas_mgmt_ioctl_aen(file, arg);
3148 }
3149
3150 return -ENOTTY;
3151}
3152
3153#ifdef CONFIG_COMPAT
3154static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
3155{
3156 struct compat_megasas_iocpacket __user *cioc =
3157 (struct compat_megasas_iocpacket __user *)arg;
3158 struct megasas_iocpacket __user *ioc =
3159 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
3160 int i;
3161 int error = 0;
3162
Jeff Garzik83aabc12006-10-04 06:34:03 -04003163 if (clear_user(ioc, sizeof(*ioc)))
3164 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003165
3166 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
3167 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
3168 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
3169 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
3170 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
3171 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
3172 return -EFAULT;
3173
3174 for (i = 0; i < MAX_IOCTL_SGE; i++) {
3175 compat_uptr_t ptr;
3176
3177 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
3178 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
3179 copy_in_user(&ioc->sgl[i].iov_len,
3180 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
3181 return -EFAULT;
3182 }
3183
3184 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
3185
3186 if (copy_in_user(&cioc->frame.hdr.cmd_status,
3187 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
3188 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
3189 return -EFAULT;
3190 }
3191 return error;
3192}
3193
3194static long
3195megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
3196 unsigned long arg)
3197{
3198 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08003199 case MEGASAS_IOC_FIRMWARE32:
3200 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003201 case MEGASAS_IOC_GET_AEN:
3202 return megasas_mgmt_ioctl_aen(file, arg);
3203 }
3204
3205 return -ENOTTY;
3206}
3207#endif
3208
3209/*
3210 * File operations structure for management interface
3211 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08003212static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003213 .owner = THIS_MODULE,
3214 .open = megasas_mgmt_open,
3215 .release = megasas_mgmt_release,
3216 .fasync = megasas_mgmt_fasync,
3217 .unlocked_ioctl = megasas_mgmt_ioctl,
3218#ifdef CONFIG_COMPAT
3219 .compat_ioctl = megasas_mgmt_compat_ioctl,
3220#endif
3221};
3222
3223/*
3224 * PCI hotplug support registration structure
3225 */
3226static struct pci_driver megasas_pci_driver = {
3227
3228 .name = "megaraid_sas",
3229 .id_table = megasas_pci_table,
3230 .probe = megasas_probe_one,
3231 .remove = __devexit_p(megasas_detach_one),
bo yang31ea7082007-11-07 12:09:50 -05003232 .suspend = megasas_suspend,
3233 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003234 .shutdown = megasas_shutdown,
3235};
3236
3237/*
3238 * Sysfs driver attributes
3239 */
3240static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
3241{
3242 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
3243 MEGASAS_VERSION);
3244}
3245
3246static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
3247
3248static ssize_t
3249megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
3250{
3251 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
3252 MEGASAS_RELDATE);
3253}
3254
3255static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
3256 NULL);
3257
Sumant Patro658dced2006-10-03 13:09:14 -07003258static ssize_t
3259megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
3260{
bo yangad84db22007-11-09 04:40:16 -05003261 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07003262}
3263
3264static ssize_t
3265megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
3266{
3267 int retval = count;
3268 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
3269 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
3270 retval = -EINVAL;
3271 }
3272 return retval;
3273}
3274
3275static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUGO, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05003276 megasas_sysfs_set_dbg_lvl);
3277
3278static ssize_t
3279megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
3280{
3281 return sprintf(buf, "%u\n", poll_mode_io);
3282}
3283
3284static ssize_t
3285megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
3286 const char *buf, size_t count)
3287{
3288 int retval = count;
3289 int tmp = poll_mode_io;
3290 int i;
3291 struct megasas_instance *instance;
3292
3293 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
3294 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
3295 retval = -EINVAL;
3296 }
3297
3298 /*
3299 * Check if poll_mode_io is already set or is same as previous value
3300 */
3301 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
3302 goto out;
3303
3304 if (poll_mode_io) {
3305 /*
3306 * Start timers for all adapters
3307 */
3308 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3309 instance = megasas_mgmt_info.instance[i];
3310 if (instance) {
3311 megasas_start_timer(instance,
3312 &instance->io_completion_timer,
3313 megasas_io_completion_timer,
3314 MEGASAS_COMPLETION_TIMER_INTERVAL);
3315 }
3316 }
3317 } else {
3318 /*
3319 * Delete timers for all adapters
3320 */
3321 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3322 instance = megasas_mgmt_info.instance[i];
3323 if (instance)
3324 del_timer_sync(&instance->io_completion_timer);
3325 }
3326 }
3327
3328out:
3329 return retval;
3330}
3331
3332static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUGO,
3333 megasas_sysfs_show_poll_mode_io,
3334 megasas_sysfs_set_poll_mode_io);
Sumant Patro658dced2006-10-03 13:09:14 -07003335
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003336/**
3337 * megasas_init - Driver load entry point
3338 */
3339static int __init megasas_init(void)
3340{
3341 int rval;
3342
3343 /*
3344 * Announce driver version and other information
3345 */
3346 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
3347 MEGASAS_EXT_VERSION);
3348
3349 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
3350
3351 /*
3352 * Register character device node
3353 */
3354 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
3355
3356 if (rval < 0) {
3357 printk(KERN_DEBUG "megasas: failed to open device node\n");
3358 return rval;
3359 }
3360
3361 megasas_mgmt_majorno = rval;
3362
3363 /*
3364 * Register ourselves as PCI hotplug module
3365 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00003366 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003367
3368 if (rval) {
3369 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04003370 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003371 }
3372
Jeff Garzik83aabc12006-10-04 06:34:03 -04003373 rval = driver_create_file(&megasas_pci_driver.driver,
3374 &driver_attr_version);
3375 if (rval)
3376 goto err_dcf_attr_ver;
3377 rval = driver_create_file(&megasas_pci_driver.driver,
3378 &driver_attr_release_date);
3379 if (rval)
3380 goto err_dcf_rel_date;
3381 rval = driver_create_file(&megasas_pci_driver.driver,
3382 &driver_attr_dbg_lvl);
3383 if (rval)
3384 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05003385 rval = driver_create_file(&megasas_pci_driver.driver,
3386 &driver_attr_poll_mode_io);
3387 if (rval)
3388 goto err_dcf_poll_mode_io;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003389
3390 return rval;
bo yangad84db22007-11-09 04:40:16 -05003391
3392err_dcf_poll_mode_io:
3393 driver_remove_file(&megasas_pci_driver.driver,
3394 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04003395err_dcf_dbg_lvl:
3396 driver_remove_file(&megasas_pci_driver.driver,
3397 &driver_attr_release_date);
3398err_dcf_rel_date:
3399 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3400err_dcf_attr_ver:
3401 pci_unregister_driver(&megasas_pci_driver);
3402err_pcidrv:
3403 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3404 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003405}
3406
3407/**
3408 * megasas_exit - Driver unload entry point
3409 */
3410static void __exit megasas_exit(void)
3411{
Sumant Patro658dced2006-10-03 13:09:14 -07003412 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05003413 &driver_attr_poll_mode_io);
3414 driver_remove_file(&megasas_pci_driver.driver,
Sumant Patro658dced2006-10-03 13:09:14 -07003415 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04003416 driver_remove_file(&megasas_pci_driver.driver,
3417 &driver_attr_release_date);
3418 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003419
3420 pci_unregister_driver(&megasas_pci_driver);
3421 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3422}
3423
3424module_init(megasas_init);
3425module_exit(megasas_exit);