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