adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Linux MegaRAID driver for SAS based RAID controllers |
| 3 | * |
| 4 | * Copyright (c) 2009-2011 LSI Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version 2 |
| 9 | * of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | * |
| 20 | * FILE: megaraid_sas_fusion.c |
| 21 | * |
| 22 | * Authors: LSI Corporation |
| 23 | * Sumant Patro |
| 24 | * Adam Radford <linuxraid@lsi.com> |
| 25 | * |
| 26 | * Send feedback to: <megaraidlinux@lsi.com> |
| 27 | * |
| 28 | * Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035 |
| 29 | * ATTN: Linuxraid |
| 30 | */ |
| 31 | |
| 32 | #include <linux/kernel.h> |
| 33 | #include <linux/types.h> |
| 34 | #include <linux/pci.h> |
| 35 | #include <linux/list.h> |
| 36 | #include <linux/moduleparam.h> |
| 37 | #include <linux/module.h> |
| 38 | #include <linux/spinlock.h> |
| 39 | #include <linux/interrupt.h> |
| 40 | #include <linux/delay.h> |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 41 | #include <linux/uio.h> |
| 42 | #include <linux/uaccess.h> |
| 43 | #include <linux/fs.h> |
| 44 | #include <linux/compat.h> |
| 45 | #include <linux/blkdev.h> |
| 46 | #include <linux/mutex.h> |
| 47 | #include <linux/poll.h> |
| 48 | |
| 49 | #include <scsi/scsi.h> |
| 50 | #include <scsi/scsi_cmnd.h> |
| 51 | #include <scsi/scsi_device.h> |
| 52 | #include <scsi/scsi_host.h> |
| 53 | |
| 54 | #include "megaraid_sas_fusion.h" |
| 55 | #include "megaraid_sas.h" |
| 56 | |
| 57 | extern void megasas_free_cmds(struct megasas_instance *instance); |
| 58 | extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance |
| 59 | *instance); |
| 60 | extern void |
| 61 | megasas_complete_cmd(struct megasas_instance *instance, |
| 62 | struct megasas_cmd *cmd, u8 alt_status); |
| 63 | int megasas_is_ldio(struct scsi_cmnd *cmd); |
| 64 | int |
| 65 | wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd); |
| 66 | |
| 67 | void |
| 68 | megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd); |
| 69 | int megasas_alloc_cmds(struct megasas_instance *instance); |
| 70 | int |
| 71 | megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs); |
| 72 | int |
| 73 | megasas_issue_polled(struct megasas_instance *instance, |
| 74 | struct megasas_cmd *cmd); |
| 75 | |
| 76 | u8 |
adam radford | 36807e6 | 2011-10-08 18:15:06 -0700 | [diff] [blame] | 77 | MR_BuildRaidContext(struct megasas_instance *instance, |
| 78 | struct IO_REQUEST_INFO *io_info, |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 79 | struct RAID_CONTEXT *pRAID_Context, |
| 80 | struct MR_FW_RAID_MAP_ALL *map); |
| 81 | u16 MR_TargetIdToLdGet(u32 ldTgtId, struct MR_FW_RAID_MAP_ALL *map); |
| 82 | struct MR_LD_RAID *MR_LdRaidGet(u32 ld, struct MR_FW_RAID_MAP_ALL *map); |
| 83 | |
| 84 | u16 MR_GetLDTgtId(u32 ld, struct MR_FW_RAID_MAP_ALL *map); |
adam radford | 53ef2bb | 2011-02-24 20:56:05 -0800 | [diff] [blame] | 85 | |
| 86 | void |
| 87 | megasas_check_and_restore_queue_depth(struct megasas_instance *instance); |
| 88 | |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 89 | u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map, |
| 90 | struct LD_LOAD_BALANCE_INFO *lbInfo); |
| 91 | u16 get_updated_dev_handle(struct LD_LOAD_BALANCE_INFO *lbInfo, |
| 92 | struct IO_REQUEST_INFO *in_info); |
adam radford | 058a8fa | 2011-10-08 18:14:27 -0700 | [diff] [blame] | 93 | int megasas_transition_to_ready(struct megasas_instance *instance, int ocr); |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 94 | void megaraid_sas_kill_hba(struct megasas_instance *instance); |
| 95 | |
| 96 | extern u32 megasas_dbg_lvl; |
adam radford | c007b8b | 2012-07-17 18:20:24 -0700 | [diff] [blame] | 97 | extern int resetwaittime; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * megasas_enable_intr_fusion - Enables interrupts |
| 101 | * @regs: MFI register set |
| 102 | */ |
| 103 | void |
| 104 | megasas_enable_intr_fusion(struct megasas_register_set __iomem *regs) |
| 105 | { |
adam radford | 6497b24 | 2011-10-08 18:14:50 -0700 | [diff] [blame] | 106 | /* For Thunderbolt/Invader also clear intr on enable */ |
| 107 | writel(~0, ®s->outbound_intr_status); |
| 108 | readl(®s->outbound_intr_status); |
| 109 | |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 110 | writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask); |
| 111 | |
| 112 | /* Dummy readl to force pci flush */ |
| 113 | readl(®s->outbound_intr_mask); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * megasas_disable_intr_fusion - Disables interrupt |
| 118 | * @regs: MFI register set |
| 119 | */ |
| 120 | void |
| 121 | megasas_disable_intr_fusion(struct megasas_register_set __iomem *regs) |
| 122 | { |
| 123 | u32 mask = 0xFFFFFFFF; |
| 124 | u32 status; |
| 125 | |
| 126 | writel(mask, ®s->outbound_intr_mask); |
| 127 | /* Dummy readl to force pci flush */ |
| 128 | status = readl(®s->outbound_intr_mask); |
| 129 | } |
| 130 | |
| 131 | int |
| 132 | megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs) |
| 133 | { |
| 134 | u32 status; |
| 135 | /* |
| 136 | * Check if it is our interrupt |
| 137 | */ |
| 138 | status = readl(®s->outbound_intr_status); |
| 139 | |
| 140 | if (status & 1) { |
| 141 | writel(status, ®s->outbound_intr_status); |
| 142 | readl(®s->outbound_intr_status); |
| 143 | return 1; |
| 144 | } |
| 145 | if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK)) |
| 146 | return 0; |
| 147 | |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 148 | return 1; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * megasas_get_cmd_fusion - Get a command from the free pool |
| 153 | * @instance: Adapter soft state |
| 154 | * |
| 155 | * Returns a free command from the pool |
| 156 | */ |
| 157 | struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance |
| 158 | *instance) |
| 159 | { |
| 160 | unsigned long flags; |
| 161 | struct fusion_context *fusion = |
| 162 | (struct fusion_context *)instance->ctrl_context; |
| 163 | struct megasas_cmd_fusion *cmd = NULL; |
| 164 | |
| 165 | spin_lock_irqsave(&fusion->cmd_pool_lock, flags); |
| 166 | |
| 167 | if (!list_empty(&fusion->cmd_pool)) { |
| 168 | cmd = list_entry((&fusion->cmd_pool)->next, |
| 169 | struct megasas_cmd_fusion, list); |
| 170 | list_del_init(&cmd->list); |
| 171 | } else { |
| 172 | printk(KERN_ERR "megasas: Command pool (fusion) empty!\n"); |
| 173 | } |
| 174 | |
| 175 | spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags); |
| 176 | return cmd; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * megasas_return_cmd_fusion - Return a cmd to free command pool |
| 181 | * @instance: Adapter soft state |
| 182 | * @cmd: Command packet to be returned to free command pool |
| 183 | */ |
| 184 | static inline void |
| 185 | megasas_return_cmd_fusion(struct megasas_instance *instance, |
| 186 | struct megasas_cmd_fusion *cmd) |
| 187 | { |
| 188 | unsigned long flags; |
| 189 | struct fusion_context *fusion = |
| 190 | (struct fusion_context *)instance->ctrl_context; |
| 191 | |
| 192 | spin_lock_irqsave(&fusion->cmd_pool_lock, flags); |
| 193 | |
| 194 | cmd->scmd = NULL; |
| 195 | cmd->sync_cmd_idx = (u32)ULONG_MAX; |
| 196 | list_add_tail(&cmd->list, &fusion->cmd_pool); |
| 197 | |
| 198 | spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool |
| 203 | * @instance: Adapter soft state |
| 204 | */ |
| 205 | static void megasas_teardown_frame_pool_fusion( |
| 206 | struct megasas_instance *instance) |
| 207 | { |
| 208 | int i; |
| 209 | struct fusion_context *fusion = instance->ctrl_context; |
| 210 | |
| 211 | u16 max_cmd = instance->max_fw_cmds; |
| 212 | |
| 213 | struct megasas_cmd_fusion *cmd; |
| 214 | |
| 215 | if (!fusion->sg_dma_pool || !fusion->sense_dma_pool) { |
| 216 | printk(KERN_ERR "megasas: dma pool is null. SG Pool %p, " |
| 217 | "sense pool : %p\n", fusion->sg_dma_pool, |
| 218 | fusion->sense_dma_pool); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * Return all frames to pool |
| 224 | */ |
| 225 | for (i = 0; i < max_cmd; i++) { |
| 226 | |
| 227 | cmd = fusion->cmd_list[i]; |
| 228 | |
| 229 | if (cmd->sg_frame) |
| 230 | pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame, |
| 231 | cmd->sg_frame_phys_addr); |
| 232 | |
| 233 | if (cmd->sense) |
| 234 | pci_pool_free(fusion->sense_dma_pool, cmd->sense, |
| 235 | cmd->sense_phys_addr); |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Now destroy the pool itself |
| 240 | */ |
| 241 | pci_pool_destroy(fusion->sg_dma_pool); |
| 242 | pci_pool_destroy(fusion->sense_dma_pool); |
| 243 | |
| 244 | fusion->sg_dma_pool = NULL; |
| 245 | fusion->sense_dma_pool = NULL; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool |
| 250 | * @instance: Adapter soft state |
| 251 | */ |
| 252 | void |
| 253 | megasas_free_cmds_fusion(struct megasas_instance *instance) |
| 254 | { |
| 255 | int i; |
| 256 | struct fusion_context *fusion = instance->ctrl_context; |
| 257 | |
| 258 | u32 max_cmds, req_sz, reply_sz, io_frames_sz; |
| 259 | |
| 260 | |
| 261 | req_sz = fusion->request_alloc_sz; |
| 262 | reply_sz = fusion->reply_alloc_sz; |
| 263 | io_frames_sz = fusion->io_frames_alloc_sz; |
| 264 | |
| 265 | max_cmds = instance->max_fw_cmds; |
| 266 | |
| 267 | /* Free descriptors and request Frames memory */ |
| 268 | if (fusion->req_frames_desc) |
| 269 | dma_free_coherent(&instance->pdev->dev, req_sz, |
| 270 | fusion->req_frames_desc, |
| 271 | fusion->req_frames_desc_phys); |
| 272 | |
| 273 | if (fusion->reply_frames_desc) { |
| 274 | pci_pool_free(fusion->reply_frames_desc_pool, |
| 275 | fusion->reply_frames_desc, |
| 276 | fusion->reply_frames_desc_phys); |
| 277 | pci_pool_destroy(fusion->reply_frames_desc_pool); |
| 278 | } |
| 279 | |
| 280 | if (fusion->io_request_frames) { |
| 281 | pci_pool_free(fusion->io_request_frames_pool, |
| 282 | fusion->io_request_frames, |
| 283 | fusion->io_request_frames_phys); |
| 284 | pci_pool_destroy(fusion->io_request_frames_pool); |
| 285 | } |
| 286 | |
| 287 | /* Free the Fusion frame pool */ |
| 288 | megasas_teardown_frame_pool_fusion(instance); |
| 289 | |
| 290 | /* Free all the commands in the cmd_list */ |
| 291 | for (i = 0; i < max_cmds; i++) |
| 292 | kfree(fusion->cmd_list[i]); |
| 293 | |
| 294 | /* Free the cmd_list buffer itself */ |
| 295 | kfree(fusion->cmd_list); |
| 296 | fusion->cmd_list = NULL; |
| 297 | |
| 298 | INIT_LIST_HEAD(&fusion->cmd_pool); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * megasas_create_frame_pool_fusion - Creates DMA pool for cmd frames |
| 303 | * @instance: Adapter soft state |
| 304 | * |
| 305 | */ |
| 306 | static int megasas_create_frame_pool_fusion(struct megasas_instance *instance) |
| 307 | { |
| 308 | int i; |
| 309 | u32 max_cmd; |
| 310 | struct fusion_context *fusion; |
| 311 | struct megasas_cmd_fusion *cmd; |
| 312 | u32 total_sz_chain_frame; |
| 313 | |
| 314 | fusion = instance->ctrl_context; |
| 315 | max_cmd = instance->max_fw_cmds; |
| 316 | |
| 317 | total_sz_chain_frame = MEGASAS_MAX_SZ_CHAIN_FRAME; |
| 318 | |
| 319 | /* |
| 320 | * Use DMA pool facility provided by PCI layer |
| 321 | */ |
| 322 | |
| 323 | fusion->sg_dma_pool = pci_pool_create("megasas sg pool fusion", |
| 324 | instance->pdev, |
| 325 | total_sz_chain_frame, 4, |
| 326 | 0); |
| 327 | if (!fusion->sg_dma_pool) { |
| 328 | printk(KERN_DEBUG "megasas: failed to setup request pool " |
| 329 | "fusion\n"); |
| 330 | return -ENOMEM; |
| 331 | } |
| 332 | fusion->sense_dma_pool = pci_pool_create("megasas sense pool fusion", |
| 333 | instance->pdev, |
| 334 | SCSI_SENSE_BUFFERSIZE, 64, 0); |
| 335 | |
| 336 | if (!fusion->sense_dma_pool) { |
| 337 | printk(KERN_DEBUG "megasas: failed to setup sense pool " |
| 338 | "fusion\n"); |
| 339 | pci_pool_destroy(fusion->sg_dma_pool); |
| 340 | fusion->sg_dma_pool = NULL; |
| 341 | return -ENOMEM; |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * Allocate and attach a frame to each of the commands in cmd_list |
| 346 | */ |
| 347 | for (i = 0; i < max_cmd; i++) { |
| 348 | |
| 349 | cmd = fusion->cmd_list[i]; |
| 350 | |
| 351 | cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool, |
| 352 | GFP_KERNEL, |
| 353 | &cmd->sg_frame_phys_addr); |
| 354 | |
| 355 | cmd->sense = pci_pool_alloc(fusion->sense_dma_pool, |
| 356 | GFP_KERNEL, &cmd->sense_phys_addr); |
| 357 | /* |
| 358 | * megasas_teardown_frame_pool_fusion() takes care of freeing |
| 359 | * whatever has been allocated |
| 360 | */ |
| 361 | if (!cmd->sg_frame || !cmd->sense) { |
| 362 | printk(KERN_DEBUG "megasas: pci_pool_alloc failed\n"); |
| 363 | megasas_teardown_frame_pool_fusion(instance); |
| 364 | return -ENOMEM; |
| 365 | } |
| 366 | } |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * megasas_alloc_cmds_fusion - Allocates the command packets |
| 372 | * @instance: Adapter soft state |
| 373 | * |
| 374 | * |
| 375 | * Each frame has a 32-bit field called context. This context is used to get |
| 376 | * back the megasas_cmd_fusion from the frame when a frame gets completed |
| 377 | * In this driver, the 32 bit values are the indices into an array cmd_list. |
| 378 | * This array is used only to look up the megasas_cmd_fusion given the context. |
| 379 | * The free commands themselves are maintained in a linked list called cmd_pool. |
| 380 | * |
| 381 | * cmds are formed in the io_request and sg_frame members of the |
| 382 | * megasas_cmd_fusion. The context field is used to get a request descriptor |
| 383 | * and is used as SMID of the cmd. |
| 384 | * SMID value range is from 1 to max_fw_cmds. |
| 385 | */ |
| 386 | int |
| 387 | megasas_alloc_cmds_fusion(struct megasas_instance *instance) |
| 388 | { |
adam radford | c8e858f | 2011-10-08 18:15:13 -0700 | [diff] [blame] | 389 | int i, j, count; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 390 | u32 max_cmd, io_frames_sz; |
| 391 | struct fusion_context *fusion; |
| 392 | struct megasas_cmd_fusion *cmd; |
| 393 | union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc; |
| 394 | u32 offset; |
| 395 | dma_addr_t io_req_base_phys; |
| 396 | u8 *io_req_base; |
| 397 | |
| 398 | fusion = instance->ctrl_context; |
| 399 | |
| 400 | max_cmd = instance->max_fw_cmds; |
| 401 | |
| 402 | fusion->req_frames_desc = |
| 403 | dma_alloc_coherent(&instance->pdev->dev, |
| 404 | fusion->request_alloc_sz, |
| 405 | &fusion->req_frames_desc_phys, GFP_KERNEL); |
| 406 | |
| 407 | if (!fusion->req_frames_desc) { |
| 408 | printk(KERN_ERR "megasas; Could not allocate memory for " |
| 409 | "request_frames\n"); |
| 410 | goto fail_req_desc; |
| 411 | } |
| 412 | |
adam radford | c8e858f | 2011-10-08 18:15:13 -0700 | [diff] [blame] | 413 | count = instance->msix_vectors > 0 ? instance->msix_vectors : 1; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 414 | fusion->reply_frames_desc_pool = |
| 415 | pci_pool_create("reply_frames pool", instance->pdev, |
adam radford | c8e858f | 2011-10-08 18:15:13 -0700 | [diff] [blame] | 416 | fusion->reply_alloc_sz * count, 16, 0); |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 417 | |
| 418 | if (!fusion->reply_frames_desc_pool) { |
| 419 | printk(KERN_ERR "megasas; Could not allocate memory for " |
| 420 | "reply_frame pool\n"); |
| 421 | goto fail_reply_desc; |
| 422 | } |
| 423 | |
| 424 | fusion->reply_frames_desc = |
| 425 | pci_pool_alloc(fusion->reply_frames_desc_pool, GFP_KERNEL, |
| 426 | &fusion->reply_frames_desc_phys); |
| 427 | if (!fusion->reply_frames_desc) { |
| 428 | printk(KERN_ERR "megasas; Could not allocate memory for " |
| 429 | "reply_frame pool\n"); |
| 430 | pci_pool_destroy(fusion->reply_frames_desc_pool); |
| 431 | goto fail_reply_desc; |
| 432 | } |
| 433 | |
| 434 | reply_desc = fusion->reply_frames_desc; |
adam radford | c8e858f | 2011-10-08 18:15:13 -0700 | [diff] [blame] | 435 | for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++) |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 436 | reply_desc->Words = ULLONG_MAX; |
| 437 | |
| 438 | io_frames_sz = fusion->io_frames_alloc_sz; |
| 439 | |
| 440 | fusion->io_request_frames_pool = |
| 441 | pci_pool_create("io_request_frames pool", instance->pdev, |
| 442 | fusion->io_frames_alloc_sz, 16, 0); |
| 443 | |
| 444 | if (!fusion->io_request_frames_pool) { |
| 445 | printk(KERN_ERR "megasas: Could not allocate memory for " |
| 446 | "io_request_frame pool\n"); |
| 447 | goto fail_io_frames; |
| 448 | } |
| 449 | |
| 450 | fusion->io_request_frames = |
| 451 | pci_pool_alloc(fusion->io_request_frames_pool, GFP_KERNEL, |
| 452 | &fusion->io_request_frames_phys); |
| 453 | if (!fusion->io_request_frames) { |
| 454 | printk(KERN_ERR "megasas: Could not allocate memory for " |
| 455 | "io_request_frames frames\n"); |
| 456 | pci_pool_destroy(fusion->io_request_frames_pool); |
| 457 | goto fail_io_frames; |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers. |
| 462 | * Allocate the dynamic array first and then allocate individual |
| 463 | * commands. |
| 464 | */ |
Fengguang Wu | 7731e6b | 2012-08-24 23:27:35 +0800 | [diff] [blame] | 465 | fusion->cmd_list = kzalloc(sizeof(struct megasas_cmd_fusion *) |
| 466 | * max_cmd, GFP_KERNEL); |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 467 | |
| 468 | if (!fusion->cmd_list) { |
| 469 | printk(KERN_DEBUG "megasas: out of memory. Could not alloc " |
| 470 | "memory for cmd_list_fusion\n"); |
| 471 | goto fail_cmd_list; |
| 472 | } |
| 473 | |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 474 | max_cmd = instance->max_fw_cmds; |
| 475 | for (i = 0; i < max_cmd; i++) { |
| 476 | fusion->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd_fusion), |
| 477 | GFP_KERNEL); |
| 478 | if (!fusion->cmd_list[i]) { |
| 479 | printk(KERN_ERR "Could not alloc cmd list fusion\n"); |
| 480 | |
| 481 | for (j = 0; j < i; j++) |
| 482 | kfree(fusion->cmd_list[j]); |
| 483 | |
| 484 | kfree(fusion->cmd_list); |
| 485 | fusion->cmd_list = NULL; |
| 486 | goto fail_cmd_list; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | /* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */ |
| 491 | io_req_base = fusion->io_request_frames + |
| 492 | MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE; |
| 493 | io_req_base_phys = fusion->io_request_frames_phys + |
| 494 | MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE; |
| 495 | |
| 496 | /* |
| 497 | * Add all the commands to command pool (fusion->cmd_pool) |
| 498 | */ |
| 499 | |
| 500 | /* SMID 0 is reserved. Set SMID/index from 1 */ |
| 501 | for (i = 0; i < max_cmd; i++) { |
| 502 | cmd = fusion->cmd_list[i]; |
| 503 | offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i; |
| 504 | memset(cmd, 0, sizeof(struct megasas_cmd_fusion)); |
| 505 | cmd->index = i + 1; |
| 506 | cmd->scmd = NULL; |
| 507 | cmd->sync_cmd_idx = (u32)ULONG_MAX; /* Set to Invalid */ |
| 508 | cmd->instance = instance; |
| 509 | cmd->io_request = |
| 510 | (struct MPI2_RAID_SCSI_IO_REQUEST *) |
| 511 | (io_req_base + offset); |
| 512 | memset(cmd->io_request, 0, |
| 513 | sizeof(struct MPI2_RAID_SCSI_IO_REQUEST)); |
| 514 | cmd->io_request_phys_addr = io_req_base_phys + offset; |
| 515 | |
| 516 | list_add_tail(&cmd->list, &fusion->cmd_pool); |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | * Create a frame pool and assign one frame to each cmd |
| 521 | */ |
| 522 | if (megasas_create_frame_pool_fusion(instance)) { |
| 523 | printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n"); |
| 524 | megasas_free_cmds_fusion(instance); |
| 525 | goto fail_req_desc; |
| 526 | } |
| 527 | |
| 528 | return 0; |
| 529 | |
| 530 | fail_cmd_list: |
| 531 | pci_pool_free(fusion->io_request_frames_pool, fusion->io_request_frames, |
| 532 | fusion->io_request_frames_phys); |
| 533 | pci_pool_destroy(fusion->io_request_frames_pool); |
| 534 | fail_io_frames: |
| 535 | dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz, |
| 536 | fusion->reply_frames_desc, |
| 537 | fusion->reply_frames_desc_phys); |
| 538 | pci_pool_free(fusion->reply_frames_desc_pool, |
| 539 | fusion->reply_frames_desc, |
| 540 | fusion->reply_frames_desc_phys); |
| 541 | pci_pool_destroy(fusion->reply_frames_desc_pool); |
| 542 | |
| 543 | fail_reply_desc: |
| 544 | dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz, |
| 545 | fusion->req_frames_desc, |
| 546 | fusion->req_frames_desc_phys); |
| 547 | fail_req_desc: |
| 548 | return -ENOMEM; |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * wait_and_poll - Issues a polling command |
| 553 | * @instance: Adapter soft state |
| 554 | * @cmd: Command packet to be issued |
| 555 | * |
| 556 | * For polling, MFI requires the cmd_status to be set to 0xFF before posting. |
| 557 | */ |
| 558 | int |
| 559 | wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd) |
| 560 | { |
| 561 | int i; |
| 562 | struct megasas_header *frame_hdr = &cmd->frame->hdr; |
| 563 | |
| 564 | u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000; |
| 565 | |
| 566 | /* |
| 567 | * Wait for cmd_status to change |
| 568 | */ |
| 569 | for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) { |
| 570 | rmb(); |
| 571 | msleep(20); |
| 572 | } |
| 573 | |
| 574 | if (frame_hdr->cmd_status == 0xff) |
| 575 | return -ETIME; |
| 576 | |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * megasas_ioc_init_fusion - Initializes the FW |
| 582 | * @instance: Adapter soft state |
| 583 | * |
| 584 | * Issues the IOC Init cmd |
| 585 | */ |
| 586 | int |
| 587 | megasas_ioc_init_fusion(struct megasas_instance *instance) |
| 588 | { |
| 589 | struct megasas_init_frame *init_frame; |
| 590 | struct MPI2_IOC_INIT_REQUEST *IOCInitMessage; |
| 591 | dma_addr_t ioc_init_handle; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 592 | struct megasas_cmd *cmd; |
| 593 | u8 ret; |
| 594 | struct fusion_context *fusion; |
| 595 | union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc; |
| 596 | int i; |
| 597 | struct megasas_header *frame_hdr; |
| 598 | |
| 599 | fusion = instance->ctrl_context; |
| 600 | |
| 601 | cmd = megasas_get_cmd(instance); |
| 602 | |
| 603 | if (!cmd) { |
| 604 | printk(KERN_ERR "Could not allocate cmd for INIT Frame\n"); |
| 605 | ret = 1; |
| 606 | goto fail_get_cmd; |
| 607 | } |
| 608 | |
| 609 | IOCInitMessage = |
| 610 | dma_alloc_coherent(&instance->pdev->dev, |
| 611 | sizeof(struct MPI2_IOC_INIT_REQUEST), |
| 612 | &ioc_init_handle, GFP_KERNEL); |
| 613 | |
| 614 | if (!IOCInitMessage) { |
| 615 | printk(KERN_ERR "Could not allocate memory for " |
| 616 | "IOCInitMessage\n"); |
| 617 | ret = 1; |
| 618 | goto fail_fw_init; |
| 619 | } |
| 620 | |
| 621 | memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST)); |
| 622 | |
| 623 | IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT; |
| 624 | IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER; |
| 625 | IOCInitMessage->MsgVersion = MPI2_VERSION; |
| 626 | IOCInitMessage->HeaderVersion = MPI2_HEADER_VERSION; |
| 627 | IOCInitMessage->SystemRequestFrameSize = |
| 628 | MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4; |
| 629 | |
| 630 | IOCInitMessage->ReplyDescriptorPostQueueDepth = fusion->reply_q_depth; |
| 631 | IOCInitMessage->ReplyDescriptorPostQueueAddress = |
| 632 | fusion->reply_frames_desc_phys; |
| 633 | IOCInitMessage->SystemRequestFrameBaseAddress = |
| 634 | fusion->io_request_frames_phys; |
adam radford | 5738f99 | 2012-03-19 19:49:53 -0700 | [diff] [blame] | 635 | IOCInitMessage->HostMSIxVectors = instance->msix_vectors; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 636 | init_frame = (struct megasas_init_frame *)cmd->frame; |
| 637 | memset(init_frame, 0, MEGAMFI_FRAME_SIZE); |
| 638 | |
| 639 | frame_hdr = &cmd->frame->hdr; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 640 | frame_hdr->cmd_status = 0xFF; |
| 641 | frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE; |
| 642 | |
| 643 | init_frame->cmd = MFI_CMD_INIT; |
| 644 | init_frame->cmd_status = 0xFF; |
| 645 | |
| 646 | init_frame->queue_info_new_phys_addr_lo = ioc_init_handle; |
| 647 | init_frame->data_xfer_len = sizeof(struct MPI2_IOC_INIT_REQUEST); |
| 648 | |
| 649 | req_desc = |
| 650 | (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)fusion->req_frames_desc; |
| 651 | |
| 652 | req_desc->Words = cmd->frame_phys_addr; |
| 653 | req_desc->MFAIo.RequestFlags = |
| 654 | (MEGASAS_REQ_DESCRIPT_FLAGS_MFA << |
| 655 | MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); |
| 656 | |
| 657 | /* |
| 658 | * disable the intr before firing the init frame |
| 659 | */ |
| 660 | instance->instancet->disable_intr(instance->reg_set); |
| 661 | |
| 662 | for (i = 0; i < (10 * 1000); i += 20) { |
| 663 | if (readl(&instance->reg_set->doorbell) & 1) |
| 664 | msleep(20); |
| 665 | else |
| 666 | break; |
| 667 | } |
| 668 | |
| 669 | instance->instancet->fire_cmd(instance, req_desc->u.low, |
| 670 | req_desc->u.high, instance->reg_set); |
| 671 | |
| 672 | wait_and_poll(instance, cmd); |
| 673 | |
| 674 | frame_hdr = &cmd->frame->hdr; |
| 675 | if (frame_hdr->cmd_status != 0) { |
| 676 | ret = 1; |
| 677 | goto fail_fw_init; |
| 678 | } |
| 679 | printk(KERN_ERR "megasas:IOC Init cmd success\n"); |
| 680 | |
| 681 | ret = 0; |
| 682 | |
| 683 | fail_fw_init: |
| 684 | megasas_return_cmd(instance, cmd); |
| 685 | if (IOCInitMessage) |
| 686 | dma_free_coherent(&instance->pdev->dev, |
| 687 | sizeof(struct MPI2_IOC_INIT_REQUEST), |
| 688 | IOCInitMessage, ioc_init_handle); |
| 689 | fail_get_cmd: |
| 690 | return ret; |
| 691 | } |
| 692 | |
| 693 | /* |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 694 | * megasas_get_ld_map_info - Returns FW's ld_map structure |
| 695 | * @instance: Adapter soft state |
| 696 | * @pend: Pend the command or not |
| 697 | * Issues an internal command (DCMD) to get the FW's controller PD |
| 698 | * list structure. This information is mainly used to find out SYSTEM |
| 699 | * supported by the FW. |
| 700 | */ |
| 701 | static int |
| 702 | megasas_get_ld_map_info(struct megasas_instance *instance) |
| 703 | { |
| 704 | int ret = 0; |
| 705 | struct megasas_cmd *cmd; |
| 706 | struct megasas_dcmd_frame *dcmd; |
| 707 | struct MR_FW_RAID_MAP_ALL *ci; |
| 708 | dma_addr_t ci_h = 0; |
| 709 | u32 size_map_info; |
| 710 | struct fusion_context *fusion; |
| 711 | |
| 712 | cmd = megasas_get_cmd(instance); |
| 713 | |
| 714 | if (!cmd) { |
| 715 | printk(KERN_DEBUG "megasas: Failed to get cmd for map info.\n"); |
| 716 | return -ENOMEM; |
| 717 | } |
| 718 | |
| 719 | fusion = instance->ctrl_context; |
| 720 | |
| 721 | if (!fusion) { |
| 722 | megasas_return_cmd(instance, cmd); |
| 723 | return 1; |
| 724 | } |
| 725 | |
| 726 | dcmd = &cmd->frame->dcmd; |
| 727 | |
| 728 | size_map_info = sizeof(struct MR_FW_RAID_MAP) + |
| 729 | (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1)); |
| 730 | |
| 731 | ci = fusion->ld_map[(instance->map_id & 1)]; |
| 732 | ci_h = fusion->ld_map_phys[(instance->map_id & 1)]; |
| 733 | |
| 734 | if (!ci) { |
| 735 | printk(KERN_DEBUG "Failed to alloc mem for ld_map_info\n"); |
| 736 | megasas_return_cmd(instance, cmd); |
| 737 | return -ENOMEM; |
| 738 | } |
| 739 | |
| 740 | memset(ci, 0, sizeof(*ci)); |
| 741 | memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE); |
| 742 | |
| 743 | dcmd->cmd = MFI_CMD_DCMD; |
| 744 | dcmd->cmd_status = 0xFF; |
| 745 | dcmd->sge_count = 1; |
| 746 | dcmd->flags = MFI_FRAME_DIR_READ; |
| 747 | dcmd->timeout = 0; |
| 748 | dcmd->pad_0 = 0; |
| 749 | dcmd->data_xfer_len = size_map_info; |
| 750 | dcmd->opcode = MR_DCMD_LD_MAP_GET_INFO; |
| 751 | dcmd->sgl.sge32[0].phys_addr = ci_h; |
| 752 | dcmd->sgl.sge32[0].length = size_map_info; |
| 753 | |
| 754 | if (!megasas_issue_polled(instance, cmd)) |
| 755 | ret = 0; |
| 756 | else { |
| 757 | printk(KERN_ERR "megasas: Get LD Map Info Failed\n"); |
| 758 | ret = -1; |
| 759 | } |
| 760 | |
| 761 | megasas_return_cmd(instance, cmd); |
| 762 | |
| 763 | return ret; |
| 764 | } |
| 765 | |
| 766 | u8 |
| 767 | megasas_get_map_info(struct megasas_instance *instance) |
| 768 | { |
| 769 | struct fusion_context *fusion = instance->ctrl_context; |
| 770 | |
| 771 | fusion->fast_path_io = 0; |
| 772 | if (!megasas_get_ld_map_info(instance)) { |
| 773 | if (MR_ValidateMapInfo(fusion->ld_map[(instance->map_id & 1)], |
| 774 | fusion->load_balance_info)) { |
| 775 | fusion->fast_path_io = 1; |
| 776 | return 0; |
| 777 | } |
| 778 | } |
| 779 | return 1; |
| 780 | } |
| 781 | |
| 782 | /* |
| 783 | * megasas_sync_map_info - Returns FW's ld_map structure |
| 784 | * @instance: Adapter soft state |
| 785 | * |
| 786 | * Issues an internal command (DCMD) to get the FW's controller PD |
| 787 | * list structure. This information is mainly used to find out SYSTEM |
| 788 | * supported by the FW. |
| 789 | */ |
| 790 | int |
| 791 | megasas_sync_map_info(struct megasas_instance *instance) |
| 792 | { |
| 793 | int ret = 0, i; |
| 794 | struct megasas_cmd *cmd; |
| 795 | struct megasas_dcmd_frame *dcmd; |
| 796 | u32 size_sync_info, num_lds; |
| 797 | struct fusion_context *fusion; |
| 798 | struct MR_LD_TARGET_SYNC *ci = NULL; |
| 799 | struct MR_FW_RAID_MAP_ALL *map; |
| 800 | struct MR_LD_RAID *raid; |
| 801 | struct MR_LD_TARGET_SYNC *ld_sync; |
| 802 | dma_addr_t ci_h = 0; |
| 803 | u32 size_map_info; |
| 804 | |
| 805 | cmd = megasas_get_cmd(instance); |
| 806 | |
| 807 | if (!cmd) { |
| 808 | printk(KERN_DEBUG "megasas: Failed to get cmd for sync" |
| 809 | "info.\n"); |
| 810 | return -ENOMEM; |
| 811 | } |
| 812 | |
| 813 | fusion = instance->ctrl_context; |
| 814 | |
| 815 | if (!fusion) { |
| 816 | megasas_return_cmd(instance, cmd); |
| 817 | return 1; |
| 818 | } |
| 819 | |
| 820 | map = fusion->ld_map[instance->map_id & 1]; |
| 821 | |
| 822 | num_lds = map->raidMap.ldCount; |
| 823 | |
| 824 | dcmd = &cmd->frame->dcmd; |
| 825 | |
| 826 | size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds; |
| 827 | |
| 828 | memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE); |
| 829 | |
| 830 | ci = (struct MR_LD_TARGET_SYNC *) |
| 831 | fusion->ld_map[(instance->map_id - 1) & 1]; |
| 832 | memset(ci, 0, sizeof(struct MR_FW_RAID_MAP_ALL)); |
| 833 | |
| 834 | ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1]; |
| 835 | |
| 836 | ld_sync = (struct MR_LD_TARGET_SYNC *)ci; |
| 837 | |
| 838 | for (i = 0; i < num_lds; i++, ld_sync++) { |
| 839 | raid = MR_LdRaidGet(i, map); |
| 840 | ld_sync->targetId = MR_GetLDTgtId(i, map); |
| 841 | ld_sync->seqNum = raid->seqNum; |
| 842 | } |
| 843 | |
| 844 | size_map_info = sizeof(struct MR_FW_RAID_MAP) + |
| 845 | (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1)); |
| 846 | |
| 847 | dcmd->cmd = MFI_CMD_DCMD; |
| 848 | dcmd->cmd_status = 0xFF; |
| 849 | dcmd->sge_count = 1; |
| 850 | dcmd->flags = MFI_FRAME_DIR_WRITE; |
| 851 | dcmd->timeout = 0; |
| 852 | dcmd->pad_0 = 0; |
| 853 | dcmd->data_xfer_len = size_map_info; |
| 854 | dcmd->mbox.b[0] = num_lds; |
| 855 | dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG; |
| 856 | dcmd->opcode = MR_DCMD_LD_MAP_GET_INFO; |
| 857 | dcmd->sgl.sge32[0].phys_addr = ci_h; |
| 858 | dcmd->sgl.sge32[0].length = size_map_info; |
| 859 | |
| 860 | instance->map_update_cmd = cmd; |
| 861 | |
| 862 | instance->instancet->issue_dcmd(instance, cmd); |
| 863 | |
| 864 | return ret; |
| 865 | } |
| 866 | |
| 867 | /** |
| 868 | * megasas_init_adapter_fusion - Initializes the FW |
| 869 | * @instance: Adapter soft state |
| 870 | * |
| 871 | * This is the main function for initializing firmware. |
| 872 | */ |
| 873 | u32 |
| 874 | megasas_init_adapter_fusion(struct megasas_instance *instance) |
| 875 | { |
| 876 | struct megasas_register_set __iomem *reg_set; |
| 877 | struct fusion_context *fusion; |
| 878 | u32 max_cmd; |
adam radford | c8e858f | 2011-10-08 18:15:13 -0700 | [diff] [blame] | 879 | int i = 0, count; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 880 | |
| 881 | fusion = instance->ctrl_context; |
| 882 | |
| 883 | reg_set = instance->reg_set; |
| 884 | |
| 885 | /* |
| 886 | * Get various operational parameters from status register |
| 887 | */ |
| 888 | instance->max_fw_cmds = |
| 889 | instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF; |
| 890 | instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008); |
| 891 | |
| 892 | /* |
| 893 | * Reduce the max supported cmds by 1. This is to ensure that the |
| 894 | * reply_q_sz (1 more than the max cmd that driver may send) |
| 895 | * does not exceed max cmds that the FW can support |
| 896 | */ |
| 897 | instance->max_fw_cmds = instance->max_fw_cmds-1; |
| 898 | /* Only internal cmds (DCMD) need to have MFI frames */ |
| 899 | instance->max_mfi_cmds = MEGASAS_INT_CMDS; |
| 900 | |
| 901 | max_cmd = instance->max_fw_cmds; |
| 902 | |
| 903 | fusion->reply_q_depth = ((max_cmd + 1 + 15)/16)*16; |
| 904 | |
| 905 | fusion->request_alloc_sz = |
| 906 | sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd; |
| 907 | fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION) |
| 908 | *(fusion->reply_q_depth); |
| 909 | fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE + |
| 910 | (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * |
| 911 | (max_cmd + 1)); /* Extra 1 for SMID 0 */ |
| 912 | |
| 913 | fusion->max_sge_in_main_msg = |
| 914 | (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE - |
| 915 | offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16; |
| 916 | |
| 917 | fusion->max_sge_in_chain = |
| 918 | MEGASAS_MAX_SZ_CHAIN_FRAME / sizeof(union MPI2_SGE_IO_UNION); |
| 919 | |
| 920 | instance->max_num_sge = fusion->max_sge_in_main_msg + |
| 921 | fusion->max_sge_in_chain - 2; |
| 922 | |
| 923 | /* Used for pass thru MFI frame (DCMD) */ |
| 924 | fusion->chain_offset_mfi_pthru = |
| 925 | offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16; |
| 926 | |
| 927 | fusion->chain_offset_io_request = |
| 928 | (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE - |
| 929 | sizeof(union MPI2_SGE_IO_UNION))/16; |
| 930 | |
adam radford | c8e858f | 2011-10-08 18:15:13 -0700 | [diff] [blame] | 931 | count = instance->msix_vectors > 0 ? instance->msix_vectors : 1; |
| 932 | for (i = 0 ; i < count; i++) |
| 933 | fusion->last_reply_idx[i] = 0; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 934 | |
| 935 | /* |
| 936 | * Allocate memory for descriptors |
| 937 | * Create a pool of commands |
| 938 | */ |
| 939 | if (megasas_alloc_cmds(instance)) |
| 940 | goto fail_alloc_mfi_cmds; |
| 941 | if (megasas_alloc_cmds_fusion(instance)) |
| 942 | goto fail_alloc_cmds; |
| 943 | |
| 944 | if (megasas_ioc_init_fusion(instance)) |
| 945 | goto fail_ioc_init; |
| 946 | |
| 947 | instance->flag_ieee = 1; |
| 948 | |
| 949 | fusion->map_sz = sizeof(struct MR_FW_RAID_MAP) + |
| 950 | (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1)); |
| 951 | |
| 952 | fusion->fast_path_io = 0; |
| 953 | |
| 954 | for (i = 0; i < 2; i++) { |
| 955 | fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev, |
| 956 | fusion->map_sz, |
| 957 | &fusion->ld_map_phys[i], |
| 958 | GFP_KERNEL); |
| 959 | if (!fusion->ld_map[i]) { |
| 960 | printk(KERN_ERR "megasas: Could not allocate memory " |
| 961 | "for map info\n"); |
| 962 | goto fail_map_info; |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | if (!megasas_get_map_info(instance)) |
| 967 | megasas_sync_map_info(instance); |
| 968 | |
| 969 | return 0; |
| 970 | |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 971 | fail_map_info: |
| 972 | if (i == 1) |
| 973 | dma_free_coherent(&instance->pdev->dev, fusion->map_sz, |
| 974 | fusion->ld_map[0], fusion->ld_map_phys[0]); |
| 975 | fail_ioc_init: |
adam radford | eb1b123 | 2011-02-24 20:55:56 -0800 | [diff] [blame] | 976 | megasas_free_cmds_fusion(instance); |
| 977 | fail_alloc_cmds: |
| 978 | megasas_free_cmds(instance); |
| 979 | fail_alloc_mfi_cmds: |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 980 | return 1; |
| 981 | } |
| 982 | |
| 983 | /** |
| 984 | * megasas_fire_cmd_fusion - Sends command to the FW |
| 985 | * @frame_phys_addr : Physical address of cmd |
| 986 | * @frame_count : Number of frames for the command |
| 987 | * @regs : MFI register set |
| 988 | */ |
| 989 | void |
| 990 | megasas_fire_cmd_fusion(struct megasas_instance *instance, |
| 991 | dma_addr_t req_desc_lo, |
| 992 | u32 req_desc_hi, |
| 993 | struct megasas_register_set __iomem *regs) |
| 994 | { |
| 995 | unsigned long flags; |
| 996 | |
| 997 | spin_lock_irqsave(&instance->hba_lock, flags); |
| 998 | |
| 999 | writel(req_desc_lo, |
| 1000 | &(regs)->inbound_low_queue_port); |
| 1001 | writel(req_desc_hi, &(regs)->inbound_high_queue_port); |
| 1002 | spin_unlock_irqrestore(&instance->hba_lock, flags); |
| 1003 | } |
| 1004 | |
| 1005 | /** |
| 1006 | * map_cmd_status - Maps FW cmd status to OS cmd status |
| 1007 | * @cmd : Pointer to cmd |
| 1008 | * @status : status of cmd returned by FW |
| 1009 | * @ext_status : ext status of cmd returned by FW |
| 1010 | */ |
| 1011 | |
| 1012 | void |
| 1013 | map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status) |
| 1014 | { |
| 1015 | |
| 1016 | switch (status) { |
| 1017 | |
| 1018 | case MFI_STAT_OK: |
| 1019 | cmd->scmd->result = DID_OK << 16; |
| 1020 | break; |
| 1021 | |
| 1022 | case MFI_STAT_SCSI_IO_FAILED: |
| 1023 | case MFI_STAT_LD_INIT_IN_PROGRESS: |
| 1024 | cmd->scmd->result = (DID_ERROR << 16) | ext_status; |
| 1025 | break; |
| 1026 | |
| 1027 | case MFI_STAT_SCSI_DONE_WITH_ERROR: |
| 1028 | |
| 1029 | cmd->scmd->result = (DID_OK << 16) | ext_status; |
| 1030 | if (ext_status == SAM_STAT_CHECK_CONDITION) { |
| 1031 | memset(cmd->scmd->sense_buffer, 0, |
| 1032 | SCSI_SENSE_BUFFERSIZE); |
| 1033 | memcpy(cmd->scmd->sense_buffer, cmd->sense, |
| 1034 | SCSI_SENSE_BUFFERSIZE); |
| 1035 | cmd->scmd->result |= DRIVER_SENSE << 24; |
| 1036 | } |
| 1037 | break; |
| 1038 | |
| 1039 | case MFI_STAT_LD_OFFLINE: |
| 1040 | case MFI_STAT_DEVICE_NOT_FOUND: |
| 1041 | cmd->scmd->result = DID_BAD_TARGET << 16; |
| 1042 | break; |
adam radford | 36807e6 | 2011-10-08 18:15:06 -0700 | [diff] [blame] | 1043 | case MFI_STAT_CONFIG_SEQ_MISMATCH: |
| 1044 | cmd->scmd->result = DID_IMM_RETRY << 16; |
| 1045 | break; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1046 | default: |
| 1047 | printk(KERN_DEBUG "megasas: FW status %#x\n", status); |
| 1048 | cmd->scmd->result = DID_ERROR << 16; |
| 1049 | break; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | /** |
| 1054 | * megasas_make_sgl_fusion - Prepares 32-bit SGL |
| 1055 | * @instance: Adapter soft state |
| 1056 | * @scp: SCSI command from the mid-layer |
| 1057 | * @sgl_ptr: SGL to be filled in |
| 1058 | * @cmd: cmd we are working on |
| 1059 | * |
| 1060 | * If successful, this function returns the number of SG elements. |
| 1061 | */ |
| 1062 | static int |
| 1063 | megasas_make_sgl_fusion(struct megasas_instance *instance, |
| 1064 | struct scsi_cmnd *scp, |
| 1065 | struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr, |
| 1066 | struct megasas_cmd_fusion *cmd) |
| 1067 | { |
adam radford | 36807e6 | 2011-10-08 18:15:06 -0700 | [diff] [blame] | 1068 | int i, sg_processed, sge_count; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1069 | struct scatterlist *os_sgl; |
| 1070 | struct fusion_context *fusion; |
| 1071 | |
| 1072 | fusion = instance->ctrl_context; |
| 1073 | |
adam radford | 36807e6 | 2011-10-08 18:15:06 -0700 | [diff] [blame] | 1074 | if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) { |
| 1075 | struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr; |
| 1076 | sgl_ptr_end += fusion->max_sge_in_main_msg - 1; |
| 1077 | sgl_ptr_end->Flags = 0; |
| 1078 | } |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1079 | |
| 1080 | sge_count = scsi_dma_map(scp); |
| 1081 | |
| 1082 | BUG_ON(sge_count < 0); |
| 1083 | |
| 1084 | if (sge_count > instance->max_num_sge || !sge_count) |
| 1085 | return sge_count; |
| 1086 | |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1087 | scsi_for_each_sg(scp, os_sgl, sge_count, i) { |
| 1088 | sgl_ptr->Length = sg_dma_len(os_sgl); |
| 1089 | sgl_ptr->Address = sg_dma_address(os_sgl); |
| 1090 | sgl_ptr->Flags = 0; |
adam radford | 36807e6 | 2011-10-08 18:15:06 -0700 | [diff] [blame] | 1091 | if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) { |
| 1092 | if (i == sge_count - 1) |
| 1093 | sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST; |
| 1094 | } |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1095 | sgl_ptr++; |
| 1096 | |
| 1097 | sg_processed = i + 1; |
| 1098 | |
| 1099 | if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) && |
| 1100 | (sge_count > fusion->max_sge_in_main_msg)) { |
| 1101 | |
| 1102 | struct MPI25_IEEE_SGE_CHAIN64 *sg_chain; |
adam radford | 36807e6 | 2011-10-08 18:15:06 -0700 | [diff] [blame] | 1103 | if (instance->pdev->device == |
| 1104 | PCI_DEVICE_ID_LSI_INVADER) { |
| 1105 | if ((cmd->io_request->IoFlags & |
| 1106 | MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) != |
| 1107 | MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) |
| 1108 | cmd->io_request->ChainOffset = |
| 1109 | fusion-> |
| 1110 | chain_offset_io_request; |
| 1111 | else |
| 1112 | cmd->io_request->ChainOffset = 0; |
| 1113 | } else |
| 1114 | cmd->io_request->ChainOffset = |
| 1115 | fusion->chain_offset_io_request; |
| 1116 | |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1117 | sg_chain = sgl_ptr; |
| 1118 | /* Prepare chain element */ |
| 1119 | sg_chain->NextChainOffset = 0; |
adam radford | 36807e6 | 2011-10-08 18:15:06 -0700 | [diff] [blame] | 1120 | if (instance->pdev->device == |
| 1121 | PCI_DEVICE_ID_LSI_INVADER) |
| 1122 | sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT; |
| 1123 | else |
| 1124 | sg_chain->Flags = |
| 1125 | (IEEE_SGE_FLAGS_CHAIN_ELEMENT | |
| 1126 | MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR); |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1127 | sg_chain->Length = (sizeof(union MPI2_SGE_IO_UNION) |
| 1128 | *(sge_count - sg_processed)); |
| 1129 | sg_chain->Address = cmd->sg_frame_phys_addr; |
| 1130 | |
| 1131 | sgl_ptr = |
| 1132 | (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame; |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | return sge_count; |
| 1137 | } |
| 1138 | |
| 1139 | /** |
| 1140 | * megasas_set_pd_lba - Sets PD LBA |
| 1141 | * @cdb: CDB |
| 1142 | * @cdb_len: cdb length |
| 1143 | * @start_blk: Start block of IO |
| 1144 | * |
| 1145 | * Used to set the PD LBA in CDB for FP IOs |
| 1146 | */ |
| 1147 | void |
| 1148 | megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len, |
| 1149 | struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp, |
| 1150 | struct MR_FW_RAID_MAP_ALL *local_map_ptr, u32 ref_tag) |
| 1151 | { |
| 1152 | struct MR_LD_RAID *raid; |
| 1153 | u32 ld; |
| 1154 | u64 start_blk = io_info->pdBlock; |
| 1155 | u8 *cdb = io_request->CDB.CDB32; |
| 1156 | u32 num_blocks = io_info->numBlocks; |
adam radford | 495c560 | 2011-05-11 18:34:40 -0700 | [diff] [blame] | 1157 | u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1158 | |
| 1159 | /* Check if T10 PI (DIF) is enabled for this LD */ |
| 1160 | ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr); |
| 1161 | raid = MR_LdRaidGet(ld, local_map_ptr); |
| 1162 | if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) { |
| 1163 | memset(cdb, 0, sizeof(io_request->CDB.CDB32)); |
| 1164 | cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD; |
| 1165 | cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN; |
| 1166 | |
| 1167 | if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) |
| 1168 | cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32; |
| 1169 | else |
| 1170 | cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32; |
| 1171 | cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL; |
| 1172 | |
| 1173 | /* LBA */ |
| 1174 | cdb[12] = (u8)((start_blk >> 56) & 0xff); |
| 1175 | cdb[13] = (u8)((start_blk >> 48) & 0xff); |
| 1176 | cdb[14] = (u8)((start_blk >> 40) & 0xff); |
| 1177 | cdb[15] = (u8)((start_blk >> 32) & 0xff); |
| 1178 | cdb[16] = (u8)((start_blk >> 24) & 0xff); |
| 1179 | cdb[17] = (u8)((start_blk >> 16) & 0xff); |
| 1180 | cdb[18] = (u8)((start_blk >> 8) & 0xff); |
| 1181 | cdb[19] = (u8)(start_blk & 0xff); |
| 1182 | |
| 1183 | /* Logical block reference tag */ |
| 1184 | io_request->CDB.EEDP32.PrimaryReferenceTag = |
| 1185 | cpu_to_be32(ref_tag); |
| 1186 | io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0xffff; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1187 | io_request->IoFlags = 32; /* Specify 32-byte cdb */ |
| 1188 | |
| 1189 | /* Transfer length */ |
| 1190 | cdb[28] = (u8)((num_blocks >> 24) & 0xff); |
| 1191 | cdb[29] = (u8)((num_blocks >> 16) & 0xff); |
| 1192 | cdb[30] = (u8)((num_blocks >> 8) & 0xff); |
| 1193 | cdb[31] = (u8)(num_blocks & 0xff); |
| 1194 | |
| 1195 | /* set SCSI IO EEDPFlags */ |
| 1196 | if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) { |
| 1197 | io_request->EEDPFlags = |
| 1198 | MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG | |
| 1199 | MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG | |
| 1200 | MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP | |
| 1201 | MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG | |
| 1202 | MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD; |
| 1203 | } else { |
| 1204 | io_request->EEDPFlags = |
| 1205 | MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG | |
| 1206 | MPI2_SCSIIO_EEDPFLAGS_INSERT_OP; |
| 1207 | } |
| 1208 | io_request->Control |= (0x4 << 26); |
| 1209 | io_request->EEDPBlockSize = MEGASAS_EEDPBLOCKSIZE; |
| 1210 | } else { |
| 1211 | /* Some drives don't support 16/12 byte CDB's, convert to 10 */ |
| 1212 | if (((cdb_len == 12) || (cdb_len == 16)) && |
| 1213 | (start_blk <= 0xffffffff)) { |
| 1214 | if (cdb_len == 16) { |
| 1215 | opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10; |
| 1216 | flagvals = cdb[1]; |
| 1217 | groupnum = cdb[14]; |
| 1218 | control = cdb[15]; |
| 1219 | } else { |
| 1220 | opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10; |
| 1221 | flagvals = cdb[1]; |
| 1222 | groupnum = cdb[10]; |
| 1223 | control = cdb[11]; |
| 1224 | } |
| 1225 | |
| 1226 | memset(cdb, 0, sizeof(io_request->CDB.CDB32)); |
| 1227 | |
| 1228 | cdb[0] = opcode; |
| 1229 | cdb[1] = flagvals; |
| 1230 | cdb[6] = groupnum; |
| 1231 | cdb[9] = control; |
| 1232 | |
| 1233 | /* Transfer length */ |
| 1234 | cdb[8] = (u8)(num_blocks & 0xff); |
| 1235 | cdb[7] = (u8)((num_blocks >> 8) & 0xff); |
| 1236 | |
adam radford | 495c560 | 2011-05-11 18:34:40 -0700 | [diff] [blame] | 1237 | io_request->IoFlags = 10; /* Specify 10-byte cdb */ |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1238 | cdb_len = 10; |
adam radford | 495c560 | 2011-05-11 18:34:40 -0700 | [diff] [blame] | 1239 | } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) { |
| 1240 | /* Convert to 16 byte CDB for large LBA's */ |
| 1241 | switch (cdb_len) { |
| 1242 | case 6: |
| 1243 | opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16; |
| 1244 | control = cdb[5]; |
| 1245 | break; |
| 1246 | case 10: |
| 1247 | opcode = |
| 1248 | cdb[0] == READ_10 ? READ_16 : WRITE_16; |
| 1249 | flagvals = cdb[1]; |
| 1250 | groupnum = cdb[6]; |
| 1251 | control = cdb[9]; |
| 1252 | break; |
| 1253 | case 12: |
| 1254 | opcode = |
| 1255 | cdb[0] == READ_12 ? READ_16 : WRITE_16; |
| 1256 | flagvals = cdb[1]; |
| 1257 | groupnum = cdb[10]; |
| 1258 | control = cdb[11]; |
| 1259 | break; |
| 1260 | } |
| 1261 | |
| 1262 | memset(cdb, 0, sizeof(io_request->CDB.CDB32)); |
| 1263 | |
| 1264 | cdb[0] = opcode; |
| 1265 | cdb[1] = flagvals; |
| 1266 | cdb[14] = groupnum; |
| 1267 | cdb[15] = control; |
| 1268 | |
| 1269 | /* Transfer length */ |
| 1270 | cdb[13] = (u8)(num_blocks & 0xff); |
| 1271 | cdb[12] = (u8)((num_blocks >> 8) & 0xff); |
| 1272 | cdb[11] = (u8)((num_blocks >> 16) & 0xff); |
| 1273 | cdb[10] = (u8)((num_blocks >> 24) & 0xff); |
| 1274 | |
| 1275 | io_request->IoFlags = 16; /* Specify 16-byte cdb */ |
| 1276 | cdb_len = 16; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | /* Normal case, just load LBA here */ |
| 1280 | switch (cdb_len) { |
| 1281 | case 6: |
| 1282 | { |
| 1283 | u8 val = cdb[1] & 0xE0; |
| 1284 | cdb[3] = (u8)(start_blk & 0xff); |
| 1285 | cdb[2] = (u8)((start_blk >> 8) & 0xff); |
| 1286 | cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f); |
| 1287 | break; |
| 1288 | } |
| 1289 | case 10: |
| 1290 | cdb[5] = (u8)(start_blk & 0xff); |
| 1291 | cdb[4] = (u8)((start_blk >> 8) & 0xff); |
| 1292 | cdb[3] = (u8)((start_blk >> 16) & 0xff); |
| 1293 | cdb[2] = (u8)((start_blk >> 24) & 0xff); |
| 1294 | break; |
| 1295 | case 12: |
| 1296 | cdb[5] = (u8)(start_blk & 0xff); |
| 1297 | cdb[4] = (u8)((start_blk >> 8) & 0xff); |
| 1298 | cdb[3] = (u8)((start_blk >> 16) & 0xff); |
| 1299 | cdb[2] = (u8)((start_blk >> 24) & 0xff); |
| 1300 | break; |
| 1301 | case 16: |
| 1302 | cdb[9] = (u8)(start_blk & 0xff); |
| 1303 | cdb[8] = (u8)((start_blk >> 8) & 0xff); |
| 1304 | cdb[7] = (u8)((start_blk >> 16) & 0xff); |
| 1305 | cdb[6] = (u8)((start_blk >> 24) & 0xff); |
| 1306 | cdb[5] = (u8)((start_blk >> 32) & 0xff); |
| 1307 | cdb[4] = (u8)((start_blk >> 40) & 0xff); |
| 1308 | cdb[3] = (u8)((start_blk >> 48) & 0xff); |
| 1309 | cdb[2] = (u8)((start_blk >> 56) & 0xff); |
| 1310 | break; |
| 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | /** |
| 1316 | * megasas_build_ldio_fusion - Prepares IOs to devices |
| 1317 | * @instance: Adapter soft state |
| 1318 | * @scp: SCSI command |
| 1319 | * @cmd: Command to be prepared |
| 1320 | * |
| 1321 | * Prepares the io_request and chain elements (sg_frame) for IO |
| 1322 | * The IO can be for PD (Fast Path) or LD |
| 1323 | */ |
| 1324 | void |
| 1325 | megasas_build_ldio_fusion(struct megasas_instance *instance, |
| 1326 | struct scsi_cmnd *scp, |
| 1327 | struct megasas_cmd_fusion *cmd) |
| 1328 | { |
| 1329 | u8 fp_possible; |
adam radford | f9eff81 | 2012-10-01 19:27:07 -0700 | [diff] [blame] | 1330 | u32 start_lba_lo, start_lba_hi, device_id, datalength = 0; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1331 | struct MPI2_RAID_SCSI_IO_REQUEST *io_request; |
| 1332 | union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc; |
| 1333 | struct IO_REQUEST_INFO io_info; |
| 1334 | struct fusion_context *fusion; |
| 1335 | struct MR_FW_RAID_MAP_ALL *local_map_ptr; |
| 1336 | |
| 1337 | device_id = MEGASAS_DEV_INDEX(instance, scp); |
| 1338 | |
| 1339 | fusion = instance->ctrl_context; |
| 1340 | |
| 1341 | io_request = cmd->io_request; |
| 1342 | io_request->RaidContext.VirtualDiskTgtId = device_id; |
| 1343 | io_request->RaidContext.status = 0; |
| 1344 | io_request->RaidContext.exStatus = 0; |
| 1345 | |
| 1346 | req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc; |
| 1347 | |
| 1348 | start_lba_lo = 0; |
| 1349 | start_lba_hi = 0; |
| 1350 | fp_possible = 0; |
| 1351 | |
| 1352 | /* |
| 1353 | * 6-byte READ(0x08) or WRITE(0x0A) cdb |
| 1354 | */ |
| 1355 | if (scp->cmd_len == 6) { |
adam radford | f9eff81 | 2012-10-01 19:27:07 -0700 | [diff] [blame] | 1356 | datalength = (u32) scp->cmnd[4]; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1357 | start_lba_lo = ((u32) scp->cmnd[1] << 16) | |
| 1358 | ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3]; |
| 1359 | |
| 1360 | start_lba_lo &= 0x1FFFFF; |
| 1361 | } |
| 1362 | |
| 1363 | /* |
| 1364 | * 10-byte READ(0x28) or WRITE(0x2A) cdb |
| 1365 | */ |
| 1366 | else if (scp->cmd_len == 10) { |
adam radford | f9eff81 | 2012-10-01 19:27:07 -0700 | [diff] [blame] | 1367 | datalength = (u32) scp->cmnd[8] | |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1368 | ((u32) scp->cmnd[7] << 8); |
| 1369 | start_lba_lo = ((u32) scp->cmnd[2] << 24) | |
| 1370 | ((u32) scp->cmnd[3] << 16) | |
| 1371 | ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5]; |
| 1372 | } |
| 1373 | |
| 1374 | /* |
| 1375 | * 12-byte READ(0xA8) or WRITE(0xAA) cdb |
| 1376 | */ |
| 1377 | else if (scp->cmd_len == 12) { |
adam radford | f9eff81 | 2012-10-01 19:27:07 -0700 | [diff] [blame] | 1378 | datalength = ((u32) scp->cmnd[6] << 24) | |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1379 | ((u32) scp->cmnd[7] << 16) | |
| 1380 | ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9]; |
| 1381 | start_lba_lo = ((u32) scp->cmnd[2] << 24) | |
| 1382 | ((u32) scp->cmnd[3] << 16) | |
| 1383 | ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5]; |
| 1384 | } |
| 1385 | |
| 1386 | /* |
| 1387 | * 16-byte READ(0x88) or WRITE(0x8A) cdb |
| 1388 | */ |
| 1389 | else if (scp->cmd_len == 16) { |
adam radford | f9eff81 | 2012-10-01 19:27:07 -0700 | [diff] [blame] | 1390 | datalength = ((u32) scp->cmnd[10] << 24) | |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1391 | ((u32) scp->cmnd[11] << 16) | |
| 1392 | ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13]; |
| 1393 | start_lba_lo = ((u32) scp->cmnd[6] << 24) | |
| 1394 | ((u32) scp->cmnd[7] << 16) | |
| 1395 | ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9]; |
| 1396 | |
| 1397 | start_lba_hi = ((u32) scp->cmnd[2] << 24) | |
| 1398 | ((u32) scp->cmnd[3] << 16) | |
| 1399 | ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5]; |
| 1400 | } |
| 1401 | |
| 1402 | memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO)); |
| 1403 | io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo; |
adam radford | f9eff81 | 2012-10-01 19:27:07 -0700 | [diff] [blame] | 1404 | io_info.numBlocks = datalength; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1405 | io_info.ldTgtId = device_id; |
adam radford | f9eff81 | 2012-10-01 19:27:07 -0700 | [diff] [blame] | 1406 | io_request->DataLength = scsi_bufflen(scp); |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1407 | |
| 1408 | if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) |
| 1409 | io_info.isRead = 1; |
| 1410 | |
| 1411 | local_map_ptr = fusion->ld_map[(instance->map_id & 1)]; |
| 1412 | |
| 1413 | if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >= |
| 1414 | MAX_LOGICAL_DRIVES) || (!fusion->fast_path_io)) { |
| 1415 | io_request->RaidContext.regLockFlags = 0; |
| 1416 | fp_possible = 0; |
| 1417 | } else { |
adam radford | 36807e6 | 2011-10-08 18:15:06 -0700 | [diff] [blame] | 1418 | if (MR_BuildRaidContext(instance, &io_info, |
| 1419 | &io_request->RaidContext, |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1420 | local_map_ptr)) |
| 1421 | fp_possible = io_info.fpOkForIo; |
| 1422 | } |
| 1423 | |
adam radford | c8e858f | 2011-10-08 18:15:13 -0700 | [diff] [blame] | 1424 | /* Use smp_processor_id() for now until cmd->request->cpu is CPU |
| 1425 | id by default, not CPU group id, otherwise all MSI-X queues won't |
| 1426 | be utilized */ |
| 1427 | cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ? |
| 1428 | smp_processor_id() % instance->msix_vectors : 0; |
| 1429 | |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1430 | if (fp_possible) { |
| 1431 | megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp, |
| 1432 | local_map_ptr, start_lba_lo); |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1433 | io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST; |
| 1434 | cmd->request_desc->SCSIIO.RequestFlags = |
| 1435 | (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY |
| 1436 | << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); |
adam radford | 36807e6 | 2011-10-08 18:15:06 -0700 | [diff] [blame] | 1437 | if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) { |
| 1438 | if (io_request->RaidContext.regLockFlags == |
| 1439 | REGION_TYPE_UNUSED) |
| 1440 | cmd->request_desc->SCSIIO.RequestFlags = |
| 1441 | (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK << |
| 1442 | MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); |
| 1443 | io_request->RaidContext.Type = MPI2_TYPE_CUDA; |
| 1444 | io_request->RaidContext.nseg = 0x1; |
| 1445 | io_request->IoFlags |= |
| 1446 | MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH; |
| 1447 | io_request->RaidContext.regLockFlags |= |
| 1448 | (MR_RL_FLAGS_GRANT_DESTINATION_CUDA | |
| 1449 | MR_RL_FLAGS_SEQ_NUM_ENABLE); |
| 1450 | } |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1451 | if ((fusion->load_balance_info[device_id].loadBalanceFlag) && |
| 1452 | (io_info.isRead)) { |
| 1453 | io_info.devHandle = |
| 1454 | get_updated_dev_handle( |
| 1455 | &fusion->load_balance_info[device_id], |
| 1456 | &io_info); |
| 1457 | scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG; |
| 1458 | } else |
| 1459 | scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG; |
| 1460 | cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle; |
| 1461 | io_request->DevHandle = io_info.devHandle; |
| 1462 | } else { |
| 1463 | io_request->RaidContext.timeoutValue = |
| 1464 | local_map_ptr->raidMap.fpPdIoTimeoutSec; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1465 | cmd->request_desc->SCSIIO.RequestFlags = |
| 1466 | (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO |
| 1467 | << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); |
adam radford | 36807e6 | 2011-10-08 18:15:06 -0700 | [diff] [blame] | 1468 | if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) { |
| 1469 | if (io_request->RaidContext.regLockFlags == |
| 1470 | REGION_TYPE_UNUSED) |
| 1471 | cmd->request_desc->SCSIIO.RequestFlags = |
| 1472 | (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK << |
| 1473 | MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); |
| 1474 | io_request->RaidContext.Type = MPI2_TYPE_CUDA; |
| 1475 | io_request->RaidContext.regLockFlags |= |
| 1476 | (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 | |
| 1477 | MR_RL_FLAGS_SEQ_NUM_ENABLE); |
| 1478 | io_request->RaidContext.nseg = 0x1; |
| 1479 | } |
| 1480 | io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST; |
| 1481 | io_request->DevHandle = device_id; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1482 | } /* Not FP */ |
| 1483 | } |
| 1484 | |
| 1485 | /** |
| 1486 | * megasas_build_dcdb_fusion - Prepares IOs to devices |
| 1487 | * @instance: Adapter soft state |
| 1488 | * @scp: SCSI command |
| 1489 | * @cmd: Command to be prepared |
| 1490 | * |
| 1491 | * Prepares the io_request frame for non-io cmds |
| 1492 | */ |
| 1493 | static void |
| 1494 | megasas_build_dcdb_fusion(struct megasas_instance *instance, |
| 1495 | struct scsi_cmnd *scmd, |
| 1496 | struct megasas_cmd_fusion *cmd) |
| 1497 | { |
| 1498 | u32 device_id; |
| 1499 | struct MPI2_RAID_SCSI_IO_REQUEST *io_request; |
| 1500 | u16 pd_index = 0; |
| 1501 | struct MR_FW_RAID_MAP_ALL *local_map_ptr; |
| 1502 | struct fusion_context *fusion = instance->ctrl_context; |
| 1503 | |
| 1504 | io_request = cmd->io_request; |
| 1505 | device_id = MEGASAS_DEV_INDEX(instance, scmd); |
| 1506 | pd_index = (scmd->device->channel * MEGASAS_MAX_DEV_PER_CHANNEL) |
| 1507 | +scmd->device->id; |
| 1508 | local_map_ptr = fusion->ld_map[(instance->map_id & 1)]; |
| 1509 | |
| 1510 | /* Check if this is a system PD I/O */ |
adam radford | e187df6 | 2012-10-01 19:27:12 -0700 | [diff] [blame] | 1511 | if (scmd->device->channel < MEGASAS_MAX_PD_CHANNELS && |
| 1512 | instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) { |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1513 | io_request->Function = 0; |
| 1514 | io_request->DevHandle = |
| 1515 | local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl; |
| 1516 | io_request->RaidContext.timeoutValue = |
| 1517 | local_map_ptr->raidMap.fpPdIoTimeoutSec; |
| 1518 | io_request->RaidContext.regLockFlags = 0; |
| 1519 | io_request->RaidContext.regLockRowLBA = 0; |
| 1520 | io_request->RaidContext.regLockLength = 0; |
| 1521 | io_request->RaidContext.RAIDFlags = |
| 1522 | MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD << |
| 1523 | MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT; |
| 1524 | cmd->request_desc->SCSIIO.RequestFlags = |
| 1525 | (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY << |
| 1526 | MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); |
adam radford | 8ead581 | 2012-10-01 19:27:20 -0700 | [diff] [blame^] | 1527 | cmd->request_desc->SCSIIO.DevHandle = |
| 1528 | local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1529 | } else { |
| 1530 | io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST; |
| 1531 | io_request->DevHandle = device_id; |
| 1532 | cmd->request_desc->SCSIIO.RequestFlags = |
| 1533 | (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO << |
| 1534 | MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); |
| 1535 | } |
| 1536 | io_request->RaidContext.VirtualDiskTgtId = device_id; |
adam radford | eaa3c24 | 2011-02-24 20:56:49 -0800 | [diff] [blame] | 1537 | io_request->LUN[1] = scmd->device->lun; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1538 | io_request->DataLength = scsi_bufflen(scmd); |
| 1539 | } |
| 1540 | |
| 1541 | /** |
| 1542 | * megasas_build_io_fusion - Prepares IOs to devices |
| 1543 | * @instance: Adapter soft state |
| 1544 | * @scp: SCSI command |
| 1545 | * @cmd: Command to be prepared |
| 1546 | * |
| 1547 | * Invokes helper functions to prepare request frames |
| 1548 | * and sets flags appropriate for IO/Non-IO cmd |
| 1549 | */ |
| 1550 | int |
| 1551 | megasas_build_io_fusion(struct megasas_instance *instance, |
| 1552 | struct scsi_cmnd *scp, |
| 1553 | struct megasas_cmd_fusion *cmd) |
| 1554 | { |
| 1555 | u32 device_id, sge_count; |
| 1556 | struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request; |
| 1557 | |
| 1558 | device_id = MEGASAS_DEV_INDEX(instance, scp); |
| 1559 | |
| 1560 | /* Zero out some fields so they don't get reused */ |
adam radford | eaa3c24 | 2011-02-24 20:56:49 -0800 | [diff] [blame] | 1561 | io_request->LUN[1] = 0; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1562 | io_request->CDB.EEDP32.PrimaryReferenceTag = 0; |
| 1563 | io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0; |
| 1564 | io_request->EEDPFlags = 0; |
| 1565 | io_request->Control = 0; |
| 1566 | io_request->EEDPBlockSize = 0; |
adam radford | 36807e6 | 2011-10-08 18:15:06 -0700 | [diff] [blame] | 1567 | io_request->ChainOffset = 0; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1568 | io_request->RaidContext.RAIDFlags = 0; |
adam radford | 36807e6 | 2011-10-08 18:15:06 -0700 | [diff] [blame] | 1569 | io_request->RaidContext.Type = 0; |
| 1570 | io_request->RaidContext.nseg = 0; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1571 | |
| 1572 | memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len); |
| 1573 | /* |
| 1574 | * Just the CDB length,rest of the Flags are zero |
| 1575 | * This will be modified for FP in build_ldio_fusion |
| 1576 | */ |
| 1577 | io_request->IoFlags = scp->cmd_len; |
| 1578 | |
| 1579 | if (megasas_is_ldio(scp)) |
| 1580 | megasas_build_ldio_fusion(instance, scp, cmd); |
| 1581 | else |
| 1582 | megasas_build_dcdb_fusion(instance, scp, cmd); |
| 1583 | |
| 1584 | /* |
| 1585 | * Construct SGL |
| 1586 | */ |
| 1587 | |
| 1588 | sge_count = |
| 1589 | megasas_make_sgl_fusion(instance, scp, |
| 1590 | (struct MPI25_IEEE_SGE_CHAIN64 *) |
| 1591 | &io_request->SGL, cmd); |
| 1592 | |
| 1593 | if (sge_count > instance->max_num_sge) { |
| 1594 | printk(KERN_ERR "megasas: Error. sge_count (0x%x) exceeds " |
| 1595 | "max (0x%x) allowed\n", sge_count, |
| 1596 | instance->max_num_sge); |
| 1597 | return 1; |
| 1598 | } |
| 1599 | |
| 1600 | io_request->RaidContext.numSGE = sge_count; |
| 1601 | |
| 1602 | io_request->SGLFlags = MPI2_SGE_FLAGS_64_BIT_ADDRESSING; |
| 1603 | |
| 1604 | if (scp->sc_data_direction == PCI_DMA_TODEVICE) |
| 1605 | io_request->Control |= MPI2_SCSIIO_CONTROL_WRITE; |
| 1606 | else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) |
| 1607 | io_request->Control |= MPI2_SCSIIO_CONTROL_READ; |
| 1608 | |
| 1609 | io_request->SGLOffset0 = |
| 1610 | offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4; |
| 1611 | |
| 1612 | io_request->SenseBufferLowAddress = cmd->sense_phys_addr; |
| 1613 | io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE; |
| 1614 | |
| 1615 | cmd->scmd = scp; |
| 1616 | scp->SCp.ptr = (char *)cmd; |
| 1617 | |
| 1618 | return 0; |
| 1619 | } |
| 1620 | |
| 1621 | union MEGASAS_REQUEST_DESCRIPTOR_UNION * |
| 1622 | megasas_get_request_descriptor(struct megasas_instance *instance, u16 index) |
| 1623 | { |
| 1624 | u8 *p; |
| 1625 | struct fusion_context *fusion; |
| 1626 | |
| 1627 | if (index >= instance->max_fw_cmds) { |
| 1628 | printk(KERN_ERR "megasas: Invalid SMID (0x%x)request for " |
| 1629 | "descriptor\n", index); |
| 1630 | return NULL; |
| 1631 | } |
| 1632 | fusion = instance->ctrl_context; |
| 1633 | p = fusion->req_frames_desc |
| 1634 | +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index; |
| 1635 | |
| 1636 | return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p; |
| 1637 | } |
| 1638 | |
| 1639 | /** |
| 1640 | * megasas_build_and_issue_cmd_fusion -Main routine for building and |
| 1641 | * issuing non IOCTL cmd |
| 1642 | * @instance: Adapter soft state |
| 1643 | * @scmd: pointer to scsi cmd from OS |
| 1644 | */ |
| 1645 | static u32 |
| 1646 | megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance, |
| 1647 | struct scsi_cmnd *scmd) |
| 1648 | { |
| 1649 | struct megasas_cmd_fusion *cmd; |
| 1650 | union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc; |
| 1651 | u32 index; |
| 1652 | struct fusion_context *fusion; |
| 1653 | |
| 1654 | fusion = instance->ctrl_context; |
| 1655 | |
| 1656 | cmd = megasas_get_cmd_fusion(instance); |
| 1657 | if (!cmd) |
| 1658 | return SCSI_MLQUEUE_HOST_BUSY; |
| 1659 | |
| 1660 | index = cmd->index; |
| 1661 | |
| 1662 | req_desc = megasas_get_request_descriptor(instance, index-1); |
| 1663 | if (!req_desc) |
| 1664 | return 1; |
| 1665 | |
| 1666 | req_desc->Words = 0; |
| 1667 | cmd->request_desc = req_desc; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1668 | |
| 1669 | if (megasas_build_io_fusion(instance, scmd, cmd)) { |
| 1670 | megasas_return_cmd_fusion(instance, cmd); |
| 1671 | printk(KERN_ERR "megasas: Error building command.\n"); |
| 1672 | cmd->request_desc = NULL; |
| 1673 | return 1; |
| 1674 | } |
| 1675 | |
| 1676 | req_desc = cmd->request_desc; |
| 1677 | req_desc->SCSIIO.SMID = index; |
| 1678 | |
| 1679 | if (cmd->io_request->ChainOffset != 0 && |
| 1680 | cmd->io_request->ChainOffset != 0xF) |
| 1681 | printk(KERN_ERR "megasas: The chain offset value is not " |
| 1682 | "correct : %x\n", cmd->io_request->ChainOffset); |
| 1683 | |
| 1684 | /* |
| 1685 | * Issue the command to the FW |
| 1686 | */ |
| 1687 | atomic_inc(&instance->fw_outstanding); |
| 1688 | |
| 1689 | instance->instancet->fire_cmd(instance, |
| 1690 | req_desc->u.low, req_desc->u.high, |
| 1691 | instance->reg_set); |
| 1692 | |
| 1693 | return 0; |
| 1694 | } |
| 1695 | |
| 1696 | /** |
| 1697 | * complete_cmd_fusion - Completes command |
| 1698 | * @instance: Adapter soft state |
| 1699 | * Completes all commands that is in reply descriptor queue |
| 1700 | */ |
| 1701 | int |
adam radford | c8e858f | 2011-10-08 18:15:13 -0700 | [diff] [blame] | 1702 | complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex) |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1703 | { |
| 1704 | union MPI2_REPLY_DESCRIPTORS_UNION *desc; |
| 1705 | struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc; |
| 1706 | struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req; |
| 1707 | struct fusion_context *fusion; |
| 1708 | struct megasas_cmd *cmd_mfi; |
| 1709 | struct megasas_cmd_fusion *cmd_fusion; |
| 1710 | u16 smid, num_completed; |
| 1711 | u8 reply_descript_type, arm; |
| 1712 | u32 status, extStatus, device_id; |
| 1713 | union desc_value d_val; |
| 1714 | struct LD_LOAD_BALANCE_INFO *lbinfo; |
| 1715 | |
| 1716 | fusion = instance->ctrl_context; |
| 1717 | |
| 1718 | if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) |
| 1719 | return IRQ_HANDLED; |
| 1720 | |
| 1721 | desc = fusion->reply_frames_desc; |
adam radford | c8e858f | 2011-10-08 18:15:13 -0700 | [diff] [blame] | 1722 | desc += ((MSIxIndex * fusion->reply_alloc_sz)/ |
| 1723 | sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)) + |
| 1724 | fusion->last_reply_idx[MSIxIndex]; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1725 | |
| 1726 | reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc; |
| 1727 | |
| 1728 | d_val.word = desc->Words; |
| 1729 | |
| 1730 | reply_descript_type = reply_desc->ReplyFlags & |
| 1731 | MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK; |
| 1732 | |
| 1733 | if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) |
| 1734 | return IRQ_NONE; |
| 1735 | |
| 1736 | d_val.word = desc->Words; |
| 1737 | |
| 1738 | num_completed = 0; |
| 1739 | |
| 1740 | while ((d_val.u.low != UINT_MAX) && (d_val.u.high != UINT_MAX)) { |
| 1741 | smid = reply_desc->SMID; |
| 1742 | |
| 1743 | cmd_fusion = fusion->cmd_list[smid - 1]; |
| 1744 | |
| 1745 | scsi_io_req = |
| 1746 | (struct MPI2_RAID_SCSI_IO_REQUEST *) |
| 1747 | cmd_fusion->io_request; |
| 1748 | |
| 1749 | if (cmd_fusion->scmd) |
| 1750 | cmd_fusion->scmd->SCp.ptr = NULL; |
| 1751 | |
| 1752 | status = scsi_io_req->RaidContext.status; |
| 1753 | extStatus = scsi_io_req->RaidContext.exStatus; |
| 1754 | |
| 1755 | switch (scsi_io_req->Function) { |
| 1756 | case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/ |
| 1757 | /* Update load balancing info */ |
| 1758 | device_id = MEGASAS_DEV_INDEX(instance, |
| 1759 | cmd_fusion->scmd); |
| 1760 | lbinfo = &fusion->load_balance_info[device_id]; |
| 1761 | if (cmd_fusion->scmd->SCp.Status & |
| 1762 | MEGASAS_LOAD_BALANCE_FLAG) { |
| 1763 | arm = lbinfo->raid1DevHandle[0] == |
| 1764 | cmd_fusion->io_request->DevHandle ? 0 : |
| 1765 | 1; |
| 1766 | atomic_dec(&lbinfo->scsi_pending_cmds[arm]); |
| 1767 | cmd_fusion->scmd->SCp.Status &= |
| 1768 | ~MEGASAS_LOAD_BALANCE_FLAG; |
| 1769 | } |
| 1770 | if (reply_descript_type == |
| 1771 | MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) { |
| 1772 | if (megasas_dbg_lvl == 5) |
| 1773 | printk(KERN_ERR "\nmegasas: FAST Path " |
| 1774 | "IO Success\n"); |
| 1775 | } |
| 1776 | /* Fall thru and complete IO */ |
| 1777 | case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */ |
| 1778 | /* Map the FW Cmd Status */ |
| 1779 | map_cmd_status(cmd_fusion, status, extStatus); |
| 1780 | scsi_dma_unmap(cmd_fusion->scmd); |
| 1781 | cmd_fusion->scmd->scsi_done(cmd_fusion->scmd); |
| 1782 | scsi_io_req->RaidContext.status = 0; |
| 1783 | scsi_io_req->RaidContext.exStatus = 0; |
| 1784 | megasas_return_cmd_fusion(instance, cmd_fusion); |
| 1785 | atomic_dec(&instance->fw_outstanding); |
| 1786 | |
| 1787 | break; |
| 1788 | case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */ |
| 1789 | cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx]; |
| 1790 | megasas_complete_cmd(instance, cmd_mfi, DID_OK); |
| 1791 | cmd_fusion->flags = 0; |
| 1792 | megasas_return_cmd_fusion(instance, cmd_fusion); |
| 1793 | |
| 1794 | break; |
| 1795 | } |
| 1796 | |
adam radford | c8e858f | 2011-10-08 18:15:13 -0700 | [diff] [blame] | 1797 | fusion->last_reply_idx[MSIxIndex]++; |
| 1798 | if (fusion->last_reply_idx[MSIxIndex] >= |
| 1799 | fusion->reply_q_depth) |
| 1800 | fusion->last_reply_idx[MSIxIndex] = 0; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1801 | |
| 1802 | desc->Words = ULLONG_MAX; |
| 1803 | num_completed++; |
| 1804 | |
| 1805 | /* Get the next reply descriptor */ |
adam radford | c8e858f | 2011-10-08 18:15:13 -0700 | [diff] [blame] | 1806 | if (!fusion->last_reply_idx[MSIxIndex]) |
| 1807 | desc = fusion->reply_frames_desc + |
| 1808 | ((MSIxIndex * fusion->reply_alloc_sz)/ |
| 1809 | sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)); |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1810 | else |
| 1811 | desc++; |
| 1812 | |
| 1813 | reply_desc = |
| 1814 | (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc; |
| 1815 | |
| 1816 | d_val.word = desc->Words; |
| 1817 | |
| 1818 | reply_descript_type = reply_desc->ReplyFlags & |
| 1819 | MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK; |
| 1820 | |
| 1821 | if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) |
| 1822 | break; |
| 1823 | } |
| 1824 | |
| 1825 | if (!num_completed) |
| 1826 | return IRQ_NONE; |
| 1827 | |
| 1828 | wmb(); |
adam radford | c8e858f | 2011-10-08 18:15:13 -0700 | [diff] [blame] | 1829 | writel((MSIxIndex << 24) | fusion->last_reply_idx[MSIxIndex], |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1830 | &instance->reg_set->reply_post_host_index); |
adam radford | 53ef2bb | 2011-02-24 20:56:05 -0800 | [diff] [blame] | 1831 | megasas_check_and_restore_queue_depth(instance); |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1832 | return IRQ_HANDLED; |
| 1833 | } |
| 1834 | |
| 1835 | /** |
| 1836 | * megasas_complete_cmd_dpc_fusion - Completes command |
| 1837 | * @instance: Adapter soft state |
| 1838 | * |
| 1839 | * Tasklet to complete cmds |
| 1840 | */ |
| 1841 | void |
| 1842 | megasas_complete_cmd_dpc_fusion(unsigned long instance_addr) |
| 1843 | { |
| 1844 | struct megasas_instance *instance = |
| 1845 | (struct megasas_instance *)instance_addr; |
| 1846 | unsigned long flags; |
adam radford | c8e858f | 2011-10-08 18:15:13 -0700 | [diff] [blame] | 1847 | u32 count, MSIxIndex; |
| 1848 | |
| 1849 | count = instance->msix_vectors > 0 ? instance->msix_vectors : 1; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1850 | |
| 1851 | /* If we have already declared adapter dead, donot complete cmds */ |
| 1852 | spin_lock_irqsave(&instance->hba_lock, flags); |
| 1853 | if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) { |
| 1854 | spin_unlock_irqrestore(&instance->hba_lock, flags); |
| 1855 | return; |
| 1856 | } |
| 1857 | spin_unlock_irqrestore(&instance->hba_lock, flags); |
| 1858 | |
adam radford | c8e858f | 2011-10-08 18:15:13 -0700 | [diff] [blame] | 1859 | for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++) |
| 1860 | complete_cmd_fusion(instance, MSIxIndex); |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | /** |
| 1864 | * megasas_isr_fusion - isr entry point |
| 1865 | */ |
| 1866 | irqreturn_t megasas_isr_fusion(int irq, void *devp) |
| 1867 | { |
adam radford | c8e858f | 2011-10-08 18:15:13 -0700 | [diff] [blame] | 1868 | struct megasas_irq_context *irq_context = devp; |
| 1869 | struct megasas_instance *instance = irq_context->instance; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1870 | u32 mfiStatus, fw_state; |
| 1871 | |
adam radford | c8e858f | 2011-10-08 18:15:13 -0700 | [diff] [blame] | 1872 | if (!instance->msix_vectors) { |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1873 | mfiStatus = instance->instancet->clear_intr(instance->reg_set); |
| 1874 | if (!mfiStatus) |
| 1875 | return IRQ_NONE; |
| 1876 | } |
| 1877 | |
| 1878 | /* If we are resetting, bail */ |
adam radford | 6497b24 | 2011-10-08 18:14:50 -0700 | [diff] [blame] | 1879 | if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) { |
| 1880 | instance->instancet->clear_intr(instance->reg_set); |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1881 | return IRQ_HANDLED; |
adam radford | 6497b24 | 2011-10-08 18:14:50 -0700 | [diff] [blame] | 1882 | } |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1883 | |
adam radford | c8e858f | 2011-10-08 18:15:13 -0700 | [diff] [blame] | 1884 | if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) { |
adam radford | 6497b24 | 2011-10-08 18:14:50 -0700 | [diff] [blame] | 1885 | instance->instancet->clear_intr(instance->reg_set); |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1886 | /* If we didn't complete any commands, check for FW fault */ |
| 1887 | fw_state = instance->instancet->read_fw_status_reg( |
| 1888 | instance->reg_set) & MFI_STATE_MASK; |
| 1889 | if (fw_state == MFI_STATE_FAULT) |
| 1890 | schedule_work(&instance->work_init); |
| 1891 | } |
| 1892 | |
| 1893 | return IRQ_HANDLED; |
| 1894 | } |
| 1895 | |
| 1896 | /** |
| 1897 | * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru |
| 1898 | * @instance: Adapter soft state |
| 1899 | * mfi_cmd: megasas_cmd pointer |
| 1900 | * |
| 1901 | */ |
| 1902 | u8 |
| 1903 | build_mpt_mfi_pass_thru(struct megasas_instance *instance, |
| 1904 | struct megasas_cmd *mfi_cmd) |
| 1905 | { |
| 1906 | struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain; |
| 1907 | struct MPI2_RAID_SCSI_IO_REQUEST *io_req; |
| 1908 | struct megasas_cmd_fusion *cmd; |
| 1909 | struct fusion_context *fusion; |
| 1910 | struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr; |
| 1911 | |
| 1912 | cmd = megasas_get_cmd_fusion(instance); |
| 1913 | if (!cmd) |
| 1914 | return 1; |
| 1915 | |
| 1916 | /* Save the smid. To be used for returning the cmd */ |
| 1917 | mfi_cmd->context.smid = cmd->index; |
| 1918 | |
| 1919 | cmd->sync_cmd_idx = mfi_cmd->index; |
| 1920 | |
| 1921 | /* |
| 1922 | * For cmds where the flag is set, store the flag and check |
| 1923 | * on completion. For cmds with this flag, don't call |
| 1924 | * megasas_complete_cmd |
| 1925 | */ |
| 1926 | |
| 1927 | if (frame_hdr->flags & MFI_FRAME_DONT_POST_IN_REPLY_QUEUE) |
| 1928 | cmd->flags = MFI_FRAME_DONT_POST_IN_REPLY_QUEUE; |
| 1929 | |
| 1930 | fusion = instance->ctrl_context; |
| 1931 | io_req = cmd->io_request; |
adam radford | 36807e6 | 2011-10-08 18:15:06 -0700 | [diff] [blame] | 1932 | |
| 1933 | if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) { |
| 1934 | struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = |
| 1935 | (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL; |
| 1936 | sgl_ptr_end += fusion->max_sge_in_main_msg - 1; |
| 1937 | sgl_ptr_end->Flags = 0; |
| 1938 | } |
| 1939 | |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 1940 | mpi25_ieee_chain = |
| 1941 | (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain; |
| 1942 | |
| 1943 | io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST; |
| 1944 | io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, |
| 1945 | SGL) / 4; |
| 1946 | io_req->ChainOffset = fusion->chain_offset_mfi_pthru; |
| 1947 | |
| 1948 | mpi25_ieee_chain->Address = mfi_cmd->frame_phys_addr; |
| 1949 | |
| 1950 | mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT | |
| 1951 | MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR; |
| 1952 | |
| 1953 | mpi25_ieee_chain->Length = MEGASAS_MAX_SZ_CHAIN_FRAME; |
| 1954 | |
| 1955 | return 0; |
| 1956 | } |
| 1957 | |
| 1958 | /** |
| 1959 | * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd |
| 1960 | * @instance: Adapter soft state |
| 1961 | * @cmd: mfi cmd to build |
| 1962 | * |
| 1963 | */ |
| 1964 | union MEGASAS_REQUEST_DESCRIPTOR_UNION * |
| 1965 | build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd) |
| 1966 | { |
| 1967 | union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc; |
| 1968 | u16 index; |
| 1969 | |
| 1970 | if (build_mpt_mfi_pass_thru(instance, cmd)) { |
| 1971 | printk(KERN_ERR "Couldn't build MFI pass thru cmd\n"); |
| 1972 | return NULL; |
| 1973 | } |
| 1974 | |
| 1975 | index = cmd->context.smid; |
| 1976 | |
| 1977 | req_desc = megasas_get_request_descriptor(instance, index - 1); |
| 1978 | |
| 1979 | if (!req_desc) |
| 1980 | return NULL; |
| 1981 | |
| 1982 | req_desc->Words = 0; |
| 1983 | req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO << |
| 1984 | MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); |
| 1985 | |
| 1986 | req_desc->SCSIIO.SMID = index; |
| 1987 | |
| 1988 | return req_desc; |
| 1989 | } |
| 1990 | |
| 1991 | /** |
| 1992 | * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd |
| 1993 | * @instance: Adapter soft state |
| 1994 | * @cmd: mfi cmd pointer |
| 1995 | * |
| 1996 | */ |
| 1997 | void |
| 1998 | megasas_issue_dcmd_fusion(struct megasas_instance *instance, |
| 1999 | struct megasas_cmd *cmd) |
| 2000 | { |
| 2001 | union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 2002 | |
| 2003 | req_desc = build_mpt_cmd(instance, cmd); |
| 2004 | if (!req_desc) { |
| 2005 | printk(KERN_ERR "Couldn't issue MFI pass thru cmd\n"); |
| 2006 | return; |
| 2007 | } |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 2008 | instance->instancet->fire_cmd(instance, req_desc->u.low, |
| 2009 | req_desc->u.high, instance->reg_set); |
| 2010 | } |
| 2011 | |
| 2012 | /** |
| 2013 | * megasas_release_fusion - Reverses the FW initialization |
| 2014 | * @intance: Adapter soft state |
| 2015 | */ |
| 2016 | void |
| 2017 | megasas_release_fusion(struct megasas_instance *instance) |
| 2018 | { |
| 2019 | megasas_free_cmds(instance); |
| 2020 | megasas_free_cmds_fusion(instance); |
| 2021 | |
| 2022 | iounmap(instance->reg_set); |
| 2023 | |
| 2024 | pci_release_selected_regions(instance->pdev, instance->bar); |
| 2025 | } |
| 2026 | |
| 2027 | /** |
| 2028 | * megasas_read_fw_status_reg_fusion - returns the current FW status value |
| 2029 | * @regs: MFI register set |
| 2030 | */ |
| 2031 | static u32 |
| 2032 | megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs) |
| 2033 | { |
| 2034 | return readl(&(regs)->outbound_scratch_pad); |
| 2035 | } |
| 2036 | |
| 2037 | /** |
| 2038 | * megasas_adp_reset_fusion - For controller reset |
| 2039 | * @regs: MFI register set |
| 2040 | */ |
| 2041 | static int |
| 2042 | megasas_adp_reset_fusion(struct megasas_instance *instance, |
| 2043 | struct megasas_register_set __iomem *regs) |
| 2044 | { |
| 2045 | return 0; |
| 2046 | } |
| 2047 | |
| 2048 | /** |
| 2049 | * megasas_check_reset_fusion - For controller reset check |
| 2050 | * @regs: MFI register set |
| 2051 | */ |
| 2052 | static int |
| 2053 | megasas_check_reset_fusion(struct megasas_instance *instance, |
| 2054 | struct megasas_register_set __iomem *regs) |
| 2055 | { |
| 2056 | return 0; |
| 2057 | } |
| 2058 | |
| 2059 | /* This function waits for outstanding commands on fusion to complete */ |
| 2060 | int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance) |
| 2061 | { |
| 2062 | int i, outstanding, retval = 0; |
adam radford | c007b8b | 2012-07-17 18:20:24 -0700 | [diff] [blame] | 2063 | u32 fw_state; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 2064 | |
adam radford | c007b8b | 2012-07-17 18:20:24 -0700 | [diff] [blame] | 2065 | for (i = 0; i < resetwaittime; i++) { |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 2066 | /* Check if firmware is in fault state */ |
| 2067 | fw_state = instance->instancet->read_fw_status_reg( |
| 2068 | instance->reg_set) & MFI_STATE_MASK; |
| 2069 | if (fw_state == MFI_STATE_FAULT) { |
| 2070 | printk(KERN_WARNING "megasas: Found FW in FAULT state," |
| 2071 | " will reset adapter.\n"); |
| 2072 | retval = 1; |
| 2073 | goto out; |
| 2074 | } |
| 2075 | |
| 2076 | outstanding = atomic_read(&instance->fw_outstanding); |
| 2077 | if (!outstanding) |
| 2078 | goto out; |
| 2079 | |
| 2080 | if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) { |
| 2081 | printk(KERN_NOTICE "megasas: [%2d]waiting for %d " |
| 2082 | "commands to complete\n", i, outstanding); |
| 2083 | megasas_complete_cmd_dpc_fusion( |
| 2084 | (unsigned long)instance); |
| 2085 | } |
| 2086 | msleep(1000); |
| 2087 | } |
| 2088 | |
| 2089 | if (atomic_read(&instance->fw_outstanding)) { |
| 2090 | printk("megaraid_sas: pending commands remain after waiting, " |
| 2091 | "will reset adapter.\n"); |
| 2092 | retval = 1; |
| 2093 | } |
| 2094 | out: |
| 2095 | return retval; |
| 2096 | } |
| 2097 | |
| 2098 | void megasas_reset_reply_desc(struct megasas_instance *instance) |
| 2099 | { |
adam radford | c8e858f | 2011-10-08 18:15:13 -0700 | [diff] [blame] | 2100 | int i, count; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 2101 | struct fusion_context *fusion; |
| 2102 | union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc; |
| 2103 | |
| 2104 | fusion = instance->ctrl_context; |
adam radford | c8e858f | 2011-10-08 18:15:13 -0700 | [diff] [blame] | 2105 | count = instance->msix_vectors > 0 ? instance->msix_vectors : 1; |
| 2106 | for (i = 0 ; i < count ; i++) |
| 2107 | fusion->last_reply_idx[i] = 0; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 2108 | reply_desc = fusion->reply_frames_desc; |
adam radford | c8e858f | 2011-10-08 18:15:13 -0700 | [diff] [blame] | 2109 | for (i = 0 ; i < fusion->reply_q_depth * count; i++, reply_desc++) |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 2110 | reply_desc->Words = ULLONG_MAX; |
| 2111 | } |
| 2112 | |
| 2113 | /* Core fusion reset function */ |
| 2114 | int megasas_reset_fusion(struct Scsi_Host *shost) |
| 2115 | { |
| 2116 | int retval = SUCCESS, i, j, retry = 0; |
| 2117 | struct megasas_instance *instance; |
| 2118 | struct megasas_cmd_fusion *cmd_fusion; |
| 2119 | struct fusion_context *fusion; |
| 2120 | struct megasas_cmd *cmd_mfi; |
| 2121 | union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc; |
adam radford | 7e70e73 | 2011-05-11 18:34:08 -0700 | [diff] [blame] | 2122 | u32 host_diag, abs_state, status_reg, reset_adapter; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 2123 | |
| 2124 | instance = (struct megasas_instance *)shost->hostdata; |
| 2125 | fusion = instance->ctrl_context; |
| 2126 | |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 2127 | if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) { |
| 2128 | printk(KERN_WARNING "megaraid_sas: Hardware critical error, " |
| 2129 | "returning FAILED.\n"); |
adam radford | d4a759a | 2011-10-08 18:14:39 -0700 | [diff] [blame] | 2130 | return FAILED; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 2131 | } |
| 2132 | |
adam radford | 7e70e73 | 2011-05-11 18:34:08 -0700 | [diff] [blame] | 2133 | mutex_lock(&instance->reset_mutex); |
| 2134 | set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags); |
| 2135 | instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT; |
| 2136 | instance->instancet->disable_intr(instance->reg_set); |
| 2137 | msleep(1000); |
| 2138 | |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 2139 | /* First try waiting for commands to complete */ |
| 2140 | if (megasas_wait_for_outstanding_fusion(instance)) { |
| 2141 | printk(KERN_WARNING "megaraid_sas: resetting fusion " |
| 2142 | "adapter.\n"); |
| 2143 | /* Now return commands back to the OS */ |
| 2144 | for (i = 0 ; i < instance->max_fw_cmds; i++) { |
| 2145 | cmd_fusion = fusion->cmd_list[i]; |
| 2146 | if (cmd_fusion->scmd) { |
| 2147 | scsi_dma_unmap(cmd_fusion->scmd); |
| 2148 | cmd_fusion->scmd->result = (DID_RESET << 16); |
| 2149 | cmd_fusion->scmd->scsi_done(cmd_fusion->scmd); |
| 2150 | megasas_return_cmd_fusion(instance, cmd_fusion); |
| 2151 | atomic_dec(&instance->fw_outstanding); |
| 2152 | } |
| 2153 | } |
| 2154 | |
adam radford | 7e70e73 | 2011-05-11 18:34:08 -0700 | [diff] [blame] | 2155 | status_reg = instance->instancet->read_fw_status_reg( |
| 2156 | instance->reg_set); |
| 2157 | abs_state = status_reg & MFI_STATE_MASK; |
| 2158 | reset_adapter = status_reg & MFI_RESET_ADAPTER; |
| 2159 | if (instance->disableOnlineCtrlReset || |
| 2160 | (abs_state == MFI_STATE_FAULT && !reset_adapter)) { |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 2161 | /* Reset not supported, kill adapter */ |
| 2162 | printk(KERN_WARNING "megaraid_sas: Reset not supported" |
| 2163 | ", killing adapter.\n"); |
| 2164 | megaraid_sas_kill_hba(instance); |
| 2165 | instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR; |
| 2166 | retval = FAILED; |
| 2167 | goto out; |
| 2168 | } |
| 2169 | |
| 2170 | /* Now try to reset the chip */ |
| 2171 | for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) { |
| 2172 | writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, |
| 2173 | &instance->reg_set->fusion_seq_offset); |
| 2174 | writel(MPI2_WRSEQ_1ST_KEY_VALUE, |
| 2175 | &instance->reg_set->fusion_seq_offset); |
| 2176 | writel(MPI2_WRSEQ_2ND_KEY_VALUE, |
| 2177 | &instance->reg_set->fusion_seq_offset); |
| 2178 | writel(MPI2_WRSEQ_3RD_KEY_VALUE, |
| 2179 | &instance->reg_set->fusion_seq_offset); |
| 2180 | writel(MPI2_WRSEQ_4TH_KEY_VALUE, |
| 2181 | &instance->reg_set->fusion_seq_offset); |
| 2182 | writel(MPI2_WRSEQ_5TH_KEY_VALUE, |
| 2183 | &instance->reg_set->fusion_seq_offset); |
| 2184 | writel(MPI2_WRSEQ_6TH_KEY_VALUE, |
| 2185 | &instance->reg_set->fusion_seq_offset); |
| 2186 | |
| 2187 | /* Check that the diag write enable (DRWE) bit is on */ |
| 2188 | host_diag = readl(&instance->reg_set->fusion_host_diag); |
adam radford | 7e70e73 | 2011-05-11 18:34:08 -0700 | [diff] [blame] | 2189 | retry = 0; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 2190 | while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) { |
| 2191 | msleep(100); |
| 2192 | host_diag = |
| 2193 | readl(&instance->reg_set->fusion_host_diag); |
| 2194 | if (retry++ == 100) { |
| 2195 | printk(KERN_WARNING "megaraid_sas: " |
| 2196 | "Host diag unlock failed!\n"); |
| 2197 | break; |
| 2198 | } |
| 2199 | } |
| 2200 | if (!(host_diag & HOST_DIAG_WRITE_ENABLE)) |
| 2201 | continue; |
| 2202 | |
| 2203 | /* Send chip reset command */ |
| 2204 | writel(host_diag | HOST_DIAG_RESET_ADAPTER, |
| 2205 | &instance->reg_set->fusion_host_diag); |
| 2206 | msleep(3000); |
| 2207 | |
| 2208 | /* Make sure reset adapter bit is cleared */ |
| 2209 | host_diag = readl(&instance->reg_set->fusion_host_diag); |
| 2210 | retry = 0; |
| 2211 | while (host_diag & HOST_DIAG_RESET_ADAPTER) { |
| 2212 | msleep(100); |
| 2213 | host_diag = |
| 2214 | readl(&instance->reg_set->fusion_host_diag); |
| 2215 | if (retry++ == 1000) { |
| 2216 | printk(KERN_WARNING "megaraid_sas: " |
| 2217 | "Diag reset adapter never " |
| 2218 | "cleared!\n"); |
| 2219 | break; |
| 2220 | } |
| 2221 | } |
| 2222 | if (host_diag & HOST_DIAG_RESET_ADAPTER) |
| 2223 | continue; |
| 2224 | |
| 2225 | abs_state = |
| 2226 | instance->instancet->read_fw_status_reg( |
adam radford | 7e70e73 | 2011-05-11 18:34:08 -0700 | [diff] [blame] | 2227 | instance->reg_set) & MFI_STATE_MASK; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 2228 | retry = 0; |
| 2229 | |
| 2230 | while ((abs_state <= MFI_STATE_FW_INIT) && |
| 2231 | (retry++ < 1000)) { |
| 2232 | msleep(100); |
| 2233 | abs_state = |
| 2234 | instance->instancet->read_fw_status_reg( |
adam radford | 7e70e73 | 2011-05-11 18:34:08 -0700 | [diff] [blame] | 2235 | instance->reg_set) & MFI_STATE_MASK; |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 2236 | } |
| 2237 | if (abs_state <= MFI_STATE_FW_INIT) { |
| 2238 | printk(KERN_WARNING "megaraid_sas: firmware " |
| 2239 | "state < MFI_STATE_FW_INIT, state = " |
| 2240 | "0x%x\n", abs_state); |
| 2241 | continue; |
| 2242 | } |
| 2243 | |
| 2244 | /* Wait for FW to become ready */ |
adam radford | 058a8fa | 2011-10-08 18:14:27 -0700 | [diff] [blame] | 2245 | if (megasas_transition_to_ready(instance, 1)) { |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 2246 | printk(KERN_WARNING "megaraid_sas: Failed to " |
| 2247 | "transition controller to ready.\n"); |
| 2248 | continue; |
| 2249 | } |
| 2250 | |
| 2251 | megasas_reset_reply_desc(instance); |
| 2252 | if (megasas_ioc_init_fusion(instance)) { |
| 2253 | printk(KERN_WARNING "megaraid_sas: " |
| 2254 | "megasas_ioc_init_fusion() failed!\n"); |
| 2255 | continue; |
| 2256 | } |
| 2257 | |
adam radford | 3f0e58bc | 2011-10-08 18:14:59 -0700 | [diff] [blame] | 2258 | clear_bit(MEGASAS_FUSION_IN_RESET, |
| 2259 | &instance->reset_flags); |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 2260 | instance->instancet->enable_intr(instance->reg_set); |
| 2261 | instance->adprecovery = MEGASAS_HBA_OPERATIONAL; |
| 2262 | |
| 2263 | /* Re-fire management commands */ |
| 2264 | for (j = 0 ; j < instance->max_fw_cmds; j++) { |
| 2265 | cmd_fusion = fusion->cmd_list[j]; |
| 2266 | if (cmd_fusion->sync_cmd_idx != |
| 2267 | (u32)ULONG_MAX) { |
| 2268 | cmd_mfi = |
| 2269 | instance-> |
| 2270 | cmd_list[cmd_fusion->sync_cmd_idx]; |
| 2271 | if (cmd_mfi->frame->dcmd.opcode == |
| 2272 | MR_DCMD_LD_MAP_GET_INFO) { |
| 2273 | megasas_return_cmd(instance, |
| 2274 | cmd_mfi); |
| 2275 | megasas_return_cmd_fusion( |
| 2276 | instance, cmd_fusion); |
| 2277 | } else { |
| 2278 | req_desc = |
| 2279 | megasas_get_request_descriptor( |
| 2280 | instance, |
| 2281 | cmd_mfi->context.smid |
| 2282 | -1); |
| 2283 | if (!req_desc) |
| 2284 | printk(KERN_WARNING |
| 2285 | "req_desc NULL" |
| 2286 | "\n"); |
| 2287 | else { |
| 2288 | instance->instancet-> |
| 2289 | fire_cmd(instance, |
| 2290 | req_desc-> |
| 2291 | u.low, |
| 2292 | req_desc-> |
| 2293 | u.high, |
| 2294 | instance-> |
| 2295 | reg_set); |
| 2296 | } |
| 2297 | } |
| 2298 | } |
| 2299 | } |
| 2300 | |
| 2301 | /* Reset load balance info */ |
| 2302 | memset(fusion->load_balance_info, 0, |
| 2303 | sizeof(struct LD_LOAD_BALANCE_INFO) |
| 2304 | *MAX_LOGICAL_DRIVES); |
| 2305 | |
| 2306 | if (!megasas_get_map_info(instance)) |
| 2307 | megasas_sync_map_info(instance); |
| 2308 | |
| 2309 | /* Adapter reset completed successfully */ |
| 2310 | printk(KERN_WARNING "megaraid_sas: Reset " |
| 2311 | "successful.\n"); |
| 2312 | retval = SUCCESS; |
| 2313 | goto out; |
| 2314 | } |
| 2315 | /* Reset failed, kill the adapter */ |
| 2316 | printk(KERN_WARNING "megaraid_sas: Reset failed, killing " |
| 2317 | "adapter.\n"); |
| 2318 | megaraid_sas_kill_hba(instance); |
| 2319 | retval = FAILED; |
| 2320 | } else { |
adam radford | 3f0e58bc | 2011-10-08 18:14:59 -0700 | [diff] [blame] | 2321 | clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags); |
adam radford | 9c915a8 | 2010-12-21 13:34:31 -0800 | [diff] [blame] | 2322 | instance->instancet->enable_intr(instance->reg_set); |
| 2323 | instance->adprecovery = MEGASAS_HBA_OPERATIONAL; |
| 2324 | } |
| 2325 | out: |
| 2326 | clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags); |
| 2327 | mutex_unlock(&instance->reset_mutex); |
| 2328 | return retval; |
| 2329 | } |
| 2330 | |
| 2331 | /* Fusion OCR work queue */ |
| 2332 | void megasas_fusion_ocr_wq(struct work_struct *work) |
| 2333 | { |
| 2334 | struct megasas_instance *instance = |
| 2335 | container_of(work, struct megasas_instance, work_init); |
| 2336 | |
| 2337 | megasas_reset_fusion(instance->host); |
| 2338 | } |
| 2339 | |
| 2340 | struct megasas_instance_template megasas_instance_template_fusion = { |
| 2341 | .fire_cmd = megasas_fire_cmd_fusion, |
| 2342 | .enable_intr = megasas_enable_intr_fusion, |
| 2343 | .disable_intr = megasas_disable_intr_fusion, |
| 2344 | .clear_intr = megasas_clear_intr_fusion, |
| 2345 | .read_fw_status_reg = megasas_read_fw_status_reg_fusion, |
| 2346 | .adp_reset = megasas_adp_reset_fusion, |
| 2347 | .check_reset = megasas_check_reset_fusion, |
| 2348 | .service_isr = megasas_isr_fusion, |
| 2349 | .tasklet = megasas_complete_cmd_dpc_fusion, |
| 2350 | .init_adapter = megasas_init_adapter_fusion, |
| 2351 | .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion, |
| 2352 | .issue_dcmd = megasas_issue_dcmd_fusion, |
| 2353 | }; |