1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Serial Attached SCSI (SAS) Expander discovery and configuration
4  *
5  * Copyright (C) 2005 Adaptec, Inc.  All rights reserved.
6  * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
7  *
8  * This file is licensed under GPLv2.
9  */
10 
11 #include <linux/scatterlist.h>
12 #include <linux/blkdev.h>
13 #include <linux/slab.h>
14 #include <asm/unaligned.h>
15 
16 #include "sas_internal.h"
17 
18 #include <scsi/sas_ata.h>
19 #include <scsi/scsi_transport.h>
20 #include <scsi/scsi_transport_sas.h>
21 #include "../scsi_sas_internal.h"
22 
23 static int sas_discover_expander(struct domain_device *dev);
24 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
25 static int sas_configure_phy(struct domain_device *dev, int phy_id,
26 			     u8 *sas_addr, int include);
27 static int sas_disable_routing(struct domain_device *dev,  u8 *sas_addr);
28 
29 /* ---------- SMP task management ---------- */
30 
31 static void smp_task_timedout(struct timer_list *t)
32 {
33 	struct sas_task_slow *slow = from_timer(slow, t, timer);
34 	struct sas_task *task = slow->task;
35 	unsigned long flags;
36 
37 	spin_lock_irqsave(&task->task_state_lock, flags);
38 	if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
39 		task->task_state_flags |= SAS_TASK_STATE_ABORTED;
40 		complete(&task->slow_task->completion);
41 	}
42 	spin_unlock_irqrestore(&task->task_state_lock, flags);
43 }
44 
45 static void smp_task_done(struct sas_task *task)
46 {
47 	del_timer(&task->slow_task->timer);
48 	complete(&task->slow_task->completion);
49 }
50 
51 /* Give it some long enough timeout. In seconds. */
52 #define SMP_TIMEOUT 10
53 
54 static int smp_execute_task_sg(struct domain_device *dev,
55 		struct scatterlist *req, struct scatterlist *resp)
56 {
57 	int res, retry;
58 	struct sas_task *task = NULL;
59 	struct sas_internal *i =
60 		to_sas_internal(dev->port->ha->core.shost->transportt);
61 
62 	mutex_lock(&dev->ex_dev.cmd_mutex);
63 	for (retry = 0; retry < 3; retry++) {
64 		if (test_bit(SAS_DEV_GONE, &dev->state)) {
65 			res = -ECOMM;
66 			break;
67 		}
68 
69 		task = sas_alloc_slow_task(GFP_KERNEL);
70 		if (!task) {
71 			res = -ENOMEM;
72 			break;
73 		}
74 		task->dev = dev;
75 		task->task_proto = dev->tproto;
76 		task->smp_task.smp_req = *req;
77 		task->smp_task.smp_resp = *resp;
78 
79 		task->task_done = smp_task_done;
80 
81 		task->slow_task->timer.function = smp_task_timedout;
82 		task->slow_task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
83 		add_timer(&task->slow_task->timer);
84 
85 		res = i->dft->lldd_execute_task(task, GFP_KERNEL);
86 
87 		if (res) {
88 			del_timer(&task->slow_task->timer);
89 			pr_notice("executing SMP task failed:%d\n", res);
90 			break;
91 		}
92 
93 		wait_for_completion(&task->slow_task->completion);
94 		res = -ECOMM;
95 		if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
96 			pr_notice("smp task timed out or aborted\n");
97 			i->dft->lldd_abort_task(task);
98 			if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
99 				pr_notice("SMP task aborted and not done\n");
100 				break;
101 			}
102 		}
103 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
104 		    task->task_status.stat == SAM_STAT_GOOD) {
105 			res = 0;
106 			break;
107 		}
108 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
109 		    task->task_status.stat == SAS_DATA_UNDERRUN) {
110 			/* no error, but return the number of bytes of
111 			 * underrun */
112 			res = task->task_status.residual;
113 			break;
114 		}
115 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
116 		    task->task_status.stat == SAS_DATA_OVERRUN) {
117 			res = -EMSGSIZE;
118 			break;
119 		}
120 		if (task->task_status.resp == SAS_TASK_UNDELIVERED &&
121 		    task->task_status.stat == SAS_DEVICE_UNKNOWN)
122 			break;
123 		else {
124 			pr_notice("%s: task to dev %016llx response: 0x%x status 0x%x\n",
125 				  __func__,
126 				  SAS_ADDR(dev->sas_addr),
127 				  task->task_status.resp,
128 				  task->task_status.stat);
129 			sas_free_task(task);
130 			task = NULL;
131 		}
132 	}
133 	mutex_unlock(&dev->ex_dev.cmd_mutex);
134 
135 	BUG_ON(retry == 3 && task != NULL);
136 	sas_free_task(task);
137 	return res;
138 }
139 
140 static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
141 			    void *resp, int resp_size)
142 {
143 	struct scatterlist req_sg;
144 	struct scatterlist resp_sg;
145 
146 	sg_init_one(&req_sg, req, req_size);
147 	sg_init_one(&resp_sg, resp, resp_size);
148 	return smp_execute_task_sg(dev, &req_sg, &resp_sg);
149 }
150 
151 /* ---------- Allocations ---------- */
152 
153 static inline void *alloc_smp_req(int size)
154 {
155 	u8 *p = kzalloc(size, GFP_KERNEL);
156 	if (p)
157 		p[0] = SMP_REQUEST;
158 	return p;
159 }
160 
161 static inline void *alloc_smp_resp(int size)
162 {
163 	return kzalloc(size, GFP_KERNEL);
164 }
165 
166 static char sas_route_char(struct domain_device *dev, struct ex_phy *phy)
167 {
168 	switch (phy->routing_attr) {
169 	case TABLE_ROUTING:
170 		if (dev->ex_dev.t2t_supp)
171 			return 'U';
172 		else
173 			return 'T';
174 	case DIRECT_ROUTING:
175 		return 'D';
176 	case SUBTRACTIVE_ROUTING:
177 		return 'S';
178 	default:
179 		return '?';
180 	}
181 }
182 
183 static enum sas_device_type to_dev_type(struct discover_resp *dr)
184 {
185 	/* This is detecting a failure to transmit initial dev to host
186 	 * FIS as described in section J.5 of sas-2 r16
187 	 */
188 	if (dr->attached_dev_type == SAS_PHY_UNUSED && dr->attached_sata_dev &&
189 	    dr->linkrate >= SAS_LINK_RATE_1_5_GBPS)
190 		return SAS_SATA_PENDING;
191 	else
192 		return dr->attached_dev_type;
193 }
194 
195 static void sas_set_ex_phy(struct domain_device *dev, int phy_id, void *rsp)
196 {
197 	enum sas_device_type dev_type;
198 	enum sas_linkrate linkrate;
199 	u8 sas_addr[SAS_ADDR_SIZE];
200 	struct smp_resp *resp = rsp;
201 	struct discover_resp *dr = &resp->disc;
202 	struct sas_ha_struct *ha = dev->port->ha;
203 	struct expander_device *ex = &dev->ex_dev;
204 	struct ex_phy *phy = &ex->ex_phy[phy_id];
205 	struct sas_rphy *rphy = dev->rphy;
206 	bool new_phy = !phy->phy;
207 	char *type;
208 
209 	if (new_phy) {
210 		if (WARN_ON_ONCE(test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state)))
211 			return;
212 		phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
213 
214 		/* FIXME: error_handling */
215 		BUG_ON(!phy->phy);
216 	}
217 
218 	switch (resp->result) {
219 	case SMP_RESP_PHY_VACANT:
220 		phy->phy_state = PHY_VACANT;
221 		break;
222 	default:
223 		phy->phy_state = PHY_NOT_PRESENT;
224 		break;
225 	case SMP_RESP_FUNC_ACC:
226 		phy->phy_state = PHY_EMPTY; /* do not know yet */
227 		break;
228 	}
229 
230 	/* check if anything important changed to squelch debug */
231 	dev_type = phy->attached_dev_type;
232 	linkrate  = phy->linkrate;
233 	memcpy(sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
234 
235 	/* Handle vacant phy - rest of dr data is not valid so skip it */
236 	if (phy->phy_state == PHY_VACANT) {
237 		memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
238 		phy->attached_dev_type = SAS_PHY_UNUSED;
239 		if (!test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state)) {
240 			phy->phy_id = phy_id;
241 			goto skip;
242 		} else
243 			goto out;
244 	}
245 
246 	phy->attached_dev_type = to_dev_type(dr);
247 	if (test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state))
248 		goto out;
249 	phy->phy_id = phy_id;
250 	phy->linkrate = dr->linkrate;
251 	phy->attached_sata_host = dr->attached_sata_host;
252 	phy->attached_sata_dev  = dr->attached_sata_dev;
253 	phy->attached_sata_ps   = dr->attached_sata_ps;
254 	phy->attached_iproto = dr->iproto << 1;
255 	phy->attached_tproto = dr->tproto << 1;
256 	/* help some expanders that fail to zero sas_address in the 'no
257 	 * device' case
258 	 */
259 	if (phy->attached_dev_type == SAS_PHY_UNUSED ||
260 	    phy->linkrate < SAS_LINK_RATE_1_5_GBPS)
261 		memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
262 	else
263 		memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
264 	phy->attached_phy_id = dr->attached_phy_id;
265 	phy->phy_change_count = dr->change_count;
266 	phy->routing_attr = dr->routing_attr;
267 	phy->virtual = dr->virtual;
268 	phy->last_da_index = -1;
269 
270 	phy->phy->identify.sas_address = SAS_ADDR(phy->attached_sas_addr);
271 	phy->phy->identify.device_type = dr->attached_dev_type;
272 	phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
273 	phy->phy->identify.target_port_protocols = phy->attached_tproto;
274 	if (!phy->attached_tproto && dr->attached_sata_dev)
275 		phy->phy->identify.target_port_protocols = SAS_PROTOCOL_SATA;
276 	phy->phy->identify.phy_identifier = phy_id;
277 	phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
278 	phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
279 	phy->phy->minimum_linkrate = dr->pmin_linkrate;
280 	phy->phy->maximum_linkrate = dr->pmax_linkrate;
281 	phy->phy->negotiated_linkrate = phy->linkrate;
282 	phy->phy->enabled = (phy->linkrate != SAS_PHY_DISABLED);
283 
284  skip:
285 	if (new_phy)
286 		if (sas_phy_add(phy->phy)) {
287 			sas_phy_free(phy->phy);
288 			return;
289 		}
290 
291  out:
292 	switch (phy->attached_dev_type) {
293 	case SAS_SATA_PENDING:
294 		type = "stp pending";
295 		break;
296 	case SAS_PHY_UNUSED:
297 		type = "no device";
298 		break;
299 	case SAS_END_DEVICE:
300 		if (phy->attached_iproto) {
301 			if (phy->attached_tproto)
302 				type = "host+target";
303 			else
304 				type = "host";
305 		} else {
306 			if (dr->attached_sata_dev)
307 				type = "stp";
308 			else
309 				type = "ssp";
310 		}
311 		break;
312 	case SAS_EDGE_EXPANDER_DEVICE:
313 	case SAS_FANOUT_EXPANDER_DEVICE:
314 		type = "smp";
315 		break;
316 	default:
317 		type = "unknown";
318 	}
319 
320 	/* this routine is polled by libata error recovery so filter
321 	 * unimportant messages
322 	 */
323 	if (new_phy || phy->attached_dev_type != dev_type ||
324 	    phy->linkrate != linkrate ||
325 	    SAS_ADDR(phy->attached_sas_addr) != SAS_ADDR(sas_addr))
326 		/* pass */;
327 	else
328 		return;
329 
330 	/* if the attached device type changed and ata_eh is active,
331 	 * make sure we run revalidation when eh completes (see:
332 	 * sas_enable_revalidation)
333 	 */
334 	if (test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state))
335 		set_bit(DISCE_REVALIDATE_DOMAIN, &dev->port->disc.pending);
336 
337 	pr_debug("%sex %016llx phy%02d:%c:%X attached: %016llx (%s)\n",
338 		 test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state) ? "ata: " : "",
339 		 SAS_ADDR(dev->sas_addr), phy->phy_id,
340 		 sas_route_char(dev, phy), phy->linkrate,
341 		 SAS_ADDR(phy->attached_sas_addr), type);
342 }
343 
344 /* check if we have an existing attached ata device on this expander phy */
345 struct domain_device *sas_ex_to_ata(struct domain_device *ex_dev, int phy_id)
346 {
347 	struct ex_phy *ex_phy = &ex_dev->ex_dev.ex_phy[phy_id];
348 	struct domain_device *dev;
349 	struct sas_rphy *rphy;
350 
351 	if (!ex_phy->port)
352 		return NULL;
353 
354 	rphy = ex_phy->port->rphy;
355 	if (!rphy)
356 		return NULL;
357 
358 	dev = sas_find_dev_by_rphy(rphy);
359 
360 	if (dev && dev_is_sata(dev))
361 		return dev;
362 
363 	return NULL;
364 }
365 
366 #define DISCOVER_REQ_SIZE  16
367 #define DISCOVER_RESP_SIZE 56
368 
369 static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
370 				      u8 *disc_resp, int single)
371 {
372 	struct discover_resp *dr;
373 	int res;
374 
375 	disc_req[9] = single;
376 
377 	res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
378 			       disc_resp, DISCOVER_RESP_SIZE);
379 	if (res)
380 		return res;
381 	dr = &((struct smp_resp *)disc_resp)->disc;
382 	if (memcmp(dev->sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE) == 0) {
383 		pr_notice("Found loopback topology, just ignore it!\n");
384 		return 0;
385 	}
386 	sas_set_ex_phy(dev, single, disc_resp);
387 	return 0;
388 }
389 
390 int sas_ex_phy_discover(struct domain_device *dev, int single)
391 {
392 	struct expander_device *ex = &dev->ex_dev;
393 	int  res = 0;
394 	u8   *disc_req;
395 	u8   *disc_resp;
396 
397 	disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
398 	if (!disc_req)
399 		return -ENOMEM;
400 
401 	disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
402 	if (!disc_resp) {
403 		kfree(disc_req);
404 		return -ENOMEM;
405 	}
406 
407 	disc_req[1] = SMP_DISCOVER;
408 
409 	if (0 <= single && single < ex->num_phys) {
410 		res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
411 	} else {
412 		int i;
413 
414 		for (i = 0; i < ex->num_phys; i++) {
415 			res = sas_ex_phy_discover_helper(dev, disc_req,
416 							 disc_resp, i);
417 			if (res)
418 				goto out_err;
419 		}
420 	}
421 out_err:
422 	kfree(disc_resp);
423 	kfree(disc_req);
424 	return res;
425 }
426 
427 static int sas_expander_discover(struct domain_device *dev)
428 {
429 	struct expander_device *ex = &dev->ex_dev;
430 	int res = -ENOMEM;
431 
432 	ex->ex_phy = kcalloc(ex->num_phys, sizeof(*ex->ex_phy), GFP_KERNEL);
433 	if (!ex->ex_phy)
434 		return -ENOMEM;
435 
436 	res = sas_ex_phy_discover(dev, -1);
437 	if (res)
438 		goto out_err;
439 
440 	return 0;
441  out_err:
442 	kfree(ex->ex_phy);
443 	ex->ex_phy = NULL;
444 	return res;
445 }
446 
447 #define MAX_EXPANDER_PHYS 128
448 
449 static void ex_assign_report_general(struct domain_device *dev,
450 					    struct smp_resp *resp)
451 {
452 	struct report_general_resp *rg = &resp->rg;
453 
454 	dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
455 	dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
456 	dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
457 	dev->ex_dev.t2t_supp = rg->t2t_supp;
458 	dev->ex_dev.conf_route_table = rg->conf_route_table;
459 	dev->ex_dev.configuring = rg->configuring;
460 	memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
461 }
462 
463 #define RG_REQ_SIZE   8
464 #define RG_RESP_SIZE 32
465 
466 static int sas_ex_general(struct domain_device *dev)
467 {
468 	u8 *rg_req;
469 	struct smp_resp *rg_resp;
470 	int res;
471 	int i;
472 
473 	rg_req = alloc_smp_req(RG_REQ_SIZE);
474 	if (!rg_req)
475 		return -ENOMEM;
476 
477 	rg_resp = alloc_smp_resp(RG_RESP_SIZE);
478 	if (!rg_resp) {
479 		kfree(rg_req);
480 		return -ENOMEM;
481 	}
482 
483 	rg_req[1] = SMP_REPORT_GENERAL;
484 
485 	for (i = 0; i < 5; i++) {
486 		res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
487 				       RG_RESP_SIZE);
488 
489 		if (res) {
490 			pr_notice("RG to ex %016llx failed:0x%x\n",
491 				  SAS_ADDR(dev->sas_addr), res);
492 			goto out;
493 		} else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
494 			pr_debug("RG:ex %016llx returned SMP result:0x%x\n",
495 				 SAS_ADDR(dev->sas_addr), rg_resp->result);
496 			res = rg_resp->result;
497 			goto out;
498 		}
499 
500 		ex_assign_report_general(dev, rg_resp);
501 
502 		if (dev->ex_dev.configuring) {
503 			pr_debug("RG: ex %016llx self-configuring...\n",
504 				 SAS_ADDR(dev->sas_addr));
505 			schedule_timeout_interruptible(5*HZ);
506 		} else
507 			break;
508 	}
509 out:
510 	kfree(rg_req);
511 	kfree(rg_resp);
512 	return res;
513 }
514 
515 static void ex_assign_manuf_info(struct domain_device *dev, void
516 					*_mi_resp)
517 {
518 	u8 *mi_resp = _mi_resp;
519 	struct sas_rphy *rphy = dev->rphy;
520 	struct sas_expander_device *edev = rphy_to_expander_device(rphy);
521 
522 	memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
523 	memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
524 	memcpy(edev->product_rev, mi_resp + 36,
525 	       SAS_EXPANDER_PRODUCT_REV_LEN);
526 
527 	if (mi_resp[8] & 1) {
528 		memcpy(edev->component_vendor_id, mi_resp + 40,
529 		       SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
530 		edev->component_id = mi_resp[48] << 8 | mi_resp[49];
531 		edev->component_revision_id = mi_resp[50];
532 	}
533 }
534 
535 #define MI_REQ_SIZE   8
536 #define MI_RESP_SIZE 64
537 
538 static int sas_ex_manuf_info(struct domain_device *dev)
539 {
540 	u8 *mi_req;
541 	u8 *mi_resp;
542 	int res;
543 
544 	mi_req = alloc_smp_req(MI_REQ_SIZE);
545 	if (!mi_req)
546 		return -ENOMEM;
547 
548 	mi_resp = alloc_smp_resp(MI_RESP_SIZE);
549 	if (!mi_resp) {
550 		kfree(mi_req);
551 		return -ENOMEM;
552 	}
553 
554 	mi_req[1] = SMP_REPORT_MANUF_INFO;
555 
556 	res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
557 	if (res) {
558 		pr_notice("MI: ex %016llx failed:0x%x\n",
559 			  SAS_ADDR(dev->sas_addr), res);
560 		goto out;
561 	} else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
562 		pr_debug("MI ex %016llx returned SMP result:0x%x\n",
563 			 SAS_ADDR(dev->sas_addr), mi_resp[2]);
564 		goto out;
565 	}
566 
567 	ex_assign_manuf_info(dev, mi_resp);
568 out:
569 	kfree(mi_req);
570 	kfree(mi_resp);
571 	return res;
572 }
573 
574 #define PC_REQ_SIZE  44
575 #define PC_RESP_SIZE 8
576 
577 int sas_smp_phy_control(struct domain_device *dev, int phy_id,
578 			enum phy_func phy_func,
579 			struct sas_phy_linkrates *rates)
580 {
581 	u8 *pc_req;
582 	u8 *pc_resp;
583 	int res;
584 
585 	pc_req = alloc_smp_req(PC_REQ_SIZE);
586 	if (!pc_req)
587 		return -ENOMEM;
588 
589 	pc_resp = alloc_smp_resp(PC_RESP_SIZE);
590 	if (!pc_resp) {
591 		kfree(pc_req);
592 		return -ENOMEM;
593 	}
594 
595 	pc_req[1] = SMP_PHY_CONTROL;
596 	pc_req[9] = phy_id;
597 	pc_req[10]= phy_func;
598 	if (rates) {
599 		pc_req[32] = rates->minimum_linkrate << 4;
600 		pc_req[33] = rates->maximum_linkrate << 4;
601 	}
602 
603 	res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);
604 	if (res) {
605 		pr_err("ex %016llx phy%02d PHY control failed: %d\n",
606 		       SAS_ADDR(dev->sas_addr), phy_id, res);
607 	} else if (pc_resp[2] != SMP_RESP_FUNC_ACC) {
608 		pr_err("ex %016llx phy%02d PHY control failed: function result 0x%x\n",
609 		       SAS_ADDR(dev->sas_addr), phy_id, pc_resp[2]);
610 		res = pc_resp[2];
611 	}
612 	kfree(pc_resp);
613 	kfree(pc_req);
614 	return res;
615 }
616 
617 static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
618 {
619 	struct expander_device *ex = &dev->ex_dev;
620 	struct ex_phy *phy = &ex->ex_phy[phy_id];
621 
622 	sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
623 	phy->linkrate = SAS_PHY_DISABLED;
624 }
625 
626 static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
627 {
628 	struct expander_device *ex = &dev->ex_dev;
629 	int i;
630 
631 	for (i = 0; i < ex->num_phys; i++) {
632 		struct ex_phy *phy = &ex->ex_phy[i];
633 
634 		if (phy->phy_state == PHY_VACANT ||
635 		    phy->phy_state == PHY_NOT_PRESENT)
636 			continue;
637 
638 		if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
639 			sas_ex_disable_phy(dev, i);
640 	}
641 }
642 
643 static int sas_dev_present_in_domain(struct asd_sas_port *port,
644 					    u8 *sas_addr)
645 {
646 	struct domain_device *dev;
647 
648 	if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
649 		return 1;
650 	list_for_each_entry(dev, &port->dev_list, dev_list_node) {
651 		if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
652 			return 1;
653 	}
654 	return 0;
655 }
656 
657 #define RPEL_REQ_SIZE	16
658 #define RPEL_RESP_SIZE	32
659 int sas_smp_get_phy_events(struct sas_phy *phy)
660 {
661 	int res;
662 	u8 *req;
663 	u8 *resp;
664 	struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
665 	struct domain_device *dev = sas_find_dev_by_rphy(rphy);
666 
667 	req = alloc_smp_req(RPEL_REQ_SIZE);
668 	if (!req)
669 		return -ENOMEM;
670 
671 	resp = alloc_smp_resp(RPEL_RESP_SIZE);
672 	if (!resp) {
673 		kfree(req);
674 		return -ENOMEM;
675 	}
676 
677 	req[1] = SMP_REPORT_PHY_ERR_LOG;
678 	req[9] = phy->number;
679 
680 	res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
681 			            resp, RPEL_RESP_SIZE);
682 
683 	if (res)
684 		goto out;
685 
686 	phy->invalid_dword_count = get_unaligned_be32(&resp[12]);
687 	phy->running_disparity_error_count = get_unaligned_be32(&resp[16]);
688 	phy->loss_of_dword_sync_count = get_unaligned_be32(&resp[20]);
689 	phy->phy_reset_problem_count = get_unaligned_be32(&resp[24]);
690 
691  out:
692 	kfree(req);
693 	kfree(resp);
694 	return res;
695 
696 }
697 
698 #ifdef CONFIG_SCSI_SAS_ATA
699 
700 #define RPS_REQ_SIZE  16
701 #define RPS_RESP_SIZE 60
702 
703 int sas_get_report_phy_sata(struct domain_device *dev, int phy_id,
704 			    struct smp_resp *rps_resp)
705 {
706 	int res;
707 	u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
708 	u8 *resp = (u8 *)rps_resp;
709 
710 	if (!rps_req)
711 		return -ENOMEM;
712 
713 	rps_req[1] = SMP_REPORT_PHY_SATA;
714 	rps_req[9] = phy_id;
715 
716 	res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
717 			            rps_resp, RPS_RESP_SIZE);
718 
719 	/* 0x34 is the FIS type for the D2H fis.  There's a potential
720 	 * standards cockup here.  sas-2 explicitly specifies the FIS
721 	 * should be encoded so that FIS type is in resp[24].
722 	 * However, some expanders endian reverse this.  Undo the
723 	 * reversal here */
724 	if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
725 		int i;
726 
727 		for (i = 0; i < 5; i++) {
728 			int j = 24 + (i*4);
729 			u8 a, b;
730 			a = resp[j + 0];
731 			b = resp[j + 1];
732 			resp[j + 0] = resp[j + 3];
733 			resp[j + 1] = resp[j + 2];
734 			resp[j + 2] = b;
735 			resp[j + 3] = a;
736 		}
737 	}
738 
739 	kfree(rps_req);
740 	return res;
741 }
742 #endif
743 
744 static void sas_ex_get_linkrate(struct domain_device *parent,
745 				       struct domain_device *child,
746 				       struct ex_phy *parent_phy)
747 {
748 	struct expander_device *parent_ex = &parent->ex_dev;
749 	struct sas_port *port;
750 	int i;
751 
752 	child->pathways = 0;
753 
754 	port = parent_phy->port;
755 
756 	for (i = 0; i < parent_ex->num_phys; i++) {
757 		struct ex_phy *phy = &parent_ex->ex_phy[i];
758 
759 		if (phy->phy_state == PHY_VACANT ||
760 		    phy->phy_state == PHY_NOT_PRESENT)
761 			continue;
762 
763 		if (SAS_ADDR(phy->attached_sas_addr) ==
764 		    SAS_ADDR(child->sas_addr)) {
765 
766 			child->min_linkrate = min(parent->min_linkrate,
767 						  phy->linkrate);
768 			child->max_linkrate = max(parent->max_linkrate,
769 						  phy->linkrate);
770 			child->pathways++;
771 			sas_port_add_phy(port, phy->phy);
772 		}
773 	}
774 	child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
775 	child->pathways = min(child->pathways, parent->pathways);
776 }
777 
778 static struct domain_device *sas_ex_discover_end_dev(
779 	struct domain_device *parent, int phy_id)
780 {
781 	struct expander_device *parent_ex = &parent->ex_dev;
782 	struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
783 	struct domain_device *child = NULL;
784 	struct sas_rphy *rphy;
785 	int res;
786 
787 	if (phy->attached_sata_host || phy->attached_sata_ps)
788 		return NULL;
789 
790 	child = sas_alloc_device();
791 	if (!child)
792 		return NULL;
793 
794 	kref_get(&parent->kref);
795 	child->parent = parent;
796 	child->port   = parent->port;
797 	child->iproto = phy->attached_iproto;
798 	memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
799 	sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
800 	if (!phy->port) {
801 		phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
802 		if (unlikely(!phy->port))
803 			goto out_err;
804 		if (unlikely(sas_port_add(phy->port) != 0)) {
805 			sas_port_free(phy->port);
806 			goto out_err;
807 		}
808 	}
809 	sas_ex_get_linkrate(parent, child, phy);
810 	sas_device_set_phy(child, phy->port);
811 
812 #ifdef CONFIG_SCSI_SAS_ATA
813 	if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
814 		if (child->linkrate > parent->min_linkrate) {
815 			struct sas_phy *cphy = child->phy;
816 			enum sas_linkrate min_prate = cphy->minimum_linkrate,
817 				parent_min_lrate = parent->min_linkrate,
818 				min_linkrate = (min_prate > parent_min_lrate) ?
819 					       parent_min_lrate : 0;
820 			struct sas_phy_linkrates rates = {
821 				.maximum_linkrate = parent->min_linkrate,
822 				.minimum_linkrate = min_linkrate,
823 			};
824 			int ret;
825 
826 			pr_notice("ex %016llx phy%02d SATA device linkrate > min pathway connection rate, attempting to lower device linkrate\n",
827 				   SAS_ADDR(child->sas_addr), phy_id);
828 			ret = sas_smp_phy_control(parent, phy_id,
829 						  PHY_FUNC_LINK_RESET, &rates);
830 			if (ret) {
831 				pr_err("ex %016llx phy%02d SATA device could not set linkrate (%d)\n",
832 				       SAS_ADDR(child->sas_addr), phy_id, ret);
833 				goto out_free;
834 			}
835 			pr_notice("ex %016llx phy%02d SATA device set linkrate successfully\n",
836 				  SAS_ADDR(child->sas_addr), phy_id);
837 			child->linkrate = child->min_linkrate;
838 		}
839 		res = sas_get_ata_info(child, phy);
840 		if (res)
841 			goto out_free;
842 
843 		sas_init_dev(child);
844 		res = sas_ata_init(child);
845 		if (res)
846 			goto out_free;
847 		rphy = sas_end_device_alloc(phy->port);
848 		if (!rphy)
849 			goto out_free;
850 		rphy->identify.phy_identifier = phy_id;
851 
852 		child->rphy = rphy;
853 		get_device(&rphy->dev);
854 
855 		list_add_tail(&child->disco_list_node, &parent->port->disco_list);
856 
857 		res = sas_discover_sata(child);
858 		if (res) {
859 			pr_notice("sas_discover_sata() for device %16llx at %016llx:%02d returned 0x%x\n",
860 				  SAS_ADDR(child->sas_addr),
861 				  SAS_ADDR(parent->sas_addr), phy_id, res);
862 			goto out_list_del;
863 		}
864 	} else
865 #endif
866 	  if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
867 		child->dev_type = SAS_END_DEVICE;
868 		rphy = sas_end_device_alloc(phy->port);
869 		/* FIXME: error handling */
870 		if (unlikely(!rphy))
871 			goto out_free;
872 		child->tproto = phy->attached_tproto;
873 		sas_init_dev(child);
874 
875 		child->rphy = rphy;
876 		get_device(&rphy->dev);
877 		rphy->identify.phy_identifier = phy_id;
878 		sas_fill_in_rphy(child, rphy);
879 
880 		list_add_tail(&child->disco_list_node, &parent->port->disco_list);
881 
882 		res = sas_discover_end_dev(child);
883 		if (res) {
884 			pr_notice("sas_discover_end_dev() for device %016llx at %016llx:%02d returned 0x%x\n",
885 				  SAS_ADDR(child->sas_addr),
886 				  SAS_ADDR(parent->sas_addr), phy_id, res);
887 			goto out_list_del;
888 		}
889 	} else {
890 		pr_notice("target proto 0x%x at %016llx:0x%x not handled\n",
891 			  phy->attached_tproto, SAS_ADDR(parent->sas_addr),
892 			  phy_id);
893 		goto out_free;
894 	}
895 
896 	list_add_tail(&child->siblings, &parent_ex->children);
897 	return child;
898 
899  out_list_del:
900 	sas_rphy_free(child->rphy);
901 	list_del(&child->disco_list_node);
902 	spin_lock_irq(&parent->port->dev_list_lock);
903 	list_del(&child->dev_list_node);
904 	spin_unlock_irq(&parent->port->dev_list_lock);
905  out_free:
906 	sas_port_delete(phy->port);
907  out_err:
908 	phy->port = NULL;
909 	sas_put_device(child);
910 	return NULL;
911 }
912 
913 /* See if this phy is part of a wide port */
914 static bool sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
915 {
916 	struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
917 	int i;
918 
919 	for (i = 0; i < parent->ex_dev.num_phys; i++) {
920 		struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];
921 
922 		if (ephy == phy)
923 			continue;
924 
925 		if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
926 			    SAS_ADDR_SIZE) && ephy->port) {
927 			sas_port_add_phy(ephy->port, phy->phy);
928 			phy->port = ephy->port;
929 			phy->phy_state = PHY_DEVICE_DISCOVERED;
930 			return true;
931 		}
932 	}
933 
934 	return false;
935 }
936 
937 static struct domain_device *sas_ex_discover_expander(
938 	struct domain_device *parent, int phy_id)
939 {
940 	struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
941 	struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
942 	struct domain_device *child = NULL;
943 	struct sas_rphy *rphy;
944 	struct sas_expander_device *edev;
945 	struct asd_sas_port *port;
946 	int res;
947 
948 	if (phy->routing_attr == DIRECT_ROUTING) {
949 		pr_warn("ex %016llx:%02d:D <--> ex %016llx:0x%x is not allowed\n",
950 			SAS_ADDR(parent->sas_addr), phy_id,
951 			SAS_ADDR(phy->attached_sas_addr),
952 			phy->attached_phy_id);
953 		return NULL;
954 	}
955 	child = sas_alloc_device();
956 	if (!child)
957 		return NULL;
958 
959 	phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
960 	/* FIXME: better error handling */
961 	BUG_ON(sas_port_add(phy->port) != 0);
962 
963 
964 	switch (phy->attached_dev_type) {
965 	case SAS_EDGE_EXPANDER_DEVICE:
966 		rphy = sas_expander_alloc(phy->port,
967 					  SAS_EDGE_EXPANDER_DEVICE);
968 		break;
969 	case SAS_FANOUT_EXPANDER_DEVICE:
970 		rphy = sas_expander_alloc(phy->port,
971 					  SAS_FANOUT_EXPANDER_DEVICE);
972 		break;
973 	default:
974 		rphy = NULL;	/* shut gcc up */
975 		BUG();
976 	}
977 	port = parent->port;
978 	child->rphy = rphy;
979 	get_device(&rphy->dev);
980 	edev = rphy_to_expander_device(rphy);
981 	child->dev_type = phy->attached_dev_type;
982 	kref_get(&parent->kref);
983 	child->parent = parent;
984 	child->port = port;
985 	child->iproto = phy->attached_iproto;
986 	child->tproto = phy->attached_tproto;
987 	memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
988 	sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
989 	sas_ex_get_linkrate(parent, child, phy);
990 	edev->level = parent_ex->level + 1;
991 	parent->port->disc.max_level = max(parent->port->disc.max_level,
992 					   edev->level);
993 	sas_init_dev(child);
994 	sas_fill_in_rphy(child, rphy);
995 	sas_rphy_add(rphy);
996 
997 	spin_lock_irq(&parent->port->dev_list_lock);
998 	list_add_tail(&child->dev_list_node, &parent->port->dev_list);
999 	spin_unlock_irq(&parent->port->dev_list_lock);
1000 
1001 	res = sas_discover_expander(child);
1002 	if (res) {
1003 		sas_rphy_delete(rphy);
1004 		spin_lock_irq(&parent->port->dev_list_lock);
1005 		list_del(&child->dev_list_node);
1006 		spin_unlock_irq(&parent->port->dev_list_lock);
1007 		sas_put_device(child);
1008 		sas_port_delete(phy->port);
1009 		phy->port = NULL;
1010 		return NULL;
1011 	}
1012 	list_add_tail(&child->siblings, &parent->ex_dev.children);
1013 	return child;
1014 }
1015 
1016 static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
1017 {
1018 	struct expander_device *ex = &dev->ex_dev;
1019 	struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
1020 	struct domain_device *child = NULL;
1021 	int res = 0;
1022 
1023 	/* Phy state */
1024 	if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
1025 		if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
1026 			res = sas_ex_phy_discover(dev, phy_id);
1027 		if (res)
1028 			return res;
1029 	}
1030 
1031 	/* Parent and domain coherency */
1032 	if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
1033 			     SAS_ADDR(dev->port->sas_addr))) {
1034 		sas_add_parent_port(dev, phy_id);
1035 		return 0;
1036 	}
1037 	if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
1038 			    SAS_ADDR(dev->parent->sas_addr))) {
1039 		sas_add_parent_port(dev, phy_id);
1040 		if (ex_phy->routing_attr == TABLE_ROUTING)
1041 			sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
1042 		return 0;
1043 	}
1044 
1045 	if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
1046 		sas_ex_disable_port(dev, ex_phy->attached_sas_addr);
1047 
1048 	if (ex_phy->attached_dev_type == SAS_PHY_UNUSED) {
1049 		if (ex_phy->routing_attr == DIRECT_ROUTING) {
1050 			memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1051 			sas_configure_routing(dev, ex_phy->attached_sas_addr);
1052 		}
1053 		return 0;
1054 	} else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
1055 		return 0;
1056 
1057 	if (ex_phy->attached_dev_type != SAS_END_DEVICE &&
1058 	    ex_phy->attached_dev_type != SAS_FANOUT_EXPANDER_DEVICE &&
1059 	    ex_phy->attached_dev_type != SAS_EDGE_EXPANDER_DEVICE &&
1060 	    ex_phy->attached_dev_type != SAS_SATA_PENDING) {
1061 		pr_warn("unknown device type(0x%x) attached to ex %016llx phy%02d\n",
1062 			ex_phy->attached_dev_type,
1063 			SAS_ADDR(dev->sas_addr),
1064 			phy_id);
1065 		return 0;
1066 	}
1067 
1068 	res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
1069 	if (res) {
1070 		pr_notice("configure routing for dev %016llx reported 0x%x. Forgotten\n",
1071 			  SAS_ADDR(ex_phy->attached_sas_addr), res);
1072 		sas_disable_routing(dev, ex_phy->attached_sas_addr);
1073 		return res;
1074 	}
1075 
1076 	if (sas_ex_join_wide_port(dev, phy_id)) {
1077 		pr_debug("Attaching ex phy%02d to wide port %016llx\n",
1078 			 phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
1079 		return res;
1080 	}
1081 
1082 	switch (ex_phy->attached_dev_type) {
1083 	case SAS_END_DEVICE:
1084 	case SAS_SATA_PENDING:
1085 		child = sas_ex_discover_end_dev(dev, phy_id);
1086 		break;
1087 	case SAS_FANOUT_EXPANDER_DEVICE:
1088 		if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
1089 			pr_debug("second fanout expander %016llx phy%02d attached to ex %016llx phy%02d\n",
1090 				 SAS_ADDR(ex_phy->attached_sas_addr),
1091 				 ex_phy->attached_phy_id,
1092 				 SAS_ADDR(dev->sas_addr),
1093 				 phy_id);
1094 			sas_ex_disable_phy(dev, phy_id);
1095 			return res;
1096 		} else
1097 			memcpy(dev->port->disc.fanout_sas_addr,
1098 			       ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
1099 		/* fallthrough */
1100 	case SAS_EDGE_EXPANDER_DEVICE:
1101 		child = sas_ex_discover_expander(dev, phy_id);
1102 		break;
1103 	default:
1104 		break;
1105 	}
1106 
1107 	if (!child)
1108 		pr_notice("ex %016llx phy%02d failed to discover\n",
1109 			  SAS_ADDR(dev->sas_addr), phy_id);
1110 	return res;
1111 }
1112 
1113 static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
1114 {
1115 	struct expander_device *ex = &dev->ex_dev;
1116 	int i;
1117 
1118 	for (i = 0; i < ex->num_phys; i++) {
1119 		struct ex_phy *phy = &ex->ex_phy[i];
1120 
1121 		if (phy->phy_state == PHY_VACANT ||
1122 		    phy->phy_state == PHY_NOT_PRESENT)
1123 			continue;
1124 
1125 		if (dev_is_expander(phy->attached_dev_type) &&
1126 		    phy->routing_attr == SUBTRACTIVE_ROUTING) {
1127 
1128 			memcpy(sub_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
1129 
1130 			return 1;
1131 		}
1132 	}
1133 	return 0;
1134 }
1135 
1136 static int sas_check_level_subtractive_boundary(struct domain_device *dev)
1137 {
1138 	struct expander_device *ex = &dev->ex_dev;
1139 	struct domain_device *child;
1140 	u8 sub_addr[SAS_ADDR_SIZE] = {0, };
1141 
1142 	list_for_each_entry(child, &ex->children, siblings) {
1143 		if (!dev_is_expander(child->dev_type))
1144 			continue;
1145 		if (sub_addr[0] == 0) {
1146 			sas_find_sub_addr(child, sub_addr);
1147 			continue;
1148 		} else {
1149 			u8 s2[SAS_ADDR_SIZE];
1150 
1151 			if (sas_find_sub_addr(child, s2) &&
1152 			    (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {
1153 
1154 				pr_notice("ex %016llx->%016llx-?->%016llx diverges from subtractive boundary %016llx\n",
1155 					  SAS_ADDR(dev->sas_addr),
1156 					  SAS_ADDR(child->sas_addr),
1157 					  SAS_ADDR(s2),
1158 					  SAS_ADDR(sub_addr));
1159 
1160 				sas_ex_disable_port(child, s2);
1161 			}
1162 		}
1163 	}
1164 	return 0;
1165 }
1166 /**
1167  * sas_ex_discover_devices - discover devices attached to this expander
1168  * @dev: pointer to the expander domain device
1169  * @single: if you want to do a single phy, else set to -1;
1170  *
1171  * Configure this expander for use with its devices and register the
1172  * devices of this expander.
1173  */
1174 static int sas_ex_discover_devices(struct domain_device *dev, int single)
1175 {
1176 	struct expander_device *ex = &dev->ex_dev;
1177 	int i = 0, end = ex->num_phys;
1178 	int res = 0;
1179 
1180 	if (0 <= single && single < end) {
1181 		i = single;
1182 		end = i+1;
1183 	}
1184 
1185 	for ( ; i < end; i++) {
1186 		struct ex_phy *ex_phy = &ex->ex_phy[i];
1187 
1188 		if (ex_phy->phy_state == PHY_VACANT ||
1189 		    ex_phy->phy_state == PHY_NOT_PRESENT ||
1190 		    ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
1191 			continue;
1192 
1193 		switch (ex_phy->linkrate) {
1194 		case SAS_PHY_DISABLED:
1195 		case SAS_PHY_RESET_PROBLEM:
1196 		case SAS_SATA_PORT_SELECTOR:
1197 			continue;
1198 		default:
1199 			res = sas_ex_discover_dev(dev, i);
1200 			if (res)
1201 				break;
1202 			continue;
1203 		}
1204 	}
1205 
1206 	if (!res)
1207 		sas_check_level_subtractive_boundary(dev);
1208 
1209 	return res;
1210 }
1211 
1212 static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
1213 {
1214 	struct expander_device *ex = &dev->ex_dev;
1215 	int i;
1216 	u8  *sub_sas_addr = NULL;
1217 
1218 	if (dev->dev_type != SAS_EDGE_EXPANDER_DEVICE)
1219 		return 0;
1220 
1221 	for (i = 0; i < ex->num_phys; i++) {
1222 		struct ex_phy *phy = &ex->ex_phy[i];
1223 
1224 		if (phy->phy_state == PHY_VACANT ||
1225 		    phy->phy_state == PHY_NOT_PRESENT)
1226 			continue;
1227 
1228 		if (dev_is_expander(phy->attached_dev_type) &&
1229 		    phy->routing_attr == SUBTRACTIVE_ROUTING) {
1230 
1231 			if (!sub_sas_addr)
1232 				sub_sas_addr = &phy->attached_sas_addr[0];
1233 			else if (SAS_ADDR(sub_sas_addr) !=
1234 				 SAS_ADDR(phy->attached_sas_addr)) {
1235 
1236 				pr_notice("ex %016llx phy%02d diverges(%016llx) on subtractive boundary(%016llx). Disabled\n",
1237 					  SAS_ADDR(dev->sas_addr), i,
1238 					  SAS_ADDR(phy->attached_sas_addr),
1239 					  SAS_ADDR(sub_sas_addr));
1240 				sas_ex_disable_phy(dev, i);
1241 			}
1242 		}
1243 	}
1244 	return 0;
1245 }
1246 
1247 static void sas_print_parent_topology_bug(struct domain_device *child,
1248 						 struct ex_phy *parent_phy,
1249 						 struct ex_phy *child_phy)
1250 {
1251 	static const char *ex_type[] = {
1252 		[SAS_EDGE_EXPANDER_DEVICE] = "edge",
1253 		[SAS_FANOUT_EXPANDER_DEVICE] = "fanout",
1254 	};
1255 	struct domain_device *parent = child->parent;
1256 
1257 	pr_notice("%s ex %016llx phy%02d <--> %s ex %016llx phy%02d has %c:%c routing link!\n",
1258 		  ex_type[parent->dev_type],
1259 		  SAS_ADDR(parent->sas_addr),
1260 		  parent_phy->phy_id,
1261 
1262 		  ex_type[child->dev_type],
1263 		  SAS_ADDR(child->sas_addr),
1264 		  child_phy->phy_id,
1265 
1266 		  sas_route_char(parent, parent_phy),
1267 		  sas_route_char(child, child_phy));
1268 }
1269 
1270 static int sas_check_eeds(struct domain_device *child,
1271 				 struct ex_phy *parent_phy,
1272 				 struct ex_phy *child_phy)
1273 {
1274 	int res = 0;
1275 	struct domain_device *parent = child->parent;
1276 
1277 	if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
1278 		res = -ENODEV;
1279 		pr_warn("edge ex %016llx phy S:%02d <--> edge ex %016llx phy S:%02d, while there is a fanout ex %016llx\n",
1280 			SAS_ADDR(parent->sas_addr),
1281 			parent_phy->phy_id,
1282 			SAS_ADDR(child->sas_addr),
1283 			child_phy->phy_id,
1284 			SAS_ADDR(parent->port->disc.fanout_sas_addr));
1285 	} else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
1286 		memcpy(parent->port->disc.eeds_a, parent->sas_addr,
1287 		       SAS_ADDR_SIZE);
1288 		memcpy(parent->port->disc.eeds_b, child->sas_addr,
1289 		       SAS_ADDR_SIZE);
1290 	} else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
1291 		    SAS_ADDR(parent->sas_addr)) ||
1292 		   (SAS_ADDR(parent->port->disc.eeds_a) ==
1293 		    SAS_ADDR(child->sas_addr)))
1294 		   &&
1295 		   ((SAS_ADDR(parent->port->disc.eeds_b) ==
1296 		     SAS_ADDR(parent->sas_addr)) ||
1297 		    (SAS_ADDR(parent->port->disc.eeds_b) ==
1298 		     SAS_ADDR(child->sas_addr))))
1299 		;
1300 	else {
1301 		res = -ENODEV;
1302 		pr_warn("edge ex %016llx phy%02d <--> edge ex %016llx phy%02d link forms a third EEDS!\n",
1303 			SAS_ADDR(parent->sas_addr),
1304 			parent_phy->phy_id,
1305 			SAS_ADDR(child->sas_addr),
1306 			child_phy->phy_id);
1307 	}
1308 
1309 	return res;
1310 }
1311 
1312 /* Here we spill over 80 columns.  It is intentional.
1313  */
1314 static int sas_check_parent_topology(struct domain_device *child)
1315 {
1316 	struct expander_device *child_ex = &child->ex_dev;
1317 	struct expander_device *parent_ex;
1318 	int i;
1319 	int res = 0;
1320 
1321 	if (!child->parent)
1322 		return 0;
1323 
1324 	if (!dev_is_expander(child->parent->dev_type))
1325 		return 0;
1326 
1327 	parent_ex = &child->parent->ex_dev;
1328 
1329 	for (i = 0; i < parent_ex->num_phys; i++) {
1330 		struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
1331 		struct ex_phy *child_phy;
1332 
1333 		if (parent_phy->phy_state == PHY_VACANT ||
1334 		    parent_phy->phy_state == PHY_NOT_PRESENT)
1335 			continue;
1336 
1337 		if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
1338 			continue;
1339 
1340 		child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];
1341 
1342 		switch (child->parent->dev_type) {
1343 		case SAS_EDGE_EXPANDER_DEVICE:
1344 			if (child->dev_type == SAS_FANOUT_EXPANDER_DEVICE) {
1345 				if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
1346 				    child_phy->routing_attr != TABLE_ROUTING) {
1347 					sas_print_parent_topology_bug(child, parent_phy, child_phy);
1348 					res = -ENODEV;
1349 				}
1350 			} else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1351 				if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
1352 					res = sas_check_eeds(child, parent_phy, child_phy);
1353 				} else if (child_phy->routing_attr != TABLE_ROUTING) {
1354 					sas_print_parent_topology_bug(child, parent_phy, child_phy);
1355 					res = -ENODEV;
1356 				}
1357 			} else if (parent_phy->routing_attr == TABLE_ROUTING) {
1358 				if (child_phy->routing_attr == SUBTRACTIVE_ROUTING ||
1359 				    (child_phy->routing_attr == TABLE_ROUTING &&
1360 				     child_ex->t2t_supp && parent_ex->t2t_supp)) {
1361 					/* All good */;
1362 				} else {
1363 					sas_print_parent_topology_bug(child, parent_phy, child_phy);
1364 					res = -ENODEV;
1365 				}
1366 			}
1367 			break;
1368 		case SAS_FANOUT_EXPANDER_DEVICE:
1369 			if (parent_phy->routing_attr != TABLE_ROUTING ||
1370 			    child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
1371 				sas_print_parent_topology_bug(child, parent_phy, child_phy);
1372 				res = -ENODEV;
1373 			}
1374 			break;
1375 		default:
1376 			break;
1377 		}
1378 	}
1379 
1380 	return res;
1381 }
1382 
1383 #define RRI_REQ_SIZE  16
1384 #define RRI_RESP_SIZE 44
1385 
1386 static int sas_configure_present(struct domain_device *dev, int phy_id,
1387 				 u8 *sas_addr, int *index, int *present)
1388 {
1389 	int i, res = 0;
1390 	struct expander_device *ex = &dev->ex_dev;
1391 	struct ex_phy *phy = &ex->ex_phy[phy_id];
1392 	u8 *rri_req;
1393 	u8 *rri_resp;
1394 
1395 	*present = 0;
1396 	*index = 0;
1397 
1398 	rri_req = alloc_smp_req(RRI_REQ_SIZE);
1399 	if (!rri_req)
1400 		return -ENOMEM;
1401 
1402 	rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
1403 	if (!rri_resp) {
1404 		kfree(rri_req);
1405 		return -ENOMEM;
1406 	}
1407 
1408 	rri_req[1] = SMP_REPORT_ROUTE_INFO;
1409 	rri_req[9] = phy_id;
1410 
1411 	for (i = 0; i < ex->max_route_indexes ; i++) {
1412 		*(__be16 *)(rri_req+6) = cpu_to_be16(i);
1413 		res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
1414 				       RRI_RESP_SIZE);
1415 		if (res)
1416 			goto out;
1417 		res = rri_resp[2];
1418 		if (res == SMP_RESP_NO_INDEX) {
1419 			pr_warn("overflow of indexes: dev %016llx phy%02d index 0x%x\n",
1420 				SAS_ADDR(dev->sas_addr), phy_id, i);
1421 			goto out;
1422 		} else if (res != SMP_RESP_FUNC_ACC) {
1423 			pr_notice("%s: dev %016llx phy%02d index 0x%x result 0x%x\n",
1424 				  __func__, SAS_ADDR(dev->sas_addr), phy_id,
1425 				  i, res);
1426 			goto out;
1427 		}
1428 		if (SAS_ADDR(sas_addr) != 0) {
1429 			if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
1430 				*index = i;
1431 				if ((rri_resp[12] & 0x80) == 0x80)
1432 					*present = 0;
1433 				else
1434 					*present = 1;
1435 				goto out;
1436 			} else if (SAS_ADDR(rri_resp+16) == 0) {
1437 				*index = i;
1438 				*present = 0;
1439 				goto out;
1440 			}
1441 		} else if (SAS_ADDR(rri_resp+16) == 0 &&
1442 			   phy->last_da_index < i) {
1443 			phy->last_da_index = i;
1444 			*index = i;
1445 			*present = 0;
1446 			goto out;
1447 		}
1448 	}
1449 	res = -1;
1450 out:
1451 	kfree(rri_req);
1452 	kfree(rri_resp);
1453 	return res;
1454 }
1455 
1456 #define CRI_REQ_SIZE  44
1457 #define CRI_RESP_SIZE  8
1458 
1459 static int sas_configure_set(struct domain_device *dev, int phy_id,
1460 			     u8 *sas_addr, int index, int include)
1461 {
1462 	int res;
1463 	u8 *cri_req;
1464 	u8 *cri_resp;
1465 
1466 	cri_req = alloc_smp_req(CRI_REQ_SIZE);
1467 	if (!cri_req)
1468 		return -ENOMEM;
1469 
1470 	cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
1471 	if (!cri_resp) {
1472 		kfree(cri_req);
1473 		return -ENOMEM;
1474 	}
1475 
1476 	cri_req[1] = SMP_CONF_ROUTE_INFO;
1477 	*(__be16 *)(cri_req+6) = cpu_to_be16(index);
1478 	cri_req[9] = phy_id;
1479 	if (SAS_ADDR(sas_addr) == 0 || !include)
1480 		cri_req[12] |= 0x80;
1481 	memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);
1482 
1483 	res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
1484 			       CRI_RESP_SIZE);
1485 	if (res)
1486 		goto out;
1487 	res = cri_resp[2];
1488 	if (res == SMP_RESP_NO_INDEX) {
1489 		pr_warn("overflow of indexes: dev %016llx phy%02d index 0x%x\n",
1490 			SAS_ADDR(dev->sas_addr), phy_id, index);
1491 	}
1492 out:
1493 	kfree(cri_req);
1494 	kfree(cri_resp);
1495 	return res;
1496 }
1497 
1498 static int sas_configure_phy(struct domain_device *dev, int phy_id,
1499 				    u8 *sas_addr, int include)
1500 {
1501 	int index;
1502 	int present;
1503 	int res;
1504 
1505 	res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
1506 	if (res)
1507 		return res;
1508 	if (include ^ present)
1509 		return sas_configure_set(dev, phy_id, sas_addr, index,include);
1510 
1511 	return res;
1512 }
1513 
1514 /**
1515  * sas_configure_parent - configure routing table of parent
1516  * @parent: parent expander
1517  * @child: child expander
1518  * @sas_addr: SAS port identifier of device directly attached to child
1519  * @include: whether or not to include @child in the expander routing table
1520  */
1521 static int sas_configure_parent(struct domain_device *parent,
1522 				struct domain_device *child,
1523 				u8 *sas_addr, int include)
1524 {
1525 	struct expander_device *ex_parent = &parent->ex_dev;
1526 	int res = 0;
1527 	int i;
1528 
1529 	if (parent->parent) {
1530 		res = sas_configure_parent(parent->parent, parent, sas_addr,
1531 					   include);
1532 		if (res)
1533 			return res;
1534 	}
1535 
1536 	if (ex_parent->conf_route_table == 0) {
1537 		pr_debug("ex %016llx has self-configuring routing table\n",
1538 			 SAS_ADDR(parent->sas_addr));
1539 		return 0;
1540 	}
1541 
1542 	for (i = 0; i < ex_parent->num_phys; i++) {
1543 		struct ex_phy *phy = &ex_parent->ex_phy[i];
1544 
1545 		if ((phy->routing_attr == TABLE_ROUTING) &&
1546 		    (SAS_ADDR(phy->attached_sas_addr) ==
1547 		     SAS_ADDR(child->sas_addr))) {
1548 			res = sas_configure_phy(parent, i, sas_addr, include);
1549 			if (res)
1550 				return res;
1551 		}
1552 	}
1553 
1554 	return res;
1555 }
1556 
1557 /**
1558  * sas_configure_routing - configure routing
1559  * @dev: expander device
1560  * @sas_addr: port identifier of device directly attached to the expander device
1561  */
1562 static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr)
1563 {
1564 	if (dev->parent)
1565 		return sas_configure_parent(dev->parent, dev, sas_addr, 1);
1566 	return 0;
1567 }
1568 
1569 static int sas_disable_routing(struct domain_device *dev,  u8 *sas_addr)
1570 {
1571 	if (dev->parent)
1572 		return sas_configure_parent(dev->parent, dev, sas_addr, 0);
1573 	return 0;
1574 }
1575 
1576 /**
1577  * sas_discover_expander - expander discovery
1578  * @dev: pointer to expander domain device
1579  *
1580  * See comment in sas_discover_sata().
1581  */
1582 static int sas_discover_expander(struct domain_device *dev)
1583 {
1584 	int res;
1585 
1586 	res = sas_notify_lldd_dev_found(dev);
1587 	if (res)
1588 		return res;
1589 
1590 	res = sas_ex_general(dev);
1591 	if (res)
1592 		goto out_err;
1593 	res = sas_ex_manuf_info(dev);
1594 	if (res)
1595 		goto out_err;
1596 
1597 	res = sas_expander_discover(dev);
1598 	if (res) {
1599 		pr_warn("expander %016llx discovery failed(0x%x)\n",
1600 			SAS_ADDR(dev->sas_addr), res);
1601 		goto out_err;
1602 	}
1603 
1604 	sas_check_ex_subtractive_boundary(dev);
1605 	res = sas_check_parent_topology(dev);
1606 	if (res)
1607 		goto out_err;
1608 	return 0;
1609 out_err:
1610 	sas_notify_lldd_dev_gone(dev);
1611 	return res;
1612 }
1613 
1614 static int sas_ex_level_discovery(struct asd_sas_port *port, const int level)
1615 {
1616 	int res = 0;
1617 	struct domain_device *dev;
1618 
1619 	list_for_each_entry(dev, &port->dev_list, dev_list_node) {
1620 		if (dev_is_expander(dev->dev_type)) {
1621 			struct sas_expander_device *ex =
1622 				rphy_to_expander_device(dev->rphy);
1623 
1624 			if (level == ex->level)
1625 				res = sas_ex_discover_devices(dev, -1);
1626 			else if (level > 0)
1627 				res = sas_ex_discover_devices(port->port_dev, -1);
1628 
1629 		}
1630 	}
1631 
1632 	return res;
1633 }
1634 
1635 static int sas_ex_bfs_disc(struct asd_sas_port *port)
1636 {
1637 	int res;
1638 	int level;
1639 
1640 	do {
1641 		level = port->disc.max_level;
1642 		res = sas_ex_level_discovery(port, level);
1643 		mb();
1644 	} while (level < port->disc.max_level);
1645 
1646 	return res;
1647 }
1648 
1649 int sas_discover_root_expander(struct domain_device *dev)
1650 {
1651 	int res;
1652 	struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1653 
1654 	res = sas_rphy_add(dev->rphy);
1655 	if (res)
1656 		goto out_err;
1657 
1658 	ex->level = dev->port->disc.max_level; /* 0 */
1659 	res = sas_discover_expander(dev);
1660 	if (res)
1661 		goto out_err2;
1662 
1663 	sas_ex_bfs_disc(dev->port);
1664 
1665 	return res;
1666 
1667 out_err2:
1668 	sas_rphy_remove(dev->rphy);
1669 out_err:
1670 	return res;
1671 }
1672 
1673 /* ---------- Domain revalidation ---------- */
1674 
1675 static int sas_get_phy_discover(struct domain_device *dev,
1676 				int phy_id, struct smp_resp *disc_resp)
1677 {
1678 	int res;
1679 	u8 *disc_req;
1680 
1681 	disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
1682 	if (!disc_req)
1683 		return -ENOMEM;
1684 
1685 	disc_req[1] = SMP_DISCOVER;
1686 	disc_req[9] = phy_id;
1687 
1688 	res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
1689 			       disc_resp, DISCOVER_RESP_SIZE);
1690 	if (res)
1691 		goto out;
1692 	else if (disc_resp->result != SMP_RESP_FUNC_ACC) {
1693 		res = disc_resp->result;
1694 		goto out;
1695 	}
1696 out:
1697 	kfree(disc_req);
1698 	return res;
1699 }
1700 
1701 static int sas_get_phy_change_count(struct domain_device *dev,
1702 				    int phy_id, int *pcc)
1703 {
1704 	int res;
1705 	struct smp_resp *disc_resp;
1706 
1707 	disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1708 	if (!disc_resp)
1709 		return -ENOMEM;
1710 
1711 	res = sas_get_phy_discover(dev, phy_id, disc_resp);
1712 	if (!res)
1713 		*pcc = disc_resp->disc.change_count;
1714 
1715 	kfree(disc_resp);
1716 	return res;
1717 }
1718 
1719 static int sas_get_phy_attached_dev(struct domain_device *dev, int phy_id,
1720 				    u8 *sas_addr, enum sas_device_type *type)
1721 {
1722 	int res;
1723 	struct smp_resp *disc_resp;
1724 	struct discover_resp *dr;
1725 
1726 	disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
1727 	if (!disc_resp)
1728 		return -ENOMEM;
1729 	dr = &disc_resp->disc;
1730 
1731 	res = sas_get_phy_discover(dev, phy_id, disc_resp);
1732 	if (res == 0) {
1733 		memcpy(sas_addr, disc_resp->disc.attached_sas_addr,
1734 		       SAS_ADDR_SIZE);
1735 		*type = to_dev_type(dr);
1736 		if (*type == 0)
1737 			memset(sas_addr, 0, SAS_ADDR_SIZE);
1738 	}
1739 	kfree(disc_resp);
1740 	return res;
1741 }
1742 
1743 static int sas_find_bcast_phy(struct domain_device *dev, int *phy_id,
1744 			      int from_phy, bool update)
1745 {
1746 	struct expander_device *ex = &dev->ex_dev;
1747 	int res = 0;
1748 	int i;
1749 
1750 	for (i = from_phy; i < ex->num_phys; i++) {
1751 		int phy_change_count = 0;
1752 
1753 		res = sas_get_phy_change_count(dev, i, &phy_change_count);
1754 		switch (res) {
1755 		case SMP_RESP_PHY_VACANT:
1756 		case SMP_RESP_NO_PHY:
1757 			continue;
1758 		case SMP_RESP_FUNC_ACC:
1759 			break;
1760 		default:
1761 			return res;
1762 		}
1763 
1764 		if (phy_change_count != ex->ex_phy[i].phy_change_count) {
1765 			if (update)
1766 				ex->ex_phy[i].phy_change_count =
1767 					phy_change_count;
1768 			*phy_id = i;
1769 			return 0;
1770 		}
1771 	}
1772 	return 0;
1773 }
1774 
1775 static int sas_get_ex_change_count(struct domain_device *dev, int *ecc)
1776 {
1777 	int res;
1778 	u8  *rg_req;
1779 	struct smp_resp  *rg_resp;
1780 
1781 	rg_req = alloc_smp_req(RG_REQ_SIZE);
1782 	if (!rg_req)
1783 		return -ENOMEM;
1784 
1785 	rg_resp = alloc_smp_resp(RG_RESP_SIZE);
1786 	if (!rg_resp) {
1787 		kfree(rg_req);
1788 		return -ENOMEM;
1789 	}
1790 
1791 	rg_req[1] = SMP_REPORT_GENERAL;
1792 
1793 	res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
1794 			       RG_RESP_SIZE);
1795 	if (res)
1796 		goto out;
1797 	if (rg_resp->result != SMP_RESP_FUNC_ACC) {
1798 		res = rg_resp->result;
1799 		goto out;
1800 	}
1801 
1802 	*ecc = be16_to_cpu(rg_resp->rg.change_count);
1803 out:
1804 	kfree(rg_resp);
1805 	kfree(rg_req);
1806 	return res;
1807 }
1808 /**
1809  * sas_find_bcast_dev -  find the device issue BROADCAST(CHANGE).
1810  * @dev:domain device to be detect.
1811  * @src_dev: the device which originated BROADCAST(CHANGE).
1812  *
1813  * Add self-configuration expander support. Suppose two expander cascading,
1814  * when the first level expander is self-configuring, hotplug the disks in
1815  * second level expander, BROADCAST(CHANGE) will not only be originated
1816  * in the second level expander, but also be originated in the first level
1817  * expander (see SAS protocol SAS 2r-14, 7.11 for detail), it is to say,
1818  * expander changed count in two level expanders will all increment at least
1819  * once, but the phy which chang count has changed is the source device which
1820  * we concerned.
1821  */
1822 
1823 static int sas_find_bcast_dev(struct domain_device *dev,
1824 			      struct domain_device **src_dev)
1825 {
1826 	struct expander_device *ex = &dev->ex_dev;
1827 	int ex_change_count = -1;
1828 	int phy_id = -1;
1829 	int res;
1830 	struct domain_device *ch;
1831 
1832 	res = sas_get_ex_change_count(dev, &ex_change_count);
1833 	if (res)
1834 		goto out;
1835 	if (ex_change_count != -1 && ex_change_count != ex->ex_change_count) {
1836 		/* Just detect if this expander phys phy change count changed,
1837 		* in order to determine if this expander originate BROADCAST,
1838 		* and do not update phy change count field in our structure.
1839 		*/
1840 		res = sas_find_bcast_phy(dev, &phy_id, 0, false);
1841 		if (phy_id != -1) {
1842 			*src_dev = dev;
1843 			ex->ex_change_count = ex_change_count;
1844 			pr_info("ex %016llx phy%02d change count has changed\n",
1845 				SAS_ADDR(dev->sas_addr), phy_id);
1846 			return res;
1847 		} else
1848 			pr_info("ex %016llx phys DID NOT change\n",
1849 				SAS_ADDR(dev->sas_addr));
1850 	}
1851 	list_for_each_entry(ch, &ex->children, siblings) {
1852 		if (dev_is_expander(ch->dev_type)) {
1853 			res = sas_find_bcast_dev(ch, src_dev);
1854 			if (*src_dev)
1855 				return res;
1856 		}
1857 	}
1858 out:
1859 	return res;
1860 }
1861 
1862 static void sas_unregister_ex_tree(struct asd_sas_port *port, struct domain_device *dev)
1863 {
1864 	struct expander_device *ex = &dev->ex_dev;
1865 	struct domain_device *child, *n;
1866 
1867 	list_for_each_entry_safe(child, n, &ex->children, siblings) {
1868 		set_bit(SAS_DEV_GONE, &child->state);
1869 		if (dev_is_expander(child->dev_type))
1870 			sas_unregister_ex_tree(port, child);
1871 		else
1872 			sas_unregister_dev(port, child);
1873 	}
1874 	sas_unregister_dev(port, dev);
1875 }
1876 
1877 static void sas_unregister_devs_sas_addr(struct domain_device *parent,
1878 					 int phy_id, bool last)
1879 {
1880 	struct expander_device *ex_dev = &parent->ex_dev;
1881 	struct ex_phy *phy = &ex_dev->ex_phy[phy_id];
1882 	struct domain_device *child, *n, *found = NULL;
1883 	if (last) {
1884 		list_for_each_entry_safe(child, n,
1885 			&ex_dev->children, siblings) {
1886 			if (SAS_ADDR(child->sas_addr) ==
1887 			    SAS_ADDR(phy->attached_sas_addr)) {
1888 				set_bit(SAS_DEV_GONE, &child->state);
1889 				if (dev_is_expander(child->dev_type))
1890 					sas_unregister_ex_tree(parent->port, child);
1891 				else
1892 					sas_unregister_dev(parent->port, child);
1893 				found = child;
1894 				break;
1895 			}
1896 		}
1897 		sas_disable_routing(parent, phy->attached_sas_addr);
1898 	}
1899 	memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
1900 	if (phy->port) {
1901 		sas_port_delete_phy(phy->port, phy->phy);
1902 		sas_device_set_phy(found, phy->port);
1903 		if (phy->port->num_phys == 0)
1904 			list_add_tail(&phy->port->del_list,
1905 				&parent->port->sas_port_del_list);
1906 		phy->port = NULL;
1907 	}
1908 }
1909 
1910 static int sas_discover_bfs_by_root_level(struct domain_device *root,
1911 					  const int level)
1912 {
1913 	struct expander_device *ex_root = &root->ex_dev;
1914 	struct domain_device *child;
1915 	int res = 0;
1916 
1917 	list_for_each_entry(child, &ex_root->children, siblings) {
1918 		if (dev_is_expander(child->dev_type)) {
1919 			struct sas_expander_device *ex =
1920 				rphy_to_expander_device(child->rphy);
1921 
1922 			if (level > ex->level)
1923 				res = sas_discover_bfs_by_root_level(child,
1924 								     level);
1925 			else if (level == ex->level)
1926 				res = sas_ex_discover_devices(child, -1);
1927 		}
1928 	}
1929 	return res;
1930 }
1931 
1932 static int sas_discover_bfs_by_root(struct domain_device *dev)
1933 {
1934 	int res;
1935 	struct sas_expander_device *ex = rphy_to_expander_device(dev->rphy);
1936 	int level = ex->level+1;
1937 
1938 	res = sas_ex_discover_devices(dev, -1);
1939 	if (res)
1940 		goto out;
1941 	do {
1942 		res = sas_discover_bfs_by_root_level(dev, level);
1943 		mb();
1944 		level += 1;
1945 	} while (level <= dev->port->disc.max_level);
1946 out:
1947 	return res;
1948 }
1949 
1950 static int sas_discover_new(struct domain_device *dev, int phy_id)
1951 {
1952 	struct ex_phy *ex_phy = &dev->ex_dev.ex_phy[phy_id];
1953 	struct domain_device *child;
1954 	int res;
1955 
1956 	pr_debug("ex %016llx phy%02d new device attached\n",
1957 		 SAS_ADDR(dev->sas_addr), phy_id);
1958 	res = sas_ex_phy_discover(dev, phy_id);
1959 	if (res)
1960 		return res;
1961 
1962 	if (sas_ex_join_wide_port(dev, phy_id))
1963 		return 0;
1964 
1965 	res = sas_ex_discover_devices(dev, phy_id);
1966 	if (res)
1967 		return res;
1968 	list_for_each_entry(child, &dev->ex_dev.children, siblings) {
1969 		if (SAS_ADDR(child->sas_addr) ==
1970 		    SAS_ADDR(ex_phy->attached_sas_addr)) {
1971 			if (dev_is_expander(child->dev_type))
1972 				res = sas_discover_bfs_by_root(child);
1973 			break;
1974 		}
1975 	}
1976 	return res;
1977 }
1978 
1979 static bool dev_type_flutter(enum sas_device_type new, enum sas_device_type old)
1980 {
1981 	if (old == new)
1982 		return true;
1983 
1984 	/* treat device directed resets as flutter, if we went
1985 	 * SAS_END_DEVICE to SAS_SATA_PENDING the link needs recovery
1986 	 */
1987 	if ((old == SAS_SATA_PENDING && new == SAS_END_DEVICE) ||
1988 	    (old == SAS_END_DEVICE && new == SAS_SATA_PENDING))
1989 		return true;
1990 
1991 	return false;
1992 }
1993 
1994 static int sas_rediscover_dev(struct domain_device *dev, int phy_id,
1995 			      bool last, int sibling)
1996 {
1997 	struct expander_device *ex = &dev->ex_dev;
1998 	struct ex_phy *phy = &ex->ex_phy[phy_id];
1999 	enum sas_device_type type = SAS_PHY_UNUSED;
2000 	u8 sas_addr[SAS_ADDR_SIZE];
2001 	char msg[80] = "";
2002 	int res;
2003 
2004 	if (!last)
2005 		sprintf(msg, ", part of a wide port with phy%02d", sibling);
2006 
2007 	pr_debug("ex %016llx rediscovering phy%02d%s\n",
2008 		 SAS_ADDR(dev->sas_addr), phy_id, msg);
2009 
2010 	memset(sas_addr, 0, SAS_ADDR_SIZE);
2011 	res = sas_get_phy_attached_dev(dev, phy_id, sas_addr, &type);
2012 	switch (res) {
2013 	case SMP_RESP_NO_PHY:
2014 		phy->phy_state = PHY_NOT_PRESENT;
2015 		sas_unregister_devs_sas_addr(dev, phy_id, last);
2016 		return res;
2017 	case SMP_RESP_PHY_VACANT:
2018 		phy->phy_state = PHY_VACANT;
2019 		sas_unregister_devs_sas_addr(dev, phy_id, last);
2020 		return res;
2021 	case SMP_RESP_FUNC_ACC:
2022 		break;
2023 	case -ECOMM:
2024 		break;
2025 	default:
2026 		return res;
2027 	}
2028 
2029 	if ((SAS_ADDR(sas_addr) == 0) || (res == -ECOMM)) {
2030 		phy->phy_state = PHY_EMPTY;
2031 		sas_unregister_devs_sas_addr(dev, phy_id, last);
2032 		/*
2033 		 * Even though the PHY is empty, for convenience we discover
2034 		 * the PHY to update the PHY info, like negotiated linkrate.
2035 		 */
2036 		sas_ex_phy_discover(dev, phy_id);
2037 		return res;
2038 	} else if (SAS_ADDR(sas_addr) == SAS_ADDR(phy->attached_sas_addr) &&
2039 		   dev_type_flutter(type, phy->attached_dev_type)) {
2040 		struct domain_device *ata_dev = sas_ex_to_ata(dev, phy_id);
2041 		char *action = "";
2042 
2043 		sas_ex_phy_discover(dev, phy_id);
2044 
2045 		if (ata_dev && phy->attached_dev_type == SAS_SATA_PENDING)
2046 			action = ", needs recovery";
2047 		pr_debug("ex %016llx phy%02d broadcast flutter%s\n",
2048 			 SAS_ADDR(dev->sas_addr), phy_id, action);
2049 		return res;
2050 	}
2051 
2052 	/* we always have to delete the old device when we went here */
2053 	pr_info("ex %016llx phy%02d replace %016llx\n",
2054 		SAS_ADDR(dev->sas_addr), phy_id,
2055 		SAS_ADDR(phy->attached_sas_addr));
2056 	sas_unregister_devs_sas_addr(dev, phy_id, last);
2057 
2058 	return sas_discover_new(dev, phy_id);
2059 }
2060 
2061 /**
2062  * sas_rediscover - revalidate the domain.
2063  * @dev:domain device to be detect.
2064  * @phy_id: the phy id will be detected.
2065  *
2066  * NOTE: this process _must_ quit (return) as soon as any connection
2067  * errors are encountered.  Connection recovery is done elsewhere.
2068  * Discover process only interrogates devices in order to discover the
2069  * domain.For plugging out, we un-register the device only when it is
2070  * the last phy in the port, for other phys in this port, we just delete it
2071  * from the port.For inserting, we do discovery when it is the
2072  * first phy,for other phys in this port, we add it to the port to
2073  * forming the wide-port.
2074  */
2075 static int sas_rediscover(struct domain_device *dev, const int phy_id)
2076 {
2077 	struct expander_device *ex = &dev->ex_dev;
2078 	struct ex_phy *changed_phy = &ex->ex_phy[phy_id];
2079 	int res = 0;
2080 	int i;
2081 	bool last = true;	/* is this the last phy of the port */
2082 
2083 	pr_debug("ex %016llx phy%02d originated BROADCAST(CHANGE)\n",
2084 		 SAS_ADDR(dev->sas_addr), phy_id);
2085 
2086 	if (SAS_ADDR(changed_phy->attached_sas_addr) != 0) {
2087 		for (i = 0; i < ex->num_phys; i++) {
2088 			struct ex_phy *phy = &ex->ex_phy[i];
2089 
2090 			if (i == phy_id)
2091 				continue;
2092 			if (SAS_ADDR(phy->attached_sas_addr) ==
2093 			    SAS_ADDR(changed_phy->attached_sas_addr)) {
2094 				last = false;
2095 				break;
2096 			}
2097 		}
2098 		res = sas_rediscover_dev(dev, phy_id, last, i);
2099 	} else
2100 		res = sas_discover_new(dev, phy_id);
2101 	return res;
2102 }
2103 
2104 /**
2105  * sas_ex_revalidate_domain - revalidate the domain
2106  * @port_dev: port domain device.
2107  *
2108  * NOTE: this process _must_ quit (return) as soon as any connection
2109  * errors are encountered.  Connection recovery is done elsewhere.
2110  * Discover process only interrogates devices in order to discover the
2111  * domain.
2112  */
2113 int sas_ex_revalidate_domain(struct domain_device *port_dev)
2114 {
2115 	int res;
2116 	struct domain_device *dev = NULL;
2117 
2118 	res = sas_find_bcast_dev(port_dev, &dev);
2119 	if (res == 0 && dev) {
2120 		struct expander_device *ex = &dev->ex_dev;
2121 		int i = 0, phy_id;
2122 
2123 		do {
2124 			phy_id = -1;
2125 			res = sas_find_bcast_phy(dev, &phy_id, i, true);
2126 			if (phy_id == -1)
2127 				break;
2128 			res = sas_rediscover(dev, phy_id);
2129 			i = phy_id + 1;
2130 		} while (i < ex->num_phys);
2131 	}
2132 	return res;
2133 }
2134 
2135 void sas_smp_handler(struct bsg_job *job, struct Scsi_Host *shost,
2136 		struct sas_rphy *rphy)
2137 {
2138 	struct domain_device *dev;
2139 	unsigned int rcvlen = 0;
2140 	int ret = -EINVAL;
2141 
2142 	/* no rphy means no smp target support (ie aic94xx host) */
2143 	if (!rphy)
2144 		return sas_smp_host_handler(job, shost);
2145 
2146 	switch (rphy->identify.device_type) {
2147 	case SAS_EDGE_EXPANDER_DEVICE:
2148 	case SAS_FANOUT_EXPANDER_DEVICE:
2149 		break;
2150 	default:
2151 		pr_err("%s: can we send a smp request to a device?\n",
2152 		       __func__);
2153 		goto out;
2154 	}
2155 
2156 	dev = sas_find_dev_by_rphy(rphy);
2157 	if (!dev) {
2158 		pr_err("%s: fail to find a domain_device?\n", __func__);
2159 		goto out;
2160 	}
2161 
2162 	/* do we need to support multiple segments? */
2163 	if (job->request_payload.sg_cnt > 1 ||
2164 	    job->reply_payload.sg_cnt > 1) {
2165 		pr_info("%s: multiple segments req %u, rsp %u\n",
2166 			__func__, job->request_payload.payload_len,
2167 			job->reply_payload.payload_len);
2168 		goto out;
2169 	}
2170 
2171 	ret = smp_execute_task_sg(dev, job->request_payload.sg_list,
2172 			job->reply_payload.sg_list);
2173 	if (ret >= 0) {
2174 		/* bsg_job_done() requires the length received  */
2175 		rcvlen = job->reply_payload.payload_len - ret;
2176 		ret = 0;
2177 	}
2178 
2179 out:
2180 	bsg_job_done(job, ret, rcvlen);
2181 }
2182