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>
34 #include <target/target_core_base.h>
35 #include <target/target_core_fabric.h>
37 #include <asm/hvcall.h>
40 #include <scsi/viosrp.h>
42 #include "ibmvscsi_tgt.h"
44 #define IBMVSCSIS_VERSION "v0.2"
46 #define INITIAL_SRP_LIMIT 800
47 #define DEFAULT_MAX_SECTORS 256
49 static uint max_vdma_size = MAX_H_COPY_RDMA;
51 static char system_id[SYS_ID_NAME_LEN] = "";
52 static char partition_name[PARTITION_NAMELEN] = "UNKNOWN";
53 static uint partition_number = -1;
55 /* Adapter list and lock to control it */
56 static DEFINE_SPINLOCK(ibmvscsis_dev_lock);
57 static LIST_HEAD(ibmvscsis_dev_list);
59 static long ibmvscsis_parse_command(struct scsi_info *vscsi,
60 struct viosrp_crq *crq);
62 static void ibmvscsis_adapter_idle(struct scsi_info *vscsi);
64 static void ibmvscsis_determine_resid(struct se_cmd *se_cmd,
67 u32 residual_count = se_cmd->residual_count;
72 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
73 if (se_cmd->data_direction == DMA_TO_DEVICE) {
74 /* residual data from an underflow write */
75 rsp->flags = SRP_RSP_FLAG_DOUNDER;
76 rsp->data_out_res_cnt = cpu_to_be32(residual_count);
77 } else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
78 /* residual data from an underflow read */
79 rsp->flags = SRP_RSP_FLAG_DIUNDER;
80 rsp->data_in_res_cnt = cpu_to_be32(residual_count);
82 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
83 if (se_cmd->data_direction == DMA_TO_DEVICE) {
84 /* residual data from an overflow write */
85 rsp->flags = SRP_RSP_FLAG_DOOVER;
86 rsp->data_out_res_cnt = cpu_to_be32(residual_count);
87 } else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
88 /* residual data from an overflow read */
89 rsp->flags = SRP_RSP_FLAG_DIOVER;
90 rsp->data_in_res_cnt = cpu_to_be32(residual_count);
96 * connection_broken() - Determine if the connection to the client is good
97 * @vscsi: Pointer to our adapter structure
99 * This function attempts to send a ping MAD to the client. If the call to
100 * queue the request returns H_CLOSED then the connection has been broken
101 * and the function returns TRUE.
103 * EXECUTION ENVIRONMENT:
104 * Interrupt or Process environment
106 static bool connection_broken(struct scsi_info *vscsi)
108 struct viosrp_crq *crq;
109 u64 buffer[2] = { 0, 0 };
113 /* create a PING crq */
114 crq = (struct viosrp_crq *)&buffer;
115 crq->valid = VALID_CMD_RESP_EL;
116 crq->format = MESSAGE_IN_CRQ;
119 h_return_code = h_send_crq(vscsi->dds.unit_id,
120 cpu_to_be64(buffer[MSG_HI]),
121 cpu_to_be64(buffer[MSG_LOW]));
123 pr_debug("connection_broken: rc %ld\n", h_return_code);
125 if (h_return_code == H_CLOSED)
132 * ibmvscsis_unregister_command_q() - Helper Function-Unregister Command Queue
133 * @vscsi: Pointer to our adapter structure
135 * This function calls h_free_q then frees the interrupt bit etc.
136 * It must release the lock before doing so because of the time it can take
137 * for h_free_crq in PHYP
138 * NOTE: the caller must make sure that state and or flags will prevent
139 * interrupt handler from scheduling work.
140 * NOTE: anyone calling this function may need to set the CRQ_CLOSED flag
141 * we can't do it here, because we don't have the lock
143 * EXECUTION ENVIRONMENT:
146 static long ibmvscsis_unregister_command_q(struct scsi_info *vscsi)
149 long rc = ADAPT_SUCCESS;
153 qrc = h_free_crq(vscsi->dds.unit_id);
160 dev_err(&vscsi->dev, "unregister_command_q: error from h_free_crq %ld\n",
166 case H_LONG_BUSY_ORDER_1_MSEC:
167 /* msleep not good for small values */
168 usleep_range(1000, 2000);
171 case H_LONG_BUSY_ORDER_10_MSEC:
172 usleep_range(10000, 20000);
175 case H_LONG_BUSY_ORDER_100_MSEC:
179 case H_LONG_BUSY_ORDER_1_SEC:
183 case H_LONG_BUSY_ORDER_10_SEC:
187 case H_LONG_BUSY_ORDER_100_SEC:
192 dev_err(&vscsi->dev, "unregister_command_q: unknown error %ld from h_free_crq\n",
199 * dont wait more then 300 seconds
200 * ticks are in milliseconds more or less
202 if (ticks > 300000 && qrc != H_SUCCESS) {
204 dev_err(&vscsi->dev, "Excessive wait for h_free_crq\n");
206 } while (qrc != H_SUCCESS && rc == ADAPT_SUCCESS);
208 pr_debug("Freeing CRQ: phyp rc %ld, rc %ld\n", qrc, rc);
214 * ibmvscsis_delete_client_info() - Helper function to Delete Client Info
215 * @vscsi: Pointer to our adapter structure
216 * @client_closed: True if client closed its queue
218 * Deletes information specific to the client when the client goes away
220 * EXECUTION ENVIRONMENT:
221 * Interrupt or Process
223 static void ibmvscsis_delete_client_info(struct scsi_info *vscsi,
226 vscsi->client_cap = 0;
229 * Some things we don't want to clear if we're closing the queue,
230 * because some clients don't resend the host handshake when they
231 * get a transport event.
234 vscsi->client_data.os_type = 0;
238 * ibmvscsis_free_command_q() - Free Command Queue
239 * @vscsi: Pointer to our adapter structure
241 * This function calls unregister_command_q, then clears interrupts and
242 * any pending interrupt acknowledgments associated with the command q.
243 * It also clears memory if there is no error.
245 * PHYP did not meet the PAPR architecture so that we must give up the
246 * lock. This causes a timing hole regarding state change. To close the
247 * hole this routine does accounting on any change that occurred during
248 * the time the lock is not held.
249 * NOTE: must give up and then acquire the interrupt lock, the caller must
250 * make sure that state and or flags will prevent interrupt handler from
253 * EXECUTION ENVIRONMENT:
254 * Process level, interrupt lock is held
256 static long ibmvscsis_free_command_q(struct scsi_info *vscsi)
259 u32 flags_under_lock;
260 u16 state_under_lock;
261 long rc = ADAPT_SUCCESS;
263 if (!(vscsi->flags & CRQ_CLOSED)) {
264 vio_disable_interrupts(vscsi->dma_dev);
266 state_under_lock = vscsi->new_state;
267 flags_under_lock = vscsi->flags;
268 vscsi->phyp_acr_state = 0;
269 vscsi->phyp_acr_flags = 0;
271 spin_unlock_bh(&vscsi->intr_lock);
272 rc = ibmvscsis_unregister_command_q(vscsi);
273 spin_lock_bh(&vscsi->intr_lock);
275 if (state_under_lock != vscsi->new_state)
276 vscsi->phyp_acr_state = vscsi->new_state;
278 vscsi->phyp_acr_flags = ((~flags_under_lock) & vscsi->flags);
280 if (rc == ADAPT_SUCCESS) {
281 bytes = vscsi->cmd_q.size * PAGE_SIZE;
282 memset(vscsi->cmd_q.base_addr, 0, bytes);
283 vscsi->cmd_q.index = 0;
284 vscsi->flags |= CRQ_CLOSED;
286 ibmvscsis_delete_client_info(vscsi, false);
289 pr_debug("free_command_q: flags 0x%x, state 0x%hx, acr_flags 0x%x, acr_state 0x%hx\n",
290 vscsi->flags, vscsi->state, vscsi->phyp_acr_flags,
291 vscsi->phyp_acr_state);
297 * ibmvscsis_cmd_q_dequeue() - Get valid Command element
298 * @mask: Mask to use in case index wraps
299 * @current_index: Current index into command queue
300 * @base_addr: Pointer to start of command queue
302 * Returns a pointer to a valid command element or NULL, if the command
305 * EXECUTION ENVIRONMENT:
306 * Interrupt environment, interrupt lock held
308 static struct viosrp_crq *ibmvscsis_cmd_q_dequeue(uint mask,
310 struct viosrp_crq *base_addr)
312 struct viosrp_crq *ptr;
314 ptr = base_addr + *current_index;
317 *current_index = (*current_index + 1) & mask;
327 * ibmvscsis_send_init_message() - send initialize message to the client
328 * @vscsi: Pointer to our adapter structure
329 * @format: Which Init Message format to send
331 * EXECUTION ENVIRONMENT:
332 * Interrupt environment interrupt lock held
334 static long ibmvscsis_send_init_message(struct scsi_info *vscsi, u8 format)
336 struct viosrp_crq *crq;
337 u64 buffer[2] = { 0, 0 };
340 crq = (struct viosrp_crq *)&buffer;
341 crq->valid = VALID_INIT_MSG;
342 crq->format = format;
343 rc = h_send_crq(vscsi->dds.unit_id, cpu_to_be64(buffer[MSG_HI]),
344 cpu_to_be64(buffer[MSG_LOW]));
350 * ibmvscsis_check_init_msg() - Check init message valid
351 * @vscsi: Pointer to our adapter structure
352 * @format: Pointer to return format of Init Message, if any.
353 * Set to UNUSED_FORMAT if no Init Message in queue.
355 * Checks if an initialize message was queued by the initiatior
356 * after the queue was created and before the interrupt was enabled.
358 * EXECUTION ENVIRONMENT:
359 * Process level only, interrupt lock held
361 static long ibmvscsis_check_init_msg(struct scsi_info *vscsi, uint *format)
363 struct viosrp_crq *crq;
364 long rc = ADAPT_SUCCESS;
366 crq = ibmvscsis_cmd_q_dequeue(vscsi->cmd_q.mask, &vscsi->cmd_q.index,
367 vscsi->cmd_q.base_addr);
369 *format = (uint)UNUSED_FORMAT;
370 } else if (crq->valid == VALID_INIT_MSG && crq->format == INIT_MSG) {
371 *format = (uint)INIT_MSG;
372 crq->valid = INVALIDATE_CMD_RESP_EL;
376 * the caller has ensured no initialize message was
377 * sent after the queue was
378 * created so there should be no other message on the queue.
380 crq = ibmvscsis_cmd_q_dequeue(vscsi->cmd_q.mask,
382 vscsi->cmd_q.base_addr);
384 *format = (uint)(crq->format);
386 crq->valid = INVALIDATE_CMD_RESP_EL;
390 *format = (uint)(crq->format);
392 crq->valid = INVALIDATE_CMD_RESP_EL;
400 * ibmvscsis_disconnect() - Helper function to disconnect
401 * @work: Pointer to work_struct, gives access to our adapter structure
403 * An error has occurred or the driver received a Transport event,
404 * and the driver is requesting that the command queue be de-registered
405 * in a safe manner. If there is no outstanding I/O then we can stop the
406 * queue. If we are restarting the queue it will be reflected in the
407 * the state of the adapter.
409 * EXECUTION ENVIRONMENT:
410 * Process environment
412 static void ibmvscsis_disconnect(struct work_struct *work)
414 struct scsi_info *vscsi = container_of(work, struct scsi_info,
417 bool wait_idle = false;
419 spin_lock_bh(&vscsi->intr_lock);
420 new_state = vscsi->new_state;
421 vscsi->new_state = 0;
423 pr_debug("disconnect: flags 0x%x, state 0x%hx\n", vscsi->flags,
427 * check which state we are in and see if we
428 * should transitition to the new state
430 switch (vscsi->state) {
431 /* Should never be called while in this state. */
434 * Can never transition from this state;
435 * igonore errors and logout.
440 /* can transition from this state to UNCONFIGURING */
442 if (new_state == UNCONFIGURING)
443 vscsi->state = new_state;
447 * Can transition from this state to to unconfiguring
450 case ERR_DISCONNECT_RECONNECT:
454 vscsi->state = new_state;
464 /* can transition from this state to UNCONFIGURING */
465 case ERR_DISCONNECTED:
466 if (new_state == UNCONFIGURING)
467 vscsi->state = new_state;
473 vscsi->state = new_state;
474 vscsi->flags |= RESPONSE_Q_DOWN;
475 vscsi->flags &= ~(SCHEDULE_DISCONNECT |
476 DISCONNECT_SCHEDULED);
478 if (vscsi->flags & CFG_SLEEPING) {
479 vscsi->flags &= ~CFG_SLEEPING;
480 complete(&vscsi->unconfig);
484 /* should never happen */
486 case ERR_DISCONNECT_RECONNECT:
488 dev_err(&vscsi->dev, "disconnect: invalid state %d for WAIT_IDLE\n",
497 vscsi->flags |= RESPONSE_Q_DOWN;
498 vscsi->state = new_state;
499 vscsi->flags &= ~(SCHEDULE_DISCONNECT |
500 DISCONNECT_SCHEDULED);
501 ibmvscsis_free_command_q(vscsi);
504 case ERR_DISCONNECT_RECONNECT:
505 vscsi->state = new_state;
511 * Initiator has not done a successful srp login
512 * or has done a successful srp logout ( adapter was not
513 * busy). In the first case there can be responses queued
514 * waiting for space on the initiators response queue (MAD)
515 * The second case the adapter is idle. Assume the worse case,
516 * i.e. the second case.
518 case WAIT_CONNECTION:
522 vscsi->state = new_state;
525 /* can transition from this state to UNCONFIGURING */
527 if (new_state == UNCONFIGURING)
528 vscsi->state = new_state;
535 pr_debug("disconnect start wait, active %d, sched %d\n",
536 (int)list_empty(&vscsi->active_q),
537 (int)list_empty(&vscsi->schedule_q));
538 if (!list_empty(&vscsi->active_q) ||
539 !list_empty(&vscsi->schedule_q)) {
540 vscsi->flags |= WAIT_FOR_IDLE;
541 pr_debug("disconnect flags 0x%x\n", vscsi->flags);
543 * This routine is can not be called with the interrupt
546 spin_unlock_bh(&vscsi->intr_lock);
547 wait_for_completion(&vscsi->wait_idle);
548 spin_lock_bh(&vscsi->intr_lock);
550 pr_debug("disconnect stop wait\n");
552 ibmvscsis_adapter_idle(vscsi);
555 spin_unlock_bh(&vscsi->intr_lock);
559 * ibmvscsis_post_disconnect() - Schedule the disconnect
560 * @vscsi: Pointer to our adapter structure
561 * @new_state: State to move to after disconnecting
562 * @flag_bits: Flags to turn on in adapter structure
564 * If it's already been scheduled, then see if we need to "upgrade"
565 * the new state (if the one passed in is more "severe" than the
569 * interrupt lock is held
571 static void ibmvscsis_post_disconnect(struct scsi_info *vscsi, uint new_state,
576 /* check the validity of the new state */
580 case ERR_DISCONNECT_RECONNECT:
585 dev_err(&vscsi->dev, "post_disconnect: Invalid new state %d\n",
590 vscsi->flags |= flag_bits;
592 pr_debug("post_disconnect: new_state 0x%x, flag_bits 0x%x, vscsi->flags 0x%x, state %hx\n",
593 new_state, flag_bits, vscsi->flags, vscsi->state);
595 if (!(vscsi->flags & (DISCONNECT_SCHEDULED | SCHEDULE_DISCONNECT))) {
596 vscsi->flags |= SCHEDULE_DISCONNECT;
597 vscsi->new_state = new_state;
599 INIT_WORK(&vscsi->proc_work, ibmvscsis_disconnect);
600 (void)queue_work(vscsi->work_q, &vscsi->proc_work);
602 if (vscsi->new_state)
603 state = vscsi->new_state;
605 state = vscsi->state;
612 case ERR_DISCONNECTED:
615 if (new_state == UNCONFIGURING)
616 vscsi->new_state = new_state;
619 case ERR_DISCONNECT_RECONNECT:
623 vscsi->new_state = new_state;
632 case WAIT_CONNECTION:
635 vscsi->new_state = new_state;
643 pr_debug("Leaving post_disconnect: flags 0x%x, new_state 0x%x\n",
644 vscsi->flags, vscsi->new_state);
648 * ibmvscsis_handle_init_compl_msg() - Respond to an Init Complete Message
649 * @vscsi: Pointer to our adapter structure
651 * Must be called with interrupt lock held.
653 static long ibmvscsis_handle_init_compl_msg(struct scsi_info *vscsi)
655 long rc = ADAPT_SUCCESS;
657 switch (vscsi->state) {
660 case ERR_DISCONNECT_RECONNECT:
661 case ERR_DISCONNECTED:
667 case WAIT_CONNECTION:
668 vscsi->state = CONNECTED;
677 dev_err(&vscsi->dev, "init_msg: invalid state %d to get init compl msg\n",
679 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
687 * ibmvscsis_handle_init_msg() - Respond to an Init Message
688 * @vscsi: Pointer to our adapter structure
690 * Must be called with interrupt lock held.
692 static long ibmvscsis_handle_init_msg(struct scsi_info *vscsi)
694 long rc = ADAPT_SUCCESS;
696 switch (vscsi->state) {
697 case WAIT_CONNECTION:
698 rc = ibmvscsis_send_init_message(vscsi, INIT_COMPLETE_MSG);
701 vscsi->state = CONNECTED;
705 dev_err(&vscsi->dev, "init_msg: failed to send, rc %ld\n",
707 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
711 dev_err(&vscsi->dev, "init_msg: failed to send, rc %ld\n",
714 ibmvscsis_post_disconnect(vscsi,
715 ERR_DISCONNECT_RECONNECT, 0);
719 pr_warn("init_msg: failed to send, rc %ld\n", rc);
738 case ERR_DISCONNECT_RECONNECT:
739 case ERR_DISCONNECTED:
742 dev_err(&vscsi->dev, "init_msg: invalid state %d to get init msg\n",
744 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
752 * ibmvscsis_init_msg() - Respond to an init message
753 * @vscsi: Pointer to our adapter structure
754 * @crq: Pointer to CRQ element containing the Init Message
756 * EXECUTION ENVIRONMENT:
757 * Interrupt, interrupt lock held
759 static long ibmvscsis_init_msg(struct scsi_info *vscsi, struct viosrp_crq *crq)
761 long rc = ADAPT_SUCCESS;
763 pr_debug("init_msg: state 0x%hx\n", vscsi->state);
765 rc = h_vioctl(vscsi->dds.unit_id, H_GET_PARTNER_INFO,
766 (u64)vscsi->map_ioba | ((u64)PAGE_SIZE << 32), 0, 0, 0,
768 if (rc == H_SUCCESS) {
769 vscsi->client_data.partition_number =
770 be64_to_cpu(*(u64 *)vscsi->map_buf);
771 pr_debug("init_msg, part num %d\n",
772 vscsi->client_data.partition_number);
774 pr_debug("init_msg h_vioctl rc %ld\n", rc);
778 if (crq->format == INIT_MSG) {
779 rc = ibmvscsis_handle_init_msg(vscsi);
780 } else if (crq->format == INIT_COMPLETE_MSG) {
781 rc = ibmvscsis_handle_init_compl_msg(vscsi);
784 dev_err(&vscsi->dev, "init_msg: invalid format %d\n",
786 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
793 * ibmvscsis_establish_new_q() - Establish new CRQ queue
794 * @vscsi: Pointer to our adapter structure
796 * Must be called with interrupt lock held.
798 static long ibmvscsis_establish_new_q(struct scsi_info *vscsi)
800 long rc = ADAPT_SUCCESS;
803 vscsi->flags &= PRESERVE_FLAG_FIELDS;
804 vscsi->rsp_q_timer.timer_pops = 0;
808 rc = vio_enable_interrupts(vscsi->dma_dev);
810 pr_warn("establish_new_q: failed to enable interrupts, rc %ld\n",
815 rc = ibmvscsis_check_init_msg(vscsi, &format);
817 dev_err(&vscsi->dev, "establish_new_q: check_init_msg failed, rc %ld\n",
822 if (format == UNUSED_FORMAT) {
823 rc = ibmvscsis_send_init_message(vscsi, INIT_MSG);
836 vscsi->state = UNDEFINED;
840 } else if (format == INIT_MSG) {
841 rc = ibmvscsis_handle_init_msg(vscsi);
848 * ibmvscsis_reset_queue() - Reset CRQ Queue
849 * @vscsi: Pointer to our adapter structure
851 * This function calls h_free_q and then calls h_reg_q and does all
852 * of the bookkeeping to get us back to where we can communicate.
854 * Actually, we don't always call h_free_crq. A problem was discovered
855 * where one partition would close and reopen his queue, which would
856 * cause his partner to get a transport event, which would cause him to
857 * close and reopen his queue, which would cause the original partition
858 * to get a transport event, etc., etc. To prevent this, we don't
859 * actually close our queue if the client initiated the reset, (i.e.
860 * either we got a transport event or we have detected that the client's
863 * EXECUTION ENVIRONMENT:
864 * Process environment, called with interrupt lock held
866 static void ibmvscsis_reset_queue(struct scsi_info *vscsi)
869 long rc = ADAPT_SUCCESS;
871 pr_debug("reset_queue: flags 0x%x\n", vscsi->flags);
873 /* don't reset, the client did it for us */
874 if (vscsi->flags & (CLIENT_FAILED | TRANS_EVENT)) {
875 vscsi->flags &= PRESERVE_FLAG_FIELDS;
876 vscsi->rsp_q_timer.timer_pops = 0;
879 vscsi->state = WAIT_CONNECTION;
880 vio_enable_interrupts(vscsi->dma_dev);
882 rc = ibmvscsis_free_command_q(vscsi);
883 if (rc == ADAPT_SUCCESS) {
884 vscsi->state = WAIT_CONNECTION;
886 bytes = vscsi->cmd_q.size * PAGE_SIZE;
887 rc = h_reg_crq(vscsi->dds.unit_id,
888 vscsi->cmd_q.crq_token, bytes);
889 if (rc == H_CLOSED || rc == H_SUCCESS) {
890 rc = ibmvscsis_establish_new_q(vscsi);
893 if (rc != ADAPT_SUCCESS) {
894 pr_debug("reset_queue: reg_crq rc %ld\n", rc);
896 vscsi->state = ERR_DISCONNECTED;
897 vscsi->flags |= RESPONSE_Q_DOWN;
898 ibmvscsis_free_command_q(vscsi);
901 vscsi->state = ERR_DISCONNECTED;
902 vscsi->flags |= RESPONSE_Q_DOWN;
908 * ibmvscsis_free_cmd_resources() - Free command resources
909 * @vscsi: Pointer to our adapter structure
910 * @cmd: Command which is not longer in use
912 * Must be called with interrupt lock held.
914 static void ibmvscsis_free_cmd_resources(struct scsi_info *vscsi,
915 struct ibmvscsis_cmd *cmd)
917 struct iu_entry *iue = cmd->iue;
920 case TASK_MANAGEMENT:
923 * When the queue goes down this value is cleared, so it
924 * cannot be cleared in this general purpose function.
930 vscsi->flags &= ~PROCESSING_MAD;
935 dev_err(&vscsi->dev, "free_cmd_resources unknown type %d\n",
941 list_add_tail(&cmd->list, &vscsi->free_cmd);
944 if (list_empty(&vscsi->active_q) && list_empty(&vscsi->schedule_q) &&
945 list_empty(&vscsi->waiting_rsp) && (vscsi->flags & WAIT_FOR_IDLE)) {
946 vscsi->flags &= ~WAIT_FOR_IDLE;
947 complete(&vscsi->wait_idle);
952 * ibmvscsis_trans_event() - Handle a Transport Event
953 * @vscsi: Pointer to our adapter structure
954 * @crq: Pointer to CRQ entry containing the Transport Event
956 * Do the logic to close the I_T nexus. This function may not
957 * behave to specification.
959 * EXECUTION ENVIRONMENT:
960 * Interrupt, interrupt lock held
962 static long ibmvscsis_trans_event(struct scsi_info *vscsi,
963 struct viosrp_crq *crq)
965 long rc = ADAPT_SUCCESS;
967 pr_debug("trans_event: format %d, flags 0x%x, state 0x%hx\n",
968 (int)crq->format, vscsi->flags, vscsi->state);
970 switch (crq->format) {
973 case PARTNER_DEREGISTER:
974 ibmvscsis_delete_client_info(vscsi, true);
979 dev_err(&vscsi->dev, "trans_event: invalid format %d\n",
981 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT,
986 if (rc == ADAPT_SUCCESS) {
987 switch (vscsi->state) {
989 case ERR_DISCONNECTED:
994 vscsi->flags |= (RESPONSE_Q_DOWN | TRANS_EVENT);
1000 case WAIT_CONNECTION:
1004 ibmvscsis_post_disconnect(vscsi, WAIT_IDLE,
1009 case SRP_PROCESSING:
1010 if ((vscsi->debit > 0) ||
1011 !list_empty(&vscsi->schedule_q) ||
1012 !list_empty(&vscsi->waiting_rsp) ||
1013 !list_empty(&vscsi->active_q)) {
1014 pr_debug("debit %d, sched %d, wait %d, active %d\n",
1016 (int)list_empty(&vscsi->schedule_q),
1017 (int)list_empty(&vscsi->waiting_rsp),
1018 (int)list_empty(&vscsi->active_q));
1019 pr_warn("connection lost with outstanding work\n");
1021 pr_debug("trans_event: SRP Processing, but no outstanding work\n");
1024 ibmvscsis_post_disconnect(vscsi, WAIT_IDLE,
1029 case ERR_DISCONNECT:
1030 case ERR_DISCONNECT_RECONNECT:
1032 vscsi->flags |= (RESPONSE_Q_DOWN | TRANS_EVENT);
1037 rc = vscsi->flags & SCHEDULE_DISCONNECT;
1039 pr_debug("Leaving trans_event: flags 0x%x, state 0x%hx, rc %ld\n",
1040 vscsi->flags, vscsi->state, rc);
1046 * ibmvscsis_poll_cmd_q() - Poll Command Queue
1047 * @vscsi: Pointer to our adapter structure
1049 * Called to handle command elements that may have arrived while
1050 * interrupts were disabled.
1052 * EXECUTION ENVIRONMENT:
1053 * intr_lock must be held
1055 static void ibmvscsis_poll_cmd_q(struct scsi_info *vscsi)
1057 struct viosrp_crq *crq;
1062 pr_debug("poll_cmd_q: flags 0x%x, state 0x%hx, q index %ud\n",
1063 vscsi->flags, vscsi->state, vscsi->cmd_q.index);
1065 rc = vscsi->flags & SCHEDULE_DISCONNECT;
1066 crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
1072 vscsi->cmd_q.index =
1073 (vscsi->cmd_q.index + 1) & vscsi->cmd_q.mask;
1076 rc = ibmvscsis_parse_command(vscsi, crq);
1078 if ((uint)crq->valid == VALID_TRANS_EVENT) {
1080 * must service the transport layer events even
1081 * in an error state, dont break out until all
1082 * the consecutive transport events have been
1085 rc = ibmvscsis_trans_event(vscsi, crq);
1086 } else if (vscsi->flags & TRANS_EVENT) {
1088 * if a tranport event has occurred leave
1089 * everything but transport events on the queue
1091 pr_debug("poll_cmd_q, ignoring\n");
1094 * need to decrement the queue index so we can
1095 * look at the elment again
1097 if (vscsi->cmd_q.index)
1098 vscsi->cmd_q.index -= 1;
1101 * index is at 0 it just wrapped.
1102 * have it index last element in q
1104 vscsi->cmd_q.index = vscsi->cmd_q.mask;
1109 crq->valid = INVALIDATE_CMD_RESP_EL;
1111 crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
1118 vio_enable_interrupts(vscsi->dma_dev);
1120 pr_debug("poll_cmd_q, reenabling interrupts\n");
1128 pr_debug("Leaving poll_cmd_q: rc %ld\n", rc);
1132 * ibmvscsis_free_cmd_qs() - Free elements in queue
1133 * @vscsi: Pointer to our adapter structure
1135 * Free all of the elements on all queues that are waiting for
1139 * Called with interrupt lock held
1141 static void ibmvscsis_free_cmd_qs(struct scsi_info *vscsi)
1143 struct ibmvscsis_cmd *cmd, *nxt;
1145 pr_debug("free_cmd_qs: waiting_rsp empty %d, timer starter %d\n",
1146 (int)list_empty(&vscsi->waiting_rsp),
1147 vscsi->rsp_q_timer.started);
1149 list_for_each_entry_safe(cmd, nxt, &vscsi->waiting_rsp, list) {
1150 list_del(&cmd->list);
1151 ibmvscsis_free_cmd_resources(vscsi, cmd);
1156 * ibmvscsis_get_free_cmd() - Get free command from list
1157 * @vscsi: Pointer to our adapter structure
1159 * Must be called with interrupt lock held.
1161 static struct ibmvscsis_cmd *ibmvscsis_get_free_cmd(struct scsi_info *vscsi)
1163 struct ibmvscsis_cmd *cmd = NULL;
1164 struct iu_entry *iue;
1166 iue = srp_iu_get(&vscsi->target);
1168 cmd = list_first_entry_or_null(&vscsi->free_cmd,
1169 struct ibmvscsis_cmd, list);
1171 list_del(&cmd->list);
1173 cmd->type = UNSET_TYPE;
1174 memset(&cmd->se_cmd, 0, sizeof(cmd->se_cmd));
1184 * ibmvscsis_adapter_idle() - Helper function to handle idle adapter
1185 * @vscsi: Pointer to our adapter structure
1187 * This function is called when the adapter is idle when the driver
1188 * is attempting to clear an error condition.
1189 * The adapter is considered busy if any of its cmd queues
1190 * are non-empty. This function can be invoked
1191 * from the off level disconnect function.
1193 * EXECUTION ENVIRONMENT:
1194 * Process environment called with interrupt lock held
1196 static void ibmvscsis_adapter_idle(struct scsi_info *vscsi)
1198 int free_qs = false;
1200 pr_debug("adapter_idle: flags 0x%x, state 0x%hx\n", vscsi->flags,
1203 /* Only need to free qs if we're disconnecting from client */
1204 if (vscsi->state != WAIT_CONNECTION || vscsi->flags & TRANS_EVENT)
1207 switch (vscsi->state) {
1209 ibmvscsis_free_command_q(vscsi);
1212 if (vscsi->flags & CFG_SLEEPING) {
1213 vscsi->flags &= ~CFG_SLEEPING;
1214 complete(&vscsi->unconfig);
1217 case ERR_DISCONNECT_RECONNECT:
1218 ibmvscsis_reset_queue(vscsi);
1219 pr_debug("adapter_idle, disc_rec: flags 0x%x\n", vscsi->flags);
1222 case ERR_DISCONNECT:
1223 ibmvscsis_free_command_q(vscsi);
1224 vscsi->flags &= ~(SCHEDULE_DISCONNECT | DISCONNECT_SCHEDULED);
1225 vscsi->flags |= RESPONSE_Q_DOWN;
1226 if (vscsi->tport.enabled)
1227 vscsi->state = ERR_DISCONNECTED;
1229 vscsi->state = WAIT_ENABLED;
1230 pr_debug("adapter_idle, disc: flags 0x%x, state 0x%hx\n",
1231 vscsi->flags, vscsi->state);
1235 vscsi->rsp_q_timer.timer_pops = 0;
1238 if (vscsi->flags & TRANS_EVENT) {
1239 vscsi->state = WAIT_CONNECTION;
1240 vscsi->flags &= PRESERVE_FLAG_FIELDS;
1242 vscsi->state = CONNECTED;
1243 vscsi->flags &= ~DISCONNECT_SCHEDULED;
1246 pr_debug("adapter_idle, wait: flags 0x%x, state 0x%hx\n",
1247 vscsi->flags, vscsi->state);
1248 ibmvscsis_poll_cmd_q(vscsi);
1251 case ERR_DISCONNECTED:
1252 vscsi->flags &= ~DISCONNECT_SCHEDULED;
1253 pr_debug("adapter_idle, disconnected: flags 0x%x, state 0x%hx\n",
1254 vscsi->flags, vscsi->state);
1258 dev_err(&vscsi->dev, "adapter_idle: in invalid state %d\n",
1264 ibmvscsis_free_cmd_qs(vscsi);
1267 * There is a timing window where we could lose a disconnect request.
1268 * The known path to this window occurs during the DISCONNECT_RECONNECT
1269 * case above: reset_queue calls free_command_q, which will release the
1270 * interrupt lock. During that time, a new post_disconnect call can be
1271 * made with a "more severe" state (DISCONNECT or UNCONFIGURING).
1272 * Because the DISCONNECT_SCHEDULED flag is already set, post_disconnect
1273 * will only set the new_state. Now free_command_q reacquires the intr
1274 * lock and clears the DISCONNECT_SCHEDULED flag (using PRESERVE_FLAG_
1275 * FIELDS), and the disconnect is lost. This is particularly bad when
1276 * the new disconnect was for UNCONFIGURING, since the unconfigure hangs
1278 * Fix is that free command queue sets acr state and acr flags if there
1279 * is a change under the lock
1280 * note free command queue writes to this state it clears it
1281 * before releasing the lock, different drivers call the free command
1282 * queue different times so dont initialize above
1284 if (vscsi->phyp_acr_state != 0) {
1286 * set any bits in flags that may have been cleared by
1287 * a call to free command queue in switch statement
1290 vscsi->flags |= vscsi->phyp_acr_flags;
1291 ibmvscsis_post_disconnect(vscsi, vscsi->phyp_acr_state, 0);
1292 vscsi->phyp_acr_state = 0;
1293 vscsi->phyp_acr_flags = 0;
1295 pr_debug("adapter_idle: flags 0x%x, state 0x%hx, acr_flags 0x%x, acr_state 0x%hx\n",
1296 vscsi->flags, vscsi->state, vscsi->phyp_acr_flags,
1297 vscsi->phyp_acr_state);
1300 pr_debug("Leaving adapter_idle: flags 0x%x, state 0x%hx, new_state 0x%x\n",
1301 vscsi->flags, vscsi->state, vscsi->new_state);
1305 * ibmvscsis_copy_crq_packet() - Copy CRQ Packet
1306 * @vscsi: Pointer to our adapter structure
1307 * @cmd: Pointer to command element to use to process the request
1308 * @crq: Pointer to CRQ entry containing the request
1310 * Copy the srp information unit from the hosted
1311 * partition using remote dma
1313 * EXECUTION ENVIRONMENT:
1314 * Interrupt, interrupt lock held
1316 static long ibmvscsis_copy_crq_packet(struct scsi_info *vscsi,
1317 struct ibmvscsis_cmd *cmd,
1318 struct viosrp_crq *crq)
1320 struct iu_entry *iue = cmd->iue;
1324 len = be16_to_cpu(crq->IU_length);
1325 if ((len > SRP_MAX_IU_LEN) || (len == 0)) {
1326 dev_err(&vscsi->dev, "copy_crq: Invalid len %d passed", len);
1327 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
1328 return SRP_VIOLATION;
1331 rc = h_copy_rdma(len, vscsi->dds.window[REMOTE].liobn,
1332 be64_to_cpu(crq->IU_data_ptr),
1333 vscsi->dds.window[LOCAL].liobn, iue->sbuf->dma);
1337 cmd->init_time = mftb();
1338 iue->remote_token = crq->IU_data_ptr;
1340 pr_debug("copy_crq: ioba 0x%llx, init_time 0x%llx\n",
1341 be64_to_cpu(crq->IU_data_ptr), cmd->init_time);
1344 if (connection_broken(vscsi))
1345 ibmvscsis_post_disconnect(vscsi,
1346 ERR_DISCONNECT_RECONNECT,
1350 ibmvscsis_post_disconnect(vscsi,
1351 ERR_DISCONNECT_RECONNECT, 0);
1353 dev_err(&vscsi->dev, "copy_crq: h_copy_rdma failed, rc %ld\n",
1359 dev_err(&vscsi->dev, "copy_crq: h_copy_rdma failed, rc %ld\n",
1361 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
1369 * ibmvscsis_adapter_info - Service an Adapter Info MAnagement Data gram
1370 * @vscsi: Pointer to our adapter structure
1371 * @iue: Information Unit containing the Adapter Info MAD request
1373 * EXECUTION ENVIRONMENT:
1374 * Interrupt adapter lock is held
1376 static long ibmvscsis_adapter_info(struct scsi_info *vscsi,
1377 struct iu_entry *iue)
1379 struct viosrp_adapter_info *mad = &vio_iu(iue)->mad.adapter_info;
1380 struct mad_adapter_info_data *info;
1385 mad->common.status = cpu_to_be16(VIOSRP_MAD_SUCCESS);
1387 if (be16_to_cpu(mad->common.length) > sizeof(*info)) {
1388 mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
1392 info = dma_alloc_coherent(&vscsi->dma_dev->dev, sizeof(*info), &token,
1395 dev_err(&vscsi->dev, "bad dma_alloc_coherent %p\n",
1397 mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
1401 /* Get remote info */
1402 rc = h_copy_rdma(be16_to_cpu(mad->common.length),
1403 vscsi->dds.window[REMOTE].liobn,
1404 be64_to_cpu(mad->buffer),
1405 vscsi->dds.window[LOCAL].liobn, token);
1407 if (rc != H_SUCCESS) {
1408 if (rc == H_PERMISSION) {
1409 if (connection_broken(vscsi))
1410 flag_bits = (RESPONSE_Q_DOWN | CLIENT_FAILED);
1412 pr_warn("adapter_info: h_copy_rdma from client failed, rc %ld\n",
1414 pr_debug("adapter_info: ioba 0x%llx, flags 0x%x, flag_bits 0x%x\n",
1415 be64_to_cpu(mad->buffer), vscsi->flags, flag_bits);
1416 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
1422 * Copy client info, but ignore partition number, which we
1423 * already got from phyp - unless we failed to get it from
1424 * phyp (e.g. if we're running on a p5 system).
1426 if (vscsi->client_data.partition_number == 0)
1427 vscsi->client_data.partition_number =
1428 be32_to_cpu(info->partition_number);
1429 strncpy(vscsi->client_data.srp_version, info->srp_version,
1430 sizeof(vscsi->client_data.srp_version));
1431 strncpy(vscsi->client_data.partition_name, info->partition_name,
1432 sizeof(vscsi->client_data.partition_name));
1433 vscsi->client_data.mad_version = be32_to_cpu(info->mad_version);
1434 vscsi->client_data.os_type = be32_to_cpu(info->os_type);
1437 strncpy(info->srp_version, SRP_VERSION,
1438 sizeof(info->srp_version));
1439 strncpy(info->partition_name, vscsi->dds.partition_name,
1440 sizeof(info->partition_name));
1441 info->partition_number = cpu_to_be32(vscsi->dds.partition_num);
1442 info->mad_version = cpu_to_be32(MAD_VERSION_1);
1443 info->os_type = cpu_to_be32(LINUX);
1444 memset(&info->port_max_txu[0], 0, sizeof(info->port_max_txu));
1445 info->port_max_txu[0] = cpu_to_be32(128 * PAGE_SIZE);
1448 rc = h_copy_rdma(sizeof(*info), vscsi->dds.window[LOCAL].liobn,
1449 token, vscsi->dds.window[REMOTE].liobn,
1450 be64_to_cpu(mad->buffer));
1458 if (connection_broken(vscsi))
1459 flag_bits = (RESPONSE_Q_DOWN | CLIENT_FAILED);
1461 dev_err(&vscsi->dev, "adapter_info: h_copy_rdma to client failed, rc %ld\n",
1463 ibmvscsis_post_disconnect(vscsi,
1464 ERR_DISCONNECT_RECONNECT,
1470 dma_free_coherent(&vscsi->dma_dev->dev, sizeof(*info), info, token);
1471 pr_debug("Leaving adapter_info, rc %ld\n", rc);
1477 * ibmvscsis_cap_mad() - Service a Capabilities MAnagement Data gram
1478 * @vscsi: Pointer to our adapter structure
1479 * @iue: Information Unit containing the Capabilities MAD request
1481 * NOTE: if you return an error from this routine you must be
1482 * disconnecting or you will cause a hang
1484 * EXECUTION ENVIRONMENT:
1485 * Interrupt called with adapter lock held
1487 static int ibmvscsis_cap_mad(struct scsi_info *vscsi, struct iu_entry *iue)
1489 struct viosrp_capabilities *mad = &vio_iu(iue)->mad.capabilities;
1490 struct capabilities *cap;
1491 struct mad_capability_common *common;
1493 u16 olen, len, status, min_len, cap_len;
1498 olen = be16_to_cpu(mad->common.length);
1500 * struct capabilities hardcodes a couple capabilities after the
1501 * header, but the capabilities can actually be in any order.
1503 min_len = offsetof(struct capabilities, migration);
1504 if ((olen < min_len) || (olen > PAGE_SIZE)) {
1505 pr_warn("cap_mad: invalid len %d\n", olen);
1506 mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
1510 cap = dma_alloc_coherent(&vscsi->dma_dev->dev, olen, &token,
1513 dev_err(&vscsi->dev, "bad dma_alloc_coherent %p\n",
1515 mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
1518 rc = h_copy_rdma(olen, vscsi->dds.window[REMOTE].liobn,
1519 be64_to_cpu(mad->buffer),
1520 vscsi->dds.window[LOCAL].liobn, token);
1521 if (rc == H_SUCCESS) {
1522 strncpy(cap->name, dev_name(&vscsi->dma_dev->dev),
1525 len = olen - min_len;
1526 status = VIOSRP_MAD_SUCCESS;
1527 common = (struct mad_capability_common *)&cap->migration;
1529 while ((len > 0) && (status == VIOSRP_MAD_SUCCESS) && !rc) {
1530 pr_debug("cap_mad: len left %hd, cap type %d, cap len %hd\n",
1531 len, be32_to_cpu(common->cap_type),
1532 be16_to_cpu(common->length));
1534 cap_len = be16_to_cpu(common->length);
1535 if (cap_len > len) {
1536 dev_err(&vscsi->dev, "cap_mad: cap len mismatch with total len\n");
1537 status = VIOSRP_MAD_FAILED;
1542 dev_err(&vscsi->dev, "cap_mad: cap len is 0\n");
1543 status = VIOSRP_MAD_FAILED;
1547 switch (common->cap_type) {
1549 pr_debug("cap_mad: unsupported capability\n");
1550 common->server_support = 0;
1551 flag = cpu_to_be32((u32)CAP_LIST_SUPPORTED);
1552 cap->flags &= ~flag;
1556 len = len - cap_len;
1557 common = (struct mad_capability_common *)
1558 ((char *)common + cap_len);
1561 mad->common.status = cpu_to_be16(status);
1564 rc = h_copy_rdma(olen, vscsi->dds.window[LOCAL].liobn, token,
1565 vscsi->dds.window[REMOTE].liobn,
1566 be64_to_cpu(mad->buffer));
1568 if (rc != H_SUCCESS) {
1569 pr_debug("cap_mad: failed to copy to client, rc %ld\n",
1572 if (rc == H_PERMISSION) {
1573 if (connection_broken(vscsi))
1574 flag_bits = (RESPONSE_Q_DOWN |
1578 pr_warn("cap_mad: error copying data to client, rc %ld\n",
1580 ibmvscsis_post_disconnect(vscsi,
1581 ERR_DISCONNECT_RECONNECT,
1586 dma_free_coherent(&vscsi->dma_dev->dev, olen, cap, token);
1588 pr_debug("Leaving cap_mad, rc %ld, client_cap 0x%x\n",
1589 rc, vscsi->client_cap);
1595 * ibmvscsis_process_mad() - Service a MAnagement Data gram
1596 * @vscsi: Pointer to our adapter structure
1597 * @iue: Information Unit containing the MAD request
1599 * Must be called with interrupt lock held.
1601 static long ibmvscsis_process_mad(struct scsi_info *vscsi, struct iu_entry *iue)
1603 struct mad_common *mad = (struct mad_common *)&vio_iu(iue)->mad;
1604 struct viosrp_empty_iu *empty;
1605 long rc = ADAPT_SUCCESS;
1607 switch (be32_to_cpu(mad->type)) {
1608 case VIOSRP_EMPTY_IU_TYPE:
1609 empty = &vio_iu(iue)->mad.empty_iu;
1610 vscsi->empty_iu_id = be64_to_cpu(empty->buffer);
1611 vscsi->empty_iu_tag = be64_to_cpu(empty->common.tag);
1612 mad->status = cpu_to_be16(VIOSRP_MAD_SUCCESS);
1614 case VIOSRP_ADAPTER_INFO_TYPE:
1615 rc = ibmvscsis_adapter_info(vscsi, iue);
1617 case VIOSRP_CAPABILITIES_TYPE:
1618 rc = ibmvscsis_cap_mad(vscsi, iue);
1620 case VIOSRP_ENABLE_FAST_FAIL:
1621 if (vscsi->state == CONNECTED) {
1622 vscsi->fast_fail = true;
1623 mad->status = cpu_to_be16(VIOSRP_MAD_SUCCESS);
1625 pr_warn("fast fail mad sent after login\n");
1626 mad->status = cpu_to_be16(VIOSRP_MAD_FAILED);
1630 mad->status = cpu_to_be16(VIOSRP_MAD_NOT_SUPPORTED);
1638 * srp_snd_msg_failed() - Handle an error when sending a response
1639 * @vscsi: Pointer to our adapter structure
1640 * @rc: The return code from the h_send_crq command
1642 * Must be called with interrupt lock held.
1644 static void srp_snd_msg_failed(struct scsi_info *vscsi, long rc)
1648 if (rc != H_DROPPED) {
1649 ibmvscsis_free_cmd_qs(vscsi);
1652 vscsi->flags |= CLIENT_FAILED;
1654 /* don't flag the same problem multiple times */
1655 if (!(vscsi->flags & RESPONSE_Q_DOWN)) {
1656 vscsi->flags |= RESPONSE_Q_DOWN;
1657 if (!(vscsi->state & (ERR_DISCONNECT |
1658 ERR_DISCONNECT_RECONNECT |
1659 ERR_DISCONNECTED | UNDEFINED))) {
1660 dev_err(&vscsi->dev, "snd_msg_failed: setting RESPONSE_Q_DOWN, state 0x%hx, flags 0x%x, rc %ld\n",
1661 vscsi->state, vscsi->flags, rc);
1663 ibmvscsis_post_disconnect(vscsi,
1664 ERR_DISCONNECT_RECONNECT, 0);
1670 * The response queue is full.
1671 * If the server is processing SRP requests, i.e.
1672 * the client has successfully done an
1673 * SRP_LOGIN, then it will wait forever for room in
1674 * the queue. However if the system admin
1675 * is attempting to unconfigure the server then one
1676 * or more children will be in a state where
1677 * they are being removed. So if there is even one
1678 * child being removed then the driver assumes
1679 * the system admin is attempting to break the
1680 * connection with the client and MAX_TIMER_POPS
1683 if ((vscsi->rsp_q_timer.timer_pops < MAX_TIMER_POPS) ||
1684 (vscsi->state == SRP_PROCESSING)) {
1685 pr_debug("snd_msg_failed: response queue full, flags 0x%x, timer started %d, pops %d\n",
1686 vscsi->flags, (int)vscsi->rsp_q_timer.started,
1687 vscsi->rsp_q_timer.timer_pops);
1690 * Check if the timer is running; if it
1691 * is not then start it up.
1693 if (!vscsi->rsp_q_timer.started) {
1694 if (vscsi->rsp_q_timer.timer_pops <
1696 kt = ktime_set(0, WAIT_NANO_SECONDS);
1699 * slide the timeslice if the maximum
1700 * timer pops have already happened
1702 kt = ktime_set(WAIT_SECONDS, 0);
1705 vscsi->rsp_q_timer.started = true;
1706 hrtimer_start(&vscsi->rsp_q_timer.timer, kt,
1711 * TBD: Do we need to worry about this? Need to get
1715 * waited a long time and it appears the system admin
1716 * is bring this driver down
1718 vscsi->flags |= RESPONSE_Q_DOWN;
1719 ibmvscsis_free_cmd_qs(vscsi);
1721 * if the driver is already attempting to disconnect
1722 * from the client and has already logged an error
1723 * trace this event but don't put it in the error log
1725 if (!(vscsi->state & (ERR_DISCONNECT |
1726 ERR_DISCONNECT_RECONNECT |
1727 ERR_DISCONNECTED | UNDEFINED))) {
1728 dev_err(&vscsi->dev, "client crq full too long\n");
1729 ibmvscsis_post_disconnect(vscsi,
1730 ERR_DISCONNECT_RECONNECT,
1737 * ibmvscsis_send_messages() - Send a Response
1738 * @vscsi: Pointer to our adapter structure
1740 * Send a response, first checking the waiting queue. Responses are
1741 * sent in order they are received. If the response cannot be sent,
1742 * because the client queue is full, it stays on the waiting queue.
1745 * Called with interrupt lock held
1747 static void ibmvscsis_send_messages(struct scsi_info *vscsi)
1750 /* note do not attmempt to access the IU_data_ptr with this pointer
1753 struct viosrp_crq *crq = (struct viosrp_crq *)&msg_hi;
1754 struct ibmvscsis_cmd *cmd, *nxt;
1755 struct iu_entry *iue;
1756 long rc = ADAPT_SUCCESS;
1758 if (!(vscsi->flags & RESPONSE_Q_DOWN)) {
1759 list_for_each_entry_safe(cmd, nxt, &vscsi->waiting_rsp, list) {
1762 crq->valid = VALID_CMD_RESP_EL;
1763 crq->format = cmd->rsp.format;
1765 if (cmd->flags & CMD_FAST_FAIL)
1766 crq->status = VIOSRP_ADAPTER_FAIL;
1768 crq->IU_length = cpu_to_be16(cmd->rsp.len);
1770 rc = h_send_crq(vscsi->dma_dev->unit_address,
1771 be64_to_cpu(msg_hi),
1772 be64_to_cpu(cmd->rsp.tag));
1774 pr_debug("send_messages: cmd %p, tag 0x%llx, rc %ld\n",
1775 cmd, be64_to_cpu(cmd->rsp.tag), rc);
1777 /* if all ok free up the command element resources */
1778 if (rc == H_SUCCESS) {
1779 /* some movement has occurred */
1780 vscsi->rsp_q_timer.timer_pops = 0;
1781 list_del(&cmd->list);
1783 ibmvscsis_free_cmd_resources(vscsi, cmd);
1785 srp_snd_msg_failed(vscsi, rc);
1792 * The timer could pop with the queue empty. If
1793 * this happens, rc will always indicate a
1794 * success; clear the pop count.
1796 vscsi->rsp_q_timer.timer_pops = 0;
1799 ibmvscsis_free_cmd_qs(vscsi);
1803 /* Called with intr lock held */
1804 static void ibmvscsis_send_mad_resp(struct scsi_info *vscsi,
1805 struct ibmvscsis_cmd *cmd,
1806 struct viosrp_crq *crq)
1808 struct iu_entry *iue = cmd->iue;
1809 struct mad_common *mad = (struct mad_common *)&vio_iu(iue)->mad;
1814 rc = h_copy_rdma(sizeof(struct mad_common),
1815 vscsi->dds.window[LOCAL].liobn, iue->sbuf->dma,
1816 vscsi->dds.window[REMOTE].liobn,
1817 be64_to_cpu(crq->IU_data_ptr));
1819 cmd->rsp.format = VIOSRP_MAD_FORMAT;
1820 cmd->rsp.len = sizeof(struct mad_common);
1821 cmd->rsp.tag = mad->tag;
1822 list_add_tail(&cmd->list, &vscsi->waiting_rsp);
1823 ibmvscsis_send_messages(vscsi);
1825 pr_debug("Error sending mad response, rc %ld\n", rc);
1826 if (rc == H_PERMISSION) {
1827 if (connection_broken(vscsi))
1828 flag_bits = (RESPONSE_Q_DOWN | CLIENT_FAILED);
1830 dev_err(&vscsi->dev, "mad: failed to copy to client, rc %ld\n",
1833 ibmvscsis_free_cmd_resources(vscsi, cmd);
1834 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
1840 * ibmvscsis_mad() - Service a MAnagement Data gram.
1841 * @vscsi: Pointer to our adapter structure
1842 * @crq: Pointer to the CRQ entry containing the MAD request
1844 * EXECUTION ENVIRONMENT:
1845 * Interrupt, called with adapter lock held
1847 static long ibmvscsis_mad(struct scsi_info *vscsi, struct viosrp_crq *crq)
1849 struct iu_entry *iue;
1850 struct ibmvscsis_cmd *cmd;
1851 struct mad_common *mad;
1852 long rc = ADAPT_SUCCESS;
1854 switch (vscsi->state) {
1856 * We have not exchanged Init Msgs yet, so this MAD was sent
1857 * before the last Transport Event; client will not be
1858 * expecting a response.
1860 case WAIT_CONNECTION:
1861 pr_debug("mad: in Wait Connection state, ignoring MAD, flags %d\n",
1863 return ADAPT_SUCCESS;
1865 case SRP_PROCESSING:
1870 * We should never get here while we're in these states.
1871 * Just log an error and get out.
1875 case ERR_DISCONNECT:
1876 case ERR_DISCONNECT_RECONNECT:
1878 dev_err(&vscsi->dev, "mad: invalid adapter state %d for mad\n",
1880 return ADAPT_SUCCESS;
1883 cmd = ibmvscsis_get_free_cmd(vscsi);
1885 dev_err(&vscsi->dev, "mad: failed to get cmd, debit %d\n",
1887 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
1891 cmd->type = ADAPTER_MAD;
1893 rc = ibmvscsis_copy_crq_packet(vscsi, cmd, crq);
1895 mad = (struct mad_common *)&vio_iu(iue)->mad;
1897 pr_debug("mad: type %d\n", be32_to_cpu(mad->type));
1899 rc = ibmvscsis_process_mad(vscsi, iue);
1901 pr_debug("mad: status %hd, rc %ld\n", be16_to_cpu(mad->status),
1905 ibmvscsis_send_mad_resp(vscsi, cmd, crq);
1907 ibmvscsis_free_cmd_resources(vscsi, cmd);
1910 pr_debug("Leaving mad, rc %ld\n", rc);
1915 * ibmvscsis_login_rsp() - Create/copy a login response notice to the client
1916 * @vscsi: Pointer to our adapter structure
1917 * @cmd: Pointer to the command for the SRP Login request
1919 * EXECUTION ENVIRONMENT:
1920 * Interrupt, interrupt lock held
1922 static long ibmvscsis_login_rsp(struct scsi_info *vscsi,
1923 struct ibmvscsis_cmd *cmd)
1925 struct iu_entry *iue = cmd->iue;
1926 struct srp_login_rsp *rsp = &vio_iu(iue)->srp.login_rsp;
1927 struct format_code *fmt;
1929 long rc = ADAPT_SUCCESS;
1931 memset(rsp, 0, sizeof(struct srp_login_rsp));
1933 rsp->opcode = SRP_LOGIN_RSP;
1934 rsp->req_lim_delta = cpu_to_be32(vscsi->request_limit);
1935 rsp->tag = cmd->rsp.tag;
1936 rsp->max_it_iu_len = cpu_to_be32(SRP_MAX_IU_LEN);
1937 rsp->max_ti_iu_len = cpu_to_be32(SRP_MAX_IU_LEN);
1938 fmt = (struct format_code *)&rsp->buf_fmt;
1939 fmt->buffers = SUPPORTED_FORMATS;
1942 cmd->rsp.len = sizeof(struct srp_login_rsp);
1945 rc = h_copy_rdma(cmd->rsp.len, vscsi->dds.window[LOCAL].liobn,
1946 iue->sbuf->dma, vscsi->dds.window[REMOTE].liobn,
1947 be64_to_cpu(iue->remote_token));
1954 if (connection_broken(vscsi))
1955 flag_bits = RESPONSE_Q_DOWN | CLIENT_FAILED;
1956 dev_err(&vscsi->dev, "login_rsp: error copying to client, rc %ld\n",
1958 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
1964 dev_err(&vscsi->dev, "login_rsp: error copying to client, rc %ld\n",
1966 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
1974 * ibmvscsis_srp_login_rej() - Create/copy a login rejection notice to client
1975 * @vscsi: Pointer to our adapter structure
1976 * @cmd: Pointer to the command for the SRP Login request
1977 * @reason: The reason the SRP Login is being rejected, per SRP protocol
1979 * EXECUTION ENVIRONMENT:
1980 * Interrupt, interrupt lock held
1982 static long ibmvscsis_srp_login_rej(struct scsi_info *vscsi,
1983 struct ibmvscsis_cmd *cmd, u32 reason)
1985 struct iu_entry *iue = cmd->iue;
1986 struct srp_login_rej *rej = &vio_iu(iue)->srp.login_rej;
1987 struct format_code *fmt;
1989 long rc = ADAPT_SUCCESS;
1991 memset(rej, 0, sizeof(*rej));
1993 rej->opcode = SRP_LOGIN_REJ;
1994 rej->reason = cpu_to_be32(reason);
1995 rej->tag = cmd->rsp.tag;
1996 fmt = (struct format_code *)&rej->buf_fmt;
1997 fmt->buffers = SUPPORTED_FORMATS;
1999 cmd->rsp.len = sizeof(*rej);
2002 rc = h_copy_rdma(cmd->rsp.len, vscsi->dds.window[LOCAL].liobn,
2003 iue->sbuf->dma, vscsi->dds.window[REMOTE].liobn,
2004 be64_to_cpu(iue->remote_token));
2010 if (connection_broken(vscsi))
2011 flag_bits = RESPONSE_Q_DOWN | CLIENT_FAILED;
2012 dev_err(&vscsi->dev, "login_rej: error copying to client, rc %ld\n",
2014 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
2020 dev_err(&vscsi->dev, "login_rej: error copying to client, rc %ld\n",
2022 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2029 static int ibmvscsis_make_nexus(struct ibmvscsis_tport *tport)
2031 char *name = tport->tport_name;
2032 struct ibmvscsis_nexus *nexus;
2035 if (tport->ibmv_nexus) {
2036 pr_debug("tport->ibmv_nexus already exists\n");
2040 nexus = kzalloc(sizeof(*nexus), GFP_KERNEL);
2042 pr_err("Unable to allocate struct ibmvscsis_nexus\n");
2046 nexus->se_sess = target_alloc_session(&tport->se_tpg, 0, 0,
2047 TARGET_PROT_NORMAL, name, nexus,
2049 if (IS_ERR(nexus->se_sess)) {
2050 rc = PTR_ERR(nexus->se_sess);
2051 goto transport_init_fail;
2054 tport->ibmv_nexus = nexus;
2058 transport_init_fail:
2063 static int ibmvscsis_drop_nexus(struct ibmvscsis_tport *tport)
2065 struct se_session *se_sess;
2066 struct ibmvscsis_nexus *nexus;
2068 nexus = tport->ibmv_nexus;
2072 se_sess = nexus->se_sess;
2077 * Release the SCSI I_T Nexus to the emulated ibmvscsis Target Port
2079 target_wait_for_sess_cmds(se_sess);
2080 transport_deregister_session_configfs(se_sess);
2081 transport_deregister_session(se_sess);
2082 tport->ibmv_nexus = NULL;
2089 * ibmvscsis_srp_login() - Process an SRP Login Request
2090 * @vscsi: Pointer to our adapter structure
2091 * @cmd: Command element to use to process the SRP Login request
2092 * @crq: Pointer to CRQ entry containing the SRP Login request
2094 * EXECUTION ENVIRONMENT:
2095 * Interrupt, called with interrupt lock held
2097 static long ibmvscsis_srp_login(struct scsi_info *vscsi,
2098 struct ibmvscsis_cmd *cmd,
2099 struct viosrp_crq *crq)
2101 struct iu_entry *iue = cmd->iue;
2102 struct srp_login_req *req = &vio_iu(iue)->srp.login_req;
2104 __be64 id_extension;
2107 struct format_code *fmt;
2109 long rc = ADAPT_SUCCESS;
2111 iport = (struct port_id *)req->initiator_port_id;
2112 tport = (struct port_id *)req->target_port_id;
2113 fmt = (struct format_code *)&req->req_buf_fmt;
2114 if (be32_to_cpu(req->req_it_iu_len) > SRP_MAX_IU_LEN)
2115 reason = SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE;
2116 else if (be32_to_cpu(req->req_it_iu_len) < 64)
2117 reason = SRP_LOGIN_REJ_UNABLE_ESTABLISH_CHANNEL;
2118 else if ((be64_to_cpu(iport->id_extension) > (MAX_NUM_PORTS - 1)) ||
2119 (be64_to_cpu(tport->id_extension) > (MAX_NUM_PORTS - 1)))
2120 reason = SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL;
2121 else if (req->req_flags & SRP_MULTICHAN_MULTI)
2122 reason = SRP_LOGIN_REJ_MULTI_CHANNEL_UNSUPPORTED;
2123 else if (fmt->buffers & (~SUPPORTED_FORMATS))
2124 reason = SRP_LOGIN_REJ_UNSUPPORTED_DESCRIPTOR_FMT;
2125 else if ((fmt->buffers & SUPPORTED_FORMATS) == 0)
2126 reason = SRP_LOGIN_REJ_UNSUPPORTED_DESCRIPTOR_FMT;
2128 if (vscsi->state == SRP_PROCESSING)
2129 reason = SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED;
2131 rc = ibmvscsis_make_nexus(&vscsi->tport);
2133 reason = SRP_LOGIN_REJ_UNABLE_ESTABLISH_CHANNEL;
2135 cmd->rsp.format = VIOSRP_SRP_FORMAT;
2136 cmd->rsp.tag = req->tag;
2138 pr_debug("srp_login: reason 0x%x\n", reason);
2141 rc = ibmvscsis_srp_login_rej(vscsi, cmd, reason);
2143 rc = ibmvscsis_login_rsp(vscsi, cmd);
2147 vscsi->state = SRP_PROCESSING;
2149 list_add_tail(&cmd->list, &vscsi->waiting_rsp);
2150 ibmvscsis_send_messages(vscsi);
2152 ibmvscsis_free_cmd_resources(vscsi, cmd);
2155 pr_debug("Leaving srp_login, rc %ld\n", rc);
2160 * ibmvscsis_srp_i_logout() - Helper Function to close I_T Nexus
2161 * @vscsi: Pointer to our adapter structure
2162 * @cmd: Command element to use to process the Implicit Logout request
2163 * @crq: Pointer to CRQ entry containing the Implicit Logout request
2165 * Do the logic to close the I_T nexus. This function may not
2166 * behave to specification.
2168 * EXECUTION ENVIRONMENT:
2169 * Interrupt, interrupt lock held
2171 static long ibmvscsis_srp_i_logout(struct scsi_info *vscsi,
2172 struct ibmvscsis_cmd *cmd,
2173 struct viosrp_crq *crq)
2175 struct iu_entry *iue = cmd->iue;
2176 struct srp_i_logout *log_out = &vio_iu(iue)->srp.i_logout;
2177 long rc = ADAPT_SUCCESS;
2179 if ((vscsi->debit > 0) || !list_empty(&vscsi->schedule_q) ||
2180 !list_empty(&vscsi->waiting_rsp)) {
2181 dev_err(&vscsi->dev, "i_logout: outstanding work\n");
2182 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
2184 cmd->rsp.format = SRP_FORMAT;
2185 cmd->rsp.tag = log_out->tag;
2186 cmd->rsp.len = sizeof(struct mad_common);
2187 list_add_tail(&cmd->list, &vscsi->waiting_rsp);
2188 ibmvscsis_send_messages(vscsi);
2190 ibmvscsis_post_disconnect(vscsi, WAIT_IDLE, 0);
2196 /* Called with intr lock held */
2197 static void ibmvscsis_srp_cmd(struct scsi_info *vscsi, struct viosrp_crq *crq)
2199 struct ibmvscsis_cmd *cmd;
2200 struct iu_entry *iue;
2201 struct srp_cmd *srp;
2202 struct srp_tsk_mgmt *tsk;
2205 if (vscsi->request_limit - vscsi->debit <= 0) {
2206 /* Client has exceeded request limit */
2207 dev_err(&vscsi->dev, "Client exceeded the request limit (%d), debit %d\n",
2208 vscsi->request_limit, vscsi->debit);
2209 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2213 cmd = ibmvscsis_get_free_cmd(vscsi);
2215 dev_err(&vscsi->dev, "srp_cmd failed to get cmd, debit %d\n",
2217 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2221 srp = &vio_iu(iue)->srp.cmd;
2223 rc = ibmvscsis_copy_crq_packet(vscsi, cmd, crq);
2225 ibmvscsis_free_cmd_resources(vscsi, cmd);
2229 if (vscsi->state == SRP_PROCESSING) {
2230 switch (srp->opcode) {
2232 rc = ibmvscsis_srp_login(vscsi, cmd, crq);
2236 tsk = &vio_iu(iue)->srp.tsk_mgmt;
2237 pr_debug("tsk_mgmt tag: %llu (0x%llx)\n", tsk->tag,
2239 cmd->rsp.tag = tsk->tag;
2241 cmd->type = TASK_MANAGEMENT;
2242 list_add_tail(&cmd->list, &vscsi->schedule_q);
2243 queue_work(vscsi->work_q, &cmd->work);
2247 pr_debug("srp_cmd tag: %llu (0x%llx)\n", srp->tag,
2249 cmd->rsp.tag = srp->tag;
2251 cmd->type = SCSI_CDB;
2253 * We want to keep track of work waiting for
2256 list_add_tail(&cmd->list, &vscsi->schedule_q);
2257 queue_work(vscsi->work_q, &cmd->work);
2261 rc = ibmvscsis_srp_i_logout(vscsi, cmd, crq);
2267 ibmvscsis_free_cmd_resources(vscsi, cmd);
2268 dev_err(&vscsi->dev, "invalid srp cmd, opcode %d\n",
2270 ibmvscsis_post_disconnect(vscsi,
2271 ERR_DISCONNECT_RECONNECT, 0);
2274 } else if (srp->opcode == SRP_LOGIN_REQ && vscsi->state == CONNECTED) {
2275 rc = ibmvscsis_srp_login(vscsi, cmd, crq);
2277 ibmvscsis_free_cmd_resources(vscsi, cmd);
2278 dev_err(&vscsi->dev, "Invalid state %d to handle srp cmd\n",
2280 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2285 * ibmvscsis_ping_response() - Respond to a ping request
2286 * @vscsi: Pointer to our adapter structure
2288 * Let the client know that the server is alive and waiting on
2289 * its native I/O stack.
2290 * If any type of error occurs from the call to queue a ping
2291 * response then the client is either not accepting or receiving
2292 * interrupts. Disconnect with an error.
2294 * EXECUTION ENVIRONMENT:
2295 * Interrupt, interrupt lock held
2297 static long ibmvscsis_ping_response(struct scsi_info *vscsi)
2299 struct viosrp_crq *crq;
2300 u64 buffer[2] = { 0, 0 };
2303 crq = (struct viosrp_crq *)&buffer;
2304 crq->valid = VALID_CMD_RESP_EL;
2305 crq->format = (u8)MESSAGE_IN_CRQ;
2306 crq->status = PING_RESPONSE;
2308 rc = h_send_crq(vscsi->dds.unit_id, cpu_to_be64(buffer[MSG_HI]),
2309 cpu_to_be64(buffer[MSG_LOW]));
2315 vscsi->flags |= CLIENT_FAILED;
2317 vscsi->flags |= RESPONSE_Q_DOWN;
2319 dev_err(&vscsi->dev, "ping_response: h_send_crq failed, rc %ld\n",
2321 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2324 dev_err(&vscsi->dev, "ping_response: h_send_crq returned unknown rc %ld\n",
2326 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
2334 * ibmvscsis_parse_command() - Parse an element taken from the cmd rsp queue.
2335 * @vscsi: Pointer to our adapter structure
2336 * @crq: Pointer to CRQ element containing the SRP request
2338 * This function will return success if the command queue element is valid
2339 * and the srp iu or MAD request it pointed to was also valid. That does
2340 * not mean that an error was not returned to the client.
2342 * EXECUTION ENVIRONMENT:
2343 * Interrupt, intr lock held
2345 static long ibmvscsis_parse_command(struct scsi_info *vscsi,
2346 struct viosrp_crq *crq)
2348 long rc = ADAPT_SUCCESS;
2350 switch (crq->valid) {
2351 case VALID_CMD_RESP_EL:
2352 switch (crq->format) {
2357 if (vscsi->flags & PROCESSING_MAD) {
2359 dev_err(&vscsi->dev, "parse_command: already processing mad\n");
2360 ibmvscsis_post_disconnect(vscsi,
2361 ERR_DISCONNECT_RECONNECT,
2364 vscsi->flags |= PROCESSING_MAD;
2365 rc = ibmvscsis_mad(vscsi, crq);
2370 ibmvscsis_srp_cmd(vscsi, crq);
2373 case MESSAGE_IN_CRQ:
2374 if (crq->status == PING)
2375 ibmvscsis_ping_response(vscsi);
2379 dev_err(&vscsi->dev, "parse_command: invalid format %d\n",
2381 ibmvscsis_post_disconnect(vscsi,
2382 ERR_DISCONNECT_RECONNECT, 0);
2387 case VALID_TRANS_EVENT:
2388 rc = ibmvscsis_trans_event(vscsi, crq);
2391 case VALID_INIT_MSG:
2392 rc = ibmvscsis_init_msg(vscsi, crq);
2396 dev_err(&vscsi->dev, "parse_command: invalid valid field %d\n",
2398 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2403 * Return only what the interrupt handler cares
2404 * about. Most errors we keep right on trucking.
2406 rc = vscsi->flags & SCHEDULE_DISCONNECT;
2411 static int read_dma_window(struct scsi_info *vscsi)
2413 struct vio_dev *vdev = vscsi->dma_dev;
2414 const __be32 *dma_window;
2417 /* TODO Using of_parse_dma_window would be better, but it doesn't give
2418 * a way to read multiple windows without already knowing the size of
2419 * a window or the number of windows.
2421 dma_window = (const __be32 *)vio_get_attribute(vdev,
2422 "ibm,my-dma-window",
2425 pr_err("Couldn't find ibm,my-dma-window property\n");
2429 vscsi->dds.window[LOCAL].liobn = be32_to_cpu(*dma_window);
2432 prop = (const __be32 *)vio_get_attribute(vdev, "ibm,#dma-address-cells",
2435 pr_warn("Couldn't find ibm,#dma-address-cells property\n");
2438 dma_window += be32_to_cpu(*prop);
2441 prop = (const __be32 *)vio_get_attribute(vdev, "ibm,#dma-size-cells",
2444 pr_warn("Couldn't find ibm,#dma-size-cells property\n");
2447 dma_window += be32_to_cpu(*prop);
2450 /* dma_window should point to the second window now */
2451 vscsi->dds.window[REMOTE].liobn = be32_to_cpu(*dma_window);
2456 static struct ibmvscsis_tport *ibmvscsis_lookup_port(const char *name)
2458 struct ibmvscsis_tport *tport = NULL;
2459 struct vio_dev *vdev;
2460 struct scsi_info *vscsi;
2462 spin_lock_bh(&ibmvscsis_dev_lock);
2463 list_for_each_entry(vscsi, &ibmvscsis_dev_list, list) {
2464 vdev = vscsi->dma_dev;
2465 if (!strcmp(dev_name(&vdev->dev), name)) {
2466 tport = &vscsi->tport;
2470 spin_unlock_bh(&ibmvscsis_dev_lock);
2476 * ibmvscsis_parse_cmd() - Parse SRP Command
2477 * @vscsi: Pointer to our adapter structure
2478 * @cmd: Pointer to command element with SRP command
2480 * Parse the srp command; if it is valid then submit it to tcm.
2481 * Note: The return code does not reflect the status of the SCSI CDB.
2483 * EXECUTION ENVIRONMENT:
2486 static void ibmvscsis_parse_cmd(struct scsi_info *vscsi,
2487 struct ibmvscsis_cmd *cmd)
2489 struct iu_entry *iue = cmd->iue;
2490 struct srp_cmd *srp = (struct srp_cmd *)iue->sbuf->buf;
2491 struct ibmvscsis_nexus *nexus;
2493 enum dma_data_direction dir;
2497 nexus = vscsi->tport.ibmv_nexus;
2499 * additional length in bytes. Note that the SRP spec says that
2500 * additional length is in 4-byte words, but technically the
2501 * additional length field is only the upper 6 bits of the byte.
2502 * The lower 2 bits are reserved. If the lower 2 bits are 0 (as
2503 * all reserved fields should be), then interpreting the byte as
2504 * an int will yield the length in bytes.
2506 if (srp->add_cdb_len & 0x03) {
2507 dev_err(&vscsi->dev, "parse_cmd: reserved bits set in IU\n");
2508 spin_lock_bh(&vscsi->intr_lock);
2509 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2510 ibmvscsis_free_cmd_resources(vscsi, cmd);
2511 spin_unlock_bh(&vscsi->intr_lock);
2515 if (srp_get_desc_table(srp, &dir, &data_len)) {
2516 dev_err(&vscsi->dev, "0x%llx: parsing SRP descriptor table failed.\n",
2521 cmd->rsp.sol_not = srp->sol_not;
2523 switch (srp->task_attr) {
2524 case SRP_SIMPLE_TASK:
2525 attr = TCM_SIMPLE_TAG;
2527 case SRP_ORDERED_TASK:
2528 attr = TCM_ORDERED_TAG;
2531 attr = TCM_HEAD_TAG;
2537 dev_err(&vscsi->dev, "Invalid task attribute %d\n",
2542 cmd->se_cmd.tag = be64_to_cpu(srp->tag);
2544 spin_lock_bh(&vscsi->intr_lock);
2545 list_add_tail(&cmd->list, &vscsi->active_q);
2546 spin_unlock_bh(&vscsi->intr_lock);
2548 srp->lun.scsi_lun[0] &= 0x3f;
2550 rc = target_submit_cmd(&cmd->se_cmd, nexus->se_sess, srp->cdb,
2551 cmd->sense_buf, scsilun_to_int(&srp->lun),
2552 data_len, attr, dir, 0);
2554 dev_err(&vscsi->dev, "target_submit_cmd failed, rc %d\n", rc);
2555 spin_lock_bh(&vscsi->intr_lock);
2556 list_del(&cmd->list);
2557 ibmvscsis_free_cmd_resources(vscsi, cmd);
2558 spin_unlock_bh(&vscsi->intr_lock);
2564 spin_lock_bh(&vscsi->intr_lock);
2565 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2566 spin_unlock_bh(&vscsi->intr_lock);
2570 * ibmvscsis_parse_task() - Parse SRP Task Management Request
2571 * @vscsi: Pointer to our adapter structure
2572 * @cmd: Pointer to command element with SRP task management request
2574 * Parse the srp task management request; if it is valid then submit it to tcm.
2575 * Note: The return code does not reflect the status of the task management
2578 * EXECUTION ENVIRONMENT:
2581 static void ibmvscsis_parse_task(struct scsi_info *vscsi,
2582 struct ibmvscsis_cmd *cmd)
2584 struct iu_entry *iue = cmd->iue;
2585 struct srp_tsk_mgmt *srp_tsk = &vio_iu(iue)->srp.tsk_mgmt;
2587 u64 tag_to_abort = 0;
2589 struct ibmvscsis_nexus *nexus;
2591 nexus = vscsi->tport.ibmv_nexus;
2593 cmd->rsp.sol_not = srp_tsk->sol_not;
2595 switch (srp_tsk->tsk_mgmt_func) {
2596 case SRP_TSK_ABORT_TASK:
2597 tcm_type = TMR_ABORT_TASK;
2598 tag_to_abort = be64_to_cpu(srp_tsk->task_tag);
2600 case SRP_TSK_ABORT_TASK_SET:
2601 tcm_type = TMR_ABORT_TASK_SET;
2603 case SRP_TSK_CLEAR_TASK_SET:
2604 tcm_type = TMR_CLEAR_TASK_SET;
2606 case SRP_TSK_LUN_RESET:
2607 tcm_type = TMR_LUN_RESET;
2609 case SRP_TSK_CLEAR_ACA:
2610 tcm_type = TMR_CLEAR_ACA;
2613 dev_err(&vscsi->dev, "unknown task mgmt func %d\n",
2614 srp_tsk->tsk_mgmt_func);
2615 cmd->se_cmd.se_tmr_req->response =
2616 TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
2622 cmd->se_cmd.tag = be64_to_cpu(srp_tsk->tag);
2624 spin_lock_bh(&vscsi->intr_lock);
2625 list_add_tail(&cmd->list, &vscsi->active_q);
2626 spin_unlock_bh(&vscsi->intr_lock);
2628 srp_tsk->lun.scsi_lun[0] &= 0x3f;
2630 pr_debug("calling submit_tmr, func %d\n",
2631 srp_tsk->tsk_mgmt_func);
2632 rc = target_submit_tmr(&cmd->se_cmd, nexus->se_sess, NULL,
2633 scsilun_to_int(&srp_tsk->lun), srp_tsk,
2634 tcm_type, GFP_KERNEL, tag_to_abort, 0);
2636 dev_err(&vscsi->dev, "target_submit_tmr failed, rc %d\n",
2638 spin_lock_bh(&vscsi->intr_lock);
2639 list_del(&cmd->list);
2640 spin_unlock_bh(&vscsi->intr_lock);
2641 cmd->se_cmd.se_tmr_req->response =
2642 TMR_FUNCTION_REJECTED;
2647 transport_send_check_condition_and_sense(&cmd->se_cmd, 0, 0);
2650 static void ibmvscsis_scheduler(struct work_struct *work)
2652 struct ibmvscsis_cmd *cmd = container_of(work, struct ibmvscsis_cmd,
2654 struct scsi_info *vscsi = cmd->adapter;
2656 spin_lock_bh(&vscsi->intr_lock);
2658 /* Remove from schedule_q */
2659 list_del(&cmd->list);
2661 /* Don't submit cmd if we're disconnecting */
2662 if (vscsi->flags & (SCHEDULE_DISCONNECT | DISCONNECT_SCHEDULED)) {
2663 ibmvscsis_free_cmd_resources(vscsi, cmd);
2665 /* ibmvscsis_disconnect might be waiting for us */
2666 if (list_empty(&vscsi->active_q) &&
2667 list_empty(&vscsi->schedule_q) &&
2668 (vscsi->flags & WAIT_FOR_IDLE)) {
2669 vscsi->flags &= ~WAIT_FOR_IDLE;
2670 complete(&vscsi->wait_idle);
2673 spin_unlock_bh(&vscsi->intr_lock);
2677 spin_unlock_bh(&vscsi->intr_lock);
2679 switch (cmd->type) {
2681 ibmvscsis_parse_cmd(vscsi, cmd);
2683 case TASK_MANAGEMENT:
2684 ibmvscsis_parse_task(vscsi, cmd);
2687 dev_err(&vscsi->dev, "scheduler, invalid cmd type %d\n",
2689 spin_lock_bh(&vscsi->intr_lock);
2690 ibmvscsis_free_cmd_resources(vscsi, cmd);
2691 spin_unlock_bh(&vscsi->intr_lock);
2696 static int ibmvscsis_alloc_cmds(struct scsi_info *vscsi, int num)
2698 struct ibmvscsis_cmd *cmd;
2701 INIT_LIST_HEAD(&vscsi->free_cmd);
2702 vscsi->cmd_pool = kcalloc(num, sizeof(struct ibmvscsis_cmd),
2704 if (!vscsi->cmd_pool)
2707 for (i = 0, cmd = (struct ibmvscsis_cmd *)vscsi->cmd_pool; i < num;
2709 cmd->adapter = vscsi;
2710 INIT_WORK(&cmd->work, ibmvscsis_scheduler);
2711 list_add_tail(&cmd->list, &vscsi->free_cmd);
2717 static void ibmvscsis_free_cmds(struct scsi_info *vscsi)
2719 kfree(vscsi->cmd_pool);
2720 vscsi->cmd_pool = NULL;
2721 INIT_LIST_HEAD(&vscsi->free_cmd);
2725 * ibmvscsis_service_wait_q() - Service Waiting Queue
2726 * @timer: Pointer to timer which has expired
2728 * This routine is called when the timer pops to service the waiting
2729 * queue. Elements on the queue have completed, their responses have been
2730 * copied to the client, but the client's response queue was full so
2731 * the queue message could not be sent. The routine grabs the proper locks
2732 * and calls send messages.
2734 * EXECUTION ENVIRONMENT:
2735 * called at interrupt level
2737 static enum hrtimer_restart ibmvscsis_service_wait_q(struct hrtimer *timer)
2739 struct timer_cb *p_timer = container_of(timer, struct timer_cb, timer);
2740 struct scsi_info *vscsi = container_of(p_timer, struct scsi_info,
2743 spin_lock_bh(&vscsi->intr_lock);
2744 p_timer->timer_pops += 1;
2745 p_timer->started = false;
2746 ibmvscsis_send_messages(vscsi);
2747 spin_unlock_bh(&vscsi->intr_lock);
2749 return HRTIMER_NORESTART;
2752 static long ibmvscsis_alloctimer(struct scsi_info *vscsi)
2754 struct timer_cb *p_timer;
2756 p_timer = &vscsi->rsp_q_timer;
2757 hrtimer_init(&p_timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2759 p_timer->timer.function = ibmvscsis_service_wait_q;
2760 p_timer->started = false;
2761 p_timer->timer_pops = 0;
2763 return ADAPT_SUCCESS;
2766 static void ibmvscsis_freetimer(struct scsi_info *vscsi)
2768 struct timer_cb *p_timer;
2770 p_timer = &vscsi->rsp_q_timer;
2772 (void)hrtimer_cancel(&p_timer->timer);
2774 p_timer->started = false;
2775 p_timer->timer_pops = 0;
2778 static irqreturn_t ibmvscsis_interrupt(int dummy, void *data)
2780 struct scsi_info *vscsi = data;
2782 vio_disable_interrupts(vscsi->dma_dev);
2783 tasklet_schedule(&vscsi->work_task);
2789 * ibmvscsis_enable_change_state() - Set new state based on enabled status
2790 * @vscsi: Pointer to our adapter structure
2792 * This function determines our new state now that we are enabled. This
2793 * may involve sending an Init Complete message to the client.
2795 * Must be called with interrupt lock held.
2797 static long ibmvscsis_enable_change_state(struct scsi_info *vscsi)
2800 long rc = ADAPT_SUCCESS;
2802 bytes = vscsi->cmd_q.size * PAGE_SIZE;
2803 rc = h_reg_crq(vscsi->dds.unit_id, vscsi->cmd_q.crq_token, bytes);
2804 if (rc == H_CLOSED || rc == H_SUCCESS) {
2805 vscsi->state = WAIT_CONNECTION;
2806 rc = ibmvscsis_establish_new_q(vscsi);
2809 if (rc != ADAPT_SUCCESS) {
2810 vscsi->state = ERR_DISCONNECTED;
2811 vscsi->flags |= RESPONSE_Q_DOWN;
2818 * ibmvscsis_create_command_q() - Create Command Queue
2819 * @vscsi: Pointer to our adapter structure
2820 * @num_cmds: Currently unused. In the future, may be used to determine
2821 * the size of the CRQ.
2823 * Allocates memory for command queue maps remote memory into an ioba
2824 * initializes the command response queue
2826 * EXECUTION ENVIRONMENT:
2827 * Process level only
2829 static long ibmvscsis_create_command_q(struct scsi_info *vscsi, int num_cmds)
2832 struct vio_dev *vdev = vscsi->dma_dev;
2834 /* We might support multiple pages in the future, but just 1 for now */
2837 vscsi->cmd_q.size = pages;
2839 vscsi->cmd_q.base_addr =
2840 (struct viosrp_crq *)get_zeroed_page(GFP_KERNEL);
2841 if (!vscsi->cmd_q.base_addr)
2844 vscsi->cmd_q.mask = ((uint)pages * CRQ_PER_PAGE) - 1;
2846 vscsi->cmd_q.crq_token = dma_map_single(&vdev->dev,
2847 vscsi->cmd_q.base_addr,
2848 PAGE_SIZE, DMA_BIDIRECTIONAL);
2849 if (dma_mapping_error(&vdev->dev, vscsi->cmd_q.crq_token)) {
2850 free_page((unsigned long)vscsi->cmd_q.base_addr);
2858 * ibmvscsis_destroy_command_q - Destroy Command Queue
2859 * @vscsi: Pointer to our adapter structure
2861 * Releases memory for command queue and unmaps mapped remote memory.
2863 * EXECUTION ENVIRONMENT:
2864 * Process level only
2866 static void ibmvscsis_destroy_command_q(struct scsi_info *vscsi)
2868 dma_unmap_single(&vscsi->dma_dev->dev, vscsi->cmd_q.crq_token,
2869 PAGE_SIZE, DMA_BIDIRECTIONAL);
2870 free_page((unsigned long)vscsi->cmd_q.base_addr);
2871 vscsi->cmd_q.base_addr = NULL;
2872 vscsi->state = NO_QUEUE;
2875 static u8 ibmvscsis_fast_fail(struct scsi_info *vscsi,
2876 struct ibmvscsis_cmd *cmd)
2878 struct iu_entry *iue = cmd->iue;
2879 struct se_cmd *se_cmd = &cmd->se_cmd;
2880 struct srp_cmd *srp = (struct srp_cmd *)iue->sbuf->buf;
2881 struct scsi_sense_hdr sshdr;
2882 u8 rc = se_cmd->scsi_status;
2884 if (vscsi->fast_fail && (READ_CMD(srp->cdb) || WRITE_CMD(srp->cdb)))
2885 if (scsi_normalize_sense(se_cmd->sense_buffer,
2886 se_cmd->scsi_sense_length, &sshdr))
2887 if (sshdr.sense_key == HARDWARE_ERROR &&
2888 (se_cmd->residual_count == 0 ||
2889 se_cmd->residual_count == se_cmd->data_length)) {
2891 cmd->flags |= CMD_FAST_FAIL;
2898 * srp_build_response() - Build an SRP response buffer
2899 * @vscsi: Pointer to our adapter structure
2900 * @cmd: Pointer to command for which to send the response
2901 * @len_p: Where to return the length of the IU response sent. This
2902 * is needed to construct the CRQ response.
2904 * Build the SRP response buffer and copy it to the client's memory space.
2906 static long srp_build_response(struct scsi_info *vscsi,
2907 struct ibmvscsis_cmd *cmd, uint *len_p)
2909 struct iu_entry *iue = cmd->iue;
2910 struct se_cmd *se_cmd = &cmd->se_cmd;
2911 struct srp_rsp *rsp;
2916 long rc = ADAPT_SUCCESS;
2918 spin_lock_bh(&vscsi->intr_lock);
2920 rsp = &vio_iu(iue)->srp.rsp;
2922 memset(rsp, 0, len);
2925 rsp->opcode = SRP_RSP;
2927 if (vscsi->credit > 0 && vscsi->state == SRP_PROCESSING)
2928 rsp->req_lim_delta = cpu_to_be32(vscsi->credit);
2930 rsp->req_lim_delta = cpu_to_be32(1 + vscsi->credit);
2931 rsp->tag = cmd->rsp.tag;
2934 if (cmd->type == SCSI_CDB) {
2935 rsp->status = ibmvscsis_fast_fail(vscsi, cmd);
2937 pr_debug("build_resp: cmd %p, scsi status %d\n", cmd,
2939 ibmvscsis_determine_resid(se_cmd, rsp);
2940 if (se_cmd->scsi_sense_length && se_cmd->sense_buffer) {
2941 rsp->sense_data_len =
2942 cpu_to_be32(se_cmd->scsi_sense_length);
2943 rsp->flags |= SRP_RSP_FLAG_SNSVALID;
2944 len += se_cmd->scsi_sense_length;
2945 memcpy(data, se_cmd->sense_buffer,
2946 se_cmd->scsi_sense_length);
2948 rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
2950 } else if (cmd->flags & CMD_FAST_FAIL) {
2951 pr_debug("build_resp: cmd %p, fast fail\n", cmd);
2952 rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
2955 rsp->sol_not = (cmd->rsp.sol_not & SCSOLNT) >>
2959 /* this is task management */
2961 rsp->resp_data_len = cpu_to_be32(4);
2962 rsp->flags |= SRP_RSP_FLAG_RSPVALID;
2964 switch (se_cmd->se_tmr_req->response) {
2965 case TMR_FUNCTION_COMPLETE:
2966 case TMR_TASK_DOES_NOT_EXIST:
2967 rsp_code = SRP_TASK_MANAGEMENT_FUNCTION_COMPLETE;
2968 rsp->sol_not = (cmd->rsp.sol_not & SCSOLNT) >>
2971 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
2972 case TMR_LUN_DOES_NOT_EXIST:
2973 rsp_code = SRP_TASK_MANAGEMENT_FUNCTION_NOT_SUPPORTED;
2974 rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
2977 case TMR_FUNCTION_FAILED:
2978 case TMR_FUNCTION_REJECTED:
2980 rsp_code = SRP_TASK_MANAGEMENT_FUNCTION_FAILED;
2981 rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
2986 tsk_status = (u32 *)data;
2987 *tsk_status = cpu_to_be32(rsp_code);
2988 data = (char *)(tsk_status + 1);
2993 rc = h_copy_rdma(len, vscsi->dds.window[LOCAL].liobn, iue->sbuf->dma,
2994 vscsi->dds.window[REMOTE].liobn,
2995 be64_to_cpu(iue->remote_token));
3003 if (connection_broken(vscsi))
3004 vscsi->flags |= RESPONSE_Q_DOWN | CLIENT_FAILED;
3006 dev_err(&vscsi->dev, "build_response: error copying to client, rc %ld, flags 0x%x, state 0x%hx\n",
3007 rc, vscsi->flags, vscsi->state);
3012 dev_err(&vscsi->dev, "build_response: error copying to client, rc %ld\n",
3017 spin_unlock_bh(&vscsi->intr_lock);
3022 static int ibmvscsis_rdma(struct ibmvscsis_cmd *cmd, struct scatterlist *sg,
3023 int nsg, struct srp_direct_buf *md, int nmd,
3024 enum dma_data_direction dir, unsigned int bytes)
3026 struct iu_entry *iue = cmd->iue;
3027 struct srp_target *target = iue->target;
3028 struct scsi_info *vscsi = target->ldata;
3029 struct scatterlist *sgp;
3030 dma_addr_t client_ioba, server_ioba;
3032 ulong client_len, server_len;
3047 if (client_len == 0) {
3048 if (md_idx >= nmd) {
3049 dev_err(&vscsi->dev, "rdma: ran out of client memory descriptors\n");
3053 client_ioba = be64_to_cpu(md[md_idx].va);
3054 client_len = be32_to_cpu(md[md_idx].len);
3056 if (server_len == 0) {
3058 dev_err(&vscsi->dev, "rdma: ran out of scatter/gather list\n");
3062 server_ioba = sg_dma_address(sgp);
3063 server_len = sg_dma_len(sgp);
3068 if (buf_len > client_len)
3069 buf_len = client_len;
3071 if (buf_len > server_len)
3072 buf_len = server_len;
3074 if (buf_len > max_vdma_size)
3075 buf_len = max_vdma_size;
3077 if (dir == DMA_TO_DEVICE) {
3078 /* read from client */
3079 rc = h_copy_rdma(buf_len,
3080 vscsi->dds.window[REMOTE].liobn,
3082 vscsi->dds.window[LOCAL].liobn,
3085 /* The h_copy_rdma will cause phyp, running in another
3086 * partition, to read memory, so we need to make sure
3087 * the data has been written out, hence these syncs.
3089 /* ensure that everything is in memory */
3091 /* ensure that memory has been made visible */
3093 rc = h_copy_rdma(buf_len,
3094 vscsi->dds.window[LOCAL].liobn,
3096 vscsi->dds.window[REMOTE].liobn,
3105 if (connection_broken(vscsi)) {
3106 spin_lock_bh(&vscsi->intr_lock);
3108 (RESPONSE_Q_DOWN | CLIENT_FAILED);
3109 spin_unlock_bh(&vscsi->intr_lock);
3111 dev_err(&vscsi->dev, "rdma: h_copy_rdma failed, rc %ld\n",
3116 dev_err(&vscsi->dev, "rdma: unknown error %ld from h_copy_rdma\n",
3124 client_len -= buf_len;
3125 if (client_len == 0)
3128 client_ioba += buf_len;
3130 server_len -= buf_len;
3131 if (server_len == 0)
3134 server_ioba += buf_len;
3145 * ibmvscsis_handle_crq() - Handle CRQ
3146 * @data: Pointer to our adapter structure
3148 * Read the command elements from the command queue and copy the payloads
3149 * associated with the command elements to local memory and execute the
3152 * Note: this is an edge triggered interrupt. It can not be shared.
3154 static void ibmvscsis_handle_crq(unsigned long data)
3156 struct scsi_info *vscsi = (struct scsi_info *)data;
3157 struct viosrp_crq *crq;
3162 spin_lock_bh(&vscsi->intr_lock);
3164 pr_debug("got interrupt\n");
3167 * if we are in a path where we are waiting for all pending commands
3168 * to complete because we received a transport event and anything in
3169 * the command queue is for a new connection, do nothing
3171 if (TARGET_STOP(vscsi)) {
3172 vio_enable_interrupts(vscsi->dma_dev);
3174 pr_debug("handle_crq, don't process: flags 0x%x, state 0x%hx\n",
3175 vscsi->flags, vscsi->state);
3176 spin_unlock_bh(&vscsi->intr_lock);
3180 rc = vscsi->flags & SCHEDULE_DISCONNECT;
3181 crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
3187 * These are edege triggered interrupts. After dropping out of
3188 * the while loop, the code must check for work since an
3189 * interrupt could be lost, and an elment be left on the queue,
3193 vscsi->cmd_q.index =
3194 (vscsi->cmd_q.index + 1) & vscsi->cmd_q.mask;
3197 rc = ibmvscsis_parse_command(vscsi, crq);
3199 if ((uint)crq->valid == VALID_TRANS_EVENT) {
3201 * must service the transport layer events even
3202 * in an error state, dont break out until all
3203 * the consecutive transport events have been
3206 rc = ibmvscsis_trans_event(vscsi, crq);
3207 } else if (vscsi->flags & TRANS_EVENT) {
3209 * if a transport event has occurred leave
3210 * everything but transport events on the queue
3212 * need to decrement the queue index so we can
3213 * look at the element again
3215 if (vscsi->cmd_q.index)
3216 vscsi->cmd_q.index -= 1;
3219 * index is at 0 it just wrapped.
3220 * have it index last element in q
3222 vscsi->cmd_q.index = vscsi->cmd_q.mask;
3227 crq->valid = INVALIDATE_CMD_RESP_EL;
3229 crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
3236 vio_enable_interrupts(vscsi->dma_dev);
3238 pr_debug("handle_crq, reenabling interrupts\n");
3245 pr_debug("handle_crq, error: flags 0x%x, state 0x%hx, crq index 0x%x\n",
3246 vscsi->flags, vscsi->state, vscsi->cmd_q.index);
3249 pr_debug("Leaving handle_crq: schedule_q empty %d, flags 0x%x, state 0x%hx\n",
3250 (int)list_empty(&vscsi->schedule_q), vscsi->flags,
3253 spin_unlock_bh(&vscsi->intr_lock);
3256 static int ibmvscsis_probe(struct vio_dev *vdev,
3257 const struct vio_device_id *id)
3259 struct scsi_info *vscsi;
3264 vscsi = kzalloc(sizeof(*vscsi), GFP_KERNEL);
3267 pr_err("probe: allocation of adapter failed\n");
3271 vscsi->dma_dev = vdev;
3272 vscsi->dev = vdev->dev;
3273 INIT_LIST_HEAD(&vscsi->schedule_q);
3274 INIT_LIST_HEAD(&vscsi->waiting_rsp);
3275 INIT_LIST_HEAD(&vscsi->active_q);
3277 snprintf(vscsi->tport.tport_name, IBMVSCSIS_NAMELEN, "%s",
3278 dev_name(&vdev->dev));
3280 pr_debug("probe tport_name: %s\n", vscsi->tport.tport_name);
3282 rc = read_dma_window(vscsi);
3285 pr_debug("Probe: liobn 0x%x, riobn 0x%x\n",
3286 vscsi->dds.window[LOCAL].liobn,
3287 vscsi->dds.window[REMOTE].liobn);
3289 strcpy(vscsi->eye, "VSCSI ");
3290 strncat(vscsi->eye, vdev->name, MAX_EYE);
3292 vscsi->dds.unit_id = vdev->unit_address;
3293 strncpy(vscsi->dds.partition_name, partition_name,
3294 sizeof(vscsi->dds.partition_name));
3295 vscsi->dds.partition_num = partition_number;
3297 spin_lock_bh(&ibmvscsis_dev_lock);
3298 list_add_tail(&vscsi->list, &ibmvscsis_dev_list);
3299 spin_unlock_bh(&ibmvscsis_dev_lock);
3302 * TBD: How do we determine # of cmds to request? Do we know how
3303 * many "children" we have?
3305 vscsi->request_limit = INITIAL_SRP_LIMIT;
3306 rc = srp_target_alloc(&vscsi->target, &vdev->dev, vscsi->request_limit,
3311 vscsi->target.ldata = vscsi;
3313 rc = ibmvscsis_alloc_cmds(vscsi, vscsi->request_limit);
3315 dev_err(&vscsi->dev, "alloc_cmds failed, rc %d, num %d\n",
3316 rc, vscsi->request_limit);
3321 * Note: the lock is used in freeing timers, so must initialize
3322 * first so that ordering in case of error is correct.
3324 spin_lock_init(&vscsi->intr_lock);
3326 rc = ibmvscsis_alloctimer(vscsi);
3328 dev_err(&vscsi->dev, "probe: alloctimer failed, rc %d\n", rc);
3332 rc = ibmvscsis_create_command_q(vscsi, 256);
3334 dev_err(&vscsi->dev, "probe: create_command_q failed, rc %d\n",
3339 vscsi->map_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
3340 if (!vscsi->map_buf) {
3342 dev_err(&vscsi->dev, "probe: allocating cmd buffer failed\n");
3346 vscsi->map_ioba = dma_map_single(&vdev->dev, vscsi->map_buf, PAGE_SIZE,
3348 if (dma_mapping_error(&vdev->dev, vscsi->map_ioba)) {
3350 dev_err(&vscsi->dev, "probe: error mapping command buffer\n");
3354 hrc = h_vioctl(vscsi->dds.unit_id, H_GET_PARTNER_INFO,
3355 (u64)vscsi->map_ioba | ((u64)PAGE_SIZE << 32), 0, 0, 0,
3357 if (hrc == H_SUCCESS)
3358 vscsi->client_data.partition_number =
3359 be64_to_cpu(*(u64 *)vscsi->map_buf);
3361 * We expect the VIOCTL to fail if we're configured as "any
3362 * client can connect" and the client isn't activated yet.
3363 * We'll make the call again when he sends an init msg.
3365 pr_debug("probe hrc %ld, client partition num %d\n",
3366 hrc, vscsi->client_data.partition_number);
3368 tasklet_init(&vscsi->work_task, ibmvscsis_handle_crq,
3369 (unsigned long)vscsi);
3371 init_completion(&vscsi->wait_idle);
3372 init_completion(&vscsi->unconfig);
3374 snprintf(wq_name, 24, "ibmvscsis%s", dev_name(&vdev->dev));
3375 vscsi->work_q = create_workqueue(wq_name);
3376 if (!vscsi->work_q) {
3378 dev_err(&vscsi->dev, "create_workqueue failed\n");
3382 rc = request_irq(vdev->irq, ibmvscsis_interrupt, 0, "ibmvscsis", vscsi);
3385 dev_err(&vscsi->dev, "probe: request_irq failed, rc %d\n", rc);
3389 vscsi->state = WAIT_ENABLED;
3391 dev_set_drvdata(&vdev->dev, vscsi);
3396 destroy_workqueue(vscsi->work_q);
3398 dma_unmap_single(&vdev->dev, vscsi->map_ioba, PAGE_SIZE,
3401 kfree(vscsi->map_buf);
3403 tasklet_kill(&vscsi->work_task);
3404 ibmvscsis_unregister_command_q(vscsi);
3405 ibmvscsis_destroy_command_q(vscsi);
3407 ibmvscsis_freetimer(vscsi);
3409 ibmvscsis_free_cmds(vscsi);
3411 srp_target_free(&vscsi->target);
3413 spin_lock_bh(&ibmvscsis_dev_lock);
3414 list_del(&vscsi->list);
3415 spin_unlock_bh(&ibmvscsis_dev_lock);
3422 static int ibmvscsis_remove(struct vio_dev *vdev)
3424 struct scsi_info *vscsi = dev_get_drvdata(&vdev->dev);
3426 pr_debug("remove (%s)\n", dev_name(&vscsi->dma_dev->dev));
3428 spin_lock_bh(&vscsi->intr_lock);
3429 ibmvscsis_post_disconnect(vscsi, UNCONFIGURING, 0);
3430 vscsi->flags |= CFG_SLEEPING;
3431 spin_unlock_bh(&vscsi->intr_lock);
3432 wait_for_completion(&vscsi->unconfig);
3434 vio_disable_interrupts(vdev);
3435 free_irq(vdev->irq, vscsi);
3436 destroy_workqueue(vscsi->work_q);
3437 dma_unmap_single(&vdev->dev, vscsi->map_ioba, PAGE_SIZE,
3439 kfree(vscsi->map_buf);
3440 tasklet_kill(&vscsi->work_task);
3441 ibmvscsis_destroy_command_q(vscsi);
3442 ibmvscsis_freetimer(vscsi);
3443 ibmvscsis_free_cmds(vscsi);
3444 srp_target_free(&vscsi->target);
3445 spin_lock_bh(&ibmvscsis_dev_lock);
3446 list_del(&vscsi->list);
3447 spin_unlock_bh(&ibmvscsis_dev_lock);
3453 static ssize_t system_id_show(struct device *dev,
3454 struct device_attribute *attr, char *buf)
3456 return snprintf(buf, PAGE_SIZE, "%s\n", system_id);
3459 static ssize_t partition_number_show(struct device *dev,
3460 struct device_attribute *attr, char *buf)
3462 return snprintf(buf, PAGE_SIZE, "%x\n", partition_number);
3465 static ssize_t unit_address_show(struct device *dev,
3466 struct device_attribute *attr, char *buf)
3468 struct scsi_info *vscsi = container_of(dev, struct scsi_info, dev);
3470 return snprintf(buf, PAGE_SIZE, "%x\n", vscsi->dma_dev->unit_address);
3473 static int ibmvscsis_get_system_info(void)
3475 struct device_node *rootdn, *vdevdn;
3476 const char *id, *model, *name;
3479 rootdn = of_find_node_by_path("/");
3483 model = of_get_property(rootdn, "model", NULL);
3484 id = of_get_property(rootdn, "system-id", NULL);
3486 snprintf(system_id, sizeof(system_id), "%s-%s", model, id);
3488 name = of_get_property(rootdn, "ibm,partition-name", NULL);
3490 strncpy(partition_name, name, sizeof(partition_name));
3492 num = of_get_property(rootdn, "ibm,partition-no", NULL);
3494 partition_number = of_read_number(num, 1);
3496 of_node_put(rootdn);
3498 vdevdn = of_find_node_by_path("/vdevice");
3502 mvds = of_get_property(vdevdn, "ibm,max-virtual-dma-size",
3505 max_vdma_size = *mvds;
3506 of_node_put(vdevdn);
3512 static char *ibmvscsis_get_fabric_name(void)
3517 static char *ibmvscsis_get_fabric_wwn(struct se_portal_group *se_tpg)
3519 struct ibmvscsis_tport *tport =
3520 container_of(se_tpg, struct ibmvscsis_tport, se_tpg);
3522 return tport->tport_name;
3525 static u16 ibmvscsis_get_tag(struct se_portal_group *se_tpg)
3527 struct ibmvscsis_tport *tport =
3528 container_of(se_tpg, struct ibmvscsis_tport, se_tpg);
3530 return tport->tport_tpgt;
3533 static u32 ibmvscsis_get_default_depth(struct se_portal_group *se_tpg)
3538 static int ibmvscsis_check_true(struct se_portal_group *se_tpg)
3543 static int ibmvscsis_check_false(struct se_portal_group *se_tpg)
3548 static u32 ibmvscsis_tpg_get_inst_index(struct se_portal_group *se_tpg)
3553 static int ibmvscsis_check_stop_free(struct se_cmd *se_cmd)
3555 return target_put_sess_cmd(se_cmd);
3558 static void ibmvscsis_release_cmd(struct se_cmd *se_cmd)
3560 struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
3562 struct scsi_info *vscsi = cmd->adapter;
3564 spin_lock_bh(&vscsi->intr_lock);
3565 /* Remove from active_q */
3566 list_move_tail(&cmd->list, &vscsi->waiting_rsp);
3567 ibmvscsis_send_messages(vscsi);
3568 spin_unlock_bh(&vscsi->intr_lock);
3571 static u32 ibmvscsis_sess_get_index(struct se_session *se_sess)
3576 static int ibmvscsis_write_pending(struct se_cmd *se_cmd)
3578 struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
3580 struct iu_entry *iue = cmd->iue;
3583 rc = srp_transfer_data(cmd, &vio_iu(iue)->srp.cmd, ibmvscsis_rdma,
3586 pr_err("srp_transfer_data() failed: %d\n", rc);
3590 * We now tell TCM to add this WRITE CDB directly into the TCM storage
3591 * object execution queue.
3593 target_execute_cmd(se_cmd);
3597 static int ibmvscsis_write_pending_status(struct se_cmd *se_cmd)
3602 static void ibmvscsis_set_default_node_attrs(struct se_node_acl *nacl)
3606 static int ibmvscsis_get_cmd_state(struct se_cmd *se_cmd)
3611 static int ibmvscsis_queue_data_in(struct se_cmd *se_cmd)
3613 struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
3615 struct iu_entry *iue = cmd->iue;
3616 struct scsi_info *vscsi = cmd->adapter;
3621 rc = srp_transfer_data(cmd, &vio_iu(iue)->srp.cmd, ibmvscsis_rdma, 1,
3624 pr_err("srp_transfer_data failed: %d\n", rc);
3625 sd = se_cmd->sense_buffer;
3626 se_cmd->scsi_sense_length = 18;
3627 memset(se_cmd->sense_buffer, 0, se_cmd->scsi_sense_length);
3628 /* Logical Unit Communication Time-out asc/ascq = 0x0801 */
3629 scsi_build_sense_buffer(0, se_cmd->sense_buffer, MEDIUM_ERROR,
3633 srp_build_response(vscsi, cmd, &len);
3634 cmd->rsp.format = SRP_FORMAT;
3640 static int ibmvscsis_queue_status(struct se_cmd *se_cmd)
3642 struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
3644 struct scsi_info *vscsi = cmd->adapter;
3647 pr_debug("queue_status %p\n", se_cmd);
3649 srp_build_response(vscsi, cmd, &len);
3650 cmd->rsp.format = SRP_FORMAT;
3656 static void ibmvscsis_queue_tm_rsp(struct se_cmd *se_cmd)
3658 struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
3660 struct scsi_info *vscsi = cmd->adapter;
3663 pr_debug("queue_tm_rsp %p, status %d\n",
3664 se_cmd, (int)se_cmd->se_tmr_req->response);
3666 srp_build_response(vscsi, cmd, &len);
3667 cmd->rsp.format = SRP_FORMAT;
3671 static void ibmvscsis_aborted_task(struct se_cmd *se_cmd)
3673 /* TBD: What (if anything) should we do here? */
3674 pr_debug("ibmvscsis_aborted_task %p\n", se_cmd);
3677 static struct se_wwn *ibmvscsis_make_tport(struct target_fabric_configfs *tf,
3678 struct config_group *group,
3681 struct ibmvscsis_tport *tport;
3683 tport = ibmvscsis_lookup_port(name);
3685 tport->tport_proto_id = SCSI_PROTOCOL_SRP;
3686 pr_debug("make_tport(%s), pointer:%p, tport_id:%x\n",
3687 name, tport, tport->tport_proto_id);
3688 return &tport->tport_wwn;
3691 return ERR_PTR(-EINVAL);
3694 static void ibmvscsis_drop_tport(struct se_wwn *wwn)
3696 struct ibmvscsis_tport *tport = container_of(wwn,
3697 struct ibmvscsis_tport,
3700 pr_debug("drop_tport(%s)\n",
3701 config_item_name(&tport->tport_wwn.wwn_group.cg_item));
3704 static struct se_portal_group *ibmvscsis_make_tpg(struct se_wwn *wwn,
3705 struct config_group *group,
3708 struct ibmvscsis_tport *tport =
3709 container_of(wwn, struct ibmvscsis_tport, tport_wwn);
3712 tport->releasing = false;
3714 rc = core_tpg_register(&tport->tport_wwn, &tport->se_tpg,
3715 tport->tport_proto_id);
3719 return &tport->se_tpg;
3722 static void ibmvscsis_drop_tpg(struct se_portal_group *se_tpg)
3724 struct ibmvscsis_tport *tport = container_of(se_tpg,
3725 struct ibmvscsis_tport,
3728 tport->releasing = true;
3729 tport->enabled = false;
3732 * Release the virtual I_T Nexus for this ibmvscsis TPG
3734 ibmvscsis_drop_nexus(tport);
3736 * Deregister the se_tpg from TCM..
3738 core_tpg_deregister(se_tpg);
3741 static ssize_t ibmvscsis_wwn_version_show(struct config_item *item,
3744 return scnprintf(page, PAGE_SIZE, "%s\n", IBMVSCSIS_VERSION);
3746 CONFIGFS_ATTR_RO(ibmvscsis_wwn_, version);
3748 static struct configfs_attribute *ibmvscsis_wwn_attrs[] = {
3749 &ibmvscsis_wwn_attr_version,
3753 static ssize_t ibmvscsis_tpg_enable_show(struct config_item *item,
3756 struct se_portal_group *se_tpg = to_tpg(item);
3757 struct ibmvscsis_tport *tport = container_of(se_tpg,
3758 struct ibmvscsis_tport,
3761 return snprintf(page, PAGE_SIZE, "%d\n", (tport->enabled) ? 1 : 0);
3764 static ssize_t ibmvscsis_tpg_enable_store(struct config_item *item,
3765 const char *page, size_t count)
3767 struct se_portal_group *se_tpg = to_tpg(item);
3768 struct ibmvscsis_tport *tport = container_of(se_tpg,
3769 struct ibmvscsis_tport,
3771 struct scsi_info *vscsi = container_of(tport, struct scsi_info, tport);
3776 rc = kstrtoul(page, 0, &tmp);
3778 pr_err("Unable to extract srpt_tpg_store_enable\n");
3782 if ((tmp != 0) && (tmp != 1)) {
3783 pr_err("Illegal value for srpt_tpg_store_enable\n");
3788 spin_lock_bh(&vscsi->intr_lock);
3789 tport->enabled = true;
3790 lrc = ibmvscsis_enable_change_state(vscsi);
3792 pr_err("enable_change_state failed, rc %ld state %d\n",
3794 spin_unlock_bh(&vscsi->intr_lock);
3796 spin_lock_bh(&vscsi->intr_lock);
3797 tport->enabled = false;
3798 /* This simulates the server going down */
3799 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
3800 spin_unlock_bh(&vscsi->intr_lock);
3803 pr_debug("tpg_enable_store, tmp %ld, state %d\n", tmp, vscsi->state);
3807 CONFIGFS_ATTR(ibmvscsis_tpg_, enable);
3809 static struct configfs_attribute *ibmvscsis_tpg_attrs[] = {
3810 &ibmvscsis_tpg_attr_enable,
3814 static const struct target_core_fabric_ops ibmvscsis_ops = {
3815 .module = THIS_MODULE,
3816 .name = "ibmvscsis",
3817 .get_fabric_name = ibmvscsis_get_fabric_name,
3818 .tpg_get_wwn = ibmvscsis_get_fabric_wwn,
3819 .tpg_get_tag = ibmvscsis_get_tag,
3820 .tpg_get_default_depth = ibmvscsis_get_default_depth,
3821 .tpg_check_demo_mode = ibmvscsis_check_true,
3822 .tpg_check_demo_mode_cache = ibmvscsis_check_true,
3823 .tpg_check_demo_mode_write_protect = ibmvscsis_check_false,
3824 .tpg_check_prod_mode_write_protect = ibmvscsis_check_false,
3825 .tpg_get_inst_index = ibmvscsis_tpg_get_inst_index,
3826 .check_stop_free = ibmvscsis_check_stop_free,
3827 .release_cmd = ibmvscsis_release_cmd,
3828 .sess_get_index = ibmvscsis_sess_get_index,
3829 .write_pending = ibmvscsis_write_pending,
3830 .write_pending_status = ibmvscsis_write_pending_status,
3831 .set_default_node_attributes = ibmvscsis_set_default_node_attrs,
3832 .get_cmd_state = ibmvscsis_get_cmd_state,
3833 .queue_data_in = ibmvscsis_queue_data_in,
3834 .queue_status = ibmvscsis_queue_status,
3835 .queue_tm_rsp = ibmvscsis_queue_tm_rsp,
3836 .aborted_task = ibmvscsis_aborted_task,
3838 * Setup function pointers for logic in target_core_fabric_configfs.c
3840 .fabric_make_wwn = ibmvscsis_make_tport,
3841 .fabric_drop_wwn = ibmvscsis_drop_tport,
3842 .fabric_make_tpg = ibmvscsis_make_tpg,
3843 .fabric_drop_tpg = ibmvscsis_drop_tpg,
3845 .tfc_wwn_attrs = ibmvscsis_wwn_attrs,
3846 .tfc_tpg_base_attrs = ibmvscsis_tpg_attrs,
3849 static void ibmvscsis_dev_release(struct device *dev) {};
3851 static struct class_attribute ibmvscsis_class_attrs[] = {
3855 static struct device_attribute dev_attr_system_id =
3856 __ATTR(system_id, S_IRUGO, system_id_show, NULL);
3858 static struct device_attribute dev_attr_partition_number =
3859 __ATTR(partition_number, S_IRUGO, partition_number_show, NULL);
3861 static struct device_attribute dev_attr_unit_address =
3862 __ATTR(unit_address, S_IRUGO, unit_address_show, NULL);
3864 static struct attribute *ibmvscsis_dev_attrs[] = {
3865 &dev_attr_system_id.attr,
3866 &dev_attr_partition_number.attr,
3867 &dev_attr_unit_address.attr,
3869 ATTRIBUTE_GROUPS(ibmvscsis_dev);
3871 static struct class ibmvscsis_class = {
3872 .name = "ibmvscsis",
3873 .dev_release = ibmvscsis_dev_release,
3874 .class_attrs = ibmvscsis_class_attrs,
3875 .dev_groups = ibmvscsis_dev_groups,
3878 static struct vio_device_id ibmvscsis_device_table[] = {
3879 { "v-scsi-host", "IBM,v-scsi-host" },
3882 MODULE_DEVICE_TABLE(vio, ibmvscsis_device_table);
3884 static struct vio_driver ibmvscsis_driver = {
3885 .name = "ibmvscsis",
3886 .id_table = ibmvscsis_device_table,
3887 .probe = ibmvscsis_probe,
3888 .remove = ibmvscsis_remove,
3892 * ibmvscsis_init() - Kernel Module initialization
3894 * Note: vio_register_driver() registers callback functions, and at least one
3895 * of those callback functions calls TCM - Linux IO Target Subsystem, thus
3896 * the SCSI Target template must be registered before vio_register_driver()
3899 static int __init ibmvscsis_init(void)
3903 rc = ibmvscsis_get_system_info();
3905 pr_err("rc %d from get_system_info\n", rc);
3909 rc = class_register(&ibmvscsis_class);
3911 pr_err("failed class register\n");
3915 rc = target_register_template(&ibmvscsis_ops);
3917 pr_err("rc %d from target_register_template\n", rc);
3918 goto unregister_class;
3921 rc = vio_register_driver(&ibmvscsis_driver);
3923 pr_err("rc %d from vio_register_driver\n", rc);
3924 goto unregister_target;
3930 target_unregister_template(&ibmvscsis_ops);
3932 class_unregister(&ibmvscsis_class);
3937 static void __exit ibmvscsis_exit(void)
3939 pr_info("Unregister IBM virtual SCSI host driver\n");
3940 vio_unregister_driver(&ibmvscsis_driver);
3941 target_unregister_template(&ibmvscsis_ops);
3942 class_unregister(&ibmvscsis_class);
3945 MODULE_DESCRIPTION("IBMVSCSIS fabric driver");
3946 MODULE_AUTHOR("Bryant G. Ly and Michael Cyr");
3947 MODULE_LICENSE("GPL");
3948 MODULE_VERSION(IBMVSCSIS_VERSION);
3949 module_init(ibmvscsis_init);
3950 module_exit(ibmvscsis_exit);