sas_expander.c 55.6 KB
Newer Older
James Bottomley's avatar
James Bottomley committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
 * Serial Attached SCSI (SAS) Expander discovery and configuration
 *
 * Copyright (C) 2005 Adaptec, Inc.  All rights reserved.
 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
 *
 * This file is licensed under GPLv2.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include <linux/scatterlist.h>
26
#include <linux/blkdev.h>
27
#include <linux/slab.h>
James Bottomley's avatar
James Bottomley committed
28
29
30

#include "sas_internal.h"

31
#include <scsi/sas_ata.h>
James Bottomley's avatar
James Bottomley committed
32
33
34
35
36
37
38
39
40
41
42
43
#include <scsi/scsi_transport.h>
#include <scsi/scsi_transport_sas.h>
#include "../scsi_sas_internal.h"

static int sas_discover_expander(struct domain_device *dev);
static int sas_configure_routing(struct domain_device *dev, u8 *sas_addr);
static int sas_configure_phy(struct domain_device *dev, int phy_id,
			     u8 *sas_addr, int include);
static int sas_disable_routing(struct domain_device *dev,  u8 *sas_addr);

/* ---------- SMP task management ---------- */

44
static void smp_task_timedout(struct timer_list *t)
James Bottomley's avatar
James Bottomley committed
45
{
46
47
	struct sas_task_slow *slow = from_timer(slow, t, timer);
	struct sas_task *task = slow->task;
James Bottomley's avatar
James Bottomley committed
48
49
50
	unsigned long flags;

	spin_lock_irqsave(&task->task_state_lock, flags);
51
	if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
James Bottomley's avatar
James Bottomley committed
52
		task->task_state_flags |= SAS_TASK_STATE_ABORTED;
53
54
		complete(&task->slow_task->completion);
	}
James Bottomley's avatar
James Bottomley committed
55
56
57
58
59
	spin_unlock_irqrestore(&task->task_state_lock, flags);
}

static void smp_task_done(struct sas_task *task)
{
60
	del_timer(&task->slow_task->timer);
61
	complete(&task->slow_task->completion);
James Bottomley's avatar
James Bottomley committed
62
63
64
65
66
}

/* Give it some long enough timeout. In seconds. */
#define SMP_TIMEOUT 10

67
68
static int smp_execute_task_sg(struct domain_device *dev,
		struct scatterlist *req, struct scatterlist *resp)
James Bottomley's avatar
James Bottomley committed
69
{
70
71
	int res, retry;
	struct sas_task *task = NULL;
James Bottomley's avatar
James Bottomley committed
72
73
74
	struct sas_internal *i =
		to_sas_internal(dev->port->ha->core.shost->transportt);

75
	mutex_lock(&dev->ex_dev.cmd_mutex);
76
	for (retry = 0; retry < 3; retry++) {
77
78
79
80
81
		if (test_bit(SAS_DEV_GONE, &dev->state)) {
			res = -ECOMM;
			break;
		}

82
		task = sas_alloc_slow_task(GFP_KERNEL);
83
84
85
86
		if (!task) {
			res = -ENOMEM;
			break;
		}
87
88
		task->dev = dev;
		task->task_proto = dev->tproto;
89
90
		task->smp_task.smp_req = *req;
		task->smp_task.smp_resp = *resp;
James Bottomley's avatar
James Bottomley committed
91

92
		task->task_done = smp_task_done;
James Bottomley's avatar
James Bottomley committed
93

94
		task->slow_task->timer.function = smp_task_timedout;
95
96
		task->slow_task->timer.expires = jiffies + SMP_TIMEOUT*HZ;
		add_timer(&task->slow_task->timer);
James Bottomley's avatar
James Bottomley committed
97

98
		res = i->dft->lldd_execute_task(task, GFP_KERNEL);
James Bottomley's avatar
James Bottomley committed
99

100
		if (res) {
101
			del_timer(&task->slow_task->timer);
102
			SAS_DPRINTK("executing SMP task failed:%d\n", res);
103
			break;
James Bottomley's avatar
James Bottomley committed
104
		}
105

106
		wait_for_completion(&task->slow_task->completion);
107
		res = -ECOMM;
108
109
110
111
112
		if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
			SAS_DPRINTK("smp task timed out or aborted\n");
			i->dft->lldd_abort_task(task);
			if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
				SAS_DPRINTK("SMP task aborted and not done\n");
113
				break;
114
115
116
			}
		}
		if (task->task_status.resp == SAS_TASK_COMPLETE &&
117
		    task->task_status.stat == SAM_STAT_GOOD) {
118
119
			res = 0;
			break;
120
121
122
		}
		if (task->task_status.resp == SAS_TASK_COMPLETE &&
		    task->task_status.stat == SAS_DATA_UNDERRUN) {
123
124
125
126
			/* no error, but return the number of bytes of
			 * underrun */
			res = task->task_status.residual;
			break;
127
128
129
		}
		if (task->task_status.resp == SAS_TASK_COMPLETE &&
		    task->task_status.stat == SAS_DATA_OVERRUN) {
130
131
			res = -EMSGSIZE;
			break;
132
133
134
135
136
		}
		if (task->task_status.resp == SAS_TASK_UNDELIVERED &&
		    task->task_status.stat == SAS_DEVICE_UNKNOWN)
			break;
		else {
137
			SAS_DPRINTK("%s: task to dev %016llx response: 0x%x "
138
				    "status 0x%x\n", __func__,
139
140
141
142
143
144
				    SAS_ADDR(dev->sas_addr),
				    task->task_status.resp,
				    task->task_status.stat);
			sas_free_task(task);
			task = NULL;
		}
James Bottomley's avatar
James Bottomley committed
145
	}
146
147
	mutex_unlock(&dev->ex_dev.cmd_mutex);

148
	BUG_ON(retry == 3 && task != NULL);
149
	sas_free_task(task);
James Bottomley's avatar
James Bottomley committed
150
151
152
	return res;
}

153
154
155
156
157
158
159
160
161
162
163
static int smp_execute_task(struct domain_device *dev, void *req, int req_size,
			    void *resp, int resp_size)
{
	struct scatterlist req_sg;
	struct scatterlist resp_sg;

	sg_init_one(&req_sg, req, req_size);
	sg_init_one(&resp_sg, resp, resp_size);
	return smp_execute_task_sg(dev, &req_sg, &resp_sg);
}

James Bottomley's avatar
James Bottomley committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* ---------- Allocations ---------- */

static inline void *alloc_smp_req(int size)
{
	u8 *p = kzalloc(size, GFP_KERNEL);
	if (p)
		p[0] = SMP_REQUEST;
	return p;
}

static inline void *alloc_smp_resp(int size)
{
	return kzalloc(size, GFP_KERNEL);
}

179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
static char sas_route_char(struct domain_device *dev, struct ex_phy *phy)
{
	switch (phy->routing_attr) {
	case TABLE_ROUTING:
		if (dev->ex_dev.t2t_supp)
			return 'U';
		else
			return 'T';
	case DIRECT_ROUTING:
		return 'D';
	case SUBTRACTIVE_ROUTING:
		return 'S';
	default:
		return '?';
	}
}
James Bottomley's avatar
James Bottomley committed
195

196
static enum sas_device_type to_dev_type(struct discover_resp *dr)
James Bottomley's avatar
James Bottomley committed
197
{
198
199
200
	/* This is detecting a failure to transmit initial dev to host
	 * FIS as described in section J.5 of sas-2 r16
	 */
201
	if (dr->attached_dev_type == SAS_PHY_UNUSED && dr->attached_sata_dev &&
202
	    dr->linkrate >= SAS_LINK_RATE_1_5_GBPS)
203
		return SAS_SATA_PENDING;
204
205
206
207
208
209
	else
		return dr->attached_dev_type;
}

