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