1 /*******************************************************************************
2  * IBM Virtual SCSI Target Driver
3  * Copyright (C) 2003-2005 Dave Boutcher (boutcher@us.ibm.com) IBM Corp.
4  *			   Santiago Leon (santil@us.ibm.com) IBM Corp.
5  *			   Linda Xie (lxie@us.ibm.com) IBM Corp.
6  *
7  * Copyright (C) 2005-2011 FUJITA Tomonori <tomof@acm.org>
8  * Copyright (C) 2010 Nicholas A. Bellinger <nab@kernel.org>
9  *
10  * Authors: Bryant G. Ly <bryantly@linux.vnet.ibm.com>
11  * Authors: Michael Cyr <mikecyr@linux.vnet.ibm.com>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  ****************************************************************************/
24 
25 #define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
26 
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/types.h>
31 #include <linux/list.h>
32 #include <linux/string.h>
33 #include <linux/delay.h>
34 
35 #include <target/target_core_base.h>
36 #include <target/target_core_fabric.h>
37 
38 #include <asm/hvcall.h>
39 #include <asm/vio.h>
40 
41 #include <scsi/viosrp.h>
42 
43 #include "ibmvscsi_tgt.h"
44 
45 #define IBMVSCSIS_VERSION	"v0.2"
46 
47 #define	INITIAL_SRP_LIMIT	800
48 #define	DEFAULT_MAX_SECTORS	256
49 #define MAX_TXU			1024 * 1024
50 
51 static uint max_vdma_size = MAX_H_COPY_RDMA;
52 
53 static char system_id[SYS_ID_NAME_LEN] = "";
54 static char partition_name[PARTITION_NAMELEN] = "UNKNOWN";
55 static uint partition_number = -1;
56 
57 /* Adapter list and lock to control it */
58 static DEFINE_SPINLOCK(ibmvscsis_dev_lock);
59 static LIST_HEAD(ibmvscsis_dev_list);
60 
61 static long ibmvscsis_parse_command(struct scsi_info *vscsi,
62 				    struct viosrp_crq *crq);
63 
64 static void ibmvscsis_adapter_idle(struct scsi_info *vscsi);
65 
66 static void ibmvscsis_determine_resid(struct se_cmd *se_cmd,
67 				      struct srp_rsp *rsp)
68 {
69 	u32 residual_count = se_cmd->residual_count;
70 
71 	if (!residual_count)
72 		return;
73 
74 	if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
75 		if (se_cmd->data_direction == DMA_TO_DEVICE) {
76 			/* residual data from an underflow write */
77 			rsp->flags = SRP_RSP_FLAG_DOUNDER;
78 			rsp->data_out_res_cnt = cpu_to_be32(residual_count);
79 		} else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
80 			/* residual data from an underflow read */
81 			rsp->flags = SRP_RSP_FLAG_DIUNDER;
82 			rsp->data_in_res_cnt = cpu_to_be32(residual_count);
83 		}
84 	} else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
85 		if (se_cmd->data_direction == DMA_TO_DEVICE) {
86 			/* residual data from an overflow write */
87 			rsp->flags = SRP_RSP_FLAG_DOOVER;
88 			rsp->data_out_res_cnt = cpu_to_be32(residual_count);
89 		} else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
90 			/* residual data from an overflow read */
91 			rsp->flags = SRP_RSP_FLAG_DIOVER;
92 			rsp->data_in_res_cnt = cpu_to_be32(residual_count);
93 		}
94 	}
95 }
96 
97 /**
98  * connection_broken() - Determine if the connection to the client is good
99  * @vscsi:	Pointer to our adapter structure
100  *
101  * This function attempts to send a ping MAD to the client. If the call to
102  * queue the request returns H_CLOSED then the connection has been broken
103  * and the function returns TRUE.
104  *
105  * EXECUTION ENVIRONMENT:
106  *	Interrupt or Process environment
107  */
108 static bool connection_broken(struct scsi_info *vscsi)
109 {
110 	struct viosrp_crq *crq;
111 	u64 buffer[2] = { 0, 0 };
112 	long h_return_code;
113 	bool rc = false;
114 
115 	/* create a PING crq */
116 	crq = (struct viosrp_crq *)&buffer;
117 	crq->valid = VALID_CMD_RESP_EL;
118 	crq->format = MESSAGE_IN_CRQ;
119 	crq->status = PING;
120 
121 	h_return_code = h_send_crq(vscsi->dds.unit_id,
122 				   cpu_to_be64(buffer[MSG_HI]),
123 				   cpu_to_be64(buffer[MSG_LOW]));
124 
125 	pr_debug("connection_broken: rc %ld\n", h_return_code);
126 
127 	if (h_return_code == H_CLOSED)
128 		rc = true;
129 
130 	return rc;
131 }
132 
133 /**
134  * ibmvscsis_unregister_command_q() - Helper Function-Unregister Command Queue
135  * @vscsi:	Pointer to our adapter structure
136  *
137  * This function calls h_free_q then frees the interrupt bit etc.
138  * It must release the lock before doing so because of the time it can take
139  * for h_free_crq in PHYP
140  * NOTE: the caller must make sure that state and or flags will prevent
141  *	 interrupt handler from scheduling work.
142  * NOTE: anyone calling this function may need to set the CRQ_CLOSED flag
143  *	 we can't do it here, because we don't have the lock
144  *
145  * EXECUTION ENVIRONMENT:
146  *	Process level
147  */
148 static long ibmvscsis_unregister_command_q(struct scsi_info *vscsi)
149 {
150 	long qrc;
151 	long rc = ADAPT_SUCCESS;
152 	int ticks = 0;
153 
154 	do {
155 		qrc = h_free_crq(vscsi->dds.unit_id);
156 		switch (qrc) {
157 		case H_SUCCESS:
158 			break;
159 
160 		case H_HARDWARE:
161 		case H_PARAMETER:
162 			dev_err(&vscsi->dev, "unregister_command_q: error from h_free_crq %ld\n",
163 				qrc);
164 			rc = ERROR;
165 			break;
166 
167 		case H_BUSY:
168 		case H_LONG_BUSY_ORDER_1_MSEC:
169 			/* msleep not good for small values */
170 			usleep_range(1000, 2000);
171 			ticks += 1;
172 			break;
173 		case H_LONG_BUSY_ORDER_10_MSEC:
174 			usleep_range(10000, 20000);
175 			ticks += 10;
176 			break;
177 		case H_LONG_BUSY_ORDER_100_MSEC:
178 			msleep(100);
179 			ticks += 100;
180 			break;
181 		case H_LONG_BUSY_ORDER_1_SEC:
182 			ssleep(1);
183 			ticks += 1000;
184 			break;
185 		case H_LONG_BUSY_ORDER_10_SEC:
186 			ssleep(10);
187 			ticks += 10000;
188 			break;
189 		case H_LONG_BUSY_ORDER_100_SEC:
190 			ssleep(100);
191 			ticks += 100000;
192 			break;
193 		default:
194 			dev_err(&vscsi->dev, "unregister_command_q: unknown error %ld from h_free_crq\n",
195 				qrc);
196 			rc = ERROR;
197 			break;
198 		}
199 
200 		/*
201 		 * dont wait more then 300 seconds
202 		 * ticks are in milliseconds more or less
203 		 */
204 		if (ticks > 300000 && qrc != H_SUCCESS) {
205 			rc = ERROR;
206 			dev_err(&vscsi->dev, "Excessive wait for h_free_crq\n");
207 		}
208 	} while (qrc != H_SUCCESS && rc == ADAPT_SUCCESS);
209 
210 	pr_debug("Freeing CRQ: phyp rc %ld, rc %ld\n", qrc, rc);
211 
212 	return rc;
213 }
214 
215 /**
216  * ibmvscsis_delete_client_info() - Helper function to Delete Client Info
217  * @vscsi:	Pointer to our adapter structure
218  * @client_closed:	True if client closed its queue
219  *
220  * Deletes information specific to the client when the client goes away
221  *
222  * EXECUTION ENVIRONMENT:
223  *	Interrupt or Process
224  */
225 static void ibmvscsis_delete_client_info(struct scsi_info *vscsi,
226 					 bool client_closed)
227 {
228 	vscsi->client_cap = 0;
229 
230 	/*
231 	 * Some things we don't want to clear if we're closing the queue,
232 	 * because some clients don't resend the host handshake when they
233 	 * get a transport event.
234 	 */
235 	if (client_closed)
236 		vscsi->client_data.os_type = 0;
237 }
238 
239 /**
240  * ibmvscsis_free_command_q() - Free Command Queue
241  * @vscsi:	Pointer to our adapter structure
242  *
243  * This function calls unregister_command_q, then clears interrupts and
244  * any pending interrupt acknowledgments associated with the command q.
245  * It also clears memory if there is no error.
246  *
247  * PHYP did not meet the PAPR architecture so that we must give up the
248  * lock. This causes a timing hole regarding state change.  To close the
249  * hole this routine does accounting on any change that occurred during
250  * the time the lock is not held.
251  * NOTE: must give up and then acquire the interrupt lock, the caller must
252  *	 make sure that state and or flags will prevent interrupt handler from
253  *	 scheduling work.
254  *
255  * EXECUTION ENVIRONMENT:
256  *	Process level, interrupt lock is held
257  */
258 static long ibmvscsis_free_command_q(struct scsi_info *vscsi)
259 {
260 	int bytes;
261 	u32 flags_under_lock;
262 	u16 state_under_lock;
263 	long rc = ADAPT_SUCCESS;
264 
265 	if (!(vscsi->flags & CRQ_CLOSED)) {
266 		vio_disable_interrupts(vscsi->dma_dev);
267 
268 		state_under_lock = vscsi->new_state;
269 		flags_under_lock = vscsi->flags;
270 		vscsi->phyp_acr_state = 0;
271 		vscsi->phyp_acr_flags = 0;
272 
273 		spin_unlock_bh(&vscsi->intr_lock);
274 		rc = ibmvscsis_unregister_command_q(vscsi);
275 		spin_lock_bh(&vscsi->intr_lock);
276 
277 		if (state_under_lock != vscsi->new_state)
278 			vscsi->phyp_acr_state = vscsi->new_state;
279 
280 		vscsi->phyp_acr_flags = ((~flags_under_lock) & vscsi->flags);
281 
282 		if (rc == ADAPT_SUCCESS) {
283 			bytes = vscsi->cmd_q.size * PAGE_SIZE;
284 			memset(vscsi->cmd_q.base_addr, 0, bytes);
285 			vscsi->cmd_q.index = 0;
286 			vscsi->flags |= CRQ_CLOSED;
287 
288 			ibmvscsis_delete_client_info(vscsi, false);
289 		}
290 
291 		pr_debug("free_command_q: flags 0x%x, state 0x%hx, acr_flags 0x%x, acr_state 0x%hx\n",
292 			 vscsi->flags, vscsi->state, vscsi->phyp_acr_flags,
293 			 vscsi->phyp_acr_state);
294 	}
295 	return rc;
296 }
297 
298 /**
299  * ibmvscsis_cmd_q_dequeue() - Get valid Command element
300  * @mask:	Mask to use in case index wraps
301  * @current_index:	Current index into command queue
302  * @base_addr:	Pointer to start of command queue
303  *
304  * Returns a pointer to a valid command element or NULL, if the command
305  * queue is empty
306  *
307  * EXECUTION ENVIRONMENT:
308  *	Interrupt environment, interrupt lock held
309  */
310 static struct viosrp_crq *ibmvscsis_cmd_q_dequeue(uint mask,
311 						  uint *current_index,
312 						  struct viosrp_crq *base_addr)
313 {
314 	struct viosrp_crq *ptr;
315 
316 	ptr = base_addr + *current_index;
317 
318 	if (ptr->valid) {
319 		*current_index = (*current_index + 1) & mask;
320 		dma_rmb();
321 	} else {
322 		ptr = NULL;
323 	}
324 
325 	return ptr;
326 }
327 
328 /**
329  * ibmvscsis_send_init_message() - send initialize message to the client
330  * @vscsi:	Pointer to our adapter structure
331  * @format:	Which Init Message format to send
332  *
333  * EXECUTION ENVIRONMENT:
334  *	Interrupt environment interrupt lock held
335  */
336 static long ibmvscsis_send_init_message(struct scsi_info *vscsi, u8 format)
337 {
338 	struct viosrp_crq *crq;
339 	u64 buffer[2] = { 0, 0 };
340 	long rc;
341 
342 	crq = (struct viosrp_crq *)&buffer;
343 	crq->valid = VALID_INIT_MSG;
344 	crq->format = format;
345 	rc = h_send_crq(vscsi->dds.unit_id, cpu_to_be64(buffer[MSG_HI]),
346 			cpu_to_be64(buffer[MSG_LOW]));
347 
348 	return rc;
349 }
350 
351 /**
352  * ibmvscsis_check_init_msg() - Check init message valid
353  * @vscsi:	Pointer to our adapter structure
354  * @format:	Pointer to return format of Init Message, if any.
355  *		Set to UNUSED_FORMAT if no Init Message in queue.
356  *
357  * Checks if an initialize message was queued by the initiatior
358  * after the queue was created and before the interrupt was enabled.
359  *
360  * EXECUTION ENVIRONMENT:
361  *	Process level only, interrupt lock held
362  */
363 static long ibmvscsis_check_init_msg(struct scsi_info *vscsi, uint *format)
364 {
365 	struct viosrp_crq *crq;
366 	long rc = ADAPT_SUCCESS;
367 
368 	crq = ibmvscsis_cmd_q_dequeue(vscsi->cmd_q.mask, &vscsi->cmd_q.index,
369 				      vscsi->cmd_q.base_addr);
370 	if (!crq) {
371 		*format = (uint)UNUSED_FORMAT;
372 	} else if (crq->valid == VALID_INIT_MSG && crq->format == INIT_MSG) {
373 		*format = (uint)INIT_MSG;
374 		crq->valid = INVALIDATE_CMD_RESP_EL;
375 		dma_rmb();
376 
377 		/*
378 		 * the caller has ensured no initialize message was
379 		 * sent after the queue was
380 		 * created so there should be no other message on the queue.
381 		 */
382 		crq = ibmvscsis_cmd_q_dequeue(vscsi->cmd_q.mask,
383 					      &vscsi->cmd_q.index,
384 					      vscsi->cmd_q.base_addr);
385 		if (crq) {
386 			*format = (uint)(crq->format);
387 			rc = ERROR;
388 			crq->valid = INVALIDATE_CMD_RESP_EL;
389 			dma_rmb();
390 		}
391 	} else {
392 		*format = (uint)(crq->format);
393 		rc = ERROR;
394 		crq->valid = INVALIDATE_CMD_RESP_EL;
395 		dma_rmb();
396 	}
397 
398 	return rc;
399 }
400 
401 /**
402  * ibmvscsis_disconnect() - Helper function to disconnect
403  * @work:	Pointer to work_struct, gives access to our adapter structure
404  *
405  * An error has occurred or the driver received a Transport event,
406  * and the driver is requesting that the command queue be de-registered
407  * in a safe manner. If there is no outstanding I/O then we can stop the
408  * queue. If we are restarting the queue it will be reflected in the
409  * the state of the adapter.
410  *
411  * EXECUTION ENVIRONMENT:
412  *	Process environment
413  */
414 static void ibmvscsis_disconnect(struct work_struct *work)
415 {
416 	struct scsi_info *vscsi = container_of(work, struct scsi_info,
417 					       proc_work);
418 	u16 new_state;
419 	bool wait_idle = false;
420 
421 	spin_lock_bh(&vscsi->intr_lock);
422 	new_state = vscsi->new_state;
423 	vscsi->new_state = 0;
424 
425 	pr_debug("disconnect: flags 0x%x, state 0x%hx\n", vscsi->flags,
426 		 vscsi->state);
427 
428 	/*
429 	 * check which state we are in and see if we
430 	 * should transitition to the new state
431 	 */
432 	switch (vscsi->state) {
433 	/* Should never be called while in this state. */
434 	case NO_QUEUE:
435 	/*
436 	 * Can never transition from this state;
437 	 * igonore errors and logout.
438 	 */
439 	case UNCONFIGURING:
440 		break;
441 
442 	/* can transition from this state to UNCONFIGURING */
443 	case ERR_DISCONNECT:
444 		if (new_state == UNCONFIGURING)
445 			vscsi->state = new_state;
446 		break;
447 
448 	/*
449 	 * Can transition from this state to to unconfiguring
450 	 * or err disconnect.
451 	 */
452 	case ERR_DISCONNECT_RECONNECT:
453 		switch (new_state) {
454 		case UNCONFIGURING:
455 		case ERR_DISCONNECT:
456 			vscsi->state = new_state;
457 			break;
458 
459 		case WAIT_IDLE:
460 			break;
461 		default:
462 			break;
463 		}
464 		break;
465 
466 	/* can transition from this state to UNCONFIGURING */
467 	case ERR_DISCONNECTED:
468 		if (new_state == UNCONFIGURING)
469 			vscsi->state = new_state;
470 		break;
471 
472 	case WAIT_ENABLED:
473 		switch (new_state) {
474 		case UNCONFIGURING:
475 			vscsi->state = new_state;
476 			vscsi->flags |= RESPONSE_Q_DOWN;
477 			vscsi->flags &= ~(SCHEDULE_DISCONNECT |
478 					  DISCONNECT_SCHEDULED);
479 			dma_rmb();
480 			if (vscsi->flags & CFG_SLEEPING) {
481 				vscsi->flags &= ~CFG_SLEEPING;
482 				complete(&vscsi->unconfig);
483 			}
484 			break;
485 
486 		/* should never happen */
487 		case ERR_DISCONNECT:
488 		case ERR_DISCONNECT_RECONNECT:
489 		case WAIT_IDLE:
490 			dev_err(&vscsi->dev, "disconnect: invalid state %d for WAIT_IDLE\n",
491 				vscsi->state);
492 			break;
493 		}
494 		break;
495 
496 	case WAIT_IDLE:
497 		switch (new_state) {
498 		case UNCONFIGURING:
499 			vscsi->flags |= RESPONSE_Q_DOWN;
500 			vscsi->state = new_state;
501 			vscsi->flags &= ~(SCHEDULE_DISCONNECT |
502 					  DISCONNECT_SCHEDULED);
503 			ibmvscsis_free_command_q(vscsi);
504 			break;
505 		case ERR_DISCONNECT:
506 		case ERR_DISCONNECT_RECONNECT:
507 			vscsi->state = new_state;
508 			break;
509 		}
510 		break;
511 
512 	/*
513 	 * Initiator has not done a successful srp login
514 	 * or has done a successful srp logout ( adapter was not
515 	 * busy). In the first case there can be responses queued
516 	 * waiting for space on the initiators response queue (MAD)
517 	 * The second case the adapter is idle. Assume the worse case,
518 	 * i.e. the second case.
519 	 */
520 	case WAIT_CONNECTION:
521 	case CONNECTED:
522 	case SRP_PROCESSING:
523 		wait_idle = true;
524 		vscsi->state = new_state;
525 		break;
526 
527 	/* can transition from this state to UNCONFIGURING */
528 	case UNDEFINED:
529 		if (new_state == UNCONFIGURING)
530 			vscsi->state = new_state;
531 		break;
532 	default:
533 		break;
534 	}
535 
536 	if (wait_idle) {
537 		pr_debug("disconnect start wait, active %d, sched %d\n",
538 			 (int)list_empty(&vscsi->active_q),
539 			 (int)list_empty(&vscsi->schedule_q));
540 		if (!list_empty(&vscsi->active_q) ||
541 		    !list_empty(&vscsi->schedule_q)) {
542 			vscsi->flags |= WAIT_FOR_IDLE;
543 			pr_debug("disconnect flags 0x%x\n", vscsi->flags);
544 			/*
545 			 * This routine is can not be called with the interrupt
546 			 * lock held.
547 			 */
548 			spin_unlock_bh(&vscsi->intr_lock);
549 			wait_for_completion(&vscsi->wait_idle);
550 			spin_lock_bh(&vscsi->intr_lock);
551 		}
552 		pr_debug("disconnect stop wait\n");
553 
554 		ibmvscsis_adapter_idle(vscsi);
555 	}
556 
557 	spin_unlock_bh(&vscsi->intr_lock);
558 }
559 
560 /**
561  * ibmvscsis_post_disconnect() - Schedule the disconnect
562  * @vscsi:	Pointer to our adapter structure
563  * @new_state:	State to move to after disconnecting
564  * @flag_bits:	Flags to turn on in adapter structure
565  *
566  * If it's already been scheduled, then see if we need to "upgrade"
567  * the new state (if the one passed in is more "severe" than the
568  * previous one).
569  *
570  * PRECONDITION:
571  *	interrupt lock is held
572  */
573 static void ibmvscsis_post_disconnect(struct scsi_info *vscsi, uint new_state,
574 				      uint flag_bits)
575 {
576 	uint state;
577 
578 	/* check the validity of the new state */
579 	switch (new_state) {
580 	case UNCONFIGURING:
581 	case ERR_DISCONNECT:
582 	case ERR_DISCONNECT_RECONNECT:
583 	case WAIT_IDLE:
584 		break;
585 
586 	default:
587 		dev_err(&vscsi->dev, "post_disconnect: Invalid new state %d\n",
588 			new_state);
589 		return;
590 	}
591 
592 	vscsi->flags |= flag_bits;
593 
594 	pr_debug("post_disconnect: new_state 0x%x, flag_bits 0x%x, vscsi->flags 0x%x, state %hx\n",
595 		 new_state, flag_bits, vscsi->flags, vscsi->state);
596 
597 	if (!(vscsi->flags & (DISCONNECT_SCHEDULED | SCHEDULE_DISCONNECT))) {
598 		vscsi->flags |= SCHEDULE_DISCONNECT;
599 		vscsi->new_state = new_state;
600 
601 		INIT_WORK(&vscsi->proc_work, ibmvscsis_disconnect);
602 		(void)queue_work(vscsi->work_q, &vscsi->proc_work);
603 	} else {
604 		if (vscsi->new_state)
605 			state = vscsi->new_state;
606 		else
607 			state = vscsi->state;
608 
609 		switch (state) {
610 		case NO_QUEUE:
611 		case UNCONFIGURING:
612 			break;
613 
614 		case ERR_DISCONNECTED:
615 		case ERR_DISCONNECT:
616 		case UNDEFINED:
617 			if (new_state == UNCONFIGURING)
618 				vscsi->new_state = new_state;
619 			break;
620 
621 		case ERR_DISCONNECT_RECONNECT:
622 			switch (new_state) {
623 			case UNCONFIGURING:
624 			case ERR_DISCONNECT:
625 				vscsi->new_state = new_state;
626 				break;
627 			default:
628 				break;
629 			}
630 			break;
631 
632 		case WAIT_ENABLED:
633 		case WAIT_IDLE:
634 		case WAIT_CONNECTION:
635 		case CONNECTED:
636 		case SRP_PROCESSING:
637 			vscsi->new_state = new_state;
638 			break;
639 
640 		default:
641 			break;
642 		}
643 	}
644 
645 	pr_debug("Leaving post_disconnect: flags 0x%x, new_state 0x%x\n",
646 		 vscsi->flags, vscsi->new_state);
647 }
648 
649 /**
650  * ibmvscsis_handle_init_compl_msg() - Respond to an Init Complete Message
651  * @vscsi:	Pointer to our adapter structure
652  *
653  * Must be called with interrupt lock held.
654  */
655 static long ibmvscsis_handle_init_compl_msg(struct scsi_info *vscsi)
656 {
657 	long rc = ADAPT_SUCCESS;
658 
659 	switch (vscsi->state) {
660 	case NO_QUEUE:
661 	case ERR_DISCONNECT:
662 	case ERR_DISCONNECT_RECONNECT:
663 	case ERR_DISCONNECTED:
664 	case UNCONFIGURING:
665 	case UNDEFINED:
666 		rc = ERROR;
667 		break;
668 
669 	case WAIT_CONNECTION:
670 		vscsi->state = CONNECTED;
671 		break;
672 
673 	case WAIT_IDLE:
674 	case SRP_PROCESSING:
675 	case CONNECTED:
676 	case WAIT_ENABLED:
677 	default:
678 		rc = ERROR;
679 		dev_err(&vscsi->dev, "init_msg: invalid state %d to get init compl msg\n",
680 			vscsi->state);
681 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
682 		break;
683 	}
684 
685 	return rc;
686 }
687 
688 /**
689  * ibmvscsis_handle_init_msg() - Respond to an Init Message
690  * @vscsi:	Pointer to our adapter structure
691  *
692  * Must be called with interrupt lock held.
693  */
694 static long ibmvscsis_handle_init_msg(struct scsi_info *vscsi)
695 {
696 	long rc = ADAPT_SUCCESS;
697 
698 	switch (vscsi->state) {
699 	case WAIT_CONNECTION:
700 		rc = ibmvscsis_send_init_message(vscsi, INIT_COMPLETE_MSG);
701 		switch (rc) {
702 		case H_SUCCESS:
703 			vscsi->state = CONNECTED;
704 			break;
705 
706 		case H_PARAMETER:
707 			dev_err(&vscsi->dev, "init_msg: failed to send, rc %ld\n",
708 				rc);
709 			ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
710 			break;
711 
712 		case H_DROPPED:
713 			dev_err(&vscsi->dev, "init_msg: failed to send, rc %ld\n",
714 				rc);
715 			rc = ERROR;
716 			ibmvscsis_post_disconnect(vscsi,
717 						  ERR_DISCONNECT_RECONNECT, 0);
718 			break;
719 
720 		case H_CLOSED:
721 			pr_warn("init_msg: failed to send, rc %ld\n", rc);
722 			rc = 0;
723 			break;
724 		}
725 		break;
726 
727 	case UNDEFINED:
728 		rc = ERROR;
729 		break;
730 
731 	case UNCONFIGURING:
732 		break;
733 
734 	case WAIT_ENABLED:
735 	case CONNECTED:
736 	case SRP_PROCESSING:
737 	case WAIT_IDLE:
738 	case NO_QUEUE:
739 	case ERR_DISCONNECT:
740 	case ERR_DISCONNECT_RECONNECT:
741 	case ERR_DISCONNECTED:
742 	default:
743 		rc = ERROR;
744 		dev_err(&vscsi->dev, "init_msg: invalid state %d to get init msg\n",
745 			vscsi->state);
746 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
747 		break;
748 	}
749 
750 	return rc;
751 }
752 
753 /**
754  * ibmvscsis_init_msg() - Respond to an init message
755  * @vscsi:	Pointer to our adapter structure
756  * @crq:	Pointer to CRQ element containing the Init Message
757  *
758  * EXECUTION ENVIRONMENT:
759  *	Interrupt, interrupt lock held
760  */
761 static long ibmvscsis_init_msg(struct scsi_info *vscsi, struct viosrp_crq *crq)
762 {
763 	long rc = ADAPT_SUCCESS;
764 
765 	pr_debug("init_msg: state 0x%hx\n", vscsi->state);
766 
767 	rc = h_vioctl(vscsi->dds.unit_id, H_GET_PARTNER_INFO,
768 		      (u64)vscsi->map_ioba | ((u64)PAGE_SIZE << 32), 0, 0, 0,
769 		      0);
770 	if (rc == H_SUCCESS) {
771 		vscsi->client_data.partition_number =
772 			be64_to_cpu(*(u64 *)vscsi->map_buf);
773 		pr_debug("init_msg, part num %d\n",
774 			 vscsi->client_data.partition_number);
775 	} else {
776 		pr_debug("init_msg h_vioctl rc %ld\n", rc);
777 		rc = ADAPT_SUCCESS;
778 	}
779 
780 	if (crq->format == INIT_MSG) {
781 		rc = ibmvscsis_handle_init_msg(vscsi);
782 	} else if (crq->format == INIT_COMPLETE_MSG) {
783 		rc = ibmvscsis_handle_init_compl_msg(vscsi);
784 	} else {
785 		rc = ERROR;
786 		dev_err(&vscsi->dev, "init_msg: invalid format %d\n",
787 			(uint)crq->format);
788 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
789 	}
790 
791 	return rc;
792 }
793 
794 /**
795  * ibmvscsis_establish_new_q() - Establish new CRQ queue
796  * @vscsi:	Pointer to our adapter structure
797  *
798  * Must be called with interrupt lock held.
799  */
800 static long ibmvscsis_establish_new_q(struct scsi_info *vscsi)
801 {
802 	long rc = ADAPT_SUCCESS;
803 	uint format;
804 
805 	vscsi->flags &= PRESERVE_FLAG_FIELDS;
806 	vscsi->rsp_q_timer.timer_pops = 0;
807 	vscsi->debit = 0;
808 	vscsi->credit = 0;
809 
810 	rc = vio_enable_interrupts(vscsi->dma_dev);
811 	if (rc) {
812 		pr_warn("establish_new_q: failed to enable interrupts, rc %ld\n",
813 			rc);
814 		return rc;
815 	}
816 
817 	rc = ibmvscsis_check_init_msg(vscsi, &format);
818 	if (rc) {
819 		dev_err(&vscsi->dev, "establish_new_q: check_init_msg failed, rc %ld\n",
820 			rc);
821 		return rc;
822 	}
823 
824 	if (format == UNUSED_FORMAT) {
825 		rc = ibmvscsis_send_init_message(vscsi, INIT_MSG);
826 		switch (rc) {
827 		case H_SUCCESS:
828 		case H_DROPPED:
829 		case H_CLOSED:
830 			rc = ADAPT_SUCCESS;
831 			break;
832 
833 		case H_PARAMETER:
834 		case H_HARDWARE:
835 			break;
836 
837 		default:
838 			vscsi->state = UNDEFINED;
839 			rc = H_HARDWARE;
840 			break;
841 		}
842 	} else if (format == INIT_MSG) {
843 		rc = ibmvscsis_handle_init_msg(vscsi);
844 	}
845 
846 	return rc;
847 }
848 
849 /**
850  * ibmvscsis_reset_queue() - Reset CRQ Queue
851  * @vscsi:	Pointer to our adapter structure
852  *
853  * This function calls h_free_q and then calls h_reg_q and does all
854  * of the bookkeeping to get us back to where we can communicate.
855  *
856  * Actually, we don't always call h_free_crq.  A problem was discovered
857  * where one partition would close and reopen his queue, which would
858  * cause his partner to get a transport event, which would cause him to
859  * close and reopen his queue, which would cause the original partition
860  * to get a transport event, etc., etc.  To prevent this, we don't
861  * actually close our queue if the client initiated the reset, (i.e.
862  * either we got a transport event or we have detected that the client's
863  * queue is gone)
864  *
865  * EXECUTION ENVIRONMENT:
866  *	Process environment, called with interrupt lock held
867  */
868 static void ibmvscsis_reset_queue(struct scsi_info *vscsi)
869 {
870 	int bytes;
871 	long rc = ADAPT_SUCCESS;
872 
873 	pr_debug("reset_queue: flags 0x%x\n", vscsi->flags);
874 
875 	/* don't reset, the client did it for us */
876 	if (vscsi->flags & (CLIENT_FAILED | TRANS_EVENT)) {
877 		vscsi->flags &= PRESERVE_FLAG_FIELDS;
878 		vscsi->rsp_q_timer.timer_pops = 0;
879 		vscsi->debit = 0;
880 		vscsi->credit = 0;
881 		vscsi->state = WAIT_CONNECTION;
882 		vio_enable_interrupts(vscsi->dma_dev);
883 	} else {
884 		rc = ibmvscsis_free_command_q(vscsi);
885 		if (rc == ADAPT_SUCCESS) {
886 			vscsi->state = WAIT_CONNECTION;
887 
888 			bytes = vscsi->cmd_q.size * PAGE_SIZE;
889 			rc = h_reg_crq(vscsi->dds.unit_id,
890 				       vscsi->cmd_q.crq_token, bytes);
891 			if (rc == H_CLOSED || rc == H_SUCCESS) {
892 				rc = ibmvscsis_establish_new_q(vscsi);
893 			}
894 
895 			if (rc != ADAPT_SUCCESS) {
896 				pr_debug("reset_queue: reg_crq rc %ld\n", rc);
897 
898 				vscsi->state = ERR_DISCONNECTED;
899 				vscsi->flags |= RESPONSE_Q_DOWN;
900 				ibmvscsis_free_command_q(vscsi);
901 			}
902 		} else {
903 			vscsi->state = ERR_DISCONNECTED;
904 			vscsi->flags |= RESPONSE_Q_DOWN;
905 		}
906 	}
907 }
908 
909 /**
910  * ibmvscsis_free_cmd_resources() - Free command resources
911  * @vscsi:	Pointer to our adapter structure
912  * @cmd:	Command which is not longer in use
913  *
914  * Must be called with interrupt lock held.
915  */
916 static void ibmvscsis_free_cmd_resources(struct scsi_info *vscsi,
917 					 struct ibmvscsis_cmd *cmd)
918 {
919 	struct iu_entry *iue = cmd->iue;
920 
921 	switch (cmd->type) {
922 	case TASK_MANAGEMENT:
923 	case SCSI_CDB:
924 		/*
925 		 * When the queue goes down this value is cleared, so it
926 		 * cannot be cleared in this general purpose function.
927 		 */
928 		if (vscsi->debit)
929 			vscsi->debit -= 1;
930 		break;
931 	case ADAPTER_MAD:
932 		vscsi->flags &= ~PROCESSING_MAD;
933 		break;
934 	case UNSET_TYPE:
935 		break;
936 	default:
937 		dev_err(&vscsi->dev, "free_cmd_resources unknown type %d\n",
938 			cmd->type);
939 		break;
940 	}
941 
942 	cmd->iue = NULL;
943 	list_add_tail(&cmd->list, &vscsi->free_cmd);
944 	srp_iu_put(iue);
945 
946 	if (list_empty(&vscsi->active_q) && list_empty(&vscsi->schedule_q) &&
947 	    list_empty(&vscsi->waiting_rsp) && (vscsi->flags & WAIT_FOR_IDLE)) {
948 		vscsi->flags &= ~WAIT_FOR_IDLE;
949 		complete(&vscsi->wait_idle);
950 	}
951 }
952 
953 /**
954  * ibmvscsis_trans_event() - Handle a Transport Event
955  * @vscsi:	Pointer to our adapter structure
956  * @crq:	Pointer to CRQ entry containing the Transport Event
957  *
958  * Do the logic to close the I_T nexus.  This function may not
959  * behave to specification.
960  *
961  * EXECUTION ENVIRONMENT:
962  *	Interrupt, interrupt lock held
963  */
964 static long ibmvscsis_trans_event(struct scsi_info *vscsi,
965 				  struct viosrp_crq *crq)
966 {
967 	long rc = ADAPT_SUCCESS;
968 
969 	pr_debug("trans_event: format %d, flags 0x%x, state 0x%hx\n",
970 		 (int)crq->format, vscsi->flags, vscsi->state);
971 
972 	switch (crq->format) {
973 	case MIGRATED:
974 	case PARTNER_FAILED:
975 	case PARTNER_DEREGISTER:
976 		ibmvscsis_delete_client_info(vscsi, true);
977 		break;
978 
979 	default:
980 		rc = ERROR;
981 		dev_err(&vscsi->dev, "trans_event: invalid format %d\n",
982 			(uint)crq->format);
983 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT,
984 					  RESPONSE_Q_DOWN);
985 		break;
986 	}
987 
988 	if (rc == ADAPT_SUCCESS) {
989 		switch (vscsi->state) {
990 		case NO_QUEUE:
991 		case ERR_DISCONNECTED:
992 		case UNDEFINED:
993 			break;
994 
995 		case UNCONFIGURING:
996 			vscsi->flags |= (RESPONSE_Q_DOWN | TRANS_EVENT);
997 			break;
998 
999 		case WAIT_ENABLED:
1000 			break;
1001 
1002 		case WAIT_CONNECTION:
1003 			break;
1004 
1005 		case CONNECTED:
1006 			ibmvscsis_post_disconnect(vscsi, WAIT_IDLE,
1007 						  (RESPONSE_Q_DOWN |
1008 						   TRANS_EVENT));
1009 			break;
1010 
1011 		case SRP_PROCESSING:
1012 			if ((vscsi->debit > 0) ||
1013 			    !list_empty(&vscsi->schedule_q) ||
1014 			    !list_empty(&vscsi->waiting_rsp) ||
1015 			    !list_empty(&vscsi->active_q)) {
1016 				pr_debug("debit %d, sched %d, wait %d, active %d\n",
1017 					 vscsi->debit,
1018 					 (int)list_empty(&vscsi->schedule_q),
1019 					 (int)list_empty(&vscsi->waiting_rsp),
1020 					 (int)list_empty(&vscsi->active_q));
1021 				pr_warn("connection lost with outstanding work\n");
1022 			} else {
1023 				pr_debug("trans_event: SRP Processing, but no outstanding work\n");
1024 			}
1025 
1026 			ibmvscsis_post_disconnect(vscsi, WAIT_IDLE,
1027 						  (RESPONSE_Q_DOWN |
1028 						   TRANS_EVENT));
1029 			break;
1030 
1031 		case ERR_DISCONNECT:
1032 		case ERR_DISCONNECT_RECONNECT:
1033 		case WAIT_IDLE:
1034 			vscsi->flags |= (RESPONSE_Q_DOWN | TRANS_EVENT);
1035 			break;
1036 		}
1037 	}
1038 
1039 	rc = vscsi->flags & SCHEDULE_DISCONNECT;
1040 
1041 	pr_debug("Leaving trans_event: flags 0x%x, state 0x%hx, rc %ld\n",
1042 		 vscsi->flags, vscsi->state, rc);
1043 
1044 	return rc;
1045 }
1046 
1047 /**
1048  * ibmvscsis_poll_cmd_q() - Poll Command Queue
1049  * @vscsi:	Pointer to our adapter structure
1050  *
1051  * Called to handle command elements that may have arrived while
1052  * interrupts were disabled.
1053  *
1054  * EXECUTION ENVIRONMENT:
1055  *	intr_lock must be held
1056  */
1057 static void ibmvscsis_poll_cmd_q(struct scsi_info *vscsi)
1058 {
1059 	struct viosrp_crq *crq;
1060 	long rc;
1061 	bool ack = true;
1062 	volatile u8 valid;
1063 
1064 	pr_debug("poll_cmd_q: flags 0x%x, state 0x%hx, q index %ud\n",
1065 		 vscsi->flags, vscsi->state, vscsi->cmd_q.index);
1066 
1067 	rc = vscsi->flags & SCHEDULE_DISCONNECT;
1068 	crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
1069 	valid = crq->valid;
1070 	dma_rmb();
1071 
1072 	while (valid) {
1073 poll_work:
1074 		vscsi->cmd_q.index =
1075 			(vscsi->cmd_q.index + 1) & vscsi->cmd_q.mask;
1076 
1077 		if (!rc) {
1078 			rc = ibmvscsis_parse_command(vscsi, crq);
1079 		} else {
1080 			if ((uint)crq->valid == VALID_TRANS_EVENT) {
1081 				/*
1082 				 * must service the transport layer events even
1083 				 * in an error state, dont break out until all
1084 				 * the consecutive transport events have been
1085 				 * processed
1086 				 */
1087 				rc = ibmvscsis_trans_event(vscsi, crq);
1088 			} else if (vscsi->flags & TRANS_EVENT) {
1089 				/*
1090 				 * if a tranport event has occurred leave
1091 				 * everything but transport events on the queue
1092 				 */
1093 				pr_debug("poll_cmd_q, ignoring\n");
1094 
1095 				/*
1096 				 * need to decrement the queue index so we can
1097 				 * look at the elment again
1098 				 */
1099 				if (vscsi->cmd_q.index)
1100 					vscsi->cmd_q.index -= 1;
1101 				else
1102 					/*
1103 					 * index is at 0 it just wrapped.
1104 					 * have it index last element in q
1105 					 */
1106 					vscsi->cmd_q.index = vscsi->cmd_q.mask;
1107 				break;
1108 			}
1109 		}
1110 
1111 		crq->valid = INVALIDATE_CMD_RESP_EL;
1112 
1113 		crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
1114 		valid = crq->valid;
1115 		dma_rmb();
1116 	}
1117 
1118 	if (!rc) {
1119 		if (ack) {
1120 			vio_enable_interrupts(vscsi->dma_dev);
1121 			ack = false;
1122 			pr_debug("poll_cmd_q, reenabling interrupts\n");
1123 		}
1124 		valid = crq->valid;
1125 		dma_rmb();
1126 		if (valid)
1127 			goto poll_work;
1128 	}
1129 
1130 	pr_debug("Leaving poll_cmd_q: rc %ld\n", rc);
1131 }
1132 
1133 /**
1134  * ibmvscsis_free_cmd_qs() - Free elements in queue
1135  * @vscsi:	Pointer to our adapter structure
1136  *
1137  * Free all of the elements on all queues that are waiting for
1138  * whatever reason.
1139  *
1140  * PRECONDITION:
1141  *	Called with interrupt lock held
1142  */
1143 static void ibmvscsis_free_cmd_qs(struct scsi_info *vscsi)
1144 {
1145 	struct ibmvscsis_cmd *cmd, *nxt;
1146 
1147 	pr_debug("free_cmd_qs: waiting_rsp empty %d, timer starter %d\n",
1148 		 (int)list_empty(&vscsi->waiting_rsp),
1149 		 vscsi->rsp_q_timer.started);
1150 
1151 	list_for_each_entry_safe(cmd, nxt, &vscsi->waiting_rsp, list) {
1152 		list_del(&cmd->list);
1153 		ibmvscsis_free_cmd_resources(vscsi, cmd);
1154 	}
1155 }
1156 
1157 /**
1158  * ibmvscsis_get_free_cmd() - Get free command from list
1159  * @vscsi:	Pointer to our adapter structure
1160  *
1161  * Must be called with interrupt lock held.
1162  */
1163 static struct ibmvscsis_cmd *ibmvscsis_get_free_cmd(struct scsi_info *vscsi)
1164 {
1165 	struct ibmvscsis_cmd *cmd = NULL;
1166 	struct iu_entry *iue;
1167 
1168 	iue = srp_iu_get(&vscsi->target);
1169 	if (iue) {
1170 		cmd = list_first_entry_or_null(&vscsi->free_cmd,
1171 					       struct ibmvscsis_cmd, list);
1172 		if (cmd) {
1173 			list_del(&cmd->list);
1174 			cmd->iue = iue;
1175 			cmd->type = UNSET_TYPE;
1176 			memset(&cmd->se_cmd, 0, sizeof(cmd->se_cmd));
1177 		} else {
1178 			srp_iu_put(iue);
1179 		}
1180 	}
1181 
1182 	return cmd;
1183 }
1184 
1185 /**
1186  * ibmvscsis_adapter_idle() - Helper function to handle idle adapter
1187  * @vscsi:	Pointer to our adapter structure
1188  *
1189  * This function is called when the adapter is idle when the driver
1190  * is attempting to clear an error condition.
1191  * The adapter is considered busy if any of its cmd queues
1192  * are non-empty. This function can be invoked
1193  * from the off level disconnect function.
1194  *
1195  * EXECUTION ENVIRONMENT:
1196  *	Process environment called with interrupt lock held
1197  */
1198 static void ibmvscsis_adapter_idle(struct scsi_info *vscsi)
1199 {
1200 	int free_qs = false;
1201 
1202 	pr_debug("adapter_idle: flags 0x%x, state 0x%hx\n", vscsi->flags,
1203 		 vscsi->state);
1204 
1205 	/* Only need to free qs if we're disconnecting from client */
1206 	if (vscsi->state != WAIT_CONNECTION || vscsi->flags & TRANS_EVENT)
1207 		free_qs = true;
1208 
1209 	switch (vscsi->state) {
1210 	case UNCONFIGURING:
1211 		ibmvscsis_free_command_q(vscsi);
1212 		dma_rmb();
1213 		isync();
1214 		if (vscsi->flags & CFG_SLEEPING) {
1215 			vscsi->flags &= ~CFG_SLEEPING;
1216 			complete(&vscsi->unconfig);
1217 		}
1218 		break;
1219 	case ERR_DISCONNECT_RECONNECT:
1220 		ibmvscsis_reset_queue(vscsi);
1221 		pr_debug("adapter_idle, disc_rec: flags 0x%x\n", vscsi->flags);
1222 		break;
1223 
1224 	case ERR_DISCONNECT:
1225 		ibmvscsis_free_command_q(vscsi);
1226 		vscsi->flags &= ~(SCHEDULE_DISCONNECT | DISCONNECT_SCHEDULED);
1227 		vscsi->flags |= RESPONSE_Q_DOWN;
1228 		if (vscsi->tport.enabled)
1229 			vscsi->state = ERR_DISCONNECTED;
1230 		else
1231 			vscsi->state = WAIT_ENABLED;
1232 		pr_debug("adapter_idle, disc: flags 0x%x, state 0x%hx\n",
1233 			 vscsi->flags, vscsi->state);
1234 		break;
1235 
1236 	case WAIT_IDLE:
1237 		vscsi->rsp_q_timer.timer_pops = 0;
1238 		vscsi->debit = 0;
1239 		vscsi->credit = 0;
1240 		if (vscsi->flags & TRANS_EVENT) {
1241 			vscsi->state = WAIT_CONNECTION;
1242 			vscsi->flags &= PRESERVE_FLAG_FIELDS;
1243 		} else {
1244 			vscsi->state = CONNECTED;
1245 			vscsi->flags &= ~DISCONNECT_SCHEDULED;
1246 		}
1247 
1248 		pr_debug("adapter_idle, wait: flags 0x%x, state 0x%hx\n",
1249 			 vscsi->flags, vscsi->state);
1250 		ibmvscsis_poll_cmd_q(vscsi);
1251 		break;
1252 
1253 	case ERR_DISCONNECTED:
1254 		vscsi->flags &= ~DISCONNECT_SCHEDULED;
1255 		pr_debug("adapter_idle, disconnected: flags 0x%x, state 0x%hx\n",
1256 			 vscsi->flags, vscsi->state);
1257 		break;
1258 
1259 	default:
1260 		dev_err(&vscsi->dev, "adapter_idle: in invalid state %d\n",
1261 			vscsi->state);
1262 		break;
1263 	}
1264 
1265 	if (free_qs)
1266 		ibmvscsis_free_cmd_qs(vscsi);
1267 
1268 	/*
1269 	 * There is a timing window where we could lose a disconnect request.
1270 	 * The known path to this window occurs during the DISCONNECT_RECONNECT
1271 	 * case above: reset_queue calls free_command_q, which will release the
1272 	 * interrupt lock.  During that time, a new post_disconnect call can be
1273 	 * made with a "more severe" state (DISCONNECT or UNCONFIGURING).
1274 	 * Because the DISCONNECT_SCHEDULED flag is already set, post_disconnect
1275 	 * will only set the new_state.  Now free_command_q reacquires the intr
1276 	 * lock and clears the DISCONNECT_SCHEDULED flag (using PRESERVE_FLAG_
1277 	 * FIELDS), and the disconnect is lost.  This is particularly bad when
1278 	 * the new disconnect was for UNCONFIGURING, since the unconfigure hangs
1279 	 * forever.
1280 	 * Fix is that free command queue sets acr state and acr flags if there
1281 	 * is a change under the lock
1282 	 * note free command queue writes to this state it clears it
1283 	 * before releasing the lock, different drivers call the free command
1284 	 * queue different times so dont initialize above
1285 	 */
1286 	if (vscsi->phyp_acr_state != 0)	{
1287 		/*
1288 		 * set any bits in flags that may have been cleared by
1289 		 * a call to free command queue in switch statement
1290 		 * or reset queue
1291 		 */
1292 		vscsi->flags |= vscsi->phyp_acr_flags;
1293 		ibmvscsis_post_disconnect(vscsi, vscsi->phyp_acr_state, 0);
1294 		vscsi->phyp_acr_state = 0;
1295 		vscsi->phyp_acr_flags = 0;
1296 
1297 		pr_debug("adapter_idle: flags 0x%x, state 0x%hx, acr_flags 0x%x, acr_state 0x%hx\n",
1298 			 vscsi->flags, vscsi->state, vscsi->phyp_acr_flags,
1299 			 vscsi->phyp_acr_state);
1300 	}
1301 
1302 	pr_debug("Leaving adapter_idle: flags 0x%x, state 0x%hx, new_state 0x%x\n",
1303 		 vscsi->flags, vscsi->state, vscsi->new_state);
1304 }
1305 
1306 /**
1307  * ibmvscsis_copy_crq_packet() - Copy CRQ Packet
1308  * @vscsi:	Pointer to our adapter structure
1309  * @cmd:	Pointer to command element to use to process the request
1310  * @crq:	Pointer to CRQ entry containing the request
1311  *
1312  * Copy the srp information unit from the hosted
1313  * partition using remote dma
1314  *
1315  * EXECUTION ENVIRONMENT:
1316  *	Interrupt, interrupt lock held
1317  */
1318 static long ibmvscsis_copy_crq_packet(struct scsi_info *vscsi,
1319 				      struct ibmvscsis_cmd *cmd,
1320 				      struct viosrp_crq *crq)
1321 {
1322 	struct iu_entry *iue = cmd->iue;
1323 	long rc = 0;
1324 	u16 len;
1325 
1326 	len = be16_to_cpu(crq->IU_length);
1327 	if ((len > SRP_MAX_IU_LEN) || (len == 0)) {
1328 		dev_err(&vscsi->dev, "copy_crq: Invalid len %d passed", len);
1329 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
1330 		return SRP_VIOLATION;
1331 	}
1332 
1333 	rc = h_copy_rdma(len, vscsi->dds.window[REMOTE].liobn,
1334 			 be64_to_cpu(crq->IU_data_ptr),
1335 			 vscsi->dds.window[LOCAL].liobn, iue->sbuf->dma);
1336 
1337 	switch (rc) {
1338 	case H_SUCCESS:
1339 		cmd->init_time = mftb();
1340 		iue->remote_token = crq->IU_data_ptr;
1341 		iue->iu_len = len;
1342 		pr_debug("copy_crq: ioba 0x%llx, init_time 0x%llx\n",
1343 			 be64_to_cpu(crq->IU_data_ptr), cmd->init_time);
1344 		break;
1345 	case H_PERMISSION:
1346 		if (connection_broken(vscsi))
1347 			ibmvscsis_post_disconnect(vscsi,
1348 						  ERR_DISCONNECT_RECONNECT,
1349 						  (RESPONSE_Q_DOWN |
1350 						   CLIENT_FAILED));
1351 		else
1352 			ibmvscsis_post_disconnect(vscsi,
1353 						  ERR_DISCONNECT_RECONNECT, 0);
1354 
1355 		dev_err(&vscsi->dev, "copy_crq: h_copy_rdma failed, rc %ld\n",
1356 			rc);
1357 		break;
1358 	case H_DEST_PARM:
1359 	case H_SOURCE_PARM:
1360 	default:
1361 		dev_err(&vscsi->dev, "copy_crq: h_copy_rdma failed, rc %ld\n",
1362 			rc);
1363 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
1364 		break;
1365 	}
1366 
1367 	return rc;
1368 }
1369 
1370 /**
1371  * ibmvscsis_adapter_info - Service an Adapter Info MAnagement Data gram
1372  * @vscsi:	Pointer to our adapter structure
1373  * @iue:	Information Unit containing the Adapter Info MAD request
1374  *
1375  * EXECUTION ENVIRONMENT:
1376  *	Interrupt adapter lock is held
1377  */
1378 static long ibmvscsis_adapter_info(struct scsi_info *vscsi,
1379 				   struct iu_entry *iue)
1380 {
1381 	struct viosrp_adapter_info *mad = &vio_iu(iue)->mad.adapter_info;
1382 	struct mad_adapter_info_data *info;
1383 	uint flag_bits = 0;
1384 	dma_addr_t token;
1385 	long rc;
1386 
1387 	mad->common.status = cpu_to_be16(VIOSRP_MAD_SUCCESS);
1388 
1389 	if (be16_to_cpu(mad->common.length) > sizeof(*info)) {
1390 		mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
1391 		return 0;
1392 	}
1393 
1394 	info = dma_alloc_coherent(&vscsi->dma_dev->dev, sizeof(*info), &token,
1395 				  GFP_ATOMIC);
1396 	if (!info) {
1397 		dev_err(&vscsi->dev, "bad dma_alloc_coherent %p\n",
1398 			iue->target);
1399 		mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
1400 		return 0;
1401 	}
1402 
1403 	/* Get remote info */
1404 	rc = h_copy_rdma(be16_to_cpu(mad->common.length),
1405 			 vscsi->dds.window[REMOTE].liobn,
1406 			 be64_to_cpu(mad->buffer),
1407 			 vscsi->dds.window[LOCAL].liobn, token);
1408 
1409 	if (rc != H_SUCCESS) {
1410 		if (rc == H_PERMISSION) {
1411 			if (connection_broken(vscsi))
1412 				flag_bits = (RESPONSE_Q_DOWN | CLIENT_FAILED);
1413 		}
1414 		pr_warn("adapter_info: h_copy_rdma from client failed, rc %ld\n",
1415 			rc);
1416 		pr_debug("adapter_info: ioba 0x%llx, flags 0x%x, flag_bits 0x%x\n",
1417 			 be64_to_cpu(mad->buffer), vscsi->flags, flag_bits);
1418 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
1419 					  flag_bits);
1420 		goto free_dma;
1421 	}
1422 
1423 	/*
1424 	 * Copy client info, but ignore partition number, which we
1425 	 * already got from phyp - unless we failed to get it from
1426 	 * phyp (e.g. if we're running on a p5 system).
1427 	 */
1428 	if (vscsi->client_data.partition_number == 0)
1429 		vscsi->client_data.partition_number =
1430 			be32_to_cpu(info->partition_number);
1431 	strncpy(vscsi->client_data.srp_version, info->srp_version,
1432 		sizeof(vscsi->client_data.srp_version));
1433 	strncpy(vscsi->client_data.partition_name, info->partition_name,
1434 		sizeof(vscsi->client_data.partition_name));
1435 	vscsi->client_data.mad_version = be32_to_cpu(info->mad_version);
1436 	vscsi->client_data.os_type = be32_to_cpu(info->os_type);
1437 
1438 	/* Copy our info */
1439 	strncpy(info->srp_version, SRP_VERSION,
1440 		sizeof(info->srp_version));
1441 	strncpy(info->partition_name, vscsi->dds.partition_name,
1442 		sizeof(info->partition_name));
1443 	info->partition_number = cpu_to_be32(vscsi->dds.partition_num);
1444 	info->mad_version = cpu_to_be32(MAD_VERSION_1);
1445 	info->os_type = cpu_to_be32(LINUX);
1446 	memset(&info->port_max_txu[0], 0, sizeof(info->port_max_txu));
1447 	info->port_max_txu[0] = cpu_to_be32(MAX_TXU);
1448 
1449 	dma_wmb();
1450 	rc = h_copy_rdma(sizeof(*info), vscsi->dds.window[LOCAL].liobn,
1451 			 token, vscsi->dds.window[REMOTE].liobn,
1452 			 be64_to_cpu(mad->buffer));
1453 	switch (rc) {
1454 	case H_SUCCESS:
1455 		break;
1456 
1457 	case H_SOURCE_PARM:
1458 	case H_DEST_PARM:
1459 	case H_PERMISSION:
1460 		if (connection_broken(vscsi))
1461 			flag_bits = (RESPONSE_Q_DOWN | CLIENT_FAILED);
1462 	default:
1463 		dev_err(&vscsi->dev, "adapter_info: h_copy_rdma to client failed, rc %ld\n",
1464 			rc);
1465 		ibmvscsis_post_disconnect(vscsi,
1466 					  ERR_DISCONNECT_RECONNECT,
1467 					  flag_bits);
1468 		break;
1469 	}
1470 
1471 free_dma:
1472 	dma_free_coherent(&vscsi->dma_dev->dev, sizeof(*info), info, token);
1473 	pr_debug("Leaving adapter_info, rc %ld\n", rc);
1474 
1475 	return rc;
1476 }
1477 
1478 /**
1479  * ibmvscsis_cap_mad() - Service a Capabilities MAnagement Data gram
1480  * @vscsi:	Pointer to our adapter structure
1481  * @iue:	Information Unit containing the Capabilities MAD request
1482  *
1483  * NOTE: if you return an error from this routine you must be
1484  * disconnecting or you will cause a hang
1485  *
1486  * EXECUTION ENVIRONMENT:
1487  *	Interrupt called with adapter lock held
1488  */
1489 static int ibmvscsis_cap_mad(struct scsi_info *vscsi, struct iu_entry *iue)
1490 {
1491 	struct viosrp_capabilities *mad = &vio_iu(iue)->mad.capabilities;
1492 	struct capabilities *cap;
1493 	struct mad_capability_common *common;
1494 	dma_addr_t token;
1495 	u16 olen, len, status, min_len, cap_len;
1496 	u32 flag;
1497 	uint flag_bits = 0;
1498 	long rc = 0;
1499 
1500 	olen = be16_to_cpu(mad->common.length);
1501 	/*
1502 	 * struct capabilities hardcodes a couple capabilities after the
1503 	 * header, but the capabilities can actually be in any order.
1504 	 */
1505 	min_len = offsetof(struct capabilities, migration);
1506 	if ((olen < min_len) || (olen > PAGE_SIZE)) {
1507 		pr_warn("cap_mad: invalid len %d\n", olen);
1508 		mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
1509 		return 0;
1510 	}
1511 
1512 	cap = dma_alloc_coherent(&vscsi->dma_dev->dev, olen, &token,
1513 				 GFP_ATOMIC);
1514 	if (!cap) {
1515 		dev_err(&vscsi->dev, "bad dma_alloc_coherent %p\n",
1516 			iue->target);
1517 		mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
1518 		return 0;
1519 	}
1520 	rc = h_copy_rdma(olen, vscsi->dds.window[REMOTE].liobn,
1521 			 be64_to_cpu(mad->buffer),
1522 			 vscsi->dds.window[LOCAL].liobn, token);
1523 	if (rc == H_SUCCESS) {
1524 		strncpy(cap->name, dev_name(&vscsi->dma_dev->dev),
1525 			SRP_MAX_LOC_LEN);
1526 
1527 		len = olen - min_len;
1528 		status = VIOSRP_MAD_SUCCESS;
1529 		common = (struct mad_capability_common *)&cap->migration;
1530 
1531 		while ((len > 0) && (status == VIOSRP_MAD_SUCCESS) && !rc) {
1532 			pr_debug("cap_mad: len left %hd, cap type %d, cap len %hd\n",
1533 				 len, be32_to_cpu(common->cap_type),
1534 				 be16_to_cpu(common->length));
1535 
1536 			cap_len = be16_to_cpu(common->length);
1537 			if (cap_len > len) {
1538 				dev_err(&vscsi->dev, "cap_mad: cap len mismatch with total len\n");
1539 				status = VIOSRP_MAD_FAILED;
1540 				break;
1541 			}
1542 
1543 			if (cap_len == 0) {
1544 				dev_err(&vscsi->dev, "cap_mad: cap len is 0\n");
1545 				status = VIOSRP_MAD_FAILED;
1546 				break;
1547 			}
1548 
1549 			switch (common->cap_type) {
1550 			default:
1551 				pr_debug("cap_mad: unsupported capability\n");
1552 				common->server_support = 0;
1553 				flag = cpu_to_be32((u32)CAP_LIST_SUPPORTED);
1554 				cap->flags &= ~flag;
1555 				break;
1556 			}
1557 
1558 			len = len - cap_len;
1559 			common = (struct mad_capability_common *)
1560 				((char *)common + cap_len);
1561 		}
1562 
1563 		mad->common.status = cpu_to_be16(status);
1564 
1565 		dma_wmb();
1566 		rc = h_copy_rdma(olen, vscsi->dds.window[LOCAL].liobn, token,
1567 				 vscsi->dds.window[REMOTE].liobn,
1568 				 be64_to_cpu(mad->buffer));
1569 
1570 		if (rc != H_SUCCESS) {
1571 			pr_debug("cap_mad: failed to copy to client, rc %ld\n",
1572 				 rc);
1573 
1574 			if (rc == H_PERMISSION) {
1575 				if (connection_broken(vscsi))
1576 					flag_bits = (RESPONSE_Q_DOWN |
1577 						     CLIENT_FAILED);
1578 			}
1579 
1580 			pr_warn("cap_mad: error copying data to client, rc %ld\n",
1581 				rc);
1582 			ibmvscsis_post_disconnect(vscsi,
1583 						  ERR_DISCONNECT_RECONNECT,
1584 						  flag_bits);
1585 		}
1586 	}
1587 
1588 	dma_free_coherent(&vscsi->dma_dev->dev, olen, cap, token);
1589 
1590 	pr_debug("Leaving cap_mad, rc %ld, client_cap 0x%x\n",
1591 		 rc, vscsi->client_cap);
1592 
1593 	return rc;
1594 }
1595 
1596 /**
1597  * ibmvscsis_process_mad() - Service a MAnagement Data gram
1598  * @vscsi:	Pointer to our adapter structure
1599  * @iue:	Information Unit containing the MAD request
1600  *
1601  * Must be called with interrupt lock held.
1602  */
1603 static long ibmvscsis_process_mad(struct scsi_info *vscsi, struct iu_entry *iue)
1604 {
1605 	struct mad_common *mad = (struct mad_common *)&vio_iu(iue)->mad;
1606 	struct viosrp_empty_iu *empty;
1607 	long rc = ADAPT_SUCCESS;
1608 
1609 	switch (be32_to_cpu(mad->type)) {
1610 	case VIOSRP_EMPTY_IU_TYPE:
1611 		empty = &vio_iu(iue)->mad.empty_iu;
1612 		vscsi->empty_iu_id = be64_to_cpu(empty->buffer);
1613 		vscsi->empty_iu_tag = be64_to_cpu(empty->common.tag);
1614 		mad->status = cpu_to_be16(VIOSRP_MAD_SUCCESS);
1615 		break;
1616 	case VIOSRP_ADAPTER_INFO_TYPE:
1617 		rc = ibmvscsis_adapter_info(vscsi, iue);
1618 		break;
1619 	case VIOSRP_CAPABILITIES_TYPE:
1620 		rc = ibmvscsis_cap_mad(vscsi, iue);
1621 		break;
1622 	case VIOSRP_ENABLE_FAST_FAIL:
1623 		if (vscsi->state == CONNECTED) {
1624 			vscsi->fast_fail = true;
1625 			mad->status = cpu_to_be16(VIOSRP_MAD_SUCCESS);
1626 		} else {
1627 			pr_warn("fast fail mad sent after login\n");
1628 			mad->status = cpu_to_be16(VIOSRP_MAD_FAILED);
1629 		}
1630 		break;
1631 	default:
1632 		mad->status = cpu_to_be16(VIOSRP_MAD_NOT_SUPPORTED);
1633 		break;
1634 	}
1635 
1636 	return rc;
1637 }
1638 
1639 /**
1640  * srp_snd_msg_failed() - Handle an error when sending a response
1641  * @vscsi:	Pointer to our adapter structure
1642  * @rc:		The return code from the h_send_crq command
1643  *
1644  * Must be called with interrupt lock held.
1645  */
1646 static void srp_snd_msg_failed(struct scsi_info *vscsi, long rc)
1647 {
1648 	ktime_t kt;
1649 
1650 	if (rc != H_DROPPED) {
1651 		ibmvscsis_free_cmd_qs(vscsi);
1652 
1653 		if (rc == H_CLOSED)
1654 			vscsi->flags |= CLIENT_FAILED;
1655 
1656 		/* don't flag the same problem multiple times */
1657 		if (!(vscsi->flags & RESPONSE_Q_DOWN)) {
1658 			vscsi->flags |= RESPONSE_Q_DOWN;
1659 			if (!(vscsi->state & (ERR_DISCONNECT |
1660 					      ERR_DISCONNECT_RECONNECT |
1661 					      ERR_DISCONNECTED | UNDEFINED))) {
1662 				dev_err(&vscsi->dev, "snd_msg_failed: setting RESPONSE_Q_DOWN, state 0x%hx, flags 0x%x, rc %ld\n",
1663 					vscsi->state, vscsi->flags, rc);
1664 			}
1665 			ibmvscsis_post_disconnect(vscsi,
1666 						  ERR_DISCONNECT_RECONNECT, 0);
1667 		}
1668 		return;
1669 	}
1670 
1671 	/*
1672 	 * The response queue is full.
1673 	 * If the server is processing SRP requests, i.e.
1674 	 * the client has successfully done an
1675 	 * SRP_LOGIN, then it will wait forever for room in
1676 	 * the queue.  However if the system admin
1677 	 * is attempting to unconfigure the server then one
1678 	 * or more children will be in a state where
1679 	 * they are being removed. So if there is even one
1680 	 * child being removed then the driver assumes
1681 	 * the system admin is attempting to break the
1682 	 * connection with the client and MAX_TIMER_POPS
1683 	 * is honored.
1684 	 */
1685 	if ((vscsi->rsp_q_timer.timer_pops < MAX_TIMER_POPS) ||
1686 	    (vscsi->state == SRP_PROCESSING)) {
1687 		pr_debug("snd_msg_failed: response queue full, flags 0x%x, timer started %d, pops %d\n",
1688 			 vscsi->flags, (int)vscsi->rsp_q_timer.started,
1689 			 vscsi->rsp_q_timer.timer_pops);
1690 
1691 		/*
1692 		 * Check if the timer is running; if it
1693 		 * is not then start it up.
1694 		 */
1695 		if (!vscsi->rsp_q_timer.started) {
1696 			if (vscsi->rsp_q_timer.timer_pops <
1697 			    MAX_TIMER_POPS) {
1698 				kt = WAIT_NANO_SECONDS;
1699 			} else {
1700 				/*
1701 				 * slide the timeslice if the maximum
1702 				 * timer pops have already happened
1703 				 */
1704 				kt = ktime_set(WAIT_SECONDS, 0);
1705 			}
1706 
1707 			vscsi->rsp_q_timer.started = true;
1708 			hrtimer_start(&vscsi->rsp_q_timer.timer, kt,
1709 				      HRTIMER_MODE_REL);
1710 		}
1711 	} else {
1712 		/*
1713 		 * TBD: Do we need to worry about this? Need to get
1714 		 *      remove working.
1715 		 */
1716 		/*
1717 		 * waited a long time and it appears the system admin
1718 		 * is bring this driver down
1719 		 */
1720 		vscsi->flags |= RESPONSE_Q_DOWN;
1721 		ibmvscsis_free_cmd_qs(vscsi);
1722 		/*
1723 		 * if the driver is already attempting to disconnect
1724 		 * from the client and has already logged an error
1725 		 * trace this event but don't put it in the error log
1726 		 */
1727 		if (!(vscsi->state & (ERR_DISCONNECT |
1728 				      ERR_DISCONNECT_RECONNECT |
1729 				      ERR_DISCONNECTED | UNDEFINED))) {
1730 			dev_err(&vscsi->dev, "client crq full too long\n");
1731 			ibmvscsis_post_disconnect(vscsi,
1732 						  ERR_DISCONNECT_RECONNECT,
1733 						  0);
1734 		}
1735 	}
1736 }
1737 
1738 /**
1739  * ibmvscsis_send_messages() - Send a Response
1740  * @vscsi:	Pointer to our adapter structure
1741  *
1742  * Send a response, first checking the waiting queue. Responses are
1743  * sent in order they are received. If the response cannot be sent,
1744  * because the client queue is full, it stays on the waiting queue.
1745  *
1746  * PRECONDITION:
1747  *	Called with interrupt lock held
1748  */
1749 static void ibmvscsis_send_messages(struct scsi_info *vscsi)
1750 {
1751 	u64 msg_hi = 0;
1752 	/* note do not attmempt to access the IU_data_ptr with this pointer
1753 	 * it is not valid
1754 	 */
1755 	struct viosrp_crq *crq = (struct viosrp_crq *)&msg_hi;
1756 	struct ibmvscsis_cmd *cmd, *nxt;
1757 	struct iu_entry *iue;
1758 	long rc = ADAPT_SUCCESS;
1759 
1760 	if (!(vscsi->flags & RESPONSE_Q_DOWN)) {
1761 		list_for_each_entry_safe(cmd, nxt, &vscsi->waiting_rsp, list) {
1762 			iue = cmd->iue;
1763 
1764 			crq->valid = VALID_CMD_RESP_EL;
1765 			crq->format = cmd->rsp.format;
1766 
1767 			if (cmd->flags & CMD_FAST_FAIL)
1768 				crq->status = VIOSRP_ADAPTER_FAIL;
1769 
1770 			crq->IU_length = cpu_to_be16(cmd->rsp.len);
1771 
1772 			rc = h_send_crq(vscsi->dma_dev->unit_address,
1773 					be64_to_cpu(msg_hi),
1774 					be64_to_cpu(cmd->rsp.tag));
1775 
1776 			pr_debug("send_messages: cmd %p, tag 0x%llx, rc %ld\n",
1777 				 cmd, be64_to_cpu(cmd->rsp.tag), rc);
1778 
1779 			/* if all ok free up the command element resources */
1780 			if (rc == H_SUCCESS) {
1781 				/* some movement has occurred */
1782 				vscsi->rsp_q_timer.timer_pops = 0;
1783 				list_del(&cmd->list);
1784 
1785 				ibmvscsis_free_cmd_resources(vscsi, cmd);
1786 			} else {
1787 				srp_snd_msg_failed(vscsi, rc);
1788 				break;
1789 			}
1790 		}
1791 
1792 		if (!rc) {
1793 			/*
1794 			 * The timer could pop with the queue empty.  If
1795 			 * this happens, rc will always indicate a
1796 			 * success; clear the pop count.
1797 			 */
1798 			vscsi->rsp_q_timer.timer_pops = 0;
1799 		}
1800 	} else {
1801 		ibmvscsis_free_cmd_qs(vscsi);
1802 	}
1803 }
1804 
1805 /* Called with intr lock held */
1806 static void ibmvscsis_send_mad_resp(struct scsi_info *vscsi,
1807 				    struct ibmvscsis_cmd *cmd,
1808 				    struct viosrp_crq *crq)
1809 {
1810 	struct iu_entry *iue = cmd->iue;
1811 	struct mad_common *mad = (struct mad_common *)&vio_iu(iue)->mad;
1812 	uint flag_bits = 0;
1813 	long rc;
1814 
1815 	dma_wmb();
1816 	rc = h_copy_rdma(sizeof(struct mad_common),
1817 			 vscsi->dds.window[LOCAL].liobn, iue->sbuf->dma,
1818 			 vscsi->dds.window[REMOTE].liobn,
1819 			 be64_to_cpu(crq->IU_data_ptr));
1820 	if (!rc) {
1821 		cmd->rsp.format = VIOSRP_MAD_FORMAT;
1822 		cmd->rsp.len = sizeof(struct mad_common);
1823 		cmd->rsp.tag = mad->tag;
1824 		list_add_tail(&cmd->list, &vscsi->waiting_rsp);
1825 		ibmvscsis_send_messages(vscsi);
1826 	} else {
1827 		pr_debug("Error sending mad response, rc %ld\n", rc);
1828 		if (rc == H_PERMISSION) {
1829 			if (connection_broken(vscsi))
1830 				flag_bits = (RESPONSE_Q_DOWN | CLIENT_FAILED);
1831 		}
1832 		dev_err(&vscsi->dev, "mad: failed to copy to client, rc %ld\n",
1833 			rc);
1834 
1835 		ibmvscsis_free_cmd_resources(vscsi, cmd);
1836 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
1837 					  flag_bits);
1838 	}
1839 }
1840 
1841 /**
1842  * ibmvscsis_mad() - Service a MAnagement Data gram.
1843  * @vscsi:	Pointer to our adapter structure
1844  * @crq:	Pointer to the CRQ entry containing the MAD request
1845  *
1846  * EXECUTION ENVIRONMENT:
1847  *	Interrupt, called with adapter lock held
1848  */
1849 static long ibmvscsis_mad(struct scsi_info *vscsi, struct viosrp_crq *crq)
1850 {
1851 	struct iu_entry *iue;
1852 	struct ibmvscsis_cmd *cmd;
1853 	struct mad_common *mad;
1854 	long rc = ADAPT_SUCCESS;
1855 
1856 	switch (vscsi->state) {
1857 		/*
1858 		 * We have not exchanged Init Msgs yet, so this MAD was sent
1859 		 * before the last Transport Event; client will not be
1860 		 * expecting a response.
1861 		 */
1862 	case WAIT_CONNECTION:
1863 		pr_debug("mad: in Wait Connection state, ignoring MAD, flags %d\n",
1864 			 vscsi->flags);
1865 		return ADAPT_SUCCESS;
1866 
1867 	case SRP_PROCESSING:
1868 	case CONNECTED:
1869 		break;
1870 
1871 		/*
1872 		 * We should never get here while we're in these states.
1873 		 * Just log an error and get out.
1874 		 */
1875 	case UNCONFIGURING:
1876 	case WAIT_IDLE:
1877 	case ERR_DISCONNECT:
1878 	case ERR_DISCONNECT_RECONNECT:
1879 	default:
1880 		dev_err(&vscsi->dev, "mad: invalid adapter state %d for mad\n",
1881 			vscsi->state);
1882 		return ADAPT_SUCCESS;
1883 	}
1884 
1885 	cmd = ibmvscsis_get_free_cmd(vscsi);
1886 	if (!cmd) {
1887 		dev_err(&vscsi->dev, "mad: failed to get cmd, debit %d\n",
1888 			vscsi->debit);
1889 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
1890 		return ERROR;
1891 	}
1892 	iue = cmd->iue;
1893 	cmd->type = ADAPTER_MAD;
1894 
1895 	rc = ibmvscsis_copy_crq_packet(vscsi, cmd, crq);
1896 	if (!rc) {
1897 		mad = (struct mad_common *)&vio_iu(iue)->mad;
1898 
1899 		pr_debug("mad: type %d\n", be32_to_cpu(mad->type));
1900 
1901 		rc = ibmvscsis_process_mad(vscsi, iue);
1902 
1903 		pr_debug("mad: status %hd, rc %ld\n", be16_to_cpu(mad->status),
1904 			 rc);
1905 
1906 		if (!rc)
1907 			ibmvscsis_send_mad_resp(vscsi, cmd, crq);
1908 	} else {
1909 		ibmvscsis_free_cmd_resources(vscsi, cmd);
1910 	}
1911 
1912 	pr_debug("Leaving mad, rc %ld\n", rc);
1913 	return rc;
1914 }
1915 
1916 /**
1917  * ibmvscsis_login_rsp() - Create/copy a login response notice to the client
1918  * @vscsi:	Pointer to our adapter structure
1919  * @cmd:	Pointer to the command for the SRP Login request
1920  *
1921  * EXECUTION ENVIRONMENT:
1922  *	Interrupt, interrupt lock held
1923  */
1924 static long ibmvscsis_login_rsp(struct scsi_info *vscsi,
1925 				struct ibmvscsis_cmd *cmd)
1926 {
1927 	struct iu_entry *iue = cmd->iue;
1928 	struct srp_login_rsp *rsp = &vio_iu(iue)->srp.login_rsp;
1929 	struct format_code *fmt;
1930 	uint flag_bits = 0;
1931 	long rc = ADAPT_SUCCESS;
1932 
1933 	memset(rsp, 0, sizeof(struct srp_login_rsp));
1934 
1935 	rsp->opcode = SRP_LOGIN_RSP;
1936 	rsp->req_lim_delta = cpu_to_be32(vscsi->request_limit);
1937 	rsp->tag = cmd->rsp.tag;
1938 	rsp->max_it_iu_len = cpu_to_be32(SRP_MAX_IU_LEN);
1939 	rsp->max_ti_iu_len = cpu_to_be32(SRP_MAX_IU_LEN);
1940 	fmt = (struct format_code *)&rsp->buf_fmt;
1941 	fmt->buffers = SUPPORTED_FORMATS;
1942 	vscsi->credit = 0;
1943 
1944 	cmd->rsp.len = sizeof(struct srp_login_rsp);
1945 
1946 	dma_wmb();
1947 	rc = h_copy_rdma(cmd->rsp.len, vscsi->dds.window[LOCAL].liobn,
1948 			 iue->sbuf->dma, vscsi->dds.window[REMOTE].liobn,
1949 			 be64_to_cpu(iue->remote_token));
1950 
1951 	switch (rc) {
1952 	case H_SUCCESS:
1953 		break;
1954 
1955 	case H_PERMISSION:
1956 		if (connection_broken(vscsi))
1957 			flag_bits = RESPONSE_Q_DOWN | CLIENT_FAILED;
1958 		dev_err(&vscsi->dev, "login_rsp: error copying to client, rc %ld\n",
1959 			rc);
1960 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
1961 					  flag_bits);
1962 		break;
1963 	case H_SOURCE_PARM:
1964 	case H_DEST_PARM:
1965 	default:
1966 		dev_err(&vscsi->dev, "login_rsp: error copying to client, rc %ld\n",
1967 			rc);
1968 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
1969 		break;
1970 	}
1971 
1972 	return rc;
1973 }
1974 
1975 /**
1976  * ibmvscsis_srp_login_rej() - Create/copy a login rejection notice to client
1977  * @vscsi:	Pointer to our adapter structure
1978  * @cmd:	Pointer to the command for the SRP Login request
1979  * @reason:	The reason the SRP Login is being rejected, per SRP protocol
1980  *
1981  * EXECUTION ENVIRONMENT:
1982  *	Interrupt, interrupt lock held
1983  */
1984 static long ibmvscsis_srp_login_rej(struct scsi_info *vscsi,
1985 				    struct ibmvscsis_cmd *cmd, u32 reason)
1986 {
1987 	struct iu_entry *iue = cmd->iue;
1988 	struct srp_login_rej *rej = &vio_iu(iue)->srp.login_rej;
1989 	struct format_code *fmt;
1990 	uint flag_bits = 0;
1991 	long rc = ADAPT_SUCCESS;
1992 
1993 	memset(rej, 0, sizeof(*rej));
1994 
1995 	rej->opcode = SRP_LOGIN_REJ;
1996 	rej->reason = cpu_to_be32(reason);
1997 	rej->tag = cmd->rsp.tag;
1998 	fmt = (struct format_code *)&rej->buf_fmt;
1999 	fmt->buffers = SUPPORTED_FORMATS;
2000 
2001 	cmd->rsp.len = sizeof(*rej);
2002 
2003 	dma_wmb();
2004 	rc = h_copy_rdma(cmd->rsp.len, vscsi->dds.window[LOCAL].liobn,
2005 			 iue->sbuf->dma, vscsi->dds.window[REMOTE].liobn,
2006 			 be64_to_cpu(iue->remote_token));
2007 
2008 	switch (rc) {
2009 	case H_SUCCESS:
2010 		break;
2011 	case H_PERMISSION:
2012 		if (connection_broken(vscsi))
2013 			flag_bits = RESPONSE_Q_DOWN | CLIENT_FAILED;
2014 		dev_err(&vscsi->dev, "login_rej: error copying to client, rc %ld\n",
2015 			rc);
2016 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
2017 					  flag_bits);
2018 		break;
2019 	case H_SOURCE_PARM:
2020 	case H_DEST_PARM:
2021 	default:
2022 		dev_err(&vscsi->dev, "login_rej: error copying to client, rc %ld\n",
2023 			rc);
2024 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2025 		break;
2026 	}
2027 
2028 	return rc;
2029 }
2030 
2031 static int ibmvscsis_make_nexus(struct ibmvscsis_tport *tport)
2032 {
2033 	char *name = tport->tport_name;
2034 	struct ibmvscsis_nexus *nexus;
2035 	int rc;
2036 
2037 	if (tport->ibmv_nexus) {
2038 		pr_debug("tport->ibmv_nexus already exists\n");
2039 		return 0;
2040 	}
2041 
2042 	nexus = kzalloc(sizeof(*nexus), GFP_KERNEL);
2043 	if (!nexus) {
2044 		pr_err("Unable to allocate struct ibmvscsis_nexus\n");
2045 		return -ENOMEM;
2046 	}
2047 
2048 	nexus->se_sess = target_alloc_session(&tport->se_tpg, 0, 0,
2049 					      TARGET_PROT_NORMAL, name, nexus,
2050 					      NULL);
2051 	if (IS_ERR(nexus->se_sess)) {
2052 		rc = PTR_ERR(nexus->se_sess);
2053 		goto transport_init_fail;
2054 	}
2055 
2056 	tport->ibmv_nexus = nexus;
2057 
2058 	return 0;
2059 
2060 transport_init_fail:
2061 	kfree(nexus);
2062 	return rc;
2063 }
2064 
2065 static int ibmvscsis_drop_nexus(struct ibmvscsis_tport *tport)
2066 {
2067 	struct se_session *se_sess;
2068 	struct ibmvscsis_nexus *nexus;
2069 
2070 	nexus = tport->ibmv_nexus;
2071 	if (!nexus)
2072 		return -ENODEV;
2073 
2074 	se_sess = nexus->se_sess;
2075 	if (!se_sess)
2076 		return -ENODEV;
2077 
2078 	/*
2079 	 * Release the SCSI I_T Nexus to the emulated ibmvscsis Target Port
2080 	 */
2081 	target_wait_for_sess_cmds(se_sess);
2082 	transport_deregister_session_configfs(se_sess);
2083 	transport_deregister_session(se_sess);
2084 	tport->ibmv_nexus = NULL;
2085 	kfree(nexus);
2086 
2087 	return 0;
2088 }
2089 
2090 /**
2091  * ibmvscsis_srp_login() - Process an SRP Login Request
2092  * @vscsi:	Pointer to our adapter structure
2093  * @cmd:	Command element to use to process the SRP Login request
2094  * @crq:	Pointer to CRQ entry containing the SRP Login request
2095  *
2096  * EXECUTION ENVIRONMENT:
2097  *	Interrupt, called with interrupt lock held
2098  */
2099 static long ibmvscsis_srp_login(struct scsi_info *vscsi,
2100 				struct ibmvscsis_cmd *cmd,
2101 				struct viosrp_crq *crq)
2102 {
2103 	struct iu_entry *iue = cmd->iue;
2104 	struct srp_login_req *req = &vio_iu(iue)->srp.login_req;
2105 	struct port_id {
2106 		__be64 id_extension;
2107 		__be64 io_guid;
2108 	} *iport, *tport;
2109 	struct format_code *fmt;
2110 	u32 reason = 0x0;
2111 	long rc = ADAPT_SUCCESS;
2112 
2113 	iport = (struct port_id *)req->initiator_port_id;
2114 	tport = (struct port_id *)req->target_port_id;
2115 	fmt = (struct format_code *)&req->req_buf_fmt;
2116 	if (be32_to_cpu(req->req_it_iu_len) > SRP_MAX_IU_LEN)
2117 		reason = SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE;
2118 	else if (be32_to_cpu(req->req_it_iu_len) < 64)
2119 		reason = SRP_LOGIN_REJ_UNABLE_ESTABLISH_CHANNEL;
2120 	else if ((be64_to_cpu(iport->id_extension) > (MAX_NUM_PORTS - 1)) ||
2121 		 (be64_to_cpu(tport->id_extension) > (MAX_NUM_PORTS - 1)))
2122 		reason = SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL;
2123 	else if (req->req_flags & SRP_MULTICHAN_MULTI)
2124 		reason = SRP_LOGIN_REJ_MULTI_CHANNEL_UNSUPPORTED;
2125 	else if (fmt->buffers & (~SUPPORTED_FORMATS))
2126 		reason = SRP_LOGIN_REJ_UNSUPPORTED_DESCRIPTOR_FMT;
2127 	else if ((fmt->buffers & SUPPORTED_FORMATS) == 0)
2128 		reason = SRP_LOGIN_REJ_UNSUPPORTED_DESCRIPTOR_FMT;
2129 
2130 	if (vscsi->state == SRP_PROCESSING)
2131 		reason = SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED;
2132 
2133 	rc = ibmvscsis_make_nexus(&vscsi->tport);
2134 	if (rc)
2135 		reason = SRP_LOGIN_REJ_UNABLE_ESTABLISH_CHANNEL;
2136 
2137 	cmd->rsp.format = VIOSRP_SRP_FORMAT;
2138 	cmd->rsp.tag = req->tag;
2139 
2140 	pr_debug("srp_login: reason 0x%x\n", reason);
2141 
2142 	if (reason)
2143 		rc = ibmvscsis_srp_login_rej(vscsi, cmd, reason);
2144 	else
2145 		rc = ibmvscsis_login_rsp(vscsi, cmd);
2146 
2147 	if (!rc) {
2148 		if (!reason)
2149 			vscsi->state = SRP_PROCESSING;
2150 
2151 		list_add_tail(&cmd->list, &vscsi->waiting_rsp);
2152 		ibmvscsis_send_messages(vscsi);
2153 	} else {
2154 		ibmvscsis_free_cmd_resources(vscsi, cmd);
2155 	}
2156 
2157 	pr_debug("Leaving srp_login, rc %ld\n", rc);
2158 	return rc;
2159 }
2160 
2161 /**
2162  * ibmvscsis_srp_i_logout() - Helper Function to close I_T Nexus
2163  * @vscsi:	Pointer to our adapter structure
2164  * @cmd:	Command element to use to process the Implicit Logout request
2165  * @crq:	Pointer to CRQ entry containing the Implicit Logout request
2166  *
2167  * Do the logic to close the I_T nexus.  This function may not
2168  * behave to specification.
2169  *
2170  * EXECUTION ENVIRONMENT:
2171  *	Interrupt, interrupt lock held
2172  */
2173 static long ibmvscsis_srp_i_logout(struct scsi_info *vscsi,
2174 				   struct ibmvscsis_cmd *cmd,
2175 				   struct viosrp_crq *crq)
2176 {
2177 	struct iu_entry *iue = cmd->iue;
2178 	struct srp_i_logout *log_out = &vio_iu(iue)->srp.i_logout;
2179 	long rc = ADAPT_SUCCESS;
2180 
2181 	if ((vscsi->debit > 0) || !list_empty(&vscsi->schedule_q) ||
2182 	    !list_empty(&vscsi->waiting_rsp)) {
2183 		dev_err(&vscsi->dev, "i_logout: outstanding work\n");
2184 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
2185 	} else {
2186 		cmd->rsp.format = SRP_FORMAT;
2187 		cmd->rsp.tag = log_out->tag;
2188 		cmd->rsp.len = sizeof(struct mad_common);
2189 		list_add_tail(&cmd->list, &vscsi->waiting_rsp);
2190 		ibmvscsis_send_messages(vscsi);
2191 
2192 		ibmvscsis_post_disconnect(vscsi, WAIT_IDLE, 0);
2193 	}
2194 
2195 	return rc;
2196 }
2197 
2198 /* Called with intr lock held */
2199 static void ibmvscsis_srp_cmd(struct scsi_info *vscsi, struct viosrp_crq *crq)
2200 {
2201 	struct ibmvscsis_cmd *cmd;
2202 	struct iu_entry *iue;
2203 	struct srp_cmd *srp;
2204 	struct srp_tsk_mgmt *tsk;
2205 	long rc;
2206 
2207 	if (vscsi->request_limit - vscsi->debit <= 0) {
2208 		/* Client has exceeded request limit */
2209 		dev_err(&vscsi->dev, "Client exceeded the request limit (%d), debit %d\n",
2210 			vscsi->request_limit, vscsi->debit);
2211 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2212 		return;
2213 	}
2214 
2215 	cmd = ibmvscsis_get_free_cmd(vscsi);
2216 	if (!cmd) {
2217 		dev_err(&vscsi->dev, "srp_cmd failed to get cmd, debit %d\n",
2218 			vscsi->debit);
2219 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2220 		return;
2221 	}
2222 	iue = cmd->iue;
2223 	srp = &vio_iu(iue)->srp.cmd;
2224 
2225 	rc = ibmvscsis_copy_crq_packet(vscsi, cmd, crq);
2226 	if (rc) {
2227 		ibmvscsis_free_cmd_resources(vscsi, cmd);
2228 		return;
2229 	}
2230 
2231 	if (vscsi->state == SRP_PROCESSING) {
2232 		switch (srp->opcode) {
2233 		case SRP_LOGIN_REQ:
2234 			rc = ibmvscsis_srp_login(vscsi, cmd, crq);
2235 			break;
2236 
2237 		case SRP_TSK_MGMT:
2238 			tsk = &vio_iu(iue)->srp.tsk_mgmt;
2239 			pr_debug("tsk_mgmt tag: %llu (0x%llx)\n", tsk->tag,
2240 				 tsk->tag);
2241 			cmd->rsp.tag = tsk->tag;
2242 			vscsi->debit += 1;
2243 			cmd->type = TASK_MANAGEMENT;
2244 			list_add_tail(&cmd->list, &vscsi->schedule_q);
2245 			queue_work(vscsi->work_q, &cmd->work);
2246 			break;
2247 
2248 		case SRP_CMD:
2249 			pr_debug("srp_cmd tag: %llu (0x%llx)\n", srp->tag,
2250 				 srp->tag);
2251 			cmd->rsp.tag = srp->tag;
2252 			vscsi->debit += 1;
2253 			cmd->type = SCSI_CDB;
2254 			/*
2255 			 * We want to keep track of work waiting for
2256 			 * the workqueue.
2257 			 */
2258 			list_add_tail(&cmd->list, &vscsi->schedule_q);
2259 			queue_work(vscsi->work_q, &cmd->work);
2260 			break;
2261 
2262 		case SRP_I_LOGOUT:
2263 			rc = ibmvscsis_srp_i_logout(vscsi, cmd, crq);
2264 			break;
2265 
2266 		case SRP_CRED_RSP:
2267 		case SRP_AER_RSP:
2268 		default:
2269 			ibmvscsis_free_cmd_resources(vscsi, cmd);
2270 			dev_err(&vscsi->dev, "invalid srp cmd, opcode %d\n",
2271 				(uint)srp->opcode);
2272 			ibmvscsis_post_disconnect(vscsi,
2273 						  ERR_DISCONNECT_RECONNECT, 0);
2274 			break;
2275 		}
2276 	} else if (srp->opcode == SRP_LOGIN_REQ && vscsi->state == CONNECTED) {
2277 		rc = ibmvscsis_srp_login(vscsi, cmd, crq);
2278 	} else {
2279 		ibmvscsis_free_cmd_resources(vscsi, cmd);
2280 		dev_err(&vscsi->dev, "Invalid state %d to handle srp cmd\n",
2281 			vscsi->state);
2282 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2283 	}
2284 }
2285 
2286 /**
2287  * ibmvscsis_ping_response() - Respond to a ping request
2288  * @vscsi:	Pointer to our adapter structure
2289  *
2290  * Let the client know that the server is alive and waiting on
2291  * its native I/O stack.
2292  * If any type of error occurs from the call to queue a ping
2293  * response then the client is either not accepting or receiving
2294  * interrupts.  Disconnect with an error.
2295  *
2296  * EXECUTION ENVIRONMENT:
2297  *	Interrupt, interrupt lock held
2298  */
2299 static long ibmvscsis_ping_response(struct scsi_info *vscsi)
2300 {
2301 	struct viosrp_crq *crq;
2302 	u64 buffer[2] = { 0, 0 };
2303 	long rc;
2304 
2305 	crq = (struct viosrp_crq *)&buffer;
2306 	crq->valid = VALID_CMD_RESP_EL;
2307 	crq->format = (u8)MESSAGE_IN_CRQ;
2308 	crq->status = PING_RESPONSE;
2309 
2310 	rc = h_send_crq(vscsi->dds.unit_id, cpu_to_be64(buffer[MSG_HI]),
2311 			cpu_to_be64(buffer[MSG_LOW]));
2312 
2313 	switch (rc) {
2314 	case H_SUCCESS:
2315 		break;
2316 	case H_CLOSED:
2317 		vscsi->flags |= CLIENT_FAILED;
2318 	case H_DROPPED:
2319 		vscsi->flags |= RESPONSE_Q_DOWN;
2320 	case H_REMOTE_PARM:
2321 		dev_err(&vscsi->dev, "ping_response: h_send_crq failed, rc %ld\n",
2322 			rc);
2323 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2324 		break;
2325 	default:
2326 		dev_err(&vscsi->dev, "ping_response: h_send_crq returned unknown rc %ld\n",
2327 			rc);
2328 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
2329 		break;
2330 	}
2331 
2332 	return rc;
2333 }
2334 
2335 /**
2336  * ibmvscsis_parse_command() - Parse an element taken from the cmd rsp queue.
2337  * @vscsi:	Pointer to our adapter structure
2338  * @crq:	Pointer to CRQ element containing the SRP request
2339  *
2340  * This function will return success if the command queue element is valid
2341  * and the srp iu or MAD request it pointed to was also valid.  That does
2342  * not mean that an error was not returned to the client.
2343  *
2344  * EXECUTION ENVIRONMENT:
2345  *	Interrupt, intr lock held
2346  */
2347 static long ibmvscsis_parse_command(struct scsi_info *vscsi,
2348 				    struct viosrp_crq *crq)
2349 {
2350 	long rc = ADAPT_SUCCESS;
2351 
2352 	switch (crq->valid) {
2353 	case VALID_CMD_RESP_EL:
2354 		switch (crq->format) {
2355 		case OS400_FORMAT:
2356 		case AIX_FORMAT:
2357 		case LINUX_FORMAT:
2358 		case MAD_FORMAT:
2359 			if (vscsi->flags & PROCESSING_MAD) {
2360 				rc = ERROR;
2361 				dev_err(&vscsi->dev, "parse_command: already processing mad\n");
2362 				ibmvscsis_post_disconnect(vscsi,
2363 						       ERR_DISCONNECT_RECONNECT,
2364 						       0);
2365 			} else {
2366 				vscsi->flags |= PROCESSING_MAD;
2367 				rc = ibmvscsis_mad(vscsi, crq);
2368 			}
2369 			break;
2370 
2371 		case SRP_FORMAT:
2372 			ibmvscsis_srp_cmd(vscsi, crq);
2373 			break;
2374 
2375 		case MESSAGE_IN_CRQ:
2376 			if (crq->status == PING)
2377 				ibmvscsis_ping_response(vscsi);
2378 			break;
2379 
2380 		default:
2381 			dev_err(&vscsi->dev, "parse_command: invalid format %d\n",
2382 				(uint)crq->format);
2383 			ibmvscsis_post_disconnect(vscsi,
2384 						  ERR_DISCONNECT_RECONNECT, 0);
2385 			break;
2386 		}
2387 		break;
2388 
2389 	case VALID_TRANS_EVENT:
2390 		rc = ibmvscsis_trans_event(vscsi, crq);
2391 		break;
2392 
2393 	case VALID_INIT_MSG:
2394 		rc = ibmvscsis_init_msg(vscsi, crq);
2395 		break;
2396 
2397 	default:
2398 		dev_err(&vscsi->dev, "parse_command: invalid valid field %d\n",
2399 			(uint)crq->valid);
2400 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2401 		break;
2402 	}
2403 
2404 	/*
2405 	 * Return only what the interrupt handler cares
2406 	 * about. Most errors we keep right on trucking.
2407 	 */
2408 	rc = vscsi->flags & SCHEDULE_DISCONNECT;
2409 
2410 	return rc;
2411 }
2412 
2413 static int read_dma_window(struct scsi_info *vscsi)
2414 {
2415 	struct vio_dev *vdev = vscsi->dma_dev;
2416 	const __be32 *dma_window;
2417 	const __be32 *prop;
2418 
2419 	/* TODO Using of_parse_dma_window would be better, but it doesn't give
2420 	 * a way to read multiple windows without already knowing the size of
2421 	 * a window or the number of windows.
2422 	 */
2423 	dma_window = (const __be32 *)vio_get_attribute(vdev,
2424 						       "ibm,my-dma-window",
2425 						       NULL);
2426 	if (!dma_window) {
2427 		pr_err("Couldn't find ibm,my-dma-window property\n");
2428 		return -1;
2429 	}
2430 
2431 	vscsi->dds.window[LOCAL].liobn = be32_to_cpu(*dma_window);
2432 	dma_window++;
2433 
2434 	prop = (const __be32 *)vio_get_attribute(vdev, "ibm,#dma-address-cells",
2435 						 NULL);
2436 	if (!prop) {
2437 		pr_warn("Couldn't find ibm,#dma-address-cells property\n");
2438 		dma_window++;
2439 	} else {
2440 		dma_window += be32_to_cpu(*prop);
2441 	}
2442 
2443 	prop = (const __be32 *)vio_get_attribute(vdev, "ibm,#dma-size-cells",
2444 						 NULL);
2445 	if (!prop) {
2446 		pr_warn("Couldn't find ibm,#dma-size-cells property\n");
2447 		dma_window++;
2448 	} else {
2449 		dma_window += be32_to_cpu(*prop);
2450 	}
2451 
2452 	/* dma_window should point to the second window now */
2453 	vscsi->dds.window[REMOTE].liobn = be32_to_cpu(*dma_window);
2454 
2455 	return 0;
2456 }
2457 
2458 static struct ibmvscsis_tport *ibmvscsis_lookup_port(const char *name)
2459 {
2460 	struct ibmvscsis_tport *tport = NULL;
2461 	struct vio_dev *vdev;
2462 	struct scsi_info *vscsi;
2463 
2464 	spin_lock_bh(&ibmvscsis_dev_lock);
2465 	list_for_each_entry(vscsi, &ibmvscsis_dev_list, list) {
2466 		vdev = vscsi->dma_dev;
2467 		if (!strcmp(dev_name(&vdev->dev), name)) {
2468 			tport = &vscsi->tport;
2469 			break;
2470 		}
2471 	}
2472 	spin_unlock_bh(&ibmvscsis_dev_lock);
2473 
2474 	return tport;
2475 }
2476 
2477 /**
2478  * ibmvscsis_parse_cmd() - Parse SRP Command
2479  * @vscsi:	Pointer to our adapter structure
2480  * @cmd:	Pointer to command element with SRP command
2481  *
2482  * Parse the srp command; if it is valid then submit it to tcm.
2483  * Note: The return code does not reflect the status of the SCSI CDB.
2484  *
2485  * EXECUTION ENVIRONMENT:
2486  *	Process level
2487  */
2488 static void ibmvscsis_parse_cmd(struct scsi_info *vscsi,
2489 				struct ibmvscsis_cmd *cmd)
2490 {
2491 	struct iu_entry *iue = cmd->iue;
2492 	struct srp_cmd *srp = (struct srp_cmd *)iue->sbuf->buf;
2493 	struct ibmvscsis_nexus *nexus;
2494 	u64 data_len = 0;
2495 	enum dma_data_direction dir;
2496 	int attr = 0;
2497 	int rc = 0;
2498 
2499 	nexus = vscsi->tport.ibmv_nexus;
2500 	/*
2501 	 * additional length in bytes.  Note that the SRP spec says that
2502 	 * additional length is in 4-byte words, but technically the
2503 	 * additional length field is only the upper 6 bits of the byte.
2504 	 * The lower 2 bits are reserved.  If the lower 2 bits are 0 (as
2505 	 * all reserved fields should be), then interpreting the byte as
2506 	 * an int will yield the length in bytes.
2507 	 */
2508 	if (srp->add_cdb_len & 0x03) {
2509 		dev_err(&vscsi->dev, "parse_cmd: reserved bits set in IU\n");
2510 		spin_lock_bh(&vscsi->intr_lock);
2511 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2512 		ibmvscsis_free_cmd_resources(vscsi, cmd);
2513 		spin_unlock_bh(&vscsi->intr_lock);
2514 		return;
2515 	}
2516 
2517 	if (srp_get_desc_table(srp, &dir, &data_len)) {
2518 		dev_err(&vscsi->dev, "0x%llx: parsing SRP descriptor table failed.\n",
2519 			srp->tag);
2520 		goto fail;
2521 	}
2522 
2523 	cmd->rsp.sol_not = srp->sol_not;
2524 
2525 	switch (srp->task_attr) {
2526 	case SRP_SIMPLE_TASK:
2527 		attr = TCM_SIMPLE_TAG;
2528 		break;
2529 	case SRP_ORDERED_TASK:
2530 		attr = TCM_ORDERED_TAG;
2531 		break;
2532 	case SRP_HEAD_TASK:
2533 		attr = TCM_HEAD_TAG;
2534 		break;
2535 	case SRP_ACA_TASK:
2536 		attr = TCM_ACA_TAG;
2537 		break;
2538 	default:
2539 		dev_err(&vscsi->dev, "Invalid task attribute %d\n",
2540 			srp->task_attr);
2541 		goto fail;
2542 	}
2543 
2544 	cmd->se_cmd.tag = be64_to_cpu(srp->tag);
2545 
2546 	spin_lock_bh(&vscsi->intr_lock);
2547 	list_add_tail(&cmd->list, &vscsi->active_q);
2548 	spin_unlock_bh(&vscsi->intr_lock);
2549 
2550 	srp->lun.scsi_lun[0] &= 0x3f;
2551 
2552 	rc = target_submit_cmd(&cmd->se_cmd, nexus->se_sess, srp->cdb,
2553 			       cmd->sense_buf, scsilun_to_int(&srp->lun),
2554 			       data_len, attr, dir, 0);
2555 	if (rc) {
2556 		dev_err(&vscsi->dev, "target_submit_cmd failed, rc %d\n", rc);
2557 		spin_lock_bh(&vscsi->intr_lock);
2558 		list_del(&cmd->list);
2559 		ibmvscsis_free_cmd_resources(vscsi, cmd);
2560 		spin_unlock_bh(&vscsi->intr_lock);
2561 		goto fail;
2562 	}
2563 	return;
2564 
2565 fail:
2566 	spin_lock_bh(&vscsi->intr_lock);
2567 	ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2568 	spin_unlock_bh(&vscsi->intr_lock);
2569 }
2570 
2571 /**
2572  * ibmvscsis_parse_task() - Parse SRP Task Management Request
2573  * @vscsi:	Pointer to our adapter structure
2574  * @cmd:	Pointer to command element with SRP task management request
2575  *
2576  * Parse the srp task management request; if it is valid then submit it to tcm.
2577  * Note: The return code does not reflect the status of the task management
2578  * request.
2579  *
2580  * EXECUTION ENVIRONMENT:
2581  *	Processor level
2582  */
2583 static void ibmvscsis_parse_task(struct scsi_info *vscsi,
2584 				 struct ibmvscsis_cmd *cmd)
2585 {
2586 	struct iu_entry *iue = cmd->iue;
2587 	struct srp_tsk_mgmt *srp_tsk = &vio_iu(iue)->srp.tsk_mgmt;
2588 	int tcm_type;
2589 	u64 tag_to_abort = 0;
2590 	int rc = 0;
2591 	struct ibmvscsis_nexus *nexus;
2592 
2593 	nexus = vscsi->tport.ibmv_nexus;
2594 
2595 	cmd->rsp.sol_not = srp_tsk->sol_not;
2596 
2597 	switch (srp_tsk->tsk_mgmt_func) {
2598 	case SRP_TSK_ABORT_TASK:
2599 		tcm_type = TMR_ABORT_TASK;
2600 		tag_to_abort = be64_to_cpu(srp_tsk->task_tag);
2601 		break;
2602 	case SRP_TSK_ABORT_TASK_SET:
2603 		tcm_type = TMR_ABORT_TASK_SET;
2604 		break;
2605 	case SRP_TSK_CLEAR_TASK_SET:
2606 		tcm_type = TMR_CLEAR_TASK_SET;
2607 		break;
2608 	case SRP_TSK_LUN_RESET:
2609 		tcm_type = TMR_LUN_RESET;
2610 		break;
2611 	case SRP_TSK_CLEAR_ACA:
2612 		tcm_type = TMR_CLEAR_ACA;
2613 		break;
2614 	default:
2615 		dev_err(&vscsi->dev, "unknown task mgmt func %d\n",
2616 			srp_tsk->tsk_mgmt_func);
2617 		cmd->se_cmd.se_tmr_req->response =
2618 			TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
2619 		rc = -1;
2620 		break;
2621 	}
2622 
2623 	if (!rc) {
2624 		cmd->se_cmd.tag = be64_to_cpu(srp_tsk->tag);
2625 
2626 		spin_lock_bh(&vscsi->intr_lock);
2627 		list_add_tail(&cmd->list, &vscsi->active_q);
2628 		spin_unlock_bh(&vscsi->intr_lock);
2629 
2630 		srp_tsk->lun.scsi_lun[0] &= 0x3f;
2631 
2632 		pr_debug("calling submit_tmr, func %d\n",
2633 			 srp_tsk->tsk_mgmt_func);
2634 		rc = target_submit_tmr(&cmd->se_cmd, nexus->se_sess, NULL,
2635 				       scsilun_to_int(&srp_tsk->lun), srp_tsk,
2636 				       tcm_type, GFP_KERNEL, tag_to_abort, 0);
2637 		if (rc) {
2638 			dev_err(&vscsi->dev, "target_submit_tmr failed, rc %d\n",
2639 				rc);
2640 			spin_lock_bh(&vscsi->intr_lock);
2641 			list_del(&cmd->list);
2642 			spin_unlock_bh(&vscsi->intr_lock);
2643 			cmd->se_cmd.se_tmr_req->response =
2644 				TMR_FUNCTION_REJECTED;
2645 		}
2646 	}
2647 
2648 	if (rc)
2649 		transport_send_check_condition_and_sense(&cmd->se_cmd, 0, 0);
2650 }
2651 
2652 static void ibmvscsis_scheduler(struct work_struct *work)
2653 {
2654 	struct ibmvscsis_cmd *cmd = container_of(work, struct ibmvscsis_cmd,
2655 						 work);
2656 	struct scsi_info *vscsi = cmd->adapter;
2657 
2658 	spin_lock_bh(&vscsi->intr_lock);
2659 
2660 	/* Remove from schedule_q */
2661 	list_del(&cmd->list);
2662 
2663 	/* Don't submit cmd if we're disconnecting */
2664 	if (vscsi->flags & (SCHEDULE_DISCONNECT | DISCONNECT_SCHEDULED)) {
2665 		ibmvscsis_free_cmd_resources(vscsi, cmd);
2666 
2667 		/* ibmvscsis_disconnect might be waiting for us */
2668 		if (list_empty(&vscsi->active_q) &&
2669 		    list_empty(&vscsi->schedule_q) &&
2670 		    (vscsi->flags & WAIT_FOR_IDLE)) {
2671 			vscsi->flags &= ~WAIT_FOR_IDLE;
2672 			complete(&vscsi->wait_idle);
2673 		}
2674 
2675 		spin_unlock_bh(&vscsi->intr_lock);
2676 		return;
2677 	}
2678 
2679 	spin_unlock_bh(&vscsi->intr_lock);
2680 
2681 	switch (cmd->type) {
2682 	case SCSI_CDB:
2683 		ibmvscsis_parse_cmd(vscsi, cmd);
2684 		break;
2685 	case TASK_MANAGEMENT:
2686 		ibmvscsis_parse_task(vscsi, cmd);
2687 		break;
2688 	default:
2689 		dev_err(&vscsi->dev, "scheduler, invalid cmd type %d\n",
2690 			cmd->type);
2691 		spin_lock_bh(&vscsi->intr_lock);
2692 		ibmvscsis_free_cmd_resources(vscsi, cmd);
2693 		spin_unlock_bh(&vscsi->intr_lock);
2694 		break;
2695 	}
2696 }
2697 
2698 static int ibmvscsis_alloc_cmds(struct scsi_info *vscsi, int num)
2699 {
2700 	struct ibmvscsis_cmd *cmd;
2701 	int i;
2702 
2703 	INIT_LIST_HEAD(&vscsi->free_cmd);
2704 	vscsi->cmd_pool = kcalloc(num, sizeof(struct ibmvscsis_cmd),
2705 				  GFP_KERNEL);
2706 	if (!vscsi->cmd_pool)
2707 		return -ENOMEM;
2708 
2709 	for (i = 0, cmd = (struct ibmvscsis_cmd *)vscsi->cmd_pool; i < num;
2710 	     i++, cmd++) {
2711 		cmd->adapter = vscsi;
2712 		INIT_WORK(&cmd->work, ibmvscsis_scheduler);
2713 		list_add_tail(&cmd->list, &vscsi->free_cmd);
2714 	}
2715 
2716 	return 0;
2717 }
2718 
2719 static void ibmvscsis_free_cmds(struct scsi_info *vscsi)
2720 {
2721 	kfree(vscsi->cmd_pool);
2722 	vscsi->cmd_pool = NULL;
2723 	INIT_LIST_HEAD(&vscsi->free_cmd);
2724 }
2725 
2726 /**
2727  * ibmvscsis_service_wait_q() - Service Waiting Queue
2728  * @timer:	Pointer to timer which has expired
2729  *
2730  * This routine is called when the timer pops to service the waiting
2731  * queue. Elements on the queue have completed, their responses have been
2732  * copied to the client, but the client's response queue was full so
2733  * the queue message could not be sent. The routine grabs the proper locks
2734  * and calls send messages.
2735  *
2736  * EXECUTION ENVIRONMENT:
2737  *	called at interrupt level
2738  */
2739 static enum hrtimer_restart ibmvscsis_service_wait_q(struct hrtimer *timer)
2740 {
2741 	struct timer_cb *p_timer = container_of(timer, struct timer_cb, timer);
2742 	struct scsi_info *vscsi = container_of(p_timer, struct scsi_info,
2743 					       rsp_q_timer);
2744 
2745 	spin_lock_bh(&vscsi->intr_lock);
2746 	p_timer->timer_pops += 1;
2747 	p_timer->started = false;
2748 	ibmvscsis_send_messages(vscsi);
2749 	spin_unlock_bh(&vscsi->intr_lock);
2750 
2751 	return HRTIMER_NORESTART;
2752 }
2753 
2754 static long ibmvscsis_alloctimer(struct scsi_info *vscsi)
2755 {
2756 	struct timer_cb *p_timer;
2757 
2758 	p_timer = &vscsi->rsp_q_timer;
2759 	hrtimer_init(&p_timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2760 
2761 	p_timer->timer.function = ibmvscsis_service_wait_q;
2762 	p_timer->started = false;
2763 	p_timer->timer_pops = 0;
2764 
2765 	return ADAPT_SUCCESS;
2766 }
2767 
2768 static void ibmvscsis_freetimer(struct scsi_info *vscsi)
2769 {
2770 	struct timer_cb *p_timer;
2771 
2772 	p_timer = &vscsi->rsp_q_timer;
2773 
2774 	(void)hrtimer_cancel(&p_timer->timer);
2775 
2776 	p_timer->started = false;
2777 	p_timer->timer_pops = 0;
2778 }
2779 
2780 static irqreturn_t ibmvscsis_interrupt(int dummy, void *data)
2781 {
2782 	struct scsi_info *vscsi = data;
2783 
2784 	vio_disable_interrupts(vscsi->dma_dev);
2785 	tasklet_schedule(&vscsi->work_task);
2786 
2787 	return IRQ_HANDLED;
2788 }
2789 
2790 /**
2791  * ibmvscsis_enable_change_state() - Set new state based on enabled status
2792  * @vscsi:	Pointer to our adapter structure
2793  *
2794  * This function determines our new state now that we are enabled.  This
2795  * may involve sending an Init Complete message to the client.
2796  *
2797  * Must be called with interrupt lock held.
2798  */
2799 static long ibmvscsis_enable_change_state(struct scsi_info *vscsi)
2800 {
2801 	int bytes;
2802 	long rc = ADAPT_SUCCESS;
2803 
2804 	bytes = vscsi->cmd_q.size * PAGE_SIZE;
2805 	rc = h_reg_crq(vscsi->dds.unit_id, vscsi->cmd_q.crq_token, bytes);
2806 	if (rc == H_CLOSED || rc == H_SUCCESS) {
2807 		vscsi->state = WAIT_CONNECTION;
2808 		rc = ibmvscsis_establish_new_q(vscsi);
2809 	}
2810 
2811 	if (rc != ADAPT_SUCCESS) {
2812 		vscsi->state = ERR_DISCONNECTED;
2813 		vscsi->flags |= RESPONSE_Q_DOWN;
2814 	}
2815 
2816 	return rc;
2817 }
2818 
2819 /**
2820  * ibmvscsis_create_command_q() - Create Command Queue
2821  * @vscsi:	Pointer to our adapter structure
2822  * @num_cmds:	Currently unused.  In the future, may be used to determine
2823  *		the size of the CRQ.
2824  *
2825  * Allocates memory for command queue maps remote memory into an ioba
2826  * initializes the command response queue
2827  *
2828  * EXECUTION ENVIRONMENT:
2829  *	Process level only
2830  */
2831 static long ibmvscsis_create_command_q(struct scsi_info *vscsi, int num_cmds)
2832 {
2833 	int pages;
2834 	struct vio_dev *vdev = vscsi->dma_dev;
2835 
2836 	/* We might support multiple pages in the future, but just 1 for now */
2837 	pages = 1;
2838 
2839 	vscsi->cmd_q.size = pages;
2840 
2841 	vscsi->cmd_q.base_addr =
2842 		(struct viosrp_crq *)get_zeroed_page(GFP_KERNEL);
2843 	if (!vscsi->cmd_q.base_addr)
2844 		return -ENOMEM;
2845 
2846 	vscsi->cmd_q.mask = ((uint)pages * CRQ_PER_PAGE) - 1;
2847 
2848 	vscsi->cmd_q.crq_token = dma_map_single(&vdev->dev,
2849 						vscsi->cmd_q.base_addr,
2850 						PAGE_SIZE, DMA_BIDIRECTIONAL);
2851 	if (dma_mapping_error(&vdev->dev, vscsi->cmd_q.crq_token)) {
2852 		free_page((unsigned long)vscsi->cmd_q.base_addr);
2853 		return -ENOMEM;
2854 	}
2855 
2856 	return 0;
2857 }
2858 
2859 /**
2860  * ibmvscsis_destroy_command_q - Destroy Command Queue
2861  * @vscsi:	Pointer to our adapter structure
2862  *
2863  * Releases memory for command queue and unmaps mapped remote memory.
2864  *
2865  * EXECUTION ENVIRONMENT:
2866  *	Process level only
2867  */
2868 static void ibmvscsis_destroy_command_q(struct scsi_info *vscsi)
2869 {
2870 	dma_unmap_single(&vscsi->dma_dev->dev, vscsi->cmd_q.crq_token,
2871 			 PAGE_SIZE, DMA_BIDIRECTIONAL);
2872 	free_page((unsigned long)vscsi->cmd_q.base_addr);
2873 	vscsi->cmd_q.base_addr = NULL;
2874 	vscsi->state = NO_QUEUE;
2875 }
2876 
2877 static u8 ibmvscsis_fast_fail(struct scsi_info *vscsi,
2878 			      struct ibmvscsis_cmd *cmd)
2879 {
2880 	struct iu_entry *iue = cmd->iue;
2881 	struct se_cmd *se_cmd = &cmd->se_cmd;
2882 	struct srp_cmd *srp = (struct srp_cmd *)iue->sbuf->buf;
2883 	struct scsi_sense_hdr sshdr;
2884 	u8 rc = se_cmd->scsi_status;
2885 
2886 	if (vscsi->fast_fail && (READ_CMD(srp->cdb) || WRITE_CMD(srp->cdb)))
2887 		if (scsi_normalize_sense(se_cmd->sense_buffer,
2888 					 se_cmd->scsi_sense_length, &sshdr))
2889 			if (sshdr.sense_key == HARDWARE_ERROR &&
2890 			    (se_cmd->residual_count == 0 ||
2891 			     se_cmd->residual_count == se_cmd->data_length)) {
2892 				rc = NO_SENSE;
2893 				cmd->flags |= CMD_FAST_FAIL;
2894 			}
2895 
2896 	return rc;
2897 }
2898 
2899 /**
2900  * srp_build_response() - Build an SRP response buffer
2901  * @vscsi:	Pointer to our adapter structure
2902  * @cmd:	Pointer to command for which to send the response
2903  * @len_p:	Where to return the length of the IU response sent.  This
2904  *		is needed to construct the CRQ response.
2905  *
2906  * Build the SRP response buffer and copy it to the client's memory space.
2907  */
2908 static long srp_build_response(struct scsi_info *vscsi,
2909 			       struct ibmvscsis_cmd *cmd, uint *len_p)
2910 {
2911 	struct iu_entry *iue = cmd->iue;
2912 	struct se_cmd *se_cmd = &cmd->se_cmd;
2913 	struct srp_rsp *rsp;
2914 	uint len;
2915 	u32 rsp_code;
2916 	char *data;
2917 	u32 *tsk_status;
2918 	long rc = ADAPT_SUCCESS;
2919 
2920 	spin_lock_bh(&vscsi->intr_lock);
2921 
2922 	rsp = &vio_iu(iue)->srp.rsp;
2923 	len = sizeof(*rsp);
2924 	memset(rsp, 0, len);
2925 	data = rsp->data;
2926 
2927 	rsp->opcode = SRP_RSP;
2928 
2929 	if (vscsi->credit > 0 && vscsi->state == SRP_PROCESSING)
2930 		rsp->req_lim_delta = cpu_to_be32(vscsi->credit);
2931 	else
2932 		rsp->req_lim_delta = cpu_to_be32(1 + vscsi->credit);
2933 	rsp->tag = cmd->rsp.tag;
2934 	rsp->flags = 0;
2935 
2936 	if (cmd->type == SCSI_CDB) {
2937 		rsp->status = ibmvscsis_fast_fail(vscsi, cmd);
2938 		if (rsp->status) {
2939 			pr_debug("build_resp: cmd %p, scsi status %d\n", cmd,
2940 				 (int)rsp->status);
2941 			ibmvscsis_determine_resid(se_cmd, rsp);
2942 			if (se_cmd->scsi_sense_length && se_cmd->sense_buffer) {
2943 				rsp->sense_data_len =
2944 					cpu_to_be32(se_cmd->scsi_sense_length);
2945 				rsp->flags |= SRP_RSP_FLAG_SNSVALID;
2946 				len += se_cmd->scsi_sense_length;
2947 				memcpy(data, se_cmd->sense_buffer,
2948 				       se_cmd->scsi_sense_length);
2949 			}
2950 			rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
2951 				UCSOLNT_RESP_SHIFT;
2952 		} else if (cmd->flags & CMD_FAST_FAIL) {
2953 			pr_debug("build_resp: cmd %p, fast fail\n", cmd);
2954 			rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
2955 				UCSOLNT_RESP_SHIFT;
2956 		} else {
2957 			rsp->sol_not = (cmd->rsp.sol_not & SCSOLNT) >>
2958 				SCSOLNT_RESP_SHIFT;
2959 		}
2960 	} else {
2961 		/* this is task management */
2962 		rsp->status = 0;
2963 		rsp->resp_data_len = cpu_to_be32(4);
2964 		rsp->flags |= SRP_RSP_FLAG_RSPVALID;
2965 
2966 		switch (se_cmd->se_tmr_req->response) {
2967 		case TMR_FUNCTION_COMPLETE:
2968 		case TMR_TASK_DOES_NOT_EXIST:
2969 			rsp_code = SRP_TASK_MANAGEMENT_FUNCTION_COMPLETE;
2970 			rsp->sol_not = (cmd->rsp.sol_not & SCSOLNT) >>
2971 				SCSOLNT_RESP_SHIFT;
2972 			break;
2973 		case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
2974 		case TMR_LUN_DOES_NOT_EXIST:
2975 			rsp_code = SRP_TASK_MANAGEMENT_FUNCTION_NOT_SUPPORTED;
2976 			rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
2977 				UCSOLNT_RESP_SHIFT;
2978 			break;
2979 		case TMR_FUNCTION_FAILED:
2980 		case TMR_FUNCTION_REJECTED:
2981 		default:
2982 			rsp_code = SRP_TASK_MANAGEMENT_FUNCTION_FAILED;
2983 			rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
2984 				UCSOLNT_RESP_SHIFT;
2985 			break;
2986 		}
2987 
2988 		tsk_status = (u32 *)data;
2989 		*tsk_status = cpu_to_be32(rsp_code);
2990 		data = (char *)(tsk_status + 1);
2991 		len += 4;
2992 	}
2993 
2994 	dma_wmb();
2995 	rc = h_copy_rdma(len, vscsi->dds.window[LOCAL].liobn, iue->sbuf->dma,
2996 			 vscsi->dds.window[REMOTE].liobn,
2997 			 be64_to_cpu(iue->remote_token));
2998 
2999 	switch (rc) {
3000 	case H_SUCCESS:
3001 		vscsi->credit = 0;
3002 		*len_p = len;
3003 		break;
3004 	case H_PERMISSION:
3005 		if (connection_broken(vscsi))
3006 			vscsi->flags |= RESPONSE_Q_DOWN | CLIENT_FAILED;
3007 
3008 		dev_err(&vscsi->dev, "build_response: error copying to client, rc %ld, flags 0x%x, state 0x%hx\n",
3009 			rc, vscsi->flags, vscsi->state);
3010 		break;
3011 	case H_SOURCE_PARM:
3012 	case H_DEST_PARM:
3013 	default:
3014 		dev_err(&vscsi->dev, "build_response: error copying to client, rc %ld\n",
3015 			rc);
3016 		break;
3017 	}
3018 
3019 	spin_unlock_bh(&vscsi->intr_lock);
3020 
3021 	return rc;
3022 }
3023 
3024 static int ibmvscsis_rdma(struct ibmvscsis_cmd *cmd, struct scatterlist *sg,
3025 			  int nsg, struct srp_direct_buf *md, int nmd,
3026 			  enum dma_data_direction dir, unsigned int bytes)
3027 {
3028 	struct iu_entry *iue = cmd->iue;
3029 	struct srp_target *target = iue->target;
3030 	struct scsi_info *vscsi = target->ldata;
3031 	struct scatterlist *sgp;
3032 	dma_addr_t client_ioba, server_ioba;
3033 	ulong buf_len;
3034 	ulong client_len, server_len;
3035 	int md_idx;
3036 	long tx_len;
3037 	long rc = 0;
3038 
3039 	if (bytes == 0)
3040 		return 0;
3041 
3042 	sgp = sg;
3043 	client_len = 0;
3044 	server_len = 0;
3045 	md_idx = 0;
3046 	tx_len = bytes;
3047 
3048 	do {
3049 		if (client_len == 0) {
3050 			if (md_idx >= nmd) {
3051 				dev_err(&vscsi->dev, "rdma: ran out of client memory descriptors\n");
3052 				rc = -EIO;
3053 				break;
3054 			}
3055 			client_ioba = be64_to_cpu(md[md_idx].va);
3056 			client_len = be32_to_cpu(md[md_idx].len);
3057 		}
3058 		if (server_len == 0) {
3059 			if (!sgp) {
3060 				dev_err(&vscsi->dev, "rdma: ran out of scatter/gather list\n");
3061 				rc = -EIO;
3062 				break;
3063 			}
3064 			server_ioba = sg_dma_address(sgp);
3065 			server_len = sg_dma_len(sgp);
3066 		}
3067 
3068 		buf_len = tx_len;
3069 
3070 		if (buf_len > client_len)
3071 			buf_len = client_len;
3072 
3073 		if (buf_len > server_len)
3074 			buf_len = server_len;
3075 
3076 		if (buf_len > max_vdma_size)
3077 			buf_len = max_vdma_size;
3078 
3079 		if (dir == DMA_TO_DEVICE) {
3080 			/* read from client */
3081 			rc = h_copy_rdma(buf_len,
3082 					 vscsi->dds.window[REMOTE].liobn,
3083 					 client_ioba,
3084 					 vscsi->dds.window[LOCAL].liobn,
3085 					 server_ioba);
3086 		} else {
3087 			/* The h_copy_rdma will cause phyp, running in another
3088 			 * partition, to read memory, so we need to make sure
3089 			 * the data has been written out, hence these syncs.
3090 			 */
3091 			/* ensure that everything is in memory */
3092 			isync();
3093 			/* ensure that memory has been made visible */
3094 			dma_wmb();
3095 			rc = h_copy_rdma(buf_len,
3096 					 vscsi->dds.window[LOCAL].liobn,
3097 					 server_ioba,
3098 					 vscsi->dds.window[REMOTE].liobn,
3099 					 client_ioba);
3100 		}
3101 		switch (rc) {
3102 		case H_SUCCESS:
3103 			break;
3104 		case H_PERMISSION:
3105 		case H_SOURCE_PARM:
3106 		case H_DEST_PARM:
3107 			if (connection_broken(vscsi)) {
3108 				spin_lock_bh(&vscsi->intr_lock);
3109 				vscsi->flags |=
3110 					(RESPONSE_Q_DOWN | CLIENT_FAILED);
3111 				spin_unlock_bh(&vscsi->intr_lock);
3112 			}
3113 			dev_err(&vscsi->dev, "rdma: h_copy_rdma failed, rc %ld\n",
3114 				rc);
3115 			break;
3116 
3117 		default:
3118 			dev_err(&vscsi->dev, "rdma: unknown error %ld from h_copy_rdma\n",
3119 				rc);
3120 			break;
3121 		}
3122 
3123 		if (!rc) {
3124 			tx_len -= buf_len;
3125 			if (tx_len) {
3126 				client_len -= buf_len;
3127 				if (client_len == 0)
3128 					md_idx++;
3129 				else
3130 					client_ioba += buf_len;
3131 
3132 				server_len -= buf_len;
3133 				if (server_len == 0)
3134 					sgp = sg_next(sgp);
3135 				else
3136 					server_ioba += buf_len;
3137 			} else {
3138 				break;
3139 			}
3140 		}
3141 	} while (!rc);
3142 
3143 	return rc;
3144 }
3145 
3146 /**
3147  * ibmvscsis_handle_crq() - Handle CRQ
3148  * @data:	Pointer to our adapter structure
3149  *
3150  * Read the command elements from the command queue and copy the payloads
3151  * associated with the command elements to local memory and execute the
3152  * SRP requests.
3153  *
3154  * Note: this is an edge triggered interrupt. It can not be shared.
3155  */
3156 static void ibmvscsis_handle_crq(unsigned long data)
3157 {
3158 	struct scsi_info *vscsi = (struct scsi_info *)data;
3159 	struct viosrp_crq *crq;
3160 	long rc;
3161 	bool ack = true;
3162 	volatile u8 valid;
3163 
3164 	spin_lock_bh(&vscsi->intr_lock);
3165 
3166 	pr_debug("got interrupt\n");
3167 
3168 	/*
3169 	 * if we are in a path where we are waiting for all pending commands
3170 	 * to complete because we received a transport event and anything in
3171 	 * the command queue is for a new connection, do nothing
3172 	 */
3173 	if (TARGET_STOP(vscsi)) {
3174 		vio_enable_interrupts(vscsi->dma_dev);
3175 
3176 		pr_debug("handle_crq, don't process: flags 0x%x, state 0x%hx\n",
3177 			 vscsi->flags, vscsi->state);
3178 		spin_unlock_bh(&vscsi->intr_lock);
3179 		return;
3180 	}
3181 
3182 	rc = vscsi->flags & SCHEDULE_DISCONNECT;
3183 	crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
3184 	valid = crq->valid;
3185 	dma_rmb();
3186 
3187 	while (valid) {
3188 		/*
3189 		 * These are edege triggered interrupts. After dropping out of
3190 		 * the while loop, the code must check for work since an
3191 		 * interrupt could be lost, and an elment be left on the queue,
3192 		 * hence the label.
3193 		 */
3194 cmd_work:
3195 		vscsi->cmd_q.index =
3196 			(vscsi->cmd_q.index + 1) & vscsi->cmd_q.mask;
3197 
3198 		if (!rc) {
3199 			rc = ibmvscsis_parse_command(vscsi, crq);
3200 		} else {
3201 			if ((uint)crq->valid == VALID_TRANS_EVENT) {
3202 				/*
3203 				 * must service the transport layer events even
3204 				 * in an error state, dont break out until all
3205 				 * the consecutive transport events have been
3206 				 * processed
3207 				 */
3208 				rc = ibmvscsis_trans_event(vscsi, crq);
3209 			} else if (vscsi->flags & TRANS_EVENT) {
3210 				/*
3211 				 * if a transport event has occurred leave
3212 				 * everything but transport events on the queue
3213 				 *
3214 				 * need to decrement the queue index so we can
3215 				 * look at the element again
3216 				 */
3217 				if (vscsi->cmd_q.index)
3218 					vscsi->cmd_q.index -= 1;
3219 				else
3220 					/*
3221 					 * index is at 0 it just wrapped.
3222 					 * have it index last element in q
3223 					 */
3224 					vscsi->cmd_q.index = vscsi->cmd_q.mask;
3225 				break;
3226 			}
3227 		}
3228 
3229 		crq->valid = INVALIDATE_CMD_RESP_EL;
3230 
3231 		crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
3232 		valid = crq->valid;
3233 		dma_rmb();
3234 	}
3235 
3236 	if (!rc) {
3237 		if (ack) {
3238 			vio_enable_interrupts(vscsi->dma_dev);
3239 			ack = false;
3240 			pr_debug("handle_crq, reenabling interrupts\n");
3241 		}
3242 		valid = crq->valid;
3243 		dma_rmb();
3244 		if (valid)
3245 			goto cmd_work;
3246 	} else {
3247 		pr_debug("handle_crq, error: flags 0x%x, state 0x%hx, crq index 0x%x\n",
3248 			 vscsi->flags, vscsi->state, vscsi->cmd_q.index);
3249 	}
3250 
3251 	pr_debug("Leaving handle_crq: schedule_q empty %d, flags 0x%x, state 0x%hx\n",
3252 		 (int)list_empty(&vscsi->schedule_q), vscsi->flags,
3253 		 vscsi->state);
3254 
3255 	spin_unlock_bh(&vscsi->intr_lock);
3256 }
3257 
3258 static int ibmvscsis_probe(struct vio_dev *vdev,
3259 			   const struct vio_device_id *id)
3260 {
3261 	struct scsi_info *vscsi;
3262 	int rc = 0;
3263 	long hrc = 0;
3264 	char wq_name[24];
3265 
3266 	vscsi = kzalloc(sizeof(*vscsi), GFP_KERNEL);
3267 	if (!vscsi) {
3268 		rc = -ENOMEM;
3269 		pr_err("probe: allocation of adapter failed\n");
3270 		return rc;
3271 	}
3272 
3273 	vscsi->dma_dev = vdev;
3274 	vscsi->dev = vdev->dev;
3275 	INIT_LIST_HEAD(&vscsi->schedule_q);
3276 	INIT_LIST_HEAD(&vscsi->waiting_rsp);
3277 	INIT_LIST_HEAD(&vscsi->active_q);
3278 
3279 	snprintf(vscsi->tport.tport_name, IBMVSCSIS_NAMELEN, "%s",
3280 		 dev_name(&vdev->dev));
3281 
3282 	pr_debug("probe tport_name: %s\n", vscsi->tport.tport_name);
3283 
3284 	rc = read_dma_window(vscsi);
3285 	if (rc)
3286 		goto free_adapter;
3287 	pr_debug("Probe: liobn 0x%x, riobn 0x%x\n",
3288 		 vscsi->dds.window[LOCAL].liobn,
3289 		 vscsi->dds.window[REMOTE].liobn);
3290 
3291 	strcpy(vscsi->eye, "VSCSI ");
3292 	strncat(vscsi->eye, vdev->name, MAX_EYE);
3293 
3294 	vscsi->dds.unit_id = vdev->unit_address;
3295 	strncpy(vscsi->dds.partition_name, partition_name,
3296 		sizeof(vscsi->dds.partition_name));
3297 	vscsi->dds.partition_num = partition_number;
3298 
3299 	spin_lock_bh(&ibmvscsis_dev_lock);
3300 	list_add_tail(&vscsi->list, &ibmvscsis_dev_list);
3301 	spin_unlock_bh(&ibmvscsis_dev_lock);
3302 
3303 	/*
3304 	 * TBD: How do we determine # of cmds to request?  Do we know how
3305 	 * many "children" we have?
3306 	 */
3307 	vscsi->request_limit = INITIAL_SRP_LIMIT;
3308 	rc = srp_target_alloc(&vscsi->target, &vdev->dev, vscsi->request_limit,
3309 			      SRP_MAX_IU_LEN);
3310 	if (rc)
3311 		goto rem_list;
3312 
3313 	vscsi->target.ldata = vscsi;
3314 
3315 	rc = ibmvscsis_alloc_cmds(vscsi, vscsi->request_limit);
3316 	if (rc) {
3317 		dev_err(&vscsi->dev, "alloc_cmds failed, rc %d, num %d\n",
3318 			rc, vscsi->request_limit);
3319 		goto free_target;
3320 	}
3321 
3322 	/*
3323 	 * Note: the lock is used in freeing timers, so must initialize
3324 	 * first so that ordering in case of error is correct.
3325 	 */
3326 	spin_lock_init(&vscsi->intr_lock);
3327 
3328 	rc = ibmvscsis_alloctimer(vscsi);
3329 	if (rc) {
3330 		dev_err(&vscsi->dev, "probe: alloctimer failed, rc %d\n", rc);
3331 		goto free_cmds;
3332 	}
3333 
3334 	rc = ibmvscsis_create_command_q(vscsi, 256);
3335 	if (rc) {
3336 		dev_err(&vscsi->dev, "probe: create_command_q failed, rc %d\n",
3337 			rc);
3338 		goto free_timer;
3339 	}
3340 
3341 	vscsi->map_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
3342 	if (!vscsi->map_buf) {
3343 		rc = -ENOMEM;
3344 		dev_err(&vscsi->dev, "probe: allocating cmd buffer failed\n");
3345 		goto destroy_queue;
3346 	}
3347 
3348 	vscsi->map_ioba = dma_map_single(&vdev->dev, vscsi->map_buf, PAGE_SIZE,
3349 					 DMA_BIDIRECTIONAL);
3350 	if (dma_mapping_error(&vdev->dev, vscsi->map_ioba)) {
3351 		rc = -ENOMEM;
3352 		dev_err(&vscsi->dev, "probe: error mapping command buffer\n");
3353 		goto free_buf;
3354 	}
3355 
3356 	hrc = h_vioctl(vscsi->dds.unit_id, H_GET_PARTNER_INFO,
3357 		       (u64)vscsi->map_ioba | ((u64)PAGE_SIZE << 32), 0, 0, 0,
3358 		       0);
3359 	if (hrc == H_SUCCESS)
3360 		vscsi->client_data.partition_number =
3361 			be64_to_cpu(*(u64 *)vscsi->map_buf);
3362 	/*
3363 	 * We expect the VIOCTL to fail if we're configured as "any
3364 	 * client can connect" and the client isn't activated yet.
3365 	 * We'll make the call again when he sends an init msg.
3366 	 */
3367 	pr_debug("probe hrc %ld, client partition num %d\n",
3368 		 hrc, vscsi->client_data.partition_number);
3369 
3370 	tasklet_init(&vscsi->work_task, ibmvscsis_handle_crq,
3371 		     (unsigned long)vscsi);
3372 
3373 	init_completion(&vscsi->wait_idle);
3374 	init_completion(&vscsi->unconfig);
3375 
3376 	snprintf(wq_name, 24, "ibmvscsis%s", dev_name(&vdev->dev));
3377 	vscsi->work_q = create_workqueue(wq_name);
3378 	if (!vscsi->work_q) {
3379 		rc = -ENOMEM;
3380 		dev_err(&vscsi->dev, "create_workqueue failed\n");
3381 		goto unmap_buf;
3382 	}
3383 
3384 	rc = request_irq(vdev->irq, ibmvscsis_interrupt, 0, "ibmvscsis", vscsi);
3385 	if (rc) {
3386 		rc = -EPERM;
3387 		dev_err(&vscsi->dev, "probe: request_irq failed, rc %d\n", rc);
3388 		goto destroy_WQ;
3389 	}
3390 
3391 	vscsi->state = WAIT_ENABLED;
3392 
3393 	dev_set_drvdata(&vdev->dev, vscsi);
3394 
3395 	return 0;
3396 
3397 destroy_WQ:
3398 	destroy_workqueue(vscsi->work_q);
3399 unmap_buf:
3400 	dma_unmap_single(&vdev->dev, vscsi->map_ioba, PAGE_SIZE,
3401 			 DMA_BIDIRECTIONAL);
3402 free_buf:
3403 	kfree(vscsi->map_buf);
3404 destroy_queue:
3405 	tasklet_kill(&vscsi->work_task);
3406 	ibmvscsis_unregister_command_q(vscsi);
3407 	ibmvscsis_destroy_command_q(vscsi);
3408 free_timer:
3409 	ibmvscsis_freetimer(vscsi);
3410 free_cmds:
3411 	ibmvscsis_free_cmds(vscsi);
3412 free_target:
3413 	srp_target_free(&vscsi->target);
3414 rem_list:
3415 	spin_lock_bh(&ibmvscsis_dev_lock);
3416 	list_del(&vscsi->list);
3417 	spin_unlock_bh(&ibmvscsis_dev_lock);
3418 free_adapter:
3419 	kfree(vscsi);
3420 
3421 	return rc;
3422 }
3423 
3424 static int ibmvscsis_remove(struct vio_dev *vdev)
3425 {
3426 	struct scsi_info *vscsi = dev_get_drvdata(&vdev->dev);
3427 
3428 	pr_debug("remove (%s)\n", dev_name(&vscsi->dma_dev->dev));
3429 
3430 	spin_lock_bh(&vscsi->intr_lock);
3431 	ibmvscsis_post_disconnect(vscsi, UNCONFIGURING, 0);
3432 	vscsi->flags |= CFG_SLEEPING;
3433 	spin_unlock_bh(&vscsi->intr_lock);
3434 	wait_for_completion(&vscsi->unconfig);
3435 
3436 	vio_disable_interrupts(vdev);
3437 	free_irq(vdev->irq, vscsi);
3438 	destroy_workqueue(vscsi->work_q);
3439 	dma_unmap_single(&vdev->dev, vscsi->map_ioba, PAGE_SIZE,
3440 			 DMA_BIDIRECTIONAL);
3441 	kfree(vscsi->map_buf);
3442 	tasklet_kill(&vscsi->work_task);
3443 	ibmvscsis_destroy_command_q(vscsi);
3444 	ibmvscsis_freetimer(vscsi);
3445 	ibmvscsis_free_cmds(vscsi);
3446 	srp_target_free(&vscsi->target);
3447 	spin_lock_bh(&ibmvscsis_dev_lock);
3448 	list_del(&vscsi->list);
3449 	spin_unlock_bh(&ibmvscsis_dev_lock);
3450 	kfree(vscsi);
3451 
3452 	return 0;
3453 }
3454 
3455 static ssize_t system_id_show(struct device *dev,
3456 			      struct device_attribute *attr, char *buf)
3457 {
3458 	return snprintf(buf, PAGE_SIZE, "%s\n", system_id);
3459 }
3460 
3461 static ssize_t partition_number_show(struct device *dev,
3462 				     struct device_attribute *attr, char *buf)
3463 {
3464 	return snprintf(buf, PAGE_SIZE, "%x\n", partition_number);
3465 }
3466 
3467 static ssize_t unit_address_show(struct device *dev,
3468 				 struct device_attribute *attr, char *buf)
3469 {
3470 	struct scsi_info *vscsi = container_of(dev, struct scsi_info, dev);
3471 
3472 	return snprintf(buf, PAGE_SIZE, "%x\n", vscsi->dma_dev->unit_address);
3473 }
3474 
3475 static int ibmvscsis_get_system_info(void)
3476 {
3477 	struct device_node *rootdn, *vdevdn;
3478 	const char *id, *model, *name;
3479 	const uint *num;
3480 
3481 	rootdn = of_find_node_by_path("/");
3482 	if (!rootdn)
3483 		return -ENOENT;
3484 
3485 	model = of_get_property(rootdn, "model", NULL);
3486 	id = of_get_property(rootdn, "system-id", NULL);
3487 	if (model && id)
3488 		snprintf(system_id, sizeof(system_id), "%s-%s", model, id);
3489 
3490 	name = of_get_property(rootdn, "ibm,partition-name", NULL);
3491 	if (name)
3492 		strncpy(partition_name, name, sizeof(partition_name));
3493 
3494 	num = of_get_property(rootdn, "ibm,partition-no", NULL);
3495 	if (num)
3496 		partition_number = of_read_number(num, 1);
3497 
3498 	of_node_put(rootdn);
3499 
3500 	vdevdn = of_find_node_by_path("/vdevice");
3501 	if (vdevdn) {
3502 		const uint *mvds;
3503 
3504 		mvds = of_get_property(vdevdn, "ibm,max-virtual-dma-size",
3505 				       NULL);
3506 		if (mvds)
3507 			max_vdma_size = *mvds;
3508 		of_node_put(vdevdn);
3509 	}
3510 
3511 	return 0;
3512 }
3513 
3514 static char *ibmvscsis_get_fabric_name(void)
3515 {
3516 	return "ibmvscsis";
3517 }
3518 
3519 static char *ibmvscsis_get_fabric_wwn(struct se_portal_group *se_tpg)
3520 {
3521 	struct ibmvscsis_tport *tport =
3522 		container_of(se_tpg, struct ibmvscsis_tport, se_tpg);
3523 
3524 	return tport->tport_name;
3525 }
3526 
3527 static u16 ibmvscsis_get_tag(struct se_portal_group *se_tpg)
3528 {
3529 	struct ibmvscsis_tport *tport =
3530 		container_of(se_tpg, struct ibmvscsis_tport, se_tpg);
3531 
3532 	return tport->tport_tpgt;
3533 }
3534 
3535 static u32 ibmvscsis_get_default_depth(struct se_portal_group *se_tpg)
3536 {
3537 	return 1;
3538 }
3539 
3540 static int ibmvscsis_check_true(struct se_portal_group *se_tpg)
3541 {
3542 	return 1;
3543 }
3544 
3545 static int ibmvscsis_check_false(struct se_portal_group *se_tpg)
3546 {
3547 	return 0;
3548 }
3549 
3550 static u32 ibmvscsis_tpg_get_inst_index(struct se_portal_group *se_tpg)
3551 {
3552 	return 1;
3553 }
3554 
3555 static int ibmvscsis_check_stop_free(struct se_cmd *se_cmd)
3556 {
3557 	return target_put_sess_cmd(se_cmd);
3558 }
3559 
3560 static void ibmvscsis_release_cmd(struct se_cmd *se_cmd)
3561 {
3562 	struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
3563 						 se_cmd);
3564 	struct scsi_info *vscsi = cmd->adapter;
3565 
3566 	spin_lock_bh(&vscsi->intr_lock);
3567 	/* Remove from active_q */
3568 	list_move_tail(&cmd->list, &vscsi->waiting_rsp);
3569 	ibmvscsis_send_messages(vscsi);
3570 	spin_unlock_bh(&vscsi->intr_lock);
3571 }
3572 
3573 static u32 ibmvscsis_sess_get_index(struct se_session *se_sess)
3574 {
3575 	return 0;
3576 }
3577 
3578 static int ibmvscsis_write_pending(struct se_cmd *se_cmd)
3579 {
3580 	struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
3581 						 se_cmd);
3582 	struct iu_entry *iue = cmd->iue;
3583 	int rc;
3584 
3585 	rc = srp_transfer_data(cmd, &vio_iu(iue)->srp.cmd, ibmvscsis_rdma,
3586 			       1, 1);
3587 	if (rc) {
3588 		pr_err("srp_transfer_data() failed: %d\n", rc);
3589 		return -EIO;
3590 	}
3591 	/*
3592 	 * We now tell TCM to add this WRITE CDB directly into the TCM storage
3593 	 * object execution queue.
3594 	 */
3595 	target_execute_cmd(se_cmd);
3596 	return 0;
3597 }
3598 
3599 static int ibmvscsis_write_pending_status(struct se_cmd *se_cmd)
3600 {
3601 	return 0;
3602 }
3603 
3604 static void ibmvscsis_set_default_node_attrs(struct se_node_acl *nacl)
3605 {
3606 }
3607 
3608 static int ibmvscsis_get_cmd_state(struct se_cmd *se_cmd)
3609 {
3610 	return 0;
3611 }
3612 
3613 static int ibmvscsis_queue_data_in(struct se_cmd *se_cmd)
3614 {
3615 	struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
3616 						 se_cmd);
3617 	struct iu_entry *iue = cmd->iue;
3618 	struct scsi_info *vscsi = cmd->adapter;
3619 	char *sd;
3620 	uint len = 0;
3621 	int rc;
3622 
3623 	rc = srp_transfer_data(cmd, &vio_iu(iue)->srp.cmd, ibmvscsis_rdma, 1,
3624 			       1);
3625 	if (rc) {
3626 		pr_err("srp_transfer_data failed: %d\n", rc);
3627 		sd = se_cmd->sense_buffer;
3628 		se_cmd->scsi_sense_length = 18;
3629 		memset(se_cmd->sense_buffer, 0, se_cmd->scsi_sense_length);
3630 		/* Logical Unit Communication Time-out asc/ascq = 0x0801 */
3631 		scsi_build_sense_buffer(0, se_cmd->sense_buffer, MEDIUM_ERROR,
3632 					0x08, 0x01);
3633 	}
3634 
3635 	srp_build_response(vscsi, cmd, &len);
3636 	cmd->rsp.format = SRP_FORMAT;
3637 	cmd->rsp.len = len;
3638 
3639 	return 0;
3640 }
3641 
3642 static int ibmvscsis_queue_status(struct se_cmd *se_cmd)
3643 {
3644 	struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
3645 						 se_cmd);
3646 	struct scsi_info *vscsi = cmd->adapter;
3647 	uint len;
3648 
3649 	pr_debug("queue_status %p\n", se_cmd);
3650 
3651 	srp_build_response(vscsi, cmd, &len);
3652 	cmd->rsp.format = SRP_FORMAT;
3653 	cmd->rsp.len = len;
3654 
3655 	return 0;
3656 }
3657 
3658 static void ibmvscsis_queue_tm_rsp(struct se_cmd *se_cmd)
3659 {
3660 	struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
3661 						 se_cmd);
3662 	struct scsi_info *vscsi = cmd->adapter;
3663 	uint len;
3664 
3665 	pr_debug("queue_tm_rsp %p, status %d\n",
3666 		 se_cmd, (int)se_cmd->se_tmr_req->response);
3667 
3668 	srp_build_response(vscsi, cmd, &len);
3669 	cmd->rsp.format = SRP_FORMAT;
3670 	cmd->rsp.len = len;
3671 }
3672 
3673 static void ibmvscsis_aborted_task(struct se_cmd *se_cmd)
3674 {
3675 	/* TBD: What (if anything) should we do here? */
3676 	pr_debug("ibmvscsis_aborted_task %p\n", se_cmd);
3677 }
3678 
3679 static struct se_wwn *ibmvscsis_make_tport(struct target_fabric_configfs *tf,
3680 					   struct config_group *group,
3681 					   const char *name)
3682 {
3683 	struct ibmvscsis_tport *tport;
3684 
3685 	tport = ibmvscsis_lookup_port(name);
3686 	if (tport) {
3687 		tport->tport_proto_id = SCSI_PROTOCOL_SRP;
3688 		pr_debug("make_tport(%s), pointer:%p, tport_id:%x\n",
3689 			 name, tport, tport->tport_proto_id);
3690 		return &tport->tport_wwn;
3691 	}
3692 
3693 	return ERR_PTR(-EINVAL);
3694 }
3695 
3696 static void ibmvscsis_drop_tport(struct se_wwn *wwn)
3697 {
3698 	struct ibmvscsis_tport *tport = container_of(wwn,
3699 						     struct ibmvscsis_tport,
3700 						     tport_wwn);
3701 
3702 	pr_debug("drop_tport(%s)\n",
3703 		 config_item_name(&tport->tport_wwn.wwn_group.cg_item));
3704 }
3705 
3706 static struct se_portal_group *ibmvscsis_make_tpg(struct se_wwn *wwn,
3707 						  struct config_group *group,
3708 						  const char *name)
3709 {
3710 	struct ibmvscsis_tport *tport =
3711 		container_of(wwn, struct ibmvscsis_tport, tport_wwn);
3712 	int rc;
3713 
3714 	tport->releasing = false;
3715 
3716 	rc = core_tpg_register(&tport->tport_wwn, &tport->se_tpg,
3717 			       tport->tport_proto_id);
3718 	if (rc)
3719 		return ERR_PTR(rc);
3720 
3721 	return &tport->se_tpg;
3722 }
3723 
3724 static void ibmvscsis_drop_tpg(struct se_portal_group *se_tpg)
3725 {
3726 	struct ibmvscsis_tport *tport = container_of(se_tpg,
3727 						     struct ibmvscsis_tport,
3728 						     se_tpg);
3729 
3730 	tport->releasing = true;
3731 	tport->enabled = false;
3732 
3733 	/*
3734 	 * Release the virtual I_T Nexus for this ibmvscsis TPG
3735 	 */
3736 	ibmvscsis_drop_nexus(tport);
3737 	/*
3738 	 * Deregister the se_tpg from TCM..
3739 	 */
3740 	core_tpg_deregister(se_tpg);
3741 }
3742 
3743 static ssize_t ibmvscsis_wwn_version_show(struct config_item *item,
3744 					  char *page)
3745 {
3746 	return scnprintf(page, PAGE_SIZE, "%s\n", IBMVSCSIS_VERSION);
3747 }
3748 CONFIGFS_ATTR_RO(ibmvscsis_wwn_, version);
3749 
3750 static struct configfs_attribute *ibmvscsis_wwn_attrs[] = {
3751 	&ibmvscsis_wwn_attr_version,
3752 	NULL,
3753 };
3754 
3755 static ssize_t ibmvscsis_tpg_enable_show(struct config_item *item,
3756 					 char *page)
3757 {
3758 	struct se_portal_group *se_tpg = to_tpg(item);
3759 	struct ibmvscsis_tport *tport = container_of(se_tpg,
3760 						     struct ibmvscsis_tport,
3761 						     se_tpg);
3762 
3763 	return snprintf(page, PAGE_SIZE, "%d\n", (tport->enabled) ? 1 : 0);
3764 }
3765 
3766 static ssize_t ibmvscsis_tpg_enable_store(struct config_item *item,
3767 					  const char *page, size_t count)
3768 {
3769 	struct se_portal_group *se_tpg = to_tpg(item);
3770 	struct ibmvscsis_tport *tport = container_of(se_tpg,
3771 						     struct ibmvscsis_tport,
3772 						     se_tpg);
3773 	struct scsi_info *vscsi = container_of(tport, struct scsi_info, tport);
3774 	unsigned long tmp;
3775 	int rc;
3776 	long lrc;
3777 
3778 	rc = kstrtoul(page, 0, &tmp);
3779 	if (rc < 0) {
3780 		pr_err("Unable to extract srpt_tpg_store_enable\n");
3781 		return -EINVAL;
3782 	}
3783 
3784 	if ((tmp != 0) && (tmp != 1)) {
3785 		pr_err("Illegal value for srpt_tpg_store_enable\n");
3786 		return -EINVAL;
3787 	}
3788 
3789 	if (tmp) {
3790 		spin_lock_bh(&vscsi->intr_lock);
3791 		tport->enabled = true;
3792 		lrc = ibmvscsis_enable_change_state(vscsi);
3793 		if (lrc)
3794 			pr_err("enable_change_state failed, rc %ld state %d\n",
3795 			       lrc, vscsi->state);
3796 		spin_unlock_bh(&vscsi->intr_lock);
3797 	} else {
3798 		spin_lock_bh(&vscsi->intr_lock);
3799 		tport->enabled = false;
3800 		/* This simulates the server going down */
3801 		ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
3802 		spin_unlock_bh(&vscsi->intr_lock);
3803 	}
3804 
3805 	pr_debug("tpg_enable_store, tmp %ld, state %d\n", tmp, vscsi->state);
3806 
3807 	return count;
3808 }
3809 CONFIGFS_ATTR(ibmvscsis_tpg_, enable);
3810 
3811 static struct configfs_attribute *ibmvscsis_tpg_attrs[] = {
3812 	&ibmvscsis_tpg_attr_enable,
3813 	NULL,
3814 };
3815 
3816 static const struct target_core_fabric_ops ibmvscsis_ops = {
3817 	.module				= THIS_MODULE,
3818 	.name				= "ibmvscsis",
3819 	.max_data_sg_nents		= MAX_TXU / PAGE_SIZE,
3820 	.get_fabric_name		= ibmvscsis_get_fabric_name,
3821 	.tpg_get_wwn			= ibmvscsis_get_fabric_wwn,
3822 	.tpg_get_tag			= ibmvscsis_get_tag,
3823 	.tpg_get_default_depth		= ibmvscsis_get_default_depth,
3824 	.tpg_check_demo_mode		= ibmvscsis_check_true,
3825 	.tpg_check_demo_mode_cache	= ibmvscsis_check_true,
3826 	.tpg_check_demo_mode_write_protect = ibmvscsis_check_false,
3827 	.tpg_check_prod_mode_write_protect = ibmvscsis_check_false,
3828 	.tpg_get_inst_index		= ibmvscsis_tpg_get_inst_index,
3829 	.check_stop_free		= ibmvscsis_check_stop_free,
3830 	.release_cmd			= ibmvscsis_release_cmd,
3831 	.sess_get_index			= ibmvscsis_sess_get_index,
3832 	.write_pending			= ibmvscsis_write_pending,
3833 	.write_pending_status		= ibmvscsis_write_pending_status,
3834 	.set_default_node_attributes	= ibmvscsis_set_default_node_attrs,
3835 	.get_cmd_state			= ibmvscsis_get_cmd_state,
3836 	.queue_data_in			= ibmvscsis_queue_data_in,
3837 	.queue_status			= ibmvscsis_queue_status,
3838 	.queue_tm_rsp			= ibmvscsis_queue_tm_rsp,
3839 	.aborted_task			= ibmvscsis_aborted_task,
3840 	/*
3841 	 * Setup function pointers for logic in target_core_fabric_configfs.c
3842 	 */
3843 	.fabric_make_wwn		= ibmvscsis_make_tport,
3844 	.fabric_drop_wwn		= ibmvscsis_drop_tport,
3845 	.fabric_make_tpg		= ibmvscsis_make_tpg,
3846 	.fabric_drop_tpg		= ibmvscsis_drop_tpg,
3847 
3848 	.tfc_wwn_attrs			= ibmvscsis_wwn_attrs,
3849 	.tfc_tpg_base_attrs		= ibmvscsis_tpg_attrs,
3850 };
3851 
3852 static void ibmvscsis_dev_release(struct device *dev) {};
3853 
3854 static struct class_attribute ibmvscsis_class_attrs[] = {
3855 	__ATTR_NULL,
3856 };
3857 
3858 static struct device_attribute dev_attr_system_id =
3859 	__ATTR(system_id, S_IRUGO, system_id_show, NULL);
3860 
3861 static struct device_attribute dev_attr_partition_number =
3862 	__ATTR(partition_number, S_IRUGO, partition_number_show, NULL);
3863 
3864 static struct device_attribute dev_attr_unit_address =
3865 	__ATTR(unit_address, S_IRUGO, unit_address_show, NULL);
3866 
3867 static struct attribute *ibmvscsis_dev_attrs[] = {
3868 	&dev_attr_system_id.attr,
3869 	&dev_attr_partition_number.attr,
3870 	&dev_attr_unit_address.attr,
3871 };
3872 ATTRIBUTE_GROUPS(ibmvscsis_dev);
3873 
3874 static struct class ibmvscsis_class = {
3875 	.name		= "ibmvscsis",
3876 	.dev_release	= ibmvscsis_dev_release,
3877 	.class_attrs	= ibmvscsis_class_attrs,
3878 	.dev_groups	= ibmvscsis_dev_groups,
3879 };
3880 
3881 static struct vio_device_id ibmvscsis_device_table[] = {
3882 	{ "v-scsi-host", "IBM,v-scsi-host" },
3883 	{ "", "" }
3884 };
3885 MODULE_DEVICE_TABLE(vio, ibmvscsis_device_table);
3886 
3887 static struct vio_driver ibmvscsis_driver = {
3888 	.name = "ibmvscsis",
3889 	.id_table = ibmvscsis_device_table,
3890 	.probe = ibmvscsis_probe,
3891 	.remove = ibmvscsis_remove,
3892 };
3893 
3894 /*
3895  * ibmvscsis_init() - Kernel Module initialization
3896  *
3897  * Note: vio_register_driver() registers callback functions, and at least one
3898  * of those callback functions calls TCM - Linux IO Target Subsystem, thus
3899  * the SCSI Target template must be registered before vio_register_driver()
3900  * is called.
3901  */
3902 static int __init ibmvscsis_init(void)
3903 {
3904 	int rc = 0;
3905 
3906 	rc = ibmvscsis_get_system_info();
3907 	if (rc) {
3908 		pr_err("rc %d from get_system_info\n", rc);
3909 		goto out;
3910 	}
3911 
3912 	rc = class_register(&ibmvscsis_class);
3913 	if (rc) {
3914 		pr_err("failed class register\n");
3915 		goto out;
3916 	}
3917 
3918 	rc = target_register_template(&ibmvscsis_ops);
3919 	if (rc) {
3920 		pr_err("rc %d from target_register_template\n", rc);
3921 		goto unregister_class;
3922 	}
3923 
3924 	rc = vio_register_driver(&ibmvscsis_driver);
3925 	if (rc) {
3926 		pr_err("rc %d from vio_register_driver\n", rc);
3927 		goto unregister_target;
3928 	}
3929 
3930 	return 0;
3931 
3932 unregister_target:
3933 	target_unregister_template(&ibmvscsis_ops);
3934 unregister_class:
3935 	class_unregister(&ibmvscsis_class);
3936 out:
3937 	return rc;
3938 }
3939 
3940 static void __exit ibmvscsis_exit(void)
3941 {
3942 	pr_info("Unregister IBM virtual SCSI host driver\n");
3943 	vio_unregister_driver(&ibmvscsis_driver);
3944 	target_unregister_template(&ibmvscsis_ops);
3945 	class_unregister(&ibmvscsis_class);
3946 }
3947 
3948 MODULE_DESCRIPTION("IBMVSCSIS fabric driver");
3949 MODULE_AUTHOR("Bryant G. Ly and Michael Cyr");
3950 MODULE_LICENSE("GPL");
3951 MODULE_VERSION(IBMVSCSIS_VERSION);
3952 module_init(ibmvscsis_init);
3953 module_exit(ibmvscsis_exit);
3954