static void sas_set_ex_phy(struct domain_device *dev, int phy_id, void *rsp)
{
210
	enum sas_device_type dev_type;
211
212
213
214
	enum sas_linkrate linkrate;
	u8 sas_addr[SAS_ADDR_SIZE];
	struct smp_resp *resp = rsp;
	struct discover_resp *dr = &resp->disc;
215
	struct sas_ha_struct *ha = dev->port->ha;
James Bottomley's avatar
James Bottomley committed
216
217
218
	struct expander_device *ex = &dev->ex_dev;
	struct ex_phy *phy = &ex->ex_phy[phy_id];
	struct sas_rphy *rphy = dev->rphy;
219
220
	bool new_phy = !phy->phy;
	char *type;
James Bottomley's avatar
James Bottomley committed
221

222
	if (new_phy) {
223
224
		if (WARN_ON_ONCE(test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state)))
			return;
James Bottomley's avatar
James Bottomley committed
225
226
227
228
229
230
231
232
233
		phy->phy = sas_phy_alloc(&rphy->dev, phy_id);

		/* FIXME: error_handling */
		BUG_ON(!phy->phy);
	}

	switch (resp->result) {
	case SMP_RESP_PHY_VACANT:
		phy->phy_state = PHY_VACANT;
234
		break;
James Bottomley's avatar
James Bottomley committed
235
236
	default:
		phy->phy_state = PHY_NOT_PRESENT;
237
		break;
James Bottomley's avatar
James Bottomley committed
238
239
240
241
242
	case SMP_RESP_FUNC_ACC:
		phy->phy_state = PHY_EMPTY; /* do not know yet */
		break;
	}

243
244
245
246
247
	/* check if anything important changed to squelch debug */
	dev_type = phy->attached_dev_type;
	linkrate  = phy->linkrate;
	memcpy(sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);

248
249
250
	/* Handle vacant phy - rest of dr data is not valid so skip it */
	if (phy->phy_state == PHY_VACANT) {
		memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
251
		phy->attached_dev_type = SAS_PHY_UNUSED;
252
253
254
255
256
257
258
		if (!test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state)) {
			phy->phy_id = phy_id;
			goto skip;
		} else
			goto out;
	}

259
	phy->attached_dev_type = to_dev_type(dr);
260
261
	if (test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state))
		goto out;
James Bottomley's avatar
James Bottomley committed
262
263
264
265
266
267
268
	phy->phy_id = phy_id;
	phy->linkrate = dr->linkrate;
	phy->attached_sata_host = dr->attached_sata_host;
	phy->attached_sata_dev  = dr->attached_sata_dev;
	phy->attached_sata_ps   = dr->attached_sata_ps;
	phy->attached_iproto = dr->iproto << 1;
	phy->attached_tproto = dr->tproto << 1;
269
270
271
	/* help some expanders that fail to zero sas_address in the 'no
	 * device' case
	 */
272
	if (phy->attached_dev_type == SAS_PHY_UNUSED ||
273
274
275
276
	    phy->linkrate < SAS_LINK_RATE_1_5_GBPS)
		memset(phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
	else
		memcpy(phy->attached_sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE);
James Bottomley's avatar
James Bottomley committed
277
278
279
280
281
282
	phy->attached_phy_id = dr->attached_phy_id;
	phy->phy_change_count = dr->change_count;
	phy->routing_attr = dr->routing_attr;
	phy->virtual = dr->virtual;
	phy->last_da_index = -1;

283
	phy->phy->identify.sas_address = SAS_ADDR(phy->attached_sas_addr);
284
	phy->phy->identify.device_type = dr->attached_dev_type;
James Bottomley's avatar
James Bottomley committed
285
286
	phy->phy->identify.initiator_port_protocols = phy->attached_iproto;
	phy->phy->identify.target_port_protocols = phy->attached_tproto;
287
288
	if (!phy->attached_tproto && dr->attached_sata_dev)
		phy->phy->identify.target_port_protocols = SAS_PROTOCOL_SATA;
James Bottomley's avatar
James Bottomley committed
289
	phy->phy->identify.phy_identifier = phy_id;
290
291
292
293
	phy->phy->minimum_linkrate_hw = dr->hmin_linkrate;
	phy->phy->maximum_linkrate_hw = dr->hmax_linkrate;
	phy->phy->minimum_linkrate = dr->pmin_linkrate;
	phy->phy->maximum_linkrate = dr->pmax_linkrate;
294
	phy->phy->negotiated_linkrate = phy->linkrate;
295
	phy->phy->enabled = (phy->linkrate != SAS_PHY_DISABLED);
James Bottomley's avatar
James Bottomley committed
296

297
 skip:
298
	if (new_phy)
299
300
301
302
		if (sas_phy_add(phy->phy)) {
			sas_phy_free(phy->phy);
			return;
		}
James Bottomley's avatar
James Bottomley committed
303

304
 out:
305
	switch (phy->attached_dev_type) {
306
	case SAS_SATA_PENDING:
307
308
		type = "stp pending";
		break;
309
	case SAS_PHY_UNUSED:
310
311
		type = "no device";
		break;
312
	case SAS_END_DEVICE:
313
314
315
316
317
318
319
320
321
322
323
324
		if (phy->attached_iproto) {
			if (phy->attached_tproto)
				type = "host+target";
			else
				type = "host";
		} else {
			if (dr->attached_sata_dev)
				type = "stp";
			else
				type = "ssp";
		}
		break;
325
326
	case SAS_EDGE_EXPANDER_DEVICE:
	case SAS_FANOUT_EXPANDER_DEVICE:
327
328
329
330
331
		type = "smp";
		break;
	default:
		type = "unknown";
	}
James Bottomley's avatar
James Bottomley committed
332

333
334
335
336
337
338
339
340
341
342
	/* this routine is polled by libata error recovery so filter
	 * unimportant messages
	 */
	if (new_phy || phy->attached_dev_type != dev_type ||
	    phy->linkrate != linkrate ||
	    SAS_ADDR(phy->attached_sas_addr) != SAS_ADDR(sas_addr))
		/* pass */;
	else
		return;

343
344
345
346
347
348
349
350
351
	/* if the attached device type changed and ata_eh is active,
	 * make sure we run revalidation when eh completes (see:
	 * sas_enable_revalidation)
	 */
	if (test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state))
		set_bit(DISCE_REVALIDATE_DOMAIN, &dev->port->disc.pending);

	SAS_DPRINTK("%sex %016llx phy%02d:%c:%X attached: %016llx (%s)\n",
		    test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state) ? "ata: " : "",
352
353
354
		    SAS_ADDR(dev->sas_addr), phy->phy_id,
		    sas_route_char(dev, phy), phy->linkrate,
		    SAS_ADDR(phy->attached_sas_addr), type);
James Bottomley's avatar
James Bottomley committed
355
356
}

357
/* check if we have an existing attached ata device on this expander phy */
358
struct domain_device *sas_ex_to_ata(struct domain_device *ex_dev, int phy_id)
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
{
	struct ex_phy *ex_phy = &ex_dev->ex_dev.ex_phy[phy_id];
	struct domain_device *dev;
	struct sas_rphy *rphy;

	if (!ex_phy->port)
		return NULL;

	rphy = ex_phy->port->rphy;
	if (!rphy)
		return NULL;

	dev = sas_find_dev_by_rphy(rphy);

	if (dev && dev_is_sata(dev))
		return dev;

	return NULL;
}

