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