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