James Bottomley's avatar
James Bottomley committed
379
380
381
#define DISCOVER_REQ_SIZE  16
#define DISCOVER_RESP_SIZE 56

382
383
384
static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
				      u8 *disc_resp, int single)
{
385
386
	struct discover_resp *dr;
	int res;
387
388
389

	disc_req[9] = single;

390
391
392
393
394
395
396
397
	res = smp_execute_task(dev, disc_req, DISCOVER_REQ_SIZE,
			       disc_resp, DISCOVER_RESP_SIZE);
	if (res)
		return res;
	dr = &((struct smp_resp *)disc_resp)->disc;
	if (memcmp(dev->sas_addr, dr->attached_sas_addr, SAS_ADDR_SIZE) == 0) {
		sas_printk("Found loopback topology, just ignore it!\n");
		return 0;
398
399
400
401
402
	}
	sas_set_ex_phy(dev, single, disc_resp);
	return 0;
}

403
int sas_ex_phy_discover(struct domain_device *dev, int single)
James Bottomley's avatar
James Bottomley committed
404
405
406
407
408
409
410
411
412
413
{
	struct expander_device *ex = &dev->ex_dev;
	int  res = 0;
	u8   *disc_req;
	u8   *disc_resp;

	disc_req = alloc_smp_req(DISCOVER_REQ_SIZE);
	if (!disc_req)
		return -ENOMEM;

414
	disc_resp = alloc_smp_resp(DISCOVER_RESP_SIZE);
James Bottomley's avatar
James Bottomley committed
415
416
417
418
419
420
421
422
	if (!disc_resp) {
		kfree(disc_req);
		return -ENOMEM;
	}

	disc_req[1] = SMP_DISCOVER;

	if (0 <= single && single < ex->num_phys) {
423
		res = sas_ex_phy_discover_helper(dev, disc_req, disc_resp, single);
James Bottomley's avatar
James Bottomley committed
424
425
426
427
	} else {
		int i;

		for (i = 0; i < ex->num_phys; i++) {
428
429
			res = sas_ex_phy_discover_helper(dev, disc_req,
							 disc_resp, i);
James Bottomley's avatar
James Bottomley committed
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
			if (res)
				goto out_err;
		}
	}
out_err:
	kfree(disc_resp);
	kfree(disc_req);
	return res;
}

static int sas_expander_discover(struct domain_device *dev)
{
	struct expander_device *ex = &dev->ex_dev;
	int res = -ENOMEM;

Kees Cook's avatar
Kees Cook committed
445
	ex->ex_phy = kcalloc(ex->num_phys, sizeof(*ex->ex_phy), GFP_KERNEL);
James Bottomley's avatar
James Bottomley committed
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
	if (!ex->ex_phy)
		return -ENOMEM;

	res = sas_ex_phy_discover(dev, -1);
	if (res)
		goto out_err;

	return 0;
 out_err:
	kfree(ex->ex_phy);
	ex->ex_phy = NULL;
	return res;
}

#define MAX_EXPANDER_PHYS 128

static void ex_assign_report_general(struct domain_device *dev,
					    struct smp_resp *resp)
{
	struct report_general_resp *rg = &resp->rg;

	dev->ex_dev.ex_change_count = be16_to_cpu(rg->change_count);
	dev->ex_dev.max_route_indexes = be16_to_cpu(rg->route_indexes);
	dev->ex_dev.num_phys = min(rg->num_phys, (u8)MAX_EXPANDER_PHYS);
470
	dev->ex_dev.t2t_supp = rg->t2t_supp;
James Bottomley's avatar
James Bottomley committed
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
	dev->ex_dev.conf_route_table = rg->conf_route_table;
	dev->ex_dev.configuring = rg->configuring;
	memcpy(dev->ex_dev.enclosure_logical_id, rg->enclosure_logical_id, 8);
}

#define RG_REQ_SIZE   8
#define RG_RESP_SIZE 32

static int sas_ex_general(struct domain_device *dev)
{
	u8 *rg_req;
	struct smp_resp *rg_resp;
	int res;
	int i;

	rg_req = alloc_smp_req(RG_REQ_SIZE);
	if (!rg_req)
		return -ENOMEM;

	rg_resp = alloc_smp_resp(RG_RESP_SIZE);
	if (!rg_resp) {
		kfree(rg_req);
		return -ENOMEM;
	}

	rg_req[1] = SMP_REPORT_GENERAL;

	for (i = 0; i < 5; i++) {
		res = smp_execute_task(dev, rg_req, RG_REQ_SIZE, rg_resp,
				       RG_RESP_SIZE);

		if (res) {
			SAS_DPRINTK("RG to ex %016llx failed:0x%x\n",
				    SAS_ADDR(dev->sas_addr), res);
			goto out;
		} else if (rg_resp->result != SMP_RESP_FUNC_ACC) {
			SAS_DPRINTK("RG:ex %016llx returned SMP result:0x%x\n",
				    SAS_ADDR(dev->sas_addr), rg_resp->result);
			res = rg_resp->result;
			goto out;
		}

		ex_assign_report_general(dev, rg_resp);

		if (dev->ex_dev.configuring) {
			SAS_DPRINTK("RG: ex %llx self-configuring...\n",
				    SAS_ADDR(dev->sas_addr));
			schedule_timeout_interruptible(5*HZ);
		} else
			break;
	}
out:
	kfree(rg_req);
	kfree(rg_resp);
	return res;
}

