jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1 | /* |
Sakthivel K | f586099 | 2013-04-17 16:37:02 +0530 | [diff] [blame] | 2 | * PMC-Sierra PM8001/8081/8088/8089 SAS/SATA based host adapters driver |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2008-2009 USI Co., Ltd. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions, and the following disclaimer, |
| 12 | * without modification. |
| 13 | * 2. Redistributions in binary form must reproduce at minimum a disclaimer |
| 14 | * substantially similar to the "NO WARRANTY" disclaimer below |
| 15 | * ("Disclaimer") and any redistribution must be conditioned upon |
| 16 | * including a substantially similar Disclaimer requirement for further |
| 17 | * binary redistribution. |
| 18 | * 3. Neither the names of the above-listed copyright holders nor the names |
| 19 | * of any contributors may be used to endorse or promote products derived |
| 20 | * from this software without specific prior written permission. |
| 21 | * |
| 22 | * Alternatively, this software may be distributed under the terms of the |
| 23 | * GNU General Public License ("GPL") version 2 as published by the Free |
| 24 | * Software Foundation. |
| 25 | * |
| 26 | * NO WARRANTY |
| 27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 28 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 29 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR |
| 30 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 31 | * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 33 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 34 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 35 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 36 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 37 | * POSSIBILITY OF SUCH DAMAGES. |
| 38 | * |
| 39 | */ |
| 40 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 41 | #include <linux/slab.h> |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 42 | #include "pm8001_sas.h" |
| 43 | |
| 44 | /** |
| 45 | * pm8001_find_tag - from sas task to find out tag that belongs to this task |
| 46 | * @task: the task sent to the LLDD |
| 47 | * @tag: the found tag associated with the task |
| 48 | */ |
| 49 | static int pm8001_find_tag(struct sas_task *task, u32 *tag) |
| 50 | { |
| 51 | if (task->lldd_task) { |
| 52 | struct pm8001_ccb_info *ccb; |
| 53 | ccb = task->lldd_task; |
| 54 | *tag = ccb->ccb_tag; |
| 55 | return 1; |
| 56 | } |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | /** |
Tomas Henzl | ef30054 | 2014-07-09 17:20:40 +0530 | [diff] [blame] | 61 | * pm8001_tag_free - free the no more needed tag |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 62 | * @pm8001_ha: our hba struct |
| 63 | * @tag: the found tag associated with the task |
| 64 | */ |
Tomas Henzl | ef30054 | 2014-07-09 17:20:40 +0530 | [diff] [blame] | 65 | void pm8001_tag_free(struct pm8001_hba_info *pm8001_ha, u32 tag) |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 66 | { |
| 67 | void *bitmap = pm8001_ha->tags; |
| 68 | clear_bit(tag, bitmap); |
| 69 | } |
| 70 | |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 71 | /** |
| 72 | * pm8001_tag_alloc - allocate a empty tag for task used. |
| 73 | * @pm8001_ha: our hba struct |
| 74 | * @tag_out: the found empty tag . |
| 75 | */ |
| 76 | inline int pm8001_tag_alloc(struct pm8001_hba_info *pm8001_ha, u32 *tag_out) |
| 77 | { |
Tomas Henzl | ef30054 | 2014-07-09 17:20:40 +0530 | [diff] [blame] | 78 | unsigned int tag; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 79 | void *bitmap = pm8001_ha->tags; |
Tomas Henzl | 646cdf0 | 2014-07-09 17:21:01 +0530 | [diff] [blame] | 80 | unsigned long flags; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 81 | |
Tomas Henzl | 646cdf0 | 2014-07-09 17:21:01 +0530 | [diff] [blame] | 82 | spin_lock_irqsave(&pm8001_ha->bitmap_lock, flags); |
Tomas Henzl | ef30054 | 2014-07-09 17:20:40 +0530 | [diff] [blame] | 83 | tag = find_first_zero_bit(bitmap, pm8001_ha->tags_num); |
Tomas Henzl | 646cdf0 | 2014-07-09 17:21:01 +0530 | [diff] [blame] | 84 | if (tag >= pm8001_ha->tags_num) { |
| 85 | spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 86 | return -SAS_QUEUE_FULL; |
Tomas Henzl | 646cdf0 | 2014-07-09 17:21:01 +0530 | [diff] [blame] | 87 | } |
Tomas Henzl | ef30054 | 2014-07-09 17:20:40 +0530 | [diff] [blame] | 88 | set_bit(tag, bitmap); |
Tomas Henzl | 646cdf0 | 2014-07-09 17:21:01 +0530 | [diff] [blame] | 89 | spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 90 | *tag_out = tag; |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | void pm8001_tag_init(struct pm8001_hba_info *pm8001_ha) |
| 95 | { |
| 96 | int i; |
| 97 | for (i = 0; i < pm8001_ha->tags_num; ++i) |
Tomas Henzl | ef30054 | 2014-07-09 17:20:40 +0530 | [diff] [blame] | 98 | pm8001_tag_free(pm8001_ha, i); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
| 102 | * pm8001_mem_alloc - allocate memory for pm8001. |
| 103 | * @pdev: pci device. |
| 104 | * @virt_addr: the allocated virtual address |
| 105 | * @pphys_addr_hi: the physical address high byte address. |
| 106 | * @pphys_addr_lo: the physical address low byte address. |
| 107 | * @mem_size: memory size. |
| 108 | */ |
| 109 | int pm8001_mem_alloc(struct pci_dev *pdev, void **virt_addr, |
| 110 | dma_addr_t *pphys_addr, u32 *pphys_addr_hi, |
| 111 | u32 *pphys_addr_lo, u32 mem_size, u32 align) |
| 112 | { |
| 113 | caddr_t mem_virt_alloc; |
| 114 | dma_addr_t mem_dma_handle; |
| 115 | u64 phys_align; |
| 116 | u64 align_offset = 0; |
| 117 | if (align) |
| 118 | align_offset = (dma_addr_t)align - 1; |
Christoph Hellwig | f73bdeb | 2018-10-10 19:59:50 +0200 | [diff] [blame] | 119 | mem_virt_alloc = dma_zalloc_coherent(&pdev->dev, mem_size + align, |
| 120 | &mem_dma_handle, GFP_KERNEL); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 121 | if (!mem_virt_alloc) { |
| 122 | pm8001_printk("memory allocation error\n"); |
| 123 | return -1; |
| 124 | } |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 125 | *pphys_addr = mem_dma_handle; |
| 126 | phys_align = (*pphys_addr + align_offset) & ~align_offset; |
| 127 | *virt_addr = (void *)mem_virt_alloc + phys_align - *pphys_addr; |
| 128 | *pphys_addr_hi = upper_32_bits(phys_align); |
| 129 | *pphys_addr_lo = lower_32_bits(phys_align); |
| 130 | return 0; |
| 131 | } |
| 132 | /** |
| 133 | * pm8001_find_ha_by_dev - from domain device which come from sas layer to |
| 134 | * find out our hba struct. |
| 135 | * @dev: the domain device which from sas layer. |
| 136 | */ |
| 137 | static |
| 138 | struct pm8001_hba_info *pm8001_find_ha_by_dev(struct domain_device *dev) |
| 139 | { |
| 140 | struct sas_ha_struct *sha = dev->port->ha; |
| 141 | struct pm8001_hba_info *pm8001_ha = sha->lldd_ha; |
| 142 | return pm8001_ha; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * pm8001_phy_control - this function should be registered to |
| 147 | * sas_domain_function_template to provide libsas used, note: this is just |
| 148 | * control the HBA phy rather than other expander phy if you want control |
| 149 | * other phy, you should use SMP command. |
| 150 | * @sas_phy: which phy in HBA phys. |
| 151 | * @func: the operation. |
| 152 | * @funcdata: always NULL. |
| 153 | */ |
| 154 | int pm8001_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func, |
| 155 | void *funcdata) |
| 156 | { |
| 157 | int rc = 0, phy_id = sas_phy->id; |
| 158 | struct pm8001_hba_info *pm8001_ha = NULL; |
| 159 | struct sas_phy_linkrates *rates; |
Deepak Ukey | cd13575 | 2018-09-11 14:18:02 +0530 | [diff] [blame] | 160 | struct sas_ha_struct *sas_ha; |
| 161 | struct pm8001_phy *phy; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 162 | DECLARE_COMPLETION_ONSTACK(completion); |
Mark Salyzyn | d95d000 | 2012-01-17 09:18:57 -0500 | [diff] [blame] | 163 | unsigned long flags; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 164 | pm8001_ha = sas_phy->ha->lldd_ha; |
Deepak Ukey | cd13575 | 2018-09-11 14:18:02 +0530 | [diff] [blame] | 165 | phy = &pm8001_ha->phy[phy_id]; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 166 | pm8001_ha->phy[phy_id].enable_completion = &completion; |
| 167 | switch (func) { |
| 168 | case PHY_FUNC_SET_LINK_RATE: |
| 169 | rates = funcdata; |
| 170 | if (rates->minimum_linkrate) { |
| 171 | pm8001_ha->phy[phy_id].minimum_linkrate = |
| 172 | rates->minimum_linkrate; |
| 173 | } |
| 174 | if (rates->maximum_linkrate) { |
| 175 | pm8001_ha->phy[phy_id].maximum_linkrate = |
| 176 | rates->maximum_linkrate; |
| 177 | } |
Deepak Ukey | cd13575 | 2018-09-11 14:18:02 +0530 | [diff] [blame] | 178 | if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) { |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 179 | PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id); |
| 180 | wait_for_completion(&completion); |
| 181 | } |
| 182 | PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id, |
| 183 | PHY_LINK_RESET); |
| 184 | break; |
| 185 | case PHY_FUNC_HARD_RESET: |
Deepak Ukey | cd13575 | 2018-09-11 14:18:02 +0530 | [diff] [blame] | 186 | if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) { |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 187 | PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id); |
| 188 | wait_for_completion(&completion); |
| 189 | } |
| 190 | PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id, |
| 191 | PHY_HARD_RESET); |
| 192 | break; |
| 193 | case PHY_FUNC_LINK_RESET: |
Deepak Ukey | cd13575 | 2018-09-11 14:18:02 +0530 | [diff] [blame] | 194 | if (pm8001_ha->phy[phy_id].phy_state == PHY_LINK_DISABLE) { |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 195 | PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id); |
| 196 | wait_for_completion(&completion); |
| 197 | } |
| 198 | PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id, |
| 199 | PHY_LINK_RESET); |
| 200 | break; |
| 201 | case PHY_FUNC_RELEASE_SPINUP_HOLD: |
| 202 | PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id, |
| 203 | PHY_LINK_RESET); |
| 204 | break; |
| 205 | case PHY_FUNC_DISABLE: |
Deepak Ukey | cd13575 | 2018-09-11 14:18:02 +0530 | [diff] [blame] | 206 | if (pm8001_ha->chip_id != chip_8001) { |
| 207 | if (pm8001_ha->phy[phy_id].phy_state == |
| 208 | PHY_STATE_LINK_UP_SPCV) { |
| 209 | sas_ha = pm8001_ha->sas; |
| 210 | sas_phy_disconnected(&phy->sas_phy); |
| 211 | sas_ha->notify_phy_event(&phy->sas_phy, |
| 212 | PHYE_LOSS_OF_SIGNAL); |
| 213 | phy->phy_attached = 0; |
| 214 | } |
| 215 | } else { |
| 216 | if (pm8001_ha->phy[phy_id].phy_state == |
| 217 | PHY_STATE_LINK_UP_SPC) { |
| 218 | sas_ha = pm8001_ha->sas; |
| 219 | sas_phy_disconnected(&phy->sas_phy); |
| 220 | sas_ha->notify_phy_event(&phy->sas_phy, |
| 221 | PHYE_LOSS_OF_SIGNAL); |
| 222 | phy->phy_attached = 0; |
| 223 | } |
| 224 | } |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 225 | PM8001_CHIP_DISP->phy_stop_req(pm8001_ha, phy_id); |
| 226 | break; |
Mark Salyzyn | d95d000 | 2012-01-17 09:18:57 -0500 | [diff] [blame] | 227 | case PHY_FUNC_GET_EVENTS: |
| 228 | spin_lock_irqsave(&pm8001_ha->lock, flags); |
Sakthivel K | f586099 | 2013-04-17 16:37:02 +0530 | [diff] [blame] | 229 | if (pm8001_ha->chip_id == chip_8001) { |
| 230 | if (-1 == pm8001_bar4_shift(pm8001_ha, |
Mark Salyzyn | d95d000 | 2012-01-17 09:18:57 -0500 | [diff] [blame] | 231 | (phy_id < 4) ? 0x30000 : 0x40000)) { |
Sakthivel K | f586099 | 2013-04-17 16:37:02 +0530 | [diff] [blame] | 232 | spin_unlock_irqrestore(&pm8001_ha->lock, flags); |
| 233 | return -EINVAL; |
| 234 | } |
Mark Salyzyn | d95d000 | 2012-01-17 09:18:57 -0500 | [diff] [blame] | 235 | } |
| 236 | { |
| 237 | struct sas_phy *phy = sas_phy->phy; |
| 238 | uint32_t *qp = (uint32_t *)(((char *) |
| 239 | pm8001_ha->io_mem[2].memvirtaddr) |
| 240 | + 0x1034 + (0x4000 * (phy_id & 3))); |
| 241 | |
| 242 | phy->invalid_dword_count = qp[0]; |
| 243 | phy->running_disparity_error_count = qp[1]; |
| 244 | phy->loss_of_dword_sync_count = qp[3]; |
| 245 | phy->phy_reset_problem_count = qp[4]; |
| 246 | } |
Sakthivel K | f586099 | 2013-04-17 16:37:02 +0530 | [diff] [blame] | 247 | if (pm8001_ha->chip_id == chip_8001) |
| 248 | pm8001_bar4_shift(pm8001_ha, 0); |
Mark Salyzyn | d95d000 | 2012-01-17 09:18:57 -0500 | [diff] [blame] | 249 | spin_unlock_irqrestore(&pm8001_ha->lock, flags); |
| 250 | return 0; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 251 | default: |
Mark Salyzyn | d95d000 | 2012-01-17 09:18:57 -0500 | [diff] [blame] | 252 | rc = -EOPNOTSUPP; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 253 | } |
| 254 | msleep(300); |
| 255 | return rc; |
| 256 | } |
| 257 | |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 258 | /** |
| 259 | * pm8001_scan_start - we should enable all HBA phys by sending the phy_start |
| 260 | * command to HBA. |
| 261 | * @shost: the scsi host data. |
| 262 | */ |
| 263 | void pm8001_scan_start(struct Scsi_Host *shost) |
| 264 | { |
| 265 | int i; |
| 266 | struct pm8001_hba_info *pm8001_ha; |
| 267 | struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost); |
| 268 | pm8001_ha = sha->lldd_ha; |
Sakthivel K | f586099 | 2013-04-17 16:37:02 +0530 | [diff] [blame] | 269 | /* SAS_RE_INITIALIZATION not available in SPCv/ve */ |
| 270 | if (pm8001_ha->chip_id == chip_8001) |
| 271 | PM8001_CHIP_DISP->sas_re_init_req(pm8001_ha); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 272 | for (i = 0; i < pm8001_ha->chip->n_phy; ++i) |
| 273 | PM8001_CHIP_DISP->phy_start_req(pm8001_ha, i); |
| 274 | } |
| 275 | |
| 276 | int pm8001_scan_finished(struct Scsi_Host *shost, unsigned long time) |
| 277 | { |
Dan Williams | b1124cd | 2011-12-19 16:42:34 -0800 | [diff] [blame] | 278 | struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); |
| 279 | |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 280 | /* give the phy enabling interrupt event time to come in (1s |
| 281 | * is empirically about all it takes) */ |
| 282 | if (time < HZ) |
| 283 | return 0; |
| 284 | /* Wait for discovery to finish */ |
Dan Williams | b1124cd | 2011-12-19 16:42:34 -0800 | [diff] [blame] | 285 | sas_drain_work(ha); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 286 | return 1; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * pm8001_task_prep_smp - the dispatcher function, prepare data for smp task |
| 291 | * @pm8001_ha: our hba card information |
| 292 | * @ccb: the ccb which attached to smp task |
| 293 | */ |
| 294 | static int pm8001_task_prep_smp(struct pm8001_hba_info *pm8001_ha, |
| 295 | struct pm8001_ccb_info *ccb) |
| 296 | { |
| 297 | return PM8001_CHIP_DISP->smp_req(pm8001_ha, ccb); |
| 298 | } |
| 299 | |
| 300 | u32 pm8001_get_ncq_tag(struct sas_task *task, u32 *tag) |
| 301 | { |
| 302 | struct ata_queued_cmd *qc = task->uldd_task; |
| 303 | if (qc) { |
| 304 | if (qc->tf.command == ATA_CMD_FPDMA_WRITE || |
Hannes Reinecke | ef026b1 | 2016-04-25 12:45:44 +0200 | [diff] [blame] | 305 | qc->tf.command == ATA_CMD_FPDMA_READ || |
| 306 | qc->tf.command == ATA_CMD_FPDMA_RECV || |
Hannes Reinecke | 661ce1f | 2016-04-25 12:45:45 +0200 | [diff] [blame] | 307 | qc->tf.command == ATA_CMD_FPDMA_SEND || |
| 308 | qc->tf.command == ATA_CMD_NCQ_NON_DATA) { |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 309 | *tag = qc->tag; |
| 310 | return 1; |
| 311 | } |
| 312 | } |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * pm8001_task_prep_ata - the dispatcher function, prepare data for sata task |
| 318 | * @pm8001_ha: our hba card information |
| 319 | * @ccb: the ccb which attached to sata task |
| 320 | */ |
| 321 | static int pm8001_task_prep_ata(struct pm8001_hba_info *pm8001_ha, |
| 322 | struct pm8001_ccb_info *ccb) |
| 323 | { |
| 324 | return PM8001_CHIP_DISP->sata_req(pm8001_ha, ccb); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * pm8001_task_prep_ssp_tm - the dispatcher function, prepare task management data |
| 329 | * @pm8001_ha: our hba card information |
| 330 | * @ccb: the ccb which attached to TM |
| 331 | * @tmf: the task management IU |
| 332 | */ |
| 333 | static int pm8001_task_prep_ssp_tm(struct pm8001_hba_info *pm8001_ha, |
| 334 | struct pm8001_ccb_info *ccb, struct pm8001_tmf_task *tmf) |
| 335 | { |
| 336 | return PM8001_CHIP_DISP->ssp_tm_req(pm8001_ha, ccb, tmf); |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * pm8001_task_prep_ssp - the dispatcher function,prepare ssp data for ssp task |
| 341 | * @pm8001_ha: our hba card information |
| 342 | * @ccb: the ccb which attached to ssp task |
| 343 | */ |
| 344 | static int pm8001_task_prep_ssp(struct pm8001_hba_info *pm8001_ha, |
| 345 | struct pm8001_ccb_info *ccb) |
| 346 | { |
| 347 | return PM8001_CHIP_DISP->ssp_io_req(pm8001_ha, ccb); |
| 348 | } |
Dan Williams | 11e1636 | 2011-09-20 15:11:03 -0700 | [diff] [blame] | 349 | |
jack wang | 1cc943a | 2009-12-07 17:22:42 +0800 | [diff] [blame] | 350 | /* Find the local port id that's attached to this device */ |
| 351 | static int sas_find_local_port_id(struct domain_device *dev) |
| 352 | { |
| 353 | struct domain_device *pdev = dev->parent; |
| 354 | |
| 355 | /* Directly attached device */ |
| 356 | if (!pdev) |
| 357 | return dev->port->id; |
| 358 | while (pdev) { |
| 359 | struct domain_device *pdev_p = pdev->parent; |
| 360 | if (!pdev_p) |
| 361 | return pdev->port->id; |
| 362 | pdev = pdev->parent; |
| 363 | } |
| 364 | return 0; |
| 365 | } |
| 366 | |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 367 | /** |
jack_wang | 97ee208 | 2009-11-05 22:33:51 +0800 | [diff] [blame] | 368 | * pm8001_task_exec - queue the task(ssp, smp && ata) to the hardware. |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 369 | * @task: the task to be execute. |
| 370 | * @num: if can_queue great than 1, the task can be queued up. for SMP task, |
| 371 | * we always execute one one time. |
| 372 | * @gfp_flags: gfp_flags. |
jack_wang | 97ee208 | 2009-11-05 22:33:51 +0800 | [diff] [blame] | 373 | * @is_tmf: if it is task management task. |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 374 | * @tmf: the task management IU |
| 375 | */ |
| 376 | #define DEV_IS_GONE(pm8001_dev) \ |
James Bottomley | aa9f832 | 2013-05-07 14:44:06 -0700 | [diff] [blame] | 377 | ((!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED))) |
Christoph Hellwig | 79855d1 | 2014-11-05 10:36:28 +0100 | [diff] [blame] | 378 | static int pm8001_task_exec(struct sas_task *task, |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 379 | gfp_t gfp_flags, int is_tmf, struct pm8001_tmf_task *tmf) |
| 380 | { |
| 381 | struct domain_device *dev = task->dev; |
| 382 | struct pm8001_hba_info *pm8001_ha; |
| 383 | struct pm8001_device *pm8001_dev; |
jack wang | 1cc943a | 2009-12-07 17:22:42 +0800 | [diff] [blame] | 384 | struct pm8001_port *port = NULL; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 385 | struct sas_task *t = task; |
| 386 | struct pm8001_ccb_info *ccb; |
| 387 | u32 tag = 0xdeadbeef, rc, n_elem = 0; |
Dan Williams | 312d3e5 | 2011-11-17 17:59:50 -0800 | [diff] [blame] | 388 | unsigned long flags = 0; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 389 | |
| 390 | if (!dev->port) { |
| 391 | struct task_status_struct *tsm = &t->task_status; |
| 392 | tsm->resp = SAS_TASK_UNDELIVERED; |
| 393 | tsm->stat = SAS_PHY_DOWN; |
James Bottomley | aa9f832 | 2013-05-07 14:44:06 -0700 | [diff] [blame] | 394 | if (dev->dev_type != SAS_SATA_DEV) |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 395 | t->task_done(t); |
| 396 | return 0; |
| 397 | } |
| 398 | pm8001_ha = pm8001_find_ha_by_dev(task->dev); |
Deepak Ukey | 72349b6 | 2018-09-11 14:18:04 +0530 | [diff] [blame] | 399 | if (pm8001_ha->controller_fatal_error) { |
| 400 | struct task_status_struct *ts = &t->task_status; |
| 401 | |
| 402 | ts->resp = SAS_TASK_UNDELIVERED; |
| 403 | t->task_done(t); |
| 404 | return 0; |
| 405 | } |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 406 | PM8001_IO_DBG(pm8001_ha, pm8001_printk("pm8001_task_exec device \n ")); |
| 407 | spin_lock_irqsave(&pm8001_ha->lock, flags); |
| 408 | do { |
| 409 | dev = t->dev; |
| 410 | pm8001_dev = dev->lldd_dev; |
jack wang | 1cc943a | 2009-12-07 17:22:42 +0800 | [diff] [blame] | 411 | port = &pm8001_ha->port[sas_find_local_port_id(dev)]; |
Mark Salyzyn | b90b378 | 2011-09-22 08:50:01 -0700 | [diff] [blame] | 412 | if (DEV_IS_GONE(pm8001_dev) || !port->port_attached) { |
jack wang | 1cc943a | 2009-12-07 17:22:42 +0800 | [diff] [blame] | 413 | if (sas_protocol_ata(t->task_proto)) { |
| 414 | struct task_status_struct *ts = &t->task_status; |
| 415 | ts->resp = SAS_TASK_UNDELIVERED; |
| 416 | ts->stat = SAS_PHY_DOWN; |
| 417 | |
| 418 | spin_unlock_irqrestore(&pm8001_ha->lock, flags); |
jack wang | 1cc943a | 2009-12-07 17:22:42 +0800 | [diff] [blame] | 419 | t->task_done(t); |
jack wang | 1cc943a | 2009-12-07 17:22:42 +0800 | [diff] [blame] | 420 | spin_lock_irqsave(&pm8001_ha->lock, flags); |
jack wang | 1cc943a | 2009-12-07 17:22:42 +0800 | [diff] [blame] | 421 | continue; |
| 422 | } else { |
| 423 | struct task_status_struct *ts = &t->task_status; |
| 424 | ts->resp = SAS_TASK_UNDELIVERED; |
| 425 | ts->stat = SAS_PHY_DOWN; |
| 426 | t->task_done(t); |
jack wang | 1cc943a | 2009-12-07 17:22:42 +0800 | [diff] [blame] | 427 | continue; |
| 428 | } |
| 429 | } |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 430 | rc = pm8001_tag_alloc(pm8001_ha, &tag); |
| 431 | if (rc) |
| 432 | goto err_out; |
| 433 | ccb = &pm8001_ha->ccb_info[tag]; |
| 434 | |
| 435 | if (!sas_protocol_ata(t->task_proto)) { |
| 436 | if (t->num_scatter) { |
| 437 | n_elem = dma_map_sg(pm8001_ha->dev, |
| 438 | t->scatter, |
| 439 | t->num_scatter, |
| 440 | t->data_dir); |
| 441 | if (!n_elem) { |
| 442 | rc = -ENOMEM; |
jack_wang | 97ee208 | 2009-11-05 22:33:51 +0800 | [diff] [blame] | 443 | goto err_out_tag; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | } else { |
| 447 | n_elem = t->num_scatter; |
| 448 | } |
| 449 | |
jack_wang | 97ee208 | 2009-11-05 22:33:51 +0800 | [diff] [blame] | 450 | t->lldd_task = ccb; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 451 | ccb->n_elem = n_elem; |
| 452 | ccb->ccb_tag = tag; |
| 453 | ccb->task = t; |
XinHong Zhu | aed97b8 | 2014-02-14 16:01:28 +0800 | [diff] [blame] | 454 | ccb->device = pm8001_dev; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 455 | switch (t->task_proto) { |
| 456 | case SAS_PROTOCOL_SMP: |
| 457 | rc = pm8001_task_prep_smp(pm8001_ha, ccb); |
| 458 | break; |
| 459 | case SAS_PROTOCOL_SSP: |
| 460 | if (is_tmf) |
| 461 | rc = pm8001_task_prep_ssp_tm(pm8001_ha, |
| 462 | ccb, tmf); |
| 463 | else |
| 464 | rc = pm8001_task_prep_ssp(pm8001_ha, ccb); |
| 465 | break; |
| 466 | case SAS_PROTOCOL_SATA: |
| 467 | case SAS_PROTOCOL_STP: |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 468 | rc = pm8001_task_prep_ata(pm8001_ha, ccb); |
| 469 | break; |
| 470 | default: |
| 471 | dev_printk(KERN_ERR, pm8001_ha->dev, |
| 472 | "unknown sas_task proto: 0x%x\n", |
| 473 | t->task_proto); |
| 474 | rc = -EINVAL; |
| 475 | break; |
| 476 | } |
| 477 | |
| 478 | if (rc) { |
| 479 | PM8001_IO_DBG(pm8001_ha, |
| 480 | pm8001_printk("rc is %x\n", rc)); |
| 481 | goto err_out_tag; |
| 482 | } |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 483 | /* TODO: select normal or high priority */ |
| 484 | spin_lock(&t->task_state_lock); |
| 485 | t->task_state_flags |= SAS_TASK_AT_INITIATOR; |
| 486 | spin_unlock(&t->task_state_lock); |
| 487 | pm8001_dev->running_req++; |
Christoph Hellwig | 79855d1 | 2014-11-05 10:36:28 +0100 | [diff] [blame] | 488 | } while (0); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 489 | rc = 0; |
| 490 | goto out_done; |
| 491 | |
| 492 | err_out_tag: |
| 493 | pm8001_tag_free(pm8001_ha, tag); |
| 494 | err_out: |
| 495 | dev_printk(KERN_ERR, pm8001_ha->dev, "pm8001 exec failed[%d]!\n", rc); |
| 496 | if (!sas_protocol_ata(t->task_proto)) |
| 497 | if (n_elem) |
Deepak Ukey | 76cb25b | 2018-09-11 14:18:03 +0530 | [diff] [blame] | 498 | dma_unmap_sg(pm8001_ha->dev, t->scatter, t->num_scatter, |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 499 | t->data_dir); |
| 500 | out_done: |
| 501 | spin_unlock_irqrestore(&pm8001_ha->lock, flags); |
| 502 | return rc; |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * pm8001_queue_command - register for upper layer used, all IO commands sent |
| 507 | * to HBA are from this interface. |
| 508 | * @task: the task to be execute. |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 509 | * @gfp_flags: gfp_flags |
| 510 | */ |
Christoph Hellwig | 79855d1 | 2014-11-05 10:36:28 +0100 | [diff] [blame] | 511 | int pm8001_queue_command(struct sas_task *task, gfp_t gfp_flags) |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 512 | { |
Christoph Hellwig | 79855d1 | 2014-11-05 10:36:28 +0100 | [diff] [blame] | 513 | return pm8001_task_exec(task, gfp_flags, 0, NULL); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 514 | } |
| 515 | |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 516 | /** |
| 517 | * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb. |
| 518 | * @pm8001_ha: our hba card information |
| 519 | * @ccb: the ccb which attached to ssp task |
| 520 | * @task: the task to be free. |
| 521 | * @ccb_idx: ccb index. |
| 522 | */ |
| 523 | void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha, |
| 524 | struct sas_task *task, struct pm8001_ccb_info *ccb, u32 ccb_idx) |
| 525 | { |
| 526 | if (!ccb->task) |
| 527 | return; |
| 528 | if (!sas_protocol_ata(task->task_proto)) |
| 529 | if (ccb->n_elem) |
| 530 | dma_unmap_sg(pm8001_ha->dev, task->scatter, |
| 531 | task->num_scatter, task->data_dir); |
| 532 | |
| 533 | switch (task->task_proto) { |
| 534 | case SAS_PROTOCOL_SMP: |
| 535 | dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1, |
Christoph Hellwig | f73bdeb | 2018-10-10 19:59:50 +0200 | [diff] [blame] | 536 | DMA_FROM_DEVICE); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 537 | dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1, |
Christoph Hellwig | f73bdeb | 2018-10-10 19:59:50 +0200 | [diff] [blame] | 538 | DMA_TO_DEVICE); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 539 | break; |
| 540 | |
| 541 | case SAS_PROTOCOL_SATA: |
| 542 | case SAS_PROTOCOL_STP: |
| 543 | case SAS_PROTOCOL_SSP: |
| 544 | default: |
| 545 | /* do nothing */ |
| 546 | break; |
| 547 | } |
| 548 | task->lldd_task = NULL; |
| 549 | ccb->task = NULL; |
| 550 | ccb->ccb_tag = 0xFFFFFFFF; |
Mark Salyzyn | 5954d73 | 2012-01-17 11:52:24 -0500 | [diff] [blame] | 551 | ccb->open_retry = 0; |
Tomas Henzl | ef30054 | 2014-07-09 17:20:40 +0530 | [diff] [blame] | 552 | pm8001_tag_free(pm8001_ha, ccb_idx); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | /** |
jack_wang | 97ee208 | 2009-11-05 22:33:51 +0800 | [diff] [blame] | 556 | * pm8001_alloc_dev - find a empty pm8001_device |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 557 | * @pm8001_ha: our hba card information |
| 558 | */ |
Baoyou Xie | 7efa59e | 2016-09-23 21:54:22 +0800 | [diff] [blame] | 559 | static struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha) |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 560 | { |
| 561 | u32 dev; |
| 562 | for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) { |
James Bottomley | aa9f832 | 2013-05-07 14:44:06 -0700 | [diff] [blame] | 563 | if (pm8001_ha->devices[dev].dev_type == SAS_PHY_UNUSED) { |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 564 | pm8001_ha->devices[dev].id = dev; |
| 565 | return &pm8001_ha->devices[dev]; |
| 566 | } |
| 567 | } |
| 568 | if (dev == PM8001_MAX_DEVICES) { |
| 569 | PM8001_FAIL_DBG(pm8001_ha, |
| 570 | pm8001_printk("max support %d devices, ignore ..\n", |
| 571 | PM8001_MAX_DEVICES)); |
| 572 | } |
| 573 | return NULL; |
| 574 | } |
Sakthivel K | c6b9ef5 | 2013-03-19 18:08:08 +0530 | [diff] [blame] | 575 | /** |
| 576 | * pm8001_find_dev - find a matching pm8001_device |
| 577 | * @pm8001_ha: our hba card information |
| 578 | */ |
| 579 | struct pm8001_device *pm8001_find_dev(struct pm8001_hba_info *pm8001_ha, |
| 580 | u32 device_id) |
| 581 | { |
| 582 | u32 dev; |
| 583 | for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) { |
| 584 | if (pm8001_ha->devices[dev].device_id == device_id) |
| 585 | return &pm8001_ha->devices[dev]; |
| 586 | } |
| 587 | if (dev == PM8001_MAX_DEVICES) { |
| 588 | PM8001_FAIL_DBG(pm8001_ha, pm8001_printk("NO MATCHING " |
| 589 | "DEVICE FOUND !!!\n")); |
| 590 | } |
| 591 | return NULL; |
| 592 | } |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 593 | |
| 594 | static void pm8001_free_dev(struct pm8001_device *pm8001_dev) |
| 595 | { |
| 596 | u32 id = pm8001_dev->id; |
| 597 | memset(pm8001_dev, 0, sizeof(*pm8001_dev)); |
| 598 | pm8001_dev->id = id; |
James Bottomley | aa9f832 | 2013-05-07 14:44:06 -0700 | [diff] [blame] | 599 | pm8001_dev->dev_type = SAS_PHY_UNUSED; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 600 | pm8001_dev->device_id = PM8001_MAX_DEVICES; |
| 601 | pm8001_dev->sas_device = NULL; |
| 602 | } |
| 603 | |
| 604 | /** |
jack_wang | 97ee208 | 2009-11-05 22:33:51 +0800 | [diff] [blame] | 605 | * pm8001_dev_found_notify - libsas notify a device is found. |
| 606 | * @dev: the device structure which sas layer used. |
| 607 | * |
| 608 | * when libsas find a sas domain device, it should tell the LLDD that |
| 609 | * device is found, and then LLDD register this device to HBA firmware |
| 610 | * by the command "OPC_INB_REG_DEV", after that the HBA will assign a |
| 611 | * device ID(according to device's sas address) and returned it to LLDD. From |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 612 | * now on, we communicate with HBA FW with the device ID which HBA assigned |
Daniel Mack | 3ad2f3fb | 2010-02-03 08:01:28 +0800 | [diff] [blame] | 613 | * rather than sas address. it is the necessary step for our HBA but it is |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 614 | * the optional for other HBA driver. |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 615 | */ |
| 616 | static int pm8001_dev_found_notify(struct domain_device *dev) |
| 617 | { |
| 618 | unsigned long flags = 0; |
| 619 | int res = 0; |
| 620 | struct pm8001_hba_info *pm8001_ha = NULL; |
| 621 | struct domain_device *parent_dev = dev->parent; |
| 622 | struct pm8001_device *pm8001_device; |
| 623 | DECLARE_COMPLETION_ONSTACK(completion); |
| 624 | u32 flag = 0; |
| 625 | pm8001_ha = pm8001_find_ha_by_dev(dev); |
| 626 | spin_lock_irqsave(&pm8001_ha->lock, flags); |
| 627 | |
| 628 | pm8001_device = pm8001_alloc_dev(pm8001_ha); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 629 | if (!pm8001_device) { |
| 630 | res = -1; |
| 631 | goto found_out; |
| 632 | } |
jack wang | f01f4e6 | 2009-12-07 17:22:55 +0800 | [diff] [blame] | 633 | pm8001_device->sas_device = dev; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 634 | dev->lldd_dev = pm8001_device; |
| 635 | pm8001_device->dev_type = dev->dev_type; |
| 636 | pm8001_device->dcompletion = &completion; |
| 637 | if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) { |
| 638 | int phy_id; |
| 639 | struct ex_phy *phy; |
| 640 | for (phy_id = 0; phy_id < parent_dev->ex_dev.num_phys; |
| 641 | phy_id++) { |
| 642 | phy = &parent_dev->ex_dev.ex_phy[phy_id]; |
| 643 | if (SAS_ADDR(phy->attached_sas_addr) |
| 644 | == SAS_ADDR(dev->sas_addr)) { |
| 645 | pm8001_device->attached_phy = phy_id; |
| 646 | break; |
| 647 | } |
| 648 | } |
| 649 | if (phy_id == parent_dev->ex_dev.num_phys) { |
| 650 | PM8001_FAIL_DBG(pm8001_ha, |
| 651 | pm8001_printk("Error: no attached dev:%016llx" |
| 652 | " at ex:%016llx.\n", SAS_ADDR(dev->sas_addr), |
| 653 | SAS_ADDR(parent_dev->sas_addr))); |
| 654 | res = -1; |
| 655 | } |
| 656 | } else { |
James Bottomley | aa9f832 | 2013-05-07 14:44:06 -0700 | [diff] [blame] | 657 | if (dev->dev_type == SAS_SATA_DEV) { |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 658 | pm8001_device->attached_phy = |
| 659 | dev->rphy->identify.phy_identifier; |
| 660 | flag = 1; /* directly sata*/ |
| 661 | } |
| 662 | } /*register this device to HBA*/ |
Mark Salyzyn | 6fbc769 | 2011-09-26 07:57:36 -0700 | [diff] [blame] | 663 | PM8001_DISC_DBG(pm8001_ha, pm8001_printk("Found device\n")); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 664 | PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag); |
| 665 | spin_unlock_irqrestore(&pm8001_ha->lock, flags); |
| 666 | wait_for_completion(&completion); |
James Bottomley | aa9f832 | 2013-05-07 14:44:06 -0700 | [diff] [blame] | 667 | if (dev->dev_type == SAS_END_DEVICE) |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 668 | msleep(50); |
Mark Salyzyn | 5c4fb76 | 2012-01-17 12:56:45 -0500 | [diff] [blame] | 669 | pm8001_ha->flags = PM8001F_RUN_TIME; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 670 | return 0; |
| 671 | found_out: |
| 672 | spin_unlock_irqrestore(&pm8001_ha->lock, flags); |
| 673 | return res; |
| 674 | } |
| 675 | |
| 676 | int pm8001_dev_found(struct domain_device *dev) |
| 677 | { |
| 678 | return pm8001_dev_found_notify(dev); |
| 679 | } |
| 680 | |
Sakthivel K | c6b9ef5 | 2013-03-19 18:08:08 +0530 | [diff] [blame] | 681 | void pm8001_task_done(struct sas_task *task) |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 682 | { |
Dan Williams | f0bf750 | 2012-06-21 23:36:30 -0700 | [diff] [blame] | 683 | if (!del_timer(&task->slow_task->timer)) |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 684 | return; |
Dan Williams | f0bf750 | 2012-06-21 23:36:30 -0700 | [diff] [blame] | 685 | complete(&task->slow_task->completion); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 686 | } |
| 687 | |
Kees Cook | 77570ee | 2017-08-22 16:05:14 -0700 | [diff] [blame] | 688 | static void pm8001_tmf_timedout(struct timer_list *t) |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 689 | { |
Kees Cook | 77570ee | 2017-08-22 16:05:14 -0700 | [diff] [blame] | 690 | struct sas_task_slow *slow = from_timer(slow, t, timer); |
| 691 | struct sas_task *task = slow->task; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 692 | |
| 693 | task->task_state_flags |= SAS_TASK_STATE_ABORTED; |
Dan Williams | f0bf750 | 2012-06-21 23:36:30 -0700 | [diff] [blame] | 694 | complete(&task->slow_task->completion); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | #define PM8001_TASK_TIMEOUT 20 |
| 698 | /** |
jack_wang | 97ee208 | 2009-11-05 22:33:51 +0800 | [diff] [blame] | 699 | * pm8001_exec_internal_tmf_task - execute some task management commands. |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 700 | * @dev: the wanted device. |
| 701 | * @tmf: which task management wanted to be take. |
| 702 | * @para_len: para_len. |
| 703 | * @parameter: ssp task parameter. |
jack_wang | 97ee208 | 2009-11-05 22:33:51 +0800 | [diff] [blame] | 704 | * |
| 705 | * when errors or exception happened, we may want to do something, for example |
| 706 | * abort the issued task which result in this execption, it is done by calling |
| 707 | * this function, note it is also with the task execute interface. |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 708 | */ |
| 709 | static int pm8001_exec_internal_tmf_task(struct domain_device *dev, |
| 710 | void *parameter, u32 para_len, struct pm8001_tmf_task *tmf) |
| 711 | { |
| 712 | int res, retry; |
| 713 | struct sas_task *task = NULL; |
| 714 | struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev); |
Anand Kumar Santhanam | e912457b | 2013-09-17 16:58:10 +0530 | [diff] [blame] | 715 | struct pm8001_device *pm8001_dev = dev->lldd_dev; |
| 716 | DECLARE_COMPLETION_ONSTACK(completion_setstate); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 717 | |
| 718 | for (retry = 0; retry < 3; retry++) { |
Dan Williams | f0bf750 | 2012-06-21 23:36:30 -0700 | [diff] [blame] | 719 | task = sas_alloc_slow_task(GFP_KERNEL); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 720 | if (!task) |
| 721 | return -ENOMEM; |
| 722 | |
| 723 | task->dev = dev; |
| 724 | task->task_proto = dev->tproto; |
| 725 | memcpy(&task->ssp_task, parameter, para_len); |
| 726 | task->task_done = pm8001_task_done; |
Kees Cook | 841b86f | 2017-10-23 09:40:42 +0200 | [diff] [blame] | 727 | task->slow_task->timer.function = pm8001_tmf_timedout; |
Dan Williams | f0bf750 | 2012-06-21 23:36:30 -0700 | [diff] [blame] | 728 | task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT*HZ; |
| 729 | add_timer(&task->slow_task->timer); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 730 | |
Christoph Hellwig | 79855d1 | 2014-11-05 10:36:28 +0100 | [diff] [blame] | 731 | res = pm8001_task_exec(task, GFP_KERNEL, 1, tmf); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 732 | |
| 733 | if (res) { |
Dan Williams | f0bf750 | 2012-06-21 23:36:30 -0700 | [diff] [blame] | 734 | del_timer(&task->slow_task->timer); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 735 | PM8001_FAIL_DBG(pm8001_ha, |
| 736 | pm8001_printk("Executing internal task " |
| 737 | "failed\n")); |
| 738 | goto ex_err; |
| 739 | } |
Dan Williams | f0bf750 | 2012-06-21 23:36:30 -0700 | [diff] [blame] | 740 | wait_for_completion(&task->slow_task->completion); |
Anand Kumar Santhanam | e912457b | 2013-09-17 16:58:10 +0530 | [diff] [blame] | 741 | if (pm8001_ha->chip_id != chip_8001) { |
| 742 | pm8001_dev->setds_completion = &completion_setstate; |
| 743 | PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha, |
| 744 | pm8001_dev, 0x01); |
| 745 | wait_for_completion(&completion_setstate); |
| 746 | } |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 747 | res = -TMF_RESP_FUNC_FAILED; |
| 748 | /* Even TMF timed out, return direct. */ |
| 749 | if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) { |
| 750 | if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) { |
| 751 | PM8001_FAIL_DBG(pm8001_ha, |
| 752 | pm8001_printk("TMF task[%x]timeout.\n", |
| 753 | tmf->tmf)); |
| 754 | goto ex_err; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | if (task->task_status.resp == SAS_TASK_COMPLETE && |
James Bottomley | df64d3c | 2010-07-27 15:51:13 -0500 | [diff] [blame] | 759 | task->task_status.stat == SAM_STAT_GOOD) { |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 760 | res = TMF_RESP_FUNC_COMPLETE; |
| 761 | break; |
| 762 | } |
| 763 | |
| 764 | if (task->task_status.resp == SAS_TASK_COMPLETE && |
| 765 | task->task_status.stat == SAS_DATA_UNDERRUN) { |
| 766 | /* no error, but return the number of bytes of |
| 767 | * underrun */ |
| 768 | res = task->task_status.residual; |
| 769 | break; |
| 770 | } |
| 771 | |
| 772 | if (task->task_status.resp == SAS_TASK_COMPLETE && |
| 773 | task->task_status.stat == SAS_DATA_OVERRUN) { |
| 774 | PM8001_FAIL_DBG(pm8001_ha, |
| 775 | pm8001_printk("Blocked task error.\n")); |
| 776 | res = -EMSGSIZE; |
| 777 | break; |
| 778 | } else { |
jack_wang | 97ee208 | 2009-11-05 22:33:51 +0800 | [diff] [blame] | 779 | PM8001_EH_DBG(pm8001_ha, |
| 780 | pm8001_printk(" Task to dev %016llx response:" |
| 781 | "0x%x status 0x%x\n", |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 782 | SAS_ADDR(dev->sas_addr), |
| 783 | task->task_status.resp, |
| 784 | task->task_status.stat)); |
Dan Williams | 4fcf812 | 2011-07-29 17:26:39 -0700 | [diff] [blame] | 785 | sas_free_task(task); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 786 | task = NULL; |
| 787 | } |
| 788 | } |
| 789 | ex_err: |
| 790 | BUG_ON(retry == 3 && task != NULL); |
Dan Williams | 4fcf812 | 2011-07-29 17:26:39 -0700 | [diff] [blame] | 791 | sas_free_task(task); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 792 | return res; |
| 793 | } |
| 794 | |
| 795 | static int |
| 796 | pm8001_exec_internal_task_abort(struct pm8001_hba_info *pm8001_ha, |
| 797 | struct pm8001_device *pm8001_dev, struct domain_device *dev, u32 flag, |
| 798 | u32 task_tag) |
| 799 | { |
| 800 | int res, retry; |
jack_wang | 97ee208 | 2009-11-05 22:33:51 +0800 | [diff] [blame] | 801 | u32 ccb_tag; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 802 | struct pm8001_ccb_info *ccb; |
| 803 | struct sas_task *task = NULL; |
| 804 | |
| 805 | for (retry = 0; retry < 3; retry++) { |
Dan Williams | f0bf750 | 2012-06-21 23:36:30 -0700 | [diff] [blame] | 806 | task = sas_alloc_slow_task(GFP_KERNEL); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 807 | if (!task) |
| 808 | return -ENOMEM; |
| 809 | |
| 810 | task->dev = dev; |
| 811 | task->task_proto = dev->tproto; |
| 812 | task->task_done = pm8001_task_done; |
Kees Cook | 841b86f | 2017-10-23 09:40:42 +0200 | [diff] [blame] | 813 | task->slow_task->timer.function = pm8001_tmf_timedout; |
Dan Williams | f0bf750 | 2012-06-21 23:36:30 -0700 | [diff] [blame] | 814 | task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT * HZ; |
| 815 | add_timer(&task->slow_task->timer); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 816 | |
jack_wang | 97ee208 | 2009-11-05 22:33:51 +0800 | [diff] [blame] | 817 | res = pm8001_tag_alloc(pm8001_ha, &ccb_tag); |
| 818 | if (res) |
| 819 | return res; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 820 | ccb = &pm8001_ha->ccb_info[ccb_tag]; |
| 821 | ccb->device = pm8001_dev; |
| 822 | ccb->ccb_tag = ccb_tag; |
| 823 | ccb->task = task; |
Viswas G | 3b700e3 | 2015-08-11 15:06:28 +0530 | [diff] [blame] | 824 | ccb->n_elem = 0; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 825 | |
| 826 | res = PM8001_CHIP_DISP->task_abort(pm8001_ha, |
| 827 | pm8001_dev, flag, task_tag, ccb_tag); |
| 828 | |
| 829 | if (res) { |
Dan Williams | f0bf750 | 2012-06-21 23:36:30 -0700 | [diff] [blame] | 830 | del_timer(&task->slow_task->timer); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 831 | PM8001_FAIL_DBG(pm8001_ha, |
| 832 | pm8001_printk("Executing internal task " |
| 833 | "failed\n")); |
| 834 | goto ex_err; |
| 835 | } |
Dan Williams | f0bf750 | 2012-06-21 23:36:30 -0700 | [diff] [blame] | 836 | wait_for_completion(&task->slow_task->completion); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 837 | res = TMF_RESP_FUNC_FAILED; |
| 838 | /* Even TMF timed out, return direct. */ |
| 839 | if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) { |
| 840 | if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) { |
| 841 | PM8001_FAIL_DBG(pm8001_ha, |
| 842 | pm8001_printk("TMF task timeout.\n")); |
| 843 | goto ex_err; |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | if (task->task_status.resp == SAS_TASK_COMPLETE && |
James Bottomley | df64d3c | 2010-07-27 15:51:13 -0500 | [diff] [blame] | 848 | task->task_status.stat == SAM_STAT_GOOD) { |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 849 | res = TMF_RESP_FUNC_COMPLETE; |
| 850 | break; |
| 851 | |
| 852 | } else { |
jack_wang | 97ee208 | 2009-11-05 22:33:51 +0800 | [diff] [blame] | 853 | PM8001_EH_DBG(pm8001_ha, |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 854 | pm8001_printk(" Task to dev %016llx response: " |
| 855 | "0x%x status 0x%x\n", |
| 856 | SAS_ADDR(dev->sas_addr), |
| 857 | task->task_status.resp, |
| 858 | task->task_status.stat)); |
Dan Williams | 4fcf812 | 2011-07-29 17:26:39 -0700 | [diff] [blame] | 859 | sas_free_task(task); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 860 | task = NULL; |
| 861 | } |
| 862 | } |
| 863 | ex_err: |
| 864 | BUG_ON(retry == 3 && task != NULL); |
Dan Williams | 4fcf812 | 2011-07-29 17:26:39 -0700 | [diff] [blame] | 865 | sas_free_task(task); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 866 | return res; |
| 867 | } |
| 868 | |
| 869 | /** |
| 870 | * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify" |
| 871 | * @dev: the device structure which sas layer used. |
| 872 | */ |
| 873 | static void pm8001_dev_gone_notify(struct domain_device *dev) |
| 874 | { |
| 875 | unsigned long flags = 0; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 876 | struct pm8001_hba_info *pm8001_ha; |
| 877 | struct pm8001_device *pm8001_dev = dev->lldd_dev; |
Dan Carpenter | 2471b89 | 2010-04-23 14:01:04 +0200 | [diff] [blame] | 878 | |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 879 | pm8001_ha = pm8001_find_ha_by_dev(dev); |
| 880 | spin_lock_irqsave(&pm8001_ha->lock, flags); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 881 | if (pm8001_dev) { |
Dan Carpenter | 2471b89 | 2010-04-23 14:01:04 +0200 | [diff] [blame] | 882 | u32 device_id = pm8001_dev->device_id; |
| 883 | |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 884 | PM8001_DISC_DBG(pm8001_ha, |
| 885 | pm8001_printk("found dev[%d:%x] is gone.\n", |
| 886 | pm8001_dev->device_id, pm8001_dev->dev_type)); |
| 887 | if (pm8001_dev->running_req) { |
| 888 | spin_unlock_irqrestore(&pm8001_ha->lock, flags); |
| 889 | pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev , |
| 890 | dev, 1, 0); |
| 891 | spin_lock_irqsave(&pm8001_ha->lock, flags); |
| 892 | } |
| 893 | PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id); |
| 894 | pm8001_free_dev(pm8001_dev); |
| 895 | } else { |
| 896 | PM8001_DISC_DBG(pm8001_ha, |
| 897 | pm8001_printk("Found dev has gone.\n")); |
| 898 | } |
| 899 | dev->lldd_dev = NULL; |
| 900 | spin_unlock_irqrestore(&pm8001_ha->lock, flags); |
| 901 | } |
| 902 | |
| 903 | void pm8001_dev_gone(struct domain_device *dev) |
| 904 | { |
| 905 | pm8001_dev_gone_notify(dev); |
| 906 | } |
| 907 | |
| 908 | static int pm8001_issue_ssp_tmf(struct domain_device *dev, |
| 909 | u8 *lun, struct pm8001_tmf_task *tmf) |
| 910 | { |
| 911 | struct sas_ssp_task ssp_task; |
| 912 | if (!(dev->tproto & SAS_PROTOCOL_SSP)) |
| 913 | return TMF_RESP_FUNC_ESUPP; |
| 914 | |
| 915 | strncpy((u8 *)&ssp_task.LUN, lun, 8); |
| 916 | return pm8001_exec_internal_tmf_task(dev, &ssp_task, sizeof(ssp_task), |
| 917 | tmf); |
| 918 | } |
| 919 | |
Mark Salyzyn | 5954d73 | 2012-01-17 11:52:24 -0500 | [diff] [blame] | 920 | /* retry commands by ha, by task and/or by device */ |
| 921 | void pm8001_open_reject_retry( |
| 922 | struct pm8001_hba_info *pm8001_ha, |
| 923 | struct sas_task *task_to_close, |
| 924 | struct pm8001_device *device_to_close) |
| 925 | { |
| 926 | int i; |
| 927 | unsigned long flags; |
| 928 | |
| 929 | if (pm8001_ha == NULL) |
| 930 | return; |
| 931 | |
| 932 | spin_lock_irqsave(&pm8001_ha->lock, flags); |
| 933 | |
| 934 | for (i = 0; i < PM8001_MAX_CCB; i++) { |
| 935 | struct sas_task *task; |
| 936 | struct task_status_struct *ts; |
| 937 | struct pm8001_device *pm8001_dev; |
| 938 | unsigned long flags1; |
| 939 | u32 tag; |
| 940 | struct pm8001_ccb_info *ccb = &pm8001_ha->ccb_info[i]; |
| 941 | |
| 942 | pm8001_dev = ccb->device; |
James Bottomley | aa9f832 | 2013-05-07 14:44:06 -0700 | [diff] [blame] | 943 | if (!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED)) |
Mark Salyzyn | 5954d73 | 2012-01-17 11:52:24 -0500 | [diff] [blame] | 944 | continue; |
| 945 | if (!device_to_close) { |
| 946 | uintptr_t d = (uintptr_t)pm8001_dev |
| 947 | - (uintptr_t)&pm8001_ha->devices; |
| 948 | if (((d % sizeof(*pm8001_dev)) != 0) |
| 949 | || ((d / sizeof(*pm8001_dev)) >= PM8001_MAX_DEVICES)) |
| 950 | continue; |
| 951 | } else if (pm8001_dev != device_to_close) |
| 952 | continue; |
| 953 | tag = ccb->ccb_tag; |
| 954 | if (!tag || (tag == 0xFFFFFFFF)) |
| 955 | continue; |
| 956 | task = ccb->task; |
| 957 | if (!task || !task->task_done) |
| 958 | continue; |
| 959 | if (task_to_close && (task != task_to_close)) |
| 960 | continue; |
| 961 | ts = &task->task_status; |
| 962 | ts->resp = SAS_TASK_COMPLETE; |
| 963 | /* Force the midlayer to retry */ |
| 964 | ts->stat = SAS_OPEN_REJECT; |
| 965 | ts->open_rej_reason = SAS_OREJ_RSVD_RETRY; |
| 966 | if (pm8001_dev) |
| 967 | pm8001_dev->running_req--; |
| 968 | spin_lock_irqsave(&task->task_state_lock, flags1); |
| 969 | task->task_state_flags &= ~SAS_TASK_STATE_PENDING; |
| 970 | task->task_state_flags &= ~SAS_TASK_AT_INITIATOR; |
| 971 | task->task_state_flags |= SAS_TASK_STATE_DONE; |
| 972 | if (unlikely((task->task_state_flags |
| 973 | & SAS_TASK_STATE_ABORTED))) { |
| 974 | spin_unlock_irqrestore(&task->task_state_lock, |
| 975 | flags1); |
| 976 | pm8001_ccb_task_free(pm8001_ha, task, ccb, tag); |
| 977 | } else { |
| 978 | spin_unlock_irqrestore(&task->task_state_lock, |
| 979 | flags1); |
| 980 | pm8001_ccb_task_free(pm8001_ha, task, ccb, tag); |
| 981 | mb();/* in order to force CPU ordering */ |
| 982 | spin_unlock_irqrestore(&pm8001_ha->lock, flags); |
| 983 | task->task_done(task); |
| 984 | spin_lock_irqsave(&pm8001_ha->lock, flags); |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | spin_unlock_irqrestore(&pm8001_ha->lock, flags); |
| 989 | } |
| 990 | |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 991 | /** |
| 992 | * Standard mandates link reset for ATA (type 0) and hard reset for |
| 993 | * SSP (type 1) , only for RECOVERY |
| 994 | */ |
| 995 | int pm8001_I_T_nexus_reset(struct domain_device *dev) |
| 996 | { |
| 997 | int rc = TMF_RESP_FUNC_FAILED; |
| 998 | struct pm8001_device *pm8001_dev; |
| 999 | struct pm8001_hba_info *pm8001_ha; |
| 1000 | struct sas_phy *phy; |
Dan Williams | e7db822 | 2012-06-21 23:30:58 -0700 | [diff] [blame] | 1001 | |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1002 | if (!dev || !dev->lldd_dev) |
Dan Williams | e7db822 | 2012-06-21 23:30:58 -0700 | [diff] [blame] | 1003 | return -ENODEV; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1004 | |
| 1005 | pm8001_dev = dev->lldd_dev; |
| 1006 | pm8001_ha = pm8001_find_ha_by_dev(dev); |
Dan Williams | f41a0c4 | 2011-12-21 21:33:17 -0800 | [diff] [blame] | 1007 | phy = sas_get_local_phy(dev); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1008 | |
| 1009 | if (dev_is_sata(dev)) { |
Dan Williams | f41a0c4 | 2011-12-21 21:33:17 -0800 | [diff] [blame] | 1010 | if (scsi_is_sas_phy_local(phy)) { |
| 1011 | rc = 0; |
| 1012 | goto out; |
| 1013 | } |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1014 | rc = sas_phy_reset(phy, 1); |
Viswas G | 3a1ae96 | 2015-08-11 15:06:26 +0530 | [diff] [blame] | 1015 | if (rc) { |
| 1016 | PM8001_EH_DBG(pm8001_ha, |
| 1017 | pm8001_printk("phy reset failed for device %x\n" |
| 1018 | "with rc %d\n", pm8001_dev->device_id, rc)); |
| 1019 | rc = TMF_RESP_FUNC_FAILED; |
| 1020 | goto out; |
| 1021 | } |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1022 | msleep(2000); |
| 1023 | rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev , |
| 1024 | dev, 1, 0); |
Viswas G | 3a1ae96 | 2015-08-11 15:06:26 +0530 | [diff] [blame] | 1025 | if (rc) { |
| 1026 | PM8001_EH_DBG(pm8001_ha, |
| 1027 | pm8001_printk("task abort failed %x\n" |
| 1028 | "with rc %d\n", pm8001_dev->device_id, rc)); |
| 1029 | rc = TMF_RESP_FUNC_FAILED; |
| 1030 | } |
Dan Williams | f41a0c4 | 2011-12-21 21:33:17 -0800 | [diff] [blame] | 1031 | } else { |
| 1032 | rc = sas_phy_reset(phy, 1); |
| 1033 | msleep(2000); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1034 | } |
| 1035 | PM8001_EH_DBG(pm8001_ha, pm8001_printk(" for device[%x]:rc=%d\n", |
| 1036 | pm8001_dev->device_id, rc)); |
Dan Williams | f41a0c4 | 2011-12-21 21:33:17 -0800 | [diff] [blame] | 1037 | out: |
| 1038 | sas_put_local_phy(phy); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1039 | return rc; |
| 1040 | } |
| 1041 | |
Sakthivel K | a6cb3d0 | 2013-03-19 18:08:40 +0530 | [diff] [blame] | 1042 | /* |
| 1043 | * This function handle the IT_NEXUS_XXX event or completion |
| 1044 | * status code for SSP/SATA/SMP I/O request. |
| 1045 | */ |
| 1046 | int pm8001_I_T_nexus_event_handler(struct domain_device *dev) |
| 1047 | { |
| 1048 | int rc = TMF_RESP_FUNC_FAILED; |
| 1049 | struct pm8001_device *pm8001_dev; |
| 1050 | struct pm8001_hba_info *pm8001_ha; |
| 1051 | struct sas_phy *phy; |
Sakthivel K | a6cb3d0 | 2013-03-19 18:08:40 +0530 | [diff] [blame] | 1052 | |
| 1053 | if (!dev || !dev->lldd_dev) |
| 1054 | return -1; |
| 1055 | |
| 1056 | pm8001_dev = dev->lldd_dev; |
Sakthivel K | a6cb3d0 | 2013-03-19 18:08:40 +0530 | [diff] [blame] | 1057 | pm8001_ha = pm8001_find_ha_by_dev(dev); |
| 1058 | |
| 1059 | PM8001_EH_DBG(pm8001_ha, |
| 1060 | pm8001_printk("I_T_Nexus handler invoked !!")); |
| 1061 | |
| 1062 | phy = sas_get_local_phy(dev); |
| 1063 | |
| 1064 | if (dev_is_sata(dev)) { |
| 1065 | DECLARE_COMPLETION_ONSTACK(completion_setstate); |
| 1066 | if (scsi_is_sas_phy_local(phy)) { |
| 1067 | rc = 0; |
| 1068 | goto out; |
| 1069 | } |
| 1070 | /* send internal ssp/sata/smp abort command to FW */ |
| 1071 | rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev , |
| 1072 | dev, 1, 0); |
| 1073 | msleep(100); |
| 1074 | |
| 1075 | /* deregister the target device */ |
| 1076 | pm8001_dev_gone_notify(dev); |
| 1077 | msleep(200); |
| 1078 | |
| 1079 | /*send phy reset to hard reset target */ |
| 1080 | rc = sas_phy_reset(phy, 1); |
| 1081 | msleep(2000); |
| 1082 | pm8001_dev->setds_completion = &completion_setstate; |
| 1083 | |
| 1084 | wait_for_completion(&completion_setstate); |
| 1085 | } else { |
| 1086 | /* send internal ssp/sata/smp abort command to FW */ |
| 1087 | rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev , |
| 1088 | dev, 1, 0); |
| 1089 | msleep(100); |
| 1090 | |
| 1091 | /* deregister the target device */ |
| 1092 | pm8001_dev_gone_notify(dev); |
| 1093 | msleep(200); |
| 1094 | |
| 1095 | /*send phy reset to hard reset target */ |
| 1096 | rc = sas_phy_reset(phy, 1); |
| 1097 | msleep(2000); |
| 1098 | } |
| 1099 | PM8001_EH_DBG(pm8001_ha, pm8001_printk(" for device[%x]:rc=%d\n", |
| 1100 | pm8001_dev->device_id, rc)); |
| 1101 | out: |
| 1102 | sas_put_local_phy(phy); |
| 1103 | |
| 1104 | return rc; |
| 1105 | } |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1106 | /* mandatory SAM-3, the task reset the specified LUN*/ |
| 1107 | int pm8001_lu_reset(struct domain_device *dev, u8 *lun) |
| 1108 | { |
| 1109 | int rc = TMF_RESP_FUNC_FAILED; |
| 1110 | struct pm8001_tmf_task tmf_task; |
| 1111 | struct pm8001_device *pm8001_dev = dev->lldd_dev; |
| 1112 | struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev); |
Nikith Ganigarakoppal | 34a9b81 | 2013-10-30 16:13:22 +0530 | [diff] [blame] | 1113 | DECLARE_COMPLETION_ONSTACK(completion_setstate); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1114 | if (dev_is_sata(dev)) { |
Dan Williams | f41a0c4 | 2011-12-21 21:33:17 -0800 | [diff] [blame] | 1115 | struct sas_phy *phy = sas_get_local_phy(dev); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1116 | rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev , |
| 1117 | dev, 1, 0); |
| 1118 | rc = sas_phy_reset(phy, 1); |
Dan Williams | f41a0c4 | 2011-12-21 21:33:17 -0800 | [diff] [blame] | 1119 | sas_put_local_phy(phy); |
Nikith Ganigarakoppal | 34a9b81 | 2013-10-30 16:13:22 +0530 | [diff] [blame] | 1120 | pm8001_dev->setds_completion = &completion_setstate; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1121 | rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha, |
| 1122 | pm8001_dev, 0x01); |
Nikith Ganigarakoppal | 34a9b81 | 2013-10-30 16:13:22 +0530 | [diff] [blame] | 1123 | wait_for_completion(&completion_setstate); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1124 | } else { |
| 1125 | tmf_task.tmf = TMF_LU_RESET; |
| 1126 | rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task); |
| 1127 | } |
| 1128 | /* If failed, fall-through I_T_Nexus reset */ |
| 1129 | PM8001_EH_DBG(pm8001_ha, pm8001_printk("for device[%x]:rc=%d\n", |
| 1130 | pm8001_dev->device_id, rc)); |
| 1131 | return rc; |
| 1132 | } |
| 1133 | |
| 1134 | /* optional SAM-3 */ |
| 1135 | int pm8001_query_task(struct sas_task *task) |
| 1136 | { |
| 1137 | u32 tag = 0xdeadbeef; |
| 1138 | int i = 0; |
| 1139 | struct scsi_lun lun; |
| 1140 | struct pm8001_tmf_task tmf_task; |
| 1141 | int rc = TMF_RESP_FUNC_FAILED; |
| 1142 | if (unlikely(!task || !task->lldd_task || !task->dev)) |
| 1143 | return rc; |
| 1144 | |
| 1145 | if (task->task_proto & SAS_PROTOCOL_SSP) { |
| 1146 | struct scsi_cmnd *cmnd = task->uldd_task; |
| 1147 | struct domain_device *dev = task->dev; |
| 1148 | struct pm8001_hba_info *pm8001_ha = |
| 1149 | pm8001_find_ha_by_dev(dev); |
| 1150 | |
| 1151 | int_to_scsilun(cmnd->device->lun, &lun); |
| 1152 | rc = pm8001_find_tag(task, &tag); |
| 1153 | if (rc == 0) { |
| 1154 | rc = TMF_RESP_FUNC_FAILED; |
| 1155 | return rc; |
| 1156 | } |
| 1157 | PM8001_EH_DBG(pm8001_ha, pm8001_printk("Query:[")); |
| 1158 | for (i = 0; i < 16; i++) |
| 1159 | printk(KERN_INFO "%02x ", cmnd->cmnd[i]); |
| 1160 | printk(KERN_INFO "]\n"); |
| 1161 | tmf_task.tmf = TMF_QUERY_TASK; |
| 1162 | tmf_task.tag_of_task_to_be_managed = tag; |
| 1163 | |
| 1164 | rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task); |
| 1165 | switch (rc) { |
| 1166 | /* The task is still in Lun, release it then */ |
| 1167 | case TMF_RESP_FUNC_SUCC: |
| 1168 | PM8001_EH_DBG(pm8001_ha, |
Mark Salyzyn | 6fbc769 | 2011-09-26 07:57:36 -0700 | [diff] [blame] | 1169 | pm8001_printk("The task is still in Lun\n")); |
| 1170 | break; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1171 | /* The task is not in Lun or failed, reset the phy */ |
| 1172 | case TMF_RESP_FUNC_FAILED: |
| 1173 | case TMF_RESP_FUNC_COMPLETE: |
| 1174 | PM8001_EH_DBG(pm8001_ha, |
| 1175 | pm8001_printk("The task is not in Lun or failed," |
Mark Salyzyn | 6fbc769 | 2011-09-26 07:57:36 -0700 | [diff] [blame] | 1176 | " reset the phy\n")); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1177 | break; |
| 1178 | } |
| 1179 | } |
| 1180 | pm8001_printk(":rc= %d\n", rc); |
| 1181 | return rc; |
| 1182 | } |
| 1183 | |
| 1184 | /* mandatory SAM-3, still need free task/ccb info, abord the specified task */ |
| 1185 | int pm8001_abort_task(struct sas_task *task) |
| 1186 | { |
| 1187 | unsigned long flags; |
Viswas G | 1db4990 | 2017-10-18 11:39:11 +0530 | [diff] [blame] | 1188 | u32 tag; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1189 | struct domain_device *dev ; |
Viswas G | 1db4990 | 2017-10-18 11:39:11 +0530 | [diff] [blame] | 1190 | struct pm8001_hba_info *pm8001_ha; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1191 | struct scsi_lun lun; |
| 1192 | struct pm8001_device *pm8001_dev; |
| 1193 | struct pm8001_tmf_task tmf_task; |
Viswas G | 869ddbd | 2017-10-18 11:39:13 +0530 | [diff] [blame] | 1194 | int rc = TMF_RESP_FUNC_FAILED, ret; |
| 1195 | u32 phy_id; |
| 1196 | struct sas_task_slow slow_task; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1197 | if (unlikely(!task || !task->lldd_task || !task->dev)) |
Viswas G | 1db4990 | 2017-10-18 11:39:11 +0530 | [diff] [blame] | 1198 | return TMF_RESP_FUNC_FAILED; |
| 1199 | dev = task->dev; |
| 1200 | pm8001_dev = dev->lldd_dev; |
| 1201 | pm8001_ha = pm8001_find_ha_by_dev(dev); |
Viswas G | 869ddbd | 2017-10-18 11:39:13 +0530 | [diff] [blame] | 1202 | phy_id = pm8001_dev->attached_phy; |
Viswas G | 1db4990 | 2017-10-18 11:39:11 +0530 | [diff] [blame] | 1203 | rc = pm8001_find_tag(task, &tag); |
| 1204 | if (rc == 0) { |
| 1205 | pm8001_printk("no tag for task:%p\n", task); |
| 1206 | return TMF_RESP_FUNC_FAILED; |
| 1207 | } |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1208 | spin_lock_irqsave(&task->task_state_lock, flags); |
| 1209 | if (task->task_state_flags & SAS_TASK_STATE_DONE) { |
| 1210 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
Viswas G | 1db4990 | 2017-10-18 11:39:11 +0530 | [diff] [blame] | 1211 | return TMF_RESP_FUNC_COMPLETE; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1212 | } |
Viswas G | 869ddbd | 2017-10-18 11:39:13 +0530 | [diff] [blame] | 1213 | task->task_state_flags |= SAS_TASK_STATE_ABORTED; |
| 1214 | if (task->slow_task == NULL) { |
| 1215 | init_completion(&slow_task.completion); |
| 1216 | task->slow_task = &slow_task; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1217 | } |
| 1218 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
| 1219 | if (task->task_proto & SAS_PROTOCOL_SSP) { |
| 1220 | struct scsi_cmnd *cmnd = task->uldd_task; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1221 | int_to_scsilun(cmnd->device->lun, &lun); |
jack_wang | 97ee208 | 2009-11-05 22:33:51 +0800 | [diff] [blame] | 1222 | tmf_task.tmf = TMF_ABORT_TASK; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1223 | tmf_task.tag_of_task_to_be_managed = tag; |
| 1224 | rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task); |
jack_wang | 97ee208 | 2009-11-05 22:33:51 +0800 | [diff] [blame] | 1225 | pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev, |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1226 | pm8001_dev->sas_device, 0, tag); |
| 1227 | } else if (task->task_proto & SAS_PROTOCOL_SATA || |
| 1228 | task->task_proto & SAS_PROTOCOL_STP) { |
Viswas G | 869ddbd | 2017-10-18 11:39:13 +0530 | [diff] [blame] | 1229 | if (pm8001_ha->chip_id == chip_8006) { |
| 1230 | DECLARE_COMPLETION_ONSTACK(completion_reset); |
| 1231 | DECLARE_COMPLETION_ONSTACK(completion); |
| 1232 | struct pm8001_phy *phy = pm8001_ha->phy + phy_id; |
| 1233 | |
| 1234 | /* 1. Set Device state as Recovery */ |
| 1235 | pm8001_dev->setds_completion = &completion; |
| 1236 | PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha, |
| 1237 | pm8001_dev, 0x03); |
| 1238 | wait_for_completion(&completion); |
| 1239 | |
| 1240 | /* 2. Send Phy Control Hard Reset */ |
| 1241 | reinit_completion(&completion); |
| 1242 | phy->reset_success = false; |
| 1243 | phy->enable_completion = &completion; |
| 1244 | phy->reset_completion = &completion_reset; |
| 1245 | ret = PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id, |
| 1246 | PHY_HARD_RESET); |
| 1247 | if (ret) |
| 1248 | goto out; |
| 1249 | PM8001_MSG_DBG(pm8001_ha, |
| 1250 | pm8001_printk("Waiting for local phy ctl\n")); |
| 1251 | wait_for_completion(&completion); |
| 1252 | if (!phy->reset_success) |
| 1253 | goto out; |
| 1254 | |
| 1255 | /* 3. Wait for Port Reset complete / Port reset TMO */ |
| 1256 | PM8001_MSG_DBG(pm8001_ha, |
| 1257 | pm8001_printk("Waiting for Port reset\n")); |
| 1258 | wait_for_completion(&completion_reset); |
| 1259 | if (phy->port_reset_status) |
| 1260 | goto out; |
| 1261 | |
| 1262 | /* |
| 1263 | * 4. SATA Abort ALL |
| 1264 | * we wait for the task to be aborted so that the task |
| 1265 | * is removed from the ccb. on success the caller is |
| 1266 | * going to free the task. |
| 1267 | */ |
| 1268 | ret = pm8001_exec_internal_task_abort(pm8001_ha, |
| 1269 | pm8001_dev, pm8001_dev->sas_device, 1, tag); |
| 1270 | if (ret) |
| 1271 | goto out; |
| 1272 | ret = wait_for_completion_timeout( |
| 1273 | &task->slow_task->completion, |
| 1274 | PM8001_TASK_TIMEOUT * HZ); |
| 1275 | if (!ret) |
| 1276 | goto out; |
| 1277 | |
| 1278 | /* 5. Set Device State as Operational */ |
| 1279 | reinit_completion(&completion); |
| 1280 | pm8001_dev->setds_completion = &completion; |
| 1281 | PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha, |
| 1282 | pm8001_dev, 0x01); |
| 1283 | wait_for_completion(&completion); |
| 1284 | } else { |
| 1285 | rc = pm8001_exec_internal_task_abort(pm8001_ha, |
| 1286 | pm8001_dev, pm8001_dev->sas_device, 0, tag); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1287 | } |
Viswas G | 869ddbd | 2017-10-18 11:39:13 +0530 | [diff] [blame] | 1288 | rc = TMF_RESP_FUNC_COMPLETE; |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1289 | } else if (task->task_proto & SAS_PROTOCOL_SMP) { |
| 1290 | /* SMP */ |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1291 | rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev, |
| 1292 | pm8001_dev->sas_device, 0, tag); |
| 1293 | |
| 1294 | } |
| 1295 | out: |
Viswas G | 869ddbd | 2017-10-18 11:39:13 +0530 | [diff] [blame] | 1296 | spin_lock_irqsave(&task->task_state_lock, flags); |
| 1297 | if (task->slow_task == &slow_task) |
| 1298 | task->slow_task = NULL; |
| 1299 | spin_unlock_irqrestore(&task->task_state_lock, flags); |
jack wang | dbf9bfe | 2009-10-14 16:19:21 +0800 | [diff] [blame] | 1300 | if (rc != TMF_RESP_FUNC_COMPLETE) |
| 1301 | pm8001_printk("rc= %d\n", rc); |
| 1302 | return rc; |
| 1303 | } |
| 1304 | |
| 1305 | int pm8001_abort_task_set(struct domain_device *dev, u8 *lun) |
| 1306 | { |
| 1307 | int rc = TMF_RESP_FUNC_FAILED; |
| 1308 | struct pm8001_tmf_task tmf_task; |
| 1309 | |
| 1310 | tmf_task.tmf = TMF_ABORT_TASK_SET; |
| 1311 | rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task); |
| 1312 | return rc; |
| 1313 | } |
| 1314 | |
| 1315 | int pm8001_clear_aca(struct domain_device *dev, u8 *lun) |
| 1316 | { |
| 1317 | int rc = TMF_RESP_FUNC_FAILED; |
| 1318 | struct pm8001_tmf_task tmf_task; |
| 1319 | |
| 1320 | tmf_task.tmf = TMF_CLEAR_ACA; |
| 1321 | rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task); |
| 1322 | |
| 1323 | return rc; |
| 1324 | } |
| 1325 | |
| 1326 | int pm8001_clear_task_set(struct domain_device *dev, u8 *lun) |
| 1327 | { |
| 1328 | int rc = TMF_RESP_FUNC_FAILED; |
| 1329 | struct pm8001_tmf_task tmf_task; |
| 1330 | struct pm8001_device *pm8001_dev = dev->lldd_dev; |
| 1331 | struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev); |
| 1332 | |
| 1333 | PM8001_EH_DBG(pm8001_ha, |
| 1334 | pm8001_printk("I_T_L_Q clear task set[%x]\n", |
| 1335 | pm8001_dev->device_id)); |
| 1336 | tmf_task.tmf = TMF_CLEAR_TASK_SET; |
| 1337 | rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task); |
| 1338 | return rc; |
| 1339 | } |
| 1340 | |