blob: 01eb9b5870b5e0ff08f33bc02a6359a3c85a60d9 [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 Patro2a3681e2006-10-03 13:19:21 -070013 * Version : v00.00.03.05
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040014 *
15 * Authors:
Sumant Patro3d6d1742006-12-29 08:13:54 -080016 * Sreenivas Bagalkote <Sreenivas.Bagalkote@lsi.com>
17 * Sumant Patro <Sumant.Patro@lsi.com>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040018 *
19 * List of supported controllers
20 *
21 * OEM Product Name VID DID SSVID SSID
22 * --- ------------ --- --- ---- ----
23 */
24
25#include <linux/kernel.h>
26#include <linux/types.h>
27#include <linux/pci.h>
28#include <linux/list.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040029#include <linux/moduleparam.h>
30#include <linux/module.h>
31#include <linux/spinlock.h>
32#include <linux/interrupt.h>
33#include <linux/delay.h>
34#include <linux/uio.h>
35#include <asm/uaccess.h>
Al Viro43399232005-10-04 17:36:04 +010036#include <linux/fs.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040037#include <linux/compat.h>
Sumant Patrocf62a0a2007-02-14 12:41:55 -080038#include <linux/blkdev.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010039#include <linux/mutex.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040040
41#include <scsi/scsi.h>
42#include <scsi/scsi_cmnd.h>
43#include <scsi/scsi_device.h>
44#include <scsi/scsi_host.h>
45#include "megaraid_sas.h"
46
47MODULE_LICENSE("GPL");
48MODULE_VERSION(MEGASAS_VERSION);
Sumant Patro3d6d1742006-12-29 08:13:54 -080049MODULE_AUTHOR("megaraidlinux@lsi.com");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040050MODULE_DESCRIPTION("LSI Logic MegaRAID SAS Driver");
51
52/*
53 * PCI ID table for all supported controllers
54 */
55static struct pci_device_id megasas_pci_table[] = {
56
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +020057 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
58 /* xscale IOP */
59 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
60 /* ppc IOP */
61 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
62 /* xscale IOP, vega */
63 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
64 /* xscale IOP */
65 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040066};
67
68MODULE_DEVICE_TABLE(pci, megasas_pci_table);
69
70static int megasas_mgmt_majorno;
71static struct megasas_mgmt_info megasas_mgmt_info;
72static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +010073static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040074
Sumant Patro658dced2006-10-03 13:09:14 -070075static u32 megasas_dbg_lvl;
76
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040077/**
78 * megasas_get_cmd - Get a command from the free pool
79 * @instance: Adapter soft state
80 *
81 * Returns a free command from the pool
82 */
Arjan van de Ven858119e2006-01-14 13:20:43 -080083static struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040084 *instance)
85{
86 unsigned long flags;
87 struct megasas_cmd *cmd = NULL;
88
89 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
90
91 if (!list_empty(&instance->cmd_pool)) {
92 cmd = list_entry((&instance->cmd_pool)->next,
93 struct megasas_cmd, list);
94 list_del_init(&cmd->list);
95 } else {
96 printk(KERN_ERR "megasas: Command pool empty!\n");
97 }
98
99 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
100 return cmd;
101}
102
103/**
104 * megasas_return_cmd - Return a cmd to free command pool
105 * @instance: Adapter soft state
106 * @cmd: Command packet to be returned to free command pool
107 */
108static inline void
109megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
110{
111 unsigned long flags;
112
113 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
114
115 cmd->scmd = NULL;
116 list_add_tail(&cmd->list, &instance->cmd_pool);
117
118 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
119}
120
Sumant Patro1341c932006-01-25 12:02:40 -0800121
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400122/**
Sumant Patro1341c932006-01-25 12:02:40 -0800123* The following functions are defined for xscale
124* (deviceid : 1064R, PERC5) controllers
125*/
126
127/**
128 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400129 * @regs: MFI register set
130 */
131static inline void
Sumant Patro1341c932006-01-25 12:02:40 -0800132megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400133{
134 writel(1, &(regs)->outbound_intr_mask);
135
136 /* Dummy readl to force pci flush */
137 readl(&regs->outbound_intr_mask);
138}
139
140/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700141 * megasas_disable_intr_xscale -Disables interrupt
142 * @regs: MFI register set
143 */
144static inline void
145megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
146{
147 u32 mask = 0x1f;
148 writel(mask, &regs->outbound_intr_mask);
149 /* Dummy readl to force pci flush */
150 readl(&regs->outbound_intr_mask);
151}
152
153/**
Sumant Patro1341c932006-01-25 12:02:40 -0800154 * megasas_read_fw_status_reg_xscale - returns the current FW status value
155 * @regs: MFI register set
156 */
157static u32
158megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
159{
160 return readl(&(regs)->outbound_msg_0);
161}
162/**
163 * megasas_clear_interrupt_xscale - Check & clear interrupt
164 * @regs: MFI register set
165 */
166static int
167megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
168{
169 u32 status;
170 /*
171 * Check if it is our interrupt
172 */
173 status = readl(&regs->outbound_intr_status);
174
175 if (!(status & MFI_OB_INTR_STATUS_MASK)) {
176 return 1;
177 }
178
179 /*
180 * Clear the interrupt by writing back the same value
181 */
182 writel(status, &regs->outbound_intr_status);
183
184 return 0;
185}
186
187/**
188 * megasas_fire_cmd_xscale - Sends command to the FW
189 * @frame_phys_addr : Physical address of cmd
190 * @frame_count : Number of frames for the command
191 * @regs : MFI register set
192 */
193static inline void
194megasas_fire_cmd_xscale(dma_addr_t frame_phys_addr,u32 frame_count, struct megasas_register_set __iomem *regs)
195{
196 writel((frame_phys_addr >> 3)|(frame_count),
197 &(regs)->inbound_queue_port);
198}
199
200static struct megasas_instance_template megasas_instance_template_xscale = {
201
202 .fire_cmd = megasas_fire_cmd_xscale,
203 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700204 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800205 .clear_intr = megasas_clear_intr_xscale,
206 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
207};
208
209/**
210* This is the end of set of functions & definitions specific
211* to xscale (deviceid : 1064R, PERC5) controllers
212*/
213
214/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800215* The following functions are defined for ppc (deviceid : 0x60)
216* controllers
217*/
218
219/**
220 * megasas_enable_intr_ppc - Enables interrupts
221 * @regs: MFI register set
222 */
223static inline void
224megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
225{
226 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
227
228 writel(~0x80000004, &(regs)->outbound_intr_mask);
229
230 /* Dummy readl to force pci flush */
231 readl(&regs->outbound_intr_mask);
232}
233
234/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700235 * megasas_disable_intr_ppc - Disable interrupt
236 * @regs: MFI register set
237 */
238static inline void
239megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
240{
241 u32 mask = 0xFFFFFFFF;
242 writel(mask, &regs->outbound_intr_mask);
243 /* Dummy readl to force pci flush */
244 readl(&regs->outbound_intr_mask);
245}
246
247/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800248 * megasas_read_fw_status_reg_ppc - returns the current FW status value
249 * @regs: MFI register set
250 */
251static u32
252megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
253{
254 return readl(&(regs)->outbound_scratch_pad);
255}
256
257/**
258 * megasas_clear_interrupt_ppc - Check & clear interrupt
259 * @regs: MFI register set
260 */
261static int
262megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
263{
264 u32 status;
265 /*
266 * Check if it is our interrupt
267 */
268 status = readl(&regs->outbound_intr_status);
269
270 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
271 return 1;
272 }
273
274 /*
275 * Clear the interrupt by writing back the same value
276 */
277 writel(status, &regs->outbound_doorbell_clear);
278
279 return 0;
280}
281/**
282 * megasas_fire_cmd_ppc - Sends command to the FW
283 * @frame_phys_addr : Physical address of cmd
284 * @frame_count : Number of frames for the command
285 * @regs : MFI register set
286 */
287static inline void
288megasas_fire_cmd_ppc(dma_addr_t frame_phys_addr, u32 frame_count, struct megasas_register_set __iomem *regs)
289{
290 writel((frame_phys_addr | (frame_count<<1))|1,
291 &(regs)->inbound_queue_port);
292}
293
294static struct megasas_instance_template megasas_instance_template_ppc = {
295
296 .fire_cmd = megasas_fire_cmd_ppc,
297 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700298 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800299 .clear_intr = megasas_clear_intr_ppc,
300 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
301};
302
303/**
304* This is the end of set of functions & definitions
305* specific to ppc (deviceid : 0x60) controllers
306*/
307
308/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400309 * megasas_issue_polled - Issues a polling command
310 * @instance: Adapter soft state
311 * @cmd: Command packet to be issued
312 *
313 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
314 */
315static int
316megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
317{
318 int i;
319 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
320
321 struct megasas_header *frame_hdr = &cmd->frame->hdr;
322
323 frame_hdr->cmd_status = 0xFF;
324 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
325
326 /*
327 * Issue the frame using inbound queue port
328 */
Sumant Patro1341c932006-01-25 12:02:40 -0800329 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400330
331 /*
332 * Wait for cmd_status to change
333 */
334 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
335 rmb();
336 msleep(1);
337 }
338
339 if (frame_hdr->cmd_status == 0xff)
340 return -ETIME;
341
342 return 0;
343}
344
345/**
346 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
347 * @instance: Adapter soft state
348 * @cmd: Command to be issued
349 *
350 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -0700351 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400352 * Used to issue ioctl commands.
353 */
354static int
355megasas_issue_blocked_cmd(struct megasas_instance *instance,
356 struct megasas_cmd *cmd)
357{
358 cmd->cmd_status = ENODATA;
359
Sumant Patro1341c932006-01-25 12:02:40 -0800360 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400361
Sumant Patro2a3681e2006-10-03 13:19:21 -0700362 wait_event_timeout(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA),
363 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400364
365 return 0;
366}
367
368/**
369 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
370 * @instance: Adapter soft state
371 * @cmd_to_abort: Previously issued cmd to be aborted
372 *
373 * MFI firmware can abort previously issued AEN comamnd (automatic event
374 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -0700375 * cmd and waits for return status.
376 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400377 */
378static int
379megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
380 struct megasas_cmd *cmd_to_abort)
381{
382 struct megasas_cmd *cmd;
383 struct megasas_abort_frame *abort_fr;
384
385 cmd = megasas_get_cmd(instance);
386
387 if (!cmd)
388 return -1;
389
390 abort_fr = &cmd->frame->abort;
391
392 /*
393 * Prepare and issue the abort frame
394 */
395 abort_fr->cmd = MFI_CMD_ABORT;
396 abort_fr->cmd_status = 0xFF;
397 abort_fr->flags = 0;
398 abort_fr->abort_context = cmd_to_abort->index;
399 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
400 abort_fr->abort_mfi_phys_addr_hi = 0;
401
402 cmd->sync_cmd = 1;
403 cmd->cmd_status = 0xFF;
404
Sumant Patro1341c932006-01-25 12:02:40 -0800405 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400406
407 /*
408 * Wait for this cmd to complete
409 */
Sumant Patro2a3681e2006-10-03 13:19:21 -0700410 wait_event_timeout(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF),
411 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400412
413 megasas_return_cmd(instance, cmd);
414 return 0;
415}
416
417/**
418 * megasas_make_sgl32 - Prepares 32-bit SGL
419 * @instance: Adapter soft state
420 * @scp: SCSI command from the mid-layer
421 * @mfi_sgl: SGL to be filled in
422 *
423 * If successful, this function returns the number of SG elements. Otherwise,
424 * it returnes -1.
425 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800426static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400427megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
428 union megasas_sgl *mfi_sgl)
429{
430 int i;
431 int sge_count;
432 struct scatterlist *os_sgl;
433
434 /*
435 * Return 0 if there is no data transfer
436 */
437 if (!scp->request_buffer || !scp->request_bufflen)
438 return 0;
439
440 if (!scp->use_sg) {
441 mfi_sgl->sge32[0].phys_addr = pci_map_single(instance->pdev,
442 scp->
443 request_buffer,
444 scp->
445 request_bufflen,
446 scp->
447 sc_data_direction);
448 mfi_sgl->sge32[0].length = scp->request_bufflen;
449
450 return 1;
451 }
452
453 os_sgl = (struct scatterlist *)scp->request_buffer;
454 sge_count = pci_map_sg(instance->pdev, os_sgl, scp->use_sg,
455 scp->sc_data_direction);
456
457 for (i = 0; i < sge_count; i++, os_sgl++) {
458 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
459 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
460 }
461
462 return sge_count;
463}
464
465/**
466 * megasas_make_sgl64 - Prepares 64-bit SGL
467 * @instance: Adapter soft state
468 * @scp: SCSI command from the mid-layer
469 * @mfi_sgl: SGL to be filled in
470 *
471 * If successful, this function returns the number of SG elements. Otherwise,
472 * it returnes -1.
473 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800474static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400475megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
476 union megasas_sgl *mfi_sgl)
477{
478 int i;
479 int sge_count;
480 struct scatterlist *os_sgl;
481
482 /*
483 * Return 0 if there is no data transfer
484 */
485 if (!scp->request_buffer || !scp->request_bufflen)
486 return 0;
487
488 if (!scp->use_sg) {
489 mfi_sgl->sge64[0].phys_addr = pci_map_single(instance->pdev,
490 scp->
491 request_buffer,
492 scp->
493 request_bufflen,
494 scp->
495 sc_data_direction);
496
497 mfi_sgl->sge64[0].length = scp->request_bufflen;
498
499 return 1;
500 }
501
502 os_sgl = (struct scatterlist *)scp->request_buffer;
503 sge_count = pci_map_sg(instance->pdev, os_sgl, scp->use_sg,
504 scp->sc_data_direction);
505
506 for (i = 0; i < sge_count; i++, os_sgl++) {
507 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
508 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
509 }
510
511 return sge_count;
512}
513
Sumant Patrob1df99d2006-10-03 12:40:47 -0700514 /**
515 * megasas_get_frame_count - Computes the number of frames
516 * @sge_count : number of sg elements
517 *
518 * Returns the number of frames required for numnber of sge's (sge_count)
519 */
520
Adrian Bunkb448de42006-11-20 03:23:49 +0100521static u32 megasas_get_frame_count(u8 sge_count)
Sumant Patrob1df99d2006-10-03 12:40:47 -0700522{
523 int num_cnt;
524 int sge_bytes;
525 u32 sge_sz;
526 u32 frame_count=0;
527
528 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
529 sizeof(struct megasas_sge32);
530
531 /*
532 * Main frame can contain 2 SGEs for 64-bit SGLs and
533 * 3 SGEs for 32-bit SGLs
534 */
535 if (IS_DMA64)
536 num_cnt = sge_count - 2;
537 else
538 num_cnt = sge_count - 3;
539
540 if(num_cnt>0){
541 sge_bytes = sge_sz * num_cnt;
542
543 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
544 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
545 }
546 /* Main frame */
547 frame_count +=1;
548
549 if (frame_count > 7)
550 frame_count = 8;
551 return frame_count;
552}
553
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400554/**
555 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
556 * @instance: Adapter soft state
557 * @scp: SCSI command
558 * @cmd: Command to be prepared in
559 *
560 * This function prepares CDB commands. These are typcially pass-through
561 * commands to the devices.
562 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800563static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400564megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
565 struct megasas_cmd *cmd)
566{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400567 u32 is_logical;
568 u32 device_id;
569 u16 flags = 0;
570 struct megasas_pthru_frame *pthru;
571
572 is_logical = MEGASAS_IS_LOGICAL(scp);
573 device_id = MEGASAS_DEV_INDEX(instance, scp);
574 pthru = (struct megasas_pthru_frame *)cmd->frame;
575
576 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
577 flags = MFI_FRAME_DIR_WRITE;
578 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
579 flags = MFI_FRAME_DIR_READ;
580 else if (scp->sc_data_direction == PCI_DMA_NONE)
581 flags = MFI_FRAME_DIR_NONE;
582
583 /*
584 * Prepare the DCDB frame
585 */
586 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
587 pthru->cmd_status = 0x0;
588 pthru->scsi_status = 0x0;
589 pthru->target_id = device_id;
590 pthru->lun = scp->device->lun;
591 pthru->cdb_len = scp->cmd_len;
592 pthru->timeout = 0;
593 pthru->flags = flags;
594 pthru->data_xfer_len = scp->request_bufflen;
595
596 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
597
598 /*
599 * Construct SGL
600 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400601 if (IS_DMA64) {
602 pthru->flags |= MFI_FRAME_SGL64;
603 pthru->sge_count = megasas_make_sgl64(instance, scp,
604 &pthru->sgl);
605 } else
606 pthru->sge_count = megasas_make_sgl32(instance, scp,
607 &pthru->sgl);
608
609 /*
610 * Sense info specific
611 */
612 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
613 pthru->sense_buf_phys_addr_hi = 0;
614 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
615
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400616 /*
617 * Compute the total number of frames this command consumes. FW uses
618 * this number to pull sufficient number of frames from host memory.
619 */
Sumant Patrob1df99d2006-10-03 12:40:47 -0700620 cmd->frame_count = megasas_get_frame_count(pthru->sge_count);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400621
622 return cmd->frame_count;
623}
624
625/**
626 * megasas_build_ldio - Prepares IOs to logical devices
627 * @instance: Adapter soft state
628 * @scp: SCSI command
629 * @cmd: Command to to be prepared
630 *
631 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
632 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800633static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400634megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
635 struct megasas_cmd *cmd)
636{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400637 u32 device_id;
638 u8 sc = scp->cmnd[0];
639 u16 flags = 0;
640 struct megasas_io_frame *ldio;
641
642 device_id = MEGASAS_DEV_INDEX(instance, scp);
643 ldio = (struct megasas_io_frame *)cmd->frame;
644
645 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
646 flags = MFI_FRAME_DIR_WRITE;
647 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
648 flags = MFI_FRAME_DIR_READ;
649
650 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -0700651 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400652 */
653 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
654 ldio->cmd_status = 0x0;
655 ldio->scsi_status = 0x0;
656 ldio->target_id = device_id;
657 ldio->timeout = 0;
658 ldio->reserved_0 = 0;
659 ldio->pad_0 = 0;
660 ldio->flags = flags;
661 ldio->start_lba_hi = 0;
662 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
663
664 /*
665 * 6-byte READ(0x08) or WRITE(0x0A) cdb
666 */
667 if (scp->cmd_len == 6) {
668 ldio->lba_count = (u32) scp->cmnd[4];
669 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
670 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
671
672 ldio->start_lba_lo &= 0x1FFFFF;
673 }
674
675 /*
676 * 10-byte READ(0x28) or WRITE(0x2A) cdb
677 */
678 else if (scp->cmd_len == 10) {
679 ldio->lba_count = (u32) scp->cmnd[8] |
680 ((u32) scp->cmnd[7] << 8);
681 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
682 ((u32) scp->cmnd[3] << 16) |
683 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
684 }
685
686 /*
687 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
688 */
689 else if (scp->cmd_len == 12) {
690 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
691 ((u32) scp->cmnd[7] << 16) |
692 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
693
694 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
695 ((u32) scp->cmnd[3] << 16) |
696 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
697 }
698
699 /*
700 * 16-byte READ(0x88) or WRITE(0x8A) cdb
701 */
702 else if (scp->cmd_len == 16) {
703 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
704 ((u32) scp->cmnd[11] << 16) |
705 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
706
707 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
708 ((u32) scp->cmnd[7] << 16) |
709 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
710
711 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
712 ((u32) scp->cmnd[3] << 16) |
713 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
714
715 }
716
717 /*
718 * Construct SGL
719 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400720 if (IS_DMA64) {
721 ldio->flags |= MFI_FRAME_SGL64;
722 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
723 } else
724 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
725
726 /*
727 * Sense info specific
728 */
729 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
730 ldio->sense_buf_phys_addr_hi = 0;
731 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
732
Sumant Patrob1df99d2006-10-03 12:40:47 -0700733 /*
734 * Compute the total number of frames this command consumes. FW uses
735 * this number to pull sufficient number of frames from host memory.
736 */
737 cmd->frame_count = megasas_get_frame_count(ldio->sge_count);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400738
739 return cmd->frame_count;
740}
741
742/**
Sumant Patrocb59aa62006-01-25 11:53:25 -0800743 * megasas_is_ldio - Checks if the cmd is for logical drive
744 * @scmd: SCSI command
745 *
746 * Called by megasas_queue_command to find out if the command to be queued
747 * is a logical drive command
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400748 */
Sumant Patrocb59aa62006-01-25 11:53:25 -0800749static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400750{
Sumant Patrocb59aa62006-01-25 11:53:25 -0800751 if (!MEGASAS_IS_LOGICAL(cmd))
752 return 0;
753 switch (cmd->cmnd[0]) {
754 case READ_10:
755 case WRITE_10:
756 case READ_12:
757 case WRITE_12:
758 case READ_6:
759 case WRITE_6:
760 case READ_16:
761 case WRITE_16:
762 return 1;
763 default:
764 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400765 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400766}
767
Sumant Patro658dced2006-10-03 13:09:14 -0700768 /**
769 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
770 * in FW
771 * @instance: Adapter soft state
772 */
773static inline void
774megasas_dump_pending_frames(struct megasas_instance *instance)
775{
776 struct megasas_cmd *cmd;
777 int i,n;
778 union megasas_sgl *mfi_sgl;
779 struct megasas_io_frame *ldio;
780 struct megasas_pthru_frame *pthru;
781 u32 sgcount;
782 u32 max_cmd = instance->max_fw_cmds;
783
784 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
785 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
786 if (IS_DMA64)
787 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
788 else
789 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
790
791 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
792 for (i = 0; i < max_cmd; i++) {
793 cmd = instance->cmd_list[i];
794 if(!cmd->scmd)
795 continue;
796 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
797 if (megasas_is_ldio(cmd->scmd)){
798 ldio = (struct megasas_io_frame *)cmd->frame;
799 mfi_sgl = &ldio->sgl;
800 sgcount = ldio->sge_count;
801 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);
802 }
803 else {
804 pthru = (struct megasas_pthru_frame *) cmd->frame;
805 mfi_sgl = &pthru->sgl;
806 sgcount = pthru->sge_count;
807 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);
808 }
809 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
810 for (n = 0; n < sgcount; n++){
811 if (IS_DMA64)
812 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) ;
813 else
814 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
815 }
816 }
817 printk(KERN_ERR "\n");
818 } /*for max_cmd*/
819 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
820 for (i = 0; i < max_cmd; i++) {
821
822 cmd = instance->cmd_list[i];
823
824 if(cmd->sync_cmd == 1){
825 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
826 }
827 }
828 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
829}
830
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400831/**
832 * megasas_queue_command - Queue entry point
833 * @scmd: SCSI command to be queued
834 * @done: Callback entry point
835 */
836static int
837megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
838{
839 u32 frame_count;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400840 struct megasas_cmd *cmd;
841 struct megasas_instance *instance;
842
843 instance = (struct megasas_instance *)
844 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -0800845
846 /* Don't process if we have already declared adapter dead */
847 if (instance->hw_crit_error)
848 return SCSI_MLQUEUE_HOST_BUSY;
849
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400850 scmd->scsi_done = done;
851 scmd->result = 0;
852
Sumant Patrocb59aa62006-01-25 11:53:25 -0800853 if (MEGASAS_IS_LOGICAL(scmd) &&
854 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
855 scmd->result = DID_BAD_TARGET << 16;
856 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400857 }
858
Sumant Patro02b01e02007-02-14 13:00:55 -0800859 switch (scmd->cmnd[0]) {
860 case SYNCHRONIZE_CACHE:
861 /*
862 * FW takes care of flush cache on its own
863 * No need to send it down
864 */
865 scmd->result = DID_OK << 16;
866 goto out_done;
867 default:
868 break;
869 }
870
Sumant Patrocb59aa62006-01-25 11:53:25 -0800871 cmd = megasas_get_cmd(instance);
872 if (!cmd)
873 return SCSI_MLQUEUE_HOST_BUSY;
874
875 /*
876 * Logical drive command
877 */
878 if (megasas_is_ldio(scmd))
879 frame_count = megasas_build_ldio(instance, scmd, cmd);
880 else
881 frame_count = megasas_build_dcdb(instance, scmd, cmd);
882
883 if (!frame_count)
884 goto out_return_cmd;
885
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400886 cmd->scmd = scmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400887
888 /*
889 * Issue the command to the FW
890 */
Sumant Patroe4a082c2006-05-30 12:03:37 -0700891 atomic_inc(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400892
Sumant Patro1341c932006-01-25 12:02:40 -0800893 instance->instancet->fire_cmd(cmd->frame_phys_addr ,cmd->frame_count-1,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400894
895 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -0800896
897 out_return_cmd:
898 megasas_return_cmd(instance, cmd);
899 out_done:
900 done(scmd);
901 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400902}
903
Christoph Hellwig147aab62006-02-17 12:13:48 +0100904static int megasas_slave_configure(struct scsi_device *sdev)
905{
906 /*
907 * Don't export physical disk devices to the disk driver.
908 *
909 * FIXME: Currently we don't export them to the midlayer at all.
910 * That will be fixed once LSI engineers have audited the
911 * firmware for possible issues.
912 */
913 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS && sdev->type == TYPE_DISK)
914 return -ENXIO;
Christoph Hellwige5b3a652006-03-10 17:08:57 +0100915
916 /*
917 * The RAID firmware may require extended timeouts.
918 */
919 if (sdev->channel >= MEGASAS_MAX_PD_CHANNELS)
920 sdev->timeout = 90 * HZ;
Christoph Hellwig147aab62006-02-17 12:13:48 +0100921 return 0;
922}
923
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400924/**
925 * megasas_wait_for_outstanding - Wait for all outstanding cmds
926 * @instance: Adapter soft state
927 *
928 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
929 * complete all its outstanding commands. Returns error if one or more IOs
930 * are pending after this time period. It also marks the controller dead.
931 */
932static int megasas_wait_for_outstanding(struct megasas_instance *instance)
933{
934 int i;
935 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
936
937 for (i = 0; i < wait_time; i++) {
938
Sumant Patroe4a082c2006-05-30 12:03:37 -0700939 int outstanding = atomic_read(&instance->fw_outstanding);
940
941 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400942 break;
943
944 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
945 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -0700946 "commands to complete\n",i,outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400947 }
948
949 msleep(1000);
950 }
951
Sumant Patroe4a082c2006-05-30 12:03:37 -0700952 if (atomic_read(&instance->fw_outstanding)) {
Sumant Patroe3bbff92006-10-03 12:28:49 -0700953 /*
954 * Send signal to FW to stop processing any pending cmds.
955 * The controller will be taken offline by the OS now.
956 */
957 writel(MFI_STOP_ADP,
958 &instance->reg_set->inbound_doorbell);
Sumant Patro658dced2006-10-03 13:09:14 -0700959 megasas_dump_pending_frames(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400960 instance->hw_crit_error = 1;
961 return FAILED;
962 }
963
964 return SUCCESS;
965}
966
967/**
968 * megasas_generic_reset - Generic reset routine
969 * @scmd: Mid-layer SCSI command
970 *
971 * This routine implements a generic reset handler for device, bus and host
972 * reset requests. Device, bus and host specific reset handlers can use this
973 * function after they do their specific tasks.
974 */
975static int megasas_generic_reset(struct scsi_cmnd *scmd)
976{
977 int ret_val;
978 struct megasas_instance *instance;
979
980 instance = (struct megasas_instance *)scmd->device->host->hostdata;
981
Jeff Garzik017560f2005-10-24 18:04:36 -0400982 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x\n",
983 scmd->serial_number, scmd->cmnd[0]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400984
985 if (instance->hw_crit_error) {
986 printk(KERN_ERR "megasas: cannot recover from previous reset "
987 "failures\n");
988 return FAILED;
989 }
990
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400991 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400992 if (ret_val == SUCCESS)
993 printk(KERN_NOTICE "megasas: reset successful \n");
994 else
995 printk(KERN_ERR "megasas: failed to do reset\n");
996
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400997 return ret_val;
998}
999
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001000/**
1001 * megasas_reset_device - Device reset handler entry point
1002 */
1003static int megasas_reset_device(struct scsi_cmnd *scmd)
1004{
1005 int ret;
1006
1007 /*
1008 * First wait for all commands to complete
1009 */
1010 ret = megasas_generic_reset(scmd);
1011
1012 return ret;
1013}
1014
1015/**
1016 * megasas_reset_bus_host - Bus & host reset handler entry point
1017 */
1018static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1019{
1020 int ret;
1021
1022 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01001023 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001024 */
1025 ret = megasas_generic_reset(scmd);
1026
1027 return ret;
1028}
1029
1030/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001031 * megasas_bios_param - Returns disk geometry for a disk
1032 * @sdev: device handle
1033 * @bdev: block device
1034 * @capacity: drive capacity
1035 * @geom: geometry parameters
1036 */
1037static int
1038megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1039 sector_t capacity, int geom[])
1040{
1041 int heads;
1042 int sectors;
1043 sector_t cylinders;
1044 unsigned long tmp;
1045 /* Default heads (64) & sectors (32) */
1046 heads = 64;
1047 sectors = 32;
1048
1049 tmp = heads * sectors;
1050 cylinders = capacity;
1051
1052 sector_div(cylinders, tmp);
1053
1054 /*
1055 * Handle extended translation size for logical drives > 1Gb
1056 */
1057
1058 if (capacity >= 0x200000) {
1059 heads = 255;
1060 sectors = 63;
1061 tmp = heads*sectors;
1062 cylinders = capacity;
1063 sector_div(cylinders, tmp);
1064 }
1065
1066 geom[0] = heads;
1067 geom[1] = sectors;
1068 geom[2] = cylinders;
1069
1070 return 0;
1071}
1072
1073/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001074 * megasas_service_aen - Processes an event notification
1075 * @instance: Adapter soft state
1076 * @cmd: AEN command completed by the ISR
1077 *
1078 * For AEN, driver sends a command down to FW that is held by the FW till an
1079 * event occurs. When an event of interest occurs, FW completes the command
1080 * that it was previously holding.
1081 *
1082 * This routines sends SIGIO signal to processes that have registered with the
1083 * driver for AEN.
1084 */
1085static void
1086megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1087{
1088 /*
1089 * Don't signal app if it is just an aborted previously registered aen
1090 */
1091 if (!cmd->abort_aen)
1092 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
1093 else
1094 cmd->abort_aen = 0;
1095
1096 instance->aen_cmd = NULL;
1097 megasas_return_cmd(instance, cmd);
1098}
1099
1100/*
1101 * Scsi host template for megaraid_sas driver
1102 */
1103static struct scsi_host_template megasas_template = {
1104
1105 .module = THIS_MODULE,
1106 .name = "LSI Logic SAS based MegaRAID driver",
1107 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01001108 .slave_configure = megasas_slave_configure,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001109 .queuecommand = megasas_queue_command,
1110 .eh_device_reset_handler = megasas_reset_device,
1111 .eh_bus_reset_handler = megasas_reset_bus_host,
1112 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001113 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001114 .use_clustering = ENABLE_CLUSTERING,
1115};
1116
1117/**
1118 * megasas_complete_int_cmd - Completes an internal command
1119 * @instance: Adapter soft state
1120 * @cmd: Command to be completed
1121 *
1122 * The megasas_issue_blocked_cmd() function waits for a command to complete
1123 * after it issues a command. This function wakes up that waiting routine by
1124 * calling wake_up() on the wait queue.
1125 */
1126static void
1127megasas_complete_int_cmd(struct megasas_instance *instance,
1128 struct megasas_cmd *cmd)
1129{
1130 cmd->cmd_status = cmd->frame->io.cmd_status;
1131
1132 if (cmd->cmd_status == ENODATA) {
1133 cmd->cmd_status = 0;
1134 }
1135 wake_up(&instance->int_cmd_wait_q);
1136}
1137
1138/**
1139 * megasas_complete_abort - Completes aborting a command
1140 * @instance: Adapter soft state
1141 * @cmd: Cmd that was issued to abort another cmd
1142 *
1143 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
1144 * after it issues an abort on a previously issued command. This function
1145 * wakes up all functions waiting on the same wait queue.
1146 */
1147static void
1148megasas_complete_abort(struct megasas_instance *instance,
1149 struct megasas_cmd *cmd)
1150{
1151 if (cmd->sync_cmd) {
1152 cmd->sync_cmd = 0;
1153 cmd->cmd_status = 0;
1154 wake_up(&instance->abort_cmd_wait_q);
1155 }
1156
1157 return;
1158}
1159
1160/**
1161 * megasas_unmap_sgbuf - Unmap SG buffers
1162 * @instance: Adapter soft state
1163 * @cmd: Completed command
1164 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001165static void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001166megasas_unmap_sgbuf(struct megasas_instance *instance, struct megasas_cmd *cmd)
1167{
1168 dma_addr_t buf_h;
1169 u8 opcode;
1170
1171 if (cmd->scmd->use_sg) {
1172 pci_unmap_sg(instance->pdev, cmd->scmd->request_buffer,
1173 cmd->scmd->use_sg, cmd->scmd->sc_data_direction);
1174 return;
1175 }
1176
1177 if (!cmd->scmd->request_bufflen)
1178 return;
1179
1180 opcode = cmd->frame->hdr.cmd;
1181
1182 if ((opcode == MFI_CMD_LD_READ) || (opcode == MFI_CMD_LD_WRITE)) {
1183 if (IS_DMA64)
1184 buf_h = cmd->frame->io.sgl.sge64[0].phys_addr;
1185 else
1186 buf_h = cmd->frame->io.sgl.sge32[0].phys_addr;
1187 } else {
1188 if (IS_DMA64)
1189 buf_h = cmd->frame->pthru.sgl.sge64[0].phys_addr;
1190 else
1191 buf_h = cmd->frame->pthru.sgl.sge32[0].phys_addr;
1192 }
1193
1194 pci_unmap_single(instance->pdev, buf_h, cmd->scmd->request_bufflen,
1195 cmd->scmd->sc_data_direction);
1196 return;
1197}
1198
1199/**
1200 * megasas_complete_cmd - Completes a command
1201 * @instance: Adapter soft state
1202 * @cmd: Command to be completed
1203 * @alt_status: If non-zero, use this value as status to
1204 * SCSI mid-layer instead of the value returned
1205 * by the FW. This should be used if caller wants
1206 * an alternate status (as in the case of aborted
1207 * commands)
1208 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001209static void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001210megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
1211 u8 alt_status)
1212{
1213 int exception = 0;
1214 struct megasas_header *hdr = &cmd->frame->hdr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001215
1216 if (cmd->scmd) {
1217 cmd->scmd->SCp.ptr = (char *)0;
1218 }
1219
1220 switch (hdr->cmd) {
1221
1222 case MFI_CMD_PD_SCSI_IO:
1223 case MFI_CMD_LD_SCSI_IO:
1224
1225 /*
1226 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1227 * issued either through an IO path or an IOCTL path. If it
1228 * was via IOCTL, we will send it to internal completion.
1229 */
1230 if (cmd->sync_cmd) {
1231 cmd->sync_cmd = 0;
1232 megasas_complete_int_cmd(instance, cmd);
1233 break;
1234 }
1235
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001236 case MFI_CMD_LD_READ:
1237 case MFI_CMD_LD_WRITE:
1238
1239 if (alt_status) {
1240 cmd->scmd->result = alt_status << 16;
1241 exception = 1;
1242 }
1243
1244 if (exception) {
1245
Sumant Patroe4a082c2006-05-30 12:03:37 -07001246 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001247
1248 megasas_unmap_sgbuf(instance, cmd);
1249 cmd->scmd->scsi_done(cmd->scmd);
1250 megasas_return_cmd(instance, cmd);
1251
1252 break;
1253 }
1254
1255 switch (hdr->cmd_status) {
1256
1257 case MFI_STAT_OK:
1258 cmd->scmd->result = DID_OK << 16;
1259 break;
1260
1261 case MFI_STAT_SCSI_IO_FAILED:
1262 case MFI_STAT_LD_INIT_IN_PROGRESS:
1263 cmd->scmd->result =
1264 (DID_ERROR << 16) | hdr->scsi_status;
1265 break;
1266
1267 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1268
1269 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1270
1271 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1272 memset(cmd->scmd->sense_buffer, 0,
1273 SCSI_SENSE_BUFFERSIZE);
1274 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1275 hdr->sense_len);
1276
1277 cmd->scmd->result |= DRIVER_SENSE << 24;
1278 }
1279
1280 break;
1281
1282 case MFI_STAT_LD_OFFLINE:
1283 case MFI_STAT_DEVICE_NOT_FOUND:
1284 cmd->scmd->result = DID_BAD_TARGET << 16;
1285 break;
1286
1287 default:
1288 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1289 hdr->cmd_status);
1290 cmd->scmd->result = DID_ERROR << 16;
1291 break;
1292 }
1293
Sumant Patroe4a082c2006-05-30 12:03:37 -07001294 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001295
1296 megasas_unmap_sgbuf(instance, cmd);
1297 cmd->scmd->scsi_done(cmd->scmd);
1298 megasas_return_cmd(instance, cmd);
1299
1300 break;
1301
1302 case MFI_CMD_SMP:
1303 case MFI_CMD_STP:
1304 case MFI_CMD_DCMD:
1305
1306 /*
1307 * See if got an event notification
1308 */
1309 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1310 megasas_service_aen(instance, cmd);
1311 else
1312 megasas_complete_int_cmd(instance, cmd);
1313
1314 break;
1315
1316 case MFI_CMD_ABORT:
1317 /*
1318 * Cmd issued to abort another cmd returned
1319 */
1320 megasas_complete_abort(instance, cmd);
1321 break;
1322
1323 default:
1324 printk("megasas: Unknown command completed! [0x%X]\n",
1325 hdr->cmd);
1326 break;
1327 }
1328}
1329
1330/**
1331 * megasas_deplete_reply_queue - Processes all completed commands
1332 * @instance: Adapter soft state
1333 * @alt_status: Alternate status to be returned to
1334 * SCSI mid-layer instead of the status
1335 * returned by the FW
1336 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001337static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001338megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1339{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001340 /*
1341 * Check if it is our interrupt
Sumant Patro1341c932006-01-25 12:02:40 -08001342 * Clear the interrupt
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001343 */
Sumant Patro1341c932006-01-25 12:02:40 -08001344 if(instance->instancet->clear_intr(instance->reg_set))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001345 return IRQ_NONE;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001346
Sumant Patroaf37acf2007-02-14 12:34:46 -08001347 if (instance->hw_crit_error)
1348 goto out_done;
Sumant Patro5d018ad2006-10-03 13:13:18 -07001349 /*
1350 * Schedule the tasklet for cmd completion
1351 */
1352 tasklet_schedule(&instance->isr_tasklet);
Sumant Patroaf37acf2007-02-14 12:34:46 -08001353out_done:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001354 return IRQ_HANDLED;
1355}
1356
1357/**
1358 * megasas_isr - isr entry point
1359 */
David Howells7d12e782006-10-05 14:55:46 +01001360static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001361{
1362 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1363 DID_OK);
1364}
1365
1366/**
1367 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08001368 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001369 *
1370 * During the initialization, FW passes can potentially be in any one of
1371 * several possible states. If the FW in operational, waiting-for-handshake
1372 * states, driver must take steps to bring it to ready state. Otherwise, it
1373 * has to wait for the ready state.
1374 */
1375static int
Sumant Patro1341c932006-01-25 12:02:40 -08001376megasas_transition_to_ready(struct megasas_instance* instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001377{
1378 int i;
1379 u8 max_wait;
1380 u32 fw_state;
1381 u32 cur_state;
1382
Sumant Patro1341c932006-01-25 12:02:40 -08001383 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001384
Sumant Patroe3bbff92006-10-03 12:28:49 -07001385 if (fw_state != MFI_STATE_READY)
1386 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1387 " state\n");
1388
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001389 while (fw_state != MFI_STATE_READY) {
1390
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001391 switch (fw_state) {
1392
1393 case MFI_STATE_FAULT:
1394
1395 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1396 return -ENODEV;
1397
1398 case MFI_STATE_WAIT_HANDSHAKE:
1399 /*
1400 * Set the CLR bit in inbound doorbell
1401 */
Sumant Patroe3bbff92006-10-03 12:28:49 -07001402 writel(MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
Sumant Patro1341c932006-01-25 12:02:40 -08001403 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001404
1405 max_wait = 2;
1406 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1407 break;
1408
Sumant Patroe3bbff92006-10-03 12:28:49 -07001409 case MFI_STATE_BOOT_MESSAGE_PENDING:
1410 writel(MFI_INIT_HOTPLUG,
1411 &instance->reg_set->inbound_doorbell);
1412
1413 max_wait = 10;
1414 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
1415 break;
1416
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001417 case MFI_STATE_OPERATIONAL:
1418 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07001419 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001420 */
Sumant Patrob274cab2006-10-03 12:52:12 -07001421 instance->instancet->disable_intr(instance->reg_set);
Sumant Patroe3bbff92006-10-03 12:28:49 -07001422 writel(MFI_RESET_FLAGS, &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001423
1424 max_wait = 10;
1425 cur_state = MFI_STATE_OPERATIONAL;
1426 break;
1427
1428 case MFI_STATE_UNDEFINED:
1429 /*
1430 * This state should not last for more than 2 seconds
1431 */
1432 max_wait = 2;
1433 cur_state = MFI_STATE_UNDEFINED;
1434 break;
1435
1436 case MFI_STATE_BB_INIT:
1437 max_wait = 2;
1438 cur_state = MFI_STATE_BB_INIT;
1439 break;
1440
1441 case MFI_STATE_FW_INIT:
1442 max_wait = 20;
1443 cur_state = MFI_STATE_FW_INIT;
1444 break;
1445
1446 case MFI_STATE_FW_INIT_2:
1447 max_wait = 20;
1448 cur_state = MFI_STATE_FW_INIT_2;
1449 break;
1450
1451 case MFI_STATE_DEVICE_SCAN:
1452 max_wait = 20;
1453 cur_state = MFI_STATE_DEVICE_SCAN;
1454 break;
1455
1456 case MFI_STATE_FLUSH_CACHE:
1457 max_wait = 20;
1458 cur_state = MFI_STATE_FLUSH_CACHE;
1459 break;
1460
1461 default:
1462 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1463 fw_state);
1464 return -ENODEV;
1465 }
1466
1467 /*
1468 * The cur_state should not last for more than max_wait secs
1469 */
1470 for (i = 0; i < (max_wait * 1000); i++) {
Sumant Patro1341c932006-01-25 12:02:40 -08001471 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1472 MFI_STATE_MASK ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001473
1474 if (fw_state == cur_state) {
1475 msleep(1);
1476 } else
1477 break;
1478 }
1479
1480 /*
1481 * Return error if fw_state hasn't changed after max_wait
1482 */
1483 if (fw_state == cur_state) {
1484 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1485 "in %d secs\n", fw_state, max_wait);
1486 return -ENODEV;
1487 }
1488 };
Sumant Patroe3bbff92006-10-03 12:28:49 -07001489 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001490
1491 return 0;
1492}
1493
1494/**
1495 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1496 * @instance: Adapter soft state
1497 */
1498static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1499{
1500 int i;
1501 u32 max_cmd = instance->max_fw_cmds;
1502 struct megasas_cmd *cmd;
1503
1504 if (!instance->frame_dma_pool)
1505 return;
1506
1507 /*
1508 * Return all frames to pool
1509 */
1510 for (i = 0; i < max_cmd; i++) {
1511
1512 cmd = instance->cmd_list[i];
1513
1514 if (cmd->frame)
1515 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1516 cmd->frame_phys_addr);
1517
1518 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07001519 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001520 cmd->sense_phys_addr);
1521 }
1522
1523 /*
1524 * Now destroy the pool itself
1525 */
1526 pci_pool_destroy(instance->frame_dma_pool);
1527 pci_pool_destroy(instance->sense_dma_pool);
1528
1529 instance->frame_dma_pool = NULL;
1530 instance->sense_dma_pool = NULL;
1531}
1532
1533/**
1534 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1535 * @instance: Adapter soft state
1536 *
1537 * Each command packet has an embedded DMA memory buffer that is used for
1538 * filling MFI frame and the SG list that immediately follows the frame. This
1539 * function creates those DMA memory buffers for each command packet by using
1540 * PCI pool facility.
1541 */
1542static int megasas_create_frame_pool(struct megasas_instance *instance)
1543{
1544 int i;
1545 u32 max_cmd;
1546 u32 sge_sz;
1547 u32 sgl_sz;
1548 u32 total_sz;
1549 u32 frame_count;
1550 struct megasas_cmd *cmd;
1551
1552 max_cmd = instance->max_fw_cmds;
1553
1554 /*
1555 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1556 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1557 */
1558 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1559 sizeof(struct megasas_sge32);
1560
1561 /*
1562 * Calculated the number of 64byte frames required for SGL
1563 */
1564 sgl_sz = sge_sz * instance->max_num_sge;
1565 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1566
1567 /*
1568 * We need one extra frame for the MFI command
1569 */
1570 frame_count++;
1571
1572 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1573 /*
1574 * Use DMA pool facility provided by PCI layer
1575 */
1576 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1577 instance->pdev, total_sz, 64,
1578 0);
1579
1580 if (!instance->frame_dma_pool) {
1581 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1582 return -ENOMEM;
1583 }
1584
1585 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1586 instance->pdev, 128, 4, 0);
1587
1588 if (!instance->sense_dma_pool) {
1589 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1590
1591 pci_pool_destroy(instance->frame_dma_pool);
1592 instance->frame_dma_pool = NULL;
1593
1594 return -ENOMEM;
1595 }
1596
1597 /*
1598 * Allocate and attach a frame to each of the commands in cmd_list.
1599 * By making cmd->index as the context instead of the &cmd, we can
1600 * always use 32bit context regardless of the architecture
1601 */
1602 for (i = 0; i < max_cmd; i++) {
1603
1604 cmd = instance->cmd_list[i];
1605
1606 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1607 GFP_KERNEL, &cmd->frame_phys_addr);
1608
1609 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1610 GFP_KERNEL, &cmd->sense_phys_addr);
1611
1612 /*
1613 * megasas_teardown_frame_pool() takes care of freeing
1614 * whatever has been allocated
1615 */
1616 if (!cmd->frame || !cmd->sense) {
1617 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1618 megasas_teardown_frame_pool(instance);
1619 return -ENOMEM;
1620 }
1621
1622 cmd->frame->io.context = cmd->index;
1623 }
1624
1625 return 0;
1626}
1627
1628/**
1629 * megasas_free_cmds - Free all the cmds in the free cmd pool
1630 * @instance: Adapter soft state
1631 */
1632static void megasas_free_cmds(struct megasas_instance *instance)
1633{
1634 int i;
1635 /* First free the MFI frame pool */
1636 megasas_teardown_frame_pool(instance);
1637
1638 /* Free all the commands in the cmd_list */
1639 for (i = 0; i < instance->max_fw_cmds; i++)
1640 kfree(instance->cmd_list[i]);
1641
1642 /* Free the cmd_list buffer itself */
1643 kfree(instance->cmd_list);
1644 instance->cmd_list = NULL;
1645
1646 INIT_LIST_HEAD(&instance->cmd_pool);
1647}
1648
1649/**
1650 * megasas_alloc_cmds - Allocates the command packets
1651 * @instance: Adapter soft state
1652 *
1653 * Each command that is issued to the FW, whether IO commands from the OS or
1654 * internal commands like IOCTLs, are wrapped in local data structure called
1655 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1656 * the FW.
1657 *
1658 * Each frame has a 32-bit field called context (tag). This context is used
1659 * to get back the megasas_cmd from the frame when a frame gets completed in
1660 * the ISR. Typically the address of the megasas_cmd itself would be used as
1661 * the context. But we wanted to keep the differences between 32 and 64 bit
1662 * systems to the mininum. We always use 32 bit integers for the context. In
1663 * this driver, the 32 bit values are the indices into an array cmd_list.
1664 * This array is used only to look up the megasas_cmd given the context. The
1665 * free commands themselves are maintained in a linked list called cmd_pool.
1666 */
1667static int megasas_alloc_cmds(struct megasas_instance *instance)
1668{
1669 int i;
1670 int j;
1671 u32 max_cmd;
1672 struct megasas_cmd *cmd;
1673
1674 max_cmd = instance->max_fw_cmds;
1675
1676 /*
1677 * instance->cmd_list is an array of struct megasas_cmd pointers.
1678 * Allocate the dynamic array first and then allocate individual
1679 * commands.
1680 */
1681 instance->cmd_list = kmalloc(sizeof(struct megasas_cmd *) * max_cmd,
1682 GFP_KERNEL);
1683
1684 if (!instance->cmd_list) {
1685 printk(KERN_DEBUG "megasas: out of memory\n");
1686 return -ENOMEM;
1687 }
1688
1689 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) * max_cmd);
1690
1691 for (i = 0; i < max_cmd; i++) {
1692 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
1693 GFP_KERNEL);
1694
1695 if (!instance->cmd_list[i]) {
1696
1697 for (j = 0; j < i; j++)
1698 kfree(instance->cmd_list[j]);
1699
1700 kfree(instance->cmd_list);
1701 instance->cmd_list = NULL;
1702
1703 return -ENOMEM;
1704 }
1705 }
1706
1707 /*
1708 * Add all the commands to command pool (instance->cmd_pool)
1709 */
1710 for (i = 0; i < max_cmd; i++) {
1711 cmd = instance->cmd_list[i];
1712 memset(cmd, 0, sizeof(struct megasas_cmd));
1713 cmd->index = i;
1714 cmd->instance = instance;
1715
1716 list_add_tail(&cmd->list, &instance->cmd_pool);
1717 }
1718
1719 /*
1720 * Create a frame pool and assign one frame to each cmd
1721 */
1722 if (megasas_create_frame_pool(instance)) {
1723 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
1724 megasas_free_cmds(instance);
1725 }
1726
1727 return 0;
1728}
1729
1730/**
1731 * megasas_get_controller_info - Returns FW's controller structure
1732 * @instance: Adapter soft state
1733 * @ctrl_info: Controller information structure
1734 *
1735 * Issues an internal command (DCMD) to get the FW's controller structure.
1736 * This information is mainly used to find out the maximum IO transfer per
1737 * command supported by the FW.
1738 */
1739static int
1740megasas_get_ctrl_info(struct megasas_instance *instance,
1741 struct megasas_ctrl_info *ctrl_info)
1742{
1743 int ret = 0;
1744 struct megasas_cmd *cmd;
1745 struct megasas_dcmd_frame *dcmd;
1746 struct megasas_ctrl_info *ci;
1747 dma_addr_t ci_h = 0;
1748
1749 cmd = megasas_get_cmd(instance);
1750
1751 if (!cmd) {
1752 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
1753 return -ENOMEM;
1754 }
1755
1756 dcmd = &cmd->frame->dcmd;
1757
1758 ci = pci_alloc_consistent(instance->pdev,
1759 sizeof(struct megasas_ctrl_info), &ci_h);
1760
1761 if (!ci) {
1762 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
1763 megasas_return_cmd(instance, cmd);
1764 return -ENOMEM;
1765 }
1766
1767 memset(ci, 0, sizeof(*ci));
1768 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1769
1770 dcmd->cmd = MFI_CMD_DCMD;
1771 dcmd->cmd_status = 0xFF;
1772 dcmd->sge_count = 1;
1773 dcmd->flags = MFI_FRAME_DIR_READ;
1774 dcmd->timeout = 0;
1775 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
1776 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
1777 dcmd->sgl.sge32[0].phys_addr = ci_h;
1778 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
1779
1780 if (!megasas_issue_polled(instance, cmd)) {
1781 ret = 0;
1782 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
1783 } else {
1784 ret = -1;
1785 }
1786
1787 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
1788 ci, ci_h);
1789
1790 megasas_return_cmd(instance, cmd);
1791 return ret;
1792}
1793
1794/**
Sumant Patro5d018ad2006-10-03 13:13:18 -07001795 * megasas_complete_cmd_dpc - Returns FW's controller structure
1796 * @instance_addr: Address of adapter soft state
1797 *
1798 * Tasklet to complete cmds
1799 */
Adrian Bunkb448de42006-11-20 03:23:49 +01001800static void megasas_complete_cmd_dpc(unsigned long instance_addr)
Sumant Patro5d018ad2006-10-03 13:13:18 -07001801{
1802 u32 producer;
1803 u32 consumer;
1804 u32 context;
1805 struct megasas_cmd *cmd;
1806 struct megasas_instance *instance = (struct megasas_instance *)instance_addr;
1807
Sumant Patroaf37acf2007-02-14 12:34:46 -08001808 /* If we have already declared adapter dead, donot complete cmds */
1809 if (instance->hw_crit_error)
1810 return;
1811
Sumant Patro5d018ad2006-10-03 13:13:18 -07001812 producer = *instance->producer;
1813 consumer = *instance->consumer;
1814
1815 while (consumer != producer) {
1816 context = instance->reply_queue[consumer];
1817
1818 cmd = instance->cmd_list[context];
1819
1820 megasas_complete_cmd(instance, cmd, DID_OK);
1821
1822 consumer++;
1823 if (consumer == (instance->max_fw_cmds + 1)) {
1824 consumer = 0;
1825 }
1826 }
1827
1828 *instance->consumer = producer;
1829}
1830
1831/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001832 * megasas_init_mfi - Initializes the FW
1833 * @instance: Adapter soft state
1834 *
1835 * This is the main function for initializing MFI firmware.
1836 */
1837static int megasas_init_mfi(struct megasas_instance *instance)
1838{
1839 u32 context_sz;
1840 u32 reply_q_sz;
1841 u32 max_sectors_1;
1842 u32 max_sectors_2;
1843 struct megasas_register_set __iomem *reg_set;
1844
1845 struct megasas_cmd *cmd;
1846 struct megasas_ctrl_info *ctrl_info;
1847
1848 struct megasas_init_frame *init_frame;
1849 struct megasas_init_queue_info *initq_info;
1850 dma_addr_t init_frame_h;
1851 dma_addr_t initq_info_h;
1852
1853 /*
1854 * Map the message registers
1855 */
1856 instance->base_addr = pci_resource_start(instance->pdev, 0);
1857
1858 if (pci_request_regions(instance->pdev, "megasas: LSI Logic")) {
1859 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
1860 return -EBUSY;
1861 }
1862
1863 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
1864
1865 if (!instance->reg_set) {
1866 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
1867 goto fail_ioremap;
1868 }
1869
1870 reg_set = instance->reg_set;
1871
Sumant Patrof9876f02006-02-03 15:34:35 -08001872 switch(instance->pdev->device)
1873 {
1874 case PCI_DEVICE_ID_LSI_SAS1078R:
1875 instance->instancet = &megasas_instance_template_ppc;
1876 break;
1877 case PCI_DEVICE_ID_LSI_SAS1064R:
1878 case PCI_DEVICE_ID_DELL_PERC5:
1879 default:
1880 instance->instancet = &megasas_instance_template_xscale;
1881 break;
1882 }
Sumant Patro1341c932006-01-25 12:02:40 -08001883
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001884 /*
1885 * We expect the FW state to be READY
1886 */
Sumant Patro1341c932006-01-25 12:02:40 -08001887 if (megasas_transition_to_ready(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001888 goto fail_ready_state;
1889
1890 /*
1891 * Get various operational parameters from status register
1892 */
Sumant Patro1341c932006-01-25 12:02:40 -08001893 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07001894 /*
1895 * Reduce the max supported cmds by 1. This is to ensure that the
1896 * reply_q_sz (1 more than the max cmd that driver may send)
1897 * does not exceed max cmds that the FW can support
1898 */
1899 instance->max_fw_cmds = instance->max_fw_cmds-1;
Sumant Patro1341c932006-01-25 12:02:40 -08001900 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
1901 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001902 /*
1903 * Create a pool of commands
1904 */
1905 if (megasas_alloc_cmds(instance))
1906 goto fail_alloc_cmds;
1907
1908 /*
1909 * Allocate memory for reply queue. Length of reply queue should
1910 * be _one_ more than the maximum commands handled by the firmware.
1911 *
1912 * Note: When FW completes commands, it places corresponding contex
1913 * values in this circular reply queue. This circular queue is a fairly
1914 * typical producer-consumer queue. FW is the producer (of completed
1915 * commands) and the driver is the consumer.
1916 */
1917 context_sz = sizeof(u32);
1918 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
1919
1920 instance->reply_queue = pci_alloc_consistent(instance->pdev,
1921 reply_q_sz,
1922 &instance->reply_queue_h);
1923
1924 if (!instance->reply_queue) {
1925 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
1926 goto fail_reply_queue;
1927 }
1928
1929 /*
1930 * Prepare a init frame. Note the init frame points to queue info
1931 * structure. Each frame has SGL allocated after first 64 bytes. For
1932 * this frame - since we don't need any SGL - we use SGL's space as
1933 * queue info structure
1934 *
1935 * We will not get a NULL command below. We just created the pool.
1936 */
1937 cmd = megasas_get_cmd(instance);
1938
1939 init_frame = (struct megasas_init_frame *)cmd->frame;
1940 initq_info = (struct megasas_init_queue_info *)
1941 ((unsigned long)init_frame + 64);
1942
1943 init_frame_h = cmd->frame_phys_addr;
1944 initq_info_h = init_frame_h + 64;
1945
1946 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
1947 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
1948
1949 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
1950 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
1951
1952 initq_info->producer_index_phys_addr_lo = instance->producer_h;
1953 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
1954
1955 init_frame->cmd = MFI_CMD_INIT;
1956 init_frame->cmd_status = 0xFF;
1957 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
1958
1959 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
1960
1961 /*
Sumant Patro0e989362006-06-20 15:32:37 -07001962 * disable the intr before firing the init frame to FW
1963 */
Sumant Patrob274cab2006-10-03 12:52:12 -07001964 instance->instancet->disable_intr(instance->reg_set);
Sumant Patro0e989362006-06-20 15:32:37 -07001965
1966 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001967 * Issue the init frame in polled mode
1968 */
1969 if (megasas_issue_polled(instance, cmd)) {
1970 printk(KERN_DEBUG "megasas: Failed to init firmware\n");
1971 goto fail_fw_init;
1972 }
1973
1974 megasas_return_cmd(instance, cmd);
1975
1976 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
1977
1978 /*
1979 * Compute the max allowed sectors per IO: The controller info has two
1980 * limits on max sectors. Driver should use the minimum of these two.
1981 *
1982 * 1 << stripe_sz_ops.min = max sectors per strip
1983 *
1984 * Note that older firmwares ( < FW ver 30) didn't report information
1985 * to calculate max_sectors_1. So the number ended up as zero always.
1986 */
1987 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
1988
1989 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
1990 ctrl_info->max_strips_per_io;
1991 max_sectors_2 = ctrl_info->max_request_size;
1992
1993 instance->max_sectors_per_req = (max_sectors_1 < max_sectors_2)
1994 ? max_sectors_1 : max_sectors_2;
1995 } else
1996 instance->max_sectors_per_req = instance->max_num_sge *
1997 PAGE_SIZE / 512;
1998
1999 kfree(ctrl_info);
2000
Sumant Patro5d018ad2006-10-03 13:13:18 -07002001 /*
2002 * Setup tasklet for cmd completion
2003 */
2004
2005 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2006 (unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002007 return 0;
2008
2009 fail_fw_init:
2010 megasas_return_cmd(instance, cmd);
2011
2012 pci_free_consistent(instance->pdev, reply_q_sz,
2013 instance->reply_queue, instance->reply_queue_h);
2014 fail_reply_queue:
2015 megasas_free_cmds(instance);
2016
2017 fail_alloc_cmds:
2018 fail_ready_state:
2019 iounmap(instance->reg_set);
2020
2021 fail_ioremap:
2022 pci_release_regions(instance->pdev);
2023
2024 return -EINVAL;
2025}
2026
2027/**
2028 * megasas_release_mfi - Reverses the FW initialization
2029 * @intance: Adapter soft state
2030 */
2031static void megasas_release_mfi(struct megasas_instance *instance)
2032{
2033 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
2034
2035 pci_free_consistent(instance->pdev, reply_q_sz,
2036 instance->reply_queue, instance->reply_queue_h);
2037
2038 megasas_free_cmds(instance);
2039
2040 iounmap(instance->reg_set);
2041
2042 pci_release_regions(instance->pdev);
2043}
2044
2045/**
2046 * megasas_get_seq_num - Gets latest event sequence numbers
2047 * @instance: Adapter soft state
2048 * @eli: FW event log sequence numbers information
2049 *
2050 * FW maintains a log of all events in a non-volatile area. Upper layers would
2051 * usually find out the latest sequence number of the events, the seq number at
2052 * the boot etc. They would "read" all the events below the latest seq number
2053 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
2054 * number), they would subsribe to AEN (asynchronous event notification) and
2055 * wait for the events to happen.
2056 */
2057static int
2058megasas_get_seq_num(struct megasas_instance *instance,
2059 struct megasas_evt_log_info *eli)
2060{
2061 struct megasas_cmd *cmd;
2062 struct megasas_dcmd_frame *dcmd;
2063 struct megasas_evt_log_info *el_info;
2064 dma_addr_t el_info_h = 0;
2065
2066 cmd = megasas_get_cmd(instance);
2067
2068 if (!cmd) {
2069 return -ENOMEM;
2070 }
2071
2072 dcmd = &cmd->frame->dcmd;
2073 el_info = pci_alloc_consistent(instance->pdev,
2074 sizeof(struct megasas_evt_log_info),
2075 &el_info_h);
2076
2077 if (!el_info) {
2078 megasas_return_cmd(instance, cmd);
2079 return -ENOMEM;
2080 }
2081
2082 memset(el_info, 0, sizeof(*el_info));
2083 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2084
2085 dcmd->cmd = MFI_CMD_DCMD;
2086 dcmd->cmd_status = 0x0;
2087 dcmd->sge_count = 1;
2088 dcmd->flags = MFI_FRAME_DIR_READ;
2089 dcmd->timeout = 0;
2090 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
2091 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
2092 dcmd->sgl.sge32[0].phys_addr = el_info_h;
2093 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
2094
2095 megasas_issue_blocked_cmd(instance, cmd);
2096
2097 /*
2098 * Copy the data back into callers buffer
2099 */
2100 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
2101
2102 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
2103 el_info, el_info_h);
2104
2105 megasas_return_cmd(instance, cmd);
2106
2107 return 0;
2108}
2109
2110/**
2111 * megasas_register_aen - Registers for asynchronous event notification
2112 * @instance: Adapter soft state
2113 * @seq_num: The starting sequence number
2114 * @class_locale: Class of the event
2115 *
2116 * This function subscribes for AEN for events beyond the @seq_num. It requests
2117 * to be notified if and only if the event is of type @class_locale
2118 */
2119static int
2120megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
2121 u32 class_locale_word)
2122{
2123 int ret_val;
2124 struct megasas_cmd *cmd;
2125 struct megasas_dcmd_frame *dcmd;
2126 union megasas_evt_class_locale curr_aen;
2127 union megasas_evt_class_locale prev_aen;
2128
2129 /*
2130 * If there an AEN pending already (aen_cmd), check if the
2131 * class_locale of that pending AEN is inclusive of the new
2132 * AEN request we currently have. If it is, then we don't have
2133 * to do anything. In other words, whichever events the current
2134 * AEN request is subscribing to, have already been subscribed
2135 * to.
2136 *
2137 * If the old_cmd is _not_ inclusive, then we have to abort
2138 * that command, form a class_locale that is superset of both
2139 * old and current and re-issue to the FW
2140 */
2141
2142 curr_aen.word = class_locale_word;
2143
2144 if (instance->aen_cmd) {
2145
2146 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
2147
2148 /*
2149 * A class whose enum value is smaller is inclusive of all
2150 * higher values. If a PROGRESS (= -1) was previously
2151 * registered, then a new registration requests for higher
2152 * classes need not be sent to FW. They are automatically
2153 * included.
2154 *
2155 * Locale numbers don't have such hierarchy. They are bitmap
2156 * values
2157 */
2158 if ((prev_aen.members.class <= curr_aen.members.class) &&
2159 !((prev_aen.members.locale & curr_aen.members.locale) ^
2160 curr_aen.members.locale)) {
2161 /*
2162 * Previously issued event registration includes
2163 * current request. Nothing to do.
2164 */
2165 return 0;
2166 } else {
2167 curr_aen.members.locale |= prev_aen.members.locale;
2168
2169 if (prev_aen.members.class < curr_aen.members.class)
2170 curr_aen.members.class = prev_aen.members.class;
2171
2172 instance->aen_cmd->abort_aen = 1;
2173 ret_val = megasas_issue_blocked_abort_cmd(instance,
2174 instance->
2175 aen_cmd);
2176
2177 if (ret_val) {
2178 printk(KERN_DEBUG "megasas: Failed to abort "
2179 "previous AEN command\n");
2180 return ret_val;
2181 }
2182 }
2183 }
2184
2185 cmd = megasas_get_cmd(instance);
2186
2187 if (!cmd)
2188 return -ENOMEM;
2189
2190 dcmd = &cmd->frame->dcmd;
2191
2192 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
2193
2194 /*
2195 * Prepare DCMD for aen registration
2196 */
2197 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2198
2199 dcmd->cmd = MFI_CMD_DCMD;
2200 dcmd->cmd_status = 0x0;
2201 dcmd->sge_count = 1;
2202 dcmd->flags = MFI_FRAME_DIR_READ;
2203 dcmd->timeout = 0;
2204 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
2205 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
2206 dcmd->mbox.w[0] = seq_num;
2207 dcmd->mbox.w[1] = curr_aen.word;
2208 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
2209 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
2210
2211 /*
2212 * Store reference to the cmd used to register for AEN. When an
2213 * application wants us to register for AEN, we have to abort this
2214 * cmd and re-register with a new EVENT LOCALE supplied by that app
2215 */
2216 instance->aen_cmd = cmd;
2217
2218 /*
2219 * Issue the aen registration frame
2220 */
Sumant Patro1341c932006-01-25 12:02:40 -08002221 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002222
2223 return 0;
2224}
2225
2226/**
2227 * megasas_start_aen - Subscribes to AEN during driver load time
2228 * @instance: Adapter soft state
2229 */
2230static int megasas_start_aen(struct megasas_instance *instance)
2231{
2232 struct megasas_evt_log_info eli;
2233 union megasas_evt_class_locale class_locale;
2234
2235 /*
2236 * Get the latest sequence number from FW
2237 */
2238 memset(&eli, 0, sizeof(eli));
2239
2240 if (megasas_get_seq_num(instance, &eli))
2241 return -1;
2242
2243 /*
2244 * Register AEN with FW for latest sequence number plus 1
2245 */
2246 class_locale.members.reserved = 0;
2247 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2248 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2249
2250 return megasas_register_aen(instance, eli.newest_seq_num + 1,
2251 class_locale.word);
2252}
2253
2254/**
2255 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2256 * @instance: Adapter soft state
2257 */
2258static int megasas_io_attach(struct megasas_instance *instance)
2259{
2260 struct Scsi_Host *host = instance->host;
2261
2262 /*
2263 * Export parameters required by SCSI mid-layer
2264 */
2265 host->irq = instance->pdev->irq;
2266 host->unique_id = instance->unique_id;
2267 host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
2268 host->this_id = instance->init_id;
2269 host->sg_tablesize = instance->max_num_sge;
2270 host->max_sectors = instance->max_sectors_per_req;
2271 host->cmd_per_lun = 128;
2272 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
2273 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
2274 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08002275 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002276
2277 /*
2278 * Notify the mid-layer about the new controller
2279 */
2280 if (scsi_add_host(host, &instance->pdev->dev)) {
2281 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2282 return -ENODEV;
2283 }
2284
2285 /*
2286 * Trigger SCSI to scan our drives
2287 */
2288 scsi_scan_host(host);
2289 return 0;
2290}
2291
2292/**
2293 * megasas_probe_one - PCI hotplug entry point
2294 * @pdev: PCI device structure
2295 * @id: PCI ids of supported hotplugged adapter
2296 */
2297static int __devinit
2298megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2299{
2300 int rval;
2301 struct Scsi_Host *host;
2302 struct megasas_instance *instance;
2303
2304 /*
2305 * Announce PCI information
2306 */
2307 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2308 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2309 pdev->subsystem_device);
2310
2311 printk("bus %d:slot %d:func %d\n",
2312 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2313
2314 /*
2315 * PCI prepping: enable device set bus mastering and dma mask
2316 */
2317 rval = pci_enable_device(pdev);
2318
2319 if (rval) {
2320 return rval;
2321 }
2322
2323 pci_set_master(pdev);
2324
2325 /*
2326 * All our contollers are capable of performing 64-bit DMA
2327 */
2328 if (IS_DMA64) {
2329 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK) != 0) {
2330
2331 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2332 goto fail_set_dma_mask;
2333 }
2334 } else {
2335 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2336 goto fail_set_dma_mask;
2337 }
2338
2339 host = scsi_host_alloc(&megasas_template,
2340 sizeof(struct megasas_instance));
2341
2342 if (!host) {
2343 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2344 goto fail_alloc_instance;
2345 }
2346
2347 instance = (struct megasas_instance *)host->hostdata;
2348 memset(instance, 0, sizeof(*instance));
2349
2350 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2351 &instance->producer_h);
2352 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2353 &instance->consumer_h);
2354
2355 if (!instance->producer || !instance->consumer) {
2356 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2357 "producer, consumer\n");
2358 goto fail_alloc_dma_buf;
2359 }
2360
2361 *instance->producer = 0;
2362 *instance->consumer = 0;
2363
2364 instance->evt_detail = pci_alloc_consistent(pdev,
2365 sizeof(struct
2366 megasas_evt_detail),
2367 &instance->evt_detail_h);
2368
2369 if (!instance->evt_detail) {
2370 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2371 "event detail structure\n");
2372 goto fail_alloc_dma_buf;
2373 }
2374
2375 /*
2376 * Initialize locks and queues
2377 */
2378 INIT_LIST_HEAD(&instance->cmd_pool);
2379
Sumant Patroe4a082c2006-05-30 12:03:37 -07002380 atomic_set(&instance->fw_outstanding,0);
2381
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002382 init_waitqueue_head(&instance->int_cmd_wait_q);
2383 init_waitqueue_head(&instance->abort_cmd_wait_q);
2384
2385 spin_lock_init(&instance->cmd_pool_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002386
2387 sema_init(&instance->aen_mutex, 1);
2388 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2389
2390 /*
2391 * Initialize PCI related and misc parameters
2392 */
2393 instance->pdev = pdev;
2394 instance->host = host;
2395 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2396 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2397
Sumant Patro658dced2006-10-03 13:09:14 -07002398 megasas_dbg_lvl = 0;
2399
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002400 /*
2401 * Initialize MFI Firmware
2402 */
2403 if (megasas_init_mfi(instance))
2404 goto fail_init_mfi;
2405
2406 /*
2407 * Register IRQ
2408 */
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07002409 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002410 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2411 goto fail_irq;
2412 }
2413
Sumant Patro1341c932006-01-25 12:02:40 -08002414 instance->instancet->enable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002415
2416 /*
2417 * Store instance in PCI softstate
2418 */
2419 pci_set_drvdata(pdev, instance);
2420
2421 /*
2422 * Add this controller to megasas_mgmt_info structure so that it
2423 * can be exported to management applications
2424 */
2425 megasas_mgmt_info.count++;
2426 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2427 megasas_mgmt_info.max_index++;
2428
2429 /*
2430 * Initiate AEN (Asynchronous Event Notification)
2431 */
2432 if (megasas_start_aen(instance)) {
2433 printk(KERN_DEBUG "megasas: start aen failed\n");
2434 goto fail_start_aen;
2435 }
2436
2437 /*
2438 * Register with SCSI mid-layer
2439 */
2440 if (megasas_io_attach(instance))
2441 goto fail_io_attach;
2442
2443 return 0;
2444
2445 fail_start_aen:
2446 fail_io_attach:
2447 megasas_mgmt_info.count--;
2448 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2449 megasas_mgmt_info.max_index--;
2450
2451 pci_set_drvdata(pdev, NULL);
Sumant Patrob274cab2006-10-03 12:52:12 -07002452 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002453 free_irq(instance->pdev->irq, instance);
2454
2455 megasas_release_mfi(instance);
2456
2457 fail_irq:
2458 fail_init_mfi:
2459 fail_alloc_dma_buf:
2460 if (instance->evt_detail)
2461 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2462 instance->evt_detail,
2463 instance->evt_detail_h);
2464
2465 if (instance->producer)
2466 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2467 instance->producer_h);
2468 if (instance->consumer)
2469 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2470 instance->consumer_h);
2471 scsi_host_put(host);
2472
2473 fail_alloc_instance:
2474 fail_set_dma_mask:
2475 pci_disable_device(pdev);
2476
2477 return -ENODEV;
2478}
2479
2480/**
2481 * megasas_flush_cache - Requests FW to flush all its caches
2482 * @instance: Adapter soft state
2483 */
2484static void megasas_flush_cache(struct megasas_instance *instance)
2485{
2486 struct megasas_cmd *cmd;
2487 struct megasas_dcmd_frame *dcmd;
2488
2489 cmd = megasas_get_cmd(instance);
2490
2491 if (!cmd)
2492 return;
2493
2494 dcmd = &cmd->frame->dcmd;
2495
2496 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2497
2498 dcmd->cmd = MFI_CMD_DCMD;
2499 dcmd->cmd_status = 0x0;
2500 dcmd->sge_count = 0;
2501 dcmd->flags = MFI_FRAME_DIR_NONE;
2502 dcmd->timeout = 0;
2503 dcmd->data_xfer_len = 0;
2504 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
2505 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
2506
2507 megasas_issue_blocked_cmd(instance, cmd);
2508
2509 megasas_return_cmd(instance, cmd);
2510
2511 return;
2512}
2513
2514/**
2515 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2516 * @instance: Adapter soft state
2517 */
2518static void megasas_shutdown_controller(struct megasas_instance *instance)
2519{
2520 struct megasas_cmd *cmd;
2521 struct megasas_dcmd_frame *dcmd;
2522
2523 cmd = megasas_get_cmd(instance);
2524
2525 if (!cmd)
2526 return;
2527
2528 if (instance->aen_cmd)
2529 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
2530
2531 dcmd = &cmd->frame->dcmd;
2532
2533 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2534
2535 dcmd->cmd = MFI_CMD_DCMD;
2536 dcmd->cmd_status = 0x0;
2537 dcmd->sge_count = 0;
2538 dcmd->flags = MFI_FRAME_DIR_NONE;
2539 dcmd->timeout = 0;
2540 dcmd->data_xfer_len = 0;
2541 dcmd->opcode = MR_DCMD_CTRL_SHUTDOWN;
2542
2543 megasas_issue_blocked_cmd(instance, cmd);
2544
2545 megasas_return_cmd(instance, cmd);
2546
2547 return;
2548}
2549
2550/**
2551 * megasas_detach_one - PCI hot"un"plug entry point
2552 * @pdev: PCI device structure
2553 */
2554static void megasas_detach_one(struct pci_dev *pdev)
2555{
2556 int i;
2557 struct Scsi_Host *host;
2558 struct megasas_instance *instance;
2559
2560 instance = pci_get_drvdata(pdev);
2561 host = instance->host;
2562
2563 scsi_remove_host(instance->host);
2564 megasas_flush_cache(instance);
2565 megasas_shutdown_controller(instance);
Sumant Patro5d018ad2006-10-03 13:13:18 -07002566 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002567
2568 /*
2569 * Take the instance off the instance array. Note that we will not
2570 * decrement the max_index. We let this array be sparse array
2571 */
2572 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2573 if (megasas_mgmt_info.instance[i] == instance) {
2574 megasas_mgmt_info.count--;
2575 megasas_mgmt_info.instance[i] = NULL;
2576
2577 break;
2578 }
2579 }
2580
2581 pci_set_drvdata(instance->pdev, NULL);
2582
Sumant Patrob274cab2006-10-03 12:52:12 -07002583 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002584
2585 free_irq(instance->pdev->irq, instance);
2586
2587 megasas_release_mfi(instance);
2588
2589 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2590 instance->evt_detail, instance->evt_detail_h);
2591
2592 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2593 instance->producer_h);
2594
2595 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2596 instance->consumer_h);
2597
2598 scsi_host_put(host);
2599
2600 pci_set_drvdata(pdev, NULL);
2601
2602 pci_disable_device(pdev);
2603
2604 return;
2605}
2606
2607/**
2608 * megasas_shutdown - Shutdown entry point
2609 * @device: Generic device structure
2610 */
2611static void megasas_shutdown(struct pci_dev *pdev)
2612{
2613 struct megasas_instance *instance = pci_get_drvdata(pdev);
2614 megasas_flush_cache(instance);
2615}
2616
2617/**
2618 * megasas_mgmt_open - char node "open" entry point
2619 */
2620static int megasas_mgmt_open(struct inode *inode, struct file *filep)
2621{
2622 /*
2623 * Allow only those users with admin rights
2624 */
2625 if (!capable(CAP_SYS_ADMIN))
2626 return -EACCES;
2627
2628 return 0;
2629}
2630
2631/**
2632 * megasas_mgmt_release - char node "release" entry point
2633 */
2634static int megasas_mgmt_release(struct inode *inode, struct file *filep)
2635{
2636 filep->private_data = NULL;
2637 fasync_helper(-1, filep, 0, &megasas_async_queue);
2638
2639 return 0;
2640}
2641
2642/**
2643 * megasas_mgmt_fasync - Async notifier registration from applications
2644 *
2645 * This function adds the calling process to a driver global queue. When an
2646 * event occurs, SIGIO will be sent to all processes in this queue.
2647 */
2648static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
2649{
2650 int rc;
2651
Arjan van de Ven0b950672006-01-11 13:16:10 +01002652 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002653
2654 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
2655
Arjan van de Ven0b950672006-01-11 13:16:10 +01002656 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002657
2658 if (rc >= 0) {
2659 /* For sanity check when we get ioctl */
2660 filep->private_data = filep;
2661 return 0;
2662 }
2663
2664 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
2665
2666 return rc;
2667}
2668
2669/**
2670 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
2671 * @instance: Adapter soft state
2672 * @argp: User's ioctl packet
2673 */
2674static int
2675megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
2676 struct megasas_iocpacket __user * user_ioc,
2677 struct megasas_iocpacket *ioc)
2678{
2679 struct megasas_sge32 *kern_sge32;
2680 struct megasas_cmd *cmd;
2681 void *kbuff_arr[MAX_IOCTL_SGE];
2682 dma_addr_t buf_handle = 0;
2683 int error = 0, i;
2684 void *sense = NULL;
2685 dma_addr_t sense_handle;
2686 u32 *sense_ptr;
2687
2688 memset(kbuff_arr, 0, sizeof(kbuff_arr));
2689
2690 if (ioc->sge_count > MAX_IOCTL_SGE) {
2691 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
2692 ioc->sge_count, MAX_IOCTL_SGE);
2693 return -EINVAL;
2694 }
2695
2696 cmd = megasas_get_cmd(instance);
2697 if (!cmd) {
2698 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
2699 return -ENOMEM;
2700 }
2701
2702 /*
2703 * User's IOCTL packet has 2 frames (maximum). Copy those two
2704 * frames into our cmd's frames. cmd->frame's context will get
2705 * overwritten when we copy from user's frames. So set that value
2706 * alone separately
2707 */
2708 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
2709 cmd->frame->hdr.context = cmd->index;
2710
2711 /*
2712 * The management interface between applications and the fw uses
2713 * MFI frames. E.g, RAID configuration changes, LD property changes
2714 * etc are accomplishes through different kinds of MFI frames. The
2715 * driver needs to care only about substituting user buffers with
2716 * kernel buffers in SGLs. The location of SGL is embedded in the
2717 * struct iocpacket itself.
2718 */
2719 kern_sge32 = (struct megasas_sge32 *)
2720 ((unsigned long)cmd->frame + ioc->sgl_off);
2721
2722 /*
2723 * For each user buffer, create a mirror buffer and copy in
2724 */
2725 for (i = 0; i < ioc->sge_count; i++) {
Sumant Patro9f35fa8a2007-02-14 12:55:45 -08002726 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002727 ioc->sgl[i].iov_len,
Sumant Patro9f35fa8a2007-02-14 12:55:45 -08002728 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002729 if (!kbuff_arr[i]) {
2730 printk(KERN_DEBUG "megasas: Failed to alloc "
2731 "kernel SGL buffer for IOCTL \n");
2732 error = -ENOMEM;
2733 goto out;
2734 }
2735
2736 /*
2737 * We don't change the dma_coherent_mask, so
2738 * pci_alloc_consistent only returns 32bit addresses
2739 */
2740 kern_sge32[i].phys_addr = (u32) buf_handle;
2741 kern_sge32[i].length = ioc->sgl[i].iov_len;
2742
2743 /*
2744 * We created a kernel buffer corresponding to the
2745 * user buffer. Now copy in from the user buffer
2746 */
2747 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
2748 (u32) (ioc->sgl[i].iov_len))) {
2749 error = -EFAULT;
2750 goto out;
2751 }
2752 }
2753
2754 if (ioc->sense_len) {
Sumant Patro9f35fa8a2007-02-14 12:55:45 -08002755 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
2756 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002757 if (!sense) {
2758 error = -ENOMEM;
2759 goto out;
2760 }
2761
2762 sense_ptr =
2763 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
2764 *sense_ptr = sense_handle;
2765 }
2766
2767 /*
2768 * Set the sync_cmd flag so that the ISR knows not to complete this
2769 * cmd to the SCSI mid-layer
2770 */
2771 cmd->sync_cmd = 1;
2772 megasas_issue_blocked_cmd(instance, cmd);
2773 cmd->sync_cmd = 0;
2774
2775 /*
2776 * copy out the kernel buffers to user buffers
2777 */
2778 for (i = 0; i < ioc->sge_count; i++) {
2779 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
2780 ioc->sgl[i].iov_len)) {
2781 error = -EFAULT;
2782 goto out;
2783 }
2784 }
2785
2786 /*
2787 * copy out the sense
2788 */
2789 if (ioc->sense_len) {
2790 /*
2791 * sense_ptr points to the location that has the user
2792 * sense buffer address
2793 */
2794 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
2795 ioc->sense_off);
2796
2797 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
2798 sense, ioc->sense_len)) {
2799 error = -EFAULT;
2800 goto out;
2801 }
2802 }
2803
2804 /*
2805 * copy the status codes returned by the fw
2806 */
2807 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
2808 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
2809 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
2810 error = -EFAULT;
2811 }
2812
2813 out:
2814 if (sense) {
Sumant Patro9f35fa8a2007-02-14 12:55:45 -08002815 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002816 sense, sense_handle);
2817 }
2818
2819 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
Sumant Patro9f35fa8a2007-02-14 12:55:45 -08002820 dma_free_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002821 kern_sge32[i].length,
2822 kbuff_arr[i], kern_sge32[i].phys_addr);
2823 }
2824
2825 megasas_return_cmd(instance, cmd);
2826 return error;
2827}
2828
2829static struct megasas_instance *megasas_lookup_instance(u16 host_no)
2830{
2831 int i;
2832
2833 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2834
2835 if ((megasas_mgmt_info.instance[i]) &&
2836 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
2837 return megasas_mgmt_info.instance[i];
2838 }
2839
2840 return NULL;
2841}
2842
2843static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
2844{
2845 struct megasas_iocpacket __user *user_ioc =
2846 (struct megasas_iocpacket __user *)arg;
2847 struct megasas_iocpacket *ioc;
2848 struct megasas_instance *instance;
2849 int error;
2850
2851 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
2852 if (!ioc)
2853 return -ENOMEM;
2854
2855 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
2856 error = -EFAULT;
2857 goto out_kfree_ioc;
2858 }
2859
2860 instance = megasas_lookup_instance(ioc->host_no);
2861 if (!instance) {
2862 error = -ENODEV;
2863 goto out_kfree_ioc;
2864 }
2865
2866 /*
2867 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
2868 */
2869 if (down_interruptible(&instance->ioctl_sem)) {
2870 error = -ERESTARTSYS;
2871 goto out_kfree_ioc;
2872 }
2873 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
2874 up(&instance->ioctl_sem);
2875
2876 out_kfree_ioc:
2877 kfree(ioc);
2878 return error;
2879}
2880
2881static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
2882{
2883 struct megasas_instance *instance;
2884 struct megasas_aen aen;
2885 int error;
2886
2887 if (file->private_data != file) {
2888 printk(KERN_DEBUG "megasas: fasync_helper was not "
2889 "called first\n");
2890 return -EINVAL;
2891 }
2892
2893 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
2894 return -EFAULT;
2895
2896 instance = megasas_lookup_instance(aen.host_no);
2897
2898 if (!instance)
2899 return -ENODEV;
2900
2901 down(&instance->aen_mutex);
2902 error = megasas_register_aen(instance, aen.seq_num,
2903 aen.class_locale_word);
2904 up(&instance->aen_mutex);
2905 return error;
2906}
2907
2908/**
2909 * megasas_mgmt_ioctl - char node ioctl entry point
2910 */
2911static long
2912megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2913{
2914 switch (cmd) {
2915 case MEGASAS_IOC_FIRMWARE:
2916 return megasas_mgmt_ioctl_fw(file, arg);
2917
2918 case MEGASAS_IOC_GET_AEN:
2919 return megasas_mgmt_ioctl_aen(file, arg);
2920 }
2921
2922 return -ENOTTY;
2923}
2924
2925#ifdef CONFIG_COMPAT
2926static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
2927{
2928 struct compat_megasas_iocpacket __user *cioc =
2929 (struct compat_megasas_iocpacket __user *)arg;
2930 struct megasas_iocpacket __user *ioc =
2931 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
2932 int i;
2933 int error = 0;
2934
Jeff Garzik83aabc12006-10-04 06:34:03 -04002935 if (clear_user(ioc, sizeof(*ioc)))
2936 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002937
2938 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
2939 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
2940 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
2941 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
2942 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
2943 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
2944 return -EFAULT;
2945
2946 for (i = 0; i < MAX_IOCTL_SGE; i++) {
2947 compat_uptr_t ptr;
2948
2949 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
2950 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
2951 copy_in_user(&ioc->sgl[i].iov_len,
2952 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
2953 return -EFAULT;
2954 }
2955
2956 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
2957
2958 if (copy_in_user(&cioc->frame.hdr.cmd_status,
2959 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
2960 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
2961 return -EFAULT;
2962 }
2963 return error;
2964}
2965
2966static long
2967megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
2968 unsigned long arg)
2969{
2970 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08002971 case MEGASAS_IOC_FIRMWARE32:
2972 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002973 case MEGASAS_IOC_GET_AEN:
2974 return megasas_mgmt_ioctl_aen(file, arg);
2975 }
2976
2977 return -ENOTTY;
2978}
2979#endif
2980
2981/*
2982 * File operations structure for management interface
2983 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002984static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002985 .owner = THIS_MODULE,
2986 .open = megasas_mgmt_open,
2987 .release = megasas_mgmt_release,
2988 .fasync = megasas_mgmt_fasync,
2989 .unlocked_ioctl = megasas_mgmt_ioctl,
2990#ifdef CONFIG_COMPAT
2991 .compat_ioctl = megasas_mgmt_compat_ioctl,
2992#endif
2993};
2994
2995/*
2996 * PCI hotplug support registration structure
2997 */
2998static struct pci_driver megasas_pci_driver = {
2999
3000 .name = "megaraid_sas",
3001 .id_table = megasas_pci_table,
3002 .probe = megasas_probe_one,
3003 .remove = __devexit_p(megasas_detach_one),
3004 .shutdown = megasas_shutdown,
3005};
3006
3007/*
3008 * Sysfs driver attributes
3009 */
3010static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
3011{
3012 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
3013 MEGASAS_VERSION);
3014}
3015
3016static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
3017
3018static ssize_t
3019megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
3020{
3021 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
3022 MEGASAS_RELDATE);
3023}
3024
3025static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
3026 NULL);
3027
Sumant Patro658dced2006-10-03 13:09:14 -07003028static ssize_t
3029megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
3030{
3031 return sprintf(buf,"%u",megasas_dbg_lvl);
3032}
3033
3034static ssize_t
3035megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
3036{
3037 int retval = count;
3038 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
3039 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
3040 retval = -EINVAL;
3041 }
3042 return retval;
3043}
3044
3045static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUGO, megasas_sysfs_show_dbg_lvl,
3046 megasas_sysfs_set_dbg_lvl);
3047
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003048/**
3049 * megasas_init - Driver load entry point
3050 */
3051static int __init megasas_init(void)
3052{
3053 int rval;
3054
3055 /*
3056 * Announce driver version and other information
3057 */
3058 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
3059 MEGASAS_EXT_VERSION);
3060
3061 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
3062
3063 /*
3064 * Register character device node
3065 */
3066 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
3067
3068 if (rval < 0) {
3069 printk(KERN_DEBUG "megasas: failed to open device node\n");
3070 return rval;
3071 }
3072
3073 megasas_mgmt_majorno = rval;
3074
3075 /*
3076 * Register ourselves as PCI hotplug module
3077 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00003078 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003079
3080 if (rval) {
3081 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04003082 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003083 }
3084
Jeff Garzik83aabc12006-10-04 06:34:03 -04003085 rval = driver_create_file(&megasas_pci_driver.driver,
3086 &driver_attr_version);
3087 if (rval)
3088 goto err_dcf_attr_ver;
3089 rval = driver_create_file(&megasas_pci_driver.driver,
3090 &driver_attr_release_date);
3091 if (rval)
3092 goto err_dcf_rel_date;
3093 rval = driver_create_file(&megasas_pci_driver.driver,
3094 &driver_attr_dbg_lvl);
3095 if (rval)
3096 goto err_dcf_dbg_lvl;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003097
3098 return rval;
Jeff Garzik83aabc12006-10-04 06:34:03 -04003099err_dcf_dbg_lvl:
3100 driver_remove_file(&megasas_pci_driver.driver,
3101 &driver_attr_release_date);
3102err_dcf_rel_date:
3103 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3104err_dcf_attr_ver:
3105 pci_unregister_driver(&megasas_pci_driver);
3106err_pcidrv:
3107 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3108 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003109}
3110
3111/**
3112 * megasas_exit - Driver unload entry point
3113 */
3114static void __exit megasas_exit(void)
3115{
Sumant Patro658dced2006-10-03 13:09:14 -07003116 driver_remove_file(&megasas_pci_driver.driver,
3117 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04003118 driver_remove_file(&megasas_pci_driver.driver,
3119 &driver_attr_release_date);
3120 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003121
3122 pci_unregister_driver(&megasas_pci_driver);
3123 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3124}
3125
3126module_init(megasas_init);
3127module_exit(megasas_exit);