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