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