static void ex_assign_manuf_info(struct domain_device *dev, void
					*_mi_resp)
{
	u8 *mi_resp = _mi_resp;
	struct sas_rphy *rphy = dev->rphy;
	struct sas_expander_device *edev = rphy_to_expander_device(rphy);

	memcpy(edev->vendor_id, mi_resp + 12, SAS_EXPANDER_VENDOR_ID_LEN);
	memcpy(edev->product_id, mi_resp + 20, SAS_EXPANDER_PRODUCT_ID_LEN);
	memcpy(edev->product_rev, mi_resp + 36,
	       SAS_EXPANDER_PRODUCT_REV_LEN);

	if (mi_resp[8] & 1) {
		memcpy(edev->component_vendor_id, mi_resp + 40,
		       SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
		edev->component_id = mi_resp[48] << 8 | mi_resp[49];
		edev->component_revision_id = mi_resp[50];
	}
}

#define MI_REQ_SIZE   8
#define MI_RESP_SIZE 64

static int sas_ex_manuf_info(struct domain_device *dev)
{
	u8 *mi_req;
	u8 *mi_resp;
	int res;

	mi_req = alloc_smp_req(MI_REQ_SIZE);
	if (!mi_req)
		return -ENOMEM;

	mi_resp = alloc_smp_resp(MI_RESP_SIZE);
	if (!mi_resp) {
		kfree(mi_req);
		return -ENOMEM;
	}

	mi_req[1] = SMP_REPORT_MANUF_INFO;

	res = smp_execute_task(dev, mi_req, MI_REQ_SIZE, mi_resp,MI_RESP_SIZE);
	if (res) {
		SAS_DPRINTK("MI: ex %016llx failed:0x%x\n",
			    SAS_ADDR(dev->sas_addr), res);
		goto out;
	} else if (mi_resp[2] != SMP_RESP_FUNC_ACC) {
		SAS_DPRINTK("MI ex %016llx returned SMP result:0x%x\n",
			    SAS_ADDR(dev->sas_addr), mi_resp[2]);
		goto out;
	}

	ex_assign_manuf_info(dev, mi_resp);
out:
	kfree(mi_req);
	kfree(mi_resp);
	return res;
}

#define PC_REQ_SIZE  44
#define PC_RESP_SIZE 8

int sas_smp_phy_control(struct domain_device *dev, int phy_id,
591
592
			enum phy_func phy_func,
			struct sas_phy_linkrates *rates)
James Bottomley's avatar
James Bottomley committed
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
{
	u8 *pc_req;
	u8 *pc_resp;
	int res;

	pc_req = alloc_smp_req(PC_REQ_SIZE);
	if (!pc_req)
		return -ENOMEM;

	pc_resp = alloc_smp_resp(PC_RESP_SIZE);
	if (!pc_resp) {
		kfree(pc_req);
		return -ENOMEM;
	}

	pc_req[1] = SMP_PHY_CONTROL;
	pc_req[9] = phy_id;
	pc_req[10]= phy_func;
611
612
613
614
	if (rates) {
		pc_req[32] = rates->minimum_linkrate << 4;
		pc_req[33] = rates->maximum_linkrate << 4;
	}
James Bottomley's avatar
James Bottomley committed
615
616
617
618
619
620
621
622
623
624
625
626
627

	res = smp_execute_task(dev, pc_req, PC_REQ_SIZE, pc_resp,PC_RESP_SIZE);

	kfree(pc_resp);
	kfree(pc_req);
	return res;
}

static void sas_ex_disable_phy(struct domain_device *dev, int phy_id)
{
	struct expander_device *ex = &dev->ex_dev;
	struct ex_phy *phy = &ex->ex_phy[phy_id];

628
	sas_smp_phy_control(dev, phy_id, PHY_FUNC_DISABLE, NULL);
629
	phy->linkrate = SAS_PHY_DISABLED;
James Bottomley's avatar
James Bottomley committed
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
}

static void sas_ex_disable_port(struct domain_device *dev, u8 *sas_addr)
{
	struct expander_device *ex = &dev->ex_dev;
	int i;

	for (i = 0; i < ex->num_phys; i++) {
		struct ex_phy *phy = &ex->ex_phy[i];

		if (phy->phy_state == PHY_VACANT ||
		    phy->phy_state == PHY_NOT_PRESENT)
			continue;

		if (SAS_ADDR(phy->attached_sas_addr) == SAS_ADDR(sas_addr))
			sas_ex_disable_phy(dev, i);
	}
}

static int sas_dev_present_in_domain(struct asd_sas_port *port,
					    u8 *sas_addr)
{
	struct domain_device *dev;

	if (SAS_ADDR(port->sas_addr) == SAS_ADDR(sas_addr))
		return 1;
	list_for_each_entry(dev, &port->dev_list, dev_list_node) {
		if (SAS_ADDR(dev->sas_addr) == SAS_ADDR(sas_addr))
			return 1;
	}
	return 0;
}

#define RPEL_REQ_SIZE	16
#define RPEL_RESP_SIZE	32
int sas_smp_get_phy_events(struct sas_phy *phy)
{
	int res;
668
669
	u8 *req;
	u8 *resp;
James Bottomley's avatar
James Bottomley committed
670
671
672
	struct sas_rphy *rphy = dev_to_rphy(phy->dev.parent);
	struct domain_device *dev = sas_find_dev_by_rphy(rphy);

673
674
	req = alloc_smp_req(RPEL_REQ_SIZE);
	if (!req)
James Bottomley's avatar
James Bottomley committed
675
676
		return -ENOMEM;

677
678
679
680
681
682
	resp = alloc_smp_resp(RPEL_RESP_SIZE);
	if (!resp) {
		kfree(req);
		return -ENOMEM;
	}

James Bottomley's avatar
James Bottomley committed
683
684
685
686
687
688
	req[1] = SMP_REPORT_PHY_ERR_LOG;
	req[9] = phy->number;

	res = smp_execute_task(dev, req, RPEL_REQ_SIZE,
			            resp, RPEL_RESP_SIZE);

689
	if (res)
James Bottomley's avatar
James Bottomley committed
690
691
692
693
694
695
696
697
		goto out;

	phy->invalid_dword_count = scsi_to_u32(&resp[12]);
	phy->running_disparity_error_count = scsi_to_u32(&resp[16]);
	phy->loss_of_dword_sync_count = scsi_to_u32(&resp[20]);
	phy->phy_reset_problem_count = scsi_to_u32(&resp[24]);

 out:
698
	kfree(req);
James Bottomley's avatar
James Bottomley committed
699
700
701
702
703
	kfree(resp);
	return res;

}

704
705
#ifdef CONFIG_SCSI_SAS_ATA

James Bottomley's avatar
James Bottomley committed
706
707
708
#define RPS_REQ_SIZE  16
#define RPS_RESP_SIZE 60

709
710
int sas_get_report_phy_sata(struct domain_device *dev, int phy_id,
			    struct smp_resp *rps_resp)
James Bottomley's avatar
James Bottomley committed
711
712
713
{
	int res;
	u8 *rps_req = alloc_smp_req(RPS_REQ_SIZE);
714
	u8 *resp = (u8 *)rps_resp;
James Bottomley's avatar
James Bottomley committed
715
716
717
718
719
720
721
722
723
724

	if (!rps_req)
		return -ENOMEM;

	rps_req[1] = SMP_REPORT_PHY_SATA;
	rps_req[9] = phy_id;

	res = smp_execute_task(dev, rps_req, RPS_REQ_SIZE,
			            rps_resp, RPS_RESP_SIZE);

725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
	/* 0x34 is the FIS type for the D2H fis.  There's a potential
	 * standards cockup here.  sas-2 explicitly specifies the FIS
	 * should be encoded so that FIS type is in resp[24].
	 * However, some expanders endian reverse this.  Undo the
	 * reversal here */
	if (!res && resp[27] == 0x34 && resp[24] != 0x34) {
		int i;

		for (i = 0; i < 5; i++) {
			int j = 24 + (i*4);
			u8 a, b;
			a = resp[j + 0];
			b = resp[j + 1];
			resp[j + 0] = resp[j + 3];
			resp[j + 1] = resp[j + 2];
			resp[j + 2] = b;
			resp[j + 3] = a;
		}
	}

James Bottomley's avatar
James Bottomley committed
745
	kfree(rps_req);
746
	return res;
James Bottomley's avatar
James Bottomley committed
747
}
748
#endif
James Bottomley's avatar
James Bottomley committed
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795

static void sas_ex_get_linkrate(struct domain_device *parent,
				       struct domain_device *child,
				       struct ex_phy *parent_phy)
{
	struct expander_device *parent_ex = &parent->ex_dev;
	struct sas_port *port;
	int i;

	child->pathways = 0;

	port = parent_phy->port;

	for (i = 0; i < parent_ex->num_phys; i++) {
		struct ex_phy *phy = &parent_ex->ex_phy[i];

		if (phy->phy_state == PHY_VACANT ||
		    phy->phy_state == PHY_NOT_PRESENT)
			continue;

		if (SAS_ADDR(phy->attached_sas_addr) ==
		    SAS_ADDR(child->sas_addr)) {

			child->min_linkrate = min(parent->min_linkrate,
						  phy->linkrate);
			child->max_linkrate = max(parent->max_linkrate,
						  phy->linkrate);
			child->pathways++;
			sas_port_add_phy(port, phy->phy);
		}
	}
	child->linkrate = min(parent_phy->linkrate, child->max_linkrate);
	child->pathways = min(child->pathways, parent->pathways);
}

static struct domain_device *sas_ex_discover_end_dev(
	struct domain_device *parent, int phy_id)
{
	struct expander_device *parent_ex = &parent->ex_dev;
	struct ex_phy *phy = &parent_ex->ex_phy[phy_id];
	struct domain_device *child = NULL;
	struct sas_rphy *rphy;
	int res;

	if (phy->attached_sata_host || phy->attached_sata_ps)
		return NULL;

796
	child = sas_alloc_device();
James Bottomley's avatar
James Bottomley committed
797
798
799
	if (!child)
		return NULL;

800
	kref_get(&parent->kref);
James Bottomley's avatar
James Bottomley committed
801
802
803
804
805
	child->parent = parent;
	child->port   = parent->port;
	child->iproto = phy->attached_iproto;
	memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
	sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
806
807
808
809
810
811
812
813
814
	if (!phy->port) {
		phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
		if (unlikely(!phy->port))
			goto out_err;
		if (unlikely(sas_port_add(phy->port) != 0)) {
			sas_port_free(phy->port);
			goto out_err;
		}
	}
James Bottomley's avatar
James Bottomley committed
815
	sas_ex_get_linkrate(parent, child, phy);
816
	sas_device_set_phy(child, phy->port);
James Bottomley's avatar
James Bottomley committed
817

818
#ifdef CONFIG_SCSI_SAS_ATA
819
	if ((phy->attached_tproto & SAS_PROTOCOL_STP) || phy->attached_sata_dev) {
820
821
		res = sas_get_ata_info(child, phy);
		if (res)
822
			goto out_free;
823

824
825
826
827
		sas_init_dev(child);
		res = sas_ata_init(child);
		if (res)
			goto out_free;
828
		rphy = sas_end_device_alloc(phy->port);
829
		if (!rphy)
830
			goto out_free;
831
		rphy->identify.phy_identifier = phy_id;
832
833

		child->rphy = rphy;
834
		get_device(&rphy->dev);
835

836
		list_add_tail(&child->disco_list_node, &parent->port->disco_list);
837

James Bottomley's avatar
James Bottomley committed
838
839
840
841
842
843
		res = sas_discover_sata(child);
		if (res) {
			SAS_DPRINTK("sas_discover_sata() for device %16llx at "
				    "%016llx:0x%x returned 0x%x\n",
				    SAS_ADDR(child->sas_addr),
				    SAS_ADDR(parent->sas_addr), phy_id, res);
844
			goto out_list_del;
James Bottomley's avatar
James Bottomley committed
845
		}
846
847
	} else
#endif
848
	  if (phy->attached_tproto & SAS_PROTOCOL_SSP) {
849
		child->dev_type = SAS_END_DEVICE;
James Bottomley's avatar
James Bottomley committed
850
851
		rphy = sas_end_device_alloc(phy->port);
		/* FIXME: error handling */
852
853
		if (unlikely(!rphy))
			goto out_free;
James Bottomley's avatar
James Bottomley committed
854
855
856
857
		child->tproto = phy->attached_tproto;
		sas_init_dev(child);

		child->rphy = rphy;
858
		get_device(&rphy->dev);
859
		rphy->identify.phy_identifier = phy_id;
James Bottomley's avatar
James Bottomley committed
860
861
		sas_fill_in_rphy(child, rphy);

862
		list_add_tail(&child->disco_list_node, &parent->port->disco_list);
James Bottomley's avatar
James Bottomley committed
863
864
865
866
867
868
869

		res = sas_discover_end_dev(child);
		if (res) {
			SAS_DPRINTK("sas_discover_end_dev() for device %16llx "
				    "at %016llx:0x%x returned 0x%x\n",
				    SAS_ADDR(child->sas_addr),
				    SAS_ADDR(parent->sas_addr), phy_id, res);
870
			goto out_list_del;
James Bottomley's avatar
James Bottomley committed
871
872
873
874
875
		}
	} else {
		SAS_DPRINTK("target proto 0x%x at %016llx:0x%x not handled\n",
			    phy->attached_tproto, SAS_ADDR(parent->sas_addr),
			    phy_id);
876
		goto out_free;
James Bottomley's avatar
James Bottomley committed
877
878
879
880
	}

	list_add_tail(&child->siblings, &parent_ex->children);
	return child;
881
882

 out_list_del:
883
	sas_rphy_free(child->rphy);
884
	list_del(&child->disco_list_node);
885
	spin_lock_irq(&parent->port->dev_list_lock);
886
	list_del(&child->dev_list_node);
887
	spin_unlock_irq(&parent->port->dev_list_lock);
888
889
890
891
 out_free:
	sas_port_delete(phy->port);
 out_err:
	phy->port = NULL;
892
	sas_put_device(child);
893
	return NULL;
James Bottomley's avatar
James Bottomley committed
894
895
}

896
/* See if this phy is part of a wide port */
897
static bool sas_ex_join_wide_port(struct domain_device *parent, int phy_id)
898
899
900
901
902
903
904
905
906
907
908
909
910
{
	struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
	int i;

	for (i = 0; i < parent->ex_dev.num_phys; i++) {
		struct ex_phy *ephy = &parent->ex_dev.ex_phy[i];

		if (ephy == phy)
			continue;

		if (!memcmp(phy->attached_sas_addr, ephy->attached_sas_addr,
			    SAS_ADDR_SIZE) && ephy->port) {
			sas_port_add_phy(ephy->port, phy->phy);
911
			phy->port = ephy->port;
912
			phy->phy_state = PHY_DEVICE_DISCOVERED;
913
			return true;
914
915
916
		}
	}

917
	return false;
918
919
}

James Bottomley's avatar
James Bottomley committed
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
static struct domain_device *sas_ex_discover_expander(
	struct domain_device *parent, int phy_id)
{
	struct sas_expander_device *parent_ex = rphy_to_expander_device(parent->rphy);
	struct ex_phy *phy = &parent->ex_dev.ex_phy[phy_id];
	struct domain_device *child = NULL;
	struct sas_rphy *rphy;
	struct sas_expander_device *edev;
	struct asd_sas_port *port;
	int res;

	if (phy->routing_attr == DIRECT_ROUTING) {
		SAS_DPRINTK("ex %016llx:0x%x:D <--> ex %016llx:0x%x is not "
			    "allowed\n",
			    SAS_ADDR(parent->sas_addr), phy_id,
			    SAS_ADDR(phy->attached_sas_addr),
			    phy->attached_phy_id);
		return NULL;
	}
939
	child = sas_alloc_device();
James Bottomley's avatar
James Bottomley committed
940
941
942
943
944
945
946
947
948
	if (!child)
		return NULL;

	phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
	/* FIXME: better error handling */
	BUG_ON(sas_port_add(phy->port) != 0);


	switch (phy->attached_dev_type) {
949
	case SAS_EDGE_EXPANDER_DEVICE:
James Bottomley's avatar
James Bottomley committed
950
951
952
		rphy = sas_expander_alloc(phy->port,
					  SAS_EDGE_EXPANDER_DEVICE);
		break;
953
	case SAS_FANOUT_EXPANDER_DEVICE:
James Bottomley's avatar
James Bottomley committed
954
955
956
957
958
959
960
961
962
		rphy = sas_expander_alloc(phy->port,
					  SAS_FANOUT_EXPANDER_DEVICE);
		break;
	default:
		rphy = NULL;	/* shut gcc up */
		BUG();
	}
	port = parent->port;
	child->rphy = rphy;
963
	get_device(&rphy->dev);
James Bottomley's avatar
James Bottomley committed
964
965
	edev = rphy_to_expander_device(rphy);
	child->dev_type = phy->attached_dev_type;
966
	kref_get(&parent->kref);
James Bottomley's avatar
James Bottomley committed
967
968
969
970
971
972
973
974
975
976
977
978
979
980
	child->parent = parent;
	child->port = port;
	child->iproto = phy->attached_iproto;
	child->tproto = phy->attached_tproto;
	memcpy(child->sas_addr, phy->attached_sas_addr, SAS_ADDR_SIZE);
	sas_hash_addr(child->hashed_sas_addr, child->sas_addr);
	sas_ex_get_linkrate(parent, child, phy);
	edev->level = parent_ex->level + 1;
	parent->port->disc.max_level = max(parent->port->disc.max_level,
					   edev->level);
	sas_init_dev(child);
	sas_fill_in_rphy(child, rphy);
	sas_rphy_add(rphy);

981
	spin_lock_irq(&parent->port->dev_list_lock);
James Bottomley's avatar
James Bottomley committed
982
	list_add_tail(&child->dev_list_node, &parent->port->dev_list);
983
	spin_unlock_irq(&parent->port->dev_list_lock);
James Bottomley's avatar
James Bottomley committed
984
985
986

	res = sas_discover_expander(child);
	if (res) {
987
		sas_rphy_delete(rphy);
988
989
990
		spin_lock_irq(&parent->port->dev_list_lock);
		list_del(&child->dev_list_node);
		spin_unlock_irq(&parent->port->dev_list_lock);
991
		sas_put_device(child);
992
993
		sas_port_delete(phy->port);
		phy->port = NULL;
James Bottomley's avatar
James Bottomley committed
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
		return NULL;
	}
	list_add_tail(&child->siblings, &parent->ex_dev.children);
	return child;
}

static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
{
	struct expander_device *ex = &dev->ex_dev;
	struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
	struct domain_device *child = NULL;
	int res = 0;

	/* Phy state */
1008
	if (ex_phy->linkrate == SAS_SATA_SPINUP_HOLD) {
1009
		if (!sas_smp_phy_control(dev, phy_id, PHY_FUNC_LINK_RESET, NULL))
James Bottomley's avatar
James Bottomley committed
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
			res = sas_ex_phy_discover(dev, phy_id);
		if (res)
			return res;
	}

	/* Parent and domain coherency */
	if (!dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
			     SAS_ADDR(dev->port->sas_addr))) {
		sas_add_parent_port(dev, phy_id);
		return 0;
	}
	if (dev->parent && (SAS_ADDR(ex_phy->attached_sas_addr) ==
			    SAS_ADDR(dev->parent->sas_addr))) {
		sas_add_parent_port(dev, phy_id);
		if (ex_phy->routing_attr == TABLE_ROUTING)
			sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
		return 0;
	}

	if (sas_dev_present_in_domain(dev->port, ex_phy->attached_sas_addr))
		sas_ex_disable_port(dev, ex_phy->attached_sas_addr);

1032
	if (ex_phy->attached_dev_type == SAS_PHY_UNUSED) {
James Bottomley's avatar
James Bottomley committed
1033
1034
1035
1036
1037
		if (ex_phy->routing_attr == DIRECT_ROUTING) {
			memset(ex_phy->attached_sas_addr, 0, SAS_ADDR_SIZE);
			sas_configure_routing(dev, ex_phy->attached_sas_addr);
		}
		return 0;
1038
	} else if (ex_phy->linkrate == SAS_LINK_RATE_UNKNOWN)
James Bottomley's avatar
James Bottomley committed
1039
1040
		return 0;

1041
1042
1043
1044
	if (ex_phy->attached_dev_type != SAS_END_DEVICE &&
	    ex_phy->attached_dev_type != SAS_FANOUT_EXPANDER_DEVICE &&
	    ex_phy->attached_dev_type != SAS_EDGE_EXPANDER_DEVICE &&
	    ex_phy->attached_dev_type != SAS_SATA_PENDING) {
James Bottomley's avatar
James Bottomley committed
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
		SAS_DPRINTK("unknown device type(0x%x) attached to ex %016llx "
			    "phy 0x%x\n", ex_phy->attached_dev_type,
			    SAS_ADDR(dev->sas_addr),
			    phy_id);
		return 0;
	}

	res = sas_configure_routing(dev, ex_phy->attached_sas_addr);
	if (res) {
		SAS_DPRINTK("configure routing for dev %016llx "
			    "reported 0x%x. Forgotten\n",
			    SAS_ADDR(ex_phy->attached_sas_addr), res);
		sas_disable_routing(dev, ex_phy->attached_sas_addr);
		return res;
	}

1061
	if (sas_ex_join_wide_port(dev, phy_id)) {
1062
1063
1064
1065
1066
		SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
			    phy_id, SAS_ADDR(ex_phy->attached_sas_addr));
		return res;
	}

