blob: 6f07f911906699d9a6515e17eb0f1e3a755a73c8 [file] [log] [blame]
adam radford9c915a82010-12-21 13:34:31 -08001/*
2 * Linux MegaRAID driver for SAS based RAID controllers
3 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +05304 * Copyright (c) 2009-2013 LSI Corporation
5 * Copyright (c) 2013-2014 Avago Technologies
adam radford9c915a82010-12-21 13:34:31 -08006 *
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 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053018 * along with this program. If not, see <http://www.gnu.org/licenses/>.
adam radford9c915a82010-12-21 13:34:31 -080019 *
20 * FILE: megaraid_sas_fusion.c
21 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053022 * Authors: Avago Technologies
adam radford9c915a82010-12-21 13:34:31 -080023 * Sumant Patro
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053024 * Adam Radford
25 * Kashyap Desai <kashyap.desai@avagotech.com>
26 * Sumit Saxena <sumit.saxena@avagotech.com>
adam radford9c915a82010-12-21 13:34:31 -080027 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053028 * Send feedback to: megaraidlinux.pdl@avagotech.com
adam radford9c915a82010-12-21 13:34:31 -080029 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053030 * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
31 * San Jose, California 95131
adam radford9c915a82010-12-21 13:34:31 -080032 */
33
34#include <linux/kernel.h>
35#include <linux/types.h>
36#include <linux/pci.h>
37#include <linux/list.h>
38#include <linux/moduleparam.h>
39#include <linux/module.h>
40#include <linux/spinlock.h>
41#include <linux/interrupt.h>
42#include <linux/delay.h>
adam radford9c915a82010-12-21 13:34:31 -080043#include <linux/uio.h>
44#include <linux/uaccess.h>
45#include <linux/fs.h>
46#include <linux/compat.h>
47#include <linux/blkdev.h>
48#include <linux/mutex.h>
49#include <linux/poll.h>
50
51#include <scsi/scsi.h>
52#include <scsi/scsi_cmnd.h>
53#include <scsi/scsi_device.h>
54#include <scsi/scsi_host.h>
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +053055#include <scsi/scsi_dbg.h>
adam radford9c915a82010-12-21 13:34:31 -080056
57#include "megaraid_sas_fusion.h"
58#include "megaraid_sas.h"
59
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +053060
adam radford9c915a82010-12-21 13:34:31 -080061extern void megasas_free_cmds(struct megasas_instance *instance);
62extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
63 *instance);
64extern void
65megasas_complete_cmd(struct megasas_instance *instance,
66 struct megasas_cmd *cmd, u8 alt_status);
adam radford9c915a82010-12-21 13:34:31 -080067int
adam radford229fe472014-03-10 02:51:56 -070068wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
69 int seconds);
adam radford9c915a82010-12-21 13:34:31 -080070
71void
72megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
73int megasas_alloc_cmds(struct megasas_instance *instance);
74int
75megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
76int
77megasas_issue_polled(struct megasas_instance *instance,
78 struct megasas_cmd *cmd);
adam radford53ef2bb2011-02-24 20:56:05 -080079void
80megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
81
adam radford058a8fa2011-10-08 18:14:27 -070082int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
adam radford9c915a82010-12-21 13:34:31 -080083void megaraid_sas_kill_hba(struct megasas_instance *instance);
84
85extern u32 megasas_dbg_lvl;
adam radford229fe472014-03-10 02:51:56 -070086void megasas_sriov_heartbeat_handler(unsigned long instance_addr);
87int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
88 int initial);
89void megasas_start_timer(struct megasas_instance *instance,
90 struct timer_list *timer,
91 void *fn, unsigned long interval);
92extern struct megasas_mgmt_info megasas_mgmt_info;
adam radfordc007b8b2012-07-17 18:20:24 -070093extern int resetwaittime;
adam radford9c915a82010-12-21 13:34:31 -080094
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +053095
96
adam radford9c915a82010-12-21 13:34:31 -080097/**
98 * megasas_enable_intr_fusion - Enables interrupts
99 * @regs: MFI register set
100 */
101void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530102megasas_enable_intr_fusion(struct megasas_instance *instance)
adam radford9c915a82010-12-21 13:34:31 -0800103{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530104 struct megasas_register_set __iomem *regs;
105 regs = instance->reg_set;
Sumit.Saxena@avagotech.comc2ced172015-01-05 20:06:13 +0530106
107 instance->mask_interrupts = 0;
adam radford6497b242011-10-08 18:14:50 -0700108 /* For Thunderbolt/Invader also clear intr on enable */
109 writel(~0, &regs->outbound_intr_status);
110 readl(&regs->outbound_intr_status);
111
adam radford9c915a82010-12-21 13:34:31 -0800112 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
113
114 /* Dummy readl to force pci flush */
115 readl(&regs->outbound_intr_mask);
116}
117
118/**
119 * megasas_disable_intr_fusion - Disables interrupt
120 * @regs: MFI register set
121 */
122void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530123megasas_disable_intr_fusion(struct megasas_instance *instance)
adam radford9c915a82010-12-21 13:34:31 -0800124{
125 u32 mask = 0xFFFFFFFF;
126 u32 status;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530127 struct megasas_register_set __iomem *regs;
128 regs = instance->reg_set;
129 instance->mask_interrupts = 1;
adam radford9c915a82010-12-21 13:34:31 -0800130
131 writel(mask, &regs->outbound_intr_mask);
132 /* Dummy readl to force pci flush */
133 status = readl(&regs->outbound_intr_mask);
134}
135
136int
137megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
138{
139 u32 status;
140 /*
141 * Check if it is our interrupt
142 */
143 status = readl(&regs->outbound_intr_status);
144
145 if (status & 1) {
146 writel(status, &regs->outbound_intr_status);
147 readl(&regs->outbound_intr_status);
148 return 1;
149 }
150 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
151 return 0;
152
adam radford9c915a82010-12-21 13:34:31 -0800153 return 1;
154}
155
156/**
157 * megasas_get_cmd_fusion - Get a command from the free pool
158 * @instance: Adapter soft state
159 *
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530160 * Returns a blk_tag indexed mpt frame
adam radford9c915a82010-12-21 13:34:31 -0800161 */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530162inline struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
163 *instance, u32 blk_tag)
adam radford9c915a82010-12-21 13:34:31 -0800164{
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530165 struct fusion_context *fusion;
adam radford9c915a82010-12-21 13:34:31 -0800166
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530167 fusion = instance->ctrl_context;
168 return fusion->cmd_list[blk_tag];
adam radford9c915a82010-12-21 13:34:31 -0800169}
170
171/**
172 * megasas_return_cmd_fusion - Return a cmd to free command pool
173 * @instance: Adapter soft state
174 * @cmd: Command packet to be returned to free command pool
175 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530176inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
177 struct megasas_cmd_fusion *cmd)
adam radford9c915a82010-12-21 13:34:31 -0800178{
adam radford9c915a82010-12-21 13:34:31 -0800179 cmd->scmd = NULL;
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +0530180 memset(cmd->io_request, 0, sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
adam radford9c915a82010-12-21 13:34:31 -0800181}
182
183/**
184 * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool
185 * @instance: Adapter soft state
186 */
187static void megasas_teardown_frame_pool_fusion(
188 struct megasas_instance *instance)
189{
190 int i;
191 struct fusion_context *fusion = instance->ctrl_context;
192
193 u16 max_cmd = instance->max_fw_cmds;
194
195 struct megasas_cmd_fusion *cmd;
196
197 if (!fusion->sg_dma_pool || !fusion->sense_dma_pool) {
198 printk(KERN_ERR "megasas: dma pool is null. SG Pool %p, "
199 "sense pool : %p\n", fusion->sg_dma_pool,
200 fusion->sense_dma_pool);
201 return;
202 }
203
204 /*
205 * Return all frames to pool
206 */
207 for (i = 0; i < max_cmd; i++) {
208
209 cmd = fusion->cmd_list[i];
210
211 if (cmd->sg_frame)
212 pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
213 cmd->sg_frame_phys_addr);
214
215 if (cmd->sense)
216 pci_pool_free(fusion->sense_dma_pool, cmd->sense,
217 cmd->sense_phys_addr);
218 }
219
220 /*
221 * Now destroy the pool itself
222 */
223 pci_pool_destroy(fusion->sg_dma_pool);
224 pci_pool_destroy(fusion->sense_dma_pool);
225
226 fusion->sg_dma_pool = NULL;
227 fusion->sense_dma_pool = NULL;
228}
229
230/**
231 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
232 * @instance: Adapter soft state
233 */
234void
235megasas_free_cmds_fusion(struct megasas_instance *instance)
236{
237 int i;
238 struct fusion_context *fusion = instance->ctrl_context;
239
240 u32 max_cmds, req_sz, reply_sz, io_frames_sz;
241
242
243 req_sz = fusion->request_alloc_sz;
244 reply_sz = fusion->reply_alloc_sz;
245 io_frames_sz = fusion->io_frames_alloc_sz;
246
247 max_cmds = instance->max_fw_cmds;
248
249 /* Free descriptors and request Frames memory */
250 if (fusion->req_frames_desc)
251 dma_free_coherent(&instance->pdev->dev, req_sz,
252 fusion->req_frames_desc,
253 fusion->req_frames_desc_phys);
254
255 if (fusion->reply_frames_desc) {
256 pci_pool_free(fusion->reply_frames_desc_pool,
257 fusion->reply_frames_desc,
258 fusion->reply_frames_desc_phys);
259 pci_pool_destroy(fusion->reply_frames_desc_pool);
260 }
261
262 if (fusion->io_request_frames) {
263 pci_pool_free(fusion->io_request_frames_pool,
264 fusion->io_request_frames,
265 fusion->io_request_frames_phys);
266 pci_pool_destroy(fusion->io_request_frames_pool);
267 }
268
269 /* Free the Fusion frame pool */
270 megasas_teardown_frame_pool_fusion(instance);
271
272 /* Free all the commands in the cmd_list */
273 for (i = 0; i < max_cmds; i++)
274 kfree(fusion->cmd_list[i]);
275
276 /* Free the cmd_list buffer itself */
277 kfree(fusion->cmd_list);
278 fusion->cmd_list = NULL;
279
adam radford9c915a82010-12-21 13:34:31 -0800280}
281
282/**
283 * megasas_create_frame_pool_fusion - Creates DMA pool for cmd frames
284 * @instance: Adapter soft state
285 *
286 */
287static int megasas_create_frame_pool_fusion(struct megasas_instance *instance)
288{
289 int i;
290 u32 max_cmd;
291 struct fusion_context *fusion;
292 struct megasas_cmd_fusion *cmd;
293 u32 total_sz_chain_frame;
294
295 fusion = instance->ctrl_context;
296 max_cmd = instance->max_fw_cmds;
297
298 total_sz_chain_frame = MEGASAS_MAX_SZ_CHAIN_FRAME;
299
300 /*
301 * Use DMA pool facility provided by PCI layer
302 */
303
304 fusion->sg_dma_pool = pci_pool_create("megasas sg pool fusion",
305 instance->pdev,
306 total_sz_chain_frame, 4,
307 0);
308 if (!fusion->sg_dma_pool) {
309 printk(KERN_DEBUG "megasas: failed to setup request pool "
310 "fusion\n");
311 return -ENOMEM;
312 }
313 fusion->sense_dma_pool = pci_pool_create("megasas sense pool fusion",
314 instance->pdev,
315 SCSI_SENSE_BUFFERSIZE, 64, 0);
316
317 if (!fusion->sense_dma_pool) {
318 printk(KERN_DEBUG "megasas: failed to setup sense pool "
319 "fusion\n");
320 pci_pool_destroy(fusion->sg_dma_pool);
321 fusion->sg_dma_pool = NULL;
322 return -ENOMEM;
323 }
324
325 /*
326 * Allocate and attach a frame to each of the commands in cmd_list
327 */
328 for (i = 0; i < max_cmd; i++) {
329
330 cmd = fusion->cmd_list[i];
331
332 cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
333 GFP_KERNEL,
334 &cmd->sg_frame_phys_addr);
335
336 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
337 GFP_KERNEL, &cmd->sense_phys_addr);
338 /*
339 * megasas_teardown_frame_pool_fusion() takes care of freeing
340 * whatever has been allocated
341 */
342 if (!cmd->sg_frame || !cmd->sense) {
343 printk(KERN_DEBUG "megasas: pci_pool_alloc failed\n");
344 megasas_teardown_frame_pool_fusion(instance);
345 return -ENOMEM;
346 }
347 }
348 return 0;
349}
350
351/**
352 * megasas_alloc_cmds_fusion - Allocates the command packets
353 * @instance: Adapter soft state
354 *
355 *
356 * Each frame has a 32-bit field called context. This context is used to get
357 * back the megasas_cmd_fusion from the frame when a frame gets completed
358 * In this driver, the 32 bit values are the indices into an array cmd_list.
359 * This array is used only to look up the megasas_cmd_fusion given the context.
360 * The free commands themselves are maintained in a linked list called cmd_pool.
361 *
362 * cmds are formed in the io_request and sg_frame members of the
363 * megasas_cmd_fusion. The context field is used to get a request descriptor
364 * and is used as SMID of the cmd.
365 * SMID value range is from 1 to max_fw_cmds.
366 */
367int
368megasas_alloc_cmds_fusion(struct megasas_instance *instance)
369{
adam radfordc8e858f2011-10-08 18:15:13 -0700370 int i, j, count;
adam radford9c915a82010-12-21 13:34:31 -0800371 u32 max_cmd, io_frames_sz;
372 struct fusion_context *fusion;
373 struct megasas_cmd_fusion *cmd;
374 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
375 u32 offset;
376 dma_addr_t io_req_base_phys;
377 u8 *io_req_base;
378
379 fusion = instance->ctrl_context;
380
381 max_cmd = instance->max_fw_cmds;
382
383 fusion->req_frames_desc =
384 dma_alloc_coherent(&instance->pdev->dev,
385 fusion->request_alloc_sz,
386 &fusion->req_frames_desc_phys, GFP_KERNEL);
387
388 if (!fusion->req_frames_desc) {
389 printk(KERN_ERR "megasas; Could not allocate memory for "
390 "request_frames\n");
391 goto fail_req_desc;
392 }
393
adam radfordc8e858f2011-10-08 18:15:13 -0700394 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
adam radford9c915a82010-12-21 13:34:31 -0800395 fusion->reply_frames_desc_pool =
396 pci_pool_create("reply_frames pool", instance->pdev,
adam radfordc8e858f2011-10-08 18:15:13 -0700397 fusion->reply_alloc_sz * count, 16, 0);
adam radford9c915a82010-12-21 13:34:31 -0800398
399 if (!fusion->reply_frames_desc_pool) {
400 printk(KERN_ERR "megasas; Could not allocate memory for "
401 "reply_frame pool\n");
402 goto fail_reply_desc;
403 }
404
405 fusion->reply_frames_desc =
406 pci_pool_alloc(fusion->reply_frames_desc_pool, GFP_KERNEL,
407 &fusion->reply_frames_desc_phys);
408 if (!fusion->reply_frames_desc) {
409 printk(KERN_ERR "megasas; Could not allocate memory for "
410 "reply_frame pool\n");
411 pci_pool_destroy(fusion->reply_frames_desc_pool);
412 goto fail_reply_desc;
413 }
414
415 reply_desc = fusion->reply_frames_desc;
adam radfordc8e858f2011-10-08 18:15:13 -0700416 for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
adam radford9c915a82010-12-21 13:34:31 -0800417 reply_desc->Words = ULLONG_MAX;
418
419 io_frames_sz = fusion->io_frames_alloc_sz;
420
421 fusion->io_request_frames_pool =
422 pci_pool_create("io_request_frames pool", instance->pdev,
423 fusion->io_frames_alloc_sz, 16, 0);
424
425 if (!fusion->io_request_frames_pool) {
426 printk(KERN_ERR "megasas: Could not allocate memory for "
427 "io_request_frame pool\n");
428 goto fail_io_frames;
429 }
430
431 fusion->io_request_frames =
432 pci_pool_alloc(fusion->io_request_frames_pool, GFP_KERNEL,
433 &fusion->io_request_frames_phys);
434 if (!fusion->io_request_frames) {
435 printk(KERN_ERR "megasas: Could not allocate memory for "
436 "io_request_frames frames\n");
437 pci_pool_destroy(fusion->io_request_frames_pool);
438 goto fail_io_frames;
439 }
440
441 /*
442 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
443 * Allocate the dynamic array first and then allocate individual
444 * commands.
445 */
Fengguang Wu7731e6b2012-08-24 23:27:35 +0800446 fusion->cmd_list = kzalloc(sizeof(struct megasas_cmd_fusion *)
447 * max_cmd, GFP_KERNEL);
adam radford9c915a82010-12-21 13:34:31 -0800448
449 if (!fusion->cmd_list) {
450 printk(KERN_DEBUG "megasas: out of memory. Could not alloc "
451 "memory for cmd_list_fusion\n");
452 goto fail_cmd_list;
453 }
454
adam radford9c915a82010-12-21 13:34:31 -0800455 max_cmd = instance->max_fw_cmds;
456 for (i = 0; i < max_cmd; i++) {
457 fusion->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd_fusion),
458 GFP_KERNEL);
459 if (!fusion->cmd_list[i]) {
460 printk(KERN_ERR "Could not alloc cmd list fusion\n");
461
462 for (j = 0; j < i; j++)
463 kfree(fusion->cmd_list[j]);
464
465 kfree(fusion->cmd_list);
466 fusion->cmd_list = NULL;
467 goto fail_cmd_list;
468 }
469 }
470
471 /* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */
472 io_req_base = fusion->io_request_frames +
473 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
474 io_req_base_phys = fusion->io_request_frames_phys +
475 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
476
477 /*
478 * Add all the commands to command pool (fusion->cmd_pool)
479 */
480
481 /* SMID 0 is reserved. Set SMID/index from 1 */
482 for (i = 0; i < max_cmd; i++) {
483 cmd = fusion->cmd_list[i];
484 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
485 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
486 cmd->index = i + 1;
487 cmd->scmd = NULL;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530488 cmd->sync_cmd_idx = (i >= instance->max_scsi_cmds) ?
489 (i - instance->max_scsi_cmds) :
490 (u32)ULONG_MAX; /* Set to Invalid */
adam radford9c915a82010-12-21 13:34:31 -0800491 cmd->instance = instance;
492 cmd->io_request =
493 (struct MPI2_RAID_SCSI_IO_REQUEST *)
494 (io_req_base + offset);
495 memset(cmd->io_request, 0,
496 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
497 cmd->io_request_phys_addr = io_req_base_phys + offset;
adam radford9c915a82010-12-21 13:34:31 -0800498 }
499
500 /*
501 * Create a frame pool and assign one frame to each cmd
502 */
503 if (megasas_create_frame_pool_fusion(instance)) {
504 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
505 megasas_free_cmds_fusion(instance);
506 goto fail_req_desc;
507 }
508
509 return 0;
510
511fail_cmd_list:
512 pci_pool_free(fusion->io_request_frames_pool, fusion->io_request_frames,
513 fusion->io_request_frames_phys);
514 pci_pool_destroy(fusion->io_request_frames_pool);
515fail_io_frames:
516 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
517 fusion->reply_frames_desc,
518 fusion->reply_frames_desc_phys);
519 pci_pool_free(fusion->reply_frames_desc_pool,
520 fusion->reply_frames_desc,
521 fusion->reply_frames_desc_phys);
522 pci_pool_destroy(fusion->reply_frames_desc_pool);
523
524fail_reply_desc:
525 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
526 fusion->req_frames_desc,
527 fusion->req_frames_desc_phys);
528fail_req_desc:
529 return -ENOMEM;
530}
531
532/**
533 * wait_and_poll - Issues a polling command
534 * @instance: Adapter soft state
535 * @cmd: Command packet to be issued
536 *
537 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
538 */
539int
adam radford229fe472014-03-10 02:51:56 -0700540wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
541 int seconds)
adam radford9c915a82010-12-21 13:34:31 -0800542{
543 int i;
544 struct megasas_header *frame_hdr = &cmd->frame->hdr;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530545 struct fusion_context *fusion;
adam radford9c915a82010-12-21 13:34:31 -0800546
adam radford229fe472014-03-10 02:51:56 -0700547 u32 msecs = seconds * 1000;
adam radford9c915a82010-12-21 13:34:31 -0800548
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530549 fusion = instance->ctrl_context;
adam radford9c915a82010-12-21 13:34:31 -0800550 /*
551 * Wait for cmd_status to change
552 */
553 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
554 rmb();
555 msleep(20);
556 }
557
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530558 if (frame_hdr->cmd_status == 0xff)
adam radford9c915a82010-12-21 13:34:31 -0800559 return -ETIME;
560
561 return 0;
562}
563
564/**
565 * megasas_ioc_init_fusion - Initializes the FW
566 * @instance: Adapter soft state
567 *
568 * Issues the IOC Init cmd
569 */
570int
571megasas_ioc_init_fusion(struct megasas_instance *instance)
572{
573 struct megasas_init_frame *init_frame;
574 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage;
575 dma_addr_t ioc_init_handle;
adam radford9c915a82010-12-21 13:34:31 -0800576 struct megasas_cmd *cmd;
577 u8 ret;
578 struct fusion_context *fusion;
adam radfordc77a9bd2014-03-10 02:51:36 -0700579 union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
adam radford9c915a82010-12-21 13:34:31 -0800580 int i;
581 struct megasas_header *frame_hdr;
582
583 fusion = instance->ctrl_context;
584
585 cmd = megasas_get_cmd(instance);
586
587 if (!cmd) {
588 printk(KERN_ERR "Could not allocate cmd for INIT Frame\n");
589 ret = 1;
590 goto fail_get_cmd;
591 }
592
593 IOCInitMessage =
594 dma_alloc_coherent(&instance->pdev->dev,
595 sizeof(struct MPI2_IOC_INIT_REQUEST),
596 &ioc_init_handle, GFP_KERNEL);
597
598 if (!IOCInitMessage) {
599 printk(KERN_ERR "Could not allocate memory for "
600 "IOCInitMessage\n");
601 ret = 1;
602 goto fail_fw_init;
603 }
604
605 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
606
607 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
608 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530609 IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
610 IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
611 IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
adam radford9c915a82010-12-21 13:34:31 -0800612
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530613 IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
614 IOCInitMessage->ReplyDescriptorPostQueueAddress = cpu_to_le64(fusion->reply_frames_desc_phys);
615 IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
adam radford5738f992012-03-19 19:49:53 -0700616 IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
adam radford9c915a82010-12-21 13:34:31 -0800617 init_frame = (struct megasas_init_frame *)cmd->frame;
618 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
619
620 frame_hdr = &cmd->frame->hdr;
adam radford9c915a82010-12-21 13:34:31 -0800621 frame_hdr->cmd_status = 0xFF;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530622 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
adam radford9c915a82010-12-21 13:34:31 -0800623
624 init_frame->cmd = MFI_CMD_INIT;
625 init_frame->cmd_status = 0xFF;
626
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530627 /* driver support Extended MSIX */
628 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
629 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
630 init_frame->driver_operations.
631 mfi_capabilities.support_additional_msix = 1;
adam radford21c9e162013-09-06 15:27:14 -0700632 /* driver supports HA / Remote LUN over Fast Path interface */
633 init_frame->driver_operations.mfi_capabilities.support_fp_remote_lun
634 = 1;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530635 init_frame->driver_operations.mfi_capabilities.support_max_255lds
636 = 1;
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +0530637 init_frame->driver_operations.mfi_capabilities.support_ndrive_r1_lb
638 = 1;
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +0530639 init_frame->driver_operations.mfi_capabilities.security_protocol_cmds_fw
640 = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530641 /* Convert capability to LE32 */
642 cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
adam radford21c9e162013-09-06 15:27:14 -0700643
Hannes Reineckefdc5a972014-01-16 11:25:33 +0100644 init_frame->queue_info_new_phys_addr_hi =
645 cpu_to_le32(upper_32_bits(ioc_init_handle));
646 init_frame->queue_info_new_phys_addr_lo =
647 cpu_to_le32(lower_32_bits(ioc_init_handle));
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530648 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
adam radford9c915a82010-12-21 13:34:31 -0800649
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +0530650 req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
651 req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
adam radfordc77a9bd2014-03-10 02:51:36 -0700652 req_desc.MFAIo.RequestFlags =
adam radford9c915a82010-12-21 13:34:31 -0800653 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +0530654 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford9c915a82010-12-21 13:34:31 -0800655
656 /*
657 * disable the intr before firing the init frame
658 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530659 instance->instancet->disable_intr(instance);
adam radford9c915a82010-12-21 13:34:31 -0800660
661 for (i = 0; i < (10 * 1000); i += 20) {
662 if (readl(&instance->reg_set->doorbell) & 1)
663 msleep(20);
664 else
665 break;
666 }
667
adam radfordc77a9bd2014-03-10 02:51:36 -0700668 instance->instancet->fire_cmd(instance, req_desc.u.low,
669 req_desc.u.high, instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -0800670
adam radford229fe472014-03-10 02:51:56 -0700671 wait_and_poll(instance, cmd, MFI_POLL_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -0800672
673 frame_hdr = &cmd->frame->hdr;
674 if (frame_hdr->cmd_status != 0) {
675 ret = 1;
676 goto fail_fw_init;
677 }
678 printk(KERN_ERR "megasas:IOC Init cmd success\n");
679
680 ret = 0;
681
682fail_fw_init:
683 megasas_return_cmd(instance, cmd);
684 if (IOCInitMessage)
685 dma_free_coherent(&instance->pdev->dev,
686 sizeof(struct MPI2_IOC_INIT_REQUEST),
687 IOCInitMessage, ioc_init_handle);
688fail_get_cmd:
689 return ret;
690}
691
692/*
adam radford9c915a82010-12-21 13:34:31 -0800693 * megasas_get_ld_map_info - Returns FW's ld_map structure
694 * @instance: Adapter soft state
695 * @pend: Pend the command or not
696 * Issues an internal command (DCMD) to get the FW's controller PD
697 * list structure. This information is mainly used to find out SYSTEM
698 * supported by the FW.
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530699 * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
700 * dcmd.mbox.b[0] - number of LDs being sync'd
701 * dcmd.mbox.b[1] - 0 - complete command immediately.
702 * - 1 - pend till config change
703 * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
704 * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
705 * uses extended struct MR_FW_RAID_MAP_EXT
adam radford9c915a82010-12-21 13:34:31 -0800706 */
707static int
708megasas_get_ld_map_info(struct megasas_instance *instance)
709{
710 int ret = 0;
711 struct megasas_cmd *cmd;
712 struct megasas_dcmd_frame *dcmd;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530713 void *ci;
adam radford9c915a82010-12-21 13:34:31 -0800714 dma_addr_t ci_h = 0;
715 u32 size_map_info;
716 struct fusion_context *fusion;
717
718 cmd = megasas_get_cmd(instance);
719
720 if (!cmd) {
721 printk(KERN_DEBUG "megasas: Failed to get cmd for map info.\n");
722 return -ENOMEM;
723 }
724
725 fusion = instance->ctrl_context;
726
727 if (!fusion) {
728 megasas_return_cmd(instance, cmd);
Hannes Reinecke2f8bdfa2014-01-16 11:25:35 +0100729 return -ENXIO;
adam radford9c915a82010-12-21 13:34:31 -0800730 }
731
732 dcmd = &cmd->frame->dcmd;
733
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530734 size_map_info = fusion->current_map_sz;
adam radford9c915a82010-12-21 13:34:31 -0800735
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530736 ci = (void *) fusion->ld_map[(instance->map_id & 1)];
adam radford9c915a82010-12-21 13:34:31 -0800737 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
738
739 if (!ci) {
740 printk(KERN_DEBUG "Failed to alloc mem for ld_map_info\n");
741 megasas_return_cmd(instance, cmd);
742 return -ENOMEM;
743 }
744
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530745 memset(ci, 0, fusion->max_map_sz);
adam radford9c915a82010-12-21 13:34:31 -0800746 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530747#if VD_EXT_DEBUG
748 dev_dbg(&instance->pdev->dev,
749 "%s sending MR_DCMD_LD_MAP_GET_INFO with size %d\n",
750 __func__, cpu_to_le32(size_map_info));
751#endif
adam radford9c915a82010-12-21 13:34:31 -0800752 dcmd->cmd = MFI_CMD_DCMD;
753 dcmd->cmd_status = 0xFF;
754 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530755 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
adam radford9c915a82010-12-21 13:34:31 -0800756 dcmd->timeout = 0;
757 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530758 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
759 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
760 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
761 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -0800762
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530763 if (instance->ctrl_context && !instance->mask_interrupts)
764 ret = megasas_issue_blocked_cmd(instance, cmd,
765 MEGASAS_BLOCKED_CMD_TIMEOUT);
766 else
767 ret = megasas_issue_polled(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -0800768
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530769 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -0800770
771 return ret;
772}
773
774u8
775megasas_get_map_info(struct megasas_instance *instance)
776{
777 struct fusion_context *fusion = instance->ctrl_context;
778
779 fusion->fast_path_io = 0;
780 if (!megasas_get_ld_map_info(instance)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +0530781 if (MR_ValidateMapInfo(instance)) {
adam radford9c915a82010-12-21 13:34:31 -0800782 fusion->fast_path_io = 1;
783 return 0;
784 }
785 }
786 return 1;
787}
788
789/*
790 * megasas_sync_map_info - Returns FW's ld_map structure
791 * @instance: Adapter soft state
792 *
793 * Issues an internal command (DCMD) to get the FW's controller PD
794 * list structure. This information is mainly used to find out SYSTEM
795 * supported by the FW.
796 */
797int
798megasas_sync_map_info(struct megasas_instance *instance)
799{
800 int ret = 0, i;
801 struct megasas_cmd *cmd;
802 struct megasas_dcmd_frame *dcmd;
803 u32 size_sync_info, num_lds;
804 struct fusion_context *fusion;
805 struct MR_LD_TARGET_SYNC *ci = NULL;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530806 struct MR_DRV_RAID_MAP_ALL *map;
adam radford9c915a82010-12-21 13:34:31 -0800807 struct MR_LD_RAID *raid;
808 struct MR_LD_TARGET_SYNC *ld_sync;
809 dma_addr_t ci_h = 0;
810 u32 size_map_info;
811
812 cmd = megasas_get_cmd(instance);
813
814 if (!cmd) {
815 printk(KERN_DEBUG "megasas: Failed to get cmd for sync"
816 "info.\n");
817 return -ENOMEM;
818 }
819
820 fusion = instance->ctrl_context;
821
822 if (!fusion) {
823 megasas_return_cmd(instance, cmd);
824 return 1;
825 }
826
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530827 map = fusion->ld_drv_map[instance->map_id & 1];
adam radford9c915a82010-12-21 13:34:31 -0800828
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +0530829 num_lds = le16_to_cpu(map->raidMap.ldCount);
adam radford9c915a82010-12-21 13:34:31 -0800830
831 dcmd = &cmd->frame->dcmd;
832
833 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
834
835 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
836
837 ci = (struct MR_LD_TARGET_SYNC *)
838 fusion->ld_map[(instance->map_id - 1) & 1];
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530839 memset(ci, 0, fusion->max_map_sz);
adam radford9c915a82010-12-21 13:34:31 -0800840
841 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
842
843 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
844
845 for (i = 0; i < num_lds; i++, ld_sync++) {
846 raid = MR_LdRaidGet(i, map);
847 ld_sync->targetId = MR_GetLDTgtId(i, map);
848 ld_sync->seqNum = raid->seqNum;
849 }
850
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530851 size_map_info = fusion->current_map_sz;
adam radford9c915a82010-12-21 13:34:31 -0800852
853 dcmd->cmd = MFI_CMD_DCMD;
854 dcmd->cmd_status = 0xFF;
855 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530856 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
adam radford9c915a82010-12-21 13:34:31 -0800857 dcmd->timeout = 0;
858 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530859 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -0800860 dcmd->mbox.b[0] = num_lds;
861 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530862 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
863 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
864 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -0800865
866 instance->map_update_cmd = cmd;
867
868 instance->instancet->issue_dcmd(instance, cmd);
869
870 return ret;
871}
872
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +0530873/*
874 * meagasas_display_intel_branding - Display branding string
875 * @instance: per adapter object
876 *
877 * Return nothing.
878 */
879static void
880megasas_display_intel_branding(struct megasas_instance *instance)
881{
882 if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
883 return;
884
885 switch (instance->pdev->device) {
886 case PCI_DEVICE_ID_LSI_INVADER:
887 switch (instance->pdev->subsystem_device) {
888 case MEGARAID_INTEL_RS3DC080_SSDID:
889 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
890 instance->host->host_no,
891 MEGARAID_INTEL_RS3DC080_BRANDING);
892 break;
893 case MEGARAID_INTEL_RS3DC040_SSDID:
894 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
895 instance->host->host_no,
896 MEGARAID_INTEL_RS3DC040_BRANDING);
897 break;
898 case MEGARAID_INTEL_RS3SC008_SSDID:
899 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
900 instance->host->host_no,
901 MEGARAID_INTEL_RS3SC008_BRANDING);
902 break;
903 case MEGARAID_INTEL_RS3MC044_SSDID:
904 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
905 instance->host->host_no,
906 MEGARAID_INTEL_RS3MC044_BRANDING);
907 break;
908 default:
909 break;
910 }
911 break;
912 case PCI_DEVICE_ID_LSI_FURY:
913 switch (instance->pdev->subsystem_device) {
914 case MEGARAID_INTEL_RS3WC080_SSDID:
915 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
916 instance->host->host_no,
917 MEGARAID_INTEL_RS3WC080_BRANDING);
918 break;
919 case MEGARAID_INTEL_RS3WC040_SSDID:
920 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
921 instance->host->host_no,
922 MEGARAID_INTEL_RS3WC040_BRANDING);
923 break;
924 default:
925 break;
926 }
927 break;
928 default:
929 break;
930 }
931}
932
adam radford9c915a82010-12-21 13:34:31 -0800933/**
934 * megasas_init_adapter_fusion - Initializes the FW
935 * @instance: Adapter soft state
936 *
937 * This is the main function for initializing firmware.
938 */
939u32
940megasas_init_adapter_fusion(struct megasas_instance *instance)
941{
942 struct megasas_register_set __iomem *reg_set;
943 struct fusion_context *fusion;
944 u32 max_cmd;
adam radfordc8e858f2011-10-08 18:15:13 -0700945 int i = 0, count;
adam radford9c915a82010-12-21 13:34:31 -0800946
947 fusion = instance->ctrl_context;
948
949 reg_set = instance->reg_set;
950
951 /*
952 * Get various operational parameters from status register
953 */
954 instance->max_fw_cmds =
955 instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
956 instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008);
957
958 /*
959 * Reduce the max supported cmds by 1. This is to ensure that the
960 * reply_q_sz (1 more than the max cmd that driver may send)
961 * does not exceed max cmds that the FW can support
962 */
963 instance->max_fw_cmds = instance->max_fw_cmds-1;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +0530964
965 /*
966 * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
967 */
968 instance->max_mfi_cmds =
969 MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS;
adam radford9c915a82010-12-21 13:34:31 -0800970
971 max_cmd = instance->max_fw_cmds;
972
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +0530973 fusion->reply_q_depth = 2 * (((max_cmd + 1 + 15)/16)*16);
adam radford9c915a82010-12-21 13:34:31 -0800974
975 fusion->request_alloc_sz =
976 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
977 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
978 *(fusion->reply_q_depth);
979 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
980 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
981 (max_cmd + 1)); /* Extra 1 for SMID 0 */
982
983 fusion->max_sge_in_main_msg =
984 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
985 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
986
987 fusion->max_sge_in_chain =
988 MEGASAS_MAX_SZ_CHAIN_FRAME / sizeof(union MPI2_SGE_IO_UNION);
989
Sumit.Saxena@avagotech.coma5fd2852014-09-12 18:57:43 +0530990 instance->max_num_sge = rounddown_pow_of_two(
991 fusion->max_sge_in_main_msg + fusion->max_sge_in_chain - 2);
adam radford9c915a82010-12-21 13:34:31 -0800992
993 /* Used for pass thru MFI frame (DCMD) */
994 fusion->chain_offset_mfi_pthru =
995 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
996
997 fusion->chain_offset_io_request =
998 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
999 sizeof(union MPI2_SGE_IO_UNION))/16;
1000
adam radfordc8e858f2011-10-08 18:15:13 -07001001 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1002 for (i = 0 ; i < count; i++)
1003 fusion->last_reply_idx[i] = 0;
adam radford9c915a82010-12-21 13:34:31 -08001004
1005 /*
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05301006 * For fusion adapters, 3 commands for IOCTL and 5 commands
1007 * for driver's internal DCMDs.
1008 */
1009 instance->max_scsi_cmds = instance->max_fw_cmds -
1010 (MEGASAS_FUSION_INTERNAL_CMDS +
1011 MEGASAS_FUSION_IOCTL_CMDS);
1012 sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
1013
1014 /*
adam radford9c915a82010-12-21 13:34:31 -08001015 * Allocate memory for descriptors
1016 * Create a pool of commands
1017 */
1018 if (megasas_alloc_cmds(instance))
1019 goto fail_alloc_mfi_cmds;
1020 if (megasas_alloc_cmds_fusion(instance))
1021 goto fail_alloc_cmds;
1022
1023 if (megasas_ioc_init_fusion(instance))
1024 goto fail_ioc_init;
1025
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +05301026 megasas_display_intel_branding(instance);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05301027 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301028 dev_err(&instance->pdev->dev,
1029 "Could not get controller info. Fail from %s %d\n",
1030 __func__, __LINE__);
1031 goto fail_ioc_init;
1032 }
1033
adam radford9c915a82010-12-21 13:34:31 -08001034 instance->flag_ieee = 1;
adam radford9c915a82010-12-21 13:34:31 -08001035 fusion->fast_path_io = 0;
1036
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301037 fusion->drv_map_pages = get_order(fusion->drv_map_sz);
1038 for (i = 0; i < 2; i++) {
1039 fusion->ld_map[i] = NULL;
1040 fusion->ld_drv_map[i] = (void *)__get_free_pages(GFP_KERNEL,
1041 fusion->drv_map_pages);
1042 if (!fusion->ld_drv_map[i]) {
1043 dev_err(&instance->pdev->dev, "Could not allocate "
1044 "memory for local map info for %d pages\n",
1045 fusion->drv_map_pages);
1046 if (i == 1)
1047 free_pages((ulong)fusion->ld_drv_map[0],
1048 fusion->drv_map_pages);
1049 goto fail_ioc_init;
1050 }
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05301051 memset(fusion->ld_drv_map[i], 0,
1052 ((1 << PAGE_SHIFT) << fusion->drv_map_pages));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301053 }
1054
adam radford9c915a82010-12-21 13:34:31 -08001055 for (i = 0; i < 2; i++) {
1056 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301057 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08001058 &fusion->ld_map_phys[i],
1059 GFP_KERNEL);
1060 if (!fusion->ld_map[i]) {
1061 printk(KERN_ERR "megasas: Could not allocate memory "
1062 "for map info\n");
1063 goto fail_map_info;
1064 }
1065 }
1066
1067 if (!megasas_get_map_info(instance))
1068 megasas_sync_map_info(instance);
1069
1070 return 0;
1071
adam radford9c915a82010-12-21 13:34:31 -08001072fail_map_info:
1073 if (i == 1)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301074 dma_free_coherent(&instance->pdev->dev, fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08001075 fusion->ld_map[0], fusion->ld_map_phys[0]);
1076fail_ioc_init:
adam radfordeb1b1232011-02-24 20:55:56 -08001077 megasas_free_cmds_fusion(instance);
1078fail_alloc_cmds:
1079 megasas_free_cmds(instance);
1080fail_alloc_mfi_cmds:
adam radford9c915a82010-12-21 13:34:31 -08001081 return 1;
1082}
1083
1084/**
1085 * megasas_fire_cmd_fusion - Sends command to the FW
1086 * @frame_phys_addr : Physical address of cmd
1087 * @frame_count : Number of frames for the command
1088 * @regs : MFI register set
1089 */
1090void
1091megasas_fire_cmd_fusion(struct megasas_instance *instance,
1092 dma_addr_t req_desc_lo,
1093 u32 req_desc_hi,
1094 struct megasas_register_set __iomem *regs)
1095{
Sumit.Saxena@avagotech.com07560402014-09-12 18:57:18 +05301096#if defined(writeq) && defined(CONFIG_64BIT)
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05301097 u64 req_data = (((u64)le32_to_cpu(req_desc_hi) << 32) |
1098 le32_to_cpu(req_desc_lo));
Sumit.Saxena@avagotech.com07560402014-09-12 18:57:18 +05301099
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05301100 writeq(req_data, &(regs)->inbound_low_queue_port);
Sumit.Saxena@avagotech.com07560402014-09-12 18:57:18 +05301101#else
adam radford9c915a82010-12-21 13:34:31 -08001102 unsigned long flags;
1103
1104 spin_lock_irqsave(&instance->hba_lock, flags);
1105
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301106 writel(le32_to_cpu(req_desc_lo), &(regs)->inbound_low_queue_port);
1107 writel(le32_to_cpu(req_desc_hi), &(regs)->inbound_high_queue_port);
adam radford9c915a82010-12-21 13:34:31 -08001108 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumit.Saxena@avagotech.com07560402014-09-12 18:57:18 +05301109#endif
adam radford9c915a82010-12-21 13:34:31 -08001110}
1111
1112/**
1113 * map_cmd_status - Maps FW cmd status to OS cmd status
1114 * @cmd : Pointer to cmd
1115 * @status : status of cmd returned by FW
1116 * @ext_status : ext status of cmd returned by FW
1117 */
1118
1119void
1120map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
1121{
1122
1123 switch (status) {
1124
1125 case MFI_STAT_OK:
1126 cmd->scmd->result = DID_OK << 16;
1127 break;
1128
1129 case MFI_STAT_SCSI_IO_FAILED:
1130 case MFI_STAT_LD_INIT_IN_PROGRESS:
1131 cmd->scmd->result = (DID_ERROR << 16) | ext_status;
1132 break;
1133
1134 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1135
1136 cmd->scmd->result = (DID_OK << 16) | ext_status;
1137 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1138 memset(cmd->scmd->sense_buffer, 0,
1139 SCSI_SENSE_BUFFERSIZE);
1140 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1141 SCSI_SENSE_BUFFERSIZE);
1142 cmd->scmd->result |= DRIVER_SENSE << 24;
1143 }
1144 break;
1145
1146 case MFI_STAT_LD_OFFLINE:
1147 case MFI_STAT_DEVICE_NOT_FOUND:
1148 cmd->scmd->result = DID_BAD_TARGET << 16;
1149 break;
adam radford36807e62011-10-08 18:15:06 -07001150 case MFI_STAT_CONFIG_SEQ_MISMATCH:
1151 cmd->scmd->result = DID_IMM_RETRY << 16;
1152 break;
adam radford9c915a82010-12-21 13:34:31 -08001153 default:
1154 printk(KERN_DEBUG "megasas: FW status %#x\n", status);
1155 cmd->scmd->result = DID_ERROR << 16;
1156 break;
1157 }
1158}
1159
1160/**
1161 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1162 * @instance: Adapter soft state
1163 * @scp: SCSI command from the mid-layer
1164 * @sgl_ptr: SGL to be filled in
1165 * @cmd: cmd we are working on
1166 *
1167 * If successful, this function returns the number of SG elements.
1168 */
1169static int
1170megasas_make_sgl_fusion(struct megasas_instance *instance,
1171 struct scsi_cmnd *scp,
1172 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1173 struct megasas_cmd_fusion *cmd)
1174{
adam radford36807e62011-10-08 18:15:06 -07001175 int i, sg_processed, sge_count;
adam radford9c915a82010-12-21 13:34:31 -08001176 struct scatterlist *os_sgl;
1177 struct fusion_context *fusion;
1178
1179 fusion = instance->ctrl_context;
1180
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301181 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1182 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001183 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
1184 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1185 sgl_ptr_end->Flags = 0;
1186 }
adam radford9c915a82010-12-21 13:34:31 -08001187
1188 sge_count = scsi_dma_map(scp);
1189
1190 BUG_ON(sge_count < 0);
1191
1192 if (sge_count > instance->max_num_sge || !sge_count)
1193 return sge_count;
1194
adam radford9c915a82010-12-21 13:34:31 -08001195 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301196 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
1197 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
adam radford9c915a82010-12-21 13:34:31 -08001198 sgl_ptr->Flags = 0;
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301199 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1200 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001201 if (i == sge_count - 1)
1202 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1203 }
adam radford9c915a82010-12-21 13:34:31 -08001204 sgl_ptr++;
1205
1206 sg_processed = i + 1;
1207
1208 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
1209 (sge_count > fusion->max_sge_in_main_msg)) {
1210
1211 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301212 if ((instance->pdev->device ==
1213 PCI_DEVICE_ID_LSI_INVADER) ||
1214 (instance->pdev->device ==
1215 PCI_DEVICE_ID_LSI_FURY)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301216 if ((le16_to_cpu(cmd->io_request->IoFlags) &
1217 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
1218 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
adam radford36807e62011-10-08 18:15:06 -07001219 cmd->io_request->ChainOffset =
1220 fusion->
1221 chain_offset_io_request;
1222 else
1223 cmd->io_request->ChainOffset = 0;
1224 } else
1225 cmd->io_request->ChainOffset =
1226 fusion->chain_offset_io_request;
1227
adam radford9c915a82010-12-21 13:34:31 -08001228 sg_chain = sgl_ptr;
1229 /* Prepare chain element */
1230 sg_chain->NextChainOffset = 0;
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301231 if ((instance->pdev->device ==
1232 PCI_DEVICE_ID_LSI_INVADER) ||
1233 (instance->pdev->device ==
1234 PCI_DEVICE_ID_LSI_FURY))
adam radford36807e62011-10-08 18:15:06 -07001235 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1236 else
1237 sg_chain->Flags =
1238 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1239 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301240 sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
1241 sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08001242
1243 sgl_ptr =
1244 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301245 memset(sgl_ptr, 0, MEGASAS_MAX_SZ_CHAIN_FRAME);
adam radford9c915a82010-12-21 13:34:31 -08001246 }
1247 }
1248
1249 return sge_count;
1250}
1251
1252/**
1253 * megasas_set_pd_lba - Sets PD LBA
1254 * @cdb: CDB
1255 * @cdb_len: cdb length
1256 * @start_blk: Start block of IO
1257 *
1258 * Used to set the PD LBA in CDB for FP IOs
1259 */
1260void
1261megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1262 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301263 struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
adam radford9c915a82010-12-21 13:34:31 -08001264{
1265 struct MR_LD_RAID *raid;
1266 u32 ld;
1267 u64 start_blk = io_info->pdBlock;
1268 u8 *cdb = io_request->CDB.CDB32;
1269 u32 num_blocks = io_info->numBlocks;
adam radford495c5602011-05-11 18:34:40 -07001270 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
adam radford9c915a82010-12-21 13:34:31 -08001271
1272 /* Check if T10 PI (DIF) is enabled for this LD */
1273 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1274 raid = MR_LdRaidGet(ld, local_map_ptr);
1275 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1276 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1277 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1278 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
1279
1280 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1281 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1282 else
1283 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1284 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1285
1286 /* LBA */
1287 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1288 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1289 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1290 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1291 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1292 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1293 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1294 cdb[19] = (u8)(start_blk & 0xff);
1295
1296 /* Logical block reference tag */
1297 io_request->CDB.EEDP32.PrimaryReferenceTag =
1298 cpu_to_be32(ref_tag);
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05301299 io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301300 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08001301
1302 /* Transfer length */
1303 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1304 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1305 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1306 cdb[31] = (u8)(num_blocks & 0xff);
1307
1308 /* set SCSI IO EEDPFlags */
1309 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301310 io_request->EEDPFlags = cpu_to_le16(
adam radford9c915a82010-12-21 13:34:31 -08001311 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1312 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1313 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1314 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301315 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
adam radford9c915a82010-12-21 13:34:31 -08001316 } else {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301317 io_request->EEDPFlags = cpu_to_le16(
adam radford9c915a82010-12-21 13:34:31 -08001318 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301319 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
adam radford9c915a82010-12-21 13:34:31 -08001320 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301321 io_request->Control |= cpu_to_le32((0x4 << 26));
1322 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
adam radford9c915a82010-12-21 13:34:31 -08001323 } else {
1324 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1325 if (((cdb_len == 12) || (cdb_len == 16)) &&
1326 (start_blk <= 0xffffffff)) {
1327 if (cdb_len == 16) {
1328 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1329 flagvals = cdb[1];
1330 groupnum = cdb[14];
1331 control = cdb[15];
1332 } else {
1333 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1334 flagvals = cdb[1];
1335 groupnum = cdb[10];
1336 control = cdb[11];
1337 }
1338
1339 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1340
1341 cdb[0] = opcode;
1342 cdb[1] = flagvals;
1343 cdb[6] = groupnum;
1344 cdb[9] = control;
1345
1346 /* Transfer length */
1347 cdb[8] = (u8)(num_blocks & 0xff);
1348 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1349
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301350 io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08001351 cdb_len = 10;
adam radford495c5602011-05-11 18:34:40 -07001352 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1353 /* Convert to 16 byte CDB for large LBA's */
1354 switch (cdb_len) {
1355 case 6:
1356 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1357 control = cdb[5];
1358 break;
1359 case 10:
1360 opcode =
1361 cdb[0] == READ_10 ? READ_16 : WRITE_16;
1362 flagvals = cdb[1];
1363 groupnum = cdb[6];
1364 control = cdb[9];
1365 break;
1366 case 12:
1367 opcode =
1368 cdb[0] == READ_12 ? READ_16 : WRITE_16;
1369 flagvals = cdb[1];
1370 groupnum = cdb[10];
1371 control = cdb[11];
1372 break;
1373 }
1374
1375 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1376
1377 cdb[0] = opcode;
1378 cdb[1] = flagvals;
1379 cdb[14] = groupnum;
1380 cdb[15] = control;
1381
1382 /* Transfer length */
1383 cdb[13] = (u8)(num_blocks & 0xff);
1384 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1385 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1386 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1387
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301388 io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
adam radford495c5602011-05-11 18:34:40 -07001389 cdb_len = 16;
adam radford9c915a82010-12-21 13:34:31 -08001390 }
1391
1392 /* Normal case, just load LBA here */
1393 switch (cdb_len) {
1394 case 6:
1395 {
1396 u8 val = cdb[1] & 0xE0;
1397 cdb[3] = (u8)(start_blk & 0xff);
1398 cdb[2] = (u8)((start_blk >> 8) & 0xff);
1399 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
1400 break;
1401 }
1402 case 10:
1403 cdb[5] = (u8)(start_blk & 0xff);
1404 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1405 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1406 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1407 break;
1408 case 12:
1409 cdb[5] = (u8)(start_blk & 0xff);
1410 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1411 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1412 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1413 break;
1414 case 16:
1415 cdb[9] = (u8)(start_blk & 0xff);
1416 cdb[8] = (u8)((start_blk >> 8) & 0xff);
1417 cdb[7] = (u8)((start_blk >> 16) & 0xff);
1418 cdb[6] = (u8)((start_blk >> 24) & 0xff);
1419 cdb[5] = (u8)((start_blk >> 32) & 0xff);
1420 cdb[4] = (u8)((start_blk >> 40) & 0xff);
1421 cdb[3] = (u8)((start_blk >> 48) & 0xff);
1422 cdb[2] = (u8)((start_blk >> 56) & 0xff);
1423 break;
1424 }
1425 }
1426}
1427
1428/**
1429 * megasas_build_ldio_fusion - Prepares IOs to devices
1430 * @instance: Adapter soft state
1431 * @scp: SCSI command
1432 * @cmd: Command to be prepared
1433 *
1434 * Prepares the io_request and chain elements (sg_frame) for IO
1435 * The IO can be for PD (Fast Path) or LD
1436 */
1437void
1438megasas_build_ldio_fusion(struct megasas_instance *instance,
1439 struct scsi_cmnd *scp,
1440 struct megasas_cmd_fusion *cmd)
1441{
1442 u8 fp_possible;
adam radfordf9eff812012-10-01 19:27:07 -07001443 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
adam radford9c915a82010-12-21 13:34:31 -08001444 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1445 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1446 struct IO_REQUEST_INFO io_info;
1447 struct fusion_context *fusion;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301448 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
adam radford21c9e162013-09-06 15:27:14 -07001449 u8 *raidLUN;
adam radford9c915a82010-12-21 13:34:31 -08001450
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301451 device_id = MEGASAS_DEV_INDEX(scp);
adam radford9c915a82010-12-21 13:34:31 -08001452
1453 fusion = instance->ctrl_context;
1454
1455 io_request = cmd->io_request;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301456 io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
adam radford9c915a82010-12-21 13:34:31 -08001457 io_request->RaidContext.status = 0;
1458 io_request->RaidContext.exStatus = 0;
1459
1460 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
1461
1462 start_lba_lo = 0;
1463 start_lba_hi = 0;
1464 fp_possible = 0;
1465
1466 /*
1467 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1468 */
1469 if (scp->cmd_len == 6) {
adam radfordf9eff812012-10-01 19:27:07 -07001470 datalength = (u32) scp->cmnd[4];
adam radford9c915a82010-12-21 13:34:31 -08001471 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1472 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1473
1474 start_lba_lo &= 0x1FFFFF;
1475 }
1476
1477 /*
1478 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1479 */
1480 else if (scp->cmd_len == 10) {
adam radfordf9eff812012-10-01 19:27:07 -07001481 datalength = (u32) scp->cmnd[8] |
adam radford9c915a82010-12-21 13:34:31 -08001482 ((u32) scp->cmnd[7] << 8);
1483 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1484 ((u32) scp->cmnd[3] << 16) |
1485 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1486 }
1487
1488 /*
1489 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1490 */
1491 else if (scp->cmd_len == 12) {
adam radfordf9eff812012-10-01 19:27:07 -07001492 datalength = ((u32) scp->cmnd[6] << 24) |
adam radford9c915a82010-12-21 13:34:31 -08001493 ((u32) scp->cmnd[7] << 16) |
1494 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1495 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1496 ((u32) scp->cmnd[3] << 16) |
1497 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1498 }
1499
1500 /*
1501 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1502 */
1503 else if (scp->cmd_len == 16) {
adam radfordf9eff812012-10-01 19:27:07 -07001504 datalength = ((u32) scp->cmnd[10] << 24) |
adam radford9c915a82010-12-21 13:34:31 -08001505 ((u32) scp->cmnd[11] << 16) |
1506 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1507 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1508 ((u32) scp->cmnd[7] << 16) |
1509 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1510
1511 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1512 ((u32) scp->cmnd[3] << 16) |
1513 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1514 }
1515
1516 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
1517 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
adam radfordf9eff812012-10-01 19:27:07 -07001518 io_info.numBlocks = datalength;
adam radford9c915a82010-12-21 13:34:31 -08001519 io_info.ldTgtId = device_id;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301520 io_request->DataLength = cpu_to_le32(scsi_bufflen(scp));
adam radford9c915a82010-12-21 13:34:31 -08001521
1522 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1523 io_info.isRead = 1;
1524
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301525 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
adam radford9c915a82010-12-21 13:34:31 -08001526
1527 if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301528 instance->fw_supported_vd_count) || (!fusion->fast_path_io)) {
adam radford9c915a82010-12-21 13:34:31 -08001529 io_request->RaidContext.regLockFlags = 0;
1530 fp_possible = 0;
1531 } else {
adam radford36807e62011-10-08 18:15:06 -07001532 if (MR_BuildRaidContext(instance, &io_info,
1533 &io_request->RaidContext,
adam radford21c9e162013-09-06 15:27:14 -07001534 local_map_ptr, &raidLUN))
adam radford9c915a82010-12-21 13:34:31 -08001535 fp_possible = io_info.fpOkForIo;
1536 }
1537
Christoph Hellwig16b85282015-04-15 09:44:37 -07001538 /* Use raw_smp_processor_id() for now until cmd->request->cpu is CPU
adam radfordc8e858f2011-10-08 18:15:13 -07001539 id by default, not CPU group id, otherwise all MSI-X queues won't
1540 be utilized */
1541 cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
Christoph Hellwig16b85282015-04-15 09:44:37 -07001542 raw_smp_processor_id() % instance->msix_vectors : 0;
adam radfordc8e858f2011-10-08 18:15:13 -07001543
adam radford9c915a82010-12-21 13:34:31 -08001544 if (fp_possible) {
1545 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
1546 local_map_ptr, start_lba_lo);
adam radford9c915a82010-12-21 13:34:31 -08001547 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1548 cmd->request_desc->SCSIIO.RequestFlags =
1549 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1550 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301551 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1552 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001553 if (io_request->RaidContext.regLockFlags ==
1554 REGION_TYPE_UNUSED)
1555 cmd->request_desc->SCSIIO.RequestFlags =
1556 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1557 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1558 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1559 io_request->RaidContext.nseg = 0x1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301560 io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
adam radford36807e62011-10-08 18:15:06 -07001561 io_request->RaidContext.regLockFlags |=
1562 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
1563 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1564 }
adam radford9c915a82010-12-21 13:34:31 -08001565 if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
1566 (io_info.isRead)) {
1567 io_info.devHandle =
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05301568 get_updated_dev_handle(instance,
adam radford9c915a82010-12-21 13:34:31 -08001569 &fusion->load_balance_info[device_id],
1570 &io_info);
1571 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05301572 cmd->pd_r1_lb = io_info.pd_after_lb;
adam radford9c915a82010-12-21 13:34:31 -08001573 } else
1574 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
1575 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
1576 io_request->DevHandle = io_info.devHandle;
adam radford21c9e162013-09-06 15:27:14 -07001577 /* populate the LUN field */
1578 memcpy(io_request->LUN, raidLUN, 8);
adam radford9c915a82010-12-21 13:34:31 -08001579 } else {
1580 io_request->RaidContext.timeoutValue =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301581 cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
adam radford9c915a82010-12-21 13:34:31 -08001582 cmd->request_desc->SCSIIO.RequestFlags =
1583 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1584 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301585 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1586 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001587 if (io_request->RaidContext.regLockFlags ==
1588 REGION_TYPE_UNUSED)
1589 cmd->request_desc->SCSIIO.RequestFlags =
1590 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1591 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1592 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1593 io_request->RaidContext.regLockFlags |=
1594 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
1595 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1596 io_request->RaidContext.nseg = 0x1;
1597 }
1598 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301599 io_request->DevHandle = cpu_to_le16(device_id);
adam radford9c915a82010-12-21 13:34:31 -08001600 } /* Not FP */
1601}
1602
1603/**
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301604 * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
adam radford9c915a82010-12-21 13:34:31 -08001605 * @instance: Adapter soft state
1606 * @scp: SCSI command
1607 * @cmd: Command to be prepared
1608 *
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301609 * Prepares the io_request frame for non-rw io cmds for vd.
adam radford9c915a82010-12-21 13:34:31 -08001610 */
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301611static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance,
1612 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd)
adam radford9c915a82010-12-21 13:34:31 -08001613{
1614 u32 device_id;
1615 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1616 u16 pd_index = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301617 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
adam radford9c915a82010-12-21 13:34:31 -08001618 struct fusion_context *fusion = instance->ctrl_context;
adam radford21c9e162013-09-06 15:27:14 -07001619 u8 span, physArm;
1620 u16 devHandle;
1621 u32 ld, arRef, pd;
1622 struct MR_LD_RAID *raid;
1623 struct RAID_CONTEXT *pRAID_Context;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301624 u8 fp_possible = 1;
adam radford9c915a82010-12-21 13:34:31 -08001625
1626 io_request = cmd->io_request;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301627 device_id = MEGASAS_DEV_INDEX(scmd);
1628 pd_index = MEGASAS_PD_INDEX(scmd);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301629 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301630 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301631 /* get RAID_Context pointer */
1632 pRAID_Context = &io_request->RaidContext;
1633 /* Check with FW team */
1634 pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
1635 pRAID_Context->regLockRowLBA = 0;
1636 pRAID_Context->regLockLength = 0;
adam radford21c9e162013-09-06 15:27:14 -07001637
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301638 if (fusion->fast_path_io && (
1639 device_id < instance->fw_supported_vd_count)) {
Sumit.Saxena@avagotech.comab2f0602015-01-05 20:06:08 +05301640
adam radford21c9e162013-09-06 15:27:14 -07001641 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
Sumit.Saxena@avagotech.comab2f0602015-01-05 20:06:08 +05301642 if (ld >= instance->fw_supported_vd_count)
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301643 fp_possible = 0;
adam radford21c9e162013-09-06 15:27:14 -07001644
1645 raid = MR_LdRaidGet(ld, local_map_ptr);
adam radford21c9e162013-09-06 15:27:14 -07001646 if (!(raid->capability.fpNonRWCapable))
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301647 fp_possible = 0;
1648 } else
1649 fp_possible = 0;
adam radford21c9e162013-09-06 15:27:14 -07001650
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301651 if (!fp_possible) {
1652 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1653 io_request->DevHandle = cpu_to_le16(device_id);
1654 io_request->LUN[1] = scmd->device->lun;
1655 pRAID_Context->timeoutValue =
1656 cpu_to_le16 (scmd->request->timeout / HZ);
1657 cmd->request_desc->SCSIIO.RequestFlags =
1658 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1659 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1660 } else {
adam radford21c9e162013-09-06 15:27:14 -07001661
1662 /* set RAID context values */
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301663 pRAID_Context->configSeqNum = raid->seqNum;
1664 pRAID_Context->regLockFlags = REGION_TYPE_SHARED_READ;
1665 pRAID_Context->timeoutValue = cpu_to_le16(raid->fpIoTimeoutForLd);
adam radford21c9e162013-09-06 15:27:14 -07001666
1667 /* get the DevHandle for the PD (since this is
1668 fpNonRWCapable, this is a single disk RAID0) */
1669 span = physArm = 0;
1670 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
1671 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
1672 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
1673
1674 /* build request descriptor */
1675 cmd->request_desc->SCSIIO.RequestFlags =
1676 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301677 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford21c9e162013-09-06 15:27:14 -07001678 cmd->request_desc->SCSIIO.DevHandle = devHandle;
1679
1680 /* populate the LUN field */
1681 memcpy(io_request->LUN, raid->LUN, 8);
1682
1683 /* build the raidScsiIO structure */
1684 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1685 io_request->DevHandle = devHandle;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301686 }
1687}
adam radford21c9e162013-09-06 15:27:14 -07001688
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301689/**
1690 * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
1691 * @instance: Adapter soft state
1692 * @scp: SCSI command
1693 * @cmd: Command to be prepared
1694 * @fp_possible: parameter to detect fast path or firmware path io.
1695 *
1696 * Prepares the io_request frame for rw/non-rw io cmds for syspds
1697 */
1698static void
1699megasas_build_syspd_fusion(struct megasas_instance *instance,
1700 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd, u8 fp_possible)
1701{
1702 u32 device_id;
1703 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1704 u16 pd_index = 0;
1705 u16 os_timeout_value;
1706 u16 timeout_limit;
1707 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1708 struct RAID_CONTEXT *pRAID_Context;
1709 struct fusion_context *fusion = instance->ctrl_context;
adam radford21c9e162013-09-06 15:27:14 -07001710
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301711 device_id = MEGASAS_DEV_INDEX(scmd);
1712 pd_index = MEGASAS_PD_INDEX(scmd);
1713 os_timeout_value = scmd->request->timeout / HZ;
1714
1715 io_request = cmd->io_request;
1716 /* get RAID_Context pointer */
1717 pRAID_Context = &io_request->RaidContext;
1718 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
1719 io_request->LUN[1] = scmd->device->lun;
1720 pRAID_Context->RAIDFlags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
1721 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
1722
1723 pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
1724 pRAID_Context->configSeqNum = 0;
1725 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1726 io_request->DevHandle =
1727 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1728
1729 cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
1730 cmd->request_desc->SCSIIO.MSIxIndex =
1731 instance->msix_vectors ?
1732 (raw_smp_processor_id() % instance->msix_vectors) : 0;
1733
1734
1735 if (!fp_possible) {
1736 /* system pd firmware path */
adam radford9c915a82010-12-21 13:34:31 -08001737 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
adam radford9c915a82010-12-21 13:34:31 -08001738 cmd->request_desc->SCSIIO.RequestFlags =
1739 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301740 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1741 pRAID_Context->timeoutValue = cpu_to_le16(os_timeout_value);
1742 } else {
1743 /* system pd Fast Path */
1744 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1745 pRAID_Context->regLockFlags = 0;
1746 pRAID_Context->regLockRowLBA = 0;
1747 pRAID_Context->regLockLength = 0;
1748 timeout_limit = (scmd->device->type == TYPE_DISK) ?
1749 255 : 0xFFFF;
1750 pRAID_Context->timeoutValue =
1751 cpu_to_le16((os_timeout_value > timeout_limit) ?
1752 timeout_limit : os_timeout_value);
1753 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1754 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1755 cmd->request_desc->SCSIIO.RequestFlags |=
1756 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1757 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1758 pRAID_Context->Type = MPI2_TYPE_CUDA;
1759 pRAID_Context->nseg = 0x1;
1760 io_request->IoFlags |=
1761 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
1762 }
1763 cmd->request_desc->SCSIIO.RequestFlags =
1764 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1765 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford9c915a82010-12-21 13:34:31 -08001766 }
adam radford9c915a82010-12-21 13:34:31 -08001767}
1768
1769/**
1770 * megasas_build_io_fusion - Prepares IOs to devices
1771 * @instance: Adapter soft state
1772 * @scp: SCSI command
1773 * @cmd: Command to be prepared
1774 *
1775 * Invokes helper functions to prepare request frames
1776 * and sets flags appropriate for IO/Non-IO cmd
1777 */
1778int
1779megasas_build_io_fusion(struct megasas_instance *instance,
1780 struct scsi_cmnd *scp,
1781 struct megasas_cmd_fusion *cmd)
1782{
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301783 u32 sge_count;
1784 u8 cmd_type;
adam radford9c915a82010-12-21 13:34:31 -08001785 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
1786
adam radford9c915a82010-12-21 13:34:31 -08001787 /* Zero out some fields so they don't get reused */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001788 memset(io_request->LUN, 0x0, 8);
adam radford9c915a82010-12-21 13:34:31 -08001789 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
1790 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
1791 io_request->EEDPFlags = 0;
1792 io_request->Control = 0;
1793 io_request->EEDPBlockSize = 0;
adam radford36807e62011-10-08 18:15:06 -07001794 io_request->ChainOffset = 0;
adam radford9c915a82010-12-21 13:34:31 -08001795 io_request->RaidContext.RAIDFlags = 0;
adam radford36807e62011-10-08 18:15:06 -07001796 io_request->RaidContext.Type = 0;
1797 io_request->RaidContext.nseg = 0;
adam radford9c915a82010-12-21 13:34:31 -08001798
1799 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
1800 /*
1801 * Just the CDB length,rest of the Flags are zero
1802 * This will be modified for FP in build_ldio_fusion
1803 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301804 io_request->IoFlags = cpu_to_le16(scp->cmd_len);
adam radford9c915a82010-12-21 13:34:31 -08001805
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301806 switch (cmd_type = megasas_cmd_type(scp)) {
1807 case READ_WRITE_LDIO:
adam radford9c915a82010-12-21 13:34:31 -08001808 megasas_build_ldio_fusion(instance, scp, cmd);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301809 break;
1810 case NON_READ_WRITE_LDIO:
1811 megasas_build_ld_nonrw_fusion(instance, scp, cmd);
1812 break;
1813 case READ_WRITE_SYSPDIO:
1814 case NON_READ_WRITE_SYSPDIO:
1815 if (instance->secure_jbod_support &&
1816 (cmd_type == NON_READ_WRITE_SYSPDIO))
1817 megasas_build_syspd_fusion(instance, scp, cmd, 0);
1818 else
1819 megasas_build_syspd_fusion(instance, scp, cmd, 1);
1820 break;
1821 default:
1822 break;
1823 }
adam radford9c915a82010-12-21 13:34:31 -08001824
1825 /*
1826 * Construct SGL
1827 */
1828
1829 sge_count =
1830 megasas_make_sgl_fusion(instance, scp,
1831 (struct MPI25_IEEE_SGE_CHAIN64 *)
1832 &io_request->SGL, cmd);
1833
1834 if (sge_count > instance->max_num_sge) {
1835 printk(KERN_ERR "megasas: Error. sge_count (0x%x) exceeds "
1836 "max (0x%x) allowed\n", sge_count,
1837 instance->max_num_sge);
1838 return 1;
1839 }
1840
1841 io_request->RaidContext.numSGE = sge_count;
1842
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301843 io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
adam radford9c915a82010-12-21 13:34:31 -08001844
1845 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301846 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
adam radford9c915a82010-12-21 13:34:31 -08001847 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301848 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
adam radford9c915a82010-12-21 13:34:31 -08001849
1850 io_request->SGLOffset0 =
1851 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
1852
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301853 io_request->SenseBufferLowAddress = cpu_to_le32(cmd->sense_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08001854 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
1855
1856 cmd->scmd = scp;
1857 scp->SCp.ptr = (char *)cmd;
1858
1859 return 0;
1860}
1861
1862union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1863megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
1864{
1865 u8 *p;
1866 struct fusion_context *fusion;
1867
1868 if (index >= instance->max_fw_cmds) {
1869 printk(KERN_ERR "megasas: Invalid SMID (0x%x)request for "
adam radford229fe472014-03-10 02:51:56 -07001870 "descriptor for scsi%d\n", index,
1871 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08001872 return NULL;
1873 }
1874 fusion = instance->ctrl_context;
1875 p = fusion->req_frames_desc
1876 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
1877
1878 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
1879}
1880
1881/**
1882 * megasas_build_and_issue_cmd_fusion -Main routine for building and
1883 * issuing non IOCTL cmd
1884 * @instance: Adapter soft state
1885 * @scmd: pointer to scsi cmd from OS
1886 */
1887static u32
1888megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
1889 struct scsi_cmnd *scmd)
1890{
1891 struct megasas_cmd_fusion *cmd;
1892 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1893 u32 index;
1894 struct fusion_context *fusion;
1895
1896 fusion = instance->ctrl_context;
1897
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05301898 cmd = megasas_get_cmd_fusion(instance, scmd->request->tag);
adam radford9c915a82010-12-21 13:34:31 -08001899
1900 index = cmd->index;
1901
1902 req_desc = megasas_get_request_descriptor(instance, index-1);
1903 if (!req_desc)
1904 return 1;
1905
1906 req_desc->Words = 0;
1907 cmd->request_desc = req_desc;
adam radford9c915a82010-12-21 13:34:31 -08001908
1909 if (megasas_build_io_fusion(instance, scmd, cmd)) {
1910 megasas_return_cmd_fusion(instance, cmd);
1911 printk(KERN_ERR "megasas: Error building command.\n");
1912 cmd->request_desc = NULL;
1913 return 1;
1914 }
1915
1916 req_desc = cmd->request_desc;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301917 req_desc->SCSIIO.SMID = cpu_to_le16(index);
adam radford9c915a82010-12-21 13:34:31 -08001918
1919 if (cmd->io_request->ChainOffset != 0 &&
1920 cmd->io_request->ChainOffset != 0xF)
1921 printk(KERN_ERR "megasas: The chain offset value is not "
1922 "correct : %x\n", cmd->io_request->ChainOffset);
1923
1924 /*
1925 * Issue the command to the FW
1926 */
1927 atomic_inc(&instance->fw_outstanding);
1928
1929 instance->instancet->fire_cmd(instance,
1930 req_desc->u.low, req_desc->u.high,
1931 instance->reg_set);
1932
1933 return 0;
1934}
1935
1936/**
1937 * complete_cmd_fusion - Completes command
1938 * @instance: Adapter soft state
1939 * Completes all commands that is in reply descriptor queue
1940 */
1941int
adam radfordc8e858f2011-10-08 18:15:13 -07001942complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
adam radford9c915a82010-12-21 13:34:31 -08001943{
1944 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
1945 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
1946 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
1947 struct fusion_context *fusion;
1948 struct megasas_cmd *cmd_mfi;
1949 struct megasas_cmd_fusion *cmd_fusion;
1950 u16 smid, num_completed;
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05301951 u8 reply_descript_type;
adam radford9c915a82010-12-21 13:34:31 -08001952 u32 status, extStatus, device_id;
1953 union desc_value d_val;
1954 struct LD_LOAD_BALANCE_INFO *lbinfo;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05301955 int threshold_reply_count = 0;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05301956 struct scsi_cmnd *scmd_local = NULL;
adam radford9c915a82010-12-21 13:34:31 -08001957
1958 fusion = instance->ctrl_context;
1959
1960 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1961 return IRQ_HANDLED;
1962
1963 desc = fusion->reply_frames_desc;
adam radfordc8e858f2011-10-08 18:15:13 -07001964 desc += ((MSIxIndex * fusion->reply_alloc_sz)/
1965 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)) +
1966 fusion->last_reply_idx[MSIxIndex];
adam radford9c915a82010-12-21 13:34:31 -08001967
1968 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1969
1970 d_val.word = desc->Words;
1971
1972 reply_descript_type = reply_desc->ReplyFlags &
1973 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1974
1975 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1976 return IRQ_NONE;
1977
adam radford9c915a82010-12-21 13:34:31 -08001978 num_completed = 0;
1979
1980 while ((d_val.u.low != UINT_MAX) && (d_val.u.high != UINT_MAX)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301981 smid = le16_to_cpu(reply_desc->SMID);
adam radford9c915a82010-12-21 13:34:31 -08001982
1983 cmd_fusion = fusion->cmd_list[smid - 1];
1984
1985 scsi_io_req =
1986 (struct MPI2_RAID_SCSI_IO_REQUEST *)
1987 cmd_fusion->io_request;
1988
1989 if (cmd_fusion->scmd)
1990 cmd_fusion->scmd->SCp.ptr = NULL;
1991
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05301992 scmd_local = cmd_fusion->scmd;
adam radford9c915a82010-12-21 13:34:31 -08001993 status = scsi_io_req->RaidContext.status;
1994 extStatus = scsi_io_req->RaidContext.exStatus;
1995
1996 switch (scsi_io_req->Function) {
1997 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
1998 /* Update load balancing info */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05301999 device_id = MEGASAS_DEV_INDEX(scmd_local);
adam radford9c915a82010-12-21 13:34:31 -08002000 lbinfo = &fusion->load_balance_info[device_id];
2001 if (cmd_fusion->scmd->SCp.Status &
2002 MEGASAS_LOAD_BALANCE_FLAG) {
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05302003 atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
adam radford9c915a82010-12-21 13:34:31 -08002004 cmd_fusion->scmd->SCp.Status &=
2005 ~MEGASAS_LOAD_BALANCE_FLAG;
2006 }
2007 if (reply_descript_type ==
2008 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
2009 if (megasas_dbg_lvl == 5)
2010 printk(KERN_ERR "\nmegasas: FAST Path "
2011 "IO Success\n");
2012 }
2013 /* Fall thru and complete IO */
2014 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
2015 /* Map the FW Cmd Status */
2016 map_cmd_status(cmd_fusion, status, extStatus);
adam radford9c915a82010-12-21 13:34:31 -08002017 scsi_io_req->RaidContext.status = 0;
2018 scsi_io_req->RaidContext.exStatus = 0;
2019 megasas_return_cmd_fusion(instance, cmd_fusion);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302020 scsi_dma_unmap(scmd_local);
2021 scmd_local->scsi_done(scmd_local);
adam radford9c915a82010-12-21 13:34:31 -08002022 atomic_dec(&instance->fw_outstanding);
2023
2024 break;
2025 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
2026 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302027
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302028 /* Poll mode. Dummy free.
2029 * In case of Interrupt mode, caller has reverse check.
2030 */
2031 if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
2032 cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
2033 megasas_return_cmd(instance, cmd_mfi);
2034 } else
2035 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
adam radford9c915a82010-12-21 13:34:31 -08002036 break;
2037 }
2038
adam radfordc8e858f2011-10-08 18:15:13 -07002039 fusion->last_reply_idx[MSIxIndex]++;
2040 if (fusion->last_reply_idx[MSIxIndex] >=
2041 fusion->reply_q_depth)
2042 fusion->last_reply_idx[MSIxIndex] = 0;
adam radford9c915a82010-12-21 13:34:31 -08002043
2044 desc->Words = ULLONG_MAX;
2045 num_completed++;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05302046 threshold_reply_count++;
adam radford9c915a82010-12-21 13:34:31 -08002047
2048 /* Get the next reply descriptor */
adam radfordc8e858f2011-10-08 18:15:13 -07002049 if (!fusion->last_reply_idx[MSIxIndex])
2050 desc = fusion->reply_frames_desc +
2051 ((MSIxIndex * fusion->reply_alloc_sz)/
2052 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION));
adam radford9c915a82010-12-21 13:34:31 -08002053 else
2054 desc++;
2055
2056 reply_desc =
2057 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
2058
2059 d_val.word = desc->Words;
2060
2061 reply_descript_type = reply_desc->ReplyFlags &
2062 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
2063
2064 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
2065 break;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05302066 /*
2067 * Write to reply post host index register after completing threshold
2068 * number of reply counts and still there are more replies in reply queue
2069 * pending to be completed
2070 */
2071 if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
2072 if ((instance->pdev->device ==
2073 PCI_DEVICE_ID_LSI_INVADER) ||
2074 (instance->pdev->device ==
2075 PCI_DEVICE_ID_LSI_FURY))
2076 writel(((MSIxIndex & 0x7) << 24) |
2077 fusion->last_reply_idx[MSIxIndex],
2078 instance->reply_post_host_index_addr[MSIxIndex/8]);
2079 else
2080 writel((MSIxIndex << 24) |
2081 fusion->last_reply_idx[MSIxIndex],
2082 instance->reply_post_host_index_addr[0]);
2083 threshold_reply_count = 0;
2084 }
adam radford9c915a82010-12-21 13:34:31 -08002085 }
2086
2087 if (!num_completed)
2088 return IRQ_NONE;
2089
2090 wmb();
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302091 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2092 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
2093 writel(((MSIxIndex & 0x7) << 24) |
2094 fusion->last_reply_idx[MSIxIndex],
2095 instance->reply_post_host_index_addr[MSIxIndex/8]);
2096 else
2097 writel((MSIxIndex << 24) |
2098 fusion->last_reply_idx[MSIxIndex],
2099 instance->reply_post_host_index_addr[0]);
adam radford53ef2bb2011-02-24 20:56:05 -08002100 megasas_check_and_restore_queue_depth(instance);
adam radford9c915a82010-12-21 13:34:31 -08002101 return IRQ_HANDLED;
2102}
2103
2104/**
2105 * megasas_complete_cmd_dpc_fusion - Completes command
2106 * @instance: Adapter soft state
2107 *
2108 * Tasklet to complete cmds
2109 */
2110void
2111megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
2112{
2113 struct megasas_instance *instance =
2114 (struct megasas_instance *)instance_addr;
2115 unsigned long flags;
adam radfordc8e858f2011-10-08 18:15:13 -07002116 u32 count, MSIxIndex;
2117
2118 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
adam radford9c915a82010-12-21 13:34:31 -08002119
2120 /* If we have already declared adapter dead, donot complete cmds */
2121 spin_lock_irqsave(&instance->hba_lock, flags);
2122 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2123 spin_unlock_irqrestore(&instance->hba_lock, flags);
2124 return;
2125 }
2126 spin_unlock_irqrestore(&instance->hba_lock, flags);
2127
adam radfordc8e858f2011-10-08 18:15:13 -07002128 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
2129 complete_cmd_fusion(instance, MSIxIndex);
adam radford9c915a82010-12-21 13:34:31 -08002130}
2131
2132/**
2133 * megasas_isr_fusion - isr entry point
2134 */
2135irqreturn_t megasas_isr_fusion(int irq, void *devp)
2136{
adam radfordc8e858f2011-10-08 18:15:13 -07002137 struct megasas_irq_context *irq_context = devp;
2138 struct megasas_instance *instance = irq_context->instance;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302139 u32 mfiStatus, fw_state, dma_state;
adam radford9c915a82010-12-21 13:34:31 -08002140
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302141 if (instance->mask_interrupts)
2142 return IRQ_NONE;
2143
adam radfordc8e858f2011-10-08 18:15:13 -07002144 if (!instance->msix_vectors) {
adam radford9c915a82010-12-21 13:34:31 -08002145 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
2146 if (!mfiStatus)
2147 return IRQ_NONE;
2148 }
2149
2150 /* If we are resetting, bail */
adam radford6497b242011-10-08 18:14:50 -07002151 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
2152 instance->instancet->clear_intr(instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -08002153 return IRQ_HANDLED;
adam radford6497b242011-10-08 18:14:50 -07002154 }
adam radford9c915a82010-12-21 13:34:31 -08002155
adam radfordc8e858f2011-10-08 18:15:13 -07002156 if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
adam radford6497b242011-10-08 18:14:50 -07002157 instance->instancet->clear_intr(instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -08002158 /* If we didn't complete any commands, check for FW fault */
2159 fw_state = instance->instancet->read_fw_status_reg(
2160 instance->reg_set) & MFI_STATE_MASK;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302161 dma_state = instance->instancet->read_fw_status_reg
2162 (instance->reg_set) & MFI_STATE_DMADONE;
2163 if (instance->crash_dump_drv_support &&
2164 instance->crash_dump_app_support) {
2165 /* Start collecting crash, if DMA bit is done */
2166 if ((fw_state == MFI_STATE_FAULT) && dma_state)
2167 schedule_work(&instance->crash_init);
2168 else if (fw_state == MFI_STATE_FAULT)
2169 schedule_work(&instance->work_init);
2170 } else if (fw_state == MFI_STATE_FAULT) {
adam radford229fe472014-03-10 02:51:56 -07002171 printk(KERN_WARNING "megaraid_sas: Iop2SysDoorbellInt"
2172 "for scsi%d\n", instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002173 schedule_work(&instance->work_init);
adam radford229fe472014-03-10 02:51:56 -07002174 }
adam radford9c915a82010-12-21 13:34:31 -08002175 }
2176
2177 return IRQ_HANDLED;
2178}
2179
2180/**
2181 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
2182 * @instance: Adapter soft state
2183 * mfi_cmd: megasas_cmd pointer
2184 *
2185 */
2186u8
2187build_mpt_mfi_pass_thru(struct megasas_instance *instance,
2188 struct megasas_cmd *mfi_cmd)
2189{
2190 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
2191 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
2192 struct megasas_cmd_fusion *cmd;
2193 struct fusion_context *fusion;
2194 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
2195
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302196 fusion = instance->ctrl_context;
2197
2198 cmd = megasas_get_cmd_fusion(instance,
2199 instance->max_scsi_cmds + mfi_cmd->index);
adam radford9c915a82010-12-21 13:34:31 -08002200
2201 /* Save the smid. To be used for returning the cmd */
2202 mfi_cmd->context.smid = cmd->index;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302203
adam radford9c915a82010-12-21 13:34:31 -08002204 /*
2205 * For cmds where the flag is set, store the flag and check
2206 * on completion. For cmds with this flag, don't call
2207 * megasas_complete_cmd
2208 */
2209
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05302210 if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302211 mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
adam radford9c915a82010-12-21 13:34:31 -08002212
adam radford9c915a82010-12-21 13:34:31 -08002213 io_req = cmd->io_request;
adam radford36807e62011-10-08 18:15:06 -07002214
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05302215 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2216 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07002217 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
2218 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
2219 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2220 sgl_ptr_end->Flags = 0;
2221 }
2222
adam radford9c915a82010-12-21 13:34:31 -08002223 mpi25_ieee_chain =
2224 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
2225
2226 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
2227 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
2228 SGL) / 4;
2229 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
2230
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302231 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08002232
2233 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2234 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
2235
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302236 mpi25_ieee_chain->Length = cpu_to_le32(MEGASAS_MAX_SZ_CHAIN_FRAME);
adam radford9c915a82010-12-21 13:34:31 -08002237
2238 return 0;
2239}
2240
2241/**
2242 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
2243 * @instance: Adapter soft state
2244 * @cmd: mfi cmd to build
2245 *
2246 */
2247union MEGASAS_REQUEST_DESCRIPTOR_UNION *
2248build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
2249{
2250 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2251 u16 index;
2252
2253 if (build_mpt_mfi_pass_thru(instance, cmd)) {
2254 printk(KERN_ERR "Couldn't build MFI pass thru cmd\n");
2255 return NULL;
2256 }
2257
2258 index = cmd->context.smid;
2259
2260 req_desc = megasas_get_request_descriptor(instance, index - 1);
2261
2262 if (!req_desc)
2263 return NULL;
2264
2265 req_desc->Words = 0;
2266 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2267 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2268
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302269 req_desc->SCSIIO.SMID = cpu_to_le16(index);
adam radford9c915a82010-12-21 13:34:31 -08002270
2271 return req_desc;
2272}
2273
2274/**
2275 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
2276 * @instance: Adapter soft state
2277 * @cmd: mfi cmd pointer
2278 *
2279 */
2280void
2281megasas_issue_dcmd_fusion(struct megasas_instance *instance,
2282 struct megasas_cmd *cmd)
2283{
2284 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
adam radford9c915a82010-12-21 13:34:31 -08002285
2286 req_desc = build_mpt_cmd(instance, cmd);
2287 if (!req_desc) {
2288 printk(KERN_ERR "Couldn't issue MFI pass thru cmd\n");
2289 return;
2290 }
adam radford9c915a82010-12-21 13:34:31 -08002291 instance->instancet->fire_cmd(instance, req_desc->u.low,
2292 req_desc->u.high, instance->reg_set);
2293}
2294
2295/**
2296 * megasas_release_fusion - Reverses the FW initialization
2297 * @intance: Adapter soft state
2298 */
2299void
2300megasas_release_fusion(struct megasas_instance *instance)
2301{
2302 megasas_free_cmds(instance);
2303 megasas_free_cmds_fusion(instance);
2304
2305 iounmap(instance->reg_set);
2306
2307 pci_release_selected_regions(instance->pdev, instance->bar);
2308}
2309
2310/**
2311 * megasas_read_fw_status_reg_fusion - returns the current FW status value
2312 * @regs: MFI register set
2313 */
2314static u32
2315megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
2316{
2317 return readl(&(regs)->outbound_scratch_pad);
2318}
2319
2320/**
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302321 * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
2322 * @instance: Controller's soft instance
2323 * return: Number of allocated host crash buffers
2324 */
2325static void
2326megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
2327{
2328 unsigned int i;
2329
2330 instance->crash_buf_pages = get_order(CRASH_DMA_BUF_SIZE);
2331 for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
2332 instance->crash_buf[i] = (void *)__get_free_pages(GFP_KERNEL,
2333 instance->crash_buf_pages);
2334 if (!instance->crash_buf[i]) {
2335 dev_info(&instance->pdev->dev, "Firmware crash dump "
2336 "memory allocation failed at index %d\n", i);
2337 break;
2338 }
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05302339 memset(instance->crash_buf[i], 0,
2340 ((1 << PAGE_SHIFT) << instance->crash_buf_pages));
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302341 }
2342 instance->drv_buf_alloc = i;
2343}
2344
2345/**
2346 * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
2347 * @instance: Controller's soft instance
2348 */
2349void
2350megasas_free_host_crash_buffer(struct megasas_instance *instance)
2351{
2352 unsigned int i
2353;
2354 for (i = 0; i < instance->drv_buf_alloc; i++) {
2355 if (instance->crash_buf[i])
2356 free_pages((ulong)instance->crash_buf[i],
2357 instance->crash_buf_pages);
2358 }
2359 instance->drv_buf_index = 0;
2360 instance->drv_buf_alloc = 0;
2361 instance->fw_crash_state = UNAVAILABLE;
2362 instance->fw_crash_buffer_size = 0;
2363}
2364
2365/**
adam radford9c915a82010-12-21 13:34:31 -08002366 * megasas_adp_reset_fusion - For controller reset
2367 * @regs: MFI register set
2368 */
2369static int
2370megasas_adp_reset_fusion(struct megasas_instance *instance,
2371 struct megasas_register_set __iomem *regs)
2372{
2373 return 0;
2374}
2375
2376/**
2377 * megasas_check_reset_fusion - For controller reset check
2378 * @regs: MFI register set
2379 */
2380static int
2381megasas_check_reset_fusion(struct megasas_instance *instance,
2382 struct megasas_register_set __iomem *regs)
2383{
2384 return 0;
2385}
2386
2387/* This function waits for outstanding commands on fusion to complete */
adam radford229fe472014-03-10 02:51:56 -07002388int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
2389 int iotimeout, int *convert)
adam radford9c915a82010-12-21 13:34:31 -08002390{
adam radford229fe472014-03-10 02:51:56 -07002391 int i, outstanding, retval = 0, hb_seconds_missed = 0;
adam radfordc007b8b2012-07-17 18:20:24 -07002392 u32 fw_state;
adam radford9c915a82010-12-21 13:34:31 -08002393
adam radfordc007b8b2012-07-17 18:20:24 -07002394 for (i = 0; i < resetwaittime; i++) {
adam radford9c915a82010-12-21 13:34:31 -08002395 /* Check if firmware is in fault state */
2396 fw_state = instance->instancet->read_fw_status_reg(
2397 instance->reg_set) & MFI_STATE_MASK;
2398 if (fw_state == MFI_STATE_FAULT) {
2399 printk(KERN_WARNING "megasas: Found FW in FAULT state,"
adam radford229fe472014-03-10 02:51:56 -07002400 " will reset adapter scsi%d.\n",
2401 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002402 retval = 1;
2403 goto out;
2404 }
adam radford229fe472014-03-10 02:51:56 -07002405 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
2406 if (instance->requestorId && !iotimeout) {
2407 retval = 1;
2408 goto out;
2409 }
2410
2411 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
2412 if (instance->requestorId && iotimeout) {
2413 if (instance->hb_host_mem->HB.fwCounter !=
2414 instance->hb_host_mem->HB.driverCounter) {
2415 instance->hb_host_mem->HB.driverCounter =
2416 instance->hb_host_mem->HB.fwCounter;
2417 hb_seconds_missed = 0;
2418 } else {
2419 hb_seconds_missed++;
2420 if (hb_seconds_missed ==
2421 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
2422 printk(KERN_WARNING "megasas: SR-IOV:"
2423 " Heartbeat never completed "
2424 " while polling during I/O "
2425 " timeout handling for "
2426 "scsi%d.\n",
2427 instance->host->host_no);
2428 *convert = 1;
2429 retval = 1;
2430 goto out;
2431 }
2432 }
2433 }
adam radford9c915a82010-12-21 13:34:31 -08002434
2435 outstanding = atomic_read(&instance->fw_outstanding);
2436 if (!outstanding)
2437 goto out;
2438
2439 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2440 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
adam radford229fe472014-03-10 02:51:56 -07002441 "commands to complete for scsi%d\n", i,
2442 outstanding, instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002443 megasas_complete_cmd_dpc_fusion(
2444 (unsigned long)instance);
2445 }
2446 msleep(1000);
2447 }
2448
2449 if (atomic_read(&instance->fw_outstanding)) {
2450 printk("megaraid_sas: pending commands remain after waiting, "
adam radford229fe472014-03-10 02:51:56 -07002451 "will reset adapter scsi%d.\n",
2452 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002453 retval = 1;
2454 }
2455out:
2456 return retval;
2457}
2458
2459void megasas_reset_reply_desc(struct megasas_instance *instance)
2460{
adam radfordc8e858f2011-10-08 18:15:13 -07002461 int i, count;
adam radford9c915a82010-12-21 13:34:31 -08002462 struct fusion_context *fusion;
2463 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
2464
2465 fusion = instance->ctrl_context;
adam radfordc8e858f2011-10-08 18:15:13 -07002466 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
2467 for (i = 0 ; i < count ; i++)
2468 fusion->last_reply_idx[i] = 0;
adam radford9c915a82010-12-21 13:34:31 -08002469 reply_desc = fusion->reply_frames_desc;
adam radfordc8e858f2011-10-08 18:15:13 -07002470 for (i = 0 ; i < fusion->reply_q_depth * count; i++, reply_desc++)
adam radford9c915a82010-12-21 13:34:31 -08002471 reply_desc->Words = ULLONG_MAX;
2472}
2473
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05302474/*
2475 * megasas_refire_mgmt_cmd : Re-fire management commands
2476 * @instance: Controller's soft instance
2477*/
2478void megasas_refire_mgmt_cmd(struct megasas_instance *instance)
2479{
2480 int j;
2481 struct megasas_cmd_fusion *cmd_fusion;
2482 struct fusion_context *fusion;
2483 struct megasas_cmd *cmd_mfi;
2484 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2485 u16 smid;
2486
2487 fusion = instance->ctrl_context;
2488
2489 /* Re-fire management commands.
2490 * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
2491 */
2492 for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
2493 cmd_fusion = fusion->cmd_list[j];
2494 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2495 smid = le16_to_cpu(cmd_mfi->context.smid);
2496
2497 if (!smid)
2498 continue;
2499 req_desc = megasas_get_request_descriptor
2500 (instance, smid - 1);
2501 if (req_desc && (cmd_mfi->frame->dcmd.opcode !=
2502 cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO))) {
2503 instance->instancet->fire_cmd(instance,
2504 req_desc->u.low, req_desc->u.high,
2505 instance->reg_set);
2506 } else
2507 megasas_return_cmd(instance, cmd_mfi);
2508 }
2509}
2510
adam radford229fe472014-03-10 02:51:56 -07002511/* Check for a second path that is currently UP */
2512int megasas_check_mpio_paths(struct megasas_instance *instance,
2513 struct scsi_cmnd *scmd)
adam radford9c915a82010-12-21 13:34:31 -08002514{
adam radford229fe472014-03-10 02:51:56 -07002515 int i, j, retval = (DID_RESET << 16);
2516
2517 if (instance->mpio && instance->requestorId) {
2518 for (i = 0 ; i < MAX_MGMT_ADAPTERS ; i++)
2519 for (j = 0 ; j < MAX_LOGICAL_DRIVES; j++)
2520 if (megasas_mgmt_info.instance[i] &&
2521 (megasas_mgmt_info.instance[i] != instance) &&
2522 megasas_mgmt_info.instance[i]->mpio &&
2523 megasas_mgmt_info.instance[i]->requestorId
2524 &&
2525 (megasas_mgmt_info.instance[i]->ld_ids[j]
2526 == scmd->device->id)) {
2527 retval = (DID_NO_CONNECT << 16);
2528 goto out;
2529 }
2530 }
2531out:
2532 return retval;
2533}
2534
2535/* Core fusion reset function */
2536int megasas_reset_fusion(struct Scsi_Host *shost, int iotimeout)
2537{
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05302538 int retval = SUCCESS, i, retry = 0, convert = 0;
adam radford9c915a82010-12-21 13:34:31 -08002539 struct megasas_instance *instance;
2540 struct megasas_cmd_fusion *cmd_fusion;
2541 struct fusion_context *fusion;
adam radford7e70e732011-05-11 18:34:08 -07002542 u32 host_diag, abs_state, status_reg, reset_adapter;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302543 u32 io_timeout_in_crash_mode = 0;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302544 struct scsi_cmnd *scmd_local = NULL;
adam radford9c915a82010-12-21 13:34:31 -08002545
2546 instance = (struct megasas_instance *)shost->hostdata;
2547 fusion = instance->ctrl_context;
2548
adam radford229fe472014-03-10 02:51:56 -07002549 mutex_lock(&instance->reset_mutex);
2550
adam radford9c915a82010-12-21 13:34:31 -08002551 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2552 printk(KERN_WARNING "megaraid_sas: Hardware critical error, "
adam radford229fe472014-03-10 02:51:56 -07002553 "returning FAILED for scsi%d.\n",
2554 instance->host->host_no);
Adam Radforda2fbcbc2014-07-09 15:17:54 -07002555 mutex_unlock(&instance->reset_mutex);
adam radfordd4a759a2011-10-08 18:14:39 -07002556 return FAILED;
adam radford9c915a82010-12-21 13:34:31 -08002557 }
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302558 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
2559 abs_state = status_reg & MFI_STATE_MASK;
2560
2561 /* IO timeout detected, forcibly put FW in FAULT state */
2562 if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
2563 instance->crash_dump_app_support && iotimeout) {
2564 dev_info(&instance->pdev->dev, "IO timeout is detected, "
2565 "forcibly FAULT Firmware\n");
2566 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2567 status_reg = readl(&instance->reg_set->doorbell);
2568 writel(status_reg | MFI_STATE_FORCE_OCR,
2569 &instance->reg_set->doorbell);
2570 readl(&instance->reg_set->doorbell);
2571 mutex_unlock(&instance->reset_mutex);
2572 do {
2573 ssleep(3);
2574 io_timeout_in_crash_mode++;
2575 dev_dbg(&instance->pdev->dev, "waiting for [%d] "
2576 "seconds for crash dump collection and OCR "
2577 "to be done\n", (io_timeout_in_crash_mode * 3));
2578 } while ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
2579 (io_timeout_in_crash_mode < 80));
2580
2581 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
2582 dev_info(&instance->pdev->dev, "OCR done for IO "
2583 "timeout case\n");
2584 retval = SUCCESS;
2585 } else {
2586 dev_info(&instance->pdev->dev, "Controller is not "
2587 "operational after 240 seconds wait for IO "
2588 "timeout case in FW crash dump mode\n do "
2589 "OCR/kill adapter\n");
2590 retval = megasas_reset_fusion(shost, 0);
2591 }
2592 return retval;
2593 }
adam radford9c915a82010-12-21 13:34:31 -08002594
adam radford229fe472014-03-10 02:51:56 -07002595 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
2596 del_timer_sync(&instance->sriov_heartbeat_timer);
adam radford7e70e732011-05-11 18:34:08 -07002597 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
adam radford229fe472014-03-10 02:51:56 -07002598 instance->adprecovery = MEGASAS_ADPRESET_SM_POLLING;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302599 instance->instancet->disable_intr(instance);
adam radford7e70e732011-05-11 18:34:08 -07002600 msleep(1000);
2601
adam radford9c915a82010-12-21 13:34:31 -08002602 /* First try waiting for commands to complete */
adam radford229fe472014-03-10 02:51:56 -07002603 if (megasas_wait_for_outstanding_fusion(instance, iotimeout,
2604 &convert)) {
2605 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
adam radford9c915a82010-12-21 13:34:31 -08002606 printk(KERN_WARNING "megaraid_sas: resetting fusion "
adam radford229fe472014-03-10 02:51:56 -07002607 "adapter scsi%d.\n", instance->host->host_no);
2608 if (convert)
2609 iotimeout = 0;
2610
adam radford9c915a82010-12-21 13:34:31 -08002611 /* Now return commands back to the OS */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302612 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
adam radford9c915a82010-12-21 13:34:31 -08002613 cmd_fusion = fusion->cmd_list[i];
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302614 scmd_local = cmd_fusion->scmd;
adam radford9c915a82010-12-21 13:34:31 -08002615 if (cmd_fusion->scmd) {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302616 scmd_local->result =
adam radford229fe472014-03-10 02:51:56 -07002617 megasas_check_mpio_paths(instance,
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302618 scmd_local);
adam radford9c915a82010-12-21 13:34:31 -08002619 megasas_return_cmd_fusion(instance, cmd_fusion);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302620 scsi_dma_unmap(scmd_local);
2621 scmd_local->scsi_done(scmd_local);
adam radford9c915a82010-12-21 13:34:31 -08002622 atomic_dec(&instance->fw_outstanding);
2623 }
2624 }
2625
adam radford7e70e732011-05-11 18:34:08 -07002626 status_reg = instance->instancet->read_fw_status_reg(
2627 instance->reg_set);
2628 abs_state = status_reg & MFI_STATE_MASK;
2629 reset_adapter = status_reg & MFI_RESET_ADAPTER;
2630 if (instance->disableOnlineCtrlReset ||
2631 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
adam radford9c915a82010-12-21 13:34:31 -08002632 /* Reset not supported, kill adapter */
2633 printk(KERN_WARNING "megaraid_sas: Reset not supported"
adam radford229fe472014-03-10 02:51:56 -07002634 ", killing adapter scsi%d.\n",
2635 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002636 megaraid_sas_kill_hba(instance);
adam radford229fe472014-03-10 02:51:56 -07002637 instance->skip_heartbeat_timer_del = 1;
adam radford9c915a82010-12-21 13:34:31 -08002638 retval = FAILED;
2639 goto out;
2640 }
2641
adam radford229fe472014-03-10 02:51:56 -07002642 /* Let SR-IOV VF & PF sync up if there was a HB failure */
2643 if (instance->requestorId && !iotimeout) {
2644 msleep(MEGASAS_OCR_SETTLE_TIME_VF);
2645 /* Look for a late HB update after VF settle time */
2646 if (abs_state == MFI_STATE_OPERATIONAL &&
2647 (instance->hb_host_mem->HB.fwCounter !=
2648 instance->hb_host_mem->HB.driverCounter)) {
2649 instance->hb_host_mem->HB.driverCounter =
2650 instance->hb_host_mem->HB.fwCounter;
2651 printk(KERN_WARNING "megasas: SR-IOV:"
2652 "Late FW heartbeat update for "
2653 "scsi%d.\n",
2654 instance->host->host_no);
2655 } else {
2656 /* In VF mode, first poll for FW ready */
2657 for (i = 0;
2658 i < (MEGASAS_RESET_WAIT_TIME * 1000);
2659 i += 20) {
2660 status_reg =
2661 instance->instancet->
2662 read_fw_status_reg(
2663 instance->reg_set);
2664 abs_state = status_reg &
2665 MFI_STATE_MASK;
2666 if (abs_state == MFI_STATE_READY) {
2667 printk(KERN_WARNING "megasas"
2668 ": SR-IOV: FW was found"
2669 "to be in ready state "
2670 "for scsi%d.\n",
2671 instance->host->host_no);
2672 break;
2673 }
2674 msleep(20);
2675 }
2676 if (abs_state != MFI_STATE_READY) {
2677 printk(KERN_WARNING "megasas: SR-IOV: "
2678 "FW not in ready state after %d"
2679 " seconds for scsi%d, status_reg = "
2680 "0x%x.\n",
2681 MEGASAS_RESET_WAIT_TIME,
2682 instance->host->host_no,
2683 status_reg);
2684 megaraid_sas_kill_hba(instance);
2685 instance->skip_heartbeat_timer_del = 1;
2686 instance->adprecovery =
2687 MEGASAS_HW_CRITICAL_ERROR;
2688 retval = FAILED;
2689 goto out;
2690 }
2691 }
2692 }
2693
adam radford9c915a82010-12-21 13:34:31 -08002694 /* Now try to reset the chip */
2695 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
2696 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE,
2697 &instance->reg_set->fusion_seq_offset);
2698 writel(MPI2_WRSEQ_1ST_KEY_VALUE,
2699 &instance->reg_set->fusion_seq_offset);
2700 writel(MPI2_WRSEQ_2ND_KEY_VALUE,
2701 &instance->reg_set->fusion_seq_offset);
2702 writel(MPI2_WRSEQ_3RD_KEY_VALUE,
2703 &instance->reg_set->fusion_seq_offset);
2704 writel(MPI2_WRSEQ_4TH_KEY_VALUE,
2705 &instance->reg_set->fusion_seq_offset);
2706 writel(MPI2_WRSEQ_5TH_KEY_VALUE,
2707 &instance->reg_set->fusion_seq_offset);
2708 writel(MPI2_WRSEQ_6TH_KEY_VALUE,
2709 &instance->reg_set->fusion_seq_offset);
2710
2711 /* Check that the diag write enable (DRWE) bit is on */
2712 host_diag = readl(&instance->reg_set->fusion_host_diag);
adam radford7e70e732011-05-11 18:34:08 -07002713 retry = 0;
adam radford9c915a82010-12-21 13:34:31 -08002714 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
2715 msleep(100);
2716 host_diag =
2717 readl(&instance->reg_set->fusion_host_diag);
2718 if (retry++ == 100) {
2719 printk(KERN_WARNING "megaraid_sas: "
adam radford229fe472014-03-10 02:51:56 -07002720 "Host diag unlock failed! "
2721 "for scsi%d\n",
2722 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002723 break;
2724 }
2725 }
2726 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
2727 continue;
2728
2729 /* Send chip reset command */
2730 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
2731 &instance->reg_set->fusion_host_diag);
2732 msleep(3000);
2733
2734 /* Make sure reset adapter bit is cleared */
2735 host_diag = readl(&instance->reg_set->fusion_host_diag);
2736 retry = 0;
2737 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
2738 msleep(100);
2739 host_diag =
2740 readl(&instance->reg_set->fusion_host_diag);
2741 if (retry++ == 1000) {
2742 printk(KERN_WARNING "megaraid_sas: "
2743 "Diag reset adapter never "
adam radford229fe472014-03-10 02:51:56 -07002744 "cleared for scsi%d!\n",
2745 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002746 break;
2747 }
2748 }
2749 if (host_diag & HOST_DIAG_RESET_ADAPTER)
2750 continue;
2751
2752 abs_state =
2753 instance->instancet->read_fw_status_reg(
adam radford7e70e732011-05-11 18:34:08 -07002754 instance->reg_set) & MFI_STATE_MASK;
adam radford9c915a82010-12-21 13:34:31 -08002755 retry = 0;
2756
2757 while ((abs_state <= MFI_STATE_FW_INIT) &&
2758 (retry++ < 1000)) {
2759 msleep(100);
2760 abs_state =
2761 instance->instancet->read_fw_status_reg(
adam radford7e70e732011-05-11 18:34:08 -07002762 instance->reg_set) & MFI_STATE_MASK;
adam radford9c915a82010-12-21 13:34:31 -08002763 }
2764 if (abs_state <= MFI_STATE_FW_INIT) {
2765 printk(KERN_WARNING "megaraid_sas: firmware "
2766 "state < MFI_STATE_FW_INIT, state = "
adam radford229fe472014-03-10 02:51:56 -07002767 "0x%x for scsi%d\n", abs_state,
2768 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002769 continue;
2770 }
2771
2772 /* Wait for FW to become ready */
adam radford058a8fa2011-10-08 18:14:27 -07002773 if (megasas_transition_to_ready(instance, 1)) {
adam radford9c915a82010-12-21 13:34:31 -08002774 printk(KERN_WARNING "megaraid_sas: Failed to "
adam radford229fe472014-03-10 02:51:56 -07002775 "transition controller to ready "
2776 "for scsi%d.\n",
2777 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002778 continue;
2779 }
2780
2781 megasas_reset_reply_desc(instance);
2782 if (megasas_ioc_init_fusion(instance)) {
2783 printk(KERN_WARNING "megaraid_sas: "
adam radford229fe472014-03-10 02:51:56 -07002784 "megasas_ioc_init_fusion() failed!"
2785 " for scsi%d\n",
2786 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002787 continue;
2788 }
2789
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05302790 megasas_refire_mgmt_cmd(instance);
adam radford9c915a82010-12-21 13:34:31 -08002791
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05302792 if (megasas_get_ctrl_info(instance)) {
2793 dev_info(&instance->pdev->dev,
2794 "Failed from %s %d\n",
2795 __func__, __LINE__);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05302796 megaraid_sas_kill_hba(instance);
2797 retval = FAILED;
2798 }
adam radford9c915a82010-12-21 13:34:31 -08002799 /* Reset load balance info */
2800 memset(fusion->load_balance_info, 0,
2801 sizeof(struct LD_LOAD_BALANCE_INFO)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05302802 *MAX_LOGICAL_DRIVES_EXT);
adam radford9c915a82010-12-21 13:34:31 -08002803
2804 if (!megasas_get_map_info(instance))
2805 megasas_sync_map_info(instance);
2806
Sumit.Saxena@avagotech.comac340c52014-11-17 15:24:18 +05302807 clear_bit(MEGASAS_FUSION_IN_RESET,
2808 &instance->reset_flags);
2809 instance->instancet->enable_intr(instance);
2810 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2811
adam radford229fe472014-03-10 02:51:56 -07002812 /* Restart SR-IOV heartbeat */
2813 if (instance->requestorId) {
2814 if (!megasas_sriov_start_heartbeat(instance, 0))
2815 megasas_start_timer(instance,
2816 &instance->sriov_heartbeat_timer,
2817 megasas_sriov_heartbeat_handler,
2818 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2819 else
2820 instance->skip_heartbeat_timer_del = 1;
2821 }
2822
adam radford9c915a82010-12-21 13:34:31 -08002823 /* Adapter reset completed successfully */
2824 printk(KERN_WARNING "megaraid_sas: Reset "
adam radford229fe472014-03-10 02:51:56 -07002825 "successful for scsi%d.\n",
2826 instance->host->host_no);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302827
Sumit.Saxena@avagotech.comac340c52014-11-17 15:24:18 +05302828 if (instance->crash_dump_drv_support &&
2829 instance->crash_dump_app_support)
2830 megasas_set_crash_dump_params(instance,
2831 MR_CRASH_BUF_TURN_ON);
2832 else
2833 megasas_set_crash_dump_params(instance,
2834 MR_CRASH_BUF_TURN_OFF);
2835
adam radford9c915a82010-12-21 13:34:31 -08002836 retval = SUCCESS;
2837 goto out;
2838 }
2839 /* Reset failed, kill the adapter */
2840 printk(KERN_WARNING "megaraid_sas: Reset failed, killing "
adam radford229fe472014-03-10 02:51:56 -07002841 "adapter scsi%d.\n", instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002842 megaraid_sas_kill_hba(instance);
adam radford229fe472014-03-10 02:51:56 -07002843 instance->skip_heartbeat_timer_del = 1;
adam radford9c915a82010-12-21 13:34:31 -08002844 retval = FAILED;
2845 } else {
adam radford229fe472014-03-10 02:51:56 -07002846 /* For VF: Restart HB timer if we didn't OCR */
2847 if (instance->requestorId) {
2848 megasas_start_timer(instance,
2849 &instance->sriov_heartbeat_timer,
2850 megasas_sriov_heartbeat_handler,
2851 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2852 }
adam radford3f0e58bc2011-10-08 18:14:59 -07002853 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302854 instance->instancet->enable_intr(instance);
adam radford9c915a82010-12-21 13:34:31 -08002855 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2856 }
2857out:
2858 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2859 mutex_unlock(&instance->reset_mutex);
2860 return retval;
2861}
2862
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302863/* Fusion Crash dump collection work queue */
2864void megasas_fusion_crash_dump_wq(struct work_struct *work)
2865{
2866 struct megasas_instance *instance =
2867 container_of(work, struct megasas_instance, crash_init);
2868 u32 status_reg;
2869 u8 partial_copy = 0;
2870
2871
2872 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
2873
2874 /*
2875 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
2876 * to host crash buffers
2877 */
2878 if (instance->drv_buf_index == 0) {
2879 /* Buffer is already allocated for old Crash dump.
2880 * Do OCR and do not wait for crash dump collection
2881 */
2882 if (instance->drv_buf_alloc) {
2883 dev_info(&instance->pdev->dev, "earlier crash dump is "
2884 "not yet copied by application, ignoring this "
2885 "crash dump and initiating OCR\n");
2886 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
2887 writel(status_reg,
2888 &instance->reg_set->outbound_scratch_pad);
2889 readl(&instance->reg_set->outbound_scratch_pad);
2890 return;
2891 }
2892 megasas_alloc_host_crash_buffer(instance);
2893 dev_info(&instance->pdev->dev, "Number of host crash buffers "
2894 "allocated: %d\n", instance->drv_buf_alloc);
2895 }
2896
2897 /*
2898 * Driver has allocated max buffers, which can be allocated
2899 * and FW has more crash dump data, then driver will
2900 * ignore the data.
2901 */
2902 if (instance->drv_buf_index >= (instance->drv_buf_alloc)) {
2903 dev_info(&instance->pdev->dev, "Driver is done copying "
2904 "the buffer: %d\n", instance->drv_buf_alloc);
2905 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
2906 partial_copy = 1;
2907 } else {
2908 memcpy(instance->crash_buf[instance->drv_buf_index],
2909 instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
2910 instance->drv_buf_index++;
2911 status_reg &= ~MFI_STATE_DMADONE;
2912 }
2913
2914 if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
2915 dev_info(&instance->pdev->dev, "Crash Dump is available,number "
2916 "of copied buffers: %d\n", instance->drv_buf_index);
2917 instance->fw_crash_buffer_size = instance->drv_buf_index;
2918 instance->fw_crash_state = AVAILABLE;
2919 instance->drv_buf_index = 0;
2920 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
2921 readl(&instance->reg_set->outbound_scratch_pad);
2922 if (!partial_copy)
2923 megasas_reset_fusion(instance->host, 0);
2924 } else {
2925 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
2926 readl(&instance->reg_set->outbound_scratch_pad);
2927 }
2928}
2929
2930
adam radford9c915a82010-12-21 13:34:31 -08002931/* Fusion OCR work queue */
2932void megasas_fusion_ocr_wq(struct work_struct *work)
2933{
2934 struct megasas_instance *instance =
2935 container_of(work, struct megasas_instance, work_init);
2936
adam radford229fe472014-03-10 02:51:56 -07002937 megasas_reset_fusion(instance->host, 0);
adam radford9c915a82010-12-21 13:34:31 -08002938}
2939
2940struct megasas_instance_template megasas_instance_template_fusion = {
2941 .fire_cmd = megasas_fire_cmd_fusion,
2942 .enable_intr = megasas_enable_intr_fusion,
2943 .disable_intr = megasas_disable_intr_fusion,
2944 .clear_intr = megasas_clear_intr_fusion,
2945 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
2946 .adp_reset = megasas_adp_reset_fusion,
2947 .check_reset = megasas_check_reset_fusion,
2948 .service_isr = megasas_isr_fusion,
2949 .tasklet = megasas_complete_cmd_dpc_fusion,
2950 .init_adapter = megasas_init_adapter_fusion,
2951 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
2952 .issue_dcmd = megasas_issue_dcmd_fusion,
2953};