blob: 7eefbe0681d6e7c7ecc95d7be11b3248f89774c1 [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 Patro05e9ebb2007-05-17 05:47:51 -070013 * Version : v00.00.03.10-rc5
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040014 *
15 * Authors:
Sumant Patrocc5968c2007-02-14 13:05:42 -080016 * (email-id : megaraidlinux@lsi.com)
17 * Sreenivas Bagalkote
18 * Sumant Patro
19 * Bo Yang
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040020 *
21 * List of supported controllers
22 *
23 * OEM Product Name VID DID SSVID SSID
24 * --- ------------ --- --- ---- ----
25 */
26
27#include <linux/kernel.h>
28#include <linux/types.h>
29#include <linux/pci.h>
30#include <linux/list.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040031#include <linux/moduleparam.h>
32#include <linux/module.h>
33#include <linux/spinlock.h>
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +020034#include <linux/mutex.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040035#include <linux/interrupt.h>
36#include <linux/delay.h>
37#include <linux/uio.h>
38#include <asm/uaccess.h>
Al Viro43399232005-10-04 17:36:04 +010039#include <linux/fs.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040040#include <linux/compat.h>
Sumant Patrocf62a0a2007-02-14 12:41:55 -080041#include <linux/blkdev.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010042#include <linux/mutex.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040043
44#include <scsi/scsi.h>
45#include <scsi/scsi_cmnd.h>
46#include <scsi/scsi_device.h>
47#include <scsi/scsi_host.h>
48#include "megaraid_sas.h"
49
50MODULE_LICENSE("GPL");
51MODULE_VERSION(MEGASAS_VERSION);
Sumant Patro3d6d1742006-12-29 08:13:54 -080052MODULE_AUTHOR("megaraidlinux@lsi.com");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040053MODULE_DESCRIPTION("LSI Logic MegaRAID SAS Driver");
54
55/*
56 * PCI ID table for all supported controllers
57 */
58static struct pci_device_id megasas_pci_table[] = {
59
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +020060 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
61 /* xscale IOP */
62 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
63 /* ppc IOP */
64 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
65 /* xscale IOP, vega */
66 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
67 /* xscale IOP */
68 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040069};
70
71MODULE_DEVICE_TABLE(pci, megasas_pci_table);
72
73static int megasas_mgmt_majorno;
74static struct megasas_mgmt_info megasas_mgmt_info;
75static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +010076static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040077
Sumant Patro658dced2006-10-03 13:09:14 -070078static u32 megasas_dbg_lvl;
79
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040080/**
81 * megasas_get_cmd - Get a command from the free pool
82 * @instance: Adapter soft state
83 *
84 * Returns a free command from the pool
85 */
Arjan van de Ven858119e2006-01-14 13:20:43 -080086static struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040087 *instance)
88{
89 unsigned long flags;
90 struct megasas_cmd *cmd = NULL;
91
92 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
93
94 if (!list_empty(&instance->cmd_pool)) {
95 cmd = list_entry((&instance->cmd_pool)->next,
96 struct megasas_cmd, list);
97 list_del_init(&cmd->list);
98 } else {
99 printk(KERN_ERR "megasas: Command pool empty!\n");
100 }
101
102 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
103 return cmd;
104}
105
106/**
107 * megasas_return_cmd - Return a cmd to free command pool
108 * @instance: Adapter soft state
109 * @cmd: Command packet to be returned to free command pool
110 */
111static inline void
112megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
113{
114 unsigned long flags;
115
116 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
117
118 cmd->scmd = NULL;
119 list_add_tail(&cmd->list, &instance->cmd_pool);
120
121 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
122}
123
Sumant Patro1341c932006-01-25 12:02:40 -0800124
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400125/**
Sumant Patro1341c932006-01-25 12:02:40 -0800126* The following functions are defined for xscale
127* (deviceid : 1064R, PERC5) controllers
128*/
129
130/**
131 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400132 * @regs: MFI register set
133 */
134static inline void
Sumant Patro1341c932006-01-25 12:02:40 -0800135megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400136{
137 writel(1, &(regs)->outbound_intr_mask);
138
139 /* Dummy readl to force pci flush */
140 readl(&regs->outbound_intr_mask);
141}
142
143/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700144 * megasas_disable_intr_xscale -Disables interrupt
145 * @regs: MFI register set
146 */
147static inline void
148megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
149{
150 u32 mask = 0x1f;
151 writel(mask, &regs->outbound_intr_mask);
152 /* Dummy readl to force pci flush */
153 readl(&regs->outbound_intr_mask);
154}
155
156/**
Sumant Patro1341c932006-01-25 12:02:40 -0800157 * megasas_read_fw_status_reg_xscale - returns the current FW status value
158 * @regs: MFI register set
159 */
160static u32
161megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
162{
163 return readl(&(regs)->outbound_msg_0);
164}
165/**
166 * megasas_clear_interrupt_xscale - Check & clear interrupt
167 * @regs: MFI register set
168 */
169static int
170megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
171{
172 u32 status;
173 /*
174 * Check if it is our interrupt
175 */
176 status = readl(&regs->outbound_intr_status);
177
178 if (!(status & MFI_OB_INTR_STATUS_MASK)) {
179 return 1;
180 }
181
182 /*
183 * Clear the interrupt by writing back the same value
184 */
185 writel(status, &regs->outbound_intr_status);
186
187 return 0;
188}
189
190/**
191 * megasas_fire_cmd_xscale - Sends command to the FW
192 * @frame_phys_addr : Physical address of cmd
193 * @frame_count : Number of frames for the command
194 * @regs : MFI register set
195 */
196static inline void
197megasas_fire_cmd_xscale(dma_addr_t frame_phys_addr,u32 frame_count, struct megasas_register_set __iomem *regs)
198{
199 writel((frame_phys_addr >> 3)|(frame_count),
200 &(regs)->inbound_queue_port);
201}
202
203static struct megasas_instance_template megasas_instance_template_xscale = {
204
205 .fire_cmd = megasas_fire_cmd_xscale,
206 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700207 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800208 .clear_intr = megasas_clear_intr_xscale,
209 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
210};
211
212/**
213* This is the end of set of functions & definitions specific
214* to xscale (deviceid : 1064R, PERC5) controllers
215*/
216
217/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800218* The following functions are defined for ppc (deviceid : 0x60)
219* controllers
220*/
221
222/**
223 * megasas_enable_intr_ppc - Enables interrupts
224 * @regs: MFI register set
225 */
226static inline void
227megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
228{
229 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
230
231 writel(~0x80000004, &(regs)->outbound_intr_mask);
232
233 /* Dummy readl to force pci flush */
234 readl(&regs->outbound_intr_mask);
235}
236
237/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700238 * megasas_disable_intr_ppc - Disable interrupt
239 * @regs: MFI register set
240 */
241static inline void
242megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
243{
244 u32 mask = 0xFFFFFFFF;
245 writel(mask, &regs->outbound_intr_mask);
246 /* Dummy readl to force pci flush */
247 readl(&regs->outbound_intr_mask);
248}
249
250/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800251 * megasas_read_fw_status_reg_ppc - returns the current FW status value
252 * @regs: MFI register set
253 */
254static u32
255megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
256{
257 return readl(&(regs)->outbound_scratch_pad);
258}
259
260/**
261 * megasas_clear_interrupt_ppc - Check & clear interrupt
262 * @regs: MFI register set
263 */
264static int
265megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
266{
267 u32 status;
268 /*
269 * Check if it is our interrupt
270 */
271 status = readl(&regs->outbound_intr_status);
272
273 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
274 return 1;
275 }
276
277 /*
278 * Clear the interrupt by writing back the same value
279 */
280 writel(status, &regs->outbound_doorbell_clear);
281
282 return 0;
283}
284/**
285 * megasas_fire_cmd_ppc - Sends command to the FW
286 * @frame_phys_addr : Physical address of cmd
287 * @frame_count : Number of frames for the command
288 * @regs : MFI register set
289 */
290static inline void
291megasas_fire_cmd_ppc(dma_addr_t frame_phys_addr, u32 frame_count, struct megasas_register_set __iomem *regs)
292{
293 writel((frame_phys_addr | (frame_count<<1))|1,
294 &(regs)->inbound_queue_port);
295}
296
297static struct megasas_instance_template megasas_instance_template_ppc = {
298
299 .fire_cmd = megasas_fire_cmd_ppc,
300 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700301 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800302 .clear_intr = megasas_clear_intr_ppc,
303 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
304};
305
306/**
307* This is the end of set of functions & definitions
308* specific to ppc (deviceid : 0x60) controllers
309*/
310
311/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400312 * megasas_issue_polled - Issues a polling command
313 * @instance: Adapter soft state
314 * @cmd: Command packet to be issued
315 *
316 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
317 */
318static int
319megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
320{
321 int i;
322 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
323
324 struct megasas_header *frame_hdr = &cmd->frame->hdr;
325
326 frame_hdr->cmd_status = 0xFF;
327 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
328
329 /*
330 * Issue the frame using inbound queue port
331 */
Sumant Patro1341c932006-01-25 12:02:40 -0800332 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400333
334 /*
335 * Wait for cmd_status to change
336 */
337 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
338 rmb();
339 msleep(1);
340 }
341
342 if (frame_hdr->cmd_status == 0xff)
343 return -ETIME;
344
345 return 0;
346}
347
348/**
349 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
350 * @instance: Adapter soft state
351 * @cmd: Command to be issued
352 *
353 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -0700354 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400355 * Used to issue ioctl commands.
356 */
357static int
358megasas_issue_blocked_cmd(struct megasas_instance *instance,
359 struct megasas_cmd *cmd)
360{
361 cmd->cmd_status = ENODATA;
362
Sumant Patro1341c932006-01-25 12:02:40 -0800363 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400364
Sumant Patro2a3681e2006-10-03 13:19:21 -0700365 wait_event_timeout(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA),
366 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400367
368 return 0;
369}
370
371/**
372 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
373 * @instance: Adapter soft state
374 * @cmd_to_abort: Previously issued cmd to be aborted
375 *
376 * MFI firmware can abort previously issued AEN comamnd (automatic event
377 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -0700378 * cmd and waits for return status.
379 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400380 */
381static int
382megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
383 struct megasas_cmd *cmd_to_abort)
384{
385 struct megasas_cmd *cmd;
386 struct megasas_abort_frame *abort_fr;
387
388 cmd = megasas_get_cmd(instance);
389
390 if (!cmd)
391 return -1;
392
393 abort_fr = &cmd->frame->abort;
394
395 /*
396 * Prepare and issue the abort frame
397 */
398 abort_fr->cmd = MFI_CMD_ABORT;
399 abort_fr->cmd_status = 0xFF;
400 abort_fr->flags = 0;
401 abort_fr->abort_context = cmd_to_abort->index;
402 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
403 abort_fr->abort_mfi_phys_addr_hi = 0;
404
405 cmd->sync_cmd = 1;
406 cmd->cmd_status = 0xFF;
407
Sumant Patro1341c932006-01-25 12:02:40 -0800408 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400409
410 /*
411 * Wait for this cmd to complete
412 */
Sumant Patro2a3681e2006-10-03 13:19:21 -0700413 wait_event_timeout(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF),
414 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400415
416 megasas_return_cmd(instance, cmd);
417 return 0;
418}
419
420/**
421 * megasas_make_sgl32 - Prepares 32-bit SGL
422 * @instance: Adapter soft state
423 * @scp: SCSI command from the mid-layer
424 * @mfi_sgl: SGL to be filled in
425 *
426 * If successful, this function returns the number of SG elements. Otherwise,
427 * it returnes -1.
428 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800429static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400430megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
431 union megasas_sgl *mfi_sgl)
432{
433 int i;
434 int sge_count;
435 struct scatterlist *os_sgl;
436
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900437 sge_count = scsi_dma_map(scp);
438 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400439
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900440 if (sge_count) {
441 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
442 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
443 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
444 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400445 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400446 return sge_count;
447}
448
449/**
450 * megasas_make_sgl64 - Prepares 64-bit SGL
451 * @instance: Adapter soft state
452 * @scp: SCSI command from the mid-layer
453 * @mfi_sgl: SGL to be filled in
454 *
455 * If successful, this function returns the number of SG elements. Otherwise,
456 * it returnes -1.
457 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800458static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400459megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
460 union megasas_sgl *mfi_sgl)
461{
462 int i;
463 int sge_count;
464 struct scatterlist *os_sgl;
465
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900466 sge_count = scsi_dma_map(scp);
467 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400468
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900469 if (sge_count) {
470 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
471 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
472 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
473 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400474 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400475 return sge_count;
476}
477
Sumant Patrob1df99d2006-10-03 12:40:47 -0700478 /**
479 * megasas_get_frame_count - Computes the number of frames
480 * @sge_count : number of sg elements
481 *
482 * Returns the number of frames required for numnber of sge's (sge_count)
483 */
484
Adrian Bunkb448de42006-11-20 03:23:49 +0100485static u32 megasas_get_frame_count(u8 sge_count)
Sumant Patrob1df99d2006-10-03 12:40:47 -0700486{
487 int num_cnt;
488 int sge_bytes;
489 u32 sge_sz;
490 u32 frame_count=0;
491
492 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
493 sizeof(struct megasas_sge32);
494
495 /*
496 * Main frame can contain 2 SGEs for 64-bit SGLs and
497 * 3 SGEs for 32-bit SGLs
498 */
499 if (IS_DMA64)
500 num_cnt = sge_count - 2;
501 else
502 num_cnt = sge_count - 3;
503
504 if(num_cnt>0){
505 sge_bytes = sge_sz * num_cnt;
506
507 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
508 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
509 }
510 /* Main frame */
511 frame_count +=1;
512
513 if (frame_count > 7)
514 frame_count = 8;
515 return frame_count;
516}
517
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400518/**
519 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
520 * @instance: Adapter soft state
521 * @scp: SCSI command
522 * @cmd: Command to be prepared in
523 *
524 * This function prepares CDB commands. These are typcially pass-through
525 * commands to the devices.
526 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800527static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400528megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
529 struct megasas_cmd *cmd)
530{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400531 u32 is_logical;
532 u32 device_id;
533 u16 flags = 0;
534 struct megasas_pthru_frame *pthru;
535
536 is_logical = MEGASAS_IS_LOGICAL(scp);
537 device_id = MEGASAS_DEV_INDEX(instance, scp);
538 pthru = (struct megasas_pthru_frame *)cmd->frame;
539
540 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
541 flags = MFI_FRAME_DIR_WRITE;
542 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
543 flags = MFI_FRAME_DIR_READ;
544 else if (scp->sc_data_direction == PCI_DMA_NONE)
545 flags = MFI_FRAME_DIR_NONE;
546
547 /*
548 * Prepare the DCDB frame
549 */
550 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
551 pthru->cmd_status = 0x0;
552 pthru->scsi_status = 0x0;
553 pthru->target_id = device_id;
554 pthru->lun = scp->device->lun;
555 pthru->cdb_len = scp->cmd_len;
556 pthru->timeout = 0;
557 pthru->flags = flags;
FUJITA Tomonori155d98f2007-05-26 05:04:08 +0900558 pthru->data_xfer_len = scsi_bufflen(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400559
560 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
561
562 /*
563 * Construct SGL
564 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400565 if (IS_DMA64) {
566 pthru->flags |= MFI_FRAME_SGL64;
567 pthru->sge_count = megasas_make_sgl64(instance, scp,
568 &pthru->sgl);
569 } else
570 pthru->sge_count = megasas_make_sgl32(instance, scp,
571 &pthru->sgl);
572
573 /*
574 * Sense info specific
575 */
576 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
577 pthru->sense_buf_phys_addr_hi = 0;
578 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
579
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400580 /*
581 * Compute the total number of frames this command consumes. FW uses
582 * this number to pull sufficient number of frames from host memory.
583 */
Sumant Patrob1df99d2006-10-03 12:40:47 -0700584 cmd->frame_count = megasas_get_frame_count(pthru->sge_count);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400585
586 return cmd->frame_count;
587}
588
589/**
590 * megasas_build_ldio - Prepares IOs to logical devices
591 * @instance: Adapter soft state
592 * @scp: SCSI command
593 * @cmd: Command to to be prepared
594 *
595 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
596 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800597static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400598megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
599 struct megasas_cmd *cmd)
600{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400601 u32 device_id;
602 u8 sc = scp->cmnd[0];
603 u16 flags = 0;
604 struct megasas_io_frame *ldio;
605
606 device_id = MEGASAS_DEV_INDEX(instance, scp);
607 ldio = (struct megasas_io_frame *)cmd->frame;
608
609 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
610 flags = MFI_FRAME_DIR_WRITE;
611 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
612 flags = MFI_FRAME_DIR_READ;
613
614 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -0700615 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400616 */
617 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
618 ldio->cmd_status = 0x0;
619 ldio->scsi_status = 0x0;
620 ldio->target_id = device_id;
621 ldio->timeout = 0;
622 ldio->reserved_0 = 0;
623 ldio->pad_0 = 0;
624 ldio->flags = flags;
625 ldio->start_lba_hi = 0;
626 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
627
628 /*
629 * 6-byte READ(0x08) or WRITE(0x0A) cdb
630 */
631 if (scp->cmd_len == 6) {
632 ldio->lba_count = (u32) scp->cmnd[4];
633 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
634 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
635
636 ldio->start_lba_lo &= 0x1FFFFF;
637 }
638
639 /*
640 * 10-byte READ(0x28) or WRITE(0x2A) cdb
641 */
642 else if (scp->cmd_len == 10) {
643 ldio->lba_count = (u32) scp->cmnd[8] |
644 ((u32) scp->cmnd[7] << 8);
645 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
646 ((u32) scp->cmnd[3] << 16) |
647 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
648 }
649
650 /*
651 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
652 */
653 else if (scp->cmd_len == 12) {
654 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
655 ((u32) scp->cmnd[7] << 16) |
656 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
657
658 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
659 ((u32) scp->cmnd[3] << 16) |
660 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
661 }
662
663 /*
664 * 16-byte READ(0x88) or WRITE(0x8A) cdb
665 */
666 else if (scp->cmd_len == 16) {
667 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
668 ((u32) scp->cmnd[11] << 16) |
669 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
670
671 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
672 ((u32) scp->cmnd[7] << 16) |
673 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
674
675 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
676 ((u32) scp->cmnd[3] << 16) |
677 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
678
679 }
680
681 /*
682 * Construct SGL
683 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400684 if (IS_DMA64) {
685 ldio->flags |= MFI_FRAME_SGL64;
686 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
687 } else
688 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
689
690 /*
691 * Sense info specific
692 */
693 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
694 ldio->sense_buf_phys_addr_hi = 0;
695 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
696
Sumant Patrob1df99d2006-10-03 12:40:47 -0700697 /*
698 * Compute the total number of frames this command consumes. FW uses
699 * this number to pull sufficient number of frames from host memory.
700 */
701 cmd->frame_count = megasas_get_frame_count(ldio->sge_count);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400702
703 return cmd->frame_count;
704}
705
706/**
Sumant Patrocb59aa62006-01-25 11:53:25 -0800707 * megasas_is_ldio - Checks if the cmd is for logical drive
708 * @scmd: SCSI command
709 *
710 * Called by megasas_queue_command to find out if the command to be queued
711 * is a logical drive command
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400712 */
Sumant Patrocb59aa62006-01-25 11:53:25 -0800713static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400714{
Sumant Patrocb59aa62006-01-25 11:53:25 -0800715 if (!MEGASAS_IS_LOGICAL(cmd))
716 return 0;
717 switch (cmd->cmnd[0]) {
718 case READ_10:
719 case WRITE_10:
720 case READ_12:
721 case WRITE_12:
722 case READ_6:
723 case WRITE_6:
724 case READ_16:
725 case WRITE_16:
726 return 1;
727 default:
728 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400729 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400730}
731
Sumant Patro658dced2006-10-03 13:09:14 -0700732 /**
733 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
734 * in FW
735 * @instance: Adapter soft state
736 */
737static inline void
738megasas_dump_pending_frames(struct megasas_instance *instance)
739{
740 struct megasas_cmd *cmd;
741 int i,n;
742 union megasas_sgl *mfi_sgl;
743 struct megasas_io_frame *ldio;
744 struct megasas_pthru_frame *pthru;
745 u32 sgcount;
746 u32 max_cmd = instance->max_fw_cmds;
747
748 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
749 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
750 if (IS_DMA64)
751 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
752 else
753 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
754
755 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
756 for (i = 0; i < max_cmd; i++) {
757 cmd = instance->cmd_list[i];
758 if(!cmd->scmd)
759 continue;
760 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
761 if (megasas_is_ldio(cmd->scmd)){
762 ldio = (struct megasas_io_frame *)cmd->frame;
763 mfi_sgl = &ldio->sgl;
764 sgcount = ldio->sge_count;
765 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);
766 }
767 else {
768 pthru = (struct megasas_pthru_frame *) cmd->frame;
769 mfi_sgl = &pthru->sgl;
770 sgcount = pthru->sge_count;
771 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);
772 }
773 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
774 for (n = 0; n < sgcount; n++){
775 if (IS_DMA64)
776 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) ;
777 else
778 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
779 }
780 }
781 printk(KERN_ERR "\n");
782 } /*for max_cmd*/
783 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
784 for (i = 0; i < max_cmd; i++) {
785
786 cmd = instance->cmd_list[i];
787
788 if(cmd->sync_cmd == 1){
789 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
790 }
791 }
792 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
793}
794
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400795/**
796 * megasas_queue_command - Queue entry point
797 * @scmd: SCSI command to be queued
798 * @done: Callback entry point
799 */
800static int
801megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
802{
803 u32 frame_count;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400804 struct megasas_cmd *cmd;
805 struct megasas_instance *instance;
806
807 instance = (struct megasas_instance *)
808 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -0800809
810 /* Don't process if we have already declared adapter dead */
811 if (instance->hw_crit_error)
812 return SCSI_MLQUEUE_HOST_BUSY;
813
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400814 scmd->scsi_done = done;
815 scmd->result = 0;
816
Sumant Patrocb59aa62006-01-25 11:53:25 -0800817 if (MEGASAS_IS_LOGICAL(scmd) &&
818 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
819 scmd->result = DID_BAD_TARGET << 16;
820 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400821 }
822
Sumant Patro02b01e02007-02-14 13:00:55 -0800823 switch (scmd->cmnd[0]) {
824 case SYNCHRONIZE_CACHE:
825 /*
826 * FW takes care of flush cache on its own
827 * No need to send it down
828 */
829 scmd->result = DID_OK << 16;
830 goto out_done;
831 default:
832 break;
833 }
834
Sumant Patrocb59aa62006-01-25 11:53:25 -0800835 cmd = megasas_get_cmd(instance);
836 if (!cmd)
837 return SCSI_MLQUEUE_HOST_BUSY;
838
839 /*
840 * Logical drive command
841 */
842 if (megasas_is_ldio(scmd))
843 frame_count = megasas_build_ldio(instance, scmd, cmd);
844 else
845 frame_count = megasas_build_dcdb(instance, scmd, cmd);
846
847 if (!frame_count)
848 goto out_return_cmd;
849
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400850 cmd->scmd = scmd;
Sumant Patro05e9ebb2007-05-17 05:47:51 -0700851 scmd->SCp.ptr = (char *)cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400852
853 /*
854 * Issue the command to the FW
855 */
Sumant Patroe4a082c2006-05-30 12:03:37 -0700856 atomic_inc(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400857
Sumant Patro1341c932006-01-25 12:02:40 -0800858 instance->instancet->fire_cmd(cmd->frame_phys_addr ,cmd->frame_count-1,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400859
860 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -0800861
862 out_return_cmd:
863 megasas_return_cmd(instance, cmd);
864 out_done:
865 done(scmd);
866 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400867}
868
Christoph Hellwig147aab62006-02-17 12:13:48 +0100869static int megasas_slave_configure(struct scsi_device *sdev)
870{
871 /*
872 * Don't export physical disk devices to the disk driver.
873 *
874 * FIXME: Currently we don't export them to the midlayer at all.
875 * That will be fixed once LSI engineers have audited the
876 * firmware for possible issues.
877 */
878 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS && sdev->type == TYPE_DISK)
879 return -ENXIO;
Christoph Hellwige5b3a652006-03-10 17:08:57 +0100880
881 /*
882 * The RAID firmware may require extended timeouts.
883 */
884 if (sdev->channel >= MEGASAS_MAX_PD_CHANNELS)
Sumant Patro05e9ebb2007-05-17 05:47:51 -0700885 sdev->timeout = MEGASAS_DEFAULT_CMD_TIMEOUT * HZ;
Christoph Hellwig147aab62006-02-17 12:13:48 +0100886 return 0;
887}
888
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400889/**
890 * megasas_wait_for_outstanding - Wait for all outstanding cmds
891 * @instance: Adapter soft state
892 *
893 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
894 * complete all its outstanding commands. Returns error if one or more IOs
895 * are pending after this time period. It also marks the controller dead.
896 */
897static int megasas_wait_for_outstanding(struct megasas_instance *instance)
898{
899 int i;
900 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
901
902 for (i = 0; i < wait_time; i++) {
903
Sumant Patroe4a082c2006-05-30 12:03:37 -0700904 int outstanding = atomic_read(&instance->fw_outstanding);
905
906 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400907 break;
908
909 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
910 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -0700911 "commands to complete\n",i,outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400912 }
913
914 msleep(1000);
915 }
916
Sumant Patroe4a082c2006-05-30 12:03:37 -0700917 if (atomic_read(&instance->fw_outstanding)) {
Sumant Patroe3bbff92006-10-03 12:28:49 -0700918 /*
919 * Send signal to FW to stop processing any pending cmds.
920 * The controller will be taken offline by the OS now.
921 */
922 writel(MFI_STOP_ADP,
923 &instance->reg_set->inbound_doorbell);
Sumant Patro658dced2006-10-03 13:09:14 -0700924 megasas_dump_pending_frames(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400925 instance->hw_crit_error = 1;
926 return FAILED;
927 }
928
929 return SUCCESS;
930}
931
932/**
933 * megasas_generic_reset - Generic reset routine
934 * @scmd: Mid-layer SCSI command
935 *
936 * This routine implements a generic reset handler for device, bus and host
937 * reset requests. Device, bus and host specific reset handlers can use this
938 * function after they do their specific tasks.
939 */
940static int megasas_generic_reset(struct scsi_cmnd *scmd)
941{
942 int ret_val;
943 struct megasas_instance *instance;
944
945 instance = (struct megasas_instance *)scmd->device->host->hostdata;
946
Sumant Patro05e9ebb2007-05-17 05:47:51 -0700947 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
948 scmd->serial_number, scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400949
950 if (instance->hw_crit_error) {
951 printk(KERN_ERR "megasas: cannot recover from previous reset "
952 "failures\n");
953 return FAILED;
954 }
955
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400956 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400957 if (ret_val == SUCCESS)
958 printk(KERN_NOTICE "megasas: reset successful \n");
959 else
960 printk(KERN_ERR "megasas: failed to do reset\n");
961
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400962 return ret_val;
963}
964
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400965/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -0700966 * megasas_reset_timer - quiesce the adapter if required
967 * @scmd: scsi cmnd
968 *
969 * Sets the FW busy flag and reduces the host->can_queue if the
970 * cmd has not been completed within the timeout period.
971 */
972static enum
973scsi_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
974{
975 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
976 struct megasas_instance *instance;
977 unsigned long flags;
978
979 if (time_after(jiffies, scmd->jiffies_at_alloc +
980 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
981 return EH_NOT_HANDLED;
982 }
983
984 instance = cmd->instance;
985 if (!(instance->flag & MEGASAS_FW_BUSY)) {
986 /* FW is busy, throttle IO */
987 spin_lock_irqsave(instance->host->host_lock, flags);
988
989 instance->host->can_queue = 16;
990 instance->last_time = jiffies;
991 instance->flag |= MEGASAS_FW_BUSY;
992
993 spin_unlock_irqrestore(instance->host->host_lock, flags);
994 }
995 return EH_RESET_TIMER;
996}
997
998/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400999 * megasas_reset_device - Device reset handler entry point
1000 */
1001static int megasas_reset_device(struct scsi_cmnd *scmd)
1002{
1003 int ret;
1004
1005 /*
1006 * First wait for all commands to complete
1007 */
1008 ret = megasas_generic_reset(scmd);
1009
1010 return ret;
1011}
1012
1013/**
1014 * megasas_reset_bus_host - Bus & host reset handler entry point
1015 */
1016static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1017{
1018 int ret;
1019
1020 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01001021 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001022 */
1023 ret = megasas_generic_reset(scmd);
1024
1025 return ret;
1026}
1027
1028/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001029 * megasas_bios_param - Returns disk geometry for a disk
1030 * @sdev: device handle
1031 * @bdev: block device
1032 * @capacity: drive capacity
1033 * @geom: geometry parameters
1034 */
1035static int
1036megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1037 sector_t capacity, int geom[])
1038{
1039 int heads;
1040 int sectors;
1041 sector_t cylinders;
1042 unsigned long tmp;
1043 /* Default heads (64) & sectors (32) */
1044 heads = 64;
1045 sectors = 32;
1046
1047 tmp = heads * sectors;
1048 cylinders = capacity;
1049
1050 sector_div(cylinders, tmp);
1051
1052 /*
1053 * Handle extended translation size for logical drives > 1Gb
1054 */
1055
1056 if (capacity >= 0x200000) {
1057 heads = 255;
1058 sectors = 63;
1059 tmp = heads*sectors;
1060 cylinders = capacity;
1061 sector_div(cylinders, tmp);
1062 }
1063
1064 geom[0] = heads;
1065 geom[1] = sectors;
1066 geom[2] = cylinders;
1067
1068 return 0;
1069}
1070
1071/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001072 * megasas_service_aen - Processes an event notification
1073 * @instance: Adapter soft state
1074 * @cmd: AEN command completed by the ISR
1075 *
1076 * For AEN, driver sends a command down to FW that is held by the FW till an
1077 * event occurs. When an event of interest occurs, FW completes the command
1078 * that it was previously holding.
1079 *
1080 * This routines sends SIGIO signal to processes that have registered with the
1081 * driver for AEN.
1082 */
1083static void
1084megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1085{
1086 /*
1087 * Don't signal app if it is just an aborted previously registered aen
1088 */
1089 if (!cmd->abort_aen)
1090 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
1091 else
1092 cmd->abort_aen = 0;
1093
1094 instance->aen_cmd = NULL;
1095 megasas_return_cmd(instance, cmd);
1096}
1097
1098/*
1099 * Scsi host template for megaraid_sas driver
1100 */
1101static struct scsi_host_template megasas_template = {
1102
1103 .module = THIS_MODULE,
1104 .name = "LSI Logic SAS based MegaRAID driver",
1105 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01001106 .slave_configure = megasas_slave_configure,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001107 .queuecommand = megasas_queue_command,
1108 .eh_device_reset_handler = megasas_reset_device,
1109 .eh_bus_reset_handler = megasas_reset_bus_host,
1110 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001111 .eh_timed_out = megasas_reset_timer,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08001112 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001113 .use_clustering = ENABLE_CLUSTERING,
FUJITA Tomonori9cb83c72007-10-16 11:24:32 +02001114 .use_sg_chaining = ENABLE_SG_CHAINING,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001115};
1116
1117/**
1118 * megasas_complete_int_cmd - Completes an internal command
1119 * @instance: Adapter soft state
1120 * @cmd: Command to be completed
1121 *
1122 * The megasas_issue_blocked_cmd() function waits for a command to complete
1123 * after it issues a command. This function wakes up that waiting routine by
1124 * calling wake_up() on the wait queue.
1125 */
1126static void
1127megasas_complete_int_cmd(struct megasas_instance *instance,
1128 struct megasas_cmd *cmd)
1129{
1130 cmd->cmd_status = cmd->frame->io.cmd_status;
1131
1132 if (cmd->cmd_status == ENODATA) {
1133 cmd->cmd_status = 0;
1134 }
1135 wake_up(&instance->int_cmd_wait_q);
1136}
1137
1138/**
1139 * megasas_complete_abort - Completes aborting a command
1140 * @instance: Adapter soft state
1141 * @cmd: Cmd that was issued to abort another cmd
1142 *
1143 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
1144 * after it issues an abort on a previously issued command. This function
1145 * wakes up all functions waiting on the same wait queue.
1146 */
1147static void
1148megasas_complete_abort(struct megasas_instance *instance,
1149 struct megasas_cmd *cmd)
1150{
1151 if (cmd->sync_cmd) {
1152 cmd->sync_cmd = 0;
1153 cmd->cmd_status = 0;
1154 wake_up(&instance->abort_cmd_wait_q);
1155 }
1156
1157 return;
1158}
1159
1160/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001161 * megasas_complete_cmd - Completes a command
1162 * @instance: Adapter soft state
1163 * @cmd: Command to be completed
1164 * @alt_status: If non-zero, use this value as status to
1165 * SCSI mid-layer instead of the value returned
1166 * by the FW. This should be used if caller wants
1167 * an alternate status (as in the case of aborted
1168 * commands)
1169 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001170static void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001171megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
1172 u8 alt_status)
1173{
1174 int exception = 0;
1175 struct megasas_header *hdr = &cmd->frame->hdr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001176
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001177 if (cmd->scmd)
1178 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001179
1180 switch (hdr->cmd) {
1181
1182 case MFI_CMD_PD_SCSI_IO:
1183 case MFI_CMD_LD_SCSI_IO:
1184
1185 /*
1186 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1187 * issued either through an IO path or an IOCTL path. If it
1188 * was via IOCTL, we will send it to internal completion.
1189 */
1190 if (cmd->sync_cmd) {
1191 cmd->sync_cmd = 0;
1192 megasas_complete_int_cmd(instance, cmd);
1193 break;
1194 }
1195
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001196 case MFI_CMD_LD_READ:
1197 case MFI_CMD_LD_WRITE:
1198
1199 if (alt_status) {
1200 cmd->scmd->result = alt_status << 16;
1201 exception = 1;
1202 }
1203
1204 if (exception) {
1205
Sumant Patroe4a082c2006-05-30 12:03:37 -07001206 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001207
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001208 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001209 cmd->scmd->scsi_done(cmd->scmd);
1210 megasas_return_cmd(instance, cmd);
1211
1212 break;
1213 }
1214
1215 switch (hdr->cmd_status) {
1216
1217 case MFI_STAT_OK:
1218 cmd->scmd->result = DID_OK << 16;
1219 break;
1220
1221 case MFI_STAT_SCSI_IO_FAILED:
1222 case MFI_STAT_LD_INIT_IN_PROGRESS:
1223 cmd->scmd->result =
1224 (DID_ERROR << 16) | hdr->scsi_status;
1225 break;
1226
1227 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1228
1229 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1230
1231 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1232 memset(cmd->scmd->sense_buffer, 0,
1233 SCSI_SENSE_BUFFERSIZE);
1234 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1235 hdr->sense_len);
1236
1237 cmd->scmd->result |= DRIVER_SENSE << 24;
1238 }
1239
1240 break;
1241
1242 case MFI_STAT_LD_OFFLINE:
1243 case MFI_STAT_DEVICE_NOT_FOUND:
1244 cmd->scmd->result = DID_BAD_TARGET << 16;
1245 break;
1246
1247 default:
1248 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1249 hdr->cmd_status);
1250 cmd->scmd->result = DID_ERROR << 16;
1251 break;
1252 }
1253
Sumant Patroe4a082c2006-05-30 12:03:37 -07001254 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001255
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001256 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001257 cmd->scmd->scsi_done(cmd->scmd);
1258 megasas_return_cmd(instance, cmd);
1259
1260 break;
1261
1262 case MFI_CMD_SMP:
1263 case MFI_CMD_STP:
1264 case MFI_CMD_DCMD:
1265
1266 /*
1267 * See if got an event notification
1268 */
1269 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1270 megasas_service_aen(instance, cmd);
1271 else
1272 megasas_complete_int_cmd(instance, cmd);
1273
1274 break;
1275
1276 case MFI_CMD_ABORT:
1277 /*
1278 * Cmd issued to abort another cmd returned
1279 */
1280 megasas_complete_abort(instance, cmd);
1281 break;
1282
1283 default:
1284 printk("megasas: Unknown command completed! [0x%X]\n",
1285 hdr->cmd);
1286 break;
1287 }
1288}
1289
1290/**
1291 * megasas_deplete_reply_queue - Processes all completed commands
1292 * @instance: Adapter soft state
1293 * @alt_status: Alternate status to be returned to
1294 * SCSI mid-layer instead of the status
1295 * returned by the FW
1296 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001297static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001298megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1299{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001300 /*
1301 * Check if it is our interrupt
Sumant Patro1341c932006-01-25 12:02:40 -08001302 * Clear the interrupt
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001303 */
Sumant Patro1341c932006-01-25 12:02:40 -08001304 if(instance->instancet->clear_intr(instance->reg_set))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001305 return IRQ_NONE;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001306
Sumant Patroaf37acf2007-02-14 12:34:46 -08001307 if (instance->hw_crit_error)
1308 goto out_done;
Sumant Patro5d018ad2006-10-03 13:13:18 -07001309 /*
1310 * Schedule the tasklet for cmd completion
1311 */
1312 tasklet_schedule(&instance->isr_tasklet);
Sumant Patroaf37acf2007-02-14 12:34:46 -08001313out_done:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001314 return IRQ_HANDLED;
1315}
1316
1317/**
1318 * megasas_isr - isr entry point
1319 */
David Howells7d12e782006-10-05 14:55:46 +01001320static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001321{
1322 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1323 DID_OK);
1324}
1325
1326/**
1327 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08001328 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001329 *
1330 * During the initialization, FW passes can potentially be in any one of
1331 * several possible states. If the FW in operational, waiting-for-handshake
1332 * states, driver must take steps to bring it to ready state. Otherwise, it
1333 * has to wait for the ready state.
1334 */
1335static int
Sumant Patro1341c932006-01-25 12:02:40 -08001336megasas_transition_to_ready(struct megasas_instance* instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001337{
1338 int i;
1339 u8 max_wait;
1340 u32 fw_state;
1341 u32 cur_state;
1342
Sumant Patro1341c932006-01-25 12:02:40 -08001343 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001344
Sumant Patroe3bbff92006-10-03 12:28:49 -07001345 if (fw_state != MFI_STATE_READY)
1346 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1347 " state\n");
1348
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001349 while (fw_state != MFI_STATE_READY) {
1350
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001351 switch (fw_state) {
1352
1353 case MFI_STATE_FAULT:
1354
1355 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1356 return -ENODEV;
1357
1358 case MFI_STATE_WAIT_HANDSHAKE:
1359 /*
1360 * Set the CLR bit in inbound doorbell
1361 */
Sumant Patroe3bbff92006-10-03 12:28:49 -07001362 writel(MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
Sumant Patro1341c932006-01-25 12:02:40 -08001363 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001364
1365 max_wait = 2;
1366 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1367 break;
1368
Sumant Patroe3bbff92006-10-03 12:28:49 -07001369 case MFI_STATE_BOOT_MESSAGE_PENDING:
1370 writel(MFI_INIT_HOTPLUG,
1371 &instance->reg_set->inbound_doorbell);
1372
1373 max_wait = 10;
1374 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
1375 break;
1376
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001377 case MFI_STATE_OPERATIONAL:
1378 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07001379 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001380 */
Sumant Patrob274cab2006-10-03 12:52:12 -07001381 instance->instancet->disable_intr(instance->reg_set);
Sumant Patroe3bbff92006-10-03 12:28:49 -07001382 writel(MFI_RESET_FLAGS, &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001383
1384 max_wait = 10;
1385 cur_state = MFI_STATE_OPERATIONAL;
1386 break;
1387
1388 case MFI_STATE_UNDEFINED:
1389 /*
1390 * This state should not last for more than 2 seconds
1391 */
1392 max_wait = 2;
1393 cur_state = MFI_STATE_UNDEFINED;
1394 break;
1395
1396 case MFI_STATE_BB_INIT:
1397 max_wait = 2;
1398 cur_state = MFI_STATE_BB_INIT;
1399 break;
1400
1401 case MFI_STATE_FW_INIT:
1402 max_wait = 20;
1403 cur_state = MFI_STATE_FW_INIT;
1404 break;
1405
1406 case MFI_STATE_FW_INIT_2:
1407 max_wait = 20;
1408 cur_state = MFI_STATE_FW_INIT_2;
1409 break;
1410
1411 case MFI_STATE_DEVICE_SCAN:
1412 max_wait = 20;
1413 cur_state = MFI_STATE_DEVICE_SCAN;
1414 break;
1415
1416 case MFI_STATE_FLUSH_CACHE:
1417 max_wait = 20;
1418 cur_state = MFI_STATE_FLUSH_CACHE;
1419 break;
1420
1421 default:
1422 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1423 fw_state);
1424 return -ENODEV;
1425 }
1426
1427 /*
1428 * The cur_state should not last for more than max_wait secs
1429 */
1430 for (i = 0; i < (max_wait * 1000); i++) {
Sumant Patro1341c932006-01-25 12:02:40 -08001431 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1432 MFI_STATE_MASK ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001433
1434 if (fw_state == cur_state) {
1435 msleep(1);
1436 } else
1437 break;
1438 }
1439
1440 /*
1441 * Return error if fw_state hasn't changed after max_wait
1442 */
1443 if (fw_state == cur_state) {
1444 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1445 "in %d secs\n", fw_state, max_wait);
1446 return -ENODEV;
1447 }
1448 };
Sumant Patroe3bbff92006-10-03 12:28:49 -07001449 printk(KERN_INFO "megasas: FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001450
1451 return 0;
1452}
1453
1454/**
1455 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1456 * @instance: Adapter soft state
1457 */
1458static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1459{
1460 int i;
1461 u32 max_cmd = instance->max_fw_cmds;
1462 struct megasas_cmd *cmd;
1463
1464 if (!instance->frame_dma_pool)
1465 return;
1466
1467 /*
1468 * Return all frames to pool
1469 */
1470 for (i = 0; i < max_cmd; i++) {
1471
1472 cmd = instance->cmd_list[i];
1473
1474 if (cmd->frame)
1475 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1476 cmd->frame_phys_addr);
1477
1478 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07001479 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001480 cmd->sense_phys_addr);
1481 }
1482
1483 /*
1484 * Now destroy the pool itself
1485 */
1486 pci_pool_destroy(instance->frame_dma_pool);
1487 pci_pool_destroy(instance->sense_dma_pool);
1488
1489 instance->frame_dma_pool = NULL;
1490 instance->sense_dma_pool = NULL;
1491}
1492
1493/**
1494 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1495 * @instance: Adapter soft state
1496 *
1497 * Each command packet has an embedded DMA memory buffer that is used for
1498 * filling MFI frame and the SG list that immediately follows the frame. This
1499 * function creates those DMA memory buffers for each command packet by using
1500 * PCI pool facility.
1501 */
1502static int megasas_create_frame_pool(struct megasas_instance *instance)
1503{
1504 int i;
1505 u32 max_cmd;
1506 u32 sge_sz;
1507 u32 sgl_sz;
1508 u32 total_sz;
1509 u32 frame_count;
1510 struct megasas_cmd *cmd;
1511
1512 max_cmd = instance->max_fw_cmds;
1513
1514 /*
1515 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1516 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1517 */
1518 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1519 sizeof(struct megasas_sge32);
1520
1521 /*
1522 * Calculated the number of 64byte frames required for SGL
1523 */
1524 sgl_sz = sge_sz * instance->max_num_sge;
1525 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1526
1527 /*
1528 * We need one extra frame for the MFI command
1529 */
1530 frame_count++;
1531
1532 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1533 /*
1534 * Use DMA pool facility provided by PCI layer
1535 */
1536 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1537 instance->pdev, total_sz, 64,
1538 0);
1539
1540 if (!instance->frame_dma_pool) {
1541 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1542 return -ENOMEM;
1543 }
1544
1545 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1546 instance->pdev, 128, 4, 0);
1547
1548 if (!instance->sense_dma_pool) {
1549 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1550
1551 pci_pool_destroy(instance->frame_dma_pool);
1552 instance->frame_dma_pool = NULL;
1553
1554 return -ENOMEM;
1555 }
1556
1557 /*
1558 * Allocate and attach a frame to each of the commands in cmd_list.
1559 * By making cmd->index as the context instead of the &cmd, we can
1560 * always use 32bit context regardless of the architecture
1561 */
1562 for (i = 0; i < max_cmd; i++) {
1563
1564 cmd = instance->cmd_list[i];
1565
1566 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1567 GFP_KERNEL, &cmd->frame_phys_addr);
1568
1569 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1570 GFP_KERNEL, &cmd->sense_phys_addr);
1571
1572 /*
1573 * megasas_teardown_frame_pool() takes care of freeing
1574 * whatever has been allocated
1575 */
1576 if (!cmd->frame || !cmd->sense) {
1577 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1578 megasas_teardown_frame_pool(instance);
1579 return -ENOMEM;
1580 }
1581
1582 cmd->frame->io.context = cmd->index;
1583 }
1584
1585 return 0;
1586}
1587
1588/**
1589 * megasas_free_cmds - Free all the cmds in the free cmd pool
1590 * @instance: Adapter soft state
1591 */
1592static void megasas_free_cmds(struct megasas_instance *instance)
1593{
1594 int i;
1595 /* First free the MFI frame pool */
1596 megasas_teardown_frame_pool(instance);
1597
1598 /* Free all the commands in the cmd_list */
1599 for (i = 0; i < instance->max_fw_cmds; i++)
1600 kfree(instance->cmd_list[i]);
1601
1602 /* Free the cmd_list buffer itself */
1603 kfree(instance->cmd_list);
1604 instance->cmd_list = NULL;
1605
1606 INIT_LIST_HEAD(&instance->cmd_pool);
1607}
1608
1609/**
1610 * megasas_alloc_cmds - Allocates the command packets
1611 * @instance: Adapter soft state
1612 *
1613 * Each command that is issued to the FW, whether IO commands from the OS or
1614 * internal commands like IOCTLs, are wrapped in local data structure called
1615 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1616 * the FW.
1617 *
1618 * Each frame has a 32-bit field called context (tag). This context is used
1619 * to get back the megasas_cmd from the frame when a frame gets completed in
1620 * the ISR. Typically the address of the megasas_cmd itself would be used as
1621 * the context. But we wanted to keep the differences between 32 and 64 bit
1622 * systems to the mininum. We always use 32 bit integers for the context. In
1623 * this driver, the 32 bit values are the indices into an array cmd_list.
1624 * This array is used only to look up the megasas_cmd given the context. The
1625 * free commands themselves are maintained in a linked list called cmd_pool.
1626 */
1627static int megasas_alloc_cmds(struct megasas_instance *instance)
1628{
1629 int i;
1630 int j;
1631 u32 max_cmd;
1632 struct megasas_cmd *cmd;
1633
1634 max_cmd = instance->max_fw_cmds;
1635
1636 /*
1637 * instance->cmd_list is an array of struct megasas_cmd pointers.
1638 * Allocate the dynamic array first and then allocate individual
1639 * commands.
1640 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07001641 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001642
1643 if (!instance->cmd_list) {
1644 printk(KERN_DEBUG "megasas: out of memory\n");
1645 return -ENOMEM;
1646 }
1647
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001648
1649 for (i = 0; i < max_cmd; i++) {
1650 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
1651 GFP_KERNEL);
1652
1653 if (!instance->cmd_list[i]) {
1654
1655 for (j = 0; j < i; j++)
1656 kfree(instance->cmd_list[j]);
1657
1658 kfree(instance->cmd_list);
1659 instance->cmd_list = NULL;
1660
1661 return -ENOMEM;
1662 }
1663 }
1664
1665 /*
1666 * Add all the commands to command pool (instance->cmd_pool)
1667 */
1668 for (i = 0; i < max_cmd; i++) {
1669 cmd = instance->cmd_list[i];
1670 memset(cmd, 0, sizeof(struct megasas_cmd));
1671 cmd->index = i;
1672 cmd->instance = instance;
1673
1674 list_add_tail(&cmd->list, &instance->cmd_pool);
1675 }
1676
1677 /*
1678 * Create a frame pool and assign one frame to each cmd
1679 */
1680 if (megasas_create_frame_pool(instance)) {
1681 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
1682 megasas_free_cmds(instance);
1683 }
1684
1685 return 0;
1686}
1687
1688/**
1689 * megasas_get_controller_info - Returns FW's controller structure
1690 * @instance: Adapter soft state
1691 * @ctrl_info: Controller information structure
1692 *
1693 * Issues an internal command (DCMD) to get the FW's controller structure.
1694 * This information is mainly used to find out the maximum IO transfer per
1695 * command supported by the FW.
1696 */
1697static int
1698megasas_get_ctrl_info(struct megasas_instance *instance,
1699 struct megasas_ctrl_info *ctrl_info)
1700{
1701 int ret = 0;
1702 struct megasas_cmd *cmd;
1703 struct megasas_dcmd_frame *dcmd;
1704 struct megasas_ctrl_info *ci;
1705 dma_addr_t ci_h = 0;
1706
1707 cmd = megasas_get_cmd(instance);
1708
1709 if (!cmd) {
1710 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
1711 return -ENOMEM;
1712 }
1713
1714 dcmd = &cmd->frame->dcmd;
1715
1716 ci = pci_alloc_consistent(instance->pdev,
1717 sizeof(struct megasas_ctrl_info), &ci_h);
1718
1719 if (!ci) {
1720 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
1721 megasas_return_cmd(instance, cmd);
1722 return -ENOMEM;
1723 }
1724
1725 memset(ci, 0, sizeof(*ci));
1726 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1727
1728 dcmd->cmd = MFI_CMD_DCMD;
1729 dcmd->cmd_status = 0xFF;
1730 dcmd->sge_count = 1;
1731 dcmd->flags = MFI_FRAME_DIR_READ;
1732 dcmd->timeout = 0;
1733 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
1734 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
1735 dcmd->sgl.sge32[0].phys_addr = ci_h;
1736 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
1737
1738 if (!megasas_issue_polled(instance, cmd)) {
1739 ret = 0;
1740 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
1741 } else {
1742 ret = -1;
1743 }
1744
1745 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
1746 ci, ci_h);
1747
1748 megasas_return_cmd(instance, cmd);
1749 return ret;
1750}
1751
1752/**
Sumant Patro5d018ad2006-10-03 13:13:18 -07001753 * megasas_complete_cmd_dpc - Returns FW's controller structure
1754 * @instance_addr: Address of adapter soft state
1755 *
1756 * Tasklet to complete cmds
1757 */
Adrian Bunkb448de42006-11-20 03:23:49 +01001758static void megasas_complete_cmd_dpc(unsigned long instance_addr)
Sumant Patro5d018ad2006-10-03 13:13:18 -07001759{
1760 u32 producer;
1761 u32 consumer;
1762 u32 context;
1763 struct megasas_cmd *cmd;
1764 struct megasas_instance *instance = (struct megasas_instance *)instance_addr;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001765 unsigned long flags;
Sumant Patro5d018ad2006-10-03 13:13:18 -07001766
Sumant Patroaf37acf2007-02-14 12:34:46 -08001767 /* If we have already declared adapter dead, donot complete cmds */
1768 if (instance->hw_crit_error)
1769 return;
1770
Sumant Patro5d018ad2006-10-03 13:13:18 -07001771 producer = *instance->producer;
1772 consumer = *instance->consumer;
1773
1774 while (consumer != producer) {
1775 context = instance->reply_queue[consumer];
1776
1777 cmd = instance->cmd_list[context];
1778
1779 megasas_complete_cmd(instance, cmd, DID_OK);
1780
1781 consumer++;
1782 if (consumer == (instance->max_fw_cmds + 1)) {
1783 consumer = 0;
1784 }
1785 }
1786
1787 *instance->consumer = producer;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07001788
1789 /*
1790 * Check if we can restore can_queue
1791 */
1792 if (instance->flag & MEGASAS_FW_BUSY
1793 && time_after(jiffies, instance->last_time + 5 * HZ)
1794 && atomic_read(&instance->fw_outstanding) < 17) {
1795
1796 spin_lock_irqsave(instance->host->host_lock, flags);
1797 instance->flag &= ~MEGASAS_FW_BUSY;
1798 instance->host->can_queue =
1799 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1800
1801 spin_unlock_irqrestore(instance->host->host_lock, flags);
1802 }
1803
Sumant Patro5d018ad2006-10-03 13:13:18 -07001804}
1805
1806/**
bo yang31ea7082007-11-07 12:09:50 -05001807 * megasas_issue_init_mfi - Initializes the FW
1808 * @instance: Adapter soft state
1809 *
1810 * Issues the INIT MFI cmd
1811 */
1812static int
1813megasas_issue_init_mfi(struct megasas_instance *instance)
1814{
1815 u32 context;
1816
1817 struct megasas_cmd *cmd;
1818
1819 struct megasas_init_frame *init_frame;
1820 struct megasas_init_queue_info *initq_info;
1821 dma_addr_t init_frame_h;
1822 dma_addr_t initq_info_h;
1823
1824 /*
1825 * Prepare a init frame. Note the init frame points to queue info
1826 * structure. Each frame has SGL allocated after first 64 bytes. For
1827 * this frame - since we don't need any SGL - we use SGL's space as
1828 * queue info structure
1829 *
1830 * We will not get a NULL command below. We just created the pool.
1831 */
1832 cmd = megasas_get_cmd(instance);
1833
1834 init_frame = (struct megasas_init_frame *)cmd->frame;
1835 initq_info = (struct megasas_init_queue_info *)
1836 ((unsigned long)init_frame + 64);
1837
1838 init_frame_h = cmd->frame_phys_addr;
1839 initq_info_h = init_frame_h + 64;
1840
1841 context = init_frame->context;
1842 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
1843 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
1844 init_frame->context = context;
1845
1846 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
1847 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
1848
1849 initq_info->producer_index_phys_addr_lo = instance->producer_h;
1850 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
1851
1852 init_frame->cmd = MFI_CMD_INIT;
1853 init_frame->cmd_status = 0xFF;
1854 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
1855
1856 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
1857
1858 /*
1859 * disable the intr before firing the init frame to FW
1860 */
1861 instance->instancet->disable_intr(instance->reg_set);
1862
1863 /*
1864 * Issue the init frame in polled mode
1865 */
1866
1867 if (megasas_issue_polled(instance, cmd)) {
1868 printk(KERN_ERR "megasas: Failed to init firmware\n");
1869 megasas_return_cmd(instance, cmd);
1870 goto fail_fw_init;
1871 }
1872
1873 megasas_return_cmd(instance, cmd);
1874
1875 return 0;
1876
1877fail_fw_init:
1878 return -EINVAL;
1879}
1880
1881/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001882 * megasas_init_mfi - Initializes the FW
1883 * @instance: Adapter soft state
1884 *
1885 * This is the main function for initializing MFI firmware.
1886 */
1887static int megasas_init_mfi(struct megasas_instance *instance)
1888{
1889 u32 context_sz;
1890 u32 reply_q_sz;
1891 u32 max_sectors_1;
1892 u32 max_sectors_2;
bo yang14faea92007-11-09 04:14:00 -05001893 u32 tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001894 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001895 struct megasas_ctrl_info *ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001896 /*
1897 * Map the message registers
1898 */
1899 instance->base_addr = pci_resource_start(instance->pdev, 0);
1900
1901 if (pci_request_regions(instance->pdev, "megasas: LSI Logic")) {
1902 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
1903 return -EBUSY;
1904 }
1905
1906 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
1907
1908 if (!instance->reg_set) {
1909 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
1910 goto fail_ioremap;
1911 }
1912
1913 reg_set = instance->reg_set;
1914
Sumant Patrof9876f02006-02-03 15:34:35 -08001915 switch(instance->pdev->device)
1916 {
1917 case PCI_DEVICE_ID_LSI_SAS1078R:
1918 instance->instancet = &megasas_instance_template_ppc;
1919 break;
1920 case PCI_DEVICE_ID_LSI_SAS1064R:
1921 case PCI_DEVICE_ID_DELL_PERC5:
1922 default:
1923 instance->instancet = &megasas_instance_template_xscale;
1924 break;
1925 }
Sumant Patro1341c932006-01-25 12:02:40 -08001926
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001927 /*
1928 * We expect the FW state to be READY
1929 */
Sumant Patro1341c932006-01-25 12:02:40 -08001930 if (megasas_transition_to_ready(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001931 goto fail_ready_state;
1932
1933 /*
1934 * Get various operational parameters from status register
1935 */
Sumant Patro1341c932006-01-25 12:02:40 -08001936 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07001937 /*
1938 * Reduce the max supported cmds by 1. This is to ensure that the
1939 * reply_q_sz (1 more than the max cmd that driver may send)
1940 * does not exceed max cmds that the FW can support
1941 */
1942 instance->max_fw_cmds = instance->max_fw_cmds-1;
Sumant Patro1341c932006-01-25 12:02:40 -08001943 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
1944 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001945 /*
1946 * Create a pool of commands
1947 */
1948 if (megasas_alloc_cmds(instance))
1949 goto fail_alloc_cmds;
1950
1951 /*
1952 * Allocate memory for reply queue. Length of reply queue should
1953 * be _one_ more than the maximum commands handled by the firmware.
1954 *
1955 * Note: When FW completes commands, it places corresponding contex
1956 * values in this circular reply queue. This circular queue is a fairly
1957 * typical producer-consumer queue. FW is the producer (of completed
1958 * commands) and the driver is the consumer.
1959 */
1960 context_sz = sizeof(u32);
1961 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
1962
1963 instance->reply_queue = pci_alloc_consistent(instance->pdev,
1964 reply_q_sz,
1965 &instance->reply_queue_h);
1966
1967 if (!instance->reply_queue) {
1968 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
1969 goto fail_reply_queue;
1970 }
1971
bo yang31ea7082007-11-07 12:09:50 -05001972 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001973 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001974
1975 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
1976
1977 /*
1978 * Compute the max allowed sectors per IO: The controller info has two
1979 * limits on max sectors. Driver should use the minimum of these two.
1980 *
1981 * 1 << stripe_sz_ops.min = max sectors per strip
1982 *
1983 * Note that older firmwares ( < FW ver 30) didn't report information
1984 * to calculate max_sectors_1. So the number ended up as zero always.
1985 */
bo yang14faea92007-11-09 04:14:00 -05001986 tmp_sectors = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001987 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
1988
1989 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
1990 ctrl_info->max_strips_per_io;
1991 max_sectors_2 = ctrl_info->max_request_size;
1992
bo yang14faea92007-11-09 04:14:00 -05001993 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
1994 }
1995
1996 instance->max_sectors_per_req = instance->max_num_sge *
1997 PAGE_SIZE / 512;
1998 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
1999 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002000
2001 kfree(ctrl_info);
2002
Sumant Patro5d018ad2006-10-03 13:13:18 -07002003 /*
2004 * Setup tasklet for cmd completion
2005 */
2006
2007 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2008 (unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002009 return 0;
2010
2011 fail_fw_init:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002012
2013 pci_free_consistent(instance->pdev, reply_q_sz,
2014 instance->reply_queue, instance->reply_queue_h);
2015 fail_reply_queue:
2016 megasas_free_cmds(instance);
2017
2018 fail_alloc_cmds:
2019 fail_ready_state:
2020 iounmap(instance->reg_set);
2021
2022 fail_ioremap:
2023 pci_release_regions(instance->pdev);
2024
2025 return -EINVAL;
2026}
2027
2028/**
2029 * megasas_release_mfi - Reverses the FW initialization
2030 * @intance: Adapter soft state
2031 */
2032static void megasas_release_mfi(struct megasas_instance *instance)
2033{
2034 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
2035
2036 pci_free_consistent(instance->pdev, reply_q_sz,
2037 instance->reply_queue, instance->reply_queue_h);
2038
2039 megasas_free_cmds(instance);
2040
2041 iounmap(instance->reg_set);
2042
2043 pci_release_regions(instance->pdev);
2044}
2045
2046/**
2047 * megasas_get_seq_num - Gets latest event sequence numbers
2048 * @instance: Adapter soft state
2049 * @eli: FW event log sequence numbers information
2050 *
2051 * FW maintains a log of all events in a non-volatile area. Upper layers would
2052 * usually find out the latest sequence number of the events, the seq number at
2053 * the boot etc. They would "read" all the events below the latest seq number
2054 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
2055 * number), they would subsribe to AEN (asynchronous event notification) and
2056 * wait for the events to happen.
2057 */
2058static int
2059megasas_get_seq_num(struct megasas_instance *instance,
2060 struct megasas_evt_log_info *eli)
2061{
2062 struct megasas_cmd *cmd;
2063 struct megasas_dcmd_frame *dcmd;
2064 struct megasas_evt_log_info *el_info;
2065 dma_addr_t el_info_h = 0;
2066
2067 cmd = megasas_get_cmd(instance);
2068
2069 if (!cmd) {
2070 return -ENOMEM;
2071 }
2072
2073 dcmd = &cmd->frame->dcmd;
2074 el_info = pci_alloc_consistent(instance->pdev,
2075 sizeof(struct megasas_evt_log_info),
2076 &el_info_h);
2077
2078 if (!el_info) {
2079 megasas_return_cmd(instance, cmd);
2080 return -ENOMEM;
2081 }
2082
2083 memset(el_info, 0, sizeof(*el_info));
2084 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2085
2086 dcmd->cmd = MFI_CMD_DCMD;
2087 dcmd->cmd_status = 0x0;
2088 dcmd->sge_count = 1;
2089 dcmd->flags = MFI_FRAME_DIR_READ;
2090 dcmd->timeout = 0;
2091 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
2092 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
2093 dcmd->sgl.sge32[0].phys_addr = el_info_h;
2094 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
2095
2096 megasas_issue_blocked_cmd(instance, cmd);
2097
2098 /*
2099 * Copy the data back into callers buffer
2100 */
2101 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
2102
2103 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
2104 el_info, el_info_h);
2105
2106 megasas_return_cmd(instance, cmd);
2107
2108 return 0;
2109}
2110
2111/**
2112 * megasas_register_aen - Registers for asynchronous event notification
2113 * @instance: Adapter soft state
2114 * @seq_num: The starting sequence number
2115 * @class_locale: Class of the event
2116 *
2117 * This function subscribes for AEN for events beyond the @seq_num. It requests
2118 * to be notified if and only if the event is of type @class_locale
2119 */
2120static int
2121megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
2122 u32 class_locale_word)
2123{
2124 int ret_val;
2125 struct megasas_cmd *cmd;
2126 struct megasas_dcmd_frame *dcmd;
2127 union megasas_evt_class_locale curr_aen;
2128 union megasas_evt_class_locale prev_aen;
2129
2130 /*
2131 * If there an AEN pending already (aen_cmd), check if the
2132 * class_locale of that pending AEN is inclusive of the new
2133 * AEN request we currently have. If it is, then we don't have
2134 * to do anything. In other words, whichever events the current
2135 * AEN request is subscribing to, have already been subscribed
2136 * to.
2137 *
2138 * If the old_cmd is _not_ inclusive, then we have to abort
2139 * that command, form a class_locale that is superset of both
2140 * old and current and re-issue to the FW
2141 */
2142
2143 curr_aen.word = class_locale_word;
2144
2145 if (instance->aen_cmd) {
2146
2147 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
2148
2149 /*
2150 * A class whose enum value is smaller is inclusive of all
2151 * higher values. If a PROGRESS (= -1) was previously
2152 * registered, then a new registration requests for higher
2153 * classes need not be sent to FW. They are automatically
2154 * included.
2155 *
2156 * Locale numbers don't have such hierarchy. They are bitmap
2157 * values
2158 */
2159 if ((prev_aen.members.class <= curr_aen.members.class) &&
2160 !((prev_aen.members.locale & curr_aen.members.locale) ^
2161 curr_aen.members.locale)) {
2162 /*
2163 * Previously issued event registration includes
2164 * current request. Nothing to do.
2165 */
2166 return 0;
2167 } else {
2168 curr_aen.members.locale |= prev_aen.members.locale;
2169
2170 if (prev_aen.members.class < curr_aen.members.class)
2171 curr_aen.members.class = prev_aen.members.class;
2172
2173 instance->aen_cmd->abort_aen = 1;
2174 ret_val = megasas_issue_blocked_abort_cmd(instance,
2175 instance->
2176 aen_cmd);
2177
2178 if (ret_val) {
2179 printk(KERN_DEBUG "megasas: Failed to abort "
2180 "previous AEN command\n");
2181 return ret_val;
2182 }
2183 }
2184 }
2185
2186 cmd = megasas_get_cmd(instance);
2187
2188 if (!cmd)
2189 return -ENOMEM;
2190
2191 dcmd = &cmd->frame->dcmd;
2192
2193 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
2194
2195 /*
2196 * Prepare DCMD for aen registration
2197 */
2198 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2199
2200 dcmd->cmd = MFI_CMD_DCMD;
2201 dcmd->cmd_status = 0x0;
2202 dcmd->sge_count = 1;
2203 dcmd->flags = MFI_FRAME_DIR_READ;
2204 dcmd->timeout = 0;
2205 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
2206 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
2207 dcmd->mbox.w[0] = seq_num;
2208 dcmd->mbox.w[1] = curr_aen.word;
2209 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
2210 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
2211
2212 /*
2213 * Store reference to the cmd used to register for AEN. When an
2214 * application wants us to register for AEN, we have to abort this
2215 * cmd and re-register with a new EVENT LOCALE supplied by that app
2216 */
2217 instance->aen_cmd = cmd;
2218
2219 /*
2220 * Issue the aen registration frame
2221 */
Sumant Patro1341c932006-01-25 12:02:40 -08002222 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002223
2224 return 0;
2225}
2226
2227/**
2228 * megasas_start_aen - Subscribes to AEN during driver load time
2229 * @instance: Adapter soft state
2230 */
2231static int megasas_start_aen(struct megasas_instance *instance)
2232{
2233 struct megasas_evt_log_info eli;
2234 union megasas_evt_class_locale class_locale;
2235
2236 /*
2237 * Get the latest sequence number from FW
2238 */
2239 memset(&eli, 0, sizeof(eli));
2240
2241 if (megasas_get_seq_num(instance, &eli))
2242 return -1;
2243
2244 /*
2245 * Register AEN with FW for latest sequence number plus 1
2246 */
2247 class_locale.members.reserved = 0;
2248 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2249 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2250
2251 return megasas_register_aen(instance, eli.newest_seq_num + 1,
2252 class_locale.word);
2253}
2254
2255/**
2256 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2257 * @instance: Adapter soft state
2258 */
2259static int megasas_io_attach(struct megasas_instance *instance)
2260{
2261 struct Scsi_Host *host = instance->host;
2262
2263 /*
2264 * Export parameters required by SCSI mid-layer
2265 */
2266 host->irq = instance->pdev->irq;
2267 host->unique_id = instance->unique_id;
2268 host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
2269 host->this_id = instance->init_id;
2270 host->sg_tablesize = instance->max_num_sge;
2271 host->max_sectors = instance->max_sectors_per_req;
2272 host->cmd_per_lun = 128;
2273 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
2274 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
2275 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08002276 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002277
2278 /*
2279 * Notify the mid-layer about the new controller
2280 */
2281 if (scsi_add_host(host, &instance->pdev->dev)) {
2282 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2283 return -ENODEV;
2284 }
2285
2286 /*
2287 * Trigger SCSI to scan our drives
2288 */
2289 scsi_scan_host(host);
2290 return 0;
2291}
2292
bo yang31ea7082007-11-07 12:09:50 -05002293static int
2294megasas_set_dma_mask(struct pci_dev *pdev)
2295{
2296 /*
2297 * All our contollers are capable of performing 64-bit DMA
2298 */
2299 if (IS_DMA64) {
2300 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK) != 0) {
2301
2302 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2303 goto fail_set_dma_mask;
2304 }
2305 } else {
2306 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2307 goto fail_set_dma_mask;
2308 }
2309 return 0;
2310
2311fail_set_dma_mask:
2312 return 1;
2313}
2314
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002315/**
2316 * megasas_probe_one - PCI hotplug entry point
2317 * @pdev: PCI device structure
2318 * @id: PCI ids of supported hotplugged adapter
2319 */
2320static int __devinit
2321megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2322{
2323 int rval;
2324 struct Scsi_Host *host;
2325 struct megasas_instance *instance;
2326
2327 /*
2328 * Announce PCI information
2329 */
2330 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2331 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2332 pdev->subsystem_device);
2333
2334 printk("bus %d:slot %d:func %d\n",
2335 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2336
2337 /*
2338 * PCI prepping: enable device set bus mastering and dma mask
2339 */
2340 rval = pci_enable_device(pdev);
2341
2342 if (rval) {
2343 return rval;
2344 }
2345
2346 pci_set_master(pdev);
2347
bo yang31ea7082007-11-07 12:09:50 -05002348 if (megasas_set_dma_mask(pdev))
2349 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002350
2351 host = scsi_host_alloc(&megasas_template,
2352 sizeof(struct megasas_instance));
2353
2354 if (!host) {
2355 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2356 goto fail_alloc_instance;
2357 }
2358
2359 instance = (struct megasas_instance *)host->hostdata;
2360 memset(instance, 0, sizeof(*instance));
2361
2362 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2363 &instance->producer_h);
2364 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2365 &instance->consumer_h);
2366
2367 if (!instance->producer || !instance->consumer) {
2368 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2369 "producer, consumer\n");
2370 goto fail_alloc_dma_buf;
2371 }
2372
2373 *instance->producer = 0;
2374 *instance->consumer = 0;
2375
2376 instance->evt_detail = pci_alloc_consistent(pdev,
2377 sizeof(struct
2378 megasas_evt_detail),
2379 &instance->evt_detail_h);
2380
2381 if (!instance->evt_detail) {
2382 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2383 "event detail structure\n");
2384 goto fail_alloc_dma_buf;
2385 }
2386
2387 /*
2388 * Initialize locks and queues
2389 */
2390 INIT_LIST_HEAD(&instance->cmd_pool);
2391
Sumant Patroe4a082c2006-05-30 12:03:37 -07002392 atomic_set(&instance->fw_outstanding,0);
2393
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002394 init_waitqueue_head(&instance->int_cmd_wait_q);
2395 init_waitqueue_head(&instance->abort_cmd_wait_q);
2396
2397 spin_lock_init(&instance->cmd_pool_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002398
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02002399 mutex_init(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002400 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2401
2402 /*
2403 * Initialize PCI related and misc parameters
2404 */
2405 instance->pdev = pdev;
2406 instance->host = host;
2407 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2408 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2409
Sumant Patro658dced2006-10-03 13:09:14 -07002410 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002411 instance->flag = 0;
2412 instance->last_time = 0;
Sumant Patro658dced2006-10-03 13:09:14 -07002413
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002414 /*
2415 * Initialize MFI Firmware
2416 */
2417 if (megasas_init_mfi(instance))
2418 goto fail_init_mfi;
2419
2420 /*
2421 * Register IRQ
2422 */
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07002423 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002424 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2425 goto fail_irq;
2426 }
2427
Sumant Patro1341c932006-01-25 12:02:40 -08002428 instance->instancet->enable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002429
2430 /*
2431 * Store instance in PCI softstate
2432 */
2433 pci_set_drvdata(pdev, instance);
2434
2435 /*
2436 * Add this controller to megasas_mgmt_info structure so that it
2437 * can be exported to management applications
2438 */
2439 megasas_mgmt_info.count++;
2440 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2441 megasas_mgmt_info.max_index++;
2442
2443 /*
2444 * Initiate AEN (Asynchronous Event Notification)
2445 */
2446 if (megasas_start_aen(instance)) {
2447 printk(KERN_DEBUG "megasas: start aen failed\n");
2448 goto fail_start_aen;
2449 }
2450
2451 /*
2452 * Register with SCSI mid-layer
2453 */
2454 if (megasas_io_attach(instance))
2455 goto fail_io_attach;
2456
2457 return 0;
2458
2459 fail_start_aen:
2460 fail_io_attach:
2461 megasas_mgmt_info.count--;
2462 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2463 megasas_mgmt_info.max_index--;
2464
2465 pci_set_drvdata(pdev, NULL);
Sumant Patrob274cab2006-10-03 12:52:12 -07002466 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002467 free_irq(instance->pdev->irq, instance);
2468
2469 megasas_release_mfi(instance);
2470
2471 fail_irq:
2472 fail_init_mfi:
2473 fail_alloc_dma_buf:
2474 if (instance->evt_detail)
2475 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2476 instance->evt_detail,
2477 instance->evt_detail_h);
2478
2479 if (instance->producer)
2480 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2481 instance->producer_h);
2482 if (instance->consumer)
2483 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2484 instance->consumer_h);
2485 scsi_host_put(host);
2486
2487 fail_alloc_instance:
2488 fail_set_dma_mask:
2489 pci_disable_device(pdev);
2490
2491 return -ENODEV;
2492}
2493
2494/**
2495 * megasas_flush_cache - Requests FW to flush all its caches
2496 * @instance: Adapter soft state
2497 */
2498static void megasas_flush_cache(struct megasas_instance *instance)
2499{
2500 struct megasas_cmd *cmd;
2501 struct megasas_dcmd_frame *dcmd;
2502
2503 cmd = megasas_get_cmd(instance);
2504
2505 if (!cmd)
2506 return;
2507
2508 dcmd = &cmd->frame->dcmd;
2509
2510 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2511
2512 dcmd->cmd = MFI_CMD_DCMD;
2513 dcmd->cmd_status = 0x0;
2514 dcmd->sge_count = 0;
2515 dcmd->flags = MFI_FRAME_DIR_NONE;
2516 dcmd->timeout = 0;
2517 dcmd->data_xfer_len = 0;
2518 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
2519 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
2520
2521 megasas_issue_blocked_cmd(instance, cmd);
2522
2523 megasas_return_cmd(instance, cmd);
2524
2525 return;
2526}
2527
2528/**
2529 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2530 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05002531 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002532 */
bo yang31ea7082007-11-07 12:09:50 -05002533static void megasas_shutdown_controller(struct megasas_instance *instance,
2534 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002535{
2536 struct megasas_cmd *cmd;
2537 struct megasas_dcmd_frame *dcmd;
2538
2539 cmd = megasas_get_cmd(instance);
2540
2541 if (!cmd)
2542 return;
2543
2544 if (instance->aen_cmd)
2545 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
2546
2547 dcmd = &cmd->frame->dcmd;
2548
2549 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2550
2551 dcmd->cmd = MFI_CMD_DCMD;
2552 dcmd->cmd_status = 0x0;
2553 dcmd->sge_count = 0;
2554 dcmd->flags = MFI_FRAME_DIR_NONE;
2555 dcmd->timeout = 0;
2556 dcmd->data_xfer_len = 0;
bo yang31ea7082007-11-07 12:09:50 -05002557 dcmd->opcode = opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002558
2559 megasas_issue_blocked_cmd(instance, cmd);
2560
2561 megasas_return_cmd(instance, cmd);
2562
2563 return;
2564}
2565
2566/**
bo yang31ea7082007-11-07 12:09:50 -05002567 * megasas_suspend - driver suspend entry point
2568 * @pdev: PCI device structure
2569 * @state: PCI power state to suspend routine
2570 */
2571static int __devinit
2572megasas_suspend(struct pci_dev *pdev, pm_message_t state)
2573{
2574 struct Scsi_Host *host;
2575 struct megasas_instance *instance;
2576
2577 instance = pci_get_drvdata(pdev);
2578 host = instance->host;
2579
2580 megasas_flush_cache(instance);
2581 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
2582 tasklet_kill(&instance->isr_tasklet);
2583
2584 pci_set_drvdata(instance->pdev, instance);
2585 instance->instancet->disable_intr(instance->reg_set);
2586 free_irq(instance->pdev->irq, instance);
2587
2588 pci_save_state(pdev);
2589 pci_disable_device(pdev);
2590
2591 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2592
2593 return 0;
2594}
2595
2596/**
2597 * megasas_resume- driver resume entry point
2598 * @pdev: PCI device structure
2599 */
2600static int __devinit
2601megasas_resume(struct pci_dev *pdev)
2602{
2603 int rval;
2604 struct Scsi_Host *host;
2605 struct megasas_instance *instance;
2606
2607 instance = pci_get_drvdata(pdev);
2608 host = instance->host;
2609 pci_set_power_state(pdev, PCI_D0);
2610 pci_enable_wake(pdev, PCI_D0, 0);
2611 pci_restore_state(pdev);
2612
2613 /*
2614 * PCI prepping: enable device set bus mastering and dma mask
2615 */
2616 rval = pci_enable_device(pdev);
2617
2618 if (rval) {
2619 printk(KERN_ERR "megasas: Enable device failed\n");
2620 return rval;
2621 }
2622
2623 pci_set_master(pdev);
2624
2625 if (megasas_set_dma_mask(pdev))
2626 goto fail_set_dma_mask;
2627
2628 /*
2629 * Initialize MFI Firmware
2630 */
2631
2632 *instance->producer = 0;
2633 *instance->consumer = 0;
2634
2635 atomic_set(&instance->fw_outstanding, 0);
2636
2637 /*
2638 * We expect the FW state to be READY
2639 */
2640 if (megasas_transition_to_ready(instance))
2641 goto fail_ready_state;
2642
2643 if (megasas_issue_init_mfi(instance))
2644 goto fail_init_mfi;
2645
2646 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2647 (unsigned long)instance);
2648
2649 /*
2650 * Register IRQ
2651 */
2652 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED,
2653 "megasas", instance)) {
2654 printk(KERN_ERR "megasas: Failed to register IRQ\n");
2655 goto fail_irq;
2656 }
2657
2658 instance->instancet->enable_intr(instance->reg_set);
2659
2660 /*
2661 * Initiate AEN (Asynchronous Event Notification)
2662 */
2663 if (megasas_start_aen(instance))
2664 printk(KERN_ERR "megasas: Start AEN failed\n");
2665
2666 return 0;
2667
2668fail_irq:
2669fail_init_mfi:
2670 if (instance->evt_detail)
2671 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2672 instance->evt_detail,
2673 instance->evt_detail_h);
2674
2675 if (instance->producer)
2676 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2677 instance->producer_h);
2678 if (instance->consumer)
2679 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2680 instance->consumer_h);
2681 scsi_host_put(host);
2682
2683fail_set_dma_mask:
2684fail_ready_state:
2685
2686 pci_disable_device(pdev);
2687
2688 return -ENODEV;
2689}
2690
2691/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002692 * megasas_detach_one - PCI hot"un"plug entry point
2693 * @pdev: PCI device structure
2694 */
2695static void megasas_detach_one(struct pci_dev *pdev)
2696{
2697 int i;
2698 struct Scsi_Host *host;
2699 struct megasas_instance *instance;
2700
2701 instance = pci_get_drvdata(pdev);
2702 host = instance->host;
2703
2704 scsi_remove_host(instance->host);
2705 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05002706 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Sumant Patro5d018ad2006-10-03 13:13:18 -07002707 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002708
2709 /*
2710 * Take the instance off the instance array. Note that we will not
2711 * decrement the max_index. We let this array be sparse array
2712 */
2713 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2714 if (megasas_mgmt_info.instance[i] == instance) {
2715 megasas_mgmt_info.count--;
2716 megasas_mgmt_info.instance[i] = NULL;
2717
2718 break;
2719 }
2720 }
2721
2722 pci_set_drvdata(instance->pdev, NULL);
2723
Sumant Patrob274cab2006-10-03 12:52:12 -07002724 instance->instancet->disable_intr(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002725
2726 free_irq(instance->pdev->irq, instance);
2727
2728 megasas_release_mfi(instance);
2729
2730 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2731 instance->evt_detail, instance->evt_detail_h);
2732
2733 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2734 instance->producer_h);
2735
2736 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2737 instance->consumer_h);
2738
2739 scsi_host_put(host);
2740
2741 pci_set_drvdata(pdev, NULL);
2742
2743 pci_disable_device(pdev);
2744
2745 return;
2746}
2747
2748/**
2749 * megasas_shutdown - Shutdown entry point
2750 * @device: Generic device structure
2751 */
2752static void megasas_shutdown(struct pci_dev *pdev)
2753{
2754 struct megasas_instance *instance = pci_get_drvdata(pdev);
2755 megasas_flush_cache(instance);
2756}
2757
2758/**
2759 * megasas_mgmt_open - char node "open" entry point
2760 */
2761static int megasas_mgmt_open(struct inode *inode, struct file *filep)
2762{
2763 /*
2764 * Allow only those users with admin rights
2765 */
2766 if (!capable(CAP_SYS_ADMIN))
2767 return -EACCES;
2768
2769 return 0;
2770}
2771
2772/**
2773 * megasas_mgmt_release - char node "release" entry point
2774 */
2775static int megasas_mgmt_release(struct inode *inode, struct file *filep)
2776{
2777 filep->private_data = NULL;
2778 fasync_helper(-1, filep, 0, &megasas_async_queue);
2779
2780 return 0;
2781}
2782
2783/**
2784 * megasas_mgmt_fasync - Async notifier registration from applications
2785 *
2786 * This function adds the calling process to a driver global queue. When an
2787 * event occurs, SIGIO will be sent to all processes in this queue.
2788 */
2789static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
2790{
2791 int rc;
2792
Arjan van de Ven0b950672006-01-11 13:16:10 +01002793 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002794
2795 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
2796
Arjan van de Ven0b950672006-01-11 13:16:10 +01002797 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002798
2799 if (rc >= 0) {
2800 /* For sanity check when we get ioctl */
2801 filep->private_data = filep;
2802 return 0;
2803 }
2804
2805 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
2806
2807 return rc;
2808}
2809
2810/**
2811 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
2812 * @instance: Adapter soft state
2813 * @argp: User's ioctl packet
2814 */
2815static int
2816megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
2817 struct megasas_iocpacket __user * user_ioc,
2818 struct megasas_iocpacket *ioc)
2819{
2820 struct megasas_sge32 *kern_sge32;
2821 struct megasas_cmd *cmd;
2822 void *kbuff_arr[MAX_IOCTL_SGE];
2823 dma_addr_t buf_handle = 0;
2824 int error = 0, i;
2825 void *sense = NULL;
2826 dma_addr_t sense_handle;
2827 u32 *sense_ptr;
2828
2829 memset(kbuff_arr, 0, sizeof(kbuff_arr));
2830
2831 if (ioc->sge_count > MAX_IOCTL_SGE) {
2832 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
2833 ioc->sge_count, MAX_IOCTL_SGE);
2834 return -EINVAL;
2835 }
2836
2837 cmd = megasas_get_cmd(instance);
2838 if (!cmd) {
2839 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
2840 return -ENOMEM;
2841 }
2842
2843 /*
2844 * User's IOCTL packet has 2 frames (maximum). Copy those two
2845 * frames into our cmd's frames. cmd->frame's context will get
2846 * overwritten when we copy from user's frames. So set that value
2847 * alone separately
2848 */
2849 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
2850 cmd->frame->hdr.context = cmd->index;
2851
2852 /*
2853 * The management interface between applications and the fw uses
2854 * MFI frames. E.g, RAID configuration changes, LD property changes
2855 * etc are accomplishes through different kinds of MFI frames. The
2856 * driver needs to care only about substituting user buffers with
2857 * kernel buffers in SGLs. The location of SGL is embedded in the
2858 * struct iocpacket itself.
2859 */
2860 kern_sge32 = (struct megasas_sge32 *)
2861 ((unsigned long)cmd->frame + ioc->sgl_off);
2862
2863 /*
2864 * For each user buffer, create a mirror buffer and copy in
2865 */
2866 for (i = 0; i < ioc->sge_count; i++) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08002867 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002868 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08002869 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002870 if (!kbuff_arr[i]) {
2871 printk(KERN_DEBUG "megasas: Failed to alloc "
2872 "kernel SGL buffer for IOCTL \n");
2873 error = -ENOMEM;
2874 goto out;
2875 }
2876
2877 /*
2878 * We don't change the dma_coherent_mask, so
2879 * pci_alloc_consistent only returns 32bit addresses
2880 */
2881 kern_sge32[i].phys_addr = (u32) buf_handle;
2882 kern_sge32[i].length = ioc->sgl[i].iov_len;
2883
2884 /*
2885 * We created a kernel buffer corresponding to the
2886 * user buffer. Now copy in from the user buffer
2887 */
2888 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
2889 (u32) (ioc->sgl[i].iov_len))) {
2890 error = -EFAULT;
2891 goto out;
2892 }
2893 }
2894
2895 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08002896 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
2897 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002898 if (!sense) {
2899 error = -ENOMEM;
2900 goto out;
2901 }
2902
2903 sense_ptr =
2904 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
2905 *sense_ptr = sense_handle;
2906 }
2907
2908 /*
2909 * Set the sync_cmd flag so that the ISR knows not to complete this
2910 * cmd to the SCSI mid-layer
2911 */
2912 cmd->sync_cmd = 1;
2913 megasas_issue_blocked_cmd(instance, cmd);
2914 cmd->sync_cmd = 0;
2915
2916 /*
2917 * copy out the kernel buffers to user buffers
2918 */
2919 for (i = 0; i < ioc->sge_count; i++) {
2920 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
2921 ioc->sgl[i].iov_len)) {
2922 error = -EFAULT;
2923 goto out;
2924 }
2925 }
2926
2927 /*
2928 * copy out the sense
2929 */
2930 if (ioc->sense_len) {
2931 /*
2932 * sense_ptr points to the location that has the user
2933 * sense buffer address
2934 */
2935 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
2936 ioc->sense_off);
2937
2938 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
2939 sense, ioc->sense_len)) {
2940 error = -EFAULT;
2941 goto out;
2942 }
2943 }
2944
2945 /*
2946 * copy the status codes returned by the fw
2947 */
2948 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
2949 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
2950 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
2951 error = -EFAULT;
2952 }
2953
2954 out:
2955 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08002956 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002957 sense, sense_handle);
2958 }
2959
2960 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08002961 dma_free_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002962 kern_sge32[i].length,
2963 kbuff_arr[i], kern_sge32[i].phys_addr);
2964 }
2965
2966 megasas_return_cmd(instance, cmd);
2967 return error;
2968}
2969
2970static struct megasas_instance *megasas_lookup_instance(u16 host_no)
2971{
2972 int i;
2973
2974 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2975
2976 if ((megasas_mgmt_info.instance[i]) &&
2977 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
2978 return megasas_mgmt_info.instance[i];
2979 }
2980
2981 return NULL;
2982}
2983
2984static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
2985{
2986 struct megasas_iocpacket __user *user_ioc =
2987 (struct megasas_iocpacket __user *)arg;
2988 struct megasas_iocpacket *ioc;
2989 struct megasas_instance *instance;
2990 int error;
2991
2992 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
2993 if (!ioc)
2994 return -ENOMEM;
2995
2996 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
2997 error = -EFAULT;
2998 goto out_kfree_ioc;
2999 }
3000
3001 instance = megasas_lookup_instance(ioc->host_no);
3002 if (!instance) {
3003 error = -ENODEV;
3004 goto out_kfree_ioc;
3005 }
3006
3007 /*
3008 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
3009 */
3010 if (down_interruptible(&instance->ioctl_sem)) {
3011 error = -ERESTARTSYS;
3012 goto out_kfree_ioc;
3013 }
3014 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
3015 up(&instance->ioctl_sem);
3016
3017 out_kfree_ioc:
3018 kfree(ioc);
3019 return error;
3020}
3021
3022static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
3023{
3024 struct megasas_instance *instance;
3025 struct megasas_aen aen;
3026 int error;
3027
3028 if (file->private_data != file) {
3029 printk(KERN_DEBUG "megasas: fasync_helper was not "
3030 "called first\n");
3031 return -EINVAL;
3032 }
3033
3034 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
3035 return -EFAULT;
3036
3037 instance = megasas_lookup_instance(aen.host_no);
3038
3039 if (!instance)
3040 return -ENODEV;
3041
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02003042 mutex_lock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003043 error = megasas_register_aen(instance, aen.seq_num,
3044 aen.class_locale_word);
Matthias Kaehlckee5a69e22007-10-27 09:48:46 +02003045 mutex_unlock(&instance->aen_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003046 return error;
3047}
3048
3049/**
3050 * megasas_mgmt_ioctl - char node ioctl entry point
3051 */
3052static long
3053megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3054{
3055 switch (cmd) {
3056 case MEGASAS_IOC_FIRMWARE:
3057 return megasas_mgmt_ioctl_fw(file, arg);
3058
3059 case MEGASAS_IOC_GET_AEN:
3060 return megasas_mgmt_ioctl_aen(file, arg);
3061 }
3062
3063 return -ENOTTY;
3064}
3065
3066#ifdef CONFIG_COMPAT
3067static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
3068{
3069 struct compat_megasas_iocpacket __user *cioc =
3070 (struct compat_megasas_iocpacket __user *)arg;
3071 struct megasas_iocpacket __user *ioc =
3072 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
3073 int i;
3074 int error = 0;
3075
Jeff Garzik83aabc12006-10-04 06:34:03 -04003076 if (clear_user(ioc, sizeof(*ioc)))
3077 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003078
3079 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
3080 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
3081 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
3082 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
3083 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
3084 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
3085 return -EFAULT;
3086
3087 for (i = 0; i < MAX_IOCTL_SGE; i++) {
3088 compat_uptr_t ptr;
3089
3090 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
3091 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
3092 copy_in_user(&ioc->sgl[i].iov_len,
3093 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
3094 return -EFAULT;
3095 }
3096
3097 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
3098
3099 if (copy_in_user(&cioc->frame.hdr.cmd_status,
3100 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
3101 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
3102 return -EFAULT;
3103 }
3104 return error;
3105}
3106
3107static long
3108megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
3109 unsigned long arg)
3110{
3111 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08003112 case MEGASAS_IOC_FIRMWARE32:
3113 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003114 case MEGASAS_IOC_GET_AEN:
3115 return megasas_mgmt_ioctl_aen(file, arg);
3116 }
3117
3118 return -ENOTTY;
3119}
3120#endif
3121
3122/*
3123 * File operations structure for management interface
3124 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08003125static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003126 .owner = THIS_MODULE,
3127 .open = megasas_mgmt_open,
3128 .release = megasas_mgmt_release,
3129 .fasync = megasas_mgmt_fasync,
3130 .unlocked_ioctl = megasas_mgmt_ioctl,
3131#ifdef CONFIG_COMPAT
3132 .compat_ioctl = megasas_mgmt_compat_ioctl,
3133#endif
3134};
3135
3136/*
3137 * PCI hotplug support registration structure
3138 */
3139static struct pci_driver megasas_pci_driver = {
3140
3141 .name = "megaraid_sas",
3142 .id_table = megasas_pci_table,
3143 .probe = megasas_probe_one,
3144 .remove = __devexit_p(megasas_detach_one),
bo yang31ea7082007-11-07 12:09:50 -05003145 .suspend = megasas_suspend,
3146 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003147 .shutdown = megasas_shutdown,
3148};
3149
3150/*
3151 * Sysfs driver attributes
3152 */
3153static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
3154{
3155 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
3156 MEGASAS_VERSION);
3157}
3158
3159static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
3160
3161static ssize_t
3162megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
3163{
3164 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
3165 MEGASAS_RELDATE);
3166}
3167
3168static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
3169 NULL);
3170
Sumant Patro658dced2006-10-03 13:09:14 -07003171static ssize_t
3172megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
3173{
3174 return sprintf(buf,"%u",megasas_dbg_lvl);
3175}
3176
3177static ssize_t
3178megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
3179{
3180 int retval = count;
3181 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
3182 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
3183 retval = -EINVAL;
3184 }
3185 return retval;
3186}
3187
3188static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUGO, megasas_sysfs_show_dbg_lvl,
3189 megasas_sysfs_set_dbg_lvl);
3190
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003191/**
3192 * megasas_init - Driver load entry point
3193 */
3194static int __init megasas_init(void)
3195{
3196 int rval;
3197
3198 /*
3199 * Announce driver version and other information
3200 */
3201 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
3202 MEGASAS_EXT_VERSION);
3203
3204 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
3205
3206 /*
3207 * Register character device node
3208 */
3209 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
3210
3211 if (rval < 0) {
3212 printk(KERN_DEBUG "megasas: failed to open device node\n");
3213 return rval;
3214 }
3215
3216 megasas_mgmt_majorno = rval;
3217
3218 /*
3219 * Register ourselves as PCI hotplug module
3220 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00003221 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003222
3223 if (rval) {
3224 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04003225 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003226 }
3227
Jeff Garzik83aabc12006-10-04 06:34:03 -04003228 rval = driver_create_file(&megasas_pci_driver.driver,
3229 &driver_attr_version);
3230 if (rval)
3231 goto err_dcf_attr_ver;
3232 rval = driver_create_file(&megasas_pci_driver.driver,
3233 &driver_attr_release_date);
3234 if (rval)
3235 goto err_dcf_rel_date;
3236 rval = driver_create_file(&megasas_pci_driver.driver,
3237 &driver_attr_dbg_lvl);
3238 if (rval)
3239 goto err_dcf_dbg_lvl;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003240
3241 return rval;
Jeff Garzik83aabc12006-10-04 06:34:03 -04003242err_dcf_dbg_lvl:
3243 driver_remove_file(&megasas_pci_driver.driver,
3244 &driver_attr_release_date);
3245err_dcf_rel_date:
3246 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3247err_dcf_attr_ver:
3248 pci_unregister_driver(&megasas_pci_driver);
3249err_pcidrv:
3250 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3251 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003252}
3253
3254/**
3255 * megasas_exit - Driver unload entry point
3256 */
3257static void __exit megasas_exit(void)
3258{
Sumant Patro658dced2006-10-03 13:09:14 -07003259 driver_remove_file(&megasas_pci_driver.driver,
3260 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04003261 driver_remove_file(&megasas_pci_driver.driver,
3262 &driver_attr_release_date);
3263 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003264
3265 pci_unregister_driver(&megasas_pci_driver);
3266 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3267}
3268
3269module_init(megasas_init);
3270module_exit(megasas_exit);