James Bottomley's avatar
James Bottomley committed
1067
	switch (ex_phy->attached_dev_type) {
1068
1069
	case SAS_END_DEVICE:
	case SAS_SATA_PENDING:
James Bottomley's avatar
James Bottomley committed
1070
1071
		child = sas_ex_discover_end_dev(dev, phy_id);
		break;
1072
	case SAS_FANOUT_EXPANDER_DEVICE:
James Bottomley's avatar
James Bottomley committed
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
		if (SAS_ADDR(dev->port->disc.fanout_sas_addr)) {
			SAS_DPRINTK("second fanout expander %016llx phy 0x%x "
				    "attached to ex %016llx phy 0x%x\n",
				    SAS_ADDR(ex_phy->attached_sas_addr),
				    ex_phy->attached_phy_id,
				    SAS_ADDR(dev->sas_addr),
				    phy_id);
			sas_ex_disable_phy(dev, phy_id);
			break;
		} else
			memcpy(dev->port->disc.fanout_sas_addr,
			       ex_phy->attached_sas_addr, SAS_ADDR_SIZE);
		/* fallthrough */
1086
	case SAS_EDGE_EXPANDER_DEVICE:
James Bottomley's avatar
James Bottomley committed
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
		child = sas_ex_discover_expander(dev, phy_id);
		break;
	default:
		break;
	}

	if (child) {
		int i;

		for (i = 0; i < ex->num_phys; i++) {
			if (ex->ex_phy[i].phy_state == PHY_VACANT ||
			    ex->ex_phy[i].phy_state == PHY_NOT_PRESENT)
				continue;
1100
1101
1102
1103
			/*
			 * Due to races, the phy might not get added to the
			 * wide port, so we add the phy to the wide port here.
			 */
James Bottomley's avatar
James Bottomley committed
1104
			if (SAS_ADDR(ex->ex_phy[i].attached_sas_addr) ==
1105
			    SAS_ADDR(child->sas_addr)) {
James Bottomley's avatar
James Bottomley committed
1106
				ex->ex_phy[i].phy_state= PHY_DEVICE_DISCOVERED;
1107
				if (sas_ex_join_wide_port(dev, i))
1108
1109
1110
1111
					SAS_DPRINTK("Attaching ex phy%d to wide port %016llx\n",
						    i, SAS_ADDR(ex->ex_phy[i].attached_sas_addr));

			}
James Bottomley's avatar
James Bottomley committed
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
		}
	}

	return res;
}

