Commit | Line | Data |
---|---|---|
c4a3e0a5 BS |
1 | /* |
2 | * | |
3 | * Linux MegaRAID driver for SAS based RAID controllers | |
4 | * | |
f28cd7cf | 5 | * Copyright (c) 2003-2005 LSI Corporation. |
c4a3e0a5 BS |
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 | |
6610a6b3 | 13 | * Version : v00.00.04.01-rc1 |
c4a3e0a5 BS |
14 | * |
15 | * Authors: | |
cc5968c8 SP |
16 | * (email-id : megaraidlinux@lsi.com) |
17 | * Sreenivas Bagalkote | |
18 | * Sumant Patro | |
19 | * Bo Yang | |
c4a3e0a5 BS |
20 | * |
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> | |
c4a3e0a5 BS |
31 | #include <linux/moduleparam.h> |
32 | #include <linux/module.h> | |
33 | #include <linux/spinlock.h> | |
34 | #include <linux/interrupt.h> | |
35 | #include <linux/delay.h> | |
f2b9857e | 36 | #include <linux/smp_lock.h> |
c4a3e0a5 BS |
37 | #include <linux/uio.h> |
38 | #include <asm/uaccess.h> | |
43399236 | 39 | #include <linux/fs.h> |
c4a3e0a5 | 40 | #include <linux/compat.h> |
cf62a0a5 | 41 | #include <linux/blkdev.h> |
0b950672 | 42 | #include <linux/mutex.h> |
c3518837 | 43 | #include <linux/poll.h> |
c4a3e0a5 BS |
44 | |
45 | #include <scsi/scsi.h> | |
46 | #include <scsi/scsi_cmnd.h> | |
47 | #include <scsi/scsi_device.h> | |
48 | #include <scsi/scsi_host.h> | |
49 | #include "megaraid_sas.h" | |
50 | ||
ad84db2e | 51 | /* |
52 | * poll_mode_io:1- schedule complete completion from q cmd | |
53 | */ | |
54 | static unsigned int poll_mode_io; | |
55 | module_param_named(poll_mode_io, poll_mode_io, int, 0); | |
56 | MODULE_PARM_DESC(poll_mode_io, | |
57 | "Complete cmds from IO path, (default=0)"); | |
58 | ||
c4a3e0a5 BS |
59 | MODULE_LICENSE("GPL"); |
60 | MODULE_VERSION(MEGASAS_VERSION); | |
3d6d174a | 61 | MODULE_AUTHOR("megaraidlinux@lsi.com"); |
f28cd7cf | 62 | MODULE_DESCRIPTION("LSI MegaRAID SAS Driver"); |
c4a3e0a5 BS |
63 | |
64 | /* | |
65 | * PCI ID table for all supported controllers | |
66 | */ | |
67 | static struct pci_device_id megasas_pci_table[] = { | |
68 | ||
f3d7271c HK |
69 | {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)}, |
70 | /* xscale IOP */ | |
71 | {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)}, | |
72 | /* ppc IOP */ | |
af7a5647 | 73 | {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)}, |
74 | /* ppc IOP */ | |
6610a6b3 YB |
75 | {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)}, |
76 | /* gen2*/ | |
77 | {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)}, | |
78 | /* gen2*/ | |
f3d7271c HK |
79 | {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)}, |
80 | /* xscale IOP, vega */ | |
81 | {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)}, | |
82 | /* xscale IOP */ | |
83 | {} | |
c4a3e0a5 BS |
84 | }; |
85 | ||
86 | MODULE_DEVICE_TABLE(pci, megasas_pci_table); | |
87 | ||
88 | static int megasas_mgmt_majorno; | |
89 | static struct megasas_mgmt_info megasas_mgmt_info; | |
90 | static struct fasync_struct *megasas_async_queue; | |
0b950672 | 91 | static DEFINE_MUTEX(megasas_async_queue_mutex); |
c4a3e0a5 | 92 | |
c3518837 YB |
93 | static int megasas_poll_wait_aen; |
94 | static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait); | |
95 | ||
658dcedb SP |
96 | static u32 megasas_dbg_lvl; |
97 | ||
c3518837 YB |
98 | /* define lock for aen poll */ |
99 | spinlock_t poll_aen_lock; | |
100 | ||
7343eb65 | 101 | static void |
102 | megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd, | |
103 | u8 alt_status); | |
104 | ||
c4a3e0a5 BS |
105 | /** |
106 | * megasas_get_cmd - Get a command from the free pool | |
107 | * @instance: Adapter soft state | |
108 | * | |
109 | * Returns a free command from the pool | |
110 | */ | |
858119e1 | 111 | static struct megasas_cmd *megasas_get_cmd(struct megasas_instance |
c4a3e0a5 BS |
112 | *instance) |
113 | { | |
114 | unsigned long flags; | |
115 | struct megasas_cmd *cmd = NULL; | |
116 | ||
117 | spin_lock_irqsave(&instance->cmd_pool_lock, flags); | |
118 | ||
119 | if (!list_empty(&instance->cmd_pool)) { | |
120 | cmd = list_entry((&instance->cmd_pool)->next, | |
121 | struct megasas_cmd, list); | |
122 | list_del_init(&cmd->list); | |
123 | } else { | |
124 | printk(KERN_ERR "megasas: Command pool empty!\n"); | |
125 | } | |
126 | ||
127 | spin_unlock_irqrestore(&instance->cmd_pool_lock, flags); | |
128 | return cmd; | |
129 | } | |
130 | ||
131 | /** | |
132 | * megasas_return_cmd - Return a cmd to free command pool | |
133 | * @instance: Adapter soft state | |
134 | * @cmd: Command packet to be returned to free command pool | |
135 | */ | |
136 | static inline void | |
137 | megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd) | |
138 | { | |
139 | unsigned long flags; | |
140 | ||
141 | spin_lock_irqsave(&instance->cmd_pool_lock, flags); | |
142 | ||
143 | cmd->scmd = NULL; | |
144 | list_add_tail(&cmd->list, &instance->cmd_pool); | |
145 | ||
146 | spin_unlock_irqrestore(&instance->cmd_pool_lock, flags); | |
147 | } | |
148 | ||
1341c939 SP |
149 | |
150 | /** | |
151 | * The following functions are defined for xscale | |
152 | * (deviceid : 1064R, PERC5) controllers | |
153 | */ | |
154 | ||
c4a3e0a5 | 155 | /** |
1341c939 | 156 | * megasas_enable_intr_xscale - Enables interrupts |
c4a3e0a5 BS |
157 | * @regs: MFI register set |
158 | */ | |
159 | static inline void | |
1341c939 | 160 | megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs) |
c4a3e0a5 BS |
161 | { |
162 | writel(1, &(regs)->outbound_intr_mask); | |
163 | ||
164 | /* Dummy readl to force pci flush */ | |
165 | readl(®s->outbound_intr_mask); | |
166 | } | |
167 | ||
b274cab7 SP |
168 | /** |
169 | * megasas_disable_intr_xscale -Disables interrupt | |
170 | * @regs: MFI register set | |
171 | */ | |
172 | static inline void | |
173 | megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs) | |
174 | { | |
175 | u32 mask = 0x1f; | |
176 | writel(mask, ®s->outbound_intr_mask); | |
177 | /* Dummy readl to force pci flush */ | |
178 | readl(®s->outbound_intr_mask); | |
179 | } | |
180 | ||
1341c939 SP |
181 | /** |
182 | * megasas_read_fw_status_reg_xscale - returns the current FW status value | |
183 | * @regs: MFI register set | |
184 | */ | |
185 | static u32 | |
186 | megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs) | |
187 | { | |
188 | return readl(&(regs)->outbound_msg_0); | |
189 | } | |
190 | /** | |
191 | * megasas_clear_interrupt_xscale - Check & clear interrupt | |
192 | * @regs: MFI register set | |
193 | */ | |
194 | static int | |
195 | megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs) | |
196 | { | |
197 | u32 status; | |
198 | /* | |
199 | * Check if it is our interrupt | |
200 | */ | |
201 | status = readl(®s->outbound_intr_status); | |
202 | ||
203 | if (!(status & MFI_OB_INTR_STATUS_MASK)) { | |
204 | return 1; | |
205 | } | |
206 | ||
207 | /* | |
208 | * Clear the interrupt by writing back the same value | |
209 | */ | |
210 | writel(status, ®s->outbound_intr_status); | |
211 | ||
06f579de YB |
212 | /* Dummy readl to force pci flush */ |
213 | readl(®s->outbound_intr_status); | |
214 | ||
1341c939 SP |
215 | return 0; |
216 | } | |
217 | ||
218 | /** | |
219 | * megasas_fire_cmd_xscale - Sends command to the FW | |
220 | * @frame_phys_addr : Physical address of cmd | |
221 | * @frame_count : Number of frames for the command | |
222 | * @regs : MFI register set | |
223 | */ | |
224 | static inline void | |
225 | megasas_fire_cmd_xscale(dma_addr_t frame_phys_addr,u32 frame_count, struct megasas_register_set __iomem *regs) | |
226 | { | |
227 | writel((frame_phys_addr >> 3)|(frame_count), | |
228 | &(regs)->inbound_queue_port); | |
229 | } | |
230 | ||
231 | static struct megasas_instance_template megasas_instance_template_xscale = { | |
232 | ||
233 | .fire_cmd = megasas_fire_cmd_xscale, | |
234 | .enable_intr = megasas_enable_intr_xscale, | |
b274cab7 | 235 | .disable_intr = megasas_disable_intr_xscale, |
1341c939 SP |
236 | .clear_intr = megasas_clear_intr_xscale, |
237 | .read_fw_status_reg = megasas_read_fw_status_reg_xscale, | |
238 | }; | |
239 | ||
240 | /** | |
241 | * This is the end of set of functions & definitions specific | |
242 | * to xscale (deviceid : 1064R, PERC5) controllers | |
243 | */ | |
244 | ||
f9876f0b SP |
245 | /** |
246 | * The following functions are defined for ppc (deviceid : 0x60) | |
247 | * controllers | |
248 | */ | |
249 | ||
250 | /** | |
251 | * megasas_enable_intr_ppc - Enables interrupts | |
252 | * @regs: MFI register set | |
253 | */ | |
254 | static inline void | |
255 | megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs) | |
256 | { | |
257 | writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear); | |
258 | ||
259 | writel(~0x80000004, &(regs)->outbound_intr_mask); | |
260 | ||
261 | /* Dummy readl to force pci flush */ | |
262 | readl(®s->outbound_intr_mask); | |
263 | } | |
264 | ||
b274cab7 SP |
265 | /** |
266 | * megasas_disable_intr_ppc - Disable interrupt | |
267 | * @regs: MFI register set | |
268 | */ | |
269 | static inline void | |
270 | megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs) | |
271 | { | |
272 | u32 mask = 0xFFFFFFFF; | |
273 | writel(mask, ®s->outbound_intr_mask); | |
274 | /* Dummy readl to force pci flush */ | |
275 | readl(®s->outbound_intr_mask); | |
276 | } | |
277 | ||
f9876f0b SP |
278 | /** |
279 | * megasas_read_fw_status_reg_ppc - returns the current FW status value | |
280 | * @regs: MFI register set | |
281 | */ | |
282 | static u32 | |
283 | megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs) | |
284 | { | |
285 | return readl(&(regs)->outbound_scratch_pad); | |
286 | } | |
287 | ||
288 | /** | |
289 | * megasas_clear_interrupt_ppc - Check & clear interrupt | |
290 | * @regs: MFI register set | |
291 | */ | |
292 | static int | |
293 | megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs) | |
294 | { | |
295 | u32 status; | |
296 | /* | |
297 | * Check if it is our interrupt | |
298 | */ | |
299 | status = readl(®s->outbound_intr_status); | |
300 | ||
301 | if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) { | |
302 | return 1; | |
303 | } | |
304 | ||
305 | /* | |
306 | * Clear the interrupt by writing back the same value | |
307 | */ | |
308 | writel(status, ®s->outbound_doorbell_clear); | |
309 | ||
06f579de YB |
310 | /* Dummy readl to force pci flush */ |
311 | readl(®s->outbound_doorbell_clear); | |
312 | ||
f9876f0b SP |
313 | return 0; |
314 | } | |
315 | /** | |
316 | * megasas_fire_cmd_ppc - Sends command to the FW | |
317 | * @frame_phys_addr : Physical address of cmd | |
318 | * @frame_count : Number of frames for the command | |
319 | * @regs : MFI register set | |
320 | */ | |
321 | static inline void | |
322 | megasas_fire_cmd_ppc(dma_addr_t frame_phys_addr, u32 frame_count, struct megasas_register_set __iomem *regs) | |
323 | { | |
324 | writel((frame_phys_addr | (frame_count<<1))|1, | |
325 | &(regs)->inbound_queue_port); | |
326 | } | |
327 | ||
328 | static struct megasas_instance_template megasas_instance_template_ppc = { | |
329 | ||
330 | .fire_cmd = megasas_fire_cmd_ppc, | |
331 | .enable_intr = megasas_enable_intr_ppc, | |
b274cab7 | 332 | .disable_intr = megasas_disable_intr_ppc, |
f9876f0b SP |
333 | .clear_intr = megasas_clear_intr_ppc, |
334 | .read_fw_status_reg = megasas_read_fw_status_reg_ppc, | |
335 | }; | |
336 | ||
6610a6b3 YB |
337 | /** |
338 | * The following functions are defined for gen2 (deviceid : 0x78 0x79) | |
339 | * controllers | |
340 | */ | |
341 | ||
342 | /** | |
343 | * megasas_enable_intr_gen2 - Enables interrupts | |
344 | * @regs: MFI register set | |
345 | */ | |
346 | static inline void | |
347 | megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs) | |
348 | { | |
349 | writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear); | |
350 | ||
351 | /* write ~0x00000005 (4 & 1) to the intr mask*/ | |
352 | writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask); | |
353 | ||
354 | /* Dummy readl to force pci flush */ | |
355 | readl(®s->outbound_intr_mask); | |
356 | } | |
357 | ||
358 | /** | |
359 | * megasas_disable_intr_gen2 - Disables interrupt | |
360 | * @regs: MFI register set | |
361 | */ | |
362 | static inline void | |
363 | megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs) | |
364 | { | |
365 | u32 mask = 0xFFFFFFFF; | |
366 | writel(mask, ®s->outbound_intr_mask); | |
367 | /* Dummy readl to force pci flush */ | |
368 | readl(®s->outbound_intr_mask); | |
369 | } | |
370 | ||
371 | /** | |
372 | * megasas_read_fw_status_reg_gen2 - returns the current FW status value | |
373 | * @regs: MFI register set | |
374 | */ | |
375 | static u32 | |
376 | megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs) | |
377 | { | |
378 | return readl(&(regs)->outbound_scratch_pad); | |
379 | } | |
380 | ||
381 | /** | |
382 | * megasas_clear_interrupt_gen2 - Check & clear interrupt | |
383 | * @regs: MFI register set | |
384 | */ | |
385 | static int | |
386 | megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs) | |
387 | { | |
388 | u32 status; | |
389 | /* | |
390 | * Check if it is our interrupt | |
391 | */ | |
392 | status = readl(®s->outbound_intr_status); | |
393 | ||
394 | if (!(status & MFI_GEN2_ENABLE_INTERRUPT_MASK)) | |
395 | return 1; | |
396 | ||
397 | /* | |
398 | * Clear the interrupt by writing back the same value | |
399 | */ | |
400 | writel(status, ®s->outbound_doorbell_clear); | |
401 | ||
402 | /* Dummy readl to force pci flush */ | |
403 | readl(®s->outbound_intr_status); | |
404 | ||
405 | return 0; | |
406 | } | |
407 | /** | |
408 | * megasas_fire_cmd_gen2 - Sends command to the FW | |
409 | * @frame_phys_addr : Physical address of cmd | |
410 | * @frame_count : Number of frames for the command | |
411 | * @regs : MFI register set | |
412 | */ | |
413 | static inline void | |
414 | megasas_fire_cmd_gen2(dma_addr_t frame_phys_addr, u32 frame_count, | |
415 | struct megasas_register_set __iomem *regs) | |
416 | { | |
417 | writel((frame_phys_addr | (frame_count<<1))|1, | |
418 | &(regs)->inbound_queue_port); | |
419 | } | |
420 | ||
421 | static struct megasas_instance_template megasas_instance_template_gen2 = { | |
422 | ||
423 | .fire_cmd = megasas_fire_cmd_gen2, | |
424 | .enable_intr = megasas_enable_intr_gen2, | |
425 | .disable_intr = megasas_disable_intr_gen2, | |
426 | .clear_intr = megasas_clear_intr_gen2, | |
427 | .read_fw_status_reg = megasas_read_fw_status_reg_gen2, | |
428 | }; | |
429 | ||
f9876f0b SP |
430 | /** |
431 | * This is the end of set of functions & definitions | |
432 | * specific to ppc (deviceid : 0x60) controllers | |
433 | */ | |
434 | ||
c4a3e0a5 BS |
435 | /** |
436 | * megasas_issue_polled - Issues a polling command | |
437 | * @instance: Adapter soft state | |
438 | * @cmd: Command packet to be issued | |
439 | * | |
440 | * For polling, MFI requires the cmd_status to be set to 0xFF before posting. | |
441 | */ | |
442 | static int | |
443 | megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd) | |
444 | { | |
445 | int i; | |
446 | u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000; | |
447 | ||
448 | struct megasas_header *frame_hdr = &cmd->frame->hdr; | |
449 | ||
450 | frame_hdr->cmd_status = 0xFF; | |
451 | frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE; | |
452 | ||
453 | /* | |
454 | * Issue the frame using inbound queue port | |
455 | */ | |
1341c939 | 456 | instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set); |
c4a3e0a5 BS |
457 | |
458 | /* | |
459 | * Wait for cmd_status to change | |
460 | */ | |
461 | for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) { | |
462 | rmb(); | |
463 | msleep(1); | |
464 | } | |
465 | ||
466 | if (frame_hdr->cmd_status == 0xff) | |
467 | return -ETIME; | |
468 | ||
469 | return 0; | |
470 | } | |
471 | ||
472 | /** | |
473 | * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds | |
474 | * @instance: Adapter soft state | |
475 | * @cmd: Command to be issued | |
476 | * | |
477 | * This function waits on an event for the command to be returned from ISR. | |
2a3681e5 | 478 | * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs |
c4a3e0a5 BS |
479 | * Used to issue ioctl commands. |
480 | */ | |
481 | static int | |
482 | megasas_issue_blocked_cmd(struct megasas_instance *instance, | |
483 | struct megasas_cmd *cmd) | |
484 | { | |
485 | cmd->cmd_status = ENODATA; | |
486 | ||
1341c939 | 487 | instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set); |
c4a3e0a5 | 488 | |
2a3681e5 SP |
489 | wait_event_timeout(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA), |
490 | MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ); | |
c4a3e0a5 BS |
491 | |
492 | return 0; | |
493 | } | |
494 | ||
495 | /** | |
496 | * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd | |
497 | * @instance: Adapter soft state | |
498 | * @cmd_to_abort: Previously issued cmd to be aborted | |
499 | * | |
500 | * MFI firmware can abort previously issued AEN comamnd (automatic event | |
501 | * notification). The megasas_issue_blocked_abort_cmd() issues such abort | |
2a3681e5 SP |
502 | * cmd and waits for return status. |
503 | * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs | |
c4a3e0a5 BS |
504 | */ |
505 | static int | |
506 | megasas_issue_blocked_abort_cmd(struct megasas_instance *instance, | |
507 | struct megasas_cmd *cmd_to_abort) | |
508 | { | |
509 | struct megasas_cmd *cmd; | |
510 | struct megasas_abort_frame *abort_fr; | |
511 | ||
512 | cmd = megasas_get_cmd(instance); | |
513 | ||
514 | if (!cmd) | |
515 | return -1; | |
516 | ||
517 | abort_fr = &cmd->frame->abort; | |
518 | ||
519 | /* | |
520 | * Prepare and issue the abort frame | |
521 | */ | |
522 | abort_fr->cmd = MFI_CMD_ABORT; | |
523 | abort_fr->cmd_status = 0xFF; | |
524 | abort_fr->flags = 0; | |
525 | abort_fr->abort_context = cmd_to_abort->index; | |
526 | abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr; | |
527 | abort_fr->abort_mfi_phys_addr_hi = 0; | |
528 | ||
529 | cmd->sync_cmd = 1; | |
530 | cmd->cmd_status = 0xFF; | |
531 | ||
1341c939 | 532 | instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set); |
c4a3e0a5 BS |
533 | |
534 | /* | |
535 | * Wait for this cmd to complete | |
536 | */ | |
2a3681e5 SP |
537 | wait_event_timeout(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF), |
538 | MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ); | |
c4a3e0a5 BS |
539 | |
540 | megasas_return_cmd(instance, cmd); | |
541 | return 0; | |
542 | } | |
543 | ||
544 | /** | |
545 | * megasas_make_sgl32 - Prepares 32-bit SGL | |
546 | * @instance: Adapter soft state | |
547 | * @scp: SCSI command from the mid-layer | |
548 | * @mfi_sgl: SGL to be filled in | |
549 | * | |
550 | * If successful, this function returns the number of SG elements. Otherwise, | |
551 | * it returnes -1. | |
552 | */ | |
858119e1 | 553 | static int |
c4a3e0a5 BS |
554 | megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp, |
555 | union megasas_sgl *mfi_sgl) | |
556 | { | |
557 | int i; | |
558 | int sge_count; | |
559 | struct scatterlist *os_sgl; | |
560 | ||
155d98f0 FT |
561 | sge_count = scsi_dma_map(scp); |
562 | BUG_ON(sge_count < 0); | |
c4a3e0a5 | 563 | |
155d98f0 FT |
564 | if (sge_count) { |
565 | scsi_for_each_sg(scp, os_sgl, sge_count, i) { | |
566 | mfi_sgl->sge32[i].length = sg_dma_len(os_sgl); | |
567 | mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl); | |
568 | } | |
c4a3e0a5 | 569 | } |
c4a3e0a5 BS |
570 | return sge_count; |
571 | } | |
572 | ||
573 | /** | |
574 | * megasas_make_sgl64 - Prepares 64-bit SGL | |
575 | * @instance: Adapter soft state | |
576 | * @scp: SCSI command from the mid-layer | |
577 | * @mfi_sgl: SGL to be filled in | |
578 | * | |
579 | * If successful, this function returns the number of SG elements. Otherwise, | |
580 | * it returnes -1. | |
581 | */ | |
858119e1 | 582 | static int |
c4a3e0a5 BS |
583 | megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp, |
584 | union megasas_sgl *mfi_sgl) | |
585 | { | |
586 | int i; | |
587 | int sge_count; | |
588 | struct scatterlist *os_sgl; | |
589 | ||
155d98f0 FT |
590 | sge_count = scsi_dma_map(scp); |
591 | BUG_ON(sge_count < 0); | |
c4a3e0a5 | 592 | |
155d98f0 FT |
593 | if (sge_count) { |
594 | scsi_for_each_sg(scp, os_sgl, sge_count, i) { | |
595 | mfi_sgl->sge64[i].length = sg_dma_len(os_sgl); | |
596 | mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl); | |
597 | } | |
c4a3e0a5 | 598 | } |
c4a3e0a5 BS |
599 | return sge_count; |
600 | } | |
601 | ||
b1df99d9 SP |
602 | /** |
603 | * megasas_get_frame_count - Computes the number of frames | |
d532dbe2 | 604 | * @frame_type : type of frame- io or pthru frame |
b1df99d9 SP |
605 | * @sge_count : number of sg elements |
606 | * | |
607 | * Returns the number of frames required for numnber of sge's (sge_count) | |
608 | */ | |
609 | ||
d532dbe2 | 610 | static u32 megasas_get_frame_count(u8 sge_count, u8 frame_type) |
b1df99d9 SP |
611 | { |
612 | int num_cnt; | |
613 | int sge_bytes; | |
614 | u32 sge_sz; | |
615 | u32 frame_count=0; | |
616 | ||
617 | sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) : | |
618 | sizeof(struct megasas_sge32); | |
619 | ||
620 | /* | |
d532dbe2 | 621 | * Main frame can contain 2 SGEs for 64-bit SGLs and |
622 | * 3 SGEs for 32-bit SGLs for ldio & | |
623 | * 1 SGEs for 64-bit SGLs and | |
624 | * 2 SGEs for 32-bit SGLs for pthru frame | |
625 | */ | |
626 | if (unlikely(frame_type == PTHRU_FRAME)) { | |
627 | if (IS_DMA64) | |
628 | num_cnt = sge_count - 1; | |
629 | else | |
630 | num_cnt = sge_count - 2; | |
631 | } else { | |
632 | if (IS_DMA64) | |
633 | num_cnt = sge_count - 2; | |
634 | else | |
635 | num_cnt = sge_count - 3; | |
636 | } | |
b1df99d9 SP |
637 | |
638 | if(num_cnt>0){ | |
639 | sge_bytes = sge_sz * num_cnt; | |
640 | ||
641 | frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) + | |
642 | ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ; | |
643 | } | |
644 | /* Main frame */ | |
645 | frame_count +=1; | |
646 | ||
647 | if (frame_count > 7) | |
648 | frame_count = 8; | |
649 | return frame_count; | |
650 | } | |
651 | ||
c4a3e0a5 BS |
652 | /** |
653 | * megasas_build_dcdb - Prepares a direct cdb (DCDB) command | |
654 | * @instance: Adapter soft state | |
655 | * @scp: SCSI command | |
656 | * @cmd: Command to be prepared in | |
657 | * | |
658 | * This function prepares CDB commands. These are typcially pass-through | |
659 | * commands to the devices. | |
660 | */ | |
858119e1 | 661 | static int |
c4a3e0a5 BS |
662 | megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp, |
663 | struct megasas_cmd *cmd) | |
664 | { | |
c4a3e0a5 BS |
665 | u32 is_logical; |
666 | u32 device_id; | |
667 | u16 flags = 0; | |
668 | struct megasas_pthru_frame *pthru; | |
669 | ||
670 | is_logical = MEGASAS_IS_LOGICAL(scp); | |
671 | device_id = MEGASAS_DEV_INDEX(instance, scp); | |
672 | pthru = (struct megasas_pthru_frame *)cmd->frame; | |
673 | ||
674 | if (scp->sc_data_direction == PCI_DMA_TODEVICE) | |
675 | flags = MFI_FRAME_DIR_WRITE; | |
676 | else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) | |
677 | flags = MFI_FRAME_DIR_READ; | |
678 | else if (scp->sc_data_direction == PCI_DMA_NONE) | |
679 | flags = MFI_FRAME_DIR_NONE; | |
680 | ||
681 | /* | |
682 | * Prepare the DCDB frame | |
683 | */ | |
684 | pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO; | |
685 | pthru->cmd_status = 0x0; | |
686 | pthru->scsi_status = 0x0; | |
687 | pthru->target_id = device_id; | |
688 | pthru->lun = scp->device->lun; | |
689 | pthru->cdb_len = scp->cmd_len; | |
690 | pthru->timeout = 0; | |
691 | pthru->flags = flags; | |
155d98f0 | 692 | pthru->data_xfer_len = scsi_bufflen(scp); |
c4a3e0a5 BS |
693 | |
694 | memcpy(pthru->cdb, scp->cmnd, scp->cmd_len); | |
695 | ||
8d568253 YB |
696 | /* |
697 | * If the command is for the tape device, set the | |
698 | * pthru timeout to the os layer timeout value. | |
699 | */ | |
700 | if (scp->device->type == TYPE_TAPE) { | |
701 | if ((scp->request->timeout / HZ) > 0xFFFF) | |
702 | pthru->timeout = 0xFFFF; | |
703 | else | |
704 | pthru->timeout = scp->request->timeout / HZ; | |
705 | } | |
706 | ||
c4a3e0a5 BS |
707 | /* |
708 | * Construct SGL | |
709 | */ | |
c4a3e0a5 BS |
710 | if (IS_DMA64) { |
711 | pthru->flags |= MFI_FRAME_SGL64; | |
712 | pthru->sge_count = megasas_make_sgl64(instance, scp, | |
713 | &pthru->sgl); | |
714 | } else | |
715 | pthru->sge_count = megasas_make_sgl32(instance, scp, | |
716 | &pthru->sgl); | |
717 | ||
718 | /* | |
719 | * Sense info specific | |
720 | */ | |
721 | pthru->sense_len = SCSI_SENSE_BUFFERSIZE; | |
722 | pthru->sense_buf_phys_addr_hi = 0; | |
723 | pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr; | |
724 | ||
c4a3e0a5 BS |
725 | /* |
726 | * Compute the total number of frames this command consumes. FW uses | |
727 | * this number to pull sufficient number of frames from host memory. | |
728 | */ | |
d532dbe2 | 729 | cmd->frame_count = megasas_get_frame_count(pthru->sge_count, |
730 | PTHRU_FRAME); | |
c4a3e0a5 BS |
731 | |
732 | return cmd->frame_count; | |
733 | } | |
734 | ||
735 | /** | |
736 | * megasas_build_ldio - Prepares IOs to logical devices | |
737 | * @instance: Adapter soft state | |
738 | * @scp: SCSI command | |
fd589a8f | 739 | * @cmd: Command to be prepared |
c4a3e0a5 BS |
740 | * |
741 | * Frames (and accompanying SGLs) for regular SCSI IOs use this function. | |
742 | */ | |
858119e1 | 743 | static int |
c4a3e0a5 BS |
744 | megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp, |
745 | struct megasas_cmd *cmd) | |
746 | { | |
c4a3e0a5 BS |
747 | u32 device_id; |
748 | u8 sc = scp->cmnd[0]; | |
749 | u16 flags = 0; | |
750 | struct megasas_io_frame *ldio; | |
751 | ||
752 | device_id = MEGASAS_DEV_INDEX(instance, scp); | |
753 | ldio = (struct megasas_io_frame *)cmd->frame; | |
754 | ||
755 | if (scp->sc_data_direction == PCI_DMA_TODEVICE) | |
756 | flags = MFI_FRAME_DIR_WRITE; | |
757 | else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) | |
758 | flags = MFI_FRAME_DIR_READ; | |
759 | ||
760 | /* | |
b1df99d9 | 761 | * Prepare the Logical IO frame: 2nd bit is zero for all read cmds |
c4a3e0a5 BS |
762 | */ |
763 | ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ; | |
764 | ldio->cmd_status = 0x0; | |
765 | ldio->scsi_status = 0x0; | |
766 | ldio->target_id = device_id; | |
767 | ldio->timeout = 0; | |
768 | ldio->reserved_0 = 0; | |
769 | ldio->pad_0 = 0; | |
770 | ldio->flags = flags; | |
771 | ldio->start_lba_hi = 0; | |
772 | ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0; | |
773 | ||
774 | /* | |
775 | * 6-byte READ(0x08) or WRITE(0x0A) cdb | |
776 | */ | |
777 | if (scp->cmd_len == 6) { | |
778 | ldio->lba_count = (u32) scp->cmnd[4]; | |
779 | ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) | | |
780 | ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3]; | |
781 | ||
782 | ldio->start_lba_lo &= 0x1FFFFF; | |
783 | } | |
784 | ||
785 | /* | |
786 | * 10-byte READ(0x28) or WRITE(0x2A) cdb | |
787 | */ | |
788 | else if (scp->cmd_len == 10) { | |
789 | ldio->lba_count = (u32) scp->cmnd[8] | | |
790 | ((u32) scp->cmnd[7] << 8); | |
791 | ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) | | |
792 | ((u32) scp->cmnd[3] << 16) | | |
793 | ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5]; | |
794 | } | |
795 | ||
796 | /* | |
797 | * 12-byte READ(0xA8) or WRITE(0xAA) cdb | |
798 | */ | |
799 | else if (scp->cmd_len == 12) { | |
800 | ldio->lba_count = ((u32) scp->cmnd[6] << 24) | | |
801 | ((u32) scp->cmnd[7] << 16) | | |
802 | ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9]; | |
803 | ||
804 | ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) | | |
805 | ((u32) scp->cmnd[3] << 16) | | |
806 | ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5]; | |
807 | } | |
808 | ||
809 | /* | |
810 | * 16-byte READ(0x88) or WRITE(0x8A) cdb | |
811 | */ | |
812 | else if (scp->cmd_len == 16) { | |
813 | ldio->lba_count = ((u32) scp->cmnd[10] << 24) | | |
814 | ((u32) scp->cmnd[11] << 16) | | |
815 | ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13]; | |
816 | ||
817 | ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) | | |
818 | ((u32) scp->cmnd[7] << 16) | | |
819 | ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9]; | |
820 | ||
821 | ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) | | |
822 | ((u32) scp->cmnd[3] << 16) | | |
823 | ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5]; | |
824 | ||
825 | } | |
826 | ||
827 | /* | |
828 | * Construct SGL | |
829 | */ | |
c4a3e0a5 BS |
830 | if (IS_DMA64) { |
831 | ldio->flags |= MFI_FRAME_SGL64; | |
832 | ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl); | |
833 | } else | |
834 | ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl); | |
835 | ||
836 | /* | |
837 | * Sense info specific | |
838 | */ | |
839 | ldio->sense_len = SCSI_SENSE_BUFFERSIZE; | |
840 | ldio->sense_buf_phys_addr_hi = 0; | |
841 | ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr; | |
842 | ||
b1df99d9 SP |
843 | /* |
844 | * Compute the total number of frames this command consumes. FW uses | |
845 | * this number to pull sufficient number of frames from host memory. | |
846 | */ | |
d532dbe2 | 847 | cmd->frame_count = megasas_get_frame_count(ldio->sge_count, IO_FRAME); |
c4a3e0a5 BS |
848 | |
849 | return cmd->frame_count; | |
850 | } | |
851 | ||
852 | /** | |
cb59aa6a SP |
853 | * megasas_is_ldio - Checks if the cmd is for logical drive |
854 | * @scmd: SCSI command | |
855 | * | |
856 | * Called by megasas_queue_command to find out if the command to be queued | |
857 | * is a logical drive command | |
c4a3e0a5 | 858 | */ |
cb59aa6a | 859 | static inline int megasas_is_ldio(struct scsi_cmnd *cmd) |
c4a3e0a5 | 860 | { |
cb59aa6a SP |
861 | if (!MEGASAS_IS_LOGICAL(cmd)) |
862 | return 0; | |
863 | switch (cmd->cmnd[0]) { | |
864 | case READ_10: | |
865 | case WRITE_10: | |
866 | case READ_12: | |
867 | case WRITE_12: | |
868 | case READ_6: | |
869 | case WRITE_6: | |
870 | case READ_16: | |
871 | case WRITE_16: | |
872 | return 1; | |
873 | default: | |
874 | return 0; | |
c4a3e0a5 | 875 | } |
c4a3e0a5 BS |
876 | } |
877 | ||
658dcedb SP |
878 | /** |
879 | * megasas_dump_pending_frames - Dumps the frame address of all pending cmds | |
880 | * in FW | |
881 | * @instance: Adapter soft state | |
882 | */ | |
883 | static inline void | |
884 | megasas_dump_pending_frames(struct megasas_instance *instance) | |
885 | { | |
886 | struct megasas_cmd *cmd; | |
887 | int i,n; | |
888 | union megasas_sgl *mfi_sgl; | |
889 | struct megasas_io_frame *ldio; | |
890 | struct megasas_pthru_frame *pthru; | |
891 | u32 sgcount; | |
892 | u32 max_cmd = instance->max_fw_cmds; | |
893 | ||
894 | printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no); | |
895 | printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding)); | |
896 | if (IS_DMA64) | |
897 | printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no); | |
898 | else | |
899 | printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no); | |
900 | ||
901 | printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no); | |
902 | for (i = 0; i < max_cmd; i++) { | |
903 | cmd = instance->cmd_list[i]; | |
904 | if(!cmd->scmd) | |
905 | continue; | |
906 | printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr); | |
907 | if (megasas_is_ldio(cmd->scmd)){ | |
908 | ldio = (struct megasas_io_frame *)cmd->frame; | |
909 | mfi_sgl = &ldio->sgl; | |
910 | sgcount = ldio->sge_count; | |
911 | 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); | |
912 | } | |
913 | else { | |
914 | pthru = (struct megasas_pthru_frame *) cmd->frame; | |
915 | mfi_sgl = &pthru->sgl; | |
916 | sgcount = pthru->sge_count; | |
917 | 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); | |
918 | } | |
919 | if(megasas_dbg_lvl & MEGASAS_DBG_LVL){ | |
920 | for (n = 0; n < sgcount; n++){ | |
921 | if (IS_DMA64) | |
922 | 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) ; | |
923 | else | |
924 | printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ; | |
925 | } | |
926 | } | |
927 | printk(KERN_ERR "\n"); | |
928 | } /*for max_cmd*/ | |
929 | printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no); | |
930 | for (i = 0; i < max_cmd; i++) { | |
931 | ||
932 | cmd = instance->cmd_list[i]; | |
933 | ||
934 | if(cmd->sync_cmd == 1){ | |
935 | printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr); | |
936 | } | |
937 | } | |
938 | printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no); | |
939 | } | |
940 | ||
c4a3e0a5 BS |
941 | /** |
942 | * megasas_queue_command - Queue entry point | |
943 | * @scmd: SCSI command to be queued | |
944 | * @done: Callback entry point | |
945 | */ | |
946 | static int | |
947 | megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *)) | |
948 | { | |
949 | u32 frame_count; | |
c4a3e0a5 BS |
950 | struct megasas_cmd *cmd; |
951 | struct megasas_instance *instance; | |
952 | ||
953 | instance = (struct megasas_instance *) | |
954 | scmd->device->host->hostdata; | |
af37acfb SP |
955 | |
956 | /* Don't process if we have already declared adapter dead */ | |
957 | if (instance->hw_crit_error) | |
958 | return SCSI_MLQUEUE_HOST_BUSY; | |
959 | ||
c4a3e0a5 BS |
960 | scmd->scsi_done = done; |
961 | scmd->result = 0; | |
962 | ||
cb59aa6a SP |
963 | if (MEGASAS_IS_LOGICAL(scmd) && |
964 | (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) { | |
965 | scmd->result = DID_BAD_TARGET << 16; | |
966 | goto out_done; | |
c4a3e0a5 BS |
967 | } |
968 | ||
02b01e01 SP |
969 | switch (scmd->cmnd[0]) { |
970 | case SYNCHRONIZE_CACHE: | |
971 | /* | |
972 | * FW takes care of flush cache on its own | |
973 | * No need to send it down | |
974 | */ | |
975 | scmd->result = DID_OK << 16; | |
976 | goto out_done; | |
977 | default: | |
978 | break; | |
979 | } | |
980 | ||
cb59aa6a SP |
981 | cmd = megasas_get_cmd(instance); |
982 | if (!cmd) | |
983 | return SCSI_MLQUEUE_HOST_BUSY; | |
984 | ||
985 | /* | |
986 | * Logical drive command | |
987 | */ | |
988 | if (megasas_is_ldio(scmd)) | |
989 | frame_count = megasas_build_ldio(instance, scmd, cmd); | |
990 | else | |
991 | frame_count = megasas_build_dcdb(instance, scmd, cmd); | |
992 | ||
993 | if (!frame_count) | |
994 | goto out_return_cmd; | |
995 | ||
c4a3e0a5 | 996 | cmd->scmd = scmd; |
05e9ebbe | 997 | scmd->SCp.ptr = (char *)cmd; |
c4a3e0a5 BS |
998 | |
999 | /* | |
1000 | * Issue the command to the FW | |
1001 | */ | |
e4a082c7 | 1002 | atomic_inc(&instance->fw_outstanding); |
c4a3e0a5 | 1003 | |
1341c939 | 1004 | instance->instancet->fire_cmd(cmd->frame_phys_addr ,cmd->frame_count-1,instance->reg_set); |
ad84db2e | 1005 | /* |
1006 | * Check if we have pend cmds to be completed | |
1007 | */ | |
1008 | if (poll_mode_io && atomic_read(&instance->fw_outstanding)) | |
1009 | tasklet_schedule(&instance->isr_tasklet); | |
1010 | ||
c4a3e0a5 BS |
1011 | |
1012 | return 0; | |
cb59aa6a SP |
1013 | |
1014 | out_return_cmd: | |
1015 | megasas_return_cmd(instance, cmd); | |
1016 | out_done: | |
1017 | done(scmd); | |
1018 | return 0; | |
c4a3e0a5 BS |
1019 | } |
1020 | ||
147aab6a CH |
1021 | static int megasas_slave_configure(struct scsi_device *sdev) |
1022 | { | |
1023 | /* | |
1024 | * Don't export physical disk devices to the disk driver. | |
1025 | * | |
1026 | * FIXME: Currently we don't export them to the midlayer at all. | |
1027 | * That will be fixed once LSI engineers have audited the | |
1028 | * firmware for possible issues. | |
1029 | */ | |
1030 | if (sdev->channel < MEGASAS_MAX_PD_CHANNELS && sdev->type == TYPE_DISK) | |
1031 | return -ENXIO; | |
e5b3a65f CH |
1032 | |
1033 | /* | |
1034 | * The RAID firmware may require extended timeouts. | |
1035 | */ | |
1036 | if (sdev->channel >= MEGASAS_MAX_PD_CHANNELS) | |
ee1ab9e9 JB |
1037 | blk_queue_rq_timeout(sdev->request_queue, |
1038 | MEGASAS_DEFAULT_CMD_TIMEOUT * HZ); | |
147aab6a CH |
1039 | return 0; |
1040 | } | |
1041 | ||
7343eb65 | 1042 | /** |
1043 | * megasas_complete_cmd_dpc - Returns FW's controller structure | |
1044 | * @instance_addr: Address of adapter soft state | |
1045 | * | |
1046 | * Tasklet to complete cmds | |
1047 | */ | |
1048 | static void megasas_complete_cmd_dpc(unsigned long instance_addr) | |
1049 | { | |
1050 | u32 producer; | |
1051 | u32 consumer; | |
1052 | u32 context; | |
1053 | struct megasas_cmd *cmd; | |
1054 | struct megasas_instance *instance = | |
1055 | (struct megasas_instance *)instance_addr; | |
1056 | unsigned long flags; | |
1057 | ||
1058 | /* If we have already declared adapter dead, donot complete cmds */ | |
1059 | if (instance->hw_crit_error) | |
1060 | return; | |
1061 | ||
1062 | spin_lock_irqsave(&instance->completion_lock, flags); | |
1063 | ||
1064 | producer = *instance->producer; | |
1065 | consumer = *instance->consumer; | |
1066 | ||
1067 | while (consumer != producer) { | |
1068 | context = instance->reply_queue[consumer]; | |
1069 | ||
1070 | cmd = instance->cmd_list[context]; | |
1071 | ||
1072 | megasas_complete_cmd(instance, cmd, DID_OK); | |
1073 | ||
1074 | consumer++; | |
1075 | if (consumer == (instance->max_fw_cmds + 1)) { | |
1076 | consumer = 0; | |
1077 | } | |
1078 | } | |
1079 | ||
1080 | *instance->consumer = producer; | |
1081 | ||
1082 | spin_unlock_irqrestore(&instance->completion_lock, flags); | |
1083 | ||
1084 | /* | |
1085 | * Check if we can restore can_queue | |
1086 | */ | |
1087 | if (instance->flag & MEGASAS_FW_BUSY | |
1088 | && time_after(jiffies, instance->last_time + 5 * HZ) | |
1089 | && atomic_read(&instance->fw_outstanding) < 17) { | |
1090 | ||
1091 | spin_lock_irqsave(instance->host->host_lock, flags); | |
1092 | instance->flag &= ~MEGASAS_FW_BUSY; | |
1093 | instance->host->can_queue = | |
1094 | instance->max_fw_cmds - MEGASAS_INT_CMDS; | |
1095 | ||
1096 | spin_unlock_irqrestore(instance->host->host_lock, flags); | |
1097 | } | |
1098 | } | |
1099 | ||
c4a3e0a5 BS |
1100 | /** |
1101 | * megasas_wait_for_outstanding - Wait for all outstanding cmds | |
1102 | * @instance: Adapter soft state | |
1103 | * | |
1104 | * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to | |
1105 | * complete all its outstanding commands. Returns error if one or more IOs | |
1106 | * are pending after this time period. It also marks the controller dead. | |
1107 | */ | |
1108 | static int megasas_wait_for_outstanding(struct megasas_instance *instance) | |
1109 | { | |
1110 | int i; | |
1111 | u32 wait_time = MEGASAS_RESET_WAIT_TIME; | |
1112 | ||
1113 | for (i = 0; i < wait_time; i++) { | |
1114 | ||
e4a082c7 SP |
1115 | int outstanding = atomic_read(&instance->fw_outstanding); |
1116 | ||
1117 | if (!outstanding) | |
c4a3e0a5 BS |
1118 | break; |
1119 | ||
1120 | if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) { | |
1121 | printk(KERN_NOTICE "megasas: [%2d]waiting for %d " | |
e4a082c7 | 1122 | "commands to complete\n",i,outstanding); |
7343eb65 | 1123 | /* |
1124 | * Call cmd completion routine. Cmd to be | |
1125 | * be completed directly without depending on isr. | |
1126 | */ | |
1127 | megasas_complete_cmd_dpc((unsigned long)instance); | |
c4a3e0a5 BS |
1128 | } |
1129 | ||
1130 | msleep(1000); | |
1131 | } | |
1132 | ||
e4a082c7 | 1133 | if (atomic_read(&instance->fw_outstanding)) { |
e3bbff9f SP |
1134 | /* |
1135 | * Send signal to FW to stop processing any pending cmds. | |
1136 | * The controller will be taken offline by the OS now. | |
1137 | */ | |
1138 | writel(MFI_STOP_ADP, | |
1139 | &instance->reg_set->inbound_doorbell); | |
658dcedb | 1140 | megasas_dump_pending_frames(instance); |
c4a3e0a5 BS |
1141 | instance->hw_crit_error = 1; |
1142 | return FAILED; | |
1143 | } | |
1144 | ||
1145 | return SUCCESS; | |
1146 | } | |
1147 | ||
1148 | /** | |
1149 | * megasas_generic_reset - Generic reset routine | |
1150 | * @scmd: Mid-layer SCSI command | |
1151 | * | |
1152 | * This routine implements a generic reset handler for device, bus and host | |
1153 | * reset requests. Device, bus and host specific reset handlers can use this | |
1154 | * function after they do their specific tasks. | |
1155 | */ | |
1156 | static int megasas_generic_reset(struct scsi_cmnd *scmd) | |
1157 | { | |
1158 | int ret_val; | |
1159 | struct megasas_instance *instance; | |
1160 | ||
1161 | instance = (struct megasas_instance *)scmd->device->host->hostdata; | |
1162 | ||
05e9ebbe SP |
1163 | scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n", |
1164 | scmd->serial_number, scmd->cmnd[0], scmd->retries); | |
c4a3e0a5 BS |
1165 | |
1166 | if (instance->hw_crit_error) { | |
1167 | printk(KERN_ERR "megasas: cannot recover from previous reset " | |
1168 | "failures\n"); | |
1169 | return FAILED; | |
1170 | } | |
1171 | ||
c4a3e0a5 | 1172 | ret_val = megasas_wait_for_outstanding(instance); |
c4a3e0a5 BS |
1173 | if (ret_val == SUCCESS) |
1174 | printk(KERN_NOTICE "megasas: reset successful \n"); | |
1175 | else | |
1176 | printk(KERN_ERR "megasas: failed to do reset\n"); | |
1177 | ||
c4a3e0a5 BS |
1178 | return ret_val; |
1179 | } | |
1180 | ||
05e9ebbe SP |
1181 | /** |
1182 | * megasas_reset_timer - quiesce the adapter if required | |
1183 | * @scmd: scsi cmnd | |
1184 | * | |
1185 | * Sets the FW busy flag and reduces the host->can_queue if the | |
1186 | * cmd has not been completed within the timeout period. | |
1187 | */ | |
1188 | static enum | |
242f9dcb | 1189 | blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd) |
05e9ebbe SP |
1190 | { |
1191 | struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr; | |
1192 | struct megasas_instance *instance; | |
1193 | unsigned long flags; | |
1194 | ||
1195 | if (time_after(jiffies, scmd->jiffies_at_alloc + | |
1196 | (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) { | |
242f9dcb | 1197 | return BLK_EH_NOT_HANDLED; |
05e9ebbe SP |
1198 | } |
1199 | ||
1200 | instance = cmd->instance; | |
1201 | if (!(instance->flag & MEGASAS_FW_BUSY)) { | |
1202 | /* FW is busy, throttle IO */ | |
1203 | spin_lock_irqsave(instance->host->host_lock, flags); | |
1204 | ||
1205 | instance->host->can_queue = 16; | |
1206 | instance->last_time = jiffies; | |
1207 | instance->flag |= MEGASAS_FW_BUSY; | |
1208 | ||
1209 | spin_unlock_irqrestore(instance->host->host_lock, flags); | |
1210 | } | |
242f9dcb | 1211 | return BLK_EH_RESET_TIMER; |
05e9ebbe SP |
1212 | } |
1213 | ||
c4a3e0a5 BS |
1214 | /** |
1215 | * megasas_reset_device - Device reset handler entry point | |
1216 | */ | |
1217 | static int megasas_reset_device(struct scsi_cmnd *scmd) | |
1218 | { | |
1219 | int ret; | |
1220 | ||
1221 | /* | |
1222 | * First wait for all commands to complete | |
1223 | */ | |
1224 | ret = megasas_generic_reset(scmd); | |
1225 | ||
1226 | return ret; | |
1227 | } | |
1228 | ||
1229 | /** | |
1230 | * megasas_reset_bus_host - Bus & host reset handler entry point | |
1231 | */ | |
1232 | static int megasas_reset_bus_host(struct scsi_cmnd *scmd) | |
1233 | { | |
1234 | int ret; | |
1235 | ||
1236 | /* | |
80682fa9 | 1237 | * First wait for all commands to complete |
c4a3e0a5 BS |
1238 | */ |
1239 | ret = megasas_generic_reset(scmd); | |
1240 | ||
1241 | return ret; | |
1242 | } | |
1243 | ||
cf62a0a5 SP |
1244 | /** |
1245 | * megasas_bios_param - Returns disk geometry for a disk | |
1246 | * @sdev: device handle | |
1247 | * @bdev: block device | |
1248 | * @capacity: drive capacity | |
1249 | * @geom: geometry parameters | |
1250 | */ | |
1251 | static int | |
1252 | megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev, | |
1253 | sector_t capacity, int geom[]) | |
1254 | { | |
1255 | int heads; | |
1256 | int sectors; | |
1257 | sector_t cylinders; | |
1258 | unsigned long tmp; | |
1259 | /* Default heads (64) & sectors (32) */ | |
1260 | heads = 64; | |
1261 | sectors = 32; | |
1262 | ||
1263 | tmp = heads * sectors; | |
1264 | cylinders = capacity; | |
1265 | ||
1266 | sector_div(cylinders, tmp); | |
1267 | ||
1268 | /* | |
1269 | * Handle extended translation size for logical drives > 1Gb | |
1270 | */ | |
1271 | ||
1272 | if (capacity >= 0x200000) { | |
1273 | heads = 255; | |
1274 | sectors = 63; | |
1275 | tmp = heads*sectors; | |
1276 | cylinders = capacity; | |
1277 | sector_div(cylinders, tmp); | |
1278 | } | |
1279 | ||
1280 | geom[0] = heads; | |
1281 | geom[1] = sectors; | |
1282 | geom[2] = cylinders; | |
1283 | ||
1284 | return 0; | |
1285 | } | |
1286 | ||
c4a3e0a5 BS |
1287 | /** |
1288 | * megasas_service_aen - Processes an event notification | |
1289 | * @instance: Adapter soft state | |
1290 | * @cmd: AEN command completed by the ISR | |
1291 | * | |
1292 | * For AEN, driver sends a command down to FW that is held by the FW till an | |
1293 | * event occurs. When an event of interest occurs, FW completes the command | |
1294 | * that it was previously holding. | |
1295 | * | |
1296 | * This routines sends SIGIO signal to processes that have registered with the | |
1297 | * driver for AEN. | |
1298 | */ | |
1299 | static void | |
1300 | megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd) | |
1301 | { | |
c3518837 | 1302 | unsigned long flags; |
c4a3e0a5 BS |
1303 | /* |
1304 | * Don't signal app if it is just an aborted previously registered aen | |
1305 | */ | |
c3518837 YB |
1306 | if ((!cmd->abort_aen) && (instance->unload == 0)) { |
1307 | spin_lock_irqsave(&poll_aen_lock, flags); | |
1308 | megasas_poll_wait_aen = 1; | |
1309 | spin_unlock_irqrestore(&poll_aen_lock, flags); | |
1310 | wake_up(&megasas_poll_wait); | |
c4a3e0a5 | 1311 | kill_fasync(&megasas_async_queue, SIGIO, POLL_IN); |
c3518837 | 1312 | } |
c4a3e0a5 BS |
1313 | else |
1314 | cmd->abort_aen = 0; | |
1315 | ||
1316 | instance->aen_cmd = NULL; | |
1317 | megasas_return_cmd(instance, cmd); | |
1318 | } | |
1319 | ||
1320 | /* | |
1321 | * Scsi host template for megaraid_sas driver | |
1322 | */ | |
1323 | static struct scsi_host_template megasas_template = { | |
1324 | ||
1325 | .module = THIS_MODULE, | |
f28cd7cf | 1326 | .name = "LSI SAS based MegaRAID driver", |
c4a3e0a5 | 1327 | .proc_name = "megaraid_sas", |
147aab6a | 1328 | .slave_configure = megasas_slave_configure, |
c4a3e0a5 BS |
1329 | .queuecommand = megasas_queue_command, |
1330 | .eh_device_reset_handler = megasas_reset_device, | |
1331 | .eh_bus_reset_handler = megasas_reset_bus_host, | |
1332 | .eh_host_reset_handler = megasas_reset_bus_host, | |
05e9ebbe | 1333 | .eh_timed_out = megasas_reset_timer, |
cf62a0a5 | 1334 | .bios_param = megasas_bios_param, |
c4a3e0a5 BS |
1335 | .use_clustering = ENABLE_CLUSTERING, |
1336 | }; | |
1337 | ||
1338 | /** | |
1339 | * megasas_complete_int_cmd - Completes an internal command | |
1340 | * @instance: Adapter soft state | |
1341 | * @cmd: Command to be completed | |
1342 | * | |
1343 | * The megasas_issue_blocked_cmd() function waits for a command to complete | |
1344 | * after it issues a command. This function wakes up that waiting routine by | |
1345 | * calling wake_up() on the wait queue. | |
1346 | */ | |
1347 | static void | |
1348 | megasas_complete_int_cmd(struct megasas_instance *instance, | |
1349 | struct megasas_cmd *cmd) | |
1350 | { | |
1351 | cmd->cmd_status = cmd->frame->io.cmd_status; | |
1352 | ||
1353 | if (cmd->cmd_status == ENODATA) { | |
1354 | cmd->cmd_status = 0; | |
1355 | } | |
1356 | wake_up(&instance->int_cmd_wait_q); | |
1357 | } | |
1358 | ||
1359 | /** | |
1360 | * megasas_complete_abort - Completes aborting a command | |
1361 | * @instance: Adapter soft state | |
1362 | * @cmd: Cmd that was issued to abort another cmd | |
1363 | * | |
1364 | * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q | |
1365 | * after it issues an abort on a previously issued command. This function | |
1366 | * wakes up all functions waiting on the same wait queue. | |
1367 | */ | |
1368 | static void | |
1369 | megasas_complete_abort(struct megasas_instance *instance, | |
1370 | struct megasas_cmd *cmd) | |
1371 | { | |
1372 | if (cmd->sync_cmd) { | |
1373 | cmd->sync_cmd = 0; | |
1374 | cmd->cmd_status = 0; | |
1375 | wake_up(&instance->abort_cmd_wait_q); | |
1376 | } | |
1377 | ||
1378 | return; | |
1379 | } | |
1380 | ||
c4a3e0a5 BS |
1381 | /** |
1382 | * megasas_complete_cmd - Completes a command | |
1383 | * @instance: Adapter soft state | |
1384 | * @cmd: Command to be completed | |
1385 | * @alt_status: If non-zero, use this value as status to | |
1386 | * SCSI mid-layer instead of the value returned | |
1387 | * by the FW. This should be used if caller wants | |
1388 | * an alternate status (as in the case of aborted | |
1389 | * commands) | |
1390 | */ | |
858119e1 | 1391 | static void |
c4a3e0a5 BS |
1392 | megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd, |
1393 | u8 alt_status) | |
1394 | { | |
1395 | int exception = 0; | |
1396 | struct megasas_header *hdr = &cmd->frame->hdr; | |
c3518837 | 1397 | unsigned long flags; |
c4a3e0a5 | 1398 | |
05e9ebbe SP |
1399 | if (cmd->scmd) |
1400 | cmd->scmd->SCp.ptr = NULL; | |
c4a3e0a5 BS |
1401 | |
1402 | switch (hdr->cmd) { | |
1403 | ||
1404 | case MFI_CMD_PD_SCSI_IO: | |
1405 | case MFI_CMD_LD_SCSI_IO: | |
1406 | ||
1407 | /* | |
1408 | * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been | |
1409 | * issued either through an IO path or an IOCTL path. If it | |
1410 | * was via IOCTL, we will send it to internal completion. | |
1411 | */ | |
1412 | if (cmd->sync_cmd) { | |
1413 | cmd->sync_cmd = 0; | |
1414 | megasas_complete_int_cmd(instance, cmd); | |
1415 | break; | |
1416 | } | |
1417 | ||
c4a3e0a5 BS |
1418 | case MFI_CMD_LD_READ: |
1419 | case MFI_CMD_LD_WRITE: | |
1420 | ||
1421 | if (alt_status) { | |
1422 | cmd->scmd->result = alt_status << 16; | |
1423 | exception = 1; | |
1424 | } | |
1425 | ||
1426 | if (exception) { | |
1427 | ||
e4a082c7 | 1428 | atomic_dec(&instance->fw_outstanding); |
c4a3e0a5 | 1429 | |
155d98f0 | 1430 | scsi_dma_unmap(cmd->scmd); |
c4a3e0a5 BS |
1431 | cmd->scmd->scsi_done(cmd->scmd); |
1432 | megasas_return_cmd(instance, cmd); | |
1433 | ||
1434 | break; | |
1435 | } | |
1436 | ||
1437 | switch (hdr->cmd_status) { | |
1438 | ||
1439 | case MFI_STAT_OK: | |
1440 | cmd->scmd->result = DID_OK << 16; | |
1441 | break; | |
1442 | ||
1443 | case MFI_STAT_SCSI_IO_FAILED: | |
1444 | case MFI_STAT_LD_INIT_IN_PROGRESS: | |
1445 | cmd->scmd->result = | |
1446 | (DID_ERROR << 16) | hdr->scsi_status; | |
1447 | break; | |
1448 | ||
1449 | case MFI_STAT_SCSI_DONE_WITH_ERROR: | |
1450 | ||
1451 | cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status; | |
1452 | ||
1453 | if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) { | |
1454 | memset(cmd->scmd->sense_buffer, 0, | |
1455 | SCSI_SENSE_BUFFERSIZE); | |
1456 | memcpy(cmd->scmd->sense_buffer, cmd->sense, | |
1457 | hdr->sense_len); | |
1458 | ||
1459 | cmd->scmd->result |= DRIVER_SENSE << 24; | |
1460 | } | |
1461 | ||
1462 | break; | |
1463 | ||
1464 | case MFI_STAT_LD_OFFLINE: | |
1465 | case MFI_STAT_DEVICE_NOT_FOUND: | |
1466 | cmd->scmd->result = DID_BAD_TARGET << 16; | |
1467 | break; | |
1468 | ||
1469 | default: | |
1470 | printk(KERN_DEBUG "megasas: MFI FW status %#x\n", | |
1471 | hdr->cmd_status); | |
1472 | cmd->scmd->result = DID_ERROR << 16; | |
1473 | break; | |
1474 | } | |
1475 | ||
e4a082c7 | 1476 | atomic_dec(&instance->fw_outstanding); |
c4a3e0a5 | 1477 | |
155d98f0 | 1478 | scsi_dma_unmap(cmd->scmd); |
c4a3e0a5 BS |
1479 | cmd->scmd->scsi_done(cmd->scmd); |
1480 | megasas_return_cmd(instance, cmd); | |
1481 | ||
1482 | break; | |
1483 | ||
1484 | case MFI_CMD_SMP: | |
1485 | case MFI_CMD_STP: | |
1486 | case MFI_CMD_DCMD: | |
c3518837 YB |
1487 | if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO || |
1488 | cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) { | |
1489 | spin_lock_irqsave(&poll_aen_lock, flags); | |
1490 | megasas_poll_wait_aen = 0; | |
1491 | spin_unlock_irqrestore(&poll_aen_lock, flags); | |
1492 | } | |
c4a3e0a5 BS |
1493 | |
1494 | /* | |
1495 | * See if got an event notification | |
1496 | */ | |
1497 | if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT) | |
1498 | megasas_service_aen(instance, cmd); | |
1499 | else | |
1500 | megasas_complete_int_cmd(instance, cmd); | |
1501 | ||
1502 | break; | |
1503 | ||
1504 | case MFI_CMD_ABORT: | |
1505 | /* | |
1506 | * Cmd issued to abort another cmd returned | |
1507 | */ | |
1508 | megasas_complete_abort(instance, cmd); | |
1509 | break; | |
1510 | ||
1511 | default: | |
1512 | printk("megasas: Unknown command completed! [0x%X]\n", | |
1513 | hdr->cmd); | |
1514 | break; | |
1515 | } | |
1516 | } | |
1517 | ||
1518 | /** | |
1519 | * megasas_deplete_reply_queue - Processes all completed commands | |
1520 | * @instance: Adapter soft state | |
1521 | * @alt_status: Alternate status to be returned to | |
1522 | * SCSI mid-layer instead of the status | |
1523 | * returned by the FW | |
1524 | */ | |
858119e1 | 1525 | static int |
c4a3e0a5 BS |
1526 | megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status) |
1527 | { | |
c4a3e0a5 BS |
1528 | /* |
1529 | * Check if it is our interrupt | |
1341c939 | 1530 | * Clear the interrupt |
c4a3e0a5 | 1531 | */ |
1341c939 | 1532 | if(instance->instancet->clear_intr(instance->reg_set)) |
c4a3e0a5 | 1533 | return IRQ_NONE; |
c4a3e0a5 | 1534 | |
af37acfb SP |
1535 | if (instance->hw_crit_error) |
1536 | goto out_done; | |
5d018ad0 SP |
1537 | /* |
1538 | * Schedule the tasklet for cmd completion | |
1539 | */ | |
1540 | tasklet_schedule(&instance->isr_tasklet); | |
af37acfb | 1541 | out_done: |
c4a3e0a5 BS |
1542 | return IRQ_HANDLED; |
1543 | } | |
1544 | ||
1545 | /** | |
1546 | * megasas_isr - isr entry point | |
1547 | */ | |
7d12e780 | 1548 | static irqreturn_t megasas_isr(int irq, void *devp) |
c4a3e0a5 BS |
1549 | { |
1550 | return megasas_deplete_reply_queue((struct megasas_instance *)devp, | |
1551 | DID_OK); | |
1552 | } | |
1553 | ||
1554 | /** | |
1555 | * megasas_transition_to_ready - Move the FW to READY state | |
1341c939 | 1556 | * @instance: Adapter soft state |
c4a3e0a5 BS |
1557 | * |
1558 | * During the initialization, FW passes can potentially be in any one of | |
1559 | * several possible states. If the FW in operational, waiting-for-handshake | |
1560 | * states, driver must take steps to bring it to ready state. Otherwise, it | |
1561 | * has to wait for the ready state. | |
1562 | */ | |
1563 | static int | |
1341c939 | 1564 | megasas_transition_to_ready(struct megasas_instance* instance) |
c4a3e0a5 BS |
1565 | { |
1566 | int i; | |
1567 | u8 max_wait; | |
1568 | u32 fw_state; | |
1569 | u32 cur_state; | |
1570 | ||
1341c939 | 1571 | fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK; |
c4a3e0a5 | 1572 | |
e3bbff9f SP |
1573 | if (fw_state != MFI_STATE_READY) |
1574 | printk(KERN_INFO "megasas: Waiting for FW to come to ready" | |
1575 | " state\n"); | |
1576 | ||
c4a3e0a5 BS |
1577 | while (fw_state != MFI_STATE_READY) { |
1578 | ||
c4a3e0a5 BS |
1579 | switch (fw_state) { |
1580 | ||
1581 | case MFI_STATE_FAULT: | |
1582 | ||
1583 | printk(KERN_DEBUG "megasas: FW in FAULT state!!\n"); | |
1584 | return -ENODEV; | |
1585 | ||
1586 | case MFI_STATE_WAIT_HANDSHAKE: | |
1587 | /* | |
1588 | * Set the CLR bit in inbound doorbell | |
1589 | */ | |
e3bbff9f | 1590 | writel(MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG, |
1341c939 | 1591 | &instance->reg_set->inbound_doorbell); |
c4a3e0a5 BS |
1592 | |
1593 | max_wait = 2; | |
1594 | cur_state = MFI_STATE_WAIT_HANDSHAKE; | |
1595 | break; | |
1596 | ||
e3bbff9f SP |
1597 | case MFI_STATE_BOOT_MESSAGE_PENDING: |
1598 | writel(MFI_INIT_HOTPLUG, | |
1599 | &instance->reg_set->inbound_doorbell); | |
1600 | ||
1601 | max_wait = 10; | |
1602 | cur_state = MFI_STATE_BOOT_MESSAGE_PENDING; | |
1603 | break; | |
1604 | ||
c4a3e0a5 BS |
1605 | case MFI_STATE_OPERATIONAL: |
1606 | /* | |
e3bbff9f | 1607 | * Bring it to READY state; assuming max wait 10 secs |
c4a3e0a5 | 1608 | */ |
b274cab7 | 1609 | instance->instancet->disable_intr(instance->reg_set); |
e3bbff9f | 1610 | writel(MFI_RESET_FLAGS, &instance->reg_set->inbound_doorbell); |
c4a3e0a5 | 1611 | |
af7a5647 | 1612 | max_wait = 60; |
c4a3e0a5 BS |
1613 | cur_state = MFI_STATE_OPERATIONAL; |
1614 | break; | |
1615 | ||
1616 | case MFI_STATE_UNDEFINED: | |
1617 | /* | |
1618 | * This state should not last for more than 2 seconds | |
1619 | */ | |
1620 | max_wait = 2; | |
1621 | cur_state = MFI_STATE_UNDEFINED; | |
1622 | break; | |
1623 | ||
1624 | case MFI_STATE_BB_INIT: | |
1625 | max_wait = 2; | |
1626 | cur_state = MFI_STATE_BB_INIT; | |
1627 | break; | |
1628 | ||
1629 | case MFI_STATE_FW_INIT: | |
1630 | max_wait = 20; | |
1631 | cur_state = MFI_STATE_FW_INIT; | |
1632 | break; | |
1633 | ||
1634 | case MFI_STATE_FW_INIT_2: | |
1635 | max_wait = 20; | |
1636 | cur_state = MFI_STATE_FW_INIT_2; | |
1637 | break; | |
1638 | ||
1639 | case MFI_STATE_DEVICE_SCAN: | |
1640 | max_wait = 20; | |
1641 | cur_state = MFI_STATE_DEVICE_SCAN; | |
1642 | break; | |
1643 | ||
1644 | case MFI_STATE_FLUSH_CACHE: | |
1645 | max_wait = 20; | |
1646 | cur_state = MFI_STATE_FLUSH_CACHE; | |
1647 | break; | |
1648 | ||
1649 | default: | |
1650 | printk(KERN_DEBUG "megasas: Unknown state 0x%x\n", | |
1651 | fw_state); | |
1652 | return -ENODEV; | |
1653 | } | |
1654 | ||
1655 | /* | |
1656 | * The cur_state should not last for more than max_wait secs | |
1657 | */ | |
1658 | for (i = 0; i < (max_wait * 1000); i++) { | |
1341c939 SP |
1659 | fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & |
1660 | MFI_STATE_MASK ; | |
c4a3e0a5 BS |
1661 | |
1662 | if (fw_state == cur_state) { | |
1663 | msleep(1); | |
1664 | } else | |
1665 | break; | |
1666 | } | |
1667 | ||
1668 | /* | |
1669 | * Return error if fw_state hasn't changed after max_wait | |
1670 | */ | |
1671 | if (fw_state == cur_state) { | |
1672 | printk(KERN_DEBUG "FW state [%d] hasn't changed " | |
1673 | "in %d secs\n", fw_state, max_wait); | |
1674 | return -ENODEV; | |
1675 | } | |
1676 | }; | |
e3bbff9f | 1677 | printk(KERN_INFO "megasas: FW now in Ready state\n"); |
c4a3e0a5 BS |
1678 | |
1679 | return 0; | |
1680 | } | |
1681 | ||
1682 | /** | |
1683 | * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool | |
1684 | * @instance: Adapter soft state | |
1685 | */ | |
1686 | static void megasas_teardown_frame_pool(struct megasas_instance *instance) | |
1687 | { | |
1688 | int i; | |
1689 | u32 max_cmd = instance->max_fw_cmds; | |
1690 | struct megasas_cmd *cmd; | |
1691 | ||
1692 | if (!instance->frame_dma_pool) | |
1693 | return; | |
1694 | ||
1695 | /* | |
1696 | * Return all frames to pool | |
1697 | */ | |
1698 | for (i = 0; i < max_cmd; i++) { | |
1699 | ||
1700 | cmd = instance->cmd_list[i]; | |
1701 | ||
1702 | if (cmd->frame) | |
1703 | pci_pool_free(instance->frame_dma_pool, cmd->frame, | |
1704 | cmd->frame_phys_addr); | |
1705 | ||
1706 | if (cmd->sense) | |
e3bbff9f | 1707 | pci_pool_free(instance->sense_dma_pool, cmd->sense, |
c4a3e0a5 BS |
1708 | cmd->sense_phys_addr); |
1709 | } | |
1710 | ||
1711 | /* | |
1712 | * Now destroy the pool itself | |
1713 | */ | |
1714 | pci_pool_destroy(instance->frame_dma_pool); | |
1715 | pci_pool_destroy(instance->sense_dma_pool); | |
1716 | ||
1717 | instance->frame_dma_pool = NULL; | |
1718 | instance->sense_dma_pool = NULL; | |
1719 | } | |
1720 | ||
1721 | /** | |
1722 | * megasas_create_frame_pool - Creates DMA pool for cmd frames | |
1723 | * @instance: Adapter soft state | |
1724 | * | |
1725 | * Each command packet has an embedded DMA memory buffer that is used for | |
1726 | * filling MFI frame and the SG list that immediately follows the frame. This | |
1727 | * function creates those DMA memory buffers for each command packet by using | |
1728 | * PCI pool facility. | |
1729 | */ | |
1730 | static int megasas_create_frame_pool(struct megasas_instance *instance) | |
1731 | { | |
1732 | int i; | |
1733 | u32 max_cmd; | |
1734 | u32 sge_sz; | |
1735 | u32 sgl_sz; | |
1736 | u32 total_sz; | |
1737 | u32 frame_count; | |
1738 | struct megasas_cmd *cmd; | |
1739 | ||
1740 | max_cmd = instance->max_fw_cmds; | |
1741 | ||
1742 | /* | |
1743 | * Size of our frame is 64 bytes for MFI frame, followed by max SG | |
1744 | * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer | |
1745 | */ | |
1746 | sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) : | |
1747 | sizeof(struct megasas_sge32); | |
1748 | ||
1749 | /* | |
1750 | * Calculated the number of 64byte frames required for SGL | |
1751 | */ | |
1752 | sgl_sz = sge_sz * instance->max_num_sge; | |
1753 | frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE; | |
1754 | ||
1755 | /* | |
1756 | * We need one extra frame for the MFI command | |
1757 | */ | |
1758 | frame_count++; | |
1759 | ||
1760 | total_sz = MEGAMFI_FRAME_SIZE * frame_count; | |
1761 | /* | |
1762 | * Use DMA pool facility provided by PCI layer | |
1763 | */ | |
1764 | instance->frame_dma_pool = pci_pool_create("megasas frame pool", | |
1765 | instance->pdev, total_sz, 64, | |
1766 | 0); | |
1767 | ||
1768 | if (!instance->frame_dma_pool) { | |
1769 | printk(KERN_DEBUG "megasas: failed to setup frame pool\n"); | |
1770 | return -ENOMEM; | |
1771 | } | |
1772 | ||
1773 | instance->sense_dma_pool = pci_pool_create("megasas sense pool", | |
1774 | instance->pdev, 128, 4, 0); | |
1775 | ||
1776 | if (!instance->sense_dma_pool) { | |
1777 | printk(KERN_DEBUG "megasas: failed to setup sense pool\n"); | |
1778 | ||
1779 | pci_pool_destroy(instance->frame_dma_pool); | |
1780 | instance->frame_dma_pool = NULL; | |
1781 | ||
1782 | return -ENOMEM; | |
1783 | } | |
1784 | ||
1785 | /* | |
1786 | * Allocate and attach a frame to each of the commands in cmd_list. | |
1787 | * By making cmd->index as the context instead of the &cmd, we can | |
1788 | * always use 32bit context regardless of the architecture | |
1789 | */ | |
1790 | for (i = 0; i < max_cmd; i++) { | |
1791 | ||
1792 | cmd = instance->cmd_list[i]; | |
1793 | ||
1794 | cmd->frame = pci_pool_alloc(instance->frame_dma_pool, | |
1795 | GFP_KERNEL, &cmd->frame_phys_addr); | |
1796 | ||
1797 | cmd->sense = pci_pool_alloc(instance->sense_dma_pool, | |
1798 | GFP_KERNEL, &cmd->sense_phys_addr); | |
1799 | ||
1800 | /* | |
1801 | * megasas_teardown_frame_pool() takes care of freeing | |
1802 | * whatever has been allocated | |
1803 | */ | |
1804 | if (!cmd->frame || !cmd->sense) { | |
1805 | printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n"); | |
1806 | megasas_teardown_frame_pool(instance); | |
1807 | return -ENOMEM; | |
1808 | } | |
1809 | ||
1810 | cmd->frame->io.context = cmd->index; | |
1811 | } | |
1812 | ||
1813 | return 0; | |
1814 | } | |
1815 | ||
1816 | /** | |
1817 | * megasas_free_cmds - Free all the cmds in the free cmd pool | |
1818 | * @instance: Adapter soft state | |
1819 | */ | |
1820 | static void megasas_free_cmds(struct megasas_instance *instance) | |
1821 | { | |
1822 | int i; | |
1823 | /* First free the MFI frame pool */ | |
1824 | megasas_teardown_frame_pool(instance); | |
1825 | ||
1826 | /* Free all the commands in the cmd_list */ | |
1827 | for (i = 0; i < instance->max_fw_cmds; i++) | |
1828 | kfree(instance->cmd_list[i]); | |
1829 | ||
1830 | /* Free the cmd_list buffer itself */ | |
1831 | kfree(instance->cmd_list); | |
1832 | instance->cmd_list = NULL; | |
1833 | ||
1834 | INIT_LIST_HEAD(&instance->cmd_pool); | |
1835 | } | |
1836 | ||
1837 | /** | |
1838 | * megasas_alloc_cmds - Allocates the command packets | |
1839 | * @instance: Adapter soft state | |
1840 | * | |
1841 | * Each command that is issued to the FW, whether IO commands from the OS or | |
1842 | * internal commands like IOCTLs, are wrapped in local data structure called | |
1843 | * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to | |
1844 | * the FW. | |
1845 | * | |
1846 | * Each frame has a 32-bit field called context (tag). This context is used | |
1847 | * to get back the megasas_cmd from the frame when a frame gets completed in | |
1848 | * the ISR. Typically the address of the megasas_cmd itself would be used as | |
1849 | * the context. But we wanted to keep the differences between 32 and 64 bit | |
1850 | * systems to the mininum. We always use 32 bit integers for the context. In | |
1851 | * this driver, the 32 bit values are the indices into an array cmd_list. | |
1852 | * This array is used only to look up the megasas_cmd given the context. The | |
1853 | * free commands themselves are maintained in a linked list called cmd_pool. | |
1854 | */ | |
1855 | static int megasas_alloc_cmds(struct megasas_instance *instance) | |
1856 | { | |
1857 | int i; | |
1858 | int j; | |
1859 | u32 max_cmd; | |
1860 | struct megasas_cmd *cmd; | |
1861 | ||
1862 | max_cmd = instance->max_fw_cmds; | |
1863 | ||
1864 | /* | |
1865 | * instance->cmd_list is an array of struct megasas_cmd pointers. | |
1866 | * Allocate the dynamic array first and then allocate individual | |
1867 | * commands. | |
1868 | */ | |
dd00cc48 | 1869 | instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL); |
c4a3e0a5 BS |
1870 | |
1871 | if (!instance->cmd_list) { | |
1872 | printk(KERN_DEBUG "megasas: out of memory\n"); | |
1873 | return -ENOMEM; | |
1874 | } | |
1875 | ||
c4a3e0a5 BS |
1876 | |
1877 | for (i = 0; i < max_cmd; i++) { | |
1878 | instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd), | |
1879 | GFP_KERNEL); | |
1880 | ||
1881 | if (!instance->cmd_list[i]) { | |
1882 | ||
1883 | for (j = 0; j < i; j++) | |
1884 | kfree(instance->cmd_list[j]); | |
1885 | ||
1886 | kfree(instance->cmd_list); | |
1887 | instance->cmd_list = NULL; | |
1888 | ||
1889 | return -ENOMEM; | |
1890 | } | |
1891 | } | |
1892 | ||
1893 | /* | |
1894 | * Add all the commands to command pool (instance->cmd_pool) | |
1895 | */ | |
1896 | for (i = 0; i < max_cmd; i++) { | |
1897 | cmd = instance->cmd_list[i]; | |
1898 | memset(cmd, 0, sizeof(struct megasas_cmd)); | |
1899 | cmd->index = i; | |
1900 | cmd->instance = instance; | |
1901 | ||
1902 | list_add_tail(&cmd->list, &instance->cmd_pool); | |
1903 | } | |
1904 | ||
1905 | /* | |
1906 | * Create a frame pool and assign one frame to each cmd | |
1907 | */ | |
1908 | if (megasas_create_frame_pool(instance)) { | |
1909 | printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n"); | |
1910 | megasas_free_cmds(instance); | |
1911 | } | |
1912 | ||
1913 | return 0; | |
1914 | } | |
1915 | ||
1916 | /** | |
1917 | * megasas_get_controller_info - Returns FW's controller structure | |
1918 | * @instance: Adapter soft state | |
1919 | * @ctrl_info: Controller information structure | |
1920 | * | |
1921 | * Issues an internal command (DCMD) to get the FW's controller structure. | |
1922 | * This information is mainly used to find out the maximum IO transfer per | |
1923 | * command supported by the FW. | |
1924 | */ | |
1925 | static int | |
1926 | megasas_get_ctrl_info(struct megasas_instance *instance, | |
1927 | struct megasas_ctrl_info *ctrl_info) | |
1928 | { | |
1929 | int ret = 0; | |
1930 | struct megasas_cmd *cmd; | |
1931 | struct megasas_dcmd_frame *dcmd; | |
1932 | struct megasas_ctrl_info *ci; | |
1933 | dma_addr_t ci_h = 0; | |
1934 | ||
1935 | cmd = megasas_get_cmd(instance); | |
1936 | ||
1937 | if (!cmd) { | |
1938 | printk(KERN_DEBUG "megasas: Failed to get a free cmd\n"); | |
1939 | return -ENOMEM; | |
1940 | } | |
1941 | ||
1942 | dcmd = &cmd->frame->dcmd; | |
1943 | ||
1944 | ci = pci_alloc_consistent(instance->pdev, | |
1945 | sizeof(struct megasas_ctrl_info), &ci_h); | |
1946 | ||
1947 | if (!ci) { | |
1948 | printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n"); | |
1949 | megasas_return_cmd(instance, cmd); | |
1950 | return -ENOMEM; | |
1951 | } | |
1952 | ||
1953 | memset(ci, 0, sizeof(*ci)); | |
1954 | memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE); | |
1955 | ||
1956 | dcmd->cmd = MFI_CMD_DCMD; | |
1957 | dcmd->cmd_status = 0xFF; | |
1958 | dcmd->sge_count = 1; | |
1959 | dcmd->flags = MFI_FRAME_DIR_READ; | |
1960 | dcmd->timeout = 0; | |
1961 | dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info); | |
1962 | dcmd->opcode = MR_DCMD_CTRL_GET_INFO; | |
1963 | dcmd->sgl.sge32[0].phys_addr = ci_h; | |
1964 | dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info); | |
1965 | ||
1966 | if (!megasas_issue_polled(instance, cmd)) { | |
1967 | ret = 0; | |
1968 | memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info)); | |
1969 | } else { | |
1970 | ret = -1; | |
1971 | } | |
1972 | ||
1973 | pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info), | |
1974 | ci, ci_h); | |
1975 | ||
1976 | megasas_return_cmd(instance, cmd); | |
1977 | return ret; | |
1978 | } | |
1979 | ||
31ea7088 | 1980 | /** |
1981 | * megasas_issue_init_mfi - Initializes the FW | |
1982 | * @instance: Adapter soft state | |
1983 | * | |
1984 | * Issues the INIT MFI cmd | |
1985 | */ | |
1986 | static int | |
1987 | megasas_issue_init_mfi(struct megasas_instance *instance) | |
1988 | { | |
1989 | u32 context; | |
1990 | ||
1991 | struct megasas_cmd *cmd; | |
1992 | ||
1993 | struct megasas_init_frame *init_frame; | |
1994 | struct megasas_init_queue_info *initq_info; | |
1995 | dma_addr_t init_frame_h; | |
1996 | dma_addr_t initq_info_h; | |
1997 | ||
1998 | /* | |
1999 | * Prepare a init frame. Note the init frame points to queue info | |
2000 | * structure. Each frame has SGL allocated after first 64 bytes. For | |
2001 | * this frame - since we don't need any SGL - we use SGL's space as | |
2002 | * queue info structure | |
2003 | * | |
2004 | * We will not get a NULL command below. We just created the pool. | |
2005 | */ | |
2006 | cmd = megasas_get_cmd(instance); | |
2007 | ||
2008 | init_frame = (struct megasas_init_frame *)cmd->frame; | |
2009 | initq_info = (struct megasas_init_queue_info *) | |
2010 | ((unsigned long)init_frame + 64); | |
2011 | ||
2012 | init_frame_h = cmd->frame_phys_addr; | |
2013 | initq_info_h = init_frame_h + 64; | |
2014 | ||
2015 | context = init_frame->context; | |
2016 | memset(init_frame, 0, MEGAMFI_FRAME_SIZE); | |
2017 | memset(initq_info, 0, sizeof(struct megasas_init_queue_info)); | |
2018 | init_frame->context = context; | |
2019 | ||
2020 | initq_info->reply_queue_entries = instance->max_fw_cmds + 1; | |
2021 | initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h; | |
2022 | ||
2023 | initq_info->producer_index_phys_addr_lo = instance->producer_h; | |
2024 | initq_info->consumer_index_phys_addr_lo = instance->consumer_h; | |
2025 | ||
2026 | init_frame->cmd = MFI_CMD_INIT; | |
2027 | init_frame->cmd_status = 0xFF; | |
2028 | init_frame->queue_info_new_phys_addr_lo = initq_info_h; | |
2029 | ||
2030 | init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info); | |
2031 | ||
2032 | /* | |
2033 | * disable the intr before firing the init frame to FW | |
2034 | */ | |
2035 | instance->instancet->disable_intr(instance->reg_set); | |
2036 | ||
2037 | /* | |
2038 | * Issue the init frame in polled mode | |
2039 | */ | |
2040 | ||
2041 | if (megasas_issue_polled(instance, cmd)) { | |
2042 | printk(KERN_ERR "megasas: Failed to init firmware\n"); | |
2043 | megasas_return_cmd(instance, cmd); | |
2044 | goto fail_fw_init; | |
2045 | } | |
2046 | ||
2047 | megasas_return_cmd(instance, cmd); | |
2048 | ||
2049 | return 0; | |
2050 | ||
2051 | fail_fw_init: | |
2052 | return -EINVAL; | |
2053 | } | |
2054 | ||
ad84db2e | 2055 | /** |
2056 | * megasas_start_timer - Initializes a timer object | |
2057 | * @instance: Adapter soft state | |
2058 | * @timer: timer object to be initialized | |
2059 | * @fn: timer function | |
2060 | * @interval: time interval between timer function call | |
2061 | */ | |
2062 | static inline void | |
2063 | megasas_start_timer(struct megasas_instance *instance, | |
2064 | struct timer_list *timer, | |
2065 | void *fn, unsigned long interval) | |
2066 | { | |
2067 | init_timer(timer); | |
2068 | timer->expires = jiffies + interval; | |
2069 | timer->data = (unsigned long)instance; | |
2070 | timer->function = fn; | |
2071 | add_timer(timer); | |
2072 | } | |
2073 | ||
2074 | /** | |
2075 | * megasas_io_completion_timer - Timer fn | |
2076 | * @instance_addr: Address of adapter soft state | |
2077 | * | |
2078 | * Schedules tasklet for cmd completion | |
2079 | * if poll_mode_io is set | |
2080 | */ | |
2081 | static void | |
2082 | megasas_io_completion_timer(unsigned long instance_addr) | |
2083 | { | |
2084 | struct megasas_instance *instance = | |
2085 | (struct megasas_instance *)instance_addr; | |
2086 | ||
2087 | if (atomic_read(&instance->fw_outstanding)) | |
2088 | tasklet_schedule(&instance->isr_tasklet); | |
2089 | ||
2090 | /* Restart timer */ | |
2091 | if (poll_mode_io) | |
2092 | mod_timer(&instance->io_completion_timer, | |
2093 | jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL); | |
2094 | } | |
2095 | ||
c4a3e0a5 BS |
2096 | /** |
2097 | * megasas_init_mfi - Initializes the FW | |
2098 | * @instance: Adapter soft state | |
2099 | * | |
2100 | * This is the main function for initializing MFI firmware. | |
2101 | */ | |
2102 | static int megasas_init_mfi(struct megasas_instance *instance) | |
2103 | { | |
2104 | u32 context_sz; | |
2105 | u32 reply_q_sz; | |
2106 | u32 max_sectors_1; | |
2107 | u32 max_sectors_2; | |
14faea9f | 2108 | u32 tmp_sectors; |
c4a3e0a5 | 2109 | struct megasas_register_set __iomem *reg_set; |
c4a3e0a5 | 2110 | struct megasas_ctrl_info *ctrl_info; |
c4a3e0a5 BS |
2111 | /* |
2112 | * Map the message registers | |
2113 | */ | |
6610a6b3 YB |
2114 | if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1078GEN2) || |
2115 | (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0079GEN2)) { | |
2116 | instance->base_addr = pci_resource_start(instance->pdev, 1); | |
2117 | } else { | |
2118 | instance->base_addr = pci_resource_start(instance->pdev, 0); | |
2119 | } | |
c4a3e0a5 | 2120 | |
f28cd7cf | 2121 | if (pci_request_regions(instance->pdev, "megasas: LSI")) { |
c4a3e0a5 BS |
2122 | printk(KERN_DEBUG "megasas: IO memory region busy!\n"); |
2123 | return -EBUSY; | |
2124 | } | |
2125 | ||
2126 | instance->reg_set = ioremap_nocache(instance->base_addr, 8192); | |
2127 | ||
2128 | if (!instance->reg_set) { | |
2129 | printk(KERN_DEBUG "megasas: Failed to map IO mem\n"); | |
2130 | goto fail_ioremap; | |
2131 | } | |
2132 | ||
2133 | reg_set = instance->reg_set; | |
2134 | ||
f9876f0b SP |
2135 | switch(instance->pdev->device) |
2136 | { | |
af7a5647 | 2137 | case PCI_DEVICE_ID_LSI_SAS1078R: |
2138 | case PCI_DEVICE_ID_LSI_SAS1078DE: | |
f9876f0b SP |
2139 | instance->instancet = &megasas_instance_template_ppc; |
2140 | break; | |
6610a6b3 YB |
2141 | case PCI_DEVICE_ID_LSI_SAS1078GEN2: |
2142 | case PCI_DEVICE_ID_LSI_SAS0079GEN2: | |
2143 | instance->instancet = &megasas_instance_template_gen2; | |
2144 | break; | |
f9876f0b SP |
2145 | case PCI_DEVICE_ID_LSI_SAS1064R: |
2146 | case PCI_DEVICE_ID_DELL_PERC5: | |
2147 | default: | |
2148 | instance->instancet = &megasas_instance_template_xscale; | |
2149 | break; | |
2150 | } | |
1341c939 | 2151 | |
c4a3e0a5 BS |
2152 | /* |
2153 | * We expect the FW state to be READY | |
2154 | */ | |
1341c939 | 2155 | if (megasas_transition_to_ready(instance)) |
c4a3e0a5 BS |
2156 | goto fail_ready_state; |
2157 | ||
2158 | /* | |
2159 | * Get various operational parameters from status register | |
2160 | */ | |
1341c939 | 2161 | instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF; |
e3bbff9f SP |
2162 | /* |
2163 | * Reduce the max supported cmds by 1. This is to ensure that the | |
2164 | * reply_q_sz (1 more than the max cmd that driver may send) | |
2165 | * does not exceed max cmds that the FW can support | |
2166 | */ | |
2167 | instance->max_fw_cmds = instance->max_fw_cmds-1; | |
1341c939 SP |
2168 | instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >> |
2169 | 0x10; | |
c4a3e0a5 BS |
2170 | /* |
2171 | * Create a pool of commands | |
2172 | */ | |
2173 | if (megasas_alloc_cmds(instance)) | |
2174 | goto fail_alloc_cmds; | |
2175 | ||
2176 | /* | |
2177 | * Allocate memory for reply queue. Length of reply queue should | |
2178 | * be _one_ more than the maximum commands handled by the firmware. | |
2179 | * | |
2180 | * Note: When FW completes commands, it places corresponding contex | |
2181 | * values in this circular reply queue. This circular queue is a fairly | |
2182 | * typical producer-consumer queue. FW is the producer (of completed | |
2183 | * commands) and the driver is the consumer. | |
2184 | */ | |
2185 | context_sz = sizeof(u32); | |
2186 | reply_q_sz = context_sz * (instance->max_fw_cmds + 1); | |
2187 | ||
2188 | instance->reply_queue = pci_alloc_consistent(instance->pdev, | |
2189 | reply_q_sz, | |
2190 | &instance->reply_queue_h); | |
2191 | ||
2192 | if (!instance->reply_queue) { | |
2193 | printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n"); | |
2194 | goto fail_reply_queue; | |
2195 | } | |
2196 | ||
31ea7088 | 2197 | if (megasas_issue_init_mfi(instance)) |
c4a3e0a5 | 2198 | goto fail_fw_init; |
c4a3e0a5 BS |
2199 | |
2200 | ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL); | |
2201 | ||
2202 | /* | |
2203 | * Compute the max allowed sectors per IO: The controller info has two | |
2204 | * limits on max sectors. Driver should use the minimum of these two. | |
2205 | * | |
2206 | * 1 << stripe_sz_ops.min = max sectors per strip | |
2207 | * | |
2208 | * Note that older firmwares ( < FW ver 30) didn't report information | |
2209 | * to calculate max_sectors_1. So the number ended up as zero always. | |
2210 | */ | |
14faea9f | 2211 | tmp_sectors = 0; |
c4a3e0a5 BS |
2212 | if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) { |
2213 | ||
2214 | max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) * | |
2215 | ctrl_info->max_strips_per_io; | |
2216 | max_sectors_2 = ctrl_info->max_request_size; | |
2217 | ||
14faea9f | 2218 | tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2); |
2219 | } | |
2220 | ||
2221 | instance->max_sectors_per_req = instance->max_num_sge * | |
2222 | PAGE_SIZE / 512; | |
2223 | if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors)) | |
2224 | instance->max_sectors_per_req = tmp_sectors; | |
c4a3e0a5 BS |
2225 | |
2226 | kfree(ctrl_info); | |
2227 | ||
5d018ad0 SP |
2228 | /* |
2229 | * Setup tasklet for cmd completion | |
2230 | */ | |
2231 | ||
ad84db2e | 2232 | tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc, |
2233 | (unsigned long)instance); | |
2234 | ||
2235 | /* Initialize the cmd completion timer */ | |
2236 | if (poll_mode_io) | |
2237 | megasas_start_timer(instance, &instance->io_completion_timer, | |
2238 | megasas_io_completion_timer, | |
2239 | MEGASAS_COMPLETION_TIMER_INTERVAL); | |
c4a3e0a5 BS |
2240 | return 0; |
2241 | ||
2242 | fail_fw_init: | |
c4a3e0a5 BS |
2243 | |
2244 | pci_free_consistent(instance->pdev, reply_q_sz, | |
2245 | instance->reply_queue, instance->reply_queue_h); | |
2246 | fail_reply_queue: | |
2247 | megasas_free_cmds(instance); | |
2248 | ||
2249 | fail_alloc_cmds: | |
2250 | fail_ready_state: | |
2251 | iounmap(instance->reg_set); | |
2252 | ||
2253 | fail_ioremap: | |
2254 | pci_release_regions(instance->pdev); | |
2255 | ||
2256 | return -EINVAL; | |
2257 | } | |
2258 | ||
2259 | /** | |
2260 | * megasas_release_mfi - Reverses the FW initialization | |
2261 | * @intance: Adapter soft state | |
2262 | */ | |
2263 | static void megasas_release_mfi(struct megasas_instance *instance) | |
2264 | { | |
2265 | u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1); | |
2266 | ||
2267 | pci_free_consistent(instance->pdev, reply_q_sz, | |
2268 | instance->reply_queue, instance->reply_queue_h); | |
2269 | ||
2270 | megasas_free_cmds(instance); | |
2271 | ||
2272 | iounmap(instance->reg_set); | |
2273 | ||
2274 | pci_release_regions(instance->pdev); | |
2275 | } | |
2276 | ||
2277 | /** | |
2278 | * megasas_get_seq_num - Gets latest event sequence numbers | |
2279 | * @instance: Adapter soft state | |
2280 | * @eli: FW event log sequence numbers information | |
2281 | * | |
2282 | * FW maintains a log of all events in a non-volatile area. Upper layers would | |
2283 | * usually find out the latest sequence number of the events, the seq number at | |
2284 | * the boot etc. They would "read" all the events below the latest seq number | |
2285 | * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq | |
2286 | * number), they would subsribe to AEN (asynchronous event notification) and | |
2287 | * wait for the events to happen. | |
2288 | */ | |
2289 | static int | |
2290 | megasas_get_seq_num(struct megasas_instance *instance, | |
2291 | struct megasas_evt_log_info *eli) | |
2292 | { | |
2293 | struct megasas_cmd *cmd; | |
2294 | struct megasas_dcmd_frame *dcmd; | |
2295 | struct megasas_evt_log_info *el_info; | |
2296 | dma_addr_t el_info_h = 0; | |
2297 | ||
2298 | cmd = megasas_get_cmd(instance); | |
2299 | ||
2300 | if (!cmd) { | |
2301 | return -ENOMEM; | |
2302 | } | |
2303 | ||
2304 | dcmd = &cmd->frame->dcmd; | |
2305 | el_info = pci_alloc_consistent(instance->pdev, | |
2306 | sizeof(struct megasas_evt_log_info), | |
2307 | &el_info_h); | |
2308 | ||
2309 | if (!el_info) { | |
2310 | megasas_return_cmd(instance, cmd); | |
2311 | return -ENOMEM; | |
2312 | } | |
2313 | ||
2314 | memset(el_info, 0, sizeof(*el_info)); | |
2315 | memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE); | |
2316 | ||
2317 | dcmd->cmd = MFI_CMD_DCMD; | |
2318 | dcmd->cmd_status = 0x0; | |
2319 | dcmd->sge_count = 1; | |
2320 | dcmd->flags = MFI_FRAME_DIR_READ; | |
2321 | dcmd->timeout = 0; | |
2322 | dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info); | |
2323 | dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO; | |
2324 | dcmd->sgl.sge32[0].phys_addr = el_info_h; | |
2325 | dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info); | |
2326 | ||
2327 | megasas_issue_blocked_cmd(instance, cmd); | |
2328 | ||
2329 | /* | |
2330 | * Copy the data back into callers buffer | |
2331 | */ | |
2332 | memcpy(eli, el_info, sizeof(struct megasas_evt_log_info)); | |
2333 | ||
2334 | pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info), | |
2335 | el_info, el_info_h); | |
2336 | ||
2337 | megasas_return_cmd(instance, cmd); | |
2338 | ||
2339 | return 0; | |
2340 | } | |
2341 | ||
2342 | /** | |
2343 | * megasas_register_aen - Registers for asynchronous event notification | |
2344 | * @instance: Adapter soft state | |
2345 | * @seq_num: The starting sequence number | |
2346 | * @class_locale: Class of the event | |
2347 | * | |
2348 | * This function subscribes for AEN for events beyond the @seq_num. It requests | |
2349 | * to be notified if and only if the event is of type @class_locale | |
2350 | */ | |
2351 | static int | |
2352 | megasas_register_aen(struct megasas_instance *instance, u32 seq_num, | |
2353 | u32 class_locale_word) | |
2354 | { | |
2355 | int ret_val; | |
2356 | struct megasas_cmd *cmd; | |
2357 | struct megasas_dcmd_frame *dcmd; | |
2358 | union megasas_evt_class_locale curr_aen; | |
2359 | union megasas_evt_class_locale prev_aen; | |
2360 | ||
2361 | /* | |
2362 | * If there an AEN pending already (aen_cmd), check if the | |
2363 | * class_locale of that pending AEN is inclusive of the new | |
2364 | * AEN request we currently have. If it is, then we don't have | |
2365 | * to do anything. In other words, whichever events the current | |
2366 | * AEN request is subscribing to, have already been subscribed | |
2367 | * to. | |
2368 | * | |
2369 | * If the old_cmd is _not_ inclusive, then we have to abort | |
2370 | * that command, form a class_locale that is superset of both | |
2371 | * old and current and re-issue to the FW | |
2372 | */ | |
2373 | ||
2374 | curr_aen.word = class_locale_word; | |
2375 | ||
2376 | if (instance->aen_cmd) { | |
2377 | ||
2378 | prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1]; | |
2379 | ||
2380 | /* | |
2381 | * A class whose enum value is smaller is inclusive of all | |
2382 | * higher values. If a PROGRESS (= -1) was previously | |
2383 | * registered, then a new registration requests for higher | |
2384 | * classes need not be sent to FW. They are automatically | |
2385 | * included. | |
2386 | * | |
2387 | * Locale numbers don't have such hierarchy. They are bitmap | |
2388 | * values | |
2389 | */ | |
2390 | if ((prev_aen.members.class <= curr_aen.members.class) && | |
2391 | !((prev_aen.members.locale & curr_aen.members.locale) ^ | |
2392 | curr_aen.members.locale)) { | |
2393 | /* | |
2394 | * Previously issued event registration includes | |
2395 | * current request. Nothing to do. | |
2396 | */ | |
2397 | return 0; | |
2398 | } else { | |
2399 | curr_aen.members.locale |= prev_aen.members.locale; | |
2400 | ||
2401 | if (prev_aen.members.class < curr_aen.members.class) | |
2402 | curr_aen.members.class = prev_aen.members.class; | |
2403 | ||
2404 | instance->aen_cmd->abort_aen = 1; | |
2405 | ret_val = megasas_issue_blocked_abort_cmd(instance, | |
2406 | instance-> | |
2407 | aen_cmd); | |
2408 | ||
2409 | if (ret_val) { | |
2410 | printk(KERN_DEBUG "megasas: Failed to abort " | |
2411 | "previous AEN command\n"); | |
2412 | return ret_val; | |
2413 | } | |
2414 | } | |
2415 | } | |
2416 | ||
2417 | cmd = megasas_get_cmd(instance); | |
2418 | ||
2419 | if (!cmd) | |
2420 | return -ENOMEM; | |
2421 | ||
2422 | dcmd = &cmd->frame->dcmd; | |
2423 | ||
2424 | memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail)); | |
2425 | ||
2426 | /* | |
2427 | * Prepare DCMD for aen registration | |
2428 | */ | |
2429 | memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE); | |
2430 | ||
2431 | dcmd->cmd = MFI_CMD_DCMD; | |
2432 | dcmd->cmd_status = 0x0; | |
2433 | dcmd->sge_count = 1; | |
2434 | dcmd->flags = MFI_FRAME_DIR_READ; | |
2435 | dcmd->timeout = 0; | |
2436 | dcmd->data_xfer_len = sizeof(struct megasas_evt_detail); | |
2437 | dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT; | |
2438 | dcmd->mbox.w[0] = seq_num; | |
2439 | dcmd->mbox.w[1] = curr_aen.word; | |
2440 | dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h; | |
2441 | dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail); | |
2442 | ||
2443 | /* | |
2444 | * Store reference to the cmd used to register for AEN. When an | |
2445 | * application wants us to register for AEN, we have to abort this | |
2446 | * cmd and re-register with a new EVENT LOCALE supplied by that app | |
2447 | */ | |
2448 | instance->aen_cmd = cmd; | |
2449 | ||
2450 | /* | |
2451 | * Issue the aen registration frame | |
2452 | */ | |
1341c939 | 2453 | instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set); |
c4a3e0a5 BS |
2454 | |
2455 | return 0; | |
2456 | } | |
2457 | ||
2458 | /** | |
2459 | * megasas_start_aen - Subscribes to AEN during driver load time | |
2460 | * @instance: Adapter soft state | |
2461 | */ | |
2462 | static int megasas_start_aen(struct megasas_instance *instance) | |
2463 | { | |
2464 | struct megasas_evt_log_info eli; | |
2465 | union megasas_evt_class_locale class_locale; | |
2466 | ||
2467 | /* | |
2468 | * Get the latest sequence number from FW | |
2469 | */ | |
2470 | memset(&eli, 0, sizeof(eli)); | |
2471 | ||
2472 | if (megasas_get_seq_num(instance, &eli)) | |
2473 | return -1; | |
2474 | ||
2475 | /* | |
2476 | * Register AEN with FW for latest sequence number plus 1 | |
2477 | */ | |
2478 | class_locale.members.reserved = 0; | |
2479 | class_locale.members.locale = MR_EVT_LOCALE_ALL; | |
2480 | class_locale.members.class = MR_EVT_CLASS_DEBUG; | |
2481 | ||
2482 | return megasas_register_aen(instance, eli.newest_seq_num + 1, | |
2483 | class_locale.word); | |
2484 | } | |
2485 | ||
2486 | /** | |
2487 | * megasas_io_attach - Attaches this driver to SCSI mid-layer | |
2488 | * @instance: Adapter soft state | |
2489 | */ | |
2490 | static int megasas_io_attach(struct megasas_instance *instance) | |
2491 | { | |
2492 | struct Scsi_Host *host = instance->host; | |
2493 | ||
2494 | /* | |
2495 | * Export parameters required by SCSI mid-layer | |
2496 | */ | |
2497 | host->irq = instance->pdev->irq; | |
2498 | host->unique_id = instance->unique_id; | |
2499 | host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS; | |
2500 | host->this_id = instance->init_id; | |
2501 | host->sg_tablesize = instance->max_num_sge; | |
2502 | host->max_sectors = instance->max_sectors_per_req; | |
2503 | host->cmd_per_lun = 128; | |
2504 | host->max_channel = MEGASAS_MAX_CHANNELS - 1; | |
2505 | host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL; | |
2506 | host->max_lun = MEGASAS_MAX_LUN; | |
122da302 | 2507 | host->max_cmd_len = 16; |
c4a3e0a5 BS |
2508 | |
2509 | /* | |
2510 | * Notify the mid-layer about the new controller | |
2511 | */ | |
2512 | if (scsi_add_host(host, &instance->pdev->dev)) { | |
2513 | printk(KERN_DEBUG "megasas: scsi_add_host failed\n"); | |
2514 | return -ENODEV; | |
2515 | } | |
2516 | ||
2517 | /* | |
2518 | * Trigger SCSI to scan our drives | |
2519 | */ | |
2520 | scsi_scan_host(host); | |
2521 | return 0; | |
2522 | } | |
2523 | ||
31ea7088 | 2524 | static int |
2525 | megasas_set_dma_mask(struct pci_dev *pdev) | |
2526 | { | |
2527 | /* | |
2528 | * All our contollers are capable of performing 64-bit DMA | |
2529 | */ | |
2530 | if (IS_DMA64) { | |
6a35528a | 2531 | if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) { |
31ea7088 | 2532 | |
284901a9 | 2533 | if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) |
31ea7088 | 2534 | goto fail_set_dma_mask; |
2535 | } | |
2536 | } else { | |
284901a9 | 2537 | if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) |
31ea7088 | 2538 | goto fail_set_dma_mask; |
2539 | } | |
2540 | return 0; | |
2541 | ||
2542 | fail_set_dma_mask: | |
2543 | return 1; | |
2544 | } | |
2545 | ||
c4a3e0a5 BS |
2546 | /** |
2547 | * megasas_probe_one - PCI hotplug entry point | |
2548 | * @pdev: PCI device structure | |
2549 | * @id: PCI ids of supported hotplugged adapter | |
2550 | */ | |
2551 | static int __devinit | |
2552 | megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id) | |
2553 | { | |
2554 | int rval; | |
2555 | struct Scsi_Host *host; | |
2556 | struct megasas_instance *instance; | |
2557 | ||
2558 | /* | |
2559 | * Announce PCI information | |
2560 | */ | |
2561 | printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ", | |
2562 | pdev->vendor, pdev->device, pdev->subsystem_vendor, | |
2563 | pdev->subsystem_device); | |
2564 | ||
2565 | printk("bus %d:slot %d:func %d\n", | |
2566 | pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)); | |
2567 | ||
2568 | /* | |
2569 | * PCI prepping: enable device set bus mastering and dma mask | |
2570 | */ | |
2571 | rval = pci_enable_device(pdev); | |
2572 | ||
2573 | if (rval) { | |
2574 | return rval; | |
2575 | } | |
2576 | ||
2577 | pci_set_master(pdev); | |
2578 | ||
31ea7088 | 2579 | if (megasas_set_dma_mask(pdev)) |
2580 | goto fail_set_dma_mask; | |
c4a3e0a5 BS |
2581 | |
2582 | host = scsi_host_alloc(&megasas_template, | |
2583 | sizeof(struct megasas_instance)); | |
2584 | ||
2585 | if (!host) { | |
2586 | printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n"); | |
2587 | goto fail_alloc_instance; | |
2588 | } | |
2589 | ||
2590 | instance = (struct megasas_instance *)host->hostdata; | |
2591 | memset(instance, 0, sizeof(*instance)); | |
2592 | ||
2593 | instance->producer = pci_alloc_consistent(pdev, sizeof(u32), | |
2594 | &instance->producer_h); | |
2595 | instance->consumer = pci_alloc_consistent(pdev, sizeof(u32), | |
2596 | &instance->consumer_h); | |
2597 | ||
2598 | if (!instance->producer || !instance->consumer) { | |
2599 | printk(KERN_DEBUG "megasas: Failed to allocate memory for " | |
2600 | "producer, consumer\n"); | |
2601 | goto fail_alloc_dma_buf; | |
2602 | } | |
2603 | ||
2604 | *instance->producer = 0; | |
2605 | *instance->consumer = 0; | |
c3518837 | 2606 | megasas_poll_wait_aen = 0; |
c4a3e0a5 BS |
2607 | |
2608 | instance->evt_detail = pci_alloc_consistent(pdev, | |
2609 | sizeof(struct | |
2610 | megasas_evt_detail), | |
2611 | &instance->evt_detail_h); | |
2612 | ||
2613 | if (!instance->evt_detail) { | |
2614 | printk(KERN_DEBUG "megasas: Failed to allocate memory for " | |
2615 | "event detail structure\n"); | |
2616 | goto fail_alloc_dma_buf; | |
2617 | } | |
2618 | ||
2619 | /* | |
2620 | * Initialize locks and queues | |
2621 | */ | |
2622 | INIT_LIST_HEAD(&instance->cmd_pool); | |
2623 | ||
e4a082c7 SP |
2624 | atomic_set(&instance->fw_outstanding,0); |
2625 | ||
c4a3e0a5 BS |
2626 | init_waitqueue_head(&instance->int_cmd_wait_q); |
2627 | init_waitqueue_head(&instance->abort_cmd_wait_q); | |
2628 | ||
2629 | spin_lock_init(&instance->cmd_pool_lock); | |
7343eb65 | 2630 | spin_lock_init(&instance->completion_lock); |
c3518837 | 2631 | spin_lock_init(&poll_aen_lock); |
c4a3e0a5 | 2632 | |
e5a69e27 | 2633 | mutex_init(&instance->aen_mutex); |
c4a3e0a5 BS |
2634 | sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS); |
2635 | ||
2636 | /* | |
2637 | * Initialize PCI related and misc parameters | |
2638 | */ | |
2639 | instance->pdev = pdev; | |
2640 | instance->host = host; | |
2641 | instance->unique_id = pdev->bus->number << 8 | pdev->devfn; | |
2642 | instance->init_id = MEGASAS_DEFAULT_INIT_ID; | |
2643 | ||
658dcedb | 2644 | megasas_dbg_lvl = 0; |
05e9ebbe | 2645 | instance->flag = 0; |
c3518837 | 2646 | instance->unload = 0; |
05e9ebbe | 2647 | instance->last_time = 0; |
658dcedb | 2648 | |
c4a3e0a5 BS |
2649 | /* |
2650 | * Initialize MFI Firmware | |
2651 | */ | |
2652 | if (megasas_init_mfi(instance)) | |
2653 | goto fail_init_mfi; | |
2654 | ||
2655 | /* | |
2656 | * Register IRQ | |
2657 | */ | |
1d6f359a | 2658 | if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) { |
c4a3e0a5 BS |
2659 | printk(KERN_DEBUG "megasas: Failed to register IRQ\n"); |
2660 | goto fail_irq; | |
2661 | } | |
2662 | ||
1341c939 | 2663 | instance->instancet->enable_intr(instance->reg_set); |
c4a3e0a5 BS |
2664 | |
2665 | /* | |
2666 | * Store instance in PCI softstate | |
2667 | */ | |
2668 | pci_set_drvdata(pdev, instance); | |
2669 | ||
2670 | /* | |
2671 | * Add this controller to megasas_mgmt_info structure so that it | |
2672 | * can be exported to management applications | |
2673 | */ | |
2674 | megasas_mgmt_info.count++; | |
2675 | megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance; | |
2676 | megasas_mgmt_info.max_index++; | |
2677 | ||
2678 | /* | |
2679 | * Initiate AEN (Asynchronous Event Notification) | |
2680 | */ | |
2681 | if (megasas_start_aen(instance)) { | |
2682 | printk(KERN_DEBUG "megasas: start aen failed\n"); | |
2683 | goto fail_start_aen; | |
2684 | } | |
2685 | ||
2686 | /* | |
2687 | * Register with SCSI mid-layer | |
2688 | */ | |
2689 | if (megasas_io_attach(instance)) | |
2690 | goto fail_io_attach; | |
2691 | ||
2692 | return 0; | |
2693 | ||
2694 | fail_start_aen: | |
2695 | fail_io_attach: | |
2696 | megasas_mgmt_info.count--; | |
2697 | megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL; | |
2698 | megasas_mgmt_info.max_index--; | |
2699 | ||
2700 | pci_set_drvdata(pdev, NULL); | |
b274cab7 | 2701 | instance->instancet->disable_intr(instance->reg_set); |
c4a3e0a5 BS |
2702 | free_irq(instance->pdev->irq, instance); |
2703 | ||
2704 | megasas_release_mfi(instance); | |
2705 | ||
2706 | fail_irq: | |
2707 | fail_init_mfi: | |
2708 | fail_alloc_dma_buf: | |
2709 | if (instance->evt_detail) | |
2710 | pci_free_consistent(pdev, sizeof(struct megasas_evt_detail), | |
2711 | instance->evt_detail, | |
2712 | instance->evt_detail_h); | |
2713 | ||
2714 | if (instance->producer) | |
2715 | pci_free_consistent(pdev, sizeof(u32), instance->producer, | |
2716 | instance->producer_h); | |
2717 | if (instance->consumer) | |
2718 | pci_free_consistent(pdev, sizeof(u32), instance->consumer, | |
2719 | instance->consumer_h); | |
2720 | scsi_host_put(host); | |
2721 | ||
2722 | fail_alloc_instance: | |
2723 | fail_set_dma_mask: | |
2724 | pci_disable_device(pdev); | |
2725 | ||
2726 | return -ENODEV; | |
2727 | } | |
2728 | ||
2729 | /** | |
2730 | * megasas_flush_cache - Requests FW to flush all its caches | |
2731 | * @instance: Adapter soft state | |
2732 | */ | |
2733 | static void megasas_flush_cache(struct megasas_instance *instance) | |
2734 | { | |
2735 | struct megasas_cmd *cmd; | |
2736 | struct megasas_dcmd_frame *dcmd; | |
2737 | ||
2738 | cmd = megasas_get_cmd(instance); | |
2739 | ||
2740 | if (!cmd) | |
2741 | return; | |
2742 | ||
2743 | dcmd = &cmd->frame->dcmd; | |
2744 | ||
2745 | memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE); | |
2746 | ||
2747 | dcmd->cmd = MFI_CMD_DCMD; | |
2748 | dcmd->cmd_status = 0x0; | |
2749 | dcmd->sge_count = 0; | |
2750 | dcmd->flags = MFI_FRAME_DIR_NONE; | |
2751 | dcmd->timeout = 0; | |
2752 | dcmd->data_xfer_len = 0; | |
2753 | dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH; | |
2754 | dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE; | |
2755 | ||
2756 | megasas_issue_blocked_cmd(instance, cmd); | |
2757 | ||
2758 | megasas_return_cmd(instance, cmd); | |
2759 | ||
2760 | return; | |
2761 | } | |
2762 | ||
2763 | /** | |
2764 | * megasas_shutdown_controller - Instructs FW to shutdown the controller | |
2765 | * @instance: Adapter soft state | |
31ea7088 | 2766 | * @opcode: Shutdown/Hibernate |
c4a3e0a5 | 2767 | */ |
31ea7088 | 2768 | static void megasas_shutdown_controller(struct megasas_instance *instance, |
2769 | u32 opcode) | |
c4a3e0a5 BS |
2770 | { |
2771 | struct megasas_cmd *cmd; | |
2772 | struct megasas_dcmd_frame *dcmd; | |
2773 | ||
2774 | cmd = megasas_get_cmd(instance); | |
2775 | ||
2776 | if (!cmd) | |
2777 | return; | |
2778 | ||
2779 | if (instance->aen_cmd) | |
2780 | megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd); | |
2781 | ||
2782 | dcmd = &cmd->frame->dcmd; | |
2783 | ||
2784 | memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE); | |
2785 | ||
2786 | dcmd->cmd = MFI_CMD_DCMD; | |
2787 | dcmd->cmd_status = 0x0; | |
2788 | dcmd->sge_count = 0; | |
2789 | dcmd->flags = MFI_FRAME_DIR_NONE; | |
2790 | dcmd->timeout = 0; | |
2791 | dcmd->data_xfer_len = 0; | |
31ea7088 | 2792 | dcmd->opcode = opcode; |
c4a3e0a5 BS |
2793 | |
2794 | megasas_issue_blocked_cmd(instance, cmd); | |
2795 | ||
2796 | megasas_return_cmd(instance, cmd); | |
2797 | ||
2798 | return; | |
2799 | } | |
2800 | ||
33139b21 | 2801 | #ifdef CONFIG_PM |
31ea7088 | 2802 | /** |
ad84db2e | 2803 | * megasas_suspend - driver suspend entry point |
2804 | * @pdev: PCI device structure | |
31ea7088 | 2805 | * @state: PCI power state to suspend routine |
2806 | */ | |
33139b21 | 2807 | static int |
31ea7088 | 2808 | megasas_suspend(struct pci_dev *pdev, pm_message_t state) |
2809 | { | |
2810 | struct Scsi_Host *host; | |
2811 | struct megasas_instance *instance; | |
2812 | ||
2813 | instance = pci_get_drvdata(pdev); | |
2814 | host = instance->host; | |
2815 | ||
ad84db2e | 2816 | if (poll_mode_io) |
2817 | del_timer_sync(&instance->io_completion_timer); | |
2818 | ||
31ea7088 | 2819 | megasas_flush_cache(instance); |
2820 | megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN); | |
2821 | tasklet_kill(&instance->isr_tasklet); | |
2822 | ||
2823 | pci_set_drvdata(instance->pdev, instance); | |
2824 | instance->instancet->disable_intr(instance->reg_set); | |
2825 | free_irq(instance->pdev->irq, instance); | |
2826 | ||
2827 | pci_save_state(pdev); | |
2828 | pci_disable_device(pdev); | |
2829 | ||
2830 | pci_set_power_state(pdev, pci_choose_state(pdev, state)); | |
2831 | ||
2832 | return 0; | |
2833 | } | |
2834 | ||
2835 | /** | |
2836 | * megasas_resume- driver resume entry point | |
2837 | * @pdev: PCI device structure | |
2838 | */ | |
33139b21 | 2839 | static int |
31ea7088 | 2840 | megasas_resume(struct pci_dev *pdev) |
2841 | { | |
2842 | int rval; | |
2843 | struct Scsi_Host *host; | |
2844 | struct megasas_instance *instance; | |
2845 | ||
2846 | instance = pci_get_drvdata(pdev); | |
2847 | host = instance->host; | |
2848 | pci_set_power_state(pdev, PCI_D0); | |
2849 | pci_enable_wake(pdev, PCI_D0, 0); | |
2850 | pci_restore_state(pdev); | |
2851 | ||
2852 | /* | |
2853 | * PCI prepping: enable device set bus mastering and dma mask | |
2854 | */ | |
2855 | rval = pci_enable_device(pdev); | |
2856 | ||
2857 | if (rval) { | |
2858 | printk(KERN_ERR "megasas: Enable device failed\n"); | |
2859 | return rval; | |
2860 | } | |
2861 | ||
2862 | pci_set_master(pdev); | |
2863 | ||
2864 | if (megasas_set_dma_mask(pdev)) | |
2865 | goto fail_set_dma_mask; | |
2866 | ||
2867 | /* | |
2868 | * Initialize MFI Firmware | |
2869 | */ | |
2870 | ||
2871 | *instance->producer = 0; | |
2872 | *instance->consumer = 0; | |
2873 | ||
2874 | atomic_set(&instance->fw_outstanding, 0); | |
2875 | ||
2876 | /* | |
2877 | * We expect the FW state to be READY | |
2878 | */ | |
2879 | if (megasas_transition_to_ready(instance)) | |
2880 | goto fail_ready_state; | |
2881 | ||
2882 | if (megasas_issue_init_mfi(instance)) | |
2883 | goto fail_init_mfi; | |
2884 | ||
2885 | tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc, | |
2886 | (unsigned long)instance); | |
2887 | ||
2888 | /* | |
2889 | * Register IRQ | |
2890 | */ | |
2891 | if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, | |
2892 | "megasas", instance)) { | |
2893 | printk(KERN_ERR "megasas: Failed to register IRQ\n"); | |
2894 | goto fail_irq; | |
2895 | } | |
2896 | ||
2897 | instance->instancet->enable_intr(instance->reg_set); | |
2898 | ||
2899 | /* | |
2900 | * Initiate AEN (Asynchronous Event Notification) | |
2901 | */ | |
2902 | if (megasas_start_aen(instance)) | |
2903 | printk(KERN_ERR "megasas: Start AEN failed\n"); | |
2904 | ||
ad84db2e | 2905 | /* Initialize the cmd completion timer */ |
2906 | if (poll_mode_io) | |
2907 | megasas_start_timer(instance, &instance->io_completion_timer, | |
2908 | megasas_io_completion_timer, | |
2909 | MEGASAS_COMPLETION_TIMER_INTERVAL); | |
31ea7088 | 2910 | return 0; |
2911 | ||
2912 | fail_irq: | |
2913 | fail_init_mfi: | |
2914 | if (instance->evt_detail) | |
2915 | pci_free_consistent(pdev, sizeof(struct megasas_evt_detail), | |
2916 | instance->evt_detail, | |
2917 | instance->evt_detail_h); | |
2918 | ||
2919 | if (instance->producer) | |
2920 | pci_free_consistent(pdev, sizeof(u32), instance->producer, | |
2921 | instance->producer_h); | |
2922 | if (instance->consumer) | |
2923 | pci_free_consistent(pdev, sizeof(u32), instance->consumer, | |
2924 | instance->consumer_h); | |
2925 | scsi_host_put(host); | |
2926 | ||
2927 | fail_set_dma_mask: | |
2928 | fail_ready_state: | |
2929 | ||
2930 | pci_disable_device(pdev); | |
2931 | ||
2932 | return -ENODEV; | |
2933 | } | |
33139b21 JS |
2934 | #else |
2935 | #define megasas_suspend NULL | |
2936 | #define megasas_resume NULL | |
2937 | #endif | |
31ea7088 | 2938 | |
c4a3e0a5 BS |
2939 | /** |
2940 | * megasas_detach_one - PCI hot"un"plug entry point | |
2941 | * @pdev: PCI device structure | |
2942 | */ | |
33139b21 | 2943 | static void __devexit megasas_detach_one(struct pci_dev *pdev) |
c4a3e0a5 BS |
2944 | { |
2945 | int i; | |
2946 | struct Scsi_Host *host; | |
2947 | struct megasas_instance *instance; | |
2948 | ||
2949 | instance = pci_get_drvdata(pdev); | |
c3518837 | 2950 | instance->unload = 1; |
c4a3e0a5 BS |
2951 | host = instance->host; |
2952 | ||
ad84db2e | 2953 | if (poll_mode_io) |
2954 | del_timer_sync(&instance->io_completion_timer); | |
2955 | ||
c4a3e0a5 BS |
2956 | scsi_remove_host(instance->host); |
2957 | megasas_flush_cache(instance); | |
31ea7088 | 2958 | megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN); |
5d018ad0 | 2959 | tasklet_kill(&instance->isr_tasklet); |
c4a3e0a5 BS |
2960 | |
2961 | /* | |
2962 | * Take the instance off the instance array. Note that we will not | |
2963 | * decrement the max_index. We let this array be sparse array | |
2964 | */ | |
2965 | for (i = 0; i < megasas_mgmt_info.max_index; i++) { | |
2966 | if (megasas_mgmt_info.instance[i] == instance) { | |
2967 | megasas_mgmt_info.count--; | |
2968 | megasas_mgmt_info.instance[i] = NULL; | |
2969 | ||
2970 | break; | |
2971 | } | |
2972 | } | |
2973 | ||
2974 | pci_set_drvdata(instance->pdev, NULL); | |
2975 | ||
b274cab7 | 2976 | instance->instancet->disable_intr(instance->reg_set); |
c4a3e0a5 BS |
2977 | |
2978 | free_irq(instance->pdev->irq, instance); | |
2979 | ||
2980 | megasas_release_mfi(instance); | |
2981 | ||
2982 | pci_free_consistent(pdev, sizeof(struct megasas_evt_detail), | |
2983 | instance->evt_detail, instance->evt_detail_h); | |
2984 | ||
2985 | pci_free_consistent(pdev, sizeof(u32), instance->producer, | |
2986 | instance->producer_h); | |
2987 | ||
2988 | pci_free_consistent(pdev, sizeof(u32), instance->consumer, | |
2989 | instance->consumer_h); | |
2990 | ||
2991 | scsi_host_put(host); | |
2992 | ||
2993 | pci_set_drvdata(pdev, NULL); | |
2994 | ||
2995 | pci_disable_device(pdev); | |
2996 | ||
2997 | return; | |
2998 | } | |
2999 | ||
3000 | /** | |
3001 | * megasas_shutdown - Shutdown entry point | |
3002 | * @device: Generic device structure | |
3003 | */ | |
3004 | static void megasas_shutdown(struct pci_dev *pdev) | |
3005 | { | |
3006 | struct megasas_instance *instance = pci_get_drvdata(pdev); | |
3007 | megasas_flush_cache(instance); | |
530e6fc1 | 3008 | megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN); |
c4a3e0a5 BS |
3009 | } |
3010 | ||
3011 | /** | |
3012 | * megasas_mgmt_open - char node "open" entry point | |
3013 | */ | |
3014 | static int megasas_mgmt_open(struct inode *inode, struct file *filep) | |
3015 | { | |
f2b9857e | 3016 | cycle_kernel_lock(); |
c4a3e0a5 BS |
3017 | /* |
3018 | * Allow only those users with admin rights | |
3019 | */ | |
3020 | if (!capable(CAP_SYS_ADMIN)) | |
3021 | return -EACCES; | |
3022 | ||
3023 | return 0; | |
3024 | } | |
3025 | ||
c4a3e0a5 BS |
3026 | /** |
3027 | * megasas_mgmt_fasync - Async notifier registration from applications | |
3028 | * | |
3029 | * This function adds the calling process to a driver global queue. When an | |
3030 | * event occurs, SIGIO will be sent to all processes in this queue. | |
3031 | */ | |
3032 | static int megasas_mgmt_fasync(int fd, struct file *filep, int mode) | |
3033 | { | |
3034 | int rc; | |
3035 | ||
0b950672 | 3036 | mutex_lock(&megasas_async_queue_mutex); |
c4a3e0a5 BS |
3037 | |
3038 | rc = fasync_helper(fd, filep, mode, &megasas_async_queue); | |
3039 | ||
0b950672 | 3040 | mutex_unlock(&megasas_async_queue_mutex); |
c4a3e0a5 BS |
3041 | |
3042 | if (rc >= 0) { | |
3043 | /* For sanity check when we get ioctl */ | |
3044 | filep->private_data = filep; | |
3045 | return 0; | |
3046 | } | |
3047 | ||
3048 | printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc); | |
3049 | ||
3050 | return rc; | |
3051 | } | |
3052 | ||
c3518837 YB |
3053 | /** |
3054 | * megasas_mgmt_poll - char node "poll" entry point | |
3055 | * */ | |
3056 | static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait) | |
3057 | { | |
3058 | unsigned int mask; | |
3059 | unsigned long flags; | |
3060 | poll_wait(file, &megasas_poll_wait, wait); | |
3061 | spin_lock_irqsave(&poll_aen_lock, flags); | |
3062 | if (megasas_poll_wait_aen) | |
3063 | mask = (POLLIN | POLLRDNORM); | |
3064 | else | |
3065 | mask = 0; | |
3066 | spin_unlock_irqrestore(&poll_aen_lock, flags); | |
3067 | return mask; | |
3068 | } | |
3069 | ||
c4a3e0a5 BS |
3070 | /** |
3071 | * megasas_mgmt_fw_ioctl - Issues management ioctls to FW | |
3072 | * @instance: Adapter soft state | |
3073 | * @argp: User's ioctl packet | |
3074 | */ | |
3075 | static int | |
3076 | megasas_mgmt_fw_ioctl(struct megasas_instance *instance, | |
3077 | struct megasas_iocpacket __user * user_ioc, | |
3078 | struct megasas_iocpacket *ioc) | |
3079 | { | |
3080 | struct megasas_sge32 *kern_sge32; | |
3081 | struct megasas_cmd *cmd; | |
3082 | void *kbuff_arr[MAX_IOCTL_SGE]; | |
3083 | dma_addr_t buf_handle = 0; | |
3084 | int error = 0, i; | |
3085 | void *sense = NULL; | |
3086 | dma_addr_t sense_handle; | |
3087 | u32 *sense_ptr; | |
3088 | ||
3089 | memset(kbuff_arr, 0, sizeof(kbuff_arr)); | |
3090 | ||
3091 | if (ioc->sge_count > MAX_IOCTL_SGE) { | |
3092 | printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n", | |
3093 | ioc->sge_count, MAX_IOCTL_SGE); | |
3094 | return -EINVAL; | |
3095 | } | |
3096 | ||
3097 | cmd = megasas_get_cmd(instance); | |
3098 | if (!cmd) { | |
3099 | printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n"); | |
3100 | return -ENOMEM; | |
3101 | } | |
3102 | ||
3103 | /* | |
3104 | * User's IOCTL packet has 2 frames (maximum). Copy those two | |
3105 | * frames into our cmd's frames. cmd->frame's context will get | |
3106 | * overwritten when we copy from user's frames. So set that value | |
3107 | * alone separately | |
3108 | */ | |
3109 | memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE); | |
3110 | cmd->frame->hdr.context = cmd->index; | |
c3518837 | 3111 | cmd->frame->hdr.pad_0 = 0; |
c4a3e0a5 BS |
3112 | |
3113 | /* | |
3114 | * The management interface between applications and the fw uses | |
3115 | * MFI frames. E.g, RAID configuration changes, LD property changes | |
3116 | * etc are accomplishes through different kinds of MFI frames. The | |
3117 | * driver needs to care only about substituting user buffers with | |
3118 | * kernel buffers in SGLs. The location of SGL is embedded in the | |
3119 | * struct iocpacket itself. | |
3120 | */ | |
3121 | kern_sge32 = (struct megasas_sge32 *) | |
3122 | ((unsigned long)cmd->frame + ioc->sgl_off); | |
3123 | ||
3124 | /* | |
3125 | * For each user buffer, create a mirror buffer and copy in | |
3126 | */ | |
3127 | for (i = 0; i < ioc->sge_count; i++) { | |
9f35fa8a | 3128 | kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev, |
c4a3e0a5 | 3129 | ioc->sgl[i].iov_len, |
9f35fa8a | 3130 | &buf_handle, GFP_KERNEL); |
c4a3e0a5 BS |
3131 | if (!kbuff_arr[i]) { |
3132 | printk(KERN_DEBUG "megasas: Failed to alloc " | |
3133 | "kernel SGL buffer for IOCTL \n"); | |
3134 | error = -ENOMEM; | |
3135 | goto out; | |
3136 | } | |
3137 | ||
3138 | /* | |
3139 | * We don't change the dma_coherent_mask, so | |
3140 | * pci_alloc_consistent only returns 32bit addresses | |
3141 | */ | |
3142 | kern_sge32[i].phys_addr = (u32) buf_handle; | |
3143 | kern_sge32[i].length = ioc->sgl[i].iov_len; | |
3144 | ||
3145 | /* | |
3146 | * We created a kernel buffer corresponding to the | |
3147 | * user buffer. Now copy in from the user buffer | |
3148 | */ | |
3149 | if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base, | |
3150 | (u32) (ioc->sgl[i].iov_len))) { | |
3151 | error = -EFAULT; | |
3152 | goto out; | |
3153 | } | |
3154 | } | |
3155 | ||
3156 | if (ioc->sense_len) { | |
9f35fa8a SP |
3157 | sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len, |
3158 | &sense_handle, GFP_KERNEL); | |
c4a3e0a5 BS |
3159 | if (!sense) { |
3160 | error = -ENOMEM; | |
3161 | goto out; | |
3162 | } | |
3163 | ||
3164 | sense_ptr = | |
3165 | (u32 *) ((unsigned long)cmd->frame + ioc->sense_off); | |
3166 | *sense_ptr = sense_handle; | |
3167 | } | |
3168 | ||
3169 | /* | |
3170 | * Set the sync_cmd flag so that the ISR knows not to complete this | |
3171 | * cmd to the SCSI mid-layer | |
3172 | */ | |
3173 | cmd->sync_cmd = 1; | |
3174 | megasas_issue_blocked_cmd(instance, cmd); | |
3175 | cmd->sync_cmd = 0; | |
3176 | ||
3177 | /* | |
3178 | * copy out the kernel buffers to user buffers | |
3179 | */ | |
3180 | for (i = 0; i < ioc->sge_count; i++) { | |
3181 | if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i], | |
3182 | ioc->sgl[i].iov_len)) { | |
3183 | error = -EFAULT; | |
3184 | goto out; | |
3185 | } | |
3186 | } | |
3187 | ||
3188 | /* | |
3189 | * copy out the sense | |
3190 | */ | |
3191 | if (ioc->sense_len) { | |
3192 | /* | |
b70a41e0 | 3193 | * sense_ptr points to the location that has the user |
c4a3e0a5 BS |
3194 | * sense buffer address |
3195 | */ | |
b70a41e0 | 3196 | sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw + |
3197 | ioc->sense_off); | |
c4a3e0a5 | 3198 | |
b70a41e0 | 3199 | if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)), |
3200 | sense, ioc->sense_len)) { | |
b10c36a5 | 3201 | printk(KERN_ERR "megasas: Failed to copy out to user " |
3202 | "sense data\n"); | |
c4a3e0a5 BS |
3203 | error = -EFAULT; |
3204 | goto out; | |
3205 | } | |
3206 | } | |
3207 | ||
3208 | /* | |
3209 | * copy the status codes returned by the fw | |
3210 | */ | |
3211 | if (copy_to_user(&user_ioc->frame.hdr.cmd_status, | |
3212 | &cmd->frame->hdr.cmd_status, sizeof(u8))) { | |
3213 | printk(KERN_DEBUG "megasas: Error copying out cmd_status\n"); | |
3214 | error = -EFAULT; | |
3215 | } | |
3216 | ||
3217 | out: | |
3218 | if (sense) { | |
9f35fa8a | 3219 | dma_free_coherent(&instance->pdev->dev, ioc->sense_len, |
c4a3e0a5 BS |
3220 | sense, sense_handle); |
3221 | } | |
3222 | ||
3223 | for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) { | |
9f35fa8a | 3224 | dma_free_coherent(&instance->pdev->dev, |
c4a3e0a5 BS |
3225 | kern_sge32[i].length, |
3226 | kbuff_arr[i], kern_sge32[i].phys_addr); | |
3227 | } | |
3228 | ||
3229 | megasas_return_cmd(instance, cmd); | |
3230 | return error; | |
3231 | } | |
3232 | ||
3233 | static struct megasas_instance *megasas_lookup_instance(u16 host_no) | |
3234 | { | |
3235 | int i; | |
3236 | ||
3237 | for (i = 0; i < megasas_mgmt_info.max_index; i++) { | |
3238 | ||
3239 | if ((megasas_mgmt_info.instance[i]) && | |
3240 | (megasas_mgmt_info.instance[i]->host->host_no == host_no)) | |
3241 | return megasas_mgmt_info.instance[i]; | |
3242 | } | |
3243 | ||
3244 | return NULL; | |
3245 | } | |
3246 | ||
3247 | static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg) | |
3248 | { | |
3249 | struct megasas_iocpacket __user *user_ioc = | |
3250 | (struct megasas_iocpacket __user *)arg; | |
3251 | struct megasas_iocpacket *ioc; | |
3252 | struct megasas_instance *instance; | |
3253 | int error; | |
3254 | ||
3255 | ioc = kmalloc(sizeof(*ioc), GFP_KERNEL); | |
3256 | if (!ioc) | |
3257 | return -ENOMEM; | |
3258 | ||
3259 | if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) { | |
3260 | error = -EFAULT; | |
3261 | goto out_kfree_ioc; | |
3262 | } | |
3263 | ||
3264 | instance = megasas_lookup_instance(ioc->host_no); | |
3265 | if (!instance) { | |
3266 | error = -ENODEV; | |
3267 | goto out_kfree_ioc; | |
3268 | } | |
3269 | ||
3270 | /* | |
3271 | * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds | |
3272 | */ | |
3273 | if (down_interruptible(&instance->ioctl_sem)) { | |
3274 | error = -ERESTARTSYS; | |
3275 | goto out_kfree_ioc; | |
3276 | } | |
3277 | error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc); | |
3278 | up(&instance->ioctl_sem); | |
3279 | ||
3280 | out_kfree_ioc: | |
3281 | kfree(ioc); | |
3282 | return error; | |
3283 | } | |
3284 | ||
3285 | static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg) | |
3286 | { | |
3287 | struct megasas_instance *instance; | |
3288 | struct megasas_aen aen; | |
3289 | int error; | |
3290 | ||
3291 | if (file->private_data != file) { | |
3292 | printk(KERN_DEBUG "megasas: fasync_helper was not " | |
3293 | "called first\n"); | |
3294 | return -EINVAL; | |
3295 | } | |
3296 | ||
3297 | if (copy_from_user(&aen, (void __user *)arg, sizeof(aen))) | |
3298 | return -EFAULT; | |
3299 | ||
3300 | instance = megasas_lookup_instance(aen.host_no); | |
3301 | ||
3302 | if (!instance) | |
3303 | return -ENODEV; | |
3304 | ||
e5a69e27 | 3305 | mutex_lock(&instance->aen_mutex); |
c4a3e0a5 BS |
3306 | error = megasas_register_aen(instance, aen.seq_num, |
3307 | aen.class_locale_word); | |
e5a69e27 | 3308 | mutex_unlock(&instance->aen_mutex); |
c4a3e0a5 BS |
3309 | return error; |
3310 | } | |
3311 | ||
3312 | /** | |
3313 | * megasas_mgmt_ioctl - char node ioctl entry point | |
3314 | */ | |
3315 | static long | |
3316 | megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
3317 | { | |
3318 | switch (cmd) { | |
3319 | case MEGASAS_IOC_FIRMWARE: | |
3320 | return megasas_mgmt_ioctl_fw(file, arg); | |
3321 | ||
3322 | case MEGASAS_IOC_GET_AEN: | |
3323 | return megasas_mgmt_ioctl_aen(file, arg); | |
3324 | } | |
3325 | ||
3326 | return -ENOTTY; | |
3327 | } | |
3328 | ||
3329 | #ifdef CONFIG_COMPAT | |
3330 | static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg) | |
3331 | { | |
3332 | struct compat_megasas_iocpacket __user *cioc = | |
3333 | (struct compat_megasas_iocpacket __user *)arg; | |
3334 | struct megasas_iocpacket __user *ioc = | |
3335 | compat_alloc_user_space(sizeof(struct megasas_iocpacket)); | |
3336 | int i; | |
3337 | int error = 0; | |
3338 | ||
83aabc1b JG |
3339 | if (clear_user(ioc, sizeof(*ioc))) |
3340 | return -EFAULT; | |
c4a3e0a5 BS |
3341 | |
3342 | if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) || | |
3343 | copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) || | |
3344 | copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) || | |
3345 | copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) || | |
3346 | copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) || | |
3347 | copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32))) | |
3348 | return -EFAULT; | |
3349 | ||
3350 | for (i = 0; i < MAX_IOCTL_SGE; i++) { | |
3351 | compat_uptr_t ptr; | |
3352 | ||
3353 | if (get_user(ptr, &cioc->sgl[i].iov_base) || | |
3354 | put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) || | |
3355 | copy_in_user(&ioc->sgl[i].iov_len, | |
3356 | &cioc->sgl[i].iov_len, sizeof(compat_size_t))) | |
3357 | return -EFAULT; | |
3358 | } | |
3359 | ||
3360 | error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc); | |
3361 | ||
3362 | if (copy_in_user(&cioc->frame.hdr.cmd_status, | |
3363 | &ioc->frame.hdr.cmd_status, sizeof(u8))) { | |
3364 | printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n"); | |
3365 | return -EFAULT; | |
3366 | } | |
3367 | return error; | |
3368 | } | |
3369 | ||
3370 | static long | |
3371 | megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd, | |
3372 | unsigned long arg) | |
3373 | { | |
3374 | switch (cmd) { | |
cb59aa6a SP |
3375 | case MEGASAS_IOC_FIRMWARE32: |
3376 | return megasas_mgmt_compat_ioctl_fw(file, arg); | |
c4a3e0a5 BS |
3377 | case MEGASAS_IOC_GET_AEN: |
3378 | return megasas_mgmt_ioctl_aen(file, arg); | |
3379 | } | |
3380 | ||
3381 | return -ENOTTY; | |
3382 | } | |
3383 | #endif | |
3384 | ||
3385 | /* | |
3386 | * File operations structure for management interface | |
3387 | */ | |
00977a59 | 3388 | static const struct file_operations megasas_mgmt_fops = { |
c4a3e0a5 BS |
3389 | .owner = THIS_MODULE, |
3390 | .open = megasas_mgmt_open, | |
c4a3e0a5 BS |
3391 | .fasync = megasas_mgmt_fasync, |
3392 | .unlocked_ioctl = megasas_mgmt_ioctl, | |
c3518837 | 3393 | .poll = megasas_mgmt_poll, |
c4a3e0a5 BS |
3394 | #ifdef CONFIG_COMPAT |
3395 | .compat_ioctl = megasas_mgmt_compat_ioctl, | |
3396 | #endif | |
3397 | }; | |
3398 | ||
3399 | /* | |
3400 | * PCI hotplug support registration structure | |
3401 | */ | |
3402 | static struct pci_driver megasas_pci_driver = { | |
3403 | ||
3404 | .name = "megaraid_sas", | |
3405 | .id_table = megasas_pci_table, | |
3406 | .probe = megasas_probe_one, | |
3407 | .remove = __devexit_p(megasas_detach_one), | |
31ea7088 | 3408 | .suspend = megasas_suspend, |
3409 | .resume = megasas_resume, | |
c4a3e0a5 BS |
3410 | .shutdown = megasas_shutdown, |
3411 | }; | |
3412 | ||
3413 | /* | |
3414 | * Sysfs driver attributes | |
3415 | */ | |
3416 | static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf) | |
3417 | { | |
3418 | return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n", | |
3419 | MEGASAS_VERSION); | |
3420 | } | |
3421 | ||
3422 | static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL); | |
3423 | ||
3424 | static ssize_t | |
3425 | megasas_sysfs_show_release_date(struct device_driver *dd, char *buf) | |
3426 | { | |
3427 | return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n", | |
3428 | MEGASAS_RELDATE); | |
3429 | } | |
3430 | ||
3431 | static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date, | |
3432 | NULL); | |
3433 | ||
658dcedb SP |
3434 | static ssize_t |
3435 | megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf) | |
3436 | { | |
ad84db2e | 3437 | return sprintf(buf, "%u\n", megasas_dbg_lvl); |
658dcedb SP |
3438 | } |
3439 | ||
3440 | static ssize_t | |
3441 | megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count) | |
3442 | { | |
3443 | int retval = count; | |
3444 | if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){ | |
3445 | printk(KERN_ERR "megasas: could not set dbg_lvl\n"); | |
3446 | retval = -EINVAL; | |
3447 | } | |
3448 | return retval; | |
3449 | } | |
3450 | ||
66dca9b8 | 3451 | static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl, |
ad84db2e | 3452 | megasas_sysfs_set_dbg_lvl); |
3453 | ||
3454 | static ssize_t | |
3455 | megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf) | |
3456 | { | |
3457 | return sprintf(buf, "%u\n", poll_mode_io); | |
3458 | } | |
3459 | ||
3460 | static ssize_t | |
3461 | megasas_sysfs_set_poll_mode_io(struct device_driver *dd, | |
3462 | const char *buf, size_t count) | |
3463 | { | |
3464 | int retval = count; | |
3465 | int tmp = poll_mode_io; | |
3466 | int i; | |
3467 | struct megasas_instance *instance; | |
3468 | ||
3469 | if (sscanf(buf, "%u", &poll_mode_io) < 1) { | |
3470 | printk(KERN_ERR "megasas: could not set poll_mode_io\n"); | |
3471 | retval = -EINVAL; | |
3472 | } | |
3473 | ||
3474 | /* | |
3475 | * Check if poll_mode_io is already set or is same as previous value | |
3476 | */ | |
3477 | if ((tmp && poll_mode_io) || (tmp == poll_mode_io)) | |
3478 | goto out; | |
3479 | ||
3480 | if (poll_mode_io) { | |
3481 | /* | |
3482 | * Start timers for all adapters | |
3483 | */ | |
3484 | for (i = 0; i < megasas_mgmt_info.max_index; i++) { | |
3485 | instance = megasas_mgmt_info.instance[i]; | |
3486 | if (instance) { | |
3487 | megasas_start_timer(instance, | |
3488 | &instance->io_completion_timer, | |
3489 | megasas_io_completion_timer, | |
3490 | MEGASAS_COMPLETION_TIMER_INTERVAL); | |
3491 | } | |
3492 | } | |
3493 | } else { | |
3494 | /* | |
3495 | * Delete timers for all adapters | |
3496 | */ | |
3497 | for (i = 0; i < megasas_mgmt_info.max_index; i++) { | |
3498 | instance = megasas_mgmt_info.instance[i]; | |
3499 | if (instance) | |
3500 | del_timer_sync(&instance->io_completion_timer); | |
3501 | } | |
3502 | } | |
3503 | ||
3504 | out: | |
3505 | return retval; | |
3506 | } | |
3507 | ||
3508 | static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUGO, | |
3509 | megasas_sysfs_show_poll_mode_io, | |
3510 | megasas_sysfs_set_poll_mode_io); | |
658dcedb | 3511 | |
c4a3e0a5 BS |
3512 | /** |
3513 | * megasas_init - Driver load entry point | |
3514 | */ | |
3515 | static int __init megasas_init(void) | |
3516 | { | |
3517 | int rval; | |
3518 | ||
3519 | /* | |
3520 | * Announce driver version and other information | |
3521 | */ | |
3522 | printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION, | |
3523 | MEGASAS_EXT_VERSION); | |
3524 | ||
3525 | memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info)); | |
3526 | ||
3527 | /* | |
3528 | * Register character device node | |
3529 | */ | |
3530 | rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops); | |
3531 | ||
3532 | if (rval < 0) { | |
3533 | printk(KERN_DEBUG "megasas: failed to open device node\n"); | |
3534 | return rval; | |
3535 | } | |
3536 | ||
3537 | megasas_mgmt_majorno = rval; | |
3538 | ||
3539 | /* | |
3540 | * Register ourselves as PCI hotplug module | |
3541 | */ | |
4041b9cd | 3542 | rval = pci_register_driver(&megasas_pci_driver); |
c4a3e0a5 BS |
3543 | |
3544 | if (rval) { | |
3545 | printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n"); | |
83aabc1b JG |
3546 | goto err_pcidrv; |
3547 | } | |
3548 | ||
3549 | rval = driver_create_file(&megasas_pci_driver.driver, | |
3550 | &driver_attr_version); | |
3551 | if (rval) | |
3552 | goto err_dcf_attr_ver; | |
3553 | rval = driver_create_file(&megasas_pci_driver.driver, | |
3554 | &driver_attr_release_date); | |
3555 | if (rval) | |
3556 | goto err_dcf_rel_date; | |
3557 | rval = driver_create_file(&megasas_pci_driver.driver, | |
3558 | &driver_attr_dbg_lvl); | |
3559 | if (rval) | |
3560 | goto err_dcf_dbg_lvl; | |
ad84db2e | 3561 | rval = driver_create_file(&megasas_pci_driver.driver, |
3562 | &driver_attr_poll_mode_io); | |
3563 | if (rval) | |
3564 | goto err_dcf_poll_mode_io; | |
c4a3e0a5 BS |
3565 | |
3566 | return rval; | |
ad84db2e | 3567 | |
3568 | err_dcf_poll_mode_io: | |
3569 | driver_remove_file(&megasas_pci_driver.driver, | |
3570 | &driver_attr_dbg_lvl); | |
83aabc1b JG |
3571 | err_dcf_dbg_lvl: |
3572 | driver_remove_file(&megasas_pci_driver.driver, | |
3573 | &driver_attr_release_date); | |
3574 | err_dcf_rel_date: | |
3575 | driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version); | |
3576 | err_dcf_attr_ver: | |
3577 | pci_unregister_driver(&megasas_pci_driver); | |
3578 | err_pcidrv: | |
3579 | unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl"); | |
3580 | return rval; | |
c4a3e0a5 BS |
3581 | } |
3582 | ||
3583 | /** | |
3584 | * megasas_exit - Driver unload entry point | |
3585 | */ | |
3586 | static void __exit megasas_exit(void) | |
3587 | { | |
ad84db2e | 3588 | driver_remove_file(&megasas_pci_driver.driver, |
3589 | &driver_attr_poll_mode_io); | |
658dcedb SP |
3590 | driver_remove_file(&megasas_pci_driver.driver, |
3591 | &driver_attr_dbg_lvl); | |
83aabc1b JG |
3592 | driver_remove_file(&megasas_pci_driver.driver, |
3593 | &driver_attr_release_date); | |
3594 | driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version); | |
c4a3e0a5 BS |
3595 | |
3596 | pci_unregister_driver(&megasas_pci_driver); | |
3597 | unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl"); | |
3598 | } | |
3599 | ||
3600 | module_init(megasas_init); | |
3601 | module_exit(megasas_exit); |