blob: ebb948c016bbfa871db78ebc3ce5508562440585 [file] [log] [blame]
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001/*
2 *
3 * Linux MegaRAID driver for SAS based RAID controllers
4 *
5 * Copyright (c) 2003-2005 LSI Logic Corporation.
6 *
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
Sumant Patro05e9ebb2007-05-17 05:47:51 -070013 * Version : v00.00.03.10-rc5
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
49MODULE_LICENSE("GPL");
50MODULE_VERSION(MEGASAS_VERSION);
Sumant Patro3d6d1742006-12-29 08:13:54 -080051MODULE_AUTHOR("megaraidlinux@lsi.com");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040052MODULE_DESCRIPTION("LSI Logic MegaRAID SAS Driver");
53
54/*
55 * PCI ID table for all supported controllers
56 */
57static struct pci_device_id megasas_pci_table[] = {
58
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +020059 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
60 /* xscale IOP */
61 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
62 /* ppc IOP */
63 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
64 /* xscale IOP, vega */
65 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
66 /* xscale IOP */
67 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040068};
69
70MODULE_DEVICE_TABLE(pci, megasas_pci_table);
71
72static int megasas_mgmt_majorno;
73static struct megasas_mgmt_info megasas_mgmt_info;
74static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +010075static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040076
Sumant Patro658dced2006-10-03 13:09:14 -070077static u32 megasas_dbg_lvl;
78
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040079/**
80 * megasas_get_cmd - Get a command from the free pool
81 * @instance: Adapter soft state
82 *
83 * Returns a free command from the pool
84 */
Arjan van de Ven858119e2006-01-14 13:20:43 -080085static struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040086 *instance)
87{
88 unsigned long flags;
89 struct megasas_cmd *cmd = NULL;
90
91 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
92
93 if (!list_empty(&instance->cmd_pool)) {
94 cmd = list_entry((&instance->cmd_pool)->next,
95 struct megasas_cmd, list);
96 list_del_init(&cmd->list);
97 } else {
98 printk(KERN_ERR "megasas: Command pool empty!\n");
99 }
100
101 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
102 return cmd;
103}
104
105/**
106 * megasas_return_cmd - Return a cmd to free command pool
107 * @instance: Adapter soft state
108 * @cmd: Command packet to be returned to free command pool
109 */
110static inline void
111megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
112{
113 unsigned long flags;
114
115 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
116
117 cmd->scmd = NULL;
118 list_add_tail(&cmd->list, &instance->cmd_pool);
119
120 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
121}
122
Sumant Patro1341c932006-01-25 12:02:40 -0800123
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400124/**
Sumant Patro1341c932006-01-25 12:02:40 -0800125* The following functions are defined for xscale
126* (deviceid : 1064R, PERC5) controllers
127*/
128
129/**
130 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400131 * @regs: MFI register set
132 */
133static inline void
Sumant Patro1341c932006-01-25 12:02:40 -0800134megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400135{
136 writel(1, &(regs)->outbound_intr_mask);
137
138 /* Dummy readl to force pci flush */
139 readl(&regs->outbound_intr_mask);
140}
141
142/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700143 * megasas_disable_intr_xscale -Disables interrupt
144 * @regs: MFI register set
145 */
146static inline void
147megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
148{
149 u32 mask = 0x1f;
150 writel(mask, &regs->outbound_intr_mask);
151 /* Dummy readl to force pci flush */
152 readl(&regs->outbound_intr_mask);
153}
154
155/**
Sumant Patro1341c932006-01-25 12:02:40 -0800156 * megasas_read_fw_status_reg_xscale - returns the current FW status value
157 * @regs: MFI register set
158 */
159static u32
160megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
161{
162 return readl(&(regs)->outbound_msg_0);
163}
164/**
165 * megasas_clear_interrupt_xscale - Check & clear interrupt
166 * @regs: MFI register set
167 */
168static int
169megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
170{
171 u32 status;
172 /*
173 * Check if it is our interrupt
174 */
175 status = readl(&regs->outbound_intr_status);
176
177 if (!(status & MFI_OB_INTR_STATUS_MASK)) {
178 return 1;
179 }
180
181 /*
182 * Clear the interrupt by writing back the same value
183 */
184 writel(status, &regs->outbound_intr_status);
185
186 return 0;
187}
188
189/**
190 * megasas_fire_cmd_xscale - Sends command to the FW
191 * @frame_phys_addr : Physical address of cmd
192 * @frame_count : Number of frames for the command
193 * @regs : MFI register set
194 */
195static inline void
196megasas_fire_cmd_xscale(dma_addr_t frame_phys_addr,u32 frame_count, struct megasas_register_set __iomem *regs)
197{
198 writel((frame_phys_addr >> 3)|(frame_count),
199 &(regs)->inbound_queue_port);
200}
201
202static struct megasas_instance_template megasas_instance_template_xscale = {
203
204 .fire_cmd = megasas_fire_cmd_xscale,
205 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700206 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800207 .clear_intr = megasas_clear_intr_xscale,
208 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
209};
210
211/**
212* This is the end of set of functions & definitions specific
213* to xscale (deviceid : 1064R, PERC5) controllers
214*/
215
216/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800217* The following functions are defined for ppc (deviceid : 0x60)
218* controllers
219*/
220
221/**
222 * megasas_enable_intr_ppc - Enables interrupts
223 * @regs: MFI register set
224 */
225static inline void
226megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
227{
228 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
229
230 writel(~0x80000004, &(regs)->outbound_intr_mask);
231
232 /* Dummy readl to force pci flush */
233 readl(&regs->outbound_intr_mask);
234}
235
236/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700237 * megasas_disable_intr_ppc - Disable interrupt
238 * @regs: MFI register set
239 */
240static inline void
241megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
242{
243 u32 mask = 0xFFFFFFFF;
244 writel(mask, &regs->outbound_intr_mask);
245 /* Dummy readl to force pci flush */
246 readl(&regs->outbound_intr_mask);
247}
248
249/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800250 * megasas_read_fw_status_reg_ppc - returns the current FW status value
251 * @regs: MFI register set
252 */
253static u32
254megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
255{
256 return readl(&(regs)->outbound_scratch_pad);
257}
258
259/**
260 * megasas_clear_interrupt_ppc - Check & clear interrupt
261 * @regs: MFI register set
262 */
263static int
264megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
265{
266 u32 status;
267 /*
268 * Check if it is our interrupt
269 */
270 status = readl(&regs->outbound_intr_status);
271
272 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
273 return 1;
274 }
275
276 /*
277 * Clear the interrupt by writing back the same value
278 */
279 writel(status, &regs->outbound_doorbell_clear);
280
281 return 0;
282}
283/**
284 * megasas_fire_cmd_ppc - Sends command to the FW
285 * @frame_phys_addr : Physical address of cmd
286 * @frame_count : Number of frames for the command
287 * @regs : MFI register set
288 */
289static inline void
290megasas_fire_cmd_ppc(dma_addr_t frame_phys_addr, u32 frame_count, struct megasas_register_set __iomem *regs)
291{
292 writel((frame_phys_addr | (frame_count<<1))|1,
293 &(regs)->inbound_queue_port);
294}
295
296static struct megasas_instance_template megasas_instance_template_ppc = {
297
298 .fire_cmd = megasas_fire_cmd_ppc,
299 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700300 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800301 .clear_intr = megasas_clear_intr_ppc,
302 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
303};
304
305/**
306* This is the end of set of functions & definitions
307* specific to ppc (deviceid : 0x60) controllers
308*/
309
310/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400311 * megasas_issue_polled - Issues a polling command
312 * @instance: Adapter soft state
313 * @cmd: Command packet to be issued
314 *
315 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
316 */
317static int
318megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
319{
320 int i;
321 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
322
323 struct megasas_header *frame_hdr = &cmd->frame->hdr;
324
325 frame_hdr->cmd_status = 0xFF;
326 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
327
328 /*
329 * Issue the frame using inbound queue port
330 */
Sumant Patro1341c932006-01-25 12:02:40 -0800331 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400332
333 /*
334 * Wait for cmd_status to change
335 */
336 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
337 rmb();
338 msleep(1);
339 }
340
341 if (frame_hdr->cmd_status == 0xff)
342 return -ETIME;
343
344 return 0;
345}
346
347/**
348 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
349 * @instance: Adapter soft state
350 * @cmd: Command to be issued
351 *
352 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -0700353 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400354 * Used to issue ioctl commands.
355 */
356static int
357megasas_issue_blocked_cmd(struct megasas_instance *instance,
358 struct megasas_cmd *cmd)
359{
360 cmd->cmd_status = ENODATA;
361
Sumant Patro1341c932006-01-25 12:02:40 -0800362 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400363
Sumant Patro2a3681e2006-10-03 13:19:21 -0700364 wait_event_timeout(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA),
365 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400366
367 return 0;
368}
369
370/**
371 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
372 * @instance: Adapter soft state
373 * @cmd_to_abort: Previously issued cmd to be aborted
374 *
375 * MFI firmware can abort previously issued AEN comamnd (automatic event
376 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -0700377 * cmd and waits for return status.
378 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400379 */
380static int
381megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
382 struct megasas_cmd *cmd_to_abort)
383{
384 struct megasas_cmd *cmd;
385 struct megasas_abort_frame *abort_fr;
386
387 cmd = megasas_get_cmd(instance);
388
389 if (!cmd)
390 return -1;
391
392 abort_fr = &cmd->frame->abort;
393
394 /*
395 * Prepare and issue the abort frame
396 */
397 abort_fr->cmd = MFI_CMD_ABORT;
398 abort_fr->cmd_status = 0xFF;
399 abort_fr->flags = 0;
400 abort_fr->abort_context = cmd_to_abort->index;
401 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
402 abort_fr->abort_mfi_phys_addr_hi = 0;
403
404 cmd->sync_cmd = 1;
405 cmd->cmd_status = 0xFF;
406
Sumant Patro1341c932006-01-25 12:02:40 -0800407 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400408
409 /*
410 * Wait for this cmd to complete
411 */
Sumant Patro2a3681e2006-10-03 13:19:21 -0700412 wait_event_timeout(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF),
413 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400414
415 megasas_return_cmd(instance, cmd);
416 return 0;
417}
418
419/**
420 * megasas_make_sgl32 - Prepares 32-bit SGL
421 * @instance: Adapter soft state
422 * @scp: SCSI command from the mid-layer
423 * @mfi_sgl: SGL to be filled in
424 *
425 * If successful, this function returns the number of SG elements. Otherwise,
426 * it returnes -1.
427 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800428static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400429megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
430 union megasas_sgl *mfi_sgl)
431{
432 int i;
433 int sge_count;
434 struct scatterlist *os_sgl;
435
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900436 sge_count = scsi_dma_map(scp);
437 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400438
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900439 if (sge_count) {
440 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
441 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
442 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
443 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400444 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400445 return sge_count;
446}
447
448/**
449 * megasas_make_sgl64 - Prepares 64-bit SGL
450 * @instance: Adapter soft state
451 * @scp: SCSI command from the mid-layer
452 * @mfi_sgl: SGL to be filled in
453 *
454 * If successful, this function returns the number of SG elements. Otherwise,
455 * it returnes -1.
456 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800457static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400458megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
459 union megasas_sgl *mfi_sgl)
460{
461 int i;
462 int sge_count;
463 struct scatterlist *os_sgl;
464
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900465 sge_count = scsi_dma_map(scp);
466 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400467
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900468 if (sge_count) {
469 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
470 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
471 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
472 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400473 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400474 return sge_count;
475}
476
Sumant Patrob1df99d2006-10-03 12:40:47 -0700477 /**
478 * megasas_get_frame_count - Computes the number of frames
479 * @sge_count : number of sg elements
480 *
481 * Returns the number of frames required for numnber of sge's (sge_count)
482 */
483
Adrian Bunkb448de42006-11-20 03:23:49 +0100484static u32 megasas_get_frame_count(u8 sge_count)
Sumant Patrob1df99d2006-10-03 12:40:47 -0700485{
486 int num_cnt;
487 int sge_bytes;
488 u32 sge_sz;
489 u32 frame_count=0;
490
491 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
492 sizeof(struct megasas_sge32);
493
494 /*
495 * Main frame can contain 2 SGEs for 64-bit SGLs and
496 * 3 SGEs for 32-bit SGLs
497 */
498 if (IS_DMA64)
499 num_cnt = sge_count - 2;
500 else
501 num_cnt = sge_count - 3;
502
503 if(num_cnt>0){
504 sge_bytes = sge_sz * num_cnt;
505
506 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
507 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
508 }
509 /* Main frame */
510 frame_count +=1;
511
512 if (frame_count > 7)
513 frame_count = 8;
514 return frame_count;
515}
516
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400517/**
518 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
519 * @instance: Adapter soft state
520 * @scp: SCSI command
521 * @cmd: Command to be prepared in
522 *
523 * This function prepares CDB commands. These are typcially pass-through
524 * commands to the devices.
525 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800526static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400527megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
528 struct megasas_cmd *cmd)
529{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400530 u32 is_logical;
531 u32 device_id;
532 u16 flags = 0;
533 struct megasas_pthru_frame *pthru;
534
535 is_logical = MEGASAS_IS_LOGICAL(scp);
536 device_id = MEGASAS_DEV_INDEX(instance, scp);
537 pthru = (struct megasas_pthru_frame *)cmd->frame;
538
539 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
540 flags = MFI_FRAME_DIR_WRITE;
541 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
542 flags = MFI_FRAME_DIR_READ;
543 else if (scp->sc_data_direction == PCI_DMA_NONE)
544 flags = MFI_FRAME_DIR_NONE;
545
546 /*
547 * Prepare the DCDB frame
548 */
549 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
550 pthru->cmd_status = 0x0;
551 pthru->scsi_status = 0x0;
552 pthru->target_id = device_id;
553 pthru->lun = scp->device->lun;
554 pthru->cdb_len = scp->cmd_len;
555 pthru->timeout = 0;
556 pthru->flags = flags;
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900557 pthru->data_xfer_len = scsi_bufflen(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400558
559 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
560
561 /*
562 * Construct SGL
563 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400564 if (IS_DMA64) {
565 pthru->flags |= MFI_FRAME_SGL64;
566 pthru->sge_count = megasas_make_sgl64(instance, scp,
567 &pthru->sgl);
568 } else
569 pthru->sge_count = megasas_make_sgl32(instance, scp,
570 &pthru->sgl);
571
572 /*
573 * Sense info specific
574 */
575 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
576 pthru->sense_buf_phys_addr_hi = 0;
577 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
578
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400579 /*
580 * Compute the total number of frames this command consumes. FW uses
581 * this number to pull sufficient number of frames from host memory.
582 */
Sumant Patrob1df99d2006-10-03 12:40:47 -0700583 cmd->frame_count = megasas_get_frame_count(pthru->sge_count);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400584
585 return cmd->frame_count;
586}
587
588/**
589 * megasas_build_ldio - Prepares IOs to logical devices
590 * @instance: Adapter soft state
591 * @scp: SCSI command
592 * @cmd: Command to to be prepared
593 *
594 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
595 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800596static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400597megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
598 struct megasas_cmd *cmd)
599{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400600 u32 device_id;
601 u8 sc = scp->cmnd[0];
602 u16 flags = 0;
603 struct megasas_io_frame *ldio;
604
605 device_id = MEGASAS_DEV_INDEX(instance, scp);
606 ldio = (struct megasas_io_frame *)cmd->frame;
607
608 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
609 flags = MFI_FRAME_DIR_WRITE;
610 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
611 flags = MFI_FRAME_DIR_READ;
612
613 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -0700614 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400615 */
616 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
617 ldio->cmd_status = 0x0;
618 ldio->scsi_status = 0x0;
619 ldio->target_id = device_id;
620 ldio->timeout = 0;
621 ldio->reserved_0 = 0;
622 ldio->pad_0 = 0;
623 ldio->flags = flags;
624 ldio->start_lba_hi = 0;
625 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
626
627 /*
628 * 6-byte READ(0x08) or WRITE(0x0A) cdb
629 */
630 if (scp->cmd_len == 6) {
631 ldio->lba_count = (u32) scp->cmnd[4];
632 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
633 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
634
635 ldio->start_lba_lo &= 0x1FFFFF;
636 }
637
638 /*
639 * 10-byte READ(0x28) or WRITE(0x2A) cdb
640 */
641 else if (scp->cmd_len == 10) {
642 ldio->lba_count = (u32) scp->cmnd[8] |
643 ((u32) scp->cmnd[7] << 8);
644 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
645 ((u32) scp->cmnd[3] << 16) |
646 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
647 }
648
649 /*
650 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
651 */
652 else if (scp->cmd_len == 12) {
653 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
654 ((u32) scp->cmnd[7] << 16) |
655 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
656
657 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
658 ((u32) scp->cmnd[3] << 16) |
659 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
660 }
661
662 /*
663 * 16-byte READ(0x88) or WRITE(0x8A) cdb
664 */
665 else if (scp->cmd_len == 16) {
666 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
667 ((u32) scp->cmnd[11] << 16) |
668 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
669
670 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
671 ((u32) scp->cmnd[7] << 16) |
672 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
673
674 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
675 ((u32) scp->cmnd[3] << 16) |
676 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
677
678 }
679
680 /*
681 * Construct SGL
682 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400683 if (IS_DMA64) {
684 ldio->flags |= MFI_FRAME_SGL64;
685 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
686 } else
687 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
688
689 /*
690 * Sense info specific
691 */
692 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
693 ldio->sense_buf_phys_addr_hi = 0;
694 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
695
Sumant Patrob1df99d2006-10-03 12:40:47 -0700696 /*
697 * Compute the total number of frames this command consumes. FW uses
698 * this number to pull sufficient number of frames from host memory.
699 */
700 cmd->frame_count = megasas_get_frame_count(ldio->sge_count);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400701
702 return cmd->frame_count;
703}
704
705/**
Sumant Patrocb59aa62006-01-25 11:53:25 -0800706 * megasas_is_ldio - Checks if the cmd is for logical drive
707 * @scmd: SCSI command
708 *
709 * Called by megasas_queue_command to find out if the command to be queued
710 * is a logical drive command
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400711 */
Sumant Patrocb59aa62006-01-25 11:53:25 -0800712static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400713{
Sumant Patrocb59aa62006-01-25 11:53:25 -0800714 if (!MEGASAS_IS_LOGICAL(cmd))
715 return 0;
716 switch (cmd->cmnd[0]) {
717 case READ_10:
718 case WRITE_10:
719 case READ_12:
720 case WRITE_12:
721 case READ_6:
722 case WRITE_6:
723 case READ_16:
724 case WRITE_16:
725 return 1;
726 default:
727 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400728 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400729}
730
Sumant Patro658dced2006-10-03 13:09:14 -0700731 /**
732 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
733 * in FW
734 * @instance: Adapter soft state
735 */
736static inline void
737megasas_dump_pending_frames(struct megasas_instance *instance)
738{
739 struct megasas_cmd *cmd;
740 int i,n;
741 union megasas_sgl *mfi_sgl;
742 struct megasas_io_frame *ldio;
743 struct megasas_pthru_frame *pthru;
744 u32 sgcount;
745 u32 max_cmd = instance->max_fw_cmds;
746
747 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
748 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
749 if (IS_DMA64)
750 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
751 else
752 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
753
754 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
755 for (i = 0; i < max_cmd; i++) {
756 cmd = instance->cmd_list[i];
757 if(!cmd->scmd)
758 continue;
759 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
760 if (megasas_is_ldio(cmd->scmd)){
761 ldio = (struct megasas_io_frame *)cmd->frame;
762 mfi_sgl = &ldio->sgl;
763 sgcount = ldio->sge_count;
764 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);
765 }
766 else {
767 pthru = (struct megasas_pthru_frame *) cmd->frame;
768 mfi_sgl = &pthru->sgl;
769 sgcount = pthru->sge_count;
770 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);
771 }
772 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
773 for (n = 0; n < sgcount; n++){
774 if (IS_DMA64)
775 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) ;
776 else
777 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
778 }
779 }
780 printk(KERN_ERR "\n");
781 } /*for max_cmd*/
782 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
783 for (i = 0; i < max_cmd; i++) {
784
785 cmd = instance->cmd_list[i];
786
787 if(cmd->sync_cmd == 1){
788 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
789 }
790 }
791 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
792}
793
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400794/**
795 * megasas_queue_command - Queue entry point
796 * @scmd: SCSI command to be queued
797 * @done: Callback entry point
798 */
799static int
800megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
801{
802 u32 frame_count;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400803 struct megasas_cmd *cmd;
804 struct megasas_instance *instance;
805
806 instance = (struct megasas_instance *)
807 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -0800808
809 /* Don't process if we have already declared adapter dead */
810 if (instance->hw_crit_error)
811 return SCSI_MLQUEUE_HOST_BUSY;
812
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400813 scmd->scsi_done = done;
814 scmd->result = 0;
815
Sumant Patrocb59aa62006-01-25 11:53:25 -0800816 if (MEGASAS_IS_LOGICAL(scmd) &&
817 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
818 scmd->result = DID_BAD_TARGET << 16;
819 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400820 }
821
Sumant Patro02b01e02007-02-14 13:00:55 -0800822 switch (scmd->cmnd[0]) {
823 case SYNCHRONIZE_CACHE:
824 /*
825 * FW takes care of flush cache on its own
826 * No need to send it down
827 */
828 scmd->result = DID_OK << 16;
829 goto out_done;
830 default:
831 break;
832 }
833
Sumant Patrocb59aa62006-01-25 11:53:25 -0800834 cmd = megasas_get_cmd(instance);
835 if (!cmd)
836 return SCSI_MLQUEUE_HOST_BUSY;
837
838 /*
839 * Logical drive command
840 */
841 if (megasas_is_ldio(scmd))
842 frame_count = megasas_build_ldio(instance, scmd, cmd);
843 else
844 frame_count = megasas_build_dcdb(instance, scmd, cmd);
845
846 if (!frame_count)
847 goto out_return_cmd;
848
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400849 cmd->scmd = scmd;
Sumant Patro05e9ebb2007-05-17 05:47:51 -0700850 scmd->SCp.ptr = (char *)cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400851
852 /*
853 * Issue the command to the FW
854 */
Sumant Patroe4a082c2006-05-30 12:03:37 -0700855 atomic_inc(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400856
Sumant Patro1341c932006-01-25 12:02:40 -0800857 instance->instancet->fire_cmd(cmd->frame_phys_addr ,cmd->frame_count-1,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400858
859 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -0800860
861 out_return_cmd:
862 megasas_return_cmd(instance, cmd);
863 out_done:
864 done(scmd);
865 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400866}
867
Christoph Hellwig147aab62006-02-17 12:13:48 +0100868static int megasas_slave_configure(struct scsi_device *sdev)
869{
870 /*
871 * Don't export physical disk devices to the disk driver.
872 *
873 * FIXME: Currently we don't export them to the midlayer at all.
874 * That will be fixed once LSI engineers have audited the
875 * firmware for possible issues.
876 */
877 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS && sdev->type == TYPE_DISK)
878 return -ENXIO;
Christoph Hellwige5b3a652006-03-10 17:08:57 +0100879
880 /*
881 * The RAID firmware may require extended timeouts.
882 */
883 if (sdev->channel >= MEGASAS_MAX_PD_CHANNELS)
Sumant Patro05e9ebb2007-05-17 05:47:51 -0700884 sdev->timeout = MEGASAS_DEFAULT_CMD_TIMEOUT * HZ;
Christoph Hellwig147aab62006-02-17 12:13:48 +0100885 return 0;
886}
887
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400888/**
889 * megasas_wait_for_outstanding - Wait for all outstanding cmds
890 * @instance: Adapter soft state
891 *
892 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
893 * complete all its outstanding commands. Returns error if one or more IOs
894 * are pending after this time period. It also marks the controller dead.
895 */
896static int megasas_wait_for_outstanding(struct megasas_instance *instance)
897{
898 int i;
899 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
900
901 for (i = 0; i < wait_time; i++) {
902
Sumant Patroe4a082c2006-05-30 12:03:37 -0700903 int outstanding = atomic_read(&instance->fw_outstanding);
904
905 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400906 break;
907
908 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
909 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -0700910 "commands to complete\n",i,outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400911 }
912
913 msleep(1000);
914 }
915
Sumant Patroe4a082c2006-05-30 12:03:37 -0700916 if (atomic_read(&instance->fw_outstanding)) {
Sumant Patroe3bbff92006-10-03 12:28:49 -0700917 /*
918 * Send signal to FW to stop processing any pending cmds.
919 * The controller will be taken offline by the OS now.
920 */
921 writel(MFI_STOP_ADP,
922 &instance->reg_set->inbound_doorbell);
Sumant Patro658dced2006-10-03 13:09:14 -0700923 megasas_dump_pending_frames(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400924 instance->hw_crit_error = 1;
925 return FAILED;
926 }
927
928 return SUCCESS;
929}
930
931/**
932 * megasas_generic_reset - Generic reset routine
933 * @scmd: Mid-layer SCSI command
934 *
935 * This routine implements a generic reset handler for device, bus and host
936 * reset requests. Device, bus and host specific reset handlers can use this
937 * function after they do their specific tasks.
938 */
939static int megasas_generic_reset(struct scsi_cmnd *scmd)
940{
941 int ret_val;
942 struct megasas_instance *instance;
943
944 instance = (struct megasas_instance *)scmd->device->host->hostdata;
945
Sumant Patro05e9ebb2007-05-17 05:47:51 -0700946 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
947 scmd->serial_number, scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400948
949 if (instance->hw_crit_error) {
950 printk(KERN_ERR "megasas: cannot recover from previous reset "
951 "failures\n");
952 return FAILED;
953 }
954
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400955 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400956 if (ret_val == SUCCESS)
957 printk(KERN_NOTICE "megasas: reset successful \n");
958 else
959 printk(KERN_ERR "megasas: failed to do reset\n");
960
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400961 return ret_val;
962}
963
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400964/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -0700965 * megasas_reset_timer - quiesce the adapter if required
966 * @scmd: scsi cmnd
967 *
968 * Sets the FW busy flag and reduces the host->can_queue if the
969 * cmd has not been completed within the timeout period.
970 */
971static enum
972scsi_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
973{
974 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
975 struct megasas_instance *instance;
976 unsigned long flags;
977
978 if (time_after(jiffies, scmd->jiffies_at_alloc +
979 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
980 return EH_NOT_HANDLED;
981 }
982
983 instance = cmd->instance;
984 if (!(instance->flag & MEGASAS_FW_BUSY)) {
985 /* FW is busy, throttle IO */
986 spin_lock_irqsave(instance->host->host_lock, flags);
987
988 instance->host->can_queue = 16;
989 instance->last_time = jiffies;
990 instance->flag |= MEGASAS_FW_BUSY;
991
992 spin_unlock_irqrestore(instance->host->host_lock, flags);
993 }
994 return EH_RESET_TIMER;
995}
996
997/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400998 * megasas_reset_device - Device reset handler entry point
999 */
1000static int megasas_reset_device(struct scsi_cmnd *scmd)
1001{
1002 int ret;
1003
1004 /*
1005 * First wait for all commands to complete
1006 */
1007 ret = megasas_generic_reset(scmd);
1008
1009 return ret;
1010}
1011
1012/**
1013 * megasas_reset_bus_host - Bus & host reset handler entry point
1014 */
1015static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1016{
1017 int ret;
1018
1019 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01001020 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001021 */
1022 ret = megasas_generic_reset(scmd);
1023
1024 return ret;
1025}
1026
1027/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001028 * megasas_bios_param - Returns disk geometry for a disk
1029 * @sdev: device handle
1030 * @bdev: block device
1031 * @capacity: drive capacity
1032 * @geom: geometry parameters
1033 */
1034static int
1035megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1036 sector_t capacity, int geom[])
1037{
1038 int heads;
1039 int sectors;
1040 sector_t cylinders;
1041 unsigned long tmp;
1042 /* Default heads (64) & sectors (32) */
1043 heads = 64;
1044 sectors = 32;
1045
1046 tmp = heads * sectors;
1047 cylinders = capacity;
1048
1049 sector_div(cylinders, tmp);
1050
1051 /*
1052 * Handle extended translation size for logical drives > 1Gb
1053 */
1054
1055 if (capacity >= 0x200000) {
1056 heads = 255;
1057 sectors = 63;
1058 tmp = heads*sectors;
1059 cylinders = capacity;
1060 sector_div(cylinders, tmp);
1061 }
1062
1063 geom[0] = heads;
1064 geom[1] = sectors;
1065 geom[2] = cylinders;
1066
1067 return 0;
1068}
1069
1070/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001071 * megasas_service_aen - Processes an event notification
1072 * @instance: Adapter soft state
1073 * @cmd: AEN command completed by the ISR
1074 *
1075 * For AEN, driver sends a command down to FW that is held by the FW till an
1076 * event occurs. When an event of interest occurs, FW completes the command
1077 * that it was previously holding.
1078 *
1079 * This routines sends SIGIO signal to processes that have registered with the
1080 * driver for AEN.
1081 */
1082static void
1083megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1084{
1085 /*
1086 * Don't signal app if it is just an aborted previously registered aen
1087 */
1088 if (!cmd->abort_aen)
1089 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
1090 else
1091 cmd->abort_aen = 0;
1092
1093 instance->aen_cmd = NULL;
1094 megasas_return_cmd(instance, cmd);
1095}
1096
1097/*
1098 * Scsi host template for megaraid_sas driver
1099 */
1100static struct scsi_host_template megasas_template = {
1101
1102 .module = THIS_MODULE,
1103 .name = "LSI Logic SAS based MegaRAID driver",
1104 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01001105 .slave_configure = megasas_slave_configure,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001106 .queuecommand = megasas_queue_command,
1107 .eh_device_reset_handler = megasas_reset_device,
1108 .eh_bus_reset_handler = megasas_reset_bus_host,
1109 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001110 .eh_timed_out = megasas_reset_timer,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001111 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001112 .use_clustering = ENABLE_CLUSTERING,
1113};
1114
1115/**
1116 * megasas_complete_int_cmd - Completes an internal command
1117 * @instance: Adapter soft state
1118 * @cmd: Command to be completed
1119 *
1120 * The megasas_issue_blocked_cmd() function waits for a command to complete
1121 * after it issues a command. This function wakes up that waiting routine by
1122 * calling wake_up() on the wait queue.
1123 */
1124static void
1125megasas_complete_int_cmd(struct megasas_instance *instance,
1126 struct megasas_cmd *cmd)
1127{
1128 cmd->cmd_status = cmd->frame->io.cmd_status;
1129
1130 if (cmd->cmd_status == ENODATA) {
1131 cmd->cmd_status = 0;
1132 }
1133 wake_up(&instance->int_cmd_wait_q);
1134}
1135
1136/**
1137 * megasas_complete_abort - Completes aborting a command
1138 * @instance: Adapter soft state
1139 * @cmd: Cmd that was issued to abort another cmd
1140 *
1141 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
1142 * after it issues an abort on a previously issued command. This function
1143 * wakes up all functions waiting on the same wait queue.
1144 */
1145static void
1146megasas_complete_abort(struct megasas_instance *instance,
1147 struct megasas_cmd *cmd)
1148{
1149 if (cmd->sync_cmd) {
1150 cmd->sync_cmd = 0;
1151 cmd->cmd_status = 0;
1152 wake_up(&instance->abort_cmd_wait_q);
1153 }
1154
1155 return;
1156}
1157
1158/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001159 * megasas_complete_cmd - Completes a command
1160 * @instance: Adapter soft state
1161 * @cmd: Command to be completed
1162 * @alt_status: If non-zero, use this value as status to
1163 * SCSI mid-layer instead of the value returned
1164 * by the FW. This should be used if caller wants
1165 * an alternate status (as in the case of aborted
1166 * commands)
1167 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001168static void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001169megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
1170 u8 alt_status)
1171{
1172 int exception = 0;
1173 struct megasas_header *hdr = &cmd->frame->hdr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001174
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001175 if (cmd->scmd)
1176 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001177
1178 switch (hdr->cmd) {
1179
1180 case MFI_CMD_PD_SCSI_IO:
1181 case MFI_CMD_LD_SCSI_IO:
1182
1183 /*
1184 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1185 * issued either through an IO path or an IOCTL path. If it
1186 * was via IOCTL, we will send it to internal completion.
1187 */
1188 if (cmd->sync_cmd) {
1189 cmd->sync_cmd = 0;
1190 megasas_complete_int_cmd(instance, cmd);
1191 break;
1192 }
1193
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001194 case MFI_CMD_LD_READ:
1195 case MFI_CMD_LD_WRITE:
1196
1197 if (alt_status) {
1198 cmd->scmd->result = alt_status << 16;
1199 exception = 1;
1200 }
1201
1202 if (exception) {
1203
Sumant Patroe4a082c2006-05-30 12:03:37 -07001204 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001205
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001206 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001207 cmd->scmd->scsi_done(cmd->scmd);
1208 megasas_return_cmd(instance, cmd);
1209
1210 break;
1211 }
1212
1213 switch (hdr->cmd_status) {
1214
1215 case MFI_STAT_OK:
1216 cmd->scmd->result = DID_OK << 16;
1217 break;
1218
1219 case MFI_STAT_SCSI_IO_FAILED:
1220 case MFI_STAT_LD_INIT_IN_PROGRESS:
1221 cmd->scmd->result =
1222 (DID_ERROR << 16) | hdr->scsi_status;
1223 break;
1224
1225 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1226
1227 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1228
1229 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1230 memset(cmd->scmd->sense_buffer, 0,
1231 SCSI_SENSE_BUFFERSIZE);
1232 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1233 hdr->sense_len);
1234
1235 cmd->scmd->result |= DRIVER_SENSE << 24;
1236 }
1237
1238 break;
1239
1240 case MFI_STAT_LD_OFFLINE:
1241 case MFI_STAT_DEVICE_NOT_FOUND:
1242 cmd->scmd->result = DID_BAD_TARGET << 16;
1243 break;
1244
1245 default:
1246 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1247 hdr->cmd_status);
1248 cmd->scmd->result = DID_ERROR << 16;
1249 break;
1250 }
1251
Sumant Patroe4a082c2006-05-30 12:03:37 -07001252 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001253
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001254 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001255 cmd->scmd->scsi_done(cmd->scmd);
1256 megasas_return_cmd(instance, cmd);
1257
1258 break;
1259
1260 case MFI_CMD_SMP:
1261 case MFI_CMD_STP:
1262 case MFI_CMD_DCMD:
1263
1264 /*
1265 * See if got an event notification
1266 */
1267 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1268 megasas_service_aen(instance, cmd);
1269 else
1270 megasas_complete_int_cmd(instance, cmd);
1271
1272 break;
1273
1274 case MFI_CMD_ABORT:
1275 /*
1276 * Cmd issued to abort another cmd returned
1277 */
1278 megasas_complete_abort(instance, cmd);
1279 break;
1280
1281 default:
1282 printk("megasas: Unknown command completed! [0x%X]\n",
1283 hdr->cmd);
1284 break;
1285 }
1286}
1287
1288/**
1289 * megasas_deplete_reply_queue - Processes all completed commands
1290 * @instance: Adapter soft state
1291 * @alt_status: Alternate status to be returned to
1292 * SCSI mid-layer instead of the status
1293 * returned by the FW
1294 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001295static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001296megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1297{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001298 /*
1299 * Check if it is our interrupt
Sumant Patro1341c932006-01-25 12:02:40 -08001300 * Clear the interrupt
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001301 */
Sumant Patro1341c932006-01-25 12:02:40 -08001302 if(instance->instancet->clear_intr(instance->reg_set))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001303 return IRQ_NONE;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001304
Sumant Patroaf37acf2007-02-14 12:34:46 -08001305 if (instance->hw_crit_error)
1306 goto out_done;
Sumant Patro5d018ad2006-10-03 13:13:18 -07001307 /*
1308 * Schedule the tasklet for cmd completion
1309 */
1310 tasklet_schedule(&instance->isr_tasklet);
Sumant Patroaf37acf2007-02-14 12:34:46 -08001311out_done:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001312 return IRQ_HANDLED;
1313}
1314
1315/**
1316 * megasas_isr - isr entry point
1317 */
David Howells7d12e782006-10-05 14:55:46 +01001318static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001319{
1320 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1321 DID_OK);
1322}
1323
1324/**
1325 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08001326 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001327 *
1328 * During the initialization, FW passes can potentially be in any one of
1329 * several possible states. If the FW in operational, waiting-for-handshake
1330 * states, driver must take steps to bring it to ready state. Otherwise, it
1331 * has to wait for the ready state.
1332 */
1333static int
Sumant Patro1341c932006-01-25 12:02:40 -08001334megasas_transition_to_ready(struct megasas_instance* instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001335{
1336 int i;
1337 u8 max_wait;
1338 u32 fw_state;
1339 u32 cur_state;
1340
Sumant Patro1341c932006-01-25 12:02:40 -08001341 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001342
Sumant Patroe3bbff92006-10-03 12:28:49 -07001343 if (fw_state != MFI_STATE_READY)
1344 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1345 " state\n");
1346
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001347 while (fw_state != MFI_STATE_READY) {
1348
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001349 switch (fw_state) {
1350
1351 case MFI_STATE_FAULT:
1352
1353 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1354 return -ENODEV;
1355
1356 case MFI_STATE_WAIT_HANDSHAKE:
1357 /*
1358 * Set the CLR bit in inbound doorbell
1359 */
Sumant Patroe3bbff92006-10-03 12:28:49 -07001360 writel(MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
Sumant Patro1341c932006-01-25 12:02:40 -08001361 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001362
1363 max_wait = 2;
1364 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1365 break;
1366
Sumant Patroe3bbff92006-10-03 12:28:49 -07001367 case MFI_STATE_BOOT_MESSAGE_PENDING:
1368 writel(MFI_INIT_HOTPLUG,
1369 &instance->reg_set->inbound_doorbell);
1370
1371 max_wait = 10;
1372 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
1373 break;
1374
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001375 case MFI_STATE_OPERATIONAL:
1376 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07001377 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001378 */
Sumant Patrob274cab2006-10-03 12:52:12 -07001379 instance->instancet->disable_intr(instance->reg_set);
Sumant Patroe3bbff92006-10-03 12:28:49 -07001380 writel(MFI_RESET_FLAGS, &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001381
1382 max_wait = 10;
1383 cur_state = MFI_STATE_OPERATIONAL;
1384 break;
1385
1386 case MFI_STATE_UNDEFINED:
1387 /*
1388 * This state should not last for more than 2 seconds
1389 */
1390 max_wait = 2;
1391 cur_state = MFI_STATE_UNDEFINED;
1392 break;
1393
1394 case MFI_STATE_BB_INIT:
1395 max_wait = 2;
1396 cur_state = MFI_STATE_BB_INIT;
1397 break;
1398
1399 case MFI_STATE_FW_INIT:
1400 max_wait = 20;
1401 cur_state = MFI_STATE_FW_INIT;
1402 break;
1403
1404 case MFI_STATE_FW_INIT_2:
1405 max_wait = 20;
1406 cur_state = MFI_STATE_FW_INIT_2;
1407 break;
1408
1409 case MFI_STATE_DEVICE_SCAN:
1410 max_wait = 20;
1411 cur_state = MFI_STATE_DEVICE_SCAN;
1412 break;
1413
1414 case MFI_STATE_FLUSH_CACHE:
1415 max_wait = 20;
1416 cur_state = MFI_STATE_FLUSH_CACHE;
1417 break;
1418
1419 default:
1420 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1421 fw_state);
1422 return -ENODEV;
1423 }
1424
1425 /*
1426 * The cur_state should not last for more than max_wait secs
1427 */
1428 for (i = 0; i < (max_wait * 1000); i++) {
Sumant Patro1341c932006-01-25 12:02:40 -08001429 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1430 MFI_STATE_MASK ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001431
1432 if (fw_state == cur_state) {
1433 msleep(1);
1434 } else
1435 break;
1436 }
1437
1438 /*
1439 * Return error if fw_state hasn't changed after max_wait
1440 */
1441 if (fw_state == cur_state) {
1442 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1443 "in %d secs\n", fw_state, max_wait);
1444 return -ENODEV;
1445 }
1446 };
Sumant Patroe3bbff92006-10-03 12:28:49 -07001447 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001448
1449 return 0;
1450}
1451
1452/**
1453 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1454 * @instance: Adapter soft state
1455 */
1456static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1457{
1458 int i;
1459 u32 max_cmd = instance->max_fw_cmds;
1460 struct megasas_cmd *cmd;
1461
1462 if (!instance->frame_dma_pool)
1463 return;
1464
1465 /*
1466 * Return all frames to pool
1467 */
1468 for (i = 0; i < max_cmd; i++) {
1469
1470 cmd = instance->cmd_list[i];
1471
1472 if (cmd->frame)
1473 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1474 cmd->frame_phys_addr);
1475
1476 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07001477 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001478 cmd->sense_phys_addr);
1479 }
1480
1481 /*
1482 * Now destroy the pool itself
1483 */
1484 pci_pool_destroy(instance->frame_dma_pool);
1485 pci_pool_destroy(instance->sense_dma_pool);
1486
1487 instance->frame_dma_pool = NULL;
1488 instance->sense_dma_pool = NULL;
1489}
1490
1491/**
1492 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1493 * @instance: Adapter soft state
1494 *
1495 * Each command packet has an embedded DMA memory buffer that is used for
1496 * filling MFI frame and the SG list that immediately follows the frame. This
1497 * function creates those DMA memory buffers for each command packet by using
1498 * PCI pool facility.
1499 */
1500static int megasas_create_frame_pool(struct megasas_instance *instance)
1501{
1502 int i;
1503 u32 max_cmd;
1504 u32 sge_sz;
1505 u32 sgl_sz;
1506 u32 total_sz;
1507 u32 frame_count;
1508 struct megasas_cmd *cmd;
1509
1510 max_cmd = instance->max_fw_cmds;
1511
1512 /*
1513 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1514 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1515 */
1516 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1517 sizeof(struct megasas_sge32);
1518
1519 /*
1520 * Calculated the number of 64byte frames required for SGL
1521 */
1522 sgl_sz = sge_sz * instance->max_num_sge;
1523 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1524
1525 /*
1526 * We need one extra frame for the MFI command
1527 */
1528 frame_count++;
1529
1530 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1531 /*
1532 * Use DMA pool facility provided by PCI layer
1533 */
1534 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1535 instance->pdev, total_sz, 64,
1536 0);
1537
1538 if (!instance->frame_dma_pool) {
1539 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1540 return -ENOMEM;
1541 }
1542
1543 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1544 instance->pdev, 128, 4, 0);
1545
1546 if (!instance->sense_dma_pool) {
1547 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1548
1549 pci_pool_destroy(instance->frame_dma_pool);
1550 instance->frame_dma_pool = NULL;
1551
1552 return -ENOMEM;
1553 }
1554
1555 /*
1556 * Allocate and attach a frame to each of the commands in cmd_list.
1557 * By making cmd->index as the context instead of the &cmd, we can
1558 * always use 32bit context regardless of the architecture
1559 */
1560 for (i = 0; i < max_cmd; i++) {
1561
1562 cmd = instance->cmd_list[i];
1563
1564 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1565 GFP_KERNEL, &cmd->frame_phys_addr);
1566
1567 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1568 GFP_KERNEL, &cmd->sense_phys_addr);
1569
1570 /*
1571 * megasas_teardown_frame_pool() takes care of freeing
1572 * whatever has been allocated
1573 */
1574 if (!cmd->frame || !cmd->sense) {
1575 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1576 megasas_teardown_frame_pool(instance);
1577 return -ENOMEM;
1578 }
1579
1580 cmd->frame->io.context = cmd->index;
1581 }
1582
1583 return 0;
1584}
1585
1586/**
1587 * megasas_free_cmds - Free all the cmds in the free cmd pool
1588 * @instance: Adapter soft state
1589 */
1590static void megasas_free_cmds(struct megasas_instance *instance)
1591{
1592 int i;
1593 /* First free the MFI frame pool */
1594 megasas_teardown_frame_pool(instance);
1595
1596 /* Free all the commands in the cmd_list */
1597 for (i = 0; i < instance->max_fw_cmds; i++)
1598 kfree(instance->cmd_list[i]);
1599
1600 /* Free the cmd_list buffer itself */
1601 kfree(instance->cmd_list);
1602 instance->cmd_list = NULL;
1603
1604 INIT_LIST_HEAD(&instance->cmd_pool);
1605}
1606
1607/**
1608 * megasas_alloc_cmds - Allocates the command packets
1609 * @instance: Adapter soft state
1610 *
1611 * Each command that is issued to the FW, whether IO commands from the OS or
1612 * internal commands like IOCTLs, are wrapped in local data structure called
1613 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1614 * the FW.
1615 *
1616 * Each frame has a 32-bit field called context (tag). This context is used
1617 * to get back the megasas_cmd from the frame when a frame gets completed in
1618 * the ISR. Typically the address of the megasas_cmd itself would be used as
1619 * the context. But we wanted to keep the differences between 32 and 64 bit
1620 * systems to the mininum. We always use 32 bit integers for the context. In
1621 * this driver, the 32 bit values are the indices into an array cmd_list.
1622 * This array is used only to look up the megasas_cmd given the context. The
1623 * free commands themselves are maintained in a linked list called cmd_pool.
1624 */
1625static int megasas_alloc_cmds(struct megasas_instance *instance)
1626{
1627 int i;
1628 int j;
1629 u32 max_cmd;
1630 struct megasas_cmd *cmd;
1631
1632 max_cmd = instance->max_fw_cmds;
1633
1634 /*
1635 * instance->cmd_list is an array of struct megasas_cmd pointers.
1636 * Allocate the dynamic array first and then allocate individual
1637 * commands.
1638 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001639 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001640
1641 if (!instance->cmd_list) {
1642 printk(KERN_DEBUG "megasas: out of memory\n");
1643 return -ENOMEM;
1644 }
1645
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001646
1647 for (i = 0; i < max_cmd; i++) {
1648 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
1649 GFP_KERNEL);
1650
1651 if (!instance->cmd_list[i]) {
1652
1653 for (j = 0; j < i; j++)
1654 kfree(instance->cmd_list[j]);
1655
1656 kfree(instance->cmd_list);
1657 instance->cmd_list = NULL;
1658
1659 return -ENOMEM;
1660 }
1661 }
1662
1663 /*
1664 * Add all the commands to command pool (instance->cmd_pool)
1665 */
1666 for (i = 0; i < max_cmd; i++) {
1667 cmd = instance->cmd_list[i];
1668 memset(cmd, 0, sizeof(struct megasas_cmd));
1669 cmd->index = i;
1670 cmd->instance = instance;
1671
1672 list_add_tail(&cmd->list, &instance->cmd_pool);
1673 }
1674
1675 /*
1676 * Create a frame pool and assign one frame to each cmd
1677 */
1678 if (megasas_create_frame_pool(instance)) {
1679 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
1680 megasas_free_cmds(instance);
1681 }
1682
1683 return 0;
1684}
1685
1686/**
1687 * megasas_get_controller_info - Returns FW's controller structure
1688 * @instance: Adapter soft state
1689 * @ctrl_info: Controller information structure
1690 *
1691 * Issues an internal command (DCMD) to get the FW's controller structure.
1692 * This information is mainly used to find out the maximum IO transfer per
1693 * command supported by the FW.
1694 */
1695static int
1696megasas_get_ctrl_info(struct megasas_instance *instance,
1697 struct megasas_ctrl_info *ctrl_info)
1698{
1699 int ret = 0;
1700 struct megasas_cmd *cmd;
1701 struct megasas_dcmd_frame *dcmd;
1702 struct megasas_ctrl_info *ci;
1703 dma_addr_t ci_h = 0;
1704
1705 cmd = megasas_get_cmd(instance);
1706
1707 if (!cmd) {
1708 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
1709 return -ENOMEM;
1710 }
1711
1712 dcmd = &cmd->frame->dcmd;
1713
1714 ci = pci_alloc_consistent(instance->pdev,
1715 sizeof(struct megasas_ctrl_info), &ci_h);
1716
1717 if (!ci) {
1718 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
1719 megasas_return_cmd(instance, cmd);
1720 return -ENOMEM;
1721 }
1722
1723 memset(ci, 0, sizeof(*ci));
1724 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1725
1726 dcmd->cmd = MFI_CMD_DCMD;
1727 dcmd->cmd_status = 0xFF;
1728 dcmd->sge_count = 1;
1729 dcmd->flags = MFI_FRAME_DIR_READ;
1730 dcmd->timeout = 0;
1731 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
1732 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
1733 dcmd->sgl.sge32[0].phys_addr = ci_h;
1734 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
1735
1736 if (!megasas_issue_polled(instance, cmd)) {
1737 ret = 0;
1738 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
1739 } else {
1740 ret = -1;
1741 }
1742
1743 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
1744 ci, ci_h);
1745
1746 megasas_return_cmd(instance, cmd);
1747 return ret;
1748}
1749
1750/**
Sumant Patro5d018ad2006-10-03 13:13:18 -07001751 * megasas_complete_cmd_dpc - Returns FW's controller structure
1752 * @instance_addr: Address of adapter soft state
1753 *
1754 * Tasklet to complete cmds
1755 */
Adrian Bunkb448de42006-11-20 03:23:49 +01001756static void megasas_complete_cmd_dpc(unsigned long instance_addr)
Sumant Patro5d018ad2006-10-03 13:13:18 -07001757{
1758 u32 producer;
1759 u32 consumer;
1760 u32 context;
1761 struct megasas_cmd *cmd;
1762 struct megasas_instance *instance = (struct megasas_instance *)instance_addr;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001763 unsigned long flags;
Sumant Patro5d018ad2006-10-03 13:13:18 -07001764
Sumant Patroaf37acf2007-02-14 12:34:46 -08001765 /* If we have already declared adapter dead, donot complete cmds */
1766 if (instance->hw_crit_error)
1767 return;
1768
Sumant Patro5d018ad2006-10-03 13:13:18 -07001769 producer = *instance->producer;
1770 consumer = *instance->consumer;
1771
1772 while (consumer != producer) {
1773 context = instance->reply_queue[consumer];
1774
1775 cmd = instance->cmd_list[context];
1776
1777 megasas_complete_cmd(instance, cmd, DID_OK);
1778
1779 consumer++;
1780 if (consumer == (instance->max_fw_cmds + 1)) {
1781 consumer = 0;
1782 }
1783 }
1784
1785 *instance->consumer = producer;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001786
1787 /*
1788 * Check if we can restore can_queue
1789 */
1790 if (instance->flag & MEGASAS_FW_BUSY
1791 && time_after(jiffies, instance->last_time + 5 * HZ)
1792 && atomic_read(&instance->fw_outstanding) < 17) {
1793
1794 spin_lock_irqsave(instance->host->host_lock, flags);
1795 instance->flag &= ~MEGASAS_FW_BUSY;
1796 instance->host->can_queue =
1797 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1798
1799 spin_unlock_irqrestore(instance->host->host_lock, flags);
1800 }
1801
Sumant Patro5d018ad2006-10-03 13:13:18 -07001802}
1803
1804/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001805 * megasas_init_mfi - Initializes the FW
1806 * @instance: Adapter soft state
1807 *
1808 * This is the main function for initializing MFI firmware.
1809 */
1810static int megasas_init_mfi(struct megasas_instance *instance)
1811{
1812 u32 context_sz;
1813 u32 reply_q_sz;
1814 u32 max_sectors_1;
1815 u32 max_sectors_2;
1816 struct megasas_register_set __iomem *reg_set;
1817
1818 struct megasas_cmd *cmd;
1819 struct megasas_ctrl_info *ctrl_info;
1820
1821 struct megasas_init_frame *init_frame;
1822 struct megasas_init_queue_info *initq_info;
1823 dma_addr_t init_frame_h;
1824 dma_addr_t initq_info_h;
1825
1826 /*
1827 * Map the message registers
1828 */
1829 instance->base_addr = pci_resource_start(instance->pdev, 0);
1830
1831 if (pci_request_regions(instance->pdev, "megasas: LSI Logic")) {
1832 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
1833 return -EBUSY;
1834 }
1835
1836 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
1837
1838 if (!instance->reg_set) {
1839 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
1840 goto fail_ioremap;
1841 }
1842
1843 reg_set = instance->reg_set;
1844
Sumant Patrof9876f02006-02-03 15:34:35 -08001845 switch(instance->pdev->device)
1846 {
1847 case PCI_DEVICE_ID_LSI_SAS1078R:
1848 instance->instancet = &megasas_instance_template_ppc;
1849 break;
1850 case PCI_DEVICE_ID_LSI_SAS1064R:
1851 case PCI_DEVICE_ID_DELL_PERC5:
1852 default:
1853 instance->instancet = &megasas_instance_template_xscale;
1854 break;
1855 }
Sumant Patro1341c932006-01-25 12:02:40 -08001856
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001857 /*
1858 * We expect the FW state to be READY
1859 */
Sumant Patro1341c932006-01-25 12:02:40 -08001860 if (megasas_transition_to_ready(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001861 goto fail_ready_state;
1862
1863 /*
1864 * Get various operational parameters from status register
1865 */
Sumant Patro1341c932006-01-25 12:02:40 -08001866 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07001867 /*
1868 * Reduce the max supported cmds by 1. This is to ensure that the
1869 * reply_q_sz (1 more than the max cmd that driver may send)
1870 * does not exceed max cmds that the FW can support
1871 */
1872 instance->max_fw_cmds = instance->max_fw_cmds-1;
Sumant Patro1341c932006-01-25 12:02:40 -08001873 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
1874 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001875 /*
1876 * Create a pool of commands
1877 */
1878 if (megasas_alloc_cmds(instance))
1879 goto fail_alloc_cmds;
1880
1881 /*
1882 * Allocate memory for reply queue. Length of reply queue should
1883 * be _one_ more than the maximum commands handled by the firmware.
1884 *
1885 * Note: When FW completes commands, it places corresponding contex
1886 * values in this circular reply queue. This circular queue is a fairly
1887 * typical producer-consumer queue. FW is the producer (of completed
1888 * commands) and the driver is the consumer.
1889 */
1890 context_sz = sizeof(u32);
1891 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
1892
1893 instance->reply_queue = pci_alloc_consistent(instance->pdev,
1894 reply_q_sz,
1895 &instance->reply_queue_h);
1896
1897 if (!instance->reply_queue) {
1898 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
1899 goto fail_reply_queue;
1900 }
1901
1902 /*
1903 * Prepare a init frame. Note the init frame points to queue info
1904 * structure. Each frame has SGL allocated after first 64 bytes. For
1905 * this frame - since we don't need any SGL - we use SGL's space as
1906 * queue info structure
1907 *
1908 * We will not get a NULL command below. We just created the pool.
1909 */
1910 cmd = megasas_get_cmd(instance);
1911
1912 init_frame = (struct megasas_init_frame *)cmd->frame;
1913 initq_info = (struct megasas_init_queue_info *)
1914 ((unsigned long)init_frame + 64);
1915
1916 init_frame_h = cmd->frame_phys_addr;
1917 initq_info_h = init_frame_h + 64;
1918
1919 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
1920 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
1921
1922 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
1923 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
1924
1925 initq_info->producer_index_phys_addr_lo = instance->producer_h;
1926 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
1927
1928 init_frame->cmd = MFI_CMD_INIT;
1929 init_frame->cmd_status = 0xFF;
1930 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
1931
1932 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
1933
1934 /*
Sumant Patro0e989362006-06-20 15:32:37 -07001935 * disable the intr before firing the init frame to FW
1936 */
Sumant Patrob274cab2006-10-03 12:52:12 -07001937 instance->instancet->disable_intr(instance->reg_set);
Sumant Patro0e989362006-06-20 15:32:37 -07001938
1939 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001940 * Issue the init frame in polled mode
1941 */
1942 if (megasas_issue_polled(instance, cmd)) {
1943 printk(KERN_DEBUG "megasas: Failed to init firmware\n");
1944 goto fail_fw_init;
1945 }
1946
1947 megasas_return_cmd(instance, cmd);
1948
1949 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
1950
1951 /*
1952 * Compute the max allowed sectors per IO: The controller info has two
1953 * limits on max sectors. Driver should use the minimum of these two.
1954 *
1955 * 1 << stripe_sz_ops.min = max sectors per strip
1956 *
1957 * Note that older firmwares ( < FW ver 30) didn't report information
1958 * to calculate max_sectors_1. So the number ended up as zero always.
1959 */
1960 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
1961
1962 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
1963 ctrl_info->max_strips_per_io;
1964 max_sectors_2 = ctrl_info->max_request_size;
1965
1966 instance->max_sectors_per_req = (max_sectors_1 < max_sectors_2)
1967 ? max_sectors_1 : max_sectors_2;
1968 } else
1969 instance->max_sectors_per_req = instance->max_num_sge *
1970 PAGE_SIZE / 512;
1971
1972 kfree(ctrl_info);
1973
Sumant Patro5d018ad2006-10-03 13:13:18 -07001974 /*
1975 * Setup tasklet for cmd completion
1976 */
1977
1978 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
1979 (unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001980 return 0;
1981
1982 fail_fw_init:
1983 megasas_return_cmd(instance, cmd);
1984
1985 pci_free_consistent(instance->pdev, reply_q_sz,
1986 instance->reply_queue, instance->reply_queue_h);
1987 fail_reply_queue:
1988 megasas_free_cmds(instance);
1989
1990 fail_alloc_cmds:
1991 fail_ready_state:
1992 iounmap(instance->reg_set);
1993
1994 fail_ioremap:
1995 pci_release_regions(instance->pdev);
1996
1997 return -EINVAL;
1998}
1999
2000/**
2001 * megasas_release_mfi - Reverses the FW initialization
2002 * @intance: Adapter soft state
2003 */
2004static void megasas_release_mfi(struct megasas_instance *instance)
2005{
2006 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
2007
2008 pci_free_consistent(instance->pdev, reply_q_sz,
2009 instance->reply_queue, instance->reply_queue_h);
2010
2011 megasas_free_cmds(instance);
2012
2013 iounmap(instance->reg_set);
2014
2015 pci_release_regions(instance->pdev);
2016}
2017
2018/**
2019 * megasas_get_seq_num - Gets latest event sequence numbers
2020 * @instance: Adapter soft state
2021 * @eli: FW event log sequence numbers information
2022 *
2023 * FW maintains a log of all events in a non-volatile area. Upper layers would
2024 * usually find out the latest sequence number of the events, the seq number at
2025 * the boot etc. They would "read" all the events below the latest seq number
2026 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
2027 * number), they would subsribe to AEN (asynchronous event notification) and
2028 * wait for the events to happen.
2029 */
2030static int
2031megasas_get_seq_num(struct megasas_instance *instance,
2032 struct megasas_evt_log_info *eli)
2033{
2034 struct megasas_cmd *cmd;
2035 struct megasas_dcmd_frame *dcmd;
2036 struct megasas_evt_log_info *el_info;
2037 dma_addr_t el_info_h = 0;
2038
2039 cmd = megasas_get_cmd(instance);
2040
2041 if (!cmd) {
2042 return -ENOMEM;
2043 }
2044
2045 dcmd = &cmd->frame->dcmd;
2046 el_info = pci_alloc_consistent(instance->pdev,
2047 sizeof(struct megasas_evt_log_info),
2048 &el_info_h);
2049
2050 if (!el_info) {
2051 megasas_return_cmd(instance, cmd);
2052 return -ENOMEM;
2053 }
2054
2055 memset(el_info, 0, sizeof(*el_info));
2056 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2057
2058 dcmd->cmd = MFI_CMD_DCMD;
2059 dcmd->cmd_status = 0x0;
2060 dcmd->sge_count = 1;
2061 dcmd->flags = MFI_FRAME_DIR_READ;
2062 dcmd->timeout = 0;
2063 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
2064 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
2065 dcmd->sgl.sge32[0].phys_addr = el_info_h;
2066 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
2067
2068 megasas_issue_blocked_cmd(instance, cmd);
2069
2070 /*
2071 * Copy the data back into callers buffer
2072 */
2073 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
2074
2075 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
2076 el_info, el_info_h);
2077
2078 megasas_return_cmd(instance, cmd);
2079
2080 return 0;
2081}
2082
2083/**
2084 * megasas_register_aen - Registers for asynchronous event notification
2085 * @instance: Adapter soft state
2086 * @seq_num: The starting sequence number
2087 * @class_locale: Class of the event
2088 *
2089 * This function subscribes for AEN for events beyond the @seq_num. It requests
2090 * to be notified if and only if the event is of type @class_locale
2091 */
2092static int
2093megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
2094 u32 class_locale_word)
2095{
2096 int ret_val;
2097 struct megasas_cmd *cmd;
2098 struct megasas_dcmd_frame *dcmd;
2099 union megasas_evt_class_locale curr_aen;
2100 union megasas_evt_class_locale prev_aen;
2101
2102 /*
2103 * If there an AEN pending already (aen_cmd), check if the
2104 * class_locale of that pending AEN is inclusive of the new
2105 * AEN request we currently have. If it is, then we don't have
2106 * to do anything. In other words, whichever events the current
2107 * AEN request is subscribing to, have already been subscribed
2108 * to.
2109 *
2110 * If the old_cmd is _not_ inclusive, then we have to abort
2111 * that command, form a class_locale that is superset of both
2112 * old and current and re-issue to the FW
2113 */
2114
2115 curr_aen.word = class_locale_word;
2116
2117 if (instance->aen_cmd) {
2118
2119 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
2120
2121 /*
2122 * A class whose enum value is smaller is inclusive of all
2123 * higher values. If a PROGRESS (= -1) was previously
2124 * registered, then a new registration requests for higher
2125 * classes need not be sent to FW. They are automatically
2126 * included.
2127 *
2128 * Locale numbers don't have such hierarchy. They are bitmap
2129 * values
2130 */
2131 if ((prev_aen.members.class <= curr_aen.members.class) &&
2132 !((prev_aen.members.locale & curr_aen.members.locale) ^
2133 curr_aen.members.locale)) {
2134 /*
2135 * Previously issued event registration includes
2136 * current request. Nothing to do.
2137 */
2138 return 0;
2139 } else {
2140 curr_aen.members.locale |= prev_aen.members.locale;
2141
2142 if (prev_aen.members.class < curr_aen.members.class)
2143 curr_aen.members.class = prev_aen.members.class;
2144
2145 instance->aen_cmd->abort_aen = 1;
2146 ret_val = megasas_issue_blocked_abort_cmd(instance,
2147 instance->
2148 aen_cmd);
2149
2150 if (ret_val) {
2151 printk(KERN_DEBUG "megasas: Failed to abort "
2152 "previous AEN command\n");
2153 return ret_val;
2154 }
2155 }
2156 }
2157
2158 cmd = megasas_get_cmd(instance);
2159
2160 if (!cmd)
2161 return -ENOMEM;
2162
2163 dcmd = &cmd->frame->dcmd;
2164
2165 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
2166
2167 /*
2168 * Prepare DCMD for aen registration
2169 */
2170 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2171
2172 dcmd->cmd = MFI_CMD_DCMD;
2173 dcmd->cmd_status = 0x0;
2174 dcmd->sge_count = 1;
2175 dcmd->flags = MFI_FRAME_DIR_READ;
2176 dcmd->timeout = 0;
2177 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
2178 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
2179 dcmd->mbox.w[0] = seq_num;
2180 dcmd->mbox.w[1] = curr_aen.word;
2181 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
2182 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
2183
2184 /*
2185 * Store reference to the cmd used to register for AEN. When an
2186 * application wants us to register for AEN, we have to abort this
2187 * cmd and re-register with a new EVENT LOCALE supplied by that app
2188 */
2189 instance->aen_cmd = cmd;
2190
2191 /*
2192 * Issue the aen registration frame
2193 */
Sumant Patro1341c932006-01-25 12:02:40 -08002194 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002195
2196 return 0;
2197}
2198
2199/**
2200 * megasas_start_aen - Subscribes to AEN during driver load time
2201 * @instance: Adapter soft state
2202 */
2203static int megasas_start_aen(struct megasas_instance *instance)
2204{
2205 struct megasas_evt_log_info eli;
2206 union megasas_evt_class_locale class_locale;
2207
2208 /*
2209 * Get the latest sequence number from FW
2210 */
2211 memset(&eli, 0, sizeof(eli));
2212
2213 if (megasas_get_seq_num(instance, &eli))
2214 return -1;
2215
2216 /*
2217 * Register AEN with FW for latest sequence number plus 1
2218 */
2219 class_locale.members.reserved = 0;
2220 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2221 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2222
2223 return megasas_register_aen(instance, eli.newest_seq_num + 1,
2224 class_locale.word);
2225}
2226
2227/**
2228 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2229 * @instance: Adapter soft state
2230 */
2231static int megasas_io_attach(struct megasas_instance *instance)
2232{
2233 struct Scsi_Host *host = instance->host;
2234
2235 /*
2236 * Export parameters required by SCSI mid-layer
2237 */
2238 host->irq = instance->pdev->irq;
2239 host->unique_id = instance->unique_id;
2240 host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
2241 host->this_id = instance->init_id;
2242 host->sg_tablesize = instance->max_num_sge;
2243 host->max_sectors = instance->max_sectors_per_req;
2244 host->cmd_per_lun = 128;
2245 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
2246 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
2247 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08002248 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002249
2250 /*
2251 * Notify the mid-layer about the new controller
2252 */
2253 if (scsi_add_host(host, &instance->pdev->dev)) {
2254 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2255 return -ENODEV;
2256 }
2257
2258 /*
2259 * Trigger SCSI to scan our drives
2260 */
2261 scsi_scan_host(host);
2262 return 0;
2263}
2264
2265/**
2266 * megasas_probe_one - PCI hotplug entry point
2267 * @pdev: PCI device structure
2268 * @id: PCI ids of supported hotplugged adapter
2269 */
2270static int __devinit
2271megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2272{
2273 int rval;
2274 struct Scsi_Host *host;
2275 struct megasas_instance *instance;
2276
2277 /*
2278 * Announce PCI information
2279 */
2280 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2281 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2282 pdev->subsystem_device);
2283
2284 printk("bus %d:slot %d:func %d\n",
2285 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2286
2287 /*
2288 * PCI prepping: enable device set bus mastering and dma mask
2289 */
2290 rval = pci_enable_device(pdev);
2291
2292 if (rval) {
2293 return rval;
2294 }
2295
2296 pci_set_master(pdev);
2297
2298 /*
2299 * All our contollers are capable of performing 64-bit DMA
2300 */
2301 if (IS_DMA64) {
2302 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK) != 0) {
2303
2304 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2305 goto fail_set_dma_mask;
2306 }
2307 } else {
2308 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2309 goto fail_set_dma_mask;
2310 }
2311
2312 host = scsi_host_alloc(&megasas_template,
2313 sizeof(struct megasas_instance));
2314
2315 if (!host) {
2316 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2317 goto fail_alloc_instance;
2318 }
2319
2320 instance = (struct megasas_instance *)host->hostdata;
2321 memset(instance, 0, sizeof(*instance));
2322
2323 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2324 &instance->producer_h);
2325 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2326 &instance->consumer_h);
2327
2328 if (!instance->producer || !instance->consumer) {
2329 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2330 "producer, consumer\n");
2331 goto fail_alloc_dma_buf;
2332 }
2333
2334 *instance->producer = 0;
2335 *instance->consumer = 0;
2336
2337 instance->evt_detail = pci_alloc_consistent(pdev,
2338 sizeof(struct
2339 megasas_evt_detail),
2340 &instance->evt_detail_h);
2341
2342 if (!instance->evt_detail) {
2343 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2344 "event detail structure\n");
2345 goto fail_alloc_dma_buf;
2346 }
2347
2348 /*
2349 * Initialize locks and queues
2350 */
2351 INIT_LIST_HEAD(&instance->cmd_pool);
2352
Sumant Patroe4a082c2006-05-30 12:03:37 -07002353 atomic_set(&instance->fw_outstanding,0);
2354
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002355 init_waitqueue_head(&instance->int_cmd_wait_q);
2356 init_waitqueue_head(&instance->abort_cmd_wait_q);
2357
2358 spin_lock_init(&instance->cmd_pool_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002359
2360 sema_init(&instance->aen_mutex, 1);
2361 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2362
2363 /*
2364 * Initialize PCI related and misc parameters
2365 */
2366 instance->pdev = pdev;
2367 instance->host = host;
2368 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2369 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2370
Sumant Patro658dced2006-10-03 13:09:14 -07002371 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002372 instance->flag = 0;
2373 instance->last_time = 0;
Sumant Patro658dced2006-10-03 13:09:14 -07002374
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002375 /*
2376 * Initialize MFI Firmware
2377 */
2378 if (megasas_init_mfi(instance))
2379 goto fail_init_mfi;
2380
2381 /*
2382 * Register IRQ
2383 */
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07002384 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002385 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2386 goto fail_irq;
2387 }
2388
Sumant Patro1341c932006-01-25 12:02:40 -08002389 instance->instancet->enable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002390
2391 /*
2392 * Store instance in PCI softstate
2393 */
2394 pci_set_drvdata(pdev, instance);
2395
2396 /*
2397 * Add this controller to megasas_mgmt_info structure so that it
2398 * can be exported to management applications
2399 */
2400 megasas_mgmt_info.count++;
2401 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2402 megasas_mgmt_info.max_index++;
2403
2404 /*
2405 * Initiate AEN (Asynchronous Event Notification)
2406 */
2407 if (megasas_start_aen(instance)) {
2408 printk(KERN_DEBUG "megasas: start aen failed\n");
2409 goto fail_start_aen;
2410 }
2411
2412 /*
2413 * Register with SCSI mid-layer
2414 */
2415 if (megasas_io_attach(instance))
2416 goto fail_io_attach;
2417
2418 return 0;
2419
2420 fail_start_aen:
2421 fail_io_attach:
2422 megasas_mgmt_info.count--;
2423 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2424 megasas_mgmt_info.max_index--;
2425
2426 pci_set_drvdata(pdev, NULL);
Sumant Patrob274cab2006-10-03 12:52:12 -07002427 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002428 free_irq(instance->pdev->irq, instance);
2429
2430 megasas_release_mfi(instance);
2431
2432 fail_irq:
2433 fail_init_mfi:
2434 fail_alloc_dma_buf:
2435 if (instance->evt_detail)
2436 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2437 instance->evt_detail,
2438 instance->evt_detail_h);
2439
2440 if (instance->producer)
2441 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2442 instance->producer_h);
2443 if (instance->consumer)
2444 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2445 instance->consumer_h);
2446 scsi_host_put(host);
2447
2448 fail_alloc_instance:
2449 fail_set_dma_mask:
2450 pci_disable_device(pdev);
2451
2452 return -ENODEV;
2453}
2454
2455/**
2456 * megasas_flush_cache - Requests FW to flush all its caches
2457 * @instance: Adapter soft state
2458 */
2459static void megasas_flush_cache(struct megasas_instance *instance)
2460{
2461 struct megasas_cmd *cmd;
2462 struct megasas_dcmd_frame *dcmd;
2463
2464 cmd = megasas_get_cmd(instance);
2465
2466 if (!cmd)
2467 return;
2468
2469 dcmd = &cmd->frame->dcmd;
2470
2471 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2472
2473 dcmd->cmd = MFI_CMD_DCMD;
2474 dcmd->cmd_status = 0x0;
2475 dcmd->sge_count = 0;
2476 dcmd->flags = MFI_FRAME_DIR_NONE;
2477 dcmd->timeout = 0;
2478 dcmd->data_xfer_len = 0;
2479 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
2480 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
2481
2482 megasas_issue_blocked_cmd(instance, cmd);
2483
2484 megasas_return_cmd(instance, cmd);
2485
2486 return;
2487}
2488
2489/**
2490 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2491 * @instance: Adapter soft state
2492 */
2493static void megasas_shutdown_controller(struct megasas_instance *instance)
2494{
2495 struct megasas_cmd *cmd;
2496 struct megasas_dcmd_frame *dcmd;
2497
2498 cmd = megasas_get_cmd(instance);
2499
2500 if (!cmd)
2501 return;
2502
2503 if (instance->aen_cmd)
2504 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
2505
2506 dcmd = &cmd->frame->dcmd;
2507
2508 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2509
2510 dcmd->cmd = MFI_CMD_DCMD;
2511 dcmd->cmd_status = 0x0;
2512 dcmd->sge_count = 0;
2513 dcmd->flags = MFI_FRAME_DIR_NONE;
2514 dcmd->timeout = 0;
2515 dcmd->data_xfer_len = 0;
2516 dcmd->opcode = MR_DCMD_CTRL_SHUTDOWN;
2517
2518 megasas_issue_blocked_cmd(instance, cmd);
2519
2520 megasas_return_cmd(instance, cmd);
2521
2522 return;
2523}
2524
2525/**
2526 * megasas_detach_one - PCI hot"un"plug entry point
2527 * @pdev: PCI device structure
2528 */
2529static void megasas_detach_one(struct pci_dev *pdev)
2530{
2531 int i;
2532 struct Scsi_Host *host;
2533 struct megasas_instance *instance;
2534
2535 instance = pci_get_drvdata(pdev);
2536 host = instance->host;
2537
2538 scsi_remove_host(instance->host);
2539 megasas_flush_cache(instance);
2540 megasas_shutdown_controller(instance);
Sumant Patro5d018ad2006-10-03 13:13:18 -07002541 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002542
2543 /*
2544 * Take the instance off the instance array. Note that we will not
2545 * decrement the max_index. We let this array be sparse array
2546 */
2547 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2548 if (megasas_mgmt_info.instance[i] == instance) {
2549 megasas_mgmt_info.count--;
2550 megasas_mgmt_info.instance[i] = NULL;
2551
2552 break;
2553 }
2554 }
2555
2556 pci_set_drvdata(instance->pdev, NULL);
2557
Sumant Patrob274cab2006-10-03 12:52:12 -07002558 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002559
2560 free_irq(instance->pdev->irq, instance);
2561
2562 megasas_release_mfi(instance);
2563
2564 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2565 instance->evt_detail, instance->evt_detail_h);
2566
2567 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2568 instance->producer_h);
2569
2570 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2571 instance->consumer_h);
2572
2573 scsi_host_put(host);
2574
2575 pci_set_drvdata(pdev, NULL);
2576
2577 pci_disable_device(pdev);
2578
2579 return;
2580}
2581
2582/**
2583 * megasas_shutdown - Shutdown entry point
2584 * @device: Generic device structure
2585 */
2586static void megasas_shutdown(struct pci_dev *pdev)
2587{
2588 struct megasas_instance *instance = pci_get_drvdata(pdev);
2589 megasas_flush_cache(instance);
2590}
2591
2592/**
2593 * megasas_mgmt_open - char node "open" entry point
2594 */
2595static int megasas_mgmt_open(struct inode *inode, struct file *filep)
2596{
2597 /*
2598 * Allow only those users with admin rights
2599 */
2600 if (!capable(CAP_SYS_ADMIN))
2601 return -EACCES;
2602
2603 return 0;
2604}
2605
2606/**
2607 * megasas_mgmt_release - char node "release" entry point
2608 */
2609static int megasas_mgmt_release(struct inode *inode, struct file *filep)
2610{
2611 filep->private_data = NULL;
2612 fasync_helper(-1, filep, 0, &megasas_async_queue);
2613
2614 return 0;
2615}
2616
2617/**
2618 * megasas_mgmt_fasync - Async notifier registration from applications
2619 *
2620 * This function adds the calling process to a driver global queue. When an
2621 * event occurs, SIGIO will be sent to all processes in this queue.
2622 */
2623static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
2624{
2625 int rc;
2626
Arjan van de Ven0b950672006-01-11 13:16:10 +01002627 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002628
2629 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
2630
Arjan van de Ven0b950672006-01-11 13:16:10 +01002631 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002632
2633 if (rc >= 0) {
2634 /* For sanity check when we get ioctl */
2635 filep->private_data = filep;
2636 return 0;
2637 }
2638
2639 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
2640
2641 return rc;
2642}
2643
2644/**
2645 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
2646 * @instance: Adapter soft state
2647 * @argp: User's ioctl packet
2648 */
2649static int
2650megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
2651 struct megasas_iocpacket __user * user_ioc,
2652 struct megasas_iocpacket *ioc)
2653{
2654 struct megasas_sge32 *kern_sge32;
2655 struct megasas_cmd *cmd;
2656 void *kbuff_arr[MAX_IOCTL_SGE];
2657 dma_addr_t buf_handle = 0;
2658 int error = 0, i;
2659 void *sense = NULL;
2660 dma_addr_t sense_handle;
2661 u32 *sense_ptr;
2662
2663 memset(kbuff_arr, 0, sizeof(kbuff_arr));
2664
2665 if (ioc->sge_count > MAX_IOCTL_SGE) {
2666 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
2667 ioc->sge_count, MAX_IOCTL_SGE);
2668 return -EINVAL;
2669 }
2670
2671 cmd = megasas_get_cmd(instance);
2672 if (!cmd) {
2673 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
2674 return -ENOMEM;
2675 }
2676
2677 /*
2678 * User's IOCTL packet has 2 frames (maximum). Copy those two
2679 * frames into our cmd's frames. cmd->frame's context will get
2680 * overwritten when we copy from user's frames. So set that value
2681 * alone separately
2682 */
2683 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
2684 cmd->frame->hdr.context = cmd->index;
2685
2686 /*
2687 * The management interface between applications and the fw uses
2688 * MFI frames. E.g, RAID configuration changes, LD property changes
2689 * etc are accomplishes through different kinds of MFI frames. The
2690 * driver needs to care only about substituting user buffers with
2691 * kernel buffers in SGLs. The location of SGL is embedded in the
2692 * struct iocpacket itself.
2693 */
2694 kern_sge32 = (struct megasas_sge32 *)
2695 ((unsigned long)cmd->frame + ioc->sgl_off);
2696
2697 /*
2698 * For each user buffer, create a mirror buffer and copy in
2699 */
2700 for (i = 0; i < ioc->sge_count; i++) {
Sumant Patro9f35fa8a2007-02-14 12:55:45 -08002701 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002702 ioc->sgl[i].iov_len,
Sumant Patro9f35fa8a2007-02-14 12:55:45 -08002703 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002704 if (!kbuff_arr[i]) {
2705 printk(KERN_DEBUG "megasas: Failed to alloc "
2706 "kernel SGL buffer for IOCTL \n");
2707 error = -ENOMEM;
2708 goto out;
2709 }
2710
2711 /*
2712 * We don't change the dma_coherent_mask, so
2713 * pci_alloc_consistent only returns 32bit addresses
2714 */
2715 kern_sge32[i].phys_addr = (u32) buf_handle;
2716 kern_sge32[i].length = ioc->sgl[i].iov_len;
2717
2718 /*
2719 * We created a kernel buffer corresponding to the
2720 * user buffer. Now copy in from the user buffer
2721 */
2722 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
2723 (u32) (ioc->sgl[i].iov_len))) {
2724 error = -EFAULT;
2725 goto out;
2726 }
2727 }
2728
2729 if (ioc->sense_len) {
Sumant Patro9f35fa8a2007-02-14 12:55:45 -08002730 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
2731 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002732 if (!sense) {
2733 error = -ENOMEM;
2734 goto out;
2735 }
2736
2737 sense_ptr =
2738 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
2739 *sense_ptr = sense_handle;
2740 }
2741
2742 /*
2743 * Set the sync_cmd flag so that the ISR knows not to complete this
2744 * cmd to the SCSI mid-layer
2745 */
2746 cmd->sync_cmd = 1;
2747 megasas_issue_blocked_cmd(instance, cmd);
2748 cmd->sync_cmd = 0;
2749
2750 /*
2751 * copy out the kernel buffers to user buffers
2752 */
2753 for (i = 0; i < ioc->sge_count; i++) {
2754 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
2755 ioc->sgl[i].iov_len)) {
2756 error = -EFAULT;
2757 goto out;
2758 }
2759 }
2760
2761 /*
2762 * copy out the sense
2763 */
2764 if (ioc->sense_len) {
2765 /*
2766 * sense_ptr points to the location that has the user
2767 * sense buffer address
2768 */
2769 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
2770 ioc->sense_off);
2771
2772 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
2773 sense, ioc->sense_len)) {
2774 error = -EFAULT;
2775 goto out;
2776 }
2777 }
2778
2779 /*
2780 * copy the status codes returned by the fw
2781 */
2782 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
2783 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
2784 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
2785 error = -EFAULT;
2786 }
2787
2788 out:
2789 if (sense) {
Sumant Patro9f35fa8a2007-02-14 12:55:45 -08002790 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002791 sense, sense_handle);
2792 }
2793
2794 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
Sumant Patro9f35fa8a2007-02-14 12:55:45 -08002795 dma_free_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002796 kern_sge32[i].length,
2797 kbuff_arr[i], kern_sge32[i].phys_addr);
2798 }
2799
2800 megasas_return_cmd(instance, cmd);
2801 return error;
2802}
2803
2804static struct megasas_instance *megasas_lookup_instance(u16 host_no)
2805{
2806 int i;
2807
2808 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2809
2810 if ((megasas_mgmt_info.instance[i]) &&
2811 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
2812 return megasas_mgmt_info.instance[i];
2813 }
2814
2815 return NULL;
2816}
2817
2818static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
2819{
2820 struct megasas_iocpacket __user *user_ioc =
2821 (struct megasas_iocpacket __user *)arg;
2822 struct megasas_iocpacket *ioc;
2823 struct megasas_instance *instance;
2824 int error;
2825
2826 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
2827 if (!ioc)
2828 return -ENOMEM;
2829
2830 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
2831 error = -EFAULT;
2832 goto out_kfree_ioc;
2833 }
2834
2835 instance = megasas_lookup_instance(ioc->host_no);
2836 if (!instance) {
2837 error = -ENODEV;
2838 goto out_kfree_ioc;
2839 }
2840
2841 /*
2842 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
2843 */
2844 if (down_interruptible(&instance->ioctl_sem)) {
2845 error = -ERESTARTSYS;
2846 goto out_kfree_ioc;
2847 }
2848 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
2849 up(&instance->ioctl_sem);
2850
2851 out_kfree_ioc:
2852 kfree(ioc);
2853 return error;
2854}
2855
2856static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
2857{
2858 struct megasas_instance *instance;
2859 struct megasas_aen aen;
2860 int error;
2861
2862 if (file->private_data != file) {
2863 printk(KERN_DEBUG "megasas: fasync_helper was not "
2864 "called first\n");
2865 return -EINVAL;
2866 }
2867
2868 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
2869 return -EFAULT;
2870
2871 instance = megasas_lookup_instance(aen.host_no);
2872
2873 if (!instance)
2874 return -ENODEV;
2875
2876 down(&instance->aen_mutex);
2877 error = megasas_register_aen(instance, aen.seq_num,
2878 aen.class_locale_word);
2879 up(&instance->aen_mutex);
2880 return error;
2881}
2882
2883/**
2884 * megasas_mgmt_ioctl - char node ioctl entry point
2885 */
2886static long
2887megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2888{
2889 switch (cmd) {
2890 case MEGASAS_IOC_FIRMWARE:
2891 return megasas_mgmt_ioctl_fw(file, arg);
2892
2893 case MEGASAS_IOC_GET_AEN:
2894 return megasas_mgmt_ioctl_aen(file, arg);
2895 }
2896
2897 return -ENOTTY;
2898}
2899
2900#ifdef CONFIG_COMPAT
2901static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
2902{
2903 struct compat_megasas_iocpacket __user *cioc =
2904 (struct compat_megasas_iocpacket __user *)arg;
2905 struct megasas_iocpacket __user *ioc =
2906 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
2907 int i;
2908 int error = 0;
2909
Jeff Garzik83aabc12006-10-04 06:34:03 -04002910 if (clear_user(ioc, sizeof(*ioc)))
2911 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002912
2913 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
2914 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
2915 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
2916 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
2917 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
2918 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
2919 return -EFAULT;
2920
2921 for (i = 0; i < MAX_IOCTL_SGE; i++) {
2922 compat_uptr_t ptr;
2923
2924 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
2925 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
2926 copy_in_user(&ioc->sgl[i].iov_len,
2927 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
2928 return -EFAULT;
2929 }
2930
2931 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
2932
2933 if (copy_in_user(&cioc->frame.hdr.cmd_status,
2934 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
2935 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
2936 return -EFAULT;
2937 }
2938 return error;
2939}
2940
2941static long
2942megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
2943 unsigned long arg)
2944{
2945 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08002946 case MEGASAS_IOC_FIRMWARE32:
2947 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002948 case MEGASAS_IOC_GET_AEN:
2949 return megasas_mgmt_ioctl_aen(file, arg);
2950 }
2951
2952 return -ENOTTY;
2953}
2954#endif
2955
2956/*
2957 * File operations structure for management interface
2958 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002959static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002960 .owner = THIS_MODULE,
2961 .open = megasas_mgmt_open,
2962 .release = megasas_mgmt_release,
2963 .fasync = megasas_mgmt_fasync,
2964 .unlocked_ioctl = megasas_mgmt_ioctl,
2965#ifdef CONFIG_COMPAT
2966 .compat_ioctl = megasas_mgmt_compat_ioctl,
2967#endif
2968};
2969
2970/*
2971 * PCI hotplug support registration structure
2972 */
2973static struct pci_driver megasas_pci_driver = {
2974
2975 .name = "megaraid_sas",
2976 .id_table = megasas_pci_table,
2977 .probe = megasas_probe_one,
2978 .remove = __devexit_p(megasas_detach_one),
2979 .shutdown = megasas_shutdown,
2980};
2981
2982/*
2983 * Sysfs driver attributes
2984 */
2985static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
2986{
2987 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
2988 MEGASAS_VERSION);
2989}
2990
2991static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
2992
2993static ssize_t
2994megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
2995{
2996 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
2997 MEGASAS_RELDATE);
2998}
2999
3000static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
3001 NULL);
3002
Sumant Patro658dced2006-10-03 13:09:14 -07003003static ssize_t
3004megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
3005{
3006 return sprintf(buf,"%u",megasas_dbg_lvl);
3007}
3008
3009static ssize_t
3010megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
3011{
3012 int retval = count;
3013 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
3014 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
3015 retval = -EINVAL;
3016 }
3017 return retval;
3018}
3019
3020static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUGO, megasas_sysfs_show_dbg_lvl,
3021 megasas_sysfs_set_dbg_lvl);
3022
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003023/**
3024 * megasas_init - Driver load entry point
3025 */
3026static int __init megasas_init(void)
3027{
3028 int rval;
3029
3030 /*
3031 * Announce driver version and other information
3032 */
3033 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
3034 MEGASAS_EXT_VERSION);
3035
3036 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
3037
3038 /*
3039 * Register character device node
3040 */
3041 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
3042
3043 if (rval < 0) {
3044 printk(KERN_DEBUG "megasas: failed to open device node\n");
3045 return rval;
3046 }
3047
3048 megasas_mgmt_majorno = rval;
3049
3050 /*
3051 * Register ourselves as PCI hotplug module
3052 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00003053 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003054
3055 if (rval) {
3056 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04003057 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003058 }
3059
Jeff Garzik83aabc12006-10-04 06:34:03 -04003060 rval = driver_create_file(&megasas_pci_driver.driver,
3061 &driver_attr_version);
3062 if (rval)
3063 goto err_dcf_attr_ver;
3064 rval = driver_create_file(&megasas_pci_driver.driver,
3065 &driver_attr_release_date);
3066 if (rval)
3067 goto err_dcf_rel_date;
3068 rval = driver_create_file(&megasas_pci_driver.driver,
3069 &driver_attr_dbg_lvl);
3070 if (rval)
3071 goto err_dcf_dbg_lvl;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003072
3073 return rval;
Jeff Garzik83aabc12006-10-04 06:34:03 -04003074err_dcf_dbg_lvl:
3075 driver_remove_file(&megasas_pci_driver.driver,
3076 &driver_attr_release_date);
3077err_dcf_rel_date:
3078 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3079err_dcf_attr_ver:
3080 pci_unregister_driver(&megasas_pci_driver);
3081err_pcidrv:
3082 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3083 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003084}
3085
3086/**
3087 * megasas_exit - Driver unload entry point
3088 */
3089static void __exit megasas_exit(void)
3090{
Sumant Patro658dced2006-10-03 13:09:14 -07003091 driver_remove_file(&megasas_pci_driver.driver,
3092 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04003093 driver_remove_file(&megasas_pci_driver.driver,
3094 &driver_attr_release_date);
3095 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003096
3097 pci_unregister_driver(&megasas_pci_driver);
3098 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3099}
3100
3101module_init(megasas_init);
3102module_exit(megasas_exit);