static int sas_find_sub_addr(struct domain_device *dev, u8 *sub_addr)
{
	struct expander_device *ex = &dev->ex_dev;
	int i;

	for (i = 0; i < ex->num_phys; i++) {
		struct ex_phy *phy = &ex->ex_phy[i];

		if (phy->phy_state == PHY_VACANT ||
		    phy->phy_state == PHY_NOT_PRESENT)
			continue;

1130
1131
		if ((phy->attached_dev_type == SAS_EDGE_EXPANDER_DEVICE ||
		     phy->attached_dev_type == SAS_FANOUT_EXPANDER_DEVICE) &&
James Bottomley's avatar
James Bottomley committed
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
		    phy->routing_attr == SUBTRACTIVE_ROUTING) {

			memcpy(sub_addr, phy->attached_sas_addr,SAS_ADDR_SIZE);

			return 1;
		}
	}
	return 0;
}

static int sas_check_level_subtractive_boundary(struct domain_device *dev)
{
	struct expander_device *ex = &dev->ex_dev;
	struct domain_device *child;
	u8 sub_addr[8] = {0, };

	list_for_each_entry(child, &ex->children, siblings) {
1149
1150
		if (child->dev_type != SAS_EDGE_EXPANDER_DEVICE &&
		    child->dev_type != SAS_FANOUT_EXPANDER_DEVICE)
James Bottomley's avatar
James Bottomley committed
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
			continue;
		if (sub_addr[0] == 0) {
			sas_find_sub_addr(child, sub_addr);
			continue;
		} else {
			u8 s2[8];

			if (sas_find_sub_addr(child, s2) &&
			    (SAS_ADDR(sub_addr) != SAS_ADDR(s2))) {

				SAS_DPRINTK("ex %016llx->%016llx-?->%016llx "
					    "diverges from subtractive "
					    "boundary %016llx\n",
					    SAS_ADDR(dev->sas_addr),
					    SAS_ADDR(child->sas_addr),
					    SAS_ADDR(s2),
					    SAS_ADDR(sub_addr));

				sas_ex_disable_port(child, s2);
			}
		}
	}
	return 0;
}
/**
1176
1177
1178
 * sas_ex_discover_devices - discover devices attached to this expander
 * @dev: pointer to the expander domain device
 * @single: if you want to do a single phy, else set to -1;
James Bottomley's avatar
James Bottomley committed
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
 *
 * Configure this expander for use with its devices and register the
 * devices of this expander.
 */
