[GFS2] Fix up merge of Linus' kernel into GFS2
[pandora-kernel.git] / drivers / scsi / libsas / sas_phy.c
1 /*
2  * Serial Attached SCSI (SAS) Phy class
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 "sas_internal.h"
26 #include <scsi/scsi_host.h>
27 #include <scsi/scsi_transport.h>
28 #include <scsi/scsi_transport_sas.h>
29 #include "../scsi_sas_internal.h"
30
31 /* ---------- Phy events ---------- */
32
33 static void sas_phye_loss_of_signal(void *data)
34 {
35         struct asd_sas_phy *phy = data;
36
37         sas_begin_event(PHYE_LOSS_OF_SIGNAL, &phy->ha->event_lock,
38                         &phy->phy_events_pending);
39         phy->error = 0;
40         sas_deform_port(phy);
41 }
42
43 static void sas_phye_oob_done(void *data)
44 {
45         struct asd_sas_phy *phy = data;
46
47         sas_begin_event(PHYE_OOB_DONE, &phy->ha->event_lock,
48                         &phy->phy_events_pending);
49         phy->error = 0;
50 }
51
52 static void sas_phye_oob_error(void *data)
53 {
54         struct asd_sas_phy *phy = data;
55         struct sas_ha_struct *sas_ha = phy->ha;
56         struct asd_sas_port *port = phy->port;
57         struct sas_internal *i =
58                 to_sas_internal(sas_ha->core.shost->transportt);
59
60         sas_begin_event(PHYE_OOB_ERROR, &phy->ha->event_lock,
61                         &phy->phy_events_pending);
62
63         sas_deform_port(phy);
64
65         if (!port && phy->enabled && i->dft->lldd_control_phy) {
66                 phy->error++;
67                 switch (phy->error) {
68                 case 1:
69                 case 2:
70                         i->dft->lldd_control_phy(phy, PHY_FUNC_HARD_RESET,
71                                                  NULL);
72                         break;
73                 case 3:
74                 default:
75                         phy->error = 0;
76                         phy->enabled = 0;
77                         i->dft->lldd_control_phy(phy, PHY_FUNC_DISABLE, NULL);
78                         break;
79                 }
80         }
81 }
82
83 static void sas_phye_spinup_hold(void *data)
84 {
85         struct asd_sas_phy *phy = data;
86         struct sas_ha_struct *sas_ha = phy->ha;
87         struct sas_internal *i =
88                 to_sas_internal(sas_ha->core.shost->transportt);
89
90         sas_begin_event(PHYE_SPINUP_HOLD, &phy->ha->event_lock,
91                         &phy->phy_events_pending);
92
93         phy->error = 0;
94         i->dft->lldd_control_phy(phy, PHY_FUNC_RELEASE_SPINUP_HOLD, NULL);
95 }
96
97 /* ---------- Phy class registration ---------- */
98
99 int sas_register_phys(struct sas_ha_struct *sas_ha)
100 {
101         int i;
102
103         static void (*sas_phy_event_fns[PHY_NUM_EVENTS])(void *) = {
104                 [PHYE_LOSS_OF_SIGNAL] = sas_phye_loss_of_signal,
105                 [PHYE_OOB_DONE] = sas_phye_oob_done,
106                 [PHYE_OOB_ERROR] = sas_phye_oob_error,
107                 [PHYE_SPINUP_HOLD] = sas_phye_spinup_hold,
108         };
109
110         static void (*sas_port_event_fns[PORT_NUM_EVENTS])(void *) = {
111                 [PORTE_BYTES_DMAED] = sas_porte_bytes_dmaed,
112                 [PORTE_BROADCAST_RCVD] = sas_porte_broadcast_rcvd,
113                 [PORTE_LINK_RESET_ERR] = sas_porte_link_reset_err,
114                 [PORTE_TIMER_EVENT] = sas_porte_timer_event,
115                 [PORTE_HARD_RESET] = sas_porte_hard_reset,
116         };
117
118         /* Now register the phys. */
119         for (i = 0; i < sas_ha->num_phys; i++) {
120                 int k;
121                 struct asd_sas_phy *phy = sas_ha->sas_phy[i];
122
123                 phy->error = 0;
124                 INIT_LIST_HEAD(&phy->port_phy_el);
125                 for (k = 0; k < PORT_NUM_EVENTS; k++)
126                         INIT_WORK(&phy->port_events[k], sas_port_event_fns[k],
127                                   phy);
128
129                 for (k = 0; k < PHY_NUM_EVENTS; k++)
130                         INIT_WORK(&phy->phy_events[k], sas_phy_event_fns[k],
131                                   phy);
132                 phy->port = NULL;
133                 phy->ha = sas_ha;
134                 spin_lock_init(&phy->frame_rcvd_lock);
135                 spin_lock_init(&phy->sas_prim_lock);
136                 phy->frame_rcvd_size = 0;
137
138                 phy->phy = sas_phy_alloc(&sas_ha->core.shost->shost_gendev,
139                                          i);
140                 if (!phy->phy)
141                         return -ENOMEM;
142
143                 phy->phy->identify.initiator_port_protocols =
144                         phy->iproto;
145                 phy->phy->identify.target_port_protocols = phy->tproto;
146                 phy->phy->identify.sas_address = SAS_ADDR(sas_ha->sas_addr);
147                 phy->phy->identify.phy_identifier = i;
148                 phy->phy->minimum_linkrate_hw = SAS_LINK_RATE_UNKNOWN;
149                 phy->phy->maximum_linkrate_hw = SAS_LINK_RATE_UNKNOWN;
150                 phy->phy->minimum_linkrate = SAS_LINK_RATE_UNKNOWN;
151                 phy->phy->maximum_linkrate = SAS_LINK_RATE_UNKNOWN;
152                 phy->phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
153
154                 sas_phy_add(phy->phy);
155         }
156
157         return 0;
158 }