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.
7 * Copyright (C) 2005-2011 FUJITA Tomonori <tomof@acm.org>
8 * Copyright (C) 2010 Nicholas A. Bellinger <nab@kernel.org>
10 * Authors: Bryant G. Ly <bryantly@linux.vnet.ibm.com>
11 * Authors: Michael Cyr <mikecyr@linux.vnet.ibm.com>
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.
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.
23 ****************************************************************************/
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
35 #include <target/target_core_base.h>
36 #include <target/target_core_fabric.h>
38 #include <asm/hvcall.h>
41 #include <scsi/viosrp.h>
43 #include "ibmvscsi_tgt.h"
45 #define IBMVSCSIS_VERSION "v0.2"
47 #define INITIAL_SRP_LIMIT 800
48 #define DEFAULT_MAX_SECTORS 256
49 #define MAX_TXU 1024 * 1024
51 static uint max_vdma_size = MAX_H_COPY_RDMA;
53 static char system_id[SYS_ID_NAME_LEN] = "";
54 static char partition_name[PARTITION_NAMELEN] = "UNKNOWN";
55 static uint partition_number = -1;
57 /* Adapter list and lock to control it */
58 static DEFINE_SPINLOCK(ibmvscsis_dev_lock);
59 static LIST_HEAD(ibmvscsis_dev_list);
61 static long ibmvscsis_parse_command(struct scsi_info *vscsi,
62 struct viosrp_crq *crq);
64 static void ibmvscsis_adapter_idle(struct scsi_info *vscsi);
66 static void ibmvscsis_determine_resid(struct se_cmd *se_cmd,
69 u32 residual_count = se_cmd->residual_count;
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);
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);
98 * connection_broken() - Determine if the connection to the client is good
99 * @vscsi: Pointer to our adapter structure
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.
105 * EXECUTION ENVIRONMENT:
106 * Interrupt or Process environment
108 static bool connection_broken(struct scsi_info *vscsi)
110 struct viosrp_crq *crq;
111 u64 buffer[2] = { 0, 0 };
115 /* create a PING crq */
116 crq = (struct viosrp_crq *)&buffer;
117 crq->valid = VALID_CMD_RESP_EL;
118 crq->format = MESSAGE_IN_CRQ;
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]));
125 pr_debug("connection_broken: rc %ld\n", h_return_code);
127 if (h_return_code == H_CLOSED)
134 * ibmvscsis_unregister_command_q() - Helper Function-Unregister Command Queue
135 * @vscsi: Pointer to our adapter structure
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
145 * EXECUTION ENVIRONMENT:
148 static long ibmvscsis_unregister_command_q(struct scsi_info *vscsi)
151 long rc = ADAPT_SUCCESS;
155 qrc = h_free_crq(vscsi->dds.unit_id);
162 dev_err(&vscsi->dev, "unregister_command_q: error from h_free_crq %ld\n",
168 case H_LONG_BUSY_ORDER_1_MSEC:
169 /* msleep not good for small values */
170 usleep_range(1000, 2000);
173 case H_LONG_BUSY_ORDER_10_MSEC:
174 usleep_range(10000, 20000);
177 case H_LONG_BUSY_ORDER_100_MSEC:
181 case H_LONG_BUSY_ORDER_1_SEC:
185 case H_LONG_BUSY_ORDER_10_SEC:
189 case H_LONG_BUSY_ORDER_100_SEC:
194 dev_err(&vscsi->dev, "unregister_command_q: unknown error %ld from h_free_crq\n",
201 * dont wait more then 300 seconds
202 * ticks are in milliseconds more or less
204 if (ticks > 300000 && qrc != H_SUCCESS) {
206 dev_err(&vscsi->dev, "Excessive wait for h_free_crq\n");
208 } while (qrc != H_SUCCESS && rc == ADAPT_SUCCESS);
210 pr_debug("Freeing CRQ: phyp rc %ld, rc %ld\n", qrc, rc);
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
220 * Deletes information specific to the client when the client goes away
222 * EXECUTION ENVIRONMENT:
223 * Interrupt or Process
225 static void ibmvscsis_delete_client_info(struct scsi_info *vscsi,
228 vscsi->client_cap = 0;
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.
236 vscsi->client_data.os_type = 0;
240 * ibmvscsis_free_command_q() - Free Command Queue
241 * @vscsi: Pointer to our adapter structure
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.
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
255 * EXECUTION ENVIRONMENT:
256 * Process level, interrupt lock is held
258 static long ibmvscsis_free_command_q(struct scsi_info *vscsi)
261 u32 flags_under_lock;
262 u16 state_under_lock;
263 long rc = ADAPT_SUCCESS;
265 if (!(vscsi->flags & CRQ_CLOSED)) {
266 vio_disable_interrupts(vscsi->dma_dev);
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;
273 spin_unlock_bh(&vscsi->intr_lock);
274 rc = ibmvscsis_unregister_command_q(vscsi);
275 spin_lock_bh(&vscsi->intr_lock);
277 if (state_under_lock != vscsi->new_state)
278 vscsi->phyp_acr_state = vscsi->new_state;
280 vscsi->phyp_acr_flags = ((~flags_under_lock) & vscsi->flags);
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;
288 ibmvscsis_delete_client_info(vscsi, false);
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);
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
304 * Returns a pointer to a valid command element or NULL, if the command
307 * EXECUTION ENVIRONMENT:
308 * Interrupt environment, interrupt lock held
310 static struct viosrp_crq *ibmvscsis_cmd_q_dequeue(uint mask,
312 struct viosrp_crq *base_addr)
314 struct viosrp_crq *ptr;
316 ptr = base_addr + *current_index;
319 *current_index = (*current_index + 1) & mask;
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
333 * EXECUTION ENVIRONMENT:
334 * Interrupt environment interrupt lock held
336 static long ibmvscsis_send_init_message(struct scsi_info *vscsi, u8 format)
338 struct viosrp_crq *crq;
339 u64 buffer[2] = { 0, 0 };
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]));
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.
357 * Checks if an initialize message was queued by the initiatior
358 * after the queue was created and before the interrupt was enabled.
360 * EXECUTION ENVIRONMENT:
361 * Process level only, interrupt lock held
363 static long ibmvscsis_check_init_msg(struct scsi_info *vscsi, uint *format)
365 struct viosrp_crq *crq;
366 long rc = ADAPT_SUCCESS;
368 crq = ibmvscsis_cmd_q_dequeue(vscsi->cmd_q.mask, &vscsi->cmd_q.index,
369 vscsi->cmd_q.base_addr);
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;
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.
382 crq = ibmvscsis_cmd_q_dequeue(vscsi->cmd_q.mask,
384 vscsi->cmd_q.base_addr);
386 *format = (uint)(crq->format);
388 crq->valid = INVALIDATE_CMD_RESP_EL;
392 *format = (uint)(crq->format);
394 crq->valid = INVALIDATE_CMD_RESP_EL;
402 * ibmvscsis_disconnect() - Helper function to disconnect
403 * @work: Pointer to work_struct, gives access to our adapter structure
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.
411 * EXECUTION ENVIRONMENT:
412 * Process environment
414 static void ibmvscsis_disconnect(struct work_struct *work)
416 struct scsi_info *vscsi = container_of(work, struct scsi_info,
419 bool wait_idle = false;
421 spin_lock_bh(&vscsi->intr_lock);
422 new_state = vscsi->new_state;
423 vscsi->new_state = 0;
425 pr_debug("disconnect: flags 0x%x, state 0x%hx\n", vscsi->flags,
429 * check which state we are in and see if we
430 * should transitition to the new state
432 switch (vscsi->state) {
433 /* Should never be called while in this state. */
436 * Can never transition from this state;
437 * igonore errors and logout.
442 /* can transition from this state to UNCONFIGURING */
444 if (new_state == UNCONFIGURING)
445 vscsi->state = new_state;
449 * Can transition from this state to to unconfiguring
452 case ERR_DISCONNECT_RECONNECT:
456 vscsi->state = new_state;
466 /* can transition from this state to UNCONFIGURING */
467 case ERR_DISCONNECTED:
468 if (new_state == UNCONFIGURING)
469 vscsi->state = new_state;
475 vscsi->state = new_state;
476 vscsi->flags |= RESPONSE_Q_DOWN;
477 vscsi->flags &= ~(SCHEDULE_DISCONNECT |
478 DISCONNECT_SCHEDULED);
480 if (vscsi->flags & CFG_SLEEPING) {
481 vscsi->flags &= ~CFG_SLEEPING;
482 complete(&vscsi->unconfig);
486 /* should never happen */
488 case ERR_DISCONNECT_RECONNECT:
490 dev_err(&vscsi->dev, "disconnect: invalid state %d for WAIT_IDLE\n",
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);
506 case ERR_DISCONNECT_RECONNECT:
507 vscsi->state = new_state;
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.
520 case WAIT_CONNECTION:
524 vscsi->state = new_state;
527 /* can transition from this state to UNCONFIGURING */
529 if (new_state == UNCONFIGURING)
530 vscsi->state = new_state;
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);
545 * This routine is can not be called with the interrupt
548 spin_unlock_bh(&vscsi->intr_lock);
549 wait_for_completion(&vscsi->wait_idle);
550 spin_lock_bh(&vscsi->intr_lock);
552 pr_debug("disconnect stop wait\n");
554 ibmvscsis_adapter_idle(vscsi);
557 spin_unlock_bh(&vscsi->intr_lock);
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
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
571 * interrupt lock is held
573 static void ibmvscsis_post_disconnect(struct scsi_info *vscsi, uint new_state,
578 /* check the validity of the new state */
582 case ERR_DISCONNECT_RECONNECT:
587 dev_err(&vscsi->dev, "post_disconnect: Invalid new state %d\n",
592 vscsi->flags |= flag_bits;
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);
597 if (!(vscsi->flags & (DISCONNECT_SCHEDULED | SCHEDULE_DISCONNECT))) {
598 vscsi->flags |= SCHEDULE_DISCONNECT;
599 vscsi->new_state = new_state;
601 INIT_WORK(&vscsi->proc_work, ibmvscsis_disconnect);
602 (void)queue_work(vscsi->work_q, &vscsi->proc_work);
604 if (vscsi->new_state)
605 state = vscsi->new_state;
607 state = vscsi->state;
614 case ERR_DISCONNECTED:
617 if (new_state == UNCONFIGURING)
618 vscsi->new_state = new_state;
621 case ERR_DISCONNECT_RECONNECT:
625 vscsi->new_state = new_state;
634 case WAIT_CONNECTION:
637 vscsi->new_state = new_state;
645 pr_debug("Leaving post_disconnect: flags 0x%x, new_state 0x%x\n",
646 vscsi->flags, vscsi->new_state);
650 * ibmvscsis_handle_init_compl_msg() - Respond to an Init Complete Message
651 * @vscsi: Pointer to our adapter structure
653 * Must be called with interrupt lock held.
655 static long ibmvscsis_handle_init_compl_msg(struct scsi_info *vscsi)
657 long rc = ADAPT_SUCCESS;
659 switch (vscsi->state) {
662 case ERR_DISCONNECT_RECONNECT:
663 case ERR_DISCONNECTED:
669 case WAIT_CONNECTION:
670 vscsi->state = CONNECTED;
679 dev_err(&vscsi->dev, "init_msg: invalid state %d to get init compl msg\n",
681 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
689 * ibmvscsis_handle_init_msg() - Respond to an Init Message
690 * @vscsi: Pointer to our adapter structure
692 * Must be called with interrupt lock held.
694 static long ibmvscsis_handle_init_msg(struct scsi_info *vscsi)
696 long rc = ADAPT_SUCCESS;
698 switch (vscsi->state) {
699 case WAIT_CONNECTION:
700 rc = ibmvscsis_send_init_message(vscsi, INIT_COMPLETE_MSG);
703 vscsi->state = CONNECTED;
707 dev_err(&vscsi->dev, "init_msg: failed to send, rc %ld\n",
709 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
713 dev_err(&vscsi->dev, "init_msg: failed to send, rc %ld\n",
716 ibmvscsis_post_disconnect(vscsi,
717 ERR_DISCONNECT_RECONNECT, 0);
721 pr_warn("init_msg: failed to send, rc %ld\n", rc);
740 case ERR_DISCONNECT_RECONNECT:
741 case ERR_DISCONNECTED:
744 dev_err(&vscsi->dev, "init_msg: invalid state %d to get init msg\n",
746 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
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
758 * EXECUTION ENVIRONMENT:
759 * Interrupt, interrupt lock held
761 static long ibmvscsis_init_msg(struct scsi_info *vscsi, struct viosrp_crq *crq)
763 long rc = ADAPT_SUCCESS;
765 pr_debug("init_msg: state 0x%hx\n", vscsi->state);
767 rc = h_vioctl(vscsi->dds.unit_id, H_GET_PARTNER_INFO,
768 (u64)vscsi->map_ioba | ((u64)PAGE_SIZE << 32), 0, 0, 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);
776 pr_debug("init_msg h_vioctl rc %ld\n", rc);
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);
786 dev_err(&vscsi->dev, "init_msg: invalid format %d\n",
788 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
795 * ibmvscsis_establish_new_q() - Establish new CRQ queue
796 * @vscsi: Pointer to our adapter structure
798 * Must be called with interrupt lock held.
800 static long ibmvscsis_establish_new_q(struct scsi_info *vscsi)
802 long rc = ADAPT_SUCCESS;
805 vscsi->flags &= PRESERVE_FLAG_FIELDS;
806 vscsi->rsp_q_timer.timer_pops = 0;
810 rc = vio_enable_interrupts(vscsi->dma_dev);
812 pr_warn("establish_new_q: failed to enable interrupts, rc %ld\n",
817 rc = ibmvscsis_check_init_msg(vscsi, &format);
819 dev_err(&vscsi->dev, "establish_new_q: check_init_msg failed, rc %ld\n",
824 if (format == UNUSED_FORMAT) {
825 rc = ibmvscsis_send_init_message(vscsi, INIT_MSG);
838 vscsi->state = UNDEFINED;
842 } else if (format == INIT_MSG) {
843 rc = ibmvscsis_handle_init_msg(vscsi);
850 * ibmvscsis_reset_queue() - Reset CRQ Queue
851 * @vscsi: Pointer to our adapter structure
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.
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
865 * EXECUTION ENVIRONMENT:
866 * Process environment, called with interrupt lock held
868 static void ibmvscsis_reset_queue(struct scsi_info *vscsi)
871 long rc = ADAPT_SUCCESS;
873 pr_debug("reset_queue: flags 0x%x\n", vscsi->flags);
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;
881 vscsi->state = WAIT_CONNECTION;
882 vio_enable_interrupts(vscsi->dma_dev);
884 rc = ibmvscsis_free_command_q(vscsi);
885 if (rc == ADAPT_SUCCESS) {
886 vscsi->state = WAIT_CONNECTION;
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);
895 if (rc != ADAPT_SUCCESS) {
896 pr_debug("reset_queue: reg_crq rc %ld\n", rc);
898 vscsi->state = ERR_DISCONNECTED;
899 vscsi->flags |= RESPONSE_Q_DOWN;
900 ibmvscsis_free_command_q(vscsi);
903 vscsi->state = ERR_DISCONNECTED;
904 vscsi->flags |= RESPONSE_Q_DOWN;
910 * ibmvscsis_free_cmd_resources() - Free command resources
911 * @vscsi: Pointer to our adapter structure
912 * @cmd: Command which is not longer in use
914 * Must be called with interrupt lock held.
916 static void ibmvscsis_free_cmd_resources(struct scsi_info *vscsi,
917 struct ibmvscsis_cmd *cmd)
919 struct iu_entry *iue = cmd->iue;
922 case TASK_MANAGEMENT:
925 * When the queue goes down this value is cleared, so it
926 * cannot be cleared in this general purpose function.
932 vscsi->flags &= ~PROCESSING_MAD;
937 dev_err(&vscsi->dev, "free_cmd_resources unknown type %d\n",
943 list_add_tail(&cmd->list, &vscsi->free_cmd);
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);
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
958 * Do the logic to close the I_T nexus. This function may not
959 * behave to specification.
961 * EXECUTION ENVIRONMENT:
962 * Interrupt, interrupt lock held
964 static long ibmvscsis_trans_event(struct scsi_info *vscsi,
965 struct viosrp_crq *crq)
967 long rc = ADAPT_SUCCESS;
969 pr_debug("trans_event: format %d, flags 0x%x, state 0x%hx\n",
970 (int)crq->format, vscsi->flags, vscsi->state);
972 switch (crq->format) {
975 case PARTNER_DEREGISTER:
976 ibmvscsis_delete_client_info(vscsi, true);
981 dev_err(&vscsi->dev, "trans_event: invalid format %d\n",
983 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT,
988 if (rc == ADAPT_SUCCESS) {
989 switch (vscsi->state) {
991 case ERR_DISCONNECTED:
996 vscsi->flags |= (RESPONSE_Q_DOWN | TRANS_EVENT);
1002 case WAIT_CONNECTION:
1006 ibmvscsis_post_disconnect(vscsi, WAIT_IDLE,
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",
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");
1023 pr_debug("trans_event: SRP Processing, but no outstanding work\n");
1026 ibmvscsis_post_disconnect(vscsi, WAIT_IDLE,
1031 case ERR_DISCONNECT:
1032 case ERR_DISCONNECT_RECONNECT:
1034 vscsi->flags |= (RESPONSE_Q_DOWN | TRANS_EVENT);
1039 rc = vscsi->flags & SCHEDULE_DISCONNECT;
1041 pr_debug("Leaving trans_event: flags 0x%x, state 0x%hx, rc %ld\n",
1042 vscsi->flags, vscsi->state, rc);
1048 * ibmvscsis_poll_cmd_q() - Poll Command Queue
1049 * @vscsi: Pointer to our adapter structure
1051 * Called to handle command elements that may have arrived while
1052 * interrupts were disabled.
1054 * EXECUTION ENVIRONMENT:
1055 * intr_lock must be held
1057 static void ibmvscsis_poll_cmd_q(struct scsi_info *vscsi)
1059 struct viosrp_crq *crq;
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);
1067 rc = vscsi->flags & SCHEDULE_DISCONNECT;
1068 crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
1074 vscsi->cmd_q.index =
1075 (vscsi->cmd_q.index + 1) & vscsi->cmd_q.mask;
1078 rc = ibmvscsis_parse_command(vscsi, crq);
1080 if ((uint)crq->valid == VALID_TRANS_EVENT) {
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
1087 rc = ibmvscsis_trans_event(vscsi, crq);
1088 } else if (vscsi->flags & TRANS_EVENT) {
1090 * if a tranport event has occurred leave
1091 * everything but transport events on the queue
1093 pr_debug("poll_cmd_q, ignoring\n");
1096 * need to decrement the queue index so we can
1097 * look at the elment again
1099 if (vscsi->cmd_q.index)
1100 vscsi->cmd_q.index -= 1;
1103 * index is at 0 it just wrapped.
1104 * have it index last element in q
1106 vscsi->cmd_q.index = vscsi->cmd_q.mask;
1111 crq->valid = INVALIDATE_CMD_RESP_EL;
1113 crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
1120 vio_enable_interrupts(vscsi->dma_dev);
1122 pr_debug("poll_cmd_q, reenabling interrupts\n");
1130 pr_debug("Leaving poll_cmd_q: rc %ld\n", rc);
1134 * ibmvscsis_free_cmd_qs() - Free elements in queue
1135 * @vscsi: Pointer to our adapter structure
1137 * Free all of the elements on all queues that are waiting for
1141 * Called with interrupt lock held
1143 static void ibmvscsis_free_cmd_qs(struct scsi_info *vscsi)
1145 struct ibmvscsis_cmd *cmd, *nxt;
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);
1151 list_for_each_entry_safe(cmd, nxt, &vscsi->waiting_rsp, list) {
1152 list_del(&cmd->list);
1153 ibmvscsis_free_cmd_resources(vscsi, cmd);
1158 * ibmvscsis_get_free_cmd() - Get free command from list
1159 * @vscsi: Pointer to our adapter structure
1161 * Must be called with interrupt lock held.
1163 static struct ibmvscsis_cmd *ibmvscsis_get_free_cmd(struct scsi_info *vscsi)
1165 struct ibmvscsis_cmd *cmd = NULL;
1166 struct iu_entry *iue;
1168 iue = srp_iu_get(&vscsi->target);
1170 cmd = list_first_entry_or_null(&vscsi->free_cmd,
1171 struct ibmvscsis_cmd, list);
1173 list_del(&cmd->list);
1175 cmd->type = UNSET_TYPE;
1176 memset(&cmd->se_cmd, 0, sizeof(cmd->se_cmd));
1186 * ibmvscsis_adapter_idle() - Helper function to handle idle adapter
1187 * @vscsi: Pointer to our adapter structure
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.
1195 * EXECUTION ENVIRONMENT:
1196 * Process environment called with interrupt lock held
1198 static void ibmvscsis_adapter_idle(struct scsi_info *vscsi)
1200 int free_qs = false;
1202 pr_debug("adapter_idle: flags 0x%x, state 0x%hx\n", vscsi->flags,
1205 /* Only need to free qs if we're disconnecting from client */
1206 if (vscsi->state != WAIT_CONNECTION || vscsi->flags & TRANS_EVENT)
1209 switch (vscsi->state) {
1211 ibmvscsis_free_command_q(vscsi);
1214 if (vscsi->flags & CFG_SLEEPING) {
1215 vscsi->flags &= ~CFG_SLEEPING;
1216 complete(&vscsi->unconfig);
1219 case ERR_DISCONNECT_RECONNECT:
1220 ibmvscsis_reset_queue(vscsi);
1221 pr_debug("adapter_idle, disc_rec: flags 0x%x\n", vscsi->flags);
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;
1231 vscsi->state = WAIT_ENABLED;
1232 pr_debug("adapter_idle, disc: flags 0x%x, state 0x%hx\n",
1233 vscsi->flags, vscsi->state);
1237 vscsi->rsp_q_timer.timer_pops = 0;
1240 if (vscsi->flags & TRANS_EVENT) {
1241 vscsi->state = WAIT_CONNECTION;
1242 vscsi->flags &= PRESERVE_FLAG_FIELDS;
1244 vscsi->state = CONNECTED;
1245 vscsi->flags &= ~DISCONNECT_SCHEDULED;
1248 pr_debug("adapter_idle, wait: flags 0x%x, state 0x%hx\n",
1249 vscsi->flags, vscsi->state);
1250 ibmvscsis_poll_cmd_q(vscsi);
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);
1260 dev_err(&vscsi->dev, "adapter_idle: in invalid state %d\n",
1266 ibmvscsis_free_cmd_qs(vscsi);
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
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
1286 if (vscsi->phyp_acr_state != 0) {
1288 * set any bits in flags that may have been cleared by
1289 * a call to free command queue in switch statement
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;
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);
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);
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
1312 * Copy the srp information unit from the hosted
1313 * partition using remote dma
1315 * EXECUTION ENVIRONMENT:
1316 * Interrupt, interrupt lock held
1318 static long ibmvscsis_copy_crq_packet(struct scsi_info *vscsi,
1319 struct ibmvscsis_cmd *cmd,
1320 struct viosrp_crq *crq)
1322 struct iu_entry *iue = cmd->iue;
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;
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);
1339 cmd->init_time = mftb();
1340 iue->remote_token = crq->IU_data_ptr;
1342 pr_debug("copy_crq: ioba 0x%llx, init_time 0x%llx\n",
1343 be64_to_cpu(crq->IU_data_ptr), cmd->init_time);
1346 if (connection_broken(vscsi))
1347 ibmvscsis_post_disconnect(vscsi,
1348 ERR_DISCONNECT_RECONNECT,
1352 ibmvscsis_post_disconnect(vscsi,
1353 ERR_DISCONNECT_RECONNECT, 0);
1355 dev_err(&vscsi->dev, "copy_crq: h_copy_rdma failed, rc %ld\n",
1361 dev_err(&vscsi->dev, "copy_crq: h_copy_rdma failed, rc %ld\n",
1363 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
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
1375 * EXECUTION ENVIRONMENT:
1376 * Interrupt adapter lock is held
1378 static long ibmvscsis_adapter_info(struct scsi_info *vscsi,
1379 struct iu_entry *iue)
1381 struct viosrp_adapter_info *mad = &vio_iu(iue)->mad.adapter_info;
1382 struct mad_adapter_info_data *info;
1387 mad->common.status = cpu_to_be16(VIOSRP_MAD_SUCCESS);
1389 if (be16_to_cpu(mad->common.length) > sizeof(*info)) {
1390 mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
1394 info = dma_alloc_coherent(&vscsi->dma_dev->dev, sizeof(*info), &token,
1397 dev_err(&vscsi->dev, "bad dma_alloc_coherent %p\n",
1399 mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
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);
1409 if (rc != H_SUCCESS) {
1410 if (rc == H_PERMISSION) {
1411 if (connection_broken(vscsi))
1412 flag_bits = (RESPONSE_Q_DOWN | CLIENT_FAILED);
1414 pr_warn("adapter_info: h_copy_rdma from client failed, rc %ld\n",
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,
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).
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);
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);
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));
1460 if (connection_broken(vscsi))
1461 flag_bits = (RESPONSE_Q_DOWN | CLIENT_FAILED);
1463 dev_err(&vscsi->dev, "adapter_info: h_copy_rdma to client failed, rc %ld\n",
1465 ibmvscsis_post_disconnect(vscsi,
1466 ERR_DISCONNECT_RECONNECT,
1472 dma_free_coherent(&vscsi->dma_dev->dev, sizeof(*info), info, token);
1473 pr_debug("Leaving adapter_info, rc %ld\n", rc);
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
1483 * NOTE: if you return an error from this routine you must be
1484 * disconnecting or you will cause a hang
1486 * EXECUTION ENVIRONMENT:
1487 * Interrupt called with adapter lock held
1489 static int ibmvscsis_cap_mad(struct scsi_info *vscsi, struct iu_entry *iue)
1491 struct viosrp_capabilities *mad = &vio_iu(iue)->mad.capabilities;
1492 struct capabilities *cap;
1493 struct mad_capability_common *common;
1495 u16 olen, len, status, min_len, cap_len;
1500 olen = be16_to_cpu(mad->common.length);
1502 * struct capabilities hardcodes a couple capabilities after the
1503 * header, but the capabilities can actually be in any order.
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);
1512 cap = dma_alloc_coherent(&vscsi->dma_dev->dev, olen, &token,
1515 dev_err(&vscsi->dev, "bad dma_alloc_coherent %p\n",
1517 mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
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),
1527 len = olen - min_len;
1528 status = VIOSRP_MAD_SUCCESS;
1529 common = (struct mad_capability_common *)&cap->migration;
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));
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;
1544 dev_err(&vscsi->dev, "cap_mad: cap len is 0\n");
1545 status = VIOSRP_MAD_FAILED;
1549 switch (common->cap_type) {
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;
1558 len = len - cap_len;
1559 common = (struct mad_capability_common *)
1560 ((char *)common + cap_len);
1563 mad->common.status = cpu_to_be16(status);
1566 rc = h_copy_rdma(olen, vscsi->dds.window[LOCAL].liobn, token,
1567 vscsi->dds.window[REMOTE].liobn,
1568 be64_to_cpu(mad->buffer));
1570 if (rc != H_SUCCESS) {
1571 pr_debug("cap_mad: failed to copy to client, rc %ld\n",
1574 if (rc == H_PERMISSION) {
1575 if (connection_broken(vscsi))
1576 flag_bits = (RESPONSE_Q_DOWN |
1580 pr_warn("cap_mad: error copying data to client, rc %ld\n",
1582 ibmvscsis_post_disconnect(vscsi,
1583 ERR_DISCONNECT_RECONNECT,
1588 dma_free_coherent(&vscsi->dma_dev->dev, olen, cap, token);
1590 pr_debug("Leaving cap_mad, rc %ld, client_cap 0x%x\n",
1591 rc, vscsi->client_cap);
1597 * ibmvscsis_process_mad() - Service a MAnagement Data gram
1598 * @vscsi: Pointer to our adapter structure
1599 * @iue: Information Unit containing the MAD request
1601 * Must be called with interrupt lock held.
1603 static long ibmvscsis_process_mad(struct scsi_info *vscsi, struct iu_entry *iue)
1605 struct mad_common *mad = (struct mad_common *)&vio_iu(iue)->mad;
1606 struct viosrp_empty_iu *empty;
1607 long rc = ADAPT_SUCCESS;
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);
1616 case VIOSRP_ADAPTER_INFO_TYPE:
1617 rc = ibmvscsis_adapter_info(vscsi, iue);
1619 case VIOSRP_CAPABILITIES_TYPE:
1620 rc = ibmvscsis_cap_mad(vscsi, iue);
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);
1627 pr_warn("fast fail mad sent after login\n");
1628 mad->status = cpu_to_be16(VIOSRP_MAD_FAILED);
1632 mad->status = cpu_to_be16(VIOSRP_MAD_NOT_SUPPORTED);
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
1644 * Must be called with interrupt lock held.
1646 static void srp_snd_msg_failed(struct scsi_info *vscsi, long rc)
1650 if (rc != H_DROPPED) {
1651 ibmvscsis_free_cmd_qs(vscsi);
1654 vscsi->flags |= CLIENT_FAILED;
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);
1665 ibmvscsis_post_disconnect(vscsi,
1666 ERR_DISCONNECT_RECONNECT, 0);
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
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);
1692 * Check if the timer is running; if it
1693 * is not then start it up.
1695 if (!vscsi->rsp_q_timer.started) {
1696 if (vscsi->rsp_q_timer.timer_pops <
1698 kt = WAIT_NANO_SECONDS;
1701 * slide the timeslice if the maximum
1702 * timer pops have already happened
1704 kt = ktime_set(WAIT_SECONDS, 0);
1707 vscsi->rsp_q_timer.started = true;
1708 hrtimer_start(&vscsi->rsp_q_timer.timer, kt,
1713 * TBD: Do we need to worry about this? Need to get
1717 * waited a long time and it appears the system admin
1718 * is bring this driver down
1720 vscsi->flags |= RESPONSE_Q_DOWN;
1721 ibmvscsis_free_cmd_qs(vscsi);
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
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,
1739 * ibmvscsis_send_messages() - Send a Response
1740 * @vscsi: Pointer to our adapter structure
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.
1747 * Called with interrupt lock held
1749 static void ibmvscsis_send_messages(struct scsi_info *vscsi)
1752 /* note do not attmempt to access the IU_data_ptr with this pointer
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;
1760 if (!(vscsi->flags & RESPONSE_Q_DOWN)) {
1761 list_for_each_entry_safe(cmd, nxt, &vscsi->waiting_rsp, list) {
1764 crq->valid = VALID_CMD_RESP_EL;
1765 crq->format = cmd->rsp.format;
1767 if (cmd->flags & CMD_FAST_FAIL)
1768 crq->status = VIOSRP_ADAPTER_FAIL;
1770 crq->IU_length = cpu_to_be16(cmd->rsp.len);
1772 rc = h_send_crq(vscsi->dma_dev->unit_address,
1773 be64_to_cpu(msg_hi),
1774 be64_to_cpu(cmd->rsp.tag));
1776 pr_debug("send_messages: cmd %p, tag 0x%llx, rc %ld\n",
1777 cmd, be64_to_cpu(cmd->rsp.tag), rc);
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);
1785 ibmvscsis_free_cmd_resources(vscsi, cmd);
1787 srp_snd_msg_failed(vscsi, rc);
1794 * The timer could pop with the queue empty. If
1795 * this happens, rc will always indicate a
1796 * success; clear the pop count.
1798 vscsi->rsp_q_timer.timer_pops = 0;
1801 ibmvscsis_free_cmd_qs(vscsi);
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)
1810 struct iu_entry *iue = cmd->iue;
1811 struct mad_common *mad = (struct mad_common *)&vio_iu(iue)->mad;
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));
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);
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);
1832 dev_err(&vscsi->dev, "mad: failed to copy to client, rc %ld\n",
1835 ibmvscsis_free_cmd_resources(vscsi, cmd);
1836 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
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
1846 * EXECUTION ENVIRONMENT:
1847 * Interrupt, called with adapter lock held
1849 static long ibmvscsis_mad(struct scsi_info *vscsi, struct viosrp_crq *crq)
1851 struct iu_entry *iue;
1852 struct ibmvscsis_cmd *cmd;
1853 struct mad_common *mad;
1854 long rc = ADAPT_SUCCESS;
1856 switch (vscsi->state) {
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.
1862 case WAIT_CONNECTION:
1863 pr_debug("mad: in Wait Connection state, ignoring MAD, flags %d\n",
1865 return ADAPT_SUCCESS;
1867 case SRP_PROCESSING:
1872 * We should never get here while we're in these states.
1873 * Just log an error and get out.
1877 case ERR_DISCONNECT:
1878 case ERR_DISCONNECT_RECONNECT:
1880 dev_err(&vscsi->dev, "mad: invalid adapter state %d for mad\n",
1882 return ADAPT_SUCCESS;
1885 cmd = ibmvscsis_get_free_cmd(vscsi);
1887 dev_err(&vscsi->dev, "mad: failed to get cmd, debit %d\n",
1889 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
1893 cmd->type = ADAPTER_MAD;
1895 rc = ibmvscsis_copy_crq_packet(vscsi, cmd, crq);
1897 mad = (struct mad_common *)&vio_iu(iue)->mad;
1899 pr_debug("mad: type %d\n", be32_to_cpu(mad->type));
1901 rc = ibmvscsis_process_mad(vscsi, iue);
1903 pr_debug("mad: status %hd, rc %ld\n", be16_to_cpu(mad->status),
1907 ibmvscsis_send_mad_resp(vscsi, cmd, crq);
1909 ibmvscsis_free_cmd_resources(vscsi, cmd);
1912 pr_debug("Leaving mad, rc %ld\n", rc);
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
1921 * EXECUTION ENVIRONMENT:
1922 * Interrupt, interrupt lock held
1924 static long ibmvscsis_login_rsp(struct scsi_info *vscsi,
1925 struct ibmvscsis_cmd *cmd)
1927 struct iu_entry *iue = cmd->iue;
1928 struct srp_login_rsp *rsp = &vio_iu(iue)->srp.login_rsp;
1929 struct format_code *fmt;
1931 long rc = ADAPT_SUCCESS;
1933 memset(rsp, 0, sizeof(struct srp_login_rsp));
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;
1944 cmd->rsp.len = sizeof(struct srp_login_rsp);
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));
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",
1960 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
1966 dev_err(&vscsi->dev, "login_rsp: error copying to client, rc %ld\n",
1968 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
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
1981 * EXECUTION ENVIRONMENT:
1982 * Interrupt, interrupt lock held
1984 static long ibmvscsis_srp_login_rej(struct scsi_info *vscsi,
1985 struct ibmvscsis_cmd *cmd, u32 reason)
1987 struct iu_entry *iue = cmd->iue;
1988 struct srp_login_rej *rej = &vio_iu(iue)->srp.login_rej;
1989 struct format_code *fmt;
1991 long rc = ADAPT_SUCCESS;
1993 memset(rej, 0, sizeof(*rej));
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;
2001 cmd->rsp.len = sizeof(*rej);
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));
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",
2016 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
2022 dev_err(&vscsi->dev, "login_rej: error copying to client, rc %ld\n",
2024 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2031 static int ibmvscsis_make_nexus(struct ibmvscsis_tport *tport)
2033 char *name = tport->tport_name;
2034 struct ibmvscsis_nexus *nexus;
2037 if (tport->ibmv_nexus) {
2038 pr_debug("tport->ibmv_nexus already exists\n");
2042 nexus = kzalloc(sizeof(*nexus), GFP_KERNEL);
2044 pr_err("Unable to allocate struct ibmvscsis_nexus\n");
2048 nexus->se_sess = target_alloc_session(&tport->se_tpg, 0, 0,
2049 TARGET_PROT_NORMAL, name, nexus,
2051 if (IS_ERR(nexus->se_sess)) {
2052 rc = PTR_ERR(nexus->se_sess);
2053 goto transport_init_fail;
2056 tport->ibmv_nexus = nexus;
2060 transport_init_fail:
2065 static int ibmvscsis_drop_nexus(struct ibmvscsis_tport *tport)
2067 struct se_session *se_sess;
2068 struct ibmvscsis_nexus *nexus;
2070 nexus = tport->ibmv_nexus;
2074 se_sess = nexus->se_sess;
2079 * Release the SCSI I_T Nexus to the emulated ibmvscsis Target Port
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;
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
2096 * EXECUTION ENVIRONMENT:
2097 * Interrupt, called with interrupt lock held
2099 static long ibmvscsis_srp_login(struct scsi_info *vscsi,
2100 struct ibmvscsis_cmd *cmd,
2101 struct viosrp_crq *crq)
2103 struct iu_entry *iue = cmd->iue;
2104 struct srp_login_req *req = &vio_iu(iue)->srp.login_req;
2106 __be64 id_extension;
2109 struct format_code *fmt;
2111 long rc = ADAPT_SUCCESS;
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;
2130 if (vscsi->state == SRP_PROCESSING)
2131 reason = SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED;
2133 rc = ibmvscsis_make_nexus(&vscsi->tport);
2135 reason = SRP_LOGIN_REJ_UNABLE_ESTABLISH_CHANNEL;
2137 cmd->rsp.format = VIOSRP_SRP_FORMAT;
2138 cmd->rsp.tag = req->tag;
2140 pr_debug("srp_login: reason 0x%x\n", reason);
2143 rc = ibmvscsis_srp_login_rej(vscsi, cmd, reason);
2145 rc = ibmvscsis_login_rsp(vscsi, cmd);
2149 vscsi->state = SRP_PROCESSING;
2151 list_add_tail(&cmd->list, &vscsi->waiting_rsp);
2152 ibmvscsis_send_messages(vscsi);
2154 ibmvscsis_free_cmd_resources(vscsi, cmd);
2157 pr_debug("Leaving srp_login, rc %ld\n", rc);
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
2167 * Do the logic to close the I_T nexus. This function may not
2168 * behave to specification.
2170 * EXECUTION ENVIRONMENT:
2171 * Interrupt, interrupt lock held
2173 static long ibmvscsis_srp_i_logout(struct scsi_info *vscsi,
2174 struct ibmvscsis_cmd *cmd,
2175 struct viosrp_crq *crq)
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;
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);
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);
2192 ibmvscsis_post_disconnect(vscsi, WAIT_IDLE, 0);
2198 /* Called with intr lock held */
2199 static void ibmvscsis_srp_cmd(struct scsi_info *vscsi, struct viosrp_crq *crq)
2201 struct ibmvscsis_cmd *cmd;
2202 struct iu_entry *iue;
2203 struct srp_cmd *srp;
2204 struct srp_tsk_mgmt *tsk;
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);
2215 cmd = ibmvscsis_get_free_cmd(vscsi);
2217 dev_err(&vscsi->dev, "srp_cmd failed to get cmd, debit %d\n",
2219 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2223 srp = &vio_iu(iue)->srp.cmd;
2225 rc = ibmvscsis_copy_crq_packet(vscsi, cmd, crq);
2227 ibmvscsis_free_cmd_resources(vscsi, cmd);
2231 if (vscsi->state == SRP_PROCESSING) {
2232 switch (srp->opcode) {
2234 rc = ibmvscsis_srp_login(vscsi, cmd, crq);
2238 tsk = &vio_iu(iue)->srp.tsk_mgmt;
2239 pr_debug("tsk_mgmt tag: %llu (0x%llx)\n", tsk->tag,
2241 cmd->rsp.tag = tsk->tag;
2243 cmd->type = TASK_MANAGEMENT;
2244 list_add_tail(&cmd->list, &vscsi->schedule_q);
2245 queue_work(vscsi->work_q, &cmd->work);
2249 pr_debug("srp_cmd tag: %llu (0x%llx)\n", srp->tag,
2251 cmd->rsp.tag = srp->tag;
2253 cmd->type = SCSI_CDB;
2255 * We want to keep track of work waiting for
2258 list_add_tail(&cmd->list, &vscsi->schedule_q);
2259 queue_work(vscsi->work_q, &cmd->work);
2263 rc = ibmvscsis_srp_i_logout(vscsi, cmd, crq);
2269 ibmvscsis_free_cmd_resources(vscsi, cmd);
2270 dev_err(&vscsi->dev, "invalid srp cmd, opcode %d\n",
2272 ibmvscsis_post_disconnect(vscsi,
2273 ERR_DISCONNECT_RECONNECT, 0);
2276 } else if (srp->opcode == SRP_LOGIN_REQ && vscsi->state == CONNECTED) {
2277 rc = ibmvscsis_srp_login(vscsi, cmd, crq);
2279 ibmvscsis_free_cmd_resources(vscsi, cmd);
2280 dev_err(&vscsi->dev, "Invalid state %d to handle srp cmd\n",
2282 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2287 * ibmvscsis_ping_response() - Respond to a ping request
2288 * @vscsi: Pointer to our adapter structure
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.
2296 * EXECUTION ENVIRONMENT:
2297 * Interrupt, interrupt lock held
2299 static long ibmvscsis_ping_response(struct scsi_info *vscsi)
2301 struct viosrp_crq *crq;
2302 u64 buffer[2] = { 0, 0 };
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;
2310 rc = h_send_crq(vscsi->dds.unit_id, cpu_to_be64(buffer[MSG_HI]),
2311 cpu_to_be64(buffer[MSG_LOW]));
2317 vscsi->flags |= CLIENT_FAILED;
2319 vscsi->flags |= RESPONSE_Q_DOWN;
2321 dev_err(&vscsi->dev, "ping_response: h_send_crq failed, rc %ld\n",
2323 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2326 dev_err(&vscsi->dev, "ping_response: h_send_crq returned unknown rc %ld\n",
2328 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
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
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.
2344 * EXECUTION ENVIRONMENT:
2345 * Interrupt, intr lock held
2347 static long ibmvscsis_parse_command(struct scsi_info *vscsi,
2348 struct viosrp_crq *crq)
2350 long rc = ADAPT_SUCCESS;
2352 switch (crq->valid) {
2353 case VALID_CMD_RESP_EL:
2354 switch (crq->format) {
2359 if (vscsi->flags & PROCESSING_MAD) {
2361 dev_err(&vscsi->dev, "parse_command: already processing mad\n");
2362 ibmvscsis_post_disconnect(vscsi,
2363 ERR_DISCONNECT_RECONNECT,
2366 vscsi->flags |= PROCESSING_MAD;
2367 rc = ibmvscsis_mad(vscsi, crq);
2372 ibmvscsis_srp_cmd(vscsi, crq);
2375 case MESSAGE_IN_CRQ:
2376 if (crq->status == PING)
2377 ibmvscsis_ping_response(vscsi);
2381 dev_err(&vscsi->dev, "parse_command: invalid format %d\n",
2383 ibmvscsis_post_disconnect(vscsi,
2384 ERR_DISCONNECT_RECONNECT, 0);
2389 case VALID_TRANS_EVENT:
2390 rc = ibmvscsis_trans_event(vscsi, crq);
2393 case VALID_INIT_MSG:
2394 rc = ibmvscsis_init_msg(vscsi, crq);
2398 dev_err(&vscsi->dev, "parse_command: invalid valid field %d\n",
2400 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2405 * Return only what the interrupt handler cares
2406 * about. Most errors we keep right on trucking.
2408 rc = vscsi->flags & SCHEDULE_DISCONNECT;
2413 static int read_dma_window(struct scsi_info *vscsi)
2415 struct vio_dev *vdev = vscsi->dma_dev;
2416 const __be32 *dma_window;
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.
2423 dma_window = (const __be32 *)vio_get_attribute(vdev,
2424 "ibm,my-dma-window",
2427 pr_err("Couldn't find ibm,my-dma-window property\n");
2431 vscsi->dds.window[LOCAL].liobn = be32_to_cpu(*dma_window);
2434 prop = (const __be32 *)vio_get_attribute(vdev, "ibm,#dma-address-cells",
2437 pr_warn("Couldn't find ibm,#dma-address-cells property\n");
2440 dma_window += be32_to_cpu(*prop);
2443 prop = (const __be32 *)vio_get_attribute(vdev, "ibm,#dma-size-cells",
2446 pr_warn("Couldn't find ibm,#dma-size-cells property\n");
2449 dma_window += be32_to_cpu(*prop);
2452 /* dma_window should point to the second window now */
2453 vscsi->dds.window[REMOTE].liobn = be32_to_cpu(*dma_window);
2458 static struct ibmvscsis_tport *ibmvscsis_lookup_port(const char *name)
2460 struct ibmvscsis_tport *tport = NULL;
2461 struct vio_dev *vdev;
2462 struct scsi_info *vscsi;
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;
2472 spin_unlock_bh(&ibmvscsis_dev_lock);
2478 * ibmvscsis_parse_cmd() - Parse SRP Command
2479 * @vscsi: Pointer to our adapter structure
2480 * @cmd: Pointer to command element with SRP command
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.
2485 * EXECUTION ENVIRONMENT:
2488 static void ibmvscsis_parse_cmd(struct scsi_info *vscsi,
2489 struct ibmvscsis_cmd *cmd)
2491 struct iu_entry *iue = cmd->iue;
2492 struct srp_cmd *srp = (struct srp_cmd *)iue->sbuf->buf;
2493 struct ibmvscsis_nexus *nexus;
2495 enum dma_data_direction dir;
2499 nexus = vscsi->tport.ibmv_nexus;
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.
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);
2517 if (srp_get_desc_table(srp, &dir, &data_len)) {
2518 dev_err(&vscsi->dev, "0x%llx: parsing SRP descriptor table failed.\n",
2523 cmd->rsp.sol_not = srp->sol_not;
2525 switch (srp->task_attr) {
2526 case SRP_SIMPLE_TASK:
2527 attr = TCM_SIMPLE_TAG;
2529 case SRP_ORDERED_TASK:
2530 attr = TCM_ORDERED_TAG;
2533 attr = TCM_HEAD_TAG;
2539 dev_err(&vscsi->dev, "Invalid task attribute %d\n",
2544 cmd->se_cmd.tag = be64_to_cpu(srp->tag);
2546 spin_lock_bh(&vscsi->intr_lock);
2547 list_add_tail(&cmd->list, &vscsi->active_q);
2548 spin_unlock_bh(&vscsi->intr_lock);
2550 srp->lun.scsi_lun[0] &= 0x3f;
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);
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);
2566 spin_lock_bh(&vscsi->intr_lock);
2567 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2568 spin_unlock_bh(&vscsi->intr_lock);
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
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
2580 * EXECUTION ENVIRONMENT:
2583 static void ibmvscsis_parse_task(struct scsi_info *vscsi,
2584 struct ibmvscsis_cmd *cmd)
2586 struct iu_entry *iue = cmd->iue;
2587 struct srp_tsk_mgmt *srp_tsk = &vio_iu(iue)->srp.tsk_mgmt;
2589 u64 tag_to_abort = 0;
2591 struct ibmvscsis_nexus *nexus;
2593 nexus = vscsi->tport.ibmv_nexus;
2595 cmd->rsp.sol_not = srp_tsk->sol_not;
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);
2602 case SRP_TSK_ABORT_TASK_SET:
2603 tcm_type = TMR_ABORT_TASK_SET;
2605 case SRP_TSK_CLEAR_TASK_SET:
2606 tcm_type = TMR_CLEAR_TASK_SET;
2608 case SRP_TSK_LUN_RESET:
2609 tcm_type = TMR_LUN_RESET;
2611 case SRP_TSK_CLEAR_ACA:
2612 tcm_type = TMR_CLEAR_ACA;
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;
2624 cmd->se_cmd.tag = be64_to_cpu(srp_tsk->tag);
2626 spin_lock_bh(&vscsi->intr_lock);
2627 list_add_tail(&cmd->list, &vscsi->active_q);
2628 spin_unlock_bh(&vscsi->intr_lock);
2630 srp_tsk->lun.scsi_lun[0] &= 0x3f;
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);
2638 dev_err(&vscsi->dev, "target_submit_tmr failed, rc %d\n",
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;
2649 transport_send_check_condition_and_sense(&cmd->se_cmd, 0, 0);
2652 static void ibmvscsis_scheduler(struct work_struct *work)
2654 struct ibmvscsis_cmd *cmd = container_of(work, struct ibmvscsis_cmd,
2656 struct scsi_info *vscsi = cmd->adapter;
2658 spin_lock_bh(&vscsi->intr_lock);
2660 /* Remove from schedule_q */
2661 list_del(&cmd->list);
2663 /* Don't submit cmd if we're disconnecting */
2664 if (vscsi->flags & (SCHEDULE_DISCONNECT | DISCONNECT_SCHEDULED)) {
2665 ibmvscsis_free_cmd_resources(vscsi, cmd);
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);
2675 spin_unlock_bh(&vscsi->intr_lock);
2679 spin_unlock_bh(&vscsi->intr_lock);
2681 switch (cmd->type) {
2683 ibmvscsis_parse_cmd(vscsi, cmd);
2685 case TASK_MANAGEMENT:
2686 ibmvscsis_parse_task(vscsi, cmd);
2689 dev_err(&vscsi->dev, "scheduler, invalid cmd type %d\n",
2691 spin_lock_bh(&vscsi->intr_lock);
2692 ibmvscsis_free_cmd_resources(vscsi, cmd);
2693 spin_unlock_bh(&vscsi->intr_lock);
2698 static int ibmvscsis_alloc_cmds(struct scsi_info *vscsi, int num)
2700 struct ibmvscsis_cmd *cmd;
2703 INIT_LIST_HEAD(&vscsi->free_cmd);
2704 vscsi->cmd_pool = kcalloc(num, sizeof(struct ibmvscsis_cmd),
2706 if (!vscsi->cmd_pool)
2709 for (i = 0, cmd = (struct ibmvscsis_cmd *)vscsi->cmd_pool; i < num;
2711 cmd->adapter = vscsi;
2712 INIT_WORK(&cmd->work, ibmvscsis_scheduler);
2713 list_add_tail(&cmd->list, &vscsi->free_cmd);
2719 static void ibmvscsis_free_cmds(struct scsi_info *vscsi)
2721 kfree(vscsi->cmd_pool);
2722 vscsi->cmd_pool = NULL;
2723 INIT_LIST_HEAD(&vscsi->free_cmd);
2727 * ibmvscsis_service_wait_q() - Service Waiting Queue
2728 * @timer: Pointer to timer which has expired
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.
2736 * EXECUTION ENVIRONMENT:
2737 * called at interrupt level
2739 static enum hrtimer_restart ibmvscsis_service_wait_q(struct hrtimer *timer)
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,
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);
2751 return HRTIMER_NORESTART;
2754 static long ibmvscsis_alloctimer(struct scsi_info *vscsi)
2756 struct timer_cb *p_timer;
2758 p_timer = &vscsi->rsp_q_timer;
2759 hrtimer_init(&p_timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2761 p_timer->timer.function = ibmvscsis_service_wait_q;
2762 p_timer->started = false;
2763 p_timer->timer_pops = 0;
2765 return ADAPT_SUCCESS;
2768 static void ibmvscsis_freetimer(struct scsi_info *vscsi)
2770 struct timer_cb *p_timer;
2772 p_timer = &vscsi->rsp_q_timer;
2774 (void)hrtimer_cancel(&p_timer->timer);
2776 p_timer->started = false;
2777 p_timer->timer_pops = 0;
2780 static irqreturn_t ibmvscsis_interrupt(int dummy, void *data)
2782 struct scsi_info *vscsi = data;
2784 vio_disable_interrupts(vscsi->dma_dev);
2785 tasklet_schedule(&vscsi->work_task);
2791 * ibmvscsis_enable_change_state() - Set new state based on enabled status
2792 * @vscsi: Pointer to our adapter structure
2794 * This function determines our new state now that we are enabled. This
2795 * may involve sending an Init Complete message to the client.
2797 * Must be called with interrupt lock held.
2799 static long ibmvscsis_enable_change_state(struct scsi_info *vscsi)
2802 long rc = ADAPT_SUCCESS;
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);
2811 if (rc != ADAPT_SUCCESS) {
2812 vscsi->state = ERR_DISCONNECTED;
2813 vscsi->flags |= RESPONSE_Q_DOWN;
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.
2825 * Allocates memory for command queue maps remote memory into an ioba
2826 * initializes the command response queue
2828 * EXECUTION ENVIRONMENT:
2829 * Process level only
2831 static long ibmvscsis_create_command_q(struct scsi_info *vscsi, int num_cmds)
2834 struct vio_dev *vdev = vscsi->dma_dev;
2836 /* We might support multiple pages in the future, but just 1 for now */
2839 vscsi->cmd_q.size = pages;
2841 vscsi->cmd_q.base_addr =
2842 (struct viosrp_crq *)get_zeroed_page(GFP_KERNEL);
2843 if (!vscsi->cmd_q.base_addr)
2846 vscsi->cmd_q.mask = ((uint)pages * CRQ_PER_PAGE) - 1;
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);
2860 * ibmvscsis_destroy_command_q - Destroy Command Queue
2861 * @vscsi: Pointer to our adapter structure
2863 * Releases memory for command queue and unmaps mapped remote memory.
2865 * EXECUTION ENVIRONMENT:
2866 * Process level only
2868 static void ibmvscsis_destroy_command_q(struct scsi_info *vscsi)
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;
2877 static u8 ibmvscsis_fast_fail(struct scsi_info *vscsi,
2878 struct ibmvscsis_cmd *cmd)
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;
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)) {
2893 cmd->flags |= CMD_FAST_FAIL;
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.
2906 * Build the SRP response buffer and copy it to the client's memory space.
2908 static long srp_build_response(struct scsi_info *vscsi,
2909 struct ibmvscsis_cmd *cmd, uint *len_p)
2911 struct iu_entry *iue = cmd->iue;
2912 struct se_cmd *se_cmd = &cmd->se_cmd;
2913 struct srp_rsp *rsp;
2918 long rc = ADAPT_SUCCESS;
2920 spin_lock_bh(&vscsi->intr_lock);
2922 rsp = &vio_iu(iue)->srp.rsp;
2924 memset(rsp, 0, len);
2927 rsp->opcode = SRP_RSP;
2929 if (vscsi->credit > 0 && vscsi->state == SRP_PROCESSING)
2930 rsp->req_lim_delta = cpu_to_be32(vscsi->credit);
2932 rsp->req_lim_delta = cpu_to_be32(1 + vscsi->credit);
2933 rsp->tag = cmd->rsp.tag;
2936 if (cmd->type == SCSI_CDB) {
2937 rsp->status = ibmvscsis_fast_fail(vscsi, cmd);
2939 pr_debug("build_resp: cmd %p, scsi status %d\n", cmd,
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);
2950 rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
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) >>
2957 rsp->sol_not = (cmd->rsp.sol_not & SCSOLNT) >>
2961 /* this is task management */
2963 rsp->resp_data_len = cpu_to_be32(4);
2964 rsp->flags |= SRP_RSP_FLAG_RSPVALID;
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) >>
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) >>
2979 case TMR_FUNCTION_FAILED:
2980 case TMR_FUNCTION_REJECTED:
2982 rsp_code = SRP_TASK_MANAGEMENT_FUNCTION_FAILED;
2983 rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
2988 tsk_status = (u32 *)data;
2989 *tsk_status = cpu_to_be32(rsp_code);
2990 data = (char *)(tsk_status + 1);
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));
3005 if (connection_broken(vscsi))
3006 vscsi->flags |= RESPONSE_Q_DOWN | CLIENT_FAILED;
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);
3014 dev_err(&vscsi->dev, "build_response: error copying to client, rc %ld\n",
3019 spin_unlock_bh(&vscsi->intr_lock);
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)
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;
3034 ulong client_len, server_len;
3049 if (client_len == 0) {
3050 if (md_idx >= nmd) {
3051 dev_err(&vscsi->dev, "rdma: ran out of client memory descriptors\n");
3055 client_ioba = be64_to_cpu(md[md_idx].va);
3056 client_len = be32_to_cpu(md[md_idx].len);
3058 if (server_len == 0) {
3060 dev_err(&vscsi->dev, "rdma: ran out of scatter/gather list\n");
3064 server_ioba = sg_dma_address(sgp);
3065 server_len = sg_dma_len(sgp);
3070 if (buf_len > client_len)
3071 buf_len = client_len;
3073 if (buf_len > server_len)
3074 buf_len = server_len;
3076 if (buf_len > max_vdma_size)
3077 buf_len = max_vdma_size;
3079 if (dir == DMA_TO_DEVICE) {
3080 /* read from client */
3081 rc = h_copy_rdma(buf_len,
3082 vscsi->dds.window[REMOTE].liobn,
3084 vscsi->dds.window[LOCAL].liobn,
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.
3091 /* ensure that everything is in memory */
3093 /* ensure that memory has been made visible */
3095 rc = h_copy_rdma(buf_len,
3096 vscsi->dds.window[LOCAL].liobn,
3098 vscsi->dds.window[REMOTE].liobn,
3107 if (connection_broken(vscsi)) {
3108 spin_lock_bh(&vscsi->intr_lock);
3110 (RESPONSE_Q_DOWN | CLIENT_FAILED);
3111 spin_unlock_bh(&vscsi->intr_lock);
3113 dev_err(&vscsi->dev, "rdma: h_copy_rdma failed, rc %ld\n",
3118 dev_err(&vscsi->dev, "rdma: unknown error %ld from h_copy_rdma\n",
3126 client_len -= buf_len;
3127 if (client_len == 0)
3130 client_ioba += buf_len;
3132 server_len -= buf_len;
3133 if (server_len == 0)
3136 server_ioba += buf_len;
3147 * ibmvscsis_handle_crq() - Handle CRQ
3148 * @data: Pointer to our adapter structure
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
3154 * Note: this is an edge triggered interrupt. It can not be shared.
3156 static void ibmvscsis_handle_crq(unsigned long data)
3158 struct scsi_info *vscsi = (struct scsi_info *)data;
3159 struct viosrp_crq *crq;
3164 spin_lock_bh(&vscsi->intr_lock);
3166 pr_debug("got interrupt\n");
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
3173 if (TARGET_STOP(vscsi)) {
3174 vio_enable_interrupts(vscsi->dma_dev);
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);
3182 rc = vscsi->flags & SCHEDULE_DISCONNECT;
3183 crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
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,
3195 vscsi->cmd_q.index =
3196 (vscsi->cmd_q.index + 1) & vscsi->cmd_q.mask;
3199 rc = ibmvscsis_parse_command(vscsi, crq);
3201 if ((uint)crq->valid == VALID_TRANS_EVENT) {
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
3208 rc = ibmvscsis_trans_event(vscsi, crq);
3209 } else if (vscsi->flags & TRANS_EVENT) {
3211 * if a transport event has occurred leave
3212 * everything but transport events on the queue
3214 * need to decrement the queue index so we can
3215 * look at the element again
3217 if (vscsi->cmd_q.index)
3218 vscsi->cmd_q.index -= 1;
3221 * index is at 0 it just wrapped.
3222 * have it index last element in q
3224 vscsi->cmd_q.index = vscsi->cmd_q.mask;
3229 crq->valid = INVALIDATE_CMD_RESP_EL;
3231 crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
3238 vio_enable_interrupts(vscsi->dma_dev);
3240 pr_debug("handle_crq, reenabling interrupts\n");
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);
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,
3255 spin_unlock_bh(&vscsi->intr_lock);
3258 static int ibmvscsis_probe(struct vio_dev *vdev,
3259 const struct vio_device_id *id)
3261 struct scsi_info *vscsi;
3266 vscsi = kzalloc(sizeof(*vscsi), GFP_KERNEL);
3269 pr_err("probe: allocation of adapter failed\n");
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);
3279 snprintf(vscsi->tport.tport_name, IBMVSCSIS_NAMELEN, "%s",
3280 dev_name(&vdev->dev));
3282 pr_debug("probe tport_name: %s\n", vscsi->tport.tport_name);
3284 rc = read_dma_window(vscsi);
3287 pr_debug("Probe: liobn 0x%x, riobn 0x%x\n",
3288 vscsi->dds.window[LOCAL].liobn,
3289 vscsi->dds.window[REMOTE].liobn);
3291 strcpy(vscsi->eye, "VSCSI ");
3292 strncat(vscsi->eye, vdev->name, MAX_EYE);
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;
3299 spin_lock_bh(&ibmvscsis_dev_lock);
3300 list_add_tail(&vscsi->list, &ibmvscsis_dev_list);
3301 spin_unlock_bh(&ibmvscsis_dev_lock);
3304 * TBD: How do we determine # of cmds to request? Do we know how
3305 * many "children" we have?
3307 vscsi->request_limit = INITIAL_SRP_LIMIT;
3308 rc = srp_target_alloc(&vscsi->target, &vdev->dev, vscsi->request_limit,
3313 vscsi->target.ldata = vscsi;
3315 rc = ibmvscsis_alloc_cmds(vscsi, vscsi->request_limit);
3317 dev_err(&vscsi->dev, "alloc_cmds failed, rc %d, num %d\n",
3318 rc, vscsi->request_limit);
3323 * Note: the lock is used in freeing timers, so must initialize
3324 * first so that ordering in case of error is correct.
3326 spin_lock_init(&vscsi->intr_lock);
3328 rc = ibmvscsis_alloctimer(vscsi);
3330 dev_err(&vscsi->dev, "probe: alloctimer failed, rc %d\n", rc);
3334 rc = ibmvscsis_create_command_q(vscsi, 256);
3336 dev_err(&vscsi->dev, "probe: create_command_q failed, rc %d\n",
3341 vscsi->map_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
3342 if (!vscsi->map_buf) {
3344 dev_err(&vscsi->dev, "probe: allocating cmd buffer failed\n");
3348 vscsi->map_ioba = dma_map_single(&vdev->dev, vscsi->map_buf, PAGE_SIZE,
3350 if (dma_mapping_error(&vdev->dev, vscsi->map_ioba)) {
3352 dev_err(&vscsi->dev, "probe: error mapping command buffer\n");
3356 hrc = h_vioctl(vscsi->dds.unit_id, H_GET_PARTNER_INFO,
3357 (u64)vscsi->map_ioba | ((u64)PAGE_SIZE << 32), 0, 0, 0,
3359 if (hrc == H_SUCCESS)
3360 vscsi->client_data.partition_number =
3361 be64_to_cpu(*(u64 *)vscsi->map_buf);
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.
3367 pr_debug("probe hrc %ld, client partition num %d\n",
3368 hrc, vscsi->client_data.partition_number);
3370 tasklet_init(&vscsi->work_task, ibmvscsis_handle_crq,
3371 (unsigned long)vscsi);
3373 init_completion(&vscsi->wait_idle);
3374 init_completion(&vscsi->unconfig);
3376 snprintf(wq_name, 24, "ibmvscsis%s", dev_name(&vdev->dev));
3377 vscsi->work_q = create_workqueue(wq_name);
3378 if (!vscsi->work_q) {
3380 dev_err(&vscsi->dev, "create_workqueue failed\n");
3384 rc = request_irq(vdev->irq, ibmvscsis_interrupt, 0, "ibmvscsis", vscsi);
3387 dev_err(&vscsi->dev, "probe: request_irq failed, rc %d\n", rc);
3391 vscsi->state = WAIT_ENABLED;
3393 dev_set_drvdata(&vdev->dev, vscsi);
3398 destroy_workqueue(vscsi->work_q);
3400 dma_unmap_single(&vdev->dev, vscsi->map_ioba, PAGE_SIZE,
3403 kfree(vscsi->map_buf);
3405 tasklet_kill(&vscsi->work_task);
3406 ibmvscsis_unregister_command_q(vscsi);
3407 ibmvscsis_destroy_command_q(vscsi);
3409 ibmvscsis_freetimer(vscsi);
3411 ibmvscsis_free_cmds(vscsi);
3413 srp_target_free(&vscsi->target);
3415 spin_lock_bh(&ibmvscsis_dev_lock);
3416 list_del(&vscsi->list);
3417 spin_unlock_bh(&ibmvscsis_dev_lock);
3424 static int ibmvscsis_remove(struct vio_dev *vdev)
3426 struct scsi_info *vscsi = dev_get_drvdata(&vdev->dev);
3428 pr_debug("remove (%s)\n", dev_name(&vscsi->dma_dev->dev));
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);
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,
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);
3455 static ssize_t system_id_show(struct device *dev,
3456 struct device_attribute *attr, char *buf)
3458 return snprintf(buf, PAGE_SIZE, "%s\n", system_id);
3461 static ssize_t partition_number_show(struct device *dev,
3462 struct device_attribute *attr, char *buf)
3464 return snprintf(buf, PAGE_SIZE, "%x\n", partition_number);
3467 static ssize_t unit_address_show(struct device *dev,
3468 struct device_attribute *attr, char *buf)
3470 struct scsi_info *vscsi = container_of(dev, struct scsi_info, dev);
3472 return snprintf(buf, PAGE_SIZE, "%x\n", vscsi->dma_dev->unit_address);
3475 static int ibmvscsis_get_system_info(void)
3477 struct device_node *rootdn, *vdevdn;
3478 const char *id, *model, *name;
3481 rootdn = of_find_node_by_path("/");
3485 model = of_get_property(rootdn, "model", NULL);
3486 id = of_get_property(rootdn, "system-id", NULL);
3488 snprintf(system_id, sizeof(system_id), "%s-%s", model, id);
3490 name = of_get_property(rootdn, "ibm,partition-name", NULL);
3492 strncpy(partition_name, name, sizeof(partition_name));
3494 num = of_get_property(rootdn, "ibm,partition-no", NULL);
3496 partition_number = of_read_number(num, 1);
3498 of_node_put(rootdn);
3500 vdevdn = of_find_node_by_path("/vdevice");
3504 mvds = of_get_property(vdevdn, "ibm,max-virtual-dma-size",
3507 max_vdma_size = *mvds;
3508 of_node_put(vdevdn);
3514 static char *ibmvscsis_get_fabric_name(void)
3519 static char *ibmvscsis_get_fabric_wwn(struct se_portal_group *se_tpg)
3521 struct ibmvscsis_tport *tport =
3522 container_of(se_tpg, struct ibmvscsis_tport, se_tpg);
3524 return tport->tport_name;
3527 static u16 ibmvscsis_get_tag(struct se_portal_group *se_tpg)
3529 struct ibmvscsis_tport *tport =
3530 container_of(se_tpg, struct ibmvscsis_tport, se_tpg);
3532 return tport->tport_tpgt;
3535 static u32 ibmvscsis_get_default_depth(struct se_portal_group *se_tpg)
3540 static int ibmvscsis_check_true(struct se_portal_group *se_tpg)
3545 static int ibmvscsis_check_false(struct se_portal_group *se_tpg)
3550 static u32 ibmvscsis_tpg_get_inst_index(struct se_portal_group *se_tpg)
3555 static int ibmvscsis_check_stop_free(struct se_cmd *se_cmd)
3557 return target_put_sess_cmd(se_cmd);
3560 static void ibmvscsis_release_cmd(struct se_cmd *se_cmd)
3562 struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
3564 struct scsi_info *vscsi = cmd->adapter;
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);
3573 static u32 ibmvscsis_sess_get_index(struct se_session *se_sess)
3578 static int ibmvscsis_write_pending(struct se_cmd *se_cmd)
3580 struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
3582 struct iu_entry *iue = cmd->iue;
3585 rc = srp_transfer_data(cmd, &vio_iu(iue)->srp.cmd, ibmvscsis_rdma,
3588 pr_err("srp_transfer_data() failed: %d\n", rc);
3592 * We now tell TCM to add this WRITE CDB directly into the TCM storage
3593 * object execution queue.
3595 target_execute_cmd(se_cmd);
3599 static int ibmvscsis_write_pending_status(struct se_cmd *se_cmd)
3604 static void ibmvscsis_set_default_node_attrs(struct se_node_acl *nacl)
3608 static int ibmvscsis_get_cmd_state(struct se_cmd *se_cmd)
3613 static int ibmvscsis_queue_data_in(struct se_cmd *se_cmd)
3615 struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
3617 struct iu_entry *iue = cmd->iue;
3618 struct scsi_info *vscsi = cmd->adapter;
3623 rc = srp_transfer_data(cmd, &vio_iu(iue)->srp.cmd, ibmvscsis_rdma, 1,
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,
3635 srp_build_response(vscsi, cmd, &len);
3636 cmd->rsp.format = SRP_FORMAT;
3642 static int ibmvscsis_queue_status(struct se_cmd *se_cmd)
3644 struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
3646 struct scsi_info *vscsi = cmd->adapter;
3649 pr_debug("queue_status %p\n", se_cmd);
3651 srp_build_response(vscsi, cmd, &len);
3652 cmd->rsp.format = SRP_FORMAT;
3658 static void ibmvscsis_queue_tm_rsp(struct se_cmd *se_cmd)
3660 struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
3662 struct scsi_info *vscsi = cmd->adapter;
3665 pr_debug("queue_tm_rsp %p, status %d\n",
3666 se_cmd, (int)se_cmd->se_tmr_req->response);
3668 srp_build_response(vscsi, cmd, &len);
3669 cmd->rsp.format = SRP_FORMAT;
3673 static void ibmvscsis_aborted_task(struct se_cmd *se_cmd)
3675 /* TBD: What (if anything) should we do here? */
3676 pr_debug("ibmvscsis_aborted_task %p\n", se_cmd);
3679 static struct se_wwn *ibmvscsis_make_tport(struct target_fabric_configfs *tf,
3680 struct config_group *group,
3683 struct ibmvscsis_tport *tport;
3685 tport = ibmvscsis_lookup_port(name);
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;
3693 return ERR_PTR(-EINVAL);
3696 static void ibmvscsis_drop_tport(struct se_wwn *wwn)
3698 struct ibmvscsis_tport *tport = container_of(wwn,
3699 struct ibmvscsis_tport,
3702 pr_debug("drop_tport(%s)\n",
3703 config_item_name(&tport->tport_wwn.wwn_group.cg_item));
3706 static struct se_portal_group *ibmvscsis_make_tpg(struct se_wwn *wwn,
3707 struct config_group *group,
3710 struct ibmvscsis_tport *tport =
3711 container_of(wwn, struct ibmvscsis_tport, tport_wwn);
3714 tport->releasing = false;
3716 rc = core_tpg_register(&tport->tport_wwn, &tport->se_tpg,
3717 tport->tport_proto_id);
3721 return &tport->se_tpg;
3724 static void ibmvscsis_drop_tpg(struct se_portal_group *se_tpg)
3726 struct ibmvscsis_tport *tport = container_of(se_tpg,
3727 struct ibmvscsis_tport,
3730 tport->releasing = true;
3731 tport->enabled = false;
3734 * Release the virtual I_T Nexus for this ibmvscsis TPG
3736 ibmvscsis_drop_nexus(tport);
3738 * Deregister the se_tpg from TCM..
3740 core_tpg_deregister(se_tpg);
3743 static ssize_t ibmvscsis_wwn_version_show(struct config_item *item,
3746 return scnprintf(page, PAGE_SIZE, "%s\n", IBMVSCSIS_VERSION);
3748 CONFIGFS_ATTR_RO(ibmvscsis_wwn_, version);
3750 static struct configfs_attribute *ibmvscsis_wwn_attrs[] = {
3751 &ibmvscsis_wwn_attr_version,
3755 static ssize_t ibmvscsis_tpg_enable_show(struct config_item *item,
3758 struct se_portal_group *se_tpg = to_tpg(item);
3759 struct ibmvscsis_tport *tport = container_of(se_tpg,
3760 struct ibmvscsis_tport,
3763 return snprintf(page, PAGE_SIZE, "%d\n", (tport->enabled) ? 1 : 0);
3766 static ssize_t ibmvscsis_tpg_enable_store(struct config_item *item,
3767 const char *page, size_t count)
3769 struct se_portal_group *se_tpg = to_tpg(item);
3770 struct ibmvscsis_tport *tport = container_of(se_tpg,
3771 struct ibmvscsis_tport,
3773 struct scsi_info *vscsi = container_of(tport, struct scsi_info, tport);
3778 rc = kstrtoul(page, 0, &tmp);
3780 pr_err("Unable to extract srpt_tpg_store_enable\n");
3784 if ((tmp != 0) && (tmp != 1)) {
3785 pr_err("Illegal value for srpt_tpg_store_enable\n");
3790 spin_lock_bh(&vscsi->intr_lock);
3791 tport->enabled = true;
3792 lrc = ibmvscsis_enable_change_state(vscsi);
3794 pr_err("enable_change_state failed, rc %ld state %d\n",
3796 spin_unlock_bh(&vscsi->intr_lock);
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);
3805 pr_debug("tpg_enable_store, tmp %ld, state %d\n", tmp, vscsi->state);
3809 CONFIGFS_ATTR(ibmvscsis_tpg_, enable);
3811 static struct configfs_attribute *ibmvscsis_tpg_attrs[] = {
3812 &ibmvscsis_tpg_attr_enable,
3816 static const struct target_core_fabric_ops ibmvscsis_ops = {
3817 .module = THIS_MODULE,
3818 .name = "ibmvscsis",
3819 .get_fabric_name = ibmvscsis_get_fabric_name,
3820 .tpg_get_wwn = ibmvscsis_get_fabric_wwn,
3821 .tpg_get_tag = ibmvscsis_get_tag,
3822 .tpg_get_default_depth = ibmvscsis_get_default_depth,
3823 .tpg_check_demo_mode = ibmvscsis_check_true,
3824 .tpg_check_demo_mode_cache = ibmvscsis_check_true,
3825 .tpg_check_demo_mode_write_protect = ibmvscsis_check_false,
3826 .tpg_check_prod_mode_write_protect = ibmvscsis_check_false,
3827 .tpg_get_inst_index = ibmvscsis_tpg_get_inst_index,
3828 .check_stop_free = ibmvscsis_check_stop_free,
3829 .release_cmd = ibmvscsis_release_cmd,
3830 .sess_get_index = ibmvscsis_sess_get_index,
3831 .write_pending = ibmvscsis_write_pending,
3832 .write_pending_status = ibmvscsis_write_pending_status,
3833 .set_default_node_attributes = ibmvscsis_set_default_node_attrs,
3834 .get_cmd_state = ibmvscsis_get_cmd_state,
3835 .queue_data_in = ibmvscsis_queue_data_in,
3836 .queue_status = ibmvscsis_queue_status,
3837 .queue_tm_rsp = ibmvscsis_queue_tm_rsp,
3838 .aborted_task = ibmvscsis_aborted_task,
3840 * Setup function pointers for logic in target_core_fabric_configfs.c
3842 .fabric_make_wwn = ibmvscsis_make_tport,
3843 .fabric_drop_wwn = ibmvscsis_drop_tport,
3844 .fabric_make_tpg = ibmvscsis_make_tpg,
3845 .fabric_drop_tpg = ibmvscsis_drop_tpg,
3847 .tfc_wwn_attrs = ibmvscsis_wwn_attrs,
3848 .tfc_tpg_base_attrs = ibmvscsis_tpg_attrs,
3851 static void ibmvscsis_dev_release(struct device *dev) {};
3853 static struct class_attribute ibmvscsis_class_attrs[] = {
3857 static struct device_attribute dev_attr_system_id =
3858 __ATTR(system_id, S_IRUGO, system_id_show, NULL);
3860 static struct device_attribute dev_attr_partition_number =
3861 __ATTR(partition_number, S_IRUGO, partition_number_show, NULL);
3863 static struct device_attribute dev_attr_unit_address =
3864 __ATTR(unit_address, S_IRUGO, unit_address_show, NULL);
3866 static struct attribute *ibmvscsis_dev_attrs[] = {
3867 &dev_attr_system_id.attr,
3868 &dev_attr_partition_number.attr,
3869 &dev_attr_unit_address.attr,
3871 ATTRIBUTE_GROUPS(ibmvscsis_dev);
3873 static struct class ibmvscsis_class = {
3874 .name = "ibmvscsis",
3875 .dev_release = ibmvscsis_dev_release,
3876 .class_attrs = ibmvscsis_class_attrs,
3877 .dev_groups = ibmvscsis_dev_groups,
3880 static struct vio_device_id ibmvscsis_device_table[] = {
3881 { "v-scsi-host", "IBM,v-scsi-host" },
3884 MODULE_DEVICE_TABLE(vio, ibmvscsis_device_table);
3886 static struct vio_driver ibmvscsis_driver = {
3887 .name = "ibmvscsis",
3888 .id_table = ibmvscsis_device_table,
3889 .probe = ibmvscsis_probe,
3890 .remove = ibmvscsis_remove,
3894 * ibmvscsis_init() - Kernel Module initialization
3896 * Note: vio_register_driver() registers callback functions, and at least one
3897 * of those callback functions calls TCM - Linux IO Target Subsystem, thus
3898 * the SCSI Target template must be registered before vio_register_driver()
3901 static int __init ibmvscsis_init(void)
3905 rc = ibmvscsis_get_system_info();
3907 pr_err("rc %d from get_system_info\n", rc);
3911 rc = class_register(&ibmvscsis_class);
3913 pr_err("failed class register\n");
3917 rc = target_register_template(&ibmvscsis_ops);
3919 pr_err("rc %d from target_register_template\n", rc);
3920 goto unregister_class;
3923 rc = vio_register_driver(&ibmvscsis_driver);
3925 pr_err("rc %d from vio_register_driver\n", rc);
3926 goto unregister_target;
3932 target_unregister_template(&ibmvscsis_ops);
3934 class_unregister(&ibmvscsis_class);
3939 static void __exit ibmvscsis_exit(void)
3941 pr_info("Unregister IBM virtual SCSI host driver\n");
3942 vio_unregister_driver(&ibmvscsis_driver);
3943 target_unregister_template(&ibmvscsis_ops);
3944 class_unregister(&ibmvscsis_class);
3947 MODULE_DESCRIPTION("IBMVSCSIS fabric driver");
3948 MODULE_AUTHOR("Bryant G. Ly and Michael Cyr");
3949 MODULE_LICENSE("GPL");
3950 MODULE_VERSION(IBMVSCSIS_VERSION);
3951 module_init(ibmvscsis_init);
3952 module_exit(ibmvscsis_exit);