static int sas_ex_discover_devices(struct domain_device *dev, int single)
{
	struct expander_device *ex = &dev->ex_dev;
	int i = 0, end = ex->num_phys;
	int res = 0;

	if (0 <= single && single < end) {
		i = single;
		end = i+1;
	}

	for ( ; i < end; i++) {
		struct ex_phy *ex_phy = &ex->ex_phy[i];

		if (ex_phy->phy_state == PHY_VACANT ||
		    ex_phy->phy_state == PHY_NOT_PRESENT ||
		    ex_phy->phy_state == PHY_DEVICE_DISCOVERED)
			continue;

		switch (ex_phy->linkrate) {
1203
1204
1205
		case SAS_PHY_DISABLED:
		case SAS_PHY_RESET_PROBLEM:
		case SAS_SATA_PORT_SELECTOR:
James Bottomley's avatar
James Bottomley committed
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
			continue;
		default:
			res = sas_ex_discover_dev(dev, i);
			if (res)
				break;
			continue;
		}
	}

	if (!res)
		sas_check_level_subtractive_boundary(dev);

	return res;
}

static int sas_check_ex_subtractive_boundary(struct domain_device *dev)
{
	struct expander_device *ex = &dev->ex_dev;
	int i;
	u8  *sub_sas_addr = NULL;

1227
	if (dev->dev_type != SAS_EDGE_EXPANDER_DEVICE)
James Bottomley's avatar
James Bottomley committed
1228
1229
1230
1231
1232
1233
1234
1235
1236
		return 0;

	for (i = 0; i < ex->num_phys; i++) {
		struct ex_phy *phy = &ex->ex_phy[i];

		if (phy->phy_state == PHY_VACANT ||
		    phy->phy_state == PHY_NOT_PRESENT)
			continue;

1237
1238
		if ((phy->attached_dev_type == SAS_FANOUT_EXPANDER_DEVICE ||
		     phy->attached_dev_type == SAS_EDGE_EXPANDER_DEVICE) &&
James Bottomley's avatar
James Bottomley committed
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
		    phy->routing_attr == SUBTRACTIVE_ROUTING) {

			if (!sub_sas_addr)
				sub_sas_addr = &phy->attached_sas_addr[0];
			else if (SAS_ADDR(sub_sas_addr) !=
				 SAS_ADDR(phy->attached_sas_addr)) {

				SAS_DPRINTK("ex %016llx phy 0x%x "
					    "diverges(%016llx) on subtractive "
					    "boundary(%016llx). Disabled\n",
					    SAS_ADDR(dev->sas_addr), i,
					    SAS_ADDR(phy->attached_sas_addr),
					    SAS_ADDR(sub_sas_addr));
				sas_ex_disable_phy(dev, i);
			}
		}
	}
	return 0;
}

static void sas_print_parent_topology_bug(struct domain_device *child,
						 struct ex_phy *parent_phy,
						 struct ex_phy *child_phy)
{
	static const char *ex_type[] = {
1264
1265
		[SAS_EDGE_EXPANDER_DEVICE] = "edge",
		[SAS_FANOUT_EXPANDER_DEVICE] = "fanout",
James Bottomley's avatar
James Bottomley committed
1266
1267
1268
	};
	struct domain_device *parent = child->parent;

1269
1270
	sas_printk("%s ex %016llx phy 0x%x <--> %s ex %016llx "
		   "phy 0x%x has %c:%c routing link!\n",
James Bottomley's avatar
James Bottomley committed
1271
1272
1273
1274
1275
1276
1277
1278
1279

		   ex_type[parent->dev_type],
		   SAS_ADDR(parent->sas_addr),
		   parent_phy->phy_id,

		   ex_type[child->dev_type],
		   SAS_ADDR(child->sas_addr),
		   child_phy->phy_id,

1280
1281
		   sas_route_char(parent, parent_phy),
		   sas_route_char(child, child_phy));
James Bottomley's avatar
James Bottomley committed
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
}

static int sas_check_eeds(struct domain_device *child,
				 struct ex_phy *parent_phy,
				 struct ex_phy *child_phy)
{
	int res = 0;
	struct domain_device *parent = child->parent;

	if (SAS_ADDR(parent->port->disc.fanout_sas_addr) != 0) {
		res = -ENODEV;
		SAS_DPRINTK("edge ex %016llx phy S:0x%x <--> edge ex %016llx "
			    "phy S:0x%x, while there is a fanout ex %016llx\n",
			    SAS_ADDR(parent->sas_addr),
			    parent_phy->phy_id,
			    SAS_ADDR(child->sas_addr),
			    child_phy->phy_id,
			    SAS_ADDR(parent->port->disc.fanout_sas_addr));
	} else if (SAS_ADDR(parent->port->disc.eeds_a) == 0) {
		memcpy(parent->port->disc.eeds_a, parent->sas_addr,
		       SAS_ADDR_SIZE);
		memcpy(parent->port->disc.eeds_b, child->sas_addr,
		       SAS_ADDR_SIZE);
	} else if (((SAS_ADDR(parent->port->disc.eeds_a) ==
		    SAS_ADDR(parent->sas_addr)) ||
		   (SAS_ADDR(parent->port->disc.eeds_a) ==
		    SAS_ADDR(child->sas_addr)))
		   &&
		   ((SAS_ADDR(parent->port->disc.eeds_b) ==
		     SAS_ADDR(parent->sas_addr)) ||
		    (SAS_ADDR(parent->port->disc.eeds_b) ==
		     SAS_ADDR(child->sas_addr))))
		;
	else {
		res = -ENODEV;
		SAS_DPRINTK("edge ex %016llx phy 0x%x <--> edge ex %016llx "
			    "phy 0x%x link forms a third EEDS!\n",
			    SAS_ADDR(parent->sas_addr),
			    parent_phy->phy_id,
			    SAS_ADDR(child->sas_addr),
			    child_phy->phy_id);
	}

	return res;
}

/* Here we spill over 80 columns.  It is intentional.
 */
