blob: 7de267e14458ba0bafaa1138c54537796c269bff [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
Joshua Giles122da302006-02-03 15:34:17 -080013 * Version : v00.00.02.04
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040014 *
15 * Authors:
16 * Sreenivas Bagalkote <Sreenivas.Bagalkote@lsil.com>
17 * Sumant Patro <Sumant.Patro@lsil.com>
18 *
19 * List of supported controllers
20 *
21 * OEM Product Name VID DID SSVID SSID
22 * --- ------------ --- --- ---- ----
23 */
24
25#include <linux/kernel.h>
26#include <linux/types.h>
27#include <linux/pci.h>
28#include <linux/list.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040029#include <linux/moduleparam.h>
30#include <linux/module.h>
31#include <linux/spinlock.h>
32#include <linux/interrupt.h>
33#include <linux/delay.h>
34#include <linux/uio.h>
35#include <asm/uaccess.h>
Al Viro43399232005-10-04 17:36:04 +010036#include <linux/fs.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040037#include <linux/compat.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010038#include <linux/mutex.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040039
40#include <scsi/scsi.h>
41#include <scsi/scsi_cmnd.h>
42#include <scsi/scsi_device.h>
43#include <scsi/scsi_host.h>
44#include "megaraid_sas.h"
45
46MODULE_LICENSE("GPL");
47MODULE_VERSION(MEGASAS_VERSION);
48MODULE_AUTHOR("sreenivas.bagalkote@lsil.com");
49MODULE_DESCRIPTION("LSI Logic MegaRAID SAS Driver");
50
51/*
52 * PCI ID table for all supported controllers
53 */
54static struct pci_device_id megasas_pci_table[] = {
55
56 {
57 PCI_VENDOR_ID_LSI_LOGIC,
Sumant Patro1341c932006-01-25 12:02:40 -080058 PCI_DEVICE_ID_LSI_SAS1064R, // xscale IOP
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040059 PCI_ANY_ID,
60 PCI_ANY_ID,
61 },
62 {
Sumant Patrof9876f02006-02-03 15:34:35 -080063 PCI_VENDOR_ID_LSI_LOGIC,
64 PCI_DEVICE_ID_LSI_SAS1078R, // ppc IOP
65 PCI_ANY_ID,
66 PCI_ANY_ID,
67 },
68 {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040069 PCI_VENDOR_ID_DELL,
Sumant Patro1341c932006-01-25 12:02:40 -080070 PCI_DEVICE_ID_DELL_PERC5, // xscale IOP
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040071 PCI_ANY_ID,
72 PCI_ANY_ID,
73 },
74 {0} /* Terminating entry */
75};
76
77MODULE_DEVICE_TABLE(pci, megasas_pci_table);
78
79static int megasas_mgmt_majorno;
80static struct megasas_mgmt_info megasas_mgmt_info;
81static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +010082static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040083
84/**
85 * megasas_get_cmd - Get a command from the free pool
86 * @instance: Adapter soft state
87 *
88 * Returns a free command from the pool
89 */
Arjan van de Ven858119e2006-01-14 13:20:43 -080090static struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040091 *instance)
92{
93 unsigned long flags;
94 struct megasas_cmd *cmd = NULL;
95
96 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
97
98 if (!list_empty(&instance->cmd_pool)) {
99 cmd = list_entry((&instance->cmd_pool)->next,
100 struct megasas_cmd, list);
101 list_del_init(&cmd->list);
102 } else {
103 printk(KERN_ERR "megasas: Command pool empty!\n");
104 }
105
106 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
107 return cmd;
108}
109
110/**
111 * megasas_return_cmd - Return a cmd to free command pool
112 * @instance: Adapter soft state
113 * @cmd: Command packet to be returned to free command pool
114 */
115static inline void
116megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
117{
118 unsigned long flags;
119
120 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
121
122 cmd->scmd = NULL;
123 list_add_tail(&cmd->list, &instance->cmd_pool);
124
125 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
126}
127
Sumant Patro1341c932006-01-25 12:02:40 -0800128
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400129/**
Sumant Patro1341c932006-01-25 12:02:40 -0800130* The following functions are defined for xscale
131* (deviceid : 1064R, PERC5) controllers
132*/
133
134/**
135 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400136 * @regs: MFI register set
137 */
138static inline void
Sumant Patro1341c932006-01-25 12:02:40 -0800139megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400140{
141 writel(1, &(regs)->outbound_intr_mask);
142
143 /* Dummy readl to force pci flush */
144 readl(&regs->outbound_intr_mask);
145}
146
147/**
Sumant Patro1341c932006-01-25 12:02:40 -0800148 * megasas_read_fw_status_reg_xscale - returns the current FW status value
149 * @regs: MFI register set
150 */
151static u32
152megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
153{
154 return readl(&(regs)->outbound_msg_0);
155}
156/**
157 * megasas_clear_interrupt_xscale - Check & clear interrupt
158 * @regs: MFI register set
159 */
160static int
161megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
162{
163 u32 status;
164 /*
165 * Check if it is our interrupt
166 */
167 status = readl(&regs->outbound_intr_status);
168
169 if (!(status & MFI_OB_INTR_STATUS_MASK)) {
170 return 1;
171 }
172
173 /*
174 * Clear the interrupt by writing back the same value
175 */
176 writel(status, &regs->outbound_intr_status);
177
178 return 0;
179}
180
181/**
182 * megasas_fire_cmd_xscale - Sends command to the FW
183 * @frame_phys_addr : Physical address of cmd
184 * @frame_count : Number of frames for the command
185 * @regs : MFI register set
186 */
187static inline void
188megasas_fire_cmd_xscale(dma_addr_t frame_phys_addr,u32 frame_count, struct megasas_register_set __iomem *regs)
189{
190 writel((frame_phys_addr >> 3)|(frame_count),
191 &(regs)->inbound_queue_port);
192}
193
194static struct megasas_instance_template megasas_instance_template_xscale = {
195
196 .fire_cmd = megasas_fire_cmd_xscale,
197 .enable_intr = megasas_enable_intr_xscale,
198 .clear_intr = megasas_clear_intr_xscale,
199 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
200};
201
202/**
203* This is the end of set of functions & definitions specific
204* to xscale (deviceid : 1064R, PERC5) controllers
205*/
206
207/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800208* The following functions are defined for ppc (deviceid : 0x60)
209* controllers
210*/
211
212/**
213 * megasas_enable_intr_ppc - Enables interrupts
214 * @regs: MFI register set
215 */
216static inline void
217megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
218{
219 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
220
221 writel(~0x80000004, &(regs)->outbound_intr_mask);
222
223 /* Dummy readl to force pci flush */
224 readl(&regs->outbound_intr_mask);
225}
226
227/**
228 * megasas_read_fw_status_reg_ppc - returns the current FW status value
229 * @regs: MFI register set
230 */
231static u32
232megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
233{
234 return readl(&(regs)->outbound_scratch_pad);
235}
236
237/**
238 * megasas_clear_interrupt_ppc - Check & clear interrupt
239 * @regs: MFI register set
240 */
241static int
242megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
243{
244 u32 status;
245 /*
246 * Check if it is our interrupt
247 */
248 status = readl(&regs->outbound_intr_status);
249
250 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
251 return 1;
252 }
253
254 /*
255 * Clear the interrupt by writing back the same value
256 */
257 writel(status, &regs->outbound_doorbell_clear);
258
259 return 0;
260}
261/**
262 * megasas_fire_cmd_ppc - Sends command to the FW
263 * @frame_phys_addr : Physical address of cmd
264 * @frame_count : Number of frames for the command
265 * @regs : MFI register set
266 */
267static inline void
268megasas_fire_cmd_ppc(dma_addr_t frame_phys_addr, u32 frame_count, struct megasas_register_set __iomem *regs)
269{
270 writel((frame_phys_addr | (frame_count<<1))|1,
271 &(regs)->inbound_queue_port);
272}
273
274static struct megasas_instance_template megasas_instance_template_ppc = {
275
276 .fire_cmd = megasas_fire_cmd_ppc,
277 .enable_intr = megasas_enable_intr_ppc,
278 .clear_intr = megasas_clear_intr_ppc,
279 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
280};
281
282/**
283* This is the end of set of functions & definitions
284* specific to ppc (deviceid : 0x60) controllers
285*/
286
287/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400288 * megasas_disable_intr - Disables interrupts
289 * @regs: MFI register set
290 */
291static inline void
292megasas_disable_intr(struct megasas_register_set __iomem * regs)
293{
Sumant Patro1341c932006-01-25 12:02:40 -0800294 u32 mask = 0x1f;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400295 writel(mask, &regs->outbound_intr_mask);
296
297 /* Dummy readl to force pci flush */
298 readl(&regs->outbound_intr_mask);
299}
300
301/**
302 * megasas_issue_polled - Issues a polling command
303 * @instance: Adapter soft state
304 * @cmd: Command packet to be issued
305 *
306 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
307 */
308static int
309megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
310{
311 int i;
312 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
313
314 struct megasas_header *frame_hdr = &cmd->frame->hdr;
315
316 frame_hdr->cmd_status = 0xFF;
317 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
318
319 /*
320 * Issue the frame using inbound queue port
321 */
Sumant Patro1341c932006-01-25 12:02:40 -0800322 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400323
324 /*
325 * Wait for cmd_status to change
326 */
327 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
328 rmb();
329 msleep(1);
330 }
331
332 if (frame_hdr->cmd_status == 0xff)
333 return -ETIME;
334
335 return 0;
336}
337
338/**
339 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
340 * @instance: Adapter soft state
341 * @cmd: Command to be issued
342 *
343 * This function waits on an event for the command to be returned from ISR.
344 * Used to issue ioctl commands.
345 */
346static int
347megasas_issue_blocked_cmd(struct megasas_instance *instance,
348 struct megasas_cmd *cmd)
349{
350 cmd->cmd_status = ENODATA;
351
Sumant Patro1341c932006-01-25 12:02:40 -0800352 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400353
354 wait_event(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA));
355
356 return 0;
357}
358
359/**
360 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
361 * @instance: Adapter soft state
362 * @cmd_to_abort: Previously issued cmd to be aborted
363 *
364 * MFI firmware can abort previously issued AEN comamnd (automatic event
365 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
366 * cmd and blocks till it is completed.
367 */
368static int
369megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
370 struct megasas_cmd *cmd_to_abort)
371{
372 struct megasas_cmd *cmd;
373 struct megasas_abort_frame *abort_fr;
374
375 cmd = megasas_get_cmd(instance);
376
377 if (!cmd)
378 return -1;
379
380 abort_fr = &cmd->frame->abort;
381
382 /*
383 * Prepare and issue the abort frame
384 */
385 abort_fr->cmd = MFI_CMD_ABORT;
386 abort_fr->cmd_status = 0xFF;
387 abort_fr->flags = 0;
388 abort_fr->abort_context = cmd_to_abort->index;
389 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
390 abort_fr->abort_mfi_phys_addr_hi = 0;
391
392 cmd->sync_cmd = 1;
393 cmd->cmd_status = 0xFF;
394
Sumant Patro1341c932006-01-25 12:02:40 -0800395 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400396
397 /*
398 * Wait for this cmd to complete
399 */
400 wait_event(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF));
401
402 megasas_return_cmd(instance, cmd);
403 return 0;
404}
405
406/**
407 * megasas_make_sgl32 - Prepares 32-bit SGL
408 * @instance: Adapter soft state
409 * @scp: SCSI command from the mid-layer
410 * @mfi_sgl: SGL to be filled in
411 *
412 * If successful, this function returns the number of SG elements. Otherwise,
413 * it returnes -1.
414 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800415static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400416megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
417 union megasas_sgl *mfi_sgl)
418{
419 int i;
420 int sge_count;
421 struct scatterlist *os_sgl;
422
423 /*
424 * Return 0 if there is no data transfer
425 */
426 if (!scp->request_buffer || !scp->request_bufflen)
427 return 0;
428
429 if (!scp->use_sg) {
430 mfi_sgl->sge32[0].phys_addr = pci_map_single(instance->pdev,
431 scp->
432 request_buffer,
433 scp->
434 request_bufflen,
435 scp->
436 sc_data_direction);
437 mfi_sgl->sge32[0].length = scp->request_bufflen;
438
439 return 1;
440 }
441
442 os_sgl = (struct scatterlist *)scp->request_buffer;
443 sge_count = pci_map_sg(instance->pdev, os_sgl, scp->use_sg,
444 scp->sc_data_direction);
445
446 for (i = 0; i < sge_count; i++, os_sgl++) {
447 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
448 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
449 }
450
451 return sge_count;
452}
453
454/**
455 * megasas_make_sgl64 - Prepares 64-bit SGL
456 * @instance: Adapter soft state
457 * @scp: SCSI command from the mid-layer
458 * @mfi_sgl: SGL to be filled in
459 *
460 * If successful, this function returns the number of SG elements. Otherwise,
461 * it returnes -1.
462 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800463static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400464megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
465 union megasas_sgl *mfi_sgl)
466{
467 int i;
468 int sge_count;
469 struct scatterlist *os_sgl;
470
471 /*
472 * Return 0 if there is no data transfer
473 */
474 if (!scp->request_buffer || !scp->request_bufflen)
475 return 0;
476
477 if (!scp->use_sg) {
478 mfi_sgl->sge64[0].phys_addr = pci_map_single(instance->pdev,
479 scp->
480 request_buffer,
481 scp->
482 request_bufflen,
483 scp->
484 sc_data_direction);
485
486 mfi_sgl->sge64[0].length = scp->request_bufflen;
487
488 return 1;
489 }
490
491 os_sgl = (struct scatterlist *)scp->request_buffer;
492 sge_count = pci_map_sg(instance->pdev, os_sgl, scp->use_sg,
493 scp->sc_data_direction);
494
495 for (i = 0; i < sge_count; i++, os_sgl++) {
496 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
497 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
498 }
499
500 return sge_count;
501}
502
503/**
504 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
505 * @instance: Adapter soft state
506 * @scp: SCSI command
507 * @cmd: Command to be prepared in
508 *
509 * This function prepares CDB commands. These are typcially pass-through
510 * commands to the devices.
511 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800512static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400513megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
514 struct megasas_cmd *cmd)
515{
516 u32 sge_sz;
517 int sge_bytes;
518 u32 is_logical;
519 u32 device_id;
520 u16 flags = 0;
521 struct megasas_pthru_frame *pthru;
522
523 is_logical = MEGASAS_IS_LOGICAL(scp);
524 device_id = MEGASAS_DEV_INDEX(instance, scp);
525 pthru = (struct megasas_pthru_frame *)cmd->frame;
526
527 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
528 flags = MFI_FRAME_DIR_WRITE;
529 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
530 flags = MFI_FRAME_DIR_READ;
531 else if (scp->sc_data_direction == PCI_DMA_NONE)
532 flags = MFI_FRAME_DIR_NONE;
533
534 /*
535 * Prepare the DCDB frame
536 */
537 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
538 pthru->cmd_status = 0x0;
539 pthru->scsi_status = 0x0;
540 pthru->target_id = device_id;
541 pthru->lun = scp->device->lun;
542 pthru->cdb_len = scp->cmd_len;
543 pthru->timeout = 0;
544 pthru->flags = flags;
545 pthru->data_xfer_len = scp->request_bufflen;
546
547 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
548
549 /*
550 * Construct SGL
551 */
552 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
553 sizeof(struct megasas_sge32);
554
555 if (IS_DMA64) {
556 pthru->flags |= MFI_FRAME_SGL64;
557 pthru->sge_count = megasas_make_sgl64(instance, scp,
558 &pthru->sgl);
559 } else
560 pthru->sge_count = megasas_make_sgl32(instance, scp,
561 &pthru->sgl);
562
563 /*
564 * Sense info specific
565 */
566 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
567 pthru->sense_buf_phys_addr_hi = 0;
568 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
569
570 sge_bytes = sge_sz * pthru->sge_count;
571
572 /*
573 * Compute the total number of frames this command consumes. FW uses
574 * this number to pull sufficient number of frames from host memory.
575 */
576 cmd->frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
577 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) + 1;
578
579 if (cmd->frame_count > 7)
580 cmd->frame_count = 8;
581
582 return cmd->frame_count;
583}
584
585/**
586 * megasas_build_ldio - Prepares IOs to logical devices
587 * @instance: Adapter soft state
588 * @scp: SCSI command
589 * @cmd: Command to to be prepared
590 *
591 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
592 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800593static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400594megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
595 struct megasas_cmd *cmd)
596{
597 u32 sge_sz;
598 int sge_bytes;
599 u32 device_id;
600 u8 sc = scp->cmnd[0];
601 u16 flags = 0;
602 struct megasas_io_frame *ldio;
603
604 device_id = MEGASAS_DEV_INDEX(instance, scp);
605 ldio = (struct megasas_io_frame *)cmd->frame;
606
607 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
608 flags = MFI_FRAME_DIR_WRITE;
609 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
610 flags = MFI_FRAME_DIR_READ;
611
612 /*
613 * Preare the Logical IO frame: 2nd bit is zero for all read cmds
614 */
615 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
616 ldio->cmd_status = 0x0;
617 ldio->scsi_status = 0x0;
618 ldio->target_id = device_id;
619 ldio->timeout = 0;
620 ldio->reserved_0 = 0;
621 ldio->pad_0 = 0;
622 ldio->flags = flags;
623 ldio->start_lba_hi = 0;
624 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
625
626 /*
627 * 6-byte READ(0x08) or WRITE(0x0A) cdb
628 */
629 if (scp->cmd_len == 6) {
630 ldio->lba_count = (u32) scp->cmnd[4];
631 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
632 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
633
634 ldio->start_lba_lo &= 0x1FFFFF;
635 }
636
637 /*
638 * 10-byte READ(0x28) or WRITE(0x2A) cdb
639 */
640 else if (scp->cmd_len == 10) {
641 ldio->lba_count = (u32) scp->cmnd[8] |
642 ((u32) scp->cmnd[7] << 8);
643 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
644 ((u32) scp->cmnd[3] << 16) |
645 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
646 }
647
648 /*
649 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
650 */
651 else if (scp->cmd_len == 12) {
652 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
653 ((u32) scp->cmnd[7] << 16) |
654 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
655
656 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
657 ((u32) scp->cmnd[3] << 16) |
658 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
659 }
660
661 /*
662 * 16-byte READ(0x88) or WRITE(0x8A) cdb
663 */
664 else if (scp->cmd_len == 16) {
665 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
666 ((u32) scp->cmnd[11] << 16) |
667 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
668
669 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
670 ((u32) scp->cmnd[7] << 16) |
671 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
672
673 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
674 ((u32) scp->cmnd[3] << 16) |
675 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
676
677 }
678
679 /*
680 * Construct SGL
681 */
682 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
683 sizeof(struct megasas_sge32);
684
685 if (IS_DMA64) {
686 ldio->flags |= MFI_FRAME_SGL64;
687 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
688 } else
689 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
690
691 /*
692 * Sense info specific
693 */
694 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
695 ldio->sense_buf_phys_addr_hi = 0;
696 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
697
698 sge_bytes = sge_sz * ldio->sge_count;
699
700 cmd->frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
701 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) + 1;
702
703 if (cmd->frame_count > 7)
704 cmd->frame_count = 8;
705
706 return cmd->frame_count;
707}
708
709/**
Sumant Patrocb59aa62006-01-25 11:53:25 -0800710 * megasas_is_ldio - Checks if the cmd is for logical drive
711 * @scmd: SCSI command
712 *
713 * Called by megasas_queue_command to find out if the command to be queued
714 * is a logical drive command
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400715 */
Sumant Patrocb59aa62006-01-25 11:53:25 -0800716static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400717{
Sumant Patrocb59aa62006-01-25 11:53:25 -0800718 if (!MEGASAS_IS_LOGICAL(cmd))
719 return 0;
720 switch (cmd->cmnd[0]) {
721 case READ_10:
722 case WRITE_10:
723 case READ_12:
724 case WRITE_12:
725 case READ_6:
726 case WRITE_6:
727 case READ_16:
728 case WRITE_16:
729 return 1;
730 default:
731 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400732 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400733}
734
735/**
736 * megasas_queue_command - Queue entry point
737 * @scmd: SCSI command to be queued
738 * @done: Callback entry point
739 */
740static int
741megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
742{
743 u32 frame_count;
744 unsigned long flags;
745 struct megasas_cmd *cmd;
746 struct megasas_instance *instance;
747
748 instance = (struct megasas_instance *)
749 scmd->device->host->hostdata;
750 scmd->scsi_done = done;
751 scmd->result = 0;
752
Sumant Patrocb59aa62006-01-25 11:53:25 -0800753 if (MEGASAS_IS_LOGICAL(scmd) &&
754 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
755 scmd->result = DID_BAD_TARGET << 16;
756 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400757 }
758
Sumant Patrocb59aa62006-01-25 11:53:25 -0800759 cmd = megasas_get_cmd(instance);
760 if (!cmd)
761 return SCSI_MLQUEUE_HOST_BUSY;
762
763 /*
764 * Logical drive command
765 */
766 if (megasas_is_ldio(scmd))
767 frame_count = megasas_build_ldio(instance, scmd, cmd);
768 else
769 frame_count = megasas_build_dcdb(instance, scmd, cmd);
770
771 if (!frame_count)
772 goto out_return_cmd;
773
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400774 cmd->scmd = scmd;
775 scmd->SCp.ptr = (char *)cmd;
776 scmd->SCp.sent_command = jiffies;
777
778 /*
779 * Issue the command to the FW
780 */
781 spin_lock_irqsave(&instance->instance_lock, flags);
782 instance->fw_outstanding++;
783 spin_unlock_irqrestore(&instance->instance_lock, flags);
784
Sumant Patro1341c932006-01-25 12:02:40 -0800785 instance->instancet->fire_cmd(cmd->frame_phys_addr ,cmd->frame_count-1,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400786
787 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -0800788
789 out_return_cmd:
790 megasas_return_cmd(instance, cmd);
791 out_done:
792 done(scmd);
793 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400794}
795
796/**
797 * megasas_wait_for_outstanding - Wait for all outstanding cmds
798 * @instance: Adapter soft state
799 *
800 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
801 * complete all its outstanding commands. Returns error if one or more IOs
802 * are pending after this time period. It also marks the controller dead.
803 */
804static int megasas_wait_for_outstanding(struct megasas_instance *instance)
805{
806 int i;
807 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
808
809 for (i = 0; i < wait_time; i++) {
810
811 if (!instance->fw_outstanding)
812 break;
813
814 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
815 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
816 "commands to complete\n", i,
817 instance->fw_outstanding);
818 }
819
820 msleep(1000);
821 }
822
823 if (instance->fw_outstanding) {
824 instance->hw_crit_error = 1;
825 return FAILED;
826 }
827
828 return SUCCESS;
829}
830
831/**
832 * megasas_generic_reset - Generic reset routine
833 * @scmd: Mid-layer SCSI command
834 *
835 * This routine implements a generic reset handler for device, bus and host
836 * reset requests. Device, bus and host specific reset handlers can use this
837 * function after they do their specific tasks.
838 */
839static int megasas_generic_reset(struct scsi_cmnd *scmd)
840{
841 int ret_val;
842 struct megasas_instance *instance;
843
844 instance = (struct megasas_instance *)scmd->device->host->hostdata;
845
Jeff Garzik017560f2005-10-24 18:04:36 -0400846 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x\n",
847 scmd->serial_number, scmd->cmnd[0]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400848
849 if (instance->hw_crit_error) {
850 printk(KERN_ERR "megasas: cannot recover from previous reset "
851 "failures\n");
852 return FAILED;
853 }
854
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400855 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400856 if (ret_val == SUCCESS)
857 printk(KERN_NOTICE "megasas: reset successful \n");
858 else
859 printk(KERN_ERR "megasas: failed to do reset\n");
860
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400861 return ret_val;
862}
863
864static enum scsi_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
865{
866 unsigned long seconds;
867
868 if (scmd->SCp.ptr) {
869 seconds = (jiffies - scmd->SCp.sent_command) / HZ;
870
871 if (seconds < 90) {
872 return EH_RESET_TIMER;
873 } else {
874 return EH_NOT_HANDLED;
875 }
876 }
877
878 return EH_HANDLED;
879}
880
881/**
882 * megasas_reset_device - Device reset handler entry point
883 */
884static int megasas_reset_device(struct scsi_cmnd *scmd)
885{
886 int ret;
887
888 /*
889 * First wait for all commands to complete
890 */
891 ret = megasas_generic_reset(scmd);
892
893 return ret;
894}
895
896/**
897 * megasas_reset_bus_host - Bus & host reset handler entry point
898 */
899static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
900{
901 int ret;
902
903 /*
904 * Frist wait for all commands to complete
905 */
906 ret = megasas_generic_reset(scmd);
907
908 return ret;
909}
910
911/**
912 * megasas_service_aen - Processes an event notification
913 * @instance: Adapter soft state
914 * @cmd: AEN command completed by the ISR
915 *
916 * For AEN, driver sends a command down to FW that is held by the FW till an
917 * event occurs. When an event of interest occurs, FW completes the command
918 * that it was previously holding.
919 *
920 * This routines sends SIGIO signal to processes that have registered with the
921 * driver for AEN.
922 */
923static void
924megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
925{
926 /*
927 * Don't signal app if it is just an aborted previously registered aen
928 */
929 if (!cmd->abort_aen)
930 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
931 else
932 cmd->abort_aen = 0;
933
934 instance->aen_cmd = NULL;
935 megasas_return_cmd(instance, cmd);
936}
937
938/*
939 * Scsi host template for megaraid_sas driver
940 */
941static struct scsi_host_template megasas_template = {
942
943 .module = THIS_MODULE,
944 .name = "LSI Logic SAS based MegaRAID driver",
945 .proc_name = "megaraid_sas",
946 .queuecommand = megasas_queue_command,
947 .eh_device_reset_handler = megasas_reset_device,
948 .eh_bus_reset_handler = megasas_reset_bus_host,
949 .eh_host_reset_handler = megasas_reset_bus_host,
950 .eh_timed_out = megasas_reset_timer,
951 .use_clustering = ENABLE_CLUSTERING,
952};
953
954/**
955 * megasas_complete_int_cmd - Completes an internal command
956 * @instance: Adapter soft state
957 * @cmd: Command to be completed
958 *
959 * The megasas_issue_blocked_cmd() function waits for a command to complete
960 * after it issues a command. This function wakes up that waiting routine by
961 * calling wake_up() on the wait queue.
962 */
963static void
964megasas_complete_int_cmd(struct megasas_instance *instance,
965 struct megasas_cmd *cmd)
966{
967 cmd->cmd_status = cmd->frame->io.cmd_status;
968
969 if (cmd->cmd_status == ENODATA) {
970 cmd->cmd_status = 0;
971 }
972 wake_up(&instance->int_cmd_wait_q);
973}
974
975/**
976 * megasas_complete_abort - Completes aborting a command
977 * @instance: Adapter soft state
978 * @cmd: Cmd that was issued to abort another cmd
979 *
980 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
981 * after it issues an abort on a previously issued command. This function
982 * wakes up all functions waiting on the same wait queue.
983 */
984static void
985megasas_complete_abort(struct megasas_instance *instance,
986 struct megasas_cmd *cmd)
987{
988 if (cmd->sync_cmd) {
989 cmd->sync_cmd = 0;
990 cmd->cmd_status = 0;
991 wake_up(&instance->abort_cmd_wait_q);
992 }
993
994 return;
995}
996
997/**
998 * megasas_unmap_sgbuf - Unmap SG buffers
999 * @instance: Adapter soft state
1000 * @cmd: Completed command
1001 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001002static void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001003megasas_unmap_sgbuf(struct megasas_instance *instance, struct megasas_cmd *cmd)
1004{
1005 dma_addr_t buf_h;
1006 u8 opcode;
1007
1008 if (cmd->scmd->use_sg) {
1009 pci_unmap_sg(instance->pdev, cmd->scmd->request_buffer,
1010 cmd->scmd->use_sg, cmd->scmd->sc_data_direction);
1011 return;
1012 }
1013
1014 if (!cmd->scmd->request_bufflen)
1015 return;
1016
1017 opcode = cmd->frame->hdr.cmd;
1018
1019 if ((opcode == MFI_CMD_LD_READ) || (opcode == MFI_CMD_LD_WRITE)) {
1020 if (IS_DMA64)
1021 buf_h = cmd->frame->io.sgl.sge64[0].phys_addr;
1022 else
1023 buf_h = cmd->frame->io.sgl.sge32[0].phys_addr;
1024 } else {
1025 if (IS_DMA64)
1026 buf_h = cmd->frame->pthru.sgl.sge64[0].phys_addr;
1027 else
1028 buf_h = cmd->frame->pthru.sgl.sge32[0].phys_addr;
1029 }
1030
1031 pci_unmap_single(instance->pdev, buf_h, cmd->scmd->request_bufflen,
1032 cmd->scmd->sc_data_direction);
1033 return;
1034}
1035
1036/**
1037 * megasas_complete_cmd - Completes a command
1038 * @instance: Adapter soft state
1039 * @cmd: Command to be completed
1040 * @alt_status: If non-zero, use this value as status to
1041 * SCSI mid-layer instead of the value returned
1042 * by the FW. This should be used if caller wants
1043 * an alternate status (as in the case of aborted
1044 * commands)
1045 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001046static void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001047megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
1048 u8 alt_status)
1049{
1050 int exception = 0;
1051 struct megasas_header *hdr = &cmd->frame->hdr;
1052 unsigned long flags;
1053
1054 if (cmd->scmd) {
1055 cmd->scmd->SCp.ptr = (char *)0;
1056 }
1057
1058 switch (hdr->cmd) {
1059
1060 case MFI_CMD_PD_SCSI_IO:
1061 case MFI_CMD_LD_SCSI_IO:
1062
1063 /*
1064 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1065 * issued either through an IO path or an IOCTL path. If it
1066 * was via IOCTL, we will send it to internal completion.
1067 */
1068 if (cmd->sync_cmd) {
1069 cmd->sync_cmd = 0;
1070 megasas_complete_int_cmd(instance, cmd);
1071 break;
1072 }
1073
1074 /*
1075 * Don't export physical disk devices to mid-layer.
1076 */
1077 if (!MEGASAS_IS_LOGICAL(cmd->scmd) &&
1078 (hdr->cmd_status == MFI_STAT_OK) &&
1079 (cmd->scmd->cmnd[0] == INQUIRY)) {
1080
1081 if (((*(u8 *) cmd->scmd->request_buffer) & 0x1F) ==
1082 TYPE_DISK) {
1083 cmd->scmd->result = DID_BAD_TARGET << 16;
1084 exception = 1;
1085 }
1086 }
1087
1088 case MFI_CMD_LD_READ:
1089 case MFI_CMD_LD_WRITE:
1090
1091 if (alt_status) {
1092 cmd->scmd->result = alt_status << 16;
1093 exception = 1;
1094 }
1095
1096 if (exception) {
1097
1098 spin_lock_irqsave(&instance->instance_lock, flags);
1099 instance->fw_outstanding--;
1100 spin_unlock_irqrestore(&instance->instance_lock, flags);
1101
1102 megasas_unmap_sgbuf(instance, cmd);
1103 cmd->scmd->scsi_done(cmd->scmd);
1104 megasas_return_cmd(instance, cmd);
1105
1106 break;
1107 }
1108
1109 switch (hdr->cmd_status) {
1110
1111 case MFI_STAT_OK:
1112 cmd->scmd->result = DID_OK << 16;
1113 break;
1114
1115 case MFI_STAT_SCSI_IO_FAILED:
1116 case MFI_STAT_LD_INIT_IN_PROGRESS:
1117 cmd->scmd->result =
1118 (DID_ERROR << 16) | hdr->scsi_status;
1119 break;
1120
1121 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1122
1123 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1124
1125 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1126 memset(cmd->scmd->sense_buffer, 0,
1127 SCSI_SENSE_BUFFERSIZE);
1128 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1129 hdr->sense_len);
1130
1131 cmd->scmd->result |= DRIVER_SENSE << 24;
1132 }
1133
1134 break;
1135
1136 case MFI_STAT_LD_OFFLINE:
1137 case MFI_STAT_DEVICE_NOT_FOUND:
1138 cmd->scmd->result = DID_BAD_TARGET << 16;
1139 break;
1140
1141 default:
1142 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1143 hdr->cmd_status);
1144 cmd->scmd->result = DID_ERROR << 16;
1145 break;
1146 }
1147
1148 spin_lock_irqsave(&instance->instance_lock, flags);
1149 instance->fw_outstanding--;
1150 spin_unlock_irqrestore(&instance->instance_lock, flags);
1151
1152 megasas_unmap_sgbuf(instance, cmd);
1153 cmd->scmd->scsi_done(cmd->scmd);
1154 megasas_return_cmd(instance, cmd);
1155
1156 break;
1157
1158 case MFI_CMD_SMP:
1159 case MFI_CMD_STP:
1160 case MFI_CMD_DCMD:
1161
1162 /*
1163 * See if got an event notification
1164 */
1165 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1166 megasas_service_aen(instance, cmd);
1167 else
1168 megasas_complete_int_cmd(instance, cmd);
1169
1170 break;
1171
1172 case MFI_CMD_ABORT:
1173 /*
1174 * Cmd issued to abort another cmd returned
1175 */
1176 megasas_complete_abort(instance, cmd);
1177 break;
1178
1179 default:
1180 printk("megasas: Unknown command completed! [0x%X]\n",
1181 hdr->cmd);
1182 break;
1183 }
1184}
1185
1186/**
1187 * megasas_deplete_reply_queue - Processes all completed commands
1188 * @instance: Adapter soft state
1189 * @alt_status: Alternate status to be returned to
1190 * SCSI mid-layer instead of the status
1191 * returned by the FW
1192 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001193static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001194megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1195{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001196 u32 producer;
1197 u32 consumer;
1198 u32 context;
1199 struct megasas_cmd *cmd;
1200
1201 /*
1202 * Check if it is our interrupt
Sumant Patro1341c932006-01-25 12:02:40 -08001203 * Clear the interrupt
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001204 */
Sumant Patro1341c932006-01-25 12:02:40 -08001205 if(instance->instancet->clear_intr(instance->reg_set))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001206 return IRQ_NONE;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001207
1208 producer = *instance->producer;
1209 consumer = *instance->consumer;
1210
1211 while (consumer != producer) {
1212 context = instance->reply_queue[consumer];
1213
1214 cmd = instance->cmd_list[context];
1215
1216 megasas_complete_cmd(instance, cmd, alt_status);
1217
1218 consumer++;
1219 if (consumer == (instance->max_fw_cmds + 1)) {
1220 consumer = 0;
1221 }
1222 }
1223
1224 *instance->consumer = producer;
1225
1226 return IRQ_HANDLED;
1227}
1228
1229/**
1230 * megasas_isr - isr entry point
1231 */
1232static irqreturn_t megasas_isr(int irq, void *devp, struct pt_regs *regs)
1233{
1234 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1235 DID_OK);
1236}
1237
1238/**
1239 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08001240 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001241 *
1242 * During the initialization, FW passes can potentially be in any one of
1243 * several possible states. If the FW in operational, waiting-for-handshake
1244 * states, driver must take steps to bring it to ready state. Otherwise, it
1245 * has to wait for the ready state.
1246 */
1247static int
Sumant Patro1341c932006-01-25 12:02:40 -08001248megasas_transition_to_ready(struct megasas_instance* instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001249{
1250 int i;
1251 u8 max_wait;
1252 u32 fw_state;
1253 u32 cur_state;
1254
Sumant Patro1341c932006-01-25 12:02:40 -08001255 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001256
1257 while (fw_state != MFI_STATE_READY) {
1258
1259 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1260 " state\n");
1261 switch (fw_state) {
1262
1263 case MFI_STATE_FAULT:
1264
1265 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1266 return -ENODEV;
1267
1268 case MFI_STATE_WAIT_HANDSHAKE:
1269 /*
1270 * Set the CLR bit in inbound doorbell
1271 */
1272 writel(MFI_INIT_CLEAR_HANDSHAKE,
Sumant Patro1341c932006-01-25 12:02:40 -08001273 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001274
1275 max_wait = 2;
1276 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1277 break;
1278
1279 case MFI_STATE_OPERATIONAL:
1280 /*
1281 * Bring it to READY state; assuming max wait 2 secs
1282 */
Sumant Patro1341c932006-01-25 12:02:40 -08001283 megasas_disable_intr(instance->reg_set);
1284 writel(MFI_INIT_READY, &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001285
1286 max_wait = 10;
1287 cur_state = MFI_STATE_OPERATIONAL;
1288 break;
1289
1290 case MFI_STATE_UNDEFINED:
1291 /*
1292 * This state should not last for more than 2 seconds
1293 */
1294 max_wait = 2;
1295 cur_state = MFI_STATE_UNDEFINED;
1296 break;
1297
1298 case MFI_STATE_BB_INIT:
1299 max_wait = 2;
1300 cur_state = MFI_STATE_BB_INIT;
1301 break;
1302
1303 case MFI_STATE_FW_INIT:
1304 max_wait = 20;
1305 cur_state = MFI_STATE_FW_INIT;
1306 break;
1307
1308 case MFI_STATE_FW_INIT_2:
1309 max_wait = 20;
1310 cur_state = MFI_STATE_FW_INIT_2;
1311 break;
1312
1313 case MFI_STATE_DEVICE_SCAN:
1314 max_wait = 20;
1315 cur_state = MFI_STATE_DEVICE_SCAN;
1316 break;
1317
1318 case MFI_STATE_FLUSH_CACHE:
1319 max_wait = 20;
1320 cur_state = MFI_STATE_FLUSH_CACHE;
1321 break;
1322
1323 default:
1324 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1325 fw_state);
1326 return -ENODEV;
1327 }
1328
1329 /*
1330 * The cur_state should not last for more than max_wait secs
1331 */
1332 for (i = 0; i < (max_wait * 1000); i++) {
Sumant Patro1341c932006-01-25 12:02:40 -08001333 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1334 MFI_STATE_MASK ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001335
1336 if (fw_state == cur_state) {
1337 msleep(1);
1338 } else
1339 break;
1340 }
1341
1342 /*
1343 * Return error if fw_state hasn't changed after max_wait
1344 */
1345 if (fw_state == cur_state) {
1346 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1347 "in %d secs\n", fw_state, max_wait);
1348 return -ENODEV;
1349 }
1350 };
1351
1352 return 0;
1353}
1354
1355/**
1356 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1357 * @instance: Adapter soft state
1358 */
1359static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1360{
1361 int i;
1362 u32 max_cmd = instance->max_fw_cmds;
1363 struct megasas_cmd *cmd;
1364
1365 if (!instance->frame_dma_pool)
1366 return;
1367
1368 /*
1369 * Return all frames to pool
1370 */
1371 for (i = 0; i < max_cmd; i++) {
1372
1373 cmd = instance->cmd_list[i];
1374
1375 if (cmd->frame)
1376 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1377 cmd->frame_phys_addr);
1378
1379 if (cmd->sense)
1380 pci_pool_free(instance->sense_dma_pool, cmd->frame,
1381 cmd->sense_phys_addr);
1382 }
1383
1384 /*
1385 * Now destroy the pool itself
1386 */
1387 pci_pool_destroy(instance->frame_dma_pool);
1388 pci_pool_destroy(instance->sense_dma_pool);
1389
1390 instance->frame_dma_pool = NULL;
1391 instance->sense_dma_pool = NULL;
1392}
1393
1394/**
1395 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1396 * @instance: Adapter soft state
1397 *
1398 * Each command packet has an embedded DMA memory buffer that is used for
1399 * filling MFI frame and the SG list that immediately follows the frame. This
1400 * function creates those DMA memory buffers for each command packet by using
1401 * PCI pool facility.
1402 */
1403static int megasas_create_frame_pool(struct megasas_instance *instance)
1404{
1405 int i;
1406 u32 max_cmd;
1407 u32 sge_sz;
1408 u32 sgl_sz;
1409 u32 total_sz;
1410 u32 frame_count;
1411 struct megasas_cmd *cmd;
1412
1413 max_cmd = instance->max_fw_cmds;
1414
1415 /*
1416 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1417 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1418 */
1419 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1420 sizeof(struct megasas_sge32);
1421
1422 /*
1423 * Calculated the number of 64byte frames required for SGL
1424 */
1425 sgl_sz = sge_sz * instance->max_num_sge;
1426 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1427
1428 /*
1429 * We need one extra frame for the MFI command
1430 */
1431 frame_count++;
1432
1433 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1434 /*
1435 * Use DMA pool facility provided by PCI layer
1436 */
1437 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1438 instance->pdev, total_sz, 64,
1439 0);
1440
1441 if (!instance->frame_dma_pool) {
1442 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1443 return -ENOMEM;
1444 }
1445
1446 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1447 instance->pdev, 128, 4, 0);
1448
1449 if (!instance->sense_dma_pool) {
1450 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1451
1452 pci_pool_destroy(instance->frame_dma_pool);
1453 instance->frame_dma_pool = NULL;
1454
1455 return -ENOMEM;
1456 }
1457
1458 /*
1459 * Allocate and attach a frame to each of the commands in cmd_list.
1460 * By making cmd->index as the context instead of the &cmd, we can
1461 * always use 32bit context regardless of the architecture
1462 */
1463 for (i = 0; i < max_cmd; i++) {
1464
1465 cmd = instance->cmd_list[i];
1466
1467 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1468 GFP_KERNEL, &cmd->frame_phys_addr);
1469
1470 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1471 GFP_KERNEL, &cmd->sense_phys_addr);
1472
1473 /*
1474 * megasas_teardown_frame_pool() takes care of freeing
1475 * whatever has been allocated
1476 */
1477 if (!cmd->frame || !cmd->sense) {
1478 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1479 megasas_teardown_frame_pool(instance);
1480 return -ENOMEM;
1481 }
1482
1483 cmd->frame->io.context = cmd->index;
1484 }
1485
1486 return 0;
1487}
1488
1489/**
1490 * megasas_free_cmds - Free all the cmds in the free cmd pool
1491 * @instance: Adapter soft state
1492 */
1493static void megasas_free_cmds(struct megasas_instance *instance)
1494{
1495 int i;
1496 /* First free the MFI frame pool */
1497 megasas_teardown_frame_pool(instance);
1498
1499 /* Free all the commands in the cmd_list */
1500 for (i = 0; i < instance->max_fw_cmds; i++)
1501 kfree(instance->cmd_list[i]);
1502
1503 /* Free the cmd_list buffer itself */
1504 kfree(instance->cmd_list);
1505 instance->cmd_list = NULL;
1506
1507 INIT_LIST_HEAD(&instance->cmd_pool);
1508}
1509
1510/**
1511 * megasas_alloc_cmds - Allocates the command packets
1512 * @instance: Adapter soft state
1513 *
1514 * Each command that is issued to the FW, whether IO commands from the OS or
1515 * internal commands like IOCTLs, are wrapped in local data structure called
1516 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1517 * the FW.
1518 *
1519 * Each frame has a 32-bit field called context (tag). This context is used
1520 * to get back the megasas_cmd from the frame when a frame gets completed in
1521 * the ISR. Typically the address of the megasas_cmd itself would be used as
1522 * the context. But we wanted to keep the differences between 32 and 64 bit
1523 * systems to the mininum. We always use 32 bit integers for the context. In
1524 * this driver, the 32 bit values are the indices into an array cmd_list.
1525 * This array is used only to look up the megasas_cmd given the context. The
1526 * free commands themselves are maintained in a linked list called cmd_pool.
1527 */
1528static int megasas_alloc_cmds(struct megasas_instance *instance)
1529{
1530 int i;
1531 int j;
1532 u32 max_cmd;
1533 struct megasas_cmd *cmd;
1534
1535 max_cmd = instance->max_fw_cmds;
1536
1537 /*
1538 * instance->cmd_list is an array of struct megasas_cmd pointers.
1539 * Allocate the dynamic array first and then allocate individual
1540 * commands.
1541 */
1542 instance->cmd_list = kmalloc(sizeof(struct megasas_cmd *) * max_cmd,
1543 GFP_KERNEL);
1544
1545 if (!instance->cmd_list) {
1546 printk(KERN_DEBUG "megasas: out of memory\n");
1547 return -ENOMEM;
1548 }
1549
1550 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) * max_cmd);
1551
1552 for (i = 0; i < max_cmd; i++) {
1553 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
1554 GFP_KERNEL);
1555
1556 if (!instance->cmd_list[i]) {
1557
1558 for (j = 0; j < i; j++)
1559 kfree(instance->cmd_list[j]);
1560
1561 kfree(instance->cmd_list);
1562 instance->cmd_list = NULL;
1563
1564 return -ENOMEM;
1565 }
1566 }
1567
1568 /*
1569 * Add all the commands to command pool (instance->cmd_pool)
1570 */
1571 for (i = 0; i < max_cmd; i++) {
1572 cmd = instance->cmd_list[i];
1573 memset(cmd, 0, sizeof(struct megasas_cmd));
1574 cmd->index = i;
1575 cmd->instance = instance;
1576
1577 list_add_tail(&cmd->list, &instance->cmd_pool);
1578 }
1579
1580 /*
1581 * Create a frame pool and assign one frame to each cmd
1582 */
1583 if (megasas_create_frame_pool(instance)) {
1584 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
1585 megasas_free_cmds(instance);
1586 }
1587
1588 return 0;
1589}
1590
1591/**
1592 * megasas_get_controller_info - Returns FW's controller structure
1593 * @instance: Adapter soft state
1594 * @ctrl_info: Controller information structure
1595 *
1596 * Issues an internal command (DCMD) to get the FW's controller structure.
1597 * This information is mainly used to find out the maximum IO transfer per
1598 * command supported by the FW.
1599 */
1600static int
1601megasas_get_ctrl_info(struct megasas_instance *instance,
1602 struct megasas_ctrl_info *ctrl_info)
1603{
1604 int ret = 0;
1605 struct megasas_cmd *cmd;
1606 struct megasas_dcmd_frame *dcmd;
1607 struct megasas_ctrl_info *ci;
1608 dma_addr_t ci_h = 0;
1609
1610 cmd = megasas_get_cmd(instance);
1611
1612 if (!cmd) {
1613 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
1614 return -ENOMEM;
1615 }
1616
1617 dcmd = &cmd->frame->dcmd;
1618
1619 ci = pci_alloc_consistent(instance->pdev,
1620 sizeof(struct megasas_ctrl_info), &ci_h);
1621
1622 if (!ci) {
1623 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
1624 megasas_return_cmd(instance, cmd);
1625 return -ENOMEM;
1626 }
1627
1628 memset(ci, 0, sizeof(*ci));
1629 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1630
1631 dcmd->cmd = MFI_CMD_DCMD;
1632 dcmd->cmd_status = 0xFF;
1633 dcmd->sge_count = 1;
1634 dcmd->flags = MFI_FRAME_DIR_READ;
1635 dcmd->timeout = 0;
1636 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
1637 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
1638 dcmd->sgl.sge32[0].phys_addr = ci_h;
1639 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
1640
1641 if (!megasas_issue_polled(instance, cmd)) {
1642 ret = 0;
1643 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
1644 } else {
1645 ret = -1;
1646 }
1647
1648 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
1649 ci, ci_h);
1650
1651 megasas_return_cmd(instance, cmd);
1652 return ret;
1653}
1654
1655/**
1656 * megasas_init_mfi - Initializes the FW
1657 * @instance: Adapter soft state
1658 *
1659 * This is the main function for initializing MFI firmware.
1660 */
1661static int megasas_init_mfi(struct megasas_instance *instance)
1662{
1663 u32 context_sz;
1664 u32 reply_q_sz;
1665 u32 max_sectors_1;
1666 u32 max_sectors_2;
1667 struct megasas_register_set __iomem *reg_set;
1668
1669 struct megasas_cmd *cmd;
1670 struct megasas_ctrl_info *ctrl_info;
1671
1672 struct megasas_init_frame *init_frame;
1673 struct megasas_init_queue_info *initq_info;
1674 dma_addr_t init_frame_h;
1675 dma_addr_t initq_info_h;
1676
1677 /*
1678 * Map the message registers
1679 */
1680 instance->base_addr = pci_resource_start(instance->pdev, 0);
1681
1682 if (pci_request_regions(instance->pdev, "megasas: LSI Logic")) {
1683 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
1684 return -EBUSY;
1685 }
1686
1687 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
1688
1689 if (!instance->reg_set) {
1690 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
1691 goto fail_ioremap;
1692 }
1693
1694 reg_set = instance->reg_set;
1695
Sumant Patrof9876f02006-02-03 15:34:35 -08001696 switch(instance->pdev->device)
1697 {
1698 case PCI_DEVICE_ID_LSI_SAS1078R:
1699 instance->instancet = &megasas_instance_template_ppc;
1700 break;
1701 case PCI_DEVICE_ID_LSI_SAS1064R:
1702 case PCI_DEVICE_ID_DELL_PERC5:
1703 default:
1704 instance->instancet = &megasas_instance_template_xscale;
1705 break;
1706 }
Sumant Patro1341c932006-01-25 12:02:40 -08001707
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001708 /*
1709 * We expect the FW state to be READY
1710 */
Sumant Patro1341c932006-01-25 12:02:40 -08001711 if (megasas_transition_to_ready(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001712 goto fail_ready_state;
1713
1714 /*
1715 * Get various operational parameters from status register
1716 */
Sumant Patro1341c932006-01-25 12:02:40 -08001717 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
1718 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
1719 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001720 /*
1721 * Create a pool of commands
1722 */
1723 if (megasas_alloc_cmds(instance))
1724 goto fail_alloc_cmds;
1725
1726 /*
1727 * Allocate memory for reply queue. Length of reply queue should
1728 * be _one_ more than the maximum commands handled by the firmware.
1729 *
1730 * Note: When FW completes commands, it places corresponding contex
1731 * values in this circular reply queue. This circular queue is a fairly
1732 * typical producer-consumer queue. FW is the producer (of completed
1733 * commands) and the driver is the consumer.
1734 */
1735 context_sz = sizeof(u32);
1736 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
1737
1738 instance->reply_queue = pci_alloc_consistent(instance->pdev,
1739 reply_q_sz,
1740 &instance->reply_queue_h);
1741
1742 if (!instance->reply_queue) {
1743 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
1744 goto fail_reply_queue;
1745 }
1746
1747 /*
1748 * Prepare a init frame. Note the init frame points to queue info
1749 * structure. Each frame has SGL allocated after first 64 bytes. For
1750 * this frame - since we don't need any SGL - we use SGL's space as
1751 * queue info structure
1752 *
1753 * We will not get a NULL command below. We just created the pool.
1754 */
1755 cmd = megasas_get_cmd(instance);
1756
1757 init_frame = (struct megasas_init_frame *)cmd->frame;
1758 initq_info = (struct megasas_init_queue_info *)
1759 ((unsigned long)init_frame + 64);
1760
1761 init_frame_h = cmd->frame_phys_addr;
1762 initq_info_h = init_frame_h + 64;
1763
1764 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
1765 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
1766
1767 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
1768 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
1769
1770 initq_info->producer_index_phys_addr_lo = instance->producer_h;
1771 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
1772
1773 init_frame->cmd = MFI_CMD_INIT;
1774 init_frame->cmd_status = 0xFF;
1775 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
1776
1777 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
1778
1779 /*
1780 * Issue the init frame in polled mode
1781 */
1782 if (megasas_issue_polled(instance, cmd)) {
1783 printk(KERN_DEBUG "megasas: Failed to init firmware\n");
1784 goto fail_fw_init;
1785 }
1786
1787 megasas_return_cmd(instance, cmd);
1788
1789 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
1790
1791 /*
1792 * Compute the max allowed sectors per IO: The controller info has two
1793 * limits on max sectors. Driver should use the minimum of these two.
1794 *
1795 * 1 << stripe_sz_ops.min = max sectors per strip
1796 *
1797 * Note that older firmwares ( < FW ver 30) didn't report information
1798 * to calculate max_sectors_1. So the number ended up as zero always.
1799 */
1800 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
1801
1802 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
1803 ctrl_info->max_strips_per_io;
1804 max_sectors_2 = ctrl_info->max_request_size;
1805
1806 instance->max_sectors_per_req = (max_sectors_1 < max_sectors_2)
1807 ? max_sectors_1 : max_sectors_2;
1808 } else
1809 instance->max_sectors_per_req = instance->max_num_sge *
1810 PAGE_SIZE / 512;
1811
1812 kfree(ctrl_info);
1813
1814 return 0;
1815
1816 fail_fw_init:
1817 megasas_return_cmd(instance, cmd);
1818
1819 pci_free_consistent(instance->pdev, reply_q_sz,
1820 instance->reply_queue, instance->reply_queue_h);
1821 fail_reply_queue:
1822 megasas_free_cmds(instance);
1823
1824 fail_alloc_cmds:
1825 fail_ready_state:
1826 iounmap(instance->reg_set);
1827
1828 fail_ioremap:
1829 pci_release_regions(instance->pdev);
1830
1831 return -EINVAL;
1832}
1833
1834/**
1835 * megasas_release_mfi - Reverses the FW initialization
1836 * @intance: Adapter soft state
1837 */
1838static void megasas_release_mfi(struct megasas_instance *instance)
1839{
1840 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
1841
1842 pci_free_consistent(instance->pdev, reply_q_sz,
1843 instance->reply_queue, instance->reply_queue_h);
1844
1845 megasas_free_cmds(instance);
1846
1847 iounmap(instance->reg_set);
1848
1849 pci_release_regions(instance->pdev);
1850}
1851
1852/**
1853 * megasas_get_seq_num - Gets latest event sequence numbers
1854 * @instance: Adapter soft state
1855 * @eli: FW event log sequence numbers information
1856 *
1857 * FW maintains a log of all events in a non-volatile area. Upper layers would
1858 * usually find out the latest sequence number of the events, the seq number at
1859 * the boot etc. They would "read" all the events below the latest seq number
1860 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
1861 * number), they would subsribe to AEN (asynchronous event notification) and
1862 * wait for the events to happen.
1863 */
1864static int
1865megasas_get_seq_num(struct megasas_instance *instance,
1866 struct megasas_evt_log_info *eli)
1867{
1868 struct megasas_cmd *cmd;
1869 struct megasas_dcmd_frame *dcmd;
1870 struct megasas_evt_log_info *el_info;
1871 dma_addr_t el_info_h = 0;
1872
1873 cmd = megasas_get_cmd(instance);
1874
1875 if (!cmd) {
1876 return -ENOMEM;
1877 }
1878
1879 dcmd = &cmd->frame->dcmd;
1880 el_info = pci_alloc_consistent(instance->pdev,
1881 sizeof(struct megasas_evt_log_info),
1882 &el_info_h);
1883
1884 if (!el_info) {
1885 megasas_return_cmd(instance, cmd);
1886 return -ENOMEM;
1887 }
1888
1889 memset(el_info, 0, sizeof(*el_info));
1890 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1891
1892 dcmd->cmd = MFI_CMD_DCMD;
1893 dcmd->cmd_status = 0x0;
1894 dcmd->sge_count = 1;
1895 dcmd->flags = MFI_FRAME_DIR_READ;
1896 dcmd->timeout = 0;
1897 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
1898 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
1899 dcmd->sgl.sge32[0].phys_addr = el_info_h;
1900 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
1901
1902 megasas_issue_blocked_cmd(instance, cmd);
1903
1904 /*
1905 * Copy the data back into callers buffer
1906 */
1907 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
1908
1909 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
1910 el_info, el_info_h);
1911
1912 megasas_return_cmd(instance, cmd);
1913
1914 return 0;
1915}
1916
1917/**
1918 * megasas_register_aen - Registers for asynchronous event notification
1919 * @instance: Adapter soft state
1920 * @seq_num: The starting sequence number
1921 * @class_locale: Class of the event
1922 *
1923 * This function subscribes for AEN for events beyond the @seq_num. It requests
1924 * to be notified if and only if the event is of type @class_locale
1925 */
1926static int
1927megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
1928 u32 class_locale_word)
1929{
1930 int ret_val;
1931 struct megasas_cmd *cmd;
1932 struct megasas_dcmd_frame *dcmd;
1933 union megasas_evt_class_locale curr_aen;
1934 union megasas_evt_class_locale prev_aen;
1935
1936 /*
1937 * If there an AEN pending already (aen_cmd), check if the
1938 * class_locale of that pending AEN is inclusive of the new
1939 * AEN request we currently have. If it is, then we don't have
1940 * to do anything. In other words, whichever events the current
1941 * AEN request is subscribing to, have already been subscribed
1942 * to.
1943 *
1944 * If the old_cmd is _not_ inclusive, then we have to abort
1945 * that command, form a class_locale that is superset of both
1946 * old and current and re-issue to the FW
1947 */
1948
1949 curr_aen.word = class_locale_word;
1950
1951 if (instance->aen_cmd) {
1952
1953 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
1954
1955 /*
1956 * A class whose enum value is smaller is inclusive of all
1957 * higher values. If a PROGRESS (= -1) was previously
1958 * registered, then a new registration requests for higher
1959 * classes need not be sent to FW. They are automatically
1960 * included.
1961 *
1962 * Locale numbers don't have such hierarchy. They are bitmap
1963 * values
1964 */
1965 if ((prev_aen.members.class <= curr_aen.members.class) &&
1966 !((prev_aen.members.locale & curr_aen.members.locale) ^
1967 curr_aen.members.locale)) {
1968 /*
1969 * Previously issued event registration includes
1970 * current request. Nothing to do.
1971 */
1972 return 0;
1973 } else {
1974 curr_aen.members.locale |= prev_aen.members.locale;
1975
1976 if (prev_aen.members.class < curr_aen.members.class)
1977 curr_aen.members.class = prev_aen.members.class;
1978
1979 instance->aen_cmd->abort_aen = 1;
1980 ret_val = megasas_issue_blocked_abort_cmd(instance,
1981 instance->
1982 aen_cmd);
1983
1984 if (ret_val) {
1985 printk(KERN_DEBUG "megasas: Failed to abort "
1986 "previous AEN command\n");
1987 return ret_val;
1988 }
1989 }
1990 }
1991
1992 cmd = megasas_get_cmd(instance);
1993
1994 if (!cmd)
1995 return -ENOMEM;
1996
1997 dcmd = &cmd->frame->dcmd;
1998
1999 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
2000
2001 /*
2002 * Prepare DCMD for aen registration
2003 */
2004 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2005
2006 dcmd->cmd = MFI_CMD_DCMD;
2007 dcmd->cmd_status = 0x0;
2008 dcmd->sge_count = 1;
2009 dcmd->flags = MFI_FRAME_DIR_READ;
2010 dcmd->timeout = 0;
2011 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
2012 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
2013 dcmd->mbox.w[0] = seq_num;
2014 dcmd->mbox.w[1] = curr_aen.word;
2015 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
2016 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
2017
2018 /*
2019 * Store reference to the cmd used to register for AEN. When an
2020 * application wants us to register for AEN, we have to abort this
2021 * cmd and re-register with a new EVENT LOCALE supplied by that app
2022 */
2023 instance->aen_cmd = cmd;
2024
2025 /*
2026 * Issue the aen registration frame
2027 */
Sumant Patro1341c932006-01-25 12:02:40 -08002028 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002029
2030 return 0;
2031}
2032
2033/**
2034 * megasas_start_aen - Subscribes to AEN during driver load time
2035 * @instance: Adapter soft state
2036 */
2037static int megasas_start_aen(struct megasas_instance *instance)
2038{
2039 struct megasas_evt_log_info eli;
2040 union megasas_evt_class_locale class_locale;
2041
2042 /*
2043 * Get the latest sequence number from FW
2044 */
2045 memset(&eli, 0, sizeof(eli));
2046
2047 if (megasas_get_seq_num(instance, &eli))
2048 return -1;
2049
2050 /*
2051 * Register AEN with FW for latest sequence number plus 1
2052 */
2053 class_locale.members.reserved = 0;
2054 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2055 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2056
2057 return megasas_register_aen(instance, eli.newest_seq_num + 1,
2058 class_locale.word);
2059}
2060
2061/**
2062 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2063 * @instance: Adapter soft state
2064 */
2065static int megasas_io_attach(struct megasas_instance *instance)
2066{
2067 struct Scsi_Host *host = instance->host;
2068
2069 /*
2070 * Export parameters required by SCSI mid-layer
2071 */
2072 host->irq = instance->pdev->irq;
2073 host->unique_id = instance->unique_id;
2074 host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
2075 host->this_id = instance->init_id;
2076 host->sg_tablesize = instance->max_num_sge;
2077 host->max_sectors = instance->max_sectors_per_req;
2078 host->cmd_per_lun = 128;
2079 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
2080 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
2081 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08002082 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002083
2084 /*
2085 * Notify the mid-layer about the new controller
2086 */
2087 if (scsi_add_host(host, &instance->pdev->dev)) {
2088 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2089 return -ENODEV;
2090 }
2091
2092 /*
2093 * Trigger SCSI to scan our drives
2094 */
2095 scsi_scan_host(host);
2096 return 0;
2097}
2098
2099/**
2100 * megasas_probe_one - PCI hotplug entry point
2101 * @pdev: PCI device structure
2102 * @id: PCI ids of supported hotplugged adapter
2103 */
2104static int __devinit
2105megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2106{
2107 int rval;
2108 struct Scsi_Host *host;
2109 struct megasas_instance *instance;
2110
2111 /*
2112 * Announce PCI information
2113 */
2114 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2115 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2116 pdev->subsystem_device);
2117
2118 printk("bus %d:slot %d:func %d\n",
2119 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2120
2121 /*
2122 * PCI prepping: enable device set bus mastering and dma mask
2123 */
2124 rval = pci_enable_device(pdev);
2125
2126 if (rval) {
2127 return rval;
2128 }
2129
2130 pci_set_master(pdev);
2131
2132 /*
2133 * All our contollers are capable of performing 64-bit DMA
2134 */
2135 if (IS_DMA64) {
2136 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK) != 0) {
2137
2138 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2139 goto fail_set_dma_mask;
2140 }
2141 } else {
2142 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2143 goto fail_set_dma_mask;
2144 }
2145
2146 host = scsi_host_alloc(&megasas_template,
2147 sizeof(struct megasas_instance));
2148
2149 if (!host) {
2150 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2151 goto fail_alloc_instance;
2152 }
2153
2154 instance = (struct megasas_instance *)host->hostdata;
2155 memset(instance, 0, sizeof(*instance));
2156
2157 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2158 &instance->producer_h);
2159 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2160 &instance->consumer_h);
2161
2162 if (!instance->producer || !instance->consumer) {
2163 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2164 "producer, consumer\n");
2165 goto fail_alloc_dma_buf;
2166 }
2167
2168 *instance->producer = 0;
2169 *instance->consumer = 0;
2170
2171 instance->evt_detail = pci_alloc_consistent(pdev,
2172 sizeof(struct
2173 megasas_evt_detail),
2174 &instance->evt_detail_h);
2175
2176 if (!instance->evt_detail) {
2177 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2178 "event detail structure\n");
2179 goto fail_alloc_dma_buf;
2180 }
2181
2182 /*
2183 * Initialize locks and queues
2184 */
2185 INIT_LIST_HEAD(&instance->cmd_pool);
2186
2187 init_waitqueue_head(&instance->int_cmd_wait_q);
2188 init_waitqueue_head(&instance->abort_cmd_wait_q);
2189
2190 spin_lock_init(&instance->cmd_pool_lock);
2191 spin_lock_init(&instance->instance_lock);
2192
2193 sema_init(&instance->aen_mutex, 1);
2194 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2195
2196 /*
2197 * Initialize PCI related and misc parameters
2198 */
2199 instance->pdev = pdev;
2200 instance->host = host;
2201 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2202 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2203
2204 /*
2205 * Initialize MFI Firmware
2206 */
2207 if (megasas_init_mfi(instance))
2208 goto fail_init_mfi;
2209
2210 /*
2211 * Register IRQ
2212 */
2213 if (request_irq(pdev->irq, megasas_isr, SA_SHIRQ, "megasas", instance)) {
2214 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2215 goto fail_irq;
2216 }
2217
Sumant Patro1341c932006-01-25 12:02:40 -08002218 instance->instancet->enable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002219
2220 /*
2221 * Store instance in PCI softstate
2222 */
2223 pci_set_drvdata(pdev, instance);
2224
2225 /*
2226 * Add this controller to megasas_mgmt_info structure so that it
2227 * can be exported to management applications
2228 */
2229 megasas_mgmt_info.count++;
2230 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2231 megasas_mgmt_info.max_index++;
2232
2233 /*
2234 * Initiate AEN (Asynchronous Event Notification)
2235 */
2236 if (megasas_start_aen(instance)) {
2237 printk(KERN_DEBUG "megasas: start aen failed\n");
2238 goto fail_start_aen;
2239 }
2240
2241 /*
2242 * Register with SCSI mid-layer
2243 */
2244 if (megasas_io_attach(instance))
2245 goto fail_io_attach;
2246
2247 return 0;
2248
2249 fail_start_aen:
2250 fail_io_attach:
2251 megasas_mgmt_info.count--;
2252 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2253 megasas_mgmt_info.max_index--;
2254
2255 pci_set_drvdata(pdev, NULL);
2256 megasas_disable_intr(instance->reg_set);
2257 free_irq(instance->pdev->irq, instance);
2258
2259 megasas_release_mfi(instance);
2260
2261 fail_irq:
2262 fail_init_mfi:
2263 fail_alloc_dma_buf:
2264 if (instance->evt_detail)
2265 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2266 instance->evt_detail,
2267 instance->evt_detail_h);
2268
2269 if (instance->producer)
2270 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2271 instance->producer_h);
2272 if (instance->consumer)
2273 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2274 instance->consumer_h);
2275 scsi_host_put(host);
2276
2277 fail_alloc_instance:
2278 fail_set_dma_mask:
2279 pci_disable_device(pdev);
2280
2281 return -ENODEV;
2282}
2283
2284/**
2285 * megasas_flush_cache - Requests FW to flush all its caches
2286 * @instance: Adapter soft state
2287 */
2288static void megasas_flush_cache(struct megasas_instance *instance)
2289{
2290 struct megasas_cmd *cmd;
2291 struct megasas_dcmd_frame *dcmd;
2292
2293 cmd = megasas_get_cmd(instance);
2294
2295 if (!cmd)
2296 return;
2297
2298 dcmd = &cmd->frame->dcmd;
2299
2300 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2301
2302 dcmd->cmd = MFI_CMD_DCMD;
2303 dcmd->cmd_status = 0x0;
2304 dcmd->sge_count = 0;
2305 dcmd->flags = MFI_FRAME_DIR_NONE;
2306 dcmd->timeout = 0;
2307 dcmd->data_xfer_len = 0;
2308 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
2309 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
2310
2311 megasas_issue_blocked_cmd(instance, cmd);
2312
2313 megasas_return_cmd(instance, cmd);
2314
2315 return;
2316}
2317
2318/**
2319 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2320 * @instance: Adapter soft state
2321 */
2322static void megasas_shutdown_controller(struct megasas_instance *instance)
2323{
2324 struct megasas_cmd *cmd;
2325 struct megasas_dcmd_frame *dcmd;
2326
2327 cmd = megasas_get_cmd(instance);
2328
2329 if (!cmd)
2330 return;
2331
2332 if (instance->aen_cmd)
2333 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
2334
2335 dcmd = &cmd->frame->dcmd;
2336
2337 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2338
2339 dcmd->cmd = MFI_CMD_DCMD;
2340 dcmd->cmd_status = 0x0;
2341 dcmd->sge_count = 0;
2342 dcmd->flags = MFI_FRAME_DIR_NONE;
2343 dcmd->timeout = 0;
2344 dcmd->data_xfer_len = 0;
2345 dcmd->opcode = MR_DCMD_CTRL_SHUTDOWN;
2346
2347 megasas_issue_blocked_cmd(instance, cmd);
2348
2349 megasas_return_cmd(instance, cmd);
2350
2351 return;
2352}
2353
2354/**
2355 * megasas_detach_one - PCI hot"un"plug entry point
2356 * @pdev: PCI device structure
2357 */
2358static void megasas_detach_one(struct pci_dev *pdev)
2359{
2360 int i;
2361 struct Scsi_Host *host;
2362 struct megasas_instance *instance;
2363
2364 instance = pci_get_drvdata(pdev);
2365 host = instance->host;
2366
2367 scsi_remove_host(instance->host);
2368 megasas_flush_cache(instance);
2369 megasas_shutdown_controller(instance);
2370
2371 /*
2372 * Take the instance off the instance array. Note that we will not
2373 * decrement the max_index. We let this array be sparse array
2374 */
2375 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2376 if (megasas_mgmt_info.instance[i] == instance) {
2377 megasas_mgmt_info.count--;
2378 megasas_mgmt_info.instance[i] = NULL;
2379
2380 break;
2381 }
2382 }
2383
2384 pci_set_drvdata(instance->pdev, NULL);
2385
2386 megasas_disable_intr(instance->reg_set);
2387
2388 free_irq(instance->pdev->irq, instance);
2389
2390 megasas_release_mfi(instance);
2391
2392 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2393 instance->evt_detail, instance->evt_detail_h);
2394
2395 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2396 instance->producer_h);
2397
2398 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2399 instance->consumer_h);
2400
2401 scsi_host_put(host);
2402
2403 pci_set_drvdata(pdev, NULL);
2404
2405 pci_disable_device(pdev);
2406
2407 return;
2408}
2409
2410/**
2411 * megasas_shutdown - Shutdown entry point
2412 * @device: Generic device structure
2413 */
2414static void megasas_shutdown(struct pci_dev *pdev)
2415{
2416 struct megasas_instance *instance = pci_get_drvdata(pdev);
2417 megasas_flush_cache(instance);
2418}
2419
2420/**
2421 * megasas_mgmt_open - char node "open" entry point
2422 */
2423static int megasas_mgmt_open(struct inode *inode, struct file *filep)
2424{
2425 /*
2426 * Allow only those users with admin rights
2427 */
2428 if (!capable(CAP_SYS_ADMIN))
2429 return -EACCES;
2430
2431 return 0;
2432}
2433
2434/**
2435 * megasas_mgmt_release - char node "release" entry point
2436 */
2437static int megasas_mgmt_release(struct inode *inode, struct file *filep)
2438{
2439 filep->private_data = NULL;
2440 fasync_helper(-1, filep, 0, &megasas_async_queue);
2441
2442 return 0;
2443}
2444
2445/**
2446 * megasas_mgmt_fasync - Async notifier registration from applications
2447 *
2448 * This function adds the calling process to a driver global queue. When an
2449 * event occurs, SIGIO will be sent to all processes in this queue.
2450 */
2451static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
2452{
2453 int rc;
2454
Arjan van de Ven0b950672006-01-11 13:16:10 +01002455 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002456
2457 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
2458
Arjan van de Ven0b950672006-01-11 13:16:10 +01002459 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002460
2461 if (rc >= 0) {
2462 /* For sanity check when we get ioctl */
2463 filep->private_data = filep;
2464 return 0;
2465 }
2466
2467 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
2468
2469 return rc;
2470}
2471
2472/**
2473 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
2474 * @instance: Adapter soft state
2475 * @argp: User's ioctl packet
2476 */
2477static int
2478megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
2479 struct megasas_iocpacket __user * user_ioc,
2480 struct megasas_iocpacket *ioc)
2481{
2482 struct megasas_sge32 *kern_sge32;
2483 struct megasas_cmd *cmd;
2484 void *kbuff_arr[MAX_IOCTL_SGE];
2485 dma_addr_t buf_handle = 0;
2486 int error = 0, i;
2487 void *sense = NULL;
2488 dma_addr_t sense_handle;
2489 u32 *sense_ptr;
2490
2491 memset(kbuff_arr, 0, sizeof(kbuff_arr));
2492
2493 if (ioc->sge_count > MAX_IOCTL_SGE) {
2494 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
2495 ioc->sge_count, MAX_IOCTL_SGE);
2496 return -EINVAL;
2497 }
2498
2499 cmd = megasas_get_cmd(instance);
2500 if (!cmd) {
2501 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
2502 return -ENOMEM;
2503 }
2504
2505 /*
2506 * User's IOCTL packet has 2 frames (maximum). Copy those two
2507 * frames into our cmd's frames. cmd->frame's context will get
2508 * overwritten when we copy from user's frames. So set that value
2509 * alone separately
2510 */
2511 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
2512 cmd->frame->hdr.context = cmd->index;
2513
2514 /*
2515 * The management interface between applications and the fw uses
2516 * MFI frames. E.g, RAID configuration changes, LD property changes
2517 * etc are accomplishes through different kinds of MFI frames. The
2518 * driver needs to care only about substituting user buffers with
2519 * kernel buffers in SGLs. The location of SGL is embedded in the
2520 * struct iocpacket itself.
2521 */
2522 kern_sge32 = (struct megasas_sge32 *)
2523 ((unsigned long)cmd->frame + ioc->sgl_off);
2524
2525 /*
2526 * For each user buffer, create a mirror buffer and copy in
2527 */
2528 for (i = 0; i < ioc->sge_count; i++) {
2529 kbuff_arr[i] = pci_alloc_consistent(instance->pdev,
2530 ioc->sgl[i].iov_len,
2531 &buf_handle);
2532 if (!kbuff_arr[i]) {
2533 printk(KERN_DEBUG "megasas: Failed to alloc "
2534 "kernel SGL buffer for IOCTL \n");
2535 error = -ENOMEM;
2536 goto out;
2537 }
2538
2539 /*
2540 * We don't change the dma_coherent_mask, so
2541 * pci_alloc_consistent only returns 32bit addresses
2542 */
2543 kern_sge32[i].phys_addr = (u32) buf_handle;
2544 kern_sge32[i].length = ioc->sgl[i].iov_len;
2545
2546 /*
2547 * We created a kernel buffer corresponding to the
2548 * user buffer. Now copy in from the user buffer
2549 */
2550 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
2551 (u32) (ioc->sgl[i].iov_len))) {
2552 error = -EFAULT;
2553 goto out;
2554 }
2555 }
2556
2557 if (ioc->sense_len) {
2558 sense = pci_alloc_consistent(instance->pdev, ioc->sense_len,
2559 &sense_handle);
2560 if (!sense) {
2561 error = -ENOMEM;
2562 goto out;
2563 }
2564
2565 sense_ptr =
2566 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
2567 *sense_ptr = sense_handle;
2568 }
2569
2570 /*
2571 * Set the sync_cmd flag so that the ISR knows not to complete this
2572 * cmd to the SCSI mid-layer
2573 */
2574 cmd->sync_cmd = 1;
2575 megasas_issue_blocked_cmd(instance, cmd);
2576 cmd->sync_cmd = 0;
2577
2578 /*
2579 * copy out the kernel buffers to user buffers
2580 */
2581 for (i = 0; i < ioc->sge_count; i++) {
2582 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
2583 ioc->sgl[i].iov_len)) {
2584 error = -EFAULT;
2585 goto out;
2586 }
2587 }
2588
2589 /*
2590 * copy out the sense
2591 */
2592 if (ioc->sense_len) {
2593 /*
2594 * sense_ptr points to the location that has the user
2595 * sense buffer address
2596 */
2597 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
2598 ioc->sense_off);
2599
2600 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
2601 sense, ioc->sense_len)) {
2602 error = -EFAULT;
2603 goto out;
2604 }
2605 }
2606
2607 /*
2608 * copy the status codes returned by the fw
2609 */
2610 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
2611 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
2612 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
2613 error = -EFAULT;
2614 }
2615
2616 out:
2617 if (sense) {
2618 pci_free_consistent(instance->pdev, ioc->sense_len,
2619 sense, sense_handle);
2620 }
2621
2622 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
2623 pci_free_consistent(instance->pdev,
2624 kern_sge32[i].length,
2625 kbuff_arr[i], kern_sge32[i].phys_addr);
2626 }
2627
2628 megasas_return_cmd(instance, cmd);
2629 return error;
2630}
2631
2632static struct megasas_instance *megasas_lookup_instance(u16 host_no)
2633{
2634 int i;
2635
2636 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2637
2638 if ((megasas_mgmt_info.instance[i]) &&
2639 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
2640 return megasas_mgmt_info.instance[i];
2641 }
2642
2643 return NULL;
2644}
2645
2646static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
2647{
2648 struct megasas_iocpacket __user *user_ioc =
2649 (struct megasas_iocpacket __user *)arg;
2650 struct megasas_iocpacket *ioc;
2651 struct megasas_instance *instance;
2652 int error;
2653
2654 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
2655 if (!ioc)
2656 return -ENOMEM;
2657
2658 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
2659 error = -EFAULT;
2660 goto out_kfree_ioc;
2661 }
2662
2663 instance = megasas_lookup_instance(ioc->host_no);
2664 if (!instance) {
2665 error = -ENODEV;
2666 goto out_kfree_ioc;
2667 }
2668
2669 /*
2670 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
2671 */
2672 if (down_interruptible(&instance->ioctl_sem)) {
2673 error = -ERESTARTSYS;
2674 goto out_kfree_ioc;
2675 }
2676 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
2677 up(&instance->ioctl_sem);
2678
2679 out_kfree_ioc:
2680 kfree(ioc);
2681 return error;
2682}
2683
2684static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
2685{
2686 struct megasas_instance *instance;
2687 struct megasas_aen aen;
2688 int error;
2689
2690 if (file->private_data != file) {
2691 printk(KERN_DEBUG "megasas: fasync_helper was not "
2692 "called first\n");
2693 return -EINVAL;
2694 }
2695
2696 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
2697 return -EFAULT;
2698
2699 instance = megasas_lookup_instance(aen.host_no);
2700
2701 if (!instance)
2702 return -ENODEV;
2703
2704 down(&instance->aen_mutex);
2705 error = megasas_register_aen(instance, aen.seq_num,
2706 aen.class_locale_word);
2707 up(&instance->aen_mutex);
2708 return error;
2709}
2710
2711/**
2712 * megasas_mgmt_ioctl - char node ioctl entry point
2713 */
2714static long
2715megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2716{
2717 switch (cmd) {
2718 case MEGASAS_IOC_FIRMWARE:
2719 return megasas_mgmt_ioctl_fw(file, arg);
2720
2721 case MEGASAS_IOC_GET_AEN:
2722 return megasas_mgmt_ioctl_aen(file, arg);
2723 }
2724
2725 return -ENOTTY;
2726}
2727
2728#ifdef CONFIG_COMPAT
2729static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
2730{
2731 struct compat_megasas_iocpacket __user *cioc =
2732 (struct compat_megasas_iocpacket __user *)arg;
2733 struct megasas_iocpacket __user *ioc =
2734 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
2735 int i;
2736 int error = 0;
2737
2738 clear_user(ioc, sizeof(*ioc));
2739
2740 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
2741 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
2742 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
2743 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
2744 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
2745 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
2746 return -EFAULT;
2747
2748 for (i = 0; i < MAX_IOCTL_SGE; i++) {
2749 compat_uptr_t ptr;
2750
2751 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
2752 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
2753 copy_in_user(&ioc->sgl[i].iov_len,
2754 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
2755 return -EFAULT;
2756 }
2757
2758 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
2759
2760 if (copy_in_user(&cioc->frame.hdr.cmd_status,
2761 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
2762 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
2763 return -EFAULT;
2764 }
2765 return error;
2766}
2767
2768static long
2769megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
2770 unsigned long arg)
2771{
2772 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08002773 case MEGASAS_IOC_FIRMWARE32:
2774 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002775 case MEGASAS_IOC_GET_AEN:
2776 return megasas_mgmt_ioctl_aen(file, arg);
2777 }
2778
2779 return -ENOTTY;
2780}
2781#endif
2782
2783/*
2784 * File operations structure for management interface
2785 */
2786static struct file_operations megasas_mgmt_fops = {
2787 .owner = THIS_MODULE,
2788 .open = megasas_mgmt_open,
2789 .release = megasas_mgmt_release,
2790 .fasync = megasas_mgmt_fasync,
2791 .unlocked_ioctl = megasas_mgmt_ioctl,
2792#ifdef CONFIG_COMPAT
2793 .compat_ioctl = megasas_mgmt_compat_ioctl,
2794#endif
2795};
2796
2797/*
2798 * PCI hotplug support registration structure
2799 */
2800static struct pci_driver megasas_pci_driver = {
2801
2802 .name = "megaraid_sas",
2803 .id_table = megasas_pci_table,
2804 .probe = megasas_probe_one,
2805 .remove = __devexit_p(megasas_detach_one),
2806 .shutdown = megasas_shutdown,
2807};
2808
2809/*
2810 * Sysfs driver attributes
2811 */
2812static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
2813{
2814 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
2815 MEGASAS_VERSION);
2816}
2817
2818static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
2819
2820static ssize_t
2821megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
2822{
2823 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
2824 MEGASAS_RELDATE);
2825}
2826
2827static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
2828 NULL);
2829
2830/**
2831 * megasas_init - Driver load entry point
2832 */
2833static int __init megasas_init(void)
2834{
2835 int rval;
2836
2837 /*
2838 * Announce driver version and other information
2839 */
2840 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
2841 MEGASAS_EXT_VERSION);
2842
2843 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
2844
2845 /*
2846 * Register character device node
2847 */
2848 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
2849
2850 if (rval < 0) {
2851 printk(KERN_DEBUG "megasas: failed to open device node\n");
2852 return rval;
2853 }
2854
2855 megasas_mgmt_majorno = rval;
2856
2857 /*
2858 * Register ourselves as PCI hotplug module
2859 */
2860 rval = pci_module_init(&megasas_pci_driver);
2861
2862 if (rval) {
2863 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
2864 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
2865 }
2866
2867 driver_create_file(&megasas_pci_driver.driver, &driver_attr_version);
2868 driver_create_file(&megasas_pci_driver.driver,
2869 &driver_attr_release_date);
2870
2871 return rval;
2872}
2873
2874/**
2875 * megasas_exit - Driver unload entry point
2876 */
2877static void __exit megasas_exit(void)
2878{
2879 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
2880 driver_remove_file(&megasas_pci_driver.driver,
2881 &driver_attr_release_date);
2882
2883 pci_unregister_driver(&megasas_pci_driver);
2884 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
2885}
2886
2887module_init(megasas_init);
2888module_exit(megasas_exit);