blob: c730bb14543646848d00847203d39744cffa0be3 [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 /*
1275 * Check if it is our interrupt
Sumant Patro1341c932006-01-25 12:02:40 -08001276 * Clear the interrupt
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001277 */
Sumant Patro1341c932006-01-25 12:02:40 -08001278 if(instance->instancet->clear_intr(instance->reg_set))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001279 return IRQ_NONE;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001280
Sumant Patro5d018ad2006-10-03 13:13:18 -07001281 /*
1282 * Schedule the tasklet for cmd completion
1283 */
1284 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001285
1286 return IRQ_HANDLED;
1287}
1288
1289/**
1290 * megasas_isr - isr entry point
1291 */
1292static irqreturn_t megasas_isr(int irq, void *devp, struct pt_regs *regs)
1293{
1294 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1295 DID_OK);
1296}
1297
1298/**
1299 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08001300 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001301 *
1302 * During the initialization, FW passes can potentially be in any one of
1303 * several possible states. If the FW in operational, waiting-for-handshake
1304 * states, driver must take steps to bring it to ready state. Otherwise, it
1305 * has to wait for the ready state.
1306 */
1307static int
Sumant Patro1341c932006-01-25 12:02:40 -08001308megasas_transition_to_ready(struct megasas_instance* instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001309{
1310 int i;
1311 u8 max_wait;
1312 u32 fw_state;
1313 u32 cur_state;
1314
Sumant Patro1341c932006-01-25 12:02:40 -08001315 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001316
Sumant Patroe3bbff92006-10-03 12:28:49 -07001317 if (fw_state != MFI_STATE_READY)
1318 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1319 " state\n");
1320
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001321 while (fw_state != MFI_STATE_READY) {
1322
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001323 switch (fw_state) {
1324
1325 case MFI_STATE_FAULT:
1326
1327 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1328 return -ENODEV;
1329
1330 case MFI_STATE_WAIT_HANDSHAKE:
1331 /*
1332 * Set the CLR bit in inbound doorbell
1333 */
Sumant Patroe3bbff92006-10-03 12:28:49 -07001334 writel(MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
Sumant Patro1341c932006-01-25 12:02:40 -08001335 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001336
1337 max_wait = 2;
1338 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1339 break;
1340
Sumant Patroe3bbff92006-10-03 12:28:49 -07001341 case MFI_STATE_BOOT_MESSAGE_PENDING:
1342 writel(MFI_INIT_HOTPLUG,
1343 &instance->reg_set->inbound_doorbell);
1344
1345 max_wait = 10;
1346 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
1347 break;
1348
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001349 case MFI_STATE_OPERATIONAL:
1350 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07001351 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001352 */
Sumant Patrob274cab2006-10-03 12:52:12 -07001353 instance->instancet->disable_intr(instance->reg_set);
Sumant Patroe3bbff92006-10-03 12:28:49 -07001354 writel(MFI_RESET_FLAGS, &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001355
1356 max_wait = 10;
1357 cur_state = MFI_STATE_OPERATIONAL;
1358 break;
1359
1360 case MFI_STATE_UNDEFINED:
1361 /*
1362 * This state should not last for more than 2 seconds
1363 */
1364 max_wait = 2;
1365 cur_state = MFI_STATE_UNDEFINED;
1366 break;
1367
1368 case MFI_STATE_BB_INIT:
1369 max_wait = 2;
1370 cur_state = MFI_STATE_BB_INIT;
1371 break;
1372
1373 case MFI_STATE_FW_INIT:
1374 max_wait = 20;
1375 cur_state = MFI_STATE_FW_INIT;
1376 break;
1377
1378 case MFI_STATE_FW_INIT_2:
1379 max_wait = 20;
1380 cur_state = MFI_STATE_FW_INIT_2;
1381 break;
1382
1383 case MFI_STATE_DEVICE_SCAN:
1384 max_wait = 20;
1385 cur_state = MFI_STATE_DEVICE_SCAN;
1386 break;
1387
1388 case MFI_STATE_FLUSH_CACHE:
1389 max_wait = 20;
1390 cur_state = MFI_STATE_FLUSH_CACHE;
1391 break;
1392
1393 default:
1394 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1395 fw_state);
1396 return -ENODEV;
1397 }
1398
1399 /*
1400 * The cur_state should not last for more than max_wait secs
1401 */
1402 for (i = 0; i < (max_wait * 1000); i++) {
Sumant Patro1341c932006-01-25 12:02:40 -08001403 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1404 MFI_STATE_MASK ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001405
1406 if (fw_state == cur_state) {
1407 msleep(1);
1408 } else
1409 break;
1410 }
1411
1412 /*
1413 * Return error if fw_state hasn't changed after max_wait
1414 */
1415 if (fw_state == cur_state) {
1416 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1417 "in %d secs\n", fw_state, max_wait);
1418 return -ENODEV;
1419 }
1420 };
Sumant Patroe3bbff92006-10-03 12:28:49 -07001421 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001422
1423 return 0;
1424}
1425
1426/**
1427 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1428 * @instance: Adapter soft state
1429 */
1430static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1431{
1432 int i;
1433 u32 max_cmd = instance->max_fw_cmds;
1434 struct megasas_cmd *cmd;
1435
1436 if (!instance->frame_dma_pool)
1437 return;
1438
1439 /*
1440 * Return all frames to pool
1441 */
1442 for (i = 0; i < max_cmd; i++) {
1443
1444 cmd = instance->cmd_list[i];
1445
1446 if (cmd->frame)
1447 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1448 cmd->frame_phys_addr);
1449
1450 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07001451 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001452 cmd->sense_phys_addr);
1453 }
1454
1455 /*
1456 * Now destroy the pool itself
1457 */
1458 pci_pool_destroy(instance->frame_dma_pool);
1459 pci_pool_destroy(instance->sense_dma_pool);
1460
1461 instance->frame_dma_pool = NULL;
1462 instance->sense_dma_pool = NULL;
1463}
1464
1465/**
1466 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1467 * @instance: Adapter soft state
1468 *
1469 * Each command packet has an embedded DMA memory buffer that is used for
1470 * filling MFI frame and the SG list that immediately follows the frame. This
1471 * function creates those DMA memory buffers for each command packet by using
1472 * PCI pool facility.
1473 */
1474static int megasas_create_frame_pool(struct megasas_instance *instance)
1475{
1476 int i;
1477 u32 max_cmd;
1478 u32 sge_sz;
1479 u32 sgl_sz;
1480 u32 total_sz;
1481 u32 frame_count;
1482 struct megasas_cmd *cmd;
1483
1484 max_cmd = instance->max_fw_cmds;
1485
1486 /*
1487 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1488 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1489 */
1490 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1491 sizeof(struct megasas_sge32);
1492
1493 /*
1494 * Calculated the number of 64byte frames required for SGL
1495 */
1496 sgl_sz = sge_sz * instance->max_num_sge;
1497 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1498
1499 /*
1500 * We need one extra frame for the MFI command
1501 */
1502 frame_count++;
1503
1504 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1505 /*
1506 * Use DMA pool facility provided by PCI layer
1507 */
1508 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1509 instance->pdev, total_sz, 64,
1510 0);
1511
1512 if (!instance->frame_dma_pool) {
1513 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1514 return -ENOMEM;
1515 }
1516
1517 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1518 instance->pdev, 128, 4, 0);
1519
1520 if (!instance->sense_dma_pool) {
1521 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1522
1523 pci_pool_destroy(instance->frame_dma_pool);
1524 instance->frame_dma_pool = NULL;
1525
1526 return -ENOMEM;
1527 }
1528
1529 /*
1530 * Allocate and attach a frame to each of the commands in cmd_list.
1531 * By making cmd->index as the context instead of the &cmd, we can
1532 * always use 32bit context regardless of the architecture
1533 */
1534 for (i = 0; i < max_cmd; i++) {
1535
1536 cmd = instance->cmd_list[i];
1537
1538 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1539 GFP_KERNEL, &cmd->frame_phys_addr);
1540
1541 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1542 GFP_KERNEL, &cmd->sense_phys_addr);
1543
1544 /*
1545 * megasas_teardown_frame_pool() takes care of freeing
1546 * whatever has been allocated
1547 */
1548 if (!cmd->frame || !cmd->sense) {
1549 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1550 megasas_teardown_frame_pool(instance);
1551 return -ENOMEM;
1552 }
1553
1554 cmd->frame->io.context = cmd->index;
1555 }
1556
1557 return 0;
1558}
1559
1560/**
1561 * megasas_free_cmds - Free all the cmds in the free cmd pool
1562 * @instance: Adapter soft state
1563 */
1564static void megasas_free_cmds(struct megasas_instance *instance)
1565{
1566 int i;
1567 /* First free the MFI frame pool */
1568 megasas_teardown_frame_pool(instance);
1569
1570 /* Free all the commands in the cmd_list */
1571 for (i = 0; i < instance->max_fw_cmds; i++)
1572 kfree(instance->cmd_list[i]);
1573
1574 /* Free the cmd_list buffer itself */
1575 kfree(instance->cmd_list);
1576 instance->cmd_list = NULL;
1577
1578 INIT_LIST_HEAD(&instance->cmd_pool);
1579}
1580
1581/**
1582 * megasas_alloc_cmds - Allocates the command packets
1583 * @instance: Adapter soft state
1584 *
1585 * Each command that is issued to the FW, whether IO commands from the OS or
1586 * internal commands like IOCTLs, are wrapped in local data structure called
1587 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1588 * the FW.
1589 *
1590 * Each frame has a 32-bit field called context (tag). This context is used
1591 * to get back the megasas_cmd from the frame when a frame gets completed in
1592 * the ISR. Typically the address of the megasas_cmd itself would be used as
1593 * the context. But we wanted to keep the differences between 32 and 64 bit
1594 * systems to the mininum. We always use 32 bit integers for the context. In
1595 * this driver, the 32 bit values are the indices into an array cmd_list.
1596 * This array is used only to look up the megasas_cmd given the context. The
1597 * free commands themselves are maintained in a linked list called cmd_pool.
1598 */
1599static int megasas_alloc_cmds(struct megasas_instance *instance)
1600{
1601 int i;
1602 int j;
1603 u32 max_cmd;
1604 struct megasas_cmd *cmd;
1605
1606 max_cmd = instance->max_fw_cmds;
1607
1608 /*
1609 * instance->cmd_list is an array of struct megasas_cmd pointers.
1610 * Allocate the dynamic array first and then allocate individual
1611 * commands.
1612 */
1613 instance->cmd_list = kmalloc(sizeof(struct megasas_cmd *) * max_cmd,
1614 GFP_KERNEL);
1615
1616 if (!instance->cmd_list) {
1617 printk(KERN_DEBUG "megasas: out of memory\n");
1618 return -ENOMEM;
1619 }
1620
1621 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) * max_cmd);
1622
1623 for (i = 0; i < max_cmd; i++) {
1624 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
1625 GFP_KERNEL);
1626
1627 if (!instance->cmd_list[i]) {
1628
1629 for (j = 0; j < i; j++)
1630 kfree(instance->cmd_list[j]);
1631
1632 kfree(instance->cmd_list);
1633 instance->cmd_list = NULL;
1634
1635 return -ENOMEM;
1636 }
1637 }
1638
1639 /*
1640 * Add all the commands to command pool (instance->cmd_pool)
1641 */
1642 for (i = 0; i < max_cmd; i++) {
1643 cmd = instance->cmd_list[i];
1644 memset(cmd, 0, sizeof(struct megasas_cmd));
1645 cmd->index = i;
1646 cmd->instance = instance;
1647
1648 list_add_tail(&cmd->list, &instance->cmd_pool);
1649 }
1650
1651 /*
1652 * Create a frame pool and assign one frame to each cmd
1653 */
1654 if (megasas_create_frame_pool(instance)) {
1655 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
1656 megasas_free_cmds(instance);
1657 }
1658
1659 return 0;
1660}
1661
1662/**
1663 * megasas_get_controller_info - Returns FW's controller structure
1664 * @instance: Adapter soft state
1665 * @ctrl_info: Controller information structure
1666 *
1667 * Issues an internal command (DCMD) to get the FW's controller structure.
1668 * This information is mainly used to find out the maximum IO transfer per
1669 * command supported by the FW.
1670 */
1671static int
1672megasas_get_ctrl_info(struct megasas_instance *instance,
1673 struct megasas_ctrl_info *ctrl_info)
1674{
1675 int ret = 0;
1676 struct megasas_cmd *cmd;
1677 struct megasas_dcmd_frame *dcmd;
1678 struct megasas_ctrl_info *ci;
1679 dma_addr_t ci_h = 0;
1680
1681 cmd = megasas_get_cmd(instance);
1682
1683 if (!cmd) {
1684 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
1685 return -ENOMEM;
1686 }
1687
1688 dcmd = &cmd->frame->dcmd;
1689
1690 ci = pci_alloc_consistent(instance->pdev,
1691 sizeof(struct megasas_ctrl_info), &ci_h);
1692
1693 if (!ci) {
1694 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
1695 megasas_return_cmd(instance, cmd);
1696 return -ENOMEM;
1697 }
1698
1699 memset(ci, 0, sizeof(*ci));
1700 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1701
1702 dcmd->cmd = MFI_CMD_DCMD;
1703 dcmd->cmd_status = 0xFF;
1704 dcmd->sge_count = 1;
1705 dcmd->flags = MFI_FRAME_DIR_READ;
1706 dcmd->timeout = 0;
1707 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
1708 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
1709 dcmd->sgl.sge32[0].phys_addr = ci_h;
1710 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
1711
1712 if (!megasas_issue_polled(instance, cmd)) {
1713 ret = 0;
1714 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
1715 } else {
1716 ret = -1;
1717 }
1718
1719 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
1720 ci, ci_h);
1721
1722 megasas_return_cmd(instance, cmd);
1723 return ret;
1724}
1725
1726/**
Sumant Patro5d018ad2006-10-03 13:13:18 -07001727 * megasas_complete_cmd_dpc - Returns FW's controller structure
1728 * @instance_addr: Address of adapter soft state
1729 *
1730 * Tasklet to complete cmds
1731 */
1732void megasas_complete_cmd_dpc(unsigned long instance_addr)
1733{
1734 u32 producer;
1735 u32 consumer;
1736 u32 context;
1737 struct megasas_cmd *cmd;
1738 struct megasas_instance *instance = (struct megasas_instance *)instance_addr;
1739
1740 producer = *instance->producer;
1741 consumer = *instance->consumer;
1742
1743 while (consumer != producer) {
1744 context = instance->reply_queue[consumer];
1745
1746 cmd = instance->cmd_list[context];
1747
1748 megasas_complete_cmd(instance, cmd, DID_OK);
1749
1750 consumer++;
1751 if (consumer == (instance->max_fw_cmds + 1)) {
1752 consumer = 0;
1753 }
1754 }
1755
1756 *instance->consumer = producer;
1757}
1758
1759/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001760 * megasas_init_mfi - Initializes the FW
1761 * @instance: Adapter soft state
1762 *
1763 * This is the main function for initializing MFI firmware.
1764 */
1765static int megasas_init_mfi(struct megasas_instance *instance)
1766{
1767 u32 context_sz;
1768 u32 reply_q_sz;
1769 u32 max_sectors_1;
1770 u32 max_sectors_2;
1771 struct megasas_register_set __iomem *reg_set;
1772
1773 struct megasas_cmd *cmd;
1774 struct megasas_ctrl_info *ctrl_info;
1775
1776 struct megasas_init_frame *init_frame;
1777 struct megasas_init_queue_info *initq_info;
1778 dma_addr_t init_frame_h;
1779 dma_addr_t initq_info_h;
1780
1781 /*
1782 * Map the message registers
1783 */
1784 instance->base_addr = pci_resource_start(instance->pdev, 0);
1785
1786 if (pci_request_regions(instance->pdev, "megasas: LSI Logic")) {
1787 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
1788 return -EBUSY;
1789 }
1790
1791 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
1792
1793 if (!instance->reg_set) {
1794 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
1795 goto fail_ioremap;
1796 }
1797
1798 reg_set = instance->reg_set;
1799
Sumant Patrof9876f02006-02-03 15:34:35 -08001800 switch(instance->pdev->device)
1801 {
1802 case PCI_DEVICE_ID_LSI_SAS1078R:
1803 instance->instancet = &megasas_instance_template_ppc;
1804 break;
1805 case PCI_DEVICE_ID_LSI_SAS1064R:
1806 case PCI_DEVICE_ID_DELL_PERC5:
1807 default:
1808 instance->instancet = &megasas_instance_template_xscale;
1809 break;
1810 }
Sumant Patro1341c932006-01-25 12:02:40 -08001811
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001812 /*
1813 * We expect the FW state to be READY
1814 */
Sumant Patro1341c932006-01-25 12:02:40 -08001815 if (megasas_transition_to_ready(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001816 goto fail_ready_state;
1817
1818 /*
1819 * Get various operational parameters from status register
1820 */
Sumant Patro1341c932006-01-25 12:02:40 -08001821 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07001822 /*
1823 * Reduce the max supported cmds by 1. This is to ensure that the
1824 * reply_q_sz (1 more than the max cmd that driver may send)
1825 * does not exceed max cmds that the FW can support
1826 */
1827 instance->max_fw_cmds = instance->max_fw_cmds-1;
Sumant Patro1341c932006-01-25 12:02:40 -08001828 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
1829 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001830 /*
1831 * Create a pool of commands
1832 */
1833 if (megasas_alloc_cmds(instance))
1834 goto fail_alloc_cmds;
1835
1836 /*
1837 * Allocate memory for reply queue. Length of reply queue should
1838 * be _one_ more than the maximum commands handled by the firmware.
1839 *
1840 * Note: When FW completes commands, it places corresponding contex
1841 * values in this circular reply queue. This circular queue is a fairly
1842 * typical producer-consumer queue. FW is the producer (of completed
1843 * commands) and the driver is the consumer.
1844 */
1845 context_sz = sizeof(u32);
1846 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
1847
1848 instance->reply_queue = pci_alloc_consistent(instance->pdev,
1849 reply_q_sz,
1850 &instance->reply_queue_h);
1851
1852 if (!instance->reply_queue) {
1853 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
1854 goto fail_reply_queue;
1855 }
1856
1857 /*
1858 * Prepare a init frame. Note the init frame points to queue info
1859 * structure. Each frame has SGL allocated after first 64 bytes. For
1860 * this frame - since we don't need any SGL - we use SGL's space as
1861 * queue info structure
1862 *
1863 * We will not get a NULL command below. We just created the pool.
1864 */
1865 cmd = megasas_get_cmd(instance);
1866
1867 init_frame = (struct megasas_init_frame *)cmd->frame;
1868 initq_info = (struct megasas_init_queue_info *)
1869 ((unsigned long)init_frame + 64);
1870
1871 init_frame_h = cmd->frame_phys_addr;
1872 initq_info_h = init_frame_h + 64;
1873
1874 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
1875 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
1876
1877 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
1878 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
1879
1880 initq_info->producer_index_phys_addr_lo = instance->producer_h;
1881 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
1882
1883 init_frame->cmd = MFI_CMD_INIT;
1884 init_frame->cmd_status = 0xFF;
1885 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
1886
1887 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
1888
1889 /*
Sumant Patro0e989362006-06-20 15:32:37 -07001890 * disable the intr before firing the init frame to FW
1891 */
Sumant Patrob274cab2006-10-03 12:52:12 -07001892 instance->instancet->disable_intr(instance->reg_set);
Sumant Patro0e989362006-06-20 15:32:37 -07001893
1894 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001895 * Issue the init frame in polled mode
1896 */
1897 if (megasas_issue_polled(instance, cmd)) {
1898 printk(KERN_DEBUG "megasas: Failed to init firmware\n");
1899 goto fail_fw_init;
1900 }
1901
1902 megasas_return_cmd(instance, cmd);
1903
1904 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
1905
1906 /*
1907 * Compute the max allowed sectors per IO: The controller info has two
1908 * limits on max sectors. Driver should use the minimum of these two.
1909 *
1910 * 1 << stripe_sz_ops.min = max sectors per strip
1911 *
1912 * Note that older firmwares ( < FW ver 30) didn't report information
1913 * to calculate max_sectors_1. So the number ended up as zero always.
1914 */
1915 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
1916
1917 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
1918 ctrl_info->max_strips_per_io;
1919 max_sectors_2 = ctrl_info->max_request_size;
1920
1921 instance->max_sectors_per_req = (max_sectors_1 < max_sectors_2)
1922 ? max_sectors_1 : max_sectors_2;
1923 } else
1924 instance->max_sectors_per_req = instance->max_num_sge *
1925 PAGE_SIZE / 512;
1926
1927 kfree(ctrl_info);
1928
Sumant Patro5d018ad2006-10-03 13:13:18 -07001929 /*
1930 * Setup tasklet for cmd completion
1931 */
1932
1933 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
1934 (unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001935 return 0;
1936
1937 fail_fw_init:
1938 megasas_return_cmd(instance, cmd);
1939
1940 pci_free_consistent(instance->pdev, reply_q_sz,
1941 instance->reply_queue, instance->reply_queue_h);
1942 fail_reply_queue:
1943 megasas_free_cmds(instance);
1944
1945 fail_alloc_cmds:
1946 fail_ready_state:
1947 iounmap(instance->reg_set);
1948
1949 fail_ioremap:
1950 pci_release_regions(instance->pdev);
1951
1952 return -EINVAL;
1953}
1954
1955/**
1956 * megasas_release_mfi - Reverses the FW initialization
1957 * @intance: Adapter soft state
1958 */
1959static void megasas_release_mfi(struct megasas_instance *instance)
1960{
1961 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
1962
1963 pci_free_consistent(instance->pdev, reply_q_sz,
1964 instance->reply_queue, instance->reply_queue_h);
1965
1966 megasas_free_cmds(instance);
1967
1968 iounmap(instance->reg_set);
1969
1970 pci_release_regions(instance->pdev);
1971}
1972
1973/**
1974 * megasas_get_seq_num - Gets latest event sequence numbers
1975 * @instance: Adapter soft state
1976 * @eli: FW event log sequence numbers information
1977 *
1978 * FW maintains a log of all events in a non-volatile area. Upper layers would
1979 * usually find out the latest sequence number of the events, the seq number at
1980 * the boot etc. They would "read" all the events below the latest seq number
1981 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
1982 * number), they would subsribe to AEN (asynchronous event notification) and
1983 * wait for the events to happen.
1984 */
1985static int
1986megasas_get_seq_num(struct megasas_instance *instance,
1987 struct megasas_evt_log_info *eli)
1988{
1989 struct megasas_cmd *cmd;
1990 struct megasas_dcmd_frame *dcmd;
1991 struct megasas_evt_log_info *el_info;
1992 dma_addr_t el_info_h = 0;
1993
1994 cmd = megasas_get_cmd(instance);
1995
1996 if (!cmd) {
1997 return -ENOMEM;
1998 }
1999
2000 dcmd = &cmd->frame->dcmd;
2001 el_info = pci_alloc_consistent(instance->pdev,
2002 sizeof(struct megasas_evt_log_info),
2003 &el_info_h);
2004
2005 if (!el_info) {
2006 megasas_return_cmd(instance, cmd);
2007 return -ENOMEM;
2008 }
2009
2010 memset(el_info, 0, sizeof(*el_info));
2011 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2012
2013 dcmd->cmd = MFI_CMD_DCMD;
2014 dcmd->cmd_status = 0x0;
2015 dcmd->sge_count = 1;
2016 dcmd->flags = MFI_FRAME_DIR_READ;
2017 dcmd->timeout = 0;
2018 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
2019 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
2020 dcmd->sgl.sge32[0].phys_addr = el_info_h;
2021 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
2022
2023 megasas_issue_blocked_cmd(instance, cmd);
2024
2025 /*
2026 * Copy the data back into callers buffer
2027 */
2028 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
2029
2030 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
2031 el_info, el_info_h);
2032
2033 megasas_return_cmd(instance, cmd);
2034
2035 return 0;
2036}
2037
2038/**
2039 * megasas_register_aen - Registers for asynchronous event notification
2040 * @instance: Adapter soft state
2041 * @seq_num: The starting sequence number
2042 * @class_locale: Class of the event
2043 *
2044 * This function subscribes for AEN for events beyond the @seq_num. It requests
2045 * to be notified if and only if the event is of type @class_locale
2046 */
2047static int
2048megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
2049 u32 class_locale_word)
2050{
2051 int ret_val;
2052 struct megasas_cmd *cmd;
2053 struct megasas_dcmd_frame *dcmd;
2054 union megasas_evt_class_locale curr_aen;
2055 union megasas_evt_class_locale prev_aen;
2056
2057 /*
2058 * If there an AEN pending already (aen_cmd), check if the
2059 * class_locale of that pending AEN is inclusive of the new
2060 * AEN request we currently have. If it is, then we don't have
2061 * to do anything. In other words, whichever events the current
2062 * AEN request is subscribing to, have already been subscribed
2063 * to.
2064 *
2065 * If the old_cmd is _not_ inclusive, then we have to abort
2066 * that command, form a class_locale that is superset of both
2067 * old and current and re-issue to the FW
2068 */
2069
2070 curr_aen.word = class_locale_word;
2071
2072 if (instance->aen_cmd) {
2073
2074 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
2075
2076 /*
2077 * A class whose enum value is smaller is inclusive of all
2078 * higher values. If a PROGRESS (= -1) was previously
2079 * registered, then a new registration requests for higher
2080 * classes need not be sent to FW. They are automatically
2081 * included.
2082 *
2083 * Locale numbers don't have such hierarchy. They are bitmap
2084 * values
2085 */
2086 if ((prev_aen.members.class <= curr_aen.members.class) &&
2087 !((prev_aen.members.locale & curr_aen.members.locale) ^
2088 curr_aen.members.locale)) {
2089 /*
2090 * Previously issued event registration includes
2091 * current request. Nothing to do.
2092 */
2093 return 0;
2094 } else {
2095 curr_aen.members.locale |= prev_aen.members.locale;
2096
2097 if (prev_aen.members.class < curr_aen.members.class)
2098 curr_aen.members.class = prev_aen.members.class;
2099
2100 instance->aen_cmd->abort_aen = 1;
2101 ret_val = megasas_issue_blocked_abort_cmd(instance,
2102 instance->
2103 aen_cmd);
2104
2105 if (ret_val) {
2106 printk(KERN_DEBUG "megasas: Failed to abort "
2107 "previous AEN command\n");
2108 return ret_val;
2109 }
2110 }
2111 }
2112
2113 cmd = megasas_get_cmd(instance);
2114
2115 if (!cmd)
2116 return -ENOMEM;
2117
2118 dcmd = &cmd->frame->dcmd;
2119
2120 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
2121
2122 /*
2123 * Prepare DCMD for aen registration
2124 */
2125 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2126
2127 dcmd->cmd = MFI_CMD_DCMD;
2128 dcmd->cmd_status = 0x0;
2129 dcmd->sge_count = 1;
2130 dcmd->flags = MFI_FRAME_DIR_READ;
2131 dcmd->timeout = 0;
2132 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
2133 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
2134 dcmd->mbox.w[0] = seq_num;
2135 dcmd->mbox.w[1] = curr_aen.word;
2136 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
2137 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
2138
2139 /*
2140 * Store reference to the cmd used to register for AEN. When an
2141 * application wants us to register for AEN, we have to abort this
2142 * cmd and re-register with a new EVENT LOCALE supplied by that app
2143 */
2144 instance->aen_cmd = cmd;
2145
2146 /*
2147 * Issue the aen registration frame
2148 */
Sumant Patro1341c932006-01-25 12:02:40 -08002149 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002150
2151 return 0;
2152}
2153
2154/**
2155 * megasas_start_aen - Subscribes to AEN during driver load time
2156 * @instance: Adapter soft state
2157 */
2158static int megasas_start_aen(struct megasas_instance *instance)
2159{
2160 struct megasas_evt_log_info eli;
2161 union megasas_evt_class_locale class_locale;
2162
2163 /*
2164 * Get the latest sequence number from FW
2165 */
2166 memset(&eli, 0, sizeof(eli));
2167
2168 if (megasas_get_seq_num(instance, &eli))
2169 return -1;
2170
2171 /*
2172 * Register AEN with FW for latest sequence number plus 1
2173 */
2174 class_locale.members.reserved = 0;
2175 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2176 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2177
2178 return megasas_register_aen(instance, eli.newest_seq_num + 1,
2179 class_locale.word);
2180}
2181
2182/**
2183 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2184 * @instance: Adapter soft state
2185 */
2186static int megasas_io_attach(struct megasas_instance *instance)
2187{
2188 struct Scsi_Host *host = instance->host;
2189
2190 /*
2191 * Export parameters required by SCSI mid-layer
2192 */
2193 host->irq = instance->pdev->irq;
2194 host->unique_id = instance->unique_id;
2195 host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
2196 host->this_id = instance->init_id;
2197 host->sg_tablesize = instance->max_num_sge;
2198 host->max_sectors = instance->max_sectors_per_req;
2199 host->cmd_per_lun = 128;
2200 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
2201 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
2202 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08002203 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002204
2205 /*
2206 * Notify the mid-layer about the new controller
2207 */
2208 if (scsi_add_host(host, &instance->pdev->dev)) {
2209 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2210 return -ENODEV;
2211 }
2212
2213 /*
2214 * Trigger SCSI to scan our drives
2215 */
2216 scsi_scan_host(host);
2217 return 0;
2218}
2219
2220/**
2221 * megasas_probe_one - PCI hotplug entry point
2222 * @pdev: PCI device structure
2223 * @id: PCI ids of supported hotplugged adapter
2224 */
2225static int __devinit
2226megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2227{
2228 int rval;
2229 struct Scsi_Host *host;
2230 struct megasas_instance *instance;
2231
2232 /*
2233 * Announce PCI information
2234 */
2235 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2236 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2237 pdev->subsystem_device);
2238
2239 printk("bus %d:slot %d:func %d\n",
2240 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2241
2242 /*
2243 * PCI prepping: enable device set bus mastering and dma mask
2244 */
2245 rval = pci_enable_device(pdev);
2246
2247 if (rval) {
2248 return rval;
2249 }
2250
2251 pci_set_master(pdev);
2252
2253 /*
2254 * All our contollers are capable of performing 64-bit DMA
2255 */
2256 if (IS_DMA64) {
2257 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK) != 0) {
2258
2259 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2260 goto fail_set_dma_mask;
2261 }
2262 } else {
2263 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2264 goto fail_set_dma_mask;
2265 }
2266
2267 host = scsi_host_alloc(&megasas_template,
2268 sizeof(struct megasas_instance));
2269
2270 if (!host) {
2271 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2272 goto fail_alloc_instance;
2273 }
2274
2275 instance = (struct megasas_instance *)host->hostdata;
2276 memset(instance, 0, sizeof(*instance));
2277
2278 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2279 &instance->producer_h);
2280 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2281 &instance->consumer_h);
2282
2283 if (!instance->producer || !instance->consumer) {
2284 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2285 "producer, consumer\n");
2286 goto fail_alloc_dma_buf;
2287 }
2288
2289 *instance->producer = 0;
2290 *instance->consumer = 0;
2291
2292 instance->evt_detail = pci_alloc_consistent(pdev,
2293 sizeof(struct
2294 megasas_evt_detail),
2295 &instance->evt_detail_h);
2296
2297 if (!instance->evt_detail) {
2298 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2299 "event detail structure\n");
2300 goto fail_alloc_dma_buf;
2301 }
2302
2303 /*
2304 * Initialize locks and queues
2305 */
2306 INIT_LIST_HEAD(&instance->cmd_pool);
2307
Sumant Patroe4a082c2006-05-30 12:03:37 -07002308 atomic_set(&instance->fw_outstanding,0);
2309
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002310 init_waitqueue_head(&instance->int_cmd_wait_q);
2311 init_waitqueue_head(&instance->abort_cmd_wait_q);
2312
2313 spin_lock_init(&instance->cmd_pool_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002314
2315 sema_init(&instance->aen_mutex, 1);
2316 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2317
2318 /*
2319 * Initialize PCI related and misc parameters
2320 */
2321 instance->pdev = pdev;
2322 instance->host = host;
2323 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2324 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2325
Sumant Patro658dced2006-10-03 13:09:14 -07002326 megasas_dbg_lvl = 0;
2327
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002328 /*
2329 * Initialize MFI Firmware
2330 */
2331 if (megasas_init_mfi(instance))
2332 goto fail_init_mfi;
2333
2334 /*
2335 * Register IRQ
2336 */
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07002337 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002338 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2339 goto fail_irq;
2340 }
2341
Sumant Patro1341c932006-01-25 12:02:40 -08002342 instance->instancet->enable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002343
2344 /*
2345 * Store instance in PCI softstate
2346 */
2347 pci_set_drvdata(pdev, instance);
2348
2349 /*
2350 * Add this controller to megasas_mgmt_info structure so that it
2351 * can be exported to management applications
2352 */
2353 megasas_mgmt_info.count++;
2354 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2355 megasas_mgmt_info.max_index++;
2356
2357 /*
2358 * Initiate AEN (Asynchronous Event Notification)
2359 */
2360 if (megasas_start_aen(instance)) {
2361 printk(KERN_DEBUG "megasas: start aen failed\n");
2362 goto fail_start_aen;
2363 }
2364
2365 /*
2366 * Register with SCSI mid-layer
2367 */
2368 if (megasas_io_attach(instance))
2369 goto fail_io_attach;
2370
2371 return 0;
2372
2373 fail_start_aen:
2374 fail_io_attach:
2375 megasas_mgmt_info.count--;
2376 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2377 megasas_mgmt_info.max_index--;
2378
2379 pci_set_drvdata(pdev, NULL);
Sumant Patrob274cab2006-10-03 12:52:12 -07002380 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002381 free_irq(instance->pdev->irq, instance);
2382
2383 megasas_release_mfi(instance);
2384
2385 fail_irq:
2386 fail_init_mfi:
2387 fail_alloc_dma_buf:
2388 if (instance->evt_detail)
2389 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2390 instance->evt_detail,
2391 instance->evt_detail_h);
2392
2393 if (instance->producer)
2394 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2395 instance->producer_h);
2396 if (instance->consumer)
2397 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2398 instance->consumer_h);
2399 scsi_host_put(host);
2400
2401 fail_alloc_instance:
2402 fail_set_dma_mask:
2403 pci_disable_device(pdev);
2404
2405 return -ENODEV;
2406}
2407
2408/**
2409 * megasas_flush_cache - Requests FW to flush all its caches
2410 * @instance: Adapter soft state
2411 */
2412static void megasas_flush_cache(struct megasas_instance *instance)
2413{
2414 struct megasas_cmd *cmd;
2415 struct megasas_dcmd_frame *dcmd;
2416
2417 cmd = megasas_get_cmd(instance);
2418
2419 if (!cmd)
2420 return;
2421
2422 dcmd = &cmd->frame->dcmd;
2423
2424 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2425
2426 dcmd->cmd = MFI_CMD_DCMD;
2427 dcmd->cmd_status = 0x0;
2428 dcmd->sge_count = 0;
2429 dcmd->flags = MFI_FRAME_DIR_NONE;
2430 dcmd->timeout = 0;
2431 dcmd->data_xfer_len = 0;
2432 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
2433 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
2434
2435 megasas_issue_blocked_cmd(instance, cmd);
2436
2437 megasas_return_cmd(instance, cmd);
2438
2439 return;
2440}
2441
2442/**
2443 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2444 * @instance: Adapter soft state
2445 */
2446static void megasas_shutdown_controller(struct megasas_instance *instance)
2447{
2448 struct megasas_cmd *cmd;
2449 struct megasas_dcmd_frame *dcmd;
2450
2451 cmd = megasas_get_cmd(instance);
2452
2453 if (!cmd)
2454 return;
2455
2456 if (instance->aen_cmd)
2457 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
2458
2459 dcmd = &cmd->frame->dcmd;
2460
2461 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2462
2463 dcmd->cmd = MFI_CMD_DCMD;
2464 dcmd->cmd_status = 0x0;
2465 dcmd->sge_count = 0;
2466 dcmd->flags = MFI_FRAME_DIR_NONE;
2467 dcmd->timeout = 0;
2468 dcmd->data_xfer_len = 0;
2469 dcmd->opcode = MR_DCMD_CTRL_SHUTDOWN;
2470
2471 megasas_issue_blocked_cmd(instance, cmd);
2472
2473 megasas_return_cmd(instance, cmd);
2474
2475 return;
2476}
2477
2478/**
2479 * megasas_detach_one - PCI hot"un"plug entry point
2480 * @pdev: PCI device structure
2481 */
2482static void megasas_detach_one(struct pci_dev *pdev)
2483{
2484 int i;
2485 struct Scsi_Host *host;
2486 struct megasas_instance *instance;
2487
2488 instance = pci_get_drvdata(pdev);
2489 host = instance->host;
2490
2491 scsi_remove_host(instance->host);
2492 megasas_flush_cache(instance);
2493 megasas_shutdown_controller(instance);
Sumant Patro5d018ad2006-10-03 13:13:18 -07002494 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002495
2496 /*
2497 * Take the instance off the instance array. Note that we will not
2498 * decrement the max_index. We let this array be sparse array
2499 */
2500 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2501 if (megasas_mgmt_info.instance[i] == instance) {
2502 megasas_mgmt_info.count--;
2503 megasas_mgmt_info.instance[i] = NULL;
2504
2505 break;
2506 }
2507 }
2508
2509 pci_set_drvdata(instance->pdev, NULL);
2510
Sumant Patrob274cab2006-10-03 12:52:12 -07002511 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002512
2513 free_irq(instance->pdev->irq, instance);
2514
2515 megasas_release_mfi(instance);
2516
2517 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2518 instance->evt_detail, instance->evt_detail_h);
2519
2520 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2521 instance->producer_h);
2522
2523 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2524 instance->consumer_h);
2525
2526 scsi_host_put(host);
2527
2528 pci_set_drvdata(pdev, NULL);
2529
2530 pci_disable_device(pdev);
2531
2532 return;
2533}
2534
2535/**
2536 * megasas_shutdown - Shutdown entry point
2537 * @device: Generic device structure
2538 */
2539static void megasas_shutdown(struct pci_dev *pdev)
2540{
2541 struct megasas_instance *instance = pci_get_drvdata(pdev);
2542 megasas_flush_cache(instance);
2543}
2544
2545/**
2546 * megasas_mgmt_open - char node "open" entry point
2547 */
2548static int megasas_mgmt_open(struct inode *inode, struct file *filep)
2549{
2550 /*
2551 * Allow only those users with admin rights
2552 */
2553 if (!capable(CAP_SYS_ADMIN))
2554 return -EACCES;
2555
2556 return 0;
2557}
2558
2559/**
2560 * megasas_mgmt_release - char node "release" entry point
2561 */
2562static int megasas_mgmt_release(struct inode *inode, struct file *filep)
2563{
2564 filep->private_data = NULL;
2565 fasync_helper(-1, filep, 0, &megasas_async_queue);
2566
2567 return 0;
2568}
2569
2570/**
2571 * megasas_mgmt_fasync - Async notifier registration from applications
2572 *
2573 * This function adds the calling process to a driver global queue. When an
2574 * event occurs, SIGIO will be sent to all processes in this queue.
2575 */
2576static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
2577{
2578 int rc;
2579
Arjan van de Ven0b950672006-01-11 13:16:10 +01002580 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002581
2582 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
2583
Arjan van de Ven0b950672006-01-11 13:16:10 +01002584 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002585
2586 if (rc >= 0) {
2587 /* For sanity check when we get ioctl */
2588 filep->private_data = filep;
2589 return 0;
2590 }
2591
2592 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
2593
2594 return rc;
2595}
2596
2597/**
2598 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
2599 * @instance: Adapter soft state
2600 * @argp: User's ioctl packet
2601 */
2602static int
2603megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
2604 struct megasas_iocpacket __user * user_ioc,
2605 struct megasas_iocpacket *ioc)
2606{
2607 struct megasas_sge32 *kern_sge32;
2608 struct megasas_cmd *cmd;
2609 void *kbuff_arr[MAX_IOCTL_SGE];
2610 dma_addr_t buf_handle = 0;
2611 int error = 0, i;
2612 void *sense = NULL;
2613 dma_addr_t sense_handle;
2614 u32 *sense_ptr;
2615
2616 memset(kbuff_arr, 0, sizeof(kbuff_arr));
2617
2618 if (ioc->sge_count > MAX_IOCTL_SGE) {
2619 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
2620 ioc->sge_count, MAX_IOCTL_SGE);
2621 return -EINVAL;
2622 }
2623
2624 cmd = megasas_get_cmd(instance);
2625 if (!cmd) {
2626 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
2627 return -ENOMEM;
2628 }
2629
2630 /*
2631 * User's IOCTL packet has 2 frames (maximum). Copy those two
2632 * frames into our cmd's frames. cmd->frame's context will get
2633 * overwritten when we copy from user's frames. So set that value
2634 * alone separately
2635 */
2636 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
2637 cmd->frame->hdr.context = cmd->index;
2638
2639 /*
2640 * The management interface between applications and the fw uses
2641 * MFI frames. E.g, RAID configuration changes, LD property changes
2642 * etc are accomplishes through different kinds of MFI frames. The
2643 * driver needs to care only about substituting user buffers with
2644 * kernel buffers in SGLs. The location of SGL is embedded in the
2645 * struct iocpacket itself.
2646 */
2647 kern_sge32 = (struct megasas_sge32 *)
2648 ((unsigned long)cmd->frame + ioc->sgl_off);
2649
2650 /*
2651 * For each user buffer, create a mirror buffer and copy in
2652 */
2653 for (i = 0; i < ioc->sge_count; i++) {
2654 kbuff_arr[i] = pci_alloc_consistent(instance->pdev,
2655 ioc->sgl[i].iov_len,
2656 &buf_handle);
2657 if (!kbuff_arr[i]) {
2658 printk(KERN_DEBUG "megasas: Failed to alloc "
2659 "kernel SGL buffer for IOCTL \n");
2660 error = -ENOMEM;
2661 goto out;
2662 }
2663
2664 /*
2665 * We don't change the dma_coherent_mask, so
2666 * pci_alloc_consistent only returns 32bit addresses
2667 */
2668 kern_sge32[i].phys_addr = (u32) buf_handle;
2669 kern_sge32[i].length = ioc->sgl[i].iov_len;
2670
2671 /*
2672 * We created a kernel buffer corresponding to the
2673 * user buffer. Now copy in from the user buffer
2674 */
2675 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
2676 (u32) (ioc->sgl[i].iov_len))) {
2677 error = -EFAULT;
2678 goto out;
2679 }
2680 }
2681
2682 if (ioc->sense_len) {
2683 sense = pci_alloc_consistent(instance->pdev, ioc->sense_len,
2684 &sense_handle);
2685 if (!sense) {
2686 error = -ENOMEM;
2687 goto out;
2688 }
2689
2690 sense_ptr =
2691 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
2692 *sense_ptr = sense_handle;
2693 }
2694
2695 /*
2696 * Set the sync_cmd flag so that the ISR knows not to complete this
2697 * cmd to the SCSI mid-layer
2698 */
2699 cmd->sync_cmd = 1;
2700 megasas_issue_blocked_cmd(instance, cmd);
2701 cmd->sync_cmd = 0;
2702
2703 /*
2704 * copy out the kernel buffers to user buffers
2705 */
2706 for (i = 0; i < ioc->sge_count; i++) {
2707 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
2708 ioc->sgl[i].iov_len)) {
2709 error = -EFAULT;
2710 goto out;
2711 }
2712 }
2713
2714 /*
2715 * copy out the sense
2716 */
2717 if (ioc->sense_len) {
2718 /*
2719 * sense_ptr points to the location that has the user
2720 * sense buffer address
2721 */
2722 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
2723 ioc->sense_off);
2724
2725 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
2726 sense, ioc->sense_len)) {
2727 error = -EFAULT;
2728 goto out;
2729 }
2730 }
2731
2732 /*
2733 * copy the status codes returned by the fw
2734 */
2735 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
2736 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
2737 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
2738 error = -EFAULT;
2739 }
2740
2741 out:
2742 if (sense) {
2743 pci_free_consistent(instance->pdev, ioc->sense_len,
2744 sense, sense_handle);
2745 }
2746
2747 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
2748 pci_free_consistent(instance->pdev,
2749 kern_sge32[i].length,
2750 kbuff_arr[i], kern_sge32[i].phys_addr);
2751 }
2752
2753 megasas_return_cmd(instance, cmd);
2754 return error;
2755}
2756
2757static struct megasas_instance *megasas_lookup_instance(u16 host_no)
2758{
2759 int i;
2760
2761 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2762
2763 if ((megasas_mgmt_info.instance[i]) &&
2764 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
2765 return megasas_mgmt_info.instance[i];
2766 }
2767
2768 return NULL;
2769}
2770
2771static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
2772{
2773 struct megasas_iocpacket __user *user_ioc =
2774 (struct megasas_iocpacket __user *)arg;
2775 struct megasas_iocpacket *ioc;
2776 struct megasas_instance *instance;
2777 int error;
2778
2779 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
2780 if (!ioc)
2781 return -ENOMEM;
2782
2783 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
2784 error = -EFAULT;
2785 goto out_kfree_ioc;
2786 }
2787
2788 instance = megasas_lookup_instance(ioc->host_no);
2789 if (!instance) {
2790 error = -ENODEV;
2791 goto out_kfree_ioc;
2792 }
2793
2794 /*
2795 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
2796 */
2797 if (down_interruptible(&instance->ioctl_sem)) {
2798 error = -ERESTARTSYS;
2799 goto out_kfree_ioc;
2800 }
2801 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
2802 up(&instance->ioctl_sem);
2803
2804 out_kfree_ioc:
2805 kfree(ioc);
2806 return error;
2807}
2808
2809static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
2810{
2811 struct megasas_instance *instance;
2812 struct megasas_aen aen;
2813 int error;
2814
2815 if (file->private_data != file) {
2816 printk(KERN_DEBUG "megasas: fasync_helper was not "
2817 "called first\n");
2818 return -EINVAL;
2819 }
2820
2821 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
2822 return -EFAULT;
2823
2824 instance = megasas_lookup_instance(aen.host_no);
2825
2826 if (!instance)
2827 return -ENODEV;
2828
2829 down(&instance->aen_mutex);
2830 error = megasas_register_aen(instance, aen.seq_num,
2831 aen.class_locale_word);
2832 up(&instance->aen_mutex);
2833 return error;
2834}
2835
2836/**
2837 * megasas_mgmt_ioctl - char node ioctl entry point
2838 */
2839static long
2840megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2841{
2842 switch (cmd) {
2843 case MEGASAS_IOC_FIRMWARE:
2844 return megasas_mgmt_ioctl_fw(file, arg);
2845
2846 case MEGASAS_IOC_GET_AEN:
2847 return megasas_mgmt_ioctl_aen(file, arg);
2848 }
2849
2850 return -ENOTTY;
2851}
2852
2853#ifdef CONFIG_COMPAT
2854static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
2855{
2856 struct compat_megasas_iocpacket __user *cioc =
2857 (struct compat_megasas_iocpacket __user *)arg;
2858 struct megasas_iocpacket __user *ioc =
2859 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
2860 int i;
2861 int error = 0;
2862
2863 clear_user(ioc, sizeof(*ioc));
2864
2865 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
2866 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
2867 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
2868 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
2869 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
2870 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
2871 return -EFAULT;
2872
2873 for (i = 0; i < MAX_IOCTL_SGE; i++) {
2874 compat_uptr_t ptr;
2875
2876 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
2877 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
2878 copy_in_user(&ioc->sgl[i].iov_len,
2879 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
2880 return -EFAULT;
2881 }
2882
2883 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
2884
2885 if (copy_in_user(&cioc->frame.hdr.cmd_status,
2886 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
2887 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
2888 return -EFAULT;
2889 }
2890 return error;
2891}
2892
2893static long
2894megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
2895 unsigned long arg)
2896{
2897 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08002898 case MEGASAS_IOC_FIRMWARE32:
2899 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002900 case MEGASAS_IOC_GET_AEN:
2901 return megasas_mgmt_ioctl_aen(file, arg);
2902 }
2903
2904 return -ENOTTY;
2905}
2906#endif
2907
2908/*
2909 * File operations structure for management interface
2910 */
2911static struct file_operations megasas_mgmt_fops = {
2912 .owner = THIS_MODULE,
2913 .open = megasas_mgmt_open,
2914 .release = megasas_mgmt_release,
2915 .fasync = megasas_mgmt_fasync,
2916 .unlocked_ioctl = megasas_mgmt_ioctl,
2917#ifdef CONFIG_COMPAT
2918 .compat_ioctl = megasas_mgmt_compat_ioctl,
2919#endif
2920};
2921
2922/*
2923 * PCI hotplug support registration structure
2924 */
2925static struct pci_driver megasas_pci_driver = {
2926
2927 .name = "megaraid_sas",
2928 .id_table = megasas_pci_table,
2929 .probe = megasas_probe_one,
2930 .remove = __devexit_p(megasas_detach_one),
2931 .shutdown = megasas_shutdown,
2932};
2933
2934/*
2935 * Sysfs driver attributes
2936 */
2937static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
2938{
2939 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
2940 MEGASAS_VERSION);
2941}
2942
2943static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
2944
2945static ssize_t
2946megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
2947{
2948 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
2949 MEGASAS_RELDATE);
2950}
2951
2952static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
2953 NULL);
2954
Sumant Patro658dced2006-10-03 13:09:14 -07002955static ssize_t
2956megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
2957{
2958 return sprintf(buf,"%u",megasas_dbg_lvl);
2959}
2960
2961static ssize_t
2962megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
2963{
2964 int retval = count;
2965 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
2966 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
2967 retval = -EINVAL;
2968 }
2969 return retval;
2970}
2971
2972static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUGO, megasas_sysfs_show_dbg_lvl,
2973 megasas_sysfs_set_dbg_lvl);
2974
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002975/**
2976 * megasas_init - Driver load entry point
2977 */
2978static int __init megasas_init(void)
2979{
2980 int rval;
2981
2982 /*
2983 * Announce driver version and other information
2984 */
2985 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
2986 MEGASAS_EXT_VERSION);
2987
2988 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
2989
2990 /*
2991 * Register character device node
2992 */
2993 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
2994
2995 if (rval < 0) {
2996 printk(KERN_DEBUG "megasas: failed to open device node\n");
2997 return rval;
2998 }
2999
3000 megasas_mgmt_majorno = rval;
3001
3002 /*
3003 * Register ourselves as PCI hotplug module
3004 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00003005 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003006
3007 if (rval) {
3008 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
3009 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3010 }
3011
3012 driver_create_file(&megasas_pci_driver.driver, &driver_attr_version);
3013 driver_create_file(&megasas_pci_driver.driver,
3014 &driver_attr_release_date);
Sumant Patro658dced2006-10-03 13:09:14 -07003015 driver_create_file(&megasas_pci_driver.driver,
3016 &driver_attr_dbg_lvl);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003017
3018 return rval;
3019}
3020
3021/**
3022 * megasas_exit - Driver unload entry point
3023 */
3024static void __exit megasas_exit(void)
3025{
3026 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3027 driver_remove_file(&megasas_pci_driver.driver,
3028 &driver_attr_release_date);
Sumant Patro658dced2006-10-03 13:09:14 -07003029 driver_remove_file(&megasas_pci_driver.driver,
3030 &driver_attr_dbg_lvl);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003031
3032 pci_unregister_driver(&megasas_pci_driver);
3033 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3034}
3035
3036module_init(megasas_init);
3037module_exit(megasas_exit);