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