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