Linus Torvalds | ba6d10a | 2019-07-11 15:14:01 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Serial Attached SCSI (SAS) Transport Layer initialization |
| 4 | * |
| 5 | * Copyright (C) 2005 Adaptec, Inc. All rights reserved. |
| 6 | * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com> |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 11 | #include <linux/init.h> |
| 12 | #include <linux/device.h> |
| 13 | #include <linux/spinlock.h> |
Dan Williams | 81c757b | 2011-12-02 16:07:01 -0800 | [diff] [blame] | 14 | #include <scsi/sas_ata.h> |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 15 | #include <scsi/scsi_host.h> |
| 16 | #include <scsi/scsi_device.h> |
| 17 | #include <scsi/scsi_transport.h> |
| 18 | #include <scsi/scsi_transport_sas.h> |
| 19 | |
| 20 | #include "sas_internal.h" |
| 21 | |
Jason Yan | e15f669 | 2021-07-16 15:45:51 +0800 | [diff] [blame] | 22 | #include "scsi_sas_internal.h" |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 23 | |
Dan Williams | 4fcf812 | 2011-07-29 17:26:39 -0700 | [diff] [blame] | 24 | static struct kmem_cache *sas_task_cache; |
Jason Yan | 1c393b9 | 2017-12-08 17:42:04 +0800 | [diff] [blame] | 25 | static struct kmem_cache *sas_event_cache; |
Dan Williams | 4fcf812 | 2011-07-29 17:26:39 -0700 | [diff] [blame] | 26 | |
| 27 | struct sas_task *sas_alloc_task(gfp_t flags) |
| 28 | { |
| 29 | struct sas_task *task = kmem_cache_zalloc(sas_task_cache, flags); |
| 30 | |
| 31 | if (task) { |
Dan Williams | 4fcf812 | 2011-07-29 17:26:39 -0700 | [diff] [blame] | 32 | spin_lock_init(&task->task_state_lock); |
| 33 | task->task_state_flags = SAS_TASK_STATE_PENDING; |
Dan Williams | 4fcf812 | 2011-07-29 17:26:39 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | return task; |
| 37 | } |
| 38 | EXPORT_SYMBOL_GPL(sas_alloc_task); |
| 39 | |
Dan Williams | f0bf750 | 2012-06-21 23:36:30 -0700 | [diff] [blame] | 40 | struct sas_task *sas_alloc_slow_task(gfp_t flags) |
| 41 | { |
| 42 | struct sas_task *task = sas_alloc_task(flags); |
| 43 | struct sas_task_slow *slow = kmalloc(sizeof(*slow), flags); |
| 44 | |
| 45 | if (!task || !slow) { |
| 46 | if (task) |
| 47 | kmem_cache_free(sas_task_cache, task); |
| 48 | kfree(slow); |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | task->slow_task = slow; |
Kees Cook | 77570ee | 2017-08-22 16:05:14 -0700 | [diff] [blame] | 53 | slow->task = task; |
| 54 | timer_setup(&slow->timer, NULL, 0); |
Dan Williams | f0bf750 | 2012-06-21 23:36:30 -0700 | [diff] [blame] | 55 | init_completion(&slow->completion); |
| 56 | |
| 57 | return task; |
| 58 | } |
| 59 | EXPORT_SYMBOL_GPL(sas_alloc_slow_task); |
| 60 | |
Dan Williams | 4fcf812 | 2011-07-29 17:26:39 -0700 | [diff] [blame] | 61 | void sas_free_task(struct sas_task *task) |
| 62 | { |
| 63 | if (task) { |
Dan Williams | f0bf750 | 2012-06-21 23:36:30 -0700 | [diff] [blame] | 64 | kfree(task->slow_task); |
Dan Williams | 4fcf812 | 2011-07-29 17:26:39 -0700 | [diff] [blame] | 65 | kmem_cache_free(sas_task_cache, task); |
| 66 | } |
| 67 | } |
| 68 | EXPORT_SYMBOL_GPL(sas_free_task); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 69 | |
| 70 | /*------------ SAS addr hash -----------*/ |
| 71 | void sas_hash_addr(u8 *hashed, const u8 *sas_addr) |
| 72 | { |
John Garry | 7b27c5f | 2019-04-12 16:57:52 +0800 | [diff] [blame] | 73 | const u32 poly = 0x00DB2777; |
| 74 | u32 r = 0; |
| 75 | int i; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 76 | |
John Garry | 7b27c5f | 2019-04-12 16:57:52 +0800 | [diff] [blame] | 77 | for (i = 0; i < SAS_ADDR_SIZE; i++) { |
| 78 | int b; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 79 | |
John Garry | 7b27c5f | 2019-04-12 16:57:52 +0800 | [diff] [blame] | 80 | for (b = (SAS_ADDR_SIZE - 1); b >= 0; b--) { |
| 81 | r <<= 1; |
| 82 | if ((1 << b) & sas_addr[i]) { |
| 83 | if (!(r & 0x01000000)) |
| 84 | r ^= poly; |
| 85 | } else if (r & 0x01000000) { |
| 86 | r ^= poly; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | hashed[0] = (r >> 16) & 0xFF; |
| 92 | hashed[1] = (r >> 8) & 0xFF; |
| 93 | hashed[2] = r & 0xFF; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 94 | } |
| 95 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 96 | int sas_register_ha(struct sas_ha_struct *sas_ha) |
| 97 | { |
Jason Yan | 93bdbd0 | 2017-12-08 17:42:07 +0800 | [diff] [blame] | 98 | char name[64]; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 99 | int error = 0; |
| 100 | |
Dan Williams | 87c8331 | 2011-11-17 17:59:51 -0800 | [diff] [blame] | 101 | mutex_init(&sas_ha->disco_mutex); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 102 | spin_lock_init(&sas_ha->phy_port_lock); |
| 103 | sas_hash_addr(sas_ha->hashed_sas_addr, sas_ha->sas_addr); |
| 104 | |
Dan Williams | f8daa6e | 2011-12-19 17:02:25 -0800 | [diff] [blame] | 105 | set_bit(SAS_HA_REGISTERED, &sas_ha->state); |
Dan Williams | e4a9c37 | 2012-06-21 23:25:27 -0700 | [diff] [blame] | 106 | spin_lock_init(&sas_ha->lock); |
Dan Williams | b1124cd | 2011-12-19 16:42:34 -0800 | [diff] [blame] | 107 | mutex_init(&sas_ha->drain_mutex); |
Dan Williams | 5db45bd | 2012-06-21 23:30:48 -0700 | [diff] [blame] | 108 | init_waitqueue_head(&sas_ha->eh_wait_q); |
Dan Williams | b1124cd | 2011-12-19 16:42:34 -0800 | [diff] [blame] | 109 | INIT_LIST_HEAD(&sas_ha->defer_q); |
Dan Williams | 5db45bd | 2012-06-21 23:30:48 -0700 | [diff] [blame] | 110 | INIT_LIST_HEAD(&sas_ha->eh_dev_q); |
Darrick J. Wong | 6b0efb8 | 2007-01-11 14:15:43 -0800 | [diff] [blame] | 111 | |
Jason Yan | f12486e | 2017-12-08 17:42:05 +0800 | [diff] [blame] | 112 | sas_ha->event_thres = SAS_PHY_SHUTDOWN_THRES; |
| 113 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 114 | error = sas_register_phys(sas_ha); |
| 115 | if (error) { |
John Garry | 15ba780 | 2018-11-15 18:20:31 +0800 | [diff] [blame] | 116 | pr_notice("couldn't register sas phys:%d\n", error); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 117 | return error; |
| 118 | } |
| 119 | |
| 120 | error = sas_register_ports(sas_ha); |
| 121 | if (error) { |
John Garry | 15ba780 | 2018-11-15 18:20:31 +0800 | [diff] [blame] | 122 | pr_notice("couldn't register sas ports:%d\n", error); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 123 | goto Undo_phys; |
| 124 | } |
| 125 | |
Jason Yan | 93bdbd0 | 2017-12-08 17:42:07 +0800 | [diff] [blame] | 126 | error = -ENOMEM; |
| 127 | snprintf(name, sizeof(name), "%s_event_q", dev_name(sas_ha->dev)); |
| 128 | sas_ha->event_q = create_singlethread_workqueue(name); |
| 129 | if (!sas_ha->event_q) |
| 130 | goto Undo_ports; |
| 131 | |
| 132 | snprintf(name, sizeof(name), "%s_disco_q", dev_name(sas_ha->dev)); |
| 133 | sas_ha->disco_q = create_singlethread_workqueue(name); |
| 134 | if (!sas_ha->disco_q) |
| 135 | goto Undo_event_q; |
| 136 | |
Darrick J. Wong | f456393 | 2006-10-30 15:18:39 -0800 | [diff] [blame] | 137 | INIT_LIST_HEAD(&sas_ha->eh_done_q); |
Dan Williams | 3944f50 | 2011-11-29 12:08:50 -0800 | [diff] [blame] | 138 | INIT_LIST_HEAD(&sas_ha->eh_ata_q); |
Darrick J. Wong | f456393 | 2006-10-30 15:18:39 -0800 | [diff] [blame] | 139 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 140 | return 0; |
Jason Yan | 93bdbd0 | 2017-12-08 17:42:07 +0800 | [diff] [blame] | 141 | |
| 142 | Undo_event_q: |
| 143 | destroy_workqueue(sas_ha->event_q); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 144 | Undo_ports: |
| 145 | sas_unregister_ports(sas_ha); |
| 146 | Undo_phys: |
| 147 | |
| 148 | return error; |
| 149 | } |
John Garry | ce4fc33 | 2021-09-13 18:51:36 +0800 | [diff] [blame] | 150 | EXPORT_SYMBOL_GPL(sas_register_ha); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 151 | |
Dan Williams | 303694e | 2012-06-21 23:41:51 -0700 | [diff] [blame] | 152 | static void sas_disable_events(struct sas_ha_struct *sas_ha) |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 153 | { |
Dan Williams | b1124cd | 2011-12-19 16:42:34 -0800 | [diff] [blame] | 154 | /* Set the state to unregistered to avoid further unchained |
Dan Williams | 5d7f6d1 | 2012-01-12 11:47:24 -0800 | [diff] [blame] | 155 | * events to be queued, and flush any in-progress drainers |
Dan Williams | b1124cd | 2011-12-19 16:42:34 -0800 | [diff] [blame] | 156 | */ |
Dan Williams | 5d7f6d1 | 2012-01-12 11:47:24 -0800 | [diff] [blame] | 157 | mutex_lock(&sas_ha->drain_mutex); |
Dan Williams | e4a9c37 | 2012-06-21 23:25:27 -0700 | [diff] [blame] | 158 | spin_lock_irq(&sas_ha->lock); |
Dan Williams | f8daa6e | 2011-12-19 17:02:25 -0800 | [diff] [blame] | 159 | clear_bit(SAS_HA_REGISTERED, &sas_ha->state); |
Dan Williams | e4a9c37 | 2012-06-21 23:25:27 -0700 | [diff] [blame] | 160 | spin_unlock_irq(&sas_ha->lock); |
Dan Williams | 5d7f6d1 | 2012-01-12 11:47:24 -0800 | [diff] [blame] | 161 | __sas_drain_work(sas_ha); |
| 162 | mutex_unlock(&sas_ha->drain_mutex); |
Dan Williams | 303694e | 2012-06-21 23:41:51 -0700 | [diff] [blame] | 163 | } |
Darrick J. Wong | 6b0efb8 | 2007-01-11 14:15:43 -0800 | [diff] [blame] | 164 | |
Dan Williams | 303694e | 2012-06-21 23:41:51 -0700 | [diff] [blame] | 165 | int sas_unregister_ha(struct sas_ha_struct *sas_ha) |
| 166 | { |
| 167 | sas_disable_events(sas_ha); |
Darrick J. Wong | cde3f74 | 2007-01-11 14:15:03 -0800 | [diff] [blame] | 168 | sas_unregister_ports(sas_ha); |
Dan Williams | 5d7f6d1 | 2012-01-12 11:47:24 -0800 | [diff] [blame] | 169 | |
| 170 | /* flush unregistration work */ |
| 171 | mutex_lock(&sas_ha->drain_mutex); |
| 172 | __sas_drain_work(sas_ha); |
| 173 | mutex_unlock(&sas_ha->drain_mutex); |
Darrick J. Wong | cde3f74 | 2007-01-11 14:15:03 -0800 | [diff] [blame] | 174 | |
Jason Yan | 93bdbd0 | 2017-12-08 17:42:07 +0800 | [diff] [blame] | 175 | destroy_workqueue(sas_ha->disco_q); |
| 176 | destroy_workqueue(sas_ha->event_q); |
| 177 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 178 | return 0; |
| 179 | } |
John Garry | ce4fc33 | 2021-09-13 18:51:36 +0800 | [diff] [blame] | 180 | EXPORT_SYMBOL_GPL(sas_unregister_ha); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 181 | |
| 182 | static int sas_get_linkerrors(struct sas_phy *phy) |
| 183 | { |
Dan Williams | ac013ed1 | 2011-09-28 18:48:02 -0700 | [diff] [blame] | 184 | if (scsi_is_sas_phy_local(phy)) { |
| 185 | struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); |
| 186 | struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost); |
| 187 | struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number]; |
| 188 | struct sas_internal *i = |
| 189 | to_sas_internal(sas_ha->core.shost->transportt); |
| 190 | |
| 191 | return i->dft->lldd_control_phy(asd_phy, PHY_FUNC_GET_EVENTS, NULL); |
| 192 | } |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 193 | |
| 194 | return sas_smp_get_phy_events(phy); |
| 195 | } |
| 196 | |
Dan Williams | ab52663 | 2012-01-11 13:13:44 -0800 | [diff] [blame] | 197 | int sas_try_ata_reset(struct asd_sas_phy *asd_phy) |
| 198 | { |
| 199 | struct domain_device *dev = NULL; |
| 200 | |
| 201 | /* try to route user requested link resets through libata */ |
| 202 | if (asd_phy->port) |
| 203 | dev = asd_phy->port->port_dev; |
| 204 | |
| 205 | /* validate that dev has been probed */ |
| 206 | if (dev) |
| 207 | dev = sas_find_dev_by_rphy(dev->rphy); |
| 208 | |
| 209 | if (dev && dev_is_sata(dev)) { |
| 210 | sas_ata_schedule_reset(dev); |
| 211 | sas_ata_wait_eh(dev); |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | return -ENODEV; |
| 216 | } |
| 217 | |
Bart Van Assche | 121246a | 2018-02-22 13:49:59 -0800 | [diff] [blame] | 218 | /* |
Dan Williams | 81c757b | 2011-12-02 16:07:01 -0800 | [diff] [blame] | 219 | * transport_sas_phy_reset - reset a phy and permit libata to manage the link |
| 220 | * |
| 221 | * phy reset request via sysfs in host workqueue context so we know we |
| 222 | * can block on eh and safely traverse the domain_device topology |
| 223 | */ |
| 224 | static int transport_sas_phy_reset(struct sas_phy *phy, int hard_reset) |
| 225 | { |
Dan Williams | 81c757b | 2011-12-02 16:07:01 -0800 | [diff] [blame] | 226 | enum phy_func reset_type; |
| 227 | |
| 228 | if (hard_reset) |
| 229 | reset_type = PHY_FUNC_HARD_RESET; |
| 230 | else |
| 231 | reset_type = PHY_FUNC_LINK_RESET; |
| 232 | |
| 233 | if (scsi_is_sas_phy_local(phy)) { |
| 234 | struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); |
| 235 | struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost); |
| 236 | struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number]; |
| 237 | struct sas_internal *i = |
| 238 | to_sas_internal(sas_ha->core.shost->transportt); |
Dan Williams | 81c757b | 2011-12-02 16:07:01 -0800 | [diff] [blame] | 239 | |
Dan Williams | ab52663 | 2012-01-11 13:13:44 -0800 | [diff] [blame] | 240 | if (!hard_reset && sas_try_ata_reset(asd_phy) == 0) |
| 241 | return 0; |
| 242 | return i->dft->lldd_control_phy(asd_phy, reset_type, NULL); |
Dan Williams | 81c757b | 2011-12-02 16:07:01 -0800 | [diff] [blame] | 243 | } else { |
| 244 | struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent); |
| 245 | struct domain_device *ddev = sas_find_dev_by_rphy(rphy); |
| 246 | struct domain_device *ata_dev = sas_ex_to_ata(ddev, phy->number); |
| 247 | |
| 248 | if (ata_dev && !hard_reset) { |
| 249 | sas_ata_schedule_reset(ata_dev); |
| 250 | sas_ata_wait_eh(ata_dev); |
Dan Williams | ab52663 | 2012-01-11 13:13:44 -0800 | [diff] [blame] | 251 | return 0; |
Dan Williams | 81c757b | 2011-12-02 16:07:01 -0800 | [diff] [blame] | 252 | } else |
Dan Williams | ab52663 | 2012-01-11 13:13:44 -0800 | [diff] [blame] | 253 | return sas_smp_phy_control(ddev, phy->number, reset_type, NULL); |
Dan Williams | 81c757b | 2011-12-02 16:07:01 -0800 | [diff] [blame] | 254 | } |
Dan Williams | 81c757b | 2011-12-02 16:07:01 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Luo Jiaxing | 00aeaf3 | 2021-10-12 20:26:27 +0800 | [diff] [blame] | 257 | int sas_phy_enable(struct sas_phy *phy, int enable) |
Darrick J. Wong | acbf167 | 2007-01-11 14:14:57 -0800 | [diff] [blame] | 258 | { |
| 259 | int ret; |
Dan Williams | 2a559f4b | 2011-12-04 00:06:57 -0800 | [diff] [blame] | 260 | enum phy_func cmd; |
Darrick J. Wong | acbf167 | 2007-01-11 14:14:57 -0800 | [diff] [blame] | 261 | |
| 262 | if (enable) |
Dan Williams | 2a559f4b | 2011-12-04 00:06:57 -0800 | [diff] [blame] | 263 | cmd = PHY_FUNC_LINK_RESET; |
Darrick J. Wong | acbf167 | 2007-01-11 14:14:57 -0800 | [diff] [blame] | 264 | else |
Dan Williams | 2a559f4b | 2011-12-04 00:06:57 -0800 | [diff] [blame] | 265 | cmd = PHY_FUNC_DISABLE; |
Darrick J. Wong | acbf167 | 2007-01-11 14:14:57 -0800 | [diff] [blame] | 266 | |
| 267 | if (scsi_is_sas_phy_local(phy)) { |
| 268 | struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); |
| 269 | struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost); |
| 270 | struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number]; |
| 271 | struct sas_internal *i = |
| 272 | to_sas_internal(sas_ha->core.shost->transportt); |
| 273 | |
Dan Williams | 2a559f4b | 2011-12-04 00:06:57 -0800 | [diff] [blame] | 274 | if (enable) |
| 275 | ret = transport_sas_phy_reset(phy, 0); |
Jeff Skirvin | 1f4fe89 | 2011-12-16 08:21:21 +0000 | [diff] [blame] | 276 | else |
Dan Williams | 2a559f4b | 2011-12-04 00:06:57 -0800 | [diff] [blame] | 277 | ret = i->dft->lldd_control_phy(asd_phy, cmd, NULL); |
Darrick J. Wong | acbf167 | 2007-01-11 14:14:57 -0800 | [diff] [blame] | 278 | } else { |
| 279 | struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent); |
| 280 | struct domain_device *ddev = sas_find_dev_by_rphy(rphy); |
Dan Williams | 2a559f4b | 2011-12-04 00:06:57 -0800 | [diff] [blame] | 281 | |
| 282 | if (enable) |
| 283 | ret = transport_sas_phy_reset(phy, 0); |
| 284 | else |
| 285 | ret = sas_smp_phy_control(ddev, phy->number, cmd, NULL); |
Darrick J. Wong | acbf167 | 2007-01-11 14:14:57 -0800 | [diff] [blame] | 286 | } |
| 287 | return ret; |
| 288 | } |
Luo Jiaxing | 00aeaf3 | 2021-10-12 20:26:27 +0800 | [diff] [blame] | 289 | EXPORT_SYMBOL_GPL(sas_phy_enable); |
Darrick J. Wong | acbf167 | 2007-01-11 14:14:57 -0800 | [diff] [blame] | 290 | |
Darrick J. Wong | dea2221 | 2006-11-07 17:28:55 -0800 | [diff] [blame] | 291 | int sas_phy_reset(struct sas_phy *phy, int hard_reset) |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 292 | { |
| 293 | int ret; |
| 294 | enum phy_func reset_type; |
| 295 | |
Dan Williams | 26a2e68 | 2012-01-30 21:40:45 -0800 | [diff] [blame] | 296 | if (!phy->enabled) |
| 297 | return -ENODEV; |
| 298 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 299 | if (hard_reset) |
| 300 | reset_type = PHY_FUNC_HARD_RESET; |
| 301 | else |
| 302 | reset_type = PHY_FUNC_LINK_RESET; |
| 303 | |
| 304 | if (scsi_is_sas_phy_local(phy)) { |
| 305 | struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); |
| 306 | struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost); |
| 307 | struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number]; |
| 308 | struct sas_internal *i = |
| 309 | to_sas_internal(sas_ha->core.shost->transportt); |
| 310 | |
James Bottomley | a01e70e | 2006-09-06 19:28:07 -0500 | [diff] [blame] | 311 | ret = i->dft->lldd_control_phy(asd_phy, reset_type, NULL); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 312 | } else { |
| 313 | struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent); |
| 314 | struct domain_device *ddev = sas_find_dev_by_rphy(rphy); |
James Bottomley | a01e70e | 2006-09-06 19:28:07 -0500 | [diff] [blame] | 315 | ret = sas_smp_phy_control(ddev, phy->number, reset_type, NULL); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 316 | } |
| 317 | return ret; |
| 318 | } |
John Garry | ce4fc33 | 2021-09-13 18:51:36 +0800 | [diff] [blame] | 319 | EXPORT_SYMBOL_GPL(sas_phy_reset); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 320 | |
Darrick J. Wong | acbf167 | 2007-01-11 14:14:57 -0800 | [diff] [blame] | 321 | int sas_set_phy_speed(struct sas_phy *phy, |
| 322 | struct sas_phy_linkrates *rates) |
James Bottomley | a01e70e | 2006-09-06 19:28:07 -0500 | [diff] [blame] | 323 | { |
| 324 | int ret; |
| 325 | |
| 326 | if ((rates->minimum_linkrate && |
| 327 | rates->minimum_linkrate > phy->maximum_linkrate) || |
| 328 | (rates->maximum_linkrate && |
| 329 | rates->maximum_linkrate < phy->minimum_linkrate)) |
| 330 | return -EINVAL; |
| 331 | |
| 332 | if (rates->minimum_linkrate && |
| 333 | rates->minimum_linkrate < phy->minimum_linkrate_hw) |
| 334 | rates->minimum_linkrate = phy->minimum_linkrate_hw; |
| 335 | |
| 336 | if (rates->maximum_linkrate && |
| 337 | rates->maximum_linkrate > phy->maximum_linkrate_hw) |
| 338 | rates->maximum_linkrate = phy->maximum_linkrate_hw; |
| 339 | |
| 340 | if (scsi_is_sas_phy_local(phy)) { |
| 341 | struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); |
| 342 | struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(shost); |
| 343 | struct asd_sas_phy *asd_phy = sas_ha->sas_phy[phy->number]; |
| 344 | struct sas_internal *i = |
| 345 | to_sas_internal(sas_ha->core.shost->transportt); |
| 346 | |
| 347 | ret = i->dft->lldd_control_phy(asd_phy, PHY_FUNC_SET_LINK_RATE, |
| 348 | rates); |
| 349 | } else { |
| 350 | struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent); |
| 351 | struct domain_device *ddev = sas_find_dev_by_rphy(rphy); |
| 352 | ret = sas_smp_phy_control(ddev, phy->number, |
| 353 | PHY_FUNC_LINK_RESET, rates); |
| 354 | |
| 355 | } |
| 356 | |
| 357 | return ret; |
| 358 | } |
| 359 | |
Dan Williams | 303694e | 2012-06-21 23:41:51 -0700 | [diff] [blame] | 360 | void sas_prep_resume_ha(struct sas_ha_struct *ha) |
| 361 | { |
| 362 | int i; |
| 363 | |
| 364 | set_bit(SAS_HA_REGISTERED, &ha->state); |
| 365 | |
| 366 | /* clear out any stale link events/data from the suspension path */ |
| 367 | for (i = 0; i < ha->num_phys; i++) { |
| 368 | struct asd_sas_phy *phy = ha->sas_phy[i]; |
| 369 | |
| 370 | memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE); |
Dan Williams | 303694e | 2012-06-21 23:41:51 -0700 | [diff] [blame] | 371 | phy->frame_rcvd_size = 0; |
| 372 | } |
| 373 | } |
| 374 | EXPORT_SYMBOL(sas_prep_resume_ha); |
| 375 | |
| 376 | static int phys_suspended(struct sas_ha_struct *ha) |
| 377 | { |
| 378 | int i, rc = 0; |
| 379 | |
| 380 | for (i = 0; i < ha->num_phys; i++) { |
| 381 | struct asd_sas_phy *phy = ha->sas_phy[i]; |
| 382 | |
| 383 | if (phy->suspended) |
| 384 | rc++; |
| 385 | } |
| 386 | |
| 387 | return rc; |
| 388 | } |
| 389 | |
| 390 | void sas_resume_ha(struct sas_ha_struct *ha) |
| 391 | { |
| 392 | const unsigned long tmo = msecs_to_jiffies(25000); |
| 393 | int i; |
| 394 | |
| 395 | /* deform ports on phys that did not resume |
| 396 | * at this point we may be racing the phy coming back (as posted |
| 397 | * by the lldd). So we post the event and once we are in the |
| 398 | * libsas context check that the phy remains suspended before |
| 399 | * tearing it down. |
| 400 | */ |
| 401 | i = phys_suspended(ha); |
| 402 | if (i) |
| 403 | dev_info(ha->dev, "waiting up to 25 seconds for %d phy%s to resume\n", |
| 404 | i, i > 1 ? "s" : ""); |
| 405 | wait_event_timeout(ha->eh_wait_q, phys_suspended(ha) == 0, tmo); |
| 406 | for (i = 0; i < ha->num_phys; i++) { |
| 407 | struct asd_sas_phy *phy = ha->sas_phy[i]; |
| 408 | |
| 409 | if (phy->suspended) { |
| 410 | dev_warn(&phy->phy->dev, "resume timeout\n"); |
Ahmed S. Darwish | f76d9f1 | 2021-01-18 11:09:52 +0100 | [diff] [blame] | 411 | sas_notify_phy_event(phy, PHYE_RESUME_TIMEOUT, |
| 412 | GFP_KERNEL); |
Dan Williams | 303694e | 2012-06-21 23:41:51 -0700 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
| 416 | /* all phys are back up or timed out, turn on i/o so we can |
| 417 | * flush out disks that did not return |
| 418 | */ |
| 419 | scsi_unblock_requests(ha->core.shost); |
| 420 | sas_drain_work(ha); |
| 421 | } |
| 422 | EXPORT_SYMBOL(sas_resume_ha); |
| 423 | |
| 424 | void sas_suspend_ha(struct sas_ha_struct *ha) |
| 425 | { |
| 426 | int i; |
| 427 | |
| 428 | sas_disable_events(ha); |
| 429 | scsi_block_requests(ha->core.shost); |
| 430 | for (i = 0; i < ha->num_phys; i++) { |
| 431 | struct asd_sas_port *port = ha->sas_port[i]; |
| 432 | |
| 433 | sas_discover_event(port, DISCE_SUSPEND); |
| 434 | } |
| 435 | |
| 436 | /* flush suspend events while unregistered */ |
| 437 | mutex_lock(&ha->drain_mutex); |
| 438 | __sas_drain_work(ha); |
| 439 | mutex_unlock(&ha->drain_mutex); |
| 440 | } |
| 441 | EXPORT_SYMBOL(sas_suspend_ha); |
| 442 | |
Dan Williams | 0b3e09d | 2011-12-20 01:03:48 -0800 | [diff] [blame] | 443 | static void sas_phy_release(struct sas_phy *phy) |
| 444 | { |
| 445 | kfree(phy->hostdata); |
| 446 | phy->hostdata = NULL; |
| 447 | } |
| 448 | |
| 449 | static void phy_reset_work(struct work_struct *work) |
| 450 | { |
Dan Williams | 22b9153 | 2012-03-09 11:00:06 -0800 | [diff] [blame] | 451 | struct sas_phy_data *d = container_of(work, typeof(*d), reset_work.work); |
Dan Williams | 0b3e09d | 2011-12-20 01:03:48 -0800 | [diff] [blame] | 452 | |
Dan Williams | 81c757b | 2011-12-02 16:07:01 -0800 | [diff] [blame] | 453 | d->reset_result = transport_sas_phy_reset(d->phy, d->hard_reset); |
Dan Williams | 0b3e09d | 2011-12-20 01:03:48 -0800 | [diff] [blame] | 454 | } |
| 455 | |
Dan Williams | 2a559f4b | 2011-12-04 00:06:57 -0800 | [diff] [blame] | 456 | static void phy_enable_work(struct work_struct *work) |
| 457 | { |
Dan Williams | 22b9153 | 2012-03-09 11:00:06 -0800 | [diff] [blame] | 458 | struct sas_phy_data *d = container_of(work, typeof(*d), enable_work.work); |
Dan Williams | 2a559f4b | 2011-12-04 00:06:57 -0800 | [diff] [blame] | 459 | |
| 460 | d->enable_result = sas_phy_enable(d->phy, d->enable); |
| 461 | } |
| 462 | |
Dan Williams | 0b3e09d | 2011-12-20 01:03:48 -0800 | [diff] [blame] | 463 | static int sas_phy_setup(struct sas_phy *phy) |
| 464 | { |
| 465 | struct sas_phy_data *d = kzalloc(sizeof(*d), GFP_KERNEL); |
| 466 | |
| 467 | if (!d) |
| 468 | return -ENOMEM; |
| 469 | |
| 470 | mutex_init(&d->event_lock); |
Dan Williams | 22b9153 | 2012-03-09 11:00:06 -0800 | [diff] [blame] | 471 | INIT_SAS_WORK(&d->reset_work, phy_reset_work); |
| 472 | INIT_SAS_WORK(&d->enable_work, phy_enable_work); |
Dan Williams | 0b3e09d | 2011-12-20 01:03:48 -0800 | [diff] [blame] | 473 | d->phy = phy; |
| 474 | phy->hostdata = d; |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | static int queue_phy_reset(struct sas_phy *phy, int hard_reset) |
| 480 | { |
| 481 | struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); |
| 482 | struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); |
| 483 | struct sas_phy_data *d = phy->hostdata; |
| 484 | int rc; |
| 485 | |
| 486 | if (!d) |
| 487 | return -ENOMEM; |
| 488 | |
| 489 | /* libsas workqueue coordinates ata-eh reset with discovery */ |
| 490 | mutex_lock(&d->event_lock); |
| 491 | d->reset_result = 0; |
| 492 | d->hard_reset = hard_reset; |
| 493 | |
Dan Williams | e4a9c37 | 2012-06-21 23:25:27 -0700 | [diff] [blame] | 494 | spin_lock_irq(&ha->lock); |
Dan Williams | 0b3e09d | 2011-12-20 01:03:48 -0800 | [diff] [blame] | 495 | sas_queue_work(ha, &d->reset_work); |
Dan Williams | e4a9c37 | 2012-06-21 23:25:27 -0700 | [diff] [blame] | 496 | spin_unlock_irq(&ha->lock); |
Dan Williams | 0b3e09d | 2011-12-20 01:03:48 -0800 | [diff] [blame] | 497 | |
| 498 | rc = sas_drain_work(ha); |
| 499 | if (rc == 0) |
| 500 | rc = d->reset_result; |
| 501 | mutex_unlock(&d->event_lock); |
| 502 | |
| 503 | return rc; |
| 504 | } |
| 505 | |
Dan Williams | 2a559f4b | 2011-12-04 00:06:57 -0800 | [diff] [blame] | 506 | static int queue_phy_enable(struct sas_phy *phy, int enable) |
| 507 | { |
| 508 | struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); |
| 509 | struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); |
| 510 | struct sas_phy_data *d = phy->hostdata; |
| 511 | int rc; |
| 512 | |
| 513 | if (!d) |
| 514 | return -ENOMEM; |
| 515 | |
| 516 | /* libsas workqueue coordinates ata-eh reset with discovery */ |
| 517 | mutex_lock(&d->event_lock); |
| 518 | d->enable_result = 0; |
| 519 | d->enable = enable; |
| 520 | |
Dan Williams | e4a9c37 | 2012-06-21 23:25:27 -0700 | [diff] [blame] | 521 | spin_lock_irq(&ha->lock); |
Dan Williams | 2a559f4b | 2011-12-04 00:06:57 -0800 | [diff] [blame] | 522 | sas_queue_work(ha, &d->enable_work); |
Dan Williams | e4a9c37 | 2012-06-21 23:25:27 -0700 | [diff] [blame] | 523 | spin_unlock_irq(&ha->lock); |
Dan Williams | 2a559f4b | 2011-12-04 00:06:57 -0800 | [diff] [blame] | 524 | |
| 525 | rc = sas_drain_work(ha); |
| 526 | if (rc == 0) |
| 527 | rc = d->enable_result; |
| 528 | mutex_unlock(&d->event_lock); |
| 529 | |
| 530 | return rc; |
| 531 | } |
| 532 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 533 | static struct sas_function_template sft = { |
Dan Williams | 2a559f4b | 2011-12-04 00:06:57 -0800 | [diff] [blame] | 534 | .phy_enable = queue_phy_enable, |
Dan Williams | 0b3e09d | 2011-12-20 01:03:48 -0800 | [diff] [blame] | 535 | .phy_reset = queue_phy_reset, |
| 536 | .phy_setup = sas_phy_setup, |
| 537 | .phy_release = sas_phy_release, |
James Bottomley | a01e70e | 2006-09-06 19:28:07 -0500 | [diff] [blame] | 538 | .set_phy_speed = sas_set_phy_speed, |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 539 | .get_linkerrors = sas_get_linkerrors, |
FUJITA Tomonori | ba1fc17 | 2007-07-09 12:52:08 +0900 | [diff] [blame] | 540 | .smp_handler = sas_smp_handler, |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 541 | }; |
| 542 | |
Jason Yan | 8eea9dd | 2017-12-08 17:42:06 +0800 | [diff] [blame] | 543 | static inline ssize_t phy_event_threshold_show(struct device *dev, |
| 544 | struct device_attribute *attr, char *buf) |
| 545 | { |
| 546 | struct Scsi_Host *shost = class_to_shost(dev); |
| 547 | struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost); |
| 548 | |
| 549 | return scnprintf(buf, PAGE_SIZE, "%u\n", sha->event_thres); |
| 550 | } |
| 551 | |
| 552 | static inline ssize_t phy_event_threshold_store(struct device *dev, |
| 553 | struct device_attribute *attr, |
| 554 | const char *buf, size_t count) |
| 555 | { |
| 556 | struct Scsi_Host *shost = class_to_shost(dev); |
| 557 | struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost); |
| 558 | |
| 559 | sha->event_thres = simple_strtol(buf, NULL, 10); |
| 560 | |
| 561 | /* threshold cannot be set too small */ |
| 562 | if (sha->event_thres < 32) |
| 563 | sha->event_thres = 32; |
| 564 | |
| 565 | return count; |
| 566 | } |
| 567 | |
| 568 | DEVICE_ATTR(phy_event_threshold, |
| 569 | S_IRUGO|S_IWUSR, |
| 570 | phy_event_threshold_show, |
| 571 | phy_event_threshold_store); |
| 572 | EXPORT_SYMBOL_GPL(dev_attr_phy_event_threshold); |
| 573 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 574 | struct scsi_transport_template * |
| 575 | sas_domain_attach_transport(struct sas_domain_function_template *dft) |
| 576 | { |
| 577 | struct scsi_transport_template *stt = sas_attach_transport(&sft); |
| 578 | struct sas_internal *i; |
| 579 | |
| 580 | if (!stt) |
| 581 | return stt; |
| 582 | |
| 583 | i = to_sas_internal(stt); |
| 584 | i->dft = dft; |
| 585 | stt->create_work_queue = 1; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 586 | stt->eh_strategy_handler = sas_scsi_recover_host; |
| 587 | |
| 588 | return stt; |
| 589 | } |
| 590 | EXPORT_SYMBOL_GPL(sas_domain_attach_transport); |
| 591 | |
Ahmed S. Darwish | 5d6a75a | 2021-01-18 11:09:48 +0100 | [diff] [blame] | 592 | struct asd_sas_event *sas_alloc_event(struct asd_sas_phy *phy, |
| 593 | gfp_t gfp_flags) |
Jason Yan | 1c393b9 | 2017-12-08 17:42:04 +0800 | [diff] [blame] | 594 | { |
Jason Yan | f12486e | 2017-12-08 17:42:05 +0800 | [diff] [blame] | 595 | struct asd_sas_event *event; |
Jason Yan | f12486e | 2017-12-08 17:42:05 +0800 | [diff] [blame] | 596 | struct sas_ha_struct *sas_ha = phy->ha; |
| 597 | struct sas_internal *i = |
| 598 | to_sas_internal(sas_ha->core.shost->transportt); |
Jason Yan | 1c393b9 | 2017-12-08 17:42:04 +0800 | [diff] [blame] | 599 | |
Ahmed S. Darwish | c2d0f1a | 2021-01-18 11:09:39 +0100 | [diff] [blame] | 600 | event = kmem_cache_zalloc(sas_event_cache, gfp_flags); |
Jason Yan | f12486e | 2017-12-08 17:42:05 +0800 | [diff] [blame] | 601 | if (!event) |
| 602 | return NULL; |
| 603 | |
| 604 | atomic_inc(&phy->event_nr); |
| 605 | |
| 606 | if (atomic_read(&phy->event_nr) > phy->ha->event_thres) { |
| 607 | if (i->dft->lldd_control_phy) { |
| 608 | if (cmpxchg(&phy->in_shutdown, 0, 1) == 0) { |
John Garry | 3c236f8 | 2019-04-12 16:57:57 +0800 | [diff] [blame] | 609 | pr_notice("The phy%d bursting events, shut it down.\n", |
John Garry | 71a4a99 | 2018-11-15 18:20:30 +0800 | [diff] [blame] | 610 | phy->id); |
Ahmed S. Darwish | f76d9f1 | 2021-01-18 11:09:52 +0100 | [diff] [blame] | 611 | sas_notify_phy_event(phy, PHYE_SHUTDOWN, |
| 612 | gfp_flags); |
Jason Yan | f12486e | 2017-12-08 17:42:05 +0800 | [diff] [blame] | 613 | } |
| 614 | } else { |
| 615 | /* Do not support PHY control, stop allocating events */ |
| 616 | WARN_ONCE(1, "PHY control not supported.\n"); |
| 617 | kmem_cache_free(sas_event_cache, event); |
| 618 | atomic_dec(&phy->event_nr); |
| 619 | event = NULL; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | return event; |
Jason Yan | 1c393b9 | 2017-12-08 17:42:04 +0800 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | void sas_free_event(struct asd_sas_event *event) |
| 627 | { |
Jason Yan | f12486e | 2017-12-08 17:42:05 +0800 | [diff] [blame] | 628 | struct asd_sas_phy *phy = event->phy; |
| 629 | |
Jason Yan | 1c393b9 | 2017-12-08 17:42:04 +0800 | [diff] [blame] | 630 | kmem_cache_free(sas_event_cache, event); |
Jason Yan | f12486e | 2017-12-08 17:42:05 +0800 | [diff] [blame] | 631 | atomic_dec(&phy->event_nr); |
Jason Yan | 1c393b9 | 2017-12-08 17:42:04 +0800 | [diff] [blame] | 632 | } |
| 633 | |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 634 | /* ---------- SAS Class register/unregister ---------- */ |
| 635 | |
| 636 | static int __init sas_class_init(void) |
| 637 | { |
Dan Williams | 4fcf812 | 2011-07-29 17:26:39 -0700 | [diff] [blame] | 638 | sas_task_cache = KMEM_CACHE(sas_task, SLAB_HWCACHE_ALIGN); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 639 | if (!sas_task_cache) |
Jason Yan | 1c393b9 | 2017-12-08 17:42:04 +0800 | [diff] [blame] | 640 | goto out; |
| 641 | |
| 642 | sas_event_cache = KMEM_CACHE(asd_sas_event, SLAB_HWCACHE_ALIGN); |
| 643 | if (!sas_event_cache) |
| 644 | goto free_task_kmem; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 645 | |
| 646 | return 0; |
Jason Yan | 1c393b9 | 2017-12-08 17:42:04 +0800 | [diff] [blame] | 647 | free_task_kmem: |
| 648 | kmem_cache_destroy(sas_task_cache); |
| 649 | out: |
| 650 | return -ENOMEM; |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | static void __exit sas_class_exit(void) |
| 654 | { |
| 655 | kmem_cache_destroy(sas_task_cache); |
Jason Yan | 1c393b9 | 2017-12-08 17:42:04 +0800 | [diff] [blame] | 656 | kmem_cache_destroy(sas_event_cache); |
James Bottomley | 2908d77 | 2006-08-29 09:22:51 -0500 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | MODULE_AUTHOR("Luben Tuikov <luben_tuikov@adaptec.com>"); |
| 660 | MODULE_DESCRIPTION("SAS Transport Layer"); |
| 661 | MODULE_LICENSE("GPL v2"); |
| 662 | |
| 663 | module_init(sas_class_init); |
| 664 | module_exit(sas_class_exit); |
| 665 | |