1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Serial Attached SCSI (SAS) Discover process
4  *
5  * Copyright (C) 2005 Adaptec, Inc.  All rights reserved.
6  * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
7  */
8 
9 #include <linux/scatterlist.h>
10 #include <linux/slab.h>
11 #include <linux/async.h>
12 #include <scsi/scsi_host.h>
13 #include <scsi/scsi_eh.h>
14 #include "sas_internal.h"
15 
16 #include <scsi/scsi_transport.h>
17 #include <scsi/scsi_transport_sas.h>
18 #include <scsi/sas_ata.h>
19 #include "../scsi_sas_internal.h"
20 
21 /* ---------- Basic task processing for discovery purposes ---------- */
22 
23 void sas_init_dev(struct domain_device *dev)
24 {
25 	switch (dev->dev_type) {
26 	case SAS_END_DEVICE:
27 		INIT_LIST_HEAD(&dev->ssp_dev.eh_list_node);
28 		break;
29 	case SAS_EDGE_EXPANDER_DEVICE:
30 	case SAS_FANOUT_EXPANDER_DEVICE:
31 		INIT_LIST_HEAD(&dev->ex_dev.children);
32 		mutex_init(&dev->ex_dev.cmd_mutex);
33 		break;
34 	default:
35 		break;
36 	}
37 }
38 
39 /* ---------- Domain device discovery ---------- */
40 
41 /**
42  * sas_get_port_device - Discover devices which caused port creation
43  * @port: pointer to struct sas_port of interest
44  *
45  * Devices directly attached to a HA port, have no parent.  This is
46  * how we know they are (domain) "root" devices.  All other devices
47  * do, and should have their "parent" pointer set appropriately as
48  * soon as a child device is discovered.
49  */
50 static int sas_get_port_device(struct asd_sas_port *port)
51 {
52 	struct asd_sas_phy *phy;
53 	struct sas_rphy *rphy;
54 	struct domain_device *dev;
55 	int rc = -ENODEV;
56 
57 	dev = sas_alloc_device();
58 	if (!dev)
59 		return -ENOMEM;
60 
61 	spin_lock_irq(&port->phy_list_lock);
62 	if (list_empty(&port->phy_list)) {
63 		spin_unlock_irq(&port->phy_list_lock);
64 		sas_put_device(dev);
65 		return -ENODEV;
66 	}
67 	phy = container_of(port->phy_list.next, struct asd_sas_phy, port_phy_el);
68 	spin_lock(&phy->frame_rcvd_lock);
69 	memcpy(dev->frame_rcvd, phy->frame_rcvd, min(sizeof(dev->frame_rcvd),
70 					     (size_t)phy->frame_rcvd_size));
71 	spin_unlock(&phy->frame_rcvd_lock);
72 	spin_unlock_irq(&port->phy_list_lock);
73 
74 	if (dev->frame_rcvd[0] == 0x34 && port->oob_mode == SATA_OOB_MODE) {
75 		struct dev_to_host_fis *fis =
76 			(struct dev_to_host_fis *) dev->frame_rcvd;
77 		if (fis->interrupt_reason == 1 && fis->lbal == 1 &&
78 		    fis->byte_count_low==0x69 && fis->byte_count_high == 0x96
79 		    && (fis->device & ~0x10) == 0)
80 			dev->dev_type = SAS_SATA_PM;
81 		else
82 			dev->dev_type = SAS_SATA_DEV;
83 		dev->tproto = SAS_PROTOCOL_SATA;
84 	} else if (port->oob_mode == SAS_OOB_MODE) {
85 		struct sas_identify_frame *id =
86 			(struct sas_identify_frame *) dev->frame_rcvd;
87 		dev->dev_type = id->dev_type;
88 		dev->iproto = id->initiator_bits;
89 		dev->tproto = id->target_bits;
90 	} else {
91 		/* If the oob mode is OOB_NOT_CONNECTED, the port is
92 		 * disconnected due to race with PHY down. We cannot
93 		 * continue to discover this port
94 		 */
95 		sas_put_device(dev);
96 		pr_warn("Port %016llx is disconnected when discovering\n",
97 			SAS_ADDR(port->attached_sas_addr));
98 		return -ENODEV;
99 	}
100 
101 	sas_init_dev(dev);
102 
103 	dev->port = port;
104 	switch (dev->dev_type) {
105 	case SAS_SATA_DEV:
106 		rc = sas_ata_init(dev);
107 		if (rc) {
108 			rphy = NULL;
109 			break;
110 		}
111 		fallthrough;
112 	case SAS_END_DEVICE:
113 		rphy = sas_end_device_alloc(port->port);
114 		break;
115 	case SAS_EDGE_EXPANDER_DEVICE:
116 		rphy = sas_expander_alloc(port->port,
117 					  SAS_EDGE_EXPANDER_DEVICE);
118 		break;
119 	case SAS_FANOUT_EXPANDER_DEVICE:
120 		rphy = sas_expander_alloc(port->port,
121 					  SAS_FANOUT_EXPANDER_DEVICE);
122 		break;
123 	default:
124 		pr_warn("ERROR: Unidentified device type %d\n", dev->dev_type);
125 		rphy = NULL;
126 		break;
127 	}
128 
129 	if (!rphy) {
130 		sas_put_device(dev);
131 		return rc;
132 	}
133 
134 	rphy->identify.phy_identifier = phy->phy->identify.phy_identifier;
135 	memcpy(dev->sas_addr, port->attached_sas_addr, SAS_ADDR_SIZE);
136 	sas_fill_in_rphy(dev, rphy);
137 	sas_hash_addr(dev->hashed_sas_addr, dev->sas_addr);
138 	port->port_dev = dev;
139 	dev->linkrate = port->linkrate;
140 	dev->min_linkrate = port->linkrate;
141 	dev->max_linkrate = port->linkrate;
142 	dev->pathways = port->num_phys;
143 	memset(port->disc.fanout_sas_addr, 0, SAS_ADDR_SIZE);
144 	memset(port->disc.eeds_a, 0, SAS_ADDR_SIZE);
145 	memset(port->disc.eeds_b, 0, SAS_ADDR_SIZE);
146 	port->disc.max_level = 0;
147 	sas_device_set_phy(dev, port->port);
148 
149 	dev->rphy = rphy;
150 	get_device(&dev->rphy->dev);
151 
152 	if (dev_is_sata(dev) || dev->dev_type == SAS_END_DEVICE)
153 		list_add_tail(&dev->disco_list_node, &port->disco_list);
154 	else {
155 		spin_lock_irq(&port->dev_list_lock);
156 		list_add_tail(&dev->dev_list_node, &port->dev_list);
157 		spin_unlock_irq(&port->dev_list_lock);
158 	}
159 
160 	spin_lock_irq(&port->phy_list_lock);
161 	list_for_each_entry(phy, &port->phy_list, port_phy_el)
162 		sas_phy_set_target(phy, dev);
163 	spin_unlock_irq(&port->phy_list_lock);
164 
165 	return 0;
166 }
167 
168 /* ---------- Discover and Revalidate ---------- */
169 
170 int sas_notify_lldd_dev_found(struct domain_device *dev)
171 {
172 	int res = 0;
173 	struct sas_ha_struct *sas_ha = dev->port->ha;
174 	struct Scsi_Host *shost = sas_ha->core.shost;
175 	struct sas_internal *i = to_sas_internal(shost->transportt);
176 
177 	if (!i->dft->lldd_dev_found)
178 		return 0;
179 
180 	res = i->dft->lldd_dev_found(dev);
181 	if (res) {
182 		pr_warn("driver on host %s cannot handle device %016llx, error:%d\n",
183 			dev_name(sas_ha->dev),
184 			SAS_ADDR(dev->sas_addr), res);
185 		return res;
186 	}
187 	set_bit(SAS_DEV_FOUND, &dev->state);
188 	kref_get(&dev->kref);
189 	return 0;
190 }
191 
192 
193 void sas_notify_lldd_dev_gone(struct domain_device *dev)
194 {
195 	struct sas_ha_struct *sas_ha = dev->port->ha;
196 	struct Scsi_Host *shost = sas_ha->core.shost;
197 	struct sas_internal *i = to_sas_internal(shost->transportt);
198 
199 	if (!i->dft->lldd_dev_gone)
200 		return;
201 
202 	if (test_and_clear_bit(SAS_DEV_FOUND, &dev->state)) {
203 		i->dft->lldd_dev_gone(dev);
204 		sas_put_device(dev);
205 	}
206 }
207 
208 static void sas_probe_devices(struct asd_sas_port *port)
209 {
210 	struct domain_device *dev, *n;
211 
212 	/* devices must be domain members before link recovery and probe */
213 	list_for_each_entry(dev, &port->disco_list, disco_list_node) {
214 		spin_lock_irq(&port->dev_list_lock);
215 		list_add_tail(&dev->dev_list_node, &port->dev_list);
216 		spin_unlock_irq(&port->dev_list_lock);
217 	}
218 
219 	sas_probe_sata(port);
220 
221 	list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node) {
222 		int err;
223 
224 		err = sas_rphy_add(dev->rphy);
225 		if (err)
226 			sas_fail_probe(dev, __func__, err);
227 		else
228 			list_del_init(&dev->disco_list_node);
229 	}
230 }
231 
232 static void sas_suspend_devices(struct work_struct *work)
233 {
234 	struct asd_sas_phy *phy;
235 	struct domain_device *dev;
236 	struct sas_discovery_event *ev = to_sas_discovery_event(work);
237 	struct asd_sas_port *port = ev->port;
238 	struct Scsi_Host *shost = port->ha->core.shost;
239 	struct sas_internal *si = to_sas_internal(shost->transportt);
240 
241 	clear_bit(DISCE_SUSPEND, &port->disc.pending);
242 
243 	sas_suspend_sata(port);
244 
245 	/* lldd is free to forget the domain_device across the
246 	 * suspension, we force the issue here to keep the reference
247 	 * counts aligned
248 	 */
249 	list_for_each_entry(dev, &port->dev_list, dev_list_node)
250 		sas_notify_lldd_dev_gone(dev);
251 
252 	/* we are suspending, so we know events are disabled and
253 	 * phy_list is not being mutated
254 	 */
255 	list_for_each_entry(phy, &port->phy_list, port_phy_el) {
256 		if (si->dft->lldd_port_deformed)
257 			si->dft->lldd_port_deformed(phy);
258 		phy->suspended = 1;
259 		port->suspended = 1;
260 	}
261 }
262 
263 static void sas_resume_devices(struct work_struct *work)
264 {
265 	struct sas_discovery_event *ev = to_sas_discovery_event(work);
266 	struct asd_sas_port *port = ev->port;
267 
268 	clear_bit(DISCE_RESUME, &port->disc.pending);
269 
270 	sas_resume_sata(port);
271 }
272 
273 /**
274  * sas_discover_end_dev - discover an end device (SSP, etc)
275  * @dev: pointer to domain device of interest
276  *
277  * See comment in sas_discover_sata().
278  */
279 int sas_discover_end_dev(struct domain_device *dev)
280 {
281 	int res;
282 
283 	res = sas_notify_lldd_dev_found(dev);
284 	if (res)
285 		return res;
286 
287 	return 0;
288 }
289 
290 /* ---------- Device registration and unregistration ---------- */
291 
292 void sas_free_device(struct kref *kref)
293 {
294 	struct domain_device *dev = container_of(kref, typeof(*dev), kref);
295 
296 	put_device(&dev->rphy->dev);
297 	dev->rphy = NULL;
298 
299 	if (dev->parent)
300 		sas_put_device(dev->parent);
301 
302 	sas_port_put_phy(dev->phy);
303 	dev->phy = NULL;
304 
305 	/* remove the phys and ports, everything else should be gone */
306 	if (dev_is_expander(dev->dev_type))
307 		kfree(dev->ex_dev.ex_phy);
308 
309 	if (dev_is_sata(dev) && dev->sata_dev.ap) {
310 		ata_sas_tport_delete(dev->sata_dev.ap);
311 		ata_sas_port_destroy(dev->sata_dev.ap);
312 		ata_host_put(dev->sata_dev.ata_host);
313 		dev->sata_dev.ata_host = NULL;
314 		dev->sata_dev.ap = NULL;
315 	}
316 
317 	kfree(dev);
318 }
319 
320 static void sas_unregister_common_dev(struct asd_sas_port *port, struct domain_device *dev)
321 {
322 	struct sas_ha_struct *ha = port->ha;
323 
324 	sas_notify_lldd_dev_gone(dev);
325 	if (!dev->parent)
326 		dev->port->port_dev = NULL;
327 	else
328 		list_del_init(&dev->siblings);
329 
330 	spin_lock_irq(&port->dev_list_lock);
331 	list_del_init(&dev->dev_list_node);
332 	if (dev_is_sata(dev))
333 		sas_ata_end_eh(dev->sata_dev.ap);
334 	spin_unlock_irq(&port->dev_list_lock);
335 
336 	spin_lock_irq(&ha->lock);
337 	if (dev->dev_type == SAS_END_DEVICE &&
338 	    !list_empty(&dev->ssp_dev.eh_list_node)) {
339 		list_del_init(&dev->ssp_dev.eh_list_node);
340 		ha->eh_active--;
341 	}
342 	spin_unlock_irq(&ha->lock);
343 
344 	sas_put_device(dev);
345 }
346 
347 void sas_destruct_devices(struct asd_sas_port *port)
348 {
349 	struct domain_device *dev, *n;
350 
351 	list_for_each_entry_safe(dev, n, &port->destroy_list, disco_list_node) {
352 		list_del_init(&dev->disco_list_node);
353 
354 		sas_remove_children(&dev->rphy->dev);
355 		sas_rphy_delete(dev->rphy);
356 		sas_unregister_common_dev(port, dev);
357 	}
358 }
359 
360 static void sas_destruct_ports(struct asd_sas_port *port)
361 {
362 	struct sas_port *sas_port, *p;
363 
364 	list_for_each_entry_safe(sas_port, p, &port->sas_port_del_list, del_list) {
365 		list_del_init(&sas_port->del_list);
366 		sas_port_delete(sas_port);
367 	}
368 }
369 
370 void sas_unregister_dev(struct asd_sas_port *port, struct domain_device *dev)
371 {
372 	if (!test_bit(SAS_DEV_DESTROY, &dev->state) &&
373 	    !list_empty(&dev->disco_list_node)) {
374 		/* this rphy never saw sas_rphy_add */
375 		list_del_init(&dev->disco_list_node);
376 		sas_rphy_free(dev->rphy);
377 		sas_unregister_common_dev(port, dev);
378 		return;
379 	}
380 
381 	if (!test_and_set_bit(SAS_DEV_DESTROY, &dev->state)) {
382 		sas_rphy_unlink(dev->rphy);
383 		list_move_tail(&dev->disco_list_node, &port->destroy_list);
384 	}
385 }
386 
387 void sas_unregister_domain_devices(struct asd_sas_port *port, int gone)
388 {
389 	struct domain_device *dev, *n;
390 
391 	list_for_each_entry_safe_reverse(dev, n, &port->dev_list, dev_list_node) {
392 		if (gone)
393 			set_bit(SAS_DEV_GONE, &dev->state);
394 		sas_unregister_dev(port, dev);
395 	}
396 
397 	list_for_each_entry_safe(dev, n, &port->disco_list, disco_list_node)
398 		sas_unregister_dev(port, dev);
399 
400 	port->port->rphy = NULL;
401 
402 }
403 
404 void sas_device_set_phy(struct domain_device *dev, struct sas_port *port)
405 {
406 	struct sas_ha_struct *ha;
407 	struct sas_phy *new_phy;
408 
409 	if (!dev)
410 		return;
411 
412 	ha = dev->port->ha;
413 	new_phy = sas_port_get_phy(port);
414 
415 	/* pin and record last seen phy */
416 	spin_lock_irq(&ha->phy_port_lock);
417 	if (new_phy) {
418 		sas_port_put_phy(dev->phy);
419 		dev->phy = new_phy;
420 	}
421 	spin_unlock_irq(&ha->phy_port_lock);
422 }
423 
424 /* ---------- Discovery and Revalidation ---------- */
425 
426 /**
427  * sas_discover_domain - discover the domain
428  * @work: work structure embedded in port domain device.
429  *
430  * NOTE: this process _must_ quit (return) as soon as any connection
431  * errors are encountered.  Connection recovery is done elsewhere.
432  * Discover process only interrogates devices in order to discover the
433  * domain.
434  */
435 static void sas_discover_domain(struct work_struct *work)
436 {
437 	struct domain_device *dev;
438 	int error = 0;
439 	struct sas_discovery_event *ev = to_sas_discovery_event(work);
440 	struct asd_sas_port *port = ev->port;
441 
442 	clear_bit(DISCE_DISCOVER_DOMAIN, &port->disc.pending);
443 
444 	if (port->port_dev)
445 		return;
446 
447 	error = sas_get_port_device(port);
448 	if (error)
449 		return;
450 	dev = port->port_dev;
451 
452 	pr_debug("DOING DISCOVERY on port %d, pid:%d\n", port->id,
453 		 task_pid_nr(current));
454 
455 	switch (dev->dev_type) {
456 	case SAS_END_DEVICE:
457 		error = sas_discover_end_dev(dev);
458 		break;
459 	case SAS_EDGE_EXPANDER_DEVICE:
460 	case SAS_FANOUT_EXPANDER_DEVICE:
461 		error = sas_discover_root_expander(dev);
462 		break;
463 	case SAS_SATA_DEV:
464 	case SAS_SATA_PM:
465 #ifdef CONFIG_SCSI_SAS_ATA
466 		error = sas_discover_sata(dev);
467 		break;
468 #else
469 		pr_notice("ATA device seen but CONFIG_SCSI_SAS_ATA=N so cannot attach\n");
470 		/* Fall through */
471 #endif
472 		/* Fall through - only for the #else condition above. */
473 	default:
474 		error = -ENXIO;
475 		pr_err("unhandled device %d\n", dev->dev_type);
476 		break;
477 	}
478 
479 	if (error) {
480 		sas_rphy_free(dev->rphy);
481 		list_del_init(&dev->disco_list_node);
482 		spin_lock_irq(&port->dev_list_lock);
483 		list_del_init(&dev->dev_list_node);
484 		spin_unlock_irq(&port->dev_list_lock);
485 
486 		sas_put_device(dev);
487 		port->port_dev = NULL;
488 	}
489 
490 	sas_probe_devices(port);
491 
492 	pr_debug("DONE DISCOVERY on port %d, pid:%d, result:%d\n", port->id,
493 		 task_pid_nr(current), error);
494 }
495 
496 static void sas_revalidate_domain(struct work_struct *work)
497 {
498 	int res = 0;
499 	struct sas_discovery_event *ev = to_sas_discovery_event(work);
500 	struct asd_sas_port *port = ev->port;
501 	struct sas_ha_struct *ha = port->ha;
502 	struct domain_device *ddev = port->port_dev;
503 
504 	/* prevent revalidation from finding sata links in recovery */
505 	mutex_lock(&ha->disco_mutex);
506 	if (test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state)) {
507 		pr_debug("REVALIDATION DEFERRED on port %d, pid:%d\n",
508 			 port->id, task_pid_nr(current));
509 		goto out;
510 	}
511 
512 	clear_bit(DISCE_REVALIDATE_DOMAIN, &port->disc.pending);
513 
514 	pr_debug("REVALIDATING DOMAIN on port %d, pid:%d\n", port->id,
515 		 task_pid_nr(current));
516 
517 	if (ddev && dev_is_expander(ddev->dev_type))
518 		res = sas_ex_revalidate_domain(ddev);
519 
520 	pr_debug("done REVALIDATING DOMAIN on port %d, pid:%d, res 0x%x\n",
521 		 port->id, task_pid_nr(current), res);
522  out:
523 	mutex_unlock(&ha->disco_mutex);
524 
525 	sas_destruct_devices(port);
526 	sas_destruct_ports(port);
527 	sas_probe_devices(port);
528 }
529 
530 /* ---------- Events ---------- */
531 
532 static void sas_chain_work(struct sas_ha_struct *ha, struct sas_work *sw)
533 {
534 	/* chained work is not subject to SA_HA_DRAINING or
535 	 * SAS_HA_REGISTERED, because it is either submitted in the
536 	 * workqueue, or known to be submitted from a context that is
537 	 * not racing against draining
538 	 */
539 	queue_work(ha->disco_q, &sw->work);
540 }
541 
542 static void sas_chain_event(int event, unsigned long *pending,
543 			    struct sas_work *sw,
544 			    struct sas_ha_struct *ha)
545 {
546 	if (!test_and_set_bit(event, pending)) {
547 		unsigned long flags;
548 
549 		spin_lock_irqsave(&ha->lock, flags);
550 		sas_chain_work(ha, sw);
551 		spin_unlock_irqrestore(&ha->lock, flags);
552 	}
553 }
554 
555 int sas_discover_event(struct asd_sas_port *port, enum discover_event ev)
556 {
557 	struct sas_discovery *disc;
558 
559 	if (!port)
560 		return 0;
561 	disc = &port->disc;
562 
563 	BUG_ON(ev >= DISC_NUM_EVENTS);
564 
565 	sas_chain_event(ev, &disc->pending, &disc->disc_work[ev].work, port->ha);
566 
567 	return 0;
568 }
569 
570 /**
571  * sas_init_disc - initialize the discovery struct in the port
572  * @disc: port discovery structure
573  * @port: pointer to struct port
574  *
575  * Called when the ports are being initialized.
576  */
577 void sas_init_disc(struct sas_discovery *disc, struct asd_sas_port *port)
578 {
579 	int i;
580 
581 	static const work_func_t sas_event_fns[DISC_NUM_EVENTS] = {
582 		[DISCE_DISCOVER_DOMAIN] = sas_discover_domain,
583 		[DISCE_REVALIDATE_DOMAIN] = sas_revalidate_domain,
584 		[DISCE_SUSPEND] = sas_suspend_devices,
585 		[DISCE_RESUME] = sas_resume_devices,
586 	};
587 
588 	disc->pending = 0;
589 	for (i = 0; i < DISC_NUM_EVENTS; i++) {
590 		INIT_SAS_WORK(&disc->disc_work[i].work, sas_event_fns[i]);
591 		disc->disc_work[i].port = port;
592 	}
593 }
594