18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci *  linux/drivers/scsi/esas2r/esas2r_targdb.c
38c2ecf20Sopenharmony_ci *      For use with ATTO ExpressSAS R6xx SAS/SATA RAID controllers
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (c) 2001-2013 ATTO Technology, Inc.
68c2ecf20Sopenharmony_ci *  (mailto:linuxdrivers@attotech.com)
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or
98c2ecf20Sopenharmony_ci * modify it under the terms of the GNU General Public License
108c2ecf20Sopenharmony_ci * as published by the Free Software Foundation; either version 2
118c2ecf20Sopenharmony_ci * of the License, or (at your option) any later version.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful,
148c2ecf20Sopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
158c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
168c2ecf20Sopenharmony_ci * GNU General Public License for more details.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * NO WARRANTY
198c2ecf20Sopenharmony_ci * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
208c2ecf20Sopenharmony_ci * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
218c2ecf20Sopenharmony_ci * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
228c2ecf20Sopenharmony_ci * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
238c2ecf20Sopenharmony_ci * solely responsible for determining the appropriateness of using and
248c2ecf20Sopenharmony_ci * distributing the Program and assumes all risks associated with its
258c2ecf20Sopenharmony_ci * exercise of rights under this Agreement, including but not limited to
268c2ecf20Sopenharmony_ci * the risks and costs of program errors, damage to or loss of data,
278c2ecf20Sopenharmony_ci * programs or equipment, and unavailability or interruption of operations.
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci * DISCLAIMER OF LIABILITY
308c2ecf20Sopenharmony_ci * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
318c2ecf20Sopenharmony_ci * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
328c2ecf20Sopenharmony_ci * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
338c2ecf20Sopenharmony_ci * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
348c2ecf20Sopenharmony_ci * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
358c2ecf20Sopenharmony_ci * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
368c2ecf20Sopenharmony_ci * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
378c2ecf20Sopenharmony_ci *
388c2ecf20Sopenharmony_ci * You should have received a copy of the GNU General Public License
398c2ecf20Sopenharmony_ci * along with this program; if not, write to the Free Software
408c2ecf20Sopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
418c2ecf20Sopenharmony_ci * USA.
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#include "esas2r.h"
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_civoid esas2r_targ_db_initialize(struct esas2r_adapter *a)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	struct esas2r_target *t;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	for (t = a->targetdb; t < a->targetdb_end; t++) {
518c2ecf20Sopenharmony_ci		memset(t, 0, sizeof(struct esas2r_target));
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci		t->target_state = TS_NOT_PRESENT;
548c2ecf20Sopenharmony_ci		t->buffered_target_state = TS_NOT_PRESENT;
558c2ecf20Sopenharmony_ci		t->new_target_state = TS_INVALID;
568c2ecf20Sopenharmony_ci	}
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_civoid esas2r_targ_db_remove_all(struct esas2r_adapter *a, bool notify)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	struct esas2r_target *t;
628c2ecf20Sopenharmony_ci	unsigned long flags;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	for (t = a->targetdb; t < a->targetdb_end; t++) {
658c2ecf20Sopenharmony_ci		if (t->target_state != TS_PRESENT)
668c2ecf20Sopenharmony_ci			continue;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci		spin_lock_irqsave(&a->mem_lock, flags);
698c2ecf20Sopenharmony_ci		esas2r_targ_db_remove(a, t);
708c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&a->mem_lock, flags);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci		if (notify) {
738c2ecf20Sopenharmony_ci			esas2r_trace("remove id:%d", esas2r_targ_get_id(t,
748c2ecf20Sopenharmony_ci									a));
758c2ecf20Sopenharmony_ci			esas2r_target_state_changed(a, esas2r_targ_get_id(t,
768c2ecf20Sopenharmony_ci									  a),
778c2ecf20Sopenharmony_ci						    TS_NOT_PRESENT);
788c2ecf20Sopenharmony_ci		}
798c2ecf20Sopenharmony_ci	}
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_civoid esas2r_targ_db_report_changes(struct esas2r_adapter *a)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	struct esas2r_target *t;
858c2ecf20Sopenharmony_ci	unsigned long flags;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	esas2r_trace_enter();
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	if (test_bit(AF_DISC_PENDING, &a->flags)) {
908c2ecf20Sopenharmony_ci		esas2r_trace_exit();
918c2ecf20Sopenharmony_ci		return;
928c2ecf20Sopenharmony_ci	}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	for (t = a->targetdb; t < a->targetdb_end; t++) {
958c2ecf20Sopenharmony_ci		u8 state = TS_INVALID;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci		spin_lock_irqsave(&a->mem_lock, flags);
988c2ecf20Sopenharmony_ci		if (t->buffered_target_state != t->target_state)
998c2ecf20Sopenharmony_ci			state = t->buffered_target_state = t->target_state;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&a->mem_lock, flags);
1028c2ecf20Sopenharmony_ci		if (state != TS_INVALID) {
1038c2ecf20Sopenharmony_ci			esas2r_trace("targ_db_report_changes:%d",
1048c2ecf20Sopenharmony_ci				     esas2r_targ_get_id(
1058c2ecf20Sopenharmony_ci					     t,
1068c2ecf20Sopenharmony_ci					     a));
1078c2ecf20Sopenharmony_ci			esas2r_trace("state:%d", state);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci			esas2r_target_state_changed(a,
1108c2ecf20Sopenharmony_ci						    esas2r_targ_get_id(t,
1118c2ecf20Sopenharmony_ci								       a),
1128c2ecf20Sopenharmony_ci						    state);
1138c2ecf20Sopenharmony_ci		}
1148c2ecf20Sopenharmony_ci	}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	esas2r_trace_exit();
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistruct esas2r_target *esas2r_targ_db_add_raid(struct esas2r_adapter *a,
1208c2ecf20Sopenharmony_ci					      struct esas2r_disc_context *
1218c2ecf20Sopenharmony_ci					      dc)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	struct esas2r_target *t;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	esas2r_trace_enter();
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	if (dc->curr_virt_id >= ESAS2R_MAX_TARGETS) {
1288c2ecf20Sopenharmony_ci		esas2r_bugon();
1298c2ecf20Sopenharmony_ci		esas2r_trace_exit();
1308c2ecf20Sopenharmony_ci		return NULL;
1318c2ecf20Sopenharmony_ci	}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	t = a->targetdb + dc->curr_virt_id;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	if (t->target_state == TS_PRESENT) {
1368c2ecf20Sopenharmony_ci		esas2r_trace_exit();
1378c2ecf20Sopenharmony_ci		return NULL;
1388c2ecf20Sopenharmony_ci	}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	esas2r_hdebug("add RAID %s, T:%d", dc->raid_grp_name,
1418c2ecf20Sopenharmony_ci		      esas2r_targ_get_id(
1428c2ecf20Sopenharmony_ci			      t,
1438c2ecf20Sopenharmony_ci			      a));
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	if (dc->interleave == 0
1468c2ecf20Sopenharmony_ci	    || dc->block_size  == 0) {
1478c2ecf20Sopenharmony_ci		/* these are invalid values, don't create the target entry. */
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci		esas2r_hdebug("invalid RAID group dimensions");
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci		esas2r_trace_exit();
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci		return NULL;
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	t->block_size = dc->block_size;
1578c2ecf20Sopenharmony_ci	t->inter_byte = dc->interleave;
1588c2ecf20Sopenharmony_ci	t->inter_block = dc->interleave / dc->block_size;
1598c2ecf20Sopenharmony_ci	t->virt_targ_id = dc->curr_virt_id;
1608c2ecf20Sopenharmony_ci	t->phys_targ_id = ESAS2R_TARG_ID_INV;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	t->flags &= ~TF_PASS_THRU;
1638c2ecf20Sopenharmony_ci	t->flags |= TF_USED;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	t->identifier_len = 0;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	t->target_state = TS_PRESENT;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	return t;
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistruct esas2r_target *esas2r_targ_db_add_pthru(struct esas2r_adapter *a,
1738c2ecf20Sopenharmony_ci					       struct esas2r_disc_context *dc,
1748c2ecf20Sopenharmony_ci					       u8 *ident,
1758c2ecf20Sopenharmony_ci					       u8 ident_len)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	struct esas2r_target *t;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	esas2r_trace_enter();
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	if (dc->curr_virt_id >= ESAS2R_MAX_TARGETS) {
1828c2ecf20Sopenharmony_ci		esas2r_bugon();
1838c2ecf20Sopenharmony_ci		esas2r_trace_exit();
1848c2ecf20Sopenharmony_ci		return NULL;
1858c2ecf20Sopenharmony_ci	}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	/* see if we found this device before. */
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	t = esas2r_targ_db_find_by_ident(a, ident, ident_len);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	if (t == NULL) {
1928c2ecf20Sopenharmony_ci		t = a->targetdb + dc->curr_virt_id;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci		if (ident_len > sizeof(t->identifier)
1958c2ecf20Sopenharmony_ci		    || t->target_state == TS_PRESENT) {
1968c2ecf20Sopenharmony_ci			esas2r_trace_exit();
1978c2ecf20Sopenharmony_ci			return NULL;
1988c2ecf20Sopenharmony_ci		}
1998c2ecf20Sopenharmony_ci	}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	esas2r_hdebug("add PT; T:%d, V:%d, P:%d", esas2r_targ_get_id(t, a),
2028c2ecf20Sopenharmony_ci		      dc->curr_virt_id,
2038c2ecf20Sopenharmony_ci		      dc->curr_phys_id);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	t->block_size = 0;
2068c2ecf20Sopenharmony_ci	t->inter_byte = 0;
2078c2ecf20Sopenharmony_ci	t->inter_block = 0;
2088c2ecf20Sopenharmony_ci	t->virt_targ_id = dc->curr_virt_id;
2098c2ecf20Sopenharmony_ci	t->phys_targ_id = dc->curr_phys_id;
2108c2ecf20Sopenharmony_ci	t->identifier_len = ident_len;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	memcpy(t->identifier, ident, ident_len);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	t->flags |= TF_PASS_THRU | TF_USED;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	t->target_state = TS_PRESENT;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	return t;
2198c2ecf20Sopenharmony_ci}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_civoid esas2r_targ_db_remove(struct esas2r_adapter *a, struct esas2r_target *t)
2228c2ecf20Sopenharmony_ci{
2238c2ecf20Sopenharmony_ci	esas2r_trace_enter();
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	t->target_state = TS_NOT_PRESENT;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	esas2r_trace("remove id:%d", esas2r_targ_get_id(t, a));
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	esas2r_trace_exit();
2308c2ecf20Sopenharmony_ci}
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_cistruct esas2r_target *esas2r_targ_db_find_by_sas_addr(struct esas2r_adapter *a,
2338c2ecf20Sopenharmony_ci						      u64 *sas_addr)
2348c2ecf20Sopenharmony_ci{
2358c2ecf20Sopenharmony_ci	struct esas2r_target *t;
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	for (t = a->targetdb; t < a->targetdb_end; t++)
2388c2ecf20Sopenharmony_ci		if (t->sas_addr == *sas_addr)
2398c2ecf20Sopenharmony_ci			return t;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	return NULL;
2428c2ecf20Sopenharmony_ci}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistruct esas2r_target *esas2r_targ_db_find_by_ident(struct esas2r_adapter *a,
2458c2ecf20Sopenharmony_ci						   void *identifier,
2468c2ecf20Sopenharmony_ci						   u8 ident_len)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	struct esas2r_target *t;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	for (t = a->targetdb; t < a->targetdb_end; t++) {
2518c2ecf20Sopenharmony_ci		if (ident_len == t->identifier_len
2528c2ecf20Sopenharmony_ci		    && memcmp(&t->identifier[0], identifier,
2538c2ecf20Sopenharmony_ci			      ident_len) == 0)
2548c2ecf20Sopenharmony_ci			return t;
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	return NULL;
2588c2ecf20Sopenharmony_ci}
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ciu16 esas2r_targ_db_find_next_present(struct esas2r_adapter *a, u16 target_id)
2618c2ecf20Sopenharmony_ci{
2628c2ecf20Sopenharmony_ci	u16 id = target_id + 1;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	while (id < ESAS2R_MAX_TARGETS) {
2658c2ecf20Sopenharmony_ci		struct esas2r_target *t = a->targetdb + id;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci		if (t->target_state == TS_PRESENT)
2688c2ecf20Sopenharmony_ci			break;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci		id++;
2718c2ecf20Sopenharmony_ci	}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	return id;
2748c2ecf20Sopenharmony_ci}
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_cistruct esas2r_target *esas2r_targ_db_find_by_virt_id(struct esas2r_adapter *a,
2778c2ecf20Sopenharmony_ci						     u16 virt_id)
2788c2ecf20Sopenharmony_ci{
2798c2ecf20Sopenharmony_ci	struct esas2r_target *t;
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	for (t = a->targetdb; t < a->targetdb_end; t++) {
2828c2ecf20Sopenharmony_ci		if (t->target_state != TS_PRESENT)
2838c2ecf20Sopenharmony_ci			continue;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci		if (t->virt_targ_id == virt_id)
2868c2ecf20Sopenharmony_ci			return t;
2878c2ecf20Sopenharmony_ci	}
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	return NULL;
2908c2ecf20Sopenharmony_ci}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ciu16 esas2r_targ_db_get_tgt_cnt(struct esas2r_adapter *a)
2938c2ecf20Sopenharmony_ci{
2948c2ecf20Sopenharmony_ci	u16 devcnt = 0;
2958c2ecf20Sopenharmony_ci	struct esas2r_target *t;
2968c2ecf20Sopenharmony_ci	unsigned long flags;
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	spin_lock_irqsave(&a->mem_lock, flags);
2998c2ecf20Sopenharmony_ci	for (t = a->targetdb; t < a->targetdb_end; t++)
3008c2ecf20Sopenharmony_ci		if (t->target_state == TS_PRESENT)
3018c2ecf20Sopenharmony_ci			devcnt++;
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&a->mem_lock, flags);
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	return devcnt;
3068c2ecf20Sopenharmony_ci}
307