static int sas_check_parent_topology(struct domain_device *child)
{
	struct expander_device *child_ex = &child->ex_dev;
	struct expander_device *parent_ex;
	int i;
	int res = 0;

	if (!child->parent)
		return 0;

1340
1341
	if (child->parent->dev_type != SAS_EDGE_EXPANDER_DEVICE &&
	    child->parent->dev_type != SAS_FANOUT_EXPANDER_DEVICE)
James Bottomley's avatar
James Bottomley committed
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
		return 0;

	parent_ex = &child->parent->ex_dev;

	for (i = 0; i < parent_ex->num_phys; i++) {
		struct ex_phy *parent_phy = &parent_ex->ex_phy[i];
		struct ex_phy *child_phy;

		if (parent_phy->phy_state == PHY_VACANT ||
		    parent_phy->phy_state == PHY_NOT_PRESENT)
			continue;

		if (SAS_ADDR(parent_phy->attached_sas_addr) != SAS_ADDR(child->sas_addr))
			continue;

		child_phy = &child_ex->ex_phy[parent_phy->attached_phy_id];

		switch (child->parent->dev_type) {
1360
1361
		case SAS_EDGE_EXPANDER_DEVICE:
			if (child->dev_type == SAS_FANOUT_EXPANDER_DEVICE) {
James Bottomley's avatar
James Bottomley committed
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
				if (parent_phy->routing_attr != SUBTRACTIVE_ROUTING ||
				    child_phy->routing_attr != TABLE_ROUTING) {
					sas_print_parent_topology_bug(child, parent_phy, child_phy);
					res = -ENODEV;
				}
			} else if (parent_phy->routing_attr == SUBTRACTIVE_ROUTING) {
				if (child_phy->routing_attr == SUBTRACTIVE_ROUTING) {
					res = sas_check_eeds(child, parent_phy, child_phy);
				} else if (child_phy->routing_attr != TABLE_ROUTING) {
					sas_print_parent_topology_bug(child, parent_phy, child_phy);
					res = -ENODEV;
				}
1374
1375
1376
1377
1378
1379
1380
1381
1382
			} else if (parent_phy->routing_attr == TABLE_ROUTING) {
				if (child_phy->routing_attr == SUBTRACTIVE_ROUTING ||
				    (child_phy->routing_attr == TABLE_ROUTING &&
				     child_ex->t2t_supp && parent_ex->t2t_supp)) {
					/* All good */;
				} else {
					sas_print_parent_topology_bug(child, parent_phy, child_phy);
					res = -ENODEV;
				}
James Bottomley's avatar
James Bottomley committed
1383
1384
			}
			break;
1385
		case SAS_FANOUT_EXPANDER_DEVICE:
James Bottomley's avatar
James Bottomley committed
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
			if (parent_phy->routing_attr != TABLE_ROUTING ||
			    child_phy->routing_attr != SUBTRACTIVE_ROUTING) {
				sas_print_parent_topology_bug(child, parent_phy, child_phy);
				res = -ENODEV;
			}
			break;
		default:
			break;
		}
	}

	return res;
}

#define RRI_REQ_SIZE  16
#define RRI_RESP_SIZE 44

static int sas_configure_present(struct domain_device *dev, int phy_id,
				 u8 *sas_addr, int *index, int *present)
{
	int i, res = 0;
	struct expander_device *ex = &dev->ex_dev;
	struct ex_phy *phy = &ex->ex_phy[phy_id];
	u8 *rri_req;
	u8 *rri_resp;

	*present = 0;
	*index = 0;

	rri_req = alloc_smp_req(RRI_REQ_SIZE);
	if (!rri_req)
		return -ENOMEM;

	rri_resp = alloc_smp_resp(RRI_RESP_SIZE);
	if (!rri_resp) {
		kfree(rri_req);
		return -ENOMEM;
	}

	rri_req[1] = SMP_REPORT_ROUTE_INFO;
	rri_req[9] = phy_id;

	for (i = 0; i < ex->max_route_indexes ; i++) {
		*(__be16 *)(rri_req+6) = cpu_to_be16(i);
		res = smp_execute_task(dev, rri_req, RRI_REQ_SIZE, rri_resp,
				       RRI_RESP_SIZE);
		if (res)
			goto out;
		res = rri_resp[2];
		if (res == SMP_RESP_NO_INDEX) {
			SAS_DPRINTK("overflow of indexes: dev %016llx "
				    "phy 0x%x index 0x%x\n",
				    SAS_ADDR(dev->sas_addr), phy_id, i);
			goto out;
		} else if (res != SMP_RESP_FUNC_ACC) {
			SAS_DPRINTK("%s: dev %016llx phy 0x%x index 0x%x "
1442
				    "result 0x%x\n", __func__,
James Bottomley's avatar
James Bottomley committed
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
				    SAS_ADDR(dev->sas_addr), phy_id, i, res);
			goto out;
		}
		if (SAS_ADDR(sas_addr) != 0) {
			if (SAS_ADDR(rri_resp+16) == SAS_ADDR(sas_addr)) {
				*index = i;
				if ((rri_resp[12] & 0x80) == 0x80)
					*present = 0;
				else
					*present = 1;
				goto out;
			} else if (SAS_ADDR(rri_resp+16) == 0) {
				*index = i;
				*present = 0;
				goto out;
			}
		} else if (SAS_ADDR(rri_resp+16) == 0 &&
			   phy->last_da_index < i) {
			phy->last_da_index = i;
			*index = i;
			*present = 0;
			goto out;
		}
	}
	res = -1;
out:
	kfree(rri_req);
	kfree(rri_resp);
	return res;
}

#define CRI_REQ_SIZE  44
#define CRI_RESP_SIZE  8

static int sas_configure_set(struct domain_device *dev, int phy_id,
			     u8 *sas_addr, int index, int include)
{
	int res;
	u8 *cri_req;
	u8 *cri_resp;

	cri_req = alloc_smp_req(CRI_REQ_SIZE);
	if (!cri_req)
		return -ENOMEM;

	cri_resp = alloc_smp_resp(CRI_RESP_SIZE);
	if (!cri_resp) {
		kfree(cri_req);
		return -ENOMEM;
	}

	cri_req[1] = SMP_CONF_ROUTE_INFO;
	*(__be16 *)(cri_req+6) = cpu_to_be16(index);
	cri_req[9] = phy_id;
	if (SAS_ADDR(sas_addr) == 0 || !include)
		cri_req[12] |= 0x80;
	memcpy(cri_req+16, sas_addr, SAS_ADDR_SIZE);

	res = smp_execute_task(dev, cri_req, CRI_REQ_SIZE, cri_resp,
			       CRI_RESP_SIZE);
	if (res)
		goto out;
	res = cri_resp[2];
	if (res == SMP_RESP_NO_INDEX) {
		SAS_DPRINTK("overflow of indexes: dev %016llx phy 0x%x "
			    "index 0x%x\n",
			    SAS_ADDR(dev->sas_addr), phy_id, index);
	}
out:
	kfree(cri_req);
	kfree(cri_resp);
	return res;
}

static int sas_configure_phy(struct domain_device *dev, int phy_id,
				    u8 *sas_addr, int include)
{
	int index;
	int present;
	int res;

	res = sas_configure_present(dev, phy_id, sas_addr, &index, &present);
	if (res)
		return res;
	if (include ^ present)
		return sas_configure_set(dev, phy_id, sas_addr, index,include);

	return res;
}

/**
1534
1535
1536
1537
1538
 * sas_configure_parent - configure routing table of parent
 * @parent: parent expander
 * @child: child expander
 * @sas_addr: SAS port identifier of device directly attached to child
 * @include: whether or not to include @child in the expander routing table
James Bottomley's avatar
James Bottomley committed
1539
1540
1541
1542
1543
1544