xref: /linux/drivers/scsi/ibmvscsi_tgt/ibmvscsi_tgt.c (revision 387b978cb0d12cf3720ecb17e652e0a9991a08e2)
188a678bbSBryant G. Ly /*******************************************************************************
288a678bbSBryant G. Ly  * IBM Virtual SCSI Target Driver
388a678bbSBryant G. Ly  * Copyright (C) 2003-2005 Dave Boutcher (boutcher@us.ibm.com) IBM Corp.
488a678bbSBryant G. Ly  *			   Santiago Leon (santil@us.ibm.com) IBM Corp.
588a678bbSBryant G. Ly  *			   Linda Xie (lxie@us.ibm.com) IBM Corp.
688a678bbSBryant G. Ly  *
788a678bbSBryant G. Ly  * Copyright (C) 2005-2011 FUJITA Tomonori <tomof@acm.org>
888a678bbSBryant G. Ly  * Copyright (C) 2010 Nicholas A. Bellinger <nab@kernel.org>
988a678bbSBryant G. Ly  *
1088a678bbSBryant G. Ly  * Authors: Bryant G. Ly <bryantly@linux.vnet.ibm.com>
1188a678bbSBryant G. Ly  * Authors: Michael Cyr <mikecyr@linux.vnet.ibm.com>
1288a678bbSBryant G. Ly  *
1388a678bbSBryant G. Ly  * This program is free software; you can redistribute it and/or modify
1488a678bbSBryant G. Ly  * it under the terms of the GNU General Public License as published by
1588a678bbSBryant G. Ly  * the Free Software Foundation; either version 2 of the License, or
1688a678bbSBryant G. Ly  * (at your option) any later version.
1788a678bbSBryant G. Ly  *
1888a678bbSBryant G. Ly  * This program is distributed in the hope that it will be useful,
1988a678bbSBryant G. Ly  * but WITHOUT ANY WARRANTY; without even the implied warranty of
2088a678bbSBryant G. Ly  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2188a678bbSBryant G. Ly  * GNU General Public License for more details.
2288a678bbSBryant G. Ly  *
2388a678bbSBryant G. Ly  ****************************************************************************/
2488a678bbSBryant G. Ly 
2588a678bbSBryant G. Ly #define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
2688a678bbSBryant G. Ly 
2788a678bbSBryant G. Ly #include <linux/module.h>
2888a678bbSBryant G. Ly #include <linux/kernel.h>
2988a678bbSBryant G. Ly #include <linux/slab.h>
3088a678bbSBryant G. Ly #include <linux/types.h>
3188a678bbSBryant G. Ly #include <linux/list.h>
3288a678bbSBryant G. Ly #include <linux/string.h>
33e9409b26SBryant G. Ly #include <linux/delay.h>
3488a678bbSBryant G. Ly 
3588a678bbSBryant G. Ly #include <target/target_core_base.h>
3688a678bbSBryant G. Ly #include <target/target_core_fabric.h>
3788a678bbSBryant G. Ly 
3888a678bbSBryant G. Ly #include <asm/hvcall.h>
3988a678bbSBryant G. Ly #include <asm/vio.h>
4088a678bbSBryant G. Ly 
4188a678bbSBryant G. Ly #include <scsi/viosrp.h>
4288a678bbSBryant G. Ly 
4388a678bbSBryant G. Ly #include "ibmvscsi_tgt.h"
4488a678bbSBryant G. Ly 
4588a678bbSBryant G. Ly #define IBMVSCSIS_VERSION	"v0.2"
4688a678bbSBryant G. Ly 
4788a678bbSBryant G. Ly #define	INITIAL_SRP_LIMIT	800
4888a678bbSBryant G. Ly #define	DEFAULT_MAX_SECTORS	256
49*387b978cSBryant G. Ly #define MAX_TXU			1024 * 1024
5088a678bbSBryant G. Ly 
5188a678bbSBryant G. Ly static uint max_vdma_size = MAX_H_COPY_RDMA;
5288a678bbSBryant G. Ly 
5388a678bbSBryant G. Ly static char system_id[SYS_ID_NAME_LEN] = "";
5488a678bbSBryant G. Ly static char partition_name[PARTITION_NAMELEN] = "UNKNOWN";
5588a678bbSBryant G. Ly static uint partition_number = -1;
5688a678bbSBryant G. Ly 
5788a678bbSBryant G. Ly /* Adapter list and lock to control it */
5888a678bbSBryant G. Ly static DEFINE_SPINLOCK(ibmvscsis_dev_lock);
5988a678bbSBryant G. Ly static LIST_HEAD(ibmvscsis_dev_list);
6088a678bbSBryant G. Ly 
6188a678bbSBryant G. Ly static long ibmvscsis_parse_command(struct scsi_info *vscsi,
6288a678bbSBryant G. Ly 				    struct viosrp_crq *crq);
6388a678bbSBryant G. Ly 
6488a678bbSBryant G. Ly static void ibmvscsis_adapter_idle(struct scsi_info *vscsi);
6588a678bbSBryant G. Ly 
6688a678bbSBryant G. Ly static void ibmvscsis_determine_resid(struct se_cmd *se_cmd,
6788a678bbSBryant G. Ly 				      struct srp_rsp *rsp)
6888a678bbSBryant G. Ly {
6988a678bbSBryant G. Ly 	u32 residual_count = se_cmd->residual_count;
7088a678bbSBryant G. Ly 
7188a678bbSBryant G. Ly 	if (!residual_count)
7288a678bbSBryant G. Ly 		return;
7388a678bbSBryant G. Ly 
7488a678bbSBryant G. Ly 	if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
7588a678bbSBryant G. Ly 		if (se_cmd->data_direction == DMA_TO_DEVICE) {
7688a678bbSBryant G. Ly 			/* residual data from an underflow write */
7788a678bbSBryant G. Ly 			rsp->flags = SRP_RSP_FLAG_DOUNDER;
7888a678bbSBryant G. Ly 			rsp->data_out_res_cnt = cpu_to_be32(residual_count);
7988a678bbSBryant G. Ly 		} else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
8088a678bbSBryant G. Ly 			/* residual data from an underflow read */
8188a678bbSBryant G. Ly 			rsp->flags = SRP_RSP_FLAG_DIUNDER;
8288a678bbSBryant G. Ly 			rsp->data_in_res_cnt = cpu_to_be32(residual_count);
8388a678bbSBryant G. Ly 		}
8488a678bbSBryant G. Ly 	} else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
8588a678bbSBryant G. Ly 		if (se_cmd->data_direction == DMA_TO_DEVICE) {
8688a678bbSBryant G. Ly 			/* residual data from an overflow write */
8788a678bbSBryant G. Ly 			rsp->flags = SRP_RSP_FLAG_DOOVER;
8888a678bbSBryant G. Ly 			rsp->data_out_res_cnt = cpu_to_be32(residual_count);
8988a678bbSBryant G. Ly 		} else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
9088a678bbSBryant G. Ly 			/* residual data from an overflow read */
9188a678bbSBryant G. Ly 			rsp->flags = SRP_RSP_FLAG_DIOVER;
9288a678bbSBryant G. Ly 			rsp->data_in_res_cnt = cpu_to_be32(residual_count);
9388a678bbSBryant G. Ly 		}
9488a678bbSBryant G. Ly 	}
9588a678bbSBryant G. Ly }
9688a678bbSBryant G. Ly 
9788a678bbSBryant G. Ly /**
9888a678bbSBryant G. Ly  * connection_broken() - Determine if the connection to the client is good
9988a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
10088a678bbSBryant G. Ly  *
10188a678bbSBryant G. Ly  * This function attempts to send a ping MAD to the client. If the call to
10288a678bbSBryant G. Ly  * queue the request returns H_CLOSED then the connection has been broken
10388a678bbSBryant G. Ly  * and the function returns TRUE.
10488a678bbSBryant G. Ly  *
10588a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
10688a678bbSBryant G. Ly  *	Interrupt or Process environment
10788a678bbSBryant G. Ly  */
10888a678bbSBryant G. Ly static bool connection_broken(struct scsi_info *vscsi)
10988a678bbSBryant G. Ly {
11088a678bbSBryant G. Ly 	struct viosrp_crq *crq;
11188a678bbSBryant G. Ly 	u64 buffer[2] = { 0, 0 };
11288a678bbSBryant G. Ly 	long h_return_code;
11388a678bbSBryant G. Ly 	bool rc = false;
11488a678bbSBryant G. Ly 
11588a678bbSBryant G. Ly 	/* create a PING crq */
11688a678bbSBryant G. Ly 	crq = (struct viosrp_crq *)&buffer;
11788a678bbSBryant G. Ly 	crq->valid = VALID_CMD_RESP_EL;
11888a678bbSBryant G. Ly 	crq->format = MESSAGE_IN_CRQ;
11988a678bbSBryant G. Ly 	crq->status = PING;
12088a678bbSBryant G. Ly 
12188a678bbSBryant G. Ly 	h_return_code = h_send_crq(vscsi->dds.unit_id,
12288a678bbSBryant G. Ly 				   cpu_to_be64(buffer[MSG_HI]),
12388a678bbSBryant G. Ly 				   cpu_to_be64(buffer[MSG_LOW]));
12488a678bbSBryant G. Ly 
12588a678bbSBryant G. Ly 	pr_debug("connection_broken: rc %ld\n", h_return_code);
12688a678bbSBryant G. Ly 
12788a678bbSBryant G. Ly 	if (h_return_code == H_CLOSED)
12888a678bbSBryant G. Ly 		rc = true;
12988a678bbSBryant G. Ly 
13088a678bbSBryant G. Ly 	return rc;
13188a678bbSBryant G. Ly }
13288a678bbSBryant G. Ly 
13388a678bbSBryant G. Ly /**
13488a678bbSBryant G. Ly  * ibmvscsis_unregister_command_q() - Helper Function-Unregister Command Queue
13588a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
13688a678bbSBryant G. Ly  *
13788a678bbSBryant G. Ly  * This function calls h_free_q then frees the interrupt bit etc.
13888a678bbSBryant G. Ly  * It must release the lock before doing so because of the time it can take
13988a678bbSBryant G. Ly  * for h_free_crq in PHYP
14088a678bbSBryant G. Ly  * NOTE: the caller must make sure that state and or flags will prevent
14188a678bbSBryant G. Ly  *	 interrupt handler from scheduling work.
14288a678bbSBryant G. Ly  * NOTE: anyone calling this function may need to set the CRQ_CLOSED flag
14388a678bbSBryant G. Ly  *	 we can't do it here, because we don't have the lock
14488a678bbSBryant G. Ly  *
14588a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
14688a678bbSBryant G. Ly  *	Process level
14788a678bbSBryant G. Ly  */
14888a678bbSBryant G. Ly static long ibmvscsis_unregister_command_q(struct scsi_info *vscsi)
14988a678bbSBryant G. Ly {
15088a678bbSBryant G. Ly 	long qrc;
15188a678bbSBryant G. Ly 	long rc = ADAPT_SUCCESS;
15288a678bbSBryant G. Ly 	int ticks = 0;
15388a678bbSBryant G. Ly 
15488a678bbSBryant G. Ly 	do {
15588a678bbSBryant G. Ly 		qrc = h_free_crq(vscsi->dds.unit_id);
15688a678bbSBryant G. Ly 		switch (qrc) {
15788a678bbSBryant G. Ly 		case H_SUCCESS:
15888a678bbSBryant G. Ly 			break;
15988a678bbSBryant G. Ly 
16088a678bbSBryant G. Ly 		case H_HARDWARE:
16188a678bbSBryant G. Ly 		case H_PARAMETER:
16288a678bbSBryant G. Ly 			dev_err(&vscsi->dev, "unregister_command_q: error from h_free_crq %ld\n",
16388a678bbSBryant G. Ly 				qrc);
16488a678bbSBryant G. Ly 			rc = ERROR;
16588a678bbSBryant G. Ly 			break;
16688a678bbSBryant G. Ly 
16788a678bbSBryant G. Ly 		case H_BUSY:
16888a678bbSBryant G. Ly 		case H_LONG_BUSY_ORDER_1_MSEC:
16988a678bbSBryant G. Ly 			/* msleep not good for small values */
17088a678bbSBryant G. Ly 			usleep_range(1000, 2000);
17188a678bbSBryant G. Ly 			ticks += 1;
17288a678bbSBryant G. Ly 			break;
17388a678bbSBryant G. Ly 		case H_LONG_BUSY_ORDER_10_MSEC:
17488a678bbSBryant G. Ly 			usleep_range(10000, 20000);
17588a678bbSBryant G. Ly 			ticks += 10;
17688a678bbSBryant G. Ly 			break;
17788a678bbSBryant G. Ly 		case H_LONG_BUSY_ORDER_100_MSEC:
17888a678bbSBryant G. Ly 			msleep(100);
17988a678bbSBryant G. Ly 			ticks += 100;
18088a678bbSBryant G. Ly 			break;
18188a678bbSBryant G. Ly 		case H_LONG_BUSY_ORDER_1_SEC:
18288a678bbSBryant G. Ly 			ssleep(1);
18388a678bbSBryant G. Ly 			ticks += 1000;
18488a678bbSBryant G. Ly 			break;
18588a678bbSBryant G. Ly 		case H_LONG_BUSY_ORDER_10_SEC:
18688a678bbSBryant G. Ly 			ssleep(10);
18788a678bbSBryant G. Ly 			ticks += 10000;
18888a678bbSBryant G. Ly 			break;
18988a678bbSBryant G. Ly 		case H_LONG_BUSY_ORDER_100_SEC:
19088a678bbSBryant G. Ly 			ssleep(100);
19188a678bbSBryant G. Ly 			ticks += 100000;
19288a678bbSBryant G. Ly 			break;
19388a678bbSBryant G. Ly 		default:
19488a678bbSBryant G. Ly 			dev_err(&vscsi->dev, "unregister_command_q: unknown error %ld from h_free_crq\n",
19588a678bbSBryant G. Ly 				qrc);
19688a678bbSBryant G. Ly 			rc = ERROR;
19788a678bbSBryant G. Ly 			break;
19888a678bbSBryant G. Ly 		}
19988a678bbSBryant G. Ly 
20088a678bbSBryant G. Ly 		/*
20188a678bbSBryant G. Ly 		 * dont wait more then 300 seconds
20288a678bbSBryant G. Ly 		 * ticks are in milliseconds more or less
20388a678bbSBryant G. Ly 		 */
20488a678bbSBryant G. Ly 		if (ticks > 300000 && qrc != H_SUCCESS) {
20588a678bbSBryant G. Ly 			rc = ERROR;
20688a678bbSBryant G. Ly 			dev_err(&vscsi->dev, "Excessive wait for h_free_crq\n");
20788a678bbSBryant G. Ly 		}
20888a678bbSBryant G. Ly 	} while (qrc != H_SUCCESS && rc == ADAPT_SUCCESS);
20988a678bbSBryant G. Ly 
21088a678bbSBryant G. Ly 	pr_debug("Freeing CRQ: phyp rc %ld, rc %ld\n", qrc, rc);
21188a678bbSBryant G. Ly 
21288a678bbSBryant G. Ly 	return rc;
21388a678bbSBryant G. Ly }
21488a678bbSBryant G. Ly 
21588a678bbSBryant G. Ly /**
21688a678bbSBryant G. Ly  * ibmvscsis_delete_client_info() - Helper function to Delete Client Info
21788a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
21888a678bbSBryant G. Ly  * @client_closed:	True if client closed its queue
21988a678bbSBryant G. Ly  *
22088a678bbSBryant G. Ly  * Deletes information specific to the client when the client goes away
22188a678bbSBryant G. Ly  *
22288a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
22388a678bbSBryant G. Ly  *	Interrupt or Process
22488a678bbSBryant G. Ly  */
22588a678bbSBryant G. Ly static void ibmvscsis_delete_client_info(struct scsi_info *vscsi,
22688a678bbSBryant G. Ly 					 bool client_closed)
22788a678bbSBryant G. Ly {
22888a678bbSBryant G. Ly 	vscsi->client_cap = 0;
22988a678bbSBryant G. Ly 
23088a678bbSBryant G. Ly 	/*
23188a678bbSBryant G. Ly 	 * Some things we don't want to clear if we're closing the queue,
23288a678bbSBryant G. Ly 	 * because some clients don't resend the host handshake when they
23388a678bbSBryant G. Ly 	 * get a transport event.
23488a678bbSBryant G. Ly 	 */
23588a678bbSBryant G. Ly 	if (client_closed)
23688a678bbSBryant G. Ly 		vscsi->client_data.os_type = 0;
23788a678bbSBryant G. Ly }
23888a678bbSBryant G. Ly 
23988a678bbSBryant G. Ly /**
24088a678bbSBryant G. Ly  * ibmvscsis_free_command_q() - Free Command Queue
24188a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
24288a678bbSBryant G. Ly  *
24388a678bbSBryant G. Ly  * This function calls unregister_command_q, then clears interrupts and
24488a678bbSBryant G. Ly  * any pending interrupt acknowledgments associated with the command q.
24588a678bbSBryant G. Ly  * It also clears memory if there is no error.
24688a678bbSBryant G. Ly  *
24788a678bbSBryant G. Ly  * PHYP did not meet the PAPR architecture so that we must give up the
24888a678bbSBryant G. Ly  * lock. This causes a timing hole regarding state change.  To close the
24988a678bbSBryant G. Ly  * hole this routine does accounting on any change that occurred during
25088a678bbSBryant G. Ly  * the time the lock is not held.
25188a678bbSBryant G. Ly  * NOTE: must give up and then acquire the interrupt lock, the caller must
25288a678bbSBryant G. Ly  *	 make sure that state and or flags will prevent interrupt handler from
25388a678bbSBryant G. Ly  *	 scheduling work.
25488a678bbSBryant G. Ly  *
25588a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
25688a678bbSBryant G. Ly  *	Process level, interrupt lock is held
25788a678bbSBryant G. Ly  */
25888a678bbSBryant G. Ly static long ibmvscsis_free_command_q(struct scsi_info *vscsi)
25988a678bbSBryant G. Ly {
26088a678bbSBryant G. Ly 	int bytes;
26188a678bbSBryant G. Ly 	u32 flags_under_lock;
26288a678bbSBryant G. Ly 	u16 state_under_lock;
26388a678bbSBryant G. Ly 	long rc = ADAPT_SUCCESS;
26488a678bbSBryant G. Ly 
26588a678bbSBryant G. Ly 	if (!(vscsi->flags & CRQ_CLOSED)) {
26688a678bbSBryant G. Ly 		vio_disable_interrupts(vscsi->dma_dev);
26788a678bbSBryant G. Ly 
26888a678bbSBryant G. Ly 		state_under_lock = vscsi->new_state;
26988a678bbSBryant G. Ly 		flags_under_lock = vscsi->flags;
27088a678bbSBryant G. Ly 		vscsi->phyp_acr_state = 0;
27188a678bbSBryant G. Ly 		vscsi->phyp_acr_flags = 0;
27288a678bbSBryant G. Ly 
27388a678bbSBryant G. Ly 		spin_unlock_bh(&vscsi->intr_lock);
27488a678bbSBryant G. Ly 		rc = ibmvscsis_unregister_command_q(vscsi);
27588a678bbSBryant G. Ly 		spin_lock_bh(&vscsi->intr_lock);
27688a678bbSBryant G. Ly 
27788a678bbSBryant G. Ly 		if (state_under_lock != vscsi->new_state)
27888a678bbSBryant G. Ly 			vscsi->phyp_acr_state = vscsi->new_state;
27988a678bbSBryant G. Ly 
28088a678bbSBryant G. Ly 		vscsi->phyp_acr_flags = ((~flags_under_lock) & vscsi->flags);
28188a678bbSBryant G. Ly 
28288a678bbSBryant G. Ly 		if (rc == ADAPT_SUCCESS) {
28388a678bbSBryant G. Ly 			bytes = vscsi->cmd_q.size * PAGE_SIZE;
28488a678bbSBryant G. Ly 			memset(vscsi->cmd_q.base_addr, 0, bytes);
28588a678bbSBryant G. Ly 			vscsi->cmd_q.index = 0;
28688a678bbSBryant G. Ly 			vscsi->flags |= CRQ_CLOSED;
28788a678bbSBryant G. Ly 
28888a678bbSBryant G. Ly 			ibmvscsis_delete_client_info(vscsi, false);
28988a678bbSBryant G. Ly 		}
29088a678bbSBryant G. Ly 
29188a678bbSBryant G. Ly 		pr_debug("free_command_q: flags 0x%x, state 0x%hx, acr_flags 0x%x, acr_state 0x%hx\n",
29288a678bbSBryant G. Ly 			 vscsi->flags, vscsi->state, vscsi->phyp_acr_flags,
29388a678bbSBryant G. Ly 			 vscsi->phyp_acr_state);
29488a678bbSBryant G. Ly 	}
29588a678bbSBryant G. Ly 	return rc;
29688a678bbSBryant G. Ly }
29788a678bbSBryant G. Ly 
29888a678bbSBryant G. Ly /**
29988a678bbSBryant G. Ly  * ibmvscsis_cmd_q_dequeue() - Get valid Command element
30088a678bbSBryant G. Ly  * @mask:	Mask to use in case index wraps
30188a678bbSBryant G. Ly  * @current_index:	Current index into command queue
30288a678bbSBryant G. Ly  * @base_addr:	Pointer to start of command queue
30388a678bbSBryant G. Ly  *
30488a678bbSBryant G. Ly  * Returns a pointer to a valid command element or NULL, if the command
30588a678bbSBryant G. Ly  * queue is empty
30688a678bbSBryant G. Ly  *
30788a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
30888a678bbSBryant G. Ly  *	Interrupt environment, interrupt lock held
30988a678bbSBryant G. Ly  */
31088a678bbSBryant G. Ly static struct viosrp_crq *ibmvscsis_cmd_q_dequeue(uint mask,
31188a678bbSBryant G. Ly 						  uint *current_index,
31288a678bbSBryant G. Ly 						  struct viosrp_crq *base_addr)
31388a678bbSBryant G. Ly {
31488a678bbSBryant G. Ly 	struct viosrp_crq *ptr;
31588a678bbSBryant G. Ly 
31688a678bbSBryant G. Ly 	ptr = base_addr + *current_index;
31788a678bbSBryant G. Ly 
31888a678bbSBryant G. Ly 	if (ptr->valid) {
31988a678bbSBryant G. Ly 		*current_index = (*current_index + 1) & mask;
32088a678bbSBryant G. Ly 		dma_rmb();
32188a678bbSBryant G. Ly 	} else {
32288a678bbSBryant G. Ly 		ptr = NULL;
32388a678bbSBryant G. Ly 	}
32488a678bbSBryant G. Ly 
32588a678bbSBryant G. Ly 	return ptr;
32688a678bbSBryant G. Ly }
32788a678bbSBryant G. Ly 
32888a678bbSBryant G. Ly /**
32988a678bbSBryant G. Ly  * ibmvscsis_send_init_message() - send initialize message to the client
33088a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
33188a678bbSBryant G. Ly  * @format:	Which Init Message format to send
33288a678bbSBryant G. Ly  *
33388a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
33488a678bbSBryant G. Ly  *	Interrupt environment interrupt lock held
33588a678bbSBryant G. Ly  */
33688a678bbSBryant G. Ly static long ibmvscsis_send_init_message(struct scsi_info *vscsi, u8 format)
33788a678bbSBryant G. Ly {
33888a678bbSBryant G. Ly 	struct viosrp_crq *crq;
33988a678bbSBryant G. Ly 	u64 buffer[2] = { 0, 0 };
34088a678bbSBryant G. Ly 	long rc;
34188a678bbSBryant G. Ly 
34288a678bbSBryant G. Ly 	crq = (struct viosrp_crq *)&buffer;
34388a678bbSBryant G. Ly 	crq->valid = VALID_INIT_MSG;
34488a678bbSBryant G. Ly 	crq->format = format;
34588a678bbSBryant G. Ly 	rc = h_send_crq(vscsi->dds.unit_id, cpu_to_be64(buffer[MSG_HI]),
34688a678bbSBryant G. Ly 			cpu_to_be64(buffer[MSG_LOW]));
34788a678bbSBryant G. Ly 
34888a678bbSBryant G. Ly 	return rc;
34988a678bbSBryant G. Ly }
35088a678bbSBryant G. Ly 
35188a678bbSBryant G. Ly /**
35288a678bbSBryant G. Ly  * ibmvscsis_check_init_msg() - Check init message valid
35388a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
35488a678bbSBryant G. Ly  * @format:	Pointer to return format of Init Message, if any.
35588a678bbSBryant G. Ly  *		Set to UNUSED_FORMAT if no Init Message in queue.
35688a678bbSBryant G. Ly  *
35788a678bbSBryant G. Ly  * Checks if an initialize message was queued by the initiatior
35888a678bbSBryant G. Ly  * after the queue was created and before the interrupt was enabled.
35988a678bbSBryant G. Ly  *
36088a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
36188a678bbSBryant G. Ly  *	Process level only, interrupt lock held
36288a678bbSBryant G. Ly  */
36388a678bbSBryant G. Ly static long ibmvscsis_check_init_msg(struct scsi_info *vscsi, uint *format)
36488a678bbSBryant G. Ly {
36588a678bbSBryant G. Ly 	struct viosrp_crq *crq;
36688a678bbSBryant G. Ly 	long rc = ADAPT_SUCCESS;
36788a678bbSBryant G. Ly 
36888a678bbSBryant G. Ly 	crq = ibmvscsis_cmd_q_dequeue(vscsi->cmd_q.mask, &vscsi->cmd_q.index,
36988a678bbSBryant G. Ly 				      vscsi->cmd_q.base_addr);
37088a678bbSBryant G. Ly 	if (!crq) {
37188a678bbSBryant G. Ly 		*format = (uint)UNUSED_FORMAT;
37288a678bbSBryant G. Ly 	} else if (crq->valid == VALID_INIT_MSG && crq->format == INIT_MSG) {
37388a678bbSBryant G. Ly 		*format = (uint)INIT_MSG;
37488a678bbSBryant G. Ly 		crq->valid = INVALIDATE_CMD_RESP_EL;
37588a678bbSBryant G. Ly 		dma_rmb();
37688a678bbSBryant G. Ly 
37788a678bbSBryant G. Ly 		/*
37888a678bbSBryant G. Ly 		 * the caller has ensured no initialize message was
37988a678bbSBryant G. Ly 		 * sent after the queue was
38088a678bbSBryant G. Ly 		 * created so there should be no other message on the queue.
38188a678bbSBryant G. Ly 		 */
38288a678bbSBryant G. Ly 		crq = ibmvscsis_cmd_q_dequeue(vscsi->cmd_q.mask,
38388a678bbSBryant G. Ly 					      &vscsi->cmd_q.index,
38488a678bbSBryant G. Ly 					      vscsi->cmd_q.base_addr);
38588a678bbSBryant G. Ly 		if (crq) {
38688a678bbSBryant G. Ly 			*format = (uint)(crq->format);
38788a678bbSBryant G. Ly 			rc = ERROR;
38888a678bbSBryant G. Ly 			crq->valid = INVALIDATE_CMD_RESP_EL;
38988a678bbSBryant G. Ly 			dma_rmb();
39088a678bbSBryant G. Ly 		}
39188a678bbSBryant G. Ly 	} else {
39288a678bbSBryant G. Ly 		*format = (uint)(crq->format);
39388a678bbSBryant G. Ly 		rc = ERROR;
39488a678bbSBryant G. Ly 		crq->valid = INVALIDATE_CMD_RESP_EL;
39588a678bbSBryant G. Ly 		dma_rmb();
39688a678bbSBryant G. Ly 	}
39788a678bbSBryant G. Ly 
39888a678bbSBryant G. Ly 	return rc;
39988a678bbSBryant G. Ly }
40088a678bbSBryant G. Ly 
40188a678bbSBryant G. Ly /**
40288a678bbSBryant G. Ly  * ibmvscsis_disconnect() - Helper function to disconnect
40388a678bbSBryant G. Ly  * @work:	Pointer to work_struct, gives access to our adapter structure
40488a678bbSBryant G. Ly  *
40588a678bbSBryant G. Ly  * An error has occurred or the driver received a Transport event,
40688a678bbSBryant G. Ly  * and the driver is requesting that the command queue be de-registered
40788a678bbSBryant G. Ly  * in a safe manner. If there is no outstanding I/O then we can stop the
40888a678bbSBryant G. Ly  * queue. If we are restarting the queue it will be reflected in the
40988a678bbSBryant G. Ly  * the state of the adapter.
41088a678bbSBryant G. Ly  *
41188a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
41288a678bbSBryant G. Ly  *	Process environment
41388a678bbSBryant G. Ly  */
41488a678bbSBryant G. Ly static void ibmvscsis_disconnect(struct work_struct *work)
41588a678bbSBryant G. Ly {
41688a678bbSBryant G. Ly 	struct scsi_info *vscsi = container_of(work, struct scsi_info,
41788a678bbSBryant G. Ly 					       proc_work);
41888a678bbSBryant G. Ly 	u16 new_state;
41988a678bbSBryant G. Ly 	bool wait_idle = false;
42088a678bbSBryant G. Ly 
42188a678bbSBryant G. Ly 	spin_lock_bh(&vscsi->intr_lock);
42288a678bbSBryant G. Ly 	new_state = vscsi->new_state;
42388a678bbSBryant G. Ly 	vscsi->new_state = 0;
42488a678bbSBryant G. Ly 
42588a678bbSBryant G. Ly 	pr_debug("disconnect: flags 0x%x, state 0x%hx\n", vscsi->flags,
42688a678bbSBryant G. Ly 		 vscsi->state);
42788a678bbSBryant G. Ly 
42888a678bbSBryant G. Ly 	/*
42988a678bbSBryant G. Ly 	 * check which state we are in and see if we
43088a678bbSBryant G. Ly 	 * should transitition to the new state
43188a678bbSBryant G. Ly 	 */
43288a678bbSBryant G. Ly 	switch (vscsi->state) {
43388a678bbSBryant G. Ly 	/* Should never be called while in this state. */
43488a678bbSBryant G. Ly 	case NO_QUEUE:
43588a678bbSBryant G. Ly 	/*
43688a678bbSBryant G. Ly 	 * Can never transition from this state;
43788a678bbSBryant G. Ly 	 * igonore errors and logout.
43888a678bbSBryant G. Ly 	 */
43988a678bbSBryant G. Ly 	case UNCONFIGURING:
44088a678bbSBryant G. Ly 		break;
44188a678bbSBryant G. Ly 
44288a678bbSBryant G. Ly 	/* can transition from this state to UNCONFIGURING */
44388a678bbSBryant G. Ly 	case ERR_DISCONNECT:
44488a678bbSBryant G. Ly 		if (new_state == UNCONFIGURING)
44588a678bbSBryant G. Ly 			vscsi->state = new_state;
44688a678bbSBryant G. Ly 		break;
44788a678bbSBryant G. Ly 
44888a678bbSBryant G. Ly 	/*
44988a678bbSBryant G. Ly 	 * Can transition from this state to to unconfiguring
45088a678bbSBryant G. Ly 	 * or err disconnect.
45188a678bbSBryant G. Ly 	 */
45288a678bbSBryant G. Ly 	case ERR_DISCONNECT_RECONNECT:
45388a678bbSBryant G. Ly 		switch (new_state) {
45488a678bbSBryant G. Ly 		case UNCONFIGURING:
45588a678bbSBryant G. Ly 		case ERR_DISCONNECT:
45688a678bbSBryant G. Ly 			vscsi->state = new_state;
45788a678bbSBryant G. Ly 			break;
45888a678bbSBryant G. Ly 
45988a678bbSBryant G. Ly 		case WAIT_IDLE:
46088a678bbSBryant G. Ly 			break;
46188a678bbSBryant G. Ly 		default:
46288a678bbSBryant G. Ly 			break;
46388a678bbSBryant G. Ly 		}
46488a678bbSBryant G. Ly 		break;
46588a678bbSBryant G. Ly 
46688a678bbSBryant G. Ly 	/* can transition from this state to UNCONFIGURING */
46788a678bbSBryant G. Ly 	case ERR_DISCONNECTED:
46888a678bbSBryant G. Ly 		if (new_state == UNCONFIGURING)
46988a678bbSBryant G. Ly 			vscsi->state = new_state;
47088a678bbSBryant G. Ly 		break;
47188a678bbSBryant G. Ly 
47288a678bbSBryant G. Ly 	case WAIT_ENABLED:
47388a678bbSBryant G. Ly 		switch (new_state) {
4748bf11557SMichael Cyr 		case UNCONFIGURING:
4758bf11557SMichael Cyr 			vscsi->state = new_state;
4768bf11557SMichael Cyr 			vscsi->flags |= RESPONSE_Q_DOWN;
4778bf11557SMichael Cyr 			vscsi->flags &= ~(SCHEDULE_DISCONNECT |
4788bf11557SMichael Cyr 					  DISCONNECT_SCHEDULED);
4798bf11557SMichael Cyr 			dma_rmb();
4808bf11557SMichael Cyr 			if (vscsi->flags & CFG_SLEEPING) {
4818bf11557SMichael Cyr 				vscsi->flags &= ~CFG_SLEEPING;
4828bf11557SMichael Cyr 				complete(&vscsi->unconfig);
4838bf11557SMichael Cyr 			}
4848bf11557SMichael Cyr 			break;
4858bf11557SMichael Cyr 
48688a678bbSBryant G. Ly 		/* should never happen */
487c9b3379fSMichael Cyr 		case ERR_DISCONNECT:
488c9b3379fSMichael Cyr 		case ERR_DISCONNECT_RECONNECT:
48988a678bbSBryant G. Ly 		case WAIT_IDLE:
49088a678bbSBryant G. Ly 			dev_err(&vscsi->dev, "disconnect: invalid state %d for WAIT_IDLE\n",
49188a678bbSBryant G. Ly 				vscsi->state);
49288a678bbSBryant G. Ly 			break;
49388a678bbSBryant G. Ly 		}
49488a678bbSBryant G. Ly 		break;
49588a678bbSBryant G. Ly 
49688a678bbSBryant G. Ly 	case WAIT_IDLE:
49788a678bbSBryant G. Ly 		switch (new_state) {
4988bf11557SMichael Cyr 		case UNCONFIGURING:
4998bf11557SMichael Cyr 			vscsi->flags |= RESPONSE_Q_DOWN;
5008bf11557SMichael Cyr 			vscsi->state = new_state;
5018bf11557SMichael Cyr 			vscsi->flags &= ~(SCHEDULE_DISCONNECT |
5028bf11557SMichael Cyr 					  DISCONNECT_SCHEDULED);
5038bf11557SMichael Cyr 			ibmvscsis_free_command_q(vscsi);
5048bf11557SMichael Cyr 			break;
50588a678bbSBryant G. Ly 		case ERR_DISCONNECT:
50688a678bbSBryant G. Ly 		case ERR_DISCONNECT_RECONNECT:
50788a678bbSBryant G. Ly 			vscsi->state = new_state;
50888a678bbSBryant G. Ly 			break;
50988a678bbSBryant G. Ly 		}
51088a678bbSBryant G. Ly 		break;
51188a678bbSBryant G. Ly 
51288a678bbSBryant G. Ly 	/*
51388a678bbSBryant G. Ly 	 * Initiator has not done a successful srp login
51488a678bbSBryant G. Ly 	 * or has done a successful srp logout ( adapter was not
51588a678bbSBryant G. Ly 	 * busy). In the first case there can be responses queued
51688a678bbSBryant G. Ly 	 * waiting for space on the initiators response queue (MAD)
51788a678bbSBryant G. Ly 	 * The second case the adapter is idle. Assume the worse case,
51888a678bbSBryant G. Ly 	 * i.e. the second case.
51988a678bbSBryant G. Ly 	 */
52088a678bbSBryant G. Ly 	case WAIT_CONNECTION:
52188a678bbSBryant G. Ly 	case CONNECTED:
52288a678bbSBryant G. Ly 	case SRP_PROCESSING:
52388a678bbSBryant G. Ly 		wait_idle = true;
52488a678bbSBryant G. Ly 		vscsi->state = new_state;
52588a678bbSBryant G. Ly 		break;
52688a678bbSBryant G. Ly 
52788a678bbSBryant G. Ly 	/* can transition from this state to UNCONFIGURING */
52888a678bbSBryant G. Ly 	case UNDEFINED:
52988a678bbSBryant G. Ly 		if (new_state == UNCONFIGURING)
53088a678bbSBryant G. Ly 			vscsi->state = new_state;
53188a678bbSBryant G. Ly 		break;
53288a678bbSBryant G. Ly 	default:
53388a678bbSBryant G. Ly 		break;
53488a678bbSBryant G. Ly 	}
53588a678bbSBryant G. Ly 
53688a678bbSBryant G. Ly 	if (wait_idle) {
53788a678bbSBryant G. Ly 		pr_debug("disconnect start wait, active %d, sched %d\n",
53888a678bbSBryant G. Ly 			 (int)list_empty(&vscsi->active_q),
53988a678bbSBryant G. Ly 			 (int)list_empty(&vscsi->schedule_q));
54088a678bbSBryant G. Ly 		if (!list_empty(&vscsi->active_q) ||
54188a678bbSBryant G. Ly 		    !list_empty(&vscsi->schedule_q)) {
54288a678bbSBryant G. Ly 			vscsi->flags |= WAIT_FOR_IDLE;
54388a678bbSBryant G. Ly 			pr_debug("disconnect flags 0x%x\n", vscsi->flags);
54488a678bbSBryant G. Ly 			/*
54588a678bbSBryant G. Ly 			 * This routine is can not be called with the interrupt
54688a678bbSBryant G. Ly 			 * lock held.
54788a678bbSBryant G. Ly 			 */
54888a678bbSBryant G. Ly 			spin_unlock_bh(&vscsi->intr_lock);
54988a678bbSBryant G. Ly 			wait_for_completion(&vscsi->wait_idle);
55088a678bbSBryant G. Ly 			spin_lock_bh(&vscsi->intr_lock);
55188a678bbSBryant G. Ly 		}
55288a678bbSBryant G. Ly 		pr_debug("disconnect stop wait\n");
55388a678bbSBryant G. Ly 
55488a678bbSBryant G. Ly 		ibmvscsis_adapter_idle(vscsi);
55588a678bbSBryant G. Ly 	}
55688a678bbSBryant G. Ly 
55788a678bbSBryant G. Ly 	spin_unlock_bh(&vscsi->intr_lock);
55888a678bbSBryant G. Ly }
55988a678bbSBryant G. Ly 
56088a678bbSBryant G. Ly /**
56188a678bbSBryant G. Ly  * ibmvscsis_post_disconnect() - Schedule the disconnect
56288a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
56388a678bbSBryant G. Ly  * @new_state:	State to move to after disconnecting
56488a678bbSBryant G. Ly  * @flag_bits:	Flags to turn on in adapter structure
56588a678bbSBryant G. Ly  *
56688a678bbSBryant G. Ly  * If it's already been scheduled, then see if we need to "upgrade"
56788a678bbSBryant G. Ly  * the new state (if the one passed in is more "severe" than the
56888a678bbSBryant G. Ly  * previous one).
56988a678bbSBryant G. Ly  *
57088a678bbSBryant G. Ly  * PRECONDITION:
57188a678bbSBryant G. Ly  *	interrupt lock is held
57288a678bbSBryant G. Ly  */
57388a678bbSBryant G. Ly static void ibmvscsis_post_disconnect(struct scsi_info *vscsi, uint new_state,
57488a678bbSBryant G. Ly 				      uint flag_bits)
57588a678bbSBryant G. Ly {
57688a678bbSBryant G. Ly 	uint state;
57788a678bbSBryant G. Ly 
57888a678bbSBryant G. Ly 	/* check the validity of the new state */
57988a678bbSBryant G. Ly 	switch (new_state) {
58088a678bbSBryant G. Ly 	case UNCONFIGURING:
58188a678bbSBryant G. Ly 	case ERR_DISCONNECT:
58288a678bbSBryant G. Ly 	case ERR_DISCONNECT_RECONNECT:
58388a678bbSBryant G. Ly 	case WAIT_IDLE:
58488a678bbSBryant G. Ly 		break;
58588a678bbSBryant G. Ly 
58688a678bbSBryant G. Ly 	default:
58788a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "post_disconnect: Invalid new state %d\n",
58888a678bbSBryant G. Ly 			new_state);
58988a678bbSBryant G. Ly 		return;
59088a678bbSBryant G. Ly 	}
59188a678bbSBryant G. Ly 
59288a678bbSBryant G. Ly 	vscsi->flags |= flag_bits;
59388a678bbSBryant G. Ly 
59488a678bbSBryant G. Ly 	pr_debug("post_disconnect: new_state 0x%x, flag_bits 0x%x, vscsi->flags 0x%x, state %hx\n",
59588a678bbSBryant G. Ly 		 new_state, flag_bits, vscsi->flags, vscsi->state);
59688a678bbSBryant G. Ly 
59788a678bbSBryant G. Ly 	if (!(vscsi->flags & (DISCONNECT_SCHEDULED | SCHEDULE_DISCONNECT))) {
59888a678bbSBryant G. Ly 		vscsi->flags |= SCHEDULE_DISCONNECT;
59988a678bbSBryant G. Ly 		vscsi->new_state = new_state;
60088a678bbSBryant G. Ly 
60188a678bbSBryant G. Ly 		INIT_WORK(&vscsi->proc_work, ibmvscsis_disconnect);
60288a678bbSBryant G. Ly 		(void)queue_work(vscsi->work_q, &vscsi->proc_work);
60388a678bbSBryant G. Ly 	} else {
60488a678bbSBryant G. Ly 		if (vscsi->new_state)
60588a678bbSBryant G. Ly 			state = vscsi->new_state;
60688a678bbSBryant G. Ly 		else
60788a678bbSBryant G. Ly 			state = vscsi->state;
60888a678bbSBryant G. Ly 
60988a678bbSBryant G. Ly 		switch (state) {
61088a678bbSBryant G. Ly 		case NO_QUEUE:
61188a678bbSBryant G. Ly 		case UNCONFIGURING:
61288a678bbSBryant G. Ly 			break;
61388a678bbSBryant G. Ly 
61488a678bbSBryant G. Ly 		case ERR_DISCONNECTED:
61588a678bbSBryant G. Ly 		case ERR_DISCONNECT:
61688a678bbSBryant G. Ly 		case UNDEFINED:
61788a678bbSBryant G. Ly 			if (new_state == UNCONFIGURING)
61888a678bbSBryant G. Ly 				vscsi->new_state = new_state;
61988a678bbSBryant G. Ly 			break;
62088a678bbSBryant G. Ly 
62188a678bbSBryant G. Ly 		case ERR_DISCONNECT_RECONNECT:
62288a678bbSBryant G. Ly 			switch (new_state) {
62388a678bbSBryant G. Ly 			case UNCONFIGURING:
62488a678bbSBryant G. Ly 			case ERR_DISCONNECT:
62588a678bbSBryant G. Ly 				vscsi->new_state = new_state;
62688a678bbSBryant G. Ly 				break;
62788a678bbSBryant G. Ly 			default:
62888a678bbSBryant G. Ly 				break;
62988a678bbSBryant G. Ly 			}
63088a678bbSBryant G. Ly 			break;
63188a678bbSBryant G. Ly 
63288a678bbSBryant G. Ly 		case WAIT_ENABLED:
63388a678bbSBryant G. Ly 		case WAIT_IDLE:
63488a678bbSBryant G. Ly 		case WAIT_CONNECTION:
63588a678bbSBryant G. Ly 		case CONNECTED:
63688a678bbSBryant G. Ly 		case SRP_PROCESSING:
63788a678bbSBryant G. Ly 			vscsi->new_state = new_state;
63888a678bbSBryant G. Ly 			break;
63988a678bbSBryant G. Ly 
64088a678bbSBryant G. Ly 		default:
64188a678bbSBryant G. Ly 			break;
64288a678bbSBryant G. Ly 		}
64388a678bbSBryant G. Ly 	}
64488a678bbSBryant G. Ly 
64588a678bbSBryant G. Ly 	pr_debug("Leaving post_disconnect: flags 0x%x, new_state 0x%x\n",
64688a678bbSBryant G. Ly 		 vscsi->flags, vscsi->new_state);
64788a678bbSBryant G. Ly }
64888a678bbSBryant G. Ly 
64988a678bbSBryant G. Ly /**
65079fac9c9SMichael Cyr  * ibmvscsis_handle_init_compl_msg() - Respond to an Init Complete Message
65179fac9c9SMichael Cyr  * @vscsi:	Pointer to our adapter structure
65279fac9c9SMichael Cyr  *
65379fac9c9SMichael Cyr  * Must be called with interrupt lock held.
65479fac9c9SMichael Cyr  */
65579fac9c9SMichael Cyr static long ibmvscsis_handle_init_compl_msg(struct scsi_info *vscsi)
65679fac9c9SMichael Cyr {
65779fac9c9SMichael Cyr 	long rc = ADAPT_SUCCESS;
65879fac9c9SMichael Cyr 
65979fac9c9SMichael Cyr 	switch (vscsi->state) {
66079fac9c9SMichael Cyr 	case NO_QUEUE:
66179fac9c9SMichael Cyr 	case ERR_DISCONNECT:
66279fac9c9SMichael Cyr 	case ERR_DISCONNECT_RECONNECT:
66379fac9c9SMichael Cyr 	case ERR_DISCONNECTED:
66479fac9c9SMichael Cyr 	case UNCONFIGURING:
66579fac9c9SMichael Cyr 	case UNDEFINED:
66679fac9c9SMichael Cyr 		rc = ERROR;
66779fac9c9SMichael Cyr 		break;
66879fac9c9SMichael Cyr 
66979fac9c9SMichael Cyr 	case WAIT_CONNECTION:
67079fac9c9SMichael Cyr 		vscsi->state = CONNECTED;
67179fac9c9SMichael Cyr 		break;
67279fac9c9SMichael Cyr 
67379fac9c9SMichael Cyr 	case WAIT_IDLE:
67479fac9c9SMichael Cyr 	case SRP_PROCESSING:
67579fac9c9SMichael Cyr 	case CONNECTED:
67679fac9c9SMichael Cyr 	case WAIT_ENABLED:
67779fac9c9SMichael Cyr 	default:
67879fac9c9SMichael Cyr 		rc = ERROR;
67979fac9c9SMichael Cyr 		dev_err(&vscsi->dev, "init_msg: invalid state %d to get init compl msg\n",
68079fac9c9SMichael Cyr 			vscsi->state);
68179fac9c9SMichael Cyr 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
68279fac9c9SMichael Cyr 		break;
68379fac9c9SMichael Cyr 	}
68479fac9c9SMichael Cyr 
68579fac9c9SMichael Cyr 	return rc;
68679fac9c9SMichael Cyr }
68779fac9c9SMichael Cyr 
68879fac9c9SMichael Cyr /**
68979fac9c9SMichael Cyr  * ibmvscsis_handle_init_msg() - Respond to an Init Message
69079fac9c9SMichael Cyr  * @vscsi:	Pointer to our adapter structure
69179fac9c9SMichael Cyr  *
69279fac9c9SMichael Cyr  * Must be called with interrupt lock held.
69379fac9c9SMichael Cyr  */
69479fac9c9SMichael Cyr static long ibmvscsis_handle_init_msg(struct scsi_info *vscsi)
69579fac9c9SMichael Cyr {
69679fac9c9SMichael Cyr 	long rc = ADAPT_SUCCESS;
69779fac9c9SMichael Cyr 
69879fac9c9SMichael Cyr 	switch (vscsi->state) {
69979fac9c9SMichael Cyr 	case WAIT_CONNECTION:
70079fac9c9SMichael Cyr 		rc = ibmvscsis_send_init_message(vscsi, INIT_COMPLETE_MSG);
70179fac9c9SMichael Cyr 		switch (rc) {
70279fac9c9SMichael Cyr 		case H_SUCCESS:
70379fac9c9SMichael Cyr 			vscsi->state = CONNECTED;
70479fac9c9SMichael Cyr 			break;
70579fac9c9SMichael Cyr 
70679fac9c9SMichael Cyr 		case H_PARAMETER:
70779fac9c9SMichael Cyr 			dev_err(&vscsi->dev, "init_msg: failed to send, rc %ld\n",
70879fac9c9SMichael Cyr 				rc);
70979fac9c9SMichael Cyr 			ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
71079fac9c9SMichael Cyr 			break;
71179fac9c9SMichael Cyr 
71279fac9c9SMichael Cyr 		case H_DROPPED:
71379fac9c9SMichael Cyr 			dev_err(&vscsi->dev, "init_msg: failed to send, rc %ld\n",
71479fac9c9SMichael Cyr 				rc);
71579fac9c9SMichael Cyr 			rc = ERROR;
71679fac9c9SMichael Cyr 			ibmvscsis_post_disconnect(vscsi,
71779fac9c9SMichael Cyr 						  ERR_DISCONNECT_RECONNECT, 0);
71879fac9c9SMichael Cyr 			break;
71979fac9c9SMichael Cyr 
72079fac9c9SMichael Cyr 		case H_CLOSED:
72179fac9c9SMichael Cyr 			pr_warn("init_msg: failed to send, rc %ld\n", rc);
72279fac9c9SMichael Cyr 			rc = 0;
72379fac9c9SMichael Cyr 			break;
72479fac9c9SMichael Cyr 		}
72579fac9c9SMichael Cyr 		break;
72679fac9c9SMichael Cyr 
72779fac9c9SMichael Cyr 	case UNDEFINED:
72879fac9c9SMichael Cyr 		rc = ERROR;
72979fac9c9SMichael Cyr 		break;
73079fac9c9SMichael Cyr 
73179fac9c9SMichael Cyr 	case UNCONFIGURING:
73279fac9c9SMichael Cyr 		break;
73379fac9c9SMichael Cyr 
734c9b3379fSMichael Cyr 	case WAIT_ENABLED:
73579fac9c9SMichael Cyr 	case CONNECTED:
73679fac9c9SMichael Cyr 	case SRP_PROCESSING:
73779fac9c9SMichael Cyr 	case WAIT_IDLE:
73879fac9c9SMichael Cyr 	case NO_QUEUE:
73979fac9c9SMichael Cyr 	case ERR_DISCONNECT:
74079fac9c9SMichael Cyr 	case ERR_DISCONNECT_RECONNECT:
74179fac9c9SMichael Cyr 	case ERR_DISCONNECTED:
74279fac9c9SMichael Cyr 	default:
74379fac9c9SMichael Cyr 		rc = ERROR;
74479fac9c9SMichael Cyr 		dev_err(&vscsi->dev, "init_msg: invalid state %d to get init msg\n",
74579fac9c9SMichael Cyr 			vscsi->state);
74679fac9c9SMichael Cyr 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
74779fac9c9SMichael Cyr 		break;
74879fac9c9SMichael Cyr 	}
74979fac9c9SMichael Cyr 
75079fac9c9SMichael Cyr 	return rc;
75179fac9c9SMichael Cyr }
75279fac9c9SMichael Cyr 
75379fac9c9SMichael Cyr /**
75479fac9c9SMichael Cyr  * ibmvscsis_init_msg() - Respond to an init message
75579fac9c9SMichael Cyr  * @vscsi:	Pointer to our adapter structure
75679fac9c9SMichael Cyr  * @crq:	Pointer to CRQ element containing the Init Message
75779fac9c9SMichael Cyr  *
75879fac9c9SMichael Cyr  * EXECUTION ENVIRONMENT:
75979fac9c9SMichael Cyr  *	Interrupt, interrupt lock held
76079fac9c9SMichael Cyr  */
76179fac9c9SMichael Cyr static long ibmvscsis_init_msg(struct scsi_info *vscsi, struct viosrp_crq *crq)
76279fac9c9SMichael Cyr {
76379fac9c9SMichael Cyr 	long rc = ADAPT_SUCCESS;
76479fac9c9SMichael Cyr 
76579fac9c9SMichael Cyr 	pr_debug("init_msg: state 0x%hx\n", vscsi->state);
76679fac9c9SMichael Cyr 
76779fac9c9SMichael Cyr 	rc = h_vioctl(vscsi->dds.unit_id, H_GET_PARTNER_INFO,
76879fac9c9SMichael Cyr 		      (u64)vscsi->map_ioba | ((u64)PAGE_SIZE << 32), 0, 0, 0,
76979fac9c9SMichael Cyr 		      0);
77079fac9c9SMichael Cyr 	if (rc == H_SUCCESS) {
77179fac9c9SMichael Cyr 		vscsi->client_data.partition_number =
77279fac9c9SMichael Cyr 			be64_to_cpu(*(u64 *)vscsi->map_buf);
77379fac9c9SMichael Cyr 		pr_debug("init_msg, part num %d\n",
77479fac9c9SMichael Cyr 			 vscsi->client_data.partition_number);
77579fac9c9SMichael Cyr 	} else {
77679fac9c9SMichael Cyr 		pr_debug("init_msg h_vioctl rc %ld\n", rc);
77779fac9c9SMichael Cyr 		rc = ADAPT_SUCCESS;
77879fac9c9SMichael Cyr 	}
77979fac9c9SMichael Cyr 
78079fac9c9SMichael Cyr 	if (crq->format == INIT_MSG) {
78179fac9c9SMichael Cyr 		rc = ibmvscsis_handle_init_msg(vscsi);
78279fac9c9SMichael Cyr 	} else if (crq->format == INIT_COMPLETE_MSG) {
78379fac9c9SMichael Cyr 		rc = ibmvscsis_handle_init_compl_msg(vscsi);
78479fac9c9SMichael Cyr 	} else {
78579fac9c9SMichael Cyr 		rc = ERROR;
78679fac9c9SMichael Cyr 		dev_err(&vscsi->dev, "init_msg: invalid format %d\n",
78779fac9c9SMichael Cyr 			(uint)crq->format);
78879fac9c9SMichael Cyr 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
78979fac9c9SMichael Cyr 	}
79079fac9c9SMichael Cyr 
79179fac9c9SMichael Cyr 	return rc;
79279fac9c9SMichael Cyr }
79379fac9c9SMichael Cyr 
79479fac9c9SMichael Cyr /**
79579fac9c9SMichael Cyr  * ibmvscsis_establish_new_q() - Establish new CRQ queue
79679fac9c9SMichael Cyr  * @vscsi:	Pointer to our adapter structure
79779fac9c9SMichael Cyr  *
79879fac9c9SMichael Cyr  * Must be called with interrupt lock held.
79979fac9c9SMichael Cyr  */
800c9b3379fSMichael Cyr static long ibmvscsis_establish_new_q(struct scsi_info *vscsi)
80179fac9c9SMichael Cyr {
80279fac9c9SMichael Cyr 	long rc = ADAPT_SUCCESS;
80379fac9c9SMichael Cyr 	uint format;
80479fac9c9SMichael Cyr 
80579fac9c9SMichael Cyr 	vscsi->flags &= PRESERVE_FLAG_FIELDS;
80679fac9c9SMichael Cyr 	vscsi->rsp_q_timer.timer_pops = 0;
80779fac9c9SMichael Cyr 	vscsi->debit = 0;
80879fac9c9SMichael Cyr 	vscsi->credit = 0;
80979fac9c9SMichael Cyr 
81079fac9c9SMichael Cyr 	rc = vio_enable_interrupts(vscsi->dma_dev);
81179fac9c9SMichael Cyr 	if (rc) {
812c9b3379fSMichael Cyr 		pr_warn("establish_new_q: failed to enable interrupts, rc %ld\n",
81379fac9c9SMichael Cyr 			rc);
81479fac9c9SMichael Cyr 		return rc;
81579fac9c9SMichael Cyr 	}
81679fac9c9SMichael Cyr 
81779fac9c9SMichael Cyr 	rc = ibmvscsis_check_init_msg(vscsi, &format);
81879fac9c9SMichael Cyr 	if (rc) {
819c9b3379fSMichael Cyr 		dev_err(&vscsi->dev, "establish_new_q: check_init_msg failed, rc %ld\n",
82079fac9c9SMichael Cyr 			rc);
82179fac9c9SMichael Cyr 		return rc;
82279fac9c9SMichael Cyr 	}
82379fac9c9SMichael Cyr 
824c9b3379fSMichael Cyr 	if (format == UNUSED_FORMAT) {
82579fac9c9SMichael Cyr 		rc = ibmvscsis_send_init_message(vscsi, INIT_MSG);
82679fac9c9SMichael Cyr 		switch (rc) {
82779fac9c9SMichael Cyr 		case H_SUCCESS:
82879fac9c9SMichael Cyr 		case H_DROPPED:
82979fac9c9SMichael Cyr 		case H_CLOSED:
83079fac9c9SMichael Cyr 			rc = ADAPT_SUCCESS;
83179fac9c9SMichael Cyr 			break;
83279fac9c9SMichael Cyr 
83379fac9c9SMichael Cyr 		case H_PARAMETER:
83479fac9c9SMichael Cyr 		case H_HARDWARE:
83579fac9c9SMichael Cyr 			break;
83679fac9c9SMichael Cyr 
83779fac9c9SMichael Cyr 		default:
83879fac9c9SMichael Cyr 			vscsi->state = UNDEFINED;
83979fac9c9SMichael Cyr 			rc = H_HARDWARE;
84079fac9c9SMichael Cyr 			break;
84179fac9c9SMichael Cyr 		}
842c9b3379fSMichael Cyr 	} else if (format == INIT_MSG) {
843c9b3379fSMichael Cyr 		rc = ibmvscsis_handle_init_msg(vscsi);
84479fac9c9SMichael Cyr 	}
84579fac9c9SMichael Cyr 
84679fac9c9SMichael Cyr 	return rc;
84779fac9c9SMichael Cyr }
84879fac9c9SMichael Cyr 
84979fac9c9SMichael Cyr /**
85079fac9c9SMichael Cyr  * ibmvscsis_reset_queue() - Reset CRQ Queue
85179fac9c9SMichael Cyr  * @vscsi:	Pointer to our adapter structure
85279fac9c9SMichael Cyr  *
85379fac9c9SMichael Cyr  * This function calls h_free_q and then calls h_reg_q and does all
85479fac9c9SMichael Cyr  * of the bookkeeping to get us back to where we can communicate.
85579fac9c9SMichael Cyr  *
85679fac9c9SMichael Cyr  * Actually, we don't always call h_free_crq.  A problem was discovered
85779fac9c9SMichael Cyr  * where one partition would close and reopen his queue, which would
85879fac9c9SMichael Cyr  * cause his partner to get a transport event, which would cause him to
85979fac9c9SMichael Cyr  * close and reopen his queue, which would cause the original partition
86079fac9c9SMichael Cyr  * to get a transport event, etc., etc.  To prevent this, we don't
86179fac9c9SMichael Cyr  * actually close our queue if the client initiated the reset, (i.e.
86279fac9c9SMichael Cyr  * either we got a transport event or we have detected that the client's
86379fac9c9SMichael Cyr  * queue is gone)
86479fac9c9SMichael Cyr  *
86579fac9c9SMichael Cyr  * EXECUTION ENVIRONMENT:
86679fac9c9SMichael Cyr  *	Process environment, called with interrupt lock held
86779fac9c9SMichael Cyr  */
868c9b3379fSMichael Cyr static void ibmvscsis_reset_queue(struct scsi_info *vscsi)
86979fac9c9SMichael Cyr {
87079fac9c9SMichael Cyr 	int bytes;
87179fac9c9SMichael Cyr 	long rc = ADAPT_SUCCESS;
87279fac9c9SMichael Cyr 
87379fac9c9SMichael Cyr 	pr_debug("reset_queue: flags 0x%x\n", vscsi->flags);
87479fac9c9SMichael Cyr 
87579fac9c9SMichael Cyr 	/* don't reset, the client did it for us */
87679fac9c9SMichael Cyr 	if (vscsi->flags & (CLIENT_FAILED | TRANS_EVENT)) {
87779fac9c9SMichael Cyr 		vscsi->flags &= PRESERVE_FLAG_FIELDS;
87879fac9c9SMichael Cyr 		vscsi->rsp_q_timer.timer_pops = 0;
87979fac9c9SMichael Cyr 		vscsi->debit = 0;
88079fac9c9SMichael Cyr 		vscsi->credit = 0;
881c9b3379fSMichael Cyr 		vscsi->state = WAIT_CONNECTION;
88279fac9c9SMichael Cyr 		vio_enable_interrupts(vscsi->dma_dev);
88379fac9c9SMichael Cyr 	} else {
88479fac9c9SMichael Cyr 		rc = ibmvscsis_free_command_q(vscsi);
88579fac9c9SMichael Cyr 		if (rc == ADAPT_SUCCESS) {
886c9b3379fSMichael Cyr 			vscsi->state = WAIT_CONNECTION;
88779fac9c9SMichael Cyr 
88879fac9c9SMichael Cyr 			bytes = vscsi->cmd_q.size * PAGE_SIZE;
88979fac9c9SMichael Cyr 			rc = h_reg_crq(vscsi->dds.unit_id,
89079fac9c9SMichael Cyr 				       vscsi->cmd_q.crq_token, bytes);
89179fac9c9SMichael Cyr 			if (rc == H_CLOSED || rc == H_SUCCESS) {
892c9b3379fSMichael Cyr 				rc = ibmvscsis_establish_new_q(vscsi);
89379fac9c9SMichael Cyr 			}
89479fac9c9SMichael Cyr 
89579fac9c9SMichael Cyr 			if (rc != ADAPT_SUCCESS) {
89679fac9c9SMichael Cyr 				pr_debug("reset_queue: reg_crq rc %ld\n", rc);
89779fac9c9SMichael Cyr 
89879fac9c9SMichael Cyr 				vscsi->state = ERR_DISCONNECTED;
89979fac9c9SMichael Cyr 				vscsi->flags |= RESPONSE_Q_DOWN;
90079fac9c9SMichael Cyr 				ibmvscsis_free_command_q(vscsi);
90179fac9c9SMichael Cyr 			}
90279fac9c9SMichael Cyr 		} else {
90379fac9c9SMichael Cyr 			vscsi->state = ERR_DISCONNECTED;
90479fac9c9SMichael Cyr 			vscsi->flags |= RESPONSE_Q_DOWN;
90579fac9c9SMichael Cyr 		}
90679fac9c9SMichael Cyr 	}
90779fac9c9SMichael Cyr }
90879fac9c9SMichael Cyr 
90979fac9c9SMichael Cyr /**
91079fac9c9SMichael Cyr  * ibmvscsis_free_cmd_resources() - Free command resources
91179fac9c9SMichael Cyr  * @vscsi:	Pointer to our adapter structure
91279fac9c9SMichael Cyr  * @cmd:	Command which is not longer in use
91379fac9c9SMichael Cyr  *
91479fac9c9SMichael Cyr  * Must be called with interrupt lock held.
91579fac9c9SMichael Cyr  */
91679fac9c9SMichael Cyr static void ibmvscsis_free_cmd_resources(struct scsi_info *vscsi,
91779fac9c9SMichael Cyr 					 struct ibmvscsis_cmd *cmd)
91879fac9c9SMichael Cyr {
91979fac9c9SMichael Cyr 	struct iu_entry *iue = cmd->iue;
92079fac9c9SMichael Cyr 
92179fac9c9SMichael Cyr 	switch (cmd->type) {
92279fac9c9SMichael Cyr 	case TASK_MANAGEMENT:
92379fac9c9SMichael Cyr 	case SCSI_CDB:
92479fac9c9SMichael Cyr 		/*
92579fac9c9SMichael Cyr 		 * When the queue goes down this value is cleared, so it
92679fac9c9SMichael Cyr 		 * cannot be cleared in this general purpose function.
92779fac9c9SMichael Cyr 		 */
92879fac9c9SMichael Cyr 		if (vscsi->debit)
92979fac9c9SMichael Cyr 			vscsi->debit -= 1;
93079fac9c9SMichael Cyr 		break;
93179fac9c9SMichael Cyr 	case ADAPTER_MAD:
93279fac9c9SMichael Cyr 		vscsi->flags &= ~PROCESSING_MAD;
93379fac9c9SMichael Cyr 		break;
93479fac9c9SMichael Cyr 	case UNSET_TYPE:
93579fac9c9SMichael Cyr 		break;
93679fac9c9SMichael Cyr 	default:
93779fac9c9SMichael Cyr 		dev_err(&vscsi->dev, "free_cmd_resources unknown type %d\n",
93879fac9c9SMichael Cyr 			cmd->type);
93979fac9c9SMichael Cyr 		break;
94079fac9c9SMichael Cyr 	}
94179fac9c9SMichael Cyr 
94279fac9c9SMichael Cyr 	cmd->iue = NULL;
94379fac9c9SMichael Cyr 	list_add_tail(&cmd->list, &vscsi->free_cmd);
94479fac9c9SMichael Cyr 	srp_iu_put(iue);
94579fac9c9SMichael Cyr 
94679fac9c9SMichael Cyr 	if (list_empty(&vscsi->active_q) && list_empty(&vscsi->schedule_q) &&
94779fac9c9SMichael Cyr 	    list_empty(&vscsi->waiting_rsp) && (vscsi->flags & WAIT_FOR_IDLE)) {
94879fac9c9SMichael Cyr 		vscsi->flags &= ~WAIT_FOR_IDLE;
94979fac9c9SMichael Cyr 		complete(&vscsi->wait_idle);
95079fac9c9SMichael Cyr 	}
95179fac9c9SMichael Cyr }
95279fac9c9SMichael Cyr 
95379fac9c9SMichael Cyr /**
95488a678bbSBryant G. Ly  * ibmvscsis_trans_event() - Handle a Transport Event
95588a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
95688a678bbSBryant G. Ly  * @crq:	Pointer to CRQ entry containing the Transport Event
95788a678bbSBryant G. Ly  *
95888a678bbSBryant G. Ly  * Do the logic to close the I_T nexus.  This function may not
95988a678bbSBryant G. Ly  * behave to specification.
96088a678bbSBryant G. Ly  *
96188a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
96288a678bbSBryant G. Ly  *	Interrupt, interrupt lock held
96388a678bbSBryant G. Ly  */
96488a678bbSBryant G. Ly static long ibmvscsis_trans_event(struct scsi_info *vscsi,
96588a678bbSBryant G. Ly 				  struct viosrp_crq *crq)
96688a678bbSBryant G. Ly {
96788a678bbSBryant G. Ly 	long rc = ADAPT_SUCCESS;
96888a678bbSBryant G. Ly 
96988a678bbSBryant G. Ly 	pr_debug("trans_event: format %d, flags 0x%x, state 0x%hx\n",
97088a678bbSBryant G. Ly 		 (int)crq->format, vscsi->flags, vscsi->state);
97188a678bbSBryant G. Ly 
97288a678bbSBryant G. Ly 	switch (crq->format) {
97388a678bbSBryant G. Ly 	case MIGRATED:
97488a678bbSBryant G. Ly 	case PARTNER_FAILED:
97588a678bbSBryant G. Ly 	case PARTNER_DEREGISTER:
97688a678bbSBryant G. Ly 		ibmvscsis_delete_client_info(vscsi, true);
97788a678bbSBryant G. Ly 		break;
97888a678bbSBryant G. Ly 
97988a678bbSBryant G. Ly 	default:
98088a678bbSBryant G. Ly 		rc = ERROR;
98188a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "trans_event: invalid format %d\n",
98288a678bbSBryant G. Ly 			(uint)crq->format);
98388a678bbSBryant G. Ly 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT,
98488a678bbSBryant G. Ly 					  RESPONSE_Q_DOWN);
98588a678bbSBryant G. Ly 		break;
98688a678bbSBryant G. Ly 	}
98788a678bbSBryant G. Ly 
98888a678bbSBryant G. Ly 	if (rc == ADAPT_SUCCESS) {
98988a678bbSBryant G. Ly 		switch (vscsi->state) {
99088a678bbSBryant G. Ly 		case NO_QUEUE:
99188a678bbSBryant G. Ly 		case ERR_DISCONNECTED:
99288a678bbSBryant G. Ly 		case UNDEFINED:
99388a678bbSBryant G. Ly 			break;
99488a678bbSBryant G. Ly 
99588a678bbSBryant G. Ly 		case UNCONFIGURING:
99688a678bbSBryant G. Ly 			vscsi->flags |= (RESPONSE_Q_DOWN | TRANS_EVENT);
99788a678bbSBryant G. Ly 			break;
99888a678bbSBryant G. Ly 
99988a678bbSBryant G. Ly 		case WAIT_ENABLED:
100088a678bbSBryant G. Ly 			break;
100188a678bbSBryant G. Ly 
100288a678bbSBryant G. Ly 		case WAIT_CONNECTION:
100388a678bbSBryant G. Ly 			break;
100488a678bbSBryant G. Ly 
100588a678bbSBryant G. Ly 		case CONNECTED:
100688a678bbSBryant G. Ly 			ibmvscsis_post_disconnect(vscsi, WAIT_IDLE,
100788a678bbSBryant G. Ly 						  (RESPONSE_Q_DOWN |
100888a678bbSBryant G. Ly 						   TRANS_EVENT));
100988a678bbSBryant G. Ly 			break;
101088a678bbSBryant G. Ly 
101188a678bbSBryant G. Ly 		case SRP_PROCESSING:
101288a678bbSBryant G. Ly 			if ((vscsi->debit > 0) ||
101388a678bbSBryant G. Ly 			    !list_empty(&vscsi->schedule_q) ||
101488a678bbSBryant G. Ly 			    !list_empty(&vscsi->waiting_rsp) ||
101588a678bbSBryant G. Ly 			    !list_empty(&vscsi->active_q)) {
101688a678bbSBryant G. Ly 				pr_debug("debit %d, sched %d, wait %d, active %d\n",
101788a678bbSBryant G. Ly 					 vscsi->debit,
101888a678bbSBryant G. Ly 					 (int)list_empty(&vscsi->schedule_q),
101988a678bbSBryant G. Ly 					 (int)list_empty(&vscsi->waiting_rsp),
102088a678bbSBryant G. Ly 					 (int)list_empty(&vscsi->active_q));
102188a678bbSBryant G. Ly 				pr_warn("connection lost with outstanding work\n");
102288a678bbSBryant G. Ly 			} else {
102388a678bbSBryant G. Ly 				pr_debug("trans_event: SRP Processing, but no outstanding work\n");
102488a678bbSBryant G. Ly 			}
102588a678bbSBryant G. Ly 
102688a678bbSBryant G. Ly 			ibmvscsis_post_disconnect(vscsi, WAIT_IDLE,
102788a678bbSBryant G. Ly 						  (RESPONSE_Q_DOWN |
102888a678bbSBryant G. Ly 						   TRANS_EVENT));
102988a678bbSBryant G. Ly 			break;
103088a678bbSBryant G. Ly 
103188a678bbSBryant G. Ly 		case ERR_DISCONNECT:
103288a678bbSBryant G. Ly 		case ERR_DISCONNECT_RECONNECT:
103388a678bbSBryant G. Ly 		case WAIT_IDLE:
103488a678bbSBryant G. Ly 			vscsi->flags |= (RESPONSE_Q_DOWN | TRANS_EVENT);
103588a678bbSBryant G. Ly 			break;
103688a678bbSBryant G. Ly 		}
103788a678bbSBryant G. Ly 	}
103888a678bbSBryant G. Ly 
103988a678bbSBryant G. Ly 	rc = vscsi->flags & SCHEDULE_DISCONNECT;
104088a678bbSBryant G. Ly 
104188a678bbSBryant G. Ly 	pr_debug("Leaving trans_event: flags 0x%x, state 0x%hx, rc %ld\n",
104288a678bbSBryant G. Ly 		 vscsi->flags, vscsi->state, rc);
104388a678bbSBryant G. Ly 
104488a678bbSBryant G. Ly 	return rc;
104588a678bbSBryant G. Ly }
104688a678bbSBryant G. Ly 
104788a678bbSBryant G. Ly /**
104888a678bbSBryant G. Ly  * ibmvscsis_poll_cmd_q() - Poll Command Queue
104988a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
105088a678bbSBryant G. Ly  *
105188a678bbSBryant G. Ly  * Called to handle command elements that may have arrived while
105288a678bbSBryant G. Ly  * interrupts were disabled.
105388a678bbSBryant G. Ly  *
105488a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
105588a678bbSBryant G. Ly  *	intr_lock must be held
105688a678bbSBryant G. Ly  */
105788a678bbSBryant G. Ly static void ibmvscsis_poll_cmd_q(struct scsi_info *vscsi)
105888a678bbSBryant G. Ly {
105988a678bbSBryant G. Ly 	struct viosrp_crq *crq;
106088a678bbSBryant G. Ly 	long rc;
106188a678bbSBryant G. Ly 	bool ack = true;
106288a678bbSBryant G. Ly 	volatile u8 valid;
106388a678bbSBryant G. Ly 
106488a678bbSBryant G. Ly 	pr_debug("poll_cmd_q: flags 0x%x, state 0x%hx, q index %ud\n",
106588a678bbSBryant G. Ly 		 vscsi->flags, vscsi->state, vscsi->cmd_q.index);
106688a678bbSBryant G. Ly 
106788a678bbSBryant G. Ly 	rc = vscsi->flags & SCHEDULE_DISCONNECT;
106888a678bbSBryant G. Ly 	crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
106988a678bbSBryant G. Ly 	valid = crq->valid;
107088a678bbSBryant G. Ly 	dma_rmb();
107188a678bbSBryant G. Ly 
107288a678bbSBryant G. Ly 	while (valid) {
107388a678bbSBryant G. Ly poll_work:
107488a678bbSBryant G. Ly 		vscsi->cmd_q.index =
107588a678bbSBryant G. Ly 			(vscsi->cmd_q.index + 1) & vscsi->cmd_q.mask;
107688a678bbSBryant G. Ly 
107788a678bbSBryant G. Ly 		if (!rc) {
107888a678bbSBryant G. Ly 			rc = ibmvscsis_parse_command(vscsi, crq);
107988a678bbSBryant G. Ly 		} else {
108088a678bbSBryant G. Ly 			if ((uint)crq->valid == VALID_TRANS_EVENT) {
108188a678bbSBryant G. Ly 				/*
108288a678bbSBryant G. Ly 				 * must service the transport layer events even
108388a678bbSBryant G. Ly 				 * in an error state, dont break out until all
108488a678bbSBryant G. Ly 				 * the consecutive transport events have been
108588a678bbSBryant G. Ly 				 * processed
108688a678bbSBryant G. Ly 				 */
108788a678bbSBryant G. Ly 				rc = ibmvscsis_trans_event(vscsi, crq);
108888a678bbSBryant G. Ly 			} else if (vscsi->flags & TRANS_EVENT) {
108988a678bbSBryant G. Ly 				/*
109088a678bbSBryant G. Ly 				 * if a tranport event has occurred leave
109188a678bbSBryant G. Ly 				 * everything but transport events on the queue
109288a678bbSBryant G. Ly 				 */
109388a678bbSBryant G. Ly 				pr_debug("poll_cmd_q, ignoring\n");
109488a678bbSBryant G. Ly 
109588a678bbSBryant G. Ly 				/*
109688a678bbSBryant G. Ly 				 * need to decrement the queue index so we can
109788a678bbSBryant G. Ly 				 * look at the elment again
109888a678bbSBryant G. Ly 				 */
109988a678bbSBryant G. Ly 				if (vscsi->cmd_q.index)
110088a678bbSBryant G. Ly 					vscsi->cmd_q.index -= 1;
110188a678bbSBryant G. Ly 				else
110288a678bbSBryant G. Ly 					/*
110388a678bbSBryant G. Ly 					 * index is at 0 it just wrapped.
110488a678bbSBryant G. Ly 					 * have it index last element in q
110588a678bbSBryant G. Ly 					 */
110688a678bbSBryant G. Ly 					vscsi->cmd_q.index = vscsi->cmd_q.mask;
110788a678bbSBryant G. Ly 				break;
110888a678bbSBryant G. Ly 			}
110988a678bbSBryant G. Ly 		}
111088a678bbSBryant G. Ly 
111188a678bbSBryant G. Ly 		crq->valid = INVALIDATE_CMD_RESP_EL;
111288a678bbSBryant G. Ly 
111388a678bbSBryant G. Ly 		crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
111488a678bbSBryant G. Ly 		valid = crq->valid;
111588a678bbSBryant G. Ly 		dma_rmb();
111688a678bbSBryant G. Ly 	}
111788a678bbSBryant G. Ly 
111888a678bbSBryant G. Ly 	if (!rc) {
111988a678bbSBryant G. Ly 		if (ack) {
112088a678bbSBryant G. Ly 			vio_enable_interrupts(vscsi->dma_dev);
112188a678bbSBryant G. Ly 			ack = false;
112288a678bbSBryant G. Ly 			pr_debug("poll_cmd_q, reenabling interrupts\n");
112388a678bbSBryant G. Ly 		}
112488a678bbSBryant G. Ly 		valid = crq->valid;
112588a678bbSBryant G. Ly 		dma_rmb();
112688a678bbSBryant G. Ly 		if (valid)
112788a678bbSBryant G. Ly 			goto poll_work;
112888a678bbSBryant G. Ly 	}
112988a678bbSBryant G. Ly 
113088a678bbSBryant G. Ly 	pr_debug("Leaving poll_cmd_q: rc %ld\n", rc);
113188a678bbSBryant G. Ly }
113288a678bbSBryant G. Ly 
113388a678bbSBryant G. Ly /**
113488a678bbSBryant G. Ly  * ibmvscsis_free_cmd_qs() - Free elements in queue
113588a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
113688a678bbSBryant G. Ly  *
113788a678bbSBryant G. Ly  * Free all of the elements on all queues that are waiting for
113888a678bbSBryant G. Ly  * whatever reason.
113988a678bbSBryant G. Ly  *
114088a678bbSBryant G. Ly  * PRECONDITION:
114188a678bbSBryant G. Ly  *	Called with interrupt lock held
114288a678bbSBryant G. Ly  */
114388a678bbSBryant G. Ly static void ibmvscsis_free_cmd_qs(struct scsi_info *vscsi)
114488a678bbSBryant G. Ly {
114588a678bbSBryant G. Ly 	struct ibmvscsis_cmd *cmd, *nxt;
114688a678bbSBryant G. Ly 
114788a678bbSBryant G. Ly 	pr_debug("free_cmd_qs: waiting_rsp empty %d, timer starter %d\n",
114888a678bbSBryant G. Ly 		 (int)list_empty(&vscsi->waiting_rsp),
114988a678bbSBryant G. Ly 		 vscsi->rsp_q_timer.started);
115088a678bbSBryant G. Ly 
115188a678bbSBryant G. Ly 	list_for_each_entry_safe(cmd, nxt, &vscsi->waiting_rsp, list) {
115288a678bbSBryant G. Ly 		list_del(&cmd->list);
115388a678bbSBryant G. Ly 		ibmvscsis_free_cmd_resources(vscsi, cmd);
115488a678bbSBryant G. Ly 	}
115588a678bbSBryant G. Ly }
115688a678bbSBryant G. Ly 
115788a678bbSBryant G. Ly /**
115888a678bbSBryant G. Ly  * ibmvscsis_get_free_cmd() - Get free command from list
115988a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
116088a678bbSBryant G. Ly  *
116188a678bbSBryant G. Ly  * Must be called with interrupt lock held.
116288a678bbSBryant G. Ly  */
116388a678bbSBryant G. Ly static struct ibmvscsis_cmd *ibmvscsis_get_free_cmd(struct scsi_info *vscsi)
116488a678bbSBryant G. Ly {
116588a678bbSBryant G. Ly 	struct ibmvscsis_cmd *cmd = NULL;
116688a678bbSBryant G. Ly 	struct iu_entry *iue;
116788a678bbSBryant G. Ly 
116888a678bbSBryant G. Ly 	iue = srp_iu_get(&vscsi->target);
116988a678bbSBryant G. Ly 	if (iue) {
117088a678bbSBryant G. Ly 		cmd = list_first_entry_or_null(&vscsi->free_cmd,
117188a678bbSBryant G. Ly 					       struct ibmvscsis_cmd, list);
117288a678bbSBryant G. Ly 		if (cmd) {
117388a678bbSBryant G. Ly 			list_del(&cmd->list);
117488a678bbSBryant G. Ly 			cmd->iue = iue;
117588a678bbSBryant G. Ly 			cmd->type = UNSET_TYPE;
117688a678bbSBryant G. Ly 			memset(&cmd->se_cmd, 0, sizeof(cmd->se_cmd));
117788a678bbSBryant G. Ly 		} else {
117888a678bbSBryant G. Ly 			srp_iu_put(iue);
117988a678bbSBryant G. Ly 		}
118088a678bbSBryant G. Ly 	}
118188a678bbSBryant G. Ly 
118288a678bbSBryant G. Ly 	return cmd;
118388a678bbSBryant G. Ly }
118488a678bbSBryant G. Ly 
118588a678bbSBryant G. Ly /**
118688a678bbSBryant G. Ly  * ibmvscsis_adapter_idle() - Helper function to handle idle adapter
118788a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
118888a678bbSBryant G. Ly  *
118988a678bbSBryant G. Ly  * This function is called when the adapter is idle when the driver
119088a678bbSBryant G. Ly  * is attempting to clear an error condition.
119188a678bbSBryant G. Ly  * The adapter is considered busy if any of its cmd queues
119288a678bbSBryant G. Ly  * are non-empty. This function can be invoked
119388a678bbSBryant G. Ly  * from the off level disconnect function.
119488a678bbSBryant G. Ly  *
119588a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
119688a678bbSBryant G. Ly  *	Process environment called with interrupt lock held
119788a678bbSBryant G. Ly  */
119888a678bbSBryant G. Ly static void ibmvscsis_adapter_idle(struct scsi_info *vscsi)
119988a678bbSBryant G. Ly {
120088a678bbSBryant G. Ly 	int free_qs = false;
120188a678bbSBryant G. Ly 
120288a678bbSBryant G. Ly 	pr_debug("adapter_idle: flags 0x%x, state 0x%hx\n", vscsi->flags,
120388a678bbSBryant G. Ly 		 vscsi->state);
120488a678bbSBryant G. Ly 
120588a678bbSBryant G. Ly 	/* Only need to free qs if we're disconnecting from client */
120688a678bbSBryant G. Ly 	if (vscsi->state != WAIT_CONNECTION || vscsi->flags & TRANS_EVENT)
120788a678bbSBryant G. Ly 		free_qs = true;
120888a678bbSBryant G. Ly 
120988a678bbSBryant G. Ly 	switch (vscsi->state) {
12108bf11557SMichael Cyr 	case UNCONFIGURING:
12118bf11557SMichael Cyr 		ibmvscsis_free_command_q(vscsi);
12128bf11557SMichael Cyr 		dma_rmb();
12138bf11557SMichael Cyr 		isync();
12148bf11557SMichael Cyr 		if (vscsi->flags & CFG_SLEEPING) {
12158bf11557SMichael Cyr 			vscsi->flags &= ~CFG_SLEEPING;
12168bf11557SMichael Cyr 			complete(&vscsi->unconfig);
12178bf11557SMichael Cyr 		}
12188bf11557SMichael Cyr 		break;
121988a678bbSBryant G. Ly 	case ERR_DISCONNECT_RECONNECT:
1220c9b3379fSMichael Cyr 		ibmvscsis_reset_queue(vscsi);
122188a678bbSBryant G. Ly 		pr_debug("adapter_idle, disc_rec: flags 0x%x\n", vscsi->flags);
122288a678bbSBryant G. Ly 		break;
122388a678bbSBryant G. Ly 
122488a678bbSBryant G. Ly 	case ERR_DISCONNECT:
122588a678bbSBryant G. Ly 		ibmvscsis_free_command_q(vscsi);
1226c9b3379fSMichael Cyr 		vscsi->flags &= ~(SCHEDULE_DISCONNECT | DISCONNECT_SCHEDULED);
122788a678bbSBryant G. Ly 		vscsi->flags |= RESPONSE_Q_DOWN;
1228c9b3379fSMichael Cyr 		if (vscsi->tport.enabled)
122988a678bbSBryant G. Ly 			vscsi->state = ERR_DISCONNECTED;
1230c9b3379fSMichael Cyr 		else
1231c9b3379fSMichael Cyr 			vscsi->state = WAIT_ENABLED;
123288a678bbSBryant G. Ly 		pr_debug("adapter_idle, disc: flags 0x%x, state 0x%hx\n",
123388a678bbSBryant G. Ly 			 vscsi->flags, vscsi->state);
123488a678bbSBryant G. Ly 		break;
123588a678bbSBryant G. Ly 
123688a678bbSBryant G. Ly 	case WAIT_IDLE:
123788a678bbSBryant G. Ly 		vscsi->rsp_q_timer.timer_pops = 0;
123888a678bbSBryant G. Ly 		vscsi->debit = 0;
123988a678bbSBryant G. Ly 		vscsi->credit = 0;
124088a678bbSBryant G. Ly 		if (vscsi->flags & TRANS_EVENT) {
124188a678bbSBryant G. Ly 			vscsi->state = WAIT_CONNECTION;
124288a678bbSBryant G. Ly 			vscsi->flags &= PRESERVE_FLAG_FIELDS;
124388a678bbSBryant G. Ly 		} else {
124488a678bbSBryant G. Ly 			vscsi->state = CONNECTED;
124588a678bbSBryant G. Ly 			vscsi->flags &= ~DISCONNECT_SCHEDULED;
124688a678bbSBryant G. Ly 		}
124788a678bbSBryant G. Ly 
124888a678bbSBryant G. Ly 		pr_debug("adapter_idle, wait: flags 0x%x, state 0x%hx\n",
124988a678bbSBryant G. Ly 			 vscsi->flags, vscsi->state);
125088a678bbSBryant G. Ly 		ibmvscsis_poll_cmd_q(vscsi);
125188a678bbSBryant G. Ly 		break;
125288a678bbSBryant G. Ly 
125388a678bbSBryant G. Ly 	case ERR_DISCONNECTED:
125488a678bbSBryant G. Ly 		vscsi->flags &= ~DISCONNECT_SCHEDULED;
125588a678bbSBryant G. Ly 		pr_debug("adapter_idle, disconnected: flags 0x%x, state 0x%hx\n",
125688a678bbSBryant G. Ly 			 vscsi->flags, vscsi->state);
125788a678bbSBryant G. Ly 		break;
125888a678bbSBryant G. Ly 
125988a678bbSBryant G. Ly 	default:
126088a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "adapter_idle: in invalid state %d\n",
126188a678bbSBryant G. Ly 			vscsi->state);
126288a678bbSBryant G. Ly 		break;
126388a678bbSBryant G. Ly 	}
126488a678bbSBryant G. Ly 
126588a678bbSBryant G. Ly 	if (free_qs)
126688a678bbSBryant G. Ly 		ibmvscsis_free_cmd_qs(vscsi);
126788a678bbSBryant G. Ly 
126888a678bbSBryant G. Ly 	/*
126988a678bbSBryant G. Ly 	 * There is a timing window where we could lose a disconnect request.
127088a678bbSBryant G. Ly 	 * The known path to this window occurs during the DISCONNECT_RECONNECT
127188a678bbSBryant G. Ly 	 * case above: reset_queue calls free_command_q, which will release the
127288a678bbSBryant G. Ly 	 * interrupt lock.  During that time, a new post_disconnect call can be
127388a678bbSBryant G. Ly 	 * made with a "more severe" state (DISCONNECT or UNCONFIGURING).
127488a678bbSBryant G. Ly 	 * Because the DISCONNECT_SCHEDULED flag is already set, post_disconnect
127588a678bbSBryant G. Ly 	 * will only set the new_state.  Now free_command_q reacquires the intr
127688a678bbSBryant G. Ly 	 * lock and clears the DISCONNECT_SCHEDULED flag (using PRESERVE_FLAG_
127788a678bbSBryant G. Ly 	 * FIELDS), and the disconnect is lost.  This is particularly bad when
127888a678bbSBryant G. Ly 	 * the new disconnect was for UNCONFIGURING, since the unconfigure hangs
127988a678bbSBryant G. Ly 	 * forever.
128088a678bbSBryant G. Ly 	 * Fix is that free command queue sets acr state and acr flags if there
128188a678bbSBryant G. Ly 	 * is a change under the lock
128288a678bbSBryant G. Ly 	 * note free command queue writes to this state it clears it
128388a678bbSBryant G. Ly 	 * before releasing the lock, different drivers call the free command
128488a678bbSBryant G. Ly 	 * queue different times so dont initialize above
128588a678bbSBryant G. Ly 	 */
128688a678bbSBryant G. Ly 	if (vscsi->phyp_acr_state != 0)	{
128788a678bbSBryant G. Ly 		/*
128888a678bbSBryant G. Ly 		 * set any bits in flags that may have been cleared by
128988a678bbSBryant G. Ly 		 * a call to free command queue in switch statement
129088a678bbSBryant G. Ly 		 * or reset queue
129188a678bbSBryant G. Ly 		 */
129288a678bbSBryant G. Ly 		vscsi->flags |= vscsi->phyp_acr_flags;
129388a678bbSBryant G. Ly 		ibmvscsis_post_disconnect(vscsi, vscsi->phyp_acr_state, 0);
129488a678bbSBryant G. Ly 		vscsi->phyp_acr_state = 0;
129588a678bbSBryant G. Ly 		vscsi->phyp_acr_flags = 0;
129688a678bbSBryant G. Ly 
129788a678bbSBryant G. Ly 		pr_debug("adapter_idle: flags 0x%x, state 0x%hx, acr_flags 0x%x, acr_state 0x%hx\n",
129888a678bbSBryant G. Ly 			 vscsi->flags, vscsi->state, vscsi->phyp_acr_flags,
129988a678bbSBryant G. Ly 			 vscsi->phyp_acr_state);
130088a678bbSBryant G. Ly 	}
130188a678bbSBryant G. Ly 
130288a678bbSBryant G. Ly 	pr_debug("Leaving adapter_idle: flags 0x%x, state 0x%hx, new_state 0x%x\n",
130388a678bbSBryant G. Ly 		 vscsi->flags, vscsi->state, vscsi->new_state);
130488a678bbSBryant G. Ly }
130588a678bbSBryant G. Ly 
130688a678bbSBryant G. Ly /**
130788a678bbSBryant G. Ly  * ibmvscsis_copy_crq_packet() - Copy CRQ Packet
130888a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
130988a678bbSBryant G. Ly  * @cmd:	Pointer to command element to use to process the request
131088a678bbSBryant G. Ly  * @crq:	Pointer to CRQ entry containing the request
131188a678bbSBryant G. Ly  *
131288a678bbSBryant G. Ly  * Copy the srp information unit from the hosted
131388a678bbSBryant G. Ly  * partition using remote dma
131488a678bbSBryant G. Ly  *
131588a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
131688a678bbSBryant G. Ly  *	Interrupt, interrupt lock held
131788a678bbSBryant G. Ly  */
131888a678bbSBryant G. Ly static long ibmvscsis_copy_crq_packet(struct scsi_info *vscsi,
131988a678bbSBryant G. Ly 				      struct ibmvscsis_cmd *cmd,
132088a678bbSBryant G. Ly 				      struct viosrp_crq *crq)
132188a678bbSBryant G. Ly {
132288a678bbSBryant G. Ly 	struct iu_entry *iue = cmd->iue;
132388a678bbSBryant G. Ly 	long rc = 0;
132488a678bbSBryant G. Ly 	u16 len;
132588a678bbSBryant G. Ly 
132688a678bbSBryant G. Ly 	len = be16_to_cpu(crq->IU_length);
132788a678bbSBryant G. Ly 	if ((len > SRP_MAX_IU_LEN) || (len == 0)) {
132888a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "copy_crq: Invalid len %d passed", len);
132988a678bbSBryant G. Ly 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
133088a678bbSBryant G. Ly 		return SRP_VIOLATION;
133188a678bbSBryant G. Ly 	}
133288a678bbSBryant G. Ly 
133388a678bbSBryant G. Ly 	rc = h_copy_rdma(len, vscsi->dds.window[REMOTE].liobn,
133488a678bbSBryant G. Ly 			 be64_to_cpu(crq->IU_data_ptr),
133588a678bbSBryant G. Ly 			 vscsi->dds.window[LOCAL].liobn, iue->sbuf->dma);
133688a678bbSBryant G. Ly 
133788a678bbSBryant G. Ly 	switch (rc) {
133888a678bbSBryant G. Ly 	case H_SUCCESS:
133988a678bbSBryant G. Ly 		cmd->init_time = mftb();
134088a678bbSBryant G. Ly 		iue->remote_token = crq->IU_data_ptr;
134188a678bbSBryant G. Ly 		iue->iu_len = len;
134288a678bbSBryant G. Ly 		pr_debug("copy_crq: ioba 0x%llx, init_time 0x%llx\n",
134388a678bbSBryant G. Ly 			 be64_to_cpu(crq->IU_data_ptr), cmd->init_time);
134488a678bbSBryant G. Ly 		break;
134588a678bbSBryant G. Ly 	case H_PERMISSION:
134688a678bbSBryant G. Ly 		if (connection_broken(vscsi))
134788a678bbSBryant G. Ly 			ibmvscsis_post_disconnect(vscsi,
134888a678bbSBryant G. Ly 						  ERR_DISCONNECT_RECONNECT,
134988a678bbSBryant G. Ly 						  (RESPONSE_Q_DOWN |
135088a678bbSBryant G. Ly 						   CLIENT_FAILED));
135188a678bbSBryant G. Ly 		else
135288a678bbSBryant G. Ly 			ibmvscsis_post_disconnect(vscsi,
135388a678bbSBryant G. Ly 						  ERR_DISCONNECT_RECONNECT, 0);
135488a678bbSBryant G. Ly 
135588a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "copy_crq: h_copy_rdma failed, rc %ld\n",
135688a678bbSBryant G. Ly 			rc);
135788a678bbSBryant G. Ly 		break;
135888a678bbSBryant G. Ly 	case H_DEST_PARM:
135988a678bbSBryant G. Ly 	case H_SOURCE_PARM:
136088a678bbSBryant G. Ly 	default:
136188a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "copy_crq: h_copy_rdma failed, rc %ld\n",
136288a678bbSBryant G. Ly 			rc);
136388a678bbSBryant G. Ly 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
136488a678bbSBryant G. Ly 		break;
136588a678bbSBryant G. Ly 	}
136688a678bbSBryant G. Ly 
136788a678bbSBryant G. Ly 	return rc;
136888a678bbSBryant G. Ly }
136988a678bbSBryant G. Ly 
137088a678bbSBryant G. Ly /**
137188a678bbSBryant G. Ly  * ibmvscsis_adapter_info - Service an Adapter Info MAnagement Data gram
137288a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
137388a678bbSBryant G. Ly  * @iue:	Information Unit containing the Adapter Info MAD request
137488a678bbSBryant G. Ly  *
137588a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
137679fac9c9SMichael Cyr  *	Interrupt adapter lock is held
137788a678bbSBryant G. Ly  */
137888a678bbSBryant G. Ly static long ibmvscsis_adapter_info(struct scsi_info *vscsi,
137988a678bbSBryant G. Ly 				   struct iu_entry *iue)
138088a678bbSBryant G. Ly {
138188a678bbSBryant G. Ly 	struct viosrp_adapter_info *mad = &vio_iu(iue)->mad.adapter_info;
138288a678bbSBryant G. Ly 	struct mad_adapter_info_data *info;
138388a678bbSBryant G. Ly 	uint flag_bits = 0;
138488a678bbSBryant G. Ly 	dma_addr_t token;
138588a678bbSBryant G. Ly 	long rc;
138688a678bbSBryant G. Ly 
138788a678bbSBryant G. Ly 	mad->common.status = cpu_to_be16(VIOSRP_MAD_SUCCESS);
138888a678bbSBryant G. Ly 
138988a678bbSBryant G. Ly 	if (be16_to_cpu(mad->common.length) > sizeof(*info)) {
139088a678bbSBryant G. Ly 		mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
139188a678bbSBryant G. Ly 		return 0;
139288a678bbSBryant G. Ly 	}
139388a678bbSBryant G. Ly 
139488a678bbSBryant G. Ly 	info = dma_alloc_coherent(&vscsi->dma_dev->dev, sizeof(*info), &token,
139588a678bbSBryant G. Ly 				  GFP_KERNEL);
139688a678bbSBryant G. Ly 	if (!info) {
139788a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "bad dma_alloc_coherent %p\n",
139888a678bbSBryant G. Ly 			iue->target);
139988a678bbSBryant G. Ly 		mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
140088a678bbSBryant G. Ly 		return 0;
140188a678bbSBryant G. Ly 	}
140288a678bbSBryant G. Ly 
140388a678bbSBryant G. Ly 	/* Get remote info */
140488a678bbSBryant G. Ly 	rc = h_copy_rdma(be16_to_cpu(mad->common.length),
140588a678bbSBryant G. Ly 			 vscsi->dds.window[REMOTE].liobn,
140688a678bbSBryant G. Ly 			 be64_to_cpu(mad->buffer),
140788a678bbSBryant G. Ly 			 vscsi->dds.window[LOCAL].liobn, token);
140888a678bbSBryant G. Ly 
140988a678bbSBryant G. Ly 	if (rc != H_SUCCESS) {
141088a678bbSBryant G. Ly 		if (rc == H_PERMISSION) {
141188a678bbSBryant G. Ly 			if (connection_broken(vscsi))
141288a678bbSBryant G. Ly 				flag_bits = (RESPONSE_Q_DOWN | CLIENT_FAILED);
141388a678bbSBryant G. Ly 		}
141488a678bbSBryant G. Ly 		pr_warn("adapter_info: h_copy_rdma from client failed, rc %ld\n",
141588a678bbSBryant G. Ly 			rc);
141688a678bbSBryant G. Ly 		pr_debug("adapter_info: ioba 0x%llx, flags 0x%x, flag_bits 0x%x\n",
141788a678bbSBryant G. Ly 			 be64_to_cpu(mad->buffer), vscsi->flags, flag_bits);
141888a678bbSBryant G. Ly 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
141988a678bbSBryant G. Ly 					  flag_bits);
142088a678bbSBryant G. Ly 		goto free_dma;
142188a678bbSBryant G. Ly 	}
142288a678bbSBryant G. Ly 
142388a678bbSBryant G. Ly 	/*
142488a678bbSBryant G. Ly 	 * Copy client info, but ignore partition number, which we
142588a678bbSBryant G. Ly 	 * already got from phyp - unless we failed to get it from
142688a678bbSBryant G. Ly 	 * phyp (e.g. if we're running on a p5 system).
142788a678bbSBryant G. Ly 	 */
142888a678bbSBryant G. Ly 	if (vscsi->client_data.partition_number == 0)
142988a678bbSBryant G. Ly 		vscsi->client_data.partition_number =
143088a678bbSBryant G. Ly 			be32_to_cpu(info->partition_number);
143188a678bbSBryant G. Ly 	strncpy(vscsi->client_data.srp_version, info->srp_version,
143288a678bbSBryant G. Ly 		sizeof(vscsi->client_data.srp_version));
143388a678bbSBryant G. Ly 	strncpy(vscsi->client_data.partition_name, info->partition_name,
143488a678bbSBryant G. Ly 		sizeof(vscsi->client_data.partition_name));
143588a678bbSBryant G. Ly 	vscsi->client_data.mad_version = be32_to_cpu(info->mad_version);
143688a678bbSBryant G. Ly 	vscsi->client_data.os_type = be32_to_cpu(info->os_type);
143788a678bbSBryant G. Ly 
143888a678bbSBryant G. Ly 	/* Copy our info */
143988a678bbSBryant G. Ly 	strncpy(info->srp_version, SRP_VERSION,
144088a678bbSBryant G. Ly 		sizeof(info->srp_version));
144188a678bbSBryant G. Ly 	strncpy(info->partition_name, vscsi->dds.partition_name,
144288a678bbSBryant G. Ly 		sizeof(info->partition_name));
144388a678bbSBryant G. Ly 	info->partition_number = cpu_to_be32(vscsi->dds.partition_num);
144488a678bbSBryant G. Ly 	info->mad_version = cpu_to_be32(MAD_VERSION_1);
144588a678bbSBryant G. Ly 	info->os_type = cpu_to_be32(LINUX);
144688a678bbSBryant G. Ly 	memset(&info->port_max_txu[0], 0, sizeof(info->port_max_txu));
1447*387b978cSBryant G. Ly 	info->port_max_txu[0] = cpu_to_be32(MAX_TXU);
144888a678bbSBryant G. Ly 
144988a678bbSBryant G. Ly 	dma_wmb();
145088a678bbSBryant G. Ly 	rc = h_copy_rdma(sizeof(*info), vscsi->dds.window[LOCAL].liobn,
145188a678bbSBryant G. Ly 			 token, vscsi->dds.window[REMOTE].liobn,
145288a678bbSBryant G. Ly 			 be64_to_cpu(mad->buffer));
145388a678bbSBryant G. Ly 	switch (rc) {
145488a678bbSBryant G. Ly 	case H_SUCCESS:
145588a678bbSBryant G. Ly 		break;
145688a678bbSBryant G. Ly 
145788a678bbSBryant G. Ly 	case H_SOURCE_PARM:
145888a678bbSBryant G. Ly 	case H_DEST_PARM:
145988a678bbSBryant G. Ly 	case H_PERMISSION:
146088a678bbSBryant G. Ly 		if (connection_broken(vscsi))
146188a678bbSBryant G. Ly 			flag_bits = (RESPONSE_Q_DOWN | CLIENT_FAILED);
146288a678bbSBryant G. Ly 	default:
146388a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "adapter_info: h_copy_rdma to client failed, rc %ld\n",
146488a678bbSBryant G. Ly 			rc);
146588a678bbSBryant G. Ly 		ibmvscsis_post_disconnect(vscsi,
146688a678bbSBryant G. Ly 					  ERR_DISCONNECT_RECONNECT,
146788a678bbSBryant G. Ly 					  flag_bits);
146888a678bbSBryant G. Ly 		break;
146988a678bbSBryant G. Ly 	}
147088a678bbSBryant G. Ly 
147188a678bbSBryant G. Ly free_dma:
147288a678bbSBryant G. Ly 	dma_free_coherent(&vscsi->dma_dev->dev, sizeof(*info), info, token);
147388a678bbSBryant G. Ly 	pr_debug("Leaving adapter_info, rc %ld\n", rc);
147488a678bbSBryant G. Ly 
147588a678bbSBryant G. Ly 	return rc;
147688a678bbSBryant G. Ly }
147788a678bbSBryant G. Ly 
147888a678bbSBryant G. Ly /**
147988a678bbSBryant G. Ly  * ibmvscsis_cap_mad() - Service a Capabilities MAnagement Data gram
148088a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
148188a678bbSBryant G. Ly  * @iue:	Information Unit containing the Capabilities MAD request
148288a678bbSBryant G. Ly  *
148388a678bbSBryant G. Ly  * NOTE: if you return an error from this routine you must be
148488a678bbSBryant G. Ly  * disconnecting or you will cause a hang
148588a678bbSBryant G. Ly  *
148688a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
148788a678bbSBryant G. Ly  *	Interrupt called with adapter lock held
148888a678bbSBryant G. Ly  */
148988a678bbSBryant G. Ly static int ibmvscsis_cap_mad(struct scsi_info *vscsi, struct iu_entry *iue)
149088a678bbSBryant G. Ly {
149188a678bbSBryant G. Ly 	struct viosrp_capabilities *mad = &vio_iu(iue)->mad.capabilities;
149288a678bbSBryant G. Ly 	struct capabilities *cap;
149388a678bbSBryant G. Ly 	struct mad_capability_common *common;
149488a678bbSBryant G. Ly 	dma_addr_t token;
149588a678bbSBryant G. Ly 	u16 olen, len, status, min_len, cap_len;
149688a678bbSBryant G. Ly 	u32 flag;
149788a678bbSBryant G. Ly 	uint flag_bits = 0;
149888a678bbSBryant G. Ly 	long rc = 0;
149988a678bbSBryant G. Ly 
150088a678bbSBryant G. Ly 	olen = be16_to_cpu(mad->common.length);
150188a678bbSBryant G. Ly 	/*
150288a678bbSBryant G. Ly 	 * struct capabilities hardcodes a couple capabilities after the
150388a678bbSBryant G. Ly 	 * header, but the capabilities can actually be in any order.
150488a678bbSBryant G. Ly 	 */
150588a678bbSBryant G. Ly 	min_len = offsetof(struct capabilities, migration);
150688a678bbSBryant G. Ly 	if ((olen < min_len) || (olen > PAGE_SIZE)) {
150788a678bbSBryant G. Ly 		pr_warn("cap_mad: invalid len %d\n", olen);
150888a678bbSBryant G. Ly 		mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
150988a678bbSBryant G. Ly 		return 0;
151088a678bbSBryant G. Ly 	}
151188a678bbSBryant G. Ly 
151288a678bbSBryant G. Ly 	cap = dma_alloc_coherent(&vscsi->dma_dev->dev, olen, &token,
151388a678bbSBryant G. Ly 				 GFP_KERNEL);
151488a678bbSBryant G. Ly 	if (!cap) {
151588a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "bad dma_alloc_coherent %p\n",
151688a678bbSBryant G. Ly 			iue->target);
151788a678bbSBryant G. Ly 		mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
151888a678bbSBryant G. Ly 		return 0;
151988a678bbSBryant G. Ly 	}
152088a678bbSBryant G. Ly 	rc = h_copy_rdma(olen, vscsi->dds.window[REMOTE].liobn,
152188a678bbSBryant G. Ly 			 be64_to_cpu(mad->buffer),
152288a678bbSBryant G. Ly 			 vscsi->dds.window[LOCAL].liobn, token);
152388a678bbSBryant G. Ly 	if (rc == H_SUCCESS) {
152488a678bbSBryant G. Ly 		strncpy(cap->name, dev_name(&vscsi->dma_dev->dev),
152588a678bbSBryant G. Ly 			SRP_MAX_LOC_LEN);
152688a678bbSBryant G. Ly 
152788a678bbSBryant G. Ly 		len = olen - min_len;
152888a678bbSBryant G. Ly 		status = VIOSRP_MAD_SUCCESS;
152988a678bbSBryant G. Ly 		common = (struct mad_capability_common *)&cap->migration;
153088a678bbSBryant G. Ly 
153188a678bbSBryant G. Ly 		while ((len > 0) && (status == VIOSRP_MAD_SUCCESS) && !rc) {
153288a678bbSBryant G. Ly 			pr_debug("cap_mad: len left %hd, cap type %d, cap len %hd\n",
153388a678bbSBryant G. Ly 				 len, be32_to_cpu(common->cap_type),
153488a678bbSBryant G. Ly 				 be16_to_cpu(common->length));
153588a678bbSBryant G. Ly 
153688a678bbSBryant G. Ly 			cap_len = be16_to_cpu(common->length);
153788a678bbSBryant G. Ly 			if (cap_len > len) {
153888a678bbSBryant G. Ly 				dev_err(&vscsi->dev, "cap_mad: cap len mismatch with total len\n");
153988a678bbSBryant G. Ly 				status = VIOSRP_MAD_FAILED;
154088a678bbSBryant G. Ly 				break;
154188a678bbSBryant G. Ly 			}
154288a678bbSBryant G. Ly 
154388a678bbSBryant G. Ly 			if (cap_len == 0) {
154488a678bbSBryant G. Ly 				dev_err(&vscsi->dev, "cap_mad: cap len is 0\n");
154588a678bbSBryant G. Ly 				status = VIOSRP_MAD_FAILED;
154688a678bbSBryant G. Ly 				break;
154788a678bbSBryant G. Ly 			}
154888a678bbSBryant G. Ly 
154988a678bbSBryant G. Ly 			switch (common->cap_type) {
155088a678bbSBryant G. Ly 			default:
155188a678bbSBryant G. Ly 				pr_debug("cap_mad: unsupported capability\n");
155288a678bbSBryant G. Ly 				common->server_support = 0;
155388a678bbSBryant G. Ly 				flag = cpu_to_be32((u32)CAP_LIST_SUPPORTED);
155488a678bbSBryant G. Ly 				cap->flags &= ~flag;
155588a678bbSBryant G. Ly 				break;
155688a678bbSBryant G. Ly 			}
155788a678bbSBryant G. Ly 
155888a678bbSBryant G. Ly 			len = len - cap_len;
155988a678bbSBryant G. Ly 			common = (struct mad_capability_common *)
156088a678bbSBryant G. Ly 				((char *)common + cap_len);
156188a678bbSBryant G. Ly 		}
156288a678bbSBryant G. Ly 
156388a678bbSBryant G. Ly 		mad->common.status = cpu_to_be16(status);
156488a678bbSBryant G. Ly 
156588a678bbSBryant G. Ly 		dma_wmb();
156688a678bbSBryant G. Ly 		rc = h_copy_rdma(olen, vscsi->dds.window[LOCAL].liobn, token,
156788a678bbSBryant G. Ly 				 vscsi->dds.window[REMOTE].liobn,
156888a678bbSBryant G. Ly 				 be64_to_cpu(mad->buffer));
156988a678bbSBryant G. Ly 
157088a678bbSBryant G. Ly 		if (rc != H_SUCCESS) {
157188a678bbSBryant G. Ly 			pr_debug("cap_mad: failed to copy to client, rc %ld\n",
157288a678bbSBryant G. Ly 				 rc);
157388a678bbSBryant G. Ly 
157488a678bbSBryant G. Ly 			if (rc == H_PERMISSION) {
157588a678bbSBryant G. Ly 				if (connection_broken(vscsi))
157688a678bbSBryant G. Ly 					flag_bits = (RESPONSE_Q_DOWN |
157788a678bbSBryant G. Ly 						     CLIENT_FAILED);
157888a678bbSBryant G. Ly 			}
157988a678bbSBryant G. Ly 
158088a678bbSBryant G. Ly 			pr_warn("cap_mad: error copying data to client, rc %ld\n",
158188a678bbSBryant G. Ly 				rc);
158288a678bbSBryant G. Ly 			ibmvscsis_post_disconnect(vscsi,
158388a678bbSBryant G. Ly 						  ERR_DISCONNECT_RECONNECT,
158488a678bbSBryant G. Ly 						  flag_bits);
158588a678bbSBryant G. Ly 		}
158688a678bbSBryant G. Ly 	}
158788a678bbSBryant G. Ly 
158888a678bbSBryant G. Ly 	dma_free_coherent(&vscsi->dma_dev->dev, olen, cap, token);
158988a678bbSBryant G. Ly 
159088a678bbSBryant G. Ly 	pr_debug("Leaving cap_mad, rc %ld, client_cap 0x%x\n",
159188a678bbSBryant G. Ly 		 rc, vscsi->client_cap);
159288a678bbSBryant G. Ly 
159388a678bbSBryant G. Ly 	return rc;
159488a678bbSBryant G. Ly }
159588a678bbSBryant G. Ly 
159688a678bbSBryant G. Ly /**
159788a678bbSBryant G. Ly  * ibmvscsis_process_mad() - Service a MAnagement Data gram
159888a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
159988a678bbSBryant G. Ly  * @iue:	Information Unit containing the MAD request
160088a678bbSBryant G. Ly  *
160188a678bbSBryant G. Ly  * Must be called with interrupt lock held.
160288a678bbSBryant G. Ly  */
160388a678bbSBryant G. Ly static long ibmvscsis_process_mad(struct scsi_info *vscsi, struct iu_entry *iue)
160488a678bbSBryant G. Ly {
160588a678bbSBryant G. Ly 	struct mad_common *mad = (struct mad_common *)&vio_iu(iue)->mad;
160688a678bbSBryant G. Ly 	struct viosrp_empty_iu *empty;
160788a678bbSBryant G. Ly 	long rc = ADAPT_SUCCESS;
160888a678bbSBryant G. Ly 
160988a678bbSBryant G. Ly 	switch (be32_to_cpu(mad->type)) {
161088a678bbSBryant G. Ly 	case VIOSRP_EMPTY_IU_TYPE:
161188a678bbSBryant G. Ly 		empty = &vio_iu(iue)->mad.empty_iu;
161288a678bbSBryant G. Ly 		vscsi->empty_iu_id = be64_to_cpu(empty->buffer);
161388a678bbSBryant G. Ly 		vscsi->empty_iu_tag = be64_to_cpu(empty->common.tag);
161488a678bbSBryant G. Ly 		mad->status = cpu_to_be16(VIOSRP_MAD_SUCCESS);
161588a678bbSBryant G. Ly 		break;
161688a678bbSBryant G. Ly 	case VIOSRP_ADAPTER_INFO_TYPE:
161788a678bbSBryant G. Ly 		rc = ibmvscsis_adapter_info(vscsi, iue);
161888a678bbSBryant G. Ly 		break;
161988a678bbSBryant G. Ly 	case VIOSRP_CAPABILITIES_TYPE:
162088a678bbSBryant G. Ly 		rc = ibmvscsis_cap_mad(vscsi, iue);
162188a678bbSBryant G. Ly 		break;
162288a678bbSBryant G. Ly 	case VIOSRP_ENABLE_FAST_FAIL:
162388a678bbSBryant G. Ly 		if (vscsi->state == CONNECTED) {
162488a678bbSBryant G. Ly 			vscsi->fast_fail = true;
162588a678bbSBryant G. Ly 			mad->status = cpu_to_be16(VIOSRP_MAD_SUCCESS);
162688a678bbSBryant G. Ly 		} else {
162788a678bbSBryant G. Ly 			pr_warn("fast fail mad sent after login\n");
162888a678bbSBryant G. Ly 			mad->status = cpu_to_be16(VIOSRP_MAD_FAILED);
162988a678bbSBryant G. Ly 		}
163088a678bbSBryant G. Ly 		break;
163188a678bbSBryant G. Ly 	default:
163288a678bbSBryant G. Ly 		mad->status = cpu_to_be16(VIOSRP_MAD_NOT_SUPPORTED);
163388a678bbSBryant G. Ly 		break;
163488a678bbSBryant G. Ly 	}
163588a678bbSBryant G. Ly 
163688a678bbSBryant G. Ly 	return rc;
163788a678bbSBryant G. Ly }
163888a678bbSBryant G. Ly 
163988a678bbSBryant G. Ly /**
164088a678bbSBryant G. Ly  * srp_snd_msg_failed() - Handle an error when sending a response
164188a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
164288a678bbSBryant G. Ly  * @rc:		The return code from the h_send_crq command
164388a678bbSBryant G. Ly  *
164488a678bbSBryant G. Ly  * Must be called with interrupt lock held.
164588a678bbSBryant G. Ly  */
164688a678bbSBryant G. Ly static void srp_snd_msg_failed(struct scsi_info *vscsi, long rc)
164788a678bbSBryant G. Ly {
164888a678bbSBryant G. Ly 	ktime_t kt;
164988a678bbSBryant G. Ly 
165088a678bbSBryant G. Ly 	if (rc != H_DROPPED) {
165188a678bbSBryant G. Ly 		ibmvscsis_free_cmd_qs(vscsi);
165288a678bbSBryant G. Ly 
165388a678bbSBryant G. Ly 		if (rc == H_CLOSED)
165488a678bbSBryant G. Ly 			vscsi->flags |= CLIENT_FAILED;
165588a678bbSBryant G. Ly 
165688a678bbSBryant G. Ly 		/* don't flag the same problem multiple times */
165788a678bbSBryant G. Ly 		if (!(vscsi->flags & RESPONSE_Q_DOWN)) {
165888a678bbSBryant G. Ly 			vscsi->flags |= RESPONSE_Q_DOWN;
165988a678bbSBryant G. Ly 			if (!(vscsi->state & (ERR_DISCONNECT |
166088a678bbSBryant G. Ly 					      ERR_DISCONNECT_RECONNECT |
166188a678bbSBryant G. Ly 					      ERR_DISCONNECTED | UNDEFINED))) {
166288a678bbSBryant G. Ly 				dev_err(&vscsi->dev, "snd_msg_failed: setting RESPONSE_Q_DOWN, state 0x%hx, flags 0x%x, rc %ld\n",
166388a678bbSBryant G. Ly 					vscsi->state, vscsi->flags, rc);
166488a678bbSBryant G. Ly 			}
166588a678bbSBryant G. Ly 			ibmvscsis_post_disconnect(vscsi,
166688a678bbSBryant G. Ly 						  ERR_DISCONNECT_RECONNECT, 0);
166788a678bbSBryant G. Ly 		}
166888a678bbSBryant G. Ly 		return;
166988a678bbSBryant G. Ly 	}
167088a678bbSBryant G. Ly 
167188a678bbSBryant G. Ly 	/*
167288a678bbSBryant G. Ly 	 * The response queue is full.
167388a678bbSBryant G. Ly 	 * If the server is processing SRP requests, i.e.
167488a678bbSBryant G. Ly 	 * the client has successfully done an
167588a678bbSBryant G. Ly 	 * SRP_LOGIN, then it will wait forever for room in
167688a678bbSBryant G. Ly 	 * the queue.  However if the system admin
167788a678bbSBryant G. Ly 	 * is attempting to unconfigure the server then one
167888a678bbSBryant G. Ly 	 * or more children will be in a state where
167988a678bbSBryant G. Ly 	 * they are being removed. So if there is even one
168088a678bbSBryant G. Ly 	 * child being removed then the driver assumes
168188a678bbSBryant G. Ly 	 * the system admin is attempting to break the
168288a678bbSBryant G. Ly 	 * connection with the client and MAX_TIMER_POPS
168388a678bbSBryant G. Ly 	 * is honored.
168488a678bbSBryant G. Ly 	 */
168588a678bbSBryant G. Ly 	if ((vscsi->rsp_q_timer.timer_pops < MAX_TIMER_POPS) ||
168688a678bbSBryant G. Ly 	    (vscsi->state == SRP_PROCESSING)) {
168788a678bbSBryant G. Ly 		pr_debug("snd_msg_failed: response queue full, flags 0x%x, timer started %d, pops %d\n",
168888a678bbSBryant G. Ly 			 vscsi->flags, (int)vscsi->rsp_q_timer.started,
168988a678bbSBryant G. Ly 			 vscsi->rsp_q_timer.timer_pops);
169088a678bbSBryant G. Ly 
169188a678bbSBryant G. Ly 		/*
169288a678bbSBryant G. Ly 		 * Check if the timer is running; if it
169388a678bbSBryant G. Ly 		 * is not then start it up.
169488a678bbSBryant G. Ly 		 */
169588a678bbSBryant G. Ly 		if (!vscsi->rsp_q_timer.started) {
169688a678bbSBryant G. Ly 			if (vscsi->rsp_q_timer.timer_pops <
169788a678bbSBryant G. Ly 			    MAX_TIMER_POPS) {
16988b0e1953SThomas Gleixner 				kt = WAIT_NANO_SECONDS;
169988a678bbSBryant G. Ly 			} else {
170088a678bbSBryant G. Ly 				/*
170188a678bbSBryant G. Ly 				 * slide the timeslice if the maximum
170288a678bbSBryant G. Ly 				 * timer pops have already happened
170388a678bbSBryant G. Ly 				 */
170488a678bbSBryant G. Ly 				kt = ktime_set(WAIT_SECONDS, 0);
170588a678bbSBryant G. Ly 			}
170688a678bbSBryant G. Ly 
170788a678bbSBryant G. Ly 			vscsi->rsp_q_timer.started = true;
170888a678bbSBryant G. Ly 			hrtimer_start(&vscsi->rsp_q_timer.timer, kt,
170988a678bbSBryant G. Ly 				      HRTIMER_MODE_REL);
171088a678bbSBryant G. Ly 		}
171188a678bbSBryant G. Ly 	} else {
171288a678bbSBryant G. Ly 		/*
171388a678bbSBryant G. Ly 		 * TBD: Do we need to worry about this? Need to get
171488a678bbSBryant G. Ly 		 *      remove working.
171588a678bbSBryant G. Ly 		 */
171688a678bbSBryant G. Ly 		/*
171788a678bbSBryant G. Ly 		 * waited a long time and it appears the system admin
171888a678bbSBryant G. Ly 		 * is bring this driver down
171988a678bbSBryant G. Ly 		 */
172088a678bbSBryant G. Ly 		vscsi->flags |= RESPONSE_Q_DOWN;
172188a678bbSBryant G. Ly 		ibmvscsis_free_cmd_qs(vscsi);
172288a678bbSBryant G. Ly 		/*
172388a678bbSBryant G. Ly 		 * if the driver is already attempting to disconnect
172488a678bbSBryant G. Ly 		 * from the client and has already logged an error
172588a678bbSBryant G. Ly 		 * trace this event but don't put it in the error log
172688a678bbSBryant G. Ly 		 */
172788a678bbSBryant G. Ly 		if (!(vscsi->state & (ERR_DISCONNECT |
172888a678bbSBryant G. Ly 				      ERR_DISCONNECT_RECONNECT |
172988a678bbSBryant G. Ly 				      ERR_DISCONNECTED | UNDEFINED))) {
173088a678bbSBryant G. Ly 			dev_err(&vscsi->dev, "client crq full too long\n");
173188a678bbSBryant G. Ly 			ibmvscsis_post_disconnect(vscsi,
173288a678bbSBryant G. Ly 						  ERR_DISCONNECT_RECONNECT,
173388a678bbSBryant G. Ly 						  0);
173488a678bbSBryant G. Ly 		}
173588a678bbSBryant G. Ly 	}
173688a678bbSBryant G. Ly }
173788a678bbSBryant G. Ly 
173888a678bbSBryant G. Ly /**
173988a678bbSBryant G. Ly  * ibmvscsis_send_messages() - Send a Response
174088a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
174188a678bbSBryant G. Ly  *
174288a678bbSBryant G. Ly  * Send a response, first checking the waiting queue. Responses are
174388a678bbSBryant G. Ly  * sent in order they are received. If the response cannot be sent,
174488a678bbSBryant G. Ly  * because the client queue is full, it stays on the waiting queue.
174588a678bbSBryant G. Ly  *
174688a678bbSBryant G. Ly  * PRECONDITION:
174788a678bbSBryant G. Ly  *	Called with interrupt lock held
174888a678bbSBryant G. Ly  */
174988a678bbSBryant G. Ly static void ibmvscsis_send_messages(struct scsi_info *vscsi)
175088a678bbSBryant G. Ly {
175188a678bbSBryant G. Ly 	u64 msg_hi = 0;
175288a678bbSBryant G. Ly 	/* note do not attmempt to access the IU_data_ptr with this pointer
175388a678bbSBryant G. Ly 	 * it is not valid
175488a678bbSBryant G. Ly 	 */
175588a678bbSBryant G. Ly 	struct viosrp_crq *crq = (struct viosrp_crq *)&msg_hi;
175688a678bbSBryant G. Ly 	struct ibmvscsis_cmd *cmd, *nxt;
175788a678bbSBryant G. Ly 	struct iu_entry *iue;
175888a678bbSBryant G. Ly 	long rc = ADAPT_SUCCESS;
175988a678bbSBryant G. Ly 
176088a678bbSBryant G. Ly 	if (!(vscsi->flags & RESPONSE_Q_DOWN)) {
176188a678bbSBryant G. Ly 		list_for_each_entry_safe(cmd, nxt, &vscsi->waiting_rsp, list) {
176288a678bbSBryant G. Ly 			iue = cmd->iue;
176388a678bbSBryant G. Ly 
176488a678bbSBryant G. Ly 			crq->valid = VALID_CMD_RESP_EL;
176588a678bbSBryant G. Ly 			crq->format = cmd->rsp.format;
176688a678bbSBryant G. Ly 
176788a678bbSBryant G. Ly 			if (cmd->flags & CMD_FAST_FAIL)
176888a678bbSBryant G. Ly 				crq->status = VIOSRP_ADAPTER_FAIL;
176988a678bbSBryant G. Ly 
177088a678bbSBryant G. Ly 			crq->IU_length = cpu_to_be16(cmd->rsp.len);
177188a678bbSBryant G. Ly 
177288a678bbSBryant G. Ly 			rc = h_send_crq(vscsi->dma_dev->unit_address,
177388a678bbSBryant G. Ly 					be64_to_cpu(msg_hi),
177488a678bbSBryant G. Ly 					be64_to_cpu(cmd->rsp.tag));
177588a678bbSBryant G. Ly 
1776c9b3379fSMichael Cyr 			pr_debug("send_messages: cmd %p, tag 0x%llx, rc %ld\n",
1777c9b3379fSMichael Cyr 				 cmd, be64_to_cpu(cmd->rsp.tag), rc);
177888a678bbSBryant G. Ly 
177988a678bbSBryant G. Ly 			/* if all ok free up the command element resources */
178088a678bbSBryant G. Ly 			if (rc == H_SUCCESS) {
178188a678bbSBryant G. Ly 				/* some movement has occurred */
178288a678bbSBryant G. Ly 				vscsi->rsp_q_timer.timer_pops = 0;
178388a678bbSBryant G. Ly 				list_del(&cmd->list);
178488a678bbSBryant G. Ly 
178588a678bbSBryant G. Ly 				ibmvscsis_free_cmd_resources(vscsi, cmd);
178688a678bbSBryant G. Ly 			} else {
178788a678bbSBryant G. Ly 				srp_snd_msg_failed(vscsi, rc);
178888a678bbSBryant G. Ly 				break;
178988a678bbSBryant G. Ly 			}
179088a678bbSBryant G. Ly 		}
179188a678bbSBryant G. Ly 
179288a678bbSBryant G. Ly 		if (!rc) {
179388a678bbSBryant G. Ly 			/*
179488a678bbSBryant G. Ly 			 * The timer could pop with the queue empty.  If
179588a678bbSBryant G. Ly 			 * this happens, rc will always indicate a
179688a678bbSBryant G. Ly 			 * success; clear the pop count.
179788a678bbSBryant G. Ly 			 */
179888a678bbSBryant G. Ly 			vscsi->rsp_q_timer.timer_pops = 0;
179988a678bbSBryant G. Ly 		}
180088a678bbSBryant G. Ly 	} else {
180188a678bbSBryant G. Ly 		ibmvscsis_free_cmd_qs(vscsi);
180288a678bbSBryant G. Ly 	}
180388a678bbSBryant G. Ly }
180488a678bbSBryant G. Ly 
180588a678bbSBryant G. Ly /* Called with intr lock held */
180688a678bbSBryant G. Ly static void ibmvscsis_send_mad_resp(struct scsi_info *vscsi,
180788a678bbSBryant G. Ly 				    struct ibmvscsis_cmd *cmd,
180888a678bbSBryant G. Ly 				    struct viosrp_crq *crq)
180988a678bbSBryant G. Ly {
181088a678bbSBryant G. Ly 	struct iu_entry *iue = cmd->iue;
181188a678bbSBryant G. Ly 	struct mad_common *mad = (struct mad_common *)&vio_iu(iue)->mad;
181288a678bbSBryant G. Ly 	uint flag_bits = 0;
181388a678bbSBryant G. Ly 	long rc;
181488a678bbSBryant G. Ly 
181588a678bbSBryant G. Ly 	dma_wmb();
181688a678bbSBryant G. Ly 	rc = h_copy_rdma(sizeof(struct mad_common),
181788a678bbSBryant G. Ly 			 vscsi->dds.window[LOCAL].liobn, iue->sbuf->dma,
181888a678bbSBryant G. Ly 			 vscsi->dds.window[REMOTE].liobn,
181988a678bbSBryant G. Ly 			 be64_to_cpu(crq->IU_data_ptr));
182088a678bbSBryant G. Ly 	if (!rc) {
182188a678bbSBryant G. Ly 		cmd->rsp.format = VIOSRP_MAD_FORMAT;
182288a678bbSBryant G. Ly 		cmd->rsp.len = sizeof(struct mad_common);
182388a678bbSBryant G. Ly 		cmd->rsp.tag = mad->tag;
182488a678bbSBryant G. Ly 		list_add_tail(&cmd->list, &vscsi->waiting_rsp);
182588a678bbSBryant G. Ly 		ibmvscsis_send_messages(vscsi);
182688a678bbSBryant G. Ly 	} else {
182788a678bbSBryant G. Ly 		pr_debug("Error sending mad response, rc %ld\n", rc);
182888a678bbSBryant G. Ly 		if (rc == H_PERMISSION) {
182988a678bbSBryant G. Ly 			if (connection_broken(vscsi))
183088a678bbSBryant G. Ly 				flag_bits = (RESPONSE_Q_DOWN | CLIENT_FAILED);
183188a678bbSBryant G. Ly 		}
183288a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "mad: failed to copy to client, rc %ld\n",
183388a678bbSBryant G. Ly 			rc);
183488a678bbSBryant G. Ly 
183588a678bbSBryant G. Ly 		ibmvscsis_free_cmd_resources(vscsi, cmd);
183688a678bbSBryant G. Ly 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
183788a678bbSBryant G. Ly 					  flag_bits);
183888a678bbSBryant G. Ly 	}
183988a678bbSBryant G. Ly }
184088a678bbSBryant G. Ly 
184188a678bbSBryant G. Ly /**
184288a678bbSBryant G. Ly  * ibmvscsis_mad() - Service a MAnagement Data gram.
184388a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
184488a678bbSBryant G. Ly  * @crq:	Pointer to the CRQ entry containing the MAD request
184588a678bbSBryant G. Ly  *
184688a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
184779fac9c9SMichael Cyr  *	Interrupt, called with adapter lock held
184888a678bbSBryant G. Ly  */
184988a678bbSBryant G. Ly static long ibmvscsis_mad(struct scsi_info *vscsi, struct viosrp_crq *crq)
185088a678bbSBryant G. Ly {
185188a678bbSBryant G. Ly 	struct iu_entry *iue;
185288a678bbSBryant G. Ly 	struct ibmvscsis_cmd *cmd;
185388a678bbSBryant G. Ly 	struct mad_common *mad;
185488a678bbSBryant G. Ly 	long rc = ADAPT_SUCCESS;
185588a678bbSBryant G. Ly 
185688a678bbSBryant G. Ly 	switch (vscsi->state) {
185788a678bbSBryant G. Ly 		/*
185888a678bbSBryant G. Ly 		 * We have not exchanged Init Msgs yet, so this MAD was sent
185988a678bbSBryant G. Ly 		 * before the last Transport Event; client will not be
186088a678bbSBryant G. Ly 		 * expecting a response.
186188a678bbSBryant G. Ly 		 */
186288a678bbSBryant G. Ly 	case WAIT_CONNECTION:
186388a678bbSBryant G. Ly 		pr_debug("mad: in Wait Connection state, ignoring MAD, flags %d\n",
186488a678bbSBryant G. Ly 			 vscsi->flags);
186588a678bbSBryant G. Ly 		return ADAPT_SUCCESS;
186688a678bbSBryant G. Ly 
186788a678bbSBryant G. Ly 	case SRP_PROCESSING:
186888a678bbSBryant G. Ly 	case CONNECTED:
186988a678bbSBryant G. Ly 		break;
187088a678bbSBryant G. Ly 
187188a678bbSBryant G. Ly 		/*
187288a678bbSBryant G. Ly 		 * We should never get here while we're in these states.
187388a678bbSBryant G. Ly 		 * Just log an error and get out.
187488a678bbSBryant G. Ly 		 */
187588a678bbSBryant G. Ly 	case UNCONFIGURING:
187688a678bbSBryant G. Ly 	case WAIT_IDLE:
187788a678bbSBryant G. Ly 	case ERR_DISCONNECT:
187888a678bbSBryant G. Ly 	case ERR_DISCONNECT_RECONNECT:
187988a678bbSBryant G. Ly 	default:
188088a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "mad: invalid adapter state %d for mad\n",
188188a678bbSBryant G. Ly 			vscsi->state);
188288a678bbSBryant G. Ly 		return ADAPT_SUCCESS;
188388a678bbSBryant G. Ly 	}
188488a678bbSBryant G. Ly 
188588a678bbSBryant G. Ly 	cmd = ibmvscsis_get_free_cmd(vscsi);
188688a678bbSBryant G. Ly 	if (!cmd) {
188788a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "mad: failed to get cmd, debit %d\n",
188888a678bbSBryant G. Ly 			vscsi->debit);
188988a678bbSBryant G. Ly 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
189088a678bbSBryant G. Ly 		return ERROR;
189188a678bbSBryant G. Ly 	}
189288a678bbSBryant G. Ly 	iue = cmd->iue;
189388a678bbSBryant G. Ly 	cmd->type = ADAPTER_MAD;
189488a678bbSBryant G. Ly 
189588a678bbSBryant G. Ly 	rc = ibmvscsis_copy_crq_packet(vscsi, cmd, crq);
189688a678bbSBryant G. Ly 	if (!rc) {
189788a678bbSBryant G. Ly 		mad = (struct mad_common *)&vio_iu(iue)->mad;
189888a678bbSBryant G. Ly 
189988a678bbSBryant G. Ly 		pr_debug("mad: type %d\n", be32_to_cpu(mad->type));
190088a678bbSBryant G. Ly 
190188a678bbSBryant G. Ly 		rc = ibmvscsis_process_mad(vscsi, iue);
190288a678bbSBryant G. Ly 
190388a678bbSBryant G. Ly 		pr_debug("mad: status %hd, rc %ld\n", be16_to_cpu(mad->status),
190488a678bbSBryant G. Ly 			 rc);
190588a678bbSBryant G. Ly 
190688a678bbSBryant G. Ly 		if (!rc)
190788a678bbSBryant G. Ly 			ibmvscsis_send_mad_resp(vscsi, cmd, crq);
190888a678bbSBryant G. Ly 	} else {
190988a678bbSBryant G. Ly 		ibmvscsis_free_cmd_resources(vscsi, cmd);
191088a678bbSBryant G. Ly 	}
191188a678bbSBryant G. Ly 
191288a678bbSBryant G. Ly 	pr_debug("Leaving mad, rc %ld\n", rc);
191388a678bbSBryant G. Ly 	return rc;
191488a678bbSBryant G. Ly }
191588a678bbSBryant G. Ly 
191688a678bbSBryant G. Ly /**
191788a678bbSBryant G. Ly  * ibmvscsis_login_rsp() - Create/copy a login response notice to the client
191888a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
191988a678bbSBryant G. Ly  * @cmd:	Pointer to the command for the SRP Login request
192088a678bbSBryant G. Ly  *
192188a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
192288a678bbSBryant G. Ly  *	Interrupt, interrupt lock held
192388a678bbSBryant G. Ly  */
192488a678bbSBryant G. Ly static long ibmvscsis_login_rsp(struct scsi_info *vscsi,
192588a678bbSBryant G. Ly 				struct ibmvscsis_cmd *cmd)
192688a678bbSBryant G. Ly {
192788a678bbSBryant G. Ly 	struct iu_entry *iue = cmd->iue;
192888a678bbSBryant G. Ly 	struct srp_login_rsp *rsp = &vio_iu(iue)->srp.login_rsp;
192988a678bbSBryant G. Ly 	struct format_code *fmt;
193088a678bbSBryant G. Ly 	uint flag_bits = 0;
193188a678bbSBryant G. Ly 	long rc = ADAPT_SUCCESS;
193288a678bbSBryant G. Ly 
193388a678bbSBryant G. Ly 	memset(rsp, 0, sizeof(struct srp_login_rsp));
193488a678bbSBryant G. Ly 
193588a678bbSBryant G. Ly 	rsp->opcode = SRP_LOGIN_RSP;
193688a678bbSBryant G. Ly 	rsp->req_lim_delta = cpu_to_be32(vscsi->request_limit);
193788a678bbSBryant G. Ly 	rsp->tag = cmd->rsp.tag;
193888a678bbSBryant G. Ly 	rsp->max_it_iu_len = cpu_to_be32(SRP_MAX_IU_LEN);
193988a678bbSBryant G. Ly 	rsp->max_ti_iu_len = cpu_to_be32(SRP_MAX_IU_LEN);
194088a678bbSBryant G. Ly 	fmt = (struct format_code *)&rsp->buf_fmt;
194188a678bbSBryant G. Ly 	fmt->buffers = SUPPORTED_FORMATS;
194288a678bbSBryant G. Ly 	vscsi->credit = 0;
194388a678bbSBryant G. Ly 
194488a678bbSBryant G. Ly 	cmd->rsp.len = sizeof(struct srp_login_rsp);
194588a678bbSBryant G. Ly 
194688a678bbSBryant G. Ly 	dma_wmb();
194788a678bbSBryant G. Ly 	rc = h_copy_rdma(cmd->rsp.len, vscsi->dds.window[LOCAL].liobn,
194888a678bbSBryant G. Ly 			 iue->sbuf->dma, vscsi->dds.window[REMOTE].liobn,
194988a678bbSBryant G. Ly 			 be64_to_cpu(iue->remote_token));
195088a678bbSBryant G. Ly 
195188a678bbSBryant G. Ly 	switch (rc) {
195288a678bbSBryant G. Ly 	case H_SUCCESS:
195388a678bbSBryant G. Ly 		break;
195488a678bbSBryant G. Ly 
195588a678bbSBryant G. Ly 	case H_PERMISSION:
195688a678bbSBryant G. Ly 		if (connection_broken(vscsi))
195788a678bbSBryant G. Ly 			flag_bits = RESPONSE_Q_DOWN | CLIENT_FAILED;
195888a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "login_rsp: error copying to client, rc %ld\n",
195988a678bbSBryant G. Ly 			rc);
196088a678bbSBryant G. Ly 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
196188a678bbSBryant G. Ly 					  flag_bits);
196288a678bbSBryant G. Ly 		break;
196388a678bbSBryant G. Ly 	case H_SOURCE_PARM:
196488a678bbSBryant G. Ly 	case H_DEST_PARM:
196588a678bbSBryant G. Ly 	default:
196688a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "login_rsp: error copying to client, rc %ld\n",
196788a678bbSBryant G. Ly 			rc);
196888a678bbSBryant G. Ly 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
196988a678bbSBryant G. Ly 		break;
197088a678bbSBryant G. Ly 	}
197188a678bbSBryant G. Ly 
197288a678bbSBryant G. Ly 	return rc;
197388a678bbSBryant G. Ly }
197488a678bbSBryant G. Ly 
197588a678bbSBryant G. Ly /**
197688a678bbSBryant G. Ly  * ibmvscsis_srp_login_rej() - Create/copy a login rejection notice to client
197788a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
197888a678bbSBryant G. Ly  * @cmd:	Pointer to the command for the SRP Login request
197988a678bbSBryant G. Ly  * @reason:	The reason the SRP Login is being rejected, per SRP protocol
198088a678bbSBryant G. Ly  *
198188a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
198288a678bbSBryant G. Ly  *	Interrupt, interrupt lock held
198388a678bbSBryant G. Ly  */
198488a678bbSBryant G. Ly static long ibmvscsis_srp_login_rej(struct scsi_info *vscsi,
198588a678bbSBryant G. Ly 				    struct ibmvscsis_cmd *cmd, u32 reason)
198688a678bbSBryant G. Ly {
198788a678bbSBryant G. Ly 	struct iu_entry *iue = cmd->iue;
198888a678bbSBryant G. Ly 	struct srp_login_rej *rej = &vio_iu(iue)->srp.login_rej;
198988a678bbSBryant G. Ly 	struct format_code *fmt;
199088a678bbSBryant G. Ly 	uint flag_bits = 0;
199188a678bbSBryant G. Ly 	long rc = ADAPT_SUCCESS;
199288a678bbSBryant G. Ly 
199388a678bbSBryant G. Ly 	memset(rej, 0, sizeof(*rej));
199488a678bbSBryant G. Ly 
199588a678bbSBryant G. Ly 	rej->opcode = SRP_LOGIN_REJ;
199688a678bbSBryant G. Ly 	rej->reason = cpu_to_be32(reason);
199788a678bbSBryant G. Ly 	rej->tag = cmd->rsp.tag;
199888a678bbSBryant G. Ly 	fmt = (struct format_code *)&rej->buf_fmt;
199988a678bbSBryant G. Ly 	fmt->buffers = SUPPORTED_FORMATS;
200088a678bbSBryant G. Ly 
200188a678bbSBryant G. Ly 	cmd->rsp.len = sizeof(*rej);
200288a678bbSBryant G. Ly 
200388a678bbSBryant G. Ly 	dma_wmb();
200488a678bbSBryant G. Ly 	rc = h_copy_rdma(cmd->rsp.len, vscsi->dds.window[LOCAL].liobn,
200588a678bbSBryant G. Ly 			 iue->sbuf->dma, vscsi->dds.window[REMOTE].liobn,
200688a678bbSBryant G. Ly 			 be64_to_cpu(iue->remote_token));
200788a678bbSBryant G. Ly 
200888a678bbSBryant G. Ly 	switch (rc) {
200988a678bbSBryant G. Ly 	case H_SUCCESS:
201088a678bbSBryant G. Ly 		break;
201188a678bbSBryant G. Ly 	case H_PERMISSION:
201288a678bbSBryant G. Ly 		if (connection_broken(vscsi))
201388a678bbSBryant G. Ly 			flag_bits = RESPONSE_Q_DOWN | CLIENT_FAILED;
201488a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "login_rej: error copying to client, rc %ld\n",
201588a678bbSBryant G. Ly 			rc);
201688a678bbSBryant G. Ly 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
201788a678bbSBryant G. Ly 					  flag_bits);
201888a678bbSBryant G. Ly 		break;
201988a678bbSBryant G. Ly 	case H_SOURCE_PARM:
202088a678bbSBryant G. Ly 	case H_DEST_PARM:
202188a678bbSBryant G. Ly 	default:
202288a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "login_rej: error copying to client, rc %ld\n",
202388a678bbSBryant G. Ly 			rc);
202488a678bbSBryant G. Ly 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
202588a678bbSBryant G. Ly 		break;
202688a678bbSBryant G. Ly 	}
202788a678bbSBryant G. Ly 
202888a678bbSBryant G. Ly 	return rc;
202988a678bbSBryant G. Ly }
203088a678bbSBryant G. Ly 
203188a678bbSBryant G. Ly static int ibmvscsis_make_nexus(struct ibmvscsis_tport *tport)
203288a678bbSBryant G. Ly {
203388a678bbSBryant G. Ly 	char *name = tport->tport_name;
203488a678bbSBryant G. Ly 	struct ibmvscsis_nexus *nexus;
203588a678bbSBryant G. Ly 	int rc;
203688a678bbSBryant G. Ly 
203788a678bbSBryant G. Ly 	if (tport->ibmv_nexus) {
203888a678bbSBryant G. Ly 		pr_debug("tport->ibmv_nexus already exists\n");
203988a678bbSBryant G. Ly 		return 0;
204088a678bbSBryant G. Ly 	}
204188a678bbSBryant G. Ly 
204288a678bbSBryant G. Ly 	nexus = kzalloc(sizeof(*nexus), GFP_KERNEL);
204388a678bbSBryant G. Ly 	if (!nexus) {
204488a678bbSBryant G. Ly 		pr_err("Unable to allocate struct ibmvscsis_nexus\n");
204588a678bbSBryant G. Ly 		return -ENOMEM;
204688a678bbSBryant G. Ly 	}
204788a678bbSBryant G. Ly 
204888a678bbSBryant G. Ly 	nexus->se_sess = target_alloc_session(&tport->se_tpg, 0, 0,
204988a678bbSBryant G. Ly 					      TARGET_PROT_NORMAL, name, nexus,
205088a678bbSBryant G. Ly 					      NULL);
205188a678bbSBryant G. Ly 	if (IS_ERR(nexus->se_sess)) {
205288a678bbSBryant G. Ly 		rc = PTR_ERR(nexus->se_sess);
205388a678bbSBryant G. Ly 		goto transport_init_fail;
205488a678bbSBryant G. Ly 	}
205588a678bbSBryant G. Ly 
205688a678bbSBryant G. Ly 	tport->ibmv_nexus = nexus;
205788a678bbSBryant G. Ly 
205888a678bbSBryant G. Ly 	return 0;
205988a678bbSBryant G. Ly 
206088a678bbSBryant G. Ly transport_init_fail:
206188a678bbSBryant G. Ly 	kfree(nexus);
206288a678bbSBryant G. Ly 	return rc;
206388a678bbSBryant G. Ly }
206488a678bbSBryant G. Ly 
206588a678bbSBryant G. Ly static int ibmvscsis_drop_nexus(struct ibmvscsis_tport *tport)
206688a678bbSBryant G. Ly {
206788a678bbSBryant G. Ly 	struct se_session *se_sess;
206888a678bbSBryant G. Ly 	struct ibmvscsis_nexus *nexus;
206988a678bbSBryant G. Ly 
207088a678bbSBryant G. Ly 	nexus = tport->ibmv_nexus;
207188a678bbSBryant G. Ly 	if (!nexus)
207288a678bbSBryant G. Ly 		return -ENODEV;
207388a678bbSBryant G. Ly 
207488a678bbSBryant G. Ly 	se_sess = nexus->se_sess;
207588a678bbSBryant G. Ly 	if (!se_sess)
207688a678bbSBryant G. Ly 		return -ENODEV;
207788a678bbSBryant G. Ly 
207888a678bbSBryant G. Ly 	/*
207988a678bbSBryant G. Ly 	 * Release the SCSI I_T Nexus to the emulated ibmvscsis Target Port
208088a678bbSBryant G. Ly 	 */
2081712db3ebSBryant G. Ly 	target_wait_for_sess_cmds(se_sess);
2082712db3ebSBryant G. Ly 	transport_deregister_session_configfs(se_sess);
208388a678bbSBryant G. Ly 	transport_deregister_session(se_sess);
208488a678bbSBryant G. Ly 	tport->ibmv_nexus = NULL;
208588a678bbSBryant G. Ly 	kfree(nexus);
208688a678bbSBryant G. Ly 
208788a678bbSBryant G. Ly 	return 0;
208888a678bbSBryant G. Ly }
208988a678bbSBryant G. Ly 
209088a678bbSBryant G. Ly /**
209188a678bbSBryant G. Ly  * ibmvscsis_srp_login() - Process an SRP Login Request
209288a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
209388a678bbSBryant G. Ly  * @cmd:	Command element to use to process the SRP Login request
209488a678bbSBryant G. Ly  * @crq:	Pointer to CRQ entry containing the SRP Login request
209588a678bbSBryant G. Ly  *
209688a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
209788a678bbSBryant G. Ly  *	Interrupt, called with interrupt lock held
209888a678bbSBryant G. Ly  */
209988a678bbSBryant G. Ly static long ibmvscsis_srp_login(struct scsi_info *vscsi,
210088a678bbSBryant G. Ly 				struct ibmvscsis_cmd *cmd,
210188a678bbSBryant G. Ly 				struct viosrp_crq *crq)
210288a678bbSBryant G. Ly {
210388a678bbSBryant G. Ly 	struct iu_entry *iue = cmd->iue;
210488a678bbSBryant G. Ly 	struct srp_login_req *req = &vio_iu(iue)->srp.login_req;
210588a678bbSBryant G. Ly 	struct port_id {
210688a678bbSBryant G. Ly 		__be64 id_extension;
210788a678bbSBryant G. Ly 		__be64 io_guid;
210888a678bbSBryant G. Ly 	} *iport, *tport;
210988a678bbSBryant G. Ly 	struct format_code *fmt;
211088a678bbSBryant G. Ly 	u32 reason = 0x0;
211188a678bbSBryant G. Ly 	long rc = ADAPT_SUCCESS;
211288a678bbSBryant G. Ly 
211388a678bbSBryant G. Ly 	iport = (struct port_id *)req->initiator_port_id;
211488a678bbSBryant G. Ly 	tport = (struct port_id *)req->target_port_id;
211588a678bbSBryant G. Ly 	fmt = (struct format_code *)&req->req_buf_fmt;
211688a678bbSBryant G. Ly 	if (be32_to_cpu(req->req_it_iu_len) > SRP_MAX_IU_LEN)
211788a678bbSBryant G. Ly 		reason = SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE;
211888a678bbSBryant G. Ly 	else if (be32_to_cpu(req->req_it_iu_len) < 64)
211988a678bbSBryant G. Ly 		reason = SRP_LOGIN_REJ_UNABLE_ESTABLISH_CHANNEL;
212088a678bbSBryant G. Ly 	else if ((be64_to_cpu(iport->id_extension) > (MAX_NUM_PORTS - 1)) ||
212188a678bbSBryant G. Ly 		 (be64_to_cpu(tport->id_extension) > (MAX_NUM_PORTS - 1)))
212288a678bbSBryant G. Ly 		reason = SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL;
212388a678bbSBryant G. Ly 	else if (req->req_flags & SRP_MULTICHAN_MULTI)
212488a678bbSBryant G. Ly 		reason = SRP_LOGIN_REJ_MULTI_CHANNEL_UNSUPPORTED;
212588a678bbSBryant G. Ly 	else if (fmt->buffers & (~SUPPORTED_FORMATS))
212688a678bbSBryant G. Ly 		reason = SRP_LOGIN_REJ_UNSUPPORTED_DESCRIPTOR_FMT;
2127f6dbe38eSBryant G. Ly 	else if ((fmt->buffers & SUPPORTED_FORMATS) == 0)
212888a678bbSBryant G. Ly 		reason = SRP_LOGIN_REJ_UNSUPPORTED_DESCRIPTOR_FMT;
212988a678bbSBryant G. Ly 
213088a678bbSBryant G. Ly 	if (vscsi->state == SRP_PROCESSING)
213188a678bbSBryant G. Ly 		reason = SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED;
213288a678bbSBryant G. Ly 
213388a678bbSBryant G. Ly 	rc = ibmvscsis_make_nexus(&vscsi->tport);
213488a678bbSBryant G. Ly 	if (rc)
213588a678bbSBryant G. Ly 		reason = SRP_LOGIN_REJ_UNABLE_ESTABLISH_CHANNEL;
213688a678bbSBryant G. Ly 
213788a678bbSBryant G. Ly 	cmd->rsp.format = VIOSRP_SRP_FORMAT;
213888a678bbSBryant G. Ly 	cmd->rsp.tag = req->tag;
213988a678bbSBryant G. Ly 
214088a678bbSBryant G. Ly 	pr_debug("srp_login: reason 0x%x\n", reason);
214188a678bbSBryant G. Ly 
214288a678bbSBryant G. Ly 	if (reason)
214388a678bbSBryant G. Ly 		rc = ibmvscsis_srp_login_rej(vscsi, cmd, reason);
214488a678bbSBryant G. Ly 	else
214588a678bbSBryant G. Ly 		rc = ibmvscsis_login_rsp(vscsi, cmd);
214688a678bbSBryant G. Ly 
214788a678bbSBryant G. Ly 	if (!rc) {
214888a678bbSBryant G. Ly 		if (!reason)
214988a678bbSBryant G. Ly 			vscsi->state = SRP_PROCESSING;
215088a678bbSBryant G. Ly 
215188a678bbSBryant G. Ly 		list_add_tail(&cmd->list, &vscsi->waiting_rsp);
215288a678bbSBryant G. Ly 		ibmvscsis_send_messages(vscsi);
215388a678bbSBryant G. Ly 	} else {
215488a678bbSBryant G. Ly 		ibmvscsis_free_cmd_resources(vscsi, cmd);
215588a678bbSBryant G. Ly 	}
215688a678bbSBryant G. Ly 
215788a678bbSBryant G. Ly 	pr_debug("Leaving srp_login, rc %ld\n", rc);
215888a678bbSBryant G. Ly 	return rc;
215988a678bbSBryant G. Ly }
216088a678bbSBryant G. Ly 
216188a678bbSBryant G. Ly /**
216288a678bbSBryant G. Ly  * ibmvscsis_srp_i_logout() - Helper Function to close I_T Nexus
216388a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
216488a678bbSBryant G. Ly  * @cmd:	Command element to use to process the Implicit Logout request
216588a678bbSBryant G. Ly  * @crq:	Pointer to CRQ entry containing the Implicit Logout request
216688a678bbSBryant G. Ly  *
216788a678bbSBryant G. Ly  * Do the logic to close the I_T nexus.  This function may not
216888a678bbSBryant G. Ly  * behave to specification.
216988a678bbSBryant G. Ly  *
217088a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
217188a678bbSBryant G. Ly  *	Interrupt, interrupt lock held
217288a678bbSBryant G. Ly  */
217388a678bbSBryant G. Ly static long ibmvscsis_srp_i_logout(struct scsi_info *vscsi,
217488a678bbSBryant G. Ly 				   struct ibmvscsis_cmd *cmd,
217588a678bbSBryant G. Ly 				   struct viosrp_crq *crq)
217688a678bbSBryant G. Ly {
217788a678bbSBryant G. Ly 	struct iu_entry *iue = cmd->iue;
217888a678bbSBryant G. Ly 	struct srp_i_logout *log_out = &vio_iu(iue)->srp.i_logout;
217988a678bbSBryant G. Ly 	long rc = ADAPT_SUCCESS;
218088a678bbSBryant G. Ly 
218188a678bbSBryant G. Ly 	if ((vscsi->debit > 0) || !list_empty(&vscsi->schedule_q) ||
218288a678bbSBryant G. Ly 	    !list_empty(&vscsi->waiting_rsp)) {
218388a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "i_logout: outstanding work\n");
218488a678bbSBryant G. Ly 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
218588a678bbSBryant G. Ly 	} else {
218688a678bbSBryant G. Ly 		cmd->rsp.format = SRP_FORMAT;
218788a678bbSBryant G. Ly 		cmd->rsp.tag = log_out->tag;
218888a678bbSBryant G. Ly 		cmd->rsp.len = sizeof(struct mad_common);
218988a678bbSBryant G. Ly 		list_add_tail(&cmd->list, &vscsi->waiting_rsp);
219088a678bbSBryant G. Ly 		ibmvscsis_send_messages(vscsi);
219188a678bbSBryant G. Ly 
219288a678bbSBryant G. Ly 		ibmvscsis_post_disconnect(vscsi, WAIT_IDLE, 0);
219388a678bbSBryant G. Ly 	}
219488a678bbSBryant G. Ly 
219588a678bbSBryant G. Ly 	return rc;
219688a678bbSBryant G. Ly }
219788a678bbSBryant G. Ly 
219888a678bbSBryant G. Ly /* Called with intr lock held */
219988a678bbSBryant G. Ly static void ibmvscsis_srp_cmd(struct scsi_info *vscsi, struct viosrp_crq *crq)
220088a678bbSBryant G. Ly {
220188a678bbSBryant G. Ly 	struct ibmvscsis_cmd *cmd;
220288a678bbSBryant G. Ly 	struct iu_entry *iue;
220388a678bbSBryant G. Ly 	struct srp_cmd *srp;
220488a678bbSBryant G. Ly 	struct srp_tsk_mgmt *tsk;
220588a678bbSBryant G. Ly 	long rc;
220688a678bbSBryant G. Ly 
220788a678bbSBryant G. Ly 	if (vscsi->request_limit - vscsi->debit <= 0) {
220888a678bbSBryant G. Ly 		/* Client has exceeded request limit */
220988a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "Client exceeded the request limit (%d), debit %d\n",
221088a678bbSBryant G. Ly 			vscsi->request_limit, vscsi->debit);
221188a678bbSBryant G. Ly 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
221288a678bbSBryant G. Ly 		return;
221388a678bbSBryant G. Ly 	}
221488a678bbSBryant G. Ly 
221588a678bbSBryant G. Ly 	cmd = ibmvscsis_get_free_cmd(vscsi);
221688a678bbSBryant G. Ly 	if (!cmd) {
221788a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "srp_cmd failed to get cmd, debit %d\n",
221888a678bbSBryant G. Ly 			vscsi->debit);
221988a678bbSBryant G. Ly 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
222088a678bbSBryant G. Ly 		return;
222188a678bbSBryant G. Ly 	}
222288a678bbSBryant G. Ly 	iue = cmd->iue;
222388a678bbSBryant G. Ly 	srp = &vio_iu(iue)->srp.cmd;
222488a678bbSBryant G. Ly 
222588a678bbSBryant G. Ly 	rc = ibmvscsis_copy_crq_packet(vscsi, cmd, crq);
222688a678bbSBryant G. Ly 	if (rc) {
222788a678bbSBryant G. Ly 		ibmvscsis_free_cmd_resources(vscsi, cmd);
222888a678bbSBryant G. Ly 		return;
222988a678bbSBryant G. Ly 	}
223088a678bbSBryant G. Ly 
223188a678bbSBryant G. Ly 	if (vscsi->state == SRP_PROCESSING) {
223288a678bbSBryant G. Ly 		switch (srp->opcode) {
223388a678bbSBryant G. Ly 		case SRP_LOGIN_REQ:
223488a678bbSBryant G. Ly 			rc = ibmvscsis_srp_login(vscsi, cmd, crq);
223588a678bbSBryant G. Ly 			break;
223688a678bbSBryant G. Ly 
223788a678bbSBryant G. Ly 		case SRP_TSK_MGMT:
223888a678bbSBryant G. Ly 			tsk = &vio_iu(iue)->srp.tsk_mgmt;
223988a678bbSBryant G. Ly 			pr_debug("tsk_mgmt tag: %llu (0x%llx)\n", tsk->tag,
224088a678bbSBryant G. Ly 				 tsk->tag);
224188a678bbSBryant G. Ly 			cmd->rsp.tag = tsk->tag;
224288a678bbSBryant G. Ly 			vscsi->debit += 1;
224388a678bbSBryant G. Ly 			cmd->type = TASK_MANAGEMENT;
224488a678bbSBryant G. Ly 			list_add_tail(&cmd->list, &vscsi->schedule_q);
224588a678bbSBryant G. Ly 			queue_work(vscsi->work_q, &cmd->work);
224688a678bbSBryant G. Ly 			break;
224788a678bbSBryant G. Ly 
224888a678bbSBryant G. Ly 		case SRP_CMD:
224988a678bbSBryant G. Ly 			pr_debug("srp_cmd tag: %llu (0x%llx)\n", srp->tag,
225088a678bbSBryant G. Ly 				 srp->tag);
225188a678bbSBryant G. Ly 			cmd->rsp.tag = srp->tag;
225288a678bbSBryant G. Ly 			vscsi->debit += 1;
225388a678bbSBryant G. Ly 			cmd->type = SCSI_CDB;
225488a678bbSBryant G. Ly 			/*
225588a678bbSBryant G. Ly 			 * We want to keep track of work waiting for
225688a678bbSBryant G. Ly 			 * the workqueue.
225788a678bbSBryant G. Ly 			 */
225888a678bbSBryant G. Ly 			list_add_tail(&cmd->list, &vscsi->schedule_q);
225988a678bbSBryant G. Ly 			queue_work(vscsi->work_q, &cmd->work);
226088a678bbSBryant G. Ly 			break;
226188a678bbSBryant G. Ly 
226288a678bbSBryant G. Ly 		case SRP_I_LOGOUT:
226388a678bbSBryant G. Ly 			rc = ibmvscsis_srp_i_logout(vscsi, cmd, crq);
226488a678bbSBryant G. Ly 			break;
226588a678bbSBryant G. Ly 
226688a678bbSBryant G. Ly 		case SRP_CRED_RSP:
226788a678bbSBryant G. Ly 		case SRP_AER_RSP:
226888a678bbSBryant G. Ly 		default:
226988a678bbSBryant G. Ly 			ibmvscsis_free_cmd_resources(vscsi, cmd);
227088a678bbSBryant G. Ly 			dev_err(&vscsi->dev, "invalid srp cmd, opcode %d\n",
227188a678bbSBryant G. Ly 				(uint)srp->opcode);
227288a678bbSBryant G. Ly 			ibmvscsis_post_disconnect(vscsi,
227388a678bbSBryant G. Ly 						  ERR_DISCONNECT_RECONNECT, 0);
227488a678bbSBryant G. Ly 			break;
227588a678bbSBryant G. Ly 		}
227688a678bbSBryant G. Ly 	} else if (srp->opcode == SRP_LOGIN_REQ && vscsi->state == CONNECTED) {
227788a678bbSBryant G. Ly 		rc = ibmvscsis_srp_login(vscsi, cmd, crq);
227888a678bbSBryant G. Ly 	} else {
227988a678bbSBryant G. Ly 		ibmvscsis_free_cmd_resources(vscsi, cmd);
228088a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "Invalid state %d to handle srp cmd\n",
228188a678bbSBryant G. Ly 			vscsi->state);
228288a678bbSBryant G. Ly 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
228388a678bbSBryant G. Ly 	}
228488a678bbSBryant G. Ly }
228588a678bbSBryant G. Ly 
228688a678bbSBryant G. Ly /**
228788a678bbSBryant G. Ly  * ibmvscsis_ping_response() - Respond to a ping request
228888a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
228988a678bbSBryant G. Ly  *
229088a678bbSBryant G. Ly  * Let the client know that the server is alive and waiting on
229188a678bbSBryant G. Ly  * its native I/O stack.
229288a678bbSBryant G. Ly  * If any type of error occurs from the call to queue a ping
229388a678bbSBryant G. Ly  * response then the client is either not accepting or receiving
229488a678bbSBryant G. Ly  * interrupts.  Disconnect with an error.
229588a678bbSBryant G. Ly  *
229688a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
229788a678bbSBryant G. Ly  *	Interrupt, interrupt lock held
229888a678bbSBryant G. Ly  */
229988a678bbSBryant G. Ly static long ibmvscsis_ping_response(struct scsi_info *vscsi)
230088a678bbSBryant G. Ly {
230188a678bbSBryant G. Ly 	struct viosrp_crq *crq;
230288a678bbSBryant G. Ly 	u64 buffer[2] = { 0, 0 };
230388a678bbSBryant G. Ly 	long rc;
230488a678bbSBryant G. Ly 
230588a678bbSBryant G. Ly 	crq = (struct viosrp_crq *)&buffer;
230688a678bbSBryant G. Ly 	crq->valid = VALID_CMD_RESP_EL;
230788a678bbSBryant G. Ly 	crq->format = (u8)MESSAGE_IN_CRQ;
230888a678bbSBryant G. Ly 	crq->status = PING_RESPONSE;
230988a678bbSBryant G. Ly 
231088a678bbSBryant G. Ly 	rc = h_send_crq(vscsi->dds.unit_id, cpu_to_be64(buffer[MSG_HI]),
231188a678bbSBryant G. Ly 			cpu_to_be64(buffer[MSG_LOW]));
231288a678bbSBryant G. Ly 
231388a678bbSBryant G. Ly 	switch (rc) {
231488a678bbSBryant G. Ly 	case H_SUCCESS:
231588a678bbSBryant G. Ly 		break;
231688a678bbSBryant G. Ly 	case H_CLOSED:
231788a678bbSBryant G. Ly 		vscsi->flags |= CLIENT_FAILED;
231888a678bbSBryant G. Ly 	case H_DROPPED:
231988a678bbSBryant G. Ly 		vscsi->flags |= RESPONSE_Q_DOWN;
232088a678bbSBryant G. Ly 	case H_REMOTE_PARM:
232188a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "ping_response: h_send_crq failed, rc %ld\n",
232288a678bbSBryant G. Ly 			rc);
232388a678bbSBryant G. Ly 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
232488a678bbSBryant G. Ly 		break;
232588a678bbSBryant G. Ly 	default:
232688a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "ping_response: h_send_crq returned unknown rc %ld\n",
232788a678bbSBryant G. Ly 			rc);
232888a678bbSBryant G. Ly 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
232988a678bbSBryant G. Ly 		break;
233088a678bbSBryant G. Ly 	}
233188a678bbSBryant G. Ly 
233288a678bbSBryant G. Ly 	return rc;
233388a678bbSBryant G. Ly }
233488a678bbSBryant G. Ly 
233588a678bbSBryant G. Ly /**
233688a678bbSBryant G. Ly  * ibmvscsis_parse_command() - Parse an element taken from the cmd rsp queue.
233788a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
233888a678bbSBryant G. Ly  * @crq:	Pointer to CRQ element containing the SRP request
233988a678bbSBryant G. Ly  *
234088a678bbSBryant G. Ly  * This function will return success if the command queue element is valid
234188a678bbSBryant G. Ly  * and the srp iu or MAD request it pointed to was also valid.  That does
234288a678bbSBryant G. Ly  * not mean that an error was not returned to the client.
234388a678bbSBryant G. Ly  *
234488a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
234588a678bbSBryant G. Ly  *	Interrupt, intr lock held
234688a678bbSBryant G. Ly  */
234788a678bbSBryant G. Ly static long ibmvscsis_parse_command(struct scsi_info *vscsi,
234888a678bbSBryant G. Ly 				    struct viosrp_crq *crq)
234988a678bbSBryant G. Ly {
235088a678bbSBryant G. Ly 	long rc = ADAPT_SUCCESS;
235188a678bbSBryant G. Ly 
235288a678bbSBryant G. Ly 	switch (crq->valid) {
235388a678bbSBryant G. Ly 	case VALID_CMD_RESP_EL:
235488a678bbSBryant G. Ly 		switch (crq->format) {
235588a678bbSBryant G. Ly 		case OS400_FORMAT:
235688a678bbSBryant G. Ly 		case AIX_FORMAT:
235788a678bbSBryant G. Ly 		case LINUX_FORMAT:
235888a678bbSBryant G. Ly 		case MAD_FORMAT:
235988a678bbSBryant G. Ly 			if (vscsi->flags & PROCESSING_MAD) {
236088a678bbSBryant G. Ly 				rc = ERROR;
236188a678bbSBryant G. Ly 				dev_err(&vscsi->dev, "parse_command: already processing mad\n");
236288a678bbSBryant G. Ly 				ibmvscsis_post_disconnect(vscsi,
236388a678bbSBryant G. Ly 						       ERR_DISCONNECT_RECONNECT,
236488a678bbSBryant G. Ly 						       0);
236588a678bbSBryant G. Ly 			} else {
236688a678bbSBryant G. Ly 				vscsi->flags |= PROCESSING_MAD;
236788a678bbSBryant G. Ly 				rc = ibmvscsis_mad(vscsi, crq);
236888a678bbSBryant G. Ly 			}
236988a678bbSBryant G. Ly 			break;
237088a678bbSBryant G. Ly 
237188a678bbSBryant G. Ly 		case SRP_FORMAT:
237288a678bbSBryant G. Ly 			ibmvscsis_srp_cmd(vscsi, crq);
237388a678bbSBryant G. Ly 			break;
237488a678bbSBryant G. Ly 
237588a678bbSBryant G. Ly 		case MESSAGE_IN_CRQ:
237688a678bbSBryant G. Ly 			if (crq->status == PING)
237788a678bbSBryant G. Ly 				ibmvscsis_ping_response(vscsi);
237888a678bbSBryant G. Ly 			break;
237988a678bbSBryant G. Ly 
238088a678bbSBryant G. Ly 		default:
238188a678bbSBryant G. Ly 			dev_err(&vscsi->dev, "parse_command: invalid format %d\n",
238288a678bbSBryant G. Ly 				(uint)crq->format);
238388a678bbSBryant G. Ly 			ibmvscsis_post_disconnect(vscsi,
238488a678bbSBryant G. Ly 						  ERR_DISCONNECT_RECONNECT, 0);
238588a678bbSBryant G. Ly 			break;
238688a678bbSBryant G. Ly 		}
238788a678bbSBryant G. Ly 		break;
238888a678bbSBryant G. Ly 
238988a678bbSBryant G. Ly 	case VALID_TRANS_EVENT:
239088a678bbSBryant G. Ly 		rc = ibmvscsis_trans_event(vscsi, crq);
239188a678bbSBryant G. Ly 		break;
239288a678bbSBryant G. Ly 
239388a678bbSBryant G. Ly 	case VALID_INIT_MSG:
239488a678bbSBryant G. Ly 		rc = ibmvscsis_init_msg(vscsi, crq);
239588a678bbSBryant G. Ly 		break;
239688a678bbSBryant G. Ly 
239788a678bbSBryant G. Ly 	default:
239888a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "parse_command: invalid valid field %d\n",
239988a678bbSBryant G. Ly 			(uint)crq->valid);
240088a678bbSBryant G. Ly 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
240188a678bbSBryant G. Ly 		break;
240288a678bbSBryant G. Ly 	}
240388a678bbSBryant G. Ly 
240488a678bbSBryant G. Ly 	/*
240588a678bbSBryant G. Ly 	 * Return only what the interrupt handler cares
240688a678bbSBryant G. Ly 	 * about. Most errors we keep right on trucking.
240788a678bbSBryant G. Ly 	 */
240888a678bbSBryant G. Ly 	rc = vscsi->flags & SCHEDULE_DISCONNECT;
240988a678bbSBryant G. Ly 
241088a678bbSBryant G. Ly 	return rc;
241188a678bbSBryant G. Ly }
241288a678bbSBryant G. Ly 
241388a678bbSBryant G. Ly static int read_dma_window(struct scsi_info *vscsi)
241488a678bbSBryant G. Ly {
241588a678bbSBryant G. Ly 	struct vio_dev *vdev = vscsi->dma_dev;
241688a678bbSBryant G. Ly 	const __be32 *dma_window;
241788a678bbSBryant G. Ly 	const __be32 *prop;
241888a678bbSBryant G. Ly 
241988a678bbSBryant G. Ly 	/* TODO Using of_parse_dma_window would be better, but it doesn't give
242088a678bbSBryant G. Ly 	 * a way to read multiple windows without already knowing the size of
242188a678bbSBryant G. Ly 	 * a window or the number of windows.
242288a678bbSBryant G. Ly 	 */
242388a678bbSBryant G. Ly 	dma_window = (const __be32 *)vio_get_attribute(vdev,
242488a678bbSBryant G. Ly 						       "ibm,my-dma-window",
242588a678bbSBryant G. Ly 						       NULL);
242688a678bbSBryant G. Ly 	if (!dma_window) {
242788a678bbSBryant G. Ly 		pr_err("Couldn't find ibm,my-dma-window property\n");
242888a678bbSBryant G. Ly 		return -1;
242988a678bbSBryant G. Ly 	}
243088a678bbSBryant G. Ly 
243188a678bbSBryant G. Ly 	vscsi->dds.window[LOCAL].liobn = be32_to_cpu(*dma_window);
243288a678bbSBryant G. Ly 	dma_window++;
243388a678bbSBryant G. Ly 
243488a678bbSBryant G. Ly 	prop = (const __be32 *)vio_get_attribute(vdev, "ibm,#dma-address-cells",
243588a678bbSBryant G. Ly 						 NULL);
243688a678bbSBryant G. Ly 	if (!prop) {
243788a678bbSBryant G. Ly 		pr_warn("Couldn't find ibm,#dma-address-cells property\n");
243888a678bbSBryant G. Ly 		dma_window++;
243988a678bbSBryant G. Ly 	} else {
244088a678bbSBryant G. Ly 		dma_window += be32_to_cpu(*prop);
244188a678bbSBryant G. Ly 	}
244288a678bbSBryant G. Ly 
244388a678bbSBryant G. Ly 	prop = (const __be32 *)vio_get_attribute(vdev, "ibm,#dma-size-cells",
244488a678bbSBryant G. Ly 						 NULL);
244588a678bbSBryant G. Ly 	if (!prop) {
244688a678bbSBryant G. Ly 		pr_warn("Couldn't find ibm,#dma-size-cells property\n");
244788a678bbSBryant G. Ly 		dma_window++;
244888a678bbSBryant G. Ly 	} else {
244988a678bbSBryant G. Ly 		dma_window += be32_to_cpu(*prop);
245088a678bbSBryant G. Ly 	}
245188a678bbSBryant G. Ly 
245288a678bbSBryant G. Ly 	/* dma_window should point to the second window now */
245388a678bbSBryant G. Ly 	vscsi->dds.window[REMOTE].liobn = be32_to_cpu(*dma_window);
245488a678bbSBryant G. Ly 
245588a678bbSBryant G. Ly 	return 0;
245688a678bbSBryant G. Ly }
245788a678bbSBryant G. Ly 
245888a678bbSBryant G. Ly static struct ibmvscsis_tport *ibmvscsis_lookup_port(const char *name)
245988a678bbSBryant G. Ly {
246088a678bbSBryant G. Ly 	struct ibmvscsis_tport *tport = NULL;
246188a678bbSBryant G. Ly 	struct vio_dev *vdev;
246288a678bbSBryant G. Ly 	struct scsi_info *vscsi;
246388a678bbSBryant G. Ly 
246488a678bbSBryant G. Ly 	spin_lock_bh(&ibmvscsis_dev_lock);
246588a678bbSBryant G. Ly 	list_for_each_entry(vscsi, &ibmvscsis_dev_list, list) {
246688a678bbSBryant G. Ly 		vdev = vscsi->dma_dev;
246788a678bbSBryant G. Ly 		if (!strcmp(dev_name(&vdev->dev), name)) {
246888a678bbSBryant G. Ly 			tport = &vscsi->tport;
246988a678bbSBryant G. Ly 			break;
247088a678bbSBryant G. Ly 		}
247188a678bbSBryant G. Ly 	}
247288a678bbSBryant G. Ly 	spin_unlock_bh(&ibmvscsis_dev_lock);
247388a678bbSBryant G. Ly 
247488a678bbSBryant G. Ly 	return tport;
247588a678bbSBryant G. Ly }
247688a678bbSBryant G. Ly 
247788a678bbSBryant G. Ly /**
247888a678bbSBryant G. Ly  * ibmvscsis_parse_cmd() - Parse SRP Command
247988a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
248088a678bbSBryant G. Ly  * @cmd:	Pointer to command element with SRP command
248188a678bbSBryant G. Ly  *
248288a678bbSBryant G. Ly  * Parse the srp command; if it is valid then submit it to tcm.
248388a678bbSBryant G. Ly  * Note: The return code does not reflect the status of the SCSI CDB.
248488a678bbSBryant G. Ly  *
248588a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
248688a678bbSBryant G. Ly  *	Process level
248788a678bbSBryant G. Ly  */
248888a678bbSBryant G. Ly static void ibmvscsis_parse_cmd(struct scsi_info *vscsi,
248988a678bbSBryant G. Ly 				struct ibmvscsis_cmd *cmd)
249088a678bbSBryant G. Ly {
249188a678bbSBryant G. Ly 	struct iu_entry *iue = cmd->iue;
249288a678bbSBryant G. Ly 	struct srp_cmd *srp = (struct srp_cmd *)iue->sbuf->buf;
249388a678bbSBryant G. Ly 	struct ibmvscsis_nexus *nexus;
249488a678bbSBryant G. Ly 	u64 data_len = 0;
249588a678bbSBryant G. Ly 	enum dma_data_direction dir;
249688a678bbSBryant G. Ly 	int attr = 0;
249788a678bbSBryant G. Ly 	int rc = 0;
249888a678bbSBryant G. Ly 
249988a678bbSBryant G. Ly 	nexus = vscsi->tport.ibmv_nexus;
250088a678bbSBryant G. Ly 	/*
250188a678bbSBryant G. Ly 	 * additional length in bytes.  Note that the SRP spec says that
250288a678bbSBryant G. Ly 	 * additional length is in 4-byte words, but technically the
250388a678bbSBryant G. Ly 	 * additional length field is only the upper 6 bits of the byte.
250488a678bbSBryant G. Ly 	 * The lower 2 bits are reserved.  If the lower 2 bits are 0 (as
250588a678bbSBryant G. Ly 	 * all reserved fields should be), then interpreting the byte as
250688a678bbSBryant G. Ly 	 * an int will yield the length in bytes.
250788a678bbSBryant G. Ly 	 */
250888a678bbSBryant G. Ly 	if (srp->add_cdb_len & 0x03) {
250988a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "parse_cmd: reserved bits set in IU\n");
251088a678bbSBryant G. Ly 		spin_lock_bh(&vscsi->intr_lock);
251188a678bbSBryant G. Ly 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
251288a678bbSBryant G. Ly 		ibmvscsis_free_cmd_resources(vscsi, cmd);
251388a678bbSBryant G. Ly 		spin_unlock_bh(&vscsi->intr_lock);
251488a678bbSBryant G. Ly 		return;
251588a678bbSBryant G. Ly 	}
251688a678bbSBryant G. Ly 
251788a678bbSBryant G. Ly 	if (srp_get_desc_table(srp, &dir, &data_len)) {
251888a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "0x%llx: parsing SRP descriptor table failed.\n",
251988a678bbSBryant G. Ly 			srp->tag);
252088a678bbSBryant G. Ly 		goto fail;
252188a678bbSBryant G. Ly 	}
252288a678bbSBryant G. Ly 
252388a678bbSBryant G. Ly 	cmd->rsp.sol_not = srp->sol_not;
252488a678bbSBryant G. Ly 
252588a678bbSBryant G. Ly 	switch (srp->task_attr) {
252688a678bbSBryant G. Ly 	case SRP_SIMPLE_TASK:
252788a678bbSBryant G. Ly 		attr = TCM_SIMPLE_TAG;
252888a678bbSBryant G. Ly 		break;
252988a678bbSBryant G. Ly 	case SRP_ORDERED_TASK:
253088a678bbSBryant G. Ly 		attr = TCM_ORDERED_TAG;
253188a678bbSBryant G. Ly 		break;
253288a678bbSBryant G. Ly 	case SRP_HEAD_TASK:
253388a678bbSBryant G. Ly 		attr = TCM_HEAD_TAG;
253488a678bbSBryant G. Ly 		break;
253588a678bbSBryant G. Ly 	case SRP_ACA_TASK:
253688a678bbSBryant G. Ly 		attr = TCM_ACA_TAG;
253788a678bbSBryant G. Ly 		break;
253888a678bbSBryant G. Ly 	default:
253988a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "Invalid task attribute %d\n",
254088a678bbSBryant G. Ly 			srp->task_attr);
254188a678bbSBryant G. Ly 		goto fail;
254288a678bbSBryant G. Ly 	}
254388a678bbSBryant G. Ly 
254488a678bbSBryant G. Ly 	cmd->se_cmd.tag = be64_to_cpu(srp->tag);
254588a678bbSBryant G. Ly 
254688a678bbSBryant G. Ly 	spin_lock_bh(&vscsi->intr_lock);
254788a678bbSBryant G. Ly 	list_add_tail(&cmd->list, &vscsi->active_q);
254888a678bbSBryant G. Ly 	spin_unlock_bh(&vscsi->intr_lock);
254988a678bbSBryant G. Ly 
255088a678bbSBryant G. Ly 	srp->lun.scsi_lun[0] &= 0x3f;
255188a678bbSBryant G. Ly 
255288a678bbSBryant G. Ly 	rc = target_submit_cmd(&cmd->se_cmd, nexus->se_sess, srp->cdb,
255388a678bbSBryant G. Ly 			       cmd->sense_buf, scsilun_to_int(&srp->lun),
255488a678bbSBryant G. Ly 			       data_len, attr, dir, 0);
255588a678bbSBryant G. Ly 	if (rc) {
255688a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "target_submit_cmd failed, rc %d\n", rc);
25577435b32eSMichael Cyr 		spin_lock_bh(&vscsi->intr_lock);
25587435b32eSMichael Cyr 		list_del(&cmd->list);
25597435b32eSMichael Cyr 		ibmvscsis_free_cmd_resources(vscsi, cmd);
25607435b32eSMichael Cyr 		spin_unlock_bh(&vscsi->intr_lock);
256188a678bbSBryant G. Ly 		goto fail;
256288a678bbSBryant G. Ly 	}
256388a678bbSBryant G. Ly 	return;
256488a678bbSBryant G. Ly 
256588a678bbSBryant G. Ly fail:
256688a678bbSBryant G. Ly 	spin_lock_bh(&vscsi->intr_lock);
256788a678bbSBryant G. Ly 	ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
256888a678bbSBryant G. Ly 	spin_unlock_bh(&vscsi->intr_lock);
256988a678bbSBryant G. Ly }
257088a678bbSBryant G. Ly 
257188a678bbSBryant G. Ly /**
257288a678bbSBryant G. Ly  * ibmvscsis_parse_task() - Parse SRP Task Management Request
257388a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
257488a678bbSBryant G. Ly  * @cmd:	Pointer to command element with SRP task management request
257588a678bbSBryant G. Ly  *
257688a678bbSBryant G. Ly  * Parse the srp task management request; if it is valid then submit it to tcm.
257788a678bbSBryant G. Ly  * Note: The return code does not reflect the status of the task management
257888a678bbSBryant G. Ly  * request.
257988a678bbSBryant G. Ly  *
258088a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
258188a678bbSBryant G. Ly  *	Processor level
258288a678bbSBryant G. Ly  */
258388a678bbSBryant G. Ly static void ibmvscsis_parse_task(struct scsi_info *vscsi,
258488a678bbSBryant G. Ly 				 struct ibmvscsis_cmd *cmd)
258588a678bbSBryant G. Ly {
258688a678bbSBryant G. Ly 	struct iu_entry *iue = cmd->iue;
258788a678bbSBryant G. Ly 	struct srp_tsk_mgmt *srp_tsk = &vio_iu(iue)->srp.tsk_mgmt;
258888a678bbSBryant G. Ly 	int tcm_type;
258988a678bbSBryant G. Ly 	u64 tag_to_abort = 0;
259088a678bbSBryant G. Ly 	int rc = 0;
259188a678bbSBryant G. Ly 	struct ibmvscsis_nexus *nexus;
259288a678bbSBryant G. Ly 
259388a678bbSBryant G. Ly 	nexus = vscsi->tport.ibmv_nexus;
259488a678bbSBryant G. Ly 
259588a678bbSBryant G. Ly 	cmd->rsp.sol_not = srp_tsk->sol_not;
259688a678bbSBryant G. Ly 
259788a678bbSBryant G. Ly 	switch (srp_tsk->tsk_mgmt_func) {
259888a678bbSBryant G. Ly 	case SRP_TSK_ABORT_TASK:
259988a678bbSBryant G. Ly 		tcm_type = TMR_ABORT_TASK;
260088a678bbSBryant G. Ly 		tag_to_abort = be64_to_cpu(srp_tsk->task_tag);
260188a678bbSBryant G. Ly 		break;
260288a678bbSBryant G. Ly 	case SRP_TSK_ABORT_TASK_SET:
260388a678bbSBryant G. Ly 		tcm_type = TMR_ABORT_TASK_SET;
260488a678bbSBryant G. Ly 		break;
260588a678bbSBryant G. Ly 	case SRP_TSK_CLEAR_TASK_SET:
260688a678bbSBryant G. Ly 		tcm_type = TMR_CLEAR_TASK_SET;
260788a678bbSBryant G. Ly 		break;
260888a678bbSBryant G. Ly 	case SRP_TSK_LUN_RESET:
260988a678bbSBryant G. Ly 		tcm_type = TMR_LUN_RESET;
261088a678bbSBryant G. Ly 		break;
261188a678bbSBryant G. Ly 	case SRP_TSK_CLEAR_ACA:
261288a678bbSBryant G. Ly 		tcm_type = TMR_CLEAR_ACA;
261388a678bbSBryant G. Ly 		break;
261488a678bbSBryant G. Ly 	default:
261588a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "unknown task mgmt func %d\n",
261688a678bbSBryant G. Ly 			srp_tsk->tsk_mgmt_func);
261788a678bbSBryant G. Ly 		cmd->se_cmd.se_tmr_req->response =
261888a678bbSBryant G. Ly 			TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
261988a678bbSBryant G. Ly 		rc = -1;
262088a678bbSBryant G. Ly 		break;
262188a678bbSBryant G. Ly 	}
262288a678bbSBryant G. Ly 
262388a678bbSBryant G. Ly 	if (!rc) {
262488a678bbSBryant G. Ly 		cmd->se_cmd.tag = be64_to_cpu(srp_tsk->tag);
262588a678bbSBryant G. Ly 
262688a678bbSBryant G. Ly 		spin_lock_bh(&vscsi->intr_lock);
262788a678bbSBryant G. Ly 		list_add_tail(&cmd->list, &vscsi->active_q);
262888a678bbSBryant G. Ly 		spin_unlock_bh(&vscsi->intr_lock);
262988a678bbSBryant G. Ly 
263088a678bbSBryant G. Ly 		srp_tsk->lun.scsi_lun[0] &= 0x3f;
263188a678bbSBryant G. Ly 
263288a678bbSBryant G. Ly 		pr_debug("calling submit_tmr, func %d\n",
263388a678bbSBryant G. Ly 			 srp_tsk->tsk_mgmt_func);
263488a678bbSBryant G. Ly 		rc = target_submit_tmr(&cmd->se_cmd, nexus->se_sess, NULL,
263588a678bbSBryant G. Ly 				       scsilun_to_int(&srp_tsk->lun), srp_tsk,
263688a678bbSBryant G. Ly 				       tcm_type, GFP_KERNEL, tag_to_abort, 0);
263788a678bbSBryant G. Ly 		if (rc) {
263888a678bbSBryant G. Ly 			dev_err(&vscsi->dev, "target_submit_tmr failed, rc %d\n",
263988a678bbSBryant G. Ly 				rc);
26407435b32eSMichael Cyr 			spin_lock_bh(&vscsi->intr_lock);
26417435b32eSMichael Cyr 			list_del(&cmd->list);
26427435b32eSMichael Cyr 			spin_unlock_bh(&vscsi->intr_lock);
264388a678bbSBryant G. Ly 			cmd->se_cmd.se_tmr_req->response =
264488a678bbSBryant G. Ly 				TMR_FUNCTION_REJECTED;
264588a678bbSBryant G. Ly 		}
264688a678bbSBryant G. Ly 	}
264788a678bbSBryant G. Ly 
264888a678bbSBryant G. Ly 	if (rc)
264988a678bbSBryant G. Ly 		transport_send_check_condition_and_sense(&cmd->se_cmd, 0, 0);
265088a678bbSBryant G. Ly }
265188a678bbSBryant G. Ly 
265288a678bbSBryant G. Ly static void ibmvscsis_scheduler(struct work_struct *work)
265388a678bbSBryant G. Ly {
265488a678bbSBryant G. Ly 	struct ibmvscsis_cmd *cmd = container_of(work, struct ibmvscsis_cmd,
265588a678bbSBryant G. Ly 						 work);
265688a678bbSBryant G. Ly 	struct scsi_info *vscsi = cmd->adapter;
265788a678bbSBryant G. Ly 
265888a678bbSBryant G. Ly 	spin_lock_bh(&vscsi->intr_lock);
265988a678bbSBryant G. Ly 
266088a678bbSBryant G. Ly 	/* Remove from schedule_q */
266188a678bbSBryant G. Ly 	list_del(&cmd->list);
266288a678bbSBryant G. Ly 
266388a678bbSBryant G. Ly 	/* Don't submit cmd if we're disconnecting */
266488a678bbSBryant G. Ly 	if (vscsi->flags & (SCHEDULE_DISCONNECT | DISCONNECT_SCHEDULED)) {
266588a678bbSBryant G. Ly 		ibmvscsis_free_cmd_resources(vscsi, cmd);
266688a678bbSBryant G. Ly 
266788a678bbSBryant G. Ly 		/* ibmvscsis_disconnect might be waiting for us */
266888a678bbSBryant G. Ly 		if (list_empty(&vscsi->active_q) &&
266988a678bbSBryant G. Ly 		    list_empty(&vscsi->schedule_q) &&
267088a678bbSBryant G. Ly 		    (vscsi->flags & WAIT_FOR_IDLE)) {
267188a678bbSBryant G. Ly 			vscsi->flags &= ~WAIT_FOR_IDLE;
267288a678bbSBryant G. Ly 			complete(&vscsi->wait_idle);
267388a678bbSBryant G. Ly 		}
267488a678bbSBryant G. Ly 
267588a678bbSBryant G. Ly 		spin_unlock_bh(&vscsi->intr_lock);
267688a678bbSBryant G. Ly 		return;
267788a678bbSBryant G. Ly 	}
267888a678bbSBryant G. Ly 
267988a678bbSBryant G. Ly 	spin_unlock_bh(&vscsi->intr_lock);
268088a678bbSBryant G. Ly 
268188a678bbSBryant G. Ly 	switch (cmd->type) {
268288a678bbSBryant G. Ly 	case SCSI_CDB:
268388a678bbSBryant G. Ly 		ibmvscsis_parse_cmd(vscsi, cmd);
268488a678bbSBryant G. Ly 		break;
268588a678bbSBryant G. Ly 	case TASK_MANAGEMENT:
268688a678bbSBryant G. Ly 		ibmvscsis_parse_task(vscsi, cmd);
268788a678bbSBryant G. Ly 		break;
268888a678bbSBryant G. Ly 	default:
268988a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "scheduler, invalid cmd type %d\n",
269088a678bbSBryant G. Ly 			cmd->type);
269188a678bbSBryant G. Ly 		spin_lock_bh(&vscsi->intr_lock);
269288a678bbSBryant G. Ly 		ibmvscsis_free_cmd_resources(vscsi, cmd);
269388a678bbSBryant G. Ly 		spin_unlock_bh(&vscsi->intr_lock);
269488a678bbSBryant G. Ly 		break;
269588a678bbSBryant G. Ly 	}
269688a678bbSBryant G. Ly }
269788a678bbSBryant G. Ly 
269888a678bbSBryant G. Ly static int ibmvscsis_alloc_cmds(struct scsi_info *vscsi, int num)
269988a678bbSBryant G. Ly {
270088a678bbSBryant G. Ly 	struct ibmvscsis_cmd *cmd;
270188a678bbSBryant G. Ly 	int i;
270288a678bbSBryant G. Ly 
270388a678bbSBryant G. Ly 	INIT_LIST_HEAD(&vscsi->free_cmd);
270488a678bbSBryant G. Ly 	vscsi->cmd_pool = kcalloc(num, sizeof(struct ibmvscsis_cmd),
270588a678bbSBryant G. Ly 				  GFP_KERNEL);
270688a678bbSBryant G. Ly 	if (!vscsi->cmd_pool)
270788a678bbSBryant G. Ly 		return -ENOMEM;
270888a678bbSBryant G. Ly 
270988a678bbSBryant G. Ly 	for (i = 0, cmd = (struct ibmvscsis_cmd *)vscsi->cmd_pool; i < num;
271088a678bbSBryant G. Ly 	     i++, cmd++) {
271188a678bbSBryant G. Ly 		cmd->adapter = vscsi;
271288a678bbSBryant G. Ly 		INIT_WORK(&cmd->work, ibmvscsis_scheduler);
271388a678bbSBryant G. Ly 		list_add_tail(&cmd->list, &vscsi->free_cmd);
271488a678bbSBryant G. Ly 	}
271588a678bbSBryant G. Ly 
271688a678bbSBryant G. Ly 	return 0;
271788a678bbSBryant G. Ly }
271888a678bbSBryant G. Ly 
271988a678bbSBryant G. Ly static void ibmvscsis_free_cmds(struct scsi_info *vscsi)
272088a678bbSBryant G. Ly {
272188a678bbSBryant G. Ly 	kfree(vscsi->cmd_pool);
272288a678bbSBryant G. Ly 	vscsi->cmd_pool = NULL;
272388a678bbSBryant G. Ly 	INIT_LIST_HEAD(&vscsi->free_cmd);
272488a678bbSBryant G. Ly }
272588a678bbSBryant G. Ly 
272688a678bbSBryant G. Ly /**
272788a678bbSBryant G. Ly  * ibmvscsis_service_wait_q() - Service Waiting Queue
272888a678bbSBryant G. Ly  * @timer:	Pointer to timer which has expired
272988a678bbSBryant G. Ly  *
273088a678bbSBryant G. Ly  * This routine is called when the timer pops to service the waiting
273188a678bbSBryant G. Ly  * queue. Elements on the queue have completed, their responses have been
273288a678bbSBryant G. Ly  * copied to the client, but the client's response queue was full so
273388a678bbSBryant G. Ly  * the queue message could not be sent. The routine grabs the proper locks
273488a678bbSBryant G. Ly  * and calls send messages.
273588a678bbSBryant G. Ly  *
273688a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
273788a678bbSBryant G. Ly  *	called at interrupt level
273888a678bbSBryant G. Ly  */
273988a678bbSBryant G. Ly static enum hrtimer_restart ibmvscsis_service_wait_q(struct hrtimer *timer)
274088a678bbSBryant G. Ly {
274188a678bbSBryant G. Ly 	struct timer_cb *p_timer = container_of(timer, struct timer_cb, timer);
274288a678bbSBryant G. Ly 	struct scsi_info *vscsi = container_of(p_timer, struct scsi_info,
274388a678bbSBryant G. Ly 					       rsp_q_timer);
274488a678bbSBryant G. Ly 
274588a678bbSBryant G. Ly 	spin_lock_bh(&vscsi->intr_lock);
274688a678bbSBryant G. Ly 	p_timer->timer_pops += 1;
274788a678bbSBryant G. Ly 	p_timer->started = false;
274888a678bbSBryant G. Ly 	ibmvscsis_send_messages(vscsi);
274988a678bbSBryant G. Ly 	spin_unlock_bh(&vscsi->intr_lock);
275088a678bbSBryant G. Ly 
275188a678bbSBryant G. Ly 	return HRTIMER_NORESTART;
275288a678bbSBryant G. Ly }
275388a678bbSBryant G. Ly 
275488a678bbSBryant G. Ly static long ibmvscsis_alloctimer(struct scsi_info *vscsi)
275588a678bbSBryant G. Ly {
275688a678bbSBryant G. Ly 	struct timer_cb *p_timer;
275788a678bbSBryant G. Ly 
275888a678bbSBryant G. Ly 	p_timer = &vscsi->rsp_q_timer;
275988a678bbSBryant G. Ly 	hrtimer_init(&p_timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
276088a678bbSBryant G. Ly 
276188a678bbSBryant G. Ly 	p_timer->timer.function = ibmvscsis_service_wait_q;
276288a678bbSBryant G. Ly 	p_timer->started = false;
276388a678bbSBryant G. Ly 	p_timer->timer_pops = 0;
276488a678bbSBryant G. Ly 
276588a678bbSBryant G. Ly 	return ADAPT_SUCCESS;
276688a678bbSBryant G. Ly }
276788a678bbSBryant G. Ly 
276888a678bbSBryant G. Ly static void ibmvscsis_freetimer(struct scsi_info *vscsi)
276988a678bbSBryant G. Ly {
277088a678bbSBryant G. Ly 	struct timer_cb *p_timer;
277188a678bbSBryant G. Ly 
277288a678bbSBryant G. Ly 	p_timer = &vscsi->rsp_q_timer;
277388a678bbSBryant G. Ly 
277488a678bbSBryant G. Ly 	(void)hrtimer_cancel(&p_timer->timer);
277588a678bbSBryant G. Ly 
277688a678bbSBryant G. Ly 	p_timer->started = false;
277788a678bbSBryant G. Ly 	p_timer->timer_pops = 0;
277888a678bbSBryant G. Ly }
277988a678bbSBryant G. Ly 
278088a678bbSBryant G. Ly static irqreturn_t ibmvscsis_interrupt(int dummy, void *data)
278188a678bbSBryant G. Ly {
278288a678bbSBryant G. Ly 	struct scsi_info *vscsi = data;
278388a678bbSBryant G. Ly 
278488a678bbSBryant G. Ly 	vio_disable_interrupts(vscsi->dma_dev);
278588a678bbSBryant G. Ly 	tasklet_schedule(&vscsi->work_task);
278688a678bbSBryant G. Ly 
278788a678bbSBryant G. Ly 	return IRQ_HANDLED;
278888a678bbSBryant G. Ly }
278988a678bbSBryant G. Ly 
279088a678bbSBryant G. Ly /**
279188a678bbSBryant G. Ly  * ibmvscsis_enable_change_state() - Set new state based on enabled status
279288a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
279388a678bbSBryant G. Ly  *
279488a678bbSBryant G. Ly  * This function determines our new state now that we are enabled.  This
279588a678bbSBryant G. Ly  * may involve sending an Init Complete message to the client.
279688a678bbSBryant G. Ly  *
279788a678bbSBryant G. Ly  * Must be called with interrupt lock held.
279888a678bbSBryant G. Ly  */
279988a678bbSBryant G. Ly static long ibmvscsis_enable_change_state(struct scsi_info *vscsi)
280088a678bbSBryant G. Ly {
2801c9b3379fSMichael Cyr 	int bytes;
280288a678bbSBryant G. Ly 	long rc = ADAPT_SUCCESS;
280388a678bbSBryant G. Ly 
2804c9b3379fSMichael Cyr 	bytes = vscsi->cmd_q.size * PAGE_SIZE;
2805c9b3379fSMichael Cyr 	rc = h_reg_crq(vscsi->dds.unit_id, vscsi->cmd_q.crq_token, bytes);
2806c9b3379fSMichael Cyr 	if (rc == H_CLOSED || rc == H_SUCCESS) {
280788a678bbSBryant G. Ly 		vscsi->state = WAIT_CONNECTION;
2808c9b3379fSMichael Cyr 		rc = ibmvscsis_establish_new_q(vscsi);
280988a678bbSBryant G. Ly 	}
281088a678bbSBryant G. Ly 
2811c9b3379fSMichael Cyr 	if (rc != ADAPT_SUCCESS) {
2812c9b3379fSMichael Cyr 		vscsi->state = ERR_DISCONNECTED;
2813c9b3379fSMichael Cyr 		vscsi->flags |= RESPONSE_Q_DOWN;
281488a678bbSBryant G. Ly 	}
281588a678bbSBryant G. Ly 
281688a678bbSBryant G. Ly 	return rc;
281788a678bbSBryant G. Ly }
281888a678bbSBryant G. Ly 
281988a678bbSBryant G. Ly /**
282088a678bbSBryant G. Ly  * ibmvscsis_create_command_q() - Create Command Queue
282188a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
282288a678bbSBryant G. Ly  * @num_cmds:	Currently unused.  In the future, may be used to determine
282388a678bbSBryant G. Ly  *		the size of the CRQ.
282488a678bbSBryant G. Ly  *
282588a678bbSBryant G. Ly  * Allocates memory for command queue maps remote memory into an ioba
282688a678bbSBryant G. Ly  * initializes the command response queue
282788a678bbSBryant G. Ly  *
282888a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
282988a678bbSBryant G. Ly  *	Process level only
283088a678bbSBryant G. Ly  */
283188a678bbSBryant G. Ly static long ibmvscsis_create_command_q(struct scsi_info *vscsi, int num_cmds)
283288a678bbSBryant G. Ly {
283388a678bbSBryant G. Ly 	int pages;
283488a678bbSBryant G. Ly 	struct vio_dev *vdev = vscsi->dma_dev;
283588a678bbSBryant G. Ly 
283688a678bbSBryant G. Ly 	/* We might support multiple pages in the future, but just 1 for now */
283788a678bbSBryant G. Ly 	pages = 1;
283888a678bbSBryant G. Ly 
283988a678bbSBryant G. Ly 	vscsi->cmd_q.size = pages;
284088a678bbSBryant G. Ly 
284188a678bbSBryant G. Ly 	vscsi->cmd_q.base_addr =
284288a678bbSBryant G. Ly 		(struct viosrp_crq *)get_zeroed_page(GFP_KERNEL);
284388a678bbSBryant G. Ly 	if (!vscsi->cmd_q.base_addr)
284488a678bbSBryant G. Ly 		return -ENOMEM;
284588a678bbSBryant G. Ly 
284688a678bbSBryant G. Ly 	vscsi->cmd_q.mask = ((uint)pages * CRQ_PER_PAGE) - 1;
284788a678bbSBryant G. Ly 
284888a678bbSBryant G. Ly 	vscsi->cmd_q.crq_token = dma_map_single(&vdev->dev,
284988a678bbSBryant G. Ly 						vscsi->cmd_q.base_addr,
285088a678bbSBryant G. Ly 						PAGE_SIZE, DMA_BIDIRECTIONAL);
285188a678bbSBryant G. Ly 	if (dma_mapping_error(&vdev->dev, vscsi->cmd_q.crq_token)) {
285288a678bbSBryant G. Ly 		free_page((unsigned long)vscsi->cmd_q.base_addr);
285388a678bbSBryant G. Ly 		return -ENOMEM;
285488a678bbSBryant G. Ly 	}
285588a678bbSBryant G. Ly 
2856c9b3379fSMichael Cyr 	return 0;
285788a678bbSBryant G. Ly }
285888a678bbSBryant G. Ly 
285988a678bbSBryant G. Ly /**
286088a678bbSBryant G. Ly  * ibmvscsis_destroy_command_q - Destroy Command Queue
286188a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
286288a678bbSBryant G. Ly  *
286388a678bbSBryant G. Ly  * Releases memory for command queue and unmaps mapped remote memory.
286488a678bbSBryant G. Ly  *
286588a678bbSBryant G. Ly  * EXECUTION ENVIRONMENT:
286688a678bbSBryant G. Ly  *	Process level only
286788a678bbSBryant G. Ly  */
286888a678bbSBryant G. Ly static void ibmvscsis_destroy_command_q(struct scsi_info *vscsi)
286988a678bbSBryant G. Ly {
287088a678bbSBryant G. Ly 	dma_unmap_single(&vscsi->dma_dev->dev, vscsi->cmd_q.crq_token,
287188a678bbSBryant G. Ly 			 PAGE_SIZE, DMA_BIDIRECTIONAL);
287288a678bbSBryant G. Ly 	free_page((unsigned long)vscsi->cmd_q.base_addr);
287388a678bbSBryant G. Ly 	vscsi->cmd_q.base_addr = NULL;
287488a678bbSBryant G. Ly 	vscsi->state = NO_QUEUE;
287588a678bbSBryant G. Ly }
287688a678bbSBryant G. Ly 
287788a678bbSBryant G. Ly static u8 ibmvscsis_fast_fail(struct scsi_info *vscsi,
287888a678bbSBryant G. Ly 			      struct ibmvscsis_cmd *cmd)
287988a678bbSBryant G. Ly {
288088a678bbSBryant G. Ly 	struct iu_entry *iue = cmd->iue;
288188a678bbSBryant G. Ly 	struct se_cmd *se_cmd = &cmd->se_cmd;
288288a678bbSBryant G. Ly 	struct srp_cmd *srp = (struct srp_cmd *)iue->sbuf->buf;
288388a678bbSBryant G. Ly 	struct scsi_sense_hdr sshdr;
288488a678bbSBryant G. Ly 	u8 rc = se_cmd->scsi_status;
288588a678bbSBryant G. Ly 
288688a678bbSBryant G. Ly 	if (vscsi->fast_fail && (READ_CMD(srp->cdb) || WRITE_CMD(srp->cdb)))
288788a678bbSBryant G. Ly 		if (scsi_normalize_sense(se_cmd->sense_buffer,
288888a678bbSBryant G. Ly 					 se_cmd->scsi_sense_length, &sshdr))
288988a678bbSBryant G. Ly 			if (sshdr.sense_key == HARDWARE_ERROR &&
289088a678bbSBryant G. Ly 			    (se_cmd->residual_count == 0 ||
289188a678bbSBryant G. Ly 			     se_cmd->residual_count == se_cmd->data_length)) {
289288a678bbSBryant G. Ly 				rc = NO_SENSE;
289388a678bbSBryant G. Ly 				cmd->flags |= CMD_FAST_FAIL;
289488a678bbSBryant G. Ly 			}
289588a678bbSBryant G. Ly 
289688a678bbSBryant G. Ly 	return rc;
289788a678bbSBryant G. Ly }
289888a678bbSBryant G. Ly 
289988a678bbSBryant G. Ly /**
290088a678bbSBryant G. Ly  * srp_build_response() - Build an SRP response buffer
290188a678bbSBryant G. Ly  * @vscsi:	Pointer to our adapter structure
290288a678bbSBryant G. Ly  * @cmd:	Pointer to command for which to send the response
290388a678bbSBryant G. Ly  * @len_p:	Where to return the length of the IU response sent.  This
290488a678bbSBryant G. Ly  *		is needed to construct the CRQ response.
290588a678bbSBryant G. Ly  *
290688a678bbSBryant G. Ly  * Build the SRP response buffer and copy it to the client's memory space.
290788a678bbSBryant G. Ly  */
290888a678bbSBryant G. Ly static long srp_build_response(struct scsi_info *vscsi,
290988a678bbSBryant G. Ly 			       struct ibmvscsis_cmd *cmd, uint *len_p)
291088a678bbSBryant G. Ly {
291188a678bbSBryant G. Ly 	struct iu_entry *iue = cmd->iue;
291288a678bbSBryant G. Ly 	struct se_cmd *se_cmd = &cmd->se_cmd;
291388a678bbSBryant G. Ly 	struct srp_rsp *rsp;
291488a678bbSBryant G. Ly 	uint len;
291588a678bbSBryant G. Ly 	u32 rsp_code;
291688a678bbSBryant G. Ly 	char *data;
291788a678bbSBryant G. Ly 	u32 *tsk_status;
291888a678bbSBryant G. Ly 	long rc = ADAPT_SUCCESS;
291988a678bbSBryant G. Ly 
292088a678bbSBryant G. Ly 	spin_lock_bh(&vscsi->intr_lock);
292188a678bbSBryant G. Ly 
292288a678bbSBryant G. Ly 	rsp = &vio_iu(iue)->srp.rsp;
292388a678bbSBryant G. Ly 	len = sizeof(*rsp);
292488a678bbSBryant G. Ly 	memset(rsp, 0, len);
292588a678bbSBryant G. Ly 	data = rsp->data;
292688a678bbSBryant G. Ly 
292788a678bbSBryant G. Ly 	rsp->opcode = SRP_RSP;
292888a678bbSBryant G. Ly 
292988a678bbSBryant G. Ly 	if (vscsi->credit > 0 && vscsi->state == SRP_PROCESSING)
293088a678bbSBryant G. Ly 		rsp->req_lim_delta = cpu_to_be32(vscsi->credit);
293188a678bbSBryant G. Ly 	else
293288a678bbSBryant G. Ly 		rsp->req_lim_delta = cpu_to_be32(1 + vscsi->credit);
293388a678bbSBryant G. Ly 	rsp->tag = cmd->rsp.tag;
293488a678bbSBryant G. Ly 	rsp->flags = 0;
293588a678bbSBryant G. Ly 
293688a678bbSBryant G. Ly 	if (cmd->type == SCSI_CDB) {
293788a678bbSBryant G. Ly 		rsp->status = ibmvscsis_fast_fail(vscsi, cmd);
293888a678bbSBryant G. Ly 		if (rsp->status) {
293988a678bbSBryant G. Ly 			pr_debug("build_resp: cmd %p, scsi status %d\n", cmd,
294088a678bbSBryant G. Ly 				 (int)rsp->status);
294188a678bbSBryant G. Ly 			ibmvscsis_determine_resid(se_cmd, rsp);
294288a678bbSBryant G. Ly 			if (se_cmd->scsi_sense_length && se_cmd->sense_buffer) {
294388a678bbSBryant G. Ly 				rsp->sense_data_len =
294488a678bbSBryant G. Ly 					cpu_to_be32(se_cmd->scsi_sense_length);
294588a678bbSBryant G. Ly 				rsp->flags |= SRP_RSP_FLAG_SNSVALID;
294688a678bbSBryant G. Ly 				len += se_cmd->scsi_sense_length;
294788a678bbSBryant G. Ly 				memcpy(data, se_cmd->sense_buffer,
294888a678bbSBryant G. Ly 				       se_cmd->scsi_sense_length);
294988a678bbSBryant G. Ly 			}
295088a678bbSBryant G. Ly 			rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
295188a678bbSBryant G. Ly 				UCSOLNT_RESP_SHIFT;
295288a678bbSBryant G. Ly 		} else if (cmd->flags & CMD_FAST_FAIL) {
295388a678bbSBryant G. Ly 			pr_debug("build_resp: cmd %p, fast fail\n", cmd);
295488a678bbSBryant G. Ly 			rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
295588a678bbSBryant G. Ly 				UCSOLNT_RESP_SHIFT;
295688a678bbSBryant G. Ly 		} else {
295788a678bbSBryant G. Ly 			rsp->sol_not = (cmd->rsp.sol_not & SCSOLNT) >>
295888a678bbSBryant G. Ly 				SCSOLNT_RESP_SHIFT;
295988a678bbSBryant G. Ly 		}
296088a678bbSBryant G. Ly 	} else {
296188a678bbSBryant G. Ly 		/* this is task management */
296288a678bbSBryant G. Ly 		rsp->status = 0;
296388a678bbSBryant G. Ly 		rsp->resp_data_len = cpu_to_be32(4);
296488a678bbSBryant G. Ly 		rsp->flags |= SRP_RSP_FLAG_RSPVALID;
296588a678bbSBryant G. Ly 
296688a678bbSBryant G. Ly 		switch (se_cmd->se_tmr_req->response) {
296788a678bbSBryant G. Ly 		case TMR_FUNCTION_COMPLETE:
296888a678bbSBryant G. Ly 		case TMR_TASK_DOES_NOT_EXIST:
296988a678bbSBryant G. Ly 			rsp_code = SRP_TASK_MANAGEMENT_FUNCTION_COMPLETE;
297088a678bbSBryant G. Ly 			rsp->sol_not = (cmd->rsp.sol_not & SCSOLNT) >>
297188a678bbSBryant G. Ly 				SCSOLNT_RESP_SHIFT;
297288a678bbSBryant G. Ly 			break;
297388a678bbSBryant G. Ly 		case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
297488a678bbSBryant G. Ly 		case TMR_LUN_DOES_NOT_EXIST:
297588a678bbSBryant G. Ly 			rsp_code = SRP_TASK_MANAGEMENT_FUNCTION_NOT_SUPPORTED;
297688a678bbSBryant G. Ly 			rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
297788a678bbSBryant G. Ly 				UCSOLNT_RESP_SHIFT;
297888a678bbSBryant G. Ly 			break;
297988a678bbSBryant G. Ly 		case TMR_FUNCTION_FAILED:
298088a678bbSBryant G. Ly 		case TMR_FUNCTION_REJECTED:
298188a678bbSBryant G. Ly 		default:
298288a678bbSBryant G. Ly 			rsp_code = SRP_TASK_MANAGEMENT_FUNCTION_FAILED;
298388a678bbSBryant G. Ly 			rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
298488a678bbSBryant G. Ly 				UCSOLNT_RESP_SHIFT;
298588a678bbSBryant G. Ly 			break;
298688a678bbSBryant G. Ly 		}
298788a678bbSBryant G. Ly 
298888a678bbSBryant G. Ly 		tsk_status = (u32 *)data;
298988a678bbSBryant G. Ly 		*tsk_status = cpu_to_be32(rsp_code);
299088a678bbSBryant G. Ly 		data = (char *)(tsk_status + 1);
299188a678bbSBryant G. Ly 		len += 4;
299288a678bbSBryant G. Ly 	}
299388a678bbSBryant G. Ly 
299488a678bbSBryant G. Ly 	dma_wmb();
299588a678bbSBryant G. Ly 	rc = h_copy_rdma(len, vscsi->dds.window[LOCAL].liobn, iue->sbuf->dma,
299688a678bbSBryant G. Ly 			 vscsi->dds.window[REMOTE].liobn,
299788a678bbSBryant G. Ly 			 be64_to_cpu(iue->remote_token));
299888a678bbSBryant G. Ly 
299988a678bbSBryant G. Ly 	switch (rc) {
300088a678bbSBryant G. Ly 	case H_SUCCESS:
300188a678bbSBryant G. Ly 		vscsi->credit = 0;
300288a678bbSBryant G. Ly 		*len_p = len;
300388a678bbSBryant G. Ly 		break;
300488a678bbSBryant G. Ly 	case H_PERMISSION:
300588a678bbSBryant G. Ly 		if (connection_broken(vscsi))
300688a678bbSBryant G. Ly 			vscsi->flags |= RESPONSE_Q_DOWN | CLIENT_FAILED;
300788a678bbSBryant G. Ly 
300888a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "build_response: error copying to client, rc %ld, flags 0x%x, state 0x%hx\n",
300988a678bbSBryant G. Ly 			rc, vscsi->flags, vscsi->state);
301088a678bbSBryant G. Ly 		break;
301188a678bbSBryant G. Ly 	case H_SOURCE_PARM:
301288a678bbSBryant G. Ly 	case H_DEST_PARM:
301388a678bbSBryant G. Ly 	default:
301488a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "build_response: error copying to client, rc %ld\n",
301588a678bbSBryant G. Ly 			rc);
301688a678bbSBryant G. Ly 		break;
301788a678bbSBryant G. Ly 	}
301888a678bbSBryant G. Ly 
301988a678bbSBryant G. Ly 	spin_unlock_bh(&vscsi->intr_lock);
302088a678bbSBryant G. Ly 
302188a678bbSBryant G. Ly 	return rc;
302288a678bbSBryant G. Ly }
302388a678bbSBryant G. Ly 
302488a678bbSBryant G. Ly static int ibmvscsis_rdma(struct ibmvscsis_cmd *cmd, struct scatterlist *sg,
302588a678bbSBryant G. Ly 			  int nsg, struct srp_direct_buf *md, int nmd,
302688a678bbSBryant G. Ly 			  enum dma_data_direction dir, unsigned int bytes)
302788a678bbSBryant G. Ly {
302888a678bbSBryant G. Ly 	struct iu_entry *iue = cmd->iue;
302988a678bbSBryant G. Ly 	struct srp_target *target = iue->target;
303088a678bbSBryant G. Ly 	struct scsi_info *vscsi = target->ldata;
303188a678bbSBryant G. Ly 	struct scatterlist *sgp;
303288a678bbSBryant G. Ly 	dma_addr_t client_ioba, server_ioba;
303388a678bbSBryant G. Ly 	ulong buf_len;
303488a678bbSBryant G. Ly 	ulong client_len, server_len;
303588a678bbSBryant G. Ly 	int md_idx;
303688a678bbSBryant G. Ly 	long tx_len;
303788a678bbSBryant G. Ly 	long rc = 0;
303888a678bbSBryant G. Ly 
303988a678bbSBryant G. Ly 	if (bytes == 0)
304088a678bbSBryant G. Ly 		return 0;
304188a678bbSBryant G. Ly 
304288a678bbSBryant G. Ly 	sgp = sg;
304388a678bbSBryant G. Ly 	client_len = 0;
304488a678bbSBryant G. Ly 	server_len = 0;
304588a678bbSBryant G. Ly 	md_idx = 0;
304688a678bbSBryant G. Ly 	tx_len = bytes;
304788a678bbSBryant G. Ly 
304888a678bbSBryant G. Ly 	do {
304988a678bbSBryant G. Ly 		if (client_len == 0) {
305088a678bbSBryant G. Ly 			if (md_idx >= nmd) {
305188a678bbSBryant G. Ly 				dev_err(&vscsi->dev, "rdma: ran out of client memory descriptors\n");
305288a678bbSBryant G. Ly 				rc = -EIO;
305388a678bbSBryant G. Ly 				break;
305488a678bbSBryant G. Ly 			}
305588a678bbSBryant G. Ly 			client_ioba = be64_to_cpu(md[md_idx].va);
305688a678bbSBryant G. Ly 			client_len = be32_to_cpu(md[md_idx].len);
305788a678bbSBryant G. Ly 		}
305888a678bbSBryant G. Ly 		if (server_len == 0) {
305988a678bbSBryant G. Ly 			if (!sgp) {
306088a678bbSBryant G. Ly 				dev_err(&vscsi->dev, "rdma: ran out of scatter/gather list\n");
306188a678bbSBryant G. Ly 				rc = -EIO;
306288a678bbSBryant G. Ly 				break;
306388a678bbSBryant G. Ly 			}
306488a678bbSBryant G. Ly 			server_ioba = sg_dma_address(sgp);
306588a678bbSBryant G. Ly 			server_len = sg_dma_len(sgp);
306688a678bbSBryant G. Ly 		}
306788a678bbSBryant G. Ly 
306888a678bbSBryant G. Ly 		buf_len = tx_len;
306988a678bbSBryant G. Ly 
307088a678bbSBryant G. Ly 		if (buf_len > client_len)
307188a678bbSBryant G. Ly 			buf_len = client_len;
307288a678bbSBryant G. Ly 
307388a678bbSBryant G. Ly 		if (buf_len > server_len)
307488a678bbSBryant G. Ly 			buf_len = server_len;
307588a678bbSBryant G. Ly 
307688a678bbSBryant G. Ly 		if (buf_len > max_vdma_size)
307788a678bbSBryant G. Ly 			buf_len = max_vdma_size;
307888a678bbSBryant G. Ly 
307988a678bbSBryant G. Ly 		if (dir == DMA_TO_DEVICE) {
308088a678bbSBryant G. Ly 			/* read from client */
308188a678bbSBryant G. Ly 			rc = h_copy_rdma(buf_len,
308288a678bbSBryant G. Ly 					 vscsi->dds.window[REMOTE].liobn,
308388a678bbSBryant G. Ly 					 client_ioba,
308488a678bbSBryant G. Ly 					 vscsi->dds.window[LOCAL].liobn,
308588a678bbSBryant G. Ly 					 server_ioba);
308688a678bbSBryant G. Ly 		} else {
308788a678bbSBryant G. Ly 			/* The h_copy_rdma will cause phyp, running in another
308888a678bbSBryant G. Ly 			 * partition, to read memory, so we need to make sure
308988a678bbSBryant G. Ly 			 * the data has been written out, hence these syncs.
309088a678bbSBryant G. Ly 			 */
309188a678bbSBryant G. Ly 			/* ensure that everything is in memory */
309288a678bbSBryant G. Ly 			isync();
309388a678bbSBryant G. Ly 			/* ensure that memory has been made visible */
309488a678bbSBryant G. Ly 			dma_wmb();
309588a678bbSBryant G. Ly 			rc = h_copy_rdma(buf_len,
309688a678bbSBryant G. Ly 					 vscsi->dds.window[LOCAL].liobn,
309788a678bbSBryant G. Ly 					 server_ioba,
309888a678bbSBryant G. Ly 					 vscsi->dds.window[REMOTE].liobn,
309988a678bbSBryant G. Ly 					 client_ioba);
310088a678bbSBryant G. Ly 		}
310188a678bbSBryant G. Ly 		switch (rc) {
310288a678bbSBryant G. Ly 		case H_SUCCESS:
310388a678bbSBryant G. Ly 			break;
310488a678bbSBryant G. Ly 		case H_PERMISSION:
310588a678bbSBryant G. Ly 		case H_SOURCE_PARM:
310688a678bbSBryant G. Ly 		case H_DEST_PARM:
310788a678bbSBryant G. Ly 			if (connection_broken(vscsi)) {
310888a678bbSBryant G. Ly 				spin_lock_bh(&vscsi->intr_lock);
310988a678bbSBryant G. Ly 				vscsi->flags |=
311088a678bbSBryant G. Ly 					(RESPONSE_Q_DOWN | CLIENT_FAILED);
311188a678bbSBryant G. Ly 				spin_unlock_bh(&vscsi->intr_lock);
311288a678bbSBryant G. Ly 			}
311388a678bbSBryant G. Ly 			dev_err(&vscsi->dev, "rdma: h_copy_rdma failed, rc %ld\n",
311488a678bbSBryant G. Ly 				rc);
311588a678bbSBryant G. Ly 			break;
311688a678bbSBryant G. Ly 
311788a678bbSBryant G. Ly 		default:
311888a678bbSBryant G. Ly 			dev_err(&vscsi->dev, "rdma: unknown error %ld from h_copy_rdma\n",
311988a678bbSBryant G. Ly 				rc);
312088a678bbSBryant G. Ly 			break;
312188a678bbSBryant G. Ly 		}
312288a678bbSBryant G. Ly 
312388a678bbSBryant G. Ly 		if (!rc) {
312488a678bbSBryant G. Ly 			tx_len -= buf_len;
312588a678bbSBryant G. Ly 			if (tx_len) {
312688a678bbSBryant G. Ly 				client_len -= buf_len;
312788a678bbSBryant G. Ly 				if (client_len == 0)
312888a678bbSBryant G. Ly 					md_idx++;
312988a678bbSBryant G. Ly 				else
313088a678bbSBryant G. Ly 					client_ioba += buf_len;
313188a678bbSBryant G. Ly 
313288a678bbSBryant G. Ly 				server_len -= buf_len;
313388a678bbSBryant G. Ly 				if (server_len == 0)
313488a678bbSBryant G. Ly 					sgp = sg_next(sgp);
313588a678bbSBryant G. Ly 				else
313688a678bbSBryant G. Ly 					server_ioba += buf_len;
313788a678bbSBryant G. Ly 			} else {
313888a678bbSBryant G. Ly 				break;
313988a678bbSBryant G. Ly 			}
314088a678bbSBryant G. Ly 		}
314188a678bbSBryant G. Ly 	} while (!rc);
314288a678bbSBryant G. Ly 
314388a678bbSBryant G. Ly 	return rc;
314488a678bbSBryant G. Ly }
314588a678bbSBryant G. Ly 
314688a678bbSBryant G. Ly /**
314788a678bbSBryant G. Ly  * ibmvscsis_handle_crq() - Handle CRQ
314888a678bbSBryant G. Ly  * @data:	Pointer to our adapter structure
314988a678bbSBryant G. Ly  *
315088a678bbSBryant G. Ly  * Read the command elements from the command queue and copy the payloads
315188a678bbSBryant G. Ly  * associated with the command elements to local memory and execute the
315288a678bbSBryant G. Ly  * SRP requests.
315388a678bbSBryant G. Ly  *
315488a678bbSBryant G. Ly  * Note: this is an edge triggered interrupt. It can not be shared.
315588a678bbSBryant G. Ly  */
315688a678bbSBryant G. Ly static void ibmvscsis_handle_crq(unsigned long data)
315788a678bbSBryant G. Ly {
315888a678bbSBryant G. Ly 	struct scsi_info *vscsi = (struct scsi_info *)data;
315988a678bbSBryant G. Ly 	struct viosrp_crq *crq;
316088a678bbSBryant G. Ly 	long rc;
316188a678bbSBryant G. Ly 	bool ack = true;
316288a678bbSBryant G. Ly 	volatile u8 valid;
316388a678bbSBryant G. Ly 
316488a678bbSBryant G. Ly 	spin_lock_bh(&vscsi->intr_lock);
316588a678bbSBryant G. Ly 
316688a678bbSBryant G. Ly 	pr_debug("got interrupt\n");
316788a678bbSBryant G. Ly 
316888a678bbSBryant G. Ly 	/*
316988a678bbSBryant G. Ly 	 * if we are in a path where we are waiting for all pending commands
317088a678bbSBryant G. Ly 	 * to complete because we received a transport event and anything in
317188a678bbSBryant G. Ly 	 * the command queue is for a new connection, do nothing
317288a678bbSBryant G. Ly 	 */
317388a678bbSBryant G. Ly 	if (TARGET_STOP(vscsi)) {
317488a678bbSBryant G. Ly 		vio_enable_interrupts(vscsi->dma_dev);
317588a678bbSBryant G. Ly 
317688a678bbSBryant G. Ly 		pr_debug("handle_crq, don't process: flags 0x%x, state 0x%hx\n",
317788a678bbSBryant G. Ly 			 vscsi->flags, vscsi->state);
317888a678bbSBryant G. Ly 		spin_unlock_bh(&vscsi->intr_lock);
317988a678bbSBryant G. Ly 		return;
318088a678bbSBryant G. Ly 	}
318188a678bbSBryant G. Ly 
318288a678bbSBryant G. Ly 	rc = vscsi->flags & SCHEDULE_DISCONNECT;
318388a678bbSBryant G. Ly 	crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
318488a678bbSBryant G. Ly 	valid = crq->valid;
318588a678bbSBryant G. Ly 	dma_rmb();
318688a678bbSBryant G. Ly 
318788a678bbSBryant G. Ly 	while (valid) {
318888a678bbSBryant G. Ly 		/*
318988a678bbSBryant G. Ly 		 * These are edege triggered interrupts. After dropping out of
319088a678bbSBryant G. Ly 		 * the while loop, the code must check for work since an
319188a678bbSBryant G. Ly 		 * interrupt could be lost, and an elment be left on the queue,
319288a678bbSBryant G. Ly 		 * hence the label.
319388a678bbSBryant G. Ly 		 */
319488a678bbSBryant G. Ly cmd_work:
319588a678bbSBryant G. Ly 		vscsi->cmd_q.index =
319688a678bbSBryant G. Ly 			(vscsi->cmd_q.index + 1) & vscsi->cmd_q.mask;
319788a678bbSBryant G. Ly 
319888a678bbSBryant G. Ly 		if (!rc) {
319988a678bbSBryant G. Ly 			rc = ibmvscsis_parse_command(vscsi, crq);
320088a678bbSBryant G. Ly 		} else {
320188a678bbSBryant G. Ly 			if ((uint)crq->valid == VALID_TRANS_EVENT) {
320288a678bbSBryant G. Ly 				/*
320388a678bbSBryant G. Ly 				 * must service the transport layer events even
320488a678bbSBryant G. Ly 				 * in an error state, dont break out until all
320588a678bbSBryant G. Ly 				 * the consecutive transport events have been
320688a678bbSBryant G. Ly 				 * processed
320788a678bbSBryant G. Ly 				 */
320888a678bbSBryant G. Ly 				rc = ibmvscsis_trans_event(vscsi, crq);
320988a678bbSBryant G. Ly 			} else if (vscsi->flags & TRANS_EVENT) {
321088a678bbSBryant G. Ly 				/*
321181290215SBryant G. Ly 				 * if a transport event has occurred leave
321288a678bbSBryant G. Ly 				 * everything but transport events on the queue
321381290215SBryant G. Ly 				 *
321488a678bbSBryant G. Ly 				 * need to decrement the queue index so we can
321579fac9c9SMichael Cyr 				 * look at the element again
321688a678bbSBryant G. Ly 				 */
321788a678bbSBryant G. Ly 				if (vscsi->cmd_q.index)
321888a678bbSBryant G. Ly 					vscsi->cmd_q.index -= 1;
321988a678bbSBryant G. Ly 				else
322088a678bbSBryant G. Ly 					/*
322188a678bbSBryant G. Ly 					 * index is at 0 it just wrapped.
322288a678bbSBryant G. Ly 					 * have it index last element in q
322388a678bbSBryant G. Ly 					 */
322488a678bbSBryant G. Ly 					vscsi->cmd_q.index = vscsi->cmd_q.mask;
322588a678bbSBryant G. Ly 				break;
322688a678bbSBryant G. Ly 			}
322788a678bbSBryant G. Ly 		}
322888a678bbSBryant G. Ly 
322988a678bbSBryant G. Ly 		crq->valid = INVALIDATE_CMD_RESP_EL;
323088a678bbSBryant G. Ly 
323188a678bbSBryant G. Ly 		crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
323288a678bbSBryant G. Ly 		valid = crq->valid;
323388a678bbSBryant G. Ly 		dma_rmb();
323488a678bbSBryant G. Ly 	}
323588a678bbSBryant G. Ly 
323688a678bbSBryant G. Ly 	if (!rc) {
323788a678bbSBryant G. Ly 		if (ack) {
323888a678bbSBryant G. Ly 			vio_enable_interrupts(vscsi->dma_dev);
323988a678bbSBryant G. Ly 			ack = false;
324088a678bbSBryant G. Ly 			pr_debug("handle_crq, reenabling interrupts\n");
324188a678bbSBryant G. Ly 		}
324288a678bbSBryant G. Ly 		valid = crq->valid;
324388a678bbSBryant G. Ly 		dma_rmb();
324488a678bbSBryant G. Ly 		if (valid)
324588a678bbSBryant G. Ly 			goto cmd_work;
324688a678bbSBryant G. Ly 	} else {
324788a678bbSBryant G. Ly 		pr_debug("handle_crq, error: flags 0x%x, state 0x%hx, crq index 0x%x\n",
324888a678bbSBryant G. Ly 			 vscsi->flags, vscsi->state, vscsi->cmd_q.index);
324988a678bbSBryant G. Ly 	}
325088a678bbSBryant G. Ly 
325188a678bbSBryant G. Ly 	pr_debug("Leaving handle_crq: schedule_q empty %d, flags 0x%x, state 0x%hx\n",
325288a678bbSBryant G. Ly 		 (int)list_empty(&vscsi->schedule_q), vscsi->flags,
325388a678bbSBryant G. Ly 		 vscsi->state);
325488a678bbSBryant G. Ly 
325588a678bbSBryant G. Ly 	spin_unlock_bh(&vscsi->intr_lock);
325688a678bbSBryant G. Ly }
325788a678bbSBryant G. Ly 
325888a678bbSBryant G. Ly static int ibmvscsis_probe(struct vio_dev *vdev,
325988a678bbSBryant G. Ly 			   const struct vio_device_id *id)
326088a678bbSBryant G. Ly {
326188a678bbSBryant G. Ly 	struct scsi_info *vscsi;
326288a678bbSBryant G. Ly 	int rc = 0;
326388a678bbSBryant G. Ly 	long hrc = 0;
326488a678bbSBryant G. Ly 	char wq_name[24];
326588a678bbSBryant G. Ly 
326688a678bbSBryant G. Ly 	vscsi = kzalloc(sizeof(*vscsi), GFP_KERNEL);
326788a678bbSBryant G. Ly 	if (!vscsi) {
326888a678bbSBryant G. Ly 		rc = -ENOMEM;
326988a678bbSBryant G. Ly 		pr_err("probe: allocation of adapter failed\n");
327088a678bbSBryant G. Ly 		return rc;
327188a678bbSBryant G. Ly 	}
327288a678bbSBryant G. Ly 
327388a678bbSBryant G. Ly 	vscsi->dma_dev = vdev;
327488a678bbSBryant G. Ly 	vscsi->dev = vdev->dev;
327588a678bbSBryant G. Ly 	INIT_LIST_HEAD(&vscsi->schedule_q);
327688a678bbSBryant G. Ly 	INIT_LIST_HEAD(&vscsi->waiting_rsp);
327788a678bbSBryant G. Ly 	INIT_LIST_HEAD(&vscsi->active_q);
327888a678bbSBryant G. Ly 
327911950d70SMichael Cyr 	snprintf(vscsi->tport.tport_name, IBMVSCSIS_NAMELEN, "%s",
328011950d70SMichael Cyr 		 dev_name(&vdev->dev));
328188a678bbSBryant G. Ly 
328288a678bbSBryant G. Ly 	pr_debug("probe tport_name: %s\n", vscsi->tport.tport_name);
328388a678bbSBryant G. Ly 
328488a678bbSBryant G. Ly 	rc = read_dma_window(vscsi);
328588a678bbSBryant G. Ly 	if (rc)
328688a678bbSBryant G. Ly 		goto free_adapter;
328788a678bbSBryant G. Ly 	pr_debug("Probe: liobn 0x%x, riobn 0x%x\n",
328888a678bbSBryant G. Ly 		 vscsi->dds.window[LOCAL].liobn,
328988a678bbSBryant G. Ly 		 vscsi->dds.window[REMOTE].liobn);
329088a678bbSBryant G. Ly 
329188a678bbSBryant G. Ly 	strcpy(vscsi->eye, "VSCSI ");
329288a678bbSBryant G. Ly 	strncat(vscsi->eye, vdev->name, MAX_EYE);
329388a678bbSBryant G. Ly 
329488a678bbSBryant G. Ly 	vscsi->dds.unit_id = vdev->unit_address;
32959c93cf03SMichael Cyr 	strncpy(vscsi->dds.partition_name, partition_name,
32969c93cf03SMichael Cyr 		sizeof(vscsi->dds.partition_name));
32979c93cf03SMichael Cyr 	vscsi->dds.partition_num = partition_number;
329888a678bbSBryant G. Ly 
329988a678bbSBryant G. Ly 	spin_lock_bh(&ibmvscsis_dev_lock);
330088a678bbSBryant G. Ly 	list_add_tail(&vscsi->list, &ibmvscsis_dev_list);
330188a678bbSBryant G. Ly 	spin_unlock_bh(&ibmvscsis_dev_lock);
330288a678bbSBryant G. Ly 
330388a678bbSBryant G. Ly 	/*
330488a678bbSBryant G. Ly 	 * TBD: How do we determine # of cmds to request?  Do we know how
330588a678bbSBryant G. Ly 	 * many "children" we have?
330688a678bbSBryant G. Ly 	 */
330788a678bbSBryant G. Ly 	vscsi->request_limit = INITIAL_SRP_LIMIT;
330888a678bbSBryant G. Ly 	rc = srp_target_alloc(&vscsi->target, &vdev->dev, vscsi->request_limit,
330988a678bbSBryant G. Ly 			      SRP_MAX_IU_LEN);
331088a678bbSBryant G. Ly 	if (rc)
331188a678bbSBryant G. Ly 		goto rem_list;
331288a678bbSBryant G. Ly 
331388a678bbSBryant G. Ly 	vscsi->target.ldata = vscsi;
331488a678bbSBryant G. Ly 
331588a678bbSBryant G. Ly 	rc = ibmvscsis_alloc_cmds(vscsi, vscsi->request_limit);
331688a678bbSBryant G. Ly 	if (rc) {
331788a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "alloc_cmds failed, rc %d, num %d\n",
331888a678bbSBryant G. Ly 			rc, vscsi->request_limit);
331988a678bbSBryant G. Ly 		goto free_target;
332088a678bbSBryant G. Ly 	}
332188a678bbSBryant G. Ly 
332288a678bbSBryant G. Ly 	/*
332388a678bbSBryant G. Ly 	 * Note: the lock is used in freeing timers, so must initialize
332488a678bbSBryant G. Ly 	 * first so that ordering in case of error is correct.
332588a678bbSBryant G. Ly 	 */
332688a678bbSBryant G. Ly 	spin_lock_init(&vscsi->intr_lock);
332788a678bbSBryant G. Ly 
332888a678bbSBryant G. Ly 	rc = ibmvscsis_alloctimer(vscsi);
332988a678bbSBryant G. Ly 	if (rc) {
333088a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "probe: alloctimer failed, rc %d\n", rc);
333188a678bbSBryant G. Ly 		goto free_cmds;
333288a678bbSBryant G. Ly 	}
333388a678bbSBryant G. Ly 
333488a678bbSBryant G. Ly 	rc = ibmvscsis_create_command_q(vscsi, 256);
333588a678bbSBryant G. Ly 	if (rc) {
333688a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "probe: create_command_q failed, rc %d\n",
333788a678bbSBryant G. Ly 			rc);
333888a678bbSBryant G. Ly 		goto free_timer;
333988a678bbSBryant G. Ly 	}
334088a678bbSBryant G. Ly 
334188a678bbSBryant G. Ly 	vscsi->map_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
334288a678bbSBryant G. Ly 	if (!vscsi->map_buf) {
334388a678bbSBryant G. Ly 		rc = -ENOMEM;
334488a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "probe: allocating cmd buffer failed\n");
334588a678bbSBryant G. Ly 		goto destroy_queue;
334688a678bbSBryant G. Ly 	}
334788a678bbSBryant G. Ly 
334888a678bbSBryant G. Ly 	vscsi->map_ioba = dma_map_single(&vdev->dev, vscsi->map_buf, PAGE_SIZE,
334988a678bbSBryant G. Ly 					 DMA_BIDIRECTIONAL);
335088a678bbSBryant G. Ly 	if (dma_mapping_error(&vdev->dev, vscsi->map_ioba)) {
335138247febSWei Yongjun 		rc = -ENOMEM;
335288a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "probe: error mapping command buffer\n");
335388a678bbSBryant G. Ly 		goto free_buf;
335488a678bbSBryant G. Ly 	}
335588a678bbSBryant G. Ly 
335688a678bbSBryant G. Ly 	hrc = h_vioctl(vscsi->dds.unit_id, H_GET_PARTNER_INFO,
335788a678bbSBryant G. Ly 		       (u64)vscsi->map_ioba | ((u64)PAGE_SIZE << 32), 0, 0, 0,
335888a678bbSBryant G. Ly 		       0);
335988a678bbSBryant G. Ly 	if (hrc == H_SUCCESS)
336088a678bbSBryant G. Ly 		vscsi->client_data.partition_number =
336188a678bbSBryant G. Ly 			be64_to_cpu(*(u64 *)vscsi->map_buf);
336288a678bbSBryant G. Ly 	/*
336388a678bbSBryant G. Ly 	 * We expect the VIOCTL to fail if we're configured as "any
336488a678bbSBryant G. Ly 	 * client can connect" and the client isn't activated yet.
336588a678bbSBryant G. Ly 	 * We'll make the call again when he sends an init msg.
336688a678bbSBryant G. Ly 	 */
336788a678bbSBryant G. Ly 	pr_debug("probe hrc %ld, client partition num %d\n",
336888a678bbSBryant G. Ly 		 hrc, vscsi->client_data.partition_number);
336988a678bbSBryant G. Ly 
337088a678bbSBryant G. Ly 	tasklet_init(&vscsi->work_task, ibmvscsis_handle_crq,
337188a678bbSBryant G. Ly 		     (unsigned long)vscsi);
337288a678bbSBryant G. Ly 
337388a678bbSBryant G. Ly 	init_completion(&vscsi->wait_idle);
33748bf11557SMichael Cyr 	init_completion(&vscsi->unconfig);
337588a678bbSBryant G. Ly 
337688a678bbSBryant G. Ly 	snprintf(wq_name, 24, "ibmvscsis%s", dev_name(&vdev->dev));
337788a678bbSBryant G. Ly 	vscsi->work_q = create_workqueue(wq_name);
337888a678bbSBryant G. Ly 	if (!vscsi->work_q) {
337988a678bbSBryant G. Ly 		rc = -ENOMEM;
338088a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "create_workqueue failed\n");
338188a678bbSBryant G. Ly 		goto unmap_buf;
338288a678bbSBryant G. Ly 	}
338388a678bbSBryant G. Ly 
338488a678bbSBryant G. Ly 	rc = request_irq(vdev->irq, ibmvscsis_interrupt, 0, "ibmvscsis", vscsi);
338588a678bbSBryant G. Ly 	if (rc) {
338688a678bbSBryant G. Ly 		rc = -EPERM;
338788a678bbSBryant G. Ly 		dev_err(&vscsi->dev, "probe: request_irq failed, rc %d\n", rc);
338888a678bbSBryant G. Ly 		goto destroy_WQ;
338988a678bbSBryant G. Ly 	}
339088a678bbSBryant G. Ly 
3391c9b3379fSMichael Cyr 	vscsi->state = WAIT_ENABLED;
339288a678bbSBryant G. Ly 
339388a678bbSBryant G. Ly 	dev_set_drvdata(&vdev->dev, vscsi);
339488a678bbSBryant G. Ly 
339588a678bbSBryant G. Ly 	return 0;
339688a678bbSBryant G. Ly 
339788a678bbSBryant G. Ly destroy_WQ:
339888a678bbSBryant G. Ly 	destroy_workqueue(vscsi->work_q);
339988a678bbSBryant G. Ly unmap_buf:
340088a678bbSBryant G. Ly 	dma_unmap_single(&vdev->dev, vscsi->map_ioba, PAGE_SIZE,
340188a678bbSBryant G. Ly 			 DMA_BIDIRECTIONAL);
340288a678bbSBryant G. Ly free_buf:
340388a678bbSBryant G. Ly 	kfree(vscsi->map_buf);
340488a678bbSBryant G. Ly destroy_queue:
340588a678bbSBryant G. Ly 	tasklet_kill(&vscsi->work_task);
340688a678bbSBryant G. Ly 	ibmvscsis_unregister_command_q(vscsi);
340788a678bbSBryant G. Ly 	ibmvscsis_destroy_command_q(vscsi);
340888a678bbSBryant G. Ly free_timer:
340988a678bbSBryant G. Ly 	ibmvscsis_freetimer(vscsi);
341088a678bbSBryant G. Ly free_cmds:
341188a678bbSBryant G. Ly 	ibmvscsis_free_cmds(vscsi);
341288a678bbSBryant G. Ly free_target:
341388a678bbSBryant G. Ly 	srp_target_free(&vscsi->target);
341488a678bbSBryant G. Ly rem_list:
341588a678bbSBryant G. Ly 	spin_lock_bh(&ibmvscsis_dev_lock);
341688a678bbSBryant G. Ly 	list_del(&vscsi->list);
341788a678bbSBryant G. Ly 	spin_unlock_bh(&ibmvscsis_dev_lock);
341888a678bbSBryant G. Ly free_adapter:
341988a678bbSBryant G. Ly 	kfree(vscsi);
342088a678bbSBryant G. Ly 
342188a678bbSBryant G. Ly 	return rc;
342288a678bbSBryant G. Ly }
342388a678bbSBryant G. Ly 
342488a678bbSBryant G. Ly static int ibmvscsis_remove(struct vio_dev *vdev)
342588a678bbSBryant G. Ly {
342688a678bbSBryant G. Ly 	struct scsi_info *vscsi = dev_get_drvdata(&vdev->dev);
342788a678bbSBryant G. Ly 
342888a678bbSBryant G. Ly 	pr_debug("remove (%s)\n", dev_name(&vscsi->dma_dev->dev));
342988a678bbSBryant G. Ly 
34308bf11557SMichael Cyr 	spin_lock_bh(&vscsi->intr_lock);
34318bf11557SMichael Cyr 	ibmvscsis_post_disconnect(vscsi, UNCONFIGURING, 0);
34328bf11557SMichael Cyr 	vscsi->flags |= CFG_SLEEPING;
34338bf11557SMichael Cyr 	spin_unlock_bh(&vscsi->intr_lock);
34348bf11557SMichael Cyr 	wait_for_completion(&vscsi->unconfig);
343588a678bbSBryant G. Ly 
343688a678bbSBryant G. Ly 	vio_disable_interrupts(vdev);
343788a678bbSBryant G. Ly 	free_irq(vdev->irq, vscsi);
343888a678bbSBryant G. Ly 	destroy_workqueue(vscsi->work_q);
343988a678bbSBryant G. Ly 	dma_unmap_single(&vdev->dev, vscsi->map_ioba, PAGE_SIZE,
344088a678bbSBryant G. Ly 			 DMA_BIDIRECTIONAL);
344188a678bbSBryant G. Ly 	kfree(vscsi->map_buf);
344288a678bbSBryant G. Ly 	tasklet_kill(&vscsi->work_task);
344388a678bbSBryant G. Ly 	ibmvscsis_destroy_command_q(vscsi);
344488a678bbSBryant G. Ly 	ibmvscsis_freetimer(vscsi);
344588a678bbSBryant G. Ly 	ibmvscsis_free_cmds(vscsi);
344688a678bbSBryant G. Ly 	srp_target_free(&vscsi->target);
344788a678bbSBryant G. Ly 	spin_lock_bh(&ibmvscsis_dev_lock);
344888a678bbSBryant G. Ly 	list_del(&vscsi->list);
344988a678bbSBryant G. Ly 	spin_unlock_bh(&ibmvscsis_dev_lock);
345088a678bbSBryant G. Ly 	kfree(vscsi);
345188a678bbSBryant G. Ly 
345288a678bbSBryant G. Ly 	return 0;
345388a678bbSBryant G. Ly }
345488a678bbSBryant G. Ly 
345588a678bbSBryant G. Ly static ssize_t system_id_show(struct device *dev,
345688a678bbSBryant G. Ly 			      struct device_attribute *attr, char *buf)
345788a678bbSBryant G. Ly {
345888a678bbSBryant G. Ly 	return snprintf(buf, PAGE_SIZE, "%s\n", system_id);
345988a678bbSBryant G. Ly }
346088a678bbSBryant G. Ly 
346188a678bbSBryant G. Ly static ssize_t partition_number_show(struct device *dev,
346288a678bbSBryant G. Ly 				     struct device_attribute *attr, char *buf)
346388a678bbSBryant G. Ly {
346488a678bbSBryant G. Ly 	return snprintf(buf, PAGE_SIZE, "%x\n", partition_number);
346588a678bbSBryant G. Ly }
346688a678bbSBryant G. Ly 
346788a678bbSBryant G. Ly static ssize_t unit_address_show(struct device *dev,
346888a678bbSBryant G. Ly 				 struct device_attribute *attr, char *buf)
346988a678bbSBryant G. Ly {
347088a678bbSBryant G. Ly 	struct scsi_info *vscsi = container_of(dev, struct scsi_info, dev);
347188a678bbSBryant G. Ly 
347288a678bbSBryant G. Ly 	return snprintf(buf, PAGE_SIZE, "%x\n", vscsi->dma_dev->unit_address);
347388a678bbSBryant G. Ly }
347488a678bbSBryant G. Ly 
347588a678bbSBryant G. Ly static int ibmvscsis_get_system_info(void)
347688a678bbSBryant G. Ly {
347788a678bbSBryant G. Ly 	struct device_node *rootdn, *vdevdn;
347888a678bbSBryant G. Ly 	const char *id, *model, *name;
347988a678bbSBryant G. Ly 	const uint *num;
348088a678bbSBryant G. Ly 
348188a678bbSBryant G. Ly 	rootdn = of_find_node_by_path("/");
348288a678bbSBryant G. Ly 	if (!rootdn)
348388a678bbSBryant G. Ly 		return -ENOENT;
348488a678bbSBryant G. Ly 
348588a678bbSBryant G. Ly 	model = of_get_property(rootdn, "model", NULL);
348688a678bbSBryant G. Ly 	id = of_get_property(rootdn, "system-id", NULL);
348788a678bbSBryant G. Ly 	if (model && id)
348888a678bbSBryant G. Ly 		snprintf(system_id, sizeof(system_id), "%s-%s", model, id);
348988a678bbSBryant G. Ly 
349088a678bbSBryant G. Ly 	name = of_get_property(rootdn, "ibm,partition-name", NULL);
349188a678bbSBryant G. Ly 	if (name)
349288a678bbSBryant G. Ly 		strncpy(partition_name, name, sizeof(partition_name));
349388a678bbSBryant G. Ly 
349488a678bbSBryant G. Ly 	num = of_get_property(rootdn, "ibm,partition-no", NULL);
349588a678bbSBryant G. Ly 	if (num)
34969c93cf03SMichael Cyr 		partition_number = of_read_number(num, 1);
349788a678bbSBryant G. Ly 
349888a678bbSBryant G. Ly 	of_node_put(rootdn);
349988a678bbSBryant G. Ly 
350088a678bbSBryant G. Ly 	vdevdn = of_find_node_by_path("/vdevice");
350188a678bbSBryant G. Ly 	if (vdevdn) {
350288a678bbSBryant G. Ly 		const uint *mvds;
350388a678bbSBryant G. Ly 
350488a678bbSBryant G. Ly 		mvds = of_get_property(vdevdn, "ibm,max-virtual-dma-size",
350588a678bbSBryant G. Ly 				       NULL);
350688a678bbSBryant G. Ly 		if (mvds)
350788a678bbSBryant G. Ly 			max_vdma_size = *mvds;
350888a678bbSBryant G. Ly 		of_node_put(vdevdn);
350988a678bbSBryant G. Ly 	}
351088a678bbSBryant G. Ly 
351188a678bbSBryant G. Ly 	return 0;
351288a678bbSBryant G. Ly }
351388a678bbSBryant G. Ly 
351488a678bbSBryant G. Ly static char *ibmvscsis_get_fabric_name(void)
351588a678bbSBryant G. Ly {
351688a678bbSBryant G. Ly 	return "ibmvscsis";
351788a678bbSBryant G. Ly }
351888a678bbSBryant G. Ly 
351988a678bbSBryant G. Ly static char *ibmvscsis_get_fabric_wwn(struct se_portal_group *se_tpg)
352088a678bbSBryant G. Ly {
352188a678bbSBryant G. Ly 	struct ibmvscsis_tport *tport =
352288a678bbSBryant G. Ly 		container_of(se_tpg, struct ibmvscsis_tport, se_tpg);
352388a678bbSBryant G. Ly 
352488a678bbSBryant G. Ly 	return tport->tport_name;
352588a678bbSBryant G. Ly }
352688a678bbSBryant G. Ly 
352788a678bbSBryant G. Ly static u16 ibmvscsis_get_tag(struct se_portal_group *se_tpg)
352888a678bbSBryant G. Ly {
352988a678bbSBryant G. Ly 	struct ibmvscsis_tport *tport =
353088a678bbSBryant G. Ly 		container_of(se_tpg, struct ibmvscsis_tport, se_tpg);
353188a678bbSBryant G. Ly 
353288a678bbSBryant G. Ly 	return tport->tport_tpgt;
353388a678bbSBryant G. Ly }
353488a678bbSBryant G. Ly 
353588a678bbSBryant G. Ly static u32 ibmvscsis_get_default_depth(struct se_portal_group *se_tpg)
353688a678bbSBryant G. Ly {
353788a678bbSBryant G. Ly 	return 1;
353888a678bbSBryant G. Ly }
353988a678bbSBryant G. Ly 
354088a678bbSBryant G. Ly static int ibmvscsis_check_true(struct se_portal_group *se_tpg)
354188a678bbSBryant G. Ly {
354288a678bbSBryant G. Ly 	return 1;
354388a678bbSBryant G. Ly }
354488a678bbSBryant G. Ly 
354588a678bbSBryant G. Ly static int ibmvscsis_check_false(struct se_portal_group *se_tpg)
354688a678bbSBryant G. Ly {
354788a678bbSBryant G. Ly 	return 0;
354888a678bbSBryant G. Ly }
354988a678bbSBryant G. Ly 
355088a678bbSBryant G. Ly static u32 ibmvscsis_tpg_get_inst_index(struct se_portal_group *se_tpg)
355188a678bbSBryant G. Ly {
355288a678bbSBryant G. Ly 	return 1;
355388a678bbSBryant G. Ly }
355488a678bbSBryant G. Ly 
355588a678bbSBryant G. Ly static int ibmvscsis_check_stop_free(struct se_cmd *se_cmd)
355688a678bbSBryant G. Ly {
355788a678bbSBryant G. Ly 	return target_put_sess_cmd(se_cmd);
355888a678bbSBryant G. Ly }
355988a678bbSBryant G. Ly 
356088a678bbSBryant G. Ly static void ibmvscsis_release_cmd(struct se_cmd *se_cmd)
356188a678bbSBryant G. Ly {
356288a678bbSBryant G. Ly 	struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
356388a678bbSBryant G. Ly 						 se_cmd);
356488a678bbSBryant G. Ly 	struct scsi_info *vscsi = cmd->adapter;
356588a678bbSBryant G. Ly 
356688a678bbSBryant G. Ly 	spin_lock_bh(&vscsi->intr_lock);
356788a678bbSBryant G. Ly 	/* Remove from active_q */
3568980b3271SWei Yongjun 	list_move_tail(&cmd->list, &vscsi->waiting_rsp);
356988a678bbSBryant G. Ly 	ibmvscsis_send_messages(vscsi);
357088a678bbSBryant G. Ly 	spin_unlock_bh(&vscsi->intr_lock);
357188a678bbSBryant G. Ly }
357288a678bbSBryant G. Ly 
357388a678bbSBryant G. Ly static u32 ibmvscsis_sess_get_index(struct se_session *se_sess)
357488a678bbSBryant G. Ly {
357588a678bbSBryant G. Ly 	return 0;
357688a678bbSBryant G. Ly }
357788a678bbSBryant G. Ly 
357888a678bbSBryant G. Ly static int ibmvscsis_write_pending(struct se_cmd *se_cmd)
357988a678bbSBryant G. Ly {
358088a678bbSBryant G. Ly 	struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
358188a678bbSBryant G. Ly 						 se_cmd);
358288a678bbSBryant G. Ly 	struct iu_entry *iue = cmd->iue;
358388a678bbSBryant G. Ly 	int rc;
358488a678bbSBryant G. Ly 
358588a678bbSBryant G. Ly 	rc = srp_transfer_data(cmd, &vio_iu(iue)->srp.cmd, ibmvscsis_rdma,
358688a678bbSBryant G. Ly 			       1, 1);
358788a678bbSBryant G. Ly 	if (rc) {
358888a678bbSBryant G. Ly 		pr_err("srp_transfer_data() failed: %d\n", rc);
35897c9d8d0cSBryant G. Ly 		return -EIO;
359088a678bbSBryant G. Ly 	}
359188a678bbSBryant G. Ly 	/*
359288a678bbSBryant G. Ly 	 * We now tell TCM to add this WRITE CDB directly into the TCM storage
359388a678bbSBryant G. Ly 	 * object execution queue.
359488a678bbSBryant G. Ly 	 */
359588a678bbSBryant G. Ly 	target_execute_cmd(se_cmd);
359688a678bbSBryant G. Ly 	return 0;
359788a678bbSBryant G. Ly }
359888a678bbSBryant G. Ly 
359988a678bbSBryant G. Ly static int ibmvscsis_write_pending_status(struct se_cmd *se_cmd)
360088a678bbSBryant G. Ly {
360188a678bbSBryant G. Ly 	return 0;
360288a678bbSBryant G. Ly }
360388a678bbSBryant G. Ly 
360488a678bbSBryant G. Ly static void ibmvscsis_set_default_node_attrs(struct se_node_acl *nacl)
360588a678bbSBryant G. Ly {
360688a678bbSBryant G. Ly }
360788a678bbSBryant G. Ly 
360888a678bbSBryant G. Ly static int ibmvscsis_get_cmd_state(struct se_cmd *se_cmd)
360988a678bbSBryant G. Ly {
361088a678bbSBryant G. Ly 	return 0;
361188a678bbSBryant G. Ly }
361288a678bbSBryant G. Ly 
361388a678bbSBryant G. Ly static int ibmvscsis_queue_data_in(struct se_cmd *se_cmd)
361488a678bbSBryant G. Ly {
361588a678bbSBryant G. Ly 	struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
361688a678bbSBryant G. Ly 						 se_cmd);
361788a678bbSBryant G. Ly 	struct iu_entry *iue = cmd->iue;
361888a678bbSBryant G. Ly 	struct scsi_info *vscsi = cmd->adapter;
361988a678bbSBryant G. Ly 	char *sd;
362088a678bbSBryant G. Ly 	uint len = 0;
362188a678bbSBryant G. Ly 	int rc;
362288a678bbSBryant G. Ly 
362388a678bbSBryant G. Ly 	rc = srp_transfer_data(cmd, &vio_iu(iue)->srp.cmd, ibmvscsis_rdma, 1,
362488a678bbSBryant G. Ly 			       1);
362588a678bbSBryant G. Ly 	if (rc) {
362688a678bbSBryant G. Ly 		pr_err("srp_transfer_data failed: %d\n", rc);
362788a678bbSBryant G. Ly 		sd = se_cmd->sense_buffer;
362888a678bbSBryant G. Ly 		se_cmd->scsi_sense_length = 18;
362988a678bbSBryant G. Ly 		memset(se_cmd->sense_buffer, 0, se_cmd->scsi_sense_length);
363088a678bbSBryant G. Ly 		/* Logical Unit Communication Time-out asc/ascq = 0x0801 */
363188a678bbSBryant G. Ly 		scsi_build_sense_buffer(0, se_cmd->sense_buffer, MEDIUM_ERROR,
363288a678bbSBryant G. Ly 					0x08, 0x01);
363388a678bbSBryant G. Ly 	}
363488a678bbSBryant G. Ly 
363588a678bbSBryant G. Ly 	srp_build_response(vscsi, cmd, &len);
363688a678bbSBryant G. Ly 	cmd->rsp.format = SRP_FORMAT;
363788a678bbSBryant G. Ly 	cmd->rsp.len = len;
363888a678bbSBryant G. Ly 
363988a678bbSBryant G. Ly 	return 0;
364088a678bbSBryant G. Ly }
364188a678bbSBryant G. Ly 
364288a678bbSBryant G. Ly static int ibmvscsis_queue_status(struct se_cmd *se_cmd)
364388a678bbSBryant G. Ly {
364488a678bbSBryant G. Ly 	struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
364588a678bbSBryant G. Ly 						 se_cmd);
364688a678bbSBryant G. Ly 	struct scsi_info *vscsi = cmd->adapter;
364788a678bbSBryant G. Ly 	uint len;
364888a678bbSBryant G. Ly 
364988a678bbSBryant G. Ly 	pr_debug("queue_status %p\n", se_cmd);
365088a678bbSBryant G. Ly 
365188a678bbSBryant G. Ly 	srp_build_response(vscsi, cmd, &len);
365288a678bbSBryant G. Ly 	cmd->rsp.format = SRP_FORMAT;
365388a678bbSBryant G. Ly 	cmd->rsp.len = len;
365488a678bbSBryant G. Ly 
365588a678bbSBryant G. Ly 	return 0;
365688a678bbSBryant G. Ly }
365788a678bbSBryant G. Ly 
365888a678bbSBryant G. Ly static void ibmvscsis_queue_tm_rsp(struct se_cmd *se_cmd)
365988a678bbSBryant G. Ly {
366088a678bbSBryant G. Ly 	struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
366188a678bbSBryant G. Ly 						 se_cmd);
366288a678bbSBryant G. Ly 	struct scsi_info *vscsi = cmd->adapter;
366388a678bbSBryant G. Ly 	uint len;
366488a678bbSBryant G. Ly 
366588a678bbSBryant G. Ly 	pr_debug("queue_tm_rsp %p, status %d\n",
366688a678bbSBryant G. Ly 		 se_cmd, (int)se_cmd->se_tmr_req->response);
366788a678bbSBryant G. Ly 
366888a678bbSBryant G. Ly 	srp_build_response(vscsi, cmd, &len);
366988a678bbSBryant G. Ly 	cmd->rsp.format = SRP_FORMAT;
367088a678bbSBryant G. Ly 	cmd->rsp.len = len;
367188a678bbSBryant G. Ly }
367288a678bbSBryant G. Ly 
367388a678bbSBryant G. Ly static void ibmvscsis_aborted_task(struct se_cmd *se_cmd)
367488a678bbSBryant G. Ly {
367588a678bbSBryant G. Ly 	/* TBD: What (if anything) should we do here? */
367688a678bbSBryant G. Ly 	pr_debug("ibmvscsis_aborted_task %p\n", se_cmd);
367788a678bbSBryant G. Ly }
367888a678bbSBryant G. Ly 
367988a678bbSBryant G. Ly static struct se_wwn *ibmvscsis_make_tport(struct target_fabric_configfs *tf,
368088a678bbSBryant G. Ly 					   struct config_group *group,
368188a678bbSBryant G. Ly 					   const char *name)
368288a678bbSBryant G. Ly {
368388a678bbSBryant G. Ly 	struct ibmvscsis_tport *tport;
368488a678bbSBryant G. Ly 
368588a678bbSBryant G. Ly 	tport = ibmvscsis_lookup_port(name);
368688a678bbSBryant G. Ly 	if (tport) {
368788a678bbSBryant G. Ly 		tport->tport_proto_id = SCSI_PROTOCOL_SRP;
368888a678bbSBryant G. Ly 		pr_debug("make_tport(%s), pointer:%p, tport_id:%x\n",
368988a678bbSBryant G. Ly 			 name, tport, tport->tport_proto_id);
369088a678bbSBryant G. Ly 		return &tport->tport_wwn;
369188a678bbSBryant G. Ly 	}
369288a678bbSBryant G. Ly 
369388a678bbSBryant G. Ly 	return ERR_PTR(-EINVAL);
369488a678bbSBryant G. Ly }
369588a678bbSBryant G. Ly 
369688a678bbSBryant G. Ly static void ibmvscsis_drop_tport(struct se_wwn *wwn)
369788a678bbSBryant G. Ly {
369888a678bbSBryant G. Ly 	struct ibmvscsis_tport *tport = container_of(wwn,
369988a678bbSBryant G. Ly 						     struct ibmvscsis_tport,
370088a678bbSBryant G. Ly 						     tport_wwn);
370188a678bbSBryant G. Ly 
370288a678bbSBryant G. Ly 	pr_debug("drop_tport(%s)\n",
370388a678bbSBryant G. Ly 		 config_item_name(&tport->tport_wwn.wwn_group.cg_item));
370488a678bbSBryant G. Ly }
370588a678bbSBryant G. Ly 
370688a678bbSBryant G. Ly static struct se_portal_group *ibmvscsis_make_tpg(struct se_wwn *wwn,
370788a678bbSBryant G. Ly 						  struct config_group *group,
370888a678bbSBryant G. Ly 						  const char *name)
370988a678bbSBryant G. Ly {
371088a678bbSBryant G. Ly 	struct ibmvscsis_tport *tport =
371188a678bbSBryant G. Ly 		container_of(wwn, struct ibmvscsis_tport, tport_wwn);
371288a678bbSBryant G. Ly 	int rc;
371388a678bbSBryant G. Ly 
371488a678bbSBryant G. Ly 	tport->releasing = false;
371588a678bbSBryant G. Ly 
371688a678bbSBryant G. Ly 	rc = core_tpg_register(&tport->tport_wwn, &tport->se_tpg,
371788a678bbSBryant G. Ly 			       tport->tport_proto_id);
371888a678bbSBryant G. Ly 	if (rc)
371988a678bbSBryant G. Ly 		return ERR_PTR(rc);
372088a678bbSBryant G. Ly 
372188a678bbSBryant G. Ly 	return &tport->se_tpg;
372288a678bbSBryant G. Ly }
372388a678bbSBryant G. Ly 
372488a678bbSBryant G. Ly static void ibmvscsis_drop_tpg(struct se_portal_group *se_tpg)
372588a678bbSBryant G. Ly {
372688a678bbSBryant G. Ly 	struct ibmvscsis_tport *tport = container_of(se_tpg,
372788a678bbSBryant G. Ly 						     struct ibmvscsis_tport,
372888a678bbSBryant G. Ly 						     se_tpg);
372988a678bbSBryant G. Ly 
373088a678bbSBryant G. Ly 	tport->releasing = true;
373188a678bbSBryant G. Ly 	tport->enabled = false;
373288a678bbSBryant G. Ly 
373388a678bbSBryant G. Ly 	/*
373488a678bbSBryant G. Ly 	 * Release the virtual I_T Nexus for this ibmvscsis TPG
373588a678bbSBryant G. Ly 	 */
373688a678bbSBryant G. Ly 	ibmvscsis_drop_nexus(tport);
373788a678bbSBryant G. Ly 	/*
373888a678bbSBryant G. Ly 	 * Deregister the se_tpg from TCM..
373988a678bbSBryant G. Ly 	 */
374088a678bbSBryant G. Ly 	core_tpg_deregister(se_tpg);
374188a678bbSBryant G. Ly }
374288a678bbSBryant G. Ly 
374388a678bbSBryant G. Ly static ssize_t ibmvscsis_wwn_version_show(struct config_item *item,
374488a678bbSBryant G. Ly 					  char *page)
374588a678bbSBryant G. Ly {
374688a678bbSBryant G. Ly 	return scnprintf(page, PAGE_SIZE, "%s\n", IBMVSCSIS_VERSION);
374788a678bbSBryant G. Ly }
374888a678bbSBryant G. Ly CONFIGFS_ATTR_RO(ibmvscsis_wwn_, version);
374988a678bbSBryant G. Ly 
375088a678bbSBryant G. Ly static struct configfs_attribute *ibmvscsis_wwn_attrs[] = {
375188a678bbSBryant G. Ly 	&ibmvscsis_wwn_attr_version,
375288a678bbSBryant G. Ly 	NULL,
375388a678bbSBryant G. Ly };
375488a678bbSBryant G. Ly 
375588a678bbSBryant G. Ly static ssize_t ibmvscsis_tpg_enable_show(struct config_item *item,
375688a678bbSBryant G. Ly 					 char *page)
375788a678bbSBryant G. Ly {
375888a678bbSBryant G. Ly 	struct se_portal_group *se_tpg = to_tpg(item);
375988a678bbSBryant G. Ly 	struct ibmvscsis_tport *tport = container_of(se_tpg,
376088a678bbSBryant G. Ly 						     struct ibmvscsis_tport,
376188a678bbSBryant G. Ly 						     se_tpg);
376288a678bbSBryant G. Ly 
376388a678bbSBryant G. Ly 	return snprintf(page, PAGE_SIZE, "%d\n", (tport->enabled) ? 1 : 0);
376488a678bbSBryant G. Ly }
376588a678bbSBryant G. Ly 
376688a678bbSBryant G. Ly static ssize_t ibmvscsis_tpg_enable_store(struct config_item *item,
376788a678bbSBryant G. Ly 					  const char *page, size_t count)
376888a678bbSBryant G. Ly {
376988a678bbSBryant G. Ly 	struct se_portal_group *se_tpg = to_tpg(item);
377088a678bbSBryant G. Ly 	struct ibmvscsis_tport *tport = container_of(se_tpg,
377188a678bbSBryant G. Ly 						     struct ibmvscsis_tport,
377288a678bbSBryant G. Ly 						     se_tpg);
377388a678bbSBryant G. Ly 	struct scsi_info *vscsi = container_of(tport, struct scsi_info, tport);
377488a678bbSBryant G. Ly 	unsigned long tmp;
377588a678bbSBryant G. Ly 	int rc;
377688a678bbSBryant G. Ly 	long lrc;
377788a678bbSBryant G. Ly 
377888a678bbSBryant G. Ly 	rc = kstrtoul(page, 0, &tmp);
377988a678bbSBryant G. Ly 	if (rc < 0) {
378088a678bbSBryant G. Ly 		pr_err("Unable to extract srpt_tpg_store_enable\n");
378188a678bbSBryant G. Ly 		return -EINVAL;
378288a678bbSBryant G. Ly 	}
378388a678bbSBryant G. Ly 
378488a678bbSBryant G. Ly 	if ((tmp != 0) && (tmp != 1)) {
378588a678bbSBryant G. Ly 		pr_err("Illegal value for srpt_tpg_store_enable\n");
378688a678bbSBryant G. Ly 		return -EINVAL;
378788a678bbSBryant G. Ly 	}
378888a678bbSBryant G. Ly 
378988a678bbSBryant G. Ly 	if (tmp) {
379088a678bbSBryant G. Ly 		spin_lock_bh(&vscsi->intr_lock);
3791c9b3379fSMichael Cyr 		tport->enabled = true;
379288a678bbSBryant G. Ly 		lrc = ibmvscsis_enable_change_state(vscsi);
379388a678bbSBryant G. Ly 		if (lrc)
379488a678bbSBryant G. Ly 			pr_err("enable_change_state failed, rc %ld state %d\n",
379588a678bbSBryant G. Ly 			       lrc, vscsi->state);
379688a678bbSBryant G. Ly 		spin_unlock_bh(&vscsi->intr_lock);
379788a678bbSBryant G. Ly 	} else {
3798c9b3379fSMichael Cyr 		spin_lock_bh(&vscsi->intr_lock);
379988a678bbSBryant G. Ly 		tport->enabled = false;
3800c9b3379fSMichael Cyr 		/* This simulates the server going down */
3801c9b3379fSMichael Cyr 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
3802c9b3379fSMichael Cyr 		spin_unlock_bh(&vscsi->intr_lock);
380388a678bbSBryant G. Ly 	}
380488a678bbSBryant G. Ly 
3805c9b3379fSMichael Cyr 	pr_debug("tpg_enable_store, tmp %ld, state %d\n", tmp, vscsi->state);
380688a678bbSBryant G. Ly 
380788a678bbSBryant G. Ly 	return count;
380888a678bbSBryant G. Ly }
380988a678bbSBryant G. Ly CONFIGFS_ATTR(ibmvscsis_tpg_, enable);
381088a678bbSBryant G. Ly 
381188a678bbSBryant G. Ly static struct configfs_attribute *ibmvscsis_tpg_attrs[] = {
381288a678bbSBryant G. Ly 	&ibmvscsis_tpg_attr_enable,
381388a678bbSBryant G. Ly 	NULL,
381488a678bbSBryant G. Ly };
381588a678bbSBryant G. Ly 
381688a678bbSBryant G. Ly static const struct target_core_fabric_ops ibmvscsis_ops = {
381788a678bbSBryant G. Ly 	.module				= THIS_MODULE,
381888a678bbSBryant G. Ly 	.name				= "ibmvscsis",
381988a678bbSBryant G. Ly 	.get_fabric_name		= ibmvscsis_get_fabric_name,
382088a678bbSBryant G. Ly 	.tpg_get_wwn			= ibmvscsis_get_fabric_wwn,
382188a678bbSBryant G. Ly 	.tpg_get_tag			= ibmvscsis_get_tag,
382288a678bbSBryant G. Ly 	.tpg_get_default_depth		= ibmvscsis_get_default_depth,
382388a678bbSBryant G. Ly 	.tpg_check_demo_mode		= ibmvscsis_check_true,
382488a678bbSBryant G. Ly 	.tpg_check_demo_mode_cache	= ibmvscsis_check_true,
382588a678bbSBryant G. Ly 	.tpg_check_demo_mode_write_protect = ibmvscsis_check_false,
382688a678bbSBryant G. Ly 	.tpg_check_prod_mode_write_protect = ibmvscsis_check_false,
382788a678bbSBryant G. Ly 	.tpg_get_inst_index		= ibmvscsis_tpg_get_inst_index,
382888a678bbSBryant G. Ly 	.check_stop_free		= ibmvscsis_check_stop_free,
382988a678bbSBryant G. Ly 	.release_cmd			= ibmvscsis_release_cmd,
383088a678bbSBryant G. Ly 	.sess_get_index			= ibmvscsis_sess_get_index,
383188a678bbSBryant G. Ly 	.write_pending			= ibmvscsis_write_pending,
383288a678bbSBryant G. Ly 	.write_pending_status		= ibmvscsis_write_pending_status,
383388a678bbSBryant G. Ly 	.set_default_node_attributes	= ibmvscsis_set_default_node_attrs,
383488a678bbSBryant G. Ly 	.get_cmd_state			= ibmvscsis_get_cmd_state,
383588a678bbSBryant G. Ly 	.queue_data_in			= ibmvscsis_queue_data_in,
383688a678bbSBryant G. Ly 	.queue_status			= ibmvscsis_queue_status,
383788a678bbSBryant G. Ly 	.queue_tm_rsp			= ibmvscsis_queue_tm_rsp,
383888a678bbSBryant G. Ly 	.aborted_task			= ibmvscsis_aborted_task,
383988a678bbSBryant G. Ly 	/*
384088a678bbSBryant G. Ly 	 * Setup function pointers for logic in target_core_fabric_configfs.c
384188a678bbSBryant G. Ly 	 */
384288a678bbSBryant G. Ly 	.fabric_make_wwn		= ibmvscsis_make_tport,
384388a678bbSBryant G. Ly 	.fabric_drop_wwn		= ibmvscsis_drop_tport,
384488a678bbSBryant G. Ly 	.fabric_make_tpg		= ibmvscsis_make_tpg,
384588a678bbSBryant G. Ly 	.fabric_drop_tpg		= ibmvscsis_drop_tpg,
384688a678bbSBryant G. Ly 
384788a678bbSBryant G. Ly 	.tfc_wwn_attrs			= ibmvscsis_wwn_attrs,
384888a678bbSBryant G. Ly 	.tfc_tpg_base_attrs		= ibmvscsis_tpg_attrs,
384988a678bbSBryant G. Ly };
385088a678bbSBryant G. Ly 
385188a678bbSBryant G. Ly static void ibmvscsis_dev_release(struct device *dev) {};
385288a678bbSBryant G. Ly 
385388a678bbSBryant G. Ly static struct class_attribute ibmvscsis_class_attrs[] = {
385488a678bbSBryant G. Ly 	__ATTR_NULL,
385588a678bbSBryant G. Ly };
385688a678bbSBryant G. Ly 
385788a678bbSBryant G. Ly static struct device_attribute dev_attr_system_id =
385888a678bbSBryant G. Ly 	__ATTR(system_id, S_IRUGO, system_id_show, NULL);
385988a678bbSBryant G. Ly 
386088a678bbSBryant G. Ly static struct device_attribute dev_attr_partition_number =
386188a678bbSBryant G. Ly 	__ATTR(partition_number, S_IRUGO, partition_number_show, NULL);
386288a678bbSBryant G. Ly 
386388a678bbSBryant G. Ly static struct device_attribute dev_attr_unit_address =
386488a678bbSBryant G. Ly 	__ATTR(unit_address, S_IRUGO, unit_address_show, NULL);
386588a678bbSBryant G. Ly 
386688a678bbSBryant G. Ly static struct attribute *ibmvscsis_dev_attrs[] = {
386788a678bbSBryant G. Ly 	&dev_attr_system_id.attr,
386888a678bbSBryant G. Ly 	&dev_attr_partition_number.attr,
386988a678bbSBryant G. Ly 	&dev_attr_unit_address.attr,
387088a678bbSBryant G. Ly };
387188a678bbSBryant G. Ly ATTRIBUTE_GROUPS(ibmvscsis_dev);
387288a678bbSBryant G. Ly 
387388a678bbSBryant G. Ly static struct class ibmvscsis_class = {
387488a678bbSBryant G. Ly 	.name		= "ibmvscsis",
387588a678bbSBryant G. Ly 	.dev_release	= ibmvscsis_dev_release,
387688a678bbSBryant G. Ly 	.class_attrs	= ibmvscsis_class_attrs,
387788a678bbSBryant G. Ly 	.dev_groups	= ibmvscsis_dev_groups,
387888a678bbSBryant G. Ly };
387988a678bbSBryant G. Ly 
388088a678bbSBryant G. Ly static struct vio_device_id ibmvscsis_device_table[] = {
388188a678bbSBryant G. Ly 	{ "v-scsi-host", "IBM,v-scsi-host" },
388288a678bbSBryant G. Ly 	{ "", "" }
388388a678bbSBryant G. Ly };
388488a678bbSBryant G. Ly MODULE_DEVICE_TABLE(vio, ibmvscsis_device_table);
388588a678bbSBryant G. Ly 
388688a678bbSBryant G. Ly static struct vio_driver ibmvscsis_driver = {
388788a678bbSBryant G. Ly 	.name = "ibmvscsis",
388888a678bbSBryant G. Ly 	.id_table = ibmvscsis_device_table,
388988a678bbSBryant G. Ly 	.probe = ibmvscsis_probe,
389088a678bbSBryant G. Ly 	.remove = ibmvscsis_remove,
389188a678bbSBryant G. Ly };
389288a678bbSBryant G. Ly 
389388a678bbSBryant G. Ly /*
389488a678bbSBryant G. Ly  * ibmvscsis_init() - Kernel Module initialization
389588a678bbSBryant G. Ly  *
389688a678bbSBryant G. Ly  * Note: vio_register_driver() registers callback functions, and at least one
389788a678bbSBryant G. Ly  * of those callback functions calls TCM - Linux IO Target Subsystem, thus
389888a678bbSBryant G. Ly  * the SCSI Target template must be registered before vio_register_driver()
389988a678bbSBryant G. Ly  * is called.
390088a678bbSBryant G. Ly  */
390188a678bbSBryant G. Ly static int __init ibmvscsis_init(void)
390288a678bbSBryant G. Ly {
390388a678bbSBryant G. Ly 	int rc = 0;
390488a678bbSBryant G. Ly 
390588a678bbSBryant G. Ly 	rc = ibmvscsis_get_system_info();
390688a678bbSBryant G. Ly 	if (rc) {
390788a678bbSBryant G. Ly 		pr_err("rc %d from get_system_info\n", rc);
390888a678bbSBryant G. Ly 		goto out;
390988a678bbSBryant G. Ly 	}
391088a678bbSBryant G. Ly 
391188a678bbSBryant G. Ly 	rc = class_register(&ibmvscsis_class);
391288a678bbSBryant G. Ly 	if (rc) {
391388a678bbSBryant G. Ly 		pr_err("failed class register\n");
391488a678bbSBryant G. Ly 		goto out;
391588a678bbSBryant G. Ly 	}
391688a678bbSBryant G. Ly 
391788a678bbSBryant G. Ly 	rc = target_register_template(&ibmvscsis_ops);
391888a678bbSBryant G. Ly 	if (rc) {
391988a678bbSBryant G. Ly 		pr_err("rc %d from target_register_template\n", rc);
392088a678bbSBryant G. Ly 		goto unregister_class;
392188a678bbSBryant G. Ly 	}
392288a678bbSBryant G. Ly 
392388a678bbSBryant G. Ly 	rc = vio_register_driver(&ibmvscsis_driver);
392488a678bbSBryant G. Ly 	if (rc) {
392588a678bbSBryant G. Ly 		pr_err("rc %d from vio_register_driver\n", rc);
392688a678bbSBryant G. Ly 		goto unregister_target;
392788a678bbSBryant G. Ly 	}
392888a678bbSBryant G. Ly 
392988a678bbSBryant G. Ly 	return 0;
393088a678bbSBryant G. Ly 
393188a678bbSBryant G. Ly unregister_target:
393288a678bbSBryant G. Ly 	target_unregister_template(&ibmvscsis_ops);
393388a678bbSBryant G. Ly unregister_class:
393488a678bbSBryant G. Ly 	class_unregister(&ibmvscsis_class);
393588a678bbSBryant G. Ly out:
393688a678bbSBryant G. Ly 	return rc;
393788a678bbSBryant G. Ly }
393888a678bbSBryant G. Ly 
393988a678bbSBryant G. Ly static void __exit ibmvscsis_exit(void)
394088a678bbSBryant G. Ly {
394188a678bbSBryant G. Ly 	pr_info("Unregister IBM virtual SCSI host driver\n");
394288a678bbSBryant G. Ly 	vio_unregister_driver(&ibmvscsis_driver);
394388a678bbSBryant G. Ly 	target_unregister_template(&ibmvscsis_ops);
394488a678bbSBryant G. Ly 	class_unregister(&ibmvscsis_class);
394588a678bbSBryant G. Ly }
394688a678bbSBryant G. Ly 
394788a678bbSBryant G. Ly MODULE_DESCRIPTION("IBMVSCSIS fabric driver");
394888a678bbSBryant G. Ly MODULE_AUTHOR("Bryant G. Ly and Michael Cyr");
394988a678bbSBryant G. Ly MODULE_LICENSE("GPL");
395088a678bbSBryant G. Ly MODULE_VERSION(IBMVSCSIS_VERSION);
395188a678bbSBryant G. Ly module_init(ibmvscsis_init);
395288a678bbSBryant G. Ly module_exit(ibmvscsis_exit);
3953