xref: /openbmc/linux/drivers/scsi/cxlflash/main.c (revision 1aaba11d)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2c21e0bbfSMatthew R. Ochs /*
3c21e0bbfSMatthew R. Ochs  * CXL Flash Device Driver
4c21e0bbfSMatthew R. Ochs  *
5c21e0bbfSMatthew R. Ochs  * Written by: Manoj N. Kumar <manoj@linux.vnet.ibm.com>, IBM Corporation
6c21e0bbfSMatthew R. Ochs  *             Matthew R. Ochs <mrochs@linux.vnet.ibm.com>, IBM Corporation
7c21e0bbfSMatthew R. Ochs  *
8c21e0bbfSMatthew R. Ochs  * Copyright (C) 2015 IBM Corporation
9c21e0bbfSMatthew R. Ochs  */
10c21e0bbfSMatthew R. Ochs 
11c21e0bbfSMatthew R. Ochs #include <linux/delay.h>
12c21e0bbfSMatthew R. Ochs #include <linux/list.h>
13c21e0bbfSMatthew R. Ochs #include <linux/module.h>
14c21e0bbfSMatthew R. Ochs #include <linux/pci.h>
15c21e0bbfSMatthew R. Ochs 
16c21e0bbfSMatthew R. Ochs #include <asm/unaligned.h>
17c21e0bbfSMatthew R. Ochs 
18c21e0bbfSMatthew R. Ochs #include <scsi/scsi_cmnd.h>
19c21e0bbfSMatthew R. Ochs #include <scsi/scsi_host.h>
2065be2c79SMatthew R. Ochs #include <uapi/scsi/cxlflash_ioctl.h>
21c21e0bbfSMatthew R. Ochs 
22c21e0bbfSMatthew R. Ochs #include "main.h"
23c21e0bbfSMatthew R. Ochs #include "sislite.h"
24c21e0bbfSMatthew R. Ochs #include "common.h"
25c21e0bbfSMatthew R. Ochs 
26c21e0bbfSMatthew R. Ochs MODULE_DESCRIPTION(CXLFLASH_ADAPTER_NAME);
27c21e0bbfSMatthew R. Ochs MODULE_AUTHOR("Manoj N. Kumar <manoj@linux.vnet.ibm.com>");
28c21e0bbfSMatthew R. Ochs MODULE_AUTHOR("Matthew R. Ochs <mrochs@linux.vnet.ibm.com>");
29c21e0bbfSMatthew R. Ochs MODULE_LICENSE("GPL");
30c21e0bbfSMatthew R. Ochs 
31a834a36bSUma Krishnan static struct class *cxlflash_class;
32a834a36bSUma Krishnan static u32 cxlflash_major;
33a834a36bSUma Krishnan static DECLARE_BITMAP(cxlflash_minor, CXLFLASH_MAX_ADAPTERS);
34a834a36bSUma Krishnan 
35c21e0bbfSMatthew R. Ochs /**
36c21e0bbfSMatthew R. Ochs  * process_cmd_err() - command error handler
37c21e0bbfSMatthew R. Ochs  * @cmd:	AFU command that experienced the error.
38c21e0bbfSMatthew R. Ochs  * @scp:	SCSI command associated with the AFU command in error.
39c21e0bbfSMatthew R. Ochs  *
40c21e0bbfSMatthew R. Ochs  * Translates error bits from AFU command to SCSI command results.
41c21e0bbfSMatthew R. Ochs  */
process_cmd_err(struct afu_cmd * cmd,struct scsi_cmnd * scp)42c21e0bbfSMatthew R. Ochs static void process_cmd_err(struct afu_cmd *cmd, struct scsi_cmnd *scp)
43c21e0bbfSMatthew R. Ochs {
44fb67d44dSMatthew R. Ochs 	struct afu *afu = cmd->parent;
45fb67d44dSMatthew R. Ochs 	struct cxlflash_cfg *cfg = afu->parent;
46fb67d44dSMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
47c21e0bbfSMatthew R. Ochs 	struct sisl_ioasa *ioasa;
488396012fSMatthew R. Ochs 	u32 resid;
49c21e0bbfSMatthew R. Ochs 
50c21e0bbfSMatthew R. Ochs 	ioasa = &(cmd->sa);
51c21e0bbfSMatthew R. Ochs 
52c21e0bbfSMatthew R. Ochs 	if (ioasa->rc.flags & SISL_RC_FLAGS_UNDERRUN) {
538396012fSMatthew R. Ochs 		resid = ioasa->resid;
548396012fSMatthew R. Ochs 		scsi_set_resid(scp, resid);
55fb67d44dSMatthew R. Ochs 		dev_dbg(dev, "%s: cmd underrun cmd = %p scp = %p, resid = %d\n",
568396012fSMatthew R. Ochs 			__func__, cmd, scp, resid);
57c21e0bbfSMatthew R. Ochs 	}
58c21e0bbfSMatthew R. Ochs 
59c21e0bbfSMatthew R. Ochs 	if (ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN) {
60fb67d44dSMatthew R. Ochs 		dev_dbg(dev, "%s: cmd underrun cmd = %p scp = %p\n",
61c21e0bbfSMatthew R. Ochs 			__func__, cmd, scp);
62c21e0bbfSMatthew R. Ochs 		scp->result = (DID_ERROR << 16);
63c21e0bbfSMatthew R. Ochs 	}
64c21e0bbfSMatthew R. Ochs 
65fb67d44dSMatthew R. Ochs 	dev_dbg(dev, "%s: cmd failed afu_rc=%02x scsi_rc=%02x fc_rc=%02x "
66fb67d44dSMatthew R. Ochs 		"afu_extra=%02x scsi_extra=%02x fc_extra=%02x\n", __func__,
67fb67d44dSMatthew R. Ochs 		ioasa->rc.afu_rc, ioasa->rc.scsi_rc, ioasa->rc.fc_rc,
68fb67d44dSMatthew R. Ochs 		ioasa->afu_extra, ioasa->scsi_extra, ioasa->fc_extra);
69c21e0bbfSMatthew R. Ochs 
70c21e0bbfSMatthew R. Ochs 	if (ioasa->rc.scsi_rc) {
71c21e0bbfSMatthew R. Ochs 		/* We have a SCSI status */
72c21e0bbfSMatthew R. Ochs 		if (ioasa->rc.flags & SISL_RC_FLAGS_SENSE_VALID) {
73c21e0bbfSMatthew R. Ochs 			memcpy(scp->sense_buffer, ioasa->sense_data,
74c21e0bbfSMatthew R. Ochs 			       SISL_SENSE_DATA_LEN);
75c21e0bbfSMatthew R. Ochs 			scp->result = ioasa->rc.scsi_rc;
76c21e0bbfSMatthew R. Ochs 		} else
77c21e0bbfSMatthew R. Ochs 			scp->result = ioasa->rc.scsi_rc | (DID_ERROR << 16);
78c21e0bbfSMatthew R. Ochs 	}
79c21e0bbfSMatthew R. Ochs 
80c21e0bbfSMatthew R. Ochs 	/*
81c21e0bbfSMatthew R. Ochs 	 * We encountered an error. Set scp->result based on nature
82c21e0bbfSMatthew R. Ochs 	 * of error.
83c21e0bbfSMatthew R. Ochs 	 */
84c21e0bbfSMatthew R. Ochs 	if (ioasa->rc.fc_rc) {
85c21e0bbfSMatthew R. Ochs 		/* We have an FC status */
86c21e0bbfSMatthew R. Ochs 		switch (ioasa->rc.fc_rc) {
87c21e0bbfSMatthew R. Ochs 		case SISL_FC_RC_LINKDOWN:
88c21e0bbfSMatthew R. Ochs 			scp->result = (DID_REQUEUE << 16);
89c21e0bbfSMatthew R. Ochs 			break;
90c21e0bbfSMatthew R. Ochs 		case SISL_FC_RC_RESID:
91c21e0bbfSMatthew R. Ochs 			/* This indicates an FCP resid underrun */
92c21e0bbfSMatthew R. Ochs 			if (!(ioasa->rc.flags & SISL_RC_FLAGS_OVERRUN)) {
93c21e0bbfSMatthew R. Ochs 				/* If the SISL_RC_FLAGS_OVERRUN flag was set,
94c21e0bbfSMatthew R. Ochs 				 * then we will handle this error else where.
95c21e0bbfSMatthew R. Ochs 				 * If not then we must handle it here.
968396012fSMatthew R. Ochs 				 * This is probably an AFU bug.
97c21e0bbfSMatthew R. Ochs 				 */
98c21e0bbfSMatthew R. Ochs 				scp->result = (DID_ERROR << 16);
99c21e0bbfSMatthew R. Ochs 			}
100c21e0bbfSMatthew R. Ochs 			break;
101c21e0bbfSMatthew R. Ochs 		case SISL_FC_RC_RESIDERR:
102c21e0bbfSMatthew R. Ochs 			/* Resid mismatch between adapter and device */
103c21e0bbfSMatthew R. Ochs 		case SISL_FC_RC_TGTABORT:
104c21e0bbfSMatthew R. Ochs 		case SISL_FC_RC_ABORTOK:
105c21e0bbfSMatthew R. Ochs 		case SISL_FC_RC_ABORTFAIL:
106c21e0bbfSMatthew R. Ochs 		case SISL_FC_RC_NOLOGI:
107c21e0bbfSMatthew R. Ochs 		case SISL_FC_RC_ABORTPEND:
108c21e0bbfSMatthew R. Ochs 		case SISL_FC_RC_WRABORTPEND:
109c21e0bbfSMatthew R. Ochs 		case SISL_FC_RC_NOEXP:
110c21e0bbfSMatthew R. Ochs 		case SISL_FC_RC_INUSE:
111c21e0bbfSMatthew R. Ochs 			scp->result = (DID_ERROR << 16);
112c21e0bbfSMatthew R. Ochs 			break;
113c21e0bbfSMatthew R. Ochs 		}
114c21e0bbfSMatthew R. Ochs 	}
115c21e0bbfSMatthew R. Ochs 
116c21e0bbfSMatthew R. Ochs 	if (ioasa->rc.afu_rc) {
117c21e0bbfSMatthew R. Ochs 		/* We have an AFU error */
118c21e0bbfSMatthew R. Ochs 		switch (ioasa->rc.afu_rc) {
119c21e0bbfSMatthew R. Ochs 		case SISL_AFU_RC_NO_CHANNELS:
1208396012fSMatthew R. Ochs 			scp->result = (DID_NO_CONNECT << 16);
121c21e0bbfSMatthew R. Ochs 			break;
122c21e0bbfSMatthew R. Ochs 		case SISL_AFU_RC_DATA_DMA_ERR:
123c21e0bbfSMatthew R. Ochs 			switch (ioasa->afu_extra) {
124c21e0bbfSMatthew R. Ochs 			case SISL_AFU_DMA_ERR_PAGE_IN:
125c21e0bbfSMatthew R. Ochs 				/* Retry */
126c21e0bbfSMatthew R. Ochs 				scp->result = (DID_IMM_RETRY << 16);
127c21e0bbfSMatthew R. Ochs 				break;
128c21e0bbfSMatthew R. Ochs 			case SISL_AFU_DMA_ERR_INVALID_EA:
129c21e0bbfSMatthew R. Ochs 			default:
130c21e0bbfSMatthew R. Ochs 				scp->result = (DID_ERROR << 16);
131c21e0bbfSMatthew R. Ochs 			}
132c21e0bbfSMatthew R. Ochs 			break;
133c21e0bbfSMatthew R. Ochs 		case SISL_AFU_RC_OUT_OF_DATA_BUFS:
134c21e0bbfSMatthew R. Ochs 			/* Retry */
135ebb54b20SMike Christie 			scp->result = (DID_ERROR << 16);
136c21e0bbfSMatthew R. Ochs 			break;
137c21e0bbfSMatthew R. Ochs 		default:
138c21e0bbfSMatthew R. Ochs 			scp->result = (DID_ERROR << 16);
139c21e0bbfSMatthew R. Ochs 		}
140c21e0bbfSMatthew R. Ochs 	}
141c21e0bbfSMatthew R. Ochs }
142c21e0bbfSMatthew R. Ochs 
143c21e0bbfSMatthew R. Ochs /**
144c21e0bbfSMatthew R. Ochs  * cmd_complete() - command completion handler
145c21e0bbfSMatthew R. Ochs  * @cmd:	AFU command that has completed.
146c21e0bbfSMatthew R. Ochs  *
1478ba1ddb3SMatthew R. Ochs  * For SCSI commands this routine prepares and submits commands that have
1488ba1ddb3SMatthew R. Ochs  * either completed or timed out to the SCSI stack. For internal commands
1498ba1ddb3SMatthew R. Ochs  * (TMF or AFU), this routine simply notifies the originator that the
1508ba1ddb3SMatthew R. Ochs  * command has completed.
151c21e0bbfSMatthew R. Ochs  */
cmd_complete(struct afu_cmd * cmd)152c21e0bbfSMatthew R. Ochs static void cmd_complete(struct afu_cmd *cmd)
153c21e0bbfSMatthew R. Ochs {
154c21e0bbfSMatthew R. Ochs 	struct scsi_cmnd *scp;
155c21e0bbfSMatthew R. Ochs 	ulong lock_flags;
156c21e0bbfSMatthew R. Ochs 	struct afu *afu = cmd->parent;
157c21e0bbfSMatthew R. Ochs 	struct cxlflash_cfg *cfg = afu->parent;
158fb67d44dSMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
159a002bf83SUma Krishnan 	struct hwq *hwq = get_hwq(afu, cmd->hwq_index);
160c21e0bbfSMatthew R. Ochs 
161a002bf83SUma Krishnan 	spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
162a002bf83SUma Krishnan 	list_del(&cmd->list);
163a002bf83SUma Krishnan 	spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
164a002bf83SUma Krishnan 
165fe7f9698SMatthew R. Ochs 	if (cmd->scp) {
166fe7f9698SMatthew R. Ochs 		scp = cmd->scp;
1678396012fSMatthew R. Ochs 		if (unlikely(cmd->sa.ioasc))
168c21e0bbfSMatthew R. Ochs 			process_cmd_err(cmd, scp);
169c21e0bbfSMatthew R. Ochs 		else
170c21e0bbfSMatthew R. Ochs 			scp->result = (DID_OK << 16);
171c21e0bbfSMatthew R. Ochs 
172fb67d44dSMatthew R. Ochs 		dev_dbg_ratelimited(dev, "%s:scp=%p result=%08x ioasc=%08x\n",
173fb67d44dSMatthew R. Ochs 				    __func__, scp, scp->result, cmd->sa.ioasc);
174e82d6b17SBart Van Assche 		scsi_done(scp);
1758ba1ddb3SMatthew R. Ochs 	} else if (cmd->cmd_tmf) {
176018d1dc9SMatthew R. Ochs 		spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
177c21e0bbfSMatthew R. Ochs 		cfg->tmf_active = false;
178c21e0bbfSMatthew R. Ochs 		wake_up_all_locked(&cfg->tmf_waitq);
179018d1dc9SMatthew R. Ochs 		spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
180c21e0bbfSMatthew R. Ochs 	} else
181c21e0bbfSMatthew R. Ochs 		complete(&cmd->cevent);
182c21e0bbfSMatthew R. Ochs }
183c21e0bbfSMatthew R. Ochs 
184c21e0bbfSMatthew R. Ochs /**
185a1ea04b3SUma Krishnan  * flush_pending_cmds() - flush all pending commands on this hardware queue
186a1ea04b3SUma Krishnan  * @hwq:	Hardware queue to flush.
187a1ea04b3SUma Krishnan  *
188a1ea04b3SUma Krishnan  * The hardware send queue lock associated with this hardware queue must be
189a1ea04b3SUma Krishnan  * held when calling this routine.
190a1ea04b3SUma Krishnan  */
flush_pending_cmds(struct hwq * hwq)191a1ea04b3SUma Krishnan static void flush_pending_cmds(struct hwq *hwq)
192a1ea04b3SUma Krishnan {
1938ba1ddb3SMatthew R. Ochs 	struct cxlflash_cfg *cfg = hwq->afu->parent;
194a1ea04b3SUma Krishnan 	struct afu_cmd *cmd, *tmp;
195a1ea04b3SUma Krishnan 	struct scsi_cmnd *scp;
1968ba1ddb3SMatthew R. Ochs 	ulong lock_flags;
197a1ea04b3SUma Krishnan 
198a1ea04b3SUma Krishnan 	list_for_each_entry_safe(cmd, tmp, &hwq->pending_cmds, list) {
199a1ea04b3SUma Krishnan 		/* Bypass command when on a doneq, cmd_complete() will handle */
200a1ea04b3SUma Krishnan 		if (!list_empty(&cmd->queue))
201a1ea04b3SUma Krishnan 			continue;
202a1ea04b3SUma Krishnan 
203a1ea04b3SUma Krishnan 		list_del(&cmd->list);
204a1ea04b3SUma Krishnan 
205a1ea04b3SUma Krishnan 		if (cmd->scp) {
206a1ea04b3SUma Krishnan 			scp = cmd->scp;
207a1ea04b3SUma Krishnan 			scp->result = (DID_IMM_RETRY << 16);
208e82d6b17SBart Van Assche 			scsi_done(scp);
209a1ea04b3SUma Krishnan 		} else {
210a1ea04b3SUma Krishnan 			cmd->cmd_aborted = true;
2118ba1ddb3SMatthew R. Ochs 
2128ba1ddb3SMatthew R. Ochs 			if (cmd->cmd_tmf) {
2138ba1ddb3SMatthew R. Ochs 				spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
2148ba1ddb3SMatthew R. Ochs 				cfg->tmf_active = false;
2158ba1ddb3SMatthew R. Ochs 				wake_up_all_locked(&cfg->tmf_waitq);
2168ba1ddb3SMatthew R. Ochs 				spin_unlock_irqrestore(&cfg->tmf_slock,
2178ba1ddb3SMatthew R. Ochs 						       lock_flags);
2188ba1ddb3SMatthew R. Ochs 			} else
219a1ea04b3SUma Krishnan 				complete(&cmd->cevent);
220a1ea04b3SUma Krishnan 		}
221a1ea04b3SUma Krishnan 	}
222a1ea04b3SUma Krishnan }
223a1ea04b3SUma Krishnan 
224a1ea04b3SUma Krishnan /**
225a96851d3SUma Krishnan  * context_reset() - reset context via specified register
226a96851d3SUma Krishnan  * @hwq:	Hardware queue owning the context to be reset.
2279c7d1ee5SMatthew R. Ochs  * @reset_reg:	MMIO register to perform reset.
228a96851d3SUma Krishnan  *
2297c4c41f1SUma Krishnan  * When the reset is successful, the SISLite specification guarantees that
2307c4c41f1SUma Krishnan  * the AFU has aborted all currently pending I/O. Accordingly, these commands
2317c4c41f1SUma Krishnan  * must be flushed.
2327c4c41f1SUma Krishnan  *
233a96851d3SUma Krishnan  * Return: 0 on success, -errno on failure
23415305514SMatthew R. Ochs  */
context_reset(struct hwq * hwq,__be64 __iomem * reset_reg)235a96851d3SUma Krishnan static int context_reset(struct hwq *hwq, __be64 __iomem *reset_reg)
23615305514SMatthew R. Ochs {
237a96851d3SUma Krishnan 	struct cxlflash_cfg *cfg = hwq->afu->parent;
2383d2f617dSUma Krishnan 	struct device *dev = &cfg->dev->dev;
239a96851d3SUma Krishnan 	int rc = -ETIMEDOUT;
240a96851d3SUma Krishnan 	int nretry = 0;
241a96851d3SUma Krishnan 	u64 val = 0x1;
2427c4c41f1SUma Krishnan 	ulong lock_flags;
24315305514SMatthew R. Ochs 
244a96851d3SUma Krishnan 	dev_dbg(dev, "%s: hwq=%p\n", __func__, hwq);
24515305514SMatthew R. Ochs 
2467c4c41f1SUma Krishnan 	spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
2477c4c41f1SUma Krishnan 
248a96851d3SUma Krishnan 	writeq_be(val, reset_reg);
24915305514SMatthew R. Ochs 	do {
250a96851d3SUma Krishnan 		val = readq_be(reset_reg);
251a96851d3SUma Krishnan 		if ((val & 0x1) == 0x0) {
252a96851d3SUma Krishnan 			rc = 0;
25315305514SMatthew R. Ochs 			break;
254a96851d3SUma Krishnan 		}
255a96851d3SUma Krishnan 
25615305514SMatthew R. Ochs 		/* Double delay each time */
257ea765431SManoj N. Kumar 		udelay(1 << nretry);
25815305514SMatthew R. Ochs 	} while (nretry++ < MC_ROOM_RETRY_CNT);
2593d2f617dSUma Krishnan 
2607c4c41f1SUma Krishnan 	if (!rc)
2617c4c41f1SUma Krishnan 		flush_pending_cmds(hwq);
2627c4c41f1SUma Krishnan 
2637c4c41f1SUma Krishnan 	spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
2647c4c41f1SUma Krishnan 
265a96851d3SUma Krishnan 	dev_dbg(dev, "%s: returning rc=%d, val=%016llx nretry=%d\n",
266a96851d3SUma Krishnan 		__func__, rc, val, nretry);
267a96851d3SUma Krishnan 	return rc;
26815305514SMatthew R. Ochs }
26915305514SMatthew R. Ochs 
27015305514SMatthew R. Ochs /**
271a96851d3SUma Krishnan  * context_reset_ioarrin() - reset context via IOARRIN register
272a96851d3SUma Krishnan  * @hwq:	Hardware queue owning the context to be reset.
273a96851d3SUma Krishnan  *
274a96851d3SUma Krishnan  * Return: 0 on success, -errno on failure
2759c7d1ee5SMatthew R. Ochs  */
context_reset_ioarrin(struct hwq * hwq)276a96851d3SUma Krishnan static int context_reset_ioarrin(struct hwq *hwq)
2779c7d1ee5SMatthew R. Ochs {
278a96851d3SUma Krishnan 	return context_reset(hwq, &hwq->host_map->ioarrin);
2799c7d1ee5SMatthew R. Ochs }
2809c7d1ee5SMatthew R. Ochs 
2819c7d1ee5SMatthew R. Ochs /**
282a96851d3SUma Krishnan  * context_reset_sq() - reset context via SQ_CONTEXT_RESET register
283a96851d3SUma Krishnan  * @hwq:	Hardware queue owning the context to be reset.
284a96851d3SUma Krishnan  *
285a96851d3SUma Krishnan  * Return: 0 on success, -errno on failure
286696d0b0cSMatthew R. Ochs  */
context_reset_sq(struct hwq * hwq)287a96851d3SUma Krishnan static int context_reset_sq(struct hwq *hwq)
288696d0b0cSMatthew R. Ochs {
289a96851d3SUma Krishnan 	return context_reset(hwq, &hwq->host_map->sq_ctx_reset);
290696d0b0cSMatthew R. Ochs }
291696d0b0cSMatthew R. Ochs 
292696d0b0cSMatthew R. Ochs /**
29348b4be36SMatthew R. Ochs  * send_cmd_ioarrin() - sends an AFU command via IOARRIN register
29415305514SMatthew R. Ochs  * @afu:	AFU associated with the host.
29515305514SMatthew R. Ochs  * @cmd:	AFU command to send.
29615305514SMatthew R. Ochs  *
29715305514SMatthew R. Ochs  * Return:
2981284fb0cSMatthew R. Ochs  *	0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
29915305514SMatthew R. Ochs  */
send_cmd_ioarrin(struct afu * afu,struct afu_cmd * cmd)30048b4be36SMatthew R. Ochs static int send_cmd_ioarrin(struct afu *afu, struct afu_cmd *cmd)
30115305514SMatthew R. Ochs {
30215305514SMatthew R. Ochs 	struct cxlflash_cfg *cfg = afu->parent;
30315305514SMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
304bfc0bab1SUma Krishnan 	struct hwq *hwq = get_hwq(afu, cmd->hwq_index);
30515305514SMatthew R. Ochs 	int rc = 0;
30611f7b184SUma Krishnan 	s64 room;
30711f7b184SUma Krishnan 	ulong lock_flags;
30815305514SMatthew R. Ochs 
30915305514SMatthew R. Ochs 	/*
31011f7b184SUma Krishnan 	 * To avoid the performance penalty of MMIO, spread the update of
31111f7b184SUma Krishnan 	 * 'room' over multiple commands.
31215305514SMatthew R. Ochs 	 */
31366ea9bccSUma Krishnan 	spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
314bfc0bab1SUma Krishnan 	if (--hwq->room < 0) {
315bfc0bab1SUma Krishnan 		room = readq_be(&hwq->host_map->cmd_room);
31611f7b184SUma Krishnan 		if (room <= 0) {
31711f7b184SUma Krishnan 			dev_dbg_ratelimited(dev, "%s: no cmd_room to send "
31811f7b184SUma Krishnan 					    "0x%02X, room=0x%016llX\n",
31911f7b184SUma Krishnan 					    __func__, cmd->rcb.cdb[0], room);
320bfc0bab1SUma Krishnan 			hwq->room = 0;
32111f7b184SUma Krishnan 			rc = SCSI_MLQUEUE_HOST_BUSY;
32211f7b184SUma Krishnan 			goto out;
32311f7b184SUma Krishnan 		}
324bfc0bab1SUma Krishnan 		hwq->room = room - 1;
32515305514SMatthew R. Ochs 	}
32615305514SMatthew R. Ochs 
327a002bf83SUma Krishnan 	list_add(&cmd->list, &hwq->pending_cmds);
328bfc0bab1SUma Krishnan 	writeq_be((u64)&cmd->rcb, &hwq->host_map->ioarrin);
32915305514SMatthew R. Ochs out:
33066ea9bccSUma Krishnan 	spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
331d58188c3SUma Krishnan 	dev_dbg_ratelimited(dev, "%s: cmd=%p len=%u ea=%016llx rc=%d\n",
332d58188c3SUma Krishnan 		__func__, cmd, cmd->rcb.data_len, cmd->rcb.data_ea, rc);
33315305514SMatthew R. Ochs 	return rc;
33415305514SMatthew R. Ochs }
33515305514SMatthew R. Ochs 
33615305514SMatthew R. Ochs /**
337696d0b0cSMatthew R. Ochs  * send_cmd_sq() - sends an AFU command via SQ ring
338696d0b0cSMatthew R. Ochs  * @afu:	AFU associated with the host.
339696d0b0cSMatthew R. Ochs  * @cmd:	AFU command to send.
340696d0b0cSMatthew R. Ochs  *
341696d0b0cSMatthew R. Ochs  * Return:
342696d0b0cSMatthew R. Ochs  *	0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
343696d0b0cSMatthew R. Ochs  */
send_cmd_sq(struct afu * afu,struct afu_cmd * cmd)344696d0b0cSMatthew R. Ochs static int send_cmd_sq(struct afu *afu, struct afu_cmd *cmd)
345696d0b0cSMatthew R. Ochs {
346696d0b0cSMatthew R. Ochs 	struct cxlflash_cfg *cfg = afu->parent;
347696d0b0cSMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
348bfc0bab1SUma Krishnan 	struct hwq *hwq = get_hwq(afu, cmd->hwq_index);
349696d0b0cSMatthew R. Ochs 	int rc = 0;
350696d0b0cSMatthew R. Ochs 	int newval;
351696d0b0cSMatthew R. Ochs 	ulong lock_flags;
352696d0b0cSMatthew R. Ochs 
353bfc0bab1SUma Krishnan 	newval = atomic_dec_if_positive(&hwq->hsq_credits);
354696d0b0cSMatthew R. Ochs 	if (newval <= 0) {
355696d0b0cSMatthew R. Ochs 		rc = SCSI_MLQUEUE_HOST_BUSY;
356696d0b0cSMatthew R. Ochs 		goto out;
357696d0b0cSMatthew R. Ochs 	}
358696d0b0cSMatthew R. Ochs 
359696d0b0cSMatthew R. Ochs 	cmd->rcb.ioasa = &cmd->sa;
360696d0b0cSMatthew R. Ochs 
361bfc0bab1SUma Krishnan 	spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
362696d0b0cSMatthew R. Ochs 
363bfc0bab1SUma Krishnan 	*hwq->hsq_curr = cmd->rcb;
364bfc0bab1SUma Krishnan 	if (hwq->hsq_curr < hwq->hsq_end)
365bfc0bab1SUma Krishnan 		hwq->hsq_curr++;
366696d0b0cSMatthew R. Ochs 	else
367bfc0bab1SUma Krishnan 		hwq->hsq_curr = hwq->hsq_start;
368a002bf83SUma Krishnan 
369a002bf83SUma Krishnan 	list_add(&cmd->list, &hwq->pending_cmds);
370bfc0bab1SUma Krishnan 	writeq_be((u64)hwq->hsq_curr, &hwq->host_map->sq_tail);
371696d0b0cSMatthew R. Ochs 
372bfc0bab1SUma Krishnan 	spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
373696d0b0cSMatthew R. Ochs out:
374fb67d44dSMatthew R. Ochs 	dev_dbg(dev, "%s: cmd=%p len=%u ea=%016llx ioasa=%p rc=%d curr=%p "
375fb67d44dSMatthew R. Ochs 	       "head=%016llx tail=%016llx\n", __func__, cmd, cmd->rcb.data_len,
376bfc0bab1SUma Krishnan 	       cmd->rcb.data_ea, cmd->rcb.ioasa, rc, hwq->hsq_curr,
377bfc0bab1SUma Krishnan 	       readq_be(&hwq->host_map->sq_head),
378bfc0bab1SUma Krishnan 	       readq_be(&hwq->host_map->sq_tail));
379696d0b0cSMatthew R. Ochs 	return rc;
380696d0b0cSMatthew R. Ochs }
381696d0b0cSMatthew R. Ochs 
382696d0b0cSMatthew R. Ochs /**
38315305514SMatthew R. Ochs  * wait_resp() - polls for a response or timeout to a sent AFU command
38415305514SMatthew R. Ochs  * @afu:	AFU associated with the host.
38515305514SMatthew R. Ochs  * @cmd:	AFU command that was sent.
3869ba848acSMatthew R. Ochs  *
387a96851d3SUma Krishnan  * Return: 0 on success, -errno on failure
38815305514SMatthew R. Ochs  */
wait_resp(struct afu * afu,struct afu_cmd * cmd)3899ba848acSMatthew R. Ochs static int wait_resp(struct afu *afu, struct afu_cmd *cmd)
39015305514SMatthew R. Ochs {
391fb67d44dSMatthew R. Ochs 	struct cxlflash_cfg *cfg = afu->parent;
392fb67d44dSMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
3939ba848acSMatthew R. Ochs 	int rc = 0;
39415305514SMatthew R. Ochs 	ulong timeout = msecs_to_jiffies(cmd->rcb.timeout * 2 * 1000);
39515305514SMatthew R. Ochs 
39615305514SMatthew R. Ochs 	timeout = wait_for_completion_timeout(&cmd->cevent, timeout);
397a96851d3SUma Krishnan 	if (!timeout)
398a96851d3SUma Krishnan 		rc = -ETIMEDOUT;
39915305514SMatthew R. Ochs 
400a1ea04b3SUma Krishnan 	if (cmd->cmd_aborted)
401a1ea04b3SUma Krishnan 		rc = -EAGAIN;
402a1ea04b3SUma Krishnan 
4039ba848acSMatthew R. Ochs 	if (unlikely(cmd->sa.ioasc != 0)) {
404fb67d44dSMatthew R. Ochs 		dev_err(dev, "%s: cmd %02x failed, ioasc=%08x\n",
405fb67d44dSMatthew R. Ochs 			__func__, cmd->rcb.cdb[0], cmd->sa.ioasc);
406a96851d3SUma Krishnan 		rc = -EIO;
4079ba848acSMatthew R. Ochs 	}
4089ba848acSMatthew R. Ochs 
4099ba848acSMatthew R. Ochs 	return rc;
41015305514SMatthew R. Ochs }
41115305514SMatthew R. Ochs 
41215305514SMatthew R. Ochs /**
4131dd0c0e4SMatthew R. Ochs  * cmd_to_target_hwq() - selects a target hardware queue for a SCSI command
4141dd0c0e4SMatthew R. Ochs  * @host:	SCSI host associated with device.
4151dd0c0e4SMatthew R. Ochs  * @scp:	SCSI command to send.
4161dd0c0e4SMatthew R. Ochs  * @afu:	SCSI command to send.
4171dd0c0e4SMatthew R. Ochs  *
4181dd0c0e4SMatthew R. Ochs  * Hashes a command based upon the hardware queue mode.
4191dd0c0e4SMatthew R. Ochs  *
4201dd0c0e4SMatthew R. Ochs  * Return: Trusted index of target hardware queue
4211dd0c0e4SMatthew R. Ochs  */
cmd_to_target_hwq(struct Scsi_Host * host,struct scsi_cmnd * scp,struct afu * afu)4221dd0c0e4SMatthew R. Ochs static u32 cmd_to_target_hwq(struct Scsi_Host *host, struct scsi_cmnd *scp,
4231dd0c0e4SMatthew R. Ochs 			     struct afu *afu)
4241dd0c0e4SMatthew R. Ochs {
4251dd0c0e4SMatthew R. Ochs 	u32 tag;
4261dd0c0e4SMatthew R. Ochs 	u32 hwq = 0;
4271dd0c0e4SMatthew R. Ochs 
4281dd0c0e4SMatthew R. Ochs 	if (afu->num_hwqs == 1)
4291dd0c0e4SMatthew R. Ochs 		return 0;
4301dd0c0e4SMatthew R. Ochs 
4311dd0c0e4SMatthew R. Ochs 	switch (afu->hwq_mode) {
4321dd0c0e4SMatthew R. Ochs 	case HWQ_MODE_RR:
4331dd0c0e4SMatthew R. Ochs 		hwq = afu->hwq_rr_count++ % afu->num_hwqs;
4341dd0c0e4SMatthew R. Ochs 		break;
4351dd0c0e4SMatthew R. Ochs 	case HWQ_MODE_TAG:
436d3e16aecSBart Van Assche 		tag = blk_mq_unique_tag(scsi_cmd_to_rq(scp));
4371dd0c0e4SMatthew R. Ochs 		hwq = blk_mq_unique_tag_to_hwq(tag);
4381dd0c0e4SMatthew R. Ochs 		break;
4391dd0c0e4SMatthew R. Ochs 	case HWQ_MODE_CPU:
4401dd0c0e4SMatthew R. Ochs 		hwq = smp_processor_id() % afu->num_hwqs;
4411dd0c0e4SMatthew R. Ochs 		break;
4421dd0c0e4SMatthew R. Ochs 	default:
4431dd0c0e4SMatthew R. Ochs 		WARN_ON_ONCE(1);
4441dd0c0e4SMatthew R. Ochs 	}
4451dd0c0e4SMatthew R. Ochs 
4461dd0c0e4SMatthew R. Ochs 	return hwq;
4471dd0c0e4SMatthew R. Ochs }
4481dd0c0e4SMatthew R. Ochs 
4491dd0c0e4SMatthew R. Ochs /**
450c21e0bbfSMatthew R. Ochs  * send_tmf() - sends a Task Management Function (TMF)
45132abbedaSMatthew R. Ochs  * @cfg:	Internal structure associated with the host.
45232abbedaSMatthew R. Ochs  * @sdev:	SCSI device destined for TMF.
453c21e0bbfSMatthew R. Ochs  * @tmfcmd:	TMF command to send.
454c21e0bbfSMatthew R. Ochs  *
455c21e0bbfSMatthew R. Ochs  * Return:
4568ba1ddb3SMatthew R. Ochs  *	0 on success, SCSI_MLQUEUE_HOST_BUSY or -errno on failure
457c21e0bbfSMatthew R. Ochs  */
send_tmf(struct cxlflash_cfg * cfg,struct scsi_device * sdev,u64 tmfcmd)45832abbedaSMatthew R. Ochs static int send_tmf(struct cxlflash_cfg *cfg, struct scsi_device *sdev,
45932abbedaSMatthew R. Ochs 		    u64 tmfcmd)
460c21e0bbfSMatthew R. Ochs {
46132abbedaSMatthew R. Ochs 	struct afu *afu = cfg->afu;
4628ba1ddb3SMatthew R. Ochs 	struct afu_cmd *cmd = NULL;
4634392ba49SMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
46432abbedaSMatthew R. Ochs 	struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ);
4659a597cd4SUma Krishnan 	bool needs_deletion = false;
4668ba1ddb3SMatthew R. Ochs 	char *buf = NULL;
467c21e0bbfSMatthew R. Ochs 	ulong lock_flags;
468c21e0bbfSMatthew R. Ochs 	int rc = 0;
469018d1dc9SMatthew R. Ochs 	ulong to;
470c21e0bbfSMatthew R. Ochs 
4718ba1ddb3SMatthew R. Ochs 	buf = kzalloc(sizeof(*cmd) + __alignof__(*cmd) - 1, GFP_KERNEL);
4728ba1ddb3SMatthew R. Ochs 	if (unlikely(!buf)) {
4738ba1ddb3SMatthew R. Ochs 		dev_err(dev, "%s: no memory for command\n", __func__);
4748ba1ddb3SMatthew R. Ochs 		rc = -ENOMEM;
4758ba1ddb3SMatthew R. Ochs 		goto out;
4768ba1ddb3SMatthew R. Ochs 	}
4778ba1ddb3SMatthew R. Ochs 
4788ba1ddb3SMatthew R. Ochs 	cmd = (struct afu_cmd *)PTR_ALIGN(buf, __alignof__(*cmd));
4798ba1ddb3SMatthew R. Ochs 	INIT_LIST_HEAD(&cmd->queue);
4808ba1ddb3SMatthew R. Ochs 
481018d1dc9SMatthew R. Ochs 	/* When Task Management Function is active do not send another */
482018d1dc9SMatthew R. Ochs 	spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
483c21e0bbfSMatthew R. Ochs 	if (cfg->tmf_active)
484018d1dc9SMatthew R. Ochs 		wait_event_interruptible_lock_irq(cfg->tmf_waitq,
485018d1dc9SMatthew R. Ochs 						  !cfg->tmf_active,
486018d1dc9SMatthew R. Ochs 						  cfg->tmf_slock);
487c21e0bbfSMatthew R. Ochs 	cfg->tmf_active = true;
488018d1dc9SMatthew R. Ochs 	spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
489c21e0bbfSMatthew R. Ochs 
490d4ace351SMatthew R. Ochs 	cmd->parent = afu;
491d4ace351SMatthew R. Ochs 	cmd->cmd_tmf = true;
49232abbedaSMatthew R. Ochs 	cmd->hwq_index = hwq->index;
493d4ace351SMatthew R. Ochs 
494bfc0bab1SUma Krishnan 	cmd->rcb.ctx_id = hwq->ctx_hndl;
4955fbb96c8SMatthew R. Ochs 	cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
49632abbedaSMatthew R. Ochs 	cmd->rcb.port_sel = CHAN2PORTMASK(sdev->channel);
49732abbedaSMatthew R. Ochs 	cmd->rcb.lun_id = lun_to_lunid(sdev->lun);
498c21e0bbfSMatthew R. Ochs 	cmd->rcb.req_flags = (SISL_REQ_FLAGS_PORT_LUN_ID |
499d4ace351SMatthew R. Ochs 			      SISL_REQ_FLAGS_SUP_UNDERRUN |
500d4ace351SMatthew R. Ochs 			      SISL_REQ_FLAGS_TMF_CMD);
501c21e0bbfSMatthew R. Ochs 	memcpy(cmd->rcb.cdb, &tmfcmd, sizeof(tmfcmd));
502c21e0bbfSMatthew R. Ochs 
50348b4be36SMatthew R. Ochs 	rc = afu->send_cmd(afu, cmd);
504c21e0bbfSMatthew R. Ochs 	if (unlikely(rc)) {
505018d1dc9SMatthew R. Ochs 		spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
506c21e0bbfSMatthew R. Ochs 		cfg->tmf_active = false;
507018d1dc9SMatthew R. Ochs 		spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
508c21e0bbfSMatthew R. Ochs 		goto out;
509c21e0bbfSMatthew R. Ochs 	}
510c21e0bbfSMatthew R. Ochs 
511018d1dc9SMatthew R. Ochs 	spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
512018d1dc9SMatthew R. Ochs 	to = msecs_to_jiffies(5000);
513018d1dc9SMatthew R. Ochs 	to = wait_event_interruptible_lock_irq_timeout(cfg->tmf_waitq,
514018d1dc9SMatthew R. Ochs 						       !cfg->tmf_active,
515018d1dc9SMatthew R. Ochs 						       cfg->tmf_slock,
516018d1dc9SMatthew R. Ochs 						       to);
517018d1dc9SMatthew R. Ochs 	if (!to) {
518fb67d44dSMatthew R. Ochs 		dev_err(dev, "%s: TMF timed out\n", __func__);
5198ba1ddb3SMatthew R. Ochs 		rc = -ETIMEDOUT;
5209a597cd4SUma Krishnan 		needs_deletion = true;
5218ba1ddb3SMatthew R. Ochs 	} else if (cmd->cmd_aborted) {
5228ba1ddb3SMatthew R. Ochs 		dev_err(dev, "%s: TMF aborted\n", __func__);
5238ba1ddb3SMatthew R. Ochs 		rc = -EAGAIN;
5248ba1ddb3SMatthew R. Ochs 	} else if (cmd->sa.ioasc) {
5258ba1ddb3SMatthew R. Ochs 		dev_err(dev, "%s: TMF failed ioasc=%08x\n",
5268ba1ddb3SMatthew R. Ochs 			__func__, cmd->sa.ioasc);
5278ba1ddb3SMatthew R. Ochs 		rc = -EIO;
528018d1dc9SMatthew R. Ochs 	}
5298ba1ddb3SMatthew R. Ochs 	cfg->tmf_active = false;
530018d1dc9SMatthew R. Ochs 	spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
5319a597cd4SUma Krishnan 
5329a597cd4SUma Krishnan 	if (needs_deletion) {
5339a597cd4SUma Krishnan 		spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
5349a597cd4SUma Krishnan 		list_del(&cmd->list);
5359a597cd4SUma Krishnan 		spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
5369a597cd4SUma Krishnan 	}
537c21e0bbfSMatthew R. Ochs out:
5388ba1ddb3SMatthew R. Ochs 	kfree(buf);
539c21e0bbfSMatthew R. Ochs 	return rc;
540c21e0bbfSMatthew R. Ochs }
541c21e0bbfSMatthew R. Ochs 
542c21e0bbfSMatthew R. Ochs /**
543c21e0bbfSMatthew R. Ochs  * cxlflash_driver_info() - information handler for this host driver
544c21e0bbfSMatthew R. Ochs  * @host:	SCSI host associated with device.
545c21e0bbfSMatthew R. Ochs  *
546c21e0bbfSMatthew R. Ochs  * Return: A string describing the device.
547c21e0bbfSMatthew R. Ochs  */
cxlflash_driver_info(struct Scsi_Host * host)548c21e0bbfSMatthew R. Ochs static const char *cxlflash_driver_info(struct Scsi_Host *host)
549c21e0bbfSMatthew R. Ochs {
550c21e0bbfSMatthew R. Ochs 	return CXLFLASH_ADAPTER_NAME;
551c21e0bbfSMatthew R. Ochs }
552c21e0bbfSMatthew R. Ochs 
553c21e0bbfSMatthew R. Ochs /**
554c21e0bbfSMatthew R. Ochs  * cxlflash_queuecommand() - sends a mid-layer request
555c21e0bbfSMatthew R. Ochs  * @host:	SCSI host associated with device.
556c21e0bbfSMatthew R. Ochs  * @scp:	SCSI command to send.
557c21e0bbfSMatthew R. Ochs  *
5581284fb0cSMatthew R. Ochs  * Return: 0 on success, SCSI_MLQUEUE_HOST_BUSY on failure
559c21e0bbfSMatthew R. Ochs  */
cxlflash_queuecommand(struct Scsi_Host * host,struct scsi_cmnd * scp)560c21e0bbfSMatthew R. Ochs static int cxlflash_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scp)
561c21e0bbfSMatthew R. Ochs {
562fb67d44dSMatthew R. Ochs 	struct cxlflash_cfg *cfg = shost_priv(host);
563c21e0bbfSMatthew R. Ochs 	struct afu *afu = cfg->afu;
5644392ba49SMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
565479ad8e9SMatthew R. Ochs 	struct afu_cmd *cmd = sc_to_afuci(scp);
5669d89326cSMatthew R. Ochs 	struct scatterlist *sg = scsi_sglist(scp);
5671dd0c0e4SMatthew R. Ochs 	int hwq_index = cmd_to_target_hwq(host, scp, afu);
5681dd0c0e4SMatthew R. Ochs 	struct hwq *hwq = get_hwq(afu, hwq_index);
5699d89326cSMatthew R. Ochs 	u16 req_flags = SISL_REQ_FLAGS_SUP_UNDERRUN;
570c21e0bbfSMatthew R. Ochs 	ulong lock_flags;
571c21e0bbfSMatthew R. Ochs 	int rc = 0;
572c21e0bbfSMatthew R. Ochs 
5734392ba49SMatthew R. Ochs 	dev_dbg_ratelimited(dev, "%s: (scp=%p) %d/%d/%d/%llu "
574fb67d44dSMatthew R. Ochs 			    "cdb=(%08x-%08x-%08x-%08x)\n",
575c21e0bbfSMatthew R. Ochs 			    __func__, scp, host->host_no, scp->device->channel,
576c21e0bbfSMatthew R. Ochs 			    scp->device->id, scp->device->lun,
577c21e0bbfSMatthew R. Ochs 			    get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
578c21e0bbfSMatthew R. Ochs 			    get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
579c21e0bbfSMatthew R. Ochs 			    get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
580c21e0bbfSMatthew R. Ochs 			    get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
581c21e0bbfSMatthew R. Ochs 
582018d1dc9SMatthew R. Ochs 	/*
583018d1dc9SMatthew R. Ochs 	 * If a Task Management Function is active, wait for it to complete
584c21e0bbfSMatthew R. Ochs 	 * before continuing with regular commands.
585c21e0bbfSMatthew R. Ochs 	 */
586018d1dc9SMatthew R. Ochs 	spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
587c21e0bbfSMatthew R. Ochs 	if (cfg->tmf_active) {
588018d1dc9SMatthew R. Ochs 		spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
589c21e0bbfSMatthew R. Ochs 		rc = SCSI_MLQUEUE_HOST_BUSY;
590c21e0bbfSMatthew R. Ochs 		goto out;
591c21e0bbfSMatthew R. Ochs 	}
592018d1dc9SMatthew R. Ochs 	spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
593c21e0bbfSMatthew R. Ochs 
5945cdac81aSMatthew R. Ochs 	switch (cfg->state) {
595323e3342SMatthew R. Ochs 	case STATE_PROBING:
596323e3342SMatthew R. Ochs 	case STATE_PROBED:
597439e85c1SMatthew R. Ochs 	case STATE_RESET:
598fb67d44dSMatthew R. Ochs 		dev_dbg_ratelimited(dev, "%s: device is in reset\n", __func__);
5995cdac81aSMatthew R. Ochs 		rc = SCSI_MLQUEUE_HOST_BUSY;
6005cdac81aSMatthew R. Ochs 		goto out;
6015cdac81aSMatthew R. Ochs 	case STATE_FAILTERM:
602fb67d44dSMatthew R. Ochs 		dev_dbg_ratelimited(dev, "%s: device has failed\n", __func__);
6035cdac81aSMatthew R. Ochs 		scp->result = (DID_NO_CONNECT << 16);
604e82d6b17SBart Van Assche 		scsi_done(scp);
6055cdac81aSMatthew R. Ochs 		rc = 0;
6065cdac81aSMatthew R. Ochs 		goto out;
6075cdac81aSMatthew R. Ochs 	default:
608e0f76ad1SUma Krishnan 		atomic_inc(&afu->cmds_active);
6095cdac81aSMatthew R. Ochs 		break;
6105cdac81aSMatthew R. Ochs 	}
6115cdac81aSMatthew R. Ochs 
6129d89326cSMatthew R. Ochs 	if (likely(sg)) {
61350b787f7SMatthew R. Ochs 		cmd->rcb.data_len = sg->length;
61450b787f7SMatthew R. Ochs 		cmd->rcb.data_ea = (uintptr_t)sg_virt(sg);
6159d89326cSMatthew R. Ochs 	}
6169d89326cSMatthew R. Ochs 
617fe7f9698SMatthew R. Ochs 	cmd->scp = scp;
6189d89326cSMatthew R. Ochs 	cmd->parent = afu;
6191dd0c0e4SMatthew R. Ochs 	cmd->hwq_index = hwq_index;
6209d89326cSMatthew R. Ochs 
62196cf727fSUma Krishnan 	cmd->sa.ioasc = 0;
622bfc0bab1SUma Krishnan 	cmd->rcb.ctx_id = hwq->ctx_hndl;
6235fbb96c8SMatthew R. Ochs 	cmd->rcb.msi = SISL_MSI_RRQ_UPDATED;
6248fa4f177SMatthew R. Ochs 	cmd->rcb.port_sel = CHAN2PORTMASK(scp->device->channel);
625c21e0bbfSMatthew R. Ochs 	cmd->rcb.lun_id = lun_to_lunid(scp->device->lun);
626c21e0bbfSMatthew R. Ochs 
627c21e0bbfSMatthew R. Ochs 	if (scp->sc_data_direction == DMA_TO_DEVICE)
6289d89326cSMatthew R. Ochs 		req_flags |= SISL_REQ_FLAGS_HOST_WRITE;
629c21e0bbfSMatthew R. Ochs 
6309d89326cSMatthew R. Ochs 	cmd->rcb.req_flags = req_flags;
631c21e0bbfSMatthew R. Ochs 	memcpy(cmd->rcb.cdb, scp->cmnd, sizeof(cmd->rcb.cdb));
632c21e0bbfSMatthew R. Ochs 
63348b4be36SMatthew R. Ochs 	rc = afu->send_cmd(afu, cmd);
634e0f76ad1SUma Krishnan 	atomic_dec(&afu->cmds_active);
635c21e0bbfSMatthew R. Ochs out:
636c21e0bbfSMatthew R. Ochs 	return rc;
637c21e0bbfSMatthew R. Ochs }
638c21e0bbfSMatthew R. Ochs 
639c21e0bbfSMatthew R. Ochs /**
640c21e0bbfSMatthew R. Ochs  * cxlflash_wait_for_pci_err_recovery() - wait for error recovery during probe
6411284fb0cSMatthew R. Ochs  * @cfg:	Internal structure associated with the host.
642c21e0bbfSMatthew R. Ochs  */
cxlflash_wait_for_pci_err_recovery(struct cxlflash_cfg * cfg)643c21e0bbfSMatthew R. Ochs static void cxlflash_wait_for_pci_err_recovery(struct cxlflash_cfg *cfg)
644c21e0bbfSMatthew R. Ochs {
645c21e0bbfSMatthew R. Ochs 	struct pci_dev *pdev = cfg->dev;
646c21e0bbfSMatthew R. Ochs 
647c21e0bbfSMatthew R. Ochs 	if (pci_channel_offline(pdev))
648439e85c1SMatthew R. Ochs 		wait_event_timeout(cfg->reset_waitq,
649c21e0bbfSMatthew R. Ochs 				   !pci_channel_offline(pdev),
650c21e0bbfSMatthew R. Ochs 				   CXLFLASH_PCI_ERROR_RECOVERY_TIMEOUT);
651c21e0bbfSMatthew R. Ochs }
652c21e0bbfSMatthew R. Ochs 
653c21e0bbfSMatthew R. Ochs /**
654c21e0bbfSMatthew R. Ochs  * free_mem() - free memory associated with the AFU
6551284fb0cSMatthew R. Ochs  * @cfg:	Internal structure associated with the host.
656c21e0bbfSMatthew R. Ochs  */
free_mem(struct cxlflash_cfg * cfg)657c21e0bbfSMatthew R. Ochs static void free_mem(struct cxlflash_cfg *cfg)
658c21e0bbfSMatthew R. Ochs {
659c21e0bbfSMatthew R. Ochs 	struct afu *afu = cfg->afu;
660c21e0bbfSMatthew R. Ochs 
661c21e0bbfSMatthew R. Ochs 	if (cfg->afu) {
662c21e0bbfSMatthew R. Ochs 		free_pages((ulong)afu, get_order(sizeof(struct afu)));
663c21e0bbfSMatthew R. Ochs 		cfg->afu = NULL;
664c21e0bbfSMatthew R. Ochs 	}
665c21e0bbfSMatthew R. Ochs }
666c21e0bbfSMatthew R. Ochs 
667c21e0bbfSMatthew R. Ochs /**
6680b09e711SUma Krishnan  * cxlflash_reset_sync() - synchronizing point for asynchronous resets
6690b09e711SUma Krishnan  * @cfg:	Internal structure associated with the host.
6700b09e711SUma Krishnan  */
cxlflash_reset_sync(struct cxlflash_cfg * cfg)6710b09e711SUma Krishnan static void cxlflash_reset_sync(struct cxlflash_cfg *cfg)
6720b09e711SUma Krishnan {
6730b09e711SUma Krishnan 	if (cfg->async_reset_cookie == 0)
6740b09e711SUma Krishnan 		return;
6750b09e711SUma Krishnan 
6760b09e711SUma Krishnan 	/* Wait until all async calls prior to this cookie have completed */
6770b09e711SUma Krishnan 	async_synchronize_cookie(cfg->async_reset_cookie + 1);
6780b09e711SUma Krishnan 	cfg->async_reset_cookie = 0;
6790b09e711SUma Krishnan }
6800b09e711SUma Krishnan 
6810b09e711SUma Krishnan /**
682c21e0bbfSMatthew R. Ochs  * stop_afu() - stops the AFU command timers and unmaps the MMIO space
6831284fb0cSMatthew R. Ochs  * @cfg:	Internal structure associated with the host.
684c21e0bbfSMatthew R. Ochs  *
685c21e0bbfSMatthew R. Ochs  * Safe to call with AFU in a partially allocated/initialized state.
686ee91e332SManoj Kumar  *
6870df5bef7SUma Krishnan  * Cancels scheduled worker threads, waits for any active internal AFU
688cba06e6dSMatthew R. Ochs  * commands to timeout, disables IRQ polling and then unmaps the MMIO space.
689c21e0bbfSMatthew R. Ochs  */
stop_afu(struct cxlflash_cfg * cfg)690c21e0bbfSMatthew R. Ochs static void stop_afu(struct cxlflash_cfg *cfg)
691c21e0bbfSMatthew R. Ochs {
692c21e0bbfSMatthew R. Ochs 	struct afu *afu = cfg->afu;
693bfc0bab1SUma Krishnan 	struct hwq *hwq;
694bfc0bab1SUma Krishnan 	int i;
695c21e0bbfSMatthew R. Ochs 
6960df5bef7SUma Krishnan 	cancel_work_sync(&cfg->work_q);
6970b09e711SUma Krishnan 	if (!current_is_async())
6980b09e711SUma Krishnan 		cxlflash_reset_sync(cfg);
6990df5bef7SUma Krishnan 
700c21e0bbfSMatthew R. Ochs 	if (likely(afu)) {
701de01283bSMatthew R. Ochs 		while (atomic_read(&afu->cmds_active))
702de01283bSMatthew R. Ochs 			ssleep(1);
703bfc0bab1SUma Krishnan 
704bfc0bab1SUma Krishnan 		if (afu_is_irqpoll_enabled(afu)) {
7053065267aSMatthew R. Ochs 			for (i = 0; i < afu->num_hwqs; i++) {
706bfc0bab1SUma Krishnan 				hwq = get_hwq(afu, i);
707bfc0bab1SUma Krishnan 
708bfc0bab1SUma Krishnan 				irq_poll_disable(&hwq->irqpoll);
709bfc0bab1SUma Krishnan 			}
710bfc0bab1SUma Krishnan 		}
711bfc0bab1SUma Krishnan 
712c21e0bbfSMatthew R. Ochs 		if (likely(afu->afu_map)) {
71325b8e08eSMatthew R. Ochs 			cfg->ops->psa_unmap(afu->afu_map);
714c21e0bbfSMatthew R. Ochs 			afu->afu_map = NULL;
715c21e0bbfSMatthew R. Ochs 		}
716c21e0bbfSMatthew R. Ochs 	}
717c21e0bbfSMatthew R. Ochs }
718c21e0bbfSMatthew R. Ochs 
719c21e0bbfSMatthew R. Ochs /**
7209526f360SManoj N. Kumar  * term_intr() - disables all AFU interrupts
7211284fb0cSMatthew R. Ochs  * @cfg:	Internal structure associated with the host.
722c21e0bbfSMatthew R. Ochs  * @level:	Depth of allocation, where to begin waterfall tear down.
723bfc0bab1SUma Krishnan  * @index:	Index of the hardware queue.
724c21e0bbfSMatthew R. Ochs  *
725c21e0bbfSMatthew R. Ochs  * Safe to call with AFU/MC in partially allocated/initialized state.
726c21e0bbfSMatthew R. Ochs  */
term_intr(struct cxlflash_cfg * cfg,enum undo_level level,u32 index)727bfc0bab1SUma Krishnan static void term_intr(struct cxlflash_cfg *cfg, enum undo_level level,
728bfc0bab1SUma Krishnan 		      u32 index)
729c21e0bbfSMatthew R. Ochs {
730c21e0bbfSMatthew R. Ochs 	struct afu *afu = cfg->afu;
7314392ba49SMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
732bfc0bab1SUma Krishnan 	struct hwq *hwq;
733c21e0bbfSMatthew R. Ochs 
734bfc0bab1SUma Krishnan 	if (!afu) {
735bfc0bab1SUma Krishnan 		dev_err(dev, "%s: returning with NULL afu\n", __func__);
736bfc0bab1SUma Krishnan 		return;
737bfc0bab1SUma Krishnan 	}
738bfc0bab1SUma Krishnan 
739bfc0bab1SUma Krishnan 	hwq = get_hwq(afu, index);
740bfc0bab1SUma Krishnan 
741b070545dSUma Krishnan 	if (!hwq->ctx_cookie) {
742bfc0bab1SUma Krishnan 		dev_err(dev, "%s: returning with NULL MC\n", __func__);
743c21e0bbfSMatthew R. Ochs 		return;
744c21e0bbfSMatthew R. Ochs 	}
745c21e0bbfSMatthew R. Ochs 
746c21e0bbfSMatthew R. Ochs 	switch (level) {
747c21e0bbfSMatthew R. Ochs 	case UNMAP_THREE:
748bfc0bab1SUma Krishnan 		/* SISL_MSI_ASYNC_ERROR is setup only for the primary HWQ */
749bfc0bab1SUma Krishnan 		if (index == PRIMARY_HWQ)
75025b8e08eSMatthew R. Ochs 			cfg->ops->unmap_afu_irq(hwq->ctx_cookie, 3, hwq);
751df561f66SGustavo A. R. Silva 		fallthrough;
752c21e0bbfSMatthew R. Ochs 	case UNMAP_TWO:
75325b8e08eSMatthew R. Ochs 		cfg->ops->unmap_afu_irq(hwq->ctx_cookie, 2, hwq);
754df561f66SGustavo A. R. Silva 		fallthrough;
755c21e0bbfSMatthew R. Ochs 	case UNMAP_ONE:
75625b8e08eSMatthew R. Ochs 		cfg->ops->unmap_afu_irq(hwq->ctx_cookie, 1, hwq);
757df561f66SGustavo A. R. Silva 		fallthrough;
758c21e0bbfSMatthew R. Ochs 	case FREE_IRQ:
75925b8e08eSMatthew R. Ochs 		cfg->ops->free_afu_irqs(hwq->ctx_cookie);
760df561f66SGustavo A. R. Silva 		fallthrough;
7619526f360SManoj N. Kumar 	case UNDO_NOOP:
7629526f360SManoj N. Kumar 		/* No action required */
7639526f360SManoj N. Kumar 		break;
764c21e0bbfSMatthew R. Ochs 	}
765c21e0bbfSMatthew R. Ochs }
766c21e0bbfSMatthew R. Ochs 
767c21e0bbfSMatthew R. Ochs /**
7689526f360SManoj N. Kumar  * term_mc() - terminates the master context
7699526f360SManoj N. Kumar  * @cfg:	Internal structure associated with the host.
770bfc0bab1SUma Krishnan  * @index:	Index of the hardware queue.
7719526f360SManoj N. Kumar  *
7729526f360SManoj N. Kumar  * Safe to call with AFU/MC in partially allocated/initialized state.
7739526f360SManoj N. Kumar  */
term_mc(struct cxlflash_cfg * cfg,u32 index)774bfc0bab1SUma Krishnan static void term_mc(struct cxlflash_cfg *cfg, u32 index)
7759526f360SManoj N. Kumar {
7769526f360SManoj N. Kumar 	struct afu *afu = cfg->afu;
7779526f360SManoj N. Kumar 	struct device *dev = &cfg->dev->dev;
778bfc0bab1SUma Krishnan 	struct hwq *hwq;
779a1ea04b3SUma Krishnan 	ulong lock_flags;
7809526f360SManoj N. Kumar 
781bfc0bab1SUma Krishnan 	if (!afu) {
782bfc0bab1SUma Krishnan 		dev_err(dev, "%s: returning with NULL afu\n", __func__);
7839526f360SManoj N. Kumar 		return;
7849526f360SManoj N. Kumar 	}
7859526f360SManoj N. Kumar 
786bfc0bab1SUma Krishnan 	hwq = get_hwq(afu, index);
787bfc0bab1SUma Krishnan 
788b070545dSUma Krishnan 	if (!hwq->ctx_cookie) {
789bfc0bab1SUma Krishnan 		dev_err(dev, "%s: returning with NULL MC\n", __func__);
790bfc0bab1SUma Krishnan 		return;
791bfc0bab1SUma Krishnan 	}
792bfc0bab1SUma Krishnan 
79325b8e08eSMatthew R. Ochs 	WARN_ON(cfg->ops->stop_context(hwq->ctx_cookie));
794bfc0bab1SUma Krishnan 	if (index != PRIMARY_HWQ)
79525b8e08eSMatthew R. Ochs 		WARN_ON(cfg->ops->release_context(hwq->ctx_cookie));
796b070545dSUma Krishnan 	hwq->ctx_cookie = NULL;
797a1ea04b3SUma Krishnan 
798d2d354a6SUma Krishnan 	spin_lock_irqsave(&hwq->hrrq_slock, lock_flags);
799d2d354a6SUma Krishnan 	hwq->hrrq_online = false;
800d2d354a6SUma Krishnan 	spin_unlock_irqrestore(&hwq->hrrq_slock, lock_flags);
801d2d354a6SUma Krishnan 
802a1ea04b3SUma Krishnan 	spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
803a1ea04b3SUma Krishnan 	flush_pending_cmds(hwq);
804a1ea04b3SUma Krishnan 	spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
8059526f360SManoj N. Kumar }
8069526f360SManoj N. Kumar 
8079526f360SManoj N. Kumar /**
808c21e0bbfSMatthew R. Ochs  * term_afu() - terminates the AFU
8091284fb0cSMatthew R. Ochs  * @cfg:	Internal structure associated with the host.
810c21e0bbfSMatthew R. Ochs  *
811c21e0bbfSMatthew R. Ochs  * Safe to call with AFU/MC in partially allocated/initialized state.
812c21e0bbfSMatthew R. Ochs  */
term_afu(struct cxlflash_cfg * cfg)813c21e0bbfSMatthew R. Ochs static void term_afu(struct cxlflash_cfg *cfg)
814c21e0bbfSMatthew R. Ochs {
815fb67d44dSMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
816bfc0bab1SUma Krishnan 	int k;
817fb67d44dSMatthew R. Ochs 
8189526f360SManoj N. Kumar 	/*
8199526f360SManoj N. Kumar 	 * Tear down is carefully orchestrated to ensure
8209526f360SManoj N. Kumar 	 * no interrupts can come in when the problem state
8219526f360SManoj N. Kumar 	 * area is unmapped.
8229526f360SManoj N. Kumar 	 *
823bfc0bab1SUma Krishnan 	 * 1) Disable all AFU interrupts for each master
8249526f360SManoj N. Kumar 	 * 2) Unmap the problem state area
825bfc0bab1SUma Krishnan 	 * 3) Stop each master context
8269526f360SManoj N. Kumar 	 */
8273065267aSMatthew R. Ochs 	for (k = cfg->afu->num_hwqs - 1; k >= 0; k--)
828bfc0bab1SUma Krishnan 		term_intr(cfg, UNMAP_THREE, k);
829bfc0bab1SUma Krishnan 
830c21e0bbfSMatthew R. Ochs 	stop_afu(cfg);
831c21e0bbfSMatthew R. Ochs 
8323065267aSMatthew R. Ochs 	for (k = cfg->afu->num_hwqs - 1; k >= 0; k--)
833bfc0bab1SUma Krishnan 		term_mc(cfg, k);
8346ded8b3cSUma Krishnan 
835fb67d44dSMatthew R. Ochs 	dev_dbg(dev, "%s: returning\n", __func__);
836c21e0bbfSMatthew R. Ochs }
837c21e0bbfSMatthew R. Ochs 
838c21e0bbfSMatthew R. Ochs /**
839704c4b0dSUma Krishnan  * notify_shutdown() - notifies device of pending shutdown
840704c4b0dSUma Krishnan  * @cfg:	Internal structure associated with the host.
841704c4b0dSUma Krishnan  * @wait:	Whether to wait for shutdown processing to complete.
842704c4b0dSUma Krishnan  *
843704c4b0dSUma Krishnan  * This function will notify the AFU that the adapter is being shutdown
844704c4b0dSUma Krishnan  * and will wait for shutdown processing to complete if wait is true.
845704c4b0dSUma Krishnan  * This notification should flush pending I/Os to the device and halt
846704c4b0dSUma Krishnan  * further I/Os until the next AFU reset is issued and device restarted.
847704c4b0dSUma Krishnan  */
notify_shutdown(struct cxlflash_cfg * cfg,bool wait)848704c4b0dSUma Krishnan static void notify_shutdown(struct cxlflash_cfg *cfg, bool wait)
849704c4b0dSUma Krishnan {
850704c4b0dSUma Krishnan 	struct afu *afu = cfg->afu;
851704c4b0dSUma Krishnan 	struct device *dev = &cfg->dev->dev;
852704c4b0dSUma Krishnan 	struct dev_dependent_vals *ddv;
8530aa14887SMatthew R. Ochs 	__be64 __iomem *fc_port_regs;
854704c4b0dSUma Krishnan 	u64 reg, status;
855704c4b0dSUma Krishnan 	int i, retry_cnt = 0;
856704c4b0dSUma Krishnan 
857704c4b0dSUma Krishnan 	ddv = (struct dev_dependent_vals *)cfg->dev_id->driver_data;
858704c4b0dSUma Krishnan 	if (!(ddv->flags & CXLFLASH_NOTIFY_SHUTDOWN))
859704c4b0dSUma Krishnan 		return;
860704c4b0dSUma Krishnan 
8611bd2b282SUma Krishnan 	if (!afu || !afu->afu_map) {
862fb67d44dSMatthew R. Ochs 		dev_dbg(dev, "%s: Problem state area not mapped\n", __func__);
8631bd2b282SUma Krishnan 		return;
8641bd2b282SUma Krishnan 	}
8651bd2b282SUma Krishnan 
866704c4b0dSUma Krishnan 	/* Notify AFU */
86778ae028eSMatthew R. Ochs 	for (i = 0; i < cfg->num_fc_ports; i++) {
8680aa14887SMatthew R. Ochs 		fc_port_regs = get_fc_port_regs(cfg, i);
8690aa14887SMatthew R. Ochs 
8700aa14887SMatthew R. Ochs 		reg = readq_be(&fc_port_regs[FC_CONFIG2 / 8]);
871704c4b0dSUma Krishnan 		reg |= SISL_FC_SHUTDOWN_NORMAL;
8720aa14887SMatthew R. Ochs 		writeq_be(reg, &fc_port_regs[FC_CONFIG2 / 8]);
873704c4b0dSUma Krishnan 	}
874704c4b0dSUma Krishnan 
875704c4b0dSUma Krishnan 	if (!wait)
876704c4b0dSUma Krishnan 		return;
877704c4b0dSUma Krishnan 
878704c4b0dSUma Krishnan 	/* Wait up to 1.5 seconds for shutdown processing to complete */
87978ae028eSMatthew R. Ochs 	for (i = 0; i < cfg->num_fc_ports; i++) {
8800aa14887SMatthew R. Ochs 		fc_port_regs = get_fc_port_regs(cfg, i);
881704c4b0dSUma Krishnan 		retry_cnt = 0;
8820aa14887SMatthew R. Ochs 
883704c4b0dSUma Krishnan 		while (true) {
8840aa14887SMatthew R. Ochs 			status = readq_be(&fc_port_regs[FC_STATUS / 8]);
885704c4b0dSUma Krishnan 			if (status & SISL_STATUS_SHUTDOWN_COMPLETE)
886704c4b0dSUma Krishnan 				break;
887704c4b0dSUma Krishnan 			if (++retry_cnt >= MC_RETRY_CNT) {
888704c4b0dSUma Krishnan 				dev_dbg(dev, "%s: port %d shutdown processing "
889704c4b0dSUma Krishnan 					"not yet completed\n", __func__, i);
890704c4b0dSUma Krishnan 				break;
891704c4b0dSUma Krishnan 			}
892704c4b0dSUma Krishnan 			msleep(100 * retry_cnt);
893704c4b0dSUma Krishnan 		}
894704c4b0dSUma Krishnan 	}
895704c4b0dSUma Krishnan }
896704c4b0dSUma Krishnan 
897704c4b0dSUma Krishnan /**
898a834a36bSUma Krishnan  * cxlflash_get_minor() - gets the first available minor number
899a834a36bSUma Krishnan  *
900a834a36bSUma Krishnan  * Return: Unique minor number that can be used to create the character device.
901a834a36bSUma Krishnan  */
cxlflash_get_minor(void)902a834a36bSUma Krishnan static int cxlflash_get_minor(void)
903a834a36bSUma Krishnan {
904a834a36bSUma Krishnan 	int minor;
905a834a36bSUma Krishnan 	long bit;
906a834a36bSUma Krishnan 
907a834a36bSUma Krishnan 	bit = find_first_zero_bit(cxlflash_minor, CXLFLASH_MAX_ADAPTERS);
908a834a36bSUma Krishnan 	if (bit >= CXLFLASH_MAX_ADAPTERS)
909a834a36bSUma Krishnan 		return -1;
910a834a36bSUma Krishnan 
911a834a36bSUma Krishnan 	minor = bit & MINORMASK;
912a834a36bSUma Krishnan 	set_bit(minor, cxlflash_minor);
913a834a36bSUma Krishnan 	return minor;
914a834a36bSUma Krishnan }
915a834a36bSUma Krishnan 
916a834a36bSUma Krishnan /**
917a834a36bSUma Krishnan  * cxlflash_put_minor() - releases the minor number
918a834a36bSUma Krishnan  * @minor:	Minor number that is no longer needed.
919a834a36bSUma Krishnan  */
cxlflash_put_minor(int minor)920a834a36bSUma Krishnan static void cxlflash_put_minor(int minor)
921a834a36bSUma Krishnan {
922a834a36bSUma Krishnan 	clear_bit(minor, cxlflash_minor);
923a834a36bSUma Krishnan }
924a834a36bSUma Krishnan 
925a834a36bSUma Krishnan /**
926a834a36bSUma Krishnan  * cxlflash_release_chrdev() - release the character device for the host
927a834a36bSUma Krishnan  * @cfg:	Internal structure associated with the host.
928a834a36bSUma Krishnan  */
cxlflash_release_chrdev(struct cxlflash_cfg * cfg)929a834a36bSUma Krishnan static void cxlflash_release_chrdev(struct cxlflash_cfg *cfg)
930a834a36bSUma Krishnan {
931a834a36bSUma Krishnan 	device_unregister(cfg->chardev);
932a834a36bSUma Krishnan 	cfg->chardev = NULL;
933a834a36bSUma Krishnan 	cdev_del(&cfg->cdev);
934a834a36bSUma Krishnan 	cxlflash_put_minor(MINOR(cfg->cdev.dev));
935a834a36bSUma Krishnan }
936a834a36bSUma Krishnan 
937a834a36bSUma Krishnan /**
938c21e0bbfSMatthew R. Ochs  * cxlflash_remove() - PCI entry point to tear down host
939c21e0bbfSMatthew R. Ochs  * @pdev:	PCI device associated with the host.
940c21e0bbfSMatthew R. Ochs  *
941323e3342SMatthew R. Ochs  * Safe to use as a cleanup in partially allocated/initialized state. Note that
942323e3342SMatthew R. Ochs  * the reset_waitq is flushed as part of the stop/termination of user contexts.
943c21e0bbfSMatthew R. Ochs  */
cxlflash_remove(struct pci_dev * pdev)944c21e0bbfSMatthew R. Ochs static void cxlflash_remove(struct pci_dev *pdev)
945c21e0bbfSMatthew R. Ochs {
946c21e0bbfSMatthew R. Ochs 	struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
947fb67d44dSMatthew R. Ochs 	struct device *dev = &pdev->dev;
948c21e0bbfSMatthew R. Ochs 	ulong lock_flags;
949c21e0bbfSMatthew R. Ochs 
950babf985dSUma Krishnan 	if (!pci_is_enabled(pdev)) {
951fb67d44dSMatthew R. Ochs 		dev_dbg(dev, "%s: Device is disabled\n", __func__);
952babf985dSUma Krishnan 		return;
953babf985dSUma Krishnan 	}
954babf985dSUma Krishnan 
955a3feb6efSUma Krishnan 	/* Yield to running recovery threads before continuing with remove */
956a3feb6efSUma Krishnan 	wait_event(cfg->reset_waitq, cfg->state != STATE_RESET &&
957a3feb6efSUma Krishnan 				     cfg->state != STATE_PROBING);
958018d1dc9SMatthew R. Ochs 	spin_lock_irqsave(&cfg->tmf_slock, lock_flags);
959c21e0bbfSMatthew R. Ochs 	if (cfg->tmf_active)
960018d1dc9SMatthew R. Ochs 		wait_event_interruptible_lock_irq(cfg->tmf_waitq,
961018d1dc9SMatthew R. Ochs 						  !cfg->tmf_active,
962018d1dc9SMatthew R. Ochs 						  cfg->tmf_slock);
963018d1dc9SMatthew R. Ochs 	spin_unlock_irqrestore(&cfg->tmf_slock, lock_flags);
964c21e0bbfSMatthew R. Ochs 
965704c4b0dSUma Krishnan 	/* Notify AFU and wait for shutdown processing to complete */
966704c4b0dSUma Krishnan 	notify_shutdown(cfg, true);
967704c4b0dSUma Krishnan 
9685cdac81aSMatthew R. Ochs 	cfg->state = STATE_FAILTERM;
96965be2c79SMatthew R. Ochs 	cxlflash_stop_term_user_contexts(cfg);
9705cdac81aSMatthew R. Ochs 
971c21e0bbfSMatthew R. Ochs 	switch (cfg->init_state) {
972a834a36bSUma Krishnan 	case INIT_STATE_CDEV:
973a834a36bSUma Krishnan 		cxlflash_release_chrdev(cfg);
974df561f66SGustavo A. R. Silva 		fallthrough;
975c21e0bbfSMatthew R. Ochs 	case INIT_STATE_SCSI:
97665be2c79SMatthew R. Ochs 		cxlflash_term_local_luns(cfg);
977c21e0bbfSMatthew R. Ochs 		scsi_remove_host(cfg->host);
978df561f66SGustavo A. R. Silva 		fallthrough;
979c21e0bbfSMatthew R. Ochs 	case INIT_STATE_AFU:
980b45cdbafSManoj Kumar 		term_afu(cfg);
981df561f66SGustavo A. R. Silva 		fallthrough;
982c21e0bbfSMatthew R. Ochs 	case INIT_STATE_PCI:
98348e077dbSUma Krishnan 		cfg->ops->destroy_afu(cfg->afu_cookie);
984c21e0bbfSMatthew R. Ochs 		pci_disable_device(pdev);
985df561f66SGustavo A. R. Silva 		fallthrough;
986c21e0bbfSMatthew R. Ochs 	case INIT_STATE_NONE:
987c21e0bbfSMatthew R. Ochs 		free_mem(cfg);
9888b5b1e87SMatthew R. Ochs 		scsi_host_put(cfg->host);
989c21e0bbfSMatthew R. Ochs 		break;
990c21e0bbfSMatthew R. Ochs 	}
991c21e0bbfSMatthew R. Ochs 
992fb67d44dSMatthew R. Ochs 	dev_dbg(dev, "%s: returning\n", __func__);
993c21e0bbfSMatthew R. Ochs }
994c21e0bbfSMatthew R. Ochs 
995c21e0bbfSMatthew R. Ochs /**
996c21e0bbfSMatthew R. Ochs  * alloc_mem() - allocates the AFU and its command pool
9971284fb0cSMatthew R. Ochs  * @cfg:	Internal structure associated with the host.
998c21e0bbfSMatthew R. Ochs  *
999c21e0bbfSMatthew R. Ochs  * A partially allocated state remains on failure.
1000c21e0bbfSMatthew R. Ochs  *
1001c21e0bbfSMatthew R. Ochs  * Return:
1002c21e0bbfSMatthew R. Ochs  *	0 on success
1003c21e0bbfSMatthew R. Ochs  *	-ENOMEM on failure to allocate memory
1004c21e0bbfSMatthew R. Ochs  */
alloc_mem(struct cxlflash_cfg * cfg)1005c21e0bbfSMatthew R. Ochs static int alloc_mem(struct cxlflash_cfg *cfg)
1006c21e0bbfSMatthew R. Ochs {
1007c21e0bbfSMatthew R. Ochs 	int rc = 0;
10084392ba49SMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
1009c21e0bbfSMatthew R. Ochs 
1010696d0b0cSMatthew R. Ochs 	/* AFU is ~28k, i.e. only one 64k page or up to seven 4k pages */
1011c21e0bbfSMatthew R. Ochs 	cfg->afu = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1012c21e0bbfSMatthew R. Ochs 					    get_order(sizeof(struct afu)));
1013c21e0bbfSMatthew R. Ochs 	if (unlikely(!cfg->afu)) {
10144392ba49SMatthew R. Ochs 		dev_err(dev, "%s: cannot get %d free pages\n",
1015c21e0bbfSMatthew R. Ochs 			__func__, get_order(sizeof(struct afu)));
1016c21e0bbfSMatthew R. Ochs 		rc = -ENOMEM;
1017c21e0bbfSMatthew R. Ochs 		goto out;
1018c21e0bbfSMatthew R. Ochs 	}
1019c21e0bbfSMatthew R. Ochs 	cfg->afu->parent = cfg;
10203065267aSMatthew R. Ochs 	cfg->afu->desired_hwqs = CXLFLASH_DEF_HWQS;
1021c21e0bbfSMatthew R. Ochs 	cfg->afu->afu_map = NULL;
1022c21e0bbfSMatthew R. Ochs out:
1023c21e0bbfSMatthew R. Ochs 	return rc;
1024c21e0bbfSMatthew R. Ochs }
1025c21e0bbfSMatthew R. Ochs 
1026c21e0bbfSMatthew R. Ochs /**
1027c21e0bbfSMatthew R. Ochs  * init_pci() - initializes the host as a PCI device
10281284fb0cSMatthew R. Ochs  * @cfg:	Internal structure associated with the host.
1029c21e0bbfSMatthew R. Ochs  *
10301284fb0cSMatthew R. Ochs  * Return: 0 on success, -errno on failure
1031c21e0bbfSMatthew R. Ochs  */
init_pci(struct cxlflash_cfg * cfg)1032c21e0bbfSMatthew R. Ochs static int init_pci(struct cxlflash_cfg *cfg)
1033c21e0bbfSMatthew R. Ochs {
1034c21e0bbfSMatthew R. Ochs 	struct pci_dev *pdev = cfg->dev;
1035fb67d44dSMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
1036c21e0bbfSMatthew R. Ochs 	int rc = 0;
1037c21e0bbfSMatthew R. Ochs 
1038c21e0bbfSMatthew R. Ochs 	rc = pci_enable_device(pdev);
1039c21e0bbfSMatthew R. Ochs 	if (rc || pci_channel_offline(pdev)) {
1040c21e0bbfSMatthew R. Ochs 		if (pci_channel_offline(pdev)) {
1041c21e0bbfSMatthew R. Ochs 			cxlflash_wait_for_pci_err_recovery(cfg);
1042c21e0bbfSMatthew R. Ochs 			rc = pci_enable_device(pdev);
1043c21e0bbfSMatthew R. Ochs 		}
1044c21e0bbfSMatthew R. Ochs 
1045c21e0bbfSMatthew R. Ochs 		if (rc) {
1046fb67d44dSMatthew R. Ochs 			dev_err(dev, "%s: Cannot enable adapter\n", __func__);
1047c21e0bbfSMatthew R. Ochs 			cxlflash_wait_for_pci_err_recovery(cfg);
1048961487e4SManoj N. Kumar 			goto out;
1049c21e0bbfSMatthew R. Ochs 		}
1050c21e0bbfSMatthew R. Ochs 	}
1051c21e0bbfSMatthew R. Ochs 
1052c21e0bbfSMatthew R. Ochs out:
1053fb67d44dSMatthew R. Ochs 	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
1054c21e0bbfSMatthew R. Ochs 	return rc;
1055c21e0bbfSMatthew R. Ochs }
1056c21e0bbfSMatthew R. Ochs 
1057c21e0bbfSMatthew R. Ochs /**
1058c21e0bbfSMatthew R. Ochs  * init_scsi() - adds the host to the SCSI stack and kicks off host scan
10591284fb0cSMatthew R. Ochs  * @cfg:	Internal structure associated with the host.
1060c21e0bbfSMatthew R. Ochs  *
10611284fb0cSMatthew R. Ochs  * Return: 0 on success, -errno on failure
1062c21e0bbfSMatthew R. Ochs  */
init_scsi(struct cxlflash_cfg * cfg)1063c21e0bbfSMatthew R. Ochs static int init_scsi(struct cxlflash_cfg *cfg)
1064c21e0bbfSMatthew R. Ochs {
1065c21e0bbfSMatthew R. Ochs 	struct pci_dev *pdev = cfg->dev;
1066fb67d44dSMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
1067c21e0bbfSMatthew R. Ochs 	int rc = 0;
1068c21e0bbfSMatthew R. Ochs 
1069c21e0bbfSMatthew R. Ochs 	rc = scsi_add_host(cfg->host, &pdev->dev);
1070c21e0bbfSMatthew R. Ochs 	if (rc) {
1071fb67d44dSMatthew R. Ochs 		dev_err(dev, "%s: scsi_add_host failed rc=%d\n", __func__, rc);
1072c21e0bbfSMatthew R. Ochs 		goto out;
1073c21e0bbfSMatthew R. Ochs 	}
1074c21e0bbfSMatthew R. Ochs 
1075c21e0bbfSMatthew R. Ochs 	scsi_scan_host(cfg->host);
1076c21e0bbfSMatthew R. Ochs 
1077c21e0bbfSMatthew R. Ochs out:
1078fb67d44dSMatthew R. Ochs 	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
1079c21e0bbfSMatthew R. Ochs 	return rc;
1080c21e0bbfSMatthew R. Ochs }
1081c21e0bbfSMatthew R. Ochs 
1082c21e0bbfSMatthew R. Ochs /**
1083c21e0bbfSMatthew R. Ochs  * set_port_online() - transitions the specified host FC port to online state
1084c21e0bbfSMatthew R. Ochs  * @fc_regs:	Top of MMIO region defined for specified port.
1085c21e0bbfSMatthew R. Ochs  *
1086c21e0bbfSMatthew R. Ochs  * The provided MMIO region must be mapped prior to call. Online state means
1087c21e0bbfSMatthew R. Ochs  * that the FC link layer has synced, completed the handshaking process, and
1088c21e0bbfSMatthew R. Ochs  * is ready for login to start.
1089c21e0bbfSMatthew R. Ochs  */
set_port_online(__be64 __iomem * fc_regs)10901786f4a0SMatthew R. Ochs static void set_port_online(__be64 __iomem *fc_regs)
1091c21e0bbfSMatthew R. Ochs {
1092c21e0bbfSMatthew R. Ochs 	u64 cmdcfg;
1093c21e0bbfSMatthew R. Ochs 
1094c21e0bbfSMatthew R. Ochs 	cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
1095c21e0bbfSMatthew R. Ochs 	cmdcfg &= (~FC_MTIP_CMDCONFIG_OFFLINE);	/* clear OFF_LINE */
1096c21e0bbfSMatthew R. Ochs 	cmdcfg |= (FC_MTIP_CMDCONFIG_ONLINE);	/* set ON_LINE */
1097c21e0bbfSMatthew R. Ochs 	writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
1098c21e0bbfSMatthew R. Ochs }
1099c21e0bbfSMatthew R. Ochs 
1100c21e0bbfSMatthew R. Ochs /**
1101c21e0bbfSMatthew R. Ochs  * set_port_offline() - transitions the specified host FC port to offline state
1102c21e0bbfSMatthew R. Ochs  * @fc_regs:	Top of MMIO region defined for specified port.
1103c21e0bbfSMatthew R. Ochs  *
1104c21e0bbfSMatthew R. Ochs  * The provided MMIO region must be mapped prior to call.
1105c21e0bbfSMatthew R. Ochs  */
set_port_offline(__be64 __iomem * fc_regs)11061786f4a0SMatthew R. Ochs static void set_port_offline(__be64 __iomem *fc_regs)
1107c21e0bbfSMatthew R. Ochs {
1108c21e0bbfSMatthew R. Ochs 	u64 cmdcfg;
1109c21e0bbfSMatthew R. Ochs 
1110c21e0bbfSMatthew R. Ochs 	cmdcfg = readq_be(&fc_regs[FC_MTIP_CMDCONFIG / 8]);
1111c21e0bbfSMatthew R. Ochs 	cmdcfg &= (~FC_MTIP_CMDCONFIG_ONLINE);	/* clear ON_LINE */
1112c21e0bbfSMatthew R. Ochs 	cmdcfg |= (FC_MTIP_CMDCONFIG_OFFLINE);	/* set OFF_LINE */
1113c21e0bbfSMatthew R. Ochs 	writeq_be(cmdcfg, &fc_regs[FC_MTIP_CMDCONFIG / 8]);
1114c21e0bbfSMatthew R. Ochs }
1115c21e0bbfSMatthew R. Ochs 
1116c21e0bbfSMatthew R. Ochs /**
1117c21e0bbfSMatthew R. Ochs  * wait_port_online() - waits for the specified host FC port come online
1118c21e0bbfSMatthew R. Ochs  * @fc_regs:	Top of MMIO region defined for specified port.
1119c21e0bbfSMatthew R. Ochs  * @delay_us:	Number of microseconds to delay between reading port status.
1120c21e0bbfSMatthew R. Ochs  * @nretry:	Number of cycles to retry reading port status.
1121c21e0bbfSMatthew R. Ochs  *
1122c21e0bbfSMatthew R. Ochs  * The provided MMIO region must be mapped prior to call. This will timeout
1123c21e0bbfSMatthew R. Ochs  * when the cable is not plugged in.
1124c21e0bbfSMatthew R. Ochs  *
1125c21e0bbfSMatthew R. Ochs  * Return:
1126c21e0bbfSMatthew R. Ochs  *	TRUE (1) when the specified port is online
1127c21e0bbfSMatthew R. Ochs  *	FALSE (0) when the specified port fails to come online after timeout
1128c21e0bbfSMatthew R. Ochs  */
wait_port_online(__be64 __iomem * fc_regs,u32 delay_us,u32 nretry)1129fb67d44dSMatthew R. Ochs static bool wait_port_online(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
1130c21e0bbfSMatthew R. Ochs {
1131c21e0bbfSMatthew R. Ochs 	u64 status;
1132c21e0bbfSMatthew R. Ochs 
1133fb67d44dSMatthew R. Ochs 	WARN_ON(delay_us < 1000);
1134c21e0bbfSMatthew R. Ochs 
1135c21e0bbfSMatthew R. Ochs 	do {
1136c21e0bbfSMatthew R. Ochs 		msleep(delay_us / 1000);
1137c21e0bbfSMatthew R. Ochs 		status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
113805dab432SMatthew R. Ochs 		if (status == U64_MAX)
113905dab432SMatthew R. Ochs 			nretry /= 2;
1140c21e0bbfSMatthew R. Ochs 	} while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_ONLINE &&
1141c21e0bbfSMatthew R. Ochs 		 nretry--);
1142c21e0bbfSMatthew R. Ochs 
1143c21e0bbfSMatthew R. Ochs 	return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_ONLINE);
1144c21e0bbfSMatthew R. Ochs }
1145c21e0bbfSMatthew R. Ochs 
1146c21e0bbfSMatthew R. Ochs /**
1147c21e0bbfSMatthew R. Ochs  * wait_port_offline() - waits for the specified host FC port go offline
1148c21e0bbfSMatthew R. Ochs  * @fc_regs:	Top of MMIO region defined for specified port.
1149c21e0bbfSMatthew R. Ochs  * @delay_us:	Number of microseconds to delay between reading port status.
1150c21e0bbfSMatthew R. Ochs  * @nretry:	Number of cycles to retry reading port status.
1151c21e0bbfSMatthew R. Ochs  *
1152c21e0bbfSMatthew R. Ochs  * The provided MMIO region must be mapped prior to call.
1153c21e0bbfSMatthew R. Ochs  *
1154c21e0bbfSMatthew R. Ochs  * Return:
1155c21e0bbfSMatthew R. Ochs  *	TRUE (1) when the specified port is offline
1156c21e0bbfSMatthew R. Ochs  *	FALSE (0) when the specified port fails to go offline after timeout
1157c21e0bbfSMatthew R. Ochs  */
wait_port_offline(__be64 __iomem * fc_regs,u32 delay_us,u32 nretry)1158fb67d44dSMatthew R. Ochs static bool wait_port_offline(__be64 __iomem *fc_regs, u32 delay_us, u32 nretry)
1159c21e0bbfSMatthew R. Ochs {
1160c21e0bbfSMatthew R. Ochs 	u64 status;
1161c21e0bbfSMatthew R. Ochs 
1162fb67d44dSMatthew R. Ochs 	WARN_ON(delay_us < 1000);
1163c21e0bbfSMatthew R. Ochs 
1164c21e0bbfSMatthew R. Ochs 	do {
1165c21e0bbfSMatthew R. Ochs 		msleep(delay_us / 1000);
1166c21e0bbfSMatthew R. Ochs 		status = readq_be(&fc_regs[FC_MTIP_STATUS / 8]);
116705dab432SMatthew R. Ochs 		if (status == U64_MAX)
116805dab432SMatthew R. Ochs 			nretry /= 2;
1169c21e0bbfSMatthew R. Ochs 	} while ((status & FC_MTIP_STATUS_MASK) != FC_MTIP_STATUS_OFFLINE &&
1170c21e0bbfSMatthew R. Ochs 		 nretry--);
1171c21e0bbfSMatthew R. Ochs 
1172c21e0bbfSMatthew R. Ochs 	return ((status & FC_MTIP_STATUS_MASK) == FC_MTIP_STATUS_OFFLINE);
1173c21e0bbfSMatthew R. Ochs }
1174c21e0bbfSMatthew R. Ochs 
1175c21e0bbfSMatthew R. Ochs /**
1176c21e0bbfSMatthew R. Ochs  * afu_set_wwpn() - configures the WWPN for the specified host FC port
1177c21e0bbfSMatthew R. Ochs  * @afu:	AFU associated with the host that owns the specified FC port.
1178c21e0bbfSMatthew R. Ochs  * @port:	Port number being configured.
1179c21e0bbfSMatthew R. Ochs  * @fc_regs:	Top of MMIO region defined for specified port.
1180c21e0bbfSMatthew R. Ochs  * @wwpn:	The world-wide-port-number previously discovered for port.
1181c21e0bbfSMatthew R. Ochs  *
1182c21e0bbfSMatthew R. Ochs  * The provided MMIO region must be mapped prior to call. As part of the
1183c21e0bbfSMatthew R. Ochs  * sequence to configure the WWPN, the port is toggled offline and then back
1184c21e0bbfSMatthew R. Ochs  * online. This toggling action can cause this routine to delay up to a few
1185c21e0bbfSMatthew R. Ochs  * seconds. When configured to use the internal LUN feature of the AFU, a
1186c21e0bbfSMatthew R. Ochs  * failure to come online is overridden.
1187c21e0bbfSMatthew R. Ochs  */
afu_set_wwpn(struct afu * afu,int port,__be64 __iomem * fc_regs,u64 wwpn)1188f8013261SMatthew R. Ochs static void afu_set_wwpn(struct afu *afu, int port, __be64 __iomem *fc_regs,
11891786f4a0SMatthew R. Ochs 			 u64 wwpn)
1190c21e0bbfSMatthew R. Ochs {
1191fb67d44dSMatthew R. Ochs 	struct cxlflash_cfg *cfg = afu->parent;
1192fb67d44dSMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
1193fb67d44dSMatthew R. Ochs 
1194c21e0bbfSMatthew R. Ochs 	set_port_offline(fc_regs);
1195c21e0bbfSMatthew R. Ochs 	if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1196c21e0bbfSMatthew R. Ochs 			       FC_PORT_STATUS_RETRY_CNT)) {
1197fb67d44dSMatthew R. Ochs 		dev_dbg(dev, "%s: wait on port %d to go offline timed out\n",
1198c21e0bbfSMatthew R. Ochs 			__func__, port);
1199c21e0bbfSMatthew R. Ochs 	}
1200c21e0bbfSMatthew R. Ochs 
1201c21e0bbfSMatthew R. Ochs 	writeq_be(wwpn, &fc_regs[FC_PNAME / 8]);
1202c21e0bbfSMatthew R. Ochs 
1203c21e0bbfSMatthew R. Ochs 	set_port_online(fc_regs);
1204c21e0bbfSMatthew R. Ochs 	if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1205c21e0bbfSMatthew R. Ochs 			      FC_PORT_STATUS_RETRY_CNT)) {
1206fb67d44dSMatthew R. Ochs 		dev_dbg(dev, "%s: wait on port %d to go online timed out\n",
1207c21e0bbfSMatthew R. Ochs 			__func__, port);
1208c21e0bbfSMatthew R. Ochs 	}
1209c21e0bbfSMatthew R. Ochs }
1210c21e0bbfSMatthew R. Ochs 
1211c21e0bbfSMatthew R. Ochs /**
1212c21e0bbfSMatthew R. Ochs  * afu_link_reset() - resets the specified host FC port
1213c21e0bbfSMatthew R. Ochs  * @afu:	AFU associated with the host that owns the specified FC port.
1214c21e0bbfSMatthew R. Ochs  * @port:	Port number being configured.
1215c21e0bbfSMatthew R. Ochs  * @fc_regs:	Top of MMIO region defined for specified port.
1216c21e0bbfSMatthew R. Ochs  *
1217c21e0bbfSMatthew R. Ochs  * The provided MMIO region must be mapped prior to call. The sequence to
1218c21e0bbfSMatthew R. Ochs  * reset the port involves toggling it offline and then back online. This
1219c21e0bbfSMatthew R. Ochs  * action can cause this routine to delay up to a few seconds. An effort
1220c21e0bbfSMatthew R. Ochs  * is made to maintain link with the device by switching to host to use
1221c21e0bbfSMatthew R. Ochs  * the alternate port exclusively while the reset takes place.
1222c21e0bbfSMatthew R. Ochs  * failure to come online is overridden.
1223c21e0bbfSMatthew R. Ochs  */
afu_link_reset(struct afu * afu,int port,__be64 __iomem * fc_regs)12241786f4a0SMatthew R. Ochs static void afu_link_reset(struct afu *afu, int port, __be64 __iomem *fc_regs)
1225c21e0bbfSMatthew R. Ochs {
1226fb67d44dSMatthew R. Ochs 	struct cxlflash_cfg *cfg = afu->parent;
1227fb67d44dSMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
1228c21e0bbfSMatthew R. Ochs 	u64 port_sel;
1229c21e0bbfSMatthew R. Ochs 
1230c21e0bbfSMatthew R. Ochs 	/* first switch the AFU to the other links, if any */
1231c21e0bbfSMatthew R. Ochs 	port_sel = readq_be(&afu->afu_map->global.regs.afu_port_sel);
12324da74db0SDan Carpenter 	port_sel &= ~(1ULL << port);
1233c21e0bbfSMatthew R. Ochs 	writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
1234c21e0bbfSMatthew R. Ochs 	cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
1235c21e0bbfSMatthew R. Ochs 
1236c21e0bbfSMatthew R. Ochs 	set_port_offline(fc_regs);
1237c21e0bbfSMatthew R. Ochs 	if (!wait_port_offline(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1238c21e0bbfSMatthew R. Ochs 			       FC_PORT_STATUS_RETRY_CNT))
1239fb67d44dSMatthew R. Ochs 		dev_err(dev, "%s: wait on port %d to go offline timed out\n",
1240c21e0bbfSMatthew R. Ochs 			__func__, port);
1241c21e0bbfSMatthew R. Ochs 
1242c21e0bbfSMatthew R. Ochs 	set_port_online(fc_regs);
1243c21e0bbfSMatthew R. Ochs 	if (!wait_port_online(fc_regs, FC_PORT_STATUS_RETRY_INTERVAL_US,
1244c21e0bbfSMatthew R. Ochs 			      FC_PORT_STATUS_RETRY_CNT))
1245fb67d44dSMatthew R. Ochs 		dev_err(dev, "%s: wait on port %d to go online timed out\n",
1246c21e0bbfSMatthew R. Ochs 			__func__, port);
1247c21e0bbfSMatthew R. Ochs 
1248c21e0bbfSMatthew R. Ochs 	/* switch back to include this port */
12494da74db0SDan Carpenter 	port_sel |= (1ULL << port);
1250c21e0bbfSMatthew R. Ochs 	writeq_be(port_sel, &afu->afu_map->global.regs.afu_port_sel);
1251c21e0bbfSMatthew R. Ochs 	cxlflash_afu_sync(afu, 0, 0, AFU_GSYNC);
1252c21e0bbfSMatthew R. Ochs 
1253fb67d44dSMatthew R. Ochs 	dev_dbg(dev, "%s: returning port_sel=%016llx\n", __func__, port_sel);
1254c21e0bbfSMatthew R. Ochs }
1255c21e0bbfSMatthew R. Ochs 
1256c21e0bbfSMatthew R. Ochs /**
1257c21e0bbfSMatthew R. Ochs  * afu_err_intr_init() - clears and initializes the AFU for error interrupts
1258c21e0bbfSMatthew R. Ochs  * @afu:	AFU associated with the host.
1259c21e0bbfSMatthew R. Ochs  */
afu_err_intr_init(struct afu * afu)1260c21e0bbfSMatthew R. Ochs static void afu_err_intr_init(struct afu *afu)
1261c21e0bbfSMatthew R. Ochs {
126278ae028eSMatthew R. Ochs 	struct cxlflash_cfg *cfg = afu->parent;
12630aa14887SMatthew R. Ochs 	__be64 __iomem *fc_port_regs;
1264c21e0bbfSMatthew R. Ochs 	int i;
1265bfc0bab1SUma Krishnan 	struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ);
1266c21e0bbfSMatthew R. Ochs 	u64 reg;
1267c21e0bbfSMatthew R. Ochs 
1268c21e0bbfSMatthew R. Ochs 	/* global async interrupts: AFU clears afu_ctrl on context exit
1269c21e0bbfSMatthew R. Ochs 	 * if async interrupts were sent to that context. This prevents
1270c21e0bbfSMatthew R. Ochs 	 * the AFU form sending further async interrupts when
1271c21e0bbfSMatthew R. Ochs 	 * there is
1272c21e0bbfSMatthew R. Ochs 	 * nobody to receive them.
1273c21e0bbfSMatthew R. Ochs 	 */
1274c21e0bbfSMatthew R. Ochs 
1275c21e0bbfSMatthew R. Ochs 	/* mask all */
1276c21e0bbfSMatthew R. Ochs 	writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_mask);
1277bfc0bab1SUma Krishnan 	/* set LISN# to send and point to primary master context */
1278bfc0bab1SUma Krishnan 	reg = ((u64) (((hwq->ctx_hndl << 8) | SISL_MSI_ASYNC_ERROR)) << 40);
1279c21e0bbfSMatthew R. Ochs 
1280c21e0bbfSMatthew R. Ochs 	if (afu->internal_lun)
1281c21e0bbfSMatthew R. Ochs 		reg |= 1;	/* Bit 63 indicates local lun */
1282c21e0bbfSMatthew R. Ochs 	writeq_be(reg, &afu->afu_map->global.regs.afu_ctrl);
1283c21e0bbfSMatthew R. Ochs 	/* clear all */
1284c21e0bbfSMatthew R. Ochs 	writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1285c21e0bbfSMatthew R. Ochs 	/* unmask bits that are of interest */
1286c21e0bbfSMatthew R. Ochs 	/* note: afu can send an interrupt after this step */
1287c21e0bbfSMatthew R. Ochs 	writeq_be(SISL_ASTATUS_MASK, &afu->afu_map->global.regs.aintr_mask);
1288c21e0bbfSMatthew R. Ochs 	/* clear again in case a bit came on after previous clear but before */
1289c21e0bbfSMatthew R. Ochs 	/* unmask */
1290c21e0bbfSMatthew R. Ochs 	writeq_be(-1ULL, &afu->afu_map->global.regs.aintr_clear);
1291c21e0bbfSMatthew R. Ochs 
1292c21e0bbfSMatthew R. Ochs 	/* Clear/Set internal lun bits */
12930aa14887SMatthew R. Ochs 	fc_port_regs = get_fc_port_regs(cfg, 0);
12940aa14887SMatthew R. Ochs 	reg = readq_be(&fc_port_regs[FC_CONFIG2 / 8]);
1295c21e0bbfSMatthew R. Ochs 	reg &= SISL_FC_INTERNAL_MASK;
1296c21e0bbfSMatthew R. Ochs 	if (afu->internal_lun)
1297c21e0bbfSMatthew R. Ochs 		reg |= ((u64)(afu->internal_lun - 1) << SISL_FC_INTERNAL_SHIFT);
12980aa14887SMatthew R. Ochs 	writeq_be(reg, &fc_port_regs[FC_CONFIG2 / 8]);
1299c21e0bbfSMatthew R. Ochs 
1300c21e0bbfSMatthew R. Ochs 	/* now clear FC errors */
130178ae028eSMatthew R. Ochs 	for (i = 0; i < cfg->num_fc_ports; i++) {
13020aa14887SMatthew R. Ochs 		fc_port_regs = get_fc_port_regs(cfg, i);
13030aa14887SMatthew R. Ochs 
13040aa14887SMatthew R. Ochs 		writeq_be(0xFFFFFFFFU, &fc_port_regs[FC_ERROR / 8]);
13050aa14887SMatthew R. Ochs 		writeq_be(0, &fc_port_regs[FC_ERRCAP / 8]);
1306c21e0bbfSMatthew R. Ochs 	}
1307c21e0bbfSMatthew R. Ochs 
1308c21e0bbfSMatthew R. Ochs 	/* sync interrupts for master's IOARRIN write */
1309c21e0bbfSMatthew R. Ochs 	/* note that unlike asyncs, there can be no pending sync interrupts */
1310c21e0bbfSMatthew R. Ochs 	/* at this time (this is a fresh context and master has not written */
1311c21e0bbfSMatthew R. Ochs 	/* IOARRIN yet), so there is nothing to clear. */
1312c21e0bbfSMatthew R. Ochs 
1313c21e0bbfSMatthew R. Ochs 	/* set LISN#, it is always sent to the context that wrote IOARRIN */
13143065267aSMatthew R. Ochs 	for (i = 0; i < afu->num_hwqs; i++) {
1315bfc0bab1SUma Krishnan 		hwq = get_hwq(afu, i);
1316bfc0bab1SUma Krishnan 
1317465891feSMatthew R. Ochs 		reg = readq_be(&hwq->host_map->ctx_ctrl);
1318465891feSMatthew R. Ochs 		WARN_ON((reg & SISL_CTX_CTRL_LISN_MASK) != 0);
1319465891feSMatthew R. Ochs 		reg |= SISL_MSI_SYNC_ERROR;
1320465891feSMatthew R. Ochs 		writeq_be(reg, &hwq->host_map->ctx_ctrl);
1321bfc0bab1SUma Krishnan 		writeq_be(SISL_ISTATUS_MASK, &hwq->host_map->intr_mask);
1322bfc0bab1SUma Krishnan 	}
1323c21e0bbfSMatthew R. Ochs }
1324c21e0bbfSMatthew R. Ochs 
1325c21e0bbfSMatthew R. Ochs /**
1326c21e0bbfSMatthew R. Ochs  * cxlflash_sync_err_irq() - interrupt handler for synchronous errors
1327c21e0bbfSMatthew R. Ochs  * @irq:	Interrupt number.
1328c21e0bbfSMatthew R. Ochs  * @data:	Private data provided at interrupt registration, the AFU.
1329c21e0bbfSMatthew R. Ochs  *
1330c21e0bbfSMatthew R. Ochs  * Return: Always return IRQ_HANDLED.
1331c21e0bbfSMatthew R. Ochs  */
cxlflash_sync_err_irq(int irq,void * data)1332c21e0bbfSMatthew R. Ochs static irqreturn_t cxlflash_sync_err_irq(int irq, void *data)
1333c21e0bbfSMatthew R. Ochs {
1334bfc0bab1SUma Krishnan 	struct hwq *hwq = (struct hwq *)data;
1335bfc0bab1SUma Krishnan 	struct cxlflash_cfg *cfg = hwq->afu->parent;
1336fb67d44dSMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
1337c21e0bbfSMatthew R. Ochs 	u64 reg;
1338c21e0bbfSMatthew R. Ochs 	u64 reg_unmasked;
1339c21e0bbfSMatthew R. Ochs 
1340bfc0bab1SUma Krishnan 	reg = readq_be(&hwq->host_map->intr_status);
1341c21e0bbfSMatthew R. Ochs 	reg_unmasked = (reg & SISL_ISTATUS_UNMASK);
1342c21e0bbfSMatthew R. Ochs 
1343c21e0bbfSMatthew R. Ochs 	if (reg_unmasked == 0UL) {
1344fb67d44dSMatthew R. Ochs 		dev_err(dev, "%s: spurious interrupt, intr_status=%016llx\n",
1345fb67d44dSMatthew R. Ochs 			__func__, reg);
1346c21e0bbfSMatthew R. Ochs 		goto cxlflash_sync_err_irq_exit;
1347c21e0bbfSMatthew R. Ochs 	}
1348c21e0bbfSMatthew R. Ochs 
1349fb67d44dSMatthew R. Ochs 	dev_err(dev, "%s: unexpected interrupt, intr_status=%016llx\n",
1350fb67d44dSMatthew R. Ochs 		__func__, reg);
1351c21e0bbfSMatthew R. Ochs 
1352bfc0bab1SUma Krishnan 	writeq_be(reg_unmasked, &hwq->host_map->intr_clear);
1353c21e0bbfSMatthew R. Ochs 
1354c21e0bbfSMatthew R. Ochs cxlflash_sync_err_irq_exit:
1355c21e0bbfSMatthew R. Ochs 	return IRQ_HANDLED;
1356c21e0bbfSMatthew R. Ochs }
1357c21e0bbfSMatthew R. Ochs 
1358c21e0bbfSMatthew R. Ochs /**
135976a6ebbeSMatthew R. Ochs  * process_hrrq() - process the read-response queue
1360cf0ad7a1SLee Jones  * @hwq:	HWQ associated with the host.
1361f918b4a8SMatthew R. Ochs  * @doneq:	Queue of commands harvested from the RRQ.
1362cba06e6dSMatthew R. Ochs  * @budget:	Threshold of RRQ entries to process.
1363f918b4a8SMatthew R. Ochs  *
1364f918b4a8SMatthew R. Ochs  * This routine must be called holding the disabled RRQ spin lock.
1365c21e0bbfSMatthew R. Ochs  *
136676a6ebbeSMatthew R. Ochs  * Return: The number of entries processed.
1367c21e0bbfSMatthew R. Ochs  */
process_hrrq(struct hwq * hwq,struct list_head * doneq,int budget)1368bfc0bab1SUma Krishnan static int process_hrrq(struct hwq *hwq, struct list_head *doneq, int budget)
1369c21e0bbfSMatthew R. Ochs {
1370bfc0bab1SUma Krishnan 	struct afu *afu = hwq->afu;
1371c21e0bbfSMatthew R. Ochs 	struct afu_cmd *cmd;
1372696d0b0cSMatthew R. Ochs 	struct sisl_ioasa *ioasa;
1373696d0b0cSMatthew R. Ochs 	struct sisl_ioarcb *ioarcb;
1374bfc0bab1SUma Krishnan 	bool toggle = hwq->toggle;
137576a6ebbeSMatthew R. Ochs 	int num_hrrq = 0;
1376c21e0bbfSMatthew R. Ochs 	u64 entry,
1377bfc0bab1SUma Krishnan 	    *hrrq_start = hwq->hrrq_start,
1378bfc0bab1SUma Krishnan 	    *hrrq_end = hwq->hrrq_end,
1379bfc0bab1SUma Krishnan 	    *hrrq_curr = hwq->hrrq_curr;
1380c21e0bbfSMatthew R. Ochs 
1381cba06e6dSMatthew R. Ochs 	/* Process ready RRQ entries up to the specified budget (if any) */
1382c21e0bbfSMatthew R. Ochs 	while (true) {
1383c21e0bbfSMatthew R. Ochs 		entry = *hrrq_curr;
1384c21e0bbfSMatthew R. Ochs 
1385c21e0bbfSMatthew R. Ochs 		if ((entry & SISL_RESP_HANDLE_T_BIT) != toggle)
1386c21e0bbfSMatthew R. Ochs 			break;
1387c21e0bbfSMatthew R. Ochs 
1388696d0b0cSMatthew R. Ochs 		entry &= ~SISL_RESP_HANDLE_T_BIT;
1389696d0b0cSMatthew R. Ochs 
1390696d0b0cSMatthew R. Ochs 		if (afu_is_sq_cmd_mode(afu)) {
1391696d0b0cSMatthew R. Ochs 			ioasa = (struct sisl_ioasa *)entry;
1392696d0b0cSMatthew R. Ochs 			cmd = container_of(ioasa, struct afu_cmd, sa);
1393696d0b0cSMatthew R. Ochs 		} else {
1394696d0b0cSMatthew R. Ochs 			ioarcb = (struct sisl_ioarcb *)entry;
1395696d0b0cSMatthew R. Ochs 			cmd = container_of(ioarcb, struct afu_cmd, rcb);
1396696d0b0cSMatthew R. Ochs 		}
1397696d0b0cSMatthew R. Ochs 
1398f918b4a8SMatthew R. Ochs 		list_add_tail(&cmd->queue, doneq);
1399c21e0bbfSMatthew R. Ochs 
1400c21e0bbfSMatthew R. Ochs 		/* Advance to next entry or wrap and flip the toggle bit */
1401c21e0bbfSMatthew R. Ochs 		if (hrrq_curr < hrrq_end)
1402c21e0bbfSMatthew R. Ochs 			hrrq_curr++;
1403c21e0bbfSMatthew R. Ochs 		else {
1404c21e0bbfSMatthew R. Ochs 			hrrq_curr = hrrq_start;
1405c21e0bbfSMatthew R. Ochs 			toggle ^= SISL_RESP_HANDLE_T_BIT;
1406c21e0bbfSMatthew R. Ochs 		}
1407696d0b0cSMatthew R. Ochs 
1408bfc0bab1SUma Krishnan 		atomic_inc(&hwq->hsq_credits);
140976a6ebbeSMatthew R. Ochs 		num_hrrq++;
1410cba06e6dSMatthew R. Ochs 
1411cba06e6dSMatthew R. Ochs 		if (budget > 0 && num_hrrq >= budget)
1412cba06e6dSMatthew R. Ochs 			break;
1413c21e0bbfSMatthew R. Ochs 	}
1414c21e0bbfSMatthew R. Ochs 
1415bfc0bab1SUma Krishnan 	hwq->hrrq_curr = hrrq_curr;
1416bfc0bab1SUma Krishnan 	hwq->toggle = toggle;
1417c21e0bbfSMatthew R. Ochs 
141876a6ebbeSMatthew R. Ochs 	return num_hrrq;
141976a6ebbeSMatthew R. Ochs }
142076a6ebbeSMatthew R. Ochs 
142176a6ebbeSMatthew R. Ochs /**
1422f918b4a8SMatthew R. Ochs  * process_cmd_doneq() - process a queue of harvested RRQ commands
1423f918b4a8SMatthew R. Ochs  * @doneq:	Queue of completed commands.
1424f918b4a8SMatthew R. Ochs  *
1425f918b4a8SMatthew R. Ochs  * Note that upon return the queue can no longer be trusted.
1426f918b4a8SMatthew R. Ochs  */
process_cmd_doneq(struct list_head * doneq)1427f918b4a8SMatthew R. Ochs static void process_cmd_doneq(struct list_head *doneq)
1428f918b4a8SMatthew R. Ochs {
1429f918b4a8SMatthew R. Ochs 	struct afu_cmd *cmd, *tmp;
1430f918b4a8SMatthew R. Ochs 
1431f918b4a8SMatthew R. Ochs 	WARN_ON(list_empty(doneq));
1432f918b4a8SMatthew R. Ochs 
1433f918b4a8SMatthew R. Ochs 	list_for_each_entry_safe(cmd, tmp, doneq, queue)
1434f918b4a8SMatthew R. Ochs 		cmd_complete(cmd);
1435f918b4a8SMatthew R. Ochs }
1436f918b4a8SMatthew R. Ochs 
1437f918b4a8SMatthew R. Ochs /**
1438cba06e6dSMatthew R. Ochs  * cxlflash_irqpoll() - process a queue of harvested RRQ commands
1439cba06e6dSMatthew R. Ochs  * @irqpoll:	IRQ poll structure associated with queue to poll.
1440cba06e6dSMatthew R. Ochs  * @budget:	Threshold of RRQ entries to process per poll.
1441cba06e6dSMatthew R. Ochs  *
1442cba06e6dSMatthew R. Ochs  * Return: The number of entries processed.
1443cba06e6dSMatthew R. Ochs  */
cxlflash_irqpoll(struct irq_poll * irqpoll,int budget)1444cba06e6dSMatthew R. Ochs static int cxlflash_irqpoll(struct irq_poll *irqpoll, int budget)
1445cba06e6dSMatthew R. Ochs {
1446bfc0bab1SUma Krishnan 	struct hwq *hwq = container_of(irqpoll, struct hwq, irqpoll);
1447cba06e6dSMatthew R. Ochs 	unsigned long hrrq_flags;
1448cba06e6dSMatthew R. Ochs 	LIST_HEAD(doneq);
1449cba06e6dSMatthew R. Ochs 	int num_entries = 0;
1450cba06e6dSMatthew R. Ochs 
1451bfc0bab1SUma Krishnan 	spin_lock_irqsave(&hwq->hrrq_slock, hrrq_flags);
1452cba06e6dSMatthew R. Ochs 
1453bfc0bab1SUma Krishnan 	num_entries = process_hrrq(hwq, &doneq, budget);
1454cba06e6dSMatthew R. Ochs 	if (num_entries < budget)
1455cba06e6dSMatthew R. Ochs 		irq_poll_complete(irqpoll);
1456cba06e6dSMatthew R. Ochs 
1457bfc0bab1SUma Krishnan 	spin_unlock_irqrestore(&hwq->hrrq_slock, hrrq_flags);
1458cba06e6dSMatthew R. Ochs 
1459cba06e6dSMatthew R. Ochs 	process_cmd_doneq(&doneq);
1460cba06e6dSMatthew R. Ochs 	return num_entries;
1461cba06e6dSMatthew R. Ochs }
1462cba06e6dSMatthew R. Ochs 
1463cba06e6dSMatthew R. Ochs /**
146476a6ebbeSMatthew R. Ochs  * cxlflash_rrq_irq() - interrupt handler for read-response queue (normal path)
146576a6ebbeSMatthew R. Ochs  * @irq:	Interrupt number.
146676a6ebbeSMatthew R. Ochs  * @data:	Private data provided at interrupt registration, the AFU.
146776a6ebbeSMatthew R. Ochs  *
1468f918b4a8SMatthew R. Ochs  * Return: IRQ_HANDLED or IRQ_NONE when no ready entries found.
146976a6ebbeSMatthew R. Ochs  */
cxlflash_rrq_irq(int irq,void * data)147076a6ebbeSMatthew R. Ochs static irqreturn_t cxlflash_rrq_irq(int irq, void *data)
147176a6ebbeSMatthew R. Ochs {
1472bfc0bab1SUma Krishnan 	struct hwq *hwq = (struct hwq *)data;
1473bfc0bab1SUma Krishnan 	struct afu *afu = hwq->afu;
1474f918b4a8SMatthew R. Ochs 	unsigned long hrrq_flags;
1475f918b4a8SMatthew R. Ochs 	LIST_HEAD(doneq);
1476f918b4a8SMatthew R. Ochs 	int num_entries = 0;
147776a6ebbeSMatthew R. Ochs 
1478bfc0bab1SUma Krishnan 	spin_lock_irqsave(&hwq->hrrq_slock, hrrq_flags);
1479cba06e6dSMatthew R. Ochs 
1480d2d354a6SUma Krishnan 	/* Silently drop spurious interrupts when queue is not online */
1481d2d354a6SUma Krishnan 	if (!hwq->hrrq_online) {
1482d2d354a6SUma Krishnan 		spin_unlock_irqrestore(&hwq->hrrq_slock, hrrq_flags);
1483d2d354a6SUma Krishnan 		return IRQ_HANDLED;
1484d2d354a6SUma Krishnan 	}
1485d2d354a6SUma Krishnan 
1486cba06e6dSMatthew R. Ochs 	if (afu_is_irqpoll_enabled(afu)) {
1487bfc0bab1SUma Krishnan 		irq_poll_sched(&hwq->irqpoll);
1488bfc0bab1SUma Krishnan 		spin_unlock_irqrestore(&hwq->hrrq_slock, hrrq_flags);
1489cba06e6dSMatthew R. Ochs 		return IRQ_HANDLED;
1490cba06e6dSMatthew R. Ochs 	}
1491cba06e6dSMatthew R. Ochs 
1492bfc0bab1SUma Krishnan 	num_entries = process_hrrq(hwq, &doneq, -1);
1493bfc0bab1SUma Krishnan 	spin_unlock_irqrestore(&hwq->hrrq_slock, hrrq_flags);
1494f918b4a8SMatthew R. Ochs 
1495f918b4a8SMatthew R. Ochs 	if (num_entries == 0)
1496f918b4a8SMatthew R. Ochs 		return IRQ_NONE;
1497f918b4a8SMatthew R. Ochs 
1498f918b4a8SMatthew R. Ochs 	process_cmd_doneq(&doneq);
1499c21e0bbfSMatthew R. Ochs 	return IRQ_HANDLED;
1500c21e0bbfSMatthew R. Ochs }
1501c21e0bbfSMatthew R. Ochs 
1502e2ef33faSMatthew R. Ochs /*
1503e2ef33faSMatthew R. Ochs  * Asynchronous interrupt information table
1504e2ef33faSMatthew R. Ochs  *
1505e2ef33faSMatthew R. Ochs  * NOTE:
1506e2ef33faSMatthew R. Ochs  *	- Order matters here as this array is indexed by bit position.
1507e2ef33faSMatthew R. Ochs  *
1508e2ef33faSMatthew R. Ochs  *	- The checkpatch script considers the BUILD_SISL_ASTATUS_FC_PORT macro
1509e2ef33faSMatthew R. Ochs  *	  as complex and complains due to a lack of parentheses/braces.
1510e2ef33faSMatthew R. Ochs  */
1511e2ef33faSMatthew R. Ochs #define ASTATUS_FC(_a, _b, _c, _d)					 \
1512e2ef33faSMatthew R. Ochs 	{ SISL_ASTATUS_FC##_a##_##_b, _c, _a, (_d) }
1513e2ef33faSMatthew R. Ochs 
1514e2ef33faSMatthew R. Ochs #define BUILD_SISL_ASTATUS_FC_PORT(_a)					 \
1515e2ef33faSMatthew R. Ochs 	ASTATUS_FC(_a, LINK_UP, "link up", 0),				 \
1516e2ef33faSMatthew R. Ochs 	ASTATUS_FC(_a, LINK_DN, "link down", 0),			 \
1517e2ef33faSMatthew R. Ochs 	ASTATUS_FC(_a, LOGI_S, "login succeeded", SCAN_HOST),		 \
1518e2ef33faSMatthew R. Ochs 	ASTATUS_FC(_a, LOGI_F, "login failed", CLR_FC_ERROR),		 \
1519e2ef33faSMatthew R. Ochs 	ASTATUS_FC(_a, LOGI_R, "login timed out, retrying", LINK_RESET), \
1520e2ef33faSMatthew R. Ochs 	ASTATUS_FC(_a, CRC_T, "CRC threshold exceeded", LINK_RESET),	 \
1521e2ef33faSMatthew R. Ochs 	ASTATUS_FC(_a, LOGO, "target initiated LOGO", 0),		 \
1522e2ef33faSMatthew R. Ochs 	ASTATUS_FC(_a, OTHER, "other error", CLR_FC_ERROR | LINK_RESET)
1523e2ef33faSMatthew R. Ochs 
1524e2ef33faSMatthew R. Ochs static const struct asyc_intr_info ainfo[] = {
1525e2ef33faSMatthew R. Ochs 	BUILD_SISL_ASTATUS_FC_PORT(1),
1526e2ef33faSMatthew R. Ochs 	BUILD_SISL_ASTATUS_FC_PORT(0),
1527e2ef33faSMatthew R. Ochs 	BUILD_SISL_ASTATUS_FC_PORT(3),
1528e2ef33faSMatthew R. Ochs 	BUILD_SISL_ASTATUS_FC_PORT(2)
1529e2ef33faSMatthew R. Ochs };
1530e2ef33faSMatthew R. Ochs 
1531c21e0bbfSMatthew R. Ochs /**
1532c21e0bbfSMatthew R. Ochs  * cxlflash_async_err_irq() - interrupt handler for asynchronous errors
1533c21e0bbfSMatthew R. Ochs  * @irq:	Interrupt number.
1534c21e0bbfSMatthew R. Ochs  * @data:	Private data provided at interrupt registration, the AFU.
1535c21e0bbfSMatthew R. Ochs  *
1536c21e0bbfSMatthew R. Ochs  * Return: Always return IRQ_HANDLED.
1537c21e0bbfSMatthew R. Ochs  */
cxlflash_async_err_irq(int irq,void * data)1538c21e0bbfSMatthew R. Ochs static irqreturn_t cxlflash_async_err_irq(int irq, void *data)
1539c21e0bbfSMatthew R. Ochs {
1540bfc0bab1SUma Krishnan 	struct hwq *hwq = (struct hwq *)data;
1541bfc0bab1SUma Krishnan 	struct afu *afu = hwq->afu;
15424392ba49SMatthew R. Ochs 	struct cxlflash_cfg *cfg = afu->parent;
15434392ba49SMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
1544c21e0bbfSMatthew R. Ochs 	const struct asyc_intr_info *info;
15451786f4a0SMatthew R. Ochs 	struct sisl_global_map __iomem *global = &afu->afu_map->global;
15460aa14887SMatthew R. Ochs 	__be64 __iomem *fc_port_regs;
1547e2ef33faSMatthew R. Ochs 	u64 reg_unmasked;
1548c21e0bbfSMatthew R. Ochs 	u64 reg;
1549e2ef33faSMatthew R. Ochs 	u64 bit;
1550c21e0bbfSMatthew R. Ochs 	u8 port;
1551c21e0bbfSMatthew R. Ochs 
1552c21e0bbfSMatthew R. Ochs 	reg = readq_be(&global->regs.aintr_status);
1553c21e0bbfSMatthew R. Ochs 	reg_unmasked = (reg & SISL_ASTATUS_UNMASK);
1554c21e0bbfSMatthew R. Ochs 
1555e2ef33faSMatthew R. Ochs 	if (unlikely(reg_unmasked == 0)) {
1556fb67d44dSMatthew R. Ochs 		dev_err(dev, "%s: spurious interrupt, aintr_status=%016llx\n",
1557c21e0bbfSMatthew R. Ochs 			__func__, reg);
1558c21e0bbfSMatthew R. Ochs 		goto out;
1559c21e0bbfSMatthew R. Ochs 	}
1560c21e0bbfSMatthew R. Ochs 
1561f15fbf8dSMatthew R. Ochs 	/* FYI, it is 'okay' to clear AFU status before FC_ERROR */
1562c21e0bbfSMatthew R. Ochs 	writeq_be(reg_unmasked, &global->regs.aintr_clear);
1563c21e0bbfSMatthew R. Ochs 
1564f15fbf8dSMatthew R. Ochs 	/* Check each bit that is on */
1565e2ef33faSMatthew R. Ochs 	for_each_set_bit(bit, (ulong *)&reg_unmasked, BITS_PER_LONG) {
1566e2ef33faSMatthew R. Ochs 		if (unlikely(bit >= ARRAY_SIZE(ainfo))) {
1567e2ef33faSMatthew R. Ochs 			WARN_ON_ONCE(1);
1568c21e0bbfSMatthew R. Ochs 			continue;
1569e2ef33faSMatthew R. Ochs 		}
1570e2ef33faSMatthew R. Ochs 
1571e2ef33faSMatthew R. Ochs 		info = &ainfo[bit];
1572e2ef33faSMatthew R. Ochs 		if (unlikely(info->status != 1ULL << bit)) {
1573e2ef33faSMatthew R. Ochs 			WARN_ON_ONCE(1);
1574e2ef33faSMatthew R. Ochs 			continue;
1575e2ef33faSMatthew R. Ochs 		}
1576c21e0bbfSMatthew R. Ochs 
1577c21e0bbfSMatthew R. Ochs 		port = info->port;
15780aa14887SMatthew R. Ochs 		fc_port_regs = get_fc_port_regs(cfg, port);
1579c21e0bbfSMatthew R. Ochs 
1580fb67d44dSMatthew R. Ochs 		dev_err(dev, "%s: FC Port %d -> %s, fc_status=%016llx\n",
1581c21e0bbfSMatthew R. Ochs 			__func__, port, info->desc,
15820aa14887SMatthew R. Ochs 		       readq_be(&fc_port_regs[FC_STATUS / 8]));
1583c21e0bbfSMatthew R. Ochs 
1584c21e0bbfSMatthew R. Ochs 		/*
1585f15fbf8dSMatthew R. Ochs 		 * Do link reset first, some OTHER errors will set FC_ERROR
1586c21e0bbfSMatthew R. Ochs 		 * again if cleared before or w/o a reset
1587c21e0bbfSMatthew R. Ochs 		 */
1588c21e0bbfSMatthew R. Ochs 		if (info->action & LINK_RESET) {
15894392ba49SMatthew R. Ochs 			dev_err(dev, "%s: FC Port %d: resetting link\n",
1590c21e0bbfSMatthew R. Ochs 				__func__, port);
1591c21e0bbfSMatthew R. Ochs 			cfg->lr_state = LINK_RESET_REQUIRED;
1592c21e0bbfSMatthew R. Ochs 			cfg->lr_port = port;
1593c21e0bbfSMatthew R. Ochs 			schedule_work(&cfg->work_q);
1594c21e0bbfSMatthew R. Ochs 		}
1595c21e0bbfSMatthew R. Ochs 
1596c21e0bbfSMatthew R. Ochs 		if (info->action & CLR_FC_ERROR) {
15970aa14887SMatthew R. Ochs 			reg = readq_be(&fc_port_regs[FC_ERROR / 8]);
1598c21e0bbfSMatthew R. Ochs 
1599c21e0bbfSMatthew R. Ochs 			/*
1600f15fbf8dSMatthew R. Ochs 			 * Since all errors are unmasked, FC_ERROR and FC_ERRCAP
1601c21e0bbfSMatthew R. Ochs 			 * should be the same and tracing one is sufficient.
1602c21e0bbfSMatthew R. Ochs 			 */
1603c21e0bbfSMatthew R. Ochs 
1604fb67d44dSMatthew R. Ochs 			dev_err(dev, "%s: fc %d: clearing fc_error=%016llx\n",
1605c21e0bbfSMatthew R. Ochs 				__func__, port, reg);
1606c21e0bbfSMatthew R. Ochs 
16070aa14887SMatthew R. Ochs 			writeq_be(reg, &fc_port_regs[FC_ERROR / 8]);
16080aa14887SMatthew R. Ochs 			writeq_be(0, &fc_port_regs[FC_ERRCAP / 8]);
1609c21e0bbfSMatthew R. Ochs 		}
1610ef51074aSMatthew R. Ochs 
1611ef51074aSMatthew R. Ochs 		if (info->action & SCAN_HOST) {
1612ef51074aSMatthew R. Ochs 			atomic_inc(&cfg->scan_host_needed);
1613ef51074aSMatthew R. Ochs 			schedule_work(&cfg->work_q);
1614ef51074aSMatthew R. Ochs 		}
1615c21e0bbfSMatthew R. Ochs 	}
1616c21e0bbfSMatthew R. Ochs 
1617c21e0bbfSMatthew R. Ochs out:
1618c21e0bbfSMatthew R. Ochs 	return IRQ_HANDLED;
1619c21e0bbfSMatthew R. Ochs }
1620c21e0bbfSMatthew R. Ochs 
1621c21e0bbfSMatthew R. Ochs /**
1622c21e0bbfSMatthew R. Ochs  * read_vpd() - obtains the WWPNs from VPD
16231284fb0cSMatthew R. Ochs  * @cfg:	Internal structure associated with the host.
162478ae028eSMatthew R. Ochs  * @wwpn:	Array of size MAX_FC_PORTS to pass back WWPNs
1625c21e0bbfSMatthew R. Ochs  *
16261284fb0cSMatthew R. Ochs  * Return: 0 on success, -errno on failure
1627c21e0bbfSMatthew R. Ochs  */
read_vpd(struct cxlflash_cfg * cfg,u64 wwpn[])1628c21e0bbfSMatthew R. Ochs static int read_vpd(struct cxlflash_cfg *cfg, u64 wwpn[])
1629c21e0bbfSMatthew R. Ochs {
1630fb67d44dSMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
1631fb67d44dSMatthew R. Ochs 	struct pci_dev *pdev = cfg->dev;
163289031795SHeiner Kallweit 	int i, k, rc = 0;
163389031795SHeiner Kallweit 	unsigned int kw_size;
1634c21e0bbfSMatthew R. Ochs 	ssize_t vpd_size;
1635c21e0bbfSMatthew R. Ochs 	char vpd_data[CXLFLASH_VPD_LEN];
1636c21e0bbfSMatthew R. Ochs 	char tmp_buf[WWPN_BUF_LEN] = { 0 };
16370d419130SMatthew R. Ochs 	const struct dev_dependent_vals *ddv = (struct dev_dependent_vals *)
16380d419130SMatthew R. Ochs 						cfg->dev_id->driver_data;
16390d419130SMatthew R. Ochs 	const bool wwpn_vpd_required = ddv->flags & CXLFLASH_WWPN_VPD_REQUIRED;
16400d419130SMatthew R. Ochs 	const char *wwpn_vpd_tags[MAX_FC_PORTS] = { "V5", "V6", "V7", "V8" };
1641c21e0bbfSMatthew R. Ochs 
1642c21e0bbfSMatthew R. Ochs 	/* Get the VPD data from the device */
164325b8e08eSMatthew R. Ochs 	vpd_size = cfg->ops->read_adapter_vpd(pdev, vpd_data, sizeof(vpd_data));
1644c21e0bbfSMatthew R. Ochs 	if (unlikely(vpd_size <= 0)) {
1645fb67d44dSMatthew R. Ochs 		dev_err(dev, "%s: Unable to read VPD (size = %ld)\n",
1646c21e0bbfSMatthew R. Ochs 			__func__, vpd_size);
1647c21e0bbfSMatthew R. Ochs 		rc = -ENODEV;
1648c21e0bbfSMatthew R. Ochs 		goto out;
1649c21e0bbfSMatthew R. Ochs 	}
1650c21e0bbfSMatthew R. Ochs 
1651c21e0bbfSMatthew R. Ochs 	/*
1652c21e0bbfSMatthew R. Ochs 	 * Find the offset of the WWPN tag within the read only
1653c21e0bbfSMatthew R. Ochs 	 * VPD data and validate the found field (partials are
1654c21e0bbfSMatthew R. Ochs 	 * no good to us). Convert the ASCII data to an integer
1655c21e0bbfSMatthew R. Ochs 	 * value. Note that we must copy to a temporary buffer
1656c21e0bbfSMatthew R. Ochs 	 * because the conversion service requires that the ASCII
1657c21e0bbfSMatthew R. Ochs 	 * string be terminated.
16580d419130SMatthew R. Ochs 	 *
16590d419130SMatthew R. Ochs 	 * Allow for WWPN not being found for all devices, setting
16600d419130SMatthew R. Ochs 	 * the returned WWPN to zero when not found. Notify with a
16610d419130SMatthew R. Ochs 	 * log error for cards that should have had WWPN keywords
16620d419130SMatthew R. Ochs 	 * in the VPD - cards requiring WWPN will not have their
16630d419130SMatthew R. Ochs 	 * ports programmed and operate in an undefined state.
1664c21e0bbfSMatthew R. Ochs 	 */
166578ae028eSMatthew R. Ochs 	for (k = 0; k < cfg->num_fc_ports; k++) {
166689031795SHeiner Kallweit 		i = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
166789031795SHeiner Kallweit 						 wwpn_vpd_tags[k], &kw_size);
166889031795SHeiner Kallweit 		if (i == -ENOENT) {
16690d419130SMatthew R. Ochs 			if (wwpn_vpd_required)
16700d419130SMatthew R. Ochs 				dev_err(dev, "%s: Port %d WWPN not found\n",
1671fb67d44dSMatthew R. Ochs 					__func__, k);
16720d419130SMatthew R. Ochs 			wwpn[k] = 0ULL;
16730d419130SMatthew R. Ochs 			continue;
1674c21e0bbfSMatthew R. Ochs 		}
1675c21e0bbfSMatthew R. Ochs 
167689031795SHeiner Kallweit 		if (i < 0 || kw_size != WWPN_LEN) {
1677fb67d44dSMatthew R. Ochs 			dev_err(dev, "%s: Port %d WWPN incomplete or bad VPD\n",
1678c21e0bbfSMatthew R. Ochs 				__func__, k);
1679c21e0bbfSMatthew R. Ochs 			rc = -ENODEV;
1680c21e0bbfSMatthew R. Ochs 			goto out;
1681c21e0bbfSMatthew R. Ochs 		}
1682c21e0bbfSMatthew R. Ochs 
1683c21e0bbfSMatthew R. Ochs 		memcpy(tmp_buf, &vpd_data[i], WWPN_LEN);
1684c21e0bbfSMatthew R. Ochs 		rc = kstrtoul(tmp_buf, WWPN_LEN, (ulong *)&wwpn[k]);
1685c21e0bbfSMatthew R. Ochs 		if (unlikely(rc)) {
1686fb67d44dSMatthew R. Ochs 			dev_err(dev, "%s: WWPN conversion failed for port %d\n",
1687fb67d44dSMatthew R. Ochs 				__func__, k);
1688c21e0bbfSMatthew R. Ochs 			rc = -ENODEV;
1689c21e0bbfSMatthew R. Ochs 			goto out;
1690c21e0bbfSMatthew R. Ochs 		}
169178ae028eSMatthew R. Ochs 
169278ae028eSMatthew R. Ochs 		dev_dbg(dev, "%s: wwpn%d=%016llx\n", __func__, k, wwpn[k]);
1693c21e0bbfSMatthew R. Ochs 	}
1694c21e0bbfSMatthew R. Ochs 
1695c21e0bbfSMatthew R. Ochs out:
1696fb67d44dSMatthew R. Ochs 	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
1697c21e0bbfSMatthew R. Ochs 	return rc;
1698c21e0bbfSMatthew R. Ochs }
1699c21e0bbfSMatthew R. Ochs 
1700c21e0bbfSMatthew R. Ochs /**
1701c21e0bbfSMatthew R. Ochs  * init_pcr() - initialize the provisioning and control registers
17021284fb0cSMatthew R. Ochs  * @cfg:	Internal structure associated with the host.
1703c21e0bbfSMatthew R. Ochs  *
1704c21e0bbfSMatthew R. Ochs  * Also sets up fast access to the mapped registers and initializes AFU
1705c21e0bbfSMatthew R. Ochs  * command fields that never change.
1706c21e0bbfSMatthew R. Ochs  */
init_pcr(struct cxlflash_cfg * cfg)170715305514SMatthew R. Ochs static void init_pcr(struct cxlflash_cfg *cfg)
1708c21e0bbfSMatthew R. Ochs {
1709c21e0bbfSMatthew R. Ochs 	struct afu *afu = cfg->afu;
17101786f4a0SMatthew R. Ochs 	struct sisl_ctrl_map __iomem *ctrl_map;
1711bfc0bab1SUma Krishnan 	struct hwq *hwq;
171225b8e08eSMatthew R. Ochs 	void *cookie;
1713c21e0bbfSMatthew R. Ochs 	int i;
1714c21e0bbfSMatthew R. Ochs 
1715c21e0bbfSMatthew R. Ochs 	for (i = 0; i < MAX_CONTEXT; i++) {
1716c21e0bbfSMatthew R. Ochs 		ctrl_map = &afu->afu_map->ctrls[i].ctrl;
1717f15fbf8dSMatthew R. Ochs 		/* Disrupt any clients that could be running */
1718c21e0bbfSMatthew R. Ochs 		/* e.g. clients that survived a master restart */
1719c21e0bbfSMatthew R. Ochs 		writeq_be(0, &ctrl_map->rht_start);
1720c21e0bbfSMatthew R. Ochs 		writeq_be(0, &ctrl_map->rht_cnt_id);
1721c21e0bbfSMatthew R. Ochs 		writeq_be(0, &ctrl_map->ctx_cap);
1722c21e0bbfSMatthew R. Ochs 	}
1723c21e0bbfSMatthew R. Ochs 
1724bfc0bab1SUma Krishnan 	/* Copy frequently used fields into hwq */
17253065267aSMatthew R. Ochs 	for (i = 0; i < afu->num_hwqs; i++) {
1726bfc0bab1SUma Krishnan 		hwq = get_hwq(afu, i);
172725b8e08eSMatthew R. Ochs 		cookie = hwq->ctx_cookie;
1728bfc0bab1SUma Krishnan 
172925b8e08eSMatthew R. Ochs 		hwq->ctx_hndl = (u16) cfg->ops->process_element(cookie);
1730bfc0bab1SUma Krishnan 		hwq->host_map = &afu->afu_map->hosts[hwq->ctx_hndl].host;
1731bfc0bab1SUma Krishnan 		hwq->ctrl_map = &afu->afu_map->ctrls[hwq->ctx_hndl].ctrl;
1732c21e0bbfSMatthew R. Ochs 
1733c21e0bbfSMatthew R. Ochs 		/* Program the Endian Control for the master context */
1734bfc0bab1SUma Krishnan 		writeq_be(SISL_ENDIAN_CTRL, &hwq->host_map->endian_ctrl);
1735bfc0bab1SUma Krishnan 	}
1736c21e0bbfSMatthew R. Ochs }
1737c21e0bbfSMatthew R. Ochs 
1738c21e0bbfSMatthew R. Ochs /**
1739c21e0bbfSMatthew R. Ochs  * init_global() - initialize AFU global registers
17401284fb0cSMatthew R. Ochs  * @cfg:	Internal structure associated with the host.
1741c21e0bbfSMatthew R. Ochs  */
init_global(struct cxlflash_cfg * cfg)174215305514SMatthew R. Ochs static int init_global(struct cxlflash_cfg *cfg)
1743c21e0bbfSMatthew R. Ochs {
1744c21e0bbfSMatthew R. Ochs 	struct afu *afu = cfg->afu;
17454392ba49SMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
1746bfc0bab1SUma Krishnan 	struct hwq *hwq;
1747bfc0bab1SUma Krishnan 	struct sisl_host_map __iomem *hmap;
17480aa14887SMatthew R. Ochs 	__be64 __iomem *fc_port_regs;
174978ae028eSMatthew R. Ochs 	u64 wwpn[MAX_FC_PORTS];	/* wwpn of AFU ports */
1750c21e0bbfSMatthew R. Ochs 	int i = 0, num_ports = 0;
1751c21e0bbfSMatthew R. Ochs 	int rc = 0;
1752d44af4b0SUma Krishnan 	int j;
1753d44af4b0SUma Krishnan 	void *ctx;
1754c21e0bbfSMatthew R. Ochs 	u64 reg;
1755c21e0bbfSMatthew R. Ochs 
1756c21e0bbfSMatthew R. Ochs 	rc = read_vpd(cfg, &wwpn[0]);
1757c21e0bbfSMatthew R. Ochs 	if (rc) {
17584392ba49SMatthew R. Ochs 		dev_err(dev, "%s: could not read vpd rc=%d\n", __func__, rc);
1759c21e0bbfSMatthew R. Ochs 		goto out;
1760c21e0bbfSMatthew R. Ochs 	}
1761c21e0bbfSMatthew R. Ochs 
1762bfc0bab1SUma Krishnan 	/* Set up RRQ and SQ in HWQ for master issued cmds */
17633065267aSMatthew R. Ochs 	for (i = 0; i < afu->num_hwqs; i++) {
1764bfc0bab1SUma Krishnan 		hwq = get_hwq(afu, i);
1765bfc0bab1SUma Krishnan 		hmap = hwq->host_map;
1766bfc0bab1SUma Krishnan 
1767bfc0bab1SUma Krishnan 		writeq_be((u64) hwq->hrrq_start, &hmap->rrq_start);
1768bfc0bab1SUma Krishnan 		writeq_be((u64) hwq->hrrq_end, &hmap->rrq_end);
1769d2d354a6SUma Krishnan 		hwq->hrrq_online = true;
1770c21e0bbfSMatthew R. Ochs 
1771696d0b0cSMatthew R. Ochs 		if (afu_is_sq_cmd_mode(afu)) {
1772bfc0bab1SUma Krishnan 			writeq_be((u64)hwq->hsq_start, &hmap->sq_start);
1773bfc0bab1SUma Krishnan 			writeq_be((u64)hwq->hsq_end, &hmap->sq_end);
1774bfc0bab1SUma Krishnan 		}
1775696d0b0cSMatthew R. Ochs 	}
1776696d0b0cSMatthew R. Ochs 
1777c21e0bbfSMatthew R. Ochs 	/* AFU configuration */
1778c21e0bbfSMatthew R. Ochs 	reg = readq_be(&afu->afu_map->global.regs.afu_config);
1779c21e0bbfSMatthew R. Ochs 	reg |= SISL_AFUCONF_AR_ALL|SISL_AFUCONF_ENDIAN;
1780c21e0bbfSMatthew R. Ochs 	/* enable all auto retry options and control endianness */
1781c21e0bbfSMatthew R. Ochs 	/* leave others at default: */
1782c21e0bbfSMatthew R. Ochs 	/* CTX_CAP write protected, mbox_r does not clear on read and */
1783c21e0bbfSMatthew R. Ochs 	/* checker on if dual afu */
1784c21e0bbfSMatthew R. Ochs 	writeq_be(reg, &afu->afu_map->global.regs.afu_config);
1785c21e0bbfSMatthew R. Ochs 
1786f15fbf8dSMatthew R. Ochs 	/* Global port select: select either port */
1787c21e0bbfSMatthew R. Ochs 	if (afu->internal_lun) {
1788f15fbf8dSMatthew R. Ochs 		/* Only use port 0 */
1789c21e0bbfSMatthew R. Ochs 		writeq_be(PORT0, &afu->afu_map->global.regs.afu_port_sel);
179078ae028eSMatthew R. Ochs 		num_ports = 0;
1791c21e0bbfSMatthew R. Ochs 	} else {
17928fa4f177SMatthew R. Ochs 		writeq_be(PORT_MASK(cfg->num_fc_ports),
17938fa4f177SMatthew R. Ochs 			  &afu->afu_map->global.regs.afu_port_sel);
179478ae028eSMatthew R. Ochs 		num_ports = cfg->num_fc_ports;
1795c21e0bbfSMatthew R. Ochs 	}
1796c21e0bbfSMatthew R. Ochs 
1797c21e0bbfSMatthew R. Ochs 	for (i = 0; i < num_ports; i++) {
17980aa14887SMatthew R. Ochs 		fc_port_regs = get_fc_port_regs(cfg, i);
17990aa14887SMatthew R. Ochs 
1800f15fbf8dSMatthew R. Ochs 		/* Unmask all errors (but they are still masked at AFU) */
18010aa14887SMatthew R. Ochs 		writeq_be(0, &fc_port_regs[FC_ERRMSK / 8]);
1802f15fbf8dSMatthew R. Ochs 		/* Clear CRC error cnt & set a threshold */
18030aa14887SMatthew R. Ochs 		(void)readq_be(&fc_port_regs[FC_CNT_CRCERR / 8]);
18040aa14887SMatthew R. Ochs 		writeq_be(MC_CRC_THRESH, &fc_port_regs[FC_CRC_THRESH / 8]);
1805c21e0bbfSMatthew R. Ochs 
1806f15fbf8dSMatthew R. Ochs 		/* Set WWPNs. If already programmed, wwpn[i] is 0 */
1807f8013261SMatthew R. Ochs 		if (wwpn[i] != 0)
18080aa14887SMatthew R. Ochs 			afu_set_wwpn(afu, i, &fc_port_regs[0], wwpn[i]);
1809c21e0bbfSMatthew R. Ochs 		/* Programming WWPN back to back causes additional
1810c21e0bbfSMatthew R. Ochs 		 * offline/online transitions and a PLOGI
1811c21e0bbfSMatthew R. Ochs 		 */
1812c21e0bbfSMatthew R. Ochs 		msleep(100);
1813c21e0bbfSMatthew R. Ochs 	}
1814c21e0bbfSMatthew R. Ochs 
1815d44af4b0SUma Krishnan 	if (afu_is_ocxl_lisn(afu)) {
1816d44af4b0SUma Krishnan 		/* Set up the LISN effective address for each master */
1817d44af4b0SUma Krishnan 		for (i = 0; i < afu->num_hwqs; i++) {
1818d44af4b0SUma Krishnan 			hwq = get_hwq(afu, i);
1819d44af4b0SUma Krishnan 			ctx = hwq->ctx_cookie;
1820d44af4b0SUma Krishnan 
1821d44af4b0SUma Krishnan 			for (j = 0; j < hwq->num_irqs; j++) {
1822d44af4b0SUma Krishnan 				reg = cfg->ops->get_irq_objhndl(ctx, j);
1823d44af4b0SUma Krishnan 				writeq_be(reg, &hwq->ctrl_map->lisn_ea[j]);
1824d44af4b0SUma Krishnan 			}
1825d44af4b0SUma Krishnan 
1826d44af4b0SUma Krishnan 			reg = hwq->ctx_hndl;
1827d44af4b0SUma Krishnan 			writeq_be(SISL_LISN_PASID(reg, reg),
1828d44af4b0SUma Krishnan 				  &hwq->ctrl_map->lisn_pasid[0]);
1829d44af4b0SUma Krishnan 			writeq_be(SISL_LISN_PASID(0UL, reg),
1830d44af4b0SUma Krishnan 				  &hwq->ctrl_map->lisn_pasid[1]);
1831d44af4b0SUma Krishnan 		}
1832d44af4b0SUma Krishnan 	}
1833d44af4b0SUma Krishnan 
1834f15fbf8dSMatthew R. Ochs 	/* Set up master's own CTX_CAP to allow real mode, host translation */
1835f15fbf8dSMatthew R. Ochs 	/* tables, afu cmds and read/write GSCSI cmds. */
1836c21e0bbfSMatthew R. Ochs 	/* First, unlock ctx_cap write by reading mbox */
18373065267aSMatthew R. Ochs 	for (i = 0; i < afu->num_hwqs; i++) {
1838bfc0bab1SUma Krishnan 		hwq = get_hwq(afu, i);
1839bfc0bab1SUma Krishnan 
1840bfc0bab1SUma Krishnan 		(void)readq_be(&hwq->ctrl_map->mbox_r);	/* unlock ctx_cap */
1841c21e0bbfSMatthew R. Ochs 		writeq_be((SISL_CTX_CAP_REAL_MODE | SISL_CTX_CAP_HOST_XLATE |
1842c21e0bbfSMatthew R. Ochs 			SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD |
1843c21e0bbfSMatthew R. Ochs 			SISL_CTX_CAP_AFU_CMD | SISL_CTX_CAP_GSCSI_CMD),
1844bfc0bab1SUma Krishnan 			&hwq->ctrl_map->ctx_cap);
1845bfc0bab1SUma Krishnan 	}
18463223c01aSMatthew R. Ochs 
18473223c01aSMatthew R. Ochs 	/*
18483223c01aSMatthew R. Ochs 	 * Determine write-same unmap support for host by evaluating the unmap
18493223c01aSMatthew R. Ochs 	 * sector support bit of the context control register associated with
18503223c01aSMatthew R. Ochs 	 * the primary hardware queue. Note that while this status is reflected
18513223c01aSMatthew R. Ochs 	 * in a context register, the outcome can be assumed to be host-wide.
18523223c01aSMatthew R. Ochs 	 */
18533223c01aSMatthew R. Ochs 	hwq = get_hwq(afu, PRIMARY_HWQ);
18543223c01aSMatthew R. Ochs 	reg = readq_be(&hwq->host_map->ctx_ctrl);
18553223c01aSMatthew R. Ochs 	if (reg & SISL_CTX_CTRL_UNMAP_SECTOR)
18563223c01aSMatthew R. Ochs 		cfg->ws_unmap = true;
18573223c01aSMatthew R. Ochs 
1858f15fbf8dSMatthew R. Ochs 	/* Initialize heartbeat */
1859c21e0bbfSMatthew R. Ochs 	afu->hb = readq_be(&afu->afu_map->global.regs.afu_hb);
1860c21e0bbfSMatthew R. Ochs out:
1861c21e0bbfSMatthew R. Ochs 	return rc;
1862c21e0bbfSMatthew R. Ochs }
1863c21e0bbfSMatthew R. Ochs 
1864c21e0bbfSMatthew R. Ochs /**
1865c21e0bbfSMatthew R. Ochs  * start_afu() - initializes and starts the AFU
18661284fb0cSMatthew R. Ochs  * @cfg:	Internal structure associated with the host.
1867c21e0bbfSMatthew R. Ochs  */
start_afu(struct cxlflash_cfg * cfg)1868c21e0bbfSMatthew R. Ochs static int start_afu(struct cxlflash_cfg *cfg)
1869c21e0bbfSMatthew R. Ochs {
1870c21e0bbfSMatthew R. Ochs 	struct afu *afu = cfg->afu;
1871fb67d44dSMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
1872bfc0bab1SUma Krishnan 	struct hwq *hwq;
1873c21e0bbfSMatthew R. Ochs 	int rc = 0;
1874bfc0bab1SUma Krishnan 	int i;
1875c21e0bbfSMatthew R. Ochs 
1876c21e0bbfSMatthew R. Ochs 	init_pcr(cfg);
1877c21e0bbfSMatthew R. Ochs 
1878bfc0bab1SUma Krishnan 	/* Initialize each HWQ */
18793065267aSMatthew R. Ochs 	for (i = 0; i < afu->num_hwqs; i++) {
1880bfc0bab1SUma Krishnan 		hwq = get_hwq(afu, i);
1881bfc0bab1SUma Krishnan 
1882bfc0bab1SUma Krishnan 		/* After an AFU reset, RRQ entries are stale, clear them */
1883bfc0bab1SUma Krishnan 		memset(&hwq->rrq_entry, 0, sizeof(hwq->rrq_entry));
1884bfc0bab1SUma Krishnan 
1885bfc0bab1SUma Krishnan 		/* Initialize RRQ pointers */
1886bfc0bab1SUma Krishnan 		hwq->hrrq_start = &hwq->rrq_entry[0];
1887bfc0bab1SUma Krishnan 		hwq->hrrq_end = &hwq->rrq_entry[NUM_RRQ_ENTRY - 1];
1888bfc0bab1SUma Krishnan 		hwq->hrrq_curr = hwq->hrrq_start;
1889bfc0bab1SUma Krishnan 		hwq->toggle = 1;
189066ea9bccSUma Krishnan 
189166ea9bccSUma Krishnan 		/* Initialize spin locks */
1892bfc0bab1SUma Krishnan 		spin_lock_init(&hwq->hrrq_slock);
189366ea9bccSUma Krishnan 		spin_lock_init(&hwq->hsq_slock);
1894c21e0bbfSMatthew R. Ochs 
1895696d0b0cSMatthew R. Ochs 		/* Initialize SQ */
1896696d0b0cSMatthew R. Ochs 		if (afu_is_sq_cmd_mode(afu)) {
1897bfc0bab1SUma Krishnan 			memset(&hwq->sq, 0, sizeof(hwq->sq));
1898bfc0bab1SUma Krishnan 			hwq->hsq_start = &hwq->sq[0];
1899bfc0bab1SUma Krishnan 			hwq->hsq_end = &hwq->sq[NUM_SQ_ENTRY - 1];
1900bfc0bab1SUma Krishnan 			hwq->hsq_curr = hwq->hsq_start;
1901696d0b0cSMatthew R. Ochs 
1902bfc0bab1SUma Krishnan 			atomic_set(&hwq->hsq_credits, NUM_SQ_ENTRY - 1);
1903696d0b0cSMatthew R. Ochs 		}
1904696d0b0cSMatthew R. Ochs 
1905cba06e6dSMatthew R. Ochs 		/* Initialize IRQ poll */
1906cba06e6dSMatthew R. Ochs 		if (afu_is_irqpoll_enabled(afu))
1907bfc0bab1SUma Krishnan 			irq_poll_init(&hwq->irqpoll, afu->irqpoll_weight,
1908cba06e6dSMatthew R. Ochs 				      cxlflash_irqpoll);
1909cba06e6dSMatthew R. Ochs 
1910bfc0bab1SUma Krishnan 	}
1911bfc0bab1SUma Krishnan 
1912c21e0bbfSMatthew R. Ochs 	rc = init_global(cfg);
1913c21e0bbfSMatthew R. Ochs 
1914fb67d44dSMatthew R. Ochs 	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
1915c21e0bbfSMatthew R. Ochs 	return rc;
1916c21e0bbfSMatthew R. Ochs }
1917c21e0bbfSMatthew R. Ochs 
1918c21e0bbfSMatthew R. Ochs /**
19199526f360SManoj N. Kumar  * init_intr() - setup interrupt handlers for the master context
19201284fb0cSMatthew R. Ochs  * @cfg:	Internal structure associated with the host.
1921bfc0bab1SUma Krishnan  * @hwq:	Hardware queue to initialize.
1922c21e0bbfSMatthew R. Ochs  *
19231284fb0cSMatthew R. Ochs  * Return: 0 on success, -errno on failure
1924c21e0bbfSMatthew R. Ochs  */
init_intr(struct cxlflash_cfg * cfg,struct hwq * hwq)19259526f360SManoj N. Kumar static enum undo_level init_intr(struct cxlflash_cfg *cfg,
1926bfc0bab1SUma Krishnan 				 struct hwq *hwq)
1927c21e0bbfSMatthew R. Ochs {
19289526f360SManoj N. Kumar 	struct device *dev = &cfg->dev->dev;
1929b070545dSUma Krishnan 	void *ctx = hwq->ctx_cookie;
1930c21e0bbfSMatthew R. Ochs 	int rc = 0;
19319526f360SManoj N. Kumar 	enum undo_level level = UNDO_NOOP;
1932bfc0bab1SUma Krishnan 	bool is_primary_hwq = (hwq->index == PRIMARY_HWQ);
1933e11e0ff8SUma Krishnan 	int num_irqs = hwq->num_irqs;
1934c21e0bbfSMatthew R. Ochs 
193525b8e08eSMatthew R. Ochs 	rc = cfg->ops->allocate_afu_irqs(ctx, num_irqs);
1936c21e0bbfSMatthew R. Ochs 	if (unlikely(rc)) {
1937fb67d44dSMatthew R. Ochs 		dev_err(dev, "%s: allocate_afu_irqs failed rc=%d\n",
1938c21e0bbfSMatthew R. Ochs 			__func__, rc);
19399526f360SManoj N. Kumar 		level = UNDO_NOOP;
1940c21e0bbfSMatthew R. Ochs 		goto out;
1941c21e0bbfSMatthew R. Ochs 	}
1942c21e0bbfSMatthew R. Ochs 
194325b8e08eSMatthew R. Ochs 	rc = cfg->ops->map_afu_irq(ctx, 1, cxlflash_sync_err_irq, hwq,
1944c21e0bbfSMatthew R. Ochs 				   "SISL_MSI_SYNC_ERROR");
1945c21e0bbfSMatthew R. Ochs 	if (unlikely(rc <= 0)) {
1946fb67d44dSMatthew R. Ochs 		dev_err(dev, "%s: SISL_MSI_SYNC_ERROR map failed\n", __func__);
1947c21e0bbfSMatthew R. Ochs 		level = FREE_IRQ;
1948c21e0bbfSMatthew R. Ochs 		goto out;
1949c21e0bbfSMatthew R. Ochs 	}
1950c21e0bbfSMatthew R. Ochs 
195125b8e08eSMatthew R. Ochs 	rc = cfg->ops->map_afu_irq(ctx, 2, cxlflash_rrq_irq, hwq,
1952c21e0bbfSMatthew R. Ochs 				   "SISL_MSI_RRQ_UPDATED");
1953c21e0bbfSMatthew R. Ochs 	if (unlikely(rc <= 0)) {
1954fb67d44dSMatthew R. Ochs 		dev_err(dev, "%s: SISL_MSI_RRQ_UPDATED map failed\n", __func__);
1955c21e0bbfSMatthew R. Ochs 		level = UNMAP_ONE;
1956c21e0bbfSMatthew R. Ochs 		goto out;
1957c21e0bbfSMatthew R. Ochs 	}
1958c21e0bbfSMatthew R. Ochs 
1959bfc0bab1SUma Krishnan 	/* SISL_MSI_ASYNC_ERROR is setup only for the primary HWQ */
1960bfc0bab1SUma Krishnan 	if (!is_primary_hwq)
1961bfc0bab1SUma Krishnan 		goto out;
1962bfc0bab1SUma Krishnan 
196325b8e08eSMatthew R. Ochs 	rc = cfg->ops->map_afu_irq(ctx, 3, cxlflash_async_err_irq, hwq,
1964c21e0bbfSMatthew R. Ochs 				   "SISL_MSI_ASYNC_ERROR");
1965c21e0bbfSMatthew R. Ochs 	if (unlikely(rc <= 0)) {
1966fb67d44dSMatthew R. Ochs 		dev_err(dev, "%s: SISL_MSI_ASYNC_ERROR map failed\n", __func__);
1967c21e0bbfSMatthew R. Ochs 		level = UNMAP_TWO;
1968c21e0bbfSMatthew R. Ochs 		goto out;
1969c21e0bbfSMatthew R. Ochs 	}
19709526f360SManoj N. Kumar out:
19719526f360SManoj N. Kumar 	return level;
19729526f360SManoj N. Kumar }
1973c21e0bbfSMatthew R. Ochs 
19749526f360SManoj N. Kumar /**
19759526f360SManoj N. Kumar  * init_mc() - create and register as the master context
19769526f360SManoj N. Kumar  * @cfg:	Internal structure associated with the host.
1977cf0ad7a1SLee Jones  * @index:	HWQ Index of the master context.
19789526f360SManoj N. Kumar  *
19799526f360SManoj N. Kumar  * Return: 0 on success, -errno on failure
19809526f360SManoj N. Kumar  */
init_mc(struct cxlflash_cfg * cfg,u32 index)1981bfc0bab1SUma Krishnan static int init_mc(struct cxlflash_cfg *cfg, u32 index)
19829526f360SManoj N. Kumar {
1983b070545dSUma Krishnan 	void *ctx;
19849526f360SManoj N. Kumar 	struct device *dev = &cfg->dev->dev;
1985bfc0bab1SUma Krishnan 	struct hwq *hwq = get_hwq(cfg->afu, index);
19869526f360SManoj N. Kumar 	int rc = 0;
1987e11e0ff8SUma Krishnan 	int num_irqs;
19889526f360SManoj N. Kumar 	enum undo_level level;
19899526f360SManoj N. Kumar 
1990bfc0bab1SUma Krishnan 	hwq->afu = cfg->afu;
1991bfc0bab1SUma Krishnan 	hwq->index = index;
1992a002bf83SUma Krishnan 	INIT_LIST_HEAD(&hwq->pending_cmds);
1993bfc0bab1SUma Krishnan 
1994e11e0ff8SUma Krishnan 	if (index == PRIMARY_HWQ) {
199525b8e08eSMatthew R. Ochs 		ctx = cfg->ops->get_context(cfg->dev, cfg->afu_cookie);
1996e11e0ff8SUma Krishnan 		num_irqs = 3;
1997e11e0ff8SUma Krishnan 	} else {
199825b8e08eSMatthew R. Ochs 		ctx = cfg->ops->dev_context_init(cfg->dev, cfg->afu_cookie);
1999e11e0ff8SUma Krishnan 		num_irqs = 2;
2000e11e0ff8SUma Krishnan 	}
20010df69c60SUma Krishnan 	if (IS_ERR_OR_NULL(ctx)) {
20029526f360SManoj N. Kumar 		rc = -ENOMEM;
2003bfc0bab1SUma Krishnan 		goto err1;
20049526f360SManoj N. Kumar 	}
2005bfc0bab1SUma Krishnan 
2006b070545dSUma Krishnan 	WARN_ON(hwq->ctx_cookie);
2007b070545dSUma Krishnan 	hwq->ctx_cookie = ctx;
2008e11e0ff8SUma Krishnan 	hwq->num_irqs = num_irqs;
20099526f360SManoj N. Kumar 
20109526f360SManoj N. Kumar 	/* Set it up as a master with the CXL */
201125b8e08eSMatthew R. Ochs 	cfg->ops->set_master(ctx);
20129526f360SManoj N. Kumar 
2013bfc0bab1SUma Krishnan 	/* Reset AFU when initializing primary context */
2014bfc0bab1SUma Krishnan 	if (index == PRIMARY_HWQ) {
201525b8e08eSMatthew R. Ochs 		rc = cfg->ops->afu_reset(ctx);
20169526f360SManoj N. Kumar 		if (unlikely(rc)) {
2017bfc0bab1SUma Krishnan 			dev_err(dev, "%s: AFU reset failed rc=%d\n",
2018bfc0bab1SUma Krishnan 				      __func__, rc);
2019bfc0bab1SUma Krishnan 			goto err1;
2020bfc0bab1SUma Krishnan 		}
20219526f360SManoj N. Kumar 	}
20229526f360SManoj N. Kumar 
2023bfc0bab1SUma Krishnan 	level = init_intr(cfg, hwq);
20249526f360SManoj N. Kumar 	if (unlikely(level)) {
2025fb67d44dSMatthew R. Ochs 		dev_err(dev, "%s: interrupt init failed rc=%d\n", __func__, rc);
2026bfc0bab1SUma Krishnan 		goto err2;
20279526f360SManoj N. Kumar 	}
2028c21e0bbfSMatthew R. Ochs 
202925b8e08eSMatthew R. Ochs 	/* Finally, activate the context by starting it */
203025b8e08eSMatthew R. Ochs 	rc = cfg->ops->start_context(hwq->ctx_cookie);
2031c21e0bbfSMatthew R. Ochs 	if (unlikely(rc)) {
2032c21e0bbfSMatthew R. Ochs 		dev_err(dev, "%s: start context failed rc=%d\n", __func__, rc);
2033c21e0bbfSMatthew R. Ochs 		level = UNMAP_THREE;
2034bfc0bab1SUma Krishnan 		goto err2;
2035c21e0bbfSMatthew R. Ochs 	}
2036bfc0bab1SUma Krishnan 
2037bfc0bab1SUma Krishnan out:
2038fb67d44dSMatthew R. Ochs 	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
2039c21e0bbfSMatthew R. Ochs 	return rc;
2040bfc0bab1SUma Krishnan err2:
2041bfc0bab1SUma Krishnan 	term_intr(cfg, level, index);
2042bfc0bab1SUma Krishnan 	if (index != PRIMARY_HWQ)
204325b8e08eSMatthew R. Ochs 		cfg->ops->release_context(ctx);
2044bfc0bab1SUma Krishnan err1:
2045b070545dSUma Krishnan 	hwq->ctx_cookie = NULL;
2046bfc0bab1SUma Krishnan 	goto out;
2047c21e0bbfSMatthew R. Ochs }
2048c21e0bbfSMatthew R. Ochs 
2049c21e0bbfSMatthew R. Ochs /**
205056518072SMatthew R. Ochs  * get_num_afu_ports() - determines and configures the number of AFU ports
205156518072SMatthew R. Ochs  * @cfg:	Internal structure associated with the host.
205256518072SMatthew R. Ochs  *
205356518072SMatthew R. Ochs  * This routine determines the number of AFU ports by converting the global
205456518072SMatthew R. Ochs  * port selection mask. The converted value is only valid following an AFU
205556518072SMatthew R. Ochs  * reset (explicit or power-on). This routine must be invoked shortly after
205656518072SMatthew R. Ochs  * mapping as other routines are dependent on the number of ports during the
205756518072SMatthew R. Ochs  * initialization sequence.
205856518072SMatthew R. Ochs  *
205956518072SMatthew R. Ochs  * To support legacy AFUs that might not have reflected an initial global
206056518072SMatthew R. Ochs  * port mask (value read is 0), default to the number of ports originally
206156518072SMatthew R. Ochs  * supported by the cxlflash driver (2) before hardware with other port
206256518072SMatthew R. Ochs  * offerings was introduced.
206356518072SMatthew R. Ochs  */
get_num_afu_ports(struct cxlflash_cfg * cfg)206456518072SMatthew R. Ochs static void get_num_afu_ports(struct cxlflash_cfg *cfg)
206556518072SMatthew R. Ochs {
206656518072SMatthew R. Ochs 	struct afu *afu = cfg->afu;
206756518072SMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
206856518072SMatthew R. Ochs 	u64 port_mask;
206956518072SMatthew R. Ochs 	int num_fc_ports = LEGACY_FC_PORTS;
207056518072SMatthew R. Ochs 
207156518072SMatthew R. Ochs 	port_mask = readq_be(&afu->afu_map->global.regs.afu_port_sel);
207256518072SMatthew R. Ochs 	if (port_mask != 0ULL)
207356518072SMatthew R. Ochs 		num_fc_ports = min(ilog2(port_mask) + 1, MAX_FC_PORTS);
207456518072SMatthew R. Ochs 
207556518072SMatthew R. Ochs 	dev_dbg(dev, "%s: port_mask=%016llx num_fc_ports=%d\n",
207656518072SMatthew R. Ochs 		__func__, port_mask, num_fc_ports);
207756518072SMatthew R. Ochs 
207856518072SMatthew R. Ochs 	cfg->num_fc_ports = num_fc_ports;
207956518072SMatthew R. Ochs 	cfg->host->max_channel = PORTNUM2CHAN(num_fc_ports);
208056518072SMatthew R. Ochs }
208156518072SMatthew R. Ochs 
208256518072SMatthew R. Ochs /**
2083c21e0bbfSMatthew R. Ochs  * init_afu() - setup as master context and start AFU
20841284fb0cSMatthew R. Ochs  * @cfg:	Internal structure associated with the host.
2085c21e0bbfSMatthew R. Ochs  *
2086c21e0bbfSMatthew R. Ochs  * This routine is a higher level of control for configuring the
2087c21e0bbfSMatthew R. Ochs  * AFU on probe and reset paths.
2088c21e0bbfSMatthew R. Ochs  *
20891284fb0cSMatthew R. Ochs  * Return: 0 on success, -errno on failure
2090c21e0bbfSMatthew R. Ochs  */
init_afu(struct cxlflash_cfg * cfg)2091c21e0bbfSMatthew R. Ochs static int init_afu(struct cxlflash_cfg *cfg)
2092c21e0bbfSMatthew R. Ochs {
2093c21e0bbfSMatthew R. Ochs 	u64 reg;
2094c21e0bbfSMatthew R. Ochs 	int rc = 0;
2095c21e0bbfSMatthew R. Ochs 	struct afu *afu = cfg->afu;
2096c21e0bbfSMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
2097bfc0bab1SUma Krishnan 	struct hwq *hwq;
2098bfc0bab1SUma Krishnan 	int i;
2099c21e0bbfSMatthew R. Ochs 
210025b8e08eSMatthew R. Ochs 	cfg->ops->perst_reloads_same_image(cfg->afu_cookie, true);
21015cdac81aSMatthew R. Ochs 
2102e63a8d88SMatthew R. Ochs 	mutex_init(&afu->sync_active);
21033065267aSMatthew R. Ochs 	afu->num_hwqs = afu->desired_hwqs;
21043065267aSMatthew R. Ochs 	for (i = 0; i < afu->num_hwqs; i++) {
2105bfc0bab1SUma Krishnan 		rc = init_mc(cfg, i);
2106c21e0bbfSMatthew R. Ochs 		if (rc) {
2107bfc0bab1SUma Krishnan 			dev_err(dev, "%s: init_mc failed rc=%d index=%d\n",
2108bfc0bab1SUma Krishnan 				__func__, rc, i);
2109bfc0bab1SUma Krishnan 			goto err1;
2110bfc0bab1SUma Krishnan 		}
2111c21e0bbfSMatthew R. Ochs 	}
2112c21e0bbfSMatthew R. Ochs 
2113bfc0bab1SUma Krishnan 	/* Map the entire MMIO space of the AFU using the first context */
2114bfc0bab1SUma Krishnan 	hwq = get_hwq(afu, PRIMARY_HWQ);
211525b8e08eSMatthew R. Ochs 	afu->afu_map = cfg->ops->psa_map(hwq->ctx_cookie);
2116c21e0bbfSMatthew R. Ochs 	if (!afu->afu_map) {
211725b8e08eSMatthew R. Ochs 		dev_err(dev, "%s: psa_map failed\n", __func__);
2118ee3491baSMatthew R. Ochs 		rc = -ENOMEM;
2119c21e0bbfSMatthew R. Ochs 		goto err1;
2120c21e0bbfSMatthew R. Ochs 	}
2121c21e0bbfSMatthew R. Ochs 
2122e5ce067bSMatthew R. Ochs 	/* No byte reverse on reading afu_version or string will be backwards */
2123e5ce067bSMatthew R. Ochs 	reg = readq(&afu->afu_map->global.regs.afu_version);
2124e5ce067bSMatthew R. Ochs 	memcpy(afu->version, &reg, sizeof(reg));
2125c21e0bbfSMatthew R. Ochs 	afu->interface_version =
2126c21e0bbfSMatthew R. Ochs 	    readq_be(&afu->afu_map->global.regs.interface_version);
2127e5ce067bSMatthew R. Ochs 	if ((afu->interface_version + 1) == 0) {
2128fb67d44dSMatthew R. Ochs 		dev_err(dev, "Back level AFU, please upgrade. AFU version %s "
2129fb67d44dSMatthew R. Ochs 			"interface version %016llx\n", afu->version,
2130e5ce067bSMatthew R. Ochs 		       afu->interface_version);
2131e5ce067bSMatthew R. Ochs 		rc = -EINVAL;
21320df5bef7SUma Krishnan 		goto err1;
2133ee3491baSMatthew R. Ochs 	}
2134ee3491baSMatthew R. Ochs 
2135696d0b0cSMatthew R. Ochs 	if (afu_is_sq_cmd_mode(afu)) {
2136696d0b0cSMatthew R. Ochs 		afu->send_cmd = send_cmd_sq;
2137696d0b0cSMatthew R. Ochs 		afu->context_reset = context_reset_sq;
2138696d0b0cSMatthew R. Ochs 	} else {
213948b4be36SMatthew R. Ochs 		afu->send_cmd = send_cmd_ioarrin;
214048b4be36SMatthew R. Ochs 		afu->context_reset = context_reset_ioarrin;
2141696d0b0cSMatthew R. Ochs 	}
214248b4be36SMatthew R. Ochs 
2143fb67d44dSMatthew R. Ochs 	dev_dbg(dev, "%s: afu_ver=%s interface_ver=%016llx\n", __func__,
2144ee3491baSMatthew R. Ochs 		afu->version, afu->interface_version);
2145c21e0bbfSMatthew R. Ochs 
214656518072SMatthew R. Ochs 	get_num_afu_ports(cfg);
214756518072SMatthew R. Ochs 
2148c21e0bbfSMatthew R. Ochs 	rc = start_afu(cfg);
2149c21e0bbfSMatthew R. Ochs 	if (rc) {
2150fb67d44dSMatthew R. Ochs 		dev_err(dev, "%s: start_afu failed, rc=%d\n", __func__, rc);
21510df5bef7SUma Krishnan 		goto err1;
2152c21e0bbfSMatthew R. Ochs 	}
2153c21e0bbfSMatthew R. Ochs 
2154c21e0bbfSMatthew R. Ochs 	afu_err_intr_init(cfg->afu);
21553065267aSMatthew R. Ochs 	for (i = 0; i < afu->num_hwqs; i++) {
2156bfc0bab1SUma Krishnan 		hwq = get_hwq(afu, i);
2157bfc0bab1SUma Krishnan 
2158bfc0bab1SUma Krishnan 		hwq->room = readq_be(&hwq->host_map->cmd_room);
2159bfc0bab1SUma Krishnan 	}
2160c21e0bbfSMatthew R. Ochs 
21612cb79266SMatthew R. Ochs 	/* Restore the LUN mappings */
21622cb79266SMatthew R. Ochs 	cxlflash_restore_luntable(cfg);
2163ee3491baSMatthew R. Ochs out:
2164fb67d44dSMatthew R. Ochs 	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
2165c21e0bbfSMatthew R. Ochs 	return rc;
2166ee3491baSMatthew R. Ochs 
2167ee3491baSMatthew R. Ochs err1:
21683065267aSMatthew R. Ochs 	for (i = afu->num_hwqs - 1; i >= 0; i--) {
2169bfc0bab1SUma Krishnan 		term_intr(cfg, UNMAP_THREE, i);
2170bfc0bab1SUma Krishnan 		term_mc(cfg, i);
2171bfc0bab1SUma Krishnan 	}
2172ee3491baSMatthew R. Ochs 	goto out;
2173c21e0bbfSMatthew R. Ochs }
2174c21e0bbfSMatthew R. Ochs 
2175c21e0bbfSMatthew R. Ochs /**
21760b09e711SUma Krishnan  * afu_reset() - resets the AFU
21770b09e711SUma Krishnan  * @cfg:	Internal structure associated with the host.
21780b09e711SUma Krishnan  *
21790b09e711SUma Krishnan  * Return: 0 on success, -errno on failure
21800b09e711SUma Krishnan  */
afu_reset(struct cxlflash_cfg * cfg)21810b09e711SUma Krishnan static int afu_reset(struct cxlflash_cfg *cfg)
21820b09e711SUma Krishnan {
21830b09e711SUma Krishnan 	struct device *dev = &cfg->dev->dev;
21840b09e711SUma Krishnan 	int rc = 0;
21850b09e711SUma Krishnan 
21860b09e711SUma Krishnan 	/* Stop the context before the reset. Since the context is
21870b09e711SUma Krishnan 	 * no longer available restart it after the reset is complete
21880b09e711SUma Krishnan 	 */
21890b09e711SUma Krishnan 	term_afu(cfg);
21900b09e711SUma Krishnan 
21910b09e711SUma Krishnan 	rc = init_afu(cfg);
21920b09e711SUma Krishnan 
21930b09e711SUma Krishnan 	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
21940b09e711SUma Krishnan 	return rc;
21950b09e711SUma Krishnan }
21960b09e711SUma Krishnan 
21970b09e711SUma Krishnan /**
21980b09e711SUma Krishnan  * drain_ioctls() - wait until all currently executing ioctls have completed
21990b09e711SUma Krishnan  * @cfg:	Internal structure associated with the host.
22000b09e711SUma Krishnan  *
22010b09e711SUma Krishnan  * Obtain write access to read/write semaphore that wraps ioctl
22020b09e711SUma Krishnan  * handling to 'drain' ioctls currently executing.
22030b09e711SUma Krishnan  */
drain_ioctls(struct cxlflash_cfg * cfg)22040b09e711SUma Krishnan static void drain_ioctls(struct cxlflash_cfg *cfg)
22050b09e711SUma Krishnan {
22060b09e711SUma Krishnan 	down_write(&cfg->ioctl_rwsem);
22070b09e711SUma Krishnan 	up_write(&cfg->ioctl_rwsem);
22080b09e711SUma Krishnan }
22090b09e711SUma Krishnan 
22100b09e711SUma Krishnan /**
22110b09e711SUma Krishnan  * cxlflash_async_reset_host() - asynchronous host reset handler
22120b09e711SUma Krishnan  * @data:	Private data provided while scheduling reset.
22130b09e711SUma Krishnan  * @cookie:	Cookie that can be used for checkpointing.
22140b09e711SUma Krishnan  */
cxlflash_async_reset_host(void * data,async_cookie_t cookie)22150b09e711SUma Krishnan static void cxlflash_async_reset_host(void *data, async_cookie_t cookie)
22160b09e711SUma Krishnan {
22170b09e711SUma Krishnan 	struct cxlflash_cfg *cfg = data;
22180b09e711SUma Krishnan 	struct device *dev = &cfg->dev->dev;
22190b09e711SUma Krishnan 	int rc = 0;
22200b09e711SUma Krishnan 
22210b09e711SUma Krishnan 	if (cfg->state != STATE_RESET) {
22220b09e711SUma Krishnan 		dev_dbg(dev, "%s: Not performing a reset, state=%d\n",
22230b09e711SUma Krishnan 			__func__, cfg->state);
22240b09e711SUma Krishnan 		goto out;
22250b09e711SUma Krishnan 	}
22260b09e711SUma Krishnan 
22270b09e711SUma Krishnan 	drain_ioctls(cfg);
22280b09e711SUma Krishnan 	cxlflash_mark_contexts_error(cfg);
22290b09e711SUma Krishnan 	rc = afu_reset(cfg);
22300b09e711SUma Krishnan 	if (rc)
22310b09e711SUma Krishnan 		cfg->state = STATE_FAILTERM;
22320b09e711SUma Krishnan 	else
22330b09e711SUma Krishnan 		cfg->state = STATE_NORMAL;
22340b09e711SUma Krishnan 	wake_up_all(&cfg->reset_waitq);
22350b09e711SUma Krishnan 
22360b09e711SUma Krishnan out:
22370b09e711SUma Krishnan 	scsi_unblock_requests(cfg->host);
22380b09e711SUma Krishnan }
22390b09e711SUma Krishnan 
22400b09e711SUma Krishnan /**
22410b09e711SUma Krishnan  * cxlflash_schedule_async_reset() - schedule an asynchronous host reset
22420b09e711SUma Krishnan  * @cfg:	Internal structure associated with the host.
22430b09e711SUma Krishnan  */
cxlflash_schedule_async_reset(struct cxlflash_cfg * cfg)22440b09e711SUma Krishnan static void cxlflash_schedule_async_reset(struct cxlflash_cfg *cfg)
22450b09e711SUma Krishnan {
22460b09e711SUma Krishnan 	struct device *dev = &cfg->dev->dev;
22470b09e711SUma Krishnan 
22480b09e711SUma Krishnan 	if (cfg->state != STATE_NORMAL) {
22490b09e711SUma Krishnan 		dev_dbg(dev, "%s: Not performing reset state=%d\n",
22500b09e711SUma Krishnan 			__func__, cfg->state);
22510b09e711SUma Krishnan 		return;
22520b09e711SUma Krishnan 	}
22530b09e711SUma Krishnan 
22540b09e711SUma Krishnan 	cfg->state = STATE_RESET;
22550b09e711SUma Krishnan 	scsi_block_requests(cfg->host);
22560b09e711SUma Krishnan 	cfg->async_reset_cookie = async_schedule(cxlflash_async_reset_host,
22570b09e711SUma Krishnan 						 cfg);
22580b09e711SUma Krishnan }
22590b09e711SUma Krishnan 
22600b09e711SUma Krishnan /**
2261cf243027SMatthew R. Ochs  * send_afu_cmd() - builds and sends an internal AFU command
2262c21e0bbfSMatthew R. Ochs  * @afu:	AFU associated with the host.
2263cf243027SMatthew R. Ochs  * @rcb:	Pre-populated IOARCB describing command to send.
2264c21e0bbfSMatthew R. Ochs  *
2265cf243027SMatthew R. Ochs  * The AFU can only take one internal AFU command at a time. This limitation is
2266cf243027SMatthew R. Ochs  * enforced by using a mutex to provide exclusive access to the AFU during the
2267cf243027SMatthew R. Ochs  * operation. This design point requires calling threads to not be on interrupt
2268cf243027SMatthew R. Ochs  * context due to the possibility of sleeping during concurrent AFU operations.
2269c21e0bbfSMatthew R. Ochs  *
2270cf243027SMatthew R. Ochs  * The command status is optionally passed back to the caller when the caller
2271cf243027SMatthew R. Ochs  * populates the IOASA field of the IOARCB with a pointer to an IOASA structure.
22725cdac81aSMatthew R. Ochs  *
2273c21e0bbfSMatthew R. Ochs  * Return:
2274539d890cSUma Krishnan  *	0 on success, -errno on failure
2275c21e0bbfSMatthew R. Ochs  */
send_afu_cmd(struct afu * afu,struct sisl_ioarcb * rcb)2276cf243027SMatthew R. Ochs static int send_afu_cmd(struct afu *afu, struct sisl_ioarcb *rcb)
2277c21e0bbfSMatthew R. Ochs {
22785cdac81aSMatthew R. Ochs 	struct cxlflash_cfg *cfg = afu->parent;
22794392ba49SMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
2280c21e0bbfSMatthew R. Ochs 	struct afu_cmd *cmd = NULL;
2281bfc0bab1SUma Krishnan 	struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ);
22829a597cd4SUma Krishnan 	ulong lock_flags;
2283350bb478SMatthew R. Ochs 	char *buf = NULL;
2284c21e0bbfSMatthew R. Ochs 	int rc = 0;
2285a96851d3SUma Krishnan 	int nretry = 0;
2286c21e0bbfSMatthew R. Ochs 
22875cdac81aSMatthew R. Ochs 	if (cfg->state != STATE_NORMAL) {
2288fb67d44dSMatthew R. Ochs 		dev_dbg(dev, "%s: Sync not required state=%u\n",
2289fb67d44dSMatthew R. Ochs 			__func__, cfg->state);
22905cdac81aSMatthew R. Ochs 		return 0;
22915cdac81aSMatthew R. Ochs 	}
22925cdac81aSMatthew R. Ochs 
2293e63a8d88SMatthew R. Ochs 	mutex_lock(&afu->sync_active);
2294de01283bSMatthew R. Ochs 	atomic_inc(&afu->cmds_active);
2295a1ea04b3SUma Krishnan 	buf = kmalloc(sizeof(*cmd) + __alignof__(*cmd) - 1, GFP_KERNEL);
2296350bb478SMatthew R. Ochs 	if (unlikely(!buf)) {
2297350bb478SMatthew R. Ochs 		dev_err(dev, "%s: no memory for command\n", __func__);
2298539d890cSUma Krishnan 		rc = -ENOMEM;
2299c21e0bbfSMatthew R. Ochs 		goto out;
2300c21e0bbfSMatthew R. Ochs 	}
2301c21e0bbfSMatthew R. Ochs 
2302350bb478SMatthew R. Ochs 	cmd = (struct afu_cmd *)PTR_ALIGN(buf, __alignof__(*cmd));
2303a96851d3SUma Krishnan 
2304a96851d3SUma Krishnan retry:
2305a1ea04b3SUma Krishnan 	memset(cmd, 0, sizeof(*cmd));
2306cf243027SMatthew R. Ochs 	memcpy(&cmd->rcb, rcb, sizeof(*rcb));
2307a1ea04b3SUma Krishnan 	INIT_LIST_HEAD(&cmd->queue);
2308350bb478SMatthew R. Ochs 	init_completion(&cmd->cevent);
2309350bb478SMatthew R. Ochs 	cmd->parent = afu;
2310bfc0bab1SUma Krishnan 	cmd->hwq_index = hwq->index;
2311bfc0bab1SUma Krishnan 	cmd->rcb.ctx_id = hwq->ctx_hndl;
2312c21e0bbfSMatthew R. Ochs 
2313cf243027SMatthew R. Ochs 	dev_dbg(dev, "%s: afu=%p cmd=%p type=%02x nretry=%d\n",
2314cf243027SMatthew R. Ochs 		__func__, afu, cmd, cmd->rcb.cdb[0], nretry);
2315c21e0bbfSMatthew R. Ochs 
231648b4be36SMatthew R. Ochs 	rc = afu->send_cmd(afu, cmd);
2317539d890cSUma Krishnan 	if (unlikely(rc)) {
2318539d890cSUma Krishnan 		rc = -ENOBUFS;
2319c21e0bbfSMatthew R. Ochs 		goto out;
2320539d890cSUma Krishnan 	}
2321c21e0bbfSMatthew R. Ochs 
23229ba848acSMatthew R. Ochs 	rc = wait_resp(afu, cmd);
2323a1ea04b3SUma Krishnan 	switch (rc) {
2324a1ea04b3SUma Krishnan 	case -ETIMEDOUT:
2325a96851d3SUma Krishnan 		rc = afu->context_reset(hwq);
2326a1ea04b3SUma Krishnan 		if (rc) {
23279a597cd4SUma Krishnan 			/* Delete the command from pending_cmds list */
23289a597cd4SUma Krishnan 			spin_lock_irqsave(&hwq->hsq_slock, lock_flags);
23299a597cd4SUma Krishnan 			list_del(&cmd->list);
23309a597cd4SUma Krishnan 			spin_unlock_irqrestore(&hwq->hsq_slock, lock_flags);
23319a597cd4SUma Krishnan 
23320b09e711SUma Krishnan 			cxlflash_schedule_async_reset(cfg);
2333a1ea04b3SUma Krishnan 			break;
2334a1ea04b3SUma Krishnan 		}
2335df561f66SGustavo A. R. Silva 		fallthrough;	/* to retry */
2336a1ea04b3SUma Krishnan 	case -EAGAIN:
2337a1ea04b3SUma Krishnan 		if (++nretry < 2)
2338a1ea04b3SUma Krishnan 			goto retry;
2339df561f66SGustavo A. R. Silva 		fallthrough;	/* to exit */
2340a1ea04b3SUma Krishnan 	default:
2341a1ea04b3SUma Krishnan 		break;
2342a96851d3SUma Krishnan 	}
2343a96851d3SUma Krishnan 
2344cf243027SMatthew R. Ochs 	if (rcb->ioasa)
2345cf243027SMatthew R. Ochs 		*rcb->ioasa = cmd->sa;
2346c21e0bbfSMatthew R. Ochs out:
2347de01283bSMatthew R. Ochs 	atomic_dec(&afu->cmds_active);
2348e63a8d88SMatthew R. Ochs 	mutex_unlock(&afu->sync_active);
2349350bb478SMatthew R. Ochs 	kfree(buf);
2350fb67d44dSMatthew R. Ochs 	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
2351c21e0bbfSMatthew R. Ochs 	return rc;
2352c21e0bbfSMatthew R. Ochs }
2353c21e0bbfSMatthew R. Ochs 
2354c21e0bbfSMatthew R. Ochs /**
2355cf243027SMatthew R. Ochs  * cxlflash_afu_sync() - builds and sends an AFU sync command
2356cf243027SMatthew R. Ochs  * @afu:	AFU associated with the host.
2357cf243027SMatthew R. Ochs  * @ctx:	Identifies context requesting sync.
2358cf243027SMatthew R. Ochs  * @res:	Identifies resource requesting sync.
2359cf243027SMatthew R. Ochs  * @mode:	Type of sync to issue (lightweight, heavyweight, global).
2360cf243027SMatthew R. Ochs  *
2361cf243027SMatthew R. Ochs  * AFU sync operations are only necessary and allowed when the device is
2362cf243027SMatthew R. Ochs  * operating normally. When not operating normally, sync requests can occur as
2363cf243027SMatthew R. Ochs  * part of cleaning up resources associated with an adapter prior to removal.
2364cf243027SMatthew R. Ochs  * In this scenario, these requests are simply ignored (safe due to the AFU
2365cf243027SMatthew R. Ochs  * going away).
2366cf243027SMatthew R. Ochs  *
2367cf243027SMatthew R. Ochs  * Return:
2368cf243027SMatthew R. Ochs  *	0 on success, -errno on failure
2369cf243027SMatthew R. Ochs  */
cxlflash_afu_sync(struct afu * afu,ctx_hndl_t ctx,res_hndl_t res,u8 mode)2370cf243027SMatthew R. Ochs int cxlflash_afu_sync(struct afu *afu, ctx_hndl_t ctx, res_hndl_t res, u8 mode)
2371cf243027SMatthew R. Ochs {
2372cf243027SMatthew R. Ochs 	struct cxlflash_cfg *cfg = afu->parent;
2373cf243027SMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
2374cf243027SMatthew R. Ochs 	struct sisl_ioarcb rcb = { 0 };
2375cf243027SMatthew R. Ochs 
2376cf243027SMatthew R. Ochs 	dev_dbg(dev, "%s: afu=%p ctx=%u res=%u mode=%u\n",
2377cf243027SMatthew R. Ochs 		__func__, afu, ctx, res, mode);
2378cf243027SMatthew R. Ochs 
2379cf243027SMatthew R. Ochs 	rcb.req_flags = SISL_REQ_FLAGS_AFU_CMD;
2380cf243027SMatthew R. Ochs 	rcb.msi = SISL_MSI_RRQ_UPDATED;
2381cf243027SMatthew R. Ochs 	rcb.timeout = MC_AFU_SYNC_TIMEOUT;
2382cf243027SMatthew R. Ochs 
2383cf243027SMatthew R. Ochs 	rcb.cdb[0] = SISL_AFU_CMD_SYNC;
2384cf243027SMatthew R. Ochs 	rcb.cdb[1] = mode;
2385cf243027SMatthew R. Ochs 	put_unaligned_be16(ctx, &rcb.cdb[2]);
2386cf243027SMatthew R. Ochs 	put_unaligned_be32(res, &rcb.cdb[4]);
2387cf243027SMatthew R. Ochs 
2388cf243027SMatthew R. Ochs 	return send_afu_cmd(afu, &rcb);
2389cf243027SMatthew R. Ochs }
2390cf243027SMatthew R. Ochs 
2391cf243027SMatthew R. Ochs /**
23927c4c41f1SUma Krishnan  * cxlflash_eh_abort_handler() - abort a SCSI command
23937c4c41f1SUma Krishnan  * @scp:	SCSI command to abort.
23947c4c41f1SUma Krishnan  *
23957c4c41f1SUma Krishnan  * CXL Flash devices do not support a single command abort. Reset the context
23967c4c41f1SUma Krishnan  * as per SISLite specification. Flush any pending commands in the hardware
23977c4c41f1SUma Krishnan  * queue before the reset.
23987c4c41f1SUma Krishnan  *
23997c4c41f1SUma Krishnan  * Return: SUCCESS/FAILED as defined in scsi/scsi.h
24007c4c41f1SUma Krishnan  */
cxlflash_eh_abort_handler(struct scsi_cmnd * scp)24017c4c41f1SUma Krishnan static int cxlflash_eh_abort_handler(struct scsi_cmnd *scp)
24027c4c41f1SUma Krishnan {
24037c4c41f1SUma Krishnan 	int rc = FAILED;
24047c4c41f1SUma Krishnan 	struct Scsi_Host *host = scp->device->host;
24057c4c41f1SUma Krishnan 	struct cxlflash_cfg *cfg = shost_priv(host);
24067c4c41f1SUma Krishnan 	struct afu_cmd *cmd = sc_to_afuc(scp);
24077c4c41f1SUma Krishnan 	struct device *dev = &cfg->dev->dev;
24087c4c41f1SUma Krishnan 	struct afu *afu = cfg->afu;
24097c4c41f1SUma Krishnan 	struct hwq *hwq = get_hwq(afu, cmd->hwq_index);
24107c4c41f1SUma Krishnan 
24117c4c41f1SUma Krishnan 	dev_dbg(dev, "%s: (scp=%p) %d/%d/%d/%llu "
24127c4c41f1SUma Krishnan 		"cdb=(%08x-%08x-%08x-%08x)\n", __func__, scp, host->host_no,
24137c4c41f1SUma Krishnan 		scp->device->channel, scp->device->id, scp->device->lun,
24147c4c41f1SUma Krishnan 		get_unaligned_be32(&((u32 *)scp->cmnd)[0]),
24157c4c41f1SUma Krishnan 		get_unaligned_be32(&((u32 *)scp->cmnd)[1]),
24167c4c41f1SUma Krishnan 		get_unaligned_be32(&((u32 *)scp->cmnd)[2]),
24177c4c41f1SUma Krishnan 		get_unaligned_be32(&((u32 *)scp->cmnd)[3]));
24187c4c41f1SUma Krishnan 
24197c4c41f1SUma Krishnan 	/* When the state is not normal, another reset/reload is in progress.
24207c4c41f1SUma Krishnan 	 * Return failed and the mid-layer will invoke host reset handler.
24217c4c41f1SUma Krishnan 	 */
24227c4c41f1SUma Krishnan 	if (cfg->state != STATE_NORMAL) {
24237c4c41f1SUma Krishnan 		dev_dbg(dev, "%s: Invalid state for abort, state=%d\n",
24247c4c41f1SUma Krishnan 			__func__, cfg->state);
24257c4c41f1SUma Krishnan 		goto out;
24267c4c41f1SUma Krishnan 	}
24277c4c41f1SUma Krishnan 
24287c4c41f1SUma Krishnan 	rc = afu->context_reset(hwq);
24297c4c41f1SUma Krishnan 	if (unlikely(rc))
24307c4c41f1SUma Krishnan 		goto out;
24317c4c41f1SUma Krishnan 
24327c4c41f1SUma Krishnan 	rc = SUCCESS;
24337c4c41f1SUma Krishnan 
24347c4c41f1SUma Krishnan out:
24357c4c41f1SUma Krishnan 	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
24367c4c41f1SUma Krishnan 	return rc;
24377c4c41f1SUma Krishnan }
24387c4c41f1SUma Krishnan 
24397c4c41f1SUma Krishnan /**
244015305514SMatthew R. Ochs  * cxlflash_eh_device_reset_handler() - reset a single LUN
244115305514SMatthew R. Ochs  * @scp:	SCSI command to send.
244215305514SMatthew R. Ochs  *
244315305514SMatthew R. Ochs  * Return:
244415305514SMatthew R. Ochs  *	SUCCESS as defined in scsi/scsi.h
244515305514SMatthew R. Ochs  *	FAILED as defined in scsi/scsi.h
244615305514SMatthew R. Ochs  */
cxlflash_eh_device_reset_handler(struct scsi_cmnd * scp)244715305514SMatthew R. Ochs static int cxlflash_eh_device_reset_handler(struct scsi_cmnd *scp)
244815305514SMatthew R. Ochs {
244915305514SMatthew R. Ochs 	int rc = SUCCESS;
245032abbedaSMatthew R. Ochs 	struct scsi_device *sdev = scp->device;
245132abbedaSMatthew R. Ochs 	struct Scsi_Host *host = sdev->host;
2452fb67d44dSMatthew R. Ochs 	struct cxlflash_cfg *cfg = shost_priv(host);
2453fb67d44dSMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
245415305514SMatthew R. Ochs 	int rcr = 0;
245515305514SMatthew R. Ochs 
24565a4d9d77SMatthew R. Ochs 	dev_dbg(dev, "%s: %d/%d/%d/%llu\n", __func__,
24575a4d9d77SMatthew R. Ochs 		host->host_no, sdev->channel, sdev->id, sdev->lun);
2458ed486daaSMatthew R. Ochs retry:
245915305514SMatthew R. Ochs 	switch (cfg->state) {
246015305514SMatthew R. Ochs 	case STATE_NORMAL:
246132abbedaSMatthew R. Ochs 		rcr = send_tmf(cfg, sdev, TMF_LUN_RESET);
246215305514SMatthew R. Ochs 		if (unlikely(rcr))
246315305514SMatthew R. Ochs 			rc = FAILED;
246415305514SMatthew R. Ochs 		break;
246515305514SMatthew R. Ochs 	case STATE_RESET:
246615305514SMatthew R. Ochs 		wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
2467ed486daaSMatthew R. Ochs 		goto retry;
246815305514SMatthew R. Ochs 	default:
246915305514SMatthew R. Ochs 		rc = FAILED;
247015305514SMatthew R. Ochs 		break;
247115305514SMatthew R. Ochs 	}
247215305514SMatthew R. Ochs 
2473fb67d44dSMatthew R. Ochs 	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
247415305514SMatthew R. Ochs 	return rc;
247515305514SMatthew R. Ochs }
247615305514SMatthew R. Ochs 
247715305514SMatthew R. Ochs /**
247815305514SMatthew R. Ochs  * cxlflash_eh_host_reset_handler() - reset the host adapter
247915305514SMatthew R. Ochs  * @scp:	SCSI command from stack identifying host.
248015305514SMatthew R. Ochs  *
24811d3324c3SMatthew R. Ochs  * Following a reset, the state is evaluated again in case an EEH occurred
24821d3324c3SMatthew R. Ochs  * during the reset. In such a scenario, the host reset will either yield
24831d3324c3SMatthew R. Ochs  * until the EEH recovery is complete or return success or failure based
24841d3324c3SMatthew R. Ochs  * upon the current device state.
24851d3324c3SMatthew R. Ochs  *
248615305514SMatthew R. Ochs  * Return:
248715305514SMatthew R. Ochs  *	SUCCESS as defined in scsi/scsi.h
248815305514SMatthew R. Ochs  *	FAILED as defined in scsi/scsi.h
248915305514SMatthew R. Ochs  */
cxlflash_eh_host_reset_handler(struct scsi_cmnd * scp)249015305514SMatthew R. Ochs static int cxlflash_eh_host_reset_handler(struct scsi_cmnd *scp)
249115305514SMatthew R. Ochs {
249215305514SMatthew R. Ochs 	int rc = SUCCESS;
249315305514SMatthew R. Ochs 	int rcr = 0;
249415305514SMatthew R. Ochs 	struct Scsi_Host *host = scp->device->host;
2495fb67d44dSMatthew R. Ochs 	struct cxlflash_cfg *cfg = shost_priv(host);
2496fb67d44dSMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
249715305514SMatthew R. Ochs 
24985a4d9d77SMatthew R. Ochs 	dev_dbg(dev, "%s: %d\n", __func__, host->host_no);
249915305514SMatthew R. Ochs 
250015305514SMatthew R. Ochs 	switch (cfg->state) {
250115305514SMatthew R. Ochs 	case STATE_NORMAL:
250215305514SMatthew R. Ochs 		cfg->state = STATE_RESET;
2503f411396dSManoj N. Kumar 		drain_ioctls(cfg);
250415305514SMatthew R. Ochs 		cxlflash_mark_contexts_error(cfg);
250515305514SMatthew R. Ochs 		rcr = afu_reset(cfg);
250615305514SMatthew R. Ochs 		if (rcr) {
250715305514SMatthew R. Ochs 			rc = FAILED;
250815305514SMatthew R. Ochs 			cfg->state = STATE_FAILTERM;
250915305514SMatthew R. Ochs 		} else
251015305514SMatthew R. Ochs 			cfg->state = STATE_NORMAL;
251115305514SMatthew R. Ochs 		wake_up_all(&cfg->reset_waitq);
25121d3324c3SMatthew R. Ochs 		ssleep(1);
2513df561f66SGustavo A. R. Silva 		fallthrough;
251415305514SMatthew R. Ochs 	case STATE_RESET:
251515305514SMatthew R. Ochs 		wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
251615305514SMatthew R. Ochs 		if (cfg->state == STATE_NORMAL)
251715305514SMatthew R. Ochs 			break;
2518df561f66SGustavo A. R. Silva 		fallthrough;
251915305514SMatthew R. Ochs 	default:
252015305514SMatthew R. Ochs 		rc = FAILED;
252115305514SMatthew R. Ochs 		break;
252215305514SMatthew R. Ochs 	}
252315305514SMatthew R. Ochs 
2524fb67d44dSMatthew R. Ochs 	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
252515305514SMatthew R. Ochs 	return rc;
252615305514SMatthew R. Ochs }
252715305514SMatthew R. Ochs 
252815305514SMatthew R. Ochs /**
252915305514SMatthew R. Ochs  * cxlflash_change_queue_depth() - change the queue depth for the device
253015305514SMatthew R. Ochs  * @sdev:	SCSI device destined for queue depth change.
253115305514SMatthew R. Ochs  * @qdepth:	Requested queue depth value to set.
253215305514SMatthew R. Ochs  *
253315305514SMatthew R. Ochs  * The requested queue depth is capped to the maximum supported value.
253415305514SMatthew R. Ochs  *
253515305514SMatthew R. Ochs  * Return: The actual queue depth set.
253615305514SMatthew R. Ochs  */
cxlflash_change_queue_depth(struct scsi_device * sdev,int qdepth)253715305514SMatthew R. Ochs static int cxlflash_change_queue_depth(struct scsi_device *sdev, int qdepth)
253815305514SMatthew R. Ochs {
253915305514SMatthew R. Ochs 
254015305514SMatthew R. Ochs 	if (qdepth > CXLFLASH_MAX_CMDS_PER_LUN)
254115305514SMatthew R. Ochs 		qdepth = CXLFLASH_MAX_CMDS_PER_LUN;
254215305514SMatthew R. Ochs 
254315305514SMatthew R. Ochs 	scsi_change_queue_depth(sdev, qdepth);
254415305514SMatthew R. Ochs 	return sdev->queue_depth;
254515305514SMatthew R. Ochs }
254615305514SMatthew R. Ochs 
254715305514SMatthew R. Ochs /**
254815305514SMatthew R. Ochs  * cxlflash_show_port_status() - queries and presents the current port status
2549e0f01a21SMatthew R. Ochs  * @port:	Desired port for status reporting.
25503b225cd3SMatthew R. Ochs  * @cfg:	Internal structure associated with the host.
255115305514SMatthew R. Ochs  * @buf:	Buffer of length PAGE_SIZE to report back port status in ASCII.
255215305514SMatthew R. Ochs  *
255378ae028eSMatthew R. Ochs  * Return: The size of the ASCII string returned in @buf or -EINVAL.
255415305514SMatthew R. Ochs  */
cxlflash_show_port_status(u32 port,struct cxlflash_cfg * cfg,char * buf)25553b225cd3SMatthew R. Ochs static ssize_t cxlflash_show_port_status(u32 port,
25563b225cd3SMatthew R. Ochs 					 struct cxlflash_cfg *cfg,
25573b225cd3SMatthew R. Ochs 					 char *buf)
255815305514SMatthew R. Ochs {
255978ae028eSMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
256015305514SMatthew R. Ochs 	char *disp_status;
256115305514SMatthew R. Ochs 	u64 status;
25620aa14887SMatthew R. Ochs 	__be64 __iomem *fc_port_regs;
256315305514SMatthew R. Ochs 
256478ae028eSMatthew R. Ochs 	WARN_ON(port >= MAX_FC_PORTS);
256578ae028eSMatthew R. Ochs 
256678ae028eSMatthew R. Ochs 	if (port >= cfg->num_fc_ports) {
256778ae028eSMatthew R. Ochs 		dev_info(dev, "%s: Port %d not supported on this card.\n",
256878ae028eSMatthew R. Ochs 			__func__, port);
256978ae028eSMatthew R. Ochs 		return -EINVAL;
257078ae028eSMatthew R. Ochs 	}
257115305514SMatthew R. Ochs 
25720aa14887SMatthew R. Ochs 	fc_port_regs = get_fc_port_regs(cfg, port);
25730aa14887SMatthew R. Ochs 	status = readq_be(&fc_port_regs[FC_MTIP_STATUS / 8]);
2574e0f01a21SMatthew R. Ochs 	status &= FC_MTIP_STATUS_MASK;
257515305514SMatthew R. Ochs 
257615305514SMatthew R. Ochs 	if (status == FC_MTIP_STATUS_ONLINE)
257715305514SMatthew R. Ochs 		disp_status = "online";
257815305514SMatthew R. Ochs 	else if (status == FC_MTIP_STATUS_OFFLINE)
257915305514SMatthew R. Ochs 		disp_status = "offline";
258015305514SMatthew R. Ochs 	else
258115305514SMatthew R. Ochs 		disp_status = "unknown";
258215305514SMatthew R. Ochs 
2583e0f01a21SMatthew R. Ochs 	return scnprintf(buf, PAGE_SIZE, "%s\n", disp_status);
258415305514SMatthew R. Ochs }
258515305514SMatthew R. Ochs 
258615305514SMatthew R. Ochs /**
2587e0f01a21SMatthew R. Ochs  * port0_show() - queries and presents the current status of port 0
2588e0f01a21SMatthew R. Ochs  * @dev:	Generic device associated with the host owning the port.
2589e0f01a21SMatthew R. Ochs  * @attr:	Device attribute representing the port.
2590e0f01a21SMatthew R. Ochs  * @buf:	Buffer of length PAGE_SIZE to report back port status in ASCII.
2591e0f01a21SMatthew R. Ochs  *
2592e0f01a21SMatthew R. Ochs  * Return: The size of the ASCII string returned in @buf.
2593e0f01a21SMatthew R. Ochs  */
port0_show(struct device * dev,struct device_attribute * attr,char * buf)2594e0f01a21SMatthew R. Ochs static ssize_t port0_show(struct device *dev,
2595e0f01a21SMatthew R. Ochs 			  struct device_attribute *attr,
2596e0f01a21SMatthew R. Ochs 			  char *buf)
2597e0f01a21SMatthew R. Ochs {
2598fb67d44dSMatthew R. Ochs 	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2599e0f01a21SMatthew R. Ochs 
26003b225cd3SMatthew R. Ochs 	return cxlflash_show_port_status(0, cfg, buf);
2601e0f01a21SMatthew R. Ochs }
2602e0f01a21SMatthew R. Ochs 
2603e0f01a21SMatthew R. Ochs /**
2604e0f01a21SMatthew R. Ochs  * port1_show() - queries and presents the current status of port 1
2605e0f01a21SMatthew R. Ochs  * @dev:	Generic device associated with the host owning the port.
2606e0f01a21SMatthew R. Ochs  * @attr:	Device attribute representing the port.
2607e0f01a21SMatthew R. Ochs  * @buf:	Buffer of length PAGE_SIZE to report back port status in ASCII.
2608e0f01a21SMatthew R. Ochs  *
2609e0f01a21SMatthew R. Ochs  * Return: The size of the ASCII string returned in @buf.
2610e0f01a21SMatthew R. Ochs  */
port1_show(struct device * dev,struct device_attribute * attr,char * buf)2611e0f01a21SMatthew R. Ochs static ssize_t port1_show(struct device *dev,
2612e0f01a21SMatthew R. Ochs 			  struct device_attribute *attr,
2613e0f01a21SMatthew R. Ochs 			  char *buf)
2614e0f01a21SMatthew R. Ochs {
2615fb67d44dSMatthew R. Ochs 	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2616e0f01a21SMatthew R. Ochs 
26173b225cd3SMatthew R. Ochs 	return cxlflash_show_port_status(1, cfg, buf);
2618e0f01a21SMatthew R. Ochs }
2619e0f01a21SMatthew R. Ochs 
2620e0f01a21SMatthew R. Ochs /**
26211cd7fabcSMatthew R. Ochs  * port2_show() - queries and presents the current status of port 2
26221cd7fabcSMatthew R. Ochs  * @dev:	Generic device associated with the host owning the port.
26231cd7fabcSMatthew R. Ochs  * @attr:	Device attribute representing the port.
26241cd7fabcSMatthew R. Ochs  * @buf:	Buffer of length PAGE_SIZE to report back port status in ASCII.
26251cd7fabcSMatthew R. Ochs  *
26261cd7fabcSMatthew R. Ochs  * Return: The size of the ASCII string returned in @buf.
26271cd7fabcSMatthew R. Ochs  */
port2_show(struct device * dev,struct device_attribute * attr,char * buf)26281cd7fabcSMatthew R. Ochs static ssize_t port2_show(struct device *dev,
26291cd7fabcSMatthew R. Ochs 			  struct device_attribute *attr,
26301cd7fabcSMatthew R. Ochs 			  char *buf)
26311cd7fabcSMatthew R. Ochs {
26321cd7fabcSMatthew R. Ochs 	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
26331cd7fabcSMatthew R. Ochs 
26341cd7fabcSMatthew R. Ochs 	return cxlflash_show_port_status(2, cfg, buf);
26351cd7fabcSMatthew R. Ochs }
26361cd7fabcSMatthew R. Ochs 
26371cd7fabcSMatthew R. Ochs /**
26381cd7fabcSMatthew R. Ochs  * port3_show() - queries and presents the current status of port 3
26391cd7fabcSMatthew R. Ochs  * @dev:	Generic device associated with the host owning the port.
26401cd7fabcSMatthew R. Ochs  * @attr:	Device attribute representing the port.
26411cd7fabcSMatthew R. Ochs  * @buf:	Buffer of length PAGE_SIZE to report back port status in ASCII.
26421cd7fabcSMatthew R. Ochs  *
26431cd7fabcSMatthew R. Ochs  * Return: The size of the ASCII string returned in @buf.
26441cd7fabcSMatthew R. Ochs  */
port3_show(struct device * dev,struct device_attribute * attr,char * buf)26451cd7fabcSMatthew R. Ochs static ssize_t port3_show(struct device *dev,
26461cd7fabcSMatthew R. Ochs 			  struct device_attribute *attr,
26471cd7fabcSMatthew R. Ochs 			  char *buf)
26481cd7fabcSMatthew R. Ochs {
26491cd7fabcSMatthew R. Ochs 	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
26501cd7fabcSMatthew R. Ochs 
26511cd7fabcSMatthew R. Ochs 	return cxlflash_show_port_status(3, cfg, buf);
26521cd7fabcSMatthew R. Ochs }
26531cd7fabcSMatthew R. Ochs 
26541cd7fabcSMatthew R. Ochs /**
2655e0f01a21SMatthew R. Ochs  * lun_mode_show() - presents the current LUN mode of the host
265615305514SMatthew R. Ochs  * @dev:	Generic device associated with the host.
2657e0f01a21SMatthew R. Ochs  * @attr:	Device attribute representing the LUN mode.
265815305514SMatthew R. Ochs  * @buf:	Buffer of length PAGE_SIZE to report back the LUN mode in ASCII.
265915305514SMatthew R. Ochs  *
266015305514SMatthew R. Ochs  * Return: The size of the ASCII string returned in @buf.
266115305514SMatthew R. Ochs  */
lun_mode_show(struct device * dev,struct device_attribute * attr,char * buf)2662e0f01a21SMatthew R. Ochs static ssize_t lun_mode_show(struct device *dev,
266315305514SMatthew R. Ochs 			     struct device_attribute *attr, char *buf)
266415305514SMatthew R. Ochs {
2665fb67d44dSMatthew R. Ochs 	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
266615305514SMatthew R. Ochs 	struct afu *afu = cfg->afu;
266715305514SMatthew R. Ochs 
2668e0f01a21SMatthew R. Ochs 	return scnprintf(buf, PAGE_SIZE, "%u\n", afu->internal_lun);
266915305514SMatthew R. Ochs }
267015305514SMatthew R. Ochs 
267115305514SMatthew R. Ochs /**
2672e0f01a21SMatthew R. Ochs  * lun_mode_store() - sets the LUN mode of the host
267315305514SMatthew R. Ochs  * @dev:	Generic device associated with the host.
2674e0f01a21SMatthew R. Ochs  * @attr:	Device attribute representing the LUN mode.
267515305514SMatthew R. Ochs  * @buf:	Buffer of length PAGE_SIZE containing the LUN mode in ASCII.
267615305514SMatthew R. Ochs  * @count:	Length of data resizing in @buf.
267715305514SMatthew R. Ochs  *
267815305514SMatthew R. Ochs  * The CXL Flash AFU supports a dummy LUN mode where the external
267915305514SMatthew R. Ochs  * links and storage are not required. Space on the FPGA is used
268015305514SMatthew R. Ochs  * to create 1 or 2 small LUNs which are presented to the system
268115305514SMatthew R. Ochs  * as if they were a normal storage device. This feature is useful
268215305514SMatthew R. Ochs  * during development and also provides manufacturing with a way
268315305514SMatthew R. Ochs  * to test the AFU without an actual device.
268415305514SMatthew R. Ochs  *
268515305514SMatthew R. Ochs  * 0 = external LUN[s] (default)
268615305514SMatthew R. Ochs  * 1 = internal LUN (1 x 64K, 512B blocks, id 0)
268715305514SMatthew R. Ochs  * 2 = internal LUN (1 x 64K, 4K blocks, id 0)
268815305514SMatthew R. Ochs  * 3 = internal LUN (2 x 32K, 512B blocks, ids 0,1)
268915305514SMatthew R. Ochs  * 4 = internal LUN (2 x 32K, 4K blocks, ids 0,1)
269015305514SMatthew R. Ochs  *
269115305514SMatthew R. Ochs  * Return: The size of the ASCII string returned in @buf.
269215305514SMatthew R. Ochs  */
lun_mode_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2693e0f01a21SMatthew R. Ochs static ssize_t lun_mode_store(struct device *dev,
269415305514SMatthew R. Ochs 			      struct device_attribute *attr,
269515305514SMatthew R. Ochs 			      const char *buf, size_t count)
269615305514SMatthew R. Ochs {
269715305514SMatthew R. Ochs 	struct Scsi_Host *shost = class_to_shost(dev);
2698fb67d44dSMatthew R. Ochs 	struct cxlflash_cfg *cfg = shost_priv(shost);
269915305514SMatthew R. Ochs 	struct afu *afu = cfg->afu;
270015305514SMatthew R. Ochs 	int rc;
270115305514SMatthew R. Ochs 	u32 lun_mode;
270215305514SMatthew R. Ochs 
270315305514SMatthew R. Ochs 	rc = kstrtouint(buf, 10, &lun_mode);
270415305514SMatthew R. Ochs 	if (!rc && (lun_mode < 5) && (lun_mode != afu->internal_lun)) {
270515305514SMatthew R. Ochs 		afu->internal_lun = lun_mode;
2706603ecce9SManoj N. Kumar 
2707603ecce9SManoj N. Kumar 		/*
2708603ecce9SManoj N. Kumar 		 * When configured for internal LUN, there is only one channel,
270978ae028eSMatthew R. Ochs 		 * channel number 0, else there will be one less than the number
271078ae028eSMatthew R. Ochs 		 * of fc ports for this card.
2711603ecce9SManoj N. Kumar 		 */
2712603ecce9SManoj N. Kumar 		if (afu->internal_lun)
2713603ecce9SManoj N. Kumar 			shost->max_channel = 0;
2714603ecce9SManoj N. Kumar 		else
27158fa4f177SMatthew R. Ochs 			shost->max_channel = PORTNUM2CHAN(cfg->num_fc_ports);
2716603ecce9SManoj N. Kumar 
271715305514SMatthew R. Ochs 		afu_reset(cfg);
271815305514SMatthew R. Ochs 		scsi_scan_host(cfg->host);
271915305514SMatthew R. Ochs 	}
272015305514SMatthew R. Ochs 
272115305514SMatthew R. Ochs 	return count;
272215305514SMatthew R. Ochs }
272315305514SMatthew R. Ochs 
272415305514SMatthew R. Ochs /**
2725e0f01a21SMatthew R. Ochs  * ioctl_version_show() - presents the current ioctl version of the host
272615305514SMatthew R. Ochs  * @dev:	Generic device associated with the host.
272715305514SMatthew R. Ochs  * @attr:	Device attribute representing the ioctl version.
272815305514SMatthew R. Ochs  * @buf:	Buffer of length PAGE_SIZE to report back the ioctl version.
272915305514SMatthew R. Ochs  *
273015305514SMatthew R. Ochs  * Return: The size of the ASCII string returned in @buf.
273115305514SMatthew R. Ochs  */
ioctl_version_show(struct device * dev,struct device_attribute * attr,char * buf)2732e0f01a21SMatthew R. Ochs static ssize_t ioctl_version_show(struct device *dev,
2733e0f01a21SMatthew R. Ochs 				  struct device_attribute *attr, char *buf)
273415305514SMatthew R. Ochs {
2735d6e32f53SMatthew R. Ochs 	ssize_t bytes = 0;
2736d6e32f53SMatthew R. Ochs 
2737d6e32f53SMatthew R. Ochs 	bytes = scnprintf(buf, PAGE_SIZE,
2738d6e32f53SMatthew R. Ochs 			  "disk: %u\n", DK_CXLFLASH_VERSION_0);
2739d6e32f53SMatthew R. Ochs 	bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
2740d6e32f53SMatthew R. Ochs 			   "host: %u\n", HT_CXLFLASH_VERSION_0);
2741d6e32f53SMatthew R. Ochs 
2742d6e32f53SMatthew R. Ochs 	return bytes;
274315305514SMatthew R. Ochs }
274415305514SMatthew R. Ochs 
274515305514SMatthew R. Ochs /**
2746e0f01a21SMatthew R. Ochs  * cxlflash_show_port_lun_table() - queries and presents the port LUN table
2747e0f01a21SMatthew R. Ochs  * @port:	Desired port for status reporting.
27483b225cd3SMatthew R. Ochs  * @cfg:	Internal structure associated with the host.
2749e0f01a21SMatthew R. Ochs  * @buf:	Buffer of length PAGE_SIZE to report back port status in ASCII.
2750e0f01a21SMatthew R. Ochs  *
275178ae028eSMatthew R. Ochs  * Return: The size of the ASCII string returned in @buf or -EINVAL.
2752e0f01a21SMatthew R. Ochs  */
cxlflash_show_port_lun_table(u32 port,struct cxlflash_cfg * cfg,char * buf)2753e0f01a21SMatthew R. Ochs static ssize_t cxlflash_show_port_lun_table(u32 port,
27543b225cd3SMatthew R. Ochs 					    struct cxlflash_cfg *cfg,
2755e0f01a21SMatthew R. Ochs 					    char *buf)
2756e0f01a21SMatthew R. Ochs {
275778ae028eSMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
27580aa14887SMatthew R. Ochs 	__be64 __iomem *fc_port_luns;
2759e0f01a21SMatthew R. Ochs 	int i;
2760e0f01a21SMatthew R. Ochs 	ssize_t bytes = 0;
2761e0f01a21SMatthew R. Ochs 
276278ae028eSMatthew R. Ochs 	WARN_ON(port >= MAX_FC_PORTS);
276378ae028eSMatthew R. Ochs 
276478ae028eSMatthew R. Ochs 	if (port >= cfg->num_fc_ports) {
276578ae028eSMatthew R. Ochs 		dev_info(dev, "%s: Port %d not supported on this card.\n",
276678ae028eSMatthew R. Ochs 			__func__, port);
276778ae028eSMatthew R. Ochs 		return -EINVAL;
276878ae028eSMatthew R. Ochs 	}
2769e0f01a21SMatthew R. Ochs 
27700aa14887SMatthew R. Ochs 	fc_port_luns = get_fc_port_luns(cfg, port);
2771e0f01a21SMatthew R. Ochs 
2772e0f01a21SMatthew R. Ochs 	for (i = 0; i < CXLFLASH_NUM_VLUNS; i++)
2773e0f01a21SMatthew R. Ochs 		bytes += scnprintf(buf + bytes, PAGE_SIZE - bytes,
27740aa14887SMatthew R. Ochs 				   "%03d: %016llx\n",
27750aa14887SMatthew R. Ochs 				   i, readq_be(&fc_port_luns[i]));
2776e0f01a21SMatthew R. Ochs 	return bytes;
2777e0f01a21SMatthew R. Ochs }
2778e0f01a21SMatthew R. Ochs 
2779e0f01a21SMatthew R. Ochs /**
2780e0f01a21SMatthew R. Ochs  * port0_lun_table_show() - presents the current LUN table of port 0
2781e0f01a21SMatthew R. Ochs  * @dev:	Generic device associated with the host owning the port.
2782e0f01a21SMatthew R. Ochs  * @attr:	Device attribute representing the port.
2783e0f01a21SMatthew R. Ochs  * @buf:	Buffer of length PAGE_SIZE to report back port status in ASCII.
2784e0f01a21SMatthew R. Ochs  *
2785e0f01a21SMatthew R. Ochs  * Return: The size of the ASCII string returned in @buf.
2786e0f01a21SMatthew R. Ochs  */
port0_lun_table_show(struct device * dev,struct device_attribute * attr,char * buf)2787e0f01a21SMatthew R. Ochs static ssize_t port0_lun_table_show(struct device *dev,
2788e0f01a21SMatthew R. Ochs 				    struct device_attribute *attr,
2789e0f01a21SMatthew R. Ochs 				    char *buf)
2790e0f01a21SMatthew R. Ochs {
2791fb67d44dSMatthew R. Ochs 	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2792e0f01a21SMatthew R. Ochs 
27933b225cd3SMatthew R. Ochs 	return cxlflash_show_port_lun_table(0, cfg, buf);
2794e0f01a21SMatthew R. Ochs }
2795e0f01a21SMatthew R. Ochs 
2796e0f01a21SMatthew R. Ochs /**
2797e0f01a21SMatthew R. Ochs  * port1_lun_table_show() - presents the current LUN table of port 1
2798e0f01a21SMatthew R. Ochs  * @dev:	Generic device associated with the host owning the port.
2799e0f01a21SMatthew R. Ochs  * @attr:	Device attribute representing the port.
2800e0f01a21SMatthew R. Ochs  * @buf:	Buffer of length PAGE_SIZE to report back port status in ASCII.
2801e0f01a21SMatthew R. Ochs  *
2802e0f01a21SMatthew R. Ochs  * Return: The size of the ASCII string returned in @buf.
2803e0f01a21SMatthew R. Ochs  */
port1_lun_table_show(struct device * dev,struct device_attribute * attr,char * buf)2804e0f01a21SMatthew R. Ochs static ssize_t port1_lun_table_show(struct device *dev,
2805e0f01a21SMatthew R. Ochs 				    struct device_attribute *attr,
2806e0f01a21SMatthew R. Ochs 				    char *buf)
2807e0f01a21SMatthew R. Ochs {
2808fb67d44dSMatthew R. Ochs 	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2809e0f01a21SMatthew R. Ochs 
28103b225cd3SMatthew R. Ochs 	return cxlflash_show_port_lun_table(1, cfg, buf);
2811e0f01a21SMatthew R. Ochs }
2812e0f01a21SMatthew R. Ochs 
2813e0f01a21SMatthew R. Ochs /**
28141cd7fabcSMatthew R. Ochs  * port2_lun_table_show() - presents the current LUN table of port 2
28151cd7fabcSMatthew R. Ochs  * @dev:	Generic device associated with the host owning the port.
28161cd7fabcSMatthew R. Ochs  * @attr:	Device attribute representing the port.
28171cd7fabcSMatthew R. Ochs  * @buf:	Buffer of length PAGE_SIZE to report back port status in ASCII.
28181cd7fabcSMatthew R. Ochs  *
28191cd7fabcSMatthew R. Ochs  * Return: The size of the ASCII string returned in @buf.
28201cd7fabcSMatthew R. Ochs  */
port2_lun_table_show(struct device * dev,struct device_attribute * attr,char * buf)28211cd7fabcSMatthew R. Ochs static ssize_t port2_lun_table_show(struct device *dev,
28221cd7fabcSMatthew R. Ochs 				    struct device_attribute *attr,
28231cd7fabcSMatthew R. Ochs 				    char *buf)
28241cd7fabcSMatthew R. Ochs {
28251cd7fabcSMatthew R. Ochs 	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
28261cd7fabcSMatthew R. Ochs 
28271cd7fabcSMatthew R. Ochs 	return cxlflash_show_port_lun_table(2, cfg, buf);
28281cd7fabcSMatthew R. Ochs }
28291cd7fabcSMatthew R. Ochs 
28301cd7fabcSMatthew R. Ochs /**
28311cd7fabcSMatthew R. Ochs  * port3_lun_table_show() - presents the current LUN table of port 3
28321cd7fabcSMatthew R. Ochs  * @dev:	Generic device associated with the host owning the port.
28331cd7fabcSMatthew R. Ochs  * @attr:	Device attribute representing the port.
28341cd7fabcSMatthew R. Ochs  * @buf:	Buffer of length PAGE_SIZE to report back port status in ASCII.
28351cd7fabcSMatthew R. Ochs  *
28361cd7fabcSMatthew R. Ochs  * Return: The size of the ASCII string returned in @buf.
28371cd7fabcSMatthew R. Ochs  */
port3_lun_table_show(struct device * dev,struct device_attribute * attr,char * buf)28381cd7fabcSMatthew R. Ochs static ssize_t port3_lun_table_show(struct device *dev,
28391cd7fabcSMatthew R. Ochs 				    struct device_attribute *attr,
28401cd7fabcSMatthew R. Ochs 				    char *buf)
28411cd7fabcSMatthew R. Ochs {
28421cd7fabcSMatthew R. Ochs 	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
28431cd7fabcSMatthew R. Ochs 
28441cd7fabcSMatthew R. Ochs 	return cxlflash_show_port_lun_table(3, cfg, buf);
28451cd7fabcSMatthew R. Ochs }
28461cd7fabcSMatthew R. Ochs 
28471cd7fabcSMatthew R. Ochs /**
2848cba06e6dSMatthew R. Ochs  * irqpoll_weight_show() - presents the current IRQ poll weight for the host
2849cba06e6dSMatthew R. Ochs  * @dev:	Generic device associated with the host.
2850cba06e6dSMatthew R. Ochs  * @attr:	Device attribute representing the IRQ poll weight.
2851cba06e6dSMatthew R. Ochs  * @buf:	Buffer of length PAGE_SIZE to report back the current IRQ poll
2852cba06e6dSMatthew R. Ochs  *		weight in ASCII.
2853cba06e6dSMatthew R. Ochs  *
2854cba06e6dSMatthew R. Ochs  * An IRQ poll weight of 0 indicates polling is disabled.
2855cba06e6dSMatthew R. Ochs  *
2856cba06e6dSMatthew R. Ochs  * Return: The size of the ASCII string returned in @buf.
2857cba06e6dSMatthew R. Ochs  */
irqpoll_weight_show(struct device * dev,struct device_attribute * attr,char * buf)2858cba06e6dSMatthew R. Ochs static ssize_t irqpoll_weight_show(struct device *dev,
2859cba06e6dSMatthew R. Ochs 				   struct device_attribute *attr, char *buf)
2860cba06e6dSMatthew R. Ochs {
2861cba06e6dSMatthew R. Ochs 	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2862cba06e6dSMatthew R. Ochs 	struct afu *afu = cfg->afu;
2863cba06e6dSMatthew R. Ochs 
2864cba06e6dSMatthew R. Ochs 	return scnprintf(buf, PAGE_SIZE, "%u\n", afu->irqpoll_weight);
2865cba06e6dSMatthew R. Ochs }
2866cba06e6dSMatthew R. Ochs 
2867cba06e6dSMatthew R. Ochs /**
2868cba06e6dSMatthew R. Ochs  * irqpoll_weight_store() - sets the current IRQ poll weight for the host
2869cba06e6dSMatthew R. Ochs  * @dev:	Generic device associated with the host.
2870cba06e6dSMatthew R. Ochs  * @attr:	Device attribute representing the IRQ poll weight.
2871cba06e6dSMatthew R. Ochs  * @buf:	Buffer of length PAGE_SIZE containing the desired IRQ poll
2872cba06e6dSMatthew R. Ochs  *		weight in ASCII.
2873cba06e6dSMatthew R. Ochs  * @count:	Length of data resizing in @buf.
2874cba06e6dSMatthew R. Ochs  *
2875cba06e6dSMatthew R. Ochs  * An IRQ poll weight of 0 indicates polling is disabled.
2876cba06e6dSMatthew R. Ochs  *
2877cba06e6dSMatthew R. Ochs  * Return: The size of the ASCII string returned in @buf.
2878cba06e6dSMatthew R. Ochs  */
irqpoll_weight_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2879cba06e6dSMatthew R. Ochs static ssize_t irqpoll_weight_store(struct device *dev,
2880cba06e6dSMatthew R. Ochs 				    struct device_attribute *attr,
2881cba06e6dSMatthew R. Ochs 				    const char *buf, size_t count)
2882cba06e6dSMatthew R. Ochs {
2883cba06e6dSMatthew R. Ochs 	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
2884cba06e6dSMatthew R. Ochs 	struct device *cfgdev = &cfg->dev->dev;
2885cba06e6dSMatthew R. Ochs 	struct afu *afu = cfg->afu;
2886bfc0bab1SUma Krishnan 	struct hwq *hwq;
2887cba06e6dSMatthew R. Ochs 	u32 weight;
2888bfc0bab1SUma Krishnan 	int rc, i;
2889cba06e6dSMatthew R. Ochs 
2890cba06e6dSMatthew R. Ochs 	rc = kstrtouint(buf, 10, &weight);
2891cba06e6dSMatthew R. Ochs 	if (rc)
2892cba06e6dSMatthew R. Ochs 		return -EINVAL;
2893cba06e6dSMatthew R. Ochs 
2894cba06e6dSMatthew R. Ochs 	if (weight > 256) {
2895cba06e6dSMatthew R. Ochs 		dev_info(cfgdev,
2896cba06e6dSMatthew R. Ochs 			 "Invalid IRQ poll weight. It must be 256 or less.\n");
2897cba06e6dSMatthew R. Ochs 		return -EINVAL;
2898cba06e6dSMatthew R. Ochs 	}
2899cba06e6dSMatthew R. Ochs 
2900cba06e6dSMatthew R. Ochs 	if (weight == afu->irqpoll_weight) {
2901cba06e6dSMatthew R. Ochs 		dev_info(cfgdev,
2902cba06e6dSMatthew R. Ochs 			 "Current IRQ poll weight has the same weight.\n");
2903cba06e6dSMatthew R. Ochs 		return -EINVAL;
2904cba06e6dSMatthew R. Ochs 	}
2905cba06e6dSMatthew R. Ochs 
2906bfc0bab1SUma Krishnan 	if (afu_is_irqpoll_enabled(afu)) {
29073065267aSMatthew R. Ochs 		for (i = 0; i < afu->num_hwqs; i++) {
2908bfc0bab1SUma Krishnan 			hwq = get_hwq(afu, i);
2909bfc0bab1SUma Krishnan 
2910bfc0bab1SUma Krishnan 			irq_poll_disable(&hwq->irqpoll);
2911bfc0bab1SUma Krishnan 		}
2912bfc0bab1SUma Krishnan 	}
2913cba06e6dSMatthew R. Ochs 
2914cba06e6dSMatthew R. Ochs 	afu->irqpoll_weight = weight;
2915cba06e6dSMatthew R. Ochs 
2916bfc0bab1SUma Krishnan 	if (weight > 0) {
29173065267aSMatthew R. Ochs 		for (i = 0; i < afu->num_hwqs; i++) {
2918bfc0bab1SUma Krishnan 			hwq = get_hwq(afu, i);
2919bfc0bab1SUma Krishnan 
2920bfc0bab1SUma Krishnan 			irq_poll_init(&hwq->irqpoll, weight, cxlflash_irqpoll);
2921bfc0bab1SUma Krishnan 		}
2922bfc0bab1SUma Krishnan 	}
2923cba06e6dSMatthew R. Ochs 
2924cba06e6dSMatthew R. Ochs 	return count;
2925cba06e6dSMatthew R. Ochs }
2926cba06e6dSMatthew R. Ochs 
2927cba06e6dSMatthew R. Ochs /**
29283065267aSMatthew R. Ochs  * num_hwqs_show() - presents the number of hardware queues for the host
29293065267aSMatthew R. Ochs  * @dev:	Generic device associated with the host.
29303065267aSMatthew R. Ochs  * @attr:	Device attribute representing the number of hardware queues.
29313065267aSMatthew R. Ochs  * @buf:	Buffer of length PAGE_SIZE to report back the number of hardware
29323065267aSMatthew R. Ochs  *		queues in ASCII.
29333065267aSMatthew R. Ochs  *
29343065267aSMatthew R. Ochs  * Return: The size of the ASCII string returned in @buf.
29353065267aSMatthew R. Ochs  */
num_hwqs_show(struct device * dev,struct device_attribute * attr,char * buf)29363065267aSMatthew R. Ochs static ssize_t num_hwqs_show(struct device *dev,
29373065267aSMatthew R. Ochs 			     struct device_attribute *attr, char *buf)
29383065267aSMatthew R. Ochs {
29393065267aSMatthew R. Ochs 	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
29403065267aSMatthew R. Ochs 	struct afu *afu = cfg->afu;
29413065267aSMatthew R. Ochs 
29423065267aSMatthew R. Ochs 	return scnprintf(buf, PAGE_SIZE, "%u\n", afu->num_hwqs);
29433065267aSMatthew R. Ochs }
29443065267aSMatthew R. Ochs 
29453065267aSMatthew R. Ochs /**
29463065267aSMatthew R. Ochs  * num_hwqs_store() - sets the number of hardware queues for the host
29473065267aSMatthew R. Ochs  * @dev:	Generic device associated with the host.
29483065267aSMatthew R. Ochs  * @attr:	Device attribute representing the number of hardware queues.
29493065267aSMatthew R. Ochs  * @buf:	Buffer of length PAGE_SIZE containing the number of hardware
29503065267aSMatthew R. Ochs  *		queues in ASCII.
29513065267aSMatthew R. Ochs  * @count:	Length of data resizing in @buf.
29523065267aSMatthew R. Ochs  *
29533065267aSMatthew R. Ochs  * n > 0: num_hwqs = n
29543065267aSMatthew R. Ochs  * n = 0: num_hwqs = num_online_cpus()
29553065267aSMatthew R. Ochs  * n < 0: num_online_cpus() / abs(n)
29563065267aSMatthew R. Ochs  *
29573065267aSMatthew R. Ochs  * Return: The size of the ASCII string returned in @buf.
29583065267aSMatthew R. Ochs  */
num_hwqs_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)29593065267aSMatthew R. Ochs static ssize_t num_hwqs_store(struct device *dev,
29603065267aSMatthew R. Ochs 			      struct device_attribute *attr,
29613065267aSMatthew R. Ochs 			      const char *buf, size_t count)
29623065267aSMatthew R. Ochs {
29633065267aSMatthew R. Ochs 	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
29643065267aSMatthew R. Ochs 	struct afu *afu = cfg->afu;
29653065267aSMatthew R. Ochs 	int rc;
29663065267aSMatthew R. Ochs 	int nhwqs, num_hwqs;
29673065267aSMatthew R. Ochs 
29683065267aSMatthew R. Ochs 	rc = kstrtoint(buf, 10, &nhwqs);
29693065267aSMatthew R. Ochs 	if (rc)
29703065267aSMatthew R. Ochs 		return -EINVAL;
29713065267aSMatthew R. Ochs 
29723065267aSMatthew R. Ochs 	if (nhwqs >= 1)
29733065267aSMatthew R. Ochs 		num_hwqs = nhwqs;
29743065267aSMatthew R. Ochs 	else if (nhwqs == 0)
29753065267aSMatthew R. Ochs 		num_hwqs = num_online_cpus();
29763065267aSMatthew R. Ochs 	else
29773065267aSMatthew R. Ochs 		num_hwqs = num_online_cpus() / abs(nhwqs);
29783065267aSMatthew R. Ochs 
29793065267aSMatthew R. Ochs 	afu->desired_hwqs = min(num_hwqs, CXLFLASH_MAX_HWQS);
29803065267aSMatthew R. Ochs 	WARN_ON_ONCE(afu->desired_hwqs == 0);
29813065267aSMatthew R. Ochs 
29823065267aSMatthew R. Ochs retry:
29833065267aSMatthew R. Ochs 	switch (cfg->state) {
29843065267aSMatthew R. Ochs 	case STATE_NORMAL:
29853065267aSMatthew R. Ochs 		cfg->state = STATE_RESET;
29863065267aSMatthew R. Ochs 		drain_ioctls(cfg);
29873065267aSMatthew R. Ochs 		cxlflash_mark_contexts_error(cfg);
29883065267aSMatthew R. Ochs 		rc = afu_reset(cfg);
29893065267aSMatthew R. Ochs 		if (rc)
29903065267aSMatthew R. Ochs 			cfg->state = STATE_FAILTERM;
29913065267aSMatthew R. Ochs 		else
29923065267aSMatthew R. Ochs 			cfg->state = STATE_NORMAL;
29933065267aSMatthew R. Ochs 		wake_up_all(&cfg->reset_waitq);
29943065267aSMatthew R. Ochs 		break;
29953065267aSMatthew R. Ochs 	case STATE_RESET:
29963065267aSMatthew R. Ochs 		wait_event(cfg->reset_waitq, cfg->state != STATE_RESET);
29973065267aSMatthew R. Ochs 		if (cfg->state == STATE_NORMAL)
29983065267aSMatthew R. Ochs 			goto retry;
2999df561f66SGustavo A. R. Silva 		fallthrough;
30003065267aSMatthew R. Ochs 	default:
30013065267aSMatthew R. Ochs 		/* Ideally should not happen */
30023065267aSMatthew R. Ochs 		dev_err(dev, "%s: Device is not ready, state=%d\n",
30033065267aSMatthew R. Ochs 			__func__, cfg->state);
30043065267aSMatthew R. Ochs 		break;
30053065267aSMatthew R. Ochs 	}
30063065267aSMatthew R. Ochs 
30073065267aSMatthew R. Ochs 	return count;
30083065267aSMatthew R. Ochs }
30093065267aSMatthew R. Ochs 
30101dd0c0e4SMatthew R. Ochs static const char *hwq_mode_name[MAX_HWQ_MODE] = { "rr", "tag", "cpu" };
30111dd0c0e4SMatthew R. Ochs 
30121dd0c0e4SMatthew R. Ochs /**
30131dd0c0e4SMatthew R. Ochs  * hwq_mode_show() - presents the HWQ steering mode for the host
30141dd0c0e4SMatthew R. Ochs  * @dev:	Generic device associated with the host.
30151dd0c0e4SMatthew R. Ochs  * @attr:	Device attribute representing the HWQ steering mode.
30161dd0c0e4SMatthew R. Ochs  * @buf:	Buffer of length PAGE_SIZE to report back the HWQ steering mode
30171dd0c0e4SMatthew R. Ochs  *		as a character string.
30181dd0c0e4SMatthew R. Ochs  *
30191dd0c0e4SMatthew R. Ochs  * Return: The size of the ASCII string returned in @buf.
30201dd0c0e4SMatthew R. Ochs  */
hwq_mode_show(struct device * dev,struct device_attribute * attr,char * buf)30211dd0c0e4SMatthew R. Ochs static ssize_t hwq_mode_show(struct device *dev,
30221dd0c0e4SMatthew R. Ochs 			     struct device_attribute *attr, char *buf)
30231dd0c0e4SMatthew R. Ochs {
30241dd0c0e4SMatthew R. Ochs 	struct cxlflash_cfg *cfg = shost_priv(class_to_shost(dev));
30251dd0c0e4SMatthew R. Ochs 	struct afu *afu = cfg->afu;
30261dd0c0e4SMatthew R. Ochs 
30271dd0c0e4SMatthew R. Ochs 	return scnprintf(buf, PAGE_SIZE, "%s\n", hwq_mode_name[afu->hwq_mode]);
30281dd0c0e4SMatthew R. Ochs }
30291dd0c0e4SMatthew R. Ochs 
30301dd0c0e4SMatthew R. Ochs /**
30311dd0c0e4SMatthew R. Ochs  * hwq_mode_store() - sets the HWQ steering mode for the host
30321dd0c0e4SMatthew R. Ochs  * @dev:	Generic device associated with the host.
30331dd0c0e4SMatthew R. Ochs  * @attr:	Device attribute representing the HWQ steering mode.
30341dd0c0e4SMatthew R. Ochs  * @buf:	Buffer of length PAGE_SIZE containing the HWQ steering mode
30351dd0c0e4SMatthew R. Ochs  *		as a character string.
30361dd0c0e4SMatthew R. Ochs  * @count:	Length of data resizing in @buf.
30371dd0c0e4SMatthew R. Ochs  *
30381dd0c0e4SMatthew R. Ochs  * rr = Round-Robin
30391dd0c0e4SMatthew R. Ochs  * tag = Block MQ Tagging
30401dd0c0e4SMatthew R. Ochs  * cpu = CPU Affinity
30411dd0c0e4SMatthew R. Ochs  *
30421dd0c0e4SMatthew R. Ochs  * Return: The size of the ASCII string returned in @buf.
30431dd0c0e4SMatthew R. Ochs  */
hwq_mode_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)30441dd0c0e4SMatthew R. Ochs static ssize_t hwq_mode_store(struct device *dev,
30451dd0c0e4SMatthew R. Ochs 			      struct device_attribute *attr,
30461dd0c0e4SMatthew R. Ochs 			      const char *buf, size_t count)
30471dd0c0e4SMatthew R. Ochs {
30481dd0c0e4SMatthew R. Ochs 	struct Scsi_Host *shost = class_to_shost(dev);
30491dd0c0e4SMatthew R. Ochs 	struct cxlflash_cfg *cfg = shost_priv(shost);
30501dd0c0e4SMatthew R. Ochs 	struct device *cfgdev = &cfg->dev->dev;
30511dd0c0e4SMatthew R. Ochs 	struct afu *afu = cfg->afu;
30521dd0c0e4SMatthew R. Ochs 	int i;
30531dd0c0e4SMatthew R. Ochs 	u32 mode = MAX_HWQ_MODE;
30541dd0c0e4SMatthew R. Ochs 
30551dd0c0e4SMatthew R. Ochs 	for (i = 0; i < MAX_HWQ_MODE; i++) {
30561dd0c0e4SMatthew R. Ochs 		if (!strncmp(hwq_mode_name[i], buf, strlen(hwq_mode_name[i]))) {
30571dd0c0e4SMatthew R. Ochs 			mode = i;
30581dd0c0e4SMatthew R. Ochs 			break;
30591dd0c0e4SMatthew R. Ochs 		}
30601dd0c0e4SMatthew R. Ochs 	}
30611dd0c0e4SMatthew R. Ochs 
30621dd0c0e4SMatthew R. Ochs 	if (mode >= MAX_HWQ_MODE) {
30631dd0c0e4SMatthew R. Ochs 		dev_info(cfgdev, "Invalid HWQ steering mode.\n");
30641dd0c0e4SMatthew R. Ochs 		return -EINVAL;
30651dd0c0e4SMatthew R. Ochs 	}
30661dd0c0e4SMatthew R. Ochs 
30671dd0c0e4SMatthew R. Ochs 	afu->hwq_mode = mode;
30681dd0c0e4SMatthew R. Ochs 
30691dd0c0e4SMatthew R. Ochs 	return count;
30701dd0c0e4SMatthew R. Ochs }
30711dd0c0e4SMatthew R. Ochs 
30723065267aSMatthew R. Ochs /**
3073e0f01a21SMatthew R. Ochs  * mode_show() - presents the current mode of the device
307415305514SMatthew R. Ochs  * @dev:	Generic device associated with the device.
307515305514SMatthew R. Ochs  * @attr:	Device attribute representing the device mode.
307615305514SMatthew R. Ochs  * @buf:	Buffer of length PAGE_SIZE to report back the dev mode in ASCII.
307715305514SMatthew R. Ochs  *
307815305514SMatthew R. Ochs  * Return: The size of the ASCII string returned in @buf.
307915305514SMatthew R. Ochs  */
mode_show(struct device * dev,struct device_attribute * attr,char * buf)3080e0f01a21SMatthew R. Ochs static ssize_t mode_show(struct device *dev,
308115305514SMatthew R. Ochs 			 struct device_attribute *attr, char *buf)
308215305514SMatthew R. Ochs {
308315305514SMatthew R. Ochs 	struct scsi_device *sdev = to_scsi_device(dev);
308415305514SMatthew R. Ochs 
3085e0f01a21SMatthew R. Ochs 	return scnprintf(buf, PAGE_SIZE, "%s\n",
308615305514SMatthew R. Ochs 			 sdev->hostdata ? "superpipe" : "legacy");
308715305514SMatthew R. Ochs }
308815305514SMatthew R. Ochs 
308915305514SMatthew R. Ochs /*
309015305514SMatthew R. Ochs  * Host attributes
309115305514SMatthew R. Ochs  */
3092e0f01a21SMatthew R. Ochs static DEVICE_ATTR_RO(port0);
3093e0f01a21SMatthew R. Ochs static DEVICE_ATTR_RO(port1);
30941cd7fabcSMatthew R. Ochs static DEVICE_ATTR_RO(port2);
30951cd7fabcSMatthew R. Ochs static DEVICE_ATTR_RO(port3);
3096e0f01a21SMatthew R. Ochs static DEVICE_ATTR_RW(lun_mode);
3097e0f01a21SMatthew R. Ochs static DEVICE_ATTR_RO(ioctl_version);
3098e0f01a21SMatthew R. Ochs static DEVICE_ATTR_RO(port0_lun_table);
3099e0f01a21SMatthew R. Ochs static DEVICE_ATTR_RO(port1_lun_table);
31001cd7fabcSMatthew R. Ochs static DEVICE_ATTR_RO(port2_lun_table);
31011cd7fabcSMatthew R. Ochs static DEVICE_ATTR_RO(port3_lun_table);
3102cba06e6dSMatthew R. Ochs static DEVICE_ATTR_RW(irqpoll_weight);
31033065267aSMatthew R. Ochs static DEVICE_ATTR_RW(num_hwqs);
31041dd0c0e4SMatthew R. Ochs static DEVICE_ATTR_RW(hwq_mode);
310515305514SMatthew R. Ochs 
3106780c6789SBart Van Assche static struct attribute *cxlflash_host_attrs[] = {
3107780c6789SBart Van Assche 	&dev_attr_port0.attr,
3108780c6789SBart Van Assche 	&dev_attr_port1.attr,
3109780c6789SBart Van Assche 	&dev_attr_port2.attr,
3110780c6789SBart Van Assche 	&dev_attr_port3.attr,
3111780c6789SBart Van Assche 	&dev_attr_lun_mode.attr,
3112780c6789SBart Van Assche 	&dev_attr_ioctl_version.attr,
3113780c6789SBart Van Assche 	&dev_attr_port0_lun_table.attr,
3114780c6789SBart Van Assche 	&dev_attr_port1_lun_table.attr,
3115780c6789SBart Van Assche 	&dev_attr_port2_lun_table.attr,
3116780c6789SBart Van Assche 	&dev_attr_port3_lun_table.attr,
3117780c6789SBart Van Assche 	&dev_attr_irqpoll_weight.attr,
3118780c6789SBart Van Assche 	&dev_attr_num_hwqs.attr,
3119780c6789SBart Van Assche 	&dev_attr_hwq_mode.attr,
312015305514SMatthew R. Ochs 	NULL
312115305514SMatthew R. Ochs };
312215305514SMatthew R. Ochs 
3123780c6789SBart Van Assche ATTRIBUTE_GROUPS(cxlflash_host);
3124780c6789SBart Van Assche 
312515305514SMatthew R. Ochs /*
312615305514SMatthew R. Ochs  * Device attributes
312715305514SMatthew R. Ochs  */
3128e0f01a21SMatthew R. Ochs static DEVICE_ATTR_RO(mode);
312915305514SMatthew R. Ochs 
3130780c6789SBart Van Assche static struct attribute *cxlflash_dev_attrs[] = {
3131780c6789SBart Van Assche 	&dev_attr_mode.attr,
313215305514SMatthew R. Ochs 	NULL
313315305514SMatthew R. Ochs };
313415305514SMatthew R. Ochs 
3135780c6789SBart Van Assche ATTRIBUTE_GROUPS(cxlflash_dev);
3136780c6789SBart Van Assche 
313715305514SMatthew R. Ochs /*
313815305514SMatthew R. Ochs  * Host template
313915305514SMatthew R. Ochs  */
314015305514SMatthew R. Ochs static struct scsi_host_template driver_template = {
314115305514SMatthew R. Ochs 	.module = THIS_MODULE,
314215305514SMatthew R. Ochs 	.name = CXLFLASH_ADAPTER_NAME,
314315305514SMatthew R. Ochs 	.info = cxlflash_driver_info,
314415305514SMatthew R. Ochs 	.ioctl = cxlflash_ioctl,
314515305514SMatthew R. Ochs 	.proc_name = CXLFLASH_NAME,
314615305514SMatthew R. Ochs 	.queuecommand = cxlflash_queuecommand,
31477c4c41f1SUma Krishnan 	.eh_abort_handler = cxlflash_eh_abort_handler,
314815305514SMatthew R. Ochs 	.eh_device_reset_handler = cxlflash_eh_device_reset_handler,
314915305514SMatthew R. Ochs 	.eh_host_reset_handler = cxlflash_eh_host_reset_handler,
315015305514SMatthew R. Ochs 	.change_queue_depth = cxlflash_change_queue_depth,
315183430833SManoj N. Kumar 	.cmd_per_lun = CXLFLASH_MAX_CMDS_PER_LUN,
315215305514SMatthew R. Ochs 	.can_queue = CXLFLASH_MAX_CMDS,
31535fbb96c8SMatthew R. Ochs 	.cmd_size = sizeof(struct afu_cmd) + __alignof__(struct afu_cmd) - 1,
315415305514SMatthew R. Ochs 	.this_id = -1,
315568ab2d76SUma Krishnan 	.sg_tablesize = 1,	/* No scatter gather support */
315615305514SMatthew R. Ochs 	.max_sectors = CXLFLASH_MAX_SECTORS,
3157780c6789SBart Van Assche 	.shost_groups = cxlflash_host_groups,
3158780c6789SBart Van Assche 	.sdev_groups = cxlflash_dev_groups,
315915305514SMatthew R. Ochs };
316015305514SMatthew R. Ochs 
316115305514SMatthew R. Ochs /*
316215305514SMatthew R. Ochs  * Device dependent values
316315305514SMatthew R. Ochs  */
316496e1b660SUma Krishnan static struct dev_dependent_vals dev_corsa_vals = { CXLFLASH_MAX_SECTORS,
31650d419130SMatthew R. Ochs 					CXLFLASH_WWPN_VPD_REQUIRED };
316696e1b660SUma Krishnan static struct dev_dependent_vals dev_flash_gt_vals = { CXLFLASH_MAX_SECTORS,
3167704c4b0dSUma Krishnan 					CXLFLASH_NOTIFY_SHUTDOWN };
316894344520SMatthew R. Ochs static struct dev_dependent_vals dev_briard_vals = { CXLFLASH_MAX_SECTORS,
316907d0c52fSUma Krishnan 					(CXLFLASH_NOTIFY_SHUTDOWN |
317007d0c52fSUma Krishnan 					CXLFLASH_OCXL_DEV) };
317115305514SMatthew R. Ochs 
317215305514SMatthew R. Ochs /*
317315305514SMatthew R. Ochs  * PCI device binding table
317415305514SMatthew R. Ochs  */
317515305514SMatthew R. Ochs static struct pci_device_id cxlflash_pci_table[] = {
317615305514SMatthew R. Ochs 	{PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CORSA,
317715305514SMatthew R. Ochs 	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_corsa_vals},
3178a2746fb1SManoj Kumar 	{PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_FLASH_GT,
3179a2746fb1SManoj Kumar 	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_flash_gt_vals},
318094344520SMatthew R. Ochs 	{PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_BRIARD,
318194344520SMatthew R. Ochs 	 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (kernel_ulong_t)&dev_briard_vals},
318215305514SMatthew R. Ochs 	{}
318315305514SMatthew R. Ochs };
318415305514SMatthew R. Ochs 
318515305514SMatthew R. Ochs MODULE_DEVICE_TABLE(pci, cxlflash_pci_table);
318615305514SMatthew R. Ochs 
318715305514SMatthew R. Ochs /**
3188c21e0bbfSMatthew R. Ochs  * cxlflash_worker_thread() - work thread handler for the AFU
3189c21e0bbfSMatthew R. Ochs  * @work:	Work structure contained within cxlflash associated with host.
3190c21e0bbfSMatthew R. Ochs  *
3191c21e0bbfSMatthew R. Ochs  * Handles the following events:
3192c21e0bbfSMatthew R. Ochs  * - Link reset which cannot be performed on interrupt context due to
3193c21e0bbfSMatthew R. Ochs  * blocking up to a few seconds
3194ef51074aSMatthew R. Ochs  * - Rescan the host
3195c21e0bbfSMatthew R. Ochs  */
cxlflash_worker_thread(struct work_struct * work)3196c21e0bbfSMatthew R. Ochs static void cxlflash_worker_thread(struct work_struct *work)
3197c21e0bbfSMatthew R. Ochs {
31985cdac81aSMatthew R. Ochs 	struct cxlflash_cfg *cfg = container_of(work, struct cxlflash_cfg,
31995cdac81aSMatthew R. Ochs 						work_q);
3200c21e0bbfSMatthew R. Ochs 	struct afu *afu = cfg->afu;
32014392ba49SMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
32020aa14887SMatthew R. Ochs 	__be64 __iomem *fc_port_regs;
3203c21e0bbfSMatthew R. Ochs 	int port;
3204c21e0bbfSMatthew R. Ochs 	ulong lock_flags;
3205c21e0bbfSMatthew R. Ochs 
32065cdac81aSMatthew R. Ochs 	/* Avoid MMIO if the device has failed */
32075cdac81aSMatthew R. Ochs 
32085cdac81aSMatthew R. Ochs 	if (cfg->state != STATE_NORMAL)
32095cdac81aSMatthew R. Ochs 		return;
32105cdac81aSMatthew R. Ochs 
3211c21e0bbfSMatthew R. Ochs 	spin_lock_irqsave(cfg->host->host_lock, lock_flags);
3212c21e0bbfSMatthew R. Ochs 
3213c21e0bbfSMatthew R. Ochs 	if (cfg->lr_state == LINK_RESET_REQUIRED) {
3214c21e0bbfSMatthew R. Ochs 		port = cfg->lr_port;
3215c21e0bbfSMatthew R. Ochs 		if (port < 0)
32164392ba49SMatthew R. Ochs 			dev_err(dev, "%s: invalid port index %d\n",
32174392ba49SMatthew R. Ochs 				__func__, port);
3218c21e0bbfSMatthew R. Ochs 		else {
3219c21e0bbfSMatthew R. Ochs 			spin_unlock_irqrestore(cfg->host->host_lock,
3220c21e0bbfSMatthew R. Ochs 					       lock_flags);
3221c21e0bbfSMatthew R. Ochs 
3222c21e0bbfSMatthew R. Ochs 			/* The reset can block... */
32230aa14887SMatthew R. Ochs 			fc_port_regs = get_fc_port_regs(cfg, port);
32240aa14887SMatthew R. Ochs 			afu_link_reset(afu, port, fc_port_regs);
3225c21e0bbfSMatthew R. Ochs 			spin_lock_irqsave(cfg->host->host_lock, lock_flags);
3226c21e0bbfSMatthew R. Ochs 		}
3227c21e0bbfSMatthew R. Ochs 
3228c21e0bbfSMatthew R. Ochs 		cfg->lr_state = LINK_RESET_COMPLETE;
3229c21e0bbfSMatthew R. Ochs 	}
3230c21e0bbfSMatthew R. Ochs 
3231c21e0bbfSMatthew R. Ochs 	spin_unlock_irqrestore(cfg->host->host_lock, lock_flags);
3232ef51074aSMatthew R. Ochs 
3233ef51074aSMatthew R. Ochs 	if (atomic_dec_if_positive(&cfg->scan_host_needed) >= 0)
3234ef51074aSMatthew R. Ochs 		scsi_scan_host(cfg->host);
3235c21e0bbfSMatthew R. Ochs }
3236c21e0bbfSMatthew R. Ochs 
3237c21e0bbfSMatthew R. Ochs /**
3238a834a36bSUma Krishnan  * cxlflash_chr_open() - character device open handler
3239a834a36bSUma Krishnan  * @inode:	Device inode associated with this character device.
3240a834a36bSUma Krishnan  * @file:	File pointer for this device.
3241a834a36bSUma Krishnan  *
3242a834a36bSUma Krishnan  * Only users with admin privileges are allowed to open the character device.
3243a834a36bSUma Krishnan  *
3244a834a36bSUma Krishnan  * Return: 0 on success, -errno on failure
3245a834a36bSUma Krishnan  */
cxlflash_chr_open(struct inode * inode,struct file * file)3246a834a36bSUma Krishnan static int cxlflash_chr_open(struct inode *inode, struct file *file)
3247a834a36bSUma Krishnan {
3248a834a36bSUma Krishnan 	struct cxlflash_cfg *cfg;
3249a834a36bSUma Krishnan 
3250a834a36bSUma Krishnan 	if (!capable(CAP_SYS_ADMIN))
3251a834a36bSUma Krishnan 		return -EACCES;
3252a834a36bSUma Krishnan 
3253a834a36bSUma Krishnan 	cfg = container_of(inode->i_cdev, struct cxlflash_cfg, cdev);
3254a834a36bSUma Krishnan 	file->private_data = cfg;
3255a834a36bSUma Krishnan 
3256a834a36bSUma Krishnan 	return 0;
3257a834a36bSUma Krishnan }
3258a834a36bSUma Krishnan 
3259d6e32f53SMatthew R. Ochs /**
3260d6e32f53SMatthew R. Ochs  * decode_hioctl() - translates encoded host ioctl to easily identifiable string
3261d6e32f53SMatthew R. Ochs  * @cmd:        The host ioctl command to decode.
3262d6e32f53SMatthew R. Ochs  *
3263d6e32f53SMatthew R. Ochs  * Return: A string identifying the decoded host ioctl.
3264d6e32f53SMatthew R. Ochs  */
decode_hioctl(unsigned int cmd)32656f4e626fSNathan Chancellor static char *decode_hioctl(unsigned int cmd)
3266d6e32f53SMatthew R. Ochs {
3267d6e32f53SMatthew R. Ochs 	switch (cmd) {
32689cf43a36SMatthew R. Ochs 	case HT_CXLFLASH_LUN_PROVISION:
32699cf43a36SMatthew R. Ochs 		return __stringify_1(HT_CXLFLASH_LUN_PROVISION);
3270d6e32f53SMatthew R. Ochs 	}
3271d6e32f53SMatthew R. Ochs 
3272d6e32f53SMatthew R. Ochs 	return "UNKNOWN";
3273d6e32f53SMatthew R. Ochs }
3274d6e32f53SMatthew R. Ochs 
3275d6e32f53SMatthew R. Ochs /**
32769cf43a36SMatthew R. Ochs  * cxlflash_lun_provision() - host LUN provisioning handler
32779cf43a36SMatthew R. Ochs  * @cfg:	Internal structure associated with the host.
3278cf0ad7a1SLee Jones  * @lunprov:	Kernel copy of userspace ioctl data structure.
32799cf43a36SMatthew R. Ochs  *
32809cf43a36SMatthew R. Ochs  * Return: 0 on success, -errno on failure
32819cf43a36SMatthew R. Ochs  */
cxlflash_lun_provision(struct cxlflash_cfg * cfg,struct ht_cxlflash_lun_provision * lunprov)32829cf43a36SMatthew R. Ochs static int cxlflash_lun_provision(struct cxlflash_cfg *cfg,
32839cf43a36SMatthew R. Ochs 				  struct ht_cxlflash_lun_provision *lunprov)
32849cf43a36SMatthew R. Ochs {
32859cf43a36SMatthew R. Ochs 	struct afu *afu = cfg->afu;
32869cf43a36SMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
32879cf43a36SMatthew R. Ochs 	struct sisl_ioarcb rcb;
32889cf43a36SMatthew R. Ochs 	struct sisl_ioasa asa;
32899cf43a36SMatthew R. Ochs 	__be64 __iomem *fc_port_regs;
32909cf43a36SMatthew R. Ochs 	u16 port = lunprov->port;
32919cf43a36SMatthew R. Ochs 	u16 scmd = lunprov->hdr.subcmd;
32929cf43a36SMatthew R. Ochs 	u16 type;
32939cf43a36SMatthew R. Ochs 	u64 reg;
32949cf43a36SMatthew R. Ochs 	u64 size;
32959cf43a36SMatthew R. Ochs 	u64 lun_id;
32969cf43a36SMatthew R. Ochs 	int rc = 0;
32979cf43a36SMatthew R. Ochs 
32989cf43a36SMatthew R. Ochs 	if (!afu_is_lun_provision(afu)) {
32999cf43a36SMatthew R. Ochs 		rc = -ENOTSUPP;
33009cf43a36SMatthew R. Ochs 		goto out;
33019cf43a36SMatthew R. Ochs 	}
33029cf43a36SMatthew R. Ochs 
33039cf43a36SMatthew R. Ochs 	if (port >= cfg->num_fc_ports) {
33049cf43a36SMatthew R. Ochs 		rc = -EINVAL;
33059cf43a36SMatthew R. Ochs 		goto out;
33069cf43a36SMatthew R. Ochs 	}
33079cf43a36SMatthew R. Ochs 
33089cf43a36SMatthew R. Ochs 	switch (scmd) {
33099cf43a36SMatthew R. Ochs 	case HT_CXLFLASH_LUN_PROVISION_SUBCMD_CREATE_LUN:
33109cf43a36SMatthew R. Ochs 		type = SISL_AFU_LUN_PROVISION_CREATE;
33119cf43a36SMatthew R. Ochs 		size = lunprov->size;
33129cf43a36SMatthew R. Ochs 		lun_id = 0;
33139cf43a36SMatthew R. Ochs 		break;
33149cf43a36SMatthew R. Ochs 	case HT_CXLFLASH_LUN_PROVISION_SUBCMD_DELETE_LUN:
33159cf43a36SMatthew R. Ochs 		type = SISL_AFU_LUN_PROVISION_DELETE;
33169cf43a36SMatthew R. Ochs 		size = 0;
33179cf43a36SMatthew R. Ochs 		lun_id = lunprov->lun_id;
33189cf43a36SMatthew R. Ochs 		break;
33199cf43a36SMatthew R. Ochs 	case HT_CXLFLASH_LUN_PROVISION_SUBCMD_QUERY_PORT:
33209cf43a36SMatthew R. Ochs 		fc_port_regs = get_fc_port_regs(cfg, port);
33219cf43a36SMatthew R. Ochs 
33229cf43a36SMatthew R. Ochs 		reg = readq_be(&fc_port_regs[FC_MAX_NUM_LUNS / 8]);
33239cf43a36SMatthew R. Ochs 		lunprov->max_num_luns = reg;
33249cf43a36SMatthew R. Ochs 		reg = readq_be(&fc_port_regs[FC_CUR_NUM_LUNS / 8]);
33259cf43a36SMatthew R. Ochs 		lunprov->cur_num_luns = reg;
33269cf43a36SMatthew R. Ochs 		reg = readq_be(&fc_port_regs[FC_MAX_CAP_PORT / 8]);
33279cf43a36SMatthew R. Ochs 		lunprov->max_cap_port = reg;
33289cf43a36SMatthew R. Ochs 		reg = readq_be(&fc_port_regs[FC_CUR_CAP_PORT / 8]);
33299cf43a36SMatthew R. Ochs 		lunprov->cur_cap_port = reg;
33309cf43a36SMatthew R. Ochs 
33319cf43a36SMatthew R. Ochs 		goto out;
33329cf43a36SMatthew R. Ochs 	default:
33339cf43a36SMatthew R. Ochs 		rc = -EINVAL;
33349cf43a36SMatthew R. Ochs 		goto out;
33359cf43a36SMatthew R. Ochs 	}
33369cf43a36SMatthew R. Ochs 
33379cf43a36SMatthew R. Ochs 	memset(&rcb, 0, sizeof(rcb));
33389cf43a36SMatthew R. Ochs 	memset(&asa, 0, sizeof(asa));
33399cf43a36SMatthew R. Ochs 	rcb.req_flags = SISL_REQ_FLAGS_AFU_CMD;
33409cf43a36SMatthew R. Ochs 	rcb.lun_id = lun_id;
33419cf43a36SMatthew R. Ochs 	rcb.msi = SISL_MSI_RRQ_UPDATED;
33429cf43a36SMatthew R. Ochs 	rcb.timeout = MC_LUN_PROV_TIMEOUT;
33439cf43a36SMatthew R. Ochs 	rcb.ioasa = &asa;
33449cf43a36SMatthew R. Ochs 
33459cf43a36SMatthew R. Ochs 	rcb.cdb[0] = SISL_AFU_CMD_LUN_PROVISION;
33469cf43a36SMatthew R. Ochs 	rcb.cdb[1] = type;
33479cf43a36SMatthew R. Ochs 	rcb.cdb[2] = port;
33489cf43a36SMatthew R. Ochs 	put_unaligned_be64(size, &rcb.cdb[8]);
33499cf43a36SMatthew R. Ochs 
33509cf43a36SMatthew R. Ochs 	rc = send_afu_cmd(afu, &rcb);
33519cf43a36SMatthew R. Ochs 	if (rc) {
33529cf43a36SMatthew R. Ochs 		dev_err(dev, "%s: send_afu_cmd failed rc=%d asc=%08x afux=%x\n",
33539cf43a36SMatthew R. Ochs 			__func__, rc, asa.ioasc, asa.afu_extra);
33549cf43a36SMatthew R. Ochs 		goto out;
33559cf43a36SMatthew R. Ochs 	}
33569cf43a36SMatthew R. Ochs 
33579cf43a36SMatthew R. Ochs 	if (scmd == HT_CXLFLASH_LUN_PROVISION_SUBCMD_CREATE_LUN) {
33589cf43a36SMatthew R. Ochs 		lunprov->lun_id = (u64)asa.lunid_hi << 32 | asa.lunid_lo;
33599cf43a36SMatthew R. Ochs 		memcpy(lunprov->wwid, asa.wwid, sizeof(lunprov->wwid));
33609cf43a36SMatthew R. Ochs 	}
33619cf43a36SMatthew R. Ochs out:
33629cf43a36SMatthew R. Ochs 	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
33639cf43a36SMatthew R. Ochs 	return rc;
33649cf43a36SMatthew R. Ochs }
33659cf43a36SMatthew R. Ochs 
33669cf43a36SMatthew R. Ochs /**
3367bc88ac47SMatthew R. Ochs  * cxlflash_afu_debug() - host AFU debug handler
3368bc88ac47SMatthew R. Ochs  * @cfg:	Internal structure associated with the host.
3369cf0ad7a1SLee Jones  * @afu_dbg:	Kernel copy of userspace ioctl data structure.
3370bc88ac47SMatthew R. Ochs  *
3371bc88ac47SMatthew R. Ochs  * For debug requests requiring a data buffer, always provide an aligned
3372bc88ac47SMatthew R. Ochs  * (cache line) buffer to the AFU to appease any alignment requirements.
3373bc88ac47SMatthew R. Ochs  *
3374bc88ac47SMatthew R. Ochs  * Return: 0 on success, -errno on failure
3375bc88ac47SMatthew R. Ochs  */
cxlflash_afu_debug(struct cxlflash_cfg * cfg,struct ht_cxlflash_afu_debug * afu_dbg)3376bc88ac47SMatthew R. Ochs static int cxlflash_afu_debug(struct cxlflash_cfg *cfg,
3377bc88ac47SMatthew R. Ochs 			      struct ht_cxlflash_afu_debug *afu_dbg)
3378bc88ac47SMatthew R. Ochs {
3379bc88ac47SMatthew R. Ochs 	struct afu *afu = cfg->afu;
3380bc88ac47SMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
3381bc88ac47SMatthew R. Ochs 	struct sisl_ioarcb rcb;
3382bc88ac47SMatthew R. Ochs 	struct sisl_ioasa asa;
3383bc88ac47SMatthew R. Ochs 	char *buf = NULL;
3384bc88ac47SMatthew R. Ochs 	char *kbuf = NULL;
3385bc88ac47SMatthew R. Ochs 	void __user *ubuf = (__force void __user *)afu_dbg->data_ea;
3386bc88ac47SMatthew R. Ochs 	u16 req_flags = SISL_REQ_FLAGS_AFU_CMD;
3387bc88ac47SMatthew R. Ochs 	u32 ulen = afu_dbg->data_len;
3388bc88ac47SMatthew R. Ochs 	bool is_write = afu_dbg->hdr.flags & HT_CXLFLASH_HOST_WRITE;
3389bc88ac47SMatthew R. Ochs 	int rc = 0;
3390bc88ac47SMatthew R. Ochs 
3391bc88ac47SMatthew R. Ochs 	if (!afu_is_afu_debug(afu)) {
3392bc88ac47SMatthew R. Ochs 		rc = -ENOTSUPP;
3393bc88ac47SMatthew R. Ochs 		goto out;
3394bc88ac47SMatthew R. Ochs 	}
3395bc88ac47SMatthew R. Ochs 
3396bc88ac47SMatthew R. Ochs 	if (ulen) {
3397bc88ac47SMatthew R. Ochs 		req_flags |= SISL_REQ_FLAGS_SUP_UNDERRUN;
3398bc88ac47SMatthew R. Ochs 
3399bc88ac47SMatthew R. Ochs 		if (ulen > HT_CXLFLASH_AFU_DEBUG_MAX_DATA_LEN) {
3400bc88ac47SMatthew R. Ochs 			rc = -EINVAL;
3401bc88ac47SMatthew R. Ochs 			goto out;
3402bc88ac47SMatthew R. Ochs 		}
3403bc88ac47SMatthew R. Ochs 
3404bc88ac47SMatthew R. Ochs 		buf = kmalloc(ulen + cache_line_size() - 1, GFP_KERNEL);
3405bc88ac47SMatthew R. Ochs 		if (unlikely(!buf)) {
3406bc88ac47SMatthew R. Ochs 			rc = -ENOMEM;
3407bc88ac47SMatthew R. Ochs 			goto out;
3408bc88ac47SMatthew R. Ochs 		}
3409bc88ac47SMatthew R. Ochs 
3410bc88ac47SMatthew R. Ochs 		kbuf = PTR_ALIGN(buf, cache_line_size());
3411bc88ac47SMatthew R. Ochs 
3412bc88ac47SMatthew R. Ochs 		if (is_write) {
3413bc88ac47SMatthew R. Ochs 			req_flags |= SISL_REQ_FLAGS_HOST_WRITE;
3414bc88ac47SMatthew R. Ochs 
3415eeac8cdaSDan Carpenter 			if (copy_from_user(kbuf, ubuf, ulen)) {
3416eeac8cdaSDan Carpenter 				rc = -EFAULT;
3417bc88ac47SMatthew R. Ochs 				goto out;
3418bc88ac47SMatthew R. Ochs 			}
3419bc88ac47SMatthew R. Ochs 		}
3420eeac8cdaSDan Carpenter 	}
3421bc88ac47SMatthew R. Ochs 
3422bc88ac47SMatthew R. Ochs 	memset(&rcb, 0, sizeof(rcb));
3423bc88ac47SMatthew R. Ochs 	memset(&asa, 0, sizeof(asa));
3424bc88ac47SMatthew R. Ochs 
3425bc88ac47SMatthew R. Ochs 	rcb.req_flags = req_flags;
3426bc88ac47SMatthew R. Ochs 	rcb.msi = SISL_MSI_RRQ_UPDATED;
3427bc88ac47SMatthew R. Ochs 	rcb.timeout = MC_AFU_DEBUG_TIMEOUT;
3428bc88ac47SMatthew R. Ochs 	rcb.ioasa = &asa;
3429bc88ac47SMatthew R. Ochs 
3430bc88ac47SMatthew R. Ochs 	if (ulen) {
3431bc88ac47SMatthew R. Ochs 		rcb.data_len = ulen;
3432bc88ac47SMatthew R. Ochs 		rcb.data_ea = (uintptr_t)kbuf;
3433bc88ac47SMatthew R. Ochs 	}
3434bc88ac47SMatthew R. Ochs 
3435bc88ac47SMatthew R. Ochs 	rcb.cdb[0] = SISL_AFU_CMD_DEBUG;
3436bc88ac47SMatthew R. Ochs 	memcpy(&rcb.cdb[4], afu_dbg->afu_subcmd,
3437bc88ac47SMatthew R. Ochs 	       HT_CXLFLASH_AFU_DEBUG_SUBCMD_LEN);
3438bc88ac47SMatthew R. Ochs 
3439bc88ac47SMatthew R. Ochs 	rc = send_afu_cmd(afu, &rcb);
3440bc88ac47SMatthew R. Ochs 	if (rc) {
3441bc88ac47SMatthew R. Ochs 		dev_err(dev, "%s: send_afu_cmd failed rc=%d asc=%08x afux=%x\n",
3442bc88ac47SMatthew R. Ochs 			__func__, rc, asa.ioasc, asa.afu_extra);
3443bc88ac47SMatthew R. Ochs 		goto out;
3444bc88ac47SMatthew R. Ochs 	}
3445bc88ac47SMatthew R. Ochs 
3446eeac8cdaSDan Carpenter 	if (ulen && !is_write) {
3447eeac8cdaSDan Carpenter 		if (copy_to_user(ubuf, kbuf, ulen))
3448eeac8cdaSDan Carpenter 			rc = -EFAULT;
3449eeac8cdaSDan Carpenter 	}
3450bc88ac47SMatthew R. Ochs out:
3451bc88ac47SMatthew R. Ochs 	kfree(buf);
3452bc88ac47SMatthew R. Ochs 	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
3453bc88ac47SMatthew R. Ochs 	return rc;
3454bc88ac47SMatthew R. Ochs }
3455bc88ac47SMatthew R. Ochs 
3456bc88ac47SMatthew R. Ochs /**
3457d6e32f53SMatthew R. Ochs  * cxlflash_chr_ioctl() - character device IOCTL handler
3458d6e32f53SMatthew R. Ochs  * @file:	File pointer for this device.
3459d6e32f53SMatthew R. Ochs  * @cmd:	IOCTL command.
3460d6e32f53SMatthew R. Ochs  * @arg:	Userspace ioctl data structure.
3461d6e32f53SMatthew R. Ochs  *
3462d6e32f53SMatthew R. Ochs  * A read/write semaphore is used to implement a 'drain' of currently
3463d6e32f53SMatthew R. Ochs  * running ioctls. The read semaphore is taken at the beginning of each
3464d6e32f53SMatthew R. Ochs  * ioctl thread and released upon concluding execution. Additionally the
3465d6e32f53SMatthew R. Ochs  * semaphore should be released and then reacquired in any ioctl execution
3466d6e32f53SMatthew R. Ochs  * path which will wait for an event to occur that is outside the scope of
3467d6e32f53SMatthew R. Ochs  * the ioctl (i.e. an adapter reset). To drain the ioctls currently running,
3468d6e32f53SMatthew R. Ochs  * a thread simply needs to acquire the write semaphore.
3469d6e32f53SMatthew R. Ochs  *
3470d6e32f53SMatthew R. Ochs  * Return: 0 on success, -errno on failure
3471d6e32f53SMatthew R. Ochs  */
cxlflash_chr_ioctl(struct file * file,unsigned int cmd,unsigned long arg)3472d6e32f53SMatthew R. Ochs static long cxlflash_chr_ioctl(struct file *file, unsigned int cmd,
3473d6e32f53SMatthew R. Ochs 			       unsigned long arg)
3474d6e32f53SMatthew R. Ochs {
3475d6e32f53SMatthew R. Ochs 	typedef int (*hioctl) (struct cxlflash_cfg *, void *);
3476d6e32f53SMatthew R. Ochs 
3477d6e32f53SMatthew R. Ochs 	struct cxlflash_cfg *cfg = file->private_data;
3478d6e32f53SMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
3479d6e32f53SMatthew R. Ochs 	char buf[sizeof(union cxlflash_ht_ioctls)];
3480d6e32f53SMatthew R. Ochs 	void __user *uarg = (void __user *)arg;
3481d6e32f53SMatthew R. Ochs 	struct ht_cxlflash_hdr *hdr;
3482d6e32f53SMatthew R. Ochs 	size_t size = 0;
3483d6e32f53SMatthew R. Ochs 	bool known_ioctl = false;
3484d6e32f53SMatthew R. Ochs 	int idx = 0;
3485d6e32f53SMatthew R. Ochs 	int rc = 0;
3486d6e32f53SMatthew R. Ochs 	hioctl do_ioctl = NULL;
3487d6e32f53SMatthew R. Ochs 
3488d6e32f53SMatthew R. Ochs 	static const struct {
3489d6e32f53SMatthew R. Ochs 		size_t size;
3490d6e32f53SMatthew R. Ochs 		hioctl ioctl;
3491d6e32f53SMatthew R. Ochs 	} ioctl_tbl[] = {	/* NOTE: order matters here */
34929cf43a36SMatthew R. Ochs 	{ sizeof(struct ht_cxlflash_lun_provision),
34939cf43a36SMatthew R. Ochs 		(hioctl)cxlflash_lun_provision },
3494bc88ac47SMatthew R. Ochs 	{ sizeof(struct ht_cxlflash_afu_debug),
3495bc88ac47SMatthew R. Ochs 		(hioctl)cxlflash_afu_debug },
3496d6e32f53SMatthew R. Ochs 	};
3497d6e32f53SMatthew R. Ochs 
3498d6e32f53SMatthew R. Ochs 	/* Hold read semaphore so we can drain if needed */
3499d6e32f53SMatthew R. Ochs 	down_read(&cfg->ioctl_rwsem);
3500d6e32f53SMatthew R. Ochs 
3501d6e32f53SMatthew R. Ochs 	dev_dbg(dev, "%s: cmd=%u idx=%d tbl_size=%lu\n",
3502d6e32f53SMatthew R. Ochs 		__func__, cmd, idx, sizeof(ioctl_tbl));
3503d6e32f53SMatthew R. Ochs 
3504d6e32f53SMatthew R. Ochs 	switch (cmd) {
35059cf43a36SMatthew R. Ochs 	case HT_CXLFLASH_LUN_PROVISION:
3506bc88ac47SMatthew R. Ochs 	case HT_CXLFLASH_AFU_DEBUG:
35079cf43a36SMatthew R. Ochs 		known_ioctl = true;
35089cf43a36SMatthew R. Ochs 		idx = _IOC_NR(HT_CXLFLASH_LUN_PROVISION) - _IOC_NR(cmd);
35099cf43a36SMatthew R. Ochs 		size = ioctl_tbl[idx].size;
35109cf43a36SMatthew R. Ochs 		do_ioctl = ioctl_tbl[idx].ioctl;
35119cf43a36SMatthew R. Ochs 
35129cf43a36SMatthew R. Ochs 		if (likely(do_ioctl))
35139cf43a36SMatthew R. Ochs 			break;
35149cf43a36SMatthew R. Ochs 
3515df561f66SGustavo A. R. Silva 		fallthrough;
3516d6e32f53SMatthew R. Ochs 	default:
3517d6e32f53SMatthew R. Ochs 		rc = -EINVAL;
3518d6e32f53SMatthew R. Ochs 		goto out;
3519d6e32f53SMatthew R. Ochs 	}
3520d6e32f53SMatthew R. Ochs 
3521d6e32f53SMatthew R. Ochs 	if (unlikely(copy_from_user(&buf, uarg, size))) {
3522d6e32f53SMatthew R. Ochs 		dev_err(dev, "%s: copy_from_user() fail "
3523d6e32f53SMatthew R. Ochs 			"size=%lu cmd=%d (%s) uarg=%p\n",
3524d6e32f53SMatthew R. Ochs 			__func__, size, cmd, decode_hioctl(cmd), uarg);
3525d6e32f53SMatthew R. Ochs 		rc = -EFAULT;
3526d6e32f53SMatthew R. Ochs 		goto out;
3527d6e32f53SMatthew R. Ochs 	}
3528d6e32f53SMatthew R. Ochs 
3529d6e32f53SMatthew R. Ochs 	hdr = (struct ht_cxlflash_hdr *)&buf;
3530d6e32f53SMatthew R. Ochs 	if (hdr->version != HT_CXLFLASH_VERSION_0) {
3531d6e32f53SMatthew R. Ochs 		dev_dbg(dev, "%s: Version %u not supported for %s\n",
3532d6e32f53SMatthew R. Ochs 			__func__, hdr->version, decode_hioctl(cmd));
3533d6e32f53SMatthew R. Ochs 		rc = -EINVAL;
3534d6e32f53SMatthew R. Ochs 		goto out;
3535d6e32f53SMatthew R. Ochs 	}
3536d6e32f53SMatthew R. Ochs 
3537d6e32f53SMatthew R. Ochs 	if (hdr->rsvd[0] || hdr->rsvd[1] || hdr->return_flags) {
3538d6e32f53SMatthew R. Ochs 		dev_dbg(dev, "%s: Reserved/rflags populated\n", __func__);
3539d6e32f53SMatthew R. Ochs 		rc = -EINVAL;
3540d6e32f53SMatthew R. Ochs 		goto out;
3541d6e32f53SMatthew R. Ochs 	}
3542d6e32f53SMatthew R. Ochs 
3543d6e32f53SMatthew R. Ochs 	rc = do_ioctl(cfg, (void *)&buf);
3544d6e32f53SMatthew R. Ochs 	if (likely(!rc))
3545d6e32f53SMatthew R. Ochs 		if (unlikely(copy_to_user(uarg, &buf, size))) {
3546d6e32f53SMatthew R. Ochs 			dev_err(dev, "%s: copy_to_user() fail "
3547d6e32f53SMatthew R. Ochs 				"size=%lu cmd=%d (%s) uarg=%p\n",
3548d6e32f53SMatthew R. Ochs 				__func__, size, cmd, decode_hioctl(cmd), uarg);
3549d6e32f53SMatthew R. Ochs 			rc = -EFAULT;
3550d6e32f53SMatthew R. Ochs 		}
3551d6e32f53SMatthew R. Ochs 
3552d6e32f53SMatthew R. Ochs 	/* fall through to exit */
3553d6e32f53SMatthew R. Ochs 
3554d6e32f53SMatthew R. Ochs out:
3555d6e32f53SMatthew R. Ochs 	up_read(&cfg->ioctl_rwsem);
3556d6e32f53SMatthew R. Ochs 	if (unlikely(rc && known_ioctl))
3557d6e32f53SMatthew R. Ochs 		dev_err(dev, "%s: ioctl %s (%08X) returned rc=%d\n",
3558d6e32f53SMatthew R. Ochs 			__func__, decode_hioctl(cmd), cmd, rc);
3559d6e32f53SMatthew R. Ochs 	else
3560d6e32f53SMatthew R. Ochs 		dev_dbg(dev, "%s: ioctl %s (%08X) returned rc=%d\n",
3561d6e32f53SMatthew R. Ochs 			__func__, decode_hioctl(cmd), cmd, rc);
3562d6e32f53SMatthew R. Ochs 	return rc;
3563d6e32f53SMatthew R. Ochs }
3564d6e32f53SMatthew R. Ochs 
3565a834a36bSUma Krishnan /*
3566a834a36bSUma Krishnan  * Character device file operations
3567a834a36bSUma Krishnan  */
3568a834a36bSUma Krishnan static const struct file_operations cxlflash_chr_fops = {
3569a834a36bSUma Krishnan 	.owner          = THIS_MODULE,
3570a834a36bSUma Krishnan 	.open           = cxlflash_chr_open,
3571d6e32f53SMatthew R. Ochs 	.unlocked_ioctl	= cxlflash_chr_ioctl,
35721832f2d8SArnd Bergmann 	.compat_ioctl	= compat_ptr_ioctl,
3573a834a36bSUma Krishnan };
3574a834a36bSUma Krishnan 
3575a834a36bSUma Krishnan /**
3576a834a36bSUma Krishnan  * init_chrdev() - initialize the character device for the host
3577a834a36bSUma Krishnan  * @cfg:	Internal structure associated with the host.
3578a834a36bSUma Krishnan  *
3579a834a36bSUma Krishnan  * Return: 0 on success, -errno on failure
3580a834a36bSUma Krishnan  */
init_chrdev(struct cxlflash_cfg * cfg)3581a834a36bSUma Krishnan static int init_chrdev(struct cxlflash_cfg *cfg)
3582a834a36bSUma Krishnan {
3583a834a36bSUma Krishnan 	struct device *dev = &cfg->dev->dev;
3584a834a36bSUma Krishnan 	struct device *char_dev;
3585a834a36bSUma Krishnan 	dev_t devno;
3586a834a36bSUma Krishnan 	int minor;
3587a834a36bSUma Krishnan 	int rc = 0;
3588a834a36bSUma Krishnan 
3589a834a36bSUma Krishnan 	minor = cxlflash_get_minor();
3590a834a36bSUma Krishnan 	if (unlikely(minor < 0)) {
3591a834a36bSUma Krishnan 		dev_err(dev, "%s: Exhausted allowed adapters\n", __func__);
3592a834a36bSUma Krishnan 		rc = -ENOSPC;
3593a834a36bSUma Krishnan 		goto out;
3594a834a36bSUma Krishnan 	}
3595a834a36bSUma Krishnan 
3596a834a36bSUma Krishnan 	devno = MKDEV(cxlflash_major, minor);
3597a834a36bSUma Krishnan 	cdev_init(&cfg->cdev, &cxlflash_chr_fops);
3598a834a36bSUma Krishnan 
3599a834a36bSUma Krishnan 	rc = cdev_add(&cfg->cdev, devno, 1);
3600a834a36bSUma Krishnan 	if (rc) {
3601a834a36bSUma Krishnan 		dev_err(dev, "%s: cdev_add failed rc=%d\n", __func__, rc);
3602a834a36bSUma Krishnan 		goto err1;
3603a834a36bSUma Krishnan 	}
3604a834a36bSUma Krishnan 
3605a834a36bSUma Krishnan 	char_dev = device_create(cxlflash_class, NULL, devno,
3606a834a36bSUma Krishnan 				 NULL, "cxlflash%d", minor);
3607a834a36bSUma Krishnan 	if (IS_ERR(char_dev)) {
3608a834a36bSUma Krishnan 		rc = PTR_ERR(char_dev);
3609a834a36bSUma Krishnan 		dev_err(dev, "%s: device_create failed rc=%d\n",
3610a834a36bSUma Krishnan 			__func__, rc);
3611a834a36bSUma Krishnan 		goto err2;
3612a834a36bSUma Krishnan 	}
3613a834a36bSUma Krishnan 
3614a834a36bSUma Krishnan 	cfg->chardev = char_dev;
3615a834a36bSUma Krishnan out:
3616a834a36bSUma Krishnan 	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
3617a834a36bSUma Krishnan 	return rc;
3618a834a36bSUma Krishnan err2:
3619a834a36bSUma Krishnan 	cdev_del(&cfg->cdev);
3620a834a36bSUma Krishnan err1:
3621a834a36bSUma Krishnan 	cxlflash_put_minor(minor);
3622a834a36bSUma Krishnan 	goto out;
3623a834a36bSUma Krishnan }
3624a834a36bSUma Krishnan 
3625a834a36bSUma Krishnan /**
3626c21e0bbfSMatthew R. Ochs  * cxlflash_probe() - PCI entry point to add host
3627c21e0bbfSMatthew R. Ochs  * @pdev:	PCI device associated with the host.
3628c21e0bbfSMatthew R. Ochs  * @dev_id:	PCI device id associated with device.
3629c21e0bbfSMatthew R. Ochs  *
3630323e3342SMatthew R. Ochs  * The device will initially start out in a 'probing' state and
3631323e3342SMatthew R. Ochs  * transition to the 'normal' state at the end of a successful
3632323e3342SMatthew R. Ochs  * probe. Should an EEH event occur during probe, the notification
3633323e3342SMatthew R. Ochs  * thread (error_detected()) will wait until the probe handler
3634323e3342SMatthew R. Ochs  * is nearly complete. At that time, the device will be moved to
3635323e3342SMatthew R. Ochs  * a 'probed' state and the EEH thread woken up to drive the slot
3636323e3342SMatthew R. Ochs  * reset and recovery (device moves to 'normal' state). Meanwhile,
3637323e3342SMatthew R. Ochs  * the probe will be allowed to exit successfully.
3638323e3342SMatthew R. Ochs  *
36391284fb0cSMatthew R. Ochs  * Return: 0 on success, -errno on failure
3640c21e0bbfSMatthew R. Ochs  */
cxlflash_probe(struct pci_dev * pdev,const struct pci_device_id * dev_id)3641c21e0bbfSMatthew R. Ochs static int cxlflash_probe(struct pci_dev *pdev,
3642c21e0bbfSMatthew R. Ochs 			  const struct pci_device_id *dev_id)
3643c21e0bbfSMatthew R. Ochs {
3644c21e0bbfSMatthew R. Ochs 	struct Scsi_Host *host;
3645c21e0bbfSMatthew R. Ochs 	struct cxlflash_cfg *cfg = NULL;
3646fb67d44dSMatthew R. Ochs 	struct device *dev = &pdev->dev;
3647c21e0bbfSMatthew R. Ochs 	struct dev_dependent_vals *ddv;
3648c21e0bbfSMatthew R. Ochs 	int rc = 0;
364978ae028eSMatthew R. Ochs 	int k;
3650c21e0bbfSMatthew R. Ochs 
3651c21e0bbfSMatthew R. Ochs 	dev_dbg(&pdev->dev, "%s: Found CXLFLASH with IRQ: %d\n",
3652c21e0bbfSMatthew R. Ochs 		__func__, pdev->irq);
3653c21e0bbfSMatthew R. Ochs 
3654c21e0bbfSMatthew R. Ochs 	ddv = (struct dev_dependent_vals *)dev_id->driver_data;
3655c21e0bbfSMatthew R. Ochs 	driver_template.max_sectors = ddv->max_sectors;
3656c21e0bbfSMatthew R. Ochs 
3657c21e0bbfSMatthew R. Ochs 	host = scsi_host_alloc(&driver_template, sizeof(struct cxlflash_cfg));
3658c21e0bbfSMatthew R. Ochs 	if (!host) {
3659fb67d44dSMatthew R. Ochs 		dev_err(dev, "%s: scsi_host_alloc failed\n", __func__);
3660c21e0bbfSMatthew R. Ochs 		rc = -ENOMEM;
3661c21e0bbfSMatthew R. Ochs 		goto out;
3662c21e0bbfSMatthew R. Ochs 	}
3663c21e0bbfSMatthew R. Ochs 
3664c21e0bbfSMatthew R. Ochs 	host->max_id = CXLFLASH_MAX_NUM_TARGETS_PER_BUS;
3665c21e0bbfSMatthew R. Ochs 	host->max_lun = CXLFLASH_MAX_NUM_LUNS_PER_TARGET;
3666c21e0bbfSMatthew R. Ochs 	host->unique_id = host->host_no;
3667c21e0bbfSMatthew R. Ochs 	host->max_cmd_len = CXLFLASH_MAX_CDB_LEN;
3668c21e0bbfSMatthew R. Ochs 
3669fb67d44dSMatthew R. Ochs 	cfg = shost_priv(host);
3670bb61b843SVaibhav Jain 	cfg->state = STATE_PROBING;
3671c21e0bbfSMatthew R. Ochs 	cfg->host = host;
3672c21e0bbfSMatthew R. Ochs 	rc = alloc_mem(cfg);
3673c21e0bbfSMatthew R. Ochs 	if (rc) {
3674fb67d44dSMatthew R. Ochs 		dev_err(dev, "%s: alloc_mem failed\n", __func__);
3675c21e0bbfSMatthew R. Ochs 		rc = -ENOMEM;
36768b5b1e87SMatthew R. Ochs 		scsi_host_put(cfg->host);
3677c21e0bbfSMatthew R. Ochs 		goto out;
3678c21e0bbfSMatthew R. Ochs 	}
3679c21e0bbfSMatthew R. Ochs 
3680c21e0bbfSMatthew R. Ochs 	cfg->init_state = INIT_STATE_NONE;
3681c21e0bbfSMatthew R. Ochs 	cfg->dev = pdev;
368217ead26fSMatthew R. Ochs 	cfg->cxl_fops = cxlflash_cxl_fops;
3683de5d35afSUma Krishnan 	cfg->ops = cxlflash_assign_ops(ddv);
3684de5d35afSUma Krishnan 	WARN_ON_ONCE(!cfg->ops);
368507d0c52fSUma Krishnan 
36862cb79266SMatthew R. Ochs 	/*
368778ae028eSMatthew R. Ochs 	 * Promoted LUNs move to the top of the LUN table. The rest stay on
368878ae028eSMatthew R. Ochs 	 * the bottom half. The bottom half grows from the end (index = 255),
368978ae028eSMatthew R. Ochs 	 * whereas the top half grows from the beginning (index = 0).
369078ae028eSMatthew R. Ochs 	 *
369178ae028eSMatthew R. Ochs 	 * Initialize the last LUN index for all possible ports.
36922cb79266SMatthew R. Ochs 	 */
36932cb79266SMatthew R. Ochs 	cfg->promote_lun_index = 0;
369478ae028eSMatthew R. Ochs 
369578ae028eSMatthew R. Ochs 	for (k = 0; k < MAX_FC_PORTS; k++)
369678ae028eSMatthew R. Ochs 		cfg->last_lun_index[k] = CXLFLASH_NUM_VLUNS/2 - 1;
36972cb79266SMatthew R. Ochs 
3698c21e0bbfSMatthew R. Ochs 	cfg->dev_id = (struct pci_device_id *)dev_id;
3699c21e0bbfSMatthew R. Ochs 
3700c21e0bbfSMatthew R. Ochs 	init_waitqueue_head(&cfg->tmf_waitq);
3701439e85c1SMatthew R. Ochs 	init_waitqueue_head(&cfg->reset_waitq);
3702c21e0bbfSMatthew R. Ochs 
3703c21e0bbfSMatthew R. Ochs 	INIT_WORK(&cfg->work_q, cxlflash_worker_thread);
3704c21e0bbfSMatthew R. Ochs 	cfg->lr_state = LINK_RESET_INVALID;
3705c21e0bbfSMatthew R. Ochs 	cfg->lr_port = -1;
37060d73122cSMatthew R. Ochs 	spin_lock_init(&cfg->tmf_slock);
370765be2c79SMatthew R. Ochs 	mutex_init(&cfg->ctx_tbl_list_mutex);
370865be2c79SMatthew R. Ochs 	mutex_init(&cfg->ctx_recovery_mutex);
37090a27ae51SMatthew R. Ochs 	init_rwsem(&cfg->ioctl_rwsem);
371065be2c79SMatthew R. Ochs 	INIT_LIST_HEAD(&cfg->ctx_err_recovery);
371165be2c79SMatthew R. Ochs 	INIT_LIST_HEAD(&cfg->lluns);
3712c21e0bbfSMatthew R. Ochs 
3713c21e0bbfSMatthew R. Ochs 	pci_set_drvdata(pdev, cfg);
3714c21e0bbfSMatthew R. Ochs 
3715c21e0bbfSMatthew R. Ochs 	rc = init_pci(cfg);
3716c21e0bbfSMatthew R. Ochs 	if (rc) {
3717fb67d44dSMatthew R. Ochs 		dev_err(dev, "%s: init_pci failed rc=%d\n", __func__, rc);
3718c21e0bbfSMatthew R. Ochs 		goto out_remove;
3719c21e0bbfSMatthew R. Ochs 	}
3720c21e0bbfSMatthew R. Ochs 	cfg->init_state = INIT_STATE_PCI;
3721c21e0bbfSMatthew R. Ochs 
372248e077dbSUma Krishnan 	cfg->afu_cookie = cfg->ops->create_afu(pdev);
372348e077dbSUma Krishnan 	if (unlikely(!cfg->afu_cookie)) {
372448e077dbSUma Krishnan 		dev_err(dev, "%s: create_afu failed\n", __func__);
3725d0b1e4a6SWei Yongjun 		rc = -ENOMEM;
372648e077dbSUma Krishnan 		goto out_remove;
372748e077dbSUma Krishnan 	}
372848e077dbSUma Krishnan 
3729c21e0bbfSMatthew R. Ochs 	rc = init_afu(cfg);
3730323e3342SMatthew R. Ochs 	if (rc && !wq_has_sleeper(&cfg->reset_waitq)) {
3731fb67d44dSMatthew R. Ochs 		dev_err(dev, "%s: init_afu failed rc=%d\n", __func__, rc);
3732c21e0bbfSMatthew R. Ochs 		goto out_remove;
3733c21e0bbfSMatthew R. Ochs 	}
3734c21e0bbfSMatthew R. Ochs 	cfg->init_state = INIT_STATE_AFU;
3735c21e0bbfSMatthew R. Ochs 
3736c21e0bbfSMatthew R. Ochs 	rc = init_scsi(cfg);
3737c21e0bbfSMatthew R. Ochs 	if (rc) {
3738fb67d44dSMatthew R. Ochs 		dev_err(dev, "%s: init_scsi failed rc=%d\n", __func__, rc);
3739c21e0bbfSMatthew R. Ochs 		goto out_remove;
3740c21e0bbfSMatthew R. Ochs 	}
3741c21e0bbfSMatthew R. Ochs 	cfg->init_state = INIT_STATE_SCSI;
3742c21e0bbfSMatthew R. Ochs 
3743a834a36bSUma Krishnan 	rc = init_chrdev(cfg);
3744a834a36bSUma Krishnan 	if (rc) {
3745a834a36bSUma Krishnan 		dev_err(dev, "%s: init_chrdev failed rc=%d\n", __func__, rc);
3746a834a36bSUma Krishnan 		goto out_remove;
3747a834a36bSUma Krishnan 	}
3748a834a36bSUma Krishnan 	cfg->init_state = INIT_STATE_CDEV;
3749a834a36bSUma Krishnan 
3750323e3342SMatthew R. Ochs 	if (wq_has_sleeper(&cfg->reset_waitq)) {
3751323e3342SMatthew R. Ochs 		cfg->state = STATE_PROBED;
3752323e3342SMatthew R. Ochs 		wake_up_all(&cfg->reset_waitq);
3753323e3342SMatthew R. Ochs 	} else
3754323e3342SMatthew R. Ochs 		cfg->state = STATE_NORMAL;
3755c21e0bbfSMatthew R. Ochs out:
3756fb67d44dSMatthew R. Ochs 	dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
3757c21e0bbfSMatthew R. Ochs 	return rc;
3758c21e0bbfSMatthew R. Ochs 
3759c21e0bbfSMatthew R. Ochs out_remove:
3760bb61b843SVaibhav Jain 	cfg->state = STATE_PROBED;
3761c21e0bbfSMatthew R. Ochs 	cxlflash_remove(pdev);
3762c21e0bbfSMatthew R. Ochs 	goto out;
3763c21e0bbfSMatthew R. Ochs }
3764c21e0bbfSMatthew R. Ochs 
37655cdac81aSMatthew R. Ochs /**
37665cdac81aSMatthew R. Ochs  * cxlflash_pci_error_detected() - called when a PCI error is detected
37675cdac81aSMatthew R. Ochs  * @pdev:	PCI device struct.
37685cdac81aSMatthew R. Ochs  * @state:	PCI channel state.
37695cdac81aSMatthew R. Ochs  *
37701d3324c3SMatthew R. Ochs  * When an EEH occurs during an active reset, wait until the reset is
37711d3324c3SMatthew R. Ochs  * complete and then take action based upon the device state.
37721d3324c3SMatthew R. Ochs  *
37735cdac81aSMatthew R. Ochs  * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
37745cdac81aSMatthew R. Ochs  */
cxlflash_pci_error_detected(struct pci_dev * pdev,pci_channel_state_t state)37755cdac81aSMatthew R. Ochs static pci_ers_result_t cxlflash_pci_error_detected(struct pci_dev *pdev,
37765cdac81aSMatthew R. Ochs 						    pci_channel_state_t state)
37775cdac81aSMatthew R. Ochs {
377865be2c79SMatthew R. Ochs 	int rc = 0;
37795cdac81aSMatthew R. Ochs 	struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
37805cdac81aSMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
37815cdac81aSMatthew R. Ochs 
37825cdac81aSMatthew R. Ochs 	dev_dbg(dev, "%s: pdev=%p state=%u\n", __func__, pdev, state);
37835cdac81aSMatthew R. Ochs 
37845cdac81aSMatthew R. Ochs 	switch (state) {
37855cdac81aSMatthew R. Ochs 	case pci_channel_io_frozen:
3786323e3342SMatthew R. Ochs 		wait_event(cfg->reset_waitq, cfg->state != STATE_RESET &&
3787323e3342SMatthew R. Ochs 					     cfg->state != STATE_PROBING);
37881d3324c3SMatthew R. Ochs 		if (cfg->state == STATE_FAILTERM)
37891d3324c3SMatthew R. Ochs 			return PCI_ERS_RESULT_DISCONNECT;
37901d3324c3SMatthew R. Ochs 
3791439e85c1SMatthew R. Ochs 		cfg->state = STATE_RESET;
37925cdac81aSMatthew R. Ochs 		scsi_block_requests(cfg->host);
37930a27ae51SMatthew R. Ochs 		drain_ioctls(cfg);
379465be2c79SMatthew R. Ochs 		rc = cxlflash_mark_contexts_error(cfg);
379565be2c79SMatthew R. Ochs 		if (unlikely(rc))
3796fb67d44dSMatthew R. Ochs 			dev_err(dev, "%s: Failed to mark user contexts rc=%d\n",
379765be2c79SMatthew R. Ochs 				__func__, rc);
37989526f360SManoj N. Kumar 		term_afu(cfg);
37995cdac81aSMatthew R. Ochs 		return PCI_ERS_RESULT_NEED_RESET;
38005cdac81aSMatthew R. Ochs 	case pci_channel_io_perm_failure:
38015cdac81aSMatthew R. Ochs 		cfg->state = STATE_FAILTERM;
3802439e85c1SMatthew R. Ochs 		wake_up_all(&cfg->reset_waitq);
38035cdac81aSMatthew R. Ochs 		scsi_unblock_requests(cfg->host);
38045cdac81aSMatthew R. Ochs 		return PCI_ERS_RESULT_DISCONNECT;
38055cdac81aSMatthew R. Ochs 	default:
38065cdac81aSMatthew R. Ochs 		break;
38075cdac81aSMatthew R. Ochs 	}
38085cdac81aSMatthew R. Ochs 	return PCI_ERS_RESULT_NEED_RESET;
38095cdac81aSMatthew R. Ochs }
38105cdac81aSMatthew R. Ochs 
38115cdac81aSMatthew R. Ochs /**
38125cdac81aSMatthew R. Ochs  * cxlflash_pci_slot_reset() - called when PCI slot has been reset
38135cdac81aSMatthew R. Ochs  * @pdev:	PCI device struct.
38145cdac81aSMatthew R. Ochs  *
38155cdac81aSMatthew R. Ochs  * This routine is called by the pci error recovery code after the PCI
38165cdac81aSMatthew R. Ochs  * slot has been reset, just before we should resume normal operations.
38175cdac81aSMatthew R. Ochs  *
38185cdac81aSMatthew R. Ochs  * Return: PCI_ERS_RESULT_RECOVERED or PCI_ERS_RESULT_DISCONNECT
38195cdac81aSMatthew R. Ochs  */
cxlflash_pci_slot_reset(struct pci_dev * pdev)38205cdac81aSMatthew R. Ochs static pci_ers_result_t cxlflash_pci_slot_reset(struct pci_dev *pdev)
38215cdac81aSMatthew R. Ochs {
38225cdac81aSMatthew R. Ochs 	int rc = 0;
38235cdac81aSMatthew R. Ochs 	struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
38245cdac81aSMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
38255cdac81aSMatthew R. Ochs 
38265cdac81aSMatthew R. Ochs 	dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
38275cdac81aSMatthew R. Ochs 
38285cdac81aSMatthew R. Ochs 	rc = init_afu(cfg);
38295cdac81aSMatthew R. Ochs 	if (unlikely(rc)) {
3830fb67d44dSMatthew R. Ochs 		dev_err(dev, "%s: EEH recovery failed rc=%d\n", __func__, rc);
38315cdac81aSMatthew R. Ochs 		return PCI_ERS_RESULT_DISCONNECT;
38325cdac81aSMatthew R. Ochs 	}
38335cdac81aSMatthew R. Ochs 
38345cdac81aSMatthew R. Ochs 	return PCI_ERS_RESULT_RECOVERED;
38355cdac81aSMatthew R. Ochs }
38365cdac81aSMatthew R. Ochs 
38375cdac81aSMatthew R. Ochs /**
38385cdac81aSMatthew R. Ochs  * cxlflash_pci_resume() - called when normal operation can resume
38395cdac81aSMatthew R. Ochs  * @pdev:	PCI device struct
38405cdac81aSMatthew R. Ochs  */
cxlflash_pci_resume(struct pci_dev * pdev)38415cdac81aSMatthew R. Ochs static void cxlflash_pci_resume(struct pci_dev *pdev)
38425cdac81aSMatthew R. Ochs {
38435cdac81aSMatthew R. Ochs 	struct cxlflash_cfg *cfg = pci_get_drvdata(pdev);
38445cdac81aSMatthew R. Ochs 	struct device *dev = &cfg->dev->dev;
38455cdac81aSMatthew R. Ochs 
38465cdac81aSMatthew R. Ochs 	dev_dbg(dev, "%s: pdev=%p\n", __func__, pdev);
38475cdac81aSMatthew R. Ochs 
38485cdac81aSMatthew R. Ochs 	cfg->state = STATE_NORMAL;
3849439e85c1SMatthew R. Ochs 	wake_up_all(&cfg->reset_waitq);
38505cdac81aSMatthew R. Ochs 	scsi_unblock_requests(cfg->host);
38515cdac81aSMatthew R. Ochs }
38525cdac81aSMatthew R. Ochs 
3853a834a36bSUma Krishnan /**
3854a834a36bSUma Krishnan  * cxlflash_devnode() - provides devtmpfs for devices in the cxlflash class
3855a834a36bSUma Krishnan  * @dev:	Character device.
3856a834a36bSUma Krishnan  * @mode:	Mode that can be used to verify access.
3857a834a36bSUma Krishnan  *
3858a834a36bSUma Krishnan  * Return: Allocated string describing the devtmpfs structure.
3859a834a36bSUma Krishnan  */
cxlflash_devnode(const struct device * dev,umode_t * mode)386069b14fdeSStephen Rothwell static char *cxlflash_devnode(const struct device *dev, umode_t *mode)
3861a834a36bSUma Krishnan {
3862a834a36bSUma Krishnan 	return kasprintf(GFP_KERNEL, "cxlflash/%s", dev_name(dev));
3863a834a36bSUma Krishnan }
3864a834a36bSUma Krishnan 
3865a834a36bSUma Krishnan /**
3866a834a36bSUma Krishnan  * cxlflash_class_init() - create character device class
3867a834a36bSUma Krishnan  *
3868a834a36bSUma Krishnan  * Return: 0 on success, -errno on failure
3869a834a36bSUma Krishnan  */
cxlflash_class_init(void)3870a834a36bSUma Krishnan static int cxlflash_class_init(void)
3871a834a36bSUma Krishnan {
3872a834a36bSUma Krishnan 	dev_t devno;
3873a834a36bSUma Krishnan 	int rc = 0;
3874a834a36bSUma Krishnan 
3875a834a36bSUma Krishnan 	rc = alloc_chrdev_region(&devno, 0, CXLFLASH_MAX_ADAPTERS, "cxlflash");
3876a834a36bSUma Krishnan 	if (unlikely(rc)) {
3877a834a36bSUma Krishnan 		pr_err("%s: alloc_chrdev_region failed rc=%d\n", __func__, rc);
3878a834a36bSUma Krishnan 		goto out;
3879a834a36bSUma Krishnan 	}
3880a834a36bSUma Krishnan 
3881a834a36bSUma Krishnan 	cxlflash_major = MAJOR(devno);
3882a834a36bSUma Krishnan 
3883*1aaba11dSGreg Kroah-Hartman 	cxlflash_class = class_create("cxlflash");
3884a834a36bSUma Krishnan 	if (IS_ERR(cxlflash_class)) {
3885a834a36bSUma Krishnan 		rc = PTR_ERR(cxlflash_class);
3886a834a36bSUma Krishnan 		pr_err("%s: class_create failed rc=%d\n", __func__, rc);
3887a834a36bSUma Krishnan 		goto err;
3888a834a36bSUma Krishnan 	}
3889a834a36bSUma Krishnan 
3890a834a36bSUma Krishnan 	cxlflash_class->devnode = cxlflash_devnode;
3891a834a36bSUma Krishnan out:
3892a834a36bSUma Krishnan 	pr_debug("%s: returning rc=%d\n", __func__, rc);
3893a834a36bSUma Krishnan 	return rc;
3894a834a36bSUma Krishnan err:
3895a834a36bSUma Krishnan 	unregister_chrdev_region(devno, CXLFLASH_MAX_ADAPTERS);
3896a834a36bSUma Krishnan 	goto out;
3897a834a36bSUma Krishnan }
3898a834a36bSUma Krishnan 
3899a834a36bSUma Krishnan /**
3900a834a36bSUma Krishnan  * cxlflash_class_exit() - destroy character device class
3901a834a36bSUma Krishnan  */
cxlflash_class_exit(void)3902a834a36bSUma Krishnan static void cxlflash_class_exit(void)
3903a834a36bSUma Krishnan {
3904a834a36bSUma Krishnan 	dev_t devno = MKDEV(cxlflash_major, 0);
3905a834a36bSUma Krishnan 
3906a834a36bSUma Krishnan 	class_destroy(cxlflash_class);
3907a834a36bSUma Krishnan 	unregister_chrdev_region(devno, CXLFLASH_MAX_ADAPTERS);
3908a834a36bSUma Krishnan }
3909a834a36bSUma Krishnan 
39105cdac81aSMatthew R. Ochs static const struct pci_error_handlers cxlflash_err_handler = {
39115cdac81aSMatthew R. Ochs 	.error_detected = cxlflash_pci_error_detected,
39125cdac81aSMatthew R. Ochs 	.slot_reset = cxlflash_pci_slot_reset,
39135cdac81aSMatthew R. Ochs 	.resume = cxlflash_pci_resume,
39145cdac81aSMatthew R. Ochs };
39155cdac81aSMatthew R. Ochs 
3916c21e0bbfSMatthew R. Ochs /*
3917c21e0bbfSMatthew R. Ochs  * PCI device structure
3918c21e0bbfSMatthew R. Ochs  */
3919c21e0bbfSMatthew R. Ochs static struct pci_driver cxlflash_driver = {
3920c21e0bbfSMatthew R. Ochs 	.name = CXLFLASH_NAME,
3921c21e0bbfSMatthew R. Ochs 	.id_table = cxlflash_pci_table,
3922c21e0bbfSMatthew R. Ochs 	.probe = cxlflash_probe,
3923c21e0bbfSMatthew R. Ochs 	.remove = cxlflash_remove,
3924babf985dSUma Krishnan 	.shutdown = cxlflash_remove,
39255cdac81aSMatthew R. Ochs 	.err_handler = &cxlflash_err_handler,
3926c21e0bbfSMatthew R. Ochs };
3927c21e0bbfSMatthew R. Ochs 
3928c21e0bbfSMatthew R. Ochs /**
3929c21e0bbfSMatthew R. Ochs  * init_cxlflash() - module entry point
3930c21e0bbfSMatthew R. Ochs  *
39311284fb0cSMatthew R. Ochs  * Return: 0 on success, -errno on failure
3932c21e0bbfSMatthew R. Ochs  */
init_cxlflash(void)3933c21e0bbfSMatthew R. Ochs static int __init init_cxlflash(void)
3934c21e0bbfSMatthew R. Ochs {
3935a834a36bSUma Krishnan 	int rc;
3936a834a36bSUma Krishnan 
3937cd41e18dSMatthew R. Ochs 	check_sizes();
393865be2c79SMatthew R. Ochs 	cxlflash_list_init();
3939a834a36bSUma Krishnan 	rc = cxlflash_class_init();
3940a834a36bSUma Krishnan 	if (unlikely(rc))
3941a834a36bSUma Krishnan 		goto out;
394265be2c79SMatthew R. Ochs 
3943a834a36bSUma Krishnan 	rc = pci_register_driver(&cxlflash_driver);
3944a834a36bSUma Krishnan 	if (unlikely(rc))
3945a834a36bSUma Krishnan 		goto err;
3946a834a36bSUma Krishnan out:
3947a834a36bSUma Krishnan 	pr_debug("%s: returning rc=%d\n", __func__, rc);
3948a834a36bSUma Krishnan 	return rc;
3949a834a36bSUma Krishnan err:
3950a834a36bSUma Krishnan 	cxlflash_class_exit();
3951a834a36bSUma Krishnan 	goto out;
3952c21e0bbfSMatthew R. Ochs }
3953c21e0bbfSMatthew R. Ochs 
3954c21e0bbfSMatthew R. Ochs /**
3955c21e0bbfSMatthew R. Ochs  * exit_cxlflash() - module exit point
3956c21e0bbfSMatthew R. Ochs  */
exit_cxlflash(void)3957c21e0bbfSMatthew R. Ochs static void __exit exit_cxlflash(void)
3958c21e0bbfSMatthew R. Ochs {
395965be2c79SMatthew R. Ochs 	cxlflash_term_global_luns();
396065be2c79SMatthew R. Ochs 	cxlflash_free_errpage();
396165be2c79SMatthew R. Ochs 
3962c21e0bbfSMatthew R. Ochs 	pci_unregister_driver(&cxlflash_driver);
3963a834a36bSUma Krishnan 	cxlflash_class_exit();
3964c21e0bbfSMatthew R. Ochs }
3965c21e0bbfSMatthew R. Ochs 
3966c21e0bbfSMatthew R. Ochs module_init(init_cxlflash);
3967c21e0bbfSMatthew R. Ochs module_exit(exit_cxlflash);
3968