2 * linux/drivers/scsi/esas2r/esas2r_targdb.c
3 * For use with ATTO ExpressSAS R6xx SAS/SATA RAID controllers
5 * Copyright (c) 2001-2013 ATTO Technology, Inc.
6 * (mailto:linuxdrivers@attotech.com)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23 * solely responsible for determining the appropriateness of using and
24 * distributing the Program and assumes all risks associated with its
25 * exercise of rights under this Agreement, including but not limited to
26 * the risks and costs of program errors, damage to or loss of data,
27 * programs or equipment, and unavailability or interruption of operations.
29 * DISCLAIMER OF LIABILITY
30 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
46 void esas2r_targ_db_initialize(struct esas2r_adapter
*a
)
48 struct esas2r_target
*t
;
50 for (t
= a
->targetdb
; t
< a
->targetdb_end
; t
++) {
51 memset(t
, 0, sizeof(struct esas2r_target
));
53 t
->target_state
= TS_NOT_PRESENT
;
54 t
->buffered_target_state
= TS_NOT_PRESENT
;
55 t
->new_target_state
= TS_INVALID
;
59 void esas2r_targ_db_remove_all(struct esas2r_adapter
*a
, bool notify
)
61 struct esas2r_target
*t
;
64 for (t
= a
->targetdb
; t
< a
->targetdb_end
; t
++) {
65 if (t
->target_state
!= TS_PRESENT
)
68 spin_lock_irqsave(&a
->mem_lock
, flags
);
69 esas2r_targ_db_remove(a
, t
);
70 spin_unlock_irqrestore(&a
->mem_lock
, flags
);
73 esas2r_trace("remove id:%d", esas2r_targ_get_id(t
,
75 esas2r_target_state_changed(a
, esas2r_targ_get_id(t
,
82 void esas2r_targ_db_report_changes(struct esas2r_adapter
*a
)
84 struct esas2r_target
*t
;
89 if (test_bit(AF_DISC_PENDING
, &a
->flags
)) {
94 for (t
= a
->targetdb
; t
< a
->targetdb_end
; t
++) {
95 u8 state
= TS_INVALID
;
97 spin_lock_irqsave(&a
->mem_lock
, flags
);
98 if (t
->buffered_target_state
!= t
->target_state
)
99 state
= t
->buffered_target_state
= t
->target_state
;
101 spin_unlock_irqrestore(&a
->mem_lock
, flags
);
102 if (state
!= TS_INVALID
) {
103 esas2r_trace("targ_db_report_changes:%d",
107 esas2r_trace("state:%d", state
);
109 esas2r_target_state_changed(a
,
110 esas2r_targ_get_id(t
,
119 struct esas2r_target
*esas2r_targ_db_add_raid(struct esas2r_adapter
*a
,
120 struct esas2r_disc_context
*
123 struct esas2r_target
*t
;
125 esas2r_trace_enter();
127 if (dc
->curr_virt_id
>= ESAS2R_MAX_TARGETS
) {
133 t
= a
->targetdb
+ dc
->curr_virt_id
;
135 if (t
->target_state
== TS_PRESENT
) {
140 esas2r_hdebug("add RAID %s, T:%d", dc
->raid_grp_name
,
145 if (dc
->interleave
== 0
146 || dc
->block_size
== 0) {
147 /* these are invalid values, don't create the target entry. */
149 esas2r_hdebug("invalid RAID group dimensions");
156 t
->block_size
= dc
->block_size
;
157 t
->inter_byte
= dc
->interleave
;
158 t
->inter_block
= dc
->interleave
/ dc
->block_size
;
159 t
->virt_targ_id
= dc
->curr_virt_id
;
160 t
->phys_targ_id
= ESAS2R_TARG_ID_INV
;
162 t
->flags
&= ~TF_PASS_THRU
;
165 t
->identifier_len
= 0;
167 t
->target_state
= TS_PRESENT
;
172 struct esas2r_target
*esas2r_targ_db_add_pthru(struct esas2r_adapter
*a
,
173 struct esas2r_disc_context
*dc
,
177 struct esas2r_target
*t
;
179 esas2r_trace_enter();
181 if (dc
->curr_virt_id
>= ESAS2R_MAX_TARGETS
) {
187 /* see if we found this device before. */
189 t
= esas2r_targ_db_find_by_ident(a
, ident
, ident_len
);
192 t
= a
->targetdb
+ dc
->curr_virt_id
;
194 if (ident_len
> sizeof(t
->identifier
)
195 || t
->target_state
== TS_PRESENT
) {
201 esas2r_hdebug("add PT; T:%d, V:%d, P:%d", esas2r_targ_get_id(t
, a
),
208 t
->virt_targ_id
= dc
->curr_virt_id
;
209 t
->phys_targ_id
= dc
->curr_phys_id
;
210 t
->identifier_len
= ident_len
;
212 memcpy(t
->identifier
, ident
, ident_len
);
214 t
->flags
|= TF_PASS_THRU
| TF_USED
;
216 t
->target_state
= TS_PRESENT
;
221 void esas2r_targ_db_remove(struct esas2r_adapter
*a
, struct esas2r_target
*t
)
223 esas2r_trace_enter();
225 t
->target_state
= TS_NOT_PRESENT
;
227 esas2r_trace("remove id:%d", esas2r_targ_get_id(t
, a
));
232 struct esas2r_target
*esas2r_targ_db_find_by_sas_addr(struct esas2r_adapter
*a
,
235 struct esas2r_target
*t
;
237 for (t
= a
->targetdb
; t
< a
->targetdb_end
; t
++)
238 if (t
->sas_addr
== *sas_addr
)
244 struct esas2r_target
*esas2r_targ_db_find_by_ident(struct esas2r_adapter
*a
,
248 struct esas2r_target
*t
;
250 for (t
= a
->targetdb
; t
< a
->targetdb_end
; t
++) {
251 if (ident_len
== t
->identifier_len
252 && memcmp(&t
->identifier
[0], identifier
,
260 u16
esas2r_targ_db_find_next_present(struct esas2r_adapter
*a
, u16 target_id
)
262 u16 id
= target_id
+ 1;
264 while (id
< ESAS2R_MAX_TARGETS
) {
265 struct esas2r_target
*t
= a
->targetdb
+ id
;
267 if (t
->target_state
== TS_PRESENT
)
276 struct esas2r_target
*esas2r_targ_db_find_by_virt_id(struct esas2r_adapter
*a
,
279 struct esas2r_target
*t
;
281 for (t
= a
->targetdb
; t
< a
->targetdb_end
; t
++) {
282 if (t
->target_state
!= TS_PRESENT
)
285 if (t
->virt_targ_id
== virt_id
)
292 u16
esas2r_targ_db_get_tgt_cnt(struct esas2r_adapter
*a
)
295 struct esas2r_target
*t
;
298 spin_lock_irqsave(&a
->mem_lock
, flags
);
299 for (t
= a
->targetdb
; t
< a
->targetdb_end
; t
++)
300 if (t
->target_state
== TS_PRESENT
)
303 spin_unlock_irqrestore(&a
->mem_lock
, flags
);