1873e65bcSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2f0c568a4SJianyun Li /* 3f0c568a4SJianyun Li * Marvell UMI driver 4f0c568a4SJianyun Li * 5f0c568a4SJianyun Li * Copyright 2011 Marvell. <jyli@marvell.com> 6f0c568a4SJianyun Li */ 7f0c568a4SJianyun Li 8f0c568a4SJianyun Li #include <linux/kernel.h> 9f0c568a4SJianyun Li #include <linux/module.h> 10f0c568a4SJianyun Li #include <linux/moduleparam.h> 11f0c568a4SJianyun Li #include <linux/init.h> 12f0c568a4SJianyun Li #include <linux/device.h> 13f0c568a4SJianyun Li #include <linux/pci.h> 14f0c568a4SJianyun Li #include <linux/list.h> 15f0c568a4SJianyun Li #include <linux/spinlock.h> 16f0c568a4SJianyun Li #include <linux/interrupt.h> 17f0c568a4SJianyun Li #include <linux/delay.h> 1836f8ef7fSTina Ruchandani #include <linux/ktime.h> 19f0c568a4SJianyun Li #include <linux/blkdev.h> 20f0c568a4SJianyun Li #include <linux/io.h> 21f0c568a4SJianyun Li #include <scsi/scsi.h> 22f0c568a4SJianyun Li #include <scsi/scsi_cmnd.h> 23bd756ddeSShun Fu #include <scsi/scsi_device.h> 24f0c568a4SJianyun Li #include <scsi/scsi_host.h> 25f0c568a4SJianyun Li #include <scsi/scsi_transport.h> 26f0c568a4SJianyun Li #include <scsi/scsi_eh.h> 27f0c568a4SJianyun Li #include <linux/uaccess.h> 28bd756ddeSShun Fu #include <linux/kthread.h> 29f0c568a4SJianyun Li 30f0c568a4SJianyun Li #include "mvumi.h" 31f0c568a4SJianyun Li 32f0c568a4SJianyun Li MODULE_LICENSE("GPL"); 33f0c568a4SJianyun Li MODULE_AUTHOR("jyli@marvell.com"); 34f0c568a4SJianyun Li MODULE_DESCRIPTION("Marvell UMI Driver"); 35f0c568a4SJianyun Li 369baa3c34SBenoit Taine static const struct pci_device_id mvumi_pci_table[] = { 37c85bcadcSMyron Stowe { PCI_DEVICE(PCI_VENDOR_ID_MARVELL_EXT, PCI_DEVICE_ID_MARVELL_MV9143) }, 38c85bcadcSMyron Stowe { PCI_DEVICE(PCI_VENDOR_ID_MARVELL_EXT, PCI_DEVICE_ID_MARVELL_MV9580) }, 39f0c568a4SJianyun Li { 0 } 40f0c568a4SJianyun Li }; 41f0c568a4SJianyun Li 42f0c568a4SJianyun Li MODULE_DEVICE_TABLE(pci, mvumi_pci_table); 43f0c568a4SJianyun Li 44f0c568a4SJianyun Li static void tag_init(struct mvumi_tag *st, unsigned short size) 45f0c568a4SJianyun Li { 46f0c568a4SJianyun Li unsigned short i; 47f0c568a4SJianyun Li BUG_ON(size != st->size); 48f0c568a4SJianyun Li st->top = size; 49f0c568a4SJianyun Li for (i = 0; i < size; i++) 50f0c568a4SJianyun Li st->stack[i] = size - 1 - i; 51f0c568a4SJianyun Li } 52f0c568a4SJianyun Li 53f0c568a4SJianyun Li static unsigned short tag_get_one(struct mvumi_hba *mhba, struct mvumi_tag *st) 54f0c568a4SJianyun Li { 55f0c568a4SJianyun Li BUG_ON(st->top <= 0); 56f0c568a4SJianyun Li return st->stack[--st->top]; 57f0c568a4SJianyun Li } 58f0c568a4SJianyun Li 59f0c568a4SJianyun Li static void tag_release_one(struct mvumi_hba *mhba, struct mvumi_tag *st, 60f0c568a4SJianyun Li unsigned short tag) 61f0c568a4SJianyun Li { 62f0c568a4SJianyun Li BUG_ON(st->top >= st->size); 63f0c568a4SJianyun Li st->stack[st->top++] = tag; 64f0c568a4SJianyun Li } 65f0c568a4SJianyun Li 66f0c568a4SJianyun Li static bool tag_is_empty(struct mvumi_tag *st) 67f0c568a4SJianyun Li { 68f0c568a4SJianyun Li if (st->top == 0) 6959f90f5eSJiapeng Chong return true; 70f0c568a4SJianyun Li else 7159f90f5eSJiapeng Chong return false; 72f0c568a4SJianyun Li } 73f0c568a4SJianyun Li 74f0c568a4SJianyun Li static void mvumi_unmap_pci_addr(struct pci_dev *dev, void **addr_array) 75f0c568a4SJianyun Li { 76f0c568a4SJianyun Li int i; 77f0c568a4SJianyun Li 78f0c568a4SJianyun Li for (i = 0; i < MAX_BASE_ADDRESS; i++) 79f0c568a4SJianyun Li if ((pci_resource_flags(dev, i) & IORESOURCE_MEM) && 80f0c568a4SJianyun Li addr_array[i]) 81f0c568a4SJianyun Li pci_iounmap(dev, addr_array[i]); 82f0c568a4SJianyun Li } 83f0c568a4SJianyun Li 84f0c568a4SJianyun Li static int mvumi_map_pci_addr(struct pci_dev *dev, void **addr_array) 85f0c568a4SJianyun Li { 86f0c568a4SJianyun Li int i; 87f0c568a4SJianyun Li 88f0c568a4SJianyun Li for (i = 0; i < MAX_BASE_ADDRESS; i++) { 89f0c568a4SJianyun Li if (pci_resource_flags(dev, i) & IORESOURCE_MEM) { 90f0c568a4SJianyun Li addr_array[i] = pci_iomap(dev, i, 0); 91f0c568a4SJianyun Li if (!addr_array[i]) { 92f0c568a4SJianyun Li dev_err(&dev->dev, "failed to map Bar[%d]\n", 93f0c568a4SJianyun Li i); 94f0c568a4SJianyun Li mvumi_unmap_pci_addr(dev, addr_array); 95f0c568a4SJianyun Li return -ENOMEM; 96f0c568a4SJianyun Li } 97f0c568a4SJianyun Li } else 98f0c568a4SJianyun Li addr_array[i] = NULL; 99f0c568a4SJianyun Li 100f0c568a4SJianyun Li dev_dbg(&dev->dev, "Bar %d : %p.\n", i, addr_array[i]); 101f0c568a4SJianyun Li } 102f0c568a4SJianyun Li 103f0c568a4SJianyun Li return 0; 104f0c568a4SJianyun Li } 105f0c568a4SJianyun Li 106f0c568a4SJianyun Li static struct mvumi_res *mvumi_alloc_mem_resource(struct mvumi_hba *mhba, 107f0c568a4SJianyun Li enum resource_type type, unsigned int size) 108f0c568a4SJianyun Li { 109bd756ddeSShun Fu struct mvumi_res *res = kzalloc(sizeof(*res), GFP_ATOMIC); 110f0c568a4SJianyun Li 111f0c568a4SJianyun Li if (!res) { 112f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, 11359e13d48SMasanari Iida "Failed to allocate memory for resource manager.\n"); 114f0c568a4SJianyun Li return NULL; 115f0c568a4SJianyun Li } 116f0c568a4SJianyun Li 117f0c568a4SJianyun Li switch (type) { 118f0c568a4SJianyun Li case RESOURCE_CACHED_MEMORY: 119bd756ddeSShun Fu res->virt_addr = kzalloc(size, GFP_ATOMIC); 120f0c568a4SJianyun Li if (!res->virt_addr) { 121f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, 122f0c568a4SJianyun Li "unable to allocate memory,size = %d.\n", size); 123f0c568a4SJianyun Li kfree(res); 124f0c568a4SJianyun Li return NULL; 125f0c568a4SJianyun Li } 126f0c568a4SJianyun Li break; 127f0c568a4SJianyun Li 128f0c568a4SJianyun Li case RESOURCE_UNCACHED_MEMORY: 129f0c568a4SJianyun Li size = round_up(size, 8); 130750afb08SLuis Chamberlain res->virt_addr = dma_alloc_coherent(&mhba->pdev->dev, size, 131750afb08SLuis Chamberlain &res->bus_addr, 132750afb08SLuis Chamberlain GFP_KERNEL); 133f0c568a4SJianyun Li if (!res->virt_addr) { 134f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, 135f0c568a4SJianyun Li "unable to allocate consistent mem," 136f0c568a4SJianyun Li "size = %d.\n", size); 137f0c568a4SJianyun Li kfree(res); 138f0c568a4SJianyun Li return NULL; 139f0c568a4SJianyun Li } 140f0c568a4SJianyun Li break; 141f0c568a4SJianyun Li 142f0c568a4SJianyun Li default: 143f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, "unknown resource type %d.\n", type); 144f0c568a4SJianyun Li kfree(res); 145f0c568a4SJianyun Li return NULL; 146f0c568a4SJianyun Li } 147f0c568a4SJianyun Li 148f0c568a4SJianyun Li res->type = type; 149f0c568a4SJianyun Li res->size = size; 150f0c568a4SJianyun Li INIT_LIST_HEAD(&res->entry); 151f0c568a4SJianyun Li list_add_tail(&res->entry, &mhba->res_list); 152f0c568a4SJianyun Li 153f0c568a4SJianyun Li return res; 154f0c568a4SJianyun Li } 155f0c568a4SJianyun Li 156f0c568a4SJianyun Li static void mvumi_release_mem_resource(struct mvumi_hba *mhba) 157f0c568a4SJianyun Li { 158f0c568a4SJianyun Li struct mvumi_res *res, *tmp; 159f0c568a4SJianyun Li 160f0c568a4SJianyun Li list_for_each_entry_safe(res, tmp, &mhba->res_list, entry) { 161f0c568a4SJianyun Li switch (res->type) { 162f0c568a4SJianyun Li case RESOURCE_UNCACHED_MEMORY: 163ab8e7f4bSChristoph Hellwig dma_free_coherent(&mhba->pdev->dev, res->size, 164f0c568a4SJianyun Li res->virt_addr, res->bus_addr); 165f0c568a4SJianyun Li break; 166f0c568a4SJianyun Li case RESOURCE_CACHED_MEMORY: 167f0c568a4SJianyun Li kfree(res->virt_addr); 168f0c568a4SJianyun Li break; 169f0c568a4SJianyun Li default: 170f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, 171f0c568a4SJianyun Li "unknown resource type %d\n", res->type); 172f0c568a4SJianyun Li break; 173f0c568a4SJianyun Li } 174f0c568a4SJianyun Li list_del(&res->entry); 175f0c568a4SJianyun Li kfree(res); 176f0c568a4SJianyun Li } 177f0c568a4SJianyun Li mhba->fw_flag &= ~MVUMI_FW_ALLOC; 178f0c568a4SJianyun Li } 179f0c568a4SJianyun Li 180f0c568a4SJianyun Li /** 181f0c568a4SJianyun Li * mvumi_make_sgl - Prepares SGL 182f0c568a4SJianyun Li * @mhba: Adapter soft state 183f0c568a4SJianyun Li * @scmd: SCSI command from the mid-layer 184f0c568a4SJianyun Li * @sgl_p: SGL to be filled in 1855ccd6265SLee Jones * @sg_count: return the number of SG elements 186f0c568a4SJianyun Li * 187f0c568a4SJianyun Li * If successful, this function returns 0. otherwise, it returns -1. 188f0c568a4SJianyun Li */ 189f0c568a4SJianyun Li static int mvumi_make_sgl(struct mvumi_hba *mhba, struct scsi_cmnd *scmd, 190f0c568a4SJianyun Li void *sgl_p, unsigned char *sg_count) 191f0c568a4SJianyun Li { 192f0c568a4SJianyun Li struct scatterlist *sg; 193f0c568a4SJianyun Li struct mvumi_sgl *m_sg = (struct mvumi_sgl *) sgl_p; 194f0c568a4SJianyun Li unsigned int i; 195f0c568a4SJianyun Li unsigned int sgnum = scsi_sg_count(scmd); 196f0c568a4SJianyun Li dma_addr_t busaddr; 197f0c568a4SJianyun Li 1983c1a30dfSMing Lei *sg_count = dma_map_sg(&mhba->pdev->dev, scsi_sglist(scmd), sgnum, 199ab8e7f4bSChristoph Hellwig scmd->sc_data_direction); 200f0c568a4SJianyun Li if (*sg_count > mhba->max_sge) { 2014bd13a07SAlexey Khoroshilov dev_err(&mhba->pdev->dev, 2024bd13a07SAlexey Khoroshilov "sg count[0x%x] is bigger than max sg[0x%x].\n", 203f0c568a4SJianyun Li *sg_count, mhba->max_sge); 2043c1a30dfSMing Lei dma_unmap_sg(&mhba->pdev->dev, scsi_sglist(scmd), sgnum, 205ab8e7f4bSChristoph Hellwig scmd->sc_data_direction); 206f0c568a4SJianyun Li return -1; 207f0c568a4SJianyun Li } 2083c1a30dfSMing Lei scsi_for_each_sg(scmd, sg, *sg_count, i) { 2093c1a30dfSMing Lei busaddr = sg_dma_address(sg); 210f0c568a4SJianyun Li m_sg->baseaddr_l = cpu_to_le32(lower_32_bits(busaddr)); 211f0c568a4SJianyun Li m_sg->baseaddr_h = cpu_to_le32(upper_32_bits(busaddr)); 212f0c568a4SJianyun Li m_sg->flags = 0; 2133c1a30dfSMing Lei sgd_setsz(mhba, m_sg, cpu_to_le32(sg_dma_len(sg))); 214f0c568a4SJianyun Li if ((i + 1) == *sg_count) 215bd756ddeSShun Fu m_sg->flags |= 1U << mhba->eot_flag; 216f0c568a4SJianyun Li 217bd756ddeSShun Fu sgd_inc(mhba, m_sg); 218f0c568a4SJianyun Li } 219f0c568a4SJianyun Li 220f0c568a4SJianyun Li return 0; 221f0c568a4SJianyun Li } 222f0c568a4SJianyun Li 223f0c568a4SJianyun Li static int mvumi_internal_cmd_sgl(struct mvumi_hba *mhba, struct mvumi_cmd *cmd, 224f0c568a4SJianyun Li unsigned int size) 225f0c568a4SJianyun Li { 226f0c568a4SJianyun Li struct mvumi_sgl *m_sg; 227f0c568a4SJianyun Li void *virt_addr; 228f0c568a4SJianyun Li dma_addr_t phy_addr; 229f0c568a4SJianyun Li 230f0c568a4SJianyun Li if (size == 0) 231f0c568a4SJianyun Li return 0; 232f0c568a4SJianyun Li 233750afb08SLuis Chamberlain virt_addr = dma_alloc_coherent(&mhba->pdev->dev, size, &phy_addr, 234ab8e7f4bSChristoph Hellwig GFP_KERNEL); 235f0c568a4SJianyun Li if (!virt_addr) 236f0c568a4SJianyun Li return -1; 237f0c568a4SJianyun Li 238f0c568a4SJianyun Li m_sg = (struct mvumi_sgl *) &cmd->frame->payload[0]; 239f0c568a4SJianyun Li cmd->frame->sg_counts = 1; 240f0c568a4SJianyun Li cmd->data_buf = virt_addr; 241f0c568a4SJianyun Li 242f0c568a4SJianyun Li m_sg->baseaddr_l = cpu_to_le32(lower_32_bits(phy_addr)); 243f0c568a4SJianyun Li m_sg->baseaddr_h = cpu_to_le32(upper_32_bits(phy_addr)); 244bd756ddeSShun Fu m_sg->flags = 1U << mhba->eot_flag; 245bd756ddeSShun Fu sgd_setsz(mhba, m_sg, cpu_to_le32(size)); 246f0c568a4SJianyun Li 247f0c568a4SJianyun Li return 0; 248f0c568a4SJianyun Li } 249f0c568a4SJianyun Li 250f0c568a4SJianyun Li static struct mvumi_cmd *mvumi_create_internal_cmd(struct mvumi_hba *mhba, 251f0c568a4SJianyun Li unsigned int buf_size) 252f0c568a4SJianyun Li { 253f0c568a4SJianyun Li struct mvumi_cmd *cmd; 254f0c568a4SJianyun Li 255f0c568a4SJianyun Li cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 256f0c568a4SJianyun Li if (!cmd) { 257f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, "failed to create a internal cmd\n"); 258f0c568a4SJianyun Li return NULL; 259f0c568a4SJianyun Li } 260f0c568a4SJianyun Li INIT_LIST_HEAD(&cmd->queue_pointer); 261f0c568a4SJianyun Li 262ab8e7f4bSChristoph Hellwig cmd->frame = dma_alloc_coherent(&mhba->pdev->dev, mhba->ib_max_size, 263ab8e7f4bSChristoph Hellwig &cmd->frame_phys, GFP_KERNEL); 264f0c568a4SJianyun Li if (!cmd->frame) { 265f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, "failed to allocate memory for FW" 266f0c568a4SJianyun Li " frame,size = %d.\n", mhba->ib_max_size); 267f0c568a4SJianyun Li kfree(cmd); 268f0c568a4SJianyun Li return NULL; 269f0c568a4SJianyun Li } 270f0c568a4SJianyun Li 271f0c568a4SJianyun Li if (buf_size) { 272f0c568a4SJianyun Li if (mvumi_internal_cmd_sgl(mhba, cmd, buf_size)) { 273f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, "failed to allocate memory" 274f0c568a4SJianyun Li " for internal frame\n"); 275ab8e7f4bSChristoph Hellwig dma_free_coherent(&mhba->pdev->dev, mhba->ib_max_size, 276bd756ddeSShun Fu cmd->frame, cmd->frame_phys); 277f0c568a4SJianyun Li kfree(cmd); 278f0c568a4SJianyun Li return NULL; 279f0c568a4SJianyun Li } 280f0c568a4SJianyun Li } else 281f0c568a4SJianyun Li cmd->frame->sg_counts = 0; 282f0c568a4SJianyun Li 283f0c568a4SJianyun Li return cmd; 284f0c568a4SJianyun Li } 285f0c568a4SJianyun Li 286f0c568a4SJianyun Li static void mvumi_delete_internal_cmd(struct mvumi_hba *mhba, 287f0c568a4SJianyun Li struct mvumi_cmd *cmd) 288f0c568a4SJianyun Li { 289f0c568a4SJianyun Li struct mvumi_sgl *m_sg; 290f0c568a4SJianyun Li unsigned int size; 291f0c568a4SJianyun Li dma_addr_t phy_addr; 292f0c568a4SJianyun Li 293f0c568a4SJianyun Li if (cmd && cmd->frame) { 294f0c568a4SJianyun Li if (cmd->frame->sg_counts) { 295f0c568a4SJianyun Li m_sg = (struct mvumi_sgl *) &cmd->frame->payload[0]; 296bd756ddeSShun Fu sgd_getsz(mhba, m_sg, size); 297f0c568a4SJianyun Li 298f0c568a4SJianyun Li phy_addr = (dma_addr_t) m_sg->baseaddr_l | 299f0c568a4SJianyun Li (dma_addr_t) ((m_sg->baseaddr_h << 16) << 16); 300f0c568a4SJianyun Li 301ab8e7f4bSChristoph Hellwig dma_free_coherent(&mhba->pdev->dev, size, cmd->data_buf, 302f0c568a4SJianyun Li phy_addr); 303f0c568a4SJianyun Li } 304ab8e7f4bSChristoph Hellwig dma_free_coherent(&mhba->pdev->dev, mhba->ib_max_size, 305bd756ddeSShun Fu cmd->frame, cmd->frame_phys); 306f0c568a4SJianyun Li kfree(cmd); 307f0c568a4SJianyun Li } 308f0c568a4SJianyun Li } 309f0c568a4SJianyun Li 310f0c568a4SJianyun Li /** 311f0c568a4SJianyun Li * mvumi_get_cmd - Get a command from the free pool 312f0c568a4SJianyun Li * @mhba: Adapter soft state 313f0c568a4SJianyun Li * 314f0c568a4SJianyun Li * Returns a free command from the pool 315f0c568a4SJianyun Li */ 316f0c568a4SJianyun Li static struct mvumi_cmd *mvumi_get_cmd(struct mvumi_hba *mhba) 317f0c568a4SJianyun Li { 318f0c568a4SJianyun Li struct mvumi_cmd *cmd = NULL; 319f0c568a4SJianyun Li 320f0c568a4SJianyun Li if (likely(!list_empty(&mhba->cmd_pool))) { 321f0c568a4SJianyun Li cmd = list_entry((&mhba->cmd_pool)->next, 322f0c568a4SJianyun Li struct mvumi_cmd, queue_pointer); 323f0c568a4SJianyun Li list_del_init(&cmd->queue_pointer); 324f0c568a4SJianyun Li } else 325f0c568a4SJianyun Li dev_warn(&mhba->pdev->dev, "command pool is empty!\n"); 326f0c568a4SJianyun Li 327f0c568a4SJianyun Li return cmd; 328f0c568a4SJianyun Li } 329f0c568a4SJianyun Li 330f0c568a4SJianyun Li /** 331f0c568a4SJianyun Li * mvumi_return_cmd - Return a cmd to free command pool 332f0c568a4SJianyun Li * @mhba: Adapter soft state 333f0c568a4SJianyun Li * @cmd: Command packet to be returned to free command pool 334f0c568a4SJianyun Li */ 335f0c568a4SJianyun Li static inline void mvumi_return_cmd(struct mvumi_hba *mhba, 336f0c568a4SJianyun Li struct mvumi_cmd *cmd) 337f0c568a4SJianyun Li { 338f0c568a4SJianyun Li cmd->scmd = NULL; 339f0c568a4SJianyun Li list_add_tail(&cmd->queue_pointer, &mhba->cmd_pool); 340f0c568a4SJianyun Li } 341f0c568a4SJianyun Li 342f0c568a4SJianyun Li /** 343f0c568a4SJianyun Li * mvumi_free_cmds - Free all the cmds in the free cmd pool 344f0c568a4SJianyun Li * @mhba: Adapter soft state 345f0c568a4SJianyun Li */ 346f0c568a4SJianyun Li static void mvumi_free_cmds(struct mvumi_hba *mhba) 347f0c568a4SJianyun Li { 348f0c568a4SJianyun Li struct mvumi_cmd *cmd; 349f0c568a4SJianyun Li 350f0c568a4SJianyun Li while (!list_empty(&mhba->cmd_pool)) { 351f0c568a4SJianyun Li cmd = list_first_entry(&mhba->cmd_pool, struct mvumi_cmd, 352f0c568a4SJianyun Li queue_pointer); 353f0c568a4SJianyun Li list_del(&cmd->queue_pointer); 354bd756ddeSShun Fu if (!(mhba->hba_capability & HS_CAPABILITY_SUPPORT_DYN_SRC)) 355f0c568a4SJianyun Li kfree(cmd->frame); 356f0c568a4SJianyun Li kfree(cmd); 357f0c568a4SJianyun Li } 358f0c568a4SJianyun Li } 359f0c568a4SJianyun Li 360f0c568a4SJianyun Li /** 361f0c568a4SJianyun Li * mvumi_alloc_cmds - Allocates the command packets 362f0c568a4SJianyun Li * @mhba: Adapter soft state 363f0c568a4SJianyun Li * 364f0c568a4SJianyun Li */ 365f0c568a4SJianyun Li static int mvumi_alloc_cmds(struct mvumi_hba *mhba) 366f0c568a4SJianyun Li { 367f0c568a4SJianyun Li int i; 368f0c568a4SJianyun Li struct mvumi_cmd *cmd; 369f0c568a4SJianyun Li 370f0c568a4SJianyun Li for (i = 0; i < mhba->max_io; i++) { 371f0c568a4SJianyun Li cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 372f0c568a4SJianyun Li if (!cmd) 373f0c568a4SJianyun Li goto err_exit; 374f0c568a4SJianyun Li 375f0c568a4SJianyun Li INIT_LIST_HEAD(&cmd->queue_pointer); 376f0c568a4SJianyun Li list_add_tail(&cmd->queue_pointer, &mhba->cmd_pool); 377bd756ddeSShun Fu if (mhba->hba_capability & HS_CAPABILITY_SUPPORT_DYN_SRC) { 378bd756ddeSShun Fu cmd->frame = mhba->ib_frame + i * mhba->ib_max_size; 379bd756ddeSShun Fu cmd->frame_phys = mhba->ib_frame_phys 380bd756ddeSShun Fu + i * mhba->ib_max_size; 381bd756ddeSShun Fu } else 382f0c568a4SJianyun Li cmd->frame = kzalloc(mhba->ib_max_size, GFP_KERNEL); 383f0c568a4SJianyun Li if (!cmd->frame) 384f0c568a4SJianyun Li goto err_exit; 385f0c568a4SJianyun Li } 386f0c568a4SJianyun Li return 0; 387f0c568a4SJianyun Li 388f0c568a4SJianyun Li err_exit: 389f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, 390f0c568a4SJianyun Li "failed to allocate memory for cmd[0x%x].\n", i); 391f0c568a4SJianyun Li while (!list_empty(&mhba->cmd_pool)) { 392f0c568a4SJianyun Li cmd = list_first_entry(&mhba->cmd_pool, struct mvumi_cmd, 393f0c568a4SJianyun Li queue_pointer); 394f0c568a4SJianyun Li list_del(&cmd->queue_pointer); 395bd756ddeSShun Fu if (!(mhba->hba_capability & HS_CAPABILITY_SUPPORT_DYN_SRC)) 396f0c568a4SJianyun Li kfree(cmd->frame); 397f0c568a4SJianyun Li kfree(cmd); 398f0c568a4SJianyun Li } 399f0c568a4SJianyun Li return -ENOMEM; 400f0c568a4SJianyun Li } 401f0c568a4SJianyun Li 402bd756ddeSShun Fu static unsigned int mvumi_check_ib_list_9143(struct mvumi_hba *mhba) 403f0c568a4SJianyun Li { 404bd756ddeSShun Fu unsigned int ib_rp_reg; 405bd756ddeSShun Fu struct mvumi_hw_regs *regs = mhba->regs; 406f0c568a4SJianyun Li 407bd756ddeSShun Fu ib_rp_reg = ioread32(mhba->regs->inb_read_pointer); 408bd756ddeSShun Fu 409bd756ddeSShun Fu if (unlikely(((ib_rp_reg & regs->cl_slot_num_mask) == 410bd756ddeSShun Fu (mhba->ib_cur_slot & regs->cl_slot_num_mask)) && 411bd756ddeSShun Fu ((ib_rp_reg & regs->cl_pointer_toggle) 412bd756ddeSShun Fu != (mhba->ib_cur_slot & regs->cl_pointer_toggle)))) { 413bd756ddeSShun Fu dev_warn(&mhba->pdev->dev, "no free slot to use.\n"); 414bd756ddeSShun Fu return 0; 415bd756ddeSShun Fu } 416f0c568a4SJianyun Li if (atomic_read(&mhba->fw_outstanding) >= mhba->max_io) { 417f0c568a4SJianyun Li dev_warn(&mhba->pdev->dev, "firmware io overflow.\n"); 418bd756ddeSShun Fu return 0; 419bd756ddeSShun Fu } else { 420bd756ddeSShun Fu return mhba->max_io - atomic_read(&mhba->fw_outstanding); 421f0c568a4SJianyun Li } 422f0c568a4SJianyun Li } 423f0c568a4SJianyun Li 424bd756ddeSShun Fu static unsigned int mvumi_check_ib_list_9580(struct mvumi_hba *mhba) 425bd756ddeSShun Fu { 426bd756ddeSShun Fu unsigned int count; 427bd756ddeSShun Fu if (atomic_read(&mhba->fw_outstanding) >= (mhba->max_io - 1)) 428bd756ddeSShun Fu return 0; 429bd756ddeSShun Fu count = ioread32(mhba->ib_shadow); 430bd756ddeSShun Fu if (count == 0xffff) 431bd756ddeSShun Fu return 0; 432bd756ddeSShun Fu return count; 433bd756ddeSShun Fu } 434bd756ddeSShun Fu 435bd756ddeSShun Fu static void mvumi_get_ib_list_entry(struct mvumi_hba *mhba, void **ib_entry) 436bd756ddeSShun Fu { 437bd756ddeSShun Fu unsigned int cur_ib_entry; 438bd756ddeSShun Fu 439bd756ddeSShun Fu cur_ib_entry = mhba->ib_cur_slot & mhba->regs->cl_slot_num_mask; 440f0c568a4SJianyun Li cur_ib_entry++; 441f0c568a4SJianyun Li if (cur_ib_entry >= mhba->list_num_io) { 442f0c568a4SJianyun Li cur_ib_entry -= mhba->list_num_io; 443bd756ddeSShun Fu mhba->ib_cur_slot ^= mhba->regs->cl_pointer_toggle; 444f0c568a4SJianyun Li } 445bd756ddeSShun Fu mhba->ib_cur_slot &= ~mhba->regs->cl_slot_num_mask; 446bd756ddeSShun Fu mhba->ib_cur_slot |= (cur_ib_entry & mhba->regs->cl_slot_num_mask); 447bd756ddeSShun Fu if (mhba->hba_capability & HS_CAPABILITY_SUPPORT_DYN_SRC) { 448bd756ddeSShun Fu *ib_entry = mhba->ib_list + cur_ib_entry * 449bd756ddeSShun Fu sizeof(struct mvumi_dyn_list_entry); 450bd756ddeSShun Fu } else { 451f0c568a4SJianyun Li *ib_entry = mhba->ib_list + cur_ib_entry * mhba->ib_max_size; 452bd756ddeSShun Fu } 453f0c568a4SJianyun Li atomic_inc(&mhba->fw_outstanding); 454f0c568a4SJianyun Li } 455f0c568a4SJianyun Li 456f0c568a4SJianyun Li static void mvumi_send_ib_list_entry(struct mvumi_hba *mhba) 457f0c568a4SJianyun Li { 458bd756ddeSShun Fu iowrite32(0xffff, mhba->ib_shadow); 459bd756ddeSShun Fu iowrite32(mhba->ib_cur_slot, mhba->regs->inb_write_pointer); 460f0c568a4SJianyun Li } 461f0c568a4SJianyun Li 462f0c568a4SJianyun Li static char mvumi_check_ob_frame(struct mvumi_hba *mhba, 463f0c568a4SJianyun Li unsigned int cur_obf, struct mvumi_rsp_frame *p_outb_frame) 464f0c568a4SJianyun Li { 465f0c568a4SJianyun Li unsigned short tag, request_id; 466f0c568a4SJianyun Li 467f0c568a4SJianyun Li udelay(1); 468f0c568a4SJianyun Li p_outb_frame = mhba->ob_list + cur_obf * mhba->ob_max_size; 469f0c568a4SJianyun Li request_id = p_outb_frame->request_id; 470f0c568a4SJianyun Li tag = p_outb_frame->tag; 471f0c568a4SJianyun Li if (tag > mhba->tag_pool.size) { 472f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, "ob frame data error\n"); 473f0c568a4SJianyun Li return -1; 474f0c568a4SJianyun Li } 475f0c568a4SJianyun Li if (mhba->tag_cmd[tag] == NULL) { 476f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, "tag[0x%x] with NO command\n", tag); 477f0c568a4SJianyun Li return -1; 478f0c568a4SJianyun Li } else if (mhba->tag_cmd[tag]->request_id != request_id && 479f0c568a4SJianyun Li mhba->request_id_enabled) { 480f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, "request ID from FW:0x%x," 481f0c568a4SJianyun Li "cmd request ID:0x%x\n", request_id, 482f0c568a4SJianyun Li mhba->tag_cmd[tag]->request_id); 483f0c568a4SJianyun Li return -1; 484f0c568a4SJianyun Li } 485f0c568a4SJianyun Li 486f0c568a4SJianyun Li return 0; 487f0c568a4SJianyun Li } 488f0c568a4SJianyun Li 489bd756ddeSShun Fu static int mvumi_check_ob_list_9143(struct mvumi_hba *mhba, 490bd756ddeSShun Fu unsigned int *cur_obf, unsigned int *assign_obf_end) 491bd756ddeSShun Fu { 492bd756ddeSShun Fu unsigned int ob_write, ob_write_shadow; 493bd756ddeSShun Fu struct mvumi_hw_regs *regs = mhba->regs; 494bd756ddeSShun Fu 495bd756ddeSShun Fu do { 496bd756ddeSShun Fu ob_write = ioread32(regs->outb_copy_pointer); 497bd756ddeSShun Fu ob_write_shadow = ioread32(mhba->ob_shadow); 498bd756ddeSShun Fu } while ((ob_write & regs->cl_slot_num_mask) != ob_write_shadow); 499bd756ddeSShun Fu 500bd756ddeSShun Fu *cur_obf = mhba->ob_cur_slot & mhba->regs->cl_slot_num_mask; 501bd756ddeSShun Fu *assign_obf_end = ob_write & mhba->regs->cl_slot_num_mask; 502bd756ddeSShun Fu 503bd756ddeSShun Fu if ((ob_write & regs->cl_pointer_toggle) != 504bd756ddeSShun Fu (mhba->ob_cur_slot & regs->cl_pointer_toggle)) { 505bd756ddeSShun Fu *assign_obf_end += mhba->list_num_io; 506bd756ddeSShun Fu } 507bd756ddeSShun Fu return 0; 508bd756ddeSShun Fu } 509bd756ddeSShun Fu 510bd756ddeSShun Fu static int mvumi_check_ob_list_9580(struct mvumi_hba *mhba, 511bd756ddeSShun Fu unsigned int *cur_obf, unsigned int *assign_obf_end) 512bd756ddeSShun Fu { 513bd756ddeSShun Fu unsigned int ob_write; 514bd756ddeSShun Fu struct mvumi_hw_regs *regs = mhba->regs; 515bd756ddeSShun Fu 516bd756ddeSShun Fu ob_write = ioread32(regs->outb_read_pointer); 517bd756ddeSShun Fu ob_write = ioread32(regs->outb_copy_pointer); 518bd756ddeSShun Fu *cur_obf = mhba->ob_cur_slot & mhba->regs->cl_slot_num_mask; 519bd756ddeSShun Fu *assign_obf_end = ob_write & mhba->regs->cl_slot_num_mask; 520bd756ddeSShun Fu if (*assign_obf_end < *cur_obf) 521bd756ddeSShun Fu *assign_obf_end += mhba->list_num_io; 522bd756ddeSShun Fu else if (*assign_obf_end == *cur_obf) 523bd756ddeSShun Fu return -1; 524bd756ddeSShun Fu return 0; 525bd756ddeSShun Fu } 526bd756ddeSShun Fu 527f0c568a4SJianyun Li static void mvumi_receive_ob_list_entry(struct mvumi_hba *mhba) 528f0c568a4SJianyun Li { 529f0c568a4SJianyun Li unsigned int cur_obf, assign_obf_end, i; 530f0c568a4SJianyun Li struct mvumi_ob_data *ob_data; 531f0c568a4SJianyun Li struct mvumi_rsp_frame *p_outb_frame; 532bd756ddeSShun Fu struct mvumi_hw_regs *regs = mhba->regs; 533f0c568a4SJianyun Li 534bd756ddeSShun Fu if (mhba->instancet->check_ob_list(mhba, &cur_obf, &assign_obf_end)) 535bd756ddeSShun Fu return; 536f0c568a4SJianyun Li 537f0c568a4SJianyun Li for (i = (assign_obf_end - cur_obf); i != 0; i--) { 538f0c568a4SJianyun Li cur_obf++; 539f0c568a4SJianyun Li if (cur_obf >= mhba->list_num_io) { 540f0c568a4SJianyun Li cur_obf -= mhba->list_num_io; 541bd756ddeSShun Fu mhba->ob_cur_slot ^= regs->cl_pointer_toggle; 542f0c568a4SJianyun Li } 543f0c568a4SJianyun Li 544f0c568a4SJianyun Li p_outb_frame = mhba->ob_list + cur_obf * mhba->ob_max_size; 545f0c568a4SJianyun Li 546f0c568a4SJianyun Li /* Copy pointer may point to entry in outbound list 547f0c568a4SJianyun Li * before entry has valid data 548f0c568a4SJianyun Li */ 549f0c568a4SJianyun Li if (unlikely(p_outb_frame->tag > mhba->tag_pool.size || 550f0c568a4SJianyun Li mhba->tag_cmd[p_outb_frame->tag] == NULL || 551f0c568a4SJianyun Li p_outb_frame->request_id != 552f0c568a4SJianyun Li mhba->tag_cmd[p_outb_frame->tag]->request_id)) 553f0c568a4SJianyun Li if (mvumi_check_ob_frame(mhba, cur_obf, p_outb_frame)) 554f0c568a4SJianyun Li continue; 555f0c568a4SJianyun Li 556f0c568a4SJianyun Li if (!list_empty(&mhba->ob_data_list)) { 557f0c568a4SJianyun Li ob_data = (struct mvumi_ob_data *) 558f0c568a4SJianyun Li list_first_entry(&mhba->ob_data_list, 559f0c568a4SJianyun Li struct mvumi_ob_data, list); 560f0c568a4SJianyun Li list_del_init(&ob_data->list); 561f0c568a4SJianyun Li } else { 562f0c568a4SJianyun Li ob_data = NULL; 563f0c568a4SJianyun Li if (cur_obf == 0) { 564f0c568a4SJianyun Li cur_obf = mhba->list_num_io - 1; 565bd756ddeSShun Fu mhba->ob_cur_slot ^= regs->cl_pointer_toggle; 566f0c568a4SJianyun Li } else 567f0c568a4SJianyun Li cur_obf -= 1; 568f0c568a4SJianyun Li break; 569f0c568a4SJianyun Li } 570f0c568a4SJianyun Li 571f0c568a4SJianyun Li memcpy(ob_data->data, p_outb_frame, mhba->ob_max_size); 572f0c568a4SJianyun Li p_outb_frame->tag = 0xff; 573f0c568a4SJianyun Li 574f0c568a4SJianyun Li list_add_tail(&ob_data->list, &mhba->free_ob_list); 575f0c568a4SJianyun Li } 576bd756ddeSShun Fu mhba->ob_cur_slot &= ~regs->cl_slot_num_mask; 577bd756ddeSShun Fu mhba->ob_cur_slot |= (cur_obf & regs->cl_slot_num_mask); 578bd756ddeSShun Fu iowrite32(mhba->ob_cur_slot, regs->outb_read_pointer); 579f0c568a4SJianyun Li } 580f0c568a4SJianyun Li 581bd756ddeSShun Fu static void mvumi_reset(struct mvumi_hba *mhba) 582f0c568a4SJianyun Li { 583bd756ddeSShun Fu struct mvumi_hw_regs *regs = mhba->regs; 584bd756ddeSShun Fu 585bd756ddeSShun Fu iowrite32(0, regs->enpointa_mask_reg); 586bd756ddeSShun Fu if (ioread32(regs->arm_to_pciea_msg1) != HANDSHAKE_DONESTATE) 587f0c568a4SJianyun Li return; 588f0c568a4SJianyun Li 589bd756ddeSShun Fu iowrite32(DRBL_SOFT_RESET, regs->pciea_to_arm_drbl_reg); 590f0c568a4SJianyun Li } 591f0c568a4SJianyun Li 592f0c568a4SJianyun Li static unsigned char mvumi_start(struct mvumi_hba *mhba); 593f0c568a4SJianyun Li 594f0c568a4SJianyun Li static int mvumi_wait_for_outstanding(struct mvumi_hba *mhba) 595f0c568a4SJianyun Li { 596f0c568a4SJianyun Li mhba->fw_state = FW_STATE_ABORT; 597bd756ddeSShun Fu mvumi_reset(mhba); 598f0c568a4SJianyun Li 599f0c568a4SJianyun Li if (mvumi_start(mhba)) 600f0c568a4SJianyun Li return FAILED; 601f0c568a4SJianyun Li else 602f0c568a4SJianyun Li return SUCCESS; 603f0c568a4SJianyun Li } 604f0c568a4SJianyun Li 605bd756ddeSShun Fu static int mvumi_wait_for_fw(struct mvumi_hba *mhba) 606bd756ddeSShun Fu { 607bd756ddeSShun Fu struct mvumi_hw_regs *regs = mhba->regs; 608bd756ddeSShun Fu u32 tmp; 609bd756ddeSShun Fu unsigned long before; 610bd756ddeSShun Fu before = jiffies; 611bd756ddeSShun Fu 612bd756ddeSShun Fu iowrite32(0, regs->enpointa_mask_reg); 613bd756ddeSShun Fu tmp = ioread32(regs->arm_to_pciea_msg1); 614bd756ddeSShun Fu while (tmp != HANDSHAKE_READYSTATE) { 615bd756ddeSShun Fu iowrite32(DRBL_MU_RESET, regs->pciea_to_arm_drbl_reg); 616bd756ddeSShun Fu if (time_after(jiffies, before + FW_MAX_DELAY * HZ)) { 617bd756ddeSShun Fu dev_err(&mhba->pdev->dev, 618bd756ddeSShun Fu "FW reset failed [0x%x].\n", tmp); 619bd756ddeSShun Fu return FAILED; 620bd756ddeSShun Fu } 621bd756ddeSShun Fu 622bd756ddeSShun Fu msleep(500); 623bd756ddeSShun Fu rmb(); 624bd756ddeSShun Fu tmp = ioread32(regs->arm_to_pciea_msg1); 625bd756ddeSShun Fu } 626bd756ddeSShun Fu 627bd756ddeSShun Fu return SUCCESS; 628bd756ddeSShun Fu } 629bd756ddeSShun Fu 630bd756ddeSShun Fu static void mvumi_backup_bar_addr(struct mvumi_hba *mhba) 631bd756ddeSShun Fu { 632bd756ddeSShun Fu unsigned char i; 633bd756ddeSShun Fu 634bd756ddeSShun Fu for (i = 0; i < MAX_BASE_ADDRESS; i++) { 635bd756ddeSShun Fu pci_read_config_dword(mhba->pdev, 0x10 + i * 4, 636bd756ddeSShun Fu &mhba->pci_base[i]); 637bd756ddeSShun Fu } 638bd756ddeSShun Fu } 639bd756ddeSShun Fu 640bd756ddeSShun Fu static void mvumi_restore_bar_addr(struct mvumi_hba *mhba) 641bd756ddeSShun Fu { 642bd756ddeSShun Fu unsigned char i; 643bd756ddeSShun Fu 644bd756ddeSShun Fu for (i = 0; i < MAX_BASE_ADDRESS; i++) { 645bd756ddeSShun Fu if (mhba->pci_base[i]) 646bd756ddeSShun Fu pci_write_config_dword(mhba->pdev, 0x10 + i * 4, 647bd756ddeSShun Fu mhba->pci_base[i]); 648bd756ddeSShun Fu } 649bd756ddeSShun Fu } 650bd756ddeSShun Fu 651ab8e7f4bSChristoph Hellwig static int mvumi_pci_set_master(struct pci_dev *pdev) 652bd756ddeSShun Fu { 653ab8e7f4bSChristoph Hellwig int ret = 0; 654ab8e7f4bSChristoph Hellwig 655bd756ddeSShun Fu pci_set_master(pdev); 656bd756ddeSShun Fu 657bd756ddeSShun Fu if (IS_DMA64) { 658ab8e7f4bSChristoph Hellwig if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) 659ab8e7f4bSChristoph Hellwig ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32)); 660bd756ddeSShun Fu } else 661ab8e7f4bSChristoph Hellwig ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32)); 662bd756ddeSShun Fu 663bd756ddeSShun Fu return ret; 664bd756ddeSShun Fu } 665bd756ddeSShun Fu 666bd756ddeSShun Fu static int mvumi_reset_host_9580(struct mvumi_hba *mhba) 667bd756ddeSShun Fu { 668bd756ddeSShun Fu mhba->fw_state = FW_STATE_ABORT; 669bd756ddeSShun Fu 670bd756ddeSShun Fu iowrite32(0, mhba->regs->reset_enable); 671bd756ddeSShun Fu iowrite32(0xf, mhba->regs->reset_request); 672bd756ddeSShun Fu 673bd756ddeSShun Fu iowrite32(0x10, mhba->regs->reset_enable); 674bd756ddeSShun Fu iowrite32(0x10, mhba->regs->reset_request); 675bd756ddeSShun Fu msleep(100); 676bd756ddeSShun Fu pci_disable_device(mhba->pdev); 677bd756ddeSShun Fu 678bd756ddeSShun Fu if (pci_enable_device(mhba->pdev)) { 679bd756ddeSShun Fu dev_err(&mhba->pdev->dev, "enable device failed\n"); 680bd756ddeSShun Fu return FAILED; 681bd756ddeSShun Fu } 682bd756ddeSShun Fu if (mvumi_pci_set_master(mhba->pdev)) { 683bd756ddeSShun Fu dev_err(&mhba->pdev->dev, "set master failed\n"); 684bd756ddeSShun Fu return FAILED; 685bd756ddeSShun Fu } 686bd756ddeSShun Fu mvumi_restore_bar_addr(mhba); 687bd756ddeSShun Fu if (mvumi_wait_for_fw(mhba) == FAILED) 688bd756ddeSShun Fu return FAILED; 689bd756ddeSShun Fu 690bd756ddeSShun Fu return mvumi_wait_for_outstanding(mhba); 691bd756ddeSShun Fu } 692bd756ddeSShun Fu 693bd756ddeSShun Fu static int mvumi_reset_host_9143(struct mvumi_hba *mhba) 694bd756ddeSShun Fu { 695bd756ddeSShun Fu return mvumi_wait_for_outstanding(mhba); 696bd756ddeSShun Fu } 697bd756ddeSShun Fu 698f0c568a4SJianyun Li static int mvumi_host_reset(struct scsi_cmnd *scmd) 699f0c568a4SJianyun Li { 700f0c568a4SJianyun Li struct mvumi_hba *mhba; 701f0c568a4SJianyun Li 702f0c568a4SJianyun Li mhba = (struct mvumi_hba *) scmd->device->host->hostdata; 703f0c568a4SJianyun Li 7047df158ceSHannes Reinecke scmd_printk(KERN_NOTICE, scmd, "RESET -%u cmd=%x retries=%x\n", 7057df158ceSHannes Reinecke scmd->request->tag, scmd->cmnd[0], scmd->retries); 706f0c568a4SJianyun Li 707bd756ddeSShun Fu return mhba->instancet->reset_host(mhba); 708f0c568a4SJianyun Li } 709f0c568a4SJianyun Li 710f0c568a4SJianyun Li static int mvumi_issue_blocked_cmd(struct mvumi_hba *mhba, 711f0c568a4SJianyun Li struct mvumi_cmd *cmd) 712f0c568a4SJianyun Li { 713f0c568a4SJianyun Li unsigned long flags; 714f0c568a4SJianyun Li 715f0c568a4SJianyun Li cmd->cmd_status = REQ_STATUS_PENDING; 716f0c568a4SJianyun Li 717f0c568a4SJianyun Li if (atomic_read(&cmd->sync_cmd)) { 718f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, 719f0c568a4SJianyun Li "last blocked cmd not finished, sync_cmd = %d\n", 720f0c568a4SJianyun Li atomic_read(&cmd->sync_cmd)); 721f0c568a4SJianyun Li BUG_ON(1); 722f0c568a4SJianyun Li return -1; 723f0c568a4SJianyun Li } 724f0c568a4SJianyun Li atomic_inc(&cmd->sync_cmd); 725f0c568a4SJianyun Li spin_lock_irqsave(mhba->shost->host_lock, flags); 726f0c568a4SJianyun Li mhba->instancet->fire_cmd(mhba, cmd); 727f0c568a4SJianyun Li spin_unlock_irqrestore(mhba->shost->host_lock, flags); 728f0c568a4SJianyun Li 729f0c568a4SJianyun Li wait_event_timeout(mhba->int_cmd_wait_q, 730f0c568a4SJianyun Li (cmd->cmd_status != REQ_STATUS_PENDING), 731f0c568a4SJianyun Li MVUMI_INTERNAL_CMD_WAIT_TIME * HZ); 732f0c568a4SJianyun Li 733f0c568a4SJianyun Li /* command timeout */ 734f0c568a4SJianyun Li if (atomic_read(&cmd->sync_cmd)) { 735f0c568a4SJianyun Li spin_lock_irqsave(mhba->shost->host_lock, flags); 736f0c568a4SJianyun Li atomic_dec(&cmd->sync_cmd); 737f0c568a4SJianyun Li if (mhba->tag_cmd[cmd->frame->tag]) { 7387512ddefSYueHaibing mhba->tag_cmd[cmd->frame->tag] = NULL; 739f0c568a4SJianyun Li dev_warn(&mhba->pdev->dev, "TIMEOUT:release tag [%d]\n", 740f0c568a4SJianyun Li cmd->frame->tag); 741f0c568a4SJianyun Li tag_release_one(mhba, &mhba->tag_pool, cmd->frame->tag); 742f0c568a4SJianyun Li } 743f0c568a4SJianyun Li if (!list_empty(&cmd->queue_pointer)) { 744f0c568a4SJianyun Li dev_warn(&mhba->pdev->dev, 745f0c568a4SJianyun Li "TIMEOUT:A internal command doesn't send!\n"); 746f0c568a4SJianyun Li list_del_init(&cmd->queue_pointer); 747f0c568a4SJianyun Li } else 748f0c568a4SJianyun Li atomic_dec(&mhba->fw_outstanding); 749f0c568a4SJianyun Li 750f0c568a4SJianyun Li spin_unlock_irqrestore(mhba->shost->host_lock, flags); 751f0c568a4SJianyun Li } 752f0c568a4SJianyun Li return 0; 753f0c568a4SJianyun Li } 754f0c568a4SJianyun Li 755f0c568a4SJianyun Li static void mvumi_release_fw(struct mvumi_hba *mhba) 756f0c568a4SJianyun Li { 757f0c568a4SJianyun Li mvumi_free_cmds(mhba); 758f0c568a4SJianyun Li mvumi_release_mem_resource(mhba); 759f0c568a4SJianyun Li mvumi_unmap_pci_addr(mhba->pdev, mhba->base_addr); 760ab8e7f4bSChristoph Hellwig dma_free_coherent(&mhba->pdev->dev, HSP_MAX_SIZE, 761bd756ddeSShun Fu mhba->handshake_page, mhba->handshake_page_phys); 762bd756ddeSShun Fu kfree(mhba->regs); 763f0c568a4SJianyun Li pci_release_regions(mhba->pdev); 764f0c568a4SJianyun Li } 765f0c568a4SJianyun Li 766f0c568a4SJianyun Li static unsigned char mvumi_flush_cache(struct mvumi_hba *mhba) 767f0c568a4SJianyun Li { 768f0c568a4SJianyun Li struct mvumi_cmd *cmd; 769f0c568a4SJianyun Li struct mvumi_msg_frame *frame; 770f0c568a4SJianyun Li unsigned char device_id, retry = 0; 771f0c568a4SJianyun Li unsigned char bitcount = sizeof(unsigned char) * 8; 772f0c568a4SJianyun Li 773f0c568a4SJianyun Li for (device_id = 0; device_id < mhba->max_target_id; device_id++) { 774f0c568a4SJianyun Li if (!(mhba->target_map[device_id / bitcount] & 775f0c568a4SJianyun Li (1 << (device_id % bitcount)))) 776f0c568a4SJianyun Li continue; 777f0c568a4SJianyun Li get_cmd: cmd = mvumi_create_internal_cmd(mhba, 0); 778f0c568a4SJianyun Li if (!cmd) { 779f0c568a4SJianyun Li if (retry++ >= 5) { 780f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, "failed to get memory" 781f0c568a4SJianyun Li " for internal flush cache cmd for " 782f0c568a4SJianyun Li "device %d", device_id); 783f0c568a4SJianyun Li retry = 0; 784f0c568a4SJianyun Li continue; 785f0c568a4SJianyun Li } else 786f0c568a4SJianyun Li goto get_cmd; 787f0c568a4SJianyun Li } 788f0c568a4SJianyun Li cmd->scmd = NULL; 789f0c568a4SJianyun Li cmd->cmd_status = REQ_STATUS_PENDING; 790f0c568a4SJianyun Li atomic_set(&cmd->sync_cmd, 0); 791f0c568a4SJianyun Li frame = cmd->frame; 792f0c568a4SJianyun Li frame->req_function = CL_FUN_SCSI_CMD; 793f0c568a4SJianyun Li frame->device_id = device_id; 794f0c568a4SJianyun Li frame->cmd_flag = CMD_FLAG_NON_DATA; 795f0c568a4SJianyun Li frame->data_transfer_length = 0; 796f0c568a4SJianyun Li frame->cdb_length = MAX_COMMAND_SIZE; 797f0c568a4SJianyun Li memset(frame->cdb, 0, MAX_COMMAND_SIZE); 798f0c568a4SJianyun Li frame->cdb[0] = SCSI_CMD_MARVELL_SPECIFIC; 799bd756ddeSShun Fu frame->cdb[1] = CDB_CORE_MODULE; 800f0c568a4SJianyun Li frame->cdb[2] = CDB_CORE_SHUTDOWN; 801f0c568a4SJianyun Li 802f0c568a4SJianyun Li mvumi_issue_blocked_cmd(mhba, cmd); 803f0c568a4SJianyun Li if (cmd->cmd_status != SAM_STAT_GOOD) { 804f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, 805f0c568a4SJianyun Li "device %d flush cache failed, status=0x%x.\n", 806f0c568a4SJianyun Li device_id, cmd->cmd_status); 807f0c568a4SJianyun Li } 808f0c568a4SJianyun Li 809f0c568a4SJianyun Li mvumi_delete_internal_cmd(mhba, cmd); 810f0c568a4SJianyun Li } 811f0c568a4SJianyun Li return 0; 812f0c568a4SJianyun Li } 813f0c568a4SJianyun Li 814f0c568a4SJianyun Li static unsigned char 815f0c568a4SJianyun Li mvumi_calculate_checksum(struct mvumi_hs_header *p_header, 816f0c568a4SJianyun Li unsigned short len) 817f0c568a4SJianyun Li { 818f0c568a4SJianyun Li unsigned char *ptr; 819f0c568a4SJianyun Li unsigned char ret = 0, i; 820f0c568a4SJianyun Li 821f0c568a4SJianyun Li ptr = (unsigned char *) p_header->frame_content; 822f0c568a4SJianyun Li for (i = 0; i < len; i++) { 823f0c568a4SJianyun Li ret ^= *ptr; 824f0c568a4SJianyun Li ptr++; 825f0c568a4SJianyun Li } 826f0c568a4SJianyun Li 827f0c568a4SJianyun Li return ret; 828f0c568a4SJianyun Li } 829f0c568a4SJianyun Li 830bd756ddeSShun Fu static void mvumi_hs_build_page(struct mvumi_hba *mhba, 831f0c568a4SJianyun Li struct mvumi_hs_header *hs_header) 832f0c568a4SJianyun Li { 833f0c568a4SJianyun Li struct mvumi_hs_page2 *hs_page2; 834f0c568a4SJianyun Li struct mvumi_hs_page4 *hs_page4; 835f0c568a4SJianyun Li struct mvumi_hs_page3 *hs_page3; 83636f8ef7fSTina Ruchandani u64 time; 83736f8ef7fSTina Ruchandani u64 local_time; 838f0c568a4SJianyun Li 839f0c568a4SJianyun Li switch (hs_header->page_code) { 840f0c568a4SJianyun Li case HS_PAGE_HOST_INFO: 841f0c568a4SJianyun Li hs_page2 = (struct mvumi_hs_page2 *) hs_header; 842f0c568a4SJianyun Li hs_header->frame_length = sizeof(*hs_page2) - 4; 843f0c568a4SJianyun Li memset(hs_header->frame_content, 0, hs_header->frame_length); 844f0c568a4SJianyun Li hs_page2->host_type = 3; /* 3 mean linux*/ 845bd756ddeSShun Fu if (mhba->hba_capability & HS_CAPABILITY_SUPPORT_DYN_SRC) 846bd756ddeSShun Fu hs_page2->host_cap = 0x08;/* host dynamic source mode */ 847f0c568a4SJianyun Li hs_page2->host_ver.ver_major = VER_MAJOR; 848f0c568a4SJianyun Li hs_page2->host_ver.ver_minor = VER_MINOR; 849f0c568a4SJianyun Li hs_page2->host_ver.ver_oem = VER_OEM; 850f0c568a4SJianyun Li hs_page2->host_ver.ver_build = VER_BUILD; 851f0c568a4SJianyun Li hs_page2->system_io_bus = 0; 852f0c568a4SJianyun Li hs_page2->slot_number = 0; 853f0c568a4SJianyun Li hs_page2->intr_level = 0; 854f0c568a4SJianyun Li hs_page2->intr_vector = 0; 85536f8ef7fSTina Ruchandani time = ktime_get_real_seconds(); 85636f8ef7fSTina Ruchandani local_time = (time - (sys_tz.tz_minuteswest * 60)); 857f0c568a4SJianyun Li hs_page2->seconds_since1970 = local_time; 858f0c568a4SJianyun Li hs_header->checksum = mvumi_calculate_checksum(hs_header, 859f0c568a4SJianyun Li hs_header->frame_length); 860f0c568a4SJianyun Li break; 861f0c568a4SJianyun Li 862f0c568a4SJianyun Li case HS_PAGE_FIRM_CTL: 863f0c568a4SJianyun Li hs_page3 = (struct mvumi_hs_page3 *) hs_header; 864f0c568a4SJianyun Li hs_header->frame_length = sizeof(*hs_page3) - 4; 865f0c568a4SJianyun Li memset(hs_header->frame_content, 0, hs_header->frame_length); 866f0c568a4SJianyun Li hs_header->checksum = mvumi_calculate_checksum(hs_header, 867f0c568a4SJianyun Li hs_header->frame_length); 868f0c568a4SJianyun Li break; 869f0c568a4SJianyun Li 870f0c568a4SJianyun Li case HS_PAGE_CL_INFO: 871f0c568a4SJianyun Li hs_page4 = (struct mvumi_hs_page4 *) hs_header; 872f0c568a4SJianyun Li hs_header->frame_length = sizeof(*hs_page4) - 4; 873f0c568a4SJianyun Li memset(hs_header->frame_content, 0, hs_header->frame_length); 874f0c568a4SJianyun Li hs_page4->ib_baseaddr_l = lower_32_bits(mhba->ib_list_phys); 875f0c568a4SJianyun Li hs_page4->ib_baseaddr_h = upper_32_bits(mhba->ib_list_phys); 876f0c568a4SJianyun Li 877f0c568a4SJianyun Li hs_page4->ob_baseaddr_l = lower_32_bits(mhba->ob_list_phys); 878f0c568a4SJianyun Li hs_page4->ob_baseaddr_h = upper_32_bits(mhba->ob_list_phys); 879f0c568a4SJianyun Li hs_page4->ib_entry_size = mhba->ib_max_size_setting; 880f0c568a4SJianyun Li hs_page4->ob_entry_size = mhba->ob_max_size_setting; 881bd756ddeSShun Fu if (mhba->hba_capability 882bd756ddeSShun Fu & HS_CAPABILITY_NEW_PAGE_IO_DEPTH_DEF) { 883bd756ddeSShun Fu hs_page4->ob_depth = find_first_bit((unsigned long *) 884bd756ddeSShun Fu &mhba->list_num_io, 885bd756ddeSShun Fu BITS_PER_LONG); 886bd756ddeSShun Fu hs_page4->ib_depth = find_first_bit((unsigned long *) 887bd756ddeSShun Fu &mhba->list_num_io, 888bd756ddeSShun Fu BITS_PER_LONG); 889bd756ddeSShun Fu } else { 890bd756ddeSShun Fu hs_page4->ob_depth = (u8) mhba->list_num_io; 891bd756ddeSShun Fu hs_page4->ib_depth = (u8) mhba->list_num_io; 892bd756ddeSShun Fu } 893f0c568a4SJianyun Li hs_header->checksum = mvumi_calculate_checksum(hs_header, 894f0c568a4SJianyun Li hs_header->frame_length); 895f0c568a4SJianyun Li break; 896f0c568a4SJianyun Li 897f0c568a4SJianyun Li default: 898f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, "cannot build page, code[0x%x]\n", 899f0c568a4SJianyun Li hs_header->page_code); 900f0c568a4SJianyun Li break; 901f0c568a4SJianyun Li } 902f0c568a4SJianyun Li } 903f0c568a4SJianyun Li 904f0c568a4SJianyun Li /** 905f0c568a4SJianyun Li * mvumi_init_data - Initialize requested date for FW 906f0c568a4SJianyun Li * @mhba: Adapter soft state 907f0c568a4SJianyun Li */ 908f0c568a4SJianyun Li static int mvumi_init_data(struct mvumi_hba *mhba) 909f0c568a4SJianyun Li { 910f0c568a4SJianyun Li struct mvumi_ob_data *ob_pool; 911f0c568a4SJianyun Li struct mvumi_res *res_mgnt; 912f0c568a4SJianyun Li unsigned int tmp_size, offset, i; 913f0c568a4SJianyun Li void *virmem, *v; 914f0c568a4SJianyun Li dma_addr_t p; 915f0c568a4SJianyun Li 916f0c568a4SJianyun Li if (mhba->fw_flag & MVUMI_FW_ALLOC) 917f0c568a4SJianyun Li return 0; 918f0c568a4SJianyun Li 919f0c568a4SJianyun Li tmp_size = mhba->ib_max_size * mhba->max_io; 920bd756ddeSShun Fu if (mhba->hba_capability & HS_CAPABILITY_SUPPORT_DYN_SRC) 921bd756ddeSShun Fu tmp_size += sizeof(struct mvumi_dyn_list_entry) * mhba->max_io; 922bd756ddeSShun Fu 923f0c568a4SJianyun Li tmp_size += 128 + mhba->ob_max_size * mhba->max_io; 924bd756ddeSShun Fu tmp_size += 8 + sizeof(u32)*2 + 16; 925f0c568a4SJianyun Li 926f0c568a4SJianyun Li res_mgnt = mvumi_alloc_mem_resource(mhba, 927f0c568a4SJianyun Li RESOURCE_UNCACHED_MEMORY, tmp_size); 928f0c568a4SJianyun Li if (!res_mgnt) { 929f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, 930f0c568a4SJianyun Li "failed to allocate memory for inbound list\n"); 931f0c568a4SJianyun Li goto fail_alloc_dma_buf; 932f0c568a4SJianyun Li } 933f0c568a4SJianyun Li 934f0c568a4SJianyun Li p = res_mgnt->bus_addr; 935f0c568a4SJianyun Li v = res_mgnt->virt_addr; 936f0c568a4SJianyun Li /* ib_list */ 937f0c568a4SJianyun Li offset = round_up(p, 128) - p; 938f0c568a4SJianyun Li p += offset; 939f0c568a4SJianyun Li v += offset; 940f0c568a4SJianyun Li mhba->ib_list = v; 941f0c568a4SJianyun Li mhba->ib_list_phys = p; 942bd756ddeSShun Fu if (mhba->hba_capability & HS_CAPABILITY_SUPPORT_DYN_SRC) { 943bd756ddeSShun Fu v += sizeof(struct mvumi_dyn_list_entry) * mhba->max_io; 944bd756ddeSShun Fu p += sizeof(struct mvumi_dyn_list_entry) * mhba->max_io; 945bd756ddeSShun Fu mhba->ib_frame = v; 946bd756ddeSShun Fu mhba->ib_frame_phys = p; 947bd756ddeSShun Fu } 948f0c568a4SJianyun Li v += mhba->ib_max_size * mhba->max_io; 949f0c568a4SJianyun Li p += mhba->ib_max_size * mhba->max_io; 950bd756ddeSShun Fu 951f0c568a4SJianyun Li /* ib shadow */ 952f0c568a4SJianyun Li offset = round_up(p, 8) - p; 953f0c568a4SJianyun Li p += offset; 954f0c568a4SJianyun Li v += offset; 955f0c568a4SJianyun Li mhba->ib_shadow = v; 956f0c568a4SJianyun Li mhba->ib_shadow_phys = p; 957bd756ddeSShun Fu p += sizeof(u32)*2; 958bd756ddeSShun Fu v += sizeof(u32)*2; 959f0c568a4SJianyun Li /* ob shadow */ 960bd756ddeSShun Fu if (mhba->pdev->device == PCI_DEVICE_ID_MARVELL_MV9580) { 961f0c568a4SJianyun Li offset = round_up(p, 8) - p; 962f0c568a4SJianyun Li p += offset; 963f0c568a4SJianyun Li v += offset; 964f0c568a4SJianyun Li mhba->ob_shadow = v; 965f0c568a4SJianyun Li mhba->ob_shadow_phys = p; 966f0c568a4SJianyun Li p += 8; 967f0c568a4SJianyun Li v += 8; 968bd756ddeSShun Fu } else { 969bd756ddeSShun Fu offset = round_up(p, 4) - p; 970bd756ddeSShun Fu p += offset; 971bd756ddeSShun Fu v += offset; 972bd756ddeSShun Fu mhba->ob_shadow = v; 973bd756ddeSShun Fu mhba->ob_shadow_phys = p; 974bd756ddeSShun Fu p += 4; 975bd756ddeSShun Fu v += 4; 976bd756ddeSShun Fu } 977f0c568a4SJianyun Li 978f0c568a4SJianyun Li /* ob list */ 979f0c568a4SJianyun Li offset = round_up(p, 128) - p; 980f0c568a4SJianyun Li p += offset; 981f0c568a4SJianyun Li v += offset; 982f0c568a4SJianyun Li 983f0c568a4SJianyun Li mhba->ob_list = v; 984f0c568a4SJianyun Li mhba->ob_list_phys = p; 985f0c568a4SJianyun Li 986f0c568a4SJianyun Li /* ob data pool */ 987f0c568a4SJianyun Li tmp_size = mhba->max_io * (mhba->ob_max_size + sizeof(*ob_pool)); 988f0c568a4SJianyun Li tmp_size = round_up(tmp_size, 8); 989f0c568a4SJianyun Li 990f0c568a4SJianyun Li res_mgnt = mvumi_alloc_mem_resource(mhba, 991f0c568a4SJianyun Li RESOURCE_CACHED_MEMORY, tmp_size); 992f0c568a4SJianyun Li if (!res_mgnt) { 993f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, 994f0c568a4SJianyun Li "failed to allocate memory for outbound data buffer\n"); 995f0c568a4SJianyun Li goto fail_alloc_dma_buf; 996f0c568a4SJianyun Li } 997f0c568a4SJianyun Li virmem = res_mgnt->virt_addr; 998f0c568a4SJianyun Li 999f0c568a4SJianyun Li for (i = mhba->max_io; i != 0; i--) { 1000f0c568a4SJianyun Li ob_pool = (struct mvumi_ob_data *) virmem; 1001f0c568a4SJianyun Li list_add_tail(&ob_pool->list, &mhba->ob_data_list); 1002f0c568a4SJianyun Li virmem += mhba->ob_max_size + sizeof(*ob_pool); 1003f0c568a4SJianyun Li } 1004f0c568a4SJianyun Li 1005f0c568a4SJianyun Li tmp_size = sizeof(unsigned short) * mhba->max_io + 1006f0c568a4SJianyun Li sizeof(struct mvumi_cmd *) * mhba->max_io; 1007f0c568a4SJianyun Li tmp_size += round_up(mhba->max_target_id, sizeof(unsigned char) * 8) / 1008f0c568a4SJianyun Li (sizeof(unsigned char) * 8); 1009f0c568a4SJianyun Li 1010f0c568a4SJianyun Li res_mgnt = mvumi_alloc_mem_resource(mhba, 1011f0c568a4SJianyun Li RESOURCE_CACHED_MEMORY, tmp_size); 1012f0c568a4SJianyun Li if (!res_mgnt) { 1013f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, 1014f0c568a4SJianyun Li "failed to allocate memory for tag and target map\n"); 1015f0c568a4SJianyun Li goto fail_alloc_dma_buf; 1016f0c568a4SJianyun Li } 1017f0c568a4SJianyun Li 1018f0c568a4SJianyun Li virmem = res_mgnt->virt_addr; 1019f0c568a4SJianyun Li mhba->tag_pool.stack = virmem; 1020f0c568a4SJianyun Li mhba->tag_pool.size = mhba->max_io; 1021f0c568a4SJianyun Li tag_init(&mhba->tag_pool, mhba->max_io); 1022f0c568a4SJianyun Li virmem += sizeof(unsigned short) * mhba->max_io; 1023f0c568a4SJianyun Li 1024f0c568a4SJianyun Li mhba->tag_cmd = virmem; 1025f0c568a4SJianyun Li virmem += sizeof(struct mvumi_cmd *) * mhba->max_io; 1026f0c568a4SJianyun Li 1027f0c568a4SJianyun Li mhba->target_map = virmem; 1028f0c568a4SJianyun Li 1029f0c568a4SJianyun Li mhba->fw_flag |= MVUMI_FW_ALLOC; 1030f0c568a4SJianyun Li return 0; 1031f0c568a4SJianyun Li 1032f0c568a4SJianyun Li fail_alloc_dma_buf: 1033f0c568a4SJianyun Li mvumi_release_mem_resource(mhba); 1034f0c568a4SJianyun Li return -1; 1035f0c568a4SJianyun Li } 1036f0c568a4SJianyun Li 1037f0c568a4SJianyun Li static int mvumi_hs_process_page(struct mvumi_hba *mhba, 1038f0c568a4SJianyun Li struct mvumi_hs_header *hs_header) 1039f0c568a4SJianyun Li { 1040f0c568a4SJianyun Li struct mvumi_hs_page1 *hs_page1; 1041f0c568a4SJianyun Li unsigned char page_checksum; 1042f0c568a4SJianyun Li 1043f0c568a4SJianyun Li page_checksum = mvumi_calculate_checksum(hs_header, 1044f0c568a4SJianyun Li hs_header->frame_length); 1045f0c568a4SJianyun Li if (page_checksum != hs_header->checksum) { 1046f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, "checksum error\n"); 1047f0c568a4SJianyun Li return -1; 1048f0c568a4SJianyun Li } 1049f0c568a4SJianyun Li 1050f0c568a4SJianyun Li switch (hs_header->page_code) { 1051f0c568a4SJianyun Li case HS_PAGE_FIRM_CAP: 1052f0c568a4SJianyun Li hs_page1 = (struct mvumi_hs_page1 *) hs_header; 1053f0c568a4SJianyun Li 1054f0c568a4SJianyun Li mhba->max_io = hs_page1->max_io_support; 1055f0c568a4SJianyun Li mhba->list_num_io = hs_page1->cl_inout_list_depth; 1056f0c568a4SJianyun Li mhba->max_transfer_size = hs_page1->max_transfer_size; 1057f0c568a4SJianyun Li mhba->max_target_id = hs_page1->max_devices_support; 1058f0c568a4SJianyun Li mhba->hba_capability = hs_page1->capability; 1059f0c568a4SJianyun Li mhba->ib_max_size_setting = hs_page1->cl_in_max_entry_size; 1060f0c568a4SJianyun Li mhba->ib_max_size = (1 << hs_page1->cl_in_max_entry_size) << 2; 1061f0c568a4SJianyun Li 1062f0c568a4SJianyun Li mhba->ob_max_size_setting = hs_page1->cl_out_max_entry_size; 1063f0c568a4SJianyun Li mhba->ob_max_size = (1 << hs_page1->cl_out_max_entry_size) << 2; 1064f0c568a4SJianyun Li 1065f0c568a4SJianyun Li dev_dbg(&mhba->pdev->dev, "FW version:%d\n", 1066f0c568a4SJianyun Li hs_page1->fw_ver.ver_build); 1067f0c568a4SJianyun Li 1068bd756ddeSShun Fu if (mhba->hba_capability & HS_CAPABILITY_SUPPORT_COMPACT_SG) 1069bd756ddeSShun Fu mhba->eot_flag = 22; 1070bd756ddeSShun Fu else 1071bd756ddeSShun Fu mhba->eot_flag = 27; 1072bd756ddeSShun Fu if (mhba->hba_capability & HS_CAPABILITY_NEW_PAGE_IO_DEPTH_DEF) 1073bd756ddeSShun Fu mhba->list_num_io = 1 << hs_page1->cl_inout_list_depth; 1074f0c568a4SJianyun Li break; 1075f0c568a4SJianyun Li default: 1076f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, "handshake: page code error\n"); 1077f0c568a4SJianyun Li return -1; 1078f0c568a4SJianyun Li } 1079f0c568a4SJianyun Li return 0; 1080f0c568a4SJianyun Li } 1081f0c568a4SJianyun Li 1082f0c568a4SJianyun Li /** 1083f0c568a4SJianyun Li * mvumi_handshake - Move the FW to READY state 1084f0c568a4SJianyun Li * @mhba: Adapter soft state 1085f0c568a4SJianyun Li * 1086f0c568a4SJianyun Li * During the initialization, FW passes can potentially be in any one of 1087f0c568a4SJianyun Li * several possible states. If the FW in operational, waiting-for-handshake 1088f0c568a4SJianyun Li * states, driver must take steps to bring it to ready state. Otherwise, it 1089f0c568a4SJianyun Li * has to wait for the ready state. 1090f0c568a4SJianyun Li */ 1091f0c568a4SJianyun Li static int mvumi_handshake(struct mvumi_hba *mhba) 1092f0c568a4SJianyun Li { 1093f0c568a4SJianyun Li unsigned int hs_state, tmp, hs_fun; 1094f0c568a4SJianyun Li struct mvumi_hs_header *hs_header; 1095bd756ddeSShun Fu struct mvumi_hw_regs *regs = mhba->regs; 1096f0c568a4SJianyun Li 1097f0c568a4SJianyun Li if (mhba->fw_state == FW_STATE_STARTING) 1098f0c568a4SJianyun Li hs_state = HS_S_START; 1099f0c568a4SJianyun Li else { 1100bd756ddeSShun Fu tmp = ioread32(regs->arm_to_pciea_msg0); 1101f0c568a4SJianyun Li hs_state = HS_GET_STATE(tmp); 1102f0c568a4SJianyun Li dev_dbg(&mhba->pdev->dev, "handshake state[0x%x].\n", hs_state); 1103f0c568a4SJianyun Li if (HS_GET_STATUS(tmp) != HS_STATUS_OK) { 1104f0c568a4SJianyun Li mhba->fw_state = FW_STATE_STARTING; 1105f0c568a4SJianyun Li return -1; 1106f0c568a4SJianyun Li } 1107f0c568a4SJianyun Li } 1108f0c568a4SJianyun Li 1109f0c568a4SJianyun Li hs_fun = 0; 1110f0c568a4SJianyun Li switch (hs_state) { 1111f0c568a4SJianyun Li case HS_S_START: 1112f0c568a4SJianyun Li mhba->fw_state = FW_STATE_HANDSHAKING; 1113f0c568a4SJianyun Li HS_SET_STATUS(hs_fun, HS_STATUS_OK); 1114f0c568a4SJianyun Li HS_SET_STATE(hs_fun, HS_S_RESET); 1115bd756ddeSShun Fu iowrite32(HANDSHAKE_SIGNATURE, regs->pciea_to_arm_msg1); 1116bd756ddeSShun Fu iowrite32(hs_fun, regs->pciea_to_arm_msg0); 1117bd756ddeSShun Fu iowrite32(DRBL_HANDSHAKE, regs->pciea_to_arm_drbl_reg); 1118f0c568a4SJianyun Li break; 1119f0c568a4SJianyun Li 1120f0c568a4SJianyun Li case HS_S_RESET: 1121f0c568a4SJianyun Li iowrite32(lower_32_bits(mhba->handshake_page_phys), 1122bd756ddeSShun Fu regs->pciea_to_arm_msg1); 1123f0c568a4SJianyun Li iowrite32(upper_32_bits(mhba->handshake_page_phys), 1124bd756ddeSShun Fu regs->arm_to_pciea_msg1); 1125f0c568a4SJianyun Li HS_SET_STATUS(hs_fun, HS_STATUS_OK); 1126f0c568a4SJianyun Li HS_SET_STATE(hs_fun, HS_S_PAGE_ADDR); 1127bd756ddeSShun Fu iowrite32(hs_fun, regs->pciea_to_arm_msg0); 1128bd756ddeSShun Fu iowrite32(DRBL_HANDSHAKE, regs->pciea_to_arm_drbl_reg); 1129f0c568a4SJianyun Li break; 1130f0c568a4SJianyun Li 1131f0c568a4SJianyun Li case HS_S_PAGE_ADDR: 1132f0c568a4SJianyun Li case HS_S_QUERY_PAGE: 1133f0c568a4SJianyun Li case HS_S_SEND_PAGE: 1134f0c568a4SJianyun Li hs_header = (struct mvumi_hs_header *) mhba->handshake_page; 1135f0c568a4SJianyun Li if (hs_header->page_code == HS_PAGE_FIRM_CAP) { 1136f0c568a4SJianyun Li mhba->hba_total_pages = 1137f0c568a4SJianyun Li ((struct mvumi_hs_page1 *) hs_header)->total_pages; 1138f0c568a4SJianyun Li 1139f0c568a4SJianyun Li if (mhba->hba_total_pages == 0) 1140f0c568a4SJianyun Li mhba->hba_total_pages = HS_PAGE_TOTAL-1; 1141f0c568a4SJianyun Li } 1142f0c568a4SJianyun Li 1143f0c568a4SJianyun Li if (hs_state == HS_S_QUERY_PAGE) { 1144f0c568a4SJianyun Li if (mvumi_hs_process_page(mhba, hs_header)) { 1145f0c568a4SJianyun Li HS_SET_STATE(hs_fun, HS_S_ABORT); 1146f0c568a4SJianyun Li return -1; 1147f0c568a4SJianyun Li } 1148f0c568a4SJianyun Li if (mvumi_init_data(mhba)) { 1149f0c568a4SJianyun Li HS_SET_STATE(hs_fun, HS_S_ABORT); 1150f0c568a4SJianyun Li return -1; 1151f0c568a4SJianyun Li } 1152f0c568a4SJianyun Li } else if (hs_state == HS_S_PAGE_ADDR) { 1153f0c568a4SJianyun Li hs_header->page_code = 0; 1154f0c568a4SJianyun Li mhba->hba_total_pages = HS_PAGE_TOTAL-1; 1155f0c568a4SJianyun Li } 1156f0c568a4SJianyun Li 1157f0c568a4SJianyun Li if ((hs_header->page_code + 1) <= mhba->hba_total_pages) { 1158f0c568a4SJianyun Li hs_header->page_code++; 1159f0c568a4SJianyun Li if (hs_header->page_code != HS_PAGE_FIRM_CAP) { 1160f0c568a4SJianyun Li mvumi_hs_build_page(mhba, hs_header); 1161f0c568a4SJianyun Li HS_SET_STATE(hs_fun, HS_S_SEND_PAGE); 1162f0c568a4SJianyun Li } else 1163f0c568a4SJianyun Li HS_SET_STATE(hs_fun, HS_S_QUERY_PAGE); 1164f0c568a4SJianyun Li } else 1165f0c568a4SJianyun Li HS_SET_STATE(hs_fun, HS_S_END); 1166f0c568a4SJianyun Li 1167f0c568a4SJianyun Li HS_SET_STATUS(hs_fun, HS_STATUS_OK); 1168bd756ddeSShun Fu iowrite32(hs_fun, regs->pciea_to_arm_msg0); 1169bd756ddeSShun Fu iowrite32(DRBL_HANDSHAKE, regs->pciea_to_arm_drbl_reg); 1170f0c568a4SJianyun Li break; 1171f0c568a4SJianyun Li 1172f0c568a4SJianyun Li case HS_S_END: 1173f0c568a4SJianyun Li /* Set communication list ISR */ 1174bd756ddeSShun Fu tmp = ioread32(regs->enpointa_mask_reg); 1175bd756ddeSShun Fu tmp |= regs->int_comaout | regs->int_comaerr; 1176bd756ddeSShun Fu iowrite32(tmp, regs->enpointa_mask_reg); 1177f0c568a4SJianyun Li iowrite32(mhba->list_num_io, mhba->ib_shadow); 117859e13d48SMasanari Iida /* Set InBound List Available count shadow */ 1179f0c568a4SJianyun Li iowrite32(lower_32_bits(mhba->ib_shadow_phys), 1180bd756ddeSShun Fu regs->inb_aval_count_basel); 1181f0c568a4SJianyun Li iowrite32(upper_32_bits(mhba->ib_shadow_phys), 1182bd756ddeSShun Fu regs->inb_aval_count_baseh); 1183f0c568a4SJianyun Li 1184bd756ddeSShun Fu if (mhba->pdev->device == PCI_DEVICE_ID_MARVELL_MV9143) { 118559e13d48SMasanari Iida /* Set OutBound List Available count shadow */ 1186bd756ddeSShun Fu iowrite32((mhba->list_num_io-1) | 1187bd756ddeSShun Fu regs->cl_pointer_toggle, 1188f0c568a4SJianyun Li mhba->ob_shadow); 1189bd756ddeSShun Fu iowrite32(lower_32_bits(mhba->ob_shadow_phys), 1190bd756ddeSShun Fu regs->outb_copy_basel); 1191bd756ddeSShun Fu iowrite32(upper_32_bits(mhba->ob_shadow_phys), 1192bd756ddeSShun Fu regs->outb_copy_baseh); 1193bd756ddeSShun Fu } 1194f0c568a4SJianyun Li 1195bd756ddeSShun Fu mhba->ib_cur_slot = (mhba->list_num_io - 1) | 1196bd756ddeSShun Fu regs->cl_pointer_toggle; 1197bd756ddeSShun Fu mhba->ob_cur_slot = (mhba->list_num_io - 1) | 1198bd756ddeSShun Fu regs->cl_pointer_toggle; 1199f0c568a4SJianyun Li mhba->fw_state = FW_STATE_STARTED; 1200f0c568a4SJianyun Li 1201f0c568a4SJianyun Li break; 1202f0c568a4SJianyun Li default: 1203f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, "unknown handshake state [0x%x].\n", 1204f0c568a4SJianyun Li hs_state); 1205f0c568a4SJianyun Li return -1; 1206f0c568a4SJianyun Li } 1207f0c568a4SJianyun Li return 0; 1208f0c568a4SJianyun Li } 1209f0c568a4SJianyun Li 1210f0c568a4SJianyun Li static unsigned char mvumi_handshake_event(struct mvumi_hba *mhba) 1211f0c568a4SJianyun Li { 1212f0c568a4SJianyun Li unsigned int isr_status; 1213f0c568a4SJianyun Li unsigned long before; 1214f0c568a4SJianyun Li 1215f0c568a4SJianyun Li before = jiffies; 1216f0c568a4SJianyun Li mvumi_handshake(mhba); 1217f0c568a4SJianyun Li do { 1218bd756ddeSShun Fu isr_status = mhba->instancet->read_fw_status_reg(mhba); 1219f0c568a4SJianyun Li 1220f0c568a4SJianyun Li if (mhba->fw_state == FW_STATE_STARTED) 1221f0c568a4SJianyun Li return 0; 1222f0c568a4SJianyun Li if (time_after(jiffies, before + FW_MAX_DELAY * HZ)) { 1223f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, 1224f0c568a4SJianyun Li "no handshake response at state 0x%x.\n", 1225f0c568a4SJianyun Li mhba->fw_state); 1226f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, 1227f0c568a4SJianyun Li "isr : global=0x%x,status=0x%x.\n", 1228f0c568a4SJianyun Li mhba->global_isr, isr_status); 1229f0c568a4SJianyun Li return -1; 1230f0c568a4SJianyun Li } 1231f0c568a4SJianyun Li rmb(); 1232f0c568a4SJianyun Li usleep_range(1000, 2000); 1233f0c568a4SJianyun Li } while (!(isr_status & DRBL_HANDSHAKE_ISR)); 1234f0c568a4SJianyun Li 1235f0c568a4SJianyun Li return 0; 1236f0c568a4SJianyun Li } 1237f0c568a4SJianyun Li 1238f0c568a4SJianyun Li static unsigned char mvumi_check_handshake(struct mvumi_hba *mhba) 1239f0c568a4SJianyun Li { 1240f0c568a4SJianyun Li unsigned int tmp; 1241f0c568a4SJianyun Li unsigned long before; 1242f0c568a4SJianyun Li 1243f0c568a4SJianyun Li before = jiffies; 1244bd756ddeSShun Fu tmp = ioread32(mhba->regs->arm_to_pciea_msg1); 1245f0c568a4SJianyun Li while ((tmp != HANDSHAKE_READYSTATE) && (tmp != HANDSHAKE_DONESTATE)) { 1246f0c568a4SJianyun Li if (tmp != HANDSHAKE_READYSTATE) 1247f0c568a4SJianyun Li iowrite32(DRBL_MU_RESET, 1248bd756ddeSShun Fu mhba->regs->pciea_to_arm_drbl_reg); 1249f0c568a4SJianyun Li if (time_after(jiffies, before + FW_MAX_DELAY * HZ)) { 1250f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, 1251f0c568a4SJianyun Li "invalid signature [0x%x].\n", tmp); 1252f0c568a4SJianyun Li return -1; 1253f0c568a4SJianyun Li } 1254f0c568a4SJianyun Li usleep_range(1000, 2000); 1255f0c568a4SJianyun Li rmb(); 1256bd756ddeSShun Fu tmp = ioread32(mhba->regs->arm_to_pciea_msg1); 1257f0c568a4SJianyun Li } 1258f0c568a4SJianyun Li 1259f0c568a4SJianyun Li mhba->fw_state = FW_STATE_STARTING; 1260f0c568a4SJianyun Li dev_dbg(&mhba->pdev->dev, "start firmware handshake...\n"); 1261f0c568a4SJianyun Li do { 1262f0c568a4SJianyun Li if (mvumi_handshake_event(mhba)) { 1263f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, 1264f0c568a4SJianyun Li "handshake failed at state 0x%x.\n", 1265f0c568a4SJianyun Li mhba->fw_state); 1266f0c568a4SJianyun Li return -1; 1267f0c568a4SJianyun Li } 1268f0c568a4SJianyun Li } while (mhba->fw_state != FW_STATE_STARTED); 1269f0c568a4SJianyun Li 1270f0c568a4SJianyun Li dev_dbg(&mhba->pdev->dev, "firmware handshake done\n"); 1271f0c568a4SJianyun Li 1272f0c568a4SJianyun Li return 0; 1273f0c568a4SJianyun Li } 1274f0c568a4SJianyun Li 1275f0c568a4SJianyun Li static unsigned char mvumi_start(struct mvumi_hba *mhba) 1276f0c568a4SJianyun Li { 1277f0c568a4SJianyun Li unsigned int tmp; 1278bd756ddeSShun Fu struct mvumi_hw_regs *regs = mhba->regs; 1279f0c568a4SJianyun Li 1280bd756ddeSShun Fu /* clear Door bell */ 1281bd756ddeSShun Fu tmp = ioread32(regs->arm_to_pciea_drbl_reg); 1282bd756ddeSShun Fu iowrite32(tmp, regs->arm_to_pciea_drbl_reg); 1283bd756ddeSShun Fu 1284bd756ddeSShun Fu iowrite32(regs->int_drbl_int_mask, regs->arm_to_pciea_mask_reg); 1285bd756ddeSShun Fu tmp = ioread32(regs->enpointa_mask_reg) | regs->int_dl_cpu2pciea; 1286bd756ddeSShun Fu iowrite32(tmp, regs->enpointa_mask_reg); 1287bd756ddeSShun Fu msleep(100); 1288f0c568a4SJianyun Li if (mvumi_check_handshake(mhba)) 1289f0c568a4SJianyun Li return -1; 1290f0c568a4SJianyun Li 1291f0c568a4SJianyun Li return 0; 1292f0c568a4SJianyun Li } 1293f0c568a4SJianyun Li 1294f0c568a4SJianyun Li /** 1295f0c568a4SJianyun Li * mvumi_complete_cmd - Completes a command 1296f0c568a4SJianyun Li * @mhba: Adapter soft state 1297f0c568a4SJianyun Li * @cmd: Command to be completed 12985ccd6265SLee Jones * @ob_frame: Command response 1299f0c568a4SJianyun Li */ 1300f0c568a4SJianyun Li static void mvumi_complete_cmd(struct mvumi_hba *mhba, struct mvumi_cmd *cmd, 1301f0c568a4SJianyun Li struct mvumi_rsp_frame *ob_frame) 1302f0c568a4SJianyun Li { 1303f0c568a4SJianyun Li struct scsi_cmnd *scmd = cmd->scmd; 1304f0c568a4SJianyun Li 1305f0c568a4SJianyun Li cmd->scmd->SCp.ptr = NULL; 1306f0c568a4SJianyun Li scmd->result = ob_frame->req_status; 1307f0c568a4SJianyun Li 1308f0c568a4SJianyun Li switch (ob_frame->req_status) { 1309f0c568a4SJianyun Li case SAM_STAT_GOOD: 1310f0c568a4SJianyun Li scmd->result |= DID_OK << 16; 1311f0c568a4SJianyun Li break; 1312f0c568a4SJianyun Li case SAM_STAT_BUSY: 1313f0c568a4SJianyun Li scmd->result |= DID_BUS_BUSY << 16; 1314f0c568a4SJianyun Li break; 1315f0c568a4SJianyun Li case SAM_STAT_CHECK_CONDITION: 1316f0c568a4SJianyun Li scmd->result |= (DID_OK << 16); 1317f0c568a4SJianyun Li if (ob_frame->rsp_flag & CL_RSP_FLAG_SENSEDATA) { 1318f0c568a4SJianyun Li memcpy(cmd->scmd->sense_buffer, ob_frame->payload, 1319f0c568a4SJianyun Li sizeof(struct mvumi_sense_data)); 1320f0c568a4SJianyun Li } 1321f0c568a4SJianyun Li break; 1322f0c568a4SJianyun Li default: 1323*16576ad8SHannes Reinecke scmd->result |= (DID_ABORT << 16); 1324f0c568a4SJianyun Li break; 1325f0c568a4SJianyun Li } 1326f0c568a4SJianyun Li 13274bd13a07SAlexey Khoroshilov if (scsi_bufflen(scmd)) 1328ab8e7f4bSChristoph Hellwig dma_unmap_sg(&mhba->pdev->dev, scsi_sglist(scmd), 1329f0c568a4SJianyun Li scsi_sg_count(scmd), 1330ab8e7f4bSChristoph Hellwig scmd->sc_data_direction); 1331f0c568a4SJianyun Li cmd->scmd->scsi_done(scmd); 1332f0c568a4SJianyun Li mvumi_return_cmd(mhba, cmd); 1333f0c568a4SJianyun Li } 1334bd756ddeSShun Fu 1335f0c568a4SJianyun Li static void mvumi_complete_internal_cmd(struct mvumi_hba *mhba, 1336f0c568a4SJianyun Li struct mvumi_cmd *cmd, 1337f0c568a4SJianyun Li struct mvumi_rsp_frame *ob_frame) 1338f0c568a4SJianyun Li { 1339f0c568a4SJianyun Li if (atomic_read(&cmd->sync_cmd)) { 1340f0c568a4SJianyun Li cmd->cmd_status = ob_frame->req_status; 1341f0c568a4SJianyun Li 1342f0c568a4SJianyun Li if ((ob_frame->req_status == SAM_STAT_CHECK_CONDITION) && 1343f0c568a4SJianyun Li (ob_frame->rsp_flag & CL_RSP_FLAG_SENSEDATA) && 1344f0c568a4SJianyun Li cmd->data_buf) { 1345f0c568a4SJianyun Li memcpy(cmd->data_buf, ob_frame->payload, 1346f0c568a4SJianyun Li sizeof(struct mvumi_sense_data)); 1347f0c568a4SJianyun Li } 1348f0c568a4SJianyun Li atomic_dec(&cmd->sync_cmd); 1349f0c568a4SJianyun Li wake_up(&mhba->int_cmd_wait_q); 1350f0c568a4SJianyun Li } 1351f0c568a4SJianyun Li } 1352f0c568a4SJianyun Li 1353f0c568a4SJianyun Li static void mvumi_show_event(struct mvumi_hba *mhba, 1354f0c568a4SJianyun Li struct mvumi_driver_event *ptr) 1355f0c568a4SJianyun Li { 1356f0c568a4SJianyun Li unsigned int i; 1357f0c568a4SJianyun Li 1358f0c568a4SJianyun Li dev_warn(&mhba->pdev->dev, 1359f0c568a4SJianyun Li "Event[0x%x] id[0x%x] severity[0x%x] device id[0x%x]\n", 1360f0c568a4SJianyun Li ptr->sequence_no, ptr->event_id, ptr->severity, ptr->device_id); 1361f0c568a4SJianyun Li if (ptr->param_count) { 1362f0c568a4SJianyun Li printk(KERN_WARNING "Event param(len 0x%x): ", 1363f0c568a4SJianyun Li ptr->param_count); 1364f0c568a4SJianyun Li for (i = 0; i < ptr->param_count; i++) 1365f0c568a4SJianyun Li printk(KERN_WARNING "0x%x ", ptr->params[i]); 1366f0c568a4SJianyun Li 1367f0c568a4SJianyun Li printk(KERN_WARNING "\n"); 1368f0c568a4SJianyun Li } 1369f0c568a4SJianyun Li 1370f0c568a4SJianyun Li if (ptr->sense_data_length) { 1371f0c568a4SJianyun Li printk(KERN_WARNING "Event sense data(len 0x%x): ", 1372f0c568a4SJianyun Li ptr->sense_data_length); 1373f0c568a4SJianyun Li for (i = 0; i < ptr->sense_data_length; i++) 1374f0c568a4SJianyun Li printk(KERN_WARNING "0x%x ", ptr->sense_data[i]); 1375f0c568a4SJianyun Li printk(KERN_WARNING "\n"); 1376f0c568a4SJianyun Li } 1377f0c568a4SJianyun Li } 1378f0c568a4SJianyun Li 1379bd756ddeSShun Fu static int mvumi_handle_hotplug(struct mvumi_hba *mhba, u16 devid, int status) 1380bd756ddeSShun Fu { 1381bd756ddeSShun Fu struct scsi_device *sdev; 1382bd756ddeSShun Fu int ret = -1; 1383bd756ddeSShun Fu 1384bd756ddeSShun Fu if (status == DEVICE_OFFLINE) { 1385bd756ddeSShun Fu sdev = scsi_device_lookup(mhba->shost, 0, devid, 0); 1386bd756ddeSShun Fu if (sdev) { 1387bd756ddeSShun Fu dev_dbg(&mhba->pdev->dev, "remove disk %d-%d-%d.\n", 0, 1388bd756ddeSShun Fu sdev->id, 0); 1389bd756ddeSShun Fu scsi_remove_device(sdev); 1390bd756ddeSShun Fu scsi_device_put(sdev); 1391bd756ddeSShun Fu ret = 0; 1392bd756ddeSShun Fu } else 1393bd756ddeSShun Fu dev_err(&mhba->pdev->dev, " no disk[%d] to remove\n", 1394bd756ddeSShun Fu devid); 1395bd756ddeSShun Fu } else if (status == DEVICE_ONLINE) { 1396bd756ddeSShun Fu sdev = scsi_device_lookup(mhba->shost, 0, devid, 0); 1397bd756ddeSShun Fu if (!sdev) { 1398bd756ddeSShun Fu scsi_add_device(mhba->shost, 0, devid, 0); 1399bd756ddeSShun Fu dev_dbg(&mhba->pdev->dev, " add disk %d-%d-%d.\n", 0, 1400bd756ddeSShun Fu devid, 0); 1401bd756ddeSShun Fu ret = 0; 1402bd756ddeSShun Fu } else { 1403bd756ddeSShun Fu dev_err(&mhba->pdev->dev, " don't add disk %d-%d-%d.\n", 1404bd756ddeSShun Fu 0, devid, 0); 1405bd756ddeSShun Fu scsi_device_put(sdev); 1406bd756ddeSShun Fu } 1407bd756ddeSShun Fu } 1408bd756ddeSShun Fu return ret; 1409bd756ddeSShun Fu } 1410bd756ddeSShun Fu 1411bd756ddeSShun Fu static u64 mvumi_inquiry(struct mvumi_hba *mhba, 1412bd756ddeSShun Fu unsigned int id, struct mvumi_cmd *cmd) 1413bd756ddeSShun Fu { 1414bd756ddeSShun Fu struct mvumi_msg_frame *frame; 1415bd756ddeSShun Fu u64 wwid = 0; 1416bd756ddeSShun Fu int cmd_alloc = 0; 1417bd756ddeSShun Fu int data_buf_len = 64; 1418bd756ddeSShun Fu 1419bd756ddeSShun Fu if (!cmd) { 1420bd756ddeSShun Fu cmd = mvumi_create_internal_cmd(mhba, data_buf_len); 1421bd756ddeSShun Fu if (cmd) 1422bd756ddeSShun Fu cmd_alloc = 1; 1423bd756ddeSShun Fu else 1424bd756ddeSShun Fu return 0; 1425bd756ddeSShun Fu } else { 1426bd756ddeSShun Fu memset(cmd->data_buf, 0, data_buf_len); 1427bd756ddeSShun Fu } 1428bd756ddeSShun Fu cmd->scmd = NULL; 1429bd756ddeSShun Fu cmd->cmd_status = REQ_STATUS_PENDING; 1430bd756ddeSShun Fu atomic_set(&cmd->sync_cmd, 0); 1431bd756ddeSShun Fu frame = cmd->frame; 1432bd756ddeSShun Fu frame->device_id = (u16) id; 1433bd756ddeSShun Fu frame->cmd_flag = CMD_FLAG_DATA_IN; 1434bd756ddeSShun Fu frame->req_function = CL_FUN_SCSI_CMD; 1435bd756ddeSShun Fu frame->cdb_length = 6; 1436bd756ddeSShun Fu frame->data_transfer_length = MVUMI_INQUIRY_LENGTH; 1437bd756ddeSShun Fu memset(frame->cdb, 0, frame->cdb_length); 1438bd756ddeSShun Fu frame->cdb[0] = INQUIRY; 1439bd756ddeSShun Fu frame->cdb[4] = frame->data_transfer_length; 1440bd756ddeSShun Fu 1441bd756ddeSShun Fu mvumi_issue_blocked_cmd(mhba, cmd); 1442bd756ddeSShun Fu 1443bd756ddeSShun Fu if (cmd->cmd_status == SAM_STAT_GOOD) { 1444bd756ddeSShun Fu if (mhba->pdev->device == PCI_DEVICE_ID_MARVELL_MV9143) 1445bd756ddeSShun Fu wwid = id + 1; 1446bd756ddeSShun Fu else 1447bd756ddeSShun Fu memcpy((void *)&wwid, 1448bd756ddeSShun Fu (cmd->data_buf + MVUMI_INQUIRY_UUID_OFF), 1449bd756ddeSShun Fu MVUMI_INQUIRY_UUID_LEN); 1450bd756ddeSShun Fu dev_dbg(&mhba->pdev->dev, 1451bd756ddeSShun Fu "inquiry device(0:%d:0) wwid(%llx)\n", id, wwid); 1452bd756ddeSShun Fu } else { 1453bd756ddeSShun Fu wwid = 0; 1454bd756ddeSShun Fu } 1455bd756ddeSShun Fu if (cmd_alloc) 1456bd756ddeSShun Fu mvumi_delete_internal_cmd(mhba, cmd); 1457bd756ddeSShun Fu 1458bd756ddeSShun Fu return wwid; 1459bd756ddeSShun Fu } 1460bd756ddeSShun Fu 1461bd756ddeSShun Fu static void mvumi_detach_devices(struct mvumi_hba *mhba) 1462bd756ddeSShun Fu { 1463bd756ddeSShun Fu struct mvumi_device *mv_dev = NULL , *dev_next; 1464bd756ddeSShun Fu struct scsi_device *sdev = NULL; 1465bd756ddeSShun Fu 1466bd756ddeSShun Fu mutex_lock(&mhba->device_lock); 1467bd756ddeSShun Fu 1468bd756ddeSShun Fu /* detach Hard Disk */ 1469bd756ddeSShun Fu list_for_each_entry_safe(mv_dev, dev_next, 1470bd756ddeSShun Fu &mhba->shost_dev_list, list) { 1471bd756ddeSShun Fu mvumi_handle_hotplug(mhba, mv_dev->id, DEVICE_OFFLINE); 1472bd756ddeSShun Fu list_del_init(&mv_dev->list); 1473bd756ddeSShun Fu dev_dbg(&mhba->pdev->dev, "release device(0:%d:0) wwid(%llx)\n", 1474bd756ddeSShun Fu mv_dev->id, mv_dev->wwid); 1475bd756ddeSShun Fu kfree(mv_dev); 1476bd756ddeSShun Fu } 1477bd756ddeSShun Fu list_for_each_entry_safe(mv_dev, dev_next, &mhba->mhba_dev_list, list) { 1478bd756ddeSShun Fu list_del_init(&mv_dev->list); 1479bd756ddeSShun Fu dev_dbg(&mhba->pdev->dev, "release device(0:%d:0) wwid(%llx)\n", 1480bd756ddeSShun Fu mv_dev->id, mv_dev->wwid); 1481bd756ddeSShun Fu kfree(mv_dev); 1482bd756ddeSShun Fu } 1483bd756ddeSShun Fu 1484bd756ddeSShun Fu /* detach virtual device */ 1485bd756ddeSShun Fu if (mhba->pdev->device == PCI_DEVICE_ID_MARVELL_MV9580) 1486bd756ddeSShun Fu sdev = scsi_device_lookup(mhba->shost, 0, 1487bd756ddeSShun Fu mhba->max_target_id - 1, 0); 1488bd756ddeSShun Fu 1489bd756ddeSShun Fu if (sdev) { 1490bd756ddeSShun Fu scsi_remove_device(sdev); 1491bd756ddeSShun Fu scsi_device_put(sdev); 1492bd756ddeSShun Fu } 1493bd756ddeSShun Fu 1494bd756ddeSShun Fu mutex_unlock(&mhba->device_lock); 1495bd756ddeSShun Fu } 1496bd756ddeSShun Fu 1497bd756ddeSShun Fu static void mvumi_rescan_devices(struct mvumi_hba *mhba, int id) 1498bd756ddeSShun Fu { 1499bd756ddeSShun Fu struct scsi_device *sdev; 1500bd756ddeSShun Fu 1501bd756ddeSShun Fu sdev = scsi_device_lookup(mhba->shost, 0, id, 0); 1502bd756ddeSShun Fu if (sdev) { 1503bd756ddeSShun Fu scsi_rescan_device(&sdev->sdev_gendev); 1504bd756ddeSShun Fu scsi_device_put(sdev); 1505bd756ddeSShun Fu } 1506bd756ddeSShun Fu } 1507bd756ddeSShun Fu 1508bd756ddeSShun Fu static int mvumi_match_devices(struct mvumi_hba *mhba, int id, u64 wwid) 1509bd756ddeSShun Fu { 1510bd756ddeSShun Fu struct mvumi_device *mv_dev = NULL; 1511bd756ddeSShun Fu 1512bd756ddeSShun Fu list_for_each_entry(mv_dev, &mhba->shost_dev_list, list) { 1513bd756ddeSShun Fu if (mv_dev->wwid == wwid) { 1514bd756ddeSShun Fu if (mv_dev->id != id) { 1515bd756ddeSShun Fu dev_err(&mhba->pdev->dev, 1516bd756ddeSShun Fu "%s has same wwid[%llx] ," 1517bd756ddeSShun Fu " but different id[%d %d]\n", 1518bd756ddeSShun Fu __func__, mv_dev->wwid, mv_dev->id, id); 1519bd756ddeSShun Fu return -1; 1520bd756ddeSShun Fu } else { 1521bd756ddeSShun Fu if (mhba->pdev->device == 1522bd756ddeSShun Fu PCI_DEVICE_ID_MARVELL_MV9143) 1523bd756ddeSShun Fu mvumi_rescan_devices(mhba, id); 1524bd756ddeSShun Fu return 1; 1525bd756ddeSShun Fu } 1526bd756ddeSShun Fu } 1527bd756ddeSShun Fu } 1528bd756ddeSShun Fu return 0; 1529bd756ddeSShun Fu } 1530bd756ddeSShun Fu 1531bd756ddeSShun Fu static void mvumi_remove_devices(struct mvumi_hba *mhba, int id) 1532bd756ddeSShun Fu { 1533bd756ddeSShun Fu struct mvumi_device *mv_dev = NULL, *dev_next; 1534bd756ddeSShun Fu 1535bd756ddeSShun Fu list_for_each_entry_safe(mv_dev, dev_next, 1536bd756ddeSShun Fu &mhba->shost_dev_list, list) { 1537bd756ddeSShun Fu if (mv_dev->id == id) { 1538bd756ddeSShun Fu dev_dbg(&mhba->pdev->dev, 1539bd756ddeSShun Fu "detach device(0:%d:0) wwid(%llx) from HOST\n", 1540bd756ddeSShun Fu mv_dev->id, mv_dev->wwid); 1541bd756ddeSShun Fu mvumi_handle_hotplug(mhba, mv_dev->id, DEVICE_OFFLINE); 1542bd756ddeSShun Fu list_del_init(&mv_dev->list); 1543bd756ddeSShun Fu kfree(mv_dev); 1544bd756ddeSShun Fu } 1545bd756ddeSShun Fu } 1546bd756ddeSShun Fu } 1547bd756ddeSShun Fu 1548bd756ddeSShun Fu static int mvumi_probe_devices(struct mvumi_hba *mhba) 1549bd756ddeSShun Fu { 1550bd756ddeSShun Fu int id, maxid; 1551bd756ddeSShun Fu u64 wwid = 0; 1552bd756ddeSShun Fu struct mvumi_device *mv_dev = NULL; 1553bd756ddeSShun Fu struct mvumi_cmd *cmd = NULL; 1554bd756ddeSShun Fu int found = 0; 1555bd756ddeSShun Fu 1556bd756ddeSShun Fu cmd = mvumi_create_internal_cmd(mhba, 64); 1557bd756ddeSShun Fu if (!cmd) 1558bd756ddeSShun Fu return -1; 1559bd756ddeSShun Fu 1560bd756ddeSShun Fu if (mhba->pdev->device == PCI_DEVICE_ID_MARVELL_MV9143) 1561bd756ddeSShun Fu maxid = mhba->max_target_id; 1562bd756ddeSShun Fu else 1563bd756ddeSShun Fu maxid = mhba->max_target_id - 1; 1564bd756ddeSShun Fu 1565bd756ddeSShun Fu for (id = 0; id < maxid; id++) { 1566bd756ddeSShun Fu wwid = mvumi_inquiry(mhba, id, cmd); 1567bd756ddeSShun Fu if (!wwid) { 1568bd756ddeSShun Fu /* device no response, remove it */ 1569bd756ddeSShun Fu mvumi_remove_devices(mhba, id); 1570bd756ddeSShun Fu } else { 1571bd756ddeSShun Fu /* device response, add it */ 1572bd756ddeSShun Fu found = mvumi_match_devices(mhba, id, wwid); 1573bd756ddeSShun Fu if (!found) { 1574bd756ddeSShun Fu mvumi_remove_devices(mhba, id); 1575bd756ddeSShun Fu mv_dev = kzalloc(sizeof(struct mvumi_device), 1576bd756ddeSShun Fu GFP_KERNEL); 1577bd756ddeSShun Fu if (!mv_dev) { 1578bd756ddeSShun Fu dev_err(&mhba->pdev->dev, 1579bd756ddeSShun Fu "%s alloc mv_dev failed\n", 1580bd756ddeSShun Fu __func__); 1581bd756ddeSShun Fu continue; 1582bd756ddeSShun Fu } 1583bd756ddeSShun Fu mv_dev->id = id; 1584bd756ddeSShun Fu mv_dev->wwid = wwid; 1585bd756ddeSShun Fu mv_dev->sdev = NULL; 1586bd756ddeSShun Fu INIT_LIST_HEAD(&mv_dev->list); 1587bd756ddeSShun Fu list_add_tail(&mv_dev->list, 1588bd756ddeSShun Fu &mhba->mhba_dev_list); 1589bd756ddeSShun Fu dev_dbg(&mhba->pdev->dev, 1590bd756ddeSShun Fu "probe a new device(0:%d:0)" 1591bd756ddeSShun Fu " wwid(%llx)\n", id, mv_dev->wwid); 1592bd756ddeSShun Fu } else if (found == -1) 1593bd756ddeSShun Fu return -1; 1594bd756ddeSShun Fu else 1595bd756ddeSShun Fu continue; 1596bd756ddeSShun Fu } 1597bd756ddeSShun Fu } 1598bd756ddeSShun Fu 1599bd756ddeSShun Fu if (cmd) 1600bd756ddeSShun Fu mvumi_delete_internal_cmd(mhba, cmd); 1601bd756ddeSShun Fu 1602bd756ddeSShun Fu return 0; 1603bd756ddeSShun Fu } 1604bd756ddeSShun Fu 1605bd756ddeSShun Fu static int mvumi_rescan_bus(void *data) 1606bd756ddeSShun Fu { 1607bd756ddeSShun Fu int ret = 0; 1608bd756ddeSShun Fu struct mvumi_hba *mhba = (struct mvumi_hba *) data; 1609bd756ddeSShun Fu struct mvumi_device *mv_dev = NULL , *dev_next; 1610bd756ddeSShun Fu 1611bd756ddeSShun Fu while (!kthread_should_stop()) { 1612bd756ddeSShun Fu 1613bd756ddeSShun Fu set_current_state(TASK_INTERRUPTIBLE); 1614bd756ddeSShun Fu if (!atomic_read(&mhba->pnp_count)) 1615bd756ddeSShun Fu schedule(); 1616bd756ddeSShun Fu msleep(1000); 1617bd756ddeSShun Fu atomic_set(&mhba->pnp_count, 0); 1618bd756ddeSShun Fu __set_current_state(TASK_RUNNING); 1619bd756ddeSShun Fu 1620bd756ddeSShun Fu mutex_lock(&mhba->device_lock); 1621bd756ddeSShun Fu ret = mvumi_probe_devices(mhba); 1622bd756ddeSShun Fu if (!ret) { 1623bd756ddeSShun Fu list_for_each_entry_safe(mv_dev, dev_next, 1624bd756ddeSShun Fu &mhba->mhba_dev_list, list) { 1625bd756ddeSShun Fu if (mvumi_handle_hotplug(mhba, mv_dev->id, 1626bd756ddeSShun Fu DEVICE_ONLINE)) { 1627bd756ddeSShun Fu dev_err(&mhba->pdev->dev, 1628bd756ddeSShun Fu "%s add device(0:%d:0) failed" 1629bd756ddeSShun Fu "wwid(%llx) has exist\n", 1630bd756ddeSShun Fu __func__, 1631bd756ddeSShun Fu mv_dev->id, mv_dev->wwid); 1632bd756ddeSShun Fu list_del_init(&mv_dev->list); 1633bd756ddeSShun Fu kfree(mv_dev); 1634bd756ddeSShun Fu } else { 1635bd756ddeSShun Fu list_move_tail(&mv_dev->list, 1636bd756ddeSShun Fu &mhba->shost_dev_list); 1637bd756ddeSShun Fu } 1638bd756ddeSShun Fu } 1639bd756ddeSShun Fu } 1640bd756ddeSShun Fu mutex_unlock(&mhba->device_lock); 1641bd756ddeSShun Fu } 1642bd756ddeSShun Fu return 0; 1643bd756ddeSShun Fu } 1644bd756ddeSShun Fu 1645bd756ddeSShun Fu static void mvumi_proc_msg(struct mvumi_hba *mhba, 1646bd756ddeSShun Fu struct mvumi_hotplug_event *param) 1647bd756ddeSShun Fu { 1648bd756ddeSShun Fu u16 size = param->size; 1649bd756ddeSShun Fu const unsigned long *ar_bitmap; 1650bd756ddeSShun Fu const unsigned long *re_bitmap; 1651bd756ddeSShun Fu int index; 1652bd756ddeSShun Fu 1653bd756ddeSShun Fu if (mhba->fw_flag & MVUMI_FW_ATTACH) { 1654bd756ddeSShun Fu index = -1; 1655bd756ddeSShun Fu ar_bitmap = (const unsigned long *) param->bitmap; 1656bd756ddeSShun Fu re_bitmap = (const unsigned long *) ¶m->bitmap[size >> 3]; 1657bd756ddeSShun Fu 1658bd756ddeSShun Fu mutex_lock(&mhba->sas_discovery_mutex); 1659bd756ddeSShun Fu do { 1660bd756ddeSShun Fu index = find_next_zero_bit(ar_bitmap, size, index + 1); 1661bd756ddeSShun Fu if (index >= size) 1662bd756ddeSShun Fu break; 1663bd756ddeSShun Fu mvumi_handle_hotplug(mhba, index, DEVICE_ONLINE); 1664bd756ddeSShun Fu } while (1); 1665bd756ddeSShun Fu 1666bd756ddeSShun Fu index = -1; 1667bd756ddeSShun Fu do { 1668bd756ddeSShun Fu index = find_next_zero_bit(re_bitmap, size, index + 1); 1669bd756ddeSShun Fu if (index >= size) 1670bd756ddeSShun Fu break; 1671bd756ddeSShun Fu mvumi_handle_hotplug(mhba, index, DEVICE_OFFLINE); 1672bd756ddeSShun Fu } while (1); 1673bd756ddeSShun Fu mutex_unlock(&mhba->sas_discovery_mutex); 1674bd756ddeSShun Fu } 1675bd756ddeSShun Fu } 1676bd756ddeSShun Fu 1677f0c568a4SJianyun Li static void mvumi_notification(struct mvumi_hba *mhba, u8 msg, void *buffer) 1678f0c568a4SJianyun Li { 1679f0c568a4SJianyun Li if (msg == APICDB1_EVENT_GETEVENT) { 1680f0c568a4SJianyun Li int i, count; 1681f0c568a4SJianyun Li struct mvumi_driver_event *param = NULL; 1682f0c568a4SJianyun Li struct mvumi_event_req *er = buffer; 1683f0c568a4SJianyun Li count = er->count; 1684f0c568a4SJianyun Li if (count > MAX_EVENTS_RETURNED) { 1685f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, "event count[0x%x] is bigger" 1686f0c568a4SJianyun Li " than max event count[0x%x].\n", 1687f0c568a4SJianyun Li count, MAX_EVENTS_RETURNED); 1688f0c568a4SJianyun Li return; 1689f0c568a4SJianyun Li } 1690f0c568a4SJianyun Li for (i = 0; i < count; i++) { 1691f0c568a4SJianyun Li param = &er->events[i]; 1692f0c568a4SJianyun Li mvumi_show_event(mhba, param); 1693f0c568a4SJianyun Li } 1694bd756ddeSShun Fu } else if (msg == APICDB1_HOST_GETEVENT) { 1695bd756ddeSShun Fu mvumi_proc_msg(mhba, buffer); 1696f0c568a4SJianyun Li } 1697f0c568a4SJianyun Li } 1698f0c568a4SJianyun Li 1699f0c568a4SJianyun Li static int mvumi_get_event(struct mvumi_hba *mhba, unsigned char msg) 1700f0c568a4SJianyun Li { 1701f0c568a4SJianyun Li struct mvumi_cmd *cmd; 1702f0c568a4SJianyun Li struct mvumi_msg_frame *frame; 1703f0c568a4SJianyun Li 1704f0c568a4SJianyun Li cmd = mvumi_create_internal_cmd(mhba, 512); 1705f0c568a4SJianyun Li if (!cmd) 1706f0c568a4SJianyun Li return -1; 1707f0c568a4SJianyun Li cmd->scmd = NULL; 1708f0c568a4SJianyun Li cmd->cmd_status = REQ_STATUS_PENDING; 1709f0c568a4SJianyun Li atomic_set(&cmd->sync_cmd, 0); 1710f0c568a4SJianyun Li frame = cmd->frame; 1711f0c568a4SJianyun Li frame->device_id = 0; 1712f0c568a4SJianyun Li frame->cmd_flag = CMD_FLAG_DATA_IN; 1713f0c568a4SJianyun Li frame->req_function = CL_FUN_SCSI_CMD; 1714f0c568a4SJianyun Li frame->cdb_length = MAX_COMMAND_SIZE; 1715f0c568a4SJianyun Li frame->data_transfer_length = sizeof(struct mvumi_event_req); 1716f0c568a4SJianyun Li memset(frame->cdb, 0, MAX_COMMAND_SIZE); 1717f0c568a4SJianyun Li frame->cdb[0] = APICDB0_EVENT; 1718f0c568a4SJianyun Li frame->cdb[1] = msg; 1719f0c568a4SJianyun Li mvumi_issue_blocked_cmd(mhba, cmd); 1720f0c568a4SJianyun Li 1721f0c568a4SJianyun Li if (cmd->cmd_status != SAM_STAT_GOOD) 1722f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, "get event failed, status=0x%x.\n", 1723f0c568a4SJianyun Li cmd->cmd_status); 1724f0c568a4SJianyun Li else 1725f0c568a4SJianyun Li mvumi_notification(mhba, cmd->frame->cdb[1], cmd->data_buf); 1726f0c568a4SJianyun Li 1727f0c568a4SJianyun Li mvumi_delete_internal_cmd(mhba, cmd); 1728f0c568a4SJianyun Li return 0; 1729f0c568a4SJianyun Li } 1730f0c568a4SJianyun Li 1731f0c568a4SJianyun Li static void mvumi_scan_events(struct work_struct *work) 1732f0c568a4SJianyun Li { 1733f0c568a4SJianyun Li struct mvumi_events_wq *mu_ev = 1734f0c568a4SJianyun Li container_of(work, struct mvumi_events_wq, work_q); 1735f0c568a4SJianyun Li 1736f0c568a4SJianyun Li mvumi_get_event(mu_ev->mhba, mu_ev->event); 1737f0c568a4SJianyun Li kfree(mu_ev); 1738f0c568a4SJianyun Li } 1739f0c568a4SJianyun Li 1740bd756ddeSShun Fu static void mvumi_launch_events(struct mvumi_hba *mhba, u32 isr_status) 1741f0c568a4SJianyun Li { 1742f0c568a4SJianyun Li struct mvumi_events_wq *mu_ev; 1743f0c568a4SJianyun Li 1744bd756ddeSShun Fu while (isr_status & (DRBL_BUS_CHANGE | DRBL_EVENT_NOTIFY)) { 1745bd756ddeSShun Fu if (isr_status & DRBL_BUS_CHANGE) { 1746bd756ddeSShun Fu atomic_inc(&mhba->pnp_count); 1747bd756ddeSShun Fu wake_up_process(mhba->dm_thread); 1748bd756ddeSShun Fu isr_status &= ~(DRBL_BUS_CHANGE); 1749bd756ddeSShun Fu continue; 1750bd756ddeSShun Fu } 1751bd756ddeSShun Fu 1752f0c568a4SJianyun Li mu_ev = kzalloc(sizeof(*mu_ev), GFP_ATOMIC); 1753f0c568a4SJianyun Li if (mu_ev) { 1754f0c568a4SJianyun Li INIT_WORK(&mu_ev->work_q, mvumi_scan_events); 1755f0c568a4SJianyun Li mu_ev->mhba = mhba; 1756bd756ddeSShun Fu mu_ev->event = APICDB1_EVENT_GETEVENT; 1757bd756ddeSShun Fu isr_status &= ~(DRBL_EVENT_NOTIFY); 1758f0c568a4SJianyun Li mu_ev->param = NULL; 1759f0c568a4SJianyun Li schedule_work(&mu_ev->work_q); 1760f0c568a4SJianyun Li } 1761f0c568a4SJianyun Li } 1762bd756ddeSShun Fu } 1763f0c568a4SJianyun Li 1764f0c568a4SJianyun Li static void mvumi_handle_clob(struct mvumi_hba *mhba) 1765f0c568a4SJianyun Li { 1766f0c568a4SJianyun Li struct mvumi_rsp_frame *ob_frame; 1767f0c568a4SJianyun Li struct mvumi_cmd *cmd; 1768f0c568a4SJianyun Li struct mvumi_ob_data *pool; 1769f0c568a4SJianyun Li 1770f0c568a4SJianyun Li while (!list_empty(&mhba->free_ob_list)) { 1771f0c568a4SJianyun Li pool = list_first_entry(&mhba->free_ob_list, 1772f0c568a4SJianyun Li struct mvumi_ob_data, list); 1773f0c568a4SJianyun Li list_del_init(&pool->list); 1774f0c568a4SJianyun Li list_add_tail(&pool->list, &mhba->ob_data_list); 1775f0c568a4SJianyun Li 1776f0c568a4SJianyun Li ob_frame = (struct mvumi_rsp_frame *) &pool->data[0]; 1777f0c568a4SJianyun Li cmd = mhba->tag_cmd[ob_frame->tag]; 1778f0c568a4SJianyun Li 1779f0c568a4SJianyun Li atomic_dec(&mhba->fw_outstanding); 17807512ddefSYueHaibing mhba->tag_cmd[ob_frame->tag] = NULL; 1781f0c568a4SJianyun Li tag_release_one(mhba, &mhba->tag_pool, ob_frame->tag); 1782f0c568a4SJianyun Li if (cmd->scmd) 1783f0c568a4SJianyun Li mvumi_complete_cmd(mhba, cmd, ob_frame); 1784f0c568a4SJianyun Li else 1785f0c568a4SJianyun Li mvumi_complete_internal_cmd(mhba, cmd, ob_frame); 1786f0c568a4SJianyun Li } 1787f0c568a4SJianyun Li mhba->instancet->fire_cmd(mhba, NULL); 1788f0c568a4SJianyun Li } 1789f0c568a4SJianyun Li 1790f0c568a4SJianyun Li static irqreturn_t mvumi_isr_handler(int irq, void *devp) 1791f0c568a4SJianyun Li { 1792f0c568a4SJianyun Li struct mvumi_hba *mhba = (struct mvumi_hba *) devp; 1793f0c568a4SJianyun Li unsigned long flags; 1794f0c568a4SJianyun Li 1795f0c568a4SJianyun Li spin_lock_irqsave(mhba->shost->host_lock, flags); 1796f0c568a4SJianyun Li if (unlikely(mhba->instancet->clear_intr(mhba) || !mhba->global_isr)) { 1797f0c568a4SJianyun Li spin_unlock_irqrestore(mhba->shost->host_lock, flags); 1798f0c568a4SJianyun Li return IRQ_NONE; 1799f0c568a4SJianyun Li } 1800f0c568a4SJianyun Li 1801bd756ddeSShun Fu if (mhba->global_isr & mhba->regs->int_dl_cpu2pciea) { 1802bd756ddeSShun Fu if (mhba->isr_status & (DRBL_BUS_CHANGE | DRBL_EVENT_NOTIFY)) 1803bd756ddeSShun Fu mvumi_launch_events(mhba, mhba->isr_status); 1804f0c568a4SJianyun Li if (mhba->isr_status & DRBL_HANDSHAKE_ISR) { 1805f0c568a4SJianyun Li dev_warn(&mhba->pdev->dev, "enter handshake again!\n"); 1806f0c568a4SJianyun Li mvumi_handshake(mhba); 1807f0c568a4SJianyun Li } 1808bd756ddeSShun Fu 1809f0c568a4SJianyun Li } 1810f0c568a4SJianyun Li 1811bd756ddeSShun Fu if (mhba->global_isr & mhba->regs->int_comaout) 1812f0c568a4SJianyun Li mvumi_receive_ob_list_entry(mhba); 1813f0c568a4SJianyun Li 1814f0c568a4SJianyun Li mhba->global_isr = 0; 1815f0c568a4SJianyun Li mhba->isr_status = 0; 1816f0c568a4SJianyun Li if (mhba->fw_state == FW_STATE_STARTED) 1817f0c568a4SJianyun Li mvumi_handle_clob(mhba); 1818f0c568a4SJianyun Li spin_unlock_irqrestore(mhba->shost->host_lock, flags); 1819f0c568a4SJianyun Li return IRQ_HANDLED; 1820f0c568a4SJianyun Li } 1821f0c568a4SJianyun Li 1822f0c568a4SJianyun Li static enum mvumi_qc_result mvumi_send_command(struct mvumi_hba *mhba, 1823f0c568a4SJianyun Li struct mvumi_cmd *cmd) 1824f0c568a4SJianyun Li { 1825f0c568a4SJianyun Li void *ib_entry; 1826f0c568a4SJianyun Li struct mvumi_msg_frame *ib_frame; 1827f0c568a4SJianyun Li unsigned int frame_len; 1828f0c568a4SJianyun Li 1829f0c568a4SJianyun Li ib_frame = cmd->frame; 1830f0c568a4SJianyun Li if (unlikely(mhba->fw_state != FW_STATE_STARTED)) { 1831f0c568a4SJianyun Li dev_dbg(&mhba->pdev->dev, "firmware not ready.\n"); 1832f0c568a4SJianyun Li return MV_QUEUE_COMMAND_RESULT_NO_RESOURCE; 1833f0c568a4SJianyun Li } 1834f0c568a4SJianyun Li if (tag_is_empty(&mhba->tag_pool)) { 1835f0c568a4SJianyun Li dev_dbg(&mhba->pdev->dev, "no free tag.\n"); 1836f0c568a4SJianyun Li return MV_QUEUE_COMMAND_RESULT_NO_RESOURCE; 1837f0c568a4SJianyun Li } 1838bd756ddeSShun Fu mvumi_get_ib_list_entry(mhba, &ib_entry); 1839f0c568a4SJianyun Li 1840f0c568a4SJianyun Li cmd->frame->tag = tag_get_one(mhba, &mhba->tag_pool); 1841f0c568a4SJianyun Li cmd->frame->request_id = mhba->io_seq++; 1842f0c568a4SJianyun Li cmd->request_id = cmd->frame->request_id; 1843f0c568a4SJianyun Li mhba->tag_cmd[cmd->frame->tag] = cmd; 1844f0c568a4SJianyun Li frame_len = sizeof(*ib_frame) - 4 + 1845f0c568a4SJianyun Li ib_frame->sg_counts * sizeof(struct mvumi_sgl); 1846bd756ddeSShun Fu if (mhba->hba_capability & HS_CAPABILITY_SUPPORT_DYN_SRC) { 1847bd756ddeSShun Fu struct mvumi_dyn_list_entry *dle; 1848bd756ddeSShun Fu dle = ib_entry; 1849bd756ddeSShun Fu dle->src_low_addr = 1850bd756ddeSShun Fu cpu_to_le32(lower_32_bits(cmd->frame_phys)); 1851bd756ddeSShun Fu dle->src_high_addr = 1852bd756ddeSShun Fu cpu_to_le32(upper_32_bits(cmd->frame_phys)); 1853bd756ddeSShun Fu dle->if_length = (frame_len >> 2) & 0xFFF; 1854bd756ddeSShun Fu } else { 1855f0c568a4SJianyun Li memcpy(ib_entry, ib_frame, frame_len); 1856bd756ddeSShun Fu } 1857f0c568a4SJianyun Li return MV_QUEUE_COMMAND_RESULT_SENT; 1858f0c568a4SJianyun Li } 1859f0c568a4SJianyun Li 1860f0c568a4SJianyun Li static void mvumi_fire_cmd(struct mvumi_hba *mhba, struct mvumi_cmd *cmd) 1861f0c568a4SJianyun Li { 1862f0c568a4SJianyun Li unsigned short num_of_cl_sent = 0; 1863bd756ddeSShun Fu unsigned int count; 1864f0c568a4SJianyun Li enum mvumi_qc_result result; 1865f0c568a4SJianyun Li 1866f0c568a4SJianyun Li if (cmd) 1867f0c568a4SJianyun Li list_add_tail(&cmd->queue_pointer, &mhba->waiting_req_list); 1868bd756ddeSShun Fu count = mhba->instancet->check_ib_list(mhba); 1869bd756ddeSShun Fu if (list_empty(&mhba->waiting_req_list) || !count) 1870bd756ddeSShun Fu return; 1871f0c568a4SJianyun Li 1872bd756ddeSShun Fu do { 1873f0c568a4SJianyun Li cmd = list_first_entry(&mhba->waiting_req_list, 1874f0c568a4SJianyun Li struct mvumi_cmd, queue_pointer); 1875f0c568a4SJianyun Li list_del_init(&cmd->queue_pointer); 1876f0c568a4SJianyun Li result = mvumi_send_command(mhba, cmd); 1877f0c568a4SJianyun Li switch (result) { 1878f0c568a4SJianyun Li case MV_QUEUE_COMMAND_RESULT_SENT: 1879f0c568a4SJianyun Li num_of_cl_sent++; 1880f0c568a4SJianyun Li break; 1881f0c568a4SJianyun Li case MV_QUEUE_COMMAND_RESULT_NO_RESOURCE: 1882f0c568a4SJianyun Li list_add(&cmd->queue_pointer, &mhba->waiting_req_list); 1883f0c568a4SJianyun Li if (num_of_cl_sent > 0) 1884f0c568a4SJianyun Li mvumi_send_ib_list_entry(mhba); 1885f0c568a4SJianyun Li 1886f0c568a4SJianyun Li return; 1887f0c568a4SJianyun Li } 1888bd756ddeSShun Fu } while (!list_empty(&mhba->waiting_req_list) && count--); 1889bd756ddeSShun Fu 1890f0c568a4SJianyun Li if (num_of_cl_sent > 0) 1891f0c568a4SJianyun Li mvumi_send_ib_list_entry(mhba); 1892f0c568a4SJianyun Li } 1893f0c568a4SJianyun Li 1894f0c568a4SJianyun Li /** 1895f0c568a4SJianyun Li * mvumi_enable_intr - Enables interrupts 1896bd756ddeSShun Fu * @mhba: Adapter soft state 1897f0c568a4SJianyun Li */ 1898bd756ddeSShun Fu static void mvumi_enable_intr(struct mvumi_hba *mhba) 1899f0c568a4SJianyun Li { 1900f0c568a4SJianyun Li unsigned int mask; 1901bd756ddeSShun Fu struct mvumi_hw_regs *regs = mhba->regs; 1902f0c568a4SJianyun Li 1903bd756ddeSShun Fu iowrite32(regs->int_drbl_int_mask, regs->arm_to_pciea_mask_reg); 1904bd756ddeSShun Fu mask = ioread32(regs->enpointa_mask_reg); 1905bd756ddeSShun Fu mask |= regs->int_dl_cpu2pciea | regs->int_comaout | regs->int_comaerr; 1906bd756ddeSShun Fu iowrite32(mask, regs->enpointa_mask_reg); 1907f0c568a4SJianyun Li } 1908f0c568a4SJianyun Li 1909f0c568a4SJianyun Li /** 1910f0c568a4SJianyun Li * mvumi_disable_intr -Disables interrupt 1911bd756ddeSShun Fu * @mhba: Adapter soft state 1912f0c568a4SJianyun Li */ 1913bd756ddeSShun Fu static void mvumi_disable_intr(struct mvumi_hba *mhba) 1914f0c568a4SJianyun Li { 1915f0c568a4SJianyun Li unsigned int mask; 1916bd756ddeSShun Fu struct mvumi_hw_regs *regs = mhba->regs; 1917f0c568a4SJianyun Li 1918bd756ddeSShun Fu iowrite32(0, regs->arm_to_pciea_mask_reg); 1919bd756ddeSShun Fu mask = ioread32(regs->enpointa_mask_reg); 1920bd756ddeSShun Fu mask &= ~(regs->int_dl_cpu2pciea | regs->int_comaout | 1921bd756ddeSShun Fu regs->int_comaerr); 1922bd756ddeSShun Fu iowrite32(mask, regs->enpointa_mask_reg); 1923f0c568a4SJianyun Li } 1924f0c568a4SJianyun Li 1925f0c568a4SJianyun Li static int mvumi_clear_intr(void *extend) 1926f0c568a4SJianyun Li { 1927f0c568a4SJianyun Li struct mvumi_hba *mhba = (struct mvumi_hba *) extend; 1928f0c568a4SJianyun Li unsigned int status, isr_status = 0, tmp = 0; 1929bd756ddeSShun Fu struct mvumi_hw_regs *regs = mhba->regs; 1930f0c568a4SJianyun Li 1931bd756ddeSShun Fu status = ioread32(regs->main_int_cause_reg); 1932bd756ddeSShun Fu if (!(status & regs->int_mu) || status == 0xFFFFFFFF) 1933f0c568a4SJianyun Li return 1; 1934bd756ddeSShun Fu if (unlikely(status & regs->int_comaerr)) { 1935bd756ddeSShun Fu tmp = ioread32(regs->outb_isr_cause); 1936bd756ddeSShun Fu if (mhba->pdev->device == PCI_DEVICE_ID_MARVELL_MV9580) { 1937bd756ddeSShun Fu if (tmp & regs->clic_out_err) { 1938bd756ddeSShun Fu iowrite32(tmp & regs->clic_out_err, 1939bd756ddeSShun Fu regs->outb_isr_cause); 1940bd756ddeSShun Fu } 1941bd756ddeSShun Fu } else { 1942bd756ddeSShun Fu if (tmp & (regs->clic_in_err | regs->clic_out_err)) 1943bd756ddeSShun Fu iowrite32(tmp & (regs->clic_in_err | 1944bd756ddeSShun Fu regs->clic_out_err), 1945bd756ddeSShun Fu regs->outb_isr_cause); 1946bd756ddeSShun Fu } 1947bd756ddeSShun Fu status ^= mhba->regs->int_comaerr; 1948f0c568a4SJianyun Li /* inbound or outbound parity error, command will timeout */ 1949f0c568a4SJianyun Li } 1950bd756ddeSShun Fu if (status & regs->int_comaout) { 1951bd756ddeSShun Fu tmp = ioread32(regs->outb_isr_cause); 1952bd756ddeSShun Fu if (tmp & regs->clic_irq) 1953bd756ddeSShun Fu iowrite32(tmp & regs->clic_irq, regs->outb_isr_cause); 1954f0c568a4SJianyun Li } 1955bd756ddeSShun Fu if (status & regs->int_dl_cpu2pciea) { 1956bd756ddeSShun Fu isr_status = ioread32(regs->arm_to_pciea_drbl_reg); 1957f0c568a4SJianyun Li if (isr_status) 1958bd756ddeSShun Fu iowrite32(isr_status, regs->arm_to_pciea_drbl_reg); 1959f0c568a4SJianyun Li } 1960f0c568a4SJianyun Li 1961f0c568a4SJianyun Li mhba->global_isr = status; 1962f0c568a4SJianyun Li mhba->isr_status = isr_status; 1963f0c568a4SJianyun Li 1964f0c568a4SJianyun Li return 0; 1965f0c568a4SJianyun Li } 1966f0c568a4SJianyun Li 1967f0c568a4SJianyun Li /** 1968f0c568a4SJianyun Li * mvumi_read_fw_status_reg - returns the current FW status value 1969bd756ddeSShun Fu * @mhba: Adapter soft state 1970f0c568a4SJianyun Li */ 1971bd756ddeSShun Fu static unsigned int mvumi_read_fw_status_reg(struct mvumi_hba *mhba) 1972f0c568a4SJianyun Li { 1973f0c568a4SJianyun Li unsigned int status; 1974f0c568a4SJianyun Li 1975bd756ddeSShun Fu status = ioread32(mhba->regs->arm_to_pciea_drbl_reg); 1976f0c568a4SJianyun Li if (status) 1977bd756ddeSShun Fu iowrite32(status, mhba->regs->arm_to_pciea_drbl_reg); 1978f0c568a4SJianyun Li return status; 1979f0c568a4SJianyun Li } 1980f0c568a4SJianyun Li 1981bd756ddeSShun Fu static struct mvumi_instance_template mvumi_instance_9143 = { 1982f0c568a4SJianyun Li .fire_cmd = mvumi_fire_cmd, 1983f0c568a4SJianyun Li .enable_intr = mvumi_enable_intr, 1984f0c568a4SJianyun Li .disable_intr = mvumi_disable_intr, 1985f0c568a4SJianyun Li .clear_intr = mvumi_clear_intr, 1986f0c568a4SJianyun Li .read_fw_status_reg = mvumi_read_fw_status_reg, 1987bd756ddeSShun Fu .check_ib_list = mvumi_check_ib_list_9143, 1988bd756ddeSShun Fu .check_ob_list = mvumi_check_ob_list_9143, 1989bd756ddeSShun Fu .reset_host = mvumi_reset_host_9143, 1990bd756ddeSShun Fu }; 1991bd756ddeSShun Fu 1992bd756ddeSShun Fu static struct mvumi_instance_template mvumi_instance_9580 = { 1993bd756ddeSShun Fu .fire_cmd = mvumi_fire_cmd, 1994bd756ddeSShun Fu .enable_intr = mvumi_enable_intr, 1995bd756ddeSShun Fu .disable_intr = mvumi_disable_intr, 1996bd756ddeSShun Fu .clear_intr = mvumi_clear_intr, 1997bd756ddeSShun Fu .read_fw_status_reg = mvumi_read_fw_status_reg, 1998bd756ddeSShun Fu .check_ib_list = mvumi_check_ib_list_9580, 1999bd756ddeSShun Fu .check_ob_list = mvumi_check_ob_list_9580, 2000bd756ddeSShun Fu .reset_host = mvumi_reset_host_9580, 2001f0c568a4SJianyun Li }; 2002f0c568a4SJianyun Li 2003f0c568a4SJianyun Li static int mvumi_slave_configure(struct scsi_device *sdev) 2004f0c568a4SJianyun Li { 2005f0c568a4SJianyun Li struct mvumi_hba *mhba; 2006f0c568a4SJianyun Li unsigned char bitcount = sizeof(unsigned char) * 8; 2007f0c568a4SJianyun Li 2008f0c568a4SJianyun Li mhba = (struct mvumi_hba *) sdev->host->hostdata; 2009f0c568a4SJianyun Li if (sdev->id >= mhba->max_target_id) 2010f0c568a4SJianyun Li return -EINVAL; 2011f0c568a4SJianyun Li 2012f0c568a4SJianyun Li mhba->target_map[sdev->id / bitcount] |= (1 << (sdev->id % bitcount)); 2013f0c568a4SJianyun Li return 0; 2014f0c568a4SJianyun Li } 2015f0c568a4SJianyun Li 2016f0c568a4SJianyun Li /** 2017f0c568a4SJianyun Li * mvumi_build_frame - Prepares a direct cdb (DCDB) command 2018f0c568a4SJianyun Li * @mhba: Adapter soft state 2019f0c568a4SJianyun Li * @scmd: SCSI command 2020f0c568a4SJianyun Li * @cmd: Command to be prepared in 2021f0c568a4SJianyun Li * 2022f0c568a4SJianyun Li * This function prepares CDB commands. These are typcially pass-through 2023f0c568a4SJianyun Li * commands to the devices. 2024f0c568a4SJianyun Li */ 2025f0c568a4SJianyun Li static unsigned char mvumi_build_frame(struct mvumi_hba *mhba, 2026f0c568a4SJianyun Li struct scsi_cmnd *scmd, struct mvumi_cmd *cmd) 2027f0c568a4SJianyun Li { 2028f0c568a4SJianyun Li struct mvumi_msg_frame *pframe; 2029f0c568a4SJianyun Li 2030f0c568a4SJianyun Li cmd->scmd = scmd; 2031f0c568a4SJianyun Li cmd->cmd_status = REQ_STATUS_PENDING; 2032f0c568a4SJianyun Li pframe = cmd->frame; 2033f0c568a4SJianyun Li pframe->device_id = ((unsigned short) scmd->device->id) | 2034f0c568a4SJianyun Li (((unsigned short) scmd->device->lun) << 8); 2035f0c568a4SJianyun Li pframe->cmd_flag = 0; 2036f0c568a4SJianyun Li 2037f0c568a4SJianyun Li switch (scmd->sc_data_direction) { 2038f0c568a4SJianyun Li case DMA_NONE: 2039f0c568a4SJianyun Li pframe->cmd_flag |= CMD_FLAG_NON_DATA; 2040f0c568a4SJianyun Li break; 2041f0c568a4SJianyun Li case DMA_FROM_DEVICE: 2042f0c568a4SJianyun Li pframe->cmd_flag |= CMD_FLAG_DATA_IN; 2043f0c568a4SJianyun Li break; 2044f0c568a4SJianyun Li case DMA_TO_DEVICE: 2045f0c568a4SJianyun Li pframe->cmd_flag |= CMD_FLAG_DATA_OUT; 2046f0c568a4SJianyun Li break; 2047f0c568a4SJianyun Li case DMA_BIDIRECTIONAL: 2048f0c568a4SJianyun Li default: 2049f0c568a4SJianyun Li dev_warn(&mhba->pdev->dev, "unexpected data direction[%d] " 2050f0c568a4SJianyun Li "cmd[0x%x]\n", scmd->sc_data_direction, scmd->cmnd[0]); 2051f0c568a4SJianyun Li goto error; 2052f0c568a4SJianyun Li } 2053f0c568a4SJianyun Li 2054f0c568a4SJianyun Li pframe->cdb_length = scmd->cmd_len; 2055f0c568a4SJianyun Li memcpy(pframe->cdb, scmd->cmnd, pframe->cdb_length); 2056f0c568a4SJianyun Li pframe->req_function = CL_FUN_SCSI_CMD; 2057f0c568a4SJianyun Li if (scsi_bufflen(scmd)) { 2058f0c568a4SJianyun Li if (mvumi_make_sgl(mhba, scmd, &pframe->payload[0], 2059f0c568a4SJianyun Li &pframe->sg_counts)) 2060f0c568a4SJianyun Li goto error; 2061f0c568a4SJianyun Li 2062f0c568a4SJianyun Li pframe->data_transfer_length = scsi_bufflen(scmd); 2063f0c568a4SJianyun Li } else { 2064f0c568a4SJianyun Li pframe->sg_counts = 0; 2065f0c568a4SJianyun Li pframe->data_transfer_length = 0; 2066f0c568a4SJianyun Li } 2067f0c568a4SJianyun Li return 0; 2068f0c568a4SJianyun Li 2069f0c568a4SJianyun Li error: 2070f2b1e9c6SHannes Reinecke scsi_build_sense(scmd, 0, ILLEGAL_REQUEST, 0x24, 0); 2071f0c568a4SJianyun Li return -1; 2072f0c568a4SJianyun Li } 2073f0c568a4SJianyun Li 2074f0c568a4SJianyun Li /** 2075f0c568a4SJianyun Li * mvumi_queue_command - Queue entry point 20765ccd6265SLee Jones * @shost: Scsi host to queue command on 2077f0c568a4SJianyun Li * @scmd: SCSI command to be queued 2078f0c568a4SJianyun Li */ 2079f0c568a4SJianyun Li static int mvumi_queue_command(struct Scsi_Host *shost, 2080f0c568a4SJianyun Li struct scsi_cmnd *scmd) 2081f0c568a4SJianyun Li { 2082f0c568a4SJianyun Li struct mvumi_cmd *cmd; 2083f0c568a4SJianyun Li struct mvumi_hba *mhba; 2084f0c568a4SJianyun Li unsigned long irq_flags; 2085f0c568a4SJianyun Li 2086f0c568a4SJianyun Li spin_lock_irqsave(shost->host_lock, irq_flags); 2087f0c568a4SJianyun Li 2088f0c568a4SJianyun Li mhba = (struct mvumi_hba *) shost->hostdata; 2089f0c568a4SJianyun Li scmd->result = 0; 2090f0c568a4SJianyun Li cmd = mvumi_get_cmd(mhba); 2091f0c568a4SJianyun Li if (unlikely(!cmd)) { 2092f0c568a4SJianyun Li spin_unlock_irqrestore(shost->host_lock, irq_flags); 2093f0c568a4SJianyun Li return SCSI_MLQUEUE_HOST_BUSY; 2094f0c568a4SJianyun Li } 2095f0c568a4SJianyun Li 2096f0c568a4SJianyun Li if (unlikely(mvumi_build_frame(mhba, scmd, cmd))) 2097f0c568a4SJianyun Li goto out_return_cmd; 2098f0c568a4SJianyun Li 2099f0c568a4SJianyun Li cmd->scmd = scmd; 2100f0c568a4SJianyun Li scmd->SCp.ptr = (char *) cmd; 2101f0c568a4SJianyun Li mhba->instancet->fire_cmd(mhba, cmd); 2102f0c568a4SJianyun Li spin_unlock_irqrestore(shost->host_lock, irq_flags); 2103f0c568a4SJianyun Li return 0; 2104f0c568a4SJianyun Li 2105f0c568a4SJianyun Li out_return_cmd: 2106f0c568a4SJianyun Li mvumi_return_cmd(mhba, cmd); 2107f0c568a4SJianyun Li scmd->scsi_done(scmd); 2108f0c568a4SJianyun Li spin_unlock_irqrestore(shost->host_lock, irq_flags); 2109f0c568a4SJianyun Li return 0; 2110f0c568a4SJianyun Li } 2111f0c568a4SJianyun Li 2112f0c568a4SJianyun Li static enum blk_eh_timer_return mvumi_timed_out(struct scsi_cmnd *scmd) 2113f0c568a4SJianyun Li { 2114f0c568a4SJianyun Li struct mvumi_cmd *cmd = (struct mvumi_cmd *) scmd->SCp.ptr; 2115f0c568a4SJianyun Li struct Scsi_Host *host = scmd->device->host; 2116f0c568a4SJianyun Li struct mvumi_hba *mhba = shost_priv(host); 2117f0c568a4SJianyun Li unsigned long flags; 2118f0c568a4SJianyun Li 2119f0c568a4SJianyun Li spin_lock_irqsave(mhba->shost->host_lock, flags); 2120f0c568a4SJianyun Li 2121f0c568a4SJianyun Li if (mhba->tag_cmd[cmd->frame->tag]) { 21227512ddefSYueHaibing mhba->tag_cmd[cmd->frame->tag] = NULL; 2123f0c568a4SJianyun Li tag_release_one(mhba, &mhba->tag_pool, cmd->frame->tag); 2124f0c568a4SJianyun Li } 2125f0c568a4SJianyun Li if (!list_empty(&cmd->queue_pointer)) 2126f0c568a4SJianyun Li list_del_init(&cmd->queue_pointer); 2127f0c568a4SJianyun Li else 2128f0c568a4SJianyun Li atomic_dec(&mhba->fw_outstanding); 2129f0c568a4SJianyun Li 2130*16576ad8SHannes Reinecke scmd->result = (DID_ABORT << 16); 2131f0c568a4SJianyun Li scmd->SCp.ptr = NULL; 2132f0c568a4SJianyun Li if (scsi_bufflen(scmd)) { 2133ab8e7f4bSChristoph Hellwig dma_unmap_sg(&mhba->pdev->dev, scsi_sglist(scmd), 2134f0c568a4SJianyun Li scsi_sg_count(scmd), 2135ab8e7f4bSChristoph Hellwig scmd->sc_data_direction); 2136f0c568a4SJianyun Li } 2137f0c568a4SJianyun Li mvumi_return_cmd(mhba, cmd); 2138f0c568a4SJianyun Li spin_unlock_irqrestore(mhba->shost->host_lock, flags); 2139f0c568a4SJianyun Li 21406600593cSChristoph Hellwig return BLK_EH_DONE; 2141f0c568a4SJianyun Li } 2142f0c568a4SJianyun Li 2143f0c568a4SJianyun Li static int 2144f0c568a4SJianyun Li mvumi_bios_param(struct scsi_device *sdev, struct block_device *bdev, 2145f0c568a4SJianyun Li sector_t capacity, int geom[]) 2146f0c568a4SJianyun Li { 2147f0c568a4SJianyun Li int heads, sectors; 2148f0c568a4SJianyun Li sector_t cylinders; 2149f0c568a4SJianyun Li unsigned long tmp; 2150f0c568a4SJianyun Li 2151f0c568a4SJianyun Li heads = 64; 2152f0c568a4SJianyun Li sectors = 32; 2153f0c568a4SJianyun Li tmp = heads * sectors; 2154f0c568a4SJianyun Li cylinders = capacity; 2155f0c568a4SJianyun Li sector_div(cylinders, tmp); 2156f0c568a4SJianyun Li 2157f0c568a4SJianyun Li if (capacity >= 0x200000) { 2158f0c568a4SJianyun Li heads = 255; 2159f0c568a4SJianyun Li sectors = 63; 2160f0c568a4SJianyun Li tmp = heads * sectors; 2161f0c568a4SJianyun Li cylinders = capacity; 2162f0c568a4SJianyun Li sector_div(cylinders, tmp); 2163f0c568a4SJianyun Li } 2164f0c568a4SJianyun Li geom[0] = heads; 2165f0c568a4SJianyun Li geom[1] = sectors; 2166f0c568a4SJianyun Li geom[2] = cylinders; 2167f0c568a4SJianyun Li 2168f0c568a4SJianyun Li return 0; 2169f0c568a4SJianyun Li } 2170f0c568a4SJianyun Li 2171f0c568a4SJianyun Li static struct scsi_host_template mvumi_template = { 2172f0c568a4SJianyun Li 2173f0c568a4SJianyun Li .module = THIS_MODULE, 2174f0c568a4SJianyun Li .name = "Marvell Storage Controller", 2175f0c568a4SJianyun Li .slave_configure = mvumi_slave_configure, 2176f0c568a4SJianyun Li .queuecommand = mvumi_queue_command, 2177103eb3b5SChristoph Hellwig .eh_timed_out = mvumi_timed_out, 2178f0c568a4SJianyun Li .eh_host_reset_handler = mvumi_host_reset, 2179f0c568a4SJianyun Li .bios_param = mvumi_bios_param, 21804af14d11SChristoph Hellwig .dma_boundary = PAGE_SIZE - 1, 2181f0c568a4SJianyun Li .this_id = -1, 2182f0c568a4SJianyun Li }; 2183f0c568a4SJianyun Li 2184bd756ddeSShun Fu static int mvumi_cfg_hw_reg(struct mvumi_hba *mhba) 2185bd756ddeSShun Fu { 2186bd756ddeSShun Fu void *base = NULL; 2187bd756ddeSShun Fu struct mvumi_hw_regs *regs; 2188bd756ddeSShun Fu 2189bd756ddeSShun Fu switch (mhba->pdev->device) { 2190bd756ddeSShun Fu case PCI_DEVICE_ID_MARVELL_MV9143: 2191bd756ddeSShun Fu mhba->mmio = mhba->base_addr[0]; 2192bd756ddeSShun Fu base = mhba->mmio; 2193bd756ddeSShun Fu if (!mhba->regs) { 2194bd756ddeSShun Fu mhba->regs = kzalloc(sizeof(*regs), GFP_KERNEL); 2195bd756ddeSShun Fu if (mhba->regs == NULL) 2196bd756ddeSShun Fu return -ENOMEM; 2197bd756ddeSShun Fu } 2198bd756ddeSShun Fu regs = mhba->regs; 2199bd756ddeSShun Fu 2200bd756ddeSShun Fu /* For Arm */ 2201bd756ddeSShun Fu regs->ctrl_sts_reg = base + 0x20104; 2202bd756ddeSShun Fu regs->rstoutn_mask_reg = base + 0x20108; 2203bd756ddeSShun Fu regs->sys_soft_rst_reg = base + 0x2010C; 2204bd756ddeSShun Fu regs->main_int_cause_reg = base + 0x20200; 2205bd756ddeSShun Fu regs->enpointa_mask_reg = base + 0x2020C; 2206bd756ddeSShun Fu regs->rstoutn_en_reg = base + 0xF1400; 2207bd756ddeSShun Fu /* For Doorbell */ 2208bd756ddeSShun Fu regs->pciea_to_arm_drbl_reg = base + 0x20400; 2209bd756ddeSShun Fu regs->arm_to_pciea_drbl_reg = base + 0x20408; 2210bd756ddeSShun Fu regs->arm_to_pciea_mask_reg = base + 0x2040C; 2211bd756ddeSShun Fu regs->pciea_to_arm_msg0 = base + 0x20430; 2212bd756ddeSShun Fu regs->pciea_to_arm_msg1 = base + 0x20434; 2213bd756ddeSShun Fu regs->arm_to_pciea_msg0 = base + 0x20438; 2214bd756ddeSShun Fu regs->arm_to_pciea_msg1 = base + 0x2043C; 2215bd756ddeSShun Fu 2216bd756ddeSShun Fu /* For Message Unit */ 2217bd756ddeSShun Fu 2218bd756ddeSShun Fu regs->inb_aval_count_basel = base + 0x508; 2219bd756ddeSShun Fu regs->inb_aval_count_baseh = base + 0x50C; 2220bd756ddeSShun Fu regs->inb_write_pointer = base + 0x518; 2221bd756ddeSShun Fu regs->inb_read_pointer = base + 0x51C; 2222bd756ddeSShun Fu regs->outb_coal_cfg = base + 0x568; 2223bd756ddeSShun Fu regs->outb_copy_basel = base + 0x5B0; 2224bd756ddeSShun Fu regs->outb_copy_baseh = base + 0x5B4; 2225bd756ddeSShun Fu regs->outb_copy_pointer = base + 0x544; 2226bd756ddeSShun Fu regs->outb_read_pointer = base + 0x548; 2227bd756ddeSShun Fu regs->outb_isr_cause = base + 0x560; 2228bd756ddeSShun Fu regs->outb_coal_cfg = base + 0x568; 2229bd756ddeSShun Fu /* Bit setting for HW */ 2230bd756ddeSShun Fu regs->int_comaout = 1 << 8; 2231bd756ddeSShun Fu regs->int_comaerr = 1 << 6; 2232bd756ddeSShun Fu regs->int_dl_cpu2pciea = 1 << 1; 2233bd756ddeSShun Fu regs->cl_pointer_toggle = 1 << 12; 2234bd756ddeSShun Fu regs->clic_irq = 1 << 1; 2235bd756ddeSShun Fu regs->clic_in_err = 1 << 8; 2236bd756ddeSShun Fu regs->clic_out_err = 1 << 12; 2237bd756ddeSShun Fu regs->cl_slot_num_mask = 0xFFF; 2238bd756ddeSShun Fu regs->int_drbl_int_mask = 0x3FFFFFFF; 2239bd756ddeSShun Fu regs->int_mu = regs->int_dl_cpu2pciea | regs->int_comaout | 2240bd756ddeSShun Fu regs->int_comaerr; 2241bd756ddeSShun Fu break; 2242bd756ddeSShun Fu case PCI_DEVICE_ID_MARVELL_MV9580: 2243bd756ddeSShun Fu mhba->mmio = mhba->base_addr[2]; 2244bd756ddeSShun Fu base = mhba->mmio; 2245bd756ddeSShun Fu if (!mhba->regs) { 2246bd756ddeSShun Fu mhba->regs = kzalloc(sizeof(*regs), GFP_KERNEL); 2247bd756ddeSShun Fu if (mhba->regs == NULL) 2248bd756ddeSShun Fu return -ENOMEM; 2249bd756ddeSShun Fu } 2250bd756ddeSShun Fu regs = mhba->regs; 2251bd756ddeSShun Fu /* For Arm */ 2252bd756ddeSShun Fu regs->ctrl_sts_reg = base + 0x20104; 2253bd756ddeSShun Fu regs->rstoutn_mask_reg = base + 0x1010C; 2254bd756ddeSShun Fu regs->sys_soft_rst_reg = base + 0x10108; 2255bd756ddeSShun Fu regs->main_int_cause_reg = base + 0x10200; 2256bd756ddeSShun Fu regs->enpointa_mask_reg = base + 0x1020C; 2257bd756ddeSShun Fu regs->rstoutn_en_reg = base + 0xF1400; 2258bd756ddeSShun Fu 2259bd756ddeSShun Fu /* For Doorbell */ 2260bd756ddeSShun Fu regs->pciea_to_arm_drbl_reg = base + 0x10460; 2261bd756ddeSShun Fu regs->arm_to_pciea_drbl_reg = base + 0x10480; 2262bd756ddeSShun Fu regs->arm_to_pciea_mask_reg = base + 0x10484; 2263bd756ddeSShun Fu regs->pciea_to_arm_msg0 = base + 0x10400; 2264bd756ddeSShun Fu regs->pciea_to_arm_msg1 = base + 0x10404; 2265bd756ddeSShun Fu regs->arm_to_pciea_msg0 = base + 0x10420; 2266bd756ddeSShun Fu regs->arm_to_pciea_msg1 = base + 0x10424; 2267bd756ddeSShun Fu 2268bd756ddeSShun Fu /* For reset*/ 2269bd756ddeSShun Fu regs->reset_request = base + 0x10108; 2270bd756ddeSShun Fu regs->reset_enable = base + 0x1010c; 2271bd756ddeSShun Fu 2272bd756ddeSShun Fu /* For Message Unit */ 2273bd756ddeSShun Fu regs->inb_aval_count_basel = base + 0x4008; 2274bd756ddeSShun Fu regs->inb_aval_count_baseh = base + 0x400C; 2275bd756ddeSShun Fu regs->inb_write_pointer = base + 0x4018; 2276bd756ddeSShun Fu regs->inb_read_pointer = base + 0x401C; 2277bd756ddeSShun Fu regs->outb_copy_basel = base + 0x4058; 2278bd756ddeSShun Fu regs->outb_copy_baseh = base + 0x405C; 2279bd756ddeSShun Fu regs->outb_copy_pointer = base + 0x406C; 2280bd756ddeSShun Fu regs->outb_read_pointer = base + 0x4070; 2281bd756ddeSShun Fu regs->outb_coal_cfg = base + 0x4080; 2282bd756ddeSShun Fu regs->outb_isr_cause = base + 0x4088; 2283bd756ddeSShun Fu /* Bit setting for HW */ 2284bd756ddeSShun Fu regs->int_comaout = 1 << 4; 2285bd756ddeSShun Fu regs->int_dl_cpu2pciea = 1 << 12; 2286bd756ddeSShun Fu regs->int_comaerr = 1 << 29; 2287bd756ddeSShun Fu regs->cl_pointer_toggle = 1 << 14; 2288bd756ddeSShun Fu regs->cl_slot_num_mask = 0x3FFF; 2289bd756ddeSShun Fu regs->clic_irq = 1 << 0; 2290bd756ddeSShun Fu regs->clic_out_err = 1 << 1; 2291bd756ddeSShun Fu regs->int_drbl_int_mask = 0x3FFFFFFF; 2292bd756ddeSShun Fu regs->int_mu = regs->int_dl_cpu2pciea | regs->int_comaout; 2293bd756ddeSShun Fu break; 2294bd756ddeSShun Fu default: 2295bd756ddeSShun Fu return -1; 2296bd756ddeSShun Fu } 2297bd756ddeSShun Fu 2298bd756ddeSShun Fu return 0; 2299bd756ddeSShun Fu } 2300bd756ddeSShun Fu 2301f0c568a4SJianyun Li /** 2302f0c568a4SJianyun Li * mvumi_init_fw - Initializes the FW 2303f0c568a4SJianyun Li * @mhba: Adapter soft state 2304f0c568a4SJianyun Li * 2305f0c568a4SJianyun Li * This is the main function for initializing firmware. 2306f0c568a4SJianyun Li */ 2307f0c568a4SJianyun Li static int mvumi_init_fw(struct mvumi_hba *mhba) 2308f0c568a4SJianyun Li { 2309f0c568a4SJianyun Li int ret = 0; 2310f0c568a4SJianyun Li 2311f0c568a4SJianyun Li if (pci_request_regions(mhba->pdev, MV_DRIVER_NAME)) { 2312f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, "IO memory region busy!\n"); 2313f0c568a4SJianyun Li return -EBUSY; 2314f0c568a4SJianyun Li } 2315f0c568a4SJianyun Li ret = mvumi_map_pci_addr(mhba->pdev, mhba->base_addr); 2316f0c568a4SJianyun Li if (ret) 2317f0c568a4SJianyun Li goto fail_ioremap; 2318f0c568a4SJianyun Li 2319f0c568a4SJianyun Li switch (mhba->pdev->device) { 2320f0c568a4SJianyun Li case PCI_DEVICE_ID_MARVELL_MV9143: 2321bd756ddeSShun Fu mhba->instancet = &mvumi_instance_9143; 2322f0c568a4SJianyun Li mhba->io_seq = 0; 2323f0c568a4SJianyun Li mhba->max_sge = MVUMI_MAX_SG_ENTRY; 2324f0c568a4SJianyun Li mhba->request_id_enabled = 1; 2325f0c568a4SJianyun Li break; 2326bd756ddeSShun Fu case PCI_DEVICE_ID_MARVELL_MV9580: 2327bd756ddeSShun Fu mhba->instancet = &mvumi_instance_9580; 2328bd756ddeSShun Fu mhba->io_seq = 0; 2329bd756ddeSShun Fu mhba->max_sge = MVUMI_MAX_SG_ENTRY; 2330bd756ddeSShun Fu break; 2331f0c568a4SJianyun Li default: 2332f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, "device 0x%x not supported!\n", 2333f0c568a4SJianyun Li mhba->pdev->device); 2334f0c568a4SJianyun Li mhba->instancet = NULL; 2335f0c568a4SJianyun Li ret = -EINVAL; 2336f0c568a4SJianyun Li goto fail_alloc_mem; 2337f0c568a4SJianyun Li } 2338f0c568a4SJianyun Li dev_dbg(&mhba->pdev->dev, "device id : %04X is found.\n", 2339f0c568a4SJianyun Li mhba->pdev->device); 2340bd756ddeSShun Fu ret = mvumi_cfg_hw_reg(mhba); 2341bd756ddeSShun Fu if (ret) { 2342bd756ddeSShun Fu dev_err(&mhba->pdev->dev, 2343bd756ddeSShun Fu "failed to allocate memory for reg\n"); 2344bd756ddeSShun Fu ret = -ENOMEM; 2345bd756ddeSShun Fu goto fail_alloc_mem; 2346bd756ddeSShun Fu } 2347ab8e7f4bSChristoph Hellwig mhba->handshake_page = dma_alloc_coherent(&mhba->pdev->dev, 2348ab8e7f4bSChristoph Hellwig HSP_MAX_SIZE, &mhba->handshake_page_phys, GFP_KERNEL); 2349f0c568a4SJianyun Li if (!mhba->handshake_page) { 2350f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, 2351f0c568a4SJianyun Li "failed to allocate memory for handshake\n"); 2352f0c568a4SJianyun Li ret = -ENOMEM; 2353bd756ddeSShun Fu goto fail_alloc_page; 2354f0c568a4SJianyun Li } 2355f0c568a4SJianyun Li 2356f0c568a4SJianyun Li if (mvumi_start(mhba)) { 2357f0c568a4SJianyun Li ret = -EINVAL; 2358f0c568a4SJianyun Li goto fail_ready_state; 2359f0c568a4SJianyun Li } 2360f0c568a4SJianyun Li ret = mvumi_alloc_cmds(mhba); 2361f0c568a4SJianyun Li if (ret) 2362f0c568a4SJianyun Li goto fail_ready_state; 2363f0c568a4SJianyun Li 2364f0c568a4SJianyun Li return 0; 2365f0c568a4SJianyun Li 2366f0c568a4SJianyun Li fail_ready_state: 2367f0c568a4SJianyun Li mvumi_release_mem_resource(mhba); 2368ab8e7f4bSChristoph Hellwig dma_free_coherent(&mhba->pdev->dev, HSP_MAX_SIZE, 2369bd756ddeSShun Fu mhba->handshake_page, mhba->handshake_page_phys); 2370bd756ddeSShun Fu fail_alloc_page: 2371bd756ddeSShun Fu kfree(mhba->regs); 2372f0c568a4SJianyun Li fail_alloc_mem: 2373f0c568a4SJianyun Li mvumi_unmap_pci_addr(mhba->pdev, mhba->base_addr); 2374f0c568a4SJianyun Li fail_ioremap: 2375f0c568a4SJianyun Li pci_release_regions(mhba->pdev); 2376f0c568a4SJianyun Li 2377f0c568a4SJianyun Li return ret; 2378f0c568a4SJianyun Li } 2379f0c568a4SJianyun Li 2380f0c568a4SJianyun Li /** 2381f0c568a4SJianyun Li * mvumi_io_attach - Attaches this driver to SCSI mid-layer 2382f0c568a4SJianyun Li * @mhba: Adapter soft state 2383f0c568a4SJianyun Li */ 2384f0c568a4SJianyun Li static int mvumi_io_attach(struct mvumi_hba *mhba) 2385f0c568a4SJianyun Li { 2386f0c568a4SJianyun Li struct Scsi_Host *host = mhba->shost; 2387bd756ddeSShun Fu struct scsi_device *sdev = NULL; 2388f0c568a4SJianyun Li int ret; 2389f0c568a4SJianyun Li unsigned int max_sg = (mhba->ib_max_size + 4 - 2390f0c568a4SJianyun Li sizeof(struct mvumi_msg_frame)) / sizeof(struct mvumi_sgl); 2391f0c568a4SJianyun Li 2392f0c568a4SJianyun Li host->irq = mhba->pdev->irq; 2393f0c568a4SJianyun Li host->unique_id = mhba->unique_id; 2394f0c568a4SJianyun Li host->can_queue = (mhba->max_io - 1) ? (mhba->max_io - 1) : 1; 2395f0c568a4SJianyun Li host->sg_tablesize = mhba->max_sge > max_sg ? max_sg : mhba->max_sge; 2396f0c568a4SJianyun Li host->max_sectors = mhba->max_transfer_size / 512; 2397f0c568a4SJianyun Li host->cmd_per_lun = (mhba->max_io - 1) ? (mhba->max_io - 1) : 1; 2398f0c568a4SJianyun Li host->max_id = mhba->max_target_id; 2399f0c568a4SJianyun Li host->max_cmd_len = MAX_COMMAND_SIZE; 2400f0c568a4SJianyun Li 2401f0c568a4SJianyun Li ret = scsi_add_host(host, &mhba->pdev->dev); 2402f0c568a4SJianyun Li if (ret) { 2403f0c568a4SJianyun Li dev_err(&mhba->pdev->dev, "scsi_add_host failed\n"); 2404f0c568a4SJianyun Li return ret; 2405f0c568a4SJianyun Li } 2406f0c568a4SJianyun Li mhba->fw_flag |= MVUMI_FW_ATTACH; 2407f0c568a4SJianyun Li 2408bd756ddeSShun Fu mutex_lock(&mhba->sas_discovery_mutex); 2409bd756ddeSShun Fu if (mhba->pdev->device == PCI_DEVICE_ID_MARVELL_MV9580) 2410bd756ddeSShun Fu ret = scsi_add_device(host, 0, mhba->max_target_id - 1, 0); 2411bd756ddeSShun Fu else 2412bd756ddeSShun Fu ret = 0; 2413bd756ddeSShun Fu if (ret) { 2414bd756ddeSShun Fu dev_err(&mhba->pdev->dev, "add virtual device failed\n"); 2415bd756ddeSShun Fu mutex_unlock(&mhba->sas_discovery_mutex); 2416bd756ddeSShun Fu goto fail_add_device; 2417bd756ddeSShun Fu } 2418bd756ddeSShun Fu 2419bd756ddeSShun Fu mhba->dm_thread = kthread_create(mvumi_rescan_bus, 2420bd756ddeSShun Fu mhba, "mvumi_scanthread"); 2421bd756ddeSShun Fu if (IS_ERR(mhba->dm_thread)) { 2422bd756ddeSShun Fu dev_err(&mhba->pdev->dev, 2423bd756ddeSShun Fu "failed to create device scan thread\n"); 2424055f15abSJing Xiangfeng ret = PTR_ERR(mhba->dm_thread); 2425bd756ddeSShun Fu mutex_unlock(&mhba->sas_discovery_mutex); 2426bd756ddeSShun Fu goto fail_create_thread; 2427bd756ddeSShun Fu } 2428bd756ddeSShun Fu atomic_set(&mhba->pnp_count, 1); 2429bd756ddeSShun Fu wake_up_process(mhba->dm_thread); 2430bd756ddeSShun Fu 2431bd756ddeSShun Fu mutex_unlock(&mhba->sas_discovery_mutex); 2432f0c568a4SJianyun Li return 0; 2433bd756ddeSShun Fu 2434bd756ddeSShun Fu fail_create_thread: 2435bd756ddeSShun Fu if (mhba->pdev->device == PCI_DEVICE_ID_MARVELL_MV9580) 2436bd756ddeSShun Fu sdev = scsi_device_lookup(mhba->shost, 0, 2437bd756ddeSShun Fu mhba->max_target_id - 1, 0); 2438bd756ddeSShun Fu if (sdev) { 2439bd756ddeSShun Fu scsi_remove_device(sdev); 2440bd756ddeSShun Fu scsi_device_put(sdev); 2441bd756ddeSShun Fu } 2442bd756ddeSShun Fu fail_add_device: 2443bd756ddeSShun Fu scsi_remove_host(mhba->shost); 2444bd756ddeSShun Fu return ret; 2445f0c568a4SJianyun Li } 2446f0c568a4SJianyun Li 2447f0c568a4SJianyun Li /** 2448f0c568a4SJianyun Li * mvumi_probe_one - PCI hotplug entry point 2449f0c568a4SJianyun Li * @pdev: PCI device structure 2450f0c568a4SJianyun Li * @id: PCI ids of supported hotplugged adapter 2451f0c568a4SJianyun Li */ 24526f039790SGreg Kroah-Hartman static int mvumi_probe_one(struct pci_dev *pdev, const struct pci_device_id *id) 2453f0c568a4SJianyun Li { 2454f0c568a4SJianyun Li struct Scsi_Host *host; 2455f0c568a4SJianyun Li struct mvumi_hba *mhba; 2456f0c568a4SJianyun Li int ret; 2457f0c568a4SJianyun Li 2458f0c568a4SJianyun Li dev_dbg(&pdev->dev, " %#4.04x:%#4.04x:%#4.04x:%#4.04x: ", 2459f0c568a4SJianyun Li pdev->vendor, pdev->device, pdev->subsystem_vendor, 2460f0c568a4SJianyun Li pdev->subsystem_device); 2461f0c568a4SJianyun Li 2462f0c568a4SJianyun Li ret = pci_enable_device(pdev); 2463f0c568a4SJianyun Li if (ret) 2464f0c568a4SJianyun Li return ret; 2465f0c568a4SJianyun Li 2466ab8e7f4bSChristoph Hellwig ret = mvumi_pci_set_master(pdev); 2467f0c568a4SJianyun Li if (ret) 2468f0c568a4SJianyun Li goto fail_set_dma_mask; 2469f0c568a4SJianyun Li 2470f0c568a4SJianyun Li host = scsi_host_alloc(&mvumi_template, sizeof(*mhba)); 2471f0c568a4SJianyun Li if (!host) { 2472f0c568a4SJianyun Li dev_err(&pdev->dev, "scsi_host_alloc failed\n"); 2473f0c568a4SJianyun Li ret = -ENOMEM; 2474f0c568a4SJianyun Li goto fail_alloc_instance; 2475f0c568a4SJianyun Li } 2476f0c568a4SJianyun Li mhba = shost_priv(host); 2477f0c568a4SJianyun Li 2478f0c568a4SJianyun Li INIT_LIST_HEAD(&mhba->cmd_pool); 2479f0c568a4SJianyun Li INIT_LIST_HEAD(&mhba->ob_data_list); 2480f0c568a4SJianyun Li INIT_LIST_HEAD(&mhba->free_ob_list); 2481f0c568a4SJianyun Li INIT_LIST_HEAD(&mhba->res_list); 2482f0c568a4SJianyun Li INIT_LIST_HEAD(&mhba->waiting_req_list); 2483bd756ddeSShun Fu mutex_init(&mhba->device_lock); 2484bd756ddeSShun Fu INIT_LIST_HEAD(&mhba->mhba_dev_list); 2485bd756ddeSShun Fu INIT_LIST_HEAD(&mhba->shost_dev_list); 2486f0c568a4SJianyun Li atomic_set(&mhba->fw_outstanding, 0); 2487f0c568a4SJianyun Li init_waitqueue_head(&mhba->int_cmd_wait_q); 2488bd756ddeSShun Fu mutex_init(&mhba->sas_discovery_mutex); 2489f0c568a4SJianyun Li 2490f0c568a4SJianyun Li mhba->pdev = pdev; 2491f0c568a4SJianyun Li mhba->shost = host; 2492f0c568a4SJianyun Li mhba->unique_id = pdev->bus->number << 8 | pdev->devfn; 2493f0c568a4SJianyun Li 2494f0c568a4SJianyun Li ret = mvumi_init_fw(mhba); 2495f0c568a4SJianyun Li if (ret) 2496f0c568a4SJianyun Li goto fail_init_fw; 2497f0c568a4SJianyun Li 2498f0c568a4SJianyun Li ret = request_irq(mhba->pdev->irq, mvumi_isr_handler, IRQF_SHARED, 2499f0c568a4SJianyun Li "mvumi", mhba); 2500f0c568a4SJianyun Li if (ret) { 2501f0c568a4SJianyun Li dev_err(&pdev->dev, "failed to register IRQ\n"); 2502f0c568a4SJianyun Li goto fail_init_irq; 2503f0c568a4SJianyun Li } 2504bd756ddeSShun Fu 2505bd756ddeSShun Fu mhba->instancet->enable_intr(mhba); 2506f0c568a4SJianyun Li pci_set_drvdata(pdev, mhba); 2507f0c568a4SJianyun Li 2508f0c568a4SJianyun Li ret = mvumi_io_attach(mhba); 2509f0c568a4SJianyun Li if (ret) 2510f0c568a4SJianyun Li goto fail_io_attach; 2511bd756ddeSShun Fu 2512bd756ddeSShun Fu mvumi_backup_bar_addr(mhba); 2513f0c568a4SJianyun Li dev_dbg(&pdev->dev, "probe mvumi driver successfully.\n"); 2514f0c568a4SJianyun Li 2515f0c568a4SJianyun Li return 0; 2516f0c568a4SJianyun Li 2517f0c568a4SJianyun Li fail_io_attach: 2518bd756ddeSShun Fu mhba->instancet->disable_intr(mhba); 2519f0c568a4SJianyun Li free_irq(mhba->pdev->irq, mhba); 2520f0c568a4SJianyun Li fail_init_irq: 2521f0c568a4SJianyun Li mvumi_release_fw(mhba); 2522f0c568a4SJianyun Li fail_init_fw: 2523f0c568a4SJianyun Li scsi_host_put(host); 2524f0c568a4SJianyun Li 2525f0c568a4SJianyun Li fail_alloc_instance: 2526f0c568a4SJianyun Li fail_set_dma_mask: 2527f0c568a4SJianyun Li pci_disable_device(pdev); 2528f0c568a4SJianyun Li 2529f0c568a4SJianyun Li return ret; 2530f0c568a4SJianyun Li } 2531f0c568a4SJianyun Li 2532f0c568a4SJianyun Li static void mvumi_detach_one(struct pci_dev *pdev) 2533f0c568a4SJianyun Li { 2534f0c568a4SJianyun Li struct Scsi_Host *host; 2535f0c568a4SJianyun Li struct mvumi_hba *mhba; 2536f0c568a4SJianyun Li 2537f0c568a4SJianyun Li mhba = pci_get_drvdata(pdev); 2538bd756ddeSShun Fu if (mhba->dm_thread) { 2539bd756ddeSShun Fu kthread_stop(mhba->dm_thread); 2540bd756ddeSShun Fu mhba->dm_thread = NULL; 2541bd756ddeSShun Fu } 2542bd756ddeSShun Fu 2543bd756ddeSShun Fu mvumi_detach_devices(mhba); 2544f0c568a4SJianyun Li host = mhba->shost; 2545f0c568a4SJianyun Li scsi_remove_host(mhba->shost); 2546f0c568a4SJianyun Li mvumi_flush_cache(mhba); 2547f0c568a4SJianyun Li 2548bd756ddeSShun Fu mhba->instancet->disable_intr(mhba); 2549f0c568a4SJianyun Li free_irq(mhba->pdev->irq, mhba); 2550f0c568a4SJianyun Li mvumi_release_fw(mhba); 2551f0c568a4SJianyun Li scsi_host_put(host); 2552f0c568a4SJianyun Li pci_disable_device(pdev); 2553f0c568a4SJianyun Li dev_dbg(&pdev->dev, "driver is removed!\n"); 2554f0c568a4SJianyun Li } 2555f0c568a4SJianyun Li 2556f0c568a4SJianyun Li /** 2557f0c568a4SJianyun Li * mvumi_shutdown - Shutdown entry point 255853fdec73SVaibhav Gupta * @pdev: PCI device structure 2559f0c568a4SJianyun Li */ 2560f0c568a4SJianyun Li static void mvumi_shutdown(struct pci_dev *pdev) 2561f0c568a4SJianyun Li { 2562f0c568a4SJianyun Li struct mvumi_hba *mhba = pci_get_drvdata(pdev); 2563f0c568a4SJianyun Li 2564f0c568a4SJianyun Li mvumi_flush_cache(mhba); 2565f0c568a4SJianyun Li } 2566f0c568a4SJianyun Li 25670572edbcSVaibhav Gupta static int __maybe_unused mvumi_suspend(struct device *dev) 2568f0c568a4SJianyun Li { 25690572edbcSVaibhav Gupta struct pci_dev *pdev = to_pci_dev(dev); 25700572edbcSVaibhav Gupta struct mvumi_hba *mhba = pci_get_drvdata(pdev); 2571f0c568a4SJianyun Li 2572f0c568a4SJianyun Li mvumi_flush_cache(mhba); 2573f0c568a4SJianyun Li 2574bd756ddeSShun Fu mhba->instancet->disable_intr(mhba); 2575f0c568a4SJianyun Li mvumi_unmap_pci_addr(pdev, mhba->base_addr); 2576f0c568a4SJianyun Li 2577f0c568a4SJianyun Li return 0; 2578f0c568a4SJianyun Li } 2579f0c568a4SJianyun Li 25800572edbcSVaibhav Gupta static int __maybe_unused mvumi_resume(struct device *dev) 2581f0c568a4SJianyun Li { 2582f0c568a4SJianyun Li int ret; 25830572edbcSVaibhav Gupta struct pci_dev *pdev = to_pci_dev(dev); 25840572edbcSVaibhav Gupta struct mvumi_hba *mhba = pci_get_drvdata(pdev); 2585f0c568a4SJianyun Li 2586bddbd00cSChristoph Hellwig ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32)); 2587f0c568a4SJianyun Li if (ret) 2588f0c568a4SJianyun Li goto fail; 2589f0c568a4SJianyun Li ret = mvumi_map_pci_addr(mhba->pdev, mhba->base_addr); 2590f0c568a4SJianyun Li if (ret) 2591f0c568a4SJianyun Li goto release_regions; 2592f0c568a4SJianyun Li 2593bd756ddeSShun Fu if (mvumi_cfg_hw_reg(mhba)) { 2594bd756ddeSShun Fu ret = -EINVAL; 2595bd756ddeSShun Fu goto unmap_pci_addr; 2596bd756ddeSShun Fu } 2597bd756ddeSShun Fu 2598f0c568a4SJianyun Li mhba->mmio = mhba->base_addr[0]; 2599bd756ddeSShun Fu mvumi_reset(mhba); 2600f0c568a4SJianyun Li 2601f0c568a4SJianyun Li if (mvumi_start(mhba)) { 2602f0c568a4SJianyun Li ret = -EINVAL; 2603f0c568a4SJianyun Li goto unmap_pci_addr; 2604f0c568a4SJianyun Li } 2605f0c568a4SJianyun Li 2606bd756ddeSShun Fu mhba->instancet->enable_intr(mhba); 2607f0c568a4SJianyun Li 2608f0c568a4SJianyun Li return 0; 2609f0c568a4SJianyun Li 2610f0c568a4SJianyun Li unmap_pci_addr: 2611f0c568a4SJianyun Li mvumi_unmap_pci_addr(pdev, mhba->base_addr); 2612f0c568a4SJianyun Li release_regions: 2613f0c568a4SJianyun Li pci_release_regions(pdev); 2614f0c568a4SJianyun Li fail: 2615f0c568a4SJianyun Li 2616f0c568a4SJianyun Li return ret; 2617f0c568a4SJianyun Li } 2618f0c568a4SJianyun Li 26190572edbcSVaibhav Gupta static SIMPLE_DEV_PM_OPS(mvumi_pm_ops, mvumi_suspend, mvumi_resume); 26200572edbcSVaibhav Gupta 2621f0c568a4SJianyun Li static struct pci_driver mvumi_pci_driver = { 2622f0c568a4SJianyun Li 2623f0c568a4SJianyun Li .name = MV_DRIVER_NAME, 2624f0c568a4SJianyun Li .id_table = mvumi_pci_table, 2625f0c568a4SJianyun Li .probe = mvumi_probe_one, 26266f039790SGreg Kroah-Hartman .remove = mvumi_detach_one, 2627f0c568a4SJianyun Li .shutdown = mvumi_shutdown, 26280572edbcSVaibhav Gupta .driver.pm = &mvumi_pm_ops, 2629f0c568a4SJianyun Li }; 2630f0c568a4SJianyun Li 2631f9c25ccfSYueHaibing module_pci_driver(mvumi_pci